]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blobdiff - drivers/md/dm-io.c
dm io: new interface
[mirror_ubuntu-zesty-kernel.git] / drivers / md / dm-io.c
index da663d2ff552b4b3a4d44bec0c4ec84e4e904365..0c63809ab70ebf54f5a3891c5948bd6daec14d38 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2003 Sistina Software
+ * Copyright (C) 2006 Red Hat GmbH
  *
  * This file is released under the GPL.
  */
 
 static struct bio_set *_bios;
 
+struct dm_io_client {
+       mempool_t *pool;
+       struct bio_set *bios;
+};
+
 /* FIXME: can we shrink this ? */
 struct io {
        unsigned long error;
        atomic_t count;
        struct task_struct *sleeper;
+       struct dm_io_client *client;
        io_notify_fn callback;
        void *context;
 };
@@ -26,12 +33,24 @@ struct io {
 /*
  * io contexts are only dynamically allocated for asynchronous
  * io.  Since async io is likely to be the majority of io we'll
- * have the same number of io contexts as buffer heads ! (FIXME:
- * must reduce this).
+ * have the same number of io contexts as bios! (FIXME: must reduce this).
  */
 static unsigned _num_ios;
 static mempool_t *_io_pool;
 
+/*
+ * Temporary functions to allow old and new interfaces to co-exist.
+ */
+static struct bio_set *bios(struct dm_io_client *client)
+{
+       return client ? client->bios : _bios;
+}
+
+static mempool_t *io_pool(struct dm_io_client *client)
+{
+       return client ? client->pool : _io_pool;
+}
+
 static unsigned int pages_to_ios(unsigned int pages)
 {
        return 4 * pages;       /* too many ? */
@@ -60,7 +79,7 @@ static int resize_pool(unsigned int new_ios)
                if (!_io_pool)
                        return -ENOMEM;
 
-               _bios = bioset_create(16, 16, 4);
+               _bios = bioset_create(16, 16);
                if (!_bios) {
                        mempool_destroy(_io_pool);
                        _io_pool = NULL;
@@ -84,6 +103,51 @@ void dm_io_put(unsigned int num_pages)
        resize_pool(_num_ios - pages_to_ios(num_pages));
 }
 
+/*
+ * Create a client with mempool and bioset.
+ */
+struct dm_io_client *dm_io_client_create(unsigned num_pages)
+{
+       unsigned ios = pages_to_ios(num_pages);
+       struct dm_io_client *client;
+
+       client = kmalloc(sizeof(*client), GFP_KERNEL);
+       if (!client)
+               return ERR_PTR(-ENOMEM);
+
+       client->pool = mempool_create_kmalloc_pool(ios, sizeof(struct io));
+       if (!client->pool)
+               goto bad;
+
+       client->bios = bioset_create(16, 16);
+       if (!client->bios)
+               goto bad;
+
+       return client;
+
+   bad:
+       if (client->pool)
+               mempool_destroy(client->pool);
+       kfree(client);
+       return ERR_PTR(-ENOMEM);
+}
+EXPORT_SYMBOL(dm_io_client_create);
+
+int dm_io_client_resize(unsigned num_pages, struct dm_io_client *client)
+{
+       return mempool_resize(client->pool, pages_to_ios(num_pages),
+                             GFP_KERNEL);
+}
+EXPORT_SYMBOL(dm_io_client_resize);
+
+void dm_io_client_destroy(struct dm_io_client *client)
+{
+       mempool_destroy(client->pool);
+       bioset_free(client->bios);
+       kfree(client);
+}
+EXPORT_SYMBOL(dm_io_client_destroy);
+
 /*-----------------------------------------------------------------
  * We need to keep track of which region a bio is doing io for.
  * In order to save a memory allocation we store this the last
@@ -92,12 +156,12 @@ void dm_io_put(unsigned int num_pages)
  *---------------------------------------------------------------*/
 static inline void bio_set_region(struct bio *bio, unsigned region)
 {
-       bio->bi_io_vec[bio->bi_max_vecs - 1].bv_len = region;
+       bio->bi_io_vec[bio->bi_max_vecs].bv_len = region;
 }
 
 static inline unsigned bio_get_region(struct bio *bio)
 {
-       return bio->bi_io_vec[bio->bi_max_vecs - 1].bv_len;
+       return bio->bi_io_vec[bio->bi_max_vecs].bv_len;
 }
 
 /*-----------------------------------------------------------------
@@ -118,7 +182,7 @@ static void dec_count(struct io *io, unsigned int region, int error)
                        io_notify_fn fn = io->callback;
                        void *context = io->context;
 
-                       mempool_free(io, _io_pool);
+                       mempool_free(io, io_pool(io->client));
                        fn(r, context);
                }
        }
@@ -126,7 +190,8 @@ static void dec_count(struct io *io, unsigned int region, int error)
 
 static int endio(struct bio *bio, unsigned int done, int error)
 {
-       struct io *io = (struct io *) bio->bi_private;
+       struct io *io;
+       unsigned region;
 
        /* keep going until we've finished */
        if (bio->bi_size)
@@ -135,9 +200,17 @@ static int endio(struct bio *bio, unsigned int done, int error)
        if (error && bio_data_dir(bio) == READ)
                zero_fill_bio(bio);
 
-       dec_count(io, bio_get_region(bio), error);
+       /*
+        * The bio destructor in bio_put() may use the io object.
+        */
+       io = bio->bi_private;
+       region = bio_get_region(bio);
+
+       bio->bi_max_vecs++;
        bio_put(bio);
 
+       dec_count(io, region, error);
+
        return 0;
 }
 
@@ -208,6 +281,9 @@ static void bvec_dp_init(struct dpages *dp, struct bio_vec *bvec)
        dp->context_ptr = bvec;
 }
 
+/*
+ * Functions for getting the pages from a VMA.
+ */
 static void vm_get_page(struct dpages *dp,
                 struct page **p, unsigned long *len, unsigned *offset)
 {
@@ -232,7 +308,34 @@ static void vm_dp_init(struct dpages *dp, void *data)
 
 static void dm_bio_destructor(struct bio *bio)
 {
-       bio_free(bio, _bios);
+       struct io *io = bio->bi_private;
+
+       bio_free(bio, bios(io->client));
+}
+
+/*
+ * Functions for getting the pages from kernel memory.
+ */
+static void km_get_page(struct dpages *dp, struct page **p, unsigned long *len,
+                       unsigned *offset)
+{
+       *p = virt_to_page(dp->context_ptr);
+       *offset = dp->context_u;
+       *len = PAGE_SIZE - dp->context_u;
+}
+
+static void km_next_page(struct dpages *dp)
+{
+       dp->context_ptr += PAGE_SIZE - dp->context_u;
+       dp->context_u = 0;
+}
+
+static void km_dp_init(struct dpages *dp, void *data)
+{
+       dp->get_page = km_get_page;
+       dp->next_page = km_next_page;
+       dp->context_u = ((unsigned long) data) & (PAGE_SIZE - 1);
+       dp->context_ptr = data;
 }
 
 /*-----------------------------------------------------------------
@@ -250,16 +353,18 @@ static void do_region(int rw, unsigned int region, struct io_region *where,
 
        while (remaining) {
                /*
-                * Allocate a suitably sized bio, we add an extra
-                * bvec for bio_get/set_region().
+                * Allocate a suitably sized-bio: we add an extra
+                * bvec for bio_get/set_region() and decrement bi_max_vecs
+                * to hide it from bio_add_page().
                 */
-               num_bvecs = (remaining / (PAGE_SIZE >> 9)) + 2;
-               bio = bio_alloc_bioset(GFP_NOIO, num_bvecs, _bios);
+               num_bvecs = (remaining / (PAGE_SIZE >> SECTOR_SHIFT)) + 2;
+               bio = bio_alloc_bioset(GFP_NOIO, num_bvecs, bios(io->client));
                bio->bi_sector = where->sector + (where->count - remaining);
                bio->bi_bdev = where->bdev;
                bio->bi_end_io = endio;
                bio->bi_private = io;
                bio->bi_destructor = dm_bio_destructor;
+               bio->bi_max_vecs--;
                bio_set_region(bio, region);
 
                /*
@@ -302,14 +407,15 @@ static void dispatch_io(int rw, unsigned int num_regions,
        }
 
        /*
-        * Drop the extra refence that we were holding to avoid
+        * Drop the extra reference that we were holding to avoid
         * the io being completed too early.
         */
        dec_count(io, 0, 0);
 }
 
-static int sync_io(unsigned int num_regions, struct io_region *where,
-           int rw, struct dpages *dp, unsigned long *error_bits)
+static int sync_io(struct dm_io_client *client, unsigned int num_regions,
+                  struct io_region *where, int rw, struct dpages *dp,
+                  unsigned long *error_bits)
 {
        struct io io;
 
@@ -321,6 +427,7 @@ static int sync_io(unsigned int num_regions, struct io_region *where,
        io.error = 0;
        atomic_set(&io.count, 1); /* see dispatch_io() */
        io.sleeper = current;
+       io.client = client;
 
        dispatch_io(rw, num_regions, where, dp, &io, 1);
 
@@ -337,12 +444,15 @@ static int sync_io(unsigned int num_regions, struct io_region *where,
        if (atomic_read(&io.count))
                return -EINTR;
 
-       *error_bits = io.error;
+       if (error_bits)
+               *error_bits = io.error;
+
        return io.error ? -EIO : 0;
 }
 
-static int async_io(unsigned int num_regions, struct io_region *where, int rw,
-            struct dpages *dp, io_notify_fn fn, void *context)
+static int async_io(struct dm_io_client *client, unsigned int num_regions,
+                   struct io_region *where, int rw, struct dpages *dp,
+                   io_notify_fn fn, void *context)
 {
        struct io *io;
 
@@ -352,10 +462,11 @@ static int async_io(unsigned int num_regions, struct io_region *where, int rw,
                return -EIO;
        }
 
-       io = mempool_alloc(_io_pool, GFP_NOIO);
+       io = mempool_alloc(io_pool(client), GFP_NOIO);
        io->error = 0;
        atomic_set(&io->count, 1); /* see dispatch_io() */
        io->sleeper = NULL;
+       io->client = client;
        io->callback = fn;
        io->context = context;
 
@@ -369,7 +480,7 @@ int dm_io_sync(unsigned int num_regions, struct io_region *where, int rw,
 {
        struct dpages dp;
        list_dp_init(&dp, pl, offset);
-       return sync_io(num_regions, where, rw, &dp, error_bits);
+       return sync_io(NULL, num_regions, where, rw, &dp, error_bits);
 }
 
 int dm_io_sync_bvec(unsigned int num_regions, struct io_region *where, int rw,
@@ -377,7 +488,7 @@ int dm_io_sync_bvec(unsigned int num_regions, struct io_region *where, int rw,
 {
        struct dpages dp;
        bvec_dp_init(&dp, bvec);
-       return sync_io(num_regions, where, rw, &dp, error_bits);
+       return sync_io(NULL, num_regions, where, rw, &dp, error_bits);
 }
 
 int dm_io_sync_vm(unsigned int num_regions, struct io_region *where, int rw,
@@ -385,7 +496,7 @@ int dm_io_sync_vm(unsigned int num_regions, struct io_region *where, int rw,
 {
        struct dpages dp;
        vm_dp_init(&dp, data);
-       return sync_io(num_regions, where, rw, &dp, error_bits);
+       return sync_io(NULL, num_regions, where, rw, &dp, error_bits);
 }
 
 int dm_io_async(unsigned int num_regions, struct io_region *where, int rw,
@@ -394,7 +505,7 @@ int dm_io_async(unsigned int num_regions, struct io_region *where, int rw,
 {
        struct dpages dp;
        list_dp_init(&dp, pl, offset);
-       return async_io(num_regions, where, rw, &dp, fn, context);
+       return async_io(NULL, num_regions, where, rw, &dp, fn, context);
 }
 
 int dm_io_async_bvec(unsigned int num_regions, struct io_region *where, int rw,
@@ -402,7 +513,7 @@ int dm_io_async_bvec(unsigned int num_regions, struct io_region *where, int rw,
 {
        struct dpages dp;
        bvec_dp_init(&dp, bvec);
-       return async_io(num_regions, where, rw, &dp, fn, context);
+       return async_io(NULL, num_regions, where, rw, &dp, fn, context);
 }
 
 int dm_io_async_vm(unsigned int num_regions, struct io_region *where, int rw,
@@ -410,8 +521,57 @@ int dm_io_async_vm(unsigned int num_regions, struct io_region *where, int rw,
 {
        struct dpages dp;
        vm_dp_init(&dp, data);
-       return async_io(num_regions, where, rw, &dp, fn, context);
+       return async_io(NULL, num_regions, where, rw, &dp, fn, context);
+}
+
+static int dp_init(struct dm_io_request *io_req, struct dpages *dp)
+{
+       /* Set up dpages based on memory type */
+       switch (io_req->mem.type) {
+       case DM_IO_PAGE_LIST:
+               list_dp_init(dp, io_req->mem.ptr.pl, io_req->mem.offset);
+               break;
+
+       case DM_IO_BVEC:
+               bvec_dp_init(dp, io_req->mem.ptr.bvec);
+               break;
+
+       case DM_IO_VMA:
+               vm_dp_init(dp, io_req->mem.ptr.vma);
+               break;
+
+       case DM_IO_KMEM:
+               km_dp_init(dp, io_req->mem.ptr.addr);
+               break;
+
+       default:
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+/*
+ * New collapsed (a)synchronous interface
+ */
+int dm_io(struct dm_io_request *io_req, unsigned num_regions,
+         struct io_region *where, unsigned long *sync_error_bits)
+{
+       int r;
+       struct dpages dp;
+
+       r = dp_init(io_req, &dp);
+       if (r)
+               return r;
+
+       if (!io_req->notify.fn)
+               return sync_io(io_req->client, num_regions, where,
+                              io_req->bi_rw, &dp, sync_error_bits);
+
+       return async_io(io_req->client, num_regions, where, io_req->bi_rw,
+                       &dp, io_req->notify.fn, io_req->notify.context);
 }
+EXPORT_SYMBOL(dm_io);
 
 EXPORT_SYMBOL(dm_io_get);
 EXPORT_SYMBOL(dm_io_put);