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