[PATCH 02/14] block: reorganize [__]bio_map_kern()

From: Tejun Heo
Date: Tue Mar 24 2009 - 12:07:26 EST


Impact: code reorganization, cleanup

Reorganize [__]bio_map_kern() such that __bio_map_kern() only maps the
pages while bio_map_kern() handles allocation. Also, if not all pages
can be mapped, __bio_map_kern() fails directly instead of doing
partial mapping and letting bio_map_kern() detect it and fail.

While at it, convert to PFN_*() macros for page number calculations.

The reorganized __bio_map_kern() will be used in the next patch for
bio_map_kern_prealloc().

Signed-off-by: Tejun Heo <tj@xxxxxxxxxx>
Cc: Jens Axboe <axboe@xxxxxxxxx>
---
fs/bio.c | 56 +++++++++++++++++++++++---------------------------------
1 files changed, 23 insertions(+), 33 deletions(-)

diff --git a/fs/bio.c b/fs/bio.c
index d4f0632..746b566 100644
--- a/fs/bio.c
+++ b/fs/bio.c
@@ -26,6 +26,7 @@
#include <linux/mempool.h>
#include <linux/workqueue.h>
#include <linux/blktrace_api.h>
+#include <linux/pfn.h>
#include <trace/block.h>
#include <scsi/sg.h> /* for struct sg_iovec */

@@ -1109,41 +1110,27 @@ static void bio_map_kern_endio(struct bio *bio, int err)
}


-static struct bio *__bio_map_kern(struct request_queue *q, void *data,
- unsigned int len, gfp_t gfp_mask)
+static int __bio_map_kern(struct request_queue *q, struct bio *bio,
+ void *data, unsigned int len)
{
- unsigned long kaddr = (unsigned long)data;
- unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
- unsigned long start = kaddr >> PAGE_SHIFT;
- const int nr_pages = end - start;
int offset, i;
- struct bio *bio;
-
- bio = bio_alloc(gfp_mask, nr_pages);
- if (!bio)
- return ERR_PTR(-ENOMEM);
-
- offset = offset_in_page(kaddr);
- for (i = 0; i < nr_pages; i++) {
- unsigned int bytes = PAGE_SIZE - offset;

- if (len <= 0)
- break;
-
- if (bytes > len)
- bytes = len;
+ offset = offset_in_page(data);
+ for (i = 0; len; i++) {
+ unsigned int bytes = min_t(unsigned int,
+ len, PAGE_SIZE - offset);

+ /* don't support partial mapping */
if (bio_add_pc_page(q, bio, virt_to_page(data), bytes,
offset) < bytes)
- break;
+ return -EINVAL;

data += bytes;
len -= bytes;
offset = 0;
}

- bio->bi_end_io = bio_map_kern_endio;
- return bio;
+ return 0;
}

/**
@@ -1159,20 +1146,23 @@ static struct bio *__bio_map_kern(struct request_queue *q, void *data,
struct bio *bio_map_kern(struct request_queue *q, void *data, unsigned int len,
gfp_t gfp_mask)
{
+ const unsigned long kaddr = (unsigned long)data;
+ const int nr_pages = PFN_UP(kaddr + len) - PFN_DOWN(kaddr);
struct bio *bio;
+ int error;

- bio = __bio_map_kern(q, data, len, gfp_mask);
- if (IS_ERR(bio))
- return bio;
+ bio = bio_alloc(gfp_mask, nr_pages);
+ if (unlikely(!bio))
+ return ERR_PTR(-ENOMEM);
+ bio->bi_end_io = bio_map_kern_endio;

- if (bio->bi_size == len)
- return bio;
+ error = __bio_map_kern(q, bio, data, len);
+ if (unlikely(error)) {
+ bio_put(bio);
+ return ERR_PTR(error);
+ }

- /*
- * Don't support partial mappings.
- */
- bio_put(bio);
- return ERR_PTR(-EINVAL);
+ return bio;
}

static void bio_copy_kern_endio(struct bio *bio, int err)
--
1.6.0.2

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/