Skip to contentSkip to conversation

btrfs-progs: cmds/filesystem: add --usage-ratio and --wasted-bytes options

PATCH New Qu Wenruo 1 participant 3 messages Mar 12, 2024

Subsystems (2)
lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

v2 cover btrfs-progs: cmds/filesystem: add --usage-ratio and --wasted-bytes options

[CHANGELOG]
v2:
- Sync the newer kernel uapi
- Remove the "lone" mentions
Now the new options would be "--usage-ratio" and "--wasted-bytes".

This the progs support for the new kernel defrag parameters.

This adds 2 new fine tunning parameters, --usage-ratio and --wasted-bytes.

The ratio is between [0, 100] (aka, percentage value), and wasted bytes is between [0, U32_MAX], but in reality the value only makes sense below max file extent size (for both compressed and regular extents). Any value higher than max file extent size would mostly disable the wasted bytes check (as it would always be false).

The default usage ratio is 5%, and 16MiB wasted bytes.
If the kernel doesn't support the new options, it would fall back to the old ioctl flags without the 2 new flags.

Qu Wenruo (2):
btrfs-progs: defrag: sync the usage ratio/wasted bytes fine-tunning
from kernel
btrfs-progs: cmds/filesystem: add --usage-ratio and --wasted-bytes for
defrag

 Documentation/btrfs-filesystem.rst | 21 ++++++++++++
 cmds/filesystem.c                  | 53 ++++++++++++++++++++++++++++--
 kernel-shared/uapi/btrfs.h         | 39 ++++++++++++++++++++--
 3 files changed, 107 insertions(+), 6 deletions(-)

--
2.44.0

author
#1 lore raw

PATCH v2 1/2 btrfs-progs: defrag: sync the usage ratio/wasted bytes fine-tunning from kernel

This would add the defrag usage ratio and wasted bytes fine-tunning support for defrag.

---
 kernel-shared/uapi/btrfs.h | 39 +++++++++++++++++++++++++++++++++++---
 1 file changed, 36 insertions(+), 3 deletions(-)
kernel-shared/uapi/btrfs.h +36 -3
index 34e84b89edf2..b595a8deb0be 100644
@@ -642,8 +642,15 @@ _static_assert(sizeof(struct btrfs_ioctl_clone_range_args) == 32);
  * Used by:
  * struct btrfs_ioctl_defrag_range_args.flags
  */
-#define BTRFS_DEFRAG_RANGE_COMPRESS 1
-#define BTRFS_DEFRAG_RANGE_START_IO 2
+#define BTRFS_DEFRAG_RANGE_COMPRESS		(1ULL << 0)
+#define BTRFS_DEFRAG_RANGE_START_IO		(1ULL << 1)
+#define BTRFS_DEFRAG_RANGE_USAGE_RATIO		(1ULL << 2)
+#define BTRFS_DEFRAG_RANGE_WASTED_BYTES		(1ULL << 3)
+#define BTRFS_DEFRAG_RANGE_FLAGS_SUPP	(BTRFS_DEFRAG_RANGE_COMPRESS |	\
+					 BTRFS_DEFRAG_RANGE_START_IO |	\
+					 BTRFS_DEFRAG_RANGE_USAGE_RATIO |\
+					 BTRFS_DEFRAG_RANGE_WASTED_BYTES)
+
 struct btrfs_ioctl_defrag_range_args {
 	/* start of the defrag operation */
 	__u64 start;
@@ -671,8 +678,34 @@ struct btrfs_ioctl_defrag_range_args {
 	 */
 	__u32 compress_type;
 
+	/*
+	 * File extents which has lower usage ratio than this would be defragged.
+	 *
+	 * Valid values are [0, 100].
+	 *
+	 * 0 means no check based on usage ratio.
+	 * 1 means one file extent would be defragged if its referred size
+	 * (file extent num bytes) is smaller than 1% of its on-disk extent size.
+	 * 100 means one file extent would be defragged if its referred size
+	 * (file extent num bytes) is smaller than 100% of its on-disk extent size.
+	 */
+	__u32 usage_ratio;
+
+	/*
+	 * File extents which has more "wasted" bytes than this would be
+	 * defragged.
+	 *
+	 * "Wasted" bytes just means the difference between the file extent size
+	 * (file extent num bytes) against the on-disk extent size
+	 * (file extent disk num bytes).
+	 *
+	 * Valid values are [0, U32_MAX], but values larger than
+	 * BTRFS_MAX_EXTENT_SIZE would not make much sense.
+	 */
+	__u32 wasted_bytes;
+
 	/* spare for later */
-	__u32 unused[4];
+	__u32 unused[2];
 };
 _static_assert(sizeof(struct btrfs_ioctl_defrag_range_args) == 48);
 
Signature · 1 line
2.44.0
author
#2 lore raw

PATCH v2 2/2 btrfs-progs: cmds/filesystem: add --usage-ratio and --wasted-bytes for defrag

In reply to Qu Wenruo

This allows defrag subcommand to accept new "--usage-ratio" and "--wasted-bytes" options.

These two options allows defrag to handle extents which have no adjacent mergable extents but has low usage ratio or high wasted bytes against its on-disk extent.

The default behavior is, defrag would go with 5% as the usage_ratio and 16M as the wasted bytes on the first try.

If the kernel doesn't support it, then retry without the fine tunning flags.

---
 Documentation/btrfs-filesystem.rst | 21 ++++++++++++
 cmds/filesystem.c                  | 53 ++++++++++++++++++++++++++++--
 2 files changed, 71 insertions(+), 3 deletions(-)
Documentation/btrfs-filesystem.rst +21 -0
index 83207a5a263e..213be6ff4a14 100644
@@ -144,6 +144,27 @@ defragment [options] <file>|<dir> [<file>|<dir>...]
                 The range is default (the whole file) or given by *-s* and *-l*, split into
                 the steps or done in one go if the step is larger. Minimum range size is 256KiB.
 
+	--usage-ratio <ratio>
+		If a file extent which is not a defrag target (e.g. no mergeable
+		adjacent extent), but has a usage ratio lower than (ratio / 100)
+		of the on-disk extent size, it would also be defraged
+		for its potential to free up the on-disk extent.
+
+		Valid values are in the range [0, 100].
+
+	--wasted-bytes <bytes>[kKmMgGgtTpPeE]
+		If a file extent which is not a defrag target (e.g. no mergeable
+		adjacent extent), but has more "wasted" bytes (the difference
+		between the file extent and the on-disk extent size) than this
+		value, it would also be defragged for its potential to free up
+		on-disk extent.
+
+		Valid values are in the range [0, U32_MAX], but any value larger than 128K
+		would make no sense for compressed file extents, and any value larger than
+		128M would make no sense for regular file extents.
+		As the largest file extent supported is 128K for compressed extent and 128M
+		otherwise, thus it's impossible to free up more bytes than those limits.
+
         -v
                 (deprecated) alias for global *-v* option
 
cmds/filesystem.c +50 -3

109 lines not on this page · raw message

Signature · 1 line
2.44.0