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-core.h"
9 #include "dm-ima.h"
10 #include <linux/module.h>
11 #include <linux/vmalloc.h>
12 #include <linux/miscdevice.h>
13 #include <linux/sched/mm.h>
14 #include <linux/init.h>
15 #include <linux/wait.h>
16 #include <linux/slab.h>
17 #include <linux/rbtree.h>
18 #include <linux/dm-ioctl.h>
19 #include <linux/hdreg.h>
20 #include <linux/compat.h>
21 #include <linux/nospec.h>
22
23 #include <linux/uaccess.h>
24 #include <linux/ima.h>
25
26 #define DM_MSG_PREFIX "ioctl"
27 #define DM_DRIVER_EMAIL "dm-devel@redhat.com"
28
29 struct dm_file {
30 /*
31 * poll will wait until the global event number is greater than
32 * this value.
33 */
34 volatile unsigned int global_event_nr;
35 };
36
37 /*-----------------------------------------------------------------
38 * The ioctl interface needs to be able to look up devices by
39 * name or uuid.
40 *---------------------------------------------------------------*/
41 struct hash_cell {
42 struct rb_node name_node;
43 struct rb_node uuid_node;
44 bool name_set;
45 bool uuid_set;
46
47 char *name;
48 char *uuid;
49 struct mapped_device *md;
50 struct dm_table *new_map;
51 };
52
53 struct vers_iter {
54 size_t param_size;
55 struct dm_target_versions *vers, *old_vers;
56 char *end;
57 uint32_t flags;
58 };
59
60
61 static struct rb_root name_rb_tree = RB_ROOT;
62 static struct rb_root uuid_rb_tree = RB_ROOT;
63
64 static void dm_hash_remove_all(bool keep_open_devices, bool mark_deferred, bool only_deferred);
65
66 /*
67 * Guards access to both hash tables.
68 */
69 static DECLARE_RWSEM(_hash_lock);
70
71 /*
72 * Protects use of mdptr to obtain hash cell name and uuid from mapped device.
73 */
74 static DEFINE_MUTEX(dm_hash_cells_mutex);
75
dm_hash_exit(void)76 static void dm_hash_exit(void)
77 {
78 dm_hash_remove_all(false, false, false);
79 }
80
81 /*-----------------------------------------------------------------
82 * Code for looking up a device by name
83 *---------------------------------------------------------------*/
__get_name_cell(const char * str)84 static struct hash_cell *__get_name_cell(const char *str)
85 {
86 struct rb_node *n = name_rb_tree.rb_node;
87
88 while (n) {
89 struct hash_cell *hc = container_of(n, struct hash_cell, name_node);
90 int c = strcmp(hc->name, str);
91 if (!c) {
92 dm_get(hc->md);
93 return hc;
94 }
95 n = c >= 0 ? n->rb_left : n->rb_right;
96 }
97
98 return NULL;
99 }
100
__get_uuid_cell(const char * str)101 static struct hash_cell *__get_uuid_cell(const char *str)
102 {
103 struct rb_node *n = uuid_rb_tree.rb_node;
104
105 while (n) {
106 struct hash_cell *hc = container_of(n, struct hash_cell, uuid_node);
107 int c = strcmp(hc->uuid, str);
108 if (!c) {
109 dm_get(hc->md);
110 return hc;
111 }
112 n = c >= 0 ? n->rb_left : n->rb_right;
113 }
114
115 return NULL;
116 }
117
__unlink_name(struct hash_cell * hc)118 static void __unlink_name(struct hash_cell *hc)
119 {
120 if (hc->name_set) {
121 hc->name_set = false;
122 rb_erase(&hc->name_node, &name_rb_tree);
123 }
124 }
125
__unlink_uuid(struct hash_cell * hc)126 static void __unlink_uuid(struct hash_cell *hc)
127 {
128 if (hc->uuid_set) {
129 hc->uuid_set = false;
130 rb_erase(&hc->uuid_node, &uuid_rb_tree);
131 }
132 }
133
__link_name(struct hash_cell * new_hc)134 static void __link_name(struct hash_cell *new_hc)
135 {
136 struct rb_node **n, *parent;
137
138 __unlink_name(new_hc);
139
140 new_hc->name_set = true;
141
142 n = &name_rb_tree.rb_node;
143 parent = NULL;
144
145 while (*n) {
146 struct hash_cell *hc = container_of(*n, struct hash_cell, name_node);
147 int c = strcmp(hc->name, new_hc->name);
148 BUG_ON(!c);
149 parent = *n;
150 n = c >= 0 ? &hc->name_node.rb_left : &hc->name_node.rb_right;
151 }
152
153 rb_link_node(&new_hc->name_node, parent, n);
154 rb_insert_color(&new_hc->name_node, &name_rb_tree);
155 }
156
__link_uuid(struct hash_cell * new_hc)157 static void __link_uuid(struct hash_cell *new_hc)
158 {
159 struct rb_node **n, *parent;
160
161 __unlink_uuid(new_hc);
162
163 new_hc->uuid_set = true;
164
165 n = &uuid_rb_tree.rb_node;
166 parent = NULL;
167
168 while (*n) {
169 struct hash_cell *hc = container_of(*n, struct hash_cell, uuid_node);
170 int c = strcmp(hc->uuid, new_hc->uuid);
171 BUG_ON(!c);
172 parent = *n;
173 n = c > 0 ? &hc->uuid_node.rb_left : &hc->uuid_node.rb_right;
174 }
175
176 rb_link_node(&new_hc->uuid_node, parent, n);
177 rb_insert_color(&new_hc->uuid_node, &uuid_rb_tree);
178 }
179
__get_dev_cell(uint64_t dev)180 static struct hash_cell *__get_dev_cell(uint64_t dev)
181 {
182 struct mapped_device *md;
183 struct hash_cell *hc;
184
185 md = dm_get_md(huge_decode_dev(dev));
186 if (!md)
187 return NULL;
188
189 hc = dm_get_mdptr(md);
190 if (!hc) {
191 dm_put(md);
192 return NULL;
193 }
194
195 return hc;
196 }
197
198 /*-----------------------------------------------------------------
199 * Inserting, removing and renaming a device.
200 *---------------------------------------------------------------*/
alloc_cell(const char * name,const char * uuid,struct mapped_device * md)201 static struct hash_cell *alloc_cell(const char *name, const char *uuid,
202 struct mapped_device *md)
203 {
204 struct hash_cell *hc;
205
206 hc = kmalloc(sizeof(*hc), GFP_KERNEL);
207 if (!hc)
208 return NULL;
209
210 hc->name = kstrdup(name, GFP_KERNEL);
211 if (!hc->name) {
212 kfree(hc);
213 return NULL;
214 }
215
216 if (!uuid)
217 hc->uuid = NULL;
218
219 else {
220 hc->uuid = kstrdup(uuid, GFP_KERNEL);
221 if (!hc->uuid) {
222 kfree(hc->name);
223 kfree(hc);
224 return NULL;
225 }
226 }
227
228 hc->name_set = hc->uuid_set = false;
229 hc->md = md;
230 hc->new_map = NULL;
231 return hc;
232 }
233
free_cell(struct hash_cell * hc)234 static void free_cell(struct hash_cell *hc)
235 {
236 if (hc) {
237 kfree(hc->name);
238 kfree(hc->uuid);
239 kfree(hc);
240 }
241 }
242
243 /*
244 * The kdev_t and uuid of a device can never change once it is
245 * initially inserted.
246 */
dm_hash_insert(const char * name,const char * uuid,struct mapped_device * md)247 static int dm_hash_insert(const char *name, const char *uuid, struct mapped_device *md)
248 {
249 struct hash_cell *cell, *hc;
250
251 /*
252 * Allocate the new cells.
253 */
254 cell = alloc_cell(name, uuid, md);
255 if (!cell)
256 return -ENOMEM;
257
258 /*
259 * Insert the cell into both hash tables.
260 */
261 down_write(&_hash_lock);
262 hc = __get_name_cell(name);
263 if (hc) {
264 dm_put(hc->md);
265 goto bad;
266 }
267
268 __link_name(cell);
269
270 if (uuid) {
271 hc = __get_uuid_cell(uuid);
272 if (hc) {
273 __unlink_name(cell);
274 dm_put(hc->md);
275 goto bad;
276 }
277 __link_uuid(cell);
278 }
279 dm_get(md);
280 mutex_lock(&dm_hash_cells_mutex);
281 dm_set_mdptr(md, cell);
282 mutex_unlock(&dm_hash_cells_mutex);
283 up_write(&_hash_lock);
284
285 return 0;
286
287 bad:
288 up_write(&_hash_lock);
289 free_cell(cell);
290 return -EBUSY;
291 }
292
__hash_remove(struct hash_cell * hc)293 static struct dm_table *__hash_remove(struct hash_cell *hc)
294 {
295 struct dm_table *table;
296 int srcu_idx;
297
298 /* remove from the dev trees */
299 __unlink_name(hc);
300 __unlink_uuid(hc);
301 mutex_lock(&dm_hash_cells_mutex);
302 dm_set_mdptr(hc->md, NULL);
303 mutex_unlock(&dm_hash_cells_mutex);
304
305 table = dm_get_live_table(hc->md, &srcu_idx);
306 if (table)
307 dm_table_event(table);
308 dm_put_live_table(hc->md, srcu_idx);
309
310 table = NULL;
311 if (hc->new_map)
312 table = hc->new_map;
313 dm_put(hc->md);
314 free_cell(hc);
315
316 return table;
317 }
318
dm_hash_remove_all(bool keep_open_devices,bool mark_deferred,bool only_deferred)319 static void dm_hash_remove_all(bool keep_open_devices, bool mark_deferred, bool only_deferred)
320 {
321 int dev_skipped;
322 struct rb_node *n;
323 struct hash_cell *hc;
324 struct mapped_device *md;
325 struct dm_table *t;
326
327 retry:
328 dev_skipped = 0;
329
330 down_write(&_hash_lock);
331
332 for (n = rb_first(&name_rb_tree); n; n = rb_next(n)) {
333 hc = container_of(n, struct hash_cell, name_node);
334 md = hc->md;
335 dm_get(md);
336
337 if (keep_open_devices &&
338 dm_lock_for_deletion(md, mark_deferred, only_deferred)) {
339 dm_put(md);
340 dev_skipped++;
341 continue;
342 }
343
344 t = __hash_remove(hc);
345
346 up_write(&_hash_lock);
347
348 if (t) {
349 dm_sync_table(md);
350 dm_table_destroy(t);
351 }
352 dm_ima_measure_on_device_remove(md, true);
353 dm_put(md);
354 if (likely(keep_open_devices))
355 dm_destroy(md);
356 else
357 dm_destroy_immediate(md);
358
359 /*
360 * Some mapped devices may be using other mapped
361 * devices, so repeat until we make no further
362 * progress. If a new mapped device is created
363 * here it will also get removed.
364 */
365 goto retry;
366 }
367
368 up_write(&_hash_lock);
369
370 if (dev_skipped)
371 DMWARN("remove_all left %d open device(s)", dev_skipped);
372 }
373
374 /*
375 * Set the uuid of a hash_cell that isn't already set.
376 */
__set_cell_uuid(struct hash_cell * hc,char * new_uuid)377 static void __set_cell_uuid(struct hash_cell *hc, char *new_uuid)
378 {
379 mutex_lock(&dm_hash_cells_mutex);
380 hc->uuid = new_uuid;
381 mutex_unlock(&dm_hash_cells_mutex);
382
383 __link_uuid(hc);
384 }
385
386 /*
387 * Changes the name of a hash_cell and returns the old name for
388 * the caller to free.
389 */
__change_cell_name(struct hash_cell * hc,char * new_name)390 static char *__change_cell_name(struct hash_cell *hc, char *new_name)
391 {
392 char *old_name;
393
394 /*
395 * Rename and move the name cell.
396 */
397 __unlink_name(hc);
398 old_name = hc->name;
399
400 mutex_lock(&dm_hash_cells_mutex);
401 hc->name = new_name;
402 mutex_unlock(&dm_hash_cells_mutex);
403
404 __link_name(hc);
405
406 return old_name;
407 }
408
dm_hash_rename(struct dm_ioctl * param,const char * new)409 static struct mapped_device *dm_hash_rename(struct dm_ioctl *param,
410 const char *new)
411 {
412 char *new_data, *old_name = NULL;
413 struct hash_cell *hc;
414 struct dm_table *table;
415 struct mapped_device *md;
416 unsigned int change_uuid = (param->flags & DM_UUID_FLAG) ? 1 : 0;
417 int srcu_idx;
418
419 /*
420 * duplicate new.
421 */
422 new_data = kstrdup(new, GFP_KERNEL);
423 if (!new_data)
424 return ERR_PTR(-ENOMEM);
425
426 down_write(&_hash_lock);
427
428 /*
429 * Is new free ?
430 */
431 if (change_uuid)
432 hc = __get_uuid_cell(new);
433 else
434 hc = __get_name_cell(new);
435
436 if (hc) {
437 DMERR("Unable to change %s on mapped device %s to one that already exists: %s",
438 change_uuid ? "uuid" : "name",
439 param->name, new);
440 dm_put(hc->md);
441 up_write(&_hash_lock);
442 kfree(new_data);
443 return ERR_PTR(-EBUSY);
444 }
445
446 /*
447 * Is there such a device as 'old' ?
448 */
449 hc = __get_name_cell(param->name);
450 if (!hc) {
451 DMERR("Unable to rename non-existent device, %s to %s%s",
452 param->name, change_uuid ? "uuid " : "", new);
453 up_write(&_hash_lock);
454 kfree(new_data);
455 return ERR_PTR(-ENXIO);
456 }
457
458 /*
459 * Does this device already have a uuid?
460 */
461 if (change_uuid && hc->uuid) {
462 DMERR("Unable to change uuid of mapped device %s to %s "
463 "because uuid is already set to %s",
464 param->name, new, hc->uuid);
465 dm_put(hc->md);
466 up_write(&_hash_lock);
467 kfree(new_data);
468 return ERR_PTR(-EINVAL);
469 }
470
471 if (change_uuid)
472 __set_cell_uuid(hc, new_data);
473 else
474 old_name = __change_cell_name(hc, new_data);
475
476 /*
477 * Wake up any dm event waiters.
478 */
479 table = dm_get_live_table(hc->md, &srcu_idx);
480 if (table)
481 dm_table_event(table);
482 dm_put_live_table(hc->md, srcu_idx);
483
484 if (!dm_kobject_uevent(hc->md, KOBJ_CHANGE, param->event_nr, false))
485 param->flags |= DM_UEVENT_GENERATED_FLAG;
486
487 md = hc->md;
488
489 dm_ima_measure_on_device_rename(md);
490
491 up_write(&_hash_lock);
492 kfree(old_name);
493
494 return md;
495 }
496
dm_deferred_remove(void)497 void dm_deferred_remove(void)
498 {
499 dm_hash_remove_all(true, false, true);
500 }
501
502 /*-----------------------------------------------------------------
503 * Implementation of the ioctl commands
504 *---------------------------------------------------------------*/
505 /*
506 * All the ioctl commands get dispatched to functions with this
507 * prototype.
508 */
509 typedef int (*ioctl_fn)(struct file *filp, struct dm_ioctl *param, size_t param_size);
510
remove_all(struct file * filp,struct dm_ioctl * param,size_t param_size)511 static int remove_all(struct file *filp, struct dm_ioctl *param, size_t param_size)
512 {
513 dm_hash_remove_all(true, !!(param->flags & DM_DEFERRED_REMOVE), false);
514 param->data_size = 0;
515 return 0;
516 }
517
518 /*
519 * Round up the ptr to an 8-byte boundary.
520 */
521 #define ALIGN_MASK 7
align_val(size_t val)522 static inline size_t align_val(size_t val)
523 {
524 return (val + ALIGN_MASK) & ~ALIGN_MASK;
525 }
align_ptr(void * ptr)526 static inline void *align_ptr(void *ptr)
527 {
528 return (void *)align_val((size_t)ptr);
529 }
530
531 /*
532 * Retrieves the data payload buffer from an already allocated
533 * struct dm_ioctl.
534 */
get_result_buffer(struct dm_ioctl * param,size_t param_size,size_t * len)535 static void *get_result_buffer(struct dm_ioctl *param, size_t param_size,
536 size_t *len)
537 {
538 param->data_start = align_ptr(param + 1) - (void *) param;
539
540 if (param->data_start < param_size)
541 *len = param_size - param->data_start;
542 else
543 *len = 0;
544
545 return ((void *) param) + param->data_start;
546 }
547
filter_device(struct hash_cell * hc,const char * pfx_name,const char * pfx_uuid)548 static bool filter_device(struct hash_cell *hc, const char *pfx_name, const char *pfx_uuid)
549 {
550 const char *val;
551 size_t val_len, pfx_len;
552
553 val = hc->name;
554 val_len = strlen(val);
555 pfx_len = strnlen(pfx_name, DM_NAME_LEN);
556 if (pfx_len > val_len)
557 return false;
558 if (memcmp(val, pfx_name, pfx_len))
559 return false;
560
561 val = hc->uuid ? hc->uuid : "";
562 val_len = strlen(val);
563 pfx_len = strnlen(pfx_uuid, DM_UUID_LEN);
564 if (pfx_len > val_len)
565 return false;
566 if (memcmp(val, pfx_uuid, pfx_len))
567 return false;
568
569 return true;
570 }
571
list_devices(struct file * filp,struct dm_ioctl * param,size_t param_size)572 static int list_devices(struct file *filp, struct dm_ioctl *param, size_t param_size)
573 {
574 struct rb_node *n;
575 struct hash_cell *hc;
576 size_t len, needed = 0;
577 struct gendisk *disk;
578 struct dm_name_list *orig_nl, *nl, *old_nl = NULL;
579 uint32_t *event_nr;
580
581 down_write(&_hash_lock);
582
583 /*
584 * Loop through all the devices working out how much
585 * space we need.
586 */
587 for (n = rb_first(&name_rb_tree); n; n = rb_next(n)) {
588 hc = container_of(n, struct hash_cell, name_node);
589 if (!filter_device(hc, param->name, param->uuid))
590 continue;
591 needed += align_val(offsetof(struct dm_name_list, name) + strlen(hc->name) + 1);
592 needed += align_val(sizeof(uint32_t) * 2);
593 if (param->flags & DM_UUID_FLAG && hc->uuid)
594 needed += align_val(strlen(hc->uuid) + 1);
595 }
596
597 /*
598 * Grab our output buffer.
599 */
600 nl = orig_nl = get_result_buffer(param, param_size, &len);
601 if (len < needed || len < sizeof(nl->dev)) {
602 param->flags |= DM_BUFFER_FULL_FLAG;
603 goto out;
604 }
605 param->data_size = param->data_start + needed;
606
607 nl->dev = 0; /* Flags no data */
608
609 /*
610 * Now loop through filling out the names.
611 */
612 for (n = rb_first(&name_rb_tree); n; n = rb_next(n)) {
613 void *uuid_ptr;
614 hc = container_of(n, struct hash_cell, name_node);
615 if (!filter_device(hc, param->name, param->uuid))
616 continue;
617 if (old_nl)
618 old_nl->next = (uint32_t) ((void *) nl -
619 (void *) old_nl);
620 disk = dm_disk(hc->md);
621 nl->dev = huge_encode_dev(disk_devt(disk));
622 nl->next = 0;
623 strcpy(nl->name, hc->name);
624
625 old_nl = nl;
626 event_nr = align_ptr(nl->name + strlen(hc->name) + 1);
627 event_nr[0] = dm_get_event_nr(hc->md);
628 event_nr[1] = 0;
629 uuid_ptr = align_ptr(event_nr + 2);
630 if (param->flags & DM_UUID_FLAG) {
631 if (hc->uuid) {
632 event_nr[1] |= DM_NAME_LIST_FLAG_HAS_UUID;
633 strcpy(uuid_ptr, hc->uuid);
634 uuid_ptr = align_ptr(uuid_ptr + strlen(hc->uuid) + 1);
635 } else {
636 event_nr[1] |= DM_NAME_LIST_FLAG_DOESNT_HAVE_UUID;
637 }
638 }
639 nl = uuid_ptr;
640 }
641 /*
642 * If mismatch happens, security may be compromised due to buffer
643 * overflow, so it's better to crash.
644 */
645 BUG_ON((char *)nl - (char *)orig_nl != needed);
646
647 out:
648 up_write(&_hash_lock);
649 return 0;
650 }
651
list_version_get_needed(struct target_type * tt,void * needed_param)652 static void list_version_get_needed(struct target_type *tt, void *needed_param)
653 {
654 size_t *needed = needed_param;
655
656 *needed += sizeof(struct dm_target_versions);
657 *needed += strlen(tt->name) + 1;
658 *needed += ALIGN_MASK;
659 }
660
list_version_get_info(struct target_type * tt,void * param)661 static void list_version_get_info(struct target_type *tt, void *param)
662 {
663 struct vers_iter *info = param;
664
665 /* Check space - it might have changed since the first iteration */
666 if ((char *)info->vers + sizeof(tt->version) + strlen(tt->name) + 1 >
667 info->end) {
668
669 info->flags = DM_BUFFER_FULL_FLAG;
670 return;
671 }
672
673 if (info->old_vers)
674 info->old_vers->next = (uint32_t) ((void *)info->vers -
675 (void *)info->old_vers);
676 info->vers->version[0] = tt->version[0];
677 info->vers->version[1] = tt->version[1];
678 info->vers->version[2] = tt->version[2];
679 info->vers->next = 0;
680 strcpy(info->vers->name, tt->name);
681
682 info->old_vers = info->vers;
683 info->vers = align_ptr(((void *) ++info->vers) + strlen(tt->name) + 1);
684 }
685
__list_versions(struct dm_ioctl * param,size_t param_size,const char * name)686 static int __list_versions(struct dm_ioctl *param, size_t param_size, const char *name)
687 {
688 size_t len, needed = 0;
689 struct dm_target_versions *vers;
690 struct vers_iter iter_info;
691 struct target_type *tt = NULL;
692
693 if (name) {
694 tt = dm_get_target_type(name);
695 if (!tt)
696 return -EINVAL;
697 }
698
699 /*
700 * Loop through all the devices working out how much
701 * space we need.
702 */
703 if (!tt)
704 dm_target_iterate(list_version_get_needed, &needed);
705 else
706 list_version_get_needed(tt, &needed);
707
708 /*
709 * Grab our output buffer.
710 */
711 vers = get_result_buffer(param, param_size, &len);
712 if (len < needed) {
713 param->flags |= DM_BUFFER_FULL_FLAG;
714 goto out;
715 }
716 param->data_size = param->data_start + needed;
717
718 iter_info.param_size = param_size;
719 iter_info.old_vers = NULL;
720 iter_info.vers = vers;
721 iter_info.flags = 0;
722 iter_info.end = (char *)vers + needed;
723
724 /*
725 * Now loop through filling out the names & versions.
726 */
727 if (!tt)
728 dm_target_iterate(list_version_get_info, &iter_info);
729 else
730 list_version_get_info(tt, &iter_info);
731 param->flags |= iter_info.flags;
732
733 out:
734 if (tt)
735 dm_put_target_type(tt);
736 return 0;
737 }
738
list_versions(struct file * filp,struct dm_ioctl * param,size_t param_size)739 static int list_versions(struct file *filp, struct dm_ioctl *param, size_t param_size)
740 {
741 return __list_versions(param, param_size, NULL);
742 }
743
get_target_version(struct file * filp,struct dm_ioctl * param,size_t param_size)744 static int get_target_version(struct file *filp, struct dm_ioctl *param, size_t param_size)
745 {
746 return __list_versions(param, param_size, param->name);
747 }
748
check_name(const char * name)749 static int check_name(const char *name)
750 {
751 if (strchr(name, '/')) {
752 DMERR("invalid device name");
753 return -EINVAL;
754 }
755
756 return 0;
757 }
758
759 /*
760 * On successful return, the caller must not attempt to acquire
761 * _hash_lock without first calling dm_put_live_table, because dm_table_destroy
762 * waits for this dm_put_live_table and could be called under this lock.
763 */
dm_get_inactive_table(struct mapped_device * md,int * srcu_idx)764 static struct dm_table *dm_get_inactive_table(struct mapped_device *md, int *srcu_idx)
765 {
766 struct hash_cell *hc;
767 struct dm_table *table = NULL;
768
769 /* increment rcu count, we don't care about the table pointer */
770 dm_get_live_table(md, srcu_idx);
771
772 down_read(&_hash_lock);
773 hc = dm_get_mdptr(md);
774 if (!hc || hc->md != md) {
775 DMERR("device has been removed from the dev hash table.");
776 goto out;
777 }
778
779 table = hc->new_map;
780
781 out:
782 up_read(&_hash_lock);
783
784 return table;
785 }
786
dm_get_live_or_inactive_table(struct mapped_device * md,struct dm_ioctl * param,int * srcu_idx)787 static struct dm_table *dm_get_live_or_inactive_table(struct mapped_device *md,
788 struct dm_ioctl *param,
789 int *srcu_idx)
790 {
791 return (param->flags & DM_QUERY_INACTIVE_TABLE_FLAG) ?
792 dm_get_inactive_table(md, srcu_idx) : dm_get_live_table(md, srcu_idx);
793 }
794
795 /*
796 * Fills in a dm_ioctl structure, ready for sending back to
797 * userland.
798 */
__dev_status(struct mapped_device * md,struct dm_ioctl * param)799 static void __dev_status(struct mapped_device *md, struct dm_ioctl *param)
800 {
801 struct gendisk *disk = dm_disk(md);
802 struct dm_table *table;
803 int srcu_idx;
804
805 param->flags &= ~(DM_SUSPEND_FLAG | DM_READONLY_FLAG |
806 DM_ACTIVE_PRESENT_FLAG | DM_INTERNAL_SUSPEND_FLAG);
807
808 if (dm_suspended_md(md))
809 param->flags |= DM_SUSPEND_FLAG;
810
811 if (dm_suspended_internally_md(md))
812 param->flags |= DM_INTERNAL_SUSPEND_FLAG;
813
814 if (dm_test_deferred_remove_flag(md))
815 param->flags |= DM_DEFERRED_REMOVE;
816
817 param->dev = huge_encode_dev(disk_devt(disk));
818
819 /*
820 * Yes, this will be out of date by the time it gets back
821 * to userland, but it is still very useful for
822 * debugging.
823 */
824 param->open_count = dm_open_count(md);
825
826 param->event_nr = dm_get_event_nr(md);
827 param->target_count = 0;
828
829 table = dm_get_live_table(md, &srcu_idx);
830 if (table) {
831 if (!(param->flags & DM_QUERY_INACTIVE_TABLE_FLAG)) {
832 if (get_disk_ro(disk))
833 param->flags |= DM_READONLY_FLAG;
834 param->target_count = table->num_targets;
835 }
836
837 param->flags |= DM_ACTIVE_PRESENT_FLAG;
838 }
839 dm_put_live_table(md, srcu_idx);
840
841 if (param->flags & DM_QUERY_INACTIVE_TABLE_FLAG) {
842 int srcu_idx;
843 table = dm_get_inactive_table(md, &srcu_idx);
844 if (table) {
845 if (!(dm_table_get_mode(table) & FMODE_WRITE))
846 param->flags |= DM_READONLY_FLAG;
847 param->target_count = table->num_targets;
848 }
849 dm_put_live_table(md, srcu_idx);
850 }
851 }
852
dev_create(struct file * filp,struct dm_ioctl * param,size_t param_size)853 static int dev_create(struct file *filp, struct dm_ioctl *param, size_t param_size)
854 {
855 int r, m = DM_ANY_MINOR;
856 struct mapped_device *md;
857
858 r = check_name(param->name);
859 if (r)
860 return r;
861
862 if (param->flags & DM_PERSISTENT_DEV_FLAG)
863 m = MINOR(huge_decode_dev(param->dev));
864
865 r = dm_create(m, &md);
866 if (r)
867 return r;
868
869 r = dm_hash_insert(param->name, *param->uuid ? param->uuid : NULL, md);
870 if (r) {
871 dm_put(md);
872 dm_destroy(md);
873 return r;
874 }
875
876 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
877
878 __dev_status(md, param);
879
880 dm_put(md);
881
882 return 0;
883 }
884
885 /*
886 * Always use UUID for lookups if it's present, otherwise use name or dev.
887 */
__find_device_hash_cell(struct dm_ioctl * param)888 static struct hash_cell *__find_device_hash_cell(struct dm_ioctl *param)
889 {
890 struct hash_cell *hc = NULL;
891
892 if (*param->uuid) {
893 if (*param->name || param->dev) {
894 DMERR("Invalid ioctl structure: uuid %s, name %s, dev %llx",
895 param->uuid, param->name, (unsigned long long)param->dev);
896 return NULL;
897 }
898
899 hc = __get_uuid_cell(param->uuid);
900 if (!hc)
901 return NULL;
902 } else if (*param->name) {
903 if (param->dev) {
904 DMERR("Invalid ioctl structure: name %s, dev %llx",
905 param->name, (unsigned long long)param->dev);
906 return NULL;
907 }
908
909 hc = __get_name_cell(param->name);
910 if (!hc)
911 return NULL;
912 } else if (param->dev) {
913 hc = __get_dev_cell(param->dev);
914 if (!hc)
915 return NULL;
916 } else
917 return NULL;
918
919 /*
920 * Sneakily write in both the name and the uuid
921 * while we have the cell.
922 */
923 strlcpy(param->name, hc->name, sizeof(param->name));
924 if (hc->uuid)
925 strlcpy(param->uuid, hc->uuid, sizeof(param->uuid));
926 else
927 param->uuid[0] = '\0';
928
929 if (hc->new_map)
930 param->flags |= DM_INACTIVE_PRESENT_FLAG;
931 else
932 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
933
934 return hc;
935 }
936
find_device(struct dm_ioctl * param)937 static struct mapped_device *find_device(struct dm_ioctl *param)
938 {
939 struct hash_cell *hc;
940 struct mapped_device *md = NULL;
941
942 down_read(&_hash_lock);
943 hc = __find_device_hash_cell(param);
944 if (hc)
945 md = hc->md;
946 up_read(&_hash_lock);
947
948 return md;
949 }
950
dev_remove(struct file * filp,struct dm_ioctl * param,size_t param_size)951 static int dev_remove(struct file *filp, struct dm_ioctl *param, size_t param_size)
952 {
953 struct hash_cell *hc;
954 struct mapped_device *md;
955 int r;
956 struct dm_table *t;
957
958 down_write(&_hash_lock);
959 hc = __find_device_hash_cell(param);
960
961 if (!hc) {
962 DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
963 up_write(&_hash_lock);
964 return -ENXIO;
965 }
966
967 md = hc->md;
968
969 /*
970 * Ensure the device is not open and nothing further can open it.
971 */
972 r = dm_lock_for_deletion(md, !!(param->flags & DM_DEFERRED_REMOVE), false);
973 if (r) {
974 if (r == -EBUSY && param->flags & DM_DEFERRED_REMOVE) {
975 up_write(&_hash_lock);
976 dm_put(md);
977 return 0;
978 }
979 DMDEBUG_LIMIT("unable to remove open device %s", hc->name);
980 up_write(&_hash_lock);
981 dm_put(md);
982 return r;
983 }
984
985 t = __hash_remove(hc);
986 up_write(&_hash_lock);
987
988 if (t) {
989 dm_sync_table(md);
990 dm_table_destroy(t);
991 }
992
993 param->flags &= ~DM_DEFERRED_REMOVE;
994
995 dm_ima_measure_on_device_remove(md, false);
996
997 if (!dm_kobject_uevent(md, KOBJ_REMOVE, param->event_nr, false))
998 param->flags |= DM_UEVENT_GENERATED_FLAG;
999
1000 dm_put(md);
1001 dm_destroy(md);
1002 return 0;
1003 }
1004
1005 /*
1006 * Check a string doesn't overrun the chunk of
1007 * memory we copied from userland.
1008 */
invalid_str(char * str,void * end)1009 static int invalid_str(char *str, void *end)
1010 {
1011 while ((void *) str < end)
1012 if (!*str++)
1013 return 0;
1014
1015 return -EINVAL;
1016 }
1017
dev_rename(struct file * filp,struct dm_ioctl * param,size_t param_size)1018 static int dev_rename(struct file *filp, struct dm_ioctl *param, size_t param_size)
1019 {
1020 int r;
1021 char *new_data = (char *) param + param->data_start;
1022 struct mapped_device *md;
1023 unsigned int change_uuid = (param->flags & DM_UUID_FLAG) ? 1 : 0;
1024
1025 if (new_data < param->data ||
1026 invalid_str(new_data, (void *) param + param_size) || !*new_data ||
1027 strlen(new_data) > (change_uuid ? DM_UUID_LEN - 1 : DM_NAME_LEN - 1)) {
1028 DMERR("Invalid new mapped device name or uuid string supplied.");
1029 return -EINVAL;
1030 }
1031
1032 if (!change_uuid) {
1033 r = check_name(new_data);
1034 if (r)
1035 return r;
1036 }
1037
1038 md = dm_hash_rename(param, new_data);
1039 if (IS_ERR(md))
1040 return PTR_ERR(md);
1041
1042 __dev_status(md, param);
1043 dm_put(md);
1044
1045 return 0;
1046 }
1047
dev_set_geometry(struct file * filp,struct dm_ioctl * param,size_t param_size)1048 static int dev_set_geometry(struct file *filp, struct dm_ioctl *param, size_t param_size)
1049 {
1050 int r = -EINVAL, x;
1051 struct mapped_device *md;
1052 struct hd_geometry geometry;
1053 unsigned long indata[4];
1054 char *geostr = (char *) param + param->data_start;
1055 char dummy;
1056
1057 md = find_device(param);
1058 if (!md)
1059 return -ENXIO;
1060
1061 if (geostr < param->data ||
1062 invalid_str(geostr, (void *) param + param_size)) {
1063 DMERR("Invalid geometry supplied.");
1064 goto out;
1065 }
1066
1067 x = sscanf(geostr, "%lu %lu %lu %lu%c", indata,
1068 indata + 1, indata + 2, indata + 3, &dummy);
1069
1070 if (x != 4) {
1071 DMERR("Unable to interpret geometry settings.");
1072 goto out;
1073 }
1074
1075 if (indata[0] > 65535 || indata[1] > 255 ||
1076 indata[2] > 255 || indata[3] > ULONG_MAX) {
1077 DMERR("Geometry exceeds range limits.");
1078 goto out;
1079 }
1080
1081 geometry.cylinders = indata[0];
1082 geometry.heads = indata[1];
1083 geometry.sectors = indata[2];
1084 geometry.start = indata[3];
1085
1086 r = dm_set_geometry(md, &geometry);
1087
1088 param->data_size = 0;
1089
1090 out:
1091 dm_put(md);
1092 return r;
1093 }
1094
do_suspend(struct dm_ioctl * param)1095 static int do_suspend(struct dm_ioctl *param)
1096 {
1097 int r = 0;
1098 unsigned int suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
1099 struct mapped_device *md;
1100
1101 md = find_device(param);
1102 if (!md)
1103 return -ENXIO;
1104
1105 if (param->flags & DM_SKIP_LOCKFS_FLAG)
1106 suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
1107 if (param->flags & DM_NOFLUSH_FLAG)
1108 suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
1109
1110 if (!dm_suspended_md(md)) {
1111 r = dm_suspend(md, suspend_flags);
1112 if (r)
1113 goto out;
1114 }
1115
1116 __dev_status(md, param);
1117
1118 out:
1119 dm_put(md);
1120
1121 return r;
1122 }
1123
do_resume(struct dm_ioctl * param)1124 static int do_resume(struct dm_ioctl *param)
1125 {
1126 int r = 0;
1127 unsigned int suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
1128 struct hash_cell *hc;
1129 struct mapped_device *md;
1130 struct dm_table *new_map, *old_map = NULL;
1131 bool need_resize_uevent = false;
1132
1133 down_write(&_hash_lock);
1134
1135 hc = __find_device_hash_cell(param);
1136 if (!hc) {
1137 DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
1138 up_write(&_hash_lock);
1139 return -ENXIO;
1140 }
1141
1142 md = hc->md;
1143
1144 new_map = hc->new_map;
1145 hc->new_map = NULL;
1146 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
1147
1148 up_write(&_hash_lock);
1149
1150 /* Do we need to load a new map ? */
1151 if (new_map) {
1152 sector_t old_size, new_size;
1153
1154 /* Suspend if it isn't already suspended */
1155 if (param->flags & DM_SKIP_LOCKFS_FLAG)
1156 suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
1157 if (param->flags & DM_NOFLUSH_FLAG)
1158 suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
1159 if (!dm_suspended_md(md))
1160 dm_suspend(md, suspend_flags);
1161
1162 old_size = dm_get_size(md);
1163 old_map = dm_swap_table(md, new_map);
1164 if (IS_ERR(old_map)) {
1165 dm_sync_table(md);
1166 dm_table_destroy(new_map);
1167 dm_put(md);
1168 return PTR_ERR(old_map);
1169 }
1170 new_size = dm_get_size(md);
1171 if (old_size && new_size && old_size != new_size)
1172 need_resize_uevent = true;
1173
1174 if (dm_table_get_mode(new_map) & FMODE_WRITE)
1175 set_disk_ro(dm_disk(md), 0);
1176 else
1177 set_disk_ro(dm_disk(md), 1);
1178 }
1179
1180 if (dm_suspended_md(md)) {
1181 r = dm_resume(md);
1182 if (!r) {
1183 dm_ima_measure_on_device_resume(md, new_map ? true : false);
1184
1185 if (!dm_kobject_uevent(md, KOBJ_CHANGE, param->event_nr, need_resize_uevent))
1186 param->flags |= DM_UEVENT_GENERATED_FLAG;
1187 }
1188 }
1189
1190 /*
1191 * Since dm_swap_table synchronizes RCU, nobody should be in
1192 * read-side critical section already.
1193 */
1194 if (old_map)
1195 dm_table_destroy(old_map);
1196
1197 if (!r)
1198 __dev_status(md, param);
1199
1200 dm_put(md);
1201 return r;
1202 }
1203
1204 /*
1205 * Set or unset the suspension state of a device.
1206 * If the device already is in the requested state we just return its status.
1207 */
dev_suspend(struct file * filp,struct dm_ioctl * param,size_t param_size)1208 static int dev_suspend(struct file *filp, struct dm_ioctl *param, size_t param_size)
1209 {
1210 if (param->flags & DM_SUSPEND_FLAG)
1211 return do_suspend(param);
1212
1213 return do_resume(param);
1214 }
1215
1216 /*
1217 * Copies device info back to user space, used by
1218 * the create and info ioctls.
1219 */
dev_status(struct file * filp,struct dm_ioctl * param,size_t param_size)1220 static int dev_status(struct file *filp, struct dm_ioctl *param, size_t param_size)
1221 {
1222 struct mapped_device *md;
1223
1224 md = find_device(param);
1225 if (!md)
1226 return -ENXIO;
1227
1228 __dev_status(md, param);
1229 dm_put(md);
1230
1231 return 0;
1232 }
1233
1234 /*
1235 * Build up the status struct for each target
1236 */
retrieve_status(struct dm_table * table,struct dm_ioctl * param,size_t param_size)1237 static void retrieve_status(struct dm_table *table,
1238 struct dm_ioctl *param, size_t param_size)
1239 {
1240 unsigned int i, num_targets;
1241 struct dm_target_spec *spec;
1242 char *outbuf, *outptr;
1243 status_type_t type;
1244 size_t remaining, len, used = 0;
1245 unsigned int status_flags = 0;
1246
1247 outptr = outbuf = get_result_buffer(param, param_size, &len);
1248
1249 if (param->flags & DM_STATUS_TABLE_FLAG)
1250 type = STATUSTYPE_TABLE;
1251 else if (param->flags & DM_IMA_MEASUREMENT_FLAG)
1252 type = STATUSTYPE_IMA;
1253 else
1254 type = STATUSTYPE_INFO;
1255
1256 /* Get all the target info */
1257 num_targets = table->num_targets;
1258 for (i = 0; i < num_targets; i++) {
1259 struct dm_target *ti = dm_table_get_target(table, i);
1260 size_t l;
1261
1262 remaining = len - (outptr - outbuf);
1263 if (remaining <= sizeof(struct dm_target_spec)) {
1264 param->flags |= DM_BUFFER_FULL_FLAG;
1265 break;
1266 }
1267
1268 spec = (struct dm_target_spec *) outptr;
1269
1270 spec->status = 0;
1271 spec->sector_start = ti->begin;
1272 spec->length = ti->len;
1273 strncpy(spec->target_type, ti->type->name,
1274 sizeof(spec->target_type) - 1);
1275
1276 outptr += sizeof(struct dm_target_spec);
1277 remaining = len - (outptr - outbuf);
1278 if (remaining <= 0) {
1279 param->flags |= DM_BUFFER_FULL_FLAG;
1280 break;
1281 }
1282
1283 /* Get the status/table string from the target driver */
1284 if (ti->type->status) {
1285 if (param->flags & DM_NOFLUSH_FLAG)
1286 status_flags |= DM_STATUS_NOFLUSH_FLAG;
1287 ti->type->status(ti, type, status_flags, outptr, remaining);
1288 } else
1289 outptr[0] = '\0';
1290
1291 l = strlen(outptr) + 1;
1292 if (l == remaining) {
1293 param->flags |= DM_BUFFER_FULL_FLAG;
1294 break;
1295 }
1296
1297 outptr += l;
1298 used = param->data_start + (outptr - outbuf);
1299
1300 outptr = align_ptr(outptr);
1301 spec->next = outptr - outbuf;
1302 }
1303
1304 if (used)
1305 param->data_size = used;
1306
1307 param->target_count = num_targets;
1308 }
1309
1310 /*
1311 * Wait for a device to report an event
1312 */
dev_wait(struct file * filp,struct dm_ioctl * param,size_t param_size)1313 static int dev_wait(struct file *filp, struct dm_ioctl *param, size_t param_size)
1314 {
1315 int r = 0;
1316 struct mapped_device *md;
1317 struct dm_table *table;
1318 int srcu_idx;
1319
1320 md = find_device(param);
1321 if (!md)
1322 return -ENXIO;
1323
1324 /*
1325 * Wait for a notification event
1326 */
1327 if (dm_wait_event(md, param->event_nr)) {
1328 r = -ERESTARTSYS;
1329 goto out;
1330 }
1331
1332 /*
1333 * The userland program is going to want to know what
1334 * changed to trigger the event, so we may as well tell
1335 * him and save an ioctl.
1336 */
1337 __dev_status(md, param);
1338
1339 table = dm_get_live_or_inactive_table(md, param, &srcu_idx);
1340 if (table)
1341 retrieve_status(table, param, param_size);
1342 dm_put_live_table(md, srcu_idx);
1343
1344 out:
1345 dm_put(md);
1346
1347 return r;
1348 }
1349
1350 /*
1351 * Remember the global event number and make it possible to poll
1352 * for further events.
1353 */
dev_arm_poll(struct file * filp,struct dm_ioctl * param,size_t param_size)1354 static int dev_arm_poll(struct file *filp, struct dm_ioctl *param, size_t param_size)
1355 {
1356 struct dm_file *priv = filp->private_data;
1357
1358 priv->global_event_nr = atomic_read(&dm_global_event_nr);
1359
1360 return 0;
1361 }
1362
get_mode(struct dm_ioctl * param)1363 static inline fmode_t get_mode(struct dm_ioctl *param)
1364 {
1365 fmode_t mode = FMODE_READ | FMODE_WRITE;
1366
1367 if (param->flags & DM_READONLY_FLAG)
1368 mode = FMODE_READ;
1369
1370 return mode;
1371 }
1372
next_target(struct dm_target_spec * last,uint32_t next,void * end,struct dm_target_spec ** spec,char ** target_params)1373 static int next_target(struct dm_target_spec *last, uint32_t next, void *end,
1374 struct dm_target_spec **spec, char **target_params)
1375 {
1376 *spec = (struct dm_target_spec *) ((unsigned char *) last + next);
1377 *target_params = (char *) (*spec + 1);
1378
1379 if (*spec < (last + 1))
1380 return -EINVAL;
1381
1382 return invalid_str(*target_params, end);
1383 }
1384
populate_table(struct dm_table * table,struct dm_ioctl * param,size_t param_size)1385 static int populate_table(struct dm_table *table,
1386 struct dm_ioctl *param, size_t param_size)
1387 {
1388 int r;
1389 unsigned int i = 0;
1390 struct dm_target_spec *spec = (struct dm_target_spec *) param;
1391 uint32_t next = param->data_start;
1392 void *end = (void *) param + param_size;
1393 char *target_params;
1394
1395 if (!param->target_count) {
1396 DMERR("populate_table: no targets specified");
1397 return -EINVAL;
1398 }
1399
1400 for (i = 0; i < param->target_count; i++) {
1401
1402 r = next_target(spec, next, end, &spec, &target_params);
1403 if (r) {
1404 DMERR("unable to find target");
1405 return r;
1406 }
1407
1408 r = dm_table_add_target(table, spec->target_type,
1409 (sector_t) spec->sector_start,
1410 (sector_t) spec->length,
1411 target_params);
1412 if (r) {
1413 DMERR("error adding target to table");
1414 return r;
1415 }
1416
1417 next = spec->next;
1418 }
1419
1420 return dm_table_complete(table);
1421 }
1422
is_valid_type(enum dm_queue_mode cur,enum dm_queue_mode new)1423 static bool is_valid_type(enum dm_queue_mode cur, enum dm_queue_mode new)
1424 {
1425 if (cur == new ||
1426 (cur == DM_TYPE_BIO_BASED && new == DM_TYPE_DAX_BIO_BASED))
1427 return true;
1428
1429 return false;
1430 }
1431
table_load(struct file * filp,struct dm_ioctl * param,size_t param_size)1432 static int table_load(struct file *filp, struct dm_ioctl *param, size_t param_size)
1433 {
1434 int r;
1435 struct hash_cell *hc;
1436 struct dm_table *t, *old_map = NULL;
1437 struct mapped_device *md;
1438 struct target_type *immutable_target_type;
1439
1440 md = find_device(param);
1441 if (!md)
1442 return -ENXIO;
1443
1444 r = dm_table_create(&t, get_mode(param), param->target_count, md);
1445 if (r)
1446 goto err;
1447
1448 /* Protect md->type and md->queue against concurrent table loads. */
1449 dm_lock_md_type(md);
1450 r = populate_table(t, param, param_size);
1451 if (r)
1452 goto err_unlock_md_type;
1453
1454 dm_ima_measure_on_table_load(t, STATUSTYPE_IMA);
1455
1456 immutable_target_type = dm_get_immutable_target_type(md);
1457 if (immutable_target_type &&
1458 (immutable_target_type != dm_table_get_immutable_target_type(t)) &&
1459 !dm_table_get_wildcard_target(t)) {
1460 DMERR("can't replace immutable target type %s",
1461 immutable_target_type->name);
1462 r = -EINVAL;
1463 goto err_unlock_md_type;
1464 }
1465
1466 if (dm_get_md_type(md) == DM_TYPE_NONE) {
1467 /* setup md->queue to reflect md's type (may block) */
1468 r = dm_setup_md_queue(md, t);
1469 if (r) {
1470 DMERR("unable to set up device queue for new table.");
1471 goto err_unlock_md_type;
1472 }
1473 } else if (!is_valid_type(dm_get_md_type(md), dm_table_get_type(t))) {
1474 DMERR("can't change device type (old=%u vs new=%u) after initial table load.",
1475 dm_get_md_type(md), dm_table_get_type(t));
1476 r = -EINVAL;
1477 goto err_unlock_md_type;
1478 }
1479
1480 dm_unlock_md_type(md);
1481
1482 /* stage inactive table */
1483 down_write(&_hash_lock);
1484 hc = dm_get_mdptr(md);
1485 if (!hc || hc->md != md) {
1486 DMERR("device has been removed from the dev hash table.");
1487 up_write(&_hash_lock);
1488 r = -ENXIO;
1489 goto err_destroy_table;
1490 }
1491
1492 if (hc->new_map)
1493 old_map = hc->new_map;
1494 hc->new_map = t;
1495 up_write(&_hash_lock);
1496
1497 param->flags |= DM_INACTIVE_PRESENT_FLAG;
1498 __dev_status(md, param);
1499
1500 if (old_map) {
1501 dm_sync_table(md);
1502 dm_table_destroy(old_map);
1503 }
1504
1505 dm_put(md);
1506
1507 return 0;
1508
1509 err_unlock_md_type:
1510 dm_unlock_md_type(md);
1511 err_destroy_table:
1512 dm_table_destroy(t);
1513 err:
1514 dm_put(md);
1515
1516 return r;
1517 }
1518
table_clear(struct file * filp,struct dm_ioctl * param,size_t param_size)1519 static int table_clear(struct file *filp, struct dm_ioctl *param, size_t param_size)
1520 {
1521 struct hash_cell *hc;
1522 struct mapped_device *md;
1523 struct dm_table *old_map = NULL;
1524 bool has_new_map = false;
1525
1526 down_write(&_hash_lock);
1527
1528 hc = __find_device_hash_cell(param);
1529 if (!hc) {
1530 DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
1531 up_write(&_hash_lock);
1532 return -ENXIO;
1533 }
1534
1535 if (hc->new_map) {
1536 old_map = hc->new_map;
1537 hc->new_map = NULL;
1538 has_new_map = true;
1539 }
1540
1541 md = hc->md;
1542 up_write(&_hash_lock);
1543
1544 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
1545 __dev_status(md, param);
1546
1547 if (old_map) {
1548 dm_sync_table(md);
1549 dm_table_destroy(old_map);
1550 }
1551 dm_ima_measure_on_table_clear(md, has_new_map);
1552 dm_put(md);
1553
1554 return 0;
1555 }
1556
1557 /*
1558 * Retrieves a list of devices used by a particular dm device.
1559 */
retrieve_deps(struct dm_table * table,struct dm_ioctl * param,size_t param_size)1560 static void retrieve_deps(struct dm_table *table,
1561 struct dm_ioctl *param, size_t param_size)
1562 {
1563 unsigned int count = 0;
1564 struct list_head *tmp;
1565 size_t len, needed;
1566 struct dm_dev_internal *dd;
1567 struct dm_target_deps *deps;
1568
1569 deps = get_result_buffer(param, param_size, &len);
1570
1571 /*
1572 * Count the devices.
1573 */
1574 list_for_each(tmp, dm_table_get_devices(table))
1575 count++;
1576
1577 /*
1578 * Check we have enough space.
1579 */
1580 needed = struct_size(deps, dev, count);
1581 if (len < needed) {
1582 param->flags |= DM_BUFFER_FULL_FLAG;
1583 return;
1584 }
1585
1586 /*
1587 * Fill in the devices.
1588 */
1589 deps->count = count;
1590 count = 0;
1591 list_for_each_entry(dd, dm_table_get_devices(table), list)
1592 deps->dev[count++] = huge_encode_dev(dd->dm_dev->bdev->bd_dev);
1593
1594 param->data_size = param->data_start + needed;
1595 }
1596
table_deps(struct file * filp,struct dm_ioctl * param,size_t param_size)1597 static int table_deps(struct file *filp, struct dm_ioctl *param, size_t param_size)
1598 {
1599 struct mapped_device *md;
1600 struct dm_table *table;
1601 int srcu_idx;
1602
1603 md = find_device(param);
1604 if (!md)
1605 return -ENXIO;
1606
1607 __dev_status(md, param);
1608
1609 table = dm_get_live_or_inactive_table(md, param, &srcu_idx);
1610 if (table)
1611 retrieve_deps(table, param, param_size);
1612 dm_put_live_table(md, srcu_idx);
1613
1614 dm_put(md);
1615
1616 return 0;
1617 }
1618
1619 /*
1620 * Return the status of a device as a text string for each
1621 * target.
1622 */
table_status(struct file * filp,struct dm_ioctl * param,size_t param_size)1623 static int table_status(struct file *filp, struct dm_ioctl *param, size_t param_size)
1624 {
1625 struct mapped_device *md;
1626 struct dm_table *table;
1627 int srcu_idx;
1628
1629 md = find_device(param);
1630 if (!md)
1631 return -ENXIO;
1632
1633 __dev_status(md, param);
1634
1635 table = dm_get_live_or_inactive_table(md, param, &srcu_idx);
1636 if (table)
1637 retrieve_status(table, param, param_size);
1638 dm_put_live_table(md, srcu_idx);
1639
1640 dm_put(md);
1641
1642 return 0;
1643 }
1644
1645 /*
1646 * Process device-mapper dependent messages. Messages prefixed with '@'
1647 * are processed by the DM core. All others are delivered to the target.
1648 * Returns a number <= 1 if message was processed by device mapper.
1649 * Returns 2 if message should be delivered to the target.
1650 */
message_for_md(struct mapped_device * md,unsigned int argc,char ** argv,char * result,unsigned int maxlen)1651 static int message_for_md(struct mapped_device *md, unsigned int argc, char **argv,
1652 char *result, unsigned int maxlen)
1653 {
1654 int r;
1655
1656 if (**argv != '@')
1657 return 2; /* no '@' prefix, deliver to target */
1658
1659 if (!strcasecmp(argv[0], "@cancel_deferred_remove")) {
1660 if (argc != 1) {
1661 DMERR("Invalid arguments for @cancel_deferred_remove");
1662 return -EINVAL;
1663 }
1664 return dm_cancel_deferred_remove(md);
1665 }
1666
1667 r = dm_stats_message(md, argc, argv, result, maxlen);
1668 if (r < 2)
1669 return r;
1670
1671 DMERR("Unsupported message sent to DM core: %s", argv[0]);
1672 return -EINVAL;
1673 }
1674
1675 /*
1676 * Pass a message to the target that's at the supplied device offset.
1677 */
target_message(struct file * filp,struct dm_ioctl * param,size_t param_size)1678 static int target_message(struct file *filp, struct dm_ioctl *param, size_t param_size)
1679 {
1680 int r, argc;
1681 char **argv;
1682 struct mapped_device *md;
1683 struct dm_table *table;
1684 struct dm_target *ti;
1685 struct dm_target_msg *tmsg = (void *) param + param->data_start;
1686 size_t maxlen;
1687 char *result = get_result_buffer(param, param_size, &maxlen);
1688 int srcu_idx;
1689
1690 md = find_device(param);
1691 if (!md)
1692 return -ENXIO;
1693
1694 if (tmsg < (struct dm_target_msg *) param->data ||
1695 invalid_str(tmsg->message, (void *) param + param_size)) {
1696 DMERR("Invalid target message parameters.");
1697 r = -EINVAL;
1698 goto out;
1699 }
1700
1701 r = dm_split_args(&argc, &argv, tmsg->message);
1702 if (r) {
1703 DMERR("Failed to split target message parameters");
1704 goto out;
1705 }
1706
1707 if (!argc) {
1708 DMERR("Empty message received.");
1709 r = -EINVAL;
1710 goto out_argv;
1711 }
1712
1713 r = message_for_md(md, argc, argv, result, maxlen);
1714 if (r <= 1)
1715 goto out_argv;
1716
1717 table = dm_get_live_table(md, &srcu_idx);
1718 if (!table)
1719 goto out_table;
1720
1721 if (dm_deleting_md(md)) {
1722 r = -ENXIO;
1723 goto out_table;
1724 }
1725
1726 ti = dm_table_find_target(table, tmsg->sector);
1727 if (!ti) {
1728 DMERR("Target message sector outside device.");
1729 r = -EINVAL;
1730 } else if (ti->type->message)
1731 r = ti->type->message(ti, argc, argv, result, maxlen);
1732 else {
1733 DMERR("Target type does not support messages");
1734 r = -EINVAL;
1735 }
1736
1737 out_table:
1738 dm_put_live_table(md, srcu_idx);
1739 out_argv:
1740 kfree(argv);
1741 out:
1742 if (r >= 0)
1743 __dev_status(md, param);
1744
1745 if (r == 1) {
1746 param->flags |= DM_DATA_OUT_FLAG;
1747 if (dm_message_test_buffer_overflow(result, maxlen))
1748 param->flags |= DM_BUFFER_FULL_FLAG;
1749 else
1750 param->data_size = param->data_start + strlen(result) + 1;
1751 r = 0;
1752 }
1753
1754 dm_put(md);
1755 return r;
1756 }
1757
1758 /*
1759 * The ioctl parameter block consists of two parts, a dm_ioctl struct
1760 * followed by a data buffer. This flag is set if the second part,
1761 * which has a variable size, is not used by the function processing
1762 * the ioctl.
1763 */
1764 #define IOCTL_FLAGS_NO_PARAMS 1
1765 #define IOCTL_FLAGS_ISSUE_GLOBAL_EVENT 2
1766
1767 /*-----------------------------------------------------------------
1768 * Implementation of open/close/ioctl on the special char
1769 * device.
1770 *---------------------------------------------------------------*/
lookup_ioctl(unsigned int cmd,int * ioctl_flags)1771 static ioctl_fn lookup_ioctl(unsigned int cmd, int *ioctl_flags)
1772 {
1773 static const struct {
1774 int cmd;
1775 int flags;
1776 ioctl_fn fn;
1777 } _ioctls[] = {
1778 {DM_VERSION_CMD, 0, NULL}, /* version is dealt with elsewhere */
1779 {DM_REMOVE_ALL_CMD, IOCTL_FLAGS_NO_PARAMS | IOCTL_FLAGS_ISSUE_GLOBAL_EVENT, remove_all},
1780 {DM_LIST_DEVICES_CMD, 0, list_devices},
1781
1782 {DM_DEV_CREATE_CMD, IOCTL_FLAGS_NO_PARAMS | IOCTL_FLAGS_ISSUE_GLOBAL_EVENT, dev_create},
1783 {DM_DEV_REMOVE_CMD, IOCTL_FLAGS_NO_PARAMS | IOCTL_FLAGS_ISSUE_GLOBAL_EVENT, dev_remove},
1784 {DM_DEV_RENAME_CMD, IOCTL_FLAGS_ISSUE_GLOBAL_EVENT, dev_rename},
1785 {DM_DEV_SUSPEND_CMD, IOCTL_FLAGS_NO_PARAMS, dev_suspend},
1786 {DM_DEV_STATUS_CMD, IOCTL_FLAGS_NO_PARAMS, dev_status},
1787 {DM_DEV_WAIT_CMD, 0, dev_wait},
1788
1789 {DM_TABLE_LOAD_CMD, 0, table_load},
1790 {DM_TABLE_CLEAR_CMD, IOCTL_FLAGS_NO_PARAMS, table_clear},
1791 {DM_TABLE_DEPS_CMD, 0, table_deps},
1792 {DM_TABLE_STATUS_CMD, 0, table_status},
1793
1794 {DM_LIST_VERSIONS_CMD, 0, list_versions},
1795
1796 {DM_TARGET_MSG_CMD, 0, target_message},
1797 {DM_DEV_SET_GEOMETRY_CMD, 0, dev_set_geometry},
1798 {DM_DEV_ARM_POLL, IOCTL_FLAGS_NO_PARAMS, dev_arm_poll},
1799 {DM_GET_TARGET_VERSION, 0, get_target_version},
1800 };
1801
1802 if (unlikely(cmd >= ARRAY_SIZE(_ioctls)))
1803 return NULL;
1804
1805 cmd = array_index_nospec(cmd, ARRAY_SIZE(_ioctls));
1806 *ioctl_flags = _ioctls[cmd].flags;
1807 return _ioctls[cmd].fn;
1808 }
1809
1810 /*
1811 * As well as checking the version compatibility this always
1812 * copies the kernel interface version out.
1813 */
check_version(unsigned int cmd,struct dm_ioctl __user * user,struct dm_ioctl * kernel_params)1814 static int check_version(unsigned int cmd, struct dm_ioctl __user *user,
1815 struct dm_ioctl *kernel_params)
1816 {
1817 int r = 0;
1818
1819 /* Make certain version is first member of dm_ioctl struct */
1820 BUILD_BUG_ON(offsetof(struct dm_ioctl, version) != 0);
1821
1822 if (copy_from_user(kernel_params->version, user->version, sizeof(kernel_params->version)))
1823 return -EFAULT;
1824
1825 if ((kernel_params->version[0] != DM_VERSION_MAJOR) ||
1826 (kernel_params->version[1] > DM_VERSION_MINOR)) {
1827 DMERR("ioctl interface mismatch: kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
1828 DM_VERSION_MAJOR, DM_VERSION_MINOR,
1829 DM_VERSION_PATCHLEVEL,
1830 kernel_params->version[0],
1831 kernel_params->version[1],
1832 kernel_params->version[2],
1833 cmd);
1834 r = -EINVAL;
1835 }
1836
1837 /*
1838 * Fill in the kernel version.
1839 */
1840 kernel_params->version[0] = DM_VERSION_MAJOR;
1841 kernel_params->version[1] = DM_VERSION_MINOR;
1842 kernel_params->version[2] = DM_VERSION_PATCHLEVEL;
1843 if (copy_to_user(user->version, kernel_params->version, sizeof(kernel_params->version)))
1844 return -EFAULT;
1845
1846 return r;
1847 }
1848
1849 #define DM_PARAMS_MALLOC 0x0001 /* Params allocated with kvmalloc() */
1850 #define DM_WIPE_BUFFER 0x0010 /* Wipe input buffer before returning from ioctl */
1851
free_params(struct dm_ioctl * param,size_t param_size,int param_flags)1852 static void free_params(struct dm_ioctl *param, size_t param_size, int param_flags)
1853 {
1854 if (param_flags & DM_WIPE_BUFFER)
1855 memset(param, 0, param_size);
1856
1857 if (param_flags & DM_PARAMS_MALLOC)
1858 kvfree(param);
1859 }
1860
copy_params(struct dm_ioctl __user * user,struct dm_ioctl * param_kernel,int ioctl_flags,struct dm_ioctl ** param,int * param_flags)1861 static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl *param_kernel,
1862 int ioctl_flags, struct dm_ioctl **param, int *param_flags)
1863 {
1864 struct dm_ioctl *dmi;
1865 int secure_data;
1866 const size_t minimum_data_size = offsetof(struct dm_ioctl, data);
1867 unsigned int noio_flag;
1868
1869 /* check_version() already copied version from userspace, avoid TOCTOU */
1870 if (copy_from_user((char *)param_kernel + sizeof(param_kernel->version),
1871 (char __user *)user + sizeof(param_kernel->version),
1872 minimum_data_size - sizeof(param_kernel->version)))
1873 return -EFAULT;
1874
1875 if (unlikely(param_kernel->data_size < minimum_data_size) ||
1876 unlikely(param_kernel->data_size > DM_MAX_TARGETS * DM_MAX_TARGET_PARAMS)) {
1877 DMERR("Invalid data size in the ioctl structure: %u",
1878 param_kernel->data_size);
1879 return -EINVAL;
1880 }
1881
1882 secure_data = param_kernel->flags & DM_SECURE_DATA_FLAG;
1883
1884 *param_flags = secure_data ? DM_WIPE_BUFFER : 0;
1885
1886 if (ioctl_flags & IOCTL_FLAGS_NO_PARAMS) {
1887 dmi = param_kernel;
1888 dmi->data_size = minimum_data_size;
1889 goto data_copied;
1890 }
1891
1892 /*
1893 * Use __GFP_HIGH to avoid low memory issues when a device is
1894 * suspended and the ioctl is needed to resume it.
1895 * Use kmalloc() rather than vmalloc() when we can.
1896 */
1897 dmi = NULL;
1898 noio_flag = memalloc_noio_save();
1899 dmi = kvmalloc(param_kernel->data_size, GFP_KERNEL | __GFP_HIGH);
1900 memalloc_noio_restore(noio_flag);
1901
1902 if (!dmi) {
1903 if (secure_data && clear_user(user, param_kernel->data_size))
1904 return -EFAULT;
1905 return -ENOMEM;
1906 }
1907
1908 *param_flags |= DM_PARAMS_MALLOC;
1909
1910 /* Copy from param_kernel (which was already copied from user) */
1911 memcpy(dmi, param_kernel, minimum_data_size);
1912
1913 if (copy_from_user(&dmi->data, (char __user *)user + minimum_data_size,
1914 param_kernel->data_size - minimum_data_size))
1915 goto bad;
1916 data_copied:
1917 /* Wipe the user buffer so we do not return it to userspace */
1918 if (secure_data && clear_user(user, param_kernel->data_size))
1919 goto bad;
1920
1921 *param = dmi;
1922 return 0;
1923
1924 bad:
1925 free_params(dmi, param_kernel->data_size, *param_flags);
1926
1927 return -EFAULT;
1928 }
1929
validate_params(uint cmd,struct dm_ioctl * param)1930 static int validate_params(uint cmd, struct dm_ioctl *param)
1931 {
1932 /* Always clear this flag */
1933 param->flags &= ~DM_BUFFER_FULL_FLAG;
1934 param->flags &= ~DM_UEVENT_GENERATED_FLAG;
1935 param->flags &= ~DM_SECURE_DATA_FLAG;
1936 param->flags &= ~DM_DATA_OUT_FLAG;
1937
1938 /* Ignores parameters */
1939 if (cmd == DM_REMOVE_ALL_CMD ||
1940 cmd == DM_LIST_DEVICES_CMD ||
1941 cmd == DM_LIST_VERSIONS_CMD)
1942 return 0;
1943
1944 if (cmd == DM_DEV_CREATE_CMD) {
1945 if (!*param->name) {
1946 DMERR("name not supplied when creating device");
1947 return -EINVAL;
1948 }
1949 } else if (*param->uuid && *param->name) {
1950 DMERR("only supply one of name or uuid, cmd(%u)", cmd);
1951 return -EINVAL;
1952 }
1953
1954 /* Ensure strings are terminated */
1955 param->name[DM_NAME_LEN - 1] = '\0';
1956 param->uuid[DM_UUID_LEN - 1] = '\0';
1957
1958 return 0;
1959 }
1960
ctl_ioctl(struct file * file,uint command,struct dm_ioctl __user * user)1961 static int ctl_ioctl(struct file *file, uint command, struct dm_ioctl __user *user)
1962 {
1963 int r = 0;
1964 int ioctl_flags;
1965 int param_flags;
1966 unsigned int cmd;
1967 struct dm_ioctl *param;
1968 ioctl_fn fn = NULL;
1969 size_t input_param_size;
1970 struct dm_ioctl param_kernel;
1971
1972 /* only root can play with this */
1973 if (!capable(CAP_SYS_ADMIN))
1974 return -EACCES;
1975
1976 if (_IOC_TYPE(command) != DM_IOCTL)
1977 return -ENOTTY;
1978
1979 cmd = _IOC_NR(command);
1980
1981 /*
1982 * Check the interface version passed in. This also
1983 * writes out the kernel's interface version.
1984 */
1985 r = check_version(cmd, user, ¶m_kernel);
1986 if (r)
1987 return r;
1988
1989 /*
1990 * Nothing more to do for the version command.
1991 */
1992 if (cmd == DM_VERSION_CMD)
1993 return 0;
1994
1995 fn = lookup_ioctl(cmd, &ioctl_flags);
1996 if (!fn) {
1997 DMERR("dm_ctl_ioctl: unknown command 0x%x", command);
1998 return -ENOTTY;
1999 }
2000
2001 /*
2002 * Copy the parameters into kernel space.
2003 */
2004 r = copy_params(user, ¶m_kernel, ioctl_flags, ¶m, ¶m_flags);
2005
2006 if (r)
2007 return r;
2008
2009 input_param_size = param->data_size;
2010 r = validate_params(cmd, param);
2011 if (r)
2012 goto out;
2013
2014 param->data_size = offsetof(struct dm_ioctl, data);
2015 r = fn(file, param, input_param_size);
2016
2017 if (unlikely(param->flags & DM_BUFFER_FULL_FLAG) &&
2018 unlikely(ioctl_flags & IOCTL_FLAGS_NO_PARAMS))
2019 DMERR("ioctl %d tried to output some data but has IOCTL_FLAGS_NO_PARAMS set", cmd);
2020
2021 if (!r && ioctl_flags & IOCTL_FLAGS_ISSUE_GLOBAL_EVENT)
2022 dm_issue_global_event();
2023
2024 /*
2025 * Copy the results back to userland.
2026 */
2027 if (!r && copy_to_user(user, param, param->data_size))
2028 r = -EFAULT;
2029
2030 out:
2031 free_params(param, input_param_size, param_flags);
2032 return r;
2033 }
2034
dm_ctl_ioctl(struct file * file,uint command,ulong u)2035 static long dm_ctl_ioctl(struct file *file, uint command, ulong u)
2036 {
2037 return (long)ctl_ioctl(file, command, (struct dm_ioctl __user *)u);
2038 }
2039
2040 #ifdef CONFIG_COMPAT
dm_compat_ctl_ioctl(struct file * file,uint command,ulong u)2041 static long dm_compat_ctl_ioctl(struct file *file, uint command, ulong u)
2042 {
2043 return (long)dm_ctl_ioctl(file, command, (ulong) compat_ptr(u));
2044 }
2045 #else
2046 #define dm_compat_ctl_ioctl NULL
2047 #endif
2048
dm_open(struct inode * inode,struct file * filp)2049 static int dm_open(struct inode *inode, struct file *filp)
2050 {
2051 int r;
2052 struct dm_file *priv;
2053
2054 r = nonseekable_open(inode, filp);
2055 if (unlikely(r))
2056 return r;
2057
2058 priv = filp->private_data = kmalloc(sizeof(struct dm_file), GFP_KERNEL);
2059 if (!priv)
2060 return -ENOMEM;
2061
2062 priv->global_event_nr = atomic_read(&dm_global_event_nr);
2063
2064 return 0;
2065 }
2066
dm_release(struct inode * inode,struct file * filp)2067 static int dm_release(struct inode *inode, struct file *filp)
2068 {
2069 kfree(filp->private_data);
2070 return 0;
2071 }
2072
dm_poll(struct file * filp,poll_table * wait)2073 static __poll_t dm_poll(struct file *filp, poll_table *wait)
2074 {
2075 struct dm_file *priv = filp->private_data;
2076 __poll_t mask = 0;
2077
2078 poll_wait(filp, &dm_global_eventq, wait);
2079
2080 if ((int)(atomic_read(&dm_global_event_nr) - priv->global_event_nr) > 0)
2081 mask |= EPOLLIN;
2082
2083 return mask;
2084 }
2085
2086 static const struct file_operations _ctl_fops = {
2087 .open = dm_open,
2088 .release = dm_release,
2089 .poll = dm_poll,
2090 .unlocked_ioctl = dm_ctl_ioctl,
2091 .compat_ioctl = dm_compat_ctl_ioctl,
2092 .owner = THIS_MODULE,
2093 .llseek = noop_llseek,
2094 };
2095
2096 static struct miscdevice _dm_misc = {
2097 .minor = MAPPER_CTRL_MINOR,
2098 .name = DM_NAME,
2099 .nodename = DM_DIR "/" DM_CONTROL_NODE,
2100 .fops = &_ctl_fops
2101 };
2102
2103 MODULE_ALIAS_MISCDEV(MAPPER_CTRL_MINOR);
2104 MODULE_ALIAS("devname:" DM_DIR "/" DM_CONTROL_NODE);
2105
2106 /*
2107 * Create misc character device and link to DM_DIR/control.
2108 */
dm_interface_init(void)2109 int __init dm_interface_init(void)
2110 {
2111 int r;
2112
2113 r = misc_register(&_dm_misc);
2114 if (r) {
2115 DMERR("misc_register failed for control device");
2116 return r;
2117 }
2118
2119 DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR,
2120 DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA,
2121 DM_DRIVER_EMAIL);
2122 return 0;
2123 }
2124
dm_interface_exit(void)2125 void dm_interface_exit(void)
2126 {
2127 misc_deregister(&_dm_misc);
2128 dm_hash_exit();
2129 }
2130
2131 /**
2132 * dm_copy_name_and_uuid - Copy mapped device name & uuid into supplied buffers
2133 * @md: Pointer to mapped_device
2134 * @name: Buffer (size DM_NAME_LEN) for name
2135 * @uuid: Buffer (size DM_UUID_LEN) for uuid or empty string if uuid not defined
2136 */
dm_copy_name_and_uuid(struct mapped_device * md,char * name,char * uuid)2137 int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid)
2138 {
2139 int r = 0;
2140 struct hash_cell *hc;
2141
2142 if (!md)
2143 return -ENXIO;
2144
2145 mutex_lock(&dm_hash_cells_mutex);
2146 hc = dm_get_mdptr(md);
2147 if (!hc || hc->md != md) {
2148 r = -ENXIO;
2149 goto out;
2150 }
2151
2152 if (name)
2153 strcpy(name, hc->name);
2154 if (uuid)
2155 strcpy(uuid, hc->uuid ? : "");
2156
2157 out:
2158 mutex_unlock(&dm_hash_cells_mutex);
2159
2160 return r;
2161 }
2162 EXPORT_SYMBOL_GPL(dm_copy_name_and_uuid);
2163
2164 /**
2165 * dm_early_create - create a mapped device in early boot.
2166 *
2167 * @dmi: Contains main information of the device mapping to be created.
2168 * @spec_array: array of pointers to struct dm_target_spec. Describes the
2169 * mapping table of the device.
2170 * @target_params_array: array of strings with the parameters to a specific
2171 * target.
2172 *
2173 * Instead of having the struct dm_target_spec and the parameters for every
2174 * target embedded at the end of struct dm_ioctl (as performed in a normal
2175 * ioctl), pass them as arguments, so the caller doesn't need to serialize them.
2176 * The size of the spec_array and target_params_array is given by
2177 * @dmi->target_count.
2178 * This function is supposed to be called in early boot, so locking mechanisms
2179 * to protect against concurrent loads are not required.
2180 */
dm_early_create(struct dm_ioctl * dmi,struct dm_target_spec ** spec_array,char ** target_params_array)2181 int __init dm_early_create(struct dm_ioctl *dmi,
2182 struct dm_target_spec **spec_array,
2183 char **target_params_array)
2184 {
2185 int r, m = DM_ANY_MINOR;
2186 struct dm_table *t, *old_map;
2187 struct mapped_device *md;
2188 unsigned int i;
2189
2190 if (!dmi->target_count)
2191 return -EINVAL;
2192
2193 r = check_name(dmi->name);
2194 if (r)
2195 return r;
2196
2197 if (dmi->flags & DM_PERSISTENT_DEV_FLAG)
2198 m = MINOR(huge_decode_dev(dmi->dev));
2199
2200 /* alloc dm device */
2201 r = dm_create(m, &md);
2202 if (r)
2203 return r;
2204
2205 /* hash insert */
2206 r = dm_hash_insert(dmi->name, *dmi->uuid ? dmi->uuid : NULL, md);
2207 if (r)
2208 goto err_destroy_dm;
2209
2210 /* alloc table */
2211 r = dm_table_create(&t, get_mode(dmi), dmi->target_count, md);
2212 if (r)
2213 goto err_hash_remove;
2214
2215 /* add targets */
2216 for (i = 0; i < dmi->target_count; i++) {
2217 r = dm_table_add_target(t, spec_array[i]->target_type,
2218 (sector_t) spec_array[i]->sector_start,
2219 (sector_t) spec_array[i]->length,
2220 target_params_array[i]);
2221 if (r) {
2222 DMERR("error adding target to table");
2223 goto err_destroy_table;
2224 }
2225 }
2226
2227 /* finish table */
2228 r = dm_table_complete(t);
2229 if (r)
2230 goto err_destroy_table;
2231
2232 /* setup md->queue to reflect md's type (may block) */
2233 r = dm_setup_md_queue(md, t);
2234 if (r) {
2235 DMERR("unable to set up device queue for new table.");
2236 goto err_destroy_table;
2237 }
2238
2239 /* Set new map */
2240 dm_suspend(md, 0);
2241 old_map = dm_swap_table(md, t);
2242 if (IS_ERR(old_map)) {
2243 r = PTR_ERR(old_map);
2244 goto err_destroy_table;
2245 }
2246 set_disk_ro(dm_disk(md), !!(dmi->flags & DM_READONLY_FLAG));
2247
2248 /* resume device */
2249 r = dm_resume(md);
2250 if (r)
2251 goto err_destroy_table;
2252
2253 DMINFO("%s (%s) is ready", md->disk->disk_name, dmi->name);
2254 dm_put(md);
2255 return 0;
2256
2257 err_destroy_table:
2258 dm_table_destroy(t);
2259 err_hash_remove:
2260 (void) __hash_remove(__get_name_cell(dmi->name));
2261 /* release reference from __get_name_cell */
2262 dm_put(md);
2263 err_destroy_dm:
2264 dm_put(md);
2265 dm_destroy(md);
2266 return r;
2267 }
2268