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