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