locks: allow lockd to process blocked locks during grace period
[safe/jmp/linux-2.6] / block / blk-map.c
index 916cfc9..af37e4a 100644 (file)
@@ -5,6 +5,7 @@
 #include <linux/module.h>
 #include <linux/bio.h>
 #include <linux/blkdev.h>
+#include <scsi/sg.h>           /* for struct sg_iovec */
 
 #include "blk.h"
 
@@ -43,6 +44,7 @@ static int __blk_rq_map_user(struct request_queue *q, struct request *rq,
                             void __user *ubuf, unsigned int len)
 {
        unsigned long uaddr;
+       unsigned int alignment;
        struct bio *bio, *orig_bio;
        int reading, ret;
 
@@ -53,7 +55,8 @@ static int __blk_rq_map_user(struct request_queue *q, struct request *rq,
         * direct dma. else, set up kernel bounce buffers
         */
        uaddr = (unsigned long) ubuf;
-       if (!(uaddr & queue_dma_alignment(q)) && !(len & queue_dma_alignment(q)))
+       alignment = queue_dma_alignment(q) | q->dma_pad_mask;
+       if (!(uaddr & alignment) && !(len & alignment))
                bio = bio_map_user(q, NULL, uaddr, len, reading);
        else
                bio = bio_copy_user(q, uaddr, len, reading);
@@ -138,13 +141,16 @@ int blk_rq_map_user(struct request_queue *q, struct request *rq,
                ubuf += ret;
        }
 
+       if (!bio_flagged(bio, BIO_USER_MAPPED))
+               rq->cmd_flags |= REQ_COPY_USER;
+
        rq->buffer = rq->data = NULL;
        return 0;
 unmap_rq:
        blk_rq_unmap_user(bio);
+       rq->bio = NULL;
        return ret;
 }
-
 EXPORT_SYMBOL(blk_rq_map_user);
 
 /**
@@ -172,14 +178,26 @@ int blk_rq_map_user_iov(struct request_queue *q, struct request *rq,
                        struct sg_iovec *iov, int iov_count, unsigned int len)
 {
        struct bio *bio;
+       int i, read = rq_data_dir(rq) == READ;
+       int unaligned = 0;
 
        if (!iov || iov_count <= 0)
                return -EINVAL;
 
-       /* we don't allow misaligned data like bio_map_user() does.  If the
-        * user is using sg, they're expected to know the alignment constraints
-        * and respect them accordingly */
-       bio = bio_map_user_iov(q, NULL, iov, iov_count, rq_data_dir(rq)== READ);
+       for (i = 0; i < iov_count; i++) {
+               unsigned long uaddr = (unsigned long)iov[i].iov_base;
+
+               if (uaddr & queue_dma_alignment(q)) {
+                       unaligned = 1;
+                       break;
+               }
+       }
+
+       if (unaligned || (q->dma_pad_mask & len))
+               bio = bio_copy_user_iov(q, iov, iov_count, read);
+       else
+               bio = bio_map_user_iov(q, NULL, iov, iov_count, read);
+
        if (IS_ERR(bio))
                return PTR_ERR(bio);
 
@@ -189,14 +207,16 @@ int blk_rq_map_user_iov(struct request_queue *q, struct request *rq,
                return -EINVAL;
        }
 
+       if (!bio_flagged(bio, BIO_USER_MAPPED))
+               rq->cmd_flags |= REQ_COPY_USER;
+
+       blk_queue_bounce(q, &bio);
        bio_get(bio);
        blk_rq_bio_prep(q, rq, bio);
        rq->buffer = rq->data = NULL;
        return 0;
 }
 
-EXPORT_SYMBOL(blk_rq_map_user_iov);
-
 /**
  * blk_rq_unmap_user - unmap a request with user data
  * @bio:              start of bio list
@@ -227,7 +247,6 @@ int blk_rq_unmap_user(struct bio *bio)
 
        return ret;
 }
-
 EXPORT_SYMBOL(blk_rq_unmap_user);
 
 /**
@@ -237,10 +256,18 @@ EXPORT_SYMBOL(blk_rq_unmap_user);
  * @kbuf:      the kernel buffer
  * @len:       length of user data
  * @gfp_mask:  memory allocation flags
+ *
+ * Description:
+ *    Data will be mapped directly if possible. Otherwise a bounce
+ *    buffer is used.
  */
 int blk_rq_map_kern(struct request_queue *q, struct request *rq, void *kbuf,
                    unsigned int len, gfp_t gfp_mask)
 {
+       unsigned long kaddr;
+       unsigned int alignment;
+       int reading = rq_data_dir(rq) == READ;
+       int do_copy = 0;
        struct bio *bio;
 
        if (len > (q->max_hw_sectors << 9))
@@ -248,17 +275,28 @@ int blk_rq_map_kern(struct request_queue *q, struct request *rq, void *kbuf,
        if (!len || !kbuf)
                return -EINVAL;
 
-       bio = bio_map_kern(q, kbuf, len, gfp_mask);
+       kaddr = (unsigned long)kbuf;
+       alignment = queue_dma_alignment(q) | q->dma_pad_mask;
+       do_copy = ((kaddr & alignment) || (len & alignment) ||
+                  object_is_on_stack(kbuf));
+
+       if (do_copy)
+               bio = bio_copy_kern(q, kbuf, len, gfp_mask, reading);
+       else
+               bio = bio_map_kern(q, kbuf, len, gfp_mask);
+
        if (IS_ERR(bio))
                return PTR_ERR(bio);
 
        if (rq_data_dir(rq) == WRITE)
                bio->bi_rw |= (1 << BIO_RW);
 
+       if (do_copy)
+               rq->cmd_flags |= REQ_COPY_USER;
+
        blk_rq_bio_prep(q, rq, bio);
        blk_queue_bounce(q, &rq->bio);
        rq->buffer = rq->data = NULL;
        return 0;
 }
-
 EXPORT_SYMBOL(blk_rq_map_kern);