Skip to content

Commit 4005446

Browse files
committed
Merge tag 'for-7.1-rc4-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave/linux
Pull btrfs fixes from David Sterba: "A batch of fixes to simple quotas: - add conditional rescheduling point not dependent on the lock during inode iterations to avoid delays with PREEMPT_NONE enabled - fix subvolume deletion so it does not break the squota invariants - properly handle enabling squota, tracking extents in the initial transaction - catch and warn about underflows, clamp to zero to avoid further problems And one fix to inode size handling: - fix handling of preallocated extents beyond i_size when not using the no-holes feature" * tag 'for-7.1-rc4-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave/linux: btrfs: swallow btrfs_record_squota_delta() ENOENT btrfs: clamp to avoid squota underflow btrfs: fix squota accounting during enable generation btrfs: check for subvolume before deleting squota qgroup btrfs: always drop root->inodes lock before cond_resched() btrfs: mark file extent range dirty after converting prealloc extents
2 parents f83ef5b + f13342e commit 4005446

5 files changed

Lines changed: 83 additions & 38 deletions

File tree

fs/btrfs/extent_map.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1246,7 +1246,9 @@ static struct btrfs_inode *find_first_inode_to_shrink(struct btrfs_root *root,
12461246
write_unlock(&tree->lock);
12471247
next:
12481248
from = btrfs_ino(inode) + 1;
1249-
cond_resched_lock(&root->inodes.xa_lock);
1249+
xa_unlock(&root->inodes);
1250+
cond_resched();
1251+
xa_lock(&root->inodes);
12501252
}
12511253
xa_unlock(&root->inodes);
12521254

fs/btrfs/file.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
633633
trans->transid);
634634
btrfs_set_file_extent_num_bytes(leaf, fi,
635635
end - other_start);
636-
return 0;
636+
goto mark_dirty;
637637
}
638638
}
639639

@@ -661,7 +661,7 @@ int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
661661
other_end - start);
662662
btrfs_set_file_extent_offset(leaf, fi,
663663
start - orig_offset);
664-
return 0;
664+
goto mark_dirty;
665665
}
666666
}
667667

@@ -788,7 +788,12 @@ int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
788788
}
789789
}
790790

791-
return 0;
791+
mark_dirty:
792+
ret = btrfs_inode_set_file_extent_range(inode, start, end - start);
793+
if (ret)
794+
btrfs_abort_transaction(trans, ret);
795+
796+
return ret;
792797
}
793798

794799
/*

fs/btrfs/fs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ enum {
155155
BTRFS_FS_LOG_RECOVERING,
156156
BTRFS_FS_OPEN,
157157
BTRFS_FS_QUOTA_ENABLED,
158+
BTRFS_FS_SQUOTA_ENABLING,
158159
BTRFS_FS_UPDATE_UUID_TREE_GEN,
159160
BTRFS_FS_CREATING_FREE_SPACE_TREE,
160161
BTRFS_FS_BTREE_ERR,

fs/btrfs/inode.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10699,7 +10699,9 @@ struct btrfs_inode *btrfs_find_first_inode(struct btrfs_root *root, u64 min_ino)
1069910699
break;
1070010700

1070110701
from = btrfs_ino(inode) + 1;
10702-
cond_resched_lock(&root->inodes.xa_lock);
10702+
xa_unlock(&root->inodes);
10703+
cond_resched();
10704+
xa_lock(&root->inodes);
1070310705
}
1070410706
xa_unlock(&root->inodes);
1070510707

fs/btrfs/qgroup.c

Lines changed: 68 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,7 +1107,13 @@ int btrfs_quota_enable(struct btrfs_fs_info *fs_info,
11071107
if (simple) {
11081108
fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_SIMPLE_MODE;
11091109
btrfs_set_fs_incompat(fs_info, SIMPLE_QUOTA);
1110-
btrfs_set_qgroup_status_enable_gen(leaf, ptr, trans->transid);
1110+
/*
1111+
* Set the enable generation to the next transaction, as we cannot
1112+
* ensure that extents written during this transaction will see any
1113+
* state we have set here. So we should treat all extents of the
1114+
* transaction as coming in before squotas was enabled.
1115+
*/
1116+
btrfs_set_qgroup_status_enable_gen(leaf, ptr, trans->transid + 1);
11111117
} else {
11121118
fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
11131119
}
@@ -1210,7 +1216,15 @@ int btrfs_quota_enable(struct btrfs_fs_info *fs_info,
12101216
goto out_free_path;
12111217
}
12121218

1213-
fs_info->qgroup_enable_gen = trans->transid;
1219+
/*
1220+
* Set fs_info->qgroup_enable_gen and BTRFS_FS_SQUOTA_ENABLING
1221+
* under the transaction handle. We want to ensure that all extents in
1222+
* the next transaction definitely see them.
1223+
*/
1224+
if (simple) {
1225+
fs_info->qgroup_enable_gen = trans->transid + 1;
1226+
set_bit(BTRFS_FS_SQUOTA_ENABLING, &fs_info->flags);
1227+
}
12141228

12151229
mutex_unlock(&fs_info->qgroup_ioctl_lock);
12161230
/*
@@ -1224,9 +1238,15 @@ int btrfs_quota_enable(struct btrfs_fs_info *fs_info,
12241238
*/
12251239
ret = btrfs_commit_transaction(trans);
12261240
trans = NULL;
1241+
12271242
mutex_lock(&fs_info->qgroup_ioctl_lock);
1228-
if (ret)
1243+
if (ret) {
1244+
if (simple) {
1245+
clear_bit(BTRFS_FS_SQUOTA_ENABLING, &fs_info->flags);
1246+
fs_info->qgroup_enable_gen = 0;
1247+
}
12291248
goto out_free_path;
1249+
}
12301250

12311251
/*
12321252
* Set quota enabled flag after committing the transaction, to avoid
@@ -1236,6 +1256,8 @@ int btrfs_quota_enable(struct btrfs_fs_info *fs_info,
12361256
spin_lock(&fs_info->qgroup_lock);
12371257
fs_info->quota_root = quota_root;
12381258
set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
1259+
if (simple)
1260+
clear_bit(BTRFS_FS_SQUOTA_ENABLING, &fs_info->flags);
12391261
spin_unlock(&fs_info->qgroup_lock);
12401262

12411263
/* Skip rescan for simple qgroups. */
@@ -1715,32 +1737,24 @@ int btrfs_create_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)
17151737
return ret;
17161738
}
17171739

1718-
static bool can_delete_parent_qgroup(struct btrfs_qgroup *qgroup)
1719-
1740+
static bool can_delete_parent_qgroup(struct btrfs_fs_info *fs_info, struct btrfs_qgroup *qgroup)
17201741
{
17211742
ASSERT(btrfs_qgroup_level(qgroup->qgroupid));
1743+
if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE)
1744+
squota_check_parent_usage(fs_info, qgroup);
17221745
return list_empty(&qgroup->members);
17231746
}
17241747

17251748
/*
1726-
* Return true if we can delete the squota qgroup and false otherwise.
1727-
*
1728-
* Rules for whether we can delete:
1729-
*
1730-
* A subvolume qgroup can be removed iff the subvolume is fully deleted, which
1731-
* is iff there is 0 usage in the qgroup.
1732-
*
1733-
* A higher level qgroup can be removed iff it has no members.
1734-
* Note: We audit its usage to warn on inconsitencies without blocking deletion.
1749+
* Because a shared extent can outlive its owning subvolume, we cannot delete a
1750+
* subvol squota qgroup until all of the extents it owns are gone, even if the
1751+
* subvolume itself has been deleted.
17351752
*/
1736-
static bool can_delete_squota_qgroup(struct btrfs_fs_info *fs_info, struct btrfs_qgroup *qgroup)
1753+
static bool can_delete_squota_subvol_qgroup(struct btrfs_fs_info *fs_info,
1754+
struct btrfs_qgroup *qgroup)
17371755
{
17381756
ASSERT(btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE);
1739-
1740-
if (btrfs_qgroup_level(qgroup->qgroupid) > 0) {
1741-
squota_check_parent_usage(fs_info, qgroup);
1742-
return can_delete_parent_qgroup(qgroup);
1743-
}
1757+
ASSERT(btrfs_qgroup_level(qgroup->qgroupid) == 0);
17441758

17451759
return !(qgroup->rfer || qgroup->excl || qgroup->rfer_cmpr || qgroup->excl_cmpr);
17461760
}
@@ -1754,14 +1768,11 @@ static int can_delete_qgroup(struct btrfs_fs_info *fs_info, struct btrfs_qgroup
17541768
{
17551769
struct btrfs_key key;
17561770
BTRFS_PATH_AUTO_FREE(path);
1757-
1758-
/* Since squotas cannot be inconsistent, they have special rules for deletion. */
1759-
if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE)
1760-
return can_delete_squota_qgroup(fs_info, qgroup);
1771+
int ret;
17611772

17621773
/* For higher level qgroup, we can only delete it if it has no child. */
17631774
if (btrfs_qgroup_level(qgroup->qgroupid))
1764-
return can_delete_parent_qgroup(qgroup);
1775+
return can_delete_parent_qgroup(fs_info, qgroup);
17651776

17661777
/*
17671778
* For level-0 qgroups, we can only delete it if it has no subvolume
@@ -1777,10 +1788,21 @@ static int can_delete_qgroup(struct btrfs_fs_info *fs_info, struct btrfs_qgroup
17771788
return -ENOMEM;
17781789

17791790
/*
1780-
* The @ret from btrfs_find_root() exactly matches our definition for
1781-
* the return value, thus can be returned directly.
1791+
* Any subvol qgroup, regardless of mode, cannot be deleted if the
1792+
* subvol still exists.
17821793
*/
1783-
return btrfs_find_root(fs_info->tree_root, &key, path, NULL, NULL);
1794+
ret = btrfs_find_root(fs_info->tree_root, &key, path, NULL, NULL);
1795+
/*
1796+
* btrfs_find_root returns <0 on error, 0 if found, and >0 if not,
1797+
* so the "found" and "error" cases match our desired return values.
1798+
*/
1799+
if (ret <= 0)
1800+
return ret;
1801+
1802+
/* Squotas require additional checks, even if the subvol is deleted. */
1803+
if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_SIMPLE)
1804+
return can_delete_squota_subvol_qgroup(fs_info, qgroup);
1805+
return 1;
17841806
}
17851807

17861808
int btrfs_remove_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)
@@ -4922,7 +4944,8 @@ int btrfs_record_squota_delta(struct btrfs_fs_info *fs_info,
49224944
u64 num_bytes = delta->num_bytes;
49234945
const int sign = (delta->is_inc ? 1 : -1);
49244946

4925-
if (btrfs_qgroup_mode(fs_info) != BTRFS_QGROUP_MODE_SIMPLE)
4947+
if (btrfs_qgroup_mode(fs_info) != BTRFS_QGROUP_MODE_SIMPLE &&
4948+
!test_bit(BTRFS_FS_SQUOTA_ENABLING, &fs_info->flags))
49264949
return 0;
49274950

49284951
if (!btrfs_is_fstree(root))
@@ -4934,8 +4957,9 @@ int btrfs_record_squota_delta(struct btrfs_fs_info *fs_info,
49344957

49354958
spin_lock(&fs_info->qgroup_lock);
49364959
qgroup = find_qgroup_rb(fs_info, root);
4937-
if (!qgroup) {
4938-
ret = -ENOENT;
4960+
if (WARN_ON_ONCE(!qgroup)) {
4961+
btrfs_warn(fs_info, "squota failed to find qgroup for root %llu", root);
4962+
ret = 0;
49394963
goto out;
49404964
}
49414965

@@ -4944,8 +4968,19 @@ int btrfs_record_squota_delta(struct btrfs_fs_info *fs_info,
49444968
list_for_each_entry(qg, &qgroup_list, iterator) {
49454969
struct btrfs_qgroup_list *glist;
49464970

4947-
qg->excl += num_bytes * sign;
4948-
qg->rfer += num_bytes * sign;
4971+
ASSERT(qg->excl == qg->rfer);
4972+
if (WARN_ON_ONCE(sign < 0 && qg->excl < num_bytes)) {
4973+
btrfs_warn(fs_info,
4974+
"squota underflow qg %hu/%llu excl %llu num_bytes %llu",
4975+
btrfs_qgroup_level(qg->qgroupid),
4976+
btrfs_qgroup_subvolid(qg->qgroupid),
4977+
qg->excl, num_bytes);
4978+
qg->excl = 0;
4979+
qg->rfer = 0;
4980+
} else {
4981+
qg->excl += num_bytes * sign;
4982+
qg->rfer += num_bytes * sign;
4983+
}
49494984
qgroup_dirty(fs_info, qg);
49504985

49514986
list_for_each_entry(glist, &qg->groups, next_group)

0 commit comments

Comments
 (0)