xref: /openbmc/linux/block/bio.c (revision f9c78b2b)
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 #include <scsi/sg.h>		/* for struct sg_iovec */
32f9c78b2bSJens Axboe 
33f9c78b2bSJens Axboe #include <trace/events/block.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  */
46f9c78b2bSJens Axboe #define BV(x) { .nr_vecs = x, .name = "biovec-"__stringify(x) }
47f9c78b2bSJens Axboe static struct biovec_slab bvec_slabs[BIOVEC_NR_POOLS] __read_mostly = {
48f9c78b2bSJens Axboe 	BV(1), BV(4), BV(16), BV(64), BV(128), BV(BIO_MAX_PAGES),
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  */
56f9c78b2bSJens Axboe 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);
115f9c78b2bSJens Axboe 	slab = kmem_cache_create(bslab->name, sz, 0, SLAB_HWCACHE_ALIGN, NULL);
116f9c78b2bSJens Axboe 	if (!slab)
117f9c78b2bSJens Axboe 		goto out_unlock;
118f9c78b2bSJens Axboe 
119f9c78b2bSJens Axboe 	bslab->slab = slab;
120f9c78b2bSJens Axboe 	bslab->slab_ref = 1;
121f9c78b2bSJens Axboe 	bslab->slab_size = sz;
122f9c78b2bSJens Axboe out_unlock:
123f9c78b2bSJens Axboe 	mutex_unlock(&bio_slab_lock);
124f9c78b2bSJens Axboe 	return slab;
125f9c78b2bSJens Axboe }
126f9c78b2bSJens Axboe 
127f9c78b2bSJens Axboe static void bio_put_slab(struct bio_set *bs)
128f9c78b2bSJens Axboe {
129f9c78b2bSJens Axboe 	struct bio_slab *bslab = NULL;
130f9c78b2bSJens Axboe 	unsigned int i;
131f9c78b2bSJens Axboe 
132f9c78b2bSJens Axboe 	mutex_lock(&bio_slab_lock);
133f9c78b2bSJens Axboe 
134f9c78b2bSJens Axboe 	for (i = 0; i < bio_slab_nr; i++) {
135f9c78b2bSJens Axboe 		if (bs->bio_slab == bio_slabs[i].slab) {
136f9c78b2bSJens Axboe 			bslab = &bio_slabs[i];
137f9c78b2bSJens Axboe 			break;
138f9c78b2bSJens Axboe 		}
139f9c78b2bSJens Axboe 	}
140f9c78b2bSJens Axboe 
141f9c78b2bSJens Axboe 	if (WARN(!bslab, KERN_ERR "bio: unable to find slab!\n"))
142f9c78b2bSJens Axboe 		goto out;
143f9c78b2bSJens Axboe 
144f9c78b2bSJens Axboe 	WARN_ON(!bslab->slab_ref);
145f9c78b2bSJens Axboe 
146f9c78b2bSJens Axboe 	if (--bslab->slab_ref)
147f9c78b2bSJens Axboe 		goto out;
148f9c78b2bSJens Axboe 
149f9c78b2bSJens Axboe 	kmem_cache_destroy(bslab->slab);
150f9c78b2bSJens Axboe 	bslab->slab = NULL;
151f9c78b2bSJens Axboe 
152f9c78b2bSJens Axboe out:
153f9c78b2bSJens Axboe 	mutex_unlock(&bio_slab_lock);
154f9c78b2bSJens Axboe }
155f9c78b2bSJens Axboe 
156f9c78b2bSJens Axboe unsigned int bvec_nr_vecs(unsigned short idx)
157f9c78b2bSJens Axboe {
158f9c78b2bSJens Axboe 	return bvec_slabs[idx].nr_vecs;
159f9c78b2bSJens Axboe }
160f9c78b2bSJens Axboe 
161f9c78b2bSJens Axboe void bvec_free(mempool_t *pool, struct bio_vec *bv, unsigned int idx)
162f9c78b2bSJens Axboe {
163f9c78b2bSJens Axboe 	BIO_BUG_ON(idx >= BIOVEC_NR_POOLS);
164f9c78b2bSJens Axboe 
165f9c78b2bSJens Axboe 	if (idx == BIOVEC_MAX_IDX)
166f9c78b2bSJens Axboe 		mempool_free(bv, pool);
167f9c78b2bSJens Axboe 	else {
168f9c78b2bSJens Axboe 		struct biovec_slab *bvs = bvec_slabs + idx;
169f9c78b2bSJens Axboe 
170f9c78b2bSJens Axboe 		kmem_cache_free(bvs->slab, bv);
171f9c78b2bSJens Axboe 	}
172f9c78b2bSJens Axboe }
173f9c78b2bSJens Axboe 
174f9c78b2bSJens Axboe struct bio_vec *bvec_alloc(gfp_t gfp_mask, int nr, unsigned long *idx,
175f9c78b2bSJens Axboe 			   mempool_t *pool)
176f9c78b2bSJens Axboe {
177f9c78b2bSJens Axboe 	struct bio_vec *bvl;
178f9c78b2bSJens Axboe 
179f9c78b2bSJens Axboe 	/*
180f9c78b2bSJens Axboe 	 * see comment near bvec_array define!
181f9c78b2bSJens Axboe 	 */
182f9c78b2bSJens Axboe 	switch (nr) {
183f9c78b2bSJens Axboe 	case 1:
184f9c78b2bSJens Axboe 		*idx = 0;
185f9c78b2bSJens Axboe 		break;
186f9c78b2bSJens Axboe 	case 2 ... 4:
187f9c78b2bSJens Axboe 		*idx = 1;
188f9c78b2bSJens Axboe 		break;
189f9c78b2bSJens Axboe 	case 5 ... 16:
190f9c78b2bSJens Axboe 		*idx = 2;
191f9c78b2bSJens Axboe 		break;
192f9c78b2bSJens Axboe 	case 17 ... 64:
193f9c78b2bSJens Axboe 		*idx = 3;
194f9c78b2bSJens Axboe 		break;
195f9c78b2bSJens Axboe 	case 65 ... 128:
196f9c78b2bSJens Axboe 		*idx = 4;
197f9c78b2bSJens Axboe 		break;
198f9c78b2bSJens Axboe 	case 129 ... BIO_MAX_PAGES:
199f9c78b2bSJens Axboe 		*idx = 5;
200f9c78b2bSJens Axboe 		break;
201f9c78b2bSJens Axboe 	default:
202f9c78b2bSJens Axboe 		return NULL;
203f9c78b2bSJens Axboe 	}
204f9c78b2bSJens Axboe 
205f9c78b2bSJens Axboe 	/*
206f9c78b2bSJens Axboe 	 * idx now points to the pool we want to allocate from. only the
207f9c78b2bSJens Axboe 	 * 1-vec entry pool is mempool backed.
208f9c78b2bSJens Axboe 	 */
209f9c78b2bSJens Axboe 	if (*idx == BIOVEC_MAX_IDX) {
210f9c78b2bSJens Axboe fallback:
211f9c78b2bSJens Axboe 		bvl = mempool_alloc(pool, gfp_mask);
212f9c78b2bSJens Axboe 	} else {
213f9c78b2bSJens Axboe 		struct biovec_slab *bvs = bvec_slabs + *idx;
214f9c78b2bSJens Axboe 		gfp_t __gfp_mask = gfp_mask & ~(__GFP_WAIT | __GFP_IO);
215f9c78b2bSJens Axboe 
216f9c78b2bSJens Axboe 		/*
217f9c78b2bSJens Axboe 		 * Make this allocation restricted and don't dump info on
218f9c78b2bSJens Axboe 		 * allocation failures, since we'll fallback to the mempool
219f9c78b2bSJens Axboe 		 * in case of failure.
220f9c78b2bSJens Axboe 		 */
221f9c78b2bSJens Axboe 		__gfp_mask |= __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
222f9c78b2bSJens Axboe 
223f9c78b2bSJens Axboe 		/*
224f9c78b2bSJens Axboe 		 * Try a slab allocation. If this fails and __GFP_WAIT
225f9c78b2bSJens Axboe 		 * is set, retry with the 1-entry mempool
226f9c78b2bSJens Axboe 		 */
227f9c78b2bSJens Axboe 		bvl = kmem_cache_alloc(bvs->slab, __gfp_mask);
228f9c78b2bSJens Axboe 		if (unlikely(!bvl && (gfp_mask & __GFP_WAIT))) {
229f9c78b2bSJens Axboe 			*idx = BIOVEC_MAX_IDX;
230f9c78b2bSJens Axboe 			goto fallback;
231f9c78b2bSJens Axboe 		}
232f9c78b2bSJens Axboe 	}
233f9c78b2bSJens Axboe 
234f9c78b2bSJens Axboe 	return bvl;
235f9c78b2bSJens Axboe }
236f9c78b2bSJens Axboe 
237f9c78b2bSJens Axboe static void __bio_free(struct bio *bio)
238f9c78b2bSJens Axboe {
239f9c78b2bSJens Axboe 	bio_disassociate_task(bio);
240f9c78b2bSJens Axboe 
241f9c78b2bSJens Axboe 	if (bio_integrity(bio))
242f9c78b2bSJens Axboe 		bio_integrity_free(bio);
243f9c78b2bSJens Axboe }
244f9c78b2bSJens Axboe 
245f9c78b2bSJens Axboe static void bio_free(struct bio *bio)
246f9c78b2bSJens Axboe {
247f9c78b2bSJens Axboe 	struct bio_set *bs = bio->bi_pool;
248f9c78b2bSJens Axboe 	void *p;
249f9c78b2bSJens Axboe 
250f9c78b2bSJens Axboe 	__bio_free(bio);
251f9c78b2bSJens Axboe 
252f9c78b2bSJens Axboe 	if (bs) {
253f9c78b2bSJens Axboe 		if (bio_flagged(bio, BIO_OWNS_VEC))
254f9c78b2bSJens Axboe 			bvec_free(bs->bvec_pool, bio->bi_io_vec, BIO_POOL_IDX(bio));
255f9c78b2bSJens Axboe 
256f9c78b2bSJens Axboe 		/*
257f9c78b2bSJens Axboe 		 * If we have front padding, adjust the bio pointer before freeing
258f9c78b2bSJens Axboe 		 */
259f9c78b2bSJens Axboe 		p = bio;
260f9c78b2bSJens Axboe 		p -= bs->front_pad;
261f9c78b2bSJens Axboe 
262f9c78b2bSJens Axboe 		mempool_free(p, bs->bio_pool);
263f9c78b2bSJens Axboe 	} else {
264f9c78b2bSJens Axboe 		/* Bio was allocated by bio_kmalloc() */
265f9c78b2bSJens Axboe 		kfree(bio);
266f9c78b2bSJens Axboe 	}
267f9c78b2bSJens Axboe }
268f9c78b2bSJens Axboe 
269f9c78b2bSJens Axboe void bio_init(struct bio *bio)
270f9c78b2bSJens Axboe {
271f9c78b2bSJens Axboe 	memset(bio, 0, sizeof(*bio));
272f9c78b2bSJens Axboe 	bio->bi_flags = 1 << BIO_UPTODATE;
273f9c78b2bSJens Axboe 	atomic_set(&bio->bi_remaining, 1);
274f9c78b2bSJens Axboe 	atomic_set(&bio->bi_cnt, 1);
275f9c78b2bSJens Axboe }
276f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_init);
277f9c78b2bSJens Axboe 
278f9c78b2bSJens Axboe /**
279f9c78b2bSJens Axboe  * bio_reset - reinitialize a bio
280f9c78b2bSJens Axboe  * @bio:	bio to reset
281f9c78b2bSJens Axboe  *
282f9c78b2bSJens Axboe  * Description:
283f9c78b2bSJens Axboe  *   After calling bio_reset(), @bio will be in the same state as a freshly
284f9c78b2bSJens Axboe  *   allocated bio returned bio bio_alloc_bioset() - the only fields that are
285f9c78b2bSJens Axboe  *   preserved are the ones that are initialized by bio_alloc_bioset(). See
286f9c78b2bSJens Axboe  *   comment in struct bio.
287f9c78b2bSJens Axboe  */
288f9c78b2bSJens Axboe void bio_reset(struct bio *bio)
289f9c78b2bSJens Axboe {
290f9c78b2bSJens Axboe 	unsigned long flags = bio->bi_flags & (~0UL << BIO_RESET_BITS);
291f9c78b2bSJens Axboe 
292f9c78b2bSJens Axboe 	__bio_free(bio);
293f9c78b2bSJens Axboe 
294f9c78b2bSJens Axboe 	memset(bio, 0, BIO_RESET_BYTES);
295f9c78b2bSJens Axboe 	bio->bi_flags = flags|(1 << BIO_UPTODATE);
296f9c78b2bSJens Axboe 	atomic_set(&bio->bi_remaining, 1);
297f9c78b2bSJens Axboe }
298f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_reset);
299f9c78b2bSJens Axboe 
300f9c78b2bSJens Axboe static void bio_chain_endio(struct bio *bio, int error)
301f9c78b2bSJens Axboe {
302f9c78b2bSJens Axboe 	bio_endio(bio->bi_private, error);
303f9c78b2bSJens Axboe 	bio_put(bio);
304f9c78b2bSJens Axboe }
305f9c78b2bSJens Axboe 
306f9c78b2bSJens Axboe /**
307f9c78b2bSJens Axboe  * bio_chain - chain bio completions
308f9c78b2bSJens Axboe  * @bio: the target bio
309f9c78b2bSJens Axboe  * @parent: the @bio's parent bio
310f9c78b2bSJens Axboe  *
311f9c78b2bSJens Axboe  * The caller won't have a bi_end_io called when @bio completes - instead,
312f9c78b2bSJens Axboe  * @parent's bi_end_io won't be called until both @parent and @bio have
313f9c78b2bSJens Axboe  * completed; the chained bio will also be freed when it completes.
314f9c78b2bSJens Axboe  *
315f9c78b2bSJens Axboe  * The caller must not set bi_private or bi_end_io in @bio.
316f9c78b2bSJens Axboe  */
317f9c78b2bSJens Axboe void bio_chain(struct bio *bio, struct bio *parent)
318f9c78b2bSJens Axboe {
319f9c78b2bSJens Axboe 	BUG_ON(bio->bi_private || bio->bi_end_io);
320f9c78b2bSJens Axboe 
321f9c78b2bSJens Axboe 	bio->bi_private = parent;
322f9c78b2bSJens Axboe 	bio->bi_end_io	= bio_chain_endio;
323f9c78b2bSJens Axboe 	atomic_inc(&parent->bi_remaining);
324f9c78b2bSJens Axboe }
325f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_chain);
326f9c78b2bSJens Axboe 
327f9c78b2bSJens Axboe static void bio_alloc_rescue(struct work_struct *work)
328f9c78b2bSJens Axboe {
329f9c78b2bSJens Axboe 	struct bio_set *bs = container_of(work, struct bio_set, rescue_work);
330f9c78b2bSJens Axboe 	struct bio *bio;
331f9c78b2bSJens Axboe 
332f9c78b2bSJens Axboe 	while (1) {
333f9c78b2bSJens Axboe 		spin_lock(&bs->rescue_lock);
334f9c78b2bSJens Axboe 		bio = bio_list_pop(&bs->rescue_list);
335f9c78b2bSJens Axboe 		spin_unlock(&bs->rescue_lock);
336f9c78b2bSJens Axboe 
337f9c78b2bSJens Axboe 		if (!bio)
338f9c78b2bSJens Axboe 			break;
339f9c78b2bSJens Axboe 
340f9c78b2bSJens Axboe 		generic_make_request(bio);
341f9c78b2bSJens Axboe 	}
342f9c78b2bSJens Axboe }
343f9c78b2bSJens Axboe 
344f9c78b2bSJens Axboe static void punt_bios_to_rescuer(struct bio_set *bs)
345f9c78b2bSJens Axboe {
346f9c78b2bSJens Axboe 	struct bio_list punt, nopunt;
347f9c78b2bSJens Axboe 	struct bio *bio;
348f9c78b2bSJens Axboe 
349f9c78b2bSJens Axboe 	/*
350f9c78b2bSJens Axboe 	 * In order to guarantee forward progress we must punt only bios that
351f9c78b2bSJens Axboe 	 * were allocated from this bio_set; otherwise, if there was a bio on
352f9c78b2bSJens Axboe 	 * there for a stacking driver higher up in the stack, processing it
353f9c78b2bSJens Axboe 	 * could require allocating bios from this bio_set, and doing that from
354f9c78b2bSJens Axboe 	 * our own rescuer would be bad.
355f9c78b2bSJens Axboe 	 *
356f9c78b2bSJens Axboe 	 * Since bio lists are singly linked, pop them all instead of trying to
357f9c78b2bSJens Axboe 	 * remove from the middle of the list:
358f9c78b2bSJens Axboe 	 */
359f9c78b2bSJens Axboe 
360f9c78b2bSJens Axboe 	bio_list_init(&punt);
361f9c78b2bSJens Axboe 	bio_list_init(&nopunt);
362f9c78b2bSJens Axboe 
363f9c78b2bSJens Axboe 	while ((bio = bio_list_pop(current->bio_list)))
364f9c78b2bSJens Axboe 		bio_list_add(bio->bi_pool == bs ? &punt : &nopunt, bio);
365f9c78b2bSJens Axboe 
366f9c78b2bSJens Axboe 	*current->bio_list = nopunt;
367f9c78b2bSJens Axboe 
368f9c78b2bSJens Axboe 	spin_lock(&bs->rescue_lock);
369f9c78b2bSJens Axboe 	bio_list_merge(&bs->rescue_list, &punt);
370f9c78b2bSJens Axboe 	spin_unlock(&bs->rescue_lock);
371f9c78b2bSJens Axboe 
372f9c78b2bSJens Axboe 	queue_work(bs->rescue_workqueue, &bs->rescue_work);
373f9c78b2bSJens Axboe }
374f9c78b2bSJens Axboe 
375f9c78b2bSJens Axboe /**
376f9c78b2bSJens Axboe  * bio_alloc_bioset - allocate a bio for I/O
377f9c78b2bSJens Axboe  * @gfp_mask:   the GFP_ mask given to the slab allocator
378f9c78b2bSJens Axboe  * @nr_iovecs:	number of iovecs to pre-allocate
379f9c78b2bSJens Axboe  * @bs:		the bio_set to allocate from.
380f9c78b2bSJens Axboe  *
381f9c78b2bSJens Axboe  * Description:
382f9c78b2bSJens Axboe  *   If @bs is NULL, uses kmalloc() to allocate the bio; else the allocation is
383f9c78b2bSJens Axboe  *   backed by the @bs's mempool.
384f9c78b2bSJens Axboe  *
385f9c78b2bSJens Axboe  *   When @bs is not NULL, if %__GFP_WAIT is set then bio_alloc will always be
386f9c78b2bSJens Axboe  *   able to allocate a bio. This is due to the mempool guarantees. To make this
387f9c78b2bSJens Axboe  *   work, callers must never allocate more than 1 bio at a time from this pool.
388f9c78b2bSJens Axboe  *   Callers that need to allocate more than 1 bio must always submit the
389f9c78b2bSJens Axboe  *   previously allocated bio for IO before attempting to allocate a new one.
390f9c78b2bSJens Axboe  *   Failure to do so can cause deadlocks under memory pressure.
391f9c78b2bSJens Axboe  *
392f9c78b2bSJens Axboe  *   Note that when running under generic_make_request() (i.e. any block
393f9c78b2bSJens Axboe  *   driver), bios are not submitted until after you return - see the code in
394f9c78b2bSJens Axboe  *   generic_make_request() that converts recursion into iteration, to prevent
395f9c78b2bSJens Axboe  *   stack overflows.
396f9c78b2bSJens Axboe  *
397f9c78b2bSJens Axboe  *   This would normally mean allocating multiple bios under
398f9c78b2bSJens Axboe  *   generic_make_request() would be susceptible to deadlocks, but we have
399f9c78b2bSJens Axboe  *   deadlock avoidance code that resubmits any blocked bios from a rescuer
400f9c78b2bSJens Axboe  *   thread.
401f9c78b2bSJens Axboe  *
402f9c78b2bSJens Axboe  *   However, we do not guarantee forward progress for allocations from other
403f9c78b2bSJens Axboe  *   mempools. Doing multiple allocations from the same mempool under
404f9c78b2bSJens Axboe  *   generic_make_request() should be avoided - instead, use bio_set's front_pad
405f9c78b2bSJens Axboe  *   for per bio allocations.
406f9c78b2bSJens Axboe  *
407f9c78b2bSJens Axboe  *   RETURNS:
408f9c78b2bSJens Axboe  *   Pointer to new bio on success, NULL on failure.
409f9c78b2bSJens Axboe  */
410f9c78b2bSJens Axboe struct bio *bio_alloc_bioset(gfp_t gfp_mask, int nr_iovecs, struct bio_set *bs)
411f9c78b2bSJens Axboe {
412f9c78b2bSJens Axboe 	gfp_t saved_gfp = gfp_mask;
413f9c78b2bSJens Axboe 	unsigned front_pad;
414f9c78b2bSJens Axboe 	unsigned inline_vecs;
415f9c78b2bSJens Axboe 	unsigned long idx = BIO_POOL_NONE;
416f9c78b2bSJens Axboe 	struct bio_vec *bvl = NULL;
417f9c78b2bSJens Axboe 	struct bio *bio;
418f9c78b2bSJens Axboe 	void *p;
419f9c78b2bSJens Axboe 
420f9c78b2bSJens Axboe 	if (!bs) {
421f9c78b2bSJens Axboe 		if (nr_iovecs > UIO_MAXIOV)
422f9c78b2bSJens Axboe 			return NULL;
423f9c78b2bSJens Axboe 
424f9c78b2bSJens Axboe 		p = kmalloc(sizeof(struct bio) +
425f9c78b2bSJens Axboe 			    nr_iovecs * sizeof(struct bio_vec),
426f9c78b2bSJens Axboe 			    gfp_mask);
427f9c78b2bSJens Axboe 		front_pad = 0;
428f9c78b2bSJens Axboe 		inline_vecs = nr_iovecs;
429f9c78b2bSJens Axboe 	} else {
430f9c78b2bSJens Axboe 		/*
431f9c78b2bSJens Axboe 		 * generic_make_request() converts recursion to iteration; this
432f9c78b2bSJens Axboe 		 * means if we're running beneath it, any bios we allocate and
433f9c78b2bSJens Axboe 		 * submit will not be submitted (and thus freed) until after we
434f9c78b2bSJens Axboe 		 * return.
435f9c78b2bSJens Axboe 		 *
436f9c78b2bSJens Axboe 		 * This exposes us to a potential deadlock if we allocate
437f9c78b2bSJens Axboe 		 * multiple bios from the same bio_set() while running
438f9c78b2bSJens Axboe 		 * underneath generic_make_request(). If we were to allocate
439f9c78b2bSJens Axboe 		 * multiple bios (say a stacking block driver that was splitting
440f9c78b2bSJens Axboe 		 * bios), we would deadlock if we exhausted the mempool's
441f9c78b2bSJens Axboe 		 * reserve.
442f9c78b2bSJens Axboe 		 *
443f9c78b2bSJens Axboe 		 * We solve this, and guarantee forward progress, with a rescuer
444f9c78b2bSJens Axboe 		 * workqueue per bio_set. If we go to allocate and there are
445f9c78b2bSJens Axboe 		 * bios on current->bio_list, we first try the allocation
446f9c78b2bSJens Axboe 		 * without __GFP_WAIT; if that fails, we punt those bios we
447f9c78b2bSJens Axboe 		 * would be blocking to the rescuer workqueue before we retry
448f9c78b2bSJens Axboe 		 * with the original gfp_flags.
449f9c78b2bSJens Axboe 		 */
450f9c78b2bSJens Axboe 
451f9c78b2bSJens Axboe 		if (current->bio_list && !bio_list_empty(current->bio_list))
452f9c78b2bSJens Axboe 			gfp_mask &= ~__GFP_WAIT;
453f9c78b2bSJens Axboe 
454f9c78b2bSJens Axboe 		p = mempool_alloc(bs->bio_pool, gfp_mask);
455f9c78b2bSJens Axboe 		if (!p && gfp_mask != saved_gfp) {
456f9c78b2bSJens Axboe 			punt_bios_to_rescuer(bs);
457f9c78b2bSJens Axboe 			gfp_mask = saved_gfp;
458f9c78b2bSJens Axboe 			p = mempool_alloc(bs->bio_pool, gfp_mask);
459f9c78b2bSJens Axboe 		}
460f9c78b2bSJens Axboe 
461f9c78b2bSJens Axboe 		front_pad = bs->front_pad;
462f9c78b2bSJens Axboe 		inline_vecs = BIO_INLINE_VECS;
463f9c78b2bSJens Axboe 	}
464f9c78b2bSJens Axboe 
465f9c78b2bSJens Axboe 	if (unlikely(!p))
466f9c78b2bSJens Axboe 		return NULL;
467f9c78b2bSJens Axboe 
468f9c78b2bSJens Axboe 	bio = p + front_pad;
469f9c78b2bSJens Axboe 	bio_init(bio);
470f9c78b2bSJens Axboe 
471f9c78b2bSJens Axboe 	if (nr_iovecs > inline_vecs) {
472f9c78b2bSJens Axboe 		bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx, bs->bvec_pool);
473f9c78b2bSJens Axboe 		if (!bvl && gfp_mask != saved_gfp) {
474f9c78b2bSJens Axboe 			punt_bios_to_rescuer(bs);
475f9c78b2bSJens Axboe 			gfp_mask = saved_gfp;
476f9c78b2bSJens Axboe 			bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx, bs->bvec_pool);
477f9c78b2bSJens Axboe 		}
478f9c78b2bSJens Axboe 
479f9c78b2bSJens Axboe 		if (unlikely(!bvl))
480f9c78b2bSJens Axboe 			goto err_free;
481f9c78b2bSJens Axboe 
482f9c78b2bSJens Axboe 		bio->bi_flags |= 1 << BIO_OWNS_VEC;
483f9c78b2bSJens Axboe 	} else if (nr_iovecs) {
484f9c78b2bSJens Axboe 		bvl = bio->bi_inline_vecs;
485f9c78b2bSJens Axboe 	}
486f9c78b2bSJens Axboe 
487f9c78b2bSJens Axboe 	bio->bi_pool = bs;
488f9c78b2bSJens Axboe 	bio->bi_flags |= idx << BIO_POOL_OFFSET;
489f9c78b2bSJens Axboe 	bio->bi_max_vecs = nr_iovecs;
490f9c78b2bSJens Axboe 	bio->bi_io_vec = bvl;
491f9c78b2bSJens Axboe 	return bio;
492f9c78b2bSJens Axboe 
493f9c78b2bSJens Axboe err_free:
494f9c78b2bSJens Axboe 	mempool_free(p, bs->bio_pool);
495f9c78b2bSJens Axboe 	return NULL;
496f9c78b2bSJens Axboe }
497f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_alloc_bioset);
498f9c78b2bSJens Axboe 
499f9c78b2bSJens Axboe void zero_fill_bio(struct bio *bio)
500f9c78b2bSJens Axboe {
501f9c78b2bSJens Axboe 	unsigned long flags;
502f9c78b2bSJens Axboe 	struct bio_vec bv;
503f9c78b2bSJens Axboe 	struct bvec_iter iter;
504f9c78b2bSJens Axboe 
505f9c78b2bSJens Axboe 	bio_for_each_segment(bv, bio, iter) {
506f9c78b2bSJens Axboe 		char *data = bvec_kmap_irq(&bv, &flags);
507f9c78b2bSJens Axboe 		memset(data, 0, bv.bv_len);
508f9c78b2bSJens Axboe 		flush_dcache_page(bv.bv_page);
509f9c78b2bSJens Axboe 		bvec_kunmap_irq(data, &flags);
510f9c78b2bSJens Axboe 	}
511f9c78b2bSJens Axboe }
512f9c78b2bSJens Axboe EXPORT_SYMBOL(zero_fill_bio);
513f9c78b2bSJens Axboe 
514f9c78b2bSJens Axboe /**
515f9c78b2bSJens Axboe  * bio_put - release a reference to a bio
516f9c78b2bSJens Axboe  * @bio:   bio to release reference to
517f9c78b2bSJens Axboe  *
518f9c78b2bSJens Axboe  * Description:
519f9c78b2bSJens Axboe  *   Put a reference to a &struct bio, either one you have gotten with
520f9c78b2bSJens Axboe  *   bio_alloc, bio_get or bio_clone. The last put of a bio will free it.
521f9c78b2bSJens Axboe  **/
522f9c78b2bSJens Axboe void bio_put(struct bio *bio)
523f9c78b2bSJens Axboe {
524f9c78b2bSJens Axboe 	BIO_BUG_ON(!atomic_read(&bio->bi_cnt));
525f9c78b2bSJens Axboe 
526f9c78b2bSJens Axboe 	/*
527f9c78b2bSJens Axboe 	 * last put frees it
528f9c78b2bSJens Axboe 	 */
529f9c78b2bSJens Axboe 	if (atomic_dec_and_test(&bio->bi_cnt))
530f9c78b2bSJens Axboe 		bio_free(bio);
531f9c78b2bSJens Axboe }
532f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_put);
533f9c78b2bSJens Axboe 
534f9c78b2bSJens Axboe inline int bio_phys_segments(struct request_queue *q, struct bio *bio)
535f9c78b2bSJens Axboe {
536f9c78b2bSJens Axboe 	if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
537f9c78b2bSJens Axboe 		blk_recount_segments(q, bio);
538f9c78b2bSJens Axboe 
539f9c78b2bSJens Axboe 	return bio->bi_phys_segments;
540f9c78b2bSJens Axboe }
541f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_phys_segments);
542f9c78b2bSJens Axboe 
543f9c78b2bSJens Axboe /**
544f9c78b2bSJens Axboe  * 	__bio_clone_fast - clone a bio that shares the original bio's biovec
545f9c78b2bSJens Axboe  * 	@bio: destination bio
546f9c78b2bSJens Axboe  * 	@bio_src: bio to clone
547f9c78b2bSJens Axboe  *
548f9c78b2bSJens Axboe  *	Clone a &bio. Caller will own the returned bio, but not
549f9c78b2bSJens Axboe  *	the actual data it points to. Reference count of returned
550f9c78b2bSJens Axboe  * 	bio will be one.
551f9c78b2bSJens Axboe  *
552f9c78b2bSJens Axboe  * 	Caller must ensure that @bio_src is not freed before @bio.
553f9c78b2bSJens Axboe  */
554f9c78b2bSJens Axboe void __bio_clone_fast(struct bio *bio, struct bio *bio_src)
555f9c78b2bSJens Axboe {
556f9c78b2bSJens Axboe 	BUG_ON(bio->bi_pool && BIO_POOL_IDX(bio) != BIO_POOL_NONE);
557f9c78b2bSJens Axboe 
558f9c78b2bSJens Axboe 	/*
559f9c78b2bSJens Axboe 	 * most users will be overriding ->bi_bdev with a new target,
560f9c78b2bSJens Axboe 	 * so we don't set nor calculate new physical/hw segment counts here
561f9c78b2bSJens Axboe 	 */
562f9c78b2bSJens Axboe 	bio->bi_bdev = bio_src->bi_bdev;
563f9c78b2bSJens Axboe 	bio->bi_flags |= 1 << BIO_CLONED;
564f9c78b2bSJens Axboe 	bio->bi_rw = bio_src->bi_rw;
565f9c78b2bSJens Axboe 	bio->bi_iter = bio_src->bi_iter;
566f9c78b2bSJens Axboe 	bio->bi_io_vec = bio_src->bi_io_vec;
567f9c78b2bSJens Axboe }
568f9c78b2bSJens Axboe EXPORT_SYMBOL(__bio_clone_fast);
569f9c78b2bSJens Axboe 
570f9c78b2bSJens Axboe /**
571f9c78b2bSJens Axboe  *	bio_clone_fast - clone a bio that shares the original bio's biovec
572f9c78b2bSJens Axboe  *	@bio: bio to clone
573f9c78b2bSJens Axboe  *	@gfp_mask: allocation priority
574f9c78b2bSJens Axboe  *	@bs: bio_set to allocate from
575f9c78b2bSJens Axboe  *
576f9c78b2bSJens Axboe  * 	Like __bio_clone_fast, only also allocates the returned bio
577f9c78b2bSJens Axboe  */
578f9c78b2bSJens Axboe struct bio *bio_clone_fast(struct bio *bio, gfp_t gfp_mask, struct bio_set *bs)
579f9c78b2bSJens Axboe {
580f9c78b2bSJens Axboe 	struct bio *b;
581f9c78b2bSJens Axboe 
582f9c78b2bSJens Axboe 	b = bio_alloc_bioset(gfp_mask, 0, bs);
583f9c78b2bSJens Axboe 	if (!b)
584f9c78b2bSJens Axboe 		return NULL;
585f9c78b2bSJens Axboe 
586f9c78b2bSJens Axboe 	__bio_clone_fast(b, bio);
587f9c78b2bSJens Axboe 
588f9c78b2bSJens Axboe 	if (bio_integrity(bio)) {
589f9c78b2bSJens Axboe 		int ret;
590f9c78b2bSJens Axboe 
591f9c78b2bSJens Axboe 		ret = bio_integrity_clone(b, bio, gfp_mask);
592f9c78b2bSJens Axboe 
593f9c78b2bSJens Axboe 		if (ret < 0) {
594f9c78b2bSJens Axboe 			bio_put(b);
595f9c78b2bSJens Axboe 			return NULL;
596f9c78b2bSJens Axboe 		}
597f9c78b2bSJens Axboe 	}
598f9c78b2bSJens Axboe 
599f9c78b2bSJens Axboe 	return b;
600f9c78b2bSJens Axboe }
601f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_clone_fast);
602f9c78b2bSJens Axboe 
603f9c78b2bSJens Axboe /**
604f9c78b2bSJens Axboe  * 	bio_clone_bioset - clone a bio
605f9c78b2bSJens Axboe  * 	@bio_src: bio to clone
606f9c78b2bSJens Axboe  *	@gfp_mask: allocation priority
607f9c78b2bSJens Axboe  *	@bs: bio_set to allocate from
608f9c78b2bSJens Axboe  *
609f9c78b2bSJens Axboe  *	Clone bio. Caller will own the returned bio, but not the actual data it
610f9c78b2bSJens Axboe  *	points to. Reference count of returned bio will be one.
611f9c78b2bSJens Axboe  */
612f9c78b2bSJens Axboe struct bio *bio_clone_bioset(struct bio *bio_src, gfp_t gfp_mask,
613f9c78b2bSJens Axboe 			     struct bio_set *bs)
614f9c78b2bSJens Axboe {
615f9c78b2bSJens Axboe 	struct bvec_iter iter;
616f9c78b2bSJens Axboe 	struct bio_vec bv;
617f9c78b2bSJens Axboe 	struct bio *bio;
618f9c78b2bSJens Axboe 
619f9c78b2bSJens Axboe 	/*
620f9c78b2bSJens Axboe 	 * Pre immutable biovecs, __bio_clone() used to just do a memcpy from
621f9c78b2bSJens Axboe 	 * bio_src->bi_io_vec to bio->bi_io_vec.
622f9c78b2bSJens Axboe 	 *
623f9c78b2bSJens Axboe 	 * We can't do that anymore, because:
624f9c78b2bSJens Axboe 	 *
625f9c78b2bSJens Axboe 	 *  - The point of cloning the biovec is to produce a bio with a biovec
626f9c78b2bSJens Axboe 	 *    the caller can modify: bi_idx and bi_bvec_done should be 0.
627f9c78b2bSJens Axboe 	 *
628f9c78b2bSJens Axboe 	 *  - The original bio could've had more than BIO_MAX_PAGES biovecs; if
629f9c78b2bSJens Axboe 	 *    we tried to clone the whole thing bio_alloc_bioset() would fail.
630f9c78b2bSJens Axboe 	 *    But the clone should succeed as long as the number of biovecs we
631f9c78b2bSJens Axboe 	 *    actually need to allocate is fewer than BIO_MAX_PAGES.
632f9c78b2bSJens Axboe 	 *
633f9c78b2bSJens Axboe 	 *  - Lastly, bi_vcnt should not be looked at or relied upon by code
634f9c78b2bSJens Axboe 	 *    that does not own the bio - reason being drivers don't use it for
635f9c78b2bSJens Axboe 	 *    iterating over the biovec anymore, so expecting it to be kept up
636f9c78b2bSJens Axboe 	 *    to date (i.e. for clones that share the parent biovec) is just
637f9c78b2bSJens Axboe 	 *    asking for trouble and would force extra work on
638f9c78b2bSJens Axboe 	 *    __bio_clone_fast() anyways.
639f9c78b2bSJens Axboe 	 */
640f9c78b2bSJens Axboe 
641f9c78b2bSJens Axboe 	bio = bio_alloc_bioset(gfp_mask, bio_segments(bio_src), bs);
642f9c78b2bSJens Axboe 	if (!bio)
643f9c78b2bSJens Axboe 		return NULL;
644f9c78b2bSJens Axboe 
645f9c78b2bSJens Axboe 	bio->bi_bdev		= bio_src->bi_bdev;
646f9c78b2bSJens Axboe 	bio->bi_rw		= bio_src->bi_rw;
647f9c78b2bSJens Axboe 	bio->bi_iter.bi_sector	= bio_src->bi_iter.bi_sector;
648f9c78b2bSJens Axboe 	bio->bi_iter.bi_size	= bio_src->bi_iter.bi_size;
649f9c78b2bSJens Axboe 
650f9c78b2bSJens Axboe 	if (bio->bi_rw & REQ_DISCARD)
651f9c78b2bSJens Axboe 		goto integrity_clone;
652f9c78b2bSJens Axboe 
653f9c78b2bSJens Axboe 	if (bio->bi_rw & REQ_WRITE_SAME) {
654f9c78b2bSJens Axboe 		bio->bi_io_vec[bio->bi_vcnt++] = bio_src->bi_io_vec[0];
655f9c78b2bSJens Axboe 		goto integrity_clone;
656f9c78b2bSJens Axboe 	}
657f9c78b2bSJens Axboe 
658f9c78b2bSJens Axboe 	bio_for_each_segment(bv, bio_src, iter)
659f9c78b2bSJens Axboe 		bio->bi_io_vec[bio->bi_vcnt++] = bv;
660f9c78b2bSJens Axboe 
661f9c78b2bSJens Axboe integrity_clone:
662f9c78b2bSJens Axboe 	if (bio_integrity(bio_src)) {
663f9c78b2bSJens Axboe 		int ret;
664f9c78b2bSJens Axboe 
665f9c78b2bSJens Axboe 		ret = bio_integrity_clone(bio, bio_src, gfp_mask);
666f9c78b2bSJens Axboe 		if (ret < 0) {
667f9c78b2bSJens Axboe 			bio_put(bio);
668f9c78b2bSJens Axboe 			return NULL;
669f9c78b2bSJens Axboe 		}
670f9c78b2bSJens Axboe 	}
671f9c78b2bSJens Axboe 
672f9c78b2bSJens Axboe 	return bio;
673f9c78b2bSJens Axboe }
674f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_clone_bioset);
675f9c78b2bSJens Axboe 
676f9c78b2bSJens Axboe /**
677f9c78b2bSJens Axboe  *	bio_get_nr_vecs		- return approx number of vecs
678f9c78b2bSJens Axboe  *	@bdev:  I/O target
679f9c78b2bSJens Axboe  *
680f9c78b2bSJens Axboe  *	Return the approximate number of pages we can send to this target.
681f9c78b2bSJens Axboe  *	There's no guarantee that you will be able to fit this number of pages
682f9c78b2bSJens Axboe  *	into a bio, it does not account for dynamic restrictions that vary
683f9c78b2bSJens Axboe  *	on offset.
684f9c78b2bSJens Axboe  */
685f9c78b2bSJens Axboe int bio_get_nr_vecs(struct block_device *bdev)
686f9c78b2bSJens Axboe {
687f9c78b2bSJens Axboe 	struct request_queue *q = bdev_get_queue(bdev);
688f9c78b2bSJens Axboe 	int nr_pages;
689f9c78b2bSJens Axboe 
690f9c78b2bSJens Axboe 	nr_pages = min_t(unsigned,
691f9c78b2bSJens Axboe 		     queue_max_segments(q),
692f9c78b2bSJens Axboe 		     queue_max_sectors(q) / (PAGE_SIZE >> 9) + 1);
693f9c78b2bSJens Axboe 
694f9c78b2bSJens Axboe 	return min_t(unsigned, nr_pages, BIO_MAX_PAGES);
695f9c78b2bSJens Axboe 
696f9c78b2bSJens Axboe }
697f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_get_nr_vecs);
698f9c78b2bSJens Axboe 
699f9c78b2bSJens Axboe static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
700f9c78b2bSJens Axboe 			  *page, unsigned int len, unsigned int offset,
701f9c78b2bSJens Axboe 			  unsigned int max_sectors)
702f9c78b2bSJens Axboe {
703f9c78b2bSJens Axboe 	int retried_segments = 0;
704f9c78b2bSJens Axboe 	struct bio_vec *bvec;
705f9c78b2bSJens Axboe 
706f9c78b2bSJens Axboe 	/*
707f9c78b2bSJens Axboe 	 * cloned bio must not modify vec list
708f9c78b2bSJens Axboe 	 */
709f9c78b2bSJens Axboe 	if (unlikely(bio_flagged(bio, BIO_CLONED)))
710f9c78b2bSJens Axboe 		return 0;
711f9c78b2bSJens Axboe 
712f9c78b2bSJens Axboe 	if (((bio->bi_iter.bi_size + len) >> 9) > max_sectors)
713f9c78b2bSJens Axboe 		return 0;
714f9c78b2bSJens Axboe 
715f9c78b2bSJens Axboe 	/*
716f9c78b2bSJens Axboe 	 * For filesystems with a blocksize smaller than the pagesize
717f9c78b2bSJens Axboe 	 * we will often be called with the same page as last time and
718f9c78b2bSJens Axboe 	 * a consecutive offset.  Optimize this special case.
719f9c78b2bSJens Axboe 	 */
720f9c78b2bSJens Axboe 	if (bio->bi_vcnt > 0) {
721f9c78b2bSJens Axboe 		struct bio_vec *prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
722f9c78b2bSJens Axboe 
723f9c78b2bSJens Axboe 		if (page == prev->bv_page &&
724f9c78b2bSJens Axboe 		    offset == prev->bv_offset + prev->bv_len) {
725f9c78b2bSJens Axboe 			unsigned int prev_bv_len = prev->bv_len;
726f9c78b2bSJens Axboe 			prev->bv_len += len;
727f9c78b2bSJens Axboe 
728f9c78b2bSJens Axboe 			if (q->merge_bvec_fn) {
729f9c78b2bSJens Axboe 				struct bvec_merge_data bvm = {
730f9c78b2bSJens Axboe 					/* prev_bvec is already charged in
731f9c78b2bSJens Axboe 					   bi_size, discharge it in order to
732f9c78b2bSJens Axboe 					   simulate merging updated prev_bvec
733f9c78b2bSJens Axboe 					   as new bvec. */
734f9c78b2bSJens Axboe 					.bi_bdev = bio->bi_bdev,
735f9c78b2bSJens Axboe 					.bi_sector = bio->bi_iter.bi_sector,
736f9c78b2bSJens Axboe 					.bi_size = bio->bi_iter.bi_size -
737f9c78b2bSJens Axboe 						prev_bv_len,
738f9c78b2bSJens Axboe 					.bi_rw = bio->bi_rw,
739f9c78b2bSJens Axboe 				};
740f9c78b2bSJens Axboe 
741f9c78b2bSJens Axboe 				if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len) {
742f9c78b2bSJens Axboe 					prev->bv_len -= len;
743f9c78b2bSJens Axboe 					return 0;
744f9c78b2bSJens Axboe 				}
745f9c78b2bSJens Axboe 			}
746f9c78b2bSJens Axboe 
747f9c78b2bSJens Axboe 			goto done;
748f9c78b2bSJens Axboe 		}
749f9c78b2bSJens Axboe 	}
750f9c78b2bSJens Axboe 
751f9c78b2bSJens Axboe 	if (bio->bi_vcnt >= bio->bi_max_vecs)
752f9c78b2bSJens Axboe 		return 0;
753f9c78b2bSJens Axboe 
754f9c78b2bSJens Axboe 	/*
755f9c78b2bSJens Axboe 	 * we might lose a segment or two here, but rather that than
756f9c78b2bSJens Axboe 	 * make this too complex.
757f9c78b2bSJens Axboe 	 */
758f9c78b2bSJens Axboe 
759f9c78b2bSJens Axboe 	while (bio->bi_phys_segments >= queue_max_segments(q)) {
760f9c78b2bSJens Axboe 
761f9c78b2bSJens Axboe 		if (retried_segments)
762f9c78b2bSJens Axboe 			return 0;
763f9c78b2bSJens Axboe 
764f9c78b2bSJens Axboe 		retried_segments = 1;
765f9c78b2bSJens Axboe 		blk_recount_segments(q, bio);
766f9c78b2bSJens Axboe 	}
767f9c78b2bSJens Axboe 
768f9c78b2bSJens Axboe 	/*
769f9c78b2bSJens Axboe 	 * setup the new entry, we might clear it again later if we
770f9c78b2bSJens Axboe 	 * cannot add the page
771f9c78b2bSJens Axboe 	 */
772f9c78b2bSJens Axboe 	bvec = &bio->bi_io_vec[bio->bi_vcnt];
773f9c78b2bSJens Axboe 	bvec->bv_page = page;
774f9c78b2bSJens Axboe 	bvec->bv_len = len;
775f9c78b2bSJens Axboe 	bvec->bv_offset = offset;
776f9c78b2bSJens Axboe 
777f9c78b2bSJens Axboe 	/*
778f9c78b2bSJens Axboe 	 * if queue has other restrictions (eg varying max sector size
779f9c78b2bSJens Axboe 	 * depending on offset), it can specify a merge_bvec_fn in the
780f9c78b2bSJens Axboe 	 * queue to get further control
781f9c78b2bSJens Axboe 	 */
782f9c78b2bSJens Axboe 	if (q->merge_bvec_fn) {
783f9c78b2bSJens Axboe 		struct bvec_merge_data bvm = {
784f9c78b2bSJens Axboe 			.bi_bdev = bio->bi_bdev,
785f9c78b2bSJens Axboe 			.bi_sector = bio->bi_iter.bi_sector,
786f9c78b2bSJens Axboe 			.bi_size = bio->bi_iter.bi_size,
787f9c78b2bSJens Axboe 			.bi_rw = bio->bi_rw,
788f9c78b2bSJens Axboe 		};
789f9c78b2bSJens Axboe 
790f9c78b2bSJens Axboe 		/*
791f9c78b2bSJens Axboe 		 * merge_bvec_fn() returns number of bytes it can accept
792f9c78b2bSJens Axboe 		 * at this offset
793f9c78b2bSJens Axboe 		 */
794f9c78b2bSJens Axboe 		if (q->merge_bvec_fn(q, &bvm, bvec) < bvec->bv_len) {
795f9c78b2bSJens Axboe 			bvec->bv_page = NULL;
796f9c78b2bSJens Axboe 			bvec->bv_len = 0;
797f9c78b2bSJens Axboe 			bvec->bv_offset = 0;
798f9c78b2bSJens Axboe 			return 0;
799f9c78b2bSJens Axboe 		}
800f9c78b2bSJens Axboe 	}
801f9c78b2bSJens Axboe 
802f9c78b2bSJens Axboe 	/* If we may be able to merge these biovecs, force a recount */
803f9c78b2bSJens Axboe 	if (bio->bi_vcnt && (BIOVEC_PHYS_MERGEABLE(bvec-1, bvec)))
804f9c78b2bSJens Axboe 		bio->bi_flags &= ~(1 << BIO_SEG_VALID);
805f9c78b2bSJens Axboe 
806f9c78b2bSJens Axboe 	bio->bi_vcnt++;
807f9c78b2bSJens Axboe 	bio->bi_phys_segments++;
808f9c78b2bSJens Axboe  done:
809f9c78b2bSJens Axboe 	bio->bi_iter.bi_size += len;
810f9c78b2bSJens Axboe 	return len;
811f9c78b2bSJens Axboe }
812f9c78b2bSJens Axboe 
813f9c78b2bSJens Axboe /**
814f9c78b2bSJens Axboe  *	bio_add_pc_page	-	attempt to add page to bio
815f9c78b2bSJens Axboe  *	@q: the target queue
816f9c78b2bSJens Axboe  *	@bio: destination bio
817f9c78b2bSJens Axboe  *	@page: page to add
818f9c78b2bSJens Axboe  *	@len: vec entry length
819f9c78b2bSJens Axboe  *	@offset: vec entry offset
820f9c78b2bSJens Axboe  *
821f9c78b2bSJens Axboe  *	Attempt to add a page to the bio_vec maplist. This can fail for a
822f9c78b2bSJens Axboe  *	number of reasons, such as the bio being full or target block device
823f9c78b2bSJens Axboe  *	limitations. The target block device must allow bio's up to PAGE_SIZE,
824f9c78b2bSJens Axboe  *	so it is always possible to add a single page to an empty bio.
825f9c78b2bSJens Axboe  *
826f9c78b2bSJens Axboe  *	This should only be used by REQ_PC bios.
827f9c78b2bSJens Axboe  */
828f9c78b2bSJens Axboe int bio_add_pc_page(struct request_queue *q, struct bio *bio, struct page *page,
829f9c78b2bSJens Axboe 		    unsigned int len, unsigned int offset)
830f9c78b2bSJens Axboe {
831f9c78b2bSJens Axboe 	return __bio_add_page(q, bio, page, len, offset,
832f9c78b2bSJens Axboe 			      queue_max_hw_sectors(q));
833f9c78b2bSJens Axboe }
834f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_add_pc_page);
835f9c78b2bSJens Axboe 
836f9c78b2bSJens Axboe /**
837f9c78b2bSJens Axboe  *	bio_add_page	-	attempt to add page to bio
838f9c78b2bSJens Axboe  *	@bio: destination bio
839f9c78b2bSJens Axboe  *	@page: page to add
840f9c78b2bSJens Axboe  *	@len: vec entry length
841f9c78b2bSJens Axboe  *	@offset: vec entry offset
842f9c78b2bSJens Axboe  *
843f9c78b2bSJens Axboe  *	Attempt to add a page to the bio_vec maplist. This can fail for a
844f9c78b2bSJens Axboe  *	number of reasons, such as the bio being full or target block device
845f9c78b2bSJens Axboe  *	limitations. The target block device must allow bio's up to PAGE_SIZE,
846f9c78b2bSJens Axboe  *	so it is always possible to add a single page to an empty bio.
847f9c78b2bSJens Axboe  */
848f9c78b2bSJens Axboe int bio_add_page(struct bio *bio, struct page *page, unsigned int len,
849f9c78b2bSJens Axboe 		 unsigned int offset)
850f9c78b2bSJens Axboe {
851f9c78b2bSJens Axboe 	struct request_queue *q = bdev_get_queue(bio->bi_bdev);
852f9c78b2bSJens Axboe 	return __bio_add_page(q, bio, page, len, offset, queue_max_sectors(q));
853f9c78b2bSJens Axboe }
854f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_add_page);
855f9c78b2bSJens Axboe 
856f9c78b2bSJens Axboe struct submit_bio_ret {
857f9c78b2bSJens Axboe 	struct completion event;
858f9c78b2bSJens Axboe 	int error;
859f9c78b2bSJens Axboe };
860f9c78b2bSJens Axboe 
861f9c78b2bSJens Axboe static void submit_bio_wait_endio(struct bio *bio, int error)
862f9c78b2bSJens Axboe {
863f9c78b2bSJens Axboe 	struct submit_bio_ret *ret = bio->bi_private;
864f9c78b2bSJens Axboe 
865f9c78b2bSJens Axboe 	ret->error = error;
866f9c78b2bSJens Axboe 	complete(&ret->event);
867f9c78b2bSJens Axboe }
868f9c78b2bSJens Axboe 
869f9c78b2bSJens Axboe /**
870f9c78b2bSJens Axboe  * submit_bio_wait - submit a bio, and wait until it completes
871f9c78b2bSJens Axboe  * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
872f9c78b2bSJens Axboe  * @bio: The &struct bio which describes the I/O
873f9c78b2bSJens Axboe  *
874f9c78b2bSJens Axboe  * Simple wrapper around submit_bio(). Returns 0 on success, or the error from
875f9c78b2bSJens Axboe  * bio_endio() on failure.
876f9c78b2bSJens Axboe  */
877f9c78b2bSJens Axboe int submit_bio_wait(int rw, struct bio *bio)
878f9c78b2bSJens Axboe {
879f9c78b2bSJens Axboe 	struct submit_bio_ret ret;
880f9c78b2bSJens Axboe 
881f9c78b2bSJens Axboe 	rw |= REQ_SYNC;
882f9c78b2bSJens Axboe 	init_completion(&ret.event);
883f9c78b2bSJens Axboe 	bio->bi_private = &ret;
884f9c78b2bSJens Axboe 	bio->bi_end_io = submit_bio_wait_endio;
885f9c78b2bSJens Axboe 	submit_bio(rw, bio);
886f9c78b2bSJens Axboe 	wait_for_completion(&ret.event);
887f9c78b2bSJens Axboe 
888f9c78b2bSJens Axboe 	return ret.error;
889f9c78b2bSJens Axboe }
890f9c78b2bSJens Axboe EXPORT_SYMBOL(submit_bio_wait);
891f9c78b2bSJens Axboe 
892f9c78b2bSJens Axboe /**
893f9c78b2bSJens Axboe  * bio_advance - increment/complete a bio by some number of bytes
894f9c78b2bSJens Axboe  * @bio:	bio to advance
895f9c78b2bSJens Axboe  * @bytes:	number of bytes to complete
896f9c78b2bSJens Axboe  *
897f9c78b2bSJens Axboe  * This updates bi_sector, bi_size and bi_idx; if the number of bytes to
898f9c78b2bSJens Axboe  * complete doesn't align with a bvec boundary, then bv_len and bv_offset will
899f9c78b2bSJens Axboe  * be updated on the last bvec as well.
900f9c78b2bSJens Axboe  *
901f9c78b2bSJens Axboe  * @bio will then represent the remaining, uncompleted portion of the io.
902f9c78b2bSJens Axboe  */
903f9c78b2bSJens Axboe void bio_advance(struct bio *bio, unsigned bytes)
904f9c78b2bSJens Axboe {
905f9c78b2bSJens Axboe 	if (bio_integrity(bio))
906f9c78b2bSJens Axboe 		bio_integrity_advance(bio, bytes);
907f9c78b2bSJens Axboe 
908f9c78b2bSJens Axboe 	bio_advance_iter(bio, &bio->bi_iter, bytes);
909f9c78b2bSJens Axboe }
910f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_advance);
911f9c78b2bSJens Axboe 
912f9c78b2bSJens Axboe /**
913f9c78b2bSJens Axboe  * bio_alloc_pages - allocates a single page for each bvec in a bio
914f9c78b2bSJens Axboe  * @bio: bio to allocate pages for
915f9c78b2bSJens Axboe  * @gfp_mask: flags for allocation
916f9c78b2bSJens Axboe  *
917f9c78b2bSJens Axboe  * Allocates pages up to @bio->bi_vcnt.
918f9c78b2bSJens Axboe  *
919f9c78b2bSJens Axboe  * Returns 0 on success, -ENOMEM on failure. On failure, any allocated pages are
920f9c78b2bSJens Axboe  * freed.
921f9c78b2bSJens Axboe  */
922f9c78b2bSJens Axboe int bio_alloc_pages(struct bio *bio, gfp_t gfp_mask)
923f9c78b2bSJens Axboe {
924f9c78b2bSJens Axboe 	int i;
925f9c78b2bSJens Axboe 	struct bio_vec *bv;
926f9c78b2bSJens Axboe 
927f9c78b2bSJens Axboe 	bio_for_each_segment_all(bv, bio, i) {
928f9c78b2bSJens Axboe 		bv->bv_page = alloc_page(gfp_mask);
929f9c78b2bSJens Axboe 		if (!bv->bv_page) {
930f9c78b2bSJens Axboe 			while (--bv >= bio->bi_io_vec)
931f9c78b2bSJens Axboe 				__free_page(bv->bv_page);
932f9c78b2bSJens Axboe 			return -ENOMEM;
933f9c78b2bSJens Axboe 		}
934f9c78b2bSJens Axboe 	}
935f9c78b2bSJens Axboe 
936f9c78b2bSJens Axboe 	return 0;
937f9c78b2bSJens Axboe }
938f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_alloc_pages);
939f9c78b2bSJens Axboe 
940f9c78b2bSJens Axboe /**
941f9c78b2bSJens Axboe  * bio_copy_data - copy contents of data buffers from one chain of bios to
942f9c78b2bSJens Axboe  * another
943f9c78b2bSJens Axboe  * @src: source bio list
944f9c78b2bSJens Axboe  * @dst: destination bio list
945f9c78b2bSJens Axboe  *
946f9c78b2bSJens Axboe  * If @src and @dst are single bios, bi_next must be NULL - otherwise, treats
947f9c78b2bSJens Axboe  * @src and @dst as linked lists of bios.
948f9c78b2bSJens Axboe  *
949f9c78b2bSJens Axboe  * Stops when it reaches the end of either @src or @dst - that is, copies
950f9c78b2bSJens Axboe  * min(src->bi_size, dst->bi_size) bytes (or the equivalent for lists of bios).
951f9c78b2bSJens Axboe  */
952f9c78b2bSJens Axboe void bio_copy_data(struct bio *dst, struct bio *src)
953f9c78b2bSJens Axboe {
954f9c78b2bSJens Axboe 	struct bvec_iter src_iter, dst_iter;
955f9c78b2bSJens Axboe 	struct bio_vec src_bv, dst_bv;
956f9c78b2bSJens Axboe 	void *src_p, *dst_p;
957f9c78b2bSJens Axboe 	unsigned bytes;
958f9c78b2bSJens Axboe 
959f9c78b2bSJens Axboe 	src_iter = src->bi_iter;
960f9c78b2bSJens Axboe 	dst_iter = dst->bi_iter;
961f9c78b2bSJens Axboe 
962f9c78b2bSJens Axboe 	while (1) {
963f9c78b2bSJens Axboe 		if (!src_iter.bi_size) {
964f9c78b2bSJens Axboe 			src = src->bi_next;
965f9c78b2bSJens Axboe 			if (!src)
966f9c78b2bSJens Axboe 				break;
967f9c78b2bSJens Axboe 
968f9c78b2bSJens Axboe 			src_iter = src->bi_iter;
969f9c78b2bSJens Axboe 		}
970f9c78b2bSJens Axboe 
971f9c78b2bSJens Axboe 		if (!dst_iter.bi_size) {
972f9c78b2bSJens Axboe 			dst = dst->bi_next;
973f9c78b2bSJens Axboe 			if (!dst)
974f9c78b2bSJens Axboe 				break;
975f9c78b2bSJens Axboe 
976f9c78b2bSJens Axboe 			dst_iter = dst->bi_iter;
977f9c78b2bSJens Axboe 		}
978f9c78b2bSJens Axboe 
979f9c78b2bSJens Axboe 		src_bv = bio_iter_iovec(src, src_iter);
980f9c78b2bSJens Axboe 		dst_bv = bio_iter_iovec(dst, dst_iter);
981f9c78b2bSJens Axboe 
982f9c78b2bSJens Axboe 		bytes = min(src_bv.bv_len, dst_bv.bv_len);
983f9c78b2bSJens Axboe 
984f9c78b2bSJens Axboe 		src_p = kmap_atomic(src_bv.bv_page);
985f9c78b2bSJens Axboe 		dst_p = kmap_atomic(dst_bv.bv_page);
986f9c78b2bSJens Axboe 
987f9c78b2bSJens Axboe 		memcpy(dst_p + dst_bv.bv_offset,
988f9c78b2bSJens Axboe 		       src_p + src_bv.bv_offset,
989f9c78b2bSJens Axboe 		       bytes);
990f9c78b2bSJens Axboe 
991f9c78b2bSJens Axboe 		kunmap_atomic(dst_p);
992f9c78b2bSJens Axboe 		kunmap_atomic(src_p);
993f9c78b2bSJens Axboe 
994f9c78b2bSJens Axboe 		bio_advance_iter(src, &src_iter, bytes);
995f9c78b2bSJens Axboe 		bio_advance_iter(dst, &dst_iter, bytes);
996f9c78b2bSJens Axboe 	}
997f9c78b2bSJens Axboe }
998f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_copy_data);
999f9c78b2bSJens Axboe 
1000f9c78b2bSJens Axboe struct bio_map_data {
1001f9c78b2bSJens Axboe 	int nr_sgvecs;
1002f9c78b2bSJens Axboe 	int is_our_pages;
1003f9c78b2bSJens Axboe 	struct sg_iovec sgvecs[];
1004f9c78b2bSJens Axboe };
1005f9c78b2bSJens Axboe 
1006f9c78b2bSJens Axboe static void bio_set_map_data(struct bio_map_data *bmd, struct bio *bio,
1007f9c78b2bSJens Axboe 			     const struct sg_iovec *iov, int iov_count,
1008f9c78b2bSJens Axboe 			     int is_our_pages)
1009f9c78b2bSJens Axboe {
1010f9c78b2bSJens Axboe 	memcpy(bmd->sgvecs, iov, sizeof(struct sg_iovec) * iov_count);
1011f9c78b2bSJens Axboe 	bmd->nr_sgvecs = iov_count;
1012f9c78b2bSJens Axboe 	bmd->is_our_pages = is_our_pages;
1013f9c78b2bSJens Axboe 	bio->bi_private = bmd;
1014f9c78b2bSJens Axboe }
1015f9c78b2bSJens Axboe 
1016f9c78b2bSJens Axboe static struct bio_map_data *bio_alloc_map_data(unsigned int iov_count,
1017f9c78b2bSJens Axboe 					       gfp_t gfp_mask)
1018f9c78b2bSJens Axboe {
1019f9c78b2bSJens Axboe 	if (iov_count > UIO_MAXIOV)
1020f9c78b2bSJens Axboe 		return NULL;
1021f9c78b2bSJens Axboe 
1022f9c78b2bSJens Axboe 	return kmalloc(sizeof(struct bio_map_data) +
1023f9c78b2bSJens Axboe 		       sizeof(struct sg_iovec) * iov_count, gfp_mask);
1024f9c78b2bSJens Axboe }
1025f9c78b2bSJens Axboe 
1026f9c78b2bSJens Axboe static int __bio_copy_iov(struct bio *bio, const struct sg_iovec *iov, int iov_count,
1027f9c78b2bSJens Axboe 			  int to_user, int from_user, int do_free_page)
1028f9c78b2bSJens Axboe {
1029f9c78b2bSJens Axboe 	int ret = 0, i;
1030f9c78b2bSJens Axboe 	struct bio_vec *bvec;
1031f9c78b2bSJens Axboe 	int iov_idx = 0;
1032f9c78b2bSJens Axboe 	unsigned int iov_off = 0;
1033f9c78b2bSJens Axboe 
1034f9c78b2bSJens Axboe 	bio_for_each_segment_all(bvec, bio, i) {
1035f9c78b2bSJens Axboe 		char *bv_addr = page_address(bvec->bv_page);
1036f9c78b2bSJens Axboe 		unsigned int bv_len = bvec->bv_len;
1037f9c78b2bSJens Axboe 
1038f9c78b2bSJens Axboe 		while (bv_len && iov_idx < iov_count) {
1039f9c78b2bSJens Axboe 			unsigned int bytes;
1040f9c78b2bSJens Axboe 			char __user *iov_addr;
1041f9c78b2bSJens Axboe 
1042f9c78b2bSJens Axboe 			bytes = min_t(unsigned int,
1043f9c78b2bSJens Axboe 				      iov[iov_idx].iov_len - iov_off, bv_len);
1044f9c78b2bSJens Axboe 			iov_addr = iov[iov_idx].iov_base + iov_off;
1045f9c78b2bSJens Axboe 
1046f9c78b2bSJens Axboe 			if (!ret) {
1047f9c78b2bSJens Axboe 				if (to_user)
1048f9c78b2bSJens Axboe 					ret = copy_to_user(iov_addr, bv_addr,
1049f9c78b2bSJens Axboe 							   bytes);
1050f9c78b2bSJens Axboe 
1051f9c78b2bSJens Axboe 				if (from_user)
1052f9c78b2bSJens Axboe 					ret = copy_from_user(bv_addr, iov_addr,
1053f9c78b2bSJens Axboe 							     bytes);
1054f9c78b2bSJens Axboe 
1055f9c78b2bSJens Axboe 				if (ret)
1056f9c78b2bSJens Axboe 					ret = -EFAULT;
1057f9c78b2bSJens Axboe 			}
1058f9c78b2bSJens Axboe 
1059f9c78b2bSJens Axboe 			bv_len -= bytes;
1060f9c78b2bSJens Axboe 			bv_addr += bytes;
1061f9c78b2bSJens Axboe 			iov_addr += bytes;
1062f9c78b2bSJens Axboe 			iov_off += bytes;
1063f9c78b2bSJens Axboe 
1064f9c78b2bSJens Axboe 			if (iov[iov_idx].iov_len == iov_off) {
1065f9c78b2bSJens Axboe 				iov_idx++;
1066f9c78b2bSJens Axboe 				iov_off = 0;
1067f9c78b2bSJens Axboe 			}
1068f9c78b2bSJens Axboe 		}
1069f9c78b2bSJens Axboe 
1070f9c78b2bSJens Axboe 		if (do_free_page)
1071f9c78b2bSJens Axboe 			__free_page(bvec->bv_page);
1072f9c78b2bSJens Axboe 	}
1073f9c78b2bSJens Axboe 
1074f9c78b2bSJens Axboe 	return ret;
1075f9c78b2bSJens Axboe }
1076f9c78b2bSJens Axboe 
1077f9c78b2bSJens Axboe /**
1078f9c78b2bSJens Axboe  *	bio_uncopy_user	-	finish previously mapped bio
1079f9c78b2bSJens Axboe  *	@bio: bio being terminated
1080f9c78b2bSJens Axboe  *
1081f9c78b2bSJens Axboe  *	Free pages allocated from bio_copy_user() and write back data
1082f9c78b2bSJens Axboe  *	to user space in case of a read.
1083f9c78b2bSJens Axboe  */
1084f9c78b2bSJens Axboe int bio_uncopy_user(struct bio *bio)
1085f9c78b2bSJens Axboe {
1086f9c78b2bSJens Axboe 	struct bio_map_data *bmd = bio->bi_private;
1087f9c78b2bSJens Axboe 	struct bio_vec *bvec;
1088f9c78b2bSJens Axboe 	int ret = 0, i;
1089f9c78b2bSJens Axboe 
1090f9c78b2bSJens Axboe 	if (!bio_flagged(bio, BIO_NULL_MAPPED)) {
1091f9c78b2bSJens Axboe 		/*
1092f9c78b2bSJens Axboe 		 * if we're in a workqueue, the request is orphaned, so
1093f9c78b2bSJens Axboe 		 * don't copy into a random user address space, just free.
1094f9c78b2bSJens Axboe 		 */
1095f9c78b2bSJens Axboe 		if (current->mm)
1096f9c78b2bSJens Axboe 			ret = __bio_copy_iov(bio, bmd->sgvecs, bmd->nr_sgvecs,
1097f9c78b2bSJens Axboe 					     bio_data_dir(bio) == READ,
1098f9c78b2bSJens Axboe 					     0, bmd->is_our_pages);
1099f9c78b2bSJens Axboe 		else if (bmd->is_our_pages)
1100f9c78b2bSJens Axboe 			bio_for_each_segment_all(bvec, bio, i)
1101f9c78b2bSJens Axboe 				__free_page(bvec->bv_page);
1102f9c78b2bSJens Axboe 	}
1103f9c78b2bSJens Axboe 	kfree(bmd);
1104f9c78b2bSJens Axboe 	bio_put(bio);
1105f9c78b2bSJens Axboe 	return ret;
1106f9c78b2bSJens Axboe }
1107f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_uncopy_user);
1108f9c78b2bSJens Axboe 
1109f9c78b2bSJens Axboe /**
1110f9c78b2bSJens Axboe  *	bio_copy_user_iov	-	copy user data to bio
1111f9c78b2bSJens Axboe  *	@q: destination block queue
1112f9c78b2bSJens Axboe  *	@map_data: pointer to the rq_map_data holding pages (if necessary)
1113f9c78b2bSJens Axboe  *	@iov:	the iovec.
1114f9c78b2bSJens Axboe  *	@iov_count: number of elements in the iovec
1115f9c78b2bSJens Axboe  *	@write_to_vm: bool indicating writing to pages or not
1116f9c78b2bSJens Axboe  *	@gfp_mask: memory allocation flags
1117f9c78b2bSJens Axboe  *
1118f9c78b2bSJens Axboe  *	Prepares and returns a bio for indirect user io, bouncing data
1119f9c78b2bSJens Axboe  *	to/from kernel pages as necessary. Must be paired with
1120f9c78b2bSJens Axboe  *	call bio_uncopy_user() on io completion.
1121f9c78b2bSJens Axboe  */
1122f9c78b2bSJens Axboe struct bio *bio_copy_user_iov(struct request_queue *q,
1123f9c78b2bSJens Axboe 			      struct rq_map_data *map_data,
1124f9c78b2bSJens Axboe 			      const struct sg_iovec *iov, int iov_count,
1125f9c78b2bSJens Axboe 			      int write_to_vm, gfp_t gfp_mask)
1126f9c78b2bSJens Axboe {
1127f9c78b2bSJens Axboe 	struct bio_map_data *bmd;
1128f9c78b2bSJens Axboe 	struct bio_vec *bvec;
1129f9c78b2bSJens Axboe 	struct page *page;
1130f9c78b2bSJens Axboe 	struct bio *bio;
1131f9c78b2bSJens Axboe 	int i, ret;
1132f9c78b2bSJens Axboe 	int nr_pages = 0;
1133f9c78b2bSJens Axboe 	unsigned int len = 0;
1134f9c78b2bSJens Axboe 	unsigned int offset = map_data ? map_data->offset & ~PAGE_MASK : 0;
1135f9c78b2bSJens Axboe 
1136f9c78b2bSJens Axboe 	for (i = 0; i < iov_count; i++) {
1137f9c78b2bSJens Axboe 		unsigned long uaddr;
1138f9c78b2bSJens Axboe 		unsigned long end;
1139f9c78b2bSJens Axboe 		unsigned long start;
1140f9c78b2bSJens Axboe 
1141f9c78b2bSJens Axboe 		uaddr = (unsigned long)iov[i].iov_base;
1142f9c78b2bSJens Axboe 		end = (uaddr + iov[i].iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1143f9c78b2bSJens Axboe 		start = uaddr >> PAGE_SHIFT;
1144f9c78b2bSJens Axboe 
1145f9c78b2bSJens Axboe 		/*
1146f9c78b2bSJens Axboe 		 * Overflow, abort
1147f9c78b2bSJens Axboe 		 */
1148f9c78b2bSJens Axboe 		if (end < start)
1149f9c78b2bSJens Axboe 			return ERR_PTR(-EINVAL);
1150f9c78b2bSJens Axboe 
1151f9c78b2bSJens Axboe 		nr_pages += end - start;
1152f9c78b2bSJens Axboe 		len += iov[i].iov_len;
1153f9c78b2bSJens Axboe 	}
1154f9c78b2bSJens Axboe 
1155f9c78b2bSJens Axboe 	if (offset)
1156f9c78b2bSJens Axboe 		nr_pages++;
1157f9c78b2bSJens Axboe 
1158f9c78b2bSJens Axboe 	bmd = bio_alloc_map_data(iov_count, gfp_mask);
1159f9c78b2bSJens Axboe 	if (!bmd)
1160f9c78b2bSJens Axboe 		return ERR_PTR(-ENOMEM);
1161f9c78b2bSJens Axboe 
1162f9c78b2bSJens Axboe 	ret = -ENOMEM;
1163f9c78b2bSJens Axboe 	bio = bio_kmalloc(gfp_mask, nr_pages);
1164f9c78b2bSJens Axboe 	if (!bio)
1165f9c78b2bSJens Axboe 		goto out_bmd;
1166f9c78b2bSJens Axboe 
1167f9c78b2bSJens Axboe 	if (!write_to_vm)
1168f9c78b2bSJens Axboe 		bio->bi_rw |= REQ_WRITE;
1169f9c78b2bSJens Axboe 
1170f9c78b2bSJens Axboe 	ret = 0;
1171f9c78b2bSJens Axboe 
1172f9c78b2bSJens Axboe 	if (map_data) {
1173f9c78b2bSJens Axboe 		nr_pages = 1 << map_data->page_order;
1174f9c78b2bSJens Axboe 		i = map_data->offset / PAGE_SIZE;
1175f9c78b2bSJens Axboe 	}
1176f9c78b2bSJens Axboe 	while (len) {
1177f9c78b2bSJens Axboe 		unsigned int bytes = PAGE_SIZE;
1178f9c78b2bSJens Axboe 
1179f9c78b2bSJens Axboe 		bytes -= offset;
1180f9c78b2bSJens Axboe 
1181f9c78b2bSJens Axboe 		if (bytes > len)
1182f9c78b2bSJens Axboe 			bytes = len;
1183f9c78b2bSJens Axboe 
1184f9c78b2bSJens Axboe 		if (map_data) {
1185f9c78b2bSJens Axboe 			if (i == map_data->nr_entries * nr_pages) {
1186f9c78b2bSJens Axboe 				ret = -ENOMEM;
1187f9c78b2bSJens Axboe 				break;
1188f9c78b2bSJens Axboe 			}
1189f9c78b2bSJens Axboe 
1190f9c78b2bSJens Axboe 			page = map_data->pages[i / nr_pages];
1191f9c78b2bSJens Axboe 			page += (i % nr_pages);
1192f9c78b2bSJens Axboe 
1193f9c78b2bSJens Axboe 			i++;
1194f9c78b2bSJens Axboe 		} else {
1195f9c78b2bSJens Axboe 			page = alloc_page(q->bounce_gfp | gfp_mask);
1196f9c78b2bSJens Axboe 			if (!page) {
1197f9c78b2bSJens Axboe 				ret = -ENOMEM;
1198f9c78b2bSJens Axboe 				break;
1199f9c78b2bSJens Axboe 			}
1200f9c78b2bSJens Axboe 		}
1201f9c78b2bSJens Axboe 
1202f9c78b2bSJens Axboe 		if (bio_add_pc_page(q, bio, page, bytes, offset) < bytes)
1203f9c78b2bSJens Axboe 			break;
1204f9c78b2bSJens Axboe 
1205f9c78b2bSJens Axboe 		len -= bytes;
1206f9c78b2bSJens Axboe 		offset = 0;
1207f9c78b2bSJens Axboe 	}
1208f9c78b2bSJens Axboe 
1209f9c78b2bSJens Axboe 	if (ret)
1210f9c78b2bSJens Axboe 		goto cleanup;
1211f9c78b2bSJens Axboe 
1212f9c78b2bSJens Axboe 	/*
1213f9c78b2bSJens Axboe 	 * success
1214f9c78b2bSJens Axboe 	 */
1215f9c78b2bSJens Axboe 	if ((!write_to_vm && (!map_data || !map_data->null_mapped)) ||
1216f9c78b2bSJens Axboe 	    (map_data && map_data->from_user)) {
1217f9c78b2bSJens Axboe 		ret = __bio_copy_iov(bio, iov, iov_count, 0, 1, 0);
1218f9c78b2bSJens Axboe 		if (ret)
1219f9c78b2bSJens Axboe 			goto cleanup;
1220f9c78b2bSJens Axboe 	}
1221f9c78b2bSJens Axboe 
1222f9c78b2bSJens Axboe 	bio_set_map_data(bmd, bio, iov, iov_count, map_data ? 0 : 1);
1223f9c78b2bSJens Axboe 	return bio;
1224f9c78b2bSJens Axboe cleanup:
1225f9c78b2bSJens Axboe 	if (!map_data)
1226f9c78b2bSJens Axboe 		bio_for_each_segment_all(bvec, bio, i)
1227f9c78b2bSJens Axboe 			__free_page(bvec->bv_page);
1228f9c78b2bSJens Axboe 
1229f9c78b2bSJens Axboe 	bio_put(bio);
1230f9c78b2bSJens Axboe out_bmd:
1231f9c78b2bSJens Axboe 	kfree(bmd);
1232f9c78b2bSJens Axboe 	return ERR_PTR(ret);
1233f9c78b2bSJens Axboe }
1234f9c78b2bSJens Axboe 
1235f9c78b2bSJens Axboe /**
1236f9c78b2bSJens Axboe  *	bio_copy_user	-	copy user data to bio
1237f9c78b2bSJens Axboe  *	@q: destination block queue
1238f9c78b2bSJens Axboe  *	@map_data: pointer to the rq_map_data holding pages (if necessary)
1239f9c78b2bSJens Axboe  *	@uaddr: start of user address
1240f9c78b2bSJens Axboe  *	@len: length in bytes
1241f9c78b2bSJens Axboe  *	@write_to_vm: bool indicating writing to pages or not
1242f9c78b2bSJens Axboe  *	@gfp_mask: memory allocation flags
1243f9c78b2bSJens Axboe  *
1244f9c78b2bSJens Axboe  *	Prepares and returns a bio for indirect user io, bouncing data
1245f9c78b2bSJens Axboe  *	to/from kernel pages as necessary. Must be paired with
1246f9c78b2bSJens Axboe  *	call bio_uncopy_user() on io completion.
1247f9c78b2bSJens Axboe  */
1248f9c78b2bSJens Axboe struct bio *bio_copy_user(struct request_queue *q, struct rq_map_data *map_data,
1249f9c78b2bSJens Axboe 			  unsigned long uaddr, unsigned int len,
1250f9c78b2bSJens Axboe 			  int write_to_vm, gfp_t gfp_mask)
1251f9c78b2bSJens Axboe {
1252f9c78b2bSJens Axboe 	struct sg_iovec iov;
1253f9c78b2bSJens Axboe 
1254f9c78b2bSJens Axboe 	iov.iov_base = (void __user *)uaddr;
1255f9c78b2bSJens Axboe 	iov.iov_len = len;
1256f9c78b2bSJens Axboe 
1257f9c78b2bSJens Axboe 	return bio_copy_user_iov(q, map_data, &iov, 1, write_to_vm, gfp_mask);
1258f9c78b2bSJens Axboe }
1259f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_copy_user);
1260f9c78b2bSJens Axboe 
1261f9c78b2bSJens Axboe static struct bio *__bio_map_user_iov(struct request_queue *q,
1262f9c78b2bSJens Axboe 				      struct block_device *bdev,
1263f9c78b2bSJens Axboe 				      const struct sg_iovec *iov, int iov_count,
1264f9c78b2bSJens Axboe 				      int write_to_vm, gfp_t gfp_mask)
1265f9c78b2bSJens Axboe {
1266f9c78b2bSJens Axboe 	int i, j;
1267f9c78b2bSJens Axboe 	int nr_pages = 0;
1268f9c78b2bSJens Axboe 	struct page **pages;
1269f9c78b2bSJens Axboe 	struct bio *bio;
1270f9c78b2bSJens Axboe 	int cur_page = 0;
1271f9c78b2bSJens Axboe 	int ret, offset;
1272f9c78b2bSJens Axboe 
1273f9c78b2bSJens Axboe 	for (i = 0; i < iov_count; i++) {
1274f9c78b2bSJens Axboe 		unsigned long uaddr = (unsigned long)iov[i].iov_base;
1275f9c78b2bSJens Axboe 		unsigned long len = iov[i].iov_len;
1276f9c78b2bSJens Axboe 		unsigned long end = (uaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1277f9c78b2bSJens Axboe 		unsigned long start = uaddr >> PAGE_SHIFT;
1278f9c78b2bSJens Axboe 
1279f9c78b2bSJens Axboe 		/*
1280f9c78b2bSJens Axboe 		 * Overflow, abort
1281f9c78b2bSJens Axboe 		 */
1282f9c78b2bSJens Axboe 		if (end < start)
1283f9c78b2bSJens Axboe 			return ERR_PTR(-EINVAL);
1284f9c78b2bSJens Axboe 
1285f9c78b2bSJens Axboe 		nr_pages += end - start;
1286f9c78b2bSJens Axboe 		/*
1287f9c78b2bSJens Axboe 		 * buffer must be aligned to at least hardsector size for now
1288f9c78b2bSJens Axboe 		 */
1289f9c78b2bSJens Axboe 		if (uaddr & queue_dma_alignment(q))
1290f9c78b2bSJens Axboe 			return ERR_PTR(-EINVAL);
1291f9c78b2bSJens Axboe 	}
1292f9c78b2bSJens Axboe 
1293f9c78b2bSJens Axboe 	if (!nr_pages)
1294f9c78b2bSJens Axboe 		return ERR_PTR(-EINVAL);
1295f9c78b2bSJens Axboe 
1296f9c78b2bSJens Axboe 	bio = bio_kmalloc(gfp_mask, nr_pages);
1297f9c78b2bSJens Axboe 	if (!bio)
1298f9c78b2bSJens Axboe 		return ERR_PTR(-ENOMEM);
1299f9c78b2bSJens Axboe 
1300f9c78b2bSJens Axboe 	ret = -ENOMEM;
1301f9c78b2bSJens Axboe 	pages = kcalloc(nr_pages, sizeof(struct page *), gfp_mask);
1302f9c78b2bSJens Axboe 	if (!pages)
1303f9c78b2bSJens Axboe 		goto out;
1304f9c78b2bSJens Axboe 
1305f9c78b2bSJens Axboe 	for (i = 0; i < iov_count; i++) {
1306f9c78b2bSJens Axboe 		unsigned long uaddr = (unsigned long)iov[i].iov_base;
1307f9c78b2bSJens Axboe 		unsigned long len = iov[i].iov_len;
1308f9c78b2bSJens Axboe 		unsigned long end = (uaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1309f9c78b2bSJens Axboe 		unsigned long start = uaddr >> PAGE_SHIFT;
1310f9c78b2bSJens Axboe 		const int local_nr_pages = end - start;
1311f9c78b2bSJens Axboe 		const int page_limit = cur_page + local_nr_pages;
1312f9c78b2bSJens Axboe 
1313f9c78b2bSJens Axboe 		ret = get_user_pages_fast(uaddr, local_nr_pages,
1314f9c78b2bSJens Axboe 				write_to_vm, &pages[cur_page]);
1315f9c78b2bSJens Axboe 		if (ret < local_nr_pages) {
1316f9c78b2bSJens Axboe 			ret = -EFAULT;
1317f9c78b2bSJens Axboe 			goto out_unmap;
1318f9c78b2bSJens Axboe 		}
1319f9c78b2bSJens Axboe 
1320f9c78b2bSJens Axboe 		offset = uaddr & ~PAGE_MASK;
1321f9c78b2bSJens Axboe 		for (j = cur_page; j < page_limit; j++) {
1322f9c78b2bSJens Axboe 			unsigned int bytes = PAGE_SIZE - offset;
1323f9c78b2bSJens Axboe 
1324f9c78b2bSJens Axboe 			if (len <= 0)
1325f9c78b2bSJens Axboe 				break;
1326f9c78b2bSJens Axboe 
1327f9c78b2bSJens Axboe 			if (bytes > len)
1328f9c78b2bSJens Axboe 				bytes = len;
1329f9c78b2bSJens Axboe 
1330f9c78b2bSJens Axboe 			/*
1331f9c78b2bSJens Axboe 			 * sorry...
1332f9c78b2bSJens Axboe 			 */
1333f9c78b2bSJens Axboe 			if (bio_add_pc_page(q, bio, pages[j], bytes, offset) <
1334f9c78b2bSJens Axboe 					    bytes)
1335f9c78b2bSJens Axboe 				break;
1336f9c78b2bSJens Axboe 
1337f9c78b2bSJens Axboe 			len -= bytes;
1338f9c78b2bSJens Axboe 			offset = 0;
1339f9c78b2bSJens Axboe 		}
1340f9c78b2bSJens Axboe 
1341f9c78b2bSJens Axboe 		cur_page = j;
1342f9c78b2bSJens Axboe 		/*
1343f9c78b2bSJens Axboe 		 * release the pages we didn't map into the bio, if any
1344f9c78b2bSJens Axboe 		 */
1345f9c78b2bSJens Axboe 		while (j < page_limit)
1346f9c78b2bSJens Axboe 			page_cache_release(pages[j++]);
1347f9c78b2bSJens Axboe 	}
1348f9c78b2bSJens Axboe 
1349f9c78b2bSJens Axboe 	kfree(pages);
1350f9c78b2bSJens Axboe 
1351f9c78b2bSJens Axboe 	/*
1352f9c78b2bSJens Axboe 	 * set data direction, and check if mapped pages need bouncing
1353f9c78b2bSJens Axboe 	 */
1354f9c78b2bSJens Axboe 	if (!write_to_vm)
1355f9c78b2bSJens Axboe 		bio->bi_rw |= REQ_WRITE;
1356f9c78b2bSJens Axboe 
1357f9c78b2bSJens Axboe 	bio->bi_bdev = bdev;
1358f9c78b2bSJens Axboe 	bio->bi_flags |= (1 << BIO_USER_MAPPED);
1359f9c78b2bSJens Axboe 	return bio;
1360f9c78b2bSJens Axboe 
1361f9c78b2bSJens Axboe  out_unmap:
1362f9c78b2bSJens Axboe 	for (i = 0; i < nr_pages; i++) {
1363f9c78b2bSJens Axboe 		if(!pages[i])
1364f9c78b2bSJens Axboe 			break;
1365f9c78b2bSJens Axboe 		page_cache_release(pages[i]);
1366f9c78b2bSJens Axboe 	}
1367f9c78b2bSJens Axboe  out:
1368f9c78b2bSJens Axboe 	kfree(pages);
1369f9c78b2bSJens Axboe 	bio_put(bio);
1370f9c78b2bSJens Axboe 	return ERR_PTR(ret);
1371f9c78b2bSJens Axboe }
1372f9c78b2bSJens Axboe 
1373f9c78b2bSJens Axboe /**
1374f9c78b2bSJens Axboe  *	bio_map_user	-	map user address into bio
1375f9c78b2bSJens Axboe  *	@q: the struct request_queue for the bio
1376f9c78b2bSJens Axboe  *	@bdev: destination block device
1377f9c78b2bSJens Axboe  *	@uaddr: start of user address
1378f9c78b2bSJens Axboe  *	@len: length in bytes
1379f9c78b2bSJens Axboe  *	@write_to_vm: bool indicating writing to pages or not
1380f9c78b2bSJens Axboe  *	@gfp_mask: memory allocation flags
1381f9c78b2bSJens Axboe  *
1382f9c78b2bSJens Axboe  *	Map the user space address into a bio suitable for io to a block
1383f9c78b2bSJens Axboe  *	device. Returns an error pointer in case of error.
1384f9c78b2bSJens Axboe  */
1385f9c78b2bSJens Axboe struct bio *bio_map_user(struct request_queue *q, struct block_device *bdev,
1386f9c78b2bSJens Axboe 			 unsigned long uaddr, unsigned int len, int write_to_vm,
1387f9c78b2bSJens Axboe 			 gfp_t gfp_mask)
1388f9c78b2bSJens Axboe {
1389f9c78b2bSJens Axboe 	struct sg_iovec iov;
1390f9c78b2bSJens Axboe 
1391f9c78b2bSJens Axboe 	iov.iov_base = (void __user *)uaddr;
1392f9c78b2bSJens Axboe 	iov.iov_len = len;
1393f9c78b2bSJens Axboe 
1394f9c78b2bSJens Axboe 	return bio_map_user_iov(q, bdev, &iov, 1, write_to_vm, gfp_mask);
1395f9c78b2bSJens Axboe }
1396f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_map_user);
1397f9c78b2bSJens Axboe 
1398f9c78b2bSJens Axboe /**
1399f9c78b2bSJens Axboe  *	bio_map_user_iov - map user sg_iovec table into bio
1400f9c78b2bSJens Axboe  *	@q: the struct request_queue for the bio
1401f9c78b2bSJens Axboe  *	@bdev: destination block device
1402f9c78b2bSJens Axboe  *	@iov:	the iovec.
1403f9c78b2bSJens Axboe  *	@iov_count: number of elements in the iovec
1404f9c78b2bSJens Axboe  *	@write_to_vm: bool indicating writing to pages or not
1405f9c78b2bSJens Axboe  *	@gfp_mask: memory allocation flags
1406f9c78b2bSJens Axboe  *
1407f9c78b2bSJens Axboe  *	Map the user space address into a bio suitable for io to a block
1408f9c78b2bSJens Axboe  *	device. Returns an error pointer in case of error.
1409f9c78b2bSJens Axboe  */
1410f9c78b2bSJens Axboe struct bio *bio_map_user_iov(struct request_queue *q, struct block_device *bdev,
1411f9c78b2bSJens Axboe 			     const struct sg_iovec *iov, int iov_count,
1412f9c78b2bSJens Axboe 			     int write_to_vm, gfp_t gfp_mask)
1413f9c78b2bSJens Axboe {
1414f9c78b2bSJens Axboe 	struct bio *bio;
1415f9c78b2bSJens Axboe 
1416f9c78b2bSJens Axboe 	bio = __bio_map_user_iov(q, bdev, iov, iov_count, write_to_vm,
1417f9c78b2bSJens Axboe 				 gfp_mask);
1418f9c78b2bSJens Axboe 	if (IS_ERR(bio))
1419f9c78b2bSJens Axboe 		return bio;
1420f9c78b2bSJens Axboe 
1421f9c78b2bSJens Axboe 	/*
1422f9c78b2bSJens Axboe 	 * subtle -- if __bio_map_user() ended up bouncing a bio,
1423f9c78b2bSJens Axboe 	 * it would normally disappear when its bi_end_io is run.
1424f9c78b2bSJens Axboe 	 * however, we need it for the unmap, so grab an extra
1425f9c78b2bSJens Axboe 	 * reference to it
1426f9c78b2bSJens Axboe 	 */
1427f9c78b2bSJens Axboe 	bio_get(bio);
1428f9c78b2bSJens Axboe 
1429f9c78b2bSJens Axboe 	return bio;
1430f9c78b2bSJens Axboe }
1431f9c78b2bSJens Axboe 
1432f9c78b2bSJens Axboe static void __bio_unmap_user(struct bio *bio)
1433f9c78b2bSJens Axboe {
1434f9c78b2bSJens Axboe 	struct bio_vec *bvec;
1435f9c78b2bSJens Axboe 	int i;
1436f9c78b2bSJens Axboe 
1437f9c78b2bSJens Axboe 	/*
1438f9c78b2bSJens Axboe 	 * make sure we dirty pages we wrote to
1439f9c78b2bSJens Axboe 	 */
1440f9c78b2bSJens Axboe 	bio_for_each_segment_all(bvec, bio, i) {
1441f9c78b2bSJens Axboe 		if (bio_data_dir(bio) == READ)
1442f9c78b2bSJens Axboe 			set_page_dirty_lock(bvec->bv_page);
1443f9c78b2bSJens Axboe 
1444f9c78b2bSJens Axboe 		page_cache_release(bvec->bv_page);
1445f9c78b2bSJens Axboe 	}
1446f9c78b2bSJens Axboe 
1447f9c78b2bSJens Axboe 	bio_put(bio);
1448f9c78b2bSJens Axboe }
1449f9c78b2bSJens Axboe 
1450f9c78b2bSJens Axboe /**
1451f9c78b2bSJens Axboe  *	bio_unmap_user	-	unmap a bio
1452f9c78b2bSJens Axboe  *	@bio:		the bio being unmapped
1453f9c78b2bSJens Axboe  *
1454f9c78b2bSJens Axboe  *	Unmap a bio previously mapped by bio_map_user(). Must be called with
1455f9c78b2bSJens Axboe  *	a process context.
1456f9c78b2bSJens Axboe  *
1457f9c78b2bSJens Axboe  *	bio_unmap_user() may sleep.
1458f9c78b2bSJens Axboe  */
1459f9c78b2bSJens Axboe void bio_unmap_user(struct bio *bio)
1460f9c78b2bSJens Axboe {
1461f9c78b2bSJens Axboe 	__bio_unmap_user(bio);
1462f9c78b2bSJens Axboe 	bio_put(bio);
1463f9c78b2bSJens Axboe }
1464f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_unmap_user);
1465f9c78b2bSJens Axboe 
1466f9c78b2bSJens Axboe static void bio_map_kern_endio(struct bio *bio, int err)
1467f9c78b2bSJens Axboe {
1468f9c78b2bSJens Axboe 	bio_put(bio);
1469f9c78b2bSJens Axboe }
1470f9c78b2bSJens Axboe 
1471f9c78b2bSJens Axboe static struct bio *__bio_map_kern(struct request_queue *q, void *data,
1472f9c78b2bSJens Axboe 				  unsigned int len, gfp_t gfp_mask)
1473f9c78b2bSJens Axboe {
1474f9c78b2bSJens Axboe 	unsigned long kaddr = (unsigned long)data;
1475f9c78b2bSJens Axboe 	unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1476f9c78b2bSJens Axboe 	unsigned long start = kaddr >> PAGE_SHIFT;
1477f9c78b2bSJens Axboe 	const int nr_pages = end - start;
1478f9c78b2bSJens Axboe 	int offset, i;
1479f9c78b2bSJens Axboe 	struct bio *bio;
1480f9c78b2bSJens Axboe 
1481f9c78b2bSJens Axboe 	bio = bio_kmalloc(gfp_mask, nr_pages);
1482f9c78b2bSJens Axboe 	if (!bio)
1483f9c78b2bSJens Axboe 		return ERR_PTR(-ENOMEM);
1484f9c78b2bSJens Axboe 
1485f9c78b2bSJens Axboe 	offset = offset_in_page(kaddr);
1486f9c78b2bSJens Axboe 	for (i = 0; i < nr_pages; i++) {
1487f9c78b2bSJens Axboe 		unsigned int bytes = PAGE_SIZE - offset;
1488f9c78b2bSJens Axboe 
1489f9c78b2bSJens Axboe 		if (len <= 0)
1490f9c78b2bSJens Axboe 			break;
1491f9c78b2bSJens Axboe 
1492f9c78b2bSJens Axboe 		if (bytes > len)
1493f9c78b2bSJens Axboe 			bytes = len;
1494f9c78b2bSJens Axboe 
1495f9c78b2bSJens Axboe 		if (bio_add_pc_page(q, bio, virt_to_page(data), bytes,
1496f9c78b2bSJens Axboe 				    offset) < bytes)
1497f9c78b2bSJens Axboe 			break;
1498f9c78b2bSJens Axboe 
1499f9c78b2bSJens Axboe 		data += bytes;
1500f9c78b2bSJens Axboe 		len -= bytes;
1501f9c78b2bSJens Axboe 		offset = 0;
1502f9c78b2bSJens Axboe 	}
1503f9c78b2bSJens Axboe 
1504f9c78b2bSJens Axboe 	bio->bi_end_io = bio_map_kern_endio;
1505f9c78b2bSJens Axboe 	return bio;
1506f9c78b2bSJens Axboe }
1507f9c78b2bSJens Axboe 
1508f9c78b2bSJens Axboe /**
1509f9c78b2bSJens Axboe  *	bio_map_kern	-	map kernel address into bio
1510f9c78b2bSJens Axboe  *	@q: the struct request_queue for the bio
1511f9c78b2bSJens Axboe  *	@data: pointer to buffer to map
1512f9c78b2bSJens Axboe  *	@len: length in bytes
1513f9c78b2bSJens Axboe  *	@gfp_mask: allocation flags for bio allocation
1514f9c78b2bSJens Axboe  *
1515f9c78b2bSJens Axboe  *	Map the kernel address into a bio suitable for io to a block
1516f9c78b2bSJens Axboe  *	device. Returns an error pointer in case of error.
1517f9c78b2bSJens Axboe  */
1518f9c78b2bSJens Axboe struct bio *bio_map_kern(struct request_queue *q, void *data, unsigned int len,
1519f9c78b2bSJens Axboe 			 gfp_t gfp_mask)
1520f9c78b2bSJens Axboe {
1521f9c78b2bSJens Axboe 	struct bio *bio;
1522f9c78b2bSJens Axboe 
1523f9c78b2bSJens Axboe 	bio = __bio_map_kern(q, data, len, gfp_mask);
1524f9c78b2bSJens Axboe 	if (IS_ERR(bio))
1525f9c78b2bSJens Axboe 		return bio;
1526f9c78b2bSJens Axboe 
1527f9c78b2bSJens Axboe 	if (bio->bi_iter.bi_size == len)
1528f9c78b2bSJens Axboe 		return bio;
1529f9c78b2bSJens Axboe 
1530f9c78b2bSJens Axboe 	/*
1531f9c78b2bSJens Axboe 	 * Don't support partial mappings.
1532f9c78b2bSJens Axboe 	 */
1533f9c78b2bSJens Axboe 	bio_put(bio);
1534f9c78b2bSJens Axboe 	return ERR_PTR(-EINVAL);
1535f9c78b2bSJens Axboe }
1536f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_map_kern);
1537f9c78b2bSJens Axboe 
1538f9c78b2bSJens Axboe static void bio_copy_kern_endio(struct bio *bio, int err)
1539f9c78b2bSJens Axboe {
1540f9c78b2bSJens Axboe 	struct bio_vec *bvec;
1541f9c78b2bSJens Axboe 	const int read = bio_data_dir(bio) == READ;
1542f9c78b2bSJens Axboe 	struct bio_map_data *bmd = bio->bi_private;
1543f9c78b2bSJens Axboe 	int i;
1544f9c78b2bSJens Axboe 	char *p = bmd->sgvecs[0].iov_base;
1545f9c78b2bSJens Axboe 
1546f9c78b2bSJens Axboe 	bio_for_each_segment_all(bvec, bio, i) {
1547f9c78b2bSJens Axboe 		char *addr = page_address(bvec->bv_page);
1548f9c78b2bSJens Axboe 
1549f9c78b2bSJens Axboe 		if (read)
1550f9c78b2bSJens Axboe 			memcpy(p, addr, bvec->bv_len);
1551f9c78b2bSJens Axboe 
1552f9c78b2bSJens Axboe 		__free_page(bvec->bv_page);
1553f9c78b2bSJens Axboe 		p += bvec->bv_len;
1554f9c78b2bSJens Axboe 	}
1555f9c78b2bSJens Axboe 
1556f9c78b2bSJens Axboe 	kfree(bmd);
1557f9c78b2bSJens Axboe 	bio_put(bio);
1558f9c78b2bSJens Axboe }
1559f9c78b2bSJens Axboe 
1560f9c78b2bSJens Axboe /**
1561f9c78b2bSJens Axboe  *	bio_copy_kern	-	copy kernel address into bio
1562f9c78b2bSJens Axboe  *	@q: the struct request_queue for the bio
1563f9c78b2bSJens Axboe  *	@data: pointer to buffer to copy
1564f9c78b2bSJens Axboe  *	@len: length in bytes
1565f9c78b2bSJens Axboe  *	@gfp_mask: allocation flags for bio and page allocation
1566f9c78b2bSJens Axboe  *	@reading: data direction is READ
1567f9c78b2bSJens Axboe  *
1568f9c78b2bSJens Axboe  *	copy the kernel address into a bio suitable for io to a block
1569f9c78b2bSJens Axboe  *	device. Returns an error pointer in case of error.
1570f9c78b2bSJens Axboe  */
1571f9c78b2bSJens Axboe struct bio *bio_copy_kern(struct request_queue *q, void *data, unsigned int len,
1572f9c78b2bSJens Axboe 			  gfp_t gfp_mask, int reading)
1573f9c78b2bSJens Axboe {
1574f9c78b2bSJens Axboe 	struct bio *bio;
1575f9c78b2bSJens Axboe 	struct bio_vec *bvec;
1576f9c78b2bSJens Axboe 	int i;
1577f9c78b2bSJens Axboe 
1578f9c78b2bSJens Axboe 	bio = bio_copy_user(q, NULL, (unsigned long)data, len, 1, gfp_mask);
1579f9c78b2bSJens Axboe 	if (IS_ERR(bio))
1580f9c78b2bSJens Axboe 		return bio;
1581f9c78b2bSJens Axboe 
1582f9c78b2bSJens Axboe 	if (!reading) {
1583f9c78b2bSJens Axboe 		void *p = data;
1584f9c78b2bSJens Axboe 
1585f9c78b2bSJens Axboe 		bio_for_each_segment_all(bvec, bio, i) {
1586f9c78b2bSJens Axboe 			char *addr = page_address(bvec->bv_page);
1587f9c78b2bSJens Axboe 
1588f9c78b2bSJens Axboe 			memcpy(addr, p, bvec->bv_len);
1589f9c78b2bSJens Axboe 			p += bvec->bv_len;
1590f9c78b2bSJens Axboe 		}
1591f9c78b2bSJens Axboe 	}
1592f9c78b2bSJens Axboe 
1593f9c78b2bSJens Axboe 	bio->bi_end_io = bio_copy_kern_endio;
1594f9c78b2bSJens Axboe 
1595f9c78b2bSJens Axboe 	return bio;
1596f9c78b2bSJens Axboe }
1597f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_copy_kern);
1598f9c78b2bSJens Axboe 
1599f9c78b2bSJens Axboe /*
1600f9c78b2bSJens Axboe  * bio_set_pages_dirty() and bio_check_pages_dirty() are support functions
1601f9c78b2bSJens Axboe  * for performing direct-IO in BIOs.
1602f9c78b2bSJens Axboe  *
1603f9c78b2bSJens Axboe  * The problem is that we cannot run set_page_dirty() from interrupt context
1604f9c78b2bSJens Axboe  * because the required locks are not interrupt-safe.  So what we can do is to
1605f9c78b2bSJens Axboe  * mark the pages dirty _before_ performing IO.  And in interrupt context,
1606f9c78b2bSJens Axboe  * check that the pages are still dirty.   If so, fine.  If not, redirty them
1607f9c78b2bSJens Axboe  * in process context.
1608f9c78b2bSJens Axboe  *
1609f9c78b2bSJens Axboe  * We special-case compound pages here: normally this means reads into hugetlb
1610f9c78b2bSJens Axboe  * pages.  The logic in here doesn't really work right for compound pages
1611f9c78b2bSJens Axboe  * because the VM does not uniformly chase down the head page in all cases.
1612f9c78b2bSJens Axboe  * But dirtiness of compound pages is pretty meaningless anyway: the VM doesn't
1613f9c78b2bSJens Axboe  * handle them at all.  So we skip compound pages here at an early stage.
1614f9c78b2bSJens Axboe  *
1615f9c78b2bSJens Axboe  * Note that this code is very hard to test under normal circumstances because
1616f9c78b2bSJens Axboe  * direct-io pins the pages with get_user_pages().  This makes
1617f9c78b2bSJens Axboe  * is_page_cache_freeable return false, and the VM will not clean the pages.
1618f9c78b2bSJens Axboe  * But other code (eg, flusher threads) could clean the pages if they are mapped
1619f9c78b2bSJens Axboe  * pagecache.
1620f9c78b2bSJens Axboe  *
1621f9c78b2bSJens Axboe  * Simply disabling the call to bio_set_pages_dirty() is a good way to test the
1622f9c78b2bSJens Axboe  * deferred bio dirtying paths.
1623f9c78b2bSJens Axboe  */
1624f9c78b2bSJens Axboe 
1625f9c78b2bSJens Axboe /*
1626f9c78b2bSJens Axboe  * bio_set_pages_dirty() will mark all the bio's pages as dirty.
1627f9c78b2bSJens Axboe  */
1628f9c78b2bSJens Axboe void bio_set_pages_dirty(struct bio *bio)
1629f9c78b2bSJens Axboe {
1630f9c78b2bSJens Axboe 	struct bio_vec *bvec;
1631f9c78b2bSJens Axboe 	int i;
1632f9c78b2bSJens Axboe 
1633f9c78b2bSJens Axboe 	bio_for_each_segment_all(bvec, bio, i) {
1634f9c78b2bSJens Axboe 		struct page *page = bvec->bv_page;
1635f9c78b2bSJens Axboe 
1636f9c78b2bSJens Axboe 		if (page && !PageCompound(page))
1637f9c78b2bSJens Axboe 			set_page_dirty_lock(page);
1638f9c78b2bSJens Axboe 	}
1639f9c78b2bSJens Axboe }
1640f9c78b2bSJens Axboe 
1641f9c78b2bSJens Axboe static void bio_release_pages(struct bio *bio)
1642f9c78b2bSJens Axboe {
1643f9c78b2bSJens Axboe 	struct bio_vec *bvec;
1644f9c78b2bSJens Axboe 	int i;
1645f9c78b2bSJens Axboe 
1646f9c78b2bSJens Axboe 	bio_for_each_segment_all(bvec, bio, i) {
1647f9c78b2bSJens Axboe 		struct page *page = bvec->bv_page;
1648f9c78b2bSJens Axboe 
1649f9c78b2bSJens Axboe 		if (page)
1650f9c78b2bSJens Axboe 			put_page(page);
1651f9c78b2bSJens Axboe 	}
1652f9c78b2bSJens Axboe }
1653f9c78b2bSJens Axboe 
1654f9c78b2bSJens Axboe /*
1655f9c78b2bSJens Axboe  * bio_check_pages_dirty() will check that all the BIO's pages are still dirty.
1656f9c78b2bSJens Axboe  * If they are, then fine.  If, however, some pages are clean then they must
1657f9c78b2bSJens Axboe  * have been written out during the direct-IO read.  So we take another ref on
1658f9c78b2bSJens Axboe  * the BIO and the offending pages and re-dirty the pages in process context.
1659f9c78b2bSJens Axboe  *
1660f9c78b2bSJens Axboe  * It is expected that bio_check_pages_dirty() will wholly own the BIO from
1661f9c78b2bSJens Axboe  * here on.  It will run one page_cache_release() against each page and will
1662f9c78b2bSJens Axboe  * run one bio_put() against the BIO.
1663f9c78b2bSJens Axboe  */
1664f9c78b2bSJens Axboe 
1665f9c78b2bSJens Axboe static void bio_dirty_fn(struct work_struct *work);
1666f9c78b2bSJens Axboe 
1667f9c78b2bSJens Axboe static DECLARE_WORK(bio_dirty_work, bio_dirty_fn);
1668f9c78b2bSJens Axboe static DEFINE_SPINLOCK(bio_dirty_lock);
1669f9c78b2bSJens Axboe static struct bio *bio_dirty_list;
1670f9c78b2bSJens Axboe 
1671f9c78b2bSJens Axboe /*
1672f9c78b2bSJens Axboe  * This runs in process context
1673f9c78b2bSJens Axboe  */
1674f9c78b2bSJens Axboe static void bio_dirty_fn(struct work_struct *work)
1675f9c78b2bSJens Axboe {
1676f9c78b2bSJens Axboe 	unsigned long flags;
1677f9c78b2bSJens Axboe 	struct bio *bio;
1678f9c78b2bSJens Axboe 
1679f9c78b2bSJens Axboe 	spin_lock_irqsave(&bio_dirty_lock, flags);
1680f9c78b2bSJens Axboe 	bio = bio_dirty_list;
1681f9c78b2bSJens Axboe 	bio_dirty_list = NULL;
1682f9c78b2bSJens Axboe 	spin_unlock_irqrestore(&bio_dirty_lock, flags);
1683f9c78b2bSJens Axboe 
1684f9c78b2bSJens Axboe 	while (bio) {
1685f9c78b2bSJens Axboe 		struct bio *next = bio->bi_private;
1686f9c78b2bSJens Axboe 
1687f9c78b2bSJens Axboe 		bio_set_pages_dirty(bio);
1688f9c78b2bSJens Axboe 		bio_release_pages(bio);
1689f9c78b2bSJens Axboe 		bio_put(bio);
1690f9c78b2bSJens Axboe 		bio = next;
1691f9c78b2bSJens Axboe 	}
1692f9c78b2bSJens Axboe }
1693f9c78b2bSJens Axboe 
1694f9c78b2bSJens Axboe void bio_check_pages_dirty(struct bio *bio)
1695f9c78b2bSJens Axboe {
1696f9c78b2bSJens Axboe 	struct bio_vec *bvec;
1697f9c78b2bSJens Axboe 	int nr_clean_pages = 0;
1698f9c78b2bSJens Axboe 	int i;
1699f9c78b2bSJens Axboe 
1700f9c78b2bSJens Axboe 	bio_for_each_segment_all(bvec, bio, i) {
1701f9c78b2bSJens Axboe 		struct page *page = bvec->bv_page;
1702f9c78b2bSJens Axboe 
1703f9c78b2bSJens Axboe 		if (PageDirty(page) || PageCompound(page)) {
1704f9c78b2bSJens Axboe 			page_cache_release(page);
1705f9c78b2bSJens Axboe 			bvec->bv_page = NULL;
1706f9c78b2bSJens Axboe 		} else {
1707f9c78b2bSJens Axboe 			nr_clean_pages++;
1708f9c78b2bSJens Axboe 		}
1709f9c78b2bSJens Axboe 	}
1710f9c78b2bSJens Axboe 
1711f9c78b2bSJens Axboe 	if (nr_clean_pages) {
1712f9c78b2bSJens Axboe 		unsigned long flags;
1713f9c78b2bSJens Axboe 
1714f9c78b2bSJens Axboe 		spin_lock_irqsave(&bio_dirty_lock, flags);
1715f9c78b2bSJens Axboe 		bio->bi_private = bio_dirty_list;
1716f9c78b2bSJens Axboe 		bio_dirty_list = bio;
1717f9c78b2bSJens Axboe 		spin_unlock_irqrestore(&bio_dirty_lock, flags);
1718f9c78b2bSJens Axboe 		schedule_work(&bio_dirty_work);
1719f9c78b2bSJens Axboe 	} else {
1720f9c78b2bSJens Axboe 		bio_put(bio);
1721f9c78b2bSJens Axboe 	}
1722f9c78b2bSJens Axboe }
1723f9c78b2bSJens Axboe 
1724f9c78b2bSJens Axboe #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
1725f9c78b2bSJens Axboe void bio_flush_dcache_pages(struct bio *bi)
1726f9c78b2bSJens Axboe {
1727f9c78b2bSJens Axboe 	struct bio_vec bvec;
1728f9c78b2bSJens Axboe 	struct bvec_iter iter;
1729f9c78b2bSJens Axboe 
1730f9c78b2bSJens Axboe 	bio_for_each_segment(bvec, bi, iter)
1731f9c78b2bSJens Axboe 		flush_dcache_page(bvec.bv_page);
1732f9c78b2bSJens Axboe }
1733f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_flush_dcache_pages);
1734f9c78b2bSJens Axboe #endif
1735f9c78b2bSJens Axboe 
1736f9c78b2bSJens Axboe /**
1737f9c78b2bSJens Axboe  * bio_endio - end I/O on a bio
1738f9c78b2bSJens Axboe  * @bio:	bio
1739f9c78b2bSJens Axboe  * @error:	error, if any
1740f9c78b2bSJens Axboe  *
1741f9c78b2bSJens Axboe  * Description:
1742f9c78b2bSJens Axboe  *   bio_endio() will end I/O on the whole bio. bio_endio() is the
1743f9c78b2bSJens Axboe  *   preferred way to end I/O on a bio, it takes care of clearing
1744f9c78b2bSJens Axboe  *   BIO_UPTODATE on error. @error is 0 on success, and and one of the
1745f9c78b2bSJens Axboe  *   established -Exxxx (-EIO, for instance) error values in case
1746f9c78b2bSJens Axboe  *   something went wrong. No one should call bi_end_io() directly on a
1747f9c78b2bSJens Axboe  *   bio unless they own it and thus know that it has an end_io
1748f9c78b2bSJens Axboe  *   function.
1749f9c78b2bSJens Axboe  **/
1750f9c78b2bSJens Axboe void bio_endio(struct bio *bio, int error)
1751f9c78b2bSJens Axboe {
1752f9c78b2bSJens Axboe 	while (bio) {
1753f9c78b2bSJens Axboe 		BUG_ON(atomic_read(&bio->bi_remaining) <= 0);
1754f9c78b2bSJens Axboe 
1755f9c78b2bSJens Axboe 		if (error)
1756f9c78b2bSJens Axboe 			clear_bit(BIO_UPTODATE, &bio->bi_flags);
1757f9c78b2bSJens Axboe 		else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
1758f9c78b2bSJens Axboe 			error = -EIO;
1759f9c78b2bSJens Axboe 
1760f9c78b2bSJens Axboe 		if (!atomic_dec_and_test(&bio->bi_remaining))
1761f9c78b2bSJens Axboe 			return;
1762f9c78b2bSJens Axboe 
1763f9c78b2bSJens Axboe 		/*
1764f9c78b2bSJens Axboe 		 * Need to have a real endio function for chained bios,
1765f9c78b2bSJens Axboe 		 * otherwise various corner cases will break (like stacking
1766f9c78b2bSJens Axboe 		 * block devices that save/restore bi_end_io) - however, we want
1767f9c78b2bSJens Axboe 		 * to avoid unbounded recursion and blowing the stack. Tail call
1768f9c78b2bSJens Axboe 		 * optimization would handle this, but compiling with frame
1769f9c78b2bSJens Axboe 		 * pointers also disables gcc's sibling call optimization.
1770f9c78b2bSJens Axboe 		 */
1771f9c78b2bSJens Axboe 		if (bio->bi_end_io == bio_chain_endio) {
1772f9c78b2bSJens Axboe 			struct bio *parent = bio->bi_private;
1773f9c78b2bSJens Axboe 			bio_put(bio);
1774f9c78b2bSJens Axboe 			bio = parent;
1775f9c78b2bSJens Axboe 		} else {
1776f9c78b2bSJens Axboe 			if (bio->bi_end_io)
1777f9c78b2bSJens Axboe 				bio->bi_end_io(bio, error);
1778f9c78b2bSJens Axboe 			bio = NULL;
1779f9c78b2bSJens Axboe 		}
1780f9c78b2bSJens Axboe 	}
1781f9c78b2bSJens Axboe }
1782f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_endio);
1783f9c78b2bSJens Axboe 
1784f9c78b2bSJens Axboe /**
1785f9c78b2bSJens Axboe  * bio_endio_nodec - end I/O on a bio, without decrementing bi_remaining
1786f9c78b2bSJens Axboe  * @bio:	bio
1787f9c78b2bSJens Axboe  * @error:	error, if any
1788f9c78b2bSJens Axboe  *
1789f9c78b2bSJens Axboe  * For code that has saved and restored bi_end_io; thing hard before using this
1790f9c78b2bSJens Axboe  * function, probably you should've cloned the entire bio.
1791f9c78b2bSJens Axboe  **/
1792f9c78b2bSJens Axboe void bio_endio_nodec(struct bio *bio, int error)
1793f9c78b2bSJens Axboe {
1794f9c78b2bSJens Axboe 	atomic_inc(&bio->bi_remaining);
1795f9c78b2bSJens Axboe 	bio_endio(bio, error);
1796f9c78b2bSJens Axboe }
1797f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_endio_nodec);
1798f9c78b2bSJens Axboe 
1799f9c78b2bSJens Axboe /**
1800f9c78b2bSJens Axboe  * bio_split - split a bio
1801f9c78b2bSJens Axboe  * @bio:	bio to split
1802f9c78b2bSJens Axboe  * @sectors:	number of sectors to split from the front of @bio
1803f9c78b2bSJens Axboe  * @gfp:	gfp mask
1804f9c78b2bSJens Axboe  * @bs:		bio set to allocate from
1805f9c78b2bSJens Axboe  *
1806f9c78b2bSJens Axboe  * Allocates and returns a new bio which represents @sectors from the start of
1807f9c78b2bSJens Axboe  * @bio, and updates @bio to represent the remaining sectors.
1808f9c78b2bSJens Axboe  *
1809f9c78b2bSJens Axboe  * The newly allocated bio will point to @bio's bi_io_vec; it is the caller's
1810f9c78b2bSJens Axboe  * responsibility to ensure that @bio is not freed before the split.
1811f9c78b2bSJens Axboe  */
1812f9c78b2bSJens Axboe struct bio *bio_split(struct bio *bio, int sectors,
1813f9c78b2bSJens Axboe 		      gfp_t gfp, struct bio_set *bs)
1814f9c78b2bSJens Axboe {
1815f9c78b2bSJens Axboe 	struct bio *split = NULL;
1816f9c78b2bSJens Axboe 
1817f9c78b2bSJens Axboe 	BUG_ON(sectors <= 0);
1818f9c78b2bSJens Axboe 	BUG_ON(sectors >= bio_sectors(bio));
1819f9c78b2bSJens Axboe 
1820f9c78b2bSJens Axboe 	split = bio_clone_fast(bio, gfp, bs);
1821f9c78b2bSJens Axboe 	if (!split)
1822f9c78b2bSJens Axboe 		return NULL;
1823f9c78b2bSJens Axboe 
1824f9c78b2bSJens Axboe 	split->bi_iter.bi_size = sectors << 9;
1825f9c78b2bSJens Axboe 
1826f9c78b2bSJens Axboe 	if (bio_integrity(split))
1827f9c78b2bSJens Axboe 		bio_integrity_trim(split, 0, sectors);
1828f9c78b2bSJens Axboe 
1829f9c78b2bSJens Axboe 	bio_advance(bio, split->bi_iter.bi_size);
1830f9c78b2bSJens Axboe 
1831f9c78b2bSJens Axboe 	return split;
1832f9c78b2bSJens Axboe }
1833f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_split);
1834f9c78b2bSJens Axboe 
1835f9c78b2bSJens Axboe /**
1836f9c78b2bSJens Axboe  * bio_trim - trim a bio
1837f9c78b2bSJens Axboe  * @bio:	bio to trim
1838f9c78b2bSJens Axboe  * @offset:	number of sectors to trim from the front of @bio
1839f9c78b2bSJens Axboe  * @size:	size we want to trim @bio to, in sectors
1840f9c78b2bSJens Axboe  */
1841f9c78b2bSJens Axboe void bio_trim(struct bio *bio, int offset, int size)
1842f9c78b2bSJens Axboe {
1843f9c78b2bSJens Axboe 	/* 'bio' is a cloned bio which we need to trim to match
1844f9c78b2bSJens Axboe 	 * the given offset and size.
1845f9c78b2bSJens Axboe 	 */
1846f9c78b2bSJens Axboe 
1847f9c78b2bSJens Axboe 	size <<= 9;
1848f9c78b2bSJens Axboe 	if (offset == 0 && size == bio->bi_iter.bi_size)
1849f9c78b2bSJens Axboe 		return;
1850f9c78b2bSJens Axboe 
1851f9c78b2bSJens Axboe 	clear_bit(BIO_SEG_VALID, &bio->bi_flags);
1852f9c78b2bSJens Axboe 
1853f9c78b2bSJens Axboe 	bio_advance(bio, offset << 9);
1854f9c78b2bSJens Axboe 
1855f9c78b2bSJens Axboe 	bio->bi_iter.bi_size = size;
1856f9c78b2bSJens Axboe }
1857f9c78b2bSJens Axboe EXPORT_SYMBOL_GPL(bio_trim);
1858f9c78b2bSJens Axboe 
1859f9c78b2bSJens Axboe /*
1860f9c78b2bSJens Axboe  * create memory pools for biovec's in a bio_set.
1861f9c78b2bSJens Axboe  * use the global biovec slabs created for general use.
1862f9c78b2bSJens Axboe  */
1863f9c78b2bSJens Axboe mempool_t *biovec_create_pool(int pool_entries)
1864f9c78b2bSJens Axboe {
1865f9c78b2bSJens Axboe 	struct biovec_slab *bp = bvec_slabs + BIOVEC_MAX_IDX;
1866f9c78b2bSJens Axboe 
1867f9c78b2bSJens Axboe 	return mempool_create_slab_pool(pool_entries, bp->slab);
1868f9c78b2bSJens Axboe }
1869f9c78b2bSJens Axboe 
1870f9c78b2bSJens Axboe void bioset_free(struct bio_set *bs)
1871f9c78b2bSJens Axboe {
1872f9c78b2bSJens Axboe 	if (bs->rescue_workqueue)
1873f9c78b2bSJens Axboe 		destroy_workqueue(bs->rescue_workqueue);
1874f9c78b2bSJens Axboe 
1875f9c78b2bSJens Axboe 	if (bs->bio_pool)
1876f9c78b2bSJens Axboe 		mempool_destroy(bs->bio_pool);
1877f9c78b2bSJens Axboe 
1878f9c78b2bSJens Axboe 	if (bs->bvec_pool)
1879f9c78b2bSJens Axboe 		mempool_destroy(bs->bvec_pool);
1880f9c78b2bSJens Axboe 
1881f9c78b2bSJens Axboe 	bioset_integrity_free(bs);
1882f9c78b2bSJens Axboe 	bio_put_slab(bs);
1883f9c78b2bSJens Axboe 
1884f9c78b2bSJens Axboe 	kfree(bs);
1885f9c78b2bSJens Axboe }
1886f9c78b2bSJens Axboe EXPORT_SYMBOL(bioset_free);
1887f9c78b2bSJens Axboe 
1888f9c78b2bSJens Axboe /**
1889f9c78b2bSJens Axboe  * bioset_create  - Create a bio_set
1890f9c78b2bSJens Axboe  * @pool_size:	Number of bio and bio_vecs to cache in the mempool
1891f9c78b2bSJens Axboe  * @front_pad:	Number of bytes to allocate in front of the returned bio
1892f9c78b2bSJens Axboe  *
1893f9c78b2bSJens Axboe  * Description:
1894f9c78b2bSJens Axboe  *    Set up a bio_set to be used with @bio_alloc_bioset. Allows the caller
1895f9c78b2bSJens Axboe  *    to ask for a number of bytes to be allocated in front of the bio.
1896f9c78b2bSJens Axboe  *    Front pad allocation is useful for embedding the bio inside
1897f9c78b2bSJens Axboe  *    another structure, to avoid allocating extra data to go with the bio.
1898f9c78b2bSJens Axboe  *    Note that the bio must be embedded at the END of that structure always,
1899f9c78b2bSJens Axboe  *    or things will break badly.
1900f9c78b2bSJens Axboe  */
1901f9c78b2bSJens Axboe struct bio_set *bioset_create(unsigned int pool_size, unsigned int front_pad)
1902f9c78b2bSJens Axboe {
1903f9c78b2bSJens Axboe 	unsigned int back_pad = BIO_INLINE_VECS * sizeof(struct bio_vec);
1904f9c78b2bSJens Axboe 	struct bio_set *bs;
1905f9c78b2bSJens Axboe 
1906f9c78b2bSJens Axboe 	bs = kzalloc(sizeof(*bs), GFP_KERNEL);
1907f9c78b2bSJens Axboe 	if (!bs)
1908f9c78b2bSJens Axboe 		return NULL;
1909f9c78b2bSJens Axboe 
1910f9c78b2bSJens Axboe 	bs->front_pad = front_pad;
1911f9c78b2bSJens Axboe 
1912f9c78b2bSJens Axboe 	spin_lock_init(&bs->rescue_lock);
1913f9c78b2bSJens Axboe 	bio_list_init(&bs->rescue_list);
1914f9c78b2bSJens Axboe 	INIT_WORK(&bs->rescue_work, bio_alloc_rescue);
1915f9c78b2bSJens Axboe 
1916f9c78b2bSJens Axboe 	bs->bio_slab = bio_find_or_create_slab(front_pad + back_pad);
1917f9c78b2bSJens Axboe 	if (!bs->bio_slab) {
1918f9c78b2bSJens Axboe 		kfree(bs);
1919f9c78b2bSJens Axboe 		return NULL;
1920f9c78b2bSJens Axboe 	}
1921f9c78b2bSJens Axboe 
1922f9c78b2bSJens Axboe 	bs->bio_pool = mempool_create_slab_pool(pool_size, bs->bio_slab);
1923f9c78b2bSJens Axboe 	if (!bs->bio_pool)
1924f9c78b2bSJens Axboe 		goto bad;
1925f9c78b2bSJens Axboe 
1926f9c78b2bSJens Axboe 	bs->bvec_pool = biovec_create_pool(pool_size);
1927f9c78b2bSJens Axboe 	if (!bs->bvec_pool)
1928f9c78b2bSJens Axboe 		goto bad;
1929f9c78b2bSJens Axboe 
1930f9c78b2bSJens Axboe 	bs->rescue_workqueue = alloc_workqueue("bioset", WQ_MEM_RECLAIM, 0);
1931f9c78b2bSJens Axboe 	if (!bs->rescue_workqueue)
1932f9c78b2bSJens Axboe 		goto bad;
1933f9c78b2bSJens Axboe 
1934f9c78b2bSJens Axboe 	return bs;
1935f9c78b2bSJens Axboe bad:
1936f9c78b2bSJens Axboe 	bioset_free(bs);
1937f9c78b2bSJens Axboe 	return NULL;
1938f9c78b2bSJens Axboe }
1939f9c78b2bSJens Axboe EXPORT_SYMBOL(bioset_create);
1940f9c78b2bSJens Axboe 
1941f9c78b2bSJens Axboe #ifdef CONFIG_BLK_CGROUP
1942f9c78b2bSJens Axboe /**
1943f9c78b2bSJens Axboe  * bio_associate_current - associate a bio with %current
1944f9c78b2bSJens Axboe  * @bio: target bio
1945f9c78b2bSJens Axboe  *
1946f9c78b2bSJens Axboe  * Associate @bio with %current if it hasn't been associated yet.  Block
1947f9c78b2bSJens Axboe  * layer will treat @bio as if it were issued by %current no matter which
1948f9c78b2bSJens Axboe  * task actually issues it.
1949f9c78b2bSJens Axboe  *
1950f9c78b2bSJens Axboe  * This function takes an extra reference of @task's io_context and blkcg
1951f9c78b2bSJens Axboe  * which will be put when @bio is released.  The caller must own @bio,
1952f9c78b2bSJens Axboe  * ensure %current->io_context exists, and is responsible for synchronizing
1953f9c78b2bSJens Axboe  * calls to this function.
1954f9c78b2bSJens Axboe  */
1955f9c78b2bSJens Axboe int bio_associate_current(struct bio *bio)
1956f9c78b2bSJens Axboe {
1957f9c78b2bSJens Axboe 	struct io_context *ioc;
1958f9c78b2bSJens Axboe 	struct cgroup_subsys_state *css;
1959f9c78b2bSJens Axboe 
1960f9c78b2bSJens Axboe 	if (bio->bi_ioc)
1961f9c78b2bSJens Axboe 		return -EBUSY;
1962f9c78b2bSJens Axboe 
1963f9c78b2bSJens Axboe 	ioc = current->io_context;
1964f9c78b2bSJens Axboe 	if (!ioc)
1965f9c78b2bSJens Axboe 		return -ENOENT;
1966f9c78b2bSJens Axboe 
1967f9c78b2bSJens Axboe 	/* acquire active ref on @ioc and associate */
1968f9c78b2bSJens Axboe 	get_io_context_active(ioc);
1969f9c78b2bSJens Axboe 	bio->bi_ioc = ioc;
1970f9c78b2bSJens Axboe 
1971f9c78b2bSJens Axboe 	/* associate blkcg if exists */
1972f9c78b2bSJens Axboe 	rcu_read_lock();
1973f9c78b2bSJens Axboe 	css = task_css(current, blkio_cgrp_id);
1974f9c78b2bSJens Axboe 	if (css && css_tryget(css))
1975f9c78b2bSJens Axboe 		bio->bi_css = css;
1976f9c78b2bSJens Axboe 	rcu_read_unlock();
1977f9c78b2bSJens Axboe 
1978f9c78b2bSJens Axboe 	return 0;
1979f9c78b2bSJens Axboe }
1980f9c78b2bSJens Axboe 
1981f9c78b2bSJens Axboe /**
1982f9c78b2bSJens Axboe  * bio_disassociate_task - undo bio_associate_current()
1983f9c78b2bSJens Axboe  * @bio: target bio
1984f9c78b2bSJens Axboe  */
1985f9c78b2bSJens Axboe void bio_disassociate_task(struct bio *bio)
1986f9c78b2bSJens Axboe {
1987f9c78b2bSJens Axboe 	if (bio->bi_ioc) {
1988f9c78b2bSJens Axboe 		put_io_context(bio->bi_ioc);
1989f9c78b2bSJens Axboe 		bio->bi_ioc = NULL;
1990f9c78b2bSJens Axboe 	}
1991f9c78b2bSJens Axboe 	if (bio->bi_css) {
1992f9c78b2bSJens Axboe 		css_put(bio->bi_css);
1993f9c78b2bSJens Axboe 		bio->bi_css = NULL;
1994f9c78b2bSJens Axboe 	}
1995f9c78b2bSJens Axboe }
1996f9c78b2bSJens Axboe 
1997f9c78b2bSJens Axboe #endif /* CONFIG_BLK_CGROUP */
1998f9c78b2bSJens Axboe 
1999f9c78b2bSJens Axboe static void __init biovec_init_slabs(void)
2000f9c78b2bSJens Axboe {
2001f9c78b2bSJens Axboe 	int i;
2002f9c78b2bSJens Axboe 
2003f9c78b2bSJens Axboe 	for (i = 0; i < BIOVEC_NR_POOLS; i++) {
2004f9c78b2bSJens Axboe 		int size;
2005f9c78b2bSJens Axboe 		struct biovec_slab *bvs = bvec_slabs + i;
2006f9c78b2bSJens Axboe 
2007f9c78b2bSJens Axboe 		if (bvs->nr_vecs <= BIO_INLINE_VECS) {
2008f9c78b2bSJens Axboe 			bvs->slab = NULL;
2009f9c78b2bSJens Axboe 			continue;
2010f9c78b2bSJens Axboe 		}
2011f9c78b2bSJens Axboe 
2012f9c78b2bSJens Axboe 		size = bvs->nr_vecs * sizeof(struct bio_vec);
2013f9c78b2bSJens Axboe 		bvs->slab = kmem_cache_create(bvs->name, size, 0,
2014f9c78b2bSJens Axboe                                 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
2015f9c78b2bSJens Axboe 	}
2016f9c78b2bSJens Axboe }
2017f9c78b2bSJens Axboe 
2018f9c78b2bSJens Axboe static int __init init_bio(void)
2019f9c78b2bSJens Axboe {
2020f9c78b2bSJens Axboe 	bio_slab_max = 2;
2021f9c78b2bSJens Axboe 	bio_slab_nr = 0;
2022f9c78b2bSJens Axboe 	bio_slabs = kzalloc(bio_slab_max * sizeof(struct bio_slab), GFP_KERNEL);
2023f9c78b2bSJens Axboe 	if (!bio_slabs)
2024f9c78b2bSJens Axboe 		panic("bio: can't allocate bios\n");
2025f9c78b2bSJens Axboe 
2026f9c78b2bSJens Axboe 	bio_integrity_init();
2027f9c78b2bSJens Axboe 	biovec_init_slabs();
2028f9c78b2bSJens Axboe 
2029f9c78b2bSJens Axboe 	fs_bio_set = bioset_create(BIO_POOL_SIZE, 0);
2030f9c78b2bSJens Axboe 	if (!fs_bio_set)
2031f9c78b2bSJens Axboe 		panic("bio: can't allocate bios\n");
2032f9c78b2bSJens Axboe 
2033f9c78b2bSJens Axboe 	if (bioset_integrity_create(fs_bio_set, BIO_POOL_SIZE))
2034f9c78b2bSJens Axboe 		panic("bio: can't create integrity pool\n");
2035f9c78b2bSJens Axboe 
2036f9c78b2bSJens Axboe 	return 0;
2037f9c78b2bSJens Axboe }
2038f9c78b2bSJens Axboe subsys_initcall(init_bio);
2039