aboutsummaryrefslogtreecommitdiffstats
path: root/fs
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
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')
-rw-r--r--fs/btrfs/ctree.h4
-rw-r--r--fs/btrfs/disk-io.c5
-rw-r--r--fs/btrfs/extent_map.c20
-rw-r--r--fs/btrfs/extent_map.h3
-rw-r--r--fs/btrfs/file.c28
-rw-r--r--fs/btrfs/inode.c161
6 files changed, 193 insertions, 28 deletions
diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index 2559f5d5d00a..10129cc6656f 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -346,6 +346,8 @@ struct btrfs_fs_info {
346 int closing; 346 int closing;
347 347
348 u64 total_pinned; 348 u64 total_pinned;
349 spinlock_t delalloc_lock;
350 u64 delalloc_bytes;
349}; 351};
350/* 352/*
351 * in ram representation of the tree. extent_root is used for all allocations 353 * in ram representation of the tree. extent_root is used for all allocations
@@ -1115,6 +1117,8 @@ int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
1115 struct btrfs_root *root, struct btrfs_path *path, 1117 struct btrfs_root *root, struct btrfs_path *path,
1116 u64 isize); 1118 u64 isize);
1117/* inode.c */ 1119/* inode.c */
1120int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
1121 int for_del);
1118int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page); 1122int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page);
1119int btrfs_readpage(struct file *file, struct page *page); 1123int btrfs_readpage(struct file *file, struct page *page);
1120void btrfs_delete_inode(struct inode *inode); 1124void btrfs_delete_inode(struct inode *inode);
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index ebb2db624fdd..eebb4fb65c61 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -223,7 +223,8 @@ static int btree_writepages(struct address_space *mapping,
223 } else { 223 } else {
224 thresh = 8 * 1024 * 1024; 224 thresh = 8 * 1024 * 1024;
225 } 225 }
226 num_dirty = count_range_bits(tree, &start, thresh, EXTENT_DIRTY); 226 num_dirty = count_range_bits(tree, &start, (u64)-1,
227 thresh, EXTENT_DIRTY);
227 if (num_dirty < thresh) { 228 if (num_dirty < thresh) {
228 return 0; 229 return 0;
229 } 230 }
@@ -559,6 +560,7 @@ struct btrfs_root *open_ctree(struct super_block *sb)
559 INIT_LIST_HEAD(&fs_info->dead_roots); 560 INIT_LIST_HEAD(&fs_info->dead_roots);
560 INIT_LIST_HEAD(&fs_info->hashers); 561 INIT_LIST_HEAD(&fs_info->hashers);
561 spin_lock_init(&fs_info->hash_lock); 562 spin_lock_init(&fs_info->hash_lock);
563 spin_lock_init(&fs_info->delalloc_lock);
562 564
563 memset(&fs_info->super_kobj, 0, sizeof(fs_info->super_kobj)); 565 memset(&fs_info->super_kobj, 0, sizeof(fs_info->super_kobj));
564 init_completion(&fs_info->kobj_unregister); 566 init_completion(&fs_info->kobj_unregister);
@@ -570,6 +572,7 @@ struct btrfs_root *open_ctree(struct super_block *sb)
570 fs_info->sb = sb; 572 fs_info->sb = sb;
571 fs_info->mount_opt = 0; 573 fs_info->mount_opt = 0;
572 fs_info->max_extent = (u64)-1; 574 fs_info->max_extent = (u64)-1;
575 fs_info->delalloc_bytes = 0;
573 fs_info->btree_inode = new_inode(sb); 576 fs_info->btree_inode = new_inode(sb);
574 fs_info->btree_inode->i_ino = 1; 577 fs_info->btree_inode->i_ino = 1;
575 fs_info->btree_inode->i_nlink = 1; 578 fs_info->btree_inode->i_nlink = 1;
diff --git a/fs/btrfs/extent_map.c b/fs/btrfs/extent_map.c
index a0dff34dd437..2b92f1070274 100644
--- a/fs/btrfs/extent_map.c
+++ b/fs/btrfs/extent_map.c
@@ -1131,7 +1131,8 @@ out:
1131} 1131}
1132 1132
1133u64 count_range_bits(struct extent_map_tree *tree, 1133u64 count_range_bits(struct extent_map_tree *tree,
1134 u64 *start, u64 max_bytes, unsigned long bits) 1134 u64 *start, u64 search_end, u64 max_bytes,
1135 unsigned long bits)
1135{ 1136{
1136 struct rb_node *node; 1137 struct rb_node *node;
1137 struct extent_state *state; 1138 struct extent_state *state;
@@ -1139,9 +1140,14 @@ u64 count_range_bits(struct extent_map_tree *tree,
1139 u64 total_bytes = 0; 1140 u64 total_bytes = 0;
1140 int found = 0; 1141 int found = 0;
1141 1142
1143 if (search_end <= cur_start) {
1144 printk("search_end %Lu start %Lu\n", search_end, cur_start);
1145 WARN_ON(1);
1146 return 0;
1147 }
1148
1142 write_lock_irq(&tree->lock); 1149 write_lock_irq(&tree->lock);
1143 if (bits == EXTENT_DIRTY) { 1150 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1144 *start = 0;
1145 total_bytes = tree->dirty_bytes; 1151 total_bytes = tree->dirty_bytes;
1146 goto out; 1152 goto out;
1147 } 1153 }
@@ -1156,8 +1162,11 @@ u64 count_range_bits(struct extent_map_tree *tree,
1156 1162
1157 while(1) { 1163 while(1) {
1158 state = rb_entry(node, struct extent_state, rb_node); 1164 state = rb_entry(node, struct extent_state, rb_node);
1159 if ((state->state & bits)) { 1165 if (state->start > search_end)
1160 total_bytes += state->end - state->start + 1; 1166 break;
1167 if (state->end >= cur_start && (state->state & bits)) {
1168 total_bytes += min(search_end, state->end) + 1 -
1169 max(cur_start, state->start);
1161 if (total_bytes >= max_bytes) 1170 if (total_bytes >= max_bytes)
1162 break; 1171 break;
1163 if (!found) { 1172 if (!found) {
@@ -1173,7 +1182,6 @@ out:
1173 write_unlock_irq(&tree->lock); 1182 write_unlock_irq(&tree->lock);
1174 return total_bytes; 1183 return total_bytes;
1175} 1184}
1176
1177/* 1185/*
1178 * helper function to lock both pages and extents in the tree. 1186 * helper function to lock both pages and extents in the tree.
1179 * pages must be locked first. 1187 * pages must be locked first.
diff --git a/fs/btrfs/extent_map.h b/fs/btrfs/extent_map.h
index 6e572d3e8924..ea60f5447b5b 100644
--- a/fs/btrfs/extent_map.h
+++ b/fs/btrfs/extent_map.h
@@ -115,7 +115,8 @@ int __init extent_map_init(void);
115void extent_map_exit(void); 115void extent_map_exit(void);
116 116
117u64 count_range_bits(struct extent_map_tree *tree, 117u64 count_range_bits(struct extent_map_tree *tree,
118 u64 *start, u64 max_bytes, unsigned long bits); 118 u64 *start, u64 search_end,
119 u64 max_bytes, unsigned long bits);
119 120
120int test_range_bit(struct extent_map_tree *tree, u64 start, u64 end, 121int test_range_bit(struct extent_map_tree *tree, u64 start, u64 end,
121 int bits, int filled); 122 int bits, int filled);
diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
index 461b09663fed..71dc2d33b6c6 100644
--- a/fs/btrfs/file.c
+++ b/fs/btrfs/file.c
@@ -307,6 +307,7 @@ static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
307 inline_size > 32768 || 307 inline_size > 32768 ||
308 inline_size >= BTRFS_MAX_INLINE_DATA_SIZE(root)) { 308 inline_size >= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
309 u64 last_end; 309 u64 last_end;
310 u64 existing_delalloc = 0;
310 311
311 for (i = 0; i < num_pages; i++) { 312 for (i = 0; i < num_pages; i++) {
312 struct page *p = pages[i]; 313 struct page *p = pages[i];
@@ -316,8 +317,19 @@ static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
316 last_end = (u64)(pages[num_pages -1]->index) << 317 last_end = (u64)(pages[num_pages -1]->index) <<
317 PAGE_CACHE_SHIFT; 318 PAGE_CACHE_SHIFT;
318 last_end += PAGE_CACHE_SIZE - 1; 319 last_end += PAGE_CACHE_SIZE - 1;
320 if (start_pos < isize) {
321 u64 delalloc_start = start_pos;
322 existing_delalloc = count_range_bits(em_tree,
323 &delalloc_start,
324 end_of_last_block, (u64)-1,
325 EXTENT_DELALLOC);
326 }
319 set_extent_delalloc(em_tree, start_pos, end_of_last_block, 327 set_extent_delalloc(em_tree, start_pos, end_of_last_block,
320 GFP_NOFS); 328 GFP_NOFS);
329 spin_lock(&root->fs_info->delalloc_lock);
330 root->fs_info->delalloc_bytes += (end_of_last_block + 1 -
331 start_pos) - existing_delalloc;
332 spin_unlock(&root->fs_info->delalloc_lock);
321 } else { 333 } else {
322 u64 aligned_end; 334 u64 aligned_end;
323 /* step one, delete the existing extents in this range */ 335 /* step one, delete the existing extents in this range */
@@ -708,12 +720,12 @@ static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
708 current->backing_dev_info = inode->i_mapping->backing_dev_info; 720 current->backing_dev_info = inode->i_mapping->backing_dev_info;
709 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); 721 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
710 if (err) 722 if (err)
711 goto out; 723 goto out_nolock;
712 if (count == 0) 724 if (count == 0)
713 goto out; 725 goto out_nolock;
714 err = remove_suid(fdentry(file)); 726 err = remove_suid(fdentry(file));
715 if (err) 727 if (err)
716 goto out; 728 goto out_nolock;
717 file_update_time(file); 729 file_update_time(file);
718 730
719 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL); 731 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
@@ -758,6 +770,13 @@ static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
758 770
759 WARN_ON(num_pages > nrptrs); 771 WARN_ON(num_pages > nrptrs);
760 memset(pages, 0, sizeof(pages)); 772 memset(pages, 0, sizeof(pages));
773
774 mutex_lock(&root->fs_info->fs_mutex);
775 ret = btrfs_check_free_space(root, write_bytes, 0);
776 mutex_unlock(&root->fs_info->fs_mutex);
777 if (ret)
778 goto out;
779
761 ret = prepare_pages(root, file, pages, num_pages, 780 ret = prepare_pages(root, file, pages, num_pages,
762 pos, first_index, last_index, 781 pos, first_index, last_index,
763 write_bytes); 782 write_bytes);
@@ -787,8 +806,9 @@ static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
787 btrfs_btree_balance_dirty(root, 1); 806 btrfs_btree_balance_dirty(root, 1);
788 cond_resched(); 807 cond_resched();
789 } 808 }
790 mutex_unlock(&inode->i_mutex);
791out: 809out:
810 mutex_unlock(&inode->i_mutex);
811out_nolock:
792 kfree(pages); 812 kfree(pages);
793 if (pinned[0]) 813 if (pinned[0])
794 page_cache_release(pinned[0]); 814 page_cache_release(pinned[0]);
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);