aboutsummaryrefslogtreecommitdiffstats
path: root/fs
diff options
context:
space:
mode:
Diffstat (limited to 'fs')
-rw-r--r--fs/btrfs/check-integrity.c2
-rw-r--r--fs/btrfs/disk-io.c2
-rw-r--r--fs/btrfs/extent_io.c49
-rw-r--r--fs/btrfs/extent_io.h2
-rw-r--r--fs/btrfs/inode.c64
-rw-r--r--fs/btrfs/raid56.c2
-rw-r--r--fs/btrfs/scrub.c10
-rw-r--r--fs/btrfs/volumes.c41
-rw-r--r--fs/btrfs/volumes.h20
9 files changed, 120 insertions, 72 deletions
diff --git a/fs/btrfs/check-integrity.c b/fs/btrfs/check-integrity.c
index 18af6f48781a..1431a6965017 100644
--- a/fs/btrfs/check-integrity.c
+++ b/fs/btrfs/check-integrity.c
@@ -1700,7 +1700,7 @@ static int btrfsic_read_block(struct btrfsic_state *state,
1700 unsigned int j; 1700 unsigned int j;
1701 DECLARE_COMPLETION_ONSTACK(complete); 1701 DECLARE_COMPLETION_ONSTACK(complete);
1702 1702
1703 bio = bio_alloc(GFP_NOFS, num_pages - i); 1703 bio = btrfs_io_bio_alloc(GFP_NOFS, num_pages - i);
1704 if (!bio) { 1704 if (!bio) {
1705 printk(KERN_INFO 1705 printk(KERN_INFO
1706 "btrfsic: bio_alloc() for %u pages failed!\n", 1706 "btrfsic: bio_alloc() for %u pages failed!\n",
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 4e9ebe1f1827..ca0ea9928210 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -3128,7 +3128,7 @@ static int write_dev_flush(struct btrfs_device *device, int wait)
3128 * caller 3128 * caller
3129 */ 3129 */
3130 device->flush_bio = NULL; 3130 device->flush_bio = NULL;
3131 bio = bio_alloc(GFP_NOFS, 0); 3131 bio = btrfs_io_bio_alloc(GFP_NOFS, 0);
3132 if (!bio) 3132 if (!bio)
3133 return -ENOMEM; 3133 return -ENOMEM;
3134 3134
diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index d2ac518f90e4..fe1d6c3424a5 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -23,6 +23,7 @@
23 23
24static struct kmem_cache *extent_state_cache; 24static struct kmem_cache *extent_state_cache;
25static struct kmem_cache *extent_buffer_cache; 25static struct kmem_cache *extent_buffer_cache;
26static struct bio_set *btrfs_bioset;
26 27
27#ifdef CONFIG_BTRFS_DEBUG 28#ifdef CONFIG_BTRFS_DEBUG
28static LIST_HEAD(buffers); 29static LIST_HEAD(buffers);
@@ -125,10 +126,20 @@ int __init extent_io_init(void)
125 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); 126 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
126 if (!extent_buffer_cache) 127 if (!extent_buffer_cache)
127 goto free_state_cache; 128 goto free_state_cache;
129
130 btrfs_bioset = bioset_create(BIO_POOL_SIZE,
131 offsetof(struct btrfs_io_bio, bio));
132 if (!btrfs_bioset)
133 goto free_buffer_cache;
128 return 0; 134 return 0;
129 135
136free_buffer_cache:
137 kmem_cache_destroy(extent_buffer_cache);
138 extent_buffer_cache = NULL;
139
130free_state_cache: 140free_state_cache:
131 kmem_cache_destroy(extent_state_cache); 141 kmem_cache_destroy(extent_state_cache);
142 extent_state_cache = NULL;
132 return -ENOMEM; 143 return -ENOMEM;
133} 144}
134 145
@@ -145,6 +156,8 @@ void extent_io_exit(void)
145 kmem_cache_destroy(extent_state_cache); 156 kmem_cache_destroy(extent_state_cache);
146 if (extent_buffer_cache) 157 if (extent_buffer_cache)
147 kmem_cache_destroy(extent_buffer_cache); 158 kmem_cache_destroy(extent_buffer_cache);
159 if (btrfs_bioset)
160 bioset_free(btrfs_bioset);
148} 161}
149 162
150void extent_io_tree_init(struct extent_io_tree *tree, 163void extent_io_tree_init(struct extent_io_tree *tree,
@@ -2046,7 +2059,7 @@ int repair_io_failure(struct btrfs_fs_info *fs_info, u64 start,
2046 if (btrfs_is_parity_mirror(map_tree, logical, length, mirror_num)) 2059 if (btrfs_is_parity_mirror(map_tree, logical, length, mirror_num))
2047 return 0; 2060 return 0;
2048 2061
2049 bio = bio_alloc(GFP_NOFS, 1); 2062 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
2050 if (!bio) 2063 if (!bio)
2051 return -EIO; 2064 return -EIO;
2052 bio->bi_private = &compl; 2065 bio->bi_private = &compl;
@@ -2336,7 +2349,7 @@ static int bio_readpage_error(struct bio *failed_bio, struct page *page,
2336 return -EIO; 2349 return -EIO;
2337 } 2350 }
2338 2351
2339 bio = bio_alloc(GFP_NOFS, 1); 2352 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
2340 if (!bio) { 2353 if (!bio) {
2341 free_io_failure(inode, failrec, 0); 2354 free_io_failure(inode, failrec, 0);
2342 return -EIO; 2355 return -EIO;
@@ -2457,10 +2470,11 @@ static void end_bio_extent_readpage(struct bio *bio, int err)
2457 struct page *page = bvec->bv_page; 2470 struct page *page = bvec->bv_page;
2458 struct extent_state *cached = NULL; 2471 struct extent_state *cached = NULL;
2459 struct extent_state *state; 2472 struct extent_state *state;
2473 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
2460 2474
2461 pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, " 2475 pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, "
2462 "mirror=%ld\n", (u64)bio->bi_sector, err, 2476 "mirror=%lu\n", (u64)bio->bi_sector, err,
2463 (long int)bio->bi_bdev); 2477 io_bio->mirror_num);
2464 tree = &BTRFS_I(page->mapping->host)->io_tree; 2478 tree = &BTRFS_I(page->mapping->host)->io_tree;
2465 2479
2466 start = page_offset(page) + bvec->bv_offset; 2480 start = page_offset(page) + bvec->bv_offset;
@@ -2485,7 +2499,7 @@ static void end_bio_extent_readpage(struct bio *bio, int err)
2485 } 2499 }
2486 spin_unlock(&tree->lock); 2500 spin_unlock(&tree->lock);
2487 2501
2488 mirror = (int)(unsigned long)bio->bi_bdev; 2502 mirror = io_bio->mirror_num;
2489 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) { 2503 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
2490 ret = tree->ops->readpage_end_io_hook(page, start, end, 2504 ret = tree->ops->readpage_end_io_hook(page, start, end,
2491 state, mirror); 2505 state, mirror);
@@ -2550,17 +2564,23 @@ static void end_bio_extent_readpage(struct bio *bio, int err)
2550 bio_put(bio); 2564 bio_put(bio);
2551} 2565}
2552 2566
2567/*
2568 * this allocates from the btrfs_bioset. We're returning a bio right now
2569 * but you can call btrfs_io_bio for the appropriate container_of magic
2570 */
2553struct bio * 2571struct bio *
2554btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs, 2572btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2555 gfp_t gfp_flags) 2573 gfp_t gfp_flags)
2556{ 2574{
2557 struct bio *bio; 2575 struct bio *bio;
2558 2576
2559 bio = bio_alloc(gfp_flags, nr_vecs); 2577 bio = bio_alloc_bioset(gfp_flags, nr_vecs, btrfs_bioset);
2560 2578
2561 if (bio == NULL && (current->flags & PF_MEMALLOC)) { 2579 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
2562 while (!bio && (nr_vecs /= 2)) 2580 while (!bio && (nr_vecs /= 2)) {
2563 bio = bio_alloc(gfp_flags, nr_vecs); 2581 bio = bio_alloc_bioset(gfp_flags,
2582 nr_vecs, btrfs_bioset);
2583 }
2564 } 2584 }
2565 2585
2566 if (bio) { 2586 if (bio) {
@@ -2571,6 +2591,19 @@ btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2571 return bio; 2591 return bio;
2572} 2592}
2573 2593
2594struct bio *btrfs_bio_clone(struct bio *bio, gfp_t gfp_mask)
2595{
2596 return bio_clone_bioset(bio, gfp_mask, btrfs_bioset);
2597}
2598
2599
2600/* this also allocates from the btrfs_bioset */
2601struct bio *btrfs_io_bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs)
2602{
2603 return bio_alloc_bioset(gfp_mask, nr_iovecs, btrfs_bioset);
2604}
2605
2606
2574static int __must_check submit_one_bio(int rw, struct bio *bio, 2607static int __must_check submit_one_bio(int rw, struct bio *bio,
2575 int mirror_num, unsigned long bio_flags) 2608 int mirror_num, unsigned long bio_flags)
2576{ 2609{
diff --git a/fs/btrfs/extent_io.h b/fs/btrfs/extent_io.h
index a2c03a175009..41fb81e7ec53 100644
--- a/fs/btrfs/extent_io.h
+++ b/fs/btrfs/extent_io.h
@@ -336,6 +336,8 @@ int extent_clear_unlock_delalloc(struct inode *inode,
336struct bio * 336struct bio *
337btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs, 337btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
338 gfp_t gfp_flags); 338 gfp_t gfp_flags);
339struct bio *btrfs_io_bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs);
340struct bio *btrfs_bio_clone(struct bio *bio, gfp_t gfp_mask);
339 341
340struct btrfs_fs_info; 342struct btrfs_fs_info;
341 343
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 1669c3b4be2f..d59dddfb1f96 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -6927,7 +6927,11 @@ struct btrfs_dio_private {
6927 /* IO errors */ 6927 /* IO errors */
6928 int errors; 6928 int errors;
6929 6929
6930 /* orig_bio is our btrfs_io_bio */
6930 struct bio *orig_bio; 6931 struct bio *orig_bio;
6932
6933 /* dio_bio came from fs/direct-io.c */
6934 struct bio *dio_bio;
6931}; 6935};
6932 6936
6933static void btrfs_endio_direct_read(struct bio *bio, int err) 6937static void btrfs_endio_direct_read(struct bio *bio, int err)
@@ -6937,6 +6941,7 @@ static void btrfs_endio_direct_read(struct bio *bio, int err)
6937 struct bio_vec *bvec = bio->bi_io_vec; 6941 struct bio_vec *bvec = bio->bi_io_vec;
6938 struct inode *inode = dip->inode; 6942 struct inode *inode = dip->inode;
6939 struct btrfs_root *root = BTRFS_I(inode)->root; 6943 struct btrfs_root *root = BTRFS_I(inode)->root;
6944 struct bio *dio_bio;
6940 u64 start; 6945 u64 start;
6941 6946
6942 start = dip->logical_offset; 6947 start = dip->logical_offset;
@@ -6976,14 +6981,15 @@ failed:
6976 6981
6977 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, 6982 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
6978 dip->logical_offset + dip->bytes - 1); 6983 dip->logical_offset + dip->bytes - 1);
6979 bio->bi_private = dip->private; 6984 dio_bio = dip->dio_bio;
6980 6985
6981 kfree(dip); 6986 kfree(dip);
6982 6987
6983 /* If we had a csum failure make sure to clear the uptodate flag */ 6988 /* If we had a csum failure make sure to clear the uptodate flag */
6984 if (err) 6989 if (err)
6985 clear_bit(BIO_UPTODATE, &bio->bi_flags); 6990 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6986 dio_end_io(bio, err); 6991 dio_end_io(dio_bio, err);
6992 bio_put(bio);
6987} 6993}
6988 6994
6989static void btrfs_endio_direct_write(struct bio *bio, int err) 6995static void btrfs_endio_direct_write(struct bio *bio, int err)
@@ -6994,6 +7000,7 @@ static void btrfs_endio_direct_write(struct bio *bio, int err)
6994 struct btrfs_ordered_extent *ordered = NULL; 7000 struct btrfs_ordered_extent *ordered = NULL;
6995 u64 ordered_offset = dip->logical_offset; 7001 u64 ordered_offset = dip->logical_offset;
6996 u64 ordered_bytes = dip->bytes; 7002 u64 ordered_bytes = dip->bytes;
7003 struct bio *dio_bio;
6997 int ret; 7004 int ret;
6998 7005
6999 if (err) 7006 if (err)
@@ -7021,14 +7028,15 @@ out_test:
7021 goto again; 7028 goto again;
7022 } 7029 }
7023out_done: 7030out_done:
7024 bio->bi_private = dip->private; 7031 dio_bio = dip->dio_bio;
7025 7032
7026 kfree(dip); 7033 kfree(dip);
7027 7034
7028 /* If we had an error make sure to clear the uptodate flag */ 7035 /* If we had an error make sure to clear the uptodate flag */
7029 if (err) 7036 if (err)
7030 clear_bit(BIO_UPTODATE, &bio->bi_flags); 7037 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
7031 dio_end_io(bio, err); 7038 dio_end_io(dio_bio, err);
7039 bio_put(bio);
7032} 7040}
7033 7041
7034static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw, 7042static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
@@ -7064,10 +7072,10 @@ static void btrfs_end_dio_bio(struct bio *bio, int err)
7064 if (!atomic_dec_and_test(&dip->pending_bios)) 7072 if (!atomic_dec_and_test(&dip->pending_bios))
7065 goto out; 7073 goto out;
7066 7074
7067 if (dip->errors) 7075 if (dip->errors) {
7068 bio_io_error(dip->orig_bio); 7076 bio_io_error(dip->orig_bio);
7069 else { 7077 } else {
7070 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags); 7078 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
7071 bio_endio(dip->orig_bio, 0); 7079 bio_endio(dip->orig_bio, 0);
7072 } 7080 }
7073out: 7081out:
@@ -7242,25 +7250,34 @@ out_err:
7242 return 0; 7250 return 0;
7243} 7251}
7244 7252
7245static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode, 7253static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7246 loff_t file_offset) 7254 struct inode *inode, loff_t file_offset)
7247{ 7255{
7248 struct btrfs_root *root = BTRFS_I(inode)->root; 7256 struct btrfs_root *root = BTRFS_I(inode)->root;
7249 struct btrfs_dio_private *dip; 7257 struct btrfs_dio_private *dip;
7250 struct bio_vec *bvec = bio->bi_io_vec; 7258 struct bio_vec *bvec = dio_bio->bi_io_vec;
7259 struct bio *io_bio;
7251 int skip_sum; 7260 int skip_sum;
7252 int write = rw & REQ_WRITE; 7261 int write = rw & REQ_WRITE;
7253 int ret = 0; 7262 int ret = 0;
7254 7263
7255 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; 7264 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7256 7265
7266 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
7267
7268 if (!io_bio) {
7269 ret = -ENOMEM;
7270 goto free_ordered;
7271 }
7272
7257 dip = kmalloc(sizeof(*dip), GFP_NOFS); 7273 dip = kmalloc(sizeof(*dip), GFP_NOFS);
7258 if (!dip) { 7274 if (!dip) {
7259 ret = -ENOMEM; 7275 ret = -ENOMEM;
7260 goto free_ordered; 7276 goto free_io_bio;
7261 } 7277 }
7262 7278
7263 dip->private = bio->bi_private; 7279 dip->private = dio_bio->bi_private;
7280 io_bio->bi_private = dio_bio->bi_private;
7264 dip->inode = inode; 7281 dip->inode = inode;
7265 dip->logical_offset = file_offset; 7282 dip->logical_offset = file_offset;
7266 7283
@@ -7268,22 +7285,27 @@ static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
7268 do { 7285 do {
7269 dip->bytes += bvec->bv_len; 7286 dip->bytes += bvec->bv_len;
7270 bvec++; 7287 bvec++;
7271 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1)); 7288 } while (bvec <= (dio_bio->bi_io_vec + dio_bio->bi_vcnt - 1));
7272 7289
7273 dip->disk_bytenr = (u64)bio->bi_sector << 9; 7290 dip->disk_bytenr = (u64)dio_bio->bi_sector << 9;
7274 bio->bi_private = dip; 7291 io_bio->bi_private = dip;
7275 dip->errors = 0; 7292 dip->errors = 0;
7276 dip->orig_bio = bio; 7293 dip->orig_bio = io_bio;
7294 dip->dio_bio = dio_bio;
7277 atomic_set(&dip->pending_bios, 0); 7295 atomic_set(&dip->pending_bios, 0);
7278 7296
7279 if (write) 7297 if (write)
7280 bio->bi_end_io = btrfs_endio_direct_write; 7298 io_bio->bi_end_io = btrfs_endio_direct_write;
7281 else 7299 else
7282 bio->bi_end_io = btrfs_endio_direct_read; 7300 io_bio->bi_end_io = btrfs_endio_direct_read;
7283 7301
7284 ret = btrfs_submit_direct_hook(rw, dip, skip_sum); 7302 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7285 if (!ret) 7303 if (!ret)
7286 return; 7304 return;
7305
7306free_io_bio:
7307 bio_put(io_bio);
7308
7287free_ordered: 7309free_ordered:
7288 /* 7310 /*
7289 * If this is a write, we need to clean up the reserved space and kill 7311 * If this is a write, we need to clean up the reserved space and kill
@@ -7299,7 +7321,7 @@ free_ordered:
7299 btrfs_put_ordered_extent(ordered); 7321 btrfs_put_ordered_extent(ordered);
7300 btrfs_put_ordered_extent(ordered); 7322 btrfs_put_ordered_extent(ordered);
7301 } 7323 }
7302 bio_endio(bio, ret); 7324 bio_endio(dio_bio, ret);
7303} 7325}
7304 7326
7305static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb, 7327static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
diff --git a/fs/btrfs/raid56.c b/fs/btrfs/raid56.c
index 0740621daf6c..0525e1389f5b 100644
--- a/fs/btrfs/raid56.c
+++ b/fs/btrfs/raid56.c
@@ -1050,7 +1050,7 @@ static int rbio_add_io_page(struct btrfs_raid_bio *rbio,
1050 } 1050 }
1051 1051
1052 /* put a new bio on the list */ 1052 /* put a new bio on the list */
1053 bio = bio_alloc(GFP_NOFS, bio_max_len >> PAGE_SHIFT?:1); 1053 bio = btrfs_io_bio_alloc(GFP_NOFS, bio_max_len >> PAGE_SHIFT?:1);
1054 if (!bio) 1054 if (!bio)
1055 return -ENOMEM; 1055 return -ENOMEM;
1056 1056
diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
index f489e24659a4..79bd479317cb 100644
--- a/fs/btrfs/scrub.c
+++ b/fs/btrfs/scrub.c
@@ -1296,7 +1296,7 @@ static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
1296 } 1296 }
1297 1297
1298 WARN_ON(!page->page); 1298 WARN_ON(!page->page);
1299 bio = bio_alloc(GFP_NOFS, 1); 1299 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
1300 if (!bio) { 1300 if (!bio) {
1301 page->io_error = 1; 1301 page->io_error = 1;
1302 sblock->no_io_error_seen = 0; 1302 sblock->no_io_error_seen = 0;
@@ -1431,7 +1431,7 @@ static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1431 return -EIO; 1431 return -EIO;
1432 } 1432 }
1433 1433
1434 bio = bio_alloc(GFP_NOFS, 1); 1434 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
1435 if (!bio) 1435 if (!bio)
1436 return -EIO; 1436 return -EIO;
1437 bio->bi_bdev = page_bad->dev->bdev; 1437 bio->bi_bdev = page_bad->dev->bdev;
@@ -1522,7 +1522,7 @@ again:
1522 sbio->dev = wr_ctx->tgtdev; 1522 sbio->dev = wr_ctx->tgtdev;
1523 bio = sbio->bio; 1523 bio = sbio->bio;
1524 if (!bio) { 1524 if (!bio) {
1525 bio = bio_alloc(GFP_NOFS, wr_ctx->pages_per_wr_bio); 1525 bio = btrfs_io_bio_alloc(GFP_NOFS, wr_ctx->pages_per_wr_bio);
1526 if (!bio) { 1526 if (!bio) {
1527 mutex_unlock(&wr_ctx->wr_lock); 1527 mutex_unlock(&wr_ctx->wr_lock);
1528 return -ENOMEM; 1528 return -ENOMEM;
@@ -1930,7 +1930,7 @@ again:
1930 sbio->dev = spage->dev; 1930 sbio->dev = spage->dev;
1931 bio = sbio->bio; 1931 bio = sbio->bio;
1932 if (!bio) { 1932 if (!bio) {
1933 bio = bio_alloc(GFP_NOFS, sctx->pages_per_rd_bio); 1933 bio = btrfs_io_bio_alloc(GFP_NOFS, sctx->pages_per_rd_bio);
1934 if (!bio) 1934 if (!bio)
1935 return -ENOMEM; 1935 return -ENOMEM;
1936 sbio->bio = bio; 1936 sbio->bio = bio;
@@ -3307,7 +3307,7 @@ static int write_page_nocow(struct scrub_ctx *sctx,
3307 "btrfs: scrub write_page_nocow(bdev == NULL) is unexpected!\n"); 3307 "btrfs: scrub write_page_nocow(bdev == NULL) is unexpected!\n");
3308 return -EIO; 3308 return -EIO;
3309 } 3309 }
3310 bio = bio_alloc(GFP_NOFS, 1); 3310 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
3311 if (!bio) { 3311 if (!bio) {
3312 spin_lock(&sctx->stat_lock); 3312 spin_lock(&sctx->stat_lock);
3313 sctx->stat.malloc_errors++; 3313 sctx->stat.malloc_errors++;
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index a191bac31d85..317afc7a2af4 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -5019,42 +5019,16 @@ int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5019 return 0; 5019 return 0;
5020} 5020}
5021 5021
5022static void *merge_stripe_index_into_bio_private(void *bi_private,
5023 unsigned int stripe_index)
5024{
5025 /*
5026 * with single, dup, RAID0, RAID1 and RAID10, stripe_index is
5027 * at most 1.
5028 * The alternative solution (instead of stealing bits from the
5029 * pointer) would be to allocate an intermediate structure
5030 * that contains the old private pointer plus the stripe_index.
5031 */
5032 BUG_ON((((uintptr_t)bi_private) & 3) != 0);
5033 BUG_ON(stripe_index > 3);
5034 return (void *)(((uintptr_t)bi_private) | stripe_index);
5035}
5036
5037static struct btrfs_bio *extract_bbio_from_bio_private(void *bi_private)
5038{
5039 return (struct btrfs_bio *)(((uintptr_t)bi_private) & ~((uintptr_t)3));
5040}
5041
5042static unsigned int extract_stripe_index_from_bio_private(void *bi_private)
5043{
5044 return (unsigned int)((uintptr_t)bi_private) & 3;
5045}
5046
5047static void btrfs_end_bio(struct bio *bio, int err) 5022static void btrfs_end_bio(struct bio *bio, int err)
5048{ 5023{
5049 struct btrfs_bio *bbio = extract_bbio_from_bio_private(bio->bi_private); 5024 struct btrfs_bio *bbio = bio->bi_private;
5050 int is_orig_bio = 0; 5025 int is_orig_bio = 0;
5051 5026
5052 if (err) { 5027 if (err) {
5053 atomic_inc(&bbio->error); 5028 atomic_inc(&bbio->error);
5054 if (err == -EIO || err == -EREMOTEIO) { 5029 if (err == -EIO || err == -EREMOTEIO) {
5055 unsigned int stripe_index = 5030 unsigned int stripe_index =
5056 extract_stripe_index_from_bio_private( 5031 btrfs_io_bio(bio)->stripe_index;
5057 bio->bi_private);
5058 struct btrfs_device *dev; 5032 struct btrfs_device *dev;
5059 5033
5060 BUG_ON(stripe_index >= bbio->num_stripes); 5034 BUG_ON(stripe_index >= bbio->num_stripes);
@@ -5084,8 +5058,7 @@ static void btrfs_end_bio(struct bio *bio, int err)
5084 } 5058 }
5085 bio->bi_private = bbio->private; 5059 bio->bi_private = bbio->private;
5086 bio->bi_end_io = bbio->end_io; 5060 bio->bi_end_io = bbio->end_io;
5087 bio->bi_bdev = (struct block_device *) 5061 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
5088 (unsigned long)bbio->mirror_num;
5089 /* only send an error to the higher layers if it is 5062 /* only send an error to the higher layers if it is
5090 * beyond the tolerance of the btrfs bio 5063 * beyond the tolerance of the btrfs bio
5091 */ 5064 */
@@ -5211,8 +5184,7 @@ static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5211 struct btrfs_device *dev = bbio->stripes[dev_nr].dev; 5184 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5212 5185
5213 bio->bi_private = bbio; 5186 bio->bi_private = bbio;
5214 bio->bi_private = merge_stripe_index_into_bio_private( 5187 btrfs_io_bio(bio)->stripe_index = dev_nr;
5215 bio->bi_private, (unsigned int)dev_nr);
5216 bio->bi_end_io = btrfs_end_bio; 5188 bio->bi_end_io = btrfs_end_bio;
5217 bio->bi_sector = physical >> 9; 5189 bio->bi_sector = physical >> 9;
5218#ifdef DEBUG 5190#ifdef DEBUG
@@ -5273,8 +5245,7 @@ static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5273 if (atomic_dec_and_test(&bbio->stripes_pending)) { 5245 if (atomic_dec_and_test(&bbio->stripes_pending)) {
5274 bio->bi_private = bbio->private; 5246 bio->bi_private = bbio->private;
5275 bio->bi_end_io = bbio->end_io; 5247 bio->bi_end_io = bbio->end_io;
5276 bio->bi_bdev = (struct block_device *) 5248 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
5277 (unsigned long)bbio->mirror_num;
5278 bio->bi_sector = logical >> 9; 5249 bio->bi_sector = logical >> 9;
5279 kfree(bbio); 5250 kfree(bbio);
5280 bio_endio(bio, -EIO); 5251 bio_endio(bio, -EIO);
@@ -5352,7 +5323,7 @@ int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
5352 } 5323 }
5353 5324
5354 if (dev_nr < total_devs - 1) { 5325 if (dev_nr < total_devs - 1) {
5355 bio = bio_clone(first_bio, GFP_NOFS); 5326 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
5356 BUG_ON(!bio); /* -ENOMEM */ 5327 BUG_ON(!bio); /* -ENOMEM */
5357 } else { 5328 } else {
5358 bio = first_bio; 5329 bio = first_bio;
diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
index 845ccbb0d2e3..f6247e2a47f7 100644
--- a/fs/btrfs/volumes.h
+++ b/fs/btrfs/volumes.h
@@ -152,6 +152,26 @@ struct btrfs_fs_devices {
152 int rotating; 152 int rotating;
153}; 153};
154 154
155/*
156 * we need the mirror number and stripe index to be passed around
157 * the call chain while we are processing end_io (especially errors).
158 * Really, what we need is a btrfs_bio structure that has this info
159 * and is properly sized with its stripe array, but we're not there
160 * quite yet. We have our own btrfs bioset, and all of the bios
161 * we allocate are actually btrfs_io_bios. We'll cram as much of
162 * struct btrfs_bio as we can into this over time.
163 */
164struct btrfs_io_bio {
165 unsigned long mirror_num;
166 unsigned long stripe_index;
167 struct bio bio;
168};
169
170static inline struct btrfs_io_bio *btrfs_io_bio(struct bio *bio)
171{
172 return container_of(bio, struct btrfs_io_bio, bio);
173}
174
155struct btrfs_bio_stripe { 175struct btrfs_bio_stripe {
156 struct btrfs_device *dev; 176 struct btrfs_device *dev;
157 u64 physical; 177 u64 physical;