]> git.kernelconcepts.de Git - karo-tx-linux.git/commitdiff
fs/direct-io.c: salcuate fs_count correctly in get_more_blocks()
authorTao Ma <boyu.mt@taobao.com>
Wed, 5 Oct 2011 00:44:02 +0000 (11:44 +1100)
committerStephen Rothwell <sfr@canb.auug.org.au>
Thu, 13 Oct 2011 06:50:02 +0000 (17:50 +1100)
In get_more_blocks(), we use dio_count to calcuate fs_count and do some
tricky things to increase fs_count if dio_count isn't aligned.  But
actually it still has some corner cases that can't be coverd.  See the
following example:

dio_write foo -s 1024 -w 4096

(direct write 4096 bytes at offset 1024).  The same goes if the offset
isn't aligned to fs_blocksize.

In this case, the old calculation counts fs_count to be 1, but actually we
will write into 2 different blocks (if fs_blocksize=4096).  The old code
just works, since it will call get_block twice (and may have to allocate
and create extents twice for filesystems like ext4).  So we'd better call
get_block just once with the proper fs_count.

Signed-off-by: Tao Ma <boyu.mt@taobao.com>
Cc: "Theodore Ts'o" <tytso@mit.edu>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
fs/direct-io.c

index 44a360ca80464c78ace80bf547efe4b7b1f42d3a..b05f24ee547c1bb1855b0142fb521c150af812b0 100644 (file)
@@ -569,9 +569,8 @@ static int get_more_blocks(struct dio *dio)
        int ret;
        struct buffer_head *map_bh = &dio->map_bh;
        sector_t fs_startblk;   /* Into file, in filesystem-sized blocks */
+       sector_t fs_endblk;     /* Into file, in filesystem-sized blocks */
        unsigned long fs_count; /* Number of filesystem-sized blocks */
-       unsigned long dio_count;/* Number of dio_block-sized blocks */
-       unsigned long blkmask;
        int create;
 
        /*
@@ -582,11 +581,8 @@ static int get_more_blocks(struct dio *dio)
        if (ret == 0) {
                BUG_ON(dio->block_in_file >= dio->final_block_in_request);
                fs_startblk = dio->block_in_file >> dio->blkfactor;
-               dio_count = dio->final_block_in_request - dio->block_in_file;
-               fs_count = dio_count >> dio->blkfactor;
-               blkmask = (1 << dio->blkfactor) - 1;
-               if (dio_count & blkmask)        
-                       fs_count++;
+               fs_endblk = (dio->final_block_in_request - 1) >> dio->blkfactor;
+               fs_count = fs_endblk - fs_startblk + 1;
 
                map_bh->b_state = 0;
                map_bh->b_size = fs_count << dio->inode->i_blkbits;