Linux 3.12.39
[linux/fpc-iii.git] / drivers / md / dm-cache-policy.c
blob21c03c570c06b7d2bac4bf1abc1d41147d7cd168
1 /*
2 * Copyright (C) 2012 Red Hat. All rights reserved.
4 * This file is released under the GPL.
5 */
7 #include "dm-cache-policy-internal.h"
8 #include "dm.h"
10 #include <linux/module.h>
11 #include <linux/slab.h>
13 /*----------------------------------------------------------------*/
15 #define DM_MSG_PREFIX "cache-policy"
17 static DEFINE_SPINLOCK(register_lock);
18 static LIST_HEAD(register_list);
20 static struct dm_cache_policy_type *__find_policy(const char *name)
22 struct dm_cache_policy_type *t;
24 list_for_each_entry(t, &register_list, list)
25 if (!strcmp(t->name, name))
26 return t;
28 return NULL;
31 static struct dm_cache_policy_type *__get_policy_once(const char *name)
33 struct dm_cache_policy_type *t = __find_policy(name);
35 if (t && !try_module_get(t->owner)) {
36 DMWARN("couldn't get module %s", name);
37 t = ERR_PTR(-EINVAL);
40 return t;
43 static struct dm_cache_policy_type *get_policy_once(const char *name)
45 struct dm_cache_policy_type *t;
47 spin_lock(&register_lock);
48 t = __get_policy_once(name);
49 spin_unlock(&register_lock);
51 return t;
54 static struct dm_cache_policy_type *get_policy(const char *name)
56 struct dm_cache_policy_type *t;
58 t = get_policy_once(name);
59 if (IS_ERR(t))
60 return NULL;
62 if (t)
63 return t;
65 request_module("dm-cache-%s", name);
67 t = get_policy_once(name);
68 if (IS_ERR(t))
69 return NULL;
71 return t;
74 static void put_policy(struct dm_cache_policy_type *t)
76 module_put(t->owner);
79 int dm_cache_policy_register(struct dm_cache_policy_type *type)
81 int r;
83 /* One size fits all for now */
84 if (type->hint_size != 0 && type->hint_size != 4) {
85 DMWARN("hint size must be 0 or 4 but %llu supplied.", (unsigned long long) type->hint_size);
86 return -EINVAL;
89 spin_lock(&register_lock);
90 if (__find_policy(type->name)) {
91 DMWARN("attempt to register policy under duplicate name %s", type->name);
92 r = -EINVAL;
93 } else {
94 list_add(&type->list, &register_list);
95 r = 0;
97 spin_unlock(&register_lock);
99 return r;
101 EXPORT_SYMBOL_GPL(dm_cache_policy_register);
103 void dm_cache_policy_unregister(struct dm_cache_policy_type *type)
105 spin_lock(&register_lock);
106 list_del_init(&type->list);
107 spin_unlock(&register_lock);
109 EXPORT_SYMBOL_GPL(dm_cache_policy_unregister);
111 struct dm_cache_policy *dm_cache_policy_create(const char *name,
112 dm_cblock_t cache_size,
113 sector_t origin_size,
114 sector_t cache_block_size)
116 struct dm_cache_policy *p = NULL;
117 struct dm_cache_policy_type *type;
119 type = get_policy(name);
120 if (!type) {
121 DMWARN("unknown policy type");
122 return NULL;
125 p = type->create(cache_size, origin_size, cache_block_size);
126 if (!p) {
127 put_policy(type);
128 return NULL;
130 p->private = type;
132 return p;
134 EXPORT_SYMBOL_GPL(dm_cache_policy_create);
136 void dm_cache_policy_destroy(struct dm_cache_policy *p)
138 struct dm_cache_policy_type *t = p->private;
140 p->destroy(p);
141 put_policy(t);
143 EXPORT_SYMBOL_GPL(dm_cache_policy_destroy);
145 const char *dm_cache_policy_get_name(struct dm_cache_policy *p)
147 struct dm_cache_policy_type *t = p->private;
149 return t->name;
151 EXPORT_SYMBOL_GPL(dm_cache_policy_get_name);
153 const unsigned *dm_cache_policy_get_version(struct dm_cache_policy *p)
155 struct dm_cache_policy_type *t = p->private;
157 return t->version;
159 EXPORT_SYMBOL_GPL(dm_cache_policy_get_version);
161 size_t dm_cache_policy_get_hint_size(struct dm_cache_policy *p)
163 struct dm_cache_policy_type *t = p->private;
165 return t->hint_size;
167 EXPORT_SYMBOL_GPL(dm_cache_policy_get_hint_size);
169 /*----------------------------------------------------------------*/