block: Convert various code to bio_for_each_segment()

With immutable biovecs we don't want code accessing bi_io_vec directly -
the uses this patch changes weren't incorrect since they all own the
bio, but it makes the code harder to audit for no good reason - also,
this will help with multipage bvecs later.

Signed-off-by: Kent Overstreet <kmo@daterainc.com>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Chris Mason <chris.mason@fusionio.com>
Cc: Jaegeuk Kim <jaegeuk.kim@samsung.com>
Cc: Joern Engel <joern@logfs.org>
Cc: Prasad Joshi <prasadjoshi.linux@gmail.com>
Cc: Trond Myklebust <Trond.Myklebust@netapp.com>
This commit is contained in:
Kent Overstreet 2013-11-07 12:20:26 -08:00
parent 33879d4512
commit 2c30c71bd6
10 changed files with 66 additions and 101 deletions

View file

@ -201,18 +201,16 @@ static void end_compressed_bio_read(struct bio *bio, int err)
if (cb->errors) { if (cb->errors) {
bio_io_error(cb->orig_bio); bio_io_error(cb->orig_bio);
} else { } else {
int bio_index = 0; int i;
struct bio_vec *bvec = cb->orig_bio->bi_io_vec; struct bio_vec *bvec;
/* /*
* we have verified the checksum already, set page * we have verified the checksum already, set page
* checked so the end_io handlers know about it * checked so the end_io handlers know about it
*/ */
while (bio_index < cb->orig_bio->bi_vcnt) { bio_for_each_segment_all(bvec, cb->orig_bio, i)
SetPageChecked(bvec->bv_page); SetPageChecked(bvec->bv_page);
bvec++;
bio_index++;
}
bio_endio(cb->orig_bio, 0); bio_endio(cb->orig_bio, 0);
} }

View file

@ -842,20 +842,17 @@ int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode,
static int btree_csum_one_bio(struct bio *bio) static int btree_csum_one_bio(struct bio *bio)
{ {
struct bio_vec *bvec = bio->bi_io_vec; struct bio_vec *bvec;
int bio_index = 0;
struct btrfs_root *root; struct btrfs_root *root;
int ret = 0; int i, ret = 0;
WARN_ON(bio->bi_vcnt <= 0); bio_for_each_segment_all(bvec, bio, i) {
while (bio_index < bio->bi_vcnt) {
root = BTRFS_I(bvec->bv_page->mapping->host)->root; root = BTRFS_I(bvec->bv_page->mapping->host)->root;
ret = csum_dirty_buffer(root, bvec->bv_page); ret = csum_dirty_buffer(root, bvec->bv_page);
if (ret) if (ret)
break; break;
bio_index++;
bvec++;
} }
return ret; return ret;
} }

View file

@ -2332,12 +2332,13 @@ int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
*/ */
static void end_bio_extent_writepage(struct bio *bio, int err) static void end_bio_extent_writepage(struct bio *bio, int err)
{ {
struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; struct bio_vec *bvec;
struct extent_io_tree *tree; struct extent_io_tree *tree;
u64 start; u64 start;
u64 end; u64 end;
int i;
do { bio_for_each_segment_all(bvec, bio, i) {
struct page *page = bvec->bv_page; struct page *page = bvec->bv_page;
tree = &BTRFS_I(page->mapping->host)->io_tree; tree = &BTRFS_I(page->mapping->host)->io_tree;
@ -2355,14 +2356,11 @@ static void end_bio_extent_writepage(struct bio *bio, int err)
start = page_offset(page); start = page_offset(page);
end = start + bvec->bv_offset + bvec->bv_len - 1; end = start + bvec->bv_offset + bvec->bv_len - 1;
if (--bvec >= bio->bi_io_vec)
prefetchw(&bvec->bv_page->flags);
if (end_extent_writepage(page, err, start, end)) if (end_extent_writepage(page, err, start, end))
continue; continue;
end_page_writeback(page); end_page_writeback(page);
} while (bvec >= bio->bi_io_vec); }
bio_put(bio); bio_put(bio);
} }
@ -2392,9 +2390,8 @@ endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len,
*/ */
static void end_bio_extent_readpage(struct bio *bio, int err) static void end_bio_extent_readpage(struct bio *bio, int err)
{ {
struct bio_vec *bvec;
int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
struct bio_vec *bvec = bio->bi_io_vec;
struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
struct extent_io_tree *tree; struct extent_io_tree *tree;
u64 offset = 0; u64 offset = 0;
@ -2405,11 +2402,12 @@ static void end_bio_extent_readpage(struct bio *bio, int err)
u64 extent_len = 0; u64 extent_len = 0;
int mirror; int mirror;
int ret; int ret;
int i;
if (err) if (err)
uptodate = 0; uptodate = 0;
do { bio_for_each_segment_all(bvec, bio, i) {
struct page *page = bvec->bv_page; struct page *page = bvec->bv_page;
struct inode *inode = page->mapping->host; struct inode *inode = page->mapping->host;
@ -2433,9 +2431,6 @@ static void end_bio_extent_readpage(struct bio *bio, int err)
end = start + bvec->bv_offset + bvec->bv_len - 1; end = start + bvec->bv_offset + bvec->bv_len - 1;
len = bvec->bv_len; len = bvec->bv_len;
if (++bvec <= bvec_end)
prefetchw(&bvec->bv_page->flags);
mirror = io_bio->mirror_num; mirror = io_bio->mirror_num;
if (likely(uptodate && tree->ops && if (likely(uptodate && tree->ops &&
tree->ops->readpage_end_io_hook)) { tree->ops->readpage_end_io_hook)) {
@ -2516,7 +2511,7 @@ static void end_bio_extent_readpage(struct bio *bio, int err)
extent_start = start; extent_start = start;
extent_len = end + 1 - start; extent_len = end + 1 - start;
} }
} while (bvec <= bvec_end); }
if (extent_len) if (extent_len)
endio_readpage_release_extent(tree, extent_start, extent_len, endio_readpage_release_extent(tree, extent_start, extent_len,
@ -2547,7 +2542,6 @@ btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
} }
if (bio) { if (bio) {
bio->bi_size = 0;
bio->bi_bdev = bdev; bio->bi_bdev = bdev;
bio->bi_sector = first_sector; bio->bi_sector = first_sector;
btrfs_bio = btrfs_io_bio(bio); btrfs_bio = btrfs_io_bio(bio);
@ -3410,20 +3404,18 @@ static void end_extent_buffer_writeback(struct extent_buffer *eb)
static void end_bio_extent_buffer_writepage(struct bio *bio, int err) static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
{ {
int uptodate = err == 0; struct bio_vec *bvec;
struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
struct extent_buffer *eb; struct extent_buffer *eb;
int done; int i, done;
do { bio_for_each_segment_all(bvec, bio, i) {
struct page *page = bvec->bv_page; struct page *page = bvec->bv_page;
bvec--;
eb = (struct extent_buffer *)page->private; eb = (struct extent_buffer *)page->private;
BUG_ON(!eb); BUG_ON(!eb);
done = atomic_dec_and_test(&eb->io_pages); done = atomic_dec_and_test(&eb->io_pages);
if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) { if (err || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) {
set_bit(EXTENT_BUFFER_IOERR, &eb->bflags); set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
ClearPageUptodate(page); ClearPageUptodate(page);
SetPageError(page); SetPageError(page);
@ -3435,10 +3427,9 @@ static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
continue; continue;
end_extent_buffer_writeback(eb); end_extent_buffer_writeback(eb);
} while (bvec >= bio->bi_io_vec); }
bio_put(bio); bio_put(bio);
} }
static int write_one_eb(struct extent_buffer *eb, static int write_one_eb(struct extent_buffer *eb,

View file

@ -6779,17 +6779,16 @@ static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
static void btrfs_endio_direct_read(struct bio *bio, int err) static void btrfs_endio_direct_read(struct bio *bio, int err)
{ {
struct btrfs_dio_private *dip = bio->bi_private; struct btrfs_dio_private *dip = bio->bi_private;
struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1; struct bio_vec *bvec;
struct bio_vec *bvec = bio->bi_io_vec;
struct inode *inode = dip->inode; struct inode *inode = dip->inode;
struct btrfs_root *root = BTRFS_I(inode)->root; struct btrfs_root *root = BTRFS_I(inode)->root;
struct bio *dio_bio; struct bio *dio_bio;
u32 *csums = (u32 *)dip->csum; u32 *csums = (u32 *)dip->csum;
int index = 0;
u64 start; u64 start;
int i;
start = dip->logical_offset; start = dip->logical_offset;
do { bio_for_each_segment_all(bvec, bio, i) {
if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
struct page *page = bvec->bv_page; struct page *page = bvec->bv_page;
char *kaddr; char *kaddr;
@ -6805,18 +6804,16 @@ static void btrfs_endio_direct_read(struct bio *bio, int err)
local_irq_restore(flags); local_irq_restore(flags);
flush_dcache_page(bvec->bv_page); flush_dcache_page(bvec->bv_page);
if (csum != csums[index]) { if (csum != csums[i]) {
btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u", btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
btrfs_ino(inode), start, csum, btrfs_ino(inode), start, csum,
csums[index]); csums[i]);
err = -EIO; err = -EIO;
} }
} }
start += bvec->bv_len; start += bvec->bv_len;
bvec++; }
index++;
} while (bvec <= bvec_end);
unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
dip->logical_offset + dip->bytes - 1); dip->logical_offset + dip->bytes - 1);

View file

@ -65,9 +65,9 @@ static void ext4_finish_bio(struct bio *bio)
{ {
int i; int i;
int error = !test_bit(BIO_UPTODATE, &bio->bi_flags); int error = !test_bit(BIO_UPTODATE, &bio->bi_flags);
struct bio_vec *bvec;
for (i = 0; i < bio->bi_vcnt; i++) { bio_for_each_segment_all(bvec, bio, i) {
struct bio_vec *bvec = &bio->bi_io_vec[i];
struct page *page = bvec->bv_page; struct page *page = bvec->bv_page;
struct buffer_head *bh, *head; struct buffer_head *bh, *head;
unsigned bio_start = bvec->bv_offset; unsigned bio_start = bvec->bv_offset;

View file

@ -351,23 +351,20 @@ struct page *get_new_data_page(struct inode *inode,
static void read_end_io(struct bio *bio, int err) static void read_end_io(struct bio *bio, int err)
{ {
const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); struct bio_vec *bvec;
struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; int i;
do { bio_for_each_segment_all(bvec, bio, i) {
struct page *page = bvec->bv_page; struct page *page = bvec->bv_page;
if (--bvec >= bio->bi_io_vec) if (!err) {
prefetchw(&bvec->bv_page->flags);
if (uptodate) {
SetPageUptodate(page); SetPageUptodate(page);
} else { } else {
ClearPageUptodate(page); ClearPageUptodate(page);
SetPageError(page); SetPageError(page);
} }
unlock_page(page); unlock_page(page);
} while (bvec >= bio->bi_io_vec); }
bio_put(bio); bio_put(bio);
} }

View file

@ -575,16 +575,14 @@ static const struct segment_allocation default_salloc_ops = {
static void f2fs_end_io_write(struct bio *bio, int err) static void f2fs_end_io_write(struct bio *bio, int err)
{ {
const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
struct bio_private *p = bio->bi_private; struct bio_private *p = bio->bi_private;
struct bio_vec *bvec;
int i;
do { bio_for_each_segment_all(bvec, bio, i) {
struct page *page = bvec->bv_page; struct page *page = bvec->bv_page;
if (--bvec >= bio->bi_io_vec) if (err) {
prefetchw(&bvec->bv_page->flags);
if (!uptodate) {
SetPageError(page); SetPageError(page);
if (page->mapping) if (page->mapping)
set_bit(AS_EIO, &page->mapping->flags); set_bit(AS_EIO, &page->mapping->flags);
@ -593,7 +591,7 @@ static void f2fs_end_io_write(struct bio *bio, int err)
} }
end_page_writeback(page); end_page_writeback(page);
dec_page_count(p->sbi, F2FS_WRITEBACK); dec_page_count(p->sbi, F2FS_WRITEBACK);
} while (bvec >= bio->bi_io_vec); }
if (p->is_sync) if (p->is_sync)
complete(p->wait); complete(p->wait);

View file

@ -56,22 +56,18 @@ static DECLARE_WAIT_QUEUE_HEAD(wq);
static void writeseg_end_io(struct bio *bio, int err) static void writeseg_end_io(struct bio *bio, int err)
{ {
const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; struct bio_vec *bvec;
int i;
struct super_block *sb = bio->bi_private; struct super_block *sb = bio->bi_private;
struct logfs_super *super = logfs_super(sb); struct logfs_super *super = logfs_super(sb);
struct page *page;
BUG_ON(!uptodate); /* FIXME: Retry io or write elsewhere */ BUG_ON(!uptodate); /* FIXME: Retry io or write elsewhere */
BUG_ON(err); BUG_ON(err);
BUG_ON(bio->bi_vcnt == 0);
do {
page = bvec->bv_page;
if (--bvec >= bio->bi_io_vec)
prefetchw(&bvec->bv_page->flags);
end_page_writeback(page); bio_for_each_segment_all(bvec, bio, i) {
page_cache_release(page); end_page_writeback(bvec->bv_page);
} while (bvec >= bio->bi_io_vec); page_cache_release(bvec->bv_page);
}
bio_put(bio); bio_put(bio);
if (atomic_dec_and_test(&super->s_pending_writes)) if (atomic_dec_and_test(&super->s_pending_writes))
wake_up(&wq); wake_up(&wq);

View file

@ -43,16 +43,14 @@
*/ */
static void mpage_end_io(struct bio *bio, int err) static void mpage_end_io(struct bio *bio, int err)
{ {
const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); struct bio_vec *bv;
struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; int i;
do { bio_for_each_segment_all(bv, bio, i) {
struct page *page = bvec->bv_page; struct page *page = bv->bv_page;
if (--bvec >= bio->bi_io_vec)
prefetchw(&bvec->bv_page->flags);
if (bio_data_dir(bio) == READ) { if (bio_data_dir(bio) == READ) {
if (uptodate) { if (!err) {
SetPageUptodate(page); SetPageUptodate(page);
} else { } else {
ClearPageUptodate(page); ClearPageUptodate(page);
@ -60,14 +58,15 @@ static void mpage_end_io(struct bio *bio, int err)
} }
unlock_page(page); unlock_page(page);
} else { /* bio_data_dir(bio) == WRITE */ } else { /* bio_data_dir(bio) == WRITE */
if (!uptodate) { if (err) {
SetPageError(page); SetPageError(page);
if (page->mapping) if (page->mapping)
set_bit(AS_EIO, &page->mapping->flags); set_bit(AS_EIO, &page->mapping->flags);
} }
end_page_writeback(page); end_page_writeback(page);
} }
} while (bvec >= bio->bi_io_vec); }
bio_put(bio); bio_put(bio);
} }

View file

@ -201,18 +201,14 @@ static struct bio *bl_add_page_to_bio(struct bio *bio, int npg, int rw,
static void bl_end_io_read(struct bio *bio, int err) static void bl_end_io_read(struct bio *bio, int err)
{ {
struct parallel_io *par = bio->bi_private; struct parallel_io *par = bio->bi_private;
const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); struct bio_vec *bvec;
struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; int i;
do { if (!err)
struct page *page = bvec->bv_page; bio_for_each_segment_all(bvec, bio, i)
SetPageUptodate(bvec->bv_page);
if (--bvec >= bio->bi_io_vec) if (err) {
prefetchw(&bvec->bv_page->flags);
if (uptodate)
SetPageUptodate(page);
} while (bvec >= bio->bi_io_vec);
if (!uptodate) {
struct nfs_read_data *rdata = par->data; struct nfs_read_data *rdata = par->data;
struct nfs_pgio_header *header = rdata->header; struct nfs_pgio_header *header = rdata->header;
@ -383,20 +379,16 @@ static void mark_extents_written(struct pnfs_block_layout *bl,
static void bl_end_io_write_zero(struct bio *bio, int err) static void bl_end_io_write_zero(struct bio *bio, int err)
{ {
struct parallel_io *par = bio->bi_private; struct parallel_io *par = bio->bi_private;
const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); struct bio_vec *bvec;
struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; int i;
do { bio_for_each_segment_all(bvec, bio, i) {
struct page *page = bvec->bv_page;
if (--bvec >= bio->bi_io_vec)
prefetchw(&bvec->bv_page->flags);
/* This is the zeroing page we added */ /* This is the zeroing page we added */
end_page_writeback(page); end_page_writeback(bvec->bv_page);
page_cache_release(page); page_cache_release(bvec->bv_page);
} while (bvec >= bio->bi_io_vec); }
if (unlikely(!uptodate)) { if (unlikely(err)) {
struct nfs_write_data *data = par->data; struct nfs_write_data *data = par->data;
struct nfs_pgio_header *header = data->header; struct nfs_pgio_header *header = data->header;