1 /*
2  * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
3  * Copyright (C) 2006-2008 Red Hat GmbH
4  *
5  * This file is released under the GPL.
6  */
7 
8 #include "dm-exception-store.h"
9 
10 #include <linux/ctype.h>
11 #include <linux/mm.h>
12 #include <linux/pagemap.h>
13 #include <linux/vmalloc.h>
14 #include <linux/slab.h>
15 
16 #define DM_MSG_PREFIX "snapshot exception stores"
17 
18 static LIST_HEAD(_exception_store_types);
19 static DEFINE_SPINLOCK(_lock);
20 
21 static struct dm_exception_store_type *__find_exception_store_type(const char *name)
22 {
23 	struct dm_exception_store_type *type;
24 
25 	list_for_each_entry(type, &_exception_store_types, list)
26 		if (!strcmp(name, type->name))
27 			return type;
28 
29 	return NULL;
30 }
31 
32 static struct dm_exception_store_type *_get_exception_store_type(const char *name)
33 {
34 	struct dm_exception_store_type *type;
35 
36 	spin_lock(&_lock);
37 
38 	type = __find_exception_store_type(name);
39 
40 	if (type && !try_module_get(type->module))
41 		type = NULL;
42 
43 	spin_unlock(&_lock);
44 
45 	return type;
46 }
47 
48 /*
49  * get_type
50  * @type_name
51  *
52  * Attempt to retrieve the dm_exception_store_type by name.  If not already
53  * available, attempt to load the appropriate module.
54  *
55  * Exstore modules are named "dm-exstore-" followed by the 'type_name'.
56  * Modules may contain multiple types.
57  * This function will first try the module "dm-exstore-<type_name>",
58  * then truncate 'type_name' on the last '-' and try again.
59  *
60  * For example, if type_name was "clustered-shared", it would search
61  * 'dm-exstore-clustered-shared' then 'dm-exstore-clustered'.
62  *
63  * 'dm-exception-store-<type_name>' is too long of a name in my
64  * opinion, which is why I've chosen to have the files
65  * containing exception store implementations be 'dm-exstore-<type_name>'.
66  * If you want your module to be autoloaded, you will follow this
67  * naming convention.
68  *
69  * Returns: dm_exception_store_type* on success, NULL on failure
70  */
71 static struct dm_exception_store_type *get_type(const char *type_name)
72 {
73 	char *p, *type_name_dup;
74 	struct dm_exception_store_type *type;
75 
76 	type = _get_exception_store_type(type_name);
77 	if (type)
78 		return type;
79 
80 	type_name_dup = kstrdup(type_name, GFP_KERNEL);
81 	if (!type_name_dup) {
82 		DMERR("No memory left to attempt load for \"%s\"", type_name);
83 		return NULL;
84 	}
85 
86 	while (request_module("dm-exstore-%s", type_name_dup) ||
87 	       !(type = _get_exception_store_type(type_name))) {
88 		p = strrchr(type_name_dup, '-');
89 		if (!p)
90 			break;
91 		p[0] = '\0';
92 	}
93 
94 	if (!type)
95 		DMWARN("Module for exstore type \"%s\" not found.", type_name);
96 
97 	kfree(type_name_dup);
98 
99 	return type;
100 }
101 
102 static void put_type(struct dm_exception_store_type *type)
103 {
104 	spin_lock(&_lock);
105 	module_put(type->module);
106 	spin_unlock(&_lock);
107 }
108 
109 int dm_exception_store_type_register(struct dm_exception_store_type *type)
110 {
111 	int r = 0;
112 
113 	spin_lock(&_lock);
114 	if (!__find_exception_store_type(type->name))
115 		list_add(&type->list, &_exception_store_types);
116 	else
117 		r = -EEXIST;
118 	spin_unlock(&_lock);
119 
120 	return r;
121 }
122 EXPORT_SYMBOL(dm_exception_store_type_register);
123 
124 int dm_exception_store_type_unregister(struct dm_exception_store_type *type)
125 {
126 	spin_lock(&_lock);
127 
128 	if (!__find_exception_store_type(type->name)) {
129 		spin_unlock(&_lock);
130 		return -EINVAL;
131 	}
132 
133 	list_del(&type->list);
134 
135 	spin_unlock(&_lock);
136 
137 	return 0;
138 }
139 EXPORT_SYMBOL(dm_exception_store_type_unregister);
140 
141 /*
142  * Round a number up to the nearest 'size' boundary.  size must
143  * be a power of 2.
144  */
145 static ulong round_up(ulong n, ulong size)
146 {
147 	size--;
148 	return (n + size) & ~size;
149 }
150 
151 static int set_chunk_size(struct dm_exception_store *store,
152 			  const char *chunk_size_arg, char **error)
153 {
154 	unsigned long chunk_size_ulong;
155 	char *value;
156 
157 	chunk_size_ulong = simple_strtoul(chunk_size_arg, &value, 10);
158 	if (*chunk_size_arg == '\0' || *value != '\0') {
159 		*error = "Invalid chunk size";
160 		return -EINVAL;
161 	}
162 
163 	if (!chunk_size_ulong) {
164 		store->chunk_size = store->chunk_mask = store->chunk_shift = 0;
165 		return 0;
166 	}
167 
168 	/*
169 	 * Chunk size must be multiple of page size.  Silently
170 	 * round up if it's not.
171 	 */
172 	chunk_size_ulong = round_up(chunk_size_ulong, PAGE_SIZE >> 9);
173 
174 	return dm_exception_store_set_chunk_size(store, chunk_size_ulong,
175 						 error);
176 }
177 
178 int dm_exception_store_set_chunk_size(struct dm_exception_store *store,
179 				      unsigned long chunk_size_ulong,
180 				      char **error)
181 {
182 	/* Check chunk_size is a power of 2 */
183 	if (!is_power_of_2(chunk_size_ulong)) {
184 		*error = "Chunk size is not a power of 2";
185 		return -EINVAL;
186 	}
187 
188 	/* Validate the chunk size against the device block size */
189 	if (chunk_size_ulong % (bdev_logical_block_size(store->cow->bdev) >> 9)) {
190 		*error = "Chunk size is not a multiple of device blocksize";
191 		return -EINVAL;
192 	}
193 
194 	if (chunk_size_ulong > INT_MAX >> SECTOR_SHIFT) {
195 		*error = "Chunk size is too high";
196 		return -EINVAL;
197 	}
198 
199 	store->chunk_size = chunk_size_ulong;
200 	store->chunk_mask = chunk_size_ulong - 1;
201 	store->chunk_shift = ffs(chunk_size_ulong) - 1;
202 
203 	return 0;
204 }
205 
206 int dm_exception_store_create(struct dm_target *ti, int argc, char **argv,
207 			      unsigned *args_used,
208 			      struct dm_exception_store **store)
209 {
210 	int r = 0;
211 	struct dm_exception_store_type *type = NULL;
212 	struct dm_exception_store *tmp_store;
213 	char persistent;
214 
215 	if (argc < 3) {
216 		ti->error = "Insufficient exception store arguments";
217 		return -EINVAL;
218 	}
219 
220 	tmp_store = kmalloc(sizeof(*tmp_store), GFP_KERNEL);
221 	if (!tmp_store) {
222 		ti->error = "Exception store allocation failed";
223 		return -ENOMEM;
224 	}
225 
226 	persistent = toupper(*argv[1]);
227 	if (persistent == 'P')
228 		type = get_type("P");
229 	else if (persistent == 'N')
230 		type = get_type("N");
231 	else {
232 		ti->error = "Persistent flag is not P or N";
233 		return -EINVAL;
234 	}
235 
236 	if (!type) {
237 		ti->error = "Exception store type not recognised";
238 		r = -EINVAL;
239 		goto bad_type;
240 	}
241 
242 	tmp_store->type = type;
243 	tmp_store->ti = ti;
244 
245 	r = dm_get_device(ti, argv[0], 0, 0,
246 			  FMODE_READ | FMODE_WRITE, &tmp_store->cow);
247 	if (r) {
248 		ti->error = "Cannot get COW device";
249 		goto bad_cow;
250 	}
251 
252 	r = set_chunk_size(tmp_store, argv[2], &ti->error);
253 	if (r)
254 		goto bad_cow;
255 
256 	r = type->ctr(tmp_store, 0, NULL);
257 	if (r) {
258 		ti->error = "Exception store type constructor failed";
259 		goto bad_ctr;
260 	}
261 
262 	*args_used = 3;
263 	*store = tmp_store;
264 	return 0;
265 
266 bad_ctr:
267 	dm_put_device(ti, tmp_store->cow);
268 bad_cow:
269 	put_type(type);
270 bad_type:
271 	kfree(tmp_store);
272 	return r;
273 }
274 EXPORT_SYMBOL(dm_exception_store_create);
275 
276 void dm_exception_store_destroy(struct dm_exception_store *store)
277 {
278 	store->type->dtr(store);
279 	dm_put_device(store->ti, store->cow);
280 	put_type(store->type);
281 	kfree(store);
282 }
283 EXPORT_SYMBOL(dm_exception_store_destroy);
284 
285 int dm_exception_store_init(void)
286 {
287 	int r;
288 
289 	r = dm_transient_snapshot_init();
290 	if (r) {
291 		DMERR("Unable to register transient exception store type.");
292 		goto transient_fail;
293 	}
294 
295 	r = dm_persistent_snapshot_init();
296 	if (r) {
297 		DMERR("Unable to register persistent exception store type");
298 		goto persistent_fail;
299 	}
300 
301 	return 0;
302 
303 persistent_fail:
304 	dm_persistent_snapshot_exit();
305 transient_fail:
306 	return r;
307 }
308 
309 void dm_exception_store_exit(void)
310 {
311 	dm_persistent_snapshot_exit();
312 	dm_transient_snapshot_exit();
313 }
314