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