Skip to content

Commit facd07b

Browse files
Josef Bacikchrismason-xx
authored andcommitted
direct-io: add a hook for the fs to provide its own submit_bio function
Because BTRFS can do RAID and such, we need our own submit hook so we can setup the bio's in the correct fashion, and handle checksum errors properly. So there are a few changes here 1) The submit_io hook. This is straightforward, just call this instead of submit_bio. 2) Allow the fs to return -ENOTBLK for reads. Usually this has only worked for writes, since writes can fallback onto buffered IO. But BTRFS needs the option of falling back on buffered IO if it encounters a compressed extent, since we need to read the entire extent in and decompress it. So if we get -ENOTBLK back from get_block we'll return back and fallback on buffered just like the write case. I've tested these changes with fsx and everything seems to work. Thanks, Signed-off-by: Josef Bacik <josef@redhat.com> Signed-off-by: Chris Mason <chris.mason@oracle.com>
1 parent 66f998f commit facd07b

2 files changed

Lines changed: 45 additions & 8 deletions

File tree

‎fs/direct-io.c‎

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ struct dio {
8282
int reap_counter; /* rate limit reaping */
8383
get_block_t *get_block; /* block mapping function */
8484
dio_iodone_t *end_io; /* IO completion function */
85+
dio_submit_t *submit_io; /* IO submition function */
86+
loff_t logical_offset_in_bio; /* current first logical block in bio */
8587
sector_t final_block_in_bio; /* current final block in bio + 1 */
8688
sector_t next_block_for_io; /* next block to be put under IO,
8789
in dio_blocks units */
@@ -96,6 +98,7 @@ struct dio {
9698
unsigned cur_page_offset; /* Offset into it, in bytes */
9799
unsigned cur_page_len; /* Nr of bytes at cur_page_offset */
98100
sector_t cur_page_block; /* Where it starts */
101+
loff_t cur_page_fs_offset; /* Offset in file */
99102

100103
/* BIO completion state */
101104
spinlock_t bio_lock; /* protects BIO fields below */
@@ -300,6 +303,26 @@ static void dio_bio_end_io(struct bio *bio, int error)
300303
spin_unlock_irqrestore(&dio->bio_lock, flags);
301304
}
302305

306+
/**
307+
* dio_end_io - handle the end io action for the given bio
308+
* @bio: The direct io bio thats being completed
309+
* @error: Error if there was one
310+
*
311+
* This is meant to be called by any filesystem that uses their own dio_submit_t
312+
* so that the DIO specific endio actions are dealt with after the filesystem
313+
* has done it's completion work.
314+
*/
315+
void dio_end_io(struct bio *bio, int error)
316+
{
317+
struct dio *dio = bio->bi_private;
318+
319+
if (dio->is_async)
320+
dio_bio_end_aio(bio, error);
321+
else
322+
dio_bio_end_io(bio, error);
323+
}
324+
EXPORT_SYMBOL_GPL(dio_end_io);
325+
303326
static int
304327
dio_bio_alloc(struct dio *dio, struct block_device *bdev,
305328
sector_t first_sector, int nr_vecs)
@@ -316,6 +339,7 @@ dio_bio_alloc(struct dio *dio, struct block_device *bdev,
316339
bio->bi_end_io = dio_bio_end_io;
317340

318341
dio->bio = bio;
342+
dio->logical_offset_in_bio = dio->cur_page_fs_offset;
319343
return 0;
320344
}
321345

@@ -340,10 +364,15 @@ static void dio_bio_submit(struct dio *dio)
340364
if (dio->is_async && dio->rw == READ)
341365
bio_set_pages_dirty(bio);
342366

343-
submit_bio(dio->rw, bio);
367+
if (dio->submit_io)
368+
dio->submit_io(dio->rw, bio, dio->inode,
369+
dio->logical_offset_in_bio);
370+
else
371+
submit_bio(dio->rw, bio);
344372

345373
dio->bio = NULL;
346374
dio->boundary = 0;
375+
dio->logical_offset_in_bio = 0;
347376
}
348377

349378
/*
@@ -701,6 +730,7 @@ submit_page_section(struct dio *dio, struct page *page,
701730
dio->cur_page_offset = offset;
702731
dio->cur_page_len = len;
703732
dio->cur_page_block = blocknr;
733+
dio->cur_page_fs_offset = dio->block_in_file << dio->blkbits;
704734
out:
705735
return ret;
706736
}
@@ -935,7 +965,7 @@ static ssize_t
935965
direct_io_worker(int rw, struct kiocb *iocb, struct inode *inode,
936966
const struct iovec *iov, loff_t offset, unsigned long nr_segs,
937967
unsigned blkbits, get_block_t get_block, dio_iodone_t end_io,
938-
struct dio *dio)
968+
dio_submit_t submit_io, struct dio *dio)
939969
{
940970
unsigned long user_addr;
941971
unsigned long flags;
@@ -952,6 +982,7 @@ direct_io_worker(int rw, struct kiocb *iocb, struct inode *inode,
952982

953983
dio->get_block = get_block;
954984
dio->end_io = end_io;
985+
dio->submit_io = submit_io;
955986
dio->final_block_in_bio = -1;
956987
dio->next_block_for_io = -1;
957988

@@ -1008,7 +1039,7 @@ direct_io_worker(int rw, struct kiocb *iocb, struct inode *inode,
10081039
}
10091040
} /* end iovec loop */
10101041

1011-
if (ret == -ENOTBLK && (rw & WRITE)) {
1042+
if (ret == -ENOTBLK) {
10121043
/*
10131044
* The remaining part of the request will be
10141045
* be handled by buffered I/O when we return
@@ -1110,7 +1141,7 @@ ssize_t
11101141
__blockdev_direct_IO(int rw, struct kiocb *iocb, struct inode *inode,
11111142
struct block_device *bdev, const struct iovec *iov, loff_t offset,
11121143
unsigned long nr_segs, get_block_t get_block, dio_iodone_t end_io,
1113-
int flags)
1144+
dio_submit_t submit_io, int flags)
11141145
{
11151146
int seg;
11161147
size_t size;
@@ -1197,7 +1228,8 @@ __blockdev_direct_IO(int rw, struct kiocb *iocb, struct inode *inode,
11971228
(end > i_size_read(inode)));
11981229

11991230
retval = direct_io_worker(rw, iocb, inode, iov, offset,
1200-
nr_segs, blkbits, get_block, end_io, dio);
1231+
nr_segs, blkbits, get_block, end_io,
1232+
submit_io, dio);
12011233

12021234
/*
12031235
* In case of error extending write may have instantiated a few

‎include/linux/fs.h‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2250,10 +2250,15 @@ static inline int xip_truncate_page(struct address_space *mapping, loff_t from)
22502250
#endif
22512251

22522252
#ifdef CONFIG_BLOCK
2253+
struct bio;
2254+
typedef void (dio_submit_t)(int rw, struct bio *bio, struct inode *inode,
2255+
loff_t file_offset);
2256+
void dio_end_io(struct bio *bio, int error);
2257+
22532258
ssize_t __blockdev_direct_IO(int rw, struct kiocb *iocb, struct inode *inode,
22542259
struct block_device *bdev, const struct iovec *iov, loff_t offset,
22552260
unsigned long nr_segs, get_block_t get_block, dio_iodone_t end_io,
2256-
int lock_type);
2261+
dio_submit_t submit_io, int lock_type);
22572262

22582263
enum {
22592264
/* need locking between buffered and direct access */
@@ -2269,7 +2274,7 @@ static inline ssize_t blockdev_direct_IO(int rw, struct kiocb *iocb,
22692274
dio_iodone_t end_io)
22702275
{
22712276
return __blockdev_direct_IO(rw, iocb, inode, bdev, iov, offset,
2272-
nr_segs, get_block, end_io,
2277+
nr_segs, get_block, end_io, NULL,
22732278
DIO_LOCKING | DIO_SKIP_HOLES);
22742279
}
22752280

@@ -2279,7 +2284,7 @@ static inline ssize_t blockdev_direct_IO_no_locking(int rw, struct kiocb *iocb,
22792284
dio_iodone_t end_io)
22802285
{
22812286
return __blockdev_direct_IO(rw, iocb, inode, bdev, iov, offset,
2282-
nr_segs, get_block, end_io, 0);
2287+
nr_segs, get_block, end_io, NULL, 0);
22832288
}
22842289
#endif
22852290

0 commit comments

Comments
 (0)