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