Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2012 Red Hat. All rights reserved.
4 *
5 * This file is released under the GPL.
6 */
7
8#include "dm-cache-policy-internal.h"
9#include "dm.h"
10
11#include <linux/module.h>
12#include <linux/slab.h>
13
14/*----------------------------------------------------------------*/
15
16#define DM_MSG_PREFIX "cache-policy"
17
18static DEFINE_SPINLOCK(register_lock);
19static LIST_HEAD(register_list);
20
21static struct dm_cache_policy_type *__find_policy(const char *name)
22{
23 struct dm_cache_policy_type *t;
24
25 list_for_each_entry(t, ®ister_list, list)
26 if (!strcmp(t->name, name))
27 return t;
28
29 return NULL;
30}
31
32static struct dm_cache_policy_type *__get_policy_once(const char *name)
33{
34 struct dm_cache_policy_type *t = __find_policy(name);
35
36 if (t && !try_module_get(t->owner)) {
37 DMWARN("couldn't get module %s", name);
38 t = ERR_PTR(-EINVAL);
39 }
40
41 return t;
42}
43
44static struct dm_cache_policy_type *get_policy_once(const char *name)
45{
46 struct dm_cache_policy_type *t;
47
48 spin_lock(®ister_lock);
49 t = __get_policy_once(name);
50 spin_unlock(®ister_lock);
51
52 return t;
53}
54
55static struct dm_cache_policy_type *get_policy(const char *name)
56{
57 struct dm_cache_policy_type *t;
58
59 t = get_policy_once(name);
60 if (IS_ERR(t))
61 return NULL;
62
63 if (t)
64 return t;
65
66 request_module("dm-cache-%s", name);
67
68 t = get_policy_once(name);
69 if (IS_ERR(t))
70 return NULL;
71
72 return t;
73}
74
75static void put_policy(struct dm_cache_policy_type *t)
76{
77 module_put(t->owner);
78}
79
80int dm_cache_policy_register(struct dm_cache_policy_type *type)
81{
82 int r;
83
84 /* One size fits all for now */
85 if (type->hint_size != 0 && type->hint_size != 4) {
86 DMWARN("hint size must be 0 or 4 but %llu supplied.", (unsigned long long) type->hint_size);
87 return -EINVAL;
88 }
89
90 spin_lock(®ister_lock);
91 if (__find_policy(type->name)) {
92 DMWARN("attempt to register policy under duplicate name %s", type->name);
93 r = -EINVAL;
94 } else {
95 list_add(&type->list, ®ister_list);
96 r = 0;
97 }
98 spin_unlock(®ister_lock);
99
100 return r;
101}
102EXPORT_SYMBOL_GPL(dm_cache_policy_register);
103
104void dm_cache_policy_unregister(struct dm_cache_policy_type *type)
105{
106 spin_lock(®ister_lock);
107 list_del_init(&type->list);
108 spin_unlock(®ister_lock);
109}
110EXPORT_SYMBOL_GPL(dm_cache_policy_unregister);
111
112struct dm_cache_policy *dm_cache_policy_create(const char *name,
113 dm_cblock_t cache_size,
114 sector_t origin_size,
115 sector_t cache_block_size)
116{
117 struct dm_cache_policy *p = NULL;
118 struct dm_cache_policy_type *type;
119
120 type = get_policy(name);
121 if (!type) {
122 DMWARN("unknown policy type");
123 return ERR_PTR(-EINVAL);
124 }
125
126 p = type->create(cache_size, origin_size, cache_block_size);
127 if (!p) {
128 put_policy(type);
129 return ERR_PTR(-ENOMEM);
130 }
131 p->private = type;
132
133 return p;
134}
135EXPORT_SYMBOL_GPL(dm_cache_policy_create);
136
137void dm_cache_policy_destroy(struct dm_cache_policy *p)
138{
139 struct dm_cache_policy_type *t = p->private;
140
141 p->destroy(p);
142 put_policy(t);
143}
144EXPORT_SYMBOL_GPL(dm_cache_policy_destroy);
145
146const char *dm_cache_policy_get_name(struct dm_cache_policy *p)
147{
148 struct dm_cache_policy_type *t = p->private;
149
150 /* if t->real is set then an alias was used (e.g. "default") */
151 if (t->real)
152 return t->real->name;
153
154 return t->name;
155}
156EXPORT_SYMBOL_GPL(dm_cache_policy_get_name);
157
158const unsigned int *dm_cache_policy_get_version(struct dm_cache_policy *p)
159{
160 struct dm_cache_policy_type *t = p->private;
161
162 return t->version;
163}
164EXPORT_SYMBOL_GPL(dm_cache_policy_get_version);
165
166size_t dm_cache_policy_get_hint_size(struct dm_cache_policy *p)
167{
168 struct dm_cache_policy_type *t = p->private;
169
170 return t->hint_size;
171}
172EXPORT_SYMBOL_GPL(dm_cache_policy_get_hint_size);
173
174/*----------------------------------------------------------------*/
1/*
2 * Copyright (C) 2012 Red Hat. All rights reserved.
3 *
4 * This file is released under the GPL.
5 */
6
7#include "dm-cache-policy-internal.h"
8#include "dm.h"
9
10#include <linux/module.h>
11#include <linux/slab.h>
12
13/*----------------------------------------------------------------*/
14
15#define DM_MSG_PREFIX "cache-policy"
16
17static DEFINE_SPINLOCK(register_lock);
18static LIST_HEAD(register_list);
19
20static struct dm_cache_policy_type *__find_policy(const char *name)
21{
22 struct dm_cache_policy_type *t;
23
24 list_for_each_entry(t, ®ister_list, list)
25 if (!strcmp(t->name, name))
26 return t;
27
28 return NULL;
29}
30
31static struct dm_cache_policy_type *__get_policy_once(const char *name)
32{
33 struct dm_cache_policy_type *t = __find_policy(name);
34
35 if (t && !try_module_get(t->owner)) {
36 DMWARN("couldn't get module %s", name);
37 t = ERR_PTR(-EINVAL);
38 }
39
40 return t;
41}
42
43static struct dm_cache_policy_type *get_policy_once(const char *name)
44{
45 struct dm_cache_policy_type *t;
46
47 spin_lock(®ister_lock);
48 t = __get_policy_once(name);
49 spin_unlock(®ister_lock);
50
51 return t;
52}
53
54static struct dm_cache_policy_type *get_policy(const char *name)
55{
56 struct dm_cache_policy_type *t;
57
58 t = get_policy_once(name);
59 if (IS_ERR(t))
60 return NULL;
61
62 if (t)
63 return t;
64
65 request_module("dm-cache-%s", name);
66
67 t = get_policy_once(name);
68 if (IS_ERR(t))
69 return NULL;
70
71 return t;
72}
73
74static void put_policy(struct dm_cache_policy_type *t)
75{
76 module_put(t->owner);
77}
78
79int dm_cache_policy_register(struct dm_cache_policy_type *type)
80{
81 int r;
82
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;
87 }
88
89 spin_lock(®ister_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, ®ister_list);
95 r = 0;
96 }
97 spin_unlock(®ister_lock);
98
99 return r;
100}
101EXPORT_SYMBOL_GPL(dm_cache_policy_register);
102
103void dm_cache_policy_unregister(struct dm_cache_policy_type *type)
104{
105 spin_lock(®ister_lock);
106 list_del_init(&type->list);
107 spin_unlock(®ister_lock);
108}
109EXPORT_SYMBOL_GPL(dm_cache_policy_unregister);
110
111struct 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)
115{
116 struct dm_cache_policy *p = NULL;
117 struct dm_cache_policy_type *type;
118
119 type = get_policy(name);
120 if (!type) {
121 DMWARN("unknown policy type");
122 return ERR_PTR(-EINVAL);
123 }
124
125 p = type->create(cache_size, origin_size, cache_block_size);
126 if (!p) {
127 put_policy(type);
128 return ERR_PTR(-ENOMEM);
129 }
130 p->private = type;
131
132 return p;
133}
134EXPORT_SYMBOL_GPL(dm_cache_policy_create);
135
136void dm_cache_policy_destroy(struct dm_cache_policy *p)
137{
138 struct dm_cache_policy_type *t = p->private;
139
140 p->destroy(p);
141 put_policy(t);
142}
143EXPORT_SYMBOL_GPL(dm_cache_policy_destroy);
144
145const char *dm_cache_policy_get_name(struct dm_cache_policy *p)
146{
147 struct dm_cache_policy_type *t = p->private;
148
149 /* if t->real is set then an alias was used (e.g. "default") */
150 if (t->real)
151 return t->real->name;
152
153 return t->name;
154}
155EXPORT_SYMBOL_GPL(dm_cache_policy_get_name);
156
157const unsigned *dm_cache_policy_get_version(struct dm_cache_policy *p)
158{
159 struct dm_cache_policy_type *t = p->private;
160
161 return t->version;
162}
163EXPORT_SYMBOL_GPL(dm_cache_policy_get_version);
164
165size_t dm_cache_policy_get_hint_size(struct dm_cache_policy *p)
166{
167 struct dm_cache_policy_type *t = p->private;
168
169 return t->hint_size;
170}
171EXPORT_SYMBOL_GPL(dm_cache_policy_get_hint_size);
172
173/*----------------------------------------------------------------*/