Commit 642c5d34 authored by Christoph Hellwig's avatar Christoph Hellwig Committed by David Sterba

btrfs: allocate the btrfs_dio_private as part of the iomap dio bio

Create a new bio_set that contains all the per-bio private data needed
by btrfs for direct I/O and tell the iomap code to use that instead
of separately allocation the btrfs_dio_private structure.
Reviewed-by: default avatarNikolay Borisov <nborisov@suse.com>
Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
Reviewed-by: default avatarDavid Sterba <dsterba@suse.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent a3e171a0
...@@ -85,13 +85,15 @@ struct btrfs_dio_private { ...@@ -85,13 +85,15 @@ struct btrfs_dio_private {
*/ */
refcount_t refs; refcount_t refs;
/* dio_bio came from fs/direct-io.c */
struct bio *dio_bio;
/* Array of checksums */ /* Array of checksums */
u8 csums[]; u8 *csums;
/* This must be last */
struct bio bio;
}; };
static struct bio_set btrfs_dio_bioset;
struct btrfs_rename_ctx { struct btrfs_rename_ctx {
/* Output field. Stores the index number of the old directory entry. */ /* Output field. Stores the index number of the old directory entry. */
u64 index; u64 index;
...@@ -7828,19 +7830,19 @@ static void btrfs_dio_private_put(struct btrfs_dio_private *dip) ...@@ -7828,19 +7830,19 @@ static void btrfs_dio_private_put(struct btrfs_dio_private *dip)
if (!refcount_dec_and_test(&dip->refs)) if (!refcount_dec_and_test(&dip->refs))
return; return;
if (btrfs_op(dip->dio_bio) == BTRFS_MAP_WRITE) { if (btrfs_op(&dip->bio) == BTRFS_MAP_WRITE) {
__endio_write_update_ordered(BTRFS_I(dip->inode), __endio_write_update_ordered(BTRFS_I(dip->inode),
dip->file_offset, dip->file_offset,
dip->bytes, dip->bytes,
!dip->dio_bio->bi_status); !dip->bio.bi_status);
} else { } else {
unlock_extent(&BTRFS_I(dip->inode)->io_tree, unlock_extent(&BTRFS_I(dip->inode)->io_tree,
dip->file_offset, dip->file_offset,
dip->file_offset + dip->bytes - 1); dip->file_offset + dip->bytes - 1);
} }
bio_endio(dip->dio_bio); kfree(dip->csums);
kfree(dip); bio_endio(&dip->bio);
} }
static void submit_dio_repair_bio(struct inode *inode, struct bio *bio, static void submit_dio_repair_bio(struct inode *inode, struct bio *bio,
...@@ -7942,7 +7944,7 @@ static void btrfs_end_dio_bio(struct bio *bio) ...@@ -7942,7 +7944,7 @@ static void btrfs_end_dio_bio(struct bio *bio)
err = btrfs_check_read_dio_bio(dip, bbio, !err); err = btrfs_check_read_dio_bio(dip, bbio, !err);
if (err) if (err)
dip->dio_bio->bi_status = err; dip->bio.bi_status = err;
btrfs_record_physical_zoned(dip->inode, bbio->file_offset, bio); btrfs_record_physical_zoned(dip->inode, bbio->file_offset, bio);
...@@ -7997,49 +7999,16 @@ static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio, ...@@ -7997,49 +7999,16 @@ static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
return ret; return ret;
} }
/*
* If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
* or ordered extents whether or not we submit any bios.
*/
static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio,
struct inode *inode,
loff_t file_offset)
{
const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE);
const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
size_t dip_size;
struct btrfs_dio_private *dip;
dip_size = sizeof(*dip);
if (!write && csum) {
struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
size_t nblocks;
nblocks = dio_bio->bi_iter.bi_size >> fs_info->sectorsize_bits;
dip_size += fs_info->csum_size * nblocks;
}
dip = kzalloc(dip_size, GFP_NOFS);
if (!dip)
return NULL;
dip->inode = inode;
dip->file_offset = file_offset;
dip->bytes = dio_bio->bi_iter.bi_size;
dip->dio_bio = dio_bio;
refcount_set(&dip->refs, 1);
return dip;
}
static void btrfs_submit_direct(const struct iomap_iter *iter, static void btrfs_submit_direct(const struct iomap_iter *iter,
struct bio *dio_bio, loff_t file_offset) struct bio *dio_bio, loff_t file_offset)
{ {
struct btrfs_dio_private *dip =
container_of(dio_bio, struct btrfs_dio_private, bio);
struct inode *inode = iter->inode; struct inode *inode = iter->inode;
const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE); const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE);
struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
const bool raid56 = (btrfs_data_alloc_profile(fs_info) & const bool raid56 = (btrfs_data_alloc_profile(fs_info) &
BTRFS_BLOCK_GROUP_RAID56_MASK); BTRFS_BLOCK_GROUP_RAID56_MASK);
struct btrfs_dio_private *dip;
struct bio *bio; struct bio *bio;
u64 start_sector; u64 start_sector;
int async_submit = 0; int async_submit = 0;
...@@ -8053,24 +8022,25 @@ static void btrfs_submit_direct(const struct iomap_iter *iter, ...@@ -8053,24 +8022,25 @@ static void btrfs_submit_direct(const struct iomap_iter *iter,
struct btrfs_dio_data *dio_data = iter->private; struct btrfs_dio_data *dio_data = iter->private;
struct extent_map *em = NULL; struct extent_map *em = NULL;
dip = btrfs_create_dio_private(dio_bio, inode, file_offset); dip->inode = inode;
if (!dip) { dip->file_offset = file_offset;
if (!write) { dip->bytes = dio_bio->bi_iter.bi_size;
unlock_extent(&BTRFS_I(inode)->io_tree, file_offset, refcount_set(&dip->refs, 1);
file_offset + dio_bio->bi_iter.bi_size - 1); dip->csums = NULL;
}
dio_bio->bi_status = BLK_STS_RESOURCE; if (!write && !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
bio_endio(dio_bio); unsigned int nr_sectors =
return; (dio_bio->bi_iter.bi_size >> fs_info->sectorsize_bits);
}
if (!write) {
/* /*
* Load the csums up front to reduce csum tree searches and * Load the csums up front to reduce csum tree searches and
* contention when submitting bios. * contention when submitting bios.
*
* If we have csums disabled this will do nothing.
*/ */
status = BLK_STS_RESOURCE;
dip->csums = kcalloc(nr_sectors, fs_info->csum_size, GFP_NOFS);
if (!dip)
goto out_err;
status = btrfs_lookup_bio_sums(inode, dio_bio, dip->csums); status = btrfs_lookup_bio_sums(inode, dio_bio, dip->csums);
if (status != BLK_STS_OK) if (status != BLK_STS_OK)
goto out_err; goto out_err;
...@@ -8160,7 +8130,7 @@ static void btrfs_submit_direct(const struct iomap_iter *iter, ...@@ -8160,7 +8130,7 @@ static void btrfs_submit_direct(const struct iomap_iter *iter,
out_err_em: out_err_em:
free_extent_map(em); free_extent_map(em);
out_err: out_err:
dip->dio_bio->bi_status = status; dio_bio->bi_status = status;
btrfs_dio_private_put(dip); btrfs_dio_private_put(dip);
} }
...@@ -8171,6 +8141,7 @@ static const struct iomap_ops btrfs_dio_iomap_ops = { ...@@ -8171,6 +8141,7 @@ static const struct iomap_ops btrfs_dio_iomap_ops = {
static const struct iomap_dio_ops btrfs_dio_ops = { static const struct iomap_dio_ops btrfs_dio_ops = {
.submit_io = btrfs_submit_direct, .submit_io = btrfs_submit_direct,
.bio_set = &btrfs_dio_bioset,
}; };
ssize_t btrfs_dio_rw(struct kiocb *iocb, struct iov_iter *iter, size_t done_before) ssize_t btrfs_dio_rw(struct kiocb *iocb, struct iov_iter *iter, size_t done_before)
...@@ -8991,6 +8962,7 @@ void __cold btrfs_destroy_cachep(void) ...@@ -8991,6 +8962,7 @@ void __cold btrfs_destroy_cachep(void)
* destroy cache. * destroy cache.
*/ */
rcu_barrier(); rcu_barrier();
bioset_exit(&btrfs_dio_bioset);
kmem_cache_destroy(btrfs_inode_cachep); kmem_cache_destroy(btrfs_inode_cachep);
kmem_cache_destroy(btrfs_trans_handle_cachep); kmem_cache_destroy(btrfs_trans_handle_cachep);
kmem_cache_destroy(btrfs_path_cachep); kmem_cache_destroy(btrfs_path_cachep);
...@@ -9031,6 +9003,11 @@ int __init btrfs_init_cachep(void) ...@@ -9031,6 +9003,11 @@ int __init btrfs_init_cachep(void)
if (!btrfs_free_space_bitmap_cachep) if (!btrfs_free_space_bitmap_cachep)
goto fail; goto fail;
if (bioset_init(&btrfs_dio_bioset, BIO_POOL_SIZE,
offsetof(struct btrfs_dio_private, bio),
BIOSET_NEED_BVECS))
goto fail;
return 0; return 0;
fail: fail:
btrfs_destroy_cachep(); btrfs_destroy_cachep();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment