1 /* $NetBSD: device-mapper.c,v 1.22 2010/03/26 15:46:04 jakllsch Exp $ */ 2 3 /* 4 * Copyright (c) 2010-2011 Alex Hornung <alex@alexhornung.com> 5 * Copyright (c) 2010 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Adam Hamsik. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * I want to say thank you to all people who helped me with this project. 35 */ 36 37 #include <sys/ctype.h> 38 #include <sys/conf.h> 39 #include <sys/device.h> 40 #include <sys/disk.h> 41 #include <sys/disklabel.h> 42 #include <sys/dtype.h> 43 #include <sys/malloc.h> 44 #include <sys/module.h> 45 #include <sys/sysctl.h> 46 #include <dev/disk/dm/dm.h> 47 #include <dev/disk/dm/netbsd-dm.h> 48 49 static d_ioctl_t dmioctl; 50 static d_open_t dmopen; 51 static d_close_t dmclose; 52 static d_psize_t dmsize; 53 static d_strategy_t dmstrategy; 54 static d_dump_t dmdump; 55 56 /* New module handle and destroy routines */ 57 static int dm_modcmd(module_t mod, int cmd, void *unused); 58 static int dmdestroy(void); 59 60 static void dm_doinit(void); 61 62 static int dm_cmd_to_fun(prop_dictionary_t); 63 static int disk_ioctl_switch(cdev_t, u_long, void *); 64 static int dm_ioctl_switch(u_long); 65 #if 0 66 static void dmminphys(struct buf *); 67 #endif 68 69 static struct dev_ops dmctl_ops = { 70 { "dm", 0, D_MPSAFE }, 71 .d_open = dmopen, 72 .d_close = dmclose, 73 .d_ioctl = dmioctl, 74 }; 75 76 struct dev_ops dm_ops = { 77 { "dm", 0, D_DISK | D_MPSAFE }, 78 .d_open = dmopen, 79 .d_close = dmclose, 80 .d_read = physread, 81 .d_write = physwrite, 82 .d_ioctl = dmioctl, 83 .d_strategy = dmstrategy, 84 .d_psize = dmsize, 85 .d_dump = dmdump, 86 }; 87 88 MALLOC_DEFINE(M_DM, "dm", "Device Mapper allocations"); 89 90 int dm_debug_level = 0; 91 92 extern uint64_t dm_dev_counter; 93 94 static cdev_t dmcdev; 95 96 static moduledata_t dm_mod = { 97 "dm", 98 dm_modcmd, 99 NULL 100 }; 101 DECLARE_MODULE(dm, dm_mod, SI_SUB_RAID, SI_ORDER_ANY); 102 MODULE_VERSION(dm, 1); 103 104 /* 105 * This structure is used to translate command sent to kernel driver in 106 * <key>command</key> 107 * <value></value> 108 * to function 109 */ 110 /* 111 * This array is used to translate cmd to function pointer. 112 * 113 * Interface between libdevmapper and lvm2tools uses different 114 * names for one IOCTL call because libdevmapper do another thing 115 * then. When I run "info" or "mknodes" libdevmapper will send same 116 * ioctl to kernel but will do another things in userspace. 117 */ 118 static struct cmd_function { 119 const char *cmd; 120 int (*fn)(prop_dictionary_t); 121 } cmd_fn[] = { 122 {.cmd = "version", .fn = NULL}, 123 {.cmd = "targets", .fn = dm_list_versions_ioctl}, 124 {.cmd = "create", .fn = dm_dev_create_ioctl}, 125 {.cmd = "info", .fn = dm_dev_status_ioctl}, 126 {.cmd = "mknodes", .fn = dm_dev_status_ioctl}, 127 {.cmd = "names", .fn = dm_dev_list_ioctl}, 128 {.cmd = "suspend", .fn = dm_dev_suspend_ioctl}, 129 {.cmd = "remove", .fn = dm_dev_remove_ioctl}, 130 {.cmd = "remove_all", .fn = dm_dev_remove_all_ioctl}, 131 {.cmd = "rename", .fn = dm_dev_rename_ioctl}, 132 {.cmd = "resume", .fn = dm_dev_resume_ioctl}, 133 {.cmd = "clear", .fn = dm_table_clear_ioctl}, 134 {.cmd = "deps", .fn = dm_table_deps_ioctl}, 135 {.cmd = "reload", .fn = dm_table_load_ioctl}, 136 {.cmd = "status", .fn = dm_table_status_ioctl}, 137 {.cmd = "table", .fn = dm_table_status_ioctl}, 138 {.cmd = "message", .fn = dm_message_ioctl}, 139 }; 140 141 /* 142 * New module handle routine 143 */ 144 static int 145 dm_modcmd(module_t mod, int cmd, void *unused) 146 { 147 int error; 148 149 error = 0; 150 151 switch (cmd) { 152 case MOD_LOAD: 153 dm_doinit(); 154 kprintf("Device Mapper version %d.%d.%d loaded\n", 155 DM_VERSION_MAJOR, DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL); 156 break; 157 158 case MOD_UNLOAD: 159 /* 160 * Disable unloading of dm module if there are any devices 161 * defined in driver. This is probably too strong we need 162 * to disable auto-unload only if there is mounted dm device 163 * present. 164 */ 165 if (dm_dev_counter > 0) 166 return EBUSY; 167 /* race window here */ 168 169 error = dmdestroy(); 170 if (error) 171 break; 172 kprintf("Device Mapper unloaded\n"); 173 break; 174 175 default: 176 break; 177 } 178 179 return error; 180 } 181 182 static void 183 dm_doinit(void) 184 { 185 dm_target_init(); 186 dm_dev_init(); 187 dm_pdev_init(); 188 dmcdev = make_dev(&dmctl_ops, 0, UID_ROOT, GID_OPERATOR, 0640, "mapper/control"); 189 } 190 191 /* 192 * Destroy routine 193 */ 194 static int 195 dmdestroy(void) 196 { 197 destroy_dev(dmcdev); 198 199 dm_dev_uninit(); 200 dm_pdev_uninit(); 201 dm_target_uninit(); 202 203 return 0; 204 } 205 206 static int 207 dmopen(struct dev_open_args *ap) 208 { 209 cdev_t dev = ap->a_head.a_dev; 210 dm_dev_t *dmv; 211 212 /* Shortcut for the control device */ 213 if (minor(dev) == 0) 214 return 0; 215 216 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL) 217 return ENXIO; 218 219 dmv->is_open = 1; 220 dm_dev_unbusy(dmv); 221 222 dmdebug("dm open routine called %" PRIu32 "\n", 223 minor(ap->a_head.a_dev)); 224 return 0; 225 } 226 227 static int 228 dmclose(struct dev_close_args *ap) 229 { 230 cdev_t dev = ap->a_head.a_dev; 231 dm_dev_t *dmv; 232 233 /* Shortcut for the control device */ 234 if (minor(dev) == 0) 235 return 0; 236 237 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL) 238 return ENXIO; 239 240 dmv->is_open = 0; 241 dm_dev_unbusy(dmv); 242 243 dmdebug("dm close routine called %" PRIu32 "\n", 244 minor(ap->a_head.a_dev)); 245 return 0; 246 } 247 248 249 static int 250 dmioctl(struct dev_ioctl_args *ap) 251 { 252 cdev_t dev = ap->a_head.a_dev; 253 u_long cmd = ap->a_cmd; 254 void *data = ap->a_data; 255 struct plistref *pref; 256 257 int r, err; 258 prop_dictionary_t dm_dict_in; 259 260 err = r = 0; 261 262 dmdebug("dmioctl called\n"); 263 KKASSERT(data != NULL); 264 265 if ((r = disk_ioctl_switch(dev, cmd, data)) != ENOTTY) 266 return r; /* Handled disk ioctl */ 267 268 if ((r = dm_ioctl_switch(cmd)) != 0) 269 return r; /* Not NETBSD_DM_IOCTL */ 270 271 pref = (struct plistref *)data; /* data is for libprop */ 272 if ((r = prop_dictionary_copyin_ioctl(pref, cmd, &dm_dict_in)) != 0) 273 return r; 274 275 if ((r = dm_check_version(dm_dict_in)) == 0) 276 err = dm_cmd_to_fun(dm_dict_in); 277 278 r = prop_dictionary_copyout_ioctl(pref, cmd, dm_dict_in); 279 prop_object_release(dm_dict_in); 280 281 /* Return the dm ioctl error if any. */ 282 if (err) 283 return err; 284 return r; 285 } 286 287 /* 288 * Translate command sent from libdevmapper to func. 289 */ 290 static int 291 dm_cmd_to_fun(prop_dictionary_t dm_dict) 292 { 293 int i, size; 294 prop_string_t command; 295 struct cmd_function *p = NULL; 296 297 size = sizeof(cmd_fn) / sizeof(cmd_fn[0]); 298 299 if ((command = prop_dictionary_get(dm_dict, DM_IOCTL_COMMAND)) == NULL) 300 return EINVAL; 301 302 for (i = 0; i < size; i++) { 303 p = &cmd_fn[i]; 304 if (prop_string_equals_cstring(command, p->cmd)) 305 break; 306 } 307 308 KKASSERT(p); 309 if (i == size) { 310 dmdebug("Unknown ioctl\n"); 311 return EINVAL; 312 } 313 314 dmdebug("ioctl %s called %p\n", p->cmd, p->fn); 315 if (p->fn == NULL) 316 return 0; /* No handler required */ 317 318 return p->fn(dm_dict); 319 } 320 321 /* 322 * Call apropriate ioctl handler function. 323 */ 324 static int 325 dm_ioctl_switch(u_long cmd) 326 { 327 328 switch(cmd) { 329 case NETBSD_DM_IOCTL: 330 dmdebug("dm NETBSD_DM_IOCTL called\n"); 331 break; 332 default: 333 dmdebug("dm unknown ioctl called\n"); 334 return ENOTTY; 335 break; /* NOT REACHED */ 336 } 337 338 return 0; 339 } 340 341 /* 342 * Check for disk specific ioctls. 343 */ 344 static int 345 disk_ioctl_switch(cdev_t dev, u_long cmd, void *data) 346 { 347 dm_dev_t *dmv; 348 349 /* disk ioctls make sense only on block devices */ 350 if (minor(dev) == 0) 351 return ENOTTY; 352 353 switch(cmd) { 354 case DIOCGPART: 355 if ((dmv = dev->si_drv1) == NULL) 356 return ENODEV; 357 if (dmv->diskp->d_info.d_media_blksize == 0) { 358 return ENOTSUP; 359 } else { 360 u_int64_t size; 361 struct partinfo *dpart = data; 362 bzero(dpart, sizeof(*dpart)); 363 364 size = dm_table_size(&dmv->table_head); 365 dpart->media_offset = 0; 366 dpart->media_size = size * DEV_BSIZE; 367 dpart->media_blocks = size; 368 dpart->media_blksize = DEV_BSIZE; 369 dpart->fstype = FS_BSDFFS; 370 } 371 break; 372 373 default: 374 dmdebug("unknown disk_ioctl called\n"); 375 return ENOTTY; 376 break; /* NOT REACHED */ 377 } 378 379 return 0; 380 } 381 382 /* 383 * Do all IO operations on dm logical devices. 384 */ 385 static int 386 dmstrategy(struct dev_strategy_args *ap) 387 { 388 cdev_t dev = ap->a_head.a_dev; 389 struct bio *bio = ap->a_bio; 390 struct buf *bp = bio->bio_buf; 391 int bypass; 392 393 dm_dev_t *dmv; 394 dm_table_t *tbl; 395 dm_table_entry_t *table_en; 396 struct buf *nestbuf; 397 398 uint64_t buf_start, buf_len, issued_len; 399 uint64_t table_start, table_end; 400 uint64_t start, end; 401 402 buf_start = bio->bio_offset; 403 buf_len = bp->b_bcount; 404 405 tbl = NULL; 406 407 table_end = 0; 408 issued_len = 0; 409 410 dmv = dev->si_drv1; 411 412 switch(bp->b_cmd) { 413 case BUF_CMD_READ: 414 case BUF_CMD_WRITE: 415 case BUF_CMD_FREEBLKS: 416 bypass = 0; 417 break; 418 case BUF_CMD_FLUSH: 419 bypass = 1; 420 KKASSERT(buf_len == 0); 421 break; 422 default: 423 bp->b_error = EIO; 424 bp->b_resid = bp->b_bcount; 425 biodone(bio); 426 return 0; 427 } 428 429 if (bypass == 0 && 430 bounds_check_with_mediasize(bio, DEV_BSIZE, 431 dm_table_size(&dmv->table_head)) <= 0) { 432 bp->b_resid = bp->b_bcount; 433 biodone(bio); 434 return 0; 435 } 436 437 /* Select active table */ 438 tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE); 439 440 nestiobuf_init(bio); 441 devstat_start_transaction(&dmv->stats); 442 443 /* 444 * Find out what tables I want to select. 445 */ 446 TAILQ_FOREACH(table_en, tbl, next) { 447 /* 448 * I need need number of bytes not blocks. 449 */ 450 table_start = table_en->start * DEV_BSIZE; 451 table_end = table_start + table_en->length * DEV_BSIZE; 452 453 /* 454 * Calculate the start and end 455 */ 456 start = MAX(table_start, buf_start); 457 end = MIN(table_end, buf_start + buf_len); 458 459 if (dm_debug_level) { 460 kprintf("----------------------------------------\n"); 461 kprintf("table_start %010" PRIu64", table_end %010" 462 PRIu64 "\n", table_start, table_end); 463 kprintf("buf_start %010" PRIu64", buf_len %010" 464 PRIu64"\n", buf_start, buf_len); 465 kprintf("start-buf_start %010"PRIu64", end %010" 466 PRIu64"\n", start - buf_start, end); 467 kprintf("start %010" PRIu64", end %010" 468 PRIu64"\n", start, end); 469 } 470 471 if (bypass) { 472 nestbuf = getpbuf(NULL); 473 nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS; 474 475 nestiobuf_add(bio, nestbuf, 0, 0, &dmv->stats); 476 nestbuf->b_bio1.bio_offset = 0; 477 table_en->target->strategy(table_en, nestbuf); 478 } else if (start < end) { 479 nestbuf = getpbuf(NULL); 480 nestbuf->b_flags |= bio->bio_buf->b_flags & B_HASBOGUS; 481 482 nestiobuf_add(bio, nestbuf, 483 start - buf_start, (end - start), 484 &dmv->stats); 485 issued_len += end - start; 486 487 nestbuf->b_bio1.bio_offset = (start - table_start); 488 table_en->target->strategy(table_en, nestbuf); 489 } 490 } 491 492 if (issued_len < buf_len) 493 nestiobuf_error(bio, EINVAL); 494 nestiobuf_start(bio); 495 dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE); 496 497 return 0; 498 } 499 500 static int 501 dmdump(struct dev_dump_args *ap) 502 { 503 cdev_t dev = ap->a_head.a_dev; 504 dm_dev_t *dmv; 505 dm_table_t *tbl; 506 dm_table_entry_t *table_en; 507 uint64_t buf_start, buf_len, issued_len; 508 uint64_t table_start, table_end; 509 uint64_t start, end, data_offset; 510 off_t offset; 511 size_t length; 512 int error = 0; 513 514 buf_start = ap->a_offset; 515 buf_len = ap->a_length; 516 517 tbl = NULL; 518 519 table_end = 0; 520 issued_len = 0; 521 522 dmv = dev->si_drv1; 523 524 /* Select active table */ 525 tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE); 526 527 /* 528 * Find out what tables I want to select. 529 */ 530 TAILQ_FOREACH(table_en, tbl, next) { 531 /* 532 * I need need number of bytes not blocks. 533 */ 534 table_start = table_en->start * DEV_BSIZE; 535 table_end = table_start + table_en->length * DEV_BSIZE; 536 537 /* 538 * Calculate the start and end 539 */ 540 start = MAX(table_start, buf_start); 541 end = MIN(table_end, buf_start + buf_len); 542 543 if (ap->a_length == 0) { 544 if (table_en->target->dump == NULL) { 545 error = ENXIO; 546 goto out; 547 } 548 549 table_en->target->dump(table_en, NULL, 0, 0); 550 } else if (start < end) { 551 data_offset = start - buf_start; 552 offset = start - table_start; 553 length = end - start; 554 555 if (table_en->target->dump == NULL) { 556 error = ENXIO; 557 goto out; 558 } 559 560 table_en->target->dump(table_en, 561 (char *)ap->a_virtual + data_offset, 562 length, offset); 563 564 issued_len += end - start; 565 } 566 } 567 568 if (issued_len < buf_len) 569 error = EINVAL; 570 571 out: 572 dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE); 573 574 return error; 575 } 576 577 static int 578 dmsize(struct dev_psize_args *ap) 579 { 580 cdev_t dev = ap->a_head.a_dev; 581 dm_dev_t *dmv; 582 uint64_t size; 583 584 size = 0; 585 586 if ((dmv = dev->si_drv1) == NULL) 587 return ENXIO; 588 589 size = dm_table_size(&dmv->table_head); 590 ap->a_result = (int64_t)size; 591 592 return 0; 593 } 594 595 #if 0 596 static void 597 dmminphys(struct buf *bp) 598 { 599 600 bp->b_bcount = MIN(bp->b_bcount, MAXPHYS); 601 } 602 #endif 603 604 void 605 dmsetdiskinfo(struct disk *disk, dm_table_head_t *head) 606 { 607 struct disk_info info; 608 uint64_t dmp_size; 609 610 dmp_size = dm_table_size(head); 611 612 bzero(&info, sizeof(struct disk_info)); 613 info.d_media_blksize = DEV_BSIZE; 614 info.d_media_blocks = dmp_size; 615 #if 0 616 /* this is set by disk_setdiskinfo */ 617 info.d_media_size = dmp_size * DEV_BSIZE; 618 #endif 619 info.d_dsflags = DSO_MBRQUIET | DSO_DEVICEMAPPER | DSO_RAWPSIZE; 620 621 info.d_secpertrack = 32; 622 info.d_nheads = 64; 623 info.d_secpercyl = info.d_secpertrack * info.d_nheads; 624 info.d_ncylinders = dmp_size / info.d_secpercyl; 625 626 disk_setdiskinfo(disk, &info); 627 } 628 629 /* 630 * Transform char s to uint64_t offset number. 631 */ 632 uint64_t 633 atoi64(const char *s) 634 { 635 uint64_t n; 636 n = 0; 637 638 while (*s != '\0') { 639 if (!isdigit(*s)) 640 break; 641 642 n = (10 * n) + (*s - '0'); 643 s++; 644 } 645 646 return n; 647 } 648 649 char * 650 dm_alloc_string(int len) 651 { 652 if (len <= 0) 653 len = DM_MAX_PARAMS_SIZE; 654 return kmalloc(len, M_DM, M_WAITOK | M_ZERO); 655 } 656 657 void 658 dm_builtin_init(void *arg) 659 { 660 modeventhand_t evh = (modeventhand_t)arg; 661 662 KKASSERT(evh != NULL); 663 evh(NULL, MOD_LOAD, NULL); 664 } 665 666 void 667 dm_builtin_uninit(void *arg) 668 { 669 modeventhand_t evh = (modeventhand_t)arg; 670 671 KKASSERT(evh != NULL); 672 evh(NULL, MOD_UNLOAD, NULL); 673 } 674 675 TUNABLE_INT("debug.dm_debug", &dm_debug_level); 676 SYSCTL_INT(_debug, OID_AUTO, dm_debug, CTLFLAG_RW, &dm_debug_level, 677 0, "Enable device mapper debugging"); 678