Commit d6c0cb37 authored by Chris Mason's avatar Chris Mason

Merge branch 'cleanups_and_fixes' into inode_numbers

Conflicts:
	fs/btrfs/tree-log.c
	fs/btrfs/volumes.c
Signed-off-by: default avatarChris Mason <chris.mason@oracle.com>
parents 8e531cdf 1f78160c
...@@ -3211,7 +3211,6 @@ int btrfs_truncate_item(struct btrfs_trans_handle *trans, ...@@ -3211,7 +3211,6 @@ int btrfs_truncate_item(struct btrfs_trans_handle *trans,
struct btrfs_path *path, struct btrfs_path *path,
u32 new_size, int from_end) u32 new_size, int from_end)
{ {
int ret = 0;
int slot; int slot;
struct extent_buffer *leaf; struct extent_buffer *leaf;
struct btrfs_item *item; struct btrfs_item *item;
...@@ -3309,12 +3308,11 @@ int btrfs_truncate_item(struct btrfs_trans_handle *trans, ...@@ -3309,12 +3308,11 @@ int btrfs_truncate_item(struct btrfs_trans_handle *trans,
btrfs_set_item_size(leaf, item, new_size); btrfs_set_item_size(leaf, item, new_size);
btrfs_mark_buffer_dirty(leaf); btrfs_mark_buffer_dirty(leaf);
ret = 0;
if (btrfs_leaf_free_space(root, leaf) < 0) { if (btrfs_leaf_free_space(root, leaf) < 0) {
btrfs_print_leaf(root, leaf); btrfs_print_leaf(root, leaf);
BUG(); BUG();
} }
return ret; return 0;
} }
/* /*
...@@ -3324,7 +3322,6 @@ int btrfs_extend_item(struct btrfs_trans_handle *trans, ...@@ -3324,7 +3322,6 @@ int btrfs_extend_item(struct btrfs_trans_handle *trans,
struct btrfs_root *root, struct btrfs_path *path, struct btrfs_root *root, struct btrfs_path *path,
u32 data_size) u32 data_size)
{ {
int ret = 0;
int slot; int slot;
struct extent_buffer *leaf; struct extent_buffer *leaf;
struct btrfs_item *item; struct btrfs_item *item;
...@@ -3389,12 +3386,11 @@ int btrfs_extend_item(struct btrfs_trans_handle *trans, ...@@ -3389,12 +3386,11 @@ int btrfs_extend_item(struct btrfs_trans_handle *trans,
btrfs_set_item_size(leaf, item, old_size + data_size); btrfs_set_item_size(leaf, item, old_size + data_size);
btrfs_mark_buffer_dirty(leaf); btrfs_mark_buffer_dirty(leaf);
ret = 0;
if (btrfs_leaf_free_space(root, leaf) < 0) { if (btrfs_leaf_free_space(root, leaf) < 0) {
btrfs_print_leaf(root, leaf); btrfs_print_leaf(root, leaf);
BUG(); BUG();
} }
return ret; return 0;
} }
/* /*
......
...@@ -1113,9 +1113,6 @@ struct btrfs_fs_info { ...@@ -1113,9 +1113,6 @@ struct btrfs_fs_info {
struct btrfs_root { struct btrfs_root {
struct extent_buffer *node; struct extent_buffer *node;
/* the node lock is held while changing the node pointer */
spinlock_t node_lock;
struct extent_buffer *commit_root; struct extent_buffer *commit_root;
struct btrfs_root *log_root; struct btrfs_root *log_root;
struct btrfs_root *reloc_root; struct btrfs_root *reloc_root;
......
...@@ -50,7 +50,6 @@ static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle ...@@ -50,7 +50,6 @@ static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle
if (di) if (di)
return ERR_PTR(-EEXIST); return ERR_PTR(-EEXIST);
ret = btrfs_extend_item(trans, root, path, data_size); ret = btrfs_extend_item(trans, root, path, data_size);
WARN_ON(ret > 0);
} }
if (ret < 0) if (ret < 0)
return ERR_PTR(ret); return ERR_PTR(ret);
...@@ -438,7 +437,7 @@ int verify_dir_item(struct btrfs_root *root, ...@@ -438,7 +437,7 @@ int verify_dir_item(struct btrfs_root *root,
namelen = XATTR_NAME_MAX; namelen = XATTR_NAME_MAX;
if (btrfs_dir_name_len(leaf, dir_item) > namelen) { if (btrfs_dir_name_len(leaf, dir_item) > namelen) {
printk(KERN_CRIT "btrfS: invalid dir item name len: %u\n", printk(KERN_CRIT "btrfs: invalid dir item name len: %u\n",
(unsigned)btrfs_dir_data_len(leaf, dir_item)); (unsigned)btrfs_dir_data_len(leaf, dir_item));
return 1; return 1;
} }
......
...@@ -1053,7 +1053,6 @@ static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize, ...@@ -1053,7 +1053,6 @@ static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
INIT_LIST_HEAD(&root->dirty_list); INIT_LIST_HEAD(&root->dirty_list);
INIT_LIST_HEAD(&root->orphan_list); INIT_LIST_HEAD(&root->orphan_list);
INIT_LIST_HEAD(&root->root_list); INIT_LIST_HEAD(&root->root_list);
spin_lock_init(&root->node_lock);
spin_lock_init(&root->orphan_lock); spin_lock_init(&root->orphan_lock);
spin_lock_init(&root->inode_lock); spin_lock_init(&root->inode_lock);
spin_lock_init(&root->accounting_lock); spin_lock_init(&root->accounting_lock);
...@@ -1363,7 +1362,8 @@ static int btrfs_congested_fn(void *congested_data, int bdi_bits) ...@@ -1363,7 +1362,8 @@ static int btrfs_congested_fn(void *congested_data, int bdi_bits)
struct btrfs_device *device; struct btrfs_device *device;
struct backing_dev_info *bdi; struct backing_dev_info *bdi;
list_for_each_entry(device, &info->fs_devices->devices, dev_list) { rcu_read_lock();
list_for_each_entry_rcu(device, &info->fs_devices->devices, dev_list) {
if (!device->bdev) if (!device->bdev)
continue; continue;
bdi = blk_get_backing_dev_info(device->bdev); bdi = blk_get_backing_dev_info(device->bdev);
...@@ -1372,6 +1372,7 @@ static int btrfs_congested_fn(void *congested_data, int bdi_bits) ...@@ -1372,6 +1372,7 @@ static int btrfs_congested_fn(void *congested_data, int bdi_bits)
break; break;
} }
} }
rcu_read_unlock();
return ret; return ret;
} }
...@@ -2305,9 +2306,9 @@ int write_all_supers(struct btrfs_root *root, int max_mirrors) ...@@ -2305,9 +2306,9 @@ int write_all_supers(struct btrfs_root *root, int max_mirrors)
sb = &root->fs_info->super_for_commit; sb = &root->fs_info->super_for_commit;
dev_item = &sb->dev_item; dev_item = &sb->dev_item;
mutex_lock(&root->fs_info->fs_devices->device_list_mutex); rcu_read_lock();
head = &root->fs_info->fs_devices->devices; head = &root->fs_info->fs_devices->devices;
list_for_each_entry(dev, head, dev_list) { list_for_each_entry_rcu(dev, head, dev_list) {
if (!dev->bdev) { if (!dev->bdev) {
total_errors++; total_errors++;
continue; continue;
...@@ -2340,7 +2341,7 @@ int write_all_supers(struct btrfs_root *root, int max_mirrors) ...@@ -2340,7 +2341,7 @@ int write_all_supers(struct btrfs_root *root, int max_mirrors)
} }
total_errors = 0; total_errors = 0;
list_for_each_entry(dev, head, dev_list) { list_for_each_entry_rcu(dev, head, dev_list) {
if (!dev->bdev) if (!dev->bdev)
continue; continue;
if (!dev->in_fs_metadata || !dev->writeable) if (!dev->in_fs_metadata || !dev->writeable)
...@@ -2350,7 +2351,7 @@ int write_all_supers(struct btrfs_root *root, int max_mirrors) ...@@ -2350,7 +2351,7 @@ int write_all_supers(struct btrfs_root *root, int max_mirrors)
if (ret) if (ret)
total_errors++; total_errors++;
} }
mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); rcu_read_unlock();
if (total_errors > max_errors) { if (total_errors > max_errors) {
printk(KERN_ERR "btrfs: %d errors while writing supers\n", printk(KERN_ERR "btrfs: %d errors while writing supers\n",
total_errors); total_errors);
......
...@@ -952,7 +952,6 @@ static int convert_extent_item_v0(struct btrfs_trans_handle *trans, ...@@ -952,7 +952,6 @@ static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
BUG_ON(ret); BUG_ON(ret);
ret = btrfs_extend_item(trans, root, path, new_size); ret = btrfs_extend_item(trans, root, path, new_size);
BUG_ON(ret);
leaf = path->nodes[0]; leaf = path->nodes[0];
item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
...@@ -1560,7 +1559,6 @@ int setup_inline_extent_backref(struct btrfs_trans_handle *trans, ...@@ -1560,7 +1559,6 @@ int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
size = btrfs_extent_inline_ref_size(type); size = btrfs_extent_inline_ref_size(type);
ret = btrfs_extend_item(trans, root, path, size); ret = btrfs_extend_item(trans, root, path, size);
BUG_ON(ret);
ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
refs = btrfs_extent_refs(leaf, ei); refs = btrfs_extent_refs(leaf, ei);
...@@ -1689,7 +1687,6 @@ int update_inline_extent_backref(struct btrfs_trans_handle *trans, ...@@ -1689,7 +1687,6 @@ int update_inline_extent_backref(struct btrfs_trans_handle *trans,
end - ptr - size); end - ptr - size);
item_size -= size; item_size -= size;
ret = btrfs_truncate_item(trans, root, path, item_size, 1); ret = btrfs_truncate_item(trans, root, path, item_size, 1);
BUG_ON(ret);
} }
btrfs_mark_buffer_dirty(leaf); btrfs_mark_buffer_dirty(leaf);
return 0; return 0;
...@@ -3307,6 +3304,10 @@ static int shrink_delalloc(struct btrfs_trans_handle *trans, ...@@ -3307,6 +3304,10 @@ static int shrink_delalloc(struct btrfs_trans_handle *trans,
if (reserved == 0) if (reserved == 0)
return 0; return 0;
/* nothing to shrink - nothing to reclaim */
if (root->fs_info->delalloc_bytes == 0)
return 0;
max_reclaim = min(reserved, to_reclaim); max_reclaim = min(reserved, to_reclaim);
while (loops < 1024) { while (loops < 1024) {
......
...@@ -439,6 +439,15 @@ static int clear_state_bit(struct extent_io_tree *tree, ...@@ -439,6 +439,15 @@ static int clear_state_bit(struct extent_io_tree *tree,
return ret; return ret;
} }
static struct extent_state *
alloc_extent_state_atomic(struct extent_state *prealloc)
{
if (!prealloc)
prealloc = alloc_extent_state(GFP_ATOMIC);
return prealloc;
}
/* /*
* clear some bits on a range in the tree. This may require splitting * clear some bits on a range in the tree. This may require splitting
* or inserting elements in the tree, so the gfp mask is used to * or inserting elements in the tree, so the gfp mask is used to
...@@ -476,8 +485,7 @@ int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, ...@@ -476,8 +485,7 @@ int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
again: again:
if (!prealloc && (mask & __GFP_WAIT)) { if (!prealloc && (mask & __GFP_WAIT)) {
prealloc = alloc_extent_state(mask); prealloc = alloc_extent_state(mask);
if (!prealloc) BUG_ON(!prealloc);
return -ENOMEM;
} }
spin_lock(&tree->lock); spin_lock(&tree->lock);
...@@ -529,8 +537,8 @@ int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, ...@@ -529,8 +537,8 @@ int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
*/ */
if (state->start < start) { if (state->start < start) {
if (!prealloc) prealloc = alloc_extent_state_atomic(prealloc);
prealloc = alloc_extent_state(GFP_ATOMIC); BUG_ON(!prealloc);
err = split_state(tree, state, prealloc, start); err = split_state(tree, state, prealloc, start);
BUG_ON(err == -EEXIST); BUG_ON(err == -EEXIST);
prealloc = NULL; prealloc = NULL;
...@@ -551,8 +559,8 @@ int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, ...@@ -551,8 +559,8 @@ int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
* on the first half * on the first half
*/ */
if (state->start <= end && state->end > end) { if (state->start <= end && state->end > end) {
if (!prealloc) prealloc = alloc_extent_state_atomic(prealloc);
prealloc = alloc_extent_state(GFP_ATOMIC); BUG_ON(!prealloc);
err = split_state(tree, state, prealloc, end + 1); err = split_state(tree, state, prealloc, end + 1);
BUG_ON(err == -EEXIST); BUG_ON(err == -EEXIST);
if (wake) if (wake)
...@@ -725,8 +733,7 @@ int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, ...@@ -725,8 +733,7 @@ int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
again: again:
if (!prealloc && (mask & __GFP_WAIT)) { if (!prealloc && (mask & __GFP_WAIT)) {
prealloc = alloc_extent_state(mask); prealloc = alloc_extent_state(mask);
if (!prealloc) BUG_ON(!prealloc);
return -ENOMEM;
} }
spin_lock(&tree->lock); spin_lock(&tree->lock);
...@@ -743,6 +750,8 @@ int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, ...@@ -743,6 +750,8 @@ int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
*/ */
node = tree_search(tree, start); node = tree_search(tree, start);
if (!node) { if (!node) {
prealloc = alloc_extent_state_atomic(prealloc);
BUG_ON(!prealloc);
err = insert_state(tree, prealloc, start, end, &bits); err = insert_state(tree, prealloc, start, end, &bits);
prealloc = NULL; prealloc = NULL;
BUG_ON(err == -EEXIST); BUG_ON(err == -EEXIST);
...@@ -771,21 +780,19 @@ int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, ...@@ -771,21 +780,19 @@ int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
if (err) if (err)
goto out; goto out;
next_node = rb_next(node);
cache_state(state, cached_state); cache_state(state, cached_state);
merge_state(tree, state); merge_state(tree, state);
if (last_end == (u64)-1) if (last_end == (u64)-1)
goto out; goto out;
start = last_end + 1; start = last_end + 1;
if (start < end && prealloc && !need_resched()) { if (next_node && start < end && prealloc && !need_resched()) {
next_node = rb_next(node);
if (next_node) {
state = rb_entry(next_node, struct extent_state, state = rb_entry(next_node, struct extent_state,
rb_node); rb_node);
if (state->start == start) if (state->start == start)
goto hit_next; goto hit_next;
} }
}
goto search_again; goto search_again;
} }
...@@ -811,6 +818,9 @@ int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, ...@@ -811,6 +818,9 @@ int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
err = -EEXIST; err = -EEXIST;
goto out; goto out;
} }
prealloc = alloc_extent_state_atomic(prealloc);
BUG_ON(!prealloc);
err = split_state(tree, state, prealloc, start); err = split_state(tree, state, prealloc, start);
BUG_ON(err == -EEXIST); BUG_ON(err == -EEXIST);
prealloc = NULL; prealloc = NULL;
...@@ -841,14 +851,25 @@ int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, ...@@ -841,14 +851,25 @@ int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
this_end = end; this_end = end;
else else
this_end = last_start - 1; this_end = last_start - 1;
prealloc = alloc_extent_state_atomic(prealloc);
BUG_ON(!prealloc);
/*
* Avoid to free 'prealloc' if it can be merged with
* the later extent.
*/
atomic_inc(&prealloc->refs);
err = insert_state(tree, prealloc, start, this_end, err = insert_state(tree, prealloc, start, this_end,
&bits); &bits);
BUG_ON(err == -EEXIST); BUG_ON(err == -EEXIST);
if (err) { if (err) {
free_extent_state(prealloc);
prealloc = NULL; prealloc = NULL;
goto out; goto out;
} }
cache_state(prealloc, cached_state); cache_state(prealloc, cached_state);
free_extent_state(prealloc);
prealloc = NULL; prealloc = NULL;
start = this_end + 1; start = this_end + 1;
goto search_again; goto search_again;
...@@ -865,6 +886,9 @@ int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, ...@@ -865,6 +886,9 @@ int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
err = -EEXIST; err = -EEXIST;
goto out; goto out;
} }
prealloc = alloc_extent_state_atomic(prealloc);
BUG_ON(!prealloc);
err = split_state(tree, state, prealloc, end + 1); err = split_state(tree, state, prealloc, end + 1);
BUG_ON(err == -EEXIST); BUG_ON(err == -EEXIST);
......
...@@ -502,7 +502,6 @@ static noinline int truncate_one_csum(struct btrfs_trans_handle *trans, ...@@ -502,7 +502,6 @@ static noinline int truncate_one_csum(struct btrfs_trans_handle *trans,
u32 new_size = (bytenr - key->offset) >> blocksize_bits; u32 new_size = (bytenr - key->offset) >> blocksize_bits;
new_size *= csum_size; new_size *= csum_size;
ret = btrfs_truncate_item(trans, root, path, new_size, 1); ret = btrfs_truncate_item(trans, root, path, new_size, 1);
BUG_ON(ret);
} else if (key->offset >= bytenr && csum_end > end_byte && } else if (key->offset >= bytenr && csum_end > end_byte &&
end_byte > key->offset) { end_byte > key->offset) {
/* /*
...@@ -515,7 +514,6 @@ static noinline int truncate_one_csum(struct btrfs_trans_handle *trans, ...@@ -515,7 +514,6 @@ static noinline int truncate_one_csum(struct btrfs_trans_handle *trans,
new_size *= csum_size; new_size *= csum_size;
ret = btrfs_truncate_item(trans, root, path, new_size, 0); ret = btrfs_truncate_item(trans, root, path, new_size, 0);
BUG_ON(ret);
key->offset = end_byte; key->offset = end_byte;
ret = btrfs_set_item_key_safe(trans, root, path, key); ret = btrfs_set_item_key_safe(trans, root, path, key);
...@@ -558,10 +556,10 @@ int btrfs_del_csums(struct btrfs_trans_handle *trans, ...@@ -558,10 +556,10 @@ int btrfs_del_csums(struct btrfs_trans_handle *trans,
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) {
if (path->slots[0] == 0) if (path->slots[0] == 0)
goto out; break;
path->slots[0]--; path->slots[0]--;
} else if (ret < 0) { } else if (ret < 0) {
goto out; break;
} }
leaf = path->nodes[0]; leaf = path->nodes[0];
...@@ -586,7 +584,8 @@ int btrfs_del_csums(struct btrfs_trans_handle *trans, ...@@ -586,7 +584,8 @@ int btrfs_del_csums(struct btrfs_trans_handle *trans,
/* delete the entire item, it is inside our range */ /* delete the entire item, it is inside our range */
if (key.offset >= bytenr && csum_end <= end_byte) { if (key.offset >= bytenr && csum_end <= end_byte) {
ret = btrfs_del_item(trans, root, path); ret = btrfs_del_item(trans, root, path);
BUG_ON(ret); if (ret)
goto out;
if (key.offset == bytenr) if (key.offset == bytenr)
break; break;
} else if (key.offset < bytenr && csum_end > end_byte) { } else if (key.offset < bytenr && csum_end > end_byte) {
...@@ -640,9 +639,10 @@ int btrfs_del_csums(struct btrfs_trans_handle *trans, ...@@ -640,9 +639,10 @@ int btrfs_del_csums(struct btrfs_trans_handle *trans,
} }
btrfs_release_path(path); btrfs_release_path(path);
} }
ret = 0;
out: out:
btrfs_free_path(path); btrfs_free_path(path);
return 0; return ret;
} }
int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans, int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
...@@ -768,7 +768,6 @@ int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans, ...@@ -768,7 +768,6 @@ int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
goto insert; goto insert;
ret = btrfs_extend_item(trans, root, path, diff); ret = btrfs_extend_item(trans, root, path, diff);
BUG_ON(ret);
goto csum; goto csum;
} }
......
...@@ -130,7 +130,6 @@ int btrfs_del_inode_ref(struct btrfs_trans_handle *trans, ...@@ -130,7 +130,6 @@ int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
item_size - (ptr + sub_item_len - item_start)); item_size - (ptr + sub_item_len - item_start));
ret = btrfs_truncate_item(trans, root, path, ret = btrfs_truncate_item(trans, root, path,
item_size - sub_item_len, 1); item_size - sub_item_len, 1);
BUG_ON(ret);
out: out:
btrfs_free_path(path); btrfs_free_path(path);
return ret; return ret;
...@@ -167,7 +166,6 @@ int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans, ...@@ -167,7 +166,6 @@ int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,
old_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]); old_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
ret = btrfs_extend_item(trans, root, path, ins_len); ret = btrfs_extend_item(trans, root, path, ins_len);
BUG_ON(ret);
ref = btrfs_item_ptr(path->nodes[0], path->slots[0], ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
struct btrfs_inode_ref); struct btrfs_inode_ref);
ref = (struct btrfs_inode_ref *)((unsigned long)ref + old_size); ref = (struct btrfs_inode_ref *)((unsigned long)ref + old_size);
......
...@@ -1323,7 +1323,7 @@ static int btrfs_set_bit_hook(struct inode *inode, ...@@ -1323,7 +1323,7 @@ static int btrfs_set_bit_hook(struct inode *inode,
/* /*
* set_bit and clear bit hooks normally require _irqsave/restore * set_bit and clear bit hooks normally require _irqsave/restore
* but in this case, we are only testeing for the DELALLOC * but in this case, we are only testing for the DELALLOC
* bit, which is only set or cleared with irqs on * bit, which is only set or cleared with irqs on
*/ */
if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
...@@ -1356,7 +1356,7 @@ static int btrfs_clear_bit_hook(struct inode *inode, ...@@ -1356,7 +1356,7 @@ static int btrfs_clear_bit_hook(struct inode *inode,
{ {
/* /*
* set_bit and clear bit hooks normally require _irqsave/restore * set_bit and clear bit hooks normally require _irqsave/restore
* but in this case, we are only testeing for the DELALLOC * but in this case, we are only testing for the DELALLOC
* bit, which is only set or cleared with irqs on * bit, which is only set or cleared with irqs on
*/ */
if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
...@@ -3286,7 +3286,6 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, ...@@ -3286,7 +3286,6 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
btrfs_file_extent_calc_inline_size(size); btrfs_file_extent_calc_inline_size(size);
ret = btrfs_truncate_item(trans, root, path, ret = btrfs_truncate_item(trans, root, path,
size, 1); size, 1);
BUG_ON(ret);
} else if (root->ref_cows) { } else if (root->ref_cows) {
inode_sub_bytes(inode, item_end + 1 - inode_sub_bytes(inode, item_end + 1 -
found_key.offset); found_key.offset);
...@@ -7167,6 +7166,7 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry, ...@@ -7167,6 +7166,7 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
datasize); datasize);
if (err) { if (err) {
drop_inode = 1; drop_inode = 1;
btrfs_free_path(path);
goto out_unlock; goto out_unlock;
} }
leaf = path->nodes[0]; leaf = path->nodes[0];
......
...@@ -282,8 +282,9 @@ static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg) ...@@ -282,8 +282,9 @@ static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
if (!capable(CAP_SYS_ADMIN)) if (!capable(CAP_SYS_ADMIN))
return -EPERM; return -EPERM;
mutex_lock(&fs_info->fs_devices->device_list_mutex); rcu_read_lock();
list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) { list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
dev_list) {
if (!device->bdev) if (!device->bdev)
continue; continue;
q = bdev_get_queue(device->bdev); q = bdev_get_queue(device->bdev);
...@@ -293,7 +294,7 @@ static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg) ...@@ -293,7 +294,7 @@ static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
minlen); minlen);
} }
} }
mutex_unlock(&fs_info->fs_devices->device_list_mutex); rcu_read_unlock();
if (!num_devices) if (!num_devices)
return -EOPNOTSUPP; return -EOPNOTSUPP;
...@@ -1279,7 +1280,6 @@ static noinline int copy_to_sk(struct btrfs_root *root, ...@@ -1279,7 +1280,6 @@ static noinline int copy_to_sk(struct btrfs_root *root,
int nritems; int nritems;
int i; int i;
int slot; int slot;
int found = 0;
int ret = 0; int ret = 0;
leaf = path->nodes[0]; leaf = path->nodes[0];
...@@ -1326,7 +1326,7 @@ static noinline int copy_to_sk(struct btrfs_root *root, ...@@ -1326,7 +1326,7 @@ static noinline int copy_to_sk(struct btrfs_root *root,
item_off, item_len); item_off, item_len);
*sk_offset += item_len; *sk_offset += item_len;
} }
found++; (*num_found)++;
if (*num_found >= sk->nr_items) if (*num_found >= sk->nr_items)
break; break;
...@@ -1345,7 +1345,6 @@ static noinline int copy_to_sk(struct btrfs_root *root, ...@@ -1345,7 +1345,6 @@ static noinline int copy_to_sk(struct btrfs_root *root,
} else } else
ret = 1; ret = 1;
overflow: overflow:
*num_found += found;
return ret; return ret;
} }
......
...@@ -338,7 +338,10 @@ int btrfs_del_root_ref(struct btrfs_trans_handle *trans, ...@@ -338,7 +338,10 @@ int btrfs_del_root_ref(struct btrfs_trans_handle *trans,
*sequence = btrfs_root_ref_sequence(leaf, ref); *sequence = btrfs_root_ref_sequence(leaf, ref);
ret = btrfs_del_item(trans, tree_root, path); ret = btrfs_del_item(trans, tree_root, path);
BUG_ON(ret); if (ret) {
err = ret;
goto out;
}
} else } else
err = -ENOENT; err = -ENOENT;
...@@ -350,6 +353,7 @@ int btrfs_del_root_ref(struct btrfs_trans_handle *trans, ...@@ -350,6 +353,7 @@ int btrfs_del_root_ref(struct btrfs_trans_handle *trans,
goto again; goto again;
} }
out:
btrfs_free_path(path); btrfs_free_path(path);
return err; return err;
} }
......
...@@ -507,8 +507,10 @@ static struct dentry *get_default_root(struct super_block *sb, ...@@ -507,8 +507,10 @@ static struct dentry *get_default_root(struct super_block *sb,
*/ */
dir_id = btrfs_super_root_dir(&root->fs_info->super_copy); dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0); di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
if (IS_ERR(di)) if (IS_ERR(di)) {
btrfs_free_path(path);
return ERR_CAST(di); return ERR_CAST(di);
}
if (!di) { if (!di) {
/* /*
* Ok the default dir item isn't there. This is weird since * Ok the default dir item isn't there. This is weird since
......
...@@ -382,7 +382,6 @@ static noinline int overwrite_item(struct btrfs_trans_handle *trans, ...@@ -382,7 +382,6 @@ static noinline int overwrite_item(struct btrfs_trans_handle *trans,
} else if (found_size < item_size) { } else if (found_size < item_size) {
ret = btrfs_extend_item(trans, root, path, ret = btrfs_extend_item(trans, root, path,
item_size - found_size); item_size - found_size);
BUG_ON(ret);
} }
} else if (ret) { } else if (ret) {
return ret; return ret;
...@@ -590,6 +589,7 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans, ...@@ -590,6 +589,7 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
ins.objectid, ins.offset, ins.objectid, ins.offset,
0, root->root_key.objectid, 0, root->root_key.objectid,
key->objectid, offset); key->objectid, offset);
BUG_ON(ret);
} else { } else {
/* /*
* insert the extent pointer in the extent * insert the extent pointer in the extent
...@@ -678,7 +678,10 @@ static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans, ...@@ -678,7 +678,10 @@ static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
btrfs_release_path(path); btrfs_release_path(path);
inode = read_one_inode(root, location.objectid); inode = read_one_inode(root, location.objectid);
BUG_ON(!inode); if (!inode) {
kfree(name);
return -EIO;
}
ret = link_to_fixup_dir(trans, root, path, location.objectid); ret = link_to_fixup_dir(trans, root, path, location.objectid);
BUG_ON(ret); BUG_ON(ret);
...@@ -817,7 +820,10 @@ static noinline int add_inode_ref(struct btrfs_trans_handle *trans, ...@@ -817,7 +820,10 @@ static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
return -ENOENT; return -ENOENT;
inode = read_one_inode(root, key->objectid); inode = read_one_inode(root, key->objectid);
BUG_ON(!inode); if (!inode) {
iput(dir);
return -EIO;
}
ref_ptr = btrfs_item_ptr_offset(eb, slot); ref_ptr = btrfs_item_ptr_offset(eb, slot);
ref_end = ref_ptr + btrfs_item_size_nr(eb, slot); ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
...@@ -1051,11 +1057,13 @@ static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans, ...@@ -1051,11 +1057,13 @@ static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
break; break;
ret = btrfs_del_item(trans, root, path); ret = btrfs_del_item(trans, root, path);
BUG_ON(ret); if (ret)
goto out;
btrfs_release_path(path); btrfs_release_path(path);
inode = read_one_inode(root, key.offset); inode = read_one_inode(root, key.offset);
BUG_ON(!inode); if (!inode)
return -EIO;
ret = fixup_inode_link_count(trans, root, inode); ret = fixup_inode_link_count(trans, root, inode);
BUG_ON(ret); BUG_ON(ret);
...@@ -1069,8 +1077,10 @@ static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans, ...@@ -1069,8 +1077,10 @@ static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
*/ */
key.offset = (u64)-1; key.offset = (u64)-1;
} }
ret = 0;
out:
btrfs_release_path(path); btrfs_release_path(path);
return 0; return ret;
} }
...@@ -1089,7 +1099,8 @@ static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans, ...@@ -1089,7 +1099,8 @@ static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
struct inode *inode; struct inode *inode;
inode = read_one_inode(root, objectid); inode = read_one_inode(root, objectid);
BUG_ON(!inode); if (!inode)
return -EIO;
key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID; key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY); btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
...@@ -1176,7 +1187,8 @@ static noinline int replay_one_name(struct btrfs_trans_handle *trans, ...@@ -1176,7 +1187,8 @@ static noinline int replay_one_name(struct btrfs_trans_handle *trans,
int ret; int ret;
dir = read_one_inode(root, key->objectid); dir = read_one_inode(root, key->objectid);
BUG_ON(!dir); if (!dir)
return -EIO;
name_len = btrfs_dir_name_len(eb, di); name_len = btrfs_dir_name_len(eb, di);
name = kmalloc(name_len, GFP_NOFS); name = kmalloc(name_len, GFP_NOFS);
...@@ -1432,7 +1444,10 @@ static noinline int check_item_in_log(struct btrfs_trans_handle *trans, ...@@ -1432,7 +1444,10 @@ static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
btrfs_release_path(path); btrfs_release_path(path);
btrfs_release_path(log_path); btrfs_release_path(log_path);
inode = read_one_inode(root, location.objectid); inode = read_one_inode(root, location.objectid);
BUG_ON(!inode); if (!inode) {
kfree(name);
return -EIO;
}
ret = link_to_fixup_dir(trans, root, ret = link_to_fixup_dir(trans, root,
path, location.objectid); path, location.objectid);
...@@ -2589,7 +2604,8 @@ static int drop_objectid_items(struct btrfs_trans_handle *trans, ...@@ -2589,7 +2604,8 @@ static int drop_objectid_items(struct btrfs_trans_handle *trans,
break; break;
ret = btrfs_del_item(trans, log, path); ret = btrfs_del_item(trans, log, path);
BUG_ON(ret); if (ret)
break;
btrfs_release_path(path); btrfs_release_path(path);
} }
btrfs_release_path(path); btrfs_release_path(path);
......
#!/bin/bash
#
# determine-version -- report a useful version for releases
#
# Copyright 2008, Aron Griffis <agriffis@n01se.net>
# Copyright 2008, Oracle
# Released under the GNU GPLv2
v="v0.16"
which git &> /dev/null
if [ $? == 0 ]; then
git branch >& /dev/null
if [ $? == 0 ]; then
if head=`git rev-parse --verify HEAD 2>/dev/null`; then
if tag=`git describe --tags 2>/dev/null`; then
v="$tag"
fi
# Are there uncommitted changes?
git update-index --refresh --unmerged > /dev/null
if git diff-index --name-only HEAD | \
grep -v "^scripts/package" \
| read dummy; then
v="$v"-dirty
fi
fi
fi
fi
echo "#ifndef __BUILD_VERSION" > .build-version.h
echo "#define __BUILD_VERSION" >> .build-version.h
echo "#define BTRFS_BUILD_VERSION \"Btrfs $v\"" >> .build-version.h
echo "#endif" >> .build-version.h
diff -q version.h .build-version.h >& /dev/null
if [ $? == 0 ]; then
rm .build-version.h
exit 0
fi
mv .build-version.h version.h
...@@ -350,7 +350,7 @@ static noinline int device_list_add(const char *path, ...@@ -350,7 +350,7 @@ static noinline int device_list_add(const char *path,
INIT_LIST_HEAD(&device->dev_alloc_list); INIT_LIST_HEAD(&device->dev_alloc_list);
mutex_lock(&fs_devices->device_list_mutex); mutex_lock(&fs_devices->device_list_mutex);
list_add(&device->dev_list, &fs_devices->devices); list_add_rcu(&device->dev_list, &fs_devices->devices);
mutex_unlock(&fs_devices->device_list_mutex); mutex_unlock(&fs_devices->device_list_mutex);
device->fs_devices = fs_devices; device->fs_devices = fs_devices;
...@@ -393,7 +393,7 @@ static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig) ...@@ -393,7 +393,7 @@ static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
fs_devices->latest_trans = orig->latest_trans; fs_devices->latest_trans = orig->latest_trans;
memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid)); memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
mutex_lock(&orig->device_list_mutex); /* We have held the volume lock, it is safe to get the devices. */
list_for_each_entry(orig_dev, &orig->devices, dev_list) { list_for_each_entry(orig_dev, &orig->devices, dev_list) {
device = kzalloc(sizeof(*device), GFP_NOFS); device = kzalloc(sizeof(*device), GFP_NOFS);
if (!device) if (!device)
...@@ -416,10 +416,8 @@ static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig) ...@@ -416,10 +416,8 @@ static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
device->fs_devices = fs_devices; device->fs_devices = fs_devices;
fs_devices->num_devices++; fs_devices->num_devices++;
} }
mutex_unlock(&orig->device_list_mutex);
return fs_devices; return fs_devices;
error: error:
mutex_unlock(&orig->device_list_mutex);
free_fs_devices(fs_devices); free_fs_devices(fs_devices);
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
} }
...@@ -430,7 +428,7 @@ int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices) ...@@ -430,7 +428,7 @@ int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
mutex_lock(&uuid_mutex); mutex_lock(&uuid_mutex);
again: again:
mutex_lock(&fs_devices->device_list_mutex); /* This is the initialized path, it is safe to release the devices. */
list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
if (device->in_fs_metadata) if (device->in_fs_metadata)
continue; continue;
...@@ -450,7 +448,6 @@ int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices) ...@@ -450,7 +448,6 @@ int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
kfree(device->name); kfree(device->name);
kfree(device); kfree(device);
} }
mutex_unlock(&fs_devices->device_list_mutex);
if (fs_devices->seed) { if (fs_devices->seed) {
fs_devices = fs_devices->seed; fs_devices = fs_devices->seed;
...@@ -461,6 +458,29 @@ int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices) ...@@ -461,6 +458,29 @@ int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
return 0; return 0;
} }
static void __free_device(struct work_struct *work)
{
struct btrfs_device *device;
device = container_of(work, struct btrfs_device, rcu_work);
if (device->bdev)
blkdev_put(device->bdev, device->mode);
kfree(device->name);
kfree(device);
}
static void free_device(struct rcu_head *head)
{
struct btrfs_device *device;
device = container_of(head, struct btrfs_device, rcu);
INIT_WORK(&device->rcu_work, __free_device);
schedule_work(&device->rcu_work);
}
static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices) static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
{ {
struct btrfs_device *device; struct btrfs_device *device;
...@@ -468,20 +488,32 @@ static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices) ...@@ -468,20 +488,32 @@ static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
if (--fs_devices->opened > 0) if (--fs_devices->opened > 0)
return 0; return 0;
mutex_lock(&fs_devices->device_list_mutex);
list_for_each_entry(device, &fs_devices->devices, dev_list) { list_for_each_entry(device, &fs_devices->devices, dev_list) {
if (device->bdev) { struct btrfs_device *new_device;
blkdev_put(device->bdev, device->mode);
if (device->bdev)
fs_devices->open_devices--; fs_devices->open_devices--;
}
if (device->writeable) { if (device->writeable) {
list_del_init(&device->dev_alloc_list); list_del_init(&device->dev_alloc_list);
fs_devices->rw_devices--; fs_devices->rw_devices--;
} }
device->bdev = NULL; new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
device->writeable = 0; BUG_ON(!new_device);
device->in_fs_metadata = 0; memcpy(new_device, device, sizeof(*new_device));
new_device->name = kstrdup(device->name, GFP_NOFS);
BUG_ON(!new_device->name);
new_device->bdev = NULL;
new_device->writeable = 0;
new_device->in_fs_metadata = 0;
list_replace_rcu(&device->dev_list, &new_device->dev_list);
call_rcu(&device->rcu, free_device);
} }
mutex_unlock(&fs_devices->device_list_mutex);
WARN_ON(fs_devices->open_devices); WARN_ON(fs_devices->open_devices);
WARN_ON(fs_devices->rw_devices); WARN_ON(fs_devices->rw_devices);
fs_devices->opened = 0; fs_devices->opened = 0;
...@@ -584,6 +616,7 @@ static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices, ...@@ -584,6 +616,7 @@ static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
list_add(&device->dev_alloc_list, list_add(&device->dev_alloc_list,
&fs_devices->alloc_list); &fs_devices->alloc_list);
} }
brelse(bh);
continue; continue;
error_brelse: error_brelse:
...@@ -933,14 +966,14 @@ static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, ...@@ -933,14 +966,14 @@ static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
if (ret > 0) { if (ret > 0) {
ret = btrfs_previous_item(root, path, key.objectid, ret = btrfs_previous_item(root, path, key.objectid,
BTRFS_DEV_EXTENT_KEY); BTRFS_DEV_EXTENT_KEY);
BUG_ON(ret); if (ret)
goto out;
leaf = path->nodes[0]; leaf = path->nodes[0];
btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
extent = btrfs_item_ptr(leaf, path->slots[0], extent = btrfs_item_ptr(leaf, path->slots[0],
struct btrfs_dev_extent); struct btrfs_dev_extent);
BUG_ON(found_key.offset > start || found_key.offset + BUG_ON(found_key.offset > start || found_key.offset +
btrfs_dev_extent_length(leaf, extent) < start); btrfs_dev_extent_length(leaf, extent) < start);
ret = 0;
} else if (ret == 0) { } else if (ret == 0) {
leaf = path->nodes[0]; leaf = path->nodes[0];
extent = btrfs_item_ptr(leaf, path->slots[0], extent = btrfs_item_ptr(leaf, path->slots[0],
...@@ -951,8 +984,8 @@ static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, ...@@ -951,8 +984,8 @@ static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
if (device->bytes_used > 0) if (device->bytes_used > 0)
device->bytes_used -= btrfs_dev_extent_length(leaf, extent); device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
ret = btrfs_del_item(trans, root, path); ret = btrfs_del_item(trans, root, path);
BUG_ON(ret);
out:
btrfs_free_path(path); btrfs_free_path(path);
return ret; return ret;
} }
...@@ -1187,11 +1220,13 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path) ...@@ -1187,11 +1220,13 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path)
struct block_device *bdev; struct block_device *bdev;
struct buffer_head *bh = NULL; struct buffer_head *bh = NULL;
struct btrfs_super_block *disk_super; struct btrfs_super_block *disk_super;
struct btrfs_fs_devices *cur_devices;
u64 all_avail; u64 all_avail;
u64 devid; u64 devid;
u64 num_devices; u64 num_devices;
u8 *dev_uuid; u8 *dev_uuid;
int ret = 0; int ret = 0;
bool clear_super = false;
mutex_lock(&uuid_mutex); mutex_lock(&uuid_mutex);
mutex_lock(&root->fs_info->volume_mutex); mutex_lock(&root->fs_info->volume_mutex);
...@@ -1222,14 +1257,16 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path) ...@@ -1222,14 +1257,16 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path)
device = NULL; device = NULL;
devices = &root->fs_info->fs_devices->devices; devices = &root->fs_info->fs_devices->devices;
mutex_lock(&root->fs_info->fs_devices->device_list_mutex); /*
* It is safe to read the devices since the volume_mutex
* is held.
*/
list_for_each_entry(tmp, devices, dev_list) { list_for_each_entry(tmp, devices, dev_list) {
if (tmp->in_fs_metadata && !tmp->bdev) { if (tmp->in_fs_metadata && !tmp->bdev) {
device = tmp; device = tmp;
break; break;
} }
} }
mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
bdev = NULL; bdev = NULL;
bh = NULL; bh = NULL;
disk_super = NULL; disk_super = NULL;
...@@ -1271,8 +1308,11 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path) ...@@ -1271,8 +1308,11 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path)
} }
if (device->writeable) { if (device->writeable) {
lock_chunks(root);
list_del_init(&device->dev_alloc_list); list_del_init(&device->dev_alloc_list);
unlock_chunks(root);
root->fs_info->fs_devices->rw_devices--; root->fs_info->fs_devices->rw_devices--;
clear_super = true;
} }
ret = btrfs_shrink_device(device, 0); ret = btrfs_shrink_device(device, 0);
...@@ -1291,9 +1331,10 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path) ...@@ -1291,9 +1331,10 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path)
* the device list while someone else is writing out all * the device list while someone else is writing out all
* the device supers. * the device supers.
*/ */
cur_devices = device->fs_devices;
mutex_lock(&root->fs_info->fs_devices->device_list_mutex); mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
list_del_init(&device->dev_list); list_del_rcu(&device->dev_list);
mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
device->fs_devices->num_devices--; device->fs_devices->num_devices--;
...@@ -1307,34 +1348,36 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path) ...@@ -1307,34 +1348,36 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path)
if (device->bdev == root->fs_info->fs_devices->latest_bdev) if (device->bdev == root->fs_info->fs_devices->latest_bdev)
root->fs_info->fs_devices->latest_bdev = next_device->bdev; root->fs_info->fs_devices->latest_bdev = next_device->bdev;
if (device->bdev) { if (device->bdev)
blkdev_put(device->bdev, device->mode);
device->bdev = NULL;
device->fs_devices->open_devices--; device->fs_devices->open_devices--;
}
call_rcu(&device->rcu, free_device);
mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1; num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices); btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
if (device->fs_devices->open_devices == 0) { if (cur_devices->open_devices == 0) {
struct btrfs_fs_devices *fs_devices; struct btrfs_fs_devices *fs_devices;
fs_devices = root->fs_info->fs_devices; fs_devices = root->fs_info->fs_devices;
while (fs_devices) { while (fs_devices) {
if (fs_devices->seed == device->fs_devices) if (fs_devices->seed == cur_devices)
break; break;
fs_devices = fs_devices->seed; fs_devices = fs_devices->seed;
} }
fs_devices->seed = device->fs_devices->seed; fs_devices->seed = cur_devices->seed;
device->fs_devices->seed = NULL; cur_devices->seed = NULL;
__btrfs_close_devices(device->fs_devices); lock_chunks(root);
free_fs_devices(device->fs_devices); __btrfs_close_devices(cur_devices);
unlock_chunks(root);
free_fs_devices(cur_devices);
} }
/* /*
* at this point, the device is zero sized. We want to * at this point, the device is zero sized. We want to
* remove it from the devices list and zero out the old super * remove it from the devices list and zero out the old super
*/ */
if (device->writeable) { if (clear_super) {
/* make sure this device isn't detected as part of /* make sure this device isn't detected as part of
* the FS anymore * the FS anymore
*/ */
...@@ -1343,8 +1386,6 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path) ...@@ -1343,8 +1386,6 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path)
sync_dirty_buffer(bh); sync_dirty_buffer(bh);
} }
kfree(device->name);
kfree(device);
ret = 0; ret = 0;
error_brelse: error_brelse:
...@@ -1358,8 +1399,10 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path) ...@@ -1358,8 +1399,10 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path)
return ret; return ret;
error_undo: error_undo:
if (device->writeable) { if (device->writeable) {
lock_chunks(root);
list_add(&device->dev_alloc_list, list_add(&device->dev_alloc_list,
&root->fs_info->fs_devices->alloc_list); &root->fs_info->fs_devices->alloc_list);
unlock_chunks(root);
root->fs_info->fs_devices->rw_devices++; root->fs_info->fs_devices->rw_devices++;
} }
goto error_brelse; goto error_brelse;
...@@ -1399,7 +1442,12 @@ static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans, ...@@ -1399,7 +1442,12 @@ static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
INIT_LIST_HEAD(&seed_devices->devices); INIT_LIST_HEAD(&seed_devices->devices);
INIT_LIST_HEAD(&seed_devices->alloc_list); INIT_LIST_HEAD(&seed_devices->alloc_list);
mutex_init(&seed_devices->device_list_mutex); mutex_init(&seed_devices->device_list_mutex);
list_splice_init(&fs_devices->devices, &seed_devices->devices);
mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
synchronize_rcu);
mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list); list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
list_for_each_entry(device, &seed_devices->devices, dev_list) { list_for_each_entry(device, &seed_devices->devices, dev_list) {
device->fs_devices = seed_devices; device->fs_devices = seed_devices;
...@@ -1596,7 +1644,7 @@ int btrfs_init_new_device(struct btrfs_root *root, char *device_path) ...@@ -1596,7 +1644,7 @@ int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
* half setup * half setup
*/ */
mutex_lock(&root->fs_info->fs_devices->device_list_mutex); mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
list_add(&device->dev_list, &root->fs_info->fs_devices->devices); list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
list_add(&device->dev_alloc_list, list_add(&device->dev_alloc_list,
&root->fs_info->fs_devices->alloc_list); &root->fs_info->fs_devices->alloc_list);
root->fs_info->fs_devices->num_devices++; root->fs_info->fs_devices->num_devices++;
...@@ -1754,10 +1802,9 @@ static int btrfs_free_chunk(struct btrfs_trans_handle *trans, ...@@ -1754,10 +1802,9 @@ static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
BUG_ON(ret); BUG_ON(ret);
ret = btrfs_del_item(trans, root, path); ret = btrfs_del_item(trans, root, path);
BUG_ON(ret);
btrfs_free_path(path); btrfs_free_path(path);
return 0; return ret;
} }
static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64 static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
......
...@@ -89,6 +89,8 @@ struct btrfs_device { ...@@ -89,6 +89,8 @@ struct btrfs_device {
struct scrub_dev *scrub_device; struct scrub_dev *scrub_device;
struct btrfs_work work; struct btrfs_work work;
struct rcu_head rcu;
struct work_struct rcu_work;
}; };
struct btrfs_fs_devices { struct btrfs_fs_devices {
......
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