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