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