]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Copyright (C) 2001, 2002 Sistina Software (UK) Limited. | |
2b06cfff | 3 | * Copyright (C) 2004 - 2006 Red Hat, Inc. All rights reserved. |
1da177e4 LT |
4 | * |
5 | * This file is released under the GPL. | |
6 | */ | |
7 | ||
8 | #include "dm.h" | |
9 | ||
10 | #include <linux/module.h> | |
11 | #include <linux/vmalloc.h> | |
12 | #include <linux/miscdevice.h> | |
13 | #include <linux/init.h> | |
14 | #include <linux/wait.h> | |
15 | #include <linux/slab.h> | |
1da177e4 | 16 | #include <linux/dm-ioctl.h> |
3ac51e74 | 17 | #include <linux/hdreg.h> |
76c072b4 | 18 | #include <linux/compat.h> |
1da177e4 LT |
19 | |
20 | #include <asm/uaccess.h> | |
21 | ||
72d94861 | 22 | #define DM_MSG_PREFIX "ioctl" |
1da177e4 LT |
23 | #define DM_DRIVER_EMAIL "dm-devel@redhat.com" |
24 | ||
25 | /*----------------------------------------------------------------- | |
26 | * The ioctl interface needs to be able to look up devices by | |
27 | * name or uuid. | |
28 | *---------------------------------------------------------------*/ | |
29 | struct hash_cell { | |
30 | struct list_head name_list; | |
31 | struct list_head uuid_list; | |
32 | ||
33 | char *name; | |
34 | char *uuid; | |
35 | struct mapped_device *md; | |
36 | struct dm_table *new_map; | |
37 | }; | |
38 | ||
39 | struct vers_iter { | |
40 | size_t param_size; | |
41 | struct dm_target_versions *vers, *old_vers; | |
42 | char *end; | |
43 | uint32_t flags; | |
44 | }; | |
45 | ||
46 | ||
47 | #define NUM_BUCKETS 64 | |
48 | #define MASK_BUCKETS (NUM_BUCKETS - 1) | |
49 | static struct list_head _name_buckets[NUM_BUCKETS]; | |
50 | static struct list_head _uuid_buckets[NUM_BUCKETS]; | |
51 | ||
5c6bd75d | 52 | static void dm_hash_remove_all(int keep_open_devices); |
1da177e4 LT |
53 | |
54 | /* | |
55 | * Guards access to both hash tables. | |
56 | */ | |
57 | static DECLARE_RWSEM(_hash_lock); | |
58 | ||
59 | static void init_buckets(struct list_head *buckets) | |
60 | { | |
61 | unsigned int i; | |
62 | ||
63 | for (i = 0; i < NUM_BUCKETS; i++) | |
64 | INIT_LIST_HEAD(buckets + i); | |
65 | } | |
66 | ||
67 | static int dm_hash_init(void) | |
68 | { | |
69 | init_buckets(_name_buckets); | |
70 | init_buckets(_uuid_buckets); | |
1da177e4 LT |
71 | return 0; |
72 | } | |
73 | ||
74 | static void dm_hash_exit(void) | |
75 | { | |
5c6bd75d | 76 | dm_hash_remove_all(0); |
1da177e4 LT |
77 | } |
78 | ||
79 | /*----------------------------------------------------------------- | |
80 | * Hash function: | |
81 | * We're not really concerned with the str hash function being | |
82 | * fast since it's only used by the ioctl interface. | |
83 | *---------------------------------------------------------------*/ | |
84 | static unsigned int hash_str(const char *str) | |
85 | { | |
86 | const unsigned int hash_mult = 2654435387U; | |
87 | unsigned int h = 0; | |
88 | ||
89 | while (*str) | |
90 | h = (h + (unsigned int) *str++) * hash_mult; | |
91 | ||
92 | return h & MASK_BUCKETS; | |
93 | } | |
94 | ||
95 | /*----------------------------------------------------------------- | |
96 | * Code for looking up a device by name | |
97 | *---------------------------------------------------------------*/ | |
98 | static struct hash_cell *__get_name_cell(const char *str) | |
99 | { | |
100 | struct hash_cell *hc; | |
101 | unsigned int h = hash_str(str); | |
102 | ||
103 | list_for_each_entry (hc, _name_buckets + h, name_list) | |
7ec75f25 JM |
104 | if (!strcmp(hc->name, str)) { |
105 | dm_get(hc->md); | |
1da177e4 | 106 | return hc; |
7ec75f25 | 107 | } |
1da177e4 LT |
108 | |
109 | return NULL; | |
110 | } | |
111 | ||
112 | static struct hash_cell *__get_uuid_cell(const char *str) | |
113 | { | |
114 | struct hash_cell *hc; | |
115 | unsigned int h = hash_str(str); | |
116 | ||
117 | list_for_each_entry (hc, _uuid_buckets + h, uuid_list) | |
7ec75f25 JM |
118 | if (!strcmp(hc->uuid, str)) { |
119 | dm_get(hc->md); | |
1da177e4 | 120 | return hc; |
7ec75f25 | 121 | } |
1da177e4 LT |
122 | |
123 | return NULL; | |
124 | } | |
125 | ||
126 | /*----------------------------------------------------------------- | |
127 | * Inserting, removing and renaming a device. | |
128 | *---------------------------------------------------------------*/ | |
1da177e4 LT |
129 | static struct hash_cell *alloc_cell(const char *name, const char *uuid, |
130 | struct mapped_device *md) | |
131 | { | |
132 | struct hash_cell *hc; | |
133 | ||
134 | hc = kmalloc(sizeof(*hc), GFP_KERNEL); | |
135 | if (!hc) | |
136 | return NULL; | |
137 | ||
543537bd | 138 | hc->name = kstrdup(name, GFP_KERNEL); |
1da177e4 LT |
139 | if (!hc->name) { |
140 | kfree(hc); | |
141 | return NULL; | |
142 | } | |
143 | ||
144 | if (!uuid) | |
145 | hc->uuid = NULL; | |
146 | ||
147 | else { | |
543537bd | 148 | hc->uuid = kstrdup(uuid, GFP_KERNEL); |
1da177e4 LT |
149 | if (!hc->uuid) { |
150 | kfree(hc->name); | |
151 | kfree(hc); | |
152 | return NULL; | |
153 | } | |
154 | } | |
155 | ||
156 | INIT_LIST_HEAD(&hc->name_list); | |
157 | INIT_LIST_HEAD(&hc->uuid_list); | |
158 | hc->md = md; | |
159 | hc->new_map = NULL; | |
160 | return hc; | |
161 | } | |
162 | ||
163 | static void free_cell(struct hash_cell *hc) | |
164 | { | |
165 | if (hc) { | |
166 | kfree(hc->name); | |
167 | kfree(hc->uuid); | |
168 | kfree(hc); | |
169 | } | |
170 | } | |
171 | ||
1da177e4 LT |
172 | /* |
173 | * The kdev_t and uuid of a device can never change once it is | |
174 | * initially inserted. | |
175 | */ | |
176 | static int dm_hash_insert(const char *name, const char *uuid, struct mapped_device *md) | |
177 | { | |
7ec75f25 | 178 | struct hash_cell *cell, *hc; |
1da177e4 LT |
179 | |
180 | /* | |
181 | * Allocate the new cells. | |
182 | */ | |
183 | cell = alloc_cell(name, uuid, md); | |
184 | if (!cell) | |
185 | return -ENOMEM; | |
186 | ||
187 | /* | |
188 | * Insert the cell into both hash tables. | |
189 | */ | |
190 | down_write(&_hash_lock); | |
7ec75f25 JM |
191 | hc = __get_name_cell(name); |
192 | if (hc) { | |
193 | dm_put(hc->md); | |
1da177e4 | 194 | goto bad; |
7ec75f25 | 195 | } |
1da177e4 LT |
196 | |
197 | list_add(&cell->name_list, _name_buckets + hash_str(name)); | |
198 | ||
199 | if (uuid) { | |
7ec75f25 JM |
200 | hc = __get_uuid_cell(uuid); |
201 | if (hc) { | |
1da177e4 | 202 | list_del(&cell->name_list); |
7ec75f25 | 203 | dm_put(hc->md); |
1da177e4 LT |
204 | goto bad; |
205 | } | |
206 | list_add(&cell->uuid_list, _uuid_buckets + hash_str(uuid)); | |
207 | } | |
1da177e4 LT |
208 | dm_get(md); |
209 | dm_set_mdptr(md, cell); | |
210 | up_write(&_hash_lock); | |
211 | ||
212 | return 0; | |
213 | ||
214 | bad: | |
215 | up_write(&_hash_lock); | |
216 | free_cell(cell); | |
217 | return -EBUSY; | |
218 | } | |
219 | ||
220 | static void __hash_remove(struct hash_cell *hc) | |
221 | { | |
269fd2a6 | 222 | struct dm_table *table; |
223 | ||
1da177e4 LT |
224 | /* remove from the dev hash */ |
225 | list_del(&hc->uuid_list); | |
226 | list_del(&hc->name_list); | |
1da177e4 | 227 | dm_set_mdptr(hc->md, NULL); |
269fd2a6 | 228 | |
229 | table = dm_get_table(hc->md); | |
230 | if (table) { | |
231 | dm_table_event(table); | |
232 | dm_table_put(table); | |
233 | } | |
234 | ||
1da177e4 | 235 | if (hc->new_map) |
d5816876 | 236 | dm_table_destroy(hc->new_map); |
1134e5ae | 237 | dm_put(hc->md); |
1da177e4 LT |
238 | free_cell(hc); |
239 | } | |
240 | ||
5c6bd75d | 241 | static void dm_hash_remove_all(int keep_open_devices) |
1da177e4 | 242 | { |
5c6bd75d | 243 | int i, dev_skipped, dev_removed; |
1da177e4 LT |
244 | struct hash_cell *hc; |
245 | struct list_head *tmp, *n; | |
246 | ||
247 | down_write(&_hash_lock); | |
5c6bd75d AK |
248 | |
249 | retry: | |
250 | dev_skipped = dev_removed = 0; | |
1da177e4 LT |
251 | for (i = 0; i < NUM_BUCKETS; i++) { |
252 | list_for_each_safe (tmp, n, _name_buckets + i) { | |
253 | hc = list_entry(tmp, struct hash_cell, name_list); | |
5c6bd75d AK |
254 | |
255 | if (keep_open_devices && | |
256 | dm_lock_for_deletion(hc->md)) { | |
257 | dev_skipped++; | |
258 | continue; | |
259 | } | |
1da177e4 | 260 | __hash_remove(hc); |
5c6bd75d | 261 | dev_removed = 1; |
1da177e4 LT |
262 | } |
263 | } | |
5c6bd75d AK |
264 | |
265 | /* | |
266 | * Some mapped devices may be using other mapped devices, so if any | |
267 | * still exist, repeat until we make no further progress. | |
268 | */ | |
269 | if (dev_skipped) { | |
270 | if (dev_removed) | |
271 | goto retry; | |
272 | ||
273 | DMWARN("remove_all left %d open device(s)", dev_skipped); | |
274 | } | |
275 | ||
1da177e4 LT |
276 | up_write(&_hash_lock); |
277 | } | |
278 | ||
60935eb2 | 279 | static int dm_hash_rename(uint32_t cookie, const char *old, const char *new) |
1da177e4 LT |
280 | { |
281 | char *new_name, *old_name; | |
282 | struct hash_cell *hc; | |
81f1777a | 283 | struct dm_table *table; |
1da177e4 LT |
284 | |
285 | /* | |
286 | * duplicate new. | |
287 | */ | |
543537bd | 288 | new_name = kstrdup(new, GFP_KERNEL); |
1da177e4 LT |
289 | if (!new_name) |
290 | return -ENOMEM; | |
291 | ||
292 | down_write(&_hash_lock); | |
293 | ||
294 | /* | |
295 | * Is new free ? | |
296 | */ | |
297 | hc = __get_name_cell(new); | |
298 | if (hc) { | |
299 | DMWARN("asked to rename to an already existing name %s -> %s", | |
300 | old, new); | |
7ec75f25 | 301 | dm_put(hc->md); |
1da177e4 LT |
302 | up_write(&_hash_lock); |
303 | kfree(new_name); | |
304 | return -EBUSY; | |
305 | } | |
306 | ||
307 | /* | |
308 | * Is there such a device as 'old' ? | |
309 | */ | |
310 | hc = __get_name_cell(old); | |
311 | if (!hc) { | |
312 | DMWARN("asked to rename a non existent device %s -> %s", | |
313 | old, new); | |
314 | up_write(&_hash_lock); | |
315 | kfree(new_name); | |
316 | return -ENXIO; | |
317 | } | |
318 | ||
319 | /* | |
320 | * rename and move the name cell. | |
321 | */ | |
1da177e4 LT |
322 | list_del(&hc->name_list); |
323 | old_name = hc->name; | |
324 | hc->name = new_name; | |
325 | list_add(&hc->name_list, _name_buckets + hash_str(new_name)); | |
326 | ||
81f1777a | 327 | /* |
328 | * Wake up any dm event waiters. | |
329 | */ | |
330 | table = dm_get_table(hc->md); | |
331 | if (table) { | |
332 | dm_table_event(table); | |
333 | dm_table_put(table); | |
334 | } | |
335 | ||
60935eb2 | 336 | dm_kobject_uevent(hc->md, KOBJ_CHANGE, cookie); |
69267a30 | 337 | |
7ec75f25 | 338 | dm_put(hc->md); |
1da177e4 LT |
339 | up_write(&_hash_lock); |
340 | kfree(old_name); | |
341 | return 0; | |
342 | } | |
343 | ||
344 | /*----------------------------------------------------------------- | |
345 | * Implementation of the ioctl commands | |
346 | *---------------------------------------------------------------*/ | |
347 | /* | |
348 | * All the ioctl commands get dispatched to functions with this | |
349 | * prototype. | |
350 | */ | |
351 | typedef int (*ioctl_fn)(struct dm_ioctl *param, size_t param_size); | |
352 | ||
353 | static int remove_all(struct dm_ioctl *param, size_t param_size) | |
354 | { | |
5c6bd75d | 355 | dm_hash_remove_all(1); |
1da177e4 LT |
356 | param->data_size = 0; |
357 | return 0; | |
358 | } | |
359 | ||
360 | /* | |
361 | * Round up the ptr to an 8-byte boundary. | |
362 | */ | |
363 | #define ALIGN_MASK 7 | |
364 | static inline void *align_ptr(void *ptr) | |
365 | { | |
366 | return (void *) (((size_t) (ptr + ALIGN_MASK)) & ~ALIGN_MASK); | |
367 | } | |
368 | ||
369 | /* | |
370 | * Retrieves the data payload buffer from an already allocated | |
371 | * struct dm_ioctl. | |
372 | */ | |
373 | static void *get_result_buffer(struct dm_ioctl *param, size_t param_size, | |
374 | size_t *len) | |
375 | { | |
376 | param->data_start = align_ptr(param + 1) - (void *) param; | |
377 | ||
378 | if (param->data_start < param_size) | |
379 | *len = param_size - param->data_start; | |
380 | else | |
381 | *len = 0; | |
382 | ||
383 | return ((void *) param) + param->data_start; | |
384 | } | |
385 | ||
386 | static int list_devices(struct dm_ioctl *param, size_t param_size) | |
387 | { | |
388 | unsigned int i; | |
389 | struct hash_cell *hc; | |
390 | size_t len, needed = 0; | |
391 | struct gendisk *disk; | |
392 | struct dm_name_list *nl, *old_nl = NULL; | |
393 | ||
394 | down_write(&_hash_lock); | |
395 | ||
396 | /* | |
397 | * Loop through all the devices working out how much | |
398 | * space we need. | |
399 | */ | |
400 | for (i = 0; i < NUM_BUCKETS; i++) { | |
401 | list_for_each_entry (hc, _name_buckets + i, name_list) { | |
402 | needed += sizeof(struct dm_name_list); | |
403 | needed += strlen(hc->name) + 1; | |
404 | needed += ALIGN_MASK; | |
405 | } | |
406 | } | |
407 | ||
408 | /* | |
409 | * Grab our output buffer. | |
410 | */ | |
411 | nl = get_result_buffer(param, param_size, &len); | |
412 | if (len < needed) { | |
413 | param->flags |= DM_BUFFER_FULL_FLAG; | |
414 | goto out; | |
415 | } | |
416 | param->data_size = param->data_start + needed; | |
417 | ||
418 | nl->dev = 0; /* Flags no data */ | |
419 | ||
420 | /* | |
421 | * Now loop through filling out the names. | |
422 | */ | |
423 | for (i = 0; i < NUM_BUCKETS; i++) { | |
424 | list_for_each_entry (hc, _name_buckets + i, name_list) { | |
425 | if (old_nl) | |
426 | old_nl->next = (uint32_t) ((void *) nl - | |
427 | (void *) old_nl); | |
428 | disk = dm_disk(hc->md); | |
f331c029 | 429 | nl->dev = huge_encode_dev(disk_devt(disk)); |
1da177e4 LT |
430 | nl->next = 0; |
431 | strcpy(nl->name, hc->name); | |
432 | ||
433 | old_nl = nl; | |
434 | nl = align_ptr(((void *) ++nl) + strlen(hc->name) + 1); | |
435 | } | |
436 | } | |
437 | ||
438 | out: | |
439 | up_write(&_hash_lock); | |
440 | return 0; | |
441 | } | |
442 | ||
443 | static void list_version_get_needed(struct target_type *tt, void *needed_param) | |
444 | { | |
445 | size_t *needed = needed_param; | |
446 | ||
c4cc6635 | 447 | *needed += sizeof(struct dm_target_versions); |
1da177e4 | 448 | *needed += strlen(tt->name); |
1da177e4 LT |
449 | *needed += ALIGN_MASK; |
450 | } | |
451 | ||
452 | static void list_version_get_info(struct target_type *tt, void *param) | |
453 | { | |
454 | struct vers_iter *info = param; | |
455 | ||
456 | /* Check space - it might have changed since the first iteration */ | |
457 | if ((char *)info->vers + sizeof(tt->version) + strlen(tt->name) + 1 > | |
458 | info->end) { | |
459 | ||
460 | info->flags = DM_BUFFER_FULL_FLAG; | |
461 | return; | |
462 | } | |
463 | ||
464 | if (info->old_vers) | |
465 | info->old_vers->next = (uint32_t) ((void *)info->vers - | |
466 | (void *)info->old_vers); | |
467 | info->vers->version[0] = tt->version[0]; | |
468 | info->vers->version[1] = tt->version[1]; | |
469 | info->vers->version[2] = tt->version[2]; | |
470 | info->vers->next = 0; | |
471 | strcpy(info->vers->name, tt->name); | |
472 | ||
473 | info->old_vers = info->vers; | |
474 | info->vers = align_ptr(((void *) ++info->vers) + strlen(tt->name) + 1); | |
475 | } | |
476 | ||
477 | static int list_versions(struct dm_ioctl *param, size_t param_size) | |
478 | { | |
479 | size_t len, needed = 0; | |
480 | struct dm_target_versions *vers; | |
481 | struct vers_iter iter_info; | |
482 | ||
483 | /* | |
484 | * Loop through all the devices working out how much | |
485 | * space we need. | |
486 | */ | |
487 | dm_target_iterate(list_version_get_needed, &needed); | |
488 | ||
489 | /* | |
490 | * Grab our output buffer. | |
491 | */ | |
492 | vers = get_result_buffer(param, param_size, &len); | |
493 | if (len < needed) { | |
494 | param->flags |= DM_BUFFER_FULL_FLAG; | |
495 | goto out; | |
496 | } | |
497 | param->data_size = param->data_start + needed; | |
498 | ||
499 | iter_info.param_size = param_size; | |
500 | iter_info.old_vers = NULL; | |
501 | iter_info.vers = vers; | |
502 | iter_info.flags = 0; | |
503 | iter_info.end = (char *)vers+len; | |
504 | ||
505 | /* | |
506 | * Now loop through filling out the names & versions. | |
507 | */ | |
508 | dm_target_iterate(list_version_get_info, &iter_info); | |
509 | param->flags |= iter_info.flags; | |
510 | ||
511 | out: | |
512 | return 0; | |
513 | } | |
514 | ||
515 | ||
516 | ||
517 | static int check_name(const char *name) | |
518 | { | |
519 | if (strchr(name, '/')) { | |
520 | DMWARN("invalid device name"); | |
521 | return -EINVAL; | |
522 | } | |
523 | ||
524 | return 0; | |
525 | } | |
526 | ||
527 | /* | |
528 | * Fills in a dm_ioctl structure, ready for sending back to | |
529 | * userland. | |
530 | */ | |
531 | static int __dev_status(struct mapped_device *md, struct dm_ioctl *param) | |
532 | { | |
533 | struct gendisk *disk = dm_disk(md); | |
534 | struct dm_table *table; | |
1da177e4 LT |
535 | |
536 | param->flags &= ~(DM_SUSPEND_FLAG | DM_READONLY_FLAG | | |
537 | DM_ACTIVE_PRESENT_FLAG); | |
538 | ||
539 | if (dm_suspended(md)) | |
540 | param->flags |= DM_SUSPEND_FLAG; | |
541 | ||
f331c029 | 542 | param->dev = huge_encode_dev(disk_devt(disk)); |
1da177e4 | 543 | |
5c6bd75d AK |
544 | /* |
545 | * Yes, this will be out of date by the time it gets back | |
546 | * to userland, but it is still very useful for | |
547 | * debugging. | |
548 | */ | |
549 | param->open_count = dm_open_count(md); | |
1da177e4 | 550 | |
b7db9956 | 551 | if (get_disk_ro(disk)) |
1da177e4 LT |
552 | param->flags |= DM_READONLY_FLAG; |
553 | ||
554 | param->event_nr = dm_get_event_nr(md); | |
555 | ||
556 | table = dm_get_table(md); | |
557 | if (table) { | |
558 | param->flags |= DM_ACTIVE_PRESENT_FLAG; | |
559 | param->target_count = dm_table_get_num_targets(table); | |
560 | dm_table_put(table); | |
561 | } else | |
562 | param->target_count = 0; | |
563 | ||
564 | return 0; | |
565 | } | |
566 | ||
567 | static int dev_create(struct dm_ioctl *param, size_t param_size) | |
568 | { | |
2b06cfff | 569 | int r, m = DM_ANY_MINOR; |
1da177e4 LT |
570 | struct mapped_device *md; |
571 | ||
572 | r = check_name(param->name); | |
573 | if (r) | |
574 | return r; | |
575 | ||
576 | if (param->flags & DM_PERSISTENT_DEV_FLAG) | |
2b06cfff | 577 | m = MINOR(huge_decode_dev(param->dev)); |
1da177e4 | 578 | |
2b06cfff | 579 | r = dm_create(m, &md); |
1da177e4 LT |
580 | if (r) |
581 | return r; | |
582 | ||
583 | r = dm_hash_insert(param->name, *param->uuid ? param->uuid : NULL, md); | |
584 | if (r) { | |
585 | dm_put(md); | |
586 | return r; | |
587 | } | |
588 | ||
589 | param->flags &= ~DM_INACTIVE_PRESENT_FLAG; | |
590 | ||
591 | r = __dev_status(md, param); | |
592 | dm_put(md); | |
593 | ||
594 | return r; | |
595 | } | |
596 | ||
597 | /* | |
598 | * Always use UUID for lookups if it's present, otherwise use name or dev. | |
599 | */ | |
858119e1 | 600 | static struct hash_cell *__find_device_hash_cell(struct dm_ioctl *param) |
1da177e4 | 601 | { |
9ade92a9 AK |
602 | struct mapped_device *md; |
603 | void *mdptr = NULL; | |
604 | ||
1da177e4 LT |
605 | if (*param->uuid) |
606 | return __get_uuid_cell(param->uuid); | |
9ade92a9 AK |
607 | |
608 | if (*param->name) | |
1da177e4 | 609 | return __get_name_cell(param->name); |
9ade92a9 AK |
610 | |
611 | md = dm_get_md(huge_decode_dev(param->dev)); | |
bfc5ecdf AK |
612 | if (!md) |
613 | goto out; | |
614 | ||
615 | mdptr = dm_get_mdptr(md); | |
616 | if (!mdptr) | |
617 | dm_put(md); | |
9ade92a9 | 618 | |
bfc5ecdf | 619 | out: |
9ade92a9 | 620 | return mdptr; |
1da177e4 LT |
621 | } |
622 | ||
858119e1 | 623 | static struct mapped_device *find_device(struct dm_ioctl *param) |
1da177e4 LT |
624 | { |
625 | struct hash_cell *hc; | |
626 | struct mapped_device *md = NULL; | |
627 | ||
628 | down_read(&_hash_lock); | |
629 | hc = __find_device_hash_cell(param); | |
630 | if (hc) { | |
631 | md = hc->md; | |
1da177e4 LT |
632 | |
633 | /* | |
634 | * Sneakily write in both the name and the uuid | |
635 | * while we have the cell. | |
636 | */ | |
637 | strncpy(param->name, hc->name, sizeof(param->name)); | |
638 | if (hc->uuid) | |
639 | strncpy(param->uuid, hc->uuid, sizeof(param->uuid)-1); | |
640 | else | |
641 | param->uuid[0] = '\0'; | |
642 | ||
643 | if (hc->new_map) | |
644 | param->flags |= DM_INACTIVE_PRESENT_FLAG; | |
645 | else | |
646 | param->flags &= ~DM_INACTIVE_PRESENT_FLAG; | |
647 | } | |
648 | up_read(&_hash_lock); | |
649 | ||
650 | return md; | |
651 | } | |
652 | ||
653 | static int dev_remove(struct dm_ioctl *param, size_t param_size) | |
654 | { | |
655 | struct hash_cell *hc; | |
7ec75f25 | 656 | struct mapped_device *md; |
5c6bd75d | 657 | int r; |
1da177e4 LT |
658 | |
659 | down_write(&_hash_lock); | |
660 | hc = __find_device_hash_cell(param); | |
661 | ||
662 | if (!hc) { | |
663 | DMWARN("device doesn't appear to be in the dev hash table."); | |
664 | up_write(&_hash_lock); | |
665 | return -ENXIO; | |
666 | } | |
667 | ||
7ec75f25 JM |
668 | md = hc->md; |
669 | ||
5c6bd75d AK |
670 | /* |
671 | * Ensure the device is not open and nothing further can open it. | |
672 | */ | |
673 | r = dm_lock_for_deletion(md); | |
674 | if (r) { | |
675 | DMWARN("unable to remove open device %s", hc->name); | |
676 | up_write(&_hash_lock); | |
677 | dm_put(md); | |
678 | return r; | |
679 | } | |
680 | ||
1da177e4 LT |
681 | __hash_remove(hc); |
682 | up_write(&_hash_lock); | |
60935eb2 MB |
683 | |
684 | dm_kobject_uevent(md, KOBJ_REMOVE, param->event_nr); | |
685 | ||
7ec75f25 | 686 | dm_put(md); |
1da177e4 LT |
687 | param->data_size = 0; |
688 | return 0; | |
689 | } | |
690 | ||
691 | /* | |
692 | * Check a string doesn't overrun the chunk of | |
693 | * memory we copied from userland. | |
694 | */ | |
695 | static int invalid_str(char *str, void *end) | |
696 | { | |
697 | while ((void *) str < end) | |
698 | if (!*str++) | |
699 | return 0; | |
700 | ||
701 | return -EINVAL; | |
702 | } | |
703 | ||
704 | static int dev_rename(struct dm_ioctl *param, size_t param_size) | |
705 | { | |
706 | int r; | |
707 | char *new_name = (char *) param + param->data_start; | |
708 | ||
27238b2b | 709 | if (new_name < param->data || |
bc0fd67f MB |
710 | invalid_str(new_name, (void *) param + param_size) || |
711 | strlen(new_name) > DM_NAME_LEN - 1) { | |
1da177e4 LT |
712 | DMWARN("Invalid new logical volume name supplied."); |
713 | return -EINVAL; | |
714 | } | |
715 | ||
716 | r = check_name(new_name); | |
717 | if (r) | |
718 | return r; | |
719 | ||
720 | param->data_size = 0; | |
60935eb2 | 721 | return dm_hash_rename(param->event_nr, param->name, new_name); |
1da177e4 LT |
722 | } |
723 | ||
3ac51e74 DW |
724 | static int dev_set_geometry(struct dm_ioctl *param, size_t param_size) |
725 | { | |
726 | int r = -EINVAL, x; | |
727 | struct mapped_device *md; | |
728 | struct hd_geometry geometry; | |
729 | unsigned long indata[4]; | |
730 | char *geostr = (char *) param + param->data_start; | |
731 | ||
732 | md = find_device(param); | |
733 | if (!md) | |
734 | return -ENXIO; | |
735 | ||
27238b2b | 736 | if (geostr < param->data || |
3ac51e74 DW |
737 | invalid_str(geostr, (void *) param + param_size)) { |
738 | DMWARN("Invalid geometry supplied."); | |
739 | goto out; | |
740 | } | |
741 | ||
742 | x = sscanf(geostr, "%lu %lu %lu %lu", indata, | |
743 | indata + 1, indata + 2, indata + 3); | |
744 | ||
745 | if (x != 4) { | |
746 | DMWARN("Unable to interpret geometry settings."); | |
747 | goto out; | |
748 | } | |
749 | ||
750 | if (indata[0] > 65535 || indata[1] > 255 || | |
751 | indata[2] > 255 || indata[3] > ULONG_MAX) { | |
752 | DMWARN("Geometry exceeds range limits."); | |
753 | goto out; | |
754 | } | |
755 | ||
756 | geometry.cylinders = indata[0]; | |
757 | geometry.heads = indata[1]; | |
758 | geometry.sectors = indata[2]; | |
759 | geometry.start = indata[3]; | |
760 | ||
761 | r = dm_set_geometry(md, &geometry); | |
762 | if (!r) | |
763 | r = __dev_status(md, param); | |
764 | ||
765 | param->data_size = 0; | |
766 | ||
767 | out: | |
768 | dm_put(md); | |
769 | return r; | |
770 | } | |
771 | ||
1da177e4 LT |
772 | static int do_suspend(struct dm_ioctl *param) |
773 | { | |
774 | int r = 0; | |
a3d77d35 | 775 | unsigned suspend_flags = DM_SUSPEND_LOCKFS_FLAG; |
1da177e4 LT |
776 | struct mapped_device *md; |
777 | ||
778 | md = find_device(param); | |
779 | if (!md) | |
780 | return -ENXIO; | |
781 | ||
6da487dc | 782 | if (param->flags & DM_SKIP_LOCKFS_FLAG) |
a3d77d35 | 783 | suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG; |
81fdb096 KU |
784 | if (param->flags & DM_NOFLUSH_FLAG) |
785 | suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG; | |
6da487dc | 786 | |
1da177e4 | 787 | if (!dm_suspended(md)) |
a3d77d35 | 788 | r = dm_suspend(md, suspend_flags); |
1da177e4 LT |
789 | |
790 | if (!r) | |
791 | r = __dev_status(md, param); | |
792 | ||
793 | dm_put(md); | |
794 | return r; | |
795 | } | |
796 | ||
797 | static int do_resume(struct dm_ioctl *param) | |
798 | { | |
799 | int r = 0; | |
a3d77d35 | 800 | unsigned suspend_flags = DM_SUSPEND_LOCKFS_FLAG; |
1da177e4 LT |
801 | struct hash_cell *hc; |
802 | struct mapped_device *md; | |
803 | struct dm_table *new_map; | |
804 | ||
805 | down_write(&_hash_lock); | |
806 | ||
807 | hc = __find_device_hash_cell(param); | |
808 | if (!hc) { | |
809 | DMWARN("device doesn't appear to be in the dev hash table."); | |
810 | up_write(&_hash_lock); | |
811 | return -ENXIO; | |
812 | } | |
813 | ||
814 | md = hc->md; | |
1da177e4 LT |
815 | |
816 | new_map = hc->new_map; | |
817 | hc->new_map = NULL; | |
818 | param->flags &= ~DM_INACTIVE_PRESENT_FLAG; | |
819 | ||
820 | up_write(&_hash_lock); | |
821 | ||
822 | /* Do we need to load a new map ? */ | |
823 | if (new_map) { | |
824 | /* Suspend if it isn't already suspended */ | |
6da487dc | 825 | if (param->flags & DM_SKIP_LOCKFS_FLAG) |
a3d77d35 | 826 | suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG; |
81fdb096 KU |
827 | if (param->flags & DM_NOFLUSH_FLAG) |
828 | suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG; | |
1da177e4 | 829 | if (!dm_suspended(md)) |
a3d77d35 | 830 | dm_suspend(md, suspend_flags); |
1da177e4 LT |
831 | |
832 | r = dm_swap_table(md, new_map); | |
833 | if (r) { | |
d5816876 | 834 | dm_table_destroy(new_map); |
1da177e4 | 835 | dm_put(md); |
1da177e4 LT |
836 | return r; |
837 | } | |
838 | ||
839 | if (dm_table_get_mode(new_map) & FMODE_WRITE) | |
840 | set_disk_ro(dm_disk(md), 0); | |
841 | else | |
842 | set_disk_ro(dm_disk(md), 1); | |
1da177e4 LT |
843 | } |
844 | ||
845 | if (dm_suspended(md)) | |
846 | r = dm_resume(md); | |
847 | ||
60935eb2 MB |
848 | |
849 | if (!r) { | |
850 | dm_kobject_uevent(md, KOBJ_CHANGE, param->event_nr); | |
1da177e4 | 851 | r = __dev_status(md, param); |
60935eb2 | 852 | } |
1da177e4 LT |
853 | |
854 | dm_put(md); | |
855 | return r; | |
856 | } | |
857 | ||
858 | /* | |
859 | * Set or unset the suspension state of a device. | |
860 | * If the device already is in the requested state we just return its status. | |
861 | */ | |
862 | static int dev_suspend(struct dm_ioctl *param, size_t param_size) | |
863 | { | |
864 | if (param->flags & DM_SUSPEND_FLAG) | |
865 | return do_suspend(param); | |
866 | ||
867 | return do_resume(param); | |
868 | } | |
869 | ||
870 | /* | |
871 | * Copies device info back to user space, used by | |
872 | * the create and info ioctls. | |
873 | */ | |
874 | static int dev_status(struct dm_ioctl *param, size_t param_size) | |
875 | { | |
876 | int r; | |
877 | struct mapped_device *md; | |
878 | ||
879 | md = find_device(param); | |
880 | if (!md) | |
881 | return -ENXIO; | |
882 | ||
883 | r = __dev_status(md, param); | |
884 | dm_put(md); | |
885 | return r; | |
886 | } | |
887 | ||
888 | /* | |
889 | * Build up the status struct for each target | |
890 | */ | |
891 | static void retrieve_status(struct dm_table *table, | |
892 | struct dm_ioctl *param, size_t param_size) | |
893 | { | |
894 | unsigned int i, num_targets; | |
895 | struct dm_target_spec *spec; | |
896 | char *outbuf, *outptr; | |
897 | status_type_t type; | |
898 | size_t remaining, len, used = 0; | |
899 | ||
900 | outptr = outbuf = get_result_buffer(param, param_size, &len); | |
901 | ||
902 | if (param->flags & DM_STATUS_TABLE_FLAG) | |
903 | type = STATUSTYPE_TABLE; | |
904 | else | |
905 | type = STATUSTYPE_INFO; | |
906 | ||
907 | /* Get all the target info */ | |
908 | num_targets = dm_table_get_num_targets(table); | |
909 | for (i = 0; i < num_targets; i++) { | |
910 | struct dm_target *ti = dm_table_get_target(table, i); | |
911 | ||
912 | remaining = len - (outptr - outbuf); | |
913 | if (remaining <= sizeof(struct dm_target_spec)) { | |
914 | param->flags |= DM_BUFFER_FULL_FLAG; | |
915 | break; | |
916 | } | |
917 | ||
918 | spec = (struct dm_target_spec *) outptr; | |
919 | ||
920 | spec->status = 0; | |
921 | spec->sector_start = ti->begin; | |
922 | spec->length = ti->len; | |
923 | strncpy(spec->target_type, ti->type->name, | |
924 | sizeof(spec->target_type)); | |
925 | ||
926 | outptr += sizeof(struct dm_target_spec); | |
927 | remaining = len - (outptr - outbuf); | |
928 | if (remaining <= 0) { | |
929 | param->flags |= DM_BUFFER_FULL_FLAG; | |
930 | break; | |
931 | } | |
932 | ||
933 | /* Get the status/table string from the target driver */ | |
934 | if (ti->type->status) { | |
935 | if (ti->type->status(ti, type, outptr, remaining)) { | |
936 | param->flags |= DM_BUFFER_FULL_FLAG; | |
937 | break; | |
938 | } | |
939 | } else | |
940 | outptr[0] = '\0'; | |
941 | ||
942 | outptr += strlen(outptr) + 1; | |
943 | used = param->data_start + (outptr - outbuf); | |
944 | ||
945 | outptr = align_ptr(outptr); | |
946 | spec->next = outptr - outbuf; | |
947 | } | |
948 | ||
949 | if (used) | |
950 | param->data_size = used; | |
951 | ||
952 | param->target_count = num_targets; | |
953 | } | |
954 | ||
955 | /* | |
956 | * Wait for a device to report an event | |
957 | */ | |
958 | static int dev_wait(struct dm_ioctl *param, size_t param_size) | |
959 | { | |
960 | int r; | |
961 | struct mapped_device *md; | |
962 | struct dm_table *table; | |
963 | ||
964 | md = find_device(param); | |
965 | if (!md) | |
966 | return -ENXIO; | |
967 | ||
968 | /* | |
969 | * Wait for a notification event | |
970 | */ | |
971 | if (dm_wait_event(md, param->event_nr)) { | |
972 | r = -ERESTARTSYS; | |
973 | goto out; | |
974 | } | |
975 | ||
976 | /* | |
977 | * The userland program is going to want to know what | |
978 | * changed to trigger the event, so we may as well tell | |
979 | * him and save an ioctl. | |
980 | */ | |
981 | r = __dev_status(md, param); | |
982 | if (r) | |
983 | goto out; | |
984 | ||
985 | table = dm_get_table(md); | |
986 | if (table) { | |
987 | retrieve_status(table, param, param_size); | |
988 | dm_table_put(table); | |
989 | } | |
990 | ||
991 | out: | |
992 | dm_put(md); | |
993 | return r; | |
994 | } | |
995 | ||
aeb5d727 | 996 | static inline fmode_t get_mode(struct dm_ioctl *param) |
1da177e4 | 997 | { |
aeb5d727 | 998 | fmode_t mode = FMODE_READ | FMODE_WRITE; |
1da177e4 LT |
999 | |
1000 | if (param->flags & DM_READONLY_FLAG) | |
1001 | mode = FMODE_READ; | |
1002 | ||
1003 | return mode; | |
1004 | } | |
1005 | ||
1006 | static int next_target(struct dm_target_spec *last, uint32_t next, void *end, | |
1007 | struct dm_target_spec **spec, char **target_params) | |
1008 | { | |
1009 | *spec = (struct dm_target_spec *) ((unsigned char *) last + next); | |
1010 | *target_params = (char *) (*spec + 1); | |
1011 | ||
1012 | if (*spec < (last + 1)) | |
1013 | return -EINVAL; | |
1014 | ||
1015 | return invalid_str(*target_params, end); | |
1016 | } | |
1017 | ||
1018 | static int populate_table(struct dm_table *table, | |
1019 | struct dm_ioctl *param, size_t param_size) | |
1020 | { | |
1021 | int r; | |
1022 | unsigned int i = 0; | |
1023 | struct dm_target_spec *spec = (struct dm_target_spec *) param; | |
1024 | uint32_t next = param->data_start; | |
1025 | void *end = (void *) param + param_size; | |
1026 | char *target_params; | |
1027 | ||
1028 | if (!param->target_count) { | |
1029 | DMWARN("populate_table: no targets specified"); | |
1030 | return -EINVAL; | |
1031 | } | |
1032 | ||
1033 | for (i = 0; i < param->target_count; i++) { | |
1034 | ||
1035 | r = next_target(spec, next, end, &spec, &target_params); | |
1036 | if (r) { | |
1037 | DMWARN("unable to find target"); | |
1038 | return r; | |
1039 | } | |
1040 | ||
1041 | r = dm_table_add_target(table, spec->target_type, | |
1042 | (sector_t) spec->sector_start, | |
1043 | (sector_t) spec->length, | |
1044 | target_params); | |
1045 | if (r) { | |
1046 | DMWARN("error adding target to table"); | |
1047 | return r; | |
1048 | } | |
1049 | ||
1050 | next = spec->next; | |
1051 | } | |
1052 | ||
1053 | return dm_table_complete(table); | |
1054 | } | |
1055 | ||
9c47008d MP |
1056 | static int table_prealloc_integrity(struct dm_table *t, |
1057 | struct mapped_device *md) | |
1058 | { | |
1059 | struct list_head *devices = dm_table_get_devices(t); | |
1060 | struct dm_dev_internal *dd; | |
1061 | ||
1062 | list_for_each_entry(dd, devices, list) | |
1063 | if (bdev_get_integrity(dd->dm_dev.bdev)) | |
1064 | return blk_integrity_register(dm_disk(md), NULL); | |
1065 | ||
1066 | return 0; | |
1067 | } | |
1068 | ||
1da177e4 LT |
1069 | static int table_load(struct dm_ioctl *param, size_t param_size) |
1070 | { | |
1071 | int r; | |
1072 | struct hash_cell *hc; | |
1073 | struct dm_table *t; | |
1134e5ae MA |
1074 | struct mapped_device *md; |
1075 | ||
1076 | md = find_device(param); | |
1077 | if (!md) | |
1078 | return -ENXIO; | |
1da177e4 | 1079 | |
1134e5ae | 1080 | r = dm_table_create(&t, get_mode(param), param->target_count, md); |
1da177e4 | 1081 | if (r) |
1134e5ae | 1082 | goto out; |
1da177e4 LT |
1083 | |
1084 | r = populate_table(t, param, param_size); | |
1085 | if (r) { | |
f80a5570 | 1086 | dm_table_destroy(t); |
1134e5ae | 1087 | goto out; |
1da177e4 LT |
1088 | } |
1089 | ||
9c47008d MP |
1090 | r = table_prealloc_integrity(t, md); |
1091 | if (r) { | |
1092 | DMERR("%s: could not register integrity profile.", | |
1093 | dm_device_name(md)); | |
1094 | dm_table_destroy(t); | |
1095 | goto out; | |
1096 | } | |
1097 | ||
1da177e4 | 1098 | down_write(&_hash_lock); |
1134e5ae MA |
1099 | hc = dm_get_mdptr(md); |
1100 | if (!hc || hc->md != md) { | |
1101 | DMWARN("device has been removed from the dev hash table."); | |
f80a5570 | 1102 | dm_table_destroy(t); |
1134e5ae MA |
1103 | up_write(&_hash_lock); |
1104 | r = -ENXIO; | |
1105 | goto out; | |
1da177e4 LT |
1106 | } |
1107 | ||
1108 | if (hc->new_map) | |
d5816876 | 1109 | dm_table_destroy(hc->new_map); |
1da177e4 | 1110 | hc->new_map = t; |
1134e5ae MA |
1111 | up_write(&_hash_lock); |
1112 | ||
1da177e4 | 1113 | param->flags |= DM_INACTIVE_PRESENT_FLAG; |
1134e5ae MA |
1114 | r = __dev_status(md, param); |
1115 | ||
1116 | out: | |
1117 | dm_put(md); | |
1da177e4 | 1118 | |
1da177e4 LT |
1119 | return r; |
1120 | } | |
1121 | ||
1122 | static int table_clear(struct dm_ioctl *param, size_t param_size) | |
1123 | { | |
1124 | int r; | |
1125 | struct hash_cell *hc; | |
7ec75f25 | 1126 | struct mapped_device *md; |
1da177e4 LT |
1127 | |
1128 | down_write(&_hash_lock); | |
1129 | ||
1130 | hc = __find_device_hash_cell(param); | |
1131 | if (!hc) { | |
1132 | DMWARN("device doesn't appear to be in the dev hash table."); | |
1133 | up_write(&_hash_lock); | |
1134 | return -ENXIO; | |
1135 | } | |
1136 | ||
1137 | if (hc->new_map) { | |
d5816876 | 1138 | dm_table_destroy(hc->new_map); |
1da177e4 LT |
1139 | hc->new_map = NULL; |
1140 | } | |
1141 | ||
1142 | param->flags &= ~DM_INACTIVE_PRESENT_FLAG; | |
1143 | ||
1144 | r = __dev_status(hc->md, param); | |
7ec75f25 | 1145 | md = hc->md; |
1da177e4 | 1146 | up_write(&_hash_lock); |
7ec75f25 | 1147 | dm_put(md); |
1da177e4 LT |
1148 | return r; |
1149 | } | |
1150 | ||
1151 | /* | |
1152 | * Retrieves a list of devices used by a particular dm device. | |
1153 | */ | |
1154 | static void retrieve_deps(struct dm_table *table, | |
1155 | struct dm_ioctl *param, size_t param_size) | |
1156 | { | |
1157 | unsigned int count = 0; | |
1158 | struct list_head *tmp; | |
1159 | size_t len, needed; | |
82b1519b | 1160 | struct dm_dev_internal *dd; |
1da177e4 LT |
1161 | struct dm_target_deps *deps; |
1162 | ||
1163 | deps = get_result_buffer(param, param_size, &len); | |
1164 | ||
1165 | /* | |
1166 | * Count the devices. | |
1167 | */ | |
1168 | list_for_each (tmp, dm_table_get_devices(table)) | |
1169 | count++; | |
1170 | ||
1171 | /* | |
1172 | * Check we have enough space. | |
1173 | */ | |
1174 | needed = sizeof(*deps) + (sizeof(*deps->dev) * count); | |
1175 | if (len < needed) { | |
1176 | param->flags |= DM_BUFFER_FULL_FLAG; | |
1177 | return; | |
1178 | } | |
1179 | ||
1180 | /* | |
1181 | * Fill in the devices. | |
1182 | */ | |
1183 | deps->count = count; | |
1184 | count = 0; | |
1185 | list_for_each_entry (dd, dm_table_get_devices(table), list) | |
82b1519b | 1186 | deps->dev[count++] = huge_encode_dev(dd->dm_dev.bdev->bd_dev); |
1da177e4 LT |
1187 | |
1188 | param->data_size = param->data_start + needed; | |
1189 | } | |
1190 | ||
1191 | static int table_deps(struct dm_ioctl *param, size_t param_size) | |
1192 | { | |
1193 | int r = 0; | |
1194 | struct mapped_device *md; | |
1195 | struct dm_table *table; | |
1196 | ||
1197 | md = find_device(param); | |
1198 | if (!md) | |
1199 | return -ENXIO; | |
1200 | ||
1201 | r = __dev_status(md, param); | |
1202 | if (r) | |
1203 | goto out; | |
1204 | ||
1205 | table = dm_get_table(md); | |
1206 | if (table) { | |
1207 | retrieve_deps(table, param, param_size); | |
1208 | dm_table_put(table); | |
1209 | } | |
1210 | ||
1211 | out: | |
1212 | dm_put(md); | |
1213 | return r; | |
1214 | } | |
1215 | ||
1216 | /* | |
1217 | * Return the status of a device as a text string for each | |
1218 | * target. | |
1219 | */ | |
1220 | static int table_status(struct dm_ioctl *param, size_t param_size) | |
1221 | { | |
1222 | int r; | |
1223 | struct mapped_device *md; | |
1224 | struct dm_table *table; | |
1225 | ||
1226 | md = find_device(param); | |
1227 | if (!md) | |
1228 | return -ENXIO; | |
1229 | ||
1230 | r = __dev_status(md, param); | |
1231 | if (r) | |
1232 | goto out; | |
1233 | ||
1234 | table = dm_get_table(md); | |
1235 | if (table) { | |
1236 | retrieve_status(table, param, param_size); | |
1237 | dm_table_put(table); | |
1238 | } | |
1239 | ||
1240 | out: | |
1241 | dm_put(md); | |
1242 | return r; | |
1243 | } | |
1244 | ||
1245 | /* | |
1246 | * Pass a message to the target that's at the supplied device offset. | |
1247 | */ | |
1248 | static int target_message(struct dm_ioctl *param, size_t param_size) | |
1249 | { | |
1250 | int r, argc; | |
1251 | char **argv; | |
1252 | struct mapped_device *md; | |
1253 | struct dm_table *table; | |
1254 | struct dm_target *ti; | |
1255 | struct dm_target_msg *tmsg = (void *) param + param->data_start; | |
1256 | ||
1257 | md = find_device(param); | |
1258 | if (!md) | |
1259 | return -ENXIO; | |
1260 | ||
1261 | r = __dev_status(md, param); | |
1262 | if (r) | |
1263 | goto out; | |
1264 | ||
027d50f9 | 1265 | if (tmsg < (struct dm_target_msg *) param->data || |
1da177e4 LT |
1266 | invalid_str(tmsg->message, (void *) param + param_size)) { |
1267 | DMWARN("Invalid target message parameters."); | |
1268 | r = -EINVAL; | |
1269 | goto out; | |
1270 | } | |
1271 | ||
1272 | r = dm_split_args(&argc, &argv, tmsg->message); | |
1273 | if (r) { | |
1274 | DMWARN("Failed to split target message parameters"); | |
1275 | goto out; | |
1276 | } | |
1277 | ||
1278 | table = dm_get_table(md); | |
1279 | if (!table) | |
1280 | goto out_argv; | |
1281 | ||
512875bd JN |
1282 | ti = dm_table_find_target(table, tmsg->sector); |
1283 | if (!dm_target_is_valid(ti)) { | |
1da177e4 LT |
1284 | DMWARN("Target message sector outside device."); |
1285 | r = -EINVAL; | |
512875bd | 1286 | } else if (ti->type->message) |
1da177e4 LT |
1287 | r = ti->type->message(ti, argc, argv); |
1288 | else { | |
1289 | DMWARN("Target type does not support messages"); | |
1290 | r = -EINVAL; | |
1291 | } | |
1292 | ||
1da177e4 LT |
1293 | dm_table_put(table); |
1294 | out_argv: | |
1295 | kfree(argv); | |
1296 | out: | |
1297 | param->data_size = 0; | |
1298 | dm_put(md); | |
1299 | return r; | |
1300 | } | |
1301 | ||
1302 | /*----------------------------------------------------------------- | |
1303 | * Implementation of open/close/ioctl on the special char | |
1304 | * device. | |
1305 | *---------------------------------------------------------------*/ | |
1306 | static ioctl_fn lookup_ioctl(unsigned int cmd) | |
1307 | { | |
1308 | static struct { | |
1309 | int cmd; | |
1310 | ioctl_fn fn; | |
1311 | } _ioctls[] = { | |
1312 | {DM_VERSION_CMD, NULL}, /* version is dealt with elsewhere */ | |
1313 | {DM_REMOVE_ALL_CMD, remove_all}, | |
1314 | {DM_LIST_DEVICES_CMD, list_devices}, | |
1315 | ||
1316 | {DM_DEV_CREATE_CMD, dev_create}, | |
1317 | {DM_DEV_REMOVE_CMD, dev_remove}, | |
1318 | {DM_DEV_RENAME_CMD, dev_rename}, | |
1319 | {DM_DEV_SUSPEND_CMD, dev_suspend}, | |
1320 | {DM_DEV_STATUS_CMD, dev_status}, | |
1321 | {DM_DEV_WAIT_CMD, dev_wait}, | |
1322 | ||
1323 | {DM_TABLE_LOAD_CMD, table_load}, | |
1324 | {DM_TABLE_CLEAR_CMD, table_clear}, | |
1325 | {DM_TABLE_DEPS_CMD, table_deps}, | |
1326 | {DM_TABLE_STATUS_CMD, table_status}, | |
1327 | ||
1328 | {DM_LIST_VERSIONS_CMD, list_versions}, | |
1329 | ||
3ac51e74 DW |
1330 | {DM_TARGET_MSG_CMD, target_message}, |
1331 | {DM_DEV_SET_GEOMETRY_CMD, dev_set_geometry} | |
1da177e4 LT |
1332 | }; |
1333 | ||
1334 | return (cmd >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[cmd].fn; | |
1335 | } | |
1336 | ||
1337 | /* | |
1338 | * As well as checking the version compatibility this always | |
1339 | * copies the kernel interface version out. | |
1340 | */ | |
1341 | static int check_version(unsigned int cmd, struct dm_ioctl __user *user) | |
1342 | { | |
1343 | uint32_t version[3]; | |
1344 | int r = 0; | |
1345 | ||
1346 | if (copy_from_user(version, user->version, sizeof(version))) | |
1347 | return -EFAULT; | |
1348 | ||
1349 | if ((DM_VERSION_MAJOR != version[0]) || | |
1350 | (DM_VERSION_MINOR < version[1])) { | |
1351 | DMWARN("ioctl interface mismatch: " | |
1352 | "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)", | |
1353 | DM_VERSION_MAJOR, DM_VERSION_MINOR, | |
1354 | DM_VERSION_PATCHLEVEL, | |
1355 | version[0], version[1], version[2], cmd); | |
1356 | r = -EINVAL; | |
1357 | } | |
1358 | ||
1359 | /* | |
1360 | * Fill in the kernel version. | |
1361 | */ | |
1362 | version[0] = DM_VERSION_MAJOR; | |
1363 | version[1] = DM_VERSION_MINOR; | |
1364 | version[2] = DM_VERSION_PATCHLEVEL; | |
1365 | if (copy_to_user(user->version, version, sizeof(version))) | |
1366 | return -EFAULT; | |
1367 | ||
1368 | return r; | |
1369 | } | |
1370 | ||
1371 | static void free_params(struct dm_ioctl *param) | |
1372 | { | |
1373 | vfree(param); | |
1374 | } | |
1375 | ||
1376 | static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl **param) | |
1377 | { | |
1378 | struct dm_ioctl tmp, *dmi; | |
1379 | ||
76c072b4 | 1380 | if (copy_from_user(&tmp, user, sizeof(tmp) - sizeof(tmp.data))) |
1da177e4 LT |
1381 | return -EFAULT; |
1382 | ||
76c072b4 | 1383 | if (tmp.data_size < (sizeof(tmp) - sizeof(tmp.data))) |
1da177e4 LT |
1384 | return -EINVAL; |
1385 | ||
bb56acf8 | 1386 | dmi = vmalloc(tmp.data_size); |
1da177e4 LT |
1387 | if (!dmi) |
1388 | return -ENOMEM; | |
1389 | ||
1390 | if (copy_from_user(dmi, user, tmp.data_size)) { | |
1391 | vfree(dmi); | |
1392 | return -EFAULT; | |
1393 | } | |
1394 | ||
1395 | *param = dmi; | |
1396 | return 0; | |
1397 | } | |
1398 | ||
1399 | static int validate_params(uint cmd, struct dm_ioctl *param) | |
1400 | { | |
1401 | /* Always clear this flag */ | |
1402 | param->flags &= ~DM_BUFFER_FULL_FLAG; | |
1403 | ||
1404 | /* Ignores parameters */ | |
1405 | if (cmd == DM_REMOVE_ALL_CMD || | |
1406 | cmd == DM_LIST_DEVICES_CMD || | |
1407 | cmd == DM_LIST_VERSIONS_CMD) | |
1408 | return 0; | |
1409 | ||
1410 | if ((cmd == DM_DEV_CREATE_CMD)) { | |
1411 | if (!*param->name) { | |
1412 | DMWARN("name not supplied when creating device"); | |
1413 | return -EINVAL; | |
1414 | } | |
1415 | } else if ((*param->uuid && *param->name)) { | |
1416 | DMWARN("only supply one of name or uuid, cmd(%u)", cmd); | |
1417 | return -EINVAL; | |
1418 | } | |
1419 | ||
1420 | /* Ensure strings are terminated */ | |
1421 | param->name[DM_NAME_LEN - 1] = '\0'; | |
1422 | param->uuid[DM_UUID_LEN - 1] = '\0'; | |
1423 | ||
1424 | return 0; | |
1425 | } | |
1426 | ||
27238b2b | 1427 | static int ctl_ioctl(uint command, struct dm_ioctl __user *user) |
1da177e4 LT |
1428 | { |
1429 | int r = 0; | |
1430 | unsigned int cmd; | |
a26ffd4a | 1431 | struct dm_ioctl *uninitialized_var(param); |
1da177e4 LT |
1432 | ioctl_fn fn = NULL; |
1433 | size_t param_size; | |
1434 | ||
1435 | /* only root can play with this */ | |
1436 | if (!capable(CAP_SYS_ADMIN)) | |
1437 | return -EACCES; | |
1438 | ||
1439 | if (_IOC_TYPE(command) != DM_IOCTL) | |
1440 | return -ENOTTY; | |
1441 | ||
1442 | cmd = _IOC_NR(command); | |
1443 | ||
1444 | /* | |
1445 | * Check the interface version passed in. This also | |
1446 | * writes out the kernel's interface version. | |
1447 | */ | |
1448 | r = check_version(cmd, user); | |
1449 | if (r) | |
1450 | return r; | |
1451 | ||
1452 | /* | |
1453 | * Nothing more to do for the version command. | |
1454 | */ | |
1455 | if (cmd == DM_VERSION_CMD) | |
1456 | return 0; | |
1457 | ||
1458 | fn = lookup_ioctl(cmd); | |
1459 | if (!fn) { | |
1460 | DMWARN("dm_ctl_ioctl: unknown command 0x%x", command); | |
1461 | return -ENOTTY; | |
1462 | } | |
1463 | ||
1464 | /* | |
1465 | * Trying to avoid low memory issues when a device is | |
1466 | * suspended. | |
1467 | */ | |
1468 | current->flags |= PF_MEMALLOC; | |
1469 | ||
1470 | /* | |
1471 | * Copy the parameters into kernel space. | |
1472 | */ | |
1473 | r = copy_params(user, ¶m); | |
1da177e4 | 1474 | |
dab6a429 AK |
1475 | current->flags &= ~PF_MEMALLOC; |
1476 | ||
1477 | if (r) | |
1478 | return r; | |
1da177e4 LT |
1479 | |
1480 | r = validate_params(cmd, param); | |
1481 | if (r) | |
1482 | goto out; | |
1483 | ||
1484 | param_size = param->data_size; | |
1485 | param->data_size = sizeof(*param); | |
1486 | r = fn(param, param_size); | |
1487 | ||
1488 | /* | |
1489 | * Copy the results back to userland. | |
1490 | */ | |
1491 | if (!r && copy_to_user(user, param, param->data_size)) | |
1492 | r = -EFAULT; | |
1493 | ||
1494 | out: | |
1495 | free_params(param); | |
1da177e4 LT |
1496 | return r; |
1497 | } | |
1498 | ||
27238b2b AK |
1499 | static long dm_ctl_ioctl(struct file *file, uint command, ulong u) |
1500 | { | |
1501 | return (long)ctl_ioctl(command, (struct dm_ioctl __user *)u); | |
1502 | } | |
1503 | ||
76c072b4 MB |
1504 | #ifdef CONFIG_COMPAT |
1505 | static long dm_compat_ctl_ioctl(struct file *file, uint command, ulong u) | |
1506 | { | |
1507 | return (long)dm_ctl_ioctl(file, command, (ulong) compat_ptr(u)); | |
1508 | } | |
1509 | #else | |
1510 | #define dm_compat_ctl_ioctl NULL | |
1511 | #endif | |
1512 | ||
fa027c2a | 1513 | static const struct file_operations _ctl_fops = { |
27238b2b | 1514 | .unlocked_ioctl = dm_ctl_ioctl, |
76c072b4 | 1515 | .compat_ioctl = dm_compat_ctl_ioctl, |
1da177e4 LT |
1516 | .owner = THIS_MODULE, |
1517 | }; | |
1518 | ||
1519 | static struct miscdevice _dm_misc = { | |
1520 | .minor = MISC_DYNAMIC_MINOR, | |
1521 | .name = DM_NAME, | |
d4056405 | 1522 | .devnode = "mapper/control", |
1da177e4 LT |
1523 | .fops = &_ctl_fops |
1524 | }; | |
1525 | ||
1526 | /* | |
1527 | * Create misc character device and link to DM_DIR/control. | |
1528 | */ | |
1529 | int __init dm_interface_init(void) | |
1530 | { | |
1531 | int r; | |
1532 | ||
1533 | r = dm_hash_init(); | |
1534 | if (r) | |
1535 | return r; | |
1536 | ||
1537 | r = misc_register(&_dm_misc); | |
1538 | if (r) { | |
1539 | DMERR("misc_register failed for control device"); | |
1540 | dm_hash_exit(); | |
1541 | return r; | |
1542 | } | |
1543 | ||
1544 | DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR, | |
1545 | DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA, | |
1546 | DM_DRIVER_EMAIL); | |
1547 | return 0; | |
1548 | } | |
1549 | ||
1550 | void dm_interface_exit(void) | |
1551 | { | |
1552 | if (misc_deregister(&_dm_misc) < 0) | |
1553 | DMERR("misc_deregister failed for control device"); | |
1554 | ||
1555 | dm_hash_exit(); | |
1556 | } | |
96a1f7db MA |
1557 | |
1558 | /** | |
1559 | * dm_copy_name_and_uuid - Copy mapped device name & uuid into supplied buffers | |
1560 | * @md: Pointer to mapped_device | |
1561 | * @name: Buffer (size DM_NAME_LEN) for name | |
1562 | * @uuid: Buffer (size DM_UUID_LEN) for uuid or empty string if uuid not defined | |
1563 | */ | |
1564 | int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid) | |
1565 | { | |
1566 | int r = 0; | |
1567 | struct hash_cell *hc; | |
1568 | ||
1569 | if (!md) | |
1570 | return -ENXIO; | |
1571 | ||
1572 | dm_get(md); | |
1573 | down_read(&_hash_lock); | |
1574 | hc = dm_get_mdptr(md); | |
1575 | if (!hc || hc->md != md) { | |
1576 | r = -ENXIO; | |
1577 | goto out; | |
1578 | } | |
1579 | ||
23d39f63 MB |
1580 | if (name) |
1581 | strcpy(name, hc->name); | |
1582 | if (uuid) | |
1583 | strcpy(uuid, hc->uuid ? : ""); | |
96a1f7db MA |
1584 | |
1585 | out: | |
1586 | up_read(&_hash_lock); | |
1587 | dm_put(md); | |
1588 | ||
1589 | return r; | |
1590 | } |