4 * Copyright (C) 2005-2009
5 * Neil Brown <neilb@suse.de>
6 * Released under the GPL, version 2
10 #include <linux/namei.h>
11 #include <linux/crc32.h>
12 #include <linux/statfs.h>
13 #include <linux/mount.h>
14 #include <linux/exportfs.h>
15 #include <linux/slab.h>
17 static struct super_operations lafs_sops;
18 static const struct export_operations lafs_export_ops;
20 /*---------------------------------------------------------------------
21 * Write out state and super blocks
22 * The super blocks only need to be written when the geometry of the
23 * array changes such as when a device is added, removed, or resized.
24 * So we don't bother with that just yet.
25 * The state block needs to be written - twice on each device - whenever
26 * a checkpoint is completed. All copies are identical and the writes
27 * proceed in parallel. There are 4 stateblock locations on each device.
28 * 2 are typically less recent than the other two. We over-write the
30 * FIXME on a RAID4 we should pad the write to be a full stripe.
32 * Locking issues: This is called from the checkpoint thread and so
33 * it does not race with anything else exclusive to that thread.
34 * The nonlog information needs to be reviewed once that functionality
38 int lafs_write_state(struct fs *fs)
40 struct lafs_state *st;
45 st->seq = cpu_to_le32(fs->seq);
46 st->nonlog_segment = cpu_to_le32(fs->nonlog_segment);
47 st->nonlog_dev = cpu_to_le16(fs->nonlog_dev);
48 st->nonlog_offset = cpu_to_le16(fs->nonlog_offset);
49 st->nextyouth = cpu_to_le16(fs->youth_next);
50 st->checkpointcluster = cpu_to_le64(fs->checkpointcluster);
51 for (i = 0; i < fs->maxsnapshot; i++)
52 st->root_inodes[i] = cpu_to_le64(fs->ss[i].root_addr);
55 st->checksum = crc32_le(0, (unsigned char *)st, fs->statesize);
57 for (d = 0; d < fs->devices ; d++)
58 for (i = (fs->seq & 1); i < 4 ; i += 2)
59 lafs_super_write(fs, d, fs->devs[d].stateaddr[i] >> 9,
60 (char *)st, fs->statesize);
62 /* FIXME what about a write error ??? */
67 valid_devblock(struct lafs_dev *db, sector_t addr, sector_t size)
69 /* check that this devblock is valid, given that
70 * it was found at sector 'addr'
77 if (strncmp(db->idtag, "LaFS-DeviceBlock", 16) != 0)
79 if (le32_to_cpu(db->version) != LAFS_DEV_VERS)
81 /* uuid can be anything */
84 crc2 = crc32_le(0, (unsigned char *)db, LAFS_DEVBLK_SIZE);
87 dprintk("%lx != %lx\n", (unsigned long)crc,
92 byteaddr = (u64)addr << 9; /* convert to byte */
93 if (le64_to_cpu(db->devaddr[0]) != byteaddr &&
94 le64_to_cpu(db->devaddr[1]) != byteaddr)
97 if (db->statebits < 10 || db->statebits > 16)
99 if (db->blockbits < 9 || db->blockbits > 20)
101 if (le16_to_cpu(db->width) < 1 || le16_to_cpu(db->width) >= 512)
103 if (le32_to_cpu(db->stride) < 1)
105 /* devaddr[0] must be early, [1] must be late */
106 if (le64_to_cpu(db->devaddr[0]) >=
107 le64_to_cpu(db->segment_offset))
110 if (le64_to_cpu(db->devaddr[1]) <
111 le64_to_cpu(db->segment_offset) +
112 ((((sector_t)le32_to_cpu(db->segment_count)
113 * le32_to_cpu(db->segment_size)))
117 /* 2 is an absolute minimum segment size, a few hundred is more
118 * likely. We'll put a lower limit of 8, and an upper of 800000
120 if (le32_to_cpu(db->segment_size) < 8 ||
121 le32_to_cpu(db->segment_size) > 800000)
124 if (le32_to_cpu(db->segment_offset) >
125 (le32_to_cpu(db->segment_size)<<db->blockbits) * 10)
128 /* The 4 state blocks live before the first or after the last segment.
129 * The distance from start of first to end of last is either:
130 * - segment_count * segment_size if width*stride <= segment_size
131 * - (width-1) * stride + segment_size / width * segment_count
132 * if width * stride > segment_size
134 segsize = le32_to_cpu(db->segment_size);
135 segsize *= le32_to_cpu(db->segment_count);
136 if (le16_to_cpu(db->width) * le32_to_cpu(db->stride)
137 > le32_to_cpu(db->segment_size)) {
138 int stride = le32_to_cpu(db->stride);
139 int width = le16_to_cpu(db->width);
141 sector_div(segsize, width);
142 segsize += (width - 1) * stride;
144 segsize <<= db->blockbits;
145 for (i = 0; i < 4; i++) {
146 sector_t addr = le64_to_cpu(db->stateaddr[i]);
147 int offset = le32_to_cpu(db->segment_offset);
148 if (addr + (1<<db->statebits) > offset &&
149 addr < offset + segsize)
151 if (addr + (1<<db->statebits) > (size << db->blockbits))
155 /* Check all segments fit within device */
156 if (le32_to_cpu(db->segment_offset) + segsize > (size << db->blockbits))
159 /* I guess it look sane enough... */
164 compare_dev(struct lafs_dev *orig, struct lafs_dev *new)
166 /* Both these are known to be valid.
168 * 0 if they are for same filesystem, but 'new' is older
169 * 1 if they are for same filesystem, and 'new' is newer
170 * -1 if they are for different filesystems
172 if (memcmp(orig->uuid, new->uuid, 16))
174 if (u32_after(le32_to_cpu(new->seq),
175 le32_to_cpu(orig->seq)))
181 valid_stateblock(struct lafs_state *st, struct lafs_dev *dv)
183 /* Given the 'dv' devblock, make sure 'st' is a valid
184 * and consistent stateblock
187 if (strncmp(st->idtag, "LaFS-State-Block", 16) != 0)
189 if (le32_to_cpu(st->version) != LAFS_STATE_VERS)
193 if (crc32_le(0, (unsigned char *)st, 1<<dv->statebits) != crc)
197 if (memcmp(st->uuid, dv->uuid, 16))
200 if (sizeof(*st) + le32_to_cpu(st->maxsnapshot) * 8
201 > (1<<dv->statebits))
204 /* Don't support RO sharing yet. */
212 compare_state(struct lafs_state *orig, struct lafs_state *new)
214 /* return 1 if 'new' is actually newer than 'orig'.
215 * We already know they are both valid and have the same
216 * uuid... I don't think there is anything else to be checked
218 return u32_after(le32_to_cpu(new->seq), le32_to_cpu(orig->seq));
223 * As we can have multiple devices, things are slightly non-obvious.
224 * The 'devname' can be either a device name, starting '/', or
225 * a filesytem name (not starting '/').
226 * The 'data' is a standard comma-separated list of options.
227 * For 'mount' these are:
229 * - devices in addition to 'dev_name'
231 * - A new device, with a superblock already present, to be added.
233 * - don't complain if not all devices are given
234 * ?? quota stuff, cleaning parameters,
236 * For 'remount', options are
237 * dev= - add another device
238 * new= - the device is being added.
245 int statebits, blockbits;
250 struct block_device *bdev;
251 struct lafs_dev *devblock;
252 struct lafs_state *stateblock;
253 int devchoice, statechoice;
258 count_devs(const char *name, char *data)
263 while (data && *data) {
264 if (strncmp(data, "dev=", 4) == 0)
266 if (strncmp(data, "new=", 4) == 0)
268 data = strchr(data, ',');
276 parse_opts(struct options *op, const char *name, char *data)
281 memset(op, 0, sizeof(*op));
282 op->devcnt = count_devs(name, data);
283 op->devlist = kzalloc(op->devcnt*sizeof(op->devlist[0]), GFP_KERNEL);
290 op->devlist[dv].is_name = 1;
291 op->devlist[dv++].dev = name;
294 while ((p = strsep(&data, ",")) != NULL) {
297 if (strncmp(p, "dev=", 4) == 0)
298 op->devlist[dv++].dev = p+4;
299 else if (strncmp(p, "new=", 4) == 0) {
300 op->devlist[dv].is_new = 1;
301 op->devlist[dv++].dev = p+4;
304 "LaFS: Unrecognised mount option \"%s\"\n", p);
315 lafs_load_super(struct block_device *bdev, void *opv, int silent)
317 /* Find the devblock and the stateblock for this device
319 * Only do basic internal consistancy checks. Inter-device
320 * checks happen later
322 struct options *op = opv;
325 sector_t sect, dev_addr = 0;
330 int have_dev = 0, have_state = 0;
333 dv = &op->devlist[op->curr_dev];
334 BUG_ON(dv->devblock);
335 BUG_ON(dv->stateblock);
337 n = queue_logical_block_size(bdev->bd_disk->queue);
338 if (n < LAFS_DEVBLK_SIZE)
339 n = LAFS_DEVBLK_SIZE;
340 BUG_ON(n > PAGE_SIZE);
341 dv->devblock = kmalloc(n, GFP_KERNEL);
344 pg = alloc_page(GFP_KERNEL);
348 devsize = i_size_read(bdev->bd_inode);
350 /* Now find a devblock, check the first two possible locations,
351 * and the last two. If two devblocks are found with different
352 * uuids, we are confused!
355 for (i = 0; i < 4; i++) {
356 /* try to read block at 'sect' */
357 int ok = lafs_sync_page_io(bdev, sect, 0, n, pg, READ);
359 if (ok && valid_devblock(page_address(pg), sect, devsize)) {
362 memcpy(dv->devblock, page_address(pg), n);
364 } else switch (compare_dev(dv->devblock,
366 case 0: /* older, do nothing */
368 case 1: /* newer, overwrite */
369 memcpy(dv->devblock, page_address(pg), n);
372 default: /* inconsistent --- HELP */
373 printk(KERN_ERR "LaFS: inconsistent device-blocks found.\n");
382 sect = devsize & ~(sector_t)(n-1);
387 /* FIXME - we've lost the read error, if it was significant */
391 printk(KERN_ERR "LaFS - no valid devblock found.\n");
395 /* OK, we have a valid devblock, that's nice.
396 * Now we should be able to find some stateblocks.
397 * The locations are in the devblock
399 n = le32_to_cpu(1<<dv->devblock->statebits);
401 n < queue_logical_block_size(bdev->bd_disk->queue) ||
403 printk(KERN_ERR "LaFS: statesize of %u not acceptable.\n", n);
407 dv->stateblock = kmalloc(n, GFP_KERNEL);
411 for (i = 0; i < 4; i++) {
413 sect = le64_to_cpu(dv->devblock->stateaddr[i])>>9;
414 ok = lafs_sync_page_io(bdev, sect, 0, n, pg, READ);
415 if (ok && valid_stateblock(page_address(pg), dv->devblock)) {
418 memcpy(dv->stateblock, page_address(pg), n);
420 } else if (compare_state(dv->stateblock,
422 memcpy(dv->stateblock, page_address(pg), n);
430 dv->devchoice = dev_addr;
431 dv->statechoice = state_addr;
435 printk(KERN_ERR "LaFS: no valid stateblock found.\n");
438 page_cache_release(pg);
443 check_devs(struct options *op)
445 /* Check we have enough, that they are for the same
446 * uuid, and they they don't overlap
447 * Also check that 'seq' number of devblocks
450 int seqlo = le32_to_cpu(op->devlist[0].devblock->seq);
451 int seqhi = le32_to_cpu(op->devlist[0].devblock->seq);
456 for (i = 1; i < op->devcnt; i++) {
457 if (memcmp(op->devlist[0].stateblock->uuid,
458 op->devlist[i].stateblock->uuid,
462 if (le32_to_cpu(op->devlist[i].devblock->seq) == seqlo)
464 else if (le32_to_cpu(op->devlist[i].devblock->seq) == seqlo+1) {
467 } else if (le32_to_cpu(op->devlist[i].devblock->seq) == seqhi-1)
472 if (u32_after(le32_to_cpu(op->devlist[i].stateblock->seq),
473 le32_to_cpu(op->devlist[newstate].
477 if (le32_to_cpu(op->devlist[newstate].stateblock->devices)
481 op->statebits = op->devlist[0].devblock->statebits;
482 op->blockbits = op->devlist[0].devblock->blockbits;
484 /* Now check devices don't overlap in start/size.
485 * We do a simple quadratic search
487 for (i = 0; i < op->devcnt; i++)
488 for (j = 0; j < op->devcnt; j++)
490 if (le64_to_cpu(op->devlist[i].devblock->start) <
491 le64_to_cpu(op->devlist[j].devblock->start) &&
493 le64_to_cpu(op->devlist[i].devblock->start)+
494 le64_to_cpu(op->devlist[i].devblock->size) >
495 le64_to_cpu(op->devlist[j].devblock->start))
500 /* we identify lafs superblocks by the filesystem uuid. This means
501 * that block-level snapshots cannot be mounted. You should use
502 * fs-level snapshots instead.
504 static int sb_test(struct super_block *sb, void *data)
506 struct sb_key *ptn = data;
507 struct sb_key *sk = sb->s_fs_info;
508 return memcmp(ptn->fs->state->uuid,
509 sk->fs->state->uuid, 16) == 0;
512 static int sb_set(struct super_block *sb, void *data)
514 struct sb_key *ptn = data;
516 return set_anon_super(sb, NULL);
521 lafs_load(struct fs *fs, struct options *op, int newest)
523 /* We seem to have a full set of devices for the filesystem.
524 * Time to create our fs_info structure and fill it out.
525 * This only includes information from the dev and state blocks.
526 * Finding the root-inode comes a bit later.
528 struct lafs_state *st;
533 st = fs->state = op->devlist[newest].stateblock;
534 op->devlist[newest].stateblock = NULL;
539 fs->seq = le32_to_cpu(st->seq);
540 fs->devices = op->devcnt;
541 fs->devs_loaded = fs->devices; /* FIXME use this or lose this */
542 fs->statesize = 1 << op->statebits;
543 fs->blocksize = 1 << op->blockbits;
544 fs->blocksize_bits = op->blockbits;
546 fs->nonlog_segment = le32_to_cpu(st->nonlog_segment);
547 fs->nonlog_dev = le16_to_cpu(st->nonlog_dev);
548 fs->nonlog_offset = le16_to_cpu(st->nonlog_offset);
549 fs->youth_next = le16_to_cpu(st->nextyouth);
550 fs->checkpoint_youth = fs->youth_next;
551 if (fs->youth_next < 8)
553 fs->scan.first_free_pass = 1;
554 fs->scan.free_dev = -1;
556 fs->maxsnapshot = le32_to_cpu(st->maxsnapshot);
558 fs->scan.free_usages = kmalloc(PAGE_SIZE, GFP_KERNEL);
559 err = lafs_segtrack_init(fs->segtrack);
561 fs->ss = kzalloc(sizeof(struct snapshot)*fs->maxsnapshot, GFP_KERNEL);
562 if (!fs->ss || !fs->scan.free_usages || err) {
568 fs->checkpointcluster = le64_to_cpu(st->checkpointcluster);
569 for (i = 0; i < fs->maxsnapshot; i++) {
570 fs->ss[i].root_addr =
571 le64_to_cpu(st->root_inodes[i]);
572 dprintk("root inode %d are %llu\n",
573 i, fs->ss[i].root_addr);
575 INIT_LIST_HEAD(&fs->pending_orphans);
576 INIT_LIST_HEAD(&fs->inode_index);
577 INIT_LIST_HEAD(&fs->phase_leafs[0]);
578 INIT_LIST_HEAD(&fs->phase_leafs[1]);
579 INIT_LIST_HEAD(&fs->clean_leafs);
580 INIT_LIST_HEAD(&fs->account_leafs);
581 atomic_set(&fs->sb_writes_pending, 0);
582 init_waitqueue_head(&fs->sb_writes_wait);
583 init_waitqueue_head(&fs->async_complete);
584 init_waitqueue_head(&fs->trunc_wait);
585 mutex_init(&fs->cleaner.lock);
586 spin_lock_init(&fs->stable_lock);
587 spin_lock_init(&fs->alloc_lock);
588 spin_lock_init(&fs->lock);
589 init_waitqueue_head(&fs->phase_wait);
591 INIT_WORK(&fs->done_work, lafs_done_work);
593 /* FIXME add congention and unplug functions to this bdi */
594 err = bdi_init(&fs->bdi);
599 fs->phase_locked = 0;
600 for (i = 0; i < WC_NUM; i++) {
602 mutex_init(&fs->wc[i].lock);
603 for (j = 0; j < 4 ; j++) {
604 atomic_set(&fs->wc[i].pending_cnt[j], 0);
605 INIT_LIST_HEAD(&fs->wc[i].pending_blocks[j]);
607 init_waitqueue_head(&fs->wc[i].pending_wait);
608 fs->wc[i].seg.dev = -1;
611 fs->max_newsegs = 32; /* FIXME this should be configurable */
614 fs->devs = kzalloc(sizeof(struct fs_dev)*fs->devices, GFP_KERNEL);
618 k = kzalloc(sizeof(*k), GFP_KERNEL);
620 fs->prime_sb = sget(&lafs_fs_type, sb_test, sb_set, k);
621 if (IS_ERR(fs->prime_sb)) {
623 err = PTR_ERR(fs->prime_sb);
626 if (fs->prime_sb->s_root) {
627 /* filesystem with this uuid already exists */
628 deactivate_locked_super(fs->prime_sb);
634 err = bdi_register_dev(&fs->bdi, fs->prime_sb->s_dev);
636 deactivate_locked_super(fs->prime_sb);
641 fs->prime_sb->s_bdi = &fs->bdi;
643 fs->prime_sb->s_blocksize = 1 << op->blockbits;
644 fs->prime_sb->s_blocksize_bits = op->blockbits;
645 fs->prime_sb->s_op = &lafs_sops;
646 fs->prime_sb->s_export_op = &lafs_export_ops;
647 fs->prime_sb->s_root = NULL;
649 /* We allow 29 bits for nanosecs, so they must be even. */
650 fs->prime_sb->s_time_gran = 2;
652 for (i = 0; i < fs->devices; i++) {
653 struct fs_dev *dv = &fs->devs[i];
654 struct devent *de = &op->devlist[i];
659 dv->devblk = de->devblock;
662 dv->recent_dev = de->devchoice;
663 dv->recent_state = de->statechoice;
665 dv->start = le64_to_cpu(dv->devblk->start);
666 dv->size = le64_to_cpu(dv->devblk->size);
667 dprintk("Dev %d seems to range %llu + %llu\n",
668 i, (unsigned long long)dv->start,
669 (unsigned long long)dv->size);
671 dv->width = le16_to_cpu(dv->devblk->width);
672 dv->stride = le32_to_cpu(dv->devblk->stride);
673 dv->segment_size = le32_to_cpu(dv->devblk->segment_size);
674 dv->segment_offset = le32_to_cpu(dv->devblk->segment_offset);
675 dv->segment_count = le32_to_cpu(dv->devblk->segment_count);
676 dv->usage_inum = le32_to_cpu(dv->devblk->usage_inum);
678 if (dv->segment_size > fs->max_segment)
679 fs->max_segment = dv->segment_size;
681 if (dv->width * dv->stride <= dv->segment_size) {
682 dv->tables_per_seg = dv->segment_size /
683 dv->width / dv->stride;
684 dv->rows_per_table = dv->stride;
685 dv->segment_stride = dv->segment_size;
687 dv->tables_per_seg = 1;
688 dv->rows_per_table = dv->segment_size / dv->width;
689 dv->segment_stride = dv->rows_per_table;
691 /* table size is the number of blocks in the segment usage
694 dv->tablesize = (dv->segment_count + (1<<(fs->blocksize_bits-1)) + 1)
695 >> (fs->blocksize_bits-1);
697 for (j = 0; j < 2; j++)
698 dv->devaddr[j] = le64_to_cpu(dv->devblk->devaddr[j]);
699 for (j = 0; j < 4; j++)
700 dv->stateaddr[j] = le64_to_cpu(dv->devblk->stateaddr[j]);
705 bdi_destroy(&fs->bdi);
706 kfree(fs->scan.free_usages);
707 lafs_segtrack_free(fs->segtrack);
714 static int show_orphans(struct fs *fs)
716 struct datablock *db;
717 printk("Orphans:\n");
718 list_for_each_entry(db, &fs->pending_orphans,
720 struct inode *ino = iget_my_inode(db);
721 printk("orphan=%s\n", strblk(&db->b));
723 lafs_print_tree(&LAFSI(ino)->iblock->b, 0);
726 printk("cleaner active: %d %d\n", fs->cleaner.active,
728 return 1; /* meaningless, but makes it easy to add to wait_event below */
731 static void lafs_kill_sb(struct super_block *sb)
733 struct fs *fs = fs_from_sb(sb);
734 /* Release the 'struct fs' */
737 /* FIXME should I refcount this when there are multiple
738 * filesets? How does that work?
741 /* Delay final destruction of the root inode */
742 /* FIXME all the sbs... */
743 set_bit(I_Deleting, &LAFSI(fs->ss[0].root)->iflags);
745 /* FIXME I'm not sure we should be waiting for the
746 * cleaner. Maybe we should just release all tc->cleaning
749 set_bit(CleanerDisabled, &fs->fsstate);
751 wait_event(fs->async_complete,
753 !test_bit(OrphansRunning, &fs->fsstate) &&
754 list_empty(&fs->pending_orphans) &&
755 fs->scan.done == 1 &&
756 fs->cleaner.active == 0);
758 if (LAFSI(fs->ss[0].root)->md.fs.accesstime) {
759 struct inode *i = LAFSI(fs->ss[0].root)->md.fs.accesstime;
760 LAFSI(fs->ss[0].root)->md.fs.accesstime = NULL;
764 kill_anon_super(fs->prime_sb);
766 bdi_destroy(&fs->bdi);
768 for (i = 0; i < fs->devices; i++) {
769 struct fs_dev *dv = &fs->devs[i];
771 blkdev_put(dv->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
774 /* Final checkpoint will have cleared out the leafs lists,
775 * so they should all be empty.
777 /* Lets see what is on the 'leaf' list? */
778 for (i = 0; i < 2; i++) {
780 dprintk("For phase %d\n", i);
782 list_for_each_entry(b, &fs->phase_leafs[i], lru) {
783 /* FIXME this only OK for readonly mounts.
785 getref(b, MKREF(release));
787 if (test_bit(B_Pinned, &b->flags)) {
788 /* didn't fix the pincnt !! */
789 printk("This was pinned: %s\n", strblk(b));
790 lafs_print_tree(b, 1);
793 putref(b, MKREF(release));
797 BUG_ON(!list_empty(&fs->clean_leafs));
799 flush_scheduled_work();
800 lafs_stop_thread(fs);
802 for (i = 0; i < 4; i++)
803 if (fs->cleaner.seg[i].chead)
804 put_page(fs->cleaner.seg[i].chead);
809 lafs_segtrack_free(fs->segtrack);
810 kfree(fs->scan.free_usages);
811 kfree(fs->prime_sb->s_fs_info);
816 lafs_put_super(struct super_block *sb)
818 struct fs *fs = fs_from_sb(sb);
820 struct lafs_inode *li;
822 /* If !fs->thread, we never really mounted the fs, so this
823 * cleanup is inappropriate .. and cannot work anyway.
826 lafs_checkpoint_lock(fs);
827 lafs_checkpoint_start(fs);
828 if (sb == fs->prime_sb)
829 /* Don't incorporate any more segusage/quota updates. */
830 set_bit(FinalCheckpoint, &fs->fsstate);
831 lafs_checkpoint_unlock_wait(fs);
832 lafs_cluster_wait_all(fs);
835 if (sb == fs->prime_sb) {
837 /* This is the main sb, not a snapshot or
839 * Now that all inodes have been invalidated we can do
840 * the final checkpoint.
842 lafs_close_all_segments(fs);
843 lafs_empty_segment_table(fs);
844 lafs_seg_put_all(fs);
848 for (d=0; d < fs->devices; d++)
849 if (fs->devs[d].segsum) {
850 iput(fs->devs[d].segsum);
851 fs->devs[d].segsum = NULL;
855 /* need to break a circular reference... */
856 for (ss = 0; ss < fs->maxsnapshot; ss++)
857 if (fs->ss[ss].root &&
858 fs->ss[ss].root->i_sb == sb) {
859 dprintk("Putting ss %d\n", ss);
860 li = LAFSI(fs->ss[ss].root);
861 if (test_bit(B_Realloc, &li->dblock->b.flags))
863 iput(fs->ss[ss].root);
864 fs->ss[ss].root = NULL;
870 lafs_get_devs(struct fs *fs, struct options *op, int flags)
875 for (i = 0; i < op->devcnt; i++) {
876 struct block_device *bdev;
879 bdev = blkdev_get_by_path(op->devlist[i].dev,
880 FMODE_READ|FMODE_WRITE|FMODE_EXCL,
885 err = lafs_load_super(bdev, op, flags & MS_SILENT ? 1 : 0);
888 op->devlist[i].bdev = bdev;
897 lafs_get_sb(struct file_system_type *fs_type,
898 int flags, const char *dev_name, void *data,
899 struct vfsmount *mnt)
901 /* as we may have multiple devices, some in 'data', we cannot just
902 * use get_sb_bdev, we need to roll-our-own.
903 * We call get_sb_bdev on *each* bdev, and make sure the returned
904 * superblocks are either all new, or all for the same filesystem.
905 * If the later, we return the primary.
906 * If the former, we init the filesystem copying static data
908 * First we 'blkdev_get_by_path' each device, exclusive to lafs
909 * Then we 'sget' a superblock that knows any/all the devices.
910 * This may be pre-existing, or may be new
911 * If new, it will be created knowing all devices.
912 * If pre-existing, and don't have correct device list, error
917 struct fs *fs = kzalloc(sizeof(*fs), GFP_KERNEL);
925 err = parse_opts(&op, dev_name, cdata);
929 /* We now have as list of device names. We call blkdev_get_by_path
930 * on each to collect some superblocks.
932 err = lafs_get_devs(fs, &op, flags);
936 /* Each device has a valid dev and state block. Hopefully they
937 * are all for the same filesystem. If they don't have the
938 * same uuid, we will bale-out here. We also check that we have
939 * enough, and that they don't overlap.
940 * While we are looking at state blocks, pick the newest.
942 newest = check_devs(&op);
948 /* So they seem to be the same - better create our
949 * 'fs' structure and fill it in
951 err = lafs_load(fs, &op, newest);
955 /* Well, all the devices check out. Now we need to find the
957 err = lafs_mount(fs);
959 err = lafs_start_thread(fs);
961 /* Don't wait for any scan to finish ... */
963 fs->checkpointing = 0;
964 deactivate_locked_super(fs->prime_sb);
966 fs->prime_sb->s_flags |= MS_ACTIVE;
967 simple_set_mnt(mnt, fs->prime_sb);
969 /* And there you have it. Filesystem all mounted, root dir found,
970 * metadata files initialised, all pigs fed, and ready to fly!!!
974 /* Now we clean up 'options'. Anything that is wanted has
975 * been moved into 'fs', so we just discard anything we find
979 for (i = 0; i < op.devcnt; i++) {
980 kfree(op.devlist[i].devblock);
981 kfree(op.devlist[i].stateblock);
982 if (op.devlist[i].bdev)
983 blkdev_put(op.devlist[i].bdev,
984 FMODE_READ|FMODE_WRITE|FMODE_EXCL);
991 static struct dentry *lafs_get_subset_root(struct inode *ino)
993 /* ino must be a TypeInodeFile inode in the prime filesystem. */
994 struct fs *fs = fs_from_inode(ino);
995 struct super_block *sb;
997 struct inode *rootdir, *imapfile;
998 struct dentry *root = NULL;
1002 rootdir = lafs_iget(ino, 2, SYNC);
1003 if (IS_ERR(rootdir) && PTR_ERR(rootdir) == -ENOENT) {
1004 rootdir = lafs_new_inode(fs, ino, NULL,
1005 TypeDir, 2, 0755, NULL);
1006 /* FIXME could the inode get written before we set
1007 * the link count ??*/
1008 rootdir->i_nlink = 2;
1010 if (IS_ERR(rootdir))
1011 err = PTR_ERR(rootdir);
1013 root = d_alloc_root(rootdir);
1014 imapfile = lafs_iget(ino, 1, SYNC);
1015 if (IS_ERR(imapfile) && PTR_ERR(imapfile) == -ENOENT)
1016 imapfile = lafs_new_inode(fs, ino, NULL,
1017 TypeInodeMap, 1, 0, NULL);
1019 if (IS_ERR(imapfile))
1020 err = PTR_ERR(imapfile);
1026 struct inode *atime = lafs_iget(ino, 3, SYNC);
1027 if (!IS_ERR(atime)) {
1028 if (LAFSI(atime)->type != TypeAccessTime) {
1032 LAFSI(ino)->md.fs.accesstime = atime;
1033 } else if (PTR_ERR(atime) != -ENOENT)
1038 return ERR_PTR(err);
1044 lafs_get_subset(struct file_system_type *fs_type,
1045 int flags, const char *dev_name, void *data,
1046 struct vfsmount *mnt)
1048 /* mount, possibly creating, a sub-fileset.
1049 * dev_name must be an absolute path that leads
1050 * to an object in a lafs file-system (or snapshot).
1051 * The object must be either an InodeFile or
1052 * an empty directory in the main file-system
1053 * with mode 0 (though that rule might change).
1054 * In the latter case we change the object to an
1056 * FIXME must require readonly for snapshots, and readwrite
1060 struct nameidata nd;
1062 struct super_block *sb;
1065 struct dentry *root;
1067 err = path_lookup(dev_name, LOOKUP_FOLLOW, &nd);
1070 sb = nd.path.dentry->d_sb;
1072 if (sb->s_type != &lafs_fs_type &&
1073 sb->s_type != &lafs_snap_fs_type)
1075 /* FIXME test not a subset filesystem */
1076 ino = nd.path.dentry->d_inode;
1077 if (LAFSI(ino)->type != TypeInodeFile &&
1078 LAFSI(ino)->type != TypeDir)
1080 fs = fs_from_sb(sb);
1081 down_write(&sb->s_umount);
1082 mutex_lock(&ino->i_mutex);
1084 if (LAFSI(ino)->type == TypeDir) {
1085 struct datablock *inodb;
1086 /* maybe convert this to TypeInodeFile */
1088 if (sb->s_type != &lafs_fs_type)
1091 /* FIXME maybe I should run orphans */
1093 if ((ino->i_mode & 07777) != 0)
1095 inodb = lafs_inode_dblock(ino, SYNC, MKREF(make_subset));
1096 err = PTR_ERR(inodb);
1099 lafs_iolock_block(&inodb->b);
1100 set_bit(B_PinPending, &inodb->b.flags);
1101 lafs_iounlock_block(&inodb->b);
1102 lafs_checkpoint_lock(fs);
1103 err = lafs_pin_dblock(inodb, ReleaseSpace);
1106 u32 parent = LAFSI(ino)->md.file.parent;
1107 /* OK, we are good to go making this filesystem */
1108 LAFSI(ino)->type = TypeInodeFile;
1109 LAFSI(ino)->metadata_size = (sizeof(struct la_inode) +
1110 sizeof(struct fs_metadata));
1111 ino->i_op = &lafs_subset_ino_operations;
1112 ino->i_fop = &lafs_subset_file_operations;
1113 md = &LAFSI(ino)->md.fs;
1115 ino->i_mtime = current_fs_time(sb);
1116 md->cblocks_used = 0;
1117 md->pblocks_used = 0;
1118 md->ablocks_used = 0;
1119 md->blocks_allowed = 10000; /* FIXME */
1120 md->blocks_unalloc = 0;
1121 /* FIXME should I be using inode_init here */
1122 md->creation_age = fs->wc[0].cluster_seq;
1123 md->inodes_used = 0;
1124 md->parent = parent;
1125 md->quota_inums[0] = 0;
1126 md->quota_inums[1] = 0;
1127 md->quota_inums[2] = 0;
1128 md->quota_inodes[0] = NULL;
1129 md->quota_inodes[1] = NULL;
1130 md->quota_inodes[2] = NULL;
1131 md->accesstime = NULL;
1133 lafs_dirty_dblock(inodb);
1134 lafs_dirty_inode(ino);
1135 /* We use a checkpoint to commit this change,
1136 * it is too unusual to bother logging
1138 lafs_checkpoint_start(fs);
1139 lafs_checkpoint_unlock_wait(fs);
1141 lafs_checkpoint_unlock(fs);
1143 putdref(inodb, MKREF(make_subset));
1147 /* We have a TypeInodeFile so we can make a superblock */
1148 root = lafs_get_subset_root(ino);
1152 err = PTR_ERR(root);
1154 mnt->mnt_sb = root->d_sb;
1155 atomic_inc(&mnt->mnt_sb->s_active);
1156 mnt->mnt_root = root;
1159 mutex_unlock(&ino->i_mutex);
1161 up_write(&ino->i_sb->s_umount);
1168 static struct dentry *subset_lookup(struct inode *dir, struct dentry *dentry,
1169 struct nameidata *nd)
1171 d_add(dentry, NULL);
1175 static int subset_readdir(struct file *filp, void *dirent, filldir_t filldir)
1177 struct dentry *dentry = filp->f_dentry;
1178 struct lafs_inode *lai = LAFSI(dentry->d_inode);
1180 loff_t i = filp->f_pos;
1184 ino = dentry->d_inode->i_ino;
1185 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1191 ino = lai->md.fs.parent;
1192 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1203 const struct file_operations lafs_subset_file_operations = {
1204 .readdir = subset_readdir,
1207 const struct inode_operations lafs_subset_ino_operations = {
1208 .lookup = subset_lookup,
1212 struct file_system_type lafs_fs_type = {
1213 .owner = THIS_MODULE,
1215 .get_sb = lafs_get_sb,
1216 .kill_sb = lafs_kill_sb,
1217 .fs_flags = FS_REQUIRES_DEV,
1220 static struct file_system_type lafs_subset_fs_type = {
1221 .owner = THIS_MODULE,
1222 .name = "lafs_subset",
1223 .get_sb = lafs_get_subset,
1226 static int __init lafs_init(void)
1230 BUILD_BUG_ON(B_NUM_FLAGS > 32);
1232 err = lafs_ihash_init();
1233 err = err ?: register_filesystem(&lafs_fs_type);
1234 err = err ?: register_filesystem(&lafs_snap_fs_type);
1235 err = err ?: register_filesystem(&lafs_subset_fs_type);
1241 unregister_filesystem(&lafs_fs_type);
1242 unregister_filesystem(&lafs_snap_fs_type);
1243 unregister_filesystem(&lafs_subset_fs_type);
1248 static void __exit lafs_exit(void)
1250 unregister_filesystem(&lafs_fs_type);
1251 unregister_filesystem(&lafs_snap_fs_type);
1252 unregister_filesystem(&lafs_subset_fs_type);
1256 static struct inode *lafs_nfs_get_inode(struct super_block *sb,
1257 u64 ino, u32 generation)
1259 struct fs *fs = fs_from_sb(sb);
1260 struct inode *inode;
1262 inode = lafs_iget(fs->ss[0].root, ino, SYNC);
1264 return ERR_CAST(inode);
1265 if (generation && inode->i_generation != generation) {
1267 return ERR_PTR(-ESTALE);
1273 static struct dentry *lafs_fh_to_dentry(struct super_block *sb, struct fid *fid,
1274 int fh_len, int fh_type)
1276 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1277 lafs_nfs_get_inode);
1280 static struct dentry *lafs_fh_to_parent(struct super_block *sb, struct fid *fid,
1281 int fh_len, int fh_type)
1283 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1284 lafs_nfs_get_inode);
1287 static struct dentry *lafs_get_parent(struct dentry *child)
1290 struct inode *inode;
1291 switch(LAFSI(child->d_inode)->type) {
1293 inum = LAFSI(child->d_inode)->md.file.parent;
1296 inum = LAFSI(child->d_inode)->md.fs.parent;
1299 inode = lafs_iget(LAFSI(child->d_inode)->filesys,
1302 return ERR_CAST(inode);
1303 return d_obtain_alias(inode);
1306 static const struct export_operations lafs_export_ops = {
1307 .fh_to_dentry = lafs_fh_to_dentry,
1308 .fh_to_parent = lafs_fh_to_parent,
1309 .get_parent = lafs_get_parent,
1312 static struct inode *lafs_alloc_inode(struct super_block *sb)
1314 struct lafs_inode *li;
1315 li = kmalloc(sizeof(*li), GFP_NOFS);
1318 inode_init_once(&li->vfs_inode);
1319 li->vfs_inode.i_data.backing_dev_info = sb->s_bdi;
1322 li->update_cluster = 0;
1323 li->md.fs.name = NULL;
1325 init_rwsem(&li->ind_sem);
1326 INIT_LIST_HEAD(&li->free_index);
1328 return &li->vfs_inode;
1331 static void kfree_inode(struct rcu_head *head)
1333 struct lafs_inode *lai = container_of(head, struct lafs_inode,
1335 if (lai->type == TypeInodeFile)
1336 kfree(lai->md.fs.name);
1340 void lafs_destroy_inode(struct inode *inode)
1342 struct datablock *db;
1343 struct inode *fsys = LAFSI(inode)->filesys;
1345 BUG_ON(!list_empty(&inode->i_sb_list));
1346 // Cannot test i_list as dispose_list just does list_del
1347 db = lafs_inode_get_dblock(inode, MKREF(destroy));
1350 set_bit(I_Destroyed, &LAFSI(inode)->iflags);
1351 if (test_and_clear_bit(B_Async, &db->b.flags))
1352 lafs_wake_thread(fs_from_inode(inode));
1353 putdref(db, MKREF(destroy));
1357 LAFSI(inode)->filesys = NULL;
1359 spin_lock(&inode->i_data.private_lock);
1360 if (LAFSI(inode)->iblock)
1361 LAFS_BUG(atomic_read(&LAFSI(inode)->iblock->b.refcnt),
1362 &LAFSI(inode)->iblock->b);
1363 /* FIXME could there be Async blocks keeps a refcount?
1364 * we should free them
1366 spin_unlock(&inode->i_data.private_lock);
1367 lafs_release_index(&LAFSI(inode)->free_index);
1368 call_rcu(&LAFSI(inode)->md.rcu,
1373 static int lafs_sync_fs(struct super_block *sb, int wait)
1375 if (fs_from_sb(sb)->thread == NULL)
1376 /* Filesystem in not active - nothing to sync */
1379 /* We only reach here if s_dirt was set, so it
1380 * is reasonable to force a checkpoint.
1382 lafs_checkpoint_start(fs_from_sb(sb));
1384 lafs_checkpoint_wait(fs_from_sb(sb));
1388 static int lafs_statfs(struct dentry *de, struct kstatfs *buf)
1393 struct fs *fs = fs_from_inode(de->d_inode);
1394 struct lafs_inode *fsroot = LAFSI(LAFSI(de->d_inode)->filesys);
1395 struct lafs_inode *laroot = LAFSI(fs->ss[0].root);
1398 fsuuid = (u32 *)fs->state->uuid;
1399 for (i = 0; i < 16 / 4 ; i++) {
1400 fsid ^= le32_to_cpu(fsuuid[i]);
1401 buf->f_fsid.val[i/2] = fsid;
1403 buf->f_fsid.val[1] ^= fsroot->vfs_inode.i_ino;
1404 buf->f_type = 0x4C614654; /* "LaFS" */
1405 buf->f_bsize = fs->blocksize;
1406 buf->f_blocks = fsroot->md.fs.blocks_allowed;
1407 if (buf->f_blocks == 0) {
1408 /* should subtract usage of all other filesystems...*/
1409 for (i = 0; i < fs->devs_loaded; i++)
1410 buf->f_blocks += fs->devs[i].size;
1415 buf->f_namelen = 255;
1418 spin_lock(&laroot->vfs_inode.i_lock);
1419 /* "bavail" is "blocks we could succeed in adding to the filesystem".
1420 * "bfree" is effectively total blocks - used blocks
1422 buf->f_bavail = fs->free_blocks + fs->clean_reserved - fs->allocated_blocks;
1423 spin_unlock(&laroot->vfs_inode.i_lock);
1424 spin_lock(&fsroot->vfs_inode.i_lock);
1425 buf->f_bfree = buf->f_blocks - (fsroot->md.fs.cblocks_used +
1426 fsroot->md.fs.pblocks_used +
1427 fsroot->md.fs.ablocks_used);
1428 if (buf->f_bfree < buf->f_bavail)
1429 buf->f_bavail = buf->f_bfree;
1430 dprintk("df: tot=%ld free=%ld avail=%ld(%ld-%ld-%ld) cb=%ld pb=%ld ab=%ld\n",
1431 (long)buf->f_blocks, (long)buf->f_bfree, (long)buf->f_bavail,
1432 (long)fs->free_blocks, (long)fs->clean_reserved,
1433 (long)fs->allocated_blocks,
1434 (long)fsroot->md.fs.cblocks_used, (long)fsroot->md.fs.pblocks_used,
1435 (long)fsroot->md.fs.ablocks_used);
1436 spin_unlock(&fsroot->vfs_inode.i_lock);
1440 static struct super_operations lafs_sops = {
1441 .alloc_inode = lafs_alloc_inode,
1442 .destroy_inode = lafs_destroy_inode, /* Inverse of 'alloc_inode' */
1443 /* Don't use read_inode */
1444 .dirty_inode = lafs_dirty_inode,
1445 /* .write_inode not needed */
1448 .evict_inode = lafs_evict_inode,
1449 .put_super = lafs_put_super,
1450 .sync_fs = lafs_sync_fs,
1451 /* write_super_lockfs ?? */
1453 .statfs = lafs_statfs,
1457 MODULE_AUTHOR("Neil Brown");
1458 MODULE_DESCRIPTION("LaFS - Log Structured File System");
1459 MODULE_LICENSE("GPL");
1460 module_init(lafs_init);
1461 module_exit(lafs_exit);
1463 module_param(lafs_trace, int, 0644);
1467 static int do_dump(const char *val, struct kernel_param *kp)
1469 extern void lafs_dump_orphans(void);
1470 extern void lafs_dump_tree(void);
1471 extern void lafs_dump_cleanable(void);
1472 extern void lafs_dump_usage(void);
1474 printk("Want dump of %s\n", val);
1475 if (strncmp(val, "orphan", 6) == 0)
1476 lafs_dump_orphans();
1477 if (strncmp(val, "tree", 4) == 0)
1479 if (strncmp(val, "cleanable", 9) == 0)
1480 lafs_dump_cleanable();
1481 if (strncmp(val, "usage", 5) == 0)
1486 static int get_dump(char *buffer, struct kernel_param *kp)
1488 strcpy(buffer, "orphans,tree,cleanable,usage");
1489 return strlen(buffer);
1493 module_param_call(dump, do_dump, get_dump, &arg, 0775);