Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/linux-btrfs

Pull btrfs fixes from Chris Mason:
"It turns out that we had two crc bugs when running fsx-linux in a
loop. Many thanks to Josef, Miao Xie, and Dave Sterba for nailing it
all down. Miao also has a new OOM fix in this v2 pull as well.

Ilya fixed a regression Liu Bo found in the balance ioctls for pausing
and resuming a running balance across drives.

Josef's orphan truncate patch fixes an obscure corruption we'd see
during xfstests.

Arne's patches address problems with subvolume quotas. If the user
destroys quota groups incorrectly the FS will refuse to mount.

The rest are smaller fixes and plugs for memory leaks."

* 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/linux-btrfs: (30 commits)
Btrfs: fix repeated delalloc work allocation
Btrfs: fix wrong max device number for single profile
Btrfs: fix missed transaction->aborted check
Btrfs: Add ACCESS_ONCE() to transaction->abort accesses
Btrfs: put csums on the right ordered extent
Btrfs: use right range to find checksum for compressed extents
Btrfs: fix panic when recovering tree log
Btrfs: do not allow logged extents to be merged or removed
Btrfs: fix a regression in balance usage filter
Btrfs: prevent qgroup destroy when there are still relations
Btrfs: ignore orphan qgroup relations
Btrfs: reorder locks and sanity checks in btrfs_ioctl_defrag
Btrfs: fix unlock order in btrfs_ioctl_rm_dev
Btrfs: fix unlock order in btrfs_ioctl_resize
Btrfs: fix "mutually exclusive op is running" error code
Btrfs: bring back balance pause/resume logic
btrfs: update timestamps on truncate()
btrfs: fix btrfs_cont_expand() freeing IS_ERR em
Btrfs: fix a bug when llseek for delalloc bytes behind prealloc extents
Btrfs: fix off-by-one in lseek
...

+300 -98
+4 -2
fs/btrfs/extent-tree.c
··· 3997 3997 * We make the other tasks wait for the flush only when we can flush 3998 3998 * all things. 3999 3999 */ 4000 - if (ret && flush == BTRFS_RESERVE_FLUSH_ALL) { 4000 + if (ret && flush != BTRFS_RESERVE_NO_FLUSH) { 4001 4001 flushing = true; 4002 4002 space_info->flush = 1; 4003 4003 } ··· 5560 5560 int empty_cluster = 2 * 1024 * 1024; 5561 5561 struct btrfs_space_info *space_info; 5562 5562 int loop = 0; 5563 - int index = 0; 5563 + int index = __get_raid_index(data); 5564 5564 int alloc_type = (data & BTRFS_BLOCK_GROUP_DATA) ? 5565 5565 RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC; 5566 5566 bool found_uncached_bg = false; ··· 6788 6788 &wc->flags[level]); 6789 6789 if (ret < 0) { 6790 6790 btrfs_tree_unlock_rw(eb, path->locks[level]); 6791 + path->locks[level] = 0; 6791 6792 return ret; 6792 6793 } 6793 6794 BUG_ON(wc->refs[level] == 0); 6794 6795 if (wc->refs[level] == 1) { 6795 6796 btrfs_tree_unlock_rw(eb, path->locks[level]); 6797 + path->locks[level] = 0; 6796 6798 return 1; 6797 6799 } 6798 6800 }
+12 -1
fs/btrfs/extent_map.c
··· 171 171 if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags)) 172 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 178 if (extent_map_end(prev) == next->start && 175 179 prev->flags == next->flags && 176 180 prev->bdev == next->bdev && ··· 259 255 if (!em) 260 256 goto out; 261 257 262 - list_move(&em->list, &tree->modified_extents); 258 + if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags)) 259 + list_move(&em->list, &tree->modified_extents); 263 260 em->generation = gen; 264 261 clear_bit(EXTENT_FLAG_PINNED, &em->flags); 265 262 em->mod_start = em->start; ··· 283 278 write_unlock(&tree->lock); 284 279 return ret; 285 280 281 + } 282 + 283 + void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em) 284 + { 285 + clear_bit(EXTENT_FLAG_LOGGING, &em->flags); 286 + try_merge_map(tree, em); 286 287 } 287 288 288 289 /**
+1
fs/btrfs/extent_map.h
··· 69 69 int __init extent_map_init(void); 70 70 void extent_map_exit(void); 71 71 int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len, u64 gen); 72 + void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em); 72 73 struct extent_map *search_extent_mapping(struct extent_map_tree *tree, 73 74 u64 start, u64 len); 74 75 #endif
+2 -2
fs/btrfs/file-item.c
··· 460 460 if (!contig) 461 461 offset = page_offset(bvec->bv_page) + bvec->bv_offset; 462 462 463 - if (!contig && (offset >= ordered->file_offset + ordered->len || 464 - offset < ordered->file_offset)) { 463 + if (offset >= ordered->file_offset + ordered->len || 464 + offset < ordered->file_offset) { 465 465 unsigned long bytes_left; 466 466 sums->len = this_sum_bytes; 467 467 this_sum_bytes = 0;
+7 -3
fs/btrfs/file.c
··· 2241 2241 if (lockend <= lockstart) 2242 2242 lockend = lockstart + root->sectorsize; 2243 2243 2244 + lockend--; 2244 2245 len = lockend - lockstart + 1; 2245 2246 2246 2247 len = max_t(u64, len, root->sectorsize); ··· 2308 2307 } 2309 2308 } 2310 2309 2311 - *offset = start; 2312 - free_extent_map(em); 2313 - break; 2310 + if (!test_bit(EXTENT_FLAG_PREALLOC, 2311 + &em->flags)) { 2312 + *offset = start; 2313 + free_extent_map(em); 2314 + break; 2315 + } 2314 2316 } 2315 2317 } 2316 2318
+12 -8
fs/btrfs/free-space-cache.c
··· 1862 1862 { 1863 1863 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; 1864 1864 struct btrfs_free_space *info; 1865 - int ret = 0; 1865 + int ret; 1866 + bool re_search = false; 1866 1867 1867 1868 spin_lock(&ctl->tree_lock); 1868 1869 1869 1870 again: 1871 + ret = 0; 1870 1872 if (!bytes) 1871 1873 goto out_lock; 1872 1874 ··· 1881 1879 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), 1882 1880 1, 0); 1883 1881 if (!info) { 1884 - /* the tree logging code might be calling us before we 1885 - * have fully loaded the free space rbtree for this 1886 - * block group. So it is possible the entry won't 1887 - * be in the rbtree yet at all. The caching code 1888 - * will make sure not to put it in the rbtree if 1889 - * the logging code has pinned it. 1882 + /* 1883 + * If we found a partial bit of our free space in a 1884 + * bitmap but then couldn't find the other part this may 1885 + * be a problem, so WARN about it. 1890 1886 */ 1887 + WARN_ON(re_search); 1891 1888 goto out_lock; 1892 1889 } 1893 1890 } 1894 1891 1892 + re_search = false; 1895 1893 if (!info->bitmap) { 1896 1894 unlink_free_space(ctl, info); 1897 1895 if (offset == info->offset) { ··· 1937 1935 } 1938 1936 1939 1937 ret = remove_from_bitmap(ctl, info, &offset, &bytes); 1940 - if (ret == -EAGAIN) 1938 + if (ret == -EAGAIN) { 1939 + re_search = true; 1941 1940 goto again; 1941 + } 1942 1942 BUG_ON(ret); /* logic error */ 1943 1943 out_lock: 1944 1944 spin_unlock(&ctl->tree_lock);
+102 -35
fs/btrfs/inode.c
··· 88 88 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, 89 89 }; 90 90 91 - static int btrfs_setsize(struct inode *inode, loff_t newsize); 91 + static int btrfs_setsize(struct inode *inode, struct iattr *attr); 92 92 static int btrfs_truncate(struct inode *inode); 93 93 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); 94 94 static noinline int cow_file_range(struct inode *inode, ··· 2478 2478 continue; 2479 2479 } 2480 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 2493 ret = btrfs_truncate(inode); 2482 2494 } else { 2483 2495 nr_unlink++; ··· 3677 3665 block_end - cur_offset, 0); 3678 3666 if (IS_ERR(em)) { 3679 3667 err = PTR_ERR(em); 3668 + em = NULL; 3680 3669 break; 3681 3670 } 3682 3671 last_byte = min(extent_map_end(em), block_end); ··· 3761 3748 return err; 3762 3749 } 3763 3750 3764 - static int btrfs_setsize(struct inode *inode, loff_t newsize) 3751 + static int btrfs_setsize(struct inode *inode, struct iattr *attr) 3765 3752 { 3766 3753 struct btrfs_root *root = BTRFS_I(inode)->root; 3767 3754 struct btrfs_trans_handle *trans; 3768 3755 loff_t oldsize = i_size_read(inode); 3756 + loff_t newsize = attr->ia_size; 3757 + int mask = attr->ia_valid; 3769 3758 int ret; 3770 3759 3771 3760 if (newsize == oldsize) 3772 3761 return 0; 3762 + 3763 + /* 3764 + * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a 3765 + * special case where we need to update the times despite not having 3766 + * these flags set. For all other operations the VFS set these flags 3767 + * explicitly if it wants a timestamp update. 3768 + */ 3769 + if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME)))) 3770 + inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb); 3773 3771 3774 3772 if (newsize > oldsize) { 3775 3773 truncate_pagecache(inode, oldsize, newsize); ··· 3807 3783 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, 3808 3784 &BTRFS_I(inode)->runtime_flags); 3809 3785 3786 + /* 3787 + * 1 for the orphan item we're going to add 3788 + * 1 for the orphan item deletion. 3789 + */ 3790 + trans = btrfs_start_transaction(root, 2); 3791 + if (IS_ERR(trans)) 3792 + return PTR_ERR(trans); 3793 + 3794 + /* 3795 + * We need to do this in case we fail at _any_ point during the 3796 + * actual truncate. Once we do the truncate_setsize we could 3797 + * invalidate pages which forces any outstanding ordered io to 3798 + * be instantly completed which will give us extents that need 3799 + * to be truncated. If we fail to get an orphan inode down we 3800 + * could have left over extents that were never meant to live, 3801 + * so we need to garuntee from this point on that everything 3802 + * will be consistent. 3803 + */ 3804 + ret = btrfs_orphan_add(trans, inode); 3805 + btrfs_end_transaction(trans, root); 3806 + if (ret) 3807 + return ret; 3808 + 3810 3809 /* we don't support swapfiles, so vmtruncate shouldn't fail */ 3811 3810 truncate_setsize(inode, newsize); 3812 3811 ret = btrfs_truncate(inode); 3812 + if (ret && inode->i_nlink) 3813 + btrfs_orphan_del(NULL, inode); 3813 3814 } 3814 3815 3815 3816 return ret; ··· 3854 3805 return err; 3855 3806 3856 3807 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { 3857 - err = btrfs_setsize(inode, attr->ia_size); 3808 + err = btrfs_setsize(inode, attr); 3858 3809 if (err) 3859 3810 return err; 3860 3811 } ··· 5621 5572 return em; 5622 5573 if (em) { 5623 5574 /* 5624 - * if our em maps to a hole, there might 5625 - * actually be delalloc bytes behind it 5575 + * if our em maps to 5576 + * - a hole or 5577 + * - a pre-alloc extent, 5578 + * there might actually be delalloc bytes behind it. 5626 5579 */ 5627 - if (em->block_start != EXTENT_MAP_HOLE) 5580 + if (em->block_start != EXTENT_MAP_HOLE && 5581 + !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) 5628 5582 return em; 5629 5583 else 5630 5584 hole_em = em; ··· 5709 5657 */ 5710 5658 em->block_start = hole_em->block_start; 5711 5659 em->block_len = hole_len; 5660 + if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) 5661 + set_bit(EXTENT_FLAG_PREALLOC, &em->flags); 5712 5662 } else { 5713 5663 em->start = range_start; 5714 5664 em->len = found; ··· 6969 6915 6970 6916 /* 6971 6917 * 1 for the truncate slack space 6972 - * 1 for the orphan item we're going to add 6973 - * 1 for the orphan item deletion 6974 6918 * 1 for updating the inode. 6975 6919 */ 6976 - trans = btrfs_start_transaction(root, 4); 6920 + trans = btrfs_start_transaction(root, 2); 6977 6921 if (IS_ERR(trans)) { 6978 6922 err = PTR_ERR(trans); 6979 6923 goto out; ··· 6981 6929 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv, 6982 6930 min_size); 6983 6931 BUG_ON(ret); 6984 - 6985 - ret = btrfs_orphan_add(trans, inode); 6986 - if (ret) { 6987 - btrfs_end_transaction(trans, root); 6988 - goto out; 6989 - } 6990 6932 6991 6933 /* 6992 6934 * setattr is responsible for setting the ordered_data_close flag, ··· 7050 7004 ret = btrfs_orphan_del(trans, inode); 7051 7005 if (ret) 7052 7006 err = ret; 7053 - } else if (ret && inode->i_nlink > 0) { 7054 - /* 7055 - * Failed to do the truncate, remove us from the in memory 7056 - * orphan list. 7057 - */ 7058 - ret = btrfs_orphan_del(NULL, inode); 7059 7007 } 7060 7008 7061 7009 if (trans) { ··· 7571 7531 */ 7572 7532 int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) 7573 7533 { 7574 - struct list_head *head = &root->fs_info->delalloc_inodes; 7575 7534 struct btrfs_inode *binode; 7576 7535 struct inode *inode; 7577 7536 struct btrfs_delalloc_work *work, *next; 7578 7537 struct list_head works; 7538 + struct list_head splice; 7579 7539 int ret = 0; 7580 7540 7581 7541 if (root->fs_info->sb->s_flags & MS_RDONLY) 7582 7542 return -EROFS; 7583 7543 7584 7544 INIT_LIST_HEAD(&works); 7585 - 7545 + INIT_LIST_HEAD(&splice); 7546 + again: 7586 7547 spin_lock(&root->fs_info->delalloc_lock); 7587 - while (!list_empty(head)) { 7588 - binode = list_entry(head->next, struct btrfs_inode, 7548 + list_splice_init(&root->fs_info->delalloc_inodes, &splice); 7549 + while (!list_empty(&splice)) { 7550 + binode = list_entry(splice.next, struct btrfs_inode, 7589 7551 delalloc_inodes); 7552 + 7553 + list_del_init(&binode->delalloc_inodes); 7554 + 7590 7555 inode = igrab(&binode->vfs_inode); 7591 7556 if (!inode) 7592 - list_del_init(&binode->delalloc_inodes); 7557 + continue; 7558 + 7559 + list_add_tail(&binode->delalloc_inodes, 7560 + &root->fs_info->delalloc_inodes); 7593 7561 spin_unlock(&root->fs_info->delalloc_lock); 7594 - if (inode) { 7595 - work = btrfs_alloc_delalloc_work(inode, 0, delay_iput); 7596 - if (!work) { 7597 - ret = -ENOMEM; 7598 - goto out; 7599 - } 7600 - list_add_tail(&work->list, &works); 7601 - btrfs_queue_worker(&root->fs_info->flush_workers, 7602 - &work->work); 7562 + 7563 + work = btrfs_alloc_delalloc_work(inode, 0, delay_iput); 7564 + if (unlikely(!work)) { 7565 + ret = -ENOMEM; 7566 + goto out; 7603 7567 } 7568 + list_add_tail(&work->list, &works); 7569 + btrfs_queue_worker(&root->fs_info->flush_workers, 7570 + &work->work); 7571 + 7604 7572 cond_resched(); 7605 7573 spin_lock(&root->fs_info->delalloc_lock); 7574 + } 7575 + spin_unlock(&root->fs_info->delalloc_lock); 7576 + 7577 + list_for_each_entry_safe(work, next, &works, list) { 7578 + list_del_init(&work->list); 7579 + btrfs_wait_and_free_delalloc_work(work); 7580 + } 7581 + 7582 + spin_lock(&root->fs_info->delalloc_lock); 7583 + if (!list_empty(&root->fs_info->delalloc_inodes)) { 7584 + spin_unlock(&root->fs_info->delalloc_lock); 7585 + goto again; 7606 7586 } 7607 7587 spin_unlock(&root->fs_info->delalloc_lock); 7608 7588 ··· 7638 7578 atomic_read(&root->fs_info->async_delalloc_pages) == 0)); 7639 7579 } 7640 7580 atomic_dec(&root->fs_info->async_submit_draining); 7581 + return 0; 7641 7582 out: 7642 7583 list_for_each_entry_safe(work, next, &works, list) { 7643 7584 list_del_init(&work->list); 7644 7585 btrfs_wait_and_free_delalloc_work(work); 7586 + } 7587 + 7588 + if (!list_empty_careful(&splice)) { 7589 + spin_lock(&root->fs_info->delalloc_lock); 7590 + list_splice_tail(&splice, &root->fs_info->delalloc_inodes); 7591 + spin_unlock(&root->fs_info->delalloc_lock); 7645 7592 } 7646 7593 return ret; 7647 7594 }
+94 -35
fs/btrfs/ioctl.c
··· 1339 1339 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 1340 1340 1)) { 1341 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 1346 mutex_lock(&root->fs_info->volume_mutex); ··· 1363 1362 printk(KERN_INFO "btrfs: resizing devid %llu\n", 1364 1363 (unsigned long long)devid); 1365 1364 } 1365 + 1366 1366 device = btrfs_find_device(root->fs_info, devid, NULL, NULL); 1367 1367 if (!device) { 1368 1368 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n", ··· 1371 1369 ret = -EINVAL; 1372 1370 goto out_free; 1373 1371 } 1374 - if (device->fs_devices && device->fs_devices->seeding) { 1372 + 1373 + if (!device->writeable) { 1375 1374 printk(KERN_INFO "btrfs: resizer unable to apply on " 1376 - "seeding device %llu\n", 1375 + "readonly device %llu\n", 1377 1376 (unsigned long long)devid); 1378 1377 ret = -EINVAL; 1379 1378 goto out_free; ··· 1446 1443 kfree(vol_args); 1447 1444 out: 1448 1445 mutex_unlock(&root->fs_info->volume_mutex); 1449 - mnt_drop_write_file(file); 1450 1446 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); 1447 + mnt_drop_write_file(file); 1451 1448 return ret; 1452 1449 } 1453 1450 ··· 2098 2095 err = inode_permission(inode, MAY_WRITE | MAY_EXEC); 2099 2096 if (err) 2100 2097 goto out_dput; 2101 - 2102 - /* check if subvolume may be deleted by a non-root user */ 2103 - err = btrfs_may_delete(dir, dentry, 1); 2104 - if (err) 2105 - goto out_dput; 2106 2098 } 2099 + 2100 + /* check if subvolume may be deleted by a user */ 2101 + err = btrfs_may_delete(dir, dentry, 1); 2102 + if (err) 2103 + goto out_dput; 2107 2104 2108 2105 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { 2109 2106 err = -EINVAL; ··· 2186 2183 struct btrfs_ioctl_defrag_range_args *range; 2187 2184 int ret; 2188 2185 2189 - if (btrfs_root_readonly(root)) 2190 - return -EROFS; 2186 + ret = mnt_want_write_file(file); 2187 + if (ret) 2188 + return ret; 2191 2189 2192 2190 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 2193 2191 1)) { 2194 2192 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); 2195 - return -EINPROGRESS; 2193 + mnt_drop_write_file(file); 2194 + return -EINVAL; 2196 2195 } 2197 - ret = mnt_want_write_file(file); 2198 - if (ret) { 2199 - atomic_set(&root->fs_info->mutually_exclusive_operation_running, 2200 - 0); 2201 - return ret; 2196 + 2197 + if (btrfs_root_readonly(root)) { 2198 + ret = -EROFS; 2199 + goto out; 2202 2200 } 2203 2201 2204 2202 switch (inode->i_mode & S_IFMT) { ··· 2251 2247 ret = -EINVAL; 2252 2248 } 2253 2249 out: 2254 - mnt_drop_write_file(file); 2255 2250 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); 2251 + mnt_drop_write_file(file); 2256 2252 return ret; 2257 2253 } 2258 2254 ··· 2267 2263 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 2268 2264 1)) { 2269 2265 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); 2270 - return -EINPROGRESS; 2266 + return -EINVAL; 2271 2267 } 2272 2268 2273 2269 mutex_lock(&root->fs_info->volume_mutex); ··· 2304 2300 1)) { 2305 2301 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); 2306 2302 mnt_drop_write_file(file); 2307 - return -EINPROGRESS; 2303 + return -EINVAL; 2308 2304 } 2309 2305 2310 2306 mutex_lock(&root->fs_info->volume_mutex); ··· 2320 2316 kfree(vol_args); 2321 2317 out: 2322 2318 mutex_unlock(&root->fs_info->volume_mutex); 2323 - mnt_drop_write_file(file); 2324 2319 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); 2320 + mnt_drop_write_file(file); 2325 2321 return ret; 2326 2322 } 2327 2323 ··· 3441 3437 struct btrfs_fs_info *fs_info = root->fs_info; 3442 3438 struct btrfs_ioctl_balance_args *bargs; 3443 3439 struct btrfs_balance_control *bctl; 3440 + bool need_unlock; /* for mut. excl. ops lock */ 3444 3441 int ret; 3445 - int need_to_clear_lock = 0; 3446 3442 3447 3443 if (!capable(CAP_SYS_ADMIN)) 3448 3444 return -EPERM; ··· 3451 3447 if (ret) 3452 3448 return ret; 3453 3449 3454 - mutex_lock(&fs_info->volume_mutex); 3450 + again: 3451 + if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) { 3452 + mutex_lock(&fs_info->volume_mutex); 3453 + mutex_lock(&fs_info->balance_mutex); 3454 + need_unlock = true; 3455 + goto locked; 3456 + } 3457 + 3458 + /* 3459 + * mut. excl. ops lock is locked. Three possibilites: 3460 + * (1) some other op is running 3461 + * (2) balance is running 3462 + * (3) balance is paused -- special case (think resume) 3463 + */ 3455 3464 mutex_lock(&fs_info->balance_mutex); 3465 + if (fs_info->balance_ctl) { 3466 + /* this is either (2) or (3) */ 3467 + if (!atomic_read(&fs_info->balance_running)) { 3468 + mutex_unlock(&fs_info->balance_mutex); 3469 + if (!mutex_trylock(&fs_info->volume_mutex)) 3470 + goto again; 3471 + mutex_lock(&fs_info->balance_mutex); 3472 + 3473 + if (fs_info->balance_ctl && 3474 + !atomic_read(&fs_info->balance_running)) { 3475 + /* this is (3) */ 3476 + need_unlock = false; 3477 + goto locked; 3478 + } 3479 + 3480 + mutex_unlock(&fs_info->balance_mutex); 3481 + mutex_unlock(&fs_info->volume_mutex); 3482 + goto again; 3483 + } else { 3484 + /* this is (2) */ 3485 + mutex_unlock(&fs_info->balance_mutex); 3486 + ret = -EINPROGRESS; 3487 + goto out; 3488 + } 3489 + } else { 3490 + /* this is (1) */ 3491 + mutex_unlock(&fs_info->balance_mutex); 3492 + pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); 3493 + ret = -EINVAL; 3494 + goto out; 3495 + } 3496 + 3497 + locked: 3498 + BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running)); 3456 3499 3457 3500 if (arg) { 3458 3501 bargs = memdup_user(arg, sizeof(*bargs)); 3459 3502 if (IS_ERR(bargs)) { 3460 3503 ret = PTR_ERR(bargs); 3461 - goto out; 3504 + goto out_unlock; 3462 3505 } 3463 3506 3464 3507 if (bargs->flags & BTRFS_BALANCE_RESUME) { ··· 3525 3474 bargs = NULL; 3526 3475 } 3527 3476 3528 - if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 3529 - 1)) { 3530 - pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); 3477 + if (fs_info->balance_ctl) { 3531 3478 ret = -EINPROGRESS; 3532 3479 goto out_bargs; 3533 3480 } 3534 - need_to_clear_lock = 1; 3535 3481 3536 3482 bctl = kzalloc(sizeof(*bctl), GFP_NOFS); 3537 3483 if (!bctl) { ··· 3549 3501 } 3550 3502 3551 3503 do_balance: 3552 - ret = btrfs_balance(bctl, bargs); 3553 3504 /* 3554 - * bctl is freed in __cancel_balance or in free_fs_info if 3555 - * restriper was paused all the way until unmount 3505 + * Ownership of bctl and mutually_exclusive_operation_running 3506 + * goes to to btrfs_balance. bctl is freed in __cancel_balance, 3507 + * or, if restriper was paused all the way until unmount, in 3508 + * free_fs_info. mutually_exclusive_operation_running is 3509 + * cleared in __cancel_balance. 3556 3510 */ 3511 + need_unlock = false; 3512 + 3513 + ret = btrfs_balance(bctl, bargs); 3514 + 3557 3515 if (arg) { 3558 3516 if (copy_to_user(arg, bargs, sizeof(*bargs))) 3559 3517 ret = -EFAULT; ··· 3567 3513 3568 3514 out_bargs: 3569 3515 kfree(bargs); 3570 - out: 3571 - if (need_to_clear_lock) 3572 - atomic_set(&root->fs_info->mutually_exclusive_operation_running, 3573 - 0); 3516 + out_unlock: 3574 3517 mutex_unlock(&fs_info->balance_mutex); 3575 3518 mutex_unlock(&fs_info->volume_mutex); 3519 + if (need_unlock) 3520 + atomic_set(&fs_info->mutually_exclusive_operation_running, 0); 3521 + out: 3576 3522 mnt_drop_write_file(file); 3577 3523 return ret; 3578 3524 } ··· 3750 3696 if (IS_ERR(sa)) { 3751 3697 ret = PTR_ERR(sa); 3752 3698 goto drop_write; 3699 + } 3700 + 3701 + if (!sa->qgroupid) { 3702 + ret = -EINVAL; 3703 + goto out; 3753 3704 } 3754 3705 3755 3706 trans = btrfs_join_transaction(root);
+19 -1
fs/btrfs/qgroup.c
··· 379 379 380 380 ret = add_relation_rb(fs_info, found_key.objectid, 381 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 389 if (ret) 383 390 goto out; 384 391 next2: ··· 963 956 struct btrfs_fs_info *fs_info, u64 qgroupid) 964 957 { 965 958 struct btrfs_root *quota_root; 959 + struct btrfs_qgroup *qgroup; 966 960 int ret = 0; 967 961 968 962 quota_root = fs_info->quota_root; 969 963 if (!quota_root) 970 964 return -EINVAL; 971 965 966 + /* check if there are no relations to this qgroup */ 967 + spin_lock(&fs_info->qgroup_lock); 968 + qgroup = find_qgroup_rb(fs_info, qgroupid); 969 + if (qgroup) { 970 + if (!list_empty(&qgroup->groups) || !list_empty(&qgroup->members)) { 971 + spin_unlock(&fs_info->qgroup_lock); 972 + return -EBUSY; 973 + } 974 + } 975 + spin_unlock(&fs_info->qgroup_lock); 976 + 972 977 ret = del_qgroup_item(trans, quota_root, qgroupid); 973 978 974 979 spin_lock(&fs_info->qgroup_lock); 975 980 del_qgroup_rb(quota_root->fs_info, qgroupid); 976 - 977 981 spin_unlock(&fs_info->qgroup_lock); 978 982 979 983 return ret;
+3 -1
fs/btrfs/send.c
··· 1814 1814 (unsigned long)nce->ino); 1815 1815 if (!nce_head) { 1816 1816 nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS); 1817 - if (!nce_head) 1817 + if (!nce_head) { 1818 + kfree(nce); 1818 1819 return -ENOMEM; 1820 + } 1819 1821 INIT_LIST_HEAD(nce_head); 1820 1822 1821 1823 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
+1 -1
fs/btrfs/super.c
··· 267 267 function, line, errstr); 268 268 return; 269 269 } 270 - trans->transaction->aborted = errno; 270 + ACCESS_ONCE(trans->transaction->aborted) = errno; 271 271 __btrfs_std_error(root->fs_info, function, line, errno, NULL); 272 272 } 273 273 /*
+18 -1
fs/btrfs/transaction.c
··· 1468 1468 goto cleanup_transaction; 1469 1469 } 1470 1470 1471 - if (cur_trans->aborted) { 1471 + /* Stop the commit early if ->aborted is set */ 1472 + if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 1472 1473 ret = cur_trans->aborted; 1473 1474 goto cleanup_transaction; 1474 1475 } ··· 1575 1574 wait_event(cur_trans->writer_wait, 1576 1575 atomic_read(&cur_trans->num_writers) == 1); 1577 1576 1577 + /* ->aborted might be set after the previous check, so check it */ 1578 + if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 1579 + ret = cur_trans->aborted; 1580 + goto cleanup_transaction; 1581 + } 1578 1582 /* 1579 1583 * the reloc mutex makes sure that we stop 1580 1584 * the balancing code from coming in and moving ··· 1658 1652 1659 1653 ret = commit_cowonly_roots(trans, root); 1660 1654 if (ret) { 1655 + mutex_unlock(&root->fs_info->tree_log_mutex); 1656 + mutex_unlock(&root->fs_info->reloc_mutex); 1657 + goto cleanup_transaction; 1658 + } 1659 + 1660 + /* 1661 + * The tasks which save the space cache and inode cache may also 1662 + * update ->aborted, check it. 1663 + */ 1664 + if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 1665 + ret = cur_trans->aborted; 1661 1666 mutex_unlock(&root->fs_info->tree_log_mutex); 1662 1667 mutex_unlock(&root->fs_info->reloc_mutex); 1663 1668 goto cleanup_transaction;
+8 -2
fs/btrfs/tree-log.c
··· 3357 3357 if (skip_csum) 3358 3358 return 0; 3359 3359 3360 + if (em->compress_type) { 3361 + csum_offset = 0; 3362 + csum_len = block_len; 3363 + } 3364 + 3360 3365 /* block start is already adjusted for the file extent offset. */ 3361 3366 ret = btrfs_lookup_csums_range(log->fs_info->csum_root, 3362 3367 em->block_start + csum_offset, ··· 3415 3410 em = list_entry(extents.next, struct extent_map, list); 3416 3411 3417 3412 list_del_init(&em->list); 3418 - clear_bit(EXTENT_FLAG_LOGGING, &em->flags); 3419 3413 3420 3414 /* 3421 3415 * If we had an error we just need to delete everybody from our 3422 3416 * private list. 3423 3417 */ 3424 3418 if (ret) { 3419 + clear_em_logging(tree, em); 3425 3420 free_extent_map(em); 3426 3421 continue; 3427 3422 } ··· 3429 3424 write_unlock(&tree->lock); 3430 3425 3431 3426 ret = log_one_extent(trans, inode, root, em, path); 3432 - free_extent_map(em); 3433 3427 write_lock(&tree->lock); 3428 + clear_em_logging(tree, em); 3429 + free_extent_map(em); 3434 3430 } 3435 3431 WARN_ON(!list_empty(&extents)); 3436 3432 write_unlock(&tree->lock);
+17 -6
fs/btrfs/volumes.c
··· 1431 1431 } 1432 1432 } else { 1433 1433 ret = btrfs_get_bdev_and_sb(device_path, 1434 - FMODE_READ | FMODE_EXCL, 1434 + FMODE_WRITE | FMODE_EXCL, 1435 1435 root->fs_info->bdev_holder, 0, 1436 1436 &bdev, &bh); 1437 1437 if (ret) ··· 2614 2614 cache = btrfs_lookup_block_group(fs_info, chunk_offset); 2615 2615 chunk_used = btrfs_block_group_used(&cache->item); 2616 2616 2617 - user_thresh = div_factor_fine(cache->key.offset, bargs->usage); 2617 + if (bargs->usage == 0) 2618 + user_thresh = 0; 2619 + else if (bargs->usage > 100) 2620 + user_thresh = cache->key.offset; 2621 + else 2622 + user_thresh = div_factor_fine(cache->key.offset, 2623 + bargs->usage); 2624 + 2618 2625 if (chunk_used < user_thresh) 2619 2626 ret = 0; 2620 2627 ··· 2966 2959 unset_balance_control(fs_info); 2967 2960 ret = del_balance_item(fs_info->tree_root); 2968 2961 BUG_ON(ret); 2962 + 2963 + atomic_set(&fs_info->mutually_exclusive_operation_running, 0); 2969 2964 } 2970 2965 2971 2966 void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock, ··· 3147 3138 out: 3148 3139 if (bctl->flags & BTRFS_BALANCE_RESUME) 3149 3140 __cancel_balance(fs_info); 3150 - else 3141 + else { 3151 3142 kfree(bctl); 3143 + atomic_set(&fs_info->mutually_exclusive_operation_running, 0); 3144 + } 3152 3145 return ret; 3153 3146 } 3154 3147 ··· 3167 3156 ret = btrfs_balance(fs_info->balance_ctl, NULL); 3168 3157 } 3169 3158 3170 - atomic_set(&fs_info->mutually_exclusive_operation_running, 0); 3171 3159 mutex_unlock(&fs_info->balance_mutex); 3172 3160 mutex_unlock(&fs_info->volume_mutex); 3173 3161 ··· 3189 3179 return 0; 3190 3180 } 3191 3181 3192 - WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)); 3193 3182 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance"); 3194 3183 if (IS_ERR(tsk)) 3195 3184 return PTR_ERR(tsk); ··· 3241 3232 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs); 3242 3233 btrfs_balance_sys(leaf, item, &disk_bargs); 3243 3234 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs); 3235 + 3236 + WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)); 3244 3237 3245 3238 mutex_lock(&fs_info->volume_mutex); 3246 3239 mutex_lock(&fs_info->balance_mutex); ··· 3507 3496 { 1, 1, 2, 2, 2, 2 /* raid1 */ }, 3508 3497 { 1, 2, 1, 1, 1, 2 /* dup */ }, 3509 3498 { 1, 1, 0, 2, 1, 1 /* raid0 */ }, 3510 - { 1, 1, 0, 1, 1, 1 /* single */ }, 3499 + { 1, 1, 1, 1, 1, 1 /* single */ }, 3511 3500 }; 3512 3501 3513 3502 static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,