1 /* $NetBSD: device-mapper.c,v 1.10 2009/12/06 14:33:46 haad Exp $ */ 2 3 /* 4 * Copyright (c) 2008 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/dkio.h> 42 #include <sys/disk.h> 43 #include <sys/disklabel.h> 44 #include <sys/ioctl.h> 45 #include <sys/ioccom.h> 46 #include <sys/kmem.h> 47 #include <sys/module.h> 48 49 #include "netbsd-dm.h" 50 #include "dm.h" 51 52 static dev_type_open(dmopen); 53 static dev_type_close(dmclose); 54 static dev_type_read(dmread); 55 static dev_type_write(dmwrite); 56 static dev_type_ioctl(dmioctl); 57 static dev_type_strategy(dmstrategy); 58 static dev_type_size(dmsize); 59 60 /* attach and detach routines */ 61 int dmattach(void); 62 int dmdestroy(void); 63 64 static int dm_cmd_to_fun(prop_dictionary_t); 65 static int disk_ioctl_switch(dev_t, u_long, void *); 66 static int dm_ioctl_switch(u_long); 67 static void dmminphys(struct buf *); 68 69 /* ***Variable-definitions*** */ 70 const struct bdevsw dm_bdevsw = { 71 .d_open = dmopen, 72 .d_close = dmclose, 73 .d_strategy = dmstrategy, 74 .d_ioctl = dmioctl, 75 .d_dump = nodump, 76 .d_psize = dmsize, 77 .d_flag = D_DISK | D_MPSAFE 78 }; 79 80 const struct cdevsw dm_cdevsw = { 81 .d_open = dmopen, 82 .d_close = dmclose, 83 .d_read = dmread, 84 .d_write = dmwrite, 85 .d_ioctl = dmioctl, 86 .d_stop = nostop, 87 .d_tty = notty, 88 .d_poll = nopoll, 89 .d_mmap = nommap, 90 .d_kqfilter = nokqfilter, 91 .d_flag = D_DISK | D_MPSAFE 92 }; 93 94 const struct dkdriver dmdkdriver = { 95 .d_strategy = dmstrategy 96 }; 97 98 extern uint64_t dev_counter; 99 100 /* 101 * This array is used to translate cmd to function pointer. 102 * 103 * Interface between libdevmapper and lvm2tools uses different 104 * names for one IOCTL call because libdevmapper do another thing 105 * then. When I run "info" or "mknodes" libdevmapper will send same 106 * ioctl to kernel but will do another things in userspace. 107 * 108 */ 109 struct cmd_function cmd_fn[] = { 110 { .cmd = "version", .fn = dm_get_version_ioctl}, 111 { .cmd = "targets", .fn = dm_list_versions_ioctl}, 112 { .cmd = "create", .fn = dm_dev_create_ioctl}, 113 { .cmd = "info", .fn = dm_dev_status_ioctl}, 114 { .cmd = "mknodes", .fn = dm_dev_status_ioctl}, 115 { .cmd = "names", .fn = dm_dev_list_ioctl}, 116 { .cmd = "suspend", .fn = dm_dev_suspend_ioctl}, 117 { .cmd = "remove", .fn = dm_dev_remove_ioctl}, 118 { .cmd = "rename", .fn = dm_dev_rename_ioctl}, 119 { .cmd = "resume", .fn = dm_dev_resume_ioctl}, 120 { .cmd = "clear", .fn = dm_table_clear_ioctl}, 121 { .cmd = "deps", .fn = dm_table_deps_ioctl}, 122 { .cmd = "reload", .fn = dm_table_load_ioctl}, 123 { .cmd = "status", .fn = dm_table_status_ioctl}, 124 { .cmd = "table", .fn = dm_table_status_ioctl}, 125 {NULL, NULL} 126 }; 127 128 129 MODULE(MODULE_CLASS_DRIVER, dm, NULL); 130 131 /* New module handle routine */ 132 static int 133 dm_modcmd(modcmd_t cmd, void *arg) 134 { 135 #ifdef _MODULE 136 int bmajor = -1, cmajor = -1; 137 138 switch (cmd) { 139 case MODULE_CMD_INIT: 140 dmattach(); 141 return devsw_attach("dm", &dm_bdevsw, &bmajor, 142 &dm_cdevsw, &cmajor); 143 break; 144 145 case MODULE_CMD_FINI: 146 /* 147 * Disable unloading of dm module if there are any devices 148 * defined in driver. This is probably too strong we need 149 * to disable auto-unload only if there is mounted dm device 150 * present. 151 */ 152 if (dev_counter > 0) 153 return EBUSY; 154 dmdestroy(); 155 return devsw_detach(&dm_bdevsw, &dm_cdevsw); 156 break; 157 case MODULE_CMD_STAT: 158 return ENOTTY; 159 160 default: 161 return ENOTTY; 162 } 163 164 return 0; 165 #else 166 167 if (cmd == MODULE_CMD_INIT) 168 return 0; 169 return ENOTTY; 170 171 #endif /* _MODULE */ 172 } 173 174 175 /* attach routine */ 176 int 177 dmattach(void) 178 { 179 dm_target_init(); 180 dm_dev_init(); 181 dm_pdev_init(); 182 183 return 0; 184 } 185 186 /* Destroy routine */ 187 int 188 dmdestroy(void) 189 { 190 dm_dev_destroy(); 191 dm_pdev_destroy(); 192 dm_target_destroy(); 193 194 return 0; 195 } 196 197 static int 198 dmopen(dev_t dev, int flags, int mode, struct lwp *l) 199 { 200 aprint_debug("open routine called %" PRIu32 "\n", minor(dev)); 201 return 0; 202 } 203 204 static int 205 dmclose(dev_t dev, int flags, int mode, struct lwp *l) 206 { 207 aprint_debug("CLOSE routine called\n"); 208 209 return 0; 210 } 211 212 213 static int 214 dmioctl(dev_t dev, const u_long cmd, void *data, int flag, struct lwp *l) 215 { 216 int r; 217 prop_dictionary_t dm_dict_in; 218 219 r = 0; 220 221 aprint_debug("dmioctl called\n"); 222 223 KASSERT(data != NULL); 224 225 if (disk_ioctl_switch(dev, cmd, data) != 0) { 226 struct plistref *pref = (struct plistref *) data; 227 228 if((r = prop_dictionary_copyin_ioctl(pref, cmd, &dm_dict_in)) != 0) 229 return r; 230 231 if ((r = dm_check_version(dm_dict_in)) != 0) { 232 prop_object_release(dm_dict_in); 233 return r; 234 } 235 236 /* call cmd selected function */ 237 if ((r = dm_ioctl_switch(cmd)) != 0) { 238 prop_object_release(dm_dict_in); 239 return r; 240 } 241 242 /* run ioctl routine */ 243 if ((r = dm_cmd_to_fun(dm_dict_in)) != 0) { 244 prop_object_release(dm_dict_in); 245 return r; 246 } 247 248 r = prop_dictionary_copyout_ioctl(pref, cmd, dm_dict_in); 249 250 prop_object_release(dm_dict_in); 251 } 252 253 return r; 254 } 255 256 /* 257 * Translate command sent from libdevmapper to func. 258 */ 259 static int 260 dm_cmd_to_fun(prop_dictionary_t dm_dict){ 261 int i, r; 262 prop_string_t command; 263 264 r = 0; 265 266 if ((command = prop_dictionary_get(dm_dict, DM_IOCTL_COMMAND)) == NULL) 267 return EINVAL; 268 269 for(i = 0; cmd_fn[i].cmd != NULL; i++) 270 if (prop_string_equals_cstring(command, cmd_fn[i].cmd)) 271 break; 272 273 if (cmd_fn[i].cmd == NULL) 274 return EINVAL; 275 276 aprint_debug("ioctl %s called\n", cmd_fn[i].cmd); 277 r = cmd_fn[i].fn(dm_dict); 278 279 return r; 280 } 281 282 /* Call apropriate ioctl handler function. */ 283 static int 284 dm_ioctl_switch(u_long cmd) 285 { 286 int r; 287 288 r = 0; 289 290 switch(cmd) { 291 292 case NETBSD_DM_IOCTL: 293 aprint_debug("NetBSD_DM_IOCTL called\n"); 294 break; 295 296 default: 297 aprint_debug("unknown ioctl called\n"); 298 return ENOTTY; 299 break; /* NOT REACHED */ 300 } 301 302 return r; 303 } 304 305 /* 306 * Check for disk specific ioctls. 307 */ 308 309 static int 310 disk_ioctl_switch(dev_t dev, u_long cmd, void *data) 311 { 312 dm_dev_t *dmv; 313 314 switch(cmd) { 315 case DIOCGWEDGEINFO: 316 { 317 struct dkwedge_info *dkw = (void *) data; 318 319 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL) 320 return ENOENT; 321 322 aprint_debug("DIOCGWEDGEINFO ioctl called\n"); 323 324 strlcpy(dkw->dkw_devname, dmv->name, 16); 325 strlcpy(dkw->dkw_wname, dmv->name, DM_NAME_LEN); 326 strlcpy(dkw->dkw_parent, dmv->name, 16); 327 328 dkw->dkw_offset = 0; 329 dkw->dkw_size = dm_table_size(&dmv->table_head); 330 strcpy(dkw->dkw_ptype, DKW_PTYPE_FFS); 331 332 dm_dev_unbusy(dmv); 333 break; 334 } 335 336 case DIOCGDISKINFO: 337 { 338 struct plistref *pref = (struct plistref *) data; 339 340 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL) 341 return ENOENT; 342 343 if (dmv->diskp->dk_info == NULL) { 344 dm_dev_unbusy(dmv); 345 return ENOTSUP; 346 } else 347 prop_dictionary_copyout_ioctl(pref, cmd, 348 dmv->diskp->dk_info); 349 350 dm_dev_unbusy(dmv); 351 352 break; 353 } 354 355 default: 356 aprint_debug("unknown disk_ioctl called\n"); 357 return 1; 358 break; /* NOT REACHED */ 359 } 360 361 return 0; 362 } 363 364 /* 365 * Do all IO operations on dm logical devices. 366 */ 367 static void 368 dmstrategy(struct buf *bp) 369 { 370 dm_dev_t *dmv; 371 dm_table_t *tbl; 372 dm_table_entry_t *table_en; 373 struct buf *nestbuf; 374 375 uint32_t dev_type; 376 377 uint64_t buf_start, buf_len, issued_len; 378 uint64_t table_start, table_end; 379 uint64_t start, end; 380 381 buf_start = bp->b_blkno * DEV_BSIZE; 382 buf_len = bp->b_bcount; 383 384 tbl = NULL; 385 386 table_end = 0; 387 dev_type = 0; 388 issued_len = 0; 389 390 if ((dmv = dm_dev_lookup(NULL, NULL, minor(bp->b_dev))) == NULL) { 391 bp->b_error = EIO; 392 bp->b_resid = bp->b_bcount; 393 biodone(bp); 394 return; 395 } 396 397 if (bounds_check_with_mediasize(bp, DEV_BSIZE, 398 dm_table_size(&dmv->table_head)) <= 0) { 399 dm_dev_unbusy(dmv); 400 bp->b_resid = bp->b_bcount; 401 biodone(bp); 402 return; 403 } 404 405 /* FIXME: have to be called with IPL_BIO*/ 406 disk_busy(dmv->diskp); 407 408 /* Select active table */ 409 tbl = dm_table_get_entry(&dmv->table_head, DM_TABLE_ACTIVE); 410 411 /* Nested buffers count down to zero therefore I have 412 to set bp->b_resid to maximal value. */ 413 bp->b_resid = bp->b_bcount; 414 415 /* 416 * Find out what tables I want to select. 417 */ 418 SLIST_FOREACH(table_en, tbl, next) 419 { 420 /* I need need number of bytes not blocks. */ 421 table_start = table_en->start * DEV_BSIZE; 422 /* 423 * I have to sub 1 from table_en->length to prevent 424 * off by one error 425 */ 426 table_end = table_start + (table_en->length)* DEV_BSIZE; 427 428 start = MAX(table_start, buf_start); 429 430 end = MIN(table_end, buf_start + buf_len); 431 432 aprint_debug("----------------------------------------\n"); 433 aprint_debug("table_start %010" PRIu64", table_end %010" 434 PRIu64 "\n", table_start, table_end); 435 aprint_debug("buf_start %010" PRIu64", buf_len %010" 436 PRIu64"\n", buf_start, buf_len); 437 aprint_debug("start-buf_start %010"PRIu64", end %010" 438 PRIu64"\n", start - buf_start, end); 439 aprint_debug("start %010" PRIu64" , end %010" 440 PRIu64"\n", start, end); 441 aprint_debug("\n----------------------------------------\n"); 442 443 if (start < end) { 444 /* create nested buffer */ 445 nestbuf = getiobuf(NULL, true); 446 447 nestiobuf_setup(bp, nestbuf, start - buf_start, 448 (end - start)); 449 450 issued_len += end - start; 451 452 /* I need number of blocks. */ 453 nestbuf->b_blkno = (start - table_start) / DEV_BSIZE; 454 455 table_en->target->strategy(table_en, nestbuf); 456 } 457 } 458 459 if (issued_len < buf_len) 460 nestiobuf_done(bp, buf_len - issued_len, EINVAL); 461 462 /* FIXME have to be called with SPL_BIO*/ 463 disk_unbusy(dmv->diskp, buf_len, bp != NULL ? bp->b_flags & B_READ : 0); 464 465 dm_table_release(&dmv->table_head, DM_TABLE_ACTIVE); 466 dm_dev_unbusy(dmv); 467 468 return; 469 } 470 471 472 static int 473 dmread(dev_t dev, struct uio *uio, int flag) 474 { 475 return (physio(dmstrategy, NULL, dev, B_READ, dmminphys, uio)); 476 } 477 478 static int 479 dmwrite(dev_t dev, struct uio *uio, int flag) 480 { 481 return (physio(dmstrategy, NULL, dev, B_WRITE, dmminphys, uio)); 482 } 483 484 static int 485 dmsize(dev_t dev) 486 { 487 dm_dev_t *dmv; 488 uint64_t size; 489 490 size = 0; 491 492 if ((dmv = dm_dev_lookup(NULL, NULL, minor(dev))) == NULL) 493 return -ENOENT; 494 495 size = dm_table_size(&dmv->table_head); 496 dm_dev_unbusy(dmv); 497 498 return size; 499 } 500 501 static void 502 dmminphys(struct buf *bp) 503 { 504 bp->b_bcount = MIN(bp->b_bcount, MAXPHYS); 505 } 506 507 void 508 dmgetproperties(struct disk *disk, dm_table_head_t *head) 509 { 510 prop_dictionary_t disk_info, odisk_info, geom; 511 int dmp_size; 512 513 dmp_size = dm_table_size(head); 514 515 disk_info = prop_dictionary_create(); 516 517 prop_dictionary_set_cstring_nocopy(disk_info, "type", "ESDI"); 518 519 geom = prop_dictionary_create(); 520 521 prop_dictionary_set_uint64(geom, "sectors-per-unit", dmp_size); 522 523 prop_dictionary_set_uint32(geom, "sector-size", 524 DEV_BSIZE /* XXX 512? */); 525 526 prop_dictionary_set_uint32(geom, "sectors-per-track", 32); 527 528 prop_dictionary_set_uint32(geom, "tracks-per-cylinder", 64); 529 530 prop_dictionary_set_uint32(geom, "cylinders-per-unit", dmp_size / 2048); 531 532 prop_dictionary_set(disk_info, "geometry", geom); 533 prop_object_release(geom); 534 535 odisk_info = disk->dk_info; 536 537 disk->dk_info = disk_info; 538 539 if (odisk_info != NULL) 540 prop_object_release(odisk_info); 541 } 542