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