blob: 99d38a6925a432c12ab055e9876492e8350ded05 [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
8#include "dm.h"
9
10#include <linux/module.h>
11#include <linux/vmalloc.h>
12#include <linux/miscdevice.h>
13#include <linux/init.h>
14#include <linux/wait.h>
15#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/dm-ioctl.h>
Darrick J. Wong3ac51e72006-03-27 01:17:54 -080017#include <linux/hdreg.h>
Milan Broz76c072b2008-02-08 02:09:56 +000018#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include <asm/uaccess.h>
21
Alasdair G Kergon72d94862006-06-26 00:27:35 -070022#define DM_MSG_PREFIX "ioctl"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#define DM_DRIVER_EMAIL "dm-devel@redhat.com"
24
25/*-----------------------------------------------------------------
26 * The ioctl interface needs to be able to look up devices by
27 * name or uuid.
28 *---------------------------------------------------------------*/
29struct hash_cell {
30 struct list_head name_list;
31 struct list_head uuid_list;
32
33 char *name;
34 char *uuid;
35 struct mapped_device *md;
36 struct dm_table *new_map;
37};
38
39struct vers_iter {
40 size_t param_size;
41 struct dm_target_versions *vers, *old_vers;
42 char *end;
43 uint32_t flags;
44};
45
46
47#define NUM_BUCKETS 64
48#define MASK_BUCKETS (NUM_BUCKETS - 1)
49static struct list_head _name_buckets[NUM_BUCKETS];
50static struct list_head _uuid_buckets[NUM_BUCKETS];
51
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -070052static void dm_hash_remove_all(int keep_open_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54/*
55 * Guards access to both hash tables.
56 */
57static DECLARE_RWSEM(_hash_lock);
58
Mikulas Patocka60769052009-12-10 23:51:52 +000059/*
60 * Protects use of mdptr to obtain hash cell name and uuid from mapped device.
61 */
62static DEFINE_MUTEX(dm_hash_cells_mutex);
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static void init_buckets(struct list_head *buckets)
65{
66 unsigned int i;
67
68 for (i = 0; i < NUM_BUCKETS; i++)
69 INIT_LIST_HEAD(buckets + i);
70}
71
72static int dm_hash_init(void)
73{
74 init_buckets(_name_buckets);
75 init_buckets(_uuid_buckets);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 return 0;
77}
78
79static void dm_hash_exit(void)
80{
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -070081 dm_hash_remove_all(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
84/*-----------------------------------------------------------------
85 * Hash function:
86 * We're not really concerned with the str hash function being
87 * fast since it's only used by the ioctl interface.
88 *---------------------------------------------------------------*/
89static unsigned int hash_str(const char *str)
90{
91 const unsigned int hash_mult = 2654435387U;
92 unsigned int h = 0;
93
94 while (*str)
95 h = (h + (unsigned int) *str++) * hash_mult;
96
97 return h & MASK_BUCKETS;
98}
99
100/*-----------------------------------------------------------------
101 * Code for looking up a device by name
102 *---------------------------------------------------------------*/
103static struct hash_cell *__get_name_cell(const char *str)
104{
105 struct hash_cell *hc;
106 unsigned int h = hash_str(str);
107
108 list_for_each_entry (hc, _name_buckets + h, name_list)
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700109 if (!strcmp(hc->name, str)) {
110 dm_get(hc->md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return hc;
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 return NULL;
115}
116
117static struct hash_cell *__get_uuid_cell(const char *str)
118{
119 struct hash_cell *hc;
120 unsigned int h = hash_str(str);
121
122 list_for_each_entry (hc, _uuid_buckets + h, uuid_list)
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700123 if (!strcmp(hc->uuid, str)) {
124 dm_get(hc->md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return hc;
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 return NULL;
129}
130
131/*-----------------------------------------------------------------
132 * Inserting, removing and renaming a device.
133 *---------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134static struct hash_cell *alloc_cell(const char *name, const char *uuid,
135 struct mapped_device *md)
136{
137 struct hash_cell *hc;
138
139 hc = kmalloc(sizeof(*hc), GFP_KERNEL);
140 if (!hc)
141 return NULL;
142
Paulo Marques543537b2005-06-23 00:09:02 -0700143 hc->name = kstrdup(name, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (!hc->name) {
145 kfree(hc);
146 return NULL;
147 }
148
149 if (!uuid)
150 hc->uuid = NULL;
151
152 else {
Paulo Marques543537b2005-06-23 00:09:02 -0700153 hc->uuid = kstrdup(uuid, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if (!hc->uuid) {
155 kfree(hc->name);
156 kfree(hc);
157 return NULL;
158 }
159 }
160
161 INIT_LIST_HEAD(&hc->name_list);
162 INIT_LIST_HEAD(&hc->uuid_list);
163 hc->md = md;
164 hc->new_map = NULL;
165 return hc;
166}
167
168static void free_cell(struct hash_cell *hc)
169{
170 if (hc) {
171 kfree(hc->name);
172 kfree(hc->uuid);
173 kfree(hc);
174 }
175}
176
177/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 * The kdev_t and uuid of a device can never change once it is
179 * initially inserted.
180 */
181static int dm_hash_insert(const char *name, const char *uuid, struct mapped_device *md)
182{
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700183 struct hash_cell *cell, *hc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 /*
186 * Allocate the new cells.
187 */
188 cell = alloc_cell(name, uuid, md);
189 if (!cell)
190 return -ENOMEM;
191
192 /*
193 * Insert the cell into both hash tables.
194 */
195 down_write(&_hash_lock);
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700196 hc = __get_name_cell(name);
197 if (hc) {
198 dm_put(hc->md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 goto bad;
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 list_add(&cell->name_list, _name_buckets + hash_str(name));
203
204 if (uuid) {
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700205 hc = __get_uuid_cell(uuid);
206 if (hc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 list_del(&cell->name_list);
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700208 dm_put(hc->md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 goto bad;
210 }
211 list_add(&cell->uuid_list, _uuid_buckets + hash_str(uuid));
212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 dm_get(md);
Mikulas Patocka60769052009-12-10 23:51:52 +0000214 mutex_lock(&dm_hash_cells_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 dm_set_mdptr(md, cell);
Mikulas Patocka60769052009-12-10 23:51:52 +0000216 mutex_unlock(&dm_hash_cells_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 up_write(&_hash_lock);
218
219 return 0;
220
221 bad:
222 up_write(&_hash_lock);
223 free_cell(cell);
224 return -EBUSY;
225}
226
227static void __hash_remove(struct hash_cell *hc)
228{
goggin, edward269fd2a2005-09-27 21:45:44 -0700229 struct dm_table *table;
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 /* remove from the dev hash */
232 list_del(&hc->uuid_list);
233 list_del(&hc->name_list);
Mikulas Patocka60769052009-12-10 23:51:52 +0000234 mutex_lock(&dm_hash_cells_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 dm_set_mdptr(hc->md, NULL);
Mikulas Patocka60769052009-12-10 23:51:52 +0000236 mutex_unlock(&dm_hash_cells_mutex);
goggin, edward269fd2a2005-09-27 21:45:44 -0700237
Alasdair G Kergon7c666412009-12-10 23:52:19 +0000238 table = dm_get_live_table(hc->md);
goggin, edward269fd2a2005-09-27 21:45:44 -0700239 if (table) {
240 dm_table_event(table);
241 dm_table_put(table);
242 }
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 if (hc->new_map)
Mikulas Patockad5816872009-01-06 03:05:10 +0000245 dm_table_destroy(hc->new_map);
Mike Anderson1134e5a2006-03-27 01:17:54 -0800246 dm_put(hc->md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 free_cell(hc);
248}
249
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700250static void dm_hash_remove_all(int keep_open_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Kiyoshi Ueda98f33282010-08-12 04:13:55 +0100252 int i, dev_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 struct hash_cell *hc;
Kiyoshi Ueda98f33282010-08-12 04:13:55 +0100254 struct mapped_device *md;
255
256retry:
257 dev_skipped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 down_write(&_hash_lock);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 for (i = 0; i < NUM_BUCKETS; i++) {
Kiyoshi Ueda98f33282010-08-12 04:13:55 +0100262 list_for_each_entry(hc, _name_buckets + i, name_list) {
263 md = hc->md;
264 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700265
Kiyoshi Ueda98f33282010-08-12 04:13:55 +0100266 if (keep_open_devices && dm_lock_for_deletion(md)) {
267 dm_put(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700268 dev_skipped++;
269 continue;
270 }
Kiyoshi Ueda98f33282010-08-12 04:13:55 +0100271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 __hash_remove(hc);
Kiyoshi Ueda98f33282010-08-12 04:13:55 +0100273
274 up_write(&_hash_lock);
275
276 dm_put(md);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +0100277 if (likely(keep_open_devices))
278 dm_destroy(md);
279 else
280 dm_destroy_immediate(md);
Kiyoshi Ueda98f33282010-08-12 04:13:55 +0100281
282 /*
283 * Some mapped devices may be using other mapped
284 * devices, so repeat until we make no further
285 * progress. If a new mapped device is created
286 * here it will also get removed.
287 */
288 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290 }
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 up_write(&_hash_lock);
Kiyoshi Ueda98f33282010-08-12 04:13:55 +0100293
294 if (dev_skipped)
295 DMWARN("remove_all left %d open device(s)", dev_skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
Peter Jones84c89552011-01-13 19:59:47 +0000298/*
299 * Set the uuid of a hash_cell that isn't already set.
300 */
301static void __set_cell_uuid(struct hash_cell *hc, char *new_uuid)
302{
303 mutex_lock(&dm_hash_cells_mutex);
304 hc->uuid = new_uuid;
305 mutex_unlock(&dm_hash_cells_mutex);
306
307 list_add(&hc->uuid_list, _uuid_buckets + hash_str(new_uuid));
308}
309
310/*
311 * Changes the name of a hash_cell and returns the old name for
312 * the caller to free.
313 */
314static char *__change_cell_name(struct hash_cell *hc, char *new_name)
315{
316 char *old_name;
317
318 /*
319 * Rename and move the name cell.
320 */
321 list_del(&hc->name_list);
322 old_name = hc->name;
323
324 mutex_lock(&dm_hash_cells_mutex);
325 hc->name = new_name;
326 mutex_unlock(&dm_hash_cells_mutex);
327
328 list_add(&hc->name_list, _name_buckets + hash_str(new_name));
329
330 return old_name;
331}
332
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100333static struct mapped_device *dm_hash_rename(struct dm_ioctl *param,
334 const char *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Peter Jones84c89552011-01-13 19:59:47 +0000336 char *new_data, *old_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 struct hash_cell *hc;
goggin, edward81f17772006-01-06 00:20:01 -0800338 struct dm_table *table;
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100339 struct mapped_device *md;
Peter Jones84c89552011-01-13 19:59:47 +0000340 unsigned change_uuid = (param->flags & DM_UUID_FLAG) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 /*
343 * duplicate new.
344 */
Peter Jones84c89552011-01-13 19:59:47 +0000345 new_data = kstrdup(new, GFP_KERNEL);
346 if (!new_data)
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100347 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 down_write(&_hash_lock);
350
351 /*
352 * Is new free ?
353 */
Peter Jones84c89552011-01-13 19:59:47 +0000354 if (change_uuid)
355 hc = __get_uuid_cell(new);
356 else
357 hc = __get_name_cell(new);
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (hc) {
Peter Jones84c89552011-01-13 19:59:47 +0000360 DMWARN("Unable to change %s on mapped device %s to one that "
361 "already exists: %s",
362 change_uuid ? "uuid" : "name",
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100363 param->name, new);
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700364 dm_put(hc->md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 up_write(&_hash_lock);
Peter Jones84c89552011-01-13 19:59:47 +0000366 kfree(new_data);
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100367 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
369
370 /*
371 * Is there such a device as 'old' ?
372 */
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100373 hc = __get_name_cell(param->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (!hc) {
Peter Jones84c89552011-01-13 19:59:47 +0000375 DMWARN("Unable to rename non-existent device, %s to %s%s",
376 param->name, change_uuid ? "uuid " : "", new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 up_write(&_hash_lock);
Peter Jones84c89552011-01-13 19:59:47 +0000378 kfree(new_data);
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100379 return ERR_PTR(-ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381
382 /*
Peter Jones84c89552011-01-13 19:59:47 +0000383 * Does this device already have a uuid?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 */
Peter Jones84c89552011-01-13 19:59:47 +0000385 if (change_uuid && hc->uuid) {
386 DMWARN("Unable to change uuid of mapped device %s to %s "
387 "because uuid is already set to %s",
388 param->name, new, hc->uuid);
389 dm_put(hc->md);
390 up_write(&_hash_lock);
391 kfree(new_data);
392 return ERR_PTR(-EINVAL);
393 }
394
395 if (change_uuid)
396 __set_cell_uuid(hc, new_data);
397 else
398 old_name = __change_cell_name(hc, new_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
goggin, edward81f17772006-01-06 00:20:01 -0800400 /*
401 * Wake up any dm event waiters.
402 */
Alasdair G Kergon7c666412009-12-10 23:52:19 +0000403 table = dm_get_live_table(hc->md);
goggin, edward81f17772006-01-06 00:20:01 -0800404 if (table) {
405 dm_table_event(table);
406 dm_table_put(table);
407 }
408
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100409 if (!dm_kobject_uevent(hc->md, KOBJ_CHANGE, param->event_nr))
410 param->flags |= DM_UEVENT_GENERATED_FLAG;
Alasdair G Kergon69267a32007-12-13 14:15:57 +0000411
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100412 md = hc->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 up_write(&_hash_lock);
414 kfree(old_name);
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100415
416 return md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
419/*-----------------------------------------------------------------
420 * Implementation of the ioctl commands
421 *---------------------------------------------------------------*/
422/*
423 * All the ioctl commands get dispatched to functions with this
424 * prototype.
425 */
426typedef int (*ioctl_fn)(struct dm_ioctl *param, size_t param_size);
427
428static int remove_all(struct dm_ioctl *param, size_t param_size)
429{
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700430 dm_hash_remove_all(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 param->data_size = 0;
432 return 0;
433}
434
435/*
436 * Round up the ptr to an 8-byte boundary.
437 */
438#define ALIGN_MASK 7
439static inline void *align_ptr(void *ptr)
440{
441 return (void *) (((size_t) (ptr + ALIGN_MASK)) & ~ALIGN_MASK);
442}
443
444/*
445 * Retrieves the data payload buffer from an already allocated
446 * struct dm_ioctl.
447 */
448static void *get_result_buffer(struct dm_ioctl *param, size_t param_size,
449 size_t *len)
450{
451 param->data_start = align_ptr(param + 1) - (void *) param;
452
453 if (param->data_start < param_size)
454 *len = param_size - param->data_start;
455 else
456 *len = 0;
457
458 return ((void *) param) + param->data_start;
459}
460
461static int list_devices(struct dm_ioctl *param, size_t param_size)
462{
463 unsigned int i;
464 struct hash_cell *hc;
465 size_t len, needed = 0;
466 struct gendisk *disk;
467 struct dm_name_list *nl, *old_nl = NULL;
468
469 down_write(&_hash_lock);
470
471 /*
472 * Loop through all the devices working out how much
473 * space we need.
474 */
475 for (i = 0; i < NUM_BUCKETS; i++) {
476 list_for_each_entry (hc, _name_buckets + i, name_list) {
477 needed += sizeof(struct dm_name_list);
478 needed += strlen(hc->name) + 1;
479 needed += ALIGN_MASK;
480 }
481 }
482
483 /*
484 * Grab our output buffer.
485 */
486 nl = get_result_buffer(param, param_size, &len);
487 if (len < needed) {
488 param->flags |= DM_BUFFER_FULL_FLAG;
489 goto out;
490 }
491 param->data_size = param->data_start + needed;
492
493 nl->dev = 0; /* Flags no data */
494
495 /*
496 * Now loop through filling out the names.
497 */
498 for (i = 0; i < NUM_BUCKETS; i++) {
499 list_for_each_entry (hc, _name_buckets + i, name_list) {
500 if (old_nl)
501 old_nl->next = (uint32_t) ((void *) nl -
502 (void *) old_nl);
503 disk = dm_disk(hc->md);
Tejun Heof331c022008-09-03 09:01:48 +0200504 nl->dev = huge_encode_dev(disk_devt(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 nl->next = 0;
506 strcpy(nl->name, hc->name);
507
508 old_nl = nl;
509 nl = align_ptr(((void *) ++nl) + strlen(hc->name) + 1);
510 }
511 }
512
513 out:
514 up_write(&_hash_lock);
515 return 0;
516}
517
518static void list_version_get_needed(struct target_type *tt, void *needed_param)
519{
520 size_t *needed = needed_param;
521
Alasdair G Kergonc4cc6632005-11-21 21:32:33 -0800522 *needed += sizeof(struct dm_target_versions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 *needed += strlen(tt->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 *needed += ALIGN_MASK;
525}
526
527static void list_version_get_info(struct target_type *tt, void *param)
528{
529 struct vers_iter *info = param;
530
531 /* Check space - it might have changed since the first iteration */
532 if ((char *)info->vers + sizeof(tt->version) + strlen(tt->name) + 1 >
533 info->end) {
534
535 info->flags = DM_BUFFER_FULL_FLAG;
536 return;
537 }
538
539 if (info->old_vers)
540 info->old_vers->next = (uint32_t) ((void *)info->vers -
541 (void *)info->old_vers);
542 info->vers->version[0] = tt->version[0];
543 info->vers->version[1] = tt->version[1];
544 info->vers->version[2] = tt->version[2];
545 info->vers->next = 0;
546 strcpy(info->vers->name, tt->name);
547
548 info->old_vers = info->vers;
549 info->vers = align_ptr(((void *) ++info->vers) + strlen(tt->name) + 1);
550}
551
552static int list_versions(struct dm_ioctl *param, size_t param_size)
553{
554 size_t len, needed = 0;
555 struct dm_target_versions *vers;
556 struct vers_iter iter_info;
557
558 /*
559 * Loop through all the devices working out how much
560 * space we need.
561 */
562 dm_target_iterate(list_version_get_needed, &needed);
563
564 /*
565 * Grab our output buffer.
566 */
567 vers = get_result_buffer(param, param_size, &len);
568 if (len < needed) {
569 param->flags |= DM_BUFFER_FULL_FLAG;
570 goto out;
571 }
572 param->data_size = param->data_start + needed;
573
574 iter_info.param_size = param_size;
575 iter_info.old_vers = NULL;
576 iter_info.vers = vers;
577 iter_info.flags = 0;
578 iter_info.end = (char *)vers+len;
579
580 /*
581 * Now loop through filling out the names & versions.
582 */
583 dm_target_iterate(list_version_get_info, &iter_info);
584 param->flags |= iter_info.flags;
585
586 out:
587 return 0;
588}
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590static int check_name(const char *name)
591{
592 if (strchr(name, '/')) {
593 DMWARN("invalid device name");
594 return -EINVAL;
595 }
596
597 return 0;
598}
599
600/*
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000601 * On successful return, the caller must not attempt to acquire
602 * _hash_lock without first calling dm_table_put, because dm_table_destroy
603 * waits for this dm_table_put and could be called under this lock.
604 */
605static struct dm_table *dm_get_inactive_table(struct mapped_device *md)
606{
607 struct hash_cell *hc;
608 struct dm_table *table = NULL;
609
610 down_read(&_hash_lock);
611 hc = dm_get_mdptr(md);
612 if (!hc || hc->md != md) {
613 DMWARN("device has been removed from the dev hash table.");
614 goto out;
615 }
616
617 table = hc->new_map;
618 if (table)
619 dm_table_get(table);
620
621out:
622 up_read(&_hash_lock);
623
624 return table;
625}
626
627static struct dm_table *dm_get_live_or_inactive_table(struct mapped_device *md,
628 struct dm_ioctl *param)
629{
630 return (param->flags & DM_QUERY_INACTIVE_TABLE_FLAG) ?
631 dm_get_inactive_table(md) : dm_get_live_table(md);
632}
633
634/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 * Fills in a dm_ioctl structure, ready for sending back to
636 * userland.
637 */
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100638static void __dev_status(struct mapped_device *md, struct dm_ioctl *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 struct gendisk *disk = dm_disk(md);
641 struct dm_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 param->flags &= ~(DM_SUSPEND_FLAG | DM_READONLY_FLAG |
644 DM_ACTIVE_PRESENT_FLAG);
645
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +0000646 if (dm_suspended_md(md))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 param->flags |= DM_SUSPEND_FLAG;
648
Tejun Heof331c022008-09-03 09:01:48 +0200649 param->dev = huge_encode_dev(disk_devt(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700651 /*
652 * Yes, this will be out of date by the time it gets back
653 * to userland, but it is still very useful for
654 * debugging.
655 */
656 param->open_count = dm_open_count(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 param->event_nr = dm_get_event_nr(md);
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000659 param->target_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Alasdair G Kergon7c666412009-12-10 23:52:19 +0000661 table = dm_get_live_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (table) {
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000663 if (!(param->flags & DM_QUERY_INACTIVE_TABLE_FLAG)) {
664 if (get_disk_ro(disk))
665 param->flags |= DM_READONLY_FLAG;
666 param->target_count = dm_table_get_num_targets(table);
667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 dm_table_put(table);
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000669
670 param->flags |= DM_ACTIVE_PRESENT_FLAG;
671 }
672
673 if (param->flags & DM_QUERY_INACTIVE_TABLE_FLAG) {
674 table = dm_get_inactive_table(md);
675 if (table) {
676 if (!(dm_table_get_mode(table) & FMODE_WRITE))
677 param->flags |= DM_READONLY_FLAG;
678 param->target_count = dm_table_get_num_targets(table);
679 dm_table_put(table);
680 }
681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
684static int dev_create(struct dm_ioctl *param, size_t param_size)
685{
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700686 int r, m = DM_ANY_MINOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 struct mapped_device *md;
688
689 r = check_name(param->name);
690 if (r)
691 return r;
692
693 if (param->flags & DM_PERSISTENT_DEV_FLAG)
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700694 m = MINOR(huge_decode_dev(param->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700696 r = dm_create(m, &md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (r)
698 return r;
699
700 r = dm_hash_insert(param->name, *param->uuid ? param->uuid : NULL, md);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +0100701 if (r) {
702 dm_put(md);
703 dm_destroy(md);
704 return r;
705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
708
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100709 __dev_status(md, param);
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 dm_put(md);
712
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +0100713 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
716/*
717 * Always use UUID for lookups if it's present, otherwise use name or dev.
718 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800719static struct hash_cell *__find_device_hash_cell(struct dm_ioctl *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -0800721 struct mapped_device *md;
Mikulas Patocka0ddf9642011-08-02 12:32:06 +0100722 struct hash_cell *hc = NULL;
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -0800723
Mikulas Patocka0ddf9642011-08-02 12:32:06 +0100724 if (*param->uuid) {
725 hc = __get_uuid_cell(param->uuid);
726 if (!hc)
727 return NULL;
728 goto fill_params;
729 }
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -0800730
Mikulas Patocka0ddf9642011-08-02 12:32:06 +0100731 if (*param->name) {
732 hc = __get_name_cell(param->name);
733 if (!hc)
734 return NULL;
735 goto fill_params;
736 }
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -0800737
738 md = dm_get_md(huge_decode_dev(param->dev));
Alasdair G Kergonbfc5ecd2006-11-08 17:44:42 -0800739 if (!md)
Mikulas Patocka0ddf9642011-08-02 12:32:06 +0100740 return NULL;
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -0800741
Mikulas Patocka0ddf9642011-08-02 12:32:06 +0100742 hc = dm_get_mdptr(md);
743 if (!hc) {
Alasdair G Kergonbfc5ecd2006-11-08 17:44:42 -0800744 dm_put(md);
Mikulas Patocka0ddf9642011-08-02 12:32:06 +0100745 return NULL;
746 }
Alasdair G Kergonbfc5ecd2006-11-08 17:44:42 -0800747
Mikulas Patocka0ddf9642011-08-02 12:32:06 +0100748fill_params:
749 /*
750 * Sneakily write in both the name and the uuid
751 * while we have the cell.
752 */
753 strlcpy(param->name, hc->name, sizeof(param->name));
754 if (hc->uuid)
755 strlcpy(param->uuid, hc->uuid, sizeof(param->uuid));
756 else
757 param->uuid[0] = '\0';
758
759 if (hc->new_map)
760 param->flags |= DM_INACTIVE_PRESENT_FLAG;
761 else
762 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
763
764 return hc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
Arjan van de Ven858119e2006-01-14 13:20:43 -0800767static struct mapped_device *find_device(struct dm_ioctl *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
769 struct hash_cell *hc;
770 struct mapped_device *md = NULL;
771
772 down_read(&_hash_lock);
773 hc = __find_device_hash_cell(param);
Mikulas Patocka0ddf9642011-08-02 12:32:06 +0100774 if (hc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 md = hc->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 up_read(&_hash_lock);
777
778 return md;
779}
780
781static int dev_remove(struct dm_ioctl *param, size_t param_size)
782{
783 struct hash_cell *hc;
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700784 struct mapped_device *md;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700785 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 down_write(&_hash_lock);
788 hc = __find_device_hash_cell(param);
789
790 if (!hc) {
Milan Broz810b4922011-01-13 19:59:55 +0000791 DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 up_write(&_hash_lock);
793 return -ENXIO;
794 }
795
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700796 md = hc->md;
797
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700798 /*
799 * Ensure the device is not open and nothing further can open it.
800 */
801 r = dm_lock_for_deletion(md);
802 if (r) {
Milan Broz810b4922011-01-13 19:59:55 +0000803 DMDEBUG_LIMIT("unable to remove open device %s", hc->name);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700804 up_write(&_hash_lock);
805 dm_put(md);
806 return r;
807 }
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 __hash_remove(hc);
810 up_write(&_hash_lock);
Milan Broz60935eb2009-06-22 10:12:30 +0100811
Peter Rajnoha3abf85b2010-03-06 02:32:31 +0000812 if (!dm_kobject_uevent(md, KOBJ_REMOVE, param->event_nr))
813 param->flags |= DM_UEVENT_GENERATED_FLAG;
Milan Broz60935eb2009-06-22 10:12:30 +0100814
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700815 dm_put(md);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +0100816 dm_destroy(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return 0;
818}
819
820/*
821 * Check a string doesn't overrun the chunk of
822 * memory we copied from userland.
823 */
824static int invalid_str(char *str, void *end)
825{
826 while ((void *) str < end)
827 if (!*str++)
828 return 0;
829
830 return -EINVAL;
831}
832
833static int dev_rename(struct dm_ioctl *param, size_t param_size)
834{
835 int r;
Peter Jones84c89552011-01-13 19:59:47 +0000836 char *new_data = (char *) param + param->data_start;
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100837 struct mapped_device *md;
Peter Jones84c89552011-01-13 19:59:47 +0000838 unsigned change_uuid = (param->flags & DM_UUID_FLAG) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Peter Jones84c89552011-01-13 19:59:47 +0000840 if (new_data < param->data ||
841 invalid_str(new_data, (void *) param + param_size) ||
842 strlen(new_data) > (change_uuid ? DM_UUID_LEN - 1 : DM_NAME_LEN - 1)) {
843 DMWARN("Invalid new mapped device name or uuid string supplied.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return -EINVAL;
845 }
846
Peter Jones84c89552011-01-13 19:59:47 +0000847 if (!change_uuid) {
848 r = check_name(new_data);
849 if (r)
850 return r;
851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Peter Jones84c89552011-01-13 19:59:47 +0000853 md = dm_hash_rename(param, new_data);
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100854 if (IS_ERR(md))
855 return PTR_ERR(md);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +0000856
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100857 __dev_status(md, param);
858 dm_put(md);
859
860 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}
862
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800863static int dev_set_geometry(struct dm_ioctl *param, size_t param_size)
864{
865 int r = -EINVAL, x;
866 struct mapped_device *md;
867 struct hd_geometry geometry;
868 unsigned long indata[4];
869 char *geostr = (char *) param + param->data_start;
870
871 md = find_device(param);
872 if (!md)
873 return -ENXIO;
874
Alasdair G Kergon27238b22008-02-08 02:09:53 +0000875 if (geostr < param->data ||
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800876 invalid_str(geostr, (void *) param + param_size)) {
877 DMWARN("Invalid geometry supplied.");
878 goto out;
879 }
880
881 x = sscanf(geostr, "%lu %lu %lu %lu", indata,
882 indata + 1, indata + 2, indata + 3);
883
884 if (x != 4) {
885 DMWARN("Unable to interpret geometry settings.");
886 goto out;
887 }
888
889 if (indata[0] > 65535 || indata[1] > 255 ||
890 indata[2] > 255 || indata[3] > ULONG_MAX) {
891 DMWARN("Geometry exceeds range limits.");
892 goto out;
893 }
894
895 geometry.cylinders = indata[0];
896 geometry.heads = indata[1];
897 geometry.sectors = indata[2];
898 geometry.start = indata[3];
899
900 r = dm_set_geometry(md, &geometry);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800901
902 param->data_size = 0;
903
904out:
905 dm_put(md);
906 return r;
907}
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909static int do_suspend(struct dm_ioctl *param)
910{
911 int r = 0;
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -0800912 unsigned suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 struct mapped_device *md;
914
915 md = find_device(param);
916 if (!md)
917 return -ENXIO;
918
Alasdair G Kergon6da487d2006-01-06 00:20:07 -0800919 if (param->flags & DM_SKIP_LOCKFS_FLAG)
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -0800920 suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
Kiyoshi Ueda81fdb092006-12-08 02:41:07 -0800921 if (param->flags & DM_NOFLUSH_FLAG)
922 suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
Alasdair G Kergon6da487d2006-01-06 00:20:07 -0800923
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100924 if (!dm_suspended_md(md)) {
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -0800925 r = dm_suspend(md, suspend_flags);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100926 if (r)
927 goto out;
928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100930 __dev_status(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100932out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 dm_put(md);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return r;
936}
937
938static int do_resume(struct dm_ioctl *param)
939{
940 int r = 0;
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -0800941 unsigned suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 struct hash_cell *hc;
943 struct mapped_device *md;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +0000944 struct dm_table *new_map, *old_map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 down_write(&_hash_lock);
947
948 hc = __find_device_hash_cell(param);
949 if (!hc) {
Milan Broz810b4922011-01-13 19:59:55 +0000950 DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 up_write(&_hash_lock);
952 return -ENXIO;
953 }
954
955 md = hc->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 new_map = hc->new_map;
958 hc->new_map = NULL;
959 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
960
961 up_write(&_hash_lock);
962
963 /* Do we need to load a new map ? */
964 if (new_map) {
965 /* Suspend if it isn't already suspended */
Alasdair G Kergon6da487d2006-01-06 00:20:07 -0800966 if (param->flags & DM_SKIP_LOCKFS_FLAG)
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -0800967 suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
Kiyoshi Ueda81fdb092006-12-08 02:41:07 -0800968 if (param->flags & DM_NOFLUSH_FLAG)
969 suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +0000970 if (!dm_suspended_md(md))
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -0800971 dm_suspend(md, suspend_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Alasdair G Kergon042d2a92009-12-10 23:52:24 +0000973 old_map = dm_swap_table(md, new_map);
974 if (IS_ERR(old_map)) {
Mikulas Patockad5816872009-01-06 03:05:10 +0000975 dm_table_destroy(new_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 dm_put(md);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +0000977 return PTR_ERR(old_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
979
980 if (dm_table_get_mode(new_map) & FMODE_WRITE)
981 set_disk_ro(dm_disk(md), 0);
982 else
983 set_disk_ro(dm_disk(md), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
985
Mike Snitzer0f3649a2010-03-06 02:32:24 +0000986 if (dm_suspended_md(md)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 r = dm_resume(md);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +0000988 if (!r && !dm_kobject_uevent(md, KOBJ_CHANGE, param->event_nr))
989 param->flags |= DM_UEVENT_GENERATED_FLAG;
Mike Snitzer0f3649a2010-03-06 02:32:24 +0000990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Alasdair G Kergon042d2a92009-12-10 23:52:24 +0000992 if (old_map)
993 dm_table_destroy(old_map);
Milan Broz60935eb2009-06-22 10:12:30 +0100994
Mike Snitzer0f3649a2010-03-06 02:32:24 +0000995 if (!r)
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100996 __dev_status(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 dm_put(md);
999 return r;
1000}
1001
1002/*
1003 * Set or unset the suspension state of a device.
1004 * If the device already is in the requested state we just return its status.
1005 */
1006static int dev_suspend(struct dm_ioctl *param, size_t param_size)
1007{
1008 if (param->flags & DM_SUSPEND_FLAG)
1009 return do_suspend(param);
1010
1011 return do_resume(param);
1012}
1013
1014/*
1015 * Copies device info back to user space, used by
1016 * the create and info ioctls.
1017 */
1018static int dev_status(struct dm_ioctl *param, size_t param_size)
1019{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 struct mapped_device *md;
1021
1022 md = find_device(param);
1023 if (!md)
1024 return -ENXIO;
1025
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001026 __dev_status(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 dm_put(md);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001028
1029 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031
1032/*
1033 * Build up the status struct for each target
1034 */
1035static void retrieve_status(struct dm_table *table,
1036 struct dm_ioctl *param, size_t param_size)
1037{
1038 unsigned int i, num_targets;
1039 struct dm_target_spec *spec;
1040 char *outbuf, *outptr;
1041 status_type_t type;
1042 size_t remaining, len, used = 0;
1043
1044 outptr = outbuf = get_result_buffer(param, param_size, &len);
1045
1046 if (param->flags & DM_STATUS_TABLE_FLAG)
1047 type = STATUSTYPE_TABLE;
1048 else
1049 type = STATUSTYPE_INFO;
1050
1051 /* Get all the target info */
1052 num_targets = dm_table_get_num_targets(table);
1053 for (i = 0; i < num_targets; i++) {
1054 struct dm_target *ti = dm_table_get_target(table, i);
1055
1056 remaining = len - (outptr - outbuf);
1057 if (remaining <= sizeof(struct dm_target_spec)) {
1058 param->flags |= DM_BUFFER_FULL_FLAG;
1059 break;
1060 }
1061
1062 spec = (struct dm_target_spec *) outptr;
1063
1064 spec->status = 0;
1065 spec->sector_start = ti->begin;
1066 spec->length = ti->len;
1067 strncpy(spec->target_type, ti->type->name,
1068 sizeof(spec->target_type));
1069
1070 outptr += sizeof(struct dm_target_spec);
1071 remaining = len - (outptr - outbuf);
1072 if (remaining <= 0) {
1073 param->flags |= DM_BUFFER_FULL_FLAG;
1074 break;
1075 }
1076
1077 /* Get the status/table string from the target driver */
1078 if (ti->type->status) {
1079 if (ti->type->status(ti, type, outptr, remaining)) {
1080 param->flags |= DM_BUFFER_FULL_FLAG;
1081 break;
1082 }
1083 } else
1084 outptr[0] = '\0';
1085
1086 outptr += strlen(outptr) + 1;
1087 used = param->data_start + (outptr - outbuf);
1088
1089 outptr = align_ptr(outptr);
1090 spec->next = outptr - outbuf;
1091 }
1092
1093 if (used)
1094 param->data_size = used;
1095
1096 param->target_count = num_targets;
1097}
1098
1099/*
1100 * Wait for a device to report an event
1101 */
1102static int dev_wait(struct dm_ioctl *param, size_t param_size)
1103{
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001104 int r = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 struct mapped_device *md;
1106 struct dm_table *table;
1107
1108 md = find_device(param);
1109 if (!md)
1110 return -ENXIO;
1111
1112 /*
1113 * Wait for a notification event
1114 */
1115 if (dm_wait_event(md, param->event_nr)) {
1116 r = -ERESTARTSYS;
1117 goto out;
1118 }
1119
1120 /*
1121 * The userland program is going to want to know what
1122 * changed to trigger the event, so we may as well tell
1123 * him and save an ioctl.
1124 */
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001125 __dev_status(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +00001127 table = dm_get_live_or_inactive_table(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 if (table) {
1129 retrieve_status(table, param, param_size);
1130 dm_table_put(table);
1131 }
1132
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001133out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 dm_put(md);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 return r;
1137}
1138
Al Viroaeb5d722008-09-02 15:28:45 -04001139static inline fmode_t get_mode(struct dm_ioctl *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
Al Viroaeb5d722008-09-02 15:28:45 -04001141 fmode_t mode = FMODE_READ | FMODE_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143 if (param->flags & DM_READONLY_FLAG)
1144 mode = FMODE_READ;
1145
1146 return mode;
1147}
1148
1149static int next_target(struct dm_target_spec *last, uint32_t next, void *end,
1150 struct dm_target_spec **spec, char **target_params)
1151{
1152 *spec = (struct dm_target_spec *) ((unsigned char *) last + next);
1153 *target_params = (char *) (*spec + 1);
1154
1155 if (*spec < (last + 1))
1156 return -EINVAL;
1157
1158 return invalid_str(*target_params, end);
1159}
1160
1161static int populate_table(struct dm_table *table,
1162 struct dm_ioctl *param, size_t param_size)
1163{
1164 int r;
1165 unsigned int i = 0;
1166 struct dm_target_spec *spec = (struct dm_target_spec *) param;
1167 uint32_t next = param->data_start;
1168 void *end = (void *) param + param_size;
1169 char *target_params;
1170
1171 if (!param->target_count) {
1172 DMWARN("populate_table: no targets specified");
1173 return -EINVAL;
1174 }
1175
1176 for (i = 0; i < param->target_count; i++) {
1177
1178 r = next_target(spec, next, end, &spec, &target_params);
1179 if (r) {
1180 DMWARN("unable to find target");
1181 return r;
1182 }
1183
1184 r = dm_table_add_target(table, spec->target_type,
1185 (sector_t) spec->sector_start,
1186 (sector_t) spec->length,
1187 target_params);
1188 if (r) {
1189 DMWARN("error adding target to table");
1190 return r;
1191 }
1192
1193 next = spec->next;
1194 }
1195
1196 return dm_table_complete(table);
1197}
1198
1199static int table_load(struct dm_ioctl *param, size_t param_size)
1200{
1201 int r;
1202 struct hash_cell *hc;
1203 struct dm_table *t;
Mike Anderson1134e5a2006-03-27 01:17:54 -08001204 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Mike Anderson1134e5a2006-03-27 01:17:54 -08001206 md = find_device(param);
1207 if (!md)
1208 return -ENXIO;
1209
1210 r = dm_table_create(&t, get_mode(param), param->target_count, md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (r)
Mike Anderson1134e5a2006-03-27 01:17:54 -08001212 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 r = populate_table(t, param, param_size);
1215 if (r) {
Mikulas Patockaf80a5572009-03-16 17:44:26 +00001216 dm_table_destroy(t);
Mike Anderson1134e5a2006-03-27 01:17:54 -08001217 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
1219
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001220 /* Protect md->type and md->queue against concurrent table loads. */
Mike Snitzera5664da2010-08-12 04:14:01 +01001221 dm_lock_md_type(md);
1222 if (dm_get_md_type(md) == DM_TYPE_NONE)
1223 /* Initial table load: acquire type of table. */
1224 dm_set_md_type(md, dm_table_get_type(t));
1225 else if (dm_get_md_type(md) != dm_table_get_type(t)) {
1226 DMWARN("can't change device type after initial table load.");
1227 dm_table_destroy(t);
1228 dm_unlock_md_type(md);
1229 r = -EINVAL;
1230 goto out;
1231 }
Mike Snitzer4a0b4dd2010-08-12 04:14:02 +01001232
1233 /* setup md->queue to reflect md's type (may block) */
1234 r = dm_setup_md_queue(md);
1235 if (r) {
1236 DMWARN("unable to set up device queue for new table.");
1237 dm_table_destroy(t);
1238 dm_unlock_md_type(md);
1239 goto out;
1240 }
Mike Snitzera5664da2010-08-12 04:14:01 +01001241 dm_unlock_md_type(md);
1242
1243 /* stage inactive table */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 down_write(&_hash_lock);
Mike Anderson1134e5a2006-03-27 01:17:54 -08001245 hc = dm_get_mdptr(md);
1246 if (!hc || hc->md != md) {
1247 DMWARN("device has been removed from the dev hash table.");
Mikulas Patockaf80a5572009-03-16 17:44:26 +00001248 dm_table_destroy(t);
Mike Anderson1134e5a2006-03-27 01:17:54 -08001249 up_write(&_hash_lock);
1250 r = -ENXIO;
1251 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 }
1253
1254 if (hc->new_map)
Mikulas Patockad5816872009-01-06 03:05:10 +00001255 dm_table_destroy(hc->new_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 hc->new_map = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 up_write(&_hash_lock);
Mike Anderson1134e5a2006-03-27 01:17:54 -08001258
1259 param->flags |= DM_INACTIVE_PRESENT_FLAG;
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001260 __dev_status(md, param);
Mike Anderson1134e5a2006-03-27 01:17:54 -08001261
1262out:
1263 dm_put(md);
1264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 return r;
1266}
1267
1268static int table_clear(struct dm_ioctl *param, size_t param_size)
1269{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 struct hash_cell *hc;
Jeff Mahoney7ec75f22006-06-26 00:27:24 -07001271 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 down_write(&_hash_lock);
1274
1275 hc = __find_device_hash_cell(param);
1276 if (!hc) {
Milan Broz810b4922011-01-13 19:59:55 +00001277 DMDEBUG_LIMIT("device doesn't appear to be in the dev hash table.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 up_write(&_hash_lock);
1279 return -ENXIO;
1280 }
1281
1282 if (hc->new_map) {
Mikulas Patockad5816872009-01-06 03:05:10 +00001283 dm_table_destroy(hc->new_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 hc->new_map = NULL;
1285 }
1286
1287 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
1288
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001289 __dev_status(hc->md, param);
Jeff Mahoney7ec75f22006-06-26 00:27:24 -07001290 md = hc->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 up_write(&_hash_lock);
Jeff Mahoney7ec75f22006-06-26 00:27:24 -07001292 dm_put(md);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001293
1294 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295}
1296
1297/*
1298 * Retrieves a list of devices used by a particular dm device.
1299 */
1300static void retrieve_deps(struct dm_table *table,
1301 struct dm_ioctl *param, size_t param_size)
1302{
1303 unsigned int count = 0;
1304 struct list_head *tmp;
1305 size_t len, needed;
Mikulas Patocka82b15192008-10-10 13:37:09 +01001306 struct dm_dev_internal *dd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 struct dm_target_deps *deps;
1308
1309 deps = get_result_buffer(param, param_size, &len);
1310
1311 /*
1312 * Count the devices.
1313 */
1314 list_for_each (tmp, dm_table_get_devices(table))
1315 count++;
1316
1317 /*
1318 * Check we have enough space.
1319 */
1320 needed = sizeof(*deps) + (sizeof(*deps->dev) * count);
1321 if (len < needed) {
1322 param->flags |= DM_BUFFER_FULL_FLAG;
1323 return;
1324 }
1325
1326 /*
1327 * Fill in the devices.
1328 */
1329 deps->count = count;
1330 count = 0;
1331 list_for_each_entry (dd, dm_table_get_devices(table), list)
Mikulas Patocka82b15192008-10-10 13:37:09 +01001332 deps->dev[count++] = huge_encode_dev(dd->dm_dev.bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 param->data_size = param->data_start + needed;
1335}
1336
1337static int table_deps(struct dm_ioctl *param, size_t param_size)
1338{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 struct mapped_device *md;
1340 struct dm_table *table;
1341
1342 md = find_device(param);
1343 if (!md)
1344 return -ENXIO;
1345
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001346 __dev_status(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +00001348 table = dm_get_live_or_inactive_table(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 if (table) {
1350 retrieve_deps(table, param, param_size);
1351 dm_table_put(table);
1352 }
1353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 dm_put(md);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001355
1356 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357}
1358
1359/*
1360 * Return the status of a device as a text string for each
1361 * target.
1362 */
1363static int table_status(struct dm_ioctl *param, size_t param_size)
1364{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 struct mapped_device *md;
1366 struct dm_table *table;
1367
1368 md = find_device(param);
1369 if (!md)
1370 return -ENXIO;
1371
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001372 __dev_status(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +00001374 table = dm_get_live_or_inactive_table(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 if (table) {
1376 retrieve_status(table, param, param_size);
1377 dm_table_put(table);
1378 }
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 dm_put(md);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001381
1382 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383}
1384
1385/*
1386 * Pass a message to the target that's at the supplied device offset.
1387 */
1388static int target_message(struct dm_ioctl *param, size_t param_size)
1389{
1390 int r, argc;
1391 char **argv;
1392 struct mapped_device *md;
1393 struct dm_table *table;
1394 struct dm_target *ti;
1395 struct dm_target_msg *tmsg = (void *) param + param->data_start;
1396
1397 md = find_device(param);
1398 if (!md)
1399 return -ENXIO;
1400
Milan Broz027d50f2007-10-19 22:38:36 +01001401 if (tmsg < (struct dm_target_msg *) param->data ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 invalid_str(tmsg->message, (void *) param + param_size)) {
1403 DMWARN("Invalid target message parameters.");
1404 r = -EINVAL;
1405 goto out;
1406 }
1407
1408 r = dm_split_args(&argc, &argv, tmsg->message);
1409 if (r) {
1410 DMWARN("Failed to split target message parameters");
1411 goto out;
1412 }
1413
Alasdair G Kergon2ca4c922011-08-02 12:32:03 +01001414 if (!argc) {
1415 DMWARN("Empty message received.");
1416 goto out;
1417 }
1418
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001419 table = dm_get_live_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 if (!table)
1421 goto out_argv;
1422
Mike Andersonc50abeb2009-12-10 23:52:20 +00001423 if (dm_deleting_md(md)) {
1424 r = -ENXIO;
1425 goto out_table;
1426 }
1427
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001428 ti = dm_table_find_target(table, tmsg->sector);
1429 if (!dm_target_is_valid(ti)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 DMWARN("Target message sector outside device.");
1431 r = -EINVAL;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001432 } else if (ti->type->message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 r = ti->type->message(ti, argc, argv);
1434 else {
1435 DMWARN("Target type does not support messages");
1436 r = -EINVAL;
1437 }
1438
Mike Andersonc50abeb2009-12-10 23:52:20 +00001439 out_table:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 dm_table_put(table);
1441 out_argv:
1442 kfree(argv);
1443 out:
1444 param->data_size = 0;
1445 dm_put(md);
1446 return r;
1447}
1448
1449/*-----------------------------------------------------------------
1450 * Implementation of open/close/ioctl on the special char
1451 * device.
1452 *---------------------------------------------------------------*/
1453static ioctl_fn lookup_ioctl(unsigned int cmd)
1454{
1455 static struct {
1456 int cmd;
1457 ioctl_fn fn;
1458 } _ioctls[] = {
1459 {DM_VERSION_CMD, NULL}, /* version is dealt with elsewhere */
1460 {DM_REMOVE_ALL_CMD, remove_all},
1461 {DM_LIST_DEVICES_CMD, list_devices},
1462
1463 {DM_DEV_CREATE_CMD, dev_create},
1464 {DM_DEV_REMOVE_CMD, dev_remove},
1465 {DM_DEV_RENAME_CMD, dev_rename},
1466 {DM_DEV_SUSPEND_CMD, dev_suspend},
1467 {DM_DEV_STATUS_CMD, dev_status},
1468 {DM_DEV_WAIT_CMD, dev_wait},
1469
1470 {DM_TABLE_LOAD_CMD, table_load},
1471 {DM_TABLE_CLEAR_CMD, table_clear},
1472 {DM_TABLE_DEPS_CMD, table_deps},
1473 {DM_TABLE_STATUS_CMD, table_status},
1474
1475 {DM_LIST_VERSIONS_CMD, list_versions},
1476
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001477 {DM_TARGET_MSG_CMD, target_message},
1478 {DM_DEV_SET_GEOMETRY_CMD, dev_set_geometry}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 };
1480
1481 return (cmd >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[cmd].fn;
1482}
1483
1484/*
1485 * As well as checking the version compatibility this always
1486 * copies the kernel interface version out.
1487 */
1488static int check_version(unsigned int cmd, struct dm_ioctl __user *user)
1489{
1490 uint32_t version[3];
1491 int r = 0;
1492
1493 if (copy_from_user(version, user->version, sizeof(version)))
1494 return -EFAULT;
1495
1496 if ((DM_VERSION_MAJOR != version[0]) ||
1497 (DM_VERSION_MINOR < version[1])) {
1498 DMWARN("ioctl interface mismatch: "
1499 "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
1500 DM_VERSION_MAJOR, DM_VERSION_MINOR,
1501 DM_VERSION_PATCHLEVEL,
1502 version[0], version[1], version[2], cmd);
1503 r = -EINVAL;
1504 }
1505
1506 /*
1507 * Fill in the kernel version.
1508 */
1509 version[0] = DM_VERSION_MAJOR;
1510 version[1] = DM_VERSION_MINOR;
1511 version[2] = DM_VERSION_PATCHLEVEL;
1512 if (copy_to_user(user->version, version, sizeof(version)))
1513 return -EFAULT;
1514
1515 return r;
1516}
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl **param)
1519{
1520 struct dm_ioctl tmp, *dmi;
Milan Brozf8681202011-03-24 13:54:30 +00001521 int secure_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Milan Broz76c072b2008-02-08 02:09:56 +00001523 if (copy_from_user(&tmp, user, sizeof(tmp) - sizeof(tmp.data)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 return -EFAULT;
1525
Milan Broz76c072b2008-02-08 02:09:56 +00001526 if (tmp.data_size < (sizeof(tmp) - sizeof(tmp.data)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 return -EINVAL;
1528
Milan Brozf8681202011-03-24 13:54:30 +00001529 secure_data = tmp.flags & DM_SECURE_DATA_FLAG;
1530
Jesper Juhlbb56acf2007-10-19 22:38:54 +01001531 dmi = vmalloc(tmp.data_size);
Milan Brozf8681202011-03-24 13:54:30 +00001532 if (!dmi) {
1533 if (secure_data && clear_user(user, tmp.data_size))
1534 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 return -ENOMEM;
Milan Brozf8681202011-03-24 13:54:30 +00001536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Milan Broz6bb43b52011-03-24 13:54:28 +00001538 if (copy_from_user(dmi, user, tmp.data_size))
1539 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Milan Brozf8681202011-03-24 13:54:30 +00001541 /* Wipe the user buffer so we do not return it to userspace */
1542 if (secure_data && clear_user(user, tmp.data_size))
1543 goto bad;
1544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 *param = dmi;
1546 return 0;
Milan Broz6bb43b52011-03-24 13:54:28 +00001547
1548bad:
Milan Brozf8681202011-03-24 13:54:30 +00001549 if (secure_data)
1550 memset(dmi, 0, tmp.data_size);
Milan Broz6bb43b52011-03-24 13:54:28 +00001551 vfree(dmi);
1552 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553}
1554
1555static int validate_params(uint cmd, struct dm_ioctl *param)
1556{
1557 /* Always clear this flag */
1558 param->flags &= ~DM_BUFFER_FULL_FLAG;
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00001559 param->flags &= ~DM_UEVENT_GENERATED_FLAG;
Milan Brozf8681202011-03-24 13:54:30 +00001560 param->flags &= ~DM_SECURE_DATA_FLAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 /* Ignores parameters */
1563 if (cmd == DM_REMOVE_ALL_CMD ||
1564 cmd == DM_LIST_DEVICES_CMD ||
1565 cmd == DM_LIST_VERSIONS_CMD)
1566 return 0;
1567
1568 if ((cmd == DM_DEV_CREATE_CMD)) {
1569 if (!*param->name) {
1570 DMWARN("name not supplied when creating device");
1571 return -EINVAL;
1572 }
1573 } else if ((*param->uuid && *param->name)) {
1574 DMWARN("only supply one of name or uuid, cmd(%u)", cmd);
1575 return -EINVAL;
1576 }
1577
1578 /* Ensure strings are terminated */
1579 param->name[DM_NAME_LEN - 1] = '\0';
1580 param->uuid[DM_UUID_LEN - 1] = '\0';
1581
1582 return 0;
1583}
1584
Alasdair G Kergon27238b22008-02-08 02:09:53 +00001585static int ctl_ioctl(uint command, struct dm_ioctl __user *user)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
1587 int r = 0;
Milan Brozf8681202011-03-24 13:54:30 +00001588 int wipe_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 unsigned int cmd;
Andrew Mortona26ffd42008-02-08 02:10:16 +00001590 struct dm_ioctl *uninitialized_var(param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 ioctl_fn fn = NULL;
Milan Broz6bb43b52011-03-24 13:54:28 +00001592 size_t input_param_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
1594 /* only root can play with this */
1595 if (!capable(CAP_SYS_ADMIN))
1596 return -EACCES;
1597
1598 if (_IOC_TYPE(command) != DM_IOCTL)
1599 return -ENOTTY;
1600
1601 cmd = _IOC_NR(command);
1602
1603 /*
1604 * Check the interface version passed in. This also
1605 * writes out the kernel's interface version.
1606 */
1607 r = check_version(cmd, user);
1608 if (r)
1609 return r;
1610
1611 /*
1612 * Nothing more to do for the version command.
1613 */
1614 if (cmd == DM_VERSION_CMD)
1615 return 0;
1616
1617 fn = lookup_ioctl(cmd);
1618 if (!fn) {
1619 DMWARN("dm_ctl_ioctl: unknown command 0x%x", command);
1620 return -ENOTTY;
1621 }
1622
1623 /*
1624 * Trying to avoid low memory issues when a device is
1625 * suspended.
1626 */
1627 current->flags |= PF_MEMALLOC;
1628
1629 /*
1630 * Copy the parameters into kernel space.
1631 */
1632 r = copy_params(user, &param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Alasdair G Kergondab6a422006-02-01 03:04:52 -08001634 current->flags &= ~PF_MEMALLOC;
1635
1636 if (r)
1637 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Milan Brozf8681202011-03-24 13:54:30 +00001639 input_param_size = param->data_size;
1640 wipe_buffer = param->flags & DM_SECURE_DATA_FLAG;
1641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 r = validate_params(cmd, param);
1643 if (r)
1644 goto out;
1645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 param->data_size = sizeof(*param);
Milan Broz6bb43b52011-03-24 13:54:28 +00001647 r = fn(param, input_param_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
1649 /*
1650 * Copy the results back to userland.
1651 */
1652 if (!r && copy_to_user(user, param, param->data_size))
1653 r = -EFAULT;
1654
Milan Broz6bb43b52011-03-24 13:54:28 +00001655out:
Milan Brozf8681202011-03-24 13:54:30 +00001656 if (wipe_buffer)
1657 memset(param, 0, input_param_size);
1658
Milan Broz6bb43b52011-03-24 13:54:28 +00001659 vfree(param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 return r;
1661}
1662
Alasdair G Kergon27238b22008-02-08 02:09:53 +00001663static long dm_ctl_ioctl(struct file *file, uint command, ulong u)
1664{
1665 return (long)ctl_ioctl(command, (struct dm_ioctl __user *)u);
1666}
1667
Milan Broz76c072b2008-02-08 02:09:56 +00001668#ifdef CONFIG_COMPAT
1669static long dm_compat_ctl_ioctl(struct file *file, uint command, ulong u)
1670{
1671 return (long)dm_ctl_ioctl(file, command, (ulong) compat_ptr(u));
1672}
1673#else
1674#define dm_compat_ctl_ioctl NULL
1675#endif
1676
Arjan van de Venfa027c22007-02-12 00:55:33 -08001677static const struct file_operations _ctl_fops = {
Arnd Bergmann402ab352010-08-12 04:13:57 +01001678 .open = nonseekable_open,
Alasdair G Kergon27238b22008-02-08 02:09:53 +00001679 .unlocked_ioctl = dm_ctl_ioctl,
Milan Broz76c072b2008-02-08 02:09:56 +00001680 .compat_ioctl = dm_compat_ctl_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 .owner = THIS_MODULE,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001682 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683};
1684
1685static struct miscdevice _dm_misc = {
Peter Rajnoha7e507eb2010-08-12 04:14:05 +01001686 .minor = MAPPER_CTRL_MINOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 .name = DM_NAME,
Peter Rajnoha7e507eb2010-08-12 04:14:05 +01001688 .nodename = DM_DIR "/" DM_CONTROL_NODE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 .fops = &_ctl_fops
1690};
1691
Peter Rajnoha7e507eb2010-08-12 04:14:05 +01001692MODULE_ALIAS_MISCDEV(MAPPER_CTRL_MINOR);
1693MODULE_ALIAS("devname:" DM_DIR "/" DM_CONTROL_NODE);
1694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695/*
1696 * Create misc character device and link to DM_DIR/control.
1697 */
1698int __init dm_interface_init(void)
1699{
1700 int r;
1701
1702 r = dm_hash_init();
1703 if (r)
1704 return r;
1705
1706 r = misc_register(&_dm_misc);
1707 if (r) {
1708 DMERR("misc_register failed for control device");
1709 dm_hash_exit();
1710 return r;
1711 }
1712
1713 DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR,
1714 DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA,
1715 DM_DRIVER_EMAIL);
1716 return 0;
1717}
1718
1719void dm_interface_exit(void)
1720{
1721 if (misc_deregister(&_dm_misc) < 0)
1722 DMERR("misc_deregister failed for control device");
1723
1724 dm_hash_exit();
1725}
Mike Anderson96a1f7d2007-10-19 22:47:59 +01001726
1727/**
1728 * dm_copy_name_and_uuid - Copy mapped device name & uuid into supplied buffers
1729 * @md: Pointer to mapped_device
1730 * @name: Buffer (size DM_NAME_LEN) for name
1731 * @uuid: Buffer (size DM_UUID_LEN) for uuid or empty string if uuid not defined
1732 */
1733int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid)
1734{
1735 int r = 0;
1736 struct hash_cell *hc;
1737
1738 if (!md)
1739 return -ENXIO;
1740
Mikulas Patocka60769052009-12-10 23:51:52 +00001741 mutex_lock(&dm_hash_cells_mutex);
Mike Anderson96a1f7d2007-10-19 22:47:59 +01001742 hc = dm_get_mdptr(md);
1743 if (!hc || hc->md != md) {
1744 r = -ENXIO;
1745 goto out;
1746 }
1747
Milan Broz23d39f62009-01-06 03:05:04 +00001748 if (name)
1749 strcpy(name, hc->name);
1750 if (uuid)
1751 strcpy(uuid, hc->uuid ? : "");
Mike Anderson96a1f7d2007-10-19 22:47:59 +01001752
1753out:
Mikulas Patocka60769052009-12-10 23:51:52 +00001754 mutex_unlock(&dm_hash_cells_mutex);
Mike Anderson96a1f7d2007-10-19 22:47:59 +01001755
1756 return r;
1757}