blob: d7500e1c26f24dc878bff7bfde6c16d1ac872245 [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{
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700252 int i, dev_skipped, dev_removed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 struct hash_cell *hc;
254 struct list_head *tmp, *n;
255
256 down_write(&_hash_lock);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700257
258retry:
259 dev_skipped = dev_removed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 for (i = 0; i < NUM_BUCKETS; i++) {
261 list_for_each_safe (tmp, n, _name_buckets + i) {
262 hc = list_entry(tmp, struct hash_cell, name_list);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700263
264 if (keep_open_devices &&
265 dm_lock_for_deletion(hc->md)) {
266 dev_skipped++;
267 continue;
268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 __hash_remove(hc);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700270 dev_removed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272 }
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700273
274 /*
275 * Some mapped devices may be using other mapped devices, so if any
276 * still exist, repeat until we make no further progress.
277 */
278 if (dev_skipped) {
279 if (dev_removed)
280 goto retry;
281
282 DMWARN("remove_all left %d open device(s)", dev_skipped);
283 }
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 up_write(&_hash_lock);
286}
287
Peter Rajnoha3abf85b2010-03-06 02:32:31 +0000288static int dm_hash_rename(uint32_t cookie, uint32_t *flags, const char *old,
289 const char *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 char *new_name, *old_name;
292 struct hash_cell *hc;
goggin, edward81f17772006-01-06 00:20:01 -0800293 struct dm_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 /*
296 * duplicate new.
297 */
Paulo Marques543537b2005-06-23 00:09:02 -0700298 new_name = kstrdup(new, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (!new_name)
300 return -ENOMEM;
301
302 down_write(&_hash_lock);
303
304 /*
305 * Is new free ?
306 */
307 hc = __get_name_cell(new);
308 if (hc) {
309 DMWARN("asked to rename to an already existing name %s -> %s",
310 old, new);
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700311 dm_put(hc->md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 up_write(&_hash_lock);
313 kfree(new_name);
314 return -EBUSY;
315 }
316
317 /*
318 * Is there such a device as 'old' ?
319 */
320 hc = __get_name_cell(old);
321 if (!hc) {
322 DMWARN("asked to rename a non existent device %s -> %s",
323 old, new);
324 up_write(&_hash_lock);
325 kfree(new_name);
326 return -ENXIO;
327 }
328
329 /*
330 * rename and move the name cell.
331 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 list_del(&hc->name_list);
333 old_name = hc->name;
Mikulas Patocka60769052009-12-10 23:51:52 +0000334 mutex_lock(&dm_hash_cells_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 hc->name = new_name;
Mikulas Patocka60769052009-12-10 23:51:52 +0000336 mutex_unlock(&dm_hash_cells_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 list_add(&hc->name_list, _name_buckets + hash_str(new_name));
338
goggin, edward81f17772006-01-06 00:20:01 -0800339 /*
340 * Wake up any dm event waiters.
341 */
Alasdair G Kergon7c666412009-12-10 23:52:19 +0000342 table = dm_get_live_table(hc->md);
goggin, edward81f17772006-01-06 00:20:01 -0800343 if (table) {
344 dm_table_event(table);
345 dm_table_put(table);
346 }
347
Peter Rajnoha3abf85b2010-03-06 02:32:31 +0000348 if (!dm_kobject_uevent(hc->md, KOBJ_CHANGE, cookie))
349 *flags |= DM_UEVENT_GENERATED_FLAG;
Alasdair G Kergon69267a32007-12-13 14:15:57 +0000350
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700351 dm_put(hc->md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 up_write(&_hash_lock);
353 kfree(old_name);
354 return 0;
355}
356
357/*-----------------------------------------------------------------
358 * Implementation of the ioctl commands
359 *---------------------------------------------------------------*/
360/*
361 * All the ioctl commands get dispatched to functions with this
362 * prototype.
363 */
364typedef int (*ioctl_fn)(struct dm_ioctl *param, size_t param_size);
365
366static int remove_all(struct dm_ioctl *param, size_t param_size)
367{
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700368 dm_hash_remove_all(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 param->data_size = 0;
370 return 0;
371}
372
373/*
374 * Round up the ptr to an 8-byte boundary.
375 */
376#define ALIGN_MASK 7
377static inline void *align_ptr(void *ptr)
378{
379 return (void *) (((size_t) (ptr + ALIGN_MASK)) & ~ALIGN_MASK);
380}
381
382/*
383 * Retrieves the data payload buffer from an already allocated
384 * struct dm_ioctl.
385 */
386static void *get_result_buffer(struct dm_ioctl *param, size_t param_size,
387 size_t *len)
388{
389 param->data_start = align_ptr(param + 1) - (void *) param;
390
391 if (param->data_start < param_size)
392 *len = param_size - param->data_start;
393 else
394 *len = 0;
395
396 return ((void *) param) + param->data_start;
397}
398
399static int list_devices(struct dm_ioctl *param, size_t param_size)
400{
401 unsigned int i;
402 struct hash_cell *hc;
403 size_t len, needed = 0;
404 struct gendisk *disk;
405 struct dm_name_list *nl, *old_nl = NULL;
406
407 down_write(&_hash_lock);
408
409 /*
410 * Loop through all the devices working out how much
411 * space we need.
412 */
413 for (i = 0; i < NUM_BUCKETS; i++) {
414 list_for_each_entry (hc, _name_buckets + i, name_list) {
415 needed += sizeof(struct dm_name_list);
416 needed += strlen(hc->name) + 1;
417 needed += ALIGN_MASK;
418 }
419 }
420
421 /*
422 * Grab our output buffer.
423 */
424 nl = get_result_buffer(param, param_size, &len);
425 if (len < needed) {
426 param->flags |= DM_BUFFER_FULL_FLAG;
427 goto out;
428 }
429 param->data_size = param->data_start + needed;
430
431 nl->dev = 0; /* Flags no data */
432
433 /*
434 * Now loop through filling out the names.
435 */
436 for (i = 0; i < NUM_BUCKETS; i++) {
437 list_for_each_entry (hc, _name_buckets + i, name_list) {
438 if (old_nl)
439 old_nl->next = (uint32_t) ((void *) nl -
440 (void *) old_nl);
441 disk = dm_disk(hc->md);
Tejun Heof331c022008-09-03 09:01:48 +0200442 nl->dev = huge_encode_dev(disk_devt(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 nl->next = 0;
444 strcpy(nl->name, hc->name);
445
446 old_nl = nl;
447 nl = align_ptr(((void *) ++nl) + strlen(hc->name) + 1);
448 }
449 }
450
451 out:
452 up_write(&_hash_lock);
453 return 0;
454}
455
456static void list_version_get_needed(struct target_type *tt, void *needed_param)
457{
458 size_t *needed = needed_param;
459
Alasdair G Kergonc4cc6632005-11-21 21:32:33 -0800460 *needed += sizeof(struct dm_target_versions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 *needed += strlen(tt->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 *needed += ALIGN_MASK;
463}
464
465static void list_version_get_info(struct target_type *tt, void *param)
466{
467 struct vers_iter *info = param;
468
469 /* Check space - it might have changed since the first iteration */
470 if ((char *)info->vers + sizeof(tt->version) + strlen(tt->name) + 1 >
471 info->end) {
472
473 info->flags = DM_BUFFER_FULL_FLAG;
474 return;
475 }
476
477 if (info->old_vers)
478 info->old_vers->next = (uint32_t) ((void *)info->vers -
479 (void *)info->old_vers);
480 info->vers->version[0] = tt->version[0];
481 info->vers->version[1] = tt->version[1];
482 info->vers->version[2] = tt->version[2];
483 info->vers->next = 0;
484 strcpy(info->vers->name, tt->name);
485
486 info->old_vers = info->vers;
487 info->vers = align_ptr(((void *) ++info->vers) + strlen(tt->name) + 1);
488}
489
490static int list_versions(struct dm_ioctl *param, size_t param_size)
491{
492 size_t len, needed = 0;
493 struct dm_target_versions *vers;
494 struct vers_iter iter_info;
495
496 /*
497 * Loop through all the devices working out how much
498 * space we need.
499 */
500 dm_target_iterate(list_version_get_needed, &needed);
501
502 /*
503 * Grab our output buffer.
504 */
505 vers = get_result_buffer(param, param_size, &len);
506 if (len < needed) {
507 param->flags |= DM_BUFFER_FULL_FLAG;
508 goto out;
509 }
510 param->data_size = param->data_start + needed;
511
512 iter_info.param_size = param_size;
513 iter_info.old_vers = NULL;
514 iter_info.vers = vers;
515 iter_info.flags = 0;
516 iter_info.end = (char *)vers+len;
517
518 /*
519 * Now loop through filling out the names & versions.
520 */
521 dm_target_iterate(list_version_get_info, &iter_info);
522 param->flags |= iter_info.flags;
523
524 out:
525 return 0;
526}
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528static int check_name(const char *name)
529{
530 if (strchr(name, '/')) {
531 DMWARN("invalid device name");
532 return -EINVAL;
533 }
534
535 return 0;
536}
537
538/*
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000539 * On successful return, the caller must not attempt to acquire
540 * _hash_lock without first calling dm_table_put, because dm_table_destroy
541 * waits for this dm_table_put and could be called under this lock.
542 */
543static struct dm_table *dm_get_inactive_table(struct mapped_device *md)
544{
545 struct hash_cell *hc;
546 struct dm_table *table = NULL;
547
548 down_read(&_hash_lock);
549 hc = dm_get_mdptr(md);
550 if (!hc || hc->md != md) {
551 DMWARN("device has been removed from the dev hash table.");
552 goto out;
553 }
554
555 table = hc->new_map;
556 if (table)
557 dm_table_get(table);
558
559out:
560 up_read(&_hash_lock);
561
562 return table;
563}
564
565static struct dm_table *dm_get_live_or_inactive_table(struct mapped_device *md,
566 struct dm_ioctl *param)
567{
568 return (param->flags & DM_QUERY_INACTIVE_TABLE_FLAG) ?
569 dm_get_inactive_table(md) : dm_get_live_table(md);
570}
571
572/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 * Fills in a dm_ioctl structure, ready for sending back to
574 * userland.
575 */
576static int __dev_status(struct mapped_device *md, struct dm_ioctl *param)
577{
578 struct gendisk *disk = dm_disk(md);
579 struct dm_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 param->flags &= ~(DM_SUSPEND_FLAG | DM_READONLY_FLAG |
582 DM_ACTIVE_PRESENT_FLAG);
583
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +0000584 if (dm_suspended_md(md))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 param->flags |= DM_SUSPEND_FLAG;
586
Tejun Heof331c022008-09-03 09:01:48 +0200587 param->dev = huge_encode_dev(disk_devt(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700589 /*
590 * Yes, this will be out of date by the time it gets back
591 * to userland, but it is still very useful for
592 * debugging.
593 */
594 param->open_count = dm_open_count(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 param->event_nr = dm_get_event_nr(md);
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000597 param->target_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Alasdair G Kergon7c666412009-12-10 23:52:19 +0000599 table = dm_get_live_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (table) {
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000601 if (!(param->flags & DM_QUERY_INACTIVE_TABLE_FLAG)) {
602 if (get_disk_ro(disk))
603 param->flags |= DM_READONLY_FLAG;
604 param->target_count = dm_table_get_num_targets(table);
605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 dm_table_put(table);
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000607
608 param->flags |= DM_ACTIVE_PRESENT_FLAG;
609 }
610
611 if (param->flags & DM_QUERY_INACTIVE_TABLE_FLAG) {
612 table = dm_get_inactive_table(md);
613 if (table) {
614 if (!(dm_table_get_mode(table) & FMODE_WRITE))
615 param->flags |= DM_READONLY_FLAG;
616 param->target_count = dm_table_get_num_targets(table);
617 dm_table_put(table);
618 }
619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 return 0;
622}
623
624static int dev_create(struct dm_ioctl *param, size_t param_size)
625{
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700626 int r, m = DM_ANY_MINOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 struct mapped_device *md;
628
629 r = check_name(param->name);
630 if (r)
631 return r;
632
633 if (param->flags & DM_PERSISTENT_DEV_FLAG)
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700634 m = MINOR(huge_decode_dev(param->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700636 r = dm_create(m, &md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (r)
638 return r;
639
640 r = dm_hash_insert(param->name, *param->uuid ? param->uuid : NULL, md);
641 if (r) {
642 dm_put(md);
643 return r;
644 }
645
646 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
647
648 r = __dev_status(md, param);
649 dm_put(md);
650
651 return r;
652}
653
654/*
655 * Always use UUID for lookups if it's present, otherwise use name or dev.
656 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800657static struct hash_cell *__find_device_hash_cell(struct dm_ioctl *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -0800659 struct mapped_device *md;
660 void *mdptr = NULL;
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (*param->uuid)
663 return __get_uuid_cell(param->uuid);
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -0800664
665 if (*param->name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return __get_name_cell(param->name);
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -0800667
668 md = dm_get_md(huge_decode_dev(param->dev));
Alasdair G Kergonbfc5ecd2006-11-08 17:44:42 -0800669 if (!md)
670 goto out;
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -0800671
Alasdair G Kergonbfc5ecd2006-11-08 17:44:42 -0800672 mdptr = dm_get_mdptr(md);
673 if (!mdptr)
674 dm_put(md);
675
676out:
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -0800677 return mdptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
Arjan van de Ven858119e2006-01-14 13:20:43 -0800680static struct mapped_device *find_device(struct dm_ioctl *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
682 struct hash_cell *hc;
683 struct mapped_device *md = NULL;
684
685 down_read(&_hash_lock);
686 hc = __find_device_hash_cell(param);
687 if (hc) {
688 md = hc->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 /*
691 * Sneakily write in both the name and the uuid
692 * while we have the cell.
693 */
Roel Kluina518b862009-12-10 23:52:07 +0000694 strlcpy(param->name, hc->name, sizeof(param->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (hc->uuid)
Roel Kluina518b862009-12-10 23:52:07 +0000696 strlcpy(param->uuid, hc->uuid, sizeof(param->uuid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 else
698 param->uuid[0] = '\0';
699
700 if (hc->new_map)
701 param->flags |= DM_INACTIVE_PRESENT_FLAG;
702 else
703 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
704 }
705 up_read(&_hash_lock);
706
707 return md;
708}
709
710static int dev_remove(struct dm_ioctl *param, size_t param_size)
711{
712 struct hash_cell *hc;
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700713 struct mapped_device *md;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700714 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 down_write(&_hash_lock);
717 hc = __find_device_hash_cell(param);
718
719 if (!hc) {
720 DMWARN("device doesn't appear to be in the dev hash table.");
721 up_write(&_hash_lock);
722 return -ENXIO;
723 }
724
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700725 md = hc->md;
726
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700727 /*
728 * Ensure the device is not open and nothing further can open it.
729 */
730 r = dm_lock_for_deletion(md);
731 if (r) {
732 DMWARN("unable to remove open device %s", hc->name);
733 up_write(&_hash_lock);
734 dm_put(md);
735 return r;
736 }
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 __hash_remove(hc);
739 up_write(&_hash_lock);
Milan Broz60935eb2009-06-22 10:12:30 +0100740
Peter Rajnoha3abf85b2010-03-06 02:32:31 +0000741 if (!dm_kobject_uevent(md, KOBJ_REMOVE, param->event_nr))
742 param->flags |= DM_UEVENT_GENERATED_FLAG;
Milan Broz60935eb2009-06-22 10:12:30 +0100743
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700744 dm_put(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return 0;
746}
747
748/*
749 * Check a string doesn't overrun the chunk of
750 * memory we copied from userland.
751 */
752static int invalid_str(char *str, void *end)
753{
754 while ((void *) str < end)
755 if (!*str++)
756 return 0;
757
758 return -EINVAL;
759}
760
761static int dev_rename(struct dm_ioctl *param, size_t param_size)
762{
763 int r;
764 char *new_name = (char *) param + param->data_start;
765
Alasdair G Kergon27238b22008-02-08 02:09:53 +0000766 if (new_name < param->data ||
Milan Brozbc0fd672009-03-16 16:56:01 +0000767 invalid_str(new_name, (void *) param + param_size) ||
768 strlen(new_name) > DM_NAME_LEN - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 DMWARN("Invalid new logical volume name supplied.");
770 return -EINVAL;
771 }
772
773 r = check_name(new_name);
774 if (r)
775 return r;
776
777 param->data_size = 0;
Peter Rajnoha3abf85b2010-03-06 02:32:31 +0000778
779 return dm_hash_rename(param->event_nr, &param->flags, param->name,
780 new_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800783static int dev_set_geometry(struct dm_ioctl *param, size_t param_size)
784{
785 int r = -EINVAL, x;
786 struct mapped_device *md;
787 struct hd_geometry geometry;
788 unsigned long indata[4];
789 char *geostr = (char *) param + param->data_start;
790
791 md = find_device(param);
792 if (!md)
793 return -ENXIO;
794
Alasdair G Kergon27238b22008-02-08 02:09:53 +0000795 if (geostr < param->data ||
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800796 invalid_str(geostr, (void *) param + param_size)) {
797 DMWARN("Invalid geometry supplied.");
798 goto out;
799 }
800
801 x = sscanf(geostr, "%lu %lu %lu %lu", indata,
802 indata + 1, indata + 2, indata + 3);
803
804 if (x != 4) {
805 DMWARN("Unable to interpret geometry settings.");
806 goto out;
807 }
808
809 if (indata[0] > 65535 || indata[1] > 255 ||
810 indata[2] > 255 || indata[3] > ULONG_MAX) {
811 DMWARN("Geometry exceeds range limits.");
812 goto out;
813 }
814
815 geometry.cylinders = indata[0];
816 geometry.heads = indata[1];
817 geometry.sectors = indata[2];
818 geometry.start = indata[3];
819
820 r = dm_set_geometry(md, &geometry);
821 if (!r)
822 r = __dev_status(md, param);
823
824 param->data_size = 0;
825
826out:
827 dm_put(md);
828 return r;
829}
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831static int do_suspend(struct dm_ioctl *param)
832{
833 int r = 0;
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -0800834 unsigned suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 struct mapped_device *md;
836
837 md = find_device(param);
838 if (!md)
839 return -ENXIO;
840
Alasdair G Kergon6da487d2006-01-06 00:20:07 -0800841 if (param->flags & DM_SKIP_LOCKFS_FLAG)
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -0800842 suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
Kiyoshi Ueda81fdb092006-12-08 02:41:07 -0800843 if (param->flags & DM_NOFLUSH_FLAG)
844 suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
Alasdair G Kergon6da487d2006-01-06 00:20:07 -0800845
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +0000846 if (!dm_suspended_md(md))
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -0800847 r = dm_suspend(md, suspend_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 if (!r)
850 r = __dev_status(md, param);
851
852 dm_put(md);
853 return r;
854}
855
856static int do_resume(struct dm_ioctl *param)
857{
858 int r = 0;
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -0800859 unsigned suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 struct hash_cell *hc;
861 struct mapped_device *md;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +0000862 struct dm_table *new_map, *old_map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 down_write(&_hash_lock);
865
866 hc = __find_device_hash_cell(param);
867 if (!hc) {
868 DMWARN("device doesn't appear to be in the dev hash table.");
869 up_write(&_hash_lock);
870 return -ENXIO;
871 }
872
873 md = hc->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 new_map = hc->new_map;
876 hc->new_map = NULL;
877 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
878
879 up_write(&_hash_lock);
880
881 /* Do we need to load a new map ? */
882 if (new_map) {
883 /* Suspend if it isn't already suspended */
Alasdair G Kergon6da487d2006-01-06 00:20:07 -0800884 if (param->flags & DM_SKIP_LOCKFS_FLAG)
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -0800885 suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
Kiyoshi Ueda81fdb092006-12-08 02:41:07 -0800886 if (param->flags & DM_NOFLUSH_FLAG)
887 suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +0000888 if (!dm_suspended_md(md))
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -0800889 dm_suspend(md, suspend_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Alasdair G Kergon042d2a92009-12-10 23:52:24 +0000891 old_map = dm_swap_table(md, new_map);
892 if (IS_ERR(old_map)) {
Mikulas Patockad5816872009-01-06 03:05:10 +0000893 dm_table_destroy(new_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 dm_put(md);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +0000895 return PTR_ERR(old_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
897
898 if (dm_table_get_mode(new_map) & FMODE_WRITE)
899 set_disk_ro(dm_disk(md), 0);
900 else
901 set_disk_ro(dm_disk(md), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903
Mike Snitzer0f3649a2010-03-06 02:32:24 +0000904 if (dm_suspended_md(md)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 r = dm_resume(md);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +0000906 if (!r && !dm_kobject_uevent(md, KOBJ_CHANGE, param->event_nr))
907 param->flags |= DM_UEVENT_GENERATED_FLAG;
Mike Snitzer0f3649a2010-03-06 02:32:24 +0000908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Alasdair G Kergon042d2a92009-12-10 23:52:24 +0000910 if (old_map)
911 dm_table_destroy(old_map);
Milan Broz60935eb2009-06-22 10:12:30 +0100912
Mike Snitzer0f3649a2010-03-06 02:32:24 +0000913 if (!r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 r = __dev_status(md, param);
915
916 dm_put(md);
917 return r;
918}
919
920/*
921 * Set or unset the suspension state of a device.
922 * If the device already is in the requested state we just return its status.
923 */
924static int dev_suspend(struct dm_ioctl *param, size_t param_size)
925{
926 if (param->flags & DM_SUSPEND_FLAG)
927 return do_suspend(param);
928
929 return do_resume(param);
930}
931
932/*
933 * Copies device info back to user space, used by
934 * the create and info ioctls.
935 */
936static int dev_status(struct dm_ioctl *param, size_t param_size)
937{
938 int r;
939 struct mapped_device *md;
940
941 md = find_device(param);
942 if (!md)
943 return -ENXIO;
944
945 r = __dev_status(md, param);
946 dm_put(md);
947 return r;
948}
949
950/*
951 * Build up the status struct for each target
952 */
953static void retrieve_status(struct dm_table *table,
954 struct dm_ioctl *param, size_t param_size)
955{
956 unsigned int i, num_targets;
957 struct dm_target_spec *spec;
958 char *outbuf, *outptr;
959 status_type_t type;
960 size_t remaining, len, used = 0;
961
962 outptr = outbuf = get_result_buffer(param, param_size, &len);
963
964 if (param->flags & DM_STATUS_TABLE_FLAG)
965 type = STATUSTYPE_TABLE;
966 else
967 type = STATUSTYPE_INFO;
968
969 /* Get all the target info */
970 num_targets = dm_table_get_num_targets(table);
971 for (i = 0; i < num_targets; i++) {
972 struct dm_target *ti = dm_table_get_target(table, i);
973
974 remaining = len - (outptr - outbuf);
975 if (remaining <= sizeof(struct dm_target_spec)) {
976 param->flags |= DM_BUFFER_FULL_FLAG;
977 break;
978 }
979
980 spec = (struct dm_target_spec *) outptr;
981
982 spec->status = 0;
983 spec->sector_start = ti->begin;
984 spec->length = ti->len;
985 strncpy(spec->target_type, ti->type->name,
986 sizeof(spec->target_type));
987
988 outptr += sizeof(struct dm_target_spec);
989 remaining = len - (outptr - outbuf);
990 if (remaining <= 0) {
991 param->flags |= DM_BUFFER_FULL_FLAG;
992 break;
993 }
994
995 /* Get the status/table string from the target driver */
996 if (ti->type->status) {
997 if (ti->type->status(ti, type, outptr, remaining)) {
998 param->flags |= DM_BUFFER_FULL_FLAG;
999 break;
1000 }
1001 } else
1002 outptr[0] = '\0';
1003
1004 outptr += strlen(outptr) + 1;
1005 used = param->data_start + (outptr - outbuf);
1006
1007 outptr = align_ptr(outptr);
1008 spec->next = outptr - outbuf;
1009 }
1010
1011 if (used)
1012 param->data_size = used;
1013
1014 param->target_count = num_targets;
1015}
1016
1017/*
1018 * Wait for a device to report an event
1019 */
1020static int dev_wait(struct dm_ioctl *param, size_t param_size)
1021{
1022 int r;
1023 struct mapped_device *md;
1024 struct dm_table *table;
1025
1026 md = find_device(param);
1027 if (!md)
1028 return -ENXIO;
1029
1030 /*
1031 * Wait for a notification event
1032 */
1033 if (dm_wait_event(md, param->event_nr)) {
1034 r = -ERESTARTSYS;
1035 goto out;
1036 }
1037
1038 /*
1039 * The userland program is going to want to know what
1040 * changed to trigger the event, so we may as well tell
1041 * him and save an ioctl.
1042 */
1043 r = __dev_status(md, param);
1044 if (r)
1045 goto out;
1046
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +00001047 table = dm_get_live_or_inactive_table(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (table) {
1049 retrieve_status(table, param, param_size);
1050 dm_table_put(table);
1051 }
1052
1053 out:
1054 dm_put(md);
1055 return r;
1056}
1057
Al Viroaeb5d722008-09-02 15:28:45 -04001058static inline fmode_t get_mode(struct dm_ioctl *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Al Viroaeb5d722008-09-02 15:28:45 -04001060 fmode_t mode = FMODE_READ | FMODE_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 if (param->flags & DM_READONLY_FLAG)
1063 mode = FMODE_READ;
1064
1065 return mode;
1066}
1067
1068static int next_target(struct dm_target_spec *last, uint32_t next, void *end,
1069 struct dm_target_spec **spec, char **target_params)
1070{
1071 *spec = (struct dm_target_spec *) ((unsigned char *) last + next);
1072 *target_params = (char *) (*spec + 1);
1073
1074 if (*spec < (last + 1))
1075 return -EINVAL;
1076
1077 return invalid_str(*target_params, end);
1078}
1079
1080static int populate_table(struct dm_table *table,
1081 struct dm_ioctl *param, size_t param_size)
1082{
1083 int r;
1084 unsigned int i = 0;
1085 struct dm_target_spec *spec = (struct dm_target_spec *) param;
1086 uint32_t next = param->data_start;
1087 void *end = (void *) param + param_size;
1088 char *target_params;
1089
1090 if (!param->target_count) {
1091 DMWARN("populate_table: no targets specified");
1092 return -EINVAL;
1093 }
1094
1095 for (i = 0; i < param->target_count; i++) {
1096
1097 r = next_target(spec, next, end, &spec, &target_params);
1098 if (r) {
1099 DMWARN("unable to find target");
1100 return r;
1101 }
1102
1103 r = dm_table_add_target(table, spec->target_type,
1104 (sector_t) spec->sector_start,
1105 (sector_t) spec->length,
1106 target_params);
1107 if (r) {
1108 DMWARN("error adding target to table");
1109 return r;
1110 }
1111
1112 next = spec->next;
1113 }
1114
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001115 r = dm_table_set_type(table);
1116 if (r) {
1117 DMWARN("unable to set table type");
1118 return r;
1119 }
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 return dm_table_complete(table);
1122}
1123
Martin K. Petersen9c470082009-04-09 00:27:12 +01001124static int table_prealloc_integrity(struct dm_table *t,
1125 struct mapped_device *md)
1126{
1127 struct list_head *devices = dm_table_get_devices(t);
1128 struct dm_dev_internal *dd;
1129
1130 list_for_each_entry(dd, devices, list)
1131 if (bdev_get_integrity(dd->dm_dev.bdev))
1132 return blk_integrity_register(dm_disk(md), NULL);
1133
1134 return 0;
1135}
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137static int table_load(struct dm_ioctl *param, size_t param_size)
1138{
1139 int r;
1140 struct hash_cell *hc;
1141 struct dm_table *t;
Mike Anderson1134e5a2006-03-27 01:17:54 -08001142 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Mike Anderson1134e5a2006-03-27 01:17:54 -08001144 md = find_device(param);
1145 if (!md)
1146 return -ENXIO;
1147
1148 r = dm_table_create(&t, get_mode(param), param->target_count, md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 if (r)
Mike Anderson1134e5a2006-03-27 01:17:54 -08001150 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 r = populate_table(t, param, param_size);
1153 if (r) {
Mikulas Patockaf80a5572009-03-16 17:44:26 +00001154 dm_table_destroy(t);
Mike Anderson1134e5a2006-03-27 01:17:54 -08001155 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
1157
Martin K. Petersen9c470082009-04-09 00:27:12 +01001158 r = table_prealloc_integrity(t, md);
1159 if (r) {
1160 DMERR("%s: could not register integrity profile.",
1161 dm_device_name(md));
1162 dm_table_destroy(t);
1163 goto out;
1164 }
1165
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001166 r = dm_table_alloc_md_mempools(t);
1167 if (r) {
1168 DMWARN("unable to allocate mempools for this table");
1169 dm_table_destroy(t);
1170 goto out;
1171 }
1172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 down_write(&_hash_lock);
Mike Anderson1134e5a2006-03-27 01:17:54 -08001174 hc = dm_get_mdptr(md);
1175 if (!hc || hc->md != md) {
1176 DMWARN("device has been removed from the dev hash table.");
Mikulas Patockaf80a5572009-03-16 17:44:26 +00001177 dm_table_destroy(t);
Mike Anderson1134e5a2006-03-27 01:17:54 -08001178 up_write(&_hash_lock);
1179 r = -ENXIO;
1180 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
1182
1183 if (hc->new_map)
Mikulas Patockad5816872009-01-06 03:05:10 +00001184 dm_table_destroy(hc->new_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 hc->new_map = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 up_write(&_hash_lock);
Mike Anderson1134e5a2006-03-27 01:17:54 -08001187
1188 param->flags |= DM_INACTIVE_PRESENT_FLAG;
1189 r = __dev_status(md, param);
1190
1191out:
1192 dm_put(md);
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return r;
1195}
1196
1197static int table_clear(struct dm_ioctl *param, size_t param_size)
1198{
1199 int r;
1200 struct hash_cell *hc;
Jeff Mahoney7ec75f22006-06-26 00:27:24 -07001201 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 down_write(&_hash_lock);
1204
1205 hc = __find_device_hash_cell(param);
1206 if (!hc) {
1207 DMWARN("device doesn't appear to be in the dev hash table.");
1208 up_write(&_hash_lock);
1209 return -ENXIO;
1210 }
1211
1212 if (hc->new_map) {
Mikulas Patockad5816872009-01-06 03:05:10 +00001213 dm_table_destroy(hc->new_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 hc->new_map = NULL;
1215 }
1216
1217 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
1218
1219 r = __dev_status(hc->md, param);
Jeff Mahoney7ec75f22006-06-26 00:27:24 -07001220 md = hc->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 up_write(&_hash_lock);
Jeff Mahoney7ec75f22006-06-26 00:27:24 -07001222 dm_put(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 return r;
1224}
1225
1226/*
1227 * Retrieves a list of devices used by a particular dm device.
1228 */
1229static void retrieve_deps(struct dm_table *table,
1230 struct dm_ioctl *param, size_t param_size)
1231{
1232 unsigned int count = 0;
1233 struct list_head *tmp;
1234 size_t len, needed;
Mikulas Patocka82b15192008-10-10 13:37:09 +01001235 struct dm_dev_internal *dd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 struct dm_target_deps *deps;
1237
1238 deps = get_result_buffer(param, param_size, &len);
1239
1240 /*
1241 * Count the devices.
1242 */
1243 list_for_each (tmp, dm_table_get_devices(table))
1244 count++;
1245
1246 /*
1247 * Check we have enough space.
1248 */
1249 needed = sizeof(*deps) + (sizeof(*deps->dev) * count);
1250 if (len < needed) {
1251 param->flags |= DM_BUFFER_FULL_FLAG;
1252 return;
1253 }
1254
1255 /*
1256 * Fill in the devices.
1257 */
1258 deps->count = count;
1259 count = 0;
1260 list_for_each_entry (dd, dm_table_get_devices(table), list)
Mikulas Patocka82b15192008-10-10 13:37:09 +01001261 deps->dev[count++] = huge_encode_dev(dd->dm_dev.bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 param->data_size = param->data_start + needed;
1264}
1265
1266static int table_deps(struct dm_ioctl *param, size_t param_size)
1267{
1268 int r = 0;
1269 struct mapped_device *md;
1270 struct dm_table *table;
1271
1272 md = find_device(param);
1273 if (!md)
1274 return -ENXIO;
1275
1276 r = __dev_status(md, param);
1277 if (r)
1278 goto out;
1279
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +00001280 table = dm_get_live_or_inactive_table(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 if (table) {
1282 retrieve_deps(table, param, param_size);
1283 dm_table_put(table);
1284 }
1285
1286 out:
1287 dm_put(md);
1288 return r;
1289}
1290
1291/*
1292 * Return the status of a device as a text string for each
1293 * target.
1294 */
1295static int table_status(struct dm_ioctl *param, size_t param_size)
1296{
1297 int r;
1298 struct mapped_device *md;
1299 struct dm_table *table;
1300
1301 md = find_device(param);
1302 if (!md)
1303 return -ENXIO;
1304
1305 r = __dev_status(md, param);
1306 if (r)
1307 goto out;
1308
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +00001309 table = dm_get_live_or_inactive_table(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 if (table) {
1311 retrieve_status(table, param, param_size);
1312 dm_table_put(table);
1313 }
1314
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +00001315out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 dm_put(md);
1317 return r;
1318}
1319
1320/*
1321 * Pass a message to the target that's at the supplied device offset.
1322 */
1323static int target_message(struct dm_ioctl *param, size_t param_size)
1324{
1325 int r, argc;
1326 char **argv;
1327 struct mapped_device *md;
1328 struct dm_table *table;
1329 struct dm_target *ti;
1330 struct dm_target_msg *tmsg = (void *) param + param->data_start;
1331
1332 md = find_device(param);
1333 if (!md)
1334 return -ENXIO;
1335
1336 r = __dev_status(md, param);
1337 if (r)
1338 goto out;
1339
Milan Broz027d50f2007-10-19 22:38:36 +01001340 if (tmsg < (struct dm_target_msg *) param->data ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 invalid_str(tmsg->message, (void *) param + param_size)) {
1342 DMWARN("Invalid target message parameters.");
1343 r = -EINVAL;
1344 goto out;
1345 }
1346
1347 r = dm_split_args(&argc, &argv, tmsg->message);
1348 if (r) {
1349 DMWARN("Failed to split target message parameters");
1350 goto out;
1351 }
1352
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001353 table = dm_get_live_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 if (!table)
1355 goto out_argv;
1356
Mike Andersonc50abeb2009-12-10 23:52:20 +00001357 if (dm_deleting_md(md)) {
1358 r = -ENXIO;
1359 goto out_table;
1360 }
1361
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001362 ti = dm_table_find_target(table, tmsg->sector);
1363 if (!dm_target_is_valid(ti)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 DMWARN("Target message sector outside device.");
1365 r = -EINVAL;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001366 } else if (ti->type->message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 r = ti->type->message(ti, argc, argv);
1368 else {
1369 DMWARN("Target type does not support messages");
1370 r = -EINVAL;
1371 }
1372
Mike Andersonc50abeb2009-12-10 23:52:20 +00001373 out_table:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 dm_table_put(table);
1375 out_argv:
1376 kfree(argv);
1377 out:
1378 param->data_size = 0;
1379 dm_put(md);
1380 return r;
1381}
1382
1383/*-----------------------------------------------------------------
1384 * Implementation of open/close/ioctl on the special char
1385 * device.
1386 *---------------------------------------------------------------*/
1387static ioctl_fn lookup_ioctl(unsigned int cmd)
1388{
1389 static struct {
1390 int cmd;
1391 ioctl_fn fn;
1392 } _ioctls[] = {
1393 {DM_VERSION_CMD, NULL}, /* version is dealt with elsewhere */
1394 {DM_REMOVE_ALL_CMD, remove_all},
1395 {DM_LIST_DEVICES_CMD, list_devices},
1396
1397 {DM_DEV_CREATE_CMD, dev_create},
1398 {DM_DEV_REMOVE_CMD, dev_remove},
1399 {DM_DEV_RENAME_CMD, dev_rename},
1400 {DM_DEV_SUSPEND_CMD, dev_suspend},
1401 {DM_DEV_STATUS_CMD, dev_status},
1402 {DM_DEV_WAIT_CMD, dev_wait},
1403
1404 {DM_TABLE_LOAD_CMD, table_load},
1405 {DM_TABLE_CLEAR_CMD, table_clear},
1406 {DM_TABLE_DEPS_CMD, table_deps},
1407 {DM_TABLE_STATUS_CMD, table_status},
1408
1409 {DM_LIST_VERSIONS_CMD, list_versions},
1410
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001411 {DM_TARGET_MSG_CMD, target_message},
1412 {DM_DEV_SET_GEOMETRY_CMD, dev_set_geometry}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 };
1414
1415 return (cmd >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[cmd].fn;
1416}
1417
1418/*
1419 * As well as checking the version compatibility this always
1420 * copies the kernel interface version out.
1421 */
1422static int check_version(unsigned int cmd, struct dm_ioctl __user *user)
1423{
1424 uint32_t version[3];
1425 int r = 0;
1426
1427 if (copy_from_user(version, user->version, sizeof(version)))
1428 return -EFAULT;
1429
1430 if ((DM_VERSION_MAJOR != version[0]) ||
1431 (DM_VERSION_MINOR < version[1])) {
1432 DMWARN("ioctl interface mismatch: "
1433 "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
1434 DM_VERSION_MAJOR, DM_VERSION_MINOR,
1435 DM_VERSION_PATCHLEVEL,
1436 version[0], version[1], version[2], cmd);
1437 r = -EINVAL;
1438 }
1439
1440 /*
1441 * Fill in the kernel version.
1442 */
1443 version[0] = DM_VERSION_MAJOR;
1444 version[1] = DM_VERSION_MINOR;
1445 version[2] = DM_VERSION_PATCHLEVEL;
1446 if (copy_to_user(user->version, version, sizeof(version)))
1447 return -EFAULT;
1448
1449 return r;
1450}
1451
1452static void free_params(struct dm_ioctl *param)
1453{
1454 vfree(param);
1455}
1456
1457static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl **param)
1458{
1459 struct dm_ioctl tmp, *dmi;
1460
Milan Broz76c072b2008-02-08 02:09:56 +00001461 if (copy_from_user(&tmp, user, sizeof(tmp) - sizeof(tmp.data)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return -EFAULT;
1463
Milan Broz76c072b2008-02-08 02:09:56 +00001464 if (tmp.data_size < (sizeof(tmp) - sizeof(tmp.data)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 return -EINVAL;
1466
Jesper Juhlbb56acf2007-10-19 22:38:54 +01001467 dmi = vmalloc(tmp.data_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 if (!dmi)
1469 return -ENOMEM;
1470
1471 if (copy_from_user(dmi, user, tmp.data_size)) {
1472 vfree(dmi);
1473 return -EFAULT;
1474 }
1475
1476 *param = dmi;
1477 return 0;
1478}
1479
1480static int validate_params(uint cmd, struct dm_ioctl *param)
1481{
1482 /* Always clear this flag */
1483 param->flags &= ~DM_BUFFER_FULL_FLAG;
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00001484 param->flags &= ~DM_UEVENT_GENERATED_FLAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 /* Ignores parameters */
1487 if (cmd == DM_REMOVE_ALL_CMD ||
1488 cmd == DM_LIST_DEVICES_CMD ||
1489 cmd == DM_LIST_VERSIONS_CMD)
1490 return 0;
1491
1492 if ((cmd == DM_DEV_CREATE_CMD)) {
1493 if (!*param->name) {
1494 DMWARN("name not supplied when creating device");
1495 return -EINVAL;
1496 }
1497 } else if ((*param->uuid && *param->name)) {
1498 DMWARN("only supply one of name or uuid, cmd(%u)", cmd);
1499 return -EINVAL;
1500 }
1501
1502 /* Ensure strings are terminated */
1503 param->name[DM_NAME_LEN - 1] = '\0';
1504 param->uuid[DM_UUID_LEN - 1] = '\0';
1505
1506 return 0;
1507}
1508
Alasdair G Kergon27238b22008-02-08 02:09:53 +00001509static int ctl_ioctl(uint command, struct dm_ioctl __user *user)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
1511 int r = 0;
1512 unsigned int cmd;
Andrew Mortona26ffd42008-02-08 02:10:16 +00001513 struct dm_ioctl *uninitialized_var(param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 ioctl_fn fn = NULL;
1515 size_t param_size;
1516
1517 /* only root can play with this */
1518 if (!capable(CAP_SYS_ADMIN))
1519 return -EACCES;
1520
1521 if (_IOC_TYPE(command) != DM_IOCTL)
1522 return -ENOTTY;
1523
1524 cmd = _IOC_NR(command);
1525
1526 /*
1527 * Check the interface version passed in. This also
1528 * writes out the kernel's interface version.
1529 */
1530 r = check_version(cmd, user);
1531 if (r)
1532 return r;
1533
1534 /*
1535 * Nothing more to do for the version command.
1536 */
1537 if (cmd == DM_VERSION_CMD)
1538 return 0;
1539
1540 fn = lookup_ioctl(cmd);
1541 if (!fn) {
1542 DMWARN("dm_ctl_ioctl: unknown command 0x%x", command);
1543 return -ENOTTY;
1544 }
1545
1546 /*
1547 * Trying to avoid low memory issues when a device is
1548 * suspended.
1549 */
1550 current->flags |= PF_MEMALLOC;
1551
1552 /*
1553 * Copy the parameters into kernel space.
1554 */
1555 r = copy_params(user, &param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Alasdair G Kergondab6a422006-02-01 03:04:52 -08001557 current->flags &= ~PF_MEMALLOC;
1558
1559 if (r)
1560 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 r = validate_params(cmd, param);
1563 if (r)
1564 goto out;
1565
1566 param_size = param->data_size;
1567 param->data_size = sizeof(*param);
1568 r = fn(param, param_size);
1569
1570 /*
1571 * Copy the results back to userland.
1572 */
1573 if (!r && copy_to_user(user, param, param->data_size))
1574 r = -EFAULT;
1575
1576 out:
1577 free_params(param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 return r;
1579}
1580
Alasdair G Kergon27238b22008-02-08 02:09:53 +00001581static long dm_ctl_ioctl(struct file *file, uint command, ulong u)
1582{
1583 return (long)ctl_ioctl(command, (struct dm_ioctl __user *)u);
1584}
1585
Milan Broz76c072b2008-02-08 02:09:56 +00001586#ifdef CONFIG_COMPAT
1587static long dm_compat_ctl_ioctl(struct file *file, uint command, ulong u)
1588{
1589 return (long)dm_ctl_ioctl(file, command, (ulong) compat_ptr(u));
1590}
1591#else
1592#define dm_compat_ctl_ioctl NULL
1593#endif
1594
Arjan van de Venfa027c22007-02-12 00:55:33 -08001595static const struct file_operations _ctl_fops = {
Alasdair G Kergon27238b22008-02-08 02:09:53 +00001596 .unlocked_ioctl = dm_ctl_ioctl,
Milan Broz76c072b2008-02-08 02:09:56 +00001597 .compat_ioctl = dm_compat_ctl_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 .owner = THIS_MODULE,
1599};
1600
1601static struct miscdevice _dm_misc = {
1602 .minor = MISC_DYNAMIC_MINOR,
1603 .name = DM_NAME,
Kay Sieverse454cea2009-09-18 23:01:12 +02001604 .nodename = "mapper/control",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 .fops = &_ctl_fops
1606};
1607
1608/*
1609 * Create misc character device and link to DM_DIR/control.
1610 */
1611int __init dm_interface_init(void)
1612{
1613 int r;
1614
1615 r = dm_hash_init();
1616 if (r)
1617 return r;
1618
1619 r = misc_register(&_dm_misc);
1620 if (r) {
1621 DMERR("misc_register failed for control device");
1622 dm_hash_exit();
1623 return r;
1624 }
1625
1626 DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR,
1627 DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA,
1628 DM_DRIVER_EMAIL);
1629 return 0;
1630}
1631
1632void dm_interface_exit(void)
1633{
1634 if (misc_deregister(&_dm_misc) < 0)
1635 DMERR("misc_deregister failed for control device");
1636
1637 dm_hash_exit();
1638}
Mike Anderson96a1f7d2007-10-19 22:47:59 +01001639
1640/**
1641 * dm_copy_name_and_uuid - Copy mapped device name & uuid into supplied buffers
1642 * @md: Pointer to mapped_device
1643 * @name: Buffer (size DM_NAME_LEN) for name
1644 * @uuid: Buffer (size DM_UUID_LEN) for uuid or empty string if uuid not defined
1645 */
1646int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid)
1647{
1648 int r = 0;
1649 struct hash_cell *hc;
1650
1651 if (!md)
1652 return -ENXIO;
1653
Mikulas Patocka60769052009-12-10 23:51:52 +00001654 mutex_lock(&dm_hash_cells_mutex);
Mike Anderson96a1f7d2007-10-19 22:47:59 +01001655 hc = dm_get_mdptr(md);
1656 if (!hc || hc->md != md) {
1657 r = -ENXIO;
1658 goto out;
1659 }
1660
Milan Broz23d39f62009-01-06 03:05:04 +00001661 if (name)
1662 strcpy(name, hc->name);
1663 if (uuid)
1664 strcpy(uuid, hc->uuid ? : "");
Mike Anderson96a1f7d2007-10-19 22:47:59 +01001665
1666out:
Mikulas Patocka60769052009-12-10 23:51:52 +00001667 mutex_unlock(&dm_hash_cells_mutex);
Mike Anderson96a1f7d2007-10-19 22:47:59 +01001668
1669 return r;
1670}