Skip to contentSkip to conversation

Ioctl to clear unused space in various ways

PATCH Superseded David Sterba 5 participants 14 messages Feb 28, 2025 – Mar 11, 2025 Btrfs file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

David Sterba

author maintainer
#0 lore raw

cover Ioctl to clear unused space in various ways

Add ioctl that is similar to FITRIM and in addition to trim can do also zeroing (either plain overwrite, or unmap the blocks if the device supports it) and secure erase.

This can be used to zero the unused space in e.g. VM images (when run from inside the guest, if fstrim is not supported) or free space on thin-provisioned devices.

The secure erase is provided by blkdiscard command but I'm not aware of equivalent that can be run on a filesystem, so this is for parity.

David Sterba (5):
btrfs: extend trim callchains to pass the operation type
btrfs: add new ioctl CLEAR_FREE
btrfs: add zeroout mode to CLEAR_FREE ioctl
btrfs: add secure erase mode to CLEAR_FREE ioctl
btrfs: add more zeroout modes to CLEAR_FREE ioctl

 fs/btrfs/discard.c          |   4 +-
 fs/btrfs/extent-tree.c      | 159 +++++++++++++++++++++++++++++++-----
 fs/btrfs/extent-tree.h      |   5 +-
 fs/btrfs/free-space-cache.c |  29 ++++---
 fs/btrfs/free-space-cache.h |   8 +-
 fs/btrfs/inode.c            |   2 +-
 fs/btrfs/ioctl.c            |  42 ++++++++++
 fs/btrfs/volumes.c          |   3 +-
 include/uapi/linux/btrfs.h  |  46 +++++++++++
 9 files changed, 258 insertions(+), 40 deletions(-)
Signature · 1 line
2.47.1
author maintainer
#1 lore raw

PATCH 1/5 btrfs: extend trim callchains to pass the operation type

Preparatory work for more than trim/discard operation that can be performed on the unused space from an ioctl. As FITRIM is not extensible, we'll need a new one. Now we extend any caller that takes part in the trim/discard to take one parameter defining the type of operation.

The operation multiplexer btrfs_issue_clear_op() will be extended in followup patches.

---
 fs/btrfs/discard.c          |  4 +--
 fs/btrfs/extent-tree.c      | 51 +++++++++++++++++++++++--------------
 fs/btrfs/extent-tree.h      |  3 ++-
 fs/btrfs/free-space-cache.c | 29 +++++++++++----------
 fs/btrfs/free-space-cache.h |  8 +++---
 fs/btrfs/inode.c            |  2 +-
 fs/btrfs/volumes.c          |  3 ++-
 include/uapi/linux/btrfs.h  |  8 ++++++
 8 files changed, 68 insertions(+), 40 deletions(-)
fs/btrfs/discard.c +2 -2
index e815d165cccc..cd7220465f1f 100644
@@ -518,13 +518,13 @@ static void btrfs_discard_workfn(struct work_struct *work)
 		btrfs_trim_block_group_bitmaps(block_group, &trimmed,
 				       block_group->discard_cursor,
 				       btrfs_block_group_end(block_group),
-				       minlen, maxlen, true);
+				       minlen, maxlen, true, BTRFS_CLEAR_OP_DISCARD);
 		discard_ctl->discard_bitmap_bytes += trimmed;
 	} else {
 		btrfs_trim_block_group_extents(block_group, &trimmed,
 				       block_group->discard_cursor,
 				       btrfs_block_group_end(block_group),
-				       minlen, true);
+				       minlen, true, BTRFS_CLEAR_OP_DISCARD);
 		discard_ctl->discard_extent_bytes += trimmed;
 	}
 
fs/btrfs/extent-tree.c +32 -19

131 lines not on this page · raw message

fs/btrfs/extent-tree.h +2 -1
index 0ed682d9ed7b..c8e1a30309ab 100644
@@ -163,7 +163,8 @@ int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
 			struct extent_buffer *parent);
 void btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info, u64 start, u64 end);
 int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
-			 u64 num_bytes, u64 *actual_bytes);
+			 u64 num_bytes, u64 *actual_bytes,
+			 enum btrfs_clear_op_type clear);
 int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range);
 
 #endif
fs/btrfs/free-space-cache.c +16 -13
index 3095cce904b5..161fdcab07e4 100644
@@ -3652,7 +3652,8 @@ static int do_trimming(struct btrfs_block_group *block_group,
 		       u64 *total_trimmed, u64 start, u64 bytes,
 		       u64 reserved_start, u64 reserved_bytes,
 		       enum btrfs_trim_state reserved_trim_state,
-		       struct btrfs_trim_range *trim_entry)
+		       struct btrfs_trim_range *trim_entry,
+		       enum btrfs_clear_op_type clear)
 {
 	struct btrfs_space_info *space_info = block_group->space_info;
 	struct btrfs_fs_info *fs_info = block_group->fs_info;
@@ -3674,7 +3675,7 @@ static int do_trimming(struct btrfs_block_group *block_group,
 	spin_unlock(&block_group->lock);
 	spin_unlock(&space_info->lock);
 
-	ret = btrfs_discard_extent(fs_info, start, bytes, &trimmed);
+	ret = btrfs_discard_extent(fs_info, start, bytes, &trimmed, clear);
 	if (!ret) {
 		*total_trimmed += trimmed;
 		trim_state = BTRFS_TRIM_STATE_TRIMMED;
@@ -3711,7 +3712,7 @@ static int do_trimming(struct btrfs_block_group *block_group,
  */
 static int trim_no_bitmap(struct btrfs_block_group *block_group,
 			  u64 *total_trimmed, u64 start, u64 end, u64 minlen,
-			  bool async)
+			  bool async, enum btrfs_clear_op_type clear)
 {
 	struct btrfs_discard_ctl *discard_ctl =
 					&block_group->fs_info->discard_ctl;
@@ -3800,7 +3801,7 @@ static int trim_no_bitmap(struct btrfs_block_group *block_group,
 
 		ret = do_trimming(block_group, total_trimmed, start, bytes,
 				  extent_start, extent_bytes, extent_trim_state,
-				  &trim_entry);
+				  &trim_entry, clear);
 		if (ret) {
 			block_group->discard_cursor = start + bytes;
 			break;
@@ -3877,7 +3878,7 @@ static void end_trimming_bitmap(struct btrfs_free_space_ctl *ctl,
  */
 static int trim_bitmaps(struct btrfs_block_group *block_group,
 			u64 *total_trimmed, u64 start, u64 end, u64 minlen,
-			u64 maxlen, bool async)
+			u64 maxlen, bool async, enum btrfs_clear_op_type clear)
 {
 	struct btrfs_discard_ctl *discard_ctl =
 					&block_group->fs_info->discard_ctl;
@@ -3986,7 +3987,7 @@ static int trim_bitmaps(struct btrfs_block_group *block_group,
 		mutex_unlock(&ctl->cache_writeout_mutex);
 
 		ret = do_trimming(block_group, total_trimmed, start, bytes,
-				  start, bytes, 0, &trim_entry);
+				  start, bytes, 0, &trim_entry, clear);
 		if (ret) {
 			reset_trimming_bitmap(ctl, offset);
 			block_group->discard_cursor =
@@ -4020,7 +4021,8 @@ static int trim_bitmaps(struct btrfs_block_group *block_group,
 }
 
 int btrfs_trim_block_group(struct btrfs_block_group *block_group,
-			   u64 *trimmed, u64 start, u64 end, u64 minlen)
+			   u64 *trimmed, u64 start, u64 end, u64 minlen,
+			   enum btrfs_clear_op_type clear)
 {
 	struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
 	int ret;
@@ -4038,11 +4040,11 @@ int btrfs_trim_block_group(struct btrfs_block_group *block_group,
 	btrfs_freeze_block_group(block_group);
 	spin_unlock(&block_group->lock);
 
-	ret = trim_no_bitmap(block_group, trimmed, start, end, minlen, false);
+	ret = trim_no_bitmap(block_group, trimmed, start, end, minlen, false, clear);
 	if (ret)
 		goto out;
 
-	ret = trim_bitmaps(block_group, trimmed, start, end, minlen, 0, false);
+	ret = trim_bitmaps(block_group, trimmed, start, end, minlen, 0, false, clear);
 	div64_u64_rem(end, BITS_PER_BITMAP * ctl->unit, &rem);
 	/* If we ended in the middle of a bitmap, reset the trimming flag */
 	if (rem)
@@ -4054,7 +4056,7 @@ int btrfs_trim_block_group(struct btrfs_block_group *block_group,
 
 int btrfs_trim_block_group_extents(struct btrfs_block_group *block_group,
 				   u64 *trimmed, u64 start, u64 end, u64 minlen,
-				   bool async)
+				   bool async, enum btrfs_clear_op_type clear)
 {
 	int ret;
 
@@ -4068,7 +4070,7 @@ int btrfs_trim_block_group_extents(struct btrfs_block_group *block_group,
 	btrfs_freeze_block_group(block_group);
 	spin_unlock(&block_group->lock);
 
-	ret = trim_no_bitmap(block_group, trimmed, start, end, minlen, async);
+	ret = trim_no_bitmap(block_group, trimmed, start, end, minlen, async, clear);
 	btrfs_unfreeze_block_group(block_group);
 
 	return ret;
@@ -4076,7 +4078,8 @@ int btrfs_trim_block_group_extents(struct btrfs_block_group *block_group,
 
 int btrfs_trim_block_group_bitmaps(struct btrfs_block_group *block_group,
 				   u64 *trimmed, u64 start, u64 end, u64 minlen,
-				   u64 maxlen, bool async)
+				   u64 maxlen, bool async,
+				   enum btrfs_clear_op_type clear)
 {
 	int ret;
 
@@ -4091,7 +4094,7 @@ int btrfs_trim_block_group_bitmaps(struct btrfs_block_group *block_group,
 	spin_unlock(&block_group->lock);
 
 	ret = trim_bitmaps(block_group, trimmed, start, end, minlen, maxlen,
-			   async);
+			   async, clear);
 
 	btrfs_unfreeze_block_group(block_group);
 
fs/btrfs/free-space-cache.h +5 -3
index 9f1dbfdee8ca..c4c2e5571355 100644
@@ -159,13 +159,15 @@ void btrfs_return_cluster_to_free_space(
 			       struct btrfs_block_group *block_group,
 			       struct btrfs_free_cluster *cluster);
 int btrfs_trim_block_group(struct btrfs_block_group *block_group,
-			   u64 *trimmed, u64 start, u64 end, u64 minlen);
+			   u64 *trimmed, u64 start, u64 end, u64 minlen,
+			   enum btrfs_clear_op_type clear);
 int btrfs_trim_block_group_extents(struct btrfs_block_group *block_group,
 				   u64 *trimmed, u64 start, u64 end, u64 minlen,
-				   bool async);
+				   bool async, enum btrfs_clear_op_type clear);
 int btrfs_trim_block_group_bitmaps(struct btrfs_block_group *block_group,
 				   u64 *trimmed, u64 start, u64 end, u64 minlen,
-				   u64 maxlen, bool async);
+				   u64 maxlen, bool async,
+				   enum btrfs_clear_op_type clear);
 
 bool btrfs_free_space_cache_v1_active(struct btrfs_fs_info *fs_info);
 int btrfs_set_free_space_cache_v1_active(struct btrfs_fs_info *fs_info, bool active);
fs/btrfs/inode.c +1 -1
index f9bd9242acd3..282ccd1869c0 100644
@@ -3289,7 +3289,7 @@ int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent)
 				btrfs_discard_extent(fs_info,
 						ordered_extent->disk_bytenr,
 						ordered_extent->disk_num_bytes,
-						NULL);
+						NULL, BTRFS_CLEAR_OP_DISCARD);
 			btrfs_free_reserved_extent(fs_info,
 					ordered_extent->disk_bytenr,
 					ordered_extent->disk_num_bytes, 1);
fs/btrfs/volumes.c +2 -1
index 515003a23e65..b6d007e358d4 100644
@@ -3534,7 +3534,8 @@ int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
 	 * filesystem's point of view.
 	 */
 	if (btrfs_is_zoned(fs_info)) {
-		ret = btrfs_discard_extent(fs_info, chunk_offset, length, NULL);
+		ret = btrfs_discard_extent(fs_info, chunk_offset, length, NULL,
+					   BTRFS_CLEAR_OP_DISCARD);
 		if (ret)
 			btrfs_info(fs_info,
 				"failed to reset zone %llu after relocation",
include/uapi/linux/btrfs.h +8 -0
index d3b222d7af24..64f971a6bcb2 100644
@@ -1086,6 +1086,14 @@ enum btrfs_err_code {
 	BTRFS_ERROR_DEV_RAID1C4_MIN_NOT_MET,
 };
 
+/*
+ * Type of operation that will be used to clear unused blocks.
+ */
+enum btrfs_clear_op_type {
+	BTRFS_CLEAR_OP_DISCARD,
+	BTRFS_NR_CLEAR_OP_TYPES,
+};
+
 #define BTRFS_IOC_SNAP_CREATE _IOW(BTRFS_IOCTL_MAGIC, 1, \
 				   struct btrfs_ioctl_vol_args)
 #define BTRFS_IOC_DEFRAG _IOW(BTRFS_IOCTL_MAGIC, 2, \
Signature · 1 line
2.47.1
author maintainer
#2 lore raw

PATCH 2/5 btrfs: add new ioctl CLEAR_FREE

In reply to David Sterba

Add a new ioctl that is an extensible version of FITRIM. It currently does only the trim/discard and will be extended by other modes like zeroing or block unmapping.

We need a new ioctl for that because struct fstrim_range does not provide any existing or reserved member for extensions. The new ioctl also supports TRIM as the operation type.

---
 fs/btrfs/extent-tree.c     | 92 ++++++++++++++++++++++++++++++++++++++
 fs/btrfs/extent-tree.h     |  2 +
 fs/btrfs/ioctl.c           | 42 +++++++++++++++++
 include/uapi/linux/btrfs.h | 20 +++++++++
 4 files changed, 156 insertions(+)
fs/btrfs/extent-tree.c +92 -0
index df86ffde478b..4ab9850b7383 100644
@@ -6562,3 +6562,95 @@ int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range)
 		return bg_ret;
 	return dev_ret;
 }
+
+int btrfs_clear_free_space(struct btrfs_fs_info *fs_info,
+			   struct btrfs_ioctl_clear_free_args *args)
+{
+	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
+	struct btrfs_device *device;
+	struct btrfs_block_group *cache = NULL;
+	u64 group_cleared;
+	u64 range_end = U64_MAX;
+	u64 start;
+	u64 end;
+	u64 cleared = 0;
+	u64 bg_failed = 0;
+	u64 dev_failed = 0;
+	int bg_ret = 0;
+	int dev_ret = 0;
+	int ret = 0;
+
+	if (args->start == U64_MAX)
+		return -EINVAL;
+
+	/*
+	 * Check range overflow if args->length is set.  The default args->length
+	 * is U64_MAX.
+	 */
+	if (args->length != U64_MAX &&
+	    check_add_overflow(args->start, args->length, &range_end))
+		return -EINVAL;
+
+	cache = btrfs_lookup_first_block_group(fs_info, args->start);
+	for (; cache; cache = btrfs_next_block_group(cache)) {
+		if (cache->start >= range_end) {
+			btrfs_put_block_group(cache);
+			break;
+		}
+
+		start = max(args->start, cache->start);
+		end = min(range_end, cache->start + cache->length);
+
+		if (end - start >= args->minlen) {
+			if (!btrfs_block_group_done(cache)) {
+				ret = btrfs_cache_block_group(cache, true);
+				if (ret) {
+					bg_failed++;
+					bg_ret = ret;
+					continue;
+				}
+			}
+			ret = btrfs_trim_block_group(cache, &group_cleared,
+						     start, end, args->minlen,
+						     args->type);
+
+			cleared += group_cleared;
+			if (ret) {
+				bg_failed++;
+				bg_ret = ret;
+				continue;
+			}
+		}
+	}
+
+	if (bg_failed)
+		btrfs_warn(fs_info,
+			"failed to clear %llu block group(s), last error %d",
+			bg_failed, bg_ret);
+
+	mutex_lock(&fs_devices->device_list_mutex);
+	list_for_each_entry(device, &fs_devices->devices, dev_list) {
+		if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
+			continue;
+
+		ret = btrfs_trim_free_extents(device, &group_cleared, args->type);
+		if (ret) {
+			dev_failed++;
+			dev_ret = ret;
+			break;
+		}
+
+		cleared += group_cleared;
+	}
+	mutex_unlock(&fs_devices->device_list_mutex);
+
+	if (dev_failed)
+		btrfs_warn(fs_info,
+			"failed to trim %llu device(s), last error %d",
+			dev_failed, dev_ret);
+	args->length = cleared;
+	if (bg_ret)
+		return bg_ret;
+
+	return dev_ret;
+}
fs/btrfs/extent-tree.h +2 -0
index c8e1a30309ab..e0702b276825 100644
@@ -166,5 +166,7 @@ int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
 			 u64 num_bytes, u64 *actual_bytes,
 			 enum btrfs_clear_op_type clear);
 int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range);
+int btrfs_clear_free_space(struct btrfs_fs_info *fs_info,
+			   struct btrfs_ioctl_clear_free_args *args);
 
 #endif
fs/btrfs/ioctl.c +42 -0

54 lines not on this page · raw message

include/uapi/linux/btrfs.h +20 -0
index 64f971a6bcb2..278010aff02e 100644
@@ -1094,6 +1094,24 @@ enum btrfs_clear_op_type {
 	BTRFS_NR_CLEAR_OP_TYPES,
 };
 
+struct btrfs_ioctl_clear_free_args {
+	/* In, type of clearing operation, enumerated in btrfs_clear_free_op_type. */
+	__u32 type;
+	/* Reserved must be zero. */
+	__u32 reserved1;
+	/*
+	 * In. Starting offset to clear from in the logical address space (same
+	 * as fstrim_range::start).
+	 */
+	__u64 start;			/* in */
+	/* In, out. Length from the start to clear (same as fstrim_range::length). */
+	__u64 length;
+	/* In. Minimal length to clear (same as fstrim_range::minlen). */
+	__u64 minlen;
+	/* Reserved, must be zero. */
+	__u64 reserved2[4];
+};
+
 #define BTRFS_IOC_SNAP_CREATE _IOW(BTRFS_IOCTL_MAGIC, 1, \
 				   struct btrfs_ioctl_vol_args)
 #define BTRFS_IOC_DEFRAG _IOW(BTRFS_IOCTL_MAGIC, 2, \
@@ -1200,6 +1218,8 @@ enum btrfs_clear_op_type {
 				   struct btrfs_ioctl_vol_args_v2)
 #define BTRFS_IOC_LOGICAL_INO_V2 _IOWR(BTRFS_IOCTL_MAGIC, 59, \
 					struct btrfs_ioctl_logical_ino_args)
+#define BTRFS_IOC_CLEAR_FREE _IOW(BTRFS_IOCTL_MAGIC, 90, \
+				struct btrfs_ioctl_clear_free_args)
 #define BTRFS_IOC_GET_SUBVOL_INFO _IOR(BTRFS_IOCTL_MAGIC, 60, \
 				struct btrfs_ioctl_get_subvol_info_args)
 #define BTRFS_IOC_GET_SUBVOL_ROOTREF _IOWR(BTRFS_IOCTL_MAGIC, 61, \
Signature · 1 line
2.47.1
#3 lore raw

Re: [PATCH 2/5] btrfs: add new ioctl CLEAR_FREE

New to lkml, please correct me if I made any mistake :P

Quoted 28 lines
fs/btrfs/ioctl.c +28 -0
+static int btrfs_ioctl_clear_free(struct file *file, void __user *arg)
+{
+	struct btrfs_fs_info *fs_info;
+	struct btrfs_ioctl_clear_free_args args;
+	u64 total_bytes;
+	int ret;
+
+	if (!capable(CAP_SYS_ADMIN))
+		return -EPERM;
+
+	if (copy_from_user(&args, arg, sizeof(args)))
+		return -EFAULT;
+
+	if (args.type >= BTRFS_NR_CLEAR_OP_TYPES)
+		return -EOPNOTSUPP;
+
+	ret = mnt_want_write_file(file);
+	if (ret)
+		return ret;
+
+	fs_info = inode_to_fs_info(file_inode(file));
+	total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
+	if (args.start > total_bytes) {
+		ret = -EINVAL;
+		goto out_drop_write;
+	}
+
+	ret = btrfs_clear_free_space(fs_info, &args);

fs/btrfs/ioctl.c static int btrfs_ioctl_subvol_sync(struct btrfs_fs_info *fs_info, void __user *a · line 5253

Quoted 10 lines
fs/btrfs/ioctl.c +10 -0
+	if (ret < 0)
+		goto out_drop_write;
+
+	if (copy_to_user(arg, &args, sizeof(args)))
+		ret = -EFAULT;
+
+out_drop_write:
+	mnt_drop_write_file(file);
+
+	return 0;

previous stored return value int ret is not used here.

Quoted 1 line

+}

#4 lore raw

Re: [PATCH 2/5] btrfs: add new ioctl CLEAR_FREE

Quoted 33 lines · Sun YangKai

New to lkml, please correct me if I made any mistake :P

Quoted 31 lines
fs/btrfs/ioctl.c +31 -0
+static int btrfs_ioctl_clear_free(struct file *file, void __user *arg)
+{
+	struct btrfs_fs_info *fs_info;
+	struct btrfs_ioctl_clear_free_args args;
+	u64 total_bytes;
+	int ret;
+
+	if (!capable(CAP_SYS_ADMIN))
+		return -EPERM;
+
+	if (copy_from_user(&args, arg, sizeof(args)))
+		return -EFAULT;
+
+	if (args.type >= BTRFS_NR_CLEAR_OP_TYPES)
+		return -EOPNOTSUPP;
+
+	ret = mnt_want_write_file(file);
+	if (ret)
+		return ret;
+
+	fs_info = inode_to_fs_info(file_inode(file));
+	total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
+	if (args.start > total_bytes) {
+		ret = -EINVAL;
+		goto out_drop_write;
+	}
+
+	ret = btrfs_clear_free_space(fs_info, &args);
+	if (ret < 0)
+		goto out_drop_write;
+
Quoted 8 lines

fs/btrfs/ioctl.c static int btrfs_ioctl_subvol_sync(struct btrfs_fs_info *fs_info, void __user *a · line 5253

Quoted 7 lines
fs/btrfs/ioctl.c +7 -0
+	if (copy_to_user(arg, &args, sizeof(args)))
+		ret = -EFAULT;
+
+out_drop_write:
+	mnt_drop_write_file(file);
+
+	return 0;

previous stored return value int ret is not used here.

Right, that's a mistake, it should have been. I'll fix it, thanks.

#5 lore raw

Re: [PATCH 2/5] btrfs: add new ioctl CLEAR_FREE

In reply to David Sterba

include/uapi/linux/btrfs.h enum btrfs_clear_op_type { · line 1222

Quoted 6 lines · David Sterba
include/uapi/linux/btrfs.h +2 -0
@@ -1200,6 +1218,8 @@ enum btrfs_clear_op_type {
 				   struct btrfs_ioctl_vol_args_v2)
 #define BTRFS_IOC_LOGICAL_INO_V2 _IOWR(BTRFS_IOCTL_MAGIC, 59, \
 					struct btrfs_ioctl_logical_ino_args)
+#define BTRFS_IOC_CLEAR_FREE _IOW(BTRFS_IOCTL_MAGIC, 90, \
+				struct btrfs_ioctl_clear_free_args)

This will be moved to the end and renumbered to nr. 66. This just shows the age of the patchset when this was the last one.

Quoted 6 lines
include/uapi/linux/btrfs.h +0 -0
 #define BTRFS_IOC_GET_SUBVOL_INFO _IOR(BTRFS_IOCTL_MAGIC, 60, \
 				struct btrfs_ioctl_get_subvol_info_args)
 #define BTRFS_IOC_GET_SUBVOL_ROOTREF _IOWR(BTRFS_IOCTL_MAGIC, 61, \
Signature · 1 line
2.47.1
#6 lore raw

Re: [PATCH 2/5] btrfs: add new ioctl CLEAR_FREE

In reply to David Sterba
Quoted 9 lines · David Sterba

Add a new ioctl that is an extensible version of FITRIM. It currently does only the trim/discard and will be extended by other modes like zeroing or block unmapping.

We need a new ioctl for that because struct fstrim_range does not provide any existing or reserved member for extensions. The new ioctl also supports TRIM as the operation type.

[...]

I /think/ we need some extra checks for zoned here. blkdev_issue_zeroout won't work on zoned devices IFF the 'start' range is not aligned to the write pointer. Also blkdev_issue_discard() on 'unused space' of a zoned filesystem won't do what a user is expecting.

I think this needs:

fs/btrfs/ioctl.c static int btrfs_ioctl_subvol_sync(struct btrfs_fs_info *fs_info, void __user *a · line 5224

Quoted 9 lines
fs/btrfs/ioctl.c +9 -0
+static int btrfs_ioctl_clear_free(struct file *file, void __user *arg)
+{
+	struct btrfs_fs_info *fs_info;
+	struct btrfs_ioctl_clear_free_args args;
+	u64 total_bytes;
+	int ret;
+
+	if (!capable(CAP_SYS_ADMIN))
+		return -EPERM;
	if (btrfs_is_zoned(fs_info))
		return -EOPNOTSUPP;
Quoted 31 lines
fs/btrfs/ioctl.c +31 -0
+
+	if (copy_from_user(&args, arg, sizeof(args)))
+		return -EFAULT;
+
+	if (args.type >= BTRFS_NR_CLEAR_OP_TYPES)
+		return -EOPNOTSUPP;
+
+	ret = mnt_want_write_file(file);
+	if (ret)
+		return ret;
+
+	fs_info = inode_to_fs_info(file_inode(file));
+	total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
+	if (args.start > total_bytes) {
+		ret = -EINVAL;
+		goto out_drop_write;
+	}
+
+	ret = btrfs_clear_free_space(fs_info, &args);
+	if (ret < 0)
+		goto out_drop_write;
+
+	if (copy_to_user(arg, &args, sizeof(args)))
+		ret = -EFAULT;
+
+out_drop_write:
+	mnt_drop_write_file(file);
+
+	return 0;
+}
+
#7 lore raw

Re: [PATCH 2/5] btrfs: add new ioctl CLEAR_FREE

Quoted 20 lines · Johannes Thumshirn
Quoted 9 lines · David Sterba

Add a new ioctl that is an extensible version of FITRIM. It currently does only the trim/discard and will be extended by other modes like zeroing or block unmapping.

We need a new ioctl for that because struct fstrim_range does not provide any existing or reserved member for extensions. The new ioctl also supports TRIM as the operation type.

[...]

I /think/ we need some extra checks for zoned here. blkdev_issue_zeroout won't work on zoned devices IFF the 'start' range is not aligned to the write pointer. Also blkdev_issue_discard() on 'unused space' of a zoned filesystem won't do what a user is expecting.

I think this needs:

Quoted 12 lines

fs/btrfs/ioctl.c static int btrfs_ioctl_subvol_sync(struct btrfs_fs_info *fs_info, void __user *a · line 5224

Quoted 9 lines
fs/btrfs/ioctl.c +9 -0
+static int btrfs_ioctl_clear_free(struct file *file, void __user *arg)
+{
+	struct btrfs_fs_info *fs_info;
+	struct btrfs_ioctl_clear_free_args args;
+	u64 total_bytes;
+	int ret;
+
+	if (!capable(CAP_SYS_ADMIN))
+		return -EPERM;
	if (btrfs_is_zoned(fs_info))
		return -EOPNOTSUPP;

Right, I'll add it, thanks. As the ioctl is extension of FITRIM, the same checks should be done (btrfs_ioctl_fitrim).

#8 lore raw

Re: [PATCH 2/5] btrfs: add new ioctl CLEAR_FREE

In reply to Johannes Thumshirn
Quoted 9 lines · Johannes Thumshirn
Quoted 8 lines · David Sterba

Add a new ioctl that is an extensible version of FITRIM. It currently does only the trim/discard and will be extended by other modes like zeroing or block unmapping.

We need a new ioctl for that because struct fstrim_range does not provide any existing or reserved member for extensions. The new ioctl also supports TRIM as the operation type.

Quoted 8 lines
Quoted 1 line

[...]

I /think/ we need some extra checks for zoned here. blkdev_issue_zeroout won't work on zoned devices IFF the 'start' range is not aligned to the write pointer. Also blkdev_issue_discard() on 'unused space' of a zoned filesystem won't do what a user is expecting.

Zoned SAS HDDs can support discard on conventional zones. And if they do not, we can still do the emulated zeroout on conventional zones.
For sequential zones, we can do a zone reset of all zones that have been written but have all blocks free (so unused).

Quoted 15 lines

I think this needs:

fs/btrfs/ioctl.c static int btrfs_ioctl_subvol_sync(struct btrfs_fs_info *fs_info, void __user *a · line 5224

Quoted 9 lines
fs/btrfs/ioctl.c +9 -0
+static int btrfs_ioctl_clear_free(struct file *file, void __user *arg)
+{
+	struct btrfs_fs_info *fs_info;
+	struct btrfs_ioctl_clear_free_args args;
+	u64 total_bytes;
+	int ret;
+
+	if (!capable(CAP_SYS_ADMIN))
+		return -EPERM;
	if (btrfs_is_zoned(fs_info))
		return -EOPNOTSUPP;

With the above observations, this may be a bit too harsh. Though it is probably fine for now for zones, but adding a comment mentioning the above things we could do may be good as a reminder for later improvements.

Quoted 32 lines
Quoted 32 lines
fs/btrfs/ioctl.c +31 -0
+
+	if (copy_from_user(&args, arg, sizeof(args)))
+		return -EFAULT;
+
+	if (args.type >= BTRFS_NR_CLEAR_OP_TYPES)
+		return -EOPNOTSUPP;
+
+	ret = mnt_want_write_file(file);
+	if (ret)
+		return ret;
+
+	fs_info = inode_to_fs_info(file_inode(file));
+	total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
+	if (args.start > total_bytes) {
+		ret = -EINVAL;
+		goto out_drop_write;
+	}
+
+	ret = btrfs_clear_free_space(fs_info, &args);
+	if (ret < 0)
+		goto out_drop_write;
+
+	if (copy_to_user(arg, &args, sizeof(args)))
+		ret = -EFAULT;
+
+out_drop_write:
+	mnt_drop_write_file(file);
+
+	return 0;
+}
+
Signature · 2 lines
Damien Le Moal
Western Digital Research
#9 lore raw

Re: [PATCH 2/5] btrfs: add new ioctl CLEAR_FREE

Quoted 40 lines · Damien Le Moal
Quoted 9 lines · Johannes Thumshirn
Quoted 8 lines · David Sterba

Add a new ioctl that is an extensible version of FITRIM. It currently does only the trim/discard and will be extended by other modes like zeroing or block unmapping.

We need a new ioctl for that because struct fstrim_range does not provide any existing or reserved member for extensions. The new ioctl also supports TRIM as the operation type.

Quoted 8 lines
Quoted 1 line

[...]

I /think/ we need some extra checks for zoned here. blkdev_issue_zeroout won't work on zoned devices IFF the 'start' range is not aligned to the write pointer. Also blkdev_issue_discard() on 'unused space' of a zoned filesystem won't do what a user is expecting.

Zoned SAS HDDs can support discard on conventional zones. And if they do not, we can still do the emulated zeroout on conventional zones.
For sequential zones, we can do a zone reset of all zones that have been written but have all blocks free (so unused).

Quoted 15 lines

I think this needs:

fs/btrfs/ioctl.c static int btrfs_ioctl_subvol_sync(struct btrfs_fs_info *fs_info, void __user *a · line 5224

Quoted 9 lines
fs/btrfs/ioctl.c +9 -0
+static int btrfs_ioctl_clear_free(struct file *file, void __user *arg)
+{
+	struct btrfs_fs_info *fs_info;
+	struct btrfs_ioctl_clear_free_args args;
+	u64 total_bytes;
+	int ret;
+
+	if (!capable(CAP_SYS_ADMIN))
+		return -EPERM;
	if (btrfs_is_zoned(fs_info))
		return -EOPNOTSUPP;
Quoted 3 lines

With the above observations, this may be a bit too harsh. Though it is probably fine for now for zones, but adding a comment mentioning the above things we could do may be good as a reminder for later improvements.

Thanks for the info, I'll update the comment.

author maintainer
#10 lore raw

PATCH 3/5 btrfs: add zeroout mode to CLEAR_FREE ioctl

In reply to David Sterba

Add new type of clearing that will write zeros to the unused space (similar to what trim/discard would do).

The mode is implemented by blkdev_issue_zeroout() that can write zeros to the blocks explicitly unless the hardware implements UNMAP command that unmaps the blocks that effectively appear as zeroed. This is handled transparently.

As a special case of thin provisioning device, the UNMAP is usually handled and can free the underlying space.

---
 fs/btrfs/extent-tree.c     | 6 ++++++
 include/uapi/linux/btrfs.h | 6 ++++++
 2 files changed, 12 insertions(+)
fs/btrfs/extent-tree.c +6 -0
index 4ab9850b7383..6c45625a293a 100644
@@ -1250,10 +1250,16 @@ static int remove_extent_backref(struct btrfs_trans_handle *trans,
 static int btrfs_issue_clear_op(struct block_device *bdev, u64 start, u64 size,
 				enum btrfs_clear_op_type clear)
 {
+	unsigned int flags = BLKDEV_ZERO_KILLABLE;
+
 	switch (clear) {
 	case BTRFS_CLEAR_OP_DISCARD:
 		return blkdev_issue_discard(bdev, start >> SECTOR_SHIFT,
 					    size >> SECTOR_SHIFT, GFP_NOFS);
+	case BTRFS_CLEAR_OP_ZERO:
+		return blkdev_issue_zeroout(bdev, start >> SECTOR_SHIFT,
+					    size >> SECTOR_SHIFT, GFP_NOFS,
+					    flags);
 	default:
 		return -EOPNOTSUPP;
 	}
include/uapi/linux/btrfs.h +6 -0
index 278010aff02e..65700c289140 100644
@@ -1091,6 +1091,12 @@ enum btrfs_err_code {
  */
 enum btrfs_clear_op_type {
 	BTRFS_CLEAR_OP_DISCARD,
+	/*
+	 * Write zeros to the range, either overwrite or with hardware offload
+	 * that can unmap the blocks internally.
+	 * (Same as blkdev_issue_zeroout() with 0 flags).
+	 */
+	BTRFS_CLEAR_OP_ZERO,
 	BTRFS_NR_CLEAR_OP_TYPES,
 };
 
Signature · 1 line
2.47.1
author maintainer
#11 lore raw

PATCH 4/5 btrfs: add secure erase mode to CLEAR_FREE ioctl

In reply to David Sterba

Add another type of clearing that will do secure erase on the unused space.

This requires hardware support and works as a regular discard while also deleting any copied or cached blocks. Same as "blkdiscard --secure".

The unused space ranges may not be aligned to the secure erase block or be of a sufficient length, the exact result depends on the device. Some blocks may still contain valid data even after this ioctl.

---
 fs/btrfs/extent-tree.c     | 3 +++
 include/uapi/linux/btrfs.h | 7 +++++++
 2 files changed, 10 insertions(+)
fs/btrfs/extent-tree.c +3 -0
index 6c45625a293a..e38760fbf324 100644
@@ -1253,6 +1253,9 @@ static int btrfs_issue_clear_op(struct block_device *bdev, u64 start, u64 size,
 	unsigned int flags = BLKDEV_ZERO_KILLABLE;
 
 	switch (clear) {
+	case BTRFS_CLEAR_OP_SECURE_ERASE:
+		return blkdev_issue_secure_erase(bdev, start >> SECTOR_SHIFT,
+						 size >> SECTOR_SHIFT, GFP_NOFS);
 	case BTRFS_CLEAR_OP_DISCARD:
 		return blkdev_issue_discard(bdev, start >> SECTOR_SHIFT,
 					    size >> SECTOR_SHIFT, GFP_NOFS);
include/uapi/linux/btrfs.h +7 -0
index 65700c289140..018f0f1bbd5f 100644
@@ -1097,6 +1097,13 @@ enum btrfs_clear_op_type {
 	 * (Same as blkdev_issue_zeroout() with 0 flags).
 	 */
 	BTRFS_CLEAR_OP_ZERO,
+	/*
+	 * Do a secure erase operation on the range. If supported by the
+	 * underlying hardware, this works as regular discard except that all
+	 * copies of the discarded blocks that were possibly created by
+	 * garbage collection must also be erased.
+	 */
+	BTRFS_CLEAR_OP_SECURE_ERASE,
 	BTRFS_NR_CLEAR_OP_TYPES,
 };
 
Signature · 1 line
2.47.1
author maintainer
#12 lore raw

PATCH 5/5 btrfs: add more zeroout modes to CLEAR_FREE ioctl

In reply to David Sterba

The zeroing mode BTRFS_CLEAR_OP_ZERO is safe for use regardless of the underlying device capabilities, either zeros are written or the device will unmap the blocks. This a safe behaviour.

In case it's desired to do one or the another add modes that can enforce that or fail when unsupported;

- CLEAR_OP_ZERO - overwrite by zero blocks, forbid unmapping blocks by the device

- CLEAR_OP_ZERO_NOFALLBACK - unmap the blocks by device and do not fall back to overwriting by zeros

Implemented by __blkdev_issue_zeroout() and also documented there.

---
 fs/btrfs/extent-tree.c     | 11 +++++++++--
 include/uapi/linux/btrfs.h |  5 +++++
 2 files changed, 14 insertions(+), 2 deletions(-)
fs/btrfs/extent-tree.c +9 -2
index e38760fbf324..779216aa8ce0 100644
@@ -1259,10 +1259,17 @@ static int btrfs_issue_clear_op(struct block_device *bdev, u64 start, u64 size,
 	case BTRFS_CLEAR_OP_DISCARD:
 		return blkdev_issue_discard(bdev, start >> SECTOR_SHIFT,
 					    size >> SECTOR_SHIFT, GFP_NOFS);
+	case BTRFS_CLEAR_OP_ZERO_NOUNMAP:
+		flags |= BLKDEV_ZERO_NOUNMAP;
+		return blkdev_issue_zeroout(bdev, start >> SECTOR_SHIFT,
+					    size >> SECTOR_SHIFT, GFP_NOFS, flags);
+	case BTRFS_CLEAR_OP_ZERO_NOFALLBACK:
+		flags |= BLKDEV_ZERO_NOFALLBACK;
+		return blkdev_issue_zeroout(bdev, start >> SECTOR_SHIFT,
+					    size >> SECTOR_SHIFT, GFP_NOFS, flags);
 	case BTRFS_CLEAR_OP_ZERO:
 		return blkdev_issue_zeroout(bdev, start >> SECTOR_SHIFT,
-					    size >> SECTOR_SHIFT, GFP_NOFS,
-					    flags);
+					    size >> SECTOR_SHIFT, GFP_NOFS, flags);
 	default:
 		return -EOPNOTSUPP;
 	}
include/uapi/linux/btrfs.h +5 -0
index 018f0f1bbd5f..12e54f3b0a13 100644
@@ -1104,6 +1104,11 @@ enum btrfs_clear_op_type {
 	 * garbage collection must also be erased.
 	 */
 	BTRFS_CLEAR_OP_SECURE_ERASE,
+
+	/* Overwrite by zeros, do not try to unmap blocks. */
+	BTRFS_CLEAR_OP_ZERO_NOUNMAP,
+	/* Request unmapping the blocks and don't fall back to writing zeros. */
+	BTRFS_CLEAR_OP_ZERO_NOFALLBACK,
 	BTRFS_NR_CLEAR_OP_TYPES,
 };
 
Signature · 1 line
2.47.1
#13 lore raw
In reply to David Sterba
Quoted 10 lines · David Sterba

Add ioctl that is similar to FITRIM and in addition to trim can do also zeroing (either plain overwrite, or unmap the blocks if the device supports it) and secure erase.

This can be used to zero the unused space in e.g. VM images (when run from inside the guest, if fstrim is not supported) or free space on thin-provisioned devices.

The secure erase is provided by blkdiscard command but I'm not aware of equivalent that can be run on a filesystem, so this is for parity.

For current implementation of TRIM we have the in-memory cache that tracks which chunks have been trimmed so it's not duplicating the IO. As the CLEAR code builds on tre trim code the cache would apply to any of the new modes, which is probably not what we want.