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