Ioctl to clear unused space in various ways
David Sterba
maintainerv2 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.
v2:
- return -EOPNOTSUPP for zoned mode, with a note
- add new operation to reset the chunk map status wrt trim
David Sterba (6):
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
btrfs: add mode to clear chunk map status 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 | 62 ++++++++++++++ fs/btrfs/volumes.c | 8 +- fs/btrfs/volumes.h | 1 + include/uapi/linux/btrfs.h | 53 ++++++++++++ 10 files changed, 291 insertions(+), 40 deletions(-)
Signature · 1 line
2.47.1
David Sterba
maintainerPATCH v2 1/6 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
@@ -524,13 +524,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
@@ -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
@@ -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
@@ -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
@@ -3307,7 +3307,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
@@ -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
@@ -1096,6 +1096,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
David Sterba
maintainerPATCH v2 2/6 btrfs: add new ioctl CLEAR_FREE
In reply to David SterbaAdd 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 | 49 ++++++++++++++++++++ include/uapi/linux/btrfs.h | 20 +++++++++ 4 files changed, 163 insertions(+)
fs/btrfs/extent-tree.c +92 -0
@@ -6570,3 +6570,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
@@ -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 +49 -0
61 lines not on this page · raw message
include/uapi/linux/btrfs.h +20 -0
@@ -1104,6 +1104,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, \
@@ -1224,6 +1242,8 @@ enum btrfs_clear_op_type { struct btrfs_ioctl_encoded_io_args)#define BTRFS_IOC_SUBVOL_SYNC_WAIT _IOW(BTRFS_IOCTL_MAGIC, 65, \ struct btrfs_ioctl_subvol_wait) +#define BTRFS_IOC_CLEAR_FREE _IOWR(BTRFS_IOCTL_MAGIC, 66, \ + struct btrfs_ioctl_clear_free_args) #ifdef __cplusplus }
Signature · 1 line
2.47.1
David Sterba
maintainerPATCH v2 3/6 btrfs: add zeroout mode to CLEAR_FREE ioctl
In reply to David SterbaAdd 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
@@ -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
@@ -1101,6 +1101,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
David Sterba
maintainerPATCH v2 4/6 btrfs: add secure erase mode to CLEAR_FREE ioctl
In reply to David SterbaAdd 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
@@ -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
@@ -1107,6 +1107,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
David Sterba
maintainerPATCH v2 6/6 btrfs: add mode to clear chunk map status to CLEAR_FREE ioctl
In reply to David SterbaThe trim status is tracked for each chunk in the fs_info::mapping_tree and updated as trim is called either manually by 'fstrim' or automatically when discard=async is enabled.
With the new modes it's necessary to allow clearing the cache otherwise on a fully or partially trimmed filesystem the ioctl won't work as expected.
Add separate clear free operation to reset just the trim status bits from all chunks. This should be called namely when the clearing operation is *not* trim (e.g. zeroout or secure erase).
--- fs/btrfs/ioctl.c | 13 +++++++++++++ fs/btrfs/volumes.c | 5 +++++ fs/btrfs/volumes.h | 1 + include/uapi/linux/btrfs.h | 7 +++++++ 4 files changed, 26 insertions(+)
fs/btrfs/ioctl.c +13 -0
@@ -5235,6 +5235,19 @@ static int btrfs_ioctl_clear_free(struct file *file, void __user *arg)if (args.type >=BTRFS_NR_CLEAR_OP_TYPES )return -EOPNOTSUPP ; +if (args.type ==BTRFS_CLEAR_OP_RESET_CHUNK_STATUS_CACHE ) { +write_lock (&fs_info->mapping_tree_lock); +for (struct rb_node *node =rb_first_cached (&fs_info->mapping_tree); + node; node =rb_next (node)) { +struct btrfs_chunk_map *map; + + map = rb_entry(node, struct btrfs_chunk_map, rb_node); +btrfs_chunk_map_clear_bits (map,CHUNK_TRIMMED ); + } +write_unlock (&fs_info->mapping_tree_lock); +return 0 ; + } + ret =mnt_want_write_file (file);if (ret)return ret;
fs/btrfs/volumes.c +5 -0
@@ -8079,6 +8079,11 @@ static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info *fs_info)return ret; } +void btrfs_chunk_map_clear_bits (struct btrfs_chunk_map *map,unsigned int bits) +{ +chunk_map_device_clear_bits (map, bits); +} +/* * Ensure that all dev extents are mapped to correct chunk, otherwise * later chunk allocation/free would cause unexpected behavior.
fs/btrfs/volumes.h +1 -0
@@ -785,6 +785,7 @@ struct btrfs_chunk_map *btrfs_find_chunk_map_nolock(struct btrfs_fs_info *fs_inf u64 logical, u64 length);struct btrfs_chunk_map *btrfs_get_chunk_map (struct btrfs_fs_info *fs_info,u64 logical,u64 length); +void btrfs_chunk_map_clear_bits (struct btrfs_chunk_map *map,unsigned int bits);void btrfs_remove_chunk_map (struct btrfs_fs_info *fs_info,struct btrfs_chunk_map *map);void btrfs_release_disk_super (struct btrfs_super_block *super);
include/uapi/linux/btrfs.h +7 -0
@@ -1119,6 +1119,13 @@ enum btrfs_clear_op_type {BTRFS_CLEAR_OP_ZERO_NOUNMAP ,/* Request unmapping the blocks and don't fall back to writing zeros. */ BTRFS_CLEAR_OP_ZERO_NOFALLBACK , + +/* +* Only reset status of previously cleared (by any operation) chunks, +* tracked in memory since the last mount. Without that repeated calls +* to clear will skip already processed chunks. +*/ +BTRFS_CLEAR_OP_RESET_CHUNK_STATUS_CACHE , BTRFS_NR_CLEAR_OP_TYPES, };
Signature · 1 line
2.47.1
David Sterba
maintainerPATCH v2 5/6 btrfs: add more zeroout modes to CLEAR_FREE ioctl
In reply to David SterbaThe 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
@@ -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
@@ -1114,6 +1114,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
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.
I've found more things to add to the API to support more use cases so this will be moved to another major release. For example physical ranges rather than logical (current FITRIM deficiency wrt btrfs), per-device clearing. Also the cache dropping can be adjusted for a device or range.