blob: 41852ae287a58c29e675dd4b794f1670f9dc53e8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07003 * Copyright (C) 2004 - 2006 Red Hat, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * This file is released under the GPL.
6 */
7
Mike Snitzer4cc96132016-05-12 16:28:10 -04008#include "dm-core.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
10#include <linux/module.h>
11#include <linux/vmalloc.h>
12#include <linux/miscdevice.h>
Ingo Molnar5b3cc152017-02-02 20:43:54 +010013#include <linux/sched/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/init.h>
15#include <linux/wait.h>
16#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/dm-ioctl.h>
Darrick J. Wong3ac51e72006-03-27 01:17:54 -080018#include <linux/hdreg.h>
Milan Broz76c072b2008-02-08 02:09:56 +000019#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080021#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Alasdair G Kergon72d94862006-06-26 00:27:35 -070023#define DM_MSG_PREFIX "ioctl"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#define DM_DRIVER_EMAIL "dm-devel@redhat.com"
25
26/*-----------------------------------------------------------------
27 * The ioctl interface needs to be able to look up devices by
28 * name or uuid.
29 *---------------------------------------------------------------*/
30struct hash_cell {
31 struct list_head name_list;
32 struct list_head uuid_list;
33
34 char *name;
35 char *uuid;
36 struct mapped_device *md;
37 struct dm_table *new_map;
38};
39
40struct vers_iter {
41 size_t param_size;
42 struct dm_target_versions *vers, *old_vers;
43 char *end;
44 uint32_t flags;
45};
46
47
48#define NUM_BUCKETS 64
49#define MASK_BUCKETS (NUM_BUCKETS - 1)
50static struct list_head _name_buckets[NUM_BUCKETS];
51static struct list_head _uuid_buckets[NUM_BUCKETS];
52
Mikulas Patocka2c140a22013-11-01 18:27:41 -040053static void dm_hash_remove_all(bool keep_open_devices, bool mark_deferred, bool only_deferred);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/*
56 * Guards access to both hash tables.
57 */
58static DECLARE_RWSEM(_hash_lock);
59
Mikulas Patocka60769052009-12-10 23:51:52 +000060/*
61 * Protects use of mdptr to obtain hash cell name and uuid from mapped device.
62 */
63static DEFINE_MUTEX(dm_hash_cells_mutex);
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static void init_buckets(struct list_head *buckets)
66{
67 unsigned int i;
68
69 for (i = 0; i < NUM_BUCKETS; i++)
70 INIT_LIST_HEAD(buckets + i);
71}
72
73static int dm_hash_init(void)
74{
75 init_buckets(_name_buckets);
76 init_buckets(_uuid_buckets);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return 0;
78}
79
80static void dm_hash_exit(void)
81{
Mikulas Patocka2c140a22013-11-01 18:27:41 -040082 dm_hash_remove_all(false, false, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
85/*-----------------------------------------------------------------
86 * Hash function:
87 * We're not really concerned with the str hash function being
88 * fast since it's only used by the ioctl interface.
89 *---------------------------------------------------------------*/
90static unsigned int hash_str(const char *str)
91{
92 const unsigned int hash_mult = 2654435387U;
93 unsigned int h = 0;
94
95 while (*str)
96 h = (h + (unsigned int) *str++) * hash_mult;
97
98 return h & MASK_BUCKETS;
99}
100
101/*-----------------------------------------------------------------
102 * Code for looking up a device by name
103 *---------------------------------------------------------------*/
104static struct hash_cell *__get_name_cell(const char *str)
105{
106 struct hash_cell *hc;
107 unsigned int h = hash_str(str);
108
109 list_for_each_entry (hc, _name_buckets + h, name_list)
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700110 if (!strcmp(hc->name, str)) {
111 dm_get(hc->md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return hc;
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 return NULL;
116}
117
118static struct hash_cell *__get_uuid_cell(const char *str)
119{
120 struct hash_cell *hc;
121 unsigned int h = hash_str(str);
122
123 list_for_each_entry (hc, _uuid_buckets + h, uuid_list)
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700124 if (!strcmp(hc->uuid, str)) {
125 dm_get(hc->md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return hc;
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 return NULL;
130}
131
Mikulas Patockaba2e19b2011-08-02 12:32:06 +0100132static struct hash_cell *__get_dev_cell(uint64_t dev)
133{
134 struct mapped_device *md;
135 struct hash_cell *hc;
136
137 md = dm_get_md(huge_decode_dev(dev));
138 if (!md)
139 return NULL;
140
141 hc = dm_get_mdptr(md);
142 if (!hc) {
143 dm_put(md);
144 return NULL;
145 }
146
147 return hc;
148}
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/*-----------------------------------------------------------------
151 * Inserting, removing and renaming a device.
152 *---------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153static struct hash_cell *alloc_cell(const char *name, const char *uuid,
154 struct mapped_device *md)
155{
156 struct hash_cell *hc;
157
158 hc = kmalloc(sizeof(*hc), GFP_KERNEL);
159 if (!hc)
160 return NULL;
161
Paulo Marques543537b2005-06-23 00:09:02 -0700162 hc->name = kstrdup(name, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 if (!hc->name) {
164 kfree(hc);
165 return NULL;
166 }
167
168 if (!uuid)
169 hc->uuid = NULL;
170
171 else {
Paulo Marques543537b2005-06-23 00:09:02 -0700172 hc->uuid = kstrdup(uuid, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (!hc->uuid) {
174 kfree(hc->name);
175 kfree(hc);
176 return NULL;
177 }
178 }
179
180 INIT_LIST_HEAD(&hc->name_list);
181 INIT_LIST_HEAD(&hc->uuid_list);
182 hc->md = md;
183 hc->new_map = NULL;
184 return hc;
185}
186
187static void free_cell(struct hash_cell *hc)
188{
189 if (hc) {
190 kfree(hc->name);
191 kfree(hc->uuid);
192 kfree(hc);
193 }
194}
195
196/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 * The kdev_t and uuid of a device can never change once it is
198 * initially inserted.
199 */
200static int dm_hash_insert(const char *name, const char *uuid, struct mapped_device *md)
201{
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700202 struct hash_cell *cell, *hc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 /*
205 * Allocate the new cells.
206 */
207 cell = alloc_cell(name, uuid, md);
208 if (!cell)
209 return -ENOMEM;
210
211 /*
212 * Insert the cell into both hash tables.
213 */
214 down_write(&_hash_lock);
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700215 hc = __get_name_cell(name);
216 if (hc) {
217 dm_put(hc->md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 goto bad;
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 list_add(&cell->name_list, _name_buckets + hash_str(name));
222
223 if (uuid) {
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700224 hc = __get_uuid_cell(uuid);
225 if (hc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 list_del(&cell->name_list);
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700227 dm_put(hc->md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 goto bad;
229 }
230 list_add(&cell->uuid_list, _uuid_buckets + hash_str(uuid));
231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 dm_get(md);
Mikulas Patocka60769052009-12-10 23:51:52 +0000233 mutex_lock(&dm_hash_cells_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 dm_set_mdptr(md, cell);
Mikulas Patocka60769052009-12-10 23:51:52 +0000235 mutex_unlock(&dm_hash_cells_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 up_write(&_hash_lock);
237
238 return 0;
239
240 bad:
241 up_write(&_hash_lock);
242 free_cell(cell);
243 return -EBUSY;
244}
245
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100246static struct dm_table *__hash_remove(struct hash_cell *hc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
goggin, edward269fd2a2005-09-27 21:45:44 -0700248 struct dm_table *table;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100249 int srcu_idx;
goggin, edward269fd2a2005-09-27 21:45:44 -0700250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 /* remove from the dev hash */
252 list_del(&hc->uuid_list);
253 list_del(&hc->name_list);
Mikulas Patocka60769052009-12-10 23:51:52 +0000254 mutex_lock(&dm_hash_cells_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 dm_set_mdptr(hc->md, NULL);
Mikulas Patocka60769052009-12-10 23:51:52 +0000256 mutex_unlock(&dm_hash_cells_mutex);
goggin, edward269fd2a2005-09-27 21:45:44 -0700257
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100258 table = dm_get_live_table(hc->md, &srcu_idx);
259 if (table)
goggin, edward269fd2a2005-09-27 21:45:44 -0700260 dm_table_event(table);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100261 dm_put_live_table(hc->md, srcu_idx);
goggin, edward269fd2a2005-09-27 21:45:44 -0700262
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100263 table = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (hc->new_map)
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100265 table = hc->new_map;
Mike Anderson1134e5a2006-03-27 01:17:54 -0800266 dm_put(hc->md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 free_cell(hc);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100268
269 return table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400272static void dm_hash_remove_all(bool keep_open_devices, bool mark_deferred, bool only_deferred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Kiyoshi Ueda98f33282010-08-12 04:13:55 +0100274 int i, dev_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 struct hash_cell *hc;
Kiyoshi Ueda98f33282010-08-12 04:13:55 +0100276 struct mapped_device *md;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100277 struct dm_table *t;
Kiyoshi Ueda98f33282010-08-12 04:13:55 +0100278
279retry:
280 dev_skipped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 down_write(&_hash_lock);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 for (i = 0; i < NUM_BUCKETS; i++) {
Kiyoshi Ueda98f33282010-08-12 04:13:55 +0100285 list_for_each_entry(hc, _name_buckets + i, name_list) {
286 md = hc->md;
287 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700288
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400289 if (keep_open_devices &&
290 dm_lock_for_deletion(md, mark_deferred, only_deferred)) {
Kiyoshi Ueda98f33282010-08-12 04:13:55 +0100291 dm_put(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700292 dev_skipped++;
293 continue;
294 }
Kiyoshi Ueda98f33282010-08-12 04:13:55 +0100295
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100296 t = __hash_remove(hc);
Kiyoshi Ueda98f33282010-08-12 04:13:55 +0100297
298 up_write(&_hash_lock);
299
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100300 if (t) {
301 dm_sync_table(md);
302 dm_table_destroy(t);
303 }
Kiyoshi Ueda98f33282010-08-12 04:13:55 +0100304 dm_put(md);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +0100305 if (likely(keep_open_devices))
306 dm_destroy(md);
307 else
308 dm_destroy_immediate(md);
Kiyoshi Ueda98f33282010-08-12 04:13:55 +0100309
310 /*
311 * Some mapped devices may be using other mapped
312 * devices, so repeat until we make no further
313 * progress. If a new mapped device is created
314 * here it will also get removed.
315 */
316 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
318 }
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 up_write(&_hash_lock);
Kiyoshi Ueda98f33282010-08-12 04:13:55 +0100321
322 if (dev_skipped)
323 DMWARN("remove_all left %d open device(s)", dev_skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
Peter Jones84c89552011-01-13 19:59:47 +0000326/*
327 * Set the uuid of a hash_cell that isn't already set.
328 */
329static void __set_cell_uuid(struct hash_cell *hc, char *new_uuid)
330{
331 mutex_lock(&dm_hash_cells_mutex);
332 hc->uuid = new_uuid;
333 mutex_unlock(&dm_hash_cells_mutex);
334
335 list_add(&hc->uuid_list, _uuid_buckets + hash_str(new_uuid));
336}
337
338/*
339 * Changes the name of a hash_cell and returns the old name for
340 * the caller to free.
341 */
342static char *__change_cell_name(struct hash_cell *hc, char *new_name)
343{
344 char *old_name;
345
346 /*
347 * Rename and move the name cell.
348 */
349 list_del(&hc->name_list);
350 old_name = hc->name;
351
352 mutex_lock(&dm_hash_cells_mutex);
353 hc->name = new_name;
354 mutex_unlock(&dm_hash_cells_mutex);
355
356 list_add(&hc->name_list, _name_buckets + hash_str(new_name));
357
358 return old_name;
359}
360
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100361static struct mapped_device *dm_hash_rename(struct dm_ioctl *param,
362 const char *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Peter Jones84c89552011-01-13 19:59:47 +0000364 char *new_data, *old_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 struct hash_cell *hc;
goggin, edward81f17772006-01-06 00:20:01 -0800366 struct dm_table *table;
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100367 struct mapped_device *md;
Peter Jones84c89552011-01-13 19:59:47 +0000368 unsigned change_uuid = (param->flags & DM_UUID_FLAG) ? 1 : 0;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100369 int srcu_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 /*
372 * duplicate new.
373 */
Peter Jones84c89552011-01-13 19:59:47 +0000374 new_data = kstrdup(new, GFP_KERNEL);
375 if (!new_data)
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100376 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 down_write(&_hash_lock);
379
380 /*
381 * Is new free ?
382 */
Peter Jones84c89552011-01-13 19:59:47 +0000383 if (change_uuid)
384 hc = __get_uuid_cell(new);
385 else
386 hc = __get_name_cell(new);
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (hc) {
Peter Jones84c89552011-01-13 19:59:47 +0000389 DMWARN("Unable to change %s on mapped device %s to one that "
390 "already exists: %s",
391 change_uuid ? "uuid" : "name",
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100392 param->name, new);
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700393 dm_put(hc->md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 up_write(&_hash_lock);
Peter Jones84c89552011-01-13 19:59:47 +0000395 kfree(new_data);
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100396 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398
399 /*
400 * Is there such a device as 'old' ?
401 */
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100402 hc = __get_name_cell(param->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if (!hc) {
Peter Jones84c89552011-01-13 19:59:47 +0000404 DMWARN("Unable to rename non-existent device, %s to %s%s",
405 param->name, change_uuid ? "uuid " : "", new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 up_write(&_hash_lock);
Peter Jones84c89552011-01-13 19:59:47 +0000407 kfree(new_data);
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100408 return ERR_PTR(-ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410
411 /*
Peter Jones84c89552011-01-13 19:59:47 +0000412 * Does this device already have a uuid?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 */
Peter Jones84c89552011-01-13 19:59:47 +0000414 if (change_uuid && hc->uuid) {
415 DMWARN("Unable to change uuid of mapped device %s to %s "
416 "because uuid is already set to %s",
417 param->name, new, hc->uuid);
418 dm_put(hc->md);
419 up_write(&_hash_lock);
420 kfree(new_data);
421 return ERR_PTR(-EINVAL);
422 }
423
424 if (change_uuid)
425 __set_cell_uuid(hc, new_data);
426 else
427 old_name = __change_cell_name(hc, new_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
goggin, edward81f17772006-01-06 00:20:01 -0800429 /*
430 * Wake up any dm event waiters.
431 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100432 table = dm_get_live_table(hc->md, &srcu_idx);
433 if (table)
goggin, edward81f17772006-01-06 00:20:01 -0800434 dm_table_event(table);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100435 dm_put_live_table(hc->md, srcu_idx);
goggin, edward81f17772006-01-06 00:20:01 -0800436
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100437 if (!dm_kobject_uevent(hc->md, KOBJ_CHANGE, param->event_nr))
438 param->flags |= DM_UEVENT_GENERATED_FLAG;
Alasdair G Kergon69267a32007-12-13 14:15:57 +0000439
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100440 md = hc->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 up_write(&_hash_lock);
442 kfree(old_name);
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100443
444 return md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400447void dm_deferred_remove(void)
448{
449 dm_hash_remove_all(true, false, true);
450}
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452/*-----------------------------------------------------------------
453 * Implementation of the ioctl commands
454 *---------------------------------------------------------------*/
455/*
456 * All the ioctl commands get dispatched to functions with this
457 * prototype.
458 */
459typedef int (*ioctl_fn)(struct dm_ioctl *param, size_t param_size);
460
461static int remove_all(struct dm_ioctl *param, size_t param_size)
462{
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400463 dm_hash_remove_all(true, !!(param->flags & DM_DEFERRED_REMOVE), false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 param->data_size = 0;
465 return 0;
466}
467
468/*
469 * Round up the ptr to an 8-byte boundary.
470 */
471#define ALIGN_MASK 7
472static inline void *align_ptr(void *ptr)
473{
474 return (void *) (((size_t) (ptr + ALIGN_MASK)) & ~ALIGN_MASK);
475}
476
477/*
478 * Retrieves the data payload buffer from an already allocated
479 * struct dm_ioctl.
480 */
481static void *get_result_buffer(struct dm_ioctl *param, size_t param_size,
482 size_t *len)
483{
484 param->data_start = align_ptr(param + 1) - (void *) param;
485
486 if (param->data_start < param_size)
487 *len = param_size - param->data_start;
488 else
489 *len = 0;
490
491 return ((void *) param) + param->data_start;
492}
493
494static int list_devices(struct dm_ioctl *param, size_t param_size)
495{
496 unsigned int i;
497 struct hash_cell *hc;
498 size_t len, needed = 0;
499 struct gendisk *disk;
500 struct dm_name_list *nl, *old_nl = NULL;
501
502 down_write(&_hash_lock);
503
504 /*
505 * Loop through all the devices working out how much
506 * space we need.
507 */
508 for (i = 0; i < NUM_BUCKETS; i++) {
509 list_for_each_entry (hc, _name_buckets + i, name_list) {
510 needed += sizeof(struct dm_name_list);
511 needed += strlen(hc->name) + 1;
512 needed += ALIGN_MASK;
513 }
514 }
515
516 /*
517 * Grab our output buffer.
518 */
519 nl = get_result_buffer(param, param_size, &len);
520 if (len < needed) {
521 param->flags |= DM_BUFFER_FULL_FLAG;
522 goto out;
523 }
524 param->data_size = param->data_start + needed;
525
526 nl->dev = 0; /* Flags no data */
527
528 /*
529 * Now loop through filling out the names.
530 */
531 for (i = 0; i < NUM_BUCKETS; i++) {
532 list_for_each_entry (hc, _name_buckets + i, name_list) {
533 if (old_nl)
534 old_nl->next = (uint32_t) ((void *) nl -
535 (void *) old_nl);
536 disk = dm_disk(hc->md);
Tejun Heof331c022008-09-03 09:01:48 +0200537 nl->dev = huge_encode_dev(disk_devt(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 nl->next = 0;
539 strcpy(nl->name, hc->name);
540
541 old_nl = nl;
542 nl = align_ptr(((void *) ++nl) + strlen(hc->name) + 1);
543 }
544 }
545
546 out:
547 up_write(&_hash_lock);
548 return 0;
549}
550
551static void list_version_get_needed(struct target_type *tt, void *needed_param)
552{
553 size_t *needed = needed_param;
554
Alasdair G Kergonc4cc6632005-11-21 21:32:33 -0800555 *needed += sizeof(struct dm_target_versions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 *needed += strlen(tt->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 *needed += ALIGN_MASK;
558}
559
560static void list_version_get_info(struct target_type *tt, void *param)
561{
562 struct vers_iter *info = param;
563
564 /* Check space - it might have changed since the first iteration */
565 if ((char *)info->vers + sizeof(tt->version) + strlen(tt->name) + 1 >
566 info->end) {
567
568 info->flags = DM_BUFFER_FULL_FLAG;
569 return;
570 }
571
572 if (info->old_vers)
573 info->old_vers->next = (uint32_t) ((void *)info->vers -
574 (void *)info->old_vers);
575 info->vers->version[0] = tt->version[0];
576 info->vers->version[1] = tt->version[1];
577 info->vers->version[2] = tt->version[2];
578 info->vers->next = 0;
579 strcpy(info->vers->name, tt->name);
580
581 info->old_vers = info->vers;
582 info->vers = align_ptr(((void *) ++info->vers) + strlen(tt->name) + 1);
583}
584
585static int list_versions(struct dm_ioctl *param, size_t param_size)
586{
587 size_t len, needed = 0;
588 struct dm_target_versions *vers;
589 struct vers_iter iter_info;
590
591 /*
592 * Loop through all the devices working out how much
593 * space we need.
594 */
595 dm_target_iterate(list_version_get_needed, &needed);
596
597 /*
598 * Grab our output buffer.
599 */
600 vers = get_result_buffer(param, param_size, &len);
601 if (len < needed) {
602 param->flags |= DM_BUFFER_FULL_FLAG;
603 goto out;
604 }
605 param->data_size = param->data_start + needed;
606
607 iter_info.param_size = param_size;
608 iter_info.old_vers = NULL;
609 iter_info.vers = vers;
610 iter_info.flags = 0;
611 iter_info.end = (char *)vers+len;
612
613 /*
614 * Now loop through filling out the names & versions.
615 */
616 dm_target_iterate(list_version_get_info, &iter_info);
617 param->flags |= iter_info.flags;
618
619 out:
620 return 0;
621}
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623static int check_name(const char *name)
624{
625 if (strchr(name, '/')) {
626 DMWARN("invalid device name");
627 return -EINVAL;
628 }
629
630 return 0;
631}
632
633/*
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000634 * On successful return, the caller must not attempt to acquire
Junxiao Bi88e2f902014-12-24 14:48:12 +0800635 * _hash_lock without first calling dm_put_live_table, because dm_table_destroy
636 * waits for this dm_put_live_table and could be called under this lock.
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000637 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100638static struct dm_table *dm_get_inactive_table(struct mapped_device *md, int *srcu_idx)
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000639{
640 struct hash_cell *hc;
641 struct dm_table *table = NULL;
642
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100643 /* increment rcu count, we don't care about the table pointer */
644 dm_get_live_table(md, srcu_idx);
645
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000646 down_read(&_hash_lock);
647 hc = dm_get_mdptr(md);
648 if (!hc || hc->md != md) {
649 DMWARN("device has been removed from the dev hash table.");
650 goto out;
651 }
652
653 table = hc->new_map;
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000654
655out:
656 up_read(&_hash_lock);
657
658 return table;
659}
660
661static struct dm_table *dm_get_live_or_inactive_table(struct mapped_device *md,
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100662 struct dm_ioctl *param,
663 int *srcu_idx)
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000664{
665 return (param->flags & DM_QUERY_INACTIVE_TABLE_FLAG) ?
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100666 dm_get_inactive_table(md, srcu_idx) : dm_get_live_table(md, srcu_idx);
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000667}
668
669/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 * Fills in a dm_ioctl structure, ready for sending back to
671 * userland.
672 */
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100673static void __dev_status(struct mapped_device *md, struct dm_ioctl *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
675 struct gendisk *disk = dm_disk(md);
676 struct dm_table *table;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100677 int srcu_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 param->flags &= ~(DM_SUSPEND_FLAG | DM_READONLY_FLAG |
Mike Snitzerffcc3932014-10-28 18:34:52 -0400680 DM_ACTIVE_PRESENT_FLAG | DM_INTERNAL_SUSPEND_FLAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +0000682 if (dm_suspended_md(md))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 param->flags |= DM_SUSPEND_FLAG;
684
Mike Snitzerffcc3932014-10-28 18:34:52 -0400685 if (dm_suspended_internally_md(md))
686 param->flags |= DM_INTERNAL_SUSPEND_FLAG;
687
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400688 if (dm_test_deferred_remove_flag(md))
689 param->flags |= DM_DEFERRED_REMOVE;
690
Tejun Heof331c022008-09-03 09:01:48 +0200691 param->dev = huge_encode_dev(disk_devt(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700693 /*
694 * Yes, this will be out of date by the time it gets back
695 * to userland, but it is still very useful for
696 * debugging.
697 */
698 param->open_count = dm_open_count(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 param->event_nr = dm_get_event_nr(md);
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000701 param->target_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100703 table = dm_get_live_table(md, &srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (table) {
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000705 if (!(param->flags & DM_QUERY_INACTIVE_TABLE_FLAG)) {
706 if (get_disk_ro(disk))
707 param->flags |= DM_READONLY_FLAG;
708 param->target_count = dm_table_get_num_targets(table);
709 }
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000710
711 param->flags |= DM_ACTIVE_PRESENT_FLAG;
712 }
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100713 dm_put_live_table(md, srcu_idx);
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000714
715 if (param->flags & DM_QUERY_INACTIVE_TABLE_FLAG) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100716 int srcu_idx;
717 table = dm_get_inactive_table(md, &srcu_idx);
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000718 if (table) {
719 if (!(dm_table_get_mode(table) & FMODE_WRITE))
720 param->flags |= DM_READONLY_FLAG;
721 param->target_count = dm_table_get_num_targets(table);
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000722 }
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100723 dm_put_live_table(md, srcu_idx);
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
727static int dev_create(struct dm_ioctl *param, size_t param_size)
728{
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700729 int r, m = DM_ANY_MINOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 struct mapped_device *md;
731
732 r = check_name(param->name);
733 if (r)
734 return r;
735
736 if (param->flags & DM_PERSISTENT_DEV_FLAG)
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700737 m = MINOR(huge_decode_dev(param->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700739 r = dm_create(m, &md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (r)
741 return r;
742
743 r = dm_hash_insert(param->name, *param->uuid ? param->uuid : NULL, md);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +0100744 if (r) {
745 dm_put(md);
746 dm_destroy(md);
747 return r;
748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
751
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100752 __dev_status(md, param);
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 dm_put(md);
755
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +0100756 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
759/*
760 * Always use UUID for lookups if it's present, otherwise use name or dev.
761 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800762static struct hash_cell *__find_device_hash_cell(struct dm_ioctl *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Mikulas Patocka0ddf9642011-08-02 12:32:06 +0100764 struct hash_cell *hc = NULL;
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -0800765
Mikulas Patocka0ddf9642011-08-02 12:32:06 +0100766 if (*param->uuid) {
Mikulas Patocka759dea22011-08-02 12:32:06 +0100767 if (*param->name || param->dev)
768 return NULL;
769
Mikulas Patocka0ddf9642011-08-02 12:32:06 +0100770 hc = __get_uuid_cell(param->uuid);
771 if (!hc)
772 return NULL;
Mikulas Patockaba2e19b2011-08-02 12:32:06 +0100773 } else if (*param->name) {
Mikulas Patocka759dea22011-08-02 12:32:06 +0100774 if (param->dev)
775 return NULL;
776
Mikulas Patocka0ddf9642011-08-02 12:32:06 +0100777 hc = __get_name_cell(param->name);
778 if (!hc)
779 return NULL;
Mikulas Patockaba2e19b2011-08-02 12:32:06 +0100780 } else if (param->dev) {
781 hc = __get_dev_cell(param->dev);
782 if (!hc)
783 return NULL;
784 } else
Mikulas Patocka0ddf9642011-08-02 12:32:06 +0100785 return NULL;
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -0800786
Mikulas Patocka0ddf9642011-08-02 12:32:06 +0100787 /*
788 * Sneakily write in both the name and the uuid
789 * while we have the cell.
790 */
791 strlcpy(param->name, hc->name, sizeof(param->name));
792 if (hc->uuid)
793 strlcpy(param->uuid, hc->uuid, sizeof(param->uuid));
794 else
795 param->uuid[0] = '\0';
796
797 if (hc->new_map)
798 param->flags |= DM_INACTIVE_PRESENT_FLAG;
799 else
800 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
801
802 return hc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
Arjan van de Ven858119e2006-01-14 13:20:43 -0800805static struct mapped_device *find_device(struct dm_ioctl *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
807 struct hash_cell *hc;
808 struct mapped_device *md = NULL;
809
810 down_read(&_hash_lock);
811 hc = __find_device_hash_cell(param);
Mikulas Patocka0ddf9642011-08-02 12:32:06 +0100812 if (hc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 md = hc->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 up_read(&_hash_lock);
815
816 return md;
817}
818
819static int dev_remove(struct dm_ioctl *param, size_t param_size)
820{
821 struct hash_cell *hc;
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700822 struct mapped_device *md;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700823 int r;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100824 struct dm_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 down_write(&_hash_lock);
827 hc = __find_device_hash_cell(param);
828
829 if (!hc) {
Milan Broz810b4922011-01-13 19:59:55 +0000830 DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 up_write(&_hash_lock);
832 return -ENXIO;
833 }
834
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700835 md = hc->md;
836
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700837 /*
838 * Ensure the device is not open and nothing further can open it.
839 */
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400840 r = dm_lock_for_deletion(md, !!(param->flags & DM_DEFERRED_REMOVE), false);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700841 if (r) {
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400842 if (r == -EBUSY && param->flags & DM_DEFERRED_REMOVE) {
843 up_write(&_hash_lock);
844 dm_put(md);
845 return 0;
846 }
Milan Broz810b4922011-01-13 19:59:55 +0000847 DMDEBUG_LIMIT("unable to remove open device %s", hc->name);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700848 up_write(&_hash_lock);
849 dm_put(md);
850 return r;
851 }
852
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100853 t = __hash_remove(hc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 up_write(&_hash_lock);
Milan Broz60935eb2009-06-22 10:12:30 +0100855
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100856 if (t) {
857 dm_sync_table(md);
858 dm_table_destroy(t);
859 }
860
Mikulas Patocka2c140a22013-11-01 18:27:41 -0400861 param->flags &= ~DM_DEFERRED_REMOVE;
862
Peter Rajnoha3abf85b2010-03-06 02:32:31 +0000863 if (!dm_kobject_uevent(md, KOBJ_REMOVE, param->event_nr))
864 param->flags |= DM_UEVENT_GENERATED_FLAG;
Milan Broz60935eb2009-06-22 10:12:30 +0100865
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700866 dm_put(md);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +0100867 dm_destroy(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return 0;
869}
870
871/*
872 * Check a string doesn't overrun the chunk of
873 * memory we copied from userland.
874 */
875static int invalid_str(char *str, void *end)
876{
877 while ((void *) str < end)
878 if (!*str++)
879 return 0;
880
881 return -EINVAL;
882}
883
884static int dev_rename(struct dm_ioctl *param, size_t param_size)
885{
886 int r;
Peter Jones84c89552011-01-13 19:59:47 +0000887 char *new_data = (char *) param + param->data_start;
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100888 struct mapped_device *md;
Peter Jones84c89552011-01-13 19:59:47 +0000889 unsigned change_uuid = (param->flags & DM_UUID_FLAG) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Peter Jones84c89552011-01-13 19:59:47 +0000891 if (new_data < param->data ||
Alasdair Kergonc2b04822013-08-29 16:37:45 +0100892 invalid_str(new_data, (void *) param + param_size) || !*new_data ||
Peter Jones84c89552011-01-13 19:59:47 +0000893 strlen(new_data) > (change_uuid ? DM_UUID_LEN - 1 : DM_NAME_LEN - 1)) {
894 DMWARN("Invalid new mapped device name or uuid string supplied.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return -EINVAL;
896 }
897
Peter Jones84c89552011-01-13 19:59:47 +0000898 if (!change_uuid) {
899 r = check_name(new_data);
900 if (r)
901 return r;
902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Peter Jones84c89552011-01-13 19:59:47 +0000904 md = dm_hash_rename(param, new_data);
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100905 if (IS_ERR(md))
906 return PTR_ERR(md);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +0000907
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100908 __dev_status(md, param);
909 dm_put(md);
910
911 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800914static int dev_set_geometry(struct dm_ioctl *param, size_t param_size)
915{
916 int r = -EINVAL, x;
917 struct mapped_device *md;
918 struct hd_geometry geometry;
919 unsigned long indata[4];
920 char *geostr = (char *) param + param->data_start;
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100921 char dummy;
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800922
923 md = find_device(param);
924 if (!md)
925 return -ENXIO;
926
Alasdair G Kergon27238b22008-02-08 02:09:53 +0000927 if (geostr < param->data ||
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800928 invalid_str(geostr, (void *) param + param_size)) {
929 DMWARN("Invalid geometry supplied.");
930 goto out;
931 }
932
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100933 x = sscanf(geostr, "%lu %lu %lu %lu%c", indata,
934 indata + 1, indata + 2, indata + 3, &dummy);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800935
936 if (x != 4) {
937 DMWARN("Unable to interpret geometry settings.");
938 goto out;
939 }
940
941 if (indata[0] > 65535 || indata[1] > 255 ||
942 indata[2] > 255 || indata[3] > ULONG_MAX) {
943 DMWARN("Geometry exceeds range limits.");
944 goto out;
945 }
946
947 geometry.cylinders = indata[0];
948 geometry.heads = indata[1];
949 geometry.sectors = indata[2];
950 geometry.start = indata[3];
951
952 r = dm_set_geometry(md, &geometry);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800953
954 param->data_size = 0;
955
956out:
957 dm_put(md);
958 return r;
959}
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961static int do_suspend(struct dm_ioctl *param)
962{
963 int r = 0;
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -0800964 unsigned suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 struct mapped_device *md;
966
967 md = find_device(param);
968 if (!md)
969 return -ENXIO;
970
Alasdair G Kergon6da487d2006-01-06 00:20:07 -0800971 if (param->flags & DM_SKIP_LOCKFS_FLAG)
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -0800972 suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
Kiyoshi Ueda81fdb092006-12-08 02:41:07 -0800973 if (param->flags & DM_NOFLUSH_FLAG)
974 suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
Alasdair G Kergon6da487d2006-01-06 00:20:07 -0800975
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100976 if (!dm_suspended_md(md)) {
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -0800977 r = dm_suspend(md, suspend_flags);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100978 if (r)
979 goto out;
980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100982 __dev_status(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100984out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 dm_put(md);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return r;
988}
989
990static int do_resume(struct dm_ioctl *param)
991{
992 int r = 0;
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -0800993 unsigned suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 struct hash_cell *hc;
995 struct mapped_device *md;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +0000996 struct dm_table *new_map, *old_map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 down_write(&_hash_lock);
999
1000 hc = __find_device_hash_cell(param);
1001 if (!hc) {
Milan Broz810b4922011-01-13 19:59:55 +00001002 DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 up_write(&_hash_lock);
1004 return -ENXIO;
1005 }
1006
1007 md = hc->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 new_map = hc->new_map;
1010 hc->new_map = NULL;
1011 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
1012
1013 up_write(&_hash_lock);
1014
1015 /* Do we need to load a new map ? */
1016 if (new_map) {
1017 /* Suspend if it isn't already suspended */
Alasdair G Kergon6da487d2006-01-06 00:20:07 -08001018 if (param->flags & DM_SKIP_LOCKFS_FLAG)
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08001019 suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
Kiyoshi Ueda81fdb092006-12-08 02:41:07 -08001020 if (param->flags & DM_NOFLUSH_FLAG)
1021 suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +00001022 if (!dm_suspended_md(md))
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -08001023 dm_suspend(md, suspend_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00001025 old_map = dm_swap_table(md, new_map);
1026 if (IS_ERR(old_map)) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001027 dm_sync_table(md);
Mikulas Patockad5816872009-01-06 03:05:10 +00001028 dm_table_destroy(new_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 dm_put(md);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00001030 return PTR_ERR(old_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
1032
1033 if (dm_table_get_mode(new_map) & FMODE_WRITE)
1034 set_disk_ro(dm_disk(md), 0);
1035 else
1036 set_disk_ro(dm_disk(md), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
1038
Mike Snitzer0f3649a92010-03-06 02:32:24 +00001039 if (dm_suspended_md(md)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 r = dm_resume(md);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00001041 if (!r && !dm_kobject_uevent(md, KOBJ_CHANGE, param->event_nr))
1042 param->flags |= DM_UEVENT_GENERATED_FLAG;
Mike Snitzer0f3649a92010-03-06 02:32:24 +00001043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001045 /*
1046 * Since dm_swap_table synchronizes RCU, nobody should be in
1047 * read-side critical section already.
1048 */
Alasdair G Kergon042d2a92009-12-10 23:52:24 +00001049 if (old_map)
1050 dm_table_destroy(old_map);
Milan Broz60935eb2009-06-22 10:12:30 +01001051
Mike Snitzer0f3649a92010-03-06 02:32:24 +00001052 if (!r)
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001053 __dev_status(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 dm_put(md);
1056 return r;
1057}
1058
1059/*
1060 * Set or unset the suspension state of a device.
1061 * If the device already is in the requested state we just return its status.
1062 */
1063static int dev_suspend(struct dm_ioctl *param, size_t param_size)
1064{
1065 if (param->flags & DM_SUSPEND_FLAG)
1066 return do_suspend(param);
1067
1068 return do_resume(param);
1069}
1070
1071/*
1072 * Copies device info back to user space, used by
1073 * the create and info ioctls.
1074 */
1075static int dev_status(struct dm_ioctl *param, size_t param_size)
1076{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 struct mapped_device *md;
1078
1079 md = find_device(param);
1080 if (!md)
1081 return -ENXIO;
1082
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001083 __dev_status(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 dm_put(md);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001085
1086 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087}
1088
1089/*
1090 * Build up the status struct for each target
1091 */
1092static void retrieve_status(struct dm_table *table,
1093 struct dm_ioctl *param, size_t param_size)
1094{
1095 unsigned int i, num_targets;
1096 struct dm_target_spec *spec;
1097 char *outbuf, *outptr;
1098 status_type_t type;
1099 size_t remaining, len, used = 0;
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01001100 unsigned status_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 outptr = outbuf = get_result_buffer(param, param_size, &len);
1103
1104 if (param->flags & DM_STATUS_TABLE_FLAG)
1105 type = STATUSTYPE_TABLE;
1106 else
1107 type = STATUSTYPE_INFO;
1108
1109 /* Get all the target info */
1110 num_targets = dm_table_get_num_targets(table);
1111 for (i = 0; i < num_targets; i++) {
1112 struct dm_target *ti = dm_table_get_target(table, i);
Mikulas Patockafd7c0922013-03-01 22:45:44 +00001113 size_t l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 remaining = len - (outptr - outbuf);
1116 if (remaining <= sizeof(struct dm_target_spec)) {
1117 param->flags |= DM_BUFFER_FULL_FLAG;
1118 break;
1119 }
1120
1121 spec = (struct dm_target_spec *) outptr;
1122
1123 spec->status = 0;
1124 spec->sector_start = ti->begin;
1125 spec->length = ti->len;
1126 strncpy(spec->target_type, ti->type->name,
1127 sizeof(spec->target_type));
1128
1129 outptr += sizeof(struct dm_target_spec);
1130 remaining = len - (outptr - outbuf);
1131 if (remaining <= 0) {
1132 param->flags |= DM_BUFFER_FULL_FLAG;
1133 break;
1134 }
1135
1136 /* Get the status/table string from the target driver */
1137 if (ti->type->status) {
Alasdair G Kergon1f4e0ff2012-07-27 15:08:16 +01001138 if (param->flags & DM_NOFLUSH_FLAG)
1139 status_flags |= DM_STATUS_NOFLUSH_FLAG;
Mikulas Patockafd7c0922013-03-01 22:45:44 +00001140 ti->type->status(ti, type, status_flags, outptr, remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 } else
1142 outptr[0] = '\0';
1143
Mikulas Patockafd7c0922013-03-01 22:45:44 +00001144 l = strlen(outptr) + 1;
1145 if (l == remaining) {
1146 param->flags |= DM_BUFFER_FULL_FLAG;
1147 break;
1148 }
1149
1150 outptr += l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 used = param->data_start + (outptr - outbuf);
1152
1153 outptr = align_ptr(outptr);
1154 spec->next = outptr - outbuf;
1155 }
1156
1157 if (used)
1158 param->data_size = used;
1159
1160 param->target_count = num_targets;
1161}
1162
1163/*
1164 * Wait for a device to report an event
1165 */
1166static int dev_wait(struct dm_ioctl *param, size_t param_size)
1167{
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001168 int r = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 struct mapped_device *md;
1170 struct dm_table *table;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001171 int srcu_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 md = find_device(param);
1174 if (!md)
1175 return -ENXIO;
1176
1177 /*
1178 * Wait for a notification event
1179 */
1180 if (dm_wait_event(md, param->event_nr)) {
1181 r = -ERESTARTSYS;
1182 goto out;
1183 }
1184
1185 /*
1186 * The userland program is going to want to know what
1187 * changed to trigger the event, so we may as well tell
1188 * him and save an ioctl.
1189 */
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001190 __dev_status(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001192 table = dm_get_live_or_inactive_table(md, param, &srcu_idx);
1193 if (table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 retrieve_status(table, param, param_size);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001195 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001197out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 dm_put(md);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 return r;
1201}
1202
Al Viroaeb5d722008-09-02 15:28:45 -04001203static inline fmode_t get_mode(struct dm_ioctl *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
Al Viroaeb5d722008-09-02 15:28:45 -04001205 fmode_t mode = FMODE_READ | FMODE_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 if (param->flags & DM_READONLY_FLAG)
1208 mode = FMODE_READ;
1209
1210 return mode;
1211}
1212
1213static int next_target(struct dm_target_spec *last, uint32_t next, void *end,
1214 struct dm_target_spec **spec, char **target_params)
1215{
1216 *spec = (struct dm_target_spec *) ((unsigned char *) last + next);
1217 *target_params = (char *) (*spec + 1);
1218
1219 if (*spec < (last + 1))
1220 return -EINVAL;
1221
1222 return invalid_str(*target_params, end);
1223}
1224
1225static int populate_table(struct dm_table *table,
1226 struct dm_ioctl *param, size_t param_size)
1227{
1228 int r;
1229 unsigned int i = 0;
1230 struct dm_target_spec *spec = (struct dm_target_spec *) param;
1231 uint32_t next = param->data_start;
1232 void *end = (void *) param + param_size;
1233 char *target_params;
1234
1235 if (!param->target_count) {
1236 DMWARN("populate_table: no targets specified");
1237 return -EINVAL;
1238 }
1239
1240 for (i = 0; i < param->target_count; i++) {
1241
1242 r = next_target(spec, next, end, &spec, &target_params);
1243 if (r) {
1244 DMWARN("unable to find target");
1245 return r;
1246 }
1247
1248 r = dm_table_add_target(table, spec->target_type,
1249 (sector_t) spec->sector_start,
1250 (sector_t) spec->length,
1251 target_params);
1252 if (r) {
1253 DMWARN("error adding target to table");
1254 return r;
1255 }
1256
1257 next = spec->next;
1258 }
1259
1260 return dm_table_complete(table);
1261}
1262
Bart Van Assche7e0d5742017-04-27 10:11:23 -07001263static bool is_valid_type(enum dm_queue_mode cur, enum dm_queue_mode new)
Toshi Kanib5ab4a92016-06-28 13:37:15 -06001264{
1265 if (cur == new ||
1266 (cur == DM_TYPE_BIO_BASED && new == DM_TYPE_DAX_BIO_BASED))
1267 return true;
1268
1269 return false;
1270}
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272static int table_load(struct dm_ioctl *param, size_t param_size)
1273{
1274 int r;
1275 struct hash_cell *hc;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001276 struct dm_table *t, *old_map = NULL;
Mike Anderson1134e5a2006-03-27 01:17:54 -08001277 struct mapped_device *md;
Alasdair G Kergon36a04562011-10-31 20:19:04 +00001278 struct target_type *immutable_target_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Mike Anderson1134e5a2006-03-27 01:17:54 -08001280 md = find_device(param);
1281 if (!md)
1282 return -ENXIO;
1283
1284 r = dm_table_create(&t, get_mode(param), param->target_count, md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 if (r)
Mike Snitzerf11c1c52013-08-27 20:03:00 -04001286 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Mike Snitzer00c4fc32013-08-27 18:57:03 -04001288 /* Protect md->type and md->queue against concurrent table loads. */
1289 dm_lock_md_type(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 r = populate_table(t, param, param_size);
Mike Snitzerf11c1c52013-08-27 20:03:00 -04001291 if (r)
1292 goto err_unlock_md_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Alasdair G Kergon36a04562011-10-31 20:19:04 +00001294 immutable_target_type = dm_get_immutable_target_type(md);
1295 if (immutable_target_type &&
Mike Snitzerf083b092016-02-06 18:38:46 -05001296 (immutable_target_type != dm_table_get_immutable_target_type(t)) &&
1297 !dm_table_get_wildcard_target(t)) {
Alasdair G Kergon36a04562011-10-31 20:19:04 +00001298 DMWARN("can't replace immutable target type %s",
1299 immutable_target_type->name);
Alasdair G Kergon36a04562011-10-31 20:19:04 +00001300 r = -EINVAL;
Mike Snitzerf11c1c52013-08-27 20:03:00 -04001301 goto err_unlock_md_type;
Alasdair G Kergon36a04562011-10-31 20:19:04 +00001302 }
1303
Christoph Hellwig3e6180f2015-04-30 10:10:36 -04001304 if (dm_get_md_type(md) == DM_TYPE_NONE) {
Mike Snitzera5664da2010-08-12 04:14:01 +01001305 /* Initial table load: acquire type of table. */
1306 dm_set_md_type(md, dm_table_get_type(t));
Christoph Hellwig3e6180f2015-04-30 10:10:36 -04001307
1308 /* setup md->queue to reflect md's type (may block) */
Mike Snitzer591ddcf2016-01-31 12:05:42 -05001309 r = dm_setup_md_queue(md, t);
Christoph Hellwig3e6180f2015-04-30 10:10:36 -04001310 if (r) {
1311 DMWARN("unable to set up device queue for new table.");
1312 goto err_unlock_md_type;
1313 }
Toshi Kanib5ab4a92016-06-28 13:37:15 -06001314 } else if (!is_valid_type(dm_get_md_type(md), dm_table_get_type(t))) {
Mike Snitzera5664da2010-08-12 04:14:01 +01001315 DMWARN("can't change device type after initial table load.");
Mike Snitzera5664da2010-08-12 04:14:01 +01001316 r = -EINVAL;
Mike Snitzerf11c1c52013-08-27 20:03:00 -04001317 goto err_unlock_md_type;
Mike Snitzera5664da2010-08-12 04:14:01 +01001318 }
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001319
Mike Snitzera5664da2010-08-12 04:14:01 +01001320 dm_unlock_md_type(md);
1321
1322 /* stage inactive table */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 down_write(&_hash_lock);
Mike Anderson1134e5a2006-03-27 01:17:54 -08001324 hc = dm_get_mdptr(md);
1325 if (!hc || hc->md != md) {
1326 DMWARN("device has been removed from the dev hash table.");
Mike Anderson1134e5a2006-03-27 01:17:54 -08001327 up_write(&_hash_lock);
1328 r = -ENXIO;
Mike Snitzerf11c1c52013-08-27 20:03:00 -04001329 goto err_destroy_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 }
1331
1332 if (hc->new_map)
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001333 old_map = hc->new_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 hc->new_map = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 up_write(&_hash_lock);
Mike Anderson1134e5a2006-03-27 01:17:54 -08001336
1337 param->flags |= DM_INACTIVE_PRESENT_FLAG;
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001338 __dev_status(md, param);
Mike Anderson1134e5a2006-03-27 01:17:54 -08001339
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001340 if (old_map) {
1341 dm_sync_table(md);
1342 dm_table_destroy(old_map);
1343 }
1344
Mike Anderson1134e5a2006-03-27 01:17:54 -08001345 dm_put(md);
1346
Mike Snitzerf11c1c52013-08-27 20:03:00 -04001347 return 0;
1348
1349err_unlock_md_type:
1350 dm_unlock_md_type(md);
1351err_destroy_table:
1352 dm_table_destroy(t);
1353err:
1354 dm_put(md);
1355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 return r;
1357}
1358
1359static int table_clear(struct dm_ioctl *param, size_t param_size)
1360{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 struct hash_cell *hc;
Jeff Mahoney7ec75f22006-06-26 00:27:24 -07001362 struct mapped_device *md;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001363 struct dm_table *old_map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 down_write(&_hash_lock);
1366
1367 hc = __find_device_hash_cell(param);
1368 if (!hc) {
Milan Broz810b4922011-01-13 19:59:55 +00001369 DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 up_write(&_hash_lock);
1371 return -ENXIO;
1372 }
1373
1374 if (hc->new_map) {
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001375 old_map = hc->new_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 hc->new_map = NULL;
1377 }
1378
1379 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
1380
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001381 __dev_status(hc->md, param);
Jeff Mahoney7ec75f22006-06-26 00:27:24 -07001382 md = hc->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 up_write(&_hash_lock);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001384 if (old_map) {
1385 dm_sync_table(md);
1386 dm_table_destroy(old_map);
1387 }
Jeff Mahoney7ec75f22006-06-26 00:27:24 -07001388 dm_put(md);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001389
1390 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391}
1392
1393/*
1394 * Retrieves a list of devices used by a particular dm device.
1395 */
1396static void retrieve_deps(struct dm_table *table,
1397 struct dm_ioctl *param, size_t param_size)
1398{
1399 unsigned int count = 0;
1400 struct list_head *tmp;
1401 size_t len, needed;
Mikulas Patocka82b15192008-10-10 13:37:09 +01001402 struct dm_dev_internal *dd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 struct dm_target_deps *deps;
1404
1405 deps = get_result_buffer(param, param_size, &len);
1406
1407 /*
1408 * Count the devices.
1409 */
1410 list_for_each (tmp, dm_table_get_devices(table))
1411 count++;
1412
1413 /*
1414 * Check we have enough space.
1415 */
1416 needed = sizeof(*deps) + (sizeof(*deps->dev) * count);
1417 if (len < needed) {
1418 param->flags |= DM_BUFFER_FULL_FLAG;
1419 return;
1420 }
1421
1422 /*
1423 * Fill in the devices.
1424 */
1425 deps->count = count;
1426 count = 0;
1427 list_for_each_entry (dd, dm_table_get_devices(table), list)
Benjamin Marzinski86f11522014-08-13 13:53:43 -05001428 deps->dev[count++] = huge_encode_dev(dd->dm_dev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430 param->data_size = param->data_start + needed;
1431}
1432
1433static int table_deps(struct dm_ioctl *param, size_t param_size)
1434{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 struct mapped_device *md;
1436 struct dm_table *table;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001437 int srcu_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 md = find_device(param);
1440 if (!md)
1441 return -ENXIO;
1442
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001443 __dev_status(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001445 table = dm_get_live_or_inactive_table(md, param, &srcu_idx);
1446 if (table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 retrieve_deps(table, param, param_size);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001448 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 dm_put(md);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001451
1452 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453}
1454
1455/*
1456 * Return the status of a device as a text string for each
1457 * target.
1458 */
1459static int table_status(struct dm_ioctl *param, size_t param_size)
1460{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 struct mapped_device *md;
1462 struct dm_table *table;
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001463 int srcu_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 md = find_device(param);
1466 if (!md)
1467 return -ENXIO;
1468
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001469 __dev_status(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001471 table = dm_get_live_or_inactive_table(md, param, &srcu_idx);
1472 if (table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 retrieve_status(table, param, param_size);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001474 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 dm_put(md);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001477
1478 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479}
1480
Mikulas Patockaa2606242013-03-01 22:45:49 +00001481/*
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04001482 * Process device-mapper dependent messages. Messages prefixed with '@'
1483 * are processed by the DM core. All others are delivered to the target.
Mikulas Patockaa2606242013-03-01 22:45:49 +00001484 * Returns a number <= 1 if message was processed by device mapper.
1485 * Returns 2 if message should be delivered to the target.
1486 */
1487static int message_for_md(struct mapped_device *md, unsigned argc, char **argv,
1488 char *result, unsigned maxlen)
1489{
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04001490 int r;
1491
1492 if (**argv != '@')
1493 return 2; /* no '@' prefix, deliver to target */
1494
Mikulas Patocka2c140a22013-11-01 18:27:41 -04001495 if (!strcasecmp(argv[0], "@cancel_deferred_remove")) {
1496 if (argc != 1) {
1497 DMERR("Invalid arguments for @cancel_deferred_remove");
1498 return -EINVAL;
1499 }
1500 return dm_cancel_deferred_remove(md);
1501 }
1502
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04001503 r = dm_stats_message(md, argc, argv, result, maxlen);
1504 if (r < 2)
1505 return r;
1506
1507 DMERR("Unsupported message sent to DM core: %s", argv[0]);
1508 return -EINVAL;
Mikulas Patockaa2606242013-03-01 22:45:49 +00001509}
1510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511/*
1512 * Pass a message to the target that's at the supplied device offset.
1513 */
1514static int target_message(struct dm_ioctl *param, size_t param_size)
1515{
1516 int r, argc;
1517 char **argv;
1518 struct mapped_device *md;
1519 struct dm_table *table;
1520 struct dm_target *ti;
1521 struct dm_target_msg *tmsg = (void *) param + param->data_start;
Mikulas Patockaa2606242013-03-01 22:45:49 +00001522 size_t maxlen;
1523 char *result = get_result_buffer(param, param_size, &maxlen);
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001524 int srcu_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
1526 md = find_device(param);
1527 if (!md)
1528 return -ENXIO;
1529
Milan Broz027d50f2007-10-19 22:38:36 +01001530 if (tmsg < (struct dm_target_msg *) param->data ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 invalid_str(tmsg->message, (void *) param + param_size)) {
1532 DMWARN("Invalid target message parameters.");
1533 r = -EINVAL;
1534 goto out;
1535 }
1536
1537 r = dm_split_args(&argc, &argv, tmsg->message);
1538 if (r) {
1539 DMWARN("Failed to split target message parameters");
1540 goto out;
1541 }
1542
Alasdair G Kergon2ca4c922011-08-02 12:32:03 +01001543 if (!argc) {
1544 DMWARN("Empty message received.");
Jesper Juhl902c6a92012-03-07 19:09:34 +00001545 goto out_argv;
Alasdair G Kergon2ca4c922011-08-02 12:32:03 +01001546 }
1547
Mikulas Patockaa2606242013-03-01 22:45:49 +00001548 r = message_for_md(md, argc, argv, result, maxlen);
1549 if (r <= 1)
1550 goto out_argv;
1551
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001552 table = dm_get_live_table(md, &srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (!table)
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001554 goto out_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Mike Andersonc50abeb2009-12-10 23:52:20 +00001556 if (dm_deleting_md(md)) {
1557 r = -ENXIO;
1558 goto out_table;
1559 }
1560
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001561 ti = dm_table_find_target(table, tmsg->sector);
1562 if (!dm_target_is_valid(ti)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 DMWARN("Target message sector outside device.");
1564 r = -EINVAL;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001565 } else if (ti->type->message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 r = ti->type->message(ti, argc, argv);
1567 else {
1568 DMWARN("Target type does not support messages");
1569 r = -EINVAL;
1570 }
1571
Mike Andersonc50abeb2009-12-10 23:52:20 +00001572 out_table:
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +01001573 dm_put_live_table(md, srcu_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 out_argv:
1575 kfree(argv);
1576 out:
Mikulas Patockaa2606242013-03-01 22:45:49 +00001577 if (r >= 0)
1578 __dev_status(md, param);
1579
1580 if (r == 1) {
1581 param->flags |= DM_DATA_OUT_FLAG;
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -04001582 if (dm_message_test_buffer_overflow(result, maxlen))
Mikulas Patockaa2606242013-03-01 22:45:49 +00001583 param->flags |= DM_BUFFER_FULL_FLAG;
1584 else
1585 param->data_size = param->data_start + strlen(result) + 1;
1586 r = 0;
1587 }
1588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 dm_put(md);
1590 return r;
1591}
1592
Mikulas Patockae2914cc2013-03-01 22:45:48 +00001593/*
1594 * The ioctl parameter block consists of two parts, a dm_ioctl struct
1595 * followed by a data buffer. This flag is set if the second part,
1596 * which has a variable size, is not used by the function processing
1597 * the ioctl.
1598 */
1599#define IOCTL_FLAGS_NO_PARAMS 1
1600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601/*-----------------------------------------------------------------
1602 * Implementation of open/close/ioctl on the special char
1603 * device.
1604 *---------------------------------------------------------------*/
Mikulas Patockae2914cc2013-03-01 22:45:48 +00001605static ioctl_fn lookup_ioctl(unsigned int cmd, int *ioctl_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
1607 static struct {
1608 int cmd;
Mikulas Patockae2914cc2013-03-01 22:45:48 +00001609 int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 ioctl_fn fn;
1611 } _ioctls[] = {
Mikulas Patockae2914cc2013-03-01 22:45:48 +00001612 {DM_VERSION_CMD, 0, NULL}, /* version is dealt with elsewhere */
1613 {DM_REMOVE_ALL_CMD, IOCTL_FLAGS_NO_PARAMS, remove_all},
1614 {DM_LIST_DEVICES_CMD, 0, list_devices},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Mikulas Patockae2914cc2013-03-01 22:45:48 +00001616 {DM_DEV_CREATE_CMD, IOCTL_FLAGS_NO_PARAMS, dev_create},
1617 {DM_DEV_REMOVE_CMD, IOCTL_FLAGS_NO_PARAMS, dev_remove},
1618 {DM_DEV_RENAME_CMD, 0, dev_rename},
1619 {DM_DEV_SUSPEND_CMD, IOCTL_FLAGS_NO_PARAMS, dev_suspend},
1620 {DM_DEV_STATUS_CMD, IOCTL_FLAGS_NO_PARAMS, dev_status},
1621 {DM_DEV_WAIT_CMD, 0, dev_wait},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Mikulas Patockae2914cc2013-03-01 22:45:48 +00001623 {DM_TABLE_LOAD_CMD, 0, table_load},
1624 {DM_TABLE_CLEAR_CMD, IOCTL_FLAGS_NO_PARAMS, table_clear},
1625 {DM_TABLE_DEPS_CMD, 0, table_deps},
1626 {DM_TABLE_STATUS_CMD, 0, table_status},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Mikulas Patockae2914cc2013-03-01 22:45:48 +00001628 {DM_LIST_VERSIONS_CMD, 0, list_versions},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Mikulas Patockae2914cc2013-03-01 22:45:48 +00001630 {DM_TARGET_MSG_CMD, 0, target_message},
1631 {DM_DEV_SET_GEOMETRY_CMD, 0, dev_set_geometry}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 };
1633
Mikulas Patockae2914cc2013-03-01 22:45:48 +00001634 if (unlikely(cmd >= ARRAY_SIZE(_ioctls)))
1635 return NULL;
1636
1637 *ioctl_flags = _ioctls[cmd].flags;
1638 return _ioctls[cmd].fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
1640
1641/*
1642 * As well as checking the version compatibility this always
1643 * copies the kernel interface version out.
1644 */
1645static int check_version(unsigned int cmd, struct dm_ioctl __user *user)
1646{
1647 uint32_t version[3];
1648 int r = 0;
1649
1650 if (copy_from_user(version, user->version, sizeof(version)))
1651 return -EFAULT;
1652
1653 if ((DM_VERSION_MAJOR != version[0]) ||
1654 (DM_VERSION_MINOR < version[1])) {
1655 DMWARN("ioctl interface mismatch: "
1656 "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
1657 DM_VERSION_MAJOR, DM_VERSION_MINOR,
1658 DM_VERSION_PATCHLEVEL,
1659 version[0], version[1], version[2], cmd);
1660 r = -EINVAL;
1661 }
1662
1663 /*
1664 * Fill in the kernel version.
1665 */
1666 version[0] = DM_VERSION_MAJOR;
1667 version[1] = DM_VERSION_MINOR;
1668 version[2] = DM_VERSION_PATCHLEVEL;
1669 if (copy_to_user(user->version, version, sizeof(version)))
1670 return -EFAULT;
1671
1672 return r;
1673}
1674
Bart Van Assche028b39e2016-06-28 16:36:46 +02001675#define DM_PARAMS_MALLOC 0x0001 /* Params allocated with kvmalloc() */
Mikulas Patocka9c5091f2012-12-21 20:23:36 +00001676#define DM_WIPE_BUFFER 0x0010 /* Wipe input buffer before returning from ioctl */
1677
1678static void free_params(struct dm_ioctl *param, size_t param_size, int param_flags)
1679{
1680 if (param_flags & DM_WIPE_BUFFER)
1681 memset(param, 0, param_size);
1682
Bart Van Assche028b39e2016-06-28 16:36:46 +02001683 if (param_flags & DM_PARAMS_MALLOC)
1684 kvfree(param);
Mikulas Patocka9c5091f2012-12-21 20:23:36 +00001685}
1686
Mikulas Patocka02cde502013-03-01 22:45:49 +00001687static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl *param_kernel,
1688 int ioctl_flags,
1689 struct dm_ioctl **param, int *param_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
Mikulas Patocka02cde502013-03-01 22:45:49 +00001691 struct dm_ioctl *dmi;
Milan Brozf8681202011-03-24 13:54:30 +00001692 int secure_data;
Bart Van Assche60807582016-11-18 14:28:00 -08001693 const size_t minimum_data_size = offsetof(struct dm_ioctl, data);
Michal Hockod224e932017-05-08 15:57:34 -07001694 unsigned noio_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Mikulas Patocka02cde502013-03-01 22:45:49 +00001696 if (copy_from_user(param_kernel, user, minimum_data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 return -EFAULT;
1698
Mikulas Patocka02cde502013-03-01 22:45:49 +00001699 if (param_kernel->data_size < minimum_data_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 return -EINVAL;
1701
Mikulas Patocka02cde502013-03-01 22:45:49 +00001702 secure_data = param_kernel->flags & DM_SECURE_DATA_FLAG;
Milan Brozf8681202011-03-24 13:54:30 +00001703
Mikulas Patocka9c5091f2012-12-21 20:23:36 +00001704 *param_flags = secure_data ? DM_WIPE_BUFFER : 0;
1705
Mikulas Patocka02cde502013-03-01 22:45:49 +00001706 if (ioctl_flags & IOCTL_FLAGS_NO_PARAMS) {
1707 dmi = param_kernel;
1708 dmi->data_size = minimum_data_size;
1709 goto data_copied;
1710 }
1711
Mikulas Patocka5023e5c2012-12-21 20:23:36 +00001712 /*
Junaid Shahid8c1e21622017-05-18 12:00:51 -07001713 * Use __GFP_HIGH to avoid low memory issues when a device is
1714 * suspended and the ioctl is needed to resume it.
Mikulas Patocka9c5091f2012-12-21 20:23:36 +00001715 * Use kmalloc() rather than vmalloc() when we can.
Mikulas Patocka5023e5c2012-12-21 20:23:36 +00001716 */
Mikulas Patocka9c5091f2012-12-21 20:23:36 +00001717 dmi = NULL;
Michal Hockod224e932017-05-08 15:57:34 -07001718 noio_flag = memalloc_noio_save();
Junaid Shahid8c1e21622017-05-18 12:00:51 -07001719 dmi = kvmalloc(param_kernel->data_size, GFP_KERNEL | __GFP_HIGH);
Michal Hockod224e932017-05-08 15:57:34 -07001720 memalloc_noio_restore(noio_flag);
Mikulas Patocka02cde502013-03-01 22:45:49 +00001721
1722 if (!dmi) {
1723 if (secure_data && clear_user(user, param_kernel->data_size))
Milan Brozf8681202011-03-24 13:54:30 +00001724 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 return -ENOMEM;
Milan Brozf8681202011-03-24 13:54:30 +00001726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
Bart Van Assche028b39e2016-06-28 16:36:46 +02001728 *param_flags |= DM_PARAMS_MALLOC;
1729
Mikulas Patocka02cde502013-03-01 22:45:49 +00001730 if (copy_from_user(dmi, user, param_kernel->data_size))
Milan Broz6bb43b52011-03-24 13:54:28 +00001731 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Mikulas Patocka02cde502013-03-01 22:45:49 +00001733data_copied:
Alasdair G Kergone910d7e2012-12-21 20:23:30 +00001734 /*
1735 * Abort if something changed the ioctl data while it was being copied.
1736 */
Mikulas Patocka02cde502013-03-01 22:45:49 +00001737 if (dmi->data_size != param_kernel->data_size) {
Alasdair G Kergone910d7e2012-12-21 20:23:30 +00001738 DMERR("rejecting ioctl: data size modified while processing parameters");
1739 goto bad;
1740 }
1741
Milan Brozf8681202011-03-24 13:54:30 +00001742 /* Wipe the user buffer so we do not return it to userspace */
Mikulas Patocka02cde502013-03-01 22:45:49 +00001743 if (secure_data && clear_user(user, param_kernel->data_size))
Milan Brozf8681202011-03-24 13:54:30 +00001744 goto bad;
1745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 *param = dmi;
1747 return 0;
Milan Broz6bb43b52011-03-24 13:54:28 +00001748
1749bad:
Mikulas Patocka02cde502013-03-01 22:45:49 +00001750 free_params(dmi, param_kernel->data_size, *param_flags);
Mikulas Patocka9c5091f2012-12-21 20:23:36 +00001751
Milan Broz6bb43b52011-03-24 13:54:28 +00001752 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753}
1754
1755static int validate_params(uint cmd, struct dm_ioctl *param)
1756{
1757 /* Always clear this flag */
1758 param->flags &= ~DM_BUFFER_FULL_FLAG;
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00001759 param->flags &= ~DM_UEVENT_GENERATED_FLAG;
Milan Brozf8681202011-03-24 13:54:30 +00001760 param->flags &= ~DM_SECURE_DATA_FLAG;
Mikulas Patockaa2606242013-03-01 22:45:49 +00001761 param->flags &= ~DM_DATA_OUT_FLAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
1763 /* Ignores parameters */
1764 if (cmd == DM_REMOVE_ALL_CMD ||
1765 cmd == DM_LIST_DEVICES_CMD ||
1766 cmd == DM_LIST_VERSIONS_CMD)
1767 return 0;
1768
Matthias Kaehlckee36215d2017-04-17 11:05:03 -07001769 if (cmd == DM_DEV_CREATE_CMD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 if (!*param->name) {
1771 DMWARN("name not supplied when creating device");
1772 return -EINVAL;
1773 }
Matthias Kaehlckee36215d2017-04-17 11:05:03 -07001774 } else if (*param->uuid && *param->name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 DMWARN("only supply one of name or uuid, cmd(%u)", cmd);
1776 return -EINVAL;
1777 }
1778
1779 /* Ensure strings are terminated */
1780 param->name[DM_NAME_LEN - 1] = '\0';
1781 param->uuid[DM_UUID_LEN - 1] = '\0';
1782
1783 return 0;
1784}
1785
Alasdair G Kergon27238b22008-02-08 02:09:53 +00001786static int ctl_ioctl(uint command, struct dm_ioctl __user *user)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
1788 int r = 0;
Mikulas Patockae2914cc2013-03-01 22:45:48 +00001789 int ioctl_flags;
Mikulas Patocka9c5091f2012-12-21 20:23:36 +00001790 int param_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 unsigned int cmd;
Andrew Mortona26ffd42008-02-08 02:10:16 +00001792 struct dm_ioctl *uninitialized_var(param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 ioctl_fn fn = NULL;
Milan Broz6bb43b52011-03-24 13:54:28 +00001794 size_t input_param_size;
Mikulas Patocka02cde502013-03-01 22:45:49 +00001795 struct dm_ioctl param_kernel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
1797 /* only root can play with this */
1798 if (!capable(CAP_SYS_ADMIN))
1799 return -EACCES;
1800
1801 if (_IOC_TYPE(command) != DM_IOCTL)
1802 return -ENOTTY;
1803
1804 cmd = _IOC_NR(command);
1805
1806 /*
1807 * Check the interface version passed in. This also
1808 * writes out the kernel's interface version.
1809 */
1810 r = check_version(cmd, user);
1811 if (r)
1812 return r;
1813
1814 /*
1815 * Nothing more to do for the version command.
1816 */
1817 if (cmd == DM_VERSION_CMD)
1818 return 0;
1819
Mikulas Patockae2914cc2013-03-01 22:45:48 +00001820 fn = lookup_ioctl(cmd, &ioctl_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 if (!fn) {
1822 DMWARN("dm_ctl_ioctl: unknown command 0x%x", command);
1823 return -ENOTTY;
1824 }
1825
1826 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 * Copy the parameters into kernel space.
1828 */
Mikulas Patocka02cde502013-03-01 22:45:49 +00001829 r = copy_params(user, &param_kernel, ioctl_flags, &param, &param_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Alasdair G Kergondab6a422006-02-01 03:04:52 -08001831 if (r)
1832 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Milan Brozf8681202011-03-24 13:54:30 +00001834 input_param_size = param->data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 r = validate_params(cmd, param);
1836 if (r)
1837 goto out;
1838
Adrian Salido4617f562017-04-27 10:32:55 -07001839 param->data_size = offsetof(struct dm_ioctl, data);
Milan Broz6bb43b52011-03-24 13:54:28 +00001840 r = fn(param, input_param_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Mikulas Patockae2914cc2013-03-01 22:45:48 +00001842 if (unlikely(param->flags & DM_BUFFER_FULL_FLAG) &&
1843 unlikely(ioctl_flags & IOCTL_FLAGS_NO_PARAMS))
1844 DMERR("ioctl %d tried to output some data but has IOCTL_FLAGS_NO_PARAMS set", cmd);
1845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 /*
1847 * Copy the results back to userland.
1848 */
1849 if (!r && copy_to_user(user, param, param->data_size))
1850 r = -EFAULT;
1851
Milan Broz6bb43b52011-03-24 13:54:28 +00001852out:
Mikulas Patocka9c5091f2012-12-21 20:23:36 +00001853 free_params(param, input_param_size, param_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 return r;
1855}
1856
Alasdair G Kergon27238b22008-02-08 02:09:53 +00001857static long dm_ctl_ioctl(struct file *file, uint command, ulong u)
1858{
1859 return (long)ctl_ioctl(command, (struct dm_ioctl __user *)u);
1860}
1861
Milan Broz76c072b2008-02-08 02:09:56 +00001862#ifdef CONFIG_COMPAT
1863static long dm_compat_ctl_ioctl(struct file *file, uint command, ulong u)
1864{
1865 return (long)dm_ctl_ioctl(file, command, (ulong) compat_ptr(u));
1866}
1867#else
1868#define dm_compat_ctl_ioctl NULL
1869#endif
1870
Arjan van de Venfa027c22007-02-12 00:55:33 -08001871static const struct file_operations _ctl_fops = {
Arnd Bergmann402ab352010-08-12 04:13:57 +01001872 .open = nonseekable_open,
Alasdair G Kergon27238b22008-02-08 02:09:53 +00001873 .unlocked_ioctl = dm_ctl_ioctl,
Milan Broz76c072b2008-02-08 02:09:56 +00001874 .compat_ioctl = dm_compat_ctl_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 .owner = THIS_MODULE,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001876 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877};
1878
1879static struct miscdevice _dm_misc = {
Peter Rajnoha7e507eb2010-08-12 04:14:05 +01001880 .minor = MAPPER_CTRL_MINOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 .name = DM_NAME,
Peter Rajnoha7e507eb2010-08-12 04:14:05 +01001882 .nodename = DM_DIR "/" DM_CONTROL_NODE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 .fops = &_ctl_fops
1884};
1885
Peter Rajnoha7e507eb2010-08-12 04:14:05 +01001886MODULE_ALIAS_MISCDEV(MAPPER_CTRL_MINOR);
1887MODULE_ALIAS("devname:" DM_DIR "/" DM_CONTROL_NODE);
1888
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889/*
1890 * Create misc character device and link to DM_DIR/control.
1891 */
1892int __init dm_interface_init(void)
1893{
1894 int r;
1895
1896 r = dm_hash_init();
1897 if (r)
1898 return r;
1899
1900 r = misc_register(&_dm_misc);
1901 if (r) {
1902 DMERR("misc_register failed for control device");
1903 dm_hash_exit();
1904 return r;
1905 }
1906
1907 DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR,
1908 DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA,
1909 DM_DRIVER_EMAIL);
1910 return 0;
1911}
1912
1913void dm_interface_exit(void)
1914{
Greg Kroah-Hartmanf368ed62015-07-30 15:59:57 -07001915 misc_deregister(&_dm_misc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 dm_hash_exit();
1917}
Mike Anderson96a1f7d2007-10-19 22:47:59 +01001918
1919/**
1920 * dm_copy_name_and_uuid - Copy mapped device name & uuid into supplied buffers
1921 * @md: Pointer to mapped_device
1922 * @name: Buffer (size DM_NAME_LEN) for name
1923 * @uuid: Buffer (size DM_UUID_LEN) for uuid or empty string if uuid not defined
1924 */
1925int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid)
1926{
1927 int r = 0;
1928 struct hash_cell *hc;
1929
1930 if (!md)
1931 return -ENXIO;
1932
Mikulas Patocka60769052009-12-10 23:51:52 +00001933 mutex_lock(&dm_hash_cells_mutex);
Mike Anderson96a1f7d2007-10-19 22:47:59 +01001934 hc = dm_get_mdptr(md);
1935 if (!hc || hc->md != md) {
1936 r = -ENXIO;
1937 goto out;
1938 }
1939
Milan Broz23d39f62009-01-06 03:05:04 +00001940 if (name)
1941 strcpy(name, hc->name);
1942 if (uuid)
1943 strcpy(uuid, hc->uuid ? : "");
Mike Anderson96a1f7d2007-10-19 22:47:59 +01001944
1945out:
Mikulas Patocka60769052009-12-10 23:51:52 +00001946 mutex_unlock(&dm_hash_cells_mutex);
Mike Anderson96a1f7d2007-10-19 22:47:59 +01001947
1948 return r;
1949}