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