2 * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
3 * Copyright (C) 2006-2008 Red Hat GmbH
5 * This file is released under the GPL.
8 #include "dm-exception-store.h"
11 #include <linux/pagemap.h>
12 #include <linux/vmalloc.h>
13 #include <linux/slab.h>
15 #define DM_MSG_PREFIX "snapshot exception stores"
17 static LIST_HEAD(_exception_store_types);
18 static DEFINE_SPINLOCK(_lock);
20 static struct dm_exception_store_type *__find_exception_store_type(const char *name)
22 struct dm_exception_store_type *type;
24 list_for_each_entry(type, &_exception_store_types, list)
25 if (!strcmp(name, type->name))
31 static struct dm_exception_store_type *_get_exception_store_type(const char *name)
33 struct dm_exception_store_type *type;
37 type = __find_exception_store_type(name);
39 if (type && !try_module_get(type->module))
51 * Attempt to retrieve the dm_exception_store_type by name. If not already
52 * available, attempt to load the appropriate module.
54 * Exstore modules are named "dm-exstore-" followed by the 'type_name'.
55 * Modules may contain multiple types.
56 * This function will first try the module "dm-exstore-<type_name>",
57 * then truncate 'type_name' on the last '-' and try again.
59 * For example, if type_name was "clustered-shared", it would search
60 * 'dm-exstore-clustered-shared' then 'dm-exstore-clustered'.
62 * 'dm-exception-store-<type_name>' is too long of a name in my
63 * opinion, which is why I've chosen to have the files
64 * containing exception store implementations be 'dm-exstore-<type_name>'.
65 * If you want your module to be autoloaded, you will follow this
68 * Returns: dm_exception_store_type* on success, NULL on failure
70 static struct dm_exception_store_type *get_type(const char *type_name)
72 char *p, *type_name_dup;
73 struct dm_exception_store_type *type;
75 type = _get_exception_store_type(type_name);
79 type_name_dup = kstrdup(type_name, GFP_KERNEL);
81 DMERR("No memory left to attempt load for \"%s\"", type_name);
85 while (request_module("dm-exstore-%s", type_name_dup) ||
86 !(type = _get_exception_store_type(type_name))) {
87 p = strrchr(type_name_dup, '-');
94 DMWARN("Module for exstore type \"%s\" not found.", type_name);
101 static void put_type(struct dm_exception_store_type *type)
104 module_put(type->module);
108 int dm_exception_store_type_register(struct dm_exception_store_type *type)
113 if (!__find_exception_store_type(type->name))
114 list_add(&type->list, &_exception_store_types);
121 EXPORT_SYMBOL(dm_exception_store_type_register);
123 int dm_exception_store_type_unregister(struct dm_exception_store_type *type)
127 if (!__find_exception_store_type(type->name)) {
132 list_del(&type->list);
138 EXPORT_SYMBOL(dm_exception_store_type_unregister);
140 int dm_exception_store_create(const char *type_name,
141 struct dm_exception_store **store)
144 struct dm_exception_store_type *type;
145 struct dm_exception_store *tmp_store;
147 tmp_store = kmalloc(sizeof(*tmp_store), GFP_KERNEL);
151 type = get_type(type_name);
157 tmp_store->type = type;
159 r = type->ctr(tmp_store, 0, NULL);
169 EXPORT_SYMBOL(dm_exception_store_create);
171 void dm_exception_store_destroy(struct dm_exception_store *store)
173 store->type->dtr(store);
174 put_type(store->type);
177 EXPORT_SYMBOL(dm_exception_store_destroy);
179 int dm_exception_store_init(void)
183 r = dm_transient_snapshot_init();
185 DMERR("Unable to register transient exception store type.");
189 r = dm_persistent_snapshot_init();
191 DMERR("Unable to register persistent exception store type");
192 goto persistent_fail;
198 dm_persistent_snapshot_exit();
203 void dm_exception_store_exit(void)
205 dm_persistent_snapshot_exit();
206 dm_transient_snapshot_exit();