aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs
diff options
context:
space:
mode:
Diffstat (limited to 'fs/btrfs')
-rw-r--r--fs/btrfs/btrfs_inode.h8
-rw-r--r--fs/btrfs/ctree.c68
-rw-r--r--fs/btrfs/ctree.h51
-rw-r--r--fs/btrfs/disk-io.c50
-rw-r--r--fs/btrfs/disk-io.h10
-rw-r--r--fs/btrfs/extent-tree.c339
-rw-r--r--fs/btrfs/extent_io.c2
-rw-r--r--fs/btrfs/file.c24
-rw-r--r--fs/btrfs/inode-map.c1
-rw-r--r--fs/btrfs/inode.c66
-rw-r--r--fs/btrfs/ioctl.c6
-rw-r--r--fs/btrfs/locking.c17
-rw-r--r--fs/btrfs/locking.h2
-rw-r--r--fs/btrfs/super.c5
-rw-r--r--fs/btrfs/transaction.c2
-rw-r--r--fs/btrfs/tree-log.c2
-rw-r--r--fs/btrfs/volumes.c6
17 files changed, 482 insertions, 177 deletions
diff --git a/fs/btrfs/btrfs_inode.h b/fs/btrfs/btrfs_inode.h
index a8c9693b75ac..72677ce2b74f 100644
--- a/fs/btrfs/btrfs_inode.h
+++ b/fs/btrfs/btrfs_inode.h
@@ -66,6 +66,9 @@ struct btrfs_inode {
66 */ 66 */
67 struct list_head delalloc_inodes; 67 struct list_head delalloc_inodes;
68 68
69 /* the space_info for where this inode's data allocations are done */
70 struct btrfs_space_info *space_info;
71
69 /* full 64 bit generation number, struct vfs_inode doesn't have a big 72 /* full 64 bit generation number, struct vfs_inode doesn't have a big
70 * enough field for this. 73 * enough field for this.
71 */ 74 */
@@ -94,6 +97,11 @@ struct btrfs_inode {
94 */ 97 */
95 u64 delalloc_bytes; 98 u64 delalloc_bytes;
96 99
100 /* total number of bytes that may be used for this inode for
101 * delalloc
102 */
103 u64 reserved_bytes;
104
97 /* 105 /*
98 * the size of the file stored in the metadata on disk. data=ordered 106 * the size of the file stored in the metadata on disk. data=ordered
99 * means the in-memory i_size might be larger than the size on disk 107 * means the in-memory i_size might be larger than the size on disk
diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c
index 35443cc4b9a9..37f31b5529aa 100644
--- a/fs/btrfs/ctree.c
+++ b/fs/btrfs/ctree.c
@@ -38,19 +38,12 @@ static int balance_node_right(struct btrfs_trans_handle *trans,
38static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root, 38static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
39 struct btrfs_path *path, int level, int slot); 39 struct btrfs_path *path, int level, int slot);
40 40
41inline void btrfs_init_path(struct btrfs_path *p)
42{
43 memset(p, 0, sizeof(*p));
44}
45
46struct btrfs_path *btrfs_alloc_path(void) 41struct btrfs_path *btrfs_alloc_path(void)
47{ 42{
48 struct btrfs_path *path; 43 struct btrfs_path *path;
49 path = kmem_cache_alloc(btrfs_path_cachep, GFP_NOFS); 44 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
50 if (path) { 45 if (path)
51 btrfs_init_path(path);
52 path->reada = 1; 46 path->reada = 1;
53 }
54 return path; 47 return path;
55} 48}
56 49
@@ -69,14 +62,38 @@ noinline void btrfs_set_path_blocking(struct btrfs_path *p)
69 62
70/* 63/*
71 * reset all the locked nodes in the patch to spinning locks. 64 * reset all the locked nodes in the patch to spinning locks.
65 *
66 * held is used to keep lockdep happy, when lockdep is enabled
67 * we set held to a blocking lock before we go around and
68 * retake all the spinlocks in the path. You can safely use NULL
69 * for held
72 */ 70 */
73noinline void btrfs_clear_path_blocking(struct btrfs_path *p) 71noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
72 struct extent_buffer *held)
74{ 73{
75 int i; 74 int i;
76 for (i = 0; i < BTRFS_MAX_LEVEL; i++) { 75
76#ifdef CONFIG_DEBUG_LOCK_ALLOC
77 /* lockdep really cares that we take all of these spinlocks
78 * in the right order. If any of the locks in the path are not
79 * currently blocking, it is going to complain. So, make really
80 * really sure by forcing the path to blocking before we clear
81 * the path blocking.
82 */
83 if (held)
84 btrfs_set_lock_blocking(held);
85 btrfs_set_path_blocking(p);
86#endif
87
88 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
77 if (p->nodes[i] && p->locks[i]) 89 if (p->nodes[i] && p->locks[i])
78 btrfs_clear_lock_blocking(p->nodes[i]); 90 btrfs_clear_lock_blocking(p->nodes[i]);
79 } 91 }
92
93#ifdef CONFIG_DEBUG_LOCK_ALLOC
94 if (held)
95 btrfs_clear_lock_blocking(held);
96#endif
80} 97}
81 98
82/* this also releases the path */ 99/* this also releases the path */
@@ -260,7 +277,7 @@ static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
260 if (*cow_ret == buf) 277 if (*cow_ret == buf)
261 unlock_orig = 1; 278 unlock_orig = 1;
262 279
263 WARN_ON(!btrfs_tree_locked(buf)); 280 btrfs_assert_tree_locked(buf);
264 281
265 if (parent) 282 if (parent)
266 parent_start = parent->start; 283 parent_start = parent->start;
@@ -286,7 +303,7 @@ static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
286 trans->transid, level, &ins); 303 trans->transid, level, &ins);
287 BUG_ON(ret); 304 BUG_ON(ret);
288 cow = btrfs_init_new_buffer(trans, root, prealloc_dest, 305 cow = btrfs_init_new_buffer(trans, root, prealloc_dest,
289 buf->len); 306 buf->len, level);
290 } else { 307 } else {
291 cow = btrfs_alloc_free_block(trans, root, buf->len, 308 cow = btrfs_alloc_free_block(trans, root, buf->len,
292 parent_start, 309 parent_start,
@@ -917,9 +934,9 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,
917 934
918 /* promote the child to a root */ 935 /* promote the child to a root */
919 child = read_node_slot(root, mid, 0); 936 child = read_node_slot(root, mid, 0);
937 BUG_ON(!child);
920 btrfs_tree_lock(child); 938 btrfs_tree_lock(child);
921 btrfs_set_lock_blocking(child); 939 btrfs_set_lock_blocking(child);
922 BUG_ON(!child);
923 ret = btrfs_cow_block(trans, root, child, mid, 0, &child, 0); 940 ret = btrfs_cow_block(trans, root, child, mid, 0, &child, 0);
924 BUG_ON(ret); 941 BUG_ON(ret);
925 942
@@ -1566,7 +1583,7 @@ cow_done:
1566 if (!p->skip_locking) 1583 if (!p->skip_locking)
1567 p->locks[level] = 1; 1584 p->locks[level] = 1;
1568 1585
1569 btrfs_clear_path_blocking(p); 1586 btrfs_clear_path_blocking(p, NULL);
1570 1587
1571 /* 1588 /*
1572 * we have a lock on b and as long as we aren't changing 1589 * we have a lock on b and as long as we aren't changing
@@ -1605,7 +1622,7 @@ cow_done:
1605 1622
1606 btrfs_set_path_blocking(p); 1623 btrfs_set_path_blocking(p);
1607 sret = split_node(trans, root, p, level); 1624 sret = split_node(trans, root, p, level);
1608 btrfs_clear_path_blocking(p); 1625 btrfs_clear_path_blocking(p, NULL);
1609 1626
1610 BUG_ON(sret > 0); 1627 BUG_ON(sret > 0);
1611 if (sret) { 1628 if (sret) {
@@ -1625,7 +1642,7 @@ cow_done:
1625 1642
1626 btrfs_set_path_blocking(p); 1643 btrfs_set_path_blocking(p);
1627 sret = balance_level(trans, root, p, level); 1644 sret = balance_level(trans, root, p, level);
1628 btrfs_clear_path_blocking(p); 1645 btrfs_clear_path_blocking(p, NULL);
1629 1646
1630 if (sret) { 1647 if (sret) {
1631 ret = sret; 1648 ret = sret;
@@ -1688,13 +1705,13 @@ cow_done:
1688 if (!p->skip_locking) { 1705 if (!p->skip_locking) {
1689 int lret; 1706 int lret;
1690 1707
1691 btrfs_clear_path_blocking(p); 1708 btrfs_clear_path_blocking(p, NULL);
1692 lret = btrfs_try_spin_lock(b); 1709 lret = btrfs_try_spin_lock(b);
1693 1710
1694 if (!lret) { 1711 if (!lret) {
1695 btrfs_set_path_blocking(p); 1712 btrfs_set_path_blocking(p);
1696 btrfs_tree_lock(b); 1713 btrfs_tree_lock(b);
1697 btrfs_clear_path_blocking(p); 1714 btrfs_clear_path_blocking(p, b);
1698 } 1715 }
1699 } 1716 }
1700 } else { 1717 } else {
@@ -1706,7 +1723,7 @@ cow_done:
1706 btrfs_set_path_blocking(p); 1723 btrfs_set_path_blocking(p);
1707 sret = split_leaf(trans, root, key, 1724 sret = split_leaf(trans, root, key,
1708 p, ins_len, ret == 0); 1725 p, ins_len, ret == 0);
1709 btrfs_clear_path_blocking(p); 1726 btrfs_clear_path_blocking(p, NULL);
1710 1727
1711 BUG_ON(sret > 0); 1728 BUG_ON(sret > 0);
1712 if (sret) { 1729 if (sret) {
@@ -2348,7 +2365,7 @@ static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
2348 if (slot >= btrfs_header_nritems(upper) - 1) 2365 if (slot >= btrfs_header_nritems(upper) - 1)
2349 return 1; 2366 return 1;
2350 2367
2351 WARN_ON(!btrfs_tree_locked(path->nodes[1])); 2368 btrfs_assert_tree_locked(path->nodes[1]);
2352 2369
2353 right = read_node_slot(root, upper, slot + 1); 2370 right = read_node_slot(root, upper, slot + 1);
2354 btrfs_tree_lock(right); 2371 btrfs_tree_lock(right);
@@ -2545,7 +2562,7 @@ static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
2545 if (right_nritems == 0) 2562 if (right_nritems == 0)
2546 return 1; 2563 return 1;
2547 2564
2548 WARN_ON(!btrfs_tree_locked(path->nodes[1])); 2565 btrfs_assert_tree_locked(path->nodes[1]);
2549 2566
2550 left = read_node_slot(root, path->nodes[1], slot - 1); 2567 left = read_node_slot(root, path->nodes[1], slot - 1);
2551 btrfs_tree_lock(left); 2568 btrfs_tree_lock(left);
@@ -3926,7 +3943,6 @@ find_next_key:
3926 btrfs_release_path(root, path); 3943 btrfs_release_path(root, path);
3927 goto again; 3944 goto again;
3928 } else { 3945 } else {
3929 btrfs_clear_path_blocking(path);
3930 goto out; 3946 goto out;
3931 } 3947 }
3932 } 3948 }
@@ -3946,7 +3962,7 @@ find_next_key:
3946 path->locks[level - 1] = 1; 3962 path->locks[level - 1] = 1;
3947 path->nodes[level - 1] = cur; 3963 path->nodes[level - 1] = cur;
3948 unlock_up(path, level, 1); 3964 unlock_up(path, level, 1);
3949 btrfs_clear_path_blocking(path); 3965 btrfs_clear_path_blocking(path, NULL);
3950 } 3966 }
3951out: 3967out:
3952 if (ret == 0) 3968 if (ret == 0)
@@ -4085,7 +4101,7 @@ int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
4085 4101
4086 next = read_node_slot(root, c, slot); 4102 next = read_node_slot(root, c, slot);
4087 if (!path->skip_locking) { 4103 if (!path->skip_locking) {
4088 WARN_ON(!btrfs_tree_locked(c)); 4104 btrfs_assert_tree_locked(c);
4089 btrfs_tree_lock(next); 4105 btrfs_tree_lock(next);
4090 btrfs_set_lock_blocking(next); 4106 btrfs_set_lock_blocking(next);
4091 } 4107 }
@@ -4110,7 +4126,7 @@ int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
4110 reada_for_search(root, path, level, slot, 0); 4126 reada_for_search(root, path, level, slot, 0);
4111 next = read_node_slot(root, next, 0); 4127 next = read_node_slot(root, next, 0);
4112 if (!path->skip_locking) { 4128 if (!path->skip_locking) {
4113 WARN_ON(!btrfs_tree_locked(path->nodes[level])); 4129 btrfs_assert_tree_locked(path->nodes[level]);
4114 btrfs_tree_lock(next); 4130 btrfs_tree_lock(next);
4115 btrfs_set_lock_blocking(next); 4131 btrfs_set_lock_blocking(next);
4116 } 4132 }
diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index 531db112c8bd..82491ba8fa40 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -43,11 +43,7 @@ struct btrfs_ordered_sum;
43 43
44#define BTRFS_ACL_NOT_CACHED ((void *)-1) 44#define BTRFS_ACL_NOT_CACHED ((void *)-1)
45 45
46#ifdef CONFIG_LOCKDEP 46#define BTRFS_MAX_LEVEL 8
47# define BTRFS_MAX_LEVEL 7
48#else
49# define BTRFS_MAX_LEVEL 8
50#endif
51 47
52/* holds pointers to all of the tree roots */ 48/* holds pointers to all of the tree roots */
53#define BTRFS_ROOT_TREE_OBJECTID 1ULL 49#define BTRFS_ROOT_TREE_OBJECTID 1ULL
@@ -600,13 +596,27 @@ struct btrfs_block_group_item {
600 596
601struct btrfs_space_info { 597struct btrfs_space_info {
602 u64 flags; 598 u64 flags;
603 u64 total_bytes; 599
604 u64 bytes_used; 600 u64 total_bytes; /* total bytes in the space */
605 u64 bytes_pinned; 601 u64 bytes_used; /* total bytes used on disk */
606 u64 bytes_reserved; 602 u64 bytes_pinned; /* total bytes pinned, will be freed when the
607 u64 bytes_readonly; 603 transaction finishes */
608 int full; 604 u64 bytes_reserved; /* total bytes the allocator has reserved for
609 int force_alloc; 605 current allocations */
606 u64 bytes_readonly; /* total bytes that are read only */
607
608 /* delalloc accounting */
609 u64 bytes_delalloc; /* number of bytes reserved for allocation,
610 this space is not necessarily reserved yet
611 by the allocator */
612 u64 bytes_may_use; /* number of bytes that may be used for
613 delalloc */
614
615 int full; /* indicates that we cannot allocate any more
616 chunks for this space */
617 int force_alloc; /* set if we need to force a chunk alloc for
618 this space */
619
610 struct list_head list; 620 struct list_head list;
611 621
612 /* for block groups in our same type */ 622 /* for block groups in our same type */
@@ -1715,7 +1725,8 @@ struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1715 u64 empty_size); 1725 u64 empty_size);
1716struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans, 1726struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
1717 struct btrfs_root *root, 1727 struct btrfs_root *root,
1718 u64 bytenr, u32 blocksize); 1728 u64 bytenr, u32 blocksize,
1729 int level);
1719int btrfs_alloc_extent(struct btrfs_trans_handle *trans, 1730int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1720 struct btrfs_root *root, 1731 struct btrfs_root *root,
1721 u64 num_bytes, u64 parent, u64 min_bytes, 1732 u64 num_bytes, u64 parent, u64 min_bytes,
@@ -1785,6 +1796,16 @@ int btrfs_add_dead_reloc_root(struct btrfs_root *root);
1785int btrfs_cleanup_reloc_trees(struct btrfs_root *root); 1796int btrfs_cleanup_reloc_trees(struct btrfs_root *root);
1786int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len); 1797int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len);
1787u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags); 1798u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags);
1799void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *ionde);
1800int btrfs_check_metadata_free_space(struct btrfs_root *root);
1801int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
1802 u64 bytes);
1803void btrfs_free_reserved_data_space(struct btrfs_root *root,
1804 struct inode *inode, u64 bytes);
1805void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode,
1806 u64 bytes);
1807void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
1808 u64 bytes);
1788/* ctree.c */ 1809/* ctree.c */
1789int btrfs_previous_item(struct btrfs_root *root, 1810int btrfs_previous_item(struct btrfs_root *root,
1790 struct btrfs_path *path, u64 min_objectid, 1811 struct btrfs_path *path, u64 min_objectid,
@@ -1834,9 +1855,7 @@ int btrfs_realloc_node(struct btrfs_trans_handle *trans,
1834void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p); 1855void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
1835struct btrfs_path *btrfs_alloc_path(void); 1856struct btrfs_path *btrfs_alloc_path(void);
1836void btrfs_free_path(struct btrfs_path *p); 1857void btrfs_free_path(struct btrfs_path *p);
1837void btrfs_init_path(struct btrfs_path *p);
1838void btrfs_set_path_blocking(struct btrfs_path *p); 1858void btrfs_set_path_blocking(struct btrfs_path *p);
1839void btrfs_clear_path_blocking(struct btrfs_path *p);
1840void btrfs_unlock_up_safe(struct btrfs_path *p, int level); 1859void btrfs_unlock_up_safe(struct btrfs_path *p, int level);
1841 1860
1842int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root, 1861int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
@@ -2032,8 +2051,6 @@ int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
2032unsigned long btrfs_force_ra(struct address_space *mapping, 2051unsigned long btrfs_force_ra(struct address_space *mapping,
2033 struct file_ra_state *ra, struct file *file, 2052 struct file_ra_state *ra, struct file *file,
2034 pgoff_t offset, pgoff_t last_index); 2053 pgoff_t offset, pgoff_t last_index);
2035int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
2036 int for_del);
2037int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page); 2054int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page);
2038int btrfs_readpage(struct file *file, struct page *page); 2055int btrfs_readpage(struct file *file, struct page *page);
2039void btrfs_delete_inode(struct inode *inode); 2056void btrfs_delete_inode(struct inode *inode);
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 5aebddd71193..3e18175248e0 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -75,6 +75,40 @@ struct async_submit_bio {
75 struct btrfs_work work; 75 struct btrfs_work work;
76}; 76};
77 77
78/* These are used to set the lockdep class on the extent buffer locks.
79 * The class is set by the readpage_end_io_hook after the buffer has
80 * passed csum validation but before the pages are unlocked.
81 *
82 * The lockdep class is also set by btrfs_init_new_buffer on freshly
83 * allocated blocks.
84 *
85 * The class is based on the level in the tree block, which allows lockdep
86 * to know that lower nodes nest inside the locks of higher nodes.
87 *
88 * We also add a check to make sure the highest level of the tree is
89 * the same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this
90 * code needs update as well.
91 */
92#ifdef CONFIG_DEBUG_LOCK_ALLOC
93# if BTRFS_MAX_LEVEL != 8
94# error
95# endif
96static struct lock_class_key btrfs_eb_class[BTRFS_MAX_LEVEL + 1];
97static const char *btrfs_eb_name[BTRFS_MAX_LEVEL + 1] = {
98 /* leaf */
99 "btrfs-extent-00",
100 "btrfs-extent-01",
101 "btrfs-extent-02",
102 "btrfs-extent-03",
103 "btrfs-extent-04",
104 "btrfs-extent-05",
105 "btrfs-extent-06",
106 "btrfs-extent-07",
107 /* highest possible level */
108 "btrfs-extent-08",
109};
110#endif
111
78/* 112/*
79 * extents on the btree inode are pretty simple, there's one extent 113 * extents on the btree inode are pretty simple, there's one extent
80 * that covers the entire device 114 * that covers the entire device
@@ -347,6 +381,15 @@ static int check_tree_block_fsid(struct btrfs_root *root,
347 return ret; 381 return ret;
348} 382}
349 383
384#ifdef CONFIG_DEBUG_LOCK_ALLOC
385void btrfs_set_buffer_lockdep_class(struct extent_buffer *eb, int level)
386{
387 lockdep_set_class_and_name(&eb->lock,
388 &btrfs_eb_class[level],
389 btrfs_eb_name[level]);
390}
391#endif
392
350static int btree_readpage_end_io_hook(struct page *page, u64 start, u64 end, 393static int btree_readpage_end_io_hook(struct page *page, u64 start, u64 end,
351 struct extent_state *state) 394 struct extent_state *state)
352{ 395{
@@ -392,6 +435,8 @@ static int btree_readpage_end_io_hook(struct page *page, u64 start, u64 end,
392 } 435 }
393 found_level = btrfs_header_level(eb); 436 found_level = btrfs_header_level(eb);
394 437
438 btrfs_set_buffer_lockdep_class(eb, found_level);
439
395 ret = csum_tree_block(root, eb, 1); 440 ret = csum_tree_block(root, eb, 1);
396 if (ret) 441 if (ret)
397 ret = -EIO; 442 ret = -EIO;
@@ -812,7 +857,7 @@ int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
812 struct inode *btree_inode = root->fs_info->btree_inode; 857 struct inode *btree_inode = root->fs_info->btree_inode;
813 if (btrfs_header_generation(buf) == 858 if (btrfs_header_generation(buf) ==
814 root->fs_info->running_transaction->transid) { 859 root->fs_info->running_transaction->transid) {
815 WARN_ON(!btrfs_tree_locked(buf)); 860 btrfs_assert_tree_locked(buf);
816 861
817 /* ugh, clear_extent_buffer_dirty can be expensive */ 862 /* ugh, clear_extent_buffer_dirty can be expensive */
818 btrfs_set_lock_blocking(buf); 863 btrfs_set_lock_blocking(buf);
@@ -1777,7 +1822,6 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1777 ret = find_and_setup_root(tree_root, fs_info, 1822 ret = find_and_setup_root(tree_root, fs_info,
1778 BTRFS_DEV_TREE_OBJECTID, dev_root); 1823 BTRFS_DEV_TREE_OBJECTID, dev_root);
1779 dev_root->track_dirty = 1; 1824 dev_root->track_dirty = 1;
1780
1781 if (ret) 1825 if (ret)
1782 goto fail_extent_root; 1826 goto fail_extent_root;
1783 1827
@@ -2317,7 +2361,7 @@ void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
2317 2361
2318 btrfs_set_lock_blocking(buf); 2362 btrfs_set_lock_blocking(buf);
2319 2363
2320 WARN_ON(!btrfs_tree_locked(buf)); 2364 btrfs_assert_tree_locked(buf);
2321 if (transid != root->fs_info->generation) { 2365 if (transid != root->fs_info->generation) {
2322 printk(KERN_CRIT "btrfs transid mismatch buffer %llu, " 2366 printk(KERN_CRIT "btrfs transid mismatch buffer %llu, "
2323 "found %llu running %llu\n", 2367 "found %llu running %llu\n",
diff --git a/fs/btrfs/disk-io.h b/fs/btrfs/disk-io.h
index 494a56eb2986..95029db227be 100644
--- a/fs/btrfs/disk-io.h
+++ b/fs/btrfs/disk-io.h
@@ -101,4 +101,14 @@ int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans,
101int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 101int btrfs_add_log_tree(struct btrfs_trans_handle *trans,
102 struct btrfs_root *root); 102 struct btrfs_root *root);
103int btree_lock_page_hook(struct page *page); 103int btree_lock_page_hook(struct page *page);
104
105
106#ifdef CONFIG_DEBUG_LOCK_ALLOC
107void btrfs_set_buffer_lockdep_class(struct extent_buffer *eb, int level);
108#else
109static inline void btrfs_set_buffer_lockdep_class(struct extent_buffer *eb,
110 int level)
111{
112}
113#endif
104#endif 114#endif
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index 7527523c2d2d..9abf81f71c46 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -60,6 +60,10 @@ static int update_block_group(struct btrfs_trans_handle *trans,
60 u64 bytenr, u64 num_bytes, int alloc, 60 u64 bytenr, u64 num_bytes, int alloc,
61 int mark_free); 61 int mark_free);
62 62
63static int do_chunk_alloc(struct btrfs_trans_handle *trans,
64 struct btrfs_root *extent_root, u64 alloc_bytes,
65 u64 flags, int force);
66
63static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) 67static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
64{ 68{
65 return (cache->flags & bits) == bits; 69 return (cache->flags & bits) == bits;
@@ -1323,8 +1327,25 @@ int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1323int btrfs_extent_post_op(struct btrfs_trans_handle *trans, 1327int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
1324 struct btrfs_root *root) 1328 struct btrfs_root *root)
1325{ 1329{
1326 finish_current_insert(trans, root->fs_info->extent_root, 1); 1330 u64 start;
1327 del_pending_extents(trans, root->fs_info->extent_root, 1); 1331 u64 end;
1332 int ret;
1333
1334 while(1) {
1335 finish_current_insert(trans, root->fs_info->extent_root, 1);
1336 del_pending_extents(trans, root->fs_info->extent_root, 1);
1337
1338 /* is there more work to do? */
1339 ret = find_first_extent_bit(&root->fs_info->pending_del,
1340 0, &start, &end, EXTENT_WRITEBACK);
1341 if (!ret)
1342 continue;
1343 ret = find_first_extent_bit(&root->fs_info->extent_ins,
1344 0, &start, &end, EXTENT_WRITEBACK);
1345 if (!ret)
1346 continue;
1347 break;
1348 }
1328 return 0; 1349 return 0;
1329} 1350}
1330 1351
@@ -1892,6 +1913,7 @@ static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1892 found->bytes_pinned = 0; 1913 found->bytes_pinned = 0;
1893 found->bytes_reserved = 0; 1914 found->bytes_reserved = 0;
1894 found->bytes_readonly = 0; 1915 found->bytes_readonly = 0;
1916 found->bytes_delalloc = 0;
1895 found->full = 0; 1917 found->full = 0;
1896 found->force_alloc = 0; 1918 found->force_alloc = 0;
1897 *space_info = found; 1919 *space_info = found;
@@ -1955,6 +1977,233 @@ u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
1955 return flags; 1977 return flags;
1956} 1978}
1957 1979
1980static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data)
1981{
1982 struct btrfs_fs_info *info = root->fs_info;
1983 u64 alloc_profile;
1984
1985 if (data) {
1986 alloc_profile = info->avail_data_alloc_bits &
1987 info->data_alloc_profile;
1988 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
1989 } else if (root == root->fs_info->chunk_root) {
1990 alloc_profile = info->avail_system_alloc_bits &
1991 info->system_alloc_profile;
1992 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
1993 } else {
1994 alloc_profile = info->avail_metadata_alloc_bits &
1995 info->metadata_alloc_profile;
1996 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
1997 }
1998
1999 return btrfs_reduce_alloc_profile(root, data);
2000}
2001
2002void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
2003{
2004 u64 alloc_target;
2005
2006 alloc_target = btrfs_get_alloc_profile(root, 1);
2007 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
2008 alloc_target);
2009}
2010
2011/*
2012 * for now this just makes sure we have at least 5% of our metadata space free
2013 * for use.
2014 */
2015int btrfs_check_metadata_free_space(struct btrfs_root *root)
2016{
2017 struct btrfs_fs_info *info = root->fs_info;
2018 struct btrfs_space_info *meta_sinfo;
2019 u64 alloc_target, thresh;
2020 int committed = 0, ret;
2021
2022 /* get the space info for where the metadata will live */
2023 alloc_target = btrfs_get_alloc_profile(root, 0);
2024 meta_sinfo = __find_space_info(info, alloc_target);
2025
2026again:
2027 spin_lock(&meta_sinfo->lock);
2028 if (!meta_sinfo->full)
2029 thresh = meta_sinfo->total_bytes * 80;
2030 else
2031 thresh = meta_sinfo->total_bytes * 95;
2032
2033 do_div(thresh, 100);
2034
2035 if (meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
2036 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly > thresh) {
2037 struct btrfs_trans_handle *trans;
2038 if (!meta_sinfo->full) {
2039 meta_sinfo->force_alloc = 1;
2040 spin_unlock(&meta_sinfo->lock);
2041
2042 trans = btrfs_start_transaction(root, 1);
2043 if (!trans)
2044 return -ENOMEM;
2045
2046 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2047 2 * 1024 * 1024, alloc_target, 0);
2048 btrfs_end_transaction(trans, root);
2049 goto again;
2050 }
2051 spin_unlock(&meta_sinfo->lock);
2052
2053 if (!committed) {
2054 committed = 1;
2055 trans = btrfs_join_transaction(root, 1);
2056 if (!trans)
2057 return -ENOMEM;
2058 ret = btrfs_commit_transaction(trans, root);
2059 if (ret)
2060 return ret;
2061 goto again;
2062 }
2063 return -ENOSPC;
2064 }
2065 spin_unlock(&meta_sinfo->lock);
2066
2067 return 0;
2068}
2069
2070/*
2071 * This will check the space that the inode allocates from to make sure we have
2072 * enough space for bytes.
2073 */
2074int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
2075 u64 bytes)
2076{
2077 struct btrfs_space_info *data_sinfo;
2078 int ret = 0, committed = 0;
2079
2080 /* make sure bytes are sectorsize aligned */
2081 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
2082
2083 data_sinfo = BTRFS_I(inode)->space_info;
2084again:
2085 /* make sure we have enough space to handle the data first */
2086 spin_lock(&data_sinfo->lock);
2087 if (data_sinfo->total_bytes - data_sinfo->bytes_used -
2088 data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved -
2089 data_sinfo->bytes_pinned - data_sinfo->bytes_readonly -
2090 data_sinfo->bytes_may_use < bytes) {
2091 struct btrfs_trans_handle *trans;
2092
2093 /*
2094 * if we don't have enough free bytes in this space then we need
2095 * to alloc a new chunk.
2096 */
2097 if (!data_sinfo->full) {
2098 u64 alloc_target;
2099
2100 data_sinfo->force_alloc = 1;
2101 spin_unlock(&data_sinfo->lock);
2102
2103 alloc_target = btrfs_get_alloc_profile(root, 1);
2104 trans = btrfs_start_transaction(root, 1);
2105 if (!trans)
2106 return -ENOMEM;
2107
2108 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2109 bytes + 2 * 1024 * 1024,
2110 alloc_target, 0);
2111 btrfs_end_transaction(trans, root);
2112 if (ret)
2113 return ret;
2114 goto again;
2115 }
2116 spin_unlock(&data_sinfo->lock);
2117
2118 /* commit the current transaction and try again */
2119 if (!committed) {
2120 committed = 1;
2121 trans = btrfs_join_transaction(root, 1);
2122 if (!trans)
2123 return -ENOMEM;
2124 ret = btrfs_commit_transaction(trans, root);
2125 if (ret)
2126 return ret;
2127 goto again;
2128 }
2129
2130 printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
2131 ", %llu bytes_used, %llu bytes_reserved, "
2132 "%llu bytes_pinned, %llu bytes_readonly, %llu may use"
2133 "%llu total\n", bytes, data_sinfo->bytes_delalloc,
2134 data_sinfo->bytes_used, data_sinfo->bytes_reserved,
2135 data_sinfo->bytes_pinned, data_sinfo->bytes_readonly,
2136 data_sinfo->bytes_may_use, data_sinfo->total_bytes);
2137 return -ENOSPC;
2138 }
2139 data_sinfo->bytes_may_use += bytes;
2140 BTRFS_I(inode)->reserved_bytes += bytes;
2141 spin_unlock(&data_sinfo->lock);
2142
2143 return btrfs_check_metadata_free_space(root);
2144}
2145
2146/*
2147 * if there was an error for whatever reason after calling
2148 * btrfs_check_data_free_space, call this so we can cleanup the counters.
2149 */
2150void btrfs_free_reserved_data_space(struct btrfs_root *root,
2151 struct inode *inode, u64 bytes)
2152{
2153 struct btrfs_space_info *data_sinfo;
2154
2155 /* make sure bytes are sectorsize aligned */
2156 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
2157
2158 data_sinfo = BTRFS_I(inode)->space_info;
2159 spin_lock(&data_sinfo->lock);
2160 data_sinfo->bytes_may_use -= bytes;
2161 BTRFS_I(inode)->reserved_bytes -= bytes;
2162 spin_unlock(&data_sinfo->lock);
2163}
2164
2165/* called when we are adding a delalloc extent to the inode's io_tree */
2166void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode,
2167 u64 bytes)
2168{
2169 struct btrfs_space_info *data_sinfo;
2170
2171 /* get the space info for where this inode will be storing its data */
2172 data_sinfo = BTRFS_I(inode)->space_info;
2173
2174 /* make sure we have enough space to handle the data first */
2175 spin_lock(&data_sinfo->lock);
2176 data_sinfo->bytes_delalloc += bytes;
2177
2178 /*
2179 * we are adding a delalloc extent without calling
2180 * btrfs_check_data_free_space first. This happens on a weird
2181 * writepage condition, but shouldn't hurt our accounting
2182 */
2183 if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) {
2184 data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes;
2185 BTRFS_I(inode)->reserved_bytes = 0;
2186 } else {
2187 data_sinfo->bytes_may_use -= bytes;
2188 BTRFS_I(inode)->reserved_bytes -= bytes;
2189 }
2190
2191 spin_unlock(&data_sinfo->lock);
2192}
2193
2194/* called when we are clearing an delalloc extent from the inode's io_tree */
2195void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
2196 u64 bytes)
2197{
2198 struct btrfs_space_info *info;
2199
2200 info = BTRFS_I(inode)->space_info;
2201
2202 spin_lock(&info->lock);
2203 info->bytes_delalloc -= bytes;
2204 spin_unlock(&info->lock);
2205}
2206
1958static int do_chunk_alloc(struct btrfs_trans_handle *trans, 2207static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1959 struct btrfs_root *extent_root, u64 alloc_bytes, 2208 struct btrfs_root *extent_root, u64 alloc_bytes,
1960 u64 flags, int force) 2209 u64 flags, int force)
@@ -2211,13 +2460,12 @@ static int finish_current_insert(struct btrfs_trans_handle *trans,
2211 u64 end; 2460 u64 end;
2212 u64 priv; 2461 u64 priv;
2213 u64 search = 0; 2462 u64 search = 0;
2214 u64 skipped = 0;
2215 struct btrfs_fs_info *info = extent_root->fs_info; 2463 struct btrfs_fs_info *info = extent_root->fs_info;
2216 struct btrfs_path *path; 2464 struct btrfs_path *path;
2217 struct pending_extent_op *extent_op, *tmp; 2465 struct pending_extent_op *extent_op, *tmp;
2218 struct list_head insert_list, update_list; 2466 struct list_head insert_list, update_list;
2219 int ret; 2467 int ret;
2220 int num_inserts = 0, max_inserts; 2468 int num_inserts = 0, max_inserts, restart = 0;
2221 2469
2222 path = btrfs_alloc_path(); 2470 path = btrfs_alloc_path();
2223 INIT_LIST_HEAD(&insert_list); 2471 INIT_LIST_HEAD(&insert_list);
@@ -2233,19 +2481,19 @@ again:
2233 ret = find_first_extent_bit(&info->extent_ins, search, &start, 2481 ret = find_first_extent_bit(&info->extent_ins, search, &start,
2234 &end, EXTENT_WRITEBACK); 2482 &end, EXTENT_WRITEBACK);
2235 if (ret) { 2483 if (ret) {
2236 if (skipped && all && !num_inserts && 2484 if (restart && !num_inserts &&
2237 list_empty(&update_list)) { 2485 list_empty(&update_list)) {
2238 skipped = 0; 2486 restart = 0;
2239 search = 0; 2487 search = 0;
2240 continue; 2488 continue;
2241 } 2489 }
2242 mutex_unlock(&info->extent_ins_mutex);
2243 break; 2490 break;
2244 } 2491 }
2245 2492
2246 ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS); 2493 ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS);
2247 if (!ret) { 2494 if (!ret) {
2248 skipped = 1; 2495 if (all)
2496 restart = 1;
2249 search = end + 1; 2497 search = end + 1;
2250 if (need_resched()) { 2498 if (need_resched()) {
2251 mutex_unlock(&info->extent_ins_mutex); 2499 mutex_unlock(&info->extent_ins_mutex);
@@ -2264,7 +2512,7 @@ again:
2264 list_add_tail(&extent_op->list, &insert_list); 2512 list_add_tail(&extent_op->list, &insert_list);
2265 search = end + 1; 2513 search = end + 1;
2266 if (num_inserts == max_inserts) { 2514 if (num_inserts == max_inserts) {
2267 mutex_unlock(&info->extent_ins_mutex); 2515 restart = 1;
2268 break; 2516 break;
2269 } 2517 }
2270 } else if (extent_op->type == PENDING_BACKREF_UPDATE) { 2518 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
@@ -2280,7 +2528,6 @@ again:
2280 * somebody marked this thing for deletion then just unlock it and be 2528 * somebody marked this thing for deletion then just unlock it and be
2281 * done, the free_extents will handle it 2529 * done, the free_extents will handle it
2282 */ 2530 */
2283 mutex_lock(&info->extent_ins_mutex);
2284 list_for_each_entry_safe(extent_op, tmp, &update_list, list) { 2531 list_for_each_entry_safe(extent_op, tmp, &update_list, list) {
2285 clear_extent_bits(&info->extent_ins, extent_op->bytenr, 2532 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2286 extent_op->bytenr + extent_op->num_bytes - 1, 2533 extent_op->bytenr + extent_op->num_bytes - 1,
@@ -2302,6 +2549,10 @@ again:
2302 if (!list_empty(&update_list)) { 2549 if (!list_empty(&update_list)) {
2303 ret = update_backrefs(trans, extent_root, path, &update_list); 2550 ret = update_backrefs(trans, extent_root, path, &update_list);
2304 BUG_ON(ret); 2551 BUG_ON(ret);
2552
2553 /* we may have COW'ed new blocks, so lets start over */
2554 if (all)
2555 restart = 1;
2305 } 2556 }
2306 2557
2307 /* 2558 /*
@@ -2309,9 +2560,9 @@ again:
2309 * need to make sure everything is cleaned then reset everything and 2560 * need to make sure everything is cleaned then reset everything and
2310 * go back to the beginning 2561 * go back to the beginning
2311 */ 2562 */
2312 if (!num_inserts && all && skipped) { 2563 if (!num_inserts && restart) {
2313 search = 0; 2564 search = 0;
2314 skipped = 0; 2565 restart = 0;
2315 INIT_LIST_HEAD(&update_list); 2566 INIT_LIST_HEAD(&update_list);
2316 INIT_LIST_HEAD(&insert_list); 2567 INIT_LIST_HEAD(&insert_list);
2317 goto again; 2568 goto again;
@@ -2368,27 +2619,19 @@ again:
2368 BUG_ON(ret); 2619 BUG_ON(ret);
2369 2620
2370 /* 2621 /*
2371 * if we broke out of the loop in order to insert stuff because we hit 2622 * if restart is set for whatever reason we need to go back and start
2372 * the maximum number of inserts at a time we can handle, then loop 2623 * searching through the pending list again.
2373 * back and pick up where we left off 2624 *
2625 * We just inserted some extents, which could have resulted in new
2626 * blocks being allocated, which would result in new blocks needing
2627 * updates, so if all is set we _must_ restart to get the updated
2628 * blocks.
2374 */ 2629 */
2375 if (num_inserts == max_inserts) { 2630 if (restart || all) {
2376 INIT_LIST_HEAD(&insert_list);
2377 INIT_LIST_HEAD(&update_list);
2378 num_inserts = 0;
2379 goto again;
2380 }
2381
2382 /*
2383 * again, if we need to make absolutely sure there are no more pending
2384 * extent operations left and we know that we skipped some, go back to
2385 * the beginning and do it all again
2386 */
2387 if (all && skipped) {
2388 INIT_LIST_HEAD(&insert_list); 2631 INIT_LIST_HEAD(&insert_list);
2389 INIT_LIST_HEAD(&update_list); 2632 INIT_LIST_HEAD(&update_list);
2390 search = 0; 2633 search = 0;
2391 skipped = 0; 2634 restart = 0;
2392 num_inserts = 0; 2635 num_inserts = 0;
2393 goto again; 2636 goto again;
2394 } 2637 }
@@ -2709,6 +2952,8 @@ again:
2709 goto again; 2952 goto again;
2710 } 2953 }
2711 2954
2955 if (!err)
2956 finish_current_insert(trans, extent_root, 0);
2712 return err; 2957 return err;
2713} 2958}
2714 2959
@@ -2859,7 +3104,8 @@ static noinline int find_free_extent(struct btrfs_trans_handle *trans,
2859 3104
2860 if (data & BTRFS_BLOCK_GROUP_METADATA) { 3105 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2861 last_ptr = &root->fs_info->last_alloc; 3106 last_ptr = &root->fs_info->last_alloc;
2862 empty_cluster = 64 * 1024; 3107 if (!btrfs_test_opt(root, SSD))
3108 empty_cluster = 64 * 1024;
2863 } 3109 }
2864 3110
2865 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) 3111 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
@@ -3091,6 +3337,10 @@ static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
3091 (unsigned long long)(info->total_bytes - info->bytes_used - 3337 (unsigned long long)(info->total_bytes - info->bytes_used -
3092 info->bytes_pinned - info->bytes_reserved), 3338 info->bytes_pinned - info->bytes_reserved),
3093 (info->full) ? "" : "not "); 3339 (info->full) ? "" : "not ");
3340 printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
3341 " may_use=%llu, used=%llu\n", info->total_bytes,
3342 info->bytes_pinned, info->bytes_delalloc, info->bytes_may_use,
3343 info->bytes_used);
3094 3344
3095 down_read(&info->groups_sem); 3345 down_read(&info->groups_sem);
3096 list_for_each_entry(cache, &info->block_groups, list) { 3346 list_for_each_entry(cache, &info->block_groups, list) {
@@ -3117,24 +3367,10 @@ static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3117{ 3367{
3118 int ret; 3368 int ret;
3119 u64 search_start = 0; 3369 u64 search_start = 0;
3120 u64 alloc_profile;
3121 struct btrfs_fs_info *info = root->fs_info; 3370 struct btrfs_fs_info *info = root->fs_info;
3122 3371
3123 if (data) { 3372 data = btrfs_get_alloc_profile(root, data);
3124 alloc_profile = info->avail_data_alloc_bits &
3125 info->data_alloc_profile;
3126 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
3127 } else if (root == root->fs_info->chunk_root) {
3128 alloc_profile = info->avail_system_alloc_bits &
3129 info->system_alloc_profile;
3130 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
3131 } else {
3132 alloc_profile = info->avail_metadata_alloc_bits &
3133 info->metadata_alloc_profile;
3134 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
3135 }
3136again: 3373again:
3137 data = btrfs_reduce_alloc_profile(root, data);
3138 /* 3374 /*
3139 * the only place that sets empty_size is btrfs_realloc_node, which 3375 * the only place that sets empty_size is btrfs_realloc_node, which
3140 * is not called recursively on allocations 3376 * is not called recursively on allocations
@@ -3402,7 +3638,8 @@ int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
3402 3638
3403struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans, 3639struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
3404 struct btrfs_root *root, 3640 struct btrfs_root *root,
3405 u64 bytenr, u32 blocksize) 3641 u64 bytenr, u32 blocksize,
3642 int level)
3406{ 3643{
3407 struct extent_buffer *buf; 3644 struct extent_buffer *buf;
3408 3645
@@ -3410,6 +3647,7 @@ struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
3410 if (!buf) 3647 if (!buf)
3411 return ERR_PTR(-ENOMEM); 3648 return ERR_PTR(-ENOMEM);
3412 btrfs_set_header_generation(buf, trans->transid); 3649 btrfs_set_header_generation(buf, trans->transid);
3650 btrfs_set_buffer_lockdep_class(buf, level);
3413 btrfs_tree_lock(buf); 3651 btrfs_tree_lock(buf);
3414 clean_tree_block(trans, root, buf); 3652 clean_tree_block(trans, root, buf);
3415 3653
@@ -3453,7 +3691,8 @@ struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
3453 return ERR_PTR(ret); 3691 return ERR_PTR(ret);
3454 } 3692 }
3455 3693
3456 buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize); 3694 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
3695 blocksize, level);
3457 return buf; 3696 return buf;
3458} 3697}
3459 3698
@@ -4179,13 +4418,13 @@ int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
4179 path = btrfs_alloc_path(); 4418 path = btrfs_alloc_path();
4180 BUG_ON(!path); 4419 BUG_ON(!path);
4181 4420
4182 BUG_ON(!btrfs_tree_locked(parent)); 4421 btrfs_assert_tree_locked(parent);
4183 parent_level = btrfs_header_level(parent); 4422 parent_level = btrfs_header_level(parent);
4184 extent_buffer_get(parent); 4423 extent_buffer_get(parent);
4185 path->nodes[parent_level] = parent; 4424 path->nodes[parent_level] = parent;
4186 path->slots[parent_level] = btrfs_header_nritems(parent); 4425 path->slots[parent_level] = btrfs_header_nritems(parent);
4187 4426
4188 BUG_ON(!btrfs_tree_locked(node)); 4427 btrfs_assert_tree_locked(node);
4189 level = btrfs_header_level(node); 4428 level = btrfs_header_level(node);
4190 extent_buffer_get(node); 4429 extent_buffer_get(node);
4191 path->nodes[level] = node; 4430 path->nodes[level] = node;
@@ -5641,7 +5880,9 @@ static noinline int relocate_one_extent(struct btrfs_root *extent_root,
5641 prev_block = block_start; 5880 prev_block = block_start;
5642 } 5881 }
5643 5882
5883 mutex_lock(&extent_root->fs_info->trans_mutex);
5644 btrfs_record_root_in_trans(found_root); 5884 btrfs_record_root_in_trans(found_root);
5885 mutex_unlock(&extent_root->fs_info->trans_mutex);
5645 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { 5886 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5646 /* 5887 /*
5647 * try to update data extent references while 5888 * try to update data extent references while
diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index 37d43b516b79..ebe6b29e6069 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -415,8 +415,6 @@ static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
415 415
416 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node); 416 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
417 if (node) { 417 if (node) {
418 struct extent_state *found;
419 found = rb_entry(node, struct extent_state, rb_node);
420 free_extent_state(prealloc); 418 free_extent_state(prealloc);
421 return -EEXIST; 419 return -EEXIST;
422 } 420 }
diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
index 3e8023efaff7..dc78954861b3 100644
--- a/fs/btrfs/file.c
+++ b/fs/btrfs/file.c
@@ -1091,19 +1091,24 @@ static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
1091 WARN_ON(num_pages > nrptrs); 1091 WARN_ON(num_pages > nrptrs);
1092 memset(pages, 0, sizeof(struct page *) * nrptrs); 1092 memset(pages, 0, sizeof(struct page *) * nrptrs);
1093 1093
1094 ret = btrfs_check_free_space(root, write_bytes, 0); 1094 ret = btrfs_check_data_free_space(root, inode, write_bytes);
1095 if (ret) 1095 if (ret)
1096 goto out; 1096 goto out;
1097 1097
1098 ret = prepare_pages(root, file, pages, num_pages, 1098 ret = prepare_pages(root, file, pages, num_pages,
1099 pos, first_index, last_index, 1099 pos, first_index, last_index,
1100 write_bytes); 1100 write_bytes);
1101 if (ret) 1101 if (ret) {
1102 btrfs_free_reserved_data_space(root, inode,
1103 write_bytes);
1102 goto out; 1104 goto out;
1105 }
1103 1106
1104 ret = btrfs_copy_from_user(pos, num_pages, 1107 ret = btrfs_copy_from_user(pos, num_pages,
1105 write_bytes, pages, buf); 1108 write_bytes, pages, buf);
1106 if (ret) { 1109 if (ret) {
1110 btrfs_free_reserved_data_space(root, inode,
1111 write_bytes);
1107 btrfs_drop_pages(pages, num_pages); 1112 btrfs_drop_pages(pages, num_pages);
1108 goto out; 1113 goto out;
1109 } 1114 }
@@ -1111,8 +1116,11 @@ static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
1111 ret = dirty_and_release_pages(NULL, root, file, pages, 1116 ret = dirty_and_release_pages(NULL, root, file, pages,
1112 num_pages, pos, write_bytes); 1117 num_pages, pos, write_bytes);
1113 btrfs_drop_pages(pages, num_pages); 1118 btrfs_drop_pages(pages, num_pages);
1114 if (ret) 1119 if (ret) {
1120 btrfs_free_reserved_data_space(root, inode,
1121 write_bytes);
1115 goto out; 1122 goto out;
1123 }
1116 1124
1117 if (will_write) { 1125 if (will_write) {
1118 btrfs_fdatawrite_range(inode->i_mapping, pos, 1126 btrfs_fdatawrite_range(inode->i_mapping, pos,
@@ -1136,6 +1144,8 @@ static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
1136 } 1144 }
1137out: 1145out:
1138 mutex_unlock(&inode->i_mutex); 1146 mutex_unlock(&inode->i_mutex);
1147 if (ret)
1148 err = ret;
1139 1149
1140out_nolock: 1150out_nolock:
1141 kfree(pages); 1151 kfree(pages);
@@ -1222,7 +1232,7 @@ int btrfs_sync_file(struct file *file, struct dentry *dentry, int datasync)
1222 /* 1232 /*
1223 * ok we haven't committed the transaction yet, lets do a commit 1233 * ok we haven't committed the transaction yet, lets do a commit
1224 */ 1234 */
1225 if (file->private_data) 1235 if (file && file->private_data)
1226 btrfs_ioctl_trans_end(file); 1236 btrfs_ioctl_trans_end(file);
1227 1237
1228 trans = btrfs_start_transaction(root, 1); 1238 trans = btrfs_start_transaction(root, 1);
@@ -1231,7 +1241,7 @@ int btrfs_sync_file(struct file *file, struct dentry *dentry, int datasync)
1231 goto out; 1241 goto out;
1232 } 1242 }
1233 1243
1234 ret = btrfs_log_dentry_safe(trans, root, file->f_dentry); 1244 ret = btrfs_log_dentry_safe(trans, root, dentry);
1235 if (ret < 0) 1245 if (ret < 0)
1236 goto out; 1246 goto out;
1237 1247
@@ -1245,7 +1255,7 @@ int btrfs_sync_file(struct file *file, struct dentry *dentry, int datasync)
1245 * file again, but that will end up using the synchronization 1255 * file again, but that will end up using the synchronization
1246 * inside btrfs_sync_log to keep things safe. 1256 * inside btrfs_sync_log to keep things safe.
1247 */ 1257 */
1248 mutex_unlock(&file->f_dentry->d_inode->i_mutex); 1258 mutex_unlock(&dentry->d_inode->i_mutex);
1249 1259
1250 if (ret > 0) { 1260 if (ret > 0) {
1251 ret = btrfs_commit_transaction(trans, root); 1261 ret = btrfs_commit_transaction(trans, root);
@@ -1253,7 +1263,7 @@ int btrfs_sync_file(struct file *file, struct dentry *dentry, int datasync)
1253 btrfs_sync_log(trans, root); 1263 btrfs_sync_log(trans, root);
1254 ret = btrfs_end_transaction(trans, root); 1264 ret = btrfs_end_transaction(trans, root);
1255 } 1265 }
1256 mutex_lock(&file->f_dentry->d_inode->i_mutex); 1266 mutex_lock(&dentry->d_inode->i_mutex);
1257out: 1267out:
1258 return ret > 0 ? EIO : ret; 1268 return ret > 0 ? EIO : ret;
1259} 1269}
diff --git a/fs/btrfs/inode-map.c b/fs/btrfs/inode-map.c
index 2aa79873eb46..cc7334d833c9 100644
--- a/fs/btrfs/inode-map.c
+++ b/fs/btrfs/inode-map.c
@@ -84,7 +84,6 @@ int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
84 search_key.type = 0; 84 search_key.type = 0;
85 search_key.offset = 0; 85 search_key.offset = 0;
86 86
87 btrfs_init_path(path);
88 start_found = 0; 87 start_found = 0;
89 ret = btrfs_search_slot(trans, root, &search_key, path, 0, 0); 88 ret = btrfs_search_slot(trans, root, &search_key, path, 0, 0);
90 if (ret < 0) 89 if (ret < 0)
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 8f0706210a47..7d4f948bc22a 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -102,34 +102,6 @@ static int btrfs_init_inode_security(struct inode *inode, struct inode *dir)
102} 102}
103 103
104/* 104/*
105 * a very lame attempt at stopping writes when the FS is 85% full. There
106 * are countless ways this is incorrect, but it is better than nothing.
107 */
108int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
109 int for_del)
110{
111 u64 total;
112 u64 used;
113 u64 thresh;
114 int ret = 0;
115
116 spin_lock(&root->fs_info->delalloc_lock);
117 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
118 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
119 if (for_del)
120 thresh = total * 90;
121 else
122 thresh = total * 85;
123
124 do_div(thresh, 100);
125
126 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
127 ret = -ENOSPC;
128 spin_unlock(&root->fs_info->delalloc_lock);
129 return ret;
130}
131
132/*
133 * this does all the hard work for inserting an inline extent into 105 * this does all the hard work for inserting an inline extent into
134 * the btree. The caller should have done a btrfs_drop_extents so that 106 * the btree. The caller should have done a btrfs_drop_extents so that
135 * no overlapping inline items exist in the btree 107 * no overlapping inline items exist in the btree
@@ -1190,6 +1162,7 @@ static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
1190 */ 1162 */
1191 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { 1163 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
1192 struct btrfs_root *root = BTRFS_I(inode)->root; 1164 struct btrfs_root *root = BTRFS_I(inode)->root;
1165 btrfs_delalloc_reserve_space(root, inode, end - start + 1);
1193 spin_lock(&root->fs_info->delalloc_lock); 1166 spin_lock(&root->fs_info->delalloc_lock);
1194 BTRFS_I(inode)->delalloc_bytes += end - start + 1; 1167 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
1195 root->fs_info->delalloc_bytes += end - start + 1; 1168 root->fs_info->delalloc_bytes += end - start + 1;
@@ -1223,9 +1196,12 @@ static int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
1223 (unsigned long long)end - start + 1, 1196 (unsigned long long)end - start + 1,
1224 (unsigned long long) 1197 (unsigned long long)
1225 root->fs_info->delalloc_bytes); 1198 root->fs_info->delalloc_bytes);
1199 btrfs_delalloc_free_space(root, inode, (u64)-1);
1226 root->fs_info->delalloc_bytes = 0; 1200 root->fs_info->delalloc_bytes = 0;
1227 BTRFS_I(inode)->delalloc_bytes = 0; 1201 BTRFS_I(inode)->delalloc_bytes = 0;
1228 } else { 1202 } else {
1203 btrfs_delalloc_free_space(root, inode,
1204 end - start + 1);
1229 root->fs_info->delalloc_bytes -= end - start + 1; 1205 root->fs_info->delalloc_bytes -= end - start + 1;
1230 BTRFS_I(inode)->delalloc_bytes -= end - start + 1; 1206 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
1231 } 1207 }
@@ -2245,10 +2221,6 @@ static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2245 2221
2246 root = BTRFS_I(dir)->root; 2222 root = BTRFS_I(dir)->root;
2247 2223
2248 ret = btrfs_check_free_space(root, 1, 1);
2249 if (ret)
2250 goto fail;
2251
2252 trans = btrfs_start_transaction(root, 1); 2224 trans = btrfs_start_transaction(root, 1);
2253 2225
2254 btrfs_set_trans_block_group(trans, dir); 2226 btrfs_set_trans_block_group(trans, dir);
@@ -2261,7 +2233,6 @@ static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2261 nr = trans->blocks_used; 2233 nr = trans->blocks_used;
2262 2234
2263 btrfs_end_transaction_throttle(trans, root); 2235 btrfs_end_transaction_throttle(trans, root);
2264fail:
2265 btrfs_btree_balance_dirty(root, nr); 2236 btrfs_btree_balance_dirty(root, nr);
2266 return ret; 2237 return ret;
2267} 2238}
@@ -2284,10 +2255,6 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2284 return -ENOTEMPTY; 2255 return -ENOTEMPTY;
2285 } 2256 }
2286 2257
2287 ret = btrfs_check_free_space(root, 1, 1);
2288 if (ret)
2289 goto fail;
2290
2291 trans = btrfs_start_transaction(root, 1); 2258 trans = btrfs_start_transaction(root, 1);
2292 btrfs_set_trans_block_group(trans, dir); 2259 btrfs_set_trans_block_group(trans, dir);
2293 2260
@@ -2304,7 +2271,6 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2304fail_trans: 2271fail_trans:
2305 nr = trans->blocks_used; 2272 nr = trans->blocks_used;
2306 ret = btrfs_end_transaction_throttle(trans, root); 2273 ret = btrfs_end_transaction_throttle(trans, root);
2307fail:
2308 btrfs_btree_balance_dirty(root, nr); 2274 btrfs_btree_balance_dirty(root, nr);
2309 2275
2310 if (ret && !err) 2276 if (ret && !err)
@@ -2531,8 +2497,6 @@ noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
2531 key.offset = (u64)-1; 2497 key.offset = (u64)-1;
2532 key.type = (u8)-1; 2498 key.type = (u8)-1;
2533 2499
2534 btrfs_init_path(path);
2535
2536search_again: 2500search_again:
2537 ret = btrfs_search_slot(trans, root, &key, path, -1, 1); 2501 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2538 if (ret < 0) 2502 if (ret < 0)
@@ -2820,7 +2784,7 @@ int btrfs_cont_expand(struct inode *inode, loff_t size)
2820 if (size <= hole_start) 2784 if (size <= hole_start)
2821 return 0; 2785 return 0;
2822 2786
2823 err = btrfs_check_free_space(root, 1, 0); 2787 err = btrfs_check_metadata_free_space(root);
2824 if (err) 2788 if (err)
2825 return err; 2789 return err;
2826 2790
@@ -3016,6 +2980,7 @@ static noinline void init_btrfs_i(struct inode *inode)
3016 bi->last_trans = 0; 2980 bi->last_trans = 0;
3017 bi->logged_trans = 0; 2981 bi->logged_trans = 0;
3018 bi->delalloc_bytes = 0; 2982 bi->delalloc_bytes = 0;
2983 bi->reserved_bytes = 0;
3019 bi->disk_i_size = 0; 2984 bi->disk_i_size = 0;
3020 bi->flags = 0; 2985 bi->flags = 0;
3021 bi->index_cnt = (u64)-1; 2986 bi->index_cnt = (u64)-1;
@@ -3037,6 +3002,7 @@ static int btrfs_init_locked_inode(struct inode *inode, void *p)
3037 inode->i_ino = args->ino; 3002 inode->i_ino = args->ino;
3038 init_btrfs_i(inode); 3003 init_btrfs_i(inode);
3039 BTRFS_I(inode)->root = args->root; 3004 BTRFS_I(inode)->root = args->root;
3005 btrfs_set_inode_space_info(args->root, inode);
3040 return 0; 3006 return 0;
3041} 3007}
3042 3008
@@ -3457,6 +3423,7 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
3457 BTRFS_I(inode)->index_cnt = 2; 3423 BTRFS_I(inode)->index_cnt = 2;
3458 BTRFS_I(inode)->root = root; 3424 BTRFS_I(inode)->root = root;
3459 BTRFS_I(inode)->generation = trans->transid; 3425 BTRFS_I(inode)->generation = trans->transid;
3426 btrfs_set_inode_space_info(root, inode);
3460 3427
3461 if (mode & S_IFDIR) 3428 if (mode & S_IFDIR)
3462 owner = 0; 3429 owner = 0;
@@ -3604,7 +3571,7 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
3604 if (!new_valid_dev(rdev)) 3571 if (!new_valid_dev(rdev))
3605 return -EINVAL; 3572 return -EINVAL;
3606 3573
3607 err = btrfs_check_free_space(root, 1, 0); 3574 err = btrfs_check_metadata_free_space(root);
3608 if (err) 3575 if (err)
3609 goto fail; 3576 goto fail;
3610 3577
@@ -3667,7 +3634,7 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
3667 u64 objectid; 3634 u64 objectid;
3668 u64 index = 0; 3635 u64 index = 0;
3669 3636
3670 err = btrfs_check_free_space(root, 1, 0); 3637 err = btrfs_check_metadata_free_space(root);
3671 if (err) 3638 if (err)
3672 goto fail; 3639 goto fail;
3673 trans = btrfs_start_transaction(root, 1); 3640 trans = btrfs_start_transaction(root, 1);
@@ -3735,7 +3702,7 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
3735 return -ENOENT; 3702 return -ENOENT;
3736 3703
3737 btrfs_inc_nlink(inode); 3704 btrfs_inc_nlink(inode);
3738 err = btrfs_check_free_space(root, 1, 0); 3705 err = btrfs_check_metadata_free_space(root);
3739 if (err) 3706 if (err)
3740 goto fail; 3707 goto fail;
3741 err = btrfs_set_inode_index(dir, &index); 3708 err = btrfs_set_inode_index(dir, &index);
@@ -3781,7 +3748,7 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
3781 u64 index = 0; 3748 u64 index = 0;
3782 unsigned long nr = 1; 3749 unsigned long nr = 1;
3783 3750
3784 err = btrfs_check_free_space(root, 1, 0); 3751 err = btrfs_check_metadata_free_space(root);
3785 if (err) 3752 if (err)
3786 goto out_unlock; 3753 goto out_unlock;
3787 3754
@@ -4263,7 +4230,7 @@ static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
4263{ 4230{
4264 if (PageWriteback(page) || PageDirty(page)) 4231 if (PageWriteback(page) || PageDirty(page))
4265 return 0; 4232 return 0;
4266 return __btrfs_releasepage(page, gfp_flags); 4233 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
4267} 4234}
4268 4235
4269static void btrfs_invalidatepage(struct page *page, unsigned long offset) 4236static void btrfs_invalidatepage(struct page *page, unsigned long offset)
@@ -4338,7 +4305,7 @@ int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
4338 u64 page_start; 4305 u64 page_start;
4339 u64 page_end; 4306 u64 page_end;
4340 4307
4341 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0); 4308 ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
4342 if (ret) 4309 if (ret)
4343 goto out; 4310 goto out;
4344 4311
@@ -4351,6 +4318,7 @@ again:
4351 4318
4352 if ((page->mapping != inode->i_mapping) || 4319 if ((page->mapping != inode->i_mapping) ||
4353 (page_start >= size)) { 4320 (page_start >= size)) {
4321 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
4354 /* page got truncated out from underneath us */ 4322 /* page got truncated out from underneath us */
4355 goto out_unlock; 4323 goto out_unlock;
4356 } 4324 }
@@ -4633,7 +4601,7 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4633 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 4601 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
4634 return -EXDEV; 4602 return -EXDEV;
4635 4603
4636 ret = btrfs_check_free_space(root, 1, 0); 4604 ret = btrfs_check_metadata_free_space(root);
4637 if (ret) 4605 if (ret)
4638 goto out_unlock; 4606 goto out_unlock;
4639 4607
@@ -4751,7 +4719,7 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
4751 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) 4719 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
4752 return -ENAMETOOLONG; 4720 return -ENAMETOOLONG;
4753 4721
4754 err = btrfs_check_free_space(root, 1, 0); 4722 err = btrfs_check_metadata_free_space(root);
4755 if (err) 4723 if (err)
4756 goto out_fail; 4724 goto out_fail;
4757 4725
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index 988fdc8b49eb..bca729fc80c8 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -70,7 +70,7 @@ static noinline int create_subvol(struct btrfs_root *root,
70 u64 index = 0; 70 u64 index = 0;
71 unsigned long nr = 1; 71 unsigned long nr = 1;
72 72
73 ret = btrfs_check_free_space(root, 1, 0); 73 ret = btrfs_check_metadata_free_space(root);
74 if (ret) 74 if (ret)
75 goto fail_commit; 75 goto fail_commit;
76 76
@@ -203,7 +203,7 @@ static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
203 if (!root->ref_cows) 203 if (!root->ref_cows)
204 return -EINVAL; 204 return -EINVAL;
205 205
206 ret = btrfs_check_free_space(root, 1, 0); 206 ret = btrfs_check_metadata_free_space(root);
207 if (ret) 207 if (ret)
208 goto fail_unlock; 208 goto fail_unlock;
209 209
@@ -374,7 +374,7 @@ static int btrfs_defrag_file(struct file *file)
374 unsigned long i; 374 unsigned long i;
375 int ret; 375 int ret;
376 376
377 ret = btrfs_check_free_space(root, inode->i_size, 0); 377 ret = btrfs_check_data_free_space(root, inode, inode->i_size);
378 if (ret) 378 if (ret)
379 return -ENOSPC; 379 return -ENOSPC;
380 380
diff --git a/fs/btrfs/locking.c b/fs/btrfs/locking.c
index 9ebe9385129b..47b0a88c12a2 100644
--- a/fs/btrfs/locking.c
+++ b/fs/btrfs/locking.c
@@ -25,21 +25,10 @@
25#include "extent_io.h" 25#include "extent_io.h"
26#include "locking.h" 26#include "locking.h"
27 27
28/*
29 * btrfs_header_level() isn't free, so don't call it when lockdep isn't
30 * on
31 */
32#ifdef CONFIG_DEBUG_LOCK_ALLOC
33static inline void spin_nested(struct extent_buffer *eb)
34{
35 spin_lock_nested(&eb->lock, BTRFS_MAX_LEVEL - btrfs_header_level(eb));
36}
37#else
38static inline void spin_nested(struct extent_buffer *eb) 28static inline void spin_nested(struct extent_buffer *eb)
39{ 29{
40 spin_lock(&eb->lock); 30 spin_lock(&eb->lock);
41} 31}
42#endif
43 32
44/* 33/*
45 * Setting a lock to blocking will drop the spinlock and set the 34 * Setting a lock to blocking will drop the spinlock and set the
@@ -231,8 +220,8 @@ int btrfs_tree_unlock(struct extent_buffer *eb)
231 return 0; 220 return 0;
232} 221}
233 222
234int btrfs_tree_locked(struct extent_buffer *eb) 223void btrfs_assert_tree_locked(struct extent_buffer *eb)
235{ 224{
236 return test_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags) || 225 if (!test_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags))
237 spin_is_locked(&eb->lock); 226 assert_spin_locked(&eb->lock);
238} 227}
diff --git a/fs/btrfs/locking.h b/fs/btrfs/locking.h
index 6bb0afbff928..6c4ce457168c 100644
--- a/fs/btrfs/locking.h
+++ b/fs/btrfs/locking.h
@@ -21,11 +21,11 @@
21 21
22int btrfs_tree_lock(struct extent_buffer *eb); 22int btrfs_tree_lock(struct extent_buffer *eb);
23int btrfs_tree_unlock(struct extent_buffer *eb); 23int btrfs_tree_unlock(struct extent_buffer *eb);
24int btrfs_tree_locked(struct extent_buffer *eb);
25 24
26int btrfs_try_tree_lock(struct extent_buffer *eb); 25int btrfs_try_tree_lock(struct extent_buffer *eb);
27int btrfs_try_spin_lock(struct extent_buffer *eb); 26int btrfs_try_spin_lock(struct extent_buffer *eb);
28 27
29void btrfs_set_lock_blocking(struct extent_buffer *eb); 28void btrfs_set_lock_blocking(struct extent_buffer *eb);
30void btrfs_clear_lock_blocking(struct extent_buffer *eb); 29void btrfs_clear_lock_blocking(struct extent_buffer *eb);
30void btrfs_assert_tree_locked(struct extent_buffer *eb);
31#endif 31#endif
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index f3fd7e2cbc38..19a4daf03ccb 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -379,7 +379,6 @@ int btrfs_sync_fs(struct super_block *sb, int wait)
379 btrfs_start_delalloc_inodes(root); 379 btrfs_start_delalloc_inodes(root);
380 btrfs_wait_ordered_extents(root, 0); 380 btrfs_wait_ordered_extents(root, 0);
381 381
382 btrfs_clean_old_snapshots(root);
383 trans = btrfs_start_transaction(root, 1); 382 trans = btrfs_start_transaction(root, 1);
384 ret = btrfs_commit_transaction(trans, root); 383 ret = btrfs_commit_transaction(trans, root);
385 sb->s_dirt = 0; 384 sb->s_dirt = 0;
@@ -511,6 +510,10 @@ static int btrfs_remount(struct super_block *sb, int *flags, char *data)
511 struct btrfs_root *root = btrfs_sb(sb); 510 struct btrfs_root *root = btrfs_sb(sb);
512 int ret; 511 int ret;
513 512
513 ret = btrfs_parse_options(root, data);
514 if (ret)
515 return -EINVAL;
516
514 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) 517 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
515 return 0; 518 return 0;
516 519
diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c
index 919172de5c9a..4112d53d4f4d 100644
--- a/fs/btrfs/transaction.c
+++ b/fs/btrfs/transaction.c
@@ -688,7 +688,9 @@ static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
688 num_bytes -= btrfs_root_used(&dirty->root->root_item); 688 num_bytes -= btrfs_root_used(&dirty->root->root_item);
689 bytes_used = btrfs_root_used(&root->root_item); 689 bytes_used = btrfs_root_used(&root->root_item);
690 if (num_bytes) { 690 if (num_bytes) {
691 mutex_lock(&root->fs_info->trans_mutex);
691 btrfs_record_root_in_trans(root); 692 btrfs_record_root_in_trans(root);
693 mutex_unlock(&root->fs_info->trans_mutex);
692 btrfs_set_root_used(&root->root_item, 694 btrfs_set_root_used(&root->root_item,
693 bytes_used - num_bytes); 695 bytes_used - num_bytes);
694 } 696 }
diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
index 20794290256b..9c462fbd60fa 100644
--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -2832,7 +2832,9 @@ again:
2832 BUG_ON(!wc.replay_dest); 2832 BUG_ON(!wc.replay_dest);
2833 2833
2834 wc.replay_dest->log_root = log; 2834 wc.replay_dest->log_root = log;
2835 mutex_lock(&fs_info->trans_mutex);
2835 btrfs_record_root_in_trans(wc.replay_dest); 2836 btrfs_record_root_in_trans(wc.replay_dest);
2837 mutex_unlock(&fs_info->trans_mutex);
2836 ret = walk_log_tree(trans, log, &wc); 2838 ret = walk_log_tree(trans, log, &wc);
2837 BUG_ON(ret); 2839 BUG_ON(ret);
2838 2840
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index bcd14ebccae1..1316139bf9e8 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -2894,10 +2894,6 @@ static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
2894 free_extent_map(em); 2894 free_extent_map(em);
2895 } 2895 }
2896 2896
2897 map = kzalloc(sizeof(*map), GFP_NOFS);
2898 if (!map)
2899 return -ENOMEM;
2900
2901 em = alloc_extent_map(GFP_NOFS); 2897 em = alloc_extent_map(GFP_NOFS);
2902 if (!em) 2898 if (!em)
2903 return -ENOMEM; 2899 return -ENOMEM;
@@ -3106,6 +3102,8 @@ int btrfs_read_sys_array(struct btrfs_root *root)
3106 if (!sb) 3102 if (!sb)
3107 return -ENOMEM; 3103 return -ENOMEM;
3108 btrfs_set_buffer_uptodate(sb); 3104 btrfs_set_buffer_uptodate(sb);
3105 btrfs_set_buffer_lockdep_class(sb, 0);
3106
3109 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE); 3107 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
3110 array_size = btrfs_super_sys_array_size(super_copy); 3108 array_size = btrfs_super_sys_array_size(super_copy);
3111 3109