2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
4 * Trivial changes by Alan Cox to remove EHASHCOLLISION for compatibility
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.
14 #include <linux/time.h>
15 #include <linux/bitops.h>
16 #include <linux/slab.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) set_nlink(i, 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;
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)) {
42 if (off > deh_offset(deh + j)) {
46 // this is not name found, but matched third key component
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)
120 retval = search_item(sb, key, path);
123 if (!PATH_LAST_POSITION(path)) {
124 reiserfs_error(sb, "vs-7000", "search_by_key "
125 "returned item position == 0");
129 PATH_LAST_POSITION(path)--;
139 reiserfs_error(sb, "vs-7002", "no path to here");
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);
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)
179 if (!len || (len == 1 && name[0] == '.'))
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);
189 // needed to have no names before "." and ".." those have hash
190 // value == 0 and generation conters 1 and 2 accordingly
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))
203 (de_visible(de->de_deh + de->de_entry_num) ? NAME_FOUND :
204 NAME_FOUND_INVISIBLE);
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;
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))) {
229 RFALSE(de->de_deh != B_I_DEH(de->de_bh, de->de_ih),
230 "vs-7010: array of entry headers not found");
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);
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
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,
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;
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),
299 search_by_entry_key(dir->i_sb, &key_to_search,
301 if (retval == IO_ERROR) {
302 reiserfs_error(dir->i_sb, "zam-7001", "io error");
306 /* compare names for all entries having given hash value */
308 linear_search_in_dir_item(&key_to_search, de, name,
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;
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);
324 static struct dentry *reiserfs_lookup(struct inode *dir, struct dentry *dentry,
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);
335 reiserfs_write_lock(dir->i_sb);
337 de.de_gen_number_bit_string = NULL;
339 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
340 &path_to_entry, &de);
341 pathrelse(&path_to_entry);
342 if (retval == NAME_FOUND) {
343 inode = reiserfs_iget(dir->i_sb,
344 (struct cpu_key *)&(de.de_dir_id));
345 if (!inode || IS_ERR(inode)) {
346 reiserfs_write_unlock(dir->i_sb);
347 return ERR_PTR(-EACCES);
350 /* Propagate the private flag so we know we're
351 * in the priv tree */
353 inode->i_flags |= S_PRIVATE;
355 reiserfs_write_unlock(dir->i_sb);
356 if (retval == IO_ERROR) {
357 return ERR_PTR(-EIO);
360 return d_splice_alias(inode, dentry);
364 ** looks up the dentry of the parent directory for child.
365 ** taken from ext2_get_parent
367 struct dentry *reiserfs_get_parent(struct dentry *child)
370 struct inode *inode = NULL;
371 struct reiserfs_dir_entry de;
372 INITIALIZE_PATH(path_to_entry);
373 struct inode *dir = child->d_inode;
375 if (dir->i_nlink == 0) {
376 return ERR_PTR(-ENOENT);
378 de.de_gen_number_bit_string = NULL;
380 reiserfs_write_lock(dir->i_sb);
381 retval = reiserfs_find_entry(dir, "..", 2, &path_to_entry, &de);
382 pathrelse(&path_to_entry);
383 if (retval != NAME_FOUND) {
384 reiserfs_write_unlock(dir->i_sb);
385 return ERR_PTR(-ENOENT);
387 inode = reiserfs_iget(dir->i_sb, (struct cpu_key *)&(de.de_dir_id));
388 reiserfs_write_unlock(dir->i_sb);
390 return d_obtain_alias(inode);
393 /* add entry to the directory (entry can be hidden).
395 insert definition of when hidden directories are used here -Hans
397 Does not mark dir inode dirty, do it after successesfull call to it */
399 static int reiserfs_add_entry(struct reiserfs_transaction_handle *th,
400 struct inode *dir, const char *name, int namelen,
401 struct inode *inode, int visible)
403 struct cpu_key entry_key;
404 struct reiserfs_de_head *deh;
405 INITIALIZE_PATH(path);
406 struct reiserfs_dir_entry de;
407 DECLARE_BITMAP(bit_string, MAX_GENERATION_NUMBER + 1);
409 char small_buf[32 + DEH_SIZE]; /* 48 bytes now and we avoid kmalloc
410 if we create file with short name */
412 int buflen, paste_size;
415 BUG_ON(!th->t_trans_id);
417 /* cannot allow items to be added into a busy deleted directory */
421 if (namelen > REISERFS_MAX_NAME(dir->i_sb->s_blocksize))
422 return -ENAMETOOLONG;
424 /* each entry has unique key. compose it */
425 make_cpu_key(&entry_key, dir,
426 get_third_component(dir->i_sb, name, namelen),
429 /* get memory for composing the entry */
430 buflen = DEH_SIZE + ROUND_UP(namelen);
431 if (buflen > sizeof(small_buf)) {
432 buffer = kmalloc(buflen, GFP_NOFS);
439 (get_inode_sd_version(dir) ==
440 STAT_DATA_V1) ? (DEH_SIZE + namelen) : buflen;
442 /* fill buffer : directory entry head, name[, dir objectid | , stat data | ,stat data, dir objectid ] */
443 deh = (struct reiserfs_de_head *)buffer;
444 deh->deh_location = 0; /* JDM Endian safe if 0 */
445 put_deh_offset(deh, cpu_key_k_offset(&entry_key));
446 deh->deh_state = 0; /* JDM Endian safe if 0 */
447 /* put key (ino analog) to de */
448 deh->deh_dir_id = INODE_PKEY(inode)->k_dir_id; /* safe: k_dir_id is le */
449 deh->deh_objectid = INODE_PKEY(inode)->k_objectid; /* safe: k_objectid is le */
452 memcpy((char *)(deh + 1), name, namelen);
453 /* padd by 0s to the 4 byte boundary */
454 padd_item((char *)(deh + 1), ROUND_UP(namelen), namelen);
456 /* entry is ready to be pasted into tree, set 'visibility' and 'stat data in entry' attributes */
457 mark_de_without_sd(deh);
458 visible ? mark_de_visible(deh) : mark_de_hidden(deh);
460 /* find the proper place for the new entry */
461 memset(bit_string, 0, sizeof(bit_string));
462 de.de_gen_number_bit_string = bit_string;
463 retval = reiserfs_find_entry(dir, name, namelen, &path, &de);
464 if (retval != NAME_NOT_FOUND) {
465 if (buffer != small_buf)
469 if (retval == IO_ERROR) {
473 if (retval != NAME_FOUND) {
474 reiserfs_error(dir->i_sb, "zam-7002",
475 "reiserfs_find_entry() returned "
476 "unexpected value (%d)", retval);
483 find_first_zero_bit(bit_string,
484 MAX_GENERATION_NUMBER + 1);
485 if (gen_number > MAX_GENERATION_NUMBER) {
486 /* there is no free generation number */
487 reiserfs_warning(dir->i_sb, "reiserfs-7010",
488 "Congratulations! we have got hash function "
490 if (buffer != small_buf)
495 /* adjust offset of directory enrty */
496 put_deh_offset(deh, SET_GENERATION_NUMBER(deh_offset(deh), gen_number));
497 set_cpu_key_k_offset(&entry_key, deh_offset(deh));
499 /* update max-hash-collisions counter in reiserfs_sb_info */
500 PROC_INFO_MAX(th->t_super, max_hash_collisions, gen_number);
502 if (gen_number != 0) { /* we need to re-search for the insertion point */
503 if (search_by_entry_key(dir->i_sb, &entry_key, &path, &de) !=
505 reiserfs_warning(dir->i_sb, "vs-7032",
506 "entry with this key (%K) already "
507 "exists", &entry_key);
509 if (buffer != small_buf)
516 /* perform the insertion of the entry that we have prepared */
518 reiserfs_paste_into_item(th, &path, &entry_key, dir, buffer,
520 if (buffer != small_buf)
523 reiserfs_check_path(&path);
527 dir->i_size += paste_size;
528 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
529 if (!S_ISDIR(inode->i_mode) && visible)
530 // reiserfs_mkdir or reiserfs_rename will do that by itself
531 reiserfs_update_sd(th, dir);
533 reiserfs_check_path(&path);
537 /* quota utility function, call if you've had to abort after calling
538 ** new_inode_init, and have not called reiserfs_new_inode yet.
539 ** This should only be called on inodes that do not have stat data
540 ** inserted into the tree yet.
542 static int drop_new_inode(struct inode *inode)
545 make_bad_inode(inode);
546 inode->i_flags |= S_NOQUOTA;
551 /* utility function that does setup for reiserfs_new_inode.
552 ** dquot_initialize needs lots of credits so it's better to have it
553 ** outside of a transaction, so we had to pull some bits of
554 ** reiserfs_new_inode out into this func.
556 static int new_inode_init(struct inode *inode, struct inode *dir, umode_t mode)
558 /* Make inode invalid - just in case we are going to drop it before
559 * the initialization happens */
560 INODE_PKEY(inode)->k_objectid = 0;
561 /* the quota init calls have to know who to charge the quota to, so
562 ** we have to set uid and gid here
564 inode_init_owner(inode, dir, mode);
565 dquot_initialize(inode);
569 static int reiserfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
574 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
576 JOURNAL_PER_BALANCE_CNT * 2 +
577 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
578 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
579 struct reiserfs_transaction_handle th;
580 struct reiserfs_security_handle security;
582 dquot_initialize(dir);
584 if (!(inode = new_inode(dir->i_sb))) {
587 new_inode_init(inode, dir, mode);
589 jbegin_count += reiserfs_cache_default_acl(dir);
590 retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security);
592 drop_new_inode(inode);
595 jbegin_count += retval;
596 reiserfs_write_lock(dir->i_sb);
598 retval = journal_begin(&th, dir->i_sb, jbegin_count);
600 drop_new_inode(inode);
605 reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
610 inode->i_op = &reiserfs_file_inode_operations;
611 inode->i_fop = &reiserfs_file_operations;
612 inode->i_mapping->a_ops = &reiserfs_address_space_operations;
615 reiserfs_add_entry(&th, dir, dentry->d_name.name,
616 dentry->d_name.len, inode, 1 /*visible */ );
620 reiserfs_update_sd(&th, inode);
621 err = journal_end(&th, dir->i_sb, jbegin_count);
624 unlock_new_inode(inode);
628 reiserfs_update_inode_transaction(inode);
629 reiserfs_update_inode_transaction(dir);
631 unlock_new_inode(inode);
632 d_instantiate(dentry, inode);
633 retval = journal_end(&th, dir->i_sb, jbegin_count);
636 reiserfs_write_unlock(dir->i_sb);
640 static int reiserfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
645 struct reiserfs_transaction_handle th;
646 struct reiserfs_security_handle security;
647 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
649 JOURNAL_PER_BALANCE_CNT * 3 +
650 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
651 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
653 if (!new_valid_dev(rdev))
656 dquot_initialize(dir);
658 if (!(inode = new_inode(dir->i_sb))) {
661 new_inode_init(inode, dir, mode);
663 jbegin_count += reiserfs_cache_default_acl(dir);
664 retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security);
666 drop_new_inode(inode);
669 jbegin_count += retval;
670 reiserfs_write_lock(dir->i_sb);
672 retval = journal_begin(&th, dir->i_sb, jbegin_count);
674 drop_new_inode(inode);
679 reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
685 inode->i_op = &reiserfs_special_inode_operations;
686 init_special_inode(inode, inode->i_mode, rdev);
688 //FIXME: needed for block and char devices only
689 reiserfs_update_sd(&th, inode);
691 reiserfs_update_inode_transaction(inode);
692 reiserfs_update_inode_transaction(dir);
695 reiserfs_add_entry(&th, dir, dentry->d_name.name,
696 dentry->d_name.len, inode, 1 /*visible */ );
700 reiserfs_update_sd(&th, inode);
701 err = journal_end(&th, dir->i_sb, jbegin_count);
704 unlock_new_inode(inode);
709 unlock_new_inode(inode);
710 d_instantiate(dentry, inode);
711 retval = journal_end(&th, dir->i_sb, jbegin_count);
714 reiserfs_write_unlock(dir->i_sb);
718 static int reiserfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
722 struct reiserfs_transaction_handle th;
723 struct reiserfs_security_handle security;
724 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
726 JOURNAL_PER_BALANCE_CNT * 3 +
727 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
728 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
730 dquot_initialize(dir);
732 #ifdef DISPLACE_NEW_PACKING_LOCALITIES
733 /* set flag that new packing locality created and new blocks for the content * of that directory are not displaced yet */
734 REISERFS_I(dir)->new_packing_locality = 1;
736 mode = S_IFDIR | mode;
737 if (!(inode = new_inode(dir->i_sb))) {
740 new_inode_init(inode, dir, mode);
742 jbegin_count += reiserfs_cache_default_acl(dir);
743 retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security);
745 drop_new_inode(inode);
748 jbegin_count += retval;
749 reiserfs_write_lock(dir->i_sb);
751 retval = journal_begin(&th, dir->i_sb, jbegin_count);
753 drop_new_inode(inode);
757 /* inc the link count now, so another writer doesn't overflow it while
758 ** we sleep later on.
760 INC_DIR_INODE_NLINK(dir)
762 retval = reiserfs_new_inode(&th, dir, mode, NULL /*symlink */ ,
763 old_format_only(dir->i_sb) ?
764 EMPTY_DIR_SIZE_V1 : EMPTY_DIR_SIZE,
765 dentry, inode, &security);
767 DEC_DIR_INODE_NLINK(dir)
771 reiserfs_update_inode_transaction(inode);
772 reiserfs_update_inode_transaction(dir);
774 inode->i_op = &reiserfs_dir_inode_operations;
775 inode->i_fop = &reiserfs_dir_operations;
777 // note, _this_ add_entry will not update dir's stat data
779 reiserfs_add_entry(&th, dir, dentry->d_name.name,
780 dentry->d_name.len, inode, 1 /*visible */ );
784 DEC_DIR_INODE_NLINK(dir);
785 reiserfs_update_sd(&th, inode);
786 err = journal_end(&th, dir->i_sb, jbegin_count);
789 unlock_new_inode(inode);
793 // the above add_entry did not update dir's stat data
794 reiserfs_update_sd(&th, dir);
796 unlock_new_inode(inode);
797 d_instantiate(dentry, inode);
798 retval = journal_end(&th, dir->i_sb, jbegin_count);
800 reiserfs_write_unlock(dir->i_sb);
804 static inline int reiserfs_empty_dir(struct inode *inode)
806 /* we can cheat because an old format dir cannot have
807 ** EMPTY_DIR_SIZE, and a new format dir cannot have
808 ** EMPTY_DIR_SIZE_V1. So, if the inode is either size,
809 ** regardless of disk format version, the directory is empty.
811 if (inode->i_size != EMPTY_DIR_SIZE &&
812 inode->i_size != EMPTY_DIR_SIZE_V1) {
818 static int reiserfs_rmdir(struct inode *dir, struct dentry *dentry)
822 struct reiserfs_transaction_handle th;
824 INITIALIZE_PATH(path);
825 struct reiserfs_dir_entry de;
827 /* we will be doing 2 balancings and update 2 stat data, we change quotas
828 * of the owner of the directory and of the owner of the parent directory.
829 * The quota structure is possibly deleted only on last iput => outside
830 * of this transaction */
832 JOURNAL_PER_BALANCE_CNT * 2 + 2 +
833 4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
835 dquot_initialize(dir);
837 reiserfs_write_lock(dir->i_sb);
838 retval = journal_begin(&th, dir->i_sb, jbegin_count);
842 de.de_gen_number_bit_string = NULL;
844 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
845 &path, &de)) == NAME_NOT_FOUND) {
848 } else if (retval == IO_ERROR) {
853 inode = dentry->d_inode;
855 reiserfs_update_inode_transaction(inode);
856 reiserfs_update_inode_transaction(dir);
858 if (de.de_objectid != inode->i_ino) {
859 // FIXME: compare key of an object and a key found in the
864 if (!reiserfs_empty_dir(inode)) {
869 /* cut entry from dir directory */
870 retval = reiserfs_cut_from_item(&th, &path, &(de.de_entry_key), dir, NULL, /* page */
871 0 /*new file size - not used here */ );
875 if (inode->i_nlink != 2 && inode->i_nlink != 1)
876 reiserfs_error(inode->i_sb, "reiserfs-7040",
877 "empty directory has nlink != 2 (%d)",
881 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
882 reiserfs_update_sd(&th, inode);
884 DEC_DIR_INODE_NLINK(dir)
885 dir->i_size -= (DEH_SIZE + de.de_entrylen);
886 reiserfs_update_sd(&th, dir);
888 /* prevent empty directory from getting lost */
889 add_save_link(&th, inode, 0 /* not truncate */ );
891 retval = journal_end(&th, dir->i_sb, jbegin_count);
892 reiserfs_check_path(&path);
894 reiserfs_write_unlock(dir->i_sb);
898 /* we must release path, because we did not call
899 reiserfs_cut_from_item, or reiserfs_cut_from_item does not
900 release path if operation was not complete */
902 err = journal_end(&th, dir->i_sb, jbegin_count);
903 reiserfs_write_unlock(dir->i_sb);
904 return err ? err : retval;
907 static int reiserfs_unlink(struct inode *dir, struct dentry *dentry)
911 struct reiserfs_dir_entry de;
912 INITIALIZE_PATH(path);
913 struct reiserfs_transaction_handle th;
915 unsigned long savelink;
917 dquot_initialize(dir);
919 inode = dentry->d_inode;
921 /* in this transaction we can be doing at max two balancings and update
922 * two stat datas, we change quotas of the owner of the directory and of
923 * the owner of the parent directory. The quota structure is possibly
924 * deleted only on iput => outside of this transaction */
926 JOURNAL_PER_BALANCE_CNT * 2 + 2 +
927 4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
929 reiserfs_write_lock(dir->i_sb);
930 retval = journal_begin(&th, dir->i_sb, jbegin_count);
934 de.de_gen_number_bit_string = NULL;
936 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
937 &path, &de)) == NAME_NOT_FOUND) {
940 } else if (retval == IO_ERROR) {
945 reiserfs_update_inode_transaction(inode);
946 reiserfs_update_inode_transaction(dir);
948 if (de.de_objectid != inode->i_ino) {
949 // FIXME: compare key of an object and a key found in the
955 if (!inode->i_nlink) {
956 reiserfs_warning(inode->i_sb, "reiserfs-7042",
957 "deleting nonexistent file (%lu), %d",
958 inode->i_ino, inode->i_nlink);
965 * we schedule before doing the add_save_link call, save the link
966 * count so we don't race
968 savelink = inode->i_nlink;
971 reiserfs_cut_from_item(&th, &path, &(de.de_entry_key), dir, NULL,
977 inode->i_ctime = CURRENT_TIME_SEC;
978 reiserfs_update_sd(&th, inode);
980 dir->i_size -= (de.de_entrylen + DEH_SIZE);
981 dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
982 reiserfs_update_sd(&th, dir);
985 /* prevent file from getting lost */
986 add_save_link(&th, inode, 0 /* not truncate */ );
988 retval = journal_end(&th, dir->i_sb, jbegin_count);
989 reiserfs_check_path(&path);
990 reiserfs_write_unlock(dir->i_sb);
995 err = journal_end(&th, dir->i_sb, jbegin_count);
996 reiserfs_check_path(&path);
1000 reiserfs_write_unlock(dir->i_sb);
1004 static int reiserfs_symlink(struct inode *parent_dir,
1005 struct dentry *dentry, const char *symname)
1008 struct inode *inode;
1011 struct reiserfs_transaction_handle th;
1012 struct reiserfs_security_handle security;
1013 int mode = S_IFLNK | S_IRWXUGO;
1014 /* We need blocks for transaction + (user+group)*(quotas for new inode + update of quota for directory owner) */
1016 JOURNAL_PER_BALANCE_CNT * 3 +
1017 2 * (REISERFS_QUOTA_INIT_BLOCKS(parent_dir->i_sb) +
1018 REISERFS_QUOTA_TRANS_BLOCKS(parent_dir->i_sb));
1020 dquot_initialize(parent_dir);
1022 if (!(inode = new_inode(parent_dir->i_sb))) {
1025 new_inode_init(inode, parent_dir, mode);
1027 retval = reiserfs_security_init(parent_dir, inode, &dentry->d_name,
1030 drop_new_inode(inode);
1033 jbegin_count += retval;
1035 reiserfs_write_lock(parent_dir->i_sb);
1036 item_len = ROUND_UP(strlen(symname));
1037 if (item_len > MAX_DIRECT_ITEM_LEN(parent_dir->i_sb->s_blocksize)) {
1038 retval = -ENAMETOOLONG;
1039 drop_new_inode(inode);
1043 name = kmalloc(item_len, GFP_NOFS);
1045 drop_new_inode(inode);
1049 memcpy(name, symname, strlen(symname));
1050 padd_item(name, item_len, strlen(symname));
1052 retval = journal_begin(&th, parent_dir->i_sb, jbegin_count);
1054 drop_new_inode(inode);
1060 reiserfs_new_inode(&th, parent_dir, mode, name, strlen(symname),
1061 dentry, inode, &security);
1063 if (retval) { /* reiserfs_new_inode iputs for us */
1067 reiserfs_update_inode_transaction(inode);
1068 reiserfs_update_inode_transaction(parent_dir);
1070 inode->i_op = &reiserfs_symlink_inode_operations;
1071 inode->i_mapping->a_ops = &reiserfs_address_space_operations;
1073 // must be sure this inode is written with this transaction
1075 //reiserfs_update_sd (&th, inode, READ_BLOCKS);
1077 retval = reiserfs_add_entry(&th, parent_dir, dentry->d_name.name,
1078 dentry->d_name.len, inode, 1 /*visible */ );
1082 reiserfs_update_sd(&th, inode);
1083 err = journal_end(&th, parent_dir->i_sb, jbegin_count);
1086 unlock_new_inode(inode);
1091 unlock_new_inode(inode);
1092 d_instantiate(dentry, inode);
1093 retval = journal_end(&th, parent_dir->i_sb, jbegin_count);
1095 reiserfs_write_unlock(parent_dir->i_sb);
1099 static int reiserfs_link(struct dentry *old_dentry, struct inode *dir,
1100 struct dentry *dentry)
1103 struct inode *inode = old_dentry->d_inode;
1104 struct reiserfs_transaction_handle th;
1105 /* We need blocks for transaction + update of quotas for the owners of the directory */
1107 JOURNAL_PER_BALANCE_CNT * 3 +
1108 2 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
1110 dquot_initialize(dir);
1112 reiserfs_write_lock(dir->i_sb);
1113 if (inode->i_nlink >= REISERFS_LINK_MAX) {
1114 //FIXME: sd_nlink is 32 bit for new files
1115 reiserfs_write_unlock(dir->i_sb);
1119 /* inc before scheduling so reiserfs_unlink knows we are here */
1122 retval = journal_begin(&th, dir->i_sb, jbegin_count);
1125 reiserfs_write_unlock(dir->i_sb);
1129 /* create new entry */
1131 reiserfs_add_entry(&th, dir, dentry->d_name.name,
1132 dentry->d_name.len, inode, 1 /*visible */ );
1134 reiserfs_update_inode_transaction(inode);
1135 reiserfs_update_inode_transaction(dir);
1140 err = journal_end(&th, dir->i_sb, jbegin_count);
1141 reiserfs_write_unlock(dir->i_sb);
1142 return err ? err : retval;
1145 inode->i_ctime = CURRENT_TIME_SEC;
1146 reiserfs_update_sd(&th, inode);
1149 d_instantiate(dentry, inode);
1150 retval = journal_end(&th, dir->i_sb, jbegin_count);
1151 reiserfs_write_unlock(dir->i_sb);
1155 /* de contains information pointing to an entry which */
1156 static int de_still_valid(const char *name, int len,
1157 struct reiserfs_dir_entry *de)
1159 struct reiserfs_dir_entry tmp = *de;
1161 // recalculate pointer to name and name length
1162 set_de_name_and_namelen(&tmp);
1163 // FIXME: could check more
1164 if (tmp.de_namelen != len || memcmp(name, de->de_name, len))
1169 static int entry_points_to_object(const char *name, int len,
1170 struct reiserfs_dir_entry *de,
1171 struct inode *inode)
1173 if (!de_still_valid(name, len, de))
1177 if (!de_visible(de->de_deh + de->de_entry_num))
1178 reiserfs_panic(inode->i_sb, "vs-7042",
1179 "entry must be visible");
1180 return (de->de_objectid == inode->i_ino) ? 1 : 0;
1183 /* this must be added hidden entry */
1184 if (de_visible(de->de_deh + de->de_entry_num))
1185 reiserfs_panic(NULL, "vs-7043", "entry must be visible");
1190 /* sets key of objectid the entry has to point to */
1191 static void set_ino_in_dir_entry(struct reiserfs_dir_entry *de,
1192 struct reiserfs_key *key)
1194 /* JDM These operations are endian safe - both are le */
1195 de->de_deh[de->de_entry_num].deh_dir_id = key->k_dir_id;
1196 de->de_deh[de->de_entry_num].deh_objectid = key->k_objectid;
1200 * process, that is going to call fix_nodes/do_balance must hold only
1201 * one path. If it holds 2 or more, it can get into endless waiting in
1202 * get_empty_nodes or its clones
1204 static int reiserfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1205 struct inode *new_dir, struct dentry *new_dentry)
1208 INITIALIZE_PATH(old_entry_path);
1209 INITIALIZE_PATH(new_entry_path);
1210 INITIALIZE_PATH(dot_dot_entry_path);
1211 struct item_head new_entry_ih, old_entry_ih, dot_dot_ih;
1212 struct reiserfs_dir_entry old_de, new_de, dot_dot_de;
1213 struct inode *old_inode, *new_dentry_inode;
1214 struct reiserfs_transaction_handle th;
1216 umode_t old_inode_mode;
1217 unsigned long savelink = 1;
1218 struct timespec ctime;
1220 /* three balancings: (1) old name removal, (2) new name insertion
1221 and (3) maybe "save" link insertion
1222 stat data updates: (1) old directory,
1223 (2) new directory and (3) maybe old object stat data (when it is
1224 directory) and (4) maybe stat data of object to which new entry
1225 pointed initially and (5) maybe block containing ".." of
1227 quota updates: two parent directories */
1229 JOURNAL_PER_BALANCE_CNT * 3 + 5 +
1230 4 * REISERFS_QUOTA_TRANS_BLOCKS(old_dir->i_sb);
1232 dquot_initialize(old_dir);
1233 dquot_initialize(new_dir);
1235 old_inode = old_dentry->d_inode;
1236 new_dentry_inode = new_dentry->d_inode;
1238 // make sure, that oldname still exists and points to an object we
1239 // are going to rename
1240 old_de.de_gen_number_bit_string = NULL;
1241 reiserfs_write_lock(old_dir->i_sb);
1243 reiserfs_find_entry(old_dir, old_dentry->d_name.name,
1244 old_dentry->d_name.len, &old_entry_path,
1246 pathrelse(&old_entry_path);
1247 if (retval == IO_ERROR) {
1248 reiserfs_write_unlock(old_dir->i_sb);
1252 if (retval != NAME_FOUND || old_de.de_objectid != old_inode->i_ino) {
1253 reiserfs_write_unlock(old_dir->i_sb);
1257 old_inode_mode = old_inode->i_mode;
1258 if (S_ISDIR(old_inode_mode)) {
1259 // make sure, that directory being renamed has correct ".."
1260 // and that its new parent directory has not too many links
1263 if (new_dentry_inode) {
1264 if (!reiserfs_empty_dir(new_dentry_inode)) {
1265 reiserfs_write_unlock(old_dir->i_sb);
1270 /* directory is renamed, its parent directory will be changed,
1271 ** so find ".." entry
1273 dot_dot_de.de_gen_number_bit_string = NULL;
1275 reiserfs_find_entry(old_inode, "..", 2, &dot_dot_entry_path,
1277 pathrelse(&dot_dot_entry_path);
1278 if (retval != NAME_FOUND) {
1279 reiserfs_write_unlock(old_dir->i_sb);
1283 /* inode number of .. must equal old_dir->i_ino */
1284 if (dot_dot_de.de_objectid != old_dir->i_ino) {
1285 reiserfs_write_unlock(old_dir->i_sb);
1290 retval = journal_begin(&th, old_dir->i_sb, jbegin_count);
1292 reiserfs_write_unlock(old_dir->i_sb);
1296 /* add new entry (or find the existing one) */
1298 reiserfs_add_entry(&th, new_dir, new_dentry->d_name.name,
1299 new_dentry->d_name.len, old_inode, 0);
1300 if (retval == -EEXIST) {
1301 if (!new_dentry_inode) {
1302 reiserfs_panic(old_dir->i_sb, "vs-7050",
1303 "new entry is found, new inode == 0");
1305 } else if (retval) {
1306 int err = journal_end(&th, old_dir->i_sb, jbegin_count);
1307 reiserfs_write_unlock(old_dir->i_sb);
1308 return err ? err : retval;
1311 reiserfs_update_inode_transaction(old_dir);
1312 reiserfs_update_inode_transaction(new_dir);
1314 /* this makes it so an fsync on an open fd for the old name will
1315 ** commit the rename operation
1317 reiserfs_update_inode_transaction(old_inode);
1319 if (new_dentry_inode)
1320 reiserfs_update_inode_transaction(new_dentry_inode);
1323 // look for old name using corresponding entry key (found by reiserfs_find_entry)
1325 search_by_entry_key(new_dir->i_sb, &old_de.de_entry_key,
1327 &old_de)) != NAME_FOUND) {
1328 pathrelse(&old_entry_path);
1329 journal_end(&th, old_dir->i_sb, jbegin_count);
1330 reiserfs_write_unlock(old_dir->i_sb);
1334 copy_item_head(&old_entry_ih, get_ih(&old_entry_path));
1336 reiserfs_prepare_for_journal(old_inode->i_sb, old_de.de_bh, 1);
1338 // look for new name by reiserfs_find_entry
1339 new_de.de_gen_number_bit_string = NULL;
1341 reiserfs_find_entry(new_dir, new_dentry->d_name.name,
1342 new_dentry->d_name.len, &new_entry_path,
1344 // reiserfs_add_entry should not return IO_ERROR, because it is called with essentially same parameters from
1345 // reiserfs_add_entry above, and we'll catch any i/o errors before we get here.
1346 if (retval != NAME_FOUND_INVISIBLE && retval != NAME_FOUND) {
1347 pathrelse(&new_entry_path);
1348 pathrelse(&old_entry_path);
1349 journal_end(&th, old_dir->i_sb, jbegin_count);
1350 reiserfs_write_unlock(old_dir->i_sb);
1354 copy_item_head(&new_entry_ih, get_ih(&new_entry_path));
1356 reiserfs_prepare_for_journal(old_inode->i_sb, new_de.de_bh, 1);
1358 if (S_ISDIR(old_inode->i_mode)) {
1360 search_by_entry_key(new_dir->i_sb,
1361 &dot_dot_de.de_entry_key,
1362 &dot_dot_entry_path,
1363 &dot_dot_de)) != NAME_FOUND) {
1364 pathrelse(&dot_dot_entry_path);
1365 pathrelse(&new_entry_path);
1366 pathrelse(&old_entry_path);
1367 journal_end(&th, old_dir->i_sb, jbegin_count);
1368 reiserfs_write_unlock(old_dir->i_sb);
1371 copy_item_head(&dot_dot_ih,
1372 get_ih(&dot_dot_entry_path));
1373 // node containing ".." gets into transaction
1374 reiserfs_prepare_for_journal(old_inode->i_sb,
1375 dot_dot_de.de_bh, 1);
1377 /* we should check seals here, not do
1378 this stuff, yes? Then, having
1379 gathered everything into RAM we
1380 should lock the buffers, yes? -Hans */
1381 /* probably. our rename needs to hold more
1382 ** than one path at once. The seals would
1383 ** have to be written to deal with multi-path
1386 /* sanity checking before doing the rename - avoid races many
1387 ** of the above checks could have scheduled. We have to be
1388 ** sure our items haven't been shifted by another process.
1390 if (item_moved(&new_entry_ih, &new_entry_path) ||
1391 !entry_points_to_object(new_dentry->d_name.name,
1392 new_dentry->d_name.len,
1393 &new_de, new_dentry_inode) ||
1394 item_moved(&old_entry_ih, &old_entry_path) ||
1395 !entry_points_to_object(old_dentry->d_name.name,
1396 old_dentry->d_name.len,
1397 &old_de, old_inode)) {
1398 reiserfs_restore_prepared_buffer(old_inode->i_sb,
1400 reiserfs_restore_prepared_buffer(old_inode->i_sb,
1402 if (S_ISDIR(old_inode_mode))
1403 reiserfs_restore_prepared_buffer(old_inode->
1409 if (S_ISDIR(old_inode_mode)) {
1410 if (item_moved(&dot_dot_ih, &dot_dot_entry_path) ||
1411 !entry_points_to_object("..", 2, &dot_dot_de,
1413 reiserfs_restore_prepared_buffer(old_inode->
1416 reiserfs_restore_prepared_buffer(old_inode->
1419 reiserfs_restore_prepared_buffer(old_inode->
1427 RFALSE(S_ISDIR(old_inode_mode) &&
1428 !buffer_journal_prepared(dot_dot_de.de_bh), "");
1433 /* ok, all the changes can be done in one fell swoop when we
1434 have claimed all the buffers needed. */
1436 mark_de_visible(new_de.de_deh + new_de.de_entry_num);
1437 set_ino_in_dir_entry(&new_de, INODE_PKEY(old_inode));
1438 journal_mark_dirty(&th, old_dir->i_sb, new_de.de_bh);
1440 mark_de_hidden(old_de.de_deh + old_de.de_entry_num);
1441 journal_mark_dirty(&th, old_dir->i_sb, old_de.de_bh);
1442 ctime = CURRENT_TIME_SEC;
1443 old_dir->i_ctime = old_dir->i_mtime = ctime;
1444 new_dir->i_ctime = new_dir->i_mtime = ctime;
1445 /* thanks to Alex Adriaanse <alex_a@caltech.edu> for patch which adds ctime update of
1447 old_inode->i_ctime = ctime;
1449 if (new_dentry_inode) {
1450 // adjust link number of the victim
1451 if (S_ISDIR(new_dentry_inode->i_mode)) {
1452 clear_nlink(new_dentry_inode);
1454 drop_nlink(new_dentry_inode);
1456 new_dentry_inode->i_ctime = ctime;
1457 savelink = new_dentry_inode->i_nlink;
1460 if (S_ISDIR(old_inode_mode)) {
1461 /* adjust ".." of renamed directory */
1462 set_ino_in_dir_entry(&dot_dot_de, INODE_PKEY(new_dir));
1463 journal_mark_dirty(&th, new_dir->i_sb, dot_dot_de.de_bh);
1465 if (!new_dentry_inode)
1466 /* there (in new_dir) was no directory, so it got new link
1467 (".." of renamed directory) */
1468 INC_DIR_INODE_NLINK(new_dir);
1470 /* old directory lost one link - ".. " of renamed directory */
1471 DEC_DIR_INODE_NLINK(old_dir);
1473 // looks like in 2.3.99pre3 brelse is atomic. so we can use pathrelse
1474 pathrelse(&new_entry_path);
1475 pathrelse(&dot_dot_entry_path);
1477 // FIXME: this reiserfs_cut_from_item's return value may screw up
1478 // anybody, but it will panic if will not be able to find the
1479 // entry. This needs one more clean up
1480 if (reiserfs_cut_from_item
1481 (&th, &old_entry_path, &(old_de.de_entry_key), old_dir, NULL,
1483 reiserfs_error(old_dir->i_sb, "vs-7060",
1484 "couldn't not cut old name. Fsck later?");
1486 old_dir->i_size -= DEH_SIZE + old_de.de_entrylen;
1488 reiserfs_update_sd(&th, old_dir);
1489 reiserfs_update_sd(&th, new_dir);
1490 reiserfs_update_sd(&th, old_inode);
1492 if (new_dentry_inode) {
1494 add_save_link(&th, new_dentry_inode,
1495 0 /* not truncate */ );
1496 reiserfs_update_sd(&th, new_dentry_inode);
1499 retval = journal_end(&th, old_dir->i_sb, jbegin_count);
1500 reiserfs_write_unlock(old_dir->i_sb);
1505 * directories can handle most operations...
1507 const struct inode_operations reiserfs_dir_inode_operations = {
1508 //&reiserfs_dir_operations, /* default_file_ops */
1509 .create = reiserfs_create,
1510 .lookup = reiserfs_lookup,
1511 .link = reiserfs_link,
1512 .unlink = reiserfs_unlink,
1513 .symlink = reiserfs_symlink,
1514 .mkdir = reiserfs_mkdir,
1515 .rmdir = reiserfs_rmdir,
1516 .mknod = reiserfs_mknod,
1517 .rename = reiserfs_rename,
1518 .setattr = reiserfs_setattr,
1519 .setxattr = reiserfs_setxattr,
1520 .getxattr = reiserfs_getxattr,
1521 .listxattr = reiserfs_listxattr,
1522 .removexattr = reiserfs_removexattr,
1523 .permission = reiserfs_permission,
1524 .get_acl = reiserfs_get_acl,
1528 * symlink operations.. same as page_symlink_inode_operations, with xattr
1531 const struct inode_operations reiserfs_symlink_inode_operations = {
1532 .readlink = generic_readlink,
1533 .follow_link = page_follow_link_light,
1534 .put_link = page_put_link,
1535 .setattr = reiserfs_setattr,
1536 .setxattr = reiserfs_setxattr,
1537 .getxattr = reiserfs_getxattr,
1538 .listxattr = reiserfs_listxattr,
1539 .removexattr = reiserfs_removexattr,
1540 .permission = reiserfs_permission,
1541 .get_acl = reiserfs_get_acl,
1546 * special file operations.. just xattr/acl stuff
1548 const struct inode_operations reiserfs_special_inode_operations = {
1549 .setattr = reiserfs_setattr,
1550 .setxattr = reiserfs_setxattr,
1551 .getxattr = reiserfs_getxattr,
1552 .listxattr = reiserfs_listxattr,
1553 .removexattr = reiserfs_removexattr,
1554 .permission = reiserfs_permission,
1555 .get_acl = reiserfs_get_acl,