dm raid1: fix deadlock when suspending failed device
[safe/jmp/linux-2.6] / drivers / md / dm-log.c
index e110655..5a08be0 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2003 Sistina Software
+ * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
  *
  * This file is released under the LGPL.
  */
 #include <linux/slab.h>
 #include <linux/module.h>
 #include <linux/vmalloc.h>
+#include <linux/dm-io.h>
+#include <linux/dm-dirty-log.h>
 
-#include "dm-log.h"
-#include "dm-io.h"
+#include <linux/device-mapper.h>
+
+#define DM_MSG_PREFIX "dirty region log"
 
 static LIST_HEAD(_log_types);
 static DEFINE_SPINLOCK(_lock);
 
-int dm_register_dirty_log_type(struct dirty_log_type *type)
+static struct dm_dirty_log_type *__find_dirty_log_type(const char *name)
 {
-       spin_lock(&_lock);
-       type->use_count = 0;
-       list_add(&type->list, &_log_types);
-       spin_unlock(&_lock);
+       struct dm_dirty_log_type *log_type;
 
-       return 0;
+       list_for_each_entry(log_type, &_log_types, list)
+               if (!strcmp(name, log_type->name))
+                       return log_type;
+
+       return NULL;
 }
 
-int dm_unregister_dirty_log_type(struct dirty_log_type *type)
+static struct dm_dirty_log_type *_get_dirty_log_type(const char *name)
 {
+       struct dm_dirty_log_type *log_type;
+
        spin_lock(&_lock);
 
-       if (type->use_count)
-               DMWARN("Attempt to unregister a log type that is still in use");
-       else
-               list_del(&type->list);
+       log_type = __find_dirty_log_type(name);
+       if (log_type && !try_module_get(log_type->module))
+               log_type = NULL;
 
        spin_unlock(&_lock);
 
-       return 0;
+       return log_type;
+}
+
+/*
+ * get_type
+ * @type_name
+ *
+ * Attempt to retrieve the dm_dirty_log_type by name.  If not already
+ * available, attempt to load the appropriate module.
+ *
+ * Log modules are named "dm-log-" followed by the 'type_name'.
+ * Modules may contain multiple types.
+ * This function will first try the module "dm-log-<type_name>",
+ * then truncate 'type_name' on the last '-' and try again.
+ *
+ * For example, if type_name was "clustered-disk", it would search
+ * 'dm-log-clustered-disk' then 'dm-log-clustered'.
+ *
+ * Returns: dirty_log_type* on success, NULL on failure
+ */
+static struct dm_dirty_log_type *get_type(const char *type_name)
+{
+       char *p, *type_name_dup;
+       struct dm_dirty_log_type *log_type;
+
+       if (!type_name)
+               return NULL;
+
+       log_type = _get_dirty_log_type(type_name);
+       if (log_type)
+               return log_type;
+
+       type_name_dup = kstrdup(type_name, GFP_KERNEL);
+       if (!type_name_dup) {
+               DMWARN("No memory left to attempt log module load for \"%s\"",
+                      type_name);
+               return NULL;
+       }
+
+       while (request_module("dm-log-%s", type_name_dup) ||
+              !(log_type = _get_dirty_log_type(type_name))) {
+               p = strrchr(type_name_dup, '-');
+               if (!p)
+                       break;
+               p[0] = '\0';
+       }
+
+       if (!log_type)
+               DMWARN("Module for logging type \"%s\" not found.", type_name);
+
+       kfree(type_name_dup);
+
+       return log_type;
 }
 
-static struct dirty_log_type *get_type(const char *type_name)
+static void put_type(struct dm_dirty_log_type *type)
 {
-       struct dirty_log_type *type;
+       if (!type)
+               return;
 
        spin_lock(&_lock);
-       list_for_each_entry (type, &_log_types, list)
-               if (!strcmp(type_name, type->name)) {
-                       if (!type->use_count && !try_module_get(type->module)){
-                               spin_unlock(&_lock);
-                               return NULL;
-                       }
-                       type->use_count++;
-                       spin_unlock(&_lock);
-                       return type;
-               }
+       if (!__find_dirty_log_type(type->name))
+               goto out;
 
+       module_put(type->module);
+
+out:
        spin_unlock(&_lock);
-       return NULL;
 }
 
-static void put_type(struct dirty_log_type *type)
+int dm_dirty_log_type_register(struct dm_dirty_log_type *type)
 {
+       int r = 0;
+
        spin_lock(&_lock);
-       if (!--type->use_count)
-               module_put(type->module);
+       if (!__find_dirty_log_type(type->name))
+               list_add(&type->list, &_log_types);
+       else
+               r = -EEXIST;
        spin_unlock(&_lock);
+
+       return r;
 }
+EXPORT_SYMBOL(dm_dirty_log_type_register);
 
-struct dirty_log *dm_create_dirty_log(const char *type_name, struct dm_target *ti,
-                                     unsigned int argc, char **argv)
+int dm_dirty_log_type_unregister(struct dm_dirty_log_type *type)
 {
-       struct dirty_log_type *type;
-       struct dirty_log *log;
+       spin_lock(&_lock);
+
+       if (!__find_dirty_log_type(type->name)) {
+               spin_unlock(&_lock);
+               return -EINVAL;
+       }
+
+       list_del(&type->list);
+
+       spin_unlock(&_lock);
+
+       return 0;
+}
+EXPORT_SYMBOL(dm_dirty_log_type_unregister);
+
+struct dm_dirty_log *dm_dirty_log_create(const char *type_name,
+                       struct dm_target *ti,
+                       int (*flush_callback_fn)(struct dm_target *ti),
+                       unsigned int argc, char **argv)
+{
+       struct dm_dirty_log_type *type;
+       struct dm_dirty_log *log;
 
        log = kmalloc(sizeof(*log), GFP_KERNEL);
        if (!log)
@@ -83,6 +162,7 @@ struct dirty_log *dm_create_dirty_log(const char *type_name, struct dm_target *t
                return NULL;
        }
 
+       log->flush_callback_fn = flush_callback_fn;
        log->type = type;
        if (type->ctr(log, ti, argc, argv)) {
                kfree(log);
@@ -92,13 +172,15 @@ struct dirty_log *dm_create_dirty_log(const char *type_name, struct dm_target *t
 
        return log;
 }
+EXPORT_SYMBOL(dm_dirty_log_create);
 
-void dm_destroy_dirty_log(struct dirty_log *log)
+void dm_dirty_log_destroy(struct dm_dirty_log *log)
 {
        log->type->dtr(log);
        put_type(log->type);
        kfree(log);
 }
+EXPORT_SYMBOL(dm_dirty_log_destroy);
 
 /*-----------------------------------------------------------------
  * Persistent and core logs share a lot of their implementation.
@@ -112,7 +194,7 @@ void dm_destroy_dirty_log(struct dirty_log *log)
 /*
  * The on-disk version of the metadata.
  */
-#define MIRROR_DISK_VERSION 1
+#define MIRROR_DISK_VERSION 2
 #define LOG_OFFSET 2
 
 struct log_header {
@@ -128,7 +210,9 @@ struct log_header {
 
 struct log_c {
        struct dm_target *ti;
-       int touched;
+       int touched_dirtied;
+       int touched_cleaned;
+       int flush_failed;
        uint32_t region_size;
        unsigned int region_count;
        region_t sync_count;
@@ -147,40 +231,41 @@ struct log_c {
                FORCESYNC,      /* Force a sync to happen */
        } sync;
 
+       struct dm_io_request io_req;
+
        /*
         * Disk log fields
         */
+       int log_dev_failed;
+       int log_dev_flush_failed;
        struct dm_dev *log_dev;
        struct log_header header;
 
-       struct io_region header_location;
+       struct dm_io_region header_location;
        struct log_header *disk_header;
-
-       struct io_region bits_location;
-       uint32_t *disk_bits;
 };
 
 /*
  * The touched member needs to be updated every time we access
  * one of the bitsets.
  */
-static  inline int log_test_bit(uint32_t *bs, unsigned bit)
+static inline int log_test_bit(uint32_t *bs, unsigned bit)
 {
-       return test_bit(bit, (unsigned long *) bs) ? 1 : 0;
+       return ext2_test_bit(bit, (unsigned long *) bs) ? 1 : 0;
 }
 
 static inline void log_set_bit(struct log_c *l,
                               uint32_t *bs, unsigned bit)
 {
-       set_bit(bit, (unsigned long *) bs);
-       l->touched = 1;
+       ext2_set_bit(bit, (unsigned long *) bs);
+       l->touched_cleaned = 1;
 }
 
 static inline void log_clear_bit(struct log_c *l,
                                 uint32_t *bs, unsigned bit)
 {
-       clear_bit(bit, (unsigned long *) bs);
-       l->touched = 1;
+       ext2_clear_bit(bit, (unsigned long *) bs);
+       l->touched_dirtied = 1;
 }
 
 /*----------------------------------------------------------------
@@ -200,13 +285,31 @@ static void header_from_disk(struct log_header *core, struct log_header *disk)
        core->nr_regions = le64_to_cpu(disk->nr_regions);
 }
 
+static int rw_header(struct log_c *lc, int rw)
+{
+       lc->io_req.bi_rw = rw;
+
+       return dm_io(&lc->io_req, 1, &lc->header_location, NULL);
+}
+
+static int flush_header(struct log_c *lc)
+{
+       struct dm_io_region null_location = {
+               .bdev = lc->header_location.bdev,
+               .sector = 0,
+               .count = 0,
+       };
+
+       lc->io_req.bi_rw = WRITE_BARRIER;
+
+       return dm_io(&lc->io_req, 1, &null_location, NULL);
+}
+
 static int read_header(struct log_c *log)
 {
        int r;
-       unsigned long ebits;
 
-       r = dm_io_sync_vm(1, &log->header_location, READ,
-                         log->disk_header, &ebits);
+       r = rw_header(log, READ);
        if (r)
                return r;
 
@@ -219,6 +322,11 @@ static int read_header(struct log_c *log)
                log->header.nr_regions = 0;
        }
 
+#ifdef __LITTLE_ENDIAN
+       if (log->header.version == 1)
+               log->header.version = 2;
+#endif
+
        if (log->header.version != MIRROR_DISK_VERSION) {
                DMWARN("incompatible disk log version");
                return -EINVAL;
@@ -227,57 +335,15 @@ static int read_header(struct log_c *log)
        return 0;
 }
 
-static inline int write_header(struct log_c *log)
+static int _check_region_size(struct dm_target *ti, uint32_t region_size)
 {
-       unsigned long ebits;
-
-       header_to_disk(&log->header, log->disk_header);
-       return dm_io_sync_vm(1, &log->header_location, WRITE,
-                            log->disk_header, &ebits);
-}
-
-/*----------------------------------------------------------------
- * Bits IO
- *--------------------------------------------------------------*/
-static inline void bits_to_core(uint32_t *core, uint32_t *disk, unsigned count)
-{
-       unsigned i;
-
-       for (i = 0; i < count; i++)
-               core[i] = le32_to_cpu(disk[i]);
-}
-
-static inline void bits_to_disk(uint32_t *core, uint32_t *disk, unsigned count)
-{
-       unsigned i;
-
-       /* copy across the clean/dirty bitset */
-       for (i = 0; i < count; i++)
-               disk[i] = cpu_to_le32(core[i]);
-}
-
-static int read_bits(struct log_c *log)
-{
-       int r;
-       unsigned long ebits;
-
-       r = dm_io_sync_vm(1, &log->bits_location, READ,
-                         log->disk_bits, &ebits);
-       if (r)
-               return r;
+       if (region_size < 2 || region_size > ti->len)
+               return 0;
 
-       bits_to_core(log->clean_bits, log->disk_bits,
-                    log->bitset_uint32_count);
-       return 0;
-}
+       if (!is_power_of_2(region_size))
+               return 0;
 
-static int write_bits(struct log_c *log)
-{
-       unsigned long ebits;
-       bits_to_disk(log->clean_bits, log->disk_bits,
-                    log->bitset_uint32_count);
-       return dm_io_sync_vm(1, &log->bits_location, WRITE,
-                            log->disk_bits, &ebits);
+       return 1;
 }
 
 /*----------------------------------------------------------------
@@ -286,18 +352,20 @@ static int write_bits(struct log_c *log)
  * argv contains region_size followed optionally by [no]sync
  *--------------------------------------------------------------*/
 #define BYTE_SHIFT 3
-static int core_ctr(struct dirty_log *log, struct dm_target *ti,
-                   unsigned int argc, char **argv)
+static int create_log_context(struct dm_dirty_log *log, struct dm_target *ti,
+                             unsigned int argc, char **argv,
+                             struct dm_dev *dev)
 {
        enum sync sync = DEFAULTSYNC;
 
        struct log_c *lc;
        uint32_t region_size;
        unsigned int region_count;
-       size_t bitset_size;
+       size_t bitset_size, buf_size;
+       int r;
 
        if (argc < 1 || argc > 2) {
-               DMWARN("wrong number of arguments to mirror log");
+               DMWARN("wrong number of arguments to dirty region log");
                return -EINVAL;
        }
 
@@ -307,14 +375,15 @@ static int core_ctr(struct dirty_log *log, struct dm_target *ti,
                else if (!strcmp(argv[1], "nosync"))
                        sync = NOSYNC;
                else {
-                       DMWARN("unrecognised sync argument to mirror log: %s",
-                              argv[1]);
+                       DMWARN("unrecognised sync argument to "
+                              "dirty region log: %s", argv[1]);
                        return -EINVAL;
                }
        }
 
-       if (sscanf(argv[0], "%u", &region_size) != 1) {
-               DMWARN("invalid region size string");
+       if (sscanf(argv[0], "%u", &region_size) != 1 ||
+           !_check_region_size(ti, region_size)) {
+               DMWARN("invalid region size %s", argv[0]);
                return -EINVAL;
        }
 
@@ -327,31 +396,91 @@ static int core_ctr(struct dirty_log *log, struct dm_target *ti,
        }
 
        lc->ti = ti;
-       lc->touched = 0;
+       lc->touched_dirtied = 0;
+       lc->touched_cleaned = 0;
+       lc->flush_failed = 0;
        lc->region_size = region_size;
        lc->region_count = region_count;
        lc->sync = sync;
 
        /*
-        * Work out how many words we need to hold the bitset.
+        * Work out how many "unsigned long"s we need to hold the bitset.
         */
        bitset_size = dm_round_up(region_count,
                                  sizeof(*lc->clean_bits) << BYTE_SHIFT);
        bitset_size >>= BYTE_SHIFT;
 
-       lc->bitset_uint32_count = bitset_size / 4;
-       lc->clean_bits = vmalloc(bitset_size);
-       if (!lc->clean_bits) {
-               DMWARN("couldn't allocate clean bitset");
-               kfree(lc);
-               return -ENOMEM;
+       lc->bitset_uint32_count = bitset_size / sizeof(*lc->clean_bits);
+
+       /*
+        * Disk log?
+        */
+       if (!dev) {
+               lc->clean_bits = vmalloc(bitset_size);
+               if (!lc->clean_bits) {
+                       DMWARN("couldn't allocate clean bitset");
+                       kfree(lc);
+                       return -ENOMEM;
+               }
+               lc->disk_header = NULL;
+       } else {
+               lc->log_dev = dev;
+               lc->log_dev_failed = 0;
+               lc->log_dev_flush_failed = 0;
+               lc->header_location.bdev = lc->log_dev->bdev;
+               lc->header_location.sector = 0;
+
+               /*
+                * Buffer holds both header and bitset.
+                */
+               buf_size =
+                   dm_round_up((LOG_OFFSET << SECTOR_SHIFT) + bitset_size,
+                               bdev_logical_block_size(lc->header_location.
+                                                           bdev));
+
+               if (buf_size > i_size_read(dev->bdev->bd_inode)) {
+                       DMWARN("log device %s too small: need %llu bytes",
+                               dev->name, (unsigned long long)buf_size);
+                       kfree(lc);
+                       return -EINVAL;
+               }
+
+               lc->header_location.count = buf_size >> SECTOR_SHIFT;
+
+               lc->io_req.mem.type = DM_IO_VMA;
+               lc->io_req.notify.fn = NULL;
+               lc->io_req.client = dm_io_client_create(dm_div_up(buf_size,
+                                                                  PAGE_SIZE));
+               if (IS_ERR(lc->io_req.client)) {
+                       r = PTR_ERR(lc->io_req.client);
+                       DMWARN("couldn't allocate disk io client");
+                       kfree(lc);
+                       return -ENOMEM;
+               }
+
+               lc->disk_header = vmalloc(buf_size);
+               if (!lc->disk_header) {
+                       DMWARN("couldn't allocate disk log buffer");
+                       dm_io_client_destroy(lc->io_req.client);
+                       kfree(lc);
+                       return -ENOMEM;
+               }
+
+               lc->io_req.mem.ptr.vma = lc->disk_header;
+               lc->clean_bits = (void *)lc->disk_header +
+                                (LOG_OFFSET << SECTOR_SHIFT);
        }
+
        memset(lc->clean_bits, -1, bitset_size);
 
        lc->sync_bits = vmalloc(bitset_size);
        if (!lc->sync_bits) {
                DMWARN("couldn't allocate sync bitset");
-               vfree(lc->clean_bits);
+               if (!dev)
+                       vfree(lc->clean_bits);
+               else
+                       dm_io_client_destroy(lc->io_req.client);
+               vfree(lc->disk_header);
                kfree(lc);
                return -ENOMEM;
        }
@@ -362,97 +491,79 @@ static int core_ctr(struct dirty_log *log, struct dm_target *ti,
        if (!lc->recovering_bits) {
                DMWARN("couldn't allocate sync bitset");
                vfree(lc->sync_bits);
-               vfree(lc->clean_bits);
+               if (!dev)
+                       vfree(lc->clean_bits);
+               else
+                       dm_io_client_destroy(lc->io_req.client);
+               vfree(lc->disk_header);
                kfree(lc);
                return -ENOMEM;
        }
        memset(lc->recovering_bits, 0, bitset_size);
        lc->sync_search = 0;
        log->context = lc;
+
        return 0;
 }
 
-static void core_dtr(struct dirty_log *log)
+static int core_ctr(struct dm_dirty_log *log, struct dm_target *ti,
+                   unsigned int argc, char **argv)
+{
+       return create_log_context(log, ti, argc, argv, NULL);
+}
+
+static void destroy_log_context(struct log_c *lc)
 {
-       struct log_c *lc = (struct log_c *) log->context;
-       vfree(lc->clean_bits);
        vfree(lc->sync_bits);
        vfree(lc->recovering_bits);
        kfree(lc);
 }
 
+static void core_dtr(struct dm_dirty_log *log)
+{
+       struct log_c *lc = (struct log_c *) log->context;
+
+       vfree(lc->clean_bits);
+       destroy_log_context(lc);
+}
+
 /*----------------------------------------------------------------
  * disk log constructor/destructor
  *
  * argv contains log_device region_size followed optionally by [no]sync
  *--------------------------------------------------------------*/
-static int disk_ctr(struct dirty_log *log, struct dm_target *ti,
+static int disk_ctr(struct dm_dirty_log *log, struct dm_target *ti,
                    unsigned int argc, char **argv)
 {
        int r;
-       size_t size;
-       struct log_c *lc;
        struct dm_dev *dev;
 
        if (argc < 2 || argc > 3) {
-               DMWARN("wrong number of arguments to disk mirror log");
+               DMWARN("wrong number of arguments to disk dirty region log");
                return -EINVAL;
        }
 
-       r = dm_get_device(ti, argv[0], 0, 0 /* FIXME */,
-                         FMODE_READ | FMODE_WRITE, &dev);
+       r = dm_get_device(ti, argv[0], FMODE_READ | FMODE_WRITE, &dev);
        if (r)
                return r;
 
-       r = core_ctr(log, ti, argc - 1, argv + 1);
+       r = create_log_context(log, ti, argc - 1, argv + 1, dev);
        if (r) {
                dm_put_device(ti, dev);
                return r;
        }
 
-       lc = (struct log_c *) log->context;
-       lc->log_dev = dev;
-
-       /* setup the disk header fields */
-       lc->header_location.bdev = lc->log_dev->bdev;
-       lc->header_location.sector = 0;
-       lc->header_location.count = 1;
-
-       /*
-        * We can't read less than this amount, even though we'll
-        * not be using most of this space.
-        */
-       lc->disk_header = vmalloc(1 << SECTOR_SHIFT);
-       if (!lc->disk_header)
-               goto bad;
-
-       /* setup the disk bitset fields */
-       lc->bits_location.bdev = lc->log_dev->bdev;
-       lc->bits_location.sector = LOG_OFFSET;
-
-       size = dm_round_up(lc->bitset_uint32_count * sizeof(uint32_t),
-                          1 << SECTOR_SHIFT);
-       lc->bits_location.count = size >> SECTOR_SHIFT;
-       lc->disk_bits = vmalloc(size);
-       if (!lc->disk_bits) {
-               vfree(lc->disk_header);
-               goto bad;
-       }
        return 0;
-
- bad:
-       dm_put_device(ti, lc->log_dev);
-       core_dtr(log);
-       return -ENOMEM;
 }
 
-static void disk_dtr(struct dirty_log *log)
+static void disk_dtr(struct dm_dirty_log *log)
 {
        struct log_c *lc = (struct log_c *) log->context;
+
        dm_put_device(lc->ti, lc->log_dev);
        vfree(lc->disk_header);
-       vfree(lc->disk_bits);
-       core_dtr(log);
+       dm_io_client_destroy(lc->io_req.client);
+       destroy_log_context(lc);
 }
 
 static int count_bits32(uint32_t *addr, unsigned size)
@@ -465,7 +576,16 @@ static int count_bits32(uint32_t *addr, unsigned size)
        return count;
 }
 
-static int disk_resume(struct dirty_log *log)
+static void fail_log_device(struct log_c *lc)
+{
+       if (lc->log_dev_failed)
+               return;
+
+       lc->log_dev_failed = 1;
+       dm_table_event(lc->ti->table);
+}
+
+static int disk_resume(struct dm_dirty_log *log)
 {
        int r;
        unsigned i;
@@ -474,15 +594,21 @@ static int disk_resume(struct dirty_log *log)
 
        /* read the disk header */
        r = read_header(lc);
-       if (r)
-               return r;
-
-       /* read the bits */
-       r = read_bits(lc);
-       if (r)
-               return r;
+       if (r) {
+               DMWARN("%s: Failed to read header on dirty region log device",
+                      lc->log_dev->name);
+               fail_log_device(lc);
+               /*
+                * If the log device cannot be read, we must assume
+                * all regions are out-of-sync.  If we simply return
+                * here, the state will be uninitialized and could
+                * lead us to return 'in-sync' status for regions
+                * that are actually 'out-of-sync'.
+                */
+               lc->header.nr_regions = 0;
+       }
 
-       /* set or clear any new bits */
+       /* set or clear any new bits -- device has grown */
        if (lc->sync == NOSYNC)
                for (i = lc->header.nr_regions; i < lc->region_count; i++)
                        /* FIXME: amazingly inefficient */
@@ -492,75 +618,121 @@ static int disk_resume(struct dirty_log *log)
                        /* FIXME: amazingly inefficient */
                        log_clear_bit(lc, lc->clean_bits, i);
 
+       /* clear any old bits -- device has shrunk */
+       for (i = lc->region_count; i % (sizeof(*lc->clean_bits) << BYTE_SHIFT); i++)
+               log_clear_bit(lc, lc->clean_bits, i);
+
        /* copy clean across to sync */
        memcpy(lc->sync_bits, lc->clean_bits, size);
        lc->sync_count = count_bits32(lc->clean_bits, lc->bitset_uint32_count);
-
-       /* write the bits */
-       r = write_bits(lc);
-       if (r)
-               return r;
+       lc->sync_search = 0;
 
        /* set the correct number of regions in the header */
        lc->header.nr_regions = lc->region_count;
 
+       header_to_disk(&lc->header, lc->disk_header);
+
        /* write the new header */
-       return write_header(lc);
+       r = rw_header(lc, WRITE);
+       if (!r) {
+               r = flush_header(lc);
+               if (r)
+                       lc->log_dev_flush_failed = 1;
+       }
+       if (r) {
+               DMWARN("%s: Failed to write header on dirty region log device",
+                      lc->log_dev->name);
+               fail_log_device(lc);
+       }
+
+       return r;
 }
 
-static uint32_t core_get_region_size(struct dirty_log *log)
+static uint32_t core_get_region_size(struct dm_dirty_log *log)
 {
        struct log_c *lc = (struct log_c *) log->context;
        return lc->region_size;
 }
 
-static int core_is_clean(struct dirty_log *log, region_t region)
+static int core_resume(struct dm_dirty_log *log)
+{
+       struct log_c *lc = (struct log_c *) log->context;
+       lc->sync_search = 0;
+       return 0;
+}
+
+static int core_is_clean(struct dm_dirty_log *log, region_t region)
 {
        struct log_c *lc = (struct log_c *) log->context;
        return log_test_bit(lc->clean_bits, region);
 }
 
-static int core_in_sync(struct dirty_log *log, region_t region, int block)
+static int core_in_sync(struct dm_dirty_log *log, region_t region, int block)
 {
        struct log_c *lc = (struct log_c *) log->context;
        return log_test_bit(lc->sync_bits, region);
 }
 
-static int core_flush(struct dirty_log *log)
+static int core_flush(struct dm_dirty_log *log)
 {
        /* no op */
        return 0;
 }
 
-static int disk_flush(struct dirty_log *log)
+static int disk_flush(struct dm_dirty_log *log)
 {
-       int r;
-       struct log_c *lc = (struct log_c *) log->context;
+       int r, i;
+       struct log_c *lc = log->context;
 
        /* only write if the log has changed */
-       if (!lc->touched)
+       if (!lc->touched_cleaned && !lc->touched_dirtied)
                return 0;
 
-       r = write_bits(lc);
-       if (!r)
-               lc->touched = 0;
+       if (lc->touched_cleaned && log->flush_callback_fn &&
+           log->flush_callback_fn(lc->ti)) {
+               /*
+                * At this point it is impossible to determine which
+                * regions are clean and which are dirty (without
+                * re-reading the log off disk). So mark all of them
+                * dirty.
+                */
+               lc->flush_failed = 1;
+               for (i = 0; i < lc->region_count; i++)
+                       log_clear_bit(lc, lc->clean_bits, i);
+       }
+
+       r = rw_header(lc, WRITE);
+       if (r)
+               fail_log_device(lc);
+       else {
+               if (lc->touched_dirtied) {
+                       r = flush_header(lc);
+                       if (r) {
+                               lc->log_dev_flush_failed = 1;
+                               fail_log_device(lc);
+                       } else
+                               lc->touched_dirtied = 0;
+               }
+               lc->touched_cleaned = 0;
+       }
 
        return r;
 }
 
-static void core_mark_region(struct dirty_log *log, region_t region)
+static void core_mark_region(struct dm_dirty_log *log, region_t region)
 {
        struct log_c *lc = (struct log_c *) log->context;
        log_clear_bit(lc, lc->clean_bits, region);
 }
 
-static void core_clear_region(struct dirty_log *log, region_t region)
+static void core_clear_region(struct dm_dirty_log *log, region_t region)
 {
        struct log_c *lc = (struct log_c *) log->context;
-       log_set_bit(lc, lc->clean_bits, region);
+       if (likely(!lc->flush_failed))
+               log_set_bit(lc, lc->clean_bits, region);
 }
 
-static int core_get_resync_work(struct dirty_log *log, region_t *region)
+static int core_get_resync_work(struct dm_dirty_log *log, region_t *region)
 {
        struct log_c *lc = (struct log_c *) log->context;
 
@@ -568,12 +740,13 @@ static int core_get_resync_work(struct dirty_log *log, region_t *region)
                return 0;
 
        do {
-               *region = find_next_zero_bit((unsigned long *) lc->sync_bits,
+               *region = ext2_find_next_zero_bit(
+                                            (unsigned long *) lc->sync_bits,
                                             lc->region_count,
                                             lc->sync_search);
                lc->sync_search = *region + 1;
 
-               if (*region == lc->region_count)
+               if (*region >= lc->region_count)
                        return 0;
 
        } while (log_test_bit(lc->recovering_bits, *region));
@@ -582,19 +755,22 @@ static int core_get_resync_work(struct dirty_log *log, region_t *region)
        return 1;
 }
 
-static void core_complete_resync_work(struct dirty_log *log, region_t region,
-                                     int success)
+static void core_set_region_sync(struct dm_dirty_log *log, region_t region,
+                                int in_sync)
 {
        struct log_c *lc = (struct log_c *) log->context;
 
        log_clear_bit(lc, lc->recovering_bits, region);
-       if (success) {
+       if (in_sync) {
                log_set_bit(lc, lc->sync_bits, region);
                 lc->sync_count++;
-        }
+        } else if (log_test_bit(lc->sync_bits, region)) {
+               lc->sync_count--;
+               log_clear_bit(lc, lc->sync_bits, region);
+       }
 }
 
-static region_t core_get_sync_count(struct dirty_log *log)
+static region_t core_get_sync_count(struct dm_dirty_log *log)
 {
         struct log_c *lc = (struct log_c *) log->context;
 
@@ -605,7 +781,7 @@ static region_t core_get_sync_count(struct dirty_log *log)
        if (lc->sync != DEFAULTSYNC) \
                DMEMIT("%ssync ", lc->sync == NOSYNC ? "no" : "")
 
-static int core_status(struct dirty_log *log, status_type_t status,
+static int core_status(struct dm_dirty_log *log, status_type_t status,
                       char *result, unsigned int maxlen)
 {
        int sz = 0;
@@ -613,6 +789,7 @@ static int core_status(struct dirty_log *log, status_type_t status,
 
        switch(status) {
        case STATUSTYPE_INFO:
+               DMEMIT("1 %s", log->type->name);
                break;
 
        case STATUSTYPE_TABLE:
@@ -624,21 +801,23 @@ static int core_status(struct dirty_log *log, status_type_t status,
        return sz;
 }
 
-static int disk_status(struct dirty_log *log, status_type_t status,
+static int disk_status(struct dm_dirty_log *log, status_type_t status,
                       char *result, unsigned int maxlen)
 {
        int sz = 0;
-       char buffer[16];
        struct log_c *lc = log->context;
 
        switch(status) {
        case STATUSTYPE_INFO:
+               DMEMIT("3 %s %s %c", log->type->name, lc->log_dev->name,
+                      lc->log_dev_flush_failed ? 'F' :
+                      lc->log_dev_failed ? 'D' :
+                      'A');
                break;
 
        case STATUSTYPE_TABLE:
-               format_dev_t(buffer, lc->log_dev->bdev->bd_dev);
                DMEMIT("%s %u %s %u ", log->type->name,
-                      lc->sync == DEFAULTSYNC ? 2 : 3, buffer,
+                      lc->sync == DEFAULTSYNC ? 2 : 3, lc->log_dev->name,
                       lc->region_size);
                DMEMIT_SYNC;
        }
@@ -646,11 +825,12 @@ static int disk_status(struct dirty_log *log, status_type_t status,
        return sz;
 }
 
-static struct dirty_log_type _core_type = {
+static struct dm_dirty_log_type _core_type = {
        .name = "core",
        .module = THIS_MODULE,
        .ctr = core_ctr,
        .dtr = core_dtr,
+       .resume = core_resume,
        .get_region_size = core_get_region_size,
        .is_clean = core_is_clean,
        .in_sync = core_in_sync,
@@ -658,17 +838,17 @@ static struct dirty_log_type _core_type = {
        .mark_region = core_mark_region,
        .clear_region = core_clear_region,
        .get_resync_work = core_get_resync_work,
-       .complete_resync_work = core_complete_resync_work,
+       .set_region_sync = core_set_region_sync,
        .get_sync_count = core_get_sync_count,
        .status = core_status,
 };
 
-static struct dirty_log_type _disk_type = {
+static struct dm_dirty_log_type _disk_type = {
        .name = "disk",
        .module = THIS_MODULE,
        .ctr = disk_ctr,
        .dtr = disk_dtr,
-       .suspend = disk_flush,
+       .postsuspend = disk_flush,
        .resume = disk_resume,
        .get_region_size = core_get_region_size,
        .is_clean = core_is_clean,
@@ -677,35 +857,37 @@ static struct dirty_log_type _disk_type = {
        .mark_region = core_mark_region,
        .clear_region = core_clear_region,
        .get_resync_work = core_get_resync_work,
-       .complete_resync_work = core_complete_resync_work,
+       .set_region_sync = core_set_region_sync,
        .get_sync_count = core_get_sync_count,
        .status = disk_status,
 };
 
-int __init dm_dirty_log_init(void)
+static int __init dm_dirty_log_init(void)
 {
        int r;
 
-       r = dm_register_dirty_log_type(&_core_type);
+       r = dm_dirty_log_type_register(&_core_type);
        if (r)
                DMWARN("couldn't register core log");
 
-       r = dm_register_dirty_log_type(&_disk_type);
+       r = dm_dirty_log_type_register(&_disk_type);
        if (r) {
                DMWARN("couldn't register disk type");
-               dm_unregister_dirty_log_type(&_core_type);
+               dm_dirty_log_type_unregister(&_core_type);
        }
 
        return r;
 }
 
-void dm_dirty_log_exit(void)
+static void __exit dm_dirty_log_exit(void)
 {
-       dm_unregister_dirty_log_type(&_disk_type);
-       dm_unregister_dirty_log_type(&_core_type);
+       dm_dirty_log_type_unregister(&_disk_type);
+       dm_dirty_log_type_unregister(&_core_type);
 }
 
-EXPORT_SYMBOL(dm_register_dirty_log_type);
-EXPORT_SYMBOL(dm_unregister_dirty_log_type);
-EXPORT_SYMBOL(dm_create_dirty_log);
-EXPORT_SYMBOL(dm_destroy_dirty_log);
+module_init(dm_dirty_log_init);
+module_exit(dm_dirty_log_exit);
+
+MODULE_DESCRIPTION(DM_NAME " dirty region log");
+MODULE_AUTHOR("Joe Thornber, Heinz Mauelshagen <dm-devel@redhat.com>");
+MODULE_LICENSE("GPL");