xref: /openbmc/linux/block/bio.c (revision 5151842b)
1f9c78b2bSJens Axboe /*
2f9c78b2bSJens Axboe  * Copyright (C) 2001 Jens Axboe <axboe@kernel.dk>
3f9c78b2bSJens Axboe  *
4f9c78b2bSJens Axboe  * This program is free software; you can redistribute it and/or modify
5f9c78b2bSJens Axboe  * it under the terms of the GNU General Public License version 2 as
6f9c78b2bSJens Axboe  * published by the Free Software Foundation.
7f9c78b2bSJens Axboe  *
8f9c78b2bSJens Axboe  * This program is distributed in the hope that it will be useful,
9f9c78b2bSJens Axboe  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10f9c78b2bSJens Axboe  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11f9c78b2bSJens Axboe  * GNU General Public License for more details.
12f9c78b2bSJens Axboe  *
13f9c78b2bSJens Axboe  * You should have received a copy of the GNU General Public Licens
14f9c78b2bSJens Axboe  * along with this program; if not, write to the Free Software
15f9c78b2bSJens Axboe  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-
16f9c78b2bSJens Axboe  *
17f9c78b2bSJens Axboe  */
18f9c78b2bSJens Axboe #include <linux/mm.h>
19f9c78b2bSJens Axboe #include <linux/swap.h>
20f9c78b2bSJens Axboe #include <linux/bio.h>
21f9c78b2bSJens Axboe #include <linux/blkdev.h>
22f9c78b2bSJens Axboe #include <linux/uio.h>
23f9c78b2bSJens Axboe #include <linux/iocontext.h>
24f9c78b2bSJens Axboe #include <linux/slab.h>
25f9c78b2bSJens Axboe #include <linux/init.h>
26f9c78b2bSJens Axboe #include <linux/kernel.h>
27f9c78b2bSJens Axboe #include <linux/export.h>
28f9c78b2bSJens Axboe #include <linux/mempool.h>
29f9c78b2bSJens Axboe #include <linux/workqueue.h>
30f9c78b2bSJens Axboe #include <linux/cgroup.h>
31f9c78b2bSJens Axboe 
32f9c78b2bSJens Axboe #include <trace/events/block.h>
339e234eeaSShaohua Li #include "blk.h"
34f9c78b2bSJens Axboe 
35f9c78b2bSJens Axboe /*
36f9c78b2bSJens Axboe  * Test patch to inline a certain number of bi_io_vec's inside the bio
37f9c78b2bSJens Axboe  * itself, to shrink a bio data allocation from two mempool calls to one
38f9c78b2bSJens Axboe  */
39f9c78b2bSJens Axboe #define BIO_INLINE_VECS		4
40f9c78b2bSJens Axboe 
41f9c78b2bSJens Axboe /*
42f9c78b2bSJens Axboe  * if you change this list, also change bvec_alloc or things will
43f9c78b2bSJens Axboe  * break badly! cannot be bigger than what you can fit into an
44f9c78b2bSJens Axboe  * unsigned short
45f9c78b2bSJens Axboe  */
46bd5c4facSMikulas Patocka #define BV(x, n) { .nr_vecs = x, .name = "biovec-"#n }
47ed996a52SChristoph Hellwig static struct biovec_slab bvec_slabs[BVEC_POOL_NR] __read_mostly = {
48bd5c4facSMikulas Patocka 	BV(1, 1), BV(4, 4), BV(16, 16), BV(64, 64), BV(128, 128), BV(BIO_MAX_PAGES, max),
49f9c78b2bSJens Axboe };
50f9c78b2bSJens Axboe #undef BV
51f9c78b2bSJens Axboe 
52f9c78b2bSJens Axboe /*
53f9c78b2bSJens Axboe  * fs_bio_set is the bio_set containing bio and iovec memory pools used by
54f9c78b2bSJens Axboe  * IO code that does not need private memory pools.
55f9c78b2bSJens Axboe  */
56f4f8154aSKent Overstreet struct bio_set fs_bio_set;
57f9c78b2bSJens Axboe EXPORT_SYMBOL(fs_bio_set);
58f9c78b2bSJens Axboe 
59f9c78b2bSJens Axboe /*
60f9c78b2bSJens Axboe  * Our slab pool management
61f9c78b2bSJens Axboe  */
62f9c78b2bSJens Axboe struct bio_slab {
63f9c78b2bSJens Axboe 	struct kmem_cache *slab;
64f9c78b2bSJens Axboe 	unsigned int slab_ref;
65f9c78b2bSJens Axboe 	unsigned int slab_size;
66f9c78b2bSJens Axboe 	char name[8];
67f9c78b2bSJens Axboe };
68f9c78b2bSJens Axboe static DEFINE_MUTEX(bio_slab_lock);
69f9c78b2bSJens Axboe static struct bio_slab *bio_slabs;
70f9c78b2bSJens Axboe static unsigned int bio_slab_nr, bio_slab_max;
71f9c78b2bSJens Axboe 
72f9c78b2bSJens Axboe static struct kmem_cache *bio_find_or_create_slab(unsigned int extra_size)
73f9c78b2bSJens Axboe {
74f9c78b2bSJens Axboe 	unsigned int sz = sizeof(struct bio) + extra_size;
75f9c78b2bSJens Axboe 	struct kmem_cache *slab = NULL;
76f9c78b2bSJens Axboe 	struct bio_slab *bslab, *new_bio_slabs;
77f9c78b2bSJens Axboe 	unsigned int new_bio_slab_max;
78f9c78b2bSJens Axboe 	unsigned int i, entry = -1;
79f9c78b2bSJens Axboe 
80f9c78b2bSJens Axboe 	mutex_lock(&bio_slab_lock);
81f9c78b2bSJens Axboe 
82f9c78b2bSJens Axboe 	i = 0;
83f9c78b2bSJens Axboe 	while (i < bio_slab_nr) {
84f9c78b2bSJens Axboe 		bslab = &bio_slabs[i];
85f9c78b2bSJens Axboe 
86f9c78b2bSJens Axboe 		if (!bslab->slab && entry == -1)
87f9c78b2bSJens Axboe 			entry = i;
88f9c78b2bSJens Axboe 		else if (bslab->slab_size == sz) {
89f9c78b2bSJens Axboe 			slab = bslab->slab;
90f9c78b2bSJens Axboe 			bslab->slab_ref++;
91f9c78b2bSJens Axboe 			break;
92f9c78b2bSJens Axboe 		}
93f9c78b2bSJens Axboe 		i++;
94f9c78b2bSJens Axboe 	}
95f9c78b2bSJens Axboe 
96f9c78b2bSJens Axboe 	if (slab)
97f9c78b2bSJens Axboe 		goto out_unlock;
98f9c78b2bSJens Axboe 
99f9c78b2bSJens Axboe 	if (bio_slab_nr == bio_slab_max && entry == -1) {
100f9c78b2bSJens Axboe 		new_bio_slab_max = bio_slab_max << 1;
101f9c78b2bSJens Axboe 		new_bio_slabs = krealloc(bio_slabs,
102f9c78b2bSJens Axboe 					 new_bio_slab_max * sizeof(struct bio_slab),
103f9c78b2bSJens Axboe 					 GFP_KERNEL);
104f9c78b2bSJens Axboe 		if (!new_bio_slabs)
105f9c78b2bSJens Axboe 			goto out_unlock;
106f9c78b2bSJens Axboe 		bio_slab_max = new_bio_slab_max;
107f9c78b2bSJens Axboe 		bio_slabs = new_bio_slabs;
108f9c78b2bSJens Axboe 	}
109f9c78b2bSJens Axboe 	if (entry == -1)
110f9c78b2bSJens Axboe 		entry = bio_slab_nr++;
111f9c78b2bSJens Axboe 
112f9c78b2bSJens Axboe 	bslab = &bio_slabs[entry];
113f9c78b2bSJens Axboe 
114f9c78b2bSJens Axboe 	snprintf(bslab->name, sizeof(bslab->name), "bio-%d", entry);
1156a241483SMikulas Patocka 	slab = kmem_cache_create(bslab->name, sz, ARCH_KMALLOC_MINALIGN,
1166a241483SMikulas Patocka 				 SLAB_HWCACHE_ALIGN, NULL);
117f9c78b2bSJens Axboe 	if (!slab)
118f9c78b2bSJens Axboe 		goto out_unlock;
119f9c78b2bSJens Axboe 
120f9c78b2bSJens Axboe 	bslab->slab = slab;
121f9c78b2bSJens Axboe 	bslab->slab_ref = 1;
122f9c78b2bSJens Axboe 	bslab->slab_size = sz;
123f9c78b2bSJens Axboe out_unlock:
124f9c78b2bSJens Axboe 	mutex_unlock(&bio_slab_lock);
125f9c78b2bSJens Axboe 	return slab;
126f9c78b2bSJens Axboe }
127f9c78b2bSJens Axboe 
128f9c78b2bSJens Axboe static void bio_put_slab(struct bio_set *bs)
129f9c78b2bSJens Axboe {
130f9c78b2bSJens Axboe 	struct bio_slab *bslab = NULL;
131f9c78b2bSJens Axboe 	unsigned int i;
132f9c78b2bSJens Axboe 
133f9c78b2bSJens Axboe 	mutex_lock(&bio_slab_lock);
134f9c78b2bSJens Axboe 
135f9c78b2bSJens Axboe 	for (i = 0; i < bio_slab_nr; i++) {
136f9c78b2bSJens Axboe 		if (bs->bio_slab == bio_slabs[i].slab) {
137f9c78b2bSJens Axboe 			bslab = &bio_slabs[i];
138f9c78b2bSJens Axboe 			break;
139f9c78b2bSJens Axboe 		}
140f9c78b2bSJens Axboe 	}
141f9c78b2bSJens Axboe 
142f9c78b2bSJens Axboe 	if (WARN(!bslab, KERN_ERR "bio: unable to find slab!\n"))
143f9c78b2bSJens Axboe 		goto out;
144f9c78b2bSJens Axboe 
145f9c78b2bSJens Axboe 	WARN_ON(!bslab->slab_ref);
146f9c78b2bSJens Axboe 
147f9c78b2bSJens Axboe 	if (--bslab->slab_ref)
148f9c78b2bSJens Axboe 		goto out;
149f9c78b2bSJens Axboe 
150f9c78b2bSJens Axboe 	kmem_cache_destroy(bslab->slab);
151f9c78b2bSJens Axboe 	bslab->slab = NULL;
152f9c78b2bSJens Axboe 
153f9c78b2bSJens Axboe out:
154f9c78b2bSJens Axboe 	mutex_unlock(&bio_slab_lock);
155f9c78b2bSJens Axboe }
156f9c78b2bSJens Axboe 
157f9c78b2bSJens Axboe unsigned int bvec_nr_vecs(unsigned short idx)
158f9c78b2bSJens Axboe {
159f9c78b2bSJens Axboe 	return bvec_slabs[idx].nr_vecs;
160f9c78b2bSJens Axboe }
161f9c78b2bSJens Axboe 
162f9c78b2bSJens Axboe void bvec_free(mempool_t *pool, struct bio_vec *bv, unsigned int idx)
163f9c78b2bSJens Axboe {
164ed996a52SChristoph Hellwig 	if (!idx)
165ed996a52SChristoph Hellwig 		return;
166ed996a52SChristoph Hellwig 	idx--;
167f9c78b2bSJens Axboe 
168ed996a52SChristoph Hellwig 	BIO_BUG_ON(idx >= BVEC_POOL_NR);
169ed996a52SChristoph Hellwig 
170ed996a52SChristoph Hellwig 	if (idx == BVEC_POOL_MAX) {
171f9c78b2bSJens Axboe 		mempool_free(bv, pool);
172ed996a52SChristoph Hellwig 	} else {
173f9c78b2bSJens Axboe 		struct biovec_slab *bvs = bvec_slabs + idx;
174f9c78b2bSJens Axboe 
175f9c78b2bSJens Axboe 		kmem_cache_free(bvs->slab, bv);
176f9c78b2bSJens Axboe 	}
177f9c78b2bSJens Axboe }
178f9c78b2bSJens Axboe 
179f9c78b2bSJens Axboe struct bio_vec *bvec_alloc(gfp_t gfp_mask, int nr, unsigned long *idx,
180f9c78b2bSJens Axboe 			   mempool_t *pool)
181f9c78b2bSJens Axboe {
182f9c78b2bSJens Axboe 	struct bio_vec *bvl;
183f9c78b2bSJens Axboe 
184f9c78b2bSJens Axboe 	/*
185f9c78b2bSJens Axboe 	 * see comment near bvec_array define!
186f9c78b2bSJens Axboe 	 */
187f9c78b2bSJens Axboe 	switch (nr) {
188f9c78b2bSJens Axboe 	case 1:
189f9c78b2bSJens Axboe 		*idx = 0;
190f9c78b2bSJens Axboe 		break;
191f9c78b2bSJens Axboe 	case 2 ... 4:
192f9c78b2bSJens Axboe 		*idx = 1;
193f9c78b2bSJens Axboe 		break;
194f9c78b2bSJens Axboe 	case 5 ... 16:
195f9c78b2bSJens Axboe 		*idx = 2;
196f9c78b2bSJens Axboe 		break;
197f9c78b2bSJens Axboe 	case 17 ... 64:
198f9c78b2bSJens Axboe 		*idx = 3;
199f9c78b2bSJens Axboe 		break;
200f9c78b2bSJens Axboe 	case 65 ... 128:
201f9c78b2bSJens Axboe 		*idx = 4;
202f9c78b2bSJens Axboe 		break;
203f9c78b2bSJens Axboe 	case 129 ... BIO_MAX_PAGES:
204f9c78b2bSJens Axboe 		*idx = 5;
205f9c78b2bSJens Axboe 		break;
206f9c78b2bSJens Axboe 	default:
207f9c78b2bSJens Axboe 		return NULL;
208f9c78b2bSJens Axboe 	}
209f9c78b2bSJens Axboe 
210f9c78b2bSJens Axboe 	/*
211f9c78b2bSJens Axboe 	 * idx now points to the pool we want to allocate from. only the
212f9c78b2bSJens Axboe 	 * 1-vec entry pool is mempool backed.
213f9c78b2bSJens Axboe 	 */
214ed996a52SChristoph Hellwig 	if (*idx == BVEC_POOL_MAX) {
215f9c78b2bSJens Axboe fallback:
216f9c78b2bSJens Axboe 		bvl = mempool_alloc(pool, gfp_mask);
217f9c78b2bSJens Axboe 	} else {
218f9c78b2bSJens Axboe 		struct biovec_slab *bvs = bvec_slabs + *idx;
219d0164adcSMel Gorman 		gfp_t __gfp_mask = gfp_mask & ~(__GFP_DIRECT_RECLAIM | __GFP_IO);
220f9c78b2bSJens Axboe 
221f9c78b2bSJens Axboe 		/*
222f9c78b2bSJens Axboe 		 * Make this allocation restricted and don't dump info on
223f9c78b2bSJens Axboe 		 * allocation failures, since we'll fallback to the mempool
224f9c78b2bSJens Axboe 		 * in case of failure.
225f9c78b2bSJens Axboe 		 */
226f9c78b2bSJens Axboe 		__gfp_mask |= __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
227f9c78b2bSJens Axboe 
228f9c78b2bSJens Axboe 		/*
229d0164adcSMel Gorman 		 * Try a slab allocation. If this fails and __GFP_DIRECT_RECLAIM
230f9c78b2bSJens Axboe 		 * is set, retry with the 1-entry mempool
231f9c78b2bSJens Axboe 		 */
232f9c78b2bSJens Axboe 		bvl = kmem_cache_alloc(bvs->slab, __gfp_mask);
233d0164adcSMel Gorman 		if (unlikely(!bvl && (gfp_mask & __GFP_DIRECT_RECLAIM))) {
234ed996a52SChristoph Hellwig 			*idx = BVEC_POOL_MAX;
235f9c78b2bSJens Axboe 			goto fallback;
236f9c78b2bSJens Axboe 		}
237f9c78b2bSJens Axboe 	}
238f9c78b2bSJens Axboe 
239ed996a52SChristoph Hellwig 	(*idx)++;
240f9c78b2bSJens Axboe 	return bvl;
241f9c78b2bSJens Axboe }
242f9c78b2bSJens Axboe 
2439ae3b3f5SJens Axboe void bio_uninit(struct bio *bio)
244f9c78b2bSJens Axboe {
245f9c78b2bSJens Axboe 	bio_disassociate_task(bio);
246f9c78b2bSJens Axboe }
2479ae3b3f5SJens Axboe EXPORT_SYMBOL(bio_uninit);
248f9c78b2bSJens Axboe 
249f9c78b2bSJens Axboe static void bio_free(struct bio *bio)
250f9c78b2bSJens Axboe {
251f9c78b2bSJens Axboe 	struct bio_set *bs = bio->bi_pool;
252f9c78b2bSJens Axboe 	void *p;
253f9c78b2bSJens Axboe 
2549ae3b3f5SJens Axboe 	bio_uninit(bio);
255f9c78b2bSJens Axboe 
256f9c78b2bSJens Axboe 	if (bs) {
2578aa6ba2fSKent Overstreet 		bvec_free(&bs->bvec_pool, bio->bi_io_vec, BVEC_POOL_IDX(bio));
258f9c78b2bSJens Axboe 
259f9c78b2bSJens Axboe 		/*
260f9c78b2bSJens Axboe 		 * If we have front padding, adjust the bio pointer before freeing
261f9c78b2bSJens Axboe 		 */
262f9c78b2bSJens Axboe 		p = bio;
263f9c78b2bSJens Axboe 		p -= bs->front_pad;
264f9c78b2bSJens Axboe 
2658aa6ba2fSKent Overstreet 		mempool_free(p, &bs->bio_pool);
266f9c78b2bSJens Axboe 	} else {
267f9c78b2bSJens Axboe 		/* Bio was allocated by bio_kmalloc() */
268f9c78b2bSJens Axboe 		kfree(bio);
269f9c78b2bSJens Axboe 	}
270f9c78b2bSJens Axboe }
271f9c78b2bSJens Axboe 
2729ae3b3f5SJens Axboe /*
2739ae3b3f5SJens Axboe  * Users of this function have their own bio allocation. Subsequently,
2749ae3b3f5SJens Axboe  * they must remember to pair any call to bio_init() with bio_uninit()
2759ae3b3f5SJens Axboe  * when IO has completed, or when the bio is released.
2769ae3b3f5SJens Axboe  */
2773a83f467SMing Lei void bio_init(struct bio *bio, struct bio_vec *table,
2783a83f467SMing Lei 	      unsigned short max_vecs)
279f9c78b2bSJens Axboe {
280f9c78b2bSJens Axboe 	memset(bio, 0, sizeof(*bio));
281c4cf5261SJens Axboe 	atomic_set(&bio->__bi_remaining, 1);
282dac56212SJens Axboe 	atomic_set(&bio->__bi_cnt, 1);
2833a83f467SMing Lei 
2843a83f467SMing Lei 	bio->bi_io_vec = table;
2853a83f467SMing Lei 	bio->bi_max_vecs = max_vecs;
286f9c78b2bSJens Axboe }
287f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_init);
288f9c78b2bSJens Axboe 
289f9c78b2bSJens Axboe /**
290f9c78b2bSJens Axboe  * bio_reset - reinitialize a bio
291f9c78b2bSJens Axboe  * @bio:	bio to reset
292f9c78b2bSJens Axboe  *
293f9c78b2bSJens Axboe  * Description:
294f9c78b2bSJens Axboe  *   After calling bio_reset(), @bio will be in the same state as a freshly
295f9c78b2bSJens Axboe  *   allocated bio returned bio bio_alloc_bioset() - the only fields that are
296f9c78b2bSJens Axboe  *   preserved are the ones that are initialized by bio_alloc_bioset(). See
297f9c78b2bSJens Axboe  *   comment in struct bio.
298f9c78b2bSJens Axboe  */
299f9c78b2bSJens Axboe void bio_reset(struct bio *bio)
300f9c78b2bSJens Axboe {
301f9c78b2bSJens Axboe 	unsigned long flags = bio->bi_flags & (~0UL << BIO_RESET_BITS);
302f9c78b2bSJens Axboe 
3039ae3b3f5SJens Axboe 	bio_uninit(bio);
304f9c78b2bSJens Axboe 
305f9c78b2bSJens Axboe 	memset(bio, 0, BIO_RESET_BYTES);
3064246a0b6SChristoph Hellwig 	bio->bi_flags = flags;
307c4cf5261SJens Axboe 	atomic_set(&bio->__bi_remaining, 1);
308f9c78b2bSJens Axboe }
309f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_reset);
310f9c78b2bSJens Axboe 
31138f8baaeSChristoph Hellwig static struct bio *__bio_chain_endio(struct bio *bio)
312f9c78b2bSJens Axboe {
3134246a0b6SChristoph Hellwig 	struct bio *parent = bio->bi_private;
3144246a0b6SChristoph Hellwig 
3154e4cbee9SChristoph Hellwig 	if (!parent->bi_status)
3164e4cbee9SChristoph Hellwig 		parent->bi_status = bio->bi_status;
317f9c78b2bSJens Axboe 	bio_put(bio);
31838f8baaeSChristoph Hellwig 	return parent;
31938f8baaeSChristoph Hellwig }
32038f8baaeSChristoph Hellwig 
32138f8baaeSChristoph Hellwig static void bio_chain_endio(struct bio *bio)
32238f8baaeSChristoph Hellwig {
32338f8baaeSChristoph Hellwig 	bio_endio(__bio_chain_endio(bio));
324f9c78b2bSJens Axboe }
325f9c78b2bSJens Axboe 
326f9c78b2bSJens Axboe /**
327f9c78b2bSJens Axboe  * bio_chain - chain bio completions
328f9c78b2bSJens Axboe  * @bio: the target bio
329f9c78b2bSJens Axboe  * @parent: the @bio's parent bio
330f9c78b2bSJens Axboe  *
331f9c78b2bSJens Axboe  * The caller won't have a bi_end_io called when @bio completes - instead,
332f9c78b2bSJens Axboe  * @parent's bi_end_io won't be called until both @parent and @bio have
333f9c78b2bSJens Axboe  * completed; the chained bio will also be freed when it completes.
334f9c78b2bSJens Axboe  *
335f9c78b2bSJens Axboe  * The caller must not set bi_private or bi_end_io in @bio.
336f9c78b2bSJens Axboe  */
337f9c78b2bSJens Axboe void bio_chain(struct bio *bio, struct bio *parent)
338f9c78b2bSJens Axboe {
339f9c78b2bSJens Axboe 	BUG_ON(bio->bi_private || bio->bi_end_io);
340f9c78b2bSJens Axboe 
341f9c78b2bSJens Axboe 	bio->bi_private = parent;
342f9c78b2bSJens Axboe 	bio->bi_end_io	= bio_chain_endio;
343c4cf5261SJens Axboe 	bio_inc_remaining(parent);
344f9c78b2bSJens Axboe }
345f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_chain);
346f9c78b2bSJens Axboe 
347f9c78b2bSJens Axboe static void bio_alloc_rescue(struct work_struct *work)
348f9c78b2bSJens Axboe {
349f9c78b2bSJens Axboe 	struct bio_set *bs = container_of(work, struct bio_set, rescue_work);
350f9c78b2bSJens Axboe 	struct bio *bio;
351f9c78b2bSJens Axboe 
352f9c78b2bSJens Axboe 	while (1) {
353f9c78b2bSJens Axboe 		spin_lock(&bs->rescue_lock);
354f9c78b2bSJens Axboe 		bio = bio_list_pop(&bs->rescue_list);
355f9c78b2bSJens Axboe 		spin_unlock(&bs->rescue_lock);
356f9c78b2bSJens Axboe 
357f9c78b2bSJens Axboe 		if (!bio)
358f9c78b2bSJens Axboe 			break;
359f9c78b2bSJens Axboe 
360f9c78b2bSJens Axboe 		generic_make_request(bio);
361f9c78b2bSJens Axboe 	}
362f9c78b2bSJens Axboe }
363f9c78b2bSJens Axboe 
364f9c78b2bSJens Axboe static void punt_bios_to_rescuer(struct bio_set *bs)
365f9c78b2bSJens Axboe {
366f9c78b2bSJens Axboe 	struct bio_list punt, nopunt;
367f9c78b2bSJens Axboe 	struct bio *bio;
368f9c78b2bSJens Axboe 
36947e0fb46SNeilBrown 	if (WARN_ON_ONCE(!bs->rescue_workqueue))
37047e0fb46SNeilBrown 		return;
371f9c78b2bSJens Axboe 	/*
372f9c78b2bSJens Axboe 	 * In order to guarantee forward progress we must punt only bios that
373f9c78b2bSJens Axboe 	 * were allocated from this bio_set; otherwise, if there was a bio on
374f9c78b2bSJens Axboe 	 * there for a stacking driver higher up in the stack, processing it
375f9c78b2bSJens Axboe 	 * could require allocating bios from this bio_set, and doing that from
376f9c78b2bSJens Axboe 	 * our own rescuer would be bad.
377f9c78b2bSJens Axboe 	 *
378f9c78b2bSJens Axboe 	 * Since bio lists are singly linked, pop them all instead of trying to
379f9c78b2bSJens Axboe 	 * remove from the middle of the list:
380f9c78b2bSJens Axboe 	 */
381f9c78b2bSJens Axboe 
382f9c78b2bSJens Axboe 	bio_list_init(&punt);
383f9c78b2bSJens Axboe 	bio_list_init(&nopunt);
384f9c78b2bSJens Axboe 
385f5fe1b51SNeilBrown 	while ((bio = bio_list_pop(&current->bio_list[0])))
386f9c78b2bSJens Axboe 		bio_list_add(bio->bi_pool == bs ? &punt : &nopunt, bio);
387f5fe1b51SNeilBrown 	current->bio_list[0] = nopunt;
388f9c78b2bSJens Axboe 
389f5fe1b51SNeilBrown 	bio_list_init(&nopunt);
390f5fe1b51SNeilBrown 	while ((bio = bio_list_pop(&current->bio_list[1])))
391f5fe1b51SNeilBrown 		bio_list_add(bio->bi_pool == bs ? &punt : &nopunt, bio);
392f5fe1b51SNeilBrown 	current->bio_list[1] = nopunt;
393f9c78b2bSJens Axboe 
394f9c78b2bSJens Axboe 	spin_lock(&bs->rescue_lock);
395f9c78b2bSJens Axboe 	bio_list_merge(&bs->rescue_list, &punt);
396f9c78b2bSJens Axboe 	spin_unlock(&bs->rescue_lock);
397f9c78b2bSJens Axboe 
398f9c78b2bSJens Axboe 	queue_work(bs->rescue_workqueue, &bs->rescue_work);
399f9c78b2bSJens Axboe }
400f9c78b2bSJens Axboe 
401f9c78b2bSJens Axboe /**
402f9c78b2bSJens Axboe  * bio_alloc_bioset - allocate a bio for I/O
403519c8e9fSRandy Dunlap  * @gfp_mask:   the GFP_* mask given to the slab allocator
404f9c78b2bSJens Axboe  * @nr_iovecs:	number of iovecs to pre-allocate
405f9c78b2bSJens Axboe  * @bs:		the bio_set to allocate from.
406f9c78b2bSJens Axboe  *
407f9c78b2bSJens Axboe  * Description:
408f9c78b2bSJens Axboe  *   If @bs is NULL, uses kmalloc() to allocate the bio; else the allocation is
409f9c78b2bSJens Axboe  *   backed by the @bs's mempool.
410f9c78b2bSJens Axboe  *
411d0164adcSMel Gorman  *   When @bs is not NULL, if %__GFP_DIRECT_RECLAIM is set then bio_alloc will
412d0164adcSMel Gorman  *   always be able to allocate a bio. This is due to the mempool guarantees.
413d0164adcSMel Gorman  *   To make this work, callers must never allocate more than 1 bio at a time
414d0164adcSMel Gorman  *   from this pool. Callers that need to allocate more than 1 bio must always
415d0164adcSMel Gorman  *   submit the previously allocated bio for IO before attempting to allocate
416d0164adcSMel Gorman  *   a new one. Failure to do so can cause deadlocks under memory pressure.
417f9c78b2bSJens Axboe  *
418f9c78b2bSJens Axboe  *   Note that when running under generic_make_request() (i.e. any block
419f9c78b2bSJens Axboe  *   driver), bios are not submitted until after you return - see the code in
420f9c78b2bSJens Axboe  *   generic_make_request() that converts recursion into iteration, to prevent
421f9c78b2bSJens Axboe  *   stack overflows.
422f9c78b2bSJens Axboe  *
423f9c78b2bSJens Axboe  *   This would normally mean allocating multiple bios under
424f9c78b2bSJens Axboe  *   generic_make_request() would be susceptible to deadlocks, but we have
425f9c78b2bSJens Axboe  *   deadlock avoidance code that resubmits any blocked bios from a rescuer
426f9c78b2bSJens Axboe  *   thread.
427f9c78b2bSJens Axboe  *
428f9c78b2bSJens Axboe  *   However, we do not guarantee forward progress for allocations from other
429f9c78b2bSJens Axboe  *   mempools. Doing multiple allocations from the same mempool under
430f9c78b2bSJens Axboe  *   generic_make_request() should be avoided - instead, use bio_set's front_pad
431f9c78b2bSJens Axboe  *   for per bio allocations.
432f9c78b2bSJens Axboe  *
433f9c78b2bSJens Axboe  *   RETURNS:
434f9c78b2bSJens Axboe  *   Pointer to new bio on success, NULL on failure.
435f9c78b2bSJens Axboe  */
4367a88fa19SDan Carpenter struct bio *bio_alloc_bioset(gfp_t gfp_mask, unsigned int nr_iovecs,
4377a88fa19SDan Carpenter 			     struct bio_set *bs)
438f9c78b2bSJens Axboe {
439f9c78b2bSJens Axboe 	gfp_t saved_gfp = gfp_mask;
440f9c78b2bSJens Axboe 	unsigned front_pad;
441f9c78b2bSJens Axboe 	unsigned inline_vecs;
442f9c78b2bSJens Axboe 	struct bio_vec *bvl = NULL;
443f9c78b2bSJens Axboe 	struct bio *bio;
444f9c78b2bSJens Axboe 	void *p;
445f9c78b2bSJens Axboe 
446f9c78b2bSJens Axboe 	if (!bs) {
447f9c78b2bSJens Axboe 		if (nr_iovecs > UIO_MAXIOV)
448f9c78b2bSJens Axboe 			return NULL;
449f9c78b2bSJens Axboe 
450f9c78b2bSJens Axboe 		p = kmalloc(sizeof(struct bio) +
451f9c78b2bSJens Axboe 			    nr_iovecs * sizeof(struct bio_vec),
452f9c78b2bSJens Axboe 			    gfp_mask);
453f9c78b2bSJens Axboe 		front_pad = 0;
454f9c78b2bSJens Axboe 		inline_vecs = nr_iovecs;
455f9c78b2bSJens Axboe 	} else {
456d8f429e1SJunichi Nomura 		/* should not use nobvec bioset for nr_iovecs > 0 */
4578aa6ba2fSKent Overstreet 		if (WARN_ON_ONCE(!mempool_initialized(&bs->bvec_pool) &&
4588aa6ba2fSKent Overstreet 				 nr_iovecs > 0))
459d8f429e1SJunichi Nomura 			return NULL;
460f9c78b2bSJens Axboe 		/*
461f9c78b2bSJens Axboe 		 * generic_make_request() converts recursion to iteration; this
462f9c78b2bSJens Axboe 		 * means if we're running beneath it, any bios we allocate and
463f9c78b2bSJens Axboe 		 * submit will not be submitted (and thus freed) until after we
464f9c78b2bSJens Axboe 		 * return.
465f9c78b2bSJens Axboe 		 *
466f9c78b2bSJens Axboe 		 * This exposes us to a potential deadlock if we allocate
467f9c78b2bSJens Axboe 		 * multiple bios from the same bio_set() while running
468f9c78b2bSJens Axboe 		 * underneath generic_make_request(). If we were to allocate
469f9c78b2bSJens Axboe 		 * multiple bios (say a stacking block driver that was splitting
470f9c78b2bSJens Axboe 		 * bios), we would deadlock if we exhausted the mempool's
471f9c78b2bSJens Axboe 		 * reserve.
472f9c78b2bSJens Axboe 		 *
473f9c78b2bSJens Axboe 		 * We solve this, and guarantee forward progress, with a rescuer
474f9c78b2bSJens Axboe 		 * workqueue per bio_set. If we go to allocate and there are
475f9c78b2bSJens Axboe 		 * bios on current->bio_list, we first try the allocation
476d0164adcSMel Gorman 		 * without __GFP_DIRECT_RECLAIM; if that fails, we punt those
477d0164adcSMel Gorman 		 * bios we would be blocking to the rescuer workqueue before
478d0164adcSMel Gorman 		 * we retry with the original gfp_flags.
479f9c78b2bSJens Axboe 		 */
480f9c78b2bSJens Axboe 
481f5fe1b51SNeilBrown 		if (current->bio_list &&
482f5fe1b51SNeilBrown 		    (!bio_list_empty(&current->bio_list[0]) ||
48347e0fb46SNeilBrown 		     !bio_list_empty(&current->bio_list[1])) &&
48447e0fb46SNeilBrown 		    bs->rescue_workqueue)
485d0164adcSMel Gorman 			gfp_mask &= ~__GFP_DIRECT_RECLAIM;
486f9c78b2bSJens Axboe 
4878aa6ba2fSKent Overstreet 		p = mempool_alloc(&bs->bio_pool, gfp_mask);
488f9c78b2bSJens Axboe 		if (!p && gfp_mask != saved_gfp) {
489f9c78b2bSJens Axboe 			punt_bios_to_rescuer(bs);
490f9c78b2bSJens Axboe 			gfp_mask = saved_gfp;
4918aa6ba2fSKent Overstreet 			p = mempool_alloc(&bs->bio_pool, gfp_mask);
492f9c78b2bSJens Axboe 		}
493f9c78b2bSJens Axboe 
494f9c78b2bSJens Axboe 		front_pad = bs->front_pad;
495f9c78b2bSJens Axboe 		inline_vecs = BIO_INLINE_VECS;
496f9c78b2bSJens Axboe 	}
497f9c78b2bSJens Axboe 
498f9c78b2bSJens Axboe 	if (unlikely(!p))
499f9c78b2bSJens Axboe 		return NULL;
500f9c78b2bSJens Axboe 
501f9c78b2bSJens Axboe 	bio = p + front_pad;
5023a83f467SMing Lei 	bio_init(bio, NULL, 0);
503f9c78b2bSJens Axboe 
504f9c78b2bSJens Axboe 	if (nr_iovecs > inline_vecs) {
505ed996a52SChristoph Hellwig 		unsigned long idx = 0;
506ed996a52SChristoph Hellwig 
5078aa6ba2fSKent Overstreet 		bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx, &bs->bvec_pool);
508f9c78b2bSJens Axboe 		if (!bvl && gfp_mask != saved_gfp) {
509f9c78b2bSJens Axboe 			punt_bios_to_rescuer(bs);
510f9c78b2bSJens Axboe 			gfp_mask = saved_gfp;
5118aa6ba2fSKent Overstreet 			bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx, &bs->bvec_pool);
512f9c78b2bSJens Axboe 		}
513f9c78b2bSJens Axboe 
514f9c78b2bSJens Axboe 		if (unlikely(!bvl))
515f9c78b2bSJens Axboe 			goto err_free;
516f9c78b2bSJens Axboe 
517ed996a52SChristoph Hellwig 		bio->bi_flags |= idx << BVEC_POOL_OFFSET;
518f9c78b2bSJens Axboe 	} else if (nr_iovecs) {
519f9c78b2bSJens Axboe 		bvl = bio->bi_inline_vecs;
520f9c78b2bSJens Axboe 	}
521f9c78b2bSJens Axboe 
522f9c78b2bSJens Axboe 	bio->bi_pool = bs;
523f9c78b2bSJens Axboe 	bio->bi_max_vecs = nr_iovecs;
524f9c78b2bSJens Axboe 	bio->bi_io_vec = bvl;
525f9c78b2bSJens Axboe 	return bio;
526f9c78b2bSJens Axboe 
527f9c78b2bSJens Axboe err_free:
5288aa6ba2fSKent Overstreet 	mempool_free(p, &bs->bio_pool);
529f9c78b2bSJens Axboe 	return NULL;
530f9c78b2bSJens Axboe }
531f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_alloc_bioset);
532f9c78b2bSJens Axboe 
53338a72dacSKent Overstreet void zero_fill_bio_iter(struct bio *bio, struct bvec_iter start)
534f9c78b2bSJens Axboe {
535f9c78b2bSJens Axboe 	unsigned long flags;
536f9c78b2bSJens Axboe 	struct bio_vec bv;
537f9c78b2bSJens Axboe 	struct bvec_iter iter;
538f9c78b2bSJens Axboe 
53938a72dacSKent Overstreet 	__bio_for_each_segment(bv, bio, iter, start) {
540f9c78b2bSJens Axboe 		char *data = bvec_kmap_irq(&bv, &flags);
541f9c78b2bSJens Axboe 		memset(data, 0, bv.bv_len);
542f9c78b2bSJens Axboe 		flush_dcache_page(bv.bv_page);
543f9c78b2bSJens Axboe 		bvec_kunmap_irq(data, &flags);
544f9c78b2bSJens Axboe 	}
545f9c78b2bSJens Axboe }
54638a72dacSKent Overstreet EXPORT_SYMBOL(zero_fill_bio_iter);
547f9c78b2bSJens Axboe 
548f9c78b2bSJens Axboe /**
549f9c78b2bSJens Axboe  * bio_put - release a reference to a bio
550f9c78b2bSJens Axboe  * @bio:   bio to release reference to
551f9c78b2bSJens Axboe  *
552f9c78b2bSJens Axboe  * Description:
553f9c78b2bSJens Axboe  *   Put a reference to a &struct bio, either one you have gotten with
5549b10f6a9SNeilBrown  *   bio_alloc, bio_get or bio_clone_*. The last put of a bio will free it.
555f9c78b2bSJens Axboe  **/
556f9c78b2bSJens Axboe void bio_put(struct bio *bio)
557f9c78b2bSJens Axboe {
558dac56212SJens Axboe 	if (!bio_flagged(bio, BIO_REFFED))
559dac56212SJens Axboe 		bio_free(bio);
560dac56212SJens Axboe 	else {
561dac56212SJens Axboe 		BIO_BUG_ON(!atomic_read(&bio->__bi_cnt));
562f9c78b2bSJens Axboe 
563f9c78b2bSJens Axboe 		/*
564f9c78b2bSJens Axboe 		 * last put frees it
565f9c78b2bSJens Axboe 		 */
566dac56212SJens Axboe 		if (atomic_dec_and_test(&bio->__bi_cnt))
567f9c78b2bSJens Axboe 			bio_free(bio);
568f9c78b2bSJens Axboe 	}
569dac56212SJens Axboe }
570f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_put);
571f9c78b2bSJens Axboe 
572f9c78b2bSJens Axboe inline int bio_phys_segments(struct request_queue *q, struct bio *bio)
573f9c78b2bSJens Axboe {
574f9c78b2bSJens Axboe 	if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
575f9c78b2bSJens Axboe 		blk_recount_segments(q, bio);
576f9c78b2bSJens Axboe 
577f9c78b2bSJens Axboe 	return bio->bi_phys_segments;
578f9c78b2bSJens Axboe }
579f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_phys_segments);
580f9c78b2bSJens Axboe 
581f9c78b2bSJens Axboe /**
582f9c78b2bSJens Axboe  * 	__bio_clone_fast - clone a bio that shares the original bio's biovec
583f9c78b2bSJens Axboe  * 	@bio: destination bio
584f9c78b2bSJens Axboe  * 	@bio_src: bio to clone
585f9c78b2bSJens Axboe  *
586f9c78b2bSJens Axboe  *	Clone a &bio. Caller will own the returned bio, but not
587f9c78b2bSJens Axboe  *	the actual data it points to. Reference count of returned
588f9c78b2bSJens Axboe  * 	bio will be one.
589f9c78b2bSJens Axboe  *
590f9c78b2bSJens Axboe  * 	Caller must ensure that @bio_src is not freed before @bio.
591f9c78b2bSJens Axboe  */
592f9c78b2bSJens Axboe void __bio_clone_fast(struct bio *bio, struct bio *bio_src)
593f9c78b2bSJens Axboe {
594ed996a52SChristoph Hellwig 	BUG_ON(bio->bi_pool && BVEC_POOL_IDX(bio));
595f9c78b2bSJens Axboe 
596f9c78b2bSJens Axboe 	/*
59774d46992SChristoph Hellwig 	 * most users will be overriding ->bi_disk with a new target,
598f9c78b2bSJens Axboe 	 * so we don't set nor calculate new physical/hw segment counts here
599f9c78b2bSJens Axboe 	 */
60074d46992SChristoph Hellwig 	bio->bi_disk = bio_src->bi_disk;
60162530ed8SMichael Lyle 	bio->bi_partno = bio_src->bi_partno;
602b7c44ed9SJens Axboe 	bio_set_flag(bio, BIO_CLONED);
603111be883SShaohua Li 	if (bio_flagged(bio_src, BIO_THROTTLED))
604111be883SShaohua Li 		bio_set_flag(bio, BIO_THROTTLED);
6051eff9d32SJens Axboe 	bio->bi_opf = bio_src->bi_opf;
606cb6934f8SJens Axboe 	bio->bi_write_hint = bio_src->bi_write_hint;
607f9c78b2bSJens Axboe 	bio->bi_iter = bio_src->bi_iter;
608f9c78b2bSJens Axboe 	bio->bi_io_vec = bio_src->bi_io_vec;
60920bd723eSPaolo Valente 
61020bd723eSPaolo Valente 	bio_clone_blkcg_association(bio, bio_src);
611f9c78b2bSJens Axboe }
612f9c78b2bSJens Axboe EXPORT_SYMBOL(__bio_clone_fast);
613f9c78b2bSJens Axboe 
614f9c78b2bSJens Axboe /**
615f9c78b2bSJens Axboe  *	bio_clone_fast - clone a bio that shares the original bio's biovec
616f9c78b2bSJens Axboe  *	@bio: bio to clone
617f9c78b2bSJens Axboe  *	@gfp_mask: allocation priority
618f9c78b2bSJens Axboe  *	@bs: bio_set to allocate from
619f9c78b2bSJens Axboe  *
620f9c78b2bSJens Axboe  * 	Like __bio_clone_fast, only also allocates the returned bio
621f9c78b2bSJens Axboe  */
622f9c78b2bSJens Axboe struct bio *bio_clone_fast(struct bio *bio, gfp_t gfp_mask, struct bio_set *bs)
623f9c78b2bSJens Axboe {
624f9c78b2bSJens Axboe 	struct bio *b;
625f9c78b2bSJens Axboe 
626f9c78b2bSJens Axboe 	b = bio_alloc_bioset(gfp_mask, 0, bs);
627f9c78b2bSJens Axboe 	if (!b)
628f9c78b2bSJens Axboe 		return NULL;
629f9c78b2bSJens Axboe 
630f9c78b2bSJens Axboe 	__bio_clone_fast(b, bio);
631f9c78b2bSJens Axboe 
632f9c78b2bSJens Axboe 	if (bio_integrity(bio)) {
633f9c78b2bSJens Axboe 		int ret;
634f9c78b2bSJens Axboe 
635f9c78b2bSJens Axboe 		ret = bio_integrity_clone(b, bio, gfp_mask);
636f9c78b2bSJens Axboe 
637f9c78b2bSJens Axboe 		if (ret < 0) {
638f9c78b2bSJens Axboe 			bio_put(b);
639f9c78b2bSJens Axboe 			return NULL;
640f9c78b2bSJens Axboe 		}
641f9c78b2bSJens Axboe 	}
642f9c78b2bSJens Axboe 
643f9c78b2bSJens Axboe 	return b;
644f9c78b2bSJens Axboe }
645f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_clone_fast);
646f9c78b2bSJens Axboe 
647f4595875SShaohua Li /**
648f4595875SShaohua Li  * 	bio_clone_bioset - clone a bio
649f4595875SShaohua Li  * 	@bio_src: bio to clone
650f4595875SShaohua Li  *	@gfp_mask: allocation priority
651f4595875SShaohua Li  *	@bs: bio_set to allocate from
652f4595875SShaohua Li  *
653f4595875SShaohua Li  *	Clone bio. Caller will own the returned bio, but not the actual data it
654f4595875SShaohua Li  *	points to. Reference count of returned bio will be one.
655f4595875SShaohua Li  */
656f4595875SShaohua Li struct bio *bio_clone_bioset(struct bio *bio_src, gfp_t gfp_mask,
657f4595875SShaohua Li 			     struct bio_set *bs)
658f9c78b2bSJens Axboe {
659f9c78b2bSJens Axboe 	struct bvec_iter iter;
660f9c78b2bSJens Axboe 	struct bio_vec bv;
661f9c78b2bSJens Axboe 	struct bio *bio;
662f9c78b2bSJens Axboe 
663f9c78b2bSJens Axboe 	/*
664f9c78b2bSJens Axboe 	 * Pre immutable biovecs, __bio_clone() used to just do a memcpy from
665f9c78b2bSJens Axboe 	 * bio_src->bi_io_vec to bio->bi_io_vec.
666f9c78b2bSJens Axboe 	 *
667f9c78b2bSJens Axboe 	 * We can't do that anymore, because:
668f9c78b2bSJens Axboe 	 *
669f9c78b2bSJens Axboe 	 *  - The point of cloning the biovec is to produce a bio with a biovec
670f9c78b2bSJens Axboe 	 *    the caller can modify: bi_idx and bi_bvec_done should be 0.
671f9c78b2bSJens Axboe 	 *
672f9c78b2bSJens Axboe 	 *  - The original bio could've had more than BIO_MAX_PAGES biovecs; if
673f9c78b2bSJens Axboe 	 *    we tried to clone the whole thing bio_alloc_bioset() would fail.
674f9c78b2bSJens Axboe 	 *    But the clone should succeed as long as the number of biovecs we
675f9c78b2bSJens Axboe 	 *    actually need to allocate is fewer than BIO_MAX_PAGES.
676f9c78b2bSJens Axboe 	 *
677f9c78b2bSJens Axboe 	 *  - Lastly, bi_vcnt should not be looked at or relied upon by code
678f9c78b2bSJens Axboe 	 *    that does not own the bio - reason being drivers don't use it for
679f9c78b2bSJens Axboe 	 *    iterating over the biovec anymore, so expecting it to be kept up
680f9c78b2bSJens Axboe 	 *    to date (i.e. for clones that share the parent biovec) is just
681f9c78b2bSJens Axboe 	 *    asking for trouble and would force extra work on
682f9c78b2bSJens Axboe 	 *    __bio_clone_fast() anyways.
683f9c78b2bSJens Axboe 	 */
684f9c78b2bSJens Axboe 
685f4595875SShaohua Li 	bio = bio_alloc_bioset(gfp_mask, bio_segments(bio_src), bs);
686f9c78b2bSJens Axboe 	if (!bio)
687f9c78b2bSJens Axboe 		return NULL;
68874d46992SChristoph Hellwig 	bio->bi_disk		= bio_src->bi_disk;
6891eff9d32SJens Axboe 	bio->bi_opf		= bio_src->bi_opf;
690cb6934f8SJens Axboe 	bio->bi_write_hint	= bio_src->bi_write_hint;
691f9c78b2bSJens Axboe 	bio->bi_iter.bi_sector	= bio_src->bi_iter.bi_sector;
692f9c78b2bSJens Axboe 	bio->bi_iter.bi_size	= bio_src->bi_iter.bi_size;
693f9c78b2bSJens Axboe 
6947afafc8aSAdrian Hunter 	switch (bio_op(bio)) {
6957afafc8aSAdrian Hunter 	case REQ_OP_DISCARD:
6967afafc8aSAdrian Hunter 	case REQ_OP_SECURE_ERASE:
697a6f0788eSChaitanya Kulkarni 	case REQ_OP_WRITE_ZEROES:
6987afafc8aSAdrian Hunter 		break;
6997afafc8aSAdrian Hunter 	case REQ_OP_WRITE_SAME:
700f9c78b2bSJens Axboe 		bio->bi_io_vec[bio->bi_vcnt++] = bio_src->bi_io_vec[0];
7017afafc8aSAdrian Hunter 		break;
7027afafc8aSAdrian Hunter 	default:
703f4595875SShaohua Li 		bio_for_each_segment(bv, bio_src, iter)
704f9c78b2bSJens Axboe 			bio->bi_io_vec[bio->bi_vcnt++] = bv;
7057afafc8aSAdrian Hunter 		break;
7067afafc8aSAdrian Hunter 	}
707f9c78b2bSJens Axboe 
708f9c78b2bSJens Axboe 	if (bio_integrity(bio_src)) {
709f9c78b2bSJens Axboe 		int ret;
710f9c78b2bSJens Axboe 
711f9c78b2bSJens Axboe 		ret = bio_integrity_clone(bio, bio_src, gfp_mask);
712f9c78b2bSJens Axboe 		if (ret < 0) {
713f9c78b2bSJens Axboe 			bio_put(bio);
714f9c78b2bSJens Axboe 			return NULL;
715f9c78b2bSJens Axboe 		}
716f9c78b2bSJens Axboe 	}
717f9c78b2bSJens Axboe 
71820bd723eSPaolo Valente 	bio_clone_blkcg_association(bio, bio_src);
71920bd723eSPaolo Valente 
720f9c78b2bSJens Axboe 	return bio;
721f9c78b2bSJens Axboe }
722f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_clone_bioset);
723f9c78b2bSJens Axboe 
724f9c78b2bSJens Axboe /**
725c66a14d0SKent Overstreet  *	bio_add_pc_page	-	attempt to add page to bio
726c66a14d0SKent Overstreet  *	@q: the target queue
727c66a14d0SKent Overstreet  *	@bio: destination bio
728c66a14d0SKent Overstreet  *	@page: page to add
729c66a14d0SKent Overstreet  *	@len: vec entry length
730c66a14d0SKent Overstreet  *	@offset: vec entry offset
731f9c78b2bSJens Axboe  *
732c66a14d0SKent Overstreet  *	Attempt to add a page to the bio_vec maplist. This can fail for a
733c66a14d0SKent Overstreet  *	number of reasons, such as the bio being full or target block device
734c66a14d0SKent Overstreet  *	limitations. The target block device must allow bio's up to PAGE_SIZE,
735c66a14d0SKent Overstreet  *	so it is always possible to add a single page to an empty bio.
736c66a14d0SKent Overstreet  *
737c66a14d0SKent Overstreet  *	This should only be used by REQ_PC bios.
738f9c78b2bSJens Axboe  */
739c66a14d0SKent Overstreet int bio_add_pc_page(struct request_queue *q, struct bio *bio, struct page
740c66a14d0SKent Overstreet 		    *page, unsigned int len, unsigned int offset)
741f9c78b2bSJens Axboe {
742f9c78b2bSJens Axboe 	int retried_segments = 0;
743f9c78b2bSJens Axboe 	struct bio_vec *bvec;
744f9c78b2bSJens Axboe 
745f9c78b2bSJens Axboe 	/*
746f9c78b2bSJens Axboe 	 * cloned bio must not modify vec list
747f9c78b2bSJens Axboe 	 */
748f9c78b2bSJens Axboe 	if (unlikely(bio_flagged(bio, BIO_CLONED)))
749f9c78b2bSJens Axboe 		return 0;
750f9c78b2bSJens Axboe 
751c66a14d0SKent Overstreet 	if (((bio->bi_iter.bi_size + len) >> 9) > queue_max_hw_sectors(q))
752f9c78b2bSJens Axboe 		return 0;
753f9c78b2bSJens Axboe 
754f9c78b2bSJens Axboe 	/*
755f9c78b2bSJens Axboe 	 * For filesystems with a blocksize smaller than the pagesize
756f9c78b2bSJens Axboe 	 * we will often be called with the same page as last time and
757f9c78b2bSJens Axboe 	 * a consecutive offset.  Optimize this special case.
758f9c78b2bSJens Axboe 	 */
759f9c78b2bSJens Axboe 	if (bio->bi_vcnt > 0) {
760f9c78b2bSJens Axboe 		struct bio_vec *prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
761f9c78b2bSJens Axboe 
762f9c78b2bSJens Axboe 		if (page == prev->bv_page &&
763f9c78b2bSJens Axboe 		    offset == prev->bv_offset + prev->bv_len) {
764f9c78b2bSJens Axboe 			prev->bv_len += len;
765fcbf6a08SMaurizio Lombardi 			bio->bi_iter.bi_size += len;
766f9c78b2bSJens Axboe 			goto done;
767f9c78b2bSJens Axboe 		}
76866cb45aaSJens Axboe 
76966cb45aaSJens Axboe 		/*
77066cb45aaSJens Axboe 		 * If the queue doesn't support SG gaps and adding this
77166cb45aaSJens Axboe 		 * offset would create a gap, disallow it.
77266cb45aaSJens Axboe 		 */
77303100aadSKeith Busch 		if (bvec_gap_to_prev(q, prev, offset))
77466cb45aaSJens Axboe 			return 0;
775f9c78b2bSJens Axboe 	}
776f9c78b2bSJens Axboe 
7770aa69fd3SChristoph Hellwig 	if (bio_full(bio))
778f9c78b2bSJens Axboe 		return 0;
779f9c78b2bSJens Axboe 
780f9c78b2bSJens Axboe 	/*
781f9c78b2bSJens Axboe 	 * setup the new entry, we might clear it again later if we
782f9c78b2bSJens Axboe 	 * cannot add the page
783f9c78b2bSJens Axboe 	 */
784f9c78b2bSJens Axboe 	bvec = &bio->bi_io_vec[bio->bi_vcnt];
785f9c78b2bSJens Axboe 	bvec->bv_page = page;
786f9c78b2bSJens Axboe 	bvec->bv_len = len;
787f9c78b2bSJens Axboe 	bvec->bv_offset = offset;
788fcbf6a08SMaurizio Lombardi 	bio->bi_vcnt++;
789fcbf6a08SMaurizio Lombardi 	bio->bi_phys_segments++;
790fcbf6a08SMaurizio Lombardi 	bio->bi_iter.bi_size += len;
791fcbf6a08SMaurizio Lombardi 
792fcbf6a08SMaurizio Lombardi 	/*
793fcbf6a08SMaurizio Lombardi 	 * Perform a recount if the number of segments is greater
794fcbf6a08SMaurizio Lombardi 	 * than queue_max_segments(q).
795fcbf6a08SMaurizio Lombardi 	 */
796fcbf6a08SMaurizio Lombardi 
797fcbf6a08SMaurizio Lombardi 	while (bio->bi_phys_segments > queue_max_segments(q)) {
798fcbf6a08SMaurizio Lombardi 
799fcbf6a08SMaurizio Lombardi 		if (retried_segments)
800fcbf6a08SMaurizio Lombardi 			goto failed;
801fcbf6a08SMaurizio Lombardi 
802fcbf6a08SMaurizio Lombardi 		retried_segments = 1;
803fcbf6a08SMaurizio Lombardi 		blk_recount_segments(q, bio);
804fcbf6a08SMaurizio Lombardi 	}
805f9c78b2bSJens Axboe 
806f9c78b2bSJens Axboe 	/* If we may be able to merge these biovecs, force a recount */
807fcbf6a08SMaurizio Lombardi 	if (bio->bi_vcnt > 1 && (BIOVEC_PHYS_MERGEABLE(bvec-1, bvec)))
808b7c44ed9SJens Axboe 		bio_clear_flag(bio, BIO_SEG_VALID);
809f9c78b2bSJens Axboe 
810f9c78b2bSJens Axboe  done:
811f9c78b2bSJens Axboe 	return len;
812fcbf6a08SMaurizio Lombardi 
813fcbf6a08SMaurizio Lombardi  failed:
814fcbf6a08SMaurizio Lombardi 	bvec->bv_page = NULL;
815fcbf6a08SMaurizio Lombardi 	bvec->bv_len = 0;
816fcbf6a08SMaurizio Lombardi 	bvec->bv_offset = 0;
817fcbf6a08SMaurizio Lombardi 	bio->bi_vcnt--;
818fcbf6a08SMaurizio Lombardi 	bio->bi_iter.bi_size -= len;
819fcbf6a08SMaurizio Lombardi 	blk_recount_segments(q, bio);
820fcbf6a08SMaurizio Lombardi 	return 0;
821f9c78b2bSJens Axboe }
822f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_add_pc_page);
823f9c78b2bSJens Axboe 
824f9c78b2bSJens Axboe /**
8250aa69fd3SChristoph Hellwig  * __bio_try_merge_page - try appending data to an existing bvec.
8260aa69fd3SChristoph Hellwig  * @bio: destination bio
8270aa69fd3SChristoph Hellwig  * @page: page to add
8280aa69fd3SChristoph Hellwig  * @len: length of the data to add
8290aa69fd3SChristoph Hellwig  * @off: offset of the data in @page
8300aa69fd3SChristoph Hellwig  *
8310aa69fd3SChristoph Hellwig  * Try to add the data at @page + @off to the last bvec of @bio.  This is a
8320aa69fd3SChristoph Hellwig  * a useful optimisation for file systems with a block size smaller than the
8330aa69fd3SChristoph Hellwig  * page size.
8340aa69fd3SChristoph Hellwig  *
8350aa69fd3SChristoph Hellwig  * Return %true on success or %false on failure.
8360aa69fd3SChristoph Hellwig  */
8370aa69fd3SChristoph Hellwig bool __bio_try_merge_page(struct bio *bio, struct page *page,
8380aa69fd3SChristoph Hellwig 		unsigned int len, unsigned int off)
8390aa69fd3SChristoph Hellwig {
8400aa69fd3SChristoph Hellwig 	if (WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED)))
8410aa69fd3SChristoph Hellwig 		return false;
8420aa69fd3SChristoph Hellwig 
8430aa69fd3SChristoph Hellwig 	if (bio->bi_vcnt > 0) {
8440aa69fd3SChristoph Hellwig 		struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt - 1];
8450aa69fd3SChristoph Hellwig 
8460aa69fd3SChristoph Hellwig 		if (page == bv->bv_page && off == bv->bv_offset + bv->bv_len) {
8470aa69fd3SChristoph Hellwig 			bv->bv_len += len;
8480aa69fd3SChristoph Hellwig 			bio->bi_iter.bi_size += len;
8490aa69fd3SChristoph Hellwig 			return true;
8500aa69fd3SChristoph Hellwig 		}
8510aa69fd3SChristoph Hellwig 	}
8520aa69fd3SChristoph Hellwig 	return false;
8530aa69fd3SChristoph Hellwig }
8540aa69fd3SChristoph Hellwig EXPORT_SYMBOL_GPL(__bio_try_merge_page);
8550aa69fd3SChristoph Hellwig 
8560aa69fd3SChristoph Hellwig /**
8570aa69fd3SChristoph Hellwig  * __bio_add_page - add page to a bio in a new segment
8580aa69fd3SChristoph Hellwig  * @bio: destination bio
8590aa69fd3SChristoph Hellwig  * @page: page to add
8600aa69fd3SChristoph Hellwig  * @len: length of the data to add
8610aa69fd3SChristoph Hellwig  * @off: offset of the data in @page
8620aa69fd3SChristoph Hellwig  *
8630aa69fd3SChristoph Hellwig  * Add the data at @page + @off to @bio as a new bvec.  The caller must ensure
8640aa69fd3SChristoph Hellwig  * that @bio has space for another bvec.
8650aa69fd3SChristoph Hellwig  */
8660aa69fd3SChristoph Hellwig void __bio_add_page(struct bio *bio, struct page *page,
8670aa69fd3SChristoph Hellwig 		unsigned int len, unsigned int off)
8680aa69fd3SChristoph Hellwig {
8690aa69fd3SChristoph Hellwig 	struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt];
8700aa69fd3SChristoph Hellwig 
8710aa69fd3SChristoph Hellwig 	WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
8720aa69fd3SChristoph Hellwig 	WARN_ON_ONCE(bio_full(bio));
8730aa69fd3SChristoph Hellwig 
8740aa69fd3SChristoph Hellwig 	bv->bv_page = page;
8750aa69fd3SChristoph Hellwig 	bv->bv_offset = off;
8760aa69fd3SChristoph Hellwig 	bv->bv_len = len;
8770aa69fd3SChristoph Hellwig 
8780aa69fd3SChristoph Hellwig 	bio->bi_iter.bi_size += len;
8790aa69fd3SChristoph Hellwig 	bio->bi_vcnt++;
8800aa69fd3SChristoph Hellwig }
8810aa69fd3SChristoph Hellwig EXPORT_SYMBOL_GPL(__bio_add_page);
8820aa69fd3SChristoph Hellwig 
8830aa69fd3SChristoph Hellwig /**
884f9c78b2bSJens Axboe  *	bio_add_page	-	attempt to add page to bio
885f9c78b2bSJens Axboe  *	@bio: destination bio
886f9c78b2bSJens Axboe  *	@page: page to add
887f9c78b2bSJens Axboe  *	@len: vec entry length
888f9c78b2bSJens Axboe  *	@offset: vec entry offset
889f9c78b2bSJens Axboe  *
890c66a14d0SKent Overstreet  *	Attempt to add a page to the bio_vec maplist. This will only fail
891c66a14d0SKent Overstreet  *	if either bio->bi_vcnt == bio->bi_max_vecs or it's a cloned bio.
892f9c78b2bSJens Axboe  */
893c66a14d0SKent Overstreet int bio_add_page(struct bio *bio, struct page *page,
894c66a14d0SKent Overstreet 		 unsigned int len, unsigned int offset)
895f9c78b2bSJens Axboe {
8960aa69fd3SChristoph Hellwig 	if (!__bio_try_merge_page(bio, page, len, offset)) {
8970aa69fd3SChristoph Hellwig 		if (bio_full(bio))
898c66a14d0SKent Overstreet 			return 0;
8990aa69fd3SChristoph Hellwig 		__bio_add_page(bio, page, len, offset);
900c66a14d0SKent Overstreet 	}
901c66a14d0SKent Overstreet 	return len;
902f9c78b2bSJens Axboe }
903f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_add_page);
904f9c78b2bSJens Axboe 
9052cefe4dbSKent Overstreet /**
90617d51b10SMartin Wilck  * __bio_iov_iter_get_pages - pin user or kernel pages and add them to a bio
9072cefe4dbSKent Overstreet  * @bio: bio to add pages to
9082cefe4dbSKent Overstreet  * @iter: iov iterator describing the region to be mapped
9092cefe4dbSKent Overstreet  *
91017d51b10SMartin Wilck  * Pins pages from *iter and appends them to @bio's bvec array. The
9112cefe4dbSKent Overstreet  * pages will have to be released using put_page() when done.
91217d51b10SMartin Wilck  * For multi-segment *iter, this function only adds pages from the
91317d51b10SMartin Wilck  * the next non-empty segment of the iov iterator.
9142cefe4dbSKent Overstreet  */
91517d51b10SMartin Wilck static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
9162cefe4dbSKent Overstreet {
917b403ea24SMartin Wilck 	unsigned short nr_pages = bio->bi_max_vecs - bio->bi_vcnt, idx;
9182cefe4dbSKent Overstreet 	struct bio_vec *bv = bio->bi_io_vec + bio->bi_vcnt;
9192cefe4dbSKent Overstreet 	struct page **pages = (struct page **)bv;
920b403ea24SMartin Wilck 	size_t offset;
9212cefe4dbSKent Overstreet 	ssize_t size;
9222cefe4dbSKent Overstreet 
9232cefe4dbSKent Overstreet 	size = iov_iter_get_pages(iter, pages, LONG_MAX, nr_pages, &offset);
9242cefe4dbSKent Overstreet 	if (unlikely(size <= 0))
9252cefe4dbSKent Overstreet 		return size ? size : -EFAULT;
926b403ea24SMartin Wilck 	idx = nr_pages = (size + offset + PAGE_SIZE - 1) / PAGE_SIZE;
9272cefe4dbSKent Overstreet 
9282cefe4dbSKent Overstreet 	/*
9292cefe4dbSKent Overstreet 	 * Deep magic below:  We need to walk the pinned pages backwards
9302cefe4dbSKent Overstreet 	 * because we are abusing the space allocated for the bio_vecs
9312cefe4dbSKent Overstreet 	 * for the page array.  Because the bio_vecs are larger than the
9322cefe4dbSKent Overstreet 	 * page pointers by definition this will always work.  But it also
9332cefe4dbSKent Overstreet 	 * means we can't use bio_add_page, so any changes to it's semantics
9342cefe4dbSKent Overstreet 	 * need to be reflected here as well.
9352cefe4dbSKent Overstreet 	 */
9362cefe4dbSKent Overstreet 	bio->bi_iter.bi_size += size;
9372cefe4dbSKent Overstreet 	bio->bi_vcnt += nr_pages;
9382cefe4dbSKent Overstreet 
939b403ea24SMartin Wilck 	while (idx--) {
940b403ea24SMartin Wilck 		bv[idx].bv_page = pages[idx];
941b403ea24SMartin Wilck 		bv[idx].bv_len = PAGE_SIZE;
942b403ea24SMartin Wilck 		bv[idx].bv_offset = 0;
9432cefe4dbSKent Overstreet 	}
9442cefe4dbSKent Overstreet 
9452cefe4dbSKent Overstreet 	bv[0].bv_offset += offset;
9462cefe4dbSKent Overstreet 	bv[0].bv_len -= offset;
947b403ea24SMartin Wilck 	bv[nr_pages - 1].bv_len -= nr_pages * PAGE_SIZE - offset - size;
9482cefe4dbSKent Overstreet 
9492cefe4dbSKent Overstreet 	iov_iter_advance(iter, size);
9502cefe4dbSKent Overstreet 	return 0;
9512cefe4dbSKent Overstreet }
95217d51b10SMartin Wilck 
95317d51b10SMartin Wilck /**
95417d51b10SMartin Wilck  * bio_iov_iter_get_pages - pin user or kernel pages and add them to a bio
95517d51b10SMartin Wilck  * @bio: bio to add pages to
95617d51b10SMartin Wilck  * @iter: iov iterator describing the region to be mapped
95717d51b10SMartin Wilck  *
95817d51b10SMartin Wilck  * Pins pages from *iter and appends them to @bio's bvec array. The
95917d51b10SMartin Wilck  * pages will have to be released using put_page() when done.
96017d51b10SMartin Wilck  * The function tries, but does not guarantee, to pin as many pages as
96117d51b10SMartin Wilck  * fit into the bio, or are requested in *iter, whatever is smaller.
96217d51b10SMartin Wilck  * If MM encounters an error pinning the requested pages, it stops.
96317d51b10SMartin Wilck  * Error is returned only if 0 pages could be pinned.
96417d51b10SMartin Wilck  */
96517d51b10SMartin Wilck int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
96617d51b10SMartin Wilck {
96717d51b10SMartin Wilck 	unsigned short orig_vcnt = bio->bi_vcnt;
96817d51b10SMartin Wilck 
96917d51b10SMartin Wilck 	do {
97017d51b10SMartin Wilck 		int ret = __bio_iov_iter_get_pages(bio, iter);
97117d51b10SMartin Wilck 
97217d51b10SMartin Wilck 		if (unlikely(ret))
97317d51b10SMartin Wilck 			return bio->bi_vcnt > orig_vcnt ? 0 : ret;
97417d51b10SMartin Wilck 
97517d51b10SMartin Wilck 	} while (iov_iter_count(iter) && !bio_full(bio));
97617d51b10SMartin Wilck 
97717d51b10SMartin Wilck 	return 0;
97817d51b10SMartin Wilck }
9792cefe4dbSKent Overstreet EXPORT_SYMBOL_GPL(bio_iov_iter_get_pages);
9802cefe4dbSKent Overstreet 
9814246a0b6SChristoph Hellwig static void submit_bio_wait_endio(struct bio *bio)
982f9c78b2bSJens Axboe {
98365e53aabSChristoph Hellwig 	complete(bio->bi_private);
984f9c78b2bSJens Axboe }
985f9c78b2bSJens Axboe 
986f9c78b2bSJens Axboe /**
987f9c78b2bSJens Axboe  * submit_bio_wait - submit a bio, and wait until it completes
988f9c78b2bSJens Axboe  * @bio: The &struct bio which describes the I/O
989f9c78b2bSJens Axboe  *
990f9c78b2bSJens Axboe  * Simple wrapper around submit_bio(). Returns 0 on success, or the error from
991f9c78b2bSJens Axboe  * bio_endio() on failure.
9923d289d68SJan Kara  *
9933d289d68SJan Kara  * WARNING: Unlike to how submit_bio() is usually used, this function does not
9943d289d68SJan Kara  * result in bio reference to be consumed. The caller must drop the reference
9953d289d68SJan Kara  * on his own.
996f9c78b2bSJens Axboe  */
9974e49ea4aSMike Christie int submit_bio_wait(struct bio *bio)
998f9c78b2bSJens Axboe {
999e319e1fbSByungchul Park 	DECLARE_COMPLETION_ONSTACK_MAP(done, bio->bi_disk->lockdep_map);
1000f9c78b2bSJens Axboe 
100165e53aabSChristoph Hellwig 	bio->bi_private = &done;
1002f9c78b2bSJens Axboe 	bio->bi_end_io = submit_bio_wait_endio;
10031eff9d32SJens Axboe 	bio->bi_opf |= REQ_SYNC;
10044e49ea4aSMike Christie 	submit_bio(bio);
100565e53aabSChristoph Hellwig 	wait_for_completion_io(&done);
1006f9c78b2bSJens Axboe 
100765e53aabSChristoph Hellwig 	return blk_status_to_errno(bio->bi_status);
1008f9c78b2bSJens Axboe }
1009f9c78b2bSJens Axboe EXPORT_SYMBOL(submit_bio_wait);
1010f9c78b2bSJens Axboe 
1011f9c78b2bSJens Axboe /**
1012f9c78b2bSJens Axboe  * bio_advance - increment/complete a bio by some number of bytes
1013f9c78b2bSJens Axboe  * @bio:	bio to advance
1014f9c78b2bSJens Axboe  * @bytes:	number of bytes to complete
1015f9c78b2bSJens Axboe  *
1016f9c78b2bSJens Axboe  * This updates bi_sector, bi_size and bi_idx; if the number of bytes to
1017f9c78b2bSJens Axboe  * complete doesn't align with a bvec boundary, then bv_len and bv_offset will
1018f9c78b2bSJens Axboe  * be updated on the last bvec as well.
1019f9c78b2bSJens Axboe  *
1020f9c78b2bSJens Axboe  * @bio will then represent the remaining, uncompleted portion of the io.
1021f9c78b2bSJens Axboe  */
1022f9c78b2bSJens Axboe void bio_advance(struct bio *bio, unsigned bytes)
1023f9c78b2bSJens Axboe {
1024f9c78b2bSJens Axboe 	if (bio_integrity(bio))
1025f9c78b2bSJens Axboe 		bio_integrity_advance(bio, bytes);
1026f9c78b2bSJens Axboe 
1027f9c78b2bSJens Axboe 	bio_advance_iter(bio, &bio->bi_iter, bytes);
1028f9c78b2bSJens Axboe }
1029f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_advance);
1030f9c78b2bSJens Axboe 
103145db54d5SKent Overstreet void bio_copy_data_iter(struct bio *dst, struct bvec_iter *dst_iter,
103245db54d5SKent Overstreet 			struct bio *src, struct bvec_iter *src_iter)
1033f9c78b2bSJens Axboe {
1034f9c78b2bSJens Axboe 	struct bio_vec src_bv, dst_bv;
1035f9c78b2bSJens Axboe 	void *src_p, *dst_p;
1036f9c78b2bSJens Axboe 	unsigned bytes;
1037f9c78b2bSJens Axboe 
103845db54d5SKent Overstreet 	while (src_iter->bi_size && dst_iter->bi_size) {
103945db54d5SKent Overstreet 		src_bv = bio_iter_iovec(src, *src_iter);
104045db54d5SKent Overstreet 		dst_bv = bio_iter_iovec(dst, *dst_iter);
104145db54d5SKent Overstreet 
104245db54d5SKent Overstreet 		bytes = min(src_bv.bv_len, dst_bv.bv_len);
104345db54d5SKent Overstreet 
104445db54d5SKent Overstreet 		src_p = kmap_atomic(src_bv.bv_page);
104545db54d5SKent Overstreet 		dst_p = kmap_atomic(dst_bv.bv_page);
104645db54d5SKent Overstreet 
104745db54d5SKent Overstreet 		memcpy(dst_p + dst_bv.bv_offset,
104845db54d5SKent Overstreet 		       src_p + src_bv.bv_offset,
104945db54d5SKent Overstreet 		       bytes);
105045db54d5SKent Overstreet 
105145db54d5SKent Overstreet 		kunmap_atomic(dst_p);
105245db54d5SKent Overstreet 		kunmap_atomic(src_p);
105345db54d5SKent Overstreet 
10546e6e811dSKent Overstreet 		flush_dcache_page(dst_bv.bv_page);
10556e6e811dSKent Overstreet 
105645db54d5SKent Overstreet 		bio_advance_iter(src, src_iter, bytes);
105745db54d5SKent Overstreet 		bio_advance_iter(dst, dst_iter, bytes);
105845db54d5SKent Overstreet 	}
105945db54d5SKent Overstreet }
106045db54d5SKent Overstreet EXPORT_SYMBOL(bio_copy_data_iter);
106145db54d5SKent Overstreet 
106245db54d5SKent Overstreet /**
106345db54d5SKent Overstreet  * bio_copy_data - copy contents of data buffers from one bio to another
106445db54d5SKent Overstreet  * @src: source bio
106545db54d5SKent Overstreet  * @dst: destination bio
106645db54d5SKent Overstreet  *
106745db54d5SKent Overstreet  * Stops when it reaches the end of either @src or @dst - that is, copies
106845db54d5SKent Overstreet  * min(src->bi_size, dst->bi_size) bytes (or the equivalent for lists of bios).
106945db54d5SKent Overstreet  */
107045db54d5SKent Overstreet void bio_copy_data(struct bio *dst, struct bio *src)
107145db54d5SKent Overstreet {
107245db54d5SKent Overstreet 	struct bvec_iter src_iter = src->bi_iter;
107345db54d5SKent Overstreet 	struct bvec_iter dst_iter = dst->bi_iter;
107445db54d5SKent Overstreet 
107545db54d5SKent Overstreet 	bio_copy_data_iter(dst, &dst_iter, src, &src_iter);
107645db54d5SKent Overstreet }
107745db54d5SKent Overstreet EXPORT_SYMBOL(bio_copy_data);
107845db54d5SKent Overstreet 
107945db54d5SKent Overstreet /**
108045db54d5SKent Overstreet  * bio_list_copy_data - copy contents of data buffers from one chain of bios to
108145db54d5SKent Overstreet  * another
108245db54d5SKent Overstreet  * @src: source bio list
108345db54d5SKent Overstreet  * @dst: destination bio list
108445db54d5SKent Overstreet  *
108545db54d5SKent Overstreet  * Stops when it reaches the end of either the @src list or @dst list - that is,
108645db54d5SKent Overstreet  * copies min(src->bi_size, dst->bi_size) bytes (or the equivalent for lists of
108745db54d5SKent Overstreet  * bios).
108845db54d5SKent Overstreet  */
108945db54d5SKent Overstreet void bio_list_copy_data(struct bio *dst, struct bio *src)
109045db54d5SKent Overstreet {
109145db54d5SKent Overstreet 	struct bvec_iter src_iter = src->bi_iter;
109245db54d5SKent Overstreet 	struct bvec_iter dst_iter = dst->bi_iter;
109345db54d5SKent Overstreet 
1094f9c78b2bSJens Axboe 	while (1) {
1095f9c78b2bSJens Axboe 		if (!src_iter.bi_size) {
1096f9c78b2bSJens Axboe 			src = src->bi_next;
1097f9c78b2bSJens Axboe 			if (!src)
1098f9c78b2bSJens Axboe 				break;
1099f9c78b2bSJens Axboe 
1100f9c78b2bSJens Axboe 			src_iter = src->bi_iter;
1101f9c78b2bSJens Axboe 		}
1102f9c78b2bSJens Axboe 
1103f9c78b2bSJens Axboe 		if (!dst_iter.bi_size) {
1104f9c78b2bSJens Axboe 			dst = dst->bi_next;
1105f9c78b2bSJens Axboe 			if (!dst)
1106f9c78b2bSJens Axboe 				break;
1107f9c78b2bSJens Axboe 
1108f9c78b2bSJens Axboe 			dst_iter = dst->bi_iter;
1109f9c78b2bSJens Axboe 		}
1110f9c78b2bSJens Axboe 
111145db54d5SKent Overstreet 		bio_copy_data_iter(dst, &dst_iter, src, &src_iter);
1112f9c78b2bSJens Axboe 	}
1113f9c78b2bSJens Axboe }
111445db54d5SKent Overstreet EXPORT_SYMBOL(bio_list_copy_data);
1115f9c78b2bSJens Axboe 
1116f9c78b2bSJens Axboe struct bio_map_data {
1117f9c78b2bSJens Axboe 	int is_our_pages;
111826e49cfcSKent Overstreet 	struct iov_iter iter;
111926e49cfcSKent Overstreet 	struct iovec iov[];
1120f9c78b2bSJens Axboe };
1121f9c78b2bSJens Axboe 
11220e5b935dSAl Viro static struct bio_map_data *bio_alloc_map_data(struct iov_iter *data,
1123f9c78b2bSJens Axboe 					       gfp_t gfp_mask)
1124f9c78b2bSJens Axboe {
11250e5b935dSAl Viro 	struct bio_map_data *bmd;
11260e5b935dSAl Viro 	if (data->nr_segs > UIO_MAXIOV)
1127f9c78b2bSJens Axboe 		return NULL;
1128f9c78b2bSJens Axboe 
11290e5b935dSAl Viro 	bmd = kmalloc(sizeof(struct bio_map_data) +
11300e5b935dSAl Viro 		       sizeof(struct iovec) * data->nr_segs, gfp_mask);
11310e5b935dSAl Viro 	if (!bmd)
11320e5b935dSAl Viro 		return NULL;
11330e5b935dSAl Viro 	memcpy(bmd->iov, data->iov, sizeof(struct iovec) * data->nr_segs);
11340e5b935dSAl Viro 	bmd->iter = *data;
11350e5b935dSAl Viro 	bmd->iter.iov = bmd->iov;
11360e5b935dSAl Viro 	return bmd;
1137f9c78b2bSJens Axboe }
1138f9c78b2bSJens Axboe 
11399124d3feSDongsu Park /**
11409124d3feSDongsu Park  * bio_copy_from_iter - copy all pages from iov_iter to bio
11419124d3feSDongsu Park  * @bio: The &struct bio which describes the I/O as destination
11429124d3feSDongsu Park  * @iter: iov_iter as source
11439124d3feSDongsu Park  *
11449124d3feSDongsu Park  * Copy all pages from iov_iter to bio.
11459124d3feSDongsu Park  * Returns 0 on success, or error on failure.
11469124d3feSDongsu Park  */
114798a09d61SAl Viro static int bio_copy_from_iter(struct bio *bio, struct iov_iter *iter)
1148f9c78b2bSJens Axboe {
11499124d3feSDongsu Park 	int i;
1150f9c78b2bSJens Axboe 	struct bio_vec *bvec;
1151f9c78b2bSJens Axboe 
1152f9c78b2bSJens Axboe 	bio_for_each_segment_all(bvec, bio, i) {
11539124d3feSDongsu Park 		ssize_t ret;
1154f9c78b2bSJens Axboe 
11559124d3feSDongsu Park 		ret = copy_page_from_iter(bvec->bv_page,
11569124d3feSDongsu Park 					  bvec->bv_offset,
11579124d3feSDongsu Park 					  bvec->bv_len,
115898a09d61SAl Viro 					  iter);
1159f9c78b2bSJens Axboe 
116098a09d61SAl Viro 		if (!iov_iter_count(iter))
11619124d3feSDongsu Park 			break;
1162f9c78b2bSJens Axboe 
11639124d3feSDongsu Park 		if (ret < bvec->bv_len)
11649124d3feSDongsu Park 			return -EFAULT;
1165f9c78b2bSJens Axboe 	}
1166f9c78b2bSJens Axboe 
11679124d3feSDongsu Park 	return 0;
1168f9c78b2bSJens Axboe }
1169f9c78b2bSJens Axboe 
11709124d3feSDongsu Park /**
11719124d3feSDongsu Park  * bio_copy_to_iter - copy all pages from bio to iov_iter
11729124d3feSDongsu Park  * @bio: The &struct bio which describes the I/O as source
11739124d3feSDongsu Park  * @iter: iov_iter as destination
11749124d3feSDongsu Park  *
11759124d3feSDongsu Park  * Copy all pages from bio to iov_iter.
11769124d3feSDongsu Park  * Returns 0 on success, or error on failure.
11779124d3feSDongsu Park  */
11789124d3feSDongsu Park static int bio_copy_to_iter(struct bio *bio, struct iov_iter iter)
11799124d3feSDongsu Park {
11809124d3feSDongsu Park 	int i;
11819124d3feSDongsu Park 	struct bio_vec *bvec;
11829124d3feSDongsu Park 
11839124d3feSDongsu Park 	bio_for_each_segment_all(bvec, bio, i) {
11849124d3feSDongsu Park 		ssize_t ret;
11859124d3feSDongsu Park 
11869124d3feSDongsu Park 		ret = copy_page_to_iter(bvec->bv_page,
11879124d3feSDongsu Park 					bvec->bv_offset,
11889124d3feSDongsu Park 					bvec->bv_len,
11899124d3feSDongsu Park 					&iter);
11909124d3feSDongsu Park 
11919124d3feSDongsu Park 		if (!iov_iter_count(&iter))
11929124d3feSDongsu Park 			break;
11939124d3feSDongsu Park 
11949124d3feSDongsu Park 		if (ret < bvec->bv_len)
11959124d3feSDongsu Park 			return -EFAULT;
11969124d3feSDongsu Park 	}
11979124d3feSDongsu Park 
11989124d3feSDongsu Park 	return 0;
1199f9c78b2bSJens Axboe }
1200f9c78b2bSJens Axboe 
1201491221f8SGuoqing Jiang void bio_free_pages(struct bio *bio)
12021dfa0f68SChristoph Hellwig {
12031dfa0f68SChristoph Hellwig 	struct bio_vec *bvec;
12041dfa0f68SChristoph Hellwig 	int i;
12051dfa0f68SChristoph Hellwig 
12061dfa0f68SChristoph Hellwig 	bio_for_each_segment_all(bvec, bio, i)
12071dfa0f68SChristoph Hellwig 		__free_page(bvec->bv_page);
12081dfa0f68SChristoph Hellwig }
1209491221f8SGuoqing Jiang EXPORT_SYMBOL(bio_free_pages);
12101dfa0f68SChristoph Hellwig 
1211f9c78b2bSJens Axboe /**
1212f9c78b2bSJens Axboe  *	bio_uncopy_user	-	finish previously mapped bio
1213f9c78b2bSJens Axboe  *	@bio: bio being terminated
1214f9c78b2bSJens Axboe  *
1215ddad8dd0SChristoph Hellwig  *	Free pages allocated from bio_copy_user_iov() and write back data
1216f9c78b2bSJens Axboe  *	to user space in case of a read.
1217f9c78b2bSJens Axboe  */
1218f9c78b2bSJens Axboe int bio_uncopy_user(struct bio *bio)
1219f9c78b2bSJens Axboe {
1220f9c78b2bSJens Axboe 	struct bio_map_data *bmd = bio->bi_private;
12211dfa0f68SChristoph Hellwig 	int ret = 0;
1222f9c78b2bSJens Axboe 
1223f9c78b2bSJens Axboe 	if (!bio_flagged(bio, BIO_NULL_MAPPED)) {
1224f9c78b2bSJens Axboe 		/*
1225f9c78b2bSJens Axboe 		 * if we're in a workqueue, the request is orphaned, so
12262d99b55dSHannes Reinecke 		 * don't copy into a random user address space, just free
12272d99b55dSHannes Reinecke 		 * and return -EINTR so user space doesn't expect any data.
1228f9c78b2bSJens Axboe 		 */
12292d99b55dSHannes Reinecke 		if (!current->mm)
12302d99b55dSHannes Reinecke 			ret = -EINTR;
12312d99b55dSHannes Reinecke 		else if (bio_data_dir(bio) == READ)
12329124d3feSDongsu Park 			ret = bio_copy_to_iter(bio, bmd->iter);
12331dfa0f68SChristoph Hellwig 		if (bmd->is_our_pages)
12341dfa0f68SChristoph Hellwig 			bio_free_pages(bio);
1235f9c78b2bSJens Axboe 	}
1236f9c78b2bSJens Axboe 	kfree(bmd);
1237f9c78b2bSJens Axboe 	bio_put(bio);
1238f9c78b2bSJens Axboe 	return ret;
1239f9c78b2bSJens Axboe }
1240f9c78b2bSJens Axboe 
1241f9c78b2bSJens Axboe /**
1242f9c78b2bSJens Axboe  *	bio_copy_user_iov	-	copy user data to bio
1243f9c78b2bSJens Axboe  *	@q:		destination block queue
1244f9c78b2bSJens Axboe  *	@map_data:	pointer to the rq_map_data holding pages (if necessary)
124526e49cfcSKent Overstreet  *	@iter:		iovec iterator
1246f9c78b2bSJens Axboe  *	@gfp_mask:	memory allocation flags
1247f9c78b2bSJens Axboe  *
1248f9c78b2bSJens Axboe  *	Prepares and returns a bio for indirect user io, bouncing data
1249f9c78b2bSJens Axboe  *	to/from kernel pages as necessary. Must be paired with
1250f9c78b2bSJens Axboe  *	call bio_uncopy_user() on io completion.
1251f9c78b2bSJens Axboe  */
1252f9c78b2bSJens Axboe struct bio *bio_copy_user_iov(struct request_queue *q,
1253f9c78b2bSJens Axboe 			      struct rq_map_data *map_data,
1254e81cef5dSAl Viro 			      struct iov_iter *iter,
125526e49cfcSKent Overstreet 			      gfp_t gfp_mask)
1256f9c78b2bSJens Axboe {
1257f9c78b2bSJens Axboe 	struct bio_map_data *bmd;
1258f9c78b2bSJens Axboe 	struct page *page;
1259f9c78b2bSJens Axboe 	struct bio *bio;
1260d16d44ebSAl Viro 	int i = 0, ret;
1261d16d44ebSAl Viro 	int nr_pages;
126226e49cfcSKent Overstreet 	unsigned int len = iter->count;
1263bd5ceceaSGeliang Tang 	unsigned int offset = map_data ? offset_in_page(map_data->offset) : 0;
1264f9c78b2bSJens Axboe 
12650e5b935dSAl Viro 	bmd = bio_alloc_map_data(iter, gfp_mask);
1266f9c78b2bSJens Axboe 	if (!bmd)
1267f9c78b2bSJens Axboe 		return ERR_PTR(-ENOMEM);
1268f9c78b2bSJens Axboe 
126926e49cfcSKent Overstreet 	/*
127026e49cfcSKent Overstreet 	 * We need to do a deep copy of the iov_iter including the iovecs.
127126e49cfcSKent Overstreet 	 * The caller provided iov might point to an on-stack or otherwise
127226e49cfcSKent Overstreet 	 * shortlived one.
127326e49cfcSKent Overstreet 	 */
127426e49cfcSKent Overstreet 	bmd->is_our_pages = map_data ? 0 : 1;
127526e49cfcSKent Overstreet 
1276d16d44ebSAl Viro 	nr_pages = DIV_ROUND_UP(offset + len, PAGE_SIZE);
1277d16d44ebSAl Viro 	if (nr_pages > BIO_MAX_PAGES)
1278d16d44ebSAl Viro 		nr_pages = BIO_MAX_PAGES;
1279f9c78b2bSJens Axboe 
1280f9c78b2bSJens Axboe 	ret = -ENOMEM;
1281f9c78b2bSJens Axboe 	bio = bio_kmalloc(gfp_mask, nr_pages);
1282f9c78b2bSJens Axboe 	if (!bio)
1283f9c78b2bSJens Axboe 		goto out_bmd;
1284f9c78b2bSJens Axboe 
1285f9c78b2bSJens Axboe 	ret = 0;
1286f9c78b2bSJens Axboe 
1287f9c78b2bSJens Axboe 	if (map_data) {
1288f9c78b2bSJens Axboe 		nr_pages = 1 << map_data->page_order;
1289f9c78b2bSJens Axboe 		i = map_data->offset / PAGE_SIZE;
1290f9c78b2bSJens Axboe 	}
1291f9c78b2bSJens Axboe 	while (len) {
1292f9c78b2bSJens Axboe 		unsigned int bytes = PAGE_SIZE;
1293f9c78b2bSJens Axboe 
1294f9c78b2bSJens Axboe 		bytes -= offset;
1295f9c78b2bSJens Axboe 
1296f9c78b2bSJens Axboe 		if (bytes > len)
1297f9c78b2bSJens Axboe 			bytes = len;
1298f9c78b2bSJens Axboe 
1299f9c78b2bSJens Axboe 		if (map_data) {
1300f9c78b2bSJens Axboe 			if (i == map_data->nr_entries * nr_pages) {
1301f9c78b2bSJens Axboe 				ret = -ENOMEM;
1302f9c78b2bSJens Axboe 				break;
1303f9c78b2bSJens Axboe 			}
1304f9c78b2bSJens Axboe 
1305f9c78b2bSJens Axboe 			page = map_data->pages[i / nr_pages];
1306f9c78b2bSJens Axboe 			page += (i % nr_pages);
1307f9c78b2bSJens Axboe 
1308f9c78b2bSJens Axboe 			i++;
1309f9c78b2bSJens Axboe 		} else {
1310f9c78b2bSJens Axboe 			page = alloc_page(q->bounce_gfp | gfp_mask);
1311f9c78b2bSJens Axboe 			if (!page) {
1312f9c78b2bSJens Axboe 				ret = -ENOMEM;
1313f9c78b2bSJens Axboe 				break;
1314f9c78b2bSJens Axboe 			}
1315f9c78b2bSJens Axboe 		}
1316f9c78b2bSJens Axboe 
1317f9c78b2bSJens Axboe 		if (bio_add_pc_page(q, bio, page, bytes, offset) < bytes)
1318f9c78b2bSJens Axboe 			break;
1319f9c78b2bSJens Axboe 
1320f9c78b2bSJens Axboe 		len -= bytes;
1321f9c78b2bSJens Axboe 		offset = 0;
1322f9c78b2bSJens Axboe 	}
1323f9c78b2bSJens Axboe 
1324f9c78b2bSJens Axboe 	if (ret)
1325f9c78b2bSJens Axboe 		goto cleanup;
1326f9c78b2bSJens Axboe 
13272884d0beSAl Viro 	if (map_data)
13282884d0beSAl Viro 		map_data->offset += bio->bi_iter.bi_size;
13292884d0beSAl Viro 
1330f9c78b2bSJens Axboe 	/*
1331f9c78b2bSJens Axboe 	 * success
1332f9c78b2bSJens Axboe 	 */
133326e49cfcSKent Overstreet 	if (((iter->type & WRITE) && (!map_data || !map_data->null_mapped)) ||
1334f9c78b2bSJens Axboe 	    (map_data && map_data->from_user)) {
133598a09d61SAl Viro 		ret = bio_copy_from_iter(bio, iter);
1336f9c78b2bSJens Axboe 		if (ret)
1337f9c78b2bSJens Axboe 			goto cleanup;
133898a09d61SAl Viro 	} else {
1339e81cef5dSAl Viro 		iov_iter_advance(iter, bio->bi_iter.bi_size);
1340f9c78b2bSJens Axboe 	}
1341f9c78b2bSJens Axboe 
134226e49cfcSKent Overstreet 	bio->bi_private = bmd;
13432884d0beSAl Viro 	if (map_data && map_data->null_mapped)
13442884d0beSAl Viro 		bio_set_flag(bio, BIO_NULL_MAPPED);
1345f9c78b2bSJens Axboe 	return bio;
1346f9c78b2bSJens Axboe cleanup:
1347f9c78b2bSJens Axboe 	if (!map_data)
13481dfa0f68SChristoph Hellwig 		bio_free_pages(bio);
1349f9c78b2bSJens Axboe 	bio_put(bio);
1350f9c78b2bSJens Axboe out_bmd:
1351f9c78b2bSJens Axboe 	kfree(bmd);
1352f9c78b2bSJens Axboe 	return ERR_PTR(ret);
1353f9c78b2bSJens Axboe }
1354f9c78b2bSJens Axboe 
135537f19e57SChristoph Hellwig /**
135637f19e57SChristoph Hellwig  *	bio_map_user_iov - map user iovec into bio
135737f19e57SChristoph Hellwig  *	@q:		the struct request_queue for the bio
135837f19e57SChristoph Hellwig  *	@iter:		iovec iterator
135937f19e57SChristoph Hellwig  *	@gfp_mask:	memory allocation flags
136037f19e57SChristoph Hellwig  *
136137f19e57SChristoph Hellwig  *	Map the user space address into a bio suitable for io to a block
136237f19e57SChristoph Hellwig  *	device. Returns an error pointer in case of error.
136337f19e57SChristoph Hellwig  */
136437f19e57SChristoph Hellwig struct bio *bio_map_user_iov(struct request_queue *q,
1365e81cef5dSAl Viro 			     struct iov_iter *iter,
136626e49cfcSKent Overstreet 			     gfp_t gfp_mask)
1367f9c78b2bSJens Axboe {
136826e49cfcSKent Overstreet 	int j;
1369f9c78b2bSJens Axboe 	struct bio *bio;
1370076098e5SAl Viro 	int ret;
13712b04e8f6SAl Viro 	struct bio_vec *bvec;
1372f9c78b2bSJens Axboe 
1373b282cc76SAl Viro 	if (!iov_iter_count(iter))
1374f9c78b2bSJens Axboe 		return ERR_PTR(-EINVAL);
1375f9c78b2bSJens Axboe 
1376b282cc76SAl Viro 	bio = bio_kmalloc(gfp_mask, iov_iter_npages(iter, BIO_MAX_PAGES));
1377f9c78b2bSJens Axboe 	if (!bio)
1378f9c78b2bSJens Axboe 		return ERR_PTR(-ENOMEM);
1379f9c78b2bSJens Axboe 
13800a0f1513SAl Viro 	while (iov_iter_count(iter)) {
1381629e42bcSAl Viro 		struct page **pages;
1382076098e5SAl Viro 		ssize_t bytes;
1383076098e5SAl Viro 		size_t offs, added = 0;
1384076098e5SAl Viro 		int npages;
1385f9c78b2bSJens Axboe 
13860a0f1513SAl Viro 		bytes = iov_iter_get_pages_alloc(iter, &pages, LONG_MAX, &offs);
1387076098e5SAl Viro 		if (unlikely(bytes <= 0)) {
1388076098e5SAl Viro 			ret = bytes ? bytes : -EFAULT;
1389f9c78b2bSJens Axboe 			goto out_unmap;
1390f9c78b2bSJens Axboe 		}
1391f9c78b2bSJens Axboe 
1392076098e5SAl Viro 		npages = DIV_ROUND_UP(offs + bytes, PAGE_SIZE);
1393076098e5SAl Viro 
139498f0bc99SAl Viro 		if (unlikely(offs & queue_dma_alignment(q))) {
139598f0bc99SAl Viro 			ret = -EINVAL;
139698f0bc99SAl Viro 			j = 0;
139798f0bc99SAl Viro 		} else {
1398629e42bcSAl Viro 			for (j = 0; j < npages; j++) {
139998f0bc99SAl Viro 				struct page *page = pages[j];
1400076098e5SAl Viro 				unsigned int n = PAGE_SIZE - offs;
140195d78c28SVitaly Mayatskikh 				unsigned short prev_bi_vcnt = bio->bi_vcnt;
1402f9c78b2bSJens Axboe 
1403076098e5SAl Viro 				if (n > bytes)
1404076098e5SAl Viro 					n = bytes;
1405f9c78b2bSJens Axboe 
140698f0bc99SAl Viro 				if (!bio_add_pc_page(q, bio, page, n, offs))
1407f9c78b2bSJens Axboe 					break;
1408f9c78b2bSJens Axboe 
140995d78c28SVitaly Mayatskikh 				/*
141095d78c28SVitaly Mayatskikh 				 * check if vector was merged with previous
141195d78c28SVitaly Mayatskikh 				 * drop page reference if needed
141295d78c28SVitaly Mayatskikh 				 */
141395d78c28SVitaly Mayatskikh 				if (bio->bi_vcnt == prev_bi_vcnt)
141498f0bc99SAl Viro 					put_page(page);
141595d78c28SVitaly Mayatskikh 
1416076098e5SAl Viro 				added += n;
1417076098e5SAl Viro 				bytes -= n;
1418076098e5SAl Viro 				offs = 0;
1419f9c78b2bSJens Axboe 			}
14200a0f1513SAl Viro 			iov_iter_advance(iter, added);
142198f0bc99SAl Viro 		}
1422f9c78b2bSJens Axboe 		/*
1423f9c78b2bSJens Axboe 		 * release the pages we didn't map into the bio, if any
1424f9c78b2bSJens Axboe 		 */
1425629e42bcSAl Viro 		while (j < npages)
142609cbfeafSKirill A. Shutemov 			put_page(pages[j++]);
1427629e42bcSAl Viro 		kvfree(pages);
1428e2e115d1SAl Viro 		/* couldn't stuff something into bio? */
1429e2e115d1SAl Viro 		if (bytes)
1430e2e115d1SAl Viro 			break;
1431f9c78b2bSJens Axboe 	}
1432f9c78b2bSJens Axboe 
1433b7c44ed9SJens Axboe 	bio_set_flag(bio, BIO_USER_MAPPED);
143437f19e57SChristoph Hellwig 
143537f19e57SChristoph Hellwig 	/*
14365fad1b64SBart Van Assche 	 * subtle -- if bio_map_user_iov() ended up bouncing a bio,
143737f19e57SChristoph Hellwig 	 * it would normally disappear when its bi_end_io is run.
143837f19e57SChristoph Hellwig 	 * however, we need it for the unmap, so grab an extra
143937f19e57SChristoph Hellwig 	 * reference to it
144037f19e57SChristoph Hellwig 	 */
144137f19e57SChristoph Hellwig 	bio_get(bio);
1442f9c78b2bSJens Axboe 	return bio;
1443f9c78b2bSJens Axboe 
1444f9c78b2bSJens Axboe  out_unmap:
14452b04e8f6SAl Viro 	bio_for_each_segment_all(bvec, bio, j) {
14462b04e8f6SAl Viro 		put_page(bvec->bv_page);
1447f9c78b2bSJens Axboe 	}
1448f9c78b2bSJens Axboe 	bio_put(bio);
1449f9c78b2bSJens Axboe 	return ERR_PTR(ret);
1450f9c78b2bSJens Axboe }
1451f9c78b2bSJens Axboe 
1452f9c78b2bSJens Axboe static void __bio_unmap_user(struct bio *bio)
1453f9c78b2bSJens Axboe {
1454f9c78b2bSJens Axboe 	struct bio_vec *bvec;
1455f9c78b2bSJens Axboe 	int i;
1456f9c78b2bSJens Axboe 
1457f9c78b2bSJens Axboe 	/*
1458f9c78b2bSJens Axboe 	 * make sure we dirty pages we wrote to
1459f9c78b2bSJens Axboe 	 */
1460f9c78b2bSJens Axboe 	bio_for_each_segment_all(bvec, bio, i) {
1461f9c78b2bSJens Axboe 		if (bio_data_dir(bio) == READ)
1462f9c78b2bSJens Axboe 			set_page_dirty_lock(bvec->bv_page);
1463f9c78b2bSJens Axboe 
146409cbfeafSKirill A. Shutemov 		put_page(bvec->bv_page);
1465f9c78b2bSJens Axboe 	}
1466f9c78b2bSJens Axboe 
1467f9c78b2bSJens Axboe 	bio_put(bio);
1468f9c78b2bSJens Axboe }
1469f9c78b2bSJens Axboe 
1470f9c78b2bSJens Axboe /**
1471f9c78b2bSJens Axboe  *	bio_unmap_user	-	unmap a bio
1472f9c78b2bSJens Axboe  *	@bio:		the bio being unmapped
1473f9c78b2bSJens Axboe  *
14745fad1b64SBart Van Assche  *	Unmap a bio previously mapped by bio_map_user_iov(). Must be called from
14755fad1b64SBart Van Assche  *	process context.
1476f9c78b2bSJens Axboe  *
1477f9c78b2bSJens Axboe  *	bio_unmap_user() may sleep.
1478f9c78b2bSJens Axboe  */
1479f9c78b2bSJens Axboe void bio_unmap_user(struct bio *bio)
1480f9c78b2bSJens Axboe {
1481f9c78b2bSJens Axboe 	__bio_unmap_user(bio);
1482f9c78b2bSJens Axboe 	bio_put(bio);
1483f9c78b2bSJens Axboe }
1484f9c78b2bSJens Axboe 
14854246a0b6SChristoph Hellwig static void bio_map_kern_endio(struct bio *bio)
1486f9c78b2bSJens Axboe {
1487f9c78b2bSJens Axboe 	bio_put(bio);
1488f9c78b2bSJens Axboe }
1489f9c78b2bSJens Axboe 
149075c72b83SChristoph Hellwig /**
149175c72b83SChristoph Hellwig  *	bio_map_kern	-	map kernel address into bio
149275c72b83SChristoph Hellwig  *	@q: the struct request_queue for the bio
149375c72b83SChristoph Hellwig  *	@data: pointer to buffer to map
149475c72b83SChristoph Hellwig  *	@len: length in bytes
149575c72b83SChristoph Hellwig  *	@gfp_mask: allocation flags for bio allocation
149675c72b83SChristoph Hellwig  *
149775c72b83SChristoph Hellwig  *	Map the kernel address into a bio suitable for io to a block
149875c72b83SChristoph Hellwig  *	device. Returns an error pointer in case of error.
149975c72b83SChristoph Hellwig  */
150075c72b83SChristoph Hellwig struct bio *bio_map_kern(struct request_queue *q, void *data, unsigned int len,
150175c72b83SChristoph Hellwig 			 gfp_t gfp_mask)
1502f9c78b2bSJens Axboe {
1503f9c78b2bSJens Axboe 	unsigned long kaddr = (unsigned long)data;
1504f9c78b2bSJens Axboe 	unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1505f9c78b2bSJens Axboe 	unsigned long start = kaddr >> PAGE_SHIFT;
1506f9c78b2bSJens Axboe 	const int nr_pages = end - start;
1507f9c78b2bSJens Axboe 	int offset, i;
1508f9c78b2bSJens Axboe 	struct bio *bio;
1509f9c78b2bSJens Axboe 
1510f9c78b2bSJens Axboe 	bio = bio_kmalloc(gfp_mask, nr_pages);
1511f9c78b2bSJens Axboe 	if (!bio)
1512f9c78b2bSJens Axboe 		return ERR_PTR(-ENOMEM);
1513f9c78b2bSJens Axboe 
1514f9c78b2bSJens Axboe 	offset = offset_in_page(kaddr);
1515f9c78b2bSJens Axboe 	for (i = 0; i < nr_pages; i++) {
1516f9c78b2bSJens Axboe 		unsigned int bytes = PAGE_SIZE - offset;
1517f9c78b2bSJens Axboe 
1518f9c78b2bSJens Axboe 		if (len <= 0)
1519f9c78b2bSJens Axboe 			break;
1520f9c78b2bSJens Axboe 
1521f9c78b2bSJens Axboe 		if (bytes > len)
1522f9c78b2bSJens Axboe 			bytes = len;
1523f9c78b2bSJens Axboe 
1524f9c78b2bSJens Axboe 		if (bio_add_pc_page(q, bio, virt_to_page(data), bytes,
152575c72b83SChristoph Hellwig 				    offset) < bytes) {
152675c72b83SChristoph Hellwig 			/* we don't support partial mappings */
152775c72b83SChristoph Hellwig 			bio_put(bio);
152875c72b83SChristoph Hellwig 			return ERR_PTR(-EINVAL);
152975c72b83SChristoph Hellwig 		}
1530f9c78b2bSJens Axboe 
1531f9c78b2bSJens Axboe 		data += bytes;
1532f9c78b2bSJens Axboe 		len -= bytes;
1533f9c78b2bSJens Axboe 		offset = 0;
1534f9c78b2bSJens Axboe 	}
1535f9c78b2bSJens Axboe 
1536f9c78b2bSJens Axboe 	bio->bi_end_io = bio_map_kern_endio;
1537f9c78b2bSJens Axboe 	return bio;
1538f9c78b2bSJens Axboe }
1539f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_map_kern);
1540f9c78b2bSJens Axboe 
15414246a0b6SChristoph Hellwig static void bio_copy_kern_endio(struct bio *bio)
1542f9c78b2bSJens Axboe {
15431dfa0f68SChristoph Hellwig 	bio_free_pages(bio);
15441dfa0f68SChristoph Hellwig 	bio_put(bio);
15451dfa0f68SChristoph Hellwig }
15461dfa0f68SChristoph Hellwig 
15474246a0b6SChristoph Hellwig static void bio_copy_kern_endio_read(struct bio *bio)
15481dfa0f68SChristoph Hellwig {
154942d2683aSChristoph Hellwig 	char *p = bio->bi_private;
15501dfa0f68SChristoph Hellwig 	struct bio_vec *bvec;
1551f9c78b2bSJens Axboe 	int i;
1552f9c78b2bSJens Axboe 
1553f9c78b2bSJens Axboe 	bio_for_each_segment_all(bvec, bio, i) {
15541dfa0f68SChristoph Hellwig 		memcpy(p, page_address(bvec->bv_page), bvec->bv_len);
1555f9c78b2bSJens Axboe 		p += bvec->bv_len;
1556f9c78b2bSJens Axboe 	}
1557f9c78b2bSJens Axboe 
15584246a0b6SChristoph Hellwig 	bio_copy_kern_endio(bio);
1559f9c78b2bSJens Axboe }
1560f9c78b2bSJens Axboe 
1561f9c78b2bSJens Axboe /**
1562f9c78b2bSJens Axboe  *	bio_copy_kern	-	copy kernel address into bio
1563f9c78b2bSJens Axboe  *	@q: the struct request_queue for the bio
1564f9c78b2bSJens Axboe  *	@data: pointer to buffer to copy
1565f9c78b2bSJens Axboe  *	@len: length in bytes
1566f9c78b2bSJens Axboe  *	@gfp_mask: allocation flags for bio and page allocation
1567f9c78b2bSJens Axboe  *	@reading: data direction is READ
1568f9c78b2bSJens Axboe  *
1569f9c78b2bSJens Axboe  *	copy the kernel address into a bio suitable for io to a block
1570f9c78b2bSJens Axboe  *	device. Returns an error pointer in case of error.
1571f9c78b2bSJens Axboe  */
1572f9c78b2bSJens Axboe struct bio *bio_copy_kern(struct request_queue *q, void *data, unsigned int len,
1573f9c78b2bSJens Axboe 			  gfp_t gfp_mask, int reading)
1574f9c78b2bSJens Axboe {
157542d2683aSChristoph Hellwig 	unsigned long kaddr = (unsigned long)data;
157642d2683aSChristoph Hellwig 	unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
157742d2683aSChristoph Hellwig 	unsigned long start = kaddr >> PAGE_SHIFT;
157842d2683aSChristoph Hellwig 	struct bio *bio;
1579f9c78b2bSJens Axboe 	void *p = data;
15801dfa0f68SChristoph Hellwig 	int nr_pages = 0;
1581f9c78b2bSJens Axboe 
158242d2683aSChristoph Hellwig 	/*
158342d2683aSChristoph Hellwig 	 * Overflow, abort
158442d2683aSChristoph Hellwig 	 */
158542d2683aSChristoph Hellwig 	if (end < start)
158642d2683aSChristoph Hellwig 		return ERR_PTR(-EINVAL);
1587f9c78b2bSJens Axboe 
158842d2683aSChristoph Hellwig 	nr_pages = end - start;
158942d2683aSChristoph Hellwig 	bio = bio_kmalloc(gfp_mask, nr_pages);
159042d2683aSChristoph Hellwig 	if (!bio)
159142d2683aSChristoph Hellwig 		return ERR_PTR(-ENOMEM);
159242d2683aSChristoph Hellwig 
159342d2683aSChristoph Hellwig 	while (len) {
159442d2683aSChristoph Hellwig 		struct page *page;
159542d2683aSChristoph Hellwig 		unsigned int bytes = PAGE_SIZE;
159642d2683aSChristoph Hellwig 
159742d2683aSChristoph Hellwig 		if (bytes > len)
159842d2683aSChristoph Hellwig 			bytes = len;
159942d2683aSChristoph Hellwig 
160042d2683aSChristoph Hellwig 		page = alloc_page(q->bounce_gfp | gfp_mask);
160142d2683aSChristoph Hellwig 		if (!page)
160242d2683aSChristoph Hellwig 			goto cleanup;
160342d2683aSChristoph Hellwig 
160442d2683aSChristoph Hellwig 		if (!reading)
160542d2683aSChristoph Hellwig 			memcpy(page_address(page), p, bytes);
160642d2683aSChristoph Hellwig 
160742d2683aSChristoph Hellwig 		if (bio_add_pc_page(q, bio, page, bytes, 0) < bytes)
160842d2683aSChristoph Hellwig 			break;
160942d2683aSChristoph Hellwig 
161042d2683aSChristoph Hellwig 		len -= bytes;
161142d2683aSChristoph Hellwig 		p += bytes;
1612f9c78b2bSJens Axboe 	}
1613f9c78b2bSJens Axboe 
16141dfa0f68SChristoph Hellwig 	if (reading) {
16151dfa0f68SChristoph Hellwig 		bio->bi_end_io = bio_copy_kern_endio_read;
161642d2683aSChristoph Hellwig 		bio->bi_private = data;
16171dfa0f68SChristoph Hellwig 	} else {
1618f9c78b2bSJens Axboe 		bio->bi_end_io = bio_copy_kern_endio;
16191dfa0f68SChristoph Hellwig 	}
16201dfa0f68SChristoph Hellwig 
1621f9c78b2bSJens Axboe 	return bio;
162242d2683aSChristoph Hellwig 
162342d2683aSChristoph Hellwig cleanup:
16241dfa0f68SChristoph Hellwig 	bio_free_pages(bio);
162542d2683aSChristoph Hellwig 	bio_put(bio);
162642d2683aSChristoph Hellwig 	return ERR_PTR(-ENOMEM);
1627f9c78b2bSJens Axboe }
1628f9c78b2bSJens Axboe 
1629f9c78b2bSJens Axboe /*
1630f9c78b2bSJens Axboe  * bio_set_pages_dirty() and bio_check_pages_dirty() are support functions
1631f9c78b2bSJens Axboe  * for performing direct-IO in BIOs.
1632f9c78b2bSJens Axboe  *
1633f9c78b2bSJens Axboe  * The problem is that we cannot run set_page_dirty() from interrupt context
1634f9c78b2bSJens Axboe  * because the required locks are not interrupt-safe.  So what we can do is to
1635f9c78b2bSJens Axboe  * mark the pages dirty _before_ performing IO.  And in interrupt context,
1636f9c78b2bSJens Axboe  * check that the pages are still dirty.   If so, fine.  If not, redirty them
1637f9c78b2bSJens Axboe  * in process context.
1638f9c78b2bSJens Axboe  *
1639f9c78b2bSJens Axboe  * We special-case compound pages here: normally this means reads into hugetlb
1640f9c78b2bSJens Axboe  * pages.  The logic in here doesn't really work right for compound pages
1641f9c78b2bSJens Axboe  * because the VM does not uniformly chase down the head page in all cases.
1642f9c78b2bSJens Axboe  * But dirtiness of compound pages is pretty meaningless anyway: the VM doesn't
1643f9c78b2bSJens Axboe  * handle them at all.  So we skip compound pages here at an early stage.
1644f9c78b2bSJens Axboe  *
1645f9c78b2bSJens Axboe  * Note that this code is very hard to test under normal circumstances because
1646f9c78b2bSJens Axboe  * direct-io pins the pages with get_user_pages().  This makes
1647f9c78b2bSJens Axboe  * is_page_cache_freeable return false, and the VM will not clean the pages.
1648f9c78b2bSJens Axboe  * But other code (eg, flusher threads) could clean the pages if they are mapped
1649f9c78b2bSJens Axboe  * pagecache.
1650f9c78b2bSJens Axboe  *
1651f9c78b2bSJens Axboe  * Simply disabling the call to bio_set_pages_dirty() is a good way to test the
1652f9c78b2bSJens Axboe  * deferred bio dirtying paths.
1653f9c78b2bSJens Axboe  */
1654f9c78b2bSJens Axboe 
1655f9c78b2bSJens Axboe /*
1656f9c78b2bSJens Axboe  * bio_set_pages_dirty() will mark all the bio's pages as dirty.
1657f9c78b2bSJens Axboe  */
1658f9c78b2bSJens Axboe void bio_set_pages_dirty(struct bio *bio)
1659f9c78b2bSJens Axboe {
1660f9c78b2bSJens Axboe 	struct bio_vec *bvec;
1661f9c78b2bSJens Axboe 	int i;
1662f9c78b2bSJens Axboe 
1663f9c78b2bSJens Axboe 	bio_for_each_segment_all(bvec, bio, i) {
1664f9c78b2bSJens Axboe 		struct page *page = bvec->bv_page;
1665f9c78b2bSJens Axboe 
1666f9c78b2bSJens Axboe 		if (page && !PageCompound(page))
1667f9c78b2bSJens Axboe 			set_page_dirty_lock(page);
1668f9c78b2bSJens Axboe 	}
1669f9c78b2bSJens Axboe }
16701900fcc4SKent Overstreet EXPORT_SYMBOL_GPL(bio_set_pages_dirty);
1671f9c78b2bSJens Axboe 
1672f9c78b2bSJens Axboe static void bio_release_pages(struct bio *bio)
1673f9c78b2bSJens Axboe {
1674f9c78b2bSJens Axboe 	struct bio_vec *bvec;
1675f9c78b2bSJens Axboe 	int i;
1676f9c78b2bSJens Axboe 
1677f9c78b2bSJens Axboe 	bio_for_each_segment_all(bvec, bio, i) {
1678f9c78b2bSJens Axboe 		struct page *page = bvec->bv_page;
1679f9c78b2bSJens Axboe 
1680f9c78b2bSJens Axboe 		if (page)
1681f9c78b2bSJens Axboe 			put_page(page);
1682f9c78b2bSJens Axboe 	}
1683f9c78b2bSJens Axboe }
1684f9c78b2bSJens Axboe 
1685f9c78b2bSJens Axboe /*
1686f9c78b2bSJens Axboe  * bio_check_pages_dirty() will check that all the BIO's pages are still dirty.
1687f9c78b2bSJens Axboe  * If they are, then fine.  If, however, some pages are clean then they must
1688f9c78b2bSJens Axboe  * have been written out during the direct-IO read.  So we take another ref on
1689f9c78b2bSJens Axboe  * the BIO and the offending pages and re-dirty the pages in process context.
1690f9c78b2bSJens Axboe  *
1691f9c78b2bSJens Axboe  * It is expected that bio_check_pages_dirty() will wholly own the BIO from
1692ea1754a0SKirill A. Shutemov  * here on.  It will run one put_page() against each page and will run one
1693ea1754a0SKirill A. Shutemov  * bio_put() against the BIO.
1694f9c78b2bSJens Axboe  */
1695f9c78b2bSJens Axboe 
1696f9c78b2bSJens Axboe static void bio_dirty_fn(struct work_struct *work);
1697f9c78b2bSJens Axboe 
1698f9c78b2bSJens Axboe static DECLARE_WORK(bio_dirty_work, bio_dirty_fn);
1699f9c78b2bSJens Axboe static DEFINE_SPINLOCK(bio_dirty_lock);
1700f9c78b2bSJens Axboe static struct bio *bio_dirty_list;
1701f9c78b2bSJens Axboe 
1702f9c78b2bSJens Axboe /*
1703f9c78b2bSJens Axboe  * This runs in process context
1704f9c78b2bSJens Axboe  */
1705f9c78b2bSJens Axboe static void bio_dirty_fn(struct work_struct *work)
1706f9c78b2bSJens Axboe {
1707f9c78b2bSJens Axboe 	unsigned long flags;
1708f9c78b2bSJens Axboe 	struct bio *bio;
1709f9c78b2bSJens Axboe 
1710f9c78b2bSJens Axboe 	spin_lock_irqsave(&bio_dirty_lock, flags);
1711f9c78b2bSJens Axboe 	bio = bio_dirty_list;
1712f9c78b2bSJens Axboe 	bio_dirty_list = NULL;
1713f9c78b2bSJens Axboe 	spin_unlock_irqrestore(&bio_dirty_lock, flags);
1714f9c78b2bSJens Axboe 
1715f9c78b2bSJens Axboe 	while (bio) {
1716f9c78b2bSJens Axboe 		struct bio *next = bio->bi_private;
1717f9c78b2bSJens Axboe 
1718f9c78b2bSJens Axboe 		bio_set_pages_dirty(bio);
1719f9c78b2bSJens Axboe 		bio_release_pages(bio);
1720f9c78b2bSJens Axboe 		bio_put(bio);
1721f9c78b2bSJens Axboe 		bio = next;
1722f9c78b2bSJens Axboe 	}
1723f9c78b2bSJens Axboe }
1724f9c78b2bSJens Axboe 
1725f9c78b2bSJens Axboe void bio_check_pages_dirty(struct bio *bio)
1726f9c78b2bSJens Axboe {
1727f9c78b2bSJens Axboe 	struct bio_vec *bvec;
1728f9c78b2bSJens Axboe 	int nr_clean_pages = 0;
1729f9c78b2bSJens Axboe 	int i;
1730f9c78b2bSJens Axboe 
1731f9c78b2bSJens Axboe 	bio_for_each_segment_all(bvec, bio, i) {
1732f9c78b2bSJens Axboe 		struct page *page = bvec->bv_page;
1733f9c78b2bSJens Axboe 
1734f9c78b2bSJens Axboe 		if (PageDirty(page) || PageCompound(page)) {
173509cbfeafSKirill A. Shutemov 			put_page(page);
1736f9c78b2bSJens Axboe 			bvec->bv_page = NULL;
1737f9c78b2bSJens Axboe 		} else {
1738f9c78b2bSJens Axboe 			nr_clean_pages++;
1739f9c78b2bSJens Axboe 		}
1740f9c78b2bSJens Axboe 	}
1741f9c78b2bSJens Axboe 
1742f9c78b2bSJens Axboe 	if (nr_clean_pages) {
1743f9c78b2bSJens Axboe 		unsigned long flags;
1744f9c78b2bSJens Axboe 
1745f9c78b2bSJens Axboe 		spin_lock_irqsave(&bio_dirty_lock, flags);
1746f9c78b2bSJens Axboe 		bio->bi_private = bio_dirty_list;
1747f9c78b2bSJens Axboe 		bio_dirty_list = bio;
1748f9c78b2bSJens Axboe 		spin_unlock_irqrestore(&bio_dirty_lock, flags);
1749f9c78b2bSJens Axboe 		schedule_work(&bio_dirty_work);
1750f9c78b2bSJens Axboe 	} else {
1751f9c78b2bSJens Axboe 		bio_put(bio);
1752f9c78b2bSJens Axboe 	}
1753f9c78b2bSJens Axboe }
17541900fcc4SKent Overstreet EXPORT_SYMBOL_GPL(bio_check_pages_dirty);
1755f9c78b2bSJens Axboe 
1756d62e26b3SJens Axboe void generic_start_io_acct(struct request_queue *q, int rw,
1757d62e26b3SJens Axboe 			   unsigned long sectors, struct hd_struct *part)
1758394ffa50SGu Zheng {
1759394ffa50SGu Zheng 	int cpu = part_stat_lock();
1760394ffa50SGu Zheng 
1761d62e26b3SJens Axboe 	part_round_stats(q, cpu, part);
1762394ffa50SGu Zheng 	part_stat_inc(cpu, part, ios[rw]);
1763394ffa50SGu Zheng 	part_stat_add(cpu, part, sectors[rw], sectors);
1764d62e26b3SJens Axboe 	part_inc_in_flight(q, part, rw);
1765394ffa50SGu Zheng 
1766394ffa50SGu Zheng 	part_stat_unlock();
1767394ffa50SGu Zheng }
1768394ffa50SGu Zheng EXPORT_SYMBOL(generic_start_io_acct);
1769394ffa50SGu Zheng 
1770d62e26b3SJens Axboe void generic_end_io_acct(struct request_queue *q, int rw,
1771d62e26b3SJens Axboe 			 struct hd_struct *part, unsigned long start_time)
1772394ffa50SGu Zheng {
1773394ffa50SGu Zheng 	unsigned long duration = jiffies - start_time;
1774394ffa50SGu Zheng 	int cpu = part_stat_lock();
1775394ffa50SGu Zheng 
1776394ffa50SGu Zheng 	part_stat_add(cpu, part, ticks[rw], duration);
1777d62e26b3SJens Axboe 	part_round_stats(q, cpu, part);
1778d62e26b3SJens Axboe 	part_dec_in_flight(q, part, rw);
1779394ffa50SGu Zheng 
1780394ffa50SGu Zheng 	part_stat_unlock();
1781394ffa50SGu Zheng }
1782394ffa50SGu Zheng EXPORT_SYMBOL(generic_end_io_acct);
1783394ffa50SGu Zheng 
1784f9c78b2bSJens Axboe #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
1785f9c78b2bSJens Axboe void bio_flush_dcache_pages(struct bio *bi)
1786f9c78b2bSJens Axboe {
1787f9c78b2bSJens Axboe 	struct bio_vec bvec;
1788f9c78b2bSJens Axboe 	struct bvec_iter iter;
1789f9c78b2bSJens Axboe 
1790f9c78b2bSJens Axboe 	bio_for_each_segment(bvec, bi, iter)
1791f9c78b2bSJens Axboe 		flush_dcache_page(bvec.bv_page);
1792f9c78b2bSJens Axboe }
1793f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_flush_dcache_pages);
1794f9c78b2bSJens Axboe #endif
1795f9c78b2bSJens Axboe 
1796c4cf5261SJens Axboe static inline bool bio_remaining_done(struct bio *bio)
1797c4cf5261SJens Axboe {
1798c4cf5261SJens Axboe 	/*
1799c4cf5261SJens Axboe 	 * If we're not chaining, then ->__bi_remaining is always 1 and
1800c4cf5261SJens Axboe 	 * we always end io on the first invocation.
1801c4cf5261SJens Axboe 	 */
1802c4cf5261SJens Axboe 	if (!bio_flagged(bio, BIO_CHAIN))
1803c4cf5261SJens Axboe 		return true;
1804c4cf5261SJens Axboe 
1805c4cf5261SJens Axboe 	BUG_ON(atomic_read(&bio->__bi_remaining) <= 0);
1806c4cf5261SJens Axboe 
1807326e1dbbSMike Snitzer 	if (atomic_dec_and_test(&bio->__bi_remaining)) {
1808b7c44ed9SJens Axboe 		bio_clear_flag(bio, BIO_CHAIN);
1809c4cf5261SJens Axboe 		return true;
1810326e1dbbSMike Snitzer 	}
1811c4cf5261SJens Axboe 
1812c4cf5261SJens Axboe 	return false;
1813c4cf5261SJens Axboe }
1814c4cf5261SJens Axboe 
1815f9c78b2bSJens Axboe /**
1816f9c78b2bSJens Axboe  * bio_endio - end I/O on a bio
1817f9c78b2bSJens Axboe  * @bio:	bio
1818f9c78b2bSJens Axboe  *
1819f9c78b2bSJens Axboe  * Description:
18204246a0b6SChristoph Hellwig  *   bio_endio() will end I/O on the whole bio. bio_endio() is the preferred
18214246a0b6SChristoph Hellwig  *   way to end I/O on a bio. No one should call bi_end_io() directly on a
18224246a0b6SChristoph Hellwig  *   bio unless they own it and thus know that it has an end_io function.
1823fbbaf700SNeilBrown  *
1824fbbaf700SNeilBrown  *   bio_endio() can be called several times on a bio that has been chained
1825fbbaf700SNeilBrown  *   using bio_chain().  The ->bi_end_io() function will only be called the
1826fbbaf700SNeilBrown  *   last time.  At this point the BLK_TA_COMPLETE tracing event will be
1827fbbaf700SNeilBrown  *   generated if BIO_TRACE_COMPLETION is set.
1828f9c78b2bSJens Axboe  **/
18294246a0b6SChristoph Hellwig void bio_endio(struct bio *bio)
1830f9c78b2bSJens Axboe {
1831ba8c6967SChristoph Hellwig again:
18322b885517SChristoph Hellwig 	if (!bio_remaining_done(bio))
1833ba8c6967SChristoph Hellwig 		return;
18347c20f116SChristoph Hellwig 	if (!bio_integrity_endio(bio))
18357c20f116SChristoph Hellwig 		return;
1836f9c78b2bSJens Axboe 
1837f9c78b2bSJens Axboe 	/*
1838ba8c6967SChristoph Hellwig 	 * Need to have a real endio function for chained bios, otherwise
1839ba8c6967SChristoph Hellwig 	 * various corner cases will break (like stacking block devices that
1840ba8c6967SChristoph Hellwig 	 * save/restore bi_end_io) - however, we want to avoid unbounded
1841ba8c6967SChristoph Hellwig 	 * recursion and blowing the stack. Tail call optimization would
1842ba8c6967SChristoph Hellwig 	 * handle this, but compiling with frame pointers also disables
1843ba8c6967SChristoph Hellwig 	 * gcc's sibling call optimization.
1844f9c78b2bSJens Axboe 	 */
1845f9c78b2bSJens Axboe 	if (bio->bi_end_io == bio_chain_endio) {
184638f8baaeSChristoph Hellwig 		bio = __bio_chain_endio(bio);
1847ba8c6967SChristoph Hellwig 		goto again;
1848ba8c6967SChristoph Hellwig 	}
1849ba8c6967SChristoph Hellwig 
185074d46992SChristoph Hellwig 	if (bio->bi_disk && bio_flagged(bio, BIO_TRACE_COMPLETION)) {
185174d46992SChristoph Hellwig 		trace_block_bio_complete(bio->bi_disk->queue, bio,
1852a462b950SBart Van Assche 					 blk_status_to_errno(bio->bi_status));
1853fbbaf700SNeilBrown 		bio_clear_flag(bio, BIO_TRACE_COMPLETION);
1854fbbaf700SNeilBrown 	}
1855fbbaf700SNeilBrown 
18569e234eeaSShaohua Li 	blk_throtl_bio_endio(bio);
1857b222dd2fSShaohua Li 	/* release cgroup info */
1858b222dd2fSShaohua Li 	bio_uninit(bio);
1859f9c78b2bSJens Axboe 	if (bio->bi_end_io)
18604246a0b6SChristoph Hellwig 		bio->bi_end_io(bio);
1861f9c78b2bSJens Axboe }
1862f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_endio);
1863f9c78b2bSJens Axboe 
1864f9c78b2bSJens Axboe /**
1865f9c78b2bSJens Axboe  * bio_split - split a bio
1866f9c78b2bSJens Axboe  * @bio:	bio to split
1867f9c78b2bSJens Axboe  * @sectors:	number of sectors to split from the front of @bio
1868f9c78b2bSJens Axboe  * @gfp:	gfp mask
1869f9c78b2bSJens Axboe  * @bs:		bio set to allocate from
1870f9c78b2bSJens Axboe  *
1871f9c78b2bSJens Axboe  * Allocates and returns a new bio which represents @sectors from the start of
1872f9c78b2bSJens Axboe  * @bio, and updates @bio to represent the remaining sectors.
1873f9c78b2bSJens Axboe  *
1874f3f5da62SMartin K. Petersen  * Unless this is a discard request the newly allocated bio will point
1875f3f5da62SMartin K. Petersen  * to @bio's bi_io_vec; it is the caller's responsibility to ensure that
1876f3f5da62SMartin K. Petersen  * @bio is not freed before the split.
1877f9c78b2bSJens Axboe  */
1878f9c78b2bSJens Axboe struct bio *bio_split(struct bio *bio, int sectors,
1879f9c78b2bSJens Axboe 		      gfp_t gfp, struct bio_set *bs)
1880f9c78b2bSJens Axboe {
1881f341a4d3SMikulas Patocka 	struct bio *split;
1882f9c78b2bSJens Axboe 
1883f9c78b2bSJens Axboe 	BUG_ON(sectors <= 0);
1884f9c78b2bSJens Axboe 	BUG_ON(sectors >= bio_sectors(bio));
1885f9c78b2bSJens Axboe 
1886f9c78b2bSJens Axboe 	split = bio_clone_fast(bio, gfp, bs);
1887f9c78b2bSJens Axboe 	if (!split)
1888f9c78b2bSJens Axboe 		return NULL;
1889f9c78b2bSJens Axboe 
1890f9c78b2bSJens Axboe 	split->bi_iter.bi_size = sectors << 9;
1891f9c78b2bSJens Axboe 
1892f9c78b2bSJens Axboe 	if (bio_integrity(split))
1893fbd08e76SDmitry Monakhov 		bio_integrity_trim(split);
1894f9c78b2bSJens Axboe 
1895f9c78b2bSJens Axboe 	bio_advance(bio, split->bi_iter.bi_size);
18965151842bSGreg Edwards 	bio->bi_iter.bi_done = 0;
1897f9c78b2bSJens Axboe 
1898fbbaf700SNeilBrown 	if (bio_flagged(bio, BIO_TRACE_COMPLETION))
189920d59023SGoldwyn Rodrigues 		bio_set_flag(split, BIO_TRACE_COMPLETION);
1900fbbaf700SNeilBrown 
1901f9c78b2bSJens Axboe 	return split;
1902f9c78b2bSJens Axboe }
1903f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_split);
1904f9c78b2bSJens Axboe 
1905f9c78b2bSJens Axboe /**
1906f9c78b2bSJens Axboe  * bio_trim - trim a bio
1907f9c78b2bSJens Axboe  * @bio:	bio to trim
1908f9c78b2bSJens Axboe  * @offset:	number of sectors to trim from the front of @bio
1909f9c78b2bSJens Axboe  * @size:	size we want to trim @bio to, in sectors
1910f9c78b2bSJens Axboe  */
1911f9c78b2bSJens Axboe void bio_trim(struct bio *bio, int offset, int size)
1912f9c78b2bSJens Axboe {
1913f9c78b2bSJens Axboe 	/* 'bio' is a cloned bio which we need to trim to match
1914f9c78b2bSJens Axboe 	 * the given offset and size.
1915f9c78b2bSJens Axboe 	 */
1916f9c78b2bSJens Axboe 
1917f9c78b2bSJens Axboe 	size <<= 9;
1918f9c78b2bSJens Axboe 	if (offset == 0 && size == bio->bi_iter.bi_size)
1919f9c78b2bSJens Axboe 		return;
1920f9c78b2bSJens Axboe 
1921b7c44ed9SJens Axboe 	bio_clear_flag(bio, BIO_SEG_VALID);
1922f9c78b2bSJens Axboe 
1923f9c78b2bSJens Axboe 	bio_advance(bio, offset << 9);
1924f9c78b2bSJens Axboe 
1925f9c78b2bSJens Axboe 	bio->bi_iter.bi_size = size;
1926376a78abSDmitry Monakhov 
1927376a78abSDmitry Monakhov 	if (bio_integrity(bio))
1928fbd08e76SDmitry Monakhov 		bio_integrity_trim(bio);
1929376a78abSDmitry Monakhov 
1930f9c78b2bSJens Axboe }
1931f9c78b2bSJens Axboe EXPORT_SYMBOL_GPL(bio_trim);
1932f9c78b2bSJens Axboe 
1933f9c78b2bSJens Axboe /*
1934f9c78b2bSJens Axboe  * create memory pools for biovec's in a bio_set.
1935f9c78b2bSJens Axboe  * use the global biovec slabs created for general use.
1936f9c78b2bSJens Axboe  */
19378aa6ba2fSKent Overstreet int biovec_init_pool(mempool_t *pool, int pool_entries)
1938f9c78b2bSJens Axboe {
1939ed996a52SChristoph Hellwig 	struct biovec_slab *bp = bvec_slabs + BVEC_POOL_MAX;
1940f9c78b2bSJens Axboe 
19418aa6ba2fSKent Overstreet 	return mempool_init_slab_pool(pool, pool_entries, bp->slab);
1942f9c78b2bSJens Axboe }
1943f9c78b2bSJens Axboe 
1944917a38c7SKent Overstreet /*
1945917a38c7SKent Overstreet  * bioset_exit - exit a bioset initialized with bioset_init()
1946917a38c7SKent Overstreet  *
1947917a38c7SKent Overstreet  * May be called on a zeroed but uninitialized bioset (i.e. allocated with
1948917a38c7SKent Overstreet  * kzalloc()).
1949917a38c7SKent Overstreet  */
1950917a38c7SKent Overstreet void bioset_exit(struct bio_set *bs)
1951f9c78b2bSJens Axboe {
1952f9c78b2bSJens Axboe 	if (bs->rescue_workqueue)
1953f9c78b2bSJens Axboe 		destroy_workqueue(bs->rescue_workqueue);
1954917a38c7SKent Overstreet 	bs->rescue_workqueue = NULL;
1955f9c78b2bSJens Axboe 
19568aa6ba2fSKent Overstreet 	mempool_exit(&bs->bio_pool);
19578aa6ba2fSKent Overstreet 	mempool_exit(&bs->bvec_pool);
1958f9c78b2bSJens Axboe 
1959f9c78b2bSJens Axboe 	bioset_integrity_free(bs);
1960917a38c7SKent Overstreet 	if (bs->bio_slab)
1961f9c78b2bSJens Axboe 		bio_put_slab(bs);
1962917a38c7SKent Overstreet 	bs->bio_slab = NULL;
1963917a38c7SKent Overstreet }
1964917a38c7SKent Overstreet EXPORT_SYMBOL(bioset_exit);
1965f9c78b2bSJens Axboe 
1966011067b0SNeilBrown /**
1967917a38c7SKent Overstreet  * bioset_init - Initialize a bio_set
1968dad08527SKent Overstreet  * @bs:		pool to initialize
1969917a38c7SKent Overstreet  * @pool_size:	Number of bio and bio_vecs to cache in the mempool
1970917a38c7SKent Overstreet  * @front_pad:	Number of bytes to allocate in front of the returned bio
1971917a38c7SKent Overstreet  * @flags:	Flags to modify behavior, currently %BIOSET_NEED_BVECS
1972917a38c7SKent Overstreet  *              and %BIOSET_NEED_RESCUER
1973917a38c7SKent Overstreet  *
1974dad08527SKent Overstreet  * Description:
1975dad08527SKent Overstreet  *    Set up a bio_set to be used with @bio_alloc_bioset. Allows the caller
1976dad08527SKent Overstreet  *    to ask for a number of bytes to be allocated in front of the bio.
1977dad08527SKent Overstreet  *    Front pad allocation is useful for embedding the bio inside
1978dad08527SKent Overstreet  *    another structure, to avoid allocating extra data to go with the bio.
1979dad08527SKent Overstreet  *    Note that the bio must be embedded at the END of that structure always,
1980dad08527SKent Overstreet  *    or things will break badly.
1981dad08527SKent Overstreet  *    If %BIOSET_NEED_BVECS is set in @flags, a separate pool will be allocated
1982dad08527SKent Overstreet  *    for allocating iovecs.  This pool is not needed e.g. for bio_clone_fast().
1983dad08527SKent Overstreet  *    If %BIOSET_NEED_RESCUER is set, a workqueue is created which can be used to
1984dad08527SKent Overstreet  *    dispatch queued requests when the mempool runs out of space.
1985dad08527SKent Overstreet  *
1986917a38c7SKent Overstreet  */
1987917a38c7SKent Overstreet int bioset_init(struct bio_set *bs,
1988917a38c7SKent Overstreet 		unsigned int pool_size,
1989917a38c7SKent Overstreet 		unsigned int front_pad,
1990917a38c7SKent Overstreet 		int flags)
1991917a38c7SKent Overstreet {
1992917a38c7SKent Overstreet 	unsigned int back_pad = BIO_INLINE_VECS * sizeof(struct bio_vec);
1993917a38c7SKent Overstreet 
1994917a38c7SKent Overstreet 	bs->front_pad = front_pad;
1995917a38c7SKent Overstreet 
1996917a38c7SKent Overstreet 	spin_lock_init(&bs->rescue_lock);
1997917a38c7SKent Overstreet 	bio_list_init(&bs->rescue_list);
1998917a38c7SKent Overstreet 	INIT_WORK(&bs->rescue_work, bio_alloc_rescue);
1999917a38c7SKent Overstreet 
2000917a38c7SKent Overstreet 	bs->bio_slab = bio_find_or_create_slab(front_pad + back_pad);
2001917a38c7SKent Overstreet 	if (!bs->bio_slab)
2002917a38c7SKent Overstreet 		return -ENOMEM;
2003917a38c7SKent Overstreet 
2004917a38c7SKent Overstreet 	if (mempool_init_slab_pool(&bs->bio_pool, pool_size, bs->bio_slab))
2005917a38c7SKent Overstreet 		goto bad;
2006917a38c7SKent Overstreet 
2007917a38c7SKent Overstreet 	if ((flags & BIOSET_NEED_BVECS) &&
2008917a38c7SKent Overstreet 	    biovec_init_pool(&bs->bvec_pool, pool_size))
2009917a38c7SKent Overstreet 		goto bad;
2010917a38c7SKent Overstreet 
2011917a38c7SKent Overstreet 	if (!(flags & BIOSET_NEED_RESCUER))
2012917a38c7SKent Overstreet 		return 0;
2013917a38c7SKent Overstreet 
2014917a38c7SKent Overstreet 	bs->rescue_workqueue = alloc_workqueue("bioset", WQ_MEM_RECLAIM, 0);
2015917a38c7SKent Overstreet 	if (!bs->rescue_workqueue)
2016917a38c7SKent Overstreet 		goto bad;
2017917a38c7SKent Overstreet 
2018917a38c7SKent Overstreet 	return 0;
2019917a38c7SKent Overstreet bad:
2020917a38c7SKent Overstreet 	bioset_exit(bs);
2021917a38c7SKent Overstreet 	return -ENOMEM;
2022917a38c7SKent Overstreet }
2023917a38c7SKent Overstreet EXPORT_SYMBOL(bioset_init);
2024917a38c7SKent Overstreet 
202528e89fd9SJens Axboe /*
202628e89fd9SJens Axboe  * Initialize and setup a new bio_set, based on the settings from
202728e89fd9SJens Axboe  * another bio_set.
202828e89fd9SJens Axboe  */
202928e89fd9SJens Axboe int bioset_init_from_src(struct bio_set *bs, struct bio_set *src)
203028e89fd9SJens Axboe {
203128e89fd9SJens Axboe 	int flags;
203228e89fd9SJens Axboe 
203328e89fd9SJens Axboe 	flags = 0;
203428e89fd9SJens Axboe 	if (src->bvec_pool.min_nr)
203528e89fd9SJens Axboe 		flags |= BIOSET_NEED_BVECS;
203628e89fd9SJens Axboe 	if (src->rescue_workqueue)
203728e89fd9SJens Axboe 		flags |= BIOSET_NEED_RESCUER;
203828e89fd9SJens Axboe 
203928e89fd9SJens Axboe 	return bioset_init(bs, src->bio_pool.min_nr, src->front_pad, flags);
204028e89fd9SJens Axboe }
204128e89fd9SJens Axboe EXPORT_SYMBOL(bioset_init_from_src);
204228e89fd9SJens Axboe 
2043f9c78b2bSJens Axboe #ifdef CONFIG_BLK_CGROUP
20441d933cf0STejun Heo 
20451d933cf0STejun Heo /**
20461d933cf0STejun Heo  * bio_associate_blkcg - associate a bio with the specified blkcg
20471d933cf0STejun Heo  * @bio: target bio
20481d933cf0STejun Heo  * @blkcg_css: css of the blkcg to associate
20491d933cf0STejun Heo  *
20501d933cf0STejun Heo  * Associate @bio with the blkcg specified by @blkcg_css.  Block layer will
20511d933cf0STejun Heo  * treat @bio as if it were issued by a task which belongs to the blkcg.
20521d933cf0STejun Heo  *
20531d933cf0STejun Heo  * This function takes an extra reference of @blkcg_css which will be put
20541d933cf0STejun Heo  * when @bio is released.  The caller must own @bio and is responsible for
20551d933cf0STejun Heo  * synchronizing calls to this function.
20561d933cf0STejun Heo  */
20571d933cf0STejun Heo int bio_associate_blkcg(struct bio *bio, struct cgroup_subsys_state *blkcg_css)
20581d933cf0STejun Heo {
20591d933cf0STejun Heo 	if (unlikely(bio->bi_css))
20601d933cf0STejun Heo 		return -EBUSY;
20611d933cf0STejun Heo 	css_get(blkcg_css);
20621d933cf0STejun Heo 	bio->bi_css = blkcg_css;
20631d933cf0STejun Heo 	return 0;
20641d933cf0STejun Heo }
20655aa2a96bSTejun Heo EXPORT_SYMBOL_GPL(bio_associate_blkcg);
20661d933cf0STejun Heo 
2067f9c78b2bSJens Axboe /**
2068f9c78b2bSJens Axboe  * bio_disassociate_task - undo bio_associate_current()
2069f9c78b2bSJens Axboe  * @bio: target bio
2070f9c78b2bSJens Axboe  */
2071f9c78b2bSJens Axboe void bio_disassociate_task(struct bio *bio)
2072f9c78b2bSJens Axboe {
2073f9c78b2bSJens Axboe 	if (bio->bi_ioc) {
2074f9c78b2bSJens Axboe 		put_io_context(bio->bi_ioc);
2075f9c78b2bSJens Axboe 		bio->bi_ioc = NULL;
2076f9c78b2bSJens Axboe 	}
2077f9c78b2bSJens Axboe 	if (bio->bi_css) {
2078f9c78b2bSJens Axboe 		css_put(bio->bi_css);
2079f9c78b2bSJens Axboe 		bio->bi_css = NULL;
2080f9c78b2bSJens Axboe 	}
2081f9c78b2bSJens Axboe }
2082f9c78b2bSJens Axboe 
208320bd723eSPaolo Valente /**
208420bd723eSPaolo Valente  * bio_clone_blkcg_association - clone blkcg association from src to dst bio
208520bd723eSPaolo Valente  * @dst: destination bio
208620bd723eSPaolo Valente  * @src: source bio
208720bd723eSPaolo Valente  */
208820bd723eSPaolo Valente void bio_clone_blkcg_association(struct bio *dst, struct bio *src)
208920bd723eSPaolo Valente {
209020bd723eSPaolo Valente 	if (src->bi_css)
209120bd723eSPaolo Valente 		WARN_ON(bio_associate_blkcg(dst, src->bi_css));
209220bd723eSPaolo Valente }
20938a8e6f84SShaohua Li EXPORT_SYMBOL_GPL(bio_clone_blkcg_association);
2094f9c78b2bSJens Axboe #endif /* CONFIG_BLK_CGROUP */
2095f9c78b2bSJens Axboe 
2096f9c78b2bSJens Axboe static void __init biovec_init_slabs(void)
2097f9c78b2bSJens Axboe {
2098f9c78b2bSJens Axboe 	int i;
2099f9c78b2bSJens Axboe 
2100ed996a52SChristoph Hellwig 	for (i = 0; i < BVEC_POOL_NR; i++) {
2101f9c78b2bSJens Axboe 		int size;
2102f9c78b2bSJens Axboe 		struct biovec_slab *bvs = bvec_slabs + i;
2103f9c78b2bSJens Axboe 
2104f9c78b2bSJens Axboe 		if (bvs->nr_vecs <= BIO_INLINE_VECS) {
2105f9c78b2bSJens Axboe 			bvs->slab = NULL;
2106f9c78b2bSJens Axboe 			continue;
2107f9c78b2bSJens Axboe 		}
2108f9c78b2bSJens Axboe 
2109f9c78b2bSJens Axboe 		size = bvs->nr_vecs * sizeof(struct bio_vec);
2110f9c78b2bSJens Axboe 		bvs->slab = kmem_cache_create(bvs->name, size, 0,
2111f9c78b2bSJens Axboe                                 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
2112f9c78b2bSJens Axboe 	}
2113f9c78b2bSJens Axboe }
2114f9c78b2bSJens Axboe 
2115f9c78b2bSJens Axboe static int __init init_bio(void)
2116f9c78b2bSJens Axboe {
2117f9c78b2bSJens Axboe 	bio_slab_max = 2;
2118f9c78b2bSJens Axboe 	bio_slab_nr = 0;
2119f9c78b2bSJens Axboe 	bio_slabs = kzalloc(bio_slab_max * sizeof(struct bio_slab), GFP_KERNEL);
2120f9c78b2bSJens Axboe 	if (!bio_slabs)
2121f9c78b2bSJens Axboe 		panic("bio: can't allocate bios\n");
2122f9c78b2bSJens Axboe 
2123f9c78b2bSJens Axboe 	bio_integrity_init();
2124f9c78b2bSJens Axboe 	biovec_init_slabs();
2125f9c78b2bSJens Axboe 
2126f4f8154aSKent Overstreet 	if (bioset_init(&fs_bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS))
2127f9c78b2bSJens Axboe 		panic("bio: can't allocate bios\n");
2128f9c78b2bSJens Axboe 
2129f4f8154aSKent Overstreet 	if (bioset_integrity_create(&fs_bio_set, BIO_POOL_SIZE))
2130f9c78b2bSJens Axboe 		panic("bio: can't create integrity pool\n");
2131f9c78b2bSJens Axboe 
2132f9c78b2bSJens Axboe 	return 0;
2133f9c78b2bSJens Axboe }
2134f9c78b2bSJens Axboe subsys_initcall(init_bio);
2135