Commit a22285a6 authored by Yan, Zheng's avatar Yan, Zheng Committed by Chris Mason

Btrfs: Integrate metadata reservation with start_transaction

Besides simplify the code, this change makes sure all metadata
reservation for normal metadata operations are released after
committing transaction.

Changes since V1:

Add code that check if unlink and rmdir will free space.

Add ENOSPC handling for clone ioctl.
Signed-off-by: default avatarYan Zheng <zheng.yan@oracle.com>
Signed-off-by: default avatarChris Mason <chris.mason@oracle.com>
parent f0486c68
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
struct btrfs_trans_handle; struct btrfs_trans_handle;
struct btrfs_transaction; struct btrfs_transaction;
struct btrfs_pending_snapshot;
extern struct kmem_cache *btrfs_trans_handle_cachep; extern struct kmem_cache *btrfs_trans_handle_cachep;
extern struct kmem_cache *btrfs_transaction_cachep; extern struct kmem_cache *btrfs_transaction_cachep;
extern struct kmem_cache *btrfs_bit_radix_cachep; extern struct kmem_cache *btrfs_bit_radix_cachep;
...@@ -970,6 +971,7 @@ struct btrfs_fs_info { ...@@ -970,6 +971,7 @@ struct btrfs_fs_info {
int do_barriers; int do_barriers;
int closing; int closing;
int log_root_recovering; int log_root_recovering;
int enospc_unlink;
u64 total_pinned; u64 total_pinned;
...@@ -1995,6 +1997,9 @@ void btrfs_put_block_group(struct btrfs_block_group_cache *cache); ...@@ -1995,6 +1997,9 @@ void btrfs_put_block_group(struct btrfs_block_group_cache *cache);
int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
struct btrfs_root *root, unsigned long count); struct btrfs_root *root, unsigned long count);
int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len); int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len);
int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
struct btrfs_root *root, u64 bytenr,
u64 num_bytes, u64 *refs, u64 *flags);
int btrfs_pin_extent(struct btrfs_root *root, int btrfs_pin_extent(struct btrfs_root *root,
u64 bytenr, u64 num, int reserved); u64 bytenr, u64 num, int reserved);
int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans, int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
...@@ -2075,8 +2080,6 @@ u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags); ...@@ -2075,8 +2080,6 @@ u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags);
void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *ionde); void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *ionde);
void btrfs_clear_space_info_full(struct btrfs_fs_info *info); void btrfs_clear_space_info_full(struct btrfs_fs_info *info);
int btrfs_reserve_metadata_space(struct btrfs_root *root, int num_items);
int btrfs_unreserve_metadata_space(struct btrfs_root *root, int num_items);
int btrfs_unreserve_metadata_for_delalloc(struct btrfs_root *root, int btrfs_unreserve_metadata_for_delalloc(struct btrfs_root *root,
struct inode *inode, int num_items); struct inode *inode, int num_items);
int btrfs_reserve_metadata_for_delalloc(struct btrfs_root *root, int btrfs_reserve_metadata_for_delalloc(struct btrfs_root *root,
...@@ -2089,6 +2092,13 @@ void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode, ...@@ -2089,6 +2092,13 @@ void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode,
u64 bytes); u64 bytes);
void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode, void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
u64 bytes); u64 bytes);
int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans,
struct btrfs_root *root,
int num_items, int *retries);
void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
struct btrfs_root *root);
int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
struct btrfs_pending_snapshot *pending);
void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv); void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv);
struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root); struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root);
void btrfs_free_block_rsv(struct btrfs_root *root, void btrfs_free_block_rsv(struct btrfs_root *root,
...@@ -2296,6 +2306,12 @@ int btrfs_del_inode_ref(struct btrfs_trans_handle *trans, ...@@ -2296,6 +2306,12 @@ int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
struct btrfs_root *root, struct btrfs_root *root,
const char *name, int name_len, const char *name, int name_len,
u64 inode_objectid, u64 ref_objectid, u64 *index); u64 inode_objectid, u64 ref_objectid, u64 *index);
struct btrfs_inode_ref *
btrfs_lookup_inode_ref(struct btrfs_trans_handle *trans,
struct btrfs_root *root,
struct btrfs_path *path,
const char *name, int name_len,
u64 inode_objectid, u64 ref_objectid, int mod);
int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans, int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans,
struct btrfs_root *root, struct btrfs_root *root,
struct btrfs_path *path, u64 objectid); struct btrfs_path *path, u64 objectid);
......
...@@ -318,107 +318,6 @@ int btrfs_delayed_ref_pending(struct btrfs_trans_handle *trans, u64 bytenr) ...@@ -318,107 +318,6 @@ int btrfs_delayed_ref_pending(struct btrfs_trans_handle *trans, u64 bytenr)
return ret; return ret;
} }
/*
* helper function to lookup reference count and flags of extent.
*
* the head node for delayed ref is used to store the sum of all the
* reference count modifications queued up in the rbtree. the head
* node may also store the extent flags to set. This way you can check
* to see what the reference count and extent flags would be if all of
* the delayed refs are not processed.
*/
int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
struct btrfs_root *root, u64 bytenr,
u64 num_bytes, u64 *refs, u64 *flags)
{
struct btrfs_delayed_ref_node *ref;
struct btrfs_delayed_ref_head *head;
struct btrfs_delayed_ref_root *delayed_refs;
struct btrfs_path *path;
struct btrfs_extent_item *ei;
struct extent_buffer *leaf;
struct btrfs_key key;
u32 item_size;
u64 num_refs;
u64 extent_flags;
int ret;
path = btrfs_alloc_path();
if (!path)
return -ENOMEM;
key.objectid = bytenr;
key.type = BTRFS_EXTENT_ITEM_KEY;
key.offset = num_bytes;
delayed_refs = &trans->transaction->delayed_refs;
again:
ret = btrfs_search_slot(trans, root->fs_info->extent_root,
&key, path, 0, 0);
if (ret < 0)
goto out;
if (ret == 0) {
leaf = path->nodes[0];
item_size = btrfs_item_size_nr(leaf, path->slots[0]);
if (item_size >= sizeof(*ei)) {
ei = btrfs_item_ptr(leaf, path->slots[0],
struct btrfs_extent_item);
num_refs = btrfs_extent_refs(leaf, ei);
extent_flags = btrfs_extent_flags(leaf, ei);
} else {
#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
struct btrfs_extent_item_v0 *ei0;
BUG_ON(item_size != sizeof(*ei0));
ei0 = btrfs_item_ptr(leaf, path->slots[0],
struct btrfs_extent_item_v0);
num_refs = btrfs_extent_refs_v0(leaf, ei0);
/* FIXME: this isn't correct for data */
extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
#else
BUG();
#endif
}
BUG_ON(num_refs == 0);
} else {
num_refs = 0;
extent_flags = 0;
ret = 0;
}
spin_lock(&delayed_refs->lock);
ref = find_ref_head(&delayed_refs->root, bytenr, NULL);
if (ref) {
head = btrfs_delayed_node_to_head(ref);
if (!mutex_trylock(&head->mutex)) {
atomic_inc(&ref->refs);
spin_unlock(&delayed_refs->lock);
btrfs_release_path(root->fs_info->extent_root, path);
mutex_lock(&head->mutex);
mutex_unlock(&head->mutex);
btrfs_put_delayed_ref(ref);
goto again;
}
if (head->extent_op && head->extent_op->update_flags)
extent_flags |= head->extent_op->flags_to_set;
else
BUG_ON(num_refs == 0);
num_refs += ref->ref_mod;
mutex_unlock(&head->mutex);
}
WARN_ON(num_refs == 0);
if (refs)
*refs = num_refs;
if (flags)
*flags = extent_flags;
out:
spin_unlock(&delayed_refs->lock);
btrfs_free_path(path);
return ret;
}
/* /*
* helper function to update an extent delayed ref in the * helper function to update an extent delayed ref in the
* rbtree. existing and update must both have the same * rbtree. existing and update must both have the same
......
...@@ -167,9 +167,6 @@ int btrfs_add_delayed_extent_op(struct btrfs_trans_handle *trans, ...@@ -167,9 +167,6 @@ int btrfs_add_delayed_extent_op(struct btrfs_trans_handle *trans,
struct btrfs_delayed_ref_head * struct btrfs_delayed_ref_head *
btrfs_find_delayed_ref_head(struct btrfs_trans_handle *trans, u64 bytenr); btrfs_find_delayed_ref_head(struct btrfs_trans_handle *trans, u64 bytenr);
int btrfs_delayed_ref_pending(struct btrfs_trans_handle *trans, u64 bytenr); int btrfs_delayed_ref_pending(struct btrfs_trans_handle *trans, u64 bytenr);
int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
struct btrfs_root *root, u64 bytenr,
u64 num_bytes, u64 *refs, u64 *flags);
int btrfs_update_delayed_ref(struct btrfs_trans_handle *trans, int btrfs_update_delayed_ref(struct btrfs_trans_handle *trans,
u64 bytenr, u64 num_bytes, u64 orig_parent, u64 bytenr, u64 num_bytes, u64 orig_parent,
u64 parent, u64 orig_ref_root, u64 ref_root, u64 parent, u64 orig_ref_root, u64 ref_root,
......
...@@ -1522,7 +1522,7 @@ static int transaction_kthread(void *arg) ...@@ -1522,7 +1522,7 @@ static int transaction_kthread(void *arg)
goto sleep; goto sleep;
} }
mutex_unlock(&root->fs_info->trans_mutex); mutex_unlock(&root->fs_info->trans_mutex);
trans = btrfs_start_transaction(root, 1); trans = btrfs_join_transaction(root, 1);
ret = btrfs_commit_transaction(trans, root); ret = btrfs_commit_transaction(trans, root);
sleep: sleep:
...@@ -2409,11 +2409,11 @@ int btrfs_commit_super(struct btrfs_root *root) ...@@ -2409,11 +2409,11 @@ int btrfs_commit_super(struct btrfs_root *root)
down_write(&root->fs_info->cleanup_work_sem); down_write(&root->fs_info->cleanup_work_sem);
up_write(&root->fs_info->cleanup_work_sem); up_write(&root->fs_info->cleanup_work_sem);
trans = btrfs_start_transaction(root, 1); trans = btrfs_join_transaction(root, 1);
ret = btrfs_commit_transaction(trans, root); ret = btrfs_commit_transaction(trans, root);
BUG_ON(ret); BUG_ON(ret);
/* run commit again to drop the original snapshot */ /* run commit again to drop the original snapshot */
trans = btrfs_start_transaction(root, 1); trans = btrfs_join_transaction(root, 1);
btrfs_commit_transaction(trans, root); btrfs_commit_transaction(trans, root);
ret = btrfs_write_and_wait_transaction(NULL, root); ret = btrfs_write_and_wait_transaction(NULL, root);
BUG_ON(ret); BUG_ON(ret);
......
...@@ -615,6 +615,113 @@ int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len) ...@@ -615,6 +615,113 @@ int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
return ret; return ret;
} }
/*
* helper function to lookup reference count and flags of extent.
*
* the head node for delayed ref is used to store the sum of all the
* reference count modifications queued up in the rbtree. the head
* node may also store the extent flags to set. This way you can check
* to see what the reference count and extent flags would be if all of
* the delayed refs are not processed.
*/
int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
struct btrfs_root *root, u64 bytenr,
u64 num_bytes, u64 *refs, u64 *flags)
{
struct btrfs_delayed_ref_head *head;
struct btrfs_delayed_ref_root *delayed_refs;
struct btrfs_path *path;
struct btrfs_extent_item *ei;
struct extent_buffer *leaf;
struct btrfs_key key;
u32 item_size;
u64 num_refs;
u64 extent_flags;
int ret;
path = btrfs_alloc_path();
if (!path)
return -ENOMEM;
key.objectid = bytenr;
key.type = BTRFS_EXTENT_ITEM_KEY;
key.offset = num_bytes;
if (!trans) {
path->skip_locking = 1;
path->search_commit_root = 1;
}
again:
ret = btrfs_search_slot(trans, root->fs_info->extent_root,
&key, path, 0, 0);
if (ret < 0)
goto out_free;
if (ret == 0) {
leaf = path->nodes[0];
item_size = btrfs_item_size_nr(leaf, path->slots[0]);
if (item_size >= sizeof(*ei)) {
ei = btrfs_item_ptr(leaf, path->slots[0],
struct btrfs_extent_item);
num_refs = btrfs_extent_refs(leaf, ei);
extent_flags = btrfs_extent_flags(leaf, ei);
} else {
#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
struct btrfs_extent_item_v0 *ei0;
BUG_ON(item_size != sizeof(*ei0));
ei0 = btrfs_item_ptr(leaf, path->slots[0],
struct btrfs_extent_item_v0);
num_refs = btrfs_extent_refs_v0(leaf, ei0);
/* FIXME: this isn't correct for data */
extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
#else
BUG();
#endif
}
BUG_ON(num_refs == 0);
} else {
num_refs = 0;
extent_flags = 0;
ret = 0;
}
if (!trans)
goto out;
delayed_refs = &trans->transaction->delayed_refs;
spin_lock(&delayed_refs->lock);
head = btrfs_find_delayed_ref_head(trans, bytenr);
if (head) {
if (!mutex_trylock(&head->mutex)) {
atomic_inc(&head->node.refs);
spin_unlock(&delayed_refs->lock);
btrfs_release_path(root->fs_info->extent_root, path);
mutex_lock(&head->mutex);
mutex_unlock(&head->mutex);
btrfs_put_delayed_ref(&head->node);
goto again;
}
if (head->extent_op && head->extent_op->update_flags)
extent_flags |= head->extent_op->flags_to_set;
else
BUG_ON(num_refs == 0);
num_refs += head->node.ref_mod;
mutex_unlock(&head->mutex);
}
spin_unlock(&delayed_refs->lock);
out:
WARN_ON(num_refs == 0);
if (refs)
*refs = num_refs;
if (flags)
*flags = extent_flags;
out_free:
btrfs_free_path(path);
return ret;
}
/* /*
* Back reference rules. Back refs have three main goals: * Back reference rules. Back refs have three main goals:
* *
...@@ -2948,113 +3055,6 @@ int btrfs_reserve_metadata_for_delalloc(struct btrfs_root *root, ...@@ -2948,113 +3055,6 @@ int btrfs_reserve_metadata_for_delalloc(struct btrfs_root *root,
return 0; return 0;
} }
/*
* unreserve num_items number of items worth of metadata space. This needs to
* be paired with btrfs_reserve_metadata_space.
*
* NOTE: if you have the option, run this _AFTER_ you do a
* btrfs_end_transaction, since btrfs_end_transaction will run delayed ref
* oprations which will result in more used metadata, so we want to make sure we
* can do that without issue.
*/
int btrfs_unreserve_metadata_space(struct btrfs_root *root, int num_items)
{
struct btrfs_fs_info *info = root->fs_info;
struct btrfs_space_info *meta_sinfo;
u64 num_bytes;
u64 alloc_target;
bool bug = false;
/* get the space info for where the metadata will live */
alloc_target = btrfs_get_alloc_profile(root, 0);
meta_sinfo = __find_space_info(info, alloc_target);
num_bytes = calculate_bytes_needed(root, num_items);
spin_lock(&meta_sinfo->lock);
if (meta_sinfo->bytes_may_use < num_bytes) {
bug = true;
meta_sinfo->bytes_may_use = 0;
} else {
meta_sinfo->bytes_may_use -= num_bytes;
}
spin_unlock(&meta_sinfo->lock);
BUG_ON(bug);
return 0;
}
/*
* Reserve some metadata space for use. We'll calculate the worste case number
* of bytes that would be needed to modify num_items number of items. If we
* have space, fantastic, if not, you get -ENOSPC. Please call
* btrfs_unreserve_metadata_space when you are done for the _SAME_ number of
* items you reserved, since whatever metadata you needed should have already
* been allocated.
*
* This will commit the transaction to make more space if we don't have enough
* metadata space. THe only time we don't do this is if we're reserving space
* inside of a transaction, then we will just return -ENOSPC and it is the
* callers responsibility to handle it properly.
*/
int btrfs_reserve_metadata_space(struct btrfs_root *root, int num_items)
{
struct btrfs_fs_info *info = root->fs_info;
struct btrfs_space_info *meta_sinfo;
u64 num_bytes;
u64 used;
u64 alloc_target;
int retries = 0;
/* get the space info for where the metadata will live */
alloc_target = btrfs_get_alloc_profile(root, 0);
meta_sinfo = __find_space_info(info, alloc_target);
num_bytes = calculate_bytes_needed(root, num_items);
again:
spin_lock(&meta_sinfo->lock);
if (unlikely(!meta_sinfo->bytes_root))
meta_sinfo->bytes_root = calculate_bytes_needed(root, 6);
if (!retries)
meta_sinfo->bytes_may_use += num_bytes;
used = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
meta_sinfo->bytes_super + meta_sinfo->bytes_root +
meta_sinfo->bytes_may_use + meta_sinfo->bytes_delalloc;
if (used > meta_sinfo->total_bytes) {
retries++;
if (retries == 1) {
if (maybe_allocate_chunk(NULL, root, meta_sinfo,
num_bytes))
goto again;
retries++;
} else {
spin_unlock(&meta_sinfo->lock);
}
if (retries == 2) {
shrink_delalloc(NULL, root, meta_sinfo, num_bytes);
goto again;
}
spin_lock(&meta_sinfo->lock);
meta_sinfo->bytes_may_use -= num_bytes;
spin_unlock(&meta_sinfo->lock);
dump_space_info(meta_sinfo, 0, 0);
return -ENOSPC;
}
check_force_delalloc(meta_sinfo);
spin_unlock(&meta_sinfo->lock);
return 0;
}
/* /*
* This will check the space that the inode allocates from to make sure we have * This will check the space that the inode allocates from to make sure we have
* enough space for bytes. * enough space for bytes.
...@@ -3095,9 +3095,9 @@ int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode, ...@@ -3095,9 +3095,9 @@ int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
spin_unlock(&data_sinfo->lock); spin_unlock(&data_sinfo->lock);
alloc: alloc:
alloc_target = btrfs_get_alloc_profile(root, 1); alloc_target = btrfs_get_alloc_profile(root, 1);
trans = btrfs_start_transaction(root, 1); trans = btrfs_join_transaction(root, 1);
if (!trans) if (IS_ERR(trans))
return -ENOMEM; return PTR_ERR(trans);
ret = do_chunk_alloc(trans, root->fs_info->extent_root, ret = do_chunk_alloc(trans, root->fs_info->extent_root,
bytes + 2 * 1024 * 1024, bytes + 2 * 1024 * 1024,
...@@ -3118,8 +3118,8 @@ int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode, ...@@ -3118,8 +3118,8 @@ int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
if (!committed && !root->fs_info->open_ioctl_trans) { if (!committed && !root->fs_info->open_ioctl_trans) {
committed = 1; committed = 1;
trans = btrfs_join_transaction(root, 1); trans = btrfs_join_transaction(root, 1);
if (!trans) if (IS_ERR(trans))
return -ENOMEM; return PTR_ERR(trans);
ret = btrfs_commit_transaction(trans, root); ret = btrfs_commit_transaction(trans, root);
if (ret) if (ret)
return ret; return ret;
...@@ -3701,6 +3701,59 @@ static void init_global_block_rsv(struct btrfs_fs_info *fs_info) ...@@ -3701,6 +3701,59 @@ static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv; fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
} }
static u64 calc_trans_metadata_size(struct btrfs_root *root, int num_items)
{
return (root->leafsize + root->nodesize * (BTRFS_MAX_LEVEL - 1)) *
3 * num_items;
}
int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans,
struct btrfs_root *root,
int num_items, int *retries)
{
u64 num_bytes;
int ret;
if (num_items == 0 || root->fs_info->chunk_root == root)
return 0;
num_bytes = calc_trans_metadata_size(root, num_items);
ret = btrfs_block_rsv_add(trans, root, &root->fs_info->trans_block_rsv,
num_bytes, retries);
if (!ret) {
trans->bytes_reserved += num_bytes;
trans->block_rsv = &root->fs_info->trans_block_rsv;
}
return ret;
}
void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
struct btrfs_root *root)
{
if (!trans->bytes_reserved)
return;
BUG_ON(trans->block_rsv != &root->fs_info->trans_block_rsv);
btrfs_block_rsv_release(root, trans->block_rsv,
trans->bytes_reserved);
trans->bytes_reserved = 0;
}
int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
struct btrfs_pending_snapshot *pending)
{
struct btrfs_root *root = pending->root;
struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
/*
* two for root back/forward refs, two for directory entries
* and one for root of the snapshot.
*/
u64 num_bytes = calc_trans_metadata_size(root, 5);
dst_rsv->space_info = src_rsv->space_info;
return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
}
static int update_block_group(struct btrfs_trans_handle *trans, static int update_block_group(struct btrfs_trans_handle *trans,
struct btrfs_root *root, struct btrfs_root *root,
u64 bytenr, u64 num_bytes, int alloc) u64 bytenr, u64 num_bytes, int alloc)
...@@ -5824,7 +5877,7 @@ int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref) ...@@ -5824,7 +5877,7 @@ int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref)
wc = kzalloc(sizeof(*wc), GFP_NOFS); wc = kzalloc(sizeof(*wc), GFP_NOFS);
BUG_ON(!wc); BUG_ON(!wc);
trans = btrfs_start_transaction(tree_root, 1); trans = btrfs_start_transaction(tree_root, 0);
if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
level = btrfs_header_level(root->node); level = btrfs_header_level(root->node);
...@@ -5920,7 +5973,9 @@ int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref) ...@@ -5920,7 +5973,9 @@ int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref)
BUG_ON(ret); BUG_ON(ret);
btrfs_end_transaction(trans, tree_root); btrfs_end_transaction(trans, tree_root);
trans = btrfs_start_transaction(tree_root, 1); trans = btrfs_start_transaction(tree_root, 0);
if (IS_ERR(trans))
return PTR_ERR(trans);
} else { } else {
unsigned long update; unsigned long update;
update = trans->delayed_ref_updates; update = trans->delayed_ref_updates;
......
...@@ -126,8 +126,7 @@ static noinline int dirty_and_release_pages(struct btrfs_trans_handle *trans, ...@@ -126,8 +126,7 @@ static noinline int dirty_and_release_pages(struct btrfs_trans_handle *trans,
end_of_last_block = start_pos + num_bytes - 1; end_of_last_block = start_pos + num_bytes - 1;
err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block, err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block,
NULL); NULL);
if (err) BUG_ON(err);
return err;
for (i = 0; i < num_pages; i++) { for (i = 0; i < num_pages; i++) {
struct page *p = pages[i]; struct page *p = pages[i];
...@@ -142,7 +141,7 @@ static noinline int dirty_and_release_pages(struct btrfs_trans_handle *trans, ...@@ -142,7 +141,7 @@ static noinline int dirty_and_release_pages(struct btrfs_trans_handle *trans,
* at this time. * at this time.
*/ */
} }
return err; return 0;
} }
/* /*
...@@ -1008,7 +1007,7 @@ static ssize_t btrfs_file_write(struct file *file, const char __user *buf, ...@@ -1008,7 +1007,7 @@ static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
num_written = err; num_written = err;
if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) { if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
trans = btrfs_start_transaction(root, 1); trans = btrfs_start_transaction(root, 0);
ret = btrfs_log_dentry_safe(trans, root, ret = btrfs_log_dentry_safe(trans, root,
file->f_dentry); file->f_dentry);
if (ret == 0) { if (ret == 0) {
...@@ -1104,9 +1103,9 @@ int btrfs_sync_file(struct file *file, struct dentry *dentry, int datasync) ...@@ -1104,9 +1103,9 @@ int btrfs_sync_file(struct file *file, struct dentry *dentry, int datasync)
if (file && file->private_data) if (file && file->private_data)
btrfs_ioctl_trans_end(file); btrfs_ioctl_trans_end(file);
trans = btrfs_start_transaction(root, 1); trans = btrfs_start_transaction(root, 0);
if (!trans) { if (IS_ERR(trans)) {
ret = -ENOMEM; ret = PTR_ERR(trans);
goto out; goto out;
} }
......
...@@ -49,6 +49,33 @@ static int find_name_in_backref(struct btrfs_path *path, const char *name, ...@@ -49,6 +49,33 @@ static int find_name_in_backref(struct btrfs_path *path, const char *name,
return 0; return 0;
} }
struct btrfs_inode_ref *
btrfs_lookup_inode_ref(struct btrfs_trans_handle *trans,
struct btrfs_root *root,
struct btrfs_path *path,
const char *name, int name_len,
u64 inode_objectid, u64 ref_objectid, int mod)
{
struct btrfs_key key;
struct btrfs_inode_ref *ref;
int ins_len = mod < 0 ? -1 : 0;
int cow = mod != 0;
int ret;
key.objectid = inode_objectid;
key.type = BTRFS_INODE_REF_KEY;
key.offset = ref_objectid;
ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
if (ret < 0)
return ERR_PTR(ret);
if (ret > 0)
return NULL;
if (!find_name_in_backref(path, name, name_len, &ref))
return NULL;
return ref;
}
int btrfs_del_inode_ref(struct btrfs_trans_handle *trans, int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
struct btrfs_root *root, struct btrfs_root *root,
const char *name, int name_len, const char *name, int name_len,
......
This diff is collapsed.
...@@ -239,23 +239,19 @@ static noinline int create_subvol(struct btrfs_root *root, ...@@ -239,23 +239,19 @@ static noinline int create_subvol(struct btrfs_root *root,
u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
u64 index = 0; u64 index = 0;
ret = btrfs_find_free_objectid(NULL, root->fs_info->tree_root,
0, &objectid);
if (ret)
return ret;
/* /*
* 1 - inode item * 1 - inode item
* 2 - refs * 2 - refs
* 1 - root item * 1 - root item
* 2 - dir items * 2 - dir items
*/ */
ret = btrfs_reserve_metadata_space(root, 6); trans = btrfs_start_transaction(root, 6);
if (ret) if (IS_ERR(trans))
return ret; return PTR_ERR(trans);
trans = btrfs_start_transaction(root, 1);
BUG_ON(!trans);
ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
0, &objectid);
if (ret)
goto fail;
leaf = btrfs_alloc_free_block(trans, root, root->leafsize, leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
0, objectid, NULL, 0, 0, 0); 0, objectid, NULL, 0, 0, 0);
...@@ -345,13 +341,10 @@ static noinline int create_subvol(struct btrfs_root *root, ...@@ -345,13 +341,10 @@ static noinline int create_subvol(struct btrfs_root *root,
err = btrfs_commit_transaction(trans, root); err = btrfs_commit_transaction(trans, root);
if (err && !ret) if (err && !ret)
ret = err; ret = err;
btrfs_unreserve_metadata_space(root, 6);
return ret; return ret;
} }
static int create_snapshot(struct btrfs_root *root, struct dentry *dentry, static int create_snapshot(struct btrfs_root *root, struct dentry *dentry)
char *name, int namelen)
{ {
struct inode *inode; struct inode *inode;
struct btrfs_pending_snapshot *pending_snapshot; struct btrfs_pending_snapshot *pending_snapshot;
...@@ -361,40 +354,33 @@ static int create_snapshot(struct btrfs_root *root, struct dentry *dentry, ...@@ -361,40 +354,33 @@ static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
if (!root->ref_cows) if (!root->ref_cows)
return -EINVAL; return -EINVAL;
/*
* 1 - inode item
* 2 - refs
* 1 - root item
* 2 - dir items
*/
ret = btrfs_reserve_metadata_space(root, 6);
if (ret)
goto fail;
pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS); pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
if (!pending_snapshot) { if (!pending_snapshot)
ret = -ENOMEM; return -ENOMEM;
btrfs_unreserve_metadata_space(root, 6);
goto fail; btrfs_init_block_rsv(&pending_snapshot->block_rsv);
}
pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
if (!pending_snapshot->name) {
ret = -ENOMEM;
kfree(pending_snapshot);
btrfs_unreserve_metadata_space(root, 6);
goto fail;
}
memcpy(pending_snapshot->name, name, namelen);
pending_snapshot->name[namelen] = '\0';
pending_snapshot->dentry = dentry; pending_snapshot->dentry = dentry;
trans = btrfs_start_transaction(root, 1);
BUG_ON(!trans);
pending_snapshot->root = root; pending_snapshot->root = root;
trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
if (IS_ERR(trans)) {
ret = PTR_ERR(trans);
goto fail;
}
ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
BUG_ON(ret);
list_add(&pending_snapshot->list, list_add(&pending_snapshot->list,
&trans->transaction->pending_snapshots); &trans->transaction->pending_snapshots);
ret = btrfs_commit_transaction(trans, root); ret = btrfs_commit_transaction(trans, root->fs_info->extent_root);
BUG_ON(ret); BUG_ON(ret);
btrfs_unreserve_metadata_space(root, 6);
ret = pending_snapshot->error;
if (ret)
goto fail;
btrfs_orphan_cleanup(pending_snapshot->snap);
inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry); inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
if (IS_ERR(inode)) { if (IS_ERR(inode)) {
...@@ -405,6 +391,7 @@ static int create_snapshot(struct btrfs_root *root, struct dentry *dentry, ...@@ -405,6 +391,7 @@ static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
d_instantiate(dentry, inode); d_instantiate(dentry, inode);
ret = 0; ret = 0;
fail: fail:
kfree(pending_snapshot);
return ret; return ret;
} }
...@@ -456,8 +443,7 @@ static noinline int btrfs_mksubvol(struct path *parent, ...@@ -456,8 +443,7 @@ static noinline int btrfs_mksubvol(struct path *parent,
goto out_up_read; goto out_up_read;
if (snap_src) { if (snap_src) {
error = create_snapshot(snap_src, dentry, error = create_snapshot(snap_src, dentry);
name, namelen);
} else { } else {
error = create_subvol(BTRFS_I(dir)->root, dentry, error = create_subvol(BTRFS_I(dir)->root, dentry,
name, namelen); name, namelen);
...@@ -811,7 +797,7 @@ static noinline int btrfs_ioctl_resize(struct btrfs_root *root, ...@@ -811,7 +797,7 @@ static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
device->name, (unsigned long long)new_size); device->name, (unsigned long long)new_size);
if (new_size > old_size) { if (new_size > old_size) {
trans = btrfs_start_transaction(root, 1); trans = btrfs_start_transaction(root, 0);
ret = btrfs_grow_device(trans, device, new_size); ret = btrfs_grow_device(trans, device, new_size);
btrfs_commit_transaction(trans, root); btrfs_commit_transaction(trans, root);
} else { } else {
...@@ -1300,7 +1286,13 @@ static noinline int btrfs_ioctl_snap_destroy(struct file *file, ...@@ -1300,7 +1286,13 @@ static noinline int btrfs_ioctl_snap_destroy(struct file *file,
if (err) if (err)
goto out_up_write; goto out_up_write;
trans = btrfs_start_transaction(root, 1); trans = btrfs_start_transaction(root, 0);
if (IS_ERR(trans)) {
err = PTR_ERR(trans);
goto out;
}
trans->block_rsv = &root->fs_info->global_block_rsv;
ret = btrfs_unlink_subvol(trans, root, dir, ret = btrfs_unlink_subvol(trans, root, dir,
dest->root_key.objectid, dest->root_key.objectid,
dentry->d_name.name, dentry->d_name.name,
...@@ -1550,12 +1542,6 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, ...@@ -1550,12 +1542,6 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
btrfs_wait_ordered_range(src, off, off+len); btrfs_wait_ordered_range(src, off, off+len);
} }
trans = btrfs_start_transaction(root, 1);
BUG_ON(!trans);
/* punch hole in destination first */
btrfs_drop_extents(trans, inode, off, off + len, &hint_byte, 1);
/* clone data */ /* clone data */
key.objectid = src->i_ino; key.objectid = src->i_ino;
key.type = BTRFS_EXTENT_DATA_KEY; key.type = BTRFS_EXTENT_DATA_KEY;
...@@ -1566,7 +1552,7 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, ...@@ -1566,7 +1552,7 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
* note the key will change type as we walk through the * note the key will change type as we walk through the
* tree. * tree.
*/ */
ret = btrfs_search_slot(trans, root, &key, path, 0, 0); ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
if (ret < 0) if (ret < 0)
goto out; goto out;
...@@ -1629,12 +1615,31 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, ...@@ -1629,12 +1615,31 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
new_key.objectid = inode->i_ino; new_key.objectid = inode->i_ino;
new_key.offset = key.offset + destoff - off; new_key.offset = key.offset + destoff - off;
trans = btrfs_start_transaction(root, 1);
if (IS_ERR(trans)) {
ret = PTR_ERR(trans);
goto out;
}
if (type == BTRFS_FILE_EXTENT_REG || if (type == BTRFS_FILE_EXTENT_REG ||
type == BTRFS_FILE_EXTENT_PREALLOC) { type == BTRFS_FILE_EXTENT_PREALLOC) {
if (off > key.offset) {
datao += off - key.offset;
datal -= off - key.offset;
}
if (key.offset + datal > off + len)
datal = off + len - key.offset;
ret = btrfs_drop_extents(trans, inode,
new_key.offset,
new_key.offset + datal,
&hint_byte, 1);
BUG_ON(ret);
ret = btrfs_insert_empty_item(trans, root, path, ret = btrfs_insert_empty_item(trans, root, path,
&new_key, size); &new_key, size);
if (ret) BUG_ON(ret);
goto out;
leaf = path->nodes[0]; leaf = path->nodes[0];
slot = path->slots[0]; slot = path->slots[0];
...@@ -1645,14 +1650,6 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, ...@@ -1645,14 +1650,6 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
extent = btrfs_item_ptr(leaf, slot, extent = btrfs_item_ptr(leaf, slot,
struct btrfs_file_extent_item); struct btrfs_file_extent_item);
if (off > key.offset) {
datao += off - key.offset;
datal -= off - key.offset;
}
if (key.offset + datal > off + len)
datal = off + len - key.offset;
/* disko == 0 means it's a hole */ /* disko == 0 means it's a hole */
if (!disko) if (!disko)
datao = 0; datao = 0;
...@@ -1683,14 +1680,21 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, ...@@ -1683,14 +1680,21 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
if (comp && (skip || trim)) { if (comp && (skip || trim)) {
ret = -EINVAL; ret = -EINVAL;
btrfs_end_transaction(trans, root);
goto out; goto out;
} }
size -= skip + trim; size -= skip + trim;
datal -= skip + trim; datal -= skip + trim;
ret = btrfs_drop_extents(trans, inode,
new_key.offset,
new_key.offset + datal,
&hint_byte, 1);
BUG_ON(ret);
ret = btrfs_insert_empty_item(trans, root, path, ret = btrfs_insert_empty_item(trans, root, path,
&new_key, size); &new_key, size);
if (ret) BUG_ON(ret);
goto out;
if (skip) { if (skip) {
u32 start = u32 start =
...@@ -1708,8 +1712,17 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, ...@@ -1708,8 +1712,17 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
} }
btrfs_mark_buffer_dirty(leaf); btrfs_mark_buffer_dirty(leaf);
} btrfs_release_path(root, path);
inode->i_mtime = inode->i_ctime = CURRENT_TIME;
if (new_key.offset + datal > inode->i_size)
btrfs_i_size_write(inode,
new_key.offset + datal);
BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
ret = btrfs_update_inode(trans, root, inode);
BUG_ON(ret);
btrfs_end_transaction(trans, root);
}
next: next:
btrfs_release_path(root, path); btrfs_release_path(root, path);
key.offset++; key.offset++;
...@@ -1717,17 +1730,7 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, ...@@ -1717,17 +1730,7 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
ret = 0; ret = 0;
out: out:
btrfs_release_path(root, path); btrfs_release_path(root, path);
if (ret == 0) {
inode->i_mtime = inode->i_ctime = CURRENT_TIME;
if (destoff + olen > inode->i_size)
btrfs_i_size_write(inode, destoff + olen);
BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
ret = btrfs_update_inode(trans, root, inode);
}
btrfs_end_transaction(trans, root);
unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS); unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
if (ret)
vmtruncate(inode, 0);
out_unlock: out_unlock:
mutex_unlock(&src->i_mutex); mutex_unlock(&src->i_mutex);
mutex_unlock(&inode->i_mutex); mutex_unlock(&inode->i_mutex);
......
...@@ -1649,7 +1649,7 @@ static noinline_for_stack int merge_reloc_root(struct reloc_control *rc, ...@@ -1649,7 +1649,7 @@ static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
} }
if (level == 0 && rc->stage == UPDATE_DATA_PTRS) { if (level == 0 && rc->stage == UPDATE_DATA_PTRS) {
trans = btrfs_start_transaction(root, 1); trans = btrfs_start_transaction(root, 0);
leaf = path->nodes[0]; leaf = path->nodes[0];
btrfs_item_key_to_cpu(leaf, &key, 0); btrfs_item_key_to_cpu(leaf, &key, 0);
...@@ -1675,7 +1675,7 @@ static noinline_for_stack int merge_reloc_root(struct reloc_control *rc, ...@@ -1675,7 +1675,7 @@ static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
while (1) { while (1) {
leaf = NULL; leaf = NULL;
replaced = 0; replaced = 0;
trans = btrfs_start_transaction(root, 1); trans = btrfs_start_transaction(root, 0);
max_level = level; max_level = level;
ret = walk_down_reloc_tree(reloc_root, path, &level); ret = walk_down_reloc_tree(reloc_root, path, &level);
...@@ -1803,7 +1803,7 @@ static void merge_func(struct btrfs_work *work) ...@@ -1803,7 +1803,7 @@ static void merge_func(struct btrfs_work *work)
merge_reloc_root(async->rc, root); merge_reloc_root(async->rc, root);
trans = btrfs_start_transaction(root, 1); trans = btrfs_start_transaction(root, 0);
btrfs_update_reloc_root(trans, root); btrfs_update_reloc_root(trans, root);
btrfs_end_transaction(trans, root); btrfs_end_transaction(trans, root);
} }
...@@ -3297,11 +3297,11 @@ static noinline_for_stack int relocate_block_group(struct reloc_control *rc) ...@@ -3297,11 +3297,11 @@ static noinline_for_stack int relocate_block_group(struct reloc_control *rc)
rc->create_reloc_root = 1; rc->create_reloc_root = 1;
set_reloc_control(rc); set_reloc_control(rc);
trans = btrfs_start_transaction(rc->extent_root, 1); trans = btrfs_join_transaction(rc->extent_root, 1);
btrfs_commit_transaction(trans, rc->extent_root); btrfs_commit_transaction(trans, rc->extent_root);
while (1) { while (1) {
trans = btrfs_start_transaction(rc->extent_root, 1); trans = btrfs_start_transaction(rc->extent_root, 0);
ret = find_next_extent(trans, rc, path); ret = find_next_extent(trans, rc, path);
if (ret < 0) if (ret < 0)
...@@ -3411,7 +3411,7 @@ static noinline_for_stack int relocate_block_group(struct reloc_control *rc) ...@@ -3411,7 +3411,7 @@ static noinline_for_stack int relocate_block_group(struct reloc_control *rc)
smp_mb(); smp_mb();
if (rc->extents_found > 0) { if (rc->extents_found > 0) {
trans = btrfs_start_transaction(rc->extent_root, 1); trans = btrfs_join_transaction(rc->extent_root, 1);
btrfs_commit_transaction(trans, rc->extent_root); btrfs_commit_transaction(trans, rc->extent_root);
} }
...@@ -3420,7 +3420,7 @@ static noinline_for_stack int relocate_block_group(struct reloc_control *rc) ...@@ -3420,7 +3420,7 @@ static noinline_for_stack int relocate_block_group(struct reloc_control *rc)
unset_reloc_control(rc); unset_reloc_control(rc);
/* get rid of pinned extents */ /* get rid of pinned extents */
trans = btrfs_start_transaction(rc->extent_root, 1); trans = btrfs_join_transaction(rc->extent_root, 1);
btrfs_commit_transaction(trans, rc->extent_root); btrfs_commit_transaction(trans, rc->extent_root);
return err; return err;
...@@ -3475,7 +3475,7 @@ static struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info, ...@@ -3475,7 +3475,7 @@ static struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
if (IS_ERR(root)) if (IS_ERR(root))
return ERR_CAST(root); return ERR_CAST(root);
trans = btrfs_start_transaction(root, 1); trans = btrfs_start_transaction(root, 6);
BUG_ON(!trans); BUG_ON(!trans);
err = btrfs_find_free_objectid(trans, root, objectid, &objectid); err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
...@@ -3619,7 +3619,7 @@ static noinline_for_stack int mark_garbage_root(struct btrfs_root *root) ...@@ -3619,7 +3619,7 @@ static noinline_for_stack int mark_garbage_root(struct btrfs_root *root)
struct btrfs_trans_handle *trans; struct btrfs_trans_handle *trans;
int ret; int ret;
trans = btrfs_start_transaction(root->fs_info->tree_root, 1); trans = btrfs_start_transaction(root->fs_info->tree_root, 0);
memset(&root->root_item.drop_progress, 0, memset(&root->root_item.drop_progress, 0,
sizeof(root->root_item.drop_progress)); sizeof(root->root_item.drop_progress));
......
...@@ -498,7 +498,7 @@ int btrfs_sync_fs(struct super_block *sb, int wait) ...@@ -498,7 +498,7 @@ int btrfs_sync_fs(struct super_block *sb, int wait)
btrfs_start_delalloc_inodes(root, 0); btrfs_start_delalloc_inodes(root, 0);
btrfs_wait_ordered_extents(root, 0, 0); btrfs_wait_ordered_extents(root, 0, 0);
trans = btrfs_start_transaction(root, 1); trans = btrfs_start_transaction(root, 0);
ret = btrfs_commit_transaction(trans, root); ret = btrfs_commit_transaction(trans, root);
return ret; return ret;
} }
......
...@@ -165,53 +165,89 @@ enum btrfs_trans_type { ...@@ -165,53 +165,89 @@ enum btrfs_trans_type {
TRANS_USERSPACE, TRANS_USERSPACE,
}; };
static int may_wait_transaction(struct btrfs_root *root, int type)
{
if (!root->fs_info->log_root_recovering &&
((type == TRANS_START && !root->fs_info->open_ioctl_trans) ||
type == TRANS_USERSPACE))
return 1;
return 0;
}
static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root, static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
int num_blocks, int type) u64 num_items, int type)
{ {
struct btrfs_trans_handle *h = struct btrfs_trans_handle *h;
kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); struct btrfs_transaction *cur_trans;
int retries = 0;
int ret; int ret;
again:
h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
if (!h)
return ERR_PTR(-ENOMEM);
mutex_lock(&root->fs_info->trans_mutex); mutex_lock(&root->fs_info->trans_mutex);
if (!root->fs_info->log_root_recovering && if (may_wait_transaction(root, type))
((type == TRANS_START && !root->fs_info->open_ioctl_trans) ||
type == TRANS_USERSPACE))
wait_current_trans(root); wait_current_trans(root);
ret = join_transaction(root); ret = join_transaction(root);
BUG_ON(ret); BUG_ON(ret);
h->transid = root->fs_info->running_transaction->transid; cur_trans = root->fs_info->running_transaction;
h->transaction = root->fs_info->running_transaction; cur_trans->use_count++;
h->blocks_reserved = num_blocks; mutex_unlock(&root->fs_info->trans_mutex);
h->transid = cur_trans->transid;
h->transaction = cur_trans;
h->blocks_used = 0; h->blocks_used = 0;
h->block_group = 0; h->block_group = 0;
h->bytes_reserved = 0;
h->delayed_ref_updates = 0; h->delayed_ref_updates = 0;
h->block_rsv = NULL; h->block_rsv = NULL;
if (!current->journal_info && type != TRANS_USERSPACE) smp_mb();
current->journal_info = h; if (cur_trans->blocked && may_wait_transaction(root, type)) {
btrfs_commit_transaction(h, root);
goto again;
}
if (num_items > 0) {
ret = btrfs_trans_reserve_metadata(h, root, num_items,
&retries);
if (ret == -EAGAIN) {
btrfs_commit_transaction(h, root);
goto again;
}
if (ret < 0) {
btrfs_end_transaction(h, root);
return ERR_PTR(ret);
}
}
root->fs_info->running_transaction->use_count++; mutex_lock(&root->fs_info->trans_mutex);
record_root_in_trans(h, root); record_root_in_trans(h, root);
mutex_unlock(&root->fs_info->trans_mutex); mutex_unlock(&root->fs_info->trans_mutex);
if (!current->journal_info && type != TRANS_USERSPACE)
current->journal_info = h;
return h; return h;
} }
struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
int num_blocks) int num_items)
{ {
return start_transaction(root, num_blocks, TRANS_START); return start_transaction(root, num_items, TRANS_START);
} }
struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root, struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
int num_blocks) int num_blocks)
{ {
return start_transaction(root, num_blocks, TRANS_JOIN); return start_transaction(root, 0, TRANS_JOIN);
} }
struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r, struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
int num_blocks) int num_blocks)
{ {
return start_transaction(r, num_blocks, TRANS_USERSPACE); return start_transaction(r, 0, TRANS_USERSPACE);
} }
/* wait for a transaction commit to be fully complete */ /* wait for a transaction commit to be fully complete */
...@@ -312,6 +348,8 @@ static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, ...@@ -312,6 +348,8 @@ static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
count++; count++;
} }
btrfs_trans_release_metadata(trans, root);
mutex_lock(&info->trans_mutex); mutex_lock(&info->trans_mutex);
cur_trans = info->running_transaction; cur_trans = info->running_transaction;
WARN_ON(cur_trans != trans->transaction); WARN_ON(cur_trans != trans->transaction);
...@@ -757,47 +795,49 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, ...@@ -757,47 +795,49 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
struct btrfs_root *root = pending->root; struct btrfs_root *root = pending->root;
struct btrfs_root *parent_root; struct btrfs_root *parent_root;
struct inode *parent_inode; struct inode *parent_inode;
struct dentry *dentry;
struct extent_buffer *tmp; struct extent_buffer *tmp;
struct extent_buffer *old; struct extent_buffer *old;
int ret; int ret;
u64 objectid;
int namelen;
u64 index = 0; u64 index = 0;
u64 objectid;
parent_inode = pending->dentry->d_parent->d_inode;
parent_root = BTRFS_I(parent_inode)->root;
new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS); new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
if (!new_root_item) { if (!new_root_item) {
ret = -ENOMEM; pending->error = -ENOMEM;
goto fail; goto fail;
} }
ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid); ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
if (ret) if (ret) {
pending->error = ret;
goto fail; goto fail;
}
key.objectid = objectid; key.objectid = objectid;
/* record when the snapshot was created in key.offset */ key.offset = (u64)-1;
key.offset = trans->transid; key.type = BTRFS_ROOT_ITEM_KEY;
btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
memcpy(&pending->root_key, &key, sizeof(key)); trans->block_rsv = &pending->block_rsv;
pending->root_key.offset = (u64)-1;
dentry = pending->dentry;
parent_inode = dentry->d_parent->d_inode;
parent_root = BTRFS_I(parent_inode)->root;
record_root_in_trans(trans, parent_root); record_root_in_trans(trans, parent_root);
/* /*
* insert the directory item * insert the directory item
*/ */
namelen = strlen(pending->name);
ret = btrfs_set_inode_index(parent_inode, &index); ret = btrfs_set_inode_index(parent_inode, &index);
BUG_ON(ret); BUG_ON(ret);
ret = btrfs_insert_dir_item(trans, parent_root, ret = btrfs_insert_dir_item(trans, parent_root,
pending->name, namelen, dentry->d_name.name, dentry->d_name.len,
parent_inode->i_ino, parent_inode->i_ino, &key,
&pending->root_key, BTRFS_FT_DIR, index); BTRFS_FT_DIR, index);
BUG_ON(ret); BUG_ON(ret);
btrfs_i_size_write(parent_inode, parent_inode->i_size + namelen * 2); btrfs_i_size_write(parent_inode, parent_inode->i_size +
dentry->d_name.len * 2);
ret = btrfs_update_inode(trans, parent_root, parent_inode); ret = btrfs_update_inode(trans, parent_root, parent_inode);
BUG_ON(ret); BUG_ON(ret);
...@@ -814,22 +854,29 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, ...@@ -814,22 +854,29 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
free_extent_buffer(old); free_extent_buffer(old);
btrfs_set_root_node(new_root_item, tmp); btrfs_set_root_node(new_root_item, tmp);
ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, /* record when the snapshot was created in key.offset */
new_root_item); key.offset = trans->transid;
BUG_ON(ret); ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
btrfs_tree_unlock(tmp); btrfs_tree_unlock(tmp);
free_extent_buffer(tmp); free_extent_buffer(tmp);
BUG_ON(ret);
ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root, /*
pending->root_key.objectid, * insert root back/forward references
*/
ret = btrfs_add_root_ref(trans, tree_root, objectid,
parent_root->root_key.objectid, parent_root->root_key.objectid,
parent_inode->i_ino, index, pending->name, parent_inode->i_ino, index,
namelen); dentry->d_name.name, dentry->d_name.len);
BUG_ON(ret); BUG_ON(ret);
key.offset = (u64)-1;
pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
BUG_ON(IS_ERR(pending->snap));
fail: fail:
kfree(new_root_item); kfree(new_root_item);
return ret; btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1);
return 0;
} }
/* /*
...@@ -898,6 +945,8 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans, ...@@ -898,6 +945,8 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
ret = btrfs_run_delayed_refs(trans, root, 0); ret = btrfs_run_delayed_refs(trans, root, 0);
BUG_ON(ret); BUG_ON(ret);
btrfs_trans_release_metadata(trans, root);
cur_trans = trans->transaction; cur_trans = trans->transaction;
/* /*
* set the flushing flag so procs in this transaction have to * set the flushing flag so procs in this transaction have to
......
...@@ -57,8 +57,11 @@ struct btrfs_trans_handle { ...@@ -57,8 +57,11 @@ struct btrfs_trans_handle {
struct btrfs_pending_snapshot { struct btrfs_pending_snapshot {
struct dentry *dentry; struct dentry *dentry;
struct btrfs_root *root; struct btrfs_root *root;
char *name; struct btrfs_root *snap;
struct btrfs_key root_key; /* block reservation for the operation */
struct btrfs_block_rsv block_rsv;
/* extra metadata reseration for relocation */
int error;
struct list_head list; struct list_head list;
}; };
...@@ -85,7 +88,7 @@ static inline void btrfs_set_inode_last_trans(struct btrfs_trans_handle *trans, ...@@ -85,7 +88,7 @@ static inline void btrfs_set_inode_last_trans(struct btrfs_trans_handle *trans,
int btrfs_end_transaction(struct btrfs_trans_handle *trans, int btrfs_end_transaction(struct btrfs_trans_handle *trans,
struct btrfs_root *root); struct btrfs_root *root);
struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
int num_blocks); int num_items);
struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root, struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
int num_blocks); int num_blocks);
struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r, struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
......
...@@ -1097,7 +1097,7 @@ static int btrfs_rm_dev_item(struct btrfs_root *root, ...@@ -1097,7 +1097,7 @@ static int btrfs_rm_dev_item(struct btrfs_root *root,
if (!path) if (!path)
return -ENOMEM; return -ENOMEM;
trans = btrfs_start_transaction(root, 1); trans = btrfs_start_transaction(root, 0);
key.objectid = BTRFS_DEV_ITEMS_OBJECTID; key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
key.type = BTRFS_DEV_ITEM_KEY; key.type = BTRFS_DEV_ITEM_KEY;
key.offset = device->devid; key.offset = device->devid;
...@@ -1486,7 +1486,7 @@ int btrfs_init_new_device(struct btrfs_root *root, char *device_path) ...@@ -1486,7 +1486,7 @@ int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
goto error; goto error;
} }
trans = btrfs_start_transaction(root, 1); trans = btrfs_start_transaction(root, 0);
lock_chunks(root); lock_chunks(root);
device->barriers = 1; device->barriers = 1;
...@@ -1751,9 +1751,10 @@ static int btrfs_relocate_chunk(struct btrfs_root *root, ...@@ -1751,9 +1751,10 @@ static int btrfs_relocate_chunk(struct btrfs_root *root,
/* step one, relocate all the extents inside this chunk */ /* step one, relocate all the extents inside this chunk */
ret = btrfs_relocate_block_group(extent_root, chunk_offset); ret = btrfs_relocate_block_group(extent_root, chunk_offset);
BUG_ON(ret); if (ret)
return ret;
trans = btrfs_start_transaction(root, 1); trans = btrfs_start_transaction(root, 0);
BUG_ON(!trans); BUG_ON(!trans);
lock_chunks(root); lock_chunks(root);
...@@ -1925,7 +1926,7 @@ int btrfs_balance(struct btrfs_root *dev_root) ...@@ -1925,7 +1926,7 @@ int btrfs_balance(struct btrfs_root *dev_root)
break; break;
BUG_ON(ret); BUG_ON(ret);
trans = btrfs_start_transaction(dev_root, 1); trans = btrfs_start_transaction(dev_root, 0);
BUG_ON(!trans); BUG_ON(!trans);
ret = btrfs_grow_device(trans, device, old_size); ret = btrfs_grow_device(trans, device, old_size);
...@@ -2094,11 +2095,7 @@ int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) ...@@ -2094,11 +2095,7 @@ int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
} }
/* Shrinking succeeded, else we would be at "done". */ /* Shrinking succeeded, else we would be at "done". */
trans = btrfs_start_transaction(root, 1); trans = btrfs_start_transaction(root, 0);
if (!trans) {
ret = -ENOMEM;
goto done;
}
lock_chunks(root); lock_chunks(root);
device->disk_total_bytes = new_size; device->disk_total_bytes = new_size;
......
...@@ -154,15 +154,10 @@ int __btrfs_setxattr(struct btrfs_trans_handle *trans, ...@@ -154,15 +154,10 @@ int __btrfs_setxattr(struct btrfs_trans_handle *trans,
if (trans) if (trans)
return do_setxattr(trans, inode, name, value, size, flags); return do_setxattr(trans, inode, name, value, size, flags);
ret = btrfs_reserve_metadata_space(root, 2); trans = btrfs_start_transaction(root, 2);
if (ret) if (IS_ERR(trans))
return ret; return PTR_ERR(trans);
trans = btrfs_start_transaction(root, 1);
if (!trans) {
ret = -ENOMEM;
goto out;
}
btrfs_set_trans_block_group(trans, inode); btrfs_set_trans_block_group(trans, inode);
ret = do_setxattr(trans, inode, name, value, size, flags); ret = do_setxattr(trans, inode, name, value, size, flags);
...@@ -174,7 +169,6 @@ int __btrfs_setxattr(struct btrfs_trans_handle *trans, ...@@ -174,7 +169,6 @@ int __btrfs_setxattr(struct btrfs_trans_handle *trans,
BUG_ON(ret); BUG_ON(ret);
out: out:
btrfs_end_transaction_throttle(trans, root); btrfs_end_transaction_throttle(trans, root);
btrfs_unreserve_metadata_space(root, 2);
return ret; return ret;
} }
......
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