- patches.apparmor/remove_suid_new_case_in_2.6.22.diff: Merge fix.
[linux-flexiantxendom0-3.2.10.git] / drivers / md / dm-io.c
index 4eb73d3..352c6fb 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2003 Sistina Software
+ * Copyright (C) 2006 Red Hat GmbH
  *
  * This file is released under the GPL.
  */
 #include <linux/sched.h>
 #include <linux/slab.h>
 
-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,63 +31,58 @@ 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;
 
 static unsigned int pages_to_ios(unsigned int pages)
 {
        return 4 * pages;       /* too many ? */
 }
 
-static int resize_pool(unsigned int new_ios)
+/*
+ * Create a client with mempool and bioset.
+ */
+struct dm_io_client *dm_io_client_create(unsigned num_pages)
 {
-       int r = 0;
-
-       if (_io_pool) {
-               if (new_ios == 0) {
-                       /* free off the pool */
-                       mempool_destroy(_io_pool);
-                       _io_pool = NULL;
-                       bioset_free(_bios);
-
-               } else {
-                       /* resize the pool */
-                       r = mempool_resize(_io_pool, new_ios, GFP_KERNEL);
-               }
+       unsigned ios = pages_to_ios(num_pages);
+       struct dm_io_client *client;
 
-       } else {
-               /* create new pool */
-               _io_pool = mempool_create_kmalloc_pool(new_ios,
-                                                      sizeof(struct io));
-               if (!_io_pool)
-                       return -ENOMEM;
-
-               _bios = bioset_create(16, 16, 4);
-               if (!_bios) {
-                       mempool_destroy(_io_pool);
-                       _io_pool = NULL;
-                       return -ENOMEM;
-               }
-       }
+       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;
 
-       if (!r)
-               _num_ios = new_ios;
+       client->bios = bioset_create(16, 16);
+       if (!client->bios)
+               goto bad;
 
-       return r;
+       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_get(unsigned int num_pages)
+int dm_io_client_resize(unsigned num_pages, struct dm_io_client *client)
 {
-       return resize_pool(_num_ios + pages_to_ios(num_pages));
+       return mempool_resize(client->pool, pages_to_ios(num_pages),
+                             GFP_KERNEL);
 }
+EXPORT_SYMBOL(dm_io_client_resize);
 
-void dm_io_put(unsigned int num_pages)
+void dm_io_client_destroy(struct dm_io_client *client)
 {
-       resize_pool(_num_ios - pages_to_ios(num_pages));
+       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.
@@ -118,7 +118,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->client->pool);
                        fn(r, context);
                }
        }
@@ -126,7 +126,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,10 +136,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;
 }
 
@@ -209,6 +217,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)
 {
@@ -233,7 +244,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, io->client->bios);
+}
+
+/*
+ * 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;
 }
 
 /*-----------------------------------------------------------------
@@ -256,7 +294,7 @@ static void do_region(int rw, unsigned int region, struct io_region *where,
                 * to hide it from bio_add_page().
                 */
                num_bvecs = (remaining / (PAGE_SIZE >> SECTOR_SHIFT)) + 2;
-               bio = bio_alloc_bioset(GFP_NOIO, num_bvecs, _bios);
+               bio = bio_alloc_bioset(GFP_NOIO, num_bvecs, io->client->bios);
                bio->bi_sector = where->sector + (where->count - remaining);
                bio->bi_bdev = where->bdev;
                bio->bi_end_io = endio;
@@ -311,8 +349,9 @@ static void dispatch_io(int rw, unsigned int num_regions,
        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;
 
@@ -324,6 +363,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);
 
@@ -340,12 +380,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;
 
@@ -355,10 +398,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(client->pool, 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;
 
@@ -366,61 +410,51 @@ static int async_io(unsigned int num_regions, struct io_region *where, int rw,
        return 0;
 }
 
-int dm_io_sync(unsigned int num_regions, struct io_region *where, int rw,
-              struct page_list *pl, unsigned int offset,
-              unsigned long *error_bits)
+static int dp_init(struct dm_io_request *io_req, struct dpages *dp)
 {
-       struct dpages dp;
-       list_dp_init(&dp, pl, offset);
-       return sync_io(num_regions, where, rw, &dp, error_bits);
-}
+       /* 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;
+       }
 
-int dm_io_sync_bvec(unsigned int num_regions, struct io_region *where, int rw,
-                   struct bio_vec *bvec, unsigned long *error_bits)
-{
-       struct dpages dp;
-       bvec_dp_init(&dp, bvec);
-       return sync_io(num_regions, where, rw, &dp, error_bits);
+       return 0;
 }
 
-int dm_io_sync_vm(unsigned int num_regions, struct io_region *where, int rw,
-                 void *data, unsigned long *error_bits)
+/*
+ * 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;
-       vm_dp_init(&dp, data);
-       return sync_io(num_regions, where, rw, &dp, error_bits);
-}
 
-int dm_io_async(unsigned int num_regions, struct io_region *where, int rw,
-               struct page_list *pl, unsigned int offset,
-               io_notify_fn fn, void *context)
-{
-       struct dpages dp;
-       list_dp_init(&dp, pl, offset);
-       return async_io(num_regions, where, rw, &dp, fn, context);
-}
+       r = dp_init(io_req, &dp);
+       if (r)
+               return r;
 
-int dm_io_async_bvec(unsigned int num_regions, struct io_region *where, int rw,
-                    struct bio_vec *bvec, io_notify_fn fn, void *context)
-{
-       struct dpages dp;
-       bvec_dp_init(&dp, bvec);
-       return async_io(num_regions, where, rw, &dp, fn, context);
-}
+       if (!io_req->notify.fn)
+               return sync_io(io_req->client, num_regions, where,
+                              io_req->bi_rw, &dp, sync_error_bits);
 
-int dm_io_async_vm(unsigned int num_regions, struct io_region *where, int rw,
-                  void *data, io_notify_fn fn, void *context)
-{
-       struct dpages dp;
-       vm_dp_init(&dp, data);
-       return async_io(num_regions, where, rw, &dp, fn, context);
+       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_get);
-EXPORT_SYMBOL(dm_io_put);
-EXPORT_SYMBOL(dm_io_sync);
-EXPORT_SYMBOL(dm_io_async);
-EXPORT_SYMBOL(dm_io_sync_bvec);
-EXPORT_SYMBOL(dm_io_async_bvec);
-EXPORT_SYMBOL(dm_io_sync_vm);
-EXPORT_SYMBOL(dm_io_async_vm);
+EXPORT_SYMBOL(dm_io);