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