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