diff mbox series

[3/5] btrfs: add zeroout mode to CLEAR_FREE ioctl

Message ID 20063b99847a5ac0e2d88a2d4b2082f7aa7ec40d.1740753608.git.dsterba@suse.com (mailing list archive)
State New
Headers show
Series Ioctl to clear unused space in various ways | expand

Commit Message

David Sterba Feb. 28, 2025, 2:49 p.m. UTC
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.

Signed-off-by: David Sterba <dsterba@suse.com>
---
 fs/btrfs/extent-tree.c     | 6 ++++++
 include/uapi/linux/btrfs.h | 6 ++++++
 2 files changed, 12 insertions(+)
diff mbox series

Patch

diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index 4ab9850b7383..6c45625a293a 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -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;
 	}
diff --git a/include/uapi/linux/btrfs.h b/include/uapi/linux/btrfs.h
index 278010aff02e..65700c289140 100644
--- a/include/uapi/linux/btrfs.h
+++ b/include/uapi/linux/btrfs.h
@@ -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,
 };