Merge branch 'master' into export-slabh
[kernel.git] / fs / logfs / super.c
1 /*
2  * fs/logfs/super.c
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  * Generally contains mount/umount code and also serves as a dump area for
9  * any functions that don't fit elsewhere and neither justify a file of their
10  * own.
11  */
12 #include "logfs.h"
13 #include <linux/bio.h>
14 #include <linux/slab.h>
15 #include <linux/mtd/mtd.h>
16 #include <linux/statfs.h>
17 #include <linux/buffer_head.h>
18
19 static DEFINE_MUTEX(emergency_mutex);
20 static struct page *emergency_page;
21
22 struct page *emergency_read_begin(struct address_space *mapping, pgoff_t index)
23 {
24         filler_t *filler = (filler_t *)mapping->a_ops->readpage;
25         struct page *page;
26         int err;
27
28         page = read_cache_page(mapping, index, filler, NULL);
29         if (page)
30                 return page;
31
32         /* No more pages available, switch to emergency page */
33         printk(KERN_INFO"Logfs: Using emergency page\n");
34         mutex_lock(&emergency_mutex);
35         err = filler(NULL, emergency_page);
36         if (err) {
37                 mutex_unlock(&emergency_mutex);
38                 printk(KERN_EMERG"Logfs: Error reading emergency page\n");
39                 return ERR_PTR(err);
40         }
41         return emergency_page;
42 }
43
44 void emergency_read_end(struct page *page)
45 {
46         if (page == emergency_page)
47                 mutex_unlock(&emergency_mutex);
48         else
49                 page_cache_release(page);
50 }
51
52 static void dump_segfile(struct super_block *sb)
53 {
54         struct logfs_super *super = logfs_super(sb);
55         struct logfs_segment_entry se;
56         u32 segno;
57
58         for (segno = 0; segno < super->s_no_segs; segno++) {
59                 logfs_get_segment_entry(sb, segno, &se);
60                 printk("%3x: %6x %8x", segno, be32_to_cpu(se.ec_level),
61                                 be32_to_cpu(se.valid));
62                 if (++segno < super->s_no_segs) {
63                         logfs_get_segment_entry(sb, segno, &se);
64                         printk(" %6x %8x", be32_to_cpu(se.ec_level),
65                                         be32_to_cpu(se.valid));
66                 }
67                 if (++segno < super->s_no_segs) {
68                         logfs_get_segment_entry(sb, segno, &se);
69                         printk(" %6x %8x", be32_to_cpu(se.ec_level),
70                                         be32_to_cpu(se.valid));
71                 }
72                 if (++segno < super->s_no_segs) {
73                         logfs_get_segment_entry(sb, segno, &se);
74                         printk(" %6x %8x", be32_to_cpu(se.ec_level),
75                                         be32_to_cpu(se.valid));
76                 }
77                 printk("\n");
78         }
79 }
80
81 /*
82  * logfs_crash_dump - dump debug information to device
83  *
84  * The LogFS superblock only occupies part of a segment.  This function will
85  * write as much debug information as it can gather into the spare space.
86  */
87 void logfs_crash_dump(struct super_block *sb)
88 {
89         dump_segfile(sb);
90 }
91
92 /*
93  * TODO: move to lib/string.c
94  */
95 /**
96  * memchr_inv - Find a character in an area of memory.
97  * @s: The memory area
98  * @c: The byte to search for
99  * @n: The size of the area.
100  *
101  * returns the address of the first character other than @c, or %NULL
102  * if the whole buffer contains just @c.
103  */
104 void *memchr_inv(const void *s, int c, size_t n)
105 {
106         const unsigned char *p = s;
107         while (n-- != 0)
108                 if ((unsigned char)c != *p++)
109                         return (void *)(p - 1);
110
111         return NULL;
112 }
113
114 /*
115  * FIXME: There should be a reserve for root, similar to ext2.
116  */
117 int logfs_statfs(struct dentry *dentry, struct kstatfs *stats)
118 {
119         struct super_block *sb = dentry->d_sb;
120         struct logfs_super *super = logfs_super(sb);
121
122         stats->f_type           = LOGFS_MAGIC_U32;
123         stats->f_bsize          = sb->s_blocksize;
124         stats->f_blocks         = super->s_size >> LOGFS_BLOCK_BITS >> 3;
125         stats->f_bfree          = super->s_free_bytes >> sb->s_blocksize_bits;
126         stats->f_bavail         = super->s_free_bytes >> sb->s_blocksize_bits;
127         stats->f_files          = 0;
128         stats->f_ffree          = 0;
129         stats->f_namelen        = LOGFS_MAX_NAMELEN;
130         return 0;
131 }
132
133 static int logfs_sb_set(struct super_block *sb, void *_super)
134 {
135         struct logfs_super *super = _super;
136
137         sb->s_fs_info = super;
138         sb->s_mtd = super->s_mtd;
139         sb->s_bdev = super->s_bdev;
140         return 0;
141 }
142
143 static int logfs_sb_test(struct super_block *sb, void *_super)
144 {
145         struct logfs_super *super = _super;
146         struct mtd_info *mtd = super->s_mtd;
147
148         if (mtd && sb->s_mtd == mtd)
149                 return 1;
150         if (super->s_bdev && sb->s_bdev == super->s_bdev)
151                 return 1;
152         return 0;
153 }
154
155 static void set_segment_header(struct logfs_segment_header *sh, u8 type,
156                 u8 level, u32 segno, u32 ec)
157 {
158         sh->pad = 0;
159         sh->type = type;
160         sh->level = level;
161         sh->segno = cpu_to_be32(segno);
162         sh->ec = cpu_to_be32(ec);
163         sh->gec = cpu_to_be64(segno);
164         sh->crc = logfs_crc32(sh, LOGFS_SEGMENT_HEADERSIZE, 4);
165 }
166
167 static void logfs_write_ds(struct super_block *sb, struct logfs_disk_super *ds,
168                 u32 segno, u32 ec)
169 {
170         struct logfs_super *super = logfs_super(sb);
171         struct logfs_segment_header *sh = &ds->ds_sh;
172         int i;
173
174         memset(ds, 0, sizeof(*ds));
175         set_segment_header(sh, SEG_SUPER, 0, segno, ec);
176
177         ds->ds_ifile_levels     = super->s_ifile_levels;
178         ds->ds_iblock_levels    = super->s_iblock_levels;
179         ds->ds_data_levels      = super->s_data_levels; /* XXX: Remove */
180         ds->ds_segment_shift    = super->s_segshift;
181         ds->ds_block_shift      = sb->s_blocksize_bits;
182         ds->ds_write_shift      = super->s_writeshift;
183         ds->ds_filesystem_size  = cpu_to_be64(super->s_size);
184         ds->ds_segment_size     = cpu_to_be32(super->s_segsize);
185         ds->ds_bad_seg_reserve  = cpu_to_be32(super->s_bad_seg_reserve);
186         ds->ds_feature_incompat = cpu_to_be64(super->s_feature_incompat);
187         ds->ds_feature_ro_compat= cpu_to_be64(super->s_feature_ro_compat);
188         ds->ds_feature_compat   = cpu_to_be64(super->s_feature_compat);
189         ds->ds_feature_flags    = cpu_to_be64(super->s_feature_flags);
190         ds->ds_root_reserve     = cpu_to_be64(super->s_root_reserve);
191         ds->ds_speed_reserve    = cpu_to_be64(super->s_speed_reserve);
192         journal_for_each(i)
193                 ds->ds_journal_seg[i] = cpu_to_be32(super->s_journal_seg[i]);
194         ds->ds_magic            = cpu_to_be64(LOGFS_MAGIC);
195         ds->ds_crc = logfs_crc32(ds, sizeof(*ds),
196                         LOGFS_SEGMENT_HEADERSIZE + 12);
197 }
198
199 static int write_one_sb(struct super_block *sb,
200                 struct page *(*find_sb)(struct super_block *sb, u64 *ofs))
201 {
202         struct logfs_super *super = logfs_super(sb);
203         struct logfs_disk_super *ds;
204         struct logfs_segment_entry se;
205         struct page *page;
206         u64 ofs;
207         u32 ec, segno;
208         int err;
209
210         page = find_sb(sb, &ofs);
211         if (!page)
212                 return -EIO;
213         ds = page_address(page);
214         segno = seg_no(sb, ofs);
215         logfs_get_segment_entry(sb, segno, &se);
216         ec = be32_to_cpu(se.ec_level) >> 4;
217         ec++;
218         logfs_set_segment_erased(sb, segno, ec, 0);
219         logfs_write_ds(sb, ds, segno, ec);
220         err = super->s_devops->write_sb(sb, page);
221         page_cache_release(page);
222         return err;
223 }
224
225 int logfs_write_sb(struct super_block *sb)
226 {
227         struct logfs_super *super = logfs_super(sb);
228         int err;
229
230         /* First superblock */
231         err = write_one_sb(sb, super->s_devops->find_first_sb);
232         if (err)
233                 return err;
234
235         /* Last superblock */
236         err = write_one_sb(sb, super->s_devops->find_last_sb);
237         if (err)
238                 return err;
239         return 0;
240 }
241
242 static int ds_cmp(const void *ds0, const void *ds1)
243 {
244         size_t len = sizeof(struct logfs_disk_super);
245
246         /* We know the segment headers differ, so ignore them */
247         len -= LOGFS_SEGMENT_HEADERSIZE;
248         ds0 += LOGFS_SEGMENT_HEADERSIZE;
249         ds1 += LOGFS_SEGMENT_HEADERSIZE;
250         return memcmp(ds0, ds1, len);
251 }
252
253 static int logfs_recover_sb(struct super_block *sb)
254 {
255         struct logfs_super *super = logfs_super(sb);
256         struct logfs_disk_super _ds0, *ds0 = &_ds0;
257         struct logfs_disk_super _ds1, *ds1 = &_ds1;
258         int err, valid0, valid1;
259
260         /* read first superblock */
261         err = wbuf_read(sb, super->s_sb_ofs[0], sizeof(*ds0), ds0);
262         if (err)
263                 return err;
264         /* read last superblock */
265         err = wbuf_read(sb, super->s_sb_ofs[1], sizeof(*ds1), ds1);
266         if (err)
267                 return err;
268         valid0 = logfs_check_ds(ds0) == 0;
269         valid1 = logfs_check_ds(ds1) == 0;
270
271         if (!valid0 && valid1) {
272                 printk(KERN_INFO"First superblock is invalid - fixing.\n");
273                 return write_one_sb(sb, super->s_devops->find_first_sb);
274         }
275         if (valid0 && !valid1) {
276                 printk(KERN_INFO"Last superblock is invalid - fixing.\n");
277                 return write_one_sb(sb, super->s_devops->find_last_sb);
278         }
279         if (valid0 && valid1 && ds_cmp(ds0, ds1)) {
280                 printk(KERN_INFO"Superblocks don't match - fixing.\n");
281                 return logfs_write_sb(sb);
282         }
283         /* If neither is valid now, something's wrong.  Didn't we properly
284          * check them before?!? */
285         BUG_ON(!valid0 && !valid1);
286         return 0;
287 }
288
289 static int logfs_make_writeable(struct super_block *sb)
290 {
291         int err;
292
293         err = logfs_open_segfile(sb);
294         if (err)
295                 return err;
296
297         /* Repair any broken superblock copies */
298         err = logfs_recover_sb(sb);
299         if (err)
300                 return err;
301
302         /* Check areas for trailing unaccounted data */
303         err = logfs_check_areas(sb);
304         if (err)
305                 return err;
306
307         /* Do one GC pass before any data gets dirtied */
308         logfs_gc_pass(sb);
309
310         /* after all initializations are done, replay the journal
311          * for rw-mounts, if necessary */
312         err = logfs_replay_journal(sb);
313         if (err)
314                 return err;
315
316         return 0;
317 }
318
319 static int logfs_get_sb_final(struct super_block *sb, struct vfsmount *mnt)
320 {
321         struct logfs_super *super = logfs_super(sb);
322         struct inode *rootdir;
323         int err;
324
325         /* root dir */
326         rootdir = logfs_iget(sb, LOGFS_INO_ROOT);
327         if (IS_ERR(rootdir))
328                 goto fail;
329
330         sb->s_root = d_alloc_root(rootdir);
331         if (!sb->s_root)
332                 goto fail2;
333
334         super->s_erase_page = alloc_pages(GFP_KERNEL, 0);
335         if (!super->s_erase_page)
336                 goto fail2;
337         memset(page_address(super->s_erase_page), 0xFF, PAGE_SIZE);
338
339         /* FIXME: check for read-only mounts */
340         err = logfs_make_writeable(sb);
341         if (err)
342                 goto fail3;
343
344         log_super("LogFS: Finished mounting\n");
345         simple_set_mnt(mnt, sb);
346         return 0;
347
348 fail3:
349         __free_page(super->s_erase_page);
350 fail2:
351         iput(rootdir);
352 fail:
353         iput(logfs_super(sb)->s_master_inode);
354         return -EIO;
355 }
356
357 int logfs_check_ds(struct logfs_disk_super *ds)
358 {
359         struct logfs_segment_header *sh = &ds->ds_sh;
360
361         if (ds->ds_magic != cpu_to_be64(LOGFS_MAGIC))
362                 return -EINVAL;
363         if (sh->crc != logfs_crc32(sh, LOGFS_SEGMENT_HEADERSIZE, 4))
364                 return -EINVAL;
365         if (ds->ds_crc != logfs_crc32(ds, sizeof(*ds),
366                                 LOGFS_SEGMENT_HEADERSIZE + 12))
367                 return -EINVAL;
368         return 0;
369 }
370
371 static struct page *find_super_block(struct super_block *sb)
372 {
373         struct logfs_super *super = logfs_super(sb);
374         struct page *first, *last;
375
376         first = super->s_devops->find_first_sb(sb, &super->s_sb_ofs[0]);
377         if (!first || IS_ERR(first))
378                 return NULL;
379         last = super->s_devops->find_last_sb(sb, &super->s_sb_ofs[1]);
380         if (!last || IS_ERR(first)) {
381                 page_cache_release(first);
382                 return NULL;
383         }
384
385         if (!logfs_check_ds(page_address(first))) {
386                 page_cache_release(last);
387                 return first;
388         }
389
390         /* First one didn't work, try the second superblock */
391         if (!logfs_check_ds(page_address(last))) {
392                 page_cache_release(first);
393                 return last;
394         }
395
396         /* Neither worked, sorry folks */
397         page_cache_release(first);
398         page_cache_release(last);
399         return NULL;
400 }
401
402 static int __logfs_read_sb(struct super_block *sb)
403 {
404         struct logfs_super *super = logfs_super(sb);
405         struct page *page;
406         struct logfs_disk_super *ds;
407         int i;
408
409         page = find_super_block(sb);
410         if (!page)
411                 return -EIO;
412
413         ds = page_address(page);
414         super->s_size = be64_to_cpu(ds->ds_filesystem_size);
415         super->s_root_reserve = be64_to_cpu(ds->ds_root_reserve);
416         super->s_speed_reserve = be64_to_cpu(ds->ds_speed_reserve);
417         super->s_bad_seg_reserve = be32_to_cpu(ds->ds_bad_seg_reserve);
418         super->s_segsize = 1 << ds->ds_segment_shift;
419         super->s_segmask = (1 << ds->ds_segment_shift) - 1;
420         super->s_segshift = ds->ds_segment_shift;
421         sb->s_blocksize = 1 << ds->ds_block_shift;
422         sb->s_blocksize_bits = ds->ds_block_shift;
423         super->s_writesize = 1 << ds->ds_write_shift;
424         super->s_writeshift = ds->ds_write_shift;
425         super->s_no_segs = super->s_size >> super->s_segshift;
426         super->s_no_blocks = super->s_segsize >> sb->s_blocksize_bits;
427         super->s_feature_incompat = be64_to_cpu(ds->ds_feature_incompat);
428         super->s_feature_ro_compat = be64_to_cpu(ds->ds_feature_ro_compat);
429         super->s_feature_compat = be64_to_cpu(ds->ds_feature_compat);
430         super->s_feature_flags = be64_to_cpu(ds->ds_feature_flags);
431
432         journal_for_each(i)
433                 super->s_journal_seg[i] = be32_to_cpu(ds->ds_journal_seg[i]);
434
435         super->s_ifile_levels = ds->ds_ifile_levels;
436         super->s_iblock_levels = ds->ds_iblock_levels;
437         super->s_data_levels = ds->ds_data_levels;
438         super->s_total_levels = super->s_ifile_levels + super->s_iblock_levels
439                 + super->s_data_levels;
440         page_cache_release(page);
441         return 0;
442 }
443
444 static int logfs_read_sb(struct super_block *sb, int read_only)
445 {
446         struct logfs_super *super = logfs_super(sb);
447         int ret;
448
449         super->s_btree_pool = mempool_create(32, btree_alloc, btree_free, NULL);
450         if (!super->s_btree_pool)
451                 return -ENOMEM;
452
453         btree_init_mempool64(&super->s_shadow_tree.new, super->s_btree_pool);
454         btree_init_mempool64(&super->s_shadow_tree.old, super->s_btree_pool);
455
456         ret = logfs_init_mapping(sb);
457         if (ret)
458                 return ret;
459
460         ret = __logfs_read_sb(sb);
461         if (ret)
462                 return ret;
463
464         if (super->s_feature_incompat & ~LOGFS_FEATURES_INCOMPAT)
465                 return -EIO;
466         if ((super->s_feature_ro_compat & ~LOGFS_FEATURES_RO_COMPAT) &&
467                         !read_only)
468                 return -EIO;
469
470         mutex_init(&super->s_dirop_mutex);
471         mutex_init(&super->s_object_alias_mutex);
472         INIT_LIST_HEAD(&super->s_freeing_list);
473
474         ret = logfs_init_rw(sb);
475         if (ret)
476                 return ret;
477
478         ret = logfs_init_areas(sb);
479         if (ret)
480                 return ret;
481
482         ret = logfs_init_gc(sb);
483         if (ret)
484                 return ret;
485
486         ret = logfs_init_journal(sb);
487         if (ret)
488                 return ret;
489
490         return 0;
491 }
492
493 static void logfs_kill_sb(struct super_block *sb)
494 {
495         struct logfs_super *super = logfs_super(sb);
496
497         log_super("LogFS: Start unmounting\n");
498         /* Alias entries slow down mount, so evict as many as possible */
499         sync_filesystem(sb);
500         logfs_write_anchor(sb);
501
502         /*
503          * From this point on alias entries are simply dropped - and any
504          * writes to the object store are considered bugs.
505          */
506         super->s_flags |= LOGFS_SB_FLAG_SHUTDOWN;
507         log_super("LogFS: Now in shutdown\n");
508         generic_shutdown_super(sb);
509
510         BUG_ON(super->s_dirty_used_bytes || super->s_dirty_free_bytes);
511
512         logfs_cleanup_gc(sb);
513         logfs_cleanup_journal(sb);
514         logfs_cleanup_areas(sb);
515         logfs_cleanup_rw(sb);
516         if (super->s_erase_page)
517                 __free_page(super->s_erase_page);
518         super->s_devops->put_device(sb);
519         mempool_destroy(super->s_btree_pool);
520         mempool_destroy(super->s_alias_pool);
521         kfree(super);
522         log_super("LogFS: Finished unmounting\n");
523 }
524
525 int logfs_get_sb_device(struct file_system_type *type, int flags,
526                 struct mtd_info *mtd, struct block_device *bdev,
527                 const struct logfs_device_ops *devops, struct vfsmount *mnt)
528 {
529         struct logfs_super *super;
530         struct super_block *sb;
531         int err = -ENOMEM;
532         static int mount_count;
533
534         log_super("LogFS: Start mount %x\n", mount_count++);
535         super = kzalloc(sizeof(*super), GFP_KERNEL);
536         if (!super)
537                 goto err0;
538
539         super->s_mtd    = mtd;
540         super->s_bdev   = bdev;
541         err = -EINVAL;
542         sb = sget(type, logfs_sb_test, logfs_sb_set, super);
543         if (IS_ERR(sb))
544                 goto err0;
545
546         if (sb->s_root) {
547                 /* Device is already in use */
548                 err = 0;
549                 simple_set_mnt(mnt, sb);
550                 goto err0;
551         }
552
553         super->s_devops = devops;
554
555         /*
556          * sb->s_maxbytes is limited to 8TB.  On 32bit systems, the page cache
557          * only covers 16TB and the upper 8TB are used for indirect blocks.
558          * On 64bit system we could bump up the limit, but that would make
559          * the filesystem incompatible with 32bit systems.
560          */
561         sb->s_maxbytes  = (1ull << 43) - 1;
562         sb->s_op        = &logfs_super_operations;
563         sb->s_flags     = flags | MS_NOATIME;
564
565         err = logfs_read_sb(sb, sb->s_flags & MS_RDONLY);
566         if (err)
567                 goto err1;
568
569         sb->s_flags |= MS_ACTIVE;
570         err = logfs_get_sb_final(sb, mnt);
571         if (err)
572                 goto err1;
573         return 0;
574
575 err1:
576         deactivate_locked_super(sb);
577         return err;
578 err0:
579         kfree(super);
580         //devops->put_device(sb);
581         return err;
582 }
583
584 static int logfs_get_sb(struct file_system_type *type, int flags,
585                 const char *devname, void *data, struct vfsmount *mnt)
586 {
587         ulong mtdnr;
588
589         if (!devname)
590                 return logfs_get_sb_bdev(type, flags, devname, mnt);
591         if (strncmp(devname, "mtd", 3))
592                 return logfs_get_sb_bdev(type, flags, devname, mnt);
593
594         {
595                 char *garbage;
596                 mtdnr = simple_strtoul(devname+3, &garbage, 0);
597                 if (*garbage)
598                         return -EINVAL;
599         }
600
601         return logfs_get_sb_mtd(type, flags, mtdnr, mnt);
602 }
603
604 static struct file_system_type logfs_fs_type = {
605         .owner          = THIS_MODULE,
606         .name           = "logfs",
607         .get_sb         = logfs_get_sb,
608         .kill_sb        = logfs_kill_sb,
609         .fs_flags       = FS_REQUIRES_DEV,
610
611 };
612
613 static int __init logfs_init(void)
614 {
615         int ret;
616
617         emergency_page = alloc_pages(GFP_KERNEL, 0);
618         if (!emergency_page)
619                 return -ENOMEM;
620
621         ret = logfs_compr_init();
622         if (ret)
623                 goto out1;
624
625         ret = logfs_init_inode_cache();
626         if (ret)
627                 goto out2;
628
629         return register_filesystem(&logfs_fs_type);
630 out2:
631         logfs_compr_exit();
632 out1:
633         __free_pages(emergency_page, 0);
634         return ret;
635 }
636
637 static void __exit logfs_exit(void)
638 {
639         unregister_filesystem(&logfs_fs_type);
640         logfs_destroy_inode_cache();
641         logfs_compr_exit();
642         __free_pages(emergency_page, 0);
643 }
644
645 module_init(logfs_init);
646 module_exit(logfs_exit);
647
648 MODULE_LICENSE("GPL v2");
649 MODULE_AUTHOR("Joern Engel <joern@logfs.org>");
650 MODULE_DESCRIPTION("scalable flash filesystem");