mirror of
https://github.com/linux-msm/laptops-kernel.git
synced 2026-08-13 14:19:53 -07:00
Btrfs: Support for online FS resize (grow and shrink)
Signed-off-by: Chris Mason <chris.mason@oracle.com>
This commit is contained in:
@@ -508,6 +508,8 @@ BTRFS_SETGET_STACK_FUNCS(block_group_used, struct btrfs_block_group_item,
|
||||
used, 64);
|
||||
BTRFS_SETGET_FUNCS(disk_block_group_used, struct btrfs_block_group_item,
|
||||
used, 64);
|
||||
BTRFS_SETGET_FUNCS(disk_block_group_flags, struct btrfs_block_group_item,
|
||||
flags, 8);
|
||||
|
||||
/* struct btrfs_inode_ref */
|
||||
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,
|
||||
int level,
|
||||
u64 hint,
|
||||
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,
|
||||
struct btrfs_root *root,
|
||||
struct btrfs_path *path, u64 bytenr,
|
||||
@@ -1117,6 +1122,9 @@ int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
|
||||
struct btrfs_root *root, struct btrfs_path *path,
|
||||
u64 isize);
|
||||
/* 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 for_del);
|
||||
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);
|
||||
ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size);
|
||||
int btrfs_delete_xattrs(struct btrfs_trans_handle *trans,
|
||||
struct btrfs_root *root, struct inode *inode);
|
||||
/* super.c */
|
||||
u64 btrfs_parse_size(char *str);
|
||||
#endif
|
||||
|
||||
+24
-8
@@ -471,13 +471,17 @@ insert:
|
||||
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 *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
|
||||
struct btrfs_key *location)
|
||||
{
|
||||
struct btrfs_root *root;
|
||||
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,
|
||||
(unsigned long)location->objectid);
|
||||
if (root)
|
||||
@@ -494,6 +498,23 @@ struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
|
||||
kfree(root);
|
||||
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);
|
||||
if (ret) {
|
||||
@@ -509,11 +530,6 @@ struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
|
||||
kfree(root);
|
||||
return ERR_PTR(ret);
|
||||
}
|
||||
|
||||
ret = btrfs_find_dead_roots(fs_info->tree_root,
|
||||
root->root_key.objectid, root);
|
||||
BUG_ON(ret);
|
||||
|
||||
return root;
|
||||
}
|
||||
#if 0
|
||||
|
||||
@@ -39,6 +39,8 @@ struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
|
||||
const char *name, int namelen);
|
||||
struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
|
||||
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,
|
||||
struct block_device *bdev,
|
||||
u64 device_id,
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
+102
-3
@@ -2346,7 +2346,7 @@ fail_unlock:
|
||||
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,
|
||||
pgoff_t offset, pgoff_t last_index)
|
||||
{
|
||||
@@ -2372,6 +2372,8 @@ int btrfs_defrag_file(struct file *file) {
|
||||
unsigned long ra_index = 0;
|
||||
u64 page_start;
|
||||
u64 page_end;
|
||||
u64 delalloc_start;
|
||||
u64 existing_delalloc;
|
||||
unsigned long i;
|
||||
int ret;
|
||||
|
||||
@@ -2385,8 +2387,9 @@ int btrfs_defrag_file(struct file *file) {
|
||||
last_index = inode->i_size >> PAGE_CACHE_SHIFT;
|
||||
for (i = 0; i <= last_index; i++) {
|
||||
if (i == ra_index) {
|
||||
ra_index = force_ra(inode->i_mapping, &file->f_ra,
|
||||
file, ra_index, last_index);
|
||||
ra_index = btrfs_force_ra(inode->i_mapping,
|
||||
&file->f_ra,
|
||||
file, ra_index, last_index);
|
||||
}
|
||||
page = grab_cache_page(inode->i_mapping, i);
|
||||
if (!page)
|
||||
@@ -2404,8 +2407,19 @@ int btrfs_defrag_file(struct file *file) {
|
||||
page_end = page_start + PAGE_CACHE_SIZE - 1;
|
||||
|
||||
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,
|
||||
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);
|
||||
set_page_dirty(page);
|
||||
unlock_page(page);
|
||||
@@ -2418,6 +2432,89 @@ out_unlock:
|
||||
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)
|
||||
{
|
||||
struct btrfs_ioctl_vol_args *vol_args;
|
||||
@@ -2510,6 +2607,8 @@ long btrfs_ioctl(struct file *file, unsigned int
|
||||
return btrfs_ioctl_snap_create(root, (void __user *)arg);
|
||||
case BTRFS_IOC_DEFRAG:
|
||||
return btrfs_ioctl_defrag(file);
|
||||
case BTRFS_IOC_RESIZE:
|
||||
return btrfs_ioctl_resize(root, (void __user *)arg);
|
||||
}
|
||||
|
||||
return -ENOTTY;
|
||||
|
||||
@@ -30,4 +30,6 @@ struct btrfs_ioctl_vol_args {
|
||||
struct btrfs_ioctl_vol_args)
|
||||
#define BTRFS_IOC_DEFRAG _IOW(BTRFS_IOCTL_MAGIC, 2, \
|
||||
struct btrfs_ioctl_vol_args)
|
||||
#define BTRFS_IOC_RESIZE _IOW(BTRFS_IOCTL_MAGIC, 3, \
|
||||
struct btrfs_ioctl_vol_args)
|
||||
#endif
|
||||
|
||||
@@ -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);
|
||||
if (ret < 0)
|
||||
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);
|
||||
leaf = path->nodes[0];
|
||||
ri = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_item);
|
||||
|
||||
+4
-3
@@ -74,9 +74,9 @@ static match_table_t tokens = {
|
||||
{Opt_err, NULL}
|
||||
};
|
||||
|
||||
static unsigned long parse_size(char *str)
|
||||
u64 btrfs_parse_size(char *str)
|
||||
{
|
||||
unsigned long res;
|
||||
u64 res;
|
||||
int mult = 1;
|
||||
char *end;
|
||||
char last;
|
||||
@@ -150,7 +150,8 @@ static int parse_options (char * options,
|
||||
if (info) {
|
||||
char *num = match_strdup(&args[0]);
|
||||
if (num) {
|
||||
info->max_extent = parse_size(num);
|
||||
info->max_extent =
|
||||
btrfs_parse_size(num);
|
||||
kfree(num);
|
||||
|
||||
info->max_extent = max_t(u64,
|
||||
|
||||
Reference in New Issue
Block a user