kbuild: KBUILD_VERBOSE was exported twice
[linux-2.6/mini2440.git] / fs / reiserfs / namei.c
blob4a333255f27abaf7d3292faf7636ed856299955d
1 /*
2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
4 * Trivial changes by Alan Cox to remove EHASHCOLLISION for compatibility
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.
11 * NO WARRANTY
14 #include <linux/config.h>
15 #include <linux/time.h>
16 #include <linux/bitops.h>
17 #include <linux/reiserfs_fs.h>
18 #include <linux/reiserfs_acl.h>
19 #include <linux/reiserfs_xattr.h>
20 #include <linux/smp_lock.h>
21 #include <linux/quotaops.h>
23 #define INC_DIR_INODE_NLINK(i) if (i->i_nlink != 1) { i->i_nlink++; if (i->i_nlink >= REISERFS_LINK_MAX) i->i_nlink=1; }
24 #define DEC_DIR_INODE_NLINK(i) if (i->i_nlink != 1) i->i_nlink--;
26 // directory item contains array of entry headers. This performs
27 // binary search through that array
28 static int bin_search_in_dir_item (struct reiserfs_dir_entry * de, loff_t off)
30 struct item_head * ih = de->de_ih;
31 struct reiserfs_de_head * deh = de->de_deh;
32 int rbound, lbound, j;
34 lbound = 0;
35 rbound = I_ENTRY_COUNT (ih) - 1;
37 for (j = (rbound + lbound) / 2; lbound <= rbound; j = (rbound + lbound) / 2) {
38 if (off < deh_offset (deh + j)) {
39 rbound = j - 1;
40 continue;
42 if (off > deh_offset (deh + j)) {
43 lbound = j + 1;
44 continue;
46 // this is not name found, but matched third key component
47 de->de_entry_num = j;
48 return NAME_FOUND;
51 de->de_entry_num = lbound;
52 return NAME_NOT_FOUND;
56 // comment? maybe something like set de to point to what the path points to?
57 static inline void set_de_item_location (struct reiserfs_dir_entry * de, struct path * path)
59 de->de_bh = get_last_bh (path);
60 de->de_ih = get_ih (path);
61 de->de_deh = B_I_DEH (de->de_bh, de->de_ih);
62 de->de_item_num = PATH_LAST_POSITION (path);
66 // de_bh, de_ih, de_deh (points to first element of array), de_item_num is set
67 inline void set_de_name_and_namelen (struct reiserfs_dir_entry * de)
69 struct reiserfs_de_head * deh = de->de_deh + de->de_entry_num;
71 if (de->de_entry_num >= ih_entry_count (de->de_ih))
72 BUG ();
74 de->de_entrylen = entry_length (de->de_bh, de->de_ih, de->de_entry_num);
75 de->de_namelen = de->de_entrylen - (de_with_sd (deh) ? SD_SIZE : 0);
76 de->de_name = B_I_PITEM (de->de_bh, de->de_ih) + deh_location(deh);
77 if (de->de_name[de->de_namelen - 1] == 0)
78 de->de_namelen = strlen (de->de_name);
82 // what entry points to
83 static inline void set_de_object_key (struct reiserfs_dir_entry * de)
85 if (de->de_entry_num >= ih_entry_count (de->de_ih))
86 BUG ();
87 de->de_dir_id = deh_dir_id( &(de->de_deh[de->de_entry_num]));
88 de->de_objectid = deh_objectid( &(de->de_deh[de->de_entry_num]));
92 static inline void store_de_entry_key (struct reiserfs_dir_entry * de)
94 struct reiserfs_de_head * deh = de->de_deh + de->de_entry_num;
96 if (de->de_entry_num >= ih_entry_count (de->de_ih))
97 BUG ();
99 /* store key of the found entry */
100 de->de_entry_key.version = KEY_FORMAT_3_5;
101 de->de_entry_key.on_disk_key.k_dir_id = le32_to_cpu (de->de_ih->ih_key.k_dir_id);
102 de->de_entry_key.on_disk_key.k_objectid = le32_to_cpu (de->de_ih->ih_key.k_objectid);
103 set_cpu_key_k_offset (&(de->de_entry_key), deh_offset (deh));
104 set_cpu_key_k_type (&(de->de_entry_key), TYPE_DIRENTRY);
108 /* We assign a key to each directory item, and place multiple entries
109 in a single directory item. A directory item has a key equal to the
110 key of the first directory entry in it.
112 This function first calls search_by_key, then, if item whose first
113 entry matches is not found it looks for the entry inside directory
114 item found by search_by_key. Fills the path to the entry, and to the
115 entry position in the item
119 /* The function is NOT SCHEDULE-SAFE! */
120 int search_by_entry_key (struct super_block * sb, const struct cpu_key * key,
121 struct path * path, struct reiserfs_dir_entry * de)
123 int retval;
125 retval = search_item (sb, key, path);
126 switch (retval) {
127 case ITEM_NOT_FOUND:
128 if (!PATH_LAST_POSITION (path)) {
129 reiserfs_warning (sb, "vs-7000: search_by_entry_key: search_by_key returned item position == 0");
130 pathrelse(path) ;
131 return IO_ERROR ;
133 PATH_LAST_POSITION (path) --;
135 case ITEM_FOUND:
136 break;
138 case IO_ERROR:
139 return retval;
141 default:
142 pathrelse (path);
143 reiserfs_warning (sb, "vs-7002: search_by_entry_key: no path to here");
144 return IO_ERROR;
147 set_de_item_location (de, path);
149 #ifdef CONFIG_REISERFS_CHECK
150 if (!is_direntry_le_ih (de->de_ih) ||
151 COMP_SHORT_KEYS (&(de->de_ih->ih_key), key)) {
152 print_block (de->de_bh, 0, -1, -1);
153 reiserfs_panic (sb, "vs-7005: search_by_entry_key: found item %h is not directory item or "
154 "does not belong to the same directory as key %K", de->de_ih, key);
156 #endif /* CONFIG_REISERFS_CHECK */
158 /* binary search in directory item by third componen t of the
159 key. sets de->de_entry_num of de */
160 retval = bin_search_in_dir_item (de, cpu_key_k_offset (key));
161 path->pos_in_item = de->de_entry_num;
162 if (retval != NAME_NOT_FOUND) {
163 // ugly, but rename needs de_bh, de_deh, de_name, de_namelen, de_objectid set
164 set_de_name_and_namelen (de);
165 set_de_object_key (de);
167 return retval;
172 /* Keyed 32-bit hash function using TEA in a Davis-Meyer function */
174 /* The third component is hashed, and you can choose from more than
175 one hash function. Per directory hashes are not yet implemented
176 but are thought about. This function should be moved to hashes.c
177 Jedi, please do so. -Hans */
179 static __u32 get_third_component (struct super_block * s,
180 const char * name, int len)
182 __u32 res;
184 if (!len || (len == 1 && name[0] == '.'))
185 return DOT_OFFSET;
186 if (len == 2 && name[0] == '.' && name[1] == '.')
187 return DOT_DOT_OFFSET;
189 res = REISERFS_SB(s)->s_hash_function (name, len);
191 // take bits from 7-th to 30-th including both bounds
192 res = GET_HASH_VALUE(res);
193 if (res == 0)
194 // needed to have no names before "." and ".." those have hash
195 // value == 0 and generation conters 1 and 2 accordingly
196 res = 128;
197 return res + MAX_GENERATION_NUMBER;
201 static int reiserfs_match (struct reiserfs_dir_entry * de,
202 const char * name, int namelen)
204 int retval = NAME_NOT_FOUND;
206 if ((namelen == de->de_namelen) &&
207 !memcmp(de->de_name, name, de->de_namelen))
208 retval = (de_visible (de->de_deh + de->de_entry_num) ? NAME_FOUND : NAME_FOUND_INVISIBLE);
210 return retval;
214 /* de's de_bh, de_ih, de_deh, de_item_num, de_entry_num are set already */
216 /* used when hash collisions exist */
219 static int linear_search_in_dir_item (struct cpu_key * key, struct reiserfs_dir_entry * de,
220 const char * name, int namelen)
222 struct reiserfs_de_head * deh = de->de_deh;
223 int retval;
224 int i;
226 i = de->de_entry_num;
228 if (i == I_ENTRY_COUNT (de->de_ih) ||
229 GET_HASH_VALUE (deh_offset (deh + i)) != GET_HASH_VALUE (cpu_key_k_offset (key))) {
230 i --;
233 RFALSE( de->de_deh != B_I_DEH (de->de_bh, de->de_ih),
234 "vs-7010: array of entry headers not found");
236 deh += i;
238 for (; i >= 0; i --, deh --) {
239 if (GET_HASH_VALUE (deh_offset (deh)) !=
240 GET_HASH_VALUE (cpu_key_k_offset (key))) {
241 // hash value does not match, no need to check whole name
242 return NAME_NOT_FOUND;
245 /* mark, that this generation number is used */
246 if (de->de_gen_number_bit_string)
247 set_bit (GET_GENERATION_NUMBER (deh_offset (deh)), (unsigned long *)de->de_gen_number_bit_string);
249 // calculate pointer to name and namelen
250 de->de_entry_num = i;
251 set_de_name_and_namelen (de);
253 if ((retval = reiserfs_match (de, name, namelen)) != NAME_NOT_FOUND) {
254 // de's de_name, de_namelen, de_recordlen are set. Fill the rest:
256 // key of pointed object
257 set_de_object_key (de);
259 store_de_entry_key (de);
261 // retval can be NAME_FOUND or NAME_FOUND_INVISIBLE
262 return retval;
266 if (GET_GENERATION_NUMBER (le_ih_k_offset (de->de_ih)) == 0)
267 /* we have reached left most entry in the node. In common we
268 have to go to the left neighbor, but if generation counter
269 is 0 already, we know for sure, that there is no name with
270 the same hash value */
271 // FIXME: this work correctly only because hash value can not
272 // be 0. Btw, in case of Yura's hash it is probably possible,
273 // so, this is a bug
274 return NAME_NOT_FOUND;
276 RFALSE( de->de_item_num,
277 "vs-7015: two diritems of the same directory in one node?");
279 return GOTO_PREVIOUS_ITEM;
283 // may return NAME_FOUND, NAME_FOUND_INVISIBLE, NAME_NOT_FOUND
284 // FIXME: should add something like IOERROR
285 static int reiserfs_find_entry (struct inode * dir, const char * name, int namelen,
286 struct path * path_to_entry, struct reiserfs_dir_entry * de)
288 struct cpu_key key_to_search;
289 int retval;
292 if (namelen > REISERFS_MAX_NAME (dir->i_sb->s_blocksize))
293 return NAME_NOT_FOUND;
295 /* we will search for this key in the tree */
296 make_cpu_key (&key_to_search, dir,
297 get_third_component (dir->i_sb, name, namelen), TYPE_DIRENTRY, 3);
299 while (1) {
300 retval = search_by_entry_key (dir->i_sb, &key_to_search, path_to_entry, de);
301 if (retval == IO_ERROR) {
302 reiserfs_warning (dir->i_sb, "zam-7001: io error in %s",
303 __FUNCTION__);
304 return IO_ERROR;
307 /* compare names for all entries having given hash value */
308 retval = linear_search_in_dir_item (&key_to_search, de, name, 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;
315 /* there is left neighboring item of this directory and given entry can be there */
316 set_cpu_key_k_offset (&key_to_search, le_ih_k_offset (de->de_ih) - 1);
317 pathrelse (path_to_entry);
319 } /* while (1) */
323 static struct dentry * reiserfs_lookup (struct inode * dir, struct dentry * dentry, struct nameidata *nd)
325 int retval;
326 struct inode * inode = NULL;
327 struct reiserfs_dir_entry de;
328 INITIALIZE_PATH (path_to_entry);
330 if (REISERFS_MAX_NAME (dir->i_sb->s_blocksize) < dentry->d_name.len)
331 return ERR_PTR(-ENAMETOOLONG);
333 reiserfs_write_lock(dir->i_sb);
334 de.de_gen_number_bit_string = NULL;
335 retval = reiserfs_find_entry (dir, dentry->d_name.name, dentry->d_name.len, &path_to_entry, &de);
336 pathrelse (&path_to_entry);
337 if (retval == NAME_FOUND) {
338 /* Hide the .reiserfs_priv directory */
339 if (reiserfs_xattrs (dir->i_sb) &&
340 !old_format_only(dir->i_sb) &&
341 REISERFS_SB(dir->i_sb)->priv_root &&
342 REISERFS_SB(dir->i_sb)->priv_root->d_inode &&
343 de.de_objectid == le32_to_cpu (INODE_PKEY(REISERFS_SB(dir->i_sb)->priv_root->d_inode)->k_objectid)) {
344 reiserfs_write_unlock (dir->i_sb);
345 return ERR_PTR (-EACCES);
348 inode = reiserfs_iget (dir->i_sb, (struct cpu_key *)&(de.de_dir_id));
349 if (!inode || IS_ERR(inode)) {
350 reiserfs_write_unlock(dir->i_sb);
351 return ERR_PTR(-EACCES);
354 /* Propogate the priv_object flag so we know we're in the priv tree */
355 if (is_reiserfs_priv_object (dir))
356 reiserfs_mark_inode_private (inode);
358 reiserfs_write_unlock(dir->i_sb);
359 if ( retval == IO_ERROR ) {
360 return ERR_PTR(-EIO);
363 if (inode)
364 return d_splice_alias(inode, dentry);
366 d_add(dentry, inode);
367 return NULL;
372 ** looks up the dentry of the parent directory for child.
373 ** taken from ext2_get_parent
375 struct dentry *reiserfs_get_parent(struct dentry *child)
377 int retval;
378 struct inode * inode = NULL;
379 struct reiserfs_dir_entry de;
380 INITIALIZE_PATH (path_to_entry);
381 struct dentry *parent;
382 struct inode *dir = child->d_inode ;
385 if (dir->i_nlink == 0) {
386 return ERR_PTR(-ENOENT);
388 de.de_gen_number_bit_string = NULL;
390 reiserfs_write_lock(dir->i_sb);
391 retval = reiserfs_find_entry (dir, "..", 2, &path_to_entry, &de);
392 pathrelse (&path_to_entry);
393 if (retval != NAME_FOUND) {
394 reiserfs_write_unlock(dir->i_sb);
395 return ERR_PTR(-ENOENT);
397 inode = reiserfs_iget (dir->i_sb, (struct cpu_key *)&(de.de_dir_id));
398 reiserfs_write_unlock(dir->i_sb);
400 if (!inode || IS_ERR(inode)) {
401 return ERR_PTR(-EACCES);
403 parent = d_alloc_anon(inode);
404 if (!parent) {
405 iput(inode);
406 parent = ERR_PTR(-ENOMEM);
408 return parent;
412 /* add entry to the directory (entry can be hidden).
414 insert definition of when hidden directories are used here -Hans
416 Does not mark dir inode dirty, do it after successesfull call to it */
418 static int reiserfs_add_entry (struct reiserfs_transaction_handle *th, struct inode * dir,
419 const char * name, int namelen, struct inode * inode,
420 int visible)
422 struct cpu_key entry_key;
423 struct reiserfs_de_head * deh;
424 INITIALIZE_PATH (path);
425 struct reiserfs_dir_entry de;
426 int bit_string [MAX_GENERATION_NUMBER / (sizeof(int) * 8) + 1];
427 int gen_number;
428 char small_buf[32+DEH_SIZE] ; /* 48 bytes now and we avoid kmalloc
429 if we create file with short name */
430 char * buffer;
431 int buflen, paste_size;
432 int retval;
434 BUG_ON (!th->t_trans_id);
436 /* cannot allow items to be added into a busy deleted directory */
437 if (!namelen)
438 return -EINVAL;
440 if (namelen > REISERFS_MAX_NAME (dir->i_sb->s_blocksize))
441 return -ENAMETOOLONG;
443 /* each entry has unique key. compose it */
444 make_cpu_key (&entry_key, dir,
445 get_third_component (dir->i_sb, name, namelen), TYPE_DIRENTRY, 3);
447 /* get memory for composing the entry */
448 buflen = DEH_SIZE + ROUND_UP (namelen);
449 if (buflen > sizeof (small_buf)) {
450 buffer = reiserfs_kmalloc (buflen, GFP_NOFS, dir->i_sb);
451 if (buffer == 0)
452 return -ENOMEM;
453 } else
454 buffer = small_buf;
456 paste_size = (get_inode_sd_version (dir) == STAT_DATA_V1) ? (DEH_SIZE + namelen) : buflen;
458 /* fill buffer : directory entry head, name[, dir objectid | , stat data | ,stat data, dir objectid ] */
459 deh = (struct reiserfs_de_head *)buffer;
460 deh->deh_location = 0; /* JDM Endian safe if 0 */
461 put_deh_offset( deh, cpu_key_k_offset( &entry_key ) );
462 deh->deh_state = 0; /* JDM Endian safe if 0 */
463 /* put key (ino analog) to de */
464 deh->deh_dir_id = INODE_PKEY (inode)->k_dir_id; /* safe: k_dir_id is le */
465 deh->deh_objectid = INODE_PKEY (inode)->k_objectid; /* safe: k_objectid is le */
467 /* copy name */
468 memcpy ((char *)(deh + 1), name, namelen);
469 /* padd by 0s to the 4 byte boundary */
470 padd_item ((char *)(deh + 1), ROUND_UP (namelen), namelen);
472 /* entry is ready to be pasted into tree, set 'visibility' and 'stat data in entry' attributes */
473 mark_de_without_sd (deh);
474 visible ? mark_de_visible (deh) : mark_de_hidden (deh);
476 /* find the proper place for the new entry */
477 memset (bit_string, 0, sizeof (bit_string));
478 de.de_gen_number_bit_string = (char *)bit_string;
479 retval = reiserfs_find_entry (dir, name, namelen, &path, &de);
480 if( retval != NAME_NOT_FOUND ) {
481 if (buffer != small_buf)
482 reiserfs_kfree (buffer, buflen, dir->i_sb);
483 pathrelse (&path);
485 if ( retval == IO_ERROR ) {
486 return -EIO;
489 if (retval != NAME_FOUND) {
490 reiserfs_warning (dir->i_sb, "zam-7002:%s: \"reiserfs_find_entry\" "
491 "has returned unexpected value (%d)",
492 __FUNCTION__, retval);
495 return -EEXIST;
498 gen_number = find_first_zero_bit ((unsigned long *)bit_string, MAX_GENERATION_NUMBER + 1);
499 if (gen_number > MAX_GENERATION_NUMBER) {
500 /* there is no free generation number */
501 reiserfs_warning (dir->i_sb, "reiserfs_add_entry: Congratulations! we have got hash function screwed up");
502 if (buffer != small_buf)
503 reiserfs_kfree (buffer, buflen, dir->i_sb);
504 pathrelse (&path);
505 return -EBUSY;
507 /* adjust offset of directory enrty */
508 put_deh_offset(deh, SET_GENERATION_NUMBER(deh_offset(deh), gen_number));
509 set_cpu_key_k_offset (&entry_key, deh_offset(deh));
511 /* update max-hash-collisions counter in reiserfs_sb_info */
512 PROC_INFO_MAX( th -> t_super, max_hash_collisions, gen_number );
514 if (gen_number != 0) { /* we need to re-search for the insertion point */
515 if (search_by_entry_key (dir->i_sb, &entry_key, &path, &de) != NAME_NOT_FOUND) {
516 reiserfs_warning (dir->i_sb, "vs-7032: reiserfs_add_entry: "
517 "entry with this key (%K) already exists",
518 &entry_key);
520 if (buffer != small_buf)
521 reiserfs_kfree (buffer, buflen, dir->i_sb);
522 pathrelse (&path);
523 return -EBUSY;
527 /* perform the insertion of the entry that we have prepared */
528 retval = reiserfs_paste_into_item (th, &path, &entry_key, dir, buffer, paste_size);
529 if (buffer != small_buf)
530 reiserfs_kfree (buffer, buflen, dir->i_sb);
531 if (retval) {
532 reiserfs_check_path(&path) ;
533 return retval;
536 dir->i_size += paste_size;
537 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
538 if (!S_ISDIR (inode->i_mode) && visible)
539 // reiserfs_mkdir or reiserfs_rename will do that by itself
540 reiserfs_update_sd (th, dir);
542 reiserfs_check_path(&path) ;
543 return 0;
546 /* quota utility function, call if you've had to abort after calling
547 ** new_inode_init, and have not called reiserfs_new_inode yet.
548 ** This should only be called on inodes that do not have stat data
549 ** inserted into the tree yet.
551 static int drop_new_inode(struct inode *inode) {
552 DQUOT_DROP(inode);
553 make_bad_inode(inode) ;
554 inode->i_flags |= S_NOQUOTA;
555 iput(inode) ;
556 return 0 ;
559 /* utility function that does setup for reiserfs_new_inode.
560 ** DQUOT_INIT needs lots of credits so it's better to have it
561 ** outside of a transaction, so we had to pull some bits of
562 ** reiserfs_new_inode out into this func.
564 static int new_inode_init(struct inode *inode, struct inode *dir, int mode) {
566 /* the quota init calls have to know who to charge the quota to, so
567 ** we have to set uid and gid here
569 inode->i_uid = current->fsuid;
570 inode->i_mode = mode;
572 if (dir->i_mode & S_ISGID) {
573 inode->i_gid = dir->i_gid;
574 if (S_ISDIR(mode))
575 inode->i_mode |= S_ISGID;
576 } else {
577 inode->i_gid = current->fsgid;
579 DQUOT_INIT(inode);
580 return 0 ;
583 static int reiserfs_create (struct inode * dir, struct dentry *dentry, int mode,
584 struct nameidata *nd)
586 int retval;
587 struct inode * inode;
588 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
589 int jbegin_count = JOURNAL_PER_BALANCE_CNT * 2 + 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb)+REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
590 struct reiserfs_transaction_handle th ;
591 int locked;
593 if (!(inode = new_inode(dir->i_sb))) {
594 return -ENOMEM ;
596 new_inode_init(inode, dir, mode);
598 locked = reiserfs_cache_default_acl (dir);
600 reiserfs_write_lock(dir->i_sb);
602 if (locked)
603 reiserfs_write_lock_xattrs (dir->i_sb);
605 retval = journal_begin(&th, dir->i_sb, jbegin_count);
606 if (retval) {
607 drop_new_inode (inode);
608 goto out_failed;
611 retval = reiserfs_new_inode (&th, dir, mode, NULL, 0/*i_size*/, dentry, inode);
612 if (retval)
613 goto out_failed;
615 if (locked) {
616 reiserfs_write_unlock_xattrs (dir->i_sb);
617 locked = 0;
620 inode->i_op = &reiserfs_file_inode_operations;
621 inode->i_fop = &reiserfs_file_operations;
622 inode->i_mapping->a_ops = &reiserfs_address_space_operations ;
624 retval = reiserfs_add_entry (&th, dir, dentry->d_name.name, dentry->d_name.len,
625 inode, 1/*visible*/);
626 if (retval) {
627 int err;
628 inode->i_nlink--;
629 reiserfs_update_sd (&th, inode);
630 err = journal_end(&th, dir->i_sb, jbegin_count) ;
631 if (err)
632 retval = err;
633 iput (inode);
634 goto out_failed;
636 reiserfs_update_inode_transaction(inode) ;
637 reiserfs_update_inode_transaction(dir) ;
639 d_instantiate(dentry, inode);
640 retval = journal_end(&th, dir->i_sb, jbegin_count) ;
642 out_failed:
643 if (locked)
644 reiserfs_write_unlock_xattrs (dir->i_sb);
645 reiserfs_write_unlock(dir->i_sb);
646 return retval;
650 static int reiserfs_mknod (struct inode * dir, struct dentry *dentry, int mode, dev_t rdev)
652 int retval;
653 struct inode * inode;
654 struct reiserfs_transaction_handle th ;
655 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
656 int jbegin_count = JOURNAL_PER_BALANCE_CNT * 3 + 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb)+REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
657 int locked;
659 if (!new_valid_dev(rdev))
660 return -EINVAL;
662 if (!(inode = new_inode(dir->i_sb))) {
663 return -ENOMEM ;
665 new_inode_init(inode, dir, mode);
667 locked = reiserfs_cache_default_acl (dir);
669 reiserfs_write_lock(dir->i_sb);
671 if (locked)
672 reiserfs_write_lock_xattrs (dir->i_sb);
674 retval = journal_begin(&th, dir->i_sb, jbegin_count) ;
675 if (retval) {
676 drop_new_inode (inode);
677 goto out_failed;
680 retval = reiserfs_new_inode (&th, dir, mode, NULL, 0/*i_size*/, dentry, inode);
681 if (retval) {
682 goto out_failed;
685 if (locked) {
686 reiserfs_write_unlock_xattrs (dir->i_sb);
687 locked = 0;
691 inode->i_op = &reiserfs_special_inode_operations;
692 init_special_inode(inode, inode->i_mode, rdev) ;
694 //FIXME: needed for block and char devices only
695 reiserfs_update_sd (&th, inode);
697 reiserfs_update_inode_transaction(inode) ;
698 reiserfs_update_inode_transaction(dir) ;
700 retval = reiserfs_add_entry (&th, dir, dentry->d_name.name, dentry->d_name.len,
701 inode, 1/*visible*/);
702 if (retval) {
703 int err;
704 inode->i_nlink--;
705 reiserfs_update_sd (&th, inode);
706 err = journal_end(&th, dir->i_sb, jbegin_count) ;
707 if (err)
708 retval = err;
709 iput (inode);
710 goto out_failed;
713 d_instantiate(dentry, inode);
714 retval = journal_end(&th, dir->i_sb, jbegin_count) ;
716 out_failed:
717 if (locked)
718 reiserfs_write_unlock_xattrs (dir->i_sb);
719 reiserfs_write_unlock(dir->i_sb);
720 return retval;
724 static int reiserfs_mkdir (struct inode * dir, struct dentry *dentry, int mode)
726 int retval;
727 struct inode * inode;
728 struct reiserfs_transaction_handle th ;
729 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
730 int jbegin_count = JOURNAL_PER_BALANCE_CNT * 3 + 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb)+REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
731 int locked;
733 #ifdef DISPLACE_NEW_PACKING_LOCALITIES
734 /* set flag that new packing locality created and new blocks for the content * of that directory are not displaced yet */
735 REISERFS_I(dir)->new_packing_locality = 1;
736 #endif
737 mode = S_IFDIR | mode;
738 if (!(inode = new_inode(dir->i_sb))) {
739 return -ENOMEM ;
741 new_inode_init(inode, dir, mode);
743 locked = reiserfs_cache_default_acl (dir);
745 reiserfs_write_lock(dir->i_sb);
746 if (locked)
747 reiserfs_write_lock_xattrs (dir->i_sb);
749 retval = journal_begin(&th, dir->i_sb, jbegin_count) ;
750 if (retval) {
751 drop_new_inode (inode);
752 goto out_failed;
756 /* inc the link count now, so another writer doesn't overflow it while
757 ** we sleep later on.
759 INC_DIR_INODE_NLINK(dir)
761 retval = reiserfs_new_inode (&th, dir, mode, NULL/*symlink*/,
762 old_format_only (dir->i_sb) ?
763 EMPTY_DIR_SIZE_V1 : EMPTY_DIR_SIZE,
764 dentry, inode);
765 if (retval) {
766 dir->i_nlink-- ;
767 goto out_failed;
770 if (locked) {
771 reiserfs_write_unlock_xattrs (dir->i_sb);
772 locked = 0;
775 reiserfs_update_inode_transaction(inode) ;
776 reiserfs_update_inode_transaction(dir) ;
778 inode->i_op = &reiserfs_dir_inode_operations;
779 inode->i_fop = &reiserfs_dir_operations;
781 // note, _this_ add_entry will not update dir's stat data
782 retval = reiserfs_add_entry (&th, dir, dentry->d_name.name, dentry->d_name.len,
783 inode, 1/*visible*/);
784 if (retval) {
785 int err;
786 inode->i_nlink = 0;
787 DEC_DIR_INODE_NLINK(dir);
788 reiserfs_update_sd (&th, inode);
789 err = journal_end(&th, dir->i_sb, jbegin_count) ;
790 if (err)
791 retval = err;
792 iput (inode);
793 goto out_failed;
796 // the above add_entry did not update dir's stat data
797 reiserfs_update_sd (&th, dir);
799 d_instantiate(dentry, inode);
800 retval = journal_end(&th, dir->i_sb, jbegin_count) ;
801 out_failed:
802 if (locked)
803 reiserfs_write_unlock_xattrs (dir->i_sb);
804 reiserfs_write_unlock(dir->i_sb);
805 return retval;
808 static inline int reiserfs_empty_dir(struct inode *inode) {
809 /* we can cheat because an old format dir cannot have
810 ** EMPTY_DIR_SIZE, and a new format dir cannot have
811 ** EMPTY_DIR_SIZE_V1. So, if the inode is either size,
812 ** regardless of disk format version, the directory is empty.
814 if (inode->i_size != EMPTY_DIR_SIZE &&
815 inode->i_size != EMPTY_DIR_SIZE_V1) {
816 return 0 ;
818 return 1 ;
821 static int reiserfs_rmdir (struct inode * dir, struct dentry *dentry)
823 int retval, err;
824 struct inode * inode;
825 struct reiserfs_transaction_handle th ;
826 int jbegin_count;
827 INITIALIZE_PATH (path);
828 struct reiserfs_dir_entry de;
831 /* we will be doing 2 balancings and update 2 stat data, we change quotas
832 * of the owner of the directory and of the owner of the parent directory.
833 * The quota structure is possibly deleted only on last iput => outside
834 * of this transaction */
835 jbegin_count = JOURNAL_PER_BALANCE_CNT * 2 + 2 + 4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
837 reiserfs_write_lock(dir->i_sb);
838 retval = journal_begin(&th, dir->i_sb, jbegin_count) ;
839 if (retval)
840 goto out_rmdir;
842 de.de_gen_number_bit_string = NULL;
843 if ( (retval = reiserfs_find_entry (dir, dentry->d_name.name, dentry->d_name.len, &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;
851 inode = dentry->d_inode;
853 reiserfs_update_inode_transaction(inode) ;
854 reiserfs_update_inode_transaction(dir) ;
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;
862 if (!reiserfs_empty_dir(inode)) {
863 retval = -ENOTEMPTY;
864 goto end_rmdir;
867 /* cut entry from dir directory */
868 retval = reiserfs_cut_from_item (&th, &path, &(de.de_entry_key), dir,
869 NULL, /* page */
870 0/*new file size - not used here*/);
871 if (retval < 0)
872 goto end_rmdir;
874 if ( inode->i_nlink != 2 && inode->i_nlink != 1 )
875 reiserfs_warning (inode->i_sb, "%s: empty directory has nlink "
876 "!= 2 (%d)", __FUNCTION__, inode->i_nlink);
878 inode->i_nlink = 0;
879 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
880 reiserfs_update_sd (&th, inode);
882 DEC_DIR_INODE_NLINK(dir)
883 dir->i_size -= (DEH_SIZE + de.de_entrylen);
884 reiserfs_update_sd (&th, dir);
886 /* prevent empty directory from getting lost */
887 add_save_link (&th, inode, 0/* not truncate */);
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;
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;
905 static int reiserfs_unlink (struct inode * dir, struct dentry *dentry)
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;
915 inode = dentry->d_inode;
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 = JOURNAL_PER_BALANCE_CNT * 2 + 2 + 4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
923 reiserfs_write_lock(dir->i_sb);
924 retval = journal_begin(&th, dir->i_sb, jbegin_count) ;
925 if (retval)
926 goto out_unlink;
928 de.de_gen_number_bit_string = NULL;
929 if ( (retval = reiserfs_find_entry (dir, dentry->d_name.name, dentry->d_name.len, &path, &de)) == NAME_NOT_FOUND) {
930 retval = -ENOENT;
931 goto end_unlink;
932 } else if (retval == IO_ERROR) {
933 retval = -EIO;
934 goto end_unlink;
937 reiserfs_update_inode_transaction(inode) ;
938 reiserfs_update_inode_transaction(dir) ;
940 if (de.de_objectid != inode->i_ino) {
941 // FIXME: compare key of an object and a key found in the
942 // entry
943 retval = -EIO;
944 goto end_unlink;
947 if (!inode->i_nlink) {
948 reiserfs_warning (inode->i_sb, "%s: deleting nonexistent file "
949 "(%s:%lu), %d", __FUNCTION__,
950 reiserfs_bdevname (inode->i_sb), inode->i_ino,
951 inode->i_nlink);
952 inode->i_nlink = 1;
955 inode->i_nlink--;
958 * we schedule before doing the add_save_link call, save the link
959 * count so we don't race
961 savelink = inode->i_nlink;
964 retval = reiserfs_cut_from_item (&th, &path, &(de.de_entry_key), dir, NULL, 0);
965 if (retval < 0) {
966 inode->i_nlink++;
967 goto end_unlink;
969 inode->i_ctime = CURRENT_TIME_SEC;
970 reiserfs_update_sd (&th, inode);
972 dir->i_size -= (de.de_entrylen + DEH_SIZE);
973 dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
974 reiserfs_update_sd (&th, dir);
976 if (!savelink)
977 /* prevent file from getting lost */
978 add_save_link (&th, inode, 0/* not truncate */);
980 retval = journal_end(&th, dir->i_sb, jbegin_count) ;
981 reiserfs_check_path(&path) ;
982 reiserfs_write_unlock(dir->i_sb);
983 return retval;
985 end_unlink:
986 pathrelse (&path);
987 err = journal_end(&th, dir->i_sb, jbegin_count) ;
988 reiserfs_check_path(&path) ;
989 if (err)
990 retval = err;
991 out_unlink:
992 reiserfs_write_unlock(dir->i_sb);
993 return retval;
996 static int reiserfs_symlink (struct inode * parent_dir,
997 struct dentry * dentry, const char * symname)
999 int retval;
1000 struct inode * inode;
1001 char * name;
1002 int item_len;
1003 struct reiserfs_transaction_handle th ;
1004 int mode = S_IFLNK | S_IRWXUGO;
1005 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
1006 int jbegin_count = JOURNAL_PER_BALANCE_CNT * 3 + 2 * (REISERFS_QUOTA_INIT_BLOCKS(parent_dir->i_sb)+REISERFS_QUOTA_TRANS_BLOCKS(parent_dir->i_sb));
1008 if (!(inode = new_inode(parent_dir->i_sb))) {
1009 return -ENOMEM ;
1011 new_inode_init(inode, parent_dir, mode);
1013 reiserfs_write_lock(parent_dir->i_sb);
1014 item_len = ROUND_UP (strlen (symname));
1015 if (item_len > MAX_DIRECT_ITEM_LEN (parent_dir->i_sb->s_blocksize)) {
1016 retval = -ENAMETOOLONG;
1017 drop_new_inode(inode);
1018 goto out_failed;
1021 name = reiserfs_kmalloc (item_len, GFP_NOFS, parent_dir->i_sb);
1022 if (!name) {
1023 drop_new_inode(inode);
1024 retval = -ENOMEM;
1025 goto out_failed;
1027 memcpy (name, symname, strlen (symname));
1028 padd_item (name, item_len, strlen (symname));
1030 /* We would inherit the default ACL here, but symlinks don't get ACLs */
1032 retval = journal_begin(&th, parent_dir->i_sb, jbegin_count) ;
1033 if (retval) {
1034 drop_new_inode (inode);
1035 reiserfs_kfree (name, item_len, parent_dir->i_sb);
1036 goto out_failed;
1039 retval = reiserfs_new_inode (&th, parent_dir, mode, name, strlen (symname),
1040 dentry, inode);
1041 reiserfs_kfree (name, item_len, parent_dir->i_sb);
1042 if (retval) { /* reiserfs_new_inode iputs for us */
1043 goto out_failed;
1046 reiserfs_update_inode_transaction(inode) ;
1047 reiserfs_update_inode_transaction(parent_dir) ;
1049 inode->i_op = &reiserfs_symlink_inode_operations;
1050 inode->i_mapping->a_ops = &reiserfs_address_space_operations;
1052 // must be sure this inode is written with this transaction
1054 //reiserfs_update_sd (&th, inode, READ_BLOCKS);
1056 retval = reiserfs_add_entry (&th, parent_dir, dentry->d_name.name,
1057 dentry->d_name.len, inode, 1/*visible*/);
1058 if (retval) {
1059 int err;
1060 inode->i_nlink--;
1061 reiserfs_update_sd (&th, inode);
1062 err = journal_end(&th, parent_dir->i_sb, jbegin_count) ;
1063 if (err)
1064 retval = err;
1065 iput (inode);
1066 goto out_failed;
1069 d_instantiate(dentry, inode);
1070 retval = journal_end(&th, parent_dir->i_sb, jbegin_count) ;
1071 out_failed:
1072 reiserfs_write_unlock(parent_dir->i_sb);
1073 return retval;
1076 static int reiserfs_link (struct dentry * old_dentry, struct inode * dir, struct dentry * dentry)
1078 int retval;
1079 struct inode *inode = old_dentry->d_inode;
1080 struct reiserfs_transaction_handle th ;
1081 /* We need blocks for transaction + update of quotas for the owners of the directory */
1082 int jbegin_count = JOURNAL_PER_BALANCE_CNT * 3 + 2 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
1084 reiserfs_write_lock(dir->i_sb);
1085 if (inode->i_nlink >= REISERFS_LINK_MAX) {
1086 //FIXME: sd_nlink is 32 bit for new files
1087 reiserfs_write_unlock(dir->i_sb);
1088 return -EMLINK;
1090 if (inode->i_nlink == 0) {
1091 reiserfs_write_unlock(dir->i_sb);
1092 return -ENOENT;
1095 /* inc before scheduling so reiserfs_unlink knows we are here */
1096 inode->i_nlink++;
1098 retval = journal_begin(&th, dir->i_sb, jbegin_count) ;
1099 if (retval) {
1100 inode->i_nlink--;
1101 reiserfs_write_unlock (dir->i_sb);
1102 return retval;
1105 /* create new entry */
1106 retval = reiserfs_add_entry (&th, dir, dentry->d_name.name, dentry->d_name.len,
1107 inode, 1/*visible*/);
1109 reiserfs_update_inode_transaction(inode) ;
1110 reiserfs_update_inode_transaction(dir) ;
1112 if (retval) {
1113 int err;
1114 inode->i_nlink--;
1115 err = journal_end(&th, dir->i_sb, jbegin_count) ;
1116 reiserfs_write_unlock(dir->i_sb);
1117 return err ? err : retval;
1120 inode->i_ctime = CURRENT_TIME_SEC;
1121 reiserfs_update_sd (&th, inode);
1123 atomic_inc(&inode->i_count) ;
1124 d_instantiate(dentry, inode);
1125 retval = journal_end(&th, dir->i_sb, jbegin_count) ;
1126 reiserfs_write_unlock(dir->i_sb);
1127 return retval;
1131 // de contains information pointing to an entry which
1132 static int de_still_valid (const char * name, int len, struct reiserfs_dir_entry * de)
1134 struct reiserfs_dir_entry tmp = *de;
1136 // recalculate pointer to name and name length
1137 set_de_name_and_namelen (&tmp);
1138 // FIXME: could check more
1139 if (tmp.de_namelen != len || memcmp (name, de->de_name, len))
1140 return 0;
1141 return 1;
1145 static int entry_points_to_object (const char * name, int len, struct reiserfs_dir_entry * de, struct inode * inode)
1147 if (!de_still_valid (name, len, de))
1148 return 0;
1150 if (inode) {
1151 if (!de_visible (de->de_deh + de->de_entry_num))
1152 reiserfs_panic (NULL, "vs-7042: entry_points_to_object: entry must be visible");
1153 return (de->de_objectid == inode->i_ino) ? 1 : 0;
1156 /* this must be added hidden entry */
1157 if (de_visible (de->de_deh + de->de_entry_num))
1158 reiserfs_panic (NULL, "vs-7043: entry_points_to_object: entry must be visible");
1160 return 1;
1164 /* sets key of objectid the entry has to point to */
1165 static void set_ino_in_dir_entry (struct reiserfs_dir_entry * de, struct reiserfs_key * key)
1167 /* JDM These operations are endian safe - both are le */
1168 de->de_deh[de->de_entry_num].deh_dir_id = key->k_dir_id;
1169 de->de_deh[de->de_entry_num].deh_objectid = key->k_objectid;
1174 * process, that is going to call fix_nodes/do_balance must hold only
1175 * one path. If it holds 2 or more, it can get into endless waiting in
1176 * get_empty_nodes or its clones
1178 static int reiserfs_rename (struct inode * old_dir, struct dentry *old_dentry,
1179 struct inode * new_dir, struct dentry *new_dentry)
1181 int retval;
1182 INITIALIZE_PATH (old_entry_path);
1183 INITIALIZE_PATH (new_entry_path);
1184 INITIALIZE_PATH (dot_dot_entry_path);
1185 struct item_head new_entry_ih, old_entry_ih, dot_dot_ih ;
1186 struct reiserfs_dir_entry old_de, new_de, dot_dot_de;
1187 struct inode * old_inode, * new_dentry_inode;
1188 struct reiserfs_transaction_handle th ;
1189 int jbegin_count ;
1190 umode_t old_inode_mode;
1191 unsigned long savelink = 1;
1192 struct timespec ctime;
1194 /* three balancings: (1) old name removal, (2) new name insertion
1195 and (3) maybe "save" link insertion
1196 stat data updates: (1) old directory,
1197 (2) new directory and (3) maybe old object stat data (when it is
1198 directory) and (4) maybe stat data of object to which new entry
1199 pointed initially and (5) maybe block containing ".." of
1200 renamed directory
1201 quota updates: two parent directories */
1202 jbegin_count = JOURNAL_PER_BALANCE_CNT * 3 + 5 + 4 * REISERFS_QUOTA_TRANS_BLOCKS(old_dir->i_sb);
1204 old_inode = old_dentry->d_inode;
1205 new_dentry_inode = new_dentry->d_inode;
1207 // make sure, that oldname still exists and points to an object we
1208 // are going to rename
1209 old_de.de_gen_number_bit_string = NULL;
1210 reiserfs_write_lock(old_dir->i_sb);
1211 retval = reiserfs_find_entry (old_dir, old_dentry->d_name.name, old_dentry->d_name.len,
1212 &old_entry_path, &old_de);
1213 pathrelse (&old_entry_path);
1214 if (retval == IO_ERROR) {
1215 reiserfs_write_unlock(old_dir->i_sb);
1216 return -EIO;
1219 if (retval != NAME_FOUND || old_de.de_objectid != old_inode->i_ino) {
1220 reiserfs_write_unlock(old_dir->i_sb);
1221 return -ENOENT;
1224 old_inode_mode = old_inode->i_mode;
1225 if (S_ISDIR(old_inode_mode)) {
1226 // make sure, that directory being renamed has correct ".."
1227 // and that its new parent directory has not too many links
1228 // already
1230 if (new_dentry_inode) {
1231 if (!reiserfs_empty_dir(new_dentry_inode)) {
1232 reiserfs_write_unlock(old_dir->i_sb);
1233 return -ENOTEMPTY;
1237 /* directory is renamed, its parent directory will be changed,
1238 ** so find ".." entry
1240 dot_dot_de.de_gen_number_bit_string = NULL;
1241 retval = reiserfs_find_entry (old_inode, "..", 2, &dot_dot_entry_path, &dot_dot_de);
1242 pathrelse (&dot_dot_entry_path);
1243 if (retval != NAME_FOUND) {
1244 reiserfs_write_unlock(old_dir->i_sb);
1245 return -EIO;
1248 /* inode number of .. must equal old_dir->i_ino */
1249 if (dot_dot_de.de_objectid != old_dir->i_ino) {
1250 reiserfs_write_unlock(old_dir->i_sb);
1251 return -EIO;
1255 retval = journal_begin(&th, old_dir->i_sb, jbegin_count) ;
1256 if (retval) {
1257 reiserfs_write_unlock (old_dir->i_sb);
1258 return retval;
1261 /* add new entry (or find the existing one) */
1262 retval = reiserfs_add_entry (&th, new_dir, new_dentry->d_name.name, new_dentry->d_name.len,
1263 old_inode, 0);
1264 if (retval == -EEXIST) {
1265 if (!new_dentry_inode) {
1266 reiserfs_panic (old_dir->i_sb,
1267 "vs-7050: new entry is found, new inode == 0\n");
1269 } else if (retval) {
1270 int err = journal_end(&th, old_dir->i_sb, jbegin_count) ;
1271 reiserfs_write_unlock(old_dir->i_sb);
1272 return err ? err : retval;
1275 reiserfs_update_inode_transaction(old_dir) ;
1276 reiserfs_update_inode_transaction(new_dir) ;
1278 /* this makes it so an fsync on an open fd for the old name will
1279 ** commit the rename operation
1281 reiserfs_update_inode_transaction(old_inode) ;
1283 if (new_dentry_inode)
1284 reiserfs_update_inode_transaction(new_dentry_inode) ;
1286 while (1) {
1287 // look for old name using corresponding entry key (found by reiserfs_find_entry)
1288 if ((retval = search_by_entry_key (new_dir->i_sb, &old_de.de_entry_key,
1289 &old_entry_path, &old_de)) != NAME_FOUND) {
1290 pathrelse(&old_entry_path);
1291 journal_end(&th, old_dir->i_sb, jbegin_count);
1292 reiserfs_write_unlock(old_dir->i_sb);
1293 return -EIO;
1296 copy_item_head(&old_entry_ih, get_ih(&old_entry_path)) ;
1298 reiserfs_prepare_for_journal(old_inode->i_sb, old_de.de_bh, 1) ;
1300 // look for new name by reiserfs_find_entry
1301 new_de.de_gen_number_bit_string = NULL;
1302 retval = reiserfs_find_entry (new_dir, new_dentry->d_name.name, new_dentry->d_name.len,
1303 &new_entry_path, &new_de);
1304 // reiserfs_add_entry should not return IO_ERROR, because it is called with essentially same parameters from
1305 // reiserfs_add_entry above, and we'll catch any i/o errors before we get here.
1306 if (retval != NAME_FOUND_INVISIBLE && retval != NAME_FOUND) {
1307 pathrelse(&new_entry_path);
1308 pathrelse(&old_entry_path);
1309 journal_end(&th, old_dir->i_sb, jbegin_count);
1310 reiserfs_write_unlock(old_dir->i_sb);
1311 return -EIO;
1314 copy_item_head(&new_entry_ih, get_ih(&new_entry_path)) ;
1316 reiserfs_prepare_for_journal(old_inode->i_sb, new_de.de_bh, 1) ;
1318 if (S_ISDIR(old_inode->i_mode)) {
1319 if ((retval = search_by_entry_key (new_dir->i_sb, &dot_dot_de.de_entry_key,
1320 &dot_dot_entry_path, &dot_dot_de)) != NAME_FOUND) {
1321 pathrelse(&dot_dot_entry_path);
1322 pathrelse(&new_entry_path);
1323 pathrelse(&old_entry_path);
1324 journal_end(&th, old_dir->i_sb, jbegin_count);
1325 reiserfs_write_unlock(old_dir->i_sb);
1326 return -EIO;
1328 copy_item_head(&dot_dot_ih, get_ih(&dot_dot_entry_path)) ;
1329 // node containing ".." gets into transaction
1330 reiserfs_prepare_for_journal(old_inode->i_sb, dot_dot_de.de_bh, 1) ;
1332 /* we should check seals here, not do
1333 this stuff, yes? Then, having
1334 gathered everything into RAM we
1335 should lock the buffers, yes? -Hans */
1336 /* probably. our rename needs to hold more
1337 ** than one path at once. The seals would
1338 ** have to be written to deal with multi-path
1339 ** issues -chris
1341 /* sanity checking before doing the rename - avoid races many
1342 ** of the above checks could have scheduled. We have to be
1343 ** sure our items haven't been shifted by another process.
1345 if (item_moved(&new_entry_ih, &new_entry_path) ||
1346 !entry_points_to_object(new_dentry->d_name.name,
1347 new_dentry->d_name.len,
1348 &new_de, new_dentry_inode) ||
1349 item_moved(&old_entry_ih, &old_entry_path) ||
1350 !entry_points_to_object (old_dentry->d_name.name,
1351 old_dentry->d_name.len,
1352 &old_de, old_inode)) {
1353 reiserfs_restore_prepared_buffer (old_inode->i_sb, new_de.de_bh);
1354 reiserfs_restore_prepared_buffer (old_inode->i_sb, old_de.de_bh);
1355 if (S_ISDIR(old_inode_mode))
1356 reiserfs_restore_prepared_buffer (old_inode->i_sb, dot_dot_de.de_bh);
1357 continue;
1359 if (S_ISDIR(old_inode_mode)) {
1360 if ( item_moved(&dot_dot_ih, &dot_dot_entry_path) ||
1361 !entry_points_to_object ( "..", 2, &dot_dot_de, old_dir) ) {
1362 reiserfs_restore_prepared_buffer (old_inode->i_sb, old_de.de_bh);
1363 reiserfs_restore_prepared_buffer (old_inode->i_sb, new_de.de_bh);
1364 reiserfs_restore_prepared_buffer (old_inode->i_sb, dot_dot_de.de_bh);
1365 continue;
1369 RFALSE( S_ISDIR(old_inode_mode) &&
1370 !buffer_journal_prepared(dot_dot_de.de_bh), "" );
1372 break;
1375 /* ok, all the changes can be done in one fell swoop when we
1376 have claimed all the buffers needed.*/
1378 mark_de_visible (new_de.de_deh + new_de.de_entry_num);
1379 set_ino_in_dir_entry (&new_de, INODE_PKEY (old_inode));
1380 journal_mark_dirty (&th, old_dir->i_sb, new_de.de_bh);
1382 mark_de_hidden (old_de.de_deh + old_de.de_entry_num);
1383 journal_mark_dirty (&th, old_dir->i_sb, old_de.de_bh);
1384 ctime = CURRENT_TIME_SEC;
1385 old_dir->i_ctime = old_dir->i_mtime = ctime;
1386 new_dir->i_ctime = new_dir->i_mtime = ctime;
1387 /* thanks to Alex Adriaanse <alex_a@caltech.edu> for patch which adds ctime update of
1388 renamed object */
1389 old_inode->i_ctime = ctime;
1391 if (new_dentry_inode) {
1392 // adjust link number of the victim
1393 if (S_ISDIR(new_dentry_inode->i_mode)) {
1394 new_dentry_inode->i_nlink = 0;
1395 } else {
1396 new_dentry_inode->i_nlink--;
1398 new_dentry_inode->i_ctime = ctime;
1399 savelink = new_dentry_inode->i_nlink;
1402 if (S_ISDIR(old_inode_mode)) {
1403 // adjust ".." of renamed directory
1404 set_ino_in_dir_entry (&dot_dot_de, INODE_PKEY (new_dir));
1405 journal_mark_dirty (&th, new_dir->i_sb, dot_dot_de.de_bh);
1407 if (!new_dentry_inode)
1408 /* there (in new_dir) was no directory, so it got new link
1409 (".." of renamed directory) */
1410 INC_DIR_INODE_NLINK(new_dir);
1412 /* old directory lost one link - ".. " of renamed directory */
1413 DEC_DIR_INODE_NLINK(old_dir);
1416 // looks like in 2.3.99pre3 brelse is atomic. so we can use pathrelse
1417 pathrelse (&new_entry_path);
1418 pathrelse (&dot_dot_entry_path);
1420 // FIXME: this reiserfs_cut_from_item's return value may screw up
1421 // anybody, but it will panic if will not be able to find the
1422 // entry. This needs one more clean up
1423 if (reiserfs_cut_from_item (&th, &old_entry_path, &(old_de.de_entry_key), old_dir, NULL, 0) < 0)
1424 reiserfs_warning (old_dir->i_sb, "vs-7060: reiserfs_rename: couldn't not cut old name. Fsck later?");
1426 old_dir->i_size -= DEH_SIZE + old_de.de_entrylen;
1428 reiserfs_update_sd (&th, old_dir);
1429 reiserfs_update_sd (&th, new_dir);
1430 reiserfs_update_sd (&th, old_inode);
1432 if (new_dentry_inode) {
1433 if (savelink == 0)
1434 add_save_link (&th, new_dentry_inode, 0/* not truncate */);
1435 reiserfs_update_sd (&th, new_dentry_inode);
1438 retval = journal_end(&th, old_dir->i_sb, jbegin_count) ;
1439 reiserfs_write_unlock(old_dir->i_sb);
1440 return retval;
1444 * directories can handle most operations...
1446 struct inode_operations reiserfs_dir_inode_operations = {
1447 //&reiserfs_dir_operations, /* default_file_ops */
1448 .create = reiserfs_create,
1449 .lookup = reiserfs_lookup,
1450 .link = reiserfs_link,
1451 .unlink = reiserfs_unlink,
1452 .symlink = reiserfs_symlink,
1453 .mkdir = reiserfs_mkdir,
1454 .rmdir = reiserfs_rmdir,
1455 .mknod = reiserfs_mknod,
1456 .rename = reiserfs_rename,
1457 .setattr = reiserfs_setattr,
1458 .setxattr = reiserfs_setxattr,
1459 .getxattr = reiserfs_getxattr,
1460 .listxattr = reiserfs_listxattr,
1461 .removexattr = reiserfs_removexattr,
1462 .permission = reiserfs_permission,
1466 * symlink operations.. same as page_symlink_inode_operations, with xattr
1467 * stuff added
1469 struct inode_operations reiserfs_symlink_inode_operations = {
1470 .readlink = generic_readlink,
1471 .follow_link = page_follow_link_light,
1472 .put_link = page_put_link,
1473 .setattr = reiserfs_setattr,
1474 .setxattr = reiserfs_setxattr,
1475 .getxattr = reiserfs_getxattr,
1476 .listxattr = reiserfs_listxattr,
1477 .removexattr = reiserfs_removexattr,
1478 .permission = reiserfs_permission,
1484 * special file operations.. just xattr/acl stuff
1486 struct inode_operations reiserfs_special_inode_operations = {
1487 .setattr = reiserfs_setattr,
1488 .setxattr = reiserfs_setxattr,
1489 .getxattr = reiserfs_getxattr,
1490 .listxattr = reiserfs_listxattr,
1491 .removexattr = reiserfs_removexattr,
1492 .permission = reiserfs_permission,