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