From mboxrd@z Thu Jan 1 00:00:00 1970 From: Tao Ma Subject: [PATCH for 3.2] fs/direct-io.c: Calculate fs_count correctly in get_more_blocks. Date: Mon, 31 Oct 2011 15:24:33 +0800 Message-ID: <1320045873-3956-1-git-send-email-tm@tao.ma> References: <20111029105856.GA6479@infradead.org> Cc: linux-devel@vger.kernel.org, Christoph Hellwig , Al Viro , Andrew Morton To: linux-fsdevel@vger.kernel.org Return-path: Received: from oproxy5-pub.bluehost.com ([67.222.38.55]:52025 "HELO oproxy5-pub.bluehost.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S1754051Ab1JaHY7 (ORCPT ); Mon, 31 Oct 2011 03:24:59 -0400 In-Reply-To: <20111029105856.GA6479@infradead.org> Sender: linux-fsdevel-owner@vger.kernel.org List-ID: From: Tao Ma In get_more_blocks, we use dio_count to calculate fs_count and do some tricky things to increase fs_count if dio_count isn't aligned. But actually it still has some cornor case 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 extent twice for file systems like ext4). So we'd better call get_block just once with the proper fs_count. Cc: Christoph Hellwig Cc: Al Viro Cc: Andrew Morton Signed-off-by: Tao Ma --- Christoph, this is the updated version. I have done some tests against it and it works. And the only difference between the old and this one is the change from dio to sdio. Thanks. fs/direct-io.c | 11 ++++------- 1 files changed, 4 insertions(+), 7 deletions(-) diff --git a/fs/direct-io.c b/fs/direct-io.c index d740ab6..5582183 100644 --- a/fs/direct-io.c +++ b/fs/direct-io.c @@ -580,9 +580,8 @@ static int get_more_blocks(struct dio *dio, struct dio_submit *sdio, { int ret; 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; /* @@ -593,11 +592,9 @@ static int get_more_blocks(struct dio *dio, struct dio_submit *sdio, if (ret == 0) { BUG_ON(sdio->block_in_file >= sdio->final_block_in_request); fs_startblk = sdio->block_in_file >> sdio->blkfactor; - dio_count = sdio->final_block_in_request - sdio->block_in_file; - fs_count = dio_count >> sdio->blkfactor; - blkmask = (1 << sdio->blkfactor) - 1; - if (dio_count & blkmask) - fs_count++; + fs_endblk = (sdio->final_block_in_request - 1) >> + sdio->blkfactor; + fs_count = fs_endblk - fs_startblk + 1; map_bh->b_state = 0; map_bh->b_size = fs_count << dio->inode->i_blkbits; -- 1.7.1