]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - fs/bio.c
block: Add an explicit bio flag for bios that own their bvec
[mirror_ubuntu-zesty-kernel.git] / fs / bio.c
1 /*
2 * Copyright (C) 2001 Jens Axboe <axboe@kernel.dk>
3 *
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>
22 #include <linux/iocontext.h>
23 #include <linux/slab.h>
24 #include <linux/init.h>
25 #include <linux/kernel.h>
26 #include <linux/export.h>
27 #include <linux/mempool.h>
28 #include <linux/workqueue.h>
29 #include <linux/cgroup.h>
30 #include <scsi/sg.h> /* for struct sg_iovec */
31
32 #include <trace/events/block.h>
33
34 /*
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
40 static mempool_t *bio_split_pool __read_mostly;
41
42 /*
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 */
47 #define BV(x) { .nr_vecs = x, .name = "biovec-"__stringify(x) }
48 static struct biovec_slab bvec_slabs[BIOVEC_NR_POOLS] __read_mostly = {
49 BV(1), BV(4), BV(16), BV(64), BV(128), BV(BIO_MAX_PAGES),
50 };
51 #undef BV
52
53 /*
54 * 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 */
57 struct bio_set *fs_bio_set;
58 EXPORT_SYMBOL(fs_bio_set);
59
60 /*
61 * Our slab pool management
62 */
63 struct bio_slab {
64 struct kmem_cache *slab;
65 unsigned int slab_ref;
66 unsigned int slab_size;
67 char name[8];
68 };
69 static DEFINE_MUTEX(bio_slab_lock);
70 static struct bio_slab *bio_slabs;
71 static unsigned int bio_slab_nr, bio_slab_max;
72
73 static 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;
77 struct bio_slab *bslab, *new_bio_slabs;
78 unsigned int new_bio_slab_max;
79 unsigned int i, entry = -1;
80
81 mutex_lock(&bio_slab_lock);
82
83 i = 0;
84 while (i < bio_slab_nr) {
85 bslab = &bio_slabs[i];
86
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) {
101 new_bio_slab_max = bio_slab_max << 1;
102 new_bio_slabs = krealloc(bio_slabs,
103 new_bio_slab_max * sizeof(struct bio_slab),
104 GFP_KERNEL);
105 if (!new_bio_slabs)
106 goto out_unlock;
107 bio_slab_max = new_bio_slab_max;
108 bio_slabs = new_bio_slabs;
109 }
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
120 printk(KERN_INFO "bio: create slab <%s> at %d\n", bslab->name, entry);
121 bslab->slab = slab;
122 bslab->slab_ref = 1;
123 bslab->slab_size = sz;
124 out_unlock:
125 mutex_unlock(&bio_slab_lock);
126 return slab;
127 }
128
129 static 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
154 out:
155 mutex_unlock(&bio_slab_lock);
156 }
157
158 unsigned int bvec_nr_vecs(unsigned short idx)
159 {
160 return bvec_slabs[idx].nr_vecs;
161 }
162
163 void bvec_free(mempool_t *pool, struct bio_vec *bv, unsigned int idx)
164 {
165 BIO_BUG_ON(idx >= BIOVEC_NR_POOLS);
166
167 if (idx == BIOVEC_MAX_IDX)
168 mempool_free(bv, pool);
169 else {
170 struct biovec_slab *bvs = bvec_slabs + idx;
171
172 kmem_cache_free(bvs->slab, bv);
173 }
174 }
175
176 struct bio_vec *bvec_alloc(gfp_t gfp_mask, int nr, unsigned long *idx,
177 mempool_t *pool)
178 {
179 struct bio_vec *bvl;
180
181 /*
182 * 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) {
212 fallback:
213 bvl = mempool_alloc(pool, gfp_mask);
214 } else {
215 struct biovec_slab *bvs = bvec_slabs + *idx;
216 gfp_t __gfp_mask = gfp_mask & ~(__GFP_WAIT | __GFP_IO);
217
218 /*
219 * 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.
222 */
223 __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
236 return bvl;
237 }
238
239 static void __bio_free(struct bio *bio)
240 {
241 bio_disassociate_task(bio);
242
243 if (bio_integrity(bio))
244 bio_integrity_free(bio);
245 }
246
247 static 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) {
255 if (bio_flagged(bio, BIO_OWNS_VEC))
256 bvec_free(bs->bvec_pool, bio->bi_io_vec, BIO_POOL_IDX(bio));
257
258 /*
259 * If we have front padding, adjust the bio pointer before freeing
260 */
261 p = bio;
262 p -= bs->front_pad;
263
264 mempool_free(p, bs->bio_pool);
265 } else {
266 /* Bio was allocated by bio_kmalloc() */
267 kfree(bio);
268 }
269 }
270
271 void bio_init(struct bio *bio)
272 {
273 memset(bio, 0, sizeof(*bio));
274 bio->bi_flags = 1 << BIO_UPTODATE;
275 atomic_set(&bio->bi_cnt, 1);
276 }
277 EXPORT_SYMBOL(bio_init);
278
279 /**
280 * 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 */
289 void bio_reset(struct bio *bio)
290 {
291 unsigned long flags = bio->bi_flags & (~0UL << BIO_RESET_BITS);
292
293 __bio_free(bio);
294
295 memset(bio, 0, BIO_RESET_BYTES);
296 bio->bi_flags = flags|(1 << BIO_UPTODATE);
297 }
298 EXPORT_SYMBOL(bio_reset);
299
300 static 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
317 static 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
348 /**
349 * 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
352 * @bs: the bio_set to allocate from.
353 *
354 * Description:
355 * 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 *
365 * 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 *
380 * RETURNS:
381 * Pointer to new bio on success, NULL on failure.
382 */
383 struct bio *bio_alloc_bioset(gfp_t gfp_mask, int nr_iovecs, struct bio_set *bs)
384 {
385 gfp_t saved_gfp = gfp_mask;
386 unsigned front_pad;
387 unsigned inline_vecs;
388 unsigned long idx = BIO_POOL_NONE;
389 struct bio_vec *bvl = NULL;
390 struct bio *bio;
391 void *p;
392
393 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 {
403 /*
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
427 p = mempool_alloc(bs->bio_pool, gfp_mask);
428 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
434 front_pad = bs->front_pad;
435 inline_vecs = BIO_INLINE_VECS;
436 }
437
438 if (unlikely(!p))
439 return NULL;
440
441 bio = p + front_pad;
442 bio_init(bio);
443
444 if (nr_iovecs > inline_vecs) {
445 bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx, bs->bvec_pool);
446 if (!bvl && gfp_mask != saved_gfp) {
447 punt_bios_to_rescuer(bs);
448 gfp_mask = saved_gfp;
449 bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx, bs->bvec_pool);
450 }
451
452 if (unlikely(!bvl))
453 goto err_free;
454
455 bio->bi_flags |= 1 << BIO_OWNS_VEC;
456 } else if (nr_iovecs) {
457 bvl = bio->bi_inline_vecs;
458 }
459
460 bio->bi_pool = bs;
461 bio->bi_flags |= idx << BIO_POOL_OFFSET;
462 bio->bi_max_vecs = nr_iovecs;
463 bio->bi_io_vec = bvl;
464 return bio;
465
466 err_free:
467 mempool_free(p, bs->bio_pool);
468 return NULL;
469 }
470 EXPORT_SYMBOL(bio_alloc_bioset);
471
472 void 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 }
485 EXPORT_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
493 * bio_alloc, bio_get or bio_clone. The last put of a bio will free it.
494 **/
495 void bio_put(struct bio *bio)
496 {
497 BIO_BUG_ON(!atomic_read(&bio->bi_cnt));
498
499 /*
500 * last put frees it
501 */
502 if (atomic_dec_and_test(&bio->bi_cnt))
503 bio_free(bio);
504 }
505 EXPORT_SYMBOL(bio_put);
506
507 inline int bio_phys_segments(struct request_queue *q, struct bio *bio)
508 {
509 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
510 blk_recount_segments(q, bio);
511
512 return bio->bi_phys_segments;
513 }
514 EXPORT_SYMBOL(bio_phys_segments);
515
516 /**
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 */
525 void __bio_clone(struct bio *bio, struct bio *bio_src)
526 {
527 memcpy(bio->bi_io_vec, bio_src->bi_io_vec,
528 bio_src->bi_max_vecs * sizeof(struct bio_vec));
529
530 /*
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 */
534 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;
538 bio->bi_vcnt = bio_src->bi_vcnt;
539 bio->bi_size = bio_src->bi_size;
540 bio->bi_idx = bio_src->bi_idx;
541 }
542 EXPORT_SYMBOL(__bio_clone);
543
544 /**
545 * bio_clone_bioset - clone a bio
546 * @bio: bio to clone
547 * @gfp_mask: allocation priority
548 * @bs: bio_set to allocate from
549 *
550 * Like __bio_clone, only also allocates the returned bio
551 */
552 struct bio *bio_clone_bioset(struct bio *bio, gfp_t gfp_mask,
553 struct bio_set *bs)
554 {
555 struct bio *b;
556
557 b = bio_alloc_bioset(gfp_mask, bio->bi_max_vecs, bs);
558 if (!b)
559 return NULL;
560
561 __bio_clone(b, bio);
562
563 if (bio_integrity(bio)) {
564 int ret;
565
566 ret = bio_integrity_clone(b, bio, gfp_mask);
567
568 if (ret < 0) {
569 bio_put(b);
570 return NULL;
571 }
572 }
573
574 return b;
575 }
576 EXPORT_SYMBOL(bio_clone_bioset);
577
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 */
587 int bio_get_nr_vecs(struct block_device *bdev)
588 {
589 struct request_queue *q = bdev_get_queue(bdev);
590 int nr_pages;
591
592 nr_pages = min_t(unsigned,
593 queue_max_segments(q),
594 queue_max_sectors(q) / (PAGE_SIZE >> 9) + 1);
595
596 return min_t(unsigned, nr_pages, BIO_MAX_PAGES);
597
598 }
599 EXPORT_SYMBOL(bio_get_nr_vecs);
600
601 static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
602 *page, unsigned int len, unsigned int offset,
603 unsigned short max_sectors)
604 {
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
614 if (((bio->bi_size + len) >> 9) > max_sectors)
615 return 0;
616
617 /*
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) {
627 unsigned int prev_bv_len = prev->bv_len;
628 prev->bv_len += len;
629
630 if (q->merge_bvec_fn) {
631 struct bvec_merge_data bvm = {
632 /* prev_bvec is already charged in
633 bi_size, discharge it in order to
634 simulate merging updated prev_bvec
635 as new bvec. */
636 .bi_bdev = bio->bi_bdev,
637 .bi_sector = bio->bi_sector,
638 .bi_size = bio->bi_size - prev_bv_len,
639 .bi_rw = bio->bi_rw,
640 };
641
642 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len) {
643 prev->bv_len -= len;
644 return 0;
645 }
646 }
647
648 goto done;
649 }
650 }
651
652 if (bio->bi_vcnt >= bio->bi_max_vecs)
653 return 0;
654
655 /*
656 * we might lose a segment or two here, but rather that than
657 * make this too complex.
658 */
659
660 while (bio->bi_phys_segments >= queue_max_segments(q)) {
661
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) {
684 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
691 /*
692 * merge_bvec_fn() returns number of bytes it can accept
693 * at this offset
694 */
695 if (q->merge_bvec_fn(q, &bvm, bvec) < bvec->bv_len) {
696 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 */
704 if (bio->bi_vcnt && (BIOVEC_PHYS_MERGEABLE(bvec-1, bvec)))
705 bio->bi_flags &= ~(1 << BIO_SEG_VALID);
706
707 bio->bi_vcnt++;
708 bio->bi_phys_segments++;
709 done:
710 bio->bi_size += len;
711 return len;
712 }
713
714 /**
715 * bio_add_pc_page - attempt to add page to bio
716 * @q: the target queue
717 * @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
723 * 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.
728 */
729 int bio_add_pc_page(struct request_queue *q, struct bio *bio, struct page *page,
730 unsigned int len, unsigned int offset)
731 {
732 return __bio_add_page(q, bio, page, len, offset,
733 queue_max_hw_sectors(q));
734 }
735 EXPORT_SYMBOL(bio_add_pc_page);
736
737 /**
738 * 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
745 * 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.
748 */
749 int bio_add_page(struct bio *bio, struct page *page, unsigned int len,
750 unsigned int offset)
751 {
752 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
753 return __bio_add_page(q, bio, page, len, offset, queue_max_sectors(q));
754 }
755 EXPORT_SYMBOL(bio_add_page);
756
757 struct submit_bio_ret {
758 struct completion event;
759 int error;
760 };
761
762 static 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 */
778 int 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 }
791 EXPORT_SYMBOL(submit_bio_wait);
792
793 /**
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 */
804 void 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 }
832 EXPORT_SYMBOL(bio_advance);
833
834 /**
835 * 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 */
844 int 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 }
860 EXPORT_SYMBOL(bio_alloc_pages);
861
862 /**
863 * 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 */
874 void 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 }
930 EXPORT_SYMBOL(bio_copy_data);
931
932 struct bio_map_data {
933 struct bio_vec *iovecs;
934 struct sg_iovec *sgvecs;
935 int nr_sgvecs;
936 int is_our_pages;
937 };
938
939 static void bio_set_map_data(struct bio_map_data *bmd, struct bio *bio,
940 struct sg_iovec *iov, int iov_count,
941 int is_our_pages)
942 {
943 memcpy(bmd->iovecs, bio->bi_io_vec, sizeof(struct bio_vec) * bio->bi_vcnt);
944 memcpy(bmd->sgvecs, iov, sizeof(struct sg_iovec) * iov_count);
945 bmd->nr_sgvecs = iov_count;
946 bmd->is_our_pages = is_our_pages;
947 bio->bi_private = bmd;
948 }
949
950 static void bio_free_map_data(struct bio_map_data *bmd)
951 {
952 kfree(bmd->iovecs);
953 kfree(bmd->sgvecs);
954 kfree(bmd);
955 }
956
957 static struct bio_map_data *bio_alloc_map_data(int nr_segs,
958 unsigned int iov_count,
959 gfp_t gfp_mask)
960 {
961 struct bio_map_data *bmd;
962
963 if (iov_count > UIO_MAXIOV)
964 return NULL;
965
966 bmd = kmalloc(sizeof(*bmd), gfp_mask);
967 if (!bmd)
968 return NULL;
969
970 bmd->iovecs = kmalloc(sizeof(struct bio_vec) * nr_segs, gfp_mask);
971 if (!bmd->iovecs) {
972 kfree(bmd);
973 return NULL;
974 }
975
976 bmd->sgvecs = kmalloc(sizeof(struct sg_iovec) * iov_count, gfp_mask);
977 if (bmd->sgvecs)
978 return bmd;
979
980 kfree(bmd->iovecs);
981 kfree(bmd);
982 return NULL;
983 }
984
985 static int __bio_copy_iov(struct bio *bio, struct bio_vec *iovecs,
986 struct sg_iovec *iov, int iov_count,
987 int to_user, int from_user, int do_free_page)
988 {
989 int ret = 0, i;
990 struct bio_vec *bvec;
991 int iov_idx = 0;
992 unsigned int iov_off = 0;
993
994 bio_for_each_segment_all(bvec, bio, i) {
995 char *bv_addr = page_address(bvec->bv_page);
996 unsigned int bv_len = iovecs[i].bv_len;
997
998 while (bv_len && iov_idx < iov_count) {
999 unsigned int bytes;
1000 char __user *iov_addr;
1001
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) {
1007 if (to_user)
1008 ret = copy_to_user(iov_addr, bv_addr,
1009 bytes);
1010
1011 if (from_user)
1012 ret = copy_from_user(bv_addr, iov_addr,
1013 bytes);
1014
1015 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
1030 if (do_free_page)
1031 __free_page(bvec->bv_page);
1032 }
1033
1034 return ret;
1035 }
1036
1037 /**
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 */
1044 int bio_uncopy_user(struct bio *bio)
1045 {
1046 struct bio_map_data *bmd = bio->bi_private;
1047 int ret = 0;
1048
1049 if (!bio_flagged(bio, BIO_NULL_MAPPED))
1050 ret = __bio_copy_iov(bio, bmd->iovecs, bmd->sgvecs,
1051 bmd->nr_sgvecs, bio_data_dir(bio) == READ,
1052 0, bmd->is_our_pages);
1053 bio_free_map_data(bmd);
1054 bio_put(bio);
1055 return ret;
1056 }
1057 EXPORT_SYMBOL(bio_uncopy_user);
1058
1059 /**
1060 * bio_copy_user_iov - copy user data to bio
1061 * @q: destination block queue
1062 * @map_data: pointer to the rq_map_data holding pages (if necessary)
1063 * @iov: the iovec.
1064 * @iov_count: number of elements in the iovec
1065 * @write_to_vm: bool indicating writing to pages or not
1066 * @gfp_mask: memory allocation flags
1067 *
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 */
1072 struct 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)
1076 {
1077 struct bio_map_data *bmd;
1078 struct bio_vec *bvec;
1079 struct page *page;
1080 struct bio *bio;
1081 int i, ret;
1082 int nr_pages = 0;
1083 unsigned int len = 0;
1084 unsigned int offset = map_data ? map_data->offset & ~PAGE_MASK : 0;
1085
1086 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
1095 /*
1096 * Overflow, abort
1097 */
1098 if (end < start)
1099 return ERR_PTR(-EINVAL);
1100
1101 nr_pages += end - start;
1102 len += iov[i].iov_len;
1103 }
1104
1105 if (offset)
1106 nr_pages++;
1107
1108 bmd = bio_alloc_map_data(nr_pages, iov_count, gfp_mask);
1109 if (!bmd)
1110 return ERR_PTR(-ENOMEM);
1111
1112 ret = -ENOMEM;
1113 bio = bio_kmalloc(gfp_mask, nr_pages);
1114 if (!bio)
1115 goto out_bmd;
1116
1117 if (!write_to_vm)
1118 bio->bi_rw |= REQ_WRITE;
1119
1120 ret = 0;
1121
1122 if (map_data) {
1123 nr_pages = 1 << map_data->page_order;
1124 i = map_data->offset / PAGE_SIZE;
1125 }
1126 while (len) {
1127 unsigned int bytes = PAGE_SIZE;
1128
1129 bytes -= offset;
1130
1131 if (bytes > len)
1132 bytes = len;
1133
1134 if (map_data) {
1135 if (i == map_data->nr_entries * nr_pages) {
1136 ret = -ENOMEM;
1137 break;
1138 }
1139
1140 page = map_data->pages[i / nr_pages];
1141 page += (i % nr_pages);
1142
1143 i++;
1144 } else {
1145 page = alloc_page(q->bounce_gfp | gfp_mask);
1146 if (!page) {
1147 ret = -ENOMEM;
1148 break;
1149 }
1150 }
1151
1152 if (bio_add_pc_page(q, bio, page, bytes, offset) < bytes)
1153 break;
1154
1155 len -= bytes;
1156 offset = 0;
1157 }
1158
1159 if (ret)
1160 goto cleanup;
1161
1162 /*
1163 * success
1164 */
1165 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);
1168 if (ret)
1169 goto cleanup;
1170 }
1171
1172 bio_set_map_data(bmd, bio, iov, iov_count, map_data ? 0 : 1);
1173 return bio;
1174 cleanup:
1175 if (!map_data)
1176 bio_for_each_segment_all(bvec, bio, i)
1177 __free_page(bvec->bv_page);
1178
1179 bio_put(bio);
1180 out_bmd:
1181 bio_free_map_data(bmd);
1182 return ERR_PTR(ret);
1183 }
1184
1185 /**
1186 * bio_copy_user - copy user data to bio
1187 * @q: destination block queue
1188 * @map_data: pointer to the rq_map_data holding pages (if necessary)
1189 * @uaddr: start of user address
1190 * @len: length in bytes
1191 * @write_to_vm: bool indicating writing to pages or not
1192 * @gfp_mask: memory allocation flags
1193 *
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 */
1198 struct 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)
1201 {
1202 struct sg_iovec iov;
1203
1204 iov.iov_base = (void __user *)uaddr;
1205 iov.iov_len = len;
1206
1207 return bio_copy_user_iov(q, map_data, &iov, 1, write_to_vm, gfp_mask);
1208 }
1209 EXPORT_SYMBOL(bio_copy_user);
1210
1211 static struct bio *__bio_map_user_iov(struct request_queue *q,
1212 struct block_device *bdev,
1213 struct sg_iovec *iov, int iov_count,
1214 int write_to_vm, gfp_t gfp_mask)
1215 {
1216 int i, j;
1217 int nr_pages = 0;
1218 struct page **pages;
1219 struct bio *bio;
1220 int cur_page = 0;
1221 int ret, offset;
1222
1223 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
1229 /*
1230 * Overflow, abort
1231 */
1232 if (end < start)
1233 return ERR_PTR(-EINVAL);
1234
1235 nr_pages += end - start;
1236 /*
1237 * buffer must be aligned to at least hardsector size for now
1238 */
1239 if (uaddr & queue_dma_alignment(q))
1240 return ERR_PTR(-EINVAL);
1241 }
1242
1243 if (!nr_pages)
1244 return ERR_PTR(-EINVAL);
1245
1246 bio = bio_kmalloc(gfp_mask, nr_pages);
1247 if (!bio)
1248 return ERR_PTR(-ENOMEM);
1249
1250 ret = -ENOMEM;
1251 pages = kcalloc(nr_pages, sizeof(struct page *), gfp_mask);
1252 if (!pages)
1253 goto out;
1254
1255 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;
1262
1263 ret = get_user_pages_fast(uaddr, local_nr_pages,
1264 write_to_vm, &pages[cur_page]);
1265 if (ret < local_nr_pages) {
1266 ret = -EFAULT;
1267 goto out_unmap;
1268 }
1269
1270 offset = uaddr & ~PAGE_MASK;
1271 for (j = cur_page; j < page_limit; j++) {
1272 unsigned int bytes = PAGE_SIZE - offset;
1273
1274 if (len <= 0)
1275 break;
1276
1277 if (bytes > len)
1278 bytes = len;
1279
1280 /*
1281 * sorry...
1282 */
1283 if (bio_add_pc_page(q, bio, pages[j], bytes, offset) <
1284 bytes)
1285 break;
1286
1287 len -= bytes;
1288 offset = 0;
1289 }
1290
1291 cur_page = j;
1292 /*
1293 * release the pages we didn't map into the bio, if any
1294 */
1295 while (j < page_limit)
1296 page_cache_release(pages[j++]);
1297 }
1298
1299 kfree(pages);
1300
1301 /*
1302 * set data direction, and check if mapped pages need bouncing
1303 */
1304 if (!write_to_vm)
1305 bio->bi_rw |= REQ_WRITE;
1306
1307 bio->bi_bdev = bdev;
1308 bio->bi_flags |= (1 << BIO_USER_MAPPED);
1309 return bio;
1310
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:
1318 kfree(pages);
1319 bio_put(bio);
1320 return ERR_PTR(ret);
1321 }
1322
1323 /**
1324 * bio_map_user - map user address into bio
1325 * @q: the struct request_queue for the bio
1326 * @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
1330 * @gfp_mask: memory allocation flags
1331 *
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 */
1335 struct bio *bio_map_user(struct request_queue *q, struct block_device *bdev,
1336 unsigned long uaddr, unsigned int len, int write_to_vm,
1337 gfp_t gfp_mask)
1338 {
1339 struct sg_iovec iov;
1340
1341 iov.iov_base = (void __user *)uaddr;
1342 iov.iov_len = len;
1343
1344 return bio_map_user_iov(q, bdev, &iov, 1, write_to_vm, gfp_mask);
1345 }
1346 EXPORT_SYMBOL(bio_map_user);
1347
1348 /**
1349 * bio_map_user_iov - map user sg_iovec table into bio
1350 * @q: the struct request_queue for the bio
1351 * @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
1355 * @gfp_mask: memory allocation flags
1356 *
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 */
1360 struct bio *bio_map_user_iov(struct request_queue *q, struct block_device *bdev,
1361 struct sg_iovec *iov, int iov_count,
1362 int write_to_vm, gfp_t gfp_mask)
1363 {
1364 struct bio *bio;
1365
1366 bio = __bio_map_user_iov(q, bdev, iov, iov_count, write_to_vm,
1367 gfp_mask);
1368 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
1379 return bio;
1380 }
1381
1382 static 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 */
1390 bio_for_each_segment_all(bvec, bio, i) {
1391 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 */
1409 void bio_unmap_user(struct bio *bio)
1410 {
1411 __bio_unmap_user(bio);
1412 bio_put(bio);
1413 }
1414 EXPORT_SYMBOL(bio_unmap_user);
1415
1416 static void bio_map_kern_endio(struct bio *bio, int err)
1417 {
1418 bio_put(bio);
1419 }
1420
1421 static struct bio *__bio_map_kern(struct request_queue *q, void *data,
1422 unsigned int len, gfp_t gfp_mask)
1423 {
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
1431 bio = bio_kmalloc(gfp_mask, nr_pages);
1432 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
1445 if (bio_add_pc_page(q, bio, virt_to_page(data), bytes,
1446 offset) < bytes)
1447 break;
1448
1449 data += bytes;
1450 len -= bytes;
1451 offset = 0;
1452 }
1453
1454 bio->bi_end_io = bio_map_kern_endio;
1455 return bio;
1456 }
1457
1458 /**
1459 * bio_map_kern - map kernel address into bio
1460 * @q: the struct request_queue for the bio
1461 * @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 */
1468 struct bio *bio_map_kern(struct request_queue *q, void *data, unsigned int len,
1469 gfp_t gfp_mask)
1470 {
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 }
1486 EXPORT_SYMBOL(bio_map_kern);
1487
1488 static 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;
1492 struct bio_map_data *bmd = bio->bi_private;
1493 int i;
1494 char *p = bmd->sgvecs[0].iov_base;
1495
1496 bio_for_each_segment_all(bvec, bio, i) {
1497 char *addr = page_address(bvec->bv_page);
1498 int len = bmd->iovecs[i].bv_len;
1499
1500 if (read)
1501 memcpy(p, addr, len);
1502
1503 __free_page(bvec->bv_page);
1504 p += len;
1505 }
1506
1507 bio_free_map_data(bmd);
1508 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
1517 * @reading: data direction is READ
1518 *
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 */
1522 struct bio *bio_copy_kern(struct request_queue *q, void *data, unsigned int len,
1523 gfp_t gfp_mask, int reading)
1524 {
1525 struct bio *bio;
1526 struct bio_vec *bvec;
1527 int i;
1528
1529 bio = bio_copy_user(q, NULL, (unsigned long)data, len, 1, gfp_mask);
1530 if (IS_ERR(bio))
1531 return bio;
1532
1533 if (!reading) {
1534 void *p = data;
1535
1536 bio_for_each_segment_all(bvec, bio, i) {
1537 char *addr = page_address(bvec->bv_page);
1538
1539 memcpy(addr, p, bvec->bv_len);
1540 p += bvec->bv_len;
1541 }
1542 }
1543
1544 bio->bi_end_io = bio_copy_kern_endio;
1545
1546 return bio;
1547 }
1548 EXPORT_SYMBOL(bio_copy_kern);
1549
1550 /*
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.
1569 * But other code (eg, flusher threads) could clean the pages if they are mapped
1570 * 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 */
1579 void bio_set_pages_dirty(struct bio *bio)
1580 {
1581 struct bio_vec *bvec;
1582 int i;
1583
1584 bio_for_each_segment_all(bvec, bio, i) {
1585 struct page *page = bvec->bv_page;
1586
1587 if (page && !PageCompound(page))
1588 set_page_dirty_lock(page);
1589 }
1590 }
1591
1592 static void bio_release_pages(struct bio *bio)
1593 {
1594 struct bio_vec *bvec;
1595 int i;
1596
1597 bio_for_each_segment_all(bvec, bio, i) {
1598 struct page *page = bvec->bv_page;
1599
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
1616 static void bio_dirty_fn(struct work_struct *work);
1617
1618 static DECLARE_WORK(bio_dirty_work, bio_dirty_fn);
1619 static DEFINE_SPINLOCK(bio_dirty_lock);
1620 static struct bio *bio_dirty_list;
1621
1622 /*
1623 * This runs in process context
1624 */
1625 static void bio_dirty_fn(struct work_struct *work)
1626 {
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
1645 void bio_check_pages_dirty(struct bio *bio)
1646 {
1647 struct bio_vec *bvec;
1648 int nr_clean_pages = 0;
1649 int i;
1650
1651 bio_for_each_segment_all(bvec, bio, i) {
1652 struct page *page = bvec->bv_page;
1653
1654 if (PageDirty(page) || PageCompound(page)) {
1655 page_cache_release(page);
1656 bvec->bv_page = NULL;
1657 } 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
1675 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
1676 void 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 }
1684 EXPORT_SYMBOL(bio_flush_dcache_pages);
1685 #endif
1686
1687 /**
1688 * bio_endio - end I/O on a bio
1689 * @bio: bio
1690 * @error: error, if any
1691 *
1692 * Description:
1693 * bio_endio() will end I/O on the whole bio. bio_endio() is the
1694 * 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
1697 * something went wrong. No one should call bi_end_io() directly on a
1698 * bio unless they own it and thus know that it has an end_io
1699 * function.
1700 **/
1701 void bio_endio(struct bio *bio, int error)
1702 {
1703 if (error)
1704 clear_bit(BIO_UPTODATE, &bio->bi_flags);
1705 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
1706 error = -EIO;
1707
1708 trace_block_bio_complete(bio, error);
1709
1710 if (bio->bi_end_io)
1711 bio->bi_end_io(bio, error);
1712 }
1713 EXPORT_SYMBOL(bio_endio);
1714
1715 void 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
1720 bio_endio(master, bp->error);
1721 mempool_free(bp, bp->bio2.bi_private);
1722 }
1723 }
1724 EXPORT_SYMBOL(bio_pair_release);
1725
1726 static void bio_pair_end_1(struct bio *bi, int err)
1727 {
1728 struct bio_pair *bp = container_of(bi, struct bio_pair, bio1);
1729
1730 if (err)
1731 bp->error = err;
1732
1733 bio_pair_release(bp);
1734 }
1735
1736 static void bio_pair_end_2(struct bio *bi, int err)
1737 {
1738 struct bio_pair *bp = container_of(bi, struct bio_pair, bio2);
1739
1740 if (err)
1741 bp->error = err;
1742
1743 bio_pair_release(bp);
1744 }
1745
1746 /*
1747 * split a bio - only worry about a bio with a single page in its iovec
1748 */
1749 struct bio_pair *bio_split(struct bio *bi, int first_sectors)
1750 {
1751 struct bio_pair *bp = mempool_alloc(bio_split_pool, GFP_NOIO);
1752
1753 if (!bp)
1754 return bp;
1755
1756 trace_block_split(bdev_get_queue(bi->bi_bdev), bi,
1757 bi->bi_sector + first_sectors);
1758
1759 BUG_ON(bio_segments(bi) > 1);
1760 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
1768 if (bi->bi_vcnt != 0) {
1769 bp->bv1 = *bio_iovec(bi);
1770 bp->bv2 = *bio_iovec(bi);
1771
1772 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;
1783 }
1784
1785 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;
1789 bp->bio2.bi_private = bio_split_pool;
1790
1791 if (bio_integrity(bi))
1792 bio_integrity_split(bi, bp, first_sectors);
1793
1794 return bp;
1795 }
1796 EXPORT_SYMBOL(bio_split);
1797
1798 /**
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 */
1808 sector_t bio_sector_offset(struct bio *bio, unsigned short index,
1809 unsigned int offset)
1810 {
1811 unsigned int sector_sz;
1812 struct bio_vec *bv;
1813 sector_t sectors;
1814 int i;
1815
1816 sector_sz = queue_logical_block_size(bio->bi_bdev->bd_disk->queue);
1817 sectors = 0;
1818
1819 if (index >= bio->bi_idx)
1820 index = bio->bi_vcnt - 1;
1821
1822 bio_for_each_segment_all(bv, bio, i) {
1823 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 }
1834 EXPORT_SYMBOL(bio_sector_offset);
1835
1836 /*
1837 * create memory pools for biovec's in a bio_set.
1838 * use the global biovec slabs created for general use.
1839 */
1840 mempool_t *biovec_create_pool(struct bio_set *bs, int pool_entries)
1841 {
1842 struct biovec_slab *bp = bvec_slabs + BIOVEC_MAX_IDX;
1843
1844 return mempool_create_slab_pool(pool_entries, bp->slab);
1845 }
1846
1847 void bioset_free(struct bio_set *bs)
1848 {
1849 if (bs->rescue_workqueue)
1850 destroy_workqueue(bs->rescue_workqueue);
1851
1852 if (bs->bio_pool)
1853 mempool_destroy(bs->bio_pool);
1854
1855 if (bs->bvec_pool)
1856 mempool_destroy(bs->bvec_pool);
1857
1858 bioset_integrity_free(bs);
1859 bio_put_slab(bs);
1860
1861 kfree(bs);
1862 }
1863 EXPORT_SYMBOL(bioset_free);
1864
1865 /**
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 */
1878 struct bio_set *bioset_create(unsigned int pool_size, unsigned int front_pad)
1879 {
1880 unsigned int back_pad = BIO_INLINE_VECS * sizeof(struct bio_vec);
1881 struct bio_set *bs;
1882
1883 bs = kzalloc(sizeof(*bs), GFP_KERNEL);
1884 if (!bs)
1885 return NULL;
1886
1887 bs->front_pad = front_pad;
1888
1889 spin_lock_init(&bs->rescue_lock);
1890 bio_list_init(&bs->rescue_list);
1891 INIT_WORK(&bs->rescue_work, bio_alloc_rescue);
1892
1893 bs->bio_slab = bio_find_or_create_slab(front_pad + back_pad);
1894 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);
1900 if (!bs->bio_pool)
1901 goto bad;
1902
1903 bs->bvec_pool = biovec_create_pool(bs, pool_size);
1904 if (!bs->bvec_pool)
1905 goto bad;
1906
1907 bs->rescue_workqueue = alloc_workqueue("bioset", WQ_MEM_RECLAIM, 0);
1908 if (!bs->rescue_workqueue)
1909 goto bad;
1910
1911 return bs;
1912 bad:
1913 bioset_free(bs);
1914 return NULL;
1915 }
1916 EXPORT_SYMBOL(bioset_create);
1917
1918 #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 */
1932 int 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 */
1962 void 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
1976 static 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
1984 if (bvs->nr_vecs <= BIO_INLINE_VECS) {
1985 bvs->slab = NULL;
1986 continue;
1987 }
1988
1989 size = bvs->nr_vecs * sizeof(struct bio_vec);
1990 bvs->slab = kmem_cache_create(bvs->name, size, 0,
1991 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
1992 }
1993 }
1994
1995 static int __init init_bio(void)
1996 {
1997 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");
2002
2003 bio_integrity_init();
2004 biovec_init_slabs();
2005
2006 fs_bio_set = bioset_create(BIO_POOL_SIZE, 0);
2007 if (!fs_bio_set)
2008 panic("bio: can't allocate bios\n");
2009
2010 if (bioset_integrity_create(fs_bio_set, BIO_POOL_SIZE))
2011 panic("bio: can't create integrity pool\n");
2012
2013 bio_split_pool = mempool_create_kmalloc_pool(BIO_SPLIT_ENTRIES,
2014 sizeof(struct bio_pair));
2015 if (!bio_split_pool)
2016 panic("bio: can't create split pool\n");
2017
2018 return 0;
2019 }
2020 subsys_initcall(init_bio);