* [PATCH 0/3] btrfs-progs: btrfstune --remove-simple-quota
@ 2024-07-11 21:18 Boris Burkov
2024-07-11 21:18 ` [PATCH 1/3] btrfs-progs: add a helper for clearing all the items in a tree Boris Burkov
` (3 more replies)
0 siblings, 4 replies; 9+ messages in thread
From: Boris Burkov @ 2024-07-11 21:18 UTC (permalink / raw)
To: linux-btrfs, kernel-team
To be able to nuke simple quotas entirely if you decide you don't want
them (and especially the OWNER_REFs) in your filesystem after all.
If you run
btrfstune --remove-simple-quota <dev>
on an unmounted filesystem, it will be as if simple quotas never existed
on that filesystem.
Boris Burkov (3):
btrfs-progs: add a helper for clearing all the items in a tree
btrfs-progs: btrfstune: fix documentation for --enable-simple-quota
btrfs-progs: btrfstune: add ability to remove squotas
kernel-shared/disk-io.c | 39 +++++
kernel-shared/disk-io.h | 2 +
kernel-shared/free-space-tree.c | 42 +----
.../065-btrfstune-simple-quota/test.sh | 33 ++++
tune/main.c | 18 +-
tune/quota.c | 160 ++++++++++++++++++
tune/tune.h | 1 +
7 files changed, 253 insertions(+), 42 deletions(-)
create mode 100755 tests/misc-tests/065-btrfstune-simple-quota/test.sh
--
2.45.2
^ permalink raw reply [flat|nested] 9+ messages in thread* [PATCH 1/3] btrfs-progs: add a helper for clearing all the items in a tree 2024-07-11 21:18 [PATCH 0/3] btrfs-progs: btrfstune --remove-simple-quota Boris Burkov @ 2024-07-11 21:18 ` Boris Burkov 2024-07-11 22:32 ` Qu Wenruo 2024-07-11 21:18 ` [PATCH 2/3] btrfs-progs: btrfstune: fix documentation for --enable-simple-quota Boris Burkov ` (2 subsequent siblings) 3 siblings, 1 reply; 9+ messages in thread From: Boris Burkov @ 2024-07-11 21:18 UTC (permalink / raw) To: linux-btrfs, kernel-team Used in clear_free_space_tree, this is a totally generic operation. It will also be used for clearing the qgroup tree from btrfstune. Signed-off-by: Boris Burkov <boris@bur.io> --- kernel-shared/disk-io.c | 39 ++++++++++++++++++++++++++++++ kernel-shared/disk-io.h | 2 ++ kernel-shared/free-space-tree.c | 42 ++------------------------------- 3 files changed, 43 insertions(+), 40 deletions(-) diff --git a/kernel-shared/disk-io.c b/kernel-shared/disk-io.c index 295bd50ad..1e4c46aa0 100644 --- a/kernel-shared/disk-io.c +++ b/kernel-shared/disk-io.c @@ -2342,6 +2342,45 @@ static bool is_global_root(struct btrfs_root *root) return true; return false; } + +int btrfs_clear_tree(struct btrfs_trans_handle *trans, + struct btrfs_root *root) +{ + struct btrfs_path *path; + struct btrfs_key key; + struct extent_buffer *leaf = NULL; + int ret; + int nr = 0; + + path = btrfs_alloc_path(); + if (!path) + return -ENOMEM; + + key.objectid = 0; + key.offset = 0; + key.type = 0; + + while (1) { + ret = btrfs_search_slot(trans, root, &key, path, -1, 1); + if (ret < 0) + goto out; + leaf = path->nodes[0]; + nr = btrfs_header_nritems(leaf); + if (!nr) + break; + path->slots[0] = 0; + ret = btrfs_del_items(trans, root, path, 0, nr); + if (ret) + goto out; + + btrfs_release_path(path); + } + ret = 0; +out: + btrfs_free_path(path); + return ret; +} + int btrfs_delete_and_free_root(struct btrfs_trans_handle *trans, struct btrfs_root *root) { diff --git a/kernel-shared/disk-io.h b/kernel-shared/disk-io.h index 9f848635f..702a5e274 100644 --- a/kernel-shared/disk-io.h +++ b/kernel-shared/disk-io.h @@ -241,6 +241,8 @@ int btrfs_fs_roots_compare_roots(const struct rb_node *node1, const struct rb_no struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, struct btrfs_fs_info *fs_info, struct btrfs_key *key); +int btrfs_clear_tree(struct btrfs_trans_handle *trans, + struct btrfs_root *root); int btrfs_delete_and_free_root(struct btrfs_trans_handle *trans, struct btrfs_root *root); struct btrfs_root *btrfs_csum_root(struct btrfs_fs_info *fs_info, u64 bytenr); diff --git a/kernel-shared/free-space-tree.c b/kernel-shared/free-space-tree.c index 93806ca01..08b220740 100644 --- a/kernel-shared/free-space-tree.c +++ b/kernel-shared/free-space-tree.c @@ -1228,44 +1228,6 @@ out: btrfs_abort_transaction(trans, ret); return ret; } -static int clear_free_space_tree(struct btrfs_trans_handle *trans, - struct btrfs_root *root) -{ - struct btrfs_path *path; - struct btrfs_key key; - int nr; - int ret; - - path = btrfs_alloc_path(); - if (!path) - return -ENOMEM; - - key.objectid = 0; - key.type = 0; - key.offset = 0; - - while (1) { - ret = btrfs_search_slot(trans, root, &key, path, -1, 1); - if (ret < 0) - goto out; - - nr = btrfs_header_nritems(path->nodes[0]); - if (!nr) - break; - - path->slots[0] = 0; - ret = btrfs_del_items(trans, root, path, 0, nr); - if (ret) - goto out; - - btrfs_release_path(path); - } - - ret = 0; -out: - btrfs_free_path(path); - return ret; -} int btrfs_clear_free_space_tree(struct btrfs_fs_info *fs_info) { @@ -1288,7 +1250,7 @@ int btrfs_clear_free_space_tree(struct btrfs_fs_info *fs_info) while (key.offset < fs_info->nr_global_roots) { free_space_root = btrfs_global_root(fs_info, &key); - ret = clear_free_space_tree(trans, free_space_root); + ret = btrfs_clear_tree(trans, free_space_root); if (ret) goto abort; key.offset++; @@ -1299,7 +1261,7 @@ int btrfs_clear_free_space_tree(struct btrfs_fs_info *fs_info) BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE); btrfs_set_super_compat_ro_flags(fs_info->super_copy, features); - ret = clear_free_space_tree(trans, free_space_root); + ret = btrfs_clear_tree(trans, free_space_root); if (ret) goto abort; -- 2.45.2 ^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH 1/3] btrfs-progs: add a helper for clearing all the items in a tree 2024-07-11 21:18 ` [PATCH 1/3] btrfs-progs: add a helper for clearing all the items in a tree Boris Burkov @ 2024-07-11 22:32 ` Qu Wenruo 0 siblings, 0 replies; 9+ messages in thread From: Qu Wenruo @ 2024-07-11 22:32 UTC (permalink / raw) To: Boris Burkov, linux-btrfs, kernel-team 在 2024/7/12 06:48, Boris Burkov 写道: > Used in clear_free_space_tree, this is a totally generic operation. > It will also be used for clearing the qgroup tree from btrfstune. > > Signed-off-by: Boris Burkov <boris@bur.io> > --- > kernel-shared/disk-io.c | 39 ++++++++++++++++++++++++++++++ > kernel-shared/disk-io.h | 2 ++ > kernel-shared/free-space-tree.c | 42 ++------------------------------- > 3 files changed, 43 insertions(+), 40 deletions(-) > > diff --git a/kernel-shared/disk-io.c b/kernel-shared/disk-io.c > index 295bd50ad..1e4c46aa0 100644 > --- a/kernel-shared/disk-io.c > +++ b/kernel-shared/disk-io.c > @@ -2342,6 +2342,45 @@ static bool is_global_root(struct btrfs_root *root) > return true; > return false; > } > + > +int btrfs_clear_tree(struct btrfs_trans_handle *trans, > + struct btrfs_root *root) The original function name clear_free_space_tree() is also shared inside kernel. Maybe you can also do a cleanup for kernel? Otherwise looks good to me. Reviewed-by: Qu Wenruo <wqu@suse.com> Thanks, Qu > +{ > + struct btrfs_path *path; > + struct btrfs_key key; > + struct extent_buffer *leaf = NULL; > + int ret; > + int nr = 0; > + > + path = btrfs_alloc_path(); > + if (!path) > + return -ENOMEM; > + > + key.objectid = 0; > + key.offset = 0; > + key.type = 0; > + > + while (1) { > + ret = btrfs_search_slot(trans, root, &key, path, -1, 1); > + if (ret < 0) > + goto out; > + leaf = path->nodes[0]; > + nr = btrfs_header_nritems(leaf); > + if (!nr) > + break; > + path->slots[0] = 0; > + ret = btrfs_del_items(trans, root, path, 0, nr); > + if (ret) > + goto out; > + > + btrfs_release_path(path); > + } > + ret = 0; > +out: > + btrfs_free_path(path); > + return ret; > +} > + > int btrfs_delete_and_free_root(struct btrfs_trans_handle *trans, > struct btrfs_root *root) > { > diff --git a/kernel-shared/disk-io.h b/kernel-shared/disk-io.h > index 9f848635f..702a5e274 100644 > --- a/kernel-shared/disk-io.h > +++ b/kernel-shared/disk-io.h > @@ -241,6 +241,8 @@ int btrfs_fs_roots_compare_roots(const struct rb_node *node1, const struct rb_no > struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, > struct btrfs_fs_info *fs_info, > struct btrfs_key *key); > +int btrfs_clear_tree(struct btrfs_trans_handle *trans, > + struct btrfs_root *root); > int btrfs_delete_and_free_root(struct btrfs_trans_handle *trans, > struct btrfs_root *root); > struct btrfs_root *btrfs_csum_root(struct btrfs_fs_info *fs_info, u64 bytenr); > diff --git a/kernel-shared/free-space-tree.c b/kernel-shared/free-space-tree.c > index 93806ca01..08b220740 100644 > --- a/kernel-shared/free-space-tree.c > +++ b/kernel-shared/free-space-tree.c > @@ -1228,44 +1228,6 @@ out: > btrfs_abort_transaction(trans, ret); > return ret; > } > -static int clear_free_space_tree(struct btrfs_trans_handle *trans, > - struct btrfs_root *root) > -{ > - struct btrfs_path *path; > - struct btrfs_key key; > - int nr; > - int ret; > - > - path = btrfs_alloc_path(); > - if (!path) > - return -ENOMEM; > - > - key.objectid = 0; > - key.type = 0; > - key.offset = 0; > - > - while (1) { > - ret = btrfs_search_slot(trans, root, &key, path, -1, 1); > - if (ret < 0) > - goto out; > - > - nr = btrfs_header_nritems(path->nodes[0]); > - if (!nr) > - break; > - > - path->slots[0] = 0; > - ret = btrfs_del_items(trans, root, path, 0, nr); > - if (ret) > - goto out; > - > - btrfs_release_path(path); > - } > - > - ret = 0; > -out: > - btrfs_free_path(path); > - return ret; > -} > > int btrfs_clear_free_space_tree(struct btrfs_fs_info *fs_info) > { > @@ -1288,7 +1250,7 @@ int btrfs_clear_free_space_tree(struct btrfs_fs_info *fs_info) > > while (key.offset < fs_info->nr_global_roots) { > free_space_root = btrfs_global_root(fs_info, &key); > - ret = clear_free_space_tree(trans, free_space_root); > + ret = btrfs_clear_tree(trans, free_space_root); > if (ret) > goto abort; > key.offset++; > @@ -1299,7 +1261,7 @@ int btrfs_clear_free_space_tree(struct btrfs_fs_info *fs_info) > BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE); > btrfs_set_super_compat_ro_flags(fs_info->super_copy, features); > > - ret = clear_free_space_tree(trans, free_space_root); > + ret = btrfs_clear_tree(trans, free_space_root); > if (ret) > goto abort; > ^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 2/3] btrfs-progs: btrfstune: fix documentation for --enable-simple-quota 2024-07-11 21:18 [PATCH 0/3] btrfs-progs: btrfstune --remove-simple-quota Boris Burkov 2024-07-11 21:18 ` [PATCH 1/3] btrfs-progs: add a helper for clearing all the items in a tree Boris Burkov @ 2024-07-11 21:18 ` Boris Burkov 2024-07-11 22:34 ` Qu Wenruo 2024-07-11 21:18 ` [PATCH 3/3] btrfs-progs: btrfstune: add ability to remove squotas Boris Burkov 2025-01-04 10:35 ` [PATCH 0/3] btrfs-progs: btrfstune --remove-simple-quota Anand Jain 3 siblings, 1 reply; 9+ messages in thread From: Boris Burkov @ 2024-07-11 21:18 UTC (permalink / raw) To: linux-btrfs, kernel-team The documentation lists -q as the flag for enabling simple quotas, but the actual parsing only handles --enable-simple-quota. Update the documentation string. Signed-off-by: Boris Burkov <boris@bur.io> --- tune/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tune/main.c b/tune/main.c index bec896907..cb93d2cb3 100644 --- a/tune/main.c +++ b/tune/main.c @@ -103,7 +103,7 @@ static const char * const tune_usage[] = { OPTLINE("-x", "enable skinny metadata extent refs (mkfs: skinny-metadata)"), OPTLINE("-n", "enable no-holes feature (mkfs: no-holes, more efficient sparse file representation)"), OPTLINE("-S <0|1>", "set/unset seeding status of a device"), - OPTLINE("-q", "enable simple quotas on the file system. (mkfs: squota)"), + OPTLINE("--enable-simple-quota", "enable simple quotas on the file system. (mkfs: squota)"), OPTLINE("--convert-to-block-group-tree", "convert filesystem to track block groups in " "the separate block-group-tree instead of extent tree (sets the incompat bit)"), OPTLINE("--convert-from-block-group-tree", -- 2.45.2 ^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH 2/3] btrfs-progs: btrfstune: fix documentation for --enable-simple-quota 2024-07-11 21:18 ` [PATCH 2/3] btrfs-progs: btrfstune: fix documentation for --enable-simple-quota Boris Burkov @ 2024-07-11 22:34 ` Qu Wenruo 0 siblings, 0 replies; 9+ messages in thread From: Qu Wenruo @ 2024-07-11 22:34 UTC (permalink / raw) To: Boris Burkov, linux-btrfs, kernel-team 在 2024/7/12 06:48, Boris Burkov 写道: > The documentation lists -q as the flag for enabling simple quotas, but > the actual parsing only handles --enable-simple-quota. Update the > documentation string. > > Signed-off-by: Boris Burkov <boris@bur.io> I didn't find the man page entry for simple quota in the latest devel branch. Is it missing or still experimental? Otherwise the change itself is good. Reviewed-by: Qu Wenruo <wqu@suse.com> Thanks, Qu > --- > tune/main.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/tune/main.c b/tune/main.c > index bec896907..cb93d2cb3 100644 > --- a/tune/main.c > +++ b/tune/main.c > @@ -103,7 +103,7 @@ static const char * const tune_usage[] = { > OPTLINE("-x", "enable skinny metadata extent refs (mkfs: skinny-metadata)"), > OPTLINE("-n", "enable no-holes feature (mkfs: no-holes, more efficient sparse file representation)"), > OPTLINE("-S <0|1>", "set/unset seeding status of a device"), > - OPTLINE("-q", "enable simple quotas on the file system. (mkfs: squota)"), > + OPTLINE("--enable-simple-quota", "enable simple quotas on the file system. (mkfs: squota)"), > OPTLINE("--convert-to-block-group-tree", "convert filesystem to track block groups in " > "the separate block-group-tree instead of extent tree (sets the incompat bit)"), > OPTLINE("--convert-from-block-group-tree", ^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 3/3] btrfs-progs: btrfstune: add ability to remove squotas 2024-07-11 21:18 [PATCH 0/3] btrfs-progs: btrfstune --remove-simple-quota Boris Burkov 2024-07-11 21:18 ` [PATCH 1/3] btrfs-progs: add a helper for clearing all the items in a tree Boris Burkov 2024-07-11 21:18 ` [PATCH 2/3] btrfs-progs: btrfstune: fix documentation for --enable-simple-quota Boris Burkov @ 2024-07-11 21:18 ` Boris Burkov 2024-07-11 22:43 ` Qu Wenruo 2025-01-04 10:35 ` [PATCH 0/3] btrfs-progs: btrfstune --remove-simple-quota Anand Jain 3 siblings, 1 reply; 9+ messages in thread From: Boris Burkov @ 2024-07-11 21:18 UTC (permalink / raw) To: linux-btrfs, kernel-team When simple quotas is enabled, every new data extent gets a special inline OWNER_REF item that identifies the owning subvolume. This makes simple quotas backwards incompatible with kernels older than v6.7. Even if you disable quotas on the filesystem, the OWNER_REF items are sprinkled throughout the extent tree and older kernels are unable to parse them. However, it is relatively easy to simply walk the extent tree and remove these inline ref items. This gives squota adopters the option to *fully* disable squotas on their system and un-set the incompat bit. Add this capability to btrfstune, which requires only a little tricky btrfs item data shifting. This functionality was tested with a new unit test, as well as a similar but more thorough integration test in fstests Signed-off-by: Boris Burkov <boris@bur.io> --- .../065-btrfstune-simple-quota/test.sh | 33 ++++ tune/main.c | 16 +- tune/quota.c | 160 ++++++++++++++++++ tune/tune.h | 1 + 4 files changed, 209 insertions(+), 1 deletion(-) create mode 100755 tests/misc-tests/065-btrfstune-simple-quota/test.sh diff --git a/tests/misc-tests/065-btrfstune-simple-quota/test.sh b/tests/misc-tests/065-btrfstune-simple-quota/test.sh new file mode 100755 index 000000000..d7ccaf4e9 --- /dev/null +++ b/tests/misc-tests/065-btrfstune-simple-quota/test.sh @@ -0,0 +1,33 @@ +#!/bin/bash +# Verify btrfstune for enabling and removing simple quotas + +source "$TEST_TOP/common" || exit +source "$TEST_TOP/common.convert" || exit + +check_experimental_build +setup_root_helper +prepare_test_dev + +# Create the fs without simple quota +run_check_mkfs_test_dev +run_check_mount_test_dev +populate_fs +run_check_umount_test_dev +# Enable simple quotas +run_check $SUDO_HELPER "$TOP/btrfstune" --enable-simple-quota "$TEST_DEV" +run_check_mount_test_dev +run_check $SUDO_HELPER dd if=/dev/zero of="$TEST_MNT"/file2 bs=1M count=1 +run_check_umount_test_dev +run_check $SUDO_HELPER "$TOP/btrfs" check "$TEST_DEV" + +# Populate new fs with simple quotas enabled +run_check_mkfs_test_dev -O squota +run_check_mount_test_dev +populate_fs +run_check_umount_test_dev +# Remove simple quotas +run_check $SUDO_HELPER "$TOP/btrfstune" --remove-simple-quota "$TEST_DEV" +run_check_mount_test_dev +run_check $SUDO_HELPER dd if=/dev/zero of="$TEST_MNT"/file3 bs=1M count=1 +run_check_umount_test_dev +run_check $SUDO_HELPER "$TOP/btrfs" check "$TEST_DEV" diff --git a/tune/main.c b/tune/main.c index cb93d2cb3..6ef8bbe2d 100644 --- a/tune/main.c +++ b/tune/main.c @@ -104,6 +104,7 @@ static const char * const tune_usage[] = { OPTLINE("-n", "enable no-holes feature (mkfs: no-holes, more efficient sparse file representation)"), OPTLINE("-S <0|1>", "set/unset seeding status of a device"), OPTLINE("--enable-simple-quota", "enable simple quotas on the file system. (mkfs: squota)"), + OPTLINE("--remove-simple-quota", "remove simple quotas from the file system."), OPTLINE("--convert-to-block-group-tree", "convert filesystem to track block groups in " "the separate block-group-tree instead of extent tree (sets the incompat bit)"), OPTLINE("--convert-from-block-group-tree", @@ -198,6 +199,7 @@ int BOX_MAIN(btrfstune)(int argc, char *argv[]) int ret; u64 super_flags = 0; int quota = 0; + int remove_simple_quota = 0; int fd = -1; int oflags = O_RDWR; @@ -209,7 +211,7 @@ int BOX_MAIN(btrfstune)(int argc, char *argv[]) GETOPT_VAL_DISABLE_BLOCK_GROUP_TREE, GETOPT_VAL_ENABLE_FREE_SPACE_TREE, GETOPT_VAL_ENABLE_SIMPLE_QUOTA, - + GETOPT_VAL_REMOVE_SIMPLE_QUOTA, }; static const struct option long_options[] = { { "help", no_argument, NULL, GETOPT_VAL_HELP}, @@ -221,6 +223,8 @@ int BOX_MAIN(btrfstune)(int argc, char *argv[]) GETOPT_VAL_ENABLE_FREE_SPACE_TREE}, { "enable-simple-quota", no_argument, NULL, GETOPT_VAL_ENABLE_SIMPLE_QUOTA }, + { "remove-simple-quota", no_argument, NULL, + GETOPT_VAL_REMOVE_SIMPLE_QUOTA}, #if EXPERIMENTAL { "csum", required_argument, NULL, GETOPT_VAL_CSUM }, #endif @@ -288,6 +292,10 @@ int BOX_MAIN(btrfstune)(int argc, char *argv[]) quota = 1; btrfstune_cmd_groups[QGROUP] = true; break; + case GETOPT_VAL_REMOVE_SIMPLE_QUOTA: + remove_simple_quota = 1; + btrfstune_cmd_groups[QGROUP] = true; + break; #if EXPERIMENTAL case GETOPT_VAL_CSUM: btrfs_warn_experimental( @@ -535,6 +543,12 @@ int BOX_MAIN(btrfstune)(int argc, char *argv[]) goto out; } + if (remove_simple_quota) { + ret = remove_squota(root->fs_info); + if (ret) + goto out; + } + out: if (ret < 0) { fs_info->readonly = 1; diff --git a/tune/quota.c b/tune/quota.c index a14f45307..16b2b3fb6 100644 --- a/tune/quota.c +++ b/tune/quota.c @@ -6,6 +6,166 @@ #include "common/messages.h" #include "tune/tune.h" +static int remove_quota_tree(struct btrfs_fs_info *fs_info) +{ + int ret; + struct btrfs_root *quota_root = fs_info->quota_root; + struct btrfs_root *tree_root = fs_info->tree_root; + struct btrfs_super_block *sb = fs_info->super_copy; + int super_flags = btrfs_super_incompat_flags(sb); + struct btrfs_trans_handle *trans; + + trans = btrfs_start_transaction(quota_root, 0); + ret = btrfs_clear_tree(trans, quota_root); + if (ret) { + btrfs_abort_transaction(trans, ret); + return ret; + } + + ret = btrfs_delete_and_free_root(trans, quota_root); + if (ret) { + btrfs_abort_transaction(trans, ret); + return ret; + } + fs_info->quota_root = NULL; + super_flags &= ~BTRFS_FEATURE_INCOMPAT_SIMPLE_QUOTA; + btrfs_set_super_incompat_flags(sb, super_flags); + btrfs_commit_transaction(trans, tree_root); + return 0; +} + +/* + * Given a pointer (ptr) into DATAi (i = slot), and an amount to shift, + * move all the data to the left (slots >= slot) of that ptr to the right by + * the shift amount. This overwrites the shift bytes after ptr, effectively + * removing them from the item data. We must update affected item sizes (only + * at slot) and offsets (slots >= slot). + * + * Leaf view, using '-' to show shift scale: + * Before: + * [ITEM0,...,ITEMi,...,ITEMn,-------,DATAn,...,[---DATAi---],...,DATA0] + * After: + * [ITEM0,...,ITEMi,...,ITEMn,--------,DATAn,...,[--DATAi---],...,DATA0] + * + * Zooming in on DATAi + * (ptr points at the start of the Ys, and shift is length of the Ys) + * Before: + * ...[DATAi+1][XXXXXXXXXXXXYYYYYYYYYYYYYYYYXXXXXXX][DATAi-1]... + * After: + * ...................[DATAi+1][XXXXXXXXXXXXXXXXXXX][DATAi-1]... + * Note that DATAi-1 and smaller are not affected. + */ +static void shift_leaf_data(struct btrfs_trans_handle *trans, + struct extent_buffer *leaf, int slot, + unsigned long ptr, u32 shift) +{ + u32 nr = btrfs_header_nritems(leaf); + u32 leaf_data_off = btrfs_item_ptr_offset(leaf, nr - 1); + u32 len = ptr - leaf_data_off; + u32 new_size = btrfs_item_size(leaf, slot) - shift; + for (int i = slot; i < nr; i++) { + u32 old_item_offset = btrfs_item_offset(leaf, i); + btrfs_set_item_offset(leaf, i, old_item_offset + shift); + } + memmove_extent_buffer(leaf, leaf_data_off + shift, leaf_data_off, len); + btrfs_set_item_size(leaf, slot, new_size); + btrfs_set_header_generation(leaf, trans->transid); + btrfs_mark_buffer_dirty(leaf); +} + +/* + * Iterate over the extent tree and for each EXTENT_DATA item that has an inline + * ref of type OWNER_REF, shift that leaf to eliminate the owner ref. + * + * Note: we use a search_slot per leaf rather than find_next_leaf to get the + * needed CoW-ing and rebalancing for each leaf and its path up to the root. + */ +static int remove_owner_refs(struct btrfs_fs_info *fs_info) +{ + struct btrfs_trans_handle *trans; + struct btrfs_root *extent_root; + struct btrfs_key key; + struct extent_buffer *leaf; + struct btrfs_path path = { 0 }; + int slot; + int ret; + + extent_root = btrfs_extent_root(fs_info, 0); + + trans = btrfs_start_transaction(extent_root, 0); + + key.objectid = 0; + key.type = BTRFS_EXTENT_ITEM_KEY; + key.offset = 0; + +search_slot: + ret = btrfs_search_slot(trans, extent_root, &key, &path, 1, 1); + if (ret < 0) + return ret; + leaf = path.nodes[0]; + slot = path.slots[0]; + + while (1) { + struct btrfs_key found_key; + struct btrfs_extent_item *ei; + struct btrfs_extent_inline_ref *iref; + u8 type; + unsigned long ptr; + unsigned long item_end; + + if (slot >= btrfs_header_nritems(leaf)) { + ret = btrfs_next_leaf(extent_root, &path); + if (ret < 0) { + break; + } else if (ret) { + ret = 0; + break; + } + leaf = path.nodes[0]; + slot = path.slots[0]; + btrfs_item_key_to_cpu(leaf, &key, slot); + btrfs_release_path(&path); + goto search_slot; + } + + btrfs_item_key_to_cpu(leaf, &found_key, slot); + if (found_key.type != BTRFS_EXTENT_ITEM_KEY) + goto next; + ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item); + ptr = (unsigned long)(ei + 1); + item_end = (unsigned long)ei + btrfs_item_size(leaf, slot); + /* No inline extent references; accessing type is invalid. */ + if (ptr > item_end) + goto next; + iref = (struct btrfs_extent_inline_ref *)ptr; + type = btrfs_extent_inline_ref_type(leaf, iref); + if (type == BTRFS_EXTENT_OWNER_REF_KEY) + shift_leaf_data(trans, leaf, slot, ptr, sizeof(*iref)); +next: + slot++; + } + btrfs_release_path(&path); + + ret = btrfs_commit_transaction(trans, extent_root); + if (ret < 0) { + errno = -ret; + error_msg(ERROR_MSG_COMMIT_TRANS, "%m"); + return ret; + } + return 0; +} + +int remove_squota(struct btrfs_fs_info *fs_info) +{ + int ret; + + ret = remove_owner_refs(fs_info); + if (ret) + return ret; + + return remove_quota_tree(fs_info); +} + static int create_qgroup(struct btrfs_fs_info *fs_info, struct btrfs_trans_handle *trans, u64 qgroupid) diff --git a/tune/tune.h b/tune/tune.h index 397cfe4f3..a41ba78b7 100644 --- a/tune/tune.h +++ b/tune/tune.h @@ -33,5 +33,6 @@ int convert_to_extent_tree(struct btrfs_fs_info *fs_info); int btrfs_change_csum_type(struct btrfs_fs_info *fs_info, u16 new_csum_type); int enable_quota(struct btrfs_fs_info *fs_info, bool simple); +int remove_squota(struct btrfs_fs_info *fs_info); #endif -- 2.45.2 ^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH 3/3] btrfs-progs: btrfstune: add ability to remove squotas 2024-07-11 21:18 ` [PATCH 3/3] btrfs-progs: btrfstune: add ability to remove squotas Boris Burkov @ 2024-07-11 22:43 ` Qu Wenruo 0 siblings, 0 replies; 9+ messages in thread From: Qu Wenruo @ 2024-07-11 22:43 UTC (permalink / raw) To: Boris Burkov, linux-btrfs, kernel-team 在 2024/7/12 06:48, Boris Burkov 写道: > When simple quotas is enabled, every new data extent gets a special > inline OWNER_REF item that identifies the owning subvolume. This makes > simple quotas backwards incompatible with kernels older than v6.7. Even > if you disable quotas on the filesystem, the OWNER_REF items are > sprinkled throughout the extent tree and older kernels are unable to > parse them. > > However, it is relatively easy to simply walk the extent tree and remove > these inline ref items. This gives squota adopters the option to *fully* > disable squotas on their system and un-set the incompat bit. Add this > capability to btrfstune, which requires only a little tricky btrfs item > data shifting. > > This functionality was tested with a new unit test, as well as a similar > but more thorough integration test in fstests > > Signed-off-by: Boris Burkov <boris@bur.io> > --- > .../065-btrfstune-simple-quota/test.sh | 33 ++++ > tune/main.c | 16 +- > tune/quota.c | 160 ++++++++++++++++++ > tune/tune.h | 1 + > 4 files changed, 209 insertions(+), 1 deletion(-) > create mode 100755 tests/misc-tests/065-btrfstune-simple-quota/test.sh > > diff --git a/tests/misc-tests/065-btrfstune-simple-quota/test.sh b/tests/misc-tests/065-btrfstune-simple-quota/test.sh > new file mode 100755 > index 000000000..d7ccaf4e9 > --- /dev/null > +++ b/tests/misc-tests/065-btrfstune-simple-quota/test.sh > @@ -0,0 +1,33 @@ > +#!/bin/bash > +# Verify btrfstune for enabling and removing simple quotas > + > +source "$TEST_TOP/common" || exit > +source "$TEST_TOP/common.convert" || exit > + > +check_experimental_build > +setup_root_helper > +prepare_test_dev > + > +# Create the fs without simple quota > +run_check_mkfs_test_dev > +run_check_mount_test_dev > +populate_fs > +run_check_umount_test_dev > +# Enable simple quotas > +run_check $SUDO_HELPER "$TOP/btrfstune" --enable-simple-quota "$TEST_DEV" > +run_check_mount_test_dev > +run_check $SUDO_HELPER dd if=/dev/zero of="$TEST_MNT"/file2 bs=1M count=1 > +run_check_umount_test_dev > +run_check $SUDO_HELPER "$TOP/btrfs" check "$TEST_DEV" > + > +# Populate new fs with simple quotas enabled > +run_check_mkfs_test_dev -O squota > +run_check_mount_test_dev > +populate_fs > +run_check_umount_test_dev > +# Remove simple quotas > +run_check $SUDO_HELPER "$TOP/btrfstune" --remove-simple-quota "$TEST_DEV" > +run_check_mount_test_dev > +run_check $SUDO_HELPER dd if=/dev/zero of="$TEST_MNT"/file3 bs=1M count=1 > +run_check_umount_test_dev > +run_check $SUDO_HELPER "$TOP/btrfs" check "$TEST_DEV" > diff --git a/tune/main.c b/tune/main.c > index cb93d2cb3..6ef8bbe2d 100644 > --- a/tune/main.c > +++ b/tune/main.c > @@ -104,6 +104,7 @@ static const char * const tune_usage[] = { > OPTLINE("-n", "enable no-holes feature (mkfs: no-holes, more efficient sparse file representation)"), > OPTLINE("-S <0|1>", "set/unset seeding status of a device"), > OPTLINE("--enable-simple-quota", "enable simple quotas on the file system. (mkfs: squota)"), > + OPTLINE("--remove-simple-quota", "remove simple quotas from the file system."), > OPTLINE("--convert-to-block-group-tree", "convert filesystem to track block groups in " > "the separate block-group-tree instead of extent tree (sets the incompat bit)"), > OPTLINE("--convert-from-block-group-tree", > @@ -198,6 +199,7 @@ int BOX_MAIN(btrfstune)(int argc, char *argv[]) > int ret; > u64 super_flags = 0; > int quota = 0; > + int remove_simple_quota = 0; > int fd = -1; > int oflags = O_RDWR; > > @@ -209,7 +211,7 @@ int BOX_MAIN(btrfstune)(int argc, char *argv[]) > GETOPT_VAL_DISABLE_BLOCK_GROUP_TREE, > GETOPT_VAL_ENABLE_FREE_SPACE_TREE, > GETOPT_VAL_ENABLE_SIMPLE_QUOTA, > - > + GETOPT_VAL_REMOVE_SIMPLE_QUOTA, > }; > static const struct option long_options[] = { > { "help", no_argument, NULL, GETOPT_VAL_HELP}, > @@ -221,6 +223,8 @@ int BOX_MAIN(btrfstune)(int argc, char *argv[]) > GETOPT_VAL_ENABLE_FREE_SPACE_TREE}, > { "enable-simple-quota", no_argument, NULL, > GETOPT_VAL_ENABLE_SIMPLE_QUOTA }, > + { "remove-simple-quota", no_argument, NULL, > + GETOPT_VAL_REMOVE_SIMPLE_QUOTA}, > #if EXPERIMENTAL > { "csum", required_argument, NULL, GETOPT_VAL_CSUM }, > #endif > @@ -288,6 +292,10 @@ int BOX_MAIN(btrfstune)(int argc, char *argv[]) > quota = 1; > btrfstune_cmd_groups[QGROUP] = true; > break; > + case GETOPT_VAL_REMOVE_SIMPLE_QUOTA: > + remove_simple_quota = 1; > + btrfstune_cmd_groups[QGROUP] = true; > + break; > #if EXPERIMENTAL > case GETOPT_VAL_CSUM: > btrfs_warn_experimental( > @@ -535,6 +543,12 @@ int BOX_MAIN(btrfstune)(int argc, char *argv[]) > goto out; > } > > + if (remove_simple_quota) { > + ret = remove_squota(root->fs_info); > + if (ret) > + goto out; > + } > + > out: > if (ret < 0) { > fs_info->readonly = 1; > diff --git a/tune/quota.c b/tune/quota.c > index a14f45307..16b2b3fb6 100644 > --- a/tune/quota.c > +++ b/tune/quota.c > @@ -6,6 +6,166 @@ > #include "common/messages.h" > #include "tune/tune.h" > > +static int remove_quota_tree(struct btrfs_fs_info *fs_info) > +{ > + int ret; > + struct btrfs_root *quota_root = fs_info->quota_root; > + struct btrfs_root *tree_root = fs_info->tree_root; > + struct btrfs_super_block *sb = fs_info->super_copy; > + int super_flags = btrfs_super_incompat_flags(sb); > + struct btrfs_trans_handle *trans; > + > + trans = btrfs_start_transaction(quota_root, 0); > + ret = btrfs_clear_tree(trans, quota_root); > + if (ret) { > + btrfs_abort_transaction(trans, ret); > + return ret; > + } > + > + ret = btrfs_delete_and_free_root(trans, quota_root); > + if (ret) { > + btrfs_abort_transaction(trans, ret); > + return ret; > + } > + fs_info->quota_root = NULL; > + super_flags &= ~BTRFS_FEATURE_INCOMPAT_SIMPLE_QUOTA; > + btrfs_set_super_incompat_flags(sb, super_flags); > + btrfs_commit_transaction(trans, tree_root); > + return 0; > +} > + > +/* > + * Given a pointer (ptr) into DATAi (i = slot), and an amount to shift, > + * move all the data to the left (slots >= slot) of that ptr to the right by > + * the shift amount. This overwrites the shift bytes after ptr, effectively > + * removing them from the item data. We must update affected item sizes (only > + * at slot) and offsets (slots >= slot). > + * > + * Leaf view, using '-' to show shift scale: > + * Before: > + * [ITEM0,...,ITEMi,...,ITEMn,-------,DATAn,...,[---DATAi---],...,DATA0] > + * After: > + * [ITEM0,...,ITEMi,...,ITEMn,--------,DATAn,...,[--DATAi---],...,DATA0] > + * > + * Zooming in on DATAi > + * (ptr points at the start of the Ys, and shift is length of the Ys) > + * Before: > + * ...[DATAi+1][XXXXXXXXXXXXYYYYYYYYYYYYYYYYXXXXXXX][DATAi-1]... > + * After: > + * ...................[DATAi+1][XXXXXXXXXXXXXXXXXXX][DATAi-1]... > + * Note that DATAi-1 and smaller are not affected. > + */ > +static void shift_leaf_data(struct btrfs_trans_handle *trans, > + struct extent_buffer *leaf, int slot, > + unsigned long ptr, u32 shift) > +{ > + u32 nr = btrfs_header_nritems(leaf); > + u32 leaf_data_off = btrfs_item_ptr_offset(leaf, nr - 1); > + u32 len = ptr - leaf_data_off; > + u32 new_size = btrfs_item_size(leaf, slot) - shift; > + for (int i = slot; i < nr; i++) { > + u32 old_item_offset = btrfs_item_offset(leaf, i); > + btrfs_set_item_offset(leaf, i, old_item_offset + shift); > + } IIRC you can just handle the memmove inside the item, then let btrfs_truncate_item() to do all the remaining work. Although I'm not sure if it's really going to save any code. Otherwise looks good to me. Reviewed-by: Qu Wenruo <wqu@suse.com> Thanks, Qu > + memmove_extent_buffer(leaf, leaf_data_off + shift, leaf_data_off, len); > + btrfs_set_item_size(leaf, slot, new_size); > + btrfs_set_header_generation(leaf, trans->transid); > + btrfs_mark_buffer_dirty(leaf); > +} > + > +/* > + * Iterate over the extent tree and for each EXTENT_DATA item that has an inline > + * ref of type OWNER_REF, shift that leaf to eliminate the owner ref. > + * > + * Note: we use a search_slot per leaf rather than find_next_leaf to get the > + * needed CoW-ing and rebalancing for each leaf and its path up to the root. > + */ > +static int remove_owner_refs(struct btrfs_fs_info *fs_info) > +{ > + struct btrfs_trans_handle *trans; > + struct btrfs_root *extent_root; > + struct btrfs_key key; > + struct extent_buffer *leaf; > + struct btrfs_path path = { 0 }; > + int slot; > + int ret; > + > + extent_root = btrfs_extent_root(fs_info, 0); > + > + trans = btrfs_start_transaction(extent_root, 0); > + > + key.objectid = 0; > + key.type = BTRFS_EXTENT_ITEM_KEY; > + key.offset = 0; > + > +search_slot: > + ret = btrfs_search_slot(trans, extent_root, &key, &path, 1, 1); > + if (ret < 0) > + return ret; > + leaf = path.nodes[0]; > + slot = path.slots[0]; > + > + while (1) { > + struct btrfs_key found_key; > + struct btrfs_extent_item *ei; > + struct btrfs_extent_inline_ref *iref; > + u8 type; > + unsigned long ptr; > + unsigned long item_end; > + > + if (slot >= btrfs_header_nritems(leaf)) { > + ret = btrfs_next_leaf(extent_root, &path); > + if (ret < 0) { > + break; > + } else if (ret) { > + ret = 0; > + break; > + } > + leaf = path.nodes[0]; > + slot = path.slots[0]; > + btrfs_item_key_to_cpu(leaf, &key, slot); > + btrfs_release_path(&path); > + goto search_slot; > + } > + > + btrfs_item_key_to_cpu(leaf, &found_key, slot); > + if (found_key.type != BTRFS_EXTENT_ITEM_KEY) > + goto next; > + ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item); > + ptr = (unsigned long)(ei + 1); > + item_end = (unsigned long)ei + btrfs_item_size(leaf, slot); > + /* No inline extent references; accessing type is invalid. */ > + if (ptr > item_end) > + goto next; > + iref = (struct btrfs_extent_inline_ref *)ptr; > + type = btrfs_extent_inline_ref_type(leaf, iref); > + if (type == BTRFS_EXTENT_OWNER_REF_KEY) > + shift_leaf_data(trans, leaf, slot, ptr, sizeof(*iref)); > +next: > + slot++; > + } > + btrfs_release_path(&path); > + > + ret = btrfs_commit_transaction(trans, extent_root); > + if (ret < 0) { > + errno = -ret; > + error_msg(ERROR_MSG_COMMIT_TRANS, "%m"); > + return ret; > + } > + return 0; > +} > + > +int remove_squota(struct btrfs_fs_info *fs_info) > +{ > + int ret; > + > + ret = remove_owner_refs(fs_info); > + if (ret) > + return ret; > + > + return remove_quota_tree(fs_info); > +} > + > static int create_qgroup(struct btrfs_fs_info *fs_info, > struct btrfs_trans_handle *trans, > u64 qgroupid) > diff --git a/tune/tune.h b/tune/tune.h > index 397cfe4f3..a41ba78b7 100644 > --- a/tune/tune.h > +++ b/tune/tune.h > @@ -33,5 +33,6 @@ int convert_to_extent_tree(struct btrfs_fs_info *fs_info); > int btrfs_change_csum_type(struct btrfs_fs_info *fs_info, u16 new_csum_type); > > int enable_quota(struct btrfs_fs_info *fs_info, bool simple); > +int remove_squota(struct btrfs_fs_info *fs_info); > > #endif ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH 0/3] btrfs-progs: btrfstune --remove-simple-quota 2024-07-11 21:18 [PATCH 0/3] btrfs-progs: btrfstune --remove-simple-quota Boris Burkov ` (2 preceding siblings ...) 2024-07-11 21:18 ` [PATCH 3/3] btrfs-progs: btrfstune: add ability to remove squotas Boris Burkov @ 2025-01-04 10:35 ` Anand Jain 2025-01-05 23:17 ` Qu Wenruo 3 siblings, 1 reply; 9+ messages in thread From: Anand Jain @ 2025-01-04 10:35 UTC (permalink / raw) To: linux-btrfs, David Sterba, Boris Burkov; +Cc: kernel-team btrfstune --help shows -q as the option to enable simple quota, which does not work. David, has this set missed integration? OR Boris could push? The whole series looks good to me. Reviewed-by: Anand Jain <anand.jain@oracle.com> Thx. On 12/7/24 02:48, Boris Burkov wrote: > To be able to nuke simple quotas entirely if you decide you don't want > them (and especially the OWNER_REFs) in your filesystem after all. > > If you run > btrfstune --remove-simple-quota <dev> > on an unmounted filesystem, it will be as if simple quotas never existed > on that filesystem. > > Boris Burkov (3): > btrfs-progs: add a helper for clearing all the items in a tree > btrfs-progs: btrfstune: fix documentation for --enable-simple-quota > btrfs-progs: btrfstune: add ability to remove squotas > > kernel-shared/disk-io.c | 39 +++++ > kernel-shared/disk-io.h | 2 + > kernel-shared/free-space-tree.c | 42 +---- > .../065-btrfstune-simple-quota/test.sh | 33 ++++ > tune/main.c | 18 +- > tune/quota.c | 160 ++++++++++++++++++ > tune/tune.h | 1 + > 7 files changed, 253 insertions(+), 42 deletions(-) > create mode 100755 tests/misc-tests/065-btrfstune-simple-quota/test.sh > ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH 0/3] btrfs-progs: btrfstune --remove-simple-quota 2025-01-04 10:35 ` [PATCH 0/3] btrfs-progs: btrfstune --remove-simple-quota Anand Jain @ 2025-01-05 23:17 ` Qu Wenruo 0 siblings, 0 replies; 9+ messages in thread From: Qu Wenruo @ 2025-01-05 23:17 UTC (permalink / raw) To: Anand Jain, linux-btrfs, David Sterba, Boris Burkov; +Cc: kernel-team Sorry I forgot to merge them for so long.. Now merged into devel branch with all reviewed-by tags, and some small modifications: - Put the doc fix first - Add the missing man page entry for "--enable-simple-quota" - Make remove_all_tree_items() to use btrfs_clear_tree() helper - Add the missing man page entry for "--remove-simple-quota" Thanks, Qu 在 2025/1/4 21:05, Anand Jain 写道: > > btrfstune --help shows -q as the option to enable simple quota, which > does not work. > > David, has this set missed integration? OR Boris could push? > > The whole series looks good to me. > > Reviewed-by: Anand Jain <anand.jain@oracle.com> > > Thx. > > > On 12/7/24 02:48, Boris Burkov wrote: >> To be able to nuke simple quotas entirely if you decide you don't want >> them (and especially the OWNER_REFs) in your filesystem after all. >> >> If you run >> btrfstune --remove-simple-quota <dev> >> on an unmounted filesystem, it will be as if simple quotas never existed >> on that filesystem. >> >> Boris Burkov (3): >> btrfs-progs: add a helper for clearing all the items in a tree >> btrfs-progs: btrfstune: fix documentation for --enable-simple-quota >> btrfs-progs: btrfstune: add ability to remove squotas >> >> kernel-shared/disk-io.c | 39 +++++ >> kernel-shared/disk-io.h | 2 + >> kernel-shared/free-space-tree.c | 42 +---- >> .../065-btrfstune-simple-quota/test.sh | 33 ++++ >> tune/main.c | 18 +- >> tune/quota.c | 160 ++++++++++++++++++ >> tune/tune.h | 1 + >> 7 files changed, 253 insertions(+), 42 deletions(-) >> create mode 100755 tests/misc-tests/065-btrfstune-simple-quota/test.sh >> > > ^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2025-01-05 23:18 UTC | newest] Thread overview: 9+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2024-07-11 21:18 [PATCH 0/3] btrfs-progs: btrfstune --remove-simple-quota Boris Burkov 2024-07-11 21:18 ` [PATCH 1/3] btrfs-progs: add a helper for clearing all the items in a tree Boris Burkov 2024-07-11 22:32 ` Qu Wenruo 2024-07-11 21:18 ` [PATCH 2/3] btrfs-progs: btrfstune: fix documentation for --enable-simple-quota Boris Burkov 2024-07-11 22:34 ` Qu Wenruo 2024-07-11 21:18 ` [PATCH 3/3] btrfs-progs: btrfstune: add ability to remove squotas Boris Burkov 2024-07-11 22:43 ` Qu Wenruo 2025-01-04 10:35 ` [PATCH 0/3] btrfs-progs: btrfstune --remove-simple-quota Anand Jain 2025-01-05 23:17 ` Qu Wenruo
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox