Merge branch 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso/ext4
[safe/jmp/linux-2.6] / fs / bio.c
index d4f0632..7673800 100644 (file)
--- a/fs/bio.c
+++ b/fs/bio.c
 #include <linux/module.h>
 #include <linux/mempool.h>
 #include <linux/workqueue.h>
-#include <linux/blktrace_api.h>
-#include <trace/block.h>
 #include <scsi/sg.h>           /* for struct sg_iovec */
 
-DEFINE_TRACE(block_split);
+#include <trace/events/block.h>
 
 /*
  * Test patch to inline a certain number of bi_io_vec's inside the bio
@@ -175,14 +173,6 @@ struct bio_vec *bvec_alloc_bs(gfp_t gfp_mask, int nr, unsigned long *idx,
        struct bio_vec *bvl;
 
        /*
-        * If 'bs' is given, lookup the pool and do the mempool alloc.
-        * If not, this is a bio_kmalloc() allocation and just do a
-        * kzalloc() for the exact number of vecs right away.
-        */
-       if (!bs)
-               bvl = kmalloc(nr * sizeof(struct bio_vec), gfp_mask);
-
-       /*
         * see comment near bvec_array define!
         */
        switch (nr) {
@@ -260,21 +250,6 @@ void bio_free(struct bio *bio, struct bio_set *bs)
        mempool_free(p, bs->bio_pool);
 }
 
-/*
- * default destructor for a bio allocated with bio_alloc_bioset()
- */
-static void bio_fs_destructor(struct bio *bio)
-{
-       bio_free(bio, fs_bio_set);
-}
-
-static void bio_kmalloc_destructor(struct bio *bio)
-{
-       if (bio_has_allocated_vec(bio))
-               kfree(bio->bi_io_vec);
-       kfree(bio);
-}
-
 void bio_init(struct bio *bio)
 {
        memset(bio, 0, sizeof(*bio));
@@ -301,50 +276,58 @@ void bio_init(struct bio *bio)
  **/
 struct bio *bio_alloc_bioset(gfp_t gfp_mask, int nr_iovecs, struct bio_set *bs)
 {
-       struct bio *bio = NULL;
-       void *uninitialized_var(p);
-
-       if (bs) {
-               p = mempool_alloc(bs->bio_pool, gfp_mask);
-
-               if (p)
-                       bio = p + bs->front_pad;
-       } else
-               bio = kmalloc(sizeof(*bio), gfp_mask);
-
-       if (likely(bio)) {
-               struct bio_vec *bvl = NULL;
-
-               bio_init(bio);
-               if (likely(nr_iovecs)) {
-                       unsigned long uninitialized_var(idx);
-
-                       if (nr_iovecs <= BIO_INLINE_VECS) {
-                               idx = 0;
-                               bvl = bio->bi_inline_vecs;
-                               nr_iovecs = BIO_INLINE_VECS;
-                       } else {
-                               bvl = bvec_alloc_bs(gfp_mask, nr_iovecs, &idx,
-                                                       bs);
-                               nr_iovecs = bvec_nr_vecs(idx);
-                       }
-                       if (unlikely(!bvl)) {
-                               if (bs)
-                                       mempool_free(p, bs->bio_pool);
-                               else
-                                       kfree(bio);
-                               bio = NULL;
-                               goto out;
-                       }
-                       bio->bi_flags |= idx << BIO_POOL_OFFSET;
-                       bio->bi_max_vecs = nr_iovecs;
-               }
-               bio->bi_io_vec = bvl;
+       unsigned long idx = BIO_POOL_NONE;
+       struct bio_vec *bvl = NULL;
+       struct bio *bio;
+       void *p;
+
+       p = mempool_alloc(bs->bio_pool, gfp_mask);
+       if (unlikely(!p))
+               return NULL;
+       bio = p + bs->front_pad;
+
+       bio_init(bio);
+
+       if (unlikely(!nr_iovecs))
+               goto out_set;
+
+       if (nr_iovecs <= BIO_INLINE_VECS) {
+               bvl = bio->bi_inline_vecs;
+               nr_iovecs = BIO_INLINE_VECS;
+       } else {
+               bvl = bvec_alloc_bs(gfp_mask, nr_iovecs, &idx, bs);
+               if (unlikely(!bvl))
+                       goto err_free;
+
+               nr_iovecs = bvec_nr_vecs(idx);
        }
-out:
+out_set:
+       bio->bi_flags |= idx << BIO_POOL_OFFSET;
+       bio->bi_max_vecs = nr_iovecs;
+       bio->bi_io_vec = bvl;
        return bio;
+
+err_free:
+       mempool_free(p, bs->bio_pool);
+       return NULL;
+}
+
+static void bio_fs_destructor(struct bio *bio)
+{
+       bio_free(bio, fs_bio_set);
 }
 
+/**
+ *     bio_alloc - allocate a new bio, memory pool backed
+ *     @gfp_mask: allocation mask to use
+ *     @nr_iovecs: number of iovecs
+ *
+ *     Allocate a new bio with @nr_iovecs bvecs.  If @gfp_mask
+ *     contains __GFP_WAIT, the allocation is guaranteed to succeed.
+ *
+ *     RETURNS:
+ *     Pointer to new bio on success, NULL on failure.
+ */
 struct bio *bio_alloc(gfp_t gfp_mask, int nr_iovecs)
 {
        struct bio *bio = bio_alloc_bioset(gfp_mask, nr_iovecs, fs_bio_set);
@@ -355,19 +338,45 @@ struct bio *bio_alloc(gfp_t gfp_mask, int nr_iovecs)
        return bio;
 }
 
-/*
- * Like bio_alloc(), but doesn't use a mempool backing. This means that
- * it CAN fail, but while bio_alloc() can only be used for allocations
- * that have a short (finite) life span, bio_kmalloc() should be used
- * for more permanent bio allocations (like allocating some bio's for
- * initalization or setup purposes).
- */
+static void bio_kmalloc_destructor(struct bio *bio)
+{
+       if (bio_integrity(bio))
+               bio_integrity_free(bio, fs_bio_set);
+       kfree(bio);
+}
+
+/**
+ * bio_alloc - allocate a bio for I/O
+ * @gfp_mask:   the GFP_ mask given to the slab allocator
+ * @nr_iovecs: number of iovecs to pre-allocate
+ *
+ * Description:
+ *   bio_alloc will allocate a bio and associated bio_vec array that can hold
+ *   at least @nr_iovecs entries. Allocations will be done from the
+ *   fs_bio_set. Also see @bio_alloc_bioset.
+ *
+ *   If %__GFP_WAIT is set, then bio_alloc will always be able to allocate
+ *   a bio. This is due to the mempool guarantees. To make this work, callers
+ *   must never allocate more than 1 bio at a time from this pool. Callers
+ *   that need to allocate more than 1 bio must always submit the previously
+ *   allocated bio for IO before attempting to allocate a new one. Failure to
+ *   do so can cause livelocks under memory pressure.
+ *
+ **/
 struct bio *bio_kmalloc(gfp_t gfp_mask, int nr_iovecs)
 {
-       struct bio *bio = bio_alloc_bioset(gfp_mask, nr_iovecs, NULL);
+       struct bio *bio;
 
-       if (bio)
-               bio->bi_destructor = bio_kmalloc_destructor;
+       bio = kmalloc(sizeof(struct bio) + nr_iovecs * sizeof(struct bio_vec),
+                     gfp_mask);
+       if (unlikely(!bio))
+               return NULL;
+
+       bio_init(bio);
+       bio->bi_flags |= BIO_POOL_NONE << BIO_POOL_OFFSET;
+       bio->bi_max_vecs = nr_iovecs;
+       bio->bi_io_vec = bio->bi_inline_vecs;
+       bio->bi_destructor = bio_kmalloc_destructor;
 
        return bio;
 }
@@ -488,11 +497,11 @@ int bio_get_nr_vecs(struct block_device *bdev)
        struct request_queue *q = bdev_get_queue(bdev);
        int nr_pages;
 
-       nr_pages = ((q->max_sectors << 9) + PAGE_SIZE - 1) >> PAGE_SHIFT;
-       if (nr_pages > q->max_phys_segments)
-               nr_pages = q->max_phys_segments;
-       if (nr_pages > q->max_hw_segments)
-               nr_pages = q->max_hw_segments;
+       nr_pages = ((queue_max_sectors(q) << 9) + PAGE_SIZE - 1) >> PAGE_SHIFT;
+       if (nr_pages > queue_max_phys_segments(q))
+               nr_pages = queue_max_phys_segments(q);
+       if (nr_pages > queue_max_hw_segments(q))
+               nr_pages = queue_max_hw_segments(q);
 
        return nr_pages;
 }
@@ -551,8 +560,8 @@ static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
         * make this too complex.
         */
 
-       while (bio->bi_phys_segments >= q->max_phys_segments
-              || bio->bi_phys_segments >= q->max_hw_segments) {
+       while (bio->bi_phys_segments >= queue_max_phys_segments(q)
+              || bio->bi_phys_segments >= queue_max_hw_segments(q)) {
 
                if (retried_segments)
                        return 0;
@@ -623,7 +632,8 @@ static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
 int bio_add_pc_page(struct request_queue *q, struct bio *bio, struct page *page,
                    unsigned int len, unsigned int offset)
 {
-       return __bio_add_page(q, bio, page, len, offset, q->max_hw_sectors);
+       return __bio_add_page(q, bio, page, len, offset,
+                             queue_max_hw_sectors(q));
 }
 
 /**
@@ -643,7 +653,7 @@ int bio_add_page(struct bio *bio, struct page *page, unsigned int len,
                 unsigned int offset)
 {
        struct request_queue *q = bdev_get_queue(bio->bi_bdev);
-       return __bio_add_page(q, bio, page, len, offset, q->max_sectors);
+       return __bio_add_page(q, bio, page, len, offset, queue_max_sectors(q));
 }
 
 struct bio_map_data {
@@ -695,14 +705,13 @@ static struct bio_map_data *bio_alloc_map_data(int nr_segs, int iov_count,
 }
 
 static int __bio_copy_iov(struct bio *bio, struct bio_vec *iovecs,
-                         struct sg_iovec *iov, int iov_count, int uncopy,
-                         int do_free_page)
+                         struct sg_iovec *iov, int iov_count,
+                         int to_user, int from_user, int do_free_page)
 {
        int ret = 0, i;
        struct bio_vec *bvec;
        int iov_idx = 0;
        unsigned int iov_off = 0;
-       int read = bio_data_dir(bio) == READ;
 
        __bio_for_each_segment(bvec, bio, i, 0) {
                char *bv_addr = page_address(bvec->bv_page);
@@ -710,20 +719,21 @@ static int __bio_copy_iov(struct bio *bio, struct bio_vec *iovecs,
 
                while (bv_len && iov_idx < iov_count) {
                        unsigned int bytes;
-                       char *iov_addr;
+                       char __user *iov_addr;
 
                        bytes = min_t(unsigned int,
                                      iov[iov_idx].iov_len - iov_off, bv_len);
                        iov_addr = iov[iov_idx].iov_base + iov_off;
 
                        if (!ret) {
-                               if (!read && !uncopy)
-                                       ret = copy_from_user(bv_addr, iov_addr,
-                                                            bytes);
-                               if (read && uncopy)
+                               if (to_user)
                                        ret = copy_to_user(iov_addr, bv_addr,
                                                           bytes);
 
+                               if (from_user)
+                                       ret = copy_from_user(bv_addr, iov_addr,
+                                                            bytes);
+
                                if (ret)
                                        ret = -EFAULT;
                        }
@@ -760,7 +770,8 @@ int bio_uncopy_user(struct bio *bio)
 
        if (!bio_flagged(bio, BIO_NULL_MAPPED))
                ret = __bio_copy_iov(bio, bmd->iovecs, bmd->sgvecs,
-                                    bmd->nr_sgvecs, 1, bmd->is_our_pages);
+                                    bmd->nr_sgvecs, bio_data_dir(bio) == READ,
+                                    0, bmd->is_our_pages);
        bio_free_map_data(bmd);
        bio_put(bio);
        return ret;
@@ -806,12 +817,15 @@ struct bio *bio_copy_user_iov(struct request_queue *q,
                len += iov[i].iov_len;
        }
 
+       if (offset)
+               nr_pages++;
+
        bmd = bio_alloc_map_data(nr_pages, iov_count, gfp_mask);
        if (!bmd)
                return ERR_PTR(-ENOMEM);
 
        ret = -ENOMEM;
-       bio = bio_alloc(gfp_mask, nr_pages);
+       bio = bio_kmalloc(gfp_mask, nr_pages);
        if (!bio)
                goto out_bmd;
 
@@ -862,8 +876,9 @@ struct bio *bio_copy_user_iov(struct request_queue *q,
        /*
         * success
         */
-       if (!write_to_vm && (!map_data || !map_data->null_mapped)) {
-               ret = __bio_copy_iov(bio, bio->bi_io_vec, iov, iov_count, 0, 0);
+       if ((!write_to_vm && (!map_data || !map_data->null_mapped)) ||
+           (map_data && map_data->from_user)) {
+               ret = __bio_copy_iov(bio, bio->bi_io_vec, iov, iov_count, 0, 1, 0);
                if (ret)
                        goto cleanup;
        }
@@ -935,7 +950,7 @@ static struct bio *__bio_map_user_iov(struct request_queue *q,
        if (!nr_pages)
                return ERR_PTR(-EINVAL);
 
-       bio = bio_alloc(gfp_mask, nr_pages);
+       bio = bio_kmalloc(gfp_mask, nr_pages);
        if (!bio)
                return ERR_PTR(-ENOMEM);
 
@@ -1119,7 +1134,7 @@ static struct bio *__bio_map_kern(struct request_queue *q, void *data,
        int offset, i;
        struct bio *bio;
 
-       bio = bio_alloc(gfp_mask, nr_pages);
+       bio = bio_kmalloc(gfp_mask, nr_pages);
        if (!bio)
                return ERR_PTR(-ENOMEM);
 
@@ -1187,7 +1202,7 @@ static void bio_copy_kern_endio(struct bio *bio, int err)
                char *addr = page_address(bvec->bv_page);
                int len = bmd->iovecs[i].bv_len;
 
-               if (read && !err)
+               if (read)
                        memcpy(p, addr, len);
 
                __free_page(bvec->bv_page);
@@ -1417,8 +1432,7 @@ static void bio_pair_end_2(struct bio *bi, int err)
 }
 
 /*
- * split a bio - only worry about a bio with a single page
- * in it's iovec
+ * split a bio - only worry about a bio with a single page in its iovec
  */
 struct bio_pair *bio_split(struct bio *bi, int first_sectors)
 {
@@ -1477,11 +1491,12 @@ struct bio_pair *bio_split(struct bio *bi, int first_sectors)
 sector_t bio_sector_offset(struct bio *bio, unsigned short index,
                           unsigned int offset)
 {
-       unsigned int sector_sz = queue_hardsect_size(bio->bi_bdev->bd_disk->queue);
+       unsigned int sector_sz;
        struct bio_vec *bv;
        sector_t sectors;
        int i;
 
+       sector_sz = queue_logical_block_size(bio->bi_bdev->bd_disk->queue);
        sectors = 0;
 
        if (index >= bio->bi_idx)
@@ -1586,6 +1601,13 @@ static void __init biovec_init_slabs(void)
                int size;
                struct biovec_slab *bvs = bvec_slabs + i;
 
+#ifndef CONFIG_BLK_DEV_INTEGRITY
+               if (bvs->nr_vecs <= BIO_INLINE_VECS) {
+                       bvs->slab = NULL;
+                       continue;
+               }
+#endif
+
                size = bvs->nr_vecs * sizeof(struct bio_vec);
                bvs->slab = kmem_cache_create(bvs->name, size, 0,
                                 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
@@ -1600,7 +1622,7 @@ static int __init init_bio(void)
        if (!bio_slabs)
                panic("bio: can't allocate bios\n");
 
-       bio_integrity_init_slab();
+       bio_integrity_init();
        biovec_init_slabs();
 
        fs_bio_set = bioset_create(BIO_POOL_SIZE, 0);