blob: 501043e8966ce010bd77412c767461e99da411e8 [file] [log] [blame]
Joern Engel5db53f32009-11-20 20:13:39 +01001/*
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"
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Joern Engel5db53f32009-11-20 20:13:39 +010010
11/*
12 * Atomic dir operations
13 *
14 * Directory operations are by default not atomic. Dentries and Inodes are
Anand Gadiyara8cd4562010-05-10 14:51:19 +053015 * created/removed/altered in separate operations. Therefore we need to do
Joern Engel5db53f32009-11-20 20:13:39 +010016 * 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
Lucas De Marchi25985ed2011-03-30 22:57:33 -030095 * of each character and pick a prime nearby, preferably a bit-sparse
Joern Engel5db53f32009-11-20 20:13:39 +010096 * 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{
Joern Engel30835cd2009-11-28 13:14:08 +0100136 u32 i0_blocks = I0_BLOCKS;
137 u32 i1_blocks = I1_BLOCKS;
138 u32 i2_blocks = I2_BLOCKS;
139 u32 i3_blocks = I3_BLOCKS;
140
Joern Engel5db53f32009-11-20 20:13:39 +0100141 switch (round) {
142 case 0:
Joern Engel30835cd2009-11-28 13:14:08 +0100143 return hash % i0_blocks;
Joern Engel5db53f32009-11-20 20:13:39 +0100144 case 1:
Joern Engel30835cd2009-11-28 13:14:08 +0100145 return i0_blocks + hash % (i1_blocks - i0_blocks);
Joern Engel5db53f32009-11-20 20:13:39 +0100146 case 2:
Joern Engel30835cd2009-11-28 13:14:08 +0100147 return i1_blocks + hash % (i2_blocks - i1_blocks);
Joern Engel5db53f32009-11-20 20:13:39 +0100148 case 3:
Joern Engel30835cd2009-11-28 13:14:08 +0100149 return i2_blocks + hash % (i3_blocks - i2_blocks);
Joern Engel5db53f32009-11-20 20:13:39 +0100150 case 4 ... 19:
Joern Engel30835cd2009-11-28 13:14:08 +0100151 return i3_blocks + 16 * (hash % (((1<<31) - i3_blocks) / 16))
Joern Engel5db53f32009-11-20 20:13:39 +0100152 + round - 4;
153 }
154 BUG();
155}
156
157static 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
196static int logfs_remove_inode(struct inode *inode)
197{
198 int ret;
199
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200200 drop_nlink(inode);
Joern Engel5db53f32009-11-20 20:13:39 +0100201 ret = write_inode(inode);
202 LOGFS_BUG_ON(ret, inode->i_sb);
203 return ret;
204}
205
206static 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
213static 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);
Joern Engelddfd1f02009-11-23 14:29:12 +0100232 if (!page) {
233 kfree(ta);
Joern Engel5db53f32009-11-20 20:13:39 +0100234 return -ENOENT;
Joern Engelddfd1f02009-11-23 14:29:12 +0100235 }
236 if (IS_ERR(page)) {
237 kfree(ta);
Joern Engel5db53f32009-11-20 20:13:39 +0100238 return PTR_ERR(page);
Joern Engelddfd1f02009-11-23 14:29:12 +0100239 }
Joern Engel5db53f32009-11-20 20:13:39 +0100240 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);
259out:
260 mutex_unlock(&super->s_dirop_mutex);
261 return ret;
262}
263
264static 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
272static int logfs_rmdir(struct inode *dir, struct dentry *dentry)
273{
274 struct inode *inode = dentry->d_inode;
275
276 if (!logfs_empty_dir(inode))
277 return -ENOTEMPTY;
278
279 return logfs_unlink(dir, dentry);
280}
281
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#define IMPLICIT_NODES 2
285static int __logfs_readdir(struct file *file, void *buf, filldir_t filldir)
286{
287 struct inode *dir = file->f_dentry->d_inode;
288 loff_t pos = file->f_pos - IMPLICIT_NODES;
289 struct page *page;
290 struct logfs_disk_dentry *dd;
291 int full;
292
293 BUG_ON(pos < 0);
294 for (;; pos++) {
295 if (beyond_eof(dir, pos))
296 break;
297 if (!logfs_exist_block(dir, pos)) {
298 /* deleted dentry */
299 pos = dir_seek_data(dir, pos);
300 continue;
301 }
302 page = read_cache_page(dir->i_mapping, pos,
303 (filler_t *)logfs_readpage, NULL);
304 if (IS_ERR(page))
305 return PTR_ERR(page);
Joern Engele3260682010-03-17 16:00:07 +0100306 dd = kmap(page);
Joern Engel5db53f32009-11-20 20:13:39 +0100307 BUG_ON(dd->namelen == 0);
308
309 full = filldir(buf, (char *)dd->name, be16_to_cpu(dd->namelen),
310 pos, be64_to_cpu(dd->ino), dd->type);
Joern Engele3260682010-03-17 16:00:07 +0100311 kunmap(page);
Joern Engel5db53f32009-11-20 20:13:39 +0100312 page_cache_release(page);
313 if (full)
314 break;
315 }
316
317 file->f_pos = pos + IMPLICIT_NODES;
318 return 0;
319}
320
321static int logfs_readdir(struct file *file, void *buf, filldir_t filldir)
322{
323 struct inode *inode = file->f_dentry->d_inode;
324 ino_t pino = parent_ino(file->f_dentry);
325 int err;
326
327 if (file->f_pos < 0)
328 return -EINVAL;
329
330 if (file->f_pos == 0) {
331 if (filldir(buf, ".", 1, 1, inode->i_ino, DT_DIR) < 0)
332 return 0;
333 file->f_pos++;
334 }
335 if (file->f_pos == 1) {
336 if (filldir(buf, "..", 2, 2, pino, DT_DIR) < 0)
337 return 0;
338 file->f_pos++;
339 }
340
341 err = __logfs_readdir(file, buf, filldir);
342 return err;
343}
344
345static void logfs_set_name(struct logfs_disk_dentry *dd, struct qstr *name)
346{
347 dd->namelen = cpu_to_be16(name->len);
348 memcpy(dd->name, name->name, name->len);
349}
350
351static struct dentry *logfs_lookup(struct inode *dir, struct dentry *dentry,
352 struct nameidata *nd)
353{
354 struct page *page;
355 struct logfs_disk_dentry *dd;
356 pgoff_t index;
357 u64 ino = 0;
358 struct inode *inode;
359
360 page = logfs_get_dd_page(dir, dentry);
361 if (IS_ERR(page))
362 return ERR_CAST(page);
363 if (!page) {
364 d_add(dentry, NULL);
365 return NULL;
366 }
367 index = page->index;
368 dd = kmap_atomic(page, KM_USER0);
369 ino = be64_to_cpu(dd->ino);
370 kunmap_atomic(dd, KM_USER0);
371 page_cache_release(page);
372
373 inode = logfs_iget(dir->i_sb, ino);
Al Viroa9049372011-07-08 21:20:11 -0400374 if (IS_ERR(inode))
Joern Engel5db53f32009-11-20 20:13:39 +0100375 printk(KERN_ERR"LogFS: Cannot read inode #%llx for dentry (%lx, %lx)n",
376 ino, dir->i_ino, index);
Joern Engel5db53f32009-11-20 20:13:39 +0100377 return d_splice_alias(inode, dentry);
378}
379
380static void grow_dir(struct inode *dir, loff_t index)
381{
382 index = (index + 1) << dir->i_sb->s_blocksize_bits;
383 if (i_size_read(dir) < index)
384 i_size_write(dir, index);
385}
386
387static int logfs_write_dir(struct inode *dir, struct dentry *dentry,
388 struct inode *inode)
389{
390 struct page *page;
391 struct logfs_disk_dentry *dd;
392 u32 hash = hash_32(dentry->d_name.name, dentry->d_name.len, 0);
393 pgoff_t index;
394 int round, err;
395
396 for (round = 0; round < 20; round++) {
397 index = hash_index(hash, round);
398
399 if (logfs_exist_block(dir, index))
400 continue;
401 page = find_or_create_page(dir->i_mapping, index, GFP_KERNEL);
402 if (!page)
403 return -ENOMEM;
404
405 dd = kmap_atomic(page, KM_USER0);
406 memset(dd, 0, sizeof(*dd));
407 dd->ino = cpu_to_be64(inode->i_ino);
408 dd->type = logfs_type(inode);
409 logfs_set_name(dd, &dentry->d_name);
410 kunmap_atomic(dd, KM_USER0);
411
412 err = logfs_write_buf(dir, page, WF_LOCK);
413 unlock_page(page);
414 page_cache_release(page);
415 if (!err)
416 grow_dir(dir, index);
417 return err;
418 }
419 /* FIXME: Is there a better return value? In most cases neither
420 * the filesystem nor the directory are full. But we have had
421 * too many collisions for this particular hash and no fallback.
422 */
423 return -ENOSPC;
424}
425
426static int __logfs_create(struct inode *dir, struct dentry *dentry,
427 struct inode *inode, const char *dest, long destlen)
428{
429 struct logfs_super *super = logfs_super(dir->i_sb);
430 struct logfs_inode *li = logfs_inode(inode);
431 struct logfs_transaction *ta;
432 int ret;
433
434 ta = kzalloc(sizeof(*ta), GFP_KERNEL);
Al Viro25624952010-07-04 12:23:11 +0400435 if (!ta) {
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200436 drop_nlink(inode);
Al Viro25624952010-07-04 12:23:11 +0400437 iput(inode);
Joern Engel5db53f32009-11-20 20:13:39 +0100438 return -ENOMEM;
Al Viro25624952010-07-04 12:23:11 +0400439 }
Joern Engel5db53f32009-11-20 20:13:39 +0100440
441 ta->state = CREATE_1;
442 ta->ino = inode->i_ino;
443 mutex_lock(&super->s_dirop_mutex);
444 logfs_add_transaction(inode, ta);
445
446 if (dest) {
447 /* symlink */
448 ret = logfs_inode_write(inode, dest, destlen, 0, WF_LOCK, NULL);
449 if (!ret)
450 ret = write_inode(inode);
451 } else {
452 /* creat/mkdir/mknod */
453 ret = write_inode(inode);
454 }
455 if (ret) {
456 abort_transaction(inode, ta);
457 li->li_flags |= LOGFS_IF_STILLBORN;
458 /* FIXME: truncate symlink */
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200459 drop_nlink(inode);
Joern Engel5db53f32009-11-20 20:13:39 +0100460 iput(inode);
461 goto out;
462 }
463
464 ta->state = CREATE_2;
465 logfs_add_transaction(dir, ta);
466 ret = logfs_write_dir(dir, dentry, inode);
467 /* sync directory */
468 if (!ret)
469 ret = write_inode(dir);
470
471 if (ret) {
472 logfs_del_transaction(dir, ta);
473 ta->state = CREATE_2;
474 logfs_add_transaction(inode, ta);
475 logfs_remove_inode(inode);
476 iput(inode);
477 goto out;
478 }
479 d_instantiate(dentry, inode);
480out:
481 mutex_unlock(&super->s_dirop_mutex);
482 return ret;
483}
484
Al Viro18bb1db2011-07-26 01:41:39 -0400485static int logfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Joern Engel5db53f32009-11-20 20:13:39 +0100486{
487 struct inode *inode;
488
489 /*
490 * FIXME: why do we have to fill in S_IFDIR, while the mode is
491 * correct for mknod, creat, etc.? Smells like the vfs *should*
492 * do it for us but for some reason fails to do so.
493 */
494 inode = logfs_new_inode(dir, S_IFDIR | mode);
495 if (IS_ERR(inode))
496 return PTR_ERR(inode);
497
498 inode->i_op = &logfs_dir_iops;
499 inode->i_fop = &logfs_dir_fops;
500
501 return __logfs_create(dir, dentry, inode, NULL, 0);
502}
503
Al Viro4acdaf22011-07-26 01:42:34 -0400504static int logfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Joern Engel5db53f32009-11-20 20:13:39 +0100505 struct nameidata *nd)
506{
507 struct inode *inode;
508
509 inode = logfs_new_inode(dir, mode);
510 if (IS_ERR(inode))
511 return PTR_ERR(inode);
512
513 inode->i_op = &logfs_reg_iops;
514 inode->i_fop = &logfs_reg_fops;
515 inode->i_mapping->a_ops = &logfs_reg_aops;
516
517 return __logfs_create(dir, dentry, inode, NULL, 0);
518}
519
Al Viro1a67aaf2011-07-26 01:52:52 -0400520static int logfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
Joern Engel5db53f32009-11-20 20:13:39 +0100521 dev_t rdev)
522{
523 struct inode *inode;
524
525 if (dentry->d_name.len > LOGFS_MAX_NAMELEN)
526 return -ENAMETOOLONG;
527
528 inode = logfs_new_inode(dir, mode);
529 if (IS_ERR(inode))
530 return PTR_ERR(inode);
531
532 init_special_inode(inode, mode, rdev);
533
534 return __logfs_create(dir, dentry, inode, NULL, 0);
535}
536
537static int logfs_symlink(struct inode *dir, struct dentry *dentry,
538 const char *target)
539{
540 struct inode *inode;
541 size_t destlen = strlen(target) + 1;
542
543 if (destlen > dir->i_sb->s_blocksize)
544 return -ENAMETOOLONG;
545
546 inode = logfs_new_inode(dir, S_IFLNK | 0777);
547 if (IS_ERR(inode))
548 return PTR_ERR(inode);
549
550 inode->i_op = &logfs_symlink_iops;
551 inode->i_mapping->a_ops = &logfs_reg_aops;
552
553 return __logfs_create(dir, dentry, inode, target, destlen);
554}
555
Joern Engel5db53f32009-11-20 20:13:39 +0100556static int logfs_link(struct dentry *old_dentry, struct inode *dir,
557 struct dentry *dentry)
558{
559 struct inode *inode = old_dentry->d_inode;
560
561 if (inode->i_nlink >= LOGFS_LINK_MAX)
562 return -EMLINK;
563
564 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Al Viro7de9c6ee2010-10-23 11:11:40 -0400565 ihold(inode);
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200566 inc_nlink(inode);
Joern Engel5db53f32009-11-20 20:13:39 +0100567 mark_inode_dirty_sync(inode);
568
569 return __logfs_create(dir, dentry, inode, NULL, 0);
570}
571
572static int logfs_get_dd(struct inode *dir, struct dentry *dentry,
573 struct logfs_disk_dentry *dd, loff_t *pos)
574{
575 struct page *page;
576 void *map;
577
578 page = logfs_get_dd_page(dir, dentry);
579 if (IS_ERR(page))
580 return PTR_ERR(page);
581 *pos = page->index;
582 map = kmap_atomic(page, KM_USER0);
583 memcpy(dd, map, sizeof(*dd));
584 kunmap_atomic(map, KM_USER0);
585 page_cache_release(page);
586 return 0;
587}
588
589static int logfs_delete_dd(struct inode *dir, loff_t pos)
590{
591 /*
592 * Getting called with pos somewhere beyond eof is either a goofup
593 * within this file or means someone maliciously edited the
594 * (crc-protected) journal.
595 */
596 BUG_ON(beyond_eof(dir, pos));
597 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
598 log_dir(" Delete dentry (%lx, %llx)\n", dir->i_ino, pos);
599 return logfs_delete(dir, pos, NULL);
600}
601
602/*
603 * Cross-directory rename, target does not exist. Just a little nasty.
604 * Create a new dentry in the target dir, then remove the old dentry,
605 * all the while taking care to remember our operation in the journal.
606 */
607static int logfs_rename_cross(struct inode *old_dir, struct dentry *old_dentry,
608 struct inode *new_dir, struct dentry *new_dentry)
609{
610 struct logfs_super *super = logfs_super(old_dir->i_sb);
611 struct logfs_disk_dentry dd;
612 struct logfs_transaction *ta;
613 loff_t pos;
614 int err;
615
616 /* 1. locate source dd */
617 err = logfs_get_dd(old_dir, old_dentry, &dd, &pos);
618 if (err)
619 return err;
620
621 ta = kzalloc(sizeof(*ta), GFP_KERNEL);
622 if (!ta)
623 return -ENOMEM;
624
625 ta->state = CROSS_RENAME_1;
626 ta->dir = old_dir->i_ino;
627 ta->pos = pos;
628
629 /* 2. write target dd */
630 mutex_lock(&super->s_dirop_mutex);
631 logfs_add_transaction(new_dir, ta);
632 err = logfs_write_dir(new_dir, new_dentry, old_dentry->d_inode);
633 if (!err)
634 err = write_inode(new_dir);
635
636 if (err) {
637 super->s_rename_dir = 0;
638 super->s_rename_pos = 0;
639 abort_transaction(new_dir, ta);
640 goto out;
641 }
642
643 /* 3. remove source dd */
644 ta->state = CROSS_RENAME_2;
645 logfs_add_transaction(old_dir, ta);
646 err = logfs_delete_dd(old_dir, pos);
647 if (!err)
648 err = write_inode(old_dir);
649 LOGFS_BUG_ON(err, old_dir->i_sb);
650out:
651 mutex_unlock(&super->s_dirop_mutex);
652 return err;
653}
654
655static int logfs_replace_inode(struct inode *dir, struct dentry *dentry,
656 struct logfs_disk_dentry *dd, struct inode *inode)
657{
658 loff_t pos;
659 int err;
660
661 err = logfs_get_dd(dir, dentry, dd, &pos);
662 if (err)
663 return err;
664 dd->ino = cpu_to_be64(inode->i_ino);
665 dd->type = logfs_type(inode);
666
667 err = write_dir(dir, dd, pos);
668 if (err)
669 return err;
670 log_dir("Replace dentry (%lx, %llx) %s -> %llx\n", dir->i_ino, pos,
671 dd->name, be64_to_cpu(dd->ino));
672 return write_inode(dir);
673}
674
675/* Target dentry exists - the worst case. We need to attach the source
676 * inode to the target dentry, then remove the orphaned target inode and
677 * source dentry.
678 */
679static int logfs_rename_target(struct inode *old_dir, struct dentry *old_dentry,
680 struct inode *new_dir, struct dentry *new_dentry)
681{
682 struct logfs_super *super = logfs_super(old_dir->i_sb);
683 struct inode *old_inode = old_dentry->d_inode;
684 struct inode *new_inode = new_dentry->d_inode;
685 int isdir = S_ISDIR(old_inode->i_mode);
686 struct logfs_disk_dentry dd;
687 struct logfs_transaction *ta;
688 loff_t pos;
689 int err;
690
691 BUG_ON(isdir != S_ISDIR(new_inode->i_mode));
692 if (isdir) {
693 if (!logfs_empty_dir(new_inode))
694 return -ENOTEMPTY;
695 }
696
697 /* 1. locate source dd */
698 err = logfs_get_dd(old_dir, old_dentry, &dd, &pos);
699 if (err)
700 return err;
701
702 ta = kzalloc(sizeof(*ta), GFP_KERNEL);
703 if (!ta)
704 return -ENOMEM;
705
706 ta->state = TARGET_RENAME_1;
707 ta->dir = old_dir->i_ino;
708 ta->pos = pos;
709 ta->ino = new_inode->i_ino;
710
711 /* 2. attach source inode to target dd */
712 mutex_lock(&super->s_dirop_mutex);
713 logfs_add_transaction(new_dir, ta);
714 err = logfs_replace_inode(new_dir, new_dentry, &dd, old_inode);
715 if (err) {
716 super->s_rename_dir = 0;
717 super->s_rename_pos = 0;
718 super->s_victim_ino = 0;
719 abort_transaction(new_dir, ta);
720 goto out;
721 }
722
723 /* 3. remove source dd */
724 ta->state = TARGET_RENAME_2;
725 logfs_add_transaction(old_dir, ta);
726 err = logfs_delete_dd(old_dir, pos);
727 if (!err)
728 err = write_inode(old_dir);
729 LOGFS_BUG_ON(err, old_dir->i_sb);
730
731 /* 4. remove target inode */
732 ta->state = TARGET_RENAME_3;
733 logfs_add_transaction(new_inode, ta);
734 err = logfs_remove_inode(new_inode);
735
736out:
737 mutex_unlock(&super->s_dirop_mutex);
738 return err;
739}
740
741static int logfs_rename(struct inode *old_dir, struct dentry *old_dentry,
742 struct inode *new_dir, struct dentry *new_dentry)
743{
744 if (new_dentry->d_inode)
745 return logfs_rename_target(old_dir, old_dentry,
746 new_dir, new_dentry);
747 return logfs_rename_cross(old_dir, old_dentry, new_dir, new_dentry);
748}
749
750/* No locking done here, as this is called before .get_sb() returns. */
751int logfs_replay_journal(struct super_block *sb)
752{
753 struct logfs_super *super = logfs_super(sb);
754 struct inode *inode;
755 u64 ino, pos;
756 int err;
757
758 if (super->s_victim_ino) {
759 /* delete victim inode */
760 ino = super->s_victim_ino;
761 printk(KERN_INFO"LogFS: delete unmapped inode #%llx\n", ino);
762 inode = logfs_iget(sb, ino);
763 if (IS_ERR(inode))
764 goto fail;
765
766 LOGFS_BUG_ON(i_size_read(inode) > 0, sb);
767 super->s_victim_ino = 0;
768 err = logfs_remove_inode(inode);
769 iput(inode);
770 if (err) {
771 super->s_victim_ino = ino;
772 goto fail;
773 }
774 }
775 if (super->s_rename_dir) {
776 /* delete old dd from rename */
777 ino = super->s_rename_dir;
778 pos = super->s_rename_pos;
779 printk(KERN_INFO"LogFS: delete unbacked dentry (%llx, %llx)\n",
780 ino, pos);
781 inode = logfs_iget(sb, ino);
782 if (IS_ERR(inode))
783 goto fail;
784
785 super->s_rename_dir = 0;
786 super->s_rename_pos = 0;
787 err = logfs_delete_dd(inode, pos);
788 iput(inode);
789 if (err) {
790 super->s_rename_dir = ino;
791 super->s_rename_pos = pos;
792 goto fail;
793 }
794 }
795 return 0;
796fail:
797 LOGFS_BUG(sb);
798 return -EIO;
799}
800
801const struct inode_operations logfs_symlink_iops = {
802 .readlink = generic_readlink,
803 .follow_link = page_follow_link_light,
804};
805
806const struct inode_operations logfs_dir_iops = {
807 .create = logfs_create,
808 .link = logfs_link,
809 .lookup = logfs_lookup,
810 .mkdir = logfs_mkdir,
811 .mknod = logfs_mknod,
812 .rename = logfs_rename,
813 .rmdir = logfs_rmdir,
Joern Engel5db53f32009-11-20 20:13:39 +0100814 .symlink = logfs_symlink,
815 .unlink = logfs_unlink,
816};
817const struct file_operations logfs_dir_fops = {
818 .fsync = logfs_fsync,
Arnd Bergmann02d6d682010-04-27 22:30:06 +0200819 .unlocked_ioctl = logfs_ioctl,
Joern Engel5db53f32009-11-20 20:13:39 +0100820 .readdir = logfs_readdir,
821 .read = generic_read_dir,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200822 .llseek = default_llseek,
Joern Engel5db53f32009-11-20 20:13:39 +0100823};