diff mbox series

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

Message ID 696af7c356b7ddae652be8aa997e40a391c170e6.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 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.

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

Patch

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