History

btrfs: qgroup: check generation when recording simple quota delta

Simple quotas count extents only from the moment the feature is enabled. Therefore, if we do something like: 1. create subvol S 2. write F in S 3. enable quotas 4. remove F 5. write G in S then after 3. and 4. we would expect the simple quota usage of S to be 0 (putting aside some metadata extents that might be written) and after 5., it should be the size of G plus metadata. Therefore, we need to be able to determine whether a particular quota delta we are processing predates simple quota enablement. To do this, store the transaction id when quotas were enabled. In fs_info for immediate use and in the quota status item to make it recoverable on mount. When we see a delta, check if the generation of the extent item is less than that of quota enablement. If so, we should ignore the delta from this extent.

Boris Burkov committed 3 years ago

Commit bd7c1ea1 parent 5343cd96 files · +40 −6Browse files

Unified · Split

6 files
  1. fs/btrfs/accessors.h +2 −0
  2. fs/btrfs/extent-tree.c +4 −0
  3. fs/btrfs/fs.h +1 −0
  4. fs/btrfs/qgroup.c +22 −6
  5. fs/btrfs/qgroup.h +2 −0
  6. include/uapi/linux/btrfs_tree.h +9 −0
fs/btrfs/accessors.h+2−0
@@ -980,6 +980,8 @@ BTRFS_SETGET_FUNCS(qgroup_status_flags, struct btrfs_qgroup_status_item, · 979 unmodified lines
980980 flags, 64);
981981 BTRFS_SETGET_FUNCS(qgroup_status_rescan, struct btrfs_qgroup_status_item,
982982 rescan, 64);
983+BTRFS_SETGET_FUNCS(qgroup_status_enable_gen, struct btrfs_qgroup_status_item,
984+ enable_gen, 64);
983985
984986 /* btrfs_qgroup_info_item */
985987 BTRFS_SETGET_FUNCS(qgroup_info_generation, struct btrfs_qgroup_info_item,
fs/btrfs/extent-tree.c+4−0
@@ -1566,6 +1566,7 @@ static int run_delayed_data_ref(struct btrfs_trans_handle *trans, · 1565 unmodified lines
15661566 .rsv_bytes = href->reserved_bytes,
15671567 .is_data = true,
15681568 .is_inc = true,
1569+ .generation = trans->transid,
15691570 };
15701571
15711572 if (extent_op)
@@ -1738,6 +1739,7 @@ static int run_delayed_tree_ref(struct btrfs_trans_handle *trans, · 166 unmodified lines
17381739 .rsv_bytes = 0,
17391740 .is_data = false,
17401741 .is_inc = true,
1742+ .generation = trans->transid,
17411743 };
17421744
17431745 BUG_ON(!extent_op || !extent_op->update_flags);
@@ -3287,6 +3289,7 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans, · 1543 unmodified lines
32873289 .rsv_bytes = 0,
32883290 .is_data = is_data,
32893291 .is_inc = false,
3292+ .generation = btrfs_extent_generation(leaf, ei),
32903293 };
32913294
32923295 /* In this branch refs == 1 */
@@ -4924,6 +4927,7 @@ int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, · 1631 unmodified lines
49244927 struct btrfs_squota_delta delta = {
49254928 .root = root_objectid,
49264929 .num_bytes = ins->offset,
4930+ .generation = trans->transid,
49274931 .rsv_bytes = 0,
49284932 .is_data = true,
49294933 .is_inc = true,
fs/btrfs/fs.h+1−0
@@ -687,6 +687,7 @@ struct btrfs_fs_info { · 686 unmodified lines
687687 /* Protected by qgroup_rescan_lock */
688688 bool qgroup_rescan_running;
689689 u8 qgroup_drop_subtree_thres;
690+ u64 qgroup_enable_gen;
690691
691692 /*
692693 * If this is not 0, then it indicates a serious filesystem error has
fs/btrfs/qgroup.c+22−6
@@ -369,6 +369,15 @@ static void qgroup_mark_inconsistent(struct btrfs_fs_info *fs_info) · 368 unmodified lines
369369 BTRFS_QGROUP_RUNTIME_FLAG_NO_ACCOUNTING);
370370 }
371371
372+static void qgroup_read_enable_gen(struct btrfs_fs_info *fs_info,
373+ struct extent_buffer *leaf, int slot,
374+ struct btrfs_qgroup_status_item *ptr)
375+{
376+ ASSERT(btrfs_fs_incompat(fs_info, SIMPLE_QUOTA));
377+ ASSERT(btrfs_item_size(leaf, slot) >= sizeof(*ptr));
378+ fs_info->qgroup_enable_gen = btrfs_qgroup_status_enable_gen(leaf, ptr);
379+}
380+
372381 /*
373382 * The full config is read in one go, only called from open_ctree()
374383 * It doesn't use any locking, as at this point we're still single-threaded
@@ -384,7 +393,6 @@ int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info) · 9 unmodified lines
384393 int ret = 0;
385394 u64 flags = 0;
386395 u64 rescan_progress = 0;
387− bool simple;
388396
389397 if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_DISABLED)
390398 return 0;
@@ -437,9 +445,9 @@ int btrfs_read_qgroup_config(struct btrfs_fs_info *fs_info) · 46 unmodified lines
437445 goto out;
438446 }
439447 fs_info->qgroup_flags = btrfs_qgroup_status_flags(l, ptr);
440− simple = (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_SIMPLE_MODE);
441− if (btrfs_qgroup_status_generation(l, ptr) !=
442− fs_info->generation && !simple) {
448+ if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_SIMPLE_MODE) {
449+ qgroup_read_enable_gen(fs_info, l, slot, ptr);
450+ } else if (btrfs_qgroup_status_generation(l, ptr) != fs_info->generation) {
443451 qgroup_mark_inconsistent(fs_info);
444452 btrfs_err(fs_info,
445453 "qgroup generation mismatch, marked as inconsistent");
@@ -1103,10 +1111,12 @@ int btrfs_quota_enable(struct btrfs_fs_info *fs_info, · 657 unmodified lines
11031111 btrfs_set_qgroup_status_generation(leaf, ptr, trans->transid);
11041112 btrfs_set_qgroup_status_version(leaf, ptr, BTRFS_QGROUP_STATUS_VERSION);
11051113 fs_info->qgroup_flags = BTRFS_QGROUP_STATUS_FLAG_ON;
1106− if (simple)
1114+ if (simple) {
11071115 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_SIMPLE_MODE;
1108− else
1116+ btrfs_set_qgroup_status_enable_gen(leaf, ptr, trans->transid);
1117+ } else {
11091118 fs_info->qgroup_flags |= BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT;
1119+ }
11101120 btrfs_set_qgroup_status_flags(leaf, ptr, fs_info->qgroup_flags &
11111121 BTRFS_QGROUP_STATUS_FLAGS_MASK);
11121122 btrfs_set_qgroup_status_rescan(leaf, ptr, 0);
@@ -1210,6 +1220,8 @@ out_add_root: · 97 unmodified lines
12101220 goto out_free_path;
12111221 }
12121222
1223+ fs_info->qgroup_enable_gen = trans->transid;
1224+
12131225 mutex_unlock(&fs_info->qgroup_ioctl_lock);
12141226 /*
12151227 * Commit the transaction while not holding qgroup_ioctl_lock, to avoid
@@ -4655,6 +4667,10 @@ int btrfs_record_squota_delta(struct btrfs_fs_info *fs_info, · 3439 unmodified lines
46554667 if (!is_fstree(root))
46564668 return 0;
46574669
4670+ /* If the extent predates enabling quotas, don't count it. */
4671+ if (delta->generation < fs_info->qgroup_enable_gen)
4672+ return 0;
4673+
46584674 spin_lock(&fs_info->qgroup_lock);
46594675 qgroup = find_qgroup_rb(fs_info, root);
46604676 if (!qgroup) {
fs/btrfs/qgroup.h+2−0
@@ -276,6 +276,8 @@ struct btrfs_squota_delta { · 275 unmodified lines
276276 u64 num_bytes;
277277 /* The number of bytes reserved for this extent. */
278278 u64 rsv_bytes;
279+ /* The generation the extent was created in. */
280+ u64 generation;
279281 /* Whether we are using or freeing the extent. */
280282 bool is_inc;
281283 /* Whether the extent is data or metadata. */
include/uapi/linux/btrfs_tree.h+9−0
@@ -1277,6 +1277,15 @@ struct btrfs_qgroup_status_item { · 1276 unmodified lines
12771277 * of the scan. It contains a logical address
12781278 */
12791279 __le64 rescan;
1280+
1281+ /*
1282+ * The generation when quotas were last enabled. Used by simple quotas to
1283+ * avoid decrementing when freeing an extent that was written before
1284+ * enable.
1285+ *
1286+ * Set only if flags contain BTRFS_QGROUP_STATUS_FLAG_SIMPLE_MODE.
1287+ */
1288+ __le64 enable_gen;
12801289 } __attribute__ ((__packed__));
12811290
12821291 struct btrfs_qgroup_info_item {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t