aboutsummaryrefslogtreecommitdiffstats
path: root/fs
diff options
context:
space:
mode:
authorYan, Zheng <zheng.yan@oracle.com>2010-05-16 10:48:46 -0400
committerChris Mason <chris.mason@oracle.com>2010-05-25 10:34:50 -0400
commita22285a6a32390195235171b89d157ed1a1fe932 (patch)
tree3fabc88a029e1af4f2fdcc708e7b62ef3cf3703a /fs
parentf0486c68e4bd9a06a5904d3eeb3a0d73a83befb8 (diff)
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: Yan Zheng <zheng.yan@oracle.com> Signed-off-by: Chris Mason <chris.mason@oracle.com>
Diffstat (limited to 'fs')
-rw-r--r--fs/btrfs/ctree.h20
-rw-r--r--fs/btrfs/delayed-ref.c101
-rw-r--r--fs/btrfs/delayed-ref.h3
-rw-r--r--fs/btrfs/disk-io.c6
-rw-r--r--fs/btrfs/extent-tree.c283
-rw-r--r--fs/btrfs/file.c13
-rw-r--r--fs/btrfs/inode-item.c27
-rw-r--r--fs/btrfs/inode.c403
-rw-r--r--fs/btrfs/ioctl.c157
-rw-r--r--fs/btrfs/relocation.c18
-rw-r--r--fs/btrfs/super.c2
-rw-r--r--fs/btrfs/transaction.c131
-rw-r--r--fs/btrfs/transaction.h13
-rw-r--r--fs/btrfs/volumes.c17
-rw-r--r--fs/btrfs/xattr.c12
15 files changed, 678 insertions, 528 deletions
diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index 7d2479694a58..e0aa9fb563e2 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -34,6 +34,7 @@
34 34
35struct btrfs_trans_handle; 35struct btrfs_trans_handle;
36struct btrfs_transaction; 36struct btrfs_transaction;
37struct btrfs_pending_snapshot;
37extern struct kmem_cache *btrfs_trans_handle_cachep; 38extern struct kmem_cache *btrfs_trans_handle_cachep;
38extern struct kmem_cache *btrfs_transaction_cachep; 39extern struct kmem_cache *btrfs_transaction_cachep;
39extern struct kmem_cache *btrfs_bit_radix_cachep; 40extern struct kmem_cache *btrfs_bit_radix_cachep;
@@ -970,6 +971,7 @@ struct btrfs_fs_info {
970 int do_barriers; 971 int do_barriers;
971 int closing; 972 int closing;
972 int log_root_recovering; 973 int log_root_recovering;
974 int enospc_unlink;
973 975
974 u64 total_pinned; 976 u64 total_pinned;
975 977
@@ -1995,6 +1997,9 @@ void btrfs_put_block_group(struct btrfs_block_group_cache *cache);
1995int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, 1997int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
1996 struct btrfs_root *root, unsigned long count); 1998 struct btrfs_root *root, unsigned long count);
1997int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len); 1999int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len);
2000int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
2001 struct btrfs_root *root, u64 bytenr,
2002 u64 num_bytes, u64 *refs, u64 *flags);
1998int btrfs_pin_extent(struct btrfs_root *root, 2003int btrfs_pin_extent(struct btrfs_root *root,
1999 u64 bytenr, u64 num, int reserved); 2004 u64 bytenr, u64 num, int reserved);
2000int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans, 2005int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
@@ -2075,8 +2080,6 @@ u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags);
2075void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *ionde); 2080void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *ionde);
2076void btrfs_clear_space_info_full(struct btrfs_fs_info *info); 2081void btrfs_clear_space_info_full(struct btrfs_fs_info *info);
2077 2082
2078int btrfs_reserve_metadata_space(struct btrfs_root *root, int num_items);
2079int btrfs_unreserve_metadata_space(struct btrfs_root *root, int num_items);
2080int btrfs_unreserve_metadata_for_delalloc(struct btrfs_root *root, 2083int btrfs_unreserve_metadata_for_delalloc(struct btrfs_root *root,
2081 struct inode *inode, int num_items); 2084 struct inode *inode, int num_items);
2082int btrfs_reserve_metadata_for_delalloc(struct btrfs_root *root, 2085int 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 u64 bytes); 2092 u64 bytes);
2090void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode, 2093void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
2091 u64 bytes); 2094 u64 bytes);
2095int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans,
2096 struct btrfs_root *root,
2097 int num_items, int *retries);
2098void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
2099 struct btrfs_root *root);
2100int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
2101 struct btrfs_pending_snapshot *pending);
2092void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv); 2102void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv);
2093struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root); 2103struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root);
2094void btrfs_free_block_rsv(struct btrfs_root *root, 2104void btrfs_free_block_rsv(struct btrfs_root *root,
@@ -2296,6 +2306,12 @@ int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
2296 struct btrfs_root *root, 2306 struct btrfs_root *root,
2297 const char *name, int name_len, 2307 const char *name, int name_len,
2298 u64 inode_objectid, u64 ref_objectid, u64 *index); 2308 u64 inode_objectid, u64 ref_objectid, u64 *index);
2309struct btrfs_inode_ref *
2310btrfs_lookup_inode_ref(struct btrfs_trans_handle *trans,
2311 struct btrfs_root *root,
2312 struct btrfs_path *path,
2313 const char *name, int name_len,
2314 u64 inode_objectid, u64 ref_objectid, int mod);
2299int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans, 2315int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans,
2300 struct btrfs_root *root, 2316 struct btrfs_root *root,
2301 struct btrfs_path *path, u64 objectid); 2317 struct btrfs_path *path, u64 objectid);
diff --git a/fs/btrfs/delayed-ref.c b/fs/btrfs/delayed-ref.c
index 902ce507c4e3..e807b143b857 100644
--- a/fs/btrfs/delayed-ref.c
+++ b/fs/btrfs/delayed-ref.c
@@ -319,107 +319,6 @@ out:
319} 319}
320 320
321/* 321/*
322 * helper function to lookup reference count and flags of extent.
323 *
324 * the head node for delayed ref is used to store the sum of all the
325 * reference count modifications queued up in the rbtree. the head
326 * node may also store the extent flags to set. This way you can check
327 * to see what the reference count and extent flags would be if all of
328 * the delayed refs are not processed.
329 */
330int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
331 struct btrfs_root *root, u64 bytenr,
332 u64 num_bytes, u64 *refs, u64 *flags)
333{
334 struct btrfs_delayed_ref_node *ref;
335 struct btrfs_delayed_ref_head *head;
336 struct btrfs_delayed_ref_root *delayed_refs;
337 struct btrfs_path *path;
338 struct btrfs_extent_item *ei;
339 struct extent_buffer *leaf;
340 struct btrfs_key key;
341 u32 item_size;
342 u64 num_refs;
343 u64 extent_flags;
344 int ret;
345
346 path = btrfs_alloc_path();
347 if (!path)
348 return -ENOMEM;
349
350 key.objectid = bytenr;
351 key.type = BTRFS_EXTENT_ITEM_KEY;
352 key.offset = num_bytes;
353 delayed_refs = &trans->transaction->delayed_refs;
354again:
355 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
356 &key, path, 0, 0);
357 if (ret < 0)
358 goto out;
359
360 if (ret == 0) {
361 leaf = path->nodes[0];
362 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
363 if (item_size >= sizeof(*ei)) {
364 ei = btrfs_item_ptr(leaf, path->slots[0],
365 struct btrfs_extent_item);
366 num_refs = btrfs_extent_refs(leaf, ei);
367 extent_flags = btrfs_extent_flags(leaf, ei);
368 } else {
369#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
370 struct btrfs_extent_item_v0 *ei0;
371 BUG_ON(item_size != sizeof(*ei0));
372 ei0 = btrfs_item_ptr(leaf, path->slots[0],
373 struct btrfs_extent_item_v0);
374 num_refs = btrfs_extent_refs_v0(leaf, ei0);
375 /* FIXME: this isn't correct for data */
376 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
377#else
378 BUG();
379#endif
380 }
381 BUG_ON(num_refs == 0);
382 } else {
383 num_refs = 0;
384 extent_flags = 0;
385 ret = 0;
386 }
387
388 spin_lock(&delayed_refs->lock);
389 ref = find_ref_head(&delayed_refs->root, bytenr, NULL);
390 if (ref) {
391 head = btrfs_delayed_node_to_head(ref);
392 if (!mutex_trylock(&head->mutex)) {
393 atomic_inc(&ref->refs);
394 spin_unlock(&delayed_refs->lock);
395
396 btrfs_release_path(root->fs_info->extent_root, path);
397
398 mutex_lock(&head->mutex);
399 mutex_unlock(&head->mutex);
400 btrfs_put_delayed_ref(ref);
401 goto again;
402 }
403 if (head->extent_op && head->extent_op->update_flags)
404 extent_flags |= head->extent_op->flags_to_set;
405 else
406 BUG_ON(num_refs == 0);
407
408 num_refs += ref->ref_mod;
409 mutex_unlock(&head->mutex);
410 }
411 WARN_ON(num_refs == 0);
412 if (refs)
413 *refs = num_refs;
414 if (flags)
415 *flags = extent_flags;
416out:
417 spin_unlock(&delayed_refs->lock);
418 btrfs_free_path(path);
419 return ret;
420}
421
422/*
423 * helper function to update an extent delayed ref in the 322 * helper function to update an extent delayed ref in the
424 * rbtree. existing and update must both have the same 323 * rbtree. existing and update must both have the same
425 * bytenr and parent 324 * bytenr and parent
diff --git a/fs/btrfs/delayed-ref.h b/fs/btrfs/delayed-ref.h
index f6fc67ddad36..50e3cf92fbda 100644
--- a/fs/btrfs/delayed-ref.h
+++ b/fs/btrfs/delayed-ref.h
@@ -167,9 +167,6 @@ int btrfs_add_delayed_extent_op(struct btrfs_trans_handle *trans,
167struct btrfs_delayed_ref_head * 167struct btrfs_delayed_ref_head *
168btrfs_find_delayed_ref_head(struct btrfs_trans_handle *trans, u64 bytenr); 168btrfs_find_delayed_ref_head(struct btrfs_trans_handle *trans, u64 bytenr);
169int btrfs_delayed_ref_pending(struct btrfs_trans_handle *trans, u64 bytenr); 169int btrfs_delayed_ref_pending(struct btrfs_trans_handle *trans, u64 bytenr);
170int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
171 struct btrfs_root *root, u64 bytenr,
172 u64 num_bytes, u64 *refs, u64 *flags);
173int btrfs_update_delayed_ref(struct btrfs_trans_handle *trans, 170int btrfs_update_delayed_ref(struct btrfs_trans_handle *trans,
174 u64 bytenr, u64 num_bytes, u64 orig_parent, 171 u64 bytenr, u64 num_bytes, u64 orig_parent,
175 u64 parent, u64 orig_ref_root, u64 ref_root, 172 u64 parent, u64 orig_ref_root, u64 ref_root,
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 574594cf6b51..054b4475c757 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -1522,7 +1522,7 @@ static int transaction_kthread(void *arg)
1522 goto sleep; 1522 goto sleep;
1523 } 1523 }
1524 mutex_unlock(&root->fs_info->trans_mutex); 1524 mutex_unlock(&root->fs_info->trans_mutex);
1525 trans = btrfs_start_transaction(root, 1); 1525 trans = btrfs_join_transaction(root, 1);
1526 ret = btrfs_commit_transaction(trans, root); 1526 ret = btrfs_commit_transaction(trans, root);
1527 1527
1528sleep: 1528sleep:
@@ -2409,11 +2409,11 @@ int btrfs_commit_super(struct btrfs_root *root)
2409 down_write(&root->fs_info->cleanup_work_sem); 2409 down_write(&root->fs_info->cleanup_work_sem);
2410 up_write(&root->fs_info->cleanup_work_sem); 2410 up_write(&root->fs_info->cleanup_work_sem);
2411 2411
2412 trans = btrfs_start_transaction(root, 1); 2412 trans = btrfs_join_transaction(root, 1);
2413 ret = btrfs_commit_transaction(trans, root); 2413 ret = btrfs_commit_transaction(trans, root);
2414 BUG_ON(ret); 2414 BUG_ON(ret);
2415 /* run commit again to drop the original snapshot */ 2415 /* run commit again to drop the original snapshot */
2416 trans = btrfs_start_transaction(root, 1); 2416 trans = btrfs_join_transaction(root, 1);
2417 btrfs_commit_transaction(trans, root); 2417 btrfs_commit_transaction(trans, root);
2418 ret = btrfs_write_and_wait_transaction(NULL, root); 2418 ret = btrfs_write_and_wait_transaction(NULL, root);
2419 BUG_ON(ret); 2419 BUG_ON(ret);
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index 3367278ac6a1..657df6e002d3 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -616,6 +616,113 @@ int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
616} 616}
617 617
618/* 618/*
619 * helper function to lookup reference count and flags of extent.
620 *
621 * the head node for delayed ref is used to store the sum of all the
622 * reference count modifications queued up in the rbtree. the head
623 * node may also store the extent flags to set. This way you can check
624 * to see what the reference count and extent flags would be if all of
625 * the delayed refs are not processed.
626 */
627int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
628 struct btrfs_root *root, u64 bytenr,
629 u64 num_bytes, u64 *refs, u64 *flags)
630{
631 struct btrfs_delayed_ref_head *head;
632 struct btrfs_delayed_ref_root *delayed_refs;
633 struct btrfs_path *path;
634 struct btrfs_extent_item *ei;
635 struct extent_buffer *leaf;
636 struct btrfs_key key;
637 u32 item_size;
638 u64 num_refs;
639 u64 extent_flags;
640 int ret;
641
642 path = btrfs_alloc_path();
643 if (!path)
644 return -ENOMEM;
645
646 key.objectid = bytenr;
647 key.type = BTRFS_EXTENT_ITEM_KEY;
648 key.offset = num_bytes;
649 if (!trans) {
650 path->skip_locking = 1;
651 path->search_commit_root = 1;
652 }
653again:
654 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
655 &key, path, 0, 0);
656 if (ret < 0)
657 goto out_free;
658
659 if (ret == 0) {
660 leaf = path->nodes[0];
661 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
662 if (item_size >= sizeof(*ei)) {
663 ei = btrfs_item_ptr(leaf, path->slots[0],
664 struct btrfs_extent_item);
665 num_refs = btrfs_extent_refs(leaf, ei);
666 extent_flags = btrfs_extent_flags(leaf, ei);
667 } else {
668#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
669 struct btrfs_extent_item_v0 *ei0;
670 BUG_ON(item_size != sizeof(*ei0));
671 ei0 = btrfs_item_ptr(leaf, path->slots[0],
672 struct btrfs_extent_item_v0);
673 num_refs = btrfs_extent_refs_v0(leaf, ei0);
674 /* FIXME: this isn't correct for data */
675 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
676#else
677 BUG();
678#endif
679 }
680 BUG_ON(num_refs == 0);
681 } else {
682 num_refs = 0;
683 extent_flags = 0;
684 ret = 0;
685 }
686
687 if (!trans)
688 goto out;
689
690 delayed_refs = &trans->transaction->delayed_refs;
691 spin_lock(&delayed_refs->lock);
692 head = btrfs_find_delayed_ref_head(trans, bytenr);
693 if (head) {
694 if (!mutex_trylock(&head->mutex)) {
695 atomic_inc(&head->node.refs);
696 spin_unlock(&delayed_refs->lock);
697
698 btrfs_release_path(root->fs_info->extent_root, path);
699
700 mutex_lock(&head->mutex);
701 mutex_unlock(&head->mutex);
702 btrfs_put_delayed_ref(&head->node);
703 goto again;
704 }
705 if (head->extent_op && head->extent_op->update_flags)
706 extent_flags |= head->extent_op->flags_to_set;
707 else
708 BUG_ON(num_refs == 0);
709
710 num_refs += head->node.ref_mod;
711 mutex_unlock(&head->mutex);
712 }
713 spin_unlock(&delayed_refs->lock);
714out:
715 WARN_ON(num_refs == 0);
716 if (refs)
717 *refs = num_refs;
718 if (flags)
719 *flags = extent_flags;
720out_free:
721 btrfs_free_path(path);
722 return ret;
723}
724
725/*
619 * Back reference rules. Back refs have three main goals: 726 * Back reference rules. Back refs have three main goals:
620 * 727 *
621 * 1) differentiate between all holders of references to an extent so that 728 * 1) differentiate between all holders of references to an extent so that
@@ -2949,113 +3056,6 @@ again:
2949} 3056}
2950 3057
2951/* 3058/*
2952 * unreserve num_items number of items worth of metadata space. This needs to
2953 * be paired with btrfs_reserve_metadata_space.
2954 *
2955 * NOTE: if you have the option, run this _AFTER_ you do a
2956 * btrfs_end_transaction, since btrfs_end_transaction will run delayed ref
2957 * oprations which will result in more used metadata, so we want to make sure we
2958 * can do that without issue.
2959 */
2960int btrfs_unreserve_metadata_space(struct btrfs_root *root, int num_items)
2961{
2962 struct btrfs_fs_info *info = root->fs_info;
2963 struct btrfs_space_info *meta_sinfo;
2964 u64 num_bytes;
2965 u64 alloc_target;
2966 bool bug = false;
2967
2968 /* get the space info for where the metadata will live */
2969 alloc_target = btrfs_get_alloc_profile(root, 0);
2970 meta_sinfo = __find_space_info(info, alloc_target);
2971
2972 num_bytes = calculate_bytes_needed(root, num_items);
2973
2974 spin_lock(&meta_sinfo->lock);
2975 if (meta_sinfo->bytes_may_use < num_bytes) {
2976 bug = true;
2977 meta_sinfo->bytes_may_use = 0;
2978 } else {
2979 meta_sinfo->bytes_may_use -= num_bytes;
2980 }
2981 spin_unlock(&meta_sinfo->lock);
2982
2983 BUG_ON(bug);
2984
2985 return 0;
2986}
2987
2988/*
2989 * Reserve some metadata space for use. We'll calculate the worste case number
2990 * of bytes that would be needed to modify num_items number of items. If we
2991 * have space, fantastic, if not, you get -ENOSPC. Please call
2992 * btrfs_unreserve_metadata_space when you are done for the _SAME_ number of
2993 * items you reserved, since whatever metadata you needed should have already
2994 * been allocated.
2995 *
2996 * This will commit the transaction to make more space if we don't have enough
2997 * metadata space. THe only time we don't do this is if we're reserving space
2998 * inside of a transaction, then we will just return -ENOSPC and it is the
2999 * callers responsibility to handle it properly.
3000 */
3001int btrfs_reserve_metadata_space(struct btrfs_root *root, int num_items)
3002{
3003 struct btrfs_fs_info *info = root->fs_info;
3004 struct btrfs_space_info *meta_sinfo;
3005 u64 num_bytes;
3006 u64 used;
3007 u64 alloc_target;
3008 int retries = 0;
3009
3010 /* get the space info for where the metadata will live */
3011 alloc_target = btrfs_get_alloc_profile(root, 0);
3012 meta_sinfo = __find_space_info(info, alloc_target);
3013
3014 num_bytes = calculate_bytes_needed(root, num_items);
3015again:
3016 spin_lock(&meta_sinfo->lock);
3017
3018 if (unlikely(!meta_sinfo->bytes_root))
3019 meta_sinfo->bytes_root = calculate_bytes_needed(root, 6);
3020
3021 if (!retries)
3022 meta_sinfo->bytes_may_use += num_bytes;
3023
3024 used = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
3025 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
3026 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
3027 meta_sinfo->bytes_may_use + meta_sinfo->bytes_delalloc;
3028
3029 if (used > meta_sinfo->total_bytes) {
3030 retries++;
3031 if (retries == 1) {
3032 if (maybe_allocate_chunk(NULL, root, meta_sinfo,
3033 num_bytes))
3034 goto again;
3035 retries++;
3036 } else {
3037 spin_unlock(&meta_sinfo->lock);
3038 }
3039
3040 if (retries == 2) {
3041 shrink_delalloc(NULL, root, meta_sinfo, num_bytes);
3042 goto again;
3043 }
3044 spin_lock(&meta_sinfo->lock);
3045 meta_sinfo->bytes_may_use -= num_bytes;
3046 spin_unlock(&meta_sinfo->lock);
3047
3048 dump_space_info(meta_sinfo, 0, 0);
3049 return -ENOSPC;
3050 }
3051
3052 check_force_delalloc(meta_sinfo);
3053 spin_unlock(&meta_sinfo->lock);
3054
3055 return 0;
3056}
3057
3058/*
3059 * This will check the space that the inode allocates from to make sure we have 3059 * This will check the space that the inode allocates from to make sure we have
3060 * enough space for bytes. 3060 * enough space for bytes.
3061 */ 3061 */
@@ -3095,9 +3095,9 @@ again:
3095 spin_unlock(&data_sinfo->lock); 3095 spin_unlock(&data_sinfo->lock);
3096alloc: 3096alloc:
3097 alloc_target = btrfs_get_alloc_profile(root, 1); 3097 alloc_target = btrfs_get_alloc_profile(root, 1);
3098 trans = btrfs_start_transaction(root, 1); 3098 trans = btrfs_join_transaction(root, 1);
3099 if (!trans) 3099 if (IS_ERR(trans))
3100 return -ENOMEM; 3100 return PTR_ERR(trans);
3101 3101
3102 ret = do_chunk_alloc(trans, root->fs_info->extent_root, 3102 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3103 bytes + 2 * 1024 * 1024, 3103 bytes + 2 * 1024 * 1024,
@@ -3118,8 +3118,8 @@ alloc:
3118 if (!committed && !root->fs_info->open_ioctl_trans) { 3118 if (!committed && !root->fs_info->open_ioctl_trans) {
3119 committed = 1; 3119 committed = 1;
3120 trans = btrfs_join_transaction(root, 1); 3120 trans = btrfs_join_transaction(root, 1);
3121 if (!trans) 3121 if (IS_ERR(trans))
3122 return -ENOMEM; 3122 return PTR_ERR(trans);
3123 ret = btrfs_commit_transaction(trans, root); 3123 ret = btrfs_commit_transaction(trans, root);
3124 if (ret) 3124 if (ret)
3125 return ret; 3125 return ret;
@@ -3701,6 +3701,59 @@ static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
3701 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv; 3701 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
3702} 3702}
3703 3703
3704static u64 calc_trans_metadata_size(struct btrfs_root *root, int num_items)
3705{
3706 return (root->leafsize + root->nodesize * (BTRFS_MAX_LEVEL - 1)) *
3707 3 * num_items;
3708}
3709
3710int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans,
3711 struct btrfs_root *root,
3712 int num_items, int *retries)
3713{
3714 u64 num_bytes;
3715 int ret;
3716
3717 if (num_items == 0 || root->fs_info->chunk_root == root)
3718 return 0;
3719
3720 num_bytes = calc_trans_metadata_size(root, num_items);
3721 ret = btrfs_block_rsv_add(trans, root, &root->fs_info->trans_block_rsv,
3722 num_bytes, retries);
3723 if (!ret) {
3724 trans->bytes_reserved += num_bytes;
3725 trans->block_rsv = &root->fs_info->trans_block_rsv;
3726 }
3727 return ret;
3728}
3729
3730void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
3731 struct btrfs_root *root)
3732{
3733 if (!trans->bytes_reserved)
3734 return;
3735
3736 BUG_ON(trans->block_rsv != &root->fs_info->trans_block_rsv);
3737 btrfs_block_rsv_release(root, trans->block_rsv,
3738 trans->bytes_reserved);
3739 trans->bytes_reserved = 0;
3740}
3741
3742int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
3743 struct btrfs_pending_snapshot *pending)
3744{
3745 struct btrfs_root *root = pending->root;
3746 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
3747 struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
3748 /*
3749 * two for root back/forward refs, two for directory entries
3750 * and one for root of the snapshot.
3751 */
3752 u64 num_bytes = calc_trans_metadata_size(root, 5);
3753 dst_rsv->space_info = src_rsv->space_info;
3754 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3755}
3756
3704static int update_block_group(struct btrfs_trans_handle *trans, 3757static int update_block_group(struct btrfs_trans_handle *trans,
3705 struct btrfs_root *root, 3758 struct btrfs_root *root,
3706 u64 bytenr, u64 num_bytes, int alloc) 3759 u64 bytenr, u64 num_bytes, int alloc)
@@ -5824,7 +5877,7 @@ int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref)
5824 wc = kzalloc(sizeof(*wc), GFP_NOFS); 5877 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5825 BUG_ON(!wc); 5878 BUG_ON(!wc);
5826 5879
5827 trans = btrfs_start_transaction(tree_root, 1); 5880 trans = btrfs_start_transaction(tree_root, 0);
5828 5881
5829 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { 5882 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
5830 level = btrfs_header_level(root->node); 5883 level = btrfs_header_level(root->node);
@@ -5920,7 +5973,9 @@ int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref)
5920 BUG_ON(ret); 5973 BUG_ON(ret);
5921 5974
5922 btrfs_end_transaction(trans, tree_root); 5975 btrfs_end_transaction(trans, tree_root);
5923 trans = btrfs_start_transaction(tree_root, 1); 5976 trans = btrfs_start_transaction(tree_root, 0);
5977 if (IS_ERR(trans))
5978 return PTR_ERR(trans);
5924 } else { 5979 } else {
5925 unsigned long update; 5980 unsigned long update;
5926 update = trans->delayed_ref_updates; 5981 update = trans->delayed_ref_updates;
diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
index 29ff749ff4ca..41e09e24e295 100644
--- a/fs/btrfs/file.c
+++ b/fs/btrfs/file.c
@@ -126,8 +126,7 @@ static noinline int dirty_and_release_pages(struct btrfs_trans_handle *trans,
126 end_of_last_block = start_pos + num_bytes - 1; 126 end_of_last_block = start_pos + num_bytes - 1;
127 err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block, 127 err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block,
128 NULL); 128 NULL);
129 if (err) 129 BUG_ON(err);
130 return err;
131 130
132 for (i = 0; i < num_pages; i++) { 131 for (i = 0; i < num_pages; i++) {
133 struct page *p = pages[i]; 132 struct page *p = pages[i];
@@ -142,7 +141,7 @@ static noinline int dirty_and_release_pages(struct btrfs_trans_handle *trans,
142 * at this time. 141 * at this time.
143 */ 142 */
144 } 143 }
145 return err; 144 return 0;
146} 145}
147 146
148/* 147/*
@@ -1008,7 +1007,7 @@ out_nolock:
1008 num_written = err; 1007 num_written = err;
1009 1008
1010 if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) { 1009 if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
1011 trans = btrfs_start_transaction(root, 1); 1010 trans = btrfs_start_transaction(root, 0);
1012 ret = btrfs_log_dentry_safe(trans, root, 1011 ret = btrfs_log_dentry_safe(trans, root,
1013 file->f_dentry); 1012 file->f_dentry);
1014 if (ret == 0) { 1013 if (ret == 0) {
@@ -1104,9 +1103,9 @@ int btrfs_sync_file(struct file *file, struct dentry *dentry, int datasync)
1104 if (file && file->private_data) 1103 if (file && file->private_data)
1105 btrfs_ioctl_trans_end(file); 1104 btrfs_ioctl_trans_end(file);
1106 1105
1107 trans = btrfs_start_transaction(root, 1); 1106 trans = btrfs_start_transaction(root, 0);
1108 if (!trans) { 1107 if (IS_ERR(trans)) {
1109 ret = -ENOMEM; 1108 ret = PTR_ERR(trans);
1110 goto out; 1109 goto out;
1111 } 1110 }
1112 1111
diff --git a/fs/btrfs/inode-item.c b/fs/btrfs/inode-item.c
index 72ce3c173d6a..64f1150bb48d 100644
--- a/fs/btrfs/inode-item.c
+++ b/fs/btrfs/inode-item.c
@@ -49,6 +49,33 @@ static int find_name_in_backref(struct btrfs_path *path, const char *name,
49 return 0; 49 return 0;
50} 50}
51 51
52struct btrfs_inode_ref *
53btrfs_lookup_inode_ref(struct btrfs_trans_handle *trans,
54 struct btrfs_root *root,
55 struct btrfs_path *path,
56 const char *name, int name_len,
57 u64 inode_objectid, u64 ref_objectid, int mod)
58{
59 struct btrfs_key key;
60 struct btrfs_inode_ref *ref;
61 int ins_len = mod < 0 ? -1 : 0;
62 int cow = mod != 0;
63 int ret;
64
65 key.objectid = inode_objectid;
66 key.type = BTRFS_INODE_REF_KEY;
67 key.offset = ref_objectid;
68
69 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
70 if (ret < 0)
71 return ERR_PTR(ret);
72 if (ret > 0)
73 return NULL;
74 if (!find_name_in_backref(path, name, name_len, &ref))
75 return NULL;
76 return ref;
77}
78
52int btrfs_del_inode_ref(struct btrfs_trans_handle *trans, 79int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
53 struct btrfs_root *root, 80 struct btrfs_root *root,
54 const char *name, int name_len, 81 const char *name, int name_len,
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index f44425081c02..c4b0fd12df68 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -2135,7 +2135,7 @@ void btrfs_orphan_cleanup(struct btrfs_root *root)
2135 * do a destroy_inode 2135 * do a destroy_inode
2136 */ 2136 */
2137 if (is_bad_inode(inode)) { 2137 if (is_bad_inode(inode)) {
2138 trans = btrfs_start_transaction(root, 1); 2138 trans = btrfs_start_transaction(root, 0);
2139 btrfs_orphan_del(trans, inode); 2139 btrfs_orphan_del(trans, inode);
2140 btrfs_end_transaction(trans, root); 2140 btrfs_end_transaction(trans, root);
2141 iput(inode); 2141 iput(inode);
@@ -2478,29 +2478,201 @@ out:
2478 return ret; 2478 return ret;
2479} 2479}
2480 2480
2481static int btrfs_unlink(struct inode *dir, struct dentry *dentry) 2481/* helper to check if there is any shared block in the path */
2482static int check_path_shared(struct btrfs_root *root,
2483 struct btrfs_path *path)
2484{
2485 struct extent_buffer *eb;
2486 int level;
2487 int ret;
2488 u64 refs;
2489
2490 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
2491 if (!path->nodes[level])
2492 break;
2493 eb = path->nodes[level];
2494 if (!btrfs_block_can_be_shared(root, eb))
2495 continue;
2496 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2497 &refs, NULL);
2498 if (refs > 1)
2499 return 1;
2500 }
2501 return 0;
2502}
2503
2504/*
2505 * helper to start transaction for unlink and rmdir.
2506 *
2507 * unlink and rmdir are special in btrfs, they do not always free space.
2508 * so in enospc case, we should make sure they will free space before
2509 * allowing them to use the global metadata reservation.
2510 */
2511static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2512 struct dentry *dentry)
2482{ 2513{
2483 struct btrfs_root *root;
2484 struct btrfs_trans_handle *trans; 2514 struct btrfs_trans_handle *trans;
2515 struct btrfs_root *root = BTRFS_I(dir)->root;
2516 struct btrfs_path *path;
2517 struct btrfs_inode_ref *ref;
2518 struct btrfs_dir_item *di;
2485 struct inode *inode = dentry->d_inode; 2519 struct inode *inode = dentry->d_inode;
2520 u64 index;
2521 int check_link = 1;
2522 int err = -ENOSPC;
2486 int ret; 2523 int ret;
2487 unsigned long nr = 0;
2488 2524
2489 root = BTRFS_I(dir)->root; 2525 trans = btrfs_start_transaction(root, 10);
2526 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2527 return trans;
2490 2528
2491 /* 2529 if (inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
2492 * 5 items for unlink inode 2530 return ERR_PTR(-ENOSPC);
2493 * 1 for orphan
2494 */
2495 ret = btrfs_reserve_metadata_space(root, 6);
2496 if (ret)
2497 return ret;
2498 2531
2499 trans = btrfs_start_transaction(root, 1); 2532 /* check if there is someone else holds reference */
2533 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2534 return ERR_PTR(-ENOSPC);
2535
2536 if (atomic_read(&inode->i_count) > 2)
2537 return ERR_PTR(-ENOSPC);
2538
2539 if (xchg(&root->fs_info->enospc_unlink, 1))
2540 return ERR_PTR(-ENOSPC);
2541
2542 path = btrfs_alloc_path();
2543 if (!path) {
2544 root->fs_info->enospc_unlink = 0;
2545 return ERR_PTR(-ENOMEM);
2546 }
2547
2548 trans = btrfs_start_transaction(root, 0);
2500 if (IS_ERR(trans)) { 2549 if (IS_ERR(trans)) {
2501 btrfs_unreserve_metadata_space(root, 6); 2550 btrfs_free_path(path);
2502 return PTR_ERR(trans); 2551 root->fs_info->enospc_unlink = 0;
2552 return trans;
2553 }
2554
2555 path->skip_locking = 1;
2556 path->search_commit_root = 1;
2557
2558 ret = btrfs_lookup_inode(trans, root, path,
2559 &BTRFS_I(dir)->location, 0);
2560 if (ret < 0) {
2561 err = ret;
2562 goto out;
2563 }
2564 if (ret == 0) {
2565 if (check_path_shared(root, path))
2566 goto out;
2567 } else {
2568 check_link = 0;
2503 } 2569 }
2570 btrfs_release_path(root, path);
2571
2572 ret = btrfs_lookup_inode(trans, root, path,
2573 &BTRFS_I(inode)->location, 0);
2574 if (ret < 0) {
2575 err = ret;
2576 goto out;
2577 }
2578 if (ret == 0) {
2579 if (check_path_shared(root, path))
2580 goto out;
2581 } else {
2582 check_link = 0;
2583 }
2584 btrfs_release_path(root, path);
2585
2586 if (ret == 0 && S_ISREG(inode->i_mode)) {
2587 ret = btrfs_lookup_file_extent(trans, root, path,
2588 inode->i_ino, (u64)-1, 0);
2589 if (ret < 0) {
2590 err = ret;
2591 goto out;
2592 }
2593 BUG_ON(ret == 0);
2594 if (check_path_shared(root, path))
2595 goto out;
2596 btrfs_release_path(root, path);
2597 }
2598
2599 if (!check_link) {
2600 err = 0;
2601 goto out;
2602 }
2603
2604 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2605 dentry->d_name.name, dentry->d_name.len, 0);
2606 if (IS_ERR(di)) {
2607 err = PTR_ERR(di);
2608 goto out;
2609 }
2610 if (di) {
2611 if (check_path_shared(root, path))
2612 goto out;
2613 } else {
2614 err = 0;
2615 goto out;
2616 }
2617 btrfs_release_path(root, path);
2618
2619 ref = btrfs_lookup_inode_ref(trans, root, path,
2620 dentry->d_name.name, dentry->d_name.len,
2621 inode->i_ino, dir->i_ino, 0);
2622 if (IS_ERR(ref)) {
2623 err = PTR_ERR(ref);
2624 goto out;
2625 }
2626 BUG_ON(!ref);
2627 if (check_path_shared(root, path))
2628 goto out;
2629 index = btrfs_inode_ref_index(path->nodes[0], ref);
2630 btrfs_release_path(root, path);
2631
2632 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, index,
2633 dentry->d_name.name, dentry->d_name.len, 0);
2634 if (IS_ERR(di)) {
2635 err = PTR_ERR(di);
2636 goto out;
2637 }
2638 BUG_ON(ret == -ENOENT);
2639 if (check_path_shared(root, path))
2640 goto out;
2641
2642 err = 0;
2643out:
2644 btrfs_free_path(path);
2645 if (err) {
2646 btrfs_end_transaction(trans, root);
2647 root->fs_info->enospc_unlink = 0;
2648 return ERR_PTR(err);
2649 }
2650
2651 trans->block_rsv = &root->fs_info->global_block_rsv;
2652 return trans;
2653}
2654
2655static void __unlink_end_trans(struct btrfs_trans_handle *trans,
2656 struct btrfs_root *root)
2657{
2658 if (trans->block_rsv == &root->fs_info->global_block_rsv) {
2659 BUG_ON(!root->fs_info->enospc_unlink);
2660 root->fs_info->enospc_unlink = 0;
2661 }
2662 btrfs_end_transaction_throttle(trans, root);
2663}
2664
2665static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2666{
2667 struct btrfs_root *root = BTRFS_I(dir)->root;
2668 struct btrfs_trans_handle *trans;
2669 struct inode *inode = dentry->d_inode;
2670 int ret;
2671 unsigned long nr = 0;
2672
2673 trans = __unlink_start_trans(dir, dentry);
2674 if (IS_ERR(trans))
2675 return PTR_ERR(trans);
2504 2676
2505 btrfs_set_trans_block_group(trans, dir); 2677 btrfs_set_trans_block_group(trans, dir);
2506 2678
@@ -2508,14 +2680,15 @@ static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2508 2680
2509 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, 2681 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2510 dentry->d_name.name, dentry->d_name.len); 2682 dentry->d_name.name, dentry->d_name.len);
2683 BUG_ON(ret);
2511 2684
2512 if (inode->i_nlink == 0) 2685 if (inode->i_nlink == 0) {
2513 ret = btrfs_orphan_add(trans, inode); 2686 ret = btrfs_orphan_add(trans, inode);
2687 BUG_ON(ret);
2688 }
2514 2689
2515 nr = trans->blocks_used; 2690 nr = trans->blocks_used;
2516 2691 __unlink_end_trans(trans, root);
2517 btrfs_end_transaction_throttle(trans, root);
2518 btrfs_unreserve_metadata_space(root, 6);
2519 btrfs_btree_balance_dirty(root, nr); 2692 btrfs_btree_balance_dirty(root, nr);
2520 return ret; 2693 return ret;
2521} 2694}
@@ -2587,7 +2760,6 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2587{ 2760{
2588 struct inode *inode = dentry->d_inode; 2761 struct inode *inode = dentry->d_inode;
2589 int err = 0; 2762 int err = 0;
2590 int ret;
2591 struct btrfs_root *root = BTRFS_I(dir)->root; 2763 struct btrfs_root *root = BTRFS_I(dir)->root;
2592 struct btrfs_trans_handle *trans; 2764 struct btrfs_trans_handle *trans;
2593 unsigned long nr = 0; 2765 unsigned long nr = 0;
@@ -2596,15 +2768,9 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2596 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 2768 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
2597 return -ENOTEMPTY; 2769 return -ENOTEMPTY;
2598 2770
2599 ret = btrfs_reserve_metadata_space(root, 5); 2771 trans = __unlink_start_trans(dir, dentry);
2600 if (ret) 2772 if (IS_ERR(trans))
2601 return ret;
2602
2603 trans = btrfs_start_transaction(root, 1);
2604 if (IS_ERR(trans)) {
2605 btrfs_unreserve_metadata_space(root, 5);
2606 return PTR_ERR(trans); 2773 return PTR_ERR(trans);
2607 }
2608 2774
2609 btrfs_set_trans_block_group(trans, dir); 2775 btrfs_set_trans_block_group(trans, dir);
2610 2776
@@ -2627,12 +2793,9 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2627 btrfs_i_size_write(inode, 0); 2793 btrfs_i_size_write(inode, 0);
2628out: 2794out:
2629 nr = trans->blocks_used; 2795 nr = trans->blocks_used;
2630 ret = btrfs_end_transaction_throttle(trans, root); 2796 __unlink_end_trans(trans, root);
2631 btrfs_unreserve_metadata_space(root, 5);
2632 btrfs_btree_balance_dirty(root, nr); 2797 btrfs_btree_balance_dirty(root, nr);
2633 2798
2634 if (ret && !err)
2635 err = ret;
2636 return err; 2799 return err;
2637} 2800}
2638 2801
@@ -3145,7 +3308,7 @@ int btrfs_cont_expand(struct inode *inode, loff_t size)
3145 struct btrfs_trans_handle *trans; 3308 struct btrfs_trans_handle *trans;
3146 struct btrfs_root *root = BTRFS_I(inode)->root; 3309 struct btrfs_root *root = BTRFS_I(inode)->root;
3147 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 3310 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3148 struct extent_map *em; 3311 struct extent_map *em = NULL;
3149 struct extent_state *cached_state = NULL; 3312 struct extent_state *cached_state = NULL;
3150 u64 mask = root->sectorsize - 1; 3313 u64 mask = root->sectorsize - 1;
3151 u64 hole_start = (inode->i_size + mask) & ~mask; 3314 u64 hole_start = (inode->i_size + mask) & ~mask;
@@ -3183,11 +3346,11 @@ int btrfs_cont_expand(struct inode *inode, loff_t size)
3183 u64 hint_byte = 0; 3346 u64 hint_byte = 0;
3184 hole_size = last_byte - cur_offset; 3347 hole_size = last_byte - cur_offset;
3185 3348
3186 err = btrfs_reserve_metadata_space(root, 2); 3349 trans = btrfs_start_transaction(root, 2);
3187 if (err) 3350 if (IS_ERR(trans)) {
3351 err = PTR_ERR(trans);
3188 break; 3352 break;
3189 3353 }
3190 trans = btrfs_start_transaction(root, 1);
3191 btrfs_set_trans_block_group(trans, inode); 3354 btrfs_set_trans_block_group(trans, inode);
3192 3355
3193 err = btrfs_drop_extents(trans, inode, cur_offset, 3356 err = btrfs_drop_extents(trans, inode, cur_offset,
@@ -3205,14 +3368,15 @@ int btrfs_cont_expand(struct inode *inode, loff_t size)
3205 last_byte - 1, 0); 3368 last_byte - 1, 0);
3206 3369
3207 btrfs_end_transaction(trans, root); 3370 btrfs_end_transaction(trans, root);
3208 btrfs_unreserve_metadata_space(root, 2);
3209 } 3371 }
3210 free_extent_map(em); 3372 free_extent_map(em);
3373 em = NULL;
3211 cur_offset = last_byte; 3374 cur_offset = last_byte;
3212 if (cur_offset >= block_end) 3375 if (cur_offset >= block_end)
3213 break; 3376 break;
3214 } 3377 }
3215 3378
3379 free_extent_map(em);
3216 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, 3380 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3217 GFP_NOFS); 3381 GFP_NOFS);
3218 return err; 3382 return err;
@@ -3239,10 +3403,6 @@ static int btrfs_setattr_size(struct inode *inode, struct iattr *attr)
3239 } 3403 }
3240 } 3404 }
3241 3405
3242 ret = btrfs_reserve_metadata_space(root, 1);
3243 if (ret)
3244 return ret;
3245
3246 trans = btrfs_start_transaction(root, 1); 3406 trans = btrfs_start_transaction(root, 1);
3247 btrfs_set_trans_block_group(trans, inode); 3407 btrfs_set_trans_block_group(trans, inode);
3248 3408
@@ -3251,7 +3411,6 @@ static int btrfs_setattr_size(struct inode *inode, struct iattr *attr)
3251 3411
3252 nr = trans->blocks_used; 3412 nr = trans->blocks_used;
3253 btrfs_end_transaction(trans, root); 3413 btrfs_end_transaction(trans, root);
3254 btrfs_unreserve_metadata_space(root, 1);
3255 btrfs_btree_balance_dirty(root, nr); 3414 btrfs_btree_balance_dirty(root, nr);
3256 3415
3257 if (attr->ia_size > inode->i_size) { 3416 if (attr->ia_size > inode->i_size) {
@@ -4223,26 +4382,21 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4223 if (!new_valid_dev(rdev)) 4382 if (!new_valid_dev(rdev))
4224 return -EINVAL; 4383 return -EINVAL;
4225 4384
4385 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4386 if (err)
4387 return err;
4388
4226 /* 4389 /*
4227 * 2 for inode item and ref 4390 * 2 for inode item and ref
4228 * 2 for dir items 4391 * 2 for dir items
4229 * 1 for xattr if selinux is on 4392 * 1 for xattr if selinux is on
4230 */ 4393 */
4231 err = btrfs_reserve_metadata_space(root, 5); 4394 trans = btrfs_start_transaction(root, 5);
4232 if (err) 4395 if (IS_ERR(trans))
4233 return err; 4396 return PTR_ERR(trans);
4234 4397
4235 trans = btrfs_start_transaction(root, 1);
4236 if (!trans)
4237 goto fail;
4238 btrfs_set_trans_block_group(trans, dir); 4398 btrfs_set_trans_block_group(trans, dir);
4239 4399
4240 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4241 if (err) {
4242 err = -ENOSPC;
4243 goto out_unlock;
4244 }
4245
4246 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, 4400 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4247 dentry->d_name.len, 4401 dentry->d_name.len,
4248 dentry->d_parent->d_inode->i_ino, objectid, 4402 dentry->d_parent->d_inode->i_ino, objectid,
@@ -4271,13 +4425,11 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4271out_unlock: 4425out_unlock:
4272 nr = trans->blocks_used; 4426 nr = trans->blocks_used;
4273 btrfs_end_transaction_throttle(trans, root); 4427 btrfs_end_transaction_throttle(trans, root);
4274fail: 4428 btrfs_btree_balance_dirty(root, nr);
4275 btrfs_unreserve_metadata_space(root, 5);
4276 if (drop_inode) { 4429 if (drop_inode) {
4277 inode_dec_link_count(inode); 4430 inode_dec_link_count(inode);
4278 iput(inode); 4431 iput(inode);
4279 } 4432 }
4280 btrfs_btree_balance_dirty(root, nr);
4281 return err; 4433 return err;
4282} 4434}
4283 4435
@@ -4287,32 +4439,26 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
4287 struct btrfs_trans_handle *trans; 4439 struct btrfs_trans_handle *trans;
4288 struct btrfs_root *root = BTRFS_I(dir)->root; 4440 struct btrfs_root *root = BTRFS_I(dir)->root;
4289 struct inode *inode = NULL; 4441 struct inode *inode = NULL;
4290 int err;
4291 int drop_inode = 0; 4442 int drop_inode = 0;
4443 int err;
4292 unsigned long nr = 0; 4444 unsigned long nr = 0;
4293 u64 objectid; 4445 u64 objectid;
4294 u64 index = 0; 4446 u64 index = 0;
4295 4447
4448 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4449 if (err)
4450 return err;
4296 /* 4451 /*
4297 * 2 for inode item and ref 4452 * 2 for inode item and ref
4298 * 2 for dir items 4453 * 2 for dir items
4299 * 1 for xattr if selinux is on 4454 * 1 for xattr if selinux is on
4300 */ 4455 */
4301 err = btrfs_reserve_metadata_space(root, 5); 4456 trans = btrfs_start_transaction(root, 5);
4302 if (err) 4457 if (IS_ERR(trans))
4303 return err; 4458 return PTR_ERR(trans);
4304 4459
4305 trans = btrfs_start_transaction(root, 1);
4306 if (!trans)
4307 goto fail;
4308 btrfs_set_trans_block_group(trans, dir); 4460 btrfs_set_trans_block_group(trans, dir);
4309 4461
4310 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4311 if (err) {
4312 err = -ENOSPC;
4313 goto out_unlock;
4314 }
4315
4316 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, 4462 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4317 dentry->d_name.len, 4463 dentry->d_name.len,
4318 dentry->d_parent->d_inode->i_ino, 4464 dentry->d_parent->d_inode->i_ino,
@@ -4344,8 +4490,6 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
4344out_unlock: 4490out_unlock:
4345 nr = trans->blocks_used; 4491 nr = trans->blocks_used;
4346 btrfs_end_transaction_throttle(trans, root); 4492 btrfs_end_transaction_throttle(trans, root);
4347fail:
4348 btrfs_unreserve_metadata_space(root, 5);
4349 if (drop_inode) { 4493 if (drop_inode) {
4350 inode_dec_link_count(inode); 4494 inode_dec_link_count(inode);
4351 iput(inode); 4495 iput(inode);
@@ -4372,21 +4516,21 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4372 if (root->objectid != BTRFS_I(inode)->root->objectid) 4516 if (root->objectid != BTRFS_I(inode)->root->objectid)
4373 return -EPERM; 4517 return -EPERM;
4374 4518
4375 /*
4376 * 1 item for inode ref
4377 * 2 items for dir items
4378 */
4379 err = btrfs_reserve_metadata_space(root, 3);
4380 if (err)
4381 return err;
4382
4383 btrfs_inc_nlink(inode); 4519 btrfs_inc_nlink(inode);
4384 4520
4385 err = btrfs_set_inode_index(dir, &index); 4521 err = btrfs_set_inode_index(dir, &index);
4386 if (err) 4522 if (err)
4387 goto fail; 4523 goto fail;
4388 4524
4389 trans = btrfs_start_transaction(root, 1); 4525 /*
4526 * 1 item for inode ref
4527 * 2 items for dir items
4528 */
4529 trans = btrfs_start_transaction(root, 3);
4530 if (IS_ERR(trans)) {
4531 err = PTR_ERR(trans);
4532 goto fail;
4533 }
4390 4534
4391 btrfs_set_trans_block_group(trans, dir); 4535 btrfs_set_trans_block_group(trans, dir);
4392 atomic_inc(&inode->i_count); 4536 atomic_inc(&inode->i_count);
@@ -4405,7 +4549,6 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4405 nr = trans->blocks_used; 4549 nr = trans->blocks_used;
4406 btrfs_end_transaction_throttle(trans, root); 4550 btrfs_end_transaction_throttle(trans, root);
4407fail: 4551fail:
4408 btrfs_unreserve_metadata_space(root, 3);
4409 if (drop_inode) { 4552 if (drop_inode) {
4410 inode_dec_link_count(inode); 4553 inode_dec_link_count(inode);
4411 iput(inode); 4554 iput(inode);
@@ -4425,28 +4568,20 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4425 u64 index = 0; 4568 u64 index = 0;
4426 unsigned long nr = 1; 4569 unsigned long nr = 1;
4427 4570
4571 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4572 if (err)
4573 return err;
4574
4428 /* 4575 /*
4429 * 2 items for inode and ref 4576 * 2 items for inode and ref
4430 * 2 items for dir items 4577 * 2 items for dir items
4431 * 1 for xattr if selinux is on 4578 * 1 for xattr if selinux is on
4432 */ 4579 */
4433 err = btrfs_reserve_metadata_space(root, 5); 4580 trans = btrfs_start_transaction(root, 5);
4434 if (err) 4581 if (IS_ERR(trans))
4435 return err; 4582 return PTR_ERR(trans);
4436
4437 trans = btrfs_start_transaction(root, 1);
4438 if (!trans) {
4439 err = -ENOMEM;
4440 goto out_unlock;
4441 }
4442 btrfs_set_trans_block_group(trans, dir); 4583 btrfs_set_trans_block_group(trans, dir);
4443 4584
4444 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4445 if (err) {
4446 err = -ENOSPC;
4447 goto out_fail;
4448 }
4449
4450 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, 4585 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4451 dentry->d_name.len, 4586 dentry->d_name.len,
4452 dentry->d_parent->d_inode->i_ino, objectid, 4587 dentry->d_parent->d_inode->i_ino, objectid,
@@ -4486,9 +4621,6 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4486out_fail: 4621out_fail:
4487 nr = trans->blocks_used; 4622 nr = trans->blocks_used;
4488 btrfs_end_transaction_throttle(trans, root); 4623 btrfs_end_transaction_throttle(trans, root);
4489
4490out_unlock:
4491 btrfs_unreserve_metadata_space(root, 5);
4492 if (drop_on_err) 4624 if (drop_on_err)
4493 iput(inode); 4625 iput(inode);
4494 btrfs_btree_balance_dirty(root, nr); 4626 btrfs_btree_balance_dirty(root, nr);
@@ -5426,19 +5558,6 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
5426 if (S_ISDIR(old_inode->i_mode) && new_inode && 5558 if (S_ISDIR(old_inode->i_mode) && new_inode &&
5427 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) 5559 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
5428 return -ENOTEMPTY; 5560 return -ENOTEMPTY;
5429
5430 /*
5431 * We want to reserve the absolute worst case amount of items. So if
5432 * both inodes are subvols and we need to unlink them then that would
5433 * require 4 item modifications, but if they are both normal inodes it
5434 * would require 5 item modifications, so we'll assume their normal
5435 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
5436 * should cover the worst case number of items we'll modify.
5437 */
5438 ret = btrfs_reserve_metadata_space(root, 11);
5439 if (ret)
5440 return ret;
5441
5442 /* 5561 /*
5443 * we're using rename to replace one file with another. 5562 * we're using rename to replace one file with another.
5444 * and the replacement file is large. Start IO on it now so 5563 * and the replacement file is large. Start IO on it now so
@@ -5451,8 +5570,18 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
5451 /* close the racy window with snapshot create/destroy ioctl */ 5570 /* close the racy window with snapshot create/destroy ioctl */
5452 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 5571 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5453 down_read(&root->fs_info->subvol_sem); 5572 down_read(&root->fs_info->subvol_sem);
5573 /*
5574 * We want to reserve the absolute worst case amount of items. So if
5575 * both inodes are subvols and we need to unlink them then that would
5576 * require 4 item modifications, but if they are both normal inodes it
5577 * would require 5 item modifications, so we'll assume their normal
5578 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
5579 * should cover the worst case number of items we'll modify.
5580 */
5581 trans = btrfs_start_transaction(root, 20);
5582 if (IS_ERR(trans))
5583 return PTR_ERR(trans);
5454 5584
5455 trans = btrfs_start_transaction(root, 1);
5456 btrfs_set_trans_block_group(trans, new_dir); 5585 btrfs_set_trans_block_group(trans, new_dir);
5457 5586
5458 if (dest != root) 5587 if (dest != root)
@@ -5551,7 +5680,6 @@ out_fail:
5551 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 5680 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5552 up_read(&root->fs_info->subvol_sem); 5681 up_read(&root->fs_info->subvol_sem);
5553 5682
5554 btrfs_unreserve_metadata_space(root, 11);
5555 return ret; 5683 return ret;
5556} 5684}
5557 5685
@@ -5658,26 +5786,20 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
5658 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) 5786 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
5659 return -ENAMETOOLONG; 5787 return -ENAMETOOLONG;
5660 5788
5789 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
5790 if (err)
5791 return err;
5661 /* 5792 /*
5662 * 2 items for inode item and ref 5793 * 2 items for inode item and ref
5663 * 2 items for dir items 5794 * 2 items for dir items
5664 * 1 item for xattr if selinux is on 5795 * 1 item for xattr if selinux is on
5665 */ 5796 */
5666 err = btrfs_reserve_metadata_space(root, 5); 5797 trans = btrfs_start_transaction(root, 5);
5667 if (err) 5798 if (IS_ERR(trans))
5668 return err; 5799 return PTR_ERR(trans);
5669 5800
5670 trans = btrfs_start_transaction(root, 1);
5671 if (!trans)
5672 goto out_fail;
5673 btrfs_set_trans_block_group(trans, dir); 5801 btrfs_set_trans_block_group(trans, dir);
5674 5802
5675 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
5676 if (err) {
5677 err = -ENOSPC;
5678 goto out_unlock;
5679 }
5680
5681 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, 5803 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
5682 dentry->d_name.len, 5804 dentry->d_name.len,
5683 dentry->d_parent->d_inode->i_ino, objectid, 5805 dentry->d_parent->d_inode->i_ino, objectid,
@@ -5749,8 +5871,6 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
5749out_unlock: 5871out_unlock:
5750 nr = trans->blocks_used; 5872 nr = trans->blocks_used;
5751 btrfs_end_transaction_throttle(trans, root); 5873 btrfs_end_transaction_throttle(trans, root);
5752out_fail:
5753 btrfs_unreserve_metadata_space(root, 5);
5754 if (drop_inode) { 5874 if (drop_inode) {
5755 inode_dec_link_count(inode); 5875 inode_dec_link_count(inode);
5756 iput(inode); 5876 iput(inode);
@@ -5771,21 +5891,18 @@ static int prealloc_file_range(struct inode *inode, u64 start, u64 end,
5771 u64 i_size; 5891 u64 i_size;
5772 5892
5773 while (num_bytes > 0) { 5893 while (num_bytes > 0) {
5774 trans = btrfs_start_transaction(root, 1); 5894 trans = btrfs_start_transaction(root, 3);
5895 if (IS_ERR(trans)) {
5896 ret = PTR_ERR(trans);
5897 break;
5898 }
5775 5899
5776 ret = btrfs_reserve_extent(trans, root, num_bytes, 5900 ret = btrfs_reserve_extent(trans, root, num_bytes,
5777 root->sectorsize, 0, alloc_hint, 5901 root->sectorsize, 0, alloc_hint,
5778 (u64)-1, &ins, 1); 5902 (u64)-1, &ins, 1);
5779 if (ret) { 5903 if (ret) {
5780 WARN_ON(1); 5904 btrfs_end_transaction(trans, root);
5781 goto stop_trans; 5905 break;
5782 }
5783
5784 ret = btrfs_reserve_metadata_space(root, 3);
5785 if (ret) {
5786 btrfs_free_reserved_extent(root, ins.objectid,
5787 ins.offset);
5788 goto stop_trans;
5789 } 5906 }
5790 5907
5791 ret = insert_reserved_file_extent(trans, inode, 5908 ret = insert_reserved_file_extent(trans, inode,
@@ -5819,14 +5936,8 @@ static int prealloc_file_range(struct inode *inode, u64 start, u64 end,
5819 BUG_ON(ret); 5936 BUG_ON(ret);
5820 5937
5821 btrfs_end_transaction(trans, root); 5938 btrfs_end_transaction(trans, root);
5822 btrfs_unreserve_metadata_space(root, 3);
5823 } 5939 }
5824 return ret; 5940 return ret;
5825
5826stop_trans:
5827 btrfs_end_transaction(trans, root);
5828 return ret;
5829
5830} 5941}
5831 5942
5832static long btrfs_fallocate(struct inode *inode, int mode, 5943static long btrfs_fallocate(struct inode *inode, int mode,
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index 97a97839a867..3066da468c6d 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -239,23 +239,19 @@ static noinline int create_subvol(struct btrfs_root *root,
239 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; 239 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
240 u64 index = 0; 240 u64 index = 0;
241 241
242 ret = btrfs_find_free_objectid(NULL, root->fs_info->tree_root,
243 0, &objectid);
244 if (ret)
245 return ret;
242 /* 246 /*
243 * 1 - inode item 247 * 1 - inode item
244 * 2 - refs 248 * 2 - refs
245 * 1 - root item 249 * 1 - root item
246 * 2 - dir items 250 * 2 - dir items
247 */ 251 */
248 ret = btrfs_reserve_metadata_space(root, 6); 252 trans = btrfs_start_transaction(root, 6);
249 if (ret) 253 if (IS_ERR(trans))
250 return ret; 254 return PTR_ERR(trans);
251
252 trans = btrfs_start_transaction(root, 1);
253 BUG_ON(!trans);
254
255 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
256 0, &objectid);
257 if (ret)
258 goto fail;
259 255
260 leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 256 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
261 0, objectid, NULL, 0, 0, 0); 257 0, objectid, NULL, 0, 0, 0);
@@ -345,13 +341,10 @@ fail:
345 err = btrfs_commit_transaction(trans, root); 341 err = btrfs_commit_transaction(trans, root);
346 if (err && !ret) 342 if (err && !ret)
347 ret = err; 343 ret = err;
348
349 btrfs_unreserve_metadata_space(root, 6);
350 return ret; 344 return ret;
351} 345}
352 346
353static int create_snapshot(struct btrfs_root *root, struct dentry *dentry, 347static int create_snapshot(struct btrfs_root *root, struct dentry *dentry)
354 char *name, int namelen)
355{ 348{
356 struct inode *inode; 349 struct inode *inode;
357 struct btrfs_pending_snapshot *pending_snapshot; 350 struct btrfs_pending_snapshot *pending_snapshot;
@@ -361,40 +354,33 @@ static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
361 if (!root->ref_cows) 354 if (!root->ref_cows)
362 return -EINVAL; 355 return -EINVAL;
363 356
364 /*
365 * 1 - inode item
366 * 2 - refs
367 * 1 - root item
368 * 2 - dir items
369 */
370 ret = btrfs_reserve_metadata_space(root, 6);
371 if (ret)
372 goto fail;
373
374 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS); 357 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
375 if (!pending_snapshot) { 358 if (!pending_snapshot)
376 ret = -ENOMEM; 359 return -ENOMEM;
377 btrfs_unreserve_metadata_space(root, 6); 360
378 goto fail; 361 btrfs_init_block_rsv(&pending_snapshot->block_rsv);
379 }
380 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
381 if (!pending_snapshot->name) {
382 ret = -ENOMEM;
383 kfree(pending_snapshot);
384 btrfs_unreserve_metadata_space(root, 6);
385 goto fail;
386 }
387 memcpy(pending_snapshot->name, name, namelen);
388 pending_snapshot->name[namelen] = '\0';
389 pending_snapshot->dentry = dentry; 362 pending_snapshot->dentry = dentry;
390 trans = btrfs_start_transaction(root, 1);
391 BUG_ON(!trans);
392 pending_snapshot->root = root; 363 pending_snapshot->root = root;
364
365 trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
366 if (IS_ERR(trans)) {
367 ret = PTR_ERR(trans);
368 goto fail;
369 }
370
371 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
372 BUG_ON(ret);
373
393 list_add(&pending_snapshot->list, 374 list_add(&pending_snapshot->list,
394 &trans->transaction->pending_snapshots); 375 &trans->transaction->pending_snapshots);
395 ret = btrfs_commit_transaction(trans, root); 376 ret = btrfs_commit_transaction(trans, root->fs_info->extent_root);
396 BUG_ON(ret); 377 BUG_ON(ret);
397 btrfs_unreserve_metadata_space(root, 6); 378
379 ret = pending_snapshot->error;
380 if (ret)
381 goto fail;
382
383 btrfs_orphan_cleanup(pending_snapshot->snap);
398 384
399 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry); 385 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
400 if (IS_ERR(inode)) { 386 if (IS_ERR(inode)) {
@@ -405,6 +391,7 @@ static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
405 d_instantiate(dentry, inode); 391 d_instantiate(dentry, inode);
406 ret = 0; 392 ret = 0;
407fail: 393fail:
394 kfree(pending_snapshot);
408 return ret; 395 return ret;
409} 396}
410 397
@@ -456,8 +443,7 @@ static noinline int btrfs_mksubvol(struct path *parent,
456 goto out_up_read; 443 goto out_up_read;
457 444
458 if (snap_src) { 445 if (snap_src) {
459 error = create_snapshot(snap_src, dentry, 446 error = create_snapshot(snap_src, dentry);
460 name, namelen);
461 } else { 447 } else {
462 error = create_subvol(BTRFS_I(dir)->root, dentry, 448 error = create_subvol(BTRFS_I(dir)->root, dentry,
463 name, namelen); 449 name, namelen);
@@ -811,7 +797,7 @@ static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
811 device->name, (unsigned long long)new_size); 797 device->name, (unsigned long long)new_size);
812 798
813 if (new_size > old_size) { 799 if (new_size > old_size) {
814 trans = btrfs_start_transaction(root, 1); 800 trans = btrfs_start_transaction(root, 0);
815 ret = btrfs_grow_device(trans, device, new_size); 801 ret = btrfs_grow_device(trans, device, new_size);
816 btrfs_commit_transaction(trans, root); 802 btrfs_commit_transaction(trans, root);
817 } else { 803 } else {
@@ -1300,7 +1286,13 @@ static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1300 if (err) 1286 if (err)
1301 goto out_up_write; 1287 goto out_up_write;
1302 1288
1303 trans = btrfs_start_transaction(root, 1); 1289 trans = btrfs_start_transaction(root, 0);
1290 if (IS_ERR(trans)) {
1291 err = PTR_ERR(trans);
1292 goto out;
1293 }
1294 trans->block_rsv = &root->fs_info->global_block_rsv;
1295
1304 ret = btrfs_unlink_subvol(trans, root, dir, 1296 ret = btrfs_unlink_subvol(trans, root, dir,
1305 dest->root_key.objectid, 1297 dest->root_key.objectid,
1306 dentry->d_name.name, 1298 dentry->d_name.name,
@@ -1550,12 +1542,6 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
1550 btrfs_wait_ordered_range(src, off, off+len); 1542 btrfs_wait_ordered_range(src, off, off+len);
1551 } 1543 }
1552 1544
1553 trans = btrfs_start_transaction(root, 1);
1554 BUG_ON(!trans);
1555
1556 /* punch hole in destination first */
1557 btrfs_drop_extents(trans, inode, off, off + len, &hint_byte, 1);
1558
1559 /* clone data */ 1545 /* clone data */
1560 key.objectid = src->i_ino; 1546 key.objectid = src->i_ino;
1561 key.type = BTRFS_EXTENT_DATA_KEY; 1547 key.type = BTRFS_EXTENT_DATA_KEY;
@@ -1566,7 +1552,7 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
1566 * note the key will change type as we walk through the 1552 * note the key will change type as we walk through the
1567 * tree. 1553 * tree.
1568 */ 1554 */
1569 ret = btrfs_search_slot(trans, root, &key, path, 0, 0); 1555 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1570 if (ret < 0) 1556 if (ret < 0)
1571 goto out; 1557 goto out;
1572 1558
@@ -1629,12 +1615,31 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
1629 new_key.objectid = inode->i_ino; 1615 new_key.objectid = inode->i_ino;
1630 new_key.offset = key.offset + destoff - off; 1616 new_key.offset = key.offset + destoff - off;
1631 1617
1618 trans = btrfs_start_transaction(root, 1);
1619 if (IS_ERR(trans)) {
1620 ret = PTR_ERR(trans);
1621 goto out;
1622 }
1623
1632 if (type == BTRFS_FILE_EXTENT_REG || 1624 if (type == BTRFS_FILE_EXTENT_REG ||
1633 type == BTRFS_FILE_EXTENT_PREALLOC) { 1625 type == BTRFS_FILE_EXTENT_PREALLOC) {
1626 if (off > key.offset) {
1627 datao += off - key.offset;
1628 datal -= off - key.offset;
1629 }
1630
1631 if (key.offset + datal > off + len)
1632 datal = off + len - key.offset;
1633
1634 ret = btrfs_drop_extents(trans, inode,
1635 new_key.offset,
1636 new_key.offset + datal,
1637 &hint_byte, 1);
1638 BUG_ON(ret);
1639
1634 ret = btrfs_insert_empty_item(trans, root, path, 1640 ret = btrfs_insert_empty_item(trans, root, path,
1635 &new_key, size); 1641 &new_key, size);
1636 if (ret) 1642 BUG_ON(ret);
1637 goto out;
1638 1643
1639 leaf = path->nodes[0]; 1644 leaf = path->nodes[0];
1640 slot = path->slots[0]; 1645 slot = path->slots[0];
@@ -1645,14 +1650,6 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
1645 extent = btrfs_item_ptr(leaf, slot, 1650 extent = btrfs_item_ptr(leaf, slot,
1646 struct btrfs_file_extent_item); 1651 struct btrfs_file_extent_item);
1647 1652
1648 if (off > key.offset) {
1649 datao += off - key.offset;
1650 datal -= off - key.offset;
1651 }
1652
1653 if (key.offset + datal > off + len)
1654 datal = off + len - key.offset;
1655
1656 /* disko == 0 means it's a hole */ 1653 /* disko == 0 means it's a hole */
1657 if (!disko) 1654 if (!disko)
1658 datao = 0; 1655 datao = 0;
@@ -1683,14 +1680,21 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
1683 1680
1684 if (comp && (skip || trim)) { 1681 if (comp && (skip || trim)) {
1685 ret = -EINVAL; 1682 ret = -EINVAL;
1683 btrfs_end_transaction(trans, root);
1686 goto out; 1684 goto out;
1687 } 1685 }
1688 size -= skip + trim; 1686 size -= skip + trim;
1689 datal -= skip + trim; 1687 datal -= skip + trim;
1688
1689 ret = btrfs_drop_extents(trans, inode,
1690 new_key.offset,
1691 new_key.offset + datal,
1692 &hint_byte, 1);
1693 BUG_ON(ret);
1694
1690 ret = btrfs_insert_empty_item(trans, root, path, 1695 ret = btrfs_insert_empty_item(trans, root, path,
1691 &new_key, size); 1696 &new_key, size);
1692 if (ret) 1697 BUG_ON(ret);
1693 goto out;
1694 1698
1695 if (skip) { 1699 if (skip) {
1696 u32 start = 1700 u32 start =
@@ -1708,8 +1712,17 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
1708 } 1712 }
1709 1713
1710 btrfs_mark_buffer_dirty(leaf); 1714 btrfs_mark_buffer_dirty(leaf);
1711 } 1715 btrfs_release_path(root, path);
1712 1716
1717 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1718 if (new_key.offset + datal > inode->i_size)
1719 btrfs_i_size_write(inode,
1720 new_key.offset + datal);
1721 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
1722 ret = btrfs_update_inode(trans, root, inode);
1723 BUG_ON(ret);
1724 btrfs_end_transaction(trans, root);
1725 }
1713next: 1726next:
1714 btrfs_release_path(root, path); 1727 btrfs_release_path(root, path);
1715 key.offset++; 1728 key.offset++;
@@ -1717,17 +1730,7 @@ next:
1717 ret = 0; 1730 ret = 0;
1718out: 1731out:
1719 btrfs_release_path(root, path); 1732 btrfs_release_path(root, path);
1720 if (ret == 0) {
1721 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1722 if (destoff + olen > inode->i_size)
1723 btrfs_i_size_write(inode, destoff + olen);
1724 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
1725 ret = btrfs_update_inode(trans, root, inode);
1726 }
1727 btrfs_end_transaction(trans, root);
1728 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS); 1733 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1729 if (ret)
1730 vmtruncate(inode, 0);
1731out_unlock: 1734out_unlock:
1732 mutex_unlock(&src->i_mutex); 1735 mutex_unlock(&src->i_mutex);
1733 mutex_unlock(&inode->i_mutex); 1736 mutex_unlock(&inode->i_mutex);
diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c
index d565b45a6352..145a468c300d 100644
--- a/fs/btrfs/relocation.c
+++ b/fs/btrfs/relocation.c
@@ -1649,7 +1649,7 @@ static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
1649 } 1649 }
1650 1650
1651 if (level == 0 && rc->stage == UPDATE_DATA_PTRS) { 1651 if (level == 0 && rc->stage == UPDATE_DATA_PTRS) {
1652 trans = btrfs_start_transaction(root, 1); 1652 trans = btrfs_start_transaction(root, 0);
1653 1653
1654 leaf = path->nodes[0]; 1654 leaf = path->nodes[0];
1655 btrfs_item_key_to_cpu(leaf, &key, 0); 1655 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 while (1) { 1675 while (1) {
1676 leaf = NULL; 1676 leaf = NULL;
1677 replaced = 0; 1677 replaced = 0;
1678 trans = btrfs_start_transaction(root, 1); 1678 trans = btrfs_start_transaction(root, 0);
1679 max_level = level; 1679 max_level = level;
1680 1680
1681 ret = walk_down_reloc_tree(reloc_root, path, &level); 1681 ret = walk_down_reloc_tree(reloc_root, path, &level);
@@ -1803,7 +1803,7 @@ static void merge_func(struct btrfs_work *work)
1803 1803
1804 merge_reloc_root(async->rc, root); 1804 merge_reloc_root(async->rc, root);
1805 1805
1806 trans = btrfs_start_transaction(root, 1); 1806 trans = btrfs_start_transaction(root, 0);
1807 btrfs_update_reloc_root(trans, root); 1807 btrfs_update_reloc_root(trans, root);
1808 btrfs_end_transaction(trans, root); 1808 btrfs_end_transaction(trans, root);
1809 } 1809 }
@@ -3297,11 +3297,11 @@ static noinline_for_stack int relocate_block_group(struct reloc_control *rc)
3297 rc->create_reloc_root = 1; 3297 rc->create_reloc_root = 1;
3298 set_reloc_control(rc); 3298 set_reloc_control(rc);
3299 3299
3300 trans = btrfs_start_transaction(rc->extent_root, 1); 3300 trans = btrfs_join_transaction(rc->extent_root, 1);
3301 btrfs_commit_transaction(trans, rc->extent_root); 3301 btrfs_commit_transaction(trans, rc->extent_root);
3302 3302
3303 while (1) { 3303 while (1) {
3304 trans = btrfs_start_transaction(rc->extent_root, 1); 3304 trans = btrfs_start_transaction(rc->extent_root, 0);
3305 3305
3306 ret = find_next_extent(trans, rc, path); 3306 ret = find_next_extent(trans, rc, path);
3307 if (ret < 0) 3307 if (ret < 0)
@@ -3411,7 +3411,7 @@ static noinline_for_stack int relocate_block_group(struct reloc_control *rc)
3411 smp_mb(); 3411 smp_mb();
3412 3412
3413 if (rc->extents_found > 0) { 3413 if (rc->extents_found > 0) {
3414 trans = btrfs_start_transaction(rc->extent_root, 1); 3414 trans = btrfs_join_transaction(rc->extent_root, 1);
3415 btrfs_commit_transaction(trans, rc->extent_root); 3415 btrfs_commit_transaction(trans, rc->extent_root);
3416 } 3416 }
3417 3417
@@ -3420,7 +3420,7 @@ static noinline_for_stack int relocate_block_group(struct reloc_control *rc)
3420 unset_reloc_control(rc); 3420 unset_reloc_control(rc);
3421 3421
3422 /* get rid of pinned extents */ 3422 /* get rid of pinned extents */
3423 trans = btrfs_start_transaction(rc->extent_root, 1); 3423 trans = btrfs_join_transaction(rc->extent_root, 1);
3424 btrfs_commit_transaction(trans, rc->extent_root); 3424 btrfs_commit_transaction(trans, rc->extent_root);
3425 3425
3426 return err; 3426 return err;
@@ -3475,7 +3475,7 @@ static struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
3475 if (IS_ERR(root)) 3475 if (IS_ERR(root))
3476 return ERR_CAST(root); 3476 return ERR_CAST(root);
3477 3477
3478 trans = btrfs_start_transaction(root, 1); 3478 trans = btrfs_start_transaction(root, 6);
3479 BUG_ON(!trans); 3479 BUG_ON(!trans);
3480 3480
3481 err = btrfs_find_free_objectid(trans, root, objectid, &objectid); 3481 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 struct btrfs_trans_handle *trans; 3619 struct btrfs_trans_handle *trans;
3620 int ret; 3620 int ret;
3621 3621
3622 trans = btrfs_start_transaction(root->fs_info->tree_root, 1); 3622 trans = btrfs_start_transaction(root->fs_info->tree_root, 0);
3623 3623
3624 memset(&root->root_item.drop_progress, 0, 3624 memset(&root->root_item.drop_progress, 0,
3625 sizeof(root->root_item.drop_progress)); 3625 sizeof(root->root_item.drop_progress));
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index 55265c24ab96..38d91c7e8db2 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -498,7 +498,7 @@ int btrfs_sync_fs(struct super_block *sb, int wait)
498 btrfs_start_delalloc_inodes(root, 0); 498 btrfs_start_delalloc_inodes(root, 0);
499 btrfs_wait_ordered_extents(root, 0, 0); 499 btrfs_wait_ordered_extents(root, 0, 0);
500 500
501 trans = btrfs_start_transaction(root, 1); 501 trans = btrfs_start_transaction(root, 0);
502 ret = btrfs_commit_transaction(trans, root); 502 ret = btrfs_commit_transaction(trans, root);
503 return ret; 503 return ret;
504} 504}
diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c
index 21ad37c05199..2616491a5c5b 100644
--- a/fs/btrfs/transaction.c
+++ b/fs/btrfs/transaction.c
@@ -165,53 +165,89 @@ enum btrfs_trans_type {
165 TRANS_USERSPACE, 165 TRANS_USERSPACE,
166}; 166};
167 167
168static int may_wait_transaction(struct btrfs_root *root, int type)
169{
170 if (!root->fs_info->log_root_recovering &&
171 ((type == TRANS_START && !root->fs_info->open_ioctl_trans) ||
172 type == TRANS_USERSPACE))
173 return 1;
174 return 0;
175}
176
168static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root, 177static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
169 int num_blocks, int type) 178 u64 num_items, int type)
170{ 179{
171 struct btrfs_trans_handle *h = 180 struct btrfs_trans_handle *h;
172 kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); 181 struct btrfs_transaction *cur_trans;
182 int retries = 0;
173 int ret; 183 int ret;
184again:
185 h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
186 if (!h)
187 return ERR_PTR(-ENOMEM);
174 188
175 mutex_lock(&root->fs_info->trans_mutex); 189 mutex_lock(&root->fs_info->trans_mutex);
176 if (!root->fs_info->log_root_recovering && 190 if (may_wait_transaction(root, type))
177 ((type == TRANS_START && !root->fs_info->open_ioctl_trans) ||
178 type == TRANS_USERSPACE))
179 wait_current_trans(root); 191 wait_current_trans(root);
192
180 ret = join_transaction(root); 193 ret = join_transaction(root);
181 BUG_ON(ret); 194 BUG_ON(ret);
182 195
183 h->transid = root->fs_info->running_transaction->transid; 196 cur_trans = root->fs_info->running_transaction;
184 h->transaction = root->fs_info->running_transaction; 197 cur_trans->use_count++;
185 h->blocks_reserved = num_blocks; 198 mutex_unlock(&root->fs_info->trans_mutex);
199
200 h->transid = cur_trans->transid;
201 h->transaction = cur_trans;
186 h->blocks_used = 0; 202 h->blocks_used = 0;
187 h->block_group = 0; 203 h->block_group = 0;
204 h->bytes_reserved = 0;
188 h->delayed_ref_updates = 0; 205 h->delayed_ref_updates = 0;
189 h->block_rsv = NULL; 206 h->block_rsv = NULL;
190 207
191 if (!current->journal_info && type != TRANS_USERSPACE) 208 smp_mb();
192 current->journal_info = h; 209 if (cur_trans->blocked && may_wait_transaction(root, type)) {
210 btrfs_commit_transaction(h, root);
211 goto again;
212 }
213
214 if (num_items > 0) {
215 ret = btrfs_trans_reserve_metadata(h, root, num_items,
216 &retries);
217 if (ret == -EAGAIN) {
218 btrfs_commit_transaction(h, root);
219 goto again;
220 }
221 if (ret < 0) {
222 btrfs_end_transaction(h, root);
223 return ERR_PTR(ret);
224 }
225 }
193 226
194 root->fs_info->running_transaction->use_count++; 227 mutex_lock(&root->fs_info->trans_mutex);
195 record_root_in_trans(h, root); 228 record_root_in_trans(h, root);
196 mutex_unlock(&root->fs_info->trans_mutex); 229 mutex_unlock(&root->fs_info->trans_mutex);
230
231 if (!current->journal_info && type != TRANS_USERSPACE)
232 current->journal_info = h;
197 return h; 233 return h;
198} 234}
199 235
200struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 236struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
201 int num_blocks) 237 int num_items)
202{ 238{
203 return start_transaction(root, num_blocks, TRANS_START); 239 return start_transaction(root, num_items, TRANS_START);
204} 240}
205struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root, 241struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
206 int num_blocks) 242 int num_blocks)
207{ 243{
208 return start_transaction(root, num_blocks, TRANS_JOIN); 244 return start_transaction(root, 0, TRANS_JOIN);
209} 245}
210 246
211struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r, 247struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
212 int num_blocks) 248 int num_blocks)
213{ 249{
214 return start_transaction(r, num_blocks, TRANS_USERSPACE); 250 return start_transaction(r, 0, TRANS_USERSPACE);
215} 251}
216 252
217/* wait for a transaction commit to be fully complete */ 253/* wait for a transaction commit to be fully complete */
@@ -312,6 +348,8 @@ static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
312 count++; 348 count++;
313 } 349 }
314 350
351 btrfs_trans_release_metadata(trans, root);
352
315 mutex_lock(&info->trans_mutex); 353 mutex_lock(&info->trans_mutex);
316 cur_trans = info->running_transaction; 354 cur_trans = info->running_transaction;
317 WARN_ON(cur_trans != trans->transaction); 355 WARN_ON(cur_trans != trans->transaction);
@@ -757,47 +795,49 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
757 struct btrfs_root *root = pending->root; 795 struct btrfs_root *root = pending->root;
758 struct btrfs_root *parent_root; 796 struct btrfs_root *parent_root;
759 struct inode *parent_inode; 797 struct inode *parent_inode;
798 struct dentry *dentry;
760 struct extent_buffer *tmp; 799 struct extent_buffer *tmp;
761 struct extent_buffer *old; 800 struct extent_buffer *old;
762 int ret; 801 int ret;
763 u64 objectid;
764 int namelen;
765 u64 index = 0; 802 u64 index = 0;
766 803 u64 objectid;
767 parent_inode = pending->dentry->d_parent->d_inode;
768 parent_root = BTRFS_I(parent_inode)->root;
769 804
770 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS); 805 new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
771 if (!new_root_item) { 806 if (!new_root_item) {
772 ret = -ENOMEM; 807 pending->error = -ENOMEM;
773 goto fail; 808 goto fail;
774 } 809 }
810
775 ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid); 811 ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
776 if (ret) 812 if (ret) {
813 pending->error = ret;
777 goto fail; 814 goto fail;
815 }
778 816
779 key.objectid = objectid; 817 key.objectid = objectid;
780 /* record when the snapshot was created in key.offset */ 818 key.offset = (u64)-1;
781 key.offset = trans->transid; 819 key.type = BTRFS_ROOT_ITEM_KEY;
782 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
783 820
784 memcpy(&pending->root_key, &key, sizeof(key)); 821 trans->block_rsv = &pending->block_rsv;
785 pending->root_key.offset = (u64)-1;
786 822
823 dentry = pending->dentry;
824 parent_inode = dentry->d_parent->d_inode;
825 parent_root = BTRFS_I(parent_inode)->root;
787 record_root_in_trans(trans, parent_root); 826 record_root_in_trans(trans, parent_root);
827
788 /* 828 /*
789 * insert the directory item 829 * insert the directory item
790 */ 830 */
791 namelen = strlen(pending->name);
792 ret = btrfs_set_inode_index(parent_inode, &index); 831 ret = btrfs_set_inode_index(parent_inode, &index);
793 BUG_ON(ret); 832 BUG_ON(ret);
794 ret = btrfs_insert_dir_item(trans, parent_root, 833 ret = btrfs_insert_dir_item(trans, parent_root,
795 pending->name, namelen, 834 dentry->d_name.name, dentry->d_name.len,
796 parent_inode->i_ino, 835 parent_inode->i_ino, &key,
797 &pending->root_key, BTRFS_FT_DIR, index); 836 BTRFS_FT_DIR, index);
798 BUG_ON(ret); 837 BUG_ON(ret);
799 838
800 btrfs_i_size_write(parent_inode, parent_inode->i_size + namelen * 2); 839 btrfs_i_size_write(parent_inode, parent_inode->i_size +
840 dentry->d_name.len * 2);
801 ret = btrfs_update_inode(trans, parent_root, parent_inode); 841 ret = btrfs_update_inode(trans, parent_root, parent_inode);
802 BUG_ON(ret); 842 BUG_ON(ret);
803 843
@@ -814,22 +854,29 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
814 free_extent_buffer(old); 854 free_extent_buffer(old);
815 855
816 btrfs_set_root_node(new_root_item, tmp); 856 btrfs_set_root_node(new_root_item, tmp);
817 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, 857 /* record when the snapshot was created in key.offset */
818 new_root_item); 858 key.offset = trans->transid;
819 BUG_ON(ret); 859 ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
820 btrfs_tree_unlock(tmp); 860 btrfs_tree_unlock(tmp);
821 free_extent_buffer(tmp); 861 free_extent_buffer(tmp);
862 BUG_ON(ret);
822 863
823 ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root, 864 /*
824 pending->root_key.objectid, 865 * insert root back/forward references
866 */
867 ret = btrfs_add_root_ref(trans, tree_root, objectid,
825 parent_root->root_key.objectid, 868 parent_root->root_key.objectid,
826 parent_inode->i_ino, index, pending->name, 869 parent_inode->i_ino, index,
827 namelen); 870 dentry->d_name.name, dentry->d_name.len);
828 BUG_ON(ret); 871 BUG_ON(ret);
829 872
873 key.offset = (u64)-1;
874 pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
875 BUG_ON(IS_ERR(pending->snap));
830fail: 876fail:
831 kfree(new_root_item); 877 kfree(new_root_item);
832 return ret; 878 btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1);
879 return 0;
833} 880}
834 881
835/* 882/*
@@ -898,6 +945,8 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
898 ret = btrfs_run_delayed_refs(trans, root, 0); 945 ret = btrfs_run_delayed_refs(trans, root, 0);
899 BUG_ON(ret); 946 BUG_ON(ret);
900 947
948 btrfs_trans_release_metadata(trans, root);
949
901 cur_trans = trans->transaction; 950 cur_trans = trans->transaction;
902 /* 951 /*
903 * set the flushing flag so procs in this transaction have to 952 * set the flushing flag so procs in this transaction have to
diff --git a/fs/btrfs/transaction.h b/fs/btrfs/transaction.h
index 56e728d70e39..14b3841b75d5 100644
--- a/fs/btrfs/transaction.h
+++ b/fs/btrfs/transaction.h
@@ -57,8 +57,11 @@ struct btrfs_trans_handle {
57struct btrfs_pending_snapshot { 57struct btrfs_pending_snapshot {
58 struct dentry *dentry; 58 struct dentry *dentry;
59 struct btrfs_root *root; 59 struct btrfs_root *root;
60 char *name; 60 struct btrfs_root *snap;
61 struct btrfs_key root_key; 61 /* block reservation for the operation */
62 struct btrfs_block_rsv block_rsv;
63 /* extra metadata reseration for relocation */
64 int error;
62 struct list_head list; 65 struct list_head list;
63}; 66};
64 67
@@ -85,11 +88,11 @@ static inline void btrfs_set_inode_last_trans(struct btrfs_trans_handle *trans,
85int btrfs_end_transaction(struct btrfs_trans_handle *trans, 88int btrfs_end_transaction(struct btrfs_trans_handle *trans,
86 struct btrfs_root *root); 89 struct btrfs_root *root);
87struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 90struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
88 int num_blocks); 91 int num_items);
89struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root, 92struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
90 int num_blocks); 93 int num_blocks);
91struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r, 94struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
92 int num_blocks); 95 int num_blocks);
93int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, 96int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
94 struct btrfs_root *root); 97 struct btrfs_root *root);
95int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans, 98int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 8db7b14bbae8..d6e3af8be95b 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -1097,7 +1097,7 @@ static int btrfs_rm_dev_item(struct btrfs_root *root,
1097 if (!path) 1097 if (!path)
1098 return -ENOMEM; 1098 return -ENOMEM;
1099 1099
1100 trans = btrfs_start_transaction(root, 1); 1100 trans = btrfs_start_transaction(root, 0);
1101 key.objectid = BTRFS_DEV_ITEMS_OBJECTID; 1101 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1102 key.type = BTRFS_DEV_ITEM_KEY; 1102 key.type = BTRFS_DEV_ITEM_KEY;
1103 key.offset = device->devid; 1103 key.offset = device->devid;
@@ -1486,7 +1486,7 @@ int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1486 goto error; 1486 goto error;
1487 } 1487 }
1488 1488
1489 trans = btrfs_start_transaction(root, 1); 1489 trans = btrfs_start_transaction(root, 0);
1490 lock_chunks(root); 1490 lock_chunks(root);
1491 1491
1492 device->barriers = 1; 1492 device->barriers = 1;
@@ -1751,9 +1751,10 @@ static int btrfs_relocate_chunk(struct btrfs_root *root,
1751 1751
1752 /* step one, relocate all the extents inside this chunk */ 1752 /* step one, relocate all the extents inside this chunk */
1753 ret = btrfs_relocate_block_group(extent_root, chunk_offset); 1753 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
1754 BUG_ON(ret); 1754 if (ret)
1755 return ret;
1755 1756
1756 trans = btrfs_start_transaction(root, 1); 1757 trans = btrfs_start_transaction(root, 0);
1757 BUG_ON(!trans); 1758 BUG_ON(!trans);
1758 1759
1759 lock_chunks(root); 1760 lock_chunks(root);
@@ -1925,7 +1926,7 @@ int btrfs_balance(struct btrfs_root *dev_root)
1925 break; 1926 break;
1926 BUG_ON(ret); 1927 BUG_ON(ret);
1927 1928
1928 trans = btrfs_start_transaction(dev_root, 1); 1929 trans = btrfs_start_transaction(dev_root, 0);
1929 BUG_ON(!trans); 1930 BUG_ON(!trans);
1930 1931
1931 ret = btrfs_grow_device(trans, device, old_size); 1932 ret = btrfs_grow_device(trans, device, old_size);
@@ -2094,11 +2095,7 @@ again:
2094 } 2095 }
2095 2096
2096 /* Shrinking succeeded, else we would be at "done". */ 2097 /* Shrinking succeeded, else we would be at "done". */
2097 trans = btrfs_start_transaction(root, 1); 2098 trans = btrfs_start_transaction(root, 0);
2098 if (!trans) {
2099 ret = -ENOMEM;
2100 goto done;
2101 }
2102 lock_chunks(root); 2099 lock_chunks(root);
2103 2100
2104 device->disk_total_bytes = new_size; 2101 device->disk_total_bytes = new_size;
diff --git a/fs/btrfs/xattr.c b/fs/btrfs/xattr.c
index 193b58f7d3f3..007fae581a04 100644
--- a/fs/btrfs/xattr.c
+++ b/fs/btrfs/xattr.c
@@ -154,15 +154,10 @@ int __btrfs_setxattr(struct btrfs_trans_handle *trans,
154 if (trans) 154 if (trans)
155 return do_setxattr(trans, inode, name, value, size, flags); 155 return do_setxattr(trans, inode, name, value, size, flags);
156 156
157 ret = btrfs_reserve_metadata_space(root, 2); 157 trans = btrfs_start_transaction(root, 2);
158 if (ret) 158 if (IS_ERR(trans))
159 return ret; 159 return PTR_ERR(trans);
160 160
161 trans = btrfs_start_transaction(root, 1);
162 if (!trans) {
163 ret = -ENOMEM;
164 goto out;
165 }
166 btrfs_set_trans_block_group(trans, inode); 161 btrfs_set_trans_block_group(trans, inode);
167 162
168 ret = do_setxattr(trans, inode, name, value, size, flags); 163 ret = do_setxattr(trans, inode, name, value, size, flags);
@@ -174,7 +169,6 @@ int __btrfs_setxattr(struct btrfs_trans_handle *trans,
174 BUG_ON(ret); 169 BUG_ON(ret);
175out: 170out:
176 btrfs_end_transaction_throttle(trans, root); 171 btrfs_end_transaction_throttle(trans, root);
177 btrfs_unreserve_metadata_space(root, 2);
178 return ret; 172 return ret;
179} 173}
180 174