aboutsummaryrefslogtreecommitdiffstats
path: root/fs
diff options
context:
space:
mode:
authorChris Mason <chris.mason@fusionio.com>2013-02-05 19:24:44 -0500
committerChris Mason <chris.mason@fusionio.com>2013-02-05 19:24:44 -0500
commit24f8ebe9189c91f1b3ecfaadd27ef0cb7c74907f (patch)
tree80856cf81ec5cb377f7d871233dad0f0283a0c97 /fs
parent3c911608085bf2d5a0822c418129f96a2a89d1b5 (diff)
parent59fe4f41976f6331b695ff049296d082cf621823 (diff)
Merge git://git.kernel.org/pub/scm/linux/kernel/git/josef/btrfs-next.git for-chris into for-linus
Diffstat (limited to 'fs')
-rw-r--r--fs/btrfs/extent-tree.c6
-rw-r--r--fs/btrfs/extent_map.c14
-rw-r--r--fs/btrfs/extent_map.h1
-rw-r--r--fs/btrfs/file-item.c4
-rw-r--r--fs/btrfs/file.c35
-rw-r--r--fs/btrfs/free-space-cache.c20
-rw-r--r--fs/btrfs/inode.c137
-rw-r--r--fs/btrfs/ioctl.c129
-rw-r--r--fs/btrfs/ordered-data.c13
-rw-r--r--fs/btrfs/qgroup.c20
-rw-r--r--fs/btrfs/scrub.c25
-rw-r--r--fs/btrfs/send.c4
-rw-r--r--fs/btrfs/super.c2
-rw-r--r--fs/btrfs/transaction.c46
-rw-r--r--fs/btrfs/tree-log.c10
-rw-r--r--fs/btrfs/volumes.c23
16 files changed, 370 insertions, 119 deletions
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index d133edfcd449..85b8454d9608 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -3997,7 +3997,7 @@ again:
3997 * We make the other tasks wait for the flush only when we can flush 3997 * We make the other tasks wait for the flush only when we can flush
3998 * all things. 3998 * all things.
3999 */ 3999 */
4000 if (ret && flush == BTRFS_RESERVE_FLUSH_ALL) { 4000 if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
4001 flushing = true; 4001 flushing = true;
4002 space_info->flush = 1; 4002 space_info->flush = 1;
4003 } 4003 }
@@ -5560,7 +5560,7 @@ static noinline int find_free_extent(struct btrfs_trans_handle *trans,
5560 int empty_cluster = 2 * 1024 * 1024; 5560 int empty_cluster = 2 * 1024 * 1024;
5561 struct btrfs_space_info *space_info; 5561 struct btrfs_space_info *space_info;
5562 int loop = 0; 5562 int loop = 0;
5563 int index = 0; 5563 int index = __get_raid_index(data);
5564 int alloc_type = (data & BTRFS_BLOCK_GROUP_DATA) ? 5564 int alloc_type = (data & BTRFS_BLOCK_GROUP_DATA) ?
5565 RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC; 5565 RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
5566 bool found_uncached_bg = false; 5566 bool found_uncached_bg = false;
@@ -6788,11 +6788,13 @@ static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
6788 &wc->flags[level]); 6788 &wc->flags[level]);
6789 if (ret < 0) { 6789 if (ret < 0) {
6790 btrfs_tree_unlock_rw(eb, path->locks[level]); 6790 btrfs_tree_unlock_rw(eb, path->locks[level]);
6791 path->locks[level] = 0;
6791 return ret; 6792 return ret;
6792 } 6793 }
6793 BUG_ON(wc->refs[level] == 0); 6794 BUG_ON(wc->refs[level] == 0);
6794 if (wc->refs[level] == 1) { 6795 if (wc->refs[level] == 1) {
6795 btrfs_tree_unlock_rw(eb, path->locks[level]); 6796 btrfs_tree_unlock_rw(eb, path->locks[level]);
6797 path->locks[level] = 0;
6796 return 1; 6798 return 1;
6797 } 6799 }
6798 } 6800 }
diff --git a/fs/btrfs/extent_map.c b/fs/btrfs/extent_map.c
index fff2c28497b6..9759911dd34e 100644
--- a/fs/btrfs/extent_map.c
+++ b/fs/btrfs/extent_map.c
@@ -171,6 +171,10 @@ static int mergable_maps(struct extent_map *prev, struct extent_map *next)
171 if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags)) 171 if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags))
172 return 0; 172 return 0;
173 173
174 if (test_bit(EXTENT_FLAG_LOGGING, &prev->flags) ||
175 test_bit(EXTENT_FLAG_LOGGING, &next->flags))
176 return 0;
177
174 if (extent_map_end(prev) == next->start && 178 if (extent_map_end(prev) == next->start &&
175 prev->flags == next->flags && 179 prev->flags == next->flags &&
176 prev->bdev == next->bdev && 180 prev->bdev == next->bdev &&
@@ -256,7 +260,8 @@ int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len,
256 if (!em) 260 if (!em)
257 goto out; 261 goto out;
258 262
259 list_move(&em->list, &tree->modified_extents); 263 if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags))
264 list_move(&em->list, &tree->modified_extents);
260 em->generation = gen; 265 em->generation = gen;
261 clear_bit(EXTENT_FLAG_PINNED, &em->flags); 266 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
262 em->mod_start = em->start; 267 em->mod_start = em->start;
@@ -281,6 +286,13 @@ out:
281 286
282} 287}
283 288
289void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em)
290{
291 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
292 if (em->in_tree)
293 try_merge_map(tree, em);
294}
295
284/** 296/**
285 * add_extent_mapping - add new extent map to the extent tree 297 * add_extent_mapping - add new extent map to the extent tree
286 * @tree: tree to insert new map in 298 * @tree: tree to insert new map in
diff --git a/fs/btrfs/extent_map.h b/fs/btrfs/extent_map.h
index 922943ce29e8..c6598c89cff8 100644
--- a/fs/btrfs/extent_map.h
+++ b/fs/btrfs/extent_map.h
@@ -69,6 +69,7 @@ void free_extent_map(struct extent_map *em);
69int __init extent_map_init(void); 69int __init extent_map_init(void);
70void extent_map_exit(void); 70void extent_map_exit(void);
71int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len, u64 gen); 71int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len, u64 gen);
72void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em);
72struct extent_map *search_extent_mapping(struct extent_map_tree *tree, 73struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
73 u64 start, u64 len); 74 u64 start, u64 len);
74#endif 75#endif
diff --git a/fs/btrfs/file-item.c b/fs/btrfs/file-item.c
index bd38cef42358..94aa53b38721 100644
--- a/fs/btrfs/file-item.c
+++ b/fs/btrfs/file-item.c
@@ -460,8 +460,8 @@ int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode,
460 if (!contig) 460 if (!contig)
461 offset = page_offset(bvec->bv_page) + bvec->bv_offset; 461 offset = page_offset(bvec->bv_page) + bvec->bv_offset;
462 462
463 if (!contig && (offset >= ordered->file_offset + ordered->len || 463 if (offset >= ordered->file_offset + ordered->len ||
464 offset < ordered->file_offset)) { 464 offset < ordered->file_offset) {
465 unsigned long bytes_left; 465 unsigned long bytes_left;
466 sums->len = this_sum_bytes; 466 sums->len = this_sum_bytes;
467 this_sum_bytes = 0; 467 this_sum_bytes = 0;
diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
index 20452c110d7d..b06d289f998f 100644
--- a/fs/btrfs/file.c
+++ b/fs/btrfs/file.c
@@ -293,15 +293,24 @@ static int __btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info,
293 struct btrfs_key key; 293 struct btrfs_key key;
294 struct btrfs_ioctl_defrag_range_args range; 294 struct btrfs_ioctl_defrag_range_args range;
295 int num_defrag; 295 int num_defrag;
296 int index;
297 int ret;
296 298
297 /* get the inode */ 299 /* get the inode */
298 key.objectid = defrag->root; 300 key.objectid = defrag->root;
299 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); 301 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
300 key.offset = (u64)-1; 302 key.offset = (u64)-1;
303
304 index = srcu_read_lock(&fs_info->subvol_srcu);
305
301 inode_root = btrfs_read_fs_root_no_name(fs_info, &key); 306 inode_root = btrfs_read_fs_root_no_name(fs_info, &key);
302 if (IS_ERR(inode_root)) { 307 if (IS_ERR(inode_root)) {
303 kmem_cache_free(btrfs_inode_defrag_cachep, defrag); 308 ret = PTR_ERR(inode_root);
304 return PTR_ERR(inode_root); 309 goto cleanup;
310 }
311 if (btrfs_root_refs(&inode_root->root_item) == 0) {
312 ret = -ENOENT;
313 goto cleanup;
305 } 314 }
306 315
307 key.objectid = defrag->ino; 316 key.objectid = defrag->ino;
@@ -309,9 +318,10 @@ static int __btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info,
309 key.offset = 0; 318 key.offset = 0;
310 inode = btrfs_iget(fs_info->sb, &key, inode_root, NULL); 319 inode = btrfs_iget(fs_info->sb, &key, inode_root, NULL);
311 if (IS_ERR(inode)) { 320 if (IS_ERR(inode)) {
312 kmem_cache_free(btrfs_inode_defrag_cachep, defrag); 321 ret = PTR_ERR(inode);
313 return PTR_ERR(inode); 322 goto cleanup;
314 } 323 }
324 srcu_read_unlock(&fs_info->subvol_srcu, index);
315 325
316 /* do a chunk of defrag */ 326 /* do a chunk of defrag */
317 clear_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags); 327 clear_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags);
@@ -346,6 +356,10 @@ static int __btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info,
346 356
347 iput(inode); 357 iput(inode);
348 return 0; 358 return 0;
359cleanup:
360 srcu_read_unlock(&fs_info->subvol_srcu, index);
361 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
362 return ret;
349} 363}
350 364
351/* 365/*
@@ -1595,9 +1609,10 @@ static ssize_t btrfs_file_aio_write(struct kiocb *iocb,
1595 if (err < 0 && num_written > 0) 1609 if (err < 0 && num_written > 0)
1596 num_written = err; 1610 num_written = err;
1597 } 1611 }
1598out: 1612
1599 if (sync) 1613 if (sync)
1600 atomic_dec(&BTRFS_I(inode)->sync_writers); 1614 atomic_dec(&BTRFS_I(inode)->sync_writers);
1615out:
1601 sb_end_write(inode->i_sb); 1616 sb_end_write(inode->i_sb);
1602 current->backing_dev_info = NULL; 1617 current->backing_dev_info = NULL;
1603 return num_written ? num_written : err; 1618 return num_written ? num_written : err;
@@ -2242,6 +2257,7 @@ static int find_desired_extent(struct inode *inode, loff_t *offset, int origin)
2242 if (lockend <= lockstart) 2257 if (lockend <= lockstart)
2243 lockend = lockstart + root->sectorsize; 2258 lockend = lockstart + root->sectorsize;
2244 2259
2260 lockend--;
2245 len = lockend - lockstart + 1; 2261 len = lockend - lockstart + 1;
2246 2262
2247 len = max_t(u64, len, root->sectorsize); 2263 len = max_t(u64, len, root->sectorsize);
@@ -2308,9 +2324,12 @@ static int find_desired_extent(struct inode *inode, loff_t *offset, int origin)
2308 } 2324 }
2309 } 2325 }
2310 2326
2311 *offset = start; 2327 if (!test_bit(EXTENT_FLAG_PREALLOC,
2312 free_extent_map(em); 2328 &em->flags)) {
2313 break; 2329 *offset = start;
2330 free_extent_map(em);
2331 break;
2332 }
2314 } 2333 }
2315 } 2334 }
2316 2335
diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c
index 59ea2e4349c9..0be7a8742a43 100644
--- a/fs/btrfs/free-space-cache.c
+++ b/fs/btrfs/free-space-cache.c
@@ -1862,11 +1862,13 @@ int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
1862{ 1862{
1863 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; 1863 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1864 struct btrfs_free_space *info; 1864 struct btrfs_free_space *info;
1865 int ret = 0; 1865 int ret;
1866 bool re_search = false;
1866 1867
1867 spin_lock(&ctl->tree_lock); 1868 spin_lock(&ctl->tree_lock);
1868 1869
1869again: 1870again:
1871 ret = 0;
1870 if (!bytes) 1872 if (!bytes)
1871 goto out_lock; 1873 goto out_lock;
1872 1874
@@ -1879,17 +1881,17 @@ again:
1879 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), 1881 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
1880 1, 0); 1882 1, 0);
1881 if (!info) { 1883 if (!info) {
1882 /* the tree logging code might be calling us before we 1884 /*
1883 * have fully loaded the free space rbtree for this 1885 * If we found a partial bit of our free space in a
1884 * block group. So it is possible the entry won't 1886 * bitmap but then couldn't find the other part this may
1885 * be in the rbtree yet at all. The caching code 1887 * be a problem, so WARN about it.
1886 * will make sure not to put it in the rbtree if
1887 * the logging code has pinned it.
1888 */ 1888 */
1889 WARN_ON(re_search);
1889 goto out_lock; 1890 goto out_lock;
1890 } 1891 }
1891 } 1892 }
1892 1893
1894 re_search = false;
1893 if (!info->bitmap) { 1895 if (!info->bitmap) {
1894 unlink_free_space(ctl, info); 1896 unlink_free_space(ctl, info);
1895 if (offset == info->offset) { 1897 if (offset == info->offset) {
@@ -1935,8 +1937,10 @@ again:
1935 } 1937 }
1936 1938
1937 ret = remove_from_bitmap(ctl, info, &offset, &bytes); 1939 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
1938 if (ret == -EAGAIN) 1940 if (ret == -EAGAIN) {
1941 re_search = true;
1939 goto again; 1942 goto again;
1943 }
1940 BUG_ON(ret); /* logic error */ 1944 BUG_ON(ret); /* logic error */
1941out_lock: 1945out_lock:
1942 spin_unlock(&ctl->tree_lock); 1946 spin_unlock(&ctl->tree_lock);
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 67ed24ae86bb..ca7ace7b7b52 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -88,7 +88,7 @@ static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
88 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, 88 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
89}; 89};
90 90
91static int btrfs_setsize(struct inode *inode, loff_t newsize); 91static int btrfs_setsize(struct inode *inode, struct iattr *attr);
92static int btrfs_truncate(struct inode *inode); 92static int btrfs_truncate(struct inode *inode);
93static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); 93static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
94static noinline int cow_file_range(struct inode *inode, 94static noinline int cow_file_range(struct inode *inode,
@@ -2478,6 +2478,18 @@ int btrfs_orphan_cleanup(struct btrfs_root *root)
2478 continue; 2478 continue;
2479 } 2479 }
2480 nr_truncate++; 2480 nr_truncate++;
2481
2482 /* 1 for the orphan item deletion. */
2483 trans = btrfs_start_transaction(root, 1);
2484 if (IS_ERR(trans)) {
2485 ret = PTR_ERR(trans);
2486 goto out;
2487 }
2488 ret = btrfs_orphan_add(trans, inode);
2489 btrfs_end_transaction(trans, root);
2490 if (ret)
2491 goto out;
2492
2481 ret = btrfs_truncate(inode); 2493 ret = btrfs_truncate(inode);
2482 } else { 2494 } else {
2483 nr_unlink++; 2495 nr_unlink++;
@@ -3665,6 +3677,7 @@ int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
3665 block_end - cur_offset, 0); 3677 block_end - cur_offset, 0);
3666 if (IS_ERR(em)) { 3678 if (IS_ERR(em)) {
3667 err = PTR_ERR(em); 3679 err = PTR_ERR(em);
3680 em = NULL;
3668 break; 3681 break;
3669 } 3682 }
3670 last_byte = min(extent_map_end(em), block_end); 3683 last_byte = min(extent_map_end(em), block_end);
@@ -3748,16 +3761,27 @@ next:
3748 return err; 3761 return err;
3749} 3762}
3750 3763
3751static int btrfs_setsize(struct inode *inode, loff_t newsize) 3764static int btrfs_setsize(struct inode *inode, struct iattr *attr)
3752{ 3765{
3753 struct btrfs_root *root = BTRFS_I(inode)->root; 3766 struct btrfs_root *root = BTRFS_I(inode)->root;
3754 struct btrfs_trans_handle *trans; 3767 struct btrfs_trans_handle *trans;
3755 loff_t oldsize = i_size_read(inode); 3768 loff_t oldsize = i_size_read(inode);
3769 loff_t newsize = attr->ia_size;
3770 int mask = attr->ia_valid;
3756 int ret; 3771 int ret;
3757 3772
3758 if (newsize == oldsize) 3773 if (newsize == oldsize)
3759 return 0; 3774 return 0;
3760 3775
3776 /*
3777 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
3778 * special case where we need to update the times despite not having
3779 * these flags set. For all other operations the VFS set these flags
3780 * explicitly if it wants a timestamp update.
3781 */
3782 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
3783 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
3784
3761 if (newsize > oldsize) { 3785 if (newsize > oldsize) {
3762 truncate_pagecache(inode, oldsize, newsize); 3786 truncate_pagecache(inode, oldsize, newsize);
3763 ret = btrfs_cont_expand(inode, oldsize, newsize); 3787 ret = btrfs_cont_expand(inode, oldsize, newsize);
@@ -3783,9 +3807,34 @@ static int btrfs_setsize(struct inode *inode, loff_t newsize)
3783 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, 3807 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
3784 &BTRFS_I(inode)->runtime_flags); 3808 &BTRFS_I(inode)->runtime_flags);
3785 3809
3810 /*
3811 * 1 for the orphan item we're going to add
3812 * 1 for the orphan item deletion.
3813 */
3814 trans = btrfs_start_transaction(root, 2);
3815 if (IS_ERR(trans))
3816 return PTR_ERR(trans);
3817
3818 /*
3819 * We need to do this in case we fail at _any_ point during the
3820 * actual truncate. Once we do the truncate_setsize we could
3821 * invalidate pages which forces any outstanding ordered io to
3822 * be instantly completed which will give us extents that need
3823 * to be truncated. If we fail to get an orphan inode down we
3824 * could have left over extents that were never meant to live,
3825 * so we need to garuntee from this point on that everything
3826 * will be consistent.
3827 */
3828 ret = btrfs_orphan_add(trans, inode);
3829 btrfs_end_transaction(trans, root);
3830 if (ret)
3831 return ret;
3832
3786 /* we don't support swapfiles, so vmtruncate shouldn't fail */ 3833 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3787 truncate_setsize(inode, newsize); 3834 truncate_setsize(inode, newsize);
3788 ret = btrfs_truncate(inode); 3835 ret = btrfs_truncate(inode);
3836 if (ret && inode->i_nlink)
3837 btrfs_orphan_del(NULL, inode);
3789 } 3838 }
3790 3839
3791 return ret; 3840 return ret;
@@ -3805,7 +3854,7 @@ static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3805 return err; 3854 return err;
3806 3855
3807 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { 3856 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
3808 err = btrfs_setsize(inode, attr->ia_size); 3857 err = btrfs_setsize(inode, attr);
3809 if (err) 3858 if (err)
3810 return err; 3859 return err;
3811 } 3860 }
@@ -5586,10 +5635,13 @@ struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *pag
5586 return em; 5635 return em;
5587 if (em) { 5636 if (em) {
5588 /* 5637 /*
5589 * if our em maps to a hole, there might 5638 * if our em maps to
5590 * actually be delalloc bytes behind it 5639 * - a hole or
5640 * - a pre-alloc extent,
5641 * there might actually be delalloc bytes behind it.
5591 */ 5642 */
5592 if (em->block_start != EXTENT_MAP_HOLE) 5643 if (em->block_start != EXTENT_MAP_HOLE &&
5644 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5593 return em; 5645 return em;
5594 else 5646 else
5595 hole_em = em; 5647 hole_em = em;
@@ -5671,6 +5723,8 @@ struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *pag
5671 */ 5723 */
5672 em->block_start = hole_em->block_start; 5724 em->block_start = hole_em->block_start;
5673 em->block_len = hole_len; 5725 em->block_len = hole_len;
5726 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
5727 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
5674 } else { 5728 } else {
5675 em->start = range_start; 5729 em->start = range_start;
5676 em->len = found; 5730 em->len = found;
@@ -6929,11 +6983,9 @@ static int btrfs_truncate(struct inode *inode)
6929 6983
6930 /* 6984 /*
6931 * 1 for the truncate slack space 6985 * 1 for the truncate slack space
6932 * 1 for the orphan item we're going to add
6933 * 1 for the orphan item deletion
6934 * 1 for updating the inode. 6986 * 1 for updating the inode.
6935 */ 6987 */
6936 trans = btrfs_start_transaction(root, 4); 6988 trans = btrfs_start_transaction(root, 2);
6937 if (IS_ERR(trans)) { 6989 if (IS_ERR(trans)) {
6938 err = PTR_ERR(trans); 6990 err = PTR_ERR(trans);
6939 goto out; 6991 goto out;
@@ -6944,12 +6996,6 @@ static int btrfs_truncate(struct inode *inode)
6944 min_size); 6996 min_size);
6945 BUG_ON(ret); 6997 BUG_ON(ret);
6946 6998
6947 ret = btrfs_orphan_add(trans, inode);
6948 if (ret) {
6949 btrfs_end_transaction(trans, root);
6950 goto out;
6951 }
6952
6953 /* 6999 /*
6954 * setattr is responsible for setting the ordered_data_close flag, 7000 * setattr is responsible for setting the ordered_data_close flag,
6955 * but that is only tested during the last file release. That 7001 * but that is only tested during the last file release. That
@@ -7018,12 +7064,6 @@ static int btrfs_truncate(struct inode *inode)
7018 ret = btrfs_orphan_del(trans, inode); 7064 ret = btrfs_orphan_del(trans, inode);
7019 if (ret) 7065 if (ret)
7020 err = ret; 7066 err = ret;
7021 } else if (ret && inode->i_nlink > 0) {
7022 /*
7023 * Failed to do the truncate, remove us from the in memory
7024 * orphan list.
7025 */
7026 ret = btrfs_orphan_del(NULL, inode);
7027 } 7067 }
7028 7068
7029 if (trans) { 7069 if (trans) {
@@ -7545,41 +7585,61 @@ void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
7545 */ 7585 */
7546int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) 7586int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
7547{ 7587{
7548 struct list_head *head = &root->fs_info->delalloc_inodes;
7549 struct btrfs_inode *binode; 7588 struct btrfs_inode *binode;
7550 struct inode *inode; 7589 struct inode *inode;
7551 struct btrfs_delalloc_work *work, *next; 7590 struct btrfs_delalloc_work *work, *next;
7552 struct list_head works; 7591 struct list_head works;
7592 struct list_head splice;
7553 int ret = 0; 7593 int ret = 0;
7554 7594
7555 if (root->fs_info->sb->s_flags & MS_RDONLY) 7595 if (root->fs_info->sb->s_flags & MS_RDONLY)
7556 return -EROFS; 7596 return -EROFS;
7557 7597
7558 INIT_LIST_HEAD(&works); 7598 INIT_LIST_HEAD(&works);
7559 7599 INIT_LIST_HEAD(&splice);
7600again:
7560 spin_lock(&root->fs_info->delalloc_lock); 7601 spin_lock(&root->fs_info->delalloc_lock);
7561 while (!list_empty(head)) { 7602 list_splice_init(&root->fs_info->delalloc_inodes, &splice);
7562 binode = list_entry(head->next, struct btrfs_inode, 7603 while (!list_empty(&splice)) {
7604 binode = list_entry(splice.next, struct btrfs_inode,
7563 delalloc_inodes); 7605 delalloc_inodes);
7606
7607 list_del_init(&binode->delalloc_inodes);
7608
7564 inode = igrab(&binode->vfs_inode); 7609 inode = igrab(&binode->vfs_inode);
7565 if (!inode) 7610 if (!inode)
7566 list_del_init(&binode->delalloc_inodes); 7611 continue;
7612
7613 list_add_tail(&binode->delalloc_inodes,
7614 &root->fs_info->delalloc_inodes);
7567 spin_unlock(&root->fs_info->delalloc_lock); 7615 spin_unlock(&root->fs_info->delalloc_lock);
7568 if (inode) { 7616
7569 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput); 7617 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
7570 if (!work) { 7618 if (unlikely(!work)) {
7571 ret = -ENOMEM; 7619 ret = -ENOMEM;
7572 goto out; 7620 goto out;
7573 }
7574 list_add_tail(&work->list, &works);
7575 btrfs_queue_worker(&root->fs_info->flush_workers,
7576 &work->work);
7577 } 7621 }
7622 list_add_tail(&work->list, &works);
7623 btrfs_queue_worker(&root->fs_info->flush_workers,
7624 &work->work);
7625
7578 cond_resched(); 7626 cond_resched();
7579 spin_lock(&root->fs_info->delalloc_lock); 7627 spin_lock(&root->fs_info->delalloc_lock);
7580 } 7628 }
7581 spin_unlock(&root->fs_info->delalloc_lock); 7629 spin_unlock(&root->fs_info->delalloc_lock);
7582 7630
7631 list_for_each_entry_safe(work, next, &works, list) {
7632 list_del_init(&work->list);
7633 btrfs_wait_and_free_delalloc_work(work);
7634 }
7635
7636 spin_lock(&root->fs_info->delalloc_lock);
7637 if (!list_empty(&root->fs_info->delalloc_inodes)) {
7638 spin_unlock(&root->fs_info->delalloc_lock);
7639 goto again;
7640 }
7641 spin_unlock(&root->fs_info->delalloc_lock);
7642
7583 /* the filemap_flush will queue IO into the worker threads, but 7643 /* the filemap_flush will queue IO into the worker threads, but
7584 * we have to make sure the IO is actually started and that 7644 * we have to make sure the IO is actually started and that
7585 * ordered extents get created before we return 7645 * ordered extents get created before we return
@@ -7592,11 +7652,18 @@ int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
7592 atomic_read(&root->fs_info->async_delalloc_pages) == 0)); 7652 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
7593 } 7653 }
7594 atomic_dec(&root->fs_info->async_submit_draining); 7654 atomic_dec(&root->fs_info->async_submit_draining);
7655 return 0;
7595out: 7656out:
7596 list_for_each_entry_safe(work, next, &works, list) { 7657 list_for_each_entry_safe(work, next, &works, list) {
7597 list_del_init(&work->list); 7658 list_del_init(&work->list);
7598 btrfs_wait_and_free_delalloc_work(work); 7659 btrfs_wait_and_free_delalloc_work(work);
7599 } 7660 }
7661
7662 if (!list_empty_careful(&splice)) {
7663 spin_lock(&root->fs_info->delalloc_lock);
7664 list_splice_tail(&splice, &root->fs_info->delalloc_inodes);
7665 spin_unlock(&root->fs_info->delalloc_lock);
7666 }
7600 return ret; 7667 return ret;
7601} 7668}
7602 7669
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index 7624212ae926..afbf3ac2079d 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -1339,7 +1339,8 @@ static noinline int btrfs_ioctl_resize(struct file *file,
1339 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 1339 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
1340 1)) { 1340 1)) {
1341 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); 1341 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
1342 return -EINPROGRESS; 1342 mnt_drop_write_file(file);
1343 return -EINVAL;
1343 } 1344 }
1344 1345
1345 mutex_lock(&root->fs_info->volume_mutex); 1346 mutex_lock(&root->fs_info->volume_mutex);
@@ -1362,6 +1363,7 @@ static noinline int btrfs_ioctl_resize(struct file *file,
1362 printk(KERN_INFO "btrfs: resizing devid %llu\n", 1363 printk(KERN_INFO "btrfs: resizing devid %llu\n",
1363 (unsigned long long)devid); 1364 (unsigned long long)devid);
1364 } 1365 }
1366
1365 device = btrfs_find_device(root->fs_info, devid, NULL, NULL); 1367 device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
1366 if (!device) { 1368 if (!device) {
1367 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n", 1369 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
@@ -1369,9 +1371,10 @@ static noinline int btrfs_ioctl_resize(struct file *file,
1369 ret = -EINVAL; 1371 ret = -EINVAL;
1370 goto out_free; 1372 goto out_free;
1371 } 1373 }
1372 if (device->fs_devices && device->fs_devices->seeding) { 1374
1375 if (!device->writeable) {
1373 printk(KERN_INFO "btrfs: resizer unable to apply on " 1376 printk(KERN_INFO "btrfs: resizer unable to apply on "
1374 "seeding device %llu\n", 1377 "readonly device %llu\n",
1375 (unsigned long long)devid); 1378 (unsigned long long)devid);
1376 ret = -EINVAL; 1379 ret = -EINVAL;
1377 goto out_free; 1380 goto out_free;
@@ -1443,8 +1446,8 @@ out_free:
1443 kfree(vol_args); 1446 kfree(vol_args);
1444out: 1447out:
1445 mutex_unlock(&root->fs_info->volume_mutex); 1448 mutex_unlock(&root->fs_info->volume_mutex);
1446 mnt_drop_write_file(file);
1447 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); 1449 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
1450 mnt_drop_write_file(file);
1448 return ret; 1451 return ret;
1449} 1452}
1450 1453
@@ -2095,13 +2098,13 @@ static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2095 err = inode_permission(inode, MAY_WRITE | MAY_EXEC); 2098 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2096 if (err) 2099 if (err)
2097 goto out_dput; 2100 goto out_dput;
2098
2099 /* check if subvolume may be deleted by a non-root user */
2100 err = btrfs_may_delete(dir, dentry, 1);
2101 if (err)
2102 goto out_dput;
2103 } 2101 }
2104 2102
2103 /* check if subvolume may be deleted by a user */
2104 err = btrfs_may_delete(dir, dentry, 1);
2105 if (err)
2106 goto out_dput;
2107
2105 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { 2108 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
2106 err = -EINVAL; 2109 err = -EINVAL;
2107 goto out_dput; 2110 goto out_dput;
@@ -2183,19 +2186,20 @@ static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2183 struct btrfs_ioctl_defrag_range_args *range; 2186 struct btrfs_ioctl_defrag_range_args *range;
2184 int ret; 2187 int ret;
2185 2188
2186 if (btrfs_root_readonly(root)) 2189 ret = mnt_want_write_file(file);
2187 return -EROFS; 2190 if (ret)
2191 return ret;
2188 2192
2189 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 2193 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2190 1)) { 2194 1)) {
2191 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); 2195 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
2192 return -EINPROGRESS; 2196 mnt_drop_write_file(file);
2197 return -EINVAL;
2193 } 2198 }
2194 ret = mnt_want_write_file(file); 2199
2195 if (ret) { 2200 if (btrfs_root_readonly(root)) {
2196 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 2201 ret = -EROFS;
2197 0); 2202 goto out;
2198 return ret;
2199 } 2203 }
2200 2204
2201 switch (inode->i_mode & S_IFMT) { 2205 switch (inode->i_mode & S_IFMT) {
@@ -2247,8 +2251,8 @@ static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2247 ret = -EINVAL; 2251 ret = -EINVAL;
2248 } 2252 }
2249out: 2253out:
2250 mnt_drop_write_file(file);
2251 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); 2254 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2255 mnt_drop_write_file(file);
2252 return ret; 2256 return ret;
2253} 2257}
2254 2258
@@ -2263,7 +2267,7 @@ static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
2263 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 2267 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2264 1)) { 2268 1)) {
2265 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); 2269 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
2266 return -EINPROGRESS; 2270 return -EINVAL;
2267 } 2271 }
2268 2272
2269 mutex_lock(&root->fs_info->volume_mutex); 2273 mutex_lock(&root->fs_info->volume_mutex);
@@ -2300,7 +2304,7 @@ static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2300 1)) { 2304 1)) {
2301 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); 2305 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
2302 mnt_drop_write_file(file); 2306 mnt_drop_write_file(file);
2303 return -EINPROGRESS; 2307 return -EINVAL;
2304 } 2308 }
2305 2309
2306 mutex_lock(&root->fs_info->volume_mutex); 2310 mutex_lock(&root->fs_info->volume_mutex);
@@ -2316,8 +2320,8 @@ static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2316 kfree(vol_args); 2320 kfree(vol_args);
2317out: 2321out:
2318 mutex_unlock(&root->fs_info->volume_mutex); 2322 mutex_unlock(&root->fs_info->volume_mutex);
2319 mnt_drop_write_file(file);
2320 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); 2323 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
2324 mnt_drop_write_file(file);
2321 return ret; 2325 return ret;
2322} 2326}
2323 2327
@@ -3437,8 +3441,8 @@ static long btrfs_ioctl_balance(struct file *file, void __user *arg)
3437 struct btrfs_fs_info *fs_info = root->fs_info; 3441 struct btrfs_fs_info *fs_info = root->fs_info;
3438 struct btrfs_ioctl_balance_args *bargs; 3442 struct btrfs_ioctl_balance_args *bargs;
3439 struct btrfs_balance_control *bctl; 3443 struct btrfs_balance_control *bctl;
3444 bool need_unlock; /* for mut. excl. ops lock */
3440 int ret; 3445 int ret;
3441 int need_to_clear_lock = 0;
3442 3446
3443 if (!capable(CAP_SYS_ADMIN)) 3447 if (!capable(CAP_SYS_ADMIN))
3444 return -EPERM; 3448 return -EPERM;
@@ -3447,14 +3451,61 @@ static long btrfs_ioctl_balance(struct file *file, void __user *arg)
3447 if (ret) 3451 if (ret)
3448 return ret; 3452 return ret;
3449 3453
3450 mutex_lock(&fs_info->volume_mutex); 3454again:
3455 if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
3456 mutex_lock(&fs_info->volume_mutex);
3457 mutex_lock(&fs_info->balance_mutex);
3458 need_unlock = true;
3459 goto locked;
3460 }
3461
3462 /*
3463 * mut. excl. ops lock is locked. Three possibilites:
3464 * (1) some other op is running
3465 * (2) balance is running
3466 * (3) balance is paused -- special case (think resume)
3467 */
3451 mutex_lock(&fs_info->balance_mutex); 3468 mutex_lock(&fs_info->balance_mutex);
3469 if (fs_info->balance_ctl) {
3470 /* this is either (2) or (3) */
3471 if (!atomic_read(&fs_info->balance_running)) {
3472 mutex_unlock(&fs_info->balance_mutex);
3473 if (!mutex_trylock(&fs_info->volume_mutex))
3474 goto again;
3475 mutex_lock(&fs_info->balance_mutex);
3476
3477 if (fs_info->balance_ctl &&
3478 !atomic_read(&fs_info->balance_running)) {
3479 /* this is (3) */
3480 need_unlock = false;
3481 goto locked;
3482 }
3483
3484 mutex_unlock(&fs_info->balance_mutex);
3485 mutex_unlock(&fs_info->volume_mutex);
3486 goto again;
3487 } else {
3488 /* this is (2) */
3489 mutex_unlock(&fs_info->balance_mutex);
3490 ret = -EINPROGRESS;
3491 goto out;
3492 }
3493 } else {
3494 /* this is (1) */
3495 mutex_unlock(&fs_info->balance_mutex);
3496 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
3497 ret = -EINVAL;
3498 goto out;
3499 }
3500
3501locked:
3502 BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
3452 3503
3453 if (arg) { 3504 if (arg) {
3454 bargs = memdup_user(arg, sizeof(*bargs)); 3505 bargs = memdup_user(arg, sizeof(*bargs));
3455 if (IS_ERR(bargs)) { 3506 if (IS_ERR(bargs)) {
3456 ret = PTR_ERR(bargs); 3507 ret = PTR_ERR(bargs);
3457 goto out; 3508 goto out_unlock;
3458 } 3509 }
3459 3510
3460 if (bargs->flags & BTRFS_BALANCE_RESUME) { 3511 if (bargs->flags & BTRFS_BALANCE_RESUME) {
@@ -3474,13 +3525,10 @@ static long btrfs_ioctl_balance(struct file *file, void __user *arg)
3474 bargs = NULL; 3525 bargs = NULL;
3475 } 3526 }
3476 3527
3477 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 3528 if (fs_info->balance_ctl) {
3478 1)) {
3479 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
3480 ret = -EINPROGRESS; 3529 ret = -EINPROGRESS;
3481 goto out_bargs; 3530 goto out_bargs;
3482 } 3531 }
3483 need_to_clear_lock = 1;
3484 3532
3485 bctl = kzalloc(sizeof(*bctl), GFP_NOFS); 3533 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3486 if (!bctl) { 3534 if (!bctl) {
@@ -3501,11 +3549,17 @@ static long btrfs_ioctl_balance(struct file *file, void __user *arg)
3501 } 3549 }
3502 3550
3503do_balance: 3551do_balance:
3504 ret = btrfs_balance(bctl, bargs);
3505 /* 3552 /*
3506 * bctl is freed in __cancel_balance or in free_fs_info if 3553 * Ownership of bctl and mutually_exclusive_operation_running
3507 * restriper was paused all the way until unmount 3554 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
3555 * or, if restriper was paused all the way until unmount, in
3556 * free_fs_info. mutually_exclusive_operation_running is
3557 * cleared in __cancel_balance.
3508 */ 3558 */
3559 need_unlock = false;
3560
3561 ret = btrfs_balance(bctl, bargs);
3562
3509 if (arg) { 3563 if (arg) {
3510 if (copy_to_user(arg, bargs, sizeof(*bargs))) 3564 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3511 ret = -EFAULT; 3565 ret = -EFAULT;
@@ -3513,12 +3567,12 @@ do_balance:
3513 3567
3514out_bargs: 3568out_bargs:
3515 kfree(bargs); 3569 kfree(bargs);
3516out: 3570out_unlock:
3517 if (need_to_clear_lock)
3518 atomic_set(&root->fs_info->mutually_exclusive_operation_running,
3519 0);
3520 mutex_unlock(&fs_info->balance_mutex); 3571 mutex_unlock(&fs_info->balance_mutex);
3521 mutex_unlock(&fs_info->volume_mutex); 3572 mutex_unlock(&fs_info->volume_mutex);
3573 if (need_unlock)
3574 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3575out:
3522 mnt_drop_write_file(file); 3576 mnt_drop_write_file(file);
3523 return ret; 3577 return ret;
3524} 3578}
@@ -3698,6 +3752,11 @@ static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
3698 goto drop_write; 3752 goto drop_write;
3699 } 3753 }
3700 3754
3755 if (!sa->qgroupid) {
3756 ret = -EINVAL;
3757 goto out;
3758 }
3759
3701 trans = btrfs_join_transaction(root); 3760 trans = btrfs_join_transaction(root);
3702 if (IS_ERR(trans)) { 3761 if (IS_ERR(trans)) {
3703 ret = PTR_ERR(trans); 3762 ret = PTR_ERR(trans);
diff --git a/fs/btrfs/ordered-data.c b/fs/btrfs/ordered-data.c
index f10731297040..e5ed56729607 100644
--- a/fs/btrfs/ordered-data.c
+++ b/fs/btrfs/ordered-data.c
@@ -836,9 +836,16 @@ int btrfs_ordered_update_i_size(struct inode *inode, u64 offset,
836 * if the disk i_size is already at the inode->i_size, or 836 * if the disk i_size is already at the inode->i_size, or
837 * this ordered extent is inside the disk i_size, we're done 837 * this ordered extent is inside the disk i_size, we're done
838 */ 838 */
839 if (disk_i_size == i_size || offset <= disk_i_size) { 839 if (disk_i_size == i_size)
840 goto out;
841
842 /*
843 * We still need to update disk_i_size if outstanding_isize is greater
844 * than disk_i_size.
845 */
846 if (offset <= disk_i_size &&
847 (!ordered || ordered->outstanding_isize <= disk_i_size))
840 goto out; 848 goto out;
841 }
842 849
843 /* 850 /*
844 * walk backward from this ordered extent to disk_i_size. 851 * walk backward from this ordered extent to disk_i_size.
@@ -870,7 +877,7 @@ int btrfs_ordered_update_i_size(struct inode *inode, u64 offset,
870 break; 877 break;
871 if (test->file_offset >= i_size) 878 if (test->file_offset >= i_size)
872 break; 879 break;
873 if (test->file_offset >= disk_i_size) { 880 if (entry_end(test) > disk_i_size) {
874 /* 881 /*
875 * we don't update disk_i_size now, so record this 882 * we don't update disk_i_size now, so record this
876 * undealt i_size. Or we will not know the real 883 * undealt i_size. Or we will not know the real
diff --git a/fs/btrfs/qgroup.c b/fs/btrfs/qgroup.c
index fe9d02c45f8e..a5c856234323 100644
--- a/fs/btrfs/qgroup.c
+++ b/fs/btrfs/qgroup.c
@@ -379,6 +379,13 @@ next1:
379 379
380 ret = add_relation_rb(fs_info, found_key.objectid, 380 ret = add_relation_rb(fs_info, found_key.objectid,
381 found_key.offset); 381 found_key.offset);
382 if (ret == -ENOENT) {
383 printk(KERN_WARNING
384 "btrfs: orphan qgroup relation 0x%llx->0x%llx\n",
385 (unsigned long long)found_key.objectid,
386 (unsigned long long)found_key.offset);
387 ret = 0; /* ignore the error */
388 }
382 if (ret) 389 if (ret)
383 goto out; 390 goto out;
384next2: 391next2:
@@ -956,17 +963,28 @@ int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
956 struct btrfs_fs_info *fs_info, u64 qgroupid) 963 struct btrfs_fs_info *fs_info, u64 qgroupid)
957{ 964{
958 struct btrfs_root *quota_root; 965 struct btrfs_root *quota_root;
966 struct btrfs_qgroup *qgroup;
959 int ret = 0; 967 int ret = 0;
960 968
961 quota_root = fs_info->quota_root; 969 quota_root = fs_info->quota_root;
962 if (!quota_root) 970 if (!quota_root)
963 return -EINVAL; 971 return -EINVAL;
964 972
973 /* check if there are no relations to this qgroup */
974 spin_lock(&fs_info->qgroup_lock);
975 qgroup = find_qgroup_rb(fs_info, qgroupid);
976 if (qgroup) {
977 if (!list_empty(&qgroup->groups) || !list_empty(&qgroup->members)) {
978 spin_unlock(&fs_info->qgroup_lock);
979 return -EBUSY;
980 }
981 }
982 spin_unlock(&fs_info->qgroup_lock);
983
965 ret = del_qgroup_item(trans, quota_root, qgroupid); 984 ret = del_qgroup_item(trans, quota_root, qgroupid);
966 985
967 spin_lock(&fs_info->qgroup_lock); 986 spin_lock(&fs_info->qgroup_lock);
968 del_qgroup_rb(quota_root->fs_info, qgroupid); 987 del_qgroup_rb(quota_root->fs_info, qgroupid);
969
970 spin_unlock(&fs_info->qgroup_lock); 988 spin_unlock(&fs_info->qgroup_lock);
971 989
972 return ret; 990 return ret;
diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
index bdbb94f245c9..67783e03d121 100644
--- a/fs/btrfs/scrub.c
+++ b/fs/btrfs/scrub.c
@@ -580,20 +580,29 @@ static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *fixup_ctx)
580 int corrected = 0; 580 int corrected = 0;
581 struct btrfs_key key; 581 struct btrfs_key key;
582 struct inode *inode = NULL; 582 struct inode *inode = NULL;
583 struct btrfs_fs_info *fs_info;
583 u64 end = offset + PAGE_SIZE - 1; 584 u64 end = offset + PAGE_SIZE - 1;
584 struct btrfs_root *local_root; 585 struct btrfs_root *local_root;
586 int srcu_index;
585 587
586 key.objectid = root; 588 key.objectid = root;
587 key.type = BTRFS_ROOT_ITEM_KEY; 589 key.type = BTRFS_ROOT_ITEM_KEY;
588 key.offset = (u64)-1; 590 key.offset = (u64)-1;
589 local_root = btrfs_read_fs_root_no_name(fixup->root->fs_info, &key); 591
590 if (IS_ERR(local_root)) 592 fs_info = fixup->root->fs_info;
593 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
594
595 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
596 if (IS_ERR(local_root)) {
597 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
591 return PTR_ERR(local_root); 598 return PTR_ERR(local_root);
599 }
592 600
593 key.type = BTRFS_INODE_ITEM_KEY; 601 key.type = BTRFS_INODE_ITEM_KEY;
594 key.objectid = inum; 602 key.objectid = inum;
595 key.offset = 0; 603 key.offset = 0;
596 inode = btrfs_iget(fixup->root->fs_info->sb, &key, local_root, NULL); 604 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
605 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
597 if (IS_ERR(inode)) 606 if (IS_ERR(inode))
598 return PTR_ERR(inode); 607 return PTR_ERR(inode);
599 608
@@ -606,7 +615,6 @@ static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *fixup_ctx)
606 } 615 }
607 616
608 if (PageUptodate(page)) { 617 if (PageUptodate(page)) {
609 struct btrfs_fs_info *fs_info;
610 if (PageDirty(page)) { 618 if (PageDirty(page)) {
611 /* 619 /*
612 * we need to write the data to the defect sector. the 620 * we need to write the data to the defect sector. the
@@ -3180,18 +3188,25 @@ static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root, void *ctx)
3180 u64 physical_for_dev_replace; 3188 u64 physical_for_dev_replace;
3181 u64 len; 3189 u64 len;
3182 struct btrfs_fs_info *fs_info = nocow_ctx->sctx->dev_root->fs_info; 3190 struct btrfs_fs_info *fs_info = nocow_ctx->sctx->dev_root->fs_info;
3191 int srcu_index;
3183 3192
3184 key.objectid = root; 3193 key.objectid = root;
3185 key.type = BTRFS_ROOT_ITEM_KEY; 3194 key.type = BTRFS_ROOT_ITEM_KEY;
3186 key.offset = (u64)-1; 3195 key.offset = (u64)-1;
3196
3197 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
3198
3187 local_root = btrfs_read_fs_root_no_name(fs_info, &key); 3199 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
3188 if (IS_ERR(local_root)) 3200 if (IS_ERR(local_root)) {
3201 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
3189 return PTR_ERR(local_root); 3202 return PTR_ERR(local_root);
3203 }
3190 3204
3191 key.type = BTRFS_INODE_ITEM_KEY; 3205 key.type = BTRFS_INODE_ITEM_KEY;
3192 key.objectid = inum; 3206 key.objectid = inum;
3193 key.offset = 0; 3207 key.offset = 0;
3194 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL); 3208 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
3209 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
3195 if (IS_ERR(inode)) 3210 if (IS_ERR(inode))
3196 return PTR_ERR(inode); 3211 return PTR_ERR(inode);
3197 3212
diff --git a/fs/btrfs/send.c b/fs/btrfs/send.c
index 54454542ad40..321b7fb4e441 100644
--- a/fs/btrfs/send.c
+++ b/fs/btrfs/send.c
@@ -1814,8 +1814,10 @@ static int name_cache_insert(struct send_ctx *sctx,
1814 (unsigned long)nce->ino); 1814 (unsigned long)nce->ino);
1815 if (!nce_head) { 1815 if (!nce_head) {
1816 nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS); 1816 nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS);
1817 if (!nce_head) 1817 if (!nce_head) {
1818 kfree(nce);
1818 return -ENOMEM; 1819 return -ENOMEM;
1820 }
1819 INIT_LIST_HEAD(nce_head); 1821 INIT_LIST_HEAD(nce_head);
1820 1822
1821 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head); 1823 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index 99545df1b86c..d8982e9601d3 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -267,7 +267,7 @@ void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
267 function, line, errstr); 267 function, line, errstr);
268 return; 268 return;
269 } 269 }
270 trans->transaction->aborted = errno; 270 ACCESS_ONCE(trans->transaction->aborted) = errno;
271 __btrfs_std_error(root->fs_info, function, line, errno, NULL); 271 __btrfs_std_error(root->fs_info, function, line, errno, NULL);
272} 272}
273/* 273/*
diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c
index 87fac9a21ea5..fc03aa60b684 100644
--- a/fs/btrfs/transaction.c
+++ b/fs/btrfs/transaction.c
@@ -333,12 +333,14 @@ start_transaction(struct btrfs_root *root, u64 num_items, int type,
333 &root->fs_info->trans_block_rsv, 333 &root->fs_info->trans_block_rsv,
334 num_bytes, flush); 334 num_bytes, flush);
335 if (ret) 335 if (ret)
336 return ERR_PTR(ret); 336 goto reserve_fail;
337 } 337 }
338again: 338again:
339 h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); 339 h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
340 if (!h) 340 if (!h) {
341 return ERR_PTR(-ENOMEM); 341 ret = -ENOMEM;
342 goto alloc_fail;
343 }
342 344
343 /* 345 /*
344 * If we are JOIN_NOLOCK we're already committing a transaction and 346 * If we are JOIN_NOLOCK we're already committing a transaction and
@@ -365,11 +367,7 @@ again:
365 if (ret < 0) { 367 if (ret < 0) {
366 /* We must get the transaction if we are JOIN_NOLOCK. */ 368 /* We must get the transaction if we are JOIN_NOLOCK. */
367 BUG_ON(type == TRANS_JOIN_NOLOCK); 369 BUG_ON(type == TRANS_JOIN_NOLOCK);
368 370 goto join_fail;
369 if (type < TRANS_JOIN_NOLOCK)
370 sb_end_intwrite(root->fs_info->sb);
371 kmem_cache_free(btrfs_trans_handle_cachep, h);
372 return ERR_PTR(ret);
373 } 371 }
374 372
375 cur_trans = root->fs_info->running_transaction; 373 cur_trans = root->fs_info->running_transaction;
@@ -410,6 +408,19 @@ got_it:
410 if (!current->journal_info && type != TRANS_USERSPACE) 408 if (!current->journal_info && type != TRANS_USERSPACE)
411 current->journal_info = h; 409 current->journal_info = h;
412 return h; 410 return h;
411
412join_fail:
413 if (type < TRANS_JOIN_NOLOCK)
414 sb_end_intwrite(root->fs_info->sb);
415 kmem_cache_free(btrfs_trans_handle_cachep, h);
416alloc_fail:
417 if (num_bytes)
418 btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv,
419 num_bytes);
420reserve_fail:
421 if (qgroup_reserved)
422 btrfs_qgroup_free(root, qgroup_reserved);
423 return ERR_PTR(ret);
413} 424}
414 425
415struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 426struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
@@ -1468,7 +1479,8 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1468 goto cleanup_transaction; 1479 goto cleanup_transaction;
1469 } 1480 }
1470 1481
1471 if (cur_trans->aborted) { 1482 /* Stop the commit early if ->aborted is set */
1483 if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
1472 ret = cur_trans->aborted; 1484 ret = cur_trans->aborted;
1473 goto cleanup_transaction; 1485 goto cleanup_transaction;
1474 } 1486 }
@@ -1574,6 +1586,11 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1574 wait_event(cur_trans->writer_wait, 1586 wait_event(cur_trans->writer_wait,
1575 atomic_read(&cur_trans->num_writers) == 1); 1587 atomic_read(&cur_trans->num_writers) == 1);
1576 1588
1589 /* ->aborted might be set after the previous check, so check it */
1590 if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
1591 ret = cur_trans->aborted;
1592 goto cleanup_transaction;
1593 }
1577 /* 1594 /*
1578 * the reloc mutex makes sure that we stop 1595 * the reloc mutex makes sure that we stop
1579 * the balancing code from coming in and moving 1596 * the balancing code from coming in and moving
@@ -1657,6 +1674,17 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1657 goto cleanup_transaction; 1674 goto cleanup_transaction;
1658 } 1675 }
1659 1676
1677 /*
1678 * The tasks which save the space cache and inode cache may also
1679 * update ->aborted, check it.
1680 */
1681 if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
1682 ret = cur_trans->aborted;
1683 mutex_unlock(&root->fs_info->tree_log_mutex);
1684 mutex_unlock(&root->fs_info->reloc_mutex);
1685 goto cleanup_transaction;
1686 }
1687
1660 btrfs_prepare_extent_commit(trans, root); 1688 btrfs_prepare_extent_commit(trans, root);
1661 1689
1662 cur_trans = root->fs_info->running_transaction; 1690 cur_trans = root->fs_info->running_transaction;
diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
index 83186c7e45d4..9027bb1e7466 100644
--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -3357,6 +3357,11 @@ static int log_one_extent(struct btrfs_trans_handle *trans,
3357 if (skip_csum) 3357 if (skip_csum)
3358 return 0; 3358 return 0;
3359 3359
3360 if (em->compress_type) {
3361 csum_offset = 0;
3362 csum_len = block_len;
3363 }
3364
3360 /* block start is already adjusted for the file extent offset. */ 3365 /* block start is already adjusted for the file extent offset. */
3361 ret = btrfs_lookup_csums_range(log->fs_info->csum_root, 3366 ret = btrfs_lookup_csums_range(log->fs_info->csum_root,
3362 em->block_start + csum_offset, 3367 em->block_start + csum_offset,
@@ -3410,13 +3415,13 @@ static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
3410 em = list_entry(extents.next, struct extent_map, list); 3415 em = list_entry(extents.next, struct extent_map, list);
3411 3416
3412 list_del_init(&em->list); 3417 list_del_init(&em->list);
3413 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
3414 3418
3415 /* 3419 /*
3416 * If we had an error we just need to delete everybody from our 3420 * If we had an error we just need to delete everybody from our
3417 * private list. 3421 * private list.
3418 */ 3422 */
3419 if (ret) { 3423 if (ret) {
3424 clear_em_logging(tree, em);
3420 free_extent_map(em); 3425 free_extent_map(em);
3421 continue; 3426 continue;
3422 } 3427 }
@@ -3424,8 +3429,9 @@ static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
3424 write_unlock(&tree->lock); 3429 write_unlock(&tree->lock);
3425 3430
3426 ret = log_one_extent(trans, inode, root, em, path); 3431 ret = log_one_extent(trans, inode, root, em, path);
3427 free_extent_map(em);
3428 write_lock(&tree->lock); 3432 write_lock(&tree->lock);
3433 clear_em_logging(tree, em);
3434 free_extent_map(em);
3429 } 3435 }
3430 WARN_ON(!list_empty(&extents)); 3436 WARN_ON(!list_empty(&extents));
3431 write_unlock(&tree->lock); 3437 write_unlock(&tree->lock);
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 485a5423e3c6..5cbb7f4b1672 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -1431,7 +1431,7 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1431 } 1431 }
1432 } else { 1432 } else {
1433 ret = btrfs_get_bdev_and_sb(device_path, 1433 ret = btrfs_get_bdev_and_sb(device_path,
1434 FMODE_READ | FMODE_EXCL, 1434 FMODE_WRITE | FMODE_EXCL,
1435 root->fs_info->bdev_holder, 0, 1435 root->fs_info->bdev_holder, 0,
1436 &bdev, &bh); 1436 &bdev, &bh);
1437 if (ret) 1437 if (ret)
@@ -2615,7 +2615,14 @@ static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2615 cache = btrfs_lookup_block_group(fs_info, chunk_offset); 2615 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2616 chunk_used = btrfs_block_group_used(&cache->item); 2616 chunk_used = btrfs_block_group_used(&cache->item);
2617 2617
2618 user_thresh = div_factor_fine(cache->key.offset, bargs->usage); 2618 if (bargs->usage == 0)
2619 user_thresh = 0;
2620 else if (bargs->usage > 100)
2621 user_thresh = cache->key.offset;
2622 else
2623 user_thresh = div_factor_fine(cache->key.offset,
2624 bargs->usage);
2625
2619 if (chunk_used < user_thresh) 2626 if (chunk_used < user_thresh)
2620 ret = 0; 2627 ret = 0;
2621 2628
@@ -2960,6 +2967,8 @@ static void __cancel_balance(struct btrfs_fs_info *fs_info)
2960 unset_balance_control(fs_info); 2967 unset_balance_control(fs_info);
2961 ret = del_balance_item(fs_info->tree_root); 2968 ret = del_balance_item(fs_info->tree_root);
2962 BUG_ON(ret); 2969 BUG_ON(ret);
2970
2971 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
2963} 2972}
2964 2973
2965void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock, 2974void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
@@ -3139,8 +3148,10 @@ int btrfs_balance(struct btrfs_balance_control *bctl,
3139out: 3148out:
3140 if (bctl->flags & BTRFS_BALANCE_RESUME) 3149 if (bctl->flags & BTRFS_BALANCE_RESUME)
3141 __cancel_balance(fs_info); 3150 __cancel_balance(fs_info);
3142 else 3151 else {
3143 kfree(bctl); 3152 kfree(bctl);
3153 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3154 }
3144 return ret; 3155 return ret;
3145} 3156}
3146 3157
@@ -3157,7 +3168,6 @@ static int balance_kthread(void *data)
3157 ret = btrfs_balance(fs_info->balance_ctl, NULL); 3168 ret = btrfs_balance(fs_info->balance_ctl, NULL);
3158 } 3169 }
3159 3170
3160 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3161 mutex_unlock(&fs_info->balance_mutex); 3171 mutex_unlock(&fs_info->balance_mutex);
3162 mutex_unlock(&fs_info->volume_mutex); 3172 mutex_unlock(&fs_info->volume_mutex);
3163 3173
@@ -3180,7 +3190,6 @@ int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3180 return 0; 3190 return 0;
3181 } 3191 }
3182 3192
3183 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3184 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance"); 3193 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
3185 if (IS_ERR(tsk)) 3194 if (IS_ERR(tsk))
3186 return PTR_ERR(tsk); 3195 return PTR_ERR(tsk);
@@ -3234,6 +3243,8 @@ int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
3234 btrfs_balance_sys(leaf, item, &disk_bargs); 3243 btrfs_balance_sys(leaf, item, &disk_bargs);
3235 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs); 3244 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3236 3245
3246 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3247
3237 mutex_lock(&fs_info->volume_mutex); 3248 mutex_lock(&fs_info->volume_mutex);
3238 mutex_lock(&fs_info->balance_mutex); 3249 mutex_lock(&fs_info->balance_mutex);
3239 3250
@@ -3497,7 +3508,7 @@ struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
3497 { 1, 1, 2, 2, 2, 2 /* raid1 */ }, 3508 { 1, 1, 2, 2, 2, 2 /* raid1 */ },
3498 { 1, 2, 1, 1, 1, 2 /* dup */ }, 3509 { 1, 2, 1, 1, 1, 2 /* dup */ },
3499 { 1, 1, 0, 2, 1, 1 /* raid0 */ }, 3510 { 1, 1, 0, 2, 1, 1 /* raid0 */ },
3500 { 1, 1, 0, 1, 1, 1 /* single */ }, 3511 { 1, 1, 1, 1, 1, 1 /* single */ },
3501}; 3512};
3502 3513
3503static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans, 3514static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,