Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

[PATCH] Direct IO async short read fix

The direct I/O code is mapping the read request to the file system block. If
the file size was not on a block boundary, the result would show the the read
reading past EOF. This was only happening for the AIO case. The non-AIO case
truncates the result to match file size (in direct_io_worker). This patch
does the same thing for the AIO case, it truncates the result to match the
file size if the read reads past EOF.

When I/O completes the result can be truncated to match the file size
without using i_size_read(), thus the aio result now matches the number of
bytes read to the end of file.

Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>

authored by

Daniel McNeil and committed by
Linus Torvalds
29504ff3 1f08ad02

+17 -3
+17 -3
fs/direct-io.c
··· 66 66 struct bio *bio; /* bio under assembly */ 67 67 struct inode *inode; 68 68 int rw; 69 + loff_t i_size; /* i_size when submitted */ 69 70 int lock_type; /* doesn't change */ 70 71 unsigned blkbits; /* doesn't change */ 71 72 unsigned blkfactor; /* When we're using an alignment which ··· 231 230 spin_lock_irqsave(&dio->bio_lock, flags); 232 231 if (dio->bio_count == 1) { 233 232 if (dio->is_async) { 233 + ssize_t transferred; 234 + loff_t offset; 235 + 234 236 /* 235 237 * Last reference to the dio is going away. 236 238 * Drop spinlock and complete the DIO. 237 239 */ 238 240 spin_unlock_irqrestore(&dio->bio_lock, flags); 239 - dio_complete(dio, dio->block_in_file << dio->blkbits, 240 - dio->result); 241 + 242 + /* Check for short read case */ 243 + transferred = dio->result; 244 + offset = dio->iocb->ki_pos; 245 + 246 + if ((dio->rw == READ) && 247 + ((offset + transferred) > dio->i_size)) 248 + transferred = dio->i_size - offset; 249 + 250 + dio_complete(dio, offset, transferred); 251 + 241 252 /* Complete AIO later if falling back to buffered i/o */ 242 253 if (dio->result == dio->size || 243 254 ((dio->rw == READ) && dio->result)) { 244 - aio_complete(dio->iocb, dio->result, 0); 255 + aio_complete(dio->iocb, transferred, 0); 245 256 kfree(dio); 246 257 return; 247 258 } else { ··· 964 951 dio->page_errors = 0; 965 952 dio->result = 0; 966 953 dio->iocb = iocb; 954 + dio->i_size = i_size_read(inode); 967 955 968 956 /* 969 957 * BIO completion state.