aiodio_sparse2: fix up alignment for 4k logical block sized devices
authorJeff Moyer <jmoyer@redhat.com>
Fri, 11 Feb 2011 19:08:09 +0000 (14:08 -0500)
committerChristoph Hellwig <hch@lst.de>
Sun, 13 Feb 2011 11:59:20 +0000 (12:59 +0100)
When running xfstests on a 4k logical sector device, I ran into a test
failure in test 198.  The errors were all due to trying to do 512 byte
aligned I/O on a 4k logical sector device.  The attached patch tries to
auto-detect the proper block size if no alignment is specified.  If it
fails for one reason or another, it defaults to 4k alignment.  This
seems to work fine in my test rig.

Signed-off-by: Jeff Moyer <jmoyer@redhat.com>
Signed-off-by: Christoph Hellwig <hch@lst.de>
src/aio-dio-regress/aiodio_sparse2.c

index f1225dd1bd1572ec3424de114ae83ab6d3d24f2c..681a093b6603464e45c2ee7d12774826ca48bdd3 100644 (file)
@@ -19,6 +19,8 @@
 #include <limits.h>
 #include <sys/mman.h>
 #include <sys/wait.h>
+#include <sys/ioctl.h>
+#include <sys/mount.h>
 
 #include <libaio.h>
 
@@ -274,6 +276,23 @@ void dirty_freeblocks(char *filename, int size)
        unlink(filename2);
 }
 
+static long get_logical_block_size(char *filename)
+{
+       int fd;
+       int ret;
+       int logical_block_size;
+
+       fd = open(filename, O_RDONLY);
+       if (fd < 0)
+               return 4096;
+
+       ret = ioctl(fd, BLKSSZGET, &logical_block_size);
+       close(fd);
+       if (ret < 0)
+               return 4096;
+       return logical_block_size;
+}
+
 void usage()
 {
        fprintf(stderr, "usage: dio_sparse [-n step] [-s filesize]"
@@ -314,7 +333,7 @@ long long scale_by_kmg(long long value, char scale)
 int main(int argc, char **argv)
 {
        char filename[PATH_MAX];
-       long alignment = 512;
+       long alignment = 0;
        int readsize = 65536;
        int writesize = 65536;
        int startoffset = 0;
@@ -376,6 +395,8 @@ int main(int argc, char **argv)
 
        strncpy(filename, argv[argc-1], PATH_MAX);
 
+       if (alignment == 0)
+               alignment = get_logical_block_size(filename);
        /*
         * Create some dirty free blocks by allocating, writing, syncing,
         * and then unlinking and freeing.