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