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