drm/i915: Restore missing command flush before interrupt on BLT ring
[linux/fpc-iii.git] / fs / logfs / super.c
blob33435e4b14d24aaad89bfab57da526870bbfa797
1 /*
2 * fs/logfs/super.c
4 * As should be obvious for Linux kernel code, license is GPLv2
6 * Copyright (c) 2005-2008 Joern Engel <joern@logfs.org>
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.
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>
20 static DEFINE_MUTEX(emergency_mutex);
21 static struct page *emergency_page;
23 struct page *emergency_read_begin(struct address_space *mapping, pgoff_t index)
25 filler_t *filler = (filler_t *)mapping->a_ops->readpage;
26 struct page *page;
27 int err;
29 page = read_cache_page(mapping, index, filler, NULL);
30 if (page)
31 return page;
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);
42 return emergency_page;
45 void emergency_read_end(struct page *page)
47 if (page == emergency_page)
48 mutex_unlock(&emergency_mutex);
49 else
50 page_cache_release(page);
53 static void dump_segfile(struct super_block *sb)
55 struct logfs_super *super = logfs_super(sb);
56 struct logfs_segment_entry se;
57 u32 segno;
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));
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));
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));
78 printk("\n");
83 * logfs_crash_dump - dump debug information to device
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.
88 void logfs_crash_dump(struct super_block *sb)
90 dump_segfile(sb);
94 * TODO: move to lib/string.c
96 /**
97 * memchr_inv - Find a character in an area of memory.
98 * @s: The memory area
99 * @c: The byte to search for
100 * @n: The size of the area.
102 * returns the address of the first character other than @c, or %NULL
103 * if the whole buffer contains just @c.
105 void *memchr_inv(const void *s, int c, size_t n)
107 const unsigned char *p = s;
108 while (n-- != 0)
109 if ((unsigned char)c != *p++)
110 return (void *)(p - 1);
112 return NULL;
116 * FIXME: There should be a reserve for root, similar to ext2.
118 int logfs_statfs(struct dentry *dentry, struct kstatfs *stats)
120 struct super_block *sb = dentry->d_sb;
121 struct logfs_super *super = logfs_super(sb);
123 stats->f_type = LOGFS_MAGIC_U32;
124 stats->f_bsize = sb->s_blocksize;
125 stats->f_blocks = super->s_size >> LOGFS_BLOCK_BITS >> 3;
126 stats->f_bfree = super->s_free_bytes >> sb->s_blocksize_bits;
127 stats->f_bavail = super->s_free_bytes >> sb->s_blocksize_bits;
128 stats->f_files = 0;
129 stats->f_ffree = 0;
130 stats->f_namelen = LOGFS_MAX_NAMELEN;
131 return 0;
134 static int logfs_sb_set(struct super_block *sb, void *_super)
136 struct logfs_super *super = _super;
138 sb->s_fs_info = super;
139 sb->s_mtd = super->s_mtd;
140 sb->s_bdev = super->s_bdev;
141 #ifdef CONFIG_BLOCK
142 if (sb->s_bdev)
143 sb->s_bdi = &bdev_get_queue(sb->s_bdev)->backing_dev_info;
144 #endif
145 #ifdef CONFIG_MTD
146 if (sb->s_mtd)
147 sb->s_bdi = sb->s_mtd->backing_dev_info;
148 #endif
149 return 0;
152 static int logfs_sb_test(struct super_block *sb, void *_super)
154 struct logfs_super *super = _super;
155 struct mtd_info *mtd = super->s_mtd;
157 if (mtd && sb->s_mtd == mtd)
158 return 1;
159 if (super->s_bdev && sb->s_bdev == super->s_bdev)
160 return 1;
161 return 0;
164 static void set_segment_header(struct logfs_segment_header *sh, u8 type,
165 u8 level, u32 segno, u32 ec)
167 sh->pad = 0;
168 sh->type = type;
169 sh->level = level;
170 sh->segno = cpu_to_be32(segno);
171 sh->ec = cpu_to_be32(ec);
172 sh->gec = cpu_to_be64(segno);
173 sh->crc = logfs_crc32(sh, LOGFS_SEGMENT_HEADERSIZE, 4);
176 static void logfs_write_ds(struct super_block *sb, struct logfs_disk_super *ds,
177 u32 segno, u32 ec)
179 struct logfs_super *super = logfs_super(sb);
180 struct logfs_segment_header *sh = &ds->ds_sh;
181 int i;
183 memset(ds, 0, sizeof(*ds));
184 set_segment_header(sh, SEG_SUPER, 0, segno, ec);
186 ds->ds_ifile_levels = super->s_ifile_levels;
187 ds->ds_iblock_levels = super->s_iblock_levels;
188 ds->ds_data_levels = super->s_data_levels; /* XXX: Remove */
189 ds->ds_segment_shift = super->s_segshift;
190 ds->ds_block_shift = sb->s_blocksize_bits;
191 ds->ds_write_shift = super->s_writeshift;
192 ds->ds_filesystem_size = cpu_to_be64(super->s_size);
193 ds->ds_segment_size = cpu_to_be32(super->s_segsize);
194 ds->ds_bad_seg_reserve = cpu_to_be32(super->s_bad_seg_reserve);
195 ds->ds_feature_incompat = cpu_to_be64(super->s_feature_incompat);
196 ds->ds_feature_ro_compat= cpu_to_be64(super->s_feature_ro_compat);
197 ds->ds_feature_compat = cpu_to_be64(super->s_feature_compat);
198 ds->ds_feature_flags = cpu_to_be64(super->s_feature_flags);
199 ds->ds_root_reserve = cpu_to_be64(super->s_root_reserve);
200 ds->ds_speed_reserve = cpu_to_be64(super->s_speed_reserve);
201 journal_for_each(i)
202 ds->ds_journal_seg[i] = cpu_to_be32(super->s_journal_seg[i]);
203 ds->ds_magic = cpu_to_be64(LOGFS_MAGIC);
204 ds->ds_crc = logfs_crc32(ds, sizeof(*ds),
205 LOGFS_SEGMENT_HEADERSIZE + 12);
208 static int write_one_sb(struct super_block *sb,
209 struct page *(*find_sb)(struct super_block *sb, u64 *ofs))
211 struct logfs_super *super = logfs_super(sb);
212 struct logfs_disk_super *ds;
213 struct logfs_segment_entry se;
214 struct page *page;
215 u64 ofs;
216 u32 ec, segno;
217 int err;
219 page = find_sb(sb, &ofs);
220 if (!page)
221 return -EIO;
222 ds = page_address(page);
223 segno = seg_no(sb, ofs);
224 logfs_get_segment_entry(sb, segno, &se);
225 ec = be32_to_cpu(se.ec_level) >> 4;
226 ec++;
227 logfs_set_segment_erased(sb, segno, ec, 0);
228 logfs_write_ds(sb, ds, segno, ec);
229 err = super->s_devops->write_sb(sb, page);
230 page_cache_release(page);
231 return err;
234 int logfs_write_sb(struct super_block *sb)
236 struct logfs_super *super = logfs_super(sb);
237 int err;
239 /* First superblock */
240 err = write_one_sb(sb, super->s_devops->find_first_sb);
241 if (err)
242 return err;
244 /* Last superblock */
245 err = write_one_sb(sb, super->s_devops->find_last_sb);
246 if (err)
247 return err;
248 return 0;
251 static int ds_cmp(const void *ds0, const void *ds1)
253 size_t len = sizeof(struct logfs_disk_super);
255 /* We know the segment headers differ, so ignore them */
256 len -= LOGFS_SEGMENT_HEADERSIZE;
257 ds0 += LOGFS_SEGMENT_HEADERSIZE;
258 ds1 += LOGFS_SEGMENT_HEADERSIZE;
259 return memcmp(ds0, ds1, len);
262 static int logfs_recover_sb(struct super_block *sb)
264 struct logfs_super *super = logfs_super(sb);
265 struct logfs_disk_super _ds0, *ds0 = &_ds0;
266 struct logfs_disk_super _ds1, *ds1 = &_ds1;
267 int err, valid0, valid1;
269 /* read first superblock */
270 err = wbuf_read(sb, super->s_sb_ofs[0], sizeof(*ds0), ds0);
271 if (err)
272 return err;
273 /* read last superblock */
274 err = wbuf_read(sb, super->s_sb_ofs[1], sizeof(*ds1), ds1);
275 if (err)
276 return err;
277 valid0 = logfs_check_ds(ds0) == 0;
278 valid1 = logfs_check_ds(ds1) == 0;
280 if (!valid0 && valid1) {
281 printk(KERN_INFO"First superblock is invalid - fixing.\n");
282 return write_one_sb(sb, super->s_devops->find_first_sb);
284 if (valid0 && !valid1) {
285 printk(KERN_INFO"Last superblock is invalid - fixing.\n");
286 return write_one_sb(sb, super->s_devops->find_last_sb);
288 if (valid0 && valid1 && ds_cmp(ds0, ds1)) {
289 printk(KERN_INFO"Superblocks don't match - fixing.\n");
290 return logfs_write_sb(sb);
292 /* If neither is valid now, something's wrong. Didn't we properly
293 * check them before?!? */
294 BUG_ON(!valid0 && !valid1);
295 return 0;
298 static int logfs_make_writeable(struct super_block *sb)
300 int err;
302 err = logfs_open_segfile(sb);
303 if (err)
304 return err;
306 /* Repair any broken superblock copies */
307 err = logfs_recover_sb(sb);
308 if (err)
309 return err;
311 /* Check areas for trailing unaccounted data */
312 err = logfs_check_areas(sb);
313 if (err)
314 return err;
316 /* Do one GC pass before any data gets dirtied */
317 logfs_gc_pass(sb);
319 /* after all initializations are done, replay the journal
320 * for rw-mounts, if necessary */
321 err = logfs_replay_journal(sb);
322 if (err)
323 return err;
325 return 0;
328 static int logfs_get_sb_final(struct super_block *sb)
330 struct logfs_super *super = logfs_super(sb);
331 struct inode *rootdir;
332 int err;
334 /* root dir */
335 rootdir = logfs_iget(sb, LOGFS_INO_ROOT);
336 if (IS_ERR(rootdir))
337 goto fail;
339 sb->s_root = d_alloc_root(rootdir);
340 if (!sb->s_root) {
341 iput(rootdir);
342 goto fail;
345 /* at that point we know that ->put_super() will be called */
346 super->s_erase_page = alloc_pages(GFP_KERNEL, 0);
347 if (!super->s_erase_page)
348 return -ENOMEM;
349 memset(page_address(super->s_erase_page), 0xFF, PAGE_SIZE);
351 /* FIXME: check for read-only mounts */
352 err = logfs_make_writeable(sb);
353 if (err) {
354 __free_page(super->s_erase_page);
355 return err;
358 log_super("LogFS: Finished mounting\n");
359 return 0;
361 fail:
362 iput(super->s_master_inode);
363 iput(super->s_segfile_inode);
364 iput(super->s_mapping_inode);
365 return -EIO;
368 int logfs_check_ds(struct logfs_disk_super *ds)
370 struct logfs_segment_header *sh = &ds->ds_sh;
372 if (ds->ds_magic != cpu_to_be64(LOGFS_MAGIC))
373 return -EINVAL;
374 if (sh->crc != logfs_crc32(sh, LOGFS_SEGMENT_HEADERSIZE, 4))
375 return -EINVAL;
376 if (ds->ds_crc != logfs_crc32(ds, sizeof(*ds),
377 LOGFS_SEGMENT_HEADERSIZE + 12))
378 return -EINVAL;
379 return 0;
382 static struct page *find_super_block(struct super_block *sb)
384 struct logfs_super *super = logfs_super(sb);
385 struct page *first, *last;
387 first = super->s_devops->find_first_sb(sb, &super->s_sb_ofs[0]);
388 if (!first || IS_ERR(first))
389 return NULL;
390 last = super->s_devops->find_last_sb(sb, &super->s_sb_ofs[1]);
391 if (!last || IS_ERR(last)) {
392 page_cache_release(first);
393 return NULL;
396 if (!logfs_check_ds(page_address(first))) {
397 page_cache_release(last);
398 return first;
401 /* First one didn't work, try the second superblock */
402 if (!logfs_check_ds(page_address(last))) {
403 page_cache_release(first);
404 return last;
407 /* Neither worked, sorry folks */
408 page_cache_release(first);
409 page_cache_release(last);
410 return NULL;
413 static int __logfs_read_sb(struct super_block *sb)
415 struct logfs_super *super = logfs_super(sb);
416 struct page *page;
417 struct logfs_disk_super *ds;
418 int i;
420 page = find_super_block(sb);
421 if (!page)
422 return -EINVAL;
424 ds = page_address(page);
425 super->s_size = be64_to_cpu(ds->ds_filesystem_size);
426 super->s_root_reserve = be64_to_cpu(ds->ds_root_reserve);
427 super->s_speed_reserve = be64_to_cpu(ds->ds_speed_reserve);
428 super->s_bad_seg_reserve = be32_to_cpu(ds->ds_bad_seg_reserve);
429 super->s_segsize = 1 << ds->ds_segment_shift;
430 super->s_segmask = (1 << ds->ds_segment_shift) - 1;
431 super->s_segshift = ds->ds_segment_shift;
432 sb->s_blocksize = 1 << ds->ds_block_shift;
433 sb->s_blocksize_bits = ds->ds_block_shift;
434 super->s_writesize = 1 << ds->ds_write_shift;
435 super->s_writeshift = ds->ds_write_shift;
436 super->s_no_segs = super->s_size >> super->s_segshift;
437 super->s_no_blocks = super->s_segsize >> sb->s_blocksize_bits;
438 super->s_feature_incompat = be64_to_cpu(ds->ds_feature_incompat);
439 super->s_feature_ro_compat = be64_to_cpu(ds->ds_feature_ro_compat);
440 super->s_feature_compat = be64_to_cpu(ds->ds_feature_compat);
441 super->s_feature_flags = be64_to_cpu(ds->ds_feature_flags);
443 journal_for_each(i)
444 super->s_journal_seg[i] = be32_to_cpu(ds->ds_journal_seg[i]);
446 super->s_ifile_levels = ds->ds_ifile_levels;
447 super->s_iblock_levels = ds->ds_iblock_levels;
448 super->s_data_levels = ds->ds_data_levels;
449 super->s_total_levels = super->s_ifile_levels + super->s_iblock_levels
450 + super->s_data_levels;
451 page_cache_release(page);
452 return 0;
455 static int logfs_read_sb(struct super_block *sb, int read_only)
457 struct logfs_super *super = logfs_super(sb);
458 int ret;
460 super->s_btree_pool = mempool_create(32, btree_alloc, btree_free, NULL);
461 if (!super->s_btree_pool)
462 return -ENOMEM;
464 btree_init_mempool64(&super->s_shadow_tree.new, super->s_btree_pool);
465 btree_init_mempool64(&super->s_shadow_tree.old, super->s_btree_pool);
466 btree_init_mempool32(&super->s_shadow_tree.segment_map,
467 super->s_btree_pool);
469 ret = logfs_init_mapping(sb);
470 if (ret)
471 return ret;
473 ret = __logfs_read_sb(sb);
474 if (ret)
475 return ret;
477 if (super->s_feature_incompat & ~LOGFS_FEATURES_INCOMPAT)
478 return -EIO;
479 if ((super->s_feature_ro_compat & ~LOGFS_FEATURES_RO_COMPAT) &&
480 !read_only)
481 return -EIO;
483 mutex_init(&super->s_dirop_mutex);
484 mutex_init(&super->s_object_alias_mutex);
485 INIT_LIST_HEAD(&super->s_freeing_list);
487 ret = logfs_init_rw(sb);
488 if (ret)
489 return ret;
491 ret = logfs_init_areas(sb);
492 if (ret)
493 return ret;
495 ret = logfs_init_gc(sb);
496 if (ret)
497 return ret;
499 ret = logfs_init_journal(sb);
500 if (ret)
501 return ret;
503 return 0;
506 static void logfs_kill_sb(struct super_block *sb)
508 struct logfs_super *super = logfs_super(sb);
510 log_super("LogFS: Start unmounting\n");
511 /* Alias entries slow down mount, so evict as many as possible */
512 sync_filesystem(sb);
513 logfs_write_anchor(sb);
516 * From this point on alias entries are simply dropped - and any
517 * writes to the object store are considered bugs.
519 super->s_flags |= LOGFS_SB_FLAG_SHUTDOWN;
520 log_super("LogFS: Now in shutdown\n");
521 generic_shutdown_super(sb);
523 BUG_ON(super->s_dirty_used_bytes || super->s_dirty_free_bytes);
525 logfs_cleanup_gc(sb);
526 logfs_cleanup_journal(sb);
527 logfs_cleanup_areas(sb);
528 logfs_cleanup_rw(sb);
529 if (super->s_erase_page)
530 __free_page(super->s_erase_page);
531 super->s_devops->put_device(super);
532 logfs_mempool_destroy(super->s_btree_pool);
533 logfs_mempool_destroy(super->s_alias_pool);
534 kfree(super);
535 log_super("LogFS: Finished unmounting\n");
538 static struct dentry *logfs_get_sb_device(struct logfs_super *super,
539 struct file_system_type *type, int flags)
541 struct super_block *sb;
542 int err = -ENOMEM;
543 static int mount_count;
545 log_super("LogFS: Start mount %x\n", mount_count++);
547 err = -EINVAL;
548 sb = sget(type, logfs_sb_test, logfs_sb_set, super);
549 if (IS_ERR(sb)) {
550 super->s_devops->put_device(super);
551 kfree(super);
552 return ERR_CAST(sb);
555 if (sb->s_root) {
556 /* Device is already in use */
557 super->s_devops->put_device(super);
558 kfree(super);
559 return dget(sb->s_root);
563 * sb->s_maxbytes is limited to 8TB. On 32bit systems, the page cache
564 * only covers 16TB and the upper 8TB are used for indirect blocks.
565 * On 64bit system we could bump up the limit, but that would make
566 * the filesystem incompatible with 32bit systems.
568 sb->s_maxbytes = (1ull << 43) - 1;
569 sb->s_op = &logfs_super_operations;
570 sb->s_flags = flags | MS_NOATIME;
572 err = logfs_read_sb(sb, sb->s_flags & MS_RDONLY);
573 if (err)
574 goto err1;
576 sb->s_flags |= MS_ACTIVE;
577 err = logfs_get_sb_final(sb);
578 if (err) {
579 deactivate_locked_super(sb);
580 return ERR_PTR(err);
582 return dget(sb->s_root);
584 err1:
585 /* no ->s_root, no ->put_super() */
586 iput(super->s_master_inode);
587 iput(super->s_segfile_inode);
588 iput(super->s_mapping_inode);
589 deactivate_locked_super(sb);
590 return ERR_PTR(err);
593 static struct dentry *logfs_mount(struct file_system_type *type, int flags,
594 const char *devname, void *data)
596 ulong mtdnr;
597 struct logfs_super *super;
598 int err;
600 super = kzalloc(sizeof(*super), GFP_KERNEL);
601 if (!super)
602 return ERR_PTR(-ENOMEM);
604 if (!devname)
605 err = logfs_get_sb_bdev(super, type, devname);
606 else if (strncmp(devname, "mtd", 3))
607 err = logfs_get_sb_bdev(super, type, devname);
608 else {
609 char *garbage;
610 mtdnr = simple_strtoul(devname+3, &garbage, 0);
611 if (*garbage)
612 err = -EINVAL;
613 else
614 err = logfs_get_sb_mtd(super, mtdnr);
617 if (err) {
618 kfree(super);
619 return ERR_PTR(err);
622 return logfs_get_sb_device(super, type, flags);
625 static struct file_system_type logfs_fs_type = {
626 .owner = THIS_MODULE,
627 .name = "logfs",
628 .mount = logfs_mount,
629 .kill_sb = logfs_kill_sb,
630 .fs_flags = FS_REQUIRES_DEV,
634 static int __init logfs_init(void)
636 int ret;
638 emergency_page = alloc_pages(GFP_KERNEL, 0);
639 if (!emergency_page)
640 return -ENOMEM;
642 ret = logfs_compr_init();
643 if (ret)
644 goto out1;
646 ret = logfs_init_inode_cache();
647 if (ret)
648 goto out2;
650 return register_filesystem(&logfs_fs_type);
651 out2:
652 logfs_compr_exit();
653 out1:
654 __free_pages(emergency_page, 0);
655 return ret;
658 static void __exit logfs_exit(void)
660 unregister_filesystem(&logfs_fs_type);
661 logfs_destroy_inode_cache();
662 logfs_compr_exit();
663 __free_pages(emergency_page, 0);
666 module_init(logfs_init);
667 module_exit(logfs_exit);
669 MODULE_LICENSE("GPL v2");
670 MODULE_AUTHOR("Joern Engel <joern@logfs.org>");
671 MODULE_DESCRIPTION("scalable flash filesystem");