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