]> git.ipfire.org Git - thirdparty/e2fsprogs.git/commitdiff
e2fsprogs: Add CHANNEL_FLAGS_DISCARD_ZEROES flag for io_manager
authorLukas Czerner <lczerner@redhat.com>
Thu, 18 Nov 2010 03:38:37 +0000 (03:38 +0000)
committerTheodore Ts'o <tytso@mit.edu>
Tue, 23 Nov 2010 01:41:46 +0000 (20:41 -0500)
When the device have discard support and simultaneously discard zeroes
data (and it is properly advertised), then we can take advantage of such
behavior in several e2fsprogs tools.

Add new flag CHANNEL_FLAGS_DISCARD_ZEROES for struct_io_channel so
each io_manager can take advantage of this. The flag is properly set
according to BLKDISCARDZEROES ioctl in unix_open.

Also remove old mke2fs_discard_zeroes_data() function and substitute it
with helper which test this flag.

Signed-off-by: Lukas Czerner <lczerner@redhat.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
lib/ext2fs/ext2_io.h
lib/ext2fs/unix_io.c
misc/mke2fs.c

index f26b569b9862013257ec30c3a344cf713ea51dbf..ba97341b108fb1a84482afe38bdaa6375fbc78a7 100644 (file)
@@ -29,6 +29,9 @@ typedef struct struct_io_channel *io_channel;
 typedef struct struct_io_stats *io_stats;
 
 #define CHANNEL_FLAGS_WRITETHROUGH     0x01
+#define CHANNEL_FLAGS_DISCARD_ZEROES   0x02
+
+#define io_channel_discard_zeroes_data(i) (i->flags & CHANNEL_FLAGS_DISCARD_ZEROES)
 
 struct struct_io_channel {
        errcode_t       magic;
index 2302374c9d0ea3e70d4e080e9ee0496b255d00f2..82e0fe44867f6b948a6462273a87b5f1bc351857 100644 (file)
@@ -425,12 +425,18 @@ static errcode_t flush_cached_blocks(io_channel channel,
 }
 #endif /* NO_IO_CACHE */
 
+#ifdef __linux__
+#ifndef BLKDISCARDZEROES
+#define BLKDISCARDZEROES _IO(0x12,124)
+#endif
+#endif
+
 static errcode_t unix_open(const char *name, int flags, io_channel *channel)
 {
        io_channel      io = NULL;
        struct unix_private_data *data = NULL;
        errcode_t       retval;
-       int             open_flags;
+       int             open_flags, zeroes = 0;
        struct stat     st;
 #ifdef __linux__
        struct          utsname ut;
@@ -487,6 +493,12 @@ static errcode_t unix_open(const char *name, int flags, io_channel *channel)
        }
 #endif
 
+#ifdef BLKDISCARDZEROES
+       ioctl(data->dev, BLKDISCARDZEROES, &zeroes);
+       if (zeroes)
+               io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
+#endif
+
 #if defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
        /*
         * Some operating systems require that the buffers be aligned,
index 01ad9bfa01c9f046b2bd062a1f70b55f237caafd..0c30339e4568c8d2785aca31fe6fed39877ba31d 100644 (file)
@@ -1928,10 +1928,6 @@ static int mke2fs_setup_tdb(const char *name, io_manager *io_ptr)
 #define BLKDISCARD     _IO(0x12,119)
 #endif
 
-#ifndef BLKDISCARDZEROES
-#define BLKDISCARDZEROES _IO(0x12,124)
-#endif
-
 /*
  * Return zero if the discard succeeds, and -1 if the discard fails.
  */
@@ -1966,23 +1962,6 @@ static int mke2fs_discard_blocks(ext2_filsys fs)
        return ret;
 }
 
-static int mke2fs_discard_zeroes_data(ext2_filsys fs)
-{
-       int fd;
-       int ret;
-       int discard_zeroes_data = 0;
-
-       fd = open64(fs->device_name, O_RDWR);
-
-       if (fd > 0) {
-               ioctl(fd, BLKDISCARDZEROES, &discard_zeroes_data);
-               close(fd);
-       }
-       return discard_zeroes_data;
-}
-#else
-#define mke2fs_discard_blocks(fs)      1
-#define mke2fs_discard_zeroes_data(fs) 0
 #endif
 
 int main (int argc, char *argv[])
@@ -2047,7 +2026,7 @@ int main (int argc, char *argv[])
        if (discard && (io_ptr != undo_io_manager)) {
                retval = mke2fs_discard_blocks(fs);
 
-               if (!retval && mke2fs_discard_zeroes_data(fs)) {
+               if (!retval && io_channel_discard_zeroes_data(fs->io)) {
                        if (verbose)
                                printf(_("Discard succeeded and will return 0s "
                                         " - skipping inode table wipe\n"));