fs: allow short direct-io reads to be completed via buffered IO
Subsystems (8)
PATCH 1/6 fs: allow short direct-io reads to be completed via buffered IO
This is similar to what already happens in the write case. If we have a short read while doing O_DIRECT, instead of just returning, fallthrough and try to read the rest via buffered IO. BTRFS needs this because if we encounter a compressed or inline extent during DIO, we need to fallback on buffered. If the extent is compressed we need to read the entire thing into memory and de-compress it into the users pages. I have tested this with fsx and everything works great. Thanks,
---
V1->V2: Check to see if our current ppos is >= i_size after a short DIO read, just in case it was actually a short read and we need to just return.
mm/filemap.c | 36 +++++++++++++++++++++++++++++++----- 1 files changed, 31 insertions(+), 5 deletions(-)
mm/filemap.c +31 -5
@@ -1263,7 +1263,7 @@ generic_file_aio_read(struct kiocb *iocb, const struct iovec *iov, {struct file *filp = iocb->ki_filp;ssize_t retval; -unsigned long seg; +unsigned long seg =0 ;size_t count;loff_t *ppos = &iocb->ki_pos;
@@ -1290,21 +1290,47 @@ generic_file_aio_read(struct kiocb *iocb, const struct iovec *iov, retval = mapping->a_ops->direct_IO (READ , iocb, iov, pos, nr_segs); } -if (retval >0 ) +if (retval >0 ) { *ppos = pos + retval; -if (retval) { + count -= retval; + } + +/* +* Btrfs can have a short DIO read if we encounter +* compressed extents, so if there was an error, or if +* we've already read everything we wanted to, or if +* there was a short read because we hit EOF, go ahead +* and return. Otherwise fallthrough to buffered io for +* the rest of the read. +*/ +if (retval <0 || !count || *ppos >= size) {file_accessed (filp); goto out; } } } + count = retval;for (seg =0 ; seg < nr_segs; seg++) {read_descriptor_t desc; +loff_t offset =0 ; + +/* +* If we did a short DIO read we need to skip the section of the +* iov that we've already read data into. +*/ +if (count) { +if (count > iov[seg].iov_len) { + count -= iov[seg].iov_len; +continue ; + } + offset = count; + count =0 ; + } desc.written =0 ; - desc.arg.buf = iov[seg].iov_base; - desc.count = iov[seg].iov_len; + desc.arg.buf = iov[seg].iov_base + offset; + desc.count = iov[seg].iov_len - offset;if (desc.count ==0 )continue ; desc.error =0 ;
Signature · 1 line
1.6.6.1
PATCH 2/6 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,
---V1->V2:
-Changed dio_end_io to EXPORT_SYMBOL_GPL
-Removed the own_submit blockdev dio helper
-Removed the boundary change
V2->V3:
-Keep track of the logical offset in the current bio
V3->V4:
-Kerneldoc dio_end_io
-switch if (!dio->submit_io) statement
fs/direct-io.c | 42 +++++++++++++++++++++++++++++++++++++----- include/linux/fs.h | 11 ++++++++--- 2 files changed, 45 insertions(+), 8 deletions(-)
fs/direct-io.c +37 -5
@@ -82,6 +82,8 @@ struct dio {int reap_counter;/* rate limit reaping */ get_block_t *get_block;/* block mapping function */ dio_iodone_t *end_io;/* IO completion function */ +dio_submit_t *submit_io;/* IO submition function */ +loff_t logical_offset_in_bio;/* current first logical block in bio */ sector_t final_block_in_bio;/* current final block in bio + 1 */ sector_t next_block_for_io;/* next block to be put under IO, in dio_blocks units */
@@ -96,6 +98,7 @@ struct dio {unsigned cur_page_offset;/* Offset into it, in bytes */ unsigned cur_page_len;/* Nr of bytes at cur_page_offset */ sector_t cur_page_block;/* Where it starts */ +loff_t cur_page_fs_offset;/* Offset in file */ /* BIO completion state */ spinlock_t bio_lock;/* protects BIO fields below */
@@ -300,6 +303,26 @@ static void dio_bio_end_io(struct bio *bio, int error)spin_unlock_irqrestore (&dio->bio_lock, flags); } +/** +* dio_end_io - handle the end io action for the given bio +* @bio: The direct io bio thats being completed +* @error: Error if there was one +* +* This is meant to be called by any filesystem that uses their own dio_submit_t +* so that the DIO specific endio actions are dealt with after the filesystem +* has done it's completion work. +*/ +void dio_end_io (struct bio *bio,int error) +{ +struct dio *dio = bio->bi_private; + +if (dio->is_async) +dio_bio_end_aio (bio, error); +else +dio_bio_end_io (bio, error); +} +EXPORT_SYMBOL_GPL (dio_end_io); +static int dio_bio_alloc (struct dio *dio,struct block_device *bdev,sector_t first_sector,int nr_vecs)
@@ -316,6 +339,7 @@ dio_bio_alloc(struct dio *dio, struct block_device *bdev, bio->bi_end_io = dio_bio_end_io; dio->bio = bio; + dio->logical_offset_in_bio = dio->cur_page_fs_offset;return 0 ; }
@@ -340,10 +364,15 @@ static void dio_bio_submit(struct dio *dio)if (dio->is_async && dio->rw ==READ )bio_set_pages_dirty (bio); -submit_bio (dio->rw, bio); +if (dio->submit_io) + dio->submit_io (dio->rw, bio, dio->inode, + dio->logical_offset_in_bio); +else +submit_bio (dio->rw, bio); dio->bio =NULL ; dio->boundary =0 ; + dio->logical_offset_in_bio =0 ; }/*
@@ -701,6 +730,7 @@ submit_page_section(struct dio *dio, struct page *page, dio->cur_page_offset = offset; dio->cur_page_len = len; dio->cur_page_block = blocknr; + dio->cur_page_fs_offset = dio->block_in_file << dio->blkbits; out:return ret; }
@@ -935,7 +965,7 @@ static ssize_t direct_io_worker(int rw, struct kiocb *iocb, struct inode *inode, const struct iovec *iov, loff_t offset, unsigned long nr_segs, unsigned blkbits, get_block_t get_block, dio_iodone_t end_io, - struct dio *dio) + dio_submit_t submit_io, struct dio *dio) { unsigned long user_addr;unsigned long flags;
@@ -952,6 +982,7 @@ direct_io_worker(int rw, struct kiocb *iocb, struct inode *inode, dio->get_block = get_block; dio->end_io = end_io; + dio->submit_io = submit_io; dio->final_block_in_bio =-1 ; dio->next_block_for_io =-1 ;
@@ -1008,7 +1039,7 @@ direct_io_worker(int rw, struct kiocb *iocb, struct inode *inode, } }/* end iovec loop */ -if (ret == -ENOTBLK && (rw &WRITE )) { +if (ret == -ENOTBLK ) {/* * The remaining part of the request will be * be handled by buffered I/O when we return
@@ -1110,7 +1141,7 @@ ssize_t __blockdev_direct_IO(int rw, struct kiocb *iocb, struct inode *inode, struct block_device *bdev, const struct iovec *iov, loff_t offset, unsigned long nr_segs, get_block_t get_block, dio_iodone_t end_io, - int flags) + dio_submit_t submit_io, int flags) { int seg;size_t size;
@@ -1197,7 +1228,8 @@ __blockdev_direct_IO(int rw, struct kiocb *iocb, struct inode *inode, (end > i_size_read(inode))); retval =direct_io_worker (rw, iocb, inode, iov, offset, - nr_segs, blkbits, get_block, end_io, dio); + nr_segs, blkbits, get_block, end_io, + submit_io, dio);/* * In case of error extending write may have instantiated a few
include/linux/fs.h +8 -3
@@ -2250,10 +2250,15 @@ static inline int xip_truncate_page(struct address_space *mapping, loff_t from)#endif #ifdef CONFIG_BLOCK +struct bio ; +typedef void (dio_submit_t )(int rw,struct bio *bio,struct inode *inode, +loff_t file_offset); +void dio_end_io (struct bio *bio,int error); +ssize_t __blockdev_direct_IO (int rw,struct kiocb *iocb,struct inode *inode,struct block_device *bdev,const struct iovec *iov,loff_t offset,unsigned long nr_segs,get_block_t get_block,dio_iodone_t end_io, -int lock_type); +dio_submit_t submit_io,int lock_type);enum { /* need locking between buffered and direct access */
@@ -2269,7 +2274,7 @@ static inline ssize_t blockdev_direct_IO(int rw, struct kiocb *iocb, dio_iodone_t end_io) {return __blockdev_direct_IO (rw, iocb, inode, bdev, iov, offset, - nr_segs, get_block, end_io, + nr_segs, get_block, end_io,NULL ,DIO_LOCKING |DIO_SKIP_HOLES ); }
@@ -2279,7 +2284,7 @@ static inline ssize_t blockdev_direct_IO_no_locking(int rw, struct kiocb *iocb, dio_iodone_t end_io) {return __blockdev_direct_IO (rw, iocb, inode, bdev, iov, offset, - nr_segs, get_block, end_io,0 ); + nr_segs, get_block, end_io,NULL ,0 ); }#endif
Signature · 1 line
1.6.6.1
PATCH 3/6 direct-io: do not merge logically non-contiguous requests
In reply to Josef BacikBtrfs cannot handle having logically non-contiguous requests submitted. For example if you have
Logical: [0-4095][HOLE][8192-12287] Physical: [0-4095] [4096-8191]
Normally the DIO code would put these into the same BIO's. The problem is we need to know exactly what offset is associated with what BIO so we can do our checksumming and unlocking properly, so putting them in the same BIO doesn't work. So add another check where we submit the current BIO if the physical blocks are not contigous OR the logical blocks are not contiguous.
---
V1->V2
-Be more verbose in the in-code comment
fs/direct-io.c | 20 ++++++++++++++++++-- 1 files changed, 18 insertions(+), 2 deletions(-)
fs/direct-io.c +18 -2
@@ -632,10 +632,26 @@ static int dio_send_cur_page(struct dio *dio)int ret =0 ;if (dio->bio) { +loff_t cur_offset = dio->block_in_file << dio->blkbits; +loff_t bio_next_offset = dio->logical_offset_in_bio + + dio->bio->bi_size; +/* -* See whether this new request is contiguous with the old +* See whether this new request is contiguous with the old. +* +* Btrfs cannot handl having logically non-contiguous requests +* submitted. For exmple if you have +* +* Logical: [0-4095][HOLE][8192-12287] +* Phyiscal: [0-4095] [4096-8181] +* +* We cannot submit those pages together as one BIO. So if our +* current logical offset in the file does not equal what would +* be the next logical offset in the bio, submit the bio we +* have. */ -if (dio->final_block_in_bio != dio->cur_page_block) +if (dio->final_block_in_bio != dio->cur_page_block || + cur_offset != bio_next_offset)dio_bio_submit (dio);/* * Submit now if the underlying fs is about to perform a
Signature · 1 line
1.6.6.1
Re: [PATCH 3/6] direct-io: do not merge logically non-contiguous requests
Quoted 1 line · Josef Bacik
Btrfs cannot handle having logically non-contiguous requests submitte=
d. =C2=A0For
Quoted 6 lines
example if you have
Logical: =C2=A0[0-4095][HOLE][8192-12287]
Physical: [0-4095] =C2=A0 =C2=A0 =C2=A0[4096-8191]Normally the DIO code would put these into the same BIO's. =C2=A0The =
problem is we
Quoted 1 line
need to know exactly what offset is associated with what BIO so we ca=
n do our
Quoted 1 line
checksumming and unlocking properly, so putting them in the same BIO =
doesn't
Quoted 1 line
work. =C2=A0So add another check where we submit the current BIO if t=
he physical
Quoted 11 lines
blocks are not contigous OR the logical blocks are not contiguous.
Signed-off-by Josef Bacik <[email protected]>---
V1->V2
-Be more verbose in the in-code comment=C2=A0fs/direct-io.c | =C2=A0 20 ++++++++++++++++++-- =C2=A01 files changed, 18 insertions(+), 2 deletions(-)
Btrfs has been pretty much self-contained (working well compiled against 2.6.32 for example). Is there a way that this wouldn't just start silently breaking for people compiling the latest btrfs with dkms against older kernels?
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" = in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 3/6] direct-io: do not merge logically non-contiguous requests
Quoted 19 lines · Mike Fedyk
Quoted 18 lines · Josef Bacik
Btrfs cannot handle having logically non-contiguous requests submitted. For example if you have
Logical: [0-4095][HOLE][8192-12287]
Physical: [0-4095] [4096-8191]Normally the DIO code would put these into the same BIO's. The problem is we need to know exactly what offset is associated with what BIO so we can do our checksumming and unlocking properly, so putting them in the same BIO doesn't work. So add another check where we submit the current BIO if the physical blocks are not contigous OR the logical blocks are not contiguous.
Signed-off-by Josef Bacik <[email protected]>---
V1->V2
-Be more verbose in the in-code comment
Quoted 8 lines
Quoted 3 lines
fs/direct-io.c | 20 ++++++++++++++++++--
1 files changed, 18 insertions(+), 2 deletions(-)Btrfs has been pretty much self-contained (working well compiled against 2.6.32 for example). Is there a way that this wouldn't just start silently breaking for people compiling the latest btrfs with dkms against older kernels?
Nope, you have to have these generic patches for DIO to work, so building btrfs like this will stop working with earlier kernels. Thanks,
Josef
--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH 4/6 fs: kill blockdev_direct_IO_no_locking
In reply to Josef BacikChristoph said he'd rather everybody use __blockdev_direct_IO directly instead of having a bunch of random helper functions, so thats what this patch does. It's a basic change, I've tested it with xfstests on ext4 and xfs. Thanks,
--- fs/block_dev.c | 5 +++-- fs/ext4/inode.c | 8 ++++---- fs/gfs2/aops.c | 6 +++--- fs/ocfs2/aops.c | 8 +++----- fs/xfs/linux-2.6/xfs_aops.c | 7 +++---- include/linux/fs.h | 9 --------- 6 files changed, 16 insertions(+), 27 deletions(-)
fs/block_dev.c +3 -2
@@ -172,8 +172,9 @@ blkdev_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,struct file *file = iocb->ki_filp;struct inode *inode = file->f_mapping->host; -return blockdev_direct_IO_no_locking (rw, iocb, inode,I_BDEV (inode), - iov, offset, nr_segs, blkdev_get_blocks,NULL ); +return __blockdev_direct_IO (rw, iocb, inode,I_BDEV (inode), iov, + offset, nr_segs, blkdev_get_blocks,NULL , +NULL ,0 ); }int __sync_blockdev (struct block_device *bdev,int wait)
fs/ext4/inode.c +4 -4
@@ -3494,10 +3494,10 @@ static ssize_t ext4_ind_direct_IO(int rw, struct kiocb *iocb, retry:if (rw ==READ &&ext4_should_dioread_nolock (inode)) - ret =blockdev_direct_IO_no_locking (rw, iocb, inode, - inode->i_sb->s_bdev, iov, - offset, nr_segs, - ext4_get_block,NULL ); + ret =__blockdev_direct_IO (rw, iocb, inode, + inode->i_sb->s_bdev, iov, offset, + nr_segs, ext4_get_block,NULL ,NULL , +0 );else ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
fs/gfs2/aops.c +3 -3
@@ -1039,9 +1039,9 @@ static ssize_t gfs2_direct_IO(int rw, struct kiocb *iocb,if (rv !=1 ) goto out;/* dio not valid, fall back to buffered i/o */ - rv =blockdev_direct_IO_no_locking (rw, iocb, inode, inode->i_sb->s_bdev, - iov, offset, nr_segs, - gfs2_get_block_direct,NULL ); + rv =__blockdev_direct_IO (rw, iocb, inode, inode->i_sb->s_bdev, + iov, offset, nr_segs, gfs2_get_block_direct, +NULL ,NULL ,0 ); out:gfs2_glock_dq_m (1 , &gh);gfs2_holder_uninit (&gh);
fs/ocfs2/aops.c +3 -5
@@ -669,11 +669,9 @@ static ssize_t ocfs2_direct_IO(int rw,if (i_size_read (inode) <= offset)return 0 ; - ret =blockdev_direct_IO_no_locking (rw, iocb, inode, - inode->i_sb->s_bdev, iov, offset, - nr_segs, - ocfs2_direct_IO_get_blocks, - ocfs2_dio_end_io); + ret =__blockdev_direct_IO (rw, iocb, inode, inode->i_sb->s_bdev, iov, + offset, nr_segs, ocfs2_direct_IO_get_blocks, + ocfs2_dio_end_io,NULL ,0 );mlog_exit (ret);return ret;
fs/xfs/linux-2.6/xfs_aops.c +3 -4
@@ -1617,10 +1617,9 @@ xfs_vm_direct_IO( iocb->private =xfs_alloc_ioend (inode, rw ==WRITE ?IOMAP_UNWRITTEN :IOMAP_READ ); - ret =blockdev_direct_IO_no_locking (rw, iocb, inode, bdev, iov, - offset, nr_segs, - xfs_get_blocks_direct, - xfs_end_io_direct); + ret =__blockdev_direct_IO (rw, iocb, inode, bdev, iov, offset, nr_segs, + xfs_get_blocks_direct, xfs_end_io_direct, +NULL ,0 );if (unlikely (ret != -EIOCBQUEUED && iocb->private))xfs_destroy_ioend (iocb->private);
include/linux/fs.h +0 -9
@@ -2277,15 +2277,6 @@ static inline ssize_t blockdev_direct_IO(int rw, struct kiocb *iocb, nr_segs, get_block, end_io,NULL , DIO_LOCKING | DIO_SKIP_HOLES); } - -static inline ssize_t blockdev_direct_IO_no_locking (int rw,struct kiocb *iocb, -struct inode *inode,struct block_device *bdev,const struct iovec *iov, -loff_t offset,unsigned long nr_segs,get_block_t get_block, -dio_iodone_t end_io) -{ -return __blockdev_direct_IO (rw, iocb, inode, bdev, iov, offset, - nr_segs, get_block, end_io,NULL ,0 ); -}#endif extern const struct file_operations generic_ro_fops;
Signature · 1 line
1.6.6.1
PATCH 5/6 Btrfs: add basic DIO read/write support
In reply to Josef BacikThis provides basic DIO support for reading and writing. It does not do the work to recover from mismatching checksums, that will come later. A few design changes have been made from Jim's code (sorry Jim!)
1) Use the generic direct-io code. Jim originally re-wrote all the generic DIO code in order to account for all of BTRFS's oddities, but thanks to that work it seems like the best bet is to just ignore compression and such and just opt to fallback on buffered IO.
2) Fallback on buffered IO for compressed or inline extents. Jim's code did it's own buffering to make dio with compressed extents work. Now we just fallback onto normal buffered IO.
3) Use ordered extents for the writes so that all of the
lock_extent()lookup_ordered()
type checks continue to work.
4) Do the lock_extent() lookup_ordered() loop in readpage so we don't race with DIO writes.
I've tested this with fsx and everything works great. This patch depends on my dio and filemap.c patches to work. Thanks,
--- fs/btrfs/ctree.h | 2 + fs/btrfs/file-item.c | 25 ++- fs/btrfs/file.c | 69 +++++++- fs/btrfs/inode.c | 486 ++++++++++++++++++++++++++++++++++++++++++++--- fs/btrfs/ordered-data.c | 75 +++++++- fs/btrfs/ordered-data.h | 9 +- 6 files changed, 630 insertions(+), 36 deletions(-)
fs/btrfs/ctree.h +2 -0
@@ -2257,6 +2257,8 @@ int btrfs_del_csums(struct btrfs_trans_handle *trans, struct btrfs_root *root, u64 bytenr, u64 len);int btrfs_lookup_bio_sums (struct btrfs_root *root,struct inode *inode,struct bio *bio,u32 *dst); +int btrfs_lookup_bio_sums_dio (struct btrfs_root *root,struct inode *inode, +struct bio *bio,u64 logical_offset,u32 *dst); int btrfs_insert_file_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root, u64 objectid, u64 pos,
fs/btrfs/file-item.c +21 -4
@@ -149,13 +149,14 @@ int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans, } -int btrfs_lookup_bio_sums (struct btrfs_root *root,struct inode *inode, -struct bio *bio,u32 *dst) +static int __btrfs_lookup_bio_sums (struct btrfs_root *root, +struct inode *inode,struct bio *bio, +u64 logical_offset,u32 *dst,int dio) {u32 sum;struct bio_vec *bvec = bio->bi_io_vec;int bio_index =0 ; -u64 offset; +u64 offset =0 ;u64 item_start_offset =0 ;u64 item_last_offset =0 ;u64 disk_bytenr;
@@ -174,8 +175,11 @@ int btrfs_lookup_bio_sums(struct btrfs_root *root, struct inode *inode,WARN_ON (bio->bi_vcnt <=0 ); disk_bytenr = (u64 )bio->bi_sector <<9 ; +if (dio) + offset = logical_offset;while (bio_index < bio->bi_vcnt) { - offset =page_offset (bvec->bv_page) + bvec->bv_offset; +if (!dio) + offset =page_offset (bvec->bv_page) + bvec->bv_offset; ret =btrfs_find_ordered_sum (inode, offset, disk_bytenr, &sum);if (ret ==0 ) goto found;
@@ -238,6 +242,7 @@ found:else set_state_private (io_tree, offset, sum); disk_bytenr += bvec->bv_len; + offset += bvec->bv_len; bio_index++; bvec++; }
@@ -245,6 +250,18 @@ found:return 0 ; } +int btrfs_lookup_bio_sums (struct btrfs_root *root,struct inode *inode, +struct bio *bio,u32 *dst) +{ +return __btrfs_lookup_bio_sums (root, inode, bio,0 , dst,0 ); +} + +int btrfs_lookup_bio_sums_dio (struct btrfs_root *root,struct inode *inode, +struct bio *bio,u64 offset,u32 *dst) +{ +return __btrfs_lookup_bio_sums (root, inode, bio, offset, dst,1 ); +} +int btrfs_lookup_csums_range (struct btrfs_root *root,u64 start,u64 end,struct list_head *list) {
fs/btrfs/file.c +65 -4
103 lines not on this page · raw message
fs/btrfs/inode.c +462 -24
508 lines not on this page · raw message
fs/btrfs/ordered-data.c +72 -3
111 lines not on this page · raw message
fs/btrfs/ordered-data.h +8 -1
@@ -72,6 +72,8 @@ struct btrfs_ordered_sum {#define BTRFS_ORDERED_PREALLOC 4/* set when writing to prealloced extent */ +#define BTRFS_ORDERED_DIRECT 5/* set when we're doing DIO with this extent */ +struct btrfs_ordered_extent {/* logical offset in the file */ u64 file_offset;
@@ -140,7 +142,9 @@ int btrfs_dec_test_ordered_pending(struct inode *inode,struct btrfs_ordered_extent **cached, u64 file_offset, u64 io_size);int btrfs_add_ordered_extent (struct inode *inode,u64 file_offset, -u64 start,u64 len,u64 disk_len,int tyep); +u64 start,u64 len,u64 disk_len,int type); +int btrfs_add_ordered_extent_dio (struct inode *inode,u64 file_offset, +u64 start,u64 len,u64 disk_len,int type);int btrfs_add_ordered_sum (struct inode *inode,struct btrfs_ordered_extent *entry,struct btrfs_ordered_sum *sum);
@@ -151,6 +155,9 @@ void btrfs_start_ordered_extent(struct inode *inode,int btrfs_wait_ordered_range (struct inode *inode,u64 start,u64 len);struct btrfs_ordered_extent *btrfs_lookup_first_ordered_extent (struct inode * inode,u64 file_offset); +struct btrfs_ordered_extent *btrfs_lookup_ordered_range (struct inode *inode, +u64 file_offset, +u64 len);int btrfs_ordered_update_i_size (struct inode *inode,u64 offset,struct btrfs_ordered_extent *ordered);int btrfs_find_ordered_sum (struct inode *inode,u64 offset,u64 disk_bytenr,u32 *sum);
Signature · 1 line
1.6.6.1
PATCH 6/6 Btrfs: do aio_write instead of write
In reply to Josef BacikIn order for AIO to work, we need to implement aio_write. This patch converts our btrfs_file_write to btrfs_aio_write. I've tested this with xfstests and nothing broke, and the AIO stuff magically started working. Thanks,
--- fs/btrfs/extent_io.c | 11 +++- fs/btrfs/file.c | 152 +++++++++++++++++++++++--------------------------- 2 files changed, 80 insertions(+), 83 deletions(-)
fs/btrfs/extent_io.c +10 -1
@@ -2020,6 +2020,7 @@ static int __extent_read_full_page(struct extent_io_tree *tree,sector_t sector;struct extent_map *em;struct block_device *bdev; +struct btrfs_ordered_extent *ordered;int ret;int nr =0 ;size_t page_offset =0 ;
@@ -2031,7 +2032,15 @@ static int __extent_read_full_page(struct extent_io_tree *tree,set_page_extent_mapped (page); end = page_end; -lock_extent (tree, start, end,GFP_NOFS ); +while (1 ) { +lock_extent (tree, start, end,GFP_NOFS ); + ordered =btrfs_lookup_ordered_extent (inode, start); +if (!ordered) +break ; +unlock_extent (tree, start, end,GFP_NOFS ); +btrfs_start_ordered_extent (inode, ordered,1 ); +btrfs_put_ordered_extent (ordered); + }if (page->index == last_byte >>PAGE_CACHE_SHIFT ) {char *userpage;
fs/btrfs/file.c +70 -82
246 lines not on this page · raw message
Signature · 1 line
1.6.6.1
Re: [PATCH 6/6] Btrfs: do aio_write instead of write
Quoted 3 lines · Josef Bacik
In order for AIO to work, we need to implement
aio_write. This patch converts ourbtrfs_file_writetobtrfs_aio_write. I've tested this with xfstests and nothing broke, and the AIO stuff magically started working. Thanks,
But xfstests's case 198(source: src/aio-dio-regress/aiodio_sparse2.c) still failed,
following message outputted.
--------------------
AIO write offset 0 expected 65536 got -22
AIO write offset 5242880 expected 65536 got -22
AIO write offset 10485760 expected 65536 got -22
AIO write offset 15728640 expected 65536 got -22
AIO write offset 20971520 expected 65536 got -22
AIO write offset 31457280 expected 65536 got -22
AIO write offset 36700160 expected 65536 got -22
AIO write offset 41943040 expected 65536 got -22
AIO write offset 47185920 expected 65536 got -22
AIO write offset 52428800 expected 65536 got -22
AIO write offset 57671680 expected 65536 got -22
AIO write offset 62914560 expected 65536 got -22
AIO write offset 73400320 expected 65536 got -22
AIO write offset 78643200 expected 65536 got -22
non one buffer at buf[0] => 0x00,00,00,00
non-one read at offset 0
*** WARNING *** /tmp/aaaa has not been unlinked; if you don't rm it manually first, it may influence the next run
--------------------
generic_file_direct_write()(in btrfs_file_aio_write(), fs/btrfs/file.c) returned -22, maybe it's useful for your analysing.
Thanks.
Quoted 297 lines
Signed-off-by Josef Bacik <[email protected]>--- fs/btrfs/extent_io.c | 11 +++- fs/btrfs/file.c | 152 +++++++++++++++++++++++--------------------------- 2 files changed, 80 insertions(+), 83 deletions(-)fs/btrfs/extent_io.c +10
-1@@ -2020,6 +2020,7 @@ static int __extent_read_full_page(struct extent_io_tree *tree,sector_t sector;struct extent_map *em;struct block_device *bdev; +struct btrfs_ordered_extent *ordered;int ret;int nr =0 ;size_t page_offset =0 ;@@ -2031,7 +2032,15 @@ static int __extent_read_full_page(struct extent_io_tree *tree,set_page_extent_mapped (page); end = page_end; -lock_extent (tree, start, end,GFP_NOFS ); +while (1 ) { +lock_extent (tree, start, end,GFP_NOFS ); + ordered =btrfs_lookup_ordered_extent (inode, start); +if (!ordered) +break ; +unlock_extent (tree, start, end,GFP_NOFS ); +btrfs_start_ordered_extent (inode, ordered,1 ); +btrfs_put_ordered_extent (ordered); + }if (page->index == last_byte >>PAGE_CACHE_SHIFT ) {char *userpage;fs/btrfs/file.c +70
-82@@ -46,32 +46,42 @@ static noinline int btrfs_copy_from_user(loff_t pos, int num_pages,int write_bytes,struct page **prepared_pages, -const char __user *buf) +struct iov_iter *i) { -long page_fault =0 ; -int i; +size_t copied; +int pg =0 ;int offset = pos & (PAGE_CACHE_SIZE -1 ); -for (i =0 ; i < num_pages && write_bytes >0 ; i++, offset =0 ) { +while (write_bytes >0 ) {size_t count =min_t (size_t,PAGE_CACHE_SIZE - offset, write_bytes); -struct page *page = prepared_pages[i]; -fault_in_pages_readable (buf, count); +struct page *page = prepared_pages[pg]; +again: +if (unlikely (iov_iter_fault_in_readable (i, count))) +return -EFAULT ;/* Copy data from userspace to the current page */ -kmap (page); - page_fault =__copy_from_user (page_address (page) + offset, - buf, count); + copied =iov_iter_copy_from_user (page, i, offset, count); +/* Flush processor's dcache for this page */ flush_dcache_page (page); -kunmap (page); - buf += count; - write_bytes -= count; +iov_iter_advance (i, copied); + write_bytes -= copied; -if (page_fault) -break ; +if (unlikely (copied ==0 )) { + count =min_t (size_t,PAGE_CACHE_SIZE - offset, +iov_iter_single_seg_count (i)); + goto again; + } + +if (unlikely (copied <PAGE_CACHE_SIZE - offset)) { + offset += copied; + }else { + pg++; + offset =0 ; + } } -return page_fault ? -EFAULT :0 ; +return 0 ; }/* @@ -823,60 +833,24 @@ again:return 0 ; } -/* Copied from read-write.c */ -static void wait_on_retry_sync_kiocb (struct kiocb *iocb) -{ -set_current_state (TASK_UNINTERRUPTIBLE ); -if (!kiocbIsKicked (iocb)) -schedule (); -else -kiocbClearKicked (iocb); -__set_current_state (TASK_RUNNING ); -} - -/* -* Just a copy of what do_sync_write does. -*/ -static ssize_t __btrfs_direct_write (struct file *file,const char __user *buf, -size_t count,loff_t pos,loff_t *ppos) -{ -struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count }; -unsigned long nr_segs =1 ; -struct kiocb kiocb; -ssize_t ret; - -init_sync_kiocb (&kiocb, file); - kiocb.ki_pos = pos; - kiocb.ki_left = count; - kiocb.ki_nbytes = count; - -while (1 ) { - ret =generic_file_direct_write (&kiocb, &iov, &nr_segs, pos, - ppos, count, count); -if (ret != -EIOCBRETRY ) -break ; -wait_on_retry_sync_kiocb (&kiocb); - } - -if (ret == -EIOCBQUEUED ) - ret =wait_on_sync_kiocb (&kiocb); - *ppos = kiocb.ki_pos; -return ret; -} - -static ssize_t btrfs_file_write (struct file *file,const char __user *buf, -size_t count,loff_t *ppos) +static ssize_t btrfs_file_aio_write (struct kiocb *iocb, +const struct iovec *iov, +unsigned long nr_segs,loff_t pos) { -loff_t pos; +struct file *file = iocb->ki_filp; +struct inode *inode =fdentry (file)->d_inode; +struct btrfs_root *root =BTRFS_I (inode)->root; +struct page *pinned[2 ]; +struct page **pages =NULL ; +struct iov_iter i; +loff_t *ppos = &iocb->ki_pos;loff_t start_pos;ssize_t num_written =0 ;ssize_t err =0 ; +size_t count; +size_t ocount;int ret =0 ; -struct inode *inode =fdentry (file)->d_inode; -struct btrfs_root *root =BTRFS_I (inode)->root; -struct page **pages =NULL ;int nrptrs; -struct page *pinned[2 ];unsigned long first_index;unsigned long last_index;int will_write;@@ -888,7 +862,6 @@ static ssize_t btrfs_file_write(struct file *file, const char __user *buf, pinned[0 ] =NULL ; pinned[1 ] =NULL ; - pos = *ppos; start_pos = pos;vfs_check_frozen (inode->i_sb,SB_FREEZE_WRITE );@@ -902,6 +875,11 @@ static ssize_t btrfs_file_write(struct file *file, const char __user *buf,mutex_lock (&inode->i_mutex); + err =generic_segment_checks (iov, &nr_segs, &ocount,VERIFY_READ ); +if (err) + goto out; + count = ocount; + current->backing_dev_info = inode->i_mapping->backing_dev_info; err =generic_write_checks (file, &pos, &count,S_ISBLK (inode->i_mode));if (err)@@ -918,14 +896,24 @@ static ssize_t btrfs_file_write(struct file *file, const char __user *buf,BTRFS_I (inode)->sequence++;if (unlikely (file->f_flags &O_DIRECT )) { - num_written =__btrfs_direct_write (file, buf, count, pos, - ppos); - pos += num_written; - count -= num_written; + ret =btrfs_check_data_free_space (root, inode, count); +if (ret) + goto out; -/* We've written everything we wanted to, exit */ -if (num_written <0 || !count) + num_written =generic_file_direct_write (iocb, iov, &nr_segs, + pos, ppos, count, + ocount); + +/* All reservations for DIO are done internally */ +btrfs_free_reserved_data_space (root, inode, count); + +if (num_written <0 ) { + ret = num_written; + num_written =0 ; + goto out; + }else if (num_written == count) { goto out; + }/* * We are going to do buffered for the rest of the range, so we @@ -933,18 +921,20 @@ static ssize_t btrfs_file_write(struct file *file, const char __user *buf,* done. */ buffered =1 ; - buf += num_written; + pos += num_written; } - nrptrs =min ((count +PAGE_CACHE_SIZE -1 ) /PAGE_CACHE_SIZE , -PAGE_CACHE_SIZE / (sizeof (struct page *))); +iov_iter_init (&i, iov, nr_segs, count, num_written); + nrptrs =min ((iov_iter_count (&i) +PAGE_CACHE_SIZE -1 ) / +PAGE_CACHE_SIZE ,PAGE_CACHE_SIZE / + (sizeof (struct page *))); pages =kmalloc (nrptrs *sizeof (struct page *),GFP_KERNEL );/* generic_write_checks can change our pos */ start_pos = pos; first_index = pos >>PAGE_CACHE_SHIFT ; - last_index = (pos + count) >>PAGE_CACHE_SHIFT ; + last_index = (pos +iov_iter_count (&i)) >>PAGE_CACHE_SHIFT ;/* * there are lots of better ways to do this, but this code @@ -961,7 +951,7 @@ static ssize_t btrfs_file_write(struct file *file, const char __user *buf,unlock_page (pinned[0 ]); } } -if ((pos + count) & (PAGE_CACHE_SIZE -1 )) { +if ((pos +iov_iter_count (&i)) & (PAGE_CACHE_SIZE -1 )) { pinned[1 ] =grab_cache_page (inode->i_mapping, last_index);if (!PageUptodate (pinned[1 ])) { ret =btrfs_readpage (NULL , pinned[1 ]);@@ -972,10 +962,10 @@ static ssize_t btrfs_file_write(struct file *file, const char __user *buf, } } -while (count >0 ) { +while (iov_iter_count (&i) >0 ) {size_t offset = pos & (PAGE_CACHE_SIZE -1 ); -size_t write_bytes =min (count, nrptrs * - (size_t )PAGE_CACHE_SIZE - +size_t write_bytes =min (iov_iter_count (&i), + nrptrs * (size_t )PAGE_CACHE_SIZE - offset);size_t num_pages = (write_bytes +PAGE_CACHE_SIZE -1 ) >>PAGE_CACHE_SHIFT ;@@ -997,7 +987,7 @@ static ssize_t btrfs_file_write(struct file *file, const char __user *buf, } ret =btrfs_copy_from_user (pos, num_pages, - write_bytes, pages, buf); + write_bytes, pages, &i);if (ret) {btrfs_free_reserved_data_space (root, inode, write_bytes);@@ -1026,8 +1016,6 @@ static ssize_t btrfs_file_write(struct file *file, const char __user *buf,btrfs_throttle (root); } - buf += write_bytes; - count -= write_bytes; pos += write_bytes; num_written += write_bytes;@@ -1222,7 +1210,7 @@ const struct file_operations btrfs_file_operations = { .read = do_sync_read, .aio_read = generic_file_aio_read, .splice_read = generic_file_splice_read, - .write = btrfs_file_write, + .aio_write = btrfs_file_aio_write, .mmap = btrfs_file_mmap, .open = generic_file_open, .release = btrfs_release_file,
Signature · 1 line
Shi Weihua
Re: [PATCH 6/6] Btrfs: do aio_write instead of write
Quoted 27 lines · Shi Weihua
Quoted 3 lines · Josef Bacik
In order for AIO to work, we need to implement
aio_write. This patch converts ourbtrfs_file_writetobtrfs_aio_write. I've tested this with xfstests and nothing broke, and the AIO stuff magically started working. Thanks,But xfstests's case 198(source:
src/aio-dio-regress/aiodio_sparse2.c) still failed,
following message outputted.
--------------------
AIO write offset 0 expected 65536 got -22
AIO write offset 5242880 expected 65536 got -22
AIO write offset 10485760 expected 65536 got -22
AIO write offset 15728640 expected 65536 got -22
AIO write offset 20971520 expected 65536 got -22
AIO write offset 31457280 expected 65536 got -22
AIO write offset 36700160 expected 65536 got -22
AIO write offset 41943040 expected 65536 got -22
AIO write offset 47185920 expected 65536 got -22
AIO write offset 52428800 expected 65536 got -22
AIO write offset 57671680 expected 65536 got -22
AIO write offset 62914560 expected 65536 got -22
AIO write offset 73400320 expected 65536 got -22
AIO write offset 78643200 expected 65536 got -22
non one buffer at buf[0] => 0x00,00,00,00
non-one read at offset 0
*** WARNING ***/tmp/aaaahas not been unlinked; if you don't rm it manually first, it may influence the next run
--------------------
Quoted 2 lines
generic_file_direct_write()(in
btrfs_file_aio_write(),fs/btrfs/file.c) returned -22, maybe it's useful for your analysing.
Yes, change that testcase to run -a 4096 and it will run fine. Because BTRFS doesn't pass in a bdev to __blockdev_direct_IO it doesn't do 512 byte aligned IO, just blocksize aligned IO. I will fix that at some later point, but its a little tricky since we have to figure out which bdev has the largest alignment (in case we have a 4k sector device and a 512 byte sector device in the same volume). Thanks,
Josef
Re: [PATCH 6/6] Btrfs: do aio_write instead of write
In reply to Josef BacikQuoted 6 lines · Josef Bacik
In order for AIO to work, we need to implement
aio_write. This patch converts ourbtrfs_file_writetobtrfs_aio_write. I've tested this with xfstests and nothing broke, and the AIO stuff magically started working. Thanks,Signed-off-by Josef Bacik <[email protected]>
Hi, Josef,
I've tested your patch(May 22) with my tools, and one case triggered a bug which made writev operation hang up, more information is followed.
- Steps to trigger it:
# mount /dev/sda8 /home/btrfsdisk -o nodatacow # gcc direct-io.c -o direct-io # ./direct-io O_DIRECT writev /home/btrfsdisk/testrw 4M
then on another tty, after "dmesg"...
[snip]
device fsid f44b0879c75c0e99-1d4b28f2d5c503ae devid 1 transid 11177 /dev/sda8
btrfs: setting nodatacow
INFO: task direct-io:1399 blocked for more than 120 seconds.
"echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message.
direct-io D 0000000000000003 0 1399 1341 0x00000000
ffff880137c379c8 0000000000000082 ffff880137c379d8 ffffffff00000000 ffff880137c37fd8 ffff880139730000 0000000000015440 ffff880137c37fd8 0000000000015440 0000000000015440 0000000000015440 0000000000015440
Call Trace: [<ffffffffa0119d4a>] wait_extent_bit+0xe3/0x163 [btrfs] [<ffffffff8106651f>] ? autoremove_wake_function+0x0/0x39 [<ffffffffa0119e47>] lock_extent_bits+0x7d/0xa8 [btrfs] [<ffffffffa0119e88>] lock_extent+0x16/0x18 [btrfs] [<ffffffffa01025ce>] btrfs_direct_IO+0x8e/0x1be [btrfs] [<ffffffff810c7301>] generic_file_direct_write+0xed/0x16d [<ffffffffa010bb91>] btrfs_file_aio_write+0x2af/0x8d2 [btrfs] [<ffffffff81100eae>] ? try_get_mem_cgroup_from_mm+0x39/0x49 [<ffffffffa010b8e2>] ? btrfs_file_aio_write+0x0/0x8d2 [btrfs] [<ffffffff811063ed>] do_sync_readv_writev+0xc1/0x100 [<ffffffff81106120>] ? might_fault+0x21/0x23 [<ffffffff81106151>] ? copy_from_user+0x2f/0x31 [<ffffffff811c90ab>] ? security_file_permission+0x16/0x18 [<ffffffff81107145>] do_readv_writev+0xa7/0x127 [<ffffffff81107208>] vfs_writev+0x43/0x4e [<ffffffff811072f8>] sys_writev+0x4a/0x93 [<ffffffff81009c32>] system_call_fastpath+0x16/0x1b
So, can you figure out if anything in your patch leads to the bug?
Re: [PATCH 6/6] Btrfs: do aio_write instead of write
Quoted 52 lines · liubo
Quoted 7 lines · Josef Bacik
In order for AIO to work, we need to implement
aio_write. This patch converts ourbtrfs_file_writetobtrfs_aio_write. I've tested this with xfstests and nothing broke, and the AIO stuff magically started working. Thanks,Signed-off-by Josef Bacik <[email protected]>Hi, Josef,
I've tested your patch(May 22) with my tools, and one case triggered a bug which made writev operation hang up, more information is followed.
- Steps to trigger it:
# mount /dev/sda8 /home/btrfsdisk -o nodatacow # gcc direct-io.c -o direct-io # ./direct-io O_DIRECT writev /home/btrfsdisk/testrw 4Mthen on another tty, after "dmesg"...
[snip]
device fsid f44b0879c75c0e99-1d4b28f2d5c503ae devid 1 transid 11177/dev/sda8
btrfs: setting nodatacowINFO: task direct-io:1399 blocked for more than 120 seconds."echo 0 >
/proc/sys/kernel/hung_task_timeout_secs" disables this message.
direct-io D 0000000000000003 0 1399 13410x00000000
ffff880137c379c80000000000000082ffff880137c379d8ffffffff00000000ffff880137c37fd8ffff8801397300000000000000015440ffff880137c37fd80000000000015440 0000000000015440 0000000000015440 0000000000015440Call Trace: [<ffffffffa0119d4a>] wait_extent_bit+0xe3/0x163 [btrfs] [<ffffffff8106651f>] ? autoremove_wake_function+0x0/0x39 [<ffffffffa0119e47>] lock_extent_bits+0x7d/0xa8 [btrfs] [<ffffffffa0119e88>] lock_extent+0x16/0x18 [btrfs] [<ffffffffa01025ce>] btrfs_direct_IO+0x8e/0x1be [btrfs] [<ffffffff810c7301>] generic_file_direct_write+0xed/0x16d [<ffffffffa010bb91>] btrfs_file_aio_write+0x2af/0x8d2 [btrfs] [<ffffffff81100eae>] ? try_get_mem_cgroup_from_mm+0x39/0x49 [<ffffffffa010b8e2>] ? btrfs_file_aio_write+0x0/0x8d2 [btrfs] [<ffffffff811063ed>] do_sync_readv_writev+0xc1/0x100 [<ffffffff81106120>] ? might_fault+0x21/0x23 [<ffffffff81106151>] ? copy_from_user+0x2f/0x31 [<ffffffff811c90ab>] ? security_file_permission+0x16/0x18 [<ffffffff81107145>] do_readv_writev+0xa7/0x127 [<ffffffff81107208>] vfs_writev+0x43/0x4e [<ffffffff811072f8>] sys_writev+0x4a/0x93 [<ffffffff81009c32>] system_call_fastpath+0x16/0x1b
Quoted 8 lines
So, can you figure out if anything in your patch leads to the bug? --
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Sorry, I forgot the attachment...
Plz get it.
Thanks,
- Liubo
/******************************************************************************/
/* */ /* Copyright (c) 2010 FUJITSU LIMITED */ /* */ /* This program is free software; you can redistribute it and/or modify */ /* it under the terms of the GNU General Public License as published by */ /* the Free Software Foundation; either version 2 of the License, or */ /* (at your option) any later version. */ /* */ /* This program is distributed in the hope that it will be useful, */ /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See */ /* the GNU General Public License for more details. */ /* */ /* You should have received a copy of the GNU General Public License */ /* along with this program; if not, write to the Free Software */ /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* */ /* Author: Liu Bo <[email protected]> */ /* */
/******************************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <string.h>
#include <malloc.h>
#define BUFFERSIZE 8192
#ifndef O_DIRECT
#define O_DIRECT 00040000 /* direct disk access hint */
#endif
static int rw_test(char *type, int fd, int nullfd, int zerofd, char *size);
/* Direct-io Test
*
* Return Value:
* 0: succeed
* 1: fail
*
* Parse command line arguments
* flag:
* O_DIRECT, O_DIRECT | O_SYNC
* rwtype:
* read, readv, write, writev
* test file name:
* testrw
* test file size:
* 0 | 1K | 4K | 4M | 4G | 4G+1
*/
int main(int argc, char *argv[])
{
char *rwtype;
char *testfile;
char *size;
int fd, nullfd, zerofd;
int flag = O_RDWR;
int ret;
if (--argc != 4) {
fprintf(stderr, "4 arguments are needed!\n");
exit(1);
}
if (!strcmp(argv[1], "O_DIRECT"))
flag |= O_DIRECT;
else if (!strcmp(argv[1], "O_DIRECT | O_SYNC") ||
!strcmp(argv[1], "O_DIRECT|O_SYNC"))
flag |= O_DIRECT | O_SYNC;
rwtype = argv[2];
testfile = argv[3];
size = argv[4];
if ((fd = open(testfile, flag)) < 0) {
fprintf(stderr, "Cannot open the test file! \
: %s\n\n", strerror(errno));
exit(1);
}
if ((nullfd = open("/dev/null", O_WRONLY)) < 0) {
fprintf(stderr, "Cannot open the test file! \
: %s\n\n", strerror(errno));
exit(1);
}
if ((zerofd = open("/dev/zero", O_RDONLY)) < 0) {
fprintf(stderr, "Cannot open the test file! \
: %s\n\n", strerror(errno));
exit(1);
}
ret = rw_test(rwtype, fd, nullfd, zerofd, size);
close(fd);
close(nullfd);
close(zerofd);
if (ret != 0)
return 1;
return 0;
}/*
echo "=========================" >> $RESULTFILE
* rw_test is used to test read/write/readv/writev.
*
* Arguments:
* type: read/write/readv/writev
* fd: read/write file descripter
* nullfd: write file descripter binded to /dev/null
* zerofd: read file descripter binded to /dev/zero
* size: 0 | 1K | 4K | 4M | 4G | 4G+1
*
* Return value:
* 0: succeed
* 1: write error
* -1: read error
*/
static int rw_test(char *type, int fd, int nullfd, int zerofd, char *size)
{
ssize_t n;
int iovcnt;
long limit = 0;
if(!strcmp(size, "1K")) {
limit = 1024;
} else if (!strcmp(size, "4K")) {
limit = 1024 * 4;
} else if (!strcmp(size, "4M")) {
limit = 1024 * 1024 * 4;
} else if (!strcmp(size, "4G")) {
limit = 1024 * 1024 * 4;
limit *= 1024;
} else if (!strcmp(size, "4G+1")) {
limit = 1024 * 1024 * 4;
limit *= 1024;
limit += 1;
}
if (!strcmp(type, "read")) {
char *buf;
buf = valloc(BUFFERSIZE);
if (!buf) {
perror("valloc error");
exit(1);
}
memset(buf, 0, BUFFERSIZE);
while ((n = read(fd, buf, BUFFERSIZE)) > 0) {
if (write(nullfd, buf, n) != n) {
fprintf(stderr, "write error! :\
%s\n", strerror(errno));
return 1;
}
}
if (n < 0) {
fprintf(stderr, "read error : %s\n\n", \
strerror(errno));
return -1;
}
} else if (!strcmp(type, "write")) {
char *buf;
long count = 0;
buf = valloc(BUFFERSIZE);
if (!buf) {
perror("valloc error");
exit(1);
}
memset(buf, 0, BUFFERSIZE);
while ((n = read(zerofd, buf, BUFFERSIZE)) > 0) {
if (write(fd, buf, n) != n) {
fprintf(stderr, "write error! :\
%s\n", strerror(errno));
return 1;
}
count += n;
if (count >= limit)
break;
}
if (count < limit) {
fprintf(stderr, "read error : %s\n\n", \
strerror(errno));
return -1;
}
} else if (!strcmp(type, "readv")) {
iovcnt = 2;
char *buf[iovcnt];
struct iovec iov[iovcnt];
int i;
for (i = 0; i < iovcnt; i++) {
buf[i] = valloc(BUFFERSIZE);
if (!buf[i]) {
perror("valloc error");
exit(1);
}
memset(buf[i], 0, BUFFERSIZE);
iov[i].iov_base = buf[i];
iov[i].iov_len = BUFFERSIZE;
}
while ((n = readv(fd, iov, iovcnt)) > 0) {
if (writev(nullfd, iov, iovcnt) !=
(iovcnt * BUFFERSIZE)) {
fprintf(stderr, "writev error! :\
%s\n", strerror(errno));
return 1;
}
}
if (n < 0) {
fprintf(stderr, "readv error : %s\n\n", \
strerror(errno));
return -1;
}
} else if (!strcmp(type, "writev")) {
iovcnt = 2;
char *buf[iovcnt];
struct iovec iov[iovcnt];
long count = 0;
int i;
for (i = 0; i < iovcnt; i++) {
buf[i] = valloc(BUFFERSIZE);
if (!buf[i]) {
perror("valloc error");
exit(1);
}
memset(buf[i], 0, BUFFERSIZE);
iov[i].iov_base = buf[i];
iov[i].iov_len = BUFFERSIZE;
}
while ((n = readv(zerofd, iov, iovcnt)) > 0) {
if (writev(fd, iov, iovcnt) !=
(iovcnt * BUFFERSIZE)) {
fprintf(stderr, "writev error! :\
%s\n", strerror(errno));
return 1;
}
count += n;
if (count >= limit)
break;
}
if (count < limit) {
fprintf(stderr, "readv error : %s\n\n", \
strerror(errno));
return -1;
}
}
return 0;
}Re: [PATCH 6/6] Btrfs: do aio_write instead of write
In reply to liuboQuoted 10 lines · liubo
Quoted 6 lines · Josef Bacik
In order for AIO to work, we need to implement
aio_write. This patch converts ourbtrfs_file_writetobtrfs_aio_write. I've tested this with xfstests and nothing broke, and the AIO stuff magically started working. Thanks,Signed-off-by Josef Bacik <[email protected]>Hi, Josef,
Quoted 7 lines
I've tested your patch(May 22) with my tools, and one case triggered a bug which made writev operation hang up, more information is followed.
- Steps to trigger it:
# mount /dev/sda8 /home/btrfsdisk -o nodatacow # gcc direct-io.c -o direct-io # ./direct-io O_DIRECT writev /home/btrfsdisk/testrw 4M
Thanks for sending along this test program and bug report. We've fixed a few bugs in the O_DIRECT patches, and this is working now.
The merged result is in the for-linus branch of the btrfs unstable tree.
-chris
Re: [PATCH 6/6] Btrfs: do aio_write instead of write
Quoted 7 lines · Chris Mason
Thanks for sending along this test program and bug report. We've fixed a few bugs in the
O_DIRECTpatches, and this is working now.The merged result is in the for-linus branch of the btrfs unstable tree.
-chris
Oh, Thanks a lot. I've see your great effort.
-liubo
Re: [PATCH 6/6] Btrfs: do aio_write instead of write
In reply to Josef BacikQuoted 2 lines · Josef Bacik
+ =C2=A0 =C2=A0 =C2=A0 while (1) {
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 lock_extent(tree, =
start, end, GFP_NOFS);
Quoted 1 line
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ordered =3D btrfs_=
lookup_ordered_extent(inode, start);
Quoted 2 lines
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if (!ordered)
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=
=C2=A0 break;
Quoted 1 line
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 unlock_extent(tree=
, start, end, GFP_NOFS);
Is it ok not to unlock_extent if !ordered?
I don't know if you fixed this in a later version but it stuck out to m= e :)
--=20
Dmitri Nikulin
Centre for Synchrotron Science
Monash University
Victoria 3800, Australia
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" = in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 6/6] Btrfs: do aio_write instead of write
Quoted 1 line · Dmitri Nikulin
On Sat, May 22, 2010 at 3:03 AM, Josef Bacik <[email protected]> wrote=
:
Quoted 2 lines
Quoted 2 lines
+ =A0 =A0 =A0 while (1) {
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 lock_extent(tree, start, end, GFP_NOF=
S);
Quoted 1 line
Quoted 1 line
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 ordered =3D btrfs_lookup_ordered_exte=
nt(inode, start);
Quoted 3 lines
Quoted 3 lines
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 if (!ordered)
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 break;
+ =A0 =A0 =A0 =A0 =A0 =A0 =A0 unlock_extent(tree, start, end, GFP_N=
OFS);
Quoted 3 lines
=20
Is it ok not tounlock_extentif !ordered?
I don't know if you fixed this in a later version but it stuck out to=
me :)
The construct is confusing. Ordered extents track things that we have allocated on disk and need to write. New ones can't be created while w= e
have the extent range locked. But we can't force old ones to disk with the lock held.
So, we lock then lookup and if we find nothing we can safely do our operation because no io is in progress. We unlock a little later on, o= r
at endio time.
If we find an ordered extent we drop the lock and wait for that IO to finish, then loop again.
-chris
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" = in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 6/6] Btrfs: do aio_write instead of write
On Tue, Jun 1, 2010 at 11:19 PM, Chris Mason <[email protected]> w= rote:
Quoted 2 lines
Quoted 2 lines
Is it ok not to
unlock_extentif !ordered?
I don't know if you fixed this in a later version but it stuck out t=
o me :)
Quoted 2 lines
The construct is confusing. =C2=A0Ordered extents track things that w=
e have
Quoted 1 line
allocated on disk and need to write. =C2=A0New ones can't be created =
while we
Quoted 1 line
have the extent range locked. =C2=A0But we can't force old ones to di=
sk with
Quoted 4 lines
the lock held.
So, we lock then lookup and if we find nothing we can safely do our operation because no io is in progress. =C2=A0We unlock a little late=
r on, or
Quoted 4 lines
at endio time.
If we find an ordered extent we drop the lock and wait for that IO to finish, then loop again.
Ok, that's fair enough. Maybe it's worth commenting, I'm sure I'm not the only one surprised.
Thanks,
--=20
Dmitri Nikulin
Centre for Synchrotron Science
Monash University
Victoria 3800, Australia
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" = in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html