xref: /openbmc/linux/block/bio.c (revision d1916c86)
18c16567dSChristoph Hellwig // SPDX-License-Identifier: GPL-2.0
2f9c78b2bSJens Axboe /*
3f9c78b2bSJens Axboe  * Copyright (C) 2001 Jens Axboe <axboe@kernel.dk>
4f9c78b2bSJens Axboe  */
5f9c78b2bSJens Axboe #include <linux/mm.h>
6f9c78b2bSJens Axboe #include <linux/swap.h>
7f9c78b2bSJens Axboe #include <linux/bio.h>
8f9c78b2bSJens Axboe #include <linux/blkdev.h>
9f9c78b2bSJens Axboe #include <linux/uio.h>
10f9c78b2bSJens Axboe #include <linux/iocontext.h>
11f9c78b2bSJens Axboe #include <linux/slab.h>
12f9c78b2bSJens Axboe #include <linux/init.h>
13f9c78b2bSJens Axboe #include <linux/kernel.h>
14f9c78b2bSJens Axboe #include <linux/export.h>
15f9c78b2bSJens Axboe #include <linux/mempool.h>
16f9c78b2bSJens Axboe #include <linux/workqueue.h>
17f9c78b2bSJens Axboe #include <linux/cgroup.h>
1808e18eabSJosef Bacik #include <linux/blk-cgroup.h>
19b4c5875dSDamien Le Moal #include <linux/highmem.h>
20f9c78b2bSJens Axboe 
21f9c78b2bSJens Axboe #include <trace/events/block.h>
229e234eeaSShaohua Li #include "blk.h"
2367b42d0bSJosef Bacik #include "blk-rq-qos.h"
24f9c78b2bSJens Axboe 
25f9c78b2bSJens Axboe /*
26f9c78b2bSJens Axboe  * Test patch to inline a certain number of bi_io_vec's inside the bio
27f9c78b2bSJens Axboe  * itself, to shrink a bio data allocation from two mempool calls to one
28f9c78b2bSJens Axboe  */
29f9c78b2bSJens Axboe #define BIO_INLINE_VECS		4
30f9c78b2bSJens Axboe 
31f9c78b2bSJens Axboe /*
32f9c78b2bSJens Axboe  * if you change this list, also change bvec_alloc or things will
33f9c78b2bSJens Axboe  * break badly! cannot be bigger than what you can fit into an
34f9c78b2bSJens Axboe  * unsigned short
35f9c78b2bSJens Axboe  */
36bd5c4facSMikulas Patocka #define BV(x, n) { .nr_vecs = x, .name = "biovec-"#n }
37ed996a52SChristoph Hellwig static struct biovec_slab bvec_slabs[BVEC_POOL_NR] __read_mostly = {
38bd5c4facSMikulas Patocka 	BV(1, 1), BV(4, 4), BV(16, 16), BV(64, 64), BV(128, 128), BV(BIO_MAX_PAGES, max),
39f9c78b2bSJens Axboe };
40f9c78b2bSJens Axboe #undef BV
41f9c78b2bSJens Axboe 
42f9c78b2bSJens Axboe /*
43f9c78b2bSJens Axboe  * fs_bio_set is the bio_set containing bio and iovec memory pools used by
44f9c78b2bSJens Axboe  * IO code that does not need private memory pools.
45f9c78b2bSJens Axboe  */
46f4f8154aSKent Overstreet struct bio_set fs_bio_set;
47f9c78b2bSJens Axboe EXPORT_SYMBOL(fs_bio_set);
48f9c78b2bSJens Axboe 
49f9c78b2bSJens Axboe /*
50f9c78b2bSJens Axboe  * Our slab pool management
51f9c78b2bSJens Axboe  */
52f9c78b2bSJens Axboe struct bio_slab {
53f9c78b2bSJens Axboe 	struct kmem_cache *slab;
54f9c78b2bSJens Axboe 	unsigned int slab_ref;
55f9c78b2bSJens Axboe 	unsigned int slab_size;
56f9c78b2bSJens Axboe 	char name[8];
57f9c78b2bSJens Axboe };
58f9c78b2bSJens Axboe static DEFINE_MUTEX(bio_slab_lock);
59f9c78b2bSJens Axboe static struct bio_slab *bio_slabs;
60f9c78b2bSJens Axboe static unsigned int bio_slab_nr, bio_slab_max;
61f9c78b2bSJens Axboe 
62f9c78b2bSJens Axboe static struct kmem_cache *bio_find_or_create_slab(unsigned int extra_size)
63f9c78b2bSJens Axboe {
64f9c78b2bSJens Axboe 	unsigned int sz = sizeof(struct bio) + extra_size;
65f9c78b2bSJens Axboe 	struct kmem_cache *slab = NULL;
66f9c78b2bSJens Axboe 	struct bio_slab *bslab, *new_bio_slabs;
67f9c78b2bSJens Axboe 	unsigned int new_bio_slab_max;
68f9c78b2bSJens Axboe 	unsigned int i, entry = -1;
69f9c78b2bSJens Axboe 
70f9c78b2bSJens Axboe 	mutex_lock(&bio_slab_lock);
71f9c78b2bSJens Axboe 
72f9c78b2bSJens Axboe 	i = 0;
73f9c78b2bSJens Axboe 	while (i < bio_slab_nr) {
74f9c78b2bSJens Axboe 		bslab = &bio_slabs[i];
75f9c78b2bSJens Axboe 
76f9c78b2bSJens Axboe 		if (!bslab->slab && entry == -1)
77f9c78b2bSJens Axboe 			entry = i;
78f9c78b2bSJens Axboe 		else if (bslab->slab_size == sz) {
79f9c78b2bSJens Axboe 			slab = bslab->slab;
80f9c78b2bSJens Axboe 			bslab->slab_ref++;
81f9c78b2bSJens Axboe 			break;
82f9c78b2bSJens Axboe 		}
83f9c78b2bSJens Axboe 		i++;
84f9c78b2bSJens Axboe 	}
85f9c78b2bSJens Axboe 
86f9c78b2bSJens Axboe 	if (slab)
87f9c78b2bSJens Axboe 		goto out_unlock;
88f9c78b2bSJens Axboe 
89f9c78b2bSJens Axboe 	if (bio_slab_nr == bio_slab_max && entry == -1) {
90f9c78b2bSJens Axboe 		new_bio_slab_max = bio_slab_max << 1;
91f9c78b2bSJens Axboe 		new_bio_slabs = krealloc(bio_slabs,
92f9c78b2bSJens Axboe 					 new_bio_slab_max * sizeof(struct bio_slab),
93f9c78b2bSJens Axboe 					 GFP_KERNEL);
94f9c78b2bSJens Axboe 		if (!new_bio_slabs)
95f9c78b2bSJens Axboe 			goto out_unlock;
96f9c78b2bSJens Axboe 		bio_slab_max = new_bio_slab_max;
97f9c78b2bSJens Axboe 		bio_slabs = new_bio_slabs;
98f9c78b2bSJens Axboe 	}
99f9c78b2bSJens Axboe 	if (entry == -1)
100f9c78b2bSJens Axboe 		entry = bio_slab_nr++;
101f9c78b2bSJens Axboe 
102f9c78b2bSJens Axboe 	bslab = &bio_slabs[entry];
103f9c78b2bSJens Axboe 
104f9c78b2bSJens Axboe 	snprintf(bslab->name, sizeof(bslab->name), "bio-%d", entry);
1056a241483SMikulas Patocka 	slab = kmem_cache_create(bslab->name, sz, ARCH_KMALLOC_MINALIGN,
1066a241483SMikulas Patocka 				 SLAB_HWCACHE_ALIGN, NULL);
107f9c78b2bSJens Axboe 	if (!slab)
108f9c78b2bSJens Axboe 		goto out_unlock;
109f9c78b2bSJens Axboe 
110f9c78b2bSJens Axboe 	bslab->slab = slab;
111f9c78b2bSJens Axboe 	bslab->slab_ref = 1;
112f9c78b2bSJens Axboe 	bslab->slab_size = sz;
113f9c78b2bSJens Axboe out_unlock:
114f9c78b2bSJens Axboe 	mutex_unlock(&bio_slab_lock);
115f9c78b2bSJens Axboe 	return slab;
116f9c78b2bSJens Axboe }
117f9c78b2bSJens Axboe 
118f9c78b2bSJens Axboe static void bio_put_slab(struct bio_set *bs)
119f9c78b2bSJens Axboe {
120f9c78b2bSJens Axboe 	struct bio_slab *bslab = NULL;
121f9c78b2bSJens Axboe 	unsigned int i;
122f9c78b2bSJens Axboe 
123f9c78b2bSJens Axboe 	mutex_lock(&bio_slab_lock);
124f9c78b2bSJens Axboe 
125f9c78b2bSJens Axboe 	for (i = 0; i < bio_slab_nr; i++) {
126f9c78b2bSJens Axboe 		if (bs->bio_slab == bio_slabs[i].slab) {
127f9c78b2bSJens Axboe 			bslab = &bio_slabs[i];
128f9c78b2bSJens Axboe 			break;
129f9c78b2bSJens Axboe 		}
130f9c78b2bSJens Axboe 	}
131f9c78b2bSJens Axboe 
132f9c78b2bSJens Axboe 	if (WARN(!bslab, KERN_ERR "bio: unable to find slab!\n"))
133f9c78b2bSJens Axboe 		goto out;
134f9c78b2bSJens Axboe 
135f9c78b2bSJens Axboe 	WARN_ON(!bslab->slab_ref);
136f9c78b2bSJens Axboe 
137f9c78b2bSJens Axboe 	if (--bslab->slab_ref)
138f9c78b2bSJens Axboe 		goto out;
139f9c78b2bSJens Axboe 
140f9c78b2bSJens Axboe 	kmem_cache_destroy(bslab->slab);
141f9c78b2bSJens Axboe 	bslab->slab = NULL;
142f9c78b2bSJens Axboe 
143f9c78b2bSJens Axboe out:
144f9c78b2bSJens Axboe 	mutex_unlock(&bio_slab_lock);
145f9c78b2bSJens Axboe }
146f9c78b2bSJens Axboe 
147f9c78b2bSJens Axboe unsigned int bvec_nr_vecs(unsigned short idx)
148f9c78b2bSJens Axboe {
149d6c02a9bSGreg Edwards 	return bvec_slabs[--idx].nr_vecs;
150f9c78b2bSJens Axboe }
151f9c78b2bSJens Axboe 
152f9c78b2bSJens Axboe void bvec_free(mempool_t *pool, struct bio_vec *bv, unsigned int idx)
153f9c78b2bSJens Axboe {
154ed996a52SChristoph Hellwig 	if (!idx)
155ed996a52SChristoph Hellwig 		return;
156ed996a52SChristoph Hellwig 	idx--;
157f9c78b2bSJens Axboe 
158ed996a52SChristoph Hellwig 	BIO_BUG_ON(idx >= BVEC_POOL_NR);
159ed996a52SChristoph Hellwig 
160ed996a52SChristoph Hellwig 	if (idx == BVEC_POOL_MAX) {
161f9c78b2bSJens Axboe 		mempool_free(bv, pool);
162ed996a52SChristoph Hellwig 	} else {
163f9c78b2bSJens Axboe 		struct biovec_slab *bvs = bvec_slabs + idx;
164f9c78b2bSJens Axboe 
165f9c78b2bSJens Axboe 		kmem_cache_free(bvs->slab, bv);
166f9c78b2bSJens Axboe 	}
167f9c78b2bSJens Axboe }
168f9c78b2bSJens Axboe 
169f9c78b2bSJens Axboe struct bio_vec *bvec_alloc(gfp_t gfp_mask, int nr, unsigned long *idx,
170f9c78b2bSJens Axboe 			   mempool_t *pool)
171f9c78b2bSJens Axboe {
172f9c78b2bSJens Axboe 	struct bio_vec *bvl;
173f9c78b2bSJens Axboe 
174f9c78b2bSJens Axboe 	/*
175f9c78b2bSJens Axboe 	 * see comment near bvec_array define!
176f9c78b2bSJens Axboe 	 */
177f9c78b2bSJens Axboe 	switch (nr) {
178f9c78b2bSJens Axboe 	case 1:
179f9c78b2bSJens Axboe 		*idx = 0;
180f9c78b2bSJens Axboe 		break;
181f9c78b2bSJens Axboe 	case 2 ... 4:
182f9c78b2bSJens Axboe 		*idx = 1;
183f9c78b2bSJens Axboe 		break;
184f9c78b2bSJens Axboe 	case 5 ... 16:
185f9c78b2bSJens Axboe 		*idx = 2;
186f9c78b2bSJens Axboe 		break;
187f9c78b2bSJens Axboe 	case 17 ... 64:
188f9c78b2bSJens Axboe 		*idx = 3;
189f9c78b2bSJens Axboe 		break;
190f9c78b2bSJens Axboe 	case 65 ... 128:
191f9c78b2bSJens Axboe 		*idx = 4;
192f9c78b2bSJens Axboe 		break;
193f9c78b2bSJens Axboe 	case 129 ... BIO_MAX_PAGES:
194f9c78b2bSJens Axboe 		*idx = 5;
195f9c78b2bSJens Axboe 		break;
196f9c78b2bSJens Axboe 	default:
197f9c78b2bSJens Axboe 		return NULL;
198f9c78b2bSJens Axboe 	}
199f9c78b2bSJens Axboe 
200f9c78b2bSJens Axboe 	/*
201f9c78b2bSJens Axboe 	 * idx now points to the pool we want to allocate from. only the
202f9c78b2bSJens Axboe 	 * 1-vec entry pool is mempool backed.
203f9c78b2bSJens Axboe 	 */
204ed996a52SChristoph Hellwig 	if (*idx == BVEC_POOL_MAX) {
205f9c78b2bSJens Axboe fallback:
206f9c78b2bSJens Axboe 		bvl = mempool_alloc(pool, gfp_mask);
207f9c78b2bSJens Axboe 	} else {
208f9c78b2bSJens Axboe 		struct biovec_slab *bvs = bvec_slabs + *idx;
209d0164adcSMel Gorman 		gfp_t __gfp_mask = gfp_mask & ~(__GFP_DIRECT_RECLAIM | __GFP_IO);
210f9c78b2bSJens Axboe 
211f9c78b2bSJens Axboe 		/*
212f9c78b2bSJens Axboe 		 * Make this allocation restricted and don't dump info on
213f9c78b2bSJens Axboe 		 * allocation failures, since we'll fallback to the mempool
214f9c78b2bSJens Axboe 		 * in case of failure.
215f9c78b2bSJens Axboe 		 */
216f9c78b2bSJens Axboe 		__gfp_mask |= __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
217f9c78b2bSJens Axboe 
218f9c78b2bSJens Axboe 		/*
219d0164adcSMel Gorman 		 * Try a slab allocation. If this fails and __GFP_DIRECT_RECLAIM
220f9c78b2bSJens Axboe 		 * is set, retry with the 1-entry mempool
221f9c78b2bSJens Axboe 		 */
222f9c78b2bSJens Axboe 		bvl = kmem_cache_alloc(bvs->slab, __gfp_mask);
223d0164adcSMel Gorman 		if (unlikely(!bvl && (gfp_mask & __GFP_DIRECT_RECLAIM))) {
224ed996a52SChristoph Hellwig 			*idx = BVEC_POOL_MAX;
225f9c78b2bSJens Axboe 			goto fallback;
226f9c78b2bSJens Axboe 		}
227f9c78b2bSJens Axboe 	}
228f9c78b2bSJens Axboe 
229ed996a52SChristoph Hellwig 	(*idx)++;
230f9c78b2bSJens Axboe 	return bvl;
231f9c78b2bSJens Axboe }
232f9c78b2bSJens Axboe 
2339ae3b3f5SJens Axboe void bio_uninit(struct bio *bio)
234f9c78b2bSJens Axboe {
2356f70fb66SDennis Zhou 	bio_disassociate_blkg(bio);
236f9c78b2bSJens Axboe }
2379ae3b3f5SJens Axboe EXPORT_SYMBOL(bio_uninit);
238f9c78b2bSJens Axboe 
239f9c78b2bSJens Axboe static void bio_free(struct bio *bio)
240f9c78b2bSJens Axboe {
241f9c78b2bSJens Axboe 	struct bio_set *bs = bio->bi_pool;
242f9c78b2bSJens Axboe 	void *p;
243f9c78b2bSJens Axboe 
2449ae3b3f5SJens Axboe 	bio_uninit(bio);
245f9c78b2bSJens Axboe 
246f9c78b2bSJens Axboe 	if (bs) {
2478aa6ba2fSKent Overstreet 		bvec_free(&bs->bvec_pool, bio->bi_io_vec, BVEC_POOL_IDX(bio));
248f9c78b2bSJens Axboe 
249f9c78b2bSJens Axboe 		/*
250f9c78b2bSJens Axboe 		 * If we have front padding, adjust the bio pointer before freeing
251f9c78b2bSJens Axboe 		 */
252f9c78b2bSJens Axboe 		p = bio;
253f9c78b2bSJens Axboe 		p -= bs->front_pad;
254f9c78b2bSJens Axboe 
2558aa6ba2fSKent Overstreet 		mempool_free(p, &bs->bio_pool);
256f9c78b2bSJens Axboe 	} else {
257f9c78b2bSJens Axboe 		/* Bio was allocated by bio_kmalloc() */
258f9c78b2bSJens Axboe 		kfree(bio);
259f9c78b2bSJens Axboe 	}
260f9c78b2bSJens Axboe }
261f9c78b2bSJens Axboe 
2629ae3b3f5SJens Axboe /*
2639ae3b3f5SJens Axboe  * Users of this function have their own bio allocation. Subsequently,
2649ae3b3f5SJens Axboe  * they must remember to pair any call to bio_init() with bio_uninit()
2659ae3b3f5SJens Axboe  * when IO has completed, or when the bio is released.
2669ae3b3f5SJens Axboe  */
2673a83f467SMing Lei void bio_init(struct bio *bio, struct bio_vec *table,
2683a83f467SMing Lei 	      unsigned short max_vecs)
269f9c78b2bSJens Axboe {
270f9c78b2bSJens Axboe 	memset(bio, 0, sizeof(*bio));
271c4cf5261SJens Axboe 	atomic_set(&bio->__bi_remaining, 1);
272dac56212SJens Axboe 	atomic_set(&bio->__bi_cnt, 1);
2733a83f467SMing Lei 
2743a83f467SMing Lei 	bio->bi_io_vec = table;
2753a83f467SMing Lei 	bio->bi_max_vecs = max_vecs;
276f9c78b2bSJens Axboe }
277f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_init);
278f9c78b2bSJens Axboe 
279f9c78b2bSJens Axboe /**
280f9c78b2bSJens Axboe  * bio_reset - reinitialize a bio
281f9c78b2bSJens Axboe  * @bio:	bio to reset
282f9c78b2bSJens Axboe  *
283f9c78b2bSJens Axboe  * Description:
284f9c78b2bSJens Axboe  *   After calling bio_reset(), @bio will be in the same state as a freshly
285f9c78b2bSJens Axboe  *   allocated bio returned bio bio_alloc_bioset() - the only fields that are
286f9c78b2bSJens Axboe  *   preserved are the ones that are initialized by bio_alloc_bioset(). See
287f9c78b2bSJens Axboe  *   comment in struct bio.
288f9c78b2bSJens Axboe  */
289f9c78b2bSJens Axboe void bio_reset(struct bio *bio)
290f9c78b2bSJens Axboe {
291f9c78b2bSJens Axboe 	unsigned long flags = bio->bi_flags & (~0UL << BIO_RESET_BITS);
292f9c78b2bSJens Axboe 
2939ae3b3f5SJens Axboe 	bio_uninit(bio);
294f9c78b2bSJens Axboe 
295f9c78b2bSJens Axboe 	memset(bio, 0, BIO_RESET_BYTES);
2964246a0b6SChristoph Hellwig 	bio->bi_flags = flags;
297c4cf5261SJens Axboe 	atomic_set(&bio->__bi_remaining, 1);
298f9c78b2bSJens Axboe }
299f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_reset);
300f9c78b2bSJens Axboe 
30138f8baaeSChristoph Hellwig static struct bio *__bio_chain_endio(struct bio *bio)
302f9c78b2bSJens Axboe {
3034246a0b6SChristoph Hellwig 	struct bio *parent = bio->bi_private;
3044246a0b6SChristoph Hellwig 
3054e4cbee9SChristoph Hellwig 	if (!parent->bi_status)
3064e4cbee9SChristoph Hellwig 		parent->bi_status = bio->bi_status;
307f9c78b2bSJens Axboe 	bio_put(bio);
30838f8baaeSChristoph Hellwig 	return parent;
30938f8baaeSChristoph Hellwig }
31038f8baaeSChristoph Hellwig 
31138f8baaeSChristoph Hellwig static void bio_chain_endio(struct bio *bio)
31238f8baaeSChristoph Hellwig {
31338f8baaeSChristoph Hellwig 	bio_endio(__bio_chain_endio(bio));
314f9c78b2bSJens Axboe }
315f9c78b2bSJens Axboe 
316f9c78b2bSJens Axboe /**
317f9c78b2bSJens Axboe  * bio_chain - chain bio completions
318f9c78b2bSJens Axboe  * @bio: the target bio
319f9c78b2bSJens Axboe  * @parent: the @bio's parent bio
320f9c78b2bSJens Axboe  *
321f9c78b2bSJens Axboe  * The caller won't have a bi_end_io called when @bio completes - instead,
322f9c78b2bSJens Axboe  * @parent's bi_end_io won't be called until both @parent and @bio have
323f9c78b2bSJens Axboe  * completed; the chained bio will also be freed when it completes.
324f9c78b2bSJens Axboe  *
325f9c78b2bSJens Axboe  * The caller must not set bi_private or bi_end_io in @bio.
326f9c78b2bSJens Axboe  */
327f9c78b2bSJens Axboe void bio_chain(struct bio *bio, struct bio *parent)
328f9c78b2bSJens Axboe {
329f9c78b2bSJens Axboe 	BUG_ON(bio->bi_private || bio->bi_end_io);
330f9c78b2bSJens Axboe 
331f9c78b2bSJens Axboe 	bio->bi_private = parent;
332f9c78b2bSJens Axboe 	bio->bi_end_io	= bio_chain_endio;
333c4cf5261SJens Axboe 	bio_inc_remaining(parent);
334f9c78b2bSJens Axboe }
335f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_chain);
336f9c78b2bSJens Axboe 
337f9c78b2bSJens Axboe static void bio_alloc_rescue(struct work_struct *work)
338f9c78b2bSJens Axboe {
339f9c78b2bSJens Axboe 	struct bio_set *bs = container_of(work, struct bio_set, rescue_work);
340f9c78b2bSJens Axboe 	struct bio *bio;
341f9c78b2bSJens Axboe 
342f9c78b2bSJens Axboe 	while (1) {
343f9c78b2bSJens Axboe 		spin_lock(&bs->rescue_lock);
344f9c78b2bSJens Axboe 		bio = bio_list_pop(&bs->rescue_list);
345f9c78b2bSJens Axboe 		spin_unlock(&bs->rescue_lock);
346f9c78b2bSJens Axboe 
347f9c78b2bSJens Axboe 		if (!bio)
348f9c78b2bSJens Axboe 			break;
349f9c78b2bSJens Axboe 
350f9c78b2bSJens Axboe 		generic_make_request(bio);
351f9c78b2bSJens Axboe 	}
352f9c78b2bSJens Axboe }
353f9c78b2bSJens Axboe 
354f9c78b2bSJens Axboe static void punt_bios_to_rescuer(struct bio_set *bs)
355f9c78b2bSJens Axboe {
356f9c78b2bSJens Axboe 	struct bio_list punt, nopunt;
357f9c78b2bSJens Axboe 	struct bio *bio;
358f9c78b2bSJens Axboe 
35947e0fb46SNeilBrown 	if (WARN_ON_ONCE(!bs->rescue_workqueue))
36047e0fb46SNeilBrown 		return;
361f9c78b2bSJens Axboe 	/*
362f9c78b2bSJens Axboe 	 * In order to guarantee forward progress we must punt only bios that
363f9c78b2bSJens Axboe 	 * were allocated from this bio_set; otherwise, if there was a bio on
364f9c78b2bSJens Axboe 	 * there for a stacking driver higher up in the stack, processing it
365f9c78b2bSJens Axboe 	 * could require allocating bios from this bio_set, and doing that from
366f9c78b2bSJens Axboe 	 * our own rescuer would be bad.
367f9c78b2bSJens Axboe 	 *
368f9c78b2bSJens Axboe 	 * Since bio lists are singly linked, pop them all instead of trying to
369f9c78b2bSJens Axboe 	 * remove from the middle of the list:
370f9c78b2bSJens Axboe 	 */
371f9c78b2bSJens Axboe 
372f9c78b2bSJens Axboe 	bio_list_init(&punt);
373f9c78b2bSJens Axboe 	bio_list_init(&nopunt);
374f9c78b2bSJens Axboe 
375f5fe1b51SNeilBrown 	while ((bio = bio_list_pop(&current->bio_list[0])))
376f9c78b2bSJens Axboe 		bio_list_add(bio->bi_pool == bs ? &punt : &nopunt, bio);
377f5fe1b51SNeilBrown 	current->bio_list[0] = nopunt;
378f9c78b2bSJens Axboe 
379f5fe1b51SNeilBrown 	bio_list_init(&nopunt);
380f5fe1b51SNeilBrown 	while ((bio = bio_list_pop(&current->bio_list[1])))
381f5fe1b51SNeilBrown 		bio_list_add(bio->bi_pool == bs ? &punt : &nopunt, bio);
382f5fe1b51SNeilBrown 	current->bio_list[1] = nopunt;
383f9c78b2bSJens Axboe 
384f9c78b2bSJens Axboe 	spin_lock(&bs->rescue_lock);
385f9c78b2bSJens Axboe 	bio_list_merge(&bs->rescue_list, &punt);
386f9c78b2bSJens Axboe 	spin_unlock(&bs->rescue_lock);
387f9c78b2bSJens Axboe 
388f9c78b2bSJens Axboe 	queue_work(bs->rescue_workqueue, &bs->rescue_work);
389f9c78b2bSJens Axboe }
390f9c78b2bSJens Axboe 
391f9c78b2bSJens Axboe /**
392f9c78b2bSJens Axboe  * bio_alloc_bioset - allocate a bio for I/O
393519c8e9fSRandy Dunlap  * @gfp_mask:   the GFP_* mask given to the slab allocator
394f9c78b2bSJens Axboe  * @nr_iovecs:	number of iovecs to pre-allocate
395f9c78b2bSJens Axboe  * @bs:		the bio_set to allocate from.
396f9c78b2bSJens Axboe  *
397f9c78b2bSJens Axboe  * Description:
398f9c78b2bSJens Axboe  *   If @bs is NULL, uses kmalloc() to allocate the bio; else the allocation is
399f9c78b2bSJens Axboe  *   backed by the @bs's mempool.
400f9c78b2bSJens Axboe  *
401d0164adcSMel Gorman  *   When @bs is not NULL, if %__GFP_DIRECT_RECLAIM is set then bio_alloc will
402d0164adcSMel Gorman  *   always be able to allocate a bio. This is due to the mempool guarantees.
403d0164adcSMel Gorman  *   To make this work, callers must never allocate more than 1 bio at a time
404d0164adcSMel Gorman  *   from this pool. Callers that need to allocate more than 1 bio must always
405d0164adcSMel Gorman  *   submit the previously allocated bio for IO before attempting to allocate
406d0164adcSMel Gorman  *   a new one. Failure to do so can cause deadlocks under memory pressure.
407f9c78b2bSJens Axboe  *
408f9c78b2bSJens Axboe  *   Note that when running under generic_make_request() (i.e. any block
409f9c78b2bSJens Axboe  *   driver), bios are not submitted until after you return - see the code in
410f9c78b2bSJens Axboe  *   generic_make_request() that converts recursion into iteration, to prevent
411f9c78b2bSJens Axboe  *   stack overflows.
412f9c78b2bSJens Axboe  *
413f9c78b2bSJens Axboe  *   This would normally mean allocating multiple bios under
414f9c78b2bSJens Axboe  *   generic_make_request() would be susceptible to deadlocks, but we have
415f9c78b2bSJens Axboe  *   deadlock avoidance code that resubmits any blocked bios from a rescuer
416f9c78b2bSJens Axboe  *   thread.
417f9c78b2bSJens Axboe  *
418f9c78b2bSJens Axboe  *   However, we do not guarantee forward progress for allocations from other
419f9c78b2bSJens Axboe  *   mempools. Doing multiple allocations from the same mempool under
420f9c78b2bSJens Axboe  *   generic_make_request() should be avoided - instead, use bio_set's front_pad
421f9c78b2bSJens Axboe  *   for per bio allocations.
422f9c78b2bSJens Axboe  *
423f9c78b2bSJens Axboe  *   RETURNS:
424f9c78b2bSJens Axboe  *   Pointer to new bio on success, NULL on failure.
425f9c78b2bSJens Axboe  */
4267a88fa19SDan Carpenter struct bio *bio_alloc_bioset(gfp_t gfp_mask, unsigned int nr_iovecs,
4277a88fa19SDan Carpenter 			     struct bio_set *bs)
428f9c78b2bSJens Axboe {
429f9c78b2bSJens Axboe 	gfp_t saved_gfp = gfp_mask;
430f9c78b2bSJens Axboe 	unsigned front_pad;
431f9c78b2bSJens Axboe 	unsigned inline_vecs;
432f9c78b2bSJens Axboe 	struct bio_vec *bvl = NULL;
433f9c78b2bSJens Axboe 	struct bio *bio;
434f9c78b2bSJens Axboe 	void *p;
435f9c78b2bSJens Axboe 
436f9c78b2bSJens Axboe 	if (!bs) {
437f9c78b2bSJens Axboe 		if (nr_iovecs > UIO_MAXIOV)
438f9c78b2bSJens Axboe 			return NULL;
439f9c78b2bSJens Axboe 
440f9c78b2bSJens Axboe 		p = kmalloc(sizeof(struct bio) +
441f9c78b2bSJens Axboe 			    nr_iovecs * sizeof(struct bio_vec),
442f9c78b2bSJens Axboe 			    gfp_mask);
443f9c78b2bSJens Axboe 		front_pad = 0;
444f9c78b2bSJens Axboe 		inline_vecs = nr_iovecs;
445f9c78b2bSJens Axboe 	} else {
446d8f429e1SJunichi Nomura 		/* should not use nobvec bioset for nr_iovecs > 0 */
4478aa6ba2fSKent Overstreet 		if (WARN_ON_ONCE(!mempool_initialized(&bs->bvec_pool) &&
4488aa6ba2fSKent Overstreet 				 nr_iovecs > 0))
449d8f429e1SJunichi Nomura 			return NULL;
450f9c78b2bSJens Axboe 		/*
451f9c78b2bSJens Axboe 		 * generic_make_request() converts recursion to iteration; this
452f9c78b2bSJens Axboe 		 * means if we're running beneath it, any bios we allocate and
453f9c78b2bSJens Axboe 		 * submit will not be submitted (and thus freed) until after we
454f9c78b2bSJens Axboe 		 * return.
455f9c78b2bSJens Axboe 		 *
456f9c78b2bSJens Axboe 		 * This exposes us to a potential deadlock if we allocate
457f9c78b2bSJens Axboe 		 * multiple bios from the same bio_set() while running
458f9c78b2bSJens Axboe 		 * underneath generic_make_request(). If we were to allocate
459f9c78b2bSJens Axboe 		 * multiple bios (say a stacking block driver that was splitting
460f9c78b2bSJens Axboe 		 * bios), we would deadlock if we exhausted the mempool's
461f9c78b2bSJens Axboe 		 * reserve.
462f9c78b2bSJens Axboe 		 *
463f9c78b2bSJens Axboe 		 * We solve this, and guarantee forward progress, with a rescuer
464f9c78b2bSJens Axboe 		 * workqueue per bio_set. If we go to allocate and there are
465f9c78b2bSJens Axboe 		 * bios on current->bio_list, we first try the allocation
466d0164adcSMel Gorman 		 * without __GFP_DIRECT_RECLAIM; if that fails, we punt those
467d0164adcSMel Gorman 		 * bios we would be blocking to the rescuer workqueue before
468d0164adcSMel Gorman 		 * we retry with the original gfp_flags.
469f9c78b2bSJens Axboe 		 */
470f9c78b2bSJens Axboe 
471f5fe1b51SNeilBrown 		if (current->bio_list &&
472f5fe1b51SNeilBrown 		    (!bio_list_empty(&current->bio_list[0]) ||
47347e0fb46SNeilBrown 		     !bio_list_empty(&current->bio_list[1])) &&
47447e0fb46SNeilBrown 		    bs->rescue_workqueue)
475d0164adcSMel Gorman 			gfp_mask &= ~__GFP_DIRECT_RECLAIM;
476f9c78b2bSJens Axboe 
4778aa6ba2fSKent Overstreet 		p = mempool_alloc(&bs->bio_pool, gfp_mask);
478f9c78b2bSJens Axboe 		if (!p && gfp_mask != saved_gfp) {
479f9c78b2bSJens Axboe 			punt_bios_to_rescuer(bs);
480f9c78b2bSJens Axboe 			gfp_mask = saved_gfp;
4818aa6ba2fSKent Overstreet 			p = mempool_alloc(&bs->bio_pool, gfp_mask);
482f9c78b2bSJens Axboe 		}
483f9c78b2bSJens Axboe 
484f9c78b2bSJens Axboe 		front_pad = bs->front_pad;
485f9c78b2bSJens Axboe 		inline_vecs = BIO_INLINE_VECS;
486f9c78b2bSJens Axboe 	}
487f9c78b2bSJens Axboe 
488f9c78b2bSJens Axboe 	if (unlikely(!p))
489f9c78b2bSJens Axboe 		return NULL;
490f9c78b2bSJens Axboe 
491f9c78b2bSJens Axboe 	bio = p + front_pad;
4923a83f467SMing Lei 	bio_init(bio, NULL, 0);
493f9c78b2bSJens Axboe 
494f9c78b2bSJens Axboe 	if (nr_iovecs > inline_vecs) {
495ed996a52SChristoph Hellwig 		unsigned long idx = 0;
496ed996a52SChristoph Hellwig 
4978aa6ba2fSKent Overstreet 		bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx, &bs->bvec_pool);
498f9c78b2bSJens Axboe 		if (!bvl && gfp_mask != saved_gfp) {
499f9c78b2bSJens Axboe 			punt_bios_to_rescuer(bs);
500f9c78b2bSJens Axboe 			gfp_mask = saved_gfp;
5018aa6ba2fSKent Overstreet 			bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx, &bs->bvec_pool);
502f9c78b2bSJens Axboe 		}
503f9c78b2bSJens Axboe 
504f9c78b2bSJens Axboe 		if (unlikely(!bvl))
505f9c78b2bSJens Axboe 			goto err_free;
506f9c78b2bSJens Axboe 
507ed996a52SChristoph Hellwig 		bio->bi_flags |= idx << BVEC_POOL_OFFSET;
508f9c78b2bSJens Axboe 	} else if (nr_iovecs) {
509f9c78b2bSJens Axboe 		bvl = bio->bi_inline_vecs;
510f9c78b2bSJens Axboe 	}
511f9c78b2bSJens Axboe 
512f9c78b2bSJens Axboe 	bio->bi_pool = bs;
513f9c78b2bSJens Axboe 	bio->bi_max_vecs = nr_iovecs;
514f9c78b2bSJens Axboe 	bio->bi_io_vec = bvl;
515f9c78b2bSJens Axboe 	return bio;
516f9c78b2bSJens Axboe 
517f9c78b2bSJens Axboe err_free:
5188aa6ba2fSKent Overstreet 	mempool_free(p, &bs->bio_pool);
519f9c78b2bSJens Axboe 	return NULL;
520f9c78b2bSJens Axboe }
521f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_alloc_bioset);
522f9c78b2bSJens Axboe 
52338a72dacSKent Overstreet void zero_fill_bio_iter(struct bio *bio, struct bvec_iter start)
524f9c78b2bSJens Axboe {
525f9c78b2bSJens Axboe 	unsigned long flags;
526f9c78b2bSJens Axboe 	struct bio_vec bv;
527f9c78b2bSJens Axboe 	struct bvec_iter iter;
528f9c78b2bSJens Axboe 
52938a72dacSKent Overstreet 	__bio_for_each_segment(bv, bio, iter, start) {
530f9c78b2bSJens Axboe 		char *data = bvec_kmap_irq(&bv, &flags);
531f9c78b2bSJens Axboe 		memset(data, 0, bv.bv_len);
532f9c78b2bSJens Axboe 		flush_dcache_page(bv.bv_page);
533f9c78b2bSJens Axboe 		bvec_kunmap_irq(data, &flags);
534f9c78b2bSJens Axboe 	}
535f9c78b2bSJens Axboe }
53638a72dacSKent Overstreet EXPORT_SYMBOL(zero_fill_bio_iter);
537f9c78b2bSJens Axboe 
538f9c78b2bSJens Axboe /**
539f9c78b2bSJens Axboe  * bio_put - release a reference to a bio
540f9c78b2bSJens Axboe  * @bio:   bio to release reference to
541f9c78b2bSJens Axboe  *
542f9c78b2bSJens Axboe  * Description:
543f9c78b2bSJens Axboe  *   Put a reference to a &struct bio, either one you have gotten with
5449b10f6a9SNeilBrown  *   bio_alloc, bio_get or bio_clone_*. The last put of a bio will free it.
545f9c78b2bSJens Axboe  **/
546f9c78b2bSJens Axboe void bio_put(struct bio *bio)
547f9c78b2bSJens Axboe {
548dac56212SJens Axboe 	if (!bio_flagged(bio, BIO_REFFED))
549dac56212SJens Axboe 		bio_free(bio);
550dac56212SJens Axboe 	else {
551dac56212SJens Axboe 		BIO_BUG_ON(!atomic_read(&bio->__bi_cnt));
552f9c78b2bSJens Axboe 
553f9c78b2bSJens Axboe 		/*
554f9c78b2bSJens Axboe 		 * last put frees it
555f9c78b2bSJens Axboe 		 */
556dac56212SJens Axboe 		if (atomic_dec_and_test(&bio->__bi_cnt))
557f9c78b2bSJens Axboe 			bio_free(bio);
558f9c78b2bSJens Axboe 	}
559dac56212SJens Axboe }
560f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_put);
561f9c78b2bSJens Axboe 
562f9c78b2bSJens Axboe /**
563f9c78b2bSJens Axboe  * 	__bio_clone_fast - clone a bio that shares the original bio's biovec
564f9c78b2bSJens Axboe  * 	@bio: destination bio
565f9c78b2bSJens Axboe  * 	@bio_src: bio to clone
566f9c78b2bSJens Axboe  *
567f9c78b2bSJens Axboe  *	Clone a &bio. Caller will own the returned bio, but not
568f9c78b2bSJens Axboe  *	the actual data it points to. Reference count of returned
569f9c78b2bSJens Axboe  * 	bio will be one.
570f9c78b2bSJens Axboe  *
571f9c78b2bSJens Axboe  * 	Caller must ensure that @bio_src is not freed before @bio.
572f9c78b2bSJens Axboe  */
573f9c78b2bSJens Axboe void __bio_clone_fast(struct bio *bio, struct bio *bio_src)
574f9c78b2bSJens Axboe {
575ed996a52SChristoph Hellwig 	BUG_ON(bio->bi_pool && BVEC_POOL_IDX(bio));
576f9c78b2bSJens Axboe 
577f9c78b2bSJens Axboe 	/*
57874d46992SChristoph Hellwig 	 * most users will be overriding ->bi_disk with a new target,
579f9c78b2bSJens Axboe 	 * so we don't set nor calculate new physical/hw segment counts here
580f9c78b2bSJens Axboe 	 */
58174d46992SChristoph Hellwig 	bio->bi_disk = bio_src->bi_disk;
58262530ed8SMichael Lyle 	bio->bi_partno = bio_src->bi_partno;
583b7c44ed9SJens Axboe 	bio_set_flag(bio, BIO_CLONED);
584111be883SShaohua Li 	if (bio_flagged(bio_src, BIO_THROTTLED))
585111be883SShaohua Li 		bio_set_flag(bio, BIO_THROTTLED);
5861eff9d32SJens Axboe 	bio->bi_opf = bio_src->bi_opf;
587ca474b73SHannes Reinecke 	bio->bi_ioprio = bio_src->bi_ioprio;
588cb6934f8SJens Axboe 	bio->bi_write_hint = bio_src->bi_write_hint;
589f9c78b2bSJens Axboe 	bio->bi_iter = bio_src->bi_iter;
590f9c78b2bSJens Axboe 	bio->bi_io_vec = bio_src->bi_io_vec;
59120bd723eSPaolo Valente 
592db6638d7SDennis Zhou 	bio_clone_blkg_association(bio, bio_src);
593e439bedfSDennis Zhou 	blkcg_bio_issue_init(bio);
594f9c78b2bSJens Axboe }
595f9c78b2bSJens Axboe EXPORT_SYMBOL(__bio_clone_fast);
596f9c78b2bSJens Axboe 
597f9c78b2bSJens Axboe /**
598f9c78b2bSJens Axboe  *	bio_clone_fast - clone a bio that shares the original bio's biovec
599f9c78b2bSJens Axboe  *	@bio: bio to clone
600f9c78b2bSJens Axboe  *	@gfp_mask: allocation priority
601f9c78b2bSJens Axboe  *	@bs: bio_set to allocate from
602f9c78b2bSJens Axboe  *
603f9c78b2bSJens Axboe  * 	Like __bio_clone_fast, only also allocates the returned bio
604f9c78b2bSJens Axboe  */
605f9c78b2bSJens Axboe struct bio *bio_clone_fast(struct bio *bio, gfp_t gfp_mask, struct bio_set *bs)
606f9c78b2bSJens Axboe {
607f9c78b2bSJens Axboe 	struct bio *b;
608f9c78b2bSJens Axboe 
609f9c78b2bSJens Axboe 	b = bio_alloc_bioset(gfp_mask, 0, bs);
610f9c78b2bSJens Axboe 	if (!b)
611f9c78b2bSJens Axboe 		return NULL;
612f9c78b2bSJens Axboe 
613f9c78b2bSJens Axboe 	__bio_clone_fast(b, bio);
614f9c78b2bSJens Axboe 
615f9c78b2bSJens Axboe 	if (bio_integrity(bio)) {
616f9c78b2bSJens Axboe 		int ret;
617f9c78b2bSJens Axboe 
618f9c78b2bSJens Axboe 		ret = bio_integrity_clone(b, bio, gfp_mask);
619f9c78b2bSJens Axboe 
620f9c78b2bSJens Axboe 		if (ret < 0) {
621f9c78b2bSJens Axboe 			bio_put(b);
622f9c78b2bSJens Axboe 			return NULL;
623f9c78b2bSJens Axboe 		}
624f9c78b2bSJens Axboe 	}
625f9c78b2bSJens Axboe 
626f9c78b2bSJens Axboe 	return b;
627f9c78b2bSJens Axboe }
628f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_clone_fast);
629f9c78b2bSJens Axboe 
6305919482eSMing Lei static inline bool page_is_mergeable(const struct bio_vec *bv,
6315919482eSMing Lei 		struct page *page, unsigned int len, unsigned int off,
632ff896738SChristoph Hellwig 		bool *same_page)
6335919482eSMing Lei {
6345919482eSMing Lei 	phys_addr_t vec_end_addr = page_to_phys(bv->bv_page) +
6355919482eSMing Lei 		bv->bv_offset + bv->bv_len - 1;
6365919482eSMing Lei 	phys_addr_t page_addr = page_to_phys(page);
6375919482eSMing Lei 
6385919482eSMing Lei 	if (vec_end_addr + 1 != page_addr + off)
6395919482eSMing Lei 		return false;
6405919482eSMing Lei 	if (xen_domain() && !xen_biovec_phys_mergeable(bv, page))
6415919482eSMing Lei 		return false;
64252d52d1cSChristoph Hellwig 
643ff896738SChristoph Hellwig 	*same_page = ((vec_end_addr & PAGE_MASK) == page_addr);
644ff896738SChristoph Hellwig 	if (!*same_page && pfn_to_page(PFN_DOWN(vec_end_addr)) + 1 != page)
6455919482eSMing Lei 		return false;
6465919482eSMing Lei 	return true;
6475919482eSMing Lei }
6485919482eSMing Lei 
649384209cdSChristoph Hellwig static bool bio_try_merge_pc_page(struct request_queue *q, struct bio *bio,
650384209cdSChristoph Hellwig 		struct page *page, unsigned len, unsigned offset,
651384209cdSChristoph Hellwig 		bool *same_page)
652489fbbcbSMing Lei {
653384209cdSChristoph Hellwig 	struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt - 1];
654489fbbcbSMing Lei 	unsigned long mask = queue_segment_boundary(q);
655489fbbcbSMing Lei 	phys_addr_t addr1 = page_to_phys(bv->bv_page) + bv->bv_offset;
656489fbbcbSMing Lei 	phys_addr_t addr2 = page_to_phys(page) + offset + len - 1;
657489fbbcbSMing Lei 
658489fbbcbSMing Lei 	if ((addr1 | mask) != (addr2 | mask))
659489fbbcbSMing Lei 		return false;
660489fbbcbSMing Lei 	if (bv->bv_len + len > queue_max_segment_size(q))
661489fbbcbSMing Lei 		return false;
662384209cdSChristoph Hellwig 	return __bio_try_merge_page(bio, page, len, offset, same_page);
663489fbbcbSMing Lei }
664489fbbcbSMing Lei 
665f4595875SShaohua Li /**
66619047087SMing Lei  *	__bio_add_pc_page	- attempt to add page to passthrough bio
667c66a14d0SKent Overstreet  *	@q: the target queue
668c66a14d0SKent Overstreet  *	@bio: destination bio
669c66a14d0SKent Overstreet  *	@page: page to add
670c66a14d0SKent Overstreet  *	@len: vec entry length
671c66a14d0SKent Overstreet  *	@offset: vec entry offset
672d1916c86SChristoph Hellwig  *	@same_page: return if the merge happen inside the same page
673f9c78b2bSJens Axboe  *
674c66a14d0SKent Overstreet  *	Attempt to add a page to the bio_vec maplist. This can fail for a
675c66a14d0SKent Overstreet  *	number of reasons, such as the bio being full or target block device
676c66a14d0SKent Overstreet  *	limitations. The target block device must allow bio's up to PAGE_SIZE,
677c66a14d0SKent Overstreet  *	so it is always possible to add a single page to an empty bio.
678c66a14d0SKent Overstreet  *
6795a8ce240SMing Lei  *	This should only be used by passthrough bios.
680f9c78b2bSJens Axboe  */
6814713839dSChristoph Hellwig static int __bio_add_pc_page(struct request_queue *q, struct bio *bio,
68219047087SMing Lei 		struct page *page, unsigned int len, unsigned int offset,
683d1916c86SChristoph Hellwig 		bool *same_page)
684f9c78b2bSJens Axboe {
685f9c78b2bSJens Axboe 	struct bio_vec *bvec;
686f9c78b2bSJens Axboe 
687f9c78b2bSJens Axboe 	/*
688f9c78b2bSJens Axboe 	 * cloned bio must not modify vec list
689f9c78b2bSJens Axboe 	 */
690f9c78b2bSJens Axboe 	if (unlikely(bio_flagged(bio, BIO_CLONED)))
691f9c78b2bSJens Axboe 		return 0;
692f9c78b2bSJens Axboe 
693c66a14d0SKent Overstreet 	if (((bio->bi_iter.bi_size + len) >> 9) > queue_max_hw_sectors(q))
694f9c78b2bSJens Axboe 		return 0;
695f9c78b2bSJens Axboe 
696f9c78b2bSJens Axboe 	if (bio->bi_vcnt > 0) {
697d1916c86SChristoph Hellwig 		if (bio_try_merge_pc_page(q, bio, page, len, offset, same_page))
698384209cdSChristoph Hellwig 			return len;
699320ea869SChristoph Hellwig 
700320ea869SChristoph Hellwig 		/*
701320ea869SChristoph Hellwig 		 * If the queue doesn't support SG gaps and adding this segment
702320ea869SChristoph Hellwig 		 * would create a gap, disallow it.
703320ea869SChristoph Hellwig 		 */
704384209cdSChristoph Hellwig 		bvec = &bio->bi_io_vec[bio->bi_vcnt - 1];
705320ea869SChristoph Hellwig 		if (bvec_gap_to_prev(q, bvec, offset))
706320ea869SChristoph Hellwig 			return 0;
707f9c78b2bSJens Axboe 	}
708f9c78b2bSJens Axboe 
70979d08f89SMing Lei 	if (bio_full(bio, len))
710f9c78b2bSJens Axboe 		return 0;
711f9c78b2bSJens Axboe 
71214ccb66bSChristoph Hellwig 	if (bio->bi_vcnt >= queue_max_segments(q))
713489fbbcbSMing Lei 		return 0;
714489fbbcbSMing Lei 
715f9c78b2bSJens Axboe 	bvec = &bio->bi_io_vec[bio->bi_vcnt];
716f9c78b2bSJens Axboe 	bvec->bv_page = page;
717f9c78b2bSJens Axboe 	bvec->bv_len = len;
718f9c78b2bSJens Axboe 	bvec->bv_offset = offset;
719fcbf6a08SMaurizio Lombardi 	bio->bi_vcnt++;
720dcdca753SChristoph Hellwig 	bio->bi_iter.bi_size += len;
721f9c78b2bSJens Axboe 	return len;
722f9c78b2bSJens Axboe }
72319047087SMing Lei 
72419047087SMing Lei int bio_add_pc_page(struct request_queue *q, struct bio *bio,
72519047087SMing Lei 		struct page *page, unsigned int len, unsigned int offset)
72619047087SMing Lei {
727d1916c86SChristoph Hellwig 	bool same_page = false;
728d1916c86SChristoph Hellwig 	return __bio_add_pc_page(q, bio, page, len, offset, &same_page);
72919047087SMing Lei }
730f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_add_pc_page);
731f9c78b2bSJens Axboe 
732f9c78b2bSJens Axboe /**
7330aa69fd3SChristoph Hellwig  * __bio_try_merge_page - try appending data to an existing bvec.
7340aa69fd3SChristoph Hellwig  * @bio: destination bio
735551879a4SMing Lei  * @page: start page to add
7360aa69fd3SChristoph Hellwig  * @len: length of the data to add
737551879a4SMing Lei  * @off: offset of the data relative to @page
738ff896738SChristoph Hellwig  * @same_page: return if the segment has been merged inside the same page
7390aa69fd3SChristoph Hellwig  *
7400aa69fd3SChristoph Hellwig  * Try to add the data at @page + @off to the last bvec of @bio.  This is a
7410aa69fd3SChristoph Hellwig  * a useful optimisation for file systems with a block size smaller than the
7420aa69fd3SChristoph Hellwig  * page size.
7430aa69fd3SChristoph Hellwig  *
744551879a4SMing Lei  * Warn if (@len, @off) crosses pages in case that @same_page is true.
745551879a4SMing Lei  *
7460aa69fd3SChristoph Hellwig  * Return %true on success or %false on failure.
7470aa69fd3SChristoph Hellwig  */
7480aa69fd3SChristoph Hellwig bool __bio_try_merge_page(struct bio *bio, struct page *page,
749ff896738SChristoph Hellwig 		unsigned int len, unsigned int off, bool *same_page)
7500aa69fd3SChristoph Hellwig {
7510aa69fd3SChristoph Hellwig 	if (WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED)))
7520aa69fd3SChristoph Hellwig 		return false;
7530aa69fd3SChristoph Hellwig 
7540aa69fd3SChristoph Hellwig 	if (bio->bi_vcnt > 0) {
7550aa69fd3SChristoph Hellwig 		struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt - 1];
7560aa69fd3SChristoph Hellwig 
7575919482eSMing Lei 		if (page_is_mergeable(bv, page, len, off, same_page)) {
7580aa69fd3SChristoph Hellwig 			bv->bv_len += len;
7590aa69fd3SChristoph Hellwig 			bio->bi_iter.bi_size += len;
7600aa69fd3SChristoph Hellwig 			return true;
7610aa69fd3SChristoph Hellwig 		}
7625919482eSMing Lei 	}
7630aa69fd3SChristoph Hellwig 	return false;
7640aa69fd3SChristoph Hellwig }
7650aa69fd3SChristoph Hellwig EXPORT_SYMBOL_GPL(__bio_try_merge_page);
7660aa69fd3SChristoph Hellwig 
7670aa69fd3SChristoph Hellwig /**
768551879a4SMing Lei  * __bio_add_page - add page(s) to a bio in a new segment
7690aa69fd3SChristoph Hellwig  * @bio: destination bio
770551879a4SMing Lei  * @page: start page to add
771551879a4SMing Lei  * @len: length of the data to add, may cross pages
772551879a4SMing Lei  * @off: offset of the data relative to @page, may cross pages
7730aa69fd3SChristoph Hellwig  *
7740aa69fd3SChristoph Hellwig  * Add the data at @page + @off to @bio as a new bvec.  The caller must ensure
7750aa69fd3SChristoph Hellwig  * that @bio has space for another bvec.
7760aa69fd3SChristoph Hellwig  */
7770aa69fd3SChristoph Hellwig void __bio_add_page(struct bio *bio, struct page *page,
7780aa69fd3SChristoph Hellwig 		unsigned int len, unsigned int off)
7790aa69fd3SChristoph Hellwig {
7800aa69fd3SChristoph Hellwig 	struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt];
7810aa69fd3SChristoph Hellwig 
7820aa69fd3SChristoph Hellwig 	WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
78379d08f89SMing Lei 	WARN_ON_ONCE(bio_full(bio, len));
7840aa69fd3SChristoph Hellwig 
7850aa69fd3SChristoph Hellwig 	bv->bv_page = page;
7860aa69fd3SChristoph Hellwig 	bv->bv_offset = off;
7870aa69fd3SChristoph Hellwig 	bv->bv_len = len;
7880aa69fd3SChristoph Hellwig 
7890aa69fd3SChristoph Hellwig 	bio->bi_iter.bi_size += len;
7900aa69fd3SChristoph Hellwig 	bio->bi_vcnt++;
791b8e24a93SJohannes Weiner 
792b8e24a93SJohannes Weiner 	if (!bio_flagged(bio, BIO_WORKINGSET) && unlikely(PageWorkingset(page)))
793b8e24a93SJohannes Weiner 		bio_set_flag(bio, BIO_WORKINGSET);
7940aa69fd3SChristoph Hellwig }
7950aa69fd3SChristoph Hellwig EXPORT_SYMBOL_GPL(__bio_add_page);
7960aa69fd3SChristoph Hellwig 
7970aa69fd3SChristoph Hellwig /**
798551879a4SMing Lei  *	bio_add_page	-	attempt to add page(s) to bio
799f9c78b2bSJens Axboe  *	@bio: destination bio
800551879a4SMing Lei  *	@page: start page to add
801551879a4SMing Lei  *	@len: vec entry length, may cross pages
802551879a4SMing Lei  *	@offset: vec entry offset relative to @page, may cross pages
803f9c78b2bSJens Axboe  *
804551879a4SMing Lei  *	Attempt to add page(s) to the bio_vec maplist. This will only fail
805c66a14d0SKent Overstreet  *	if either bio->bi_vcnt == bio->bi_max_vecs or it's a cloned bio.
806f9c78b2bSJens Axboe  */
807c66a14d0SKent Overstreet int bio_add_page(struct bio *bio, struct page *page,
808c66a14d0SKent Overstreet 		 unsigned int len, unsigned int offset)
809f9c78b2bSJens Axboe {
810ff896738SChristoph Hellwig 	bool same_page = false;
811ff896738SChristoph Hellwig 
812ff896738SChristoph Hellwig 	if (!__bio_try_merge_page(bio, page, len, offset, &same_page)) {
81379d08f89SMing Lei 		if (bio_full(bio, len))
814c66a14d0SKent Overstreet 			return 0;
8150aa69fd3SChristoph Hellwig 		__bio_add_page(bio, page, len, offset);
816c66a14d0SKent Overstreet 	}
817c66a14d0SKent Overstreet 	return len;
818f9c78b2bSJens Axboe }
819f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_add_page);
820f9c78b2bSJens Axboe 
821d241a95fSChristoph Hellwig void bio_release_pages(struct bio *bio, bool mark_dirty)
8227321ecbfSChristoph Hellwig {
8237321ecbfSChristoph Hellwig 	struct bvec_iter_all iter_all;
8247321ecbfSChristoph Hellwig 	struct bio_vec *bvec;
8257321ecbfSChristoph Hellwig 
826b2d0d991SChristoph Hellwig 	if (bio_flagged(bio, BIO_NO_PAGE_REF))
827b2d0d991SChristoph Hellwig 		return;
828b2d0d991SChristoph Hellwig 
829d241a95fSChristoph Hellwig 	bio_for_each_segment_all(bvec, bio, iter_all) {
830d241a95fSChristoph Hellwig 		if (mark_dirty && !PageCompound(bvec->bv_page))
831d241a95fSChristoph Hellwig 			set_page_dirty_lock(bvec->bv_page);
8327321ecbfSChristoph Hellwig 		put_page(bvec->bv_page);
8337321ecbfSChristoph Hellwig 	}
834d241a95fSChristoph Hellwig }
8357321ecbfSChristoph Hellwig 
8366d0c48aeSJens Axboe static int __bio_iov_bvec_add_pages(struct bio *bio, struct iov_iter *iter)
8376d0c48aeSJens Axboe {
8386d0c48aeSJens Axboe 	const struct bio_vec *bv = iter->bvec;
8396d0c48aeSJens Axboe 	unsigned int len;
8406d0c48aeSJens Axboe 	size_t size;
8416d0c48aeSJens Axboe 
8426d0c48aeSJens Axboe 	if (WARN_ON_ONCE(iter->iov_offset > bv->bv_len))
8436d0c48aeSJens Axboe 		return -EINVAL;
8446d0c48aeSJens Axboe 
8456d0c48aeSJens Axboe 	len = min_t(size_t, bv->bv_len - iter->iov_offset, iter->count);
8466d0c48aeSJens Axboe 	size = bio_add_page(bio, bv->bv_page, len,
8476d0c48aeSJens Axboe 				bv->bv_offset + iter->iov_offset);
848a10584c3SChristoph Hellwig 	if (unlikely(size != len))
849a10584c3SChristoph Hellwig 		return -EINVAL;
8506d0c48aeSJens Axboe 	iov_iter_advance(iter, size);
8516d0c48aeSJens Axboe 	return 0;
8526d0c48aeSJens Axboe }
8536d0c48aeSJens Axboe 
854576ed913SChristoph Hellwig #define PAGE_PTRS_PER_BVEC     (sizeof(struct bio_vec) / sizeof(struct page *))
855576ed913SChristoph Hellwig 
8562cefe4dbSKent Overstreet /**
85717d51b10SMartin Wilck  * __bio_iov_iter_get_pages - pin user or kernel pages and add them to a bio
8582cefe4dbSKent Overstreet  * @bio: bio to add pages to
8592cefe4dbSKent Overstreet  * @iter: iov iterator describing the region to be mapped
8602cefe4dbSKent Overstreet  *
86117d51b10SMartin Wilck  * Pins pages from *iter and appends them to @bio's bvec array. The
8622cefe4dbSKent Overstreet  * pages will have to be released using put_page() when done.
86317d51b10SMartin Wilck  * For multi-segment *iter, this function only adds pages from the
86417d51b10SMartin Wilck  * the next non-empty segment of the iov iterator.
8652cefe4dbSKent Overstreet  */
86617d51b10SMartin Wilck static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
8672cefe4dbSKent Overstreet {
868576ed913SChristoph Hellwig 	unsigned short nr_pages = bio->bi_max_vecs - bio->bi_vcnt;
869576ed913SChristoph Hellwig 	unsigned short entries_left = bio->bi_max_vecs - bio->bi_vcnt;
8702cefe4dbSKent Overstreet 	struct bio_vec *bv = bio->bi_io_vec + bio->bi_vcnt;
8712cefe4dbSKent Overstreet 	struct page **pages = (struct page **)bv;
87245691804SChristoph Hellwig 	bool same_page = false;
873576ed913SChristoph Hellwig 	ssize_t size, left;
874576ed913SChristoph Hellwig 	unsigned len, i;
875b403ea24SMartin Wilck 	size_t offset;
876576ed913SChristoph Hellwig 
877576ed913SChristoph Hellwig 	/*
878576ed913SChristoph Hellwig 	 * Move page array up in the allocated memory for the bio vecs as far as
879576ed913SChristoph Hellwig 	 * possible so that we can start filling biovecs from the beginning
880576ed913SChristoph Hellwig 	 * without overwriting the temporary page array.
881576ed913SChristoph Hellwig 	*/
882576ed913SChristoph Hellwig 	BUILD_BUG_ON(PAGE_PTRS_PER_BVEC < 2);
883576ed913SChristoph Hellwig 	pages += entries_left * (PAGE_PTRS_PER_BVEC - 1);
8842cefe4dbSKent Overstreet 
8852cefe4dbSKent Overstreet 	size = iov_iter_get_pages(iter, pages, LONG_MAX, nr_pages, &offset);
8862cefe4dbSKent Overstreet 	if (unlikely(size <= 0))
8872cefe4dbSKent Overstreet 		return size ? size : -EFAULT;
8882cefe4dbSKent Overstreet 
889576ed913SChristoph Hellwig 	for (left = size, i = 0; left > 0; left -= len, i++) {
890576ed913SChristoph Hellwig 		struct page *page = pages[i];
8912cefe4dbSKent Overstreet 
892576ed913SChristoph Hellwig 		len = min_t(size_t, PAGE_SIZE - offset, left);
89345691804SChristoph Hellwig 
89445691804SChristoph Hellwig 		if (__bio_try_merge_page(bio, page, len, offset, &same_page)) {
89545691804SChristoph Hellwig 			if (same_page)
89645691804SChristoph Hellwig 				put_page(page);
89745691804SChristoph Hellwig 		} else {
89879d08f89SMing Lei 			if (WARN_ON_ONCE(bio_full(bio, len)))
899576ed913SChristoph Hellwig                                 return -EINVAL;
90045691804SChristoph Hellwig 			__bio_add_page(bio, page, len, offset);
90145691804SChristoph Hellwig 		}
902576ed913SChristoph Hellwig 		offset = 0;
9032cefe4dbSKent Overstreet 	}
9042cefe4dbSKent Overstreet 
9052cefe4dbSKent Overstreet 	iov_iter_advance(iter, size);
9062cefe4dbSKent Overstreet 	return 0;
9072cefe4dbSKent Overstreet }
90817d51b10SMartin Wilck 
90917d51b10SMartin Wilck /**
9106d0c48aeSJens Axboe  * bio_iov_iter_get_pages - add user or kernel pages to a bio
91117d51b10SMartin Wilck  * @bio: bio to add pages to
9126d0c48aeSJens Axboe  * @iter: iov iterator describing the region to be added
91317d51b10SMartin Wilck  *
9146d0c48aeSJens Axboe  * This takes either an iterator pointing to user memory, or one pointing to
9156d0c48aeSJens Axboe  * kernel pages (BVEC iterator). If we're adding user pages, we pin them and
9166d0c48aeSJens Axboe  * map them into the kernel. On IO completion, the caller should put those
917399254aaSJens Axboe  * pages. If we're adding kernel pages, and the caller told us it's safe to
918399254aaSJens Axboe  * do so, we just have to add the pages to the bio directly. We don't grab an
919399254aaSJens Axboe  * extra reference to those pages (the user should already have that), and we
920399254aaSJens Axboe  * don't put the page on IO completion. The caller needs to check if the bio is
921399254aaSJens Axboe  * flagged BIO_NO_PAGE_REF on IO completion. If it isn't, then pages should be
922399254aaSJens Axboe  * released.
9236d0c48aeSJens Axboe  *
92417d51b10SMartin Wilck  * The function tries, but does not guarantee, to pin as many pages as
9256d0c48aeSJens Axboe  * fit into the bio, or are requested in *iter, whatever is smaller. If
9266d0c48aeSJens Axboe  * MM encounters an error pinning the requested pages, it stops. Error
9276d0c48aeSJens Axboe  * is returned only if 0 pages could be pinned.
92817d51b10SMartin Wilck  */
92917d51b10SMartin Wilck int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
93017d51b10SMartin Wilck {
9316d0c48aeSJens Axboe 	const bool is_bvec = iov_iter_is_bvec(iter);
93214eacf12SChristoph Hellwig 	int ret;
93314eacf12SChristoph Hellwig 
93414eacf12SChristoph Hellwig 	if (WARN_ON_ONCE(bio->bi_vcnt))
93514eacf12SChristoph Hellwig 		return -EINVAL;
93617d51b10SMartin Wilck 
93717d51b10SMartin Wilck 	do {
9386d0c48aeSJens Axboe 		if (is_bvec)
9396d0c48aeSJens Axboe 			ret = __bio_iov_bvec_add_pages(bio, iter);
9406d0c48aeSJens Axboe 		else
9416d0c48aeSJens Axboe 			ret = __bio_iov_iter_get_pages(bio, iter);
94279d08f89SMing Lei 	} while (!ret && iov_iter_count(iter) && !bio_full(bio, 0));
94317d51b10SMartin Wilck 
944b6207430SChristoph Hellwig 	if (is_bvec)
9457321ecbfSChristoph Hellwig 		bio_set_flag(bio, BIO_NO_PAGE_REF);
94614eacf12SChristoph Hellwig 	return bio->bi_vcnt ? 0 : ret;
94717d51b10SMartin Wilck }
9482cefe4dbSKent Overstreet 
9494246a0b6SChristoph Hellwig static void submit_bio_wait_endio(struct bio *bio)
950f9c78b2bSJens Axboe {
95165e53aabSChristoph Hellwig 	complete(bio->bi_private);
952f9c78b2bSJens Axboe }
953f9c78b2bSJens Axboe 
954f9c78b2bSJens Axboe /**
955f9c78b2bSJens Axboe  * submit_bio_wait - submit a bio, and wait until it completes
956f9c78b2bSJens Axboe  * @bio: The &struct bio which describes the I/O
957f9c78b2bSJens Axboe  *
958f9c78b2bSJens Axboe  * Simple wrapper around submit_bio(). Returns 0 on success, or the error from
959f9c78b2bSJens Axboe  * bio_endio() on failure.
9603d289d68SJan Kara  *
9613d289d68SJan Kara  * WARNING: Unlike to how submit_bio() is usually used, this function does not
9623d289d68SJan Kara  * result in bio reference to be consumed. The caller must drop the reference
9633d289d68SJan Kara  * on his own.
964f9c78b2bSJens Axboe  */
9654e49ea4aSMike Christie int submit_bio_wait(struct bio *bio)
966f9c78b2bSJens Axboe {
967e319e1fbSByungchul Park 	DECLARE_COMPLETION_ONSTACK_MAP(done, bio->bi_disk->lockdep_map);
968f9c78b2bSJens Axboe 
96965e53aabSChristoph Hellwig 	bio->bi_private = &done;
970f9c78b2bSJens Axboe 	bio->bi_end_io = submit_bio_wait_endio;
9711eff9d32SJens Axboe 	bio->bi_opf |= REQ_SYNC;
9724e49ea4aSMike Christie 	submit_bio(bio);
97365e53aabSChristoph Hellwig 	wait_for_completion_io(&done);
974f9c78b2bSJens Axboe 
97565e53aabSChristoph Hellwig 	return blk_status_to_errno(bio->bi_status);
976f9c78b2bSJens Axboe }
977f9c78b2bSJens Axboe EXPORT_SYMBOL(submit_bio_wait);
978f9c78b2bSJens Axboe 
979f9c78b2bSJens Axboe /**
980f9c78b2bSJens Axboe  * bio_advance - increment/complete a bio by some number of bytes
981f9c78b2bSJens Axboe  * @bio:	bio to advance
982f9c78b2bSJens Axboe  * @bytes:	number of bytes to complete
983f9c78b2bSJens Axboe  *
984f9c78b2bSJens Axboe  * This updates bi_sector, bi_size and bi_idx; if the number of bytes to
985f9c78b2bSJens Axboe  * complete doesn't align with a bvec boundary, then bv_len and bv_offset will
986f9c78b2bSJens Axboe  * be updated on the last bvec as well.
987f9c78b2bSJens Axboe  *
988f9c78b2bSJens Axboe  * @bio will then represent the remaining, uncompleted portion of the io.
989f9c78b2bSJens Axboe  */
990f9c78b2bSJens Axboe void bio_advance(struct bio *bio, unsigned bytes)
991f9c78b2bSJens Axboe {
992f9c78b2bSJens Axboe 	if (bio_integrity(bio))
993f9c78b2bSJens Axboe 		bio_integrity_advance(bio, bytes);
994f9c78b2bSJens Axboe 
995f9c78b2bSJens Axboe 	bio_advance_iter(bio, &bio->bi_iter, bytes);
996f9c78b2bSJens Axboe }
997f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_advance);
998f9c78b2bSJens Axboe 
99945db54d5SKent Overstreet void bio_copy_data_iter(struct bio *dst, struct bvec_iter *dst_iter,
100045db54d5SKent Overstreet 			struct bio *src, struct bvec_iter *src_iter)
1001f9c78b2bSJens Axboe {
1002f9c78b2bSJens Axboe 	struct bio_vec src_bv, dst_bv;
1003f9c78b2bSJens Axboe 	void *src_p, *dst_p;
1004f9c78b2bSJens Axboe 	unsigned bytes;
1005f9c78b2bSJens Axboe 
100645db54d5SKent Overstreet 	while (src_iter->bi_size && dst_iter->bi_size) {
100745db54d5SKent Overstreet 		src_bv = bio_iter_iovec(src, *src_iter);
100845db54d5SKent Overstreet 		dst_bv = bio_iter_iovec(dst, *dst_iter);
100945db54d5SKent Overstreet 
101045db54d5SKent Overstreet 		bytes = min(src_bv.bv_len, dst_bv.bv_len);
101145db54d5SKent Overstreet 
101245db54d5SKent Overstreet 		src_p = kmap_atomic(src_bv.bv_page);
101345db54d5SKent Overstreet 		dst_p = kmap_atomic(dst_bv.bv_page);
101445db54d5SKent Overstreet 
101545db54d5SKent Overstreet 		memcpy(dst_p + dst_bv.bv_offset,
101645db54d5SKent Overstreet 		       src_p + src_bv.bv_offset,
101745db54d5SKent Overstreet 		       bytes);
101845db54d5SKent Overstreet 
101945db54d5SKent Overstreet 		kunmap_atomic(dst_p);
102045db54d5SKent Overstreet 		kunmap_atomic(src_p);
102145db54d5SKent Overstreet 
10226e6e811dSKent Overstreet 		flush_dcache_page(dst_bv.bv_page);
10236e6e811dSKent Overstreet 
102445db54d5SKent Overstreet 		bio_advance_iter(src, src_iter, bytes);
102545db54d5SKent Overstreet 		bio_advance_iter(dst, dst_iter, bytes);
102645db54d5SKent Overstreet 	}
102745db54d5SKent Overstreet }
102845db54d5SKent Overstreet EXPORT_SYMBOL(bio_copy_data_iter);
102945db54d5SKent Overstreet 
103045db54d5SKent Overstreet /**
103145db54d5SKent Overstreet  * bio_copy_data - copy contents of data buffers from one bio to another
103245db54d5SKent Overstreet  * @src: source bio
103345db54d5SKent Overstreet  * @dst: destination bio
103445db54d5SKent Overstreet  *
103545db54d5SKent Overstreet  * Stops when it reaches the end of either @src or @dst - that is, copies
103645db54d5SKent Overstreet  * min(src->bi_size, dst->bi_size) bytes (or the equivalent for lists of bios).
103745db54d5SKent Overstreet  */
103845db54d5SKent Overstreet void bio_copy_data(struct bio *dst, struct bio *src)
103945db54d5SKent Overstreet {
104045db54d5SKent Overstreet 	struct bvec_iter src_iter = src->bi_iter;
104145db54d5SKent Overstreet 	struct bvec_iter dst_iter = dst->bi_iter;
104245db54d5SKent Overstreet 
104345db54d5SKent Overstreet 	bio_copy_data_iter(dst, &dst_iter, src, &src_iter);
104445db54d5SKent Overstreet }
104545db54d5SKent Overstreet EXPORT_SYMBOL(bio_copy_data);
104645db54d5SKent Overstreet 
104745db54d5SKent Overstreet /**
104845db54d5SKent Overstreet  * bio_list_copy_data - copy contents of data buffers from one chain of bios to
104945db54d5SKent Overstreet  * another
105045db54d5SKent Overstreet  * @src: source bio list
105145db54d5SKent Overstreet  * @dst: destination bio list
105245db54d5SKent Overstreet  *
105345db54d5SKent Overstreet  * Stops when it reaches the end of either the @src list or @dst list - that is,
105445db54d5SKent Overstreet  * copies min(src->bi_size, dst->bi_size) bytes (or the equivalent for lists of
105545db54d5SKent Overstreet  * bios).
105645db54d5SKent Overstreet  */
105745db54d5SKent Overstreet void bio_list_copy_data(struct bio *dst, struct bio *src)
105845db54d5SKent Overstreet {
105945db54d5SKent Overstreet 	struct bvec_iter src_iter = src->bi_iter;
106045db54d5SKent Overstreet 	struct bvec_iter dst_iter = dst->bi_iter;
106145db54d5SKent Overstreet 
1062f9c78b2bSJens Axboe 	while (1) {
1063f9c78b2bSJens Axboe 		if (!src_iter.bi_size) {
1064f9c78b2bSJens Axboe 			src = src->bi_next;
1065f9c78b2bSJens Axboe 			if (!src)
1066f9c78b2bSJens Axboe 				break;
1067f9c78b2bSJens Axboe 
1068f9c78b2bSJens Axboe 			src_iter = src->bi_iter;
1069f9c78b2bSJens Axboe 		}
1070f9c78b2bSJens Axboe 
1071f9c78b2bSJens Axboe 		if (!dst_iter.bi_size) {
1072f9c78b2bSJens Axboe 			dst = dst->bi_next;
1073f9c78b2bSJens Axboe 			if (!dst)
1074f9c78b2bSJens Axboe 				break;
1075f9c78b2bSJens Axboe 
1076f9c78b2bSJens Axboe 			dst_iter = dst->bi_iter;
1077f9c78b2bSJens Axboe 		}
1078f9c78b2bSJens Axboe 
107945db54d5SKent Overstreet 		bio_copy_data_iter(dst, &dst_iter, src, &src_iter);
1080f9c78b2bSJens Axboe 	}
1081f9c78b2bSJens Axboe }
108245db54d5SKent Overstreet EXPORT_SYMBOL(bio_list_copy_data);
1083f9c78b2bSJens Axboe 
1084f9c78b2bSJens Axboe struct bio_map_data {
1085f9c78b2bSJens Axboe 	int is_our_pages;
108626e49cfcSKent Overstreet 	struct iov_iter iter;
108726e49cfcSKent Overstreet 	struct iovec iov[];
1088f9c78b2bSJens Axboe };
1089f9c78b2bSJens Axboe 
10900e5b935dSAl Viro static struct bio_map_data *bio_alloc_map_data(struct iov_iter *data,
1091f9c78b2bSJens Axboe 					       gfp_t gfp_mask)
1092f9c78b2bSJens Axboe {
10930e5b935dSAl Viro 	struct bio_map_data *bmd;
10940e5b935dSAl Viro 	if (data->nr_segs > UIO_MAXIOV)
1095f9c78b2bSJens Axboe 		return NULL;
1096f9c78b2bSJens Axboe 
1097f1f8f292SGustavo A. R. Silva 	bmd = kmalloc(struct_size(bmd, iov, data->nr_segs), gfp_mask);
10980e5b935dSAl Viro 	if (!bmd)
10990e5b935dSAl Viro 		return NULL;
11000e5b935dSAl Viro 	memcpy(bmd->iov, data->iov, sizeof(struct iovec) * data->nr_segs);
11010e5b935dSAl Viro 	bmd->iter = *data;
11020e5b935dSAl Viro 	bmd->iter.iov = bmd->iov;
11030e5b935dSAl Viro 	return bmd;
1104f9c78b2bSJens Axboe }
1105f9c78b2bSJens Axboe 
11069124d3feSDongsu Park /**
11079124d3feSDongsu Park  * bio_copy_from_iter - copy all pages from iov_iter to bio
11089124d3feSDongsu Park  * @bio: The &struct bio which describes the I/O as destination
11099124d3feSDongsu Park  * @iter: iov_iter as source
11109124d3feSDongsu Park  *
11119124d3feSDongsu Park  * Copy all pages from iov_iter to bio.
11129124d3feSDongsu Park  * Returns 0 on success, or error on failure.
11139124d3feSDongsu Park  */
111498a09d61SAl Viro static int bio_copy_from_iter(struct bio *bio, struct iov_iter *iter)
1115f9c78b2bSJens Axboe {
1116f9c78b2bSJens Axboe 	struct bio_vec *bvec;
11176dc4f100SMing Lei 	struct bvec_iter_all iter_all;
1118f9c78b2bSJens Axboe 
11192b070cfeSChristoph Hellwig 	bio_for_each_segment_all(bvec, bio, iter_all) {
11209124d3feSDongsu Park 		ssize_t ret;
1121f9c78b2bSJens Axboe 
11229124d3feSDongsu Park 		ret = copy_page_from_iter(bvec->bv_page,
11239124d3feSDongsu Park 					  bvec->bv_offset,
11249124d3feSDongsu Park 					  bvec->bv_len,
112598a09d61SAl Viro 					  iter);
1126f9c78b2bSJens Axboe 
112798a09d61SAl Viro 		if (!iov_iter_count(iter))
11289124d3feSDongsu Park 			break;
1129f9c78b2bSJens Axboe 
11309124d3feSDongsu Park 		if (ret < bvec->bv_len)
11319124d3feSDongsu Park 			return -EFAULT;
1132f9c78b2bSJens Axboe 	}
1133f9c78b2bSJens Axboe 
11349124d3feSDongsu Park 	return 0;
1135f9c78b2bSJens Axboe }
1136f9c78b2bSJens Axboe 
11379124d3feSDongsu Park /**
11389124d3feSDongsu Park  * bio_copy_to_iter - copy all pages from bio to iov_iter
11399124d3feSDongsu Park  * @bio: The &struct bio which describes the I/O as source
11409124d3feSDongsu Park  * @iter: iov_iter as destination
11419124d3feSDongsu Park  *
11429124d3feSDongsu Park  * Copy all pages from bio to iov_iter.
11439124d3feSDongsu Park  * Returns 0 on success, or error on failure.
11449124d3feSDongsu Park  */
11459124d3feSDongsu Park static int bio_copy_to_iter(struct bio *bio, struct iov_iter iter)
11469124d3feSDongsu Park {
11479124d3feSDongsu Park 	struct bio_vec *bvec;
11486dc4f100SMing Lei 	struct bvec_iter_all iter_all;
11499124d3feSDongsu Park 
11502b070cfeSChristoph Hellwig 	bio_for_each_segment_all(bvec, bio, iter_all) {
11519124d3feSDongsu Park 		ssize_t ret;
11529124d3feSDongsu Park 
11539124d3feSDongsu Park 		ret = copy_page_to_iter(bvec->bv_page,
11549124d3feSDongsu Park 					bvec->bv_offset,
11559124d3feSDongsu Park 					bvec->bv_len,
11569124d3feSDongsu Park 					&iter);
11579124d3feSDongsu Park 
11589124d3feSDongsu Park 		if (!iov_iter_count(&iter))
11599124d3feSDongsu Park 			break;
11609124d3feSDongsu Park 
11619124d3feSDongsu Park 		if (ret < bvec->bv_len)
11629124d3feSDongsu Park 			return -EFAULT;
11639124d3feSDongsu Park 	}
11649124d3feSDongsu Park 
11659124d3feSDongsu Park 	return 0;
1166f9c78b2bSJens Axboe }
1167f9c78b2bSJens Axboe 
1168491221f8SGuoqing Jiang void bio_free_pages(struct bio *bio)
11691dfa0f68SChristoph Hellwig {
11701dfa0f68SChristoph Hellwig 	struct bio_vec *bvec;
11716dc4f100SMing Lei 	struct bvec_iter_all iter_all;
11721dfa0f68SChristoph Hellwig 
11732b070cfeSChristoph Hellwig 	bio_for_each_segment_all(bvec, bio, iter_all)
11741dfa0f68SChristoph Hellwig 		__free_page(bvec->bv_page);
11751dfa0f68SChristoph Hellwig }
1176491221f8SGuoqing Jiang EXPORT_SYMBOL(bio_free_pages);
11771dfa0f68SChristoph Hellwig 
1178f9c78b2bSJens Axboe /**
1179f9c78b2bSJens Axboe  *	bio_uncopy_user	-	finish previously mapped bio
1180f9c78b2bSJens Axboe  *	@bio: bio being terminated
1181f9c78b2bSJens Axboe  *
1182ddad8dd0SChristoph Hellwig  *	Free pages allocated from bio_copy_user_iov() and write back data
1183f9c78b2bSJens Axboe  *	to user space in case of a read.
1184f9c78b2bSJens Axboe  */
1185f9c78b2bSJens Axboe int bio_uncopy_user(struct bio *bio)
1186f9c78b2bSJens Axboe {
1187f9c78b2bSJens Axboe 	struct bio_map_data *bmd = bio->bi_private;
11881dfa0f68SChristoph Hellwig 	int ret = 0;
1189f9c78b2bSJens Axboe 
1190f9c78b2bSJens Axboe 	if (!bio_flagged(bio, BIO_NULL_MAPPED)) {
1191f9c78b2bSJens Axboe 		/*
1192f9c78b2bSJens Axboe 		 * if we're in a workqueue, the request is orphaned, so
11932d99b55dSHannes Reinecke 		 * don't copy into a random user address space, just free
11942d99b55dSHannes Reinecke 		 * and return -EINTR so user space doesn't expect any data.
1195f9c78b2bSJens Axboe 		 */
11962d99b55dSHannes Reinecke 		if (!current->mm)
11972d99b55dSHannes Reinecke 			ret = -EINTR;
11982d99b55dSHannes Reinecke 		else if (bio_data_dir(bio) == READ)
11999124d3feSDongsu Park 			ret = bio_copy_to_iter(bio, bmd->iter);
12001dfa0f68SChristoph Hellwig 		if (bmd->is_our_pages)
12011dfa0f68SChristoph Hellwig 			bio_free_pages(bio);
1202f9c78b2bSJens Axboe 	}
1203f9c78b2bSJens Axboe 	kfree(bmd);
1204f9c78b2bSJens Axboe 	bio_put(bio);
1205f9c78b2bSJens Axboe 	return ret;
1206f9c78b2bSJens Axboe }
1207f9c78b2bSJens Axboe 
1208f9c78b2bSJens Axboe /**
1209f9c78b2bSJens Axboe  *	bio_copy_user_iov	-	copy user data to bio
1210f9c78b2bSJens Axboe  *	@q:		destination block queue
1211f9c78b2bSJens Axboe  *	@map_data:	pointer to the rq_map_data holding pages (if necessary)
121226e49cfcSKent Overstreet  *	@iter:		iovec iterator
1213f9c78b2bSJens Axboe  *	@gfp_mask:	memory allocation flags
1214f9c78b2bSJens Axboe  *
1215f9c78b2bSJens Axboe  *	Prepares and returns a bio for indirect user io, bouncing data
1216f9c78b2bSJens Axboe  *	to/from kernel pages as necessary. Must be paired with
1217f9c78b2bSJens Axboe  *	call bio_uncopy_user() on io completion.
1218f9c78b2bSJens Axboe  */
1219f9c78b2bSJens Axboe struct bio *bio_copy_user_iov(struct request_queue *q,
1220f9c78b2bSJens Axboe 			      struct rq_map_data *map_data,
1221e81cef5dSAl Viro 			      struct iov_iter *iter,
122226e49cfcSKent Overstreet 			      gfp_t gfp_mask)
1223f9c78b2bSJens Axboe {
1224f9c78b2bSJens Axboe 	struct bio_map_data *bmd;
1225f9c78b2bSJens Axboe 	struct page *page;
1226f9c78b2bSJens Axboe 	struct bio *bio;
1227d16d44ebSAl Viro 	int i = 0, ret;
1228d16d44ebSAl Viro 	int nr_pages;
122926e49cfcSKent Overstreet 	unsigned int len = iter->count;
1230bd5ceceaSGeliang Tang 	unsigned int offset = map_data ? offset_in_page(map_data->offset) : 0;
1231f9c78b2bSJens Axboe 
12320e5b935dSAl Viro 	bmd = bio_alloc_map_data(iter, gfp_mask);
1233f9c78b2bSJens Axboe 	if (!bmd)
1234f9c78b2bSJens Axboe 		return ERR_PTR(-ENOMEM);
1235f9c78b2bSJens Axboe 
123626e49cfcSKent Overstreet 	/*
123726e49cfcSKent Overstreet 	 * We need to do a deep copy of the iov_iter including the iovecs.
123826e49cfcSKent Overstreet 	 * The caller provided iov might point to an on-stack or otherwise
123926e49cfcSKent Overstreet 	 * shortlived one.
124026e49cfcSKent Overstreet 	 */
124126e49cfcSKent Overstreet 	bmd->is_our_pages = map_data ? 0 : 1;
124226e49cfcSKent Overstreet 
1243d16d44ebSAl Viro 	nr_pages = DIV_ROUND_UP(offset + len, PAGE_SIZE);
1244d16d44ebSAl Viro 	if (nr_pages > BIO_MAX_PAGES)
1245d16d44ebSAl Viro 		nr_pages = BIO_MAX_PAGES;
1246f9c78b2bSJens Axboe 
1247f9c78b2bSJens Axboe 	ret = -ENOMEM;
1248f9c78b2bSJens Axboe 	bio = bio_kmalloc(gfp_mask, nr_pages);
1249f9c78b2bSJens Axboe 	if (!bio)
1250f9c78b2bSJens Axboe 		goto out_bmd;
1251f9c78b2bSJens Axboe 
1252f9c78b2bSJens Axboe 	ret = 0;
1253f9c78b2bSJens Axboe 
1254f9c78b2bSJens Axboe 	if (map_data) {
1255f9c78b2bSJens Axboe 		nr_pages = 1 << map_data->page_order;
1256f9c78b2bSJens Axboe 		i = map_data->offset / PAGE_SIZE;
1257f9c78b2bSJens Axboe 	}
1258f9c78b2bSJens Axboe 	while (len) {
1259f9c78b2bSJens Axboe 		unsigned int bytes = PAGE_SIZE;
1260f9c78b2bSJens Axboe 
1261f9c78b2bSJens Axboe 		bytes -= offset;
1262f9c78b2bSJens Axboe 
1263f9c78b2bSJens Axboe 		if (bytes > len)
1264f9c78b2bSJens Axboe 			bytes = len;
1265f9c78b2bSJens Axboe 
1266f9c78b2bSJens Axboe 		if (map_data) {
1267f9c78b2bSJens Axboe 			if (i == map_data->nr_entries * nr_pages) {
1268f9c78b2bSJens Axboe 				ret = -ENOMEM;
1269f9c78b2bSJens Axboe 				break;
1270f9c78b2bSJens Axboe 			}
1271f9c78b2bSJens Axboe 
1272f9c78b2bSJens Axboe 			page = map_data->pages[i / nr_pages];
1273f9c78b2bSJens Axboe 			page += (i % nr_pages);
1274f9c78b2bSJens Axboe 
1275f9c78b2bSJens Axboe 			i++;
1276f9c78b2bSJens Axboe 		} else {
1277f9c78b2bSJens Axboe 			page = alloc_page(q->bounce_gfp | gfp_mask);
1278f9c78b2bSJens Axboe 			if (!page) {
1279f9c78b2bSJens Axboe 				ret = -ENOMEM;
1280f9c78b2bSJens Axboe 				break;
1281f9c78b2bSJens Axboe 			}
1282f9c78b2bSJens Axboe 		}
1283f9c78b2bSJens Axboe 
1284a3761c3cSJérôme Glisse 		if (bio_add_pc_page(q, bio, page, bytes, offset) < bytes) {
1285a3761c3cSJérôme Glisse 			if (!map_data)
1286a3761c3cSJérôme Glisse 				__free_page(page);
1287f9c78b2bSJens Axboe 			break;
1288a3761c3cSJérôme Glisse 		}
1289f9c78b2bSJens Axboe 
1290f9c78b2bSJens Axboe 		len -= bytes;
1291f9c78b2bSJens Axboe 		offset = 0;
1292f9c78b2bSJens Axboe 	}
1293f9c78b2bSJens Axboe 
1294f9c78b2bSJens Axboe 	if (ret)
1295f9c78b2bSJens Axboe 		goto cleanup;
1296f9c78b2bSJens Axboe 
12972884d0beSAl Viro 	if (map_data)
12982884d0beSAl Viro 		map_data->offset += bio->bi_iter.bi_size;
12992884d0beSAl Viro 
1300f9c78b2bSJens Axboe 	/*
1301f9c78b2bSJens Axboe 	 * success
1302f9c78b2bSJens Axboe 	 */
130300e23707SDavid Howells 	if ((iov_iter_rw(iter) == WRITE && (!map_data || !map_data->null_mapped)) ||
1304f9c78b2bSJens Axboe 	    (map_data && map_data->from_user)) {
130598a09d61SAl Viro 		ret = bio_copy_from_iter(bio, iter);
1306f9c78b2bSJens Axboe 		if (ret)
1307f9c78b2bSJens Axboe 			goto cleanup;
130898a09d61SAl Viro 	} else {
1309f55adad6SKeith Busch 		if (bmd->is_our_pages)
1310f3587d76SKeith Busch 			zero_fill_bio(bio);
1311e81cef5dSAl Viro 		iov_iter_advance(iter, bio->bi_iter.bi_size);
1312f9c78b2bSJens Axboe 	}
1313f9c78b2bSJens Axboe 
131426e49cfcSKent Overstreet 	bio->bi_private = bmd;
13152884d0beSAl Viro 	if (map_data && map_data->null_mapped)
13162884d0beSAl Viro 		bio_set_flag(bio, BIO_NULL_MAPPED);
1317f9c78b2bSJens Axboe 	return bio;
1318f9c78b2bSJens Axboe cleanup:
1319f9c78b2bSJens Axboe 	if (!map_data)
13201dfa0f68SChristoph Hellwig 		bio_free_pages(bio);
1321f9c78b2bSJens Axboe 	bio_put(bio);
1322f9c78b2bSJens Axboe out_bmd:
1323f9c78b2bSJens Axboe 	kfree(bmd);
1324f9c78b2bSJens Axboe 	return ERR_PTR(ret);
1325f9c78b2bSJens Axboe }
1326f9c78b2bSJens Axboe 
132737f19e57SChristoph Hellwig /**
132837f19e57SChristoph Hellwig  *	bio_map_user_iov - map user iovec into bio
132937f19e57SChristoph Hellwig  *	@q:		the struct request_queue for the bio
133037f19e57SChristoph Hellwig  *	@iter:		iovec iterator
133137f19e57SChristoph Hellwig  *	@gfp_mask:	memory allocation flags
133237f19e57SChristoph Hellwig  *
133337f19e57SChristoph Hellwig  *	Map the user space address into a bio suitable for io to a block
133437f19e57SChristoph Hellwig  *	device. Returns an error pointer in case of error.
133537f19e57SChristoph Hellwig  */
133637f19e57SChristoph Hellwig struct bio *bio_map_user_iov(struct request_queue *q,
1337e81cef5dSAl Viro 			     struct iov_iter *iter,
133826e49cfcSKent Overstreet 			     gfp_t gfp_mask)
1339f9c78b2bSJens Axboe {
134026e49cfcSKent Overstreet 	int j;
1341f9c78b2bSJens Axboe 	struct bio *bio;
1342076098e5SAl Viro 	int ret;
1343f9c78b2bSJens Axboe 
1344b282cc76SAl Viro 	if (!iov_iter_count(iter))
1345f9c78b2bSJens Axboe 		return ERR_PTR(-EINVAL);
1346f9c78b2bSJens Axboe 
1347b282cc76SAl Viro 	bio = bio_kmalloc(gfp_mask, iov_iter_npages(iter, BIO_MAX_PAGES));
1348f9c78b2bSJens Axboe 	if (!bio)
1349f9c78b2bSJens Axboe 		return ERR_PTR(-ENOMEM);
1350f9c78b2bSJens Axboe 
13510a0f1513SAl Viro 	while (iov_iter_count(iter)) {
1352629e42bcSAl Viro 		struct page **pages;
1353076098e5SAl Viro 		ssize_t bytes;
1354076098e5SAl Viro 		size_t offs, added = 0;
1355076098e5SAl Viro 		int npages;
1356f9c78b2bSJens Axboe 
13570a0f1513SAl Viro 		bytes = iov_iter_get_pages_alloc(iter, &pages, LONG_MAX, &offs);
1358076098e5SAl Viro 		if (unlikely(bytes <= 0)) {
1359076098e5SAl Viro 			ret = bytes ? bytes : -EFAULT;
1360f9c78b2bSJens Axboe 			goto out_unmap;
1361f9c78b2bSJens Axboe 		}
1362f9c78b2bSJens Axboe 
1363076098e5SAl Viro 		npages = DIV_ROUND_UP(offs + bytes, PAGE_SIZE);
1364076098e5SAl Viro 
136598f0bc99SAl Viro 		if (unlikely(offs & queue_dma_alignment(q))) {
136698f0bc99SAl Viro 			ret = -EINVAL;
136798f0bc99SAl Viro 			j = 0;
136898f0bc99SAl Viro 		} else {
1369629e42bcSAl Viro 			for (j = 0; j < npages; j++) {
137098f0bc99SAl Viro 				struct page *page = pages[j];
1371076098e5SAl Viro 				unsigned int n = PAGE_SIZE - offs;
1372d1916c86SChristoph Hellwig 				bool same_page = false;
1373f9c78b2bSJens Axboe 
1374076098e5SAl Viro 				if (n > bytes)
1375076098e5SAl Viro 					n = bytes;
1376f9c78b2bSJens Axboe 
137719047087SMing Lei 				if (!__bio_add_pc_page(q, bio, page, n, offs,
1378d1916c86SChristoph Hellwig 						&same_page)) {
1379d1916c86SChristoph Hellwig 					if (same_page)
1380d1916c86SChristoph Hellwig 						put_page(page);
1381f9c78b2bSJens Axboe 					break;
1382d1916c86SChristoph Hellwig 				}
1383f9c78b2bSJens Axboe 
1384076098e5SAl Viro 				added += n;
1385076098e5SAl Viro 				bytes -= n;
1386076098e5SAl Viro 				offs = 0;
1387f9c78b2bSJens Axboe 			}
13880a0f1513SAl Viro 			iov_iter_advance(iter, added);
138998f0bc99SAl Viro 		}
1390f9c78b2bSJens Axboe 		/*
1391f9c78b2bSJens Axboe 		 * release the pages we didn't map into the bio, if any
1392f9c78b2bSJens Axboe 		 */
1393629e42bcSAl Viro 		while (j < npages)
139409cbfeafSKirill A. Shutemov 			put_page(pages[j++]);
1395629e42bcSAl Viro 		kvfree(pages);
1396e2e115d1SAl Viro 		/* couldn't stuff something into bio? */
1397e2e115d1SAl Viro 		if (bytes)
1398e2e115d1SAl Viro 			break;
1399f9c78b2bSJens Axboe 	}
1400f9c78b2bSJens Axboe 
1401b7c44ed9SJens Axboe 	bio_set_flag(bio, BIO_USER_MAPPED);
140237f19e57SChristoph Hellwig 
140337f19e57SChristoph Hellwig 	/*
14045fad1b64SBart Van Assche 	 * subtle -- if bio_map_user_iov() ended up bouncing a bio,
140537f19e57SChristoph Hellwig 	 * it would normally disappear when its bi_end_io is run.
140637f19e57SChristoph Hellwig 	 * however, we need it for the unmap, so grab an extra
140737f19e57SChristoph Hellwig 	 * reference to it
140837f19e57SChristoph Hellwig 	 */
140937f19e57SChristoph Hellwig 	bio_get(bio);
1410f9c78b2bSJens Axboe 	return bio;
1411f9c78b2bSJens Axboe 
1412f9c78b2bSJens Axboe  out_unmap:
1413506e0798SChristoph Hellwig 	bio_release_pages(bio, false);
1414f9c78b2bSJens Axboe 	bio_put(bio);
1415f9c78b2bSJens Axboe 	return ERR_PTR(ret);
1416f9c78b2bSJens Axboe }
1417f9c78b2bSJens Axboe 
1418f9c78b2bSJens Axboe /**
1419f9c78b2bSJens Axboe  *	bio_unmap_user	-	unmap a bio
1420f9c78b2bSJens Axboe  *	@bio:		the bio being unmapped
1421f9c78b2bSJens Axboe  *
14225fad1b64SBart Van Assche  *	Unmap a bio previously mapped by bio_map_user_iov(). Must be called from
14235fad1b64SBart Van Assche  *	process context.
1424f9c78b2bSJens Axboe  *
1425f9c78b2bSJens Axboe  *	bio_unmap_user() may sleep.
1426f9c78b2bSJens Axboe  */
1427f9c78b2bSJens Axboe void bio_unmap_user(struct bio *bio)
1428f9c78b2bSJens Axboe {
1429163cc2d3SChristoph Hellwig 	bio_release_pages(bio, bio_data_dir(bio) == READ);
1430163cc2d3SChristoph Hellwig 	bio_put(bio);
1431f9c78b2bSJens Axboe 	bio_put(bio);
1432f9c78b2bSJens Axboe }
1433f9c78b2bSJens Axboe 
1434b4c5875dSDamien Le Moal static void bio_invalidate_vmalloc_pages(struct bio *bio)
1435b4c5875dSDamien Le Moal {
1436b4c5875dSDamien Le Moal #ifdef ARCH_HAS_FLUSH_KERNEL_DCACHE_PAGE
1437b4c5875dSDamien Le Moal 	if (bio->bi_private && !op_is_write(bio_op(bio))) {
1438b4c5875dSDamien Le Moal 		unsigned long i, len = 0;
1439b4c5875dSDamien Le Moal 
1440b4c5875dSDamien Le Moal 		for (i = 0; i < bio->bi_vcnt; i++)
1441b4c5875dSDamien Le Moal 			len += bio->bi_io_vec[i].bv_len;
1442b4c5875dSDamien Le Moal 		invalidate_kernel_vmap_range(bio->bi_private, len);
1443b4c5875dSDamien Le Moal 	}
1444b4c5875dSDamien Le Moal #endif
1445b4c5875dSDamien Le Moal }
1446b4c5875dSDamien Le Moal 
14474246a0b6SChristoph Hellwig static void bio_map_kern_endio(struct bio *bio)
1448f9c78b2bSJens Axboe {
1449b4c5875dSDamien Le Moal 	bio_invalidate_vmalloc_pages(bio);
1450f9c78b2bSJens Axboe 	bio_put(bio);
1451f9c78b2bSJens Axboe }
1452f9c78b2bSJens Axboe 
145375c72b83SChristoph Hellwig /**
145475c72b83SChristoph Hellwig  *	bio_map_kern	-	map kernel address into bio
145575c72b83SChristoph Hellwig  *	@q: the struct request_queue for the bio
145675c72b83SChristoph Hellwig  *	@data: pointer to buffer to map
145775c72b83SChristoph Hellwig  *	@len: length in bytes
145875c72b83SChristoph Hellwig  *	@gfp_mask: allocation flags for bio allocation
145975c72b83SChristoph Hellwig  *
146075c72b83SChristoph Hellwig  *	Map the kernel address into a bio suitable for io to a block
146175c72b83SChristoph Hellwig  *	device. Returns an error pointer in case of error.
146275c72b83SChristoph Hellwig  */
146375c72b83SChristoph Hellwig struct bio *bio_map_kern(struct request_queue *q, void *data, unsigned int len,
146475c72b83SChristoph Hellwig 			 gfp_t gfp_mask)
1465f9c78b2bSJens Axboe {
1466f9c78b2bSJens Axboe 	unsigned long kaddr = (unsigned long)data;
1467f9c78b2bSJens Axboe 	unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1468f9c78b2bSJens Axboe 	unsigned long start = kaddr >> PAGE_SHIFT;
1469f9c78b2bSJens Axboe 	const int nr_pages = end - start;
1470b4c5875dSDamien Le Moal 	bool is_vmalloc = is_vmalloc_addr(data);
1471b4c5875dSDamien Le Moal 	struct page *page;
1472f9c78b2bSJens Axboe 	int offset, i;
1473f9c78b2bSJens Axboe 	struct bio *bio;
1474f9c78b2bSJens Axboe 
1475f9c78b2bSJens Axboe 	bio = bio_kmalloc(gfp_mask, nr_pages);
1476f9c78b2bSJens Axboe 	if (!bio)
1477f9c78b2bSJens Axboe 		return ERR_PTR(-ENOMEM);
1478f9c78b2bSJens Axboe 
1479b4c5875dSDamien Le Moal 	if (is_vmalloc) {
1480b4c5875dSDamien Le Moal 		flush_kernel_vmap_range(data, len);
1481b4c5875dSDamien Le Moal 		bio->bi_private = data;
1482b4c5875dSDamien Le Moal 	}
1483b4c5875dSDamien Le Moal 
1484f9c78b2bSJens Axboe 	offset = offset_in_page(kaddr);
1485f9c78b2bSJens Axboe 	for (i = 0; i < nr_pages; i++) {
1486f9c78b2bSJens Axboe 		unsigned int bytes = PAGE_SIZE - offset;
1487f9c78b2bSJens Axboe 
1488f9c78b2bSJens Axboe 		if (len <= 0)
1489f9c78b2bSJens Axboe 			break;
1490f9c78b2bSJens Axboe 
1491f9c78b2bSJens Axboe 		if (bytes > len)
1492f9c78b2bSJens Axboe 			bytes = len;
1493f9c78b2bSJens Axboe 
1494b4c5875dSDamien Le Moal 		if (!is_vmalloc)
1495b4c5875dSDamien Le Moal 			page = virt_to_page(data);
1496b4c5875dSDamien Le Moal 		else
1497b4c5875dSDamien Le Moal 			page = vmalloc_to_page(data);
1498b4c5875dSDamien Le Moal 		if (bio_add_pc_page(q, bio, page, bytes,
149975c72b83SChristoph Hellwig 				    offset) < bytes) {
150075c72b83SChristoph Hellwig 			/* we don't support partial mappings */
150175c72b83SChristoph Hellwig 			bio_put(bio);
150275c72b83SChristoph Hellwig 			return ERR_PTR(-EINVAL);
150375c72b83SChristoph Hellwig 		}
1504f9c78b2bSJens Axboe 
1505f9c78b2bSJens Axboe 		data += bytes;
1506f9c78b2bSJens Axboe 		len -= bytes;
1507f9c78b2bSJens Axboe 		offset = 0;
1508f9c78b2bSJens Axboe 	}
1509f9c78b2bSJens Axboe 
1510f9c78b2bSJens Axboe 	bio->bi_end_io = bio_map_kern_endio;
1511f9c78b2bSJens Axboe 	return bio;
1512f9c78b2bSJens Axboe }
1513f9c78b2bSJens Axboe 
15144246a0b6SChristoph Hellwig static void bio_copy_kern_endio(struct bio *bio)
1515f9c78b2bSJens Axboe {
15161dfa0f68SChristoph Hellwig 	bio_free_pages(bio);
15171dfa0f68SChristoph Hellwig 	bio_put(bio);
15181dfa0f68SChristoph Hellwig }
15191dfa0f68SChristoph Hellwig 
15204246a0b6SChristoph Hellwig static void bio_copy_kern_endio_read(struct bio *bio)
15211dfa0f68SChristoph Hellwig {
152242d2683aSChristoph Hellwig 	char *p = bio->bi_private;
15231dfa0f68SChristoph Hellwig 	struct bio_vec *bvec;
15246dc4f100SMing Lei 	struct bvec_iter_all iter_all;
1525f9c78b2bSJens Axboe 
15262b070cfeSChristoph Hellwig 	bio_for_each_segment_all(bvec, bio, iter_all) {
15271dfa0f68SChristoph Hellwig 		memcpy(p, page_address(bvec->bv_page), bvec->bv_len);
1528f9c78b2bSJens Axboe 		p += bvec->bv_len;
1529f9c78b2bSJens Axboe 	}
1530f9c78b2bSJens Axboe 
15314246a0b6SChristoph Hellwig 	bio_copy_kern_endio(bio);
1532f9c78b2bSJens Axboe }
1533f9c78b2bSJens Axboe 
1534f9c78b2bSJens Axboe /**
1535f9c78b2bSJens Axboe  *	bio_copy_kern	-	copy kernel address into bio
1536f9c78b2bSJens Axboe  *	@q: the struct request_queue for the bio
1537f9c78b2bSJens Axboe  *	@data: pointer to buffer to copy
1538f9c78b2bSJens Axboe  *	@len: length in bytes
1539f9c78b2bSJens Axboe  *	@gfp_mask: allocation flags for bio and page allocation
1540f9c78b2bSJens Axboe  *	@reading: data direction is READ
1541f9c78b2bSJens Axboe  *
1542f9c78b2bSJens Axboe  *	copy the kernel address into a bio suitable for io to a block
1543f9c78b2bSJens Axboe  *	device. Returns an error pointer in case of error.
1544f9c78b2bSJens Axboe  */
1545f9c78b2bSJens Axboe struct bio *bio_copy_kern(struct request_queue *q, void *data, unsigned int len,
1546f9c78b2bSJens Axboe 			  gfp_t gfp_mask, int reading)
1547f9c78b2bSJens Axboe {
154842d2683aSChristoph Hellwig 	unsigned long kaddr = (unsigned long)data;
154942d2683aSChristoph Hellwig 	unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
155042d2683aSChristoph Hellwig 	unsigned long start = kaddr >> PAGE_SHIFT;
155142d2683aSChristoph Hellwig 	struct bio *bio;
1552f9c78b2bSJens Axboe 	void *p = data;
15531dfa0f68SChristoph Hellwig 	int nr_pages = 0;
1554f9c78b2bSJens Axboe 
155542d2683aSChristoph Hellwig 	/*
155642d2683aSChristoph Hellwig 	 * Overflow, abort
155742d2683aSChristoph Hellwig 	 */
155842d2683aSChristoph Hellwig 	if (end < start)
155942d2683aSChristoph Hellwig 		return ERR_PTR(-EINVAL);
1560f9c78b2bSJens Axboe 
156142d2683aSChristoph Hellwig 	nr_pages = end - start;
156242d2683aSChristoph Hellwig 	bio = bio_kmalloc(gfp_mask, nr_pages);
156342d2683aSChristoph Hellwig 	if (!bio)
156442d2683aSChristoph Hellwig 		return ERR_PTR(-ENOMEM);
156542d2683aSChristoph Hellwig 
156642d2683aSChristoph Hellwig 	while (len) {
156742d2683aSChristoph Hellwig 		struct page *page;
156842d2683aSChristoph Hellwig 		unsigned int bytes = PAGE_SIZE;
156942d2683aSChristoph Hellwig 
157042d2683aSChristoph Hellwig 		if (bytes > len)
157142d2683aSChristoph Hellwig 			bytes = len;
157242d2683aSChristoph Hellwig 
157342d2683aSChristoph Hellwig 		page = alloc_page(q->bounce_gfp | gfp_mask);
157442d2683aSChristoph Hellwig 		if (!page)
157542d2683aSChristoph Hellwig 			goto cleanup;
157642d2683aSChristoph Hellwig 
157742d2683aSChristoph Hellwig 		if (!reading)
157842d2683aSChristoph Hellwig 			memcpy(page_address(page), p, bytes);
157942d2683aSChristoph Hellwig 
158042d2683aSChristoph Hellwig 		if (bio_add_pc_page(q, bio, page, bytes, 0) < bytes)
158142d2683aSChristoph Hellwig 			break;
158242d2683aSChristoph Hellwig 
158342d2683aSChristoph Hellwig 		len -= bytes;
158442d2683aSChristoph Hellwig 		p += bytes;
1585f9c78b2bSJens Axboe 	}
1586f9c78b2bSJens Axboe 
15871dfa0f68SChristoph Hellwig 	if (reading) {
15881dfa0f68SChristoph Hellwig 		bio->bi_end_io = bio_copy_kern_endio_read;
158942d2683aSChristoph Hellwig 		bio->bi_private = data;
15901dfa0f68SChristoph Hellwig 	} else {
1591f9c78b2bSJens Axboe 		bio->bi_end_io = bio_copy_kern_endio;
15921dfa0f68SChristoph Hellwig 	}
15931dfa0f68SChristoph Hellwig 
1594f9c78b2bSJens Axboe 	return bio;
159542d2683aSChristoph Hellwig 
159642d2683aSChristoph Hellwig cleanup:
15971dfa0f68SChristoph Hellwig 	bio_free_pages(bio);
159842d2683aSChristoph Hellwig 	bio_put(bio);
159942d2683aSChristoph Hellwig 	return ERR_PTR(-ENOMEM);
1600f9c78b2bSJens Axboe }
1601f9c78b2bSJens Axboe 
1602f9c78b2bSJens Axboe /*
1603f9c78b2bSJens Axboe  * bio_set_pages_dirty() and bio_check_pages_dirty() are support functions
1604f9c78b2bSJens Axboe  * for performing direct-IO in BIOs.
1605f9c78b2bSJens Axboe  *
1606f9c78b2bSJens Axboe  * The problem is that we cannot run set_page_dirty() from interrupt context
1607f9c78b2bSJens Axboe  * because the required locks are not interrupt-safe.  So what we can do is to
1608f9c78b2bSJens Axboe  * mark the pages dirty _before_ performing IO.  And in interrupt context,
1609f9c78b2bSJens Axboe  * check that the pages are still dirty.   If so, fine.  If not, redirty them
1610f9c78b2bSJens Axboe  * in process context.
1611f9c78b2bSJens Axboe  *
1612f9c78b2bSJens Axboe  * We special-case compound pages here: normally this means reads into hugetlb
1613f9c78b2bSJens Axboe  * pages.  The logic in here doesn't really work right for compound pages
1614f9c78b2bSJens Axboe  * because the VM does not uniformly chase down the head page in all cases.
1615f9c78b2bSJens Axboe  * But dirtiness of compound pages is pretty meaningless anyway: the VM doesn't
1616f9c78b2bSJens Axboe  * handle them at all.  So we skip compound pages here at an early stage.
1617f9c78b2bSJens Axboe  *
1618f9c78b2bSJens Axboe  * Note that this code is very hard to test under normal circumstances because
1619f9c78b2bSJens Axboe  * direct-io pins the pages with get_user_pages().  This makes
1620f9c78b2bSJens Axboe  * is_page_cache_freeable return false, and the VM will not clean the pages.
1621f9c78b2bSJens Axboe  * But other code (eg, flusher threads) could clean the pages if they are mapped
1622f9c78b2bSJens Axboe  * pagecache.
1623f9c78b2bSJens Axboe  *
1624f9c78b2bSJens Axboe  * Simply disabling the call to bio_set_pages_dirty() is a good way to test the
1625f9c78b2bSJens Axboe  * deferred bio dirtying paths.
1626f9c78b2bSJens Axboe  */
1627f9c78b2bSJens Axboe 
1628f9c78b2bSJens Axboe /*
1629f9c78b2bSJens Axboe  * bio_set_pages_dirty() will mark all the bio's pages as dirty.
1630f9c78b2bSJens Axboe  */
1631f9c78b2bSJens Axboe void bio_set_pages_dirty(struct bio *bio)
1632f9c78b2bSJens Axboe {
1633f9c78b2bSJens Axboe 	struct bio_vec *bvec;
16346dc4f100SMing Lei 	struct bvec_iter_all iter_all;
1635f9c78b2bSJens Axboe 
16362b070cfeSChristoph Hellwig 	bio_for_each_segment_all(bvec, bio, iter_all) {
16373bb50983SChristoph Hellwig 		if (!PageCompound(bvec->bv_page))
16383bb50983SChristoph Hellwig 			set_page_dirty_lock(bvec->bv_page);
1639f9c78b2bSJens Axboe 	}
1640f9c78b2bSJens Axboe }
1641f9c78b2bSJens Axboe 
1642f9c78b2bSJens Axboe /*
1643f9c78b2bSJens Axboe  * bio_check_pages_dirty() will check that all the BIO's pages are still dirty.
1644f9c78b2bSJens Axboe  * If they are, then fine.  If, however, some pages are clean then they must
1645f9c78b2bSJens Axboe  * have been written out during the direct-IO read.  So we take another ref on
164624d5493fSChristoph Hellwig  * the BIO and re-dirty the pages in process context.
1647f9c78b2bSJens Axboe  *
1648f9c78b2bSJens Axboe  * It is expected that bio_check_pages_dirty() will wholly own the BIO from
1649ea1754a0SKirill A. Shutemov  * here on.  It will run one put_page() against each page and will run one
1650ea1754a0SKirill A. Shutemov  * bio_put() against the BIO.
1651f9c78b2bSJens Axboe  */
1652f9c78b2bSJens Axboe 
1653f9c78b2bSJens Axboe static void bio_dirty_fn(struct work_struct *work);
1654f9c78b2bSJens Axboe 
1655f9c78b2bSJens Axboe static DECLARE_WORK(bio_dirty_work, bio_dirty_fn);
1656f9c78b2bSJens Axboe static DEFINE_SPINLOCK(bio_dirty_lock);
1657f9c78b2bSJens Axboe static struct bio *bio_dirty_list;
1658f9c78b2bSJens Axboe 
1659f9c78b2bSJens Axboe /*
1660f9c78b2bSJens Axboe  * This runs in process context
1661f9c78b2bSJens Axboe  */
1662f9c78b2bSJens Axboe static void bio_dirty_fn(struct work_struct *work)
1663f9c78b2bSJens Axboe {
166424d5493fSChristoph Hellwig 	struct bio *bio, *next;
1665f9c78b2bSJens Axboe 
166624d5493fSChristoph Hellwig 	spin_lock_irq(&bio_dirty_lock);
166724d5493fSChristoph Hellwig 	next = bio_dirty_list;
1668f9c78b2bSJens Axboe 	bio_dirty_list = NULL;
166924d5493fSChristoph Hellwig 	spin_unlock_irq(&bio_dirty_lock);
1670f9c78b2bSJens Axboe 
167124d5493fSChristoph Hellwig 	while ((bio = next) != NULL) {
167224d5493fSChristoph Hellwig 		next = bio->bi_private;
1673f9c78b2bSJens Axboe 
1674d241a95fSChristoph Hellwig 		bio_release_pages(bio, true);
1675f9c78b2bSJens Axboe 		bio_put(bio);
1676f9c78b2bSJens Axboe 	}
1677f9c78b2bSJens Axboe }
1678f9c78b2bSJens Axboe 
1679f9c78b2bSJens Axboe void bio_check_pages_dirty(struct bio *bio)
1680f9c78b2bSJens Axboe {
1681f9c78b2bSJens Axboe 	struct bio_vec *bvec;
168224d5493fSChristoph Hellwig 	unsigned long flags;
16836dc4f100SMing Lei 	struct bvec_iter_all iter_all;
1684f9c78b2bSJens Axboe 
16852b070cfeSChristoph Hellwig 	bio_for_each_segment_all(bvec, bio, iter_all) {
168624d5493fSChristoph Hellwig 		if (!PageDirty(bvec->bv_page) && !PageCompound(bvec->bv_page))
168724d5493fSChristoph Hellwig 			goto defer;
1688f9c78b2bSJens Axboe 	}
1689f9c78b2bSJens Axboe 
1690d241a95fSChristoph Hellwig 	bio_release_pages(bio, false);
169124d5493fSChristoph Hellwig 	bio_put(bio);
169224d5493fSChristoph Hellwig 	return;
169324d5493fSChristoph Hellwig defer:
1694f9c78b2bSJens Axboe 	spin_lock_irqsave(&bio_dirty_lock, flags);
1695f9c78b2bSJens Axboe 	bio->bi_private = bio_dirty_list;
1696f9c78b2bSJens Axboe 	bio_dirty_list = bio;
1697f9c78b2bSJens Axboe 	spin_unlock_irqrestore(&bio_dirty_lock, flags);
1698f9c78b2bSJens Axboe 	schedule_work(&bio_dirty_work);
1699f9c78b2bSJens Axboe }
1700f9c78b2bSJens Axboe 
17015b18b5a7SMikulas Patocka void update_io_ticks(struct hd_struct *part, unsigned long now)
17025b18b5a7SMikulas Patocka {
17035b18b5a7SMikulas Patocka 	unsigned long stamp;
17045b18b5a7SMikulas Patocka again:
17055b18b5a7SMikulas Patocka 	stamp = READ_ONCE(part->stamp);
17065b18b5a7SMikulas Patocka 	if (unlikely(stamp != now)) {
17075b18b5a7SMikulas Patocka 		if (likely(cmpxchg(&part->stamp, stamp, now) == stamp)) {
17085b18b5a7SMikulas Patocka 			__part_stat_add(part, io_ticks, 1);
17095b18b5a7SMikulas Patocka 		}
17105b18b5a7SMikulas Patocka 	}
17115b18b5a7SMikulas Patocka 	if (part->partno) {
17125b18b5a7SMikulas Patocka 		part = &part_to_disk(part)->part0;
17135b18b5a7SMikulas Patocka 		goto again;
17145b18b5a7SMikulas Patocka 	}
17155b18b5a7SMikulas Patocka }
1716f9c78b2bSJens Axboe 
1717ddcf35d3SMichael Callahan void generic_start_io_acct(struct request_queue *q, int op,
1718d62e26b3SJens Axboe 			   unsigned long sectors, struct hd_struct *part)
1719394ffa50SGu Zheng {
1720ddcf35d3SMichael Callahan 	const int sgrp = op_stat_group(op);
1721394ffa50SGu Zheng 
1722112f158fSMike Snitzer 	part_stat_lock();
1723112f158fSMike Snitzer 
17245b18b5a7SMikulas Patocka 	update_io_ticks(part, jiffies);
1725112f158fSMike Snitzer 	part_stat_inc(part, ios[sgrp]);
1726112f158fSMike Snitzer 	part_stat_add(part, sectors[sgrp], sectors);
1727ddcf35d3SMichael Callahan 	part_inc_in_flight(q, part, op_is_write(op));
1728394ffa50SGu Zheng 
1729394ffa50SGu Zheng 	part_stat_unlock();
1730394ffa50SGu Zheng }
1731394ffa50SGu Zheng EXPORT_SYMBOL(generic_start_io_acct);
1732394ffa50SGu Zheng 
1733ddcf35d3SMichael Callahan void generic_end_io_acct(struct request_queue *q, int req_op,
1734d62e26b3SJens Axboe 			 struct hd_struct *part, unsigned long start_time)
1735394ffa50SGu Zheng {
17365b18b5a7SMikulas Patocka 	unsigned long now = jiffies;
17375b18b5a7SMikulas Patocka 	unsigned long duration = now - start_time;
1738ddcf35d3SMichael Callahan 	const int sgrp = op_stat_group(req_op);
1739394ffa50SGu Zheng 
1740112f158fSMike Snitzer 	part_stat_lock();
1741112f158fSMike Snitzer 
17425b18b5a7SMikulas Patocka 	update_io_ticks(part, now);
1743112f158fSMike Snitzer 	part_stat_add(part, nsecs[sgrp], jiffies_to_nsecs(duration));
17445b18b5a7SMikulas Patocka 	part_stat_add(part, time_in_queue, duration);
1745ddcf35d3SMichael Callahan 	part_dec_in_flight(q, part, op_is_write(req_op));
1746394ffa50SGu Zheng 
1747394ffa50SGu Zheng 	part_stat_unlock();
1748394ffa50SGu Zheng }
1749394ffa50SGu Zheng EXPORT_SYMBOL(generic_end_io_acct);
1750394ffa50SGu Zheng 
1751c4cf5261SJens Axboe static inline bool bio_remaining_done(struct bio *bio)
1752c4cf5261SJens Axboe {
1753c4cf5261SJens Axboe 	/*
1754c4cf5261SJens Axboe 	 * If we're not chaining, then ->__bi_remaining is always 1 and
1755c4cf5261SJens Axboe 	 * we always end io on the first invocation.
1756c4cf5261SJens Axboe 	 */
1757c4cf5261SJens Axboe 	if (!bio_flagged(bio, BIO_CHAIN))
1758c4cf5261SJens Axboe 		return true;
1759c4cf5261SJens Axboe 
1760c4cf5261SJens Axboe 	BUG_ON(atomic_read(&bio->__bi_remaining) <= 0);
1761c4cf5261SJens Axboe 
1762326e1dbbSMike Snitzer 	if (atomic_dec_and_test(&bio->__bi_remaining)) {
1763b7c44ed9SJens Axboe 		bio_clear_flag(bio, BIO_CHAIN);
1764c4cf5261SJens Axboe 		return true;
1765326e1dbbSMike Snitzer 	}
1766c4cf5261SJens Axboe 
1767c4cf5261SJens Axboe 	return false;
1768c4cf5261SJens Axboe }
1769c4cf5261SJens Axboe 
1770f9c78b2bSJens Axboe /**
1771f9c78b2bSJens Axboe  * bio_endio - end I/O on a bio
1772f9c78b2bSJens Axboe  * @bio:	bio
1773f9c78b2bSJens Axboe  *
1774f9c78b2bSJens Axboe  * Description:
17754246a0b6SChristoph Hellwig  *   bio_endio() will end I/O on the whole bio. bio_endio() is the preferred
17764246a0b6SChristoph Hellwig  *   way to end I/O on a bio. No one should call bi_end_io() directly on a
17774246a0b6SChristoph Hellwig  *   bio unless they own it and thus know that it has an end_io function.
1778fbbaf700SNeilBrown  *
1779fbbaf700SNeilBrown  *   bio_endio() can be called several times on a bio that has been chained
1780fbbaf700SNeilBrown  *   using bio_chain().  The ->bi_end_io() function will only be called the
1781fbbaf700SNeilBrown  *   last time.  At this point the BLK_TA_COMPLETE tracing event will be
1782fbbaf700SNeilBrown  *   generated if BIO_TRACE_COMPLETION is set.
1783f9c78b2bSJens Axboe  **/
17844246a0b6SChristoph Hellwig void bio_endio(struct bio *bio)
1785f9c78b2bSJens Axboe {
1786ba8c6967SChristoph Hellwig again:
17872b885517SChristoph Hellwig 	if (!bio_remaining_done(bio))
1788ba8c6967SChristoph Hellwig 		return;
17897c20f116SChristoph Hellwig 	if (!bio_integrity_endio(bio))
17907c20f116SChristoph Hellwig 		return;
1791f9c78b2bSJens Axboe 
179267b42d0bSJosef Bacik 	if (bio->bi_disk)
179367b42d0bSJosef Bacik 		rq_qos_done_bio(bio->bi_disk->queue, bio);
179467b42d0bSJosef Bacik 
1795f9c78b2bSJens Axboe 	/*
1796ba8c6967SChristoph Hellwig 	 * Need to have a real endio function for chained bios, otherwise
1797ba8c6967SChristoph Hellwig 	 * various corner cases will break (like stacking block devices that
1798ba8c6967SChristoph Hellwig 	 * save/restore bi_end_io) - however, we want to avoid unbounded
1799ba8c6967SChristoph Hellwig 	 * recursion and blowing the stack. Tail call optimization would
1800ba8c6967SChristoph Hellwig 	 * handle this, but compiling with frame pointers also disables
1801ba8c6967SChristoph Hellwig 	 * gcc's sibling call optimization.
1802f9c78b2bSJens Axboe 	 */
1803f9c78b2bSJens Axboe 	if (bio->bi_end_io == bio_chain_endio) {
180438f8baaeSChristoph Hellwig 		bio = __bio_chain_endio(bio);
1805ba8c6967SChristoph Hellwig 		goto again;
1806ba8c6967SChristoph Hellwig 	}
1807ba8c6967SChristoph Hellwig 
180874d46992SChristoph Hellwig 	if (bio->bi_disk && bio_flagged(bio, BIO_TRACE_COMPLETION)) {
180974d46992SChristoph Hellwig 		trace_block_bio_complete(bio->bi_disk->queue, bio,
1810a462b950SBart Van Assche 					 blk_status_to_errno(bio->bi_status));
1811fbbaf700SNeilBrown 		bio_clear_flag(bio, BIO_TRACE_COMPLETION);
1812fbbaf700SNeilBrown 	}
1813fbbaf700SNeilBrown 
18149e234eeaSShaohua Li 	blk_throtl_bio_endio(bio);
1815b222dd2fSShaohua Li 	/* release cgroup info */
1816b222dd2fSShaohua Li 	bio_uninit(bio);
1817f9c78b2bSJens Axboe 	if (bio->bi_end_io)
18184246a0b6SChristoph Hellwig 		bio->bi_end_io(bio);
1819f9c78b2bSJens Axboe }
1820f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_endio);
1821f9c78b2bSJens Axboe 
1822f9c78b2bSJens Axboe /**
1823f9c78b2bSJens Axboe  * bio_split - split a bio
1824f9c78b2bSJens Axboe  * @bio:	bio to split
1825f9c78b2bSJens Axboe  * @sectors:	number of sectors to split from the front of @bio
1826f9c78b2bSJens Axboe  * @gfp:	gfp mask
1827f9c78b2bSJens Axboe  * @bs:		bio set to allocate from
1828f9c78b2bSJens Axboe  *
1829f9c78b2bSJens Axboe  * Allocates and returns a new bio which represents @sectors from the start of
1830f9c78b2bSJens Axboe  * @bio, and updates @bio to represent the remaining sectors.
1831f9c78b2bSJens Axboe  *
1832f3f5da62SMartin K. Petersen  * Unless this is a discard request the newly allocated bio will point
1833dad77584SBart Van Assche  * to @bio's bi_io_vec. It is the caller's responsibility to ensure that
1834dad77584SBart Van Assche  * neither @bio nor @bs are freed before the split bio.
1835f9c78b2bSJens Axboe  */
1836f9c78b2bSJens Axboe struct bio *bio_split(struct bio *bio, int sectors,
1837f9c78b2bSJens Axboe 		      gfp_t gfp, struct bio_set *bs)
1838f9c78b2bSJens Axboe {
1839f341a4d3SMikulas Patocka 	struct bio *split;
1840f9c78b2bSJens Axboe 
1841f9c78b2bSJens Axboe 	BUG_ON(sectors <= 0);
1842f9c78b2bSJens Axboe 	BUG_ON(sectors >= bio_sectors(bio));
1843f9c78b2bSJens Axboe 
1844f9c78b2bSJens Axboe 	split = bio_clone_fast(bio, gfp, bs);
1845f9c78b2bSJens Axboe 	if (!split)
1846f9c78b2bSJens Axboe 		return NULL;
1847f9c78b2bSJens Axboe 
1848f9c78b2bSJens Axboe 	split->bi_iter.bi_size = sectors << 9;
1849f9c78b2bSJens Axboe 
1850f9c78b2bSJens Axboe 	if (bio_integrity(split))
1851fbd08e76SDmitry Monakhov 		bio_integrity_trim(split);
1852f9c78b2bSJens Axboe 
1853f9c78b2bSJens Axboe 	bio_advance(bio, split->bi_iter.bi_size);
1854f9c78b2bSJens Axboe 
1855fbbaf700SNeilBrown 	if (bio_flagged(bio, BIO_TRACE_COMPLETION))
185620d59023SGoldwyn Rodrigues 		bio_set_flag(split, BIO_TRACE_COMPLETION);
1857fbbaf700SNeilBrown 
1858f9c78b2bSJens Axboe 	return split;
1859f9c78b2bSJens Axboe }
1860f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_split);
1861f9c78b2bSJens Axboe 
1862f9c78b2bSJens Axboe /**
1863f9c78b2bSJens Axboe  * bio_trim - trim a bio
1864f9c78b2bSJens Axboe  * @bio:	bio to trim
1865f9c78b2bSJens Axboe  * @offset:	number of sectors to trim from the front of @bio
1866f9c78b2bSJens Axboe  * @size:	size we want to trim @bio to, in sectors
1867f9c78b2bSJens Axboe  */
1868f9c78b2bSJens Axboe void bio_trim(struct bio *bio, int offset, int size)
1869f9c78b2bSJens Axboe {
1870f9c78b2bSJens Axboe 	/* 'bio' is a cloned bio which we need to trim to match
1871f9c78b2bSJens Axboe 	 * the given offset and size.
1872f9c78b2bSJens Axboe 	 */
1873f9c78b2bSJens Axboe 
1874f9c78b2bSJens Axboe 	size <<= 9;
1875f9c78b2bSJens Axboe 	if (offset == 0 && size == bio->bi_iter.bi_size)
1876f9c78b2bSJens Axboe 		return;
1877f9c78b2bSJens Axboe 
1878f9c78b2bSJens Axboe 	bio_advance(bio, offset << 9);
1879f9c78b2bSJens Axboe 	bio->bi_iter.bi_size = size;
1880376a78abSDmitry Monakhov 
1881376a78abSDmitry Monakhov 	if (bio_integrity(bio))
1882fbd08e76SDmitry Monakhov 		bio_integrity_trim(bio);
1883376a78abSDmitry Monakhov 
1884f9c78b2bSJens Axboe }
1885f9c78b2bSJens Axboe EXPORT_SYMBOL_GPL(bio_trim);
1886f9c78b2bSJens Axboe 
1887f9c78b2bSJens Axboe /*
1888f9c78b2bSJens Axboe  * create memory pools for biovec's in a bio_set.
1889f9c78b2bSJens Axboe  * use the global biovec slabs created for general use.
1890f9c78b2bSJens Axboe  */
18918aa6ba2fSKent Overstreet int biovec_init_pool(mempool_t *pool, int pool_entries)
1892f9c78b2bSJens Axboe {
1893ed996a52SChristoph Hellwig 	struct biovec_slab *bp = bvec_slabs + BVEC_POOL_MAX;
1894f9c78b2bSJens Axboe 
18958aa6ba2fSKent Overstreet 	return mempool_init_slab_pool(pool, pool_entries, bp->slab);
1896f9c78b2bSJens Axboe }
1897f9c78b2bSJens Axboe 
1898917a38c7SKent Overstreet /*
1899917a38c7SKent Overstreet  * bioset_exit - exit a bioset initialized with bioset_init()
1900917a38c7SKent Overstreet  *
1901917a38c7SKent Overstreet  * May be called on a zeroed but uninitialized bioset (i.e. allocated with
1902917a38c7SKent Overstreet  * kzalloc()).
1903917a38c7SKent Overstreet  */
1904917a38c7SKent Overstreet void bioset_exit(struct bio_set *bs)
1905f9c78b2bSJens Axboe {
1906f9c78b2bSJens Axboe 	if (bs->rescue_workqueue)
1907f9c78b2bSJens Axboe 		destroy_workqueue(bs->rescue_workqueue);
1908917a38c7SKent Overstreet 	bs->rescue_workqueue = NULL;
1909f9c78b2bSJens Axboe 
19108aa6ba2fSKent Overstreet 	mempool_exit(&bs->bio_pool);
19118aa6ba2fSKent Overstreet 	mempool_exit(&bs->bvec_pool);
1912f9c78b2bSJens Axboe 
1913f9c78b2bSJens Axboe 	bioset_integrity_free(bs);
1914917a38c7SKent Overstreet 	if (bs->bio_slab)
1915f9c78b2bSJens Axboe 		bio_put_slab(bs);
1916917a38c7SKent Overstreet 	bs->bio_slab = NULL;
1917917a38c7SKent Overstreet }
1918917a38c7SKent Overstreet EXPORT_SYMBOL(bioset_exit);
1919f9c78b2bSJens Axboe 
1920011067b0SNeilBrown /**
1921917a38c7SKent Overstreet  * bioset_init - Initialize a bio_set
1922dad08527SKent Overstreet  * @bs:		pool to initialize
1923917a38c7SKent Overstreet  * @pool_size:	Number of bio and bio_vecs to cache in the mempool
1924917a38c7SKent Overstreet  * @front_pad:	Number of bytes to allocate in front of the returned bio
1925917a38c7SKent Overstreet  * @flags:	Flags to modify behavior, currently %BIOSET_NEED_BVECS
1926917a38c7SKent Overstreet  *              and %BIOSET_NEED_RESCUER
1927917a38c7SKent Overstreet  *
1928dad08527SKent Overstreet  * Description:
1929dad08527SKent Overstreet  *    Set up a bio_set to be used with @bio_alloc_bioset. Allows the caller
1930dad08527SKent Overstreet  *    to ask for a number of bytes to be allocated in front of the bio.
1931dad08527SKent Overstreet  *    Front pad allocation is useful for embedding the bio inside
1932dad08527SKent Overstreet  *    another structure, to avoid allocating extra data to go with the bio.
1933dad08527SKent Overstreet  *    Note that the bio must be embedded at the END of that structure always,
1934dad08527SKent Overstreet  *    or things will break badly.
1935dad08527SKent Overstreet  *    If %BIOSET_NEED_BVECS is set in @flags, a separate pool will be allocated
1936dad08527SKent Overstreet  *    for allocating iovecs.  This pool is not needed e.g. for bio_clone_fast().
1937dad08527SKent Overstreet  *    If %BIOSET_NEED_RESCUER is set, a workqueue is created which can be used to
1938dad08527SKent Overstreet  *    dispatch queued requests when the mempool runs out of space.
1939dad08527SKent Overstreet  *
1940917a38c7SKent Overstreet  */
1941917a38c7SKent Overstreet int bioset_init(struct bio_set *bs,
1942917a38c7SKent Overstreet 		unsigned int pool_size,
1943917a38c7SKent Overstreet 		unsigned int front_pad,
1944917a38c7SKent Overstreet 		int flags)
1945917a38c7SKent Overstreet {
1946917a38c7SKent Overstreet 	unsigned int back_pad = BIO_INLINE_VECS * sizeof(struct bio_vec);
1947917a38c7SKent Overstreet 
1948917a38c7SKent Overstreet 	bs->front_pad = front_pad;
1949917a38c7SKent Overstreet 
1950917a38c7SKent Overstreet 	spin_lock_init(&bs->rescue_lock);
1951917a38c7SKent Overstreet 	bio_list_init(&bs->rescue_list);
1952917a38c7SKent Overstreet 	INIT_WORK(&bs->rescue_work, bio_alloc_rescue);
1953917a38c7SKent Overstreet 
1954917a38c7SKent Overstreet 	bs->bio_slab = bio_find_or_create_slab(front_pad + back_pad);
1955917a38c7SKent Overstreet 	if (!bs->bio_slab)
1956917a38c7SKent Overstreet 		return -ENOMEM;
1957917a38c7SKent Overstreet 
1958917a38c7SKent Overstreet 	if (mempool_init_slab_pool(&bs->bio_pool, pool_size, bs->bio_slab))
1959917a38c7SKent Overstreet 		goto bad;
1960917a38c7SKent Overstreet 
1961917a38c7SKent Overstreet 	if ((flags & BIOSET_NEED_BVECS) &&
1962917a38c7SKent Overstreet 	    biovec_init_pool(&bs->bvec_pool, pool_size))
1963917a38c7SKent Overstreet 		goto bad;
1964917a38c7SKent Overstreet 
1965917a38c7SKent Overstreet 	if (!(flags & BIOSET_NEED_RESCUER))
1966917a38c7SKent Overstreet 		return 0;
1967917a38c7SKent Overstreet 
1968917a38c7SKent Overstreet 	bs->rescue_workqueue = alloc_workqueue("bioset", WQ_MEM_RECLAIM, 0);
1969917a38c7SKent Overstreet 	if (!bs->rescue_workqueue)
1970917a38c7SKent Overstreet 		goto bad;
1971917a38c7SKent Overstreet 
1972917a38c7SKent Overstreet 	return 0;
1973917a38c7SKent Overstreet bad:
1974917a38c7SKent Overstreet 	bioset_exit(bs);
1975917a38c7SKent Overstreet 	return -ENOMEM;
1976917a38c7SKent Overstreet }
1977917a38c7SKent Overstreet EXPORT_SYMBOL(bioset_init);
1978917a38c7SKent Overstreet 
197928e89fd9SJens Axboe /*
198028e89fd9SJens Axboe  * Initialize and setup a new bio_set, based on the settings from
198128e89fd9SJens Axboe  * another bio_set.
198228e89fd9SJens Axboe  */
198328e89fd9SJens Axboe int bioset_init_from_src(struct bio_set *bs, struct bio_set *src)
198428e89fd9SJens Axboe {
198528e89fd9SJens Axboe 	int flags;
198628e89fd9SJens Axboe 
198728e89fd9SJens Axboe 	flags = 0;
198828e89fd9SJens Axboe 	if (src->bvec_pool.min_nr)
198928e89fd9SJens Axboe 		flags |= BIOSET_NEED_BVECS;
199028e89fd9SJens Axboe 	if (src->rescue_workqueue)
199128e89fd9SJens Axboe 		flags |= BIOSET_NEED_RESCUER;
199228e89fd9SJens Axboe 
199328e89fd9SJens Axboe 	return bioset_init(bs, src->bio_pool.min_nr, src->front_pad, flags);
199428e89fd9SJens Axboe }
199528e89fd9SJens Axboe EXPORT_SYMBOL(bioset_init_from_src);
199628e89fd9SJens Axboe 
1997f9c78b2bSJens Axboe #ifdef CONFIG_BLK_CGROUP
19981d933cf0STejun Heo 
19991d933cf0STejun Heo /**
20002268c0feSDennis Zhou  * bio_disassociate_blkg - puts back the blkg reference if associated
2001b5f2954dSDennis Zhou  * @bio: target bio
2002b5f2954dSDennis Zhou  *
20032268c0feSDennis Zhou  * Helper to disassociate the blkg from @bio if a blkg is associated.
2004b5f2954dSDennis Zhou  */
20052268c0feSDennis Zhou void bio_disassociate_blkg(struct bio *bio)
2006b5f2954dSDennis Zhou {
200708e18eabSJosef Bacik 	if (bio->bi_blkg) {
200808e18eabSJosef Bacik 		blkg_put(bio->bi_blkg);
200908e18eabSJosef Bacik 		bio->bi_blkg = NULL;
201008e18eabSJosef Bacik 	}
2011f9c78b2bSJens Axboe }
2012892ad71fSDennis Zhou EXPORT_SYMBOL_GPL(bio_disassociate_blkg);
2013f9c78b2bSJens Axboe 
201420bd723eSPaolo Valente /**
20152268c0feSDennis Zhou  * __bio_associate_blkg - associate a bio with the a blkg
2016f9c78b2bSJens Axboe  * @bio: target bio
2017f9c78b2bSJens Axboe  * @blkg: the blkg to associate
2018f9c78b2bSJens Axboe  *
2019beea9da0SDennis Zhou  * This tries to associate @bio with the specified @blkg.  Association failure
2020beea9da0SDennis Zhou  * is handled by walking up the blkg tree.  Therefore, the blkg associated can
2021beea9da0SDennis Zhou  * be anything between @blkg and the root_blkg.  This situation only happens
2022beea9da0SDennis Zhou  * when a cgroup is dying and then the remaining bios will spill to the closest
2023beea9da0SDennis Zhou  * alive blkg.
2024beea9da0SDennis Zhou  *
2025beea9da0SDennis Zhou  * A reference will be taken on the @blkg and will be released when @bio is
2026beea9da0SDennis Zhou  * freed.
2027f9c78b2bSJens Axboe  */
20282268c0feSDennis Zhou static void __bio_associate_blkg(struct bio *bio, struct blkcg_gq *blkg)
2029f9c78b2bSJens Axboe {
20302268c0feSDennis Zhou 	bio_disassociate_blkg(bio);
20312268c0feSDennis Zhou 
20327754f669SDennis Zhou 	bio->bi_blkg = blkg_tryget_closest(blkg);
20332268c0feSDennis Zhou }
20342268c0feSDennis Zhou 
2035fd42df30SDennis Zhou /**
2036fd42df30SDennis Zhou  * bio_associate_blkg_from_css - associate a bio with a specified css
2037fd42df30SDennis Zhou  * @bio: target bio
2038fd42df30SDennis Zhou  * @css: target css
2039fd42df30SDennis Zhou  *
2040fd42df30SDennis Zhou  * Associate @bio with the blkg found by combining the css's blkg and the
2041fc5a828bSDennis Zhou  * request_queue of the @bio.  This falls back to the queue's root_blkg if
2042fc5a828bSDennis Zhou  * the association fails with the css.
2043fd42df30SDennis Zhou  */
2044fd42df30SDennis Zhou void bio_associate_blkg_from_css(struct bio *bio,
2045fd42df30SDennis Zhou 				 struct cgroup_subsys_state *css)
2046fd42df30SDennis Zhou {
2047fc5a828bSDennis Zhou 	struct request_queue *q = bio->bi_disk->queue;
2048fc5a828bSDennis Zhou 	struct blkcg_gq *blkg;
2049fc5a828bSDennis Zhou 
2050fc5a828bSDennis Zhou 	rcu_read_lock();
2051fc5a828bSDennis Zhou 
2052fc5a828bSDennis Zhou 	if (!css || !css->parent)
2053fc5a828bSDennis Zhou 		blkg = q->root_blkg;
2054fc5a828bSDennis Zhou 	else
2055fc5a828bSDennis Zhou 		blkg = blkg_lookup_create(css_to_blkcg(css), q);
2056fc5a828bSDennis Zhou 
2057fc5a828bSDennis Zhou 	__bio_associate_blkg(bio, blkg);
2058fc5a828bSDennis Zhou 
2059fc5a828bSDennis Zhou 	rcu_read_unlock();
2060fd42df30SDennis Zhou }
2061fd42df30SDennis Zhou EXPORT_SYMBOL_GPL(bio_associate_blkg_from_css);
2062fd42df30SDennis Zhou 
20636a7f6d86SDennis Zhou #ifdef CONFIG_MEMCG
20646a7f6d86SDennis Zhou /**
20656a7f6d86SDennis Zhou  * bio_associate_blkg_from_page - associate a bio with the page's blkg
20666a7f6d86SDennis Zhou  * @bio: target bio
20676a7f6d86SDennis Zhou  * @page: the page to lookup the blkcg from
20686a7f6d86SDennis Zhou  *
20696a7f6d86SDennis Zhou  * Associate @bio with the blkg from @page's owning memcg and the respective
2070fc5a828bSDennis Zhou  * request_queue.  If cgroup_e_css returns %NULL, fall back to the queue's
2071fc5a828bSDennis Zhou  * root_blkg.
20726a7f6d86SDennis Zhou  */
20736a7f6d86SDennis Zhou void bio_associate_blkg_from_page(struct bio *bio, struct page *page)
20746a7f6d86SDennis Zhou {
20756a7f6d86SDennis Zhou 	struct cgroup_subsys_state *css;
20766a7f6d86SDennis Zhou 
20776a7f6d86SDennis Zhou 	if (!page->mem_cgroup)
20786a7f6d86SDennis Zhou 		return;
20796a7f6d86SDennis Zhou 
2080fc5a828bSDennis Zhou 	rcu_read_lock();
2081fc5a828bSDennis Zhou 
2082fc5a828bSDennis Zhou 	css = cgroup_e_css(page->mem_cgroup->css.cgroup, &io_cgrp_subsys);
2083fc5a828bSDennis Zhou 	bio_associate_blkg_from_css(bio, css);
2084fc5a828bSDennis Zhou 
2085fc5a828bSDennis Zhou 	rcu_read_unlock();
20866a7f6d86SDennis Zhou }
20876a7f6d86SDennis Zhou #endif /* CONFIG_MEMCG */
20886a7f6d86SDennis Zhou 
20892268c0feSDennis Zhou /**
20902268c0feSDennis Zhou  * bio_associate_blkg - associate a bio with a blkg
20912268c0feSDennis Zhou  * @bio: target bio
20922268c0feSDennis Zhou  *
20932268c0feSDennis Zhou  * Associate @bio with the blkg found from the bio's css and request_queue.
20942268c0feSDennis Zhou  * If one is not found, bio_lookup_blkg() creates the blkg.  If a blkg is
20952268c0feSDennis Zhou  * already associated, the css is reused and association redone as the
20962268c0feSDennis Zhou  * request_queue may have changed.
20972268c0feSDennis Zhou  */
20982268c0feSDennis Zhou void bio_associate_blkg(struct bio *bio)
20992268c0feSDennis Zhou {
2100fc5a828bSDennis Zhou 	struct cgroup_subsys_state *css;
21012268c0feSDennis Zhou 
21022268c0feSDennis Zhou 	rcu_read_lock();
21032268c0feSDennis Zhou 
2104db6638d7SDennis Zhou 	if (bio->bi_blkg)
2105fc5a828bSDennis Zhou 		css = &bio_blkcg(bio)->css;
2106db6638d7SDennis Zhou 	else
2107fc5a828bSDennis Zhou 		css = blkcg_css();
21082268c0feSDennis Zhou 
2109fc5a828bSDennis Zhou 	bio_associate_blkg_from_css(bio, css);
21102268c0feSDennis Zhou 
21112268c0feSDennis Zhou 	rcu_read_unlock();
2112f9c78b2bSJens Axboe }
21135cdf2e3fSDennis Zhou EXPORT_SYMBOL_GPL(bio_associate_blkg);
2114f9c78b2bSJens Axboe 
211520bd723eSPaolo Valente /**
2116db6638d7SDennis Zhou  * bio_clone_blkg_association - clone blkg association from src to dst bio
211720bd723eSPaolo Valente  * @dst: destination bio
211820bd723eSPaolo Valente  * @src: source bio
211920bd723eSPaolo Valente  */
2120db6638d7SDennis Zhou void bio_clone_blkg_association(struct bio *dst, struct bio *src)
212120bd723eSPaolo Valente {
21226ab21879SDennis Zhou 	rcu_read_lock();
21236ab21879SDennis Zhou 
2124fc5a828bSDennis Zhou 	if (src->bi_blkg)
21252268c0feSDennis Zhou 		__bio_associate_blkg(dst, src->bi_blkg);
21266ab21879SDennis Zhou 
21276ab21879SDennis Zhou 	rcu_read_unlock();
212820bd723eSPaolo Valente }
2129db6638d7SDennis Zhou EXPORT_SYMBOL_GPL(bio_clone_blkg_association);
2130f9c78b2bSJens Axboe #endif /* CONFIG_BLK_CGROUP */
2131f9c78b2bSJens Axboe 
2132f9c78b2bSJens Axboe static void __init biovec_init_slabs(void)
2133f9c78b2bSJens Axboe {
2134f9c78b2bSJens Axboe 	int i;
2135f9c78b2bSJens Axboe 
2136ed996a52SChristoph Hellwig 	for (i = 0; i < BVEC_POOL_NR; i++) {
2137f9c78b2bSJens Axboe 		int size;
2138f9c78b2bSJens Axboe 		struct biovec_slab *bvs = bvec_slabs + i;
2139f9c78b2bSJens Axboe 
2140f9c78b2bSJens Axboe 		if (bvs->nr_vecs <= BIO_INLINE_VECS) {
2141f9c78b2bSJens Axboe 			bvs->slab = NULL;
2142f9c78b2bSJens Axboe 			continue;
2143f9c78b2bSJens Axboe 		}
2144f9c78b2bSJens Axboe 
2145f9c78b2bSJens Axboe 		size = bvs->nr_vecs * sizeof(struct bio_vec);
2146f9c78b2bSJens Axboe 		bvs->slab = kmem_cache_create(bvs->name, size, 0,
2147f9c78b2bSJens Axboe                                 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
2148f9c78b2bSJens Axboe 	}
2149f9c78b2bSJens Axboe }
2150f9c78b2bSJens Axboe 
2151f9c78b2bSJens Axboe static int __init init_bio(void)
2152f9c78b2bSJens Axboe {
2153f9c78b2bSJens Axboe 	bio_slab_max = 2;
2154f9c78b2bSJens Axboe 	bio_slab_nr = 0;
21556396bb22SKees Cook 	bio_slabs = kcalloc(bio_slab_max, sizeof(struct bio_slab),
21566396bb22SKees Cook 			    GFP_KERNEL);
21572b24e6f6SJohannes Thumshirn 
21582b24e6f6SJohannes Thumshirn 	BUILD_BUG_ON(BIO_FLAG_LAST > BVEC_POOL_OFFSET);
21592b24e6f6SJohannes Thumshirn 
2160f9c78b2bSJens Axboe 	if (!bio_slabs)
2161f9c78b2bSJens Axboe 		panic("bio: can't allocate bios\n");
2162f9c78b2bSJens Axboe 
2163f9c78b2bSJens Axboe 	bio_integrity_init();
2164f9c78b2bSJens Axboe 	biovec_init_slabs();
2165f9c78b2bSJens Axboe 
2166f4f8154aSKent Overstreet 	if (bioset_init(&fs_bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS))
2167f9c78b2bSJens Axboe 		panic("bio: can't allocate bios\n");
2168f9c78b2bSJens Axboe 
2169f4f8154aSKent Overstreet 	if (bioset_integrity_create(&fs_bio_set, BIO_POOL_SIZE))
2170f9c78b2bSJens Axboe 		panic("bio: can't create integrity pool\n");
2171f9c78b2bSJens Axboe 
2172f9c78b2bSJens Axboe 	return 0;
2173f9c78b2bSJens Axboe }
2174f9c78b2bSJens Axboe subsys_initcall(init_bio);
2175