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 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/ioccom.h> 46 #include <sys/malloc.h> 47 #include <sys/module.h> 48 #include <sys/sysctl.h> 49 50 #include "netbsd-dm.h" 51 #include "dm.h" 52 53 static d_ioctl_t dmioctl; 54 static d_open_t dmopen; 55 static d_close_t dmclose; 56 static d_psize_t dmsize; 57 static d_strategy_t dmstrategy; 58 59 /* attach and detach routines */ 60 void dmattach(int); 61 static int dm_modcmd(module_t mod, int cmd, void *unused); 62 static int dmdestroy(void); 63 64 static void dm_doinit(void); 65 66 static int dm_cmd_to_fun(prop_dictionary_t); 67 static int disk_ioctl_switch(cdev_t, u_long, void *); 68 static int dm_ioctl_switch(u_long); 69 #if 0 70 static void dmminphys(struct buf *); 71 #endif 72 73 /* ***Variable-definitions*** */ 74 struct dev_ops dm_ops = { 75 { "dm", 0, D_DISK | 76 D_MPSAFE_READ | D_MPSAFE_WRITE | D_MPSAFE_IOCTL }, 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_DISK */ 85 }; 86 87 MALLOC_DEFINE(M_DM, "dm", "Device Mapper allocations"); 88 89 int dm_debug_level = 0; 90 91 extern uint64_t dm_dev_counter; 92 93 static cdev_t dmcdev; 94 95 static moduledata_t dm_mod = { 96 "dm", 97 dm_modcmd, 98 NULL 99 }; 100 DECLARE_MODULE(dm, dm_mod, SI_SUB_RAID, SI_ORDER_ANY); 101 102 /* 103 * This array is used to translate cmd to function pointer. 104 * 105 * Interface between libdevmapper and lvm2tools uses different 106 * names for one IOCTL call because libdevmapper do another thing 107 * then. When I run "info" or "mknodes" libdevmapper will send same 108 * ioctl to kernel but will do another things in userspace. 109 * 110 */ 111 static struct cmd_function cmd_fn[] = { 112 { .cmd = "version", .fn = dm_get_version_ioctl}, 113 { .cmd = "targets", .fn = dm_list_versions_ioctl}, 114 { .cmd = "create", .fn = dm_dev_create_ioctl}, 115 { .cmd = "info", .fn = dm_dev_status_ioctl}, 116 { .cmd = "mknodes", .fn = dm_dev_status_ioctl}, 117 { .cmd = "names", .fn = dm_dev_list_ioctl}, 118 { .cmd = "suspend", .fn = dm_dev_suspend_ioctl}, 119 { .cmd = "remove", .fn = dm_dev_remove_ioctl}, 120 { .cmd = "rename", .fn = dm_dev_rename_ioctl}, 121 { .cmd = "resume", .fn = dm_dev_resume_ioctl}, 122 { .cmd = "clear", .fn = dm_table_clear_ioctl}, 123 { .cmd = "deps", .fn = dm_table_deps_ioctl}, 124 { .cmd = "reload", .fn = dm_table_load_ioctl}, 125 { .cmd = "status", .fn = dm_table_status_ioctl}, 126 { .cmd = "table", .fn = dm_table_status_ioctl}, 127 {NULL, NULL} 128 }; 129 130 /* New module handle routine */ 131 static int 132 dm_modcmd(module_t mod, int cmd, void *unused) 133 { 134 int error, bmajor, cmajor; 135 136 error = 0; 137 bmajor = -1; 138 cmajor = -1; 139 140 switch (cmd) { 141 case MOD_LOAD: 142 dm_doinit(); 143 kprintf("Device Mapper version %d.%d.%d loaded\n", 144 DM_VERSION_MAJOR, DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL); 145 break; 146 147 case MOD_UNLOAD: 148 /* 149 * Disable unloading of dm module if there are any devices 150 * defined in driver. This is probably too strong we need 151 * to disable auto-unload only if there is mounted dm device 152 * present. 153 */ 154 if (dm_dev_counter > 0) 155 return EBUSY; 156 157 error = dmdestroy(); 158 if (error) 159 break; 160 kprintf("Device Mapper unloaded\n"); 161 break; 162 163 default: 164 break; 165 } 166 167 return error; 168 } 169 170 /* 171 * dm_detach: 172 * 173 * Autoconfiguration detach function for pseudo-device glue. 174 * This routine is called by dm_ioctl::dm_dev_remove_ioctl and by autoconf to 175 * remove devices created in device-mapper. 176 */ 177 int 178 dm_detach(dm_dev_t *dmv) 179 { 180 disable_dev(dmv); 181 182 /* Destroy active table first. */ 183 dm_table_destroy(&dmv->table_head, DM_TABLE_ACTIVE); 184 185 /* Destroy inactive table if exits, too. */ 186 dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE); 187 188 dm_table_head_destroy(&dmv->table_head); 189 190 destroy_dev(dmv->devt); 191 192 /* Destroy device */ 193 (void)dm_dev_free(dmv); 194 195 /* Decrement device counter After removing device */ 196 --dm_dev_counter; /* XXX: was atomic 64 */ 197 198 return 0; 199 } 200 201 static void 202 dm_doinit(void) 203 { 204 dm_target_init(); 205 dm_dev_init(); 206 dm_pdev_init(); 207 dmcdev = make_dev(&dm_ops, 0, UID_ROOT, GID_OPERATOR, 0640, "mapper/control"); 208 } 209 210 /* Destroy routine */ 211 static int 212 dmdestroy(void) 213 { 214 destroy_dev(dmcdev); 215 216 dm_dev_destroy(); 217 dm_pdev_destroy(); 218 dm_target_destroy(); 219 220 return 0; 221 } 222 223 static int 224 dmopen(struct dev_open_args *ap) 225 { 226 227 aprint_debug("dm open routine called %" PRIu32 "\n", 228 minor(ap->a_head.a_dev)); 229 return 0; 230 } 231 232 static int 233 dmclose(struct dev_close_args *ap) 234 { 235 236 aprint_debug("dm close routine called %" PRIu32 "\n", 237 minor(ap->a_head.a_dev)); 238 return 0; 239 } 240 241 242 static int 243 dmioctl(struct dev_ioctl_args *ap) 244 { 245 cdev_t dev = ap->a_head.a_dev; 246 u_long cmd = ap->a_cmd; 247 void *data = ap->a_data; 248 249 int r; 250 prop_dictionary_t dm_dict_in; 251 252 r = 0; 253 254 aprint_debug("dmioctl called\n"); 255 256 KKASSERT(data != NULL); 257 258 if (( r = disk_ioctl_switch(dev, cmd, data)) == ENOTTY) { 259 struct plistref *pref = (struct plistref *) data; 260 261 /* Check if we were called with NETBSD_DM_IOCTL ioctl 262 otherwise quit. */ 263 if ((r = dm_ioctl_switch(cmd)) != 0) 264 return r; 265 266 if((r = prop_dictionary_copyin_ioctl(pref, cmd, &dm_dict_in)) != 0) 267 return r; 268 269 if ((r = dm_check_version(dm_dict_in)) != 0) 270 goto cleanup_exit; 271 272 /* run ioctl routine */ 273 if ((r = dm_cmd_to_fun(dm_dict_in)) != 0) 274 goto cleanup_exit; 275 276 cleanup_exit: 277 r = prop_dictionary_copyout_ioctl(pref, cmd, dm_dict_in); 278 prop_object_release(dm_dict_in); 279 } 280 281 return 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 int i, r; 290 prop_string_t command; 291 292 r = 0; 293 294 if ((command = prop_dictionary_get(dm_dict, DM_IOCTL_COMMAND)) == NULL) 295 return EINVAL; 296 297 for(i = 0; cmd_fn[i].cmd != NULL; i++) 298 if (prop_string_equals_cstring(command, cmd_fn[i].cmd)) 299 break; 300 301 if (cmd_fn[i].cmd == NULL) 302 return EINVAL; 303 304 aprint_debug("ioctl %s called\n", cmd_fn[i].cmd); 305 r = cmd_fn[i].fn(dm_dict); 306 307 return r; 308 } 309 310 /* Call apropriate ioctl handler function. */ 311 static int 312 dm_ioctl_switch(u_long cmd) 313 { 314 315 switch(cmd) { 316 317 case NETBSD_DM_IOCTL: 318 aprint_debug("dm NetBSD_DM_IOCTL called\n"); 319 break; 320 default: 321 aprint_debug("dm unknown ioctl called\n"); 322 return ENOTTY; 323 break; /* NOT REACHED */ 324 } 325 326 return 0; 327 } 328 329 /* 330 * Check for disk specific ioctls. 331 */ 332 333 static int 334 disk_ioctl_switch(cdev_t dev, u_long cmd, void *data) 335 { 336 dm_dev_t *dmv; 337 338 /* disk ioctls make sense only on block devices */ 339 if (minor(dev) == 0) 340 return ENOTTY; 341 342 switch(cmd) { 343 case DIOCGPART: 344 { 345 struct partinfo *dpart; 346 u_int64_t size; 347 dpart = (void *)data; 348 bzero(dpart, sizeof(*dpart)); 349 350 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL) 351 return ENODEV; 352 if (dmv->diskp->d_info.d_media_blksize == 0) { 353 dm_dev_unbusy(dmv); 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 dm_dev_unbusy(dmv); 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 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL) { 408 bp->b_error = EIO; 409 bp->b_resid = bp->b_bcount; 410 biodone(bio); 411 return 0; 412 } 413 414 switch(bp->b_cmd) { 415 case BUF_CMD_READ: 416 case BUF_CMD_WRITE: 417 case BUF_CMD_FREEBLKS: 418 bypass = 0; 419 break; 420 case BUF_CMD_FLUSH: 421 bypass = 1; 422 KKASSERT(buf_len == 0); 423 break; 424 default: 425 dm_dev_unbusy(dmv); 426 bp->b_error = EIO; 427 bp->b_resid = bp->b_bcount; 428 biodone(bio); 429 return 0; 430 } 431 432 if (bypass == 0 && 433 bounds_check_with_mediasize(bio, DEV_BSIZE, 434 dm_table_size(&dmv->table_head)) <= 0) { 435 dm_dev_unbusy(dmv); 436 bp->b_resid = bp->b_bcount; 437 biodone(bio); 438 return 0; 439 } 440 441 /* Select active table */ 442 tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE); 443 444 nestiobuf_init(bio); 445 446 /* 447 * Find out what tables I want to select. 448 */ 449 SLIST_FOREACH(table_en, tbl, next) { 450 /* 451 * I need need number of bytes not blocks. 452 */ 453 table_start = table_en->start * DEV_BSIZE; 454 table_end = table_start + (table_en->length) * DEV_BSIZE; 455 456 /* 457 * Calculate the start and end 458 */ 459 start = MAX(table_start, buf_start); 460 end = MIN(table_end, buf_start + buf_len); 461 462 aprint_debug("----------------------------------------\n"); 463 aprint_debug("table_start %010" PRIu64", table_end %010" 464 PRIu64 "\n", table_start, table_end); 465 aprint_debug("buf_start %010" PRIu64", buf_len %010" 466 PRIu64"\n", buf_start, buf_len); 467 aprint_debug("start-buf_start %010"PRIu64", end %010" 468 PRIu64"\n", start - buf_start, end); 469 aprint_debug("start %010" PRIu64" , end %010" 470 PRIu64"\n", start, end); 471 aprint_debug("\n----------------------------------------\n"); 472 473 if (bypass) { 474 nestbuf = getpbuf(NULL); 475 476 nestiobuf_add(bio, nestbuf, 0, 0); 477 nestbuf->b_bio1.bio_offset = 0; 478 table_en->target->strategy(table_en, nestbuf); 479 } else if (start < end) { 480 nestbuf = getpbuf(NULL); 481 nestiobuf_add(bio, nestbuf, 482 start - buf_start, (end - start)); 483 issued_len += end - start; 484 485 nestbuf->b_bio1.bio_offset = (start - table_start); 486 table_en->target->strategy(table_en, nestbuf); 487 } 488 } 489 490 if (issued_len < buf_len) 491 nestiobuf_error(bio, EINVAL); 492 nestiobuf_start(bio); 493 dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE); 494 dm_dev_unbusy(dmv); 495 496 return 0; 497 } 498 499 static int 500 dmsize(struct dev_psize_args *ap) 501 { 502 cdev_t dev = ap->a_head.a_dev; 503 dm_dev_t *dmv; 504 uint64_t size; 505 506 size = 0; 507 508 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL) 509 return -ENOENT; 510 511 size = dm_table_size(&dmv->table_head); 512 dm_dev_unbusy(dmv); 513 514 return size; 515 } 516 517 #if 0 518 static void 519 dmminphys(struct buf *bp) 520 { 521 522 bp->b_bcount = MIN(bp->b_bcount, MAXPHYS); 523 } 524 #endif 525 526 void 527 dmsetdiskinfo(struct disk *disk, dm_table_head_t *head) 528 { 529 struct disk_info info; 530 int dmp_size; 531 532 dmp_size = dm_table_size(head); 533 534 bzero(&info, sizeof(struct disk_info)); 535 info.d_media_blksize = DEV_BSIZE; 536 info.d_media_blocks = dmp_size; 537 info.d_media_size = dmp_size * DEV_BSIZE; 538 info.d_dsflags = DSO_MBRQUIET; /* XXX */ 539 info.d_secpertrack = 32; 540 info.d_nheads = 64; 541 info.d_secpercyl = info.d_secpertrack * info.d_nheads; 542 info.d_ncylinders = dmp_size / 2048; 543 bcopy(&info, &disk->d_info, sizeof(disk->d_info)); 544 } 545 546 prop_dictionary_t 547 dmgetdiskinfo(struct disk *disk) 548 { 549 prop_dictionary_t disk_info, geom; 550 struct disk_info *pinfo; 551 552 pinfo = &disk->d_info; 553 554 disk_info = prop_dictionary_create(); 555 geom = prop_dictionary_create(); 556 557 prop_dictionary_set_cstring_nocopy(disk_info, "type", "ESDI"); 558 prop_dictionary_set_uint64(geom, "sectors-per-unit", pinfo->d_media_blocks); 559 prop_dictionary_set_uint32(geom, "sector-size", 560 DEV_BSIZE /* XXX 512? */); 561 prop_dictionary_set_uint32(geom, "sectors-per-track", 32); 562 prop_dictionary_set_uint32(geom, "tracks-per-cylinder", 64); 563 prop_dictionary_set_uint32(geom, "cylinders-per-unit", 564 pinfo->d_media_blocks / 2048); 565 prop_dictionary_set(disk_info, "geometry", geom); 566 prop_object_release(geom); 567 568 return disk_info; 569 } 570 571 void 572 dmgetproperties(struct disk *disk, dm_table_head_t *head) 573 { 574 #if 0 575 prop_dictionary_t disk_info, odisk_info, geom; 576 int dmp_size; 577 578 dmp_size = dm_table_size(head); 579 disk_info = prop_dictionary_create(); 580 geom = prop_dictionary_create(); 581 582 prop_dictionary_set_cstring_nocopy(disk_info, "type", "ESDI"); 583 prop_dictionary_set_uint64(geom, "sectors-per-unit", dmp_size); 584 prop_dictionary_set_uint32(geom, "sector-size", 585 DEV_BSIZE /* XXX 512? */); 586 prop_dictionary_set_uint32(geom, "sectors-per-track", 32); 587 prop_dictionary_set_uint32(geom, "tracks-per-cylinder", 64); 588 prop_dictionary_set_uint32(geom, "cylinders-per-unit", dmp_size / 2048); 589 prop_dictionary_set(disk_info, "geometry", geom); 590 prop_object_release(geom); 591 592 odisk_info = disk->dk_info; 593 disk->dk_info = disk_info; 594 595 if (odisk_info != NULL) 596 prop_object_release(odisk_info); 597 #endif 598 } 599 600 TUNABLE_INT("debug.dm_debug", &dm_debug_level); 601 SYSCTL_INT(_debug, OID_AUTO, dm_debug, CTLFLAG_RW, &dm_debug_level, 602 0, "Eanble device mapper debugging"); 603 604