Commit edbd8d4e authored by Chris Mason's avatar Chris Mason

Btrfs: Support for online FS resize (grow and shrink)

Signed-off-by: default avatarChris Mason <chris.mason@oracle.com>
parent 5d4fb734
...@@ -508,6 +508,8 @@ BTRFS_SETGET_STACK_FUNCS(block_group_used, struct btrfs_block_group_item, ...@@ -508,6 +508,8 @@ BTRFS_SETGET_STACK_FUNCS(block_group_used, struct btrfs_block_group_item,
used, 64); used, 64);
BTRFS_SETGET_FUNCS(disk_block_group_used, struct btrfs_block_group_item, BTRFS_SETGET_FUNCS(disk_block_group_used, struct btrfs_block_group_item,
used, 64); used, 64);
BTRFS_SETGET_FUNCS(disk_block_group_flags, struct btrfs_block_group_item,
flags, 8);
/* struct btrfs_inode_ref */ /* struct btrfs_inode_ref */
BTRFS_SETGET_FUNCS(inode_ref_name_len, struct btrfs_inode_ref, name_len, 16); BTRFS_SETGET_FUNCS(inode_ref_name_len, struct btrfs_inode_ref, name_len, 16);
...@@ -960,6 +962,9 @@ struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans, ...@@ -960,6 +962,9 @@ struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
int level, int level,
u64 hint, u64 hint,
u64 empty_size); u64 empty_size);
int btrfs_grow_extent_tree(struct btrfs_trans_handle *trans,
struct btrfs_root *root, u64 new_size);
int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 new_size);
int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans, int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
struct btrfs_root *root, struct btrfs_root *root,
struct btrfs_path *path, u64 bytenr, struct btrfs_path *path, u64 bytenr,
...@@ -1117,6 +1122,9 @@ int btrfs_csum_truncate(struct btrfs_trans_handle *trans, ...@@ -1117,6 +1122,9 @@ int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
struct btrfs_root *root, struct btrfs_path *path, struct btrfs_root *root, struct btrfs_path *path,
u64 isize); u64 isize);
/* inode.c */ /* inode.c */
unsigned long btrfs_force_ra(struct address_space *mapping,
struct file_ra_state *ra, struct file *file,
pgoff_t offset, pgoff_t last_index);
int btrfs_check_free_space(struct btrfs_root *root, u64 num_required, int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
int for_del); int for_del);
int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page); int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page);
...@@ -1162,4 +1170,6 @@ void btrfs_sysfs_del_super(struct btrfs_fs_info *root); ...@@ -1162,4 +1170,6 @@ void btrfs_sysfs_del_super(struct btrfs_fs_info *root);
ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size); ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size);
int btrfs_delete_xattrs(struct btrfs_trans_handle *trans, int btrfs_delete_xattrs(struct btrfs_trans_handle *trans,
struct btrfs_root *root, struct inode *inode); struct btrfs_root *root, struct inode *inode);
/* super.c */
u64 btrfs_parse_size(char *str);
#endif #endif
...@@ -471,13 +471,17 @@ struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info, ...@@ -471,13 +471,17 @@ struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
return root; return root;
} }
struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
struct btrfs_key *location, struct btrfs_key *location)
const char *name, int namelen)
{ {
struct btrfs_root *root; struct btrfs_root *root;
int ret; int ret;
if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
return fs_info->tree_root;
if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
return fs_info->extent_root;
root = radix_tree_lookup(&fs_info->fs_roots_radix, root = radix_tree_lookup(&fs_info->fs_roots_radix,
(unsigned long)location->objectid); (unsigned long)location->objectid);
if (root) if (root)
...@@ -494,6 +498,23 @@ struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info, ...@@ -494,6 +498,23 @@ struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
kfree(root); kfree(root);
return ERR_PTR(ret); return ERR_PTR(ret);
} }
ret = btrfs_find_dead_roots(fs_info->tree_root,
root->root_key.objectid, root);
BUG_ON(ret);
return root;
}
struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
struct btrfs_key *location,
const char *name, int namelen)
{
struct btrfs_root *root;
int ret;
root = btrfs_read_fs_root_no_name(fs_info, location);
if (!root)
return NULL;
ret = btrfs_set_root_name(root, name, namelen); ret = btrfs_set_root_name(root, name, namelen);
if (ret) { if (ret) {
...@@ -509,11 +530,6 @@ struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info, ...@@ -509,11 +530,6 @@ struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
kfree(root); kfree(root);
return ERR_PTR(ret); return ERR_PTR(ret);
} }
ret = btrfs_find_dead_roots(fs_info->tree_root,
root->root_key.objectid, root);
BUG_ON(ret);
return root; return root;
} }
#if 0 #if 0
......
...@@ -39,6 +39,8 @@ struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info, ...@@ -39,6 +39,8 @@ struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
const char *name, int namelen); const char *name, int namelen);
struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info, struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
struct btrfs_key *location); struct btrfs_key *location);
struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
struct btrfs_key *location);
int btrfs_insert_dev_radix(struct btrfs_root *root, int btrfs_insert_dev_radix(struct btrfs_root *root,
struct block_device *bdev, struct block_device *bdev,
u64 device_id, u64 device_id,
......
This diff is collapsed.
...@@ -2346,7 +2346,7 @@ static int create_snapshot(struct btrfs_root *root, char *name, int namelen) ...@@ -2346,7 +2346,7 @@ static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
return ret; return ret;
} }
static unsigned long force_ra(struct address_space *mapping, unsigned long btrfs_force_ra(struct address_space *mapping,
struct file_ra_state *ra, struct file *file, struct file_ra_state *ra, struct file *file,
pgoff_t offset, pgoff_t last_index) pgoff_t offset, pgoff_t last_index)
{ {
...@@ -2372,6 +2372,8 @@ int btrfs_defrag_file(struct file *file) { ...@@ -2372,6 +2372,8 @@ int btrfs_defrag_file(struct file *file) {
unsigned long ra_index = 0; unsigned long ra_index = 0;
u64 page_start; u64 page_start;
u64 page_end; u64 page_end;
u64 delalloc_start;
u64 existing_delalloc;
unsigned long i; unsigned long i;
int ret; int ret;
...@@ -2385,8 +2387,9 @@ int btrfs_defrag_file(struct file *file) { ...@@ -2385,8 +2387,9 @@ int btrfs_defrag_file(struct file *file) {
last_index = inode->i_size >> PAGE_CACHE_SHIFT; last_index = inode->i_size >> PAGE_CACHE_SHIFT;
for (i = 0; i <= last_index; i++) { for (i = 0; i <= last_index; i++) {
if (i == ra_index) { if (i == ra_index) {
ra_index = force_ra(inode->i_mapping, &file->f_ra, ra_index = btrfs_force_ra(inode->i_mapping,
file, ra_index, last_index); &file->f_ra,
file, ra_index, last_index);
} }
page = grab_cache_page(inode->i_mapping, i); page = grab_cache_page(inode->i_mapping, i);
if (!page) if (!page)
...@@ -2404,8 +2407,19 @@ int btrfs_defrag_file(struct file *file) { ...@@ -2404,8 +2407,19 @@ int btrfs_defrag_file(struct file *file) {
page_end = page_start + PAGE_CACHE_SIZE - 1; page_end = page_start + PAGE_CACHE_SIZE - 1;
lock_extent(em_tree, page_start, page_end, GFP_NOFS); lock_extent(em_tree, page_start, page_end, GFP_NOFS);
delalloc_start = page_start;
existing_delalloc =
count_range_bits(&BTRFS_I(inode)->extent_tree,
&delalloc_start, page_end,
PAGE_CACHE_SIZE, EXTENT_DELALLOC);
set_extent_delalloc(em_tree, page_start, set_extent_delalloc(em_tree, page_start,
page_end, GFP_NOFS); page_end, GFP_NOFS);
spin_lock(&root->fs_info->delalloc_lock);
root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE -
existing_delalloc;
spin_unlock(&root->fs_info->delalloc_lock);
unlock_extent(em_tree, page_start, page_end, GFP_NOFS); unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
set_page_dirty(page); set_page_dirty(page);
unlock_page(page); unlock_page(page);
...@@ -2418,6 +2432,89 @@ int btrfs_defrag_file(struct file *file) { ...@@ -2418,6 +2432,89 @@ int btrfs_defrag_file(struct file *file) {
return 0; return 0;
} }
static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
{
u64 new_size;
u64 old_size;
struct btrfs_ioctl_vol_args *vol_args;
struct btrfs_trans_handle *trans;
char *sizestr;
int ret = 0;
int namelen;
int mod = 0;
vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
if (!vol_args)
return -ENOMEM;
if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
ret = -EFAULT;
goto out;
}
namelen = strlen(vol_args->name);
if (namelen > BTRFS_VOL_NAME_MAX) {
ret = -EINVAL;
goto out;
}
sizestr = vol_args->name;
if (!strcmp(sizestr, "max"))
new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
else {
if (sizestr[0] == '-') {
mod = -1;
sizestr++;
} else if (sizestr[0] == '+') {
mod = 1;
sizestr++;
}
new_size = btrfs_parse_size(sizestr);
if (new_size == 0) {
ret = -EINVAL;
goto out;
}
}
mutex_lock(&root->fs_info->fs_mutex);
old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
if (mod < 0) {
if (new_size > old_size) {
ret = -EINVAL;
goto out_unlock;
}
new_size = old_size - new_size;
} else if (mod > 0) {
new_size = old_size + new_size;
}
if (new_size < 256 * 1024 * 1024) {
ret = -EINVAL;
goto out_unlock;
}
if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
ret = -EFBIG;
goto out_unlock;
}
new_size = (new_size / root->sectorsize) * root->sectorsize;
printk("new size is %Lu\n", new_size);
if (new_size > old_size) {
trans = btrfs_start_transaction(root, 1);
ret = btrfs_grow_extent_tree(trans, root, new_size);
btrfs_commit_transaction(trans, root);
} else {
ret = btrfs_shrink_extent_tree(root, new_size);
}
out_unlock:
mutex_unlock(&root->fs_info->fs_mutex);
out:
kfree(vol_args);
return ret;
}
static int btrfs_ioctl_snap_create(struct btrfs_root *root, void __user *arg) static int btrfs_ioctl_snap_create(struct btrfs_root *root, void __user *arg)
{ {
struct btrfs_ioctl_vol_args *vol_args; struct btrfs_ioctl_vol_args *vol_args;
...@@ -2510,6 +2607,8 @@ long btrfs_ioctl(struct file *file, unsigned int ...@@ -2510,6 +2607,8 @@ long btrfs_ioctl(struct file *file, unsigned int
return btrfs_ioctl_snap_create(root, (void __user *)arg); return btrfs_ioctl_snap_create(root, (void __user *)arg);
case BTRFS_IOC_DEFRAG: case BTRFS_IOC_DEFRAG:
return btrfs_ioctl_defrag(file); return btrfs_ioctl_defrag(file);
case BTRFS_IOC_RESIZE:
return btrfs_ioctl_resize(root, (void __user *)arg);
} }
return -ENOTTY; return -ENOTTY;
......
...@@ -30,4 +30,6 @@ struct btrfs_ioctl_vol_args { ...@@ -30,4 +30,6 @@ struct btrfs_ioctl_vol_args {
struct btrfs_ioctl_vol_args) struct btrfs_ioctl_vol_args)
#define BTRFS_IOC_DEFRAG _IOW(BTRFS_IOCTL_MAGIC, 2, \ #define BTRFS_IOC_DEFRAG _IOW(BTRFS_IOCTL_MAGIC, 2, \
struct btrfs_ioctl_vol_args) struct btrfs_ioctl_vol_args)
#define BTRFS_IOC_RESIZE _IOW(BTRFS_IOCTL_MAGIC, 3, \
struct btrfs_ioctl_vol_args)
#endif #endif
...@@ -179,6 +179,11 @@ int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root, ...@@ -179,6 +179,11 @@ int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
ret = btrfs_search_slot(trans, root, key, path, -1, 1); ret = btrfs_search_slot(trans, root, key, path, -1, 1);
if (ret < 0) if (ret < 0)
goto out; goto out;
if (ret) {
btrfs_print_leaf(root, path->nodes[0]);
printk("failed to del %Lu %u %Lu\n", key->objectid, key->type, key->offset);
}
BUG_ON(ret != 0); BUG_ON(ret != 0);
leaf = path->nodes[0]; leaf = path->nodes[0];
ri = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_item); ri = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_item);
......
...@@ -74,9 +74,9 @@ static match_table_t tokens = { ...@@ -74,9 +74,9 @@ static match_table_t tokens = {
{Opt_err, NULL} {Opt_err, NULL}
}; };
static unsigned long parse_size(char *str) u64 btrfs_parse_size(char *str)
{ {
unsigned long res; u64 res;
int mult = 1; int mult = 1;
char *end; char *end;
char last; char last;
...@@ -150,7 +150,8 @@ static int parse_options (char * options, ...@@ -150,7 +150,8 @@ static int parse_options (char * options,
if (info) { if (info) {
char *num = match_strdup(&args[0]); char *num = match_strdup(&args[0]);
if (num) { if (num) {
info->max_extent = parse_size(num); info->max_extent =
btrfs_parse_size(num);
kfree(num); kfree(num);
info->max_extent = max_t(u64, info->max_extent = max_t(u64,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment