blob: 79ee5ba217f2172cf786c5a31a9eeb7599d2e4a8 [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 Rajnoha856a6f12010-08-12 04:13:53 +0100288static struct mapped_device *dm_hash_rename(struct dm_ioctl *param,
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;
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100294 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 /*
297 * duplicate new.
298 */
Paulo Marques543537b2005-06-23 00:09:02 -0700299 new_name = kstrdup(new, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (!new_name)
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100301 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 down_write(&_hash_lock);
304
305 /*
306 * Is new free ?
307 */
308 hc = __get_name_cell(new);
309 if (hc) {
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100310 DMWARN("asked to rename to an already-existing name %s -> %s",
311 param->name, new);
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700312 dm_put(hc->md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 up_write(&_hash_lock);
314 kfree(new_name);
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100315 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
317
318 /*
319 * Is there such a device as 'old' ?
320 */
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100321 hc = __get_name_cell(param->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (!hc) {
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100323 DMWARN("asked to rename a non-existent device %s -> %s",
324 param->name, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 up_write(&_hash_lock);
326 kfree(new_name);
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100327 return ERR_PTR(-ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329
330 /*
331 * rename and move the name cell.
332 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 list_del(&hc->name_list);
334 old_name = hc->name;
Mikulas Patocka60769052009-12-10 23:51:52 +0000335 mutex_lock(&dm_hash_cells_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 hc->name = new_name;
Mikulas Patocka60769052009-12-10 23:51:52 +0000337 mutex_unlock(&dm_hash_cells_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 list_add(&hc->name_list, _name_buckets + hash_str(new_name));
339
goggin, edward81f17772006-01-06 00:20:01 -0800340 /*
341 * Wake up any dm event waiters.
342 */
Alasdair G Kergon7c666412009-12-10 23:52:19 +0000343 table = dm_get_live_table(hc->md);
goggin, edward81f17772006-01-06 00:20:01 -0800344 if (table) {
345 dm_table_event(table);
346 dm_table_put(table);
347 }
348
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100349 if (!dm_kobject_uevent(hc->md, KOBJ_CHANGE, param->event_nr))
350 param->flags |= DM_UEVENT_GENERATED_FLAG;
Alasdair G Kergon69267a32007-12-13 14:15:57 +0000351
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100352 md = hc->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 up_write(&_hash_lock);
354 kfree(old_name);
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100355
356 return md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
359/*-----------------------------------------------------------------
360 * Implementation of the ioctl commands
361 *---------------------------------------------------------------*/
362/*
363 * All the ioctl commands get dispatched to functions with this
364 * prototype.
365 */
366typedef int (*ioctl_fn)(struct dm_ioctl *param, size_t param_size);
367
368static int remove_all(struct dm_ioctl *param, size_t param_size)
369{
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700370 dm_hash_remove_all(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 param->data_size = 0;
372 return 0;
373}
374
375/*
376 * Round up the ptr to an 8-byte boundary.
377 */
378#define ALIGN_MASK 7
379static inline void *align_ptr(void *ptr)
380{
381 return (void *) (((size_t) (ptr + ALIGN_MASK)) & ~ALIGN_MASK);
382}
383
384/*
385 * Retrieves the data payload buffer from an already allocated
386 * struct dm_ioctl.
387 */
388static void *get_result_buffer(struct dm_ioctl *param, size_t param_size,
389 size_t *len)
390{
391 param->data_start = align_ptr(param + 1) - (void *) param;
392
393 if (param->data_start < param_size)
394 *len = param_size - param->data_start;
395 else
396 *len = 0;
397
398 return ((void *) param) + param->data_start;
399}
400
401static int list_devices(struct dm_ioctl *param, size_t param_size)
402{
403 unsigned int i;
404 struct hash_cell *hc;
405 size_t len, needed = 0;
406 struct gendisk *disk;
407 struct dm_name_list *nl, *old_nl = NULL;
408
409 down_write(&_hash_lock);
410
411 /*
412 * Loop through all the devices working out how much
413 * space we need.
414 */
415 for (i = 0; i < NUM_BUCKETS; i++) {
416 list_for_each_entry (hc, _name_buckets + i, name_list) {
417 needed += sizeof(struct dm_name_list);
418 needed += strlen(hc->name) + 1;
419 needed += ALIGN_MASK;
420 }
421 }
422
423 /*
424 * Grab our output buffer.
425 */
426 nl = get_result_buffer(param, param_size, &len);
427 if (len < needed) {
428 param->flags |= DM_BUFFER_FULL_FLAG;
429 goto out;
430 }
431 param->data_size = param->data_start + needed;
432
433 nl->dev = 0; /* Flags no data */
434
435 /*
436 * Now loop through filling out the names.
437 */
438 for (i = 0; i < NUM_BUCKETS; i++) {
439 list_for_each_entry (hc, _name_buckets + i, name_list) {
440 if (old_nl)
441 old_nl->next = (uint32_t) ((void *) nl -
442 (void *) old_nl);
443 disk = dm_disk(hc->md);
Tejun Heof331c022008-09-03 09:01:48 +0200444 nl->dev = huge_encode_dev(disk_devt(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 nl->next = 0;
446 strcpy(nl->name, hc->name);
447
448 old_nl = nl;
449 nl = align_ptr(((void *) ++nl) + strlen(hc->name) + 1);
450 }
451 }
452
453 out:
454 up_write(&_hash_lock);
455 return 0;
456}
457
458static void list_version_get_needed(struct target_type *tt, void *needed_param)
459{
460 size_t *needed = needed_param;
461
Alasdair G Kergonc4cc6632005-11-21 21:32:33 -0800462 *needed += sizeof(struct dm_target_versions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 *needed += strlen(tt->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 *needed += ALIGN_MASK;
465}
466
467static void list_version_get_info(struct target_type *tt, void *param)
468{
469 struct vers_iter *info = param;
470
471 /* Check space - it might have changed since the first iteration */
472 if ((char *)info->vers + sizeof(tt->version) + strlen(tt->name) + 1 >
473 info->end) {
474
475 info->flags = DM_BUFFER_FULL_FLAG;
476 return;
477 }
478
479 if (info->old_vers)
480 info->old_vers->next = (uint32_t) ((void *)info->vers -
481 (void *)info->old_vers);
482 info->vers->version[0] = tt->version[0];
483 info->vers->version[1] = tt->version[1];
484 info->vers->version[2] = tt->version[2];
485 info->vers->next = 0;
486 strcpy(info->vers->name, tt->name);
487
488 info->old_vers = info->vers;
489 info->vers = align_ptr(((void *) ++info->vers) + strlen(tt->name) + 1);
490}
491
492static int list_versions(struct dm_ioctl *param, size_t param_size)
493{
494 size_t len, needed = 0;
495 struct dm_target_versions *vers;
496 struct vers_iter iter_info;
497
498 /*
499 * Loop through all the devices working out how much
500 * space we need.
501 */
502 dm_target_iterate(list_version_get_needed, &needed);
503
504 /*
505 * Grab our output buffer.
506 */
507 vers = get_result_buffer(param, param_size, &len);
508 if (len < needed) {
509 param->flags |= DM_BUFFER_FULL_FLAG;
510 goto out;
511 }
512 param->data_size = param->data_start + needed;
513
514 iter_info.param_size = param_size;
515 iter_info.old_vers = NULL;
516 iter_info.vers = vers;
517 iter_info.flags = 0;
518 iter_info.end = (char *)vers+len;
519
520 /*
521 * Now loop through filling out the names & versions.
522 */
523 dm_target_iterate(list_version_get_info, &iter_info);
524 param->flags |= iter_info.flags;
525
526 out:
527 return 0;
528}
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530static int check_name(const char *name)
531{
532 if (strchr(name, '/')) {
533 DMWARN("invalid device name");
534 return -EINVAL;
535 }
536
537 return 0;
538}
539
540/*
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000541 * On successful return, the caller must not attempt to acquire
542 * _hash_lock without first calling dm_table_put, because dm_table_destroy
543 * waits for this dm_table_put and could be called under this lock.
544 */
545static struct dm_table *dm_get_inactive_table(struct mapped_device *md)
546{
547 struct hash_cell *hc;
548 struct dm_table *table = NULL;
549
550 down_read(&_hash_lock);
551 hc = dm_get_mdptr(md);
552 if (!hc || hc->md != md) {
553 DMWARN("device has been removed from the dev hash table.");
554 goto out;
555 }
556
557 table = hc->new_map;
558 if (table)
559 dm_table_get(table);
560
561out:
562 up_read(&_hash_lock);
563
564 return table;
565}
566
567static struct dm_table *dm_get_live_or_inactive_table(struct mapped_device *md,
568 struct dm_ioctl *param)
569{
570 return (param->flags & DM_QUERY_INACTIVE_TABLE_FLAG) ?
571 dm_get_inactive_table(md) : dm_get_live_table(md);
572}
573
574/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 * Fills in a dm_ioctl structure, ready for sending back to
576 * userland.
577 */
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100578static void __dev_status(struct mapped_device *md, struct dm_ioctl *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 struct gendisk *disk = dm_disk(md);
581 struct dm_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 param->flags &= ~(DM_SUSPEND_FLAG | DM_READONLY_FLAG |
584 DM_ACTIVE_PRESENT_FLAG);
585
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +0000586 if (dm_suspended_md(md))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 param->flags |= DM_SUSPEND_FLAG;
588
Tejun Heof331c022008-09-03 09:01:48 +0200589 param->dev = huge_encode_dev(disk_devt(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700591 /*
592 * Yes, this will be out of date by the time it gets back
593 * to userland, but it is still very useful for
594 * debugging.
595 */
596 param->open_count = dm_open_count(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 param->event_nr = dm_get_event_nr(md);
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000599 param->target_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Alasdair G Kergon7c666412009-12-10 23:52:19 +0000601 table = dm_get_live_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (table) {
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000603 if (!(param->flags & DM_QUERY_INACTIVE_TABLE_FLAG)) {
604 if (get_disk_ro(disk))
605 param->flags |= DM_READONLY_FLAG;
606 param->target_count = dm_table_get_num_targets(table);
607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 dm_table_put(table);
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000609
610 param->flags |= DM_ACTIVE_PRESENT_FLAG;
611 }
612
613 if (param->flags & DM_QUERY_INACTIVE_TABLE_FLAG) {
614 table = dm_get_inactive_table(md);
615 if (table) {
616 if (!(dm_table_get_mode(table) & FMODE_WRITE))
617 param->flags |= DM_READONLY_FLAG;
618 param->target_count = dm_table_get_num_targets(table);
619 dm_table_put(table);
620 }
621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
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);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100641 if (r)
642 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
645
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100646 __dev_status(md, param);
647
648out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 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;
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100765 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Alasdair G Kergon27238b22008-02-08 02:09:53 +0000767 if (new_name < param->data ||
Milan Brozbc0fd672009-03-16 16:56:01 +0000768 invalid_str(new_name, (void *) param + param_size) ||
769 strlen(new_name) > DM_NAME_LEN - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 DMWARN("Invalid new logical volume name supplied.");
771 return -EINVAL;
772 }
773
774 r = check_name(new_name);
775 if (r)
776 return r;
777
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100778 md = dm_hash_rename(param, new_name);
779 if (IS_ERR(md))
780 return PTR_ERR(md);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +0000781
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100782 __dev_status(md, param);
783 dm_put(md);
784
785 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786}
787
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800788static int dev_set_geometry(struct dm_ioctl *param, size_t param_size)
789{
790 int r = -EINVAL, x;
791 struct mapped_device *md;
792 struct hd_geometry geometry;
793 unsigned long indata[4];
794 char *geostr = (char *) param + param->data_start;
795
796 md = find_device(param);
797 if (!md)
798 return -ENXIO;
799
Alasdair G Kergon27238b22008-02-08 02:09:53 +0000800 if (geostr < param->data ||
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800801 invalid_str(geostr, (void *) param + param_size)) {
802 DMWARN("Invalid geometry supplied.");
803 goto out;
804 }
805
806 x = sscanf(geostr, "%lu %lu %lu %lu", indata,
807 indata + 1, indata + 2, indata + 3);
808
809 if (x != 4) {
810 DMWARN("Unable to interpret geometry settings.");
811 goto out;
812 }
813
814 if (indata[0] > 65535 || indata[1] > 255 ||
815 indata[2] > 255 || indata[3] > ULONG_MAX) {
816 DMWARN("Geometry exceeds range limits.");
817 goto out;
818 }
819
820 geometry.cylinders = indata[0];
821 geometry.heads = indata[1];
822 geometry.sectors = indata[2];
823 geometry.start = indata[3];
824
825 r = dm_set_geometry(md, &geometry);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800826
827 param->data_size = 0;
828
829out:
830 dm_put(md);
831 return r;
832}
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834static int do_suspend(struct dm_ioctl *param)
835{
836 int r = 0;
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -0800837 unsigned suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 struct mapped_device *md;
839
840 md = find_device(param);
841 if (!md)
842 return -ENXIO;
843
Alasdair G Kergon6da487d2006-01-06 00:20:07 -0800844 if (param->flags & DM_SKIP_LOCKFS_FLAG)
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -0800845 suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
Kiyoshi Ueda81fdb092006-12-08 02:41:07 -0800846 if (param->flags & DM_NOFLUSH_FLAG)
847 suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
Alasdair G Kergon6da487d2006-01-06 00:20:07 -0800848
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100849 if (!dm_suspended_md(md)) {
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -0800850 r = dm_suspend(md, suspend_flags);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100851 if (r)
852 goto out;
853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100855 __dev_status(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100857out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 dm_put(md);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return r;
861}
862
863static int do_resume(struct dm_ioctl *param)
864{
865 int r = 0;
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -0800866 unsigned suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 struct hash_cell *hc;
868 struct mapped_device *md;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +0000869 struct dm_table *new_map, *old_map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 down_write(&_hash_lock);
872
873 hc = __find_device_hash_cell(param);
874 if (!hc) {
875 DMWARN("device doesn't appear to be in the dev hash table.");
876 up_write(&_hash_lock);
877 return -ENXIO;
878 }
879
880 md = hc->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 new_map = hc->new_map;
883 hc->new_map = NULL;
884 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
885
886 up_write(&_hash_lock);
887
888 /* Do we need to load a new map ? */
889 if (new_map) {
890 /* Suspend if it isn't already suspended */
Alasdair G Kergon6da487d2006-01-06 00:20:07 -0800891 if (param->flags & DM_SKIP_LOCKFS_FLAG)
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -0800892 suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
Kiyoshi Ueda81fdb092006-12-08 02:41:07 -0800893 if (param->flags & DM_NOFLUSH_FLAG)
894 suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +0000895 if (!dm_suspended_md(md))
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -0800896 dm_suspend(md, suspend_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Alasdair G Kergon042d2a92009-12-10 23:52:24 +0000898 old_map = dm_swap_table(md, new_map);
899 if (IS_ERR(old_map)) {
Mikulas Patockad5816872009-01-06 03:05:10 +0000900 dm_table_destroy(new_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 dm_put(md);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +0000902 return PTR_ERR(old_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
904
905 if (dm_table_get_mode(new_map) & FMODE_WRITE)
906 set_disk_ro(dm_disk(md), 0);
907 else
908 set_disk_ro(dm_disk(md), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
910
Mike Snitzer0f3649a2010-03-06 02:32:24 +0000911 if (dm_suspended_md(md)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 r = dm_resume(md);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +0000913 if (!r && !dm_kobject_uevent(md, KOBJ_CHANGE, param->event_nr))
914 param->flags |= DM_UEVENT_GENERATED_FLAG;
Mike Snitzer0f3649a2010-03-06 02:32:24 +0000915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Alasdair G Kergon042d2a92009-12-10 23:52:24 +0000917 if (old_map)
918 dm_table_destroy(old_map);
Milan Broz60935eb2009-06-22 10:12:30 +0100919
Mike Snitzer0f3649a2010-03-06 02:32:24 +0000920 if (!r)
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100921 __dev_status(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 dm_put(md);
924 return r;
925}
926
927/*
928 * Set or unset the suspension state of a device.
929 * If the device already is in the requested state we just return its status.
930 */
931static int dev_suspend(struct dm_ioctl *param, size_t param_size)
932{
933 if (param->flags & DM_SUSPEND_FLAG)
934 return do_suspend(param);
935
936 return do_resume(param);
937}
938
939/*
940 * Copies device info back to user space, used by
941 * the create and info ioctls.
942 */
943static int dev_status(struct dm_ioctl *param, size_t param_size)
944{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 struct mapped_device *md;
946
947 md = find_device(param);
948 if (!md)
949 return -ENXIO;
950
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100951 __dev_status(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 dm_put(md);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100953
954 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956
957/*
958 * Build up the status struct for each target
959 */
960static void retrieve_status(struct dm_table *table,
961 struct dm_ioctl *param, size_t param_size)
962{
963 unsigned int i, num_targets;
964 struct dm_target_spec *spec;
965 char *outbuf, *outptr;
966 status_type_t type;
967 size_t remaining, len, used = 0;
968
969 outptr = outbuf = get_result_buffer(param, param_size, &len);
970
971 if (param->flags & DM_STATUS_TABLE_FLAG)
972 type = STATUSTYPE_TABLE;
973 else
974 type = STATUSTYPE_INFO;
975
976 /* Get all the target info */
977 num_targets = dm_table_get_num_targets(table);
978 for (i = 0; i < num_targets; i++) {
979 struct dm_target *ti = dm_table_get_target(table, i);
980
981 remaining = len - (outptr - outbuf);
982 if (remaining <= sizeof(struct dm_target_spec)) {
983 param->flags |= DM_BUFFER_FULL_FLAG;
984 break;
985 }
986
987 spec = (struct dm_target_spec *) outptr;
988
989 spec->status = 0;
990 spec->sector_start = ti->begin;
991 spec->length = ti->len;
992 strncpy(spec->target_type, ti->type->name,
993 sizeof(spec->target_type));
994
995 outptr += sizeof(struct dm_target_spec);
996 remaining = len - (outptr - outbuf);
997 if (remaining <= 0) {
998 param->flags |= DM_BUFFER_FULL_FLAG;
999 break;
1000 }
1001
1002 /* Get the status/table string from the target driver */
1003 if (ti->type->status) {
1004 if (ti->type->status(ti, type, outptr, remaining)) {
1005 param->flags |= DM_BUFFER_FULL_FLAG;
1006 break;
1007 }
1008 } else
1009 outptr[0] = '\0';
1010
1011 outptr += strlen(outptr) + 1;
1012 used = param->data_start + (outptr - outbuf);
1013
1014 outptr = align_ptr(outptr);
1015 spec->next = outptr - outbuf;
1016 }
1017
1018 if (used)
1019 param->data_size = used;
1020
1021 param->target_count = num_targets;
1022}
1023
1024/*
1025 * Wait for a device to report an event
1026 */
1027static int dev_wait(struct dm_ioctl *param, size_t param_size)
1028{
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001029 int r = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 struct mapped_device *md;
1031 struct dm_table *table;
1032
1033 md = find_device(param);
1034 if (!md)
1035 return -ENXIO;
1036
1037 /*
1038 * Wait for a notification event
1039 */
1040 if (dm_wait_event(md, param->event_nr)) {
1041 r = -ERESTARTSYS;
1042 goto out;
1043 }
1044
1045 /*
1046 * The userland program is going to want to know what
1047 * changed to trigger the event, so we may as well tell
1048 * him and save an ioctl.
1049 */
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001050 __dev_status(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +00001052 table = dm_get_live_or_inactive_table(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if (table) {
1054 retrieve_status(table, param, param_size);
1055 dm_table_put(table);
1056 }
1057
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001058out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 dm_put(md);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 return r;
1062}
1063
Al Viroaeb5d722008-09-02 15:28:45 -04001064static inline fmode_t get_mode(struct dm_ioctl *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
Al Viroaeb5d722008-09-02 15:28:45 -04001066 fmode_t mode = FMODE_READ | FMODE_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 if (param->flags & DM_READONLY_FLAG)
1069 mode = FMODE_READ;
1070
1071 return mode;
1072}
1073
1074static int next_target(struct dm_target_spec *last, uint32_t next, void *end,
1075 struct dm_target_spec **spec, char **target_params)
1076{
1077 *spec = (struct dm_target_spec *) ((unsigned char *) last + next);
1078 *target_params = (char *) (*spec + 1);
1079
1080 if (*spec < (last + 1))
1081 return -EINVAL;
1082
1083 return invalid_str(*target_params, end);
1084}
1085
1086static int populate_table(struct dm_table *table,
1087 struct dm_ioctl *param, size_t param_size)
1088{
1089 int r;
1090 unsigned int i = 0;
1091 struct dm_target_spec *spec = (struct dm_target_spec *) param;
1092 uint32_t next = param->data_start;
1093 void *end = (void *) param + param_size;
1094 char *target_params;
1095
1096 if (!param->target_count) {
1097 DMWARN("populate_table: no targets specified");
1098 return -EINVAL;
1099 }
1100
1101 for (i = 0; i < param->target_count; i++) {
1102
1103 r = next_target(spec, next, end, &spec, &target_params);
1104 if (r) {
1105 DMWARN("unable to find target");
1106 return r;
1107 }
1108
1109 r = dm_table_add_target(table, spec->target_type,
1110 (sector_t) spec->sector_start,
1111 (sector_t) spec->length,
1112 target_params);
1113 if (r) {
1114 DMWARN("error adding target to table");
1115 return r;
1116 }
1117
1118 next = spec->next;
1119 }
1120
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001121 r = dm_table_set_type(table);
1122 if (r) {
1123 DMWARN("unable to set table type");
1124 return r;
1125 }
1126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return dm_table_complete(table);
1128}
1129
Martin K. Petersen9c470082009-04-09 00:27:12 +01001130static int table_prealloc_integrity(struct dm_table *t,
1131 struct mapped_device *md)
1132{
1133 struct list_head *devices = dm_table_get_devices(t);
1134 struct dm_dev_internal *dd;
1135
1136 list_for_each_entry(dd, devices, list)
1137 if (bdev_get_integrity(dd->dm_dev.bdev))
1138 return blk_integrity_register(dm_disk(md), NULL);
1139
1140 return 0;
1141}
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143static int table_load(struct dm_ioctl *param, size_t param_size)
1144{
1145 int r;
1146 struct hash_cell *hc;
1147 struct dm_table *t;
Mike Anderson1134e5a2006-03-27 01:17:54 -08001148 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Mike Anderson1134e5a2006-03-27 01:17:54 -08001150 md = find_device(param);
1151 if (!md)
1152 return -ENXIO;
1153
1154 r = dm_table_create(&t, get_mode(param), param->target_count, md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (r)
Mike Anderson1134e5a2006-03-27 01:17:54 -08001156 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 r = populate_table(t, param, param_size);
1159 if (r) {
Mikulas Patockaf80a5572009-03-16 17:44:26 +00001160 dm_table_destroy(t);
Mike Anderson1134e5a2006-03-27 01:17:54 -08001161 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
1163
Martin K. Petersen9c470082009-04-09 00:27:12 +01001164 r = table_prealloc_integrity(t, md);
1165 if (r) {
1166 DMERR("%s: could not register integrity profile.",
1167 dm_device_name(md));
1168 dm_table_destroy(t);
1169 goto out;
1170 }
1171
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001172 r = dm_table_alloc_md_mempools(t);
1173 if (r) {
1174 DMWARN("unable to allocate mempools for this table");
1175 dm_table_destroy(t);
1176 goto out;
1177 }
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 down_write(&_hash_lock);
Mike Anderson1134e5a2006-03-27 01:17:54 -08001180 hc = dm_get_mdptr(md);
1181 if (!hc || hc->md != md) {
1182 DMWARN("device has been removed from the dev hash table.");
Mikulas Patockaf80a5572009-03-16 17:44:26 +00001183 dm_table_destroy(t);
Mike Anderson1134e5a2006-03-27 01:17:54 -08001184 up_write(&_hash_lock);
1185 r = -ENXIO;
1186 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
1188
1189 if (hc->new_map)
Mikulas Patockad5816872009-01-06 03:05:10 +00001190 dm_table_destroy(hc->new_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 hc->new_map = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 up_write(&_hash_lock);
Mike Anderson1134e5a2006-03-27 01:17:54 -08001193
1194 param->flags |= DM_INACTIVE_PRESENT_FLAG;
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001195 __dev_status(md, param);
Mike Anderson1134e5a2006-03-27 01:17:54 -08001196
1197out:
1198 dm_put(md);
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 return r;
1201}
1202
1203static int table_clear(struct dm_ioctl *param, size_t param_size)
1204{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 struct hash_cell *hc;
Jeff Mahoney7ec75f22006-06-26 00:27:24 -07001206 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 down_write(&_hash_lock);
1209
1210 hc = __find_device_hash_cell(param);
1211 if (!hc) {
1212 DMWARN("device doesn't appear to be in the dev hash table.");
1213 up_write(&_hash_lock);
1214 return -ENXIO;
1215 }
1216
1217 if (hc->new_map) {
Mikulas Patockad5816872009-01-06 03:05:10 +00001218 dm_table_destroy(hc->new_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 hc->new_map = NULL;
1220 }
1221
1222 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
1223
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001224 __dev_status(hc->md, param);
Jeff Mahoney7ec75f22006-06-26 00:27:24 -07001225 md = hc->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 up_write(&_hash_lock);
Jeff Mahoney7ec75f22006-06-26 00:27:24 -07001227 dm_put(md);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001228
1229 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230}
1231
1232/*
1233 * Retrieves a list of devices used by a particular dm device.
1234 */
1235static void retrieve_deps(struct dm_table *table,
1236 struct dm_ioctl *param, size_t param_size)
1237{
1238 unsigned int count = 0;
1239 struct list_head *tmp;
1240 size_t len, needed;
Mikulas Patocka82b15192008-10-10 13:37:09 +01001241 struct dm_dev_internal *dd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 struct dm_target_deps *deps;
1243
1244 deps = get_result_buffer(param, param_size, &len);
1245
1246 /*
1247 * Count the devices.
1248 */
1249 list_for_each (tmp, dm_table_get_devices(table))
1250 count++;
1251
1252 /*
1253 * Check we have enough space.
1254 */
1255 needed = sizeof(*deps) + (sizeof(*deps->dev) * count);
1256 if (len < needed) {
1257 param->flags |= DM_BUFFER_FULL_FLAG;
1258 return;
1259 }
1260
1261 /*
1262 * Fill in the devices.
1263 */
1264 deps->count = count;
1265 count = 0;
1266 list_for_each_entry (dd, dm_table_get_devices(table), list)
Mikulas Patocka82b15192008-10-10 13:37:09 +01001267 deps->dev[count++] = huge_encode_dev(dd->dm_dev.bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 param->data_size = param->data_start + needed;
1270}
1271
1272static int table_deps(struct dm_ioctl *param, size_t param_size)
1273{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 struct mapped_device *md;
1275 struct dm_table *table;
1276
1277 md = find_device(param);
1278 if (!md)
1279 return -ENXIO;
1280
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001281 __dev_status(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +00001283 table = dm_get_live_or_inactive_table(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 if (table) {
1285 retrieve_deps(table, param, param_size);
1286 dm_table_put(table);
1287 }
1288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 dm_put(md);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001290
1291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
1294/*
1295 * Return the status of a device as a text string for each
1296 * target.
1297 */
1298static int table_status(struct dm_ioctl *param, size_t param_size)
1299{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 struct mapped_device *md;
1301 struct dm_table *table;
1302
1303 md = find_device(param);
1304 if (!md)
1305 return -ENXIO;
1306
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001307 __dev_status(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 dm_put(md);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001316
1317 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318}
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
Milan Broz027d50f2007-10-19 22:38:36 +01001336 if (tmsg < (struct dm_target_msg *) param->data ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 invalid_str(tmsg->message, (void *) param + param_size)) {
1338 DMWARN("Invalid target message parameters.");
1339 r = -EINVAL;
1340 goto out;
1341 }
1342
1343 r = dm_split_args(&argc, &argv, tmsg->message);
1344 if (r) {
1345 DMWARN("Failed to split target message parameters");
1346 goto out;
1347 }
1348
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001349 table = dm_get_live_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 if (!table)
1351 goto out_argv;
1352
Mike Andersonc50abeb2009-12-10 23:52:20 +00001353 if (dm_deleting_md(md)) {
1354 r = -ENXIO;
1355 goto out_table;
1356 }
1357
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001358 ti = dm_table_find_target(table, tmsg->sector);
1359 if (!dm_target_is_valid(ti)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 DMWARN("Target message sector outside device.");
1361 r = -EINVAL;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001362 } else if (ti->type->message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 r = ti->type->message(ti, argc, argv);
1364 else {
1365 DMWARN("Target type does not support messages");
1366 r = -EINVAL;
1367 }
1368
Mike Andersonc50abeb2009-12-10 23:52:20 +00001369 out_table:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 dm_table_put(table);
1371 out_argv:
1372 kfree(argv);
1373 out:
1374 param->data_size = 0;
1375 dm_put(md);
1376 return r;
1377}
1378
1379/*-----------------------------------------------------------------
1380 * Implementation of open/close/ioctl on the special char
1381 * device.
1382 *---------------------------------------------------------------*/
1383static ioctl_fn lookup_ioctl(unsigned int cmd)
1384{
1385 static struct {
1386 int cmd;
1387 ioctl_fn fn;
1388 } _ioctls[] = {
1389 {DM_VERSION_CMD, NULL}, /* version is dealt with elsewhere */
1390 {DM_REMOVE_ALL_CMD, remove_all},
1391 {DM_LIST_DEVICES_CMD, list_devices},
1392
1393 {DM_DEV_CREATE_CMD, dev_create},
1394 {DM_DEV_REMOVE_CMD, dev_remove},
1395 {DM_DEV_RENAME_CMD, dev_rename},
1396 {DM_DEV_SUSPEND_CMD, dev_suspend},
1397 {DM_DEV_STATUS_CMD, dev_status},
1398 {DM_DEV_WAIT_CMD, dev_wait},
1399
1400 {DM_TABLE_LOAD_CMD, table_load},
1401 {DM_TABLE_CLEAR_CMD, table_clear},
1402 {DM_TABLE_DEPS_CMD, table_deps},
1403 {DM_TABLE_STATUS_CMD, table_status},
1404
1405 {DM_LIST_VERSIONS_CMD, list_versions},
1406
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001407 {DM_TARGET_MSG_CMD, target_message},
1408 {DM_DEV_SET_GEOMETRY_CMD, dev_set_geometry}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 };
1410
1411 return (cmd >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[cmd].fn;
1412}
1413
1414/*
1415 * As well as checking the version compatibility this always
1416 * copies the kernel interface version out.
1417 */
1418static int check_version(unsigned int cmd, struct dm_ioctl __user *user)
1419{
1420 uint32_t version[3];
1421 int r = 0;
1422
1423 if (copy_from_user(version, user->version, sizeof(version)))
1424 return -EFAULT;
1425
1426 if ((DM_VERSION_MAJOR != version[0]) ||
1427 (DM_VERSION_MINOR < version[1])) {
1428 DMWARN("ioctl interface mismatch: "
1429 "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
1430 DM_VERSION_MAJOR, DM_VERSION_MINOR,
1431 DM_VERSION_PATCHLEVEL,
1432 version[0], version[1], version[2], cmd);
1433 r = -EINVAL;
1434 }
1435
1436 /*
1437 * Fill in the kernel version.
1438 */
1439 version[0] = DM_VERSION_MAJOR;
1440 version[1] = DM_VERSION_MINOR;
1441 version[2] = DM_VERSION_PATCHLEVEL;
1442 if (copy_to_user(user->version, version, sizeof(version)))
1443 return -EFAULT;
1444
1445 return r;
1446}
1447
1448static void free_params(struct dm_ioctl *param)
1449{
1450 vfree(param);
1451}
1452
1453static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl **param)
1454{
1455 struct dm_ioctl tmp, *dmi;
1456
Milan Broz76c072b2008-02-08 02:09:56 +00001457 if (copy_from_user(&tmp, user, sizeof(tmp) - sizeof(tmp.data)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 return -EFAULT;
1459
Milan Broz76c072b2008-02-08 02:09:56 +00001460 if (tmp.data_size < (sizeof(tmp) - sizeof(tmp.data)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return -EINVAL;
1462
Jesper Juhlbb56acf2007-10-19 22:38:54 +01001463 dmi = vmalloc(tmp.data_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 if (!dmi)
1465 return -ENOMEM;
1466
1467 if (copy_from_user(dmi, user, tmp.data_size)) {
1468 vfree(dmi);
1469 return -EFAULT;
1470 }
1471
1472 *param = dmi;
1473 return 0;
1474}
1475
1476static int validate_params(uint cmd, struct dm_ioctl *param)
1477{
1478 /* Always clear this flag */
1479 param->flags &= ~DM_BUFFER_FULL_FLAG;
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00001480 param->flags &= ~DM_UEVENT_GENERATED_FLAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
1482 /* Ignores parameters */
1483 if (cmd == DM_REMOVE_ALL_CMD ||
1484 cmd == DM_LIST_DEVICES_CMD ||
1485 cmd == DM_LIST_VERSIONS_CMD)
1486 return 0;
1487
1488 if ((cmd == DM_DEV_CREATE_CMD)) {
1489 if (!*param->name) {
1490 DMWARN("name not supplied when creating device");
1491 return -EINVAL;
1492 }
1493 } else if ((*param->uuid && *param->name)) {
1494 DMWARN("only supply one of name or uuid, cmd(%u)", cmd);
1495 return -EINVAL;
1496 }
1497
1498 /* Ensure strings are terminated */
1499 param->name[DM_NAME_LEN - 1] = '\0';
1500 param->uuid[DM_UUID_LEN - 1] = '\0';
1501
1502 return 0;
1503}
1504
Alasdair G Kergon27238b22008-02-08 02:09:53 +00001505static int ctl_ioctl(uint command, struct dm_ioctl __user *user)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
1507 int r = 0;
1508 unsigned int cmd;
Andrew Mortona26ffd42008-02-08 02:10:16 +00001509 struct dm_ioctl *uninitialized_var(param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 ioctl_fn fn = NULL;
1511 size_t param_size;
1512
1513 /* only root can play with this */
1514 if (!capable(CAP_SYS_ADMIN))
1515 return -EACCES;
1516
1517 if (_IOC_TYPE(command) != DM_IOCTL)
1518 return -ENOTTY;
1519
1520 cmd = _IOC_NR(command);
1521
1522 /*
1523 * Check the interface version passed in. This also
1524 * writes out the kernel's interface version.
1525 */
1526 r = check_version(cmd, user);
1527 if (r)
1528 return r;
1529
1530 /*
1531 * Nothing more to do for the version command.
1532 */
1533 if (cmd == DM_VERSION_CMD)
1534 return 0;
1535
1536 fn = lookup_ioctl(cmd);
1537 if (!fn) {
1538 DMWARN("dm_ctl_ioctl: unknown command 0x%x", command);
1539 return -ENOTTY;
1540 }
1541
1542 /*
1543 * Trying to avoid low memory issues when a device is
1544 * suspended.
1545 */
1546 current->flags |= PF_MEMALLOC;
1547
1548 /*
1549 * Copy the parameters into kernel space.
1550 */
1551 r = copy_params(user, &param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Alasdair G Kergondab6a422006-02-01 03:04:52 -08001553 current->flags &= ~PF_MEMALLOC;
1554
1555 if (r)
1556 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 r = validate_params(cmd, param);
1559 if (r)
1560 goto out;
1561
1562 param_size = param->data_size;
1563 param->data_size = sizeof(*param);
1564 r = fn(param, param_size);
1565
1566 /*
1567 * Copy the results back to userland.
1568 */
1569 if (!r && copy_to_user(user, param, param->data_size))
1570 r = -EFAULT;
1571
1572 out:
1573 free_params(param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 return r;
1575}
1576
Alasdair G Kergon27238b22008-02-08 02:09:53 +00001577static long dm_ctl_ioctl(struct file *file, uint command, ulong u)
1578{
1579 return (long)ctl_ioctl(command, (struct dm_ioctl __user *)u);
1580}
1581
Milan Broz76c072b2008-02-08 02:09:56 +00001582#ifdef CONFIG_COMPAT
1583static long dm_compat_ctl_ioctl(struct file *file, uint command, ulong u)
1584{
1585 return (long)dm_ctl_ioctl(file, command, (ulong) compat_ptr(u));
1586}
1587#else
1588#define dm_compat_ctl_ioctl NULL
1589#endif
1590
Arjan van de Venfa027c22007-02-12 00:55:33 -08001591static const struct file_operations _ctl_fops = {
Alasdair G Kergon27238b22008-02-08 02:09:53 +00001592 .unlocked_ioctl = dm_ctl_ioctl,
Milan Broz76c072b2008-02-08 02:09:56 +00001593 .compat_ioctl = dm_compat_ctl_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 .owner = THIS_MODULE,
1595};
1596
1597static struct miscdevice _dm_misc = {
1598 .minor = MISC_DYNAMIC_MINOR,
1599 .name = DM_NAME,
Kay Sieverse454cea2009-09-18 23:01:12 +02001600 .nodename = "mapper/control",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 .fops = &_ctl_fops
1602};
1603
1604/*
1605 * Create misc character device and link to DM_DIR/control.
1606 */
1607int __init dm_interface_init(void)
1608{
1609 int r;
1610
1611 r = dm_hash_init();
1612 if (r)
1613 return r;
1614
1615 r = misc_register(&_dm_misc);
1616 if (r) {
1617 DMERR("misc_register failed for control device");
1618 dm_hash_exit();
1619 return r;
1620 }
1621
1622 DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR,
1623 DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA,
1624 DM_DRIVER_EMAIL);
1625 return 0;
1626}
1627
1628void dm_interface_exit(void)
1629{
1630 if (misc_deregister(&_dm_misc) < 0)
1631 DMERR("misc_deregister failed for control device");
1632
1633 dm_hash_exit();
1634}
Mike Anderson96a1f7d2007-10-19 22:47:59 +01001635
1636/**
1637 * dm_copy_name_and_uuid - Copy mapped device name & uuid into supplied buffers
1638 * @md: Pointer to mapped_device
1639 * @name: Buffer (size DM_NAME_LEN) for name
1640 * @uuid: Buffer (size DM_UUID_LEN) for uuid or empty string if uuid not defined
1641 */
1642int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid)
1643{
1644 int r = 0;
1645 struct hash_cell *hc;
1646
1647 if (!md)
1648 return -ENXIO;
1649
Mikulas Patocka60769052009-12-10 23:51:52 +00001650 mutex_lock(&dm_hash_cells_mutex);
Mike Anderson96a1f7d2007-10-19 22:47:59 +01001651 hc = dm_get_mdptr(md);
1652 if (!hc || hc->md != md) {
1653 r = -ENXIO;
1654 goto out;
1655 }
1656
Milan Broz23d39f62009-01-06 03:05:04 +00001657 if (name)
1658 strcpy(name, hc->name);
1659 if (uuid)
1660 strcpy(uuid, hc->uuid ? : "");
Mike Anderson96a1f7d2007-10-19 22:47:59 +01001661
1662out:
Mikulas Patocka60769052009-12-10 23:51:52 +00001663 mutex_unlock(&dm_hash_cells_mutex);
Mike Anderson96a1f7d2007-10-19 22:47:59 +01001664
1665 return r;
1666}