From 3483133cdf25f86f80530b573f0435e09300ed7d Mon Sep 17 00:00:00 2001 From: Zach Brown Date: Fri, 12 Feb 2016 19:37:48 -0800 Subject: [PATCH] Read super brick instead of mkfs Now that we have a working userspace mkfs we can read the supers on mount instead of always initializing a new file system. We still don't know how to read items from blocks so mount fails when it can't find the root dir inode. Signed-off-by: Zach Brown --- kmod/src/Makefile | 2 +- kmod/src/mkfs.c | 52 ------------------------------- kmod/src/mkfs.h | 6 ---- kmod/src/super.c | 79 +++++++++++++++++++++++++++++++++++++++++++++-- kmod/src/super.h | 3 ++ 5 files changed, 81 insertions(+), 61 deletions(-) delete mode 100644 kmod/src/mkfs.c delete mode 100644 kmod/src/mkfs.h diff --git a/kmod/src/Makefile b/kmod/src/Makefile index 239e8aef..d067f9d4 100644 --- a/kmod/src/Makefile +++ b/kmod/src/Makefile @@ -1,3 +1,3 @@ obj-$(CONFIG_SCOUTFS_FS) := scoutfs.o -scoutfs-y += dir.o inode.o item.o lsm.o mkfs.o super.o +scoutfs-y += dir.o inode.o item.o lsm.o msg.o super.o diff --git a/kmod/src/mkfs.c b/kmod/src/mkfs.c deleted file mode 100644 index 2a1df169..00000000 --- a/kmod/src/mkfs.c +++ /dev/null @@ -1,52 +0,0 @@ -#include -#include -#include -#include - -#include "super.h" -#include "item.h" -#include "key.h" -#include "mkfs.h" - -/* - * For now a file system system only exists in the item cache for the - * duration of the mount. This "mkfs" hack creates a root dir inode in - * the item cache on mount so that we can run tests in memory and not - * worry about user space or persistent storage. - */ -int scoutfs_mkfs(struct super_block *sb) -{ - const struct timespec ts = current_kernel_time(); - struct scoutfs_sb_info *sbi = SCOUTFS_SB(sb); - struct scoutfs_inode *cinode; - struct scoutfs_item *item; - struct scoutfs_key key; - int i; - - atomic64_set(&sbi->next_ino, SCOUTFS_ROOT_INO + 1); - atomic64_set(&sbi->next_blkno, 2); - - for (i = 0; i < ARRAY_SIZE(sbi->bloom_hash_keys); i++) { - get_random_bytes(&sbi->bloom_hash_keys[i], - sizeof(sbi->bloom_hash_keys[i])); - } - - scoutfs_set_key(&key, SCOUTFS_ROOT_INO, SCOUTFS_INODE_KEY, 0); - - item = scoutfs_item_create(sb, &key, sizeof(struct scoutfs_inode)); - if (IS_ERR(item)) - return PTR_ERR(item); - - cinode = item->val; - memset(cinode, 0, sizeof(struct scoutfs_inode)); - cinode->nlink = cpu_to_le32(2); - cinode->mode = cpu_to_le32(S_IFDIR | 0755); - cinode->atime.sec = cpu_to_le64(ts.tv_sec); - cinode->atime.nsec = cpu_to_le32(ts.tv_nsec); - cinode->ctime = cinode->atime; - cinode->mtime = cinode->atime; - get_random_bytes(&cinode->salt, sizeof(cinode->salt)); - - scoutfs_item_put(item); - return 0; -} diff --git a/kmod/src/mkfs.h b/kmod/src/mkfs.h deleted file mode 100644 index 51679417..00000000 --- a/kmod/src/mkfs.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef _SCOUTFS_MKFS_H_ -#define _SCOUTFS_MKFS_H_ - -int scoutfs_mkfs(struct super_block *sb); - -#endif diff --git a/kmod/src/super.c b/kmod/src/super.c index 27336d03..e21c3238 100644 --- a/kmod/src/super.c +++ b/kmod/src/super.c @@ -15,13 +15,16 @@ #include #include #include +#include +#include +#include #include "super.h" #include "format.h" -#include "mkfs.h" #include "inode.h" #include "dir.h" #include "lsm.h" +#include "msg.h" static const struct super_operations scoutfs_super_ops = { .alloc_inode = scoutfs_alloc_inode, @@ -29,6 +32,73 @@ static const struct super_operations scoutfs_super_ops = { .sync_fs = scoutfs_sync_fs, }; +static int read_supers(struct super_block *sb) +{ + struct scoutfs_sb_info *sbi = SCOUTFS_SB(sb); + struct buffer_head *bh = NULL; + struct scoutfs_super *super; + int found = -1; + u32 crc; + int i; + + for (i = 0; i < 2; i++) { + if (bh) + brelse(bh); + bh = sb_bread(sb, SCOUTFS_SUPER_BRICK + i); + if (!bh) { + scoutfs_warn(sb, "couldn't read super brick %u", i); + continue; + } + + super = (void *)bh->b_data; + + if (super->id != cpu_to_le64(SCOUTFS_SUPER_ID)) { + scoutfs_warn(sb, "super brick %u has invalid id %llx", + i, le64_to_cpu(super->id)); + continue; + } + + crc = crc32c(~0, (char *)&super->hdr.crc + sizeof(crc), + SCOUTFS_BRICK_SIZE - sizeof(crc)); + if (crc != le32_to_cpu(super->hdr.crc)) { + scoutfs_warn(sb, "super brick %u has bad crc %x (expected %x)", + i, crc, le32_to_cpu(super->hdr.crc)); + continue; + } + + if (found < 0 || (le64_to_cpu(super->hdr.seq) > + le64_to_cpu(sbi->super.hdr.seq))) { + memcpy(&sbi->super, super, + sizeof(struct scoutfs_super)); + found = i; + } + } + + if (bh) + brelse(bh); + + if (found < 0) { + scoutfs_err(sb, "unable to read valid super brick"); + return -EINVAL; + } + + scoutfs_info(sb, "using super %u with seq %llu", + found, le64_to_cpu(sbi->super.hdr.seq)); + + /* + * XXX These don't exist in the super yet. They should soon. + */ + atomic64_set(&sbi->next_ino, SCOUTFS_ROOT_INO + 1); + atomic64_set(&sbi->next_blkno, 2); + + for (i = 0; i < ARRAY_SIZE(sbi->bloom_hash_keys); i++) { + get_random_bytes(&sbi->bloom_hash_keys[i], + sizeof(sbi->bloom_hash_keys[i])); + } + + return 0; +} + static int scoutfs_fill_super(struct super_block *sb, void *data, int silent) { struct scoutfs_sb_info *sbi; @@ -48,7 +118,12 @@ static int scoutfs_fill_super(struct super_block *sb, void *data, int silent) sbi->item_root = RB_ROOT; sbi->dirty_item_root = RB_ROOT; - ret = scoutfs_mkfs(sb); + if (!sb_set_blocksize(sb, SCOUTFS_BRICK_SIZE)) { + printk(KERN_ERR "couldn't set blocksize\n"); + return -EINVAL; + } + + ret = read_supers(sb); if (ret) return ret; diff --git a/kmod/src/super.h b/kmod/src/super.h index 1b6f0be0..fd1fe36d 100644 --- a/kmod/src/super.h +++ b/kmod/src/super.h @@ -2,8 +2,11 @@ #define _SCOUTFS_SUPER_H_ #include +#include "format.h" struct scoutfs_sb_info { + struct scoutfs_super super; + atomic64_t next_ino; atomic64_t next_blkno;