[patch] fix O_DIRECT read of last block in a sparse file

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

Currently, if you open a file O_DIRECT, truncate it to a size that is not a
multiple of the disk block size, and then try to read the last block in the
file, the read will return 0.  The problem is in do_direct_IO, here:

        /* Handle holes */
        if (!buffer_mapped(map_bh)) {
                char *kaddr;

		...

                if (dio->block_in_file >=
                        i_size_read(dio->inode)>>blkbits) {
                        /* We hit eof */
                        page_cache_release(page);
                        goto out;
                }

We shift off any remaining bytes in the final block of the I/O, resulting
in a 0-sized read.  I've attached a patch that fixes this.  I'm not happy
about how ugly the math is getting, so suggestions are more than welcome.

I've tested this with a simple program that performs the steps outlined for
reproducing the problem above.  Without the patch, we get a 0-sized result
from read.  With the patch, we get the correct return value from the short
read.

Thanks,

Jeff

Signed-off-by: Jeff Moyer <[email protected]>

--- linux-2.6.16-rc1-mm1/fs/direct-io.c.orig	2006-01-19 15:45:50.000000000 -0500
+++ linux-2.6.16-rc1-mm1/fs/direct-io.c	2006-01-19 16:09:33.000000000 -0500
@@ -857,6 +857,7 @@ do_holes:
 			/* Handle holes */
 			if (!buffer_mapped(map_bh)) {
 				char *kaddr;
+				loff_t i_size = i_size_read(dio->inode);
 
 				/* AKPM: eargh, -ENOTBLK is a hack */
 				if (dio->rw == WRITE) {
@@ -864,8 +865,10 @@ do_holes:
 					return -ENOTBLK;
 				}
 
-				if (dio->block_in_file >=
-					i_size_read(dio->inode)>>blkbits) {
+				/* Be sure to account for a partial block as
+				 * the last block in the file */
+				if (dio->block_in_file >= i_size >> blkbits &&
+					!(i_size & ~((1<<blkbits)-1))) {
 					/* We hit eof */
 					page_cache_release(page);
 					goto out;
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[Index of Archives]     [Kernel Newbies]     [Netfilter]     [Bugtraq]     [Photo]     [Stuff]     [Gimp]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Video 4 Linux]     [Linux for the blind]     [Linux Resources]
  Powered by Linux