xref: /linux/fs/reiserfs/namei.c (revision 72bea132f3680ee51e7ed2cee62892b6f5121909)
1 /*
2  * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3  *
4  * Trivial changes by Alan Cox to remove EHASHCOLLISION for compatibility
5  *
6  * Trivial Changes:
7  * Rights granted to Hans Reiser to redistribute under other terms providing
8  * he accepts all liability including but not limited to patent, fitness
9  * for purpose, and direct or indirect claims arising from failure to perform.
10  *
11  * NO WARRANTY
12  */
13 
14 #include <linux/time.h>
15 #include <linux/bitops.h>
16 #include <linux/slab.h>
17 #include "reiserfs.h"
18 #include "acl.h"
19 #include "xattr.h"
20 #include <linux/quotaops.h>
21 
22 #define INC_DIR_INODE_NLINK(i) if (i->i_nlink != 1) { inc_nlink(i); if (i->i_nlink >= REISERFS_LINK_MAX) set_nlink(i, 1); }
23 #define DEC_DIR_INODE_NLINK(i) if (i->i_nlink != 1) drop_nlink(i);
24 
25 /*
26  * directory item contains array of entry headers. This performs
27  * binary search through that array
28  */
29 static int bin_search_in_dir_item(struct reiserfs_dir_entry *de, loff_t off)
30 {
31 	struct item_head *ih = de->de_ih;
32 	struct reiserfs_de_head *deh = de->de_deh;
33 	int rbound, lbound, j;
34 
35 	lbound = 0;
36 	rbound = ih_entry_count(ih) - 1;
37 
38 	for (j = (rbound + lbound) / 2; lbound <= rbound;
39 	     j = (rbound + lbound) / 2) {
40 		if (off < deh_offset(deh + j)) {
41 			rbound = j - 1;
42 			continue;
43 		}
44 		if (off > deh_offset(deh + j)) {
45 			lbound = j + 1;
46 			continue;
47 		}
48 		/* this is not name found, but matched third key component */
49 		de->de_entry_num = j;
50 		return NAME_FOUND;
51 	}
52 
53 	de->de_entry_num = lbound;
54 	return NAME_NOT_FOUND;
55 }
56 
57 /*
58  * comment?  maybe something like set de to point to what the path points to?
59  */
60 static inline void set_de_item_location(struct reiserfs_dir_entry *de,
61 					struct treepath *path)
62 {
63 	de->de_bh = get_last_bh(path);
64 	de->de_ih = tp_item_head(path);
65 	de->de_deh = B_I_DEH(de->de_bh, de->de_ih);
66 	de->de_item_num = PATH_LAST_POSITION(path);
67 }
68 
69 /*
70  * de_bh, de_ih, de_deh (points to first element of array), de_item_num is set
71  */
72 inline void set_de_name_and_namelen(struct reiserfs_dir_entry *de)
73 {
74 	struct reiserfs_de_head *deh = de->de_deh + de->de_entry_num;
75 
76 	BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih));
77 
78 	de->de_entrylen = entry_length(de->de_bh, de->de_ih, de->de_entry_num);
79 	de->de_namelen = de->de_entrylen - (de_with_sd(deh) ? SD_SIZE : 0);
80 	de->de_name = ih_item_body(de->de_bh, de->de_ih) + deh_location(deh);
81 	if (de->de_name[de->de_namelen - 1] == 0)
82 		de->de_namelen = strlen(de->de_name);
83 }
84 
85 /* what entry points to */
86 static inline void set_de_object_key(struct reiserfs_dir_entry *de)
87 {
88 	BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih));
89 	de->de_dir_id = deh_dir_id(&de->de_deh[de->de_entry_num]);
90 	de->de_objectid = deh_objectid(&de->de_deh[de->de_entry_num]);
91 }
92 
93 static inline void store_de_entry_key(struct reiserfs_dir_entry *de)
94 {
95 	struct reiserfs_de_head *deh = de->de_deh + de->de_entry_num;
96 
97 	BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih));
98 
99 	/* store key of the found entry */
100 	de->de_entry_key.version = KEY_FORMAT_3_5;
101 	de->de_entry_key.on_disk_key.k_dir_id =
102 	    le32_to_cpu(de->de_ih->ih_key.k_dir_id);
103 	de->de_entry_key.on_disk_key.k_objectid =
104 	    le32_to_cpu(de->de_ih->ih_key.k_objectid);
105 	set_cpu_key_k_offset(&de->de_entry_key, deh_offset(deh));
106 	set_cpu_key_k_type(&de->de_entry_key, TYPE_DIRENTRY);
107 }
108 
109 /*
110  * We assign a key to each directory item, and place multiple entries in a
111  * single directory item.  A directory item has a key equal to the key of
112  * the first directory entry in it.
113 
114  * This function first calls search_by_key, then, if item whose first entry
115  * matches is not found it looks for the entry inside directory item found
116  * by search_by_key. Fills the path to the entry, and to the entry position
117  * in the item
118  */
119 /* The function is NOT SCHEDULE-SAFE! */
120 int search_by_entry_key(struct super_block *sb, const struct cpu_key *key,
121 			struct treepath *path, struct reiserfs_dir_entry *de)
122 {
123 	int retval;
124 
125 	retval = search_item(sb, key, path);
126 	switch (retval) {
127 	case ITEM_NOT_FOUND:
128 		if (!PATH_LAST_POSITION(path)) {
129 			reiserfs_error(sb, "vs-7000", "search_by_key "
130 				       "returned item position == 0");
131 			pathrelse(path);
132 			return IO_ERROR;
133 		}
134 		PATH_LAST_POSITION(path)--;
135 		break;
136 
137 	case ITEM_FOUND:
138 		break;
139 
140 	case IO_ERROR:
141 		return retval;
142 
143 	default:
144 		pathrelse(path);
145 		reiserfs_error(sb, "vs-7002", "no path to here");
146 		return IO_ERROR;
147 	}
148 
149 	set_de_item_location(de, path);
150 
151 #ifdef CONFIG_REISERFS_CHECK
152 	if (!is_direntry_le_ih(de->de_ih) ||
153 	    COMP_SHORT_KEYS(&de->de_ih->ih_key, key)) {
154 		print_block(de->de_bh, 0, -1, -1);
155 		reiserfs_panic(sb, "vs-7005", "found item %h is not directory "
156 			       "item or does not belong to the same directory "
157 			       "as key %K", de->de_ih, key);
158 	}
159 #endif				/* CONFIG_REISERFS_CHECK */
160 
161 	/*
162 	 * binary search in directory item by third component of the
163 	 * key. sets de->de_entry_num of de
164 	 */
165 	retval = bin_search_in_dir_item(de, cpu_key_k_offset(key));
166 	path->pos_in_item = de->de_entry_num;
167 	if (retval != NAME_NOT_FOUND) {
168 		/*
169 		 * ugly, but rename needs de_bh, de_deh, de_name,
170 		 * de_namelen, de_objectid set
171 		 */
172 		set_de_name_and_namelen(de);
173 		set_de_object_key(de);
174 	}
175 	return retval;
176 }
177 
178 /* Keyed 32-bit hash function using TEA in a Davis-Meyer function */
179 
180 /*
181  * The third component is hashed, and you can choose from more than
182  * one hash function.  Per directory hashes are not yet implemented
183  * but are thought about. This function should be moved to hashes.c
184  * Jedi, please do so.  -Hans
185  */
186 static __u32 get_third_component(struct super_block *s,
187 				 const char *name, int len)
188 {
189 	__u32 res;
190 
191 	if (!len || (len == 1 && name[0] == '.'))
192 		return DOT_OFFSET;
193 	if (len == 2 && name[0] == '.' && name[1] == '.')
194 		return DOT_DOT_OFFSET;
195 
196 	res = REISERFS_SB(s)->s_hash_function(name, len);
197 
198 	/* take bits from 7-th to 30-th including both bounds */
199 	res = GET_HASH_VALUE(res);
200 	if (res == 0)
201 		/*
202 		 * needed to have no names before "." and ".." those have hash
203 		 * value == 0 and generation conters 1 and 2 accordingly
204 		 */
205 		res = 128;
206 	return res + MAX_GENERATION_NUMBER;
207 }
208 
209 static int reiserfs_match(struct reiserfs_dir_entry *de,
210 			  const char *name, int namelen)
211 {
212 	int retval = NAME_NOT_FOUND;
213 
214 	if ((namelen == de->de_namelen) &&
215 	    !memcmp(de->de_name, name, de->de_namelen))
216 		retval =
217 		    (de_visible(de->de_deh + de->de_entry_num) ? NAME_FOUND :
218 		     NAME_FOUND_INVISIBLE);
219 
220 	return retval;
221 }
222 
223 /* de's de_bh, de_ih, de_deh, de_item_num, de_entry_num are set already */
224 
225 /* used when hash collisions exist */
226 
227 static int linear_search_in_dir_item(struct cpu_key *key,
228 				     struct reiserfs_dir_entry *de,
229 				     const char *name, int namelen)
230 {
231 	struct reiserfs_de_head *deh = de->de_deh;
232 	int retval;
233 	int i;
234 
235 	i = de->de_entry_num;
236 
237 	if (i == ih_entry_count(de->de_ih) ||
238 	    GET_HASH_VALUE(deh_offset(deh + i)) !=
239 	    GET_HASH_VALUE(cpu_key_k_offset(key))) {
240 		i--;
241 	}
242 
243 	RFALSE(de->de_deh != B_I_DEH(de->de_bh, de->de_ih),
244 	       "vs-7010: array of entry headers not found");
245 
246 	deh += i;
247 
248 	for (; i >= 0; i--, deh--) {
249 		/* hash value does not match, no need to check whole name */
250 		if (GET_HASH_VALUE(deh_offset(deh)) !=
251 		    GET_HASH_VALUE(cpu_key_k_offset(key))) {
252 			return NAME_NOT_FOUND;
253 		}
254 
255 		/* mark that this generation number is used */
256 		if (de->de_gen_number_bit_string)
257 			set_bit(GET_GENERATION_NUMBER(deh_offset(deh)),
258 				de->de_gen_number_bit_string);
259 
260 		/* calculate pointer to name and namelen */
261 		de->de_entry_num = i;
262 		set_de_name_and_namelen(de);
263 
264 		/*
265 		 * de's de_name, de_namelen, de_recordlen are set.
266 		 * Fill the rest.
267 		 */
268 		if ((retval =
269 		     reiserfs_match(de, name, namelen)) != NAME_NOT_FOUND) {
270 
271 			/* key of pointed object */
272 			set_de_object_key(de);
273 
274 			store_de_entry_key(de);
275 
276 			/* retval can be NAME_FOUND or NAME_FOUND_INVISIBLE */
277 			return retval;
278 		}
279 	}
280 
281 	if (GET_GENERATION_NUMBER(le_ih_k_offset(de->de_ih)) == 0)
282 		/*
283 		 * we have reached left most entry in the node. In common we
284 		 * have to go to the left neighbor, but if generation counter
285 		 * is 0 already, we know for sure, that there is no name with
286 		 * the same hash value
287 		 */
288 		/*
289 		 * FIXME: this work correctly only because hash value can not
290 		 *  be 0. Btw, in case of Yura's hash it is probably possible,
291 		 * so, this is a bug
292 		 */
293 		return NAME_NOT_FOUND;
294 
295 	RFALSE(de->de_item_num,
296 	       "vs-7015: two diritems of the same directory in one node?");
297 
298 	return GOTO_PREVIOUS_ITEM;
299 }
300 
301 /*
302  * may return NAME_FOUND, NAME_FOUND_INVISIBLE, NAME_NOT_FOUND
303  * FIXME: should add something like IOERROR
304  */
305 static int reiserfs_find_entry(struct inode *dir, const char *name, int namelen,
306 			       struct treepath *path_to_entry,
307 			       struct reiserfs_dir_entry *de)
308 {
309 	struct cpu_key key_to_search;
310 	int retval;
311 
312 	if (namelen > REISERFS_MAX_NAME(dir->i_sb->s_blocksize))
313 		return NAME_NOT_FOUND;
314 
315 	/* we will search for this key in the tree */
316 	make_cpu_key(&key_to_search, dir,
317 		     get_third_component(dir->i_sb, name, namelen),
318 		     TYPE_DIRENTRY, 3);
319 
320 	while (1) {
321 		retval =
322 		    search_by_entry_key(dir->i_sb, &key_to_search,
323 					path_to_entry, de);
324 		if (retval == IO_ERROR) {
325 			reiserfs_error(dir->i_sb, "zam-7001", "io error");
326 			return IO_ERROR;
327 		}
328 
329 		/* compare names for all entries having given hash value */
330 		retval =
331 		    linear_search_in_dir_item(&key_to_search, de, name,
332 					      namelen);
333 		/*
334 		 * there is no need to scan directory anymore.
335 		 * Given entry found or does not exist
336 		 */
337 		if (retval != GOTO_PREVIOUS_ITEM) {
338 			path_to_entry->pos_in_item = de->de_entry_num;
339 			return retval;
340 		}
341 
342 		/*
343 		 * there is left neighboring item of this directory
344 		 * and given entry can be there
345 		 */
346 		set_cpu_key_k_offset(&key_to_search,
347 				     le_ih_k_offset(de->de_ih) - 1);
348 		pathrelse(path_to_entry);
349 
350 	}			/* while (1) */
351 }
352 
353 static struct dentry *reiserfs_lookup(struct inode *dir, struct dentry *dentry,
354 				      unsigned int flags)
355 {
356 	int retval;
357 	struct inode *inode = NULL;
358 	struct reiserfs_dir_entry de;
359 	INITIALIZE_PATH(path_to_entry);
360 
361 	if (REISERFS_MAX_NAME(dir->i_sb->s_blocksize) < dentry->d_name.len)
362 		return ERR_PTR(-ENAMETOOLONG);
363 
364 	reiserfs_write_lock(dir->i_sb);
365 
366 	de.de_gen_number_bit_string = NULL;
367 	retval =
368 	    reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
369 				&path_to_entry, &de);
370 	pathrelse(&path_to_entry);
371 	if (retval == NAME_FOUND) {
372 		inode = reiserfs_iget(dir->i_sb,
373 				      (struct cpu_key *)&de.de_dir_id);
374 		if (!inode || IS_ERR(inode)) {
375 			reiserfs_write_unlock(dir->i_sb);
376 			return ERR_PTR(-EACCES);
377 		}
378 
379 		/*
380 		 * Propagate the private flag so we know we're
381 		 * in the priv tree.  Also clear xattr support
382 		 * since we don't have xattrs on xattr files.
383 		 */
384 		if (IS_PRIVATE(dir))
385 			reiserfs_init_priv_inode(inode);
386 	}
387 	reiserfs_write_unlock(dir->i_sb);
388 	if (retval == IO_ERROR) {
389 		return ERR_PTR(-EIO);
390 	}
391 
392 	return d_splice_alias(inode, dentry);
393 }
394 
395 /*
396  * looks up the dentry of the parent directory for child.
397  * taken from ext2_get_parent
398  */
399 struct dentry *reiserfs_get_parent(struct dentry *child)
400 {
401 	int retval;
402 	struct inode *inode = NULL;
403 	struct reiserfs_dir_entry de;
404 	INITIALIZE_PATH(path_to_entry);
405 	struct inode *dir = d_inode(child);
406 
407 	if (dir->i_nlink == 0) {
408 		return ERR_PTR(-ENOENT);
409 	}
410 	de.de_gen_number_bit_string = NULL;
411 
412 	reiserfs_write_lock(dir->i_sb);
413 	retval = reiserfs_find_entry(dir, "..", 2, &path_to_entry, &de);
414 	pathrelse(&path_to_entry);
415 	if (retval != NAME_FOUND) {
416 		reiserfs_write_unlock(dir->i_sb);
417 		return ERR_PTR(-ENOENT);
418 	}
419 	inode = reiserfs_iget(dir->i_sb, (struct cpu_key *)&de.de_dir_id);
420 	reiserfs_write_unlock(dir->i_sb);
421 
422 	return d_obtain_alias(inode);
423 }
424 
425 /* add entry to the directory (entry can be hidden).
426 
427 insert definition of when hidden directories are used here -Hans
428 
429  Does not mark dir   inode dirty, do it after successesfull call to it */
430 
431 static int reiserfs_add_entry(struct reiserfs_transaction_handle *th,
432 			      struct inode *dir, const char *name, int namelen,
433 			      struct inode *inode, int visible)
434 {
435 	struct cpu_key entry_key;
436 	struct reiserfs_de_head *deh;
437 	INITIALIZE_PATH(path);
438 	struct reiserfs_dir_entry de;
439 	DECLARE_BITMAP(bit_string, MAX_GENERATION_NUMBER + 1);
440 	int gen_number;
441 
442 	/*
443 	 * 48 bytes now and we avoid kmalloc if we
444 	 * create file with short name
445 	 */
446 	char small_buf[32 + DEH_SIZE];
447 
448 	char *buffer;
449 	int buflen, paste_size;
450 	int retval;
451 
452 	BUG_ON(!th->t_trans_id);
453 
454 	/* each entry has unique key. compose it */
455 	make_cpu_key(&entry_key, dir,
456 		     get_third_component(dir->i_sb, name, namelen),
457 		     TYPE_DIRENTRY, 3);
458 
459 	/* get memory for composing the entry */
460 	buflen = DEH_SIZE + ROUND_UP(namelen);
461 	if (buflen > sizeof(small_buf)) {
462 		buffer = kmalloc(buflen, GFP_NOFS);
463 		if (!buffer)
464 			return -ENOMEM;
465 	} else
466 		buffer = small_buf;
467 
468 	paste_size =
469 	    (get_inode_sd_version(dir) ==
470 	     STAT_DATA_V1) ? (DEH_SIZE + namelen) : buflen;
471 
472 	/*
473 	 * fill buffer : directory entry head, name[, dir objectid | ,
474 	 * stat data | ,stat data, dir objectid ]
475 	 */
476 	deh = (struct reiserfs_de_head *)buffer;
477 	deh->deh_location = 0;	/* JDM Endian safe if 0 */
478 	put_deh_offset(deh, cpu_key_k_offset(&entry_key));
479 	deh->deh_state = 0;	/* JDM Endian safe if 0 */
480 	/* put key (ino analog) to de */
481 
482 	/* safe: k_dir_id is le */
483 	deh->deh_dir_id = INODE_PKEY(inode)->k_dir_id;
484 	/* safe: k_objectid is le */
485 	deh->deh_objectid = INODE_PKEY(inode)->k_objectid;
486 
487 	/* copy name */
488 	memcpy((char *)(deh + 1), name, namelen);
489 	/* padd by 0s to the 4 byte boundary */
490 	padd_item((char *)(deh + 1), ROUND_UP(namelen), namelen);
491 
492 	/*
493 	 * entry is ready to be pasted into tree, set 'visibility'
494 	 * and 'stat data in entry' attributes
495 	 */
496 	mark_de_without_sd(deh);
497 	visible ? mark_de_visible(deh) : mark_de_hidden(deh);
498 
499 	/* find the proper place for the new entry */
500 	memset(bit_string, 0, sizeof(bit_string));
501 	de.de_gen_number_bit_string = bit_string;
502 	retval = reiserfs_find_entry(dir, name, namelen, &path, &de);
503 	if (retval != NAME_NOT_FOUND) {
504 		if (buffer != small_buf)
505 			kfree(buffer);
506 		pathrelse(&path);
507 
508 		if (retval == IO_ERROR) {
509 			return -EIO;
510 		}
511 
512 		if (retval != NAME_FOUND) {
513 			reiserfs_error(dir->i_sb, "zam-7002",
514 				       "reiserfs_find_entry() returned "
515 				       "unexpected value (%d)", retval);
516 		}
517 
518 		return -EEXIST;
519 	}
520 
521 	gen_number =
522 	    find_first_zero_bit(bit_string,
523 				MAX_GENERATION_NUMBER + 1);
524 	if (gen_number > MAX_GENERATION_NUMBER) {
525 		/* there is no free generation number */
526 		reiserfs_warning(dir->i_sb, "reiserfs-7010",
527 				 "Congratulations! we have got hash function "
528 				 "screwed up");
529 		if (buffer != small_buf)
530 			kfree(buffer);
531 		pathrelse(&path);
532 		return -EBUSY;
533 	}
534 	/* adjust offset of directory enrty */
535 	put_deh_offset(deh, SET_GENERATION_NUMBER(deh_offset(deh), gen_number));
536 	set_cpu_key_k_offset(&entry_key, deh_offset(deh));
537 
538 	/* update max-hash-collisions counter in reiserfs_sb_info */
539 	PROC_INFO_MAX(th->t_super, max_hash_collisions, gen_number);
540 
541 	/* we need to re-search for the insertion point */
542 	if (gen_number != 0) {
543 		if (search_by_entry_key(dir->i_sb, &entry_key, &path, &de) !=
544 		    NAME_NOT_FOUND) {
545 			reiserfs_warning(dir->i_sb, "vs-7032",
546 					 "entry with this key (%K) already "
547 					 "exists", &entry_key);
548 
549 			if (buffer != small_buf)
550 				kfree(buffer);
551 			pathrelse(&path);
552 			return -EBUSY;
553 		}
554 	}
555 
556 	/* perform the insertion of the entry that we have prepared */
557 	retval =
558 	    reiserfs_paste_into_item(th, &path, &entry_key, dir, buffer,
559 				     paste_size);
560 	if (buffer != small_buf)
561 		kfree(buffer);
562 	if (retval) {
563 		reiserfs_check_path(&path);
564 		return retval;
565 	}
566 
567 	dir->i_size += paste_size;
568 	inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
569 	if (!S_ISDIR(inode->i_mode) && visible)
570 		/* reiserfs_mkdir or reiserfs_rename will do that by itself */
571 		reiserfs_update_sd(th, dir);
572 
573 	reiserfs_check_path(&path);
574 	return 0;
575 }
576 
577 /*
578  * quota utility function, call if you've had to abort after calling
579  * new_inode_init, and have not called reiserfs_new_inode yet.
580  * This should only be called on inodes that do not have stat data
581  * inserted into the tree yet.
582  */
583 static int drop_new_inode(struct inode *inode)
584 {
585 	dquot_drop(inode);
586 	make_bad_inode(inode);
587 	inode->i_flags |= S_NOQUOTA;
588 	iput(inode);
589 	return 0;
590 }
591 
592 /*
593  * utility function that does setup for reiserfs_new_inode.
594  * dquot_initialize needs lots of credits so it's better to have it
595  * outside of a transaction, so we had to pull some bits of
596  * reiserfs_new_inode out into this func.
597  */
598 static int new_inode_init(struct inode *inode, struct inode *dir, umode_t mode)
599 {
600 	/*
601 	 * Make inode invalid - just in case we are going to drop it before
602 	 * the initialization happens
603 	 */
604 	INODE_PKEY(inode)->k_objectid = 0;
605 
606 	/*
607 	 * the quota init calls have to know who to charge the quota to, so
608 	 * we have to set uid and gid here
609 	 */
610 	inode_init_owner(&nop_mnt_idmap, inode, dir, mode);
611 	return dquot_initialize(inode);
612 }
613 
614 static int reiserfs_create(struct mnt_idmap *idmap, struct inode *dir,
615 			   struct dentry *dentry, umode_t mode, bool excl)
616 {
617 	int retval;
618 	struct inode *inode;
619 	/*
620 	 * We need blocks for transaction + (user+group)*(quotas
621 	 * for new inode + update of quota for directory owner)
622 	 */
623 	int jbegin_count =
624 	    JOURNAL_PER_BALANCE_CNT * 2 +
625 	    2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
626 		 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
627 	struct reiserfs_transaction_handle th;
628 	struct reiserfs_security_handle security;
629 
630 	retval = dquot_initialize(dir);
631 	if (retval)
632 		return retval;
633 
634 	if (!(inode = new_inode(dir->i_sb))) {
635 		return -ENOMEM;
636 	}
637 	retval = new_inode_init(inode, dir, mode);
638 	if (retval) {
639 		drop_new_inode(inode);
640 		return retval;
641 	}
642 
643 	jbegin_count += reiserfs_cache_default_acl(dir);
644 	retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security);
645 	if (retval < 0) {
646 		drop_new_inode(inode);
647 		return retval;
648 	}
649 	jbegin_count += retval;
650 	reiserfs_write_lock(dir->i_sb);
651 
652 	retval = journal_begin(&th, dir->i_sb, jbegin_count);
653 	if (retval) {
654 		drop_new_inode(inode);
655 		goto out_failed;
656 	}
657 
658 	retval =
659 	    reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
660 			       inode, &security);
661 	if (retval)
662 		goto out_failed;
663 
664 	inode->i_op = &reiserfs_file_inode_operations;
665 	inode->i_fop = &reiserfs_file_operations;
666 	inode->i_mapping->a_ops = &reiserfs_address_space_operations;
667 
668 	retval =
669 	    reiserfs_add_entry(&th, dir, dentry->d_name.name,
670 			       dentry->d_name.len, inode, 1 /*visible */ );
671 	if (retval) {
672 		int err;
673 		drop_nlink(inode);
674 		reiserfs_update_sd(&th, inode);
675 		err = journal_end(&th);
676 		if (err)
677 			retval = err;
678 		unlock_new_inode(inode);
679 		iput(inode);
680 		goto out_failed;
681 	}
682 	reiserfs_update_inode_transaction(inode);
683 	reiserfs_update_inode_transaction(dir);
684 
685 	d_instantiate_new(dentry, inode);
686 	retval = journal_end(&th);
687 
688 out_failed:
689 	reiserfs_write_unlock(dir->i_sb);
690 	reiserfs_security_free(&security);
691 	return retval;
692 }
693 
694 static int reiserfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
695 			  struct dentry *dentry, umode_t mode, dev_t rdev)
696 {
697 	int retval;
698 	struct inode *inode;
699 	struct reiserfs_transaction_handle th;
700 	struct reiserfs_security_handle security;
701 	/*
702 	 * We need blocks for transaction + (user+group)*(quotas
703 	 * for new inode + update of quota for directory owner)
704 	 */
705 	int jbegin_count =
706 	    JOURNAL_PER_BALANCE_CNT * 3 +
707 	    2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
708 		 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
709 
710 	retval = dquot_initialize(dir);
711 	if (retval)
712 		return retval;
713 
714 	if (!(inode = new_inode(dir->i_sb))) {
715 		return -ENOMEM;
716 	}
717 	retval = new_inode_init(inode, dir, mode);
718 	if (retval) {
719 		drop_new_inode(inode);
720 		return retval;
721 	}
722 
723 	jbegin_count += reiserfs_cache_default_acl(dir);
724 	retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security);
725 	if (retval < 0) {
726 		drop_new_inode(inode);
727 		return retval;
728 	}
729 	jbegin_count += retval;
730 	reiserfs_write_lock(dir->i_sb);
731 
732 	retval = journal_begin(&th, dir->i_sb, jbegin_count);
733 	if (retval) {
734 		drop_new_inode(inode);
735 		goto out_failed;
736 	}
737 
738 	retval =
739 	    reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
740 			       inode, &security);
741 	if (retval) {
742 		goto out_failed;
743 	}
744 
745 	inode->i_op = &reiserfs_special_inode_operations;
746 	init_special_inode(inode, inode->i_mode, rdev);
747 
748 	/* FIXME: needed for block and char devices only */
749 	reiserfs_update_sd(&th, inode);
750 
751 	reiserfs_update_inode_transaction(inode);
752 	reiserfs_update_inode_transaction(dir);
753 
754 	retval =
755 	    reiserfs_add_entry(&th, dir, dentry->d_name.name,
756 			       dentry->d_name.len, inode, 1 /*visible */ );
757 	if (retval) {
758 		int err;
759 		drop_nlink(inode);
760 		reiserfs_update_sd(&th, inode);
761 		err = journal_end(&th);
762 		if (err)
763 			retval = err;
764 		unlock_new_inode(inode);
765 		iput(inode);
766 		goto out_failed;
767 	}
768 
769 	d_instantiate_new(dentry, inode);
770 	retval = journal_end(&th);
771 
772 out_failed:
773 	reiserfs_write_unlock(dir->i_sb);
774 	reiserfs_security_free(&security);
775 	return retval;
776 }
777 
778 static int reiserfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
779 			  struct dentry *dentry, umode_t mode)
780 {
781 	int retval;
782 	struct inode *inode;
783 	struct reiserfs_transaction_handle th;
784 	struct reiserfs_security_handle security;
785 	/*
786 	 * We need blocks for transaction + (user+group)*(quotas
787 	 * for new inode + update of quota for directory owner)
788 	 */
789 	int jbegin_count =
790 	    JOURNAL_PER_BALANCE_CNT * 3 +
791 	    2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
792 		 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
793 
794 	retval = dquot_initialize(dir);
795 	if (retval)
796 		return retval;
797 
798 #ifdef DISPLACE_NEW_PACKING_LOCALITIES
799 	/*
800 	 * set flag that new packing locality created and new blocks
801 	 * for the content of that directory are not displaced yet
802 	 */
803 	REISERFS_I(dir)->new_packing_locality = 1;
804 #endif
805 	mode = S_IFDIR | mode;
806 	if (!(inode = new_inode(dir->i_sb))) {
807 		return -ENOMEM;
808 	}
809 	retval = new_inode_init(inode, dir, mode);
810 	if (retval) {
811 		drop_new_inode(inode);
812 		return retval;
813 	}
814 
815 	jbegin_count += reiserfs_cache_default_acl(dir);
816 	retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security);
817 	if (retval < 0) {
818 		drop_new_inode(inode);
819 		return retval;
820 	}
821 	jbegin_count += retval;
822 	reiserfs_write_lock(dir->i_sb);
823 
824 	retval = journal_begin(&th, dir->i_sb, jbegin_count);
825 	if (retval) {
826 		drop_new_inode(inode);
827 		goto out_failed;
828 	}
829 
830 	/*
831 	 * inc the link count now, so another writer doesn't overflow
832 	 * it while we sleep later on.
833 	 */
834 	INC_DIR_INODE_NLINK(dir)
835 
836 	retval = reiserfs_new_inode(&th, dir, mode, NULL /*symlink */,
837 				    old_format_only(dir->i_sb) ?
838 				    EMPTY_DIR_SIZE_V1 : EMPTY_DIR_SIZE,
839 				    dentry, inode, &security);
840 	if (retval) {
841 		DEC_DIR_INODE_NLINK(dir)
842 		goto out_failed;
843 	}
844 
845 	reiserfs_update_inode_transaction(inode);
846 	reiserfs_update_inode_transaction(dir);
847 
848 	inode->i_op = &reiserfs_dir_inode_operations;
849 	inode->i_fop = &reiserfs_dir_operations;
850 
851 	/* note, _this_ add_entry will not update dir's stat data */
852 	retval =
853 	    reiserfs_add_entry(&th, dir, dentry->d_name.name,
854 			       dentry->d_name.len, inode, 1 /*visible */ );
855 	if (retval) {
856 		int err;
857 		clear_nlink(inode);
858 		DEC_DIR_INODE_NLINK(dir);
859 		reiserfs_update_sd(&th, inode);
860 		err = journal_end(&th);
861 		if (err)
862 			retval = err;
863 		unlock_new_inode(inode);
864 		iput(inode);
865 		goto out_failed;
866 	}
867 	/* the above add_entry did not update dir's stat data */
868 	reiserfs_update_sd(&th, dir);
869 
870 	d_instantiate_new(dentry, inode);
871 	retval = journal_end(&th);
872 out_failed:
873 	reiserfs_write_unlock(dir->i_sb);
874 	reiserfs_security_free(&security);
875 	return retval;
876 }
877 
878 static inline int reiserfs_empty_dir(struct inode *inode)
879 {
880 	/*
881 	 * we can cheat because an old format dir cannot have
882 	 * EMPTY_DIR_SIZE, and a new format dir cannot have
883 	 * EMPTY_DIR_SIZE_V1.  So, if the inode is either size,
884 	 * regardless of disk format version, the directory is empty.
885 	 */
886 	if (inode->i_size != EMPTY_DIR_SIZE &&
887 	    inode->i_size != EMPTY_DIR_SIZE_V1) {
888 		return 0;
889 	}
890 	return 1;
891 }
892 
893 static int reiserfs_rmdir(struct inode *dir, struct dentry *dentry)
894 {
895 	int retval, err;
896 	struct inode *inode;
897 	struct reiserfs_transaction_handle th;
898 	int jbegin_count;
899 	INITIALIZE_PATH(path);
900 	struct reiserfs_dir_entry de;
901 
902 	/*
903 	 * we will be doing 2 balancings and update 2 stat data, we
904 	 * change quotas of the owner of the directory and of the owner
905 	 * of the parent directory.  The quota structure is possibly
906 	 * deleted only on last iput => outside of this transaction
907 	 */
908 	jbegin_count =
909 	    JOURNAL_PER_BALANCE_CNT * 2 + 2 +
910 	    4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
911 
912 	retval = dquot_initialize(dir);
913 	if (retval)
914 		return retval;
915 
916 	reiserfs_write_lock(dir->i_sb);
917 	retval = journal_begin(&th, dir->i_sb, jbegin_count);
918 	if (retval)
919 		goto out_rmdir;
920 
921 	de.de_gen_number_bit_string = NULL;
922 	if ((retval =
923 	     reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
924 				 &path, &de)) == NAME_NOT_FOUND) {
925 		retval = -ENOENT;
926 		goto end_rmdir;
927 	} else if (retval == IO_ERROR) {
928 		retval = -EIO;
929 		goto end_rmdir;
930 	}
931 
932 	inode = d_inode(dentry);
933 
934 	reiserfs_update_inode_transaction(inode);
935 	reiserfs_update_inode_transaction(dir);
936 
937 	if (de.de_objectid != inode->i_ino) {
938 		/*
939 		 * FIXME: compare key of an object and a key found in the entry
940 		 */
941 		retval = -EIO;
942 		goto end_rmdir;
943 	}
944 	if (!reiserfs_empty_dir(inode)) {
945 		retval = -ENOTEMPTY;
946 		goto end_rmdir;
947 	}
948 
949 	/* cut entry from dir directory */
950 	retval = reiserfs_cut_from_item(&th, &path, &de.de_entry_key,
951 					dir, NULL,	/* page */
952 					0 /*new file size - not used here */ );
953 	if (retval < 0)
954 		goto end_rmdir;
955 
956 	if (inode->i_nlink != 2 && inode->i_nlink != 1)
957 		reiserfs_error(inode->i_sb, "reiserfs-7040",
958 			       "empty directory has nlink != 2 (%d)",
959 			       inode->i_nlink);
960 
961 	clear_nlink(inode);
962 	inode_set_mtime_to_ts(dir,
963 			      inode_set_ctime_to_ts(dir, inode_set_ctime_current(inode)));
964 	reiserfs_update_sd(&th, inode);
965 
966 	DEC_DIR_INODE_NLINK(dir)
967 	dir->i_size -= (DEH_SIZE + de.de_entrylen);
968 	reiserfs_update_sd(&th, dir);
969 
970 	/* prevent empty directory from getting lost */
971 	add_save_link(&th, inode, 0 /* not truncate */ );
972 
973 	retval = journal_end(&th);
974 	reiserfs_check_path(&path);
975 out_rmdir:
976 	reiserfs_write_unlock(dir->i_sb);
977 	return retval;
978 
979 end_rmdir:
980 	/*
981 	 * we must release path, because we did not call
982 	 * reiserfs_cut_from_item, or reiserfs_cut_from_item does not
983 	 * release path if operation was not complete
984 	 */
985 	pathrelse(&path);
986 	err = journal_end(&th);
987 	reiserfs_write_unlock(dir->i_sb);
988 	return err ? err : retval;
989 }
990 
991 static int reiserfs_unlink(struct inode *dir, struct dentry *dentry)
992 {
993 	int retval, err;
994 	struct inode *inode;
995 	struct reiserfs_dir_entry de;
996 	INITIALIZE_PATH(path);
997 	struct reiserfs_transaction_handle th;
998 	int jbegin_count;
999 	unsigned long savelink;
1000 
1001 	retval = dquot_initialize(dir);
1002 	if (retval)
1003 		return retval;
1004 
1005 	inode = d_inode(dentry);
1006 
1007 	/*
1008 	 * in this transaction we can be doing at max two balancings and
1009 	 * update two stat datas, we change quotas of the owner of the
1010 	 * directory and of the owner of the parent directory. The quota
1011 	 * structure is possibly deleted only on iput => outside of
1012 	 * this transaction
1013 	 */
1014 	jbegin_count =
1015 	    JOURNAL_PER_BALANCE_CNT * 2 + 2 +
1016 	    4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
1017 
1018 	reiserfs_write_lock(dir->i_sb);
1019 	retval = journal_begin(&th, dir->i_sb, jbegin_count);
1020 	if (retval)
1021 		goto out_unlink;
1022 
1023 	de.de_gen_number_bit_string = NULL;
1024 	if ((retval =
1025 	     reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
1026 				 &path, &de)) == NAME_NOT_FOUND) {
1027 		retval = -ENOENT;
1028 		goto end_unlink;
1029 	} else if (retval == IO_ERROR) {
1030 		retval = -EIO;
1031 		goto end_unlink;
1032 	}
1033 
1034 	reiserfs_update_inode_transaction(inode);
1035 	reiserfs_update_inode_transaction(dir);
1036 
1037 	if (de.de_objectid != inode->i_ino) {
1038 		/*
1039 		 * FIXME: compare key of an object and a key found in the entry
1040 		 */
1041 		retval = -EIO;
1042 		goto end_unlink;
1043 	}
1044 
1045 	if (!inode->i_nlink) {
1046 		reiserfs_warning(inode->i_sb, "reiserfs-7042",
1047 				 "deleting nonexistent file (%lu), %d",
1048 				 inode->i_ino, inode->i_nlink);
1049 		set_nlink(inode, 1);
1050 	}
1051 
1052 	drop_nlink(inode);
1053 
1054 	/*
1055 	 * we schedule before doing the add_save_link call, save the link
1056 	 * count so we don't race
1057 	 */
1058 	savelink = inode->i_nlink;
1059 
1060 	retval =
1061 	    reiserfs_cut_from_item(&th, &path, &de.de_entry_key, dir, NULL,
1062 				   0);
1063 	if (retval < 0) {
1064 		inc_nlink(inode);
1065 		goto end_unlink;
1066 	}
1067 	inode_set_ctime_current(inode);
1068 	reiserfs_update_sd(&th, inode);
1069 
1070 	dir->i_size -= (de.de_entrylen + DEH_SIZE);
1071 	inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
1072 	reiserfs_update_sd(&th, dir);
1073 
1074 	if (!savelink)
1075 		/* prevent file from getting lost */
1076 		add_save_link(&th, inode, 0 /* not truncate */ );
1077 
1078 	retval = journal_end(&th);
1079 	reiserfs_check_path(&path);
1080 	reiserfs_write_unlock(dir->i_sb);
1081 	return retval;
1082 
1083 end_unlink:
1084 	pathrelse(&path);
1085 	err = journal_end(&th);
1086 	reiserfs_check_path(&path);
1087 	if (err)
1088 		retval = err;
1089 out_unlink:
1090 	reiserfs_write_unlock(dir->i_sb);
1091 	return retval;
1092 }
1093 
1094 static int reiserfs_symlink(struct mnt_idmap *idmap,
1095 			    struct inode *parent_dir, struct dentry *dentry,
1096 			    const char *symname)
1097 {
1098 	int retval;
1099 	struct inode *inode;
1100 	char *name;
1101 	int item_len;
1102 	struct reiserfs_transaction_handle th;
1103 	struct reiserfs_security_handle security;
1104 	int mode = S_IFLNK | S_IRWXUGO;
1105 	/*
1106 	 * We need blocks for transaction + (user+group)*(quotas for
1107 	 * new inode + update of quota for directory owner)
1108 	 */
1109 	int jbegin_count =
1110 	    JOURNAL_PER_BALANCE_CNT * 3 +
1111 	    2 * (REISERFS_QUOTA_INIT_BLOCKS(parent_dir->i_sb) +
1112 		 REISERFS_QUOTA_TRANS_BLOCKS(parent_dir->i_sb));
1113 
1114 	retval = dquot_initialize(parent_dir);
1115 	if (retval)
1116 		return retval;
1117 
1118 	if (!(inode = new_inode(parent_dir->i_sb))) {
1119 		return -ENOMEM;
1120 	}
1121 	retval = new_inode_init(inode, parent_dir, mode);
1122 	if (retval) {
1123 		drop_new_inode(inode);
1124 		return retval;
1125 	}
1126 
1127 	retval = reiserfs_security_init(parent_dir, inode, &dentry->d_name,
1128 					&security);
1129 	if (retval < 0) {
1130 		drop_new_inode(inode);
1131 		return retval;
1132 	}
1133 	jbegin_count += retval;
1134 
1135 	reiserfs_write_lock(parent_dir->i_sb);
1136 	item_len = ROUND_UP(strlen(symname));
1137 	if (item_len > MAX_DIRECT_ITEM_LEN(parent_dir->i_sb->s_blocksize)) {
1138 		retval = -ENAMETOOLONG;
1139 		drop_new_inode(inode);
1140 		goto out_failed;
1141 	}
1142 
1143 	name = kmalloc(item_len, GFP_NOFS);
1144 	if (!name) {
1145 		drop_new_inode(inode);
1146 		retval = -ENOMEM;
1147 		goto out_failed;
1148 	}
1149 	memcpy(name, symname, strlen(symname));
1150 	padd_item(name, item_len, strlen(symname));
1151 
1152 	retval = journal_begin(&th, parent_dir->i_sb, jbegin_count);
1153 	if (retval) {
1154 		drop_new_inode(inode);
1155 		kfree(name);
1156 		goto out_failed;
1157 	}
1158 
1159 	retval =
1160 	    reiserfs_new_inode(&th, parent_dir, mode, name, strlen(symname),
1161 			       dentry, inode, &security);
1162 	kfree(name);
1163 	if (retval) {		/* reiserfs_new_inode iputs for us */
1164 		goto out_failed;
1165 	}
1166 
1167 	reiserfs_update_inode_transaction(inode);
1168 	reiserfs_update_inode_transaction(parent_dir);
1169 
1170 	inode->i_op = &reiserfs_symlink_inode_operations;
1171 	inode_nohighmem(inode);
1172 	inode->i_mapping->a_ops = &reiserfs_address_space_operations;
1173 
1174 	retval = reiserfs_add_entry(&th, parent_dir, dentry->d_name.name,
1175 				    dentry->d_name.len, inode, 1 /*visible */ );
1176 	if (retval) {
1177 		int err;
1178 		drop_nlink(inode);
1179 		reiserfs_update_sd(&th, inode);
1180 		err = journal_end(&th);
1181 		if (err)
1182 			retval = err;
1183 		unlock_new_inode(inode);
1184 		iput(inode);
1185 		goto out_failed;
1186 	}
1187 
1188 	d_instantiate_new(dentry, inode);
1189 	retval = journal_end(&th);
1190 out_failed:
1191 	reiserfs_write_unlock(parent_dir->i_sb);
1192 	reiserfs_security_free(&security);
1193 	return retval;
1194 }
1195 
1196 static int reiserfs_link(struct dentry *old_dentry, struct inode *dir,
1197 			 struct dentry *dentry)
1198 {
1199 	int retval;
1200 	struct inode *inode = d_inode(old_dentry);
1201 	struct reiserfs_transaction_handle th;
1202 	/*
1203 	 * We need blocks for transaction + update of quotas for
1204 	 * the owners of the directory
1205 	 */
1206 	int jbegin_count =
1207 	    JOURNAL_PER_BALANCE_CNT * 3 +
1208 	    2 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
1209 
1210 	retval = dquot_initialize(dir);
1211 	if (retval)
1212 		return retval;
1213 
1214 	reiserfs_write_lock(dir->i_sb);
1215 	if (inode->i_nlink >= REISERFS_LINK_MAX) {
1216 		/* FIXME: sd_nlink is 32 bit for new files */
1217 		reiserfs_write_unlock(dir->i_sb);
1218 		return -EMLINK;
1219 	}
1220 
1221 	/* inc before scheduling so reiserfs_unlink knows we are here */
1222 	inc_nlink(inode);
1223 
1224 	retval = journal_begin(&th, dir->i_sb, jbegin_count);
1225 	if (retval) {
1226 		drop_nlink(inode);
1227 		reiserfs_write_unlock(dir->i_sb);
1228 		return retval;
1229 	}
1230 
1231 	/* create new entry */
1232 	retval =
1233 	    reiserfs_add_entry(&th, dir, dentry->d_name.name,
1234 			       dentry->d_name.len, inode, 1 /*visible */ );
1235 
1236 	reiserfs_update_inode_transaction(inode);
1237 	reiserfs_update_inode_transaction(dir);
1238 
1239 	if (retval) {
1240 		int err;
1241 		drop_nlink(inode);
1242 		err = journal_end(&th);
1243 		reiserfs_write_unlock(dir->i_sb);
1244 		return err ? err : retval;
1245 	}
1246 
1247 	inode_set_ctime_current(inode);
1248 	reiserfs_update_sd(&th, inode);
1249 
1250 	ihold(inode);
1251 	d_instantiate(dentry, inode);
1252 	retval = journal_end(&th);
1253 	reiserfs_write_unlock(dir->i_sb);
1254 	return retval;
1255 }
1256 
1257 /* de contains information pointing to an entry which */
1258 static int de_still_valid(const char *name, int len,
1259 			  struct reiserfs_dir_entry *de)
1260 {
1261 	struct reiserfs_dir_entry tmp = *de;
1262 
1263 	/* recalculate pointer to name and name length */
1264 	set_de_name_and_namelen(&tmp);
1265 	/* FIXME: could check more */
1266 	if (tmp.de_namelen != len || memcmp(name, de->de_name, len))
1267 		return 0;
1268 	return 1;
1269 }
1270 
1271 static int entry_points_to_object(const char *name, int len,
1272 				  struct reiserfs_dir_entry *de,
1273 				  struct inode *inode)
1274 {
1275 	if (!de_still_valid(name, len, de))
1276 		return 0;
1277 
1278 	if (inode) {
1279 		if (!de_visible(de->de_deh + de->de_entry_num))
1280 			reiserfs_panic(inode->i_sb, "vs-7042",
1281 				       "entry must be visible");
1282 		return (de->de_objectid == inode->i_ino) ? 1 : 0;
1283 	}
1284 
1285 	/* this must be added hidden entry */
1286 	if (de_visible(de->de_deh + de->de_entry_num))
1287 		reiserfs_panic(NULL, "vs-7043", "entry must be visible");
1288 
1289 	return 1;
1290 }
1291 
1292 /* sets key of objectid the entry has to point to */
1293 static void set_ino_in_dir_entry(struct reiserfs_dir_entry *de,
1294 				 struct reiserfs_key *key)
1295 {
1296 	/* JDM These operations are endian safe - both are le */
1297 	de->de_deh[de->de_entry_num].deh_dir_id = key->k_dir_id;
1298 	de->de_deh[de->de_entry_num].deh_objectid = key->k_objectid;
1299 }
1300 
1301 /*
1302  * process, that is going to call fix_nodes/do_balance must hold only
1303  * one path. If it holds 2 or more, it can get into endless waiting in
1304  * get_empty_nodes or its clones
1305  */
1306 static int reiserfs_rename(struct mnt_idmap *idmap,
1307 			   struct inode *old_dir, struct dentry *old_dentry,
1308 			   struct inode *new_dir, struct dentry *new_dentry,
1309 			   unsigned int flags)
1310 {
1311 	int retval;
1312 	INITIALIZE_PATH(old_entry_path);
1313 	INITIALIZE_PATH(new_entry_path);
1314 	INITIALIZE_PATH(dot_dot_entry_path);
1315 	struct item_head new_entry_ih, old_entry_ih, dot_dot_ih;
1316 	struct reiserfs_dir_entry old_de, new_de, dot_dot_de;
1317 	struct inode *old_inode, *new_dentry_inode;
1318 	struct reiserfs_transaction_handle th;
1319 	int jbegin_count;
1320 	unsigned long savelink = 1;
1321 	bool update_dir_parent = false;
1322 
1323 	if (flags & ~RENAME_NOREPLACE)
1324 		return -EINVAL;
1325 
1326 	/*
1327 	 * three balancings: (1) old name removal, (2) new name insertion
1328 	 * and (3) maybe "save" link insertion
1329 	 * stat data updates: (1) old directory,
1330 	 * (2) new directory and (3) maybe old object stat data (when it is
1331 	 * directory) and (4) maybe stat data of object to which new entry
1332 	 * pointed initially and (5) maybe block containing ".." of
1333 	 * renamed directory
1334 	 * quota updates: two parent directories
1335 	 */
1336 	jbegin_count =
1337 	    JOURNAL_PER_BALANCE_CNT * 3 + 5 +
1338 	    4 * REISERFS_QUOTA_TRANS_BLOCKS(old_dir->i_sb);
1339 
1340 	retval = dquot_initialize(old_dir);
1341 	if (retval)
1342 		return retval;
1343 	retval = dquot_initialize(new_dir);
1344 	if (retval)
1345 		return retval;
1346 
1347 	old_inode = d_inode(old_dentry);
1348 	new_dentry_inode = d_inode(new_dentry);
1349 
1350 	/*
1351 	 * make sure that oldname still exists and points to an object we
1352 	 * are going to rename
1353 	 */
1354 	old_de.de_gen_number_bit_string = NULL;
1355 	reiserfs_write_lock(old_dir->i_sb);
1356 	retval =
1357 	    reiserfs_find_entry(old_dir, old_dentry->d_name.name,
1358 				old_dentry->d_name.len, &old_entry_path,
1359 				&old_de);
1360 	pathrelse(&old_entry_path);
1361 	if (retval == IO_ERROR) {
1362 		reiserfs_write_unlock(old_dir->i_sb);
1363 		return -EIO;
1364 	}
1365 
1366 	if (retval != NAME_FOUND || old_de.de_objectid != old_inode->i_ino) {
1367 		reiserfs_write_unlock(old_dir->i_sb);
1368 		return -ENOENT;
1369 	}
1370 
1371 	if (S_ISDIR(old_inode->i_mode)) {
1372 		/*
1373 		 * make sure that directory being renamed has correct ".."
1374 		 * and that its new parent directory has not too many links
1375 		 * already
1376 		 */
1377 		if (new_dentry_inode) {
1378 			if (!reiserfs_empty_dir(new_dentry_inode)) {
1379 				reiserfs_write_unlock(old_dir->i_sb);
1380 				return -ENOTEMPTY;
1381 			}
1382 		}
1383 
1384 		if (old_dir != new_dir) {
1385 			/*
1386 			 * directory is renamed, its parent directory will be
1387 			 * changed, so find ".." entry
1388 			 */
1389 			dot_dot_de.de_gen_number_bit_string = NULL;
1390 			retval =
1391 			    reiserfs_find_entry(old_inode, "..", 2,
1392 					&dot_dot_entry_path,
1393 					&dot_dot_de);
1394 			pathrelse(&dot_dot_entry_path);
1395 			if (retval != NAME_FOUND) {
1396 				reiserfs_write_unlock(old_dir->i_sb);
1397 				return -EIO;
1398 			}
1399 
1400 			/* inode number of .. must equal old_dir->i_ino */
1401 			if (dot_dot_de.de_objectid != old_dir->i_ino) {
1402 				reiserfs_write_unlock(old_dir->i_sb);
1403 				return -EIO;
1404 			}
1405 			update_dir_parent = true;
1406 		}
1407 	}
1408 
1409 	retval = journal_begin(&th, old_dir->i_sb, jbegin_count);
1410 	if (retval) {
1411 		reiserfs_write_unlock(old_dir->i_sb);
1412 		return retval;
1413 	}
1414 
1415 	/* add new entry (or find the existing one) */
1416 	retval =
1417 	    reiserfs_add_entry(&th, new_dir, new_dentry->d_name.name,
1418 			       new_dentry->d_name.len, old_inode, 0);
1419 	if (retval == -EEXIST) {
1420 		if (!new_dentry_inode) {
1421 			reiserfs_panic(old_dir->i_sb, "vs-7050",
1422 				       "new entry is found, new inode == 0");
1423 		}
1424 	} else if (retval) {
1425 		int err = journal_end(&th);
1426 		reiserfs_write_unlock(old_dir->i_sb);
1427 		return err ? err : retval;
1428 	}
1429 
1430 	reiserfs_update_inode_transaction(old_dir);
1431 	reiserfs_update_inode_transaction(new_dir);
1432 
1433 	/*
1434 	 * this makes it so an fsync on an open fd for the old name will
1435 	 * commit the rename operation
1436 	 */
1437 	reiserfs_update_inode_transaction(old_inode);
1438 
1439 	if (new_dentry_inode)
1440 		reiserfs_update_inode_transaction(new_dentry_inode);
1441 
1442 	while (1) {
1443 		/*
1444 		 * look for old name using corresponding entry key
1445 		 * (found by reiserfs_find_entry)
1446 		 */
1447 		if ((retval =
1448 		     search_by_entry_key(new_dir->i_sb, &old_de.de_entry_key,
1449 					 &old_entry_path,
1450 					 &old_de)) != NAME_FOUND) {
1451 			pathrelse(&old_entry_path);
1452 			journal_end(&th);
1453 			reiserfs_write_unlock(old_dir->i_sb);
1454 			return -EIO;
1455 		}
1456 
1457 		copy_item_head(&old_entry_ih, tp_item_head(&old_entry_path));
1458 
1459 		reiserfs_prepare_for_journal(old_inode->i_sb, old_de.de_bh, 1);
1460 
1461 		/* look for new name by reiserfs_find_entry */
1462 		new_de.de_gen_number_bit_string = NULL;
1463 		retval =
1464 		    reiserfs_find_entry(new_dir, new_dentry->d_name.name,
1465 					new_dentry->d_name.len, &new_entry_path,
1466 					&new_de);
1467 		/*
1468 		 * reiserfs_add_entry should not return IO_ERROR,
1469 		 * because it is called with essentially same parameters from
1470 		 * reiserfs_add_entry above, and we'll catch any i/o errors
1471 		 * before we get here.
1472 		 */
1473 		if (retval != NAME_FOUND_INVISIBLE && retval != NAME_FOUND) {
1474 			pathrelse(&new_entry_path);
1475 			pathrelse(&old_entry_path);
1476 			journal_end(&th);
1477 			reiserfs_write_unlock(old_dir->i_sb);
1478 			return -EIO;
1479 		}
1480 
1481 		copy_item_head(&new_entry_ih, tp_item_head(&new_entry_path));
1482 
1483 		reiserfs_prepare_for_journal(old_inode->i_sb, new_de.de_bh, 1);
1484 
1485 		if (update_dir_parent) {
1486 			if ((retval =
1487 			     search_by_entry_key(new_dir->i_sb,
1488 						 &dot_dot_de.de_entry_key,
1489 						 &dot_dot_entry_path,
1490 						 &dot_dot_de)) != NAME_FOUND) {
1491 				pathrelse(&dot_dot_entry_path);
1492 				pathrelse(&new_entry_path);
1493 				pathrelse(&old_entry_path);
1494 				journal_end(&th);
1495 				reiserfs_write_unlock(old_dir->i_sb);
1496 				return -EIO;
1497 			}
1498 			copy_item_head(&dot_dot_ih,
1499 				       tp_item_head(&dot_dot_entry_path));
1500 			/* node containing ".." gets into transaction */
1501 			reiserfs_prepare_for_journal(old_inode->i_sb,
1502 						     dot_dot_de.de_bh, 1);
1503 		}
1504 		/*
1505 		 * we should check seals here, not do
1506 		 * this stuff, yes? Then, having
1507 		 * gathered everything into RAM we
1508 		 * should lock the buffers, yes?  -Hans
1509 		 */
1510 		/*
1511 		 * probably.  our rename needs to hold more
1512 		 * than one path at once.  The seals would
1513 		 * have to be written to deal with multi-path
1514 		 * issues -chris
1515 		 */
1516 		/*
1517 		 * sanity checking before doing the rename - avoid races many
1518 		 * of the above checks could have scheduled.  We have to be
1519 		 * sure our items haven't been shifted by another process.
1520 		 */
1521 		if (item_moved(&new_entry_ih, &new_entry_path) ||
1522 		    !entry_points_to_object(new_dentry->d_name.name,
1523 					    new_dentry->d_name.len,
1524 					    &new_de, new_dentry_inode) ||
1525 		    item_moved(&old_entry_ih, &old_entry_path) ||
1526 		    !entry_points_to_object(old_dentry->d_name.name,
1527 					    old_dentry->d_name.len,
1528 					    &old_de, old_inode)) {
1529 			reiserfs_restore_prepared_buffer(old_inode->i_sb,
1530 							 new_de.de_bh);
1531 			reiserfs_restore_prepared_buffer(old_inode->i_sb,
1532 							 old_de.de_bh);
1533 			if (update_dir_parent)
1534 				reiserfs_restore_prepared_buffer(old_inode->
1535 								 i_sb,
1536 								 dot_dot_de.
1537 								 de_bh);
1538 			continue;
1539 		}
1540 		if (update_dir_parent) {
1541 			if (item_moved(&dot_dot_ih, &dot_dot_entry_path) ||
1542 			    !entry_points_to_object("..", 2, &dot_dot_de,
1543 						    old_dir)) {
1544 				reiserfs_restore_prepared_buffer(old_inode->
1545 								 i_sb,
1546 								 old_de.de_bh);
1547 				reiserfs_restore_prepared_buffer(old_inode->
1548 								 i_sb,
1549 								 new_de.de_bh);
1550 				reiserfs_restore_prepared_buffer(old_inode->
1551 								 i_sb,
1552 								 dot_dot_de.
1553 								 de_bh);
1554 				continue;
1555 			}
1556 		}
1557 
1558 		RFALSE(update_dir_parent &&
1559 		       !buffer_journal_prepared(dot_dot_de.de_bh), "");
1560 
1561 		break;
1562 	}
1563 
1564 	/*
1565 	 * ok, all the changes can be done in one fell swoop when we
1566 	 * have claimed all the buffers needed.
1567 	 */
1568 
1569 	mark_de_visible(new_de.de_deh + new_de.de_entry_num);
1570 	set_ino_in_dir_entry(&new_de, INODE_PKEY(old_inode));
1571 	journal_mark_dirty(&th, new_de.de_bh);
1572 
1573 	mark_de_hidden(old_de.de_deh + old_de.de_entry_num);
1574 	journal_mark_dirty(&th, old_de.de_bh);
1575 	/*
1576 	 * thanks to Alex Adriaanse <alex_a@caltech.edu> for patch
1577 	 * which adds ctime update of renamed object
1578 	 */
1579 	simple_rename_timestamp(old_dir, old_dentry, new_dir, new_dentry);
1580 
1581 	if (new_dentry_inode) {
1582 		/* adjust link number of the victim */
1583 		if (S_ISDIR(new_dentry_inode->i_mode)) {
1584 			clear_nlink(new_dentry_inode);
1585 		} else {
1586 			drop_nlink(new_dentry_inode);
1587 		}
1588 		savelink = new_dentry_inode->i_nlink;
1589 	}
1590 
1591 	if (update_dir_parent) {
1592 		/* adjust ".." of renamed directory */
1593 		set_ino_in_dir_entry(&dot_dot_de, INODE_PKEY(new_dir));
1594 		journal_mark_dirty(&th, dot_dot_de.de_bh);
1595 	}
1596 	if (S_ISDIR(old_inode->i_mode)) {
1597 		/*
1598 		 * there (in new_dir) was no directory, so it got new link
1599 		 * (".."  of renamed directory)
1600 		 */
1601 		if (!new_dentry_inode)
1602 			INC_DIR_INODE_NLINK(new_dir);
1603 
1604 		/* old directory lost one link - ".. " of renamed directory */
1605 		DEC_DIR_INODE_NLINK(old_dir);
1606 	}
1607 	/*
1608 	 * looks like in 2.3.99pre3 brelse is atomic.
1609 	 * so we can use pathrelse
1610 	 */
1611 	pathrelse(&new_entry_path);
1612 	pathrelse(&dot_dot_entry_path);
1613 
1614 	/*
1615 	 * FIXME: this reiserfs_cut_from_item's return value may screw up
1616 	 * anybody, but it will panic if will not be able to find the
1617 	 * entry. This needs one more clean up
1618 	 */
1619 	if (reiserfs_cut_from_item
1620 	    (&th, &old_entry_path, &old_de.de_entry_key, old_dir, NULL,
1621 	     0) < 0)
1622 		reiserfs_error(old_dir->i_sb, "vs-7060",
1623 			       "couldn't not cut old name. Fsck later?");
1624 
1625 	old_dir->i_size -= DEH_SIZE + old_de.de_entrylen;
1626 
1627 	reiserfs_update_sd(&th, old_dir);
1628 	reiserfs_update_sd(&th, new_dir);
1629 	reiserfs_update_sd(&th, old_inode);
1630 
1631 	if (new_dentry_inode) {
1632 		if (savelink == 0)
1633 			add_save_link(&th, new_dentry_inode,
1634 				      0 /* not truncate */ );
1635 		reiserfs_update_sd(&th, new_dentry_inode);
1636 	}
1637 
1638 	retval = journal_end(&th);
1639 	reiserfs_write_unlock(old_dir->i_sb);
1640 	return retval;
1641 }
1642 
1643 static const struct inode_operations reiserfs_priv_dir_inode_operations = {
1644 	.create = reiserfs_create,
1645 	.lookup = reiserfs_lookup,
1646 	.link = reiserfs_link,
1647 	.unlink = reiserfs_unlink,
1648 	.symlink = reiserfs_symlink,
1649 	.mkdir = reiserfs_mkdir,
1650 	.rmdir = reiserfs_rmdir,
1651 	.mknod = reiserfs_mknod,
1652 	.rename = reiserfs_rename,
1653 	.setattr = reiserfs_setattr,
1654 	.permission = reiserfs_permission,
1655 	.fileattr_get = reiserfs_fileattr_get,
1656 	.fileattr_set = reiserfs_fileattr_set,
1657 };
1658 
1659 static const struct inode_operations reiserfs_priv_symlink_inode_operations = {
1660 	.get_link	= page_get_link,
1661 	.setattr = reiserfs_setattr,
1662 	.permission = reiserfs_permission,
1663 };
1664 
1665 static const struct inode_operations reiserfs_priv_special_inode_operations = {
1666 	.setattr = reiserfs_setattr,
1667 	.permission = reiserfs_permission,
1668 };
1669 
1670 void reiserfs_init_priv_inode(struct inode *inode)
1671 {
1672 	inode->i_flags |= S_PRIVATE;
1673 	inode->i_opflags &= ~IOP_XATTR;
1674 
1675 	if (S_ISREG(inode->i_mode))
1676 		inode->i_op = &reiserfs_priv_file_inode_operations;
1677 	else if (S_ISDIR(inode->i_mode))
1678 		inode->i_op = &reiserfs_priv_dir_inode_operations;
1679 	else if (S_ISLNK(inode->i_mode))
1680 		inode->i_op = &reiserfs_priv_symlink_inode_operations;
1681 	else
1682 		inode->i_op = &reiserfs_priv_special_inode_operations;
1683 }
1684 
1685 /* directories can handle most operations...  */
1686 const struct inode_operations reiserfs_dir_inode_operations = {
1687 	.create = reiserfs_create,
1688 	.lookup = reiserfs_lookup,
1689 	.link = reiserfs_link,
1690 	.unlink = reiserfs_unlink,
1691 	.symlink = reiserfs_symlink,
1692 	.mkdir = reiserfs_mkdir,
1693 	.rmdir = reiserfs_rmdir,
1694 	.mknod = reiserfs_mknod,
1695 	.rename = reiserfs_rename,
1696 	.setattr = reiserfs_setattr,
1697 	.listxattr = reiserfs_listxattr,
1698 	.permission = reiserfs_permission,
1699 	.get_inode_acl = reiserfs_get_acl,
1700 	.set_acl = reiserfs_set_acl,
1701 	.fileattr_get = reiserfs_fileattr_get,
1702 	.fileattr_set = reiserfs_fileattr_set,
1703 };
1704 
1705 /*
1706  * symlink operations.. same as page_symlink_inode_operations, with xattr
1707  * stuff added
1708  */
1709 const struct inode_operations reiserfs_symlink_inode_operations = {
1710 	.get_link	= page_get_link,
1711 	.setattr = reiserfs_setattr,
1712 	.listxattr = reiserfs_listxattr,
1713 	.permission = reiserfs_permission,
1714 };
1715 
1716 /*
1717  * special file operations.. just xattr/acl stuff
1718  */
1719 const struct inode_operations reiserfs_special_inode_operations = {
1720 	.setattr = reiserfs_setattr,
1721 	.listxattr = reiserfs_listxattr,
1722 	.permission = reiserfs_permission,
1723 	.get_inode_acl = reiserfs_get_acl,
1724 	.set_acl = reiserfs_set_acl,
1725 };
1726