aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/inode.c
diff options
context:
space:
mode:
authorChris Mason <chris.mason@oracle.com>2007-12-21 16:27:21 -0500
committerChris Mason <chris.mason@oracle.com>2008-09-25 11:03:58 -0400
commit1832a6d5ee3b1af61001cadba9e10da9e91af4a4 (patch)
tree3a6dcb2a186c3623b0355b7e45d1b94ecae5e54d /fs/btrfs/inode.c
parent01f466580502c57001bf80fff709479fdb9e87a5 (diff)
Btrfs: Implement basic support for -ENOSPC
This is intended to prevent accidentally filling the drive. A determined user can still make things oops. It includes some accounting of the current bytes under delayed allocation, but this will change as things get optimized Signed-off-by: Chris Mason <chris.mason@oracle.com>
Diffstat (limited to 'fs/btrfs/inode.c')
-rw-r--r--fs/btrfs/inode.c161
1 files changed, 145 insertions, 16 deletions
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 1c61c85dc3d8..a9f5d6d417f0 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -72,6 +72,26 @@ static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
72 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, 72 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
73}; 73};
74 74
75int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
76 int for_del)
77{
78 u64 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
79 u64 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
80 u64 thresh;
81 int ret = 0;
82
83 if (for_del)
84 thresh = (total * 90) / 100;
85 else
86 thresh = (total * 85) / 100;
87
88 spin_lock(&root->fs_info->delalloc_lock);
89 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
90 ret = -ENOSPC;
91 spin_unlock(&root->fs_info->delalloc_lock);
92 return ret;
93}
94
75static int cow_file_range(struct inode *inode, u64 start, u64 end) 95static int cow_file_range(struct inode *inode, u64 start, u64 end)
76{ 96{
77 struct btrfs_root *root = BTRFS_I(inode)->root; 97 struct btrfs_root *root = BTRFS_I(inode)->root;
@@ -124,6 +144,7 @@ static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
124 u64 extent_end; 144 u64 extent_end;
125 u64 bytenr; 145 u64 bytenr;
126 u64 cow_end; 146 u64 cow_end;
147 u64 loops = 0;
127 struct btrfs_root *root = BTRFS_I(inode)->root; 148 struct btrfs_root *root = BTRFS_I(inode)->root;
128 struct extent_buffer *leaf; 149 struct extent_buffer *leaf;
129 int found_type; 150 int found_type;
@@ -169,6 +190,9 @@ again:
169 btrfs_file_extent_num_bytes(leaf, item); 190 btrfs_file_extent_num_bytes(leaf, item);
170 err = 0; 191 err = 0;
171 192
193 if (loops && start != extent_start)
194 goto not_found;
195
172 if (start < extent_start || start >= extent_end) 196 if (start < extent_start || start >= extent_end)
173 goto not_found; 197 goto not_found;
174 198
@@ -191,6 +215,7 @@ loop:
191 return 0; 215 return 0;
192 } 216 }
193 btrfs_release_path(root, path); 217 btrfs_release_path(root, path);
218 loops++;
194 goto again; 219 goto again;
195 220
196not_found: 221not_found:
@@ -202,6 +227,7 @@ not_found:
202static int run_delalloc_range(struct inode *inode, u64 start, u64 end) 227static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
203{ 228{
204 struct btrfs_root *root = BTRFS_I(inode)->root; 229 struct btrfs_root *root = BTRFS_I(inode)->root;
230 u64 num_bytes;
205 int ret; 231 int ret;
206 232
207 mutex_lock(&root->fs_info->fs_mutex); 233 mutex_lock(&root->fs_info->fs_mutex);
@@ -209,6 +235,17 @@ static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
209 ret = run_delalloc_nocow(inode, start, end); 235 ret = run_delalloc_nocow(inode, start, end);
210 else 236 else
211 ret = cow_file_range(inode, start, end); 237 ret = cow_file_range(inode, start, end);
238
239 spin_lock(&root->fs_info->delalloc_lock);
240 num_bytes = end + 1 - start;
241 if (root->fs_info->delalloc_bytes < num_bytes) {
242 printk("delalloc accounting error total %llu sub %llu\n",
243 root->fs_info->delalloc_bytes, num_bytes);
244 } else {
245 root->fs_info->delalloc_bytes -= num_bytes;
246 }
247 spin_unlock(&root->fs_info->delalloc_lock);
248
212 mutex_unlock(&root->fs_info->fs_mutex); 249 mutex_unlock(&root->fs_info->fs_mutex);
213 return ret; 250 return ret;
214} 251}
@@ -547,10 +584,15 @@ static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
547 struct btrfs_root *root; 584 struct btrfs_root *root;
548 struct btrfs_trans_handle *trans; 585 struct btrfs_trans_handle *trans;
549 int ret; 586 int ret;
550 unsigned long nr; 587 unsigned long nr = 0;
551 588
552 root = BTRFS_I(dir)->root; 589 root = BTRFS_I(dir)->root;
553 mutex_lock(&root->fs_info->fs_mutex); 590 mutex_lock(&root->fs_info->fs_mutex);
591
592 ret = btrfs_check_free_space(root, 1, 1);
593 if (ret)
594 goto fail;
595
554 trans = btrfs_start_transaction(root, 1); 596 trans = btrfs_start_transaction(root, 1);
555 597
556 btrfs_set_trans_block_group(trans, dir); 598 btrfs_set_trans_block_group(trans, dir);
@@ -558,25 +600,29 @@ static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
558 nr = trans->blocks_used; 600 nr = trans->blocks_used;
559 601
560 btrfs_end_transaction(trans, root); 602 btrfs_end_transaction(trans, root);
603fail:
561 mutex_unlock(&root->fs_info->fs_mutex); 604 mutex_unlock(&root->fs_info->fs_mutex);
562 btrfs_btree_balance_dirty(root, nr); 605 btrfs_btree_balance_dirty(root, nr);
563
564 return ret; 606 return ret;
565} 607}
566 608
567static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) 609static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
568{ 610{
569 struct inode *inode = dentry->d_inode; 611 struct inode *inode = dentry->d_inode;
570 int err; 612 int err = 0;
571 int ret; 613 int ret;
572 struct btrfs_root *root = BTRFS_I(dir)->root; 614 struct btrfs_root *root = BTRFS_I(dir)->root;
573 struct btrfs_trans_handle *trans; 615 struct btrfs_trans_handle *trans;
574 unsigned long nr; 616 unsigned long nr = 0;
575 617
576 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) 618 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
577 return -ENOTEMPTY; 619 return -ENOTEMPTY;
578 620
579 mutex_lock(&root->fs_info->fs_mutex); 621 mutex_lock(&root->fs_info->fs_mutex);
622 ret = btrfs_check_free_space(root, 1, 1);
623 if (ret)
624 goto fail;
625
580 trans = btrfs_start_transaction(root, 1); 626 trans = btrfs_start_transaction(root, 1);
581 btrfs_set_trans_block_group(trans, dir); 627 btrfs_set_trans_block_group(trans, dir);
582 628
@@ -588,6 +634,7 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
588 634
589 nr = trans->blocks_used; 635 nr = trans->blocks_used;
590 ret = btrfs_end_transaction(trans, root); 636 ret = btrfs_end_transaction(trans, root);
637fail:
591 mutex_unlock(&root->fs_info->fs_mutex); 638 mutex_unlock(&root->fs_info->fs_mutex);
592 btrfs_btree_balance_dirty(root, nr); 639 btrfs_btree_balance_dirty(root, nr);
593 640
@@ -792,17 +839,29 @@ static int btrfs_cow_one_page(struct inode *inode, struct page *page,
792 size_t zero_start) 839 size_t zero_start)
793{ 840{
794 char *kaddr; 841 char *kaddr;
795 int ret = 0;
796 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 842 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
843 struct btrfs_root *root = BTRFS_I(inode)->root;
797 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT; 844 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
798 u64 page_end = page_start + PAGE_CACHE_SIZE - 1; 845 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
846 u64 existing_delalloc;
847 u64 delalloc_start;
848 int ret = 0;
799 849
800 WARN_ON(!PageLocked(page)); 850 WARN_ON(!PageLocked(page));
801 set_page_extent_mapped(page); 851 set_page_extent_mapped(page);
802 852
803 lock_extent(em_tree, page_start, page_end, GFP_NOFS); 853 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
854 delalloc_start = page_start;
855 existing_delalloc = count_range_bits(&BTRFS_I(inode)->extent_tree,
856 &delalloc_start, page_end,
857 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
804 set_extent_delalloc(&BTRFS_I(inode)->extent_tree, page_start, 858 set_extent_delalloc(&BTRFS_I(inode)->extent_tree, page_start,
805 page_end, GFP_NOFS); 859 page_end, GFP_NOFS);
860
861 spin_lock(&root->fs_info->delalloc_lock);
862 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE - existing_delalloc;
863 spin_unlock(&root->fs_info->delalloc_lock);
864
806 if (zero_start != PAGE_CACHE_SIZE) { 865 if (zero_start != PAGE_CACHE_SIZE) {
807 kaddr = kmap(page); 866 kaddr = kmap(page);
808 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); 867 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
@@ -881,6 +940,12 @@ static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
881 if (attr->ia_size <= pos) 940 if (attr->ia_size <= pos)
882 goto out; 941 goto out;
883 942
943 mutex_lock(&root->fs_info->fs_mutex);
944 err = btrfs_check_free_space(root, 1, 0);
945 mutex_unlock(&root->fs_info->fs_mutex);
946 if (err)
947 goto fail;
948
884 btrfs_truncate_page(inode->i_mapping, inode->i_size); 949 btrfs_truncate_page(inode->i_mapping, inode->i_size);
885 950
886 lock_extent(em_tree, pos, block_end, GFP_NOFS); 951 lock_extent(em_tree, pos, block_end, GFP_NOFS);
@@ -906,7 +971,7 @@ static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
906 } 971 }
907out: 972out:
908 err = inode_setattr(inode, attr); 973 err = inode_setattr(inode, attr);
909 974fail:
910 return err; 975 return err;
911} 976}
912void btrfs_delete_inode(struct inode *inode) 977void btrfs_delete_inode(struct inode *inode)
@@ -1440,16 +1505,20 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1440{ 1505{
1441 struct btrfs_trans_handle *trans; 1506 struct btrfs_trans_handle *trans;
1442 struct btrfs_root *root = BTRFS_I(dir)->root; 1507 struct btrfs_root *root = BTRFS_I(dir)->root;
1443 struct inode *inode; 1508 struct inode *inode = NULL;
1444 int err; 1509 int err;
1445 int drop_inode = 0; 1510 int drop_inode = 0;
1446 u64 objectid; 1511 u64 objectid;
1447 unsigned long nr; 1512 unsigned long nr = 0;
1448 1513
1449 if (!new_valid_dev(rdev)) 1514 if (!new_valid_dev(rdev))
1450 return -EINVAL; 1515 return -EINVAL;
1451 1516
1452 mutex_lock(&root->fs_info->fs_mutex); 1517 mutex_lock(&root->fs_info->fs_mutex);
1518 err = btrfs_check_free_space(root, 1, 0);
1519 if (err)
1520 goto fail;
1521
1453 trans = btrfs_start_transaction(root, 1); 1522 trans = btrfs_start_transaction(root, 1);
1454 btrfs_set_trans_block_group(trans, dir); 1523 btrfs_set_trans_block_group(trans, dir);
1455 1524
@@ -1480,6 +1549,7 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1480out_unlock: 1549out_unlock:
1481 nr = trans->blocks_used; 1550 nr = trans->blocks_used;
1482 btrfs_end_transaction(trans, root); 1551 btrfs_end_transaction(trans, root);
1552fail:
1483 mutex_unlock(&root->fs_info->fs_mutex); 1553 mutex_unlock(&root->fs_info->fs_mutex);
1484 1554
1485 if (drop_inode) { 1555 if (drop_inode) {
@@ -1495,13 +1565,16 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
1495{ 1565{
1496 struct btrfs_trans_handle *trans; 1566 struct btrfs_trans_handle *trans;
1497 struct btrfs_root *root = BTRFS_I(dir)->root; 1567 struct btrfs_root *root = BTRFS_I(dir)->root;
1498 struct inode *inode; 1568 struct inode *inode = NULL;
1499 int err; 1569 int err;
1500 int drop_inode = 0; 1570 int drop_inode = 0;
1501 unsigned long nr; 1571 unsigned long nr = 0;
1502 u64 objectid; 1572 u64 objectid;
1503 1573
1504 mutex_lock(&root->fs_info->fs_mutex); 1574 mutex_lock(&root->fs_info->fs_mutex);
1575 err = btrfs_check_free_space(root, 1, 0);
1576 if (err)
1577 goto fail;
1505 trans = btrfs_start_transaction(root, 1); 1578 trans = btrfs_start_transaction(root, 1);
1506 btrfs_set_trans_block_group(trans, dir); 1579 btrfs_set_trans_block_group(trans, dir);
1507 1580
@@ -1535,6 +1608,7 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
1535out_unlock: 1608out_unlock:
1536 nr = trans->blocks_used; 1609 nr = trans->blocks_used;
1537 btrfs_end_transaction(trans, root); 1610 btrfs_end_transaction(trans, root);
1611fail:
1538 mutex_unlock(&root->fs_info->fs_mutex); 1612 mutex_unlock(&root->fs_info->fs_mutex);
1539 1613
1540 if (drop_inode) { 1614 if (drop_inode) {
@@ -1551,7 +1625,7 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1551 struct btrfs_trans_handle *trans; 1625 struct btrfs_trans_handle *trans;
1552 struct btrfs_root *root = BTRFS_I(dir)->root; 1626 struct btrfs_root *root = BTRFS_I(dir)->root;
1553 struct inode *inode = old_dentry->d_inode; 1627 struct inode *inode = old_dentry->d_inode;
1554 unsigned long nr; 1628 unsigned long nr = 0;
1555 int err; 1629 int err;
1556 int drop_inode = 0; 1630 int drop_inode = 0;
1557 1631
@@ -1564,6 +1638,9 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1564 inc_nlink(inode); 1638 inc_nlink(inode);
1565#endif 1639#endif
1566 mutex_lock(&root->fs_info->fs_mutex); 1640 mutex_lock(&root->fs_info->fs_mutex);
1641 err = btrfs_check_free_space(root, 1, 0);
1642 if (err)
1643 goto fail;
1567 trans = btrfs_start_transaction(root, 1); 1644 trans = btrfs_start_transaction(root, 1);
1568 1645
1569 btrfs_set_trans_block_group(trans, dir); 1646 btrfs_set_trans_block_group(trans, dir);
@@ -1582,6 +1659,7 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1582 1659
1583 nr = trans->blocks_used; 1660 nr = trans->blocks_used;
1584 btrfs_end_transaction(trans, root); 1661 btrfs_end_transaction(trans, root);
1662fail:
1585 mutex_unlock(&root->fs_info->fs_mutex); 1663 mutex_unlock(&root->fs_info->fs_mutex);
1586 1664
1587 if (drop_inode) { 1665 if (drop_inode) {
@@ -1603,6 +1681,10 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1603 unsigned long nr = 1; 1681 unsigned long nr = 1;
1604 1682
1605 mutex_lock(&root->fs_info->fs_mutex); 1683 mutex_lock(&root->fs_info->fs_mutex);
1684 err = btrfs_check_free_space(root, 1, 0);
1685 if (err)
1686 goto out_unlock;
1687
1606 trans = btrfs_start_transaction(root, 1); 1688 trans = btrfs_start_transaction(root, 1);
1607 btrfs_set_trans_block_group(trans, dir); 1689 btrfs_set_trans_block_group(trans, dir);
1608 1690
@@ -1869,6 +1951,15 @@ static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
1869static int btrfs_prepare_write(struct file *file, struct page *page, 1951static int btrfs_prepare_write(struct file *file, struct page *page,
1870 unsigned from, unsigned to) 1952 unsigned from, unsigned to)
1871{ 1953{
1954 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1955 int err;
1956
1957 mutex_lock(&root->fs_info->fs_mutex);
1958 err = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
1959 mutex_lock(&root->fs_info->fs_mutex);
1960 if (err)
1961 return -ENOSPC;
1962
1872 return extent_prepare_write(&BTRFS_I(page->mapping->host)->extent_tree, 1963 return extent_prepare_write(&BTRFS_I(page->mapping->host)->extent_tree,
1873 page->mapping->host, page, from, to, 1964 page->mapping->host, page, from, to,
1874 btrfs_get_extent); 1965 btrfs_get_extent);
@@ -1880,6 +1971,7 @@ int btrfs_readpage(struct file *file, struct page *page)
1880 tree = &BTRFS_I(page->mapping->host)->extent_tree; 1971 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1881 return extent_read_full_page(tree, page, btrfs_get_extent); 1972 return extent_read_full_page(tree, page, btrfs_get_extent);
1882} 1973}
1974
1883static int btrfs_writepage(struct page *page, struct writeback_control *wbc) 1975static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1884{ 1976{
1885 struct extent_map_tree *tree; 1977 struct extent_map_tree *tree;
@@ -1954,11 +2046,20 @@ static void btrfs_invalidatepage(struct page *page, unsigned long offset)
1954int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page) 2046int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
1955{ 2047{
1956 struct inode *inode = fdentry(vma->vm_file)->d_inode; 2048 struct inode *inode = fdentry(vma->vm_file)->d_inode;
2049 struct btrfs_root *root = BTRFS_I(inode)->root;
1957 unsigned long end; 2050 unsigned long end;
1958 loff_t size; 2051 loff_t size;
1959 int ret = -EINVAL; 2052 int ret;
1960 u64 page_start; 2053 u64 page_start;
1961 2054
2055 mutex_lock(&root->fs_info->fs_mutex);
2056 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
2057 mutex_lock(&root->fs_info->fs_mutex);
2058 if (ret)
2059 goto out;
2060
2061 ret = -EINVAL;
2062
1962 down_read(&BTRFS_I(inode)->root->snap_sem); 2063 down_read(&BTRFS_I(inode)->root->snap_sem);
1963 lock_page(page); 2064 lock_page(page);
1964 wait_on_page_writeback(page); 2065 wait_on_page_writeback(page);
@@ -1982,6 +2083,7 @@ int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
1982out_unlock: 2083out_unlock:
1983 up_read(&BTRFS_I(inode)->root->snap_sem); 2084 up_read(&BTRFS_I(inode)->root->snap_sem);
1984 unlock_page(page); 2085 unlock_page(page);
2086out:
1985 return ret; 2087 return ret;
1986} 2088}
1987 2089
@@ -2046,6 +2148,10 @@ static int create_subvol(struct btrfs_root *root, char *name, int namelen)
2046 unsigned long nr = 1; 2148 unsigned long nr = 1;
2047 2149
2048 mutex_lock(&root->fs_info->fs_mutex); 2150 mutex_lock(&root->fs_info->fs_mutex);
2151 ret = btrfs_check_free_space(root, 1, 0);
2152 if (ret)
2153 goto fail_commit;
2154
2049 trans = btrfs_start_transaction(root, 1); 2155 trans = btrfs_start_transaction(root, 1);
2050 BUG_ON(!trans); 2156 BUG_ON(!trans);
2051 2157
@@ -2162,7 +2268,7 @@ static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2162 int ret; 2268 int ret;
2163 int err; 2269 int err;
2164 u64 objectid; 2270 u64 objectid;
2165 unsigned long nr; 2271 unsigned long nr = 0;
2166 2272
2167 if (!root->ref_cows) 2273 if (!root->ref_cows)
2168 return -EINVAL; 2274 return -EINVAL;
@@ -2172,6 +2278,10 @@ static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2172 thaw_bdev(root->fs_info->sb->s_bdev, root->fs_info->sb); 2278 thaw_bdev(root->fs_info->sb->s_bdev, root->fs_info->sb);
2173 2279
2174 mutex_lock(&root->fs_info->fs_mutex); 2280 mutex_lock(&root->fs_info->fs_mutex);
2281 ret = btrfs_check_free_space(root, 1, 0);
2282 if (ret)
2283 goto fail_unlock;
2284
2175 trans = btrfs_start_transaction(root, 1); 2285 trans = btrfs_start_transaction(root, 1);
2176 BUG_ON(!trans); 2286 BUG_ON(!trans);
2177 2287
@@ -2229,7 +2339,7 @@ fail:
2229 2339
2230 if (err && !ret) 2340 if (err && !ret)
2231 ret = err; 2341 ret = err;
2232 2342fail_unlock:
2233 mutex_unlock(&root->fs_info->fs_mutex); 2343 mutex_unlock(&root->fs_info->fs_mutex);
2234 up_write(&root->snap_sem); 2344 up_write(&root->snap_sem);
2235 btrfs_btree_balance_dirty(root, nr); 2345 btrfs_btree_balance_dirty(root, nr);
@@ -2255,6 +2365,7 @@ static unsigned long force_ra(struct address_space *mapping,
2255 2365
2256int btrfs_defrag_file(struct file *file) { 2366int btrfs_defrag_file(struct file *file) {
2257 struct inode *inode = fdentry(file)->d_inode; 2367 struct inode *inode = fdentry(file)->d_inode;
2368 struct btrfs_root *root = BTRFS_I(inode)->root;
2258 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 2369 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2259 struct page *page; 2370 struct page *page;
2260 unsigned long last_index; 2371 unsigned long last_index;
@@ -2262,6 +2373,13 @@ int btrfs_defrag_file(struct file *file) {
2262 u64 page_start; 2373 u64 page_start;
2263 u64 page_end; 2374 u64 page_end;
2264 unsigned long i; 2375 unsigned long i;
2376 int ret;
2377
2378 mutex_lock(&root->fs_info->fs_mutex);
2379 ret = btrfs_check_free_space(root, inode->i_size, 0);
2380 mutex_unlock(&root->fs_info->fs_mutex);
2381 if (ret)
2382 return -ENOSPC;
2265 2383
2266 mutex_lock(&inode->i_mutex); 2384 mutex_lock(&inode->i_mutex);
2267 last_index = inode->i_size >> PAGE_CACHE_SHIFT; 2385 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
@@ -2522,6 +2640,10 @@ static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2522 } 2640 }
2523 2641
2524 mutex_lock(&root->fs_info->fs_mutex); 2642 mutex_lock(&root->fs_info->fs_mutex);
2643 ret = btrfs_check_free_space(root, 1, 0);
2644 if (ret)
2645 goto out_unlock;
2646
2525 trans = btrfs_start_transaction(root, 1); 2647 trans = btrfs_start_transaction(root, 1);
2526 2648
2527 btrfs_set_trans_block_group(trans, new_dir); 2649 btrfs_set_trans_block_group(trans, new_dir);
@@ -2553,6 +2675,7 @@ static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2553out_fail: 2675out_fail:
2554 btrfs_free_path(path); 2676 btrfs_free_path(path);
2555 btrfs_end_transaction(trans, root); 2677 btrfs_end_transaction(trans, root);
2678out_unlock:
2556 mutex_unlock(&root->fs_info->fs_mutex); 2679 mutex_unlock(&root->fs_info->fs_mutex);
2557 return ret; 2680 return ret;
2558} 2681}
@@ -2564,7 +2687,7 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2564 struct btrfs_root *root = BTRFS_I(dir)->root; 2687 struct btrfs_root *root = BTRFS_I(dir)->root;
2565 struct btrfs_path *path; 2688 struct btrfs_path *path;
2566 struct btrfs_key key; 2689 struct btrfs_key key;
2567 struct inode *inode; 2690 struct inode *inode = NULL;
2568 int err; 2691 int err;
2569 int drop_inode = 0; 2692 int drop_inode = 0;
2570 u64 objectid; 2693 u64 objectid;
@@ -2573,12 +2696,17 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2573 unsigned long ptr; 2696 unsigned long ptr;
2574 struct btrfs_file_extent_item *ei; 2697 struct btrfs_file_extent_item *ei;
2575 struct extent_buffer *leaf; 2698 struct extent_buffer *leaf;
2576 unsigned long nr; 2699 unsigned long nr = 0;
2577 2700
2578 name_len = strlen(symname) + 1; 2701 name_len = strlen(symname) + 1;
2579 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) 2702 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2580 return -ENAMETOOLONG; 2703 return -ENAMETOOLONG;
2704
2581 mutex_lock(&root->fs_info->fs_mutex); 2705 mutex_lock(&root->fs_info->fs_mutex);
2706 err = btrfs_check_free_space(root, 1, 0);
2707 if (err)
2708 goto out_fail;
2709
2582 trans = btrfs_start_transaction(root, 1); 2710 trans = btrfs_start_transaction(root, 1);
2583 btrfs_set_trans_block_group(trans, dir); 2711 btrfs_set_trans_block_group(trans, dir);
2584 2712
@@ -2645,6 +2773,7 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2645out_unlock: 2773out_unlock:
2646 nr = trans->blocks_used; 2774 nr = trans->blocks_used;
2647 btrfs_end_transaction(trans, root); 2775 btrfs_end_transaction(trans, root);
2776out_fail:
2648 mutex_unlock(&root->fs_info->fs_mutex); 2777 mutex_unlock(&root->fs_info->fs_mutex);
2649 if (drop_inode) { 2778 if (drop_inode) {
2650 inode_dec_link_count(inode); 2779 inode_dec_link_count(inode);