Skip to contentSkip to conversation

btrfs-progs: add lone extent defragging support

PATCH New Qu Wenruo 1 participant 3 messages Feb 5, 2024

Subsystems (2)
lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

cover btrfs-progs: add lone extent defragging support

This the progs support for the new kernel defrag parameters on lone file extents.

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

The ratio is between [0, 65536], 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 end user interface is not yet deteremined, especially for the ratio (RATIO/65536 is too trikcy for most end users).
But it is already enough to address the lone trimed file extent case mentioned in the kernel patchset.

Qu Wenruo (2):
btrfs-progs: defrag: sync the new lone extent fine-tunning from kernel
btrfs-progs: cmds/filesystem: fine-tune lone file extents defragging

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

--
2.43.0

author
#1 lore raw

PATCH 1/2 btrfs-progs: defrag: sync the new lone extent fine-tunning from kernel

This would add the long extent fine-tunning support for defrag.

---
 kernel-shared/uapi/btrfs.h | 26 +++++++++++++++++++++++---
 1 file changed, 23 insertions(+), 3 deletions(-)
kernel-shared/uapi/btrfs.h +23 -3
index 34e84b89edf2..7fea5f1bcbe8 100644
@@ -642,8 +642,14 @@ _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_LONE_RATIO		(1ULL << 2)
+#define BTRFS_DEFRAG_RANGE_LONE_WASTED_BYTES	(1ULL << 3)
+#define BTRFS_DEFRAG_RANGE_FLAGS_SUPP  (BTRFS_DEFRAG_RANGE_COMPRESS |  \
+					BTRFS_DEFRAG_RANGE_START_IO |  \
+					BTRFS_DEFRAG_RANGE_LONE_RATIO |\
+					BTRFS_DEFRAG_RANGE_LONE_WASTED_BYTES)
 struct btrfs_ioctl_defrag_range_args {
 	/* start of the defrag operation */
 	__u64 start;
@@ -671,8 +677,22 @@ struct btrfs_ioctl_defrag_range_args {
 	 */
 	__u32 compress_type;
 
+	/*
+	 * If a lone extent (has no adjacent file extent) is utilizing less
+	 * than the ratio (0 means skip this check, while 65536 means 100%) of
+	 * the on-disk space, it would be considered as a defrag target.
+	 */
+	__u32 lone_ratio;
+
+	/*
+	 * If we defrag a lone extent (has no adjacent file extent) can free
+	 * up more space than this value (in bytes), it would be considered
+	 * as a defrag target.
+	 */
+	__u32 lone_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.43.0
author
#2 lore raw

PATCH 2/2 btrfs-progs: cmds/filesystem: fine-tune lone file extents defragging

In reply to Qu Wenruo

This allows defrag to fine-tune long file extents (which have no adjacent file extents) with new "--lone-ratio" and "--lone-wasted-bytes" options.

The default behavior is, defrag would go with 4096 as the ratio (1/16), and 16M as the wasted bytes on the first try.

If the kernel doesn't support it, then retry without lone file extents handling.

---
 Documentation/btrfs-filesystem.rst | 18 ++++++++++
 cmds/filesystem.c                  | 53 ++++++++++++++++++++++++++++--
 2 files changed, 68 insertions(+), 3 deletions(-)
Documentation/btrfs-filesystem.rst +18 -0
index 54069ce444f6..f5cd34e9062f 100644
@@ -144,6 +144,24 @@ 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.
 
+	--lone-ratio <ratio>
+		For a lone file extent (which has no adjacent file exctent), if its utilizing
+		less than (ratio / 65536) 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, 65536].
+
+	--lone-wasted-bytes <bytes>[kKmMgGgtTpPeE]
+		For a lone file extent (which has no adjacent file exctent), if defragging
+		the file extent can free up more than <bytes> (excluding the file extent
+		size), the file extent would be defragged.
+
+		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.43.0