xref: /openbmc/linux/fs/reiserfs/namei.c (revision 5e0266f0)
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 IOP_XATTR
382 		 * since we don't have xattrs on xattr files.
383 		 */
384 		if (IS_PRIVATE(dir)) {
385 			inode->i_flags |= S_PRIVATE;
386 			inode->i_opflags &= ~IOP_XATTR;
387 		}
388 	}
389 	reiserfs_write_unlock(dir->i_sb);
390 	if (retval == IO_ERROR) {
391 		return ERR_PTR(-EIO);
392 	}
393 
394 	return d_splice_alias(inode, dentry);
395 }
396 
397 /*
398  * looks up the dentry of the parent directory for child.
399  * taken from ext2_get_parent
400  */
401 struct dentry *reiserfs_get_parent(struct dentry *child)
402 {
403 	int retval;
404 	struct inode *inode = NULL;
405 	struct reiserfs_dir_entry de;
406 	INITIALIZE_PATH(path_to_entry);
407 	struct inode *dir = d_inode(child);
408 
409 	if (dir->i_nlink == 0) {
410 		return ERR_PTR(-ENOENT);
411 	}
412 	de.de_gen_number_bit_string = NULL;
413 
414 	reiserfs_write_lock(dir->i_sb);
415 	retval = reiserfs_find_entry(dir, "..", 2, &path_to_entry, &de);
416 	pathrelse(&path_to_entry);
417 	if (retval != NAME_FOUND) {
418 		reiserfs_write_unlock(dir->i_sb);
419 		return ERR_PTR(-ENOENT);
420 	}
421 	inode = reiserfs_iget(dir->i_sb, (struct cpu_key *)&de.de_dir_id);
422 	reiserfs_write_unlock(dir->i_sb);
423 
424 	return d_obtain_alias(inode);
425 }
426 
427 /* add entry to the directory (entry can be hidden).
428 
429 insert definition of when hidden directories are used here -Hans
430 
431  Does not mark dir   inode dirty, do it after successesfull call to it */
432 
433 static int reiserfs_add_entry(struct reiserfs_transaction_handle *th,
434 			      struct inode *dir, const char *name, int namelen,
435 			      struct inode *inode, int visible)
436 {
437 	struct cpu_key entry_key;
438 	struct reiserfs_de_head *deh;
439 	INITIALIZE_PATH(path);
440 	struct reiserfs_dir_entry de;
441 	DECLARE_BITMAP(bit_string, MAX_GENERATION_NUMBER + 1);
442 	int gen_number;
443 
444 	/*
445 	 * 48 bytes now and we avoid kmalloc if we
446 	 * create file with short name
447 	 */
448 	char small_buf[32 + DEH_SIZE];
449 
450 	char *buffer;
451 	int buflen, paste_size;
452 	int retval;
453 
454 	BUG_ON(!th->t_trans_id);
455 
456 	/* cannot allow items to be added into a busy deleted directory */
457 	if (!namelen)
458 		return -EINVAL;
459 
460 	if (namelen > REISERFS_MAX_NAME(dir->i_sb->s_blocksize))
461 		return -ENAMETOOLONG;
462 
463 	/* each entry has unique key. compose it */
464 	make_cpu_key(&entry_key, dir,
465 		     get_third_component(dir->i_sb, name, namelen),
466 		     TYPE_DIRENTRY, 3);
467 
468 	/* get memory for composing the entry */
469 	buflen = DEH_SIZE + ROUND_UP(namelen);
470 	if (buflen > sizeof(small_buf)) {
471 		buffer = kmalloc(buflen, GFP_NOFS);
472 		if (!buffer)
473 			return -ENOMEM;
474 	} else
475 		buffer = small_buf;
476 
477 	paste_size =
478 	    (get_inode_sd_version(dir) ==
479 	     STAT_DATA_V1) ? (DEH_SIZE + namelen) : buflen;
480 
481 	/*
482 	 * fill buffer : directory entry head, name[, dir objectid | ,
483 	 * stat data | ,stat data, dir objectid ]
484 	 */
485 	deh = (struct reiserfs_de_head *)buffer;
486 	deh->deh_location = 0;	/* JDM Endian safe if 0 */
487 	put_deh_offset(deh, cpu_key_k_offset(&entry_key));
488 	deh->deh_state = 0;	/* JDM Endian safe if 0 */
489 	/* put key (ino analog) to de */
490 
491 	/* safe: k_dir_id is le */
492 	deh->deh_dir_id = INODE_PKEY(inode)->k_dir_id;
493 	/* safe: k_objectid is le */
494 	deh->deh_objectid = INODE_PKEY(inode)->k_objectid;
495 
496 	/* copy name */
497 	memcpy((char *)(deh + 1), name, namelen);
498 	/* padd by 0s to the 4 byte boundary */
499 	padd_item((char *)(deh + 1), ROUND_UP(namelen), namelen);
500 
501 	/*
502 	 * entry is ready to be pasted into tree, set 'visibility'
503 	 * and 'stat data in entry' attributes
504 	 */
505 	mark_de_without_sd(deh);
506 	visible ? mark_de_visible(deh) : mark_de_hidden(deh);
507 
508 	/* find the proper place for the new entry */
509 	memset(bit_string, 0, sizeof(bit_string));
510 	de.de_gen_number_bit_string = bit_string;
511 	retval = reiserfs_find_entry(dir, name, namelen, &path, &de);
512 	if (retval != NAME_NOT_FOUND) {
513 		if (buffer != small_buf)
514 			kfree(buffer);
515 		pathrelse(&path);
516 
517 		if (retval == IO_ERROR) {
518 			return -EIO;
519 		}
520 
521 		if (retval != NAME_FOUND) {
522 			reiserfs_error(dir->i_sb, "zam-7002",
523 				       "reiserfs_find_entry() returned "
524 				       "unexpected value (%d)", retval);
525 		}
526 
527 		return -EEXIST;
528 	}
529 
530 	gen_number =
531 	    find_first_zero_bit(bit_string,
532 				MAX_GENERATION_NUMBER + 1);
533 	if (gen_number > MAX_GENERATION_NUMBER) {
534 		/* there is no free generation number */
535 		reiserfs_warning(dir->i_sb, "reiserfs-7010",
536 				 "Congratulations! we have got hash function "
537 				 "screwed up");
538 		if (buffer != small_buf)
539 			kfree(buffer);
540 		pathrelse(&path);
541 		return -EBUSY;
542 	}
543 	/* adjust offset of directory enrty */
544 	put_deh_offset(deh, SET_GENERATION_NUMBER(deh_offset(deh), gen_number));
545 	set_cpu_key_k_offset(&entry_key, deh_offset(deh));
546 
547 	/* update max-hash-collisions counter in reiserfs_sb_info */
548 	PROC_INFO_MAX(th->t_super, max_hash_collisions, gen_number);
549 
550 	/* we need to re-search for the insertion point */
551 	if (gen_number != 0) {
552 		if (search_by_entry_key(dir->i_sb, &entry_key, &path, &de) !=
553 		    NAME_NOT_FOUND) {
554 			reiserfs_warning(dir->i_sb, "vs-7032",
555 					 "entry with this key (%K) already "
556 					 "exists", &entry_key);
557 
558 			if (buffer != small_buf)
559 				kfree(buffer);
560 			pathrelse(&path);
561 			return -EBUSY;
562 		}
563 	}
564 
565 	/* perform the insertion of the entry that we have prepared */
566 	retval =
567 	    reiserfs_paste_into_item(th, &path, &entry_key, dir, buffer,
568 				     paste_size);
569 	if (buffer != small_buf)
570 		kfree(buffer);
571 	if (retval) {
572 		reiserfs_check_path(&path);
573 		return retval;
574 	}
575 
576 	dir->i_size += paste_size;
577 	dir->i_mtime = dir->i_ctime = current_time(dir);
578 	if (!S_ISDIR(inode->i_mode) && visible)
579 		/* reiserfs_mkdir or reiserfs_rename will do that by itself */
580 		reiserfs_update_sd(th, dir);
581 
582 	reiserfs_check_path(&path);
583 	return 0;
584 }
585 
586 /*
587  * quota utility function, call if you've had to abort after calling
588  * new_inode_init, and have not called reiserfs_new_inode yet.
589  * This should only be called on inodes that do not have stat data
590  * inserted into the tree yet.
591  */
592 static int drop_new_inode(struct inode *inode)
593 {
594 	dquot_drop(inode);
595 	make_bad_inode(inode);
596 	inode->i_flags |= S_NOQUOTA;
597 	iput(inode);
598 	return 0;
599 }
600 
601 /*
602  * utility function that does setup for reiserfs_new_inode.
603  * dquot_initialize needs lots of credits so it's better to have it
604  * outside of a transaction, so we had to pull some bits of
605  * reiserfs_new_inode out into this func.
606  */
607 static int new_inode_init(struct inode *inode, struct inode *dir, umode_t mode)
608 {
609 	/*
610 	 * Make inode invalid - just in case we are going to drop it before
611 	 * the initialization happens
612 	 */
613 	INODE_PKEY(inode)->k_objectid = 0;
614 
615 	/*
616 	 * the quota init calls have to know who to charge the quota to, so
617 	 * we have to set uid and gid here
618 	 */
619 	inode_init_owner(&nop_mnt_idmap, inode, dir, mode);
620 	return dquot_initialize(inode);
621 }
622 
623 static int reiserfs_create(struct mnt_idmap *idmap, struct inode *dir,
624 			   struct dentry *dentry, umode_t mode, bool excl)
625 {
626 	int retval;
627 	struct inode *inode;
628 	/*
629 	 * We need blocks for transaction + (user+group)*(quotas
630 	 * for new inode + update of quota for directory owner)
631 	 */
632 	int jbegin_count =
633 	    JOURNAL_PER_BALANCE_CNT * 2 +
634 	    2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
635 		 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
636 	struct reiserfs_transaction_handle th;
637 	struct reiserfs_security_handle security;
638 
639 	retval = dquot_initialize(dir);
640 	if (retval)
641 		return retval;
642 
643 	if (!(inode = new_inode(dir->i_sb))) {
644 		return -ENOMEM;
645 	}
646 	retval = new_inode_init(inode, dir, mode);
647 	if (retval) {
648 		drop_new_inode(inode);
649 		return retval;
650 	}
651 
652 	jbegin_count += reiserfs_cache_default_acl(dir);
653 	retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security);
654 	if (retval < 0) {
655 		drop_new_inode(inode);
656 		return retval;
657 	}
658 	jbegin_count += retval;
659 	reiserfs_write_lock(dir->i_sb);
660 
661 	retval = journal_begin(&th, dir->i_sb, jbegin_count);
662 	if (retval) {
663 		drop_new_inode(inode);
664 		goto out_failed;
665 	}
666 
667 	retval =
668 	    reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
669 			       inode, &security);
670 	if (retval)
671 		goto out_failed;
672 
673 	inode->i_op = &reiserfs_file_inode_operations;
674 	inode->i_fop = &reiserfs_file_operations;
675 	inode->i_mapping->a_ops = &reiserfs_address_space_operations;
676 
677 	retval =
678 	    reiserfs_add_entry(&th, dir, dentry->d_name.name,
679 			       dentry->d_name.len, inode, 1 /*visible */ );
680 	if (retval) {
681 		int err;
682 		drop_nlink(inode);
683 		reiserfs_update_sd(&th, inode);
684 		err = journal_end(&th);
685 		if (err)
686 			retval = err;
687 		unlock_new_inode(inode);
688 		iput(inode);
689 		goto out_failed;
690 	}
691 	reiserfs_update_inode_transaction(inode);
692 	reiserfs_update_inode_transaction(dir);
693 
694 	d_instantiate_new(dentry, inode);
695 	retval = journal_end(&th);
696 
697 out_failed:
698 	reiserfs_write_unlock(dir->i_sb);
699 	reiserfs_security_free(&security);
700 	return retval;
701 }
702 
703 static int reiserfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
704 			  struct dentry *dentry, umode_t mode, dev_t rdev)
705 {
706 	int retval;
707 	struct inode *inode;
708 	struct reiserfs_transaction_handle th;
709 	struct reiserfs_security_handle security;
710 	/*
711 	 * We need blocks for transaction + (user+group)*(quotas
712 	 * for new inode + update of quota for directory owner)
713 	 */
714 	int jbegin_count =
715 	    JOURNAL_PER_BALANCE_CNT * 3 +
716 	    2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
717 		 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
718 
719 	retval = dquot_initialize(dir);
720 	if (retval)
721 		return retval;
722 
723 	if (!(inode = new_inode(dir->i_sb))) {
724 		return -ENOMEM;
725 	}
726 	retval = new_inode_init(inode, dir, mode);
727 	if (retval) {
728 		drop_new_inode(inode);
729 		return retval;
730 	}
731 
732 	jbegin_count += reiserfs_cache_default_acl(dir);
733 	retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security);
734 	if (retval < 0) {
735 		drop_new_inode(inode);
736 		return retval;
737 	}
738 	jbegin_count += retval;
739 	reiserfs_write_lock(dir->i_sb);
740 
741 	retval = journal_begin(&th, dir->i_sb, jbegin_count);
742 	if (retval) {
743 		drop_new_inode(inode);
744 		goto out_failed;
745 	}
746 
747 	retval =
748 	    reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
749 			       inode, &security);
750 	if (retval) {
751 		goto out_failed;
752 	}
753 
754 	inode->i_op = &reiserfs_special_inode_operations;
755 	init_special_inode(inode, inode->i_mode, rdev);
756 
757 	/* FIXME: needed for block and char devices only */
758 	reiserfs_update_sd(&th, inode);
759 
760 	reiserfs_update_inode_transaction(inode);
761 	reiserfs_update_inode_transaction(dir);
762 
763 	retval =
764 	    reiserfs_add_entry(&th, dir, dentry->d_name.name,
765 			       dentry->d_name.len, inode, 1 /*visible */ );
766 	if (retval) {
767 		int err;
768 		drop_nlink(inode);
769 		reiserfs_update_sd(&th, inode);
770 		err = journal_end(&th);
771 		if (err)
772 			retval = err;
773 		unlock_new_inode(inode);
774 		iput(inode);
775 		goto out_failed;
776 	}
777 
778 	d_instantiate_new(dentry, inode);
779 	retval = journal_end(&th);
780 
781 out_failed:
782 	reiserfs_write_unlock(dir->i_sb);
783 	reiserfs_security_free(&security);
784 	return retval;
785 }
786 
787 static int reiserfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
788 			  struct dentry *dentry, umode_t mode)
789 {
790 	int retval;
791 	struct inode *inode;
792 	struct reiserfs_transaction_handle th;
793 	struct reiserfs_security_handle security;
794 	/*
795 	 * We need blocks for transaction + (user+group)*(quotas
796 	 * for new inode + update of quota for directory owner)
797 	 */
798 	int jbegin_count =
799 	    JOURNAL_PER_BALANCE_CNT * 3 +
800 	    2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
801 		 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
802 
803 	retval = dquot_initialize(dir);
804 	if (retval)
805 		return retval;
806 
807 #ifdef DISPLACE_NEW_PACKING_LOCALITIES
808 	/*
809 	 * set flag that new packing locality created and new blocks
810 	 * for the content of that directory are not displaced yet
811 	 */
812 	REISERFS_I(dir)->new_packing_locality = 1;
813 #endif
814 	mode = S_IFDIR | mode;
815 	if (!(inode = new_inode(dir->i_sb))) {
816 		return -ENOMEM;
817 	}
818 	retval = new_inode_init(inode, dir, mode);
819 	if (retval) {
820 		drop_new_inode(inode);
821 		return retval;
822 	}
823 
824 	jbegin_count += reiserfs_cache_default_acl(dir);
825 	retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security);
826 	if (retval < 0) {
827 		drop_new_inode(inode);
828 		return retval;
829 	}
830 	jbegin_count += retval;
831 	reiserfs_write_lock(dir->i_sb);
832 
833 	retval = journal_begin(&th, dir->i_sb, jbegin_count);
834 	if (retval) {
835 		drop_new_inode(inode);
836 		goto out_failed;
837 	}
838 
839 	/*
840 	 * inc the link count now, so another writer doesn't overflow
841 	 * it while we sleep later on.
842 	 */
843 	INC_DIR_INODE_NLINK(dir)
844 
845 	retval = reiserfs_new_inode(&th, dir, mode, NULL /*symlink */,
846 				    old_format_only(dir->i_sb) ?
847 				    EMPTY_DIR_SIZE_V1 : EMPTY_DIR_SIZE,
848 				    dentry, inode, &security);
849 	if (retval) {
850 		DEC_DIR_INODE_NLINK(dir)
851 		goto out_failed;
852 	}
853 
854 	reiserfs_update_inode_transaction(inode);
855 	reiserfs_update_inode_transaction(dir);
856 
857 	inode->i_op = &reiserfs_dir_inode_operations;
858 	inode->i_fop = &reiserfs_dir_operations;
859 
860 	/* note, _this_ add_entry will not update dir's stat data */
861 	retval =
862 	    reiserfs_add_entry(&th, dir, dentry->d_name.name,
863 			       dentry->d_name.len, inode, 1 /*visible */ );
864 	if (retval) {
865 		int err;
866 		clear_nlink(inode);
867 		DEC_DIR_INODE_NLINK(dir);
868 		reiserfs_update_sd(&th, inode);
869 		err = journal_end(&th);
870 		if (err)
871 			retval = err;
872 		unlock_new_inode(inode);
873 		iput(inode);
874 		goto out_failed;
875 	}
876 	/* the above add_entry did not update dir's stat data */
877 	reiserfs_update_sd(&th, dir);
878 
879 	d_instantiate_new(dentry, inode);
880 	retval = journal_end(&th);
881 out_failed:
882 	reiserfs_write_unlock(dir->i_sb);
883 	reiserfs_security_free(&security);
884 	return retval;
885 }
886 
887 static inline int reiserfs_empty_dir(struct inode *inode)
888 {
889 	/*
890 	 * we can cheat because an old format dir cannot have
891 	 * EMPTY_DIR_SIZE, and a new format dir cannot have
892 	 * EMPTY_DIR_SIZE_V1.  So, if the inode is either size,
893 	 * regardless of disk format version, the directory is empty.
894 	 */
895 	if (inode->i_size != EMPTY_DIR_SIZE &&
896 	    inode->i_size != EMPTY_DIR_SIZE_V1) {
897 		return 0;
898 	}
899 	return 1;
900 }
901 
902 static int reiserfs_rmdir(struct inode *dir, struct dentry *dentry)
903 {
904 	int retval, err;
905 	struct inode *inode;
906 	struct reiserfs_transaction_handle th;
907 	int jbegin_count;
908 	INITIALIZE_PATH(path);
909 	struct reiserfs_dir_entry de;
910 
911 	/*
912 	 * we will be doing 2 balancings and update 2 stat data, we
913 	 * change quotas of the owner of the directory and of the owner
914 	 * of the parent directory.  The quota structure is possibly
915 	 * deleted only on last iput => outside of this transaction
916 	 */
917 	jbegin_count =
918 	    JOURNAL_PER_BALANCE_CNT * 2 + 2 +
919 	    4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
920 
921 	retval = dquot_initialize(dir);
922 	if (retval)
923 		return retval;
924 
925 	reiserfs_write_lock(dir->i_sb);
926 	retval = journal_begin(&th, dir->i_sb, jbegin_count);
927 	if (retval)
928 		goto out_rmdir;
929 
930 	de.de_gen_number_bit_string = NULL;
931 	if ((retval =
932 	     reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
933 				 &path, &de)) == NAME_NOT_FOUND) {
934 		retval = -ENOENT;
935 		goto end_rmdir;
936 	} else if (retval == IO_ERROR) {
937 		retval = -EIO;
938 		goto end_rmdir;
939 	}
940 
941 	inode = d_inode(dentry);
942 
943 	reiserfs_update_inode_transaction(inode);
944 	reiserfs_update_inode_transaction(dir);
945 
946 	if (de.de_objectid != inode->i_ino) {
947 		/*
948 		 * FIXME: compare key of an object and a key found in the entry
949 		 */
950 		retval = -EIO;
951 		goto end_rmdir;
952 	}
953 	if (!reiserfs_empty_dir(inode)) {
954 		retval = -ENOTEMPTY;
955 		goto end_rmdir;
956 	}
957 
958 	/* cut entry from dir directory */
959 	retval = reiserfs_cut_from_item(&th, &path, &de.de_entry_key,
960 					dir, NULL,	/* page */
961 					0 /*new file size - not used here */ );
962 	if (retval < 0)
963 		goto end_rmdir;
964 
965 	if (inode->i_nlink != 2 && inode->i_nlink != 1)
966 		reiserfs_error(inode->i_sb, "reiserfs-7040",
967 			       "empty directory has nlink != 2 (%d)",
968 			       inode->i_nlink);
969 
970 	clear_nlink(inode);
971 	inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(dir);
972 	reiserfs_update_sd(&th, inode);
973 
974 	DEC_DIR_INODE_NLINK(dir)
975 	dir->i_size -= (DEH_SIZE + de.de_entrylen);
976 	reiserfs_update_sd(&th, dir);
977 
978 	/* prevent empty directory from getting lost */
979 	add_save_link(&th, inode, 0 /* not truncate */ );
980 
981 	retval = journal_end(&th);
982 	reiserfs_check_path(&path);
983 out_rmdir:
984 	reiserfs_write_unlock(dir->i_sb);
985 	return retval;
986 
987 end_rmdir:
988 	/*
989 	 * we must release path, because we did not call
990 	 * reiserfs_cut_from_item, or reiserfs_cut_from_item does not
991 	 * release path if operation was not complete
992 	 */
993 	pathrelse(&path);
994 	err = journal_end(&th);
995 	reiserfs_write_unlock(dir->i_sb);
996 	return err ? err : retval;
997 }
998 
999 static int reiserfs_unlink(struct inode *dir, struct dentry *dentry)
1000 {
1001 	int retval, err;
1002 	struct inode *inode;
1003 	struct reiserfs_dir_entry de;
1004 	INITIALIZE_PATH(path);
1005 	struct reiserfs_transaction_handle th;
1006 	int jbegin_count;
1007 	unsigned long savelink;
1008 
1009 	retval = dquot_initialize(dir);
1010 	if (retval)
1011 		return retval;
1012 
1013 	inode = d_inode(dentry);
1014 
1015 	/*
1016 	 * in this transaction we can be doing at max two balancings and
1017 	 * update two stat datas, we change quotas of the owner of the
1018 	 * directory and of the owner of the parent directory. The quota
1019 	 * structure is possibly deleted only on iput => outside of
1020 	 * this transaction
1021 	 */
1022 	jbegin_count =
1023 	    JOURNAL_PER_BALANCE_CNT * 2 + 2 +
1024 	    4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
1025 
1026 	reiserfs_write_lock(dir->i_sb);
1027 	retval = journal_begin(&th, dir->i_sb, jbegin_count);
1028 	if (retval)
1029 		goto out_unlink;
1030 
1031 	de.de_gen_number_bit_string = NULL;
1032 	if ((retval =
1033 	     reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
1034 				 &path, &de)) == NAME_NOT_FOUND) {
1035 		retval = -ENOENT;
1036 		goto end_unlink;
1037 	} else if (retval == IO_ERROR) {
1038 		retval = -EIO;
1039 		goto end_unlink;
1040 	}
1041 
1042 	reiserfs_update_inode_transaction(inode);
1043 	reiserfs_update_inode_transaction(dir);
1044 
1045 	if (de.de_objectid != inode->i_ino) {
1046 		/*
1047 		 * FIXME: compare key of an object and a key found in the entry
1048 		 */
1049 		retval = -EIO;
1050 		goto end_unlink;
1051 	}
1052 
1053 	if (!inode->i_nlink) {
1054 		reiserfs_warning(inode->i_sb, "reiserfs-7042",
1055 				 "deleting nonexistent file (%lu), %d",
1056 				 inode->i_ino, inode->i_nlink);
1057 		set_nlink(inode, 1);
1058 	}
1059 
1060 	drop_nlink(inode);
1061 
1062 	/*
1063 	 * we schedule before doing the add_save_link call, save the link
1064 	 * count so we don't race
1065 	 */
1066 	savelink = inode->i_nlink;
1067 
1068 	retval =
1069 	    reiserfs_cut_from_item(&th, &path, &de.de_entry_key, dir, NULL,
1070 				   0);
1071 	if (retval < 0) {
1072 		inc_nlink(inode);
1073 		goto end_unlink;
1074 	}
1075 	inode->i_ctime = current_time(inode);
1076 	reiserfs_update_sd(&th, inode);
1077 
1078 	dir->i_size -= (de.de_entrylen + DEH_SIZE);
1079 	dir->i_ctime = dir->i_mtime = current_time(dir);
1080 	reiserfs_update_sd(&th, dir);
1081 
1082 	if (!savelink)
1083 		/* prevent file from getting lost */
1084 		add_save_link(&th, inode, 0 /* not truncate */ );
1085 
1086 	retval = journal_end(&th);
1087 	reiserfs_check_path(&path);
1088 	reiserfs_write_unlock(dir->i_sb);
1089 	return retval;
1090 
1091 end_unlink:
1092 	pathrelse(&path);
1093 	err = journal_end(&th);
1094 	reiserfs_check_path(&path);
1095 	if (err)
1096 		retval = err;
1097 out_unlink:
1098 	reiserfs_write_unlock(dir->i_sb);
1099 	return retval;
1100 }
1101 
1102 static int reiserfs_symlink(struct mnt_idmap *idmap,
1103 			    struct inode *parent_dir, struct dentry *dentry,
1104 			    const char *symname)
1105 {
1106 	int retval;
1107 	struct inode *inode;
1108 	char *name;
1109 	int item_len;
1110 	struct reiserfs_transaction_handle th;
1111 	struct reiserfs_security_handle security;
1112 	int mode = S_IFLNK | S_IRWXUGO;
1113 	/*
1114 	 * We need blocks for transaction + (user+group)*(quotas for
1115 	 * new inode + update of quota for directory owner)
1116 	 */
1117 	int jbegin_count =
1118 	    JOURNAL_PER_BALANCE_CNT * 3 +
1119 	    2 * (REISERFS_QUOTA_INIT_BLOCKS(parent_dir->i_sb) +
1120 		 REISERFS_QUOTA_TRANS_BLOCKS(parent_dir->i_sb));
1121 
1122 	retval = dquot_initialize(parent_dir);
1123 	if (retval)
1124 		return retval;
1125 
1126 	if (!(inode = new_inode(parent_dir->i_sb))) {
1127 		return -ENOMEM;
1128 	}
1129 	retval = new_inode_init(inode, parent_dir, mode);
1130 	if (retval) {
1131 		drop_new_inode(inode);
1132 		return retval;
1133 	}
1134 
1135 	retval = reiserfs_security_init(parent_dir, inode, &dentry->d_name,
1136 					&security);
1137 	if (retval < 0) {
1138 		drop_new_inode(inode);
1139 		return retval;
1140 	}
1141 	jbegin_count += retval;
1142 
1143 	reiserfs_write_lock(parent_dir->i_sb);
1144 	item_len = ROUND_UP(strlen(symname));
1145 	if (item_len > MAX_DIRECT_ITEM_LEN(parent_dir->i_sb->s_blocksize)) {
1146 		retval = -ENAMETOOLONG;
1147 		drop_new_inode(inode);
1148 		goto out_failed;
1149 	}
1150 
1151 	name = kmalloc(item_len, GFP_NOFS);
1152 	if (!name) {
1153 		drop_new_inode(inode);
1154 		retval = -ENOMEM;
1155 		goto out_failed;
1156 	}
1157 	memcpy(name, symname, strlen(symname));
1158 	padd_item(name, item_len, strlen(symname));
1159 
1160 	retval = journal_begin(&th, parent_dir->i_sb, jbegin_count);
1161 	if (retval) {
1162 		drop_new_inode(inode);
1163 		kfree(name);
1164 		goto out_failed;
1165 	}
1166 
1167 	retval =
1168 	    reiserfs_new_inode(&th, parent_dir, mode, name, strlen(symname),
1169 			       dentry, inode, &security);
1170 	kfree(name);
1171 	if (retval) {		/* reiserfs_new_inode iputs for us */
1172 		goto out_failed;
1173 	}
1174 
1175 	reiserfs_update_inode_transaction(inode);
1176 	reiserfs_update_inode_transaction(parent_dir);
1177 
1178 	inode->i_op = &reiserfs_symlink_inode_operations;
1179 	inode_nohighmem(inode);
1180 	inode->i_mapping->a_ops = &reiserfs_address_space_operations;
1181 
1182 	retval = reiserfs_add_entry(&th, parent_dir, dentry->d_name.name,
1183 				    dentry->d_name.len, inode, 1 /*visible */ );
1184 	if (retval) {
1185 		int err;
1186 		drop_nlink(inode);
1187 		reiserfs_update_sd(&th, inode);
1188 		err = journal_end(&th);
1189 		if (err)
1190 			retval = err;
1191 		unlock_new_inode(inode);
1192 		iput(inode);
1193 		goto out_failed;
1194 	}
1195 
1196 	d_instantiate_new(dentry, inode);
1197 	retval = journal_end(&th);
1198 out_failed:
1199 	reiserfs_write_unlock(parent_dir->i_sb);
1200 	reiserfs_security_free(&security);
1201 	return retval;
1202 }
1203 
1204 static int reiserfs_link(struct dentry *old_dentry, struct inode *dir,
1205 			 struct dentry *dentry)
1206 {
1207 	int retval;
1208 	struct inode *inode = d_inode(old_dentry);
1209 	struct reiserfs_transaction_handle th;
1210 	/*
1211 	 * We need blocks for transaction + update of quotas for
1212 	 * the owners of the directory
1213 	 */
1214 	int jbegin_count =
1215 	    JOURNAL_PER_BALANCE_CNT * 3 +
1216 	    2 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
1217 
1218 	retval = dquot_initialize(dir);
1219 	if (retval)
1220 		return retval;
1221 
1222 	reiserfs_write_lock(dir->i_sb);
1223 	if (inode->i_nlink >= REISERFS_LINK_MAX) {
1224 		/* FIXME: sd_nlink is 32 bit for new files */
1225 		reiserfs_write_unlock(dir->i_sb);
1226 		return -EMLINK;
1227 	}
1228 
1229 	/* inc before scheduling so reiserfs_unlink knows we are here */
1230 	inc_nlink(inode);
1231 
1232 	retval = journal_begin(&th, dir->i_sb, jbegin_count);
1233 	if (retval) {
1234 		drop_nlink(inode);
1235 		reiserfs_write_unlock(dir->i_sb);
1236 		return retval;
1237 	}
1238 
1239 	/* create new entry */
1240 	retval =
1241 	    reiserfs_add_entry(&th, dir, dentry->d_name.name,
1242 			       dentry->d_name.len, inode, 1 /*visible */ );
1243 
1244 	reiserfs_update_inode_transaction(inode);
1245 	reiserfs_update_inode_transaction(dir);
1246 
1247 	if (retval) {
1248 		int err;
1249 		drop_nlink(inode);
1250 		err = journal_end(&th);
1251 		reiserfs_write_unlock(dir->i_sb);
1252 		return err ? err : retval;
1253 	}
1254 
1255 	inode->i_ctime = current_time(inode);
1256 	reiserfs_update_sd(&th, inode);
1257 
1258 	ihold(inode);
1259 	d_instantiate(dentry, inode);
1260 	retval = journal_end(&th);
1261 	reiserfs_write_unlock(dir->i_sb);
1262 	return retval;
1263 }
1264 
1265 /* de contains information pointing to an entry which */
1266 static int de_still_valid(const char *name, int len,
1267 			  struct reiserfs_dir_entry *de)
1268 {
1269 	struct reiserfs_dir_entry tmp = *de;
1270 
1271 	/* recalculate pointer to name and name length */
1272 	set_de_name_and_namelen(&tmp);
1273 	/* FIXME: could check more */
1274 	if (tmp.de_namelen != len || memcmp(name, de->de_name, len))
1275 		return 0;
1276 	return 1;
1277 }
1278 
1279 static int entry_points_to_object(const char *name, int len,
1280 				  struct reiserfs_dir_entry *de,
1281 				  struct inode *inode)
1282 {
1283 	if (!de_still_valid(name, len, de))
1284 		return 0;
1285 
1286 	if (inode) {
1287 		if (!de_visible(de->de_deh + de->de_entry_num))
1288 			reiserfs_panic(inode->i_sb, "vs-7042",
1289 				       "entry must be visible");
1290 		return (de->de_objectid == inode->i_ino) ? 1 : 0;
1291 	}
1292 
1293 	/* this must be added hidden entry */
1294 	if (de_visible(de->de_deh + de->de_entry_num))
1295 		reiserfs_panic(NULL, "vs-7043", "entry must be visible");
1296 
1297 	return 1;
1298 }
1299 
1300 /* sets key of objectid the entry has to point to */
1301 static void set_ino_in_dir_entry(struct reiserfs_dir_entry *de,
1302 				 struct reiserfs_key *key)
1303 {
1304 	/* JDM These operations are endian safe - both are le */
1305 	de->de_deh[de->de_entry_num].deh_dir_id = key->k_dir_id;
1306 	de->de_deh[de->de_entry_num].deh_objectid = key->k_objectid;
1307 }
1308 
1309 /*
1310  * process, that is going to call fix_nodes/do_balance must hold only
1311  * one path. If it holds 2 or more, it can get into endless waiting in
1312  * get_empty_nodes or its clones
1313  */
1314 static int reiserfs_rename(struct mnt_idmap *idmap,
1315 			   struct inode *old_dir, struct dentry *old_dentry,
1316 			   struct inode *new_dir, struct dentry *new_dentry,
1317 			   unsigned int flags)
1318 {
1319 	int retval;
1320 	INITIALIZE_PATH(old_entry_path);
1321 	INITIALIZE_PATH(new_entry_path);
1322 	INITIALIZE_PATH(dot_dot_entry_path);
1323 	struct item_head new_entry_ih, old_entry_ih, dot_dot_ih;
1324 	struct reiserfs_dir_entry old_de, new_de, dot_dot_de;
1325 	struct inode *old_inode, *new_dentry_inode;
1326 	struct reiserfs_transaction_handle th;
1327 	int jbegin_count;
1328 	umode_t old_inode_mode;
1329 	unsigned long savelink = 1;
1330 	struct timespec64 ctime;
1331 
1332 	if (flags & ~RENAME_NOREPLACE)
1333 		return -EINVAL;
1334 
1335 	/*
1336 	 * three balancings: (1) old name removal, (2) new name insertion
1337 	 * and (3) maybe "save" link insertion
1338 	 * stat data updates: (1) old directory,
1339 	 * (2) new directory and (3) maybe old object stat data (when it is
1340 	 * directory) and (4) maybe stat data of object to which new entry
1341 	 * pointed initially and (5) maybe block containing ".." of
1342 	 * renamed directory
1343 	 * quota updates: two parent directories
1344 	 */
1345 	jbegin_count =
1346 	    JOURNAL_PER_BALANCE_CNT * 3 + 5 +
1347 	    4 * REISERFS_QUOTA_TRANS_BLOCKS(old_dir->i_sb);
1348 
1349 	retval = dquot_initialize(old_dir);
1350 	if (retval)
1351 		return retval;
1352 	retval = dquot_initialize(new_dir);
1353 	if (retval)
1354 		return retval;
1355 
1356 	old_inode = d_inode(old_dentry);
1357 	new_dentry_inode = d_inode(new_dentry);
1358 
1359 	/*
1360 	 * make sure that oldname still exists and points to an object we
1361 	 * are going to rename
1362 	 */
1363 	old_de.de_gen_number_bit_string = NULL;
1364 	reiserfs_write_lock(old_dir->i_sb);
1365 	retval =
1366 	    reiserfs_find_entry(old_dir, old_dentry->d_name.name,
1367 				old_dentry->d_name.len, &old_entry_path,
1368 				&old_de);
1369 	pathrelse(&old_entry_path);
1370 	if (retval == IO_ERROR) {
1371 		reiserfs_write_unlock(old_dir->i_sb);
1372 		return -EIO;
1373 	}
1374 
1375 	if (retval != NAME_FOUND || old_de.de_objectid != old_inode->i_ino) {
1376 		reiserfs_write_unlock(old_dir->i_sb);
1377 		return -ENOENT;
1378 	}
1379 
1380 	old_inode_mode = old_inode->i_mode;
1381 	if (S_ISDIR(old_inode_mode)) {
1382 		/*
1383 		 * make sure that directory being renamed has correct ".."
1384 		 * and that its new parent directory has not too many links
1385 		 * already
1386 		 */
1387 		if (new_dentry_inode) {
1388 			if (!reiserfs_empty_dir(new_dentry_inode)) {
1389 				reiserfs_write_unlock(old_dir->i_sb);
1390 				return -ENOTEMPTY;
1391 			}
1392 		}
1393 
1394 		/*
1395 		 * directory is renamed, its parent directory will be changed,
1396 		 * so find ".." entry
1397 		 */
1398 		dot_dot_de.de_gen_number_bit_string = NULL;
1399 		retval =
1400 		    reiserfs_find_entry(old_inode, "..", 2, &dot_dot_entry_path,
1401 					&dot_dot_de);
1402 		pathrelse(&dot_dot_entry_path);
1403 		if (retval != NAME_FOUND) {
1404 			reiserfs_write_unlock(old_dir->i_sb);
1405 			return -EIO;
1406 		}
1407 
1408 		/* inode number of .. must equal old_dir->i_ino */
1409 		if (dot_dot_de.de_objectid != old_dir->i_ino) {
1410 			reiserfs_write_unlock(old_dir->i_sb);
1411 			return -EIO;
1412 		}
1413 	}
1414 
1415 	retval = journal_begin(&th, old_dir->i_sb, jbegin_count);
1416 	if (retval) {
1417 		reiserfs_write_unlock(old_dir->i_sb);
1418 		return retval;
1419 	}
1420 
1421 	/* add new entry (or find the existing one) */
1422 	retval =
1423 	    reiserfs_add_entry(&th, new_dir, new_dentry->d_name.name,
1424 			       new_dentry->d_name.len, old_inode, 0);
1425 	if (retval == -EEXIST) {
1426 		if (!new_dentry_inode) {
1427 			reiserfs_panic(old_dir->i_sb, "vs-7050",
1428 				       "new entry is found, new inode == 0");
1429 		}
1430 	} else if (retval) {
1431 		int err = journal_end(&th);
1432 		reiserfs_write_unlock(old_dir->i_sb);
1433 		return err ? err : retval;
1434 	}
1435 
1436 	reiserfs_update_inode_transaction(old_dir);
1437 	reiserfs_update_inode_transaction(new_dir);
1438 
1439 	/*
1440 	 * this makes it so an fsync on an open fd for the old name will
1441 	 * commit the rename operation
1442 	 */
1443 	reiserfs_update_inode_transaction(old_inode);
1444 
1445 	if (new_dentry_inode)
1446 		reiserfs_update_inode_transaction(new_dentry_inode);
1447 
1448 	while (1) {
1449 		/*
1450 		 * look for old name using corresponding entry key
1451 		 * (found by reiserfs_find_entry)
1452 		 */
1453 		if ((retval =
1454 		     search_by_entry_key(new_dir->i_sb, &old_de.de_entry_key,
1455 					 &old_entry_path,
1456 					 &old_de)) != NAME_FOUND) {
1457 			pathrelse(&old_entry_path);
1458 			journal_end(&th);
1459 			reiserfs_write_unlock(old_dir->i_sb);
1460 			return -EIO;
1461 		}
1462 
1463 		copy_item_head(&old_entry_ih, tp_item_head(&old_entry_path));
1464 
1465 		reiserfs_prepare_for_journal(old_inode->i_sb, old_de.de_bh, 1);
1466 
1467 		/* look for new name by reiserfs_find_entry */
1468 		new_de.de_gen_number_bit_string = NULL;
1469 		retval =
1470 		    reiserfs_find_entry(new_dir, new_dentry->d_name.name,
1471 					new_dentry->d_name.len, &new_entry_path,
1472 					&new_de);
1473 		/*
1474 		 * reiserfs_add_entry should not return IO_ERROR,
1475 		 * because it is called with essentially same parameters from
1476 		 * reiserfs_add_entry above, and we'll catch any i/o errors
1477 		 * before we get here.
1478 		 */
1479 		if (retval != NAME_FOUND_INVISIBLE && retval != NAME_FOUND) {
1480 			pathrelse(&new_entry_path);
1481 			pathrelse(&old_entry_path);
1482 			journal_end(&th);
1483 			reiserfs_write_unlock(old_dir->i_sb);
1484 			return -EIO;
1485 		}
1486 
1487 		copy_item_head(&new_entry_ih, tp_item_head(&new_entry_path));
1488 
1489 		reiserfs_prepare_for_journal(old_inode->i_sb, new_de.de_bh, 1);
1490 
1491 		if (S_ISDIR(old_inode->i_mode)) {
1492 			if ((retval =
1493 			     search_by_entry_key(new_dir->i_sb,
1494 						 &dot_dot_de.de_entry_key,
1495 						 &dot_dot_entry_path,
1496 						 &dot_dot_de)) != NAME_FOUND) {
1497 				pathrelse(&dot_dot_entry_path);
1498 				pathrelse(&new_entry_path);
1499 				pathrelse(&old_entry_path);
1500 				journal_end(&th);
1501 				reiserfs_write_unlock(old_dir->i_sb);
1502 				return -EIO;
1503 			}
1504 			copy_item_head(&dot_dot_ih,
1505 				       tp_item_head(&dot_dot_entry_path));
1506 			/* node containing ".." gets into transaction */
1507 			reiserfs_prepare_for_journal(old_inode->i_sb,
1508 						     dot_dot_de.de_bh, 1);
1509 		}
1510 		/*
1511 		 * we should check seals here, not do
1512 		 * this stuff, yes? Then, having
1513 		 * gathered everything into RAM we
1514 		 * should lock the buffers, yes?  -Hans
1515 		 */
1516 		/*
1517 		 * probably.  our rename needs to hold more
1518 		 * than one path at once.  The seals would
1519 		 * have to be written to deal with multi-path
1520 		 * issues -chris
1521 		 */
1522 		/*
1523 		 * sanity checking before doing the rename - avoid races many
1524 		 * of the above checks could have scheduled.  We have to be
1525 		 * sure our items haven't been shifted by another process.
1526 		 */
1527 		if (item_moved(&new_entry_ih, &new_entry_path) ||
1528 		    !entry_points_to_object(new_dentry->d_name.name,
1529 					    new_dentry->d_name.len,
1530 					    &new_de, new_dentry_inode) ||
1531 		    item_moved(&old_entry_ih, &old_entry_path) ||
1532 		    !entry_points_to_object(old_dentry->d_name.name,
1533 					    old_dentry->d_name.len,
1534 					    &old_de, old_inode)) {
1535 			reiserfs_restore_prepared_buffer(old_inode->i_sb,
1536 							 new_de.de_bh);
1537 			reiserfs_restore_prepared_buffer(old_inode->i_sb,
1538 							 old_de.de_bh);
1539 			if (S_ISDIR(old_inode_mode))
1540 				reiserfs_restore_prepared_buffer(old_inode->
1541 								 i_sb,
1542 								 dot_dot_de.
1543 								 de_bh);
1544 			continue;
1545 		}
1546 		if (S_ISDIR(old_inode_mode)) {
1547 			if (item_moved(&dot_dot_ih, &dot_dot_entry_path) ||
1548 			    !entry_points_to_object("..", 2, &dot_dot_de,
1549 						    old_dir)) {
1550 				reiserfs_restore_prepared_buffer(old_inode->
1551 								 i_sb,
1552 								 old_de.de_bh);
1553 				reiserfs_restore_prepared_buffer(old_inode->
1554 								 i_sb,
1555 								 new_de.de_bh);
1556 				reiserfs_restore_prepared_buffer(old_inode->
1557 								 i_sb,
1558 								 dot_dot_de.
1559 								 de_bh);
1560 				continue;
1561 			}
1562 		}
1563 
1564 		RFALSE(S_ISDIR(old_inode_mode) &&
1565 		       !buffer_journal_prepared(dot_dot_de.de_bh), "");
1566 
1567 		break;
1568 	}
1569 
1570 	/*
1571 	 * ok, all the changes can be done in one fell swoop when we
1572 	 * have claimed all the buffers needed.
1573 	 */
1574 
1575 	mark_de_visible(new_de.de_deh + new_de.de_entry_num);
1576 	set_ino_in_dir_entry(&new_de, INODE_PKEY(old_inode));
1577 	journal_mark_dirty(&th, new_de.de_bh);
1578 
1579 	mark_de_hidden(old_de.de_deh + old_de.de_entry_num);
1580 	journal_mark_dirty(&th, old_de.de_bh);
1581 	ctime = current_time(old_dir);
1582 	old_dir->i_ctime = old_dir->i_mtime = ctime;
1583 	new_dir->i_ctime = new_dir->i_mtime = ctime;
1584 	/*
1585 	 * thanks to Alex Adriaanse <alex_a@caltech.edu> for patch
1586 	 * which adds ctime update of renamed object
1587 	 */
1588 	old_inode->i_ctime = ctime;
1589 
1590 	if (new_dentry_inode) {
1591 		/* adjust link number of the victim */
1592 		if (S_ISDIR(new_dentry_inode->i_mode)) {
1593 			clear_nlink(new_dentry_inode);
1594 		} else {
1595 			drop_nlink(new_dentry_inode);
1596 		}
1597 		new_dentry_inode->i_ctime = ctime;
1598 		savelink = new_dentry_inode->i_nlink;
1599 	}
1600 
1601 	if (S_ISDIR(old_inode_mode)) {
1602 		/* adjust ".." of renamed directory */
1603 		set_ino_in_dir_entry(&dot_dot_de, INODE_PKEY(new_dir));
1604 		journal_mark_dirty(&th, dot_dot_de.de_bh);
1605 
1606 		/*
1607 		 * there (in new_dir) was no directory, so it got new link
1608 		 * (".."  of renamed directory)
1609 		 */
1610 		if (!new_dentry_inode)
1611 			INC_DIR_INODE_NLINK(new_dir);
1612 
1613 		/* old directory lost one link - ".. " of renamed directory */
1614 		DEC_DIR_INODE_NLINK(old_dir);
1615 	}
1616 	/*
1617 	 * looks like in 2.3.99pre3 brelse is atomic.
1618 	 * so we can use pathrelse
1619 	 */
1620 	pathrelse(&new_entry_path);
1621 	pathrelse(&dot_dot_entry_path);
1622 
1623 	/*
1624 	 * FIXME: this reiserfs_cut_from_item's return value may screw up
1625 	 * anybody, but it will panic if will not be able to find the
1626 	 * entry. This needs one more clean up
1627 	 */
1628 	if (reiserfs_cut_from_item
1629 	    (&th, &old_entry_path, &old_de.de_entry_key, old_dir, NULL,
1630 	     0) < 0)
1631 		reiserfs_error(old_dir->i_sb, "vs-7060",
1632 			       "couldn't not cut old name. Fsck later?");
1633 
1634 	old_dir->i_size -= DEH_SIZE + old_de.de_entrylen;
1635 
1636 	reiserfs_update_sd(&th, old_dir);
1637 	reiserfs_update_sd(&th, new_dir);
1638 	reiserfs_update_sd(&th, old_inode);
1639 
1640 	if (new_dentry_inode) {
1641 		if (savelink == 0)
1642 			add_save_link(&th, new_dentry_inode,
1643 				      0 /* not truncate */ );
1644 		reiserfs_update_sd(&th, new_dentry_inode);
1645 	}
1646 
1647 	retval = journal_end(&th);
1648 	reiserfs_write_unlock(old_dir->i_sb);
1649 	return retval;
1650 }
1651 
1652 /* directories can handle most operations...  */
1653 const struct inode_operations reiserfs_dir_inode_operations = {
1654 	.create = reiserfs_create,
1655 	.lookup = reiserfs_lookup,
1656 	.link = reiserfs_link,
1657 	.unlink = reiserfs_unlink,
1658 	.symlink = reiserfs_symlink,
1659 	.mkdir = reiserfs_mkdir,
1660 	.rmdir = reiserfs_rmdir,
1661 	.mknod = reiserfs_mknod,
1662 	.rename = reiserfs_rename,
1663 	.setattr = reiserfs_setattr,
1664 	.listxattr = reiserfs_listxattr,
1665 	.permission = reiserfs_permission,
1666 	.get_inode_acl = reiserfs_get_acl,
1667 	.set_acl = reiserfs_set_acl,
1668 	.fileattr_get = reiserfs_fileattr_get,
1669 	.fileattr_set = reiserfs_fileattr_set,
1670 };
1671 
1672 /*
1673  * symlink operations.. same as page_symlink_inode_operations, with xattr
1674  * stuff added
1675  */
1676 const struct inode_operations reiserfs_symlink_inode_operations = {
1677 	.get_link	= page_get_link,
1678 	.setattr = reiserfs_setattr,
1679 	.listxattr = reiserfs_listxattr,
1680 	.permission = reiserfs_permission,
1681 };
1682 
1683 /*
1684  * special file operations.. just xattr/acl stuff
1685  */
1686 const struct inode_operations reiserfs_special_inode_operations = {
1687 	.setattr = reiserfs_setattr,
1688 	.listxattr = reiserfs_listxattr,
1689 	.permission = reiserfs_permission,
1690 	.get_inode_acl = reiserfs_get_acl,
1691 	.set_acl = reiserfs_set_acl,
1692 };
1693