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
13 #include <linux/bio.h>
14 #include <linux/mtd/mtd.h>
15 #include <linux/statfs.h>
16 #include <linux/buffer_head.h>
18 static DEFINE_MUTEX(emergency_mutex
);
19 static struct page
*emergency_page
;
21 struct page
*emergency_read_begin(struct address_space
*mapping
, pgoff_t index
)
23 filler_t
*filler
= (filler_t
*)mapping
->a_ops
->readpage
;
27 page
= read_cache_page(mapping
, index
, filler
, NULL
);
31 /* No more pages available, switch to emergency page */
32 printk(KERN_INFO
"Logfs: Using emergency page\n");
33 mutex_lock(&emergency_mutex
);
34 err
= filler(NULL
, emergency_page
);
36 mutex_unlock(&emergency_mutex
);
37 printk(KERN_EMERG
"Logfs: Error reading emergency page\n");
40 return emergency_page
;
43 void emergency_read_end(struct page
*page
)
45 if (page
== emergency_page
)
46 mutex_unlock(&emergency_mutex
);
48 page_cache_release(page
);
51 static void dump_segfile(struct super_block
*sb
)
53 struct logfs_super
*super
= logfs_super(sb
);
54 struct logfs_segment_entry se
;
57 for (segno
= 0; segno
< super
->s_no_segs
; segno
++) {
58 logfs_get_segment_entry(sb
, segno
, &se
);
59 printk("%3x: %6x %8x", segno
, be32_to_cpu(se
.ec_level
),
60 be32_to_cpu(se
.valid
));
61 if (++segno
< super
->s_no_segs
) {
62 logfs_get_segment_entry(sb
, segno
, &se
);
63 printk(" %6x %8x", be32_to_cpu(se
.ec_level
),
64 be32_to_cpu(se
.valid
));
66 if (++segno
< super
->s_no_segs
) {
67 logfs_get_segment_entry(sb
, segno
, &se
);
68 printk(" %6x %8x", be32_to_cpu(se
.ec_level
),
69 be32_to_cpu(se
.valid
));
71 if (++segno
< super
->s_no_segs
) {
72 logfs_get_segment_entry(sb
, segno
, &se
);
73 printk(" %6x %8x", be32_to_cpu(se
.ec_level
),
74 be32_to_cpu(se
.valid
));
81 * logfs_crash_dump - dump debug information to device
83 * The LogFS superblock only occupies part of a segment. This function will
84 * write as much debug information as it can gather into the spare space.
86 void logfs_crash_dump(struct super_block
*sb
)
92 * TODO: move to lib/string.c
95 * memchr_inv - Find a character in an area of memory.
97 * @c: The byte to search for
98 * @n: The size of the area.
100 * returns the address of the first character other than @c, or %NULL
101 * if the whole buffer contains just @c.
103 void *memchr_inv(const void *s
, int c
, size_t n
)
105 const unsigned char *p
= s
;
107 if ((unsigned char)c
!= *p
++)
108 return (void *)(p
- 1);
114 * FIXME: There should be a reserve for root, similar to ext2.
116 int logfs_statfs(struct dentry
*dentry
, struct kstatfs
*stats
)
118 struct super_block
*sb
= dentry
->d_sb
;
119 struct logfs_super
*super
= logfs_super(sb
);
121 stats
->f_type
= LOGFS_MAGIC_U32
;
122 stats
->f_bsize
= sb
->s_blocksize
;
123 stats
->f_blocks
= super
->s_size
>> LOGFS_BLOCK_BITS
>> 3;
124 stats
->f_bfree
= super
->s_free_bytes
>> sb
->s_blocksize_bits
;
125 stats
->f_bavail
= super
->s_free_bytes
>> sb
->s_blocksize_bits
;
128 stats
->f_namelen
= LOGFS_MAX_NAMELEN
;
132 static int logfs_sb_set(struct super_block
*sb
, void *_super
)
134 struct logfs_super
*super
= _super
;
136 sb
->s_fs_info
= super
;
137 sb
->s_mtd
= super
->s_mtd
;
138 sb
->s_bdev
= super
->s_bdev
;
142 static int logfs_sb_test(struct super_block
*sb
, void *_super
)
144 struct logfs_super
*super
= _super
;
145 struct mtd_info
*mtd
= super
->s_mtd
;
147 if (mtd
&& sb
->s_mtd
== mtd
)
149 if (super
->s_bdev
&& sb
->s_bdev
== super
->s_bdev
)
154 static void set_segment_header(struct logfs_segment_header
*sh
, u8 type
,
155 u8 level
, u32 segno
, u32 ec
)
160 sh
->segno
= cpu_to_be32(segno
);
161 sh
->ec
= cpu_to_be32(ec
);
162 sh
->gec
= cpu_to_be64(segno
);
163 sh
->crc
= logfs_crc32(sh
, LOGFS_SEGMENT_HEADERSIZE
, 4);
166 static void logfs_write_ds(struct super_block
*sb
, struct logfs_disk_super
*ds
,
169 struct logfs_super
*super
= logfs_super(sb
);
170 struct logfs_segment_header
*sh
= &ds
->ds_sh
;
173 memset(ds
, 0, sizeof(*ds
));
174 set_segment_header(sh
, SEG_SUPER
, 0, segno
, ec
);
176 ds
->ds_ifile_levels
= super
->s_ifile_levels
;
177 ds
->ds_iblock_levels
= super
->s_iblock_levels
;
178 ds
->ds_data_levels
= super
->s_data_levels
; /* XXX: Remove */
179 ds
->ds_segment_shift
= super
->s_segshift
;
180 ds
->ds_block_shift
= sb
->s_blocksize_bits
;
181 ds
->ds_write_shift
= super
->s_writeshift
;
182 ds
->ds_filesystem_size
= cpu_to_be64(super
->s_size
);
183 ds
->ds_segment_size
= cpu_to_be32(super
->s_segsize
);
184 ds
->ds_bad_seg_reserve
= cpu_to_be32(super
->s_bad_seg_reserve
);
185 ds
->ds_feature_incompat
= cpu_to_be64(super
->s_feature_incompat
);
186 ds
->ds_feature_ro_compat
= cpu_to_be64(super
->s_feature_ro_compat
);
187 ds
->ds_feature_compat
= cpu_to_be64(super
->s_feature_compat
);
188 ds
->ds_feature_flags
= cpu_to_be64(super
->s_feature_flags
);
189 ds
->ds_root_reserve
= cpu_to_be64(super
->s_root_reserve
);
190 ds
->ds_speed_reserve
= cpu_to_be64(super
->s_speed_reserve
);
192 ds
->ds_journal_seg
[i
] = cpu_to_be32(super
->s_journal_seg
[i
]);
193 ds
->ds_magic
= cpu_to_be64(LOGFS_MAGIC
);
194 ds
->ds_crc
= logfs_crc32(ds
, sizeof(*ds
),
195 LOGFS_SEGMENT_HEADERSIZE
+ 12);
198 static int write_one_sb(struct super_block
*sb
,
199 struct page
*(*find_sb
)(struct super_block
*sb
, u64
*ofs
))
201 struct logfs_super
*super
= logfs_super(sb
);
202 struct logfs_disk_super
*ds
;
203 struct logfs_segment_entry se
;
209 page
= find_sb(sb
, &ofs
);
212 ds
= page_address(page
);
213 segno
= seg_no(sb
, ofs
);
214 logfs_get_segment_entry(sb
, segno
, &se
);
215 ec
= be32_to_cpu(se
.ec_level
) >> 4;
217 logfs_set_segment_erased(sb
, segno
, ec
, 0);
218 logfs_write_ds(sb
, ds
, segno
, ec
);
219 err
= super
->s_devops
->write_sb(sb
, page
);
220 page_cache_release(page
);
224 int logfs_write_sb(struct super_block
*sb
)
226 struct logfs_super
*super
= logfs_super(sb
);
229 /* First superblock */
230 err
= write_one_sb(sb
, super
->s_devops
->find_first_sb
);
234 /* Last superblock */
235 err
= write_one_sb(sb
, super
->s_devops
->find_last_sb
);
241 static int ds_cmp(const void *ds0
, const void *ds1
)
243 size_t len
= sizeof(struct logfs_disk_super
);
245 /* We know the segment headers differ, so ignore them */
246 len
-= LOGFS_SEGMENT_HEADERSIZE
;
247 ds0
+= LOGFS_SEGMENT_HEADERSIZE
;
248 ds1
+= LOGFS_SEGMENT_HEADERSIZE
;
249 return memcmp(ds0
, ds1
, len
);
252 static int logfs_recover_sb(struct super_block
*sb
)
254 struct logfs_super
*super
= logfs_super(sb
);
255 struct logfs_disk_super _ds0
, *ds0
= &_ds0
;
256 struct logfs_disk_super _ds1
, *ds1
= &_ds1
;
257 int err
, valid0
, valid1
;
259 /* read first superblock */
260 err
= wbuf_read(sb
, super
->s_sb_ofs
[0], sizeof(*ds0
), ds0
);
263 /* read last superblock */
264 err
= wbuf_read(sb
, super
->s_sb_ofs
[1], sizeof(*ds1
), ds1
);
267 valid0
= logfs_check_ds(ds0
) == 0;
268 valid1
= logfs_check_ds(ds1
) == 0;
270 if (!valid0
&& valid1
) {
271 printk(KERN_INFO
"First superblock is invalid - fixing.\n");
272 return write_one_sb(sb
, super
->s_devops
->find_first_sb
);
274 if (valid0
&& !valid1
) {
275 printk(KERN_INFO
"Last superblock is invalid - fixing.\n");
276 return write_one_sb(sb
, super
->s_devops
->find_last_sb
);
278 if (valid0
&& valid1
&& ds_cmp(ds0
, ds1
)) {
279 printk(KERN_INFO
"Superblocks don't match - fixing.\n");
280 return write_one_sb(sb
, super
->s_devops
->find_last_sb
);
282 /* If neither is valid now, something's wrong. Didn't we properly
283 * check them before?!? */
284 BUG_ON(!valid0
&& !valid1
);
288 static int logfs_make_writeable(struct super_block
*sb
)
292 /* Repair any broken superblock copies */
293 err
= logfs_recover_sb(sb
);
297 /* Check areas for trailing unaccounted data */
298 err
= logfs_check_areas(sb
);
302 err
= logfs_open_segfile(sb
);
306 /* Do one GC pass before any data gets dirtied */
309 /* after all initializations are done, replay the journal
310 * for rw-mounts, if necessary */
311 err
= logfs_replay_journal(sb
);
318 static int logfs_get_sb_final(struct super_block
*sb
, struct vfsmount
*mnt
)
320 struct logfs_super
*super
= logfs_super(sb
);
321 struct inode
*rootdir
;
325 rootdir
= logfs_iget(sb
, LOGFS_INO_ROOT
);
329 sb
->s_root
= d_alloc_root(rootdir
);
333 super
->s_erase_page
= alloc_pages(GFP_KERNEL
, 0);
334 if (!super
->s_erase_page
)
336 memset(page_address(super
->s_erase_page
), 0xFF, PAGE_SIZE
);
338 /* FIXME: check for read-only mounts */
339 err
= logfs_make_writeable(sb
);
343 log_super("LogFS: Finished mounting\n");
344 simple_set_mnt(mnt
, sb
);
348 __free_page(super
->s_erase_page
);
352 iput(logfs_super(sb
)->s_master_inode
);
356 int logfs_check_ds(struct logfs_disk_super
*ds
)
358 struct logfs_segment_header
*sh
= &ds
->ds_sh
;
360 if (ds
->ds_magic
!= cpu_to_be64(LOGFS_MAGIC
))
362 if (sh
->crc
!= logfs_crc32(sh
, LOGFS_SEGMENT_HEADERSIZE
, 4))
364 if (ds
->ds_crc
!= logfs_crc32(ds
, sizeof(*ds
),
365 LOGFS_SEGMENT_HEADERSIZE
+ 12))
370 static struct page
*find_super_block(struct super_block
*sb
)
372 struct logfs_super
*super
= logfs_super(sb
);
373 struct page
*first
, *last
;
375 first
= super
->s_devops
->find_first_sb(sb
, &super
->s_sb_ofs
[0]);
376 if (!first
|| IS_ERR(first
))
378 last
= super
->s_devops
->find_last_sb(sb
, &super
->s_sb_ofs
[1]);
379 if (!last
|| IS_ERR(first
)) {
380 page_cache_release(first
);
384 if (!logfs_check_ds(page_address(first
))) {
385 page_cache_release(last
);
389 /* First one didn't work, try the second superblock */
390 if (!logfs_check_ds(page_address(last
))) {
391 page_cache_release(first
);
395 /* Neither worked, sorry folks */
396 page_cache_release(first
);
397 page_cache_release(last
);
401 static int __logfs_read_sb(struct super_block
*sb
)
403 struct logfs_super
*super
= logfs_super(sb
);
405 struct logfs_disk_super
*ds
;
408 page
= find_super_block(sb
);
412 ds
= page_address(page
);
413 super
->s_size
= be64_to_cpu(ds
->ds_filesystem_size
);
414 super
->s_root_reserve
= be64_to_cpu(ds
->ds_root_reserve
);
415 super
->s_speed_reserve
= be64_to_cpu(ds
->ds_speed_reserve
);
416 super
->s_bad_seg_reserve
= be32_to_cpu(ds
->ds_bad_seg_reserve
);
417 super
->s_segsize
= 1 << ds
->ds_segment_shift
;
418 super
->s_segmask
= (1 << ds
->ds_segment_shift
) - 1;
419 super
->s_segshift
= ds
->ds_segment_shift
;
420 sb
->s_blocksize
= 1 << ds
->ds_block_shift
;
421 sb
->s_blocksize_bits
= ds
->ds_block_shift
;
422 super
->s_writesize
= 1 << ds
->ds_write_shift
;
423 super
->s_writeshift
= ds
->ds_write_shift
;
424 super
->s_no_segs
= super
->s_size
>> super
->s_segshift
;
425 super
->s_no_blocks
= super
->s_segsize
>> sb
->s_blocksize_bits
;
426 super
->s_feature_incompat
= be64_to_cpu(ds
->ds_feature_incompat
);
427 super
->s_feature_ro_compat
= be64_to_cpu(ds
->ds_feature_ro_compat
);
428 super
->s_feature_compat
= be64_to_cpu(ds
->ds_feature_compat
);
429 super
->s_feature_flags
= be64_to_cpu(ds
->ds_feature_flags
);
432 super
->s_journal_seg
[i
] = be32_to_cpu(ds
->ds_journal_seg
[i
]);
434 super
->s_ifile_levels
= ds
->ds_ifile_levels
;
435 super
->s_iblock_levels
= ds
->ds_iblock_levels
;
436 super
->s_data_levels
= ds
->ds_data_levels
;
437 super
->s_total_levels
= super
->s_ifile_levels
+ super
->s_iblock_levels
438 + super
->s_data_levels
;
439 page_cache_release(page
);
443 static int logfs_read_sb(struct super_block
*sb
, int read_only
)
445 struct logfs_super
*super
= logfs_super(sb
);
448 super
->s_btree_pool
= mempool_create(32, btree_alloc
, btree_free
, NULL
);
449 if (!super
->s_btree_pool
)
452 btree_init_mempool64(&super
->s_shadow_tree
.new, super
->s_btree_pool
);
453 btree_init_mempool64(&super
->s_shadow_tree
.old
, super
->s_btree_pool
);
455 ret
= logfs_init_mapping(sb
);
459 ret
= __logfs_read_sb(sb
);
463 if (super
->s_feature_incompat
& ~LOGFS_FEATURES_INCOMPAT
)
465 if ((super
->s_feature_ro_compat
& ~LOGFS_FEATURES_RO_COMPAT
) &&
469 mutex_init(&super
->s_dirop_mutex
);
470 mutex_init(&super
->s_object_alias_mutex
);
471 INIT_LIST_HEAD(&super
->s_freeing_list
);
473 ret
= logfs_init_rw(sb
);
477 ret
= logfs_init_areas(sb
);
481 ret
= logfs_init_gc(sb
);
485 ret
= logfs_init_journal(sb
);
492 static void logfs_kill_sb(struct super_block
*sb
)
494 struct logfs_super
*super
= logfs_super(sb
);
496 log_super("LogFS: Start unmounting\n");
497 /* Alias entries slow down mount, so evict as many as possible */
499 logfs_write_anchor(sb
);
502 * From this point on alias entries are simply dropped - and any
503 * writes to the object store are considered bugs.
505 super
->s_flags
|= LOGFS_SB_FLAG_SHUTDOWN
;
506 log_super("LogFS: Now in shutdown\n");
507 generic_shutdown_super(sb
);
509 BUG_ON(super
->s_dirty_used_bytes
|| super
->s_dirty_free_bytes
);
511 logfs_cleanup_gc(sb
);
512 logfs_cleanup_journal(sb
);
513 logfs_cleanup_areas(sb
);
514 logfs_cleanup_rw(sb
);
515 if (super
->s_erase_page
)
516 __free_page(super
->s_erase_page
);
517 super
->s_devops
->put_device(sb
);
518 mempool_destroy(super
->s_btree_pool
);
519 mempool_destroy(super
->s_alias_pool
);
521 log_super("LogFS: Finished unmounting\n");
524 int logfs_get_sb_device(struct file_system_type
*type
, int flags
,
525 struct mtd_info
*mtd
, struct block_device
*bdev
,
526 const struct logfs_device_ops
*devops
, struct vfsmount
*mnt
)
528 struct logfs_super
*super
;
529 struct super_block
*sb
;
531 static int mount_count
;
533 log_super("LogFS: Start mount %x\n", mount_count
++);
534 super
= kzalloc(sizeof(*super
), GFP_KERNEL
);
539 super
->s_bdev
= bdev
;
541 sb
= sget(type
, logfs_sb_test
, logfs_sb_set
, super
);
546 /* Device is already in use */
548 simple_set_mnt(mnt
, sb
);
552 super
->s_devops
= devops
;
555 * sb->s_maxbytes is limited to 8TB. On 32bit systems, the page cache
556 * only covers 16TB and the upper 8TB are used for indirect blocks.
557 * On 64bit system we could bump up the limit, but that would make
558 * the filesystem incompatible with 32bit systems.
560 sb
->s_maxbytes
= (1ull << 43) - 1;
561 sb
->s_op
= &logfs_super_operations
;
562 sb
->s_flags
= flags
| MS_NOATIME
;
564 err
= logfs_read_sb(sb
, sb
->s_flags
& MS_RDONLY
);
568 sb
->s_flags
|= MS_ACTIVE
;
569 err
= logfs_get_sb_final(sb
, mnt
);
575 up_write(&sb
->s_umount
);
576 deactivate_super(sb
);
580 //devops->put_device(sb);
584 static int logfs_get_sb(struct file_system_type
*type
, int flags
,
585 const char *devname
, void *data
, struct vfsmount
*mnt
)
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
);
596 mtdnr
= simple_strtoul(devname
+3, &garbage
, 0);
601 return logfs_get_sb_mtd(type
, flags
, mtdnr
, mnt
);
604 static struct file_system_type logfs_fs_type
= {
605 .owner
= THIS_MODULE
,
607 .get_sb
= logfs_get_sb
,
608 .kill_sb
= logfs_kill_sb
,
609 .fs_flags
= FS_REQUIRES_DEV
,
613 static int __init
logfs_init(void)
617 emergency_page
= alloc_pages(GFP_KERNEL
, 0);
621 ret
= logfs_compr_init();
625 ret
= logfs_init_inode_cache();
629 return register_filesystem(&logfs_fs_type
);
633 __free_pages(emergency_page
, 0);
637 static void __exit
logfs_exit(void)
639 unregister_filesystem(&logfs_fs_type
);
640 logfs_destroy_inode_cache();
642 __free_pages(emergency_page
, 0);
645 module_init(logfs_init
);
646 module_exit(logfs_exit
);
648 MODULE_LICENSE("GPL v2");
649 MODULE_AUTHOR("Joern Engel <joern@logfs.org>");
650 MODULE_DESCRIPTION("scalable flash filesystem");