Merge branch 'docs-move' of git://git.kernel.org/pub/scm/linux/kernel/git/rdunlap...
[pandora-kernel.git] / fs / logfs / dir.c
1 /*
2  * fs/logfs/dir.c       - directory-related code
3  *
4  * As should be obvious for Linux kernel code, license is GPLv2
5  *
6  * Copyright (c) 2005-2008 Joern Engel <joern@logfs.org>
7  */
8 #include "logfs.h"
9 #include <linux/slab.h>
10
11 /*
12  * Atomic dir operations
13  *
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.
17  *
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)
22  *
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.
27  *
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)
32  *
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.
35  *
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.
41  *
42  * Local rename is atomic, as the old dentry is simply rewritten with a new
43  * name.
44  *
45  * Cross-directory rename works in two steps, similar to __logfs_create and
46  * logfs_unlink:
47  * 1. Write new dentry (remember old dentry in journal)
48  * 2. Remove old dentry (clear journal)
49  *
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.
53  *
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
58  *
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.
64  */
65
66 static int write_dir(struct inode *dir, struct logfs_disk_dentry *dd,
67                 loff_t pos)
68 {
69         return logfs_inode_write(dir, dd, sizeof(*dd), pos, WF_LOCK, NULL);
70 }
71
72 static int write_inode(struct inode *inode)
73 {
74         return __logfs_write_inode(inode, WF_LOCK);
75 }
76
77 static s64 dir_seek_data(struct inode *inode, s64 pos)
78 {
79         s64 new_pos = logfs_seek_data(inode, pos);
80
81         return max(pos, new_pos - 1);
82 }
83
84 static int beyond_eof(struct inode *inode, loff_t bix)
85 {
86         loff_t pos = bix << inode->i_sb->s_blocksize_bits;
87         return pos >= i_size_read(inode);
88 }
89
90 /*
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
96  * one.
97  */
98 static u32 hash_32(const char *s, int len, u32 seed)
99 {
100         u32 hash = seed;
101         int i;
102
103         for (i = 0; i < len; i++)
104                 hash = hash * 293 + s[i];
105         return hash;
106 }
107
108 /*
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
114  * be unhappy.
115  *
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.
121  *
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
125  * and error.
126  *
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.
133  */
134 static pgoff_t hash_index(u32 hash, int round)
135 {
136         u32 i0_blocks = I0_BLOCKS;
137         u32 i1_blocks = I1_BLOCKS;
138         u32 i2_blocks = I2_BLOCKS;
139         u32 i3_blocks = I3_BLOCKS;
140
141         switch (round) {
142         case 0:
143                 return hash % i0_blocks;
144         case 1:
145                 return i0_blocks + hash % (i1_blocks - i0_blocks);
146         case 2:
147                 return i1_blocks + hash % (i2_blocks - i1_blocks);
148         case 3:
149                 return i2_blocks + hash % (i3_blocks - i2_blocks);
150         case 4 ... 19:
151                 return i3_blocks + 16 * (hash % (((1<<31) - i3_blocks) / 16))
152                         + round - 4;
153         }
154         BUG();
155 }
156
157 static struct page *logfs_get_dd_page(struct inode *dir, struct dentry *dentry)
158 {
159         struct qstr *name = &dentry->d_name;
160         struct page *page;
161         struct logfs_disk_dentry *dd;
162         u32 hash = hash_32(name->name, name->len, 0);
163         pgoff_t index;
164         int round;
165
166         if (name->len > LOGFS_MAX_NAMELEN)
167                 return ERR_PTR(-ENAMETOOLONG);
168
169         for (round = 0; round < 20; round++) {
170                 index = hash_index(hash, round);
171
172                 if (beyond_eof(dir, index))
173                         return NULL;
174                 if (!logfs_exist_block(dir, index))
175                         continue;
176                 page = read_cache_page(dir->i_mapping, index,
177                                 (filler_t *)logfs_readpage, NULL);
178                 if (IS_ERR(page))
179                         return page;
180                 dd = kmap_atomic(page, KM_USER0);
181                 BUG_ON(dd->namelen == 0);
182
183                 if (name->len != be16_to_cpu(dd->namelen) ||
184                                 memcmp(name->name, dd->name, name->len)) {
185                         kunmap_atomic(dd, KM_USER0);
186                         page_cache_release(page);
187                         continue;
188                 }
189
190                 kunmap_atomic(dd, KM_USER0);
191                 return page;
192         }
193         return NULL;
194 }
195
196 static int logfs_remove_inode(struct inode *inode)
197 {
198         int ret;
199
200         inode->i_nlink--;
201         ret = write_inode(inode);
202         LOGFS_BUG_ON(ret, inode->i_sb);
203         return ret;
204 }
205
206 static void abort_transaction(struct inode *inode, struct logfs_transaction *ta)
207 {
208         if (logfs_inode(inode)->li_block)
209                 logfs_inode(inode)->li_block->ta = NULL;
210         kfree(ta);
211 }
212
213 static int logfs_unlink(struct inode *dir, struct dentry *dentry)
214 {
215         struct logfs_super *super = logfs_super(dir->i_sb);
216         struct inode *inode = dentry->d_inode;
217         struct logfs_transaction *ta;
218         struct page *page;
219         pgoff_t index;
220         int ret;
221
222         ta = kzalloc(sizeof(*ta), GFP_KERNEL);
223         if (!ta)
224                 return -ENOMEM;
225
226         ta->state = UNLINK_1;
227         ta->ino = inode->i_ino;
228
229         inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
230
231         page = logfs_get_dd_page(dir, dentry);
232         if (!page) {
233                 kfree(ta);
234                 return -ENOENT;
235         }
236         if (IS_ERR(page)) {
237                 kfree(ta);
238                 return PTR_ERR(page);
239         }
240         index = page->index;
241         page_cache_release(page);
242
243         mutex_lock(&super->s_dirop_mutex);
244         logfs_add_transaction(dir, ta);
245
246         ret = logfs_delete(dir, index, NULL);
247         if (!ret)
248                 ret = write_inode(dir);
249
250         if (ret) {
251                 abort_transaction(dir, ta);
252                 printk(KERN_ERR"LOGFS: unable to delete inode\n");
253                 goto out;
254         }
255
256         ta->state = UNLINK_2;
257         logfs_add_transaction(inode, ta);
258         ret = logfs_remove_inode(inode);
259 out:
260         mutex_unlock(&super->s_dirop_mutex);
261         return ret;
262 }
263
264 static inline int logfs_empty_dir(struct inode *dir)
265 {
266         u64 data;
267
268         data = logfs_seek_data(dir, 0) << dir->i_sb->s_blocksize_bits;
269         return data >= i_size_read(dir);
270 }
271
272 static int logfs_rmdir(struct inode *dir, struct dentry *dentry)
273 {
274         struct inode *inode = dentry->d_inode;
275
276         dentry_unhash(dentry);
277
278         if (!logfs_empty_dir(inode))
279                 return -ENOTEMPTY;
280
281         return logfs_unlink(dir, dentry);
282 }
283
284 /* FIXME: readdir currently has it's own dir_walk code.  I don't see a good
285  * way to combine the two copies */
286 #define IMPLICIT_NODES 2
287 static int __logfs_readdir(struct file *file, void *buf, filldir_t filldir)
288 {
289         struct inode *dir = file->f_dentry->d_inode;
290         loff_t pos = file->f_pos - IMPLICIT_NODES;
291         struct page *page;
292         struct logfs_disk_dentry *dd;
293         int full;
294
295         BUG_ON(pos < 0);
296         for (;; pos++) {
297                 if (beyond_eof(dir, pos))
298                         break;
299                 if (!logfs_exist_block(dir, pos)) {
300                         /* deleted dentry */
301                         pos = dir_seek_data(dir, pos);
302                         continue;
303                 }
304                 page = read_cache_page(dir->i_mapping, pos,
305                                 (filler_t *)logfs_readpage, NULL);
306                 if (IS_ERR(page))
307                         return PTR_ERR(page);
308                 dd = kmap(page);
309                 BUG_ON(dd->namelen == 0);
310
311                 full = filldir(buf, (char *)dd->name, be16_to_cpu(dd->namelen),
312                                 pos, be64_to_cpu(dd->ino), dd->type);
313                 kunmap(page);
314                 page_cache_release(page);
315                 if (full)
316                         break;
317         }
318
319         file->f_pos = pos + IMPLICIT_NODES;
320         return 0;
321 }
322
323 static int logfs_readdir(struct file *file, void *buf, filldir_t filldir)
324 {
325         struct inode *inode = file->f_dentry->d_inode;
326         ino_t pino = parent_ino(file->f_dentry);
327         int err;
328
329         if (file->f_pos < 0)
330                 return -EINVAL;
331
332         if (file->f_pos == 0) {
333                 if (filldir(buf, ".", 1, 1, inode->i_ino, DT_DIR) < 0)
334                         return 0;
335                 file->f_pos++;
336         }
337         if (file->f_pos == 1) {
338                 if (filldir(buf, "..", 2, 2, pino, DT_DIR) < 0)
339                         return 0;
340                 file->f_pos++;
341         }
342
343         err = __logfs_readdir(file, buf, filldir);
344         return err;
345 }
346
347 static void logfs_set_name(struct logfs_disk_dentry *dd, struct qstr *name)
348 {
349         dd->namelen = cpu_to_be16(name->len);
350         memcpy(dd->name, name->name, name->len);
351 }
352
353 static struct dentry *logfs_lookup(struct inode *dir, struct dentry *dentry,
354                 struct nameidata *nd)
355 {
356         struct page *page;
357         struct logfs_disk_dentry *dd;
358         pgoff_t index;
359         u64 ino = 0;
360         struct inode *inode;
361
362         page = logfs_get_dd_page(dir, dentry);
363         if (IS_ERR(page))
364                 return ERR_CAST(page);
365         if (!page) {
366                 d_add(dentry, NULL);
367                 return NULL;
368         }
369         index = page->index;
370         dd = kmap_atomic(page, KM_USER0);
371         ino = be64_to_cpu(dd->ino);
372         kunmap_atomic(dd, KM_USER0);
373         page_cache_release(page);
374
375         inode = logfs_iget(dir->i_sb, ino);
376         if (IS_ERR(inode)) {
377                 printk(KERN_ERR"LogFS: Cannot read inode #%llx for dentry (%lx, %lx)n",
378                                 ino, dir->i_ino, index);
379                 return ERR_CAST(inode);
380         }
381         return d_splice_alias(inode, dentry);
382 }
383
384 static void grow_dir(struct inode *dir, loff_t index)
385 {
386         index = (index + 1) << dir->i_sb->s_blocksize_bits;
387         if (i_size_read(dir) < index)
388                 i_size_write(dir, index);
389 }
390
391 static int logfs_write_dir(struct inode *dir, struct dentry *dentry,
392                 struct inode *inode)
393 {
394         struct page *page;
395         struct logfs_disk_dentry *dd;
396         u32 hash = hash_32(dentry->d_name.name, dentry->d_name.len, 0);
397         pgoff_t index;
398         int round, err;
399
400         for (round = 0; round < 20; round++) {
401                 index = hash_index(hash, round);
402
403                 if (logfs_exist_block(dir, index))
404                         continue;
405                 page = find_or_create_page(dir->i_mapping, index, GFP_KERNEL);
406                 if (!page)
407                         return -ENOMEM;
408
409                 dd = kmap_atomic(page, KM_USER0);
410                 memset(dd, 0, sizeof(*dd));
411                 dd->ino = cpu_to_be64(inode->i_ino);
412                 dd->type = logfs_type(inode);
413                 logfs_set_name(dd, &dentry->d_name);
414                 kunmap_atomic(dd, KM_USER0);
415
416                 err = logfs_write_buf(dir, page, WF_LOCK);
417                 unlock_page(page);
418                 page_cache_release(page);
419                 if (!err)
420                         grow_dir(dir, index);
421                 return err;
422         }
423         /* FIXME: Is there a better return value?  In most cases neither
424          * the filesystem nor the directory are full.  But we have had
425          * too many collisions for this particular hash and no fallback.
426          */
427         return -ENOSPC;
428 }
429
430 static int __logfs_create(struct inode *dir, struct dentry *dentry,
431                 struct inode *inode, const char *dest, long destlen)
432 {
433         struct logfs_super *super = logfs_super(dir->i_sb);
434         struct logfs_inode *li = logfs_inode(inode);
435         struct logfs_transaction *ta;
436         int ret;
437
438         ta = kzalloc(sizeof(*ta), GFP_KERNEL);
439         if (!ta) {
440                 inode->i_nlink--;
441                 iput(inode);
442                 return -ENOMEM;
443         }
444
445         ta->state = CREATE_1;
446         ta->ino = inode->i_ino;
447         mutex_lock(&super->s_dirop_mutex);
448         logfs_add_transaction(inode, ta);
449
450         if (dest) {
451                 /* symlink */
452                 ret = logfs_inode_write(inode, dest, destlen, 0, WF_LOCK, NULL);
453                 if (!ret)
454                         ret = write_inode(inode);
455         } else {
456                 /* creat/mkdir/mknod */
457                 ret = write_inode(inode);
458         }
459         if (ret) {
460                 abort_transaction(inode, ta);
461                 li->li_flags |= LOGFS_IF_STILLBORN;
462                 /* FIXME: truncate symlink */
463                 inode->i_nlink--;
464                 iput(inode);
465                 goto out;
466         }
467
468         ta->state = CREATE_2;
469         logfs_add_transaction(dir, ta);
470         ret = logfs_write_dir(dir, dentry, inode);
471         /* sync directory */
472         if (!ret)
473                 ret = write_inode(dir);
474
475         if (ret) {
476                 logfs_del_transaction(dir, ta);
477                 ta->state = CREATE_2;
478                 logfs_add_transaction(inode, ta);
479                 logfs_remove_inode(inode);
480                 iput(inode);
481                 goto out;
482         }
483         d_instantiate(dentry, inode);
484 out:
485         mutex_unlock(&super->s_dirop_mutex);
486         return ret;
487 }
488
489 static int logfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
490 {
491         struct inode *inode;
492
493         /*
494          * FIXME: why do we have to fill in S_IFDIR, while the mode is
495          * correct for mknod, creat, etc.?  Smells like the vfs *should*
496          * do it for us but for some reason fails to do so.
497          */
498         inode = logfs_new_inode(dir, S_IFDIR | mode);
499         if (IS_ERR(inode))
500                 return PTR_ERR(inode);
501
502         inode->i_op = &logfs_dir_iops;
503         inode->i_fop = &logfs_dir_fops;
504
505         return __logfs_create(dir, dentry, inode, NULL, 0);
506 }
507
508 static int logfs_create(struct inode *dir, struct dentry *dentry, int mode,
509                 struct nameidata *nd)
510 {
511         struct inode *inode;
512
513         inode = logfs_new_inode(dir, mode);
514         if (IS_ERR(inode))
515                 return PTR_ERR(inode);
516
517         inode->i_op = &logfs_reg_iops;
518         inode->i_fop = &logfs_reg_fops;
519         inode->i_mapping->a_ops = &logfs_reg_aops;
520
521         return __logfs_create(dir, dentry, inode, NULL, 0);
522 }
523
524 static int logfs_mknod(struct inode *dir, struct dentry *dentry, int mode,
525                 dev_t rdev)
526 {
527         struct inode *inode;
528
529         if (dentry->d_name.len > LOGFS_MAX_NAMELEN)
530                 return -ENAMETOOLONG;
531
532         inode = logfs_new_inode(dir, mode);
533         if (IS_ERR(inode))
534                 return PTR_ERR(inode);
535
536         init_special_inode(inode, mode, rdev);
537
538         return __logfs_create(dir, dentry, inode, NULL, 0);
539 }
540
541 static int logfs_symlink(struct inode *dir, struct dentry *dentry,
542                 const char *target)
543 {
544         struct inode *inode;
545         size_t destlen = strlen(target) + 1;
546
547         if (destlen > dir->i_sb->s_blocksize)
548                 return -ENAMETOOLONG;
549
550         inode = logfs_new_inode(dir, S_IFLNK | 0777);
551         if (IS_ERR(inode))
552                 return PTR_ERR(inode);
553
554         inode->i_op = &logfs_symlink_iops;
555         inode->i_mapping->a_ops = &logfs_reg_aops;
556
557         return __logfs_create(dir, dentry, inode, target, destlen);
558 }
559
560 static int logfs_permission(struct inode *inode, int mask, unsigned int flags)
561 {
562         if (flags & IPERM_FLAG_RCU)
563                 return -ECHILD;
564         return generic_permission(inode, mask, flags, NULL);
565 }
566
567 static int logfs_link(struct dentry *old_dentry, struct inode *dir,
568                 struct dentry *dentry)
569 {
570         struct inode *inode = old_dentry->d_inode;
571
572         if (inode->i_nlink >= LOGFS_LINK_MAX)
573                 return -EMLINK;
574
575         inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
576         ihold(inode);
577         inode->i_nlink++;
578         mark_inode_dirty_sync(inode);
579
580         return __logfs_create(dir, dentry, inode, NULL, 0);
581 }
582
583 static int logfs_get_dd(struct inode *dir, struct dentry *dentry,
584                 struct logfs_disk_dentry *dd, loff_t *pos)
585 {
586         struct page *page;
587         void *map;
588
589         page = logfs_get_dd_page(dir, dentry);
590         if (IS_ERR(page))
591                 return PTR_ERR(page);
592         *pos = page->index;
593         map = kmap_atomic(page, KM_USER0);
594         memcpy(dd, map, sizeof(*dd));
595         kunmap_atomic(map, KM_USER0);
596         page_cache_release(page);
597         return 0;
598 }
599
600 static int logfs_delete_dd(struct inode *dir, loff_t pos)
601 {
602         /*
603          * Getting called with pos somewhere beyond eof is either a goofup
604          * within this file or means someone maliciously edited the
605          * (crc-protected) journal.
606          */
607         BUG_ON(beyond_eof(dir, pos));
608         dir->i_ctime = dir->i_mtime = CURRENT_TIME;
609         log_dir(" Delete dentry (%lx, %llx)\n", dir->i_ino, pos);
610         return logfs_delete(dir, pos, NULL);
611 }
612
613 /*
614  * Cross-directory rename, target does not exist.  Just a little nasty.
615  * Create a new dentry in the target dir, then remove the old dentry,
616  * all the while taking care to remember our operation in the journal.
617  */
618 static int logfs_rename_cross(struct inode *old_dir, struct dentry *old_dentry,
619                               struct inode *new_dir, struct dentry *new_dentry)
620 {
621         struct logfs_super *super = logfs_super(old_dir->i_sb);
622         struct logfs_disk_dentry dd;
623         struct logfs_transaction *ta;
624         loff_t pos;
625         int err;
626
627         if (new_dentry->d_inode && S_ISDIR(new_dentry->d_inode->i_mode))
628                 dentry_unhash(new_dentry);
629
630         /* 1. locate source dd */
631         err = logfs_get_dd(old_dir, old_dentry, &dd, &pos);
632         if (err)
633                 return err;
634
635         ta = kzalloc(sizeof(*ta), GFP_KERNEL);
636         if (!ta)
637                 return -ENOMEM;
638
639         ta->state = CROSS_RENAME_1;
640         ta->dir = old_dir->i_ino;
641         ta->pos = pos;
642
643         /* 2. write target dd */
644         mutex_lock(&super->s_dirop_mutex);
645         logfs_add_transaction(new_dir, ta);
646         err = logfs_write_dir(new_dir, new_dentry, old_dentry->d_inode);
647         if (!err)
648                 err = write_inode(new_dir);
649
650         if (err) {
651                 super->s_rename_dir = 0;
652                 super->s_rename_pos = 0;
653                 abort_transaction(new_dir, ta);
654                 goto out;
655         }
656
657         /* 3. remove source dd */
658         ta->state = CROSS_RENAME_2;
659         logfs_add_transaction(old_dir, ta);
660         err = logfs_delete_dd(old_dir, pos);
661         if (!err)
662                 err = write_inode(old_dir);
663         LOGFS_BUG_ON(err, old_dir->i_sb);
664 out:
665         mutex_unlock(&super->s_dirop_mutex);
666         return err;
667 }
668
669 static int logfs_replace_inode(struct inode *dir, struct dentry *dentry,
670                 struct logfs_disk_dentry *dd, struct inode *inode)
671 {
672         loff_t pos;
673         int err;
674
675         err = logfs_get_dd(dir, dentry, dd, &pos);
676         if (err)
677                 return err;
678         dd->ino = cpu_to_be64(inode->i_ino);
679         dd->type = logfs_type(inode);
680
681         err = write_dir(dir, dd, pos);
682         if (err)
683                 return err;
684         log_dir("Replace dentry (%lx, %llx) %s -> %llx\n", dir->i_ino, pos,
685                         dd->name, be64_to_cpu(dd->ino));
686         return write_inode(dir);
687 }
688
689 /* Target dentry exists - the worst case.  We need to attach the source
690  * inode to the target dentry, then remove the orphaned target inode and
691  * source dentry.
692  */
693 static int logfs_rename_target(struct inode *old_dir, struct dentry *old_dentry,
694                                struct inode *new_dir, struct dentry *new_dentry)
695 {
696         struct logfs_super *super = logfs_super(old_dir->i_sb);
697         struct inode *old_inode = old_dentry->d_inode;
698         struct inode *new_inode = new_dentry->d_inode;
699         int isdir = S_ISDIR(old_inode->i_mode);
700         struct logfs_disk_dentry dd;
701         struct logfs_transaction *ta;
702         loff_t pos;
703         int err;
704
705         BUG_ON(isdir != S_ISDIR(new_inode->i_mode));
706         if (isdir) {
707                 if (!logfs_empty_dir(new_inode))
708                         return -ENOTEMPTY;
709         }
710
711         /* 1. locate source dd */
712         err = logfs_get_dd(old_dir, old_dentry, &dd, &pos);
713         if (err)
714                 return err;
715
716         ta = kzalloc(sizeof(*ta), GFP_KERNEL);
717         if (!ta)
718                 return -ENOMEM;
719
720         ta->state = TARGET_RENAME_1;
721         ta->dir = old_dir->i_ino;
722         ta->pos = pos;
723         ta->ino = new_inode->i_ino;
724
725         /* 2. attach source inode to target dd */
726         mutex_lock(&super->s_dirop_mutex);
727         logfs_add_transaction(new_dir, ta);
728         err = logfs_replace_inode(new_dir, new_dentry, &dd, old_inode);
729         if (err) {
730                 super->s_rename_dir = 0;
731                 super->s_rename_pos = 0;
732                 super->s_victim_ino = 0;
733                 abort_transaction(new_dir, ta);
734                 goto out;
735         }
736
737         /* 3. remove source dd */
738         ta->state = TARGET_RENAME_2;
739         logfs_add_transaction(old_dir, ta);
740         err = logfs_delete_dd(old_dir, pos);
741         if (!err)
742                 err = write_inode(old_dir);
743         LOGFS_BUG_ON(err, old_dir->i_sb);
744
745         /* 4. remove target inode */
746         ta->state = TARGET_RENAME_3;
747         logfs_add_transaction(new_inode, ta);
748         err = logfs_remove_inode(new_inode);
749
750 out:
751         mutex_unlock(&super->s_dirop_mutex);
752         return err;
753 }
754
755 static int logfs_rename(struct inode *old_dir, struct dentry *old_dentry,
756                         struct inode *new_dir, struct dentry *new_dentry)
757 {
758         if (new_dentry->d_inode)
759                 return logfs_rename_target(old_dir, old_dentry,
760                                            new_dir, new_dentry);
761         return logfs_rename_cross(old_dir, old_dentry, new_dir, new_dentry);
762 }
763
764 /* No locking done here, as this is called before .get_sb() returns. */
765 int logfs_replay_journal(struct super_block *sb)
766 {
767         struct logfs_super *super = logfs_super(sb);
768         struct inode *inode;
769         u64 ino, pos;
770         int err;
771
772         if (super->s_victim_ino) {
773                 /* delete victim inode */
774                 ino = super->s_victim_ino;
775                 printk(KERN_INFO"LogFS: delete unmapped inode #%llx\n", ino);
776                 inode = logfs_iget(sb, ino);
777                 if (IS_ERR(inode))
778                         goto fail;
779
780                 LOGFS_BUG_ON(i_size_read(inode) > 0, sb);
781                 super->s_victim_ino = 0;
782                 err = logfs_remove_inode(inode);
783                 iput(inode);
784                 if (err) {
785                         super->s_victim_ino = ino;
786                         goto fail;
787                 }
788         }
789         if (super->s_rename_dir) {
790                 /* delete old dd from rename */
791                 ino = super->s_rename_dir;
792                 pos = super->s_rename_pos;
793                 printk(KERN_INFO"LogFS: delete unbacked dentry (%llx, %llx)\n",
794                                 ino, pos);
795                 inode = logfs_iget(sb, ino);
796                 if (IS_ERR(inode))
797                         goto fail;
798
799                 super->s_rename_dir = 0;
800                 super->s_rename_pos = 0;
801                 err = logfs_delete_dd(inode, pos);
802                 iput(inode);
803                 if (err) {
804                         super->s_rename_dir = ino;
805                         super->s_rename_pos = pos;
806                         goto fail;
807                 }
808         }
809         return 0;
810 fail:
811         LOGFS_BUG(sb);
812         return -EIO;
813 }
814
815 const struct inode_operations logfs_symlink_iops = {
816         .readlink       = generic_readlink,
817         .follow_link    = page_follow_link_light,
818 };
819
820 const struct inode_operations logfs_dir_iops = {
821         .create         = logfs_create,
822         .link           = logfs_link,
823         .lookup         = logfs_lookup,
824         .mkdir          = logfs_mkdir,
825         .mknod          = logfs_mknod,
826         .rename         = logfs_rename,
827         .rmdir          = logfs_rmdir,
828         .permission     = logfs_permission,
829         .symlink        = logfs_symlink,
830         .unlink         = logfs_unlink,
831 };
832 const struct file_operations logfs_dir_fops = {
833         .fsync          = logfs_fsync,
834         .unlocked_ioctl = logfs_ioctl,
835         .readdir        = logfs_readdir,
836         .read           = generic_read_dir,
837         .llseek         = default_llseek,
838 };