Skip to content

Commit

Permalink
Btrfs: Add support for device scanning and detection ioctls
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Mason <chris.mason@oracle.com>
  • Loading branch information
Chris Mason committed Sep 25, 2008
1 parent 239b14b commit 8a4b83c
Show file tree
Hide file tree
Showing 7 changed files with 333 additions and 44 deletions.
21 changes: 19 additions & 2 deletions fs/btrfs/ctree.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ extern struct kmem_cache *btrfs_transaction_cachep;
extern struct kmem_cache *btrfs_bit_radix_cachep;
extern struct kmem_cache *btrfs_path_cachep;

#define BTRFS_MAGIC "_B4RfS_M"
#define BTRFS_MAGIC "_B5RfS_M"

#define BTRFS_MAX_LEVEL 8

Expand Down Expand Up @@ -238,6 +238,7 @@ struct btrfs_super_block {
__le64 total_bytes;
__le64 bytes_used;
__le64 root_dir_objectid;
__le64 num_devices;
__le32 sectorsize;
__le32 nodesize;
__le32 leafsize;
Expand Down Expand Up @@ -440,6 +441,7 @@ struct btrfs_block_group_cache {
};

struct btrfs_device;
struct btrfs_fs_devices;
struct btrfs_fs_info {
u8 fsid[BTRFS_FSID_SIZE];
struct btrfs_root *extent_root;
Expand Down Expand Up @@ -489,7 +491,7 @@ struct btrfs_fs_info {
u64 total_pinned;
struct list_head dirty_cowonly_roots;

struct list_head devices;
struct btrfs_fs_devices *fs_devices;
struct list_head space_info;
spinlock_t delalloc_lock;
spinlock_t new_trans_lock;
Expand Down Expand Up @@ -677,6 +679,19 @@ BTRFS_SETGET_FUNCS(device_io_width, struct btrfs_dev_item, io_width, 32);
BTRFS_SETGET_FUNCS(device_sector_size, struct btrfs_dev_item, sector_size, 32);
BTRFS_SETGET_FUNCS(device_id, struct btrfs_dev_item, devid, 64);

BTRFS_SETGET_STACK_FUNCS(stack_device_type, struct btrfs_dev_item, type, 64);
BTRFS_SETGET_STACK_FUNCS(stack_device_total_bytes, struct btrfs_dev_item,
total_bytes, 64);
BTRFS_SETGET_STACK_FUNCS(stack_device_bytes_used, struct btrfs_dev_item,
bytes_used, 64);
BTRFS_SETGET_STACK_FUNCS(stack_device_io_align, struct btrfs_dev_item,
io_align, 32);
BTRFS_SETGET_STACK_FUNCS(stack_device_io_width, struct btrfs_dev_item,
io_width, 32);
BTRFS_SETGET_STACK_FUNCS(stack_device_sector_size, struct btrfs_dev_item,
sector_size, 32);
BTRFS_SETGET_STACK_FUNCS(stack_device_id, struct btrfs_dev_item, devid, 64);

static inline char *btrfs_device_uuid(struct btrfs_dev_item *d)
{
return (char *)d + offsetof(struct btrfs_dev_item, uuid);
Expand Down Expand Up @@ -1106,6 +1121,8 @@ BTRFS_SETGET_STACK_FUNCS(super_stripesize, struct btrfs_super_block,
stripesize, 32);
BTRFS_SETGET_STACK_FUNCS(super_root_dir, struct btrfs_super_block,
root_dir_objectid, 64);
BTRFS_SETGET_STACK_FUNCS(super_num_devices, struct btrfs_super_block,
num_devices, 64);

static inline unsigned long btrfs_leaf_data(struct extent_buffer *l)
{
Expand Down
24 changes: 15 additions & 9 deletions fs/btrfs/disk-io.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,12 +365,12 @@ static int close_all_devices(struct btrfs_fs_info *fs_info)
struct list_head *next;
struct btrfs_device *device;

list = &fs_info->devices;
while(!list_empty(list)) {
next = list->next;
list_del(next);
list = &fs_info->fs_devices->devices;
list_for_each(next, list) {
device = list_entry(next, struct btrfs_device, dev_list);
kfree(device);
if (device->bdev && device->bdev != fs_info->sb->s_bdev)
close_bdev_excl(device->bdev);
device->bdev = NULL;
}
return 0;
}
Expand Down Expand Up @@ -655,7 +655,8 @@ static int add_hasher(struct btrfs_fs_info *info, char *type) {
return 0;
}
#endif
struct btrfs_root *open_ctree(struct super_block *sb)
struct btrfs_root *open_ctree(struct super_block *sb,
struct btrfs_fs_devices *fs_devices)
{
u32 sectorsize;
u32 nodesize;
Expand Down Expand Up @@ -697,8 +698,8 @@ struct btrfs_root *open_ctree(struct super_block *sb)
fs_info->extent_root = extent_root;
fs_info->chunk_root = chunk_root;
fs_info->dev_root = dev_root;
fs_info->fs_devices = fs_devices;
INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
INIT_LIST_HEAD(&fs_info->devices);
INIT_LIST_HEAD(&fs_info->space_info);
btrfs_mapping_init(&fs_info->mapping_tree);
fs_info->sb = sb;
Expand Down Expand Up @@ -779,6 +780,12 @@ struct btrfs_root *open_ctree(struct super_block *sb)
if (!btrfs_super_root(disk_super))
goto fail_sb_buffer;

if (btrfs_super_num_devices(disk_super) != fs_devices->num_devices) {
printk("Btrfs: wanted %llu devices, but found %llu\n",
(unsigned long long)btrfs_super_num_devices(disk_super),
(unsigned long long)fs_devices->num_devices);
goto fail_sb_buffer;
}
nodesize = btrfs_super_nodesize(disk_super);
leafsize = btrfs_super_leafsize(disk_super);
sectorsize = btrfs_super_sectorsize(disk_super);
Expand All @@ -799,8 +806,6 @@ struct btrfs_root *open_ctree(struct super_block *sb)
}

mutex_lock(&fs_info->fs_mutex);
ret = btrfs_read_super_device(tree_root, fs_info->sb_buffer);
BUG_ON(ret);

ret = btrfs_read_sys_array(tree_root);
BUG_ON(ret);
Expand Down Expand Up @@ -859,6 +864,7 @@ struct btrfs_root *open_ctree(struct super_block *sb)
fail_iput:
iput(fs_info->btree_inode);
fail:
close_all_devices(fs_info);
kfree(extent_root);
kfree(tree_root);
kfree(fs_info);
Expand Down
4 changes: 3 additions & 1 deletion fs/btrfs/disk-io.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#define BTRFS_SUPER_INFO_OFFSET (16 * 1024)
struct btrfs_device;
struct btrfs_fs_devices;

struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
u32 blocksize);
Expand All @@ -29,7 +30,8 @@ struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
u64 bytenr, u32 blocksize);
int clean_tree_block(struct btrfs_trans_handle *trans,
struct btrfs_root *root, struct extent_buffer *buf);
struct btrfs_root *open_ctree(struct super_block *sb);
struct btrfs_root *open_ctree(struct super_block *sb,
struct btrfs_fs_devices *fs_devices);
int close_ctree(struct btrfs_root *root);
int write_ctree_super(struct btrfs_trans_handle *trans,
struct btrfs_root *root);
Expand Down
6 changes: 5 additions & 1 deletion fs/btrfs/ioctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@

#define BTRFS_IOCTL_MAGIC 0x94
#define BTRFS_VOL_NAME_MAX 255
#define BTRFS_PATH_NAME_MAX 4095

struct btrfs_ioctl_vol_args {
char name[BTRFS_VOL_NAME_MAX + 1];
char name[BTRFS_PATH_NAME_MAX + 1];
};

#define BTRFS_IOC_SNAP_CREATE _IOW(BTRFS_IOCTL_MAGIC, 1, \
Expand All @@ -32,4 +34,6 @@ struct btrfs_ioctl_vol_args {
struct btrfs_ioctl_vol_args)
#define BTRFS_IOC_RESIZE _IOW(BTRFS_IOCTL_MAGIC, 3, \
struct btrfs_ioctl_vol_args)
#define BTRFS_IOC_SCAN_DEV _IOW(BTRFS_IOCTL_MAGIC, 4, \
struct btrfs_ioctl_vol_args)
#endif
61 changes: 44 additions & 17 deletions fs/btrfs/super.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "ioctl.h"
#include "print-tree.h"
#include "xattr.h"
#include "volumes.h"

#define BTRFS_SUPER_MAGIC 0x9123683E

Expand Down Expand Up @@ -216,7 +217,9 @@ static int parse_options (char * options,
return 1;
}

static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
static int btrfs_fill_super(struct super_block * sb,
struct btrfs_fs_devices *fs_devices,
void * data, int silent)
{
struct inode * inode;
struct dentry * root_dentry;
Expand All @@ -231,7 +234,7 @@ static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
sb->s_xattr = btrfs_xattr_handlers;
sb->s_time_gran = 1;

tree_root = open_ctree(sb);
tree_root = open_ctree(sb, fs_devices);

if (!tree_root || IS_ERR(tree_root)) {
printk("btrfs: open_ctree failed\n");
Expand Down Expand Up @@ -334,18 +337,23 @@ static int test_bdev_super(struct super_block *s, void *data)

int btrfs_get_sb_bdev(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data,
int (*fill_super)(struct super_block *, void *, int),
struct vfsmount *mnt, const char *subvol)
{
struct block_device *bdev = NULL;
struct super_block *s;
struct dentry *root;
struct btrfs_fs_devices *fs_devices = NULL;
int error = 0;

bdev = open_bdev_excl(dev_name, flags, fs_type);
if (IS_ERR(bdev))
return PTR_ERR(bdev);
error = btrfs_scan_one_device(dev_name, flags, fs_type, &fs_devices);
if (error)
return error;

error = btrfs_open_devices(fs_devices, flags, fs_type);
if (error)
return error;

bdev = fs_devices->lowest_bdev;
/*
* once the super is inserted into the list by sget, s_umount
* will protect the lockfs code from trying to start a snapshot
Expand All @@ -372,7 +380,8 @@ int btrfs_get_sb_bdev(struct file_system_type *fs_type,
s->s_flags = flags;
strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
sb_set_blocksize(s, block_size(bdev));
error = fill_super(s, data, flags & MS_SILENT ? 1 : 0);
error = btrfs_fill_super(s, fs_devices, data,
flags & MS_SILENT ? 1 : 0);
if (error) {
up_write(&s->s_umount);
deactivate_super(s);
Expand Down Expand Up @@ -408,7 +417,7 @@ int btrfs_get_sb_bdev(struct file_system_type *fs_type,
error_s:
error = PTR_ERR(s);
error_bdev:
close_bdev_excl(bdev);
btrfs_close_devices(fs_devices);
error:
return error;
}
Expand All @@ -421,8 +430,7 @@ static int btrfs_get_sb(struct file_system_type *fs_type,
char *subvol_name = NULL;

parse_options((char *)data, NULL, &subvol_name);
ret = btrfs_get_sb_bdev(fs_type, flags, dev_name, data,
btrfs_fill_super, mnt,
ret = btrfs_get_sb_bdev(fs_type, flags, dev_name, data, mnt,
subvol_name ? subvol_name : "default");
if (subvol_name)
kfree(subvol_name);
Expand All @@ -445,13 +453,6 @@ static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
return 0;
}

static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
unsigned long arg)
{
printk("btrfs control ioctl %d\n", cmd);
return 0;
}

static struct file_system_type btrfs_fs_type = {
.owner = THIS_MODULE,
.name = "btrfs",
Expand All @@ -460,6 +461,31 @@ static struct file_system_type btrfs_fs_type = {
.fs_flags = FS_REQUIRES_DEV,
};

static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
unsigned long arg)
{
struct btrfs_ioctl_vol_args *vol;
struct btrfs_fs_devices *fs_devices;
int ret;
int len;

vol = kmalloc(sizeof(*vol), GFP_KERNEL);
if (copy_from_user(vol, (void __user *)arg, sizeof(*vol))) {
ret = -EFAULT;
goto out;
}
len = strnlen(vol->name, BTRFS_PATH_NAME_MAX);
switch (cmd) {
case BTRFS_IOC_SCAN_DEV:
ret = btrfs_scan_one_device(vol->name, MS_RDONLY,
&btrfs_fs_type, &fs_devices);
break;
}
out:
kfree(vol);
return 0;
}

static void btrfs_write_super_lockfs(struct super_block *sb)
{
struct btrfs_root *root = btrfs_sb(sb);
Expand Down Expand Up @@ -567,6 +593,7 @@ static void __exit exit_btrfs_fs(void)
btrfs_interface_exit();
unregister_filesystem(&btrfs_fs_type);
btrfs_exit_sysfs();
btrfs_cleanup_fs_uuids();
}

module_init(init_btrfs_fs)
Expand Down
Loading

0 comments on commit 8a4b83c

Please sign in to comment.