Merge master.kernel.org:/pub/scm/linux/kernel/git/torvalds/linux-2.6 into next
[pandora-kernel.git] / drivers / md / dm-snap.c
index cee16fa..1ba8a47 100644 (file)
 #include <linux/slab.h>
 #include <linux/vmalloc.h>
 #include <linux/log2.h>
+#include <linux/dm-kcopyd.h>
 
 #include "dm-snap.h"
 #include "dm-bio-list.h"
-#include "kcopyd.h"
 
 #define DM_MSG_PREFIX "snapshots"
 
@@ -36,9 +36,9 @@
 #define SNAPSHOT_COPY_PRIORITY 2
 
 /*
- * Each snapshot reserves this many pages for io
+ * Reserve 1MB for each snapshot initially (with minimum of 1 page).
  */
-#define SNAPSHOT_PAGES 256
+#define SNAPSHOT_PAGES (((1UL << 20) >> PAGE_SHIFT) ? : 1)
 
 static struct workqueue_struct *ksnapd;
 static void flush_queued_bios(struct work_struct *work);
@@ -213,11 +213,15 @@ static void unregister_snapshot(struct dm_snapshot *s)
 
 /*
  * Implementation of the exception hash tables.
+ * The lowest hash_shift bits of the chunk number are ignored, allowing
+ * some consecutive chunks to be grouped together.
  */
-static int init_exception_table(struct exception_table *et, uint32_t size)
+static int init_exception_table(struct exception_table *et, uint32_t size,
+                               unsigned hash_shift)
 {
        unsigned int i;
 
+       et->hash_shift = hash_shift;
        et->hash_mask = size - 1;
        et->table = dm_vcalloc(size, sizeof(struct list_head));
        if (!et->table)
@@ -248,7 +252,7 @@ static void exit_exception_table(struct exception_table *et, struct kmem_cache *
 
 static uint32_t exception_hash(struct exception_table *et, chunk_t chunk)
 {
-       return chunk & et->hash_mask;
+       return (chunk >> et->hash_shift) & et->hash_mask;
 }
 
 static void insert_exception(struct exception_table *eh,
@@ -275,7 +279,8 @@ static struct dm_snap_exception *lookup_exception(struct exception_table *et,
 
        slot = &et->table[exception_hash(et, chunk)];
        list_for_each_entry (e, slot, hash_list)
-               if (e->old_chunk == chunk)
+               if (chunk >= e->old_chunk &&
+                   chunk <= e->old_chunk + dm_consecutive_chunk_count(e))
                        return e;
 
        return NULL;
@@ -307,6 +312,49 @@ static void free_pending_exception(struct dm_snap_pending_exception *pe)
        mempool_free(pe, pending_pool);
 }
 
+static void insert_completed_exception(struct dm_snapshot *s,
+                                      struct dm_snap_exception *new_e)
+{
+       struct exception_table *eh = &s->complete;
+       struct list_head *l;
+       struct dm_snap_exception *e = NULL;
+
+       l = &eh->table[exception_hash(eh, new_e->old_chunk)];
+
+       /* Add immediately if this table doesn't support consecutive chunks */
+       if (!eh->hash_shift)
+               goto out;
+
+       /* List is ordered by old_chunk */
+       list_for_each_entry_reverse(e, l, hash_list) {
+               /* Insert after an existing chunk? */
+               if (new_e->old_chunk == (e->old_chunk +
+                                        dm_consecutive_chunk_count(e) + 1) &&
+                   new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
+                                        dm_consecutive_chunk_count(e) + 1)) {
+                       dm_consecutive_chunk_count_inc(e);
+                       free_exception(new_e);
+                       return;
+               }
+
+               /* Insert before an existing chunk? */
+               if (new_e->old_chunk == (e->old_chunk - 1) &&
+                   new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) {
+                       dm_consecutive_chunk_count_inc(e);
+                       e->old_chunk--;
+                       e->new_chunk--;
+                       free_exception(new_e);
+                       return;
+               }
+
+               if (new_e->old_chunk > e->old_chunk)
+                       break;
+       }
+
+out:
+       list_add(&new_e->hash_list, e ? &e->hash_list : l);
+}
+
 int dm_add_exception(struct dm_snapshot *s, chunk_t old, chunk_t new)
 {
        struct dm_snap_exception *e;
@@ -316,8 +364,12 @@ int dm_add_exception(struct dm_snapshot *s, chunk_t old, chunk_t new)
                return -ENOMEM;
 
        e->old_chunk = old;
+
+       /* Consecutive_count is implicitly initialised to zero */
        e->new_chunk = new;
-       insert_exception(&s->complete, e);
+
+       insert_completed_exception(s, e);
+
        return 0;
 }
 
@@ -333,16 +385,6 @@ static int calc_max_buckets(void)
        return mem;
 }
 
-/*
- * Rounds a number down to a power of 2.
- */
-static uint32_t round_down(uint32_t n)
-{
-       while (n & (n - 1))
-               n &= (n - 1);
-       return n;
-}
-
 /*
  * Allocate room for a suitable hash table.
  */
@@ -361,9 +403,9 @@ static int init_hash_tables(struct dm_snapshot *s)
        hash_size = min(origin_dev_size, cow_dev_size) >> s->chunk_shift;
        hash_size = min(hash_size, max_buckets);
 
-       /* Round it down to a power of 2 */
-       hash_size = round_down(hash_size);
-       if (init_exception_table(&s->complete, hash_size))
+       hash_size = rounddown_pow_of_two(hash_size);
+       if (init_exception_table(&s->complete, hash_size,
+                                DM_CHUNK_CONSECUTIVE_BITS))
                return -ENOMEM;
 
        /*
@@ -374,7 +416,7 @@ static int init_hash_tables(struct dm_snapshot *s)
        if (hash_size < 64)
                hash_size = 64;
 
-       if (init_exception_table(&s->pending, hash_size)) {
+       if (init_exception_table(&s->pending, hash_size, 0)) {
                exit_exception_table(&s->complete, exception_cache);
                return -ENOMEM;
        }
@@ -494,7 +536,7 @@ static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
        s->last_percent = 0;
        init_rwsem(&s->lock);
        spin_lock_init(&s->pe_lock);
-       s->table = ti->table;
+       s->ti = ti;
 
        /* Allocate hash table for COW data */
        if (init_hash_tables(s)) {
@@ -516,7 +558,7 @@ static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
                goto bad4;
        }
 
-       r = kcopyd_client_create(SNAPSHOT_PAGES, &s->kcopyd_client);
+       r = dm_kcopyd_client_create(SNAPSHOT_PAGES, &s->kcopyd_client);
        if (r) {
                ti->error = "Could not create kcopyd client";
                goto bad5;
@@ -549,7 +591,7 @@ static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
        return 0;
 
  bad6:
-       kcopyd_client_destroy(s->kcopyd_client);
+       dm_kcopyd_client_destroy(s->kcopyd_client);
 
  bad5:
        s->store.destroy(&s->store);
@@ -571,7 +613,7 @@ static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
 
 static void __free_exceptions(struct dm_snapshot *s)
 {
-       kcopyd_client_destroy(s->kcopyd_client);
+       dm_kcopyd_client_destroy(s->kcopyd_client);
        s->kcopyd_client = NULL;
 
        exit_exception_table(&s->pending, pending_cache);
@@ -657,7 +699,7 @@ static void __invalidate_snapshot(struct dm_snapshot *s, int err)
 
        s->valid = 0;
 
-       dm_table_event(s->table);
+       dm_table_event(s->ti->table);
 }
 
 static void get_pending_exception(struct dm_snap_pending_exception *pe)
@@ -733,7 +775,7 @@ static void pending_complete(struct dm_snap_pending_exception *pe, int success)
         * Add a proper exception, and remove the
         * in-flight exception from the list.
         */
-       insert_exception(&s->complete, e);
+       insert_completed_exception(s, e);
 
  out:
        remove_exception(&pe->e);
@@ -762,7 +804,7 @@ static void commit_callback(void *context, int success)
  * Called when the copy I/O has finished.  kcopyd actually runs
  * this code so don't block.
  */
-static void copy_callback(int read_err, unsigned int write_err, void *context)
+static void copy_callback(int read_err, unsigned long write_err, void *context)
 {
        struct dm_snap_pending_exception *pe = context;
        struct dm_snapshot *s = pe->snap;
@@ -782,7 +824,7 @@ static void copy_callback(int read_err, unsigned int write_err, void *context)
 static void start_copy(struct dm_snap_pending_exception *pe)
 {
        struct dm_snapshot *s = pe->snap;
-       struct io_region src, dest;
+       struct dm_io_region src, dest;
        struct block_device *bdev = s->origin->bdev;
        sector_t dev_size;
 
@@ -797,7 +839,7 @@ static void start_copy(struct dm_snap_pending_exception *pe)
        dest.count = src.count;
 
        /* Hand over to kcopyd */
-       kcopyd_copy(s->kcopyd_client,
+       dm_kcopyd_copy(s->kcopyd_client,
                    &src, 1, &dest, 0, copy_callback, pe);
 }
 
@@ -867,11 +909,12 @@ __find_pending_exception(struct dm_snapshot *s, struct bio *bio)
 }
 
 static void remap_exception(struct dm_snapshot *s, struct dm_snap_exception *e,
-                           struct bio *bio)
+                           struct bio *bio, chunk_t chunk)
 {
        bio->bi_bdev = s->cow->bdev;
-       bio->bi_sector = chunk_to_sector(s, e->new_chunk) +
-               (bio->bi_sector & s->chunk_mask);
+       bio->bi_sector = chunk_to_sector(s, dm_chunk_number(e->new_chunk) +
+                        (chunk - e->old_chunk)) +
+                        (bio->bi_sector & s->chunk_mask);
 }
 
 static int snapshot_map(struct dm_target *ti, struct bio *bio,
@@ -902,7 +945,7 @@ static int snapshot_map(struct dm_target *ti, struct bio *bio,
        /* If the block is already remapped - use that, else remap it */
        e = lookup_exception(&s->complete, chunk);
        if (e) {
-               remap_exception(s, e, bio);
+               remap_exception(s, e, bio, chunk);
                goto out_unlock;
        }
 
@@ -919,7 +962,7 @@ static int snapshot_map(struct dm_target *ti, struct bio *bio,
                        goto out_unlock;
                }
 
-               remap_exception(s, &pe->e, bio);
+               remap_exception(s, &pe->e, bio, chunk);
                bio_list_add(&pe->snapshot_bios, bio);
 
                r = DM_MAPIO_SUBMITTED;
@@ -1017,7 +1060,7 @@ static int __origin_write(struct list_head *snapshots, struct bio *bio)
                        goto next_snapshot;
 
                /* Nothing to do if writing beyond end of snapshot */
-               if (bio->bi_sector >= dm_table_get_size(snap->table))
+               if (bio->bi_sector >= dm_table_get_size(snap->ti->table))
                        goto next_snapshot;
 
                /*
@@ -1207,7 +1250,7 @@ static int origin_status(struct dm_target *ti, status_type_t type, char *result,
 
 static struct target_type origin_target = {
        .name    = "snapshot-origin",
-       .version = {1, 5, 0},
+       .version = {1, 6, 0},
        .module  = THIS_MODULE,
        .ctr     = origin_ctr,
        .dtr     = origin_dtr,
@@ -1218,7 +1261,7 @@ static struct target_type origin_target = {
 
 static struct target_type snapshot_target = {
        .name    = "snapshot",
-       .version = {1, 5, 0},
+       .version = {1, 6, 0},
        .module  = THIS_MODULE,
        .ctr     = snapshot_ctr,
        .dtr     = snapshot_dtr,