1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 /* 29 * Soft partitioning metadevice driver (md_sp). 30 * 31 * This file contains the primary operations of the soft partitioning 32 * metadevice driver. This includes all routines for normal operation 33 * (open/close/read/write). Please see mdvar.h for a definition of 34 * metadevice operations vector (md_ops_t). This driver is loosely 35 * based on the stripe driver (md_stripe). 36 * 37 * All metadevice administration is done through the use of ioctl's. 38 * As such, all administrative routines appear in sp_ioctl.c. 39 * 40 * Soft partitions are represented both in-core and in the metadb with a 41 * unit structure. The soft partition-specific information in the unit 42 * structure includes the following information: 43 * - Device information (md_dev64_t & md key) about the device on which 44 * the soft partition is built. 45 * - Soft partition status information. 46 * - The size of the soft partition and number of extents used to 47 * make up that size. 48 * - An array of exents which define virtual/physical offset 49 * mappings and lengths for each extent. 50 * 51 * Typical soft partition operation proceeds as follows: 52 * - The unit structure is fetched from the metadb and placed into 53 * an in-core array (as with other metadevices). This operation 54 * is performed via sp_build_incore( ) and takes place during 55 * "snarfing" (when all metadevices are brought in-core at 56 * once) and when a new soft partition is created. 57 * - A soft partition is opened via sp_open( ). At open time the 58 * the soft partition unit structure is verified with the soft 59 * partition on-disk structures. Additionally, the soft partition 60 * status is checked (only soft partitions in the OK state may be 61 * opened). 62 * - Soft partition I/O is performed via sp_strategy( ) which relies on 63 * a support routine, sp_mapbuf( ), to do most of the work. 64 * sp_mapbuf( ) maps a buffer to a particular extent via a binary 65 * search of the extent array in the soft partition unit structure. 66 * Once a translation has been performed, the I/O is passed down 67 * to the next layer, which may be another metadevice or a physical 68 * disk. Since a soft partition may contain multiple, non-contiguous 69 * extents, a single I/O may have to be fragmented. 70 * - Soft partitions are closed using sp_close. 71 * 72 */ 73 74 #include <sys/param.h> 75 #include <sys/systm.h> 76 #include <sys/conf.h> 77 #include <sys/file.h> 78 #include <sys/user.h> 79 #include <sys/uio.h> 80 #include <sys/t_lock.h> 81 #include <sys/buf.h> 82 #include <sys/dkio.h> 83 #include <sys/vtoc.h> 84 #include <sys/kmem.h> 85 #include <vm/page.h> 86 #include <sys/cmn_err.h> 87 #include <sys/sysmacros.h> 88 #include <sys/types.h> 89 #include <sys/mkdev.h> 90 #include <sys/stat.h> 91 #include <sys/open.h> 92 #include <sys/lvm/mdvar.h> 93 #include <sys/lvm/md_sp.h> 94 #include <sys/lvm/md_convert.h> 95 #include <sys/lvm/md_notify.h> 96 #include <sys/lvm/md_crc.h> 97 #include <sys/modctl.h> 98 #include <sys/ddi.h> 99 #include <sys/sunddi.h> 100 #include <sys/debug.h> 101 102 #include <sys/sysevent/eventdefs.h> 103 #include <sys/sysevent/svm.h> 104 105 md_ops_t sp_md_ops; 106 #ifndef lint 107 char _depends_on[] = "drv/md"; 108 md_ops_t *md_interface_ops = &sp_md_ops; 109 #endif 110 111 extern unit_t md_nunits; 112 extern set_t md_nsets; 113 extern md_set_t md_set[]; 114 115 extern int md_status; 116 extern major_t md_major; 117 extern mdq_anchor_t md_done_daemon; 118 extern mdq_anchor_t md_sp_daemon; 119 extern kmutex_t md_mx; 120 extern kcondvar_t md_cv; 121 extern md_krwlock_t md_unit_array_rw; 122 123 static kmem_cache_t *sp_parent_cache = NULL; 124 static kmem_cache_t *sp_child_cache = NULL; 125 static void sp_send_stat_ok(mp_unit_t *); 126 static void sp_send_stat_err(mp_unit_t *); 127 128 /* 129 * FUNCTION: sp_parent_constructor() 130 * INPUT: none. 131 * OUTPUT: ps - parent save structure initialized. 132 * RETURNS: void * - ptr to initialized parent save structure. 133 * PURPOSE: initialize parent save structure. 134 */ 135 /*ARGSUSED1*/ 136 static int 137 sp_parent_constructor(void *p, void *d1, int d2) 138 { 139 mutex_init(&((md_spps_t *)p)->ps_mx, 140 NULL, MUTEX_DEFAULT, NULL); 141 return (0); 142 } 143 144 static void 145 sp_parent_init(md_spps_t *ps) 146 { 147 bzero(ps, offsetof(md_spps_t, ps_mx)); 148 } 149 150 /*ARGSUSED1*/ 151 static void 152 sp_parent_destructor(void *p, void *d) 153 { 154 mutex_destroy(&((md_spps_t *)p)->ps_mx); 155 } 156 157 /* 158 * FUNCTION: sp_child_constructor() 159 * INPUT: none. 160 * OUTPUT: cs - child save structure initialized. 161 * RETURNS: void * - ptr to initialized child save structure. 162 * PURPOSE: initialize child save structure. 163 */ 164 /*ARGSUSED1*/ 165 static int 166 sp_child_constructor(void *p, void *d1, int d2) 167 { 168 bioinit(&((md_spcs_t *)p)->cs_buf); 169 return (0); 170 } 171 172 static void 173 sp_child_init(md_spcs_t *cs) 174 { 175 cs->cs_mdunit = 0; 176 cs->cs_ps = NULL; 177 md_bioreset(&cs->cs_buf); 178 } 179 180 /*ARGSUSED1*/ 181 static void 182 sp_child_destructor(void *p, void *d) 183 { 184 biofini(&((md_spcs_t *)p)->cs_buf); 185 } 186 187 /* 188 * FUNCTION: sp_run_queue() 189 * INPUT: none. 190 * OUTPUT: none. 191 * RETURNS: void. 192 * PURPOSE: run the md_daemon to clean up memory pool. 193 */ 194 /*ARGSUSED*/ 195 static void 196 sp_run_queue(void *d) 197 { 198 if (!(md_status & MD_GBL_DAEMONS_LIVE)) 199 md_daemon(1, &md_done_daemon); 200 } 201 202 203 /* 204 * FUNCTION: sp_build_incore() 205 * INPUT: p - ptr to unit structure. 206 * snarfing - flag to tell us we are snarfing. 207 * OUTPUT: non. 208 * RETURNS: int - 0 (always). 209 * PURPOSE: place unit structure into in-core unit array (keyed from 210 * minor number). 211 */ 212 int 213 sp_build_incore(void *p, int snarfing) 214 { 215 mp_unit_t *un = (mp_unit_t *)p; 216 minor_t mnum; 217 set_t setno; 218 md_dev64_t tmpdev; 219 220 mnum = MD_SID(un); 221 222 if (MD_UNIT(mnum) != NULL) 223 return (0); 224 225 MD_STATUS(un) = 0; 226 227 if (snarfing) { 228 /* 229 * if we are snarfing, we get the device information 230 * from the metadb record (using the metadb key for 231 * that device). 232 */ 233 setno = MD_MIN2SET(mnum); 234 235 tmpdev = md_getdevnum(setno, mddb_getsidenum(setno), 236 un->un_key, MD_NOTRUST_DEVT); 237 un->un_dev = tmpdev; 238 } 239 240 /* place unit in in-core array */ 241 MD_UNIT(mnum) = un; 242 return (0); 243 } 244 245 /* 246 * FUNCTION: reset_sp() 247 * INPUT: un - unit structure to be reset/removed. 248 * mnum - minor number to be reset/removed. 249 * removing - flag to tell us if we are removing 250 * permanently or just reseting in-core 251 * structures. 252 * OUTPUT: none. 253 * RETURNS: void. 254 * PURPOSE: used to either simply reset in-core structures or to 255 * permanently remove metadevices from the metadb. 256 */ 257 void 258 reset_sp(mp_unit_t *un, minor_t mnum, int removing) 259 { 260 sv_dev_t *sv; 261 mddb_recid_t vtoc_id; 262 263 /* clean up in-core structures */ 264 md_destroy_unit_incore(mnum, &sp_md_ops); 265 266 MD_UNIT(mnum) = NULL; 267 268 /* 269 * Attempt release of minor node 270 */ 271 (void) md_remove_minor_node(mnum); 272 273 if (!removing) 274 return; 275 276 /* we are removing the soft partition from the metadb */ 277 278 /* 279 * Save off device information so we can get to 280 * it after we do the mddb_deleterec(). 281 */ 282 sv = (sv_dev_t *)kmem_alloc(sizeof (sv_dev_t), KM_SLEEP); 283 sv->setno = MD_MIN2SET(mnum); 284 sv->key = un->un_key; 285 vtoc_id = un->c.un_vtoc_id; 286 287 /* 288 * Remove self from the namespace 289 */ 290 if (un->c.un_revision & MD_FN_META_DEV) { 291 (void) md_rem_selfname(un->c.un_self_id); 292 } 293 294 /* Remove the unit structure */ 295 mddb_deleterec_wrapper(un->c.un_record_id); 296 297 if (vtoc_id) 298 mddb_deleterec_wrapper(vtoc_id); 299 300 SE_NOTIFY(EC_SVM_CONFIG, ESC_SVM_DELETE, TAG_METADEVICE, 301 MD_MIN2SET(mnum), MD_MIN2UNIT(mnum)); 302 303 /* 304 * remove the underlying device name from the metadb. if other 305 * soft partitions are built on this device, this will simply 306 * decrease the reference count for this device. otherwise the 307 * name record for this device will be removed from the metadb. 308 */ 309 md_rem_names(sv, 1); 310 kmem_free(sv, sizeof (sv_dev_t)); 311 } 312 313 /* 314 * FUNCTION: sp_send_stat_msg 315 * INPUT: un - unit reference 316 * status - status to be sent to master node 317 * MD_SP_OK - soft-partition is now OK 318 * MD_SP_ERR " " errored 319 * OUTPUT: none. 320 * RETURNS: void. 321 * PURPOSE: send a soft-partition status change to the master node. If the 322 * message succeeds we simply return. If it fails we panic as the 323 * cluster-wide view of the metadevices is now inconsistent. 324 * CALLING CONTEXT: 325 * Blockable. No locks can be held. 326 */ 327 static void 328 sp_send_stat_msg(mp_unit_t *un, sp_status_t status) 329 { 330 md_mn_msg_sp_setstat_t sp_msg; 331 md_mn_kresult_t *kres; 332 set_t setno = MD_UN2SET(un); 333 int rval; 334 const char *str = (status == MD_SP_ERR) ? "MD_SP_ERR" : "MD_SP_OK"; 335 336 sp_msg.sp_setstat_mnum = MD_SID(un); 337 sp_msg.sp_setstat_status = status; 338 339 kres = kmem_alloc(sizeof (md_mn_kresult_t), KM_SLEEP); 340 341 rval = mdmn_ksend_message(setno, MD_MN_MSG_SP_SETSTAT2, MD_MSGF_NO_LOG, 342 (char *)&sp_msg, sizeof (sp_msg), kres); 343 344 if (!MDMN_KSEND_MSG_OK(rval, kres)) { 345 mdmn_ksend_show_error(rval, kres, "MD_MN_MSG_SP_SETSTAT2"); 346 347 /* 348 * Panic as we are now in an inconsistent state. 349 */ 350 351 cmn_err(CE_PANIC, "md: %s: %s could not be set on all nodes\n", 352 md_shortname(MD_SID(un)), str); 353 } 354 355 kmem_free(kres, sizeof (md_mn_kresult_t)); 356 } 357 358 /* 359 * FUNCTION: sp_finish_error 360 * INPUT: ps - parent save structure for error-ed I/O. 361 * lock_held - set if the unit readerlock is held 362 * OUTPUT: none. 363 * RETURNS: void. 364 * PURPOSE: report a driver error 365 */ 366 static void 367 sp_finish_error(md_spps_t *ps, int lock_held) 368 { 369 struct buf *pb = ps->ps_bp; 370 mdi_unit_t *ui = ps->ps_ui; 371 md_dev64_t un_dev; /* underlying device */ 372 md_dev64_t md_dev = md_expldev(pb->b_edev); /* metadev in error */ 373 char *str; 374 375 un_dev = md_expldev(ps->ps_un->un_dev); 376 /* set error type */ 377 if (pb->b_flags & B_READ) { 378 str = "read"; 379 } else { 380 str = "write"; 381 } 382 383 384 SPPS_FREE(sp_parent_cache, ps); 385 pb->b_flags |= B_ERROR; 386 387 md_kstat_done(ui, pb, 0); 388 389 if (lock_held) { 390 md_unit_readerexit(ui); 391 } 392 md_biodone(pb); 393 394 cmn_err(CE_WARN, "md: %s: %s error on %s", 395 md_shortname(md_getminor(md_dev)), str, 396 md_devname(MD_DEV2SET(md_dev), un_dev, NULL, 0)); 397 } 398 399 400 /* 401 * FUNCTION: sp_xmit_ok 402 * INPUT: dq - daemon queue referencing failing ps structure 403 * OUTPUT: none. 404 * RETURNS: void. 405 * PURPOSE: send a message to the master node in a multi-owner diskset to 406 * update all attached nodes view of the soft-part to be MD_SP_OK. 407 * CALLING CONTEXT: 408 * Blockable. No unit lock held. 409 */ 410 static void 411 sp_xmit_ok(daemon_queue_t *dq) 412 { 413 md_spps_t *ps = (md_spps_t *)dq; 414 415 /* Send a MD_MN_MSG_SP_SETSTAT to the master */ 416 sp_send_stat_msg(ps->ps_un, MD_SP_OK); 417 418 /* 419 * Successfully transmitted error state to all nodes, now release this 420 * parent structure. 421 */ 422 SPPS_FREE(sp_parent_cache, ps); 423 } 424 425 /* 426 * FUNCTION: sp_xmit_error 427 * INPUT: dq - daemon queue referencing failing ps structure 428 * OUTPUT: none. 429 * RETURNS: void. 430 * PURPOSE: send a message to the master node in a multi-owner diskset to 431 * update all attached nodes view of the soft-part to be MD_SP_ERR. 432 * CALLING CONTEXT: 433 * Blockable. No unit lock held. 434 */ 435 static void 436 sp_xmit_error(daemon_queue_t *dq) 437 { 438 md_spps_t *ps = (md_spps_t *)dq; 439 440 /* Send a MD_MN_MSG_SP_SETSTAT to the master */ 441 sp_send_stat_msg(ps->ps_un, MD_SP_ERR); 442 443 /* 444 * Successfully transmitted error state to all nodes, now release this 445 * parent structure. 446 */ 447 SPPS_FREE(sp_parent_cache, ps); 448 } 449 static void 450 sp_send_stat_ok(mp_unit_t *un) 451 { 452 minor_t mnum = MD_SID(un); 453 md_spps_t *ps; 454 455 ps = kmem_cache_alloc(sp_parent_cache, MD_ALLOCFLAGS); 456 sp_parent_init(ps); 457 ps->ps_un = un; 458 ps->ps_ui = MDI_UNIT(mnum); 459 460 daemon_request(&md_sp_daemon, sp_xmit_ok, (daemon_queue_t *)ps, 461 REQ_OLD); 462 } 463 464 static void 465 sp_send_stat_err(mp_unit_t *un) 466 { 467 minor_t mnum = MD_SID(un); 468 md_spps_t *ps; 469 470 ps = kmem_cache_alloc(sp_parent_cache, MD_ALLOCFLAGS); 471 sp_parent_init(ps); 472 ps->ps_un = un; 473 ps->ps_ui = MDI_UNIT(mnum); 474 475 daemon_request(&md_sp_daemon, sp_xmit_error, (daemon_queue_t *)ps, 476 REQ_OLD); 477 } 478 479 480 /* 481 * FUNCTION: sp_error() 482 * INPUT: ps - parent save structure for error-ed I/O. 483 * OUTPUT: none. 484 * RETURNS: void. 485 * PURPOSE: report a driver error. 486 * CALLING CONTEXT: 487 * Interrupt - non-blockable 488 */ 489 static void 490 sp_error(md_spps_t *ps) 491 { 492 set_t setno = MD_UN2SET(ps->ps_un); 493 494 /* 495 * Drop the mutex associated with this request before (potentially) 496 * enqueuing the free onto a separate thread. We have to release the 497 * mutex before destroying the parent structure. 498 */ 499 if (!(ps->ps_flags & MD_SPPS_DONTFREE)) { 500 if (MUTEX_HELD(&ps->ps_mx)) { 501 mutex_exit(&ps->ps_mx); 502 } 503 } else { 504 /* 505 * this should only ever happen if we are panicking, 506 * since DONTFREE is only set on the parent if panicstr 507 * is non-NULL. 508 */ 509 ASSERT(panicstr); 510 } 511 512 /* 513 * For a multi-owner set we need to send a message to the master so that 514 * all nodes get the errored status when we first encounter it. To avoid 515 * deadlocking when multiple soft-partitions encounter an error on one 516 * physical unit we drop the unit readerlock before enqueueing the 517 * request. That way we can service any messages that require a 518 * writerlock to be held. Additionally, to avoid deadlocking when at 519 * the bottom of a metadevice stack and a higher level mirror has 520 * multiple requests outstanding on this soft-part, we clone the ps 521 * that failed and pass the error back up the stack to release the 522 * reference that this i/o may have in the higher-level metadevice. 523 * The other nodes in the cluster just have to modify the soft-part 524 * status and we do not need to block the i/o completion for this. 525 */ 526 if (MD_MNSET_SETNO(setno)) { 527 md_spps_t *err_ps; 528 err_ps = kmem_cache_alloc(sp_parent_cache, MD_ALLOCFLAGS); 529 sp_parent_init(err_ps); 530 531 err_ps->ps_un = ps->ps_un; 532 err_ps->ps_ui = ps->ps_ui; 533 534 md_unit_readerexit(ps->ps_ui); 535 536 daemon_request(&md_sp_daemon, sp_xmit_error, 537 (daemon_queue_t *)err_ps, REQ_OLD); 538 539 sp_finish_error(ps, 0); 540 541 return; 542 } else { 543 ps->ps_un->un_status = MD_SP_ERR; 544 } 545 546 /* Flag the error */ 547 sp_finish_error(ps, 1); 548 549 } 550 551 /* 552 * FUNCTION: sp_mapbuf() 553 * INPUT: un - unit structure for soft partition we are doing 554 * I/O on. 555 * voff - virtual offset in soft partition to map. 556 * bcount - # of blocks in the I/O. 557 * OUTPUT: bp - translated buffer to be passed down to next layer. 558 * RETURNS: 1 - request must be fragmented, more work to do, 559 * 0 - request satisified, no more work to do 560 * -1 - error 561 * PURPOSE: Map the the virtual offset in the soft partition (passed 562 * in via voff) to the "physical" offset on whatever the soft 563 * partition is built on top of. We do this by doing a binary 564 * search of the extent array in the soft partition unit 565 * structure. Once the current extent is found, we do the 566 * translation, determine if the I/O will cross extent 567 * boundaries (if so, we have to fragment the I/O), then 568 * fill in the buf structure to be passed down to the next layer. 569 */ 570 static int 571 sp_mapbuf( 572 mp_unit_t *un, 573 sp_ext_offset_t voff, 574 sp_ext_length_t bcount, 575 buf_t *bp 576 ) 577 { 578 int lo, mid, hi, found, more; 579 size_t new_bcount; 580 sp_ext_offset_t new_blkno; 581 sp_ext_offset_t new_offset; 582 sp_ext_offset_t ext_endblk; 583 md_dev64_t new_edev; 584 extern unsigned md_maxphys; 585 586 found = 0; 587 lo = 0; 588 hi = un->un_numexts - 1; 589 590 /* 591 * do a binary search to find the extent that contains the 592 * starting offset. after this loop, mid contains the index 593 * of the correct extent. 594 */ 595 while (lo <= hi && !found) { 596 mid = (lo + hi) / 2; 597 /* is the starting offset contained within the mid-ext? */ 598 if (voff >= un->un_ext[mid].un_voff && 599 voff < un->un_ext[mid].un_voff + un->un_ext[mid].un_len) 600 found = 1; 601 else if (voff < un->un_ext[mid].un_voff) 602 hi = mid - 1; 603 else /* voff > un->un_ext[mid].un_voff + un->un_ext[mid].len */ 604 lo = mid + 1; 605 } 606 607 if (!found) { 608 cmn_err(CE_WARN, "sp_mapbuf: invalid offset %llu.\n", voff); 609 return (-1); 610 } 611 612 /* translate to underlying physical offset/device */ 613 new_offset = voff - un->un_ext[mid].un_voff; 614 new_blkno = un->un_ext[mid].un_poff + new_offset; 615 new_edev = un->un_dev; 616 617 /* determine if we need to break the I/O into fragments */ 618 ext_endblk = un->un_ext[mid].un_voff + un->un_ext[mid].un_len; 619 if (voff + btodb(bcount) > ext_endblk) { 620 new_bcount = dbtob(ext_endblk - voff); 621 more = 1; 622 } else { 623 new_bcount = bcount; 624 more = 0; 625 } 626 627 /* only break up the I/O if we're not built on another metadevice */ 628 if ((md_getmajor(new_edev) != md_major) && (new_bcount > md_maxphys)) { 629 new_bcount = md_maxphys; 630 more = 1; 631 } 632 if (bp != (buf_t *)NULL) { 633 /* do bp updates */ 634 bp->b_bcount = new_bcount; 635 bp->b_lblkno = new_blkno; 636 bp->b_edev = md_dev64_to_dev(new_edev); 637 } 638 return (more); 639 } 640 641 /* 642 * FUNCTION: sp_validate() 643 * INPUT: un - unit structure to be validated. 644 * OUTPUT: none. 645 * RETURNS: 0 - soft partition ok. 646 * -1 - error. 647 * PURPOSE: called on open to sanity check the soft partition. In 648 * order to open a soft partition: 649 * - it must have at least one extent 650 * - the extent info in core and on disk must match 651 * - it may not be in an intermediate state (which would 652 * imply that a two-phase commit was interrupted) 653 * 654 * If the extent checking fails (B_ERROR returned from the read 655 * strategy call) _and_ we're a multi-owner diskset, we send a 656 * message to the master so that all nodes inherit the same view 657 * of the soft partition. 658 * If we are checking a soft-part that is marked as in error, and 659 * we can actually read and validate the watermarks we send a 660 * message to clear the error to the master node. 661 */ 662 static int 663 sp_validate(mp_unit_t *un) 664 { 665 uint_t ext; 666 struct buf *buf; 667 sp_ext_length_t len; 668 mp_watermark_t *wm; 669 set_t setno; 670 int reset_error = 0; 671 672 setno = MD_UN2SET(un); 673 674 /* sanity check unit structure components ?? */ 675 if (un->un_status != MD_SP_OK) { 676 if (un->un_status != MD_SP_ERR) { 677 cmn_err(CE_WARN, "md: %s: open failed, soft partition " 678 "status is %u.", 679 md_shortname(MD_SID(un)), 680 un->un_status); 681 return (-1); 682 } else { 683 cmn_err(CE_WARN, "md: %s: open of soft partition " 684 "in Errored state.", 685 md_shortname(MD_SID(un))); 686 reset_error = 1; 687 } 688 } 689 690 if (un->un_numexts == 0) { 691 cmn_err(CE_WARN, "md: %s: open failed, soft partition does " 692 "not have any extents.", md_shortname(MD_SID(un))); 693 return (-1); 694 } 695 696 len = 0LL; 697 for (ext = 0; ext < un->un_numexts; ext++) { 698 699 /* tally extent lengths to check total size */ 700 len += un->un_ext[ext].un_len; 701 702 /* allocate buffer for watermark */ 703 buf = getrbuf(KM_SLEEP); 704 705 /* read watermark */ 706 buf->b_flags = B_READ; 707 buf->b_edev = md_dev64_to_dev(un->un_dev); 708 buf->b_iodone = NULL; 709 buf->b_proc = NULL; 710 buf->b_bcount = sizeof (mp_watermark_t); 711 buf->b_lblkno = un->un_ext[ext].un_poff - 1; 712 buf->b_bufsize = sizeof (mp_watermark_t); 713 buf->b_un.b_addr = kmem_alloc(sizeof (mp_watermark_t), 714 KM_SLEEP); 715 716 /* 717 * make the call non-blocking so that it is not affected 718 * by a set take. 719 */ 720 md_call_strategy(buf, MD_STR_MAPPED|MD_NOBLOCK, NULL); 721 (void) biowait(buf); 722 723 if (buf->b_flags & B_ERROR) { 724 cmn_err(CE_WARN, "md: %s: open failed, could not " 725 "read watermark at block %llu for extent %u, " 726 "error %d.", md_shortname(MD_SID(un)), 727 buf->b_lblkno, ext, buf->b_error); 728 kmem_free(buf->b_un.b_addr, sizeof (mp_watermark_t)); 729 freerbuf(buf); 730 731 /* 732 * If we're a multi-owner diskset we send a message 733 * indicating that this soft-part has an invalid 734 * extent to the master node. This ensures a consistent 735 * view of the soft-part across the cluster. 736 */ 737 if (MD_MNSET_SETNO(setno)) { 738 sp_send_stat_err(un); 739 } 740 return (-1); 741 } 742 743 wm = (mp_watermark_t *)buf->b_un.b_addr; 744 745 /* make sure the checksum is correct first */ 746 if (crcchk((uchar_t *)wm, (uint_t *)&wm->wm_checksum, 747 (uint_t)sizeof (mp_watermark_t), (uchar_t *)NULL)) { 748 cmn_err(CE_WARN, "md: %s: open failed, watermark " 749 "at block %llu for extent %u does not have a " 750 "valid checksum 0x%08x.", md_shortname(MD_SID(un)), 751 buf->b_lblkno, ext, wm->wm_checksum); 752 kmem_free(buf->b_un.b_addr, sizeof (mp_watermark_t)); 753 freerbuf(buf); 754 return (-1); 755 } 756 757 if (wm->wm_magic != MD_SP_MAGIC) { 758 cmn_err(CE_WARN, "md: %s: open failed, watermark " 759 "at block %llu for extent %u does not have a " 760 "valid watermark magic number, expected 0x%x, " 761 "found 0x%x.", md_shortname(MD_SID(un)), 762 buf->b_lblkno, ext, MD_SP_MAGIC, wm->wm_magic); 763 kmem_free(buf->b_un.b_addr, sizeof (mp_watermark_t)); 764 freerbuf(buf); 765 return (-1); 766 } 767 768 /* make sure sequence number matches the current extent */ 769 if (wm->wm_seq != ext) { 770 cmn_err(CE_WARN, "md: %s: open failed, watermark " 771 "at block %llu for extent %u has invalid " 772 "sequence number %u.", md_shortname(MD_SID(un)), 773 buf->b_lblkno, ext, wm->wm_seq); 774 kmem_free(buf->b_un.b_addr, sizeof (mp_watermark_t)); 775 freerbuf(buf); 776 return (-1); 777 } 778 779 /* make sure watermark length matches unit structure */ 780 if (wm->wm_length != un->un_ext[ext].un_len) { 781 cmn_err(CE_WARN, "md: %s: open failed, watermark " 782 "at block %llu for extent %u has inconsistent " 783 "length, expected %llu, found %llu.", 784 md_shortname(MD_SID(un)), buf->b_lblkno, 785 ext, un->un_ext[ext].un_len, 786 (u_longlong_t)wm->wm_length); 787 kmem_free(buf->b_un.b_addr, sizeof (mp_watermark_t)); 788 freerbuf(buf); 789 return (-1); 790 } 791 792 /* 793 * make sure the type is a valid soft partition and not 794 * a free extent or the end. 795 */ 796 if (wm->wm_type != EXTTYP_ALLOC) { 797 cmn_err(CE_WARN, "md: %s: open failed, watermark " 798 "at block %llu for extent %u is not marked " 799 "as in-use, type = %u.", md_shortname(MD_SID(un)), 800 buf->b_lblkno, ext, wm->wm_type); 801 kmem_free(buf->b_un.b_addr, sizeof (mp_watermark_t)); 802 freerbuf(buf); 803 return (-1); 804 } 805 /* free up buffer */ 806 kmem_free(buf->b_un.b_addr, sizeof (mp_watermark_t)); 807 freerbuf(buf); 808 } 809 810 if (len != un->un_length) { 811 cmn_err(CE_WARN, "md: %s: open failed, computed length " 812 "%llu != expected length %llu.", md_shortname(MD_SID(un)), 813 len, un->un_length); 814 return (-1); 815 } 816 817 /* 818 * If we're a multi-owner set _and_ reset_error is set, we should clear 819 * the error condition on all nodes in the set. Use SP_SETSTAT2 with 820 * MD_SP_OK. 821 */ 822 if (MD_MNSET_SETNO(setno) && reset_error) { 823 sp_send_stat_ok(un); 824 } 825 return (0); 826 } 827 828 /* 829 * FUNCTION: sp_done() 830 * INPUT: child_buf - buffer attached to child save structure. 831 * this is the buffer on which I/O has just 832 * completed. 833 * OUTPUT: none. 834 * RETURNS: 0 - success. 835 * 1 - error. 836 * PURPOSE: called on I/O completion. 837 */ 838 static int 839 sp_done(struct buf *child_buf) 840 { 841 struct buf *parent_buf; 842 mdi_unit_t *ui; 843 md_spps_t *ps; 844 md_spcs_t *cs; 845 846 /* find the child save structure to which this buffer belongs */ 847 cs = (md_spcs_t *)((caddr_t)child_buf - 848 (sizeof (md_spcs_t) - sizeof (buf_t))); 849 /* now get the parent save structure */ 850 ps = cs->cs_ps; 851 parent_buf = ps->ps_bp; 852 853 mutex_enter(&ps->ps_mx); 854 /* pass any errors back up to the parent */ 855 if (child_buf->b_flags & B_ERROR) { 856 ps->ps_flags |= MD_SPPS_ERROR; 857 parent_buf->b_error = child_buf->b_error; 858 } 859 /* mapout, if needed */ 860 if (child_buf->b_flags & B_REMAPPED) 861 bp_mapout(child_buf); 862 863 ps->ps_frags--; 864 if (ps->ps_frags != 0) { 865 /* 866 * if this parent has more children, we just free the 867 * child and return. 868 */ 869 kmem_cache_free(sp_child_cache, cs); 870 mutex_exit(&ps->ps_mx); 871 return (1); 872 } 873 /* there are no more children */ 874 kmem_cache_free(sp_child_cache, cs); 875 if (ps->ps_flags & MD_SPPS_ERROR) { 876 sp_error(ps); 877 return (1); 878 } 879 ui = ps->ps_ui; 880 if (!(ps->ps_flags & MD_SPPS_DONTFREE)) { 881 mutex_exit(&ps->ps_mx); 882 } else { 883 /* 884 * this should only ever happen if we are panicking, 885 * since DONTFREE is only set on the parent if panicstr 886 * is non-NULL. 887 */ 888 ASSERT(panicstr); 889 } 890 SPPS_FREE(sp_parent_cache, ps); 891 md_kstat_done(ui, parent_buf, 0); 892 md_unit_readerexit(ui); 893 md_biodone(parent_buf); 894 return (0); 895 } 896 897 /* 898 * FUNCTION: md_sp_strategy() 899 * INPUT: parent_buf - parent buffer 900 * flag - flags 901 * private - private data 902 * OUTPUT: none. 903 * RETURNS: void. 904 * PURPOSE: Soft partitioning I/O strategy. Performs the main work 905 * needed to do I/O to a soft partition. The basic 906 * algorithm is as follows: 907 * - Allocate a child save structure to keep track 908 * of the I/O we are going to pass down. 909 * - Map the I/O to the correct extent in the soft 910 * partition (see sp_mapbuf()). 911 * - bioclone() the buffer and pass it down the 912 * stack using md_call_strategy. 913 * - If the I/O needs to split across extents, 914 * repeat the above steps until all fragments 915 * are finished. 916 */ 917 static void 918 md_sp_strategy(buf_t *parent_buf, int flag, void *private) 919 { 920 md_spps_t *ps; 921 md_spcs_t *cs; 922 int more; 923 mp_unit_t *un; 924 mdi_unit_t *ui; 925 size_t current_count; 926 off_t current_offset; 927 sp_ext_offset_t current_blkno; 928 buf_t *child_buf; 929 set_t setno = MD_MIN2SET(getminor(parent_buf->b_edev)); 930 int strat_flag = flag; 931 932 /* 933 * When doing IO to a multi owner meta device, check if set is halted. 934 * We do this check without the needed lock held, for performance 935 * reasons. 936 * If an IO just slips through while the set is locked via an 937 * MD_MN_SUSPEND_SET, we don't care about it. 938 * Only check for suspension if we are a top-level i/o request 939 * (MD_STR_NOTTOP is cleared in 'flag'); 940 */ 941 if ((md_set[setno].s_status & (MD_SET_HALTED | MD_SET_MNSET)) == 942 (MD_SET_HALTED | MD_SET_MNSET)) { 943 if ((flag & MD_STR_NOTTOP) == 0) { 944 mutex_enter(&md_mx); 945 /* Here we loop until the set is no longer halted */ 946 while (md_set[setno].s_status & MD_SET_HALTED) { 947 cv_wait(&md_cv, &md_mx); 948 } 949 mutex_exit(&md_mx); 950 } 951 } 952 953 ui = MDI_UNIT(getminor(parent_buf->b_edev)); 954 955 md_kstat_waitq_enter(ui); 956 957 un = (mp_unit_t *)md_unit_readerlock(ui); 958 959 if ((flag & MD_NOBLOCK) == 0) { 960 if (md_inc_iocount(setno) != 0) { 961 parent_buf->b_flags |= B_ERROR; 962 parent_buf->b_error = ENXIO; 963 parent_buf->b_resid = parent_buf->b_bcount; 964 md_unit_readerexit(ui); 965 biodone(parent_buf); 966 return; 967 } 968 } else { 969 md_inc_iocount_noblock(setno); 970 } 971 972 if (!(flag & MD_STR_NOTTOP)) { 973 if (md_checkbuf(ui, (md_unit_t *)un, parent_buf) != 0) { 974 md_kstat_waitq_exit(ui); 975 return; 976 } 977 } 978 979 ps = kmem_cache_alloc(sp_parent_cache, MD_ALLOCFLAGS); 980 sp_parent_init(ps); 981 982 /* 983 * Save essential information from the original buffhdr 984 * in the parent. 985 */ 986 ps->ps_un = un; 987 ps->ps_ui = ui; 988 ps->ps_bp = parent_buf; 989 ps->ps_addr = parent_buf->b_un.b_addr; 990 991 current_count = parent_buf->b_bcount; 992 current_blkno = (sp_ext_offset_t)parent_buf->b_blkno; 993 current_offset = 0; 994 995 /* 996 * if we are at the top and we are panicking, 997 * we don't free in order to save state. 998 */ 999 if (!(flag & MD_STR_NOTTOP) && (panicstr != NULL)) 1000 ps->ps_flags |= MD_SPPS_DONTFREE; 1001 1002 md_kstat_waitq_to_runq(ui); 1003 1004 ps->ps_frags++; 1005 1006 /* 1007 * Mark this i/o as MD_STR_ABR if we've had ABR enabled on this 1008 * metadevice. 1009 */ 1010 if (ui->ui_tstate & MD_ABR_CAP) 1011 strat_flag |= MD_STR_ABR; 1012 1013 /* 1014 * this loop does the main work of an I/O. we allocate a 1015 * a child save for each buf, do the logical to physical 1016 * mapping, decide if we need to frag the I/O, clone the 1017 * new I/O to pass down the stack. repeat until we've 1018 * taken care of the entire buf that was passed to us. 1019 */ 1020 do { 1021 cs = kmem_cache_alloc(sp_child_cache, MD_ALLOCFLAGS); 1022 sp_child_init(cs); 1023 child_buf = &cs->cs_buf; 1024 cs->cs_ps = ps; 1025 1026 more = sp_mapbuf(un, current_blkno, current_count, child_buf); 1027 if (more == -1) { 1028 parent_buf->b_flags |= B_ERROR; 1029 parent_buf->b_error = EIO; 1030 md_kstat_done(ui, parent_buf, 0); 1031 md_unit_readerexit(ui); 1032 md_biodone(parent_buf); 1033 kmem_cache_free(sp_parent_cache, ps); 1034 return; 1035 } 1036 1037 child_buf = md_bioclone(parent_buf, current_offset, 1038 child_buf->b_bcount, child_buf->b_edev, 1039 child_buf->b_blkno, sp_done, child_buf, 1040 KM_NOSLEEP); 1041 /* calculate new offset, counts, etc... */ 1042 current_offset += child_buf->b_bcount; 1043 current_count -= child_buf->b_bcount; 1044 current_blkno += (sp_ext_offset_t)(btodb(child_buf->b_bcount)); 1045 1046 if (more) { 1047 mutex_enter(&ps->ps_mx); 1048 ps->ps_frags++; 1049 mutex_exit(&ps->ps_mx); 1050 } 1051 1052 md_call_strategy(child_buf, strat_flag, private); 1053 } while (more); 1054 1055 if (!(flag & MD_STR_NOTTOP) && (panicstr != NULL)) { 1056 while (!(ps->ps_flags & MD_SPPS_DONE)) { 1057 md_daemon(1, &md_done_daemon); 1058 } 1059 kmem_cache_free(sp_parent_cache, ps); 1060 } 1061 } 1062 1063 /* 1064 * FUNCTION: sp_directed_read() 1065 * INPUT: mnum - minor number 1066 * vdr - vol_directed_rd_t from user 1067 * mode - access mode for copying data out. 1068 * OUTPUT: none. 1069 * RETURNS: 0 - success 1070 * Exxxxx - failure error-code 1071 * PURPOSE: Construct the necessary sub-device i/o requests to perform the 1072 * directed read as requested by the user. This is essentially the 1073 * same as md_sp_strategy() with the exception being that the 1074 * underlying 'md_call_strategy' is replaced with an ioctl call. 1075 */ 1076 int 1077 sp_directed_read(minor_t mnum, vol_directed_rd_t *vdr, int mode) 1078 { 1079 md_spps_t *ps; 1080 md_spcs_t *cs; 1081 int more; 1082 mp_unit_t *un; 1083 mdi_unit_t *ui; 1084 size_t current_count; 1085 off_t current_offset; 1086 sp_ext_offset_t current_blkno; 1087 buf_t *child_buf, *parent_buf; 1088 void *kbuffer; 1089 vol_directed_rd_t cvdr; 1090 caddr_t userbuf; 1091 offset_t useroff; 1092 int ret = 0; 1093 1094 ui = MDI_UNIT(mnum); 1095 1096 md_kstat_waitq_enter(ui); 1097 1098 bzero(&cvdr, sizeof (cvdr)); 1099 1100 un = (mp_unit_t *)md_unit_readerlock(ui); 1101 1102 /* 1103 * Construct a parent_buf header which reflects the user-supplied 1104 * request. 1105 */ 1106 1107 kbuffer = kmem_alloc(vdr->vdr_nbytes, KM_NOSLEEP); 1108 if (kbuffer == NULL) { 1109 vdr->vdr_flags |= DKV_DMR_ERROR; 1110 md_unit_readerexit(ui); 1111 return (ENOMEM); 1112 } 1113 1114 parent_buf = getrbuf(KM_NOSLEEP); 1115 if (parent_buf == NULL) { 1116 vdr->vdr_flags |= DKV_DMR_ERROR; 1117 md_unit_readerexit(ui); 1118 kmem_free(kbuffer, vdr->vdr_nbytes); 1119 return (ENOMEM); 1120 } 1121 parent_buf->b_un.b_addr = kbuffer; 1122 parent_buf->b_flags = B_READ; 1123 parent_buf->b_bcount = vdr->vdr_nbytes; 1124 parent_buf->b_lblkno = lbtodb(vdr->vdr_offset); 1125 parent_buf->b_edev = un->un_dev; 1126 1127 1128 ps = kmem_cache_alloc(sp_parent_cache, MD_ALLOCFLAGS); 1129 sp_parent_init(ps); 1130 1131 /* 1132 * Save essential information from the original buffhdr 1133 * in the parent. 1134 */ 1135 ps->ps_un = un; 1136 ps->ps_ui = ui; 1137 ps->ps_bp = parent_buf; 1138 ps->ps_addr = parent_buf->b_un.b_addr; 1139 1140 current_count = parent_buf->b_bcount; 1141 current_blkno = (sp_ext_offset_t)parent_buf->b_lblkno; 1142 current_offset = 0; 1143 1144 ps->ps_frags++; 1145 vdr->vdr_bytesread = 0; 1146 1147 /* 1148 * this loop does the main work of an I/O. we allocate a 1149 * a child save for each buf, do the logical to physical 1150 * mapping, decide if we need to frag the I/O, clone the 1151 * new I/O to pass down the stack. repeat until we've 1152 * taken care of the entire buf that was passed to us. 1153 */ 1154 do { 1155 cs = kmem_cache_alloc(sp_child_cache, MD_ALLOCFLAGS); 1156 sp_child_init(cs); 1157 child_buf = &cs->cs_buf; 1158 cs->cs_ps = ps; 1159 1160 more = sp_mapbuf(un, current_blkno, current_count, child_buf); 1161 if (more == -1) { 1162 ret = EIO; 1163 vdr->vdr_flags |= DKV_DMR_SHORT; 1164 kmem_cache_free(sp_child_cache, cs); 1165 goto err_out; 1166 } 1167 1168 cvdr.vdr_flags = vdr->vdr_flags; 1169 cvdr.vdr_side = vdr->vdr_side; 1170 cvdr.vdr_nbytes = child_buf->b_bcount; 1171 cvdr.vdr_offset = ldbtob(child_buf->b_lblkno); 1172 /* Work out where we are in the allocated buffer */ 1173 useroff = (offset_t)(uintptr_t)kbuffer; 1174 useroff = useroff + (offset_t)current_offset; 1175 cvdr.vdr_data = (void *)(uintptr_t)useroff; 1176 child_buf = md_bioclone(parent_buf, current_offset, 1177 child_buf->b_bcount, child_buf->b_edev, 1178 child_buf->b_blkno, NULL, 1179 child_buf, KM_NOSLEEP); 1180 /* calculate new offset, counts, etc... */ 1181 current_offset += child_buf->b_bcount; 1182 current_count -= child_buf->b_bcount; 1183 current_blkno += (sp_ext_offset_t)(btodb(child_buf->b_bcount)); 1184 1185 if (more) { 1186 mutex_enter(&ps->ps_mx); 1187 ps->ps_frags++; 1188 mutex_exit(&ps->ps_mx); 1189 } 1190 1191 ret = md_call_ioctl(child_buf->b_edev, DKIOCDMR, &cvdr, 1192 (mode | FKIOCTL), NULL); 1193 1194 /* 1195 * Free the child structure as we've finished with it. 1196 * Normally this would be done by sp_done() but we're just 1197 * using md_bioclone() to segment the transfer and we never 1198 * issue a strategy request so the iodone will not be called. 1199 */ 1200 kmem_cache_free(sp_child_cache, cs); 1201 if (ret == 0) { 1202 /* copyout the returned data to vdr_data + offset */ 1203 userbuf = (caddr_t)kbuffer; 1204 userbuf += (caddr_t)(cvdr.vdr_data) - (caddr_t)kbuffer; 1205 if (ddi_copyout(userbuf, vdr->vdr_data, 1206 cvdr.vdr_bytesread, mode)) { 1207 ret = EFAULT; 1208 goto err_out; 1209 } 1210 vdr->vdr_bytesread += cvdr.vdr_bytesread; 1211 } else { 1212 goto err_out; 1213 } 1214 } while (more); 1215 1216 /* 1217 * Update the user-supplied vol_directed_rd_t structure with the 1218 * contents of the last issued child request. 1219 */ 1220 vdr->vdr_flags = cvdr.vdr_flags; 1221 vdr->vdr_side = cvdr.vdr_side; 1222 bcopy(cvdr.vdr_side_name, vdr->vdr_side_name, VOL_SIDENAME); 1223 1224 err_out: 1225 if (ret != 0) { 1226 vdr->vdr_flags |= DKV_DMR_ERROR; 1227 } 1228 if (vdr->vdr_bytesread != vdr->vdr_nbytes) { 1229 vdr->vdr_flags |= DKV_DMR_SHORT; 1230 } 1231 kmem_cache_free(sp_parent_cache, ps); 1232 kmem_free(kbuffer, vdr->vdr_nbytes); 1233 freerbuf(parent_buf); 1234 md_unit_readerexit(ui); 1235 return (ret); 1236 } 1237 1238 /* 1239 * FUNCTION: sp_snarf() 1240 * INPUT: cmd - snarf cmd. 1241 * setno - set number. 1242 * OUTPUT: none. 1243 * RETURNS: 1 - soft partitions were snarfed. 1244 * 0 - no soft partitions were snarfed. 1245 * PURPOSE: Snarf soft partition metadb records into their in-core 1246 * structures. This routine is called at "snarf time" when 1247 * md loads and gets all metadevices records into memory. 1248 * The basic algorithm is simply to walk the soft partition 1249 * records in the metadb and call the soft partitioning 1250 * build_incore routine to set up the in-core structures. 1251 */ 1252 static int 1253 sp_snarf(md_snarfcmd_t cmd, set_t setno) 1254 { 1255 mp_unit_t *un; 1256 mddb_recid_t recid; 1257 int gotsomething; 1258 int all_sp_gotten; 1259 mddb_type_t rec_type; 1260 mddb_de_ic_t *dep; 1261 mddb_rb32_t *rbp; 1262 mp_unit_t *big_un; 1263 mp_unit32_od_t *small_un; 1264 size_t newreqsize; 1265 1266 1267 if (cmd == MD_SNARF_CLEANUP) 1268 return (0); 1269 1270 all_sp_gotten = 1; 1271 gotsomething = 0; 1272 1273 /* get the record type */ 1274 rec_type = (mddb_type_t)md_getshared_key(setno, 1275 sp_md_ops.md_driver.md_drivername); 1276 recid = mddb_makerecid(setno, 0); 1277 1278 /* 1279 * walk soft partition records in the metadb and call 1280 * sp_build_incore to build in-core structures. 1281 */ 1282 while ((recid = mddb_getnextrec(recid, rec_type, 0)) > 0) { 1283 /* if we've already gotten this record, go to the next one */ 1284 if (mddb_getrecprivate(recid) & MD_PRV_GOTIT) 1285 continue; 1286 1287 1288 dep = mddb_getrecdep(recid); 1289 dep->de_flags = MDDB_F_SOFTPART; 1290 rbp = dep->de_rb; 1291 1292 switch (rbp->rb_revision) { 1293 case MDDB_REV_RB: 1294 case MDDB_REV_RBFN: 1295 if ((rbp->rb_private & MD_PRV_CONVD) == 0) { 1296 /* 1297 * This means, we have an old and small record. 1298 * And this record hasn't already been converted 1299 * :-o before we create an incore metadevice 1300 * from this we have to convert it to a big 1301 * record. 1302 */ 1303 small_un = 1304 (mp_unit32_od_t *)mddb_getrecaddr(recid); 1305 newreqsize = sizeof (mp_unit_t) + 1306 ((small_un->un_numexts - 1) * 1307 sizeof (struct mp_ext)); 1308 big_un = (mp_unit_t *)kmem_zalloc(newreqsize, 1309 KM_SLEEP); 1310 softpart_convert((caddr_t)small_un, 1311 (caddr_t)big_un, SMALL_2_BIG); 1312 kmem_free(small_un, dep->de_reqsize); 1313 dep->de_rb_userdata = big_un; 1314 dep->de_reqsize = newreqsize; 1315 rbp->rb_private |= MD_PRV_CONVD; 1316 un = big_un; 1317 } else { 1318 /* Record has already been converted */ 1319 un = (mp_unit_t *)mddb_getrecaddr(recid); 1320 } 1321 un->c.un_revision &= ~MD_64BIT_META_DEV; 1322 break; 1323 case MDDB_REV_RB64: 1324 case MDDB_REV_RB64FN: 1325 /* Large device */ 1326 un = (mp_unit_t *)mddb_getrecaddr(recid); 1327 un->c.un_revision |= MD_64BIT_META_DEV; 1328 un->c.un_flag |= MD_EFILABEL; 1329 break; 1330 } 1331 NOTE_FN(rbp->rb_revision, un->c.un_revision); 1332 1333 /* 1334 * Create minor node for snarfed entry. 1335 */ 1336 (void) md_create_minor_node(MD_MIN2SET(MD_SID(un)), MD_SID(un)); 1337 1338 if (MD_UNIT(MD_SID(un)) != NULL) { 1339 /* unit is already in-core */ 1340 mddb_setrecprivate(recid, MD_PRV_PENDDEL); 1341 continue; 1342 } 1343 all_sp_gotten = 0; 1344 if (sp_build_incore((void *)un, 1) == 0) { 1345 mddb_setrecprivate(recid, MD_PRV_GOTIT); 1346 md_create_unit_incore(MD_SID(un), &sp_md_ops, 0); 1347 gotsomething = 1; 1348 } 1349 } 1350 1351 if (!all_sp_gotten) 1352 return (gotsomething); 1353 /* double-check records */ 1354 recid = mddb_makerecid(setno, 0); 1355 while ((recid = mddb_getnextrec(recid, rec_type, 0)) > 0) 1356 if (!(mddb_getrecprivate(recid) & MD_PRV_GOTIT)) 1357 mddb_setrecprivate(recid, MD_PRV_PENDDEL); 1358 1359 return (0); 1360 } 1361 1362 /* 1363 * FUNCTION: sp_halt() 1364 * INPUT: cmd - halt cmd. 1365 * setno - set number. 1366 * RETURNS: 0 - success. 1367 * 1 - err. 1368 * PURPOSE: Perform driver halt operations. As with stripe, we 1369 * support MD_HALT_CHECK and MD_HALT_DOIT. The first 1370 * does a check to see if halting can be done safely 1371 * (no open soft partitions), the second cleans up and 1372 * shuts down the driver. 1373 */ 1374 static int 1375 sp_halt(md_haltcmd_t cmd, set_t setno) 1376 { 1377 int i; 1378 mdi_unit_t *ui; 1379 minor_t mnum; 1380 1381 if (cmd == MD_HALT_CLOSE) 1382 return (0); 1383 1384 if (cmd == MD_HALT_OPEN) 1385 return (0); 1386 1387 if (cmd == MD_HALT_UNLOAD) 1388 return (0); 1389 1390 if (cmd == MD_HALT_CHECK) { 1391 for (i = 0; i < md_nunits; i++) { 1392 mnum = MD_MKMIN(setno, i); 1393 if ((ui = MDI_UNIT(mnum)) == NULL) 1394 continue; 1395 if (ui->ui_opsindex != sp_md_ops.md_selfindex) 1396 continue; 1397 if (md_unit_isopen(ui)) 1398 return (1); 1399 } 1400 return (0); 1401 } 1402 1403 if (cmd != MD_HALT_DOIT) 1404 return (1); 1405 1406 for (i = 0; i < md_nunits; i++) { 1407 mnum = MD_MKMIN(setno, i); 1408 if ((ui = MDI_UNIT(mnum)) == NULL) 1409 continue; 1410 if (ui->ui_opsindex != sp_md_ops.md_selfindex) 1411 continue; 1412 reset_sp((mp_unit_t *)MD_UNIT(mnum), mnum, 0); 1413 } 1414 1415 return (0); 1416 } 1417 1418 /* 1419 * FUNCTION: sp_open_dev() 1420 * INPUT: un - unit structure. 1421 * oflags - open flags. 1422 * OUTPUT: none. 1423 * RETURNS: 0 - success. 1424 * non-zero - err. 1425 * PURPOSE: open underlying device via md_layered_open. 1426 */ 1427 static int 1428 sp_open_dev(mp_unit_t *un, int oflags) 1429 { 1430 minor_t mnum = MD_SID(un); 1431 int err; 1432 md_dev64_t tmpdev; 1433 set_t setno = MD_MIN2SET(MD_SID(un)); 1434 side_t side = mddb_getsidenum(setno); 1435 1436 tmpdev = un->un_dev; 1437 /* 1438 * Do the open by device id if underlying is regular 1439 */ 1440 if ((md_getmajor(tmpdev) != md_major) && 1441 md_devid_found(setno, side, un->un_key) == 1) { 1442 tmpdev = md_resolve_bydevid(mnum, tmpdev, un->un_key); 1443 } 1444 err = md_layered_open(mnum, &tmpdev, oflags); 1445 un->un_dev = tmpdev; 1446 1447 if (err) 1448 return (ENXIO); 1449 1450 return (0); 1451 } 1452 1453 /* 1454 * FUNCTION: sp_open() 1455 * INPUT: dev - device to open. 1456 * flag - pass-through flag. 1457 * otyp - pass-through open type. 1458 * cred_p - credentials. 1459 * md_oflags - open flags. 1460 * OUTPUT: none. 1461 * RETURNS: 0 - success. 1462 * non-zero - err. 1463 * PURPOSE: open a soft partition. 1464 */ 1465 /* ARGSUSED */ 1466 static int 1467 sp_open( 1468 dev_t *dev, 1469 int flag, 1470 int otyp, 1471 cred_t *cred_p, 1472 int md_oflags 1473 ) 1474 { 1475 minor_t mnum = getminor(*dev); 1476 mdi_unit_t *ui = MDI_UNIT(mnum); 1477 mp_unit_t *un; 1478 int err = 0; 1479 set_t setno; 1480 1481 /* 1482 * When doing an open of a multi owner metadevice, check to see if this 1483 * node is a starting node and if a reconfig cycle is underway. 1484 * If so, the system isn't sufficiently set up enough to handle the 1485 * open (which involves I/O during sp_validate), so fail with ENXIO. 1486 */ 1487 setno = MD_MIN2SET(mnum); 1488 if ((md_set[setno].s_status & (MD_SET_MNSET | MD_SET_MN_START_RC)) == 1489 (MD_SET_MNSET | MD_SET_MN_START_RC)) { 1490 return (ENXIO); 1491 } 1492 1493 /* grab necessary locks */ 1494 un = (mp_unit_t *)md_unit_openclose_enter(ui); 1495 setno = MD_UN2SET(un); 1496 1497 /* open underlying device, if necessary */ 1498 if (! md_unit_isopen(ui) || (md_oflags & MD_OFLG_PROBEDEV)) { 1499 if ((err = sp_open_dev(un, md_oflags)) != 0) 1500 goto out; 1501 1502 if (MD_MNSET_SETNO(setno)) { 1503 /* For probe, don't incur the overhead of validate */ 1504 if (!(md_oflags & MD_OFLG_PROBEDEV)) { 1505 /* 1506 * Don't call sp_validate while 1507 * unit_openclose lock is held. So, actually 1508 * open the device, drop openclose lock, 1509 * call sp_validate, reacquire openclose lock, 1510 * and close the device. If sp_validate 1511 * succeeds, then device will be re-opened. 1512 */ 1513 if ((err = md_unit_incopen(mnum, flag, 1514 otyp)) != 0) 1515 goto out; 1516 1517 mutex_enter(&ui->ui_mx); 1518 ui->ui_lock |= MD_UL_OPENINPROGRESS; 1519 mutex_exit(&ui->ui_mx); 1520 md_unit_openclose_exit(ui); 1521 if (otyp != OTYP_LYR) 1522 rw_exit(&md_unit_array_rw.lock); 1523 1524 err = sp_validate(un); 1525 1526 if (otyp != OTYP_LYR) 1527 rw_enter(&md_unit_array_rw.lock, 1528 RW_READER); 1529 (void) md_unit_openclose_enter(ui); 1530 (void) md_unit_decopen(mnum, otyp); 1531 mutex_enter(&ui->ui_mx); 1532 ui->ui_lock &= ~MD_UL_OPENINPROGRESS; 1533 cv_broadcast(&ui->ui_cv); 1534 mutex_exit(&ui->ui_mx); 1535 /* 1536 * Should be in the same state as before 1537 * the sp_validate. 1538 */ 1539 if (err != 0) { 1540 /* close the device opened above */ 1541 md_layered_close(un->un_dev, md_oflags); 1542 err = EIO; 1543 goto out; 1544 } 1545 } 1546 /* 1547 * As we're a multi-owner metadevice we need to ensure 1548 * that all nodes have the same idea of the status. 1549 * sp_validate() will mark the device as errored (if 1550 * it cannot read the watermark) or ok (if it was 1551 * previously errored but the watermark is now valid). 1552 * This code-path is only entered on the non-probe open 1553 * so we will maintain the errored state during a probe 1554 * call. This means the sys-admin must metarecover -m 1555 * to reset the soft-partition error. 1556 */ 1557 } else { 1558 /* For probe, don't incur the overhead of validate */ 1559 if (!(md_oflags & MD_OFLG_PROBEDEV) && 1560 (err = sp_validate(un)) != 0) { 1561 /* close the device opened above */ 1562 md_layered_close(un->un_dev, md_oflags); 1563 err = EIO; 1564 goto out; 1565 } else { 1566 /* 1567 * we succeeded in validating the on disk 1568 * format versus the in core, so reset the 1569 * status if it's in error 1570 */ 1571 if (un->un_status == MD_SP_ERR) { 1572 un->un_status = MD_SP_OK; 1573 } 1574 } 1575 } 1576 } 1577 1578 /* count open */ 1579 if ((err = md_unit_incopen(mnum, flag, otyp)) != 0) 1580 goto out; 1581 1582 out: 1583 md_unit_openclose_exit(ui); 1584 return (err); 1585 } 1586 1587 /* 1588 * FUNCTION: sp_close() 1589 * INPUT: dev - device to close. 1590 * flag - pass-through flag. 1591 * otyp - pass-through type. 1592 * cred_p - credentials. 1593 * md_cflags - close flags. 1594 * OUTPUT: none. 1595 * RETURNS: 0 - success. 1596 * non-zero - err. 1597 * PURPOSE: close a soft paritition. 1598 */ 1599 /* ARGSUSED */ 1600 static int 1601 sp_close( 1602 dev_t dev, 1603 int flag, 1604 int otyp, 1605 cred_t *cred_p, 1606 int md_cflags 1607 ) 1608 { 1609 minor_t mnum = getminor(dev); 1610 mdi_unit_t *ui = MDI_UNIT(mnum); 1611 mp_unit_t *un; 1612 int err = 0; 1613 1614 /* grab necessary locks */ 1615 un = (mp_unit_t *)md_unit_openclose_enter(ui); 1616 1617 /* count closed */ 1618 if ((err = md_unit_decopen(mnum, otyp)) != 0) 1619 goto out; 1620 1621 /* close devices, if necessary */ 1622 if (! md_unit_isopen(ui) || (md_cflags & MD_OFLG_PROBEDEV)) { 1623 md_layered_close(un->un_dev, md_cflags); 1624 } 1625 1626 /* 1627 * If a MN set and transient capabilities (eg ABR/DMR) are set, 1628 * clear these capabilities if this is the last close in 1629 * the cluster 1630 */ 1631 if (MD_MNSET_SETNO(MD_UN2SET(un)) && 1632 (ui->ui_tstate & MD_ABR_CAP)) { 1633 md_unit_openclose_exit(ui); 1634 mdmn_clear_all_capabilities(mnum); 1635 return (0); 1636 } 1637 /* unlock, return success */ 1638 out: 1639 md_unit_openclose_exit(ui); 1640 return (err); 1641 } 1642 1643 1644 /* used in sp_dump routine */ 1645 static struct buf dumpbuf; 1646 1647 /* 1648 * FUNCTION: sp_dump() 1649 * INPUT: dev - device to dump to. 1650 * addr - address to dump. 1651 * blkno - blkno on device. 1652 * nblk - number of blocks to dump. 1653 * OUTPUT: none. 1654 * RETURNS: result from bdev_dump. 1655 * PURPOSE: This routine dumps memory to the disk. It assumes that 1656 * the memory has already been mapped into mainbus space. 1657 * It is called at disk interrupt priority when the system 1658 * is in trouble. 1659 * NOTE: this function is defined using 32-bit arguments, 1660 * but soft partitioning is internally 64-bit. Arguments 1661 * are casted where appropriate. 1662 */ 1663 static int 1664 sp_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk) 1665 { 1666 mp_unit_t *un; 1667 buf_t *bp; 1668 sp_ext_length_t nb; 1669 daddr_t mapblk; 1670 int result; 1671 int more; 1672 int saveresult = 0; 1673 1674 /* 1675 * Don't need to grab the unit lock. 1676 * Cause nothing else is supposed to be happenning. 1677 * Also dump is not supposed to sleep. 1678 */ 1679 un = (mp_unit_t *)MD_UNIT(getminor(dev)); 1680 1681 if ((diskaddr_t)blkno >= un->c.un_total_blocks) 1682 return (EINVAL); 1683 1684 if (((diskaddr_t)blkno + nblk) > un->c.un_total_blocks) 1685 return (EINVAL); 1686 1687 bp = &dumpbuf; 1688 nb = (sp_ext_length_t)dbtob(nblk); 1689 do { 1690 bzero((caddr_t)bp, sizeof (*bp)); 1691 more = sp_mapbuf(un, (sp_ext_offset_t)blkno, nb, bp); 1692 nblk = (int)(btodb(bp->b_bcount)); 1693 mapblk = bp->b_blkno; 1694 result = bdev_dump(bp->b_edev, addr, mapblk, nblk); 1695 if (result) 1696 saveresult = result; 1697 1698 nb -= bp->b_bcount; 1699 addr += bp->b_bcount; 1700 blkno += nblk; 1701 } while (more); 1702 1703 return (saveresult); 1704 } 1705 1706 static int 1707 sp_imp_set( 1708 set_t setno 1709 ) 1710 { 1711 mddb_recid_t recid; 1712 int gotsomething; 1713 mddb_type_t rec_type; 1714 mddb_de_ic_t *dep; 1715 mddb_rb32_t *rbp; 1716 mp_unit_t *un64; 1717 mp_unit32_od_t *un32; 1718 md_dev64_t self_devt; 1719 minor_t *self_id; /* minor needs to be updated */ 1720 md_parent_t *parent_id; /* parent needs to be updated */ 1721 mddb_recid_t *record_id; /* record id needs to be updated */ 1722 1723 gotsomething = 0; 1724 1725 rec_type = (mddb_type_t)md_getshared_key(setno, 1726 sp_md_ops.md_driver.md_drivername); 1727 recid = mddb_makerecid(setno, 0); 1728 1729 while ((recid = mddb_getnextrec(recid, rec_type, 0)) > 0) { 1730 if (mddb_getrecprivate(recid) & MD_PRV_GOTIT) 1731 continue; 1732 1733 dep = mddb_getrecdep(recid); 1734 rbp = dep->de_rb; 1735 1736 switch (rbp->rb_revision) { 1737 case MDDB_REV_RB: 1738 case MDDB_REV_RBFN: 1739 /* 1740 * Small device 1741 */ 1742 un32 = (mp_unit32_od_t *)mddb_getrecaddr(recid); 1743 self_id = &(un32->c.un_self_id); 1744 parent_id = &(un32->c.un_parent); 1745 record_id = &(un32->c.un_record_id); 1746 1747 if (!md_update_minor(setno, mddb_getsidenum 1748 (setno), un32->un_key)) 1749 goto out; 1750 break; 1751 1752 case MDDB_REV_RB64: 1753 case MDDB_REV_RB64FN: 1754 un64 = (mp_unit_t *)mddb_getrecaddr(recid); 1755 self_id = &(un64->c.un_self_id); 1756 parent_id = &(un64->c.un_parent); 1757 record_id = &(un64->c.un_record_id); 1758 1759 if (!md_update_minor(setno, mddb_getsidenum 1760 (setno), un64->un_key)) 1761 goto out; 1762 break; 1763 } 1764 1765 /* 1766 * If this is a top level and a friendly name metadevice, 1767 * update its minor in the namespace. 1768 */ 1769 if ((*parent_id == MD_NO_PARENT) && 1770 ((rbp->rb_revision == MDDB_REV_RBFN) || 1771 (rbp->rb_revision == MDDB_REV_RB64FN))) { 1772 1773 self_devt = md_makedevice(md_major, *self_id); 1774 if (!md_update_top_device_minor(setno, 1775 mddb_getsidenum(setno), self_devt)) 1776 goto out; 1777 } 1778 1779 /* 1780 * Update unit with the imported setno 1781 * 1782 */ 1783 mddb_setrecprivate(recid, MD_PRV_GOTIT); 1784 1785 *self_id = MD_MKMIN(setno, MD_MIN2UNIT(*self_id)); 1786 if (*parent_id != MD_NO_PARENT) 1787 *parent_id = MD_MKMIN(setno, MD_MIN2UNIT(*parent_id)); 1788 *record_id = MAKERECID(setno, DBID(*record_id)); 1789 1790 gotsomething = 1; 1791 } 1792 1793 out: 1794 return (gotsomething); 1795 } 1796 1797 static md_named_services_t sp_named_services[] = { 1798 {NULL, 0} 1799 }; 1800 1801 md_ops_t sp_md_ops = { 1802 sp_open, /* open */ 1803 sp_close, /* close */ 1804 md_sp_strategy, /* strategy */ 1805 NULL, /* print */ 1806 sp_dump, /* dump */ 1807 NULL, /* read */ 1808 NULL, /* write */ 1809 md_sp_ioctl, /* ioctl, */ 1810 sp_snarf, /* snarf */ 1811 sp_halt, /* halt */ 1812 NULL, /* aread */ 1813 NULL, /* awrite */ 1814 sp_imp_set, /* import set */ 1815 sp_named_services 1816 }; 1817 1818 static void 1819 init_init() 1820 { 1821 sp_parent_cache = kmem_cache_create("md_softpart_parent", 1822 sizeof (md_spps_t), 0, sp_parent_constructor, 1823 sp_parent_destructor, sp_run_queue, NULL, NULL, 0); 1824 sp_child_cache = kmem_cache_create("md_softpart_child", 1825 sizeof (md_spcs_t) - sizeof (buf_t) + biosize(), 0, 1826 sp_child_constructor, sp_child_destructor, sp_run_queue, 1827 NULL, NULL, 0); 1828 } 1829 1830 static void 1831 fini_uninit() 1832 { 1833 kmem_cache_destroy(sp_parent_cache); 1834 kmem_cache_destroy(sp_child_cache); 1835 sp_parent_cache = sp_child_cache = NULL; 1836 } 1837 1838 /* define the module linkage */ 1839 MD_PLUGIN_MISC_MODULE("soft partition module %I%", init_init(), fini_uninit()) 1840