e2fsprogs: Add discard function into struct_io_manager
In order to provide generic "discard" function for all e2fsprogs tools
add a discard function prototype into struct_io_manager. Specific
function for specific io managers can be crated that way.
This commit also creates unix_discard function which uses BLKDISCARD
ioctl to discard data blocks on the block device and bind it into
unit_io_manager structure to be available for all e2fsprogs tools.
Note that BLKDISCARD is still Linux specific ioctl, however other
unix systems may provide similar functionality. So far the
unix_discard() remains linux specific hence is embedded in #ifdef
__linux__ macro.
Signed-off-by: Lukas Czerner <lczerner@redhat.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
diff --git a/lib/ext2fs/unix_io.c b/lib/ext2fs/unix_io.c
index 1df1fdd..2302374 100644
--- a/lib/ext2fs/unix_io.c
+++ b/lib/ext2fs/unix_io.c
@@ -115,6 +115,8 @@
int count, void *data);
static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
int count, const void *data);
+static errcode_t unix_discard(io_channel channel, unsigned long long block,
+ unsigned long long count);
static struct struct_io_manager struct_unix_manager = {
EXT2_ET_MAGIC_IO_MANAGER,
@@ -130,6 +132,7 @@
unix_get_stats,
unix_read_blk64,
unix_write_blk64,
+ unix_discard,
};
io_manager unix_io_manager = &struct_unix_manager;
@@ -834,3 +837,31 @@
}
return EXT2_ET_INVALID_ARGUMENT;
}
+
+#if defined(__linux__) && !defined(BLKDISCARD)
+#define BLKDISCARD _IO(0x12,119)
+#endif
+
+static errcode_t unix_discard(io_channel channel, unsigned long long block,
+ unsigned long long count)
+{
+#ifdef BLKDISCARD
+ struct unix_private_data *data;
+ __uint64_t range[2];
+ int ret;
+
+ EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
+ data = (struct unix_private_data *) channel->private_data;
+ EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
+
+ range[0] = (__uint64_t)(block) * channel->block_size;
+ range[1] = (__uint64_t)(count) * channel->block_size;
+
+ ret = ioctl(data->dev, BLKDISCARD, &range);
+ if (ret < 0)
+ return errno;
+ return 0;
+#else
+ return EXT2_ET_UNIMPLEMENTED;
+#endif
+}