blob: 9238a54b562c9bb0b672f4bd6359871e192c21d8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jens Axboe0fe23472006-09-04 15:41:16 +02002 * Copyright (C) 2001 Jens Axboe <axboe@kernel.dk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public Licens
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
16 *
17 */
18#include <linux/mm.h>
19#include <linux/swap.h>
20#include <linux/bio.h>
21#include <linux/blkdev.h>
Tejun Heo852c7882012-03-05 13:15:27 -080022#include <linux/iocontext.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/slab.h>
24#include <linux/init.h>
25#include <linux/kernel.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050026#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/mempool.h>
28#include <linux/workqueue.h>
Tejun Heo852c7882012-03-05 13:15:27 -080029#include <linux/cgroup.h>
James Bottomley f1970ba2005-06-20 14:06:52 +020030#include <scsi/sg.h> /* for struct sg_iovec */
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Li Zefan55782132009-06-09 13:43:05 +080032#include <trace/events/block.h>
Ingo Molnar0bfc2452008-11-26 11:59:56 +010033
Jens Axboe392ddc32008-12-23 12:42:54 +010034/*
35 * Test patch to inline a certain number of bi_io_vec's inside the bio
36 * itself, to shrink a bio data allocation from two mempool calls to one
37 */
38#define BIO_INLINE_VECS 4
39
Denis ChengRq6feef532008-10-09 08:57:05 +020040static mempool_t *bio_split_pool __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/*
43 * if you change this list, also change bvec_alloc or things will
44 * break badly! cannot be bigger than what you can fit into an
45 * unsigned short
46 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define BV(x) { .nr_vecs = x, .name = "biovec-"__stringify(x) }
Martin K. Petersendf677142011-03-08 08:28:01 +010048static struct biovec_slab bvec_slabs[BIOVEC_NR_POOLS] __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 BV(1), BV(4), BV(16), BV(64), BV(128), BV(BIO_MAX_PAGES),
50};
51#undef BV
52
53/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * fs_bio_set is the bio_set containing bio and iovec memory pools used by
55 * IO code that does not need private memory pools.
56 */
Martin K. Petersen51d654e2008-06-17 18:59:56 +020057struct bio_set *fs_bio_set;
Kent Overstreet3f86a822012-09-06 15:35:01 -070058EXPORT_SYMBOL(fs_bio_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Jens Axboebb799ca2008-12-10 15:35:05 +010060/*
61 * Our slab pool management
62 */
63struct bio_slab {
64 struct kmem_cache *slab;
65 unsigned int slab_ref;
66 unsigned int slab_size;
67 char name[8];
68};
69static DEFINE_MUTEX(bio_slab_lock);
70static struct bio_slab *bio_slabs;
71static unsigned int bio_slab_nr, bio_slab_max;
72
73static struct kmem_cache *bio_find_or_create_slab(unsigned int extra_size)
74{
75 unsigned int sz = sizeof(struct bio) + extra_size;
76 struct kmem_cache *slab = NULL;
Alexey Khoroshilov389d7b22012-08-09 15:19:25 +020077 struct bio_slab *bslab, *new_bio_slabs;
Anna Leuschner386bc352012-10-22 21:53:36 +020078 unsigned int new_bio_slab_max;
Jens Axboebb799ca2008-12-10 15:35:05 +010079 unsigned int i, entry = -1;
80
81 mutex_lock(&bio_slab_lock);
82
83 i = 0;
84 while (i < bio_slab_nr) {
Thiago Farinaf06f1352010-01-19 14:07:09 +010085 bslab = &bio_slabs[i];
Jens Axboebb799ca2008-12-10 15:35:05 +010086
87 if (!bslab->slab && entry == -1)
88 entry = i;
89 else if (bslab->slab_size == sz) {
90 slab = bslab->slab;
91 bslab->slab_ref++;
92 break;
93 }
94 i++;
95 }
96
97 if (slab)
98 goto out_unlock;
99
100 if (bio_slab_nr == bio_slab_max && entry == -1) {
Anna Leuschner386bc352012-10-22 21:53:36 +0200101 new_bio_slab_max = bio_slab_max << 1;
Alexey Khoroshilov389d7b22012-08-09 15:19:25 +0200102 new_bio_slabs = krealloc(bio_slabs,
Anna Leuschner386bc352012-10-22 21:53:36 +0200103 new_bio_slab_max * sizeof(struct bio_slab),
Alexey Khoroshilov389d7b22012-08-09 15:19:25 +0200104 GFP_KERNEL);
105 if (!new_bio_slabs)
Jens Axboebb799ca2008-12-10 15:35:05 +0100106 goto out_unlock;
Anna Leuschner386bc352012-10-22 21:53:36 +0200107 bio_slab_max = new_bio_slab_max;
Alexey Khoroshilov389d7b22012-08-09 15:19:25 +0200108 bio_slabs = new_bio_slabs;
Jens Axboebb799ca2008-12-10 15:35:05 +0100109 }
110 if (entry == -1)
111 entry = bio_slab_nr++;
112
113 bslab = &bio_slabs[entry];
114
115 snprintf(bslab->name, sizeof(bslab->name), "bio-%d", entry);
116 slab = kmem_cache_create(bslab->name, sz, 0, SLAB_HWCACHE_ALIGN, NULL);
117 if (!slab)
118 goto out_unlock;
119
Mandeep Singh Baines80cdc6d2011-03-22 16:33:54 -0700120 printk(KERN_INFO "bio: create slab <%s> at %d\n", bslab->name, entry);
Jens Axboebb799ca2008-12-10 15:35:05 +0100121 bslab->slab = slab;
122 bslab->slab_ref = 1;
123 bslab->slab_size = sz;
124out_unlock:
125 mutex_unlock(&bio_slab_lock);
126 return slab;
127}
128
129static void bio_put_slab(struct bio_set *bs)
130{
131 struct bio_slab *bslab = NULL;
132 unsigned int i;
133
134 mutex_lock(&bio_slab_lock);
135
136 for (i = 0; i < bio_slab_nr; i++) {
137 if (bs->bio_slab == bio_slabs[i].slab) {
138 bslab = &bio_slabs[i];
139 break;
140 }
141 }
142
143 if (WARN(!bslab, KERN_ERR "bio: unable to find slab!\n"))
144 goto out;
145
146 WARN_ON(!bslab->slab_ref);
147
148 if (--bslab->slab_ref)
149 goto out;
150
151 kmem_cache_destroy(bslab->slab);
152 bslab->slab = NULL;
153
154out:
155 mutex_unlock(&bio_slab_lock);
156}
157
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200158unsigned int bvec_nr_vecs(unsigned short idx)
159{
160 return bvec_slabs[idx].nr_vecs;
161}
162
Kent Overstreet9f060e22012-10-12 15:29:33 -0700163void bvec_free(mempool_t *pool, struct bio_vec *bv, unsigned int idx)
Jens Axboebb799ca2008-12-10 15:35:05 +0100164{
165 BIO_BUG_ON(idx >= BIOVEC_NR_POOLS);
166
167 if (idx == BIOVEC_MAX_IDX)
Kent Overstreet9f060e22012-10-12 15:29:33 -0700168 mempool_free(bv, pool);
Jens Axboebb799ca2008-12-10 15:35:05 +0100169 else {
170 struct biovec_slab *bvs = bvec_slabs + idx;
171
172 kmem_cache_free(bvs->slab, bv);
173 }
174}
175
Kent Overstreet9f060e22012-10-12 15:29:33 -0700176struct bio_vec *bvec_alloc(gfp_t gfp_mask, int nr, unsigned long *idx,
177 mempool_t *pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 struct bio_vec *bvl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 /*
Jens Axboe7ff93452008-12-11 11:53:43 +0100182 * see comment near bvec_array define!
183 */
184 switch (nr) {
185 case 1:
186 *idx = 0;
187 break;
188 case 2 ... 4:
189 *idx = 1;
190 break;
191 case 5 ... 16:
192 *idx = 2;
193 break;
194 case 17 ... 64:
195 *idx = 3;
196 break;
197 case 65 ... 128:
198 *idx = 4;
199 break;
200 case 129 ... BIO_MAX_PAGES:
201 *idx = 5;
202 break;
203 default:
204 return NULL;
205 }
206
207 /*
208 * idx now points to the pool we want to allocate from. only the
209 * 1-vec entry pool is mempool backed.
210 */
211 if (*idx == BIOVEC_MAX_IDX) {
212fallback:
Kent Overstreet9f060e22012-10-12 15:29:33 -0700213 bvl = mempool_alloc(pool, gfp_mask);
Jens Axboe7ff93452008-12-11 11:53:43 +0100214 } else {
215 struct biovec_slab *bvs = bvec_slabs + *idx;
216 gfp_t __gfp_mask = gfp_mask & ~(__GFP_WAIT | __GFP_IO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Jens Axboe0a0d96b2008-09-11 13:17:37 +0200218 /*
Jens Axboe7ff93452008-12-11 11:53:43 +0100219 * Make this allocation restricted and don't dump info on
220 * allocation failures, since we'll fallback to the mempool
221 * in case of failure.
Jens Axboe0a0d96b2008-09-11 13:17:37 +0200222 */
Jens Axboe7ff93452008-12-11 11:53:43 +0100223 __gfp_mask |= __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
224
225 /*
226 * Try a slab allocation. If this fails and __GFP_WAIT
227 * is set, retry with the 1-entry mempool
228 */
229 bvl = kmem_cache_alloc(bvs->slab, __gfp_mask);
230 if (unlikely(!bvl && (gfp_mask & __GFP_WAIT))) {
231 *idx = BIOVEC_MAX_IDX;
232 goto fallback;
233 }
234 }
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return bvl;
237}
238
Kent Overstreet4254bba2012-09-06 15:35:00 -0700239static void __bio_free(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Kent Overstreet4254bba2012-09-06 15:35:00 -0700241 bio_disassociate_task(bio);
Jens Axboe992c5dd2007-07-18 13:18:08 +0200242
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200243 if (bio_integrity(bio))
Kent Overstreet1e2a410f2012-09-06 15:34:56 -0700244 bio_integrity_free(bio);
Kent Overstreet4254bba2012-09-06 15:35:00 -0700245}
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200246
Kent Overstreet4254bba2012-09-06 15:35:00 -0700247static void bio_free(struct bio *bio)
248{
249 struct bio_set *bs = bio->bi_pool;
250 void *p;
251
252 __bio_free(bio);
253
254 if (bs) {
Kent Overstreeta38352e2012-05-25 13:03:11 -0700255 if (bio_flagged(bio, BIO_OWNS_VEC))
Kent Overstreet9f060e22012-10-12 15:29:33 -0700256 bvec_free(bs->bvec_pool, bio->bi_io_vec, BIO_POOL_IDX(bio));
Kent Overstreet4254bba2012-09-06 15:35:00 -0700257
258 /*
259 * If we have front padding, adjust the bio pointer before freeing
260 */
261 p = bio;
Jens Axboebb799ca2008-12-10 15:35:05 +0100262 p -= bs->front_pad;
263
Kent Overstreet4254bba2012-09-06 15:35:00 -0700264 mempool_free(p, bs->bio_pool);
265 } else {
266 /* Bio was allocated by bio_kmalloc() */
267 kfree(bio);
268 }
Peter Osterlund36763472005-09-06 15:16:42 -0700269}
270
Arjan van de Ven858119e2006-01-14 13:20:43 -0800271void bio_init(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Jens Axboe2b94de52007-07-18 13:14:03 +0200273 memset(bio, 0, sizeof(*bio));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 bio->bi_flags = 1 << BIO_UPTODATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 atomic_set(&bio->bi_cnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
H Hartley Sweetena112a712009-09-26 16:19:21 +0200277EXPORT_SYMBOL(bio_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279/**
Kent Overstreetf44b48c2012-09-06 15:34:58 -0700280 * bio_reset - reinitialize a bio
281 * @bio: bio to reset
282 *
283 * Description:
284 * After calling bio_reset(), @bio will be in the same state as a freshly
285 * allocated bio returned bio bio_alloc_bioset() - the only fields that are
286 * preserved are the ones that are initialized by bio_alloc_bioset(). See
287 * comment in struct bio.
288 */
289void bio_reset(struct bio *bio)
290{
291 unsigned long flags = bio->bi_flags & (~0UL << BIO_RESET_BITS);
292
Kent Overstreet4254bba2012-09-06 15:35:00 -0700293 __bio_free(bio);
Kent Overstreetf44b48c2012-09-06 15:34:58 -0700294
295 memset(bio, 0, BIO_RESET_BYTES);
296 bio->bi_flags = flags|(1 << BIO_UPTODATE);
297}
298EXPORT_SYMBOL(bio_reset);
299
Kent Overstreetdf2cb6d2012-09-10 14:33:46 -0700300static void bio_alloc_rescue(struct work_struct *work)
301{
302 struct bio_set *bs = container_of(work, struct bio_set, rescue_work);
303 struct bio *bio;
304
305 while (1) {
306 spin_lock(&bs->rescue_lock);
307 bio = bio_list_pop(&bs->rescue_list);
308 spin_unlock(&bs->rescue_lock);
309
310 if (!bio)
311 break;
312
313 generic_make_request(bio);
314 }
315}
316
317static void punt_bios_to_rescuer(struct bio_set *bs)
318{
319 struct bio_list punt, nopunt;
320 struct bio *bio;
321
322 /*
323 * In order to guarantee forward progress we must punt only bios that
324 * were allocated from this bio_set; otherwise, if there was a bio on
325 * there for a stacking driver higher up in the stack, processing it
326 * could require allocating bios from this bio_set, and doing that from
327 * our own rescuer would be bad.
328 *
329 * Since bio lists are singly linked, pop them all instead of trying to
330 * remove from the middle of the list:
331 */
332
333 bio_list_init(&punt);
334 bio_list_init(&nopunt);
335
336 while ((bio = bio_list_pop(current->bio_list)))
337 bio_list_add(bio->bi_pool == bs ? &punt : &nopunt, bio);
338
339 *current->bio_list = nopunt;
340
341 spin_lock(&bs->rescue_lock);
342 bio_list_merge(&bs->rescue_list, &punt);
343 spin_unlock(&bs->rescue_lock);
344
345 queue_work(bs->rescue_workqueue, &bs->rescue_work);
346}
347
Kent Overstreetf44b48c2012-09-06 15:34:58 -0700348/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 * bio_alloc_bioset - allocate a bio for I/O
350 * @gfp_mask: the GFP_ mask given to the slab allocator
351 * @nr_iovecs: number of iovecs to pre-allocate
Jaak Ristiojadb18efa2010-01-15 12:05:07 +0200352 * @bs: the bio_set to allocate from.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 *
354 * Description:
Kent Overstreet3f86a822012-09-06 15:35:01 -0700355 * If @bs is NULL, uses kmalloc() to allocate the bio; else the allocation is
356 * backed by the @bs's mempool.
357 *
358 * When @bs is not NULL, if %__GFP_WAIT is set then bio_alloc will always be
359 * able to allocate a bio. This is due to the mempool guarantees. To make this
360 * work, callers must never allocate more than 1 bio at a time from this pool.
361 * Callers that need to allocate more than 1 bio must always submit the
362 * previously allocated bio for IO before attempting to allocate a new one.
363 * Failure to do so can cause deadlocks under memory pressure.
364 *
Kent Overstreetdf2cb6d2012-09-10 14:33:46 -0700365 * Note that when running under generic_make_request() (i.e. any block
366 * driver), bios are not submitted until after you return - see the code in
367 * generic_make_request() that converts recursion into iteration, to prevent
368 * stack overflows.
369 *
370 * This would normally mean allocating multiple bios under
371 * generic_make_request() would be susceptible to deadlocks, but we have
372 * deadlock avoidance code that resubmits any blocked bios from a rescuer
373 * thread.
374 *
375 * However, we do not guarantee forward progress for allocations from other
376 * mempools. Doing multiple allocations from the same mempool under
377 * generic_make_request() should be avoided - instead, use bio_set's front_pad
378 * for per bio allocations.
379 *
Kent Overstreet3f86a822012-09-06 15:35:01 -0700380 * RETURNS:
381 * Pointer to new bio on success, NULL on failure.
382 */
Al Virodd0fc662005-10-07 07:46:04 +0100383struct bio *bio_alloc_bioset(gfp_t gfp_mask, int nr_iovecs, struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Kent Overstreetdf2cb6d2012-09-10 14:33:46 -0700385 gfp_t saved_gfp = gfp_mask;
Kent Overstreet3f86a822012-09-06 15:35:01 -0700386 unsigned front_pad;
387 unsigned inline_vecs;
Tejun Heo451a9eb2009-04-15 19:50:51 +0200388 unsigned long idx = BIO_POOL_NONE;
Ingo Molnar34053972009-02-21 11:16:36 +0100389 struct bio_vec *bvl = NULL;
Tejun Heo451a9eb2009-04-15 19:50:51 +0200390 struct bio *bio;
391 void *p;
Jens Axboe0a0d96b2008-09-11 13:17:37 +0200392
Kent Overstreet3f86a822012-09-06 15:35:01 -0700393 if (!bs) {
394 if (nr_iovecs > UIO_MAXIOV)
395 return NULL;
396
397 p = kmalloc(sizeof(struct bio) +
398 nr_iovecs * sizeof(struct bio_vec),
399 gfp_mask);
400 front_pad = 0;
401 inline_vecs = nr_iovecs;
402 } else {
Kent Overstreetdf2cb6d2012-09-10 14:33:46 -0700403 /*
404 * generic_make_request() converts recursion to iteration; this
405 * means if we're running beneath it, any bios we allocate and
406 * submit will not be submitted (and thus freed) until after we
407 * return.
408 *
409 * This exposes us to a potential deadlock if we allocate
410 * multiple bios from the same bio_set() while running
411 * underneath generic_make_request(). If we were to allocate
412 * multiple bios (say a stacking block driver that was splitting
413 * bios), we would deadlock if we exhausted the mempool's
414 * reserve.
415 *
416 * We solve this, and guarantee forward progress, with a rescuer
417 * workqueue per bio_set. If we go to allocate and there are
418 * bios on current->bio_list, we first try the allocation
419 * without __GFP_WAIT; if that fails, we punt those bios we
420 * would be blocking to the rescuer workqueue before we retry
421 * with the original gfp_flags.
422 */
423
424 if (current->bio_list && !bio_list_empty(current->bio_list))
425 gfp_mask &= ~__GFP_WAIT;
426
Kent Overstreet3f86a822012-09-06 15:35:01 -0700427 p = mempool_alloc(bs->bio_pool, gfp_mask);
Kent Overstreetdf2cb6d2012-09-10 14:33:46 -0700428 if (!p && gfp_mask != saved_gfp) {
429 punt_bios_to_rescuer(bs);
430 gfp_mask = saved_gfp;
431 p = mempool_alloc(bs->bio_pool, gfp_mask);
432 }
433
Kent Overstreet3f86a822012-09-06 15:35:01 -0700434 front_pad = bs->front_pad;
435 inline_vecs = BIO_INLINE_VECS;
436 }
437
Tejun Heo451a9eb2009-04-15 19:50:51 +0200438 if (unlikely(!p))
439 return NULL;
Ingo Molnar34053972009-02-21 11:16:36 +0100440
Kent Overstreet3f86a822012-09-06 15:35:01 -0700441 bio = p + front_pad;
Ingo Molnar34053972009-02-21 11:16:36 +0100442 bio_init(bio);
443
Kent Overstreet3f86a822012-09-06 15:35:01 -0700444 if (nr_iovecs > inline_vecs) {
Kent Overstreet9f060e22012-10-12 15:29:33 -0700445 bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx, bs->bvec_pool);
Kent Overstreetdf2cb6d2012-09-10 14:33:46 -0700446 if (!bvl && gfp_mask != saved_gfp) {
447 punt_bios_to_rescuer(bs);
448 gfp_mask = saved_gfp;
Kent Overstreet9f060e22012-10-12 15:29:33 -0700449 bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx, bs->bvec_pool);
Kent Overstreetdf2cb6d2012-09-10 14:33:46 -0700450 }
451
Ingo Molnar34053972009-02-21 11:16:36 +0100452 if (unlikely(!bvl))
453 goto err_free;
Kent Overstreeta38352e2012-05-25 13:03:11 -0700454
455 bio->bi_flags |= 1 << BIO_OWNS_VEC;
Kent Overstreet3f86a822012-09-06 15:35:01 -0700456 } else if (nr_iovecs) {
457 bvl = bio->bi_inline_vecs;
Ingo Molnar34053972009-02-21 11:16:36 +0100458 }
Kent Overstreet3f86a822012-09-06 15:35:01 -0700459
460 bio->bi_pool = bs;
Ingo Molnar34053972009-02-21 11:16:36 +0100461 bio->bi_flags |= idx << BIO_POOL_OFFSET;
462 bio->bi_max_vecs = nr_iovecs;
Ingo Molnar34053972009-02-21 11:16:36 +0100463 bio->bi_io_vec = bvl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return bio;
Ingo Molnar34053972009-02-21 11:16:36 +0100465
466err_free:
Tejun Heo451a9eb2009-04-15 19:50:51 +0200467 mempool_free(p, bs->bio_pool);
Ingo Molnar34053972009-02-21 11:16:36 +0100468 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
H Hartley Sweetena112a712009-09-26 16:19:21 +0200470EXPORT_SYMBOL(bio_alloc_bioset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472void zero_fill_bio(struct bio *bio)
473{
474 unsigned long flags;
475 struct bio_vec *bv;
476 int i;
477
478 bio_for_each_segment(bv, bio, i) {
479 char *data = bvec_kmap_irq(bv, &flags);
480 memset(data, 0, bv->bv_len);
481 flush_dcache_page(bv->bv_page);
482 bvec_kunmap_irq(data, &flags);
483 }
484}
485EXPORT_SYMBOL(zero_fill_bio);
486
487/**
488 * bio_put - release a reference to a bio
489 * @bio: bio to release reference to
490 *
491 * Description:
492 * Put a reference to a &struct bio, either one you have gotten with
Alberto Bertogliad0bf112009-11-02 11:39:22 +0100493 * bio_alloc, bio_get or bio_clone. The last put of a bio will free it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 **/
495void bio_put(struct bio *bio)
496{
497 BIO_BUG_ON(!atomic_read(&bio->bi_cnt));
498
499 /*
500 * last put frees it
501 */
Kent Overstreet4254bba2012-09-06 15:35:00 -0700502 if (atomic_dec_and_test(&bio->bi_cnt))
503 bio_free(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
H Hartley Sweetena112a712009-09-26 16:19:21 +0200505EXPORT_SYMBOL(bio_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Jens Axboe165125e2007-07-24 09:28:11 +0200507inline int bio_phys_segments(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
510 blk_recount_segments(q, bio);
511
512 return bio->bi_phys_segments;
513}
H Hartley Sweetena112a712009-09-26 16:19:21 +0200514EXPORT_SYMBOL(bio_phys_segments);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516/**
517 * __bio_clone - clone a bio
518 * @bio: destination bio
519 * @bio_src: bio to clone
520 *
521 * Clone a &bio. Caller will own the returned bio, but not
522 * the actual data it points to. Reference count of returned
523 * bio will be one.
524 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800525void __bio_clone(struct bio *bio, struct bio *bio_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Andrew Mortone525e152005-08-07 09:42:12 -0700527 memcpy(bio->bi_io_vec, bio_src->bi_io_vec,
528 bio_src->bi_max_vecs * sizeof(struct bio_vec));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Jens Axboe5d840702008-01-25 12:44:44 +0100530 /*
531 * most users will be overriding ->bi_bdev with a new target,
532 * so we don't set nor calculate new physical/hw segment counts here
533 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 bio->bi_sector = bio_src->bi_sector;
535 bio->bi_bdev = bio_src->bi_bdev;
536 bio->bi_flags |= 1 << BIO_CLONED;
537 bio->bi_rw = bio_src->bi_rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 bio->bi_vcnt = bio_src->bi_vcnt;
539 bio->bi_size = bio_src->bi_size;
Andrew Mortona5453be2005-07-28 01:07:18 -0700540 bio->bi_idx = bio_src->bi_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
H Hartley Sweetena112a712009-09-26 16:19:21 +0200542EXPORT_SYMBOL(__bio_clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544/**
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700545 * bio_clone_bioset - clone a bio
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 * @bio: bio to clone
547 * @gfp_mask: allocation priority
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700548 * @bs: bio_set to allocate from
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 *
550 * Like __bio_clone, only also allocates the returned bio
551 */
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700552struct bio *bio_clone_bioset(struct bio *bio, gfp_t gfp_mask,
553 struct bio_set *bs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700555 struct bio *b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700557 b = bio_alloc_bioset(gfp_mask, bio->bi_max_vecs, bs);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200558 if (!b)
559 return NULL;
560
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200561 __bio_clone(b, bio);
562
563 if (bio_integrity(bio)) {
564 int ret;
565
Kent Overstreet1e2a410f2012-09-06 15:34:56 -0700566 ret = bio_integrity_clone(b, bio, gfp_mask);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200567
Li Zefan059ea332009-03-09 10:42:45 +0100568 if (ret < 0) {
569 bio_put(b);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200570 return NULL;
Li Zefan059ea332009-03-09 10:42:45 +0100571 }
Peter Osterlund36763472005-09-06 15:16:42 -0700572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 return b;
575}
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700576EXPORT_SYMBOL(bio_clone_bioset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578/**
579 * bio_get_nr_vecs - return approx number of vecs
580 * @bdev: I/O target
581 *
582 * Return the approximate number of pages we can send to this target.
583 * There's no guarantee that you will be able to fit this number of pages
584 * into a bio, it does not account for dynamic restrictions that vary
585 * on offset.
586 */
587int bio_get_nr_vecs(struct block_device *bdev)
588{
Jens Axboe165125e2007-07-24 09:28:11 +0200589 struct request_queue *q = bdev_get_queue(bdev);
Bernd Schubertf908ee92012-05-11 16:36:44 +0200590 int nr_pages;
591
592 nr_pages = min_t(unsigned,
Kent Overstreet5abebfd2012-02-08 22:07:18 +0100593 queue_max_segments(q),
594 queue_max_sectors(q) / (PAGE_SIZE >> 9) + 1);
Bernd Schubertf908ee92012-05-11 16:36:44 +0200595
596 return min_t(unsigned, nr_pages, BIO_MAX_PAGES);
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
H Hartley Sweetena112a712009-09-26 16:19:21 +0200599EXPORT_SYMBOL(bio_get_nr_vecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Jens Axboe165125e2007-07-24 09:28:11 +0200601static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
Mike Christiedefd94b2005-12-05 02:37:06 -0600602 *page, unsigned int len, unsigned int offset,
603 unsigned short max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
605 int retried_segments = 0;
606 struct bio_vec *bvec;
607
608 /*
609 * cloned bio must not modify vec list
610 */
611 if (unlikely(bio_flagged(bio, BIO_CLONED)))
612 return 0;
613
Jens Axboe80cfd542006-01-06 09:43:28 +0100614 if (((bio->bi_size + len) >> 9) > max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return 0;
616
Jens Axboe80cfd542006-01-06 09:43:28 +0100617 /*
618 * For filesystems with a blocksize smaller than the pagesize
619 * we will often be called with the same page as last time and
620 * a consecutive offset. Optimize this special case.
621 */
622 if (bio->bi_vcnt > 0) {
623 struct bio_vec *prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
624
625 if (page == prev->bv_page &&
626 offset == prev->bv_offset + prev->bv_len) {
Dmitry Monakhov1d616582010-01-27 22:44:36 +0300627 unsigned int prev_bv_len = prev->bv_len;
Jens Axboe80cfd542006-01-06 09:43:28 +0100628 prev->bv_len += len;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200629
630 if (q->merge_bvec_fn) {
631 struct bvec_merge_data bvm = {
Dmitry Monakhov1d616582010-01-27 22:44:36 +0300632 /* prev_bvec is already charged in
633 bi_size, discharge it in order to
634 simulate merging updated prev_bvec
635 as new bvec. */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200636 .bi_bdev = bio->bi_bdev,
637 .bi_sector = bio->bi_sector,
Dmitry Monakhov1d616582010-01-27 22:44:36 +0300638 .bi_size = bio->bi_size - prev_bv_len,
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200639 .bi_rw = bio->bi_rw,
640 };
641
Dmitry Monakhov8bf8c372010-03-03 06:28:06 +0300642 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len) {
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200643 prev->bv_len -= len;
644 return 0;
645 }
Jens Axboe80cfd542006-01-06 09:43:28 +0100646 }
647
648 goto done;
649 }
650 }
651
652 if (bio->bi_vcnt >= bio->bi_max_vecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return 0;
654
655 /*
656 * we might lose a segment or two here, but rather that than
657 * make this too complex.
658 */
659
Martin K. Petersen8a783622010-02-26 00:20:39 -0500660 while (bio->bi_phys_segments >= queue_max_segments(q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 if (retried_segments)
663 return 0;
664
665 retried_segments = 1;
666 blk_recount_segments(q, bio);
667 }
668
669 /*
670 * setup the new entry, we might clear it again later if we
671 * cannot add the page
672 */
673 bvec = &bio->bi_io_vec[bio->bi_vcnt];
674 bvec->bv_page = page;
675 bvec->bv_len = len;
676 bvec->bv_offset = offset;
677
678 /*
679 * if queue has other restrictions (eg varying max sector size
680 * depending on offset), it can specify a merge_bvec_fn in the
681 * queue to get further control
682 */
683 if (q->merge_bvec_fn) {
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200684 struct bvec_merge_data bvm = {
685 .bi_bdev = bio->bi_bdev,
686 .bi_sector = bio->bi_sector,
687 .bi_size = bio->bi_size,
688 .bi_rw = bio->bi_rw,
689 };
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 /*
692 * merge_bvec_fn() returns number of bytes it can accept
693 * at this offset
694 */
Dmitry Monakhov8bf8c372010-03-03 06:28:06 +0300695 if (q->merge_bvec_fn(q, &bvm, bvec) < bvec->bv_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 bvec->bv_page = NULL;
697 bvec->bv_len = 0;
698 bvec->bv_offset = 0;
699 return 0;
700 }
701 }
702
703 /* If we may be able to merge these biovecs, force a recount */
Mikulas Patockab8b3e162008-08-15 10:15:19 +0200704 if (bio->bi_vcnt && (BIOVEC_PHYS_MERGEABLE(bvec-1, bvec)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 bio->bi_flags &= ~(1 << BIO_SEG_VALID);
706
707 bio->bi_vcnt++;
708 bio->bi_phys_segments++;
Jens Axboe80cfd542006-01-06 09:43:28 +0100709 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 bio->bi_size += len;
711 return len;
712}
713
714/**
Mike Christie6e68af62005-11-11 05:30:27 -0600715 * bio_add_pc_page - attempt to add page to bio
Jens Axboefddfdea2006-01-31 15:24:34 +0100716 * @q: the target queue
Mike Christie6e68af62005-11-11 05:30:27 -0600717 * @bio: destination bio
718 * @page: page to add
719 * @len: vec entry length
720 * @offset: vec entry offset
721 *
722 * Attempt to add a page to the bio_vec maplist. This can fail for a
Andreas Gruenbacherc6428082011-05-27 14:52:09 +0200723 * number of reasons, such as the bio being full or target block device
724 * limitations. The target block device must allow bio's up to PAGE_SIZE,
725 * so it is always possible to add a single page to an empty bio.
726 *
727 * This should only be used by REQ_PC bios.
Mike Christie6e68af62005-11-11 05:30:27 -0600728 */
Jens Axboe165125e2007-07-24 09:28:11 +0200729int bio_add_pc_page(struct request_queue *q, struct bio *bio, struct page *page,
Mike Christie6e68af62005-11-11 05:30:27 -0600730 unsigned int len, unsigned int offset)
731{
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400732 return __bio_add_page(q, bio, page, len, offset,
733 queue_max_hw_sectors(q));
Mike Christie6e68af62005-11-11 05:30:27 -0600734}
H Hartley Sweetena112a712009-09-26 16:19:21 +0200735EXPORT_SYMBOL(bio_add_pc_page);
Mike Christie6e68af62005-11-11 05:30:27 -0600736
737/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 * bio_add_page - attempt to add page to bio
739 * @bio: destination bio
740 * @page: page to add
741 * @len: vec entry length
742 * @offset: vec entry offset
743 *
744 * Attempt to add a page to the bio_vec maplist. This can fail for a
Andreas Gruenbacherc6428082011-05-27 14:52:09 +0200745 * number of reasons, such as the bio being full or target block device
746 * limitations. The target block device must allow bio's up to PAGE_SIZE,
747 * so it is always possible to add a single page to an empty bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 */
749int bio_add_page(struct bio *bio, struct page *page, unsigned int len,
750 unsigned int offset)
751{
Mike Christiedefd94b2005-12-05 02:37:06 -0600752 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400753 return __bio_add_page(q, bio, page, len, offset, queue_max_sectors(q));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
H Hartley Sweetena112a712009-09-26 16:19:21 +0200755EXPORT_SYMBOL(bio_add_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Kent Overstreet9e882242012-09-10 14:41:12 -0700757struct submit_bio_ret {
758 struct completion event;
759 int error;
760};
761
762static void submit_bio_wait_endio(struct bio *bio, int error)
763{
764 struct submit_bio_ret *ret = bio->bi_private;
765
766 ret->error = error;
767 complete(&ret->event);
768}
769
770/**
771 * submit_bio_wait - submit a bio, and wait until it completes
772 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
773 * @bio: The &struct bio which describes the I/O
774 *
775 * Simple wrapper around submit_bio(). Returns 0 on success, or the error from
776 * bio_endio() on failure.
777 */
778int submit_bio_wait(int rw, struct bio *bio)
779{
780 struct submit_bio_ret ret;
781
782 rw |= REQ_SYNC;
783 init_completion(&ret.event);
784 bio->bi_private = &ret;
785 bio->bi_end_io = submit_bio_wait_endio;
786 submit_bio(rw, bio);
787 wait_for_completion(&ret.event);
788
789 return ret.error;
790}
791EXPORT_SYMBOL(submit_bio_wait);
792
Kent Overstreet054bdf62012-09-28 13:17:55 -0700793/**
794 * bio_advance - increment/complete a bio by some number of bytes
795 * @bio: bio to advance
796 * @bytes: number of bytes to complete
797 *
798 * This updates bi_sector, bi_size and bi_idx; if the number of bytes to
799 * complete doesn't align with a bvec boundary, then bv_len and bv_offset will
800 * be updated on the last bvec as well.
801 *
802 * @bio will then represent the remaining, uncompleted portion of the io.
803 */
804void bio_advance(struct bio *bio, unsigned bytes)
805{
806 if (bio_integrity(bio))
807 bio_integrity_advance(bio, bytes);
808
809 bio->bi_sector += bytes >> 9;
810 bio->bi_size -= bytes;
811
812 if (bio->bi_rw & BIO_NO_ADVANCE_ITER_MASK)
813 return;
814
815 while (bytes) {
816 if (unlikely(bio->bi_idx >= bio->bi_vcnt)) {
817 WARN_ONCE(1, "bio idx %d >= vcnt %d\n",
818 bio->bi_idx, bio->bi_vcnt);
819 break;
820 }
821
822 if (bytes >= bio_iovec(bio)->bv_len) {
823 bytes -= bio_iovec(bio)->bv_len;
824 bio->bi_idx++;
825 } else {
826 bio_iovec(bio)->bv_len -= bytes;
827 bio_iovec(bio)->bv_offset += bytes;
828 bytes = 0;
829 }
830 }
831}
832EXPORT_SYMBOL(bio_advance);
833
Kent Overstreet16ac3d62012-09-10 13:57:51 -0700834/**
Kent Overstreeta0787602012-09-10 14:03:28 -0700835 * bio_alloc_pages - allocates a single page for each bvec in a bio
836 * @bio: bio to allocate pages for
837 * @gfp_mask: flags for allocation
838 *
839 * Allocates pages up to @bio->bi_vcnt.
840 *
841 * Returns 0 on success, -ENOMEM on failure. On failure, any allocated pages are
842 * freed.
843 */
844int bio_alloc_pages(struct bio *bio, gfp_t gfp_mask)
845{
846 int i;
847 struct bio_vec *bv;
848
849 bio_for_each_segment_all(bv, bio, i) {
850 bv->bv_page = alloc_page(gfp_mask);
851 if (!bv->bv_page) {
852 while (--bv >= bio->bi_io_vec)
853 __free_page(bv->bv_page);
854 return -ENOMEM;
855 }
856 }
857
858 return 0;
859}
860EXPORT_SYMBOL(bio_alloc_pages);
861
862/**
Kent Overstreet16ac3d62012-09-10 13:57:51 -0700863 * bio_copy_data - copy contents of data buffers from one chain of bios to
864 * another
865 * @src: source bio list
866 * @dst: destination bio list
867 *
868 * If @src and @dst are single bios, bi_next must be NULL - otherwise, treats
869 * @src and @dst as linked lists of bios.
870 *
871 * Stops when it reaches the end of either @src or @dst - that is, copies
872 * min(src->bi_size, dst->bi_size) bytes (or the equivalent for lists of bios).
873 */
874void bio_copy_data(struct bio *dst, struct bio *src)
875{
876 struct bio_vec *src_bv, *dst_bv;
877 unsigned src_offset, dst_offset, bytes;
878 void *src_p, *dst_p;
879
880 src_bv = bio_iovec(src);
881 dst_bv = bio_iovec(dst);
882
883 src_offset = src_bv->bv_offset;
884 dst_offset = dst_bv->bv_offset;
885
886 while (1) {
887 if (src_offset == src_bv->bv_offset + src_bv->bv_len) {
888 src_bv++;
889 if (src_bv == bio_iovec_idx(src, src->bi_vcnt)) {
890 src = src->bi_next;
891 if (!src)
892 break;
893
894 src_bv = bio_iovec(src);
895 }
896
897 src_offset = src_bv->bv_offset;
898 }
899
900 if (dst_offset == dst_bv->bv_offset + dst_bv->bv_len) {
901 dst_bv++;
902 if (dst_bv == bio_iovec_idx(dst, dst->bi_vcnt)) {
903 dst = dst->bi_next;
904 if (!dst)
905 break;
906
907 dst_bv = bio_iovec(dst);
908 }
909
910 dst_offset = dst_bv->bv_offset;
911 }
912
913 bytes = min(dst_bv->bv_offset + dst_bv->bv_len - dst_offset,
914 src_bv->bv_offset + src_bv->bv_len - src_offset);
915
916 src_p = kmap_atomic(src_bv->bv_page);
917 dst_p = kmap_atomic(dst_bv->bv_page);
918
919 memcpy(dst_p + dst_bv->bv_offset,
920 src_p + src_bv->bv_offset,
921 bytes);
922
923 kunmap_atomic(dst_p);
924 kunmap_atomic(src_p);
925
926 src_offset += bytes;
927 dst_offset += bytes;
928 }
929}
930EXPORT_SYMBOL(bio_copy_data);
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932struct bio_map_data {
933 struct bio_vec *iovecs;
FUJITA Tomonoric5dec1c2008-04-11 12:56:49 +0200934 struct sg_iovec *sgvecs;
FUJITA Tomonori152e2832008-08-28 16:17:06 +0900935 int nr_sgvecs;
936 int is_our_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937};
938
FUJITA Tomonoric5dec1c2008-04-11 12:56:49 +0200939static void bio_set_map_data(struct bio_map_data *bmd, struct bio *bio,
FUJITA Tomonori152e2832008-08-28 16:17:06 +0900940 struct sg_iovec *iov, int iov_count,
941 int is_our_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
943 memcpy(bmd->iovecs, bio->bi_io_vec, sizeof(struct bio_vec) * bio->bi_vcnt);
FUJITA Tomonoric5dec1c2008-04-11 12:56:49 +0200944 memcpy(bmd->sgvecs, iov, sizeof(struct sg_iovec) * iov_count);
945 bmd->nr_sgvecs = iov_count;
FUJITA Tomonori152e2832008-08-28 16:17:06 +0900946 bmd->is_our_pages = is_our_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 bio->bi_private = bmd;
948}
949
950static void bio_free_map_data(struct bio_map_data *bmd)
951{
952 kfree(bmd->iovecs);
FUJITA Tomonoric5dec1c2008-04-11 12:56:49 +0200953 kfree(bmd->sgvecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 kfree(bmd);
955}
956
Dan Carpenter121f0992011-11-16 09:21:50 +0100957static struct bio_map_data *bio_alloc_map_data(int nr_segs,
958 unsigned int iov_count,
FUJITA Tomonori76029ff2008-08-25 20:36:08 +0200959 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Jens Axboef3f63c12010-10-29 11:46:56 -0600961 struct bio_map_data *bmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Jens Axboef3f63c12010-10-29 11:46:56 -0600963 if (iov_count > UIO_MAXIOV)
964 return NULL;
965
966 bmd = kmalloc(sizeof(*bmd), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (!bmd)
968 return NULL;
969
FUJITA Tomonori76029ff2008-08-25 20:36:08 +0200970 bmd->iovecs = kmalloc(sizeof(struct bio_vec) * nr_segs, gfp_mask);
FUJITA Tomonoric5dec1c2008-04-11 12:56:49 +0200971 if (!bmd->iovecs) {
972 kfree(bmd);
973 return NULL;
974 }
975
FUJITA Tomonori76029ff2008-08-25 20:36:08 +0200976 bmd->sgvecs = kmalloc(sizeof(struct sg_iovec) * iov_count, gfp_mask);
FUJITA Tomonoric5dec1c2008-04-11 12:56:49 +0200977 if (bmd->sgvecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return bmd;
979
FUJITA Tomonoric5dec1c2008-04-11 12:56:49 +0200980 kfree(bmd->iovecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 kfree(bmd);
982 return NULL;
983}
984
FUJITA Tomonoriaefcc282008-08-25 20:36:08 +0200985static int __bio_copy_iov(struct bio *bio, struct bio_vec *iovecs,
FUJITA Tomonoriecb554a2009-07-09 14:46:53 +0200986 struct sg_iovec *iov, int iov_count,
987 int to_user, int from_user, int do_free_page)
FUJITA Tomonoric5dec1c2008-04-11 12:56:49 +0200988{
989 int ret = 0, i;
990 struct bio_vec *bvec;
991 int iov_idx = 0;
992 unsigned int iov_off = 0;
FUJITA Tomonoric5dec1c2008-04-11 12:56:49 +0200993
Kent Overstreetd74c6d52013-02-06 12:23:11 -0800994 bio_for_each_segment_all(bvec, bio, i) {
FUJITA Tomonoric5dec1c2008-04-11 12:56:49 +0200995 char *bv_addr = page_address(bvec->bv_page);
FUJITA Tomonoriaefcc282008-08-25 20:36:08 +0200996 unsigned int bv_len = iovecs[i].bv_len;
FUJITA Tomonoric5dec1c2008-04-11 12:56:49 +0200997
998 while (bv_len && iov_idx < iov_count) {
999 unsigned int bytes;
Michal Simek0e0c6212009-06-10 12:57:07 -07001000 char __user *iov_addr;
FUJITA Tomonoric5dec1c2008-04-11 12:56:49 +02001001
1002 bytes = min_t(unsigned int,
1003 iov[iov_idx].iov_len - iov_off, bv_len);
1004 iov_addr = iov[iov_idx].iov_base + iov_off;
1005
1006 if (!ret) {
FUJITA Tomonoriecb554a2009-07-09 14:46:53 +02001007 if (to_user)
FUJITA Tomonoric5dec1c2008-04-11 12:56:49 +02001008 ret = copy_to_user(iov_addr, bv_addr,
1009 bytes);
1010
FUJITA Tomonoriecb554a2009-07-09 14:46:53 +02001011 if (from_user)
1012 ret = copy_from_user(bv_addr, iov_addr,
1013 bytes);
1014
FUJITA Tomonoric5dec1c2008-04-11 12:56:49 +02001015 if (ret)
1016 ret = -EFAULT;
1017 }
1018
1019 bv_len -= bytes;
1020 bv_addr += bytes;
1021 iov_addr += bytes;
1022 iov_off += bytes;
1023
1024 if (iov[iov_idx].iov_len == iov_off) {
1025 iov_idx++;
1026 iov_off = 0;
1027 }
1028 }
1029
FUJITA Tomonori152e2832008-08-28 16:17:06 +09001030 if (do_free_page)
FUJITA Tomonoric5dec1c2008-04-11 12:56:49 +02001031 __free_page(bvec->bv_page);
1032 }
1033
1034 return ret;
1035}
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037/**
1038 * bio_uncopy_user - finish previously mapped bio
1039 * @bio: bio being terminated
1040 *
1041 * Free pages allocated from bio_copy_user() and write back data
1042 * to user space in case of a read.
1043 */
1044int bio_uncopy_user(struct bio *bio)
1045{
1046 struct bio_map_data *bmd = bio->bi_private;
FUJITA Tomonori81882762008-09-02 16:20:19 +09001047 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
FUJITA Tomonori81882762008-09-02 16:20:19 +09001049 if (!bio_flagged(bio, BIO_NULL_MAPPED))
1050 ret = __bio_copy_iov(bio, bmd->iovecs, bmd->sgvecs,
FUJITA Tomonoriecb554a2009-07-09 14:46:53 +02001051 bmd->nr_sgvecs, bio_data_dir(bio) == READ,
1052 0, bmd->is_our_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 bio_free_map_data(bmd);
1054 bio_put(bio);
1055 return ret;
1056}
H Hartley Sweetena112a712009-09-26 16:19:21 +02001057EXPORT_SYMBOL(bio_uncopy_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059/**
FUJITA Tomonoric5dec1c2008-04-11 12:56:49 +02001060 * bio_copy_user_iov - copy user data to bio
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 * @q: destination block queue
FUJITA Tomonori152e2832008-08-28 16:17:06 +09001062 * @map_data: pointer to the rq_map_data holding pages (if necessary)
FUJITA Tomonoric5dec1c2008-04-11 12:56:49 +02001063 * @iov: the iovec.
1064 * @iov_count: number of elements in the iovec
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 * @write_to_vm: bool indicating writing to pages or not
FUJITA Tomonoria3bce902008-08-28 16:17:05 +09001066 * @gfp_mask: memory allocation flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 *
1068 * Prepares and returns a bio for indirect user io, bouncing data
1069 * to/from kernel pages as necessary. Must be paired with
1070 * call bio_uncopy_user() on io completion.
1071 */
FUJITA Tomonori152e2832008-08-28 16:17:06 +09001072struct bio *bio_copy_user_iov(struct request_queue *q,
1073 struct rq_map_data *map_data,
1074 struct sg_iovec *iov, int iov_count,
1075 int write_to_vm, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 struct bio_map_data *bmd;
1078 struct bio_vec *bvec;
1079 struct page *page;
1080 struct bio *bio;
1081 int i, ret;
FUJITA Tomonoric5dec1c2008-04-11 12:56:49 +02001082 int nr_pages = 0;
1083 unsigned int len = 0;
FUJITA Tomonori56c451f2008-12-18 14:49:37 +09001084 unsigned int offset = map_data ? map_data->offset & ~PAGE_MASK : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
FUJITA Tomonoric5dec1c2008-04-11 12:56:49 +02001086 for (i = 0; i < iov_count; i++) {
1087 unsigned long uaddr;
1088 unsigned long end;
1089 unsigned long start;
1090
1091 uaddr = (unsigned long)iov[i].iov_base;
1092 end = (uaddr + iov[i].iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1093 start = uaddr >> PAGE_SHIFT;
1094
Jens Axboecb4644c2010-11-10 14:36:25 +01001095 /*
1096 * Overflow, abort
1097 */
1098 if (end < start)
1099 return ERR_PTR(-EINVAL);
1100
FUJITA Tomonoric5dec1c2008-04-11 12:56:49 +02001101 nr_pages += end - start;
1102 len += iov[i].iov_len;
1103 }
1104
FUJITA Tomonori69838722009-04-28 20:24:29 +02001105 if (offset)
1106 nr_pages++;
1107
FUJITA Tomonoria3bce902008-08-28 16:17:05 +09001108 bmd = bio_alloc_map_data(nr_pages, iov_count, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if (!bmd)
1110 return ERR_PTR(-ENOMEM);
1111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 ret = -ENOMEM;
Tejun Heoa9e9dc22009-04-15 22:10:27 +09001113 bio = bio_kmalloc(gfp_mask, nr_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (!bio)
1115 goto out_bmd;
1116
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001117 if (!write_to_vm)
1118 bio->bi_rw |= REQ_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 ret = 0;
FUJITA Tomonori56c451f2008-12-18 14:49:37 +09001121
1122 if (map_data) {
FUJITA Tomonorie623ddb2008-12-18 14:49:36 +09001123 nr_pages = 1 << map_data->page_order;
FUJITA Tomonori56c451f2008-12-18 14:49:37 +09001124 i = map_data->offset / PAGE_SIZE;
1125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 while (len) {
FUJITA Tomonorie623ddb2008-12-18 14:49:36 +09001127 unsigned int bytes = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
FUJITA Tomonori56c451f2008-12-18 14:49:37 +09001129 bytes -= offset;
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if (bytes > len)
1132 bytes = len;
1133
FUJITA Tomonori152e2832008-08-28 16:17:06 +09001134 if (map_data) {
FUJITA Tomonorie623ddb2008-12-18 14:49:36 +09001135 if (i == map_data->nr_entries * nr_pages) {
FUJITA Tomonori152e2832008-08-28 16:17:06 +09001136 ret = -ENOMEM;
1137 break;
1138 }
FUJITA Tomonorie623ddb2008-12-18 14:49:36 +09001139
1140 page = map_data->pages[i / nr_pages];
1141 page += (i % nr_pages);
1142
1143 i++;
1144 } else {
FUJITA Tomonori152e2832008-08-28 16:17:06 +09001145 page = alloc_page(q->bounce_gfp | gfp_mask);
FUJITA Tomonorie623ddb2008-12-18 14:49:36 +09001146 if (!page) {
1147 ret = -ENOMEM;
1148 break;
1149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
1151
FUJITA Tomonori56c451f2008-12-18 14:49:37 +09001152 if (bio_add_pc_page(q, bio, page, bytes, offset) < bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
1155 len -= bytes;
FUJITA Tomonori56c451f2008-12-18 14:49:37 +09001156 offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
1158
1159 if (ret)
1160 goto cleanup;
1161
1162 /*
1163 * success
1164 */
FUJITA Tomonoriecb554a2009-07-09 14:46:53 +02001165 if ((!write_to_vm && (!map_data || !map_data->null_mapped)) ||
1166 (map_data && map_data->from_user)) {
1167 ret = __bio_copy_iov(bio, bio->bi_io_vec, iov, iov_count, 0, 1, 0);
FUJITA Tomonoric5dec1c2008-04-11 12:56:49 +02001168 if (ret)
1169 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 }
1171
FUJITA Tomonori152e2832008-08-28 16:17:06 +09001172 bio_set_map_data(bmd, bio, iov, iov_count, map_data ? 0 : 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 return bio;
1174cleanup:
FUJITA Tomonori152e2832008-08-28 16:17:06 +09001175 if (!map_data)
Kent Overstreetd74c6d52013-02-06 12:23:11 -08001176 bio_for_each_segment_all(bvec, bio, i)
FUJITA Tomonori152e2832008-08-28 16:17:06 +09001177 __free_page(bvec->bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 bio_put(bio);
1180out_bmd:
1181 bio_free_map_data(bmd);
1182 return ERR_PTR(ret);
1183}
1184
FUJITA Tomonoric5dec1c2008-04-11 12:56:49 +02001185/**
1186 * bio_copy_user - copy user data to bio
1187 * @q: destination block queue
FUJITA Tomonori152e2832008-08-28 16:17:06 +09001188 * @map_data: pointer to the rq_map_data holding pages (if necessary)
FUJITA Tomonoric5dec1c2008-04-11 12:56:49 +02001189 * @uaddr: start of user address
1190 * @len: length in bytes
1191 * @write_to_vm: bool indicating writing to pages or not
FUJITA Tomonoria3bce902008-08-28 16:17:05 +09001192 * @gfp_mask: memory allocation flags
FUJITA Tomonoric5dec1c2008-04-11 12:56:49 +02001193 *
1194 * Prepares and returns a bio for indirect user io, bouncing data
1195 * to/from kernel pages as necessary. Must be paired with
1196 * call bio_uncopy_user() on io completion.
1197 */
FUJITA Tomonori152e2832008-08-28 16:17:06 +09001198struct bio *bio_copy_user(struct request_queue *q, struct rq_map_data *map_data,
1199 unsigned long uaddr, unsigned int len,
1200 int write_to_vm, gfp_t gfp_mask)
FUJITA Tomonoric5dec1c2008-04-11 12:56:49 +02001201{
1202 struct sg_iovec iov;
1203
1204 iov.iov_base = (void __user *)uaddr;
1205 iov.iov_len = len;
1206
FUJITA Tomonori152e2832008-08-28 16:17:06 +09001207 return bio_copy_user_iov(q, map_data, &iov, 1, write_to_vm, gfp_mask);
FUJITA Tomonoric5dec1c2008-04-11 12:56:49 +02001208}
H Hartley Sweetena112a712009-09-26 16:19:21 +02001209EXPORT_SYMBOL(bio_copy_user);
FUJITA Tomonoric5dec1c2008-04-11 12:56:49 +02001210
Jens Axboe165125e2007-07-24 09:28:11 +02001211static struct bio *__bio_map_user_iov(struct request_queue *q,
James Bottomley f1970ba2005-06-20 14:06:52 +02001212 struct block_device *bdev,
1213 struct sg_iovec *iov, int iov_count,
FUJITA Tomonoria3bce902008-08-28 16:17:05 +09001214 int write_to_vm, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
James Bottomley f1970ba2005-06-20 14:06:52 +02001216 int i, j;
1217 int nr_pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 struct page **pages;
1219 struct bio *bio;
James Bottomley f1970ba2005-06-20 14:06:52 +02001220 int cur_page = 0;
1221 int ret, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
James Bottomley f1970ba2005-06-20 14:06:52 +02001223 for (i = 0; i < iov_count; i++) {
1224 unsigned long uaddr = (unsigned long)iov[i].iov_base;
1225 unsigned long len = iov[i].iov_len;
1226 unsigned long end = (uaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1227 unsigned long start = uaddr >> PAGE_SHIFT;
1228
Jens Axboecb4644c2010-11-10 14:36:25 +01001229 /*
1230 * Overflow, abort
1231 */
1232 if (end < start)
1233 return ERR_PTR(-EINVAL);
1234
James Bottomley f1970ba2005-06-20 14:06:52 +02001235 nr_pages += end - start;
1236 /*
Mike Christiead2d7222006-12-01 10:40:20 +01001237 * buffer must be aligned to at least hardsector size for now
James Bottomley f1970ba2005-06-20 14:06:52 +02001238 */
Mike Christiead2d7222006-12-01 10:40:20 +01001239 if (uaddr & queue_dma_alignment(q))
James Bottomley f1970ba2005-06-20 14:06:52 +02001240 return ERR_PTR(-EINVAL);
1241 }
1242
1243 if (!nr_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return ERR_PTR(-EINVAL);
1245
Tejun Heoa9e9dc22009-04-15 22:10:27 +09001246 bio = bio_kmalloc(gfp_mask, nr_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 if (!bio)
1248 return ERR_PTR(-ENOMEM);
1249
1250 ret = -ENOMEM;
FUJITA Tomonoria3bce902008-08-28 16:17:05 +09001251 pages = kcalloc(nr_pages, sizeof(struct page *), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 if (!pages)
1253 goto out;
1254
James Bottomley f1970ba2005-06-20 14:06:52 +02001255 for (i = 0; i < iov_count; i++) {
1256 unsigned long uaddr = (unsigned long)iov[i].iov_base;
1257 unsigned long len = iov[i].iov_len;
1258 unsigned long end = (uaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1259 unsigned long start = uaddr >> PAGE_SHIFT;
1260 const int local_nr_pages = end - start;
1261 const int page_limit = cur_page + local_nr_pages;
Jens Axboecb4644c2010-11-10 14:36:25 +01001262
Nick Pigginf5dd33c2008-07-25 19:45:25 -07001263 ret = get_user_pages_fast(uaddr, local_nr_pages,
1264 write_to_vm, &pages[cur_page]);
Jens Axboe99172152006-06-16 13:02:29 +02001265 if (ret < local_nr_pages) {
1266 ret = -EFAULT;
James Bottomley f1970ba2005-06-20 14:06:52 +02001267 goto out_unmap;
Jens Axboe99172152006-06-16 13:02:29 +02001268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
James Bottomley f1970ba2005-06-20 14:06:52 +02001270 offset = uaddr & ~PAGE_MASK;
1271 for (j = cur_page; j < page_limit; j++) {
1272 unsigned int bytes = PAGE_SIZE - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
James Bottomley f1970ba2005-06-20 14:06:52 +02001274 if (len <= 0)
1275 break;
1276
1277 if (bytes > len)
1278 bytes = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
James Bottomley f1970ba2005-06-20 14:06:52 +02001280 /*
1281 * sorry...
1282 */
Mike Christiedefd94b2005-12-05 02:37:06 -06001283 if (bio_add_pc_page(q, bio, pages[j], bytes, offset) <
1284 bytes)
James Bottomley f1970ba2005-06-20 14:06:52 +02001285 break;
1286
1287 len -= bytes;
1288 offset = 0;
1289 }
1290
1291 cur_page = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 /*
James Bottomley f1970ba2005-06-20 14:06:52 +02001293 * release the pages we didn't map into the bio, if any
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 */
James Bottomley f1970ba2005-06-20 14:06:52 +02001295 while (j < page_limit)
1296 page_cache_release(pages[j++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 kfree(pages);
1300
1301 /*
1302 * set data direction, and check if mapped pages need bouncing
1303 */
1304 if (!write_to_vm)
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001305 bio->bi_rw |= REQ_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
James Bottomley f1970ba2005-06-20 14:06:52 +02001307 bio->bi_bdev = bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 bio->bi_flags |= (1 << BIO_USER_MAPPED);
1309 return bio;
James Bottomley f1970ba2005-06-20 14:06:52 +02001310
1311 out_unmap:
1312 for (i = 0; i < nr_pages; i++) {
1313 if(!pages[i])
1314 break;
1315 page_cache_release(pages[i]);
1316 }
1317 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 kfree(pages);
1319 bio_put(bio);
1320 return ERR_PTR(ret);
1321}
1322
1323/**
1324 * bio_map_user - map user address into bio
Jens Axboe165125e2007-07-24 09:28:11 +02001325 * @q: the struct request_queue for the bio
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 * @bdev: destination block device
1327 * @uaddr: start of user address
1328 * @len: length in bytes
1329 * @write_to_vm: bool indicating writing to pages or not
FUJITA Tomonoria3bce902008-08-28 16:17:05 +09001330 * @gfp_mask: memory allocation flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 *
1332 * Map the user space address into a bio suitable for io to a block
1333 * device. Returns an error pointer in case of error.
1334 */
Jens Axboe165125e2007-07-24 09:28:11 +02001335struct bio *bio_map_user(struct request_queue *q, struct block_device *bdev,
FUJITA Tomonoria3bce902008-08-28 16:17:05 +09001336 unsigned long uaddr, unsigned int len, int write_to_vm,
1337 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
James Bottomley f1970ba2005-06-20 14:06:52 +02001339 struct sg_iovec iov;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
viro@ZenIV.linux.org.uk3f703532005-09-09 16:53:56 +01001341 iov.iov_base = (void __user *)uaddr;
James Bottomley f1970ba2005-06-20 14:06:52 +02001342 iov.iov_len = len;
1343
FUJITA Tomonoria3bce902008-08-28 16:17:05 +09001344 return bio_map_user_iov(q, bdev, &iov, 1, write_to_vm, gfp_mask);
James Bottomley f1970ba2005-06-20 14:06:52 +02001345}
H Hartley Sweetena112a712009-09-26 16:19:21 +02001346EXPORT_SYMBOL(bio_map_user);
James Bottomley f1970ba2005-06-20 14:06:52 +02001347
1348/**
1349 * bio_map_user_iov - map user sg_iovec table into bio
Jens Axboe165125e2007-07-24 09:28:11 +02001350 * @q: the struct request_queue for the bio
James Bottomley f1970ba2005-06-20 14:06:52 +02001351 * @bdev: destination block device
1352 * @iov: the iovec.
1353 * @iov_count: number of elements in the iovec
1354 * @write_to_vm: bool indicating writing to pages or not
FUJITA Tomonoria3bce902008-08-28 16:17:05 +09001355 * @gfp_mask: memory allocation flags
James Bottomley f1970ba2005-06-20 14:06:52 +02001356 *
1357 * Map the user space address into a bio suitable for io to a block
1358 * device. Returns an error pointer in case of error.
1359 */
Jens Axboe165125e2007-07-24 09:28:11 +02001360struct bio *bio_map_user_iov(struct request_queue *q, struct block_device *bdev,
James Bottomley f1970ba2005-06-20 14:06:52 +02001361 struct sg_iovec *iov, int iov_count,
FUJITA Tomonoria3bce902008-08-28 16:17:05 +09001362 int write_to_vm, gfp_t gfp_mask)
James Bottomley f1970ba2005-06-20 14:06:52 +02001363{
1364 struct bio *bio;
James Bottomley f1970ba2005-06-20 14:06:52 +02001365
FUJITA Tomonoria3bce902008-08-28 16:17:05 +09001366 bio = __bio_map_user_iov(q, bdev, iov, iov_count, write_to_vm,
1367 gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 if (IS_ERR(bio))
1369 return bio;
1370
1371 /*
1372 * subtle -- if __bio_map_user() ended up bouncing a bio,
1373 * it would normally disappear when its bi_end_io is run.
1374 * however, we need it for the unmap, so grab an extra
1375 * reference to it
1376 */
1377 bio_get(bio);
1378
Mike Christie0e75f902006-12-01 10:40:55 +01001379 return bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380}
1381
1382static void __bio_unmap_user(struct bio *bio)
1383{
1384 struct bio_vec *bvec;
1385 int i;
1386
1387 /*
1388 * make sure we dirty pages we wrote to
1389 */
Kent Overstreetd74c6d52013-02-06 12:23:11 -08001390 bio_for_each_segment_all(bvec, bio, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 if (bio_data_dir(bio) == READ)
1392 set_page_dirty_lock(bvec->bv_page);
1393
1394 page_cache_release(bvec->bv_page);
1395 }
1396
1397 bio_put(bio);
1398}
1399
1400/**
1401 * bio_unmap_user - unmap a bio
1402 * @bio: the bio being unmapped
1403 *
1404 * Unmap a bio previously mapped by bio_map_user(). Must be called with
1405 * a process context.
1406 *
1407 * bio_unmap_user() may sleep.
1408 */
1409void bio_unmap_user(struct bio *bio)
1410{
1411 __bio_unmap_user(bio);
1412 bio_put(bio);
1413}
H Hartley Sweetena112a712009-09-26 16:19:21 +02001414EXPORT_SYMBOL(bio_unmap_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
NeilBrown6712ecf2007-09-27 12:47:43 +02001416static void bio_map_kern_endio(struct bio *bio, int err)
Jens Axboeb8238252005-06-20 14:05:27 +02001417{
Jens Axboeb8238252005-06-20 14:05:27 +02001418 bio_put(bio);
Jens Axboeb8238252005-06-20 14:05:27 +02001419}
1420
Jens Axboe165125e2007-07-24 09:28:11 +02001421static struct bio *__bio_map_kern(struct request_queue *q, void *data,
Al Viro27496a82005-10-21 03:20:48 -04001422 unsigned int len, gfp_t gfp_mask)
Mike Christie df46b9a2005-06-20 14:04:44 +02001423{
1424 unsigned long kaddr = (unsigned long)data;
1425 unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1426 unsigned long start = kaddr >> PAGE_SHIFT;
1427 const int nr_pages = end - start;
1428 int offset, i;
1429 struct bio *bio;
1430
Tejun Heoa9e9dc22009-04-15 22:10:27 +09001431 bio = bio_kmalloc(gfp_mask, nr_pages);
Mike Christie df46b9a2005-06-20 14:04:44 +02001432 if (!bio)
1433 return ERR_PTR(-ENOMEM);
1434
1435 offset = offset_in_page(kaddr);
1436 for (i = 0; i < nr_pages; i++) {
1437 unsigned int bytes = PAGE_SIZE - offset;
1438
1439 if (len <= 0)
1440 break;
1441
1442 if (bytes > len)
1443 bytes = len;
1444
Mike Christiedefd94b2005-12-05 02:37:06 -06001445 if (bio_add_pc_page(q, bio, virt_to_page(data), bytes,
1446 offset) < bytes)
Mike Christie df46b9a2005-06-20 14:04:44 +02001447 break;
1448
1449 data += bytes;
1450 len -= bytes;
1451 offset = 0;
1452 }
1453
Jens Axboeb8238252005-06-20 14:05:27 +02001454 bio->bi_end_io = bio_map_kern_endio;
Mike Christie df46b9a2005-06-20 14:04:44 +02001455 return bio;
1456}
1457
1458/**
1459 * bio_map_kern - map kernel address into bio
Jens Axboe165125e2007-07-24 09:28:11 +02001460 * @q: the struct request_queue for the bio
Mike Christie df46b9a2005-06-20 14:04:44 +02001461 * @data: pointer to buffer to map
1462 * @len: length in bytes
1463 * @gfp_mask: allocation flags for bio allocation
1464 *
1465 * Map the kernel address into a bio suitable for io to a block
1466 * device. Returns an error pointer in case of error.
1467 */
Jens Axboe165125e2007-07-24 09:28:11 +02001468struct bio *bio_map_kern(struct request_queue *q, void *data, unsigned int len,
Al Viro27496a82005-10-21 03:20:48 -04001469 gfp_t gfp_mask)
Mike Christie df46b9a2005-06-20 14:04:44 +02001470{
1471 struct bio *bio;
1472
1473 bio = __bio_map_kern(q, data, len, gfp_mask);
1474 if (IS_ERR(bio))
1475 return bio;
1476
1477 if (bio->bi_size == len)
1478 return bio;
1479
1480 /*
1481 * Don't support partial mappings.
1482 */
1483 bio_put(bio);
1484 return ERR_PTR(-EINVAL);
1485}
H Hartley Sweetena112a712009-09-26 16:19:21 +02001486EXPORT_SYMBOL(bio_map_kern);
Mike Christie df46b9a2005-06-20 14:04:44 +02001487
FUJITA Tomonori68154e92008-04-25 12:47:50 +02001488static void bio_copy_kern_endio(struct bio *bio, int err)
1489{
1490 struct bio_vec *bvec;
1491 const int read = bio_data_dir(bio) == READ;
FUJITA Tomonori76029ff2008-08-25 20:36:08 +02001492 struct bio_map_data *bmd = bio->bi_private;
FUJITA Tomonori68154e92008-04-25 12:47:50 +02001493 int i;
FUJITA Tomonori76029ff2008-08-25 20:36:08 +02001494 char *p = bmd->sgvecs[0].iov_base;
FUJITA Tomonori68154e92008-04-25 12:47:50 +02001495
Kent Overstreetd74c6d52013-02-06 12:23:11 -08001496 bio_for_each_segment_all(bvec, bio, i) {
FUJITA Tomonori68154e92008-04-25 12:47:50 +02001497 char *addr = page_address(bvec->bv_page);
FUJITA Tomonori76029ff2008-08-25 20:36:08 +02001498 int len = bmd->iovecs[i].bv_len;
FUJITA Tomonori68154e92008-04-25 12:47:50 +02001499
Tejun Heo4fc981e2009-05-19 18:33:06 +09001500 if (read)
FUJITA Tomonori76029ff2008-08-25 20:36:08 +02001501 memcpy(p, addr, len);
FUJITA Tomonori68154e92008-04-25 12:47:50 +02001502
1503 __free_page(bvec->bv_page);
FUJITA Tomonori76029ff2008-08-25 20:36:08 +02001504 p += len;
FUJITA Tomonori68154e92008-04-25 12:47:50 +02001505 }
1506
FUJITA Tomonori76029ff2008-08-25 20:36:08 +02001507 bio_free_map_data(bmd);
FUJITA Tomonori68154e92008-04-25 12:47:50 +02001508 bio_put(bio);
1509}
1510
1511/**
1512 * bio_copy_kern - copy kernel address into bio
1513 * @q: the struct request_queue for the bio
1514 * @data: pointer to buffer to copy
1515 * @len: length in bytes
1516 * @gfp_mask: allocation flags for bio and page allocation
Randy Dunlapffee0252008-04-30 09:08:54 +02001517 * @reading: data direction is READ
FUJITA Tomonori68154e92008-04-25 12:47:50 +02001518 *
1519 * copy the kernel address into a bio suitable for io to a block
1520 * device. Returns an error pointer in case of error.
1521 */
1522struct bio *bio_copy_kern(struct request_queue *q, void *data, unsigned int len,
1523 gfp_t gfp_mask, int reading)
1524{
FUJITA Tomonori68154e92008-04-25 12:47:50 +02001525 struct bio *bio;
1526 struct bio_vec *bvec;
FUJITA Tomonori4d8ab622008-08-28 15:05:57 +09001527 int i;
FUJITA Tomonori68154e92008-04-25 12:47:50 +02001528
FUJITA Tomonori4d8ab622008-08-28 15:05:57 +09001529 bio = bio_copy_user(q, NULL, (unsigned long)data, len, 1, gfp_mask);
1530 if (IS_ERR(bio))
1531 return bio;
FUJITA Tomonori68154e92008-04-25 12:47:50 +02001532
1533 if (!reading) {
1534 void *p = data;
1535
Kent Overstreetd74c6d52013-02-06 12:23:11 -08001536 bio_for_each_segment_all(bvec, bio, i) {
FUJITA Tomonori68154e92008-04-25 12:47:50 +02001537 char *addr = page_address(bvec->bv_page);
1538
1539 memcpy(addr, p, bvec->bv_len);
1540 p += bvec->bv_len;
1541 }
1542 }
1543
FUJITA Tomonori68154e92008-04-25 12:47:50 +02001544 bio->bi_end_io = bio_copy_kern_endio;
FUJITA Tomonori76029ff2008-08-25 20:36:08 +02001545
FUJITA Tomonori68154e92008-04-25 12:47:50 +02001546 return bio;
FUJITA Tomonori68154e92008-04-25 12:47:50 +02001547}
H Hartley Sweetena112a712009-09-26 16:19:21 +02001548EXPORT_SYMBOL(bio_copy_kern);
FUJITA Tomonori68154e92008-04-25 12:47:50 +02001549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550/*
1551 * bio_set_pages_dirty() and bio_check_pages_dirty() are support functions
1552 * for performing direct-IO in BIOs.
1553 *
1554 * The problem is that we cannot run set_page_dirty() from interrupt context
1555 * because the required locks are not interrupt-safe. So what we can do is to
1556 * mark the pages dirty _before_ performing IO. And in interrupt context,
1557 * check that the pages are still dirty. If so, fine. If not, redirty them
1558 * in process context.
1559 *
1560 * We special-case compound pages here: normally this means reads into hugetlb
1561 * pages. The logic in here doesn't really work right for compound pages
1562 * because the VM does not uniformly chase down the head page in all cases.
1563 * But dirtiness of compound pages is pretty meaningless anyway: the VM doesn't
1564 * handle them at all. So we skip compound pages here at an early stage.
1565 *
1566 * Note that this code is very hard to test under normal circumstances because
1567 * direct-io pins the pages with get_user_pages(). This makes
1568 * is_page_cache_freeable return false, and the VM will not clean the pages.
Artem Bityutskiy0d5c3eb2012-07-25 18:12:08 +03001569 * But other code (eg, flusher threads) could clean the pages if they are mapped
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 * pagecache.
1571 *
1572 * Simply disabling the call to bio_set_pages_dirty() is a good way to test the
1573 * deferred bio dirtying paths.
1574 */
1575
1576/*
1577 * bio_set_pages_dirty() will mark all the bio's pages as dirty.
1578 */
1579void bio_set_pages_dirty(struct bio *bio)
1580{
Kent Overstreetcb34e052012-09-05 15:22:02 -07001581 struct bio_vec *bvec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 int i;
1583
Kent Overstreetcb34e052012-09-05 15:22:02 -07001584 bio_for_each_segment_all(bvec, bio, i) {
1585 struct page *page = bvec->bv_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 if (page && !PageCompound(page))
1588 set_page_dirty_lock(page);
1589 }
1590}
1591
Adrian Bunk86b6c7a2008-02-18 13:48:32 +01001592static void bio_release_pages(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
Kent Overstreetcb34e052012-09-05 15:22:02 -07001594 struct bio_vec *bvec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 int i;
1596
Kent Overstreetcb34e052012-09-05 15:22:02 -07001597 bio_for_each_segment_all(bvec, bio, i) {
1598 struct page *page = bvec->bv_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600 if (page)
1601 put_page(page);
1602 }
1603}
1604
1605/*
1606 * bio_check_pages_dirty() will check that all the BIO's pages are still dirty.
1607 * If they are, then fine. If, however, some pages are clean then they must
1608 * have been written out during the direct-IO read. So we take another ref on
1609 * the BIO and the offending pages and re-dirty the pages in process context.
1610 *
1611 * It is expected that bio_check_pages_dirty() will wholly own the BIO from
1612 * here on. It will run one page_cache_release() against each page and will
1613 * run one bio_put() against the BIO.
1614 */
1615
David Howells65f27f32006-11-22 14:55:48 +00001616static void bio_dirty_fn(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
David Howells65f27f32006-11-22 14:55:48 +00001618static DECLARE_WORK(bio_dirty_work, bio_dirty_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619static DEFINE_SPINLOCK(bio_dirty_lock);
1620static struct bio *bio_dirty_list;
1621
1622/*
1623 * This runs in process context
1624 */
David Howells65f27f32006-11-22 14:55:48 +00001625static void bio_dirty_fn(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
1627 unsigned long flags;
1628 struct bio *bio;
1629
1630 spin_lock_irqsave(&bio_dirty_lock, flags);
1631 bio = bio_dirty_list;
1632 bio_dirty_list = NULL;
1633 spin_unlock_irqrestore(&bio_dirty_lock, flags);
1634
1635 while (bio) {
1636 struct bio *next = bio->bi_private;
1637
1638 bio_set_pages_dirty(bio);
1639 bio_release_pages(bio);
1640 bio_put(bio);
1641 bio = next;
1642 }
1643}
1644
1645void bio_check_pages_dirty(struct bio *bio)
1646{
Kent Overstreetcb34e052012-09-05 15:22:02 -07001647 struct bio_vec *bvec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 int nr_clean_pages = 0;
1649 int i;
1650
Kent Overstreetcb34e052012-09-05 15:22:02 -07001651 bio_for_each_segment_all(bvec, bio, i) {
1652 struct page *page = bvec->bv_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
1654 if (PageDirty(page) || PageCompound(page)) {
1655 page_cache_release(page);
Kent Overstreetcb34e052012-09-05 15:22:02 -07001656 bvec->bv_page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 } else {
1658 nr_clean_pages++;
1659 }
1660 }
1661
1662 if (nr_clean_pages) {
1663 unsigned long flags;
1664
1665 spin_lock_irqsave(&bio_dirty_lock, flags);
1666 bio->bi_private = bio_dirty_list;
1667 bio_dirty_list = bio;
1668 spin_unlock_irqrestore(&bio_dirty_lock, flags);
1669 schedule_work(&bio_dirty_work);
1670 } else {
1671 bio_put(bio);
1672 }
1673}
1674
Ilya Loginov2d4dc892009-11-26 09:16:19 +01001675#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
1676void bio_flush_dcache_pages(struct bio *bi)
1677{
1678 int i;
1679 struct bio_vec *bvec;
1680
1681 bio_for_each_segment(bvec, bi, i)
1682 flush_dcache_page(bvec->bv_page);
1683}
1684EXPORT_SYMBOL(bio_flush_dcache_pages);
1685#endif
1686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687/**
1688 * bio_endio - end I/O on a bio
1689 * @bio: bio
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 * @error: error, if any
1691 *
1692 * Description:
NeilBrown6712ecf2007-09-27 12:47:43 +02001693 * bio_endio() will end I/O on the whole bio. bio_endio() is the
NeilBrown5bb23a62007-09-27 12:46:13 +02001694 * preferred way to end I/O on a bio, it takes care of clearing
1695 * BIO_UPTODATE on error. @error is 0 on success, and and one of the
1696 * established -Exxxx (-EIO, for instance) error values in case
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001697 * something went wrong. No one should call bi_end_io() directly on a
NeilBrown5bb23a62007-09-27 12:46:13 +02001698 * bio unless they own it and thus know that it has an end_io
1699 * function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 **/
NeilBrown6712ecf2007-09-27 12:47:43 +02001701void bio_endio(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
1703 if (error)
1704 clear_bit(BIO_UPTODATE, &bio->bi_flags);
NeilBrown9cc54d42007-09-27 12:46:12 +02001705 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
1706 error = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Tejun Heo3a366e62013-01-11 13:06:33 -08001708 trace_block_bio_complete(bio, error);
1709
NeilBrown5bb23a62007-09-27 12:46:13 +02001710 if (bio->bi_end_io)
NeilBrown6712ecf2007-09-27 12:47:43 +02001711 bio->bi_end_io(bio, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712}
H Hartley Sweetena112a712009-09-26 16:19:21 +02001713EXPORT_SYMBOL(bio_endio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
1715void bio_pair_release(struct bio_pair *bp)
1716{
1717 if (atomic_dec_and_test(&bp->cnt)) {
1718 struct bio *master = bp->bio1.bi_private;
1719
NeilBrown6712ecf2007-09-27 12:47:43 +02001720 bio_endio(master, bp->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 mempool_free(bp, bp->bio2.bi_private);
1722 }
1723}
H Hartley Sweetena112a712009-09-26 16:19:21 +02001724EXPORT_SYMBOL(bio_pair_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
NeilBrown6712ecf2007-09-27 12:47:43 +02001726static void bio_pair_end_1(struct bio *bi, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
1728 struct bio_pair *bp = container_of(bi, struct bio_pair, bio1);
1729
1730 if (err)
1731 bp->error = err;
1732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 bio_pair_release(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734}
1735
NeilBrown6712ecf2007-09-27 12:47:43 +02001736static void bio_pair_end_2(struct bio *bi, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
1738 struct bio_pair *bp = container_of(bi, struct bio_pair, bio2);
1739
1740 if (err)
1741 bp->error = err;
1742
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 bio_pair_release(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744}
1745
1746/*
Alberto Bertoglic7eee1b2009-01-25 23:36:14 -02001747 * split a bio - only worry about a bio with a single page in its iovec
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 */
Denis ChengRq6feef532008-10-09 08:57:05 +02001749struct bio_pair *bio_split(struct bio *bi, int first_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
Denis ChengRq6feef532008-10-09 08:57:05 +02001751 struct bio_pair *bp = mempool_alloc(bio_split_pool, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753 if (!bp)
1754 return bp;
1755
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001756 trace_block_split(bdev_get_queue(bi->bi_bdev), bi,
Jens Axboe2056a782006-03-23 20:00:26 +01001757 bi->bi_sector + first_sectors);
1758
Kent Overstreet5b836362012-09-04 15:20:38 -07001759 BUG_ON(bio_segments(bi) > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 atomic_set(&bp->cnt, 3);
1761 bp->error = 0;
1762 bp->bio1 = *bi;
1763 bp->bio2 = *bi;
1764 bp->bio2.bi_sector += first_sectors;
1765 bp->bio2.bi_size -= first_sectors << 9;
1766 bp->bio1.bi_size = first_sectors << 9;
1767
Shaohua Li02f39392012-09-28 10:38:48 +02001768 if (bi->bi_vcnt != 0) {
Kent Overstreet5b836362012-09-04 15:20:38 -07001769 bp->bv1 = *bio_iovec(bi);
1770 bp->bv2 = *bio_iovec(bi);
Martin K. Petersen4363ac72012-09-18 12:19:27 -04001771
Shaohua Li02f39392012-09-28 10:38:48 +02001772 if (bio_is_rw(bi)) {
1773 bp->bv2.bv_offset += first_sectors << 9;
1774 bp->bv2.bv_len -= first_sectors << 9;
1775 bp->bv1.bv_len = first_sectors << 9;
1776 }
1777
1778 bp->bio1.bi_io_vec = &bp->bv1;
1779 bp->bio2.bi_io_vec = &bp->bv2;
1780
1781 bp->bio1.bi_max_vecs = 1;
1782 bp->bio2.bi_max_vecs = 1;
Martin K. Petersen4363ac72012-09-18 12:19:27 -04001783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 bp->bio1.bi_end_io = bio_pair_end_1;
1786 bp->bio2.bi_end_io = bio_pair_end_2;
1787
1788 bp->bio1.bi_private = bi;
Denis ChengRq6feef532008-10-09 08:57:05 +02001789 bp->bio2.bi_private = bio_split_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +02001791 if (bio_integrity(bi))
1792 bio_integrity_split(bi, bp, first_sectors);
1793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 return bp;
1795}
H Hartley Sweetena112a712009-09-26 16:19:21 +02001796EXPORT_SYMBOL(bio_split);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Martin K. Petersenad3316b2008-10-01 22:42:53 -04001798/**
1799 * bio_sector_offset - Find hardware sector offset in bio
1800 * @bio: bio to inspect
1801 * @index: bio_vec index
1802 * @offset: offset in bv_page
1803 *
1804 * Return the number of hardware sectors between beginning of bio
1805 * and an end point indicated by a bio_vec index and an offset
1806 * within that vector's page.
1807 */
1808sector_t bio_sector_offset(struct bio *bio, unsigned short index,
1809 unsigned int offset)
1810{
Martin K. Petersene1defc42009-05-22 17:17:49 -04001811 unsigned int sector_sz;
Martin K. Petersenad3316b2008-10-01 22:42:53 -04001812 struct bio_vec *bv;
1813 sector_t sectors;
1814 int i;
1815
Martin K. Petersene1defc42009-05-22 17:17:49 -04001816 sector_sz = queue_logical_block_size(bio->bi_bdev->bd_disk->queue);
Martin K. Petersenad3316b2008-10-01 22:42:53 -04001817 sectors = 0;
1818
1819 if (index >= bio->bi_idx)
1820 index = bio->bi_vcnt - 1;
1821
Kent Overstreetd74c6d52013-02-06 12:23:11 -08001822 bio_for_each_segment_all(bv, bio, i) {
Martin K. Petersenad3316b2008-10-01 22:42:53 -04001823 if (i == index) {
1824 if (offset > bv->bv_offset)
1825 sectors += (offset - bv->bv_offset) / sector_sz;
1826 break;
1827 }
1828
1829 sectors += bv->bv_len / sector_sz;
1830 }
1831
1832 return sectors;
1833}
1834EXPORT_SYMBOL(bio_sector_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
1836/*
1837 * create memory pools for biovec's in a bio_set.
1838 * use the global biovec slabs created for general use.
1839 */
Kent Overstreet9f060e22012-10-12 15:29:33 -07001840mempool_t *biovec_create_pool(struct bio_set *bs, int pool_entries)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
Jens Axboe7ff93452008-12-11 11:53:43 +01001842 struct biovec_slab *bp = bvec_slabs + BIOVEC_MAX_IDX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
Kent Overstreet9f060e22012-10-12 15:29:33 -07001844 return mempool_create_slab_pool(pool_entries, bp->slab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845}
1846
1847void bioset_free(struct bio_set *bs)
1848{
Kent Overstreetdf2cb6d2012-09-10 14:33:46 -07001849 if (bs->rescue_workqueue)
1850 destroy_workqueue(bs->rescue_workqueue);
1851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 if (bs->bio_pool)
1853 mempool_destroy(bs->bio_pool);
1854
Kent Overstreet9f060e22012-10-12 15:29:33 -07001855 if (bs->bvec_pool)
1856 mempool_destroy(bs->bvec_pool);
1857
Martin K. Petersen7878cba2009-06-26 15:37:49 +02001858 bioset_integrity_free(bs);
Jens Axboebb799ca2008-12-10 15:35:05 +01001859 bio_put_slab(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
1861 kfree(bs);
1862}
H Hartley Sweetena112a712009-09-26 16:19:21 +02001863EXPORT_SYMBOL(bioset_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Jens Axboebb799ca2008-12-10 15:35:05 +01001865/**
1866 * bioset_create - Create a bio_set
1867 * @pool_size: Number of bio and bio_vecs to cache in the mempool
1868 * @front_pad: Number of bytes to allocate in front of the returned bio
1869 *
1870 * Description:
1871 * Set up a bio_set to be used with @bio_alloc_bioset. Allows the caller
1872 * to ask for a number of bytes to be allocated in front of the bio.
1873 * Front pad allocation is useful for embedding the bio inside
1874 * another structure, to avoid allocating extra data to go with the bio.
1875 * Note that the bio must be embedded at the END of that structure always,
1876 * or things will break badly.
1877 */
1878struct bio_set *bioset_create(unsigned int pool_size, unsigned int front_pad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
Jens Axboe392ddc32008-12-23 12:42:54 +01001880 unsigned int back_pad = BIO_INLINE_VECS * sizeof(struct bio_vec);
Jens Axboe1b434492008-10-22 20:32:58 +02001881 struct bio_set *bs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Jens Axboe1b434492008-10-22 20:32:58 +02001883 bs = kzalloc(sizeof(*bs), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 if (!bs)
1885 return NULL;
1886
Jens Axboebb799ca2008-12-10 15:35:05 +01001887 bs->front_pad = front_pad;
Jens Axboe1b434492008-10-22 20:32:58 +02001888
Kent Overstreetdf2cb6d2012-09-10 14:33:46 -07001889 spin_lock_init(&bs->rescue_lock);
1890 bio_list_init(&bs->rescue_list);
1891 INIT_WORK(&bs->rescue_work, bio_alloc_rescue);
1892
Jens Axboe392ddc32008-12-23 12:42:54 +01001893 bs->bio_slab = bio_find_or_create_slab(front_pad + back_pad);
Jens Axboebb799ca2008-12-10 15:35:05 +01001894 if (!bs->bio_slab) {
1895 kfree(bs);
1896 return NULL;
1897 }
1898
1899 bs->bio_pool = mempool_create_slab_pool(pool_size, bs->bio_slab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 if (!bs->bio_pool)
1901 goto bad;
1902
Kent Overstreet9f060e22012-10-12 15:29:33 -07001903 bs->bvec_pool = biovec_create_pool(bs, pool_size);
1904 if (!bs->bvec_pool)
Kent Overstreetdf2cb6d2012-09-10 14:33:46 -07001905 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Kent Overstreetdf2cb6d2012-09-10 14:33:46 -07001907 bs->rescue_workqueue = alloc_workqueue("bioset", WQ_MEM_RECLAIM, 0);
1908 if (!bs->rescue_workqueue)
1909 goto bad;
1910
1911 return bs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912bad:
1913 bioset_free(bs);
1914 return NULL;
1915}
H Hartley Sweetena112a712009-09-26 16:19:21 +02001916EXPORT_SYMBOL(bioset_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Tejun Heo852c7882012-03-05 13:15:27 -08001918#ifdef CONFIG_BLK_CGROUP
1919/**
1920 * bio_associate_current - associate a bio with %current
1921 * @bio: target bio
1922 *
1923 * Associate @bio with %current if it hasn't been associated yet. Block
1924 * layer will treat @bio as if it were issued by %current no matter which
1925 * task actually issues it.
1926 *
1927 * This function takes an extra reference of @task's io_context and blkcg
1928 * which will be put when @bio is released. The caller must own @bio,
1929 * ensure %current->io_context exists, and is responsible for synchronizing
1930 * calls to this function.
1931 */
1932int bio_associate_current(struct bio *bio)
1933{
1934 struct io_context *ioc;
1935 struct cgroup_subsys_state *css;
1936
1937 if (bio->bi_ioc)
1938 return -EBUSY;
1939
1940 ioc = current->io_context;
1941 if (!ioc)
1942 return -ENOENT;
1943
1944 /* acquire active ref on @ioc and associate */
1945 get_io_context_active(ioc);
1946 bio->bi_ioc = ioc;
1947
1948 /* associate blkcg if exists */
1949 rcu_read_lock();
1950 css = task_subsys_state(current, blkio_subsys_id);
1951 if (css && css_tryget(css))
1952 bio->bi_css = css;
1953 rcu_read_unlock();
1954
1955 return 0;
1956}
1957
1958/**
1959 * bio_disassociate_task - undo bio_associate_current()
1960 * @bio: target bio
1961 */
1962void bio_disassociate_task(struct bio *bio)
1963{
1964 if (bio->bi_ioc) {
1965 put_io_context(bio->bi_ioc);
1966 bio->bi_ioc = NULL;
1967 }
1968 if (bio->bi_css) {
1969 css_put(bio->bi_css);
1970 bio->bi_css = NULL;
1971 }
1972}
1973
1974#endif /* CONFIG_BLK_CGROUP */
1975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976static void __init biovec_init_slabs(void)
1977{
1978 int i;
1979
1980 for (i = 0; i < BIOVEC_NR_POOLS; i++) {
1981 int size;
1982 struct biovec_slab *bvs = bvec_slabs + i;
1983
Jens Axboea7fcd372008-12-05 16:10:29 +01001984 if (bvs->nr_vecs <= BIO_INLINE_VECS) {
1985 bvs->slab = NULL;
1986 continue;
1987 }
Jens Axboea7fcd372008-12-05 16:10:29 +01001988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 size = bvs->nr_vecs * sizeof(struct bio_vec);
1990 bvs->slab = kmem_cache_create(bvs->name, size, 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09001991 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 }
1993}
1994
1995static int __init init_bio(void)
1996{
Jens Axboebb799ca2008-12-10 15:35:05 +01001997 bio_slab_max = 2;
1998 bio_slab_nr = 0;
1999 bio_slabs = kzalloc(bio_slab_max * sizeof(struct bio_slab), GFP_KERNEL);
2000 if (!bio_slabs)
2001 panic("bio: can't allocate bios\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Martin K. Petersen7878cba2009-06-26 15:37:49 +02002003 bio_integrity_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 biovec_init_slabs();
2005
Jens Axboebb799ca2008-12-10 15:35:05 +01002006 fs_bio_set = bioset_create(BIO_POOL_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 if (!fs_bio_set)
2008 panic("bio: can't allocate bios\n");
2009
Martin K. Petersena91a2782011-03-17 11:11:05 +01002010 if (bioset_integrity_create(fs_bio_set, BIO_POOL_SIZE))
2011 panic("bio: can't create integrity pool\n");
2012
Matthew Dobson0eaae62a2006-03-26 01:37:47 -08002013 bio_split_pool = mempool_create_kmalloc_pool(BIO_SPLIT_ENTRIES,
2014 sizeof(struct bio_pair));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 if (!bio_split_pool)
2016 panic("bio: can't create split pool\n");
2017
2018 return 0;
2019}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020subsys_initcall(init_bio);