eeepc-laptop: restore acpi_generate_proc_event()
[safe/jmp/linux-2.6] / drivers / md / dm-log.c
index b776701..737961f 100644 (file)
@@ -9,67 +9,58 @@
 #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);
+struct dm_dirty_log_internal {
+       struct dm_dirty_log_type *type;
 
-int dm_dirty_log_type_register(struct dm_dirty_log_type *type)
-{
-       spin_lock(&_lock);
-       type->use_count = 0;
-       list_add(&type->list, &_log_types);
-       spin_unlock(&_lock);
+       struct list_head list;
+       long use;
+};
 
-       return 0;
-}
-EXPORT_SYMBOL(dm_dirty_log_type_register);
+static LIST_HEAD(_log_types);
+static DEFINE_SPINLOCK(_lock);
 
-int dm_dirty_log_type_unregister(struct dm_dirty_log_type *type)
+static struct dm_dirty_log_internal *__find_dirty_log_type(const char *name)
 {
-       spin_lock(&_lock);
+       struct dm_dirty_log_internal *log_type;
 
-       if (type->use_count)
-               DMWARN("Attempt to unregister a log type that is still in use");
-       else
-               list_del(&type->list);
+       list_for_each_entry(log_type, &_log_types, list)
+               if (!strcmp(name, log_type->type->name))
+                       return log_type;
 
-       spin_unlock(&_lock);
-
-       return 0;
+       return NULL;
 }
-EXPORT_SYMBOL(dm_dirty_log_type_unregister);
 
-static struct dm_dirty_log_type *_get_type(const char *type_name)
+static struct dm_dirty_log_internal *_get_dirty_log_type(const char *name)
 {
-       struct dm_dirty_log_type *type;
+       struct dm_dirty_log_internal *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) {
+               if (!log_type->use && !try_module_get(log_type->type->module))
+                       log_type = NULL;
+               else
+                       log_type->use++;
+       }
 
        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'.
@@ -85,11 +76,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_internal *log_type;
 
-       type = _get_type(type_name);
-       if (type)
-               return type;
+       if (!type_name)
+               return NULL;
+
+       log_type = _get_dirty_log_type(type_name);
+       if (log_type)
+               return log_type->type;
 
        type_name_dup = kstrdup(type_name, GFP_KERNEL);
        if (!type_name_dup) {
@@ -99,29 +93,100 @@ 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 ? log_type->type : NULL;
 }
 
 static void put_type(struct dm_dirty_log_type *type)
 {
+       struct dm_dirty_log_internal *log_type;
+
+       if (!type)
+               return;
+
        spin_lock(&_lock);
-       if (!--type->use_count)
+       log_type = __find_dirty_log_type(type->name);
+       if (!log_type)
+               goto out;
+
+       if (!--log_type->use)
                module_put(type->module);
+
+       BUG_ON(log_type->use < 0);
+
+out:
        spin_unlock(&_lock);
 }
 
+static struct dm_dirty_log_internal *_alloc_dirty_log_type(struct dm_dirty_log_type *type)
+{
+       struct dm_dirty_log_internal *log_type = kzalloc(sizeof(*log_type),
+                                                        GFP_KERNEL);
+
+       if (log_type)
+               log_type->type = type;
+
+       return log_type;
+}
+
+int dm_dirty_log_type_register(struct dm_dirty_log_type *type)
+{
+       struct dm_dirty_log_internal *log_type = _alloc_dirty_log_type(type);
+       int r = 0;
+
+       if (!log_type)
+               return -ENOMEM;
+
+       spin_lock(&_lock);
+       if (!__find_dirty_log_type(type->name))
+               list_add(&log_type->list, &_log_types);
+       else {
+               kfree(log_type);
+               r = -EEXIST;
+       }
+       spin_unlock(&_lock);
+
+       return r;
+}
+EXPORT_SYMBOL(dm_dirty_log_type_register);
+
+int dm_dirty_log_type_unregister(struct dm_dirty_log_type *type)
+{
+       struct dm_dirty_log_internal *log_type;
+
+       spin_lock(&_lock);
+
+       log_type = __find_dirty_log_type(type->name);
+       if (!log_type) {
+               spin_unlock(&_lock);
+               return -EINVAL;
+       }
+
+       if (log_type->use) {
+               spin_unlock(&_lock);
+               return -ETXTBSY;
+       }
+
+       list_del(&log_type->list);
+
+       spin_unlock(&_lock);
+       kfree(log_type);
+
+       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,
                                         unsigned int argc, char **argv)
@@ -261,8 +326,6 @@ 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);
 }
@@ -297,10 +360,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 +406,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;
        }
 
@@ -388,8 +457,18 @@ static int create_log_context(struct dm_dirty_log *log, struct dm_target *ti,
                 */
                buf_size = dm_round_up((LOG_OFFSET << SECTOR_SHIFT) +
                                       bitset_size, ti->limits.hardsect_size);
+
+               if (buf_size > dev->bdev->bd_inode->i_size) {
+                       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 +481,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 +498,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 +513,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;
@@ -566,8 +651,10 @@ 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) {
                DMWARN("%s: Failed to write header on dirty region log device",
                       lc->log_dev->name);
@@ -617,7 +704,7 @@ static int disk_flush(struct dm_dirty_log *log)
        if (!lc->touched)
                return 0;
 
-       r = write_header(lc);
+       r = rw_header(lc, WRITE);
        if (r)
                fail_log_device(lc);
        else
@@ -766,7 +853,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 +870,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);