nfsd: move most of nfsfh.h to fs/nfsd
[safe/jmp/linux-2.6] / drivers / md / dm-io.c
index 352c6fb..3a2e6a2 100644 (file)
@@ -5,13 +5,14 @@
  * This file is released under the GPL.
  */
 
-#include "dm-io.h"
+#include <linux/device-mapper.h>
 
 #include <linux/bio.h>
 #include <linux/mempool.h>
 #include <linux/module.h>
 #include <linux/sched.h>
 #include <linux/slab.h>
+#include <linux/dm-io.h>
 
 struct dm_io_client {
        mempool_t *pool;
@@ -20,7 +21,8 @@ struct dm_io_client {
 
 /* FIXME: can we shrink this ? */
 struct io {
-       unsigned long error;
+       unsigned long error_bits;
+       unsigned long eopnotsupp_bits;
        atomic_t count;
        struct task_struct *sleeper;
        struct dm_io_client *client;
@@ -55,7 +57,7 @@ struct dm_io_client *dm_io_client_create(unsigned num_pages)
        if (!client->pool)
                goto bad;
 
-       client->bios = bioset_create(16, 16);
+       client->bios = bioset_create(16, 0);
        if (!client->bios)
                goto bad;
 
@@ -106,15 +108,18 @@ static inline unsigned bio_get_region(struct bio *bio)
  *---------------------------------------------------------------*/
 static void dec_count(struct io *io, unsigned int region, int error)
 {
-       if (error)
-               set_bit(region, &io->error);
+       if (error) {
+               set_bit(region, &io->error_bits);
+               if (error == -EOPNOTSUPP)
+                       set_bit(region, &io->eopnotsupp_bits);
+       }
 
        if (atomic_dec_and_test(&io->count)) {
                if (io->sleeper)
                        wake_up_process(io->sleeper);
 
                else {
-                       int r = io->error;
+                       unsigned long r = io->error_bits;
                        io_notify_fn fn = io->callback;
                        void *context = io->context;
 
@@ -124,15 +129,11 @@ static void dec_count(struct io *io, unsigned int region, int error)
        }
 }
 
-static int endio(struct bio *bio, unsigned int done, int error)
+static void endio(struct bio *bio, int error)
 {
        struct io *io;
        unsigned region;
 
-       /* keep going until we've finished */
-       if (bio->bi_size)
-               return 1;
-
        if (error && bio_data_dir(bio) == READ)
                zero_fill_bio(bio);
 
@@ -146,8 +147,6 @@ static int endio(struct bio *bio, unsigned int done, int error)
        bio_put(bio);
 
        dec_count(io, region, error);
-
-       return 0;
 }
 
 /*-----------------------------------------------------------------
@@ -277,7 +276,7 @@ static void km_dp_init(struct dpages *dp, void *data)
 /*-----------------------------------------------------------------
  * IO routines that accept a list of pages.
  *---------------------------------------------------------------*/
-static void do_region(int rw, unsigned int region, struct io_region *where,
+static void do_region(int rw, unsigned region, struct dm_io_region *where,
                      struct dpages *dp, struct io *io)
 {
        struct bio *bio;
@@ -293,7 +292,12 @@ static void do_region(int rw, unsigned int region, struct io_region *where,
                 * bvec for bio_get/set_region() and decrement bi_max_vecs
                 * to hide it from bio_add_page().
                 */
-               num_bvecs = (remaining / (PAGE_SIZE >> SECTOR_SHIFT)) + 2;
+               num_bvecs = dm_sector_div_up(remaining,
+                                            (PAGE_SIZE >> SECTOR_SHIFT));
+               num_bvecs = 1 + min_t(int, bio_get_nr_vecs(where->bdev),
+                                     num_bvecs);
+               if (unlikely(num_bvecs > BIO_MAX_PAGES))
+                       num_bvecs = BIO_MAX_PAGES;
                bio = bio_alloc_bioset(GFP_NOIO, num_bvecs, io->client->bios);
                bio->bi_sector = where->sector + (where->count - remaining);
                bio->bi_bdev = where->bdev;
@@ -323,14 +327,14 @@ static void do_region(int rw, unsigned int region, struct io_region *where,
 }
 
 static void dispatch_io(int rw, unsigned int num_regions,
-                       struct io_region *where, struct dpages *dp,
+                       struct dm_io_region *where, struct dpages *dp,
                        struct io *io, int sync)
 {
        int i;
        struct dpages old_pages = *dp;
 
        if (sync)
-               rw |= (1 << BIO_RW_SYNC);
+               rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
 
        /*
         * For multiple regions we need to be careful to rewind
@@ -350,17 +354,19 @@ static void dispatch_io(int rw, unsigned int num_regions,
 }
 
 static int sync_io(struct dm_io_client *client, unsigned int num_regions,
-                  struct io_region *where, int rw, struct dpages *dp,
+                  struct dm_io_region *where, int rw, struct dpages *dp,
                   unsigned long *error_bits)
 {
        struct io io;
 
-       if (num_regions > 1 && rw != WRITE) {
+       if (num_regions > 1 && (rw & RW_MASK) != WRITE) {
                WARN_ON(1);
                return -EIO;
        }
 
-       io.error = 0;
+retry:
+       io.error_bits = 0;
+       io.eopnotsupp_bits = 0;
        atomic_set(&io.count, 1); /* see dispatch_io() */
        io.sleeper = current;
        io.client = client;
@@ -370,36 +376,39 @@ static int sync_io(struct dm_io_client *client, unsigned int num_regions,
        while (1) {
                set_current_state(TASK_UNINTERRUPTIBLE);
 
-               if (!atomic_read(&io.count) || signal_pending(current))
+               if (!atomic_read(&io.count))
                        break;
 
                io_schedule();
        }
        set_current_state(TASK_RUNNING);
 
-       if (atomic_read(&io.count))
-               return -EINTR;
+       if (io.eopnotsupp_bits && (rw & (1 << BIO_RW_BARRIER))) {
+               rw &= ~(1 << BIO_RW_BARRIER);
+               goto retry;
+       }
 
        if (error_bits)
-               *error_bits = io.error;
+               *error_bits = io.error_bits;
 
-       return io.error ? -EIO : 0;
+       return io.error_bits ? -EIO : 0;
 }
 
 static int async_io(struct dm_io_client *client, unsigned int num_regions,
-                   struct io_region *where, int rw, struct dpages *dp,
+                   struct dm_io_region *where, int rw, struct dpages *dp,
                    io_notify_fn fn, void *context)
 {
        struct io *io;
 
-       if (num_regions > 1 && rw != WRITE) {
+       if (num_regions > 1 && (rw & RW_MASK) != WRITE) {
                WARN_ON(1);
                fn(1, context);
                return -EIO;
        }
 
        io = mempool_alloc(client->pool, GFP_NOIO);
-       io->error = 0;
+       io->error_bits = 0;
+       io->eopnotsupp_bits = 0;
        atomic_set(&io->count, 1); /* see dispatch_io() */
        io->sleeper = NULL;
        io->client = client;
@@ -438,10 +447,15 @@ static int dp_init(struct dm_io_request *io_req, struct dpages *dp)
 }
 
 /*
- * New collapsed (a)synchronous interface
+ * New collapsed (a)synchronous interface.
+ *
+ * If the IO is asynchronous (i.e. it has notify.fn), you must either unplug
+ * the queue with blk_unplug() some time later or set the BIO_RW_SYNC bit in
+ * io_req->bi_rw. If you fail to do one of these, the IO will be submitted to
+ * the disk after q->unplug_delay, which defaults to 3ms in blk-settings.c.
  */
 int dm_io(struct dm_io_request *io_req, unsigned num_regions,
-         struct io_region *where, unsigned long *sync_error_bits)
+         struct dm_io_region *where, unsigned long *sync_error_bits)
 {
        int r;
        struct dpages dp;