xref: /titanic_52/usr/src/uts/common/io/devinfo.c (revision e98fafb9956429b59c817d4fbd27720c73879203)
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 2007 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  * driver for accessing kernel devinfo tree.
30  */
31 #include <sys/types.h>
32 #include <sys/pathname.h>
33 #include <sys/debug.h>
34 #include <sys/autoconf.h>
35 #include <sys/conf.h>
36 #include <sys/file.h>
37 #include <sys/kmem.h>
38 #include <sys/modctl.h>
39 #include <sys/stat.h>
40 #include <sys/ddi.h>
41 #include <sys/sunddi.h>
42 #include <sys/sunldi_impl.h>
43 #include <sys/sunndi.h>
44 #include <sys/esunddi.h>
45 #include <sys/sunmdi.h>
46 #include <sys/ddi_impldefs.h>
47 #include <sys/ndi_impldefs.h>
48 #include <sys/mdi_impldefs.h>
49 #include <sys/devinfo_impl.h>
50 #include <sys/thread.h>
51 #include <sys/modhash.h>
52 #include <sys/bitmap.h>
53 #include <util/qsort.h>
54 #include <sys/disp.h>
55 #include <sys/kobj.h>
56 #include <sys/crc32.h>
57 
58 
59 #ifdef DEBUG
60 static int di_debug;
61 #define	dcmn_err(args) if (di_debug >= 1) cmn_err args
62 #define	dcmn_err2(args) if (di_debug >= 2) cmn_err args
63 #define	dcmn_err3(args) if (di_debug >= 3) cmn_err args
64 #else
65 #define	dcmn_err(args) /* nothing */
66 #define	dcmn_err2(args) /* nothing */
67 #define	dcmn_err3(args) /* nothing */
68 #endif
69 
70 /*
71  * We partition the space of devinfo minor nodes equally between the full and
72  * unprivileged versions of the driver.  The even-numbered minor nodes are the
73  * full version, while the odd-numbered ones are the read-only version.
74  */
75 static int di_max_opens = 32;
76 
77 #define	DI_FULL_PARENT		0
78 #define	DI_READONLY_PARENT	1
79 #define	DI_NODE_SPECIES		2
80 #define	DI_UNPRIVILEGED_NODE(x)	(((x) % 2) != 0)
81 
82 #define	IOC_IDLE	0	/* snapshot ioctl states */
83 #define	IOC_SNAP	1	/* snapshot in progress */
84 #define	IOC_DONE	2	/* snapshot done, but not copied out */
85 #define	IOC_COPY	3	/* copyout in progress */
86 
87 /*
88  * Keep max alignment so we can move snapshot to different platforms
89  */
90 #define	DI_ALIGN(addr)	((addr + 7l) & ~7l)
91 
92 /*
93  * To avoid wasting memory, make a linked list of memory chunks.
94  * Size of each chunk is buf_size.
95  */
96 struct di_mem {
97 	struct di_mem *next;	/* link to next chunk */
98 	char *buf;		/* contiguous kernel memory */
99 	size_t buf_size;	/* size of buf in bytes */
100 	devmap_cookie_t cook;	/* cookie from ddi_umem_alloc */
101 };
102 
103 /*
104  * This is a stack for walking the tree without using recursion.
105  * When the devinfo tree height is above some small size, one
106  * gets watchdog resets on sun4m.
107  */
108 struct di_stack {
109 	void		*offset[MAX_TREE_DEPTH];
110 	struct dev_info *dip[MAX_TREE_DEPTH];
111 	int		circ[MAX_TREE_DEPTH];
112 	int		depth;	/* depth of current node to be copied */
113 };
114 
115 #define	TOP_OFFSET(stack)	\
116 	((di_off_t *)(stack)->offset[(stack)->depth - 1])
117 #define	TOP_NODE(stack)		\
118 	((stack)->dip[(stack)->depth - 1])
119 #define	PARENT_OFFSET(stack)	\
120 	((di_off_t *)(stack)->offset[(stack)->depth - 2])
121 #define	EMPTY_STACK(stack)	((stack)->depth == 0)
122 #define	POP_STACK(stack)	{ \
123 	ndi_devi_exit((dev_info_t *)TOP_NODE(stack), \
124 		(stack)->circ[(stack)->depth - 1]); \
125 	((stack)->depth--); \
126 }
127 #define	PUSH_STACK(stack, node, offp)	{ \
128 	ASSERT(node != NULL); \
129 	ndi_devi_enter((dev_info_t *)node, &(stack)->circ[(stack)->depth]); \
130 	(stack)->dip[(stack)->depth] = (node); \
131 	(stack)->offset[(stack)->depth] = (void *)(offp); \
132 	((stack)->depth)++; \
133 }
134 
135 #define	DI_ALL_PTR(s)	((struct di_all *)(intptr_t)di_mem_addr((s), 0))
136 
137 /*
138  * With devfs, the device tree has no global locks. The device tree is
139  * dynamic and dips may come and go if they are not locked locally. Under
140  * these conditions, pointers are no longer reliable as unique IDs.
141  * Specifically, these pointers cannot be used as keys for hash tables
142  * as the same devinfo structure may be freed in one part of the tree only
143  * to be allocated as the structure for a different device in another
144  * part of the tree. This can happen if DR and the snapshot are
145  * happening concurrently.
146  * The following data structures act as keys for devinfo nodes and
147  * pathinfo nodes.
148  */
149 
150 enum di_ktype {
151 	DI_DKEY = 1,
152 	DI_PKEY = 2
153 };
154 
155 struct di_dkey {
156 	dev_info_t	*dk_dip;
157 	major_t		dk_major;
158 	int		dk_inst;
159 	pnode_t		dk_nodeid;
160 };
161 
162 struct di_pkey {
163 	mdi_pathinfo_t	*pk_pip;
164 	char		*pk_path_addr;
165 	dev_info_t	*pk_client;
166 	dev_info_t	*pk_phci;
167 };
168 
169 struct di_key {
170 	enum di_ktype	k_type;
171 	union {
172 		struct di_dkey dkey;
173 		struct di_pkey pkey;
174 	} k_u;
175 };
176 
177 
178 struct i_lnode;
179 
180 typedef struct i_link {
181 	/*
182 	 * If a di_link struct representing this i_link struct makes it
183 	 * into the snapshot, then self will point to the offset of
184 	 * the di_link struct in the snapshot
185 	 */
186 	di_off_t	self;
187 
188 	int		spec_type;	/* block or char access type */
189 	struct i_lnode	*src_lnode;	/* src i_lnode */
190 	struct i_lnode	*tgt_lnode;	/* tgt i_lnode */
191 	struct i_link	*src_link_next;	/* next src i_link /w same i_lnode */
192 	struct i_link	*tgt_link_next;	/* next tgt i_link /w same i_lnode */
193 } i_link_t;
194 
195 typedef struct i_lnode {
196 	/*
197 	 * If a di_lnode struct representing this i_lnode struct makes it
198 	 * into the snapshot, then self will point to the offset of
199 	 * the di_lnode struct in the snapshot
200 	 */
201 	di_off_t	self;
202 
203 	/*
204 	 * used for hashing and comparing i_lnodes
205 	 */
206 	int		modid;
207 
208 	/*
209 	 * public information describing a link endpoint
210 	 */
211 	struct di_node	*di_node;	/* di_node in snapshot */
212 	dev_t		devt;		/* devt */
213 
214 	/*
215 	 * i_link ptr to links coming into this i_lnode node
216 	 * (this i_lnode is the target of these i_links)
217 	 */
218 	i_link_t	*link_in;
219 
220 	/*
221 	 * i_link ptr to links going out of this i_lnode node
222 	 * (this i_lnode is the source of these i_links)
223 	 */
224 	i_link_t	*link_out;
225 } i_lnode_t;
226 
227 /*
228  * Soft state associated with each instance of driver open.
229  */
230 static struct di_state {
231 	di_off_t mem_size;	/* total # bytes in memlist	*/
232 	struct di_mem *memlist;	/* head of memlist		*/
233 	uint_t command;		/* command from ioctl		*/
234 	int di_iocstate;	/* snapshot ioctl state		*/
235 	mod_hash_t *reg_dip_hash;
236 	mod_hash_t *reg_pip_hash;
237 	int lnode_count;
238 	int link_count;
239 
240 	mod_hash_t *lnode_hash;
241 	mod_hash_t *link_hash;
242 } **di_states;
243 
244 static kmutex_t di_lock;	/* serialize instance assignment */
245 
246 typedef enum {
247 	DI_QUIET = 0,	/* DI_QUIET must always be 0 */
248 	DI_ERR,
249 	DI_INFO,
250 	DI_TRACE,
251 	DI_TRACE1,
252 	DI_TRACE2
253 } di_cache_debug_t;
254 
255 static uint_t	di_chunk = 32;		/* I/O chunk size in pages */
256 
257 #define	DI_CACHE_LOCK(c)	(mutex_enter(&(c).cache_lock))
258 #define	DI_CACHE_UNLOCK(c)	(mutex_exit(&(c).cache_lock))
259 #define	DI_CACHE_LOCKED(c)	(mutex_owned(&(c).cache_lock))
260 
261 /*
262  * Check that whole device tree is being configured as a pre-condition for
263  * cleaning up /etc/devices files.
264  */
265 #define	DEVICES_FILES_CLEANABLE(st)	\
266 	(((st)->command & DINFOSUBTREE) && ((st)->command & DINFOFORCE) && \
267 	strcmp(DI_ALL_PTR(st)->root_path, "/") == 0)
268 
269 #define	CACHE_DEBUG(args)	\
270 	{ if (di_cache_debug != DI_QUIET) di_cache_print args; }
271 
272 typedef struct phci_walk_arg {
273 	di_off_t	off;
274 	struct di_state	*st;
275 } phci_walk_arg_t;
276 
277 static int di_open(dev_t *, int, int, cred_t *);
278 static int di_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
279 static int di_close(dev_t, int, int, cred_t *);
280 static int di_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
281 static int di_attach(dev_info_t *, ddi_attach_cmd_t);
282 static int di_detach(dev_info_t *, ddi_detach_cmd_t);
283 
284 static di_off_t di_copyformat(di_off_t, struct di_state *, intptr_t, int);
285 static di_off_t di_snapshot_and_clean(struct di_state *);
286 static di_off_t di_copydevnm(di_off_t *, struct di_state *);
287 static di_off_t di_copytree(struct dev_info *, di_off_t *, struct di_state *);
288 static di_off_t di_copynode(struct di_stack *, struct di_state *);
289 static di_off_t di_getmdata(struct ddi_minor_data *, di_off_t *, di_off_t,
290     struct di_state *);
291 static di_off_t di_getppdata(struct dev_info *, di_off_t *, struct di_state *);
292 static di_off_t di_getdpdata(struct dev_info *, di_off_t *, struct di_state *);
293 static di_off_t di_getprop(struct ddi_prop *, di_off_t *,
294     struct di_state *, struct dev_info *, int);
295 static void di_allocmem(struct di_state *, size_t);
296 static void di_freemem(struct di_state *);
297 static void di_copymem(struct di_state *st, caddr_t buf, size_t bufsiz);
298 static di_off_t di_checkmem(struct di_state *, di_off_t, size_t);
299 static caddr_t di_mem_addr(struct di_state *, di_off_t);
300 static int di_setstate(struct di_state *, int);
301 static void di_register_dip(struct di_state *, dev_info_t *, di_off_t);
302 static void di_register_pip(struct di_state *, mdi_pathinfo_t *, di_off_t);
303 static di_off_t di_getpath_data(dev_info_t *, di_off_t *, di_off_t,
304     struct di_state *, int);
305 static di_off_t di_getlink_data(di_off_t, struct di_state *);
306 static int di_dip_find(struct di_state *st, dev_info_t *node, di_off_t *off_p);
307 
308 static int cache_args_valid(struct di_state *st, int *error);
309 static int snapshot_is_cacheable(struct di_state *st);
310 static int di_cache_lookup(struct di_state *st);
311 static int di_cache_update(struct di_state *st);
312 static void di_cache_print(di_cache_debug_t msglevel, char *fmt, ...);
313 int build_vhci_list(dev_info_t *vh_devinfo, void *arg);
314 int build_phci_list(dev_info_t *ph_devinfo, void *arg);
315 
316 static struct cb_ops di_cb_ops = {
317 	di_open,		/* open */
318 	di_close,		/* close */
319 	nodev,			/* strategy */
320 	nodev,			/* print */
321 	nodev,			/* dump */
322 	nodev,			/* read */
323 	nodev,			/* write */
324 	di_ioctl,		/* ioctl */
325 	nodev,			/* devmap */
326 	nodev,			/* mmap */
327 	nodev,			/* segmap */
328 	nochpoll,		/* poll */
329 	ddi_prop_op,		/* prop_op */
330 	NULL,			/* streamtab  */
331 	D_NEW | D_MP		/* Driver compatibility flag */
332 };
333 
334 static struct dev_ops di_ops = {
335 	DEVO_REV,		/* devo_rev, */
336 	0,			/* refcnt  */
337 	di_info,		/* info */
338 	nulldev,		/* identify */
339 	nulldev,		/* probe */
340 	di_attach,		/* attach */
341 	di_detach,		/* detach */
342 	nodev,			/* reset */
343 	&di_cb_ops,		/* driver operations */
344 	NULL			/* bus operations */
345 };
346 
347 /*
348  * Module linkage information for the kernel.
349  */
350 static struct modldrv modldrv = {
351 	&mod_driverops,
352 	"DEVINFO Driver %I%",
353 	&di_ops
354 };
355 
356 static struct modlinkage modlinkage = {
357 	MODREV_1,
358 	&modldrv,
359 	NULL
360 };
361 
362 int
363 _init(void)
364 {
365 	int	error;
366 
367 	mutex_init(&di_lock, NULL, MUTEX_DRIVER, NULL);
368 
369 	error = mod_install(&modlinkage);
370 	if (error != 0) {
371 		mutex_destroy(&di_lock);
372 		return (error);
373 	}
374 
375 	return (0);
376 }
377 
378 int
379 _info(struct modinfo *modinfop)
380 {
381 	return (mod_info(&modlinkage, modinfop));
382 }
383 
384 int
385 _fini(void)
386 {
387 	int	error;
388 
389 	error = mod_remove(&modlinkage);
390 	if (error != 0) {
391 		return (error);
392 	}
393 
394 	mutex_destroy(&di_lock);
395 	return (0);
396 }
397 
398 static dev_info_t *di_dip;
399 
400 /*ARGSUSED*/
401 static int
402 di_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
403 {
404 	int error = DDI_FAILURE;
405 
406 	switch (infocmd) {
407 	case DDI_INFO_DEVT2DEVINFO:
408 		*result = (void *)di_dip;
409 		error = DDI_SUCCESS;
410 		break;
411 	case DDI_INFO_DEVT2INSTANCE:
412 		/*
413 		 * All dev_t's map to the same, single instance.
414 		 */
415 		*result = (void *)0;
416 		error = DDI_SUCCESS;
417 		break;
418 	default:
419 		break;
420 	}
421 
422 	return (error);
423 }
424 
425 static int
426 di_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
427 {
428 	int error = DDI_FAILURE;
429 
430 	switch (cmd) {
431 	case DDI_ATTACH:
432 		di_states = kmem_zalloc(
433 		    di_max_opens * sizeof (struct di_state *), KM_SLEEP);
434 
435 		if (ddi_create_minor_node(dip, "devinfo", S_IFCHR,
436 		    DI_FULL_PARENT, DDI_PSEUDO, NULL) == DDI_FAILURE ||
437 		    ddi_create_minor_node(dip, "devinfo,ro", S_IFCHR,
438 		    DI_READONLY_PARENT, DDI_PSEUDO, NULL) == DDI_FAILURE) {
439 			kmem_free(di_states,
440 			    di_max_opens * sizeof (struct di_state *));
441 			ddi_remove_minor_node(dip, NULL);
442 			error = DDI_FAILURE;
443 		} else {
444 			di_dip = dip;
445 			ddi_report_dev(dip);
446 
447 			error = DDI_SUCCESS;
448 		}
449 		break;
450 	default:
451 		error = DDI_FAILURE;
452 		break;
453 	}
454 
455 	return (error);
456 }
457 
458 static int
459 di_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
460 {
461 	int error = DDI_FAILURE;
462 
463 	switch (cmd) {
464 	case DDI_DETACH:
465 		ddi_remove_minor_node(dip, NULL);
466 		di_dip = NULL;
467 		kmem_free(di_states, di_max_opens * sizeof (struct di_state *));
468 
469 		error = DDI_SUCCESS;
470 		break;
471 	default:
472 		error = DDI_FAILURE;
473 		break;
474 	}
475 
476 	return (error);
477 }
478 
479 /*
480  * Allow multiple opens by tweaking the dev_t such that it looks like each
481  * open is getting a different minor device.  Each minor gets a separate
482  * entry in the di_states[] table.  Based on the original minor number, we
483  * discriminate opens of the full and read-only nodes.  If all of the instances
484  * of the selected minor node are currently open, we return EAGAIN.
485  */
486 /*ARGSUSED*/
487 static int
488 di_open(dev_t *devp, int flag, int otyp, cred_t *credp)
489 {
490 	int m;
491 	minor_t minor_parent = getminor(*devp);
492 
493 	if (minor_parent != DI_FULL_PARENT &&
494 	    minor_parent != DI_READONLY_PARENT)
495 		return (ENXIO);
496 
497 	mutex_enter(&di_lock);
498 
499 	for (m = minor_parent; m < di_max_opens; m += DI_NODE_SPECIES) {
500 		if (di_states[m] != NULL)
501 			continue;
502 
503 		di_states[m] = kmem_zalloc(sizeof (struct di_state), KM_SLEEP);
504 		break;	/* It's ours. */
505 	}
506 
507 	if (m >= di_max_opens) {
508 		/*
509 		 * maximum open instance for device reached
510 		 */
511 		mutex_exit(&di_lock);
512 		dcmn_err((CE_WARN, "devinfo: maximum devinfo open reached"));
513 		return (EAGAIN);
514 	}
515 	mutex_exit(&di_lock);
516 
517 	ASSERT(m < di_max_opens);
518 	*devp = makedevice(getmajor(*devp), (minor_t)(m + DI_NODE_SPECIES));
519 
520 	dcmn_err((CE_CONT, "di_open: thread = %p, assigned minor = %d\n",
521 	    (void *)curthread, m + DI_NODE_SPECIES));
522 
523 	return (0);
524 }
525 
526 /*ARGSUSED*/
527 static int
528 di_close(dev_t dev, int flag, int otype, cred_t *cred_p)
529 {
530 	struct di_state *st;
531 	int m = (int)getminor(dev) - DI_NODE_SPECIES;
532 
533 	if (m < 0) {
534 		cmn_err(CE_WARN, "closing non-existent devinfo minor %d",
535 		    m + DI_NODE_SPECIES);
536 		return (ENXIO);
537 	}
538 
539 	st = di_states[m];
540 	ASSERT(m < di_max_opens && st != NULL);
541 
542 	di_freemem(st);
543 	kmem_free(st, sizeof (struct di_state));
544 
545 	/*
546 	 * empty slot in state table
547 	 */
548 	mutex_enter(&di_lock);
549 	di_states[m] = NULL;
550 	dcmn_err((CE_CONT, "di_close: thread = %p, assigned minor = %d\n",
551 	    (void *)curthread, m + DI_NODE_SPECIES));
552 	mutex_exit(&di_lock);
553 
554 	return (0);
555 }
556 
557 
558 /*ARGSUSED*/
559 static int
560 di_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
561 {
562 	int rv, error;
563 	di_off_t off;
564 	struct di_all *all;
565 	struct di_state *st;
566 	int m = (int)getminor(dev) - DI_NODE_SPECIES;
567 
568 	major_t i;
569 	char *drv_name;
570 	size_t map_size, size;
571 	struct di_mem *dcp;
572 	int ndi_flags;
573 
574 	if (m < 0 || m >= di_max_opens) {
575 		return (ENXIO);
576 	}
577 
578 	st = di_states[m];
579 	ASSERT(st != NULL);
580 
581 	dcmn_err2((CE_CONT, "di_ioctl: mode = %x, cmd = %x\n", mode, cmd));
582 
583 	switch (cmd) {
584 	case DINFOIDENT:
585 		/*
586 		 * This is called from di_init to verify that the driver
587 		 * opened is indeed devinfo. The purpose is to guard against
588 		 * sending ioctl to an unknown driver in case of an
589 		 * unresolved major number conflict during bfu.
590 		 */
591 		*rvalp = DI_MAGIC;
592 		return (0);
593 
594 	case DINFOLODRV:
595 		/*
596 		 * Hold an installed driver and return the result
597 		 */
598 		if (DI_UNPRIVILEGED_NODE(m)) {
599 			/*
600 			 * Only the fully enabled instances may issue
601 			 * DINFOLDDRV.
602 			 */
603 			return (EACCES);
604 		}
605 
606 		drv_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
607 		if (ddi_copyin((void *)arg, drv_name, MAXNAMELEN, mode) != 0) {
608 			kmem_free(drv_name, MAXNAMELEN);
609 			return (EFAULT);
610 		}
611 
612 		/*
613 		 * Some 3rd party driver's _init() walks the device tree,
614 		 * so we load the driver module before configuring driver.
615 		 */
616 		i = ddi_name_to_major(drv_name);
617 		if (ddi_hold_driver(i) == NULL) {
618 			kmem_free(drv_name, MAXNAMELEN);
619 			return (ENXIO);
620 		}
621 
622 		ndi_flags = NDI_DEVI_PERSIST | NDI_CONFIG | NDI_NO_EVENT;
623 
624 		/*
625 		 * i_ddi_load_drvconf() below will trigger a reprobe
626 		 * via reset_nexus_flags(). NDI_DRV_CONF_REPROBE isn't
627 		 * needed here.
628 		 */
629 		modunload_disable();
630 		(void) i_ddi_load_drvconf(i);
631 		(void) ndi_devi_config_driver(ddi_root_node(), ndi_flags, i);
632 		kmem_free(drv_name, MAXNAMELEN);
633 		ddi_rele_driver(i);
634 		rv = i_ddi_devs_attached(i);
635 		modunload_enable();
636 
637 		i_ddi_di_cache_invalidate(KM_SLEEP);
638 
639 		return ((rv == DDI_SUCCESS)? 0 : ENXIO);
640 
641 	case DINFOUSRLD:
642 		/*
643 		 * The case for copying snapshot to userland
644 		 */
645 		if (di_setstate(st, IOC_COPY) == -1)
646 			return (EBUSY);
647 
648 		map_size = ((struct di_all *)
649 		    (intptr_t)di_mem_addr(st, 0))->map_size;
650 		if (map_size == 0) {
651 			(void) di_setstate(st, IOC_DONE);
652 			return (EFAULT);
653 		}
654 
655 		/*
656 		 * copyout the snapshot
657 		 */
658 		map_size = (map_size + PAGEOFFSET) & PAGEMASK;
659 
660 		/*
661 		 * Return the map size, so caller may do a sanity
662 		 * check against the return value of snapshot ioctl()
663 		 */
664 		*rvalp = (int)map_size;
665 
666 		/*
667 		 * Copy one chunk at a time
668 		 */
669 		off = 0;
670 		dcp = st->memlist;
671 		while (map_size) {
672 			size = dcp->buf_size;
673 			if (map_size <= size) {
674 				size = map_size;
675 			}
676 
677 			if (ddi_copyout(di_mem_addr(st, off),
678 			    (void *)(arg + off), size, mode) != 0) {
679 				(void) di_setstate(st, IOC_DONE);
680 				return (EFAULT);
681 			}
682 
683 			map_size -= size;
684 			off += size;
685 			dcp = dcp->next;
686 		}
687 
688 		di_freemem(st);
689 		(void) di_setstate(st, IOC_IDLE);
690 		return (0);
691 
692 	default:
693 		if ((cmd & ~DIIOC_MASK) != DIIOC) {
694 			/*
695 			 * Invalid ioctl command
696 			 */
697 			return (ENOTTY);
698 		}
699 		/*
700 		 * take a snapshot
701 		 */
702 		st->command = cmd & DIIOC_MASK;
703 		/*FALLTHROUGH*/
704 	}
705 
706 	/*
707 	 * Obtain enough memory to hold header + rootpath.  We prevent kernel
708 	 * memory exhaustion by freeing any previously allocated snapshot and
709 	 * refusing the operation; otherwise we would be allowing ioctl(),
710 	 * ioctl(), ioctl(), ..., panic.
711 	 */
712 	if (di_setstate(st, IOC_SNAP) == -1)
713 		return (EBUSY);
714 
715 	size = sizeof (struct di_all) +
716 	    sizeof (((struct dinfo_io *)(NULL))->root_path);
717 	if (size < PAGESIZE)
718 		size = PAGESIZE;
719 	di_allocmem(st, size);
720 
721 	all = (struct di_all *)(intptr_t)di_mem_addr(st, 0);
722 	all->devcnt = devcnt;
723 	all->command = st->command;
724 	all->version = DI_SNAPSHOT_VERSION;
725 	all->top_vhci_devinfo = 0;	/* filled up by build_vhci_list. */
726 
727 	/*
728 	 * Note the endianness in case we need to transport snapshot
729 	 * over the network.
730 	 */
731 #if defined(_LITTLE_ENDIAN)
732 	all->endianness = DI_LITTLE_ENDIAN;
733 #else
734 	all->endianness = DI_BIG_ENDIAN;
735 #endif
736 
737 	/* Copyin ioctl args, store in the snapshot. */
738 	if (copyinstr((void *)arg, all->root_path,
739 	    sizeof (((struct dinfo_io *)(NULL))->root_path), &size) != 0) {
740 		di_freemem(st);
741 		(void) di_setstate(st, IOC_IDLE);
742 		return (EFAULT);
743 	}
744 
745 	if ((st->command & DINFOCLEANUP) && !DEVICES_FILES_CLEANABLE(st)) {
746 		di_freemem(st);
747 		(void) di_setstate(st, IOC_IDLE);
748 		return (EINVAL);
749 	}
750 
751 	error = 0;
752 	if ((st->command & DINFOCACHE) && !cache_args_valid(st, &error)) {
753 		di_freemem(st);
754 		(void) di_setstate(st, IOC_IDLE);
755 		return (error);
756 	}
757 
758 	off = DI_ALIGN(sizeof (struct di_all) + size);
759 
760 	/*
761 	 * Only the fully enabled version may force load drivers or read
762 	 * the parent private data from a driver.
763 	 */
764 	if ((st->command & (DINFOPRIVDATA | DINFOFORCE)) != 0 &&
765 	    DI_UNPRIVILEGED_NODE(m)) {
766 		di_freemem(st);
767 		(void) di_setstate(st, IOC_IDLE);
768 		return (EACCES);
769 	}
770 
771 	/* Do we need private data? */
772 	if (st->command & DINFOPRIVDATA) {
773 		arg += sizeof (((struct dinfo_io *)(NULL))->root_path);
774 
775 #ifdef _MULTI_DATAMODEL
776 		switch (ddi_model_convert_from(mode & FMODELS)) {
777 		case DDI_MODEL_ILP32: {
778 			/*
779 			 * Cannot copy private data from 64-bit kernel
780 			 * to 32-bit app
781 			 */
782 			di_freemem(st);
783 			(void) di_setstate(st, IOC_IDLE);
784 			return (EINVAL);
785 		}
786 		case DDI_MODEL_NONE:
787 			if ((off = di_copyformat(off, st, arg, mode)) == 0) {
788 				di_freemem(st);
789 				(void) di_setstate(st, IOC_IDLE);
790 				return (EFAULT);
791 			}
792 			break;
793 		}
794 #else /* !_MULTI_DATAMODEL */
795 		if ((off = di_copyformat(off, st, arg, mode)) == 0) {
796 			di_freemem(st);
797 			(void) di_setstate(st, IOC_IDLE);
798 			return (EFAULT);
799 		}
800 #endif /* _MULTI_DATAMODEL */
801 	}
802 
803 	all->top_devinfo = DI_ALIGN(off);
804 
805 	/*
806 	 * For cache lookups we reallocate memory from scratch,
807 	 * so the value of "all" is no longer valid.
808 	 */
809 	all = NULL;
810 
811 	if (st->command & DINFOCACHE) {
812 		*rvalp = di_cache_lookup(st);
813 	} else if (snapshot_is_cacheable(st)) {
814 		DI_CACHE_LOCK(di_cache);
815 		*rvalp = di_cache_update(st);
816 		DI_CACHE_UNLOCK(di_cache);
817 	} else
818 		*rvalp = di_snapshot_and_clean(st);
819 
820 	if (*rvalp) {
821 		DI_ALL_PTR(st)->map_size = *rvalp;
822 		(void) di_setstate(st, IOC_DONE);
823 	} else {
824 		di_freemem(st);
825 		(void) di_setstate(st, IOC_IDLE);
826 	}
827 
828 	return (0);
829 }
830 
831 /*
832  * Get a chunk of memory >= size, for the snapshot
833  */
834 static void
835 di_allocmem(struct di_state *st, size_t size)
836 {
837 	struct di_mem *mem = kmem_zalloc(sizeof (struct di_mem),
838 	    KM_SLEEP);
839 	/*
840 	 * Round up size to nearest power of 2. If it is less
841 	 * than st->mem_size, set it to st->mem_size (i.e.,
842 	 * the mem_size is doubled every time) to reduce the
843 	 * number of memory allocations.
844 	 */
845 	size_t tmp = 1;
846 	while (tmp < size) {
847 		tmp <<= 1;
848 	}
849 	size = (tmp > st->mem_size) ? tmp : st->mem_size;
850 
851 	mem->buf = ddi_umem_alloc(size, DDI_UMEM_SLEEP, &mem->cook);
852 	mem->buf_size = size;
853 
854 	dcmn_err2((CE_CONT, "di_allocmem: mem_size=%x\n", st->mem_size));
855 
856 	if (st->mem_size == 0) {	/* first chunk */
857 		st->memlist = mem;
858 	} else {
859 		/*
860 		 * locate end of linked list and add a chunk at the end
861 		 */
862 		struct di_mem *dcp = st->memlist;
863 		while (dcp->next != NULL) {
864 			dcp = dcp->next;
865 		}
866 
867 		dcp->next = mem;
868 	}
869 
870 	st->mem_size += size;
871 }
872 
873 /*
874  * Copy upto bufsiz bytes of the memlist to buf
875  */
876 static void
877 di_copymem(struct di_state *st, caddr_t buf, size_t bufsiz)
878 {
879 	struct di_mem *dcp;
880 	size_t copysz;
881 
882 	if (st->mem_size == 0) {
883 		ASSERT(st->memlist == NULL);
884 		return;
885 	}
886 
887 	copysz = 0;
888 	for (dcp = st->memlist; dcp; dcp = dcp->next) {
889 
890 		ASSERT(bufsiz > 0);
891 
892 		if (bufsiz <= dcp->buf_size)
893 			copysz = bufsiz;
894 		else
895 			copysz = dcp->buf_size;
896 
897 		bcopy(dcp->buf, buf, copysz);
898 
899 		buf += copysz;
900 		bufsiz -= copysz;
901 
902 		if (bufsiz == 0)
903 			break;
904 	}
905 }
906 
907 /*
908  * Free all memory for the snapshot
909  */
910 static void
911 di_freemem(struct di_state *st)
912 {
913 	struct di_mem *dcp, *tmp;
914 
915 	dcmn_err2((CE_CONT, "di_freemem\n"));
916 
917 	if (st->mem_size) {
918 		dcp = st->memlist;
919 		while (dcp) {	/* traverse the linked list */
920 			tmp = dcp;
921 			dcp = dcp->next;
922 			ddi_umem_free(tmp->cook);
923 			kmem_free(tmp, sizeof (struct di_mem));
924 		}
925 		st->mem_size = 0;
926 		st->memlist = NULL;
927 	}
928 
929 	ASSERT(st->mem_size == 0);
930 	ASSERT(st->memlist == NULL);
931 }
932 
933 /*
934  * Copies cached data to the di_state structure.
935  * Returns:
936  *	- size of data copied, on SUCCESS
937  *	- 0 on failure
938  */
939 static int
940 di_cache2mem(struct di_cache *cache, struct di_state *st)
941 {
942 	caddr_t	pa;
943 
944 	ASSERT(st->mem_size == 0);
945 	ASSERT(st->memlist == NULL);
946 	ASSERT(!servicing_interrupt());
947 	ASSERT(DI_CACHE_LOCKED(*cache));
948 
949 	if (cache->cache_size == 0) {
950 		ASSERT(cache->cache_data == NULL);
951 		CACHE_DEBUG((DI_ERR, "Empty cache. Skipping copy"));
952 		return (0);
953 	}
954 
955 	ASSERT(cache->cache_data);
956 
957 	di_allocmem(st, cache->cache_size);
958 
959 	pa = di_mem_addr(st, 0);
960 
961 	ASSERT(pa);
962 
963 	/*
964 	 * Verify that di_allocmem() allocates contiguous memory,
965 	 * so that it is safe to do straight bcopy()
966 	 */
967 	ASSERT(st->memlist != NULL);
968 	ASSERT(st->memlist->next == NULL);
969 	bcopy(cache->cache_data, pa, cache->cache_size);
970 
971 	return (cache->cache_size);
972 }
973 
974 /*
975  * Copies a snapshot from di_state to the cache
976  * Returns:
977  *	- 0 on failure
978  *	- size of copied data on success
979  */
980 static size_t
981 di_mem2cache(struct di_state *st, struct di_cache *cache)
982 {
983 	size_t map_size;
984 
985 	ASSERT(cache->cache_size == 0);
986 	ASSERT(cache->cache_data == NULL);
987 	ASSERT(!servicing_interrupt());
988 	ASSERT(DI_CACHE_LOCKED(*cache));
989 
990 	if (st->mem_size == 0) {
991 		ASSERT(st->memlist == NULL);
992 		CACHE_DEBUG((DI_ERR, "Empty memlist. Skipping copy"));
993 		return (0);
994 	}
995 
996 	ASSERT(st->memlist);
997 
998 	/*
999 	 * The size of the memory list may be much larger than the
1000 	 * size of valid data (map_size). Cache only the valid data
1001 	 */
1002 	map_size = DI_ALL_PTR(st)->map_size;
1003 	if (map_size == 0 || map_size < sizeof (struct di_all) ||
1004 	    map_size > st->mem_size) {
1005 		CACHE_DEBUG((DI_ERR, "cannot cache: bad size: 0x%x", map_size));
1006 		return (0);
1007 	}
1008 
1009 	cache->cache_data = kmem_alloc(map_size, KM_SLEEP);
1010 	cache->cache_size = map_size;
1011 	di_copymem(st, cache->cache_data, cache->cache_size);
1012 
1013 	return (map_size);
1014 }
1015 
1016 /*
1017  * Make sure there is at least "size" bytes memory left before
1018  * going on. Otherwise, start on a new chunk.
1019  */
1020 static di_off_t
1021 di_checkmem(struct di_state *st, di_off_t off, size_t size)
1022 {
1023 	dcmn_err3((CE_CONT, "di_checkmem: off=%x size=%x\n",
1024 	    off, (int)size));
1025 
1026 	/*
1027 	 * di_checkmem() shouldn't be called with a size of zero.
1028 	 * But in case it is, we want to make sure we return a valid
1029 	 * offset within the memlist and not an offset that points us
1030 	 * at the end of the memlist.
1031 	 */
1032 	if (size == 0) {
1033 		dcmn_err((CE_WARN, "di_checkmem: invalid zero size used"));
1034 		size = 1;
1035 	}
1036 
1037 	off = DI_ALIGN(off);
1038 	if ((st->mem_size - off) < size) {
1039 		off = st->mem_size;
1040 		di_allocmem(st, size);
1041 	}
1042 
1043 	return (off);
1044 }
1045 
1046 /*
1047  * Copy the private data format from ioctl arg.
1048  * On success, the ending offset is returned. On error 0 is returned.
1049  */
1050 static di_off_t
1051 di_copyformat(di_off_t off, struct di_state *st, intptr_t arg, int mode)
1052 {
1053 	di_off_t size;
1054 	struct di_priv_data *priv;
1055 	struct di_all *all = (struct di_all *)(intptr_t)di_mem_addr(st, 0);
1056 
1057 	dcmn_err2((CE_CONT, "di_copyformat: off=%x, arg=%p mode=%x\n",
1058 	    off, (void *)arg, mode));
1059 
1060 	/*
1061 	 * Copyin data and check version.
1062 	 * We only handle private data version 0.
1063 	 */
1064 	priv = kmem_alloc(sizeof (struct di_priv_data), KM_SLEEP);
1065 	if ((ddi_copyin((void *)arg, priv, sizeof (struct di_priv_data),
1066 	    mode) != 0) || (priv->version != DI_PRIVDATA_VERSION_0)) {
1067 		kmem_free(priv, sizeof (struct di_priv_data));
1068 		return (0);
1069 	}
1070 
1071 	/*
1072 	 * Save di_priv_data copied from userland in snapshot.
1073 	 */
1074 	all->pd_version = priv->version;
1075 	all->n_ppdata = priv->n_parent;
1076 	all->n_dpdata = priv->n_driver;
1077 
1078 	/*
1079 	 * copyin private data format, modify offset accordingly
1080 	 */
1081 	if (all->n_ppdata) {	/* parent private data format */
1082 		/*
1083 		 * check memory
1084 		 */
1085 		size = all->n_ppdata * sizeof (struct di_priv_format);
1086 		off = di_checkmem(st, off, size);
1087 		all->ppdata_format = off;
1088 		if (ddi_copyin(priv->parent, di_mem_addr(st, off), size,
1089 		    mode) != 0) {
1090 			kmem_free(priv, sizeof (struct di_priv_data));
1091 			return (0);
1092 		}
1093 
1094 		off += size;
1095 	}
1096 
1097 	if (all->n_dpdata) {	/* driver private data format */
1098 		/*
1099 		 * check memory
1100 		 */
1101 		size = all->n_dpdata * sizeof (struct di_priv_format);
1102 		off = di_checkmem(st, off, size);
1103 		all->dpdata_format = off;
1104 		if (ddi_copyin(priv->driver, di_mem_addr(st, off), size,
1105 		    mode) != 0) {
1106 			kmem_free(priv, sizeof (struct di_priv_data));
1107 			return (0);
1108 		}
1109 
1110 		off += size;
1111 	}
1112 
1113 	kmem_free(priv, sizeof (struct di_priv_data));
1114 	return (off);
1115 }
1116 
1117 /*
1118  * Return the real address based on the offset (off) within snapshot
1119  */
1120 static caddr_t
1121 di_mem_addr(struct di_state *st, di_off_t off)
1122 {
1123 	struct di_mem *dcp = st->memlist;
1124 
1125 	dcmn_err3((CE_CONT, "di_mem_addr: dcp=%p off=%x\n",
1126 	    (void *)dcp, off));
1127 
1128 	ASSERT(off < st->mem_size);
1129 
1130 	while (off >= dcp->buf_size) {
1131 		off -= dcp->buf_size;
1132 		dcp = dcp->next;
1133 	}
1134 
1135 	dcmn_err3((CE_CONT, "di_mem_addr: new off=%x, return = %p\n",
1136 	    off, (void *)(dcp->buf + off)));
1137 
1138 	return (dcp->buf + off);
1139 }
1140 
1141 /*
1142  * Ideally we would use the whole key to derive the hash
1143  * value. However, the probability that two keys will
1144  * have the same dip (or pip) is very low, so
1145  * hashing by dip (or pip) pointer should suffice.
1146  */
1147 static uint_t
1148 di_hash_byptr(void *arg, mod_hash_key_t key)
1149 {
1150 	struct di_key *dik = key;
1151 	size_t rshift;
1152 	void *ptr;
1153 
1154 	ASSERT(arg == NULL);
1155 
1156 	switch (dik->k_type) {
1157 	case DI_DKEY:
1158 		ptr = dik->k_u.dkey.dk_dip;
1159 		rshift = highbit(sizeof (struct dev_info));
1160 		break;
1161 	case DI_PKEY:
1162 		ptr = dik->k_u.pkey.pk_pip;
1163 		rshift = highbit(sizeof (struct mdi_pathinfo));
1164 		break;
1165 	default:
1166 		panic("devinfo: unknown key type");
1167 		/*NOTREACHED*/
1168 	}
1169 	return (mod_hash_byptr((void *)rshift, ptr));
1170 }
1171 
1172 static void
1173 di_key_dtor(mod_hash_key_t key)
1174 {
1175 	char		*path_addr;
1176 	struct di_key	*dik = key;
1177 
1178 	switch (dik->k_type) {
1179 	case DI_DKEY:
1180 		break;
1181 	case DI_PKEY:
1182 		path_addr = dik->k_u.pkey.pk_path_addr;
1183 		if (path_addr)
1184 			kmem_free(path_addr, strlen(path_addr) + 1);
1185 		break;
1186 	default:
1187 		panic("devinfo: unknown key type");
1188 		/*NOTREACHED*/
1189 	}
1190 
1191 	kmem_free(dik, sizeof (struct di_key));
1192 }
1193 
1194 static int
1195 di_dkey_cmp(struct di_dkey *dk1, struct di_dkey *dk2)
1196 {
1197 	if (dk1->dk_dip !=  dk2->dk_dip)
1198 		return (dk1->dk_dip > dk2->dk_dip ? 1 : -1);
1199 
1200 	if (dk1->dk_major != (major_t)-1 && dk2->dk_major != (major_t)-1) {
1201 		if (dk1->dk_major !=  dk2->dk_major)
1202 			return (dk1->dk_major > dk2->dk_major ? 1 : -1);
1203 
1204 		if (dk1->dk_inst !=  dk2->dk_inst)
1205 			return (dk1->dk_inst > dk2->dk_inst ? 1 : -1);
1206 	}
1207 
1208 	if (dk1->dk_nodeid != dk2->dk_nodeid)
1209 		return (dk1->dk_nodeid > dk2->dk_nodeid ? 1 : -1);
1210 
1211 	return (0);
1212 }
1213 
1214 static int
1215 di_pkey_cmp(struct di_pkey *pk1, struct di_pkey *pk2)
1216 {
1217 	char *p1, *p2;
1218 	int rv;
1219 
1220 	if (pk1->pk_pip !=  pk2->pk_pip)
1221 		return (pk1->pk_pip > pk2->pk_pip ? 1 : -1);
1222 
1223 	p1 = pk1->pk_path_addr;
1224 	p2 = pk2->pk_path_addr;
1225 
1226 	p1 = p1 ? p1 : "";
1227 	p2 = p2 ? p2 : "";
1228 
1229 	rv = strcmp(p1, p2);
1230 	if (rv)
1231 		return (rv > 0  ? 1 : -1);
1232 
1233 	if (pk1->pk_client !=  pk2->pk_client)
1234 		return (pk1->pk_client > pk2->pk_client ? 1 : -1);
1235 
1236 	if (pk1->pk_phci !=  pk2->pk_phci)
1237 		return (pk1->pk_phci > pk2->pk_phci ? 1 : -1);
1238 
1239 	return (0);
1240 }
1241 
1242 static int
1243 di_key_cmp(mod_hash_key_t key1, mod_hash_key_t key2)
1244 {
1245 	struct di_key *dik1, *dik2;
1246 
1247 	dik1 = key1;
1248 	dik2 = key2;
1249 
1250 	if (dik1->k_type != dik2->k_type) {
1251 		panic("devinfo: mismatched keys");
1252 		/*NOTREACHED*/
1253 	}
1254 
1255 	switch (dik1->k_type) {
1256 	case DI_DKEY:
1257 		return (di_dkey_cmp(&(dik1->k_u.dkey), &(dik2->k_u.dkey)));
1258 	case DI_PKEY:
1259 		return (di_pkey_cmp(&(dik1->k_u.pkey), &(dik2->k_u.pkey)));
1260 	default:
1261 		panic("devinfo: unknown key type");
1262 		/*NOTREACHED*/
1263 	}
1264 }
1265 
1266 /*
1267  * This is the main function that takes a snapshot
1268  */
1269 static di_off_t
1270 di_snapshot(struct di_state *st)
1271 {
1272 	di_off_t off;
1273 	struct di_all *all;
1274 	dev_info_t *rootnode;
1275 	char buf[80];
1276 	int plen;
1277 	char *path;
1278 	vnode_t *vp;
1279 
1280 	all = (struct di_all *)(intptr_t)di_mem_addr(st, 0);
1281 	dcmn_err((CE_CONT, "Taking a snapshot of devinfo tree...\n"));
1282 
1283 	/*
1284 	 * Verify path before entrusting it to e_ddi_hold_devi_by_path because
1285 	 * some platforms have OBP bugs where executing the NDI_PROMNAME code
1286 	 * path against an invalid path results in panic.  The lookupnameat
1287 	 * is done relative to rootdir without a leading '/' on "devices/"
1288 	 * to force the lookup to occur in the global zone.
1289 	 */
1290 	plen = strlen("devices/") + strlen(all->root_path) + 1;
1291 	path = kmem_alloc(plen, KM_SLEEP);
1292 	(void) snprintf(path, plen, "devices/%s", all->root_path);
1293 	if (lookupnameat(path, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp, rootdir)) {
1294 		dcmn_err((CE_CONT, "Devinfo node %s not found\n",
1295 		    all->root_path));
1296 		kmem_free(path, plen);
1297 		return (0);
1298 	}
1299 	kmem_free(path, plen);
1300 	VN_RELE(vp);
1301 
1302 	/*
1303 	 * Hold the devinfo node referred by the path.
1304 	 */
1305 	rootnode = e_ddi_hold_devi_by_path(all->root_path, 0);
1306 	if (rootnode == NULL) {
1307 		dcmn_err((CE_CONT, "Devinfo node %s not found\n",
1308 		    all->root_path));
1309 		return (0);
1310 	}
1311 
1312 	(void) snprintf(buf, sizeof (buf),
1313 	    "devinfo registered dips (statep=%p)", (void *)st);
1314 
1315 	st->reg_dip_hash = mod_hash_create_extended(buf, 64,
1316 	    di_key_dtor, mod_hash_null_valdtor, di_hash_byptr,
1317 	    NULL, di_key_cmp, KM_SLEEP);
1318 
1319 
1320 	(void) snprintf(buf, sizeof (buf),
1321 	    "devinfo registered pips (statep=%p)", (void *)st);
1322 
1323 	st->reg_pip_hash = mod_hash_create_extended(buf, 64,
1324 	    di_key_dtor, mod_hash_null_valdtor, di_hash_byptr,
1325 	    NULL, di_key_cmp, KM_SLEEP);
1326 
1327 	/*
1328 	 * copy the device tree
1329 	 */
1330 	off = di_copytree(DEVI(rootnode), &all->top_devinfo, st);
1331 
1332 	if (DINFOPATH & st->command) {
1333 		mdi_walk_vhcis(build_vhci_list, st);
1334 	}
1335 
1336 	ddi_release_devi(rootnode);
1337 
1338 	/*
1339 	 * copy the devnames array
1340 	 */
1341 	all->devnames = off;
1342 	off = di_copydevnm(&all->devnames, st);
1343 
1344 
1345 	/* initialize the hash tables */
1346 	st->lnode_count = 0;
1347 	st->link_count = 0;
1348 
1349 	if (DINFOLYR & st->command) {
1350 		off = di_getlink_data(off, st);
1351 	}
1352 
1353 	/*
1354 	 * Free up hash tables
1355 	 */
1356 	mod_hash_destroy_hash(st->reg_dip_hash);
1357 	mod_hash_destroy_hash(st->reg_pip_hash);
1358 
1359 	/*
1360 	 * Record the timestamp now that we are done with snapshot.
1361 	 *
1362 	 * We compute the checksum later and then only if we cache
1363 	 * the snapshot, since checksumming adds some overhead.
1364 	 * The checksum is checked later if we read the cache file.
1365 	 * from disk.
1366 	 *
1367 	 * Set checksum field to 0 as CRC is calculated with that
1368 	 * field set to 0.
1369 	 */
1370 	all->snapshot_time = ddi_get_time();
1371 	all->cache_checksum = 0;
1372 
1373 	ASSERT(all->snapshot_time != 0);
1374 
1375 	return (off);
1376 }
1377 
1378 /*
1379  * Take a snapshot and clean /etc/devices files if DINFOCLEANUP is set
1380  */
1381 static di_off_t
1382 di_snapshot_and_clean(struct di_state *st)
1383 {
1384 	di_off_t	off;
1385 
1386 	modunload_disable();
1387 	off = di_snapshot(st);
1388 	if (off != 0 && (st->command & DINFOCLEANUP)) {
1389 		ASSERT(DEVICES_FILES_CLEANABLE(st));
1390 		/*
1391 		 * Cleanup /etc/devices files:
1392 		 * In order to accurately account for the system configuration
1393 		 * in /etc/devices files, the appropriate drivers must be
1394 		 * fully configured before the cleanup starts.
1395 		 * So enable modunload only after the cleanup.
1396 		 */
1397 		i_ddi_clean_devices_files();
1398 		/*
1399 		 * Remove backing store nodes for unused devices,
1400 		 * which retain past permissions customizations
1401 		 * and may be undesired for newly configured devices.
1402 		 */
1403 		dev_devices_cleanup();
1404 	}
1405 	modunload_enable();
1406 
1407 	return (off);
1408 }
1409 
1410 /*
1411  * construct vhci linkage in the snapshot.
1412  */
1413 int
1414 build_vhci_list(dev_info_t *vh_devinfo, void *arg)
1415 {
1416 	struct di_all *all;
1417 	struct di_node *me;
1418 	struct di_state *st;
1419 	di_off_t off;
1420 	phci_walk_arg_t pwa;
1421 
1422 	dcmn_err3((CE_CONT, "build_vhci list\n"));
1423 
1424 	dcmn_err3((CE_CONT, "vhci node %s, instance #%d\n",
1425 	    DEVI(vh_devinfo)->devi_node_name,
1426 	    DEVI(vh_devinfo)->devi_instance));
1427 
1428 	st = (struct di_state *)arg;
1429 	if (di_dip_find(st, vh_devinfo, &off) != 0) {
1430 		dcmn_err((CE_WARN, "di_dip_find error for the given node\n"));
1431 		return (DDI_WALK_TERMINATE);
1432 	}
1433 
1434 	dcmn_err3((CE_CONT, "st->mem_size: %d vh_devinfo off: 0x%x\n",
1435 	    st->mem_size, off));
1436 
1437 	all = (struct di_all *)(intptr_t)di_mem_addr(st, 0);
1438 	if (all->top_vhci_devinfo == 0) {
1439 		all->top_vhci_devinfo = off;
1440 	} else {
1441 		me = (struct di_node *)
1442 		    (intptr_t)di_mem_addr(st, all->top_vhci_devinfo);
1443 
1444 		while (me->next_vhci != 0) {
1445 			me = (struct di_node *)
1446 			    (intptr_t)di_mem_addr(st, me->next_vhci);
1447 		}
1448 
1449 		me->next_vhci = off;
1450 	}
1451 
1452 	pwa.off = off;
1453 	pwa.st = st;
1454 	mdi_vhci_walk_phcis(vh_devinfo, build_phci_list, &pwa);
1455 
1456 	return (DDI_WALK_CONTINUE);
1457 }
1458 
1459 /*
1460  * construct phci linkage for the given vhci in the snapshot.
1461  */
1462 int
1463 build_phci_list(dev_info_t *ph_devinfo, void *arg)
1464 {
1465 	struct di_node *vh_di_node;
1466 	struct di_node *me;
1467 	phci_walk_arg_t *pwa;
1468 	di_off_t off;
1469 
1470 	pwa = (phci_walk_arg_t *)arg;
1471 
1472 	dcmn_err3((CE_CONT, "build_phci list for vhci at offset: 0x%x\n",
1473 	    pwa->off));
1474 
1475 	vh_di_node = (struct di_node *)(intptr_t)di_mem_addr(pwa->st, pwa->off);
1476 
1477 	if (di_dip_find(pwa->st, ph_devinfo, &off) != 0) {
1478 		dcmn_err((CE_WARN, "di_dip_find error for the given node\n"));
1479 		return (DDI_WALK_TERMINATE);
1480 	}
1481 
1482 	dcmn_err3((CE_CONT, "phci node %s, instance #%d, at offset 0x%x\n",
1483 	    DEVI(ph_devinfo)->devi_node_name,
1484 	    DEVI(ph_devinfo)->devi_instance, off));
1485 
1486 	if (vh_di_node->top_phci == 0) {
1487 		vh_di_node->top_phci = off;
1488 		return (DDI_WALK_CONTINUE);
1489 	}
1490 
1491 	me = (struct di_node *)
1492 	    (intptr_t)di_mem_addr(pwa->st, vh_di_node->top_phci);
1493 
1494 	while (me->next_phci != 0) {
1495 		me = (struct di_node *)
1496 		    (intptr_t)di_mem_addr(pwa->st, me->next_phci);
1497 	}
1498 	me->next_phci = off;
1499 
1500 	return (DDI_WALK_CONTINUE);
1501 }
1502 
1503 /*
1504  * Assumes all devinfo nodes in device tree have been snapshotted
1505  */
1506 static void
1507 snap_driver_list(struct di_state *st, struct devnames *dnp, di_off_t *poff_p)
1508 {
1509 	struct dev_info *node;
1510 	struct di_node *me;
1511 	di_off_t off;
1512 
1513 	ASSERT(mutex_owned(&dnp->dn_lock));
1514 
1515 	node = DEVI(dnp->dn_head);
1516 	for (; node; node = node->devi_next) {
1517 		if (di_dip_find(st, (dev_info_t *)node, &off) != 0)
1518 			continue;
1519 
1520 		ASSERT(off > 0);
1521 		me = (struct di_node *)(intptr_t)di_mem_addr(st, off);
1522 		ASSERT(me->next == 0 || me->next == -1);
1523 		/*
1524 		 * Only nodes which were BOUND when they were
1525 		 * snapshotted will be added to per-driver list.
1526 		 */
1527 		if (me->next != -1)
1528 			continue;
1529 
1530 		*poff_p = off;
1531 		poff_p = &me->next;
1532 	}
1533 
1534 	*poff_p = 0;
1535 }
1536 
1537 /*
1538  * Copy the devnames array, so we have a list of drivers in the snapshot.
1539  * Also makes it possible to locate the per-driver devinfo nodes.
1540  */
1541 static di_off_t
1542 di_copydevnm(di_off_t *off_p, struct di_state *st)
1543 {
1544 	int i;
1545 	di_off_t off;
1546 	size_t size;
1547 	struct di_devnm *dnp;
1548 
1549 	dcmn_err2((CE_CONT, "di_copydevnm: *off_p = %p\n", (void *)off_p));
1550 
1551 	/*
1552 	 * make sure there is some allocated memory
1553 	 */
1554 	size = devcnt * sizeof (struct di_devnm);
1555 	off = di_checkmem(st, *off_p, size);
1556 	*off_p = off;
1557 
1558 	dcmn_err((CE_CONT, "Start copying devnamesp[%d] at offset 0x%x\n",
1559 	    devcnt, off));
1560 
1561 	dnp = (struct di_devnm *)(intptr_t)di_mem_addr(st, off);
1562 	off += size;
1563 
1564 	for (i = 0; i < devcnt; i++) {
1565 		if (devnamesp[i].dn_name == NULL) {
1566 			continue;
1567 		}
1568 
1569 		/*
1570 		 * dn_name is not freed during driver unload or removal.
1571 		 *
1572 		 * There is a race condition when make_devname() changes
1573 		 * dn_name during our strcpy. This should be rare since
1574 		 * only add_drv does this. At any rate, we never had a
1575 		 * problem with ddi_name_to_major(), which should have
1576 		 * the same problem.
1577 		 */
1578 		dcmn_err2((CE_CONT, "di_copydevnm: %s%d, off=%x\n",
1579 		    devnamesp[i].dn_name, devnamesp[i].dn_instance,
1580 		    off));
1581 
1582 		off = di_checkmem(st, off, strlen(devnamesp[i].dn_name) + 1);
1583 		dnp[i].name = off;
1584 		(void) strcpy((char *)di_mem_addr(st, off),
1585 		    devnamesp[i].dn_name);
1586 		off += DI_ALIGN(strlen(devnamesp[i].dn_name) + 1);
1587 
1588 		mutex_enter(&devnamesp[i].dn_lock);
1589 
1590 		/*
1591 		 * Snapshot per-driver node list
1592 		 */
1593 		snap_driver_list(st, &devnamesp[i], &dnp[i].head);
1594 
1595 		/*
1596 		 * This is not used by libdevinfo, leave it for now
1597 		 */
1598 		dnp[i].flags = devnamesp[i].dn_flags;
1599 		dnp[i].instance = devnamesp[i].dn_instance;
1600 
1601 		/*
1602 		 * get global properties
1603 		 */
1604 		if ((DINFOPROP & st->command) &&
1605 		    devnamesp[i].dn_global_prop_ptr) {
1606 			dnp[i].global_prop = off;
1607 			off = di_getprop(
1608 			    devnamesp[i].dn_global_prop_ptr->prop_list,
1609 			    &dnp[i].global_prop, st, NULL, DI_PROP_GLB_LIST);
1610 		}
1611 
1612 		/*
1613 		 * Bit encode driver ops: & bus_ops, cb_ops, & cb_ops->cb_str
1614 		 */
1615 		if (CB_DRV_INSTALLED(devopsp[i])) {
1616 			if (devopsp[i]->devo_cb_ops) {
1617 				dnp[i].ops |= DI_CB_OPS;
1618 				if (devopsp[i]->devo_cb_ops->cb_str)
1619 					dnp[i].ops |= DI_STREAM_OPS;
1620 			}
1621 			if (NEXUS_DRV(devopsp[i])) {
1622 				dnp[i].ops |= DI_BUS_OPS;
1623 			}
1624 		}
1625 
1626 		mutex_exit(&devnamesp[i].dn_lock);
1627 	}
1628 
1629 	dcmn_err((CE_CONT, "End copying devnamesp at offset 0x%x\n", off));
1630 
1631 	return (off);
1632 }
1633 
1634 /*
1635  * Copy the kernel devinfo tree. The tree and the devnames array forms
1636  * the entire snapshot (see also di_copydevnm).
1637  */
1638 static di_off_t
1639 di_copytree(struct dev_info *root, di_off_t *off_p, struct di_state *st)
1640 {
1641 	di_off_t off;
1642 	struct di_stack *dsp = kmem_zalloc(sizeof (struct di_stack), KM_SLEEP);
1643 
1644 	dcmn_err((CE_CONT, "di_copytree: root = %p, *off_p = %x\n",
1645 	    (void *)root, *off_p));
1646 
1647 	/* force attach drivers */
1648 	if (i_ddi_devi_attached((dev_info_t *)root) &&
1649 	    (st->command & DINFOSUBTREE) && (st->command & DINFOFORCE)) {
1650 		(void) ndi_devi_config((dev_info_t *)root,
1651 		    NDI_CONFIG | NDI_DEVI_PERSIST | NDI_NO_EVENT |
1652 		    NDI_DRV_CONF_REPROBE);
1653 	}
1654 
1655 	/*
1656 	 * Push top_devinfo onto a stack
1657 	 *
1658 	 * The stack is necessary to avoid recursion, which can overrun
1659 	 * the kernel stack.
1660 	 */
1661 	PUSH_STACK(dsp, root, off_p);
1662 
1663 	/*
1664 	 * As long as there is a node on the stack, copy the node.
1665 	 * di_copynode() is responsible for pushing and popping
1666 	 * child and sibling nodes on the stack.
1667 	 */
1668 	while (!EMPTY_STACK(dsp)) {
1669 		off = di_copynode(dsp, st);
1670 	}
1671 
1672 	/*
1673 	 * Free the stack structure
1674 	 */
1675 	kmem_free(dsp, sizeof (struct di_stack));
1676 
1677 	return (off);
1678 }
1679 
1680 /*
1681  * This is the core function, which copies all data associated with a single
1682  * node into the snapshot. The amount of information is determined by the
1683  * ioctl command.
1684  */
1685 static di_off_t
1686 di_copynode(struct di_stack *dsp, struct di_state *st)
1687 {
1688 	di_off_t off;
1689 	struct di_node *me;
1690 	struct dev_info *node;
1691 
1692 	dcmn_err2((CE_CONT, "di_copynode: depth = %x\n", dsp->depth));
1693 
1694 	node = TOP_NODE(dsp);
1695 
1696 	ASSERT(node != NULL);
1697 
1698 	/*
1699 	 * check memory usage, and fix offsets accordingly.
1700 	 */
1701 	off = di_checkmem(st, *(TOP_OFFSET(dsp)), sizeof (struct di_node));
1702 	*(TOP_OFFSET(dsp)) = off;
1703 	me = DI_NODE(di_mem_addr(st, off));
1704 
1705 	dcmn_err((CE_CONT, "copy node %s, instance #%d, at offset 0x%x\n",
1706 	    node->devi_node_name, node->devi_instance, off));
1707 
1708 	/*
1709 	 * Node parameters:
1710 	 * self		-- offset of current node within snapshot
1711 	 * nodeid	-- pointer to PROM node (tri-valued)
1712 	 * state	-- hot plugging device state
1713 	 * node_state	-- devinfo node state (CF1, CF2, etc.)
1714 	 */
1715 	me->self = off;
1716 	me->instance = node->devi_instance;
1717 	me->nodeid = node->devi_nodeid;
1718 	me->node_class = node->devi_node_class;
1719 	me->attributes = node->devi_node_attributes;
1720 	me->state = node->devi_state;
1721 	me->flags = node->devi_flags;
1722 	me->node_state = node->devi_node_state;
1723 	me->next_vhci = 0;		/* Filled up by build_vhci_list. */
1724 	me->top_phci = 0;		/* Filled up by build_phci_list. */
1725 	me->next_phci = 0;		/* Filled up by build_phci_list. */
1726 	me->multipath_component = MULTIPATH_COMPONENT_NONE; /* set default. */
1727 	me->user_private_data = NULL;
1728 
1729 	/*
1730 	 * Get parent's offset in snapshot from the stack
1731 	 * and store it in the current node
1732 	 */
1733 	if (dsp->depth > 1) {
1734 		me->parent = *(PARENT_OFFSET(dsp));
1735 	}
1736 
1737 	/*
1738 	 * Save the offset of this di_node in a hash table.
1739 	 * This is used later to resolve references to this
1740 	 * dip from other parts of the tree (per-driver list,
1741 	 * multipathing linkages, layered usage linkages).
1742 	 * The key used for the hash table is derived from
1743 	 * information in the dip.
1744 	 */
1745 	di_register_dip(st, (dev_info_t *)node, me->self);
1746 
1747 	/*
1748 	 * increment offset
1749 	 */
1750 	off += sizeof (struct di_node);
1751 
1752 #ifdef	DEVID_COMPATIBILITY
1753 	/* check for devid as property marker */
1754 	if (node->devi_devid) {
1755 		ddi_devid_t	devid;
1756 		char 		*devidstr;
1757 		int		devid_size;
1758 
1759 		/*
1760 		 * The devid is now represented as a property.
1761 		 * For micro release compatibility with di_devid interface
1762 		 * in libdevinfo we must return it as a binary structure in'
1763 		 * the snapshot.  When di_devid is removed from libdevinfo
1764 		 * in a future release (and devi_devid is deleted) then
1765 		 * code related to DEVID_COMPATIBILITY can be removed.
1766 		 */
1767 		ASSERT(node->devi_devid == DEVID_COMPATIBILITY);
1768 /* XXX should be DDI_DEV_T_NONE! */
1769 		if (ddi_prop_lookup_string(DDI_DEV_T_ANY, (dev_info_t *)node,
1770 		    DDI_PROP_DONTPASS, DEVID_PROP_NAME, &devidstr) ==
1771 		    DDI_PROP_SUCCESS) {
1772 			if (ddi_devid_str_decode(devidstr, &devid, NULL) ==
1773 			    DDI_SUCCESS) {
1774 				devid_size = ddi_devid_sizeof(devid);
1775 				off = di_checkmem(st, off, devid_size);
1776 				me->devid = off;
1777 				bcopy(devid,
1778 				    di_mem_addr(st, off), devid_size);
1779 				off += devid_size;
1780 				ddi_devid_free(devid);
1781 			}
1782 			ddi_prop_free(devidstr);
1783 		}
1784 	}
1785 #endif	/* DEVID_COMPATIBILITY */
1786 
1787 	if (node->devi_node_name) {
1788 		off = di_checkmem(st, off, strlen(node->devi_node_name) + 1);
1789 		me->node_name = off;
1790 		(void) strcpy(di_mem_addr(st, off), node->devi_node_name);
1791 		off += strlen(node->devi_node_name) + 1;
1792 	}
1793 
1794 	if (node->devi_compat_names && (node->devi_compat_length > 1)) {
1795 		off = di_checkmem(st, off, node->devi_compat_length);
1796 		me->compat_names = off;
1797 		me->compat_length = node->devi_compat_length;
1798 		bcopy(node->devi_compat_names, di_mem_addr(st, off),
1799 		    node->devi_compat_length);
1800 		off += node->devi_compat_length;
1801 	}
1802 
1803 	if (node->devi_addr) {
1804 		off = di_checkmem(st, off, strlen(node->devi_addr) + 1);
1805 		me->address = off;
1806 		(void) strcpy(di_mem_addr(st, off), node->devi_addr);
1807 		off += strlen(node->devi_addr) + 1;
1808 	}
1809 
1810 	if (node->devi_binding_name) {
1811 		off = di_checkmem(st, off, strlen(node->devi_binding_name) + 1);
1812 		me->bind_name = off;
1813 		(void) strcpy(di_mem_addr(st, off), node->devi_binding_name);
1814 		off += strlen(node->devi_binding_name) + 1;
1815 	}
1816 
1817 	me->drv_major = node->devi_major;
1818 
1819 	/*
1820 	 * If the dip is BOUND, set the next pointer of the
1821 	 * per-instance list to -1, indicating that it is yet to be resolved.
1822 	 * This will be resolved later in snap_driver_list().
1823 	 */
1824 	if (me->drv_major != -1) {
1825 		me->next = -1;
1826 	} else {
1827 		me->next = 0;
1828 	}
1829 
1830 	/*
1831 	 * An optimization to skip mutex_enter when not needed.
1832 	 */
1833 	if (!((DINFOMINOR | DINFOPROP | DINFOPATH) & st->command)) {
1834 		goto priv_data;
1835 	}
1836 
1837 	/*
1838 	 * Grab current per dev_info node lock to
1839 	 * get minor data and properties.
1840 	 */
1841 	mutex_enter(&(node->devi_lock));
1842 
1843 	if (!(DINFOMINOR & st->command)) {
1844 		goto path;
1845 	}
1846 
1847 	if (node->devi_minor) {		/* minor data */
1848 		me->minor_data = DI_ALIGN(off);
1849 		off = di_getmdata(node->devi_minor, &me->minor_data,
1850 		    me->self, st);
1851 	}
1852 
1853 path:
1854 	if (!(DINFOPATH & st->command)) {
1855 		goto property;
1856 	}
1857 
1858 	if (MDI_VHCI(node)) {
1859 		me->multipath_component = MULTIPATH_COMPONENT_VHCI;
1860 	}
1861 
1862 	if (MDI_CLIENT(node)) {
1863 		me->multipath_component = MULTIPATH_COMPONENT_CLIENT;
1864 		me->multipath_client = DI_ALIGN(off);
1865 		off = di_getpath_data((dev_info_t *)node, &me->multipath_client,
1866 		    me->self, st, 1);
1867 		dcmn_err((CE_WARN, "me->multipath_client = %x for node %p "
1868 		    "component type = %d.  off=%d",
1869 		    me->multipath_client,
1870 		    (void *)node, node->devi_mdi_component, off));
1871 	}
1872 
1873 	if (MDI_PHCI(node)) {
1874 		me->multipath_component = MULTIPATH_COMPONENT_PHCI;
1875 		me->multipath_phci = DI_ALIGN(off);
1876 		off = di_getpath_data((dev_info_t *)node, &me->multipath_phci,
1877 		    me->self, st, 0);
1878 		dcmn_err((CE_WARN, "me->multipath_phci = %x for node %p "
1879 		    "component type = %d.  off=%d",
1880 		    me->multipath_phci,
1881 		    (void *)node, node->devi_mdi_component, off));
1882 	}
1883 
1884 property:
1885 	if (!(DINFOPROP & st->command)) {
1886 		goto unlock;
1887 	}
1888 
1889 	if (node->devi_drv_prop_ptr) {	/* driver property list */
1890 		me->drv_prop = DI_ALIGN(off);
1891 		off = di_getprop(node->devi_drv_prop_ptr, &me->drv_prop, st,
1892 		    node, DI_PROP_DRV_LIST);
1893 	}
1894 
1895 	if (node->devi_sys_prop_ptr) {	/* system property list */
1896 		me->sys_prop = DI_ALIGN(off);
1897 		off = di_getprop(node->devi_sys_prop_ptr, &me->sys_prop, st,
1898 		    node, DI_PROP_SYS_LIST);
1899 	}
1900 
1901 	if (node->devi_hw_prop_ptr) {	/* hardware property list */
1902 		me->hw_prop = DI_ALIGN(off);
1903 		off = di_getprop(node->devi_hw_prop_ptr, &me->hw_prop, st,
1904 		    node, DI_PROP_HW_LIST);
1905 	}
1906 
1907 	if (node->devi_global_prop_list == NULL) {
1908 		me->glob_prop = (di_off_t)-1;	/* not global property */
1909 	} else {
1910 		/*
1911 		 * Make copy of global property list if this devinfo refers
1912 		 * global properties different from what's on the devnames
1913 		 * array. It can happen if there has been a forced
1914 		 * driver.conf update. See mod_drv(1M).
1915 		 */
1916 		ASSERT(me->drv_major != -1);
1917 		if (node->devi_global_prop_list !=
1918 		    devnamesp[me->drv_major].dn_global_prop_ptr) {
1919 			me->glob_prop = DI_ALIGN(off);
1920 			off = di_getprop(node->devi_global_prop_list->prop_list,
1921 			    &me->glob_prop, st, node, DI_PROP_GLB_LIST);
1922 		}
1923 	}
1924 
1925 unlock:
1926 	/*
1927 	 * release current per dev_info node lock
1928 	 */
1929 	mutex_exit(&(node->devi_lock));
1930 
1931 priv_data:
1932 	if (!(DINFOPRIVDATA & st->command)) {
1933 		goto pm_info;
1934 	}
1935 
1936 	if (ddi_get_parent_data((dev_info_t *)node) != NULL) {
1937 		me->parent_data = DI_ALIGN(off);
1938 		off = di_getppdata(node, &me->parent_data, st);
1939 	}
1940 
1941 	if (ddi_get_driver_private((dev_info_t *)node) != NULL) {
1942 		me->driver_data = DI_ALIGN(off);
1943 		off = di_getdpdata(node, &me->driver_data, st);
1944 	}
1945 
1946 pm_info: /* NOT implemented */
1947 
1948 subtree:
1949 	if (!(DINFOSUBTREE & st->command)) {
1950 		POP_STACK(dsp);
1951 		return (DI_ALIGN(off));
1952 	}
1953 
1954 child:
1955 	/*
1956 	 * If there is a child--push child onto stack.
1957 	 * Hold the parent busy while doing so.
1958 	 */
1959 	if (node->devi_child) {
1960 		me->child = DI_ALIGN(off);
1961 		PUSH_STACK(dsp, node->devi_child, &me->child);
1962 		return (me->child);
1963 	}
1964 
1965 sibling:
1966 	/*
1967 	 * no child node, unroll the stack till a sibling of
1968 	 * a parent node is found or root node is reached
1969 	 */
1970 	POP_STACK(dsp);
1971 	while (!EMPTY_STACK(dsp) && (node->devi_sibling == NULL)) {
1972 		node = TOP_NODE(dsp);
1973 		me = DI_NODE(di_mem_addr(st, *(TOP_OFFSET(dsp))));
1974 		POP_STACK(dsp);
1975 	}
1976 
1977 	if (!EMPTY_STACK(dsp)) {
1978 		/*
1979 		 * a sibling is found, replace top of stack by its sibling
1980 		 */
1981 		me->sibling = DI_ALIGN(off);
1982 		PUSH_STACK(dsp, node->devi_sibling, &me->sibling);
1983 		return (me->sibling);
1984 	}
1985 
1986 	/*
1987 	 * DONE with all nodes
1988 	 */
1989 	return (DI_ALIGN(off));
1990 }
1991 
1992 static i_lnode_t *
1993 i_lnode_alloc(int modid)
1994 {
1995 	i_lnode_t	*i_lnode;
1996 
1997 	i_lnode = kmem_zalloc(sizeof (i_lnode_t), KM_SLEEP);
1998 
1999 	ASSERT(modid != -1);
2000 	i_lnode->modid = modid;
2001 
2002 	return (i_lnode);
2003 }
2004 
2005 static void
2006 i_lnode_free(i_lnode_t *i_lnode)
2007 {
2008 	kmem_free(i_lnode, sizeof (i_lnode_t));
2009 }
2010 
2011 static void
2012 i_lnode_check_free(i_lnode_t *i_lnode)
2013 {
2014 	/* This lnode and its dip must have been snapshotted */
2015 	ASSERT(i_lnode->self > 0);
2016 	ASSERT(i_lnode->di_node->self > 0);
2017 
2018 	/* at least 1 link (in or out) must exist for this lnode */
2019 	ASSERT(i_lnode->link_in || i_lnode->link_out);
2020 
2021 	i_lnode_free(i_lnode);
2022 }
2023 
2024 static i_link_t *
2025 i_link_alloc(int spec_type)
2026 {
2027 	i_link_t *i_link;
2028 
2029 	i_link = kmem_zalloc(sizeof (i_link_t), KM_SLEEP);
2030 	i_link->spec_type = spec_type;
2031 
2032 	return (i_link);
2033 }
2034 
2035 static void
2036 i_link_check_free(i_link_t *i_link)
2037 {
2038 	/* This link must have been snapshotted */
2039 	ASSERT(i_link->self > 0);
2040 
2041 	/* Both endpoint lnodes must exist for this link */
2042 	ASSERT(i_link->src_lnode);
2043 	ASSERT(i_link->tgt_lnode);
2044 
2045 	kmem_free(i_link, sizeof (i_link_t));
2046 }
2047 
2048 /*ARGSUSED*/
2049 static uint_t
2050 i_lnode_hashfunc(void *arg, mod_hash_key_t key)
2051 {
2052 	i_lnode_t	*i_lnode = (i_lnode_t *)key;
2053 	struct di_node	*ptr;
2054 	dev_t		dev;
2055 
2056 	dev = i_lnode->devt;
2057 	if (dev != DDI_DEV_T_NONE)
2058 		return (i_lnode->modid + getminor(dev) + getmajor(dev));
2059 
2060 	ptr = i_lnode->di_node;
2061 	ASSERT(ptr->self > 0);
2062 	if (ptr) {
2063 		uintptr_t k = (uintptr_t)ptr;
2064 		k >>= (int)highbit(sizeof (struct di_node));
2065 		return ((uint_t)k);
2066 	}
2067 
2068 	return (i_lnode->modid);
2069 }
2070 
2071 static int
2072 i_lnode_cmp(void *arg1, void *arg2)
2073 {
2074 	i_lnode_t	*i_lnode1 = (i_lnode_t *)arg1;
2075 	i_lnode_t	*i_lnode2 = (i_lnode_t *)arg2;
2076 
2077 	if (i_lnode1->modid != i_lnode2->modid) {
2078 		return ((i_lnode1->modid < i_lnode2->modid) ? -1 : 1);
2079 	}
2080 
2081 	if (i_lnode1->di_node != i_lnode2->di_node)
2082 		return ((i_lnode1->di_node < i_lnode2->di_node) ? -1 : 1);
2083 
2084 	if (i_lnode1->devt != i_lnode2->devt)
2085 		return ((i_lnode1->devt < i_lnode2->devt) ? -1 : 1);
2086 
2087 	return (0);
2088 }
2089 
2090 /*
2091  * An lnode represents a {dip, dev_t} tuple. A link represents a
2092  * {src_lnode, tgt_lnode, spec_type} tuple.
2093  * The following callback assumes that LDI framework ref-counts the
2094  * src_dip and tgt_dip while invoking this callback.
2095  */
2096 static int
2097 di_ldi_callback(const ldi_usage_t *ldi_usage, void *arg)
2098 {
2099 	struct di_state	*st = (struct di_state *)arg;
2100 	i_lnode_t	*src_lnode, *tgt_lnode, *i_lnode;
2101 	i_link_t	**i_link_next, *i_link;
2102 	di_off_t	soff, toff;
2103 	mod_hash_val_t	nodep = NULL;
2104 	int		res;
2105 
2106 	/*
2107 	 * if the source or target of this device usage information doesn't
2108 	 * corrospond to a device node then we don't report it via
2109 	 * libdevinfo so return.
2110 	 */
2111 	if ((ldi_usage->src_dip == NULL) || (ldi_usage->tgt_dip == NULL))
2112 		return (LDI_USAGE_CONTINUE);
2113 
2114 	ASSERT(e_ddi_devi_holdcnt(ldi_usage->src_dip));
2115 	ASSERT(e_ddi_devi_holdcnt(ldi_usage->tgt_dip));
2116 
2117 	/*
2118 	 * Skip the ldi_usage if either src or tgt dip is not in the
2119 	 * snapshot. This saves us from pruning bad lnodes/links later.
2120 	 */
2121 	if (di_dip_find(st, ldi_usage->src_dip, &soff) != 0)
2122 		return (LDI_USAGE_CONTINUE);
2123 	if (di_dip_find(st, ldi_usage->tgt_dip, &toff) != 0)
2124 		return (LDI_USAGE_CONTINUE);
2125 
2126 	ASSERT(soff > 0);
2127 	ASSERT(toff > 0);
2128 
2129 	/*
2130 	 * allocate an i_lnode and add it to the lnode hash
2131 	 * if it is not already present. For this particular
2132 	 * link the lnode is a source, but it may
2133 	 * participate as tgt or src in any number of layered
2134 	 * operations - so it may already be in the hash.
2135 	 */
2136 	i_lnode = i_lnode_alloc(ldi_usage->src_modid);
2137 	i_lnode->di_node = (struct di_node *)(intptr_t)di_mem_addr(st, soff);
2138 	i_lnode->devt = ldi_usage->src_devt;
2139 
2140 	res = mod_hash_find(st->lnode_hash, i_lnode, &nodep);
2141 	if (res == MH_ERR_NOTFOUND) {
2142 		/*
2143 		 * new i_lnode
2144 		 * add it to the hash and increment the lnode count
2145 		 */
2146 		res = mod_hash_insert(st->lnode_hash, i_lnode, i_lnode);
2147 		ASSERT(res == 0);
2148 		st->lnode_count++;
2149 		src_lnode = i_lnode;
2150 	} else {
2151 		/* this i_lnode already exists in the lnode_hash */
2152 		i_lnode_free(i_lnode);
2153 		src_lnode = (i_lnode_t *)nodep;
2154 	}
2155 
2156 	/*
2157 	 * allocate a tgt i_lnode and add it to the lnode hash
2158 	 */
2159 	i_lnode = i_lnode_alloc(ldi_usage->tgt_modid);
2160 	i_lnode->di_node = (struct di_node *)(intptr_t)di_mem_addr(st, toff);
2161 	i_lnode->devt = ldi_usage->tgt_devt;
2162 
2163 	res = mod_hash_find(st->lnode_hash, i_lnode, &nodep);
2164 	if (res == MH_ERR_NOTFOUND) {
2165 		/*
2166 		 * new i_lnode
2167 		 * add it to the hash and increment the lnode count
2168 		 */
2169 		res = mod_hash_insert(st->lnode_hash, i_lnode, i_lnode);
2170 		ASSERT(res == 0);
2171 		st->lnode_count++;
2172 		tgt_lnode = i_lnode;
2173 	} else {
2174 		/* this i_lnode already exists in the lnode_hash */
2175 		i_lnode_free(i_lnode);
2176 		tgt_lnode = (i_lnode_t *)nodep;
2177 	}
2178 
2179 	/*
2180 	 * allocate a i_link
2181 	 */
2182 	i_link = i_link_alloc(ldi_usage->tgt_spec_type);
2183 	i_link->src_lnode = src_lnode;
2184 	i_link->tgt_lnode = tgt_lnode;
2185 
2186 	/*
2187 	 * add this link onto the src i_lnodes outbound i_link list
2188 	 */
2189 	i_link_next = &(src_lnode->link_out);
2190 	while (*i_link_next != NULL) {
2191 		if ((i_lnode_cmp(tgt_lnode, (*i_link_next)->tgt_lnode) == 0) &&
2192 		    (i_link->spec_type == (*i_link_next)->spec_type)) {
2193 			/* this link already exists */
2194 			kmem_free(i_link, sizeof (i_link_t));
2195 			return (LDI_USAGE_CONTINUE);
2196 		}
2197 		i_link_next = &((*i_link_next)->src_link_next);
2198 	}
2199 	*i_link_next = i_link;
2200 
2201 	/*
2202 	 * add this link onto the tgt i_lnodes inbound i_link list
2203 	 */
2204 	i_link_next = &(tgt_lnode->link_in);
2205 	while (*i_link_next != NULL) {
2206 		ASSERT(i_lnode_cmp(src_lnode, (*i_link_next)->src_lnode) != 0);
2207 		i_link_next = &((*i_link_next)->tgt_link_next);
2208 	}
2209 	*i_link_next = i_link;
2210 
2211 	/*
2212 	 * add this i_link to the link hash
2213 	 */
2214 	res = mod_hash_insert(st->link_hash, i_link, i_link);
2215 	ASSERT(res == 0);
2216 	st->link_count++;
2217 
2218 	return (LDI_USAGE_CONTINUE);
2219 }
2220 
2221 struct i_layer_data {
2222 	struct di_state	*st;
2223 	int		lnode_count;
2224 	int		link_count;
2225 	di_off_t	lnode_off;
2226 	di_off_t 	link_off;
2227 };
2228 
2229 /*ARGSUSED*/
2230 static uint_t
2231 i_link_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
2232 {
2233 	i_link_t		*i_link  = (i_link_t *)key;
2234 	struct i_layer_data	*data = arg;
2235 	struct di_link		*me;
2236 	struct di_lnode		*melnode;
2237 	struct di_node		*medinode;
2238 
2239 	ASSERT(i_link->self == 0);
2240 
2241 	i_link->self = data->link_off +
2242 	    (data->link_count * sizeof (struct di_link));
2243 	data->link_count++;
2244 
2245 	ASSERT(data->link_off > 0 && data->link_count > 0);
2246 	ASSERT(data->lnode_count == data->st->lnode_count); /* lnodes done */
2247 	ASSERT(data->link_count <= data->st->link_count);
2248 
2249 	/* fill in fields for the di_link snapshot */
2250 	me = (struct di_link *)(intptr_t)di_mem_addr(data->st, i_link->self);
2251 	me->self = i_link->self;
2252 	me->spec_type = i_link->spec_type;
2253 
2254 	/*
2255 	 * The src_lnode and tgt_lnode i_lnode_t for this i_link_t
2256 	 * are created during the LDI table walk. Since we are
2257 	 * walking the link hash, the lnode hash has already been
2258 	 * walked and the lnodes have been snapshotted. Save lnode
2259 	 * offsets.
2260 	 */
2261 	me->src_lnode = i_link->src_lnode->self;
2262 	me->tgt_lnode = i_link->tgt_lnode->self;
2263 
2264 	/*
2265 	 * Save this link's offset in the src_lnode snapshot's link_out
2266 	 * field
2267 	 */
2268 	melnode = (struct di_lnode *)
2269 	    (intptr_t)di_mem_addr(data->st, me->src_lnode);
2270 	me->src_link_next = melnode->link_out;
2271 	melnode->link_out = me->self;
2272 
2273 	/*
2274 	 * Put this link on the tgt_lnode's link_in field
2275 	 */
2276 	melnode = (struct di_lnode *)
2277 	    (intptr_t)di_mem_addr(data->st, me->tgt_lnode);
2278 	me->tgt_link_next = melnode->link_in;
2279 	melnode->link_in = me->self;
2280 
2281 	/*
2282 	 * An i_lnode_t is only created if the corresponding dip exists
2283 	 * in the snapshot. A pointer to the di_node is saved in the
2284 	 * i_lnode_t when it is allocated. For this link, get the di_node
2285 	 * for the source lnode. Then put the link on the di_node's list
2286 	 * of src links
2287 	 */
2288 	medinode = i_link->src_lnode->di_node;
2289 	me->src_node_next = medinode->src_links;
2290 	medinode->src_links = me->self;
2291 
2292 	/*
2293 	 * Put this link on the tgt_links list of the target
2294 	 * dip.
2295 	 */
2296 	medinode = i_link->tgt_lnode->di_node;
2297 	me->tgt_node_next = medinode->tgt_links;
2298 	medinode->tgt_links = me->self;
2299 
2300 	return (MH_WALK_CONTINUE);
2301 }
2302 
2303 /*ARGSUSED*/
2304 static uint_t
2305 i_lnode_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
2306 {
2307 	i_lnode_t		*i_lnode = (i_lnode_t *)key;
2308 	struct i_layer_data	*data = arg;
2309 	struct di_lnode		*me;
2310 	struct di_node		*medinode;
2311 
2312 	ASSERT(i_lnode->self == 0);
2313 
2314 	i_lnode->self = data->lnode_off +
2315 	    (data->lnode_count * sizeof (struct di_lnode));
2316 	data->lnode_count++;
2317 
2318 	ASSERT(data->lnode_off > 0 && data->lnode_count > 0);
2319 	ASSERT(data->link_count == 0); /* links not done yet */
2320 	ASSERT(data->lnode_count <= data->st->lnode_count);
2321 
2322 	/* fill in fields for the di_lnode snapshot */
2323 	me = (struct di_lnode *)(intptr_t)di_mem_addr(data->st, i_lnode->self);
2324 	me->self = i_lnode->self;
2325 
2326 	if (i_lnode->devt == DDI_DEV_T_NONE) {
2327 		me->dev_major = (major_t)-1;
2328 		me->dev_minor = (minor_t)-1;
2329 	} else {
2330 		me->dev_major = getmajor(i_lnode->devt);
2331 		me->dev_minor = getminor(i_lnode->devt);
2332 	}
2333 
2334 	/*
2335 	 * The dip corresponding to this lnode must exist in
2336 	 * the snapshot or we wouldn't have created the i_lnode_t
2337 	 * during LDI walk. Save the offset of the dip.
2338 	 */
2339 	ASSERT(i_lnode->di_node && i_lnode->di_node->self > 0);
2340 	me->node = i_lnode->di_node->self;
2341 
2342 	/*
2343 	 * There must be at least one link in or out of this lnode
2344 	 * or we wouldn't have created it. These fields will be set
2345 	 * during the link hash walk.
2346 	 */
2347 	ASSERT((i_lnode->link_in != NULL) || (i_lnode->link_out != NULL));
2348 
2349 	/*
2350 	 * set the offset of the devinfo node associated with this
2351 	 * lnode. Also update the node_next next pointer.  this pointer
2352 	 * is set if there are multiple lnodes associated with the same
2353 	 * devinfo node.  (could occure when multiple minor nodes
2354 	 * are open for one device, etc.)
2355 	 */
2356 	medinode = i_lnode->di_node;
2357 	me->node_next = medinode->lnodes;
2358 	medinode->lnodes = me->self;
2359 
2360 	return (MH_WALK_CONTINUE);
2361 }
2362 
2363 static di_off_t
2364 di_getlink_data(di_off_t off, struct di_state *st)
2365 {
2366 	struct i_layer_data data = {0};
2367 	size_t size;
2368 
2369 	dcmn_err2((CE_CONT, "di_copylyr: off = %x\n", off));
2370 
2371 	st->lnode_hash = mod_hash_create_extended("di_lnode_hash", 32,
2372 	    mod_hash_null_keydtor, (void (*)(mod_hash_val_t))i_lnode_check_free,
2373 	    i_lnode_hashfunc, NULL, i_lnode_cmp, KM_SLEEP);
2374 
2375 	st->link_hash = mod_hash_create_ptrhash("di_link_hash", 32,
2376 	    (void (*)(mod_hash_val_t))i_link_check_free, sizeof (i_link_t));
2377 
2378 	/* get driver layering information */
2379 	(void) ldi_usage_walker(st, di_ldi_callback);
2380 
2381 	/* check if there is any link data to include in the snapshot */
2382 	if (st->lnode_count == 0) {
2383 		ASSERT(st->link_count == 0);
2384 		goto out;
2385 	}
2386 
2387 	ASSERT(st->link_count != 0);
2388 
2389 	/* get a pointer to snapshot memory for all the di_lnodes */
2390 	size = sizeof (struct di_lnode) * st->lnode_count;
2391 	data.lnode_off = off = di_checkmem(st, off, size);
2392 	off += DI_ALIGN(size);
2393 
2394 	/* get a pointer to snapshot memory for all the di_links */
2395 	size = sizeof (struct di_link) * st->link_count;
2396 	data.link_off = off = di_checkmem(st, off, size);
2397 	off += DI_ALIGN(size);
2398 
2399 	data.lnode_count = data.link_count = 0;
2400 	data.st = st;
2401 
2402 	/*
2403 	 * We have lnodes and links that will go into the
2404 	 * snapshot, so let's walk the respective hashes
2405 	 * and snapshot them. The various linkages are
2406 	 * also set up during the walk.
2407 	 */
2408 	mod_hash_walk(st->lnode_hash, i_lnode_walker, (void *)&data);
2409 	ASSERT(data.lnode_count == st->lnode_count);
2410 
2411 	mod_hash_walk(st->link_hash, i_link_walker, (void *)&data);
2412 	ASSERT(data.link_count == st->link_count);
2413 
2414 out:
2415 	/* free up the i_lnodes and i_links used to create the snapshot */
2416 	mod_hash_destroy_hash(st->lnode_hash);
2417 	mod_hash_destroy_hash(st->link_hash);
2418 	st->lnode_count = 0;
2419 	st->link_count = 0;
2420 
2421 	return (off);
2422 }
2423 
2424 
2425 /*
2426  * Copy all minor data nodes attached to a devinfo node into the snapshot.
2427  * It is called from di_copynode with devi_lock held.
2428  */
2429 static di_off_t
2430 di_getmdata(struct ddi_minor_data *mnode, di_off_t *off_p, di_off_t node,
2431 	struct di_state *st)
2432 {
2433 	di_off_t off;
2434 	struct di_minor *me;
2435 
2436 	dcmn_err2((CE_CONT, "di_getmdata:\n"));
2437 
2438 	/*
2439 	 * check memory first
2440 	 */
2441 	off = di_checkmem(st, *off_p, sizeof (struct di_minor));
2442 	*off_p = off;
2443 
2444 	do {
2445 		me = (struct di_minor *)(intptr_t)di_mem_addr(st, off);
2446 		me->self = off;
2447 		me->type = mnode->type;
2448 		me->node = node;
2449 		me->user_private_data = NULL;
2450 
2451 		off += DI_ALIGN(sizeof (struct di_minor));
2452 
2453 		/*
2454 		 * Split dev_t to major/minor, so it works for
2455 		 * both ILP32 and LP64 model
2456 		 */
2457 		me->dev_major = getmajor(mnode->ddm_dev);
2458 		me->dev_minor = getminor(mnode->ddm_dev);
2459 		me->spec_type = mnode->ddm_spec_type;
2460 
2461 		if (mnode->ddm_name) {
2462 			off = di_checkmem(st, off,
2463 			    strlen(mnode->ddm_name) + 1);
2464 			me->name = off;
2465 			(void) strcpy(di_mem_addr(st, off), mnode->ddm_name);
2466 			off += DI_ALIGN(strlen(mnode->ddm_name) + 1);
2467 		}
2468 
2469 		if (mnode->ddm_node_type) {
2470 			off = di_checkmem(st, off,
2471 			    strlen(mnode->ddm_node_type) + 1);
2472 			me->node_type = off;
2473 			(void) strcpy(di_mem_addr(st, off),
2474 			    mnode->ddm_node_type);
2475 			off += DI_ALIGN(strlen(mnode->ddm_node_type) + 1);
2476 		}
2477 
2478 		off = di_checkmem(st, off, sizeof (struct di_minor));
2479 		me->next = off;
2480 		mnode = mnode->next;
2481 	} while (mnode);
2482 
2483 	me->next = 0;
2484 
2485 	return (off);
2486 }
2487 
2488 /*
2489  * di_register_dip(), di_find_dip(): The dip must be protected
2490  * from deallocation when using these routines - this can either
2491  * be a reference count, a busy hold or a per-driver lock.
2492  */
2493 
2494 static void
2495 di_register_dip(struct di_state *st, dev_info_t *dip, di_off_t off)
2496 {
2497 	struct dev_info *node = DEVI(dip);
2498 	struct di_key *key = kmem_zalloc(sizeof (*key), KM_SLEEP);
2499 	struct di_dkey *dk;
2500 
2501 	ASSERT(dip);
2502 	ASSERT(off > 0);
2503 
2504 	key->k_type = DI_DKEY;
2505 	dk = &(key->k_u.dkey);
2506 
2507 	dk->dk_dip = dip;
2508 	dk->dk_major = node->devi_major;
2509 	dk->dk_inst = node->devi_instance;
2510 	dk->dk_nodeid = node->devi_nodeid;
2511 
2512 	if (mod_hash_insert(st->reg_dip_hash, (mod_hash_key_t)key,
2513 	    (mod_hash_val_t)(uintptr_t)off) != 0) {
2514 		panic(
2515 		    "duplicate devinfo (%p) registered during device "
2516 		    "tree walk", (void *)dip);
2517 	}
2518 }
2519 
2520 
2521 static int
2522 di_dip_find(struct di_state *st, dev_info_t *dip, di_off_t *off_p)
2523 {
2524 	/*
2525 	 * uintptr_t must be used because it matches the size of void *;
2526 	 * mod_hash expects clients to place results into pointer-size
2527 	 * containers; since di_off_t is always a 32-bit offset, alignment
2528 	 * would otherwise be broken on 64-bit kernels.
2529 	 */
2530 	uintptr_t	offset;
2531 	struct		di_key key = {0};
2532 	struct		di_dkey *dk;
2533 
2534 	ASSERT(st->reg_dip_hash);
2535 	ASSERT(dip);
2536 	ASSERT(off_p);
2537 
2538 
2539 	key.k_type = DI_DKEY;
2540 	dk = &(key.k_u.dkey);
2541 
2542 	dk->dk_dip = dip;
2543 	dk->dk_major = DEVI(dip)->devi_major;
2544 	dk->dk_inst = DEVI(dip)->devi_instance;
2545 	dk->dk_nodeid = DEVI(dip)->devi_nodeid;
2546 
2547 	if (mod_hash_find(st->reg_dip_hash, (mod_hash_key_t)&key,
2548 	    (mod_hash_val_t *)&offset) == 0) {
2549 		*off_p = (di_off_t)offset;
2550 		return (0);
2551 	} else {
2552 		return (-1);
2553 	}
2554 }
2555 
2556 /*
2557  * di_register_pip(), di_find_pip(): The pip must be protected from deallocation
2558  * when using these routines. The caller must do this by protecting the
2559  * client(or phci)<->pip linkage while traversing the list and then holding the
2560  * pip when it is found in the list.
2561  */
2562 
2563 static void
2564 di_register_pip(struct di_state *st, mdi_pathinfo_t *pip, di_off_t off)
2565 {
2566 	struct di_key	*key = kmem_zalloc(sizeof (*key), KM_SLEEP);
2567 	char		*path_addr;
2568 	struct di_pkey	*pk;
2569 
2570 	ASSERT(pip);
2571 	ASSERT(off > 0);
2572 
2573 	key->k_type = DI_PKEY;
2574 	pk = &(key->k_u.pkey);
2575 
2576 	pk->pk_pip = pip;
2577 	path_addr = mdi_pi_get_addr(pip);
2578 	if (path_addr)
2579 		pk->pk_path_addr = i_ddi_strdup(path_addr, KM_SLEEP);
2580 	pk->pk_client = mdi_pi_get_client(pip);
2581 	pk->pk_phci = mdi_pi_get_phci(pip);
2582 
2583 	if (mod_hash_insert(st->reg_pip_hash, (mod_hash_key_t)key,
2584 	    (mod_hash_val_t)(uintptr_t)off) != 0) {
2585 		panic(
2586 		    "duplicate pathinfo (%p) registered during device "
2587 		    "tree walk", (void *)pip);
2588 	}
2589 }
2590 
2591 /*
2592  * As with di_register_pip, the caller must hold or lock the pip
2593  */
2594 static int
2595 di_pip_find(struct di_state *st, mdi_pathinfo_t *pip, di_off_t *off_p)
2596 {
2597 	/*
2598 	 * uintptr_t must be used because it matches the size of void *;
2599 	 * mod_hash expects clients to place results into pointer-size
2600 	 * containers; since di_off_t is always a 32-bit offset, alignment
2601 	 * would otherwise be broken on 64-bit kernels.
2602 	 */
2603 	uintptr_t	offset;
2604 	struct di_key	key = {0};
2605 	struct di_pkey	*pk;
2606 
2607 	ASSERT(st->reg_pip_hash);
2608 	ASSERT(off_p);
2609 
2610 	if (pip == NULL) {
2611 		*off_p = 0;
2612 		return (0);
2613 	}
2614 
2615 	key.k_type = DI_PKEY;
2616 	pk = &(key.k_u.pkey);
2617 
2618 	pk->pk_pip = pip;
2619 	pk->pk_path_addr = mdi_pi_get_addr(pip);
2620 	pk->pk_client = mdi_pi_get_client(pip);
2621 	pk->pk_phci = mdi_pi_get_phci(pip);
2622 
2623 	if (mod_hash_find(st->reg_pip_hash, (mod_hash_key_t)&key,
2624 	    (mod_hash_val_t *)&offset) == 0) {
2625 		*off_p = (di_off_t)offset;
2626 		return (0);
2627 	} else {
2628 		return (-1);
2629 	}
2630 }
2631 
2632 static di_path_state_t
2633 path_state_convert(mdi_pathinfo_state_t st)
2634 {
2635 	switch (st) {
2636 	case MDI_PATHINFO_STATE_ONLINE:
2637 		return (DI_PATH_STATE_ONLINE);
2638 	case MDI_PATHINFO_STATE_STANDBY:
2639 		return (DI_PATH_STATE_STANDBY);
2640 	case MDI_PATHINFO_STATE_OFFLINE:
2641 		return (DI_PATH_STATE_OFFLINE);
2642 	case MDI_PATHINFO_STATE_FAULT:
2643 		return (DI_PATH_STATE_FAULT);
2644 	default:
2645 		return (DI_PATH_STATE_UNKNOWN);
2646 	}
2647 }
2648 
2649 
2650 static di_off_t
2651 di_path_getprop(mdi_pathinfo_t *pip, di_off_t off, di_off_t *off_p,
2652     struct di_state *st)
2653 {
2654 	nvpair_t *prop = NULL;
2655 	struct di_path_prop *me;
2656 
2657 	if (mdi_pi_get_next_prop(pip, NULL) == NULL) {
2658 		*off_p = 0;
2659 		return (off);
2660 	}
2661 
2662 	off = di_checkmem(st, off, sizeof (struct di_path_prop));
2663 	*off_p = off;
2664 
2665 	while (prop = mdi_pi_get_next_prop(pip, prop)) {
2666 		int delta = 0;
2667 
2668 		me = (struct di_path_prop *)(intptr_t)di_mem_addr(st, off);
2669 		me->self = off;
2670 		off += sizeof (struct di_path_prop);
2671 
2672 		/*
2673 		 * property name
2674 		 */
2675 		off = di_checkmem(st, off, strlen(nvpair_name(prop)) + 1);
2676 		me->prop_name = off;
2677 		(void) strcpy(di_mem_addr(st, off), nvpair_name(prop));
2678 		off += strlen(nvpair_name(prop)) + 1;
2679 
2680 		switch (nvpair_type(prop)) {
2681 		case DATA_TYPE_BYTE:
2682 		case DATA_TYPE_INT16:
2683 		case DATA_TYPE_UINT16:
2684 		case DATA_TYPE_INT32:
2685 		case DATA_TYPE_UINT32:
2686 			delta = sizeof (int32_t);
2687 			me->prop_type = DDI_PROP_TYPE_INT;
2688 			off = di_checkmem(st, off, delta);
2689 			(void) nvpair_value_int32(prop,
2690 			    (int32_t *)(intptr_t)di_mem_addr(st, off));
2691 			break;
2692 
2693 		case DATA_TYPE_INT64:
2694 		case DATA_TYPE_UINT64:
2695 			delta = sizeof (int64_t);
2696 			me->prop_type = DDI_PROP_TYPE_INT64;
2697 			off = di_checkmem(st, off, delta);
2698 			(void) nvpair_value_int64(prop,
2699 			    (int64_t *)(intptr_t)di_mem_addr(st, off));
2700 			break;
2701 
2702 		case DATA_TYPE_STRING:
2703 		{
2704 			char *str;
2705 			(void) nvpair_value_string(prop, &str);
2706 			delta = strlen(str) + 1;
2707 			me->prop_type = DDI_PROP_TYPE_STRING;
2708 			off = di_checkmem(st, off, delta);
2709 			(void) strcpy(di_mem_addr(st, off), str);
2710 			break;
2711 		}
2712 		case DATA_TYPE_BYTE_ARRAY:
2713 		case DATA_TYPE_INT16_ARRAY:
2714 		case DATA_TYPE_UINT16_ARRAY:
2715 		case DATA_TYPE_INT32_ARRAY:
2716 		case DATA_TYPE_UINT32_ARRAY:
2717 		case DATA_TYPE_INT64_ARRAY:
2718 		case DATA_TYPE_UINT64_ARRAY:
2719 		{
2720 			uchar_t *buf;
2721 			uint_t nelems;
2722 			(void) nvpair_value_byte_array(prop, &buf, &nelems);
2723 			delta = nelems;
2724 			me->prop_type = DDI_PROP_TYPE_BYTE;
2725 			if (nelems != 0) {
2726 				off = di_checkmem(st, off, delta);
2727 				bcopy(buf, di_mem_addr(st, off), nelems);
2728 			}
2729 			break;
2730 		}
2731 
2732 		default:	/* Unknown or unhandled type; skip it */
2733 			delta = 0;
2734 			break;
2735 		}
2736 
2737 		if (delta > 0) {
2738 			me->prop_data = off;
2739 		}
2740 
2741 		me->prop_len = delta;
2742 		off += delta;
2743 
2744 		off = di_checkmem(st, off, sizeof (struct di_path_prop));
2745 		me->prop_next = off;
2746 	}
2747 
2748 	me->prop_next = 0;
2749 	return (off);
2750 }
2751 
2752 
2753 static void
2754 di_path_one_endpoint(struct di_path *me, di_off_t noff, di_off_t **off_pp,
2755     int get_client)
2756 {
2757 	if (get_client) {
2758 		ASSERT(me->path_client == 0);
2759 		me->path_client = noff;
2760 		ASSERT(me->path_c_link == 0);
2761 		*off_pp = &me->path_c_link;
2762 		me->path_snap_state &=
2763 		    ~(DI_PATH_SNAP_NOCLIENT | DI_PATH_SNAP_NOCLINK);
2764 	} else {
2765 		ASSERT(me->path_phci == 0);
2766 		me->path_phci = noff;
2767 		ASSERT(me->path_p_link == 0);
2768 		*off_pp = &me->path_p_link;
2769 		me->path_snap_state &=
2770 		    ~(DI_PATH_SNAP_NOPHCI | DI_PATH_SNAP_NOPLINK);
2771 	}
2772 }
2773 
2774 /*
2775  * poff_p: pointer to the linkage field. This links pips along the client|phci
2776  *	   linkage list.
2777  * noff  : Offset for the endpoint dip snapshot.
2778  */
2779 static di_off_t
2780 di_getpath_data(dev_info_t *dip, di_off_t *poff_p, di_off_t noff,
2781     struct di_state *st, int get_client)
2782 {
2783 	di_off_t off;
2784 	mdi_pathinfo_t *pip;
2785 	struct di_path *me;
2786 	mdi_pathinfo_t *(*next_pip)(dev_info_t *, mdi_pathinfo_t *);
2787 
2788 	dcmn_err2((CE_WARN, "di_getpath_data: client = %d", get_client));
2789 
2790 	/*
2791 	 * The naming of the following mdi_xyz() is unfortunately
2792 	 * non-intuitive. mdi_get_next_phci_path() follows the
2793 	 * client_link i.e. the list of pip's belonging to the
2794 	 * given client dip.
2795 	 */
2796 	if (get_client)
2797 		next_pip = &mdi_get_next_phci_path;
2798 	else
2799 		next_pip = &mdi_get_next_client_path;
2800 
2801 	off = *poff_p;
2802 
2803 	pip = NULL;
2804 	while (pip = (*next_pip)(dip, pip)) {
2805 		mdi_pathinfo_state_t state;
2806 		di_off_t stored_offset;
2807 
2808 		dcmn_err((CE_WARN, "marshalling pip = %p", (void *)pip));
2809 
2810 		mdi_pi_lock(pip);
2811 
2812 		if (di_pip_find(st, pip, &stored_offset) != -1) {
2813 			/*
2814 			 * We've already seen this pathinfo node so we need to
2815 			 * take care not to snap it again; However, one endpoint
2816 			 * and linkage will be set here. The other endpoint
2817 			 * and linkage has already been set when the pip was
2818 			 * first snapshotted i.e. when the other endpoint dip
2819 			 * was snapshotted.
2820 			 */
2821 			me = (struct di_path *)(intptr_t)
2822 			    di_mem_addr(st, stored_offset);
2823 
2824 			*poff_p = stored_offset;
2825 
2826 			di_path_one_endpoint(me, noff, &poff_p, get_client);
2827 
2828 			/*
2829 			 * The other endpoint and linkage were set when this
2830 			 * pip was snapshotted. So we are done with both
2831 			 * endpoints and linkages.
2832 			 */
2833 			ASSERT(!(me->path_snap_state &
2834 			    (DI_PATH_SNAP_NOCLIENT|DI_PATH_SNAP_NOPHCI)));
2835 			ASSERT(!(me->path_snap_state &
2836 			    (DI_PATH_SNAP_NOCLINK|DI_PATH_SNAP_NOPLINK)));
2837 
2838 			mdi_pi_unlock(pip);
2839 			continue;
2840 		}
2841 
2842 		/*
2843 		 * Now that we need to snapshot this pip, check memory
2844 		 */
2845 		off = di_checkmem(st, off, sizeof (struct di_path));
2846 		me = (struct di_path *)(intptr_t)di_mem_addr(st, off);
2847 		me->self = off;
2848 		*poff_p = off;
2849 		off += sizeof (struct di_path);
2850 
2851 		me->path_snap_state =
2852 		    DI_PATH_SNAP_NOCLINK | DI_PATH_SNAP_NOPLINK;
2853 		me->path_snap_state |=
2854 		    DI_PATH_SNAP_NOCLIENT | DI_PATH_SNAP_NOPHCI;
2855 
2856 		/*
2857 		 * Zero out fields as di_checkmem() doesn't guarantee
2858 		 * zero-filled memory
2859 		 */
2860 		me->path_client = me->path_phci = 0;
2861 		me->path_c_link = me->path_p_link = 0;
2862 
2863 		di_path_one_endpoint(me, noff, &poff_p, get_client);
2864 
2865 		/*
2866 		 * Note the existence of this pathinfo
2867 		 */
2868 		di_register_pip(st, pip, me->self);
2869 
2870 		state = mdi_pi_get_state(pip);
2871 		me->path_state = path_state_convert(state);
2872 
2873 		/*
2874 		 * Get intermediate addressing info.
2875 		 */
2876 		off = di_checkmem(st, off, strlen(mdi_pi_get_addr(pip)) + 1);
2877 		me->path_addr = off;
2878 		(void) strcpy(di_mem_addr(st, off), mdi_pi_get_addr(pip));
2879 		off += strlen(mdi_pi_get_addr(pip)) + 1;
2880 
2881 		/*
2882 		 * Get path properties if props are to be included in the
2883 		 * snapshot
2884 		 */
2885 		if (DINFOPROP & st->command) {
2886 			off = di_path_getprop(pip, off, &me->path_prop, st);
2887 		} else {
2888 			me->path_prop = 0;
2889 		}
2890 
2891 		mdi_pi_unlock(pip);
2892 	}
2893 
2894 	*poff_p = 0;
2895 
2896 	return (off);
2897 }
2898 
2899 /*
2900  * Copy a list of properties attached to a devinfo node. Called from
2901  * di_copynode with devi_lock held. The major number is passed in case
2902  * we need to call driver's prop_op entry. The value of list indicates
2903  * which list we are copying. Possible values are:
2904  * DI_PROP_DRV_LIST, DI_PROP_SYS_LIST, DI_PROP_GLB_LIST, DI_PROP_HW_LIST
2905  */
2906 static di_off_t
2907 di_getprop(struct ddi_prop *prop, di_off_t *off_p, struct di_state *st,
2908 	struct dev_info *dip, int list)
2909 {
2910 	dev_t dev;
2911 	int (*prop_op)();
2912 	int off, need_prop_op = 0;
2913 	int prop_op_fail = 0;
2914 	ddi_prop_t *propp = NULL;
2915 	struct di_prop *pp;
2916 	struct dev_ops *ops = NULL;
2917 	int prop_len;
2918 	caddr_t prop_val;
2919 
2920 
2921 	dcmn_err2((CE_CONT, "di_getprop:\n"));
2922 
2923 	ASSERT(st != NULL);
2924 
2925 	dcmn_err((CE_CONT, "copy property list at addr %p\n", (void *)prop));
2926 
2927 	/*
2928 	 * Figure out if we need to call driver's prop_op entry point.
2929 	 * The conditions are:
2930 	 *	-- driver property list
2931 	 *	-- driver must be attached and held
2932 	 *	-- driver's cb_prop_op != ddi_prop_op
2933 	 *		or parent's bus_prop_op != ddi_bus_prop_op
2934 	 */
2935 
2936 	if (list != DI_PROP_DRV_LIST) {
2937 		goto getprop;
2938 	}
2939 
2940 	/*
2941 	 * If driver is not attached or if major is -1, we ignore
2942 	 * the driver property list. No one should rely on such
2943 	 * properties.
2944 	 */
2945 	if (!i_ddi_devi_attached((dev_info_t *)dip)) {
2946 		off = *off_p;
2947 		*off_p = 0;
2948 		return (off);
2949 	}
2950 
2951 	/*
2952 	 * Now we have a driver which is held. We can examine entry points
2953 	 * and check the condition listed above.
2954 	 */
2955 	ops = dip->devi_ops;
2956 
2957 	/*
2958 	 * Some nexus drivers incorrectly set cb_prop_op to nodev,
2959 	 * nulldev or even NULL.
2960 	 */
2961 	if (ops && ops->devo_cb_ops &&
2962 	    (ops->devo_cb_ops->cb_prop_op != ddi_prop_op) &&
2963 	    (ops->devo_cb_ops->cb_prop_op != nodev) &&
2964 	    (ops->devo_cb_ops->cb_prop_op != nulldev) &&
2965 	    (ops->devo_cb_ops->cb_prop_op != NULL)) {
2966 		need_prop_op = 1;
2967 	}
2968 
2969 getprop:
2970 	/*
2971 	 * check memory availability
2972 	 */
2973 	off = di_checkmem(st, *off_p, sizeof (struct di_prop));
2974 	*off_p = off;
2975 	/*
2976 	 * Now copy properties
2977 	 */
2978 	do {
2979 		pp = (struct di_prop *)(intptr_t)di_mem_addr(st, off);
2980 		pp->self = off;
2981 		/*
2982 		 * Split dev_t to major/minor, so it works for
2983 		 * both ILP32 and LP64 model
2984 		 */
2985 		pp->dev_major = getmajor(prop->prop_dev);
2986 		pp->dev_minor = getminor(prop->prop_dev);
2987 		pp->prop_flags = prop->prop_flags;
2988 		pp->prop_list = list;
2989 
2990 		/*
2991 		 * property name
2992 		 */
2993 		off += sizeof (struct di_prop);
2994 		if (prop->prop_name) {
2995 			off = di_checkmem(st, off, strlen(prop->prop_name)
2996 			    + 1);
2997 			pp->prop_name = off;
2998 			(void) strcpy(di_mem_addr(st, off), prop->prop_name);
2999 			off += strlen(prop->prop_name) + 1;
3000 		}
3001 
3002 		/*
3003 		 * Set prop_len here. This may change later
3004 		 * if cb_prop_op returns a different length.
3005 		 */
3006 		pp->prop_len = prop->prop_len;
3007 		if (!need_prop_op) {
3008 			if (prop->prop_val == NULL) {
3009 				dcmn_err((CE_WARN,
3010 				    "devinfo: property fault at %p",
3011 				    (void *)prop));
3012 				pp->prop_data = -1;
3013 			} else if (prop->prop_len != 0) {
3014 				off = di_checkmem(st, off, prop->prop_len);
3015 				pp->prop_data = off;
3016 				bcopy(prop->prop_val, di_mem_addr(st, off),
3017 				    prop->prop_len);
3018 				off += DI_ALIGN(pp->prop_len);
3019 			}
3020 		}
3021 
3022 		off = di_checkmem(st, off, sizeof (struct di_prop));
3023 		pp->next = off;
3024 		prop = prop->prop_next;
3025 	} while (prop);
3026 
3027 	pp->next = 0;
3028 
3029 	if (!need_prop_op) {
3030 		dcmn_err((CE_CONT, "finished property "
3031 		    "list at offset 0x%x\n", off));
3032 		return (off);
3033 	}
3034 
3035 	/*
3036 	 * If there is a need to call driver's prop_op entry,
3037 	 * we must release driver's devi_lock, because the
3038 	 * cb_prop_op entry point will grab it.
3039 	 *
3040 	 * The snapshot memory has already been allocated above,
3041 	 * which means the length of an active property should
3042 	 * remain fixed for this implementation to work.
3043 	 */
3044 
3045 
3046 	prop_op = ops->devo_cb_ops->cb_prop_op;
3047 	pp = (struct di_prop *)(intptr_t)di_mem_addr(st, *off_p);
3048 
3049 	mutex_exit(&dip->devi_lock);
3050 
3051 	do {
3052 		int err;
3053 		struct di_prop *tmp;
3054 
3055 		if (pp->next) {
3056 			tmp = (struct di_prop *)
3057 			    (intptr_t)di_mem_addr(st, pp->next);
3058 		} else {
3059 			tmp = NULL;
3060 		}
3061 
3062 		/*
3063 		 * call into driver's prop_op entry point
3064 		 *
3065 		 * Must search DDI_DEV_T_NONE with DDI_DEV_T_ANY
3066 		 */
3067 		dev = makedevice(pp->dev_major, pp->dev_minor);
3068 		if (dev == DDI_DEV_T_NONE)
3069 			dev = DDI_DEV_T_ANY;
3070 
3071 		dcmn_err((CE_CONT, "call prop_op"
3072 		    "(%lx, %p, PROP_LEN_AND_VAL_BUF, "
3073 		    "DDI_PROP_DONTPASS, \"%s\", %p, &%d)\n",
3074 		    dev,
3075 		    (void *)dip,
3076 		    (char *)di_mem_addr(st, pp->prop_name),
3077 		    (void *)di_mem_addr(st, pp->prop_data),
3078 		    pp->prop_len));
3079 
3080 		if ((err = (*prop_op)(dev, (dev_info_t)dip,
3081 		    PROP_LEN_AND_VAL_ALLOC, DDI_PROP_DONTPASS,
3082 		    (char *)di_mem_addr(st, pp->prop_name),
3083 		    &prop_val, &prop_len)) != DDI_PROP_SUCCESS) {
3084 			if ((propp = i_ddi_prop_search(dev,
3085 			    (char *)di_mem_addr(st, pp->prop_name),
3086 			    (uint_t)pp->prop_flags,
3087 			    &(DEVI(dip)->devi_drv_prop_ptr))) != NULL) {
3088 				pp->prop_len = propp->prop_len;
3089 				if (pp->prop_len != 0) {
3090 					off = di_checkmem(st, off,
3091 					    pp->prop_len);
3092 					pp->prop_data = off;
3093 					bcopy(propp->prop_val, di_mem_addr(st,
3094 					    pp->prop_data), propp->prop_len);
3095 					off += DI_ALIGN(pp->prop_len);
3096 				}
3097 			} else {
3098 				prop_op_fail = 1;
3099 			}
3100 		} else if (prop_len != 0) {
3101 			pp->prop_len = prop_len;
3102 			off = di_checkmem(st, off, prop_len);
3103 			pp->prop_data = off;
3104 			bcopy(prop_val, di_mem_addr(st, off), prop_len);
3105 			off += DI_ALIGN(prop_len);
3106 			kmem_free(prop_val, prop_len);
3107 		}
3108 
3109 		if (prop_op_fail) {
3110 			pp->prop_data = -1;
3111 			dcmn_err((CE_WARN, "devinfo: prop_op failure "
3112 			    "for \"%s\" err %d",
3113 			    di_mem_addr(st, pp->prop_name), err));
3114 		}
3115 
3116 		pp = tmp;
3117 
3118 	} while (pp);
3119 
3120 	mutex_enter(&dip->devi_lock);
3121 	dcmn_err((CE_CONT, "finished property list at offset 0x%x\n", off));
3122 	return (off);
3123 }
3124 
3125 /*
3126  * find private data format attached to a dip
3127  * parent = 1 to match driver name of parent dip (for parent private data)
3128  *	0 to match driver name of current dip (for driver private data)
3129  */
3130 #define	DI_MATCH_DRIVER	0
3131 #define	DI_MATCH_PARENT	1
3132 
3133 struct di_priv_format *
3134 di_match_drv_name(struct dev_info *node, struct di_state *st, int match)
3135 {
3136 	int i, count, len;
3137 	char *drv_name;
3138 	major_t major;
3139 	struct di_all *all;
3140 	struct di_priv_format *form;
3141 
3142 	dcmn_err2((CE_CONT, "di_match_drv_name: node = %s, match = %x\n",
3143 	    node->devi_node_name, match));
3144 
3145 	if (match == DI_MATCH_PARENT) {
3146 		node = DEVI(node->devi_parent);
3147 	}
3148 
3149 	if (node == NULL) {
3150 		return (NULL);
3151 	}
3152 
3153 	major = ddi_name_to_major(node->devi_binding_name);
3154 	if (major == (major_t)(-1)) {
3155 		return (NULL);
3156 	}
3157 
3158 	/*
3159 	 * Match the driver name.
3160 	 */
3161 	drv_name = ddi_major_to_name(major);
3162 	if ((drv_name == NULL) || *drv_name == '\0') {
3163 		return (NULL);
3164 	}
3165 
3166 	/* Now get the di_priv_format array */
3167 	all = (struct di_all *)(intptr_t)di_mem_addr(st, 0);
3168 
3169 	if (match == DI_MATCH_PARENT) {
3170 		count = all->n_ppdata;
3171 		form = (struct di_priv_format *)
3172 		    (intptr_t)(di_mem_addr(st, 0) + all->ppdata_format);
3173 	} else {
3174 		count = all->n_dpdata;
3175 		form = (struct di_priv_format *)
3176 		    (intptr_t)((caddr_t)all + all->dpdata_format);
3177 	}
3178 
3179 	len = strlen(drv_name);
3180 	for (i = 0; i < count; i++) {
3181 		char *tmp;
3182 
3183 		tmp = form[i].drv_name;
3184 		while (tmp && (*tmp != '\0')) {
3185 			if (strncmp(drv_name, tmp, len) == 0) {
3186 				return (&form[i]);
3187 			}
3188 			/*
3189 			 * Move to next driver name, skipping a white space
3190 			 */
3191 			if (tmp = strchr(tmp, ' ')) {
3192 				tmp++;
3193 			}
3194 		}
3195 	}
3196 
3197 	return (NULL);
3198 }
3199 
3200 /*
3201  * The following functions copy data as specified by the format passed in.
3202  * To prevent invalid format from panicing the system, we call on_fault().
3203  * A return value of 0 indicates an error. Otherwise, the total offset
3204  * is returned.
3205  */
3206 #define	DI_MAX_PRIVDATA	(PAGESIZE >> 1)	/* max private data size */
3207 
3208 static di_off_t
3209 di_getprvdata(struct di_priv_format *pdp, struct dev_info *node,
3210     void *data, di_off_t *off_p, struct di_state *st)
3211 {
3212 	caddr_t pa;
3213 	void *ptr;
3214 	int i, size, repeat;
3215 	di_off_t off, off0, *tmp;
3216 	char *path;
3217 
3218 	label_t ljb;
3219 
3220 	dcmn_err2((CE_CONT, "di_getprvdata:\n"));
3221 
3222 	/*
3223 	 * check memory availability. Private data size is
3224 	 * limited to DI_MAX_PRIVDATA.
3225 	 */
3226 	off = di_checkmem(st, *off_p, DI_MAX_PRIVDATA);
3227 
3228 	if ((pdp->bytes == 0) || pdp->bytes > DI_MAX_PRIVDATA) {
3229 		goto failure;
3230 	}
3231 
3232 	if (!on_fault(&ljb)) {
3233 		/* copy the struct */
3234 		bcopy(data, di_mem_addr(st, off), pdp->bytes);
3235 		off0 = DI_ALIGN(pdp->bytes);
3236 
3237 		/* dereferencing pointers */
3238 		for (i = 0; i < MAX_PTR_IN_PRV; i++) {
3239 
3240 			if (pdp->ptr[i].size == 0) {
3241 				goto success;	/* no more ptrs */
3242 			}
3243 
3244 			/*
3245 			 * first, get the pointer content
3246 			 */
3247 			if ((pdp->ptr[i].offset < 0) ||
3248 			    (pdp->ptr[i].offset >
3249 			    pdp->bytes - sizeof (char *)))
3250 				goto failure;	/* wrong offset */
3251 
3252 			pa = di_mem_addr(st, off + pdp->ptr[i].offset);
3253 
3254 			/* save a tmp ptr to store off_t later */
3255 			tmp = (di_off_t *)(intptr_t)pa;
3256 
3257 			/* get pointer value, if NULL continue */
3258 			ptr = *((void **) (intptr_t)pa);
3259 			if (ptr == NULL) {
3260 				continue;
3261 			}
3262 
3263 			/*
3264 			 * next, find the repeat count (array dimension)
3265 			 */
3266 			repeat = pdp->ptr[i].len_offset;
3267 
3268 			/*
3269 			 * Positive value indicates a fixed sized array.
3270 			 * 0 or negative value indicates variable sized array.
3271 			 *
3272 			 * For variable sized array, the variable must be
3273 			 * an int member of the structure, with an offset
3274 			 * equal to the absolution value of struct member.
3275 			 */
3276 			if (repeat > pdp->bytes - sizeof (int)) {
3277 				goto failure;	/* wrong offset */
3278 			}
3279 
3280 			if (repeat >= 0) {
3281 				repeat = *((int *)
3282 				    (intptr_t)((caddr_t)data + repeat));
3283 			} else {
3284 				repeat = -repeat;
3285 			}
3286 
3287 			/*
3288 			 * next, get the size of the object to be copied
3289 			 */
3290 			size = pdp->ptr[i].size * repeat;
3291 
3292 			/*
3293 			 * Arbitrarily limit the total size of object to be
3294 			 * copied (1 byte to 1/4 page).
3295 			 */
3296 			if ((size <= 0) || (size > (DI_MAX_PRIVDATA - off0))) {
3297 				goto failure;	/* wrong size or too big */
3298 			}
3299 
3300 			/*
3301 			 * Now copy the data
3302 			 */
3303 			*tmp = off0;
3304 			bcopy(ptr, di_mem_addr(st, off + off0), size);
3305 			off0 += DI_ALIGN(size);
3306 		}
3307 	} else {
3308 		goto failure;
3309 	}
3310 
3311 success:
3312 	/*
3313 	 * success if reached here
3314 	 */
3315 	no_fault();
3316 	*off_p = off;
3317 
3318 	return (off + off0);
3319 	/*NOTREACHED*/
3320 
3321 failure:
3322 	/*
3323 	 * fault occurred
3324 	 */
3325 	no_fault();
3326 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3327 	cmn_err(CE_WARN, "devinfo: fault on private data for '%s' at %p",
3328 	    ddi_pathname((dev_info_t *)node, path), data);
3329 	kmem_free(path, MAXPATHLEN);
3330 	*off_p = -1;	/* set private data to indicate error */
3331 
3332 	return (off);
3333 }
3334 
3335 /*
3336  * get parent private data; on error, returns original offset
3337  */
3338 static di_off_t
3339 di_getppdata(struct dev_info *node, di_off_t *off_p, struct di_state *st)
3340 {
3341 	int off;
3342 	struct di_priv_format *ppdp;
3343 
3344 	dcmn_err2((CE_CONT, "di_getppdata:\n"));
3345 
3346 	/* find the parent data format */
3347 	if ((ppdp = di_match_drv_name(node, st, DI_MATCH_PARENT)) == NULL) {
3348 		off = *off_p;
3349 		*off_p = 0;	/* set parent data to none */
3350 		return (off);
3351 	}
3352 
3353 	return (di_getprvdata(ppdp, node,
3354 	    ddi_get_parent_data((dev_info_t *)node), off_p, st));
3355 }
3356 
3357 /*
3358  * get parent private data; returns original offset
3359  */
3360 static di_off_t
3361 di_getdpdata(struct dev_info *node, di_off_t *off_p, struct di_state *st)
3362 {
3363 	int off;
3364 	struct di_priv_format *dpdp;
3365 
3366 	dcmn_err2((CE_CONT, "di_getdpdata:"));
3367 
3368 	/* find the parent data format */
3369 	if ((dpdp = di_match_drv_name(node, st, DI_MATCH_DRIVER)) == NULL) {
3370 		off = *off_p;
3371 		*off_p = 0;	/* set driver data to none */
3372 		return (off);
3373 	}
3374 
3375 	return (di_getprvdata(dpdp, node,
3376 	    ddi_get_driver_private((dev_info_t *)node), off_p, st));
3377 }
3378 
3379 /*
3380  * The driver is stateful across DINFOCPYALL and DINFOUSRLD.
3381  * This function encapsulates the state machine:
3382  *
3383  *	-> IOC_IDLE -> IOC_SNAP -> IOC_DONE -> IOC_COPY ->
3384  *	|		SNAPSHOT		USRLD	 |
3385  *	--------------------------------------------------
3386  *
3387  * Returns 0 on success and -1 on failure
3388  */
3389 static int
3390 di_setstate(struct di_state *st, int new_state)
3391 {
3392 	int ret = 0;
3393 
3394 	mutex_enter(&di_lock);
3395 	switch (new_state) {
3396 	case IOC_IDLE:
3397 	case IOC_DONE:
3398 		break;
3399 	case IOC_SNAP:
3400 		if (st->di_iocstate != IOC_IDLE)
3401 			ret = -1;
3402 		break;
3403 	case IOC_COPY:
3404 		if (st->di_iocstate != IOC_DONE)
3405 			ret = -1;
3406 		break;
3407 	default:
3408 		ret = -1;
3409 	}
3410 
3411 	if (ret == 0)
3412 		st->di_iocstate = new_state;
3413 	else
3414 		cmn_err(CE_NOTE, "incorrect state transition from %d to %d",
3415 		    st->di_iocstate, new_state);
3416 	mutex_exit(&di_lock);
3417 	return (ret);
3418 }
3419 
3420 /*
3421  * We cannot assume the presence of the entire
3422  * snapshot in this routine. All we are guaranteed
3423  * is the di_all struct + 1 byte (for root_path)
3424  */
3425 static int
3426 header_plus_one_ok(struct di_all *all)
3427 {
3428 	/*
3429 	 * Refuse to read old versions
3430 	 */
3431 	if (all->version != DI_SNAPSHOT_VERSION) {
3432 		CACHE_DEBUG((DI_ERR, "bad version: 0x%x", all->version));
3433 		return (0);
3434 	}
3435 
3436 	if (all->cache_magic != DI_CACHE_MAGIC) {
3437 		CACHE_DEBUG((DI_ERR, "bad magic #: 0x%x", all->cache_magic));
3438 		return (0);
3439 	}
3440 
3441 	if (all->snapshot_time == 0) {
3442 		CACHE_DEBUG((DI_ERR, "bad timestamp: %ld", all->snapshot_time));
3443 		return (0);
3444 	}
3445 
3446 	if (all->top_devinfo == 0) {
3447 		CACHE_DEBUG((DI_ERR, "NULL top devinfo"));
3448 		return (0);
3449 	}
3450 
3451 	if (all->map_size < sizeof (*all) + 1) {
3452 		CACHE_DEBUG((DI_ERR, "bad map size: %u", all->map_size));
3453 		return (0);
3454 	}
3455 
3456 	if (all->root_path[0] != '/' || all->root_path[1] != '\0') {
3457 		CACHE_DEBUG((DI_ERR, "bad rootpath: %c%c",
3458 		    all->root_path[0], all->root_path[1]));
3459 		return (0);
3460 	}
3461 
3462 	/*
3463 	 * We can't check checksum here as we just have the header
3464 	 */
3465 
3466 	return (1);
3467 }
3468 
3469 static int
3470 chunk_write(struct vnode *vp, offset_t off, caddr_t buf, size_t len)
3471 {
3472 	rlim64_t	rlimit;
3473 	ssize_t		resid;
3474 	int		error = 0;
3475 
3476 
3477 	rlimit = RLIM64_INFINITY;
3478 
3479 	while (len) {
3480 		resid = 0;
3481 		error = vn_rdwr(UIO_WRITE, vp, buf, len, off,
3482 		    UIO_SYSSPACE, FSYNC, rlimit, kcred, &resid);
3483 
3484 		if (error || resid < 0) {
3485 			error = error ? error : EIO;
3486 			CACHE_DEBUG((DI_ERR, "write error: %d", error));
3487 			break;
3488 		}
3489 
3490 		/*
3491 		 * Check if we are making progress
3492 		 */
3493 		if (resid >= len) {
3494 			error = ENOSPC;
3495 			break;
3496 		}
3497 		buf += len - resid;
3498 		off += len - resid;
3499 		len = resid;
3500 	}
3501 
3502 	return (error);
3503 }
3504 
3505 extern int modrootloaded;
3506 extern void mdi_walk_vhcis(int (*)(dev_info_t *, void *), void *);
3507 extern void mdi_vhci_walk_phcis(dev_info_t *,
3508 	int (*)(dev_info_t *, void *), void *);
3509 
3510 static void
3511 di_cache_write(struct di_cache *cache)
3512 {
3513 	struct di_all	*all;
3514 	struct vnode	*vp;
3515 	int		oflags;
3516 	size_t		map_size;
3517 	size_t		chunk;
3518 	offset_t	off;
3519 	int		error;
3520 	char		*buf;
3521 
3522 	ASSERT(DI_CACHE_LOCKED(*cache));
3523 	ASSERT(!servicing_interrupt());
3524 
3525 	if (cache->cache_size == 0) {
3526 		ASSERT(cache->cache_data == NULL);
3527 		CACHE_DEBUG((DI_ERR, "Empty cache. Skipping write"));
3528 		return;
3529 	}
3530 
3531 	ASSERT(cache->cache_size > 0);
3532 	ASSERT(cache->cache_data);
3533 
3534 	if (!modrootloaded || rootvp == NULL || vn_is_readonly(rootvp)) {
3535 		CACHE_DEBUG((DI_ERR, "Can't write to rootFS. Skipping write"));
3536 		return;
3537 	}
3538 
3539 	all = (struct di_all *)cache->cache_data;
3540 
3541 	if (!header_plus_one_ok(all)) {
3542 		CACHE_DEBUG((DI_ERR, "Invalid header. Skipping write"));
3543 		return;
3544 	}
3545 
3546 	ASSERT(strcmp(all->root_path, "/") == 0);
3547 
3548 	/*
3549 	 * The cache_size is the total allocated memory for the cache.
3550 	 * The map_size is the actual size of valid data in the cache.
3551 	 * map_size may be smaller than cache_size but cannot exceed
3552 	 * cache_size.
3553 	 */
3554 	if (all->map_size > cache->cache_size) {
3555 		CACHE_DEBUG((DI_ERR, "map_size (0x%x) > cache_size (0x%x)."
3556 		    " Skipping write", all->map_size, cache->cache_size));
3557 		return;
3558 	}
3559 
3560 	/*
3561 	 * First unlink the temp file
3562 	 */
3563 	error = vn_remove(DI_CACHE_TEMP, UIO_SYSSPACE, RMFILE);
3564 	if (error && error != ENOENT) {
3565 		CACHE_DEBUG((DI_ERR, "%s: unlink failed: %d",
3566 		    DI_CACHE_TEMP, error));
3567 	}
3568 
3569 	if (error == EROFS) {
3570 		CACHE_DEBUG((DI_ERR, "RDONLY FS. Skipping write"));
3571 		return;
3572 	}
3573 
3574 	vp = NULL;
3575 	oflags = (FCREAT|FWRITE);
3576 	if (error = vn_open(DI_CACHE_TEMP, UIO_SYSSPACE, oflags,
3577 	    DI_CACHE_PERMS, &vp, CRCREAT, 0)) {
3578 		CACHE_DEBUG((DI_ERR, "%s: create failed: %d",
3579 		    DI_CACHE_TEMP, error));
3580 		return;
3581 	}
3582 
3583 	ASSERT(vp);
3584 
3585 	/*
3586 	 * Paranoid: Check if the file is on a read-only FS
3587 	 */
3588 	if (vn_is_readonly(vp)) {
3589 		CACHE_DEBUG((DI_ERR, "cannot write: readonly FS"));
3590 		goto fail;
3591 	}
3592 
3593 	/*
3594 	 * Note that we only write map_size bytes to disk - this saves
3595 	 * space as the actual cache size may be larger than size of
3596 	 * valid data in the cache.
3597 	 * Another advantage is that it makes verification of size
3598 	 * easier when the file is read later.
3599 	 */
3600 	map_size = all->map_size;
3601 	off = 0;
3602 	buf = cache->cache_data;
3603 
3604 	while (map_size) {
3605 		ASSERT(map_size > 0);
3606 		/*
3607 		 * Write in chunks so that VM system
3608 		 * is not overwhelmed
3609 		 */
3610 		if (map_size > di_chunk * PAGESIZE)
3611 			chunk = di_chunk * PAGESIZE;
3612 		else
3613 			chunk = map_size;
3614 
3615 		error = chunk_write(vp, off, buf, chunk);
3616 		if (error) {
3617 			CACHE_DEBUG((DI_ERR, "write failed: off=0x%x: %d",
3618 			    off, error));
3619 			goto fail;
3620 		}
3621 
3622 		off += chunk;
3623 		buf += chunk;
3624 		map_size -= chunk;
3625 
3626 		/* Give pageout a chance to run */
3627 		delay(1);
3628 	}
3629 
3630 	/*
3631 	 * Now sync the file and close it
3632 	 */
3633 	if (error = VOP_FSYNC(vp, FSYNC, kcred)) {
3634 		CACHE_DEBUG((DI_ERR, "FSYNC failed: %d", error));
3635 	}
3636 
3637 	if (error = VOP_CLOSE(vp, oflags, 1, (offset_t)0, kcred)) {
3638 		CACHE_DEBUG((DI_ERR, "close() failed: %d", error));
3639 		VN_RELE(vp);
3640 		return;
3641 	}
3642 
3643 	VN_RELE(vp);
3644 
3645 	/*
3646 	 * Now do the rename
3647 	 */
3648 	if (error = vn_rename(DI_CACHE_TEMP, DI_CACHE_FILE, UIO_SYSSPACE)) {
3649 		CACHE_DEBUG((DI_ERR, "rename failed: %d", error));
3650 		return;
3651 	}
3652 
3653 	CACHE_DEBUG((DI_INFO, "Cache write successful."));
3654 
3655 	return;
3656 
3657 fail:
3658 	(void) VOP_CLOSE(vp, oflags, 1, (offset_t)0, kcred);
3659 	VN_RELE(vp);
3660 }
3661 
3662 
3663 /*
3664  * Since we could be called early in boot,
3665  * use kobj_read_file()
3666  */
3667 static void
3668 di_cache_read(struct di_cache *cache)
3669 {
3670 	struct _buf	*file;
3671 	struct di_all	*all;
3672 	int		n;
3673 	size_t		map_size, sz, chunk;
3674 	offset_t	off;
3675 	caddr_t		buf;
3676 	uint32_t	saved_crc, crc;
3677 
3678 	ASSERT(modrootloaded);
3679 	ASSERT(DI_CACHE_LOCKED(*cache));
3680 	ASSERT(cache->cache_data == NULL);
3681 	ASSERT(cache->cache_size == 0);
3682 	ASSERT(!servicing_interrupt());
3683 
3684 	file = kobj_open_file(DI_CACHE_FILE);
3685 	if (file == (struct _buf *)-1) {
3686 		CACHE_DEBUG((DI_ERR, "%s: open failed: %d",
3687 		    DI_CACHE_FILE, ENOENT));
3688 		return;
3689 	}
3690 
3691 	/*
3692 	 * Read in the header+root_path first. The root_path must be "/"
3693 	 */
3694 	all = kmem_zalloc(sizeof (*all) + 1, KM_SLEEP);
3695 	n = kobj_read_file(file, (caddr_t)all, sizeof (*all) + 1, 0);
3696 
3697 	if ((n != sizeof (*all) + 1) || !header_plus_one_ok(all)) {
3698 		kmem_free(all, sizeof (*all) + 1);
3699 		kobj_close_file(file);
3700 		CACHE_DEBUG((DI_ERR, "cache header: read error or invalid"));
3701 		return;
3702 	}
3703 
3704 	map_size = all->map_size;
3705 
3706 	kmem_free(all, sizeof (*all) + 1);
3707 
3708 	ASSERT(map_size >= sizeof (*all) + 1);
3709 
3710 	buf = di_cache.cache_data = kmem_alloc(map_size, KM_SLEEP);
3711 	sz = map_size;
3712 	off = 0;
3713 	while (sz) {
3714 		/* Don't overload VM with large reads */
3715 		chunk = (sz > di_chunk * PAGESIZE) ? di_chunk * PAGESIZE : sz;
3716 		n = kobj_read_file(file, buf, chunk, off);
3717 		if (n != chunk) {
3718 			CACHE_DEBUG((DI_ERR, "%s: read error at offset: %lld",
3719 			    DI_CACHE_FILE, off));
3720 			goto fail;
3721 		}
3722 		off += chunk;
3723 		buf += chunk;
3724 		sz -= chunk;
3725 	}
3726 
3727 	ASSERT(off == map_size);
3728 
3729 	/*
3730 	 * Read past expected EOF to verify size.
3731 	 */
3732 	if (kobj_read_file(file, (caddr_t)&sz, 1, off) > 0) {
3733 		CACHE_DEBUG((DI_ERR, "%s: file size changed", DI_CACHE_FILE));
3734 		goto fail;
3735 	}
3736 
3737 	all = (struct di_all *)di_cache.cache_data;
3738 	if (!header_plus_one_ok(all)) {
3739 		CACHE_DEBUG((DI_ERR, "%s: file header changed", DI_CACHE_FILE));
3740 		goto fail;
3741 	}
3742 
3743 	/*
3744 	 * Compute CRC with checksum field in the cache data set to 0
3745 	 */
3746 	saved_crc = all->cache_checksum;
3747 	all->cache_checksum = 0;
3748 	CRC32(crc, di_cache.cache_data, map_size, -1U, crc32_table);
3749 	all->cache_checksum = saved_crc;
3750 
3751 	if (crc != all->cache_checksum) {
3752 		CACHE_DEBUG((DI_ERR,
3753 		    "%s: checksum error: expected=0x%x actual=0x%x",
3754 		    DI_CACHE_FILE, all->cache_checksum, crc));
3755 		goto fail;
3756 	}
3757 
3758 	if (all->map_size != map_size) {
3759 		CACHE_DEBUG((DI_ERR, "%s: map size changed", DI_CACHE_FILE));
3760 		goto fail;
3761 	}
3762 
3763 	kobj_close_file(file);
3764 
3765 	di_cache.cache_size = map_size;
3766 
3767 	return;
3768 
3769 fail:
3770 	kmem_free(di_cache.cache_data, map_size);
3771 	kobj_close_file(file);
3772 	di_cache.cache_data = NULL;
3773 	di_cache.cache_size = 0;
3774 }
3775 
3776 
3777 /*
3778  * Checks if arguments are valid for using the cache.
3779  */
3780 static int
3781 cache_args_valid(struct di_state *st, int *error)
3782 {
3783 	ASSERT(error);
3784 	ASSERT(st->mem_size > 0);
3785 	ASSERT(st->memlist != NULL);
3786 
3787 	if (!modrootloaded || !i_ddi_io_initialized()) {
3788 		CACHE_DEBUG((DI_ERR,
3789 		    "cache lookup failure: I/O subsystem not inited"));
3790 		*error = ENOTACTIVE;
3791 		return (0);
3792 	}
3793 
3794 	/*
3795 	 * No other flags allowed with DINFOCACHE
3796 	 */
3797 	if (st->command != (DINFOCACHE & DIIOC_MASK)) {
3798 		CACHE_DEBUG((DI_ERR,
3799 		    "cache lookup failure: bad flags: 0x%x",
3800 		    st->command));
3801 		*error = EINVAL;
3802 		return (0);
3803 	}
3804 
3805 	if (strcmp(DI_ALL_PTR(st)->root_path, "/") != 0) {
3806 		CACHE_DEBUG((DI_ERR,
3807 		    "cache lookup failure: bad root: %s",
3808 		    DI_ALL_PTR(st)->root_path));
3809 		*error = EINVAL;
3810 		return (0);
3811 	}
3812 
3813 	CACHE_DEBUG((DI_INFO, "cache lookup args ok: 0x%x", st->command));
3814 
3815 	*error = 0;
3816 
3817 	return (1);
3818 }
3819 
3820 static int
3821 snapshot_is_cacheable(struct di_state *st)
3822 {
3823 	ASSERT(st->mem_size > 0);
3824 	ASSERT(st->memlist != NULL);
3825 
3826 	if ((st->command & DI_CACHE_SNAPSHOT_FLAGS) !=
3827 	    (DI_CACHE_SNAPSHOT_FLAGS & DIIOC_MASK)) {
3828 		CACHE_DEBUG((DI_INFO,
3829 		    "not cacheable: incompatible flags: 0x%x",
3830 		    st->command));
3831 		return (0);
3832 	}
3833 
3834 	if (strcmp(DI_ALL_PTR(st)->root_path, "/") != 0) {
3835 		CACHE_DEBUG((DI_INFO,
3836 		    "not cacheable: incompatible root path: %s",
3837 		    DI_ALL_PTR(st)->root_path));
3838 		return (0);
3839 	}
3840 
3841 	CACHE_DEBUG((DI_INFO, "cacheable snapshot request: 0x%x", st->command));
3842 
3843 	return (1);
3844 }
3845 
3846 static int
3847 di_cache_lookup(struct di_state *st)
3848 {
3849 	size_t	rval;
3850 	int	cache_valid;
3851 
3852 	ASSERT(cache_args_valid(st, &cache_valid));
3853 	ASSERT(modrootloaded);
3854 
3855 	DI_CACHE_LOCK(di_cache);
3856 
3857 	/*
3858 	 * The following assignment determines the validity
3859 	 * of the cache as far as this snapshot is concerned.
3860 	 */
3861 	cache_valid = di_cache.cache_valid;
3862 
3863 	if (cache_valid && di_cache.cache_data == NULL) {
3864 		di_cache_read(&di_cache);
3865 		/* check for read or file error */
3866 		if (di_cache.cache_data == NULL)
3867 			cache_valid = 0;
3868 	}
3869 
3870 	if (cache_valid) {
3871 		/*
3872 		 * Ok, the cache was valid as of this particular
3873 		 * snapshot. Copy the cached snapshot. This is safe
3874 		 * to do as the cache cannot be freed (we hold the
3875 		 * cache lock). Free the memory allocated in di_state
3876 		 * up until this point - we will simply copy everything
3877 		 * in the cache.
3878 		 */
3879 
3880 		ASSERT(di_cache.cache_data != NULL);
3881 		ASSERT(di_cache.cache_size > 0);
3882 
3883 		di_freemem(st);
3884 
3885 		rval = 0;
3886 		if (di_cache2mem(&di_cache, st) > 0) {
3887 
3888 			ASSERT(DI_ALL_PTR(st));
3889 
3890 			/*
3891 			 * map_size is size of valid data in the
3892 			 * cached snapshot and may be less than
3893 			 * size of the cache.
3894 			 */
3895 			rval = DI_ALL_PTR(st)->map_size;
3896 
3897 			ASSERT(rval >= sizeof (struct di_all));
3898 			ASSERT(rval <= di_cache.cache_size);
3899 		}
3900 	} else {
3901 		/*
3902 		 * The cache isn't valid, we need to take a snapshot.
3903 		 * Set the command flags appropriately
3904 		 */
3905 		ASSERT(st->command == (DINFOCACHE & DIIOC_MASK));
3906 		st->command = (DI_CACHE_SNAPSHOT_FLAGS & DIIOC_MASK);
3907 		rval = di_cache_update(st);
3908 		st->command = (DINFOCACHE & DIIOC_MASK);
3909 	}
3910 
3911 	DI_CACHE_UNLOCK(di_cache);
3912 
3913 	/*
3914 	 * For cached snapshots, the devinfo driver always returns
3915 	 * a snapshot rooted at "/".
3916 	 */
3917 	ASSERT(rval == 0 || strcmp(DI_ALL_PTR(st)->root_path, "/") == 0);
3918 
3919 	return ((int)rval);
3920 }
3921 
3922 /*
3923  * This is a forced update of the cache  - the previous state of the cache
3924  * may be:
3925  *	- unpopulated
3926  *	- populated and invalid
3927  *	- populated and valid
3928  */
3929 static int
3930 di_cache_update(struct di_state *st)
3931 {
3932 	int rval;
3933 	uint32_t crc;
3934 	struct di_all *all;
3935 
3936 	ASSERT(DI_CACHE_LOCKED(di_cache));
3937 	ASSERT(snapshot_is_cacheable(st));
3938 
3939 	/*
3940 	 * Free the in-core cache and the on-disk file (if they exist)
3941 	 */
3942 	i_ddi_di_cache_free(&di_cache);
3943 
3944 	/*
3945 	 * Set valid flag before taking the snapshot,
3946 	 * so that any invalidations that arrive
3947 	 * during or after the snapshot are not
3948 	 * removed by us.
3949 	 */
3950 	atomic_or_32(&di_cache.cache_valid, 1);
3951 
3952 	rval = di_snapshot_and_clean(st);
3953 
3954 	if (rval == 0) {
3955 		CACHE_DEBUG((DI_ERR, "can't update cache: bad snapshot"));
3956 		return (0);
3957 	}
3958 
3959 	DI_ALL_PTR(st)->map_size = rval;
3960 
3961 	if (di_mem2cache(st, &di_cache) == 0) {
3962 		CACHE_DEBUG((DI_ERR, "can't update cache: copy failed"));
3963 		return (0);
3964 	}
3965 
3966 	ASSERT(di_cache.cache_data);
3967 	ASSERT(di_cache.cache_size > 0);
3968 
3969 	/*
3970 	 * Now that we have cached the snapshot, compute its checksum.
3971 	 * The checksum is only computed over the valid data in the
3972 	 * cache, not the entire cache.
3973 	 * Also, set all the fields (except checksum) before computing
3974 	 * checksum.
3975 	 */
3976 	all = (struct di_all *)di_cache.cache_data;
3977 	all->cache_magic = DI_CACHE_MAGIC;
3978 	all->map_size = rval;
3979 
3980 	ASSERT(all->cache_checksum == 0);
3981 	CRC32(crc, di_cache.cache_data, all->map_size, -1U, crc32_table);
3982 	all->cache_checksum = crc;
3983 
3984 	di_cache_write(&di_cache);
3985 
3986 	return (rval);
3987 }
3988 
3989 static void
3990 di_cache_print(di_cache_debug_t msglevel, char *fmt, ...)
3991 {
3992 	va_list	ap;
3993 
3994 	if (di_cache_debug <= DI_QUIET)
3995 		return;
3996 
3997 	if (di_cache_debug < msglevel)
3998 		return;
3999 
4000 	switch (msglevel) {
4001 		case DI_ERR:
4002 			msglevel = CE_WARN;
4003 			break;
4004 		case DI_INFO:
4005 		case DI_TRACE:
4006 		default:
4007 			msglevel = CE_NOTE;
4008 			break;
4009 	}
4010 
4011 	va_start(ap, fmt);
4012 	vcmn_err(msglevel, fmt, ap);
4013 	va_end(ap);
4014 }
4015