VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / fs / bio.c
1 /*
2  * Copyright (C) 2001 Jens Axboe <axboe@suse.de>
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/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>
28
29 #define BIO_POOL_SIZE 256
30
31 static mempool_t *bio_pool;
32 static kmem_cache_t *bio_slab;
33
34 #define BIOVEC_NR_POOLS 6
35
36 /*
37  * a small number of entries is fine, not going to be performance critical.
38  * basically we just need to survive
39  */
40 #define BIO_SPLIT_ENTRIES 8     
41 mempool_t *bio_split_pool;
42
43 struct biovec_pool {
44         int nr_vecs;
45         char *name; 
46         kmem_cache_t *slab;
47         mempool_t *pool;
48 };
49
50 /*
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
53  * unsigned short
54  */
55
56 #define BV(x) { .nr_vecs = x, .name = "biovec-"__stringify(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),
59 };
60 #undef BV
61
62 static inline struct bio_vec *bvec_alloc(int gfp_mask, int nr, unsigned long *idx)
63 {
64         struct biovec_pool *bp;
65         struct bio_vec *bvl;
66
67         /*
68          * see comment near bvec_array define!
69          */
70         switch (nr) {
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;
77                 default:
78                         return NULL;
79         }
80         /*
81          * idx now points to the pool we want to allocate from
82          */
83         bp = bvec_array + *idx;
84
85         bvl = mempool_alloc(bp->pool, gfp_mask);
86         if (bvl)
87                 memset(bvl, 0, bp->nr_vecs * sizeof(struct bio_vec));
88         return bvl;
89 }
90
91 /*
92  * default destructor for a bio allocated with bio_alloc()
93  */
94 void bio_destructor(struct bio *bio)
95 {
96         const int pool_idx = BIO_POOL_IDX(bio);
97         struct biovec_pool *bp = bvec_array + pool_idx;
98
99         BIO_BUG_ON(pool_idx >= BIOVEC_NR_POOLS);
100
101         /*
102          * cloned bio doesn't own the veclist
103          */
104         if (!bio_flagged(bio, BIO_CLONED))
105                 mempool_free(bio->bi_io_vec, bp->pool);
106
107         mempool_free(bio, bio_pool);
108 }
109
110 inline void bio_init(struct bio *bio)
111 {
112         bio->bi_next = NULL;
113         bio->bi_flags = 1 << BIO_UPTODATE;
114         bio->bi_rw = 0;
115         bio->bi_vcnt = 0;
116         bio->bi_idx = 0;
117         bio->bi_phys_segments = 0;
118         bio->bi_hw_segments = 0;
119         bio->bi_hw_front_size = 0;
120         bio->bi_hw_back_size = 0;
121         bio->bi_size = 0;
122         bio->bi_max_vecs = 0;
123         bio->bi_end_io = NULL;
124         atomic_set(&bio->bi_cnt, 1);
125         bio->bi_private = NULL;
126 }
127
128 /**
129  * bio_alloc - allocate a bio for I/O
130  * @gfp_mask:   the GFP_ mask given to the slab allocator
131  * @nr_iovecs:  number of iovecs to pre-allocate
132  *
133  * Description:
134  *   bio_alloc will first try it's on mempool to satisfy the allocation.
135  *   If %__GFP_WAIT is set then we will block on the internal pool waiting
136  *   for a &struct bio to become free.
137  **/
138 struct bio *bio_alloc(int gfp_mask, int nr_iovecs)
139 {
140         struct bio_vec *bvl = NULL;
141         unsigned long idx;
142         struct bio *bio;
143
144         bio = mempool_alloc(bio_pool, gfp_mask);
145         if (unlikely(!bio))
146                 goto out;
147
148         bio_init(bio);
149
150         if (unlikely(!nr_iovecs))
151                 goto noiovec;
152
153         bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx);
154         if (bvl) {
155                 bio->bi_flags |= idx << BIO_POOL_OFFSET;
156                 bio->bi_max_vecs = bvec_array[idx].nr_vecs;
157 noiovec:
158                 bio->bi_io_vec = bvl;
159                 bio->bi_destructor = bio_destructor;
160 out:
161                 return bio;
162         }
163
164         mempool_free(bio, bio_pool);
165         bio = NULL;
166         goto out;
167 }
168
169 /**
170  * bio_put - release a reference to a bio
171  * @bio:   bio to release reference to
172  *
173  * Description:
174  *   Put a reference to a &struct bio, either one you have gotten with
175  *   bio_alloc or bio_get. The last put of a bio will free it.
176  **/
177 void bio_put(struct bio *bio)
178 {
179         BIO_BUG_ON(!atomic_read(&bio->bi_cnt));
180
181         /*
182          * last put frees it
183          */
184         if (atomic_dec_and_test(&bio->bi_cnt)) {
185                 bio->bi_next = NULL;
186                 bio->bi_destructor(bio);
187         }
188 }
189
190 inline int bio_phys_segments(request_queue_t *q, struct bio *bio)
191 {
192         if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
193                 blk_recount_segments(q, bio);
194
195         return bio->bi_phys_segments;
196 }
197
198 inline int bio_hw_segments(request_queue_t *q, struct bio *bio)
199 {
200         if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
201                 blk_recount_segments(q, bio);
202
203         return bio->bi_hw_segments;
204 }
205
206 /**
207  *      __bio_clone     -       clone a bio
208  *      @bio: destination bio
209  *      @bio_src: bio to clone
210  *
211  *      Clone a &bio. Caller will own the returned bio, but not
212  *      the actual data it points to. Reference count of returned
213  *      bio will be one.
214  */
215 inline void __bio_clone(struct bio *bio, struct bio *bio_src)
216 {
217         bio->bi_io_vec = bio_src->bi_io_vec;
218
219         bio->bi_sector = bio_src->bi_sector;
220         bio->bi_bdev = bio_src->bi_bdev;
221         bio->bi_flags |= 1 << BIO_CLONED;
222         bio->bi_rw = bio_src->bi_rw;
223
224         /*
225          * notes -- maybe just leave bi_idx alone. assume identical mapping
226          * for the clone
227          */
228         bio->bi_vcnt = bio_src->bi_vcnt;
229         bio->bi_idx = bio_src->bi_idx;
230         if (bio_flagged(bio, BIO_SEG_VALID)) {
231                 bio->bi_phys_segments = bio_src->bi_phys_segments;
232                 bio->bi_hw_segments = bio_src->bi_hw_segments;
233                 bio->bi_flags |= (1 << BIO_SEG_VALID);
234         }
235         bio->bi_size = bio_src->bi_size;
236
237         /*
238          * cloned bio does not own the bio_vec, so users cannot fiddle with
239          * it. clear bi_max_vecs and clear the BIO_POOL_BITS to make this
240          * apparent
241          */
242         bio->bi_max_vecs = 0;
243         bio->bi_flags &= (BIO_POOL_MASK - 1);
244 }
245
246 /**
247  *      bio_clone       -       clone a bio
248  *      @bio: bio to clone
249  *      @gfp_mask: allocation priority
250  *
251  *      Like __bio_clone, only also allocates the returned bio
252  */
253 struct bio *bio_clone(struct bio *bio, int gfp_mask)
254 {
255         struct bio *b = bio_alloc(gfp_mask, 0);
256
257         if (b)
258                 __bio_clone(b, bio);
259
260         return b;
261 }
262
263 /**
264  *      bio_get_nr_vecs         - return approx number of vecs
265  *      @bdev:  I/O target
266  *
267  *      Return the approximate number of pages we can send to this target.
268  *      There's no guarantee that you will be able to fit this number of pages
269  *      into a bio, it does not account for dynamic restrictions that vary
270  *      on offset.
271  */
272 int bio_get_nr_vecs(struct block_device *bdev)
273 {
274         request_queue_t *q = bdev_get_queue(bdev);
275         int nr_pages;
276
277         nr_pages = ((q->max_sectors << 9) + PAGE_SIZE - 1) >> PAGE_SHIFT;
278         if (nr_pages > q->max_phys_segments)
279                 nr_pages = q->max_phys_segments;
280         if (nr_pages > q->max_hw_segments)
281                 nr_pages = q->max_hw_segments;
282
283         return nr_pages;
284 }
285
286 static int __bio_add_page(request_queue_t *q, struct bio *bio, struct page
287                           *page, unsigned int len, unsigned int offset)
288 {
289         int retried_segments = 0;
290         struct bio_vec *bvec;
291
292         /*
293          * cloned bio must not modify vec list
294          */
295         if (unlikely(bio_flagged(bio, BIO_CLONED)))
296                 return 0;
297
298         if (bio->bi_vcnt >= bio->bi_max_vecs)
299                 return 0;
300
301         if (((bio->bi_size + len) >> 9) > q->max_sectors)
302                 return 0;
303
304         /*
305          * we might lose a segment or two here, but rather that than
306          * make this too complex.
307          */
308
309         while (bio->bi_phys_segments >= q->max_phys_segments
310                || bio->bi_hw_segments >= q->max_hw_segments
311                || BIOVEC_VIRT_OVERSIZE(bio->bi_size)) {
312
313                 if (retried_segments)
314                         return 0;
315
316                 retried_segments = 1;
317                 blk_recount_segments(q, bio);
318         }
319
320         /*
321          * setup the new entry, we might clear it again later if we
322          * cannot add the page
323          */
324         bvec = &bio->bi_io_vec[bio->bi_vcnt];
325         bvec->bv_page = page;
326         bvec->bv_len = len;
327         bvec->bv_offset = offset;
328
329         /*
330          * if queue has other restrictions (eg varying max sector size
331          * depending on offset), it can specify a merge_bvec_fn in the
332          * queue to get further control
333          */
334         if (q->merge_bvec_fn) {
335                 /*
336                  * merge_bvec_fn() returns number of bytes it can accept
337                  * at this offset
338                  */
339                 if (q->merge_bvec_fn(q, bio, bvec) < len) {
340                         bvec->bv_page = NULL;
341                         bvec->bv_len = 0;
342                         bvec->bv_offset = 0;
343                         return 0;
344                 }
345         }
346
347         /* If we may be able to merge these biovecs, force a recount */
348         if (bio->bi_vcnt && (BIOVEC_PHYS_MERGEABLE(bvec-1, bvec) ||
349             BIOVEC_VIRT_MERGEABLE(bvec-1, bvec)))
350                 bio->bi_flags &= ~(1 << BIO_SEG_VALID);
351
352         bio->bi_vcnt++;
353         bio->bi_phys_segments++;
354         bio->bi_hw_segments++;
355         bio->bi_size += len;
356         return len;
357 }
358
359 /**
360  *      bio_add_page    -       attempt to add page to bio
361  *      @bio: destination bio
362  *      @page: page to add
363  *      @len: vec entry length
364  *      @offset: vec entry offset
365  *
366  *      Attempt to add a page to the bio_vec maplist. This can fail for a
367  *      number of reasons, such as the bio being full or target block
368  *      device limitations. The target block device must allow bio's
369  *      smaller than PAGE_SIZE, so it is always possible to add a single
370  *      page to an empty bio.
371  */
372 int bio_add_page(struct bio *bio, struct page *page, unsigned int len,
373                  unsigned int offset)
374 {
375         return __bio_add_page(bdev_get_queue(bio->bi_bdev), bio, page,
376                               len, offset);
377 }
378
379 /**
380  *      bio_uncopy_user -       finish previously mapped bio
381  *      @bio: bio being terminated
382  *
383  *      Free pages allocated from bio_copy_user() and write back data
384  *      to user space in case of a read.
385  */
386 int bio_uncopy_user(struct bio *bio)
387 {
388         struct bio_vec *bvec;
389         int i, ret = 0;
390
391         if (bio_data_dir(bio) == READ) {
392                 char *uaddr = bio->bi_private;
393
394                 __bio_for_each_segment(bvec, bio, i, 0) {
395                         char *addr = page_address(bvec->bv_page);
396
397                         if (!ret && copy_to_user(uaddr, addr, bvec->bv_len))
398                                 ret = -EFAULT;
399
400                         __free_page(bvec->bv_page);
401                         uaddr += bvec->bv_len;
402                 }
403         }
404
405         bio_put(bio);
406         return ret;
407 }
408
409 /**
410  *      bio_copy_user   -       copy user data to bio
411  *      @q: destination block queue
412  *      @uaddr: start of user address
413  *      @len: length in bytes
414  *      @write_to_vm: bool indicating writing to pages or not
415  *
416  *      Prepares and returns a bio for indirect user io, bouncing data
417  *      to/from kernel pages as necessary. Must be paired with
418  *      call bio_uncopy_user() on io completion.
419  */
420 struct bio *bio_copy_user(request_queue_t *q, unsigned long uaddr,
421                           unsigned int len, int write_to_vm)
422 {
423         unsigned long end = (uaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
424         unsigned long start = uaddr >> PAGE_SHIFT;
425         struct bio_vec *bvec;
426         struct page *page;
427         struct bio *bio;
428         int i, ret;
429
430         bio = bio_alloc(GFP_KERNEL, end - start);
431         if (!bio)
432                 return ERR_PTR(-ENOMEM);
433
434         ret = 0;
435         while (len) {
436                 unsigned int bytes = PAGE_SIZE;
437
438                 if (bytes > len)
439                         bytes = len;
440
441                 page = alloc_page(q->bounce_gfp | GFP_KERNEL);
442                 if (!page) {
443                         ret = -ENOMEM;
444                         break;
445                 }
446
447                 if (__bio_add_page(q, bio, page, bytes, 0) < bytes) {
448                         ret = -EINVAL;
449                         break;
450                 }
451
452                 len -= bytes;
453         }
454
455         /*
456          * success
457          */
458         if (!ret) {
459                 if (!write_to_vm) {
460                         bio->bi_rw |= (1 << BIO_RW);
461                         /*
462                          * for a write, copy in data to kernel pages
463                          */
464                         ret = -EFAULT;
465                         bio_for_each_segment(bvec, bio, i) {
466                                 char *addr = page_address(bvec->bv_page);
467
468                                 if (copy_from_user(addr, (char *) uaddr, bvec->bv_len))
469                                         goto cleanup;
470                         }
471                 }
472
473                 bio->bi_private = (void *) uaddr;
474                 return bio;
475         }
476
477         /*
478          * cleanup
479          */
480 cleanup:
481         bio_for_each_segment(bvec, bio, i)
482                 __free_page(bvec->bv_page);
483
484         bio_put(bio);
485         return ERR_PTR(ret);
486 }
487
488 static struct bio *__bio_map_user(request_queue_t *q, struct block_device *bdev,
489                                   unsigned long uaddr, unsigned int len,
490                                   int write_to_vm)
491 {
492         unsigned long end = (uaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
493         unsigned long start = uaddr >> PAGE_SHIFT;
494         const int nr_pages = end - start;
495         int ret, offset, i;
496         struct page **pages;
497         struct bio *bio;
498
499         /*
500          * transfer and buffer must be aligned to at least hardsector
501          * size for now, in the future we can relax this restriction
502          */
503         if ((uaddr & queue_dma_alignment(q)) || (len & queue_dma_alignment(q)))
504                 return ERR_PTR(-EINVAL);
505
506         bio = bio_alloc(GFP_KERNEL, nr_pages);
507         if (!bio)
508                 return ERR_PTR(-ENOMEM);
509
510         ret = -ENOMEM;
511         pages = kmalloc(nr_pages * sizeof(struct page *), GFP_KERNEL);
512         if (!pages)
513                 goto out;
514
515         down_read(&current->mm->mmap_sem);
516         ret = get_user_pages(current, current->mm, uaddr, nr_pages,
517                                                 write_to_vm, 0, pages, NULL);
518         up_read(&current->mm->mmap_sem);
519
520         if (ret < nr_pages)
521                 goto out;
522
523         bio->bi_bdev = bdev;
524
525         offset = uaddr & ~PAGE_MASK;
526         for (i = 0; i < nr_pages; i++) {
527                 unsigned int bytes = PAGE_SIZE - offset;
528
529                 if (len <= 0)
530                         break;
531
532                 if (bytes > len)
533                         bytes = len;
534
535                 /*
536                  * sorry...
537                  */
538                 if (__bio_add_page(q, bio, pages[i], bytes, offset) < bytes)
539                         break;
540
541                 len -= bytes;
542                 offset = 0;
543         }
544
545         /*
546          * release the pages we didn't map into the bio, if any
547          */
548         while (i < nr_pages)
549                 page_cache_release(pages[i++]);
550
551         kfree(pages);
552
553         /*
554          * set data direction, and check if mapped pages need bouncing
555          */
556         if (!write_to_vm)
557                 bio->bi_rw |= (1 << BIO_RW);
558
559         bio->bi_flags |= (1 << BIO_USER_MAPPED);
560         return bio;
561 out:
562         kfree(pages);
563         bio_put(bio);
564         return ERR_PTR(ret);
565 }
566
567 /**
568  *      bio_map_user    -       map user address into bio
569  *      @bdev: destination block device
570  *      @uaddr: start of user address
571  *      @len: length in bytes
572  *      @write_to_vm: bool indicating writing to pages or not
573  *
574  *      Map the user space address into a bio suitable for io to a block
575  *      device. Returns an error pointer in case of error.
576  */
577 struct bio *bio_map_user(request_queue_t *q, struct block_device *bdev,
578                          unsigned long uaddr, unsigned int len, int write_to_vm)
579 {
580         struct bio *bio;
581
582         bio = __bio_map_user(q, bdev, uaddr, len, write_to_vm);
583
584         if (IS_ERR(bio))
585                 return bio;
586
587         /*
588          * subtle -- if __bio_map_user() ended up bouncing a bio,
589          * it would normally disappear when its bi_end_io is run.
590          * however, we need it for the unmap, so grab an extra
591          * reference to it
592          */
593         bio_get(bio);
594
595         if (bio->bi_size == len)
596                 return bio;
597
598         /*
599          * don't support partial mappings
600          */
601         bio_endio(bio, bio->bi_size, 0);
602         bio_unmap_user(bio);
603         return ERR_PTR(-EINVAL);
604 }
605
606 static void __bio_unmap_user(struct bio *bio)
607 {
608         struct bio_vec *bvec;
609         int i;
610
611         /*
612          * find original bio if it was bounced
613          */
614         if (bio->bi_private) {
615                 /*
616                  * someone stole our bio, must not happen
617                  */
618                 BUG_ON(!bio_flagged(bio, BIO_BOUNCED));
619         
620                 bio = bio->bi_private;
621         }
622
623         /*
624          * make sure we dirty pages we wrote to
625          */
626         __bio_for_each_segment(bvec, bio, i, 0) {
627                 if (bio_data_dir(bio) == READ)
628                         set_page_dirty_lock(bvec->bv_page);
629
630                 page_cache_release(bvec->bv_page);
631         }
632
633         bio_put(bio);
634 }
635
636 /**
637  *      bio_unmap_user  -       unmap a bio
638  *      @bio:           the bio being unmapped
639  *
640  *      Unmap a bio previously mapped by bio_map_user(). Must be called with
641  *      a process context.
642  *
643  *      bio_unmap_user() may sleep.
644  */
645 void bio_unmap_user(struct bio *bio)
646 {
647         __bio_unmap_user(bio);
648         bio_put(bio);
649 }
650
651 /*
652  * bio_set_pages_dirty() and bio_check_pages_dirty() are support functions
653  * for performing direct-IO in BIOs.
654  *
655  * The problem is that we cannot run set_page_dirty() from interrupt context
656  * because the required locks are not interrupt-safe.  So what we can do is to
657  * mark the pages dirty _before_ performing IO.  And in interrupt context,
658  * check that the pages are still dirty.   If so, fine.  If not, redirty them
659  * in process context.
660  *
661  * We special-case compound pages here: normally this means reads into hugetlb
662  * pages.  The logic in here doesn't really work right for compound pages
663  * because the VM does not uniformly chase down the head page in all cases.
664  * But dirtiness of compound pages is pretty meaningless anyway: the VM doesn't
665  * handle them at all.  So we skip compound pages here at an early stage.
666  *
667  * Note that this code is very hard to test under normal circumstances because
668  * direct-io pins the pages with get_user_pages().  This makes
669  * is_page_cache_freeable return false, and the VM will not clean the pages.
670  * But other code (eg, pdflush) could clean the pages if they are mapped
671  * pagecache.
672  *
673  * Simply disabling the call to bio_set_pages_dirty() is a good way to test the
674  * deferred bio dirtying paths.
675  */
676
677 /*
678  * bio_set_pages_dirty() will mark all the bio's pages as dirty.
679  */
680 void bio_set_pages_dirty(struct bio *bio)
681 {
682         struct bio_vec *bvec = bio->bi_io_vec;
683         int i;
684
685         for (i = 0; i < bio->bi_vcnt; i++) {
686                 struct page *page = bvec[i].bv_page;
687
688                 if (page && !PageCompound(page))
689                         set_page_dirty_lock(page);
690         }
691 }
692
693 static void bio_release_pages(struct bio *bio)
694 {
695         struct bio_vec *bvec = bio->bi_io_vec;
696         int i;
697
698         for (i = 0; i < bio->bi_vcnt; i++) {
699                 struct page *page = bvec[i].bv_page;
700
701                 if (page)
702                         put_page(page);
703         }
704 }
705
706 /*
707  * bio_check_pages_dirty() will check that all the BIO's pages are still dirty.
708  * If they are, then fine.  If, however, some pages are clean then they must
709  * have been written out during the direct-IO read.  So we take another ref on
710  * the BIO and the offending pages and re-dirty the pages in process context.
711  *
712  * It is expected that bio_check_pages_dirty() will wholly own the BIO from
713  * here on.  It will run one page_cache_release() against each page and will
714  * run one bio_put() against the BIO.
715  */
716
717 static void bio_dirty_fn(void *data);
718
719 static DECLARE_WORK(bio_dirty_work, bio_dirty_fn, NULL);
720 static spinlock_t bio_dirty_lock = SPIN_LOCK_UNLOCKED;
721 static struct bio *bio_dirty_list;
722
723 /*
724  * This runs in process context
725  */
726 static void bio_dirty_fn(void *data)
727 {
728         unsigned long flags;
729         struct bio *bio;
730
731         spin_lock_irqsave(&bio_dirty_lock, flags);
732         bio = bio_dirty_list;
733         bio_dirty_list = NULL;
734         spin_unlock_irqrestore(&bio_dirty_lock, flags);
735
736         while (bio) {
737                 struct bio *next = bio->bi_private;
738
739                 bio_set_pages_dirty(bio);
740                 bio_release_pages(bio);
741                 bio_put(bio);
742                 bio = next;
743         }
744 }
745
746 void bio_check_pages_dirty(struct bio *bio)
747 {
748         struct bio_vec *bvec = bio->bi_io_vec;
749         int nr_clean_pages = 0;
750         int i;
751
752         for (i = 0; i < bio->bi_vcnt; i++) {
753                 struct page *page = bvec[i].bv_page;
754
755                 if (PageDirty(page) || PageCompound(page)) {
756                         page_cache_release(page);
757                         bvec[i].bv_page = NULL;
758                 } else {
759                         nr_clean_pages++;
760                 }
761         }
762
763         if (nr_clean_pages) {
764                 unsigned long flags;
765
766                 spin_lock_irqsave(&bio_dirty_lock, flags);
767                 bio->bi_private = bio_dirty_list;
768                 bio_dirty_list = bio;
769                 spin_unlock_irqrestore(&bio_dirty_lock, flags);
770                 schedule_work(&bio_dirty_work);
771         } else {
772                 bio_put(bio);
773         }
774 }
775
776 /**
777  * bio_endio - end I/O on a bio
778  * @bio:        bio
779  * @bytes_done: number of bytes completed
780  * @error:      error, if any
781  *
782  * Description:
783  *   bio_endio() will end I/O on @bytes_done number of bytes. This may be
784  *   just a partial part of the bio, or it may be the whole bio. bio_endio()
785  *   is the preferred way to end I/O on a bio, it takes care of decrementing
786  *   bi_size and clearing BIO_UPTODATE on error. @error is 0 on success, and
787  *   and one of the established -Exxxx (-EIO, for instance) error values in
788  *   case something went wrong. Noone should call bi_end_io() directly on
789  *   a bio unless they own it and thus know that it has an end_io function.
790  **/
791 void bio_endio(struct bio *bio, unsigned int bytes_done, int error)
792 {
793         if (error)
794                 clear_bit(BIO_UPTODATE, &bio->bi_flags);
795
796         if (unlikely(bytes_done > bio->bi_size)) {
797                 printk("%s: want %u bytes done, only %u left\n", __FUNCTION__,
798                                                 bytes_done, bio->bi_size);
799                 bytes_done = bio->bi_size;
800         }
801
802         bio->bi_size -= bytes_done;
803         bio->bi_sector += (bytes_done >> 9);
804
805         if (bio->bi_end_io)
806                 bio->bi_end_io(bio, bytes_done, error);
807 }
808
809 void bio_pair_release(struct bio_pair *bp)
810 {
811         if (atomic_dec_and_test(&bp->cnt)) {
812                 struct bio *master = bp->bio1.bi_private;
813
814                 bio_endio(master, master->bi_size, bp->error);
815                 mempool_free(bp, bp->bio2.bi_private);
816         }
817 }
818
819 static int bio_pair_end_1(struct bio * bi, unsigned int done, int err)
820 {
821         struct bio_pair *bp = container_of(bi, struct bio_pair, bio1);
822
823         if (err)
824                 bp->error = err;
825
826         if (bi->bi_size)
827                 return 1;
828
829         bio_pair_release(bp);
830         return 0;
831 }
832
833 static int bio_pair_end_2(struct bio * bi, unsigned int done, int err)
834 {
835         struct bio_pair *bp = container_of(bi, struct bio_pair, bio2);
836
837         if (err)
838                 bp->error = err;
839
840         if (bi->bi_size)
841                 return 1;
842
843         bio_pair_release(bp);
844         return 0;
845 }
846
847 /*
848  * split a bio - only worry about a bio with a single page
849  * in it's iovec
850  */
851 struct bio_pair *bio_split(struct bio *bi, mempool_t *pool, int first_sectors)
852 {
853         struct bio_pair *bp = mempool_alloc(pool, GFP_NOIO);
854
855         if (!bp)
856                 return bp;
857
858         BUG_ON(bi->bi_vcnt != 1);
859         BUG_ON(bi->bi_idx != 0);
860         atomic_set(&bp->cnt, 3);
861         bp->error = 0;
862         bp->bio1 = *bi;
863         bp->bio2 = *bi;
864         bp->bio2.bi_sector += first_sectors;
865         bp->bio2.bi_size -= first_sectors << 9;
866         bp->bio1.bi_size = first_sectors << 9;
867
868         bp->bv1 = bi->bi_io_vec[0];
869         bp->bv2 = bi->bi_io_vec[0];
870         bp->bv2.bv_offset += first_sectors << 9;
871         bp->bv2.bv_len -= first_sectors << 9;
872         bp->bv1.bv_len = first_sectors << 9;
873
874         bp->bio1.bi_io_vec = &bp->bv1;
875         bp->bio2.bi_io_vec = &bp->bv2;
876
877         bp->bio1.bi_end_io = bio_pair_end_1;
878         bp->bio2.bi_end_io = bio_pair_end_2;
879
880         bp->bio1.bi_private = bi;
881         bp->bio2.bi_private = pool;
882
883         return bp;
884 }
885
886 static void *bio_pair_alloc(int gfp_flags, void *data)
887 {
888         return kmalloc(sizeof(struct bio_pair), gfp_flags);
889 }
890
891 static void bio_pair_free(void *bp, void *data)
892 {
893         kfree(bp);
894 }
895
896 static void __init biovec_init_pools(void)
897 {
898         int i, size, megabytes, pool_entries = BIO_POOL_SIZE;
899         int scale = BIOVEC_NR_POOLS;
900
901         megabytes = nr_free_pages() >> (20 - PAGE_SHIFT);
902
903         /*
904          * find out where to start scaling
905          */
906         if (megabytes <= 16)
907                 scale = 0;
908         else if (megabytes <= 32)
909                 scale = 1;
910         else if (megabytes <= 64)
911                 scale = 2;
912         else if (megabytes <= 96)
913                 scale = 3;
914         else if (megabytes <= 128)
915                 scale = 4;
916
917         /*
918          * scale number of entries
919          */
920         pool_entries = megabytes * 2;
921         if (pool_entries > 256)
922                 pool_entries = 256;
923
924         for (i = 0; i < BIOVEC_NR_POOLS; i++) {
925                 struct biovec_pool *bp = bvec_array + i;
926
927                 size = bp->nr_vecs * sizeof(struct bio_vec);
928
929                 bp->slab = kmem_cache_create(bp->name, size, 0,
930                                 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
931
932                 if (i >= scale)
933                         pool_entries >>= 1;
934
935                 bp->pool = mempool_create(pool_entries, mempool_alloc_slab,
936                                         mempool_free_slab, bp->slab);
937                 if (!bp->pool)
938                         panic("biovec: can't init mempool\n");
939         }
940 }
941
942 static int __init init_bio(void)
943 {
944         bio_slab = kmem_cache_create("bio", sizeof(struct bio), 0,
945                                 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
946         bio_pool = mempool_create(BIO_POOL_SIZE, mempool_alloc_slab,
947                                 mempool_free_slab, bio_slab);
948         if (!bio_pool)
949                 panic("bio: can't create mempool\n");
950
951         biovec_init_pools();
952
953         bio_split_pool = mempool_create(BIO_SPLIT_ENTRIES,
954                                 bio_pair_alloc, bio_pair_free, NULL);
955         if (!bio_split_pool)
956                 panic("bio: can't create split pool\n");
957
958         return 0;
959 }
960
961 subsys_initcall(init_bio);
962
963 EXPORT_SYMBOL(bio_alloc);
964 EXPORT_SYMBOL(bio_put);
965 EXPORT_SYMBOL(bio_endio);
966 EXPORT_SYMBOL(bio_init);
967 EXPORT_SYMBOL(__bio_clone);
968 EXPORT_SYMBOL(bio_clone);
969 EXPORT_SYMBOL(bio_phys_segments);
970 EXPORT_SYMBOL(bio_hw_segments);
971 EXPORT_SYMBOL(bio_add_page);
972 EXPORT_SYMBOL(bio_get_nr_vecs);
973 EXPORT_SYMBOL(bio_map_user);
974 EXPORT_SYMBOL(bio_unmap_user);
975 EXPORT_SYMBOL(bio_pair_release);
976 EXPORT_SYMBOL(bio_split);
977 EXPORT_SYMBOL(bio_split_pool);
978 EXPORT_SYMBOL(bio_copy_user);
979 EXPORT_SYMBOL(bio_uncopy_user);