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