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