23 #include <linux/slab.h>
25 #include <linux/kernel.h>
26 #include <linux/export.h>
38 #define BIO_INLINE_VECS 4
47 #define BV(x) { .nr_vecs = x, .name = "biovec-"__stringify(x) }
48 static struct biovec_slab bvec_slabs[BIOVEC_NR_POOLS]
__read_mostly = {
49 BV(1),
BV(4),
BV(16),
BV(64),
BV(128),
BV(BIO_MAX_PAGES),
71 static unsigned int bio_slab_nr, bio_slab_max;
75 unsigned int sz =
sizeof(
struct bio) + extra_size;
77 struct bio_slab *bslab, *new_bio_slabs;
78 unsigned int new_bio_slab_max;
79 unsigned int i,
entry = -1;
84 while (i < bio_slab_nr) {
85 bslab = &bio_slabs[
i];
87 if (!bslab->
slab && entry == -1)
100 if (bio_slab_nr == bio_slab_max && entry == -1) {
101 new_bio_slab_max = bio_slab_max << 1;
103 new_bio_slab_max *
sizeof(
struct bio_slab),
107 bio_slab_max = new_bio_slab_max;
108 bio_slabs = new_bio_slabs;
111 entry = bio_slab_nr++;
113 bslab = &bio_slabs[
entry];
129 static void bio_put_slab(
struct bio_set *bs)
136 for (i = 0; i < bio_slab_nr; i++) {
137 if (bs->bio_slab == bio_slabs[i].
slab) {
138 bslab = &bio_slabs[
i];
143 if (
WARN(!bslab,
KERN_ERR "bio: unable to find slab!\n"))
160 return bvec_slabs[
idx].nr_vecs;
165 BIO_BUG_ON(idx >= BIOVEC_NR_POOLS);
167 if (idx == BIOVEC_MAX_IDX)
170 struct biovec_slab *bvs = bvec_slabs +
idx;
200 case 129 ... BIO_MAX_PAGES:
211 if (*idx == BIOVEC_MAX_IDX) {
215 struct biovec_slab *bvs = bvec_slabs + *
idx;
231 *idx = BIOVEC_MAX_IDX;
239 static void __bio_free(
struct bio *bio)
241 bio_disassociate_task(bio);
243 if (bio_integrity(bio))
247 static void bio_free(
struct bio *bio)
249 struct bio_set *bs = bio->bi_pool;
255 if (bio_has_allocated_vec(bio))
273 memset(bio, 0,
sizeof(*bio));
274 bio->bi_flags = 1 << BIO_UPTODATE;
291 unsigned long flags = bio->bi_flags & (~0
UL << BIO_RESET_BITS);
295 memset(bio, 0, BIO_RESET_BYTES);
296 bio->bi_flags = flags|(1 << BIO_UPTODATE);
323 unsigned inline_vecs;
324 unsigned long idx = BIO_POOL_NONE;
325 struct bio_vec *bvl =
NULL;
333 p =
kmalloc(
sizeof(
struct bio) +
334 nr_iovecs *
sizeof(
struct bio_vec),
337 inline_vecs = nr_iovecs;
340 front_pad = bs->front_pad;
350 if (nr_iovecs > inline_vecs) {
354 }
else if (nr_iovecs) {
355 bvl = bio->bi_inline_vecs;
359 bio->bi_flags |= idx << BIO_POOL_OFFSET;
360 bio->bi_max_vecs = nr_iovecs;
361 bio->bi_io_vec = bvl;
376 bio_for_each_segment(bv, bio, i) {
377 char *
data = bvec_kmap_irq(bv, &flags);
378 memset(data, 0, bv->bv_len);
380 bvec_kunmap_irq(data, &flags);
407 if (
unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
410 return bio->bi_phys_segments;
425 memcpy(bio->bi_io_vec, bio_src->bi_io_vec,
426 bio_src->bi_max_vecs *
sizeof(
struct bio_vec));
432 bio->bi_sector = bio_src->bi_sector;
433 bio->bi_bdev = bio_src->bi_bdev;
434 bio->bi_flags |= 1 << BIO_CLONED;
435 bio->bi_rw = bio_src->bi_rw;
436 bio->bi_vcnt = bio_src->bi_vcnt;
437 bio->bi_size = bio_src->bi_size;
438 bio->bi_idx = bio_src->bi_idx;
461 if (bio_integrity(bio)) {
490 nr_pages =
min_t(
unsigned,
491 queue_max_segments(q),
492 queue_max_sectors(q) / (
PAGE_SIZE >> 9) + 1);
494 return min_t(
unsigned, nr_pages, BIO_MAX_PAGES);
501 unsigned short max_sectors)
503 int retried_segments = 0;
504 struct bio_vec *bvec;
509 if (
unlikely(bio_flagged(bio, BIO_CLONED)))
512 if (((bio->bi_size + len) >> 9) > max_sectors)
520 if (bio->bi_vcnt > 0) {
521 struct bio_vec *
prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
523 if (page == prev->bv_page &&
524 offset == prev->bv_offset + prev->bv_len) {
525 unsigned int prev_bv_len = prev->bv_len;
528 if (q->merge_bvec_fn) {
529 struct bvec_merge_data bvm = {
534 .bi_bdev = bio->bi_bdev,
535 .bi_sector = bio->bi_sector,
536 .bi_size = bio->bi_size - prev_bv_len,
540 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len) {
550 if (bio->bi_vcnt >= bio->bi_max_vecs)
558 while (bio->bi_phys_segments >= queue_max_segments(q)) {
560 if (retried_segments)
563 retried_segments = 1;
571 bvec = &bio->bi_io_vec[bio->bi_vcnt];
572 bvec->bv_page =
page;
581 if (q->merge_bvec_fn) {
582 struct bvec_merge_data bvm = {
583 .bi_bdev = bio->bi_bdev,
584 .bi_sector = bio->bi_sector,
585 .bi_size = bio->bi_size,
593 if (q->merge_bvec_fn(q, &bvm, bvec) < bvec->bv_len) {
594 bvec->bv_page =
NULL;
602 if (bio->bi_vcnt && (BIOVEC_PHYS_MERGEABLE(bvec-1, bvec)))
603 bio->bi_flags &= ~(1 << BIO_SEG_VALID);
606 bio->bi_phys_segments++;
628 unsigned int len,
unsigned int offset)
630 return __bio_add_page(q, bio, page, len, offset,
631 queue_max_hw_sectors(q));
651 return __bio_add_page(q, bio, page, len, offset, queue_max_sectors(q));
662 static void bio_set_map_data(
struct bio_map_data *bmd,
struct bio *bio,
663 struct sg_iovec *iov,
int iov_count,
666 memcpy(bmd->
iovecs, bio->bi_io_vec,
sizeof(
struct bio_vec) * bio->bi_vcnt);
670 bio->bi_private = bmd;
680 static struct bio_map_data *bio_alloc_map_data(
int nr_segs,
681 unsigned int iov_count,
689 bmd =
kmalloc(
sizeof(*bmd), gfp_mask);
693 bmd->
iovecs =
kmalloc(
sizeof(
struct bio_vec) * nr_segs, gfp_mask);
708 static int __bio_copy_iov(
struct bio *bio,
struct bio_vec *
iovecs,
709 struct sg_iovec *iov,
int iov_count,
710 int to_user,
int from_user,
int do_free_page)
713 struct bio_vec *bvec;
715 unsigned int iov_off = 0;
717 __bio_for_each_segment(bvec, bio, i, 0) {
719 unsigned int bv_len = iovecs[
i].bv_len;
721 while (bv_len && iov_idx < iov_count) {
725 bytes =
min_t(
unsigned int,
726 iov[iov_idx].iov_len - iov_off, bv_len);
727 iov_addr = iov[iov_idx].
iov_base + iov_off;
747 if (iov[iov_idx].iov_len == iov_off) {
772 if (!bio_flagged(bio, BIO_NULL_MAPPED))
776 bio_free_map_data(bmd);
796 struct rq_map_data *map_data,
797 struct sg_iovec *iov,
int iov_count,
798 int write_to_vm,
gfp_t gfp_mask)
801 struct bio_vec *bvec;
806 unsigned int len = 0;
807 unsigned int offset = map_data ? map_data->offset & ~
PAGE_MASK : 0;
809 for (i = 0; i < iov_count; i++) {
814 uaddr = (
unsigned long)iov[i].iov_base;
824 nr_pages += end -
start;
831 bmd = bio_alloc_map_data(nr_pages, iov_count, gfp_mask);
836 bio = bio_kmalloc(gfp_mask, nr_pages);
846 nr_pages = 1 << map_data->page_order;
858 if (i == map_data->nr_entries * nr_pages) {
863 page = map_data->
pages[i / nr_pages];
864 page += (i % nr_pages);
888 if ((!write_to_vm && (!map_data || !map_data->null_mapped)) ||
889 (map_data && map_data->from_user)) {
890 ret = __bio_copy_iov(bio, bio->bi_io_vec, iov, iov_count, 0, 1, 0);
895 bio_set_map_data(bmd, bio, iov, iov_count, map_data ? 0 : 1);
899 bio_for_each_segment(bvec, bio, i)
904 bio_free_map_data(bmd);
922 unsigned long uaddr,
unsigned int len,
923 int write_to_vm,
gfp_t gfp_mask)
934 static struct bio *__bio_map_user_iov(
struct request_queue *q,
936 struct sg_iovec *iov,
int iov_count,
937 int write_to_vm,
gfp_t gfp_mask)
946 for (i = 0; i < iov_count; i++) {
947 unsigned long uaddr = (
unsigned long)iov[i].iov_base;
958 nr_pages += end -
start;
962 if (uaddr & queue_dma_alignment(q))
969 bio = bio_kmalloc(gfp_mask, nr_pages);
974 pages = kcalloc(nr_pages,
sizeof(
struct page *), gfp_mask);
978 for (i = 0; i < iov_count; i++) {
979 unsigned long uaddr = (
unsigned long)iov[i].iov_base;
981 unsigned long end = (uaddr + len +
PAGE_SIZE - 1) >> PAGE_SHIFT;
983 const int local_nr_pages = end -
start;
984 const int page_limit = cur_page + local_nr_pages;
987 write_to_vm, &pages[cur_page]);
988 if (ret < local_nr_pages) {
994 for (j = cur_page; j < page_limit; j++) {
1018 while (j < page_limit)
1030 bio->bi_bdev = bdev;
1031 bio->bi_flags |= (1 << BIO_USER_MAPPED);
1035 for (i = 0; i < nr_pages; i++) {
1043 return ERR_PTR(ret);
1059 unsigned long uaddr,
unsigned int len,
int write_to_vm,
1084 struct sg_iovec *iov,
int iov_count,
1085 int write_to_vm,
gfp_t gfp_mask)
1089 bio = __bio_map_user_iov(q, bdev, iov, iov_count, write_to_vm,
1105 static void __bio_unmap_user(
struct bio *bio)
1107 struct bio_vec *bvec;
1113 __bio_for_each_segment(bvec, bio, i, 0) {
1114 if (bio_data_dir(bio) ==
READ)
1134 __bio_unmap_user(bio);
1139 static void bio_map_kern_endio(
struct bio *bio,
int err)
1145 unsigned int len,
gfp_t gfp_mask)
1147 unsigned long kaddr = (
unsigned long)data;
1148 unsigned long end = (kaddr + len +
PAGE_SIZE - 1) >> PAGE_SHIFT;
1150 const int nr_pages = end -
start;
1154 bio = bio_kmalloc(gfp_mask, nr_pages);
1159 for (i = 0; i < nr_pages; i++) {
1177 bio->bi_end_io = bio_map_kern_endio;
1196 bio = __bio_map_kern(q, data, len, gfp_mask);
1200 if (bio->bi_size == len)
1211 static void bio_copy_kern_endio(
struct bio *bio,
int err)
1213 struct bio_vec *bvec;
1214 const int read = bio_data_dir(bio) ==
READ;
1217 char *p = bmd->
sgvecs[0].iov_base;
1219 __bio_for_each_segment(bvec, bio, i, 0) {
1221 int len = bmd->
iovecs[
i].bv_len;
1230 bio_free_map_data(bmd);
1249 struct bio_vec *bvec;
1259 bio_for_each_segment(bvec, bio, i) {
1262 memcpy(addr, p, bvec->bv_len);
1267 bio->bi_end_io = bio_copy_kern_endio;
1304 struct bio_vec *bvec = bio->bi_io_vec;
1307 for (i = 0; i < bio->bi_vcnt; i++) {
1308 struct page *page = bvec[
i].bv_page;
1310 if (page && !PageCompound(page))
1315 static void bio_release_pages(
struct bio *bio)
1317 struct bio_vec *bvec = bio->bi_io_vec;
1320 for (i = 0; i < bio->bi_vcnt; i++) {
1321 struct page *page = bvec[
i].bv_page;
1343 static struct bio *bio_dirty_list;
1350 unsigned long flags;
1354 bio = bio_dirty_list;
1355 bio_dirty_list =
NULL;
1356 spin_unlock_irqrestore(&bio_dirty_lock, flags);
1359 struct bio *
next = bio->bi_private;
1362 bio_release_pages(bio);
1370 struct bio_vec *bvec = bio->bi_io_vec;
1371 int nr_clean_pages = 0;
1374 for (i = 0; i < bio->bi_vcnt; i++) {
1375 struct page *page = bvec[
i].bv_page;
1377 if (PageDirty(page) || PageCompound(page)) {
1379 bvec[
i].bv_page =
NULL;
1385 if (nr_clean_pages) {
1386 unsigned long flags;
1389 bio->bi_private = bio_dirty_list;
1390 bio_dirty_list = bio;
1391 spin_unlock_irqrestore(&bio_dirty_lock, flags);
1398 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
1399 void bio_flush_dcache_pages(
struct bio *
bi)
1402 struct bio_vec *bvec;
1404 bio_for_each_segment(bvec, bi, i)
1427 clear_bit(BIO_UPTODATE, &bio->bi_flags);
1428 else if (!
test_bit(BIO_UPTODATE, &bio->bi_flags))
1432 bio->bi_end_io(bio, error);
1439 struct bio *master = bp->bio1.bi_private;
1447 static void bio_pair_end_1(
struct bio *bi,
int err)
1449 struct bio_pair *bp =
container_of(bi,
struct bio_pair, bio1);
1457 static void bio_pair_end_2(
struct bio *bi,
int err)
1459 struct bio_pair *bp =
container_of(bi,
struct bio_pair, bio2);
1470 struct bio_pair *
bio_split(
struct bio *bi,
int first_sectors)
1477 trace_block_split(bdev_get_queue(bi->bi_bdev), bi,
1478 bi->bi_sector + first_sectors);
1480 BUG_ON(bi->bi_vcnt != 1 && bi->bi_vcnt != 0);
1486 bp->bio2.bi_sector += first_sectors;
1487 bp->bio2.bi_size -= first_sectors << 9;
1488 bp->bio1.bi_size = first_sectors << 9;
1490 if (bi->bi_vcnt != 0) {
1491 bp->bv1 = bi->bi_io_vec[0];
1492 bp->bv2 = bi->bi_io_vec[0];
1494 if (bio_is_rw(bi)) {
1495 bp->bv2.bv_offset += first_sectors << 9;
1496 bp->bv2.bv_len -= first_sectors << 9;
1497 bp->bv1.bv_len = first_sectors << 9;
1500 bp->bio1.bi_io_vec = &bp->bv1;
1501 bp->bio2.bi_io_vec = &bp->bv2;
1503 bp->bio1.bi_max_vecs = 1;
1504 bp->bio2.bi_max_vecs = 1;
1507 bp->bio1.bi_end_io = bio_pair_end_1;
1508 bp->bio2.bi_end_io = bio_pair_end_2;
1510 bp->bio1.bi_private =
bi;
1511 bp->bio2.bi_private = bio_split_pool;
1513 if (bio_integrity(bi))
1531 unsigned int offset)
1533 unsigned int sector_sz;
1538 sector_sz = queue_logical_block_size(bio->bi_bdev->bd_disk->queue);
1541 if (index >= bio->bi_idx)
1542 index = bio->bi_vcnt - 1;
1544 __bio_for_each_segment(bv, bio, i, 0) {
1546 if (offset > bv->bv_offset)
1547 sectors += (offset - bv->bv_offset) / sector_sz;
1551 sectors += bv->bv_len / sector_sz;
1562 static int biovec_create_pools(
struct bio_set *bs,
int pool_entries)
1564 struct biovec_slab *bp = bvec_slabs + BIOVEC_MAX_IDX;
1566 bs->bvec_pool = mempool_create_slab_pool(pool_entries, bp->slab);
1573 static void biovec_free_pools(
struct bio_set *bs)
1584 biovec_free_pools(bs);
1613 bs->front_pad = front_pad;
1615 bs->bio_slab = bio_find_or_create_slab(front_pad + back_pad);
1616 if (!bs->bio_slab) {
1621 bs->bio_pool = mempool_create_slab_pool(pool_size, bs->bio_slab);
1625 if (!biovec_create_pools(bs, pool_size))
1634 #ifdef CONFIG_BLK_CGROUP
1648 int bio_associate_current(
struct bio *bio)
1651 struct cgroup_subsys_state *css;
1661 get_io_context_active(ioc);
1666 css = task_subsys_state(
current, blkio_subsys_id);
1667 if (css && css_tryget(css))
1678 void bio_disassociate_task(
struct bio *bio)
1685 css_put(bio->bi_css);
1692 static void __init biovec_init_slabs(
void)
1696 for (i = 0; i < BIOVEC_NR_POOLS; i++) {
1698 struct biovec_slab *bvs = bvec_slabs +
i;
1705 size = bvs->nr_vecs *
sizeof(
struct bio_vec);
1711 static int __init init_bio(
void)
1717 panic(
"bio: can't allocate bios\n");
1720 biovec_init_slabs();
1724 panic(
"bio: can't allocate bios\n");
1727 panic(
"bio: can't create integrity pool\n");
1729 bio_split_pool = mempool_create_kmalloc_pool(BIO_SPLIT_ENTRIES,
1730 sizeof(
struct bio_pair));
1731 if (!bio_split_pool)
1732 panic(
"bio: can't create split pool\n");