2 * fs/logfs/dir.c - directory-related code
4 * As should be obvious for Linux kernel code, license is GPLv2
6 * Copyright (c) 2005-2008 Joern Engel <joern@logfs.org>
9 #include <linux/slab.h>
12 * Atomic dir operations
14 * Directory operations are by default not atomic. Dentries and Inodes are
15 * created/removed/altered in separate operations. Therefore we need to do
16 * a small amount of journaling.
18 * Create, link, mkdir, mknod and symlink all share the same function to do
19 * the work: __logfs_create. This function works in two atomic steps:
20 * 1. allocate inode (remember in journal)
21 * 2. allocate dentry (clear journal)
23 * As we can only get interrupted between the two, when the inode we just
24 * created is simply stored in the anchor. On next mount, if we were
25 * interrupted, we delete the inode. From a users point of view the
26 * operation never happened.
28 * Unlink and rmdir also share the same function: unlink. Again, this
29 * function works in two atomic steps
30 * 1. remove dentry (remember inode in journal)
31 * 2. unlink inode (clear journal)
33 * And again, on the next mount, if we were interrupted, we delete the inode.
34 * From a users point of view the operation succeeded.
36 * Rename is the real pain to deal with, harder than all the other methods
37 * combined. Depending on the circumstances we can run into three cases.
38 * A "target rename" where the target dentry already existed, a "local
39 * rename" where both parent directories are identical or a "cross-directory
40 * rename" in the remaining case.
42 * Local rename is atomic, as the old dentry is simply rewritten with a new
45 * Cross-directory rename works in two steps, similar to __logfs_create and
47 * 1. Write new dentry (remember old dentry in journal)
48 * 2. Remove old dentry (clear journal)
50 * Here we remember a dentry instead of an inode. On next mount, if we were
51 * interrupted, we delete the dentry. From a users point of view, the
52 * operation succeeded.
54 * Target rename works in three atomic steps:
55 * 1. Attach old inode to new dentry (remember old dentry and new inode)
56 * 2. Remove old dentry (still remember the new inode)
57 * 3. Remove victim inode
59 * Here we remember both an inode an a dentry. If we get interrupted
60 * between steps 1 and 2, we delete both the dentry and the inode. If
61 * we get interrupted between steps 2 and 3, we delete just the inode.
62 * In either case, the remaining objects are deleted on next mount. From
63 * a users point of view, the operation succeeded.
66 static int write_dir(struct inode
*dir
, struct logfs_disk_dentry
*dd
,
69 return logfs_inode_write(dir
, dd
, sizeof(*dd
), pos
, WF_LOCK
, NULL
);
72 static int write_inode(struct inode
*inode
)
74 return __logfs_write_inode(inode
, NULL
, WF_LOCK
);
77 static s64
dir_seek_data(struct inode
*inode
, s64 pos
)
79 s64 new_pos
= logfs_seek_data(inode
, pos
);
81 return max(pos
, new_pos
- 1);
84 static int beyond_eof(struct inode
*inode
, loff_t bix
)
86 loff_t pos
= bix
<< inode
->i_sb
->s_blocksize_bits
;
87 return pos
>= i_size_read(inode
);
91 * Prime value was chosen to be roughly 256 + 26. r5 hash uses 11,
92 * so short names (len <= 9) don't even occupy the complete 32bit name
93 * space. A prime >256 ensures short names quickly spread the 32bit
94 * name space. Add about 26 for the estimated amount of information
95 * of each character and pick a prime nearby, preferably a bit-sparse
98 static u32
hash_32(const char *s
, int len
, u32 seed
)
103 for (i
= 0; i
< len
; i
++)
104 hash
= hash
* 293 + s
[i
];
109 * We have to satisfy several conflicting requirements here. Small
110 * directories should stay fairly compact and not require too many
111 * indirect blocks. The number of possible locations for a given hash
112 * should be small to make lookup() fast. And we should try hard not
113 * to overflow the 32bit name space or nfs and 32bit host systems will
116 * So we use the following scheme. First we reduce the hash to 0..15
117 * and try a direct block. If that is occupied we reduce the hash to
118 * 16..255 and try an indirect block. Same for 2x and 3x indirect
119 * blocks. Lastly we reduce the hash to 0x800_0000 .. 0xffff_ffff,
120 * but use buckets containing eight entries instead of a single one.
122 * Using 16 entries should allow for a reasonable amount of hash
123 * collisions, so the 32bit name space can be packed fairly tight
124 * before overflowing. Oh and currently we don't overflow but return
127 * How likely are collisions? Doing the appropriate math is beyond me
128 * and the Bronstein textbook. But running a test program to brute
129 * force collisions for a couple of days showed that on average the
130 * first collision occurs after 598M entries, with 290M being the
131 * smallest result. Obviously 21 entries could already cause a
132 * collision if all entries are carefully chosen.
134 static pgoff_t
hash_index(u32 hash
, int round
)
136 u32 i0_blocks
= I0_BLOCKS
;
137 u32 i1_blocks
= I1_BLOCKS
;
138 u32 i2_blocks
= I2_BLOCKS
;
139 u32 i3_blocks
= I3_BLOCKS
;
143 return hash
% i0_blocks
;
145 return i0_blocks
+ hash
% (i1_blocks
- i0_blocks
);
147 return i1_blocks
+ hash
% (i2_blocks
- i1_blocks
);
149 return i2_blocks
+ hash
% (i3_blocks
- i2_blocks
);
151 return i3_blocks
+ 16 * (hash
% (((1<<31) - i3_blocks
) / 16))
157 static struct page
*logfs_get_dd_page(struct inode
*dir
, struct dentry
*dentry
)
159 struct qstr
*name
= &dentry
->d_name
;
161 struct logfs_disk_dentry
*dd
;
162 u32 hash
= hash_32(name
->name
, name
->len
, 0);
166 if (name
->len
> LOGFS_MAX_NAMELEN
)
167 return ERR_PTR(-ENAMETOOLONG
);
169 for (round
= 0; round
< 20; round
++) {
170 index
= hash_index(hash
, round
);
172 if (beyond_eof(dir
, index
))
174 if (!logfs_exist_block(dir
, index
))
176 page
= read_cache_page(dir
->i_mapping
, index
,
177 (filler_t
*)logfs_readpage
, NULL
);
180 dd
= kmap_atomic(page
);
181 BUG_ON(dd
->namelen
== 0);
183 if (name
->len
!= be16_to_cpu(dd
->namelen
) ||
184 memcmp(name
->name
, dd
->name
, name
->len
)) {
186 page_cache_release(page
);
196 static int logfs_remove_inode(struct inode
*inode
)
201 ret
= write_inode(inode
);
202 LOGFS_BUG_ON(ret
, inode
->i_sb
);
206 static void abort_transaction(struct inode
*inode
, struct logfs_transaction
*ta
)
208 if (logfs_inode(inode
)->li_block
)
209 logfs_inode(inode
)->li_block
->ta
= NULL
;
213 static int logfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
215 struct logfs_super
*super
= logfs_super(dir
->i_sb
);
216 struct inode
*inode
= dentry
->d_inode
;
217 struct logfs_transaction
*ta
;
222 ta
= kzalloc(sizeof(*ta
), GFP_KERNEL
);
226 ta
->state
= UNLINK_1
;
227 ta
->ino
= inode
->i_ino
;
229 inode
->i_ctime
= dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
231 page
= logfs_get_dd_page(dir
, dentry
);
238 return PTR_ERR(page
);
241 page_cache_release(page
);
243 mutex_lock(&super
->s_dirop_mutex
);
244 logfs_add_transaction(dir
, ta
);
246 ret
= logfs_delete(dir
, index
, NULL
);
248 ret
= write_inode(dir
);
251 abort_transaction(dir
, ta
);
252 printk(KERN_ERR
"LOGFS: unable to delete inode\n");
256 ta
->state
= UNLINK_2
;
257 logfs_add_transaction(inode
, ta
);
258 ret
= logfs_remove_inode(inode
);
260 mutex_unlock(&super
->s_dirop_mutex
);
264 static inline int logfs_empty_dir(struct inode
*dir
)
268 data
= logfs_seek_data(dir
, 0) << dir
->i_sb
->s_blocksize_bits
;
269 return data
>= i_size_read(dir
);
272 static int logfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
274 struct inode
*inode
= dentry
->d_inode
;
276 if (!logfs_empty_dir(inode
))
279 return logfs_unlink(dir
, dentry
);
282 /* FIXME: readdir currently has it's own dir_walk code. I don't see a good
283 * way to combine the two copies */
284 static int logfs_readdir(struct file
*file
, struct dir_context
*ctx
)
286 struct inode
*dir
= file_inode(file
);
289 struct logfs_disk_dentry
*dd
;
294 if (!dir_emit_dots(file
, ctx
))
299 for (;; pos
++, ctx
->pos
++) {
301 if (beyond_eof(dir
, pos
))
303 if (!logfs_exist_block(dir
, pos
)) {
305 pos
= dir_seek_data(dir
, pos
);
308 page
= read_cache_page(dir
->i_mapping
, pos
,
309 (filler_t
*)logfs_readpage
, NULL
);
311 return PTR_ERR(page
);
313 BUG_ON(dd
->namelen
== 0);
315 full
= !dir_emit(ctx
, (char *)dd
->name
,
316 be16_to_cpu(dd
->namelen
),
317 be64_to_cpu(dd
->ino
), dd
->type
);
319 page_cache_release(page
);
326 static void logfs_set_name(struct logfs_disk_dentry
*dd
, struct qstr
*name
)
328 dd
->namelen
= cpu_to_be16(name
->len
);
329 memcpy(dd
->name
, name
->name
, name
->len
);
332 static struct dentry
*logfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
336 struct logfs_disk_dentry
*dd
;
341 page
= logfs_get_dd_page(dir
, dentry
);
343 return ERR_CAST(page
);
349 dd
= kmap_atomic(page
);
350 ino
= be64_to_cpu(dd
->ino
);
352 page_cache_release(page
);
354 inode
= logfs_iget(dir
->i_sb
, ino
);
356 printk(KERN_ERR
"LogFS: Cannot read inode #%llx for dentry (%lx, %lx)n",
357 ino
, dir
->i_ino
, index
);
358 return d_splice_alias(inode
, dentry
);
361 static void grow_dir(struct inode
*dir
, loff_t index
)
363 index
= (index
+ 1) << dir
->i_sb
->s_blocksize_bits
;
364 if (i_size_read(dir
) < index
)
365 i_size_write(dir
, index
);
368 static int logfs_write_dir(struct inode
*dir
, struct dentry
*dentry
,
372 struct logfs_disk_dentry
*dd
;
373 u32 hash
= hash_32(dentry
->d_name
.name
, dentry
->d_name
.len
, 0);
377 for (round
= 0; round
< 20; round
++) {
378 index
= hash_index(hash
, round
);
380 if (logfs_exist_block(dir
, index
))
382 page
= find_or_create_page(dir
->i_mapping
, index
, GFP_KERNEL
);
386 dd
= kmap_atomic(page
);
387 memset(dd
, 0, sizeof(*dd
));
388 dd
->ino
= cpu_to_be64(inode
->i_ino
);
389 dd
->type
= logfs_type(inode
);
390 logfs_set_name(dd
, &dentry
->d_name
);
393 err
= logfs_write_buf(dir
, page
, WF_LOCK
);
395 page_cache_release(page
);
397 grow_dir(dir
, index
);
400 /* FIXME: Is there a better return value? In most cases neither
401 * the filesystem nor the directory are full. But we have had
402 * too many collisions for this particular hash and no fallback.
407 static int __logfs_create(struct inode
*dir
, struct dentry
*dentry
,
408 struct inode
*inode
, const char *dest
, long destlen
)
410 struct logfs_super
*super
= logfs_super(dir
->i_sb
);
411 struct logfs_inode
*li
= logfs_inode(inode
);
412 struct logfs_transaction
*ta
;
415 ta
= kzalloc(sizeof(*ta
), GFP_KERNEL
);
422 ta
->state
= CREATE_1
;
423 ta
->ino
= inode
->i_ino
;
424 mutex_lock(&super
->s_dirop_mutex
);
425 logfs_add_transaction(inode
, ta
);
429 ret
= logfs_inode_write(inode
, dest
, destlen
, 0, WF_LOCK
, NULL
);
431 ret
= write_inode(inode
);
433 /* creat/mkdir/mknod */
434 ret
= write_inode(inode
);
437 abort_transaction(inode
, ta
);
438 li
->li_flags
|= LOGFS_IF_STILLBORN
;
439 /* FIXME: truncate symlink */
445 ta
->state
= CREATE_2
;
446 logfs_add_transaction(dir
, ta
);
447 ret
= logfs_write_dir(dir
, dentry
, inode
);
450 ret
= write_inode(dir
);
453 logfs_del_transaction(dir
, ta
);
454 ta
->state
= CREATE_2
;
455 logfs_add_transaction(inode
, ta
);
456 logfs_remove_inode(inode
);
460 d_instantiate(dentry
, inode
);
462 mutex_unlock(&super
->s_dirop_mutex
);
466 static int logfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
471 * FIXME: why do we have to fill in S_IFDIR, while the mode is
472 * correct for mknod, creat, etc.? Smells like the vfs *should*
473 * do it for us but for some reason fails to do so.
475 inode
= logfs_new_inode(dir
, S_IFDIR
| mode
);
477 return PTR_ERR(inode
);
479 inode
->i_op
= &logfs_dir_iops
;
480 inode
->i_fop
= &logfs_dir_fops
;
482 return __logfs_create(dir
, dentry
, inode
, NULL
, 0);
485 static int logfs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
490 inode
= logfs_new_inode(dir
, mode
);
492 return PTR_ERR(inode
);
494 inode
->i_op
= &logfs_reg_iops
;
495 inode
->i_fop
= &logfs_reg_fops
;
496 inode
->i_mapping
->a_ops
= &logfs_reg_aops
;
498 return __logfs_create(dir
, dentry
, inode
, NULL
, 0);
501 static int logfs_mknod(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
506 if (dentry
->d_name
.len
> LOGFS_MAX_NAMELEN
)
507 return -ENAMETOOLONG
;
509 inode
= logfs_new_inode(dir
, mode
);
511 return PTR_ERR(inode
);
513 init_special_inode(inode
, mode
, rdev
);
515 return __logfs_create(dir
, dentry
, inode
, NULL
, 0);
518 static int logfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
522 size_t destlen
= strlen(target
) + 1;
524 if (destlen
> dir
->i_sb
->s_blocksize
)
525 return -ENAMETOOLONG
;
527 inode
= logfs_new_inode(dir
, S_IFLNK
| 0777);
529 return PTR_ERR(inode
);
531 inode
->i_op
= &logfs_symlink_iops
;
532 inode
->i_mapping
->a_ops
= &logfs_reg_aops
;
534 return __logfs_create(dir
, dentry
, inode
, target
, destlen
);
537 static int logfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
538 struct dentry
*dentry
)
540 struct inode
*inode
= old_dentry
->d_inode
;
542 inode
->i_ctime
= dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
545 mark_inode_dirty_sync(inode
);
547 return __logfs_create(dir
, dentry
, inode
, NULL
, 0);
550 static int logfs_get_dd(struct inode
*dir
, struct dentry
*dentry
,
551 struct logfs_disk_dentry
*dd
, loff_t
*pos
)
556 page
= logfs_get_dd_page(dir
, dentry
);
558 return PTR_ERR(page
);
560 map
= kmap_atomic(page
);
561 memcpy(dd
, map
, sizeof(*dd
));
563 page_cache_release(page
);
567 static int logfs_delete_dd(struct inode
*dir
, loff_t pos
)
570 * Getting called with pos somewhere beyond eof is either a goofup
571 * within this file or means someone maliciously edited the
572 * (crc-protected) journal.
574 BUG_ON(beyond_eof(dir
, pos
));
575 dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
576 log_dir(" Delete dentry (%lx, %llx)\n", dir
->i_ino
, pos
);
577 return logfs_delete(dir
, pos
, NULL
);
581 * Cross-directory rename, target does not exist. Just a little nasty.
582 * Create a new dentry in the target dir, then remove the old dentry,
583 * all the while taking care to remember our operation in the journal.
585 static int logfs_rename_cross(struct inode
*old_dir
, struct dentry
*old_dentry
,
586 struct inode
*new_dir
, struct dentry
*new_dentry
)
588 struct logfs_super
*super
= logfs_super(old_dir
->i_sb
);
589 struct logfs_disk_dentry dd
;
590 struct logfs_transaction
*ta
;
594 /* 1. locate source dd */
595 err
= logfs_get_dd(old_dir
, old_dentry
, &dd
, &pos
);
599 ta
= kzalloc(sizeof(*ta
), GFP_KERNEL
);
603 ta
->state
= CROSS_RENAME_1
;
604 ta
->dir
= old_dir
->i_ino
;
607 /* 2. write target dd */
608 mutex_lock(&super
->s_dirop_mutex
);
609 logfs_add_transaction(new_dir
, ta
);
610 err
= logfs_write_dir(new_dir
, new_dentry
, old_dentry
->d_inode
);
612 err
= write_inode(new_dir
);
615 super
->s_rename_dir
= 0;
616 super
->s_rename_pos
= 0;
617 abort_transaction(new_dir
, ta
);
621 /* 3. remove source dd */
622 ta
->state
= CROSS_RENAME_2
;
623 logfs_add_transaction(old_dir
, ta
);
624 err
= logfs_delete_dd(old_dir
, pos
);
626 err
= write_inode(old_dir
);
627 LOGFS_BUG_ON(err
, old_dir
->i_sb
);
629 mutex_unlock(&super
->s_dirop_mutex
);
633 static int logfs_replace_inode(struct inode
*dir
, struct dentry
*dentry
,
634 struct logfs_disk_dentry
*dd
, struct inode
*inode
)
639 err
= logfs_get_dd(dir
, dentry
, dd
, &pos
);
642 dd
->ino
= cpu_to_be64(inode
->i_ino
);
643 dd
->type
= logfs_type(inode
);
645 err
= write_dir(dir
, dd
, pos
);
648 log_dir("Replace dentry (%lx, %llx) %s -> %llx\n", dir
->i_ino
, pos
,
649 dd
->name
, be64_to_cpu(dd
->ino
));
650 return write_inode(dir
);
653 /* Target dentry exists - the worst case. We need to attach the source
654 * inode to the target dentry, then remove the orphaned target inode and
657 static int logfs_rename_target(struct inode
*old_dir
, struct dentry
*old_dentry
,
658 struct inode
*new_dir
, struct dentry
*new_dentry
)
660 struct logfs_super
*super
= logfs_super(old_dir
->i_sb
);
661 struct inode
*old_inode
= old_dentry
->d_inode
;
662 struct inode
*new_inode
= new_dentry
->d_inode
;
663 int isdir
= S_ISDIR(old_inode
->i_mode
);
664 struct logfs_disk_dentry dd
;
665 struct logfs_transaction
*ta
;
669 BUG_ON(isdir
!= S_ISDIR(new_inode
->i_mode
));
671 if (!logfs_empty_dir(new_inode
))
675 /* 1. locate source dd */
676 err
= logfs_get_dd(old_dir
, old_dentry
, &dd
, &pos
);
680 ta
= kzalloc(sizeof(*ta
), GFP_KERNEL
);
684 ta
->state
= TARGET_RENAME_1
;
685 ta
->dir
= old_dir
->i_ino
;
687 ta
->ino
= new_inode
->i_ino
;
689 /* 2. attach source inode to target dd */
690 mutex_lock(&super
->s_dirop_mutex
);
691 logfs_add_transaction(new_dir
, ta
);
692 err
= logfs_replace_inode(new_dir
, new_dentry
, &dd
, old_inode
);
694 super
->s_rename_dir
= 0;
695 super
->s_rename_pos
= 0;
696 super
->s_victim_ino
= 0;
697 abort_transaction(new_dir
, ta
);
701 /* 3. remove source dd */
702 ta
->state
= TARGET_RENAME_2
;
703 logfs_add_transaction(old_dir
, ta
);
704 err
= logfs_delete_dd(old_dir
, pos
);
706 err
= write_inode(old_dir
);
707 LOGFS_BUG_ON(err
, old_dir
->i_sb
);
709 /* 4. remove target inode */
710 ta
->state
= TARGET_RENAME_3
;
711 logfs_add_transaction(new_inode
, ta
);
712 err
= logfs_remove_inode(new_inode
);
715 mutex_unlock(&super
->s_dirop_mutex
);
719 static int logfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
720 struct inode
*new_dir
, struct dentry
*new_dentry
)
722 if (new_dentry
->d_inode
)
723 return logfs_rename_target(old_dir
, old_dentry
,
724 new_dir
, new_dentry
);
725 return logfs_rename_cross(old_dir
, old_dentry
, new_dir
, new_dentry
);
728 /* No locking done here, as this is called before .get_sb() returns. */
729 int logfs_replay_journal(struct super_block
*sb
)
731 struct logfs_super
*super
= logfs_super(sb
);
736 if (super
->s_victim_ino
) {
737 /* delete victim inode */
738 ino
= super
->s_victim_ino
;
739 printk(KERN_INFO
"LogFS: delete unmapped inode #%llx\n", ino
);
740 inode
= logfs_iget(sb
, ino
);
744 LOGFS_BUG_ON(i_size_read(inode
) > 0, sb
);
745 super
->s_victim_ino
= 0;
746 err
= logfs_remove_inode(inode
);
749 super
->s_victim_ino
= ino
;
753 if (super
->s_rename_dir
) {
754 /* delete old dd from rename */
755 ino
= super
->s_rename_dir
;
756 pos
= super
->s_rename_pos
;
757 printk(KERN_INFO
"LogFS: delete unbacked dentry (%llx, %llx)\n",
759 inode
= logfs_iget(sb
, ino
);
763 super
->s_rename_dir
= 0;
764 super
->s_rename_pos
= 0;
765 err
= logfs_delete_dd(inode
, pos
);
768 super
->s_rename_dir
= ino
;
769 super
->s_rename_pos
= pos
;
779 const struct inode_operations logfs_symlink_iops
= {
780 .readlink
= generic_readlink
,
781 .follow_link
= page_follow_link_light
,
784 const struct inode_operations logfs_dir_iops
= {
785 .create
= logfs_create
,
787 .lookup
= logfs_lookup
,
788 .mkdir
= logfs_mkdir
,
789 .mknod
= logfs_mknod
,
790 .rename
= logfs_rename
,
791 .rmdir
= logfs_rmdir
,
792 .symlink
= logfs_symlink
,
793 .unlink
= logfs_unlink
,
795 const struct file_operations logfs_dir_fops
= {
796 .fsync
= logfs_fsync
,
797 .unlocked_ioctl
= logfs_ioctl
,
798 .iterate
= logfs_readdir
,
799 .read
= generic_read_dir
,
800 .llseek
= default_llseek
,