blob: 1235135b384bea5ac1e82129926e30c85d36bd9f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
3 * Copyright (C) 2004 - 2005 Red Hat, Inc. All rights reserved.
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>
16#include <linux/devfs_fs_kernel.h>
17#include <linux/dm-ioctl.h>
18
19#include <asm/uaccess.h>
20
21#define DM_DRIVER_EMAIL "dm-devel@redhat.com"
22
23/*-----------------------------------------------------------------
24 * The ioctl interface needs to be able to look up devices by
25 * name or uuid.
26 *---------------------------------------------------------------*/
27struct hash_cell {
28 struct list_head name_list;
29 struct list_head uuid_list;
30
31 char *name;
32 char *uuid;
33 struct mapped_device *md;
34 struct dm_table *new_map;
35};
36
37struct vers_iter {
38 size_t param_size;
39 struct dm_target_versions *vers, *old_vers;
40 char *end;
41 uint32_t flags;
42};
43
44
45#define NUM_BUCKETS 64
46#define MASK_BUCKETS (NUM_BUCKETS - 1)
47static struct list_head _name_buckets[NUM_BUCKETS];
48static struct list_head _uuid_buckets[NUM_BUCKETS];
49
50static void dm_hash_remove_all(void);
51
52/*
53 * Guards access to both hash tables.
54 */
55static DECLARE_RWSEM(_hash_lock);
56
57static void init_buckets(struct list_head *buckets)
58{
59 unsigned int i;
60
61 for (i = 0; i < NUM_BUCKETS; i++)
62 INIT_LIST_HEAD(buckets + i);
63}
64
65static int dm_hash_init(void)
66{
67 init_buckets(_name_buckets);
68 init_buckets(_uuid_buckets);
69 devfs_mk_dir(DM_DIR);
70 return 0;
71}
72
73static void dm_hash_exit(void)
74{
75 dm_hash_remove_all();
76 devfs_remove(DM_DIR);
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 *---------------------------------------------------------------*/
84static 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 *---------------------------------------------------------------*/
98static 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)
104 if (!strcmp(hc->name, str))
105 return hc;
106
107 return NULL;
108}
109
110static struct hash_cell *__get_uuid_cell(const char *str)
111{
112 struct hash_cell *hc;
113 unsigned int h = hash_str(str);
114
115 list_for_each_entry (hc, _uuid_buckets + h, uuid_list)
116 if (!strcmp(hc->uuid, str))
117 return hc;
118
119 return NULL;
120}
121
122/*-----------------------------------------------------------------
123 * Inserting, removing and renaming a device.
124 *---------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125static struct hash_cell *alloc_cell(const char *name, const char *uuid,
126 struct mapped_device *md)
127{
128 struct hash_cell *hc;
129
130 hc = kmalloc(sizeof(*hc), GFP_KERNEL);
131 if (!hc)
132 return NULL;
133
Paulo Marques543537b2005-06-23 00:09:02 -0700134 hc->name = kstrdup(name, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if (!hc->name) {
136 kfree(hc);
137 return NULL;
138 }
139
140 if (!uuid)
141 hc->uuid = NULL;
142
143 else {
Paulo Marques543537b2005-06-23 00:09:02 -0700144 hc->uuid = kstrdup(uuid, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (!hc->uuid) {
146 kfree(hc->name);
147 kfree(hc);
148 return NULL;
149 }
150 }
151
152 INIT_LIST_HEAD(&hc->name_list);
153 INIT_LIST_HEAD(&hc->uuid_list);
154 hc->md = md;
155 hc->new_map = NULL;
156 return hc;
157}
158
159static void free_cell(struct hash_cell *hc)
160{
161 if (hc) {
162 kfree(hc->name);
163 kfree(hc->uuid);
164 kfree(hc);
165 }
166}
167
168/*
169 * devfs stuff.
170 */
171static int register_with_devfs(struct hash_cell *hc)
172{
173 struct gendisk *disk = dm_disk(hc->md);
174
175 devfs_mk_bdev(MKDEV(disk->major, disk->first_minor),
176 S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP,
177 DM_DIR "/%s", hc->name);
178 return 0;
179}
180
181static int unregister_with_devfs(struct hash_cell *hc)
182{
183 devfs_remove(DM_DIR"/%s", hc->name);
184 return 0;
185}
186
187/*
188 * The kdev_t and uuid of a device can never change once it is
189 * initially inserted.
190 */
191static int dm_hash_insert(const char *name, const char *uuid, struct mapped_device *md)
192{
193 struct hash_cell *cell;
194
195 /*
196 * Allocate the new cells.
197 */
198 cell = alloc_cell(name, uuid, md);
199 if (!cell)
200 return -ENOMEM;
201
202 /*
203 * Insert the cell into both hash tables.
204 */
205 down_write(&_hash_lock);
206 if (__get_name_cell(name))
207 goto bad;
208
209 list_add(&cell->name_list, _name_buckets + hash_str(name));
210
211 if (uuid) {
212 if (__get_uuid_cell(uuid)) {
213 list_del(&cell->name_list);
214 goto bad;
215 }
216 list_add(&cell->uuid_list, _uuid_buckets + hash_str(uuid));
217 }
218 register_with_devfs(cell);
219 dm_get(md);
220 dm_set_mdptr(md, cell);
221 up_write(&_hash_lock);
222
223 return 0;
224
225 bad:
226 up_write(&_hash_lock);
227 free_cell(cell);
228 return -EBUSY;
229}
230
231static void __hash_remove(struct hash_cell *hc)
232{
goggin, edward269fd2a2005-09-27 21:45:44 -0700233 struct dm_table *table;
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /* remove from the dev hash */
236 list_del(&hc->uuid_list);
237 list_del(&hc->name_list);
238 unregister_with_devfs(hc);
239 dm_set_mdptr(hc->md, NULL);
goggin, edward269fd2a2005-09-27 21:45:44 -0700240
241 table = dm_get_table(hc->md);
242 if (table) {
243 dm_table_event(table);
244 dm_table_put(table);
245 }
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 dm_put(hc->md);
248 if (hc->new_map)
249 dm_table_put(hc->new_map);
250 free_cell(hc);
251}
252
253static void dm_hash_remove_all(void)
254{
255 int i;
256 struct hash_cell *hc;
257 struct list_head *tmp, *n;
258
259 down_write(&_hash_lock);
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);
263 __hash_remove(hc);
264 }
265 }
266 up_write(&_hash_lock);
267}
268
269static int dm_hash_rename(const char *old, const char *new)
270{
271 char *new_name, *old_name;
272 struct hash_cell *hc;
goggin, edward81f17772006-01-06 00:20:01 -0800273 struct dm_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 /*
276 * duplicate new.
277 */
Paulo Marques543537b2005-06-23 00:09:02 -0700278 new_name = kstrdup(new, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (!new_name)
280 return -ENOMEM;
281
282 down_write(&_hash_lock);
283
284 /*
285 * Is new free ?
286 */
287 hc = __get_name_cell(new);
288 if (hc) {
289 DMWARN("asked to rename to an already existing name %s -> %s",
290 old, new);
291 up_write(&_hash_lock);
292 kfree(new_name);
293 return -EBUSY;
294 }
295
296 /*
297 * Is there such a device as 'old' ?
298 */
299 hc = __get_name_cell(old);
300 if (!hc) {
301 DMWARN("asked to rename a non existent device %s -> %s",
302 old, new);
303 up_write(&_hash_lock);
304 kfree(new_name);
305 return -ENXIO;
306 }
307
308 /*
309 * rename and move the name cell.
310 */
311 unregister_with_devfs(hc);
312
313 list_del(&hc->name_list);
314 old_name = hc->name;
315 hc->name = new_name;
316 list_add(&hc->name_list, _name_buckets + hash_str(new_name));
317
318 /* rename the device node in devfs */
319 register_with_devfs(hc);
320
goggin, edward81f17772006-01-06 00:20:01 -0800321 /*
322 * Wake up any dm event waiters.
323 */
324 table = dm_get_table(hc->md);
325 if (table) {
326 dm_table_event(table);
327 dm_table_put(table);
328 }
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 up_write(&_hash_lock);
331 kfree(old_name);
332 return 0;
333}
334
335/*-----------------------------------------------------------------
336 * Implementation of the ioctl commands
337 *---------------------------------------------------------------*/
338/*
339 * All the ioctl commands get dispatched to functions with this
340 * prototype.
341 */
342typedef int (*ioctl_fn)(struct dm_ioctl *param, size_t param_size);
343
344static int remove_all(struct dm_ioctl *param, size_t param_size)
345{
346 dm_hash_remove_all();
347 param->data_size = 0;
348 return 0;
349}
350
351/*
352 * Round up the ptr to an 8-byte boundary.
353 */
354#define ALIGN_MASK 7
355static inline void *align_ptr(void *ptr)
356{
357 return (void *) (((size_t) (ptr + ALIGN_MASK)) & ~ALIGN_MASK);
358}
359
360/*
361 * Retrieves the data payload buffer from an already allocated
362 * struct dm_ioctl.
363 */
364static void *get_result_buffer(struct dm_ioctl *param, size_t param_size,
365 size_t *len)
366{
367 param->data_start = align_ptr(param + 1) - (void *) param;
368
369 if (param->data_start < param_size)
370 *len = param_size - param->data_start;
371 else
372 *len = 0;
373
374 return ((void *) param) + param->data_start;
375}
376
377static int list_devices(struct dm_ioctl *param, size_t param_size)
378{
379 unsigned int i;
380 struct hash_cell *hc;
381 size_t len, needed = 0;
382 struct gendisk *disk;
383 struct dm_name_list *nl, *old_nl = NULL;
384
385 down_write(&_hash_lock);
386
387 /*
388 * Loop through all the devices working out how much
389 * space we need.
390 */
391 for (i = 0; i < NUM_BUCKETS; i++) {
392 list_for_each_entry (hc, _name_buckets + i, name_list) {
393 needed += sizeof(struct dm_name_list);
394 needed += strlen(hc->name) + 1;
395 needed += ALIGN_MASK;
396 }
397 }
398
399 /*
400 * Grab our output buffer.
401 */
402 nl = get_result_buffer(param, param_size, &len);
403 if (len < needed) {
404 param->flags |= DM_BUFFER_FULL_FLAG;
405 goto out;
406 }
407 param->data_size = param->data_start + needed;
408
409 nl->dev = 0; /* Flags no data */
410
411 /*
412 * Now loop through filling out the names.
413 */
414 for (i = 0; i < NUM_BUCKETS; i++) {
415 list_for_each_entry (hc, _name_buckets + i, name_list) {
416 if (old_nl)
417 old_nl->next = (uint32_t) ((void *) nl -
418 (void *) old_nl);
419 disk = dm_disk(hc->md);
420 nl->dev = huge_encode_dev(MKDEV(disk->major, disk->first_minor));
421 nl->next = 0;
422 strcpy(nl->name, hc->name);
423
424 old_nl = nl;
425 nl = align_ptr(((void *) ++nl) + strlen(hc->name) + 1);
426 }
427 }
428
429 out:
430 up_write(&_hash_lock);
431 return 0;
432}
433
434static void list_version_get_needed(struct target_type *tt, void *needed_param)
435{
436 size_t *needed = needed_param;
437
Alasdair G Kergonc4cc6632005-11-21 21:32:33 -0800438 *needed += sizeof(struct dm_target_versions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 *needed += strlen(tt->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 *needed += ALIGN_MASK;
441}
442
443static void list_version_get_info(struct target_type *tt, void *param)
444{
445 struct vers_iter *info = param;
446
447 /* Check space - it might have changed since the first iteration */
448 if ((char *)info->vers + sizeof(tt->version) + strlen(tt->name) + 1 >
449 info->end) {
450
451 info->flags = DM_BUFFER_FULL_FLAG;
452 return;
453 }
454
455 if (info->old_vers)
456 info->old_vers->next = (uint32_t) ((void *)info->vers -
457 (void *)info->old_vers);
458 info->vers->version[0] = tt->version[0];
459 info->vers->version[1] = tt->version[1];
460 info->vers->version[2] = tt->version[2];
461 info->vers->next = 0;
462 strcpy(info->vers->name, tt->name);
463
464 info->old_vers = info->vers;
465 info->vers = align_ptr(((void *) ++info->vers) + strlen(tt->name) + 1);
466}
467
468static int list_versions(struct dm_ioctl *param, size_t param_size)
469{
470 size_t len, needed = 0;
471 struct dm_target_versions *vers;
472 struct vers_iter iter_info;
473
474 /*
475 * Loop through all the devices working out how much
476 * space we need.
477 */
478 dm_target_iterate(list_version_get_needed, &needed);
479
480 /*
481 * Grab our output buffer.
482 */
483 vers = get_result_buffer(param, param_size, &len);
484 if (len < needed) {
485 param->flags |= DM_BUFFER_FULL_FLAG;
486 goto out;
487 }
488 param->data_size = param->data_start + needed;
489
490 iter_info.param_size = param_size;
491 iter_info.old_vers = NULL;
492 iter_info.vers = vers;
493 iter_info.flags = 0;
494 iter_info.end = (char *)vers+len;
495
496 /*
497 * Now loop through filling out the names & versions.
498 */
499 dm_target_iterate(list_version_get_info, &iter_info);
500 param->flags |= iter_info.flags;
501
502 out:
503 return 0;
504}
505
506
507
508static int check_name(const char *name)
509{
510 if (strchr(name, '/')) {
511 DMWARN("invalid device name");
512 return -EINVAL;
513 }
514
515 return 0;
516}
517
518/*
519 * Fills in a dm_ioctl structure, ready for sending back to
520 * userland.
521 */
522static int __dev_status(struct mapped_device *md, struct dm_ioctl *param)
523{
524 struct gendisk *disk = dm_disk(md);
525 struct dm_table *table;
526 struct block_device *bdev;
527
528 param->flags &= ~(DM_SUSPEND_FLAG | DM_READONLY_FLAG |
529 DM_ACTIVE_PRESENT_FLAG);
530
531 if (dm_suspended(md))
532 param->flags |= DM_SUSPEND_FLAG;
533
534 param->dev = huge_encode_dev(MKDEV(disk->major, disk->first_minor));
535
536 if (!(param->flags & DM_SKIP_BDGET_FLAG)) {
537 bdev = bdget_disk(disk, 0);
538 if (!bdev)
539 return -ENXIO;
540
541 /*
542 * Yes, this will be out of date by the time it gets back
543 * to userland, but it is still very useful for
544 * debugging.
545 */
546 param->open_count = bdev->bd_openers;
547 bdput(bdev);
548 } else
549 param->open_count = -1;
550
551 if (disk->policy)
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
567static int dev_create(struct dm_ioctl *param, size_t param_size)
568{
569 int r;
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)
577 r = dm_create_with_minor(MINOR(huge_decode_dev(param->dev)), &md);
578 else
579 r = dm_create(&md);
580
581 if (r)
582 return r;
583
584 r = dm_hash_insert(param->name, *param->uuid ? param->uuid : NULL, md);
585 if (r) {
586 dm_put(md);
587 return r;
588 }
589
590 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
591
592 r = __dev_status(md, param);
593 dm_put(md);
594
595 return r;
596}
597
598/*
599 * Always use UUID for lookups if it's present, otherwise use name or dev.
600 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800601static struct hash_cell *__find_device_hash_cell(struct dm_ioctl *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
603 if (*param->uuid)
604 return __get_uuid_cell(param->uuid);
605 else if (*param->name)
606 return __get_name_cell(param->name);
607 else
608 return dm_get_mdptr(huge_decode_dev(param->dev));
609}
610
Arjan van de Ven858119e2006-01-14 13:20:43 -0800611static struct mapped_device *find_device(struct dm_ioctl *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
613 struct hash_cell *hc;
614 struct mapped_device *md = NULL;
615
616 down_read(&_hash_lock);
617 hc = __find_device_hash_cell(param);
618 if (hc) {
619 md = hc->md;
620 dm_get(md);
621
622 /*
623 * Sneakily write in both the name and the uuid
624 * while we have the cell.
625 */
626 strncpy(param->name, hc->name, sizeof(param->name));
627 if (hc->uuid)
628 strncpy(param->uuid, hc->uuid, sizeof(param->uuid)-1);
629 else
630 param->uuid[0] = '\0';
631
632 if (hc->new_map)
633 param->flags |= DM_INACTIVE_PRESENT_FLAG;
634 else
635 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
636 }
637 up_read(&_hash_lock);
638
639 return md;
640}
641
642static int dev_remove(struct dm_ioctl *param, size_t param_size)
643{
644 struct hash_cell *hc;
645
646 down_write(&_hash_lock);
647 hc = __find_device_hash_cell(param);
648
649 if (!hc) {
650 DMWARN("device doesn't appear to be in the dev hash table.");
651 up_write(&_hash_lock);
652 return -ENXIO;
653 }
654
655 __hash_remove(hc);
656 up_write(&_hash_lock);
657 param->data_size = 0;
658 return 0;
659}
660
661/*
662 * Check a string doesn't overrun the chunk of
663 * memory we copied from userland.
664 */
665static int invalid_str(char *str, void *end)
666{
667 while ((void *) str < end)
668 if (!*str++)
669 return 0;
670
671 return -EINVAL;
672}
673
674static int dev_rename(struct dm_ioctl *param, size_t param_size)
675{
676 int r;
677 char *new_name = (char *) param + param->data_start;
678
679 if (new_name < (char *) (param + 1) ||
680 invalid_str(new_name, (void *) param + param_size)) {
681 DMWARN("Invalid new logical volume name supplied.");
682 return -EINVAL;
683 }
684
685 r = check_name(new_name);
686 if (r)
687 return r;
688
689 param->data_size = 0;
690 return dm_hash_rename(param->name, new_name);
691}
692
693static int do_suspend(struct dm_ioctl *param)
694{
695 int r = 0;
Alasdair G Kergon6da487d2006-01-06 00:20:07 -0800696 int do_lockfs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 struct mapped_device *md;
698
699 md = find_device(param);
700 if (!md)
701 return -ENXIO;
702
Alasdair G Kergon6da487d2006-01-06 00:20:07 -0800703 if (param->flags & DM_SKIP_LOCKFS_FLAG)
704 do_lockfs = 0;
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (!dm_suspended(md))
Alasdair G Kergon6da487d2006-01-06 00:20:07 -0800707 r = dm_suspend(md, do_lockfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 if (!r)
710 r = __dev_status(md, param);
711
712 dm_put(md);
713 return r;
714}
715
716static int do_resume(struct dm_ioctl *param)
717{
718 int r = 0;
Alasdair G Kergon6da487d2006-01-06 00:20:07 -0800719 int do_lockfs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 struct hash_cell *hc;
721 struct mapped_device *md;
722 struct dm_table *new_map;
723
724 down_write(&_hash_lock);
725
726 hc = __find_device_hash_cell(param);
727 if (!hc) {
728 DMWARN("device doesn't appear to be in the dev hash table.");
729 up_write(&_hash_lock);
730 return -ENXIO;
731 }
732
733 md = hc->md;
734 dm_get(md);
735
736 new_map = hc->new_map;
737 hc->new_map = NULL;
738 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
739
740 up_write(&_hash_lock);
741
742 /* Do we need to load a new map ? */
743 if (new_map) {
744 /* Suspend if it isn't already suspended */
Alasdair G Kergon6da487d2006-01-06 00:20:07 -0800745 if (param->flags & DM_SKIP_LOCKFS_FLAG)
746 do_lockfs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (!dm_suspended(md))
Alasdair G Kergon6da487d2006-01-06 00:20:07 -0800748 dm_suspend(md, do_lockfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 r = dm_swap_table(md, new_map);
751 if (r) {
752 dm_put(md);
753 dm_table_put(new_map);
754 return r;
755 }
756
757 if (dm_table_get_mode(new_map) & FMODE_WRITE)
758 set_disk_ro(dm_disk(md), 0);
759 else
760 set_disk_ro(dm_disk(md), 1);
761
762 dm_table_put(new_map);
763 }
764
765 if (dm_suspended(md))
766 r = dm_resume(md);
767
768 if (!r)
769 r = __dev_status(md, param);
770
771 dm_put(md);
772 return r;
773}
774
775/*
776 * Set or unset the suspension state of a device.
777 * If the device already is in the requested state we just return its status.
778 */
779static int dev_suspend(struct dm_ioctl *param, size_t param_size)
780{
781 if (param->flags & DM_SUSPEND_FLAG)
782 return do_suspend(param);
783
784 return do_resume(param);
785}
786
787/*
788 * Copies device info back to user space, used by
789 * the create and info ioctls.
790 */
791static int dev_status(struct dm_ioctl *param, size_t param_size)
792{
793 int r;
794 struct mapped_device *md;
795
796 md = find_device(param);
797 if (!md)
798 return -ENXIO;
799
800 r = __dev_status(md, param);
801 dm_put(md);
802 return r;
803}
804
805/*
806 * Build up the status struct for each target
807 */
808static void retrieve_status(struct dm_table *table,
809 struct dm_ioctl *param, size_t param_size)
810{
811 unsigned int i, num_targets;
812 struct dm_target_spec *spec;
813 char *outbuf, *outptr;
814 status_type_t type;
815 size_t remaining, len, used = 0;
816
817 outptr = outbuf = get_result_buffer(param, param_size, &len);
818
819 if (param->flags & DM_STATUS_TABLE_FLAG)
820 type = STATUSTYPE_TABLE;
821 else
822 type = STATUSTYPE_INFO;
823
824 /* Get all the target info */
825 num_targets = dm_table_get_num_targets(table);
826 for (i = 0; i < num_targets; i++) {
827 struct dm_target *ti = dm_table_get_target(table, i);
828
829 remaining = len - (outptr - outbuf);
830 if (remaining <= sizeof(struct dm_target_spec)) {
831 param->flags |= DM_BUFFER_FULL_FLAG;
832 break;
833 }
834
835 spec = (struct dm_target_spec *) outptr;
836
837 spec->status = 0;
838 spec->sector_start = ti->begin;
839 spec->length = ti->len;
840 strncpy(spec->target_type, ti->type->name,
841 sizeof(spec->target_type));
842
843 outptr += sizeof(struct dm_target_spec);
844 remaining = len - (outptr - outbuf);
845 if (remaining <= 0) {
846 param->flags |= DM_BUFFER_FULL_FLAG;
847 break;
848 }
849
850 /* Get the status/table string from the target driver */
851 if (ti->type->status) {
852 if (ti->type->status(ti, type, outptr, remaining)) {
853 param->flags |= DM_BUFFER_FULL_FLAG;
854 break;
855 }
856 } else
857 outptr[0] = '\0';
858
859 outptr += strlen(outptr) + 1;
860 used = param->data_start + (outptr - outbuf);
861
862 outptr = align_ptr(outptr);
863 spec->next = outptr - outbuf;
864 }
865
866 if (used)
867 param->data_size = used;
868
869 param->target_count = num_targets;
870}
871
872/*
873 * Wait for a device to report an event
874 */
875static int dev_wait(struct dm_ioctl *param, size_t param_size)
876{
877 int r;
878 struct mapped_device *md;
879 struct dm_table *table;
880
881 md = find_device(param);
882 if (!md)
883 return -ENXIO;
884
885 /*
886 * Wait for a notification event
887 */
888 if (dm_wait_event(md, param->event_nr)) {
889 r = -ERESTARTSYS;
890 goto out;
891 }
892
893 /*
894 * The userland program is going to want to know what
895 * changed to trigger the event, so we may as well tell
896 * him and save an ioctl.
897 */
898 r = __dev_status(md, param);
899 if (r)
900 goto out;
901
902 table = dm_get_table(md);
903 if (table) {
904 retrieve_status(table, param, param_size);
905 dm_table_put(table);
906 }
907
908 out:
909 dm_put(md);
910 return r;
911}
912
913static inline int get_mode(struct dm_ioctl *param)
914{
915 int mode = FMODE_READ | FMODE_WRITE;
916
917 if (param->flags & DM_READONLY_FLAG)
918 mode = FMODE_READ;
919
920 return mode;
921}
922
923static int next_target(struct dm_target_spec *last, uint32_t next, void *end,
924 struct dm_target_spec **spec, char **target_params)
925{
926 *spec = (struct dm_target_spec *) ((unsigned char *) last + next);
927 *target_params = (char *) (*spec + 1);
928
929 if (*spec < (last + 1))
930 return -EINVAL;
931
932 return invalid_str(*target_params, end);
933}
934
935static int populate_table(struct dm_table *table,
936 struct dm_ioctl *param, size_t param_size)
937{
938 int r;
939 unsigned int i = 0;
940 struct dm_target_spec *spec = (struct dm_target_spec *) param;
941 uint32_t next = param->data_start;
942 void *end = (void *) param + param_size;
943 char *target_params;
944
945 if (!param->target_count) {
946 DMWARN("populate_table: no targets specified");
947 return -EINVAL;
948 }
949
950 for (i = 0; i < param->target_count; i++) {
951
952 r = next_target(spec, next, end, &spec, &target_params);
953 if (r) {
954 DMWARN("unable to find target");
955 return r;
956 }
957
958 r = dm_table_add_target(table, spec->target_type,
959 (sector_t) spec->sector_start,
960 (sector_t) spec->length,
961 target_params);
962 if (r) {
963 DMWARN("error adding target to table");
964 return r;
965 }
966
967 next = spec->next;
968 }
969
970 return dm_table_complete(table);
971}
972
973static int table_load(struct dm_ioctl *param, size_t param_size)
974{
975 int r;
976 struct hash_cell *hc;
977 struct dm_table *t;
978
979 r = dm_table_create(&t, get_mode(param), param->target_count);
980 if (r)
981 return r;
982
983 r = populate_table(t, param, param_size);
984 if (r) {
985 dm_table_put(t);
986 return r;
987 }
988
989 down_write(&_hash_lock);
990 hc = __find_device_hash_cell(param);
991 if (!hc) {
992 DMWARN("device doesn't appear to be in the dev hash table.");
993 up_write(&_hash_lock);
Kiyoshi Uedab6fcc802005-11-21 21:32:32 -0800994 dm_table_put(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 return -ENXIO;
996 }
997
998 if (hc->new_map)
999 dm_table_put(hc->new_map);
1000 hc->new_map = t;
1001 param->flags |= DM_INACTIVE_PRESENT_FLAG;
1002
1003 r = __dev_status(hc->md, param);
1004 up_write(&_hash_lock);
1005 return r;
1006}
1007
1008static int table_clear(struct dm_ioctl *param, size_t param_size)
1009{
1010 int r;
1011 struct hash_cell *hc;
1012
1013 down_write(&_hash_lock);
1014
1015 hc = __find_device_hash_cell(param);
1016 if (!hc) {
1017 DMWARN("device doesn't appear to be in the dev hash table.");
1018 up_write(&_hash_lock);
1019 return -ENXIO;
1020 }
1021
1022 if (hc->new_map) {
1023 dm_table_put(hc->new_map);
1024 hc->new_map = NULL;
1025 }
1026
1027 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
1028
1029 r = __dev_status(hc->md, param);
1030 up_write(&_hash_lock);
1031 return r;
1032}
1033
1034/*
1035 * Retrieves a list of devices used by a particular dm device.
1036 */
1037static void retrieve_deps(struct dm_table *table,
1038 struct dm_ioctl *param, size_t param_size)
1039{
1040 unsigned int count = 0;
1041 struct list_head *tmp;
1042 size_t len, needed;
1043 struct dm_dev *dd;
1044 struct dm_target_deps *deps;
1045
1046 deps = get_result_buffer(param, param_size, &len);
1047
1048 /*
1049 * Count the devices.
1050 */
1051 list_for_each (tmp, dm_table_get_devices(table))
1052 count++;
1053
1054 /*
1055 * Check we have enough space.
1056 */
1057 needed = sizeof(*deps) + (sizeof(*deps->dev) * count);
1058 if (len < needed) {
1059 param->flags |= DM_BUFFER_FULL_FLAG;
1060 return;
1061 }
1062
1063 /*
1064 * Fill in the devices.
1065 */
1066 deps->count = count;
1067 count = 0;
1068 list_for_each_entry (dd, dm_table_get_devices(table), list)
1069 deps->dev[count++] = huge_encode_dev(dd->bdev->bd_dev);
1070
1071 param->data_size = param->data_start + needed;
1072}
1073
1074static int table_deps(struct dm_ioctl *param, size_t param_size)
1075{
1076 int r = 0;
1077 struct mapped_device *md;
1078 struct dm_table *table;
1079
1080 md = find_device(param);
1081 if (!md)
1082 return -ENXIO;
1083
1084 r = __dev_status(md, param);
1085 if (r)
1086 goto out;
1087
1088 table = dm_get_table(md);
1089 if (table) {
1090 retrieve_deps(table, param, param_size);
1091 dm_table_put(table);
1092 }
1093
1094 out:
1095 dm_put(md);
1096 return r;
1097}
1098
1099/*
1100 * Return the status of a device as a text string for each
1101 * target.
1102 */
1103static int table_status(struct dm_ioctl *param, size_t param_size)
1104{
1105 int r;
1106 struct mapped_device *md;
1107 struct dm_table *table;
1108
1109 md = find_device(param);
1110 if (!md)
1111 return -ENXIO;
1112
1113 r = __dev_status(md, param);
1114 if (r)
1115 goto out;
1116
1117 table = dm_get_table(md);
1118 if (table) {
1119 retrieve_status(table, param, param_size);
1120 dm_table_put(table);
1121 }
1122
1123 out:
1124 dm_put(md);
1125 return r;
1126}
1127
1128/*
1129 * Pass a message to the target that's at the supplied device offset.
1130 */
1131static int target_message(struct dm_ioctl *param, size_t param_size)
1132{
1133 int r, argc;
1134 char **argv;
1135 struct mapped_device *md;
1136 struct dm_table *table;
1137 struct dm_target *ti;
1138 struct dm_target_msg *tmsg = (void *) param + param->data_start;
1139
1140 md = find_device(param);
1141 if (!md)
1142 return -ENXIO;
1143
1144 r = __dev_status(md, param);
1145 if (r)
1146 goto out;
1147
1148 if (tmsg < (struct dm_target_msg *) (param + 1) ||
1149 invalid_str(tmsg->message, (void *) param + param_size)) {
1150 DMWARN("Invalid target message parameters.");
1151 r = -EINVAL;
1152 goto out;
1153 }
1154
1155 r = dm_split_args(&argc, &argv, tmsg->message);
1156 if (r) {
1157 DMWARN("Failed to split target message parameters");
1158 goto out;
1159 }
1160
1161 table = dm_get_table(md);
1162 if (!table)
1163 goto out_argv;
1164
1165 if (tmsg->sector >= dm_table_get_size(table)) {
1166 DMWARN("Target message sector outside device.");
1167 r = -EINVAL;
1168 goto out_table;
1169 }
1170
1171 ti = dm_table_find_target(table, tmsg->sector);
1172 if (ti->type->message)
1173 r = ti->type->message(ti, argc, argv);
1174 else {
1175 DMWARN("Target type does not support messages");
1176 r = -EINVAL;
1177 }
1178
1179 out_table:
1180 dm_table_put(table);
1181 out_argv:
1182 kfree(argv);
1183 out:
1184 param->data_size = 0;
1185 dm_put(md);
1186 return r;
1187}
1188
1189/*-----------------------------------------------------------------
1190 * Implementation of open/close/ioctl on the special char
1191 * device.
1192 *---------------------------------------------------------------*/
1193static ioctl_fn lookup_ioctl(unsigned int cmd)
1194{
1195 static struct {
1196 int cmd;
1197 ioctl_fn fn;
1198 } _ioctls[] = {
1199 {DM_VERSION_CMD, NULL}, /* version is dealt with elsewhere */
1200 {DM_REMOVE_ALL_CMD, remove_all},
1201 {DM_LIST_DEVICES_CMD, list_devices},
1202
1203 {DM_DEV_CREATE_CMD, dev_create},
1204 {DM_DEV_REMOVE_CMD, dev_remove},
1205 {DM_DEV_RENAME_CMD, dev_rename},
1206 {DM_DEV_SUSPEND_CMD, dev_suspend},
1207 {DM_DEV_STATUS_CMD, dev_status},
1208 {DM_DEV_WAIT_CMD, dev_wait},
1209
1210 {DM_TABLE_LOAD_CMD, table_load},
1211 {DM_TABLE_CLEAR_CMD, table_clear},
1212 {DM_TABLE_DEPS_CMD, table_deps},
1213 {DM_TABLE_STATUS_CMD, table_status},
1214
1215 {DM_LIST_VERSIONS_CMD, list_versions},
1216
1217 {DM_TARGET_MSG_CMD, target_message}
1218 };
1219
1220 return (cmd >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[cmd].fn;
1221}
1222
1223/*
1224 * As well as checking the version compatibility this always
1225 * copies the kernel interface version out.
1226 */
1227static int check_version(unsigned int cmd, struct dm_ioctl __user *user)
1228{
1229 uint32_t version[3];
1230 int r = 0;
1231
1232 if (copy_from_user(version, user->version, sizeof(version)))
1233 return -EFAULT;
1234
1235 if ((DM_VERSION_MAJOR != version[0]) ||
1236 (DM_VERSION_MINOR < version[1])) {
1237 DMWARN("ioctl interface mismatch: "
1238 "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
1239 DM_VERSION_MAJOR, DM_VERSION_MINOR,
1240 DM_VERSION_PATCHLEVEL,
1241 version[0], version[1], version[2], cmd);
1242 r = -EINVAL;
1243 }
1244
1245 /*
1246 * Fill in the kernel version.
1247 */
1248 version[0] = DM_VERSION_MAJOR;
1249 version[1] = DM_VERSION_MINOR;
1250 version[2] = DM_VERSION_PATCHLEVEL;
1251 if (copy_to_user(user->version, version, sizeof(version)))
1252 return -EFAULT;
1253
1254 return r;
1255}
1256
1257static void free_params(struct dm_ioctl *param)
1258{
1259 vfree(param);
1260}
1261
1262static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl **param)
1263{
1264 struct dm_ioctl tmp, *dmi;
1265
1266 if (copy_from_user(&tmp, user, sizeof(tmp)))
1267 return -EFAULT;
1268
1269 if (tmp.data_size < sizeof(tmp))
1270 return -EINVAL;
1271
1272 dmi = (struct dm_ioctl *) vmalloc(tmp.data_size);
1273 if (!dmi)
1274 return -ENOMEM;
1275
1276 if (copy_from_user(dmi, user, tmp.data_size)) {
1277 vfree(dmi);
1278 return -EFAULT;
1279 }
1280
1281 *param = dmi;
1282 return 0;
1283}
1284
1285static int validate_params(uint cmd, struct dm_ioctl *param)
1286{
1287 /* Always clear this flag */
1288 param->flags &= ~DM_BUFFER_FULL_FLAG;
1289
1290 /* Ignores parameters */
1291 if (cmd == DM_REMOVE_ALL_CMD ||
1292 cmd == DM_LIST_DEVICES_CMD ||
1293 cmd == DM_LIST_VERSIONS_CMD)
1294 return 0;
1295
1296 if ((cmd == DM_DEV_CREATE_CMD)) {
1297 if (!*param->name) {
1298 DMWARN("name not supplied when creating device");
1299 return -EINVAL;
1300 }
1301 } else if ((*param->uuid && *param->name)) {
1302 DMWARN("only supply one of name or uuid, cmd(%u)", cmd);
1303 return -EINVAL;
1304 }
1305
1306 /* Ensure strings are terminated */
1307 param->name[DM_NAME_LEN - 1] = '\0';
1308 param->uuid[DM_UUID_LEN - 1] = '\0';
1309
1310 return 0;
1311}
1312
1313static int ctl_ioctl(struct inode *inode, struct file *file,
1314 uint command, ulong u)
1315{
1316 int r = 0;
1317 unsigned int cmd;
1318 struct dm_ioctl *param;
1319 struct dm_ioctl __user *user = (struct dm_ioctl __user *) u;
1320 ioctl_fn fn = NULL;
1321 size_t param_size;
1322
1323 /* only root can play with this */
1324 if (!capable(CAP_SYS_ADMIN))
1325 return -EACCES;
1326
1327 if (_IOC_TYPE(command) != DM_IOCTL)
1328 return -ENOTTY;
1329
1330 cmd = _IOC_NR(command);
1331
1332 /*
1333 * Check the interface version passed in. This also
1334 * writes out the kernel's interface version.
1335 */
1336 r = check_version(cmd, user);
1337 if (r)
1338 return r;
1339
1340 /*
1341 * Nothing more to do for the version command.
1342 */
1343 if (cmd == DM_VERSION_CMD)
1344 return 0;
1345
1346 fn = lookup_ioctl(cmd);
1347 if (!fn) {
1348 DMWARN("dm_ctl_ioctl: unknown command 0x%x", command);
1349 return -ENOTTY;
1350 }
1351
1352 /*
1353 * Trying to avoid low memory issues when a device is
1354 * suspended.
1355 */
1356 current->flags |= PF_MEMALLOC;
1357
1358 /*
1359 * Copy the parameters into kernel space.
1360 */
1361 r = copy_params(user, &param);
1362 if (r) {
1363 current->flags &= ~PF_MEMALLOC;
1364 return r;
1365 }
1366
1367 /*
1368 * FIXME: eventually we will remove the PF_MEMALLOC flag
1369 * here. However the tools still do nasty things like
1370 * 'load' while a device is suspended.
1371 */
1372
1373 r = validate_params(cmd, param);
1374 if (r)
1375 goto out;
1376
1377 param_size = param->data_size;
1378 param->data_size = sizeof(*param);
1379 r = fn(param, param_size);
1380
1381 /*
1382 * Copy the results back to userland.
1383 */
1384 if (!r && copy_to_user(user, param, param->data_size))
1385 r = -EFAULT;
1386
1387 out:
1388 free_params(param);
1389 current->flags &= ~PF_MEMALLOC;
1390 return r;
1391}
1392
1393static struct file_operations _ctl_fops = {
1394 .ioctl = ctl_ioctl,
1395 .owner = THIS_MODULE,
1396};
1397
1398static struct miscdevice _dm_misc = {
1399 .minor = MISC_DYNAMIC_MINOR,
1400 .name = DM_NAME,
1401 .devfs_name = "mapper/control",
1402 .fops = &_ctl_fops
1403};
1404
1405/*
1406 * Create misc character device and link to DM_DIR/control.
1407 */
1408int __init dm_interface_init(void)
1409{
1410 int r;
1411
1412 r = dm_hash_init();
1413 if (r)
1414 return r;
1415
1416 r = misc_register(&_dm_misc);
1417 if (r) {
1418 DMERR("misc_register failed for control device");
1419 dm_hash_exit();
1420 return r;
1421 }
1422
1423 DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR,
1424 DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA,
1425 DM_DRIVER_EMAIL);
1426 return 0;
1427}
1428
1429void dm_interface_exit(void)
1430{
1431 if (misc_deregister(&_dm_misc) < 0)
1432 DMERR("misc_deregister failed for control device");
1433
1434 dm_hash_exit();
1435}