md: restore ability of spare drives to spin down.
[safe/jmp/linux-2.6] / drivers / md / dm-log.c
index 82df73f..5a08be0 100644 (file)
@@ -9,41 +9,47 @@
 #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 "dm.h"
+#include <linux/device-mapper.h>
 
 #define DM_MSG_PREFIX "dirty region log"
 
 static LIST_HEAD(_log_types);
 static DEFINE_SPINLOCK(_lock);
 
-static struct dm_dirty_log_type *_get_type(const char *type_name)
+static struct dm_dirty_log_type *__find_dirty_log_type(const char *name)
 {
-       struct dm_dirty_log_type *type;
+       struct dm_dirty_log_type *log_type;
+
+       list_for_each_entry(log_type, &_log_types, list)
+               if (!strcmp(name, log_type->name))
+                       return log_type;
+
+       return NULL;
+}
+
+static struct dm_dirty_log_type *_get_dirty_log_type(const char *name)
+{
+       struct dm_dirty_log_type *log_type;
 
        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;
-               }
+
+       log_type = __find_dirty_log_type(name);
+       if (log_type && !try_module_get(log_type->module))
+               log_type = NULL;
 
        spin_unlock(&_lock);
-       return NULL;
+
+       return log_type;
 }
 
 /*
  * get_type
  * @type_name
  *
- * Attempt to retrieve the dirty_log_type by name.  If not already
+ * 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'.
@@ -59,11 +65,14 @@ static struct dm_dirty_log_type *_get_type(const char *type_name)
 static struct dm_dirty_log_type *get_type(const char *type_name)
 {
        char *p, *type_name_dup;
-       struct dm_dirty_log_type *type;
+       struct dm_dirty_log_type *log_type;
+
+       if (!type_name)
+               return NULL;
 
-       type = _get_type(type_name);
-       if (type)
-               return type;
+       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) {
@@ -73,37 +82,48 @@ static struct dm_dirty_log_type *get_type(const char *type_name)
        }
 
        while (request_module("dm-log-%s", type_name_dup) ||
-              !(type = _get_type(type_name))) {
+              !(log_type = _get_dirty_log_type(type_name))) {
                p = strrchr(type_name_dup, '-');
                if (!p)
                        break;
                p[0] = '\0';
        }
 
-       if (!type)
+       if (!log_type)
                DMWARN("Module for logging type \"%s\" not found.", type_name);
 
        kfree(type_name_dup);
 
-       return type;
+       return log_type;
 }
 
 static void put_type(struct dm_dirty_log_type *type)
 {
+       if (!type)
+               return;
+
        spin_lock(&_lock);
-       if (!--type->use_count)
-               module_put(type->module);
+       if (!__find_dirty_log_type(type->name))
+               goto out;
+
+       module_put(type->module);
+
+out:
        spin_unlock(&_lock);
 }
 
 int dm_dirty_log_type_register(struct dm_dirty_log_type *type)
 {
+       int r = 0;
+
        spin_lock(&_lock);
-       type->use_count = 0;
-       list_add(&type->list, &_log_types);
+       if (!__find_dirty_log_type(type->name))
+               list_add(&type->list, &_log_types);
+       else
+               r = -EEXIST;
        spin_unlock(&_lock);
 
-       return 0;
+       return r;
 }
 EXPORT_SYMBOL(dm_dirty_log_type_register);
 
@@ -111,10 +131,12 @@ int dm_dirty_log_type_unregister(struct dm_dirty_log_type *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);
+       if (!__find_dirty_log_type(type->name)) {
+               spin_unlock(&_lock);
+               return -EINVAL;
+       }
+
+       list_del(&type->list);
 
        spin_unlock(&_lock);
 
@@ -123,8 +145,9 @@ int dm_dirty_log_type_unregister(struct dm_dirty_log_type *type)
 EXPORT_SYMBOL(dm_dirty_log_type_unregister);
 
 struct dm_dirty_log *dm_dirty_log_create(const char *type_name,
-                                        struct dm_target *ti,
-                                        unsigned int argc, char **argv)
+                       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;
@@ -139,6 +162,7 @@ struct dm_dirty_log *dm_dirty_log_create(const char *type_name,
                return NULL;
        }
 
+       log->flush_callback_fn = flush_callback_fn;
        log->type = type;
        if (type->ctr(log, ti, argc, argv)) {
                kfree(log);
@@ -186,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;
@@ -211,6 +237,7 @@ struct log_c {
         * Disk log fields
         */
        int log_dev_failed;
+       int log_dev_flush_failed;
        struct dm_dev *log_dev;
        struct log_header header;
 
@@ -231,14 +258,14 @@ static inline void log_set_bit(struct log_c *l,
                               uint32_t *bs, unsigned bit)
 {
        ext2_set_bit(bit, (unsigned long *) bs);
-       l->touched = 1;
+       l->touched_cleaned = 1;
 }
 
 static inline void log_clear_bit(struct log_c *l,
                                 uint32_t *bs, unsigned bit)
 {
        ext2_clear_bit(bit, (unsigned long *) bs);
-       l->touched = 1;
+       l->touched_dirtied = 1;
 }
 
 /*----------------------------------------------------------------
@@ -261,12 +288,23 @@ static void header_from_disk(struct log_header *core, struct log_header *disk)
 static int rw_header(struct log_c *lc, int rw)
 {
        lc->io_req.bi_rw = rw;
-       lc->io_req.mem.ptr.vma = lc->disk_header;
-       lc->io_req.notify.fn = NULL;
 
        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;
@@ -297,10 +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)
 {
-       header_to_disk(&log->header, log->disk_header);
-       return rw_header(log, WRITE);
+       if (region_size < 2 || region_size > ti->len)
+               return 0;
+
+       if (!is_power_of_2(region_size))
+               return 0;
+
+       return 1;
 }
 
 /*----------------------------------------------------------------
@@ -338,8 +381,9 @@ static int create_log_context(struct dm_dirty_log *log, struct dm_target *ti,
                }
        }
 
-       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;
        }
 
@@ -352,7 +396,9 @@ static int create_log_context(struct dm_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;
@@ -380,16 +426,29 @@ static int create_log_context(struct dm_dirty_log *log, struct dm_target *ti,
        } 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, ti->limits.hardsect_size);
+               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)) {
@@ -402,10 +461,12 @@ static int create_log_context(struct dm_dirty_log *log, struct dm_target *ti,
                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);
        }
@@ -417,6 +478,8 @@ static int create_log_context(struct dm_dirty_log *log, struct dm_target *ti,
                DMWARN("couldn't allocate sync bitset");
                if (!dev)
                        vfree(lc->clean_bits);
+               else
+                       dm_io_client_destroy(lc->io_req.client);
                vfree(lc->disk_header);
                kfree(lc);
                return -ENOMEM;
@@ -430,6 +493,8 @@ static int create_log_context(struct dm_dirty_log *log, struct dm_target *ti,
                vfree(lc->sync_bits);
                if (!dev)
                        vfree(lc->clean_bits);
+               else
+                       dm_io_client_destroy(lc->io_req.client);
                vfree(lc->disk_header);
                kfree(lc);
                return -ENOMEM;
@@ -478,8 +543,7 @@ static int disk_ctr(struct dm_dirty_log *log, struct dm_target *ti,
                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;
 
@@ -566,8 +630,15 @@ static int disk_resume(struct dm_dirty_log *log)
        /* 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 */
-       r = 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);
@@ -610,18 +681,40 @@ static int core_flush(struct dm_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_header(lc);
+       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
-               lc->touched = 0;
+       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;
 }
@@ -635,7 +728,8 @@ static void core_mark_region(struct dm_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 dm_dirty_log *log, region_t *region)
@@ -716,7 +810,9 @@ static int disk_status(struct dm_dirty_log *log, status_type_t status,
        switch(status) {
        case STATUSTYPE_INFO:
                DMEMIT("3 %s %s %c", log->type->name, lc->log_dev->name,
-                      lc->log_dev_failed ? 'D' : 'A');
+                      lc->log_dev_flush_failed ? 'F' :
+                      lc->log_dev_failed ? 'D' :
+                      'A');
                break;
 
        case STATUSTYPE_TABLE:
@@ -766,7 +862,7 @@ static struct dm_dirty_log_type _disk_type = {
        .status = disk_status,
 };
 
-int __init dm_dirty_log_init(void)
+static int __init dm_dirty_log_init(void)
 {
        int r;
 
@@ -783,7 +879,7 @@ int __init dm_dirty_log_init(void)
        return r;
 }
 
-void __exit dm_dirty_log_exit(void)
+static void __exit dm_dirty_log_exit(void)
 {
        dm_dirty_log_type_unregister(&_disk_type);
        dm_dirty_log_type_unregister(&_core_type);