@@ -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
17861808int 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