namei.c revision d984561b326cd0fe0d1183d11b9b4fa1d011d21d
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	retval = journal_begin(&th, dir->i_sb, jbegin_count);
613	if (retval) {
614		drop_new_inode(inode);
615		goto out_failed;
616	}
617
618	retval =
619	    reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
620			       inode);
621	if (retval)
622		goto out_failed;
623
624	inode->i_op = &reiserfs_file_inode_operations;
625	inode->i_fop = &reiserfs_file_operations;
626	inode->i_mapping->a_ops = &reiserfs_address_space_operations;
627
628	retval =
629	    reiserfs_add_entry(&th, dir, dentry->d_name.name,
630			       dentry->d_name.len, inode, 1 /*visible */ );
631	if (retval) {
632		int err;
633		inode->i_nlink--;
634		reiserfs_update_sd(&th, inode);
635		err = journal_end(&th, dir->i_sb, jbegin_count);
636		if (err)
637			retval = err;
638		unlock_new_inode(inode);
639		iput(inode);
640		goto out_failed;
641	}
642	reiserfs_update_inode_transaction(inode);
643	reiserfs_update_inode_transaction(dir);
644
645	d_instantiate(dentry, inode);
646	unlock_new_inode(inode);
647	retval = journal_end(&th, dir->i_sb, jbegin_count);
648
649      out_failed:
650	reiserfs_write_unlock(dir->i_sb);
651	return retval;
652}
653
654static int reiserfs_mknod(struct inode *dir, struct dentry *dentry, int mode,
655			  dev_t rdev)
656{
657	int retval;
658	struct inode *inode;
659	struct reiserfs_transaction_handle th;
660	/* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
661	int jbegin_count =
662	    JOURNAL_PER_BALANCE_CNT * 3 +
663	    2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
664		 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
665	int locked;
666
667	if (!new_valid_dev(rdev))
668		return -EINVAL;
669
670	if (!(inode = new_inode(dir->i_sb))) {
671		return -ENOMEM;
672	}
673	new_inode_init(inode, dir, mode);
674
675	locked = reiserfs_cache_default_acl(dir);
676
677	reiserfs_write_lock(dir->i_sb);
678
679	retval = journal_begin(&th, dir->i_sb, jbegin_count);
680	if (retval) {
681		drop_new_inode(inode);
682		goto out_failed;
683	}
684
685	retval =
686	    reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
687			       inode);
688	if (retval) {
689		goto out_failed;
690	}
691
692	inode->i_op = &reiserfs_special_inode_operations;
693	init_special_inode(inode, inode->i_mode, rdev);
694
695	//FIXME: needed for block and char devices only
696	reiserfs_update_sd(&th, inode);
697
698	reiserfs_update_inode_transaction(inode);
699	reiserfs_update_inode_transaction(dir);
700
701	retval =
702	    reiserfs_add_entry(&th, dir, dentry->d_name.name,
703			       dentry->d_name.len, inode, 1 /*visible */ );
704	if (retval) {
705		int err;
706		inode->i_nlink--;
707		reiserfs_update_sd(&th, inode);
708		err = journal_end(&th, dir->i_sb, jbegin_count);
709		if (err)
710			retval = err;
711		unlock_new_inode(inode);
712		iput(inode);
713		goto out_failed;
714	}
715
716	d_instantiate(dentry, inode);
717	unlock_new_inode(inode);
718	retval = journal_end(&th, dir->i_sb, jbegin_count);
719
720      out_failed:
721	reiserfs_write_unlock(dir->i_sb);
722	return retval;
723}
724
725static int reiserfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
726{
727	int retval;
728	struct inode *inode;
729	struct reiserfs_transaction_handle th;
730	/* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
731	int jbegin_count =
732	    JOURNAL_PER_BALANCE_CNT * 3 +
733	    2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
734		 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
735	int locked;
736
737#ifdef DISPLACE_NEW_PACKING_LOCALITIES
738	/* set flag that new packing locality created and new blocks for the content     * of that directory are not displaced yet */
739	REISERFS_I(dir)->new_packing_locality = 1;
740#endif
741	mode = S_IFDIR | mode;
742	if (!(inode = new_inode(dir->i_sb))) {
743		return -ENOMEM;
744	}
745	new_inode_init(inode, dir, mode);
746
747	locked = reiserfs_cache_default_acl(dir);
748
749	reiserfs_write_lock(dir->i_sb);
750
751	retval = journal_begin(&th, dir->i_sb, jbegin_count);
752	if (retval) {
753		drop_new_inode(inode);
754		goto out_failed;
755	}
756
757	/* inc the link count now, so another writer doesn't overflow it while
758	 ** we sleep later on.
759	 */
760	INC_DIR_INODE_NLINK(dir)
761
762	    retval = reiserfs_new_inode(&th, dir, mode, NULL /*symlink */ ,
763					old_format_only(dir->i_sb) ?
764					EMPTY_DIR_SIZE_V1 : EMPTY_DIR_SIZE,
765					dentry, inode);
766	if (retval) {
767		dir->i_nlink--;
768		goto out_failed;
769	}
770
771	reiserfs_update_inode_transaction(inode);
772	reiserfs_update_inode_transaction(dir);
773
774	inode->i_op = &reiserfs_dir_inode_operations;
775	inode->i_fop = &reiserfs_dir_operations;
776
777	// note, _this_ add_entry will not update dir's stat data
778	retval =
779	    reiserfs_add_entry(&th, dir, dentry->d_name.name,
780			       dentry->d_name.len, inode, 1 /*visible */ );
781	if (retval) {
782		int err;
783		inode->i_nlink = 0;
784		DEC_DIR_INODE_NLINK(dir);
785		reiserfs_update_sd(&th, inode);
786		err = journal_end(&th, dir->i_sb, jbegin_count);
787		if (err)
788			retval = err;
789		unlock_new_inode(inode);
790		iput(inode);
791		goto out_failed;
792	}
793	// the above add_entry did not update dir's stat data
794	reiserfs_update_sd(&th, dir);
795
796	d_instantiate(dentry, inode);
797	unlock_new_inode(inode);
798	retval = journal_end(&th, dir->i_sb, jbegin_count);
799      out_failed:
800	reiserfs_write_unlock(dir->i_sb);
801	return retval;
802}
803
804static inline int reiserfs_empty_dir(struct inode *inode)
805{
806	/* we can cheat because an old format dir cannot have
807	 ** EMPTY_DIR_SIZE, and a new format dir cannot have
808	 ** EMPTY_DIR_SIZE_V1.  So, if the inode is either size,
809	 ** regardless of disk format version, the directory is empty.
810	 */
811	if (inode->i_size != EMPTY_DIR_SIZE &&
812	    inode->i_size != EMPTY_DIR_SIZE_V1) {
813		return 0;
814	}
815	return 1;
816}
817
818static int reiserfs_rmdir(struct inode *dir, struct dentry *dentry)
819{
820	int retval, err;
821	struct inode *inode;
822	struct reiserfs_transaction_handle th;
823	int jbegin_count;
824	INITIALIZE_PATH(path);
825	struct reiserfs_dir_entry de;
826
827	/* we will be doing 2 balancings and update 2 stat data, we change quotas
828	 * of the owner of the directory and of the owner of the parent directory.
829	 * The quota structure is possibly deleted only on last iput => outside
830	 * of this transaction */
831	jbegin_count =
832	    JOURNAL_PER_BALANCE_CNT * 2 + 2 +
833	    4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
834
835	reiserfs_write_lock(dir->i_sb);
836	retval = journal_begin(&th, dir->i_sb, jbegin_count);
837	if (retval)
838		goto out_rmdir;
839
840	de.de_gen_number_bit_string = NULL;
841	if ((retval =
842	     reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
843				 &path, &de)) == NAME_NOT_FOUND) {
844		retval = -ENOENT;
845		goto end_rmdir;
846	} else if (retval == IO_ERROR) {
847		retval = -EIO;
848		goto end_rmdir;
849	}
850
851	inode = dentry->d_inode;
852
853	reiserfs_update_inode_transaction(inode);
854	reiserfs_update_inode_transaction(dir);
855
856	if (de.de_objectid != inode->i_ino) {
857		// FIXME: compare key of an object and a key found in the
858		// entry
859		retval = -EIO;
860		goto end_rmdir;
861	}
862	if (!reiserfs_empty_dir(inode)) {
863		retval = -ENOTEMPTY;
864		goto end_rmdir;
865	}
866
867	/* cut entry from dir directory */
868	retval = reiserfs_cut_from_item(&th, &path, &(de.de_entry_key), dir, NULL,	/* page */
869					0 /*new file size - not used here */ );
870	if (retval < 0)
871		goto end_rmdir;
872
873	if (inode->i_nlink != 2 && inode->i_nlink != 1)
874		reiserfs_error(inode->i_sb, "reiserfs-7040",
875			       "empty directory has nlink != 2 (%d)",
876			       inode->i_nlink);
877
878	clear_nlink(inode);
879	inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
880	reiserfs_update_sd(&th, inode);
881
882	DEC_DIR_INODE_NLINK(dir)
883	    dir->i_size -= (DEH_SIZE + de.de_entrylen);
884	reiserfs_update_sd(&th, dir);
885
886	/* prevent empty directory from getting lost */
887	add_save_link(&th, inode, 0 /* not truncate */ );
888
889	retval = journal_end(&th, dir->i_sb, jbegin_count);
890	reiserfs_check_path(&path);
891      out_rmdir:
892	reiserfs_write_unlock(dir->i_sb);
893	return retval;
894
895      end_rmdir:
896	/* we must release path, because we did not call
897	   reiserfs_cut_from_item, or reiserfs_cut_from_item does not
898	   release path if operation was not complete */
899	pathrelse(&path);
900	err = journal_end(&th, dir->i_sb, jbegin_count);
901	reiserfs_write_unlock(dir->i_sb);
902	return err ? err : retval;
903}
904
905static int reiserfs_unlink(struct inode *dir, struct dentry *dentry)
906{
907	int retval, err;
908	struct inode *inode;
909	struct reiserfs_dir_entry de;
910	INITIALIZE_PATH(path);
911	struct reiserfs_transaction_handle th;
912	int jbegin_count;
913	unsigned long savelink;
914
915	inode = dentry->d_inode;
916
917	/* in this transaction we can be doing at max two balancings and update
918	 * two stat datas, we change quotas of the owner of the directory and of
919	 * the owner of the parent directory. The quota structure is possibly
920	 * deleted only on iput => outside of this transaction */
921	jbegin_count =
922	    JOURNAL_PER_BALANCE_CNT * 2 + 2 +
923	    4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
924
925	reiserfs_write_lock(dir->i_sb);
926	retval = journal_begin(&th, dir->i_sb, jbegin_count);
927	if (retval)
928		goto out_unlink;
929
930	de.de_gen_number_bit_string = NULL;
931	if ((retval =
932	     reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
933				 &path, &de)) == NAME_NOT_FOUND) {
934		retval = -ENOENT;
935		goto end_unlink;
936	} else if (retval == IO_ERROR) {
937		retval = -EIO;
938		goto end_unlink;
939	}
940
941	reiserfs_update_inode_transaction(inode);
942	reiserfs_update_inode_transaction(dir);
943
944	if (de.de_objectid != inode->i_ino) {
945		// FIXME: compare key of an object and a key found in the
946		// entry
947		retval = -EIO;
948		goto end_unlink;
949	}
950
951	if (!inode->i_nlink) {
952		reiserfs_warning(inode->i_sb, "reiserfs-7042",
953				 "deleting nonexistent file (%lu), %d",
954				 inode->i_ino, inode->i_nlink);
955		inode->i_nlink = 1;
956	}
957
958	drop_nlink(inode);
959
960	/*
961	 * we schedule before doing the add_save_link call, save the link
962	 * count so we don't race
963	 */
964	savelink = inode->i_nlink;
965
966	retval =
967	    reiserfs_cut_from_item(&th, &path, &(de.de_entry_key), dir, NULL,
968				   0);
969	if (retval < 0) {
970		inc_nlink(inode);
971		goto end_unlink;
972	}
973	inode->i_ctime = CURRENT_TIME_SEC;
974	reiserfs_update_sd(&th, inode);
975
976	dir->i_size -= (de.de_entrylen + DEH_SIZE);
977	dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
978	reiserfs_update_sd(&th, dir);
979
980	if (!savelink)
981		/* prevent file from getting lost */
982		add_save_link(&th, inode, 0 /* not truncate */ );
983
984	retval = journal_end(&th, dir->i_sb, jbegin_count);
985	reiserfs_check_path(&path);
986	reiserfs_write_unlock(dir->i_sb);
987	return retval;
988
989      end_unlink:
990	pathrelse(&path);
991	err = journal_end(&th, dir->i_sb, jbegin_count);
992	reiserfs_check_path(&path);
993	if (err)
994		retval = err;
995      out_unlink:
996	reiserfs_write_unlock(dir->i_sb);
997	return retval;
998}
999
1000static int reiserfs_symlink(struct inode *parent_dir,
1001			    struct dentry *dentry, const char *symname)
1002{
1003	int retval;
1004	struct inode *inode;
1005	char *name;
1006	int item_len;
1007	struct reiserfs_transaction_handle th;
1008	int mode = S_IFLNK | S_IRWXUGO;
1009	/* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
1010	int jbegin_count =
1011	    JOURNAL_PER_BALANCE_CNT * 3 +
1012	    2 * (REISERFS_QUOTA_INIT_BLOCKS(parent_dir->i_sb) +
1013		 REISERFS_QUOTA_TRANS_BLOCKS(parent_dir->i_sb));
1014
1015	if (!(inode = new_inode(parent_dir->i_sb))) {
1016		return -ENOMEM;
1017	}
1018	new_inode_init(inode, parent_dir, mode);
1019
1020	reiserfs_write_lock(parent_dir->i_sb);
1021	item_len = ROUND_UP(strlen(symname));
1022	if (item_len > MAX_DIRECT_ITEM_LEN(parent_dir->i_sb->s_blocksize)) {
1023		retval = -ENAMETOOLONG;
1024		drop_new_inode(inode);
1025		goto out_failed;
1026	}
1027
1028	name = kmalloc(item_len, GFP_NOFS);
1029	if (!name) {
1030		drop_new_inode(inode);
1031		retval = -ENOMEM;
1032		goto out_failed;
1033	}
1034	memcpy(name, symname, strlen(symname));
1035	padd_item(name, item_len, strlen(symname));
1036
1037	/* We would inherit the default ACL here, but symlinks don't get ACLs */
1038
1039	retval = journal_begin(&th, parent_dir->i_sb, jbegin_count);
1040	if (retval) {
1041		drop_new_inode(inode);
1042		kfree(name);
1043		goto out_failed;
1044	}
1045
1046	retval =
1047	    reiserfs_new_inode(&th, parent_dir, mode, name, strlen(symname),
1048			       dentry, inode);
1049	kfree(name);
1050	if (retval) {		/* reiserfs_new_inode iputs for us */
1051		goto out_failed;
1052	}
1053
1054	reiserfs_update_inode_transaction(inode);
1055	reiserfs_update_inode_transaction(parent_dir);
1056
1057	inode->i_op = &reiserfs_symlink_inode_operations;
1058	inode->i_mapping->a_ops = &reiserfs_address_space_operations;
1059
1060	// must be sure this inode is written with this transaction
1061	//
1062	//reiserfs_update_sd (&th, inode, READ_BLOCKS);
1063
1064	retval = reiserfs_add_entry(&th, parent_dir, dentry->d_name.name,
1065				    dentry->d_name.len, inode, 1 /*visible */ );
1066	if (retval) {
1067		int err;
1068		inode->i_nlink--;
1069		reiserfs_update_sd(&th, inode);
1070		err = journal_end(&th, parent_dir->i_sb, jbegin_count);
1071		if (err)
1072			retval = err;
1073		unlock_new_inode(inode);
1074		iput(inode);
1075		goto out_failed;
1076	}
1077
1078	d_instantiate(dentry, inode);
1079	unlock_new_inode(inode);
1080	retval = journal_end(&th, parent_dir->i_sb, jbegin_count);
1081      out_failed:
1082	reiserfs_write_unlock(parent_dir->i_sb);
1083	return retval;
1084}
1085
1086static int reiserfs_link(struct dentry *old_dentry, struct inode *dir,
1087			 struct dentry *dentry)
1088{
1089	int retval;
1090	struct inode *inode = old_dentry->d_inode;
1091	struct reiserfs_transaction_handle th;
1092	/* We need blocks for transaction + update of quotas for the owners of the directory */
1093	int jbegin_count =
1094	    JOURNAL_PER_BALANCE_CNT * 3 +
1095	    2 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
1096
1097	reiserfs_write_lock(dir->i_sb);
1098	if (inode->i_nlink >= REISERFS_LINK_MAX) {
1099		//FIXME: sd_nlink is 32 bit for new files
1100		reiserfs_write_unlock(dir->i_sb);
1101		return -EMLINK;
1102	}
1103	if (inode->i_nlink == 0) {
1104		reiserfs_write_unlock(dir->i_sb);
1105		return -ENOENT;
1106	}
1107
1108	/* inc before scheduling so reiserfs_unlink knows we are here */
1109	inc_nlink(inode);
1110
1111	retval = journal_begin(&th, dir->i_sb, jbegin_count);
1112	if (retval) {
1113		inode->i_nlink--;
1114		reiserfs_write_unlock(dir->i_sb);
1115		return retval;
1116	}
1117
1118	/* create new entry */
1119	retval =
1120	    reiserfs_add_entry(&th, dir, dentry->d_name.name,
1121			       dentry->d_name.len, inode, 1 /*visible */ );
1122
1123	reiserfs_update_inode_transaction(inode);
1124	reiserfs_update_inode_transaction(dir);
1125
1126	if (retval) {
1127		int err;
1128		inode->i_nlink--;
1129		err = journal_end(&th, dir->i_sb, jbegin_count);
1130		reiserfs_write_unlock(dir->i_sb);
1131		return err ? err : retval;
1132	}
1133
1134	inode->i_ctime = CURRENT_TIME_SEC;
1135	reiserfs_update_sd(&th, inode);
1136
1137	atomic_inc(&inode->i_count);
1138	d_instantiate(dentry, inode);
1139	retval = journal_end(&th, dir->i_sb, jbegin_count);
1140	reiserfs_write_unlock(dir->i_sb);
1141	return retval;
1142}
1143
1144// de contains information pointing to an entry which
1145static int de_still_valid(const char *name, int len,
1146			  struct reiserfs_dir_entry *de)
1147{
1148	struct reiserfs_dir_entry tmp = *de;
1149
1150	// recalculate pointer to name and name length
1151	set_de_name_and_namelen(&tmp);
1152	// FIXME: could check more
1153	if (tmp.de_namelen != len || memcmp(name, de->de_name, len))
1154		return 0;
1155	return 1;
1156}
1157
1158static int entry_points_to_object(const char *name, int len,
1159				  struct reiserfs_dir_entry *de,
1160				  struct inode *inode)
1161{
1162	if (!de_still_valid(name, len, de))
1163		return 0;
1164
1165	if (inode) {
1166		if (!de_visible(de->de_deh + de->de_entry_num))
1167			reiserfs_panic(inode->i_sb, "vs-7042",
1168				       "entry must be visible");
1169		return (de->de_objectid == inode->i_ino) ? 1 : 0;
1170	}
1171
1172	/* this must be added hidden entry */
1173	if (de_visible(de->de_deh + de->de_entry_num))
1174		reiserfs_panic(NULL, "vs-7043", "entry must be visible");
1175
1176	return 1;
1177}
1178
1179/* sets key of objectid the entry has to point to */
1180static void set_ino_in_dir_entry(struct reiserfs_dir_entry *de,
1181				 struct reiserfs_key *key)
1182{
1183	/* JDM These operations are endian safe - both are le */
1184	de->de_deh[de->de_entry_num].deh_dir_id = key->k_dir_id;
1185	de->de_deh[de->de_entry_num].deh_objectid = key->k_objectid;
1186}
1187
1188/*
1189 * process, that is going to call fix_nodes/do_balance must hold only
1190 * one path. If it holds 2 or more, it can get into endless waiting in
1191 * get_empty_nodes or its clones
1192 */
1193static int reiserfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1194			   struct inode *new_dir, struct dentry *new_dentry)
1195{
1196	int retval;
1197	INITIALIZE_PATH(old_entry_path);
1198	INITIALIZE_PATH(new_entry_path);
1199	INITIALIZE_PATH(dot_dot_entry_path);
1200	struct item_head new_entry_ih, old_entry_ih, dot_dot_ih;
1201	struct reiserfs_dir_entry old_de, new_de, dot_dot_de;
1202	struct inode *old_inode, *new_dentry_inode;
1203	struct reiserfs_transaction_handle th;
1204	int jbegin_count;
1205	umode_t old_inode_mode;
1206	unsigned long savelink = 1;
1207	struct timespec ctime;
1208
1209	/* three balancings: (1) old name removal, (2) new name insertion
1210	   and (3) maybe "save" link insertion
1211	   stat data updates: (1) old directory,
1212	   (2) new directory and (3) maybe old object stat data (when it is
1213	   directory) and (4) maybe stat data of object to which new entry
1214	   pointed initially and (5) maybe block containing ".." of
1215	   renamed directory
1216	   quota updates: two parent directories */
1217	jbegin_count =
1218	    JOURNAL_PER_BALANCE_CNT * 3 + 5 +
1219	    4 * REISERFS_QUOTA_TRANS_BLOCKS(old_dir->i_sb);
1220
1221	old_inode = old_dentry->d_inode;
1222	new_dentry_inode = new_dentry->d_inode;
1223
1224	// make sure, that oldname still exists and points to an object we
1225	// are going to rename
1226	old_de.de_gen_number_bit_string = NULL;
1227	reiserfs_write_lock(old_dir->i_sb);
1228	retval =
1229	    reiserfs_find_entry(old_dir, old_dentry->d_name.name,
1230				old_dentry->d_name.len, &old_entry_path,
1231				&old_de);
1232	pathrelse(&old_entry_path);
1233	if (retval == IO_ERROR) {
1234		reiserfs_write_unlock(old_dir->i_sb);
1235		return -EIO;
1236	}
1237
1238	if (retval != NAME_FOUND || old_de.de_objectid != old_inode->i_ino) {
1239		reiserfs_write_unlock(old_dir->i_sb);
1240		return -ENOENT;
1241	}
1242
1243	old_inode_mode = old_inode->i_mode;
1244	if (S_ISDIR(old_inode_mode)) {
1245		// make sure, that directory being renamed has correct ".."
1246		// and that its new parent directory has not too many links
1247		// already
1248
1249		if (new_dentry_inode) {
1250			if (!reiserfs_empty_dir(new_dentry_inode)) {
1251				reiserfs_write_unlock(old_dir->i_sb);
1252				return -ENOTEMPTY;
1253			}
1254		}
1255
1256		/* directory is renamed, its parent directory will be changed,
1257		 ** so find ".." entry
1258		 */
1259		dot_dot_de.de_gen_number_bit_string = NULL;
1260		retval =
1261		    reiserfs_find_entry(old_inode, "..", 2, &dot_dot_entry_path,
1262					&dot_dot_de);
1263		pathrelse(&dot_dot_entry_path);
1264		if (retval != NAME_FOUND) {
1265			reiserfs_write_unlock(old_dir->i_sb);
1266			return -EIO;
1267		}
1268
1269		/* inode number of .. must equal old_dir->i_ino */
1270		if (dot_dot_de.de_objectid != old_dir->i_ino) {
1271			reiserfs_write_unlock(old_dir->i_sb);
1272			return -EIO;
1273		}
1274	}
1275
1276	retval = journal_begin(&th, old_dir->i_sb, jbegin_count);
1277	if (retval) {
1278		reiserfs_write_unlock(old_dir->i_sb);
1279		return retval;
1280	}
1281
1282	/* add new entry (or find the existing one) */
1283	retval =
1284	    reiserfs_add_entry(&th, new_dir, new_dentry->d_name.name,
1285			       new_dentry->d_name.len, old_inode, 0);
1286	if (retval == -EEXIST) {
1287		if (!new_dentry_inode) {
1288			reiserfs_panic(old_dir->i_sb, "vs-7050",
1289				       "new entry is found, new inode == 0");
1290		}
1291	} else if (retval) {
1292		int err = journal_end(&th, old_dir->i_sb, jbegin_count);
1293		reiserfs_write_unlock(old_dir->i_sb);
1294		return err ? err : retval;
1295	}
1296
1297	reiserfs_update_inode_transaction(old_dir);
1298	reiserfs_update_inode_transaction(new_dir);
1299
1300	/* this makes it so an fsync on an open fd for the old name will
1301	 ** commit the rename operation
1302	 */
1303	reiserfs_update_inode_transaction(old_inode);
1304
1305	if (new_dentry_inode)
1306		reiserfs_update_inode_transaction(new_dentry_inode);
1307
1308	while (1) {
1309		// look for old name using corresponding entry key (found by reiserfs_find_entry)
1310		if ((retval =
1311		     search_by_entry_key(new_dir->i_sb, &old_de.de_entry_key,
1312					 &old_entry_path,
1313					 &old_de)) != NAME_FOUND) {
1314			pathrelse(&old_entry_path);
1315			journal_end(&th, old_dir->i_sb, jbegin_count);
1316			reiserfs_write_unlock(old_dir->i_sb);
1317			return -EIO;
1318		}
1319
1320		copy_item_head(&old_entry_ih, get_ih(&old_entry_path));
1321
1322		reiserfs_prepare_for_journal(old_inode->i_sb, old_de.de_bh, 1);
1323
1324		// look for new name by reiserfs_find_entry
1325		new_de.de_gen_number_bit_string = NULL;
1326		retval =
1327		    reiserfs_find_entry(new_dir, new_dentry->d_name.name,
1328					new_dentry->d_name.len, &new_entry_path,
1329					&new_de);
1330		// reiserfs_add_entry should not return IO_ERROR, because it is called with essentially same parameters from
1331		// reiserfs_add_entry above, and we'll catch any i/o errors before we get here.
1332		if (retval != NAME_FOUND_INVISIBLE && retval != NAME_FOUND) {
1333			pathrelse(&new_entry_path);
1334			pathrelse(&old_entry_path);
1335			journal_end(&th, old_dir->i_sb, jbegin_count);
1336			reiserfs_write_unlock(old_dir->i_sb);
1337			return -EIO;
1338		}
1339
1340		copy_item_head(&new_entry_ih, get_ih(&new_entry_path));
1341
1342		reiserfs_prepare_for_journal(old_inode->i_sb, new_de.de_bh, 1);
1343
1344		if (S_ISDIR(old_inode->i_mode)) {
1345			if ((retval =
1346			     search_by_entry_key(new_dir->i_sb,
1347						 &dot_dot_de.de_entry_key,
1348						 &dot_dot_entry_path,
1349						 &dot_dot_de)) != NAME_FOUND) {
1350				pathrelse(&dot_dot_entry_path);
1351				pathrelse(&new_entry_path);
1352				pathrelse(&old_entry_path);
1353				journal_end(&th, old_dir->i_sb, jbegin_count);
1354				reiserfs_write_unlock(old_dir->i_sb);
1355				return -EIO;
1356			}
1357			copy_item_head(&dot_dot_ih,
1358				       get_ih(&dot_dot_entry_path));
1359			// node containing ".." gets into transaction
1360			reiserfs_prepare_for_journal(old_inode->i_sb,
1361						     dot_dot_de.de_bh, 1);
1362		}
1363		/* we should check seals here, not do
1364		   this stuff, yes? Then, having
1365		   gathered everything into RAM we
1366		   should lock the buffers, yes?  -Hans */
1367		/* probably.  our rename needs to hold more
1368		 ** than one path at once.  The seals would
1369		 ** have to be written to deal with multi-path
1370		 ** issues -chris
1371		 */
1372		/* sanity checking before doing the rename - avoid races many
1373		 ** of the above checks could have scheduled.  We have to be
1374		 ** sure our items haven't been shifted by another process.
1375		 */
1376		if (item_moved(&new_entry_ih, &new_entry_path) ||
1377		    !entry_points_to_object(new_dentry->d_name.name,
1378					    new_dentry->d_name.len,
1379					    &new_de, new_dentry_inode) ||
1380		    item_moved(&old_entry_ih, &old_entry_path) ||
1381		    !entry_points_to_object(old_dentry->d_name.name,
1382					    old_dentry->d_name.len,
1383					    &old_de, old_inode)) {
1384			reiserfs_restore_prepared_buffer(old_inode->i_sb,
1385							 new_de.de_bh);
1386			reiserfs_restore_prepared_buffer(old_inode->i_sb,
1387							 old_de.de_bh);
1388			if (S_ISDIR(old_inode_mode))
1389				reiserfs_restore_prepared_buffer(old_inode->
1390								 i_sb,
1391								 dot_dot_de.
1392								 de_bh);
1393			continue;
1394		}
1395		if (S_ISDIR(old_inode_mode)) {
1396			if (item_moved(&dot_dot_ih, &dot_dot_entry_path) ||
1397			    !entry_points_to_object("..", 2, &dot_dot_de,
1398						    old_dir)) {
1399				reiserfs_restore_prepared_buffer(old_inode->
1400								 i_sb,
1401								 old_de.de_bh);
1402				reiserfs_restore_prepared_buffer(old_inode->
1403								 i_sb,
1404								 new_de.de_bh);
1405				reiserfs_restore_prepared_buffer(old_inode->
1406								 i_sb,
1407								 dot_dot_de.
1408								 de_bh);
1409				continue;
1410			}
1411		}
1412
1413		RFALSE(S_ISDIR(old_inode_mode) &&
1414		       !buffer_journal_prepared(dot_dot_de.de_bh), "");
1415
1416		break;
1417	}
1418
1419	/* ok, all the changes can be done in one fell swoop when we
1420	   have claimed all the buffers needed. */
1421
1422	mark_de_visible(new_de.de_deh + new_de.de_entry_num);
1423	set_ino_in_dir_entry(&new_de, INODE_PKEY(old_inode));
1424	journal_mark_dirty(&th, old_dir->i_sb, new_de.de_bh);
1425
1426	mark_de_hidden(old_de.de_deh + old_de.de_entry_num);
1427	journal_mark_dirty(&th, old_dir->i_sb, old_de.de_bh);
1428	ctime = CURRENT_TIME_SEC;
1429	old_dir->i_ctime = old_dir->i_mtime = ctime;
1430	new_dir->i_ctime = new_dir->i_mtime = ctime;
1431	/* thanks to Alex Adriaanse <alex_a@caltech.edu> for patch which adds ctime update of
1432	   renamed object */
1433	old_inode->i_ctime = ctime;
1434
1435	if (new_dentry_inode) {
1436		// adjust link number of the victim
1437		if (S_ISDIR(new_dentry_inode->i_mode)) {
1438			clear_nlink(new_dentry_inode);
1439		} else {
1440			drop_nlink(new_dentry_inode);
1441		}
1442		new_dentry_inode->i_ctime = ctime;
1443		savelink = new_dentry_inode->i_nlink;
1444	}
1445
1446	if (S_ISDIR(old_inode_mode)) {
1447		// adjust ".." of renamed directory
1448		set_ino_in_dir_entry(&dot_dot_de, INODE_PKEY(new_dir));
1449		journal_mark_dirty(&th, new_dir->i_sb, dot_dot_de.de_bh);
1450
1451		if (!new_dentry_inode)
1452			/* there (in new_dir) was no directory, so it got new link
1453			   (".."  of renamed directory) */
1454			INC_DIR_INODE_NLINK(new_dir);
1455
1456		/* old directory lost one link - ".. " of renamed directory */
1457		DEC_DIR_INODE_NLINK(old_dir);
1458	}
1459	// looks like in 2.3.99pre3 brelse is atomic. so we can use pathrelse
1460	pathrelse(&new_entry_path);
1461	pathrelse(&dot_dot_entry_path);
1462
1463	// FIXME: this reiserfs_cut_from_item's return value may screw up
1464	// anybody, but it will panic if will not be able to find the
1465	// entry. This needs one more clean up
1466	if (reiserfs_cut_from_item
1467	    (&th, &old_entry_path, &(old_de.de_entry_key), old_dir, NULL,
1468	     0) < 0)
1469		reiserfs_error(old_dir->i_sb, "vs-7060",
1470			       "couldn't not cut old name. Fsck later?");
1471
1472	old_dir->i_size -= DEH_SIZE + old_de.de_entrylen;
1473
1474	reiserfs_update_sd(&th, old_dir);
1475	reiserfs_update_sd(&th, new_dir);
1476	reiserfs_update_sd(&th, old_inode);
1477
1478	if (new_dentry_inode) {
1479		if (savelink == 0)
1480			add_save_link(&th, new_dentry_inode,
1481				      0 /* not truncate */ );
1482		reiserfs_update_sd(&th, new_dentry_inode);
1483	}
1484
1485	retval = journal_end(&th, old_dir->i_sb, jbegin_count);
1486	reiserfs_write_unlock(old_dir->i_sb);
1487	return retval;
1488}
1489
1490/*
1491 * directories can handle most operations...
1492 */
1493const struct inode_operations reiserfs_dir_inode_operations = {
1494	//&reiserfs_dir_operations,   /* default_file_ops */
1495	.create = reiserfs_create,
1496	.lookup = reiserfs_lookup,
1497	.link = reiserfs_link,
1498	.unlink = reiserfs_unlink,
1499	.symlink = reiserfs_symlink,
1500	.mkdir = reiserfs_mkdir,
1501	.rmdir = reiserfs_rmdir,
1502	.mknod = reiserfs_mknod,
1503	.rename = reiserfs_rename,
1504	.setattr = reiserfs_setattr,
1505	.setxattr = reiserfs_setxattr,
1506	.getxattr = reiserfs_getxattr,
1507	.listxattr = reiserfs_listxattr,
1508	.removexattr = reiserfs_removexattr,
1509	.permission = reiserfs_permission,
1510};
1511
1512/*
1513 * symlink operations.. same as page_symlink_inode_operations, with xattr
1514 * stuff added
1515 */
1516const struct inode_operations reiserfs_symlink_inode_operations = {
1517	.readlink = generic_readlink,
1518	.follow_link = page_follow_link_light,
1519	.put_link = page_put_link,
1520	.setattr = reiserfs_setattr,
1521	.setxattr = reiserfs_setxattr,
1522	.getxattr = reiserfs_getxattr,
1523	.listxattr = reiserfs_listxattr,
1524	.removexattr = reiserfs_removexattr,
1525	.permission = reiserfs_permission,
1526
1527};
1528
1529/*
1530 * special file operations.. just xattr/acl stuff
1531 */
1532const struct inode_operations reiserfs_special_inode_operations = {
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