linux-ext4.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Darrick J. Wong" <darrick.wong@oracle.com>
To: tytso@mit.edu, darrick.wong@oracle.com
Cc: linux-ext4@vger.kernel.org
Subject: [PATCH 15/34] libext2fs: support BLKZEROOUT/FALLOC_FL_ZERO_RANGE in ext2fs_zero_blocks
Date: Sat, 13 Sep 2014 15:12:53 -0700	[thread overview]
Message-ID: <20140913221253.13646.7723.stgit@birch.djwong.org> (raw)
In-Reply-To: <20140913221112.13646.3873.stgit@birch.djwong.org>

Plumb a new call into the IO manager to support translating
ext2fs_zero_blocks calls into the equivalent kernel-level BLKZEROOUT
ioctl or FALLOC_FL_ZERO_RANGE fallocate flag primitives when possible.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
---
 contrib/fallocate.c     |   14 +++++++++
 lib/ext2fs/ext2_io.h    |    7 ++++-
 lib/ext2fs/io_manager.c |   11 +++++++
 lib/ext2fs/mkjournal.c  |    6 ++++
 lib/ext2fs/test_io.c    |   21 ++++++++++++++
 lib/ext2fs/unix_io.c    |   71 +++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 128 insertions(+), 2 deletions(-)


diff --git a/contrib/fallocate.c b/contrib/fallocate.c
index 1f9b59a..01d4af7 100644
--- a/contrib/fallocate.c
+++ b/contrib/fallocate.c
@@ -36,6 +36,8 @@
 // #include <linux/falloc.h>
 #define FALLOC_FL_KEEP_SIZE	0x01
 #define FALLOC_FL_PUNCH_HOLE	0x02 /* de-allocates range */
+#define FALLOC_FL_COLLAPSE_RANGE	0x08
+#define FALLOC_FL_ZERO_RANGE		0x10
 
 void usage(void)
 {
@@ -95,7 +97,7 @@ int main(int argc, char **argv)
 	int	error;
 	int	tflag = 0;
 
-	while ((opt = getopt(argc, argv, "npl:o:t")) != -1) {
+	while ((opt = getopt(argc, argv, "npl:o:tzc")) != -1) {
 		switch(opt) {
 		case 'n':
 			/* do not change filesize */
@@ -106,6 +108,16 @@ int main(int argc, char **argv)
 			falloc_mode = (FALLOC_FL_PUNCH_HOLE |
 				       FALLOC_FL_KEEP_SIZE);
 			break;
+		case 'c':
+			/* collapse range mode */
+			falloc_mode = (FALLOC_FL_COLLAPSE_RANGE |
+				       FALLOC_FL_KEEP_SIZE);
+			break;
+		case 'z':
+			/* zero range mode */
+			falloc_mode = (FALLOC_FL_ZERO_RANGE |
+				       FALLOC_FL_KEEP_SIZE);
+			break;
 		case 'l':
 			length = cvtnum(optarg);
 			break;
diff --git a/lib/ext2fs/ext2_io.h b/lib/ext2fs/ext2_io.h
index 4c5a5c5..1faa720 100644
--- a/lib/ext2fs/ext2_io.h
+++ b/lib/ext2fs/ext2_io.h
@@ -93,7 +93,9 @@ struct struct_io_manager {
 	errcode_t (*cache_readahead)(io_channel channel,
 				     unsigned long long block,
 				     unsigned long long count);
-	long	reserved[15];
+	errcode_t (*zeroout)(io_channel channel, unsigned long long block,
+			     unsigned long long count);
+	long	reserved[14];
 };
 
 #define IO_FLAG_RW		0x0001
@@ -125,6 +127,9 @@ extern errcode_t io_channel_write_blk64(io_channel channel,
 extern errcode_t io_channel_discard(io_channel channel,
 				    unsigned long long block,
 				    unsigned long long count);
+extern errcode_t io_channel_zeroout(io_channel channel,
+				    unsigned long long block,
+				    unsigned long long count);
 extern errcode_t io_channel_alloc_buf(io_channel channel,
 				      int count, void *ptr);
 extern errcode_t io_channel_cache_readahead(io_channel io,
diff --git a/lib/ext2fs/io_manager.c b/lib/ext2fs/io_manager.c
index dc5888d..c395d61 100644
--- a/lib/ext2fs/io_manager.c
+++ b/lib/ext2fs/io_manager.c
@@ -112,6 +112,17 @@ errcode_t io_channel_discard(io_channel channel, unsigned long long block,
 	return EXT2_ET_UNIMPLEMENTED;
 }
 
+errcode_t io_channel_zeroout(io_channel channel, unsigned long long block,
+			     unsigned long long count)
+{
+	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
+
+	if (channel->manager->zeroout)
+		return (channel->manager->zeroout)(channel, block, count);
+
+	return EXT2_ET_UNIMPLEMENTED;
+}
+
 errcode_t io_channel_alloc_buf(io_channel io, int count, void *ptr)
 {
 	size_t	size;
diff --git a/lib/ext2fs/mkjournal.c b/lib/ext2fs/mkjournal.c
index 6f3a862..5be425c 100644
--- a/lib/ext2fs/mkjournal.c
+++ b/lib/ext2fs/mkjournal.c
@@ -164,6 +164,12 @@ errcode_t ext2fs_zero_blocks2(ext2_filsys fs, blk64_t blk, int num,
 		}
 		return 0;
 	}
+
+	/* Try a zero out command, if supported */
+	retval = io_channel_zeroout(fs->io, blk, num);
+	if (retval == 0)
+		return 0;
+
 	/* Allocate the zeroizing buffer if necessary */
 	if (!buf) {
 		buf = malloc(fs->blocksize * STRIDE_LENGTH);
diff --git a/lib/ext2fs/test_io.c b/lib/ext2fs/test_io.c
index b03a939..f7c50d1 100644
--- a/lib/ext2fs/test_io.c
+++ b/lib/ext2fs/test_io.c
@@ -86,6 +86,7 @@ void (*test_io_cb_write_byte)
 #define TEST_FLAG_SET_OPTION		0x20
 #define TEST_FLAG_DISCARD		0x40
 #define TEST_FLAG_READAHEAD		0x80
+#define TEST_FLAG_ZEROOUT		0x100
 
 static void test_dump_block(io_channel channel,
 			    struct test_private_data *data,
@@ -507,6 +508,25 @@ static errcode_t test_cache_readahead(io_channel channel,
 	return retval;
 }
 
+static errcode_t test_zeroout(io_channel channel, unsigned long long block,
+			      unsigned long long count)
+{
+	struct test_private_data *data;
+	errcode_t	retval = 0;
+
+	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
+	data = (struct test_private_data *) channel->private_data;
+	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_TEST_IO_CHANNEL);
+
+	if (data->real)
+		retval = io_channel_zeroout(data->real, block, count);
+	if (data->flags & TEST_FLAG_ZEROOUT)
+		fprintf(data->outfile,
+			"Test_io: zeroout(%llu, %llu) returned %s\n",
+			block, count, retval ? error_message(retval) : "OK");
+	return retval;
+}
+
 static struct struct_io_manager struct_test_manager = {
 	.magic		= EXT2_ET_MAGIC_IO_MANAGER,
 	.name		= "Test I/O Manager",
@@ -523,6 +543,7 @@ static struct struct_io_manager struct_test_manager = {
 	.write_blk64	= test_write_blk64,
 	.discard	= test_discard,
 	.cache_readahead	= test_cache_readahead,
+	.zeroout	= test_zeroout,
 };
 
 io_manager test_io_manager = &struct_test_manager;
diff --git a/lib/ext2fs/unix_io.c b/lib/ext2fs/unix_io.c
index 189adce..20e5b64 100644
--- a/lib/ext2fs/unix_io.c
+++ b/lib/ext2fs/unix_io.c
@@ -986,6 +986,76 @@ unimplemented:
 	return EXT2_ET_UNIMPLEMENTED;
 }
 
+#if defined(__linux__) && !defined(BLKZEROOUT)
+#define BLKZEROOUT		_IO(0x12, 127)
+#endif
+
+#if defined(__linux__) && !defined(FALLOC_FL_ZERO_RANGE)
+#define FALLOC_FL_ZERO_RANGE    0x10
+#endif
+
+static errcode_t unix_zeroout(io_channel channel, unsigned long long block,
+			      unsigned long long count)
+{
+	struct unix_private_data *data;
+	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);
+
+	if (getenv("UNIX_IO_NOZEROOUT"))
+		goto unimplemented;
+
+	if (channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
+#ifdef BLKZEROOUT
+		__u64 range[2];
+
+		range[0] = (__u64)(block) * channel->block_size;
+		range[1] = (__u64)(count) * channel->block_size;
+
+		ret = ioctl(data->dev, BLKZEROOUT, &range);
+#else
+		goto unimplemented;
+#endif
+	} else {
+#if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_ZERO_RANGE)
+		int flag = FALLOC_FL_ZERO_RANGE;
+		struct stat statbuf;
+
+		/*
+		 * If we're trying to zero a range past the end of the file,
+		 * just use regular fallocate to get there, because zeroing
+		 * a range past EOF does not extend the file.
+		 */
+		ret = fstat(data->dev, &statbuf);
+		if (ret)
+			goto err;
+		if (statbuf.st_size < (block + count) * channel->block_size)
+			flag = 0;
+		/*
+		 * If we are not on block device, try to use the zero out
+		 * primitive.
+		 */
+		ret = fallocate(data->dev,
+				flag,
+				(off_t)(block) * channel->block_size,
+				(off_t)(count) * channel->block_size);
+#else
+		goto unimplemented;
+#endif
+	}
+err:
+	if (ret < 0) {
+		if (errno == EOPNOTSUPP)
+			goto unimplemented;
+		return errno;
+	}
+	return 0;
+unimplemented:
+	return EXT2_ET_UNIMPLEMENTED;
+}
+
 static struct struct_io_manager struct_unix_manager = {
 	.magic		= EXT2_ET_MAGIC_IO_MANAGER,
 	.name		= "Unix I/O Manager",
@@ -1002,6 +1072,7 @@ static struct struct_io_manager struct_unix_manager = {
 	.write_blk64	= unix_write_blk64,
 	.discard	= unix_discard,
 	.cache_readahead	= unix_cache_readahead,
+	.zeroout	= unix_zeroout,
 };
 
 io_manager unix_io_manager = &struct_unix_manager;


  parent reply	other threads:[~2014-09-13 22:12 UTC|newest]

Thread overview: 67+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-09-13 22:11 [PATCH 00/34] e2fsprogs Summer 2014 patchbomb, part 6 Darrick J. Wong
2014-09-13 22:11 ` [PATCH 01/34] e2fsck: offer to clear overlapping extents Darrick J. Wong
2014-09-19  1:45   ` Theodore Ts'o
2014-09-13 22:11 ` [PATCH 02/34] e2fsck: fix sliding the directory block down on bigalloc Darrick J. Wong
2014-09-19  1:45   ` Theodore Ts'o
2014-09-13 22:11 ` [PATCH 03/34] misc: zero s_jnl_blocks when adding journal online or removing external journal Darrick J. Wong
2014-09-19  1:45   ` Theodore Ts'o
2014-09-13 22:11 ` [PATCH 04/34] libext2fs: ext2fs_new_block2() should call alloc_block hook Darrick J. Wong
2014-09-13 22:11 ` [PATCH 05/34] debugfs: manage needs_recover feature when messing with the journal Darrick J. Wong
2014-09-19  6:01   ` Theodore Ts'o
2014-09-13 22:11 ` [PATCH 06/34] debugfs: add LIBINTL to debugfs link command Darrick J. Wong
2014-09-19  4:46   ` Theodore Ts'o
2014-10-17 21:07     ` Darrick J. Wong
2014-10-18 16:10       ` Theodore Ts'o
2014-09-13 22:11 ` [PATCH 07/34] ext2fs: add readahead method to improve scanning Darrick J. Wong
2014-09-19 16:15   ` Theodore Ts'o
2014-09-13 22:12 ` [PATCH 08/34] libext2fs/e2fsck: provide routines to read-ahead metadata Darrick J. Wong
2014-09-13 22:12 ` [PATCH 09/34] e2fsck: read-ahead metadata during passes 1, 2, and 4 Darrick J. Wong
2014-09-13 22:12 ` [PATCH 10/34] dumpe2fs: provide a machine-readable group-only mode Darrick J. Wong
2014-09-19 16:17   ` Theodore Ts'o
2014-09-13 22:12 ` [PATCH 11/34] dumpe2fs: output cleanup Darrick J. Wong
2014-09-19 16:22   ` Theodore Ts'o
2014-09-19 20:00     ` Darrick J. Wong
2014-10-13 18:04       ` Darrick J. Wong
2014-09-13 22:12 ` [PATCH 12/34] misc: move check_plausibility into a separate file Darrick J. Wong
2014-09-19 22:16   ` Theodore Ts'o
2014-09-13 22:12 ` [PATCH 13/34] misc: add plausibility checks to debugfs/tune2fs/dumpe2fs/e2fsck Darrick J. Wong
2014-09-19 23:00   ` Theodore Ts'o
2014-09-13 22:12 ` [PATCH 14/34] misc: use libmagic when libblkid can't identify something Darrick J. Wong
2014-09-21  5:29   ` Theodore Ts'o
2014-09-13 22:12 ` Darrick J. Wong [this message]
2014-09-22  2:51   ` [PATCH 15/34] libext2fs: support BLKZEROOUT/FALLOC_FL_ZERO_RANGE in ext2fs_zero_blocks Theodore Ts'o
2014-09-29 18:58     ` Darrick J. Wong
2014-10-14  2:58   ` Darrick J. Wong
2014-10-18 16:32   ` Theodore Ts'o
2014-10-20 23:37     ` Darrick J. Wong
2014-09-13 22:12 ` [PATCH 16/34] libext2fs/e2fsck: refactor everyone who writes zero blocks to disk Darrick J. Wong
2014-10-13 10:09   ` Theodore Ts'o
2014-10-13 17:09     ` Darrick J. Wong
2014-09-13 22:13 ` [PATCH 17/34] libext2fs: support allocating uninit blocks in bmap2() Darrick J. Wong
2014-10-13 14:35   ` Theodore Ts'o
2014-10-13 16:56     ` Darrick J. Wong
2014-10-13 18:34       ` Darrick J. Wong
2014-09-13 22:13 ` [PATCH 18/34] libext2fs: file IO routines should handle uninit blocks Darrick J. Wong
2014-09-13 22:13 ` [PATCH 19/34] resize2fs: convert fs to and from 64bit mode Darrick J. Wong
2014-09-14 17:34   ` TR Reardon
2014-09-14 17:50     ` Darrick J. Wong
2014-09-13 22:13 ` [PATCH 20/34] resize2fs: adjust reserved_gdt_blocks when changing group descriptor size Darrick J. Wong
2014-09-13 22:13 ` [PATCH 21/34] tests: test resize2fs 32->64 and 64->32bit conversion code Darrick J. Wong
2014-09-13 22:13 ` [PATCH 22/34] libext2fs: find inode goal when allocating blocks Darrick J. Wong
2014-09-13 22:13 ` [PATCH 23/34] libext2fs: find/alloc a range of empty blocks Darrick J. Wong
2014-09-13 22:13 ` [PATCH 24/34] libext2fs: add new hooks to support large allocations Darrick J. Wong
2014-09-13 22:14 ` [PATCH 25/34] libext2fs: implement fallocate Darrick J. Wong
2014-09-13 22:14 ` [PATCH 26/34] libext2fs: use fallocate for creating journals and hugefiles Darrick J. Wong
2014-09-13 22:14 ` [PATCH 27/34] debugfs: implement fallocate Darrick J. Wong
2014-09-13 22:14 ` [PATCH 28/34] tests: test debugfs punch command Darrick J. Wong
2014-09-19 16:26   ` Theodore Ts'o
2014-09-19 20:01     ` Darrick J. Wong
2014-09-13 22:14 ` [PATCH 30/34] fuse2fs: translate ACL structures Darrick J. Wong
2014-09-13 22:14 ` [PATCH 31/34] fuse2fs: handle 64-bit dates correctly Darrick J. Wong
2014-09-13 22:14 ` [PATCH 32/34] fuse2fs: implement fallocate Darrick J. Wong
2014-09-13 22:15 ` [PATCH 34/34] tests: enable using fuse2fs with metadata checksum test Darrick J. Wong
2014-09-14 17:19 ` [PATCH 35/34] e2fsck: free bh when descriptor block checksum fails Darrick J. Wong
2014-09-14 19:11   ` Eric Sandeen
2014-09-19  1:46     ` Theodore Ts'o
2014-09-18 19:09 ` [PATCH 36/34] misc: fix Coverity complaints Darrick J. Wong
2014-09-19  1:47   ` Theodore Ts'o

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140913221253.13646.7723.stgit@birch.djwong.org \
    --to=darrick.wong@oracle.com \
    --cc=linux-ext4@vger.kernel.org \
    --cc=tytso@mit.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).