From: Darrick J. Wong <djwong@xxxxxxxxxx> Add a command to call XFS_IOC_MAP_FREESP. This is experimental code to see if we can build a free space defragmenter out of this. Signed-off-by: Darrick J. Wong <djwong@xxxxxxxxxx> --- io/prealloc.c | 35 +++++++++++++++++++++++++++++++++++ man/man8/xfs_io.8 | 8 +++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/io/prealloc.c b/io/prealloc.c index 5805897a4a0..0de3e142de1 100644 --- a/io/prealloc.c +++ b/io/prealloc.c @@ -45,6 +45,7 @@ static cmdinfo_t finsert_cmd; static cmdinfo_t fzero_cmd; static cmdinfo_t funshare_cmd; #endif +static cmdinfo_t fmapfree_cmd; static int offset_length( @@ -383,6 +384,30 @@ funshare_f( } #endif /* HAVE_FALLOCATE */ +static int +fmapfree_f( + int argc, + char **argv) +{ + struct xfs_flock64 segment; + struct xfs_map_freesp args = { }; + + if (!offset_length(argv[1], argv[2], &segment)) { + exitcode = 1; + return 0; + } + + args.offset = segment.l_start; + args.len = segment.l_len; + + if (ioctl(file->fd, XFS_IOC_MAP_FREESP, &args)) { + perror("XFS_IOC_MAP_FREESP"); + exitcode = 1; + return 0; + } + return 0; +} + void prealloc_init(void) { @@ -497,4 +522,14 @@ prealloc_init(void) _("unshares shared blocks within the range"); add_command(&funshare_cmd); #endif /* HAVE_FALLOCATE */ + + fmapfree_cmd.name = "fmapfree"; + fmapfree_cmd.cfunc = fmapfree_f; + fmapfree_cmd.argmin = 2; + fmapfree_cmd.argmax = 2; + fmapfree_cmd.flags = CMD_NOMAP_OK | CMD_FOREIGN_OK; + fmapfree_cmd.args = _("off len"); + fmapfree_cmd.oneline = + _("maps free space into a file"); + add_command(&fmapfree_cmd); } diff --git a/man/man8/xfs_io.8 b/man/man8/xfs_io.8 index 411144151a1..e360d22dc38 100644 --- a/man/man8/xfs_io.8 +++ b/man/man8/xfs_io.8 @@ -513,8 +513,14 @@ Call fallocate with FALLOC_FL_INSERT_RANGE flag as described in the .BR fallocate (2) manual page to create the hole by shifting data blocks. .TP +.BI fmapfree " offset length" +Maps free physical space into the file by calling XFS_IOC_MAP_FREESP as +described in the +.BR XFS_IOC_MAP_FREESP (2) +manual page. +.TP .BI fpunch " offset length" -Punches (de-allocates) blocks in the file by calling fallocate with +Punches (de-allocates) blocks in the file by calling fallocate with the FALLOC_FL_PUNCH_HOLE flag as described in the .BR fallocate (2) manual page.