2 * Copyright (C) 2001 Jens Axboe <axboe@suse.de>
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.
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.
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-
19 #include <linux/swap.h>
20 #include <linux/bio.h>
21 #include <linux/blkdev.h>
22 #include <linux/slab.h>
23 #include <linux/init.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/mempool.h>
27 #include <linux/workqueue.h>
29 #define BIO_POOL_SIZE 256
31 static mempool_t
*bio_pool
;
32 static kmem_cache_t
*bio_slab
;
34 #define BIOVEC_NR_POOLS 6
37 * a small number of entries is fine, not going to be performance critical.
38 * basically we just need to survive
40 #define BIO_SPLIT_ENTRIES 8
41 mempool_t
*bio_split_pool
;
51 * if you change this list, also change bvec_alloc or things will
52 * break badly! cannot be bigger than what you can fit into an
56 #define BV(x) { .nr_vecs = x, .name = "biovec-" #x }
57 static struct biovec_pool bvec_array
[BIOVEC_NR_POOLS
] = {
58 BV(1), BV(4), BV(16), BV(64), BV(128), BV(BIO_MAX_PAGES
),
62 static inline struct bio_vec
*bvec_alloc(int gfp_mask
, int nr
, unsigned long *idx
)
64 struct biovec_pool
*bp
;
68 * see comment near bvec_array define!
71 case 1 : *idx
= 0; break;
72 case 2 ... 4: *idx
= 1; break;
73 case 5 ... 16: *idx
= 2; break;
74 case 17 ... 64: *idx
= 3; break;
75 case 65 ... 128: *idx
= 4; break;
76 case 129 ... BIO_MAX_PAGES
: *idx
= 5; break;
81 * idx now points to the pool we want to allocate from
83 bp
= bvec_array
+ *idx
;
85 bvl
= mempool_alloc(bp
->pool
, gfp_mask
);
87 memset(bvl
, 0, bp
->nr_vecs
* sizeof(struct bio_vec
));
92 * default destructor for a bio allocated with bio_alloc()
94 void bio_destructor(struct bio
*bio
)
96 const int pool_idx
= BIO_POOL_IDX(bio
);
97 struct biovec_pool
*bp
= bvec_array
+ pool_idx
;
99 BIO_BUG_ON(pool_idx
>= BIOVEC_NR_POOLS
);
102 * cloned bio doesn't own the veclist
104 if (!bio_flagged(bio
, BIO_CLONED
))
105 mempool_free(bio
->bi_io_vec
, bp
->pool
);
107 mempool_free(bio
, bio_pool
);
110 inline void bio_init(struct bio
*bio
)
113 bio
->bi_flags
= 1 << BIO_UPTODATE
;
117 bio
->bi_phys_segments
= 0;
118 bio
->bi_hw_segments
= 0;
120 bio
->bi_max_vecs
= 0;
121 bio
->bi_end_io
= NULL
;
122 atomic_set(&bio
->bi_cnt
, 1);
123 bio
->bi_private
= NULL
;
127 * bio_alloc - allocate a bio for I/O
128 * @gfp_mask: the GFP_ mask given to the slab allocator
129 * @nr_iovecs: number of iovecs to pre-allocate
132 * bio_alloc will first try it's on mempool to satisfy the allocation.
133 * If %__GFP_WAIT is set then we will block on the internal pool waiting
134 * for a &struct bio to become free.
136 struct bio
*bio_alloc(int gfp_mask
, int nr_iovecs
)
138 struct bio_vec
*bvl
= NULL
;
142 bio
= mempool_alloc(bio_pool
, gfp_mask
);
148 if (unlikely(!nr_iovecs
))
151 bvl
= bvec_alloc(gfp_mask
, nr_iovecs
, &idx
);
153 bio
->bi_flags
|= idx
<< BIO_POOL_OFFSET
;
154 bio
->bi_max_vecs
= bvec_array
[idx
].nr_vecs
;
156 bio
->bi_io_vec
= bvl
;
157 bio
->bi_destructor
= bio_destructor
;
162 mempool_free(bio
, bio_pool
);
168 * bio_put - release a reference to a bio
169 * @bio: bio to release reference to
172 * Put a reference to a &struct bio, either one you have gotten with
173 * bio_alloc or bio_get. The last put of a bio will free it.
175 void bio_put(struct bio
*bio
)
177 BIO_BUG_ON(!atomic_read(&bio
->bi_cnt
));
182 if (atomic_dec_and_test(&bio
->bi_cnt
)) {
184 bio
->bi_destructor(bio
);
188 inline int bio_phys_segments(request_queue_t
*q
, struct bio
*bio
)
190 if (unlikely(!bio_flagged(bio
, BIO_SEG_VALID
)))
191 blk_recount_segments(q
, bio
);
193 return bio
->bi_phys_segments
;
196 inline int bio_hw_segments(request_queue_t
*q
, struct bio
*bio
)
198 if (unlikely(!bio_flagged(bio
, BIO_SEG_VALID
)))
199 blk_recount_segments(q
, bio
);
201 return bio
->bi_hw_segments
;
205 * __bio_clone - clone a bio
206 * @bio: destination bio
207 * @bio_src: bio to clone
209 * Clone a &bio. Caller will own the returned bio, but not
210 * the actual data it points to. Reference count of returned
213 inline void __bio_clone(struct bio
*bio
, struct bio
*bio_src
)
215 bio
->bi_io_vec
= bio_src
->bi_io_vec
;
217 bio
->bi_sector
= bio_src
->bi_sector
;
218 bio
->bi_bdev
= bio_src
->bi_bdev
;
219 bio
->bi_flags
|= 1 << BIO_CLONED
;
220 bio
->bi_rw
= bio_src
->bi_rw
;
223 * notes -- maybe just leave bi_idx alone. assume identical mapping
226 bio
->bi_vcnt
= bio_src
->bi_vcnt
;
227 bio
->bi_idx
= bio_src
->bi_idx
;
228 if (bio_flagged(bio
, BIO_SEG_VALID
)) {
229 bio
->bi_phys_segments
= bio_src
->bi_phys_segments
;
230 bio
->bi_hw_segments
= bio_src
->bi_hw_segments
;
231 bio
->bi_flags
|= (1 << BIO_SEG_VALID
);
233 bio
->bi_size
= bio_src
->bi_size
;
236 * cloned bio does not own the bio_vec, so users cannot fiddle with
237 * it. clear bi_max_vecs and clear the BIO_POOL_BITS to make this
240 bio
->bi_max_vecs
= 0;
241 bio
->bi_flags
&= (BIO_POOL_MASK
- 1);
245 * bio_clone - clone a bio
247 * @gfp_mask: allocation priority
249 * Like __bio_clone, only also allocates the returned bio
251 struct bio
*bio_clone(struct bio
*bio
, int gfp_mask
)
253 struct bio
*b
= bio_alloc(gfp_mask
, 0);
262 * bio_get_nr_vecs - return approx number of vecs
265 * Return the approximate number of pages we can send to this target.
266 * There's no guarantee that you will be able to fit this number of pages
267 * into a bio, it does not account for dynamic restrictions that vary
270 int bio_get_nr_vecs(struct block_device
*bdev
)
272 request_queue_t
*q
= bdev_get_queue(bdev
);
275 nr_pages
= ((q
->max_sectors
<< 9) + PAGE_SIZE
- 1) >> PAGE_SHIFT
;
276 if (nr_pages
> q
->max_phys_segments
)
277 nr_pages
= q
->max_phys_segments
;
278 if (nr_pages
> q
->max_hw_segments
)
279 nr_pages
= q
->max_hw_segments
;
284 static int __bio_add_page(request_queue_t
*q
, struct bio
*bio
, struct page
285 *page
, unsigned int len
, unsigned int offset
)
287 int retried_segments
= 0;
288 struct bio_vec
*bvec
;
291 * cloned bio must not modify vec list
293 if (unlikely(bio_flagged(bio
, BIO_CLONED
)))
296 if (bio
->bi_vcnt
>= bio
->bi_max_vecs
)
299 if (((bio
->bi_size
+ len
) >> 9) > q
->max_sectors
)
303 * we might lose a segment or two here, but rather that than
304 * make this too complex.
307 while (bio_phys_segments(q
, bio
) >= q
->max_phys_segments
308 || bio_hw_segments(q
, bio
) >= q
->max_hw_segments
) {
310 if (retried_segments
)
313 bio
->bi_flags
&= ~(1 << BIO_SEG_VALID
);
314 retried_segments
= 1;
318 * setup the new entry, we might clear it again later if we
319 * cannot add the page
321 bvec
= &bio
->bi_io_vec
[bio
->bi_vcnt
];
322 bvec
->bv_page
= page
;
324 bvec
->bv_offset
= offset
;
327 * if queue has other restrictions (eg varying max sector size
328 * depending on offset), it can specify a merge_bvec_fn in the
329 * queue to get further control
331 if (q
->merge_bvec_fn
) {
333 * merge_bvec_fn() returns number of bytes it can accept
336 if (q
->merge_bvec_fn(q
, bio
, bvec
) < len
) {
337 bvec
->bv_page
= NULL
;
345 bio
->bi_phys_segments
++;
346 bio
->bi_hw_segments
++;
352 * bio_add_page - attempt to add page to bio
353 * @bio: destination bio
355 * @len: vec entry length
356 * @offset: vec entry offset
358 * Attempt to add a page to the bio_vec maplist. This can fail for a
359 * number of reasons, such as the bio being full or target block
360 * device limitations. The target block device must allow bio's
361 * smaller than PAGE_SIZE, so it is always possible to add a single
362 * page to an empty bio.
364 int bio_add_page(struct bio
*bio
, struct page
*page
, unsigned int len
,
367 return __bio_add_page(bdev_get_queue(bio
->bi_bdev
), bio
, page
,
371 static struct bio
*__bio_map_user(request_queue_t
*q
, struct block_device
*bdev
,
372 unsigned long uaddr
, unsigned int len
,
375 unsigned long end
= (uaddr
+ len
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
376 unsigned long start
= uaddr
>> PAGE_SHIFT
;
377 const int nr_pages
= end
- start
;
383 * transfer and buffer must be aligned to at least hardsector
384 * size for now, in the future we can relax this restriction
386 if ((uaddr
& queue_dma_alignment(q
)) || (len
& queue_dma_alignment(q
)))
389 bio
= bio_alloc(GFP_KERNEL
, nr_pages
);
393 pages
= kmalloc(nr_pages
* sizeof(struct page
*), GFP_KERNEL
);
397 down_read(¤t
->mm
->mmap_sem
);
398 ret
= get_user_pages(current
, current
->mm
, uaddr
, nr_pages
,
399 write_to_vm
, 0, pages
, NULL
);
400 up_read(¤t
->mm
->mmap_sem
);
407 offset
= uaddr
& ~PAGE_MASK
;
408 for (i
= 0; i
< nr_pages
; i
++) {
409 unsigned int bytes
= PAGE_SIZE
- offset
;
420 if (__bio_add_page(q
, bio
, pages
[i
], bytes
, offset
) < bytes
)
428 * release the pages we didn't map into the bio, if any
431 page_cache_release(pages
[i
++]);
436 * set data direction, and check if mapped pages need bouncing
439 bio
->bi_rw
|= (1 << BIO_RW
);
441 blk_queue_bounce(q
, &bio
);
450 * bio_map_user - map user address into bio
451 * @bdev: destination block device
452 * @uaddr: start of user address
453 * @len: length in bytes
454 * @write_to_vm: bool indicating writing to pages or not
456 * Map the user space address into a bio suitable for io to a block
459 struct bio
*bio_map_user(request_queue_t
*q
, struct block_device
*bdev
,
460 unsigned long uaddr
, unsigned int len
, int write_to_vm
)
464 bio
= __bio_map_user(q
, bdev
, uaddr
, len
, write_to_vm
);
468 * subtle -- if __bio_map_user() ended up bouncing a bio,
469 * it would normally disappear when its bi_end_io is run.
470 * however, we need it for the unmap, so grab an extra
475 if (bio
->bi_size
< len
) {
476 bio_endio(bio
, bio
->bi_size
, 0);
477 bio_unmap_user(bio
, 0);
485 static void __bio_unmap_user(struct bio
*bio
, int write_to_vm
)
487 struct bio_vec
*bvec
;
491 * find original bio if it was bounced
493 if (bio
->bi_private
) {
495 * someone stole our bio, must not happen
497 BUG_ON(!bio_flagged(bio
, BIO_BOUNCED
));
499 bio
= bio
->bi_private
;
503 * make sure we dirty pages we wrote to
505 __bio_for_each_segment(bvec
, bio
, i
, 0) {
507 set_page_dirty_lock(bvec
->bv_page
);
509 page_cache_release(bvec
->bv_page
);
516 * bio_unmap_user - unmap a bio
517 * @bio: the bio being unmapped
518 * @write_to_vm: bool indicating whether pages were written to
520 * Unmap a bio previously mapped by bio_map_user(). The @write_to_vm
521 * must be the same as passed into bio_map_user(). Must be called with
524 * bio_unmap_user() may sleep.
526 void bio_unmap_user(struct bio
*bio
, int write_to_vm
)
528 __bio_unmap_user(bio
, write_to_vm
);
533 * bio_set_pages_dirty() and bio_check_pages_dirty() are support functions
534 * for performing direct-IO in BIOs.
536 * The problem is that we cannot run set_page_dirty() from interrupt context
537 * because the required locks are not interrupt-safe. So what we can do is to
538 * mark the pages dirty _before_ performing IO. And in interrupt context,
539 * check that the pages are still dirty. If so, fine. If not, redirty them
540 * in process context.
542 * We special-case compound pages here: normally this means reads into hugetlb
543 * pages. The logic in here doesn't really work right for compound pages
544 * because the VM does not uniformly chase down the head page in all cases.
545 * But dirtiness of compound pages is pretty meaningless anyway: the VM doesn't
546 * handle them at all. So we skip compound pages here at an early stage.
548 * Note that this code is very hard to test under normal circumstances because
549 * direct-io pins the pages with get_user_pages(). This makes
550 * is_page_cache_freeable return false, and the VM will not clean the pages.
551 * But other code (eg, pdflush) could clean the pages if they are mapped
554 * Simply disabling the call to bio_set_pages_dirty() is a good way to test the
555 * deferred bio dirtying paths.
559 * bio_set_pages_dirty() will mark all the bio's pages as dirty.
561 void bio_set_pages_dirty(struct bio
*bio
)
563 struct bio_vec
*bvec
= bio
->bi_io_vec
;
566 for (i
= 0; i
< bio
->bi_vcnt
; i
++) {
567 struct page
*page
= bvec
[i
].bv_page
;
569 if (page
&& !PageCompound(page
))
570 set_page_dirty_lock(page
);
574 static void bio_release_pages(struct bio
*bio
)
576 struct bio_vec
*bvec
= bio
->bi_io_vec
;
579 for (i
= 0; i
< bio
->bi_vcnt
; i
++) {
580 struct page
*page
= bvec
[i
].bv_page
;
588 * bio_check_pages_dirty() will check that all the BIO's pages are still dirty.
589 * If they are, then fine. If, however, some pages are clean then they must
590 * have been written out during the direct-IO read. So we take another ref on
591 * the BIO and the offending pages and re-dirty the pages in process context.
593 * It is expected that bio_check_pages_dirty() will wholly own the BIO from
594 * here on. It will run one page_cache_release() against each page and will
595 * run one bio_put() against the BIO.
598 static void bio_dirty_fn(void *data
);
600 static DECLARE_WORK(bio_dirty_work
, bio_dirty_fn
, NULL
);
601 static spinlock_t bio_dirty_lock
= SPIN_LOCK_UNLOCKED
;
602 static struct bio
*bio_dirty_list
;
605 * This runs in process context
607 static void bio_dirty_fn(void *data
)
612 spin_lock_irqsave(&bio_dirty_lock
, flags
);
613 bio
= bio_dirty_list
;
614 bio_dirty_list
= NULL
;
615 spin_unlock_irqrestore(&bio_dirty_lock
, flags
);
618 struct bio
*next
= bio
->bi_private
;
620 bio_set_pages_dirty(bio
);
621 bio_release_pages(bio
);
627 void bio_check_pages_dirty(struct bio
*bio
)
629 struct bio_vec
*bvec
= bio
->bi_io_vec
;
630 int nr_clean_pages
= 0;
633 for (i
= 0; i
< bio
->bi_vcnt
; i
++) {
634 struct page
*page
= bvec
[i
].bv_page
;
636 if (PageDirty(page
) || PageCompound(page
)) {
637 page_cache_release(page
);
638 bvec
[i
].bv_page
= NULL
;
644 if (nr_clean_pages
) {
647 spin_lock_irqsave(&bio_dirty_lock
, flags
);
648 bio
->bi_private
= bio_dirty_list
;
649 bio_dirty_list
= bio
;
650 spin_unlock_irqrestore(&bio_dirty_lock
, flags
);
651 schedule_work(&bio_dirty_work
);
658 * bio_endio - end I/O on a bio
660 * @bytes_done: number of bytes completed
661 * @error: error, if any
664 * bio_endio() will end I/O on @bytes_done number of bytes. This may be
665 * just a partial part of the bio, or it may be the whole bio. bio_endio()
666 * is the preferred way to end I/O on a bio, it takes care of decrementing
667 * bi_size and clearing BIO_UPTODATE on error. @error is 0 on success, and
668 * and one of the established -Exxxx (-EIO, for instance) error values in
669 * case something went wrong. Noone should call bi_end_io() directly on
670 * a bio unless they own it and thus know that it has an end_io function.
672 void bio_endio(struct bio
*bio
, unsigned int bytes_done
, int error
)
675 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
677 if (unlikely(bytes_done
> bio
->bi_size
)) {
678 printk("%s: want %u bytes done, only %u left\n", __FUNCTION__
,
679 bytes_done
, bio
->bi_size
);
680 bytes_done
= bio
->bi_size
;
683 bio
->bi_size
-= bytes_done
;
684 bio
->bi_sector
+= (bytes_done
>> 9);
687 bio
->bi_end_io(bio
, bytes_done
, error
);
690 void bio_pair_release(struct bio_pair
*bp
)
692 if (atomic_dec_and_test(&bp
->cnt
)) {
693 struct bio
*master
= bp
->bio1
.bi_private
;
695 bio_endio(master
, master
->bi_size
, bp
->error
);
696 mempool_free(bp
, bp
->bio2
.bi_private
);
700 static int bio_pair_end_1(struct bio
* bi
, unsigned int done
, int err
)
702 struct bio_pair
*bp
= container_of(bi
, struct bio_pair
, bio1
);
710 bio_pair_release(bp
);
714 static int bio_pair_end_2(struct bio
* bi
, unsigned int done
, int err
)
716 struct bio_pair
*bp
= container_of(bi
, struct bio_pair
, bio2
);
724 bio_pair_release(bp
);
729 * split a bio - only worry about a bio with a single page
732 struct bio_pair
*bio_split(struct bio
*bi
, mempool_t
*pool
, int first_sectors
)
734 struct bio_pair
*bp
= mempool_alloc(pool
, GFP_NOIO
);
739 BUG_ON(bi
->bi_vcnt
!= 1);
740 BUG_ON(bi
->bi_idx
!= 0);
741 atomic_set(&bp
->cnt
, 3);
745 bp
->bio2
.bi_sector
+= first_sectors
;
746 bp
->bio2
.bi_size
-= first_sectors
<< 9;
747 bp
->bio1
.bi_size
= first_sectors
<< 9;
749 bp
->bv1
= bi
->bi_io_vec
[0];
750 bp
->bv2
= bi
->bi_io_vec
[0];
751 bp
->bv2
.bv_offset
+= first_sectors
<< 9;
752 bp
->bv2
.bv_len
-= first_sectors
<< 9;
753 bp
->bv1
.bv_len
= first_sectors
<< 9;
755 bp
->bio1
.bi_io_vec
= &bp
->bv1
;
756 bp
->bio2
.bi_io_vec
= &bp
->bv2
;
758 bp
->bio1
.bi_end_io
= bio_pair_end_1
;
759 bp
->bio2
.bi_end_io
= bio_pair_end_2
;
761 bp
->bio1
.bi_private
= bi
;
762 bp
->bio2
.bi_private
= pool
;
767 static void *bio_pair_alloc(int gfp_flags
, void *data
)
769 return kmalloc(sizeof(struct bio_pair
), gfp_flags
);
772 static void bio_pair_free(void *bp
, void *data
)
777 static void __init
biovec_init_pools(void)
779 int i
, size
, megabytes
, pool_entries
= BIO_POOL_SIZE
;
780 int scale
= BIOVEC_NR_POOLS
;
782 megabytes
= nr_free_pages() >> (20 - PAGE_SHIFT
);
785 * find out where to start scaling
789 else if (megabytes
<= 32)
791 else if (megabytes
<= 64)
793 else if (megabytes
<= 96)
795 else if (megabytes
<= 128)
799 * scale number of entries
801 pool_entries
= megabytes
* 2;
802 if (pool_entries
> 256)
805 for (i
= 0; i
< BIOVEC_NR_POOLS
; i
++) {
806 struct biovec_pool
*bp
= bvec_array
+ i
;
808 size
= bp
->nr_vecs
* sizeof(struct bio_vec
);
810 bp
->slab
= kmem_cache_create(bp
->name
, size
, 0,
811 SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
813 panic("biovec: can't init slab cache\n");
818 bp
->pool
= mempool_create(pool_entries
, mempool_alloc_slab
,
819 mempool_free_slab
, bp
->slab
);
821 panic("biovec: can't init mempool\n");
825 static int __init
init_bio(void)
827 bio_slab
= kmem_cache_create("bio", sizeof(struct bio
), 0,
828 SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
830 panic("bio: can't create slab cache\n");
831 bio_pool
= mempool_create(BIO_POOL_SIZE
, mempool_alloc_slab
, mempool_free_slab
, bio_slab
);
833 panic("bio: can't create mempool\n");
837 bio_split_pool
= mempool_create(BIO_SPLIT_ENTRIES
, bio_pair_alloc
, bio_pair_free
, NULL
);
839 panic("bio: can't create split pool\n");
844 subsys_initcall(init_bio
);
846 EXPORT_SYMBOL(bio_alloc
);
847 EXPORT_SYMBOL(bio_put
);
848 EXPORT_SYMBOL(bio_endio
);
849 EXPORT_SYMBOL(bio_init
);
850 EXPORT_SYMBOL(__bio_clone
);
851 EXPORT_SYMBOL(bio_clone
);
852 EXPORT_SYMBOL(bio_phys_segments
);
853 EXPORT_SYMBOL(bio_hw_segments
);
854 EXPORT_SYMBOL(bio_add_page
);
855 EXPORT_SYMBOL(bio_get_nr_vecs
);
856 EXPORT_SYMBOL(bio_map_user
);
857 EXPORT_SYMBOL(bio_unmap_user
);
858 EXPORT_SYMBOL(bio_pair_release
);
859 EXPORT_SYMBOL(bio_split
);
860 EXPORT_SYMBOL(bio_split_pool
);