xref: /titanic_41/usr/src/uts/common/io/lvm/stripe/stripe.c (revision 68ae785055658995f54cb50bc0a4e120c882f11b)
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 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/conf.h>
31 #include <sys/file.h>
32 #include <sys/user.h>
33 #include <sys/uio.h>
34 #include <sys/t_lock.h>
35 #include <sys/buf.h>
36 #include <sys/dkio.h>
37 #include <sys/vtoc.h>
38 #include <sys/kmem.h>
39 #include <vm/page.h>
40 #include <sys/cmn_err.h>
41 #include <sys/sysmacros.h>
42 #include <sys/types.h>
43 #include <sys/mkdev.h>
44 #include <sys/stat.h>
45 #include <sys/open.h>
46 #include <sys/lvm/mdio.h>
47 #include <sys/lvm/mdvar.h>
48 #include <sys/lvm/md_stripe.h>
49 #include <sys/lvm/md_convert.h>
50 #include <sys/lvm/md_notify.h>
51 #include <sys/modctl.h>
52 #include <sys/ddi.h>
53 #include <sys/sunddi.h>
54 #include <sys/debug.h>
55 #include <sys/sysevent/eventdefs.h>
56 #include <sys/sysevent/svm.h>
57 
58 md_ops_t		stripe_md_ops;
59 #ifndef	lint
60 char			_depends_on[] = "drv/md";
61 md_ops_t		*md_interface_ops = &stripe_md_ops;
62 #endif
63 
64 extern unit_t		md_nunits;
65 extern set_t		md_nsets;
66 extern md_set_t		md_set[];
67 
68 extern kmutex_t		md_mx;
69 extern kcondvar_t	md_cv;
70 
71 extern int		md_status;
72 extern major_t		md_major;
73 extern mdq_anchor_t	md_done_daemon;
74 
75 static int		md_stripe_mcs_buf_off;
76 static kmem_cache_t	*stripe_parent_cache = NULL;
77 static kmem_cache_t	*stripe_child_cache = NULL;
78 
79 /*ARGSUSED1*/
80 static int
81 stripe_parent_constructor(void *p, void *d1, int d2)
82 {
83 	mutex_init(&((md_sps_t *)p)->ps_mx,
84 	    NULL, MUTEX_DEFAULT, NULL);
85 	return (0);
86 }
87 
88 static void
89 stripe_parent_init(void *ps)
90 {
91 	bzero(ps, offsetof(md_sps_t, ps_mx));
92 }
93 
94 /*ARGSUSED1*/
95 static void
96 stripe_parent_destructor(void *p, void *d)
97 {
98 	mutex_destroy(&((md_sps_t *)p)->ps_mx);
99 }
100 
101 /*ARGSUSED1*/
102 static int
103 stripe_child_constructor(void *p, void *d1, int d2)
104 {
105 	bioinit(&((md_scs_t *)p)->cs_buf);
106 	return (0);
107 }
108 
109 static void
110 stripe_child_init(md_scs_t *cs)
111 {
112 	cs->cs_mdunit = 0;
113 	cs->cs_ps = NULL;
114 	cs->cs_comp = NULL;
115 	md_bioreset(&cs->cs_buf);
116 }
117 
118 /*ARGSUSED1*/
119 static void
120 stripe_child_destructor(void *p, void *d)
121 {
122 	biofini(&((md_scs_t *)p)->cs_buf);
123 }
124 
125 /*ARGSUSED*/
126 static void
127 stripe_run_queue(void *d)
128 {
129 	if (!(md_status & MD_GBL_DAEMONS_LIVE))
130 		md_daemon(1, &md_done_daemon);
131 }
132 
133 static void
134 stripe_close_all_devs(ms_unit_t *un, int md_cflags)
135 {
136 	int		row;
137 	int		i;
138 	int		c;
139 	struct ms_comp	*mdcomp;
140 
141 	mdcomp = (struct ms_comp *)((void *)&((char *)un)[un->un_ocomp]);
142 	for (row = 0; row < un->un_nrows; row++) {
143 		struct ms_row *mdr = &un->un_row[row];
144 		for (i = 0, c = mdr->un_icomp; i < mdr->un_ncomp; i++) {
145 			struct ms_comp	*mdc;
146 			mdc = &mdcomp[c++];
147 			if (md_cflags & MD_OFLG_PROBEDEV) {
148 
149 			/*
150 			 * It is possible that the md_layered_open
151 			 * failed because the stripe unit structure
152 			 * contained a NODEV.  In such a case since
153 			 * there is nothing to open, there is nothing
154 			 * to close.
155 			 */
156 				if (mdc->un_dev == NODEV64)
157 					continue;
158 			}
159 			if ((md_cflags & MD_OFLG_PROBEDEV) &&
160 			    (mdc->un_mirror.ms_flags & MDM_S_PROBEOPEN)) {
161 				md_layered_close(mdc->un_dev,
162 				    md_cflags);
163 				mdc->un_mirror.ms_flags &=
164 						~MDM_S_PROBEOPEN;
165 			} else if (mdc->un_mirror.ms_flags & MDM_S_ISOPEN) {
166 				md_layered_close(mdc->un_dev, md_cflags);
167 				mdc->un_mirror.ms_flags &= ~MDM_S_ISOPEN;
168 			}
169 		}
170 	}
171 }
172 
173 static int
174 stripe_open_all_devs(ms_unit_t *un, int md_oflags)
175 {
176 	minor_t		mnum = MD_SID(un);
177 	int		row;
178 	int		i;
179 	int		c;
180 	struct ms_comp	*mdcomp;
181 	int		err;
182 	int		cont_on_errors = (md_oflags & MD_OFLG_CONT_ERRS);
183 	int		probe_err_cnt = 0;
184 	int		total_comp_cnt = 0;
185 	set_t		setno = MD_MIN2SET(MD_SID(un));
186 	side_t		side = mddb_getsidenum(setno);
187 	mdkey_t		key;
188 
189 	mdcomp = (struct ms_comp *)((void *)&((char *)un)[un->un_ocomp]);
190 
191 	/*
192 	 * For a probe call, if any component of a stripe or a concat
193 	 * can be opened, it is considered to be a success. The total number
194 	 * of components in a stripe are computed prior to starting a probe.
195 	 * This number is then compared against the number of components
196 	 * that could be be successfully opened. If none of the components
197 	 * in a stripe can be opened, only then an ENXIO is returned for a
198 	 * probe type open.
199 	 */
200 
201 	for (row = 0; row < un->un_nrows; row++) {
202 		struct ms_row *mdr = &un->un_row[row];
203 
204 		if (md_oflags & MD_OFLG_PROBEDEV)
205 			total_comp_cnt += mdr->un_ncomp;
206 
207 		for (i = 0, c = mdr->un_icomp; i < mdr->un_ncomp; i++) {
208 			struct ms_comp	*mdc;
209 			md_dev64_t tmpdev;
210 
211 			mdc = &mdcomp[c++];
212 			tmpdev = mdc->un_dev;
213 			/*
214 			 * Do the open by device id
215 			 * Check if this comp is hotspared and
216 			 * if it is then use the key for hotspare.
217 			 * MN disksets don't use devids, so we better don't use
218 			 * md_devid_found/md_resolve_bydevid there. Rather do,
219 			 * what's done in stripe_build_incore()
220 			 */
221 			if (MD_MNSET_SETNO(setno)) {
222 				if (mdc->un_mirror.ms_hs_id != 0) {
223 					(void) md_hot_spare_ifc(HS_MKDEV, 0, 0,
224 					    0, &mdc->un_mirror.ms_hs_id, NULL,
225 					    &tmpdev, NULL);
226 				}
227 			} else {
228 				key = mdc->un_mirror.ms_hs_id ?
229 				    mdc->un_mirror.ms_hs_key : mdc->un_key;
230 				if ((md_getmajor(tmpdev) != md_major) &&
231 				    md_devid_found(setno, side, key) == 1) {
232 					tmpdev = md_resolve_bydevid(mnum,
233 					    tmpdev, key);
234 				}
235 			}
236 
237 			/*
238 			 * For a submirror, we only want to open those devices
239 			 * that are not errored. If the device is errored then
240 			 * then there is no reason to open it and leaving it
241 			 * closed allows the RCM/DR code to work so that the
242 			 * errored device can be replaced.
243 			 */
244 			if ((md_oflags & MD_OFLG_PROBEDEV) ||
245 			    ! (mdc->un_mirror.ms_state & CS_ERRED)) {
246 
247 				err = md_layered_open(mnum, &tmpdev, md_oflags);
248 			} else {
249 				err = ENXIO;
250 			}
251 
252 			/*
253 			 * Only set the un_dev if the tmpdev != NODEV64. If
254 			 * it is NODEV64 then the md_layered_open() will have
255 			 * failed in some manner.
256 			 */
257 			if (tmpdev != NODEV64)
258 				mdc->un_dev = tmpdev;
259 
260 			if (err) {
261 				if (!cont_on_errors) {
262 					stripe_close_all_devs(un, md_oflags);
263 					return (ENXIO);
264 				}
265 
266 				if (md_oflags & MD_OFLG_PROBEDEV)
267 					probe_err_cnt++;
268 			} else {
269 				if (md_oflags & MD_OFLG_PROBEDEV) {
270 					mdc->un_mirror.ms_flags |=
271 						MDM_S_PROBEOPEN;
272 				} else
273 					mdc->un_mirror.ms_flags |= MDM_S_ISOPEN;
274 			}
275 		}
276 	}
277 
278 	/* If every component in a stripe could not be opened fail */
279 	if ((md_oflags & MD_OFLG_PROBEDEV) &&
280 	    (probe_err_cnt == total_comp_cnt))
281 		return (ENXIO);
282 	else
283 		return (0);
284 }
285 
286 int
287 stripe_build_incore(void *p, int snarfing)
288 {
289 	ms_unit_t *un = (ms_unit_t *)p;
290 	struct ms_comp	*mdcomp;
291 	minor_t		mnum;
292 	int		row;
293 	int		i;
294 	int		c;
295 	int		ncomps;
296 
297 	mnum = MD_SID(un);
298 
299 	if (MD_UNIT(mnum) != NULL)
300 		return (0);
301 
302 	MD_STATUS(un) = 0;
303 
304 	/*
305 	 * Reset all the is_open flags, these are probably set
306 	 * cause they just came out of the database.
307 	 */
308 	mdcomp = (struct ms_comp *)((void *)&((char *)un)[un->un_ocomp]);
309 
310 	ncomps = 0;
311 	for (row = 0; row < un->un_nrows; row++) {
312 		struct ms_row *mdr = &un->un_row[row];
313 		ncomps += mdr->un_ncomp;
314 	}
315 
316 	for (row = 0; row < un->un_nrows; row++) {
317 		struct ms_row *mdr = &un->un_row[row];
318 		for (i = 0, c = mdr->un_icomp; i < mdr->un_ncomp; i++) {
319 			struct ms_comp		*mdc;
320 			set_t			setno;
321 			md_dev64_t		tmpdev;
322 
323 			mdc = &mdcomp[c++];
324 			mdc->un_mirror.ms_flags &=
325 			    ~(MDM_S_ISOPEN | MDM_S_IOERR | MDM_S_RS_TRIED);
326 
327 			if (!snarfing)
328 				continue;
329 
330 			setno = MD_MIN2SET(mnum);
331 
332 			tmpdev = md_getdevnum(setno, mddb_getsidenum(setno),
333 			    mdc->un_key, MD_NOTRUST_DEVT);
334 			mdc->un_dev = tmpdev;
335 			/*
336 			 * Check for hotspares. If the hotspares haven't been
337 			 * snarfed yet, stripe_open_all_devs() will do the
338 			 * remapping of the dev's later.
339 			 */
340 			if (mdc->un_mirror.ms_hs_id != 0) {
341 				mdc->un_mirror.ms_orig_dev = mdc->un_dev;
342 				(void) md_hot_spare_ifc(HS_MKDEV, 0, 0,
343 				    0, &mdc->un_mirror.ms_hs_id, NULL,
344 				    &tmpdev, NULL);
345 				mdc->un_dev = tmpdev;
346 			}
347 		}
348 	}
349 
350 	MD_UNIT(mnum) = un;
351 	return (0);
352 }
353 
354 void
355 reset_stripe(ms_unit_t *un, minor_t mnum, int removing)
356 {
357 	ms_comp_t	*mdcomp;
358 	struct ms_row	*mdr;
359 	int		i, c;
360 	int		row;
361 	int		nsv;
362 	int		isv;
363 	sv_dev_t	*sv;
364 	mddb_recid_t	*recids;
365 	mddb_recid_t	vtoc_id;
366 	int		rid = 0;
367 
368 	md_destroy_unit_incore(mnum, &stripe_md_ops);
369 
370 	MD_UNIT(mnum) = NULL;
371 
372 	/*
373 	 * Attempt release of its minor node
374 	 */
375 	(void) md_remove_minor_node(mnum);
376 
377 	if (!removing)
378 		return;
379 
380 	nsv = 0;
381 	/* Count the number of devices */
382 	for (row = 0; row < un->un_nrows; row++) {
383 		mdr = &un->un_row[row];
384 		nsv += mdr->un_ncomp;
385 	}
386 	sv = (sv_dev_t *)kmem_alloc(sizeof (sv_dev_t) * nsv, KM_SLEEP);
387 
388 	/*
389 	 * allocate recids array.  since we may have to commit
390 	 * underlying soft partition records, we need an array
391 	 * of size: total number of components in stripe + 3
392 	 * (one for the stripe itself, one for the hotspare, one
393 	 * for the end marker).
394 	 */
395 	recids = kmem_alloc(sizeof (mddb_recid_t) * (nsv + 3), KM_SLEEP);
396 
397 	/*
398 	 * Save the md_dev64_t's and driver nm indexes.
399 	 * Because after the mddb_deleterec() we will
400 	 * not be able to access the unit structure.
401 	 *
402 	 * NOTE: Deleting the names before deleting the
403 	 *	 unit structure would cause problems if
404 	 *	 the machine crashed in between the two.
405 	 */
406 	isv = 0;
407 	mdcomp = (struct ms_comp *)((void *)&((char *)un)[un->un_ocomp]);
408 
409 	for (row = 0; row < un->un_nrows; row++) {
410 		mdr = &un->un_row[row];
411 		for (i = 0, c = mdr->un_icomp; i < mdr->un_ncomp; i++) {
412 			struct ms_comp	*mdc;
413 			md_dev64_t	child_dev;
414 			md_unit_t	*child_un;
415 
416 			mdc = &mdcomp[c++];
417 			if (mdc->un_mirror.ms_hs_id != 0) {
418 				mdkey_t		hs_key;
419 
420 				hs_key = mdc->un_mirror.ms_hs_key;
421 
422 				mdc->un_dev = mdc->un_mirror.ms_orig_dev;
423 				mdc->un_start_block =
424 				    mdc->un_mirror.ms_orig_blk;
425 				mdc->un_mirror.ms_hs_id = 0;
426 				mdc->un_mirror.ms_hs_key = 0;
427 				mdc->un_mirror.ms_orig_dev = 0;
428 				recids[0] = 0;
429 				recids[1] = 0;	/* recids[1] filled in below */
430 				recids[2] = 0;
431 				(void) md_hot_spare_ifc(HS_FREE, un->un_hsp_id,
432 				    0, 0, &recids[0], &hs_key, NULL, NULL);
433 				mddb_commitrecs_wrapper(recids);
434 			}
435 
436 			/*
437 			 * check if we've got metadevice below us and
438 			 * deparent it if we do.
439 			 * NOTE: currently soft partitions are the
440 			 * the only metadevices stripes can be
441 			 * built on top of.
442 			 */
443 			child_dev = mdc->un_dev;
444 			if (md_getmajor(child_dev) == md_major) {
445 				child_un = MD_UNIT(md_getminor(child_dev));
446 				md_reset_parent(child_dev);
447 				recids[rid++] = MD_RECID(child_un);
448 			}
449 
450 			sv[isv].setno = MD_MIN2SET(mnum);
451 			sv[isv++].key = mdc->un_key;
452 		}
453 	}
454 
455 	recids[rid++] = un->c.un_record_id;
456 	recids[rid] = 0;	/* filled in below */
457 
458 	/*
459 	 * Decrement the HSP reference count and
460 	 * remove the knowledge of the HSP from the unit struct.
461 	 * This is done atomically to remove a window.
462 	 */
463 	if (un->un_hsp_id != -1) {
464 		(void) md_hot_spare_ifc(HSP_DECREF, un->un_hsp_id, 0, 0,
465 		    &recids[rid++], NULL, NULL, NULL);
466 		un->un_hsp_id = -1;
467 	}
468 
469 	/* set end marker and commit records */
470 	recids[rid] = 0;
471 	mddb_commitrecs_wrapper(recids);
472 
473 	vtoc_id = un->c.un_vtoc_id;
474 
475 	/*
476 	 * Remove self from the namespace
477 	 */
478 	if (un->c.un_revision & MD_FN_META_DEV) {
479 		(void) md_rem_selfname(un->c.un_self_id);
480 	}
481 
482 	/* Remove the unit structure */
483 	mddb_deleterec_wrapper(un->c.un_record_id);
484 
485 	/* Remove the vtoc, if present */
486 	if (vtoc_id)
487 		mddb_deleterec_wrapper(vtoc_id);
488 
489 	SE_NOTIFY(EC_SVM_CONFIG, ESC_SVM_DELETE, SVM_TAG_METADEVICE,
490 	    MD_MIN2SET(mnum), MD_MIN2UNIT(mnum));
491 	md_rem_names(sv, nsv);
492 	kmem_free(sv, sizeof (sv_dev_t) * nsv);
493 	kmem_free(recids, sizeof (mddb_recid_t) * (nsv + 3));
494 }
495 
496 static void
497 stripe_error(md_sps_t *ps)
498 {
499 	struct buf	*pb = ps->ps_bp;
500 	mdi_unit_t	*ui = ps->ps_ui;
501 	md_dev64_t	dev = ps->ps_errcomp->un_dev;
502 	md_dev64_t	md_dev = md_expldev(pb->b_edev);
503 	char		*str;
504 
505 	if (pb->b_flags & B_READ) {
506 		ps->ps_errcomp->un_mirror.ms_flags |= MDM_S_READERR;
507 		str = "read";
508 	} else {
509 		ps->ps_errcomp->un_mirror.ms_flags |= MDM_S_WRTERR;
510 		str = "write";
511 	}
512 	if (!(ps->ps_flags & MD_SPS_DONTFREE)) {
513 		if (MUTEX_HELD(&ps->ps_mx)) {
514 			mutex_exit(&ps->ps_mx);
515 		}
516 	} else {
517 		ASSERT(panicstr);
518 	}
519 	SPS_FREE(stripe_parent_cache, ps);
520 	pb->b_flags |= B_ERROR;
521 
522 	md_kstat_done(ui, pb, 0);
523 	md_unit_readerexit(ui);
524 	md_biodone(pb);
525 
526 	cmn_err(CE_WARN, "md: %s: %s error on %s",
527 	    md_shortname(md_getminor(md_dev)), str,
528 	    md_devname(MD_DEV2SET(md_dev), dev, NULL, 0));
529 }
530 
531 static int
532 stripe_done(struct buf *cb)
533 {
534 	struct buf	*pb;
535 	mdi_unit_t	*ui;
536 	md_sps_t	*ps;
537 	md_scs_t	*cs;
538 
539 	/*LINTED*/
540 	cs = (md_scs_t *)((caddr_t)cb - md_stripe_mcs_buf_off);
541 	ps = cs->cs_ps;
542 	pb = ps->ps_bp;
543 
544 	mutex_enter(&ps->ps_mx);
545 	if (cb->b_flags & B_ERROR) {
546 		ps->ps_flags |= MD_SPS_ERROR;
547 		pb->b_error = cb->b_error;
548 		ps->ps_errcomp = cs->cs_comp;
549 	}
550 
551 	if (cb->b_flags & B_REMAPPED)
552 		bp_mapout(cb);
553 
554 	ps->ps_frags--;
555 	if (ps->ps_frags != 0) {
556 		mutex_exit(&ps->ps_mx);
557 		kmem_cache_free(stripe_child_cache, cs);
558 		return (1);
559 	}
560 	kmem_cache_free(stripe_child_cache, cs);
561 	if (ps->ps_flags & MD_SPS_ERROR) {
562 		stripe_error(ps);
563 		return (1);
564 	}
565 	ui = ps->ps_ui;
566 	if (!(ps->ps_flags & MD_SPS_DONTFREE)) {
567 		mutex_exit(&ps->ps_mx);
568 	} else {
569 		ASSERT(panicstr);
570 	}
571 	SPS_FREE(stripe_parent_cache, ps);
572 	md_kstat_done(ui, pb, 0);
573 	md_unit_readerexit(ui);
574 	md_biodone(pb);
575 	return (0);
576 }
577 
578 
579 /*
580  * This routine does the mapping from virtual (dev, blkno) of a metapartition
581  * to the real (dev, blkno) of a real disk partition.
582  * It goes to the md_conf[] table to find out the correct real partition
583  * dev and block number for this buffer.
584  *
585  * A single buf request can not go across real disk partition boundary.
586  * When the virtual request specified by (dev, blkno) spans more than one
587  * real partition, md_mapbuf will return 1. Then the caller should prepare
588  * another real buf and continue calling md_mapbuf to do the mapping until
589  * it returns 0.
590  *
591  */
592 
593 static int
594 md_mapbuf(
595 	ms_unit_t	*un,
596 	diskaddr_t	blkno,
597 	u_longlong_t	bcount,
598 	buf_t		*bp,	/* if bp==NULL, skip bp updates */
599 	ms_comp_t	**mdc)	/* if bp==NULL, skip mdc update */
600 {
601 	struct ms_row	*mdr;
602 	struct ms_comp	*mdcomp;
603 	diskaddr_t	stripe_blk;
604 	diskaddr_t	fragment, blk_in_row, endblk;
605 	offset_t	interlace;
606 	size_t		dev_index;
607 	int		row_index, more;
608 	extern unsigned md_maxphys;
609 	/* Work var's when bp==NULL */
610 	u_longlong_t	wb_bcount;
611 	diskaddr_t	wb_blkno;
612 	md_dev64_t	wb_edev;
613 	ms_comp_t	*wmdc;
614 
615 	/*
616 	 * Do a real calculation to derive the minor device of the
617 	 * Virtual Disk, which in turn will let us derive the
618 	 * device/minor of the underlying real device.
619 	 */
620 
621 
622 	for (row_index = 0; row_index < un->un_nrows; row_index++) {
623 		mdr = &un->un_row[row_index];
624 		if (blkno < mdr->un_cum_blocks)
625 			break;
626 	}
627 	ASSERT(row_index != un->un_nrows);
628 
629 	mdcomp = (struct ms_comp *)((void *)&((char *)un)[un->un_ocomp]);
630 
631 	blk_in_row = blkno - mdr->un_cum_blocks + mdr->un_blocks;
632 	endblk = (diskaddr_t)(blkno + howmany(bcount, DEV_BSIZE));
633 	if (mdr->un_ncomp == 1) { /* No striping */
634 		if (endblk > mdr->un_cum_blocks) {
635 			wb_bcount = ldbtob(mdr->un_cum_blocks - blkno);
636 			if ((row_index + 1) == un->un_nrows)
637 				more = 0;
638 			else
639 				more = 1;
640 		} else {
641 			wb_bcount = bcount;
642 			more = 0;
643 		}
644 		wmdc = &mdcomp[mdr->un_icomp];
645 		wb_blkno = blk_in_row;
646 	} else { /* Have striping */
647 		interlace = mdr->un_interlace;
648 		fragment = blk_in_row % interlace;
649 		if (bcount > ldbtob(interlace - fragment)) {
650 			more = 1;
651 			wb_bcount = ldbtob(interlace - fragment);
652 		} else {
653 			more = 0;
654 			wb_bcount = bcount;
655 		}
656 
657 		stripe_blk = blk_in_row / interlace;
658 		dev_index = (size_t)(stripe_blk % mdr->un_ncomp);
659 		wmdc = &mdcomp[mdr->un_icomp + dev_index];
660 		wb_blkno = (diskaddr_t)(((stripe_blk / mdr->un_ncomp)
661 			* interlace) + fragment);
662 	}
663 
664 	wb_blkno += wmdc->un_start_block;
665 	wb_edev = wmdc->un_dev;
666 
667 	/* only break up the I/O if we're not built on another metadevice */
668 	if ((md_getmajor(wb_edev) != md_major) && (wb_bcount > md_maxphys)) {
669 		wb_bcount = md_maxphys;
670 		more = 1;
671 	}
672 	if (bp != (buf_t *)NULL) {
673 		/*
674 		 * wb_bcount is limited by md_maxphys which is 'int'
675 		 */
676 		bp->b_bcount = (size_t)wb_bcount;
677 		bp->b_lblkno = wb_blkno;
678 		bp->b_edev = md_dev64_to_dev(wb_edev);
679 		*mdc = wmdc;
680 	}
681 	return (more);
682 }
683 
684 static void
685 md_stripe_strategy(buf_t *pb, int flag, void *private)
686 {
687 	md_sps_t	*ps;
688 	md_scs_t	*cs;
689 	int		doing_writes;
690 	int		more;
691 	ms_unit_t	*un;
692 	mdi_unit_t	*ui;
693 	size_t		current_count;
694 	diskaddr_t	current_blkno;
695 	off_t		current_offset;
696 	buf_t		*cb;		/* child buf pointer */
697 	set_t		setno;
698 
699 	setno = MD_MIN2SET(getminor(pb->b_edev));
700 
701 	/*
702 	 * When doing IO to a multi owner meta device, check if set is halted.
703 	 * We do this check without the needed lock held, for performance
704 	 * reasons.
705 	 * If an IO just slips through while the set is locked via an
706 	 * MD_MN_SUSPEND_SET, we don't care about it.
707 	 * Only check for a suspended set if we are a top-level i/o request
708 	 * (MD_STR_NOTTOP is cleared in 'flag').
709 	 */
710 	if ((md_set[setno].s_status & (MD_SET_HALTED | MD_SET_MNSET)) ==
711 	    (MD_SET_HALTED | MD_SET_MNSET)) {
712 		if ((flag & MD_STR_NOTTOP) == 0) {
713 			mutex_enter(&md_mx);
714 			/* Here we loop until the set is no longer halted */
715 			while (md_set[setno].s_status & MD_SET_HALTED) {
716 				cv_wait(&md_cv, &md_mx);
717 			}
718 			mutex_exit(&md_mx);
719 		}
720 	}
721 
722 	ui = MDI_UNIT(getminor(pb->b_edev));
723 
724 	md_kstat_waitq_enter(ui);
725 
726 	un = (ms_unit_t *)md_unit_readerlock(ui);
727 
728 	if ((flag & MD_NOBLOCK) == 0) {
729 		if (md_inc_iocount(setno) != 0) {
730 			pb->b_flags |= B_ERROR;
731 			pb->b_error = ENXIO;
732 			pb->b_resid = pb->b_bcount;
733 			md_unit_readerexit(ui);
734 			biodone(pb);
735 			return;
736 		}
737 	} else {
738 		md_inc_iocount_noblock(setno);
739 	}
740 
741 	if (!(flag & MD_STR_NOTTOP)) {
742 		if (md_checkbuf(ui, (md_unit_t *)un, pb) != 0) {
743 			md_kstat_waitq_exit(ui);
744 			return;
745 		}
746 	}
747 
748 	ps = kmem_cache_alloc(stripe_parent_cache, MD_ALLOCFLAGS);
749 	stripe_parent_init(ps);
750 
751 	/*
752 	 * Save essential information from the original buffhdr
753 	 * in the md_save structure.
754 	 */
755 	ps->ps_un = un;
756 	ps->ps_ui = ui;
757 	ps->ps_bp = pb;
758 	ps->ps_addr = pb->b_un.b_addr;
759 
760 	if ((pb->b_flags & B_READ) == 0)
761 		doing_writes = 1;
762 	else
763 		doing_writes = 0;
764 
765 
766 	current_count = pb->b_bcount;
767 	current_blkno = pb->b_lblkno;
768 	current_offset  = 0;
769 
770 	if (!(flag & MD_STR_NOTTOP) && panicstr)
771 		ps->ps_flags |= MD_SPS_DONTFREE;
772 
773 	md_kstat_waitq_to_runq(ui);
774 
775 	ps->ps_frags++;
776 	do {
777 		cs = kmem_cache_alloc(stripe_child_cache, MD_ALLOCFLAGS);
778 		stripe_child_init(cs);
779 		cb = &cs->cs_buf;
780 		cs->cs_ps = ps;
781 		more = md_mapbuf(un, current_blkno, current_count, cb,
782 			&cs->cs_comp);
783 
784 		cb = md_bioclone(pb, current_offset, cb->b_bcount, cb->b_edev,
785 				cb->b_lblkno, stripe_done, cb, KM_NOSLEEP);
786 		/*
787 		 * Do these calculations now,
788 		 *  so that we pickup a valid b_bcount from the chld_bp.
789 		 */
790 		current_offset += cb->b_bcount;
791 		current_count -=  cb->b_bcount;
792 		current_blkno +=  (diskaddr_t)(lbtodb(cb->b_bcount));
793 
794 		if (more) {
795 			mutex_enter(&ps->ps_mx);
796 			ps->ps_frags++;
797 			mutex_exit(&ps->ps_mx);
798 		}
799 
800 		if (doing_writes &&
801 		    cs->cs_comp->un_mirror.ms_flags & MDM_S_NOWRITE) {
802 			(void) stripe_done(cb);
803 			continue;
804 		}
805 		md_call_strategy(cb, flag, private);
806 	} while (more);
807 
808 	if (!(flag & MD_STR_NOTTOP) && panicstr) {
809 		while (!(ps->ps_flags & MD_SPS_DONE)) {
810 			md_daemon(1, &md_done_daemon);
811 			drv_usecwait(10);
812 		}
813 		kmem_cache_free(stripe_parent_cache, ps);
814 	}
815 }
816 
817 static int
818 stripe_snarf(md_snarfcmd_t cmd, set_t setno)
819 {
820 	ms_unit_t	*un;
821 	mddb_recid_t	recid;
822 	int		gotsomething;
823 	int		all_stripes_gotten;
824 	mddb_type_t	typ1;
825 	mddb_de_ic_t	*dep;
826 	mddb_rb32_t	*rbp;
827 	size_t		newreqsize;
828 	ms_unit_t	*big_un;
829 	ms_unit32_od_t	*small_un;
830 
831 
832 	if (cmd == MD_SNARF_CLEANUP)
833 		return (0);
834 
835 	all_stripes_gotten = 1;
836 	gotsomething = 0;
837 
838 	typ1 = (mddb_type_t)md_getshared_key(setno,
839 	    stripe_md_ops.md_driver.md_drivername);
840 	recid = mddb_makerecid(setno, 0);
841 
842 	while ((recid = mddb_getnextrec(recid, typ1, 0)) > 0) {
843 		if (mddb_getrecprivate(recid) & MD_PRV_GOTIT)
844 			continue;
845 
846 		dep = mddb_getrecdep(recid);
847 		dep->de_flags = MDDB_F_STRIPE;
848 		rbp = dep->de_rb;
849 
850 		switch (rbp->rb_revision) {
851 		case MDDB_REV_RB:
852 		case MDDB_REV_RBFN:
853 			if ((rbp->rb_private & MD_PRV_CONVD) == 0) {
854 				/*
855 				 * This means, we have an old and small record
856 				 * and this record hasn't already been
857 				 * converted.  Before we create an incore
858 				 * metadevice from this we have to convert it to
859 				 * a big record.
860 				 */
861 				small_un =
862 				    (ms_unit32_od_t *)mddb_getrecaddr(recid);
863 				newreqsize = get_big_stripe_req_size(small_un,
864 						COMPLETE_STRUCTURE);
865 				big_un = (ms_unit_t *)kmem_zalloc(newreqsize,
866 					KM_SLEEP);
867 				stripe_convert((caddr_t)small_un,
868 					(caddr_t)big_un, SMALL_2_BIG);
869 				kmem_free(small_un, dep->de_reqsize);
870 				dep->de_rb_userdata = big_un;
871 				dep->de_reqsize = newreqsize;
872 				un = big_un;
873 				rbp->rb_private |= MD_PRV_CONVD;
874 			} else {
875 				/* Small device had already been converted */
876 				un = (ms_unit_t *)mddb_getrecaddr(recid);
877 			}
878 			un->c.un_revision &= ~MD_64BIT_META_DEV;
879 			break;
880 		case MDDB_REV_RB64:
881 		case MDDB_REV_RB64FN:
882 			/* Big device */
883 			un = (ms_unit_t *)mddb_getrecaddr(recid);
884 			un->c.un_revision |= MD_64BIT_META_DEV;
885 			un->c.un_flag |= MD_EFILABEL;
886 			break;
887 		}
888 		NOTE_FN(rbp->rb_revision, un->c.un_revision);
889 
890 		/* Create minor node for snarfed unit. */
891 		(void) md_create_minor_node(MD_MIN2SET(MD_SID(un)), MD_SID(un));
892 
893 		if (MD_UNIT(MD_SID(un)) != NULL) {
894 			mddb_setrecprivate(recid, MD_PRV_PENDDEL);
895 			continue;
896 		}
897 		all_stripes_gotten = 0;
898 		if (stripe_build_incore((void *)un, 1) == 0) {
899 			mddb_setrecprivate(recid, MD_PRV_GOTIT);
900 			md_create_unit_incore(MD_SID(un), &stripe_md_ops, 0);
901 			gotsomething = 1;
902 		}
903 	}
904 
905 	if (!all_stripes_gotten)
906 		return (gotsomething);
907 
908 	recid = mddb_makerecid(setno, 0);
909 	while ((recid = mddb_getnextrec(recid, typ1, 0)) > 0)
910 		if (!(mddb_getrecprivate(recid) & MD_PRV_GOTIT))
911 			mddb_setrecprivate(recid, MD_PRV_PENDDEL);
912 
913 	return (0);
914 }
915 
916 static int
917 stripe_halt(md_haltcmd_t cmd, set_t setno)
918 {
919 	int		i;
920 	mdi_unit_t	*ui;
921 	minor_t		mnum;
922 
923 	if (cmd == MD_HALT_CLOSE)
924 		return (0);
925 
926 	if (cmd == MD_HALT_OPEN)
927 		return (0);
928 
929 	if (cmd == MD_HALT_UNLOAD)
930 		return (0);
931 
932 	if (cmd == MD_HALT_CHECK) {
933 		for (i = 0; i < md_nunits; i++) {
934 			mnum = MD_MKMIN(setno, i);
935 			if ((ui = MDI_UNIT(mnum)) == NULL)
936 				continue;
937 			if (ui->ui_opsindex != stripe_md_ops.md_selfindex)
938 				continue;
939 			if (md_unit_isopen(ui))
940 				return (1);
941 		}
942 		return (0);
943 	}
944 
945 	if (cmd != MD_HALT_DOIT)
946 		return (1);
947 
948 	for (i = 0; i < md_nunits; i++) {
949 		mnum = MD_MKMIN(setno, i);
950 		if ((ui = MDI_UNIT(mnum)) == NULL)
951 			continue;
952 		if (ui->ui_opsindex != stripe_md_ops.md_selfindex)
953 			continue;
954 		reset_stripe((ms_unit_t *)MD_UNIT(mnum), mnum, 0);
955 	}
956 
957 	return (0);
958 }
959 
960 /*ARGSUSED3*/
961 static int
962 stripe_open(dev_t *dev, int flag, int otyp, cred_t *cred_p, int md_oflags)
963 {
964 	minor_t		mnum = getminor(*dev);
965 	mdi_unit_t	*ui = MDI_UNIT(mnum);
966 	ms_unit_t	*un;
967 	int		err = 0;
968 	set_t		setno;
969 
970 	/*
971 	 * When doing an open of a multi owner metadevice, check to see if this
972 	 * node is a starting node and if a reconfig cycle is underway.
973 	 * If so, the system isn't sufficiently set up enough to handle the
974 	 * open (which involves I/O during sp_validate), so fail with ENXIO.
975 	 */
976 	setno = MD_MIN2SET(mnum);
977 	if ((md_set[setno].s_status & (MD_SET_MNSET | MD_SET_MN_START_RC)) ==
978 	    (MD_SET_MNSET | MD_SET_MN_START_RC)) {
979 			return (ENXIO);
980 	}
981 
982 	/* single thread */
983 	un = (ms_unit_t *)md_unit_openclose_enter(ui);
984 
985 	/* open devices, if necessary */
986 	if (! md_unit_isopen(ui) || (md_oflags & MD_OFLG_PROBEDEV)) {
987 		if ((err = stripe_open_all_devs(un, md_oflags)) != 0) {
988 			goto out;
989 		}
990 	}
991 
992 	/* count open */
993 	if ((err = md_unit_incopen(mnum, flag, otyp)) != 0)
994 		goto out;
995 
996 	/* unlock, return success */
997 out:
998 	md_unit_openclose_exit(ui);
999 	return (err);
1000 }
1001 
1002 /*ARGSUSED1*/
1003 static int
1004 stripe_close(
1005 	dev_t		dev,
1006 	int		flag,
1007 	int		otyp,
1008 	cred_t		*cred_p,
1009 	int		md_cflags
1010 )
1011 {
1012 	minor_t		mnum = getminor(dev);
1013 	mdi_unit_t	*ui = MDI_UNIT(mnum);
1014 	ms_unit_t	*un;
1015 	int		err = 0;
1016 
1017 	/* single thread */
1018 	un = (ms_unit_t *)md_unit_openclose_enter(ui);
1019 
1020 	/* count closed */
1021 	if ((err = md_unit_decopen(mnum, otyp)) != 0)
1022 		goto out;
1023 
1024 	/* close devices, if necessary */
1025 	if (! md_unit_isopen(ui) || (md_cflags & MD_OFLG_PROBEDEV)) {
1026 		stripe_close_all_devs(un, md_cflags);
1027 	}
1028 
1029 	/* unlock, return success */
1030 out:
1031 	md_unit_openclose_exit(ui);
1032 	return (err);
1033 }
1034 
1035 
1036 static struct buf dumpbuf;
1037 
1038 /*
1039  * This routine dumps memory to the disk.  It assumes that the memory has
1040  * already been mapped into mainbus space.  It is called at disk interrupt
1041  * priority when the system is in trouble.
1042  *
1043  */
1044 static int
1045 stripe_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk)
1046 {
1047 	ms_unit_t	*un;
1048 	buf_t		*bp;
1049 	ms_comp_t	*mdc;
1050 	u_longlong_t	nb;
1051 	diskaddr_t	mapblk;
1052 	int		result;
1053 	int		more;
1054 	int		saveresult = 0;
1055 
1056 	/*
1057 	 * Don't need to grab the unit lock.
1058 	 * Cause nothing else is suppose to be happenning.
1059 	 * Also dump is not suppose to sleep.
1060 	 */
1061 	un = (ms_unit_t *)MD_UNIT(getminor(dev));
1062 
1063 	if ((diskaddr_t)blkno >= un->c.un_total_blocks)
1064 		return (EINVAL);
1065 
1066 	if ((diskaddr_t)blkno + nblk > un->c.un_total_blocks)
1067 		return (EINVAL);
1068 
1069 	bp = &dumpbuf;
1070 	nb = ldbtob(nblk);
1071 	do {
1072 		bzero((caddr_t)bp, sizeof (*bp));
1073 		more = md_mapbuf(un, (diskaddr_t)blkno, nb, bp, &mdc);
1074 		nblk = btodb(bp->b_bcount);
1075 		mapblk = bp->b_lblkno;
1076 		if (!(mdc->un_mirror.ms_flags & MDM_S_NOWRITE)) {
1077 			/*
1078 			 * bdev_dump() is currently only able to take
1079 			 * 32 bit wide blkno's.
1080 			 */
1081 			result = bdev_dump(bp->b_edev, addr, (daddr_t)mapblk,
1082 						nblk);
1083 			if (result)
1084 				saveresult = result;
1085 		}
1086 
1087 		nb -= bp->b_bcount;
1088 		addr += bp->b_bcount;
1089 		blkno += nblk;
1090 	} while (more);
1091 
1092 	return (saveresult);
1093 }
1094 
1095 /*ARGSUSED*/
1096 static intptr_t
1097 stripe_shared_by_blk(
1098 	md_dev64_t dev,
1099 	void *junk,
1100 	diskaddr_t blkno,
1101 	u_longlong_t *cnt)
1102 {
1103 	ms_unit_t	*un;
1104 	buf_t		bp;
1105 	ms_comp_t	*comp;
1106 
1107 	un = MD_UNIT(md_getminor(dev));
1108 	(void) md_mapbuf(un, blkno, ldbtob(*cnt), &bp, &comp);
1109 	*cnt = (u_longlong_t)lbtodb(bp.b_bcount);
1110 	return ((intptr_t)&comp->un_mirror);
1111 }
1112 
1113 /*
1114  * stripe_block_count_skip_size() returns the following values
1115  *	so that the logical to physical block mappings can
1116  *	be calculated without intimate knowledge of the underpinnings.
1117  *
1118  *	block - first logical block number of the device.
1119  *		block = [ # of blocks before THE row ] +
1120  *			[ # of blocks in THE row before the component ]
1121  *	count - # of segments (interlaced size).
1122  *	skip  - # of logical blocks between segments, or delta to
1123  *		  get to next segment
1124  *	size  - interlace size used for the block, count, skip.
1125  */
1126 /*ARGSUSED*/
1127 static intptr_t
1128 stripe_block_count_skip_size(
1129 	md_dev64_t	 dev,
1130 	void		*junk,
1131 	int		ci,
1132 	diskaddr_t	*block,
1133 	size_t		*count,
1134 	u_longlong_t	*skip,
1135 	u_longlong_t	*size)
1136 {
1137 	ms_unit_t	*un;
1138 	int		row;
1139 	struct ms_row	*mdr;
1140 	int		cmpcount = 0;
1141 
1142 	un = MD_UNIT(md_getminor(dev));
1143 
1144 	for (row = 0; row < un->un_nrows; row++) {
1145 		mdr = &un->un_row[row];
1146 		if ((mdr->un_ncomp + cmpcount) > ci)
1147 			break;
1148 		cmpcount += mdr->un_ncomp;
1149 	}
1150 	ASSERT(row != un->un_nrows);
1151 
1152 	/*
1153 	 * Concatenations are always contiguous blocks,
1154 	 * you cannot depend on the interlace being a usable
1155 	 * value (except for stripes).
1156 	 */
1157 	if (mdr->un_ncomp == 1) {	/* Concats */
1158 		*block = mdr->un_cum_blocks - mdr->un_blocks;
1159 		*count = 1;
1160 		*skip = 0;
1161 		*size = mdr->un_blocks;
1162 	} else {			/* Stripes */
1163 		*block = (mdr->un_cum_blocks - mdr->un_blocks) +
1164 		    ((ci - cmpcount) * mdr->un_interlace);
1165 		*count	= (size_t)(mdr->un_blocks / (mdr->un_interlace
1166 			* mdr->un_ncomp));
1167 		*skip = (mdr->un_interlace * mdr->un_ncomp) - mdr->un_interlace;
1168 		*size = mdr->un_interlace;
1169 	}
1170 
1171 	return (0);
1172 }
1173 
1174 /*ARGSUSED*/
1175 static intptr_t
1176 stripe_shared_by_indx(md_dev64_t dev, void *junk, int indx)
1177 {
1178 	ms_unit_t	*un;
1179 	ms_comp_t	*comp;
1180 
1181 	un = MD_UNIT(md_getminor(dev));
1182 	comp = (struct ms_comp *)((void *)&((char *)un)[un->un_ocomp]);
1183 	comp += indx;
1184 	return ((intptr_t)&comp->un_mirror);
1185 }
1186 
1187 /*ARGSUSED*/
1188 intptr_t
1189 stripe_component_count(md_dev64_t dev, void *junk)
1190 {
1191 	/*
1192 	 * See comments for stripe_get_dev
1193 	 */
1194 
1195 	ms_unit_t	*un;
1196 	int		count = 0;
1197 	int		row;
1198 
1199 	un = MD_UNIT(md_getminor(dev));
1200 	for (row = 0; row < un->un_nrows; row++)
1201 		count += un->un_row[row].un_ncomp;
1202 	return (count);
1203 }
1204 
1205 /*ARGSUSED*/
1206 intptr_t
1207 stripe_get_dev(md_dev64_t dev, void *junk, int indx, ms_cd_info_t *cd)
1208 {
1209 	/*
1210 	 * It should be noted that stripe_replace in stripe_ioctl.c calls this
1211 	 * routine using makedevice(0, minor) for the first argument.
1212 	 *
1213 	 * If this routine at some point in the future needs to use the major
1214 	 * number stripe_replace must be changed.
1215 	 */
1216 
1217 	ms_unit_t	*un;
1218 	ms_comp_t	*comp;
1219 	md_dev64_t	tmpdev;
1220 
1221 	un = MD_UNIT(md_getminor(dev));
1222 	comp = (struct ms_comp *)((void *)&((char *)un)[un->un_ocomp]);
1223 	comp += indx;
1224 	tmpdev = comp->un_dev;
1225 	/*
1226 	 * Try to resolve devt again if NODEV64
1227 	 * Check if this comp is hotspared and if it is
1228 	 * then use key for hotspare
1229 	 */
1230 	if (tmpdev == NODEV64) {
1231 		tmpdev = md_resolve_bydevid(md_getminor(dev), tmpdev,
1232 			comp->un_mirror.ms_hs_id ?
1233 			comp->un_mirror.ms_hs_key :
1234 			comp->un_key);
1235 		comp->un_dev = tmpdev;
1236 	}
1237 
1238 	cd->cd_dev = comp->un_dev;
1239 	cd->cd_orig_dev = comp->un_mirror.ms_orig_dev;
1240 	return (0);
1241 }
1242 
1243 /*ARGSUSED*/
1244 void
1245 stripe_replace_done(md_dev64_t dev, sv_dev_t *sv)
1246 {
1247 	/*
1248 	 * See comments for stripe_get_dev
1249 	 */
1250 
1251 	minor_t		mnum = md_getminor(dev);
1252 
1253 	if (sv != NULL) {
1254 		md_rem_names(sv, 1);
1255 		kmem_free(sv, sizeof (sv_dev_t));
1256 	}
1257 
1258 	md_unit_writerexit(MDI_UNIT(mnum));
1259 }
1260 
1261 /*ARGSUSED*/
1262 intptr_t
1263 stripe_replace_dev(md_dev64_t dev, void *junk, int ci, ms_new_dev_t *nd,
1264     mddb_recid_t *recids, int nrecids, void (**replace_done)(),
1265     void **replace_data)
1266 {
1267 	minor_t		mnum;
1268 	ms_unit_t	*un;
1269 	mdi_unit_t	*ui;
1270 	ms_comp_t	*comp;
1271 	diskaddr_t	dev_size;
1272 	int		row;
1273 	int		ncomps = 0;
1274 	int		cmpcount = 0;
1275 	int		rid = 0;
1276 	struct ms_row	*mdr;
1277 	sv_dev_t	*sv = NULL;
1278 	mddb_recid_t	hs_id = 0;
1279 	set_t		setno;
1280 	side_t		side;
1281 	md_dev64_t	this_dev;
1282 
1283 	mnum = md_getminor(dev);
1284 	ui = MDI_UNIT(mnum);
1285 	setno = MD_MIN2SET(mnum);
1286 	side = mddb_getsidenum(setno);
1287 
1288 	un = md_unit_writerlock(ui);
1289 
1290 	*replace_data = NULL;
1291 	comp = (struct ms_comp *)((void *)&((char *)un)[un->un_ocomp]);
1292 
1293 	comp += ci;
1294 
1295 	/*
1296 	 * Count the number of components
1297 	 */
1298 	for (row = 0; row < un->un_nrows; row++) {
1299 		struct ms_row *mdr = &un->un_row[row];
1300 		ncomps += mdr->un_ncomp;
1301 	}
1302 
1303 	recids[0] = 0;
1304 	/*
1305 	 * No need of checking size of new device,
1306 	 * when hotsparing (it has already been done), or
1307 	 * when enabling the device.
1308 	 */
1309 	if ((nd != NULL) && (nd->nd_hs_id == 0)) {
1310 		for (row = 0; row < un->un_nrows; row++) {
1311 			mdr = &un->un_row[row];
1312 			if ((mdr->un_ncomp + cmpcount) > ci)
1313 				break;
1314 			cmpcount += mdr->un_ncomp;
1315 		}
1316 		ASSERT(row != un->un_nrows);
1317 
1318 		/* Concatenations have a ncomp = 1 */
1319 		dev_size = mdr->un_blocks / mdr->un_ncomp;
1320 
1321 		/*
1322 		 * now check to see if new comp can be used in
1323 		 * place of old comp
1324 		 */
1325 		if ((un->c.un_flag & MD_LABELED) && (ci == 0) &&
1326 		    nd->nd_labeled)
1327 			nd->nd_start_blk = 0;
1328 		else
1329 			nd->nd_nblks -= nd->nd_start_blk;
1330 
1331 		if (dev_size > nd->nd_nblks) {
1332 			md_unit_writerexit(ui);
1333 			return (MDE_COMP_TOO_SMALL);
1334 		}
1335 
1336 		sv = (sv_dev_t *)kmem_alloc(sizeof (sv_dev_t), KM_SLEEP);
1337 		sv->setno = MD_MIN2SET(mnum);
1338 		sv->key = comp->un_key;
1339 	}
1340 
1341 	/*
1342 	 * Close this component.
1343 	 */
1344 	if (comp->un_mirror.ms_flags & MDM_S_ISOPEN) {
1345 		md_layered_close(comp->un_dev, MD_OFLG_NULL);
1346 		comp->un_mirror.ms_flags &= ~MDM_S_ISOPEN;
1347 	}
1348 
1349 	/*
1350 	 * If the component is hotspared, return to the pool.
1351 	 */
1352 	if (comp->un_mirror.ms_hs_id != 0) {
1353 		hs_cmds_t	cmd;
1354 		mdkey_t		hs_key;
1355 
1356 		hs_key = comp->un_mirror.ms_hs_key;
1357 		comp->un_dev = comp->un_mirror.ms_orig_dev;
1358 		comp->un_start_block = comp->un_mirror.ms_orig_blk;
1359 		comp->un_mirror.ms_hs_key = 0;
1360 		comp->un_mirror.ms_hs_id = 0;
1361 		comp->un_mirror.ms_orig_dev = 0;
1362 
1363 		cmd = HS_FREE;
1364 		if ((comp->un_mirror.ms_state != CS_OKAY) &&
1365 		    (comp->un_mirror.ms_state != CS_RESYNC))
1366 			cmd = HS_BAD;
1367 		(void) md_hot_spare_ifc(cmd, un->un_hsp_id, 0, 0, &hs_id,
1368 		    &hs_key, NULL, NULL);
1369 	}
1370 
1371 	/*
1372 	 * Open by device id; for enable (indicated by a NULL
1373 	 * nd pointer), use the existing component info.  For
1374 	 * replace, use the new device.
1375 	 */
1376 	if (nd == NULL) {
1377 		this_dev = md_resolve_bydevid(mnum, comp->un_dev, comp->un_key);
1378 		/*
1379 		 * If someone replaced a new disk in the same slot
1380 		 * we get NODEV64 since old device id cannot be
1381 		 * resolved. The new devt is obtained from the
1382 		 * mddb since devt is going to be unchanged for the
1383 		 * enable case. No need to check for multiple
1384 		 * keys here because the caller (comp_replace)
1385 		 * has already sanity checked it for us.
1386 		 */
1387 		if (this_dev == NODEV64) {
1388 			this_dev = md_getdevnum(setno, side, comp->un_key,
1389 			    MD_TRUST_DEVT);
1390 		}
1391 	} else {
1392 		/*
1393 		 * If this is a hotspare, save the original dev_t for later
1394 		 * use. If this has occured during boot then the value of
1395 		 * comp->un_dev will be NODEV64 because of the failure to look
1396 		 * up the devid of the device.
1397 		 */
1398 		if (nd->nd_hs_id != 0)
1399 			comp->un_mirror.ms_orig_dev = comp->un_dev;
1400 		this_dev = md_resolve_bydevid(mnum, nd->nd_dev, nd->nd_key);
1401 	}
1402 
1403 	comp->un_dev = this_dev;
1404 
1405 	/*
1406 	 * Now open the new device if required. Note for a single component
1407 	 * stripe it will not be open - leave this for the mirror driver to
1408 	 * deal with.
1409 	 */
1410 	if (md_unit_isopen(ui)) {
1411 		if (md_layered_open(mnum, &this_dev, MD_OFLG_NULL)) {
1412 			mddb_recid_t	ids[3];
1413 
1414 			ids[0] = un->c.un_record_id;
1415 			ids[1] = hs_id;
1416 			ids[2] = 0;
1417 			mddb_commitrecs_wrapper(ids);
1418 			if ((nd != NULL) && (nd->nd_hs_id != 0)) {
1419 				/*
1420 				 * Revert back to the original device.
1421 				 */
1422 				comp->un_dev = comp->un_mirror.ms_orig_dev;
1423 
1424 				cmn_err(CE_WARN,
1425 				    "md: %s: open error of hotspare %s",
1426 				    md_shortname(mnum),
1427 				    md_devname(MD_MIN2SET(mnum), nd->nd_dev,
1428 				    NULL, 0));
1429 				SE_NOTIFY(EC_SVM_STATE, ESC_SVM_OPEN_FAIL,
1430 				    SVM_TAG_HS, MD_MIN2SET(mnum), nd->nd_dev);
1431 			}
1432 			md_unit_writerexit(ui);
1433 			return (MDE_COMP_OPEN_ERR);
1434 		}
1435 		if (nd != NULL)
1436 			nd->nd_dev = this_dev;
1437 
1438 		comp->un_mirror.ms_flags |= MDM_S_ISOPEN;
1439 	}
1440 
1441 	if (nd == NULL) {
1442 		recids[0] = un->c.un_record_id;
1443 		recids[1] = hs_id;
1444 		recids[2] = 0;
1445 		*replace_done = stripe_replace_done;
1446 		return (0);
1447 	}
1448 
1449 	/* if hot sparing this device */
1450 	if (nd->nd_hs_id != 0) {
1451 		char	devname[MD_MAX_CTDLEN];
1452 		char	hs_devname[MD_MAX_CTDLEN];
1453 		set_t	setno;
1454 
1455 		comp->un_mirror.ms_hs_id = nd->nd_hs_id;
1456 		comp->un_mirror.ms_hs_key = nd->nd_key;
1457 
1458 		comp->un_mirror.ms_orig_blk = comp->un_start_block;
1459 
1460 		setno = MD_MIN2SET(mnum);
1461 
1462 		(void) md_devname(setno, comp->un_mirror.ms_orig_dev, devname,
1463 					sizeof (devname));
1464 		(void) md_devname(setno, nd->nd_dev, hs_devname,
1465 		    sizeof (hs_devname));
1466 
1467 		cmn_err(CE_NOTE, "md: %s: hotspared device %s with %s",
1468 		    md_shortname(mnum), devname, hs_devname);
1469 
1470 	} else {	/* replacing the device */
1471 		comp->un_key = nd->nd_key;
1472 		*replace_data = (void *)sv;
1473 
1474 		/*
1475 		 * For the old device, make sure to reset the parent
1476 		 * if it's a  metadevice.
1477 		 */
1478 		if (md_getmajor(comp->un_dev) == md_major) {
1479 			minor_t	  comp_mnum = md_getminor(comp->un_dev);
1480 			md_unit_t *comp_un = MD_UNIT(comp_mnum);
1481 
1482 			md_reset_parent(comp->un_dev);
1483 			recids[rid++] = MD_RECID(comp_un);
1484 		}
1485 	}
1486 
1487 	comp->un_dev = nd->nd_dev;
1488 	comp->un_start_block = nd->nd_start_blk;
1489 
1490 	/*
1491 	 * For the new device, make sure to set the parent if it's a
1492 	 * metadevice.
1493 	 *
1494 	 * If we ever support using metadevices as hot spares, this
1495 	 * will need to be tested, and possibly moved into the
1496 	 * preceding "else" clause, immediately following the parent
1497 	 * reset block.  For now, it's convenient to leave it here and
1498 	 * only compress nd->nd_dev once.
1499 	 */
1500 	if (md_getmajor(comp->un_dev) == md_major) {
1501 		minor_t		comp_mnum = md_getminor(comp->un_dev);
1502 		md_unit_t	*comp_un = MD_UNIT(comp_mnum);
1503 
1504 		md_set_parent(comp->un_dev, MD_SID(un));
1505 		recids[rid++] = MD_RECID(comp_un);
1506 	}
1507 
1508 	recids[rid++] = un->c.un_record_id;
1509 	recids[rid++] = hs_id;
1510 	recids[rid] = 0;
1511 	*replace_done = stripe_replace_done;
1512 	return (0);
1513 }
1514 
1515 /*ARGSUSED*/
1516 static intptr_t
1517 stripe_hotspare_dev(
1518 	md_dev64_t	dev,
1519 	void		*junk,
1520 	int		ci,
1521 	mddb_recid_t	*recids,
1522 	int		nrecids,
1523 	void		(**replace_done)(),
1524 	void		**replace_data)
1525 {
1526 	ms_unit_t	*un;
1527 	mdi_unit_t	*ui;
1528 	ms_comp_t	*comp;
1529 	int		row;
1530 	struct ms_row	*mdr;
1531 	ms_new_dev_t	nd;
1532 	int		err;
1533 	int		i;
1534 	minor_t		mnum;
1535 	set_t		setno;
1536 	int		cmpcount = 0;
1537 
1538 	mnum = md_getminor(dev);
1539 	ui = MDI_UNIT(mnum);
1540 	un = MD_UNIT(mnum);
1541 	setno = MD_MIN2SET(mnum);
1542 
1543 	if (md_get_setstatus(setno) & MD_SET_STALE)
1544 		return (1);
1545 
1546 	if (un->un_hsp_id == -1)
1547 		return (1);
1548 
1549 	for (row = 0; row < un->un_nrows; row++) {
1550 		mdr = &un->un_row[row];
1551 		if ((mdr->un_ncomp + cmpcount) > ci)
1552 			break;
1553 		cmpcount += mdr->un_ncomp;
1554 	}
1555 	ASSERT(row != un->un_nrows);
1556 
1557 	comp = (struct ms_comp *)((void *)&((char *)un)[un->un_ocomp]);
1558 	comp += ci;
1559 	/* Concatenations have a ncomp = 1 */
1560 	nd.nd_nblks = mdr->un_blocks / mdr->un_ncomp;
1561 
1562 	if ((un->c.un_flag & MD_LABELED) && (ci == 0))
1563 		nd.nd_labeled = 1;
1564 	else
1565 		nd.nd_labeled = 0;
1566 
1567 again:
1568 	err = md_hot_spare_ifc(HS_GET, un->un_hsp_id, nd.nd_nblks,
1569 	    nd.nd_labeled, &nd.nd_hs_id, &nd.nd_key, &nd.nd_dev,
1570 	    &nd.nd_start_blk);
1571 
1572 	if (err) {
1573 		if (!stripe_replace_dev(dev, junk, ci, NULL, recids, nrecids,
1574 		    replace_done, replace_data)) {
1575 			mddb_commitrecs_wrapper(recids);
1576 			md_unit_writerexit(ui);
1577 		}
1578 		recids[0] = 0;
1579 		return (1);
1580 	}
1581 
1582 	if (stripe_replace_dev(dev, junk, ci, &nd, recids, nrecids,
1583 		replace_done, replace_data)) {
1584 
1585 		(void) md_hot_spare_ifc(HS_BAD, un->un_hsp_id, 0, 0,
1586 		    &nd.nd_hs_id, &nd.nd_key, NULL, NULL);
1587 		mddb_commitrec_wrapper(nd.nd_hs_id);
1588 		goto again;
1589 	}
1590 
1591 	/* Leave a slot for the null recid */
1592 	for (i = 0; i < (nrecids - 1); i++) {
1593 		if (recids[i] == 0) {
1594 			recids[i++] = nd.nd_hs_id;
1595 			recids[i] = 0;
1596 		}
1597 	}
1598 	return (0);
1599 }
1600 
1601 static int
1602 stripe_imp_set(
1603 	set_t	setno
1604 )
1605 {
1606 
1607 	mddb_recid_t	recid;
1608 	int		i, row, c, gotsomething;
1609 	mddb_type_t	typ1;
1610 	mddb_de_ic_t	*dep;
1611 	mddb_rb32_t	*rbp;
1612 	ms_unit32_od_t	*un32;
1613 	ms_unit_t	*un64;
1614 	md_dev64_t	self_devt;
1615 	minor_t		*self_id;	/* minor needs to be updated */
1616 	md_parent_t	*parent_id;	/* parent needs to be updated */
1617 	mddb_recid_t	*record_id;	/* record id needs to be updated */
1618 	mddb_recid_t	*hsp_id;
1619 	ms_comp32_od_t	*comp32;
1620 	ms_comp_t	*comp64;
1621 
1622 
1623 	gotsomething = 0;
1624 
1625 	typ1 = (mddb_type_t)md_getshared_key(setno,
1626 	    stripe_md_ops.md_driver.md_drivername);
1627 	recid = mddb_makerecid(setno, 0);
1628 
1629 	while ((recid = mddb_getnextrec(recid, typ1, 0)) > 0) {
1630 		if (mddb_getrecprivate(recid) & MD_PRV_GOTIT)
1631 			continue;
1632 
1633 		dep = mddb_getrecdep(recid);
1634 		rbp = dep->de_rb;
1635 
1636 		switch (rbp->rb_revision) {
1637 		case MDDB_REV_RB:
1638 		case MDDB_REV_RBFN:
1639 			/*
1640 			 * Small device
1641 			 */
1642 			un32 = (ms_unit32_od_t *)mddb_getrecaddr(recid);
1643 			self_id = &(un32->c.un_self_id);
1644 			parent_id = &(un32->c.un_parent);
1645 			record_id = &(un32->c.un_record_id);
1646 			hsp_id = &(un32->un_hsp_id);
1647 
1648 			comp32 = (ms_comp32_od_t *)((void *)&((char *)un32)
1649 				[un32->un_ocomp]);
1650 			for (row = 0; row < un32->un_nrows; row++) {
1651 			    struct ms_row32_od *mdr = &un32->un_row[row];
1652 			    for (i = 0, c = mdr->un_icomp;
1653 				i < mdr->un_ncomp; i++) {
1654 				ms_comp32_od_t *mdc;
1655 				mdc = &comp32[c++];
1656 
1657 				if (!md_update_minor(setno, mddb_getsidenum
1658 				    (setno), mdc->un_key))
1659 					goto out;
1660 
1661 				if (mdc->un_mirror.ms_hs_id != 0)
1662 				    mdc->un_mirror.ms_hs_id = MAKERECID(
1663 				    setno, mdc->un_mirror.ms_hs_id);
1664 			    }
1665 			}
1666 			break;
1667 		case MDDB_REV_RB64:
1668 		case MDDB_REV_RB64FN:
1669 			un64 = (ms_unit_t *)mddb_getrecaddr(recid);
1670 			self_id = &(un64->c.un_self_id);
1671 			parent_id = &(un64->c.un_parent);
1672 			record_id = &(un64->c.un_record_id);
1673 			hsp_id = &(un64->un_hsp_id);
1674 
1675 			comp64 = (ms_comp_t *)((void *)&((char *)un64)
1676 				[un64->un_ocomp]);
1677 			for (row = 0; row < un64->un_nrows; row++) {
1678 			    struct ms_row *mdr = &un64->un_row[row];
1679 			    for (i = 0, c = mdr->un_icomp;
1680 				i < mdr->un_ncomp; i++) {
1681 				ms_comp_t *mdc;
1682 				mdc = &comp64[c++];
1683 
1684 				if (!md_update_minor(setno, mddb_getsidenum
1685 				    (setno), mdc->un_key))
1686 					goto out;
1687 
1688 				if (mdc->un_mirror.ms_hs_id != 0)
1689 				    mdc->un_mirror.ms_hs_id = MAKERECID(
1690 				    setno, mdc->un_mirror.ms_hs_id);
1691 			    }
1692 			}
1693 			break;
1694 		}
1695 
1696 		/*
1697 		 * If this is a top level and a friendly name metadevice,
1698 		 * update its minor in the namespace.
1699 		 */
1700 		if ((*parent_id == MD_NO_PARENT) &&
1701 		    ((rbp->rb_revision == MDDB_REV_RBFN) ||
1702 		    (rbp->rb_revision == MDDB_REV_RB64FN))) {
1703 
1704 			self_devt = md_makedevice(md_major, *self_id);
1705 			if (!md_update_top_device_minor(setno,
1706 			    mddb_getsidenum(setno), self_devt))
1707 				goto out;
1708 		}
1709 
1710 		/*
1711 		 * Update unit with the imported setno
1712 		 *
1713 		 */
1714 		mddb_setrecprivate(recid, MD_PRV_GOTIT);
1715 
1716 		*self_id = MD_MKMIN(setno, MD_MIN2UNIT(*self_id));
1717 
1718 		if (*hsp_id != -1)
1719 			*hsp_id = MAKERECID(setno, DBID(*hsp_id));
1720 
1721 		if (*parent_id != MD_NO_PARENT)
1722 			*parent_id = MD_MKMIN(setno, MD_MIN2UNIT(*parent_id));
1723 		*record_id = MAKERECID(setno, DBID(*record_id));
1724 
1725 		gotsomething = 1;
1726 	}
1727 
1728 out:
1729 	return (gotsomething);
1730 }
1731 
1732 static md_named_services_t stripe_named_services[] = {
1733 	{stripe_shared_by_blk,			"shared by blk"		    },
1734 	{stripe_shared_by_indx,			"shared by indx"	    },
1735 	{stripe_component_count,		"get component count"	    },
1736 	{stripe_block_count_skip_size,		"get block count skip size" },
1737 	{stripe_get_dev,			"get device"		    },
1738 	{stripe_replace_dev,			"replace device"	    },
1739 	{stripe_hotspare_dev,			"hotspare device"	    },
1740 	{stripe_rename_check,			MDRNM_CHECK		    },
1741 	{NULL,					0}
1742 };
1743 
1744 md_ops_t stripe_md_ops = {
1745 	stripe_open,		/* open */
1746 	stripe_close,		/* close */
1747 	md_stripe_strategy,	/* strategy */
1748 	NULL,			/* print */
1749 	stripe_dump,		/* dump */
1750 	NULL,			/* read */
1751 	NULL,			/* write */
1752 	md_stripe_ioctl,	/* stripe_ioctl, */
1753 	stripe_snarf,		/* stripe_snarf */
1754 	stripe_halt,		/* stripe_halt */
1755 	NULL,			/* aread */
1756 	NULL,			/* awrite */
1757 	stripe_imp_set,		/* import set */
1758 	stripe_named_services
1759 };
1760 
1761 static void
1762 init_init()
1763 {
1764 	md_stripe_mcs_buf_off = sizeof (md_scs_t) - sizeof (buf_t);
1765 
1766 	stripe_parent_cache = kmem_cache_create("md_stripe_parent",
1767 	    sizeof (md_sps_t), 0, stripe_parent_constructor,
1768 	    stripe_parent_destructor, stripe_run_queue, NULL, NULL,
1769 	    0);
1770 	stripe_child_cache = kmem_cache_create("md_stripe_child",
1771 	    sizeof (md_scs_t) - sizeof (buf_t) + biosize(), 0,
1772 	    stripe_child_constructor, stripe_child_destructor,
1773 	    stripe_run_queue, NULL, NULL, 0);
1774 }
1775 
1776 static void
1777 fini_uninit()
1778 {
1779 	kmem_cache_destroy(stripe_parent_cache);
1780 	kmem_cache_destroy(stripe_child_cache);
1781 	stripe_parent_cache = stripe_child_cache = NULL;
1782 }
1783 
1784 /* define the module linkage */
1785 MD_PLUGIN_MISC_MODULE("stripes module %I%", init_init(), fini_uninit())
1786