Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2001, 2002 Sistina Software (UK) Limited. |
| 3 | * Copyright (C) 2004 - 2005 Red Hat, Inc. All rights reserved. |
| 4 | * |
| 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> |
| 16 | #include <linux/devfs_fs_kernel.h> |
| 17 | #include <linux/dm-ioctl.h> |
Darrick J. Wong | 3ac51e7 | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 18 | #include <linux/hdreg.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 20 | #include <asm/uaccess.h> |
| 21 | |
| 22 | #define DM_DRIVER_EMAIL "dm-devel@redhat.com" |
| 23 | |
| 24 | /*----------------------------------------------------------------- |
| 25 | * The ioctl interface needs to be able to look up devices by |
| 26 | * name or uuid. |
| 27 | *---------------------------------------------------------------*/ |
| 28 | struct hash_cell { |
| 29 | struct list_head name_list; |
| 30 | struct list_head uuid_list; |
| 31 | |
| 32 | char *name; |
| 33 | char *uuid; |
| 34 | struct mapped_device *md; |
| 35 | struct dm_table *new_map; |
| 36 | }; |
| 37 | |
| 38 | struct vers_iter { |
| 39 | size_t param_size; |
| 40 | struct dm_target_versions *vers, *old_vers; |
| 41 | char *end; |
| 42 | uint32_t flags; |
| 43 | }; |
| 44 | |
| 45 | |
| 46 | #define NUM_BUCKETS 64 |
| 47 | #define MASK_BUCKETS (NUM_BUCKETS - 1) |
| 48 | static struct list_head _name_buckets[NUM_BUCKETS]; |
| 49 | static struct list_head _uuid_buckets[NUM_BUCKETS]; |
| 50 | |
| 51 | static void dm_hash_remove_all(void); |
| 52 | |
| 53 | /* |
| 54 | * Guards access to both hash tables. |
| 55 | */ |
| 56 | static DECLARE_RWSEM(_hash_lock); |
| 57 | |
| 58 | static void init_buckets(struct list_head *buckets) |
| 59 | { |
| 60 | unsigned int i; |
| 61 | |
| 62 | for (i = 0; i < NUM_BUCKETS; i++) |
| 63 | INIT_LIST_HEAD(buckets + i); |
| 64 | } |
| 65 | |
| 66 | static int dm_hash_init(void) |
| 67 | { |
| 68 | init_buckets(_name_buckets); |
| 69 | init_buckets(_uuid_buckets); |
| 70 | devfs_mk_dir(DM_DIR); |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | static void dm_hash_exit(void) |
| 75 | { |
| 76 | dm_hash_remove_all(); |
| 77 | devfs_remove(DM_DIR); |
| 78 | } |
| 79 | |
| 80 | /*----------------------------------------------------------------- |
| 81 | * Hash function: |
| 82 | * We're not really concerned with the str hash function being |
| 83 | * fast since it's only used by the ioctl interface. |
| 84 | *---------------------------------------------------------------*/ |
| 85 | static unsigned int hash_str(const char *str) |
| 86 | { |
| 87 | const unsigned int hash_mult = 2654435387U; |
| 88 | unsigned int h = 0; |
| 89 | |
| 90 | while (*str) |
| 91 | h = (h + (unsigned int) *str++) * hash_mult; |
| 92 | |
| 93 | return h & MASK_BUCKETS; |
| 94 | } |
| 95 | |
| 96 | /*----------------------------------------------------------------- |
| 97 | * Code for looking up a device by name |
| 98 | *---------------------------------------------------------------*/ |
| 99 | static struct hash_cell *__get_name_cell(const char *str) |
| 100 | { |
| 101 | struct hash_cell *hc; |
| 102 | unsigned int h = hash_str(str); |
| 103 | |
| 104 | list_for_each_entry (hc, _name_buckets + h, name_list) |
| 105 | if (!strcmp(hc->name, str)) |
| 106 | return hc; |
| 107 | |
| 108 | return NULL; |
| 109 | } |
| 110 | |
| 111 | static struct hash_cell *__get_uuid_cell(const char *str) |
| 112 | { |
| 113 | struct hash_cell *hc; |
| 114 | unsigned int h = hash_str(str); |
| 115 | |
| 116 | list_for_each_entry (hc, _uuid_buckets + h, uuid_list) |
| 117 | if (!strcmp(hc->uuid, str)) |
| 118 | return hc; |
| 119 | |
| 120 | return NULL; |
| 121 | } |
| 122 | |
| 123 | /*----------------------------------------------------------------- |
| 124 | * Inserting, removing and renaming a device. |
| 125 | *---------------------------------------------------------------*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | static struct hash_cell *alloc_cell(const char *name, const char *uuid, |
| 127 | struct mapped_device *md) |
| 128 | { |
| 129 | struct hash_cell *hc; |
| 130 | |
| 131 | hc = kmalloc(sizeof(*hc), GFP_KERNEL); |
| 132 | if (!hc) |
| 133 | return NULL; |
| 134 | |
Paulo Marques | 543537b | 2005-06-23 00:09:02 -0700 | [diff] [blame] | 135 | hc->name = kstrdup(name, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | if (!hc->name) { |
| 137 | kfree(hc); |
| 138 | return NULL; |
| 139 | } |
| 140 | |
| 141 | if (!uuid) |
| 142 | hc->uuid = NULL; |
| 143 | |
| 144 | else { |
Paulo Marques | 543537b | 2005-06-23 00:09:02 -0700 | [diff] [blame] | 145 | hc->uuid = kstrdup(uuid, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | if (!hc->uuid) { |
| 147 | kfree(hc->name); |
| 148 | kfree(hc); |
| 149 | return NULL; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | INIT_LIST_HEAD(&hc->name_list); |
| 154 | INIT_LIST_HEAD(&hc->uuid_list); |
| 155 | hc->md = md; |
| 156 | hc->new_map = NULL; |
| 157 | return hc; |
| 158 | } |
| 159 | |
| 160 | static void free_cell(struct hash_cell *hc) |
| 161 | { |
| 162 | if (hc) { |
| 163 | kfree(hc->name); |
| 164 | kfree(hc->uuid); |
| 165 | kfree(hc); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * devfs stuff. |
| 171 | */ |
| 172 | static int register_with_devfs(struct hash_cell *hc) |
| 173 | { |
| 174 | struct gendisk *disk = dm_disk(hc->md); |
| 175 | |
| 176 | devfs_mk_bdev(MKDEV(disk->major, disk->first_minor), |
| 177 | S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP, |
| 178 | DM_DIR "/%s", hc->name); |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | static int unregister_with_devfs(struct hash_cell *hc) |
| 183 | { |
| 184 | devfs_remove(DM_DIR"/%s", hc->name); |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * The kdev_t and uuid of a device can never change once it is |
| 190 | * initially inserted. |
| 191 | */ |
| 192 | static int dm_hash_insert(const char *name, const char *uuid, struct mapped_device *md) |
| 193 | { |
| 194 | struct hash_cell *cell; |
| 195 | |
| 196 | /* |
| 197 | * Allocate the new cells. |
| 198 | */ |
| 199 | cell = alloc_cell(name, uuid, md); |
| 200 | if (!cell) |
| 201 | return -ENOMEM; |
| 202 | |
| 203 | /* |
| 204 | * Insert the cell into both hash tables. |
| 205 | */ |
| 206 | down_write(&_hash_lock); |
| 207 | if (__get_name_cell(name)) |
| 208 | goto bad; |
| 209 | |
| 210 | list_add(&cell->name_list, _name_buckets + hash_str(name)); |
| 211 | |
| 212 | if (uuid) { |
| 213 | if (__get_uuid_cell(uuid)) { |
| 214 | list_del(&cell->name_list); |
| 215 | goto bad; |
| 216 | } |
| 217 | list_add(&cell->uuid_list, _uuid_buckets + hash_str(uuid)); |
| 218 | } |
| 219 | register_with_devfs(cell); |
| 220 | dm_get(md); |
| 221 | dm_set_mdptr(md, cell); |
| 222 | up_write(&_hash_lock); |
| 223 | |
| 224 | return 0; |
| 225 | |
| 226 | bad: |
| 227 | up_write(&_hash_lock); |
| 228 | free_cell(cell); |
| 229 | return -EBUSY; |
| 230 | } |
| 231 | |
| 232 | static void __hash_remove(struct hash_cell *hc) |
| 233 | { |
goggin, edward | 269fd2a | 2005-09-27 21:45:44 -0700 | [diff] [blame] | 234 | struct dm_table *table; |
| 235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | /* remove from the dev hash */ |
| 237 | list_del(&hc->uuid_list); |
| 238 | list_del(&hc->name_list); |
| 239 | unregister_with_devfs(hc); |
| 240 | dm_set_mdptr(hc->md, NULL); |
goggin, edward | 269fd2a | 2005-09-27 21:45:44 -0700 | [diff] [blame] | 241 | |
| 242 | table = dm_get_table(hc->md); |
| 243 | if (table) { |
| 244 | dm_table_event(table); |
| 245 | dm_table_put(table); |
| 246 | } |
| 247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | if (hc->new_map) |
| 249 | dm_table_put(hc->new_map); |
Mike Anderson | 1134e5a | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 250 | dm_put(hc->md); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | free_cell(hc); |
| 252 | } |
| 253 | |
| 254 | static void dm_hash_remove_all(void) |
| 255 | { |
| 256 | int i; |
| 257 | struct hash_cell *hc; |
| 258 | struct list_head *tmp, *n; |
| 259 | |
| 260 | down_write(&_hash_lock); |
| 261 | for (i = 0; i < NUM_BUCKETS; i++) { |
| 262 | list_for_each_safe (tmp, n, _name_buckets + i) { |
| 263 | hc = list_entry(tmp, struct hash_cell, name_list); |
| 264 | __hash_remove(hc); |
| 265 | } |
| 266 | } |
| 267 | up_write(&_hash_lock); |
| 268 | } |
| 269 | |
| 270 | static int dm_hash_rename(const char *old, const char *new) |
| 271 | { |
| 272 | char *new_name, *old_name; |
| 273 | struct hash_cell *hc; |
goggin, edward | 81f1777 | 2006-01-06 00:20:01 -0800 | [diff] [blame] | 274 | struct dm_table *table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | |
| 276 | /* |
| 277 | * duplicate new. |
| 278 | */ |
Paulo Marques | 543537b | 2005-06-23 00:09:02 -0700 | [diff] [blame] | 279 | new_name = kstrdup(new, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | if (!new_name) |
| 281 | return -ENOMEM; |
| 282 | |
| 283 | down_write(&_hash_lock); |
| 284 | |
| 285 | /* |
| 286 | * Is new free ? |
| 287 | */ |
| 288 | hc = __get_name_cell(new); |
| 289 | if (hc) { |
| 290 | DMWARN("asked to rename to an already existing name %s -> %s", |
| 291 | old, new); |
| 292 | up_write(&_hash_lock); |
| 293 | kfree(new_name); |
| 294 | return -EBUSY; |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * Is there such a device as 'old' ? |
| 299 | */ |
| 300 | hc = __get_name_cell(old); |
| 301 | if (!hc) { |
| 302 | DMWARN("asked to rename a non existent device %s -> %s", |
| 303 | old, new); |
| 304 | up_write(&_hash_lock); |
| 305 | kfree(new_name); |
| 306 | return -ENXIO; |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * rename and move the name cell. |
| 311 | */ |
| 312 | unregister_with_devfs(hc); |
| 313 | |
| 314 | list_del(&hc->name_list); |
| 315 | old_name = hc->name; |
| 316 | hc->name = new_name; |
| 317 | list_add(&hc->name_list, _name_buckets + hash_str(new_name)); |
| 318 | |
| 319 | /* rename the device node in devfs */ |
| 320 | register_with_devfs(hc); |
| 321 | |
goggin, edward | 81f1777 | 2006-01-06 00:20:01 -0800 | [diff] [blame] | 322 | /* |
| 323 | * Wake up any dm event waiters. |
| 324 | */ |
| 325 | table = dm_get_table(hc->md); |
| 326 | if (table) { |
| 327 | dm_table_event(table); |
| 328 | dm_table_put(table); |
| 329 | } |
| 330 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | up_write(&_hash_lock); |
| 332 | kfree(old_name); |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | /*----------------------------------------------------------------- |
| 337 | * Implementation of the ioctl commands |
| 338 | *---------------------------------------------------------------*/ |
| 339 | /* |
| 340 | * All the ioctl commands get dispatched to functions with this |
| 341 | * prototype. |
| 342 | */ |
| 343 | typedef int (*ioctl_fn)(struct dm_ioctl *param, size_t param_size); |
| 344 | |
| 345 | static int remove_all(struct dm_ioctl *param, size_t param_size) |
| 346 | { |
| 347 | dm_hash_remove_all(); |
| 348 | param->data_size = 0; |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | /* |
| 353 | * Round up the ptr to an 8-byte boundary. |
| 354 | */ |
| 355 | #define ALIGN_MASK 7 |
| 356 | static inline void *align_ptr(void *ptr) |
| 357 | { |
| 358 | return (void *) (((size_t) (ptr + ALIGN_MASK)) & ~ALIGN_MASK); |
| 359 | } |
| 360 | |
| 361 | /* |
| 362 | * Retrieves the data payload buffer from an already allocated |
| 363 | * struct dm_ioctl. |
| 364 | */ |
| 365 | static void *get_result_buffer(struct dm_ioctl *param, size_t param_size, |
| 366 | size_t *len) |
| 367 | { |
| 368 | param->data_start = align_ptr(param + 1) - (void *) param; |
| 369 | |
| 370 | if (param->data_start < param_size) |
| 371 | *len = param_size - param->data_start; |
| 372 | else |
| 373 | *len = 0; |
| 374 | |
| 375 | return ((void *) param) + param->data_start; |
| 376 | } |
| 377 | |
| 378 | static int list_devices(struct dm_ioctl *param, size_t param_size) |
| 379 | { |
| 380 | unsigned int i; |
| 381 | struct hash_cell *hc; |
| 382 | size_t len, needed = 0; |
| 383 | struct gendisk *disk; |
| 384 | struct dm_name_list *nl, *old_nl = NULL; |
| 385 | |
| 386 | down_write(&_hash_lock); |
| 387 | |
| 388 | /* |
| 389 | * Loop through all the devices working out how much |
| 390 | * space we need. |
| 391 | */ |
| 392 | for (i = 0; i < NUM_BUCKETS; i++) { |
| 393 | list_for_each_entry (hc, _name_buckets + i, name_list) { |
| 394 | needed += sizeof(struct dm_name_list); |
| 395 | needed += strlen(hc->name) + 1; |
| 396 | needed += ALIGN_MASK; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | /* |
| 401 | * Grab our output buffer. |
| 402 | */ |
| 403 | nl = get_result_buffer(param, param_size, &len); |
| 404 | if (len < needed) { |
| 405 | param->flags |= DM_BUFFER_FULL_FLAG; |
| 406 | goto out; |
| 407 | } |
| 408 | param->data_size = param->data_start + needed; |
| 409 | |
| 410 | nl->dev = 0; /* Flags no data */ |
| 411 | |
| 412 | /* |
| 413 | * Now loop through filling out the names. |
| 414 | */ |
| 415 | for (i = 0; i < NUM_BUCKETS; i++) { |
| 416 | list_for_each_entry (hc, _name_buckets + i, name_list) { |
| 417 | if (old_nl) |
| 418 | old_nl->next = (uint32_t) ((void *) nl - |
| 419 | (void *) old_nl); |
| 420 | disk = dm_disk(hc->md); |
| 421 | nl->dev = huge_encode_dev(MKDEV(disk->major, disk->first_minor)); |
| 422 | nl->next = 0; |
| 423 | strcpy(nl->name, hc->name); |
| 424 | |
| 425 | old_nl = nl; |
| 426 | nl = align_ptr(((void *) ++nl) + strlen(hc->name) + 1); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | out: |
| 431 | up_write(&_hash_lock); |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | static void list_version_get_needed(struct target_type *tt, void *needed_param) |
| 436 | { |
| 437 | size_t *needed = needed_param; |
| 438 | |
Alasdair G Kergon | c4cc663 | 2005-11-21 21:32:33 -0800 | [diff] [blame] | 439 | *needed += sizeof(struct dm_target_versions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | *needed += strlen(tt->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | *needed += ALIGN_MASK; |
| 442 | } |
| 443 | |
| 444 | static void list_version_get_info(struct target_type *tt, void *param) |
| 445 | { |
| 446 | struct vers_iter *info = param; |
| 447 | |
| 448 | /* Check space - it might have changed since the first iteration */ |
| 449 | if ((char *)info->vers + sizeof(tt->version) + strlen(tt->name) + 1 > |
| 450 | info->end) { |
| 451 | |
| 452 | info->flags = DM_BUFFER_FULL_FLAG; |
| 453 | return; |
| 454 | } |
| 455 | |
| 456 | if (info->old_vers) |
| 457 | info->old_vers->next = (uint32_t) ((void *)info->vers - |
| 458 | (void *)info->old_vers); |
| 459 | info->vers->version[0] = tt->version[0]; |
| 460 | info->vers->version[1] = tt->version[1]; |
| 461 | info->vers->version[2] = tt->version[2]; |
| 462 | info->vers->next = 0; |
| 463 | strcpy(info->vers->name, tt->name); |
| 464 | |
| 465 | info->old_vers = info->vers; |
| 466 | info->vers = align_ptr(((void *) ++info->vers) + strlen(tt->name) + 1); |
| 467 | } |
| 468 | |
| 469 | static int list_versions(struct dm_ioctl *param, size_t param_size) |
| 470 | { |
| 471 | size_t len, needed = 0; |
| 472 | struct dm_target_versions *vers; |
| 473 | struct vers_iter iter_info; |
| 474 | |
| 475 | /* |
| 476 | * Loop through all the devices working out how much |
| 477 | * space we need. |
| 478 | */ |
| 479 | dm_target_iterate(list_version_get_needed, &needed); |
| 480 | |
| 481 | /* |
| 482 | * Grab our output buffer. |
| 483 | */ |
| 484 | vers = get_result_buffer(param, param_size, &len); |
| 485 | if (len < needed) { |
| 486 | param->flags |= DM_BUFFER_FULL_FLAG; |
| 487 | goto out; |
| 488 | } |
| 489 | param->data_size = param->data_start + needed; |
| 490 | |
| 491 | iter_info.param_size = param_size; |
| 492 | iter_info.old_vers = NULL; |
| 493 | iter_info.vers = vers; |
| 494 | iter_info.flags = 0; |
| 495 | iter_info.end = (char *)vers+len; |
| 496 | |
| 497 | /* |
| 498 | * Now loop through filling out the names & versions. |
| 499 | */ |
| 500 | dm_target_iterate(list_version_get_info, &iter_info); |
| 501 | param->flags |= iter_info.flags; |
| 502 | |
| 503 | out: |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | |
| 508 | |
| 509 | static int check_name(const char *name) |
| 510 | { |
| 511 | if (strchr(name, '/')) { |
| 512 | DMWARN("invalid device name"); |
| 513 | return -EINVAL; |
| 514 | } |
| 515 | |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | /* |
| 520 | * Fills in a dm_ioctl structure, ready for sending back to |
| 521 | * userland. |
| 522 | */ |
| 523 | static int __dev_status(struct mapped_device *md, struct dm_ioctl *param) |
| 524 | { |
| 525 | struct gendisk *disk = dm_disk(md); |
| 526 | struct dm_table *table; |
| 527 | struct block_device *bdev; |
| 528 | |
| 529 | param->flags &= ~(DM_SUSPEND_FLAG | DM_READONLY_FLAG | |
| 530 | DM_ACTIVE_PRESENT_FLAG); |
| 531 | |
| 532 | if (dm_suspended(md)) |
| 533 | param->flags |= DM_SUSPEND_FLAG; |
| 534 | |
| 535 | param->dev = huge_encode_dev(MKDEV(disk->major, disk->first_minor)); |
| 536 | |
| 537 | if (!(param->flags & DM_SKIP_BDGET_FLAG)) { |
| 538 | bdev = bdget_disk(disk, 0); |
| 539 | if (!bdev) |
| 540 | return -ENXIO; |
| 541 | |
| 542 | /* |
| 543 | * Yes, this will be out of date by the time it gets back |
| 544 | * to userland, but it is still very useful for |
| 545 | * debugging. |
| 546 | */ |
| 547 | param->open_count = bdev->bd_openers; |
| 548 | bdput(bdev); |
| 549 | } else |
| 550 | param->open_count = -1; |
| 551 | |
| 552 | if (disk->policy) |
| 553 | param->flags |= DM_READONLY_FLAG; |
| 554 | |
| 555 | param->event_nr = dm_get_event_nr(md); |
| 556 | |
| 557 | table = dm_get_table(md); |
| 558 | if (table) { |
| 559 | param->flags |= DM_ACTIVE_PRESENT_FLAG; |
| 560 | param->target_count = dm_table_get_num_targets(table); |
| 561 | dm_table_put(table); |
| 562 | } else |
| 563 | param->target_count = 0; |
| 564 | |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | static int dev_create(struct dm_ioctl *param, size_t param_size) |
| 569 | { |
| 570 | int r; |
| 571 | struct mapped_device *md; |
| 572 | |
| 573 | r = check_name(param->name); |
| 574 | if (r) |
| 575 | return r; |
| 576 | |
| 577 | if (param->flags & DM_PERSISTENT_DEV_FLAG) |
| 578 | r = dm_create_with_minor(MINOR(huge_decode_dev(param->dev)), &md); |
| 579 | else |
| 580 | r = dm_create(&md); |
| 581 | |
| 582 | if (r) |
| 583 | return r; |
| 584 | |
| 585 | r = dm_hash_insert(param->name, *param->uuid ? param->uuid : NULL, md); |
| 586 | if (r) { |
| 587 | dm_put(md); |
| 588 | return r; |
| 589 | } |
| 590 | |
| 591 | param->flags &= ~DM_INACTIVE_PRESENT_FLAG; |
| 592 | |
| 593 | r = __dev_status(md, param); |
| 594 | dm_put(md); |
| 595 | |
| 596 | return r; |
| 597 | } |
| 598 | |
| 599 | /* |
| 600 | * Always use UUID for lookups if it's present, otherwise use name or dev. |
| 601 | */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 602 | static struct hash_cell *__find_device_hash_cell(struct dm_ioctl *param) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | { |
Alasdair G Kergon | 9ade92a | 2006-03-27 01:17:53 -0800 | [diff] [blame] | 604 | struct mapped_device *md; |
| 605 | void *mdptr = NULL; |
| 606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | if (*param->uuid) |
| 608 | return __get_uuid_cell(param->uuid); |
Alasdair G Kergon | 9ade92a | 2006-03-27 01:17:53 -0800 | [diff] [blame] | 609 | |
| 610 | if (*param->name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | return __get_name_cell(param->name); |
Alasdair G Kergon | 9ade92a | 2006-03-27 01:17:53 -0800 | [diff] [blame] | 612 | |
| 613 | md = dm_get_md(huge_decode_dev(param->dev)); |
| 614 | if (md) { |
| 615 | mdptr = dm_get_mdptr(md); |
| 616 | dm_put(md); |
| 617 | } |
| 618 | |
| 619 | return mdptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | } |
| 621 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 622 | static struct mapped_device *find_device(struct dm_ioctl *param) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | { |
| 624 | struct hash_cell *hc; |
| 625 | struct mapped_device *md = NULL; |
| 626 | |
| 627 | down_read(&_hash_lock); |
| 628 | hc = __find_device_hash_cell(param); |
| 629 | if (hc) { |
| 630 | md = hc->md; |
| 631 | dm_get(md); |
| 632 | |
| 633 | /* |
| 634 | * Sneakily write in both the name and the uuid |
| 635 | * while we have the cell. |
| 636 | */ |
| 637 | strncpy(param->name, hc->name, sizeof(param->name)); |
| 638 | if (hc->uuid) |
| 639 | strncpy(param->uuid, hc->uuid, sizeof(param->uuid)-1); |
| 640 | else |
| 641 | param->uuid[0] = '\0'; |
| 642 | |
| 643 | if (hc->new_map) |
| 644 | param->flags |= DM_INACTIVE_PRESENT_FLAG; |
| 645 | else |
| 646 | param->flags &= ~DM_INACTIVE_PRESENT_FLAG; |
| 647 | } |
| 648 | up_read(&_hash_lock); |
| 649 | |
| 650 | return md; |
| 651 | } |
| 652 | |
| 653 | static int dev_remove(struct dm_ioctl *param, size_t param_size) |
| 654 | { |
| 655 | struct hash_cell *hc; |
| 656 | |
| 657 | down_write(&_hash_lock); |
| 658 | hc = __find_device_hash_cell(param); |
| 659 | |
| 660 | if (!hc) { |
| 661 | DMWARN("device doesn't appear to be in the dev hash table."); |
| 662 | up_write(&_hash_lock); |
| 663 | return -ENXIO; |
| 664 | } |
| 665 | |
| 666 | __hash_remove(hc); |
| 667 | up_write(&_hash_lock); |
| 668 | param->data_size = 0; |
| 669 | return 0; |
| 670 | } |
| 671 | |
| 672 | /* |
| 673 | * Check a string doesn't overrun the chunk of |
| 674 | * memory we copied from userland. |
| 675 | */ |
| 676 | static int invalid_str(char *str, void *end) |
| 677 | { |
| 678 | while ((void *) str < end) |
| 679 | if (!*str++) |
| 680 | return 0; |
| 681 | |
| 682 | return -EINVAL; |
| 683 | } |
| 684 | |
| 685 | static int dev_rename(struct dm_ioctl *param, size_t param_size) |
| 686 | { |
| 687 | int r; |
| 688 | char *new_name = (char *) param + param->data_start; |
| 689 | |
| 690 | if (new_name < (char *) (param + 1) || |
| 691 | invalid_str(new_name, (void *) param + param_size)) { |
| 692 | DMWARN("Invalid new logical volume name supplied."); |
| 693 | return -EINVAL; |
| 694 | } |
| 695 | |
| 696 | r = check_name(new_name); |
| 697 | if (r) |
| 698 | return r; |
| 699 | |
| 700 | param->data_size = 0; |
| 701 | return dm_hash_rename(param->name, new_name); |
| 702 | } |
| 703 | |
Darrick J. Wong | 3ac51e7 | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 704 | static int dev_set_geometry(struct dm_ioctl *param, size_t param_size) |
| 705 | { |
| 706 | int r = -EINVAL, x; |
| 707 | struct mapped_device *md; |
| 708 | struct hd_geometry geometry; |
| 709 | unsigned long indata[4]; |
| 710 | char *geostr = (char *) param + param->data_start; |
| 711 | |
| 712 | md = find_device(param); |
| 713 | if (!md) |
| 714 | return -ENXIO; |
| 715 | |
| 716 | if (geostr < (char *) (param + 1) || |
| 717 | invalid_str(geostr, (void *) param + param_size)) { |
| 718 | DMWARN("Invalid geometry supplied."); |
| 719 | goto out; |
| 720 | } |
| 721 | |
| 722 | x = sscanf(geostr, "%lu %lu %lu %lu", indata, |
| 723 | indata + 1, indata + 2, indata + 3); |
| 724 | |
| 725 | if (x != 4) { |
| 726 | DMWARN("Unable to interpret geometry settings."); |
| 727 | goto out; |
| 728 | } |
| 729 | |
| 730 | if (indata[0] > 65535 || indata[1] > 255 || |
| 731 | indata[2] > 255 || indata[3] > ULONG_MAX) { |
| 732 | DMWARN("Geometry exceeds range limits."); |
| 733 | goto out; |
| 734 | } |
| 735 | |
| 736 | geometry.cylinders = indata[0]; |
| 737 | geometry.heads = indata[1]; |
| 738 | geometry.sectors = indata[2]; |
| 739 | geometry.start = indata[3]; |
| 740 | |
| 741 | r = dm_set_geometry(md, &geometry); |
| 742 | if (!r) |
| 743 | r = __dev_status(md, param); |
| 744 | |
| 745 | param->data_size = 0; |
| 746 | |
| 747 | out: |
| 748 | dm_put(md); |
| 749 | return r; |
| 750 | } |
| 751 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | static int do_suspend(struct dm_ioctl *param) |
| 753 | { |
| 754 | int r = 0; |
Alasdair G Kergon | 6da487d | 2006-01-06 00:20:07 -0800 | [diff] [blame] | 755 | int do_lockfs = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | struct mapped_device *md; |
| 757 | |
| 758 | md = find_device(param); |
| 759 | if (!md) |
| 760 | return -ENXIO; |
| 761 | |
Alasdair G Kergon | 6da487d | 2006-01-06 00:20:07 -0800 | [diff] [blame] | 762 | if (param->flags & DM_SKIP_LOCKFS_FLAG) |
| 763 | do_lockfs = 0; |
| 764 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | if (!dm_suspended(md)) |
Alasdair G Kergon | 6da487d | 2006-01-06 00:20:07 -0800 | [diff] [blame] | 766 | r = dm_suspend(md, do_lockfs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | |
| 768 | if (!r) |
| 769 | r = __dev_status(md, param); |
| 770 | |
| 771 | dm_put(md); |
| 772 | return r; |
| 773 | } |
| 774 | |
| 775 | static int do_resume(struct dm_ioctl *param) |
| 776 | { |
| 777 | int r = 0; |
Alasdair G Kergon | 6da487d | 2006-01-06 00:20:07 -0800 | [diff] [blame] | 778 | int do_lockfs = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | struct hash_cell *hc; |
| 780 | struct mapped_device *md; |
| 781 | struct dm_table *new_map; |
| 782 | |
| 783 | down_write(&_hash_lock); |
| 784 | |
| 785 | hc = __find_device_hash_cell(param); |
| 786 | if (!hc) { |
| 787 | DMWARN("device doesn't appear to be in the dev hash table."); |
| 788 | up_write(&_hash_lock); |
| 789 | return -ENXIO; |
| 790 | } |
| 791 | |
| 792 | md = hc->md; |
| 793 | dm_get(md); |
| 794 | |
| 795 | new_map = hc->new_map; |
| 796 | hc->new_map = NULL; |
| 797 | param->flags &= ~DM_INACTIVE_PRESENT_FLAG; |
| 798 | |
| 799 | up_write(&_hash_lock); |
| 800 | |
| 801 | /* Do we need to load a new map ? */ |
| 802 | if (new_map) { |
| 803 | /* Suspend if it isn't already suspended */ |
Alasdair G Kergon | 6da487d | 2006-01-06 00:20:07 -0800 | [diff] [blame] | 804 | if (param->flags & DM_SKIP_LOCKFS_FLAG) |
| 805 | do_lockfs = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | if (!dm_suspended(md)) |
Alasdair G Kergon | 6da487d | 2006-01-06 00:20:07 -0800 | [diff] [blame] | 807 | dm_suspend(md, do_lockfs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | |
| 809 | r = dm_swap_table(md, new_map); |
| 810 | if (r) { |
| 811 | dm_put(md); |
| 812 | dm_table_put(new_map); |
| 813 | return r; |
| 814 | } |
| 815 | |
| 816 | if (dm_table_get_mode(new_map) & FMODE_WRITE) |
| 817 | set_disk_ro(dm_disk(md), 0); |
| 818 | else |
| 819 | set_disk_ro(dm_disk(md), 1); |
| 820 | |
| 821 | dm_table_put(new_map); |
| 822 | } |
| 823 | |
| 824 | if (dm_suspended(md)) |
| 825 | r = dm_resume(md); |
| 826 | |
| 827 | if (!r) |
| 828 | r = __dev_status(md, param); |
| 829 | |
| 830 | dm_put(md); |
| 831 | return r; |
| 832 | } |
| 833 | |
| 834 | /* |
| 835 | * Set or unset the suspension state of a device. |
| 836 | * If the device already is in the requested state we just return its status. |
| 837 | */ |
| 838 | static int dev_suspend(struct dm_ioctl *param, size_t param_size) |
| 839 | { |
| 840 | if (param->flags & DM_SUSPEND_FLAG) |
| 841 | return do_suspend(param); |
| 842 | |
| 843 | return do_resume(param); |
| 844 | } |
| 845 | |
| 846 | /* |
| 847 | * Copies device info back to user space, used by |
| 848 | * the create and info ioctls. |
| 849 | */ |
| 850 | static int dev_status(struct dm_ioctl *param, size_t param_size) |
| 851 | { |
| 852 | int r; |
| 853 | struct mapped_device *md; |
| 854 | |
| 855 | md = find_device(param); |
| 856 | if (!md) |
| 857 | return -ENXIO; |
| 858 | |
| 859 | r = __dev_status(md, param); |
| 860 | dm_put(md); |
| 861 | return r; |
| 862 | } |
| 863 | |
| 864 | /* |
| 865 | * Build up the status struct for each target |
| 866 | */ |
| 867 | static void retrieve_status(struct dm_table *table, |
| 868 | struct dm_ioctl *param, size_t param_size) |
| 869 | { |
| 870 | unsigned int i, num_targets; |
| 871 | struct dm_target_spec *spec; |
| 872 | char *outbuf, *outptr; |
| 873 | status_type_t type; |
| 874 | size_t remaining, len, used = 0; |
| 875 | |
| 876 | outptr = outbuf = get_result_buffer(param, param_size, &len); |
| 877 | |
| 878 | if (param->flags & DM_STATUS_TABLE_FLAG) |
| 879 | type = STATUSTYPE_TABLE; |
| 880 | else |
| 881 | type = STATUSTYPE_INFO; |
| 882 | |
| 883 | /* Get all the target info */ |
| 884 | num_targets = dm_table_get_num_targets(table); |
| 885 | for (i = 0; i < num_targets; i++) { |
| 886 | struct dm_target *ti = dm_table_get_target(table, i); |
| 887 | |
| 888 | remaining = len - (outptr - outbuf); |
| 889 | if (remaining <= sizeof(struct dm_target_spec)) { |
| 890 | param->flags |= DM_BUFFER_FULL_FLAG; |
| 891 | break; |
| 892 | } |
| 893 | |
| 894 | spec = (struct dm_target_spec *) outptr; |
| 895 | |
| 896 | spec->status = 0; |
| 897 | spec->sector_start = ti->begin; |
| 898 | spec->length = ti->len; |
| 899 | strncpy(spec->target_type, ti->type->name, |
| 900 | sizeof(spec->target_type)); |
| 901 | |
| 902 | outptr += sizeof(struct dm_target_spec); |
| 903 | remaining = len - (outptr - outbuf); |
| 904 | if (remaining <= 0) { |
| 905 | param->flags |= DM_BUFFER_FULL_FLAG; |
| 906 | break; |
| 907 | } |
| 908 | |
| 909 | /* Get the status/table string from the target driver */ |
| 910 | if (ti->type->status) { |
| 911 | if (ti->type->status(ti, type, outptr, remaining)) { |
| 912 | param->flags |= DM_BUFFER_FULL_FLAG; |
| 913 | break; |
| 914 | } |
| 915 | } else |
| 916 | outptr[0] = '\0'; |
| 917 | |
| 918 | outptr += strlen(outptr) + 1; |
| 919 | used = param->data_start + (outptr - outbuf); |
| 920 | |
| 921 | outptr = align_ptr(outptr); |
| 922 | spec->next = outptr - outbuf; |
| 923 | } |
| 924 | |
| 925 | if (used) |
| 926 | param->data_size = used; |
| 927 | |
| 928 | param->target_count = num_targets; |
| 929 | } |
| 930 | |
| 931 | /* |
| 932 | * Wait for a device to report an event |
| 933 | */ |
| 934 | static int dev_wait(struct dm_ioctl *param, size_t param_size) |
| 935 | { |
| 936 | int r; |
| 937 | struct mapped_device *md; |
| 938 | struct dm_table *table; |
| 939 | |
| 940 | md = find_device(param); |
| 941 | if (!md) |
| 942 | return -ENXIO; |
| 943 | |
| 944 | /* |
| 945 | * Wait for a notification event |
| 946 | */ |
| 947 | if (dm_wait_event(md, param->event_nr)) { |
| 948 | r = -ERESTARTSYS; |
| 949 | goto out; |
| 950 | } |
| 951 | |
| 952 | /* |
| 953 | * The userland program is going to want to know what |
| 954 | * changed to trigger the event, so we may as well tell |
| 955 | * him and save an ioctl. |
| 956 | */ |
| 957 | r = __dev_status(md, param); |
| 958 | if (r) |
| 959 | goto out; |
| 960 | |
| 961 | table = dm_get_table(md); |
| 962 | if (table) { |
| 963 | retrieve_status(table, param, param_size); |
| 964 | dm_table_put(table); |
| 965 | } |
| 966 | |
| 967 | out: |
| 968 | dm_put(md); |
| 969 | return r; |
| 970 | } |
| 971 | |
| 972 | static inline int get_mode(struct dm_ioctl *param) |
| 973 | { |
| 974 | int mode = FMODE_READ | FMODE_WRITE; |
| 975 | |
| 976 | if (param->flags & DM_READONLY_FLAG) |
| 977 | mode = FMODE_READ; |
| 978 | |
| 979 | return mode; |
| 980 | } |
| 981 | |
| 982 | static int next_target(struct dm_target_spec *last, uint32_t next, void *end, |
| 983 | struct dm_target_spec **spec, char **target_params) |
| 984 | { |
| 985 | *spec = (struct dm_target_spec *) ((unsigned char *) last + next); |
| 986 | *target_params = (char *) (*spec + 1); |
| 987 | |
| 988 | if (*spec < (last + 1)) |
| 989 | return -EINVAL; |
| 990 | |
| 991 | return invalid_str(*target_params, end); |
| 992 | } |
| 993 | |
| 994 | static int populate_table(struct dm_table *table, |
| 995 | struct dm_ioctl *param, size_t param_size) |
| 996 | { |
| 997 | int r; |
| 998 | unsigned int i = 0; |
| 999 | struct dm_target_spec *spec = (struct dm_target_spec *) param; |
| 1000 | uint32_t next = param->data_start; |
| 1001 | void *end = (void *) param + param_size; |
| 1002 | char *target_params; |
| 1003 | |
| 1004 | if (!param->target_count) { |
| 1005 | DMWARN("populate_table: no targets specified"); |
| 1006 | return -EINVAL; |
| 1007 | } |
| 1008 | |
| 1009 | for (i = 0; i < param->target_count; i++) { |
| 1010 | |
| 1011 | r = next_target(spec, next, end, &spec, &target_params); |
| 1012 | if (r) { |
| 1013 | DMWARN("unable to find target"); |
| 1014 | return r; |
| 1015 | } |
| 1016 | |
| 1017 | r = dm_table_add_target(table, spec->target_type, |
| 1018 | (sector_t) spec->sector_start, |
| 1019 | (sector_t) spec->length, |
| 1020 | target_params); |
| 1021 | if (r) { |
| 1022 | DMWARN("error adding target to table"); |
| 1023 | return r; |
| 1024 | } |
| 1025 | |
| 1026 | next = spec->next; |
| 1027 | } |
| 1028 | |
| 1029 | return dm_table_complete(table); |
| 1030 | } |
| 1031 | |
| 1032 | static int table_load(struct dm_ioctl *param, size_t param_size) |
| 1033 | { |
| 1034 | int r; |
| 1035 | struct hash_cell *hc; |
| 1036 | struct dm_table *t; |
Mike Anderson | 1134e5a | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 1037 | struct mapped_device *md; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | |
Mike Anderson | 1134e5a | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 1039 | md = find_device(param); |
| 1040 | if (!md) |
| 1041 | return -ENXIO; |
| 1042 | |
| 1043 | r = dm_table_create(&t, get_mode(param), param->target_count, md); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | if (r) |
Mike Anderson | 1134e5a | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 1045 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | |
| 1047 | r = populate_table(t, param, param_size); |
| 1048 | if (r) { |
| 1049 | dm_table_put(t); |
Mike Anderson | 1134e5a | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 1050 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | down_write(&_hash_lock); |
Mike Anderson | 1134e5a | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 1054 | hc = dm_get_mdptr(md); |
| 1055 | if (!hc || hc->md != md) { |
| 1056 | DMWARN("device has been removed from the dev hash table."); |
Kiyoshi Ueda | b6fcc80 | 2005-11-21 21:32:32 -0800 | [diff] [blame] | 1057 | dm_table_put(t); |
Mike Anderson | 1134e5a | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 1058 | up_write(&_hash_lock); |
| 1059 | r = -ENXIO; |
| 1060 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | if (hc->new_map) |
| 1064 | dm_table_put(hc->new_map); |
| 1065 | hc->new_map = t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | up_write(&_hash_lock); |
Mike Anderson | 1134e5a | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 1067 | |
| 1068 | param->flags |= DM_INACTIVE_PRESENT_FLAG; |
| 1069 | r = __dev_status(md, param); |
| 1070 | |
| 1071 | out: |
| 1072 | dm_put(md); |
| 1073 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | return r; |
| 1075 | } |
| 1076 | |
| 1077 | static int table_clear(struct dm_ioctl *param, size_t param_size) |
| 1078 | { |
| 1079 | int r; |
| 1080 | struct hash_cell *hc; |
| 1081 | |
| 1082 | down_write(&_hash_lock); |
| 1083 | |
| 1084 | hc = __find_device_hash_cell(param); |
| 1085 | if (!hc) { |
| 1086 | DMWARN("device doesn't appear to be in the dev hash table."); |
| 1087 | up_write(&_hash_lock); |
| 1088 | return -ENXIO; |
| 1089 | } |
| 1090 | |
| 1091 | if (hc->new_map) { |
| 1092 | dm_table_put(hc->new_map); |
| 1093 | hc->new_map = NULL; |
| 1094 | } |
| 1095 | |
| 1096 | param->flags &= ~DM_INACTIVE_PRESENT_FLAG; |
| 1097 | |
| 1098 | r = __dev_status(hc->md, param); |
| 1099 | up_write(&_hash_lock); |
| 1100 | return r; |
| 1101 | } |
| 1102 | |
| 1103 | /* |
| 1104 | * Retrieves a list of devices used by a particular dm device. |
| 1105 | */ |
| 1106 | static void retrieve_deps(struct dm_table *table, |
| 1107 | struct dm_ioctl *param, size_t param_size) |
| 1108 | { |
| 1109 | unsigned int count = 0; |
| 1110 | struct list_head *tmp; |
| 1111 | size_t len, needed; |
| 1112 | struct dm_dev *dd; |
| 1113 | struct dm_target_deps *deps; |
| 1114 | |
| 1115 | deps = get_result_buffer(param, param_size, &len); |
| 1116 | |
| 1117 | /* |
| 1118 | * Count the devices. |
| 1119 | */ |
| 1120 | list_for_each (tmp, dm_table_get_devices(table)) |
| 1121 | count++; |
| 1122 | |
| 1123 | /* |
| 1124 | * Check we have enough space. |
| 1125 | */ |
| 1126 | needed = sizeof(*deps) + (sizeof(*deps->dev) * count); |
| 1127 | if (len < needed) { |
| 1128 | param->flags |= DM_BUFFER_FULL_FLAG; |
| 1129 | return; |
| 1130 | } |
| 1131 | |
| 1132 | /* |
| 1133 | * Fill in the devices. |
| 1134 | */ |
| 1135 | deps->count = count; |
| 1136 | count = 0; |
| 1137 | list_for_each_entry (dd, dm_table_get_devices(table), list) |
| 1138 | deps->dev[count++] = huge_encode_dev(dd->bdev->bd_dev); |
| 1139 | |
| 1140 | param->data_size = param->data_start + needed; |
| 1141 | } |
| 1142 | |
| 1143 | static int table_deps(struct dm_ioctl *param, size_t param_size) |
| 1144 | { |
| 1145 | int r = 0; |
| 1146 | struct mapped_device *md; |
| 1147 | struct dm_table *table; |
| 1148 | |
| 1149 | md = find_device(param); |
| 1150 | if (!md) |
| 1151 | return -ENXIO; |
| 1152 | |
| 1153 | r = __dev_status(md, param); |
| 1154 | if (r) |
| 1155 | goto out; |
| 1156 | |
| 1157 | table = dm_get_table(md); |
| 1158 | if (table) { |
| 1159 | retrieve_deps(table, param, param_size); |
| 1160 | dm_table_put(table); |
| 1161 | } |
| 1162 | |
| 1163 | out: |
| 1164 | dm_put(md); |
| 1165 | return r; |
| 1166 | } |
| 1167 | |
| 1168 | /* |
| 1169 | * Return the status of a device as a text string for each |
| 1170 | * target. |
| 1171 | */ |
| 1172 | static int table_status(struct dm_ioctl *param, size_t param_size) |
| 1173 | { |
| 1174 | int r; |
| 1175 | struct mapped_device *md; |
| 1176 | struct dm_table *table; |
| 1177 | |
| 1178 | md = find_device(param); |
| 1179 | if (!md) |
| 1180 | return -ENXIO; |
| 1181 | |
| 1182 | r = __dev_status(md, param); |
| 1183 | if (r) |
| 1184 | goto out; |
| 1185 | |
| 1186 | table = dm_get_table(md); |
| 1187 | if (table) { |
| 1188 | retrieve_status(table, param, param_size); |
| 1189 | dm_table_put(table); |
| 1190 | } |
| 1191 | |
| 1192 | out: |
| 1193 | dm_put(md); |
| 1194 | return r; |
| 1195 | } |
| 1196 | |
| 1197 | /* |
| 1198 | * Pass a message to the target that's at the supplied device offset. |
| 1199 | */ |
| 1200 | static int target_message(struct dm_ioctl *param, size_t param_size) |
| 1201 | { |
| 1202 | int r, argc; |
| 1203 | char **argv; |
| 1204 | struct mapped_device *md; |
| 1205 | struct dm_table *table; |
| 1206 | struct dm_target *ti; |
| 1207 | struct dm_target_msg *tmsg = (void *) param + param->data_start; |
| 1208 | |
| 1209 | md = find_device(param); |
| 1210 | if (!md) |
| 1211 | return -ENXIO; |
| 1212 | |
| 1213 | r = __dev_status(md, param); |
| 1214 | if (r) |
| 1215 | goto out; |
| 1216 | |
| 1217 | if (tmsg < (struct dm_target_msg *) (param + 1) || |
| 1218 | invalid_str(tmsg->message, (void *) param + param_size)) { |
| 1219 | DMWARN("Invalid target message parameters."); |
| 1220 | r = -EINVAL; |
| 1221 | goto out; |
| 1222 | } |
| 1223 | |
| 1224 | r = dm_split_args(&argc, &argv, tmsg->message); |
| 1225 | if (r) { |
| 1226 | DMWARN("Failed to split target message parameters"); |
| 1227 | goto out; |
| 1228 | } |
| 1229 | |
| 1230 | table = dm_get_table(md); |
| 1231 | if (!table) |
| 1232 | goto out_argv; |
| 1233 | |
| 1234 | if (tmsg->sector >= dm_table_get_size(table)) { |
| 1235 | DMWARN("Target message sector outside device."); |
| 1236 | r = -EINVAL; |
| 1237 | goto out_table; |
| 1238 | } |
| 1239 | |
| 1240 | ti = dm_table_find_target(table, tmsg->sector); |
| 1241 | if (ti->type->message) |
| 1242 | r = ti->type->message(ti, argc, argv); |
| 1243 | else { |
| 1244 | DMWARN("Target type does not support messages"); |
| 1245 | r = -EINVAL; |
| 1246 | } |
| 1247 | |
| 1248 | out_table: |
| 1249 | dm_table_put(table); |
| 1250 | out_argv: |
| 1251 | kfree(argv); |
| 1252 | out: |
| 1253 | param->data_size = 0; |
| 1254 | dm_put(md); |
| 1255 | return r; |
| 1256 | } |
| 1257 | |
| 1258 | /*----------------------------------------------------------------- |
| 1259 | * Implementation of open/close/ioctl on the special char |
| 1260 | * device. |
| 1261 | *---------------------------------------------------------------*/ |
| 1262 | static ioctl_fn lookup_ioctl(unsigned int cmd) |
| 1263 | { |
| 1264 | static struct { |
| 1265 | int cmd; |
| 1266 | ioctl_fn fn; |
| 1267 | } _ioctls[] = { |
| 1268 | {DM_VERSION_CMD, NULL}, /* version is dealt with elsewhere */ |
| 1269 | {DM_REMOVE_ALL_CMD, remove_all}, |
| 1270 | {DM_LIST_DEVICES_CMD, list_devices}, |
| 1271 | |
| 1272 | {DM_DEV_CREATE_CMD, dev_create}, |
| 1273 | {DM_DEV_REMOVE_CMD, dev_remove}, |
| 1274 | {DM_DEV_RENAME_CMD, dev_rename}, |
| 1275 | {DM_DEV_SUSPEND_CMD, dev_suspend}, |
| 1276 | {DM_DEV_STATUS_CMD, dev_status}, |
| 1277 | {DM_DEV_WAIT_CMD, dev_wait}, |
| 1278 | |
| 1279 | {DM_TABLE_LOAD_CMD, table_load}, |
| 1280 | {DM_TABLE_CLEAR_CMD, table_clear}, |
| 1281 | {DM_TABLE_DEPS_CMD, table_deps}, |
| 1282 | {DM_TABLE_STATUS_CMD, table_status}, |
| 1283 | |
| 1284 | {DM_LIST_VERSIONS_CMD, list_versions}, |
| 1285 | |
Darrick J. Wong | 3ac51e7 | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 1286 | {DM_TARGET_MSG_CMD, target_message}, |
| 1287 | {DM_DEV_SET_GEOMETRY_CMD, dev_set_geometry} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | }; |
| 1289 | |
| 1290 | return (cmd >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[cmd].fn; |
| 1291 | } |
| 1292 | |
| 1293 | /* |
| 1294 | * As well as checking the version compatibility this always |
| 1295 | * copies the kernel interface version out. |
| 1296 | */ |
| 1297 | static int check_version(unsigned int cmd, struct dm_ioctl __user *user) |
| 1298 | { |
| 1299 | uint32_t version[3]; |
| 1300 | int r = 0; |
| 1301 | |
| 1302 | if (copy_from_user(version, user->version, sizeof(version))) |
| 1303 | return -EFAULT; |
| 1304 | |
| 1305 | if ((DM_VERSION_MAJOR != version[0]) || |
| 1306 | (DM_VERSION_MINOR < version[1])) { |
| 1307 | DMWARN("ioctl interface mismatch: " |
| 1308 | "kernel(%u.%u.%u), user(%u.%u.%u), cmd(%d)", |
| 1309 | DM_VERSION_MAJOR, DM_VERSION_MINOR, |
| 1310 | DM_VERSION_PATCHLEVEL, |
| 1311 | version[0], version[1], version[2], cmd); |
| 1312 | r = -EINVAL; |
| 1313 | } |
| 1314 | |
| 1315 | /* |
| 1316 | * Fill in the kernel version. |
| 1317 | */ |
| 1318 | version[0] = DM_VERSION_MAJOR; |
| 1319 | version[1] = DM_VERSION_MINOR; |
| 1320 | version[2] = DM_VERSION_PATCHLEVEL; |
| 1321 | if (copy_to_user(user->version, version, sizeof(version))) |
| 1322 | return -EFAULT; |
| 1323 | |
| 1324 | return r; |
| 1325 | } |
| 1326 | |
| 1327 | static void free_params(struct dm_ioctl *param) |
| 1328 | { |
| 1329 | vfree(param); |
| 1330 | } |
| 1331 | |
| 1332 | static int copy_params(struct dm_ioctl __user *user, struct dm_ioctl **param) |
| 1333 | { |
| 1334 | struct dm_ioctl tmp, *dmi; |
| 1335 | |
| 1336 | if (copy_from_user(&tmp, user, sizeof(tmp))) |
| 1337 | return -EFAULT; |
| 1338 | |
| 1339 | if (tmp.data_size < sizeof(tmp)) |
| 1340 | return -EINVAL; |
| 1341 | |
| 1342 | dmi = (struct dm_ioctl *) vmalloc(tmp.data_size); |
| 1343 | if (!dmi) |
| 1344 | return -ENOMEM; |
| 1345 | |
| 1346 | if (copy_from_user(dmi, user, tmp.data_size)) { |
| 1347 | vfree(dmi); |
| 1348 | return -EFAULT; |
| 1349 | } |
| 1350 | |
| 1351 | *param = dmi; |
| 1352 | return 0; |
| 1353 | } |
| 1354 | |
| 1355 | static int validate_params(uint cmd, struct dm_ioctl *param) |
| 1356 | { |
| 1357 | /* Always clear this flag */ |
| 1358 | param->flags &= ~DM_BUFFER_FULL_FLAG; |
| 1359 | |
| 1360 | /* Ignores parameters */ |
| 1361 | if (cmd == DM_REMOVE_ALL_CMD || |
| 1362 | cmd == DM_LIST_DEVICES_CMD || |
| 1363 | cmd == DM_LIST_VERSIONS_CMD) |
| 1364 | return 0; |
| 1365 | |
| 1366 | if ((cmd == DM_DEV_CREATE_CMD)) { |
| 1367 | if (!*param->name) { |
| 1368 | DMWARN("name not supplied when creating device"); |
| 1369 | return -EINVAL; |
| 1370 | } |
| 1371 | } else if ((*param->uuid && *param->name)) { |
| 1372 | DMWARN("only supply one of name or uuid, cmd(%u)", cmd); |
| 1373 | return -EINVAL; |
| 1374 | } |
| 1375 | |
| 1376 | /* Ensure strings are terminated */ |
| 1377 | param->name[DM_NAME_LEN - 1] = '\0'; |
| 1378 | param->uuid[DM_UUID_LEN - 1] = '\0'; |
| 1379 | |
| 1380 | return 0; |
| 1381 | } |
| 1382 | |
| 1383 | static int ctl_ioctl(struct inode *inode, struct file *file, |
| 1384 | uint command, ulong u) |
| 1385 | { |
| 1386 | int r = 0; |
| 1387 | unsigned int cmd; |
| 1388 | struct dm_ioctl *param; |
| 1389 | struct dm_ioctl __user *user = (struct dm_ioctl __user *) u; |
| 1390 | ioctl_fn fn = NULL; |
| 1391 | size_t param_size; |
| 1392 | |
| 1393 | /* only root can play with this */ |
| 1394 | if (!capable(CAP_SYS_ADMIN)) |
| 1395 | return -EACCES; |
| 1396 | |
| 1397 | if (_IOC_TYPE(command) != DM_IOCTL) |
| 1398 | return -ENOTTY; |
| 1399 | |
| 1400 | cmd = _IOC_NR(command); |
| 1401 | |
| 1402 | /* |
| 1403 | * Check the interface version passed in. This also |
| 1404 | * writes out the kernel's interface version. |
| 1405 | */ |
| 1406 | r = check_version(cmd, user); |
| 1407 | if (r) |
| 1408 | return r; |
| 1409 | |
| 1410 | /* |
| 1411 | * Nothing more to do for the version command. |
| 1412 | */ |
| 1413 | if (cmd == DM_VERSION_CMD) |
| 1414 | return 0; |
| 1415 | |
| 1416 | fn = lookup_ioctl(cmd); |
| 1417 | if (!fn) { |
| 1418 | DMWARN("dm_ctl_ioctl: unknown command 0x%x", command); |
| 1419 | return -ENOTTY; |
| 1420 | } |
| 1421 | |
| 1422 | /* |
| 1423 | * Trying to avoid low memory issues when a device is |
| 1424 | * suspended. |
| 1425 | */ |
| 1426 | current->flags |= PF_MEMALLOC; |
| 1427 | |
| 1428 | /* |
| 1429 | * Copy the parameters into kernel space. |
| 1430 | */ |
| 1431 | r = copy_params(user, ¶m); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | |
Alasdair G Kergon | dab6a42 | 2006-02-01 03:04:52 -0800 | [diff] [blame] | 1433 | current->flags &= ~PF_MEMALLOC; |
| 1434 | |
| 1435 | if (r) |
| 1436 | return r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | |
| 1438 | r = validate_params(cmd, param); |
| 1439 | if (r) |
| 1440 | goto out; |
| 1441 | |
| 1442 | param_size = param->data_size; |
| 1443 | param->data_size = sizeof(*param); |
| 1444 | r = fn(param, param_size); |
| 1445 | |
| 1446 | /* |
| 1447 | * Copy the results back to userland. |
| 1448 | */ |
| 1449 | if (!r && copy_to_user(user, param, param->data_size)) |
| 1450 | r = -EFAULT; |
| 1451 | |
| 1452 | out: |
| 1453 | free_params(param); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | return r; |
| 1455 | } |
| 1456 | |
| 1457 | static struct file_operations _ctl_fops = { |
| 1458 | .ioctl = ctl_ioctl, |
| 1459 | .owner = THIS_MODULE, |
| 1460 | }; |
| 1461 | |
| 1462 | static struct miscdevice _dm_misc = { |
| 1463 | .minor = MISC_DYNAMIC_MINOR, |
| 1464 | .name = DM_NAME, |
| 1465 | .devfs_name = "mapper/control", |
| 1466 | .fops = &_ctl_fops |
| 1467 | }; |
| 1468 | |
| 1469 | /* |
| 1470 | * Create misc character device and link to DM_DIR/control. |
| 1471 | */ |
| 1472 | int __init dm_interface_init(void) |
| 1473 | { |
| 1474 | int r; |
| 1475 | |
| 1476 | r = dm_hash_init(); |
| 1477 | if (r) |
| 1478 | return r; |
| 1479 | |
| 1480 | r = misc_register(&_dm_misc); |
| 1481 | if (r) { |
| 1482 | DMERR("misc_register failed for control device"); |
| 1483 | dm_hash_exit(); |
| 1484 | return r; |
| 1485 | } |
| 1486 | |
| 1487 | DMINFO("%d.%d.%d%s initialised: %s", DM_VERSION_MAJOR, |
| 1488 | DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL, DM_VERSION_EXTRA, |
| 1489 | DM_DRIVER_EMAIL); |
| 1490 | return 0; |
| 1491 | } |
| 1492 | |
| 1493 | void dm_interface_exit(void) |
| 1494 | { |
| 1495 | if (misc_deregister(&_dm_misc) < 0) |
| 1496 | DMERR("misc_deregister failed for control device"); |
| 1497 | |
| 1498 | dm_hash_exit(); |
| 1499 | } |