blob: 3fd8f0e169e72a85eb388cdc537a79dd423c319f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -07003 * Copyright (C) 2004 - 2006 Red Hat, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * This file is released under the GPL.
6 */
7
8#include "dm.h"
9
10#include <linux/module.h>
11#include <linux/vmalloc.h>
12#include <linux/miscdevice.h>
13#include <linux/init.h>
14#include <linux/wait.h>
15#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/dm-ioctl.h>
Darrick J. Wong3ac51e72006-03-27 01:17:54 -080017#include <linux/hdreg.h>
Milan Broz76c072b2008-02-08 02:09:56 +000018#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include <asm/uaccess.h>
21
Alasdair G Kergon72d94862006-06-26 00:27:35 -070022#define DM_MSG_PREFIX "ioctl"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#define DM_DRIVER_EMAIL "dm-devel@redhat.com"
24
25/*-----------------------------------------------------------------
26 * The ioctl interface needs to be able to look up devices by
27 * name or uuid.
28 *---------------------------------------------------------------*/
29struct hash_cell {
30 struct list_head name_list;
31 struct list_head uuid_list;
32
33 char *name;
34 char *uuid;
35 struct mapped_device *md;
36 struct dm_table *new_map;
37};
38
39struct vers_iter {
40 size_t param_size;
41 struct dm_target_versions *vers, *old_vers;
42 char *end;
43 uint32_t flags;
44};
45
46
47#define NUM_BUCKETS 64
48#define MASK_BUCKETS (NUM_BUCKETS - 1)
49static struct list_head _name_buckets[NUM_BUCKETS];
50static struct list_head _uuid_buckets[NUM_BUCKETS];
51
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -070052static void dm_hash_remove_all(int keep_open_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54/*
55 * Guards access to both hash tables.
56 */
57static DECLARE_RWSEM(_hash_lock);
58
Mikulas Patocka60769052009-12-10 23:51:52 +000059/*
60 * Protects use of mdptr to obtain hash cell name and uuid from mapped device.
61 */
62static DEFINE_MUTEX(dm_hash_cells_mutex);
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static void init_buckets(struct list_head *buckets)
65{
66 unsigned int i;
67
68 for (i = 0; i < NUM_BUCKETS; i++)
69 INIT_LIST_HEAD(buckets + i);
70}
71
72static int dm_hash_init(void)
73{
74 init_buckets(_name_buckets);
75 init_buckets(_uuid_buckets);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 return 0;
77}
78
79static void dm_hash_exit(void)
80{
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -070081 dm_hash_remove_all(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
84/*-----------------------------------------------------------------
85 * Hash function:
86 * We're not really concerned with the str hash function being
87 * fast since it's only used by the ioctl interface.
88 *---------------------------------------------------------------*/
89static unsigned int hash_str(const char *str)
90{
91 const unsigned int hash_mult = 2654435387U;
92 unsigned int h = 0;
93
94 while (*str)
95 h = (h + (unsigned int) *str++) * hash_mult;
96
97 return h & MASK_BUCKETS;
98}
99
100/*-----------------------------------------------------------------
101 * Code for looking up a device by name
102 *---------------------------------------------------------------*/
103static struct hash_cell *__get_name_cell(const char *str)
104{
105 struct hash_cell *hc;
106 unsigned int h = hash_str(str);
107
108 list_for_each_entry (hc, _name_buckets + h, name_list)
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700109 if (!strcmp(hc->name, str)) {
110 dm_get(hc->md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return hc;
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 return NULL;
115}
116
117static struct hash_cell *__get_uuid_cell(const char *str)
118{
119 struct hash_cell *hc;
120 unsigned int h = hash_str(str);
121
122 list_for_each_entry (hc, _uuid_buckets + h, uuid_list)
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700123 if (!strcmp(hc->uuid, str)) {
124 dm_get(hc->md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return hc;
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 return NULL;
129}
130
131/*-----------------------------------------------------------------
132 * Inserting, removing and renaming a device.
133 *---------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134static struct hash_cell *alloc_cell(const char *name, const char *uuid,
135 struct mapped_device *md)
136{
137 struct hash_cell *hc;
138
139 hc = kmalloc(sizeof(*hc), GFP_KERNEL);
140 if (!hc)
141 return NULL;
142
Paulo Marques543537b2005-06-23 00:09:02 -0700143 hc->name = kstrdup(name, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (!hc->name) {
145 kfree(hc);
146 return NULL;
147 }
148
149 if (!uuid)
150 hc->uuid = NULL;
151
152 else {
Paulo Marques543537b2005-06-23 00:09:02 -0700153 hc->uuid = kstrdup(uuid, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if (!hc->uuid) {
155 kfree(hc->name);
156 kfree(hc);
157 return NULL;
158 }
159 }
160
161 INIT_LIST_HEAD(&hc->name_list);
162 INIT_LIST_HEAD(&hc->uuid_list);
163 hc->md = md;
164 hc->new_map = NULL;
165 return hc;
166}
167
168static void free_cell(struct hash_cell *hc)
169{
170 if (hc) {
171 kfree(hc->name);
172 kfree(hc->uuid);
173 kfree(hc);
174 }
175}
176
177/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 * The kdev_t and uuid of a device can never change once it is
179 * initially inserted.
180 */
181static int dm_hash_insert(const char *name, const char *uuid, struct mapped_device *md)
182{
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700183 struct hash_cell *cell, *hc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 /*
186 * Allocate the new cells.
187 */
188 cell = alloc_cell(name, uuid, md);
189 if (!cell)
190 return -ENOMEM;
191
192 /*
193 * Insert the cell into both hash tables.
194 */
195 down_write(&_hash_lock);
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700196 hc = __get_name_cell(name);
197 if (hc) {
198 dm_put(hc->md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 goto bad;
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 list_add(&cell->name_list, _name_buckets + hash_str(name));
203
204 if (uuid) {
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700205 hc = __get_uuid_cell(uuid);
206 if (hc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 list_del(&cell->name_list);
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700208 dm_put(hc->md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 goto bad;
210 }
211 list_add(&cell->uuid_list, _uuid_buckets + hash_str(uuid));
212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 dm_get(md);
Mikulas Patocka60769052009-12-10 23:51:52 +0000214 mutex_lock(&dm_hash_cells_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 dm_set_mdptr(md, cell);
Mikulas Patocka60769052009-12-10 23:51:52 +0000216 mutex_unlock(&dm_hash_cells_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 up_write(&_hash_lock);
218
219 return 0;
220
221 bad:
222 up_write(&_hash_lock);
223 free_cell(cell);
224 return -EBUSY;
225}
226
227static void __hash_remove(struct hash_cell *hc)
228{
goggin, edward269fd2a2005-09-27 21:45:44 -0700229 struct dm_table *table;
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 /* remove from the dev hash */
232 list_del(&hc->uuid_list);
233 list_del(&hc->name_list);
Mikulas Patocka60769052009-12-10 23:51:52 +0000234 mutex_lock(&dm_hash_cells_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 dm_set_mdptr(hc->md, NULL);
Mikulas Patocka60769052009-12-10 23:51:52 +0000236 mutex_unlock(&dm_hash_cells_mutex);
goggin, edward269fd2a2005-09-27 21:45:44 -0700237
Alasdair G Kergon7c666412009-12-10 23:52:19 +0000238 table = dm_get_live_table(hc->md);
goggin, edward269fd2a2005-09-27 21:45:44 -0700239 if (table) {
240 dm_table_event(table);
241 dm_table_put(table);
242 }
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 if (hc->new_map)
Mikulas Patockad5816872009-01-06 03:05:10 +0000245 dm_table_destroy(hc->new_map);
Mike Anderson1134e5a2006-03-27 01:17:54 -0800246 dm_put(hc->md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 free_cell(hc);
248}
249
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700250static void dm_hash_remove_all(int keep_open_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Kiyoshi Ueda98f33282010-08-12 04:13:55 +0100252 int i, dev_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 struct hash_cell *hc;
Kiyoshi Ueda98f33282010-08-12 04:13:55 +0100254 struct mapped_device *md;
255
256retry:
257 dev_skipped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 down_write(&_hash_lock);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 for (i = 0; i < NUM_BUCKETS; i++) {
Kiyoshi Ueda98f33282010-08-12 04:13:55 +0100262 list_for_each_entry(hc, _name_buckets + i, name_list) {
263 md = hc->md;
264 dm_get(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700265
Kiyoshi Ueda98f33282010-08-12 04:13:55 +0100266 if (keep_open_devices && dm_lock_for_deletion(md)) {
267 dm_put(md);
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700268 dev_skipped++;
269 continue;
270 }
Kiyoshi Ueda98f33282010-08-12 04:13:55 +0100271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 __hash_remove(hc);
Kiyoshi Ueda98f33282010-08-12 04:13:55 +0100273
274 up_write(&_hash_lock);
275
276 dm_put(md);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +0100277 if (likely(keep_open_devices))
278 dm_destroy(md);
279 else
280 dm_destroy_immediate(md);
Kiyoshi Ueda98f33282010-08-12 04:13:55 +0100281
282 /*
283 * Some mapped devices may be using other mapped
284 * devices, so repeat until we make no further
285 * progress. If a new mapped device is created
286 * here it will also get removed.
287 */
288 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290 }
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 up_write(&_hash_lock);
Kiyoshi Ueda98f33282010-08-12 04:13:55 +0100293
294 if (dev_skipped)
295 DMWARN("remove_all left %d open device(s)", dev_skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100298static struct mapped_device *dm_hash_rename(struct dm_ioctl *param,
299 const char *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
301 char *new_name, *old_name;
302 struct hash_cell *hc;
goggin, edward81f17772006-01-06 00:20:01 -0800303 struct dm_table *table;
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100304 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 /*
307 * duplicate new.
308 */
Paulo Marques543537b2005-06-23 00:09:02 -0700309 new_name = kstrdup(new, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (!new_name)
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100311 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 down_write(&_hash_lock);
314
315 /*
316 * Is new free ?
317 */
318 hc = __get_name_cell(new);
319 if (hc) {
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100320 DMWARN("asked to rename to an already-existing name %s -> %s",
321 param->name, new);
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700322 dm_put(hc->md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 up_write(&_hash_lock);
324 kfree(new_name);
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100325 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
327
328 /*
329 * Is there such a device as 'old' ?
330 */
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100331 hc = __get_name_cell(param->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (!hc) {
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100333 DMWARN("asked to rename a non-existent device %s -> %s",
334 param->name, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 up_write(&_hash_lock);
336 kfree(new_name);
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100337 return ERR_PTR(-ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339
340 /*
341 * rename and move the name cell.
342 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 list_del(&hc->name_list);
344 old_name = hc->name;
Mikulas Patocka60769052009-12-10 23:51:52 +0000345 mutex_lock(&dm_hash_cells_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 hc->name = new_name;
Mikulas Patocka60769052009-12-10 23:51:52 +0000347 mutex_unlock(&dm_hash_cells_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 list_add(&hc->name_list, _name_buckets + hash_str(new_name));
349
goggin, edward81f17772006-01-06 00:20:01 -0800350 /*
351 * Wake up any dm event waiters.
352 */
Alasdair G Kergon7c666412009-12-10 23:52:19 +0000353 table = dm_get_live_table(hc->md);
goggin, edward81f17772006-01-06 00:20:01 -0800354 if (table) {
355 dm_table_event(table);
356 dm_table_put(table);
357 }
358
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100359 if (!dm_kobject_uevent(hc->md, KOBJ_CHANGE, param->event_nr))
360 param->flags |= DM_UEVENT_GENERATED_FLAG;
Alasdair G Kergon69267a32007-12-13 14:15:57 +0000361
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100362 md = hc->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 up_write(&_hash_lock);
364 kfree(old_name);
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100365
366 return md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
369/*-----------------------------------------------------------------
370 * Implementation of the ioctl commands
371 *---------------------------------------------------------------*/
372/*
373 * All the ioctl commands get dispatched to functions with this
374 * prototype.
375 */
376typedef int (*ioctl_fn)(struct dm_ioctl *param, size_t param_size);
377
378static int remove_all(struct dm_ioctl *param, size_t param_size)
379{
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700380 dm_hash_remove_all(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 param->data_size = 0;
382 return 0;
383}
384
385/*
386 * Round up the ptr to an 8-byte boundary.
387 */
388#define ALIGN_MASK 7
389static inline void *align_ptr(void *ptr)
390{
391 return (void *) (((size_t) (ptr + ALIGN_MASK)) & ~ALIGN_MASK);
392}
393
394/*
395 * Retrieves the data payload buffer from an already allocated
396 * struct dm_ioctl.
397 */
398static void *get_result_buffer(struct dm_ioctl *param, size_t param_size,
399 size_t *len)
400{
401 param->data_start = align_ptr(param + 1) - (void *) param;
402
403 if (param->data_start < param_size)
404 *len = param_size - param->data_start;
405 else
406 *len = 0;
407
408 return ((void *) param) + param->data_start;
409}
410
411static int list_devices(struct dm_ioctl *param, size_t param_size)
412{
413 unsigned int i;
414 struct hash_cell *hc;
415 size_t len, needed = 0;
416 struct gendisk *disk;
417 struct dm_name_list *nl, *old_nl = NULL;
418
419 down_write(&_hash_lock);
420
421 /*
422 * Loop through all the devices working out how much
423 * space we need.
424 */
425 for (i = 0; i < NUM_BUCKETS; i++) {
426 list_for_each_entry (hc, _name_buckets + i, name_list) {
427 needed += sizeof(struct dm_name_list);
428 needed += strlen(hc->name) + 1;
429 needed += ALIGN_MASK;
430 }
431 }
432
433 /*
434 * Grab our output buffer.
435 */
436 nl = get_result_buffer(param, param_size, &len);
437 if (len < needed) {
438 param->flags |= DM_BUFFER_FULL_FLAG;
439 goto out;
440 }
441 param->data_size = param->data_start + needed;
442
443 nl->dev = 0; /* Flags no data */
444
445 /*
446 * Now loop through filling out the names.
447 */
448 for (i = 0; i < NUM_BUCKETS; i++) {
449 list_for_each_entry (hc, _name_buckets + i, name_list) {
450 if (old_nl)
451 old_nl->next = (uint32_t) ((void *) nl -
452 (void *) old_nl);
453 disk = dm_disk(hc->md);
Tejun Heof331c022008-09-03 09:01:48 +0200454 nl->dev = huge_encode_dev(disk_devt(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 nl->next = 0;
456 strcpy(nl->name, hc->name);
457
458 old_nl = nl;
459 nl = align_ptr(((void *) ++nl) + strlen(hc->name) + 1);
460 }
461 }
462
463 out:
464 up_write(&_hash_lock);
465 return 0;
466}
467
468static void list_version_get_needed(struct target_type *tt, void *needed_param)
469{
470 size_t *needed = needed_param;
471
Alasdair G Kergonc4cc6632005-11-21 21:32:33 -0800472 *needed += sizeof(struct dm_target_versions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 *needed += strlen(tt->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 *needed += ALIGN_MASK;
475}
476
477static void list_version_get_info(struct target_type *tt, void *param)
478{
479 struct vers_iter *info = param;
480
481 /* Check space - it might have changed since the first iteration */
482 if ((char *)info->vers + sizeof(tt->version) + strlen(tt->name) + 1 >
483 info->end) {
484
485 info->flags = DM_BUFFER_FULL_FLAG;
486 return;
487 }
488
489 if (info->old_vers)
490 info->old_vers->next = (uint32_t) ((void *)info->vers -
491 (void *)info->old_vers);
492 info->vers->version[0] = tt->version[0];
493 info->vers->version[1] = tt->version[1];
494 info->vers->version[2] = tt->version[2];
495 info->vers->next = 0;
496 strcpy(info->vers->name, tt->name);
497
498 info->old_vers = info->vers;
499 info->vers = align_ptr(((void *) ++info->vers) + strlen(tt->name) + 1);
500}
501
502static int list_versions(struct dm_ioctl *param, size_t param_size)
503{
504 size_t len, needed = 0;
505 struct dm_target_versions *vers;
506 struct vers_iter iter_info;
507
508 /*
509 * Loop through all the devices working out how much
510 * space we need.
511 */
512 dm_target_iterate(list_version_get_needed, &needed);
513
514 /*
515 * Grab our output buffer.
516 */
517 vers = get_result_buffer(param, param_size, &len);
518 if (len < needed) {
519 param->flags |= DM_BUFFER_FULL_FLAG;
520 goto out;
521 }
522 param->data_size = param->data_start + needed;
523
524 iter_info.param_size = param_size;
525 iter_info.old_vers = NULL;
526 iter_info.vers = vers;
527 iter_info.flags = 0;
528 iter_info.end = (char *)vers+len;
529
530 /*
531 * Now loop through filling out the names & versions.
532 */
533 dm_target_iterate(list_version_get_info, &iter_info);
534 param->flags |= iter_info.flags;
535
536 out:
537 return 0;
538}
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540static int check_name(const char *name)
541{
542 if (strchr(name, '/')) {
543 DMWARN("invalid device name");
544 return -EINVAL;
545 }
546
547 return 0;
548}
549
550/*
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000551 * On successful return, the caller must not attempt to acquire
552 * _hash_lock without first calling dm_table_put, because dm_table_destroy
553 * waits for this dm_table_put and could be called under this lock.
554 */
555static struct dm_table *dm_get_inactive_table(struct mapped_device *md)
556{
557 struct hash_cell *hc;
558 struct dm_table *table = NULL;
559
560 down_read(&_hash_lock);
561 hc = dm_get_mdptr(md);
562 if (!hc || hc->md != md) {
563 DMWARN("device has been removed from the dev hash table.");
564 goto out;
565 }
566
567 table = hc->new_map;
568 if (table)
569 dm_table_get(table);
570
571out:
572 up_read(&_hash_lock);
573
574 return table;
575}
576
577static struct dm_table *dm_get_live_or_inactive_table(struct mapped_device *md,
578 struct dm_ioctl *param)
579{
580 return (param->flags & DM_QUERY_INACTIVE_TABLE_FLAG) ?
581 dm_get_inactive_table(md) : dm_get_live_table(md);
582}
583
584/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 * Fills in a dm_ioctl structure, ready for sending back to
586 * userland.
587 */
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100588static void __dev_status(struct mapped_device *md, struct dm_ioctl *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 struct gendisk *disk = dm_disk(md);
591 struct dm_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 param->flags &= ~(DM_SUSPEND_FLAG | DM_READONLY_FLAG |
594 DM_ACTIVE_PRESENT_FLAG);
595
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +0000596 if (dm_suspended_md(md))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 param->flags |= DM_SUSPEND_FLAG;
598
Tejun Heof331c022008-09-03 09:01:48 +0200599 param->dev = huge_encode_dev(disk_devt(disk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700601 /*
602 * Yes, this will be out of date by the time it gets back
603 * to userland, but it is still very useful for
604 * debugging.
605 */
606 param->open_count = dm_open_count(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 param->event_nr = dm_get_event_nr(md);
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000609 param->target_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Alasdair G Kergon7c666412009-12-10 23:52:19 +0000611 table = dm_get_live_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (table) {
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000613 if (!(param->flags & DM_QUERY_INACTIVE_TABLE_FLAG)) {
614 if (get_disk_ro(disk))
615 param->flags |= DM_READONLY_FLAG;
616 param->target_count = dm_table_get_num_targets(table);
617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 dm_table_put(table);
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +0000619
620 param->flags |= DM_ACTIVE_PRESENT_FLAG;
621 }
622
623 if (param->flags & DM_QUERY_INACTIVE_TABLE_FLAG) {
624 table = dm_get_inactive_table(md);
625 if (table) {
626 if (!(dm_table_get_mode(table) & FMODE_WRITE))
627 param->flags |= DM_READONLY_FLAG;
628 param->target_count = dm_table_get_num_targets(table);
629 dm_table_put(table);
630 }
631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
633
634static int dev_create(struct dm_ioctl *param, size_t param_size)
635{
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700636 int r, m = DM_ANY_MINOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 struct mapped_device *md;
638
639 r = check_name(param->name);
640 if (r)
641 return r;
642
643 if (param->flags & DM_PERSISTENT_DEV_FLAG)
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700644 m = MINOR(huge_decode_dev(param->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Alasdair G Kergon2b06cff2006-06-26 00:27:32 -0700646 r = dm_create(m, &md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (r)
648 return r;
649
650 r = dm_hash_insert(param->name, *param->uuid ? param->uuid : NULL, md);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +0100651 if (r) {
652 dm_put(md);
653 dm_destroy(md);
654 return r;
655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
658
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100659 __dev_status(md, param);
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 dm_put(md);
662
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +0100663 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
666/*
667 * Always use UUID for lookups if it's present, otherwise use name or dev.
668 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800669static struct hash_cell *__find_device_hash_cell(struct dm_ioctl *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -0800671 struct mapped_device *md;
672 void *mdptr = NULL;
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (*param->uuid)
675 return __get_uuid_cell(param->uuid);
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -0800676
677 if (*param->name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return __get_name_cell(param->name);
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -0800679
680 md = dm_get_md(huge_decode_dev(param->dev));
Alasdair G Kergonbfc5ecd2006-11-08 17:44:42 -0800681 if (!md)
682 goto out;
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -0800683
Alasdair G Kergonbfc5ecd2006-11-08 17:44:42 -0800684 mdptr = dm_get_mdptr(md);
685 if (!mdptr)
686 dm_put(md);
687
688out:
Alasdair G Kergon9ade92a2006-03-27 01:17:53 -0800689 return mdptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
Arjan van de Ven858119e2006-01-14 13:20:43 -0800692static struct mapped_device *find_device(struct dm_ioctl *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
694 struct hash_cell *hc;
695 struct mapped_device *md = NULL;
696
697 down_read(&_hash_lock);
698 hc = __find_device_hash_cell(param);
699 if (hc) {
700 md = hc->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 /*
703 * Sneakily write in both the name and the uuid
704 * while we have the cell.
705 */
Roel Kluina518b862009-12-10 23:52:07 +0000706 strlcpy(param->name, hc->name, sizeof(param->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (hc->uuid)
Roel Kluina518b862009-12-10 23:52:07 +0000708 strlcpy(param->uuid, hc->uuid, sizeof(param->uuid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 else
710 param->uuid[0] = '\0';
711
712 if (hc->new_map)
713 param->flags |= DM_INACTIVE_PRESENT_FLAG;
714 else
715 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
716 }
717 up_read(&_hash_lock);
718
719 return md;
720}
721
722static int dev_remove(struct dm_ioctl *param, size_t param_size)
723{
724 struct hash_cell *hc;
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700725 struct mapped_device *md;
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700726 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 down_write(&_hash_lock);
729 hc = __find_device_hash_cell(param);
730
731 if (!hc) {
732 DMWARN("device doesn't appear to be in the dev hash table.");
733 up_write(&_hash_lock);
734 return -ENXIO;
735 }
736
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700737 md = hc->md;
738
Alasdair G Kergon5c6bd752006-06-26 00:27:34 -0700739 /*
740 * Ensure the device is not open and nothing further can open it.
741 */
742 r = dm_lock_for_deletion(md);
743 if (r) {
744 DMWARN("unable to remove open device %s", hc->name);
745 up_write(&_hash_lock);
746 dm_put(md);
747 return r;
748 }
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 __hash_remove(hc);
751 up_write(&_hash_lock);
Milan Broz60935eb2009-06-22 10:12:30 +0100752
Peter Rajnoha3abf85b2010-03-06 02:32:31 +0000753 if (!dm_kobject_uevent(md, KOBJ_REMOVE, param->event_nr))
754 param->flags |= DM_UEVENT_GENERATED_FLAG;
Milan Broz60935eb2009-06-22 10:12:30 +0100755
Jeff Mahoney7ec75f22006-06-26 00:27:24 -0700756 dm_put(md);
Kiyoshi Ueda3f77316d2010-08-12 04:13:56 +0100757 dm_destroy(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return 0;
759}
760
761/*
762 * Check a string doesn't overrun the chunk of
763 * memory we copied from userland.
764 */
765static int invalid_str(char *str, void *end)
766{
767 while ((void *) str < end)
768 if (!*str++)
769 return 0;
770
771 return -EINVAL;
772}
773
774static int dev_rename(struct dm_ioctl *param, size_t param_size)
775{
776 int r;
777 char *new_name = (char *) param + param->data_start;
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100778 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Alasdair G Kergon27238b22008-02-08 02:09:53 +0000780 if (new_name < param->data ||
Milan Brozbc0fd672009-03-16 16:56:01 +0000781 invalid_str(new_name, (void *) param + param_size) ||
782 strlen(new_name) > DM_NAME_LEN - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 DMWARN("Invalid new logical volume name supplied.");
784 return -EINVAL;
785 }
786
787 r = check_name(new_name);
788 if (r)
789 return r;
790
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100791 md = dm_hash_rename(param, new_name);
792 if (IS_ERR(md))
793 return PTR_ERR(md);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +0000794
Peter Rajnoha856a6f12010-08-12 04:13:53 +0100795 __dev_status(md, param);
796 dm_put(md);
797
798 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799}
800
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800801static int dev_set_geometry(struct dm_ioctl *param, size_t param_size)
802{
803 int r = -EINVAL, x;
804 struct mapped_device *md;
805 struct hd_geometry geometry;
806 unsigned long indata[4];
807 char *geostr = (char *) param + param->data_start;
808
809 md = find_device(param);
810 if (!md)
811 return -ENXIO;
812
Alasdair G Kergon27238b22008-02-08 02:09:53 +0000813 if (geostr < param->data ||
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800814 invalid_str(geostr, (void *) param + param_size)) {
815 DMWARN("Invalid geometry supplied.");
816 goto out;
817 }
818
819 x = sscanf(geostr, "%lu %lu %lu %lu", indata,
820 indata + 1, indata + 2, indata + 3);
821
822 if (x != 4) {
823 DMWARN("Unable to interpret geometry settings.");
824 goto out;
825 }
826
827 if (indata[0] > 65535 || indata[1] > 255 ||
828 indata[2] > 255 || indata[3] > ULONG_MAX) {
829 DMWARN("Geometry exceeds range limits.");
830 goto out;
831 }
832
833 geometry.cylinders = indata[0];
834 geometry.heads = indata[1];
835 geometry.sectors = indata[2];
836 geometry.start = indata[3];
837
838 r = dm_set_geometry(md, &geometry);
Darrick J. Wong3ac51e72006-03-27 01:17:54 -0800839
840 param->data_size = 0;
841
842out:
843 dm_put(md);
844 return r;
845}
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847static int do_suspend(struct dm_ioctl *param)
848{
849 int r = 0;
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -0800850 unsigned suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 struct mapped_device *md;
852
853 md = find_device(param);
854 if (!md)
855 return -ENXIO;
856
Alasdair G Kergon6da487d2006-01-06 00:20:07 -0800857 if (param->flags & DM_SKIP_LOCKFS_FLAG)
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -0800858 suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
Kiyoshi Ueda81fdb092006-12-08 02:41:07 -0800859 if (param->flags & DM_NOFLUSH_FLAG)
860 suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
Alasdair G Kergon6da487d2006-01-06 00:20:07 -0800861
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100862 if (!dm_suspended_md(md)) {
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -0800863 r = dm_suspend(md, suspend_flags);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100864 if (r)
865 goto out;
866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100868 __dev_status(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100870out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 dm_put(md);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return r;
874}
875
876static int do_resume(struct dm_ioctl *param)
877{
878 int r = 0;
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -0800879 unsigned suspend_flags = DM_SUSPEND_LOCKFS_FLAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 struct hash_cell *hc;
881 struct mapped_device *md;
Alasdair G Kergon042d2a92009-12-10 23:52:24 +0000882 struct dm_table *new_map, *old_map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 down_write(&_hash_lock);
885
886 hc = __find_device_hash_cell(param);
887 if (!hc) {
888 DMWARN("device doesn't appear to be in the dev hash table.");
889 up_write(&_hash_lock);
890 return -ENXIO;
891 }
892
893 md = hc->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 new_map = hc->new_map;
896 hc->new_map = NULL;
897 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
898
899 up_write(&_hash_lock);
900
901 /* Do we need to load a new map ? */
902 if (new_map) {
903 /* Suspend if it isn't already suspended */
Alasdair G Kergon6da487d2006-01-06 00:20:07 -0800904 if (param->flags & DM_SKIP_LOCKFS_FLAG)
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -0800905 suspend_flags &= ~DM_SUSPEND_LOCKFS_FLAG;
Kiyoshi Ueda81fdb092006-12-08 02:41:07 -0800906 if (param->flags & DM_NOFLUSH_FLAG)
907 suspend_flags |= DM_SUSPEND_NOFLUSH_FLAG;
Kiyoshi Ueda4f186f82009-12-10 23:52:26 +0000908 if (!dm_suspended_md(md))
Kiyoshi Uedaa3d77d32006-12-08 02:41:04 -0800909 dm_suspend(md, suspend_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Alasdair G Kergon042d2a92009-12-10 23:52:24 +0000911 old_map = dm_swap_table(md, new_map);
912 if (IS_ERR(old_map)) {
Mikulas Patockad5816872009-01-06 03:05:10 +0000913 dm_table_destroy(new_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 dm_put(md);
Alasdair G Kergon042d2a92009-12-10 23:52:24 +0000915 return PTR_ERR(old_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
917
918 if (dm_table_get_mode(new_map) & FMODE_WRITE)
919 set_disk_ro(dm_disk(md), 0);
920 else
921 set_disk_ro(dm_disk(md), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
923
Mike Snitzer0f3649a92010-03-06 02:32:24 +0000924 if (dm_suspended_md(md)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 r = dm_resume(md);
Peter Rajnoha3abf85b2010-03-06 02:32:31 +0000926 if (!r && !dm_kobject_uevent(md, KOBJ_CHANGE, param->event_nr))
927 param->flags |= DM_UEVENT_GENERATED_FLAG;
Mike Snitzer0f3649a92010-03-06 02:32:24 +0000928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Alasdair G Kergon042d2a92009-12-10 23:52:24 +0000930 if (old_map)
931 dm_table_destroy(old_map);
Milan Broz60935eb2009-06-22 10:12:30 +0100932
Mike Snitzer0f3649a92010-03-06 02:32:24 +0000933 if (!r)
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100934 __dev_status(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 dm_put(md);
937 return r;
938}
939
940/*
941 * Set or unset the suspension state of a device.
942 * If the device already is in the requested state we just return its status.
943 */
944static int dev_suspend(struct dm_ioctl *param, size_t param_size)
945{
946 if (param->flags & DM_SUSPEND_FLAG)
947 return do_suspend(param);
948
949 return do_resume(param);
950}
951
952/*
953 * Copies device info back to user space, used by
954 * the create and info ioctls.
955 */
956static int dev_status(struct dm_ioctl *param, size_t param_size)
957{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 struct mapped_device *md;
959
960 md = find_device(param);
961 if (!md)
962 return -ENXIO;
963
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100964 __dev_status(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 dm_put(md);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +0100966
967 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
969
970/*
971 * Build up the status struct for each target
972 */
973static void retrieve_status(struct dm_table *table,
974 struct dm_ioctl *param, size_t param_size)
975{
976 unsigned int i, num_targets;
977 struct dm_target_spec *spec;
978 char *outbuf, *outptr;
979 status_type_t type;
980 size_t remaining, len, used = 0;
981
982 outptr = outbuf = get_result_buffer(param, param_size, &len);
983
984 if (param->flags & DM_STATUS_TABLE_FLAG)
985 type = STATUSTYPE_TABLE;
986 else
987 type = STATUSTYPE_INFO;
988
989 /* Get all the target info */
990 num_targets = dm_table_get_num_targets(table);
991 for (i = 0; i < num_targets; i++) {
992 struct dm_target *ti = dm_table_get_target(table, i);
993
994 remaining = len - (outptr - outbuf);
995 if (remaining <= sizeof(struct dm_target_spec)) {
996 param->flags |= DM_BUFFER_FULL_FLAG;
997 break;
998 }
999
1000 spec = (struct dm_target_spec *) outptr;
1001
1002 spec->status = 0;
1003 spec->sector_start = ti->begin;
1004 spec->length = ti->len;
1005 strncpy(spec->target_type, ti->type->name,
1006 sizeof(spec->target_type));
1007
1008 outptr += sizeof(struct dm_target_spec);
1009 remaining = len - (outptr - outbuf);
1010 if (remaining <= 0) {
1011 param->flags |= DM_BUFFER_FULL_FLAG;
1012 break;
1013 }
1014
1015 /* Get the status/table string from the target driver */
1016 if (ti->type->status) {
1017 if (ti->type->status(ti, type, outptr, remaining)) {
1018 param->flags |= DM_BUFFER_FULL_FLAG;
1019 break;
1020 }
1021 } else
1022 outptr[0] = '\0';
1023
1024 outptr += strlen(outptr) + 1;
1025 used = param->data_start + (outptr - outbuf);
1026
1027 outptr = align_ptr(outptr);
1028 spec->next = outptr - outbuf;
1029 }
1030
1031 if (used)
1032 param->data_size = used;
1033
1034 param->target_count = num_targets;
1035}
1036
1037/*
1038 * Wait for a device to report an event
1039 */
1040static int dev_wait(struct dm_ioctl *param, size_t param_size)
1041{
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001042 int r = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 struct mapped_device *md;
1044 struct dm_table *table;
1045
1046 md = find_device(param);
1047 if (!md)
1048 return -ENXIO;
1049
1050 /*
1051 * Wait for a notification event
1052 */
1053 if (dm_wait_event(md, param->event_nr)) {
1054 r = -ERESTARTSYS;
1055 goto out;
1056 }
1057
1058 /*
1059 * The userland program is going to want to know what
1060 * changed to trigger the event, so we may as well tell
1061 * him and save an ioctl.
1062 */
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001063 __dev_status(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +00001065 table = dm_get_live_or_inactive_table(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 if (table) {
1067 retrieve_status(table, param, param_size);
1068 dm_table_put(table);
1069 }
1070
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001071out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 dm_put(md);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 return r;
1075}
1076
Al Viroaeb5d722008-09-02 15:28:45 -04001077static inline fmode_t get_mode(struct dm_ioctl *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
Al Viroaeb5d722008-09-02 15:28:45 -04001079 fmode_t mode = FMODE_READ | FMODE_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 if (param->flags & DM_READONLY_FLAG)
1082 mode = FMODE_READ;
1083
1084 return mode;
1085}
1086
1087static int next_target(struct dm_target_spec *last, uint32_t next, void *end,
1088 struct dm_target_spec **spec, char **target_params)
1089{
1090 *spec = (struct dm_target_spec *) ((unsigned char *) last + next);
1091 *target_params = (char *) (*spec + 1);
1092
1093 if (*spec < (last + 1))
1094 return -EINVAL;
1095
1096 return invalid_str(*target_params, end);
1097}
1098
1099static int populate_table(struct dm_table *table,
1100 struct dm_ioctl *param, size_t param_size)
1101{
1102 int r;
1103 unsigned int i = 0;
1104 struct dm_target_spec *spec = (struct dm_target_spec *) param;
1105 uint32_t next = param->data_start;
1106 void *end = (void *) param + param_size;
1107 char *target_params;
1108
1109 if (!param->target_count) {
1110 DMWARN("populate_table: no targets specified");
1111 return -EINVAL;
1112 }
1113
1114 for (i = 0; i < param->target_count; i++) {
1115
1116 r = next_target(spec, next, end, &spec, &target_params);
1117 if (r) {
1118 DMWARN("unable to find target");
1119 return r;
1120 }
1121
1122 r = dm_table_add_target(table, spec->target_type,
1123 (sector_t) spec->sector_start,
1124 (sector_t) spec->length,
1125 target_params);
1126 if (r) {
1127 DMWARN("error adding target to table");
1128 return r;
1129 }
1130
1131 next = spec->next;
1132 }
1133
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001134 r = dm_table_set_type(table);
1135 if (r) {
1136 DMWARN("unable to set table type");
1137 return r;
1138 }
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 return dm_table_complete(table);
1141}
1142
Martin K. Petersen9c470082009-04-09 00:27:12 +01001143static int table_prealloc_integrity(struct dm_table *t,
1144 struct mapped_device *md)
1145{
1146 struct list_head *devices = dm_table_get_devices(t);
1147 struct dm_dev_internal *dd;
1148
1149 list_for_each_entry(dd, devices, list)
1150 if (bdev_get_integrity(dd->dm_dev.bdev))
1151 return blk_integrity_register(dm_disk(md), NULL);
1152
1153 return 0;
1154}
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156static int table_load(struct dm_ioctl *param, size_t param_size)
1157{
1158 int r;
1159 struct hash_cell *hc;
1160 struct dm_table *t;
Mike Anderson1134e5a2006-03-27 01:17:54 -08001161 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Mike Anderson1134e5a2006-03-27 01:17:54 -08001163 md = find_device(param);
1164 if (!md)
1165 return -ENXIO;
1166
1167 r = dm_table_create(&t, get_mode(param), param->target_count, md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (r)
Mike Anderson1134e5a2006-03-27 01:17:54 -08001169 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 r = populate_table(t, param, param_size);
1172 if (r) {
Mikulas Patockaf80a5572009-03-16 17:44:26 +00001173 dm_table_destroy(t);
Mike Anderson1134e5a2006-03-27 01:17:54 -08001174 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
1176
Martin K. Petersen9c470082009-04-09 00:27:12 +01001177 r = table_prealloc_integrity(t, md);
1178 if (r) {
1179 DMERR("%s: could not register integrity profile.",
1180 dm_device_name(md));
1181 dm_table_destroy(t);
1182 goto out;
1183 }
1184
Kiyoshi Uedae6ee8c02009-06-22 10:12:36 +01001185 r = dm_table_alloc_md_mempools(t);
1186 if (r) {
1187 DMWARN("unable to allocate mempools for this table");
1188 dm_table_destroy(t);
1189 goto out;
1190 }
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 down_write(&_hash_lock);
Mike Anderson1134e5a2006-03-27 01:17:54 -08001193 hc = dm_get_mdptr(md);
1194 if (!hc || hc->md != md) {
1195 DMWARN("device has been removed from the dev hash table.");
Mikulas Patockaf80a5572009-03-16 17:44:26 +00001196 dm_table_destroy(t);
Mike Anderson1134e5a2006-03-27 01:17:54 -08001197 up_write(&_hash_lock);
1198 r = -ENXIO;
1199 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
1201
1202 if (hc->new_map)
Mikulas Patockad5816872009-01-06 03:05:10 +00001203 dm_table_destroy(hc->new_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 hc->new_map = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 up_write(&_hash_lock);
Mike Anderson1134e5a2006-03-27 01:17:54 -08001206
1207 param->flags |= DM_INACTIVE_PRESENT_FLAG;
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001208 __dev_status(md, param);
Mike Anderson1134e5a2006-03-27 01:17:54 -08001209
1210out:
1211 dm_put(md);
1212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 return r;
1214}
1215
1216static int table_clear(struct dm_ioctl *param, size_t param_size)
1217{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 struct hash_cell *hc;
Jeff Mahoney7ec75f22006-06-26 00:27:24 -07001219 struct mapped_device *md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 down_write(&_hash_lock);
1222
1223 hc = __find_device_hash_cell(param);
1224 if (!hc) {
1225 DMWARN("device doesn't appear to be in the dev hash table.");
1226 up_write(&_hash_lock);
1227 return -ENXIO;
1228 }
1229
1230 if (hc->new_map) {
Mikulas Patockad5816872009-01-06 03:05:10 +00001231 dm_table_destroy(hc->new_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 hc->new_map = NULL;
1233 }
1234
1235 param->flags &= ~DM_INACTIVE_PRESENT_FLAG;
1236
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001237 __dev_status(hc->md, param);
Jeff Mahoney7ec75f22006-06-26 00:27:24 -07001238 md = hc->md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 up_write(&_hash_lock);
Jeff Mahoney7ec75f22006-06-26 00:27:24 -07001240 dm_put(md);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001241
1242 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
1245/*
1246 * Retrieves a list of devices used by a particular dm device.
1247 */
1248static void retrieve_deps(struct dm_table *table,
1249 struct dm_ioctl *param, size_t param_size)
1250{
1251 unsigned int count = 0;
1252 struct list_head *tmp;
1253 size_t len, needed;
Mikulas Patocka82b15192008-10-10 13:37:09 +01001254 struct dm_dev_internal *dd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 struct dm_target_deps *deps;
1256
1257 deps = get_result_buffer(param, param_size, &len);
1258
1259 /*
1260 * Count the devices.
1261 */
1262 list_for_each (tmp, dm_table_get_devices(table))
1263 count++;
1264
1265 /*
1266 * Check we have enough space.
1267 */
1268 needed = sizeof(*deps) + (sizeof(*deps->dev) * count);
1269 if (len < needed) {
1270 param->flags |= DM_BUFFER_FULL_FLAG;
1271 return;
1272 }
1273
1274 /*
1275 * Fill in the devices.
1276 */
1277 deps->count = count;
1278 count = 0;
1279 list_for_each_entry (dd, dm_table_get_devices(table), list)
Mikulas Patocka82b15192008-10-10 13:37:09 +01001280 deps->dev[count++] = huge_encode_dev(dd->dm_dev.bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 param->data_size = param->data_start + needed;
1283}
1284
1285static int table_deps(struct dm_ioctl *param, size_t param_size)
1286{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 struct mapped_device *md;
1288 struct dm_table *table;
1289
1290 md = find_device(param);
1291 if (!md)
1292 return -ENXIO;
1293
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001294 __dev_status(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +00001296 table = dm_get_live_or_inactive_table(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 if (table) {
1298 retrieve_deps(table, param, param_size);
1299 dm_table_put(table);
1300 }
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 dm_put(md);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001303
1304 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305}
1306
1307/*
1308 * Return the status of a device as a text string for each
1309 * target.
1310 */
1311static int table_status(struct dm_ioctl *param, size_t param_size)
1312{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 struct mapped_device *md;
1314 struct dm_table *table;
1315
1316 md = find_device(param);
1317 if (!md)
1318 return -ENXIO;
1319
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001320 __dev_status(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Mike Snitzer1d0f3ce2009-12-10 23:52:22 +00001322 table = dm_get_live_or_inactive_table(md, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 if (table) {
1324 retrieve_status(table, param, param_size);
1325 dm_table_put(table);
1326 }
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 dm_put(md);
Alasdair G Kergon094ea9a2010-08-12 04:13:52 +01001329
1330 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331}
1332
1333/*
1334 * Pass a message to the target that's at the supplied device offset.
1335 */
1336static int target_message(struct dm_ioctl *param, size_t param_size)
1337{
1338 int r, argc;
1339 char **argv;
1340 struct mapped_device *md;
1341 struct dm_table *table;
1342 struct dm_target *ti;
1343 struct dm_target_msg *tmsg = (void *) param + param->data_start;
1344
1345 md = find_device(param);
1346 if (!md)
1347 return -ENXIO;
1348
Milan Broz027d50f2007-10-19 22:38:36 +01001349 if (tmsg < (struct dm_target_msg *) param->data ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 invalid_str(tmsg->message, (void *) param + param_size)) {
1351 DMWARN("Invalid target message parameters.");
1352 r = -EINVAL;
1353 goto out;
1354 }
1355
1356 r = dm_split_args(&argc, &argv, tmsg->message);
1357 if (r) {
1358 DMWARN("Failed to split target message parameters");
1359 goto out;
1360 }
1361
Alasdair G Kergon7c666412009-12-10 23:52:19 +00001362 table = dm_get_live_table(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 if (!table)
1364 goto out_argv;
1365
Mike Andersonc50abeb2009-12-10 23:52:20 +00001366 if (dm_deleting_md(md)) {
1367 r = -ENXIO;
1368 goto out_table;
1369 }
1370
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001371 ti = dm_table_find_target(table, tmsg->sector);
1372 if (!dm_target_is_valid(ti)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 DMWARN("Target message sector outside device.");
1374 r = -EINVAL;
Jun'ichi Nomura512875b2007-12-13 14:15:25 +00001375 } else if (ti->type->message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 r = ti->type->message(ti, argc, argv);
1377 else {
1378 DMWARN("Target type does not support messages");
1379 r = -EINVAL;
1380 }
1381
Mike Andersonc50abeb2009-12-10 23:52:20 +00001382 out_table:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 dm_table_put(table);
1384 out_argv:
1385 kfree(argv);
1386 out:
1387 param->data_size = 0;
1388 dm_put(md);
1389 return r;
1390}
1391
1392/*-----------------------------------------------------------------
1393 * Implementation of open/close/ioctl on the special char
1394 * device.
1395 *---------------------------------------------------------------*/
1396static ioctl_fn lookup_ioctl(unsigned int cmd)
1397{
1398 static struct {
1399 int cmd;
1400 ioctl_fn fn;
1401 } _ioctls[] = {
1402 {DM_VERSION_CMD, NULL}, /* version is dealt with elsewhere */
1403 {DM_REMOVE_ALL_CMD, remove_all},
1404 {DM_LIST_DEVICES_CMD, list_devices},
1405
1406 {DM_DEV_CREATE_CMD, dev_create},
1407 {DM_DEV_REMOVE_CMD, dev_remove},
1408 {DM_DEV_RENAME_CMD, dev_rename},
1409 {DM_DEV_SUSPEND_CMD, dev_suspend},
1410 {DM_DEV_STATUS_CMD, dev_status},
1411 {DM_DEV_WAIT_CMD, dev_wait},
1412
1413 {DM_TABLE_LOAD_CMD, table_load},
1414 {DM_TABLE_CLEAR_CMD, table_clear},
1415 {DM_TABLE_DEPS_CMD, table_deps},
1416 {DM_TABLE_STATUS_CMD, table_status},
1417
1418 {DM_LIST_VERSIONS_CMD, list_versions},
1419
Darrick J. Wong3ac51e72006-03-27 01:17:54 -08001420 {DM_TARGET_MSG_CMD, target_message},
1421 {DM_DEV_SET_GEOMETRY_CMD, dev_set_geometry}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 };
1423
1424 return (cmd >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[cmd].fn;
1425}
1426
1427/*
1428 * As well as checking the version compatibility this always
1429 * copies the kernel interface version out.
1430 */
1431static int check_version(unsigned int cmd, struct dm_ioctl __user *user)
1432{
1433 uint32_t version[3];
1434 int r = 0;
1435
1436 if (copy_from_user(version, user->version, sizeof(version)))
1437 return -EFAULT;
1438
1439 if ((DM_VERSION_MAJOR != version[0]) ||
1440 (DM_VERSION_MINOR < version[1])) {
1441 DMWARN("ioctl interface mismatch: "
1442 "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)",
1443 DM_VERSION_MAJOR, DM_VERSION_MINOR,
1444 DM_VERSION_PATCHLEVEL,
1445 version[0], version[1], version[2], cmd);
1446 r = -EINVAL;
1447 }
1448
1449 /*
1450 * Fill in the kernel version.
1451 */
1452 version[0] = DM_VERSION_MAJOR;
1453 version[1] = DM_VERSION_MINOR;
1454 version[2] = DM_VERSION_PATCHLEVEL;
1455 if (copy_to_user(user->version, version, sizeof(version)))
1456 return -EFAULT;
1457
1458 return r;
1459}
1460
1461static void free_params(struct dm_ioctl *param)
1462{
1463 vfree(param);
1464}
1465
1466static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl **param)
1467{
1468 struct dm_ioctl tmp, *dmi;
1469
Milan Broz76c072b2008-02-08 02:09:56 +00001470 if (copy_from_user(&tmp, user, sizeof(tmp) - sizeof(tmp.data)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 return -EFAULT;
1472
Milan Broz76c072b2008-02-08 02:09:56 +00001473 if (tmp.data_size < (sizeof(tmp) - sizeof(tmp.data)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return -EINVAL;
1475
Jesper Juhlbb56acf2007-10-19 22:38:54 +01001476 dmi = vmalloc(tmp.data_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 if (!dmi)
1478 return -ENOMEM;
1479
1480 if (copy_from_user(dmi, user, tmp.data_size)) {
1481 vfree(dmi);
1482 return -EFAULT;
1483 }
1484
1485 *param = dmi;
1486 return 0;
1487}
1488
1489static int validate_params(uint cmd, struct dm_ioctl *param)
1490{
1491 /* Always clear this flag */
1492 param->flags &= ~DM_BUFFER_FULL_FLAG;
Peter Rajnoha3abf85b2010-03-06 02:32:31 +00001493 param->flags &= ~DM_UEVENT_GENERATED_FLAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495 /* Ignores parameters */
1496 if (cmd == DM_REMOVE_ALL_CMD ||
1497 cmd == DM_LIST_DEVICES_CMD ||
1498 cmd == DM_LIST_VERSIONS_CMD)
1499 return 0;
1500
1501 if ((cmd == DM_DEV_CREATE_CMD)) {
1502 if (!*param->name) {
1503 DMWARN("name not supplied when creating device");
1504 return -EINVAL;
1505 }
1506 } else if ((*param->uuid && *param->name)) {
1507 DMWARN("only supply one of name or uuid, cmd(%u)", cmd);
1508 return -EINVAL;
1509 }
1510
1511 /* Ensure strings are terminated */
1512 param->name[DM_NAME_LEN - 1] = '\0';
1513 param->uuid[DM_UUID_LEN - 1] = '\0';
1514
1515 return 0;
1516}
1517
Alasdair G Kergon27238b22008-02-08 02:09:53 +00001518static int ctl_ioctl(uint command, struct dm_ioctl __user *user)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
1520 int r = 0;
1521 unsigned int cmd;
Andrew Mortona26ffd42008-02-08 02:10:16 +00001522 struct dm_ioctl *uninitialized_var(param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 ioctl_fn fn = NULL;
1524 size_t param_size;
1525
1526 /* only root can play with this */
1527 if (!capable(CAP_SYS_ADMIN))
1528 return -EACCES;
1529
1530 if (_IOC_TYPE(command) != DM_IOCTL)
1531 return -ENOTTY;
1532
1533 cmd = _IOC_NR(command);
1534
1535 /*
1536 * Check the interface version passed in. This also
1537 * writes out the kernel's interface version.
1538 */
1539 r = check_version(cmd, user);
1540 if (r)
1541 return r;
1542
1543 /*
1544 * Nothing more to do for the version command.
1545 */
1546 if (cmd == DM_VERSION_CMD)
1547 return 0;
1548
1549 fn = lookup_ioctl(cmd);
1550 if (!fn) {
1551 DMWARN("dm_ctl_ioctl: unknown command 0x%x", command);
1552 return -ENOTTY;
1553 }
1554
1555 /*
1556 * Trying to avoid low memory issues when a device is
1557 * suspended.
1558 */
1559 current->flags |= PF_MEMALLOC;
1560
1561 /*
1562 * Copy the parameters into kernel space.
1563 */
1564 r = copy_params(user, &param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Alasdair G Kergondab6a422006-02-01 03:04:52 -08001566 current->flags &= ~PF_MEMALLOC;
1567
1568 if (r)
1569 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571 r = validate_params(cmd, param);
1572 if (r)
1573 goto out;
1574
1575 param_size = param->data_size;
1576 param->data_size = sizeof(*param);
1577 r = fn(param, param_size);
1578
1579 /*
1580 * Copy the results back to userland.
1581 */
1582 if (!r && copy_to_user(user, param, param->data_size))
1583 r = -EFAULT;
1584
1585 out:
1586 free_params(param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 return r;
1588}
1589
Alasdair G Kergon27238b22008-02-08 02:09:53 +00001590static long dm_ctl_ioctl(struct file *file, uint command, ulong u)
1591{
1592 return (long)ctl_ioctl(command, (struct dm_ioctl __user *)u);
1593}
1594
Milan Broz76c072b2008-02-08 02:09:56 +00001595#ifdef CONFIG_COMPAT
1596static long dm_compat_ctl_ioctl(struct file *file, uint command, ulong u)
1597{
1598 return (long)dm_ctl_ioctl(file, command, (ulong) compat_ptr(u));
1599}
1600#else
1601#define dm_compat_ctl_ioctl NULL
1602#endif
1603
Arjan van de Venfa027c22007-02-12 00:55:33 -08001604static const struct file_operations _ctl_fops = {
Arnd Bergmann402ab352010-08-12 04:13:57 +01001605 .open = nonseekable_open,
Alasdair G Kergon27238b22008-02-08 02:09:53 +00001606 .unlocked_ioctl = dm_ctl_ioctl,
Milan Broz76c072b2008-02-08 02:09:56 +00001607 .compat_ioctl = dm_compat_ctl_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 .owner = THIS_MODULE,
1609};
1610
1611static struct miscdevice _dm_misc = {
1612 .minor = MISC_DYNAMIC_MINOR,
1613 .name = DM_NAME,
Kay Sieverse454cea2009-09-18 23:01:12 +02001614 .nodename = "mapper/control",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 .fops = &_ctl_fops
1616};
1617
1618/*
1619 * Create misc character device and link to DM_DIR/control.
1620 */
1621int __init dm_interface_init(void)
1622{
1623 int r;
1624
1625 r = dm_hash_init();
1626 if (r)
1627 return r;
1628
1629 r = misc_register(&_dm_misc);
1630 if (r) {
1631 DMERR("misc_register failed for control device");
1632 dm_hash_exit();
1633 return r;
1634 }
1635
1636 DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR,
1637 DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA,
1638 DM_DRIVER_EMAIL);
1639 return 0;
1640}
1641
1642void dm_interface_exit(void)
1643{
1644 if (misc_deregister(&_dm_misc) < 0)
1645 DMERR("misc_deregister failed for control device");
1646
1647 dm_hash_exit();
1648}
Mike Anderson96a1f7d2007-10-19 22:47:59 +01001649
1650/**
1651 * dm_copy_name_and_uuid - Copy mapped device name & uuid into supplied buffers
1652 * @md: Pointer to mapped_device
1653 * @name: Buffer (size DM_NAME_LEN) for name
1654 * @uuid: Buffer (size DM_UUID_LEN) for uuid or empty string if uuid not defined
1655 */
1656int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid)
1657{
1658 int r = 0;
1659 struct hash_cell *hc;
1660
1661 if (!md)
1662 return -ENXIO;
1663
Mikulas Patocka60769052009-12-10 23:51:52 +00001664 mutex_lock(&dm_hash_cells_mutex);
Mike Anderson96a1f7d2007-10-19 22:47:59 +01001665 hc = dm_get_mdptr(md);
1666 if (!hc || hc->md != md) {
1667 r = -ENXIO;
1668 goto out;
1669 }
1670
Milan Broz23d39f62009-01-06 03:05:04 +00001671 if (name)
1672 strcpy(name, hc->name);
1673 if (uuid)
1674 strcpy(uuid, hc->uuid ? : "");
Mike Anderson96a1f7d2007-10-19 22:47:59 +01001675
1676out:
Mikulas Patocka60769052009-12-10 23:51:52 +00001677 mutex_unlock(&dm_hash_cells_mutex);
Mike Anderson96a1f7d2007-10-19 22:47:59 +01001678
1679 return r;
1680}