提交 2a23aa1d 编写于 作者: J Jonathan Brassow 提交者: Alasdair G Kergon

dm log: make module use tracking internal

Remove internal module reference fields from the interface.
Signed-off-by: NJonathan Brassow <jbrassow@redhat.com>
Signed-off-by: NAlasdair G Kergon <agk@redhat.com>
上级 b8206bc3
...@@ -16,34 +16,51 @@ ...@@ -16,34 +16,51 @@
#define DM_MSG_PREFIX "dirty region log" #define DM_MSG_PREFIX "dirty region log"
struct dm_dirty_log_internal {
struct dm_dirty_log_type *type;
struct list_head list;
long use;
};
static LIST_HEAD(_log_types); static LIST_HEAD(_log_types);
static DEFINE_SPINLOCK(_lock); static DEFINE_SPINLOCK(_lock);
static struct dm_dirty_log_type *_get_type(const char *type_name) static struct dm_dirty_log_internal *__find_dirty_log_type(const char *name)
{ {
struct dm_dirty_log_type *type; struct dm_dirty_log_internal *log_type;
list_for_each_entry(log_type, &_log_types, list)
if (!strcmp(name, log_type->type->name))
return log_type;
return NULL;
}
static struct dm_dirty_log_internal *_get_dirty_log_type(const char *name)
{
struct dm_dirty_log_internal *log_type;
spin_lock(&_lock); spin_lock(&_lock);
list_for_each_entry (type, &_log_types, list)
if (!strcmp(type_name, type->name)) { log_type = __find_dirty_log_type(name);
if (!type->use_count && !try_module_get(type->module)){ if (log_type) {
spin_unlock(&_lock); if (!log_type->use && !try_module_get(log_type->type->module))
return NULL; log_type = NULL;
} else
type->use_count++; log_type->use++;
spin_unlock(&_lock); }
return type;
}
spin_unlock(&_lock); spin_unlock(&_lock);
return NULL;
return log_type;
} }
/* /*
* get_type * get_type
* @type_name * @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. * available, attempt to load the appropriate module.
* *
* Log modules are named "dm-log-" followed by the 'type_name'. * Log modules are named "dm-log-" followed by the 'type_name'.
...@@ -59,11 +76,14 @@ static struct dm_dirty_log_type *_get_type(const char *type_name) ...@@ -59,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) static struct dm_dirty_log_type *get_type(const char *type_name)
{ {
char *p, *type_name_dup; 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_name)
if (type) return NULL;
return type;
log_type = _get_dirty_log_type(type_name);
if (log_type)
return log_type->type;
type_name_dup = kstrdup(type_name, GFP_KERNEL); type_name_dup = kstrdup(type_name, GFP_KERNEL);
if (!type_name_dup) { if (!type_name_dup) {
...@@ -73,50 +93,95 @@ static struct dm_dirty_log_type *get_type(const char *type_name) ...@@ -73,50 +93,95 @@ static struct dm_dirty_log_type *get_type(const char *type_name)
} }
while (request_module("dm-log-%s", type_name_dup) || 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, '-'); p = strrchr(type_name_dup, '-');
if (!p) if (!p)
break; break;
p[0] = '\0'; p[0] = '\0';
} }
if (!type) if (!log_type)
DMWARN("Module for logging type \"%s\" not found.", type_name); DMWARN("Module for logging type \"%s\" not found.", type_name);
kfree(type_name_dup); kfree(type_name_dup);
return type; return log_type ? log_type->type : NULL;
} }
static void put_type(struct dm_dirty_log_type *type) static void put_type(struct dm_dirty_log_type *type)
{ {
struct dm_dirty_log_internal *log_type;
if (!type)
return;
spin_lock(&_lock); 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); module_put(type->module);
BUG_ON(log_type->use < 0);
out:
spin_unlock(&_lock); 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) 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); spin_lock(&_lock);
type->use_count = 0; if (!__find_dirty_log_type(type->name))
list_add(&type->list, &_log_types); list_add(&log_type->list, &_log_types);
else {
kfree(log_type);
r = -EEXIST;
}
spin_unlock(&_lock); spin_unlock(&_lock);
return 0; return r;
} }
EXPORT_SYMBOL(dm_dirty_log_type_register); EXPORT_SYMBOL(dm_dirty_log_type_register);
int dm_dirty_log_type_unregister(struct dm_dirty_log_type *type) int dm_dirty_log_type_unregister(struct dm_dirty_log_type *type)
{ {
struct dm_dirty_log_internal *log_type;
spin_lock(&_lock); spin_lock(&_lock);
if (type->use_count) log_type = __find_dirty_log_type(type->name);
DMWARN("Attempt to unregister a log type that is still in use"); if (!log_type) {
else spin_unlock(&_lock);
list_del(&type->list); return -EINVAL;
}
if (log_type->use) {
spin_unlock(&_lock);
return -ETXTBSY;
}
list_del(&log_type->list);
spin_unlock(&_lock); spin_unlock(&_lock);
kfree(log_type);
return 0; return 0;
} }
......
...@@ -25,10 +25,8 @@ struct dm_dirty_log { ...@@ -25,10 +25,8 @@ struct dm_dirty_log {
}; };
struct dm_dirty_log_type { struct dm_dirty_log_type {
struct list_head list;
const char *name; const char *name;
struct module *module; struct module *module;
unsigned use_count;
int (*ctr)(struct dm_dirty_log *log, struct dm_target *ti, int (*ctr)(struct dm_dirty_log *log, struct dm_target *ti,
unsigned argc, char **argv); unsigned argc, char **argv);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册