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