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