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