aboutsummaryrefslogtreecommitdiffstats
path: root/fs/bio.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2013-05-08 13:13:35 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2013-05-08 13:13:35 -0400
commit4de13d7aa8f4d02f4dc99d4609575659f92b3c5a (patch)
tree3bc9729eabe79c6164cd29a5d605000bc82bf837 /fs/bio.c
parent5af43c24ca59a448c9312dd4a4a51d27ec3b9a73 (diff)
parentb8d4a5bf6a049303a29a3275f463f09a490b50ea (diff)
Merge branch 'for-3.10/core' of git://git.kernel.dk/linux-block
Pull block core updates from Jens Axboe: - Major bit is Kents prep work for immutable bio vecs. - Stable candidate fix for a scheduling-while-atomic in the queue bypass operation. - Fix for the hang on exceeded rq->datalen 32-bit unsigned when merging discard bios. - Tejuns changes to convert the writeback thread pool to the generic workqueue mechanism. - Runtime PM framework, SCSI patches exists on top of these in James' tree. - A few random fixes. * 'for-3.10/core' of git://git.kernel.dk/linux-block: (40 commits) relay: move remove_buf_file inside relay_close_buf partitions/efi.c: replace useless kzalloc's by kmalloc's fs/block_dev.c: fix iov_shorten() criteria in blkdev_aio_read() block: fix max discard sectors limit blkcg: fix "scheduling while atomic" in blk_queue_bypass_start Documentation: cfq-iosched: update documentation help for cfq tunables writeback: expose the bdi_wq workqueue writeback: replace custom worker pool implementation with unbound workqueue writeback: remove unused bdi_pending_list aoe: Fix unitialized var usage bio-integrity: Add explicit field for owner of bip_buf block: Add an explicit bio flag for bios that own their bvec block: Add bio_alloc_pages() block: Convert some code to bio_for_each_segment_all() block: Add bio_for_each_segment_all() bounce: Refactor __blk_queue_bounce to not use bi_io_vec raid1: use bio_copy_data() pktcdvd: Use bio_reset() in disabled code to kill bi_idx usage pktcdvd: use bio_copy_data() block: Add bio_copy_data() ...
Diffstat (limited to 'fs/bio.c')
-rw-r--r--fs/bio.c366
1 files changed, 324 insertions, 42 deletions
diff --git a/fs/bio.c b/fs/bio.c
index 954d73124b41..94bbc04dba77 100644
--- a/fs/bio.c
+++ b/fs/bio.c
@@ -161,12 +161,12 @@ unsigned int bvec_nr_vecs(unsigned short idx)
161 return bvec_slabs[idx].nr_vecs; 161 return bvec_slabs[idx].nr_vecs;
162} 162}
163 163
164void bvec_free_bs(struct bio_set *bs, struct bio_vec *bv, unsigned int idx) 164void bvec_free(mempool_t *pool, struct bio_vec *bv, unsigned int idx)
165{ 165{
166 BIO_BUG_ON(idx >= BIOVEC_NR_POOLS); 166 BIO_BUG_ON(idx >= BIOVEC_NR_POOLS);
167 167
168 if (idx == BIOVEC_MAX_IDX) 168 if (idx == BIOVEC_MAX_IDX)
169 mempool_free(bv, bs->bvec_pool); 169 mempool_free(bv, pool);
170 else { 170 else {
171 struct biovec_slab *bvs = bvec_slabs + idx; 171 struct biovec_slab *bvs = bvec_slabs + idx;
172 172
@@ -174,8 +174,8 @@ void bvec_free_bs(struct bio_set *bs, struct bio_vec *bv, unsigned int idx)
174 } 174 }
175} 175}
176 176
177struct bio_vec *bvec_alloc_bs(gfp_t gfp_mask, int nr, unsigned long *idx, 177struct bio_vec *bvec_alloc(gfp_t gfp_mask, int nr, unsigned long *idx,
178 struct bio_set *bs) 178 mempool_t *pool)
179{ 179{
180 struct bio_vec *bvl; 180 struct bio_vec *bvl;
181 181
@@ -211,7 +211,7 @@ struct bio_vec *bvec_alloc_bs(gfp_t gfp_mask, int nr, unsigned long *idx,
211 */ 211 */
212 if (*idx == BIOVEC_MAX_IDX) { 212 if (*idx == BIOVEC_MAX_IDX) {
213fallback: 213fallback:
214 bvl = mempool_alloc(bs->bvec_pool, gfp_mask); 214 bvl = mempool_alloc(pool, gfp_mask);
215 } else { 215 } else {
216 struct biovec_slab *bvs = bvec_slabs + *idx; 216 struct biovec_slab *bvs = bvec_slabs + *idx;
217 gfp_t __gfp_mask = gfp_mask & ~(__GFP_WAIT | __GFP_IO); 217 gfp_t __gfp_mask = gfp_mask & ~(__GFP_WAIT | __GFP_IO);
@@ -253,8 +253,8 @@ static void bio_free(struct bio *bio)
253 __bio_free(bio); 253 __bio_free(bio);
254 254
255 if (bs) { 255 if (bs) {
256 if (bio_has_allocated_vec(bio)) 256 if (bio_flagged(bio, BIO_OWNS_VEC))
257 bvec_free_bs(bs, bio->bi_io_vec, BIO_POOL_IDX(bio)); 257 bvec_free(bs->bvec_pool, bio->bi_io_vec, BIO_POOL_IDX(bio));
258 258
259 /* 259 /*
260 * If we have front padding, adjust the bio pointer before freeing 260 * If we have front padding, adjust the bio pointer before freeing
@@ -298,6 +298,54 @@ void bio_reset(struct bio *bio)
298} 298}
299EXPORT_SYMBOL(bio_reset); 299EXPORT_SYMBOL(bio_reset);
300 300
301static void bio_alloc_rescue(struct work_struct *work)
302{
303 struct bio_set *bs = container_of(work, struct bio_set, rescue_work);
304 struct bio *bio;
305
306 while (1) {
307 spin_lock(&bs->rescue_lock);
308 bio = bio_list_pop(&bs->rescue_list);
309 spin_unlock(&bs->rescue_lock);
310
311 if (!bio)
312 break;
313
314 generic_make_request(bio);
315 }
316}
317
318static void punt_bios_to_rescuer(struct bio_set *bs)
319{
320 struct bio_list punt, nopunt;
321 struct bio *bio;
322
323 /*
324 * In order to guarantee forward progress we must punt only bios that
325 * were allocated from this bio_set; otherwise, if there was a bio on
326 * there for a stacking driver higher up in the stack, processing it
327 * could require allocating bios from this bio_set, and doing that from
328 * our own rescuer would be bad.
329 *
330 * Since bio lists are singly linked, pop them all instead of trying to
331 * remove from the middle of the list:
332 */
333
334 bio_list_init(&punt);
335 bio_list_init(&nopunt);
336
337 while ((bio = bio_list_pop(current->bio_list)))
338 bio_list_add(bio->bi_pool == bs ? &punt : &nopunt, bio);
339
340 *current->bio_list = nopunt;
341
342 spin_lock(&bs->rescue_lock);
343 bio_list_merge(&bs->rescue_list, &punt);
344 spin_unlock(&bs->rescue_lock);
345
346 queue_work(bs->rescue_workqueue, &bs->rescue_work);
347}
348
301/** 349/**
302 * bio_alloc_bioset - allocate a bio for I/O 350 * bio_alloc_bioset - allocate a bio for I/O
303 * @gfp_mask: the GFP_ mask given to the slab allocator 351 * @gfp_mask: the GFP_ mask given to the slab allocator
@@ -315,11 +363,27 @@ EXPORT_SYMBOL(bio_reset);
315 * previously allocated bio for IO before attempting to allocate a new one. 363 * previously allocated bio for IO before attempting to allocate a new one.
316 * Failure to do so can cause deadlocks under memory pressure. 364 * Failure to do so can cause deadlocks under memory pressure.
317 * 365 *
366 * Note that when running under generic_make_request() (i.e. any block
367 * driver), bios are not submitted until after you return - see the code in
368 * generic_make_request() that converts recursion into iteration, to prevent
369 * stack overflows.
370 *
371 * This would normally mean allocating multiple bios under
372 * generic_make_request() would be susceptible to deadlocks, but we have
373 * deadlock avoidance code that resubmits any blocked bios from a rescuer
374 * thread.
375 *
376 * However, we do not guarantee forward progress for allocations from other
377 * mempools. Doing multiple allocations from the same mempool under
378 * generic_make_request() should be avoided - instead, use bio_set's front_pad
379 * for per bio allocations.
380 *
318 * RETURNS: 381 * RETURNS:
319 * Pointer to new bio on success, NULL on failure. 382 * Pointer to new bio on success, NULL on failure.
320 */ 383 */
321struct bio *bio_alloc_bioset(gfp_t gfp_mask, int nr_iovecs, struct bio_set *bs) 384struct bio *bio_alloc_bioset(gfp_t gfp_mask, int nr_iovecs, struct bio_set *bs)
322{ 385{
386 gfp_t saved_gfp = gfp_mask;
323 unsigned front_pad; 387 unsigned front_pad;
324 unsigned inline_vecs; 388 unsigned inline_vecs;
325 unsigned long idx = BIO_POOL_NONE; 389 unsigned long idx = BIO_POOL_NONE;
@@ -337,7 +401,37 @@ struct bio *bio_alloc_bioset(gfp_t gfp_mask, int nr_iovecs, struct bio_set *bs)
337 front_pad = 0; 401 front_pad = 0;
338 inline_vecs = nr_iovecs; 402 inline_vecs = nr_iovecs;
339 } else { 403 } else {
404 /*
405 * generic_make_request() converts recursion to iteration; this
406 * means if we're running beneath it, any bios we allocate and
407 * submit will not be submitted (and thus freed) until after we
408 * return.
409 *
410 * This exposes us to a potential deadlock if we allocate
411 * multiple bios from the same bio_set() while running
412 * underneath generic_make_request(). If we were to allocate
413 * multiple bios (say a stacking block driver that was splitting
414 * bios), we would deadlock if we exhausted the mempool's
415 * reserve.
416 *
417 * We solve this, and guarantee forward progress, with a rescuer
418 * workqueue per bio_set. If we go to allocate and there are
419 * bios on current->bio_list, we first try the allocation
420 * without __GFP_WAIT; if that fails, we punt those bios we
421 * would be blocking to the rescuer workqueue before we retry
422 * with the original gfp_flags.
423 */
424
425 if (current->bio_list && !bio_list_empty(current->bio_list))
426 gfp_mask &= ~__GFP_WAIT;
427
340 p = mempool_alloc(bs->bio_pool, gfp_mask); 428 p = mempool_alloc(bs->bio_pool, gfp_mask);
429 if (!p && gfp_mask != saved_gfp) {
430 punt_bios_to_rescuer(bs);
431 gfp_mask = saved_gfp;
432 p = mempool_alloc(bs->bio_pool, gfp_mask);
433 }
434
341 front_pad = bs->front_pad; 435 front_pad = bs->front_pad;
342 inline_vecs = BIO_INLINE_VECS; 436 inline_vecs = BIO_INLINE_VECS;
343 } 437 }
@@ -349,9 +443,17 @@ struct bio *bio_alloc_bioset(gfp_t gfp_mask, int nr_iovecs, struct bio_set *bs)
349 bio_init(bio); 443 bio_init(bio);
350 444
351 if (nr_iovecs > inline_vecs) { 445 if (nr_iovecs > inline_vecs) {
352 bvl = bvec_alloc_bs(gfp_mask, nr_iovecs, &idx, bs); 446 bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx, bs->bvec_pool);
447 if (!bvl && gfp_mask != saved_gfp) {
448 punt_bios_to_rescuer(bs);
449 gfp_mask = saved_gfp;
450 bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx, bs->bvec_pool);
451 }
452
353 if (unlikely(!bvl)) 453 if (unlikely(!bvl))
354 goto err_free; 454 goto err_free;
455
456 bio->bi_flags |= 1 << BIO_OWNS_VEC;
355 } else if (nr_iovecs) { 457 } else if (nr_iovecs) {
356 bvl = bio->bi_inline_vecs; 458 bvl = bio->bi_inline_vecs;
357 } 459 }
@@ -653,6 +755,181 @@ int bio_add_page(struct bio *bio, struct page *page, unsigned int len,
653} 755}
654EXPORT_SYMBOL(bio_add_page); 756EXPORT_SYMBOL(bio_add_page);
655 757
758struct submit_bio_ret {
759 struct completion event;
760 int error;
761};
762
763static void submit_bio_wait_endio(struct bio *bio, int error)
764{
765 struct submit_bio_ret *ret = bio->bi_private;
766
767 ret->error = error;
768 complete(&ret->event);
769}
770
771/**
772 * submit_bio_wait - submit a bio, and wait until it completes
773 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
774 * @bio: The &struct bio which describes the I/O
775 *
776 * Simple wrapper around submit_bio(). Returns 0 on success, or the error from
777 * bio_endio() on failure.
778 */
779int submit_bio_wait(int rw, struct bio *bio)
780{
781 struct submit_bio_ret ret;
782
783 rw |= REQ_SYNC;
784 init_completion(&ret.event);
785 bio->bi_private = &ret;
786 bio->bi_end_io = submit_bio_wait_endio;
787 submit_bio(rw, bio);
788 wait_for_completion(&ret.event);
789
790 return ret.error;
791}
792EXPORT_SYMBOL(submit_bio_wait);
793
794/**
795 * bio_advance - increment/complete a bio by some number of bytes
796 * @bio: bio to advance
797 * @bytes: number of bytes to complete
798 *
799 * This updates bi_sector, bi_size and bi_idx; if the number of bytes to
800 * complete doesn't align with a bvec boundary, then bv_len and bv_offset will
801 * be updated on the last bvec as well.
802 *
803 * @bio will then represent the remaining, uncompleted portion of the io.
804 */
805void bio_advance(struct bio *bio, unsigned bytes)
806{
807 if (bio_integrity(bio))
808 bio_integrity_advance(bio, bytes);
809
810 bio->bi_sector += bytes >> 9;
811 bio->bi_size -= bytes;
812
813 if (bio->bi_rw & BIO_NO_ADVANCE_ITER_MASK)
814 return;
815
816 while (bytes) {
817 if (unlikely(bio->bi_idx >= bio->bi_vcnt)) {
818 WARN_ONCE(1, "bio idx %d >= vcnt %d\n",
819 bio->bi_idx, bio->bi_vcnt);
820 break;
821 }
822
823 if (bytes >= bio_iovec(bio)->bv_len) {
824 bytes -= bio_iovec(bio)->bv_len;
825 bio->bi_idx++;
826 } else {
827 bio_iovec(bio)->bv_len -= bytes;
828 bio_iovec(bio)->bv_offset += bytes;
829 bytes = 0;
830 }
831 }
832}
833EXPORT_SYMBOL(bio_advance);
834
835/**
836 * bio_alloc_pages - allocates a single page for each bvec in a bio
837 * @bio: bio to allocate pages for
838 * @gfp_mask: flags for allocation
839 *
840 * Allocates pages up to @bio->bi_vcnt.
841 *
842 * Returns 0 on success, -ENOMEM on failure. On failure, any allocated pages are
843 * freed.
844 */
845int bio_alloc_pages(struct bio *bio, gfp_t gfp_mask)
846{
847 int i;
848 struct bio_vec *bv;
849
850 bio_for_each_segment_all(bv, bio, i) {
851 bv->bv_page = alloc_page(gfp_mask);
852 if (!bv->bv_page) {
853 while (--bv >= bio->bi_io_vec)
854 __free_page(bv->bv_page);
855 return -ENOMEM;
856 }
857 }
858
859 return 0;
860}
861EXPORT_SYMBOL(bio_alloc_pages);
862
863/**
864 * bio_copy_data - copy contents of data buffers from one chain of bios to
865 * another
866 * @src: source bio list
867 * @dst: destination bio list
868 *
869 * If @src and @dst are single bios, bi_next must be NULL - otherwise, treats
870 * @src and @dst as linked lists of bios.
871 *
872 * Stops when it reaches the end of either @src or @dst - that is, copies
873 * min(src->bi_size, dst->bi_size) bytes (or the equivalent for lists of bios).
874 */
875void bio_copy_data(struct bio *dst, struct bio *src)
876{
877 struct bio_vec *src_bv, *dst_bv;
878 unsigned src_offset, dst_offset, bytes;
879 void *src_p, *dst_p;
880
881 src_bv = bio_iovec(src);
882 dst_bv = bio_iovec(dst);
883
884 src_offset = src_bv->bv_offset;
885 dst_offset = dst_bv->bv_offset;
886
887 while (1) {
888 if (src_offset == src_bv->bv_offset + src_bv->bv_len) {
889 src_bv++;
890 if (src_bv == bio_iovec_idx(src, src->bi_vcnt)) {
891 src = src->bi_next;
892 if (!src)
893 break;
894
895 src_bv = bio_iovec(src);
896 }
897
898 src_offset = src_bv->bv_offset;
899 }
900
901 if (dst_offset == dst_bv->bv_offset + dst_bv->bv_len) {
902 dst_bv++;
903 if (dst_bv == bio_iovec_idx(dst, dst->bi_vcnt)) {
904 dst = dst->bi_next;
905 if (!dst)
906 break;
907
908 dst_bv = bio_iovec(dst);
909 }
910
911 dst_offset = dst_bv->bv_offset;
912 }
913
914 bytes = min(dst_bv->bv_offset + dst_bv->bv_len - dst_offset,
915 src_bv->bv_offset + src_bv->bv_len - src_offset);
916
917 src_p = kmap_atomic(src_bv->bv_page);
918 dst_p = kmap_atomic(dst_bv->bv_page);
919
920 memcpy(dst_p + dst_bv->bv_offset,
921 src_p + src_bv->bv_offset,
922 bytes);
923
924 kunmap_atomic(dst_p);
925 kunmap_atomic(src_p);
926
927 src_offset += bytes;
928 dst_offset += bytes;
929 }
930}
931EXPORT_SYMBOL(bio_copy_data);
932
656struct bio_map_data { 933struct bio_map_data {
657 struct bio_vec *iovecs; 934 struct bio_vec *iovecs;
658 struct sg_iovec *sgvecs; 935 struct sg_iovec *sgvecs;
@@ -715,7 +992,7 @@ static int __bio_copy_iov(struct bio *bio, struct bio_vec *iovecs,
715 int iov_idx = 0; 992 int iov_idx = 0;
716 unsigned int iov_off = 0; 993 unsigned int iov_off = 0;
717 994
718 __bio_for_each_segment(bvec, bio, i, 0) { 995 bio_for_each_segment_all(bvec, bio, i) {
719 char *bv_addr = page_address(bvec->bv_page); 996 char *bv_addr = page_address(bvec->bv_page);
720 unsigned int bv_len = iovecs[i].bv_len; 997 unsigned int bv_len = iovecs[i].bv_len;
721 998
@@ -897,7 +1174,7 @@ struct bio *bio_copy_user_iov(struct request_queue *q,
897 return bio; 1174 return bio;
898cleanup: 1175cleanup:
899 if (!map_data) 1176 if (!map_data)
900 bio_for_each_segment(bvec, bio, i) 1177 bio_for_each_segment_all(bvec, bio, i)
901 __free_page(bvec->bv_page); 1178 __free_page(bvec->bv_page);
902 1179
903 bio_put(bio); 1180 bio_put(bio);
@@ -1111,7 +1388,7 @@ static void __bio_unmap_user(struct bio *bio)
1111 /* 1388 /*
1112 * make sure we dirty pages we wrote to 1389 * make sure we dirty pages we wrote to
1113 */ 1390 */
1114 __bio_for_each_segment(bvec, bio, i, 0) { 1391 bio_for_each_segment_all(bvec, bio, i) {
1115 if (bio_data_dir(bio) == READ) 1392 if (bio_data_dir(bio) == READ)
1116 set_page_dirty_lock(bvec->bv_page); 1393 set_page_dirty_lock(bvec->bv_page);
1117 1394
@@ -1217,7 +1494,7 @@ static void bio_copy_kern_endio(struct bio *bio, int err)
1217 int i; 1494 int i;
1218 char *p = bmd->sgvecs[0].iov_base; 1495 char *p = bmd->sgvecs[0].iov_base;
1219 1496
1220 __bio_for_each_segment(bvec, bio, i, 0) { 1497 bio_for_each_segment_all(bvec, bio, i) {
1221 char *addr = page_address(bvec->bv_page); 1498 char *addr = page_address(bvec->bv_page);
1222 int len = bmd->iovecs[i].bv_len; 1499 int len = bmd->iovecs[i].bv_len;
1223 1500
@@ -1257,7 +1534,7 @@ struct bio *bio_copy_kern(struct request_queue *q, void *data, unsigned int len,
1257 if (!reading) { 1534 if (!reading) {
1258 void *p = data; 1535 void *p = data;
1259 1536
1260 bio_for_each_segment(bvec, bio, i) { 1537 bio_for_each_segment_all(bvec, bio, i) {
1261 char *addr = page_address(bvec->bv_page); 1538 char *addr = page_address(bvec->bv_page);
1262 1539
1263 memcpy(addr, p, bvec->bv_len); 1540 memcpy(addr, p, bvec->bv_len);
@@ -1302,11 +1579,11 @@ EXPORT_SYMBOL(bio_copy_kern);
1302 */ 1579 */
1303void bio_set_pages_dirty(struct bio *bio) 1580void bio_set_pages_dirty(struct bio *bio)
1304{ 1581{
1305 struct bio_vec *bvec = bio->bi_io_vec; 1582 struct bio_vec *bvec;
1306 int i; 1583 int i;
1307 1584
1308 for (i = 0; i < bio->bi_vcnt; i++) { 1585 bio_for_each_segment_all(bvec, bio, i) {
1309 struct page *page = bvec[i].bv_page; 1586 struct page *page = bvec->bv_page;
1310 1587
1311 if (page && !PageCompound(page)) 1588 if (page && !PageCompound(page))
1312 set_page_dirty_lock(page); 1589 set_page_dirty_lock(page);
@@ -1315,11 +1592,11 @@ void bio_set_pages_dirty(struct bio *bio)
1315 1592
1316static void bio_release_pages(struct bio *bio) 1593static void bio_release_pages(struct bio *bio)
1317{ 1594{
1318 struct bio_vec *bvec = bio->bi_io_vec; 1595 struct bio_vec *bvec;
1319 int i; 1596 int i;
1320 1597
1321 for (i = 0; i < bio->bi_vcnt; i++) { 1598 bio_for_each_segment_all(bvec, bio, i) {
1322 struct page *page = bvec[i].bv_page; 1599 struct page *page = bvec->bv_page;
1323 1600
1324 if (page) 1601 if (page)
1325 put_page(page); 1602 put_page(page);
@@ -1368,16 +1645,16 @@ static void bio_dirty_fn(struct work_struct *work)
1368 1645
1369void bio_check_pages_dirty(struct bio *bio) 1646void bio_check_pages_dirty(struct bio *bio)
1370{ 1647{
1371 struct bio_vec *bvec = bio->bi_io_vec; 1648 struct bio_vec *bvec;
1372 int nr_clean_pages = 0; 1649 int nr_clean_pages = 0;
1373 int i; 1650 int i;
1374 1651
1375 for (i = 0; i < bio->bi_vcnt; i++) { 1652 bio_for_each_segment_all(bvec, bio, i) {
1376 struct page *page = bvec[i].bv_page; 1653 struct page *page = bvec->bv_page;
1377 1654
1378 if (PageDirty(page) || PageCompound(page)) { 1655 if (PageDirty(page) || PageCompound(page)) {
1379 page_cache_release(page); 1656 page_cache_release(page);
1380 bvec[i].bv_page = NULL; 1657 bvec->bv_page = NULL;
1381 } else { 1658 } else {
1382 nr_clean_pages++; 1659 nr_clean_pages++;
1383 } 1660 }
@@ -1478,8 +1755,7 @@ struct bio_pair *bio_split(struct bio *bi, int first_sectors)
1478 trace_block_split(bdev_get_queue(bi->bi_bdev), bi, 1755 trace_block_split(bdev_get_queue(bi->bi_bdev), bi,
1479 bi->bi_sector + first_sectors); 1756 bi->bi_sector + first_sectors);
1480 1757
1481 BUG_ON(bi->bi_vcnt != 1 && bi->bi_vcnt != 0); 1758 BUG_ON(bio_segments(bi) > 1);
1482 BUG_ON(bi->bi_idx != 0);
1483 atomic_set(&bp->cnt, 3); 1759 atomic_set(&bp->cnt, 3);
1484 bp->error = 0; 1760 bp->error = 0;
1485 bp->bio1 = *bi; 1761 bp->bio1 = *bi;
@@ -1489,8 +1765,8 @@ struct bio_pair *bio_split(struct bio *bi, int first_sectors)
1489 bp->bio1.bi_size = first_sectors << 9; 1765 bp->bio1.bi_size = first_sectors << 9;
1490 1766
1491 if (bi->bi_vcnt != 0) { 1767 if (bi->bi_vcnt != 0) {
1492 bp->bv1 = bi->bi_io_vec[0]; 1768 bp->bv1 = *bio_iovec(bi);
1493 bp->bv2 = bi->bi_io_vec[0]; 1769 bp->bv2 = *bio_iovec(bi);
1494 1770
1495 if (bio_is_rw(bi)) { 1771 if (bio_is_rw(bi)) {
1496 bp->bv2.bv_offset += first_sectors << 9; 1772 bp->bv2.bv_offset += first_sectors << 9;
@@ -1542,7 +1818,7 @@ sector_t bio_sector_offset(struct bio *bio, unsigned short index,
1542 if (index >= bio->bi_idx) 1818 if (index >= bio->bi_idx)
1543 index = bio->bi_vcnt - 1; 1819 index = bio->bi_vcnt - 1;
1544 1820
1545 __bio_for_each_segment(bv, bio, i, 0) { 1821 bio_for_each_segment_all(bv, bio, i) {
1546 if (i == index) { 1822 if (i == index) {
1547 if (offset > bv->bv_offset) 1823 if (offset > bv->bv_offset)
1548 sectors += (offset - bv->bv_offset) / sector_sz; 1824 sectors += (offset - bv->bv_offset) / sector_sz;
@@ -1560,29 +1836,25 @@ EXPORT_SYMBOL(bio_sector_offset);
1560 * create memory pools for biovec's in a bio_set. 1836 * create memory pools for biovec's in a bio_set.
1561 * use the global biovec slabs created for general use. 1837 * use the global biovec slabs created for general use.
1562 */ 1838 */
1563static int biovec_create_pools(struct bio_set *bs, int pool_entries) 1839mempool_t *biovec_create_pool(struct bio_set *bs, int pool_entries)
1564{ 1840{
1565 struct biovec_slab *bp = bvec_slabs + BIOVEC_MAX_IDX; 1841 struct biovec_slab *bp = bvec_slabs + BIOVEC_MAX_IDX;
1566 1842
1567 bs->bvec_pool = mempool_create_slab_pool(pool_entries, bp->slab); 1843 return mempool_create_slab_pool(pool_entries, bp->slab);
1568 if (!bs->bvec_pool)
1569 return -ENOMEM;
1570
1571 return 0;
1572}
1573
1574static void biovec_free_pools(struct bio_set *bs)
1575{
1576 mempool_destroy(bs->bvec_pool);
1577} 1844}
1578 1845
1579void bioset_free(struct bio_set *bs) 1846void bioset_free(struct bio_set *bs)
1580{ 1847{
1848 if (bs->rescue_workqueue)
1849 destroy_workqueue(bs->rescue_workqueue);
1850
1581 if (bs->bio_pool) 1851 if (bs->bio_pool)
1582 mempool_destroy(bs->bio_pool); 1852 mempool_destroy(bs->bio_pool);
1583 1853
1854 if (bs->bvec_pool)
1855 mempool_destroy(bs->bvec_pool);
1856
1584 bioset_integrity_free(bs); 1857 bioset_integrity_free(bs);
1585 biovec_free_pools(bs);
1586 bio_put_slab(bs); 1858 bio_put_slab(bs);
1587 1859
1588 kfree(bs); 1860 kfree(bs);
@@ -1613,6 +1885,10 @@ struct bio_set *bioset_create(unsigned int pool_size, unsigned int front_pad)
1613 1885
1614 bs->front_pad = front_pad; 1886 bs->front_pad = front_pad;
1615 1887
1888 spin_lock_init(&bs->rescue_lock);
1889 bio_list_init(&bs->rescue_list);
1890 INIT_WORK(&bs->rescue_work, bio_alloc_rescue);
1891
1616 bs->bio_slab = bio_find_or_create_slab(front_pad + back_pad); 1892 bs->bio_slab = bio_find_or_create_slab(front_pad + back_pad);
1617 if (!bs->bio_slab) { 1893 if (!bs->bio_slab) {
1618 kfree(bs); 1894 kfree(bs);
@@ -1623,9 +1899,15 @@ struct bio_set *bioset_create(unsigned int pool_size, unsigned int front_pad)
1623 if (!bs->bio_pool) 1899 if (!bs->bio_pool)
1624 goto bad; 1900 goto bad;
1625 1901
1626 if (!biovec_create_pools(bs, pool_size)) 1902 bs->bvec_pool = biovec_create_pool(bs, pool_size);
1627 return bs; 1903 if (!bs->bvec_pool)
1904 goto bad;
1905
1906 bs->rescue_workqueue = alloc_workqueue("bioset", WQ_MEM_RECLAIM, 0);
1907 if (!bs->rescue_workqueue)
1908 goto bad;
1628 1909
1910 return bs;
1629bad: 1911bad:
1630 bioset_free(bs); 1912 bioset_free(bs);
1631 return NULL; 1913 return NULL;