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);
26 * directory item contains array of entry headers. This performs
27 * binary search through that array
29 static int bin_search_in_dir_item(struct reiserfs_dir_entry *de, loff_t off)
31 struct item_head *ih = de->de_ih;
32 struct reiserfs_de_head *deh = de->de_deh;
33 int rbound, lbound, j;
36 rbound = ih_entry_count(ih) - 1;
38 for (j = (rbound + lbound) / 2; lbound <= rbound;
39 j = (rbound + lbound) / 2) {
40 if (off < deh_offset(deh + j)) {
44 if (off > deh_offset(deh + j)) {
48 /* this is not name found, but matched third key component */
53 de->de_entry_num = lbound;
54 return NAME_NOT_FOUND;
58 * comment? maybe something like set de to point to what the path points to?
60 static inline void set_de_item_location(struct reiserfs_dir_entry *de,
61 struct treepath *path)
63 de->de_bh = get_last_bh(path);
64 de->de_ih = tp_item_head(path);
65 de->de_deh = B_I_DEH(de->de_bh, de->de_ih);
66 de->de_item_num = PATH_LAST_POSITION(path);
70 * de_bh, de_ih, de_deh (points to first element of array), de_item_num is set
72 inline void set_de_name_and_namelen(struct reiserfs_dir_entry *de)
74 struct reiserfs_de_head *deh = de->de_deh + de->de_entry_num;
76 BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih));
78 de->de_entrylen = entry_length(de->de_bh, de->de_ih, de->de_entry_num);
79 de->de_namelen = de->de_entrylen - (de_with_sd(deh) ? SD_SIZE : 0);
80 de->de_name = ih_item_body(de->de_bh, de->de_ih) + deh_location(deh);
81 if (de->de_name[de->de_namelen - 1] == 0)
82 de->de_namelen = strlen(de->de_name);
85 /* what entry points to */
86 static inline void set_de_object_key(struct reiserfs_dir_entry *de)
88 BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih));
89 de->de_dir_id = deh_dir_id(&de->de_deh[de->de_entry_num]);
90 de->de_objectid = deh_objectid(&de->de_deh[de->de_entry_num]);
93 static inline void store_de_entry_key(struct reiserfs_dir_entry *de)
95 struct reiserfs_de_head *deh = de->de_deh + de->de_entry_num;
97 BUG_ON(de->de_entry_num >= ih_entry_count(de->de_ih));
99 /* store key of the found entry */
100 de->de_entry_key.version = KEY_FORMAT_3_5;
101 de->de_entry_key.on_disk_key.k_dir_id =
102 le32_to_cpu(de->de_ih->ih_key.k_dir_id);
103 de->de_entry_key.on_disk_key.k_objectid =
104 le32_to_cpu(de->de_ih->ih_key.k_objectid);
105 set_cpu_key_k_offset(&de->de_entry_key, deh_offset(deh));
106 set_cpu_key_k_type(&de->de_entry_key, TYPE_DIRENTRY);
110 * We assign a key to each directory item, and place multiple entries in a
111 * single directory item. A directory item has a key equal to the key of
112 * the first directory entry in it.
114 * This function first calls search_by_key, then, if item whose first entry
115 * matches is not found it looks for the entry inside directory item found
116 * by search_by_key. Fills the path to the entry, and to the entry position
119 /* The function is NOT SCHEDULE-SAFE! */
120 int search_by_entry_key(struct super_block *sb, const struct cpu_key *key,
121 struct treepath *path, struct reiserfs_dir_entry *de)
125 retval = search_item(sb, key, path);
128 if (!PATH_LAST_POSITION(path)) {
129 reiserfs_error(sb, "vs-7000", "search_by_key "
130 "returned item position == 0");
134 PATH_LAST_POSITION(path)--;
144 reiserfs_error(sb, "vs-7002", "no path to here");
148 set_de_item_location(de, path);
150 #ifdef CONFIG_REISERFS_CHECK
151 if (!is_direntry_le_ih(de->de_ih) ||
152 COMP_SHORT_KEYS(&de->de_ih->ih_key, key)) {
153 print_block(de->de_bh, 0, -1, -1);
154 reiserfs_panic(sb, "vs-7005", "found item %h is not directory "
155 "item or does not belong to the same directory "
156 "as key %K", de->de_ih, key);
158 #endif /* CONFIG_REISERFS_CHECK */
161 * binary search in directory item by third component of the
162 * key. sets de->de_entry_num of de
164 retval = bin_search_in_dir_item(de, cpu_key_k_offset(key));
165 path->pos_in_item = de->de_entry_num;
166 if (retval != NAME_NOT_FOUND) {
168 * ugly, but rename needs de_bh, de_deh, de_name,
169 * de_namelen, de_objectid set
171 set_de_name_and_namelen(de);
172 set_de_object_key(de);
177 /* Keyed 32-bit hash function using TEA in a Davis-Meyer function */
180 * The third component is hashed, and you can choose from more than
181 * one hash function. Per directory hashes are not yet implemented
182 * but are thought about. This function should be moved to hashes.c
183 * Jedi, please do so. -Hans
185 static __u32 get_third_component(struct super_block *s,
186 const char *name, int len)
190 if (!len || (len == 1 && name[0] == '.'))
192 if (len == 2 && name[0] == '.' && name[1] == '.')
193 return DOT_DOT_OFFSET;
195 res = REISERFS_SB(s)->s_hash_function(name, len);
197 /* take bits from 7-th to 30-th including both bounds */
198 res = GET_HASH_VALUE(res);
201 * needed to have no names before "." and ".." those have hash
202 * value == 0 and generation conters 1 and 2 accordingly
205 return res + MAX_GENERATION_NUMBER;
208 static int reiserfs_match(struct reiserfs_dir_entry *de,
209 const char *name, int namelen)
211 int retval = NAME_NOT_FOUND;
213 if ((namelen == de->de_namelen) &&
214 !memcmp(de->de_name, name, de->de_namelen))
216 (de_visible(de->de_deh + de->de_entry_num) ? NAME_FOUND :
217 NAME_FOUND_INVISIBLE);
222 /* de's de_bh, de_ih, de_deh, de_item_num, de_entry_num are set already */
224 /* used when hash collisions exist */
226 static int linear_search_in_dir_item(struct cpu_key *key,
227 struct reiserfs_dir_entry *de,
228 const char *name, int namelen)
230 struct reiserfs_de_head *deh = de->de_deh;
234 i = de->de_entry_num;
236 if (i == ih_entry_count(de->de_ih) ||
237 GET_HASH_VALUE(deh_offset(deh + i)) !=
238 GET_HASH_VALUE(cpu_key_k_offset(key))) {
242 RFALSE(de->de_deh != B_I_DEH(de->de_bh, de->de_ih),
243 "vs-7010: array of entry headers not found");
247 for (; i >= 0; i--, deh--) {
248 /* hash value does not match, no need to check whole name */
249 if (GET_HASH_VALUE(deh_offset(deh)) !=
250 GET_HASH_VALUE(cpu_key_k_offset(key))) {
251 return NAME_NOT_FOUND;
254 /* mark that this generation number is used */
255 if (de->de_gen_number_bit_string)
256 set_bit(GET_GENERATION_NUMBER(deh_offset(deh)),
257 de->de_gen_number_bit_string);
259 /* calculate pointer to name and namelen */
260 de->de_entry_num = i;
261 set_de_name_and_namelen(de);
264 * de's de_name, de_namelen, de_recordlen are set.
268 reiserfs_match(de, name, namelen)) != NAME_NOT_FOUND) {
270 /* key of pointed object */
271 set_de_object_key(de);
273 store_de_entry_key(de);
275 /* retval can be NAME_FOUND or NAME_FOUND_INVISIBLE */
280 if (GET_GENERATION_NUMBER(le_ih_k_offset(de->de_ih)) == 0)
282 * we have reached left most entry in the node. In common we
283 * have to go to the left neighbor, but if generation counter
284 * is 0 already, we know for sure, that there is no name with
285 * the same hash value
288 * FIXME: this work correctly only because hash value can not
289 * be 0. Btw, in case of Yura's hash it is probably possible,
292 return NAME_NOT_FOUND;
294 RFALSE(de->de_item_num,
295 "vs-7015: two diritems of the same directory in one node?");
297 return GOTO_PREVIOUS_ITEM;
301 * may return NAME_FOUND, NAME_FOUND_INVISIBLE, NAME_NOT_FOUND
302 * FIXME: should add something like IOERROR
304 static int reiserfs_find_entry(struct inode *dir, const char *name, int namelen,
305 struct treepath *path_to_entry,
306 struct reiserfs_dir_entry *de)
308 struct cpu_key key_to_search;
311 if (namelen > REISERFS_MAX_NAME(dir->i_sb->s_blocksize))
312 return NAME_NOT_FOUND;
314 /* we will search for this key in the tree */
315 make_cpu_key(&key_to_search, dir,
316 get_third_component(dir->i_sb, name, namelen),
321 search_by_entry_key(dir->i_sb, &key_to_search,
323 if (retval == IO_ERROR) {
324 reiserfs_error(dir->i_sb, "zam-7001", "io error");
328 /* compare names for all entries having given hash value */
330 linear_search_in_dir_item(&key_to_search, de, name,
333 * there is no need to scan directory anymore.
334 * Given entry found or does not exist
336 if (retval != GOTO_PREVIOUS_ITEM) {
337 path_to_entry->pos_in_item = de->de_entry_num;
342 * there is left neighboring item of this directory
343 * and given entry can be there
345 set_cpu_key_k_offset(&key_to_search,
346 le_ih_k_offset(de->de_ih) - 1);
347 pathrelse(path_to_entry);
352 static struct dentry *reiserfs_lookup(struct inode *dir, struct dentry *dentry,
356 struct inode *inode = NULL;
357 struct reiserfs_dir_entry de;
358 INITIALIZE_PATH(path_to_entry);
360 if (REISERFS_MAX_NAME(dir->i_sb->s_blocksize) < dentry->d_name.len)
361 return ERR_PTR(-ENAMETOOLONG);
363 reiserfs_write_lock(dir->i_sb);
365 de.de_gen_number_bit_string = NULL;
367 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
368 &path_to_entry, &de);
369 pathrelse(&path_to_entry);
370 if (retval == NAME_FOUND) {
371 inode = reiserfs_iget(dir->i_sb,
372 (struct cpu_key *)&de.de_dir_id);
373 if (!inode || IS_ERR(inode)) {
374 reiserfs_write_unlock(dir->i_sb);
375 return ERR_PTR(-EACCES);
379 * Propagate the private flag so we know we're
380 * in the priv tree. Also clear IOP_XATTR
381 * since we don't have xattrs on xattr files.
383 if (IS_PRIVATE(dir)) {
384 inode->i_flags |= S_PRIVATE;
385 inode->i_opflags &= ~IOP_XATTR;
388 reiserfs_write_unlock(dir->i_sb);
389 if (retval == IO_ERROR) {
390 return ERR_PTR(-EIO);
393 return d_splice_alias(inode, dentry);
397 * looks up the dentry of the parent directory for child.
398 * taken from ext2_get_parent
400 struct dentry *reiserfs_get_parent(struct dentry *child)
403 struct inode *inode = NULL;
404 struct reiserfs_dir_entry de;
405 INITIALIZE_PATH(path_to_entry);
406 struct inode *dir = d_inode(child);
408 if (dir->i_nlink == 0) {
409 return ERR_PTR(-ENOENT);
411 de.de_gen_number_bit_string = NULL;
413 reiserfs_write_lock(dir->i_sb);
414 retval = reiserfs_find_entry(dir, "..", 2, &path_to_entry, &de);
415 pathrelse(&path_to_entry);
416 if (retval != NAME_FOUND) {
417 reiserfs_write_unlock(dir->i_sb);
418 return ERR_PTR(-ENOENT);
420 inode = reiserfs_iget(dir->i_sb, (struct cpu_key *)&de.de_dir_id);
421 reiserfs_write_unlock(dir->i_sb);
423 return d_obtain_alias(inode);
426 /* add entry to the directory (entry can be hidden).
428 insert definition of when hidden directories are used here -Hans
430 Does not mark dir inode dirty, do it after successesfull call to it */
432 static int reiserfs_add_entry(struct reiserfs_transaction_handle *th,
433 struct inode *dir, const char *name, int namelen,
434 struct inode *inode, int visible)
436 struct cpu_key entry_key;
437 struct reiserfs_de_head *deh;
438 INITIALIZE_PATH(path);
439 struct reiserfs_dir_entry de;
440 DECLARE_BITMAP(bit_string, MAX_GENERATION_NUMBER + 1);
444 * 48 bytes now and we avoid kmalloc if we
445 * create file with short name
447 char small_buf[32 + DEH_SIZE];
450 int buflen, paste_size;
453 BUG_ON(!th->t_trans_id);
455 /* cannot allow items to be added into a busy deleted directory */
459 if (namelen > REISERFS_MAX_NAME(dir->i_sb->s_blocksize))
460 return -ENAMETOOLONG;
462 /* each entry has unique key. compose it */
463 make_cpu_key(&entry_key, dir,
464 get_third_component(dir->i_sb, name, namelen),
467 /* get memory for composing the entry */
468 buflen = DEH_SIZE + ROUND_UP(namelen);
469 if (buflen > sizeof(small_buf)) {
470 buffer = kmalloc(buflen, GFP_NOFS);
477 (get_inode_sd_version(dir) ==
478 STAT_DATA_V1) ? (DEH_SIZE + namelen) : buflen;
481 * fill buffer : directory entry head, name[, dir objectid | ,
482 * stat data | ,stat data, dir objectid ]
484 deh = (struct reiserfs_de_head *)buffer;
485 deh->deh_location = 0; /* JDM Endian safe if 0 */
486 put_deh_offset(deh, cpu_key_k_offset(&entry_key));
487 deh->deh_state = 0; /* JDM Endian safe if 0 */
488 /* put key (ino analog) to de */
490 /* safe: k_dir_id is le */
491 deh->deh_dir_id = INODE_PKEY(inode)->k_dir_id;
492 /* safe: k_objectid is le */
493 deh->deh_objectid = INODE_PKEY(inode)->k_objectid;
496 memcpy((char *)(deh + 1), name, namelen);
497 /* padd by 0s to the 4 byte boundary */
498 padd_item((char *)(deh + 1), ROUND_UP(namelen), namelen);
501 * entry is ready to be pasted into tree, set 'visibility'
502 * and 'stat data in entry' attributes
504 mark_de_without_sd(deh);
505 visible ? mark_de_visible(deh) : mark_de_hidden(deh);
507 /* find the proper place for the new entry */
508 memset(bit_string, 0, sizeof(bit_string));
509 de.de_gen_number_bit_string = bit_string;
510 retval = reiserfs_find_entry(dir, name, namelen, &path, &de);
511 if (retval != NAME_NOT_FOUND) {
512 if (buffer != small_buf)
516 if (retval == IO_ERROR) {
520 if (retval != NAME_FOUND) {
521 reiserfs_error(dir->i_sb, "zam-7002",
522 "reiserfs_find_entry() returned "
523 "unexpected value (%d)", retval);
530 find_first_zero_bit(bit_string,
531 MAX_GENERATION_NUMBER + 1);
532 if (gen_number > MAX_GENERATION_NUMBER) {
533 /* there is no free generation number */
534 reiserfs_warning(dir->i_sb, "reiserfs-7010",
535 "Congratulations! we have got hash function "
537 if (buffer != small_buf)
542 /* adjust offset of directory enrty */
543 put_deh_offset(deh, SET_GENERATION_NUMBER(deh_offset(deh), gen_number));
544 set_cpu_key_k_offset(&entry_key, deh_offset(deh));
546 /* update max-hash-collisions counter in reiserfs_sb_info */
547 PROC_INFO_MAX(th->t_super, max_hash_collisions, gen_number);
549 /* we need to re-search for the insertion point */
550 if (gen_number != 0) {
551 if (search_by_entry_key(dir->i_sb, &entry_key, &path, &de) !=
553 reiserfs_warning(dir->i_sb, "vs-7032",
554 "entry with this key (%K) already "
555 "exists", &entry_key);
557 if (buffer != small_buf)
564 /* perform the insertion of the entry that we have prepared */
566 reiserfs_paste_into_item(th, &path, &entry_key, dir, buffer,
568 if (buffer != small_buf)
571 reiserfs_check_path(&path);
575 dir->i_size += paste_size;
576 dir->i_mtime = dir->i_ctime = current_time(dir);
577 if (!S_ISDIR(inode->i_mode) && visible)
578 /* reiserfs_mkdir or reiserfs_rename will do that by itself */
579 reiserfs_update_sd(th, dir);
581 reiserfs_check_path(&path);
586 * quota utility function, call if you've had to abort after calling
587 * new_inode_init, and have not called reiserfs_new_inode yet.
588 * This should only be called on inodes that do not have stat data
589 * inserted into the tree yet.
591 static int drop_new_inode(struct inode *inode)
594 make_bad_inode(inode);
595 inode->i_flags |= S_NOQUOTA;
601 * utility function that does setup for reiserfs_new_inode.
602 * dquot_initialize needs lots of credits so it's better to have it
603 * outside of a transaction, so we had to pull some bits of
604 * reiserfs_new_inode out into this func.
606 static int new_inode_init(struct inode *inode, struct inode *dir, umode_t mode)
609 * Make inode invalid - just in case we are going to drop it before
610 * the initialization happens
612 INODE_PKEY(inode)->k_objectid = 0;
615 * the quota init calls have to know who to charge the quota to, so
616 * we have to set uid and gid here
618 inode_init_owner(inode, dir, mode);
619 return dquot_initialize(inode);
622 static int reiserfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
628 * We need blocks for transaction + (user+group)*(quotas
629 * for new inode + update of quota for directory owner)
632 JOURNAL_PER_BALANCE_CNT * 2 +
633 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
634 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
635 struct reiserfs_transaction_handle th;
636 struct reiserfs_security_handle security;
638 retval = dquot_initialize(dir);
642 if (!(inode = new_inode(dir->i_sb))) {
645 retval = new_inode_init(inode, dir, mode);
647 drop_new_inode(inode);
651 jbegin_count += reiserfs_cache_default_acl(dir);
652 retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security);
654 drop_new_inode(inode);
657 jbegin_count += retval;
658 reiserfs_write_lock(dir->i_sb);
660 retval = journal_begin(&th, dir->i_sb, jbegin_count);
662 drop_new_inode(inode);
667 reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
672 inode->i_op = &reiserfs_file_inode_operations;
673 inode->i_fop = &reiserfs_file_operations;
674 inode->i_mapping->a_ops = &reiserfs_address_space_operations;
677 reiserfs_add_entry(&th, dir, dentry->d_name.name,
678 dentry->d_name.len, inode, 1 /*visible */ );
682 reiserfs_update_sd(&th, inode);
683 err = journal_end(&th);
686 unlock_new_inode(inode);
690 reiserfs_update_inode_transaction(inode);
691 reiserfs_update_inode_transaction(dir);
693 d_instantiate_new(dentry, inode);
694 retval = journal_end(&th);
697 reiserfs_write_unlock(dir->i_sb);
701 static int reiserfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
706 struct reiserfs_transaction_handle th;
707 struct reiserfs_security_handle security;
709 * We need blocks for transaction + (user+group)*(quotas
710 * for new inode + update of quota for directory owner)
713 JOURNAL_PER_BALANCE_CNT * 3 +
714 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
715 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
717 retval = dquot_initialize(dir);
721 if (!(inode = new_inode(dir->i_sb))) {
724 retval = new_inode_init(inode, dir, mode);
726 drop_new_inode(inode);
730 jbegin_count += reiserfs_cache_default_acl(dir);
731 retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security);
733 drop_new_inode(inode);
736 jbegin_count += retval;
737 reiserfs_write_lock(dir->i_sb);
739 retval = journal_begin(&th, dir->i_sb, jbegin_count);
741 drop_new_inode(inode);
746 reiserfs_new_inode(&th, dir, mode, NULL, 0 /*i_size */ , dentry,
752 inode->i_op = &reiserfs_special_inode_operations;
753 init_special_inode(inode, inode->i_mode, rdev);
755 /* FIXME: needed for block and char devices only */
756 reiserfs_update_sd(&th, inode);
758 reiserfs_update_inode_transaction(inode);
759 reiserfs_update_inode_transaction(dir);
762 reiserfs_add_entry(&th, dir, dentry->d_name.name,
763 dentry->d_name.len, inode, 1 /*visible */ );
767 reiserfs_update_sd(&th, inode);
768 err = journal_end(&th);
771 unlock_new_inode(inode);
776 d_instantiate_new(dentry, inode);
777 retval = journal_end(&th);
780 reiserfs_write_unlock(dir->i_sb);
784 static int reiserfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
788 struct reiserfs_transaction_handle th;
789 struct reiserfs_security_handle security;
791 * We need blocks for transaction + (user+group)*(quotas
792 * for new inode + update of quota for directory owner)
795 JOURNAL_PER_BALANCE_CNT * 3 +
796 2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
797 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
799 retval = dquot_initialize(dir);
803 #ifdef DISPLACE_NEW_PACKING_LOCALITIES
805 * set flag that new packing locality created and new blocks
806 * for the content of that directory are not displaced yet
808 REISERFS_I(dir)->new_packing_locality = 1;
810 mode = S_IFDIR | mode;
811 if (!(inode = new_inode(dir->i_sb))) {
814 retval = new_inode_init(inode, dir, mode);
816 drop_new_inode(inode);
820 jbegin_count += reiserfs_cache_default_acl(dir);
821 retval = reiserfs_security_init(dir, inode, &dentry->d_name, &security);
823 drop_new_inode(inode);
826 jbegin_count += retval;
827 reiserfs_write_lock(dir->i_sb);
829 retval = journal_begin(&th, dir->i_sb, jbegin_count);
831 drop_new_inode(inode);
836 * inc the link count now, so another writer doesn't overflow
837 * it while we sleep later on.
839 INC_DIR_INODE_NLINK(dir)
841 retval = reiserfs_new_inode(&th, dir, mode, NULL /*symlink */ ,
842 old_format_only(dir->i_sb) ?
843 EMPTY_DIR_SIZE_V1 : EMPTY_DIR_SIZE,
844 dentry, inode, &security);
846 DEC_DIR_INODE_NLINK(dir)
850 reiserfs_update_inode_transaction(inode);
851 reiserfs_update_inode_transaction(dir);
853 inode->i_op = &reiserfs_dir_inode_operations;
854 inode->i_fop = &reiserfs_dir_operations;
856 /* note, _this_ add_entry will not update dir's stat data */
858 reiserfs_add_entry(&th, dir, dentry->d_name.name,
859 dentry->d_name.len, inode, 1 /*visible */ );
863 DEC_DIR_INODE_NLINK(dir);
864 reiserfs_update_sd(&th, inode);
865 err = journal_end(&th);
868 unlock_new_inode(inode);
872 /* the above add_entry did not update dir's stat data */
873 reiserfs_update_sd(&th, dir);
875 d_instantiate_new(dentry, inode);
876 retval = journal_end(&th);
878 reiserfs_write_unlock(dir->i_sb);
882 static inline int reiserfs_empty_dir(struct inode *inode)
885 * we can cheat because an old format dir cannot have
886 * EMPTY_DIR_SIZE, and a new format dir cannot have
887 * EMPTY_DIR_SIZE_V1. So, if the inode is either size,
888 * regardless of disk format version, the directory is empty.
890 if (inode->i_size != EMPTY_DIR_SIZE &&
891 inode->i_size != EMPTY_DIR_SIZE_V1) {
897 static int reiserfs_rmdir(struct inode *dir, struct dentry *dentry)
901 struct reiserfs_transaction_handle th;
903 INITIALIZE_PATH(path);
904 struct reiserfs_dir_entry de;
907 * we will be doing 2 balancings and update 2 stat data, we
908 * change quotas of the owner of the directory and of the owner
909 * of the parent directory. The quota structure is possibly
910 * deleted only on last iput => outside of this transaction
913 JOURNAL_PER_BALANCE_CNT * 2 + 2 +
914 4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
916 retval = dquot_initialize(dir);
920 reiserfs_write_lock(dir->i_sb);
921 retval = journal_begin(&th, dir->i_sb, jbegin_count);
925 de.de_gen_number_bit_string = NULL;
927 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
928 &path, &de)) == NAME_NOT_FOUND) {
931 } else if (retval == IO_ERROR) {
936 inode = d_inode(dentry);
938 reiserfs_update_inode_transaction(inode);
939 reiserfs_update_inode_transaction(dir);
941 if (de.de_objectid != inode->i_ino) {
943 * FIXME: compare key of an object and a key found in the entry
948 if (!reiserfs_empty_dir(inode)) {
953 /* cut entry from dir directory */
954 retval = reiserfs_cut_from_item(&th, &path, &de.de_entry_key,
955 dir, NULL, /* page */
956 0 /*new file size - not used here */ );
960 if (inode->i_nlink != 2 && inode->i_nlink != 1)
961 reiserfs_error(inode->i_sb, "reiserfs-7040",
962 "empty directory has nlink != 2 (%d)",
966 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(dir);
967 reiserfs_update_sd(&th, inode);
969 DEC_DIR_INODE_NLINK(dir)
970 dir->i_size -= (DEH_SIZE + de.de_entrylen);
971 reiserfs_update_sd(&th, dir);
973 /* prevent empty directory from getting lost */
974 add_save_link(&th, inode, 0 /* not truncate */ );
976 retval = journal_end(&th);
977 reiserfs_check_path(&path);
979 reiserfs_write_unlock(dir->i_sb);
984 * we must release path, because we did not call
985 * reiserfs_cut_from_item, or reiserfs_cut_from_item does not
986 * release path if operation was not complete
989 err = journal_end(&th);
990 reiserfs_write_unlock(dir->i_sb);
991 return err ? err : retval;
994 static int reiserfs_unlink(struct inode *dir, struct dentry *dentry)
998 struct reiserfs_dir_entry de;
999 INITIALIZE_PATH(path);
1000 struct reiserfs_transaction_handle th;
1002 unsigned long savelink;
1004 retval = dquot_initialize(dir);
1008 inode = d_inode(dentry);
1011 * in this transaction we can be doing at max two balancings and
1012 * update two stat datas, we change quotas of the owner of the
1013 * directory and of the owner of the parent directory. The quota
1014 * structure is possibly deleted only on iput => outside of
1018 JOURNAL_PER_BALANCE_CNT * 2 + 2 +
1019 4 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
1021 reiserfs_write_lock(dir->i_sb);
1022 retval = journal_begin(&th, dir->i_sb, jbegin_count);
1026 de.de_gen_number_bit_string = NULL;
1028 reiserfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len,
1029 &path, &de)) == NAME_NOT_FOUND) {
1032 } else if (retval == IO_ERROR) {
1037 reiserfs_update_inode_transaction(inode);
1038 reiserfs_update_inode_transaction(dir);
1040 if (de.de_objectid != inode->i_ino) {
1042 * FIXME: compare key of an object and a key found in the entry
1048 if (!inode->i_nlink) {
1049 reiserfs_warning(inode->i_sb, "reiserfs-7042",
1050 "deleting nonexistent file (%lu), %d",
1051 inode->i_ino, inode->i_nlink);
1052 set_nlink(inode, 1);
1058 * we schedule before doing the add_save_link call, save the link
1059 * count so we don't race
1061 savelink = inode->i_nlink;
1064 reiserfs_cut_from_item(&th, &path, &de.de_entry_key, dir, NULL,
1070 inode->i_ctime = current_time(inode);
1071 reiserfs_update_sd(&th, inode);
1073 dir->i_size -= (de.de_entrylen + DEH_SIZE);
1074 dir->i_ctime = dir->i_mtime = current_time(dir);
1075 reiserfs_update_sd(&th, dir);
1078 /* prevent file from getting lost */
1079 add_save_link(&th, inode, 0 /* not truncate */ );
1081 retval = journal_end(&th);
1082 reiserfs_check_path(&path);
1083 reiserfs_write_unlock(dir->i_sb);
1088 err = journal_end(&th);
1089 reiserfs_check_path(&path);
1093 reiserfs_write_unlock(dir->i_sb);
1097 static int reiserfs_symlink(struct inode *parent_dir,
1098 struct dentry *dentry, const char *symname)
1101 struct inode *inode;
1104 struct reiserfs_transaction_handle th;
1105 struct reiserfs_security_handle security;
1106 int mode = S_IFLNK | S_IRWXUGO;
1108 * We need blocks for transaction + (user+group)*(quotas for
1109 * new inode + update of quota for directory owner)
1112 JOURNAL_PER_BALANCE_CNT * 3 +
1113 2 * (REISERFS_QUOTA_INIT_BLOCKS(parent_dir->i_sb) +
1114 REISERFS_QUOTA_TRANS_BLOCKS(parent_dir->i_sb));
1116 retval = dquot_initialize(parent_dir);
1120 if (!(inode = new_inode(parent_dir->i_sb))) {
1123 retval = new_inode_init(inode, parent_dir, mode);
1125 drop_new_inode(inode);
1129 retval = reiserfs_security_init(parent_dir, inode, &dentry->d_name,
1132 drop_new_inode(inode);
1135 jbegin_count += retval;
1137 reiserfs_write_lock(parent_dir->i_sb);
1138 item_len = ROUND_UP(strlen(symname));
1139 if (item_len > MAX_DIRECT_ITEM_LEN(parent_dir->i_sb->s_blocksize)) {
1140 retval = -ENAMETOOLONG;
1141 drop_new_inode(inode);
1145 name = kmalloc(item_len, GFP_NOFS);
1147 drop_new_inode(inode);
1151 memcpy(name, symname, strlen(symname));
1152 padd_item(name, item_len, strlen(symname));
1154 retval = journal_begin(&th, parent_dir->i_sb, jbegin_count);
1156 drop_new_inode(inode);
1162 reiserfs_new_inode(&th, parent_dir, mode, name, strlen(symname),
1163 dentry, inode, &security);
1165 if (retval) { /* reiserfs_new_inode iputs for us */
1169 reiserfs_update_inode_transaction(inode);
1170 reiserfs_update_inode_transaction(parent_dir);
1172 inode->i_op = &reiserfs_symlink_inode_operations;
1173 inode_nohighmem(inode);
1174 inode->i_mapping->a_ops = &reiserfs_address_space_operations;
1176 retval = reiserfs_add_entry(&th, parent_dir, dentry->d_name.name,
1177 dentry->d_name.len, inode, 1 /*visible */ );
1181 reiserfs_update_sd(&th, inode);
1182 err = journal_end(&th);
1185 unlock_new_inode(inode);
1190 d_instantiate_new(dentry, inode);
1191 retval = journal_end(&th);
1193 reiserfs_write_unlock(parent_dir->i_sb);
1197 static int reiserfs_link(struct dentry *old_dentry, struct inode *dir,
1198 struct dentry *dentry)
1201 struct inode *inode = d_inode(old_dentry);
1202 struct reiserfs_transaction_handle th;
1204 * We need blocks for transaction + update of quotas for
1205 * the owners of the directory
1208 JOURNAL_PER_BALANCE_CNT * 3 +
1209 2 * REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb);
1211 retval = dquot_initialize(dir);
1215 reiserfs_write_lock(dir->i_sb);
1216 if (inode->i_nlink >= REISERFS_LINK_MAX) {
1217 /* FIXME: sd_nlink is 32 bit for new files */
1218 reiserfs_write_unlock(dir->i_sb);
1222 /* inc before scheduling so reiserfs_unlink knows we are here */
1225 retval = journal_begin(&th, dir->i_sb, jbegin_count);
1228 reiserfs_write_unlock(dir->i_sb);
1232 /* create new entry */
1234 reiserfs_add_entry(&th, dir, dentry->d_name.name,
1235 dentry->d_name.len, inode, 1 /*visible */ );
1237 reiserfs_update_inode_transaction(inode);
1238 reiserfs_update_inode_transaction(dir);
1243 err = journal_end(&th);
1244 reiserfs_write_unlock(dir->i_sb);
1245 return err ? err : retval;
1248 inode->i_ctime = current_time(inode);
1249 reiserfs_update_sd(&th, inode);
1252 d_instantiate(dentry, inode);
1253 retval = journal_end(&th);
1254 reiserfs_write_unlock(dir->i_sb);
1258 /* de contains information pointing to an entry which */
1259 static int de_still_valid(const char *name, int len,
1260 struct reiserfs_dir_entry *de)
1262 struct reiserfs_dir_entry tmp = *de;
1264 /* recalculate pointer to name and name length */
1265 set_de_name_and_namelen(&tmp);
1266 /* FIXME: could check more */
1267 if (tmp.de_namelen != len || memcmp(name, de->de_name, len))
1272 static int entry_points_to_object(const char *name, int len,
1273 struct reiserfs_dir_entry *de,
1274 struct inode *inode)
1276 if (!de_still_valid(name, len, de))
1280 if (!de_visible(de->de_deh + de->de_entry_num))
1281 reiserfs_panic(inode->i_sb, "vs-7042",
1282 "entry must be visible");
1283 return (de->de_objectid == inode->i_ino) ? 1 : 0;
1286 /* this must be added hidden entry */
1287 if (de_visible(de->de_deh + de->de_entry_num))
1288 reiserfs_panic(NULL, "vs-7043", "entry must be visible");
1293 /* sets key of objectid the entry has to point to */
1294 static void set_ino_in_dir_entry(struct reiserfs_dir_entry *de,
1295 struct reiserfs_key *key)
1297 /* JDM These operations are endian safe - both are le */
1298 de->de_deh[de->de_entry_num].deh_dir_id = key->k_dir_id;
1299 de->de_deh[de->de_entry_num].deh_objectid = key->k_objectid;
1303 * process, that is going to call fix_nodes/do_balance must hold only
1304 * one path. If it holds 2 or more, it can get into endless waiting in
1305 * get_empty_nodes or its clones
1307 static int reiserfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1308 struct inode *new_dir, struct dentry *new_dentry,
1312 INITIALIZE_PATH(old_entry_path);
1313 INITIALIZE_PATH(new_entry_path);
1314 INITIALIZE_PATH(dot_dot_entry_path);
1315 struct item_head new_entry_ih, old_entry_ih, dot_dot_ih;
1316 struct reiserfs_dir_entry old_de, new_de, dot_dot_de;
1317 struct inode *old_inode, *new_dentry_inode;
1318 struct reiserfs_transaction_handle th;
1320 umode_t old_inode_mode;
1321 unsigned long savelink = 1;
1322 struct timespec64 ctime;
1324 if (flags & ~RENAME_NOREPLACE)
1328 * three balancings: (1) old name removal, (2) new name insertion
1329 * and (3) maybe "save" link insertion
1330 * stat data updates: (1) old directory,
1331 * (2) new directory and (3) maybe old object stat data (when it is
1332 * directory) and (4) maybe stat data of object to which new entry
1333 * pointed initially and (5) maybe block containing ".." of
1335 * quota updates: two parent directories
1338 JOURNAL_PER_BALANCE_CNT * 3 + 5 +
1339 4 * REISERFS_QUOTA_TRANS_BLOCKS(old_dir->i_sb);
1341 retval = dquot_initialize(old_dir);
1344 retval = dquot_initialize(new_dir);
1348 old_inode = d_inode(old_dentry);
1349 new_dentry_inode = d_inode(new_dentry);
1352 * make sure that oldname still exists and points to an object we
1353 * are going to rename
1355 old_de.de_gen_number_bit_string = NULL;
1356 reiserfs_write_lock(old_dir->i_sb);
1358 reiserfs_find_entry(old_dir, old_dentry->d_name.name,
1359 old_dentry->d_name.len, &old_entry_path,
1361 pathrelse(&old_entry_path);
1362 if (retval == IO_ERROR) {
1363 reiserfs_write_unlock(old_dir->i_sb);
1367 if (retval != NAME_FOUND || old_de.de_objectid != old_inode->i_ino) {
1368 reiserfs_write_unlock(old_dir->i_sb);
1372 old_inode_mode = old_inode->i_mode;
1373 if (S_ISDIR(old_inode_mode)) {
1375 * make sure that directory being renamed has correct ".."
1376 * and that its new parent directory has not too many links
1379 if (new_dentry_inode) {
1380 if (!reiserfs_empty_dir(new_dentry_inode)) {
1381 reiserfs_write_unlock(old_dir->i_sb);
1387 * directory is renamed, its parent directory will be changed,
1388 * so find ".." entry
1390 dot_dot_de.de_gen_number_bit_string = NULL;
1392 reiserfs_find_entry(old_inode, "..", 2, &dot_dot_entry_path,
1394 pathrelse(&dot_dot_entry_path);
1395 if (retval != NAME_FOUND) {
1396 reiserfs_write_unlock(old_dir->i_sb);
1400 /* inode number of .. must equal old_dir->i_ino */
1401 if (dot_dot_de.de_objectid != old_dir->i_ino) {
1402 reiserfs_write_unlock(old_dir->i_sb);
1407 retval = journal_begin(&th, old_dir->i_sb, jbegin_count);
1409 reiserfs_write_unlock(old_dir->i_sb);
1413 /* add new entry (or find the existing one) */
1415 reiserfs_add_entry(&th, new_dir, new_dentry->d_name.name,
1416 new_dentry->d_name.len, old_inode, 0);
1417 if (retval == -EEXIST) {
1418 if (!new_dentry_inode) {
1419 reiserfs_panic(old_dir->i_sb, "vs-7050",
1420 "new entry is found, new inode == 0");
1422 } else if (retval) {
1423 int err = journal_end(&th);
1424 reiserfs_write_unlock(old_dir->i_sb);
1425 return err ? err : retval;
1428 reiserfs_update_inode_transaction(old_dir);
1429 reiserfs_update_inode_transaction(new_dir);
1432 * this makes it so an fsync on an open fd for the old name will
1433 * commit the rename operation
1435 reiserfs_update_inode_transaction(old_inode);
1437 if (new_dentry_inode)
1438 reiserfs_update_inode_transaction(new_dentry_inode);
1442 * look for old name using corresponding entry key
1443 * (found by reiserfs_find_entry)
1446 search_by_entry_key(new_dir->i_sb, &old_de.de_entry_key,
1448 &old_de)) != NAME_FOUND) {
1449 pathrelse(&old_entry_path);
1451 reiserfs_write_unlock(old_dir->i_sb);
1455 copy_item_head(&old_entry_ih, tp_item_head(&old_entry_path));
1457 reiserfs_prepare_for_journal(old_inode->i_sb, old_de.de_bh, 1);
1459 /* look for new name by reiserfs_find_entry */
1460 new_de.de_gen_number_bit_string = NULL;
1462 reiserfs_find_entry(new_dir, new_dentry->d_name.name,
1463 new_dentry->d_name.len, &new_entry_path,
1466 * reiserfs_add_entry should not return IO_ERROR,
1467 * because it is called with essentially same parameters from
1468 * reiserfs_add_entry above, and we'll catch any i/o errors
1469 * before we get here.
1471 if (retval != NAME_FOUND_INVISIBLE && retval != NAME_FOUND) {
1472 pathrelse(&new_entry_path);
1473 pathrelse(&old_entry_path);
1475 reiserfs_write_unlock(old_dir->i_sb);
1479 copy_item_head(&new_entry_ih, tp_item_head(&new_entry_path));
1481 reiserfs_prepare_for_journal(old_inode->i_sb, new_de.de_bh, 1);
1483 if (S_ISDIR(old_inode->i_mode)) {
1485 search_by_entry_key(new_dir->i_sb,
1486 &dot_dot_de.de_entry_key,
1487 &dot_dot_entry_path,
1488 &dot_dot_de)) != NAME_FOUND) {
1489 pathrelse(&dot_dot_entry_path);
1490 pathrelse(&new_entry_path);
1491 pathrelse(&old_entry_path);
1493 reiserfs_write_unlock(old_dir->i_sb);
1496 copy_item_head(&dot_dot_ih,
1497 tp_item_head(&dot_dot_entry_path));
1498 /* node containing ".." gets into transaction */
1499 reiserfs_prepare_for_journal(old_inode->i_sb,
1500 dot_dot_de.de_bh, 1);
1503 * we should check seals here, not do
1504 * this stuff, yes? Then, having
1505 * gathered everything into RAM we
1506 * should lock the buffers, yes? -Hans
1509 * probably. our rename needs to hold more
1510 * than one path at once. The seals would
1511 * have to be written to deal with multi-path
1515 * sanity checking before doing the rename - avoid races many
1516 * of the above checks could have scheduled. We have to be
1517 * sure our items haven't been shifted by another process.
1519 if (item_moved(&new_entry_ih, &new_entry_path) ||
1520 !entry_points_to_object(new_dentry->d_name.name,
1521 new_dentry->d_name.len,
1522 &new_de, new_dentry_inode) ||
1523 item_moved(&old_entry_ih, &old_entry_path) ||
1524 !entry_points_to_object(old_dentry->d_name.name,
1525 old_dentry->d_name.len,
1526 &old_de, old_inode)) {
1527 reiserfs_restore_prepared_buffer(old_inode->i_sb,
1529 reiserfs_restore_prepared_buffer(old_inode->i_sb,
1531 if (S_ISDIR(old_inode_mode))
1532 reiserfs_restore_prepared_buffer(old_inode->
1538 if (S_ISDIR(old_inode_mode)) {
1539 if (item_moved(&dot_dot_ih, &dot_dot_entry_path) ||
1540 !entry_points_to_object("..", 2, &dot_dot_de,
1542 reiserfs_restore_prepared_buffer(old_inode->
1545 reiserfs_restore_prepared_buffer(old_inode->
1548 reiserfs_restore_prepared_buffer(old_inode->
1556 RFALSE(S_ISDIR(old_inode_mode) &&
1557 !buffer_journal_prepared(dot_dot_de.de_bh), "");
1563 * ok, all the changes can be done in one fell swoop when we
1564 * have claimed all the buffers needed.
1567 mark_de_visible(new_de.de_deh + new_de.de_entry_num);
1568 set_ino_in_dir_entry(&new_de, INODE_PKEY(old_inode));
1569 journal_mark_dirty(&th, new_de.de_bh);
1571 mark_de_hidden(old_de.de_deh + old_de.de_entry_num);
1572 journal_mark_dirty(&th, old_de.de_bh);
1573 ctime = current_time(old_dir);
1574 old_dir->i_ctime = old_dir->i_mtime = ctime;
1575 new_dir->i_ctime = new_dir->i_mtime = ctime;
1577 * thanks to Alex Adriaanse <alex_a@caltech.edu> for patch
1578 * which adds ctime update of renamed object
1580 old_inode->i_ctime = ctime;
1582 if (new_dentry_inode) {
1583 /* adjust link number of the victim */
1584 if (S_ISDIR(new_dentry_inode->i_mode)) {
1585 clear_nlink(new_dentry_inode);
1587 drop_nlink(new_dentry_inode);
1589 new_dentry_inode->i_ctime = ctime;
1590 savelink = new_dentry_inode->i_nlink;
1593 if (S_ISDIR(old_inode_mode)) {
1594 /* adjust ".." of renamed directory */
1595 set_ino_in_dir_entry(&dot_dot_de, INODE_PKEY(new_dir));
1596 journal_mark_dirty(&th, dot_dot_de.de_bh);
1599 * there (in new_dir) was no directory, so it got new link
1600 * (".." of renamed directory)
1602 if (!new_dentry_inode)
1603 INC_DIR_INODE_NLINK(new_dir);
1605 /* old directory lost one link - ".. " of renamed directory */
1606 DEC_DIR_INODE_NLINK(old_dir);
1609 * looks like in 2.3.99pre3 brelse is atomic.
1610 * so we can use pathrelse
1612 pathrelse(&new_entry_path);
1613 pathrelse(&dot_dot_entry_path);
1616 * FIXME: this reiserfs_cut_from_item's return value may screw up
1617 * anybody, but it will panic if will not be able to find the
1618 * entry. This needs one more clean up
1620 if (reiserfs_cut_from_item
1621 (&th, &old_entry_path, &old_de.de_entry_key, old_dir, NULL,
1623 reiserfs_error(old_dir->i_sb, "vs-7060",
1624 "couldn't not cut old name. Fsck later?");
1626 old_dir->i_size -= DEH_SIZE + old_de.de_entrylen;
1628 reiserfs_update_sd(&th, old_dir);
1629 reiserfs_update_sd(&th, new_dir);
1630 reiserfs_update_sd(&th, old_inode);
1632 if (new_dentry_inode) {
1634 add_save_link(&th, new_dentry_inode,
1635 0 /* not truncate */ );
1636 reiserfs_update_sd(&th, new_dentry_inode);
1639 retval = journal_end(&th);
1640 reiserfs_write_unlock(old_dir->i_sb);
1644 /* directories can handle most operations... */
1645 const struct inode_operations reiserfs_dir_inode_operations = {
1646 .create = reiserfs_create,
1647 .lookup = reiserfs_lookup,
1648 .link = reiserfs_link,
1649 .unlink = reiserfs_unlink,
1650 .symlink = reiserfs_symlink,
1651 .mkdir = reiserfs_mkdir,
1652 .rmdir = reiserfs_rmdir,
1653 .mknod = reiserfs_mknod,
1654 .rename = reiserfs_rename,
1655 .setattr = reiserfs_setattr,
1656 .listxattr = reiserfs_listxattr,
1657 .permission = reiserfs_permission,
1658 .get_acl = reiserfs_get_acl,
1659 .set_acl = reiserfs_set_acl,
1663 * symlink operations.. same as page_symlink_inode_operations, with xattr
1666 const struct inode_operations reiserfs_symlink_inode_operations = {
1667 .get_link = page_get_link,
1668 .setattr = reiserfs_setattr,
1669 .listxattr = reiserfs_listxattr,
1670 .permission = reiserfs_permission,
1674 * special file operations.. just xattr/acl stuff
1676 const struct inode_operations reiserfs_special_inode_operations = {
1677 .setattr = reiserfs_setattr,
1678 .listxattr = reiserfs_listxattr,
1679 .permission = reiserfs_permission,
1680 .get_acl = reiserfs_get_acl,
1681 .set_acl = reiserfs_set_acl,