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