xref: /titanic_52/usr/src/uts/common/fs/devfs/devfs_subr.c (revision 159d09a20817016f09b3ea28d1bdada4a336bb91)
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 2008 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  * miscellaneous routines for the devfs
30  */
31 
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/t_lock.h>
35 #include <sys/systm.h>
36 #include <sys/sysmacros.h>
37 #include <sys/user.h>
38 #include <sys/time.h>
39 #include <sys/vfs.h>
40 #include <sys/vnode.h>
41 #include <sys/file.h>
42 #include <sys/fcntl.h>
43 #include <sys/flock.h>
44 #include <sys/kmem.h>
45 #include <sys/uio.h>
46 #include <sys/errno.h>
47 #include <sys/stat.h>
48 #include <sys/cred.h>
49 #include <sys/dirent.h>
50 #include <sys/pathname.h>
51 #include <sys/cmn_err.h>
52 #include <sys/debug.h>
53 #include <sys/modctl.h>
54 #include <fs/fs_subr.h>
55 #include <sys/fs/dv_node.h>
56 #include <sys/fs/snode.h>
57 #include <sys/sunndi.h>
58 #include <sys/sunmdi.h>
59 #include <sys/conf.h>
60 
61 #ifdef DEBUG
62 int devfs_debug = 0x0;
63 #endif
64 
65 const char	dvnm[] = "devfs";
66 kmem_cache_t	*dv_node_cache;	/* dv_node cache */
67 
68 /*
69  * The devfs_clean_key is taken during a devfs_clean operation: it is used to
70  * prevent unnecessary code execution and for detection of potential deadlocks.
71  */
72 uint_t		devfs_clean_key;
73 
74 struct dv_node *dvroot;
75 
76 /* prototype memory vattrs */
77 vattr_t dv_vattr_dir = {
78 	AT_TYPE|AT_MODE|AT_UID|AT_GID, 		/* va_mask */
79 	VDIR,					/* va_type */
80 	DV_DIRMODE_DEFAULT,			/* va_mode */
81 	DV_UID_DEFAULT,				/* va_uid */
82 	DV_GID_DEFAULT,				/* va_gid */
83 	0,					/* va_fsid; */
84 	0,					/* va_nodeid; */
85 	0,					/* va_nlink; */
86 	0,					/* va_size; */
87 	0,					/* va_atime; */
88 	0,					/* va_mtime; */
89 	0,					/* va_ctime; */
90 	0,					/* va_rdev; */
91 	0,					/* va_blksize; */
92 	0,					/* va_nblocks; */
93 	0,					/* va_seq; */
94 };
95 
96 vattr_t dv_vattr_file = {
97 	AT_TYPE|AT_MODE|AT_SIZE|AT_UID|AT_GID|AT_RDEV,	/* va_mask */
98 	0,					/* va_type */
99 	DV_DEVMODE_DEFAULT,			/* va_mode */
100 	DV_UID_DEFAULT,				/* va_uid */
101 	DV_GID_DEFAULT,				/* va_gid */
102 	0,					/* va_fsid; */
103 	0,					/* va_nodeid; */
104 	0,					/* va_nlink; */
105 	0,					/* va_size; */
106 	0,					/* va_atime; */
107 	0,					/* va_mtime; */
108 	0,					/* va_ctime; */
109 	0,					/* va_rdev; */
110 	0,					/* va_blksize; */
111 	0,					/* va_nblocks; */
112 	0,					/* va_seq; */
113 };
114 
115 vattr_t dv_vattr_priv = {
116 	AT_TYPE|AT_MODE|AT_SIZE|AT_UID|AT_GID|AT_RDEV,	/* va_mask */
117 	0,					/* va_type */
118 	DV_DEVMODE_PRIV,			/* va_mode */
119 	DV_UID_DEFAULT,				/* va_uid */
120 	DV_GID_DEFAULT,				/* va_gid */
121 	0,					/* va_fsid; */
122 	0,					/* va_nodeid; */
123 	0,					/* va_nlink; */
124 	0,					/* va_size; */
125 	0,					/* va_atime; */
126 	0,					/* va_mtime; */
127 	0,					/* va_ctime; */
128 	0,					/* va_rdev; */
129 	0,					/* va_blksize; */
130 	0,					/* va_nblocks; */
131 	0,					/* va_seq; */
132 };
133 
134 extern dev_info_t	*clone_dip;
135 extern major_t		clone_major;
136 extern struct dev_ops	*ddi_hold_driver(major_t);
137 
138 /* dev_info node cache constructor */
139 /*ARGSUSED1*/
140 static int
141 i_dv_node_ctor(void *buf, void *cfarg, int flag)
142 {
143 	struct dv_node	*dv = (struct dv_node *)buf;
144 	struct vnode	*vp;
145 
146 	bzero(buf, sizeof (struct dv_node));
147 	vp = dv->dv_vnode = vn_alloc(flag);
148 	if (vp == NULL) {
149 		return (-1);
150 	}
151 	vp->v_data = dv;
152 	rw_init(&dv->dv_contents, NULL, RW_DEFAULT, NULL);
153 	return (0);
154 }
155 
156 /* dev_info node cache destructor */
157 /*ARGSUSED1*/
158 static void
159 i_dv_node_dtor(void *buf, void *arg)
160 {
161 	struct dv_node	*dv = (struct dv_node *)buf;
162 	struct vnode	*vp = DVTOV(dv);
163 
164 	rw_destroy(&dv->dv_contents);
165 	vn_invalid(vp);
166 	vn_free(vp);
167 }
168 
169 
170 /* initialize dev_info node cache */
171 void
172 dv_node_cache_init()
173 {
174 	ASSERT(dv_node_cache == NULL);
175 	dv_node_cache = kmem_cache_create("dv_node_cache",
176 	    sizeof (struct dv_node), 0, i_dv_node_ctor, i_dv_node_dtor,
177 	    NULL, NULL, NULL, 0);
178 
179 	tsd_create(&devfs_clean_key, NULL);
180 }
181 
182 /* destroy dev_info node cache */
183 void
184 dv_node_cache_fini()
185 {
186 	ASSERT(dv_node_cache != NULL);
187 	kmem_cache_destroy(dv_node_cache);
188 	dv_node_cache = NULL;
189 
190 	tsd_destroy(&devfs_clean_key);
191 }
192 
193 /*
194  * dv_mkino - Generate a unique inode number for devfs nodes.
195  *
196  * Although ino_t is 64 bits, the inode number is truncated to 32 bits for 32
197  * bit non-LARGEFILE applications. This means that there is a requirement to
198  * maintain the inode number as a 32 bit value or applications will have
199  * stat(2) calls fail with EOVERFLOW.  We form a 32 bit inode number from the
200  * dev_t. but if the minor number is larger than L_MAXMIN32 we fold extra minor
201  *
202  * To generate inode numbers for directories, we assume that we will never use
203  * more than half the major space - this allows for ~8190 drivers. We use this
204  * upper major number space to allocate inode numbers for directories by
205  * encoding the major and instance into this space.
206  *
207  * We also skew the result so that inode 2 is reserved for the root of the file
208  * system.
209  *
210  * As part of the future support for 64-bit dev_t APIs, the upper minor bits
211  * should be folded into the high inode bits by adding the following code
212  * after "ino |= 1":
213  *
214  * #if (L_BITSMINOR32 != L_BITSMINOR)
215  *		|* fold overflow minor bits into high bits of inode number *|
216  *		ino |= ((ino_t)(minor >> L_BITSMINOR32)) << L_BITSMINOR;
217  * #endif |* (L_BITSMINOR32 != L_BITSMINOR) *|
218  *
219  * This way only applications that use devices that overflow their minor
220  * space will have an application level impact.
221  */
222 static ino_t
223 dv_mkino(dev_info_t *devi, vtype_t typ, dev_t dev)
224 {
225 	major_t		major;
226 	minor_t		minor;
227 	ino_t		ino;
228 	static int	warn;
229 
230 	if (typ == VDIR) {
231 		major = ((L_MAXMAJ32 + 1) >> 1) + DEVI(devi)->devi_major;
232 		minor = ddi_get_instance(devi);
233 
234 		/* makedevice32 in high half of major number space */
235 		ino = (ino_t)((major << L_BITSMINOR32) | (minor & L_MAXMIN32));
236 
237 		major = DEVI(devi)->devi_major;
238 	} else {
239 		major = getmajor(dev);
240 		minor = getminor(dev);
241 
242 		/* makedevice32 */
243 		ino = (ino_t)((major << L_BITSMINOR32) | (minor & L_MAXMIN32));
244 
245 		/* make ino for VCHR different than VBLK */
246 		ino <<= 1;
247 		if (typ == VCHR)
248 			ino |= 1;
249 	}
250 
251 	ino += DV_ROOTINO + 1;		/* skew */
252 
253 	/*
254 	 * diagnose things a little early because adding the skew to a large
255 	 * minor number could roll over the major.
256 	 */
257 	if ((major >= (L_MAXMAJ32 >> 1)) && (warn == 0)) {
258 		warn = 1;
259 		cmn_err(CE_WARN, "%s: inode numbers are not unique", dvnm);
260 	}
261 
262 	return (ino);
263 }
264 
265 /*
266  * Compare two nodes lexographically to balance avl tree
267  */
268 static int
269 dv_compare_nodes(const struct dv_node *dv1, const struct dv_node *dv2)
270 {
271 	int	rv;
272 
273 	if ((rv = strcmp(dv1->dv_name, dv2->dv_name)) == 0)
274 		return (0);
275 	return ((rv < 0) ? -1 : 1);
276 }
277 
278 /*
279  * dv_mkroot
280  *
281  * Build the first VDIR dv_node.
282  */
283 struct dv_node *
284 dv_mkroot(struct vfs *vfsp, dev_t devfsdev)
285 {
286 	struct dv_node	*dv;
287 	struct vnode	*vp;
288 
289 	ASSERT(ddi_root_node() != NULL);
290 	ASSERT(dv_node_cache != NULL);
291 
292 	dcmn_err3(("dv_mkroot\n"));
293 	dv = kmem_cache_alloc(dv_node_cache, KM_SLEEP);
294 	vp = DVTOV(dv);
295 	vn_reinit(vp);
296 	vp->v_flag = VROOT;
297 	vp->v_vfsp = vfsp;
298 	vp->v_type = VDIR;
299 	vp->v_rdev = devfsdev;
300 	vn_setops(vp, dv_vnodeops);
301 	vn_exists(vp);
302 
303 	dvroot = dv;
304 
305 	dv->dv_name = NULL;		/* not needed */
306 	dv->dv_namelen = 0;
307 
308 	dv->dv_devi = ddi_root_node();
309 
310 	dv->dv_ino = DV_ROOTINO;
311 	dv->dv_nlink = 2;		/* name + . (no dv_insert) */
312 	dv->dv_dotdot = dv;		/* .. == self */
313 	dv->dv_attrvp = NULLVP;
314 	dv->dv_attr = NULL;
315 	dv->dv_flags = DV_BUILD;
316 	dv->dv_priv = NULL;
317 	dv->dv_busy = 0;
318 	dv->dv_dflt_mode = 0;
319 
320 	avl_create(&dv->dv_entries,
321 	    (int (*)(const void *, const void *))dv_compare_nodes,
322 	    sizeof (struct dv_node), offsetof(struct dv_node, dv_avllink));
323 
324 	return (dv);
325 }
326 
327 /*
328  * dv_mkdir
329  *
330  * Given an probed or attached nexus node, create a VDIR dv_node.
331  * No dv_attrvp is created at this point.
332  */
333 struct dv_node *
334 dv_mkdir(struct dv_node *ddv, dev_info_t *devi, char *nm)
335 {
336 	struct dv_node	*dv;
337 	struct vnode	*vp;
338 	size_t		nmlen;
339 
340 	ASSERT((devi));
341 	dcmn_err4(("dv_mkdir: %s\n", nm));
342 
343 	dv = kmem_cache_alloc(dv_node_cache, KM_SLEEP);
344 	nmlen = strlen(nm) + 1;
345 	dv->dv_name = kmem_alloc(nmlen, KM_SLEEP);
346 	bcopy(nm, dv->dv_name, nmlen);
347 	dv->dv_namelen = nmlen - 1;	/* '\0' not included */
348 
349 	vp = DVTOV(dv);
350 	vn_reinit(vp);
351 	vp->v_flag = 0;
352 	vp->v_vfsp = DVTOV(ddv)->v_vfsp;
353 	vp->v_type = VDIR;
354 	vp->v_rdev = DVTOV(ddv)->v_rdev;
355 	vn_setops(vp, vn_getops(DVTOV(ddv)));
356 	vn_exists(vp);
357 
358 	dv->dv_devi = devi;
359 	ndi_hold_devi(devi);
360 
361 	dv->dv_ino = dv_mkino(devi, VDIR, NODEV);
362 	dv->dv_nlink = 0;		/* updated on insert */
363 	dv->dv_dotdot = ddv;
364 	dv->dv_attrvp = NULLVP;
365 	dv->dv_attr = NULL;
366 	dv->dv_flags = DV_BUILD;
367 	dv->dv_priv = NULL;
368 	dv->dv_busy = 0;
369 	dv->dv_dflt_mode = 0;
370 
371 	avl_create(&dv->dv_entries,
372 	    (int (*)(const void *, const void *))dv_compare_nodes,
373 	    sizeof (struct dv_node), offsetof(struct dv_node, dv_avllink));
374 
375 	return (dv);
376 }
377 
378 /*
379  * dv_mknod
380  *
381  * Given a minor node, create a VCHR or VBLK dv_node.
382  * No dv_attrvp is created at this point.
383  */
384 static struct dv_node *
385 dv_mknod(struct dv_node *ddv, dev_info_t *devi, char *nm,
386 	struct ddi_minor_data *dmd)
387 {
388 	struct dv_node	*dv;
389 	struct vnode	*vp;
390 	size_t		nmlen;
391 
392 	dcmn_err4(("dv_mknod: %s\n", nm));
393 
394 	dv = kmem_cache_alloc(dv_node_cache, KM_SLEEP);
395 	nmlen = strlen(nm) + 1;
396 	dv->dv_name = kmem_alloc(nmlen, KM_SLEEP);
397 	bcopy(nm, dv->dv_name, nmlen);
398 	dv->dv_namelen = nmlen - 1;	/* no '\0' */
399 
400 	vp = DVTOV(dv);
401 	vn_reinit(vp);
402 	vp->v_flag = 0;
403 	vp->v_vfsp = DVTOV(ddv)->v_vfsp;
404 	vp->v_type = dmd->ddm_spec_type == S_IFCHR ? VCHR : VBLK;
405 	vp->v_rdev = dmd->ddm_dev;
406 	vn_setops(vp, vn_getops(DVTOV(ddv)));
407 	vn_exists(vp);
408 
409 	/* increment dev_ref with devi_lock held */
410 	ASSERT(DEVI_BUSY_OWNED(devi));
411 	mutex_enter(&DEVI(devi)->devi_lock);
412 	dv->dv_devi = devi;
413 	DEVI(devi)->devi_ref++;
414 	mutex_exit(&DEVI(devi)->devi_lock);
415 
416 	dv->dv_ino = dv_mkino(devi, vp->v_type, vp->v_rdev);
417 	dv->dv_nlink = 0;		/* updated on insert */
418 	dv->dv_dotdot = ddv;
419 	dv->dv_attrvp = NULLVP;
420 	dv->dv_attr = NULL;
421 	dv->dv_flags = 0;
422 
423 	if (dmd->type == DDM_INTERNAL_PATH)
424 		dv->dv_flags |= DV_INTERNAL;
425 	if (dmd->ddm_flags & DM_NO_FSPERM)
426 		dv->dv_flags |= DV_NO_FSPERM;
427 
428 	dv->dv_priv = dmd->ddm_node_priv;
429 	if (dv->dv_priv)
430 		dphold(dv->dv_priv);
431 
432 	/*
433 	 * Minors created with ddi_create_priv_minor_node can specify
434 	 * a default mode permission other than the devfs default.
435 	 */
436 	if (dv->dv_priv || dv->dv_flags & DV_NO_FSPERM) {
437 		dcmn_err5(("%s: dv_mknod default priv mode 0%o\n",
438 		    dv->dv_name, dmd->ddm_priv_mode));
439 		dv->dv_flags |= DV_DFLT_MODE;
440 		dv->dv_dflt_mode = dmd->ddm_priv_mode & S_IAMB;
441 	}
442 
443 	return (dv);
444 }
445 
446 /*
447  * dv_destroy
448  *
449  * Destroy what we created in dv_mkdir or dv_mknod.
450  * In the case of a *referenced* directory, do nothing.
451  */
452 /*ARGSUSED1*/
453 void
454 dv_destroy(struct dv_node *dv, uint_t flags)
455 {
456 	vnode_t *vp = DVTOV(dv);
457 	ASSERT(dv->dv_nlink == 0);		/* no references */
458 
459 	dcmn_err4(("dv_destroy: %s\n", dv->dv_name));
460 
461 	/*
462 	 * We may be asked to unlink referenced directories.
463 	 * In this case, there is nothing to be done.
464 	 * The eventual memory free will be done in
465 	 * devfs_inactive.
466 	 */
467 	if (vp->v_count != 0) {
468 		ASSERT(vp->v_type == VDIR);
469 		ASSERT(flags & DV_CLEAN_FORCE);
470 		ASSERT(DV_STALE(dv));
471 		return;
472 	}
473 
474 	if (vp->v_type == VDIR) {
475 		ASSERT(DV_FIRST_ENTRY(dv) == NULL);
476 		avl_destroy(&dv->dv_entries);
477 	}
478 
479 	if (dv->dv_attrvp != NULLVP)
480 		VN_RELE(dv->dv_attrvp);
481 	if (dv->dv_attr != NULL)
482 		kmem_free(dv->dv_attr, sizeof (struct vattr));
483 	if (dv->dv_name != NULL)
484 		kmem_free(dv->dv_name, dv->dv_namelen + 1);
485 	if (dv->dv_devi != NULL) {
486 		ndi_rele_devi(dv->dv_devi);
487 	}
488 	if (dv->dv_priv != NULL) {
489 		dpfree(dv->dv_priv);
490 	}
491 
492 	kmem_cache_free(dv_node_cache, dv);
493 }
494 
495 /*
496  * Find and hold dv_node by name
497  */
498 static struct dv_node *
499 dv_findbyname(struct dv_node *ddv, char *nm)
500 {
501 	struct dv_node  *dv;
502 	avl_index_t	where;
503 	struct dv_node	dvtmp;
504 
505 	ASSERT(RW_LOCK_HELD(&ddv->dv_contents));
506 	dcmn_err3(("dv_findbyname: %s\n", nm));
507 
508 	dvtmp.dv_name = nm;
509 	dv = avl_find(&ddv->dv_entries, &dvtmp, &where);
510 	if (dv) {
511 		ASSERT(dv->dv_dotdot == ddv);
512 		ASSERT(strcmp(dv->dv_name, nm) == 0);
513 		VN_HOLD(DVTOV(dv));
514 		return (dv);
515 	}
516 	return (NULL);
517 }
518 
519 /*
520  * Inserts a new dv_node in a parent directory
521  */
522 void
523 dv_insert(struct dv_node *ddv, struct dv_node *dv)
524 {
525 	avl_index_t	where;
526 
527 	ASSERT(RW_WRITE_HELD(&ddv->dv_contents));
528 	ASSERT(DVTOV(ddv)->v_type == VDIR);
529 	ASSERT(ddv->dv_nlink >= 2);
530 	ASSERT(dv->dv_nlink == 0);
531 
532 	dcmn_err3(("dv_insert: %s\n", dv->dv_name));
533 
534 	dv->dv_dotdot = ddv;
535 	if (DVTOV(dv)->v_type == VDIR) {
536 		ddv->dv_nlink++;	/* .. to containing directory */
537 		dv->dv_nlink = 2;	/* name + . */
538 	} else {
539 		dv->dv_nlink = 1;	/* name */
540 	}
541 
542 	/* enter node in the avl tree */
543 	VERIFY(avl_find(&ddv->dv_entries, dv, &where) == NULL);
544 	avl_insert(&ddv->dv_entries, dv, where);
545 }
546 
547 /*
548  * Unlink a dv_node from a perent directory
549  */
550 void
551 dv_unlink(struct dv_node *ddv, struct dv_node *dv)
552 {
553 	/* verify linkage of arguments */
554 	ASSERT(ddv && dv);
555 	ASSERT(dv->dv_dotdot == ddv);
556 	ASSERT(RW_WRITE_HELD(&ddv->dv_contents));
557 	ASSERT(DVTOV(ddv)->v_type == VDIR);
558 
559 	dcmn_err3(("dv_unlink: %s\n", dv->dv_name));
560 
561 	if (DVTOV(dv)->v_type == VDIR) {
562 		ddv->dv_nlink--;	/* .. to containing directory */
563 		dv->dv_nlink -= 2;	/* name + . */
564 	} else {
565 		dv->dv_nlink -= 1;	/* name */
566 	}
567 	ASSERT(ddv->dv_nlink >= 2);
568 	ASSERT(dv->dv_nlink == 0);
569 
570 	dv->dv_dotdot = NULL;
571 
572 	/* remove from avl tree */
573 	avl_remove(&ddv->dv_entries, dv);
574 }
575 
576 /*
577  * Merge devfs node specific information into an attribute structure.
578  *
579  * NOTE: specfs provides ATIME,MTIME,CTIME,SIZE,BLKSIZE,NBLOCKS on leaf node.
580  */
581 void
582 dv_vattr_merge(struct dv_node *dv, struct vattr *vap)
583 {
584 	struct vnode	*vp = DVTOV(dv);
585 
586 	vap->va_nodeid = dv->dv_ino;
587 	vap->va_nlink = dv->dv_nlink;
588 
589 	if (vp->v_type == VDIR) {
590 		vap->va_rdev = 0;
591 		vap->va_fsid = vp->v_rdev;
592 	} else {
593 		vap->va_rdev = vp->v_rdev;
594 		vap->va_fsid = DVTOV(dv->dv_dotdot)->v_rdev;
595 		vap->va_type = vp->v_type;
596 		/* don't trust the shadow file type */
597 		vap->va_mode &= ~S_IFMT;
598 		if (vap->va_type == VCHR)
599 			vap->va_mode |= S_IFCHR;
600 		else
601 			vap->va_mode |= S_IFBLK;
602 	}
603 }
604 
605 /*
606  * Get default device permission by consulting rules in
607  * privilege specification in minor node and /etc/minor_perm.
608  *
609  * This function is called from the devname filesystem to get default
610  * permissions for a device exported to a non-global zone.
611  */
612 void
613 devfs_get_defattr(struct vnode *vp, struct vattr *vap, int *no_fs_perm)
614 {
615 	mperm_t		mp;
616 	struct dv_node	*dv;
617 
618 	/* If vp isn't a dv_node, return something sensible */
619 	if (!vn_matchops(vp, dv_vnodeops)) {
620 		if (no_fs_perm)
621 			*no_fs_perm = 0;
622 		*vap = dv_vattr_file;
623 		return;
624 	}
625 
626 	/*
627 	 * For minors not created by ddi_create_priv_minor_node(),
628 	 * use devfs defaults.
629 	 */
630 	dv = VTODV(vp);
631 	if (vp->v_type == VDIR) {
632 		*vap = dv_vattr_dir;
633 	} else if (dv->dv_flags & DV_NO_FSPERM) {
634 		if (no_fs_perm)
635 			*no_fs_perm = 1;
636 		*vap = dv_vattr_priv;
637 	} else {
638 		/*
639 		 * look up perm bits from minor_perm
640 		 */
641 		*vap = dv_vattr_file;
642 		if (dev_minorperm(dv->dv_devi, dv->dv_name, &mp) == 0) {
643 			VATTR_MP_MERGE((*vap), mp);
644 			dcmn_err5(("%s: minor perm mode 0%o\n",
645 			    dv->dv_name, vap->va_mode));
646 		} else if (dv->dv_flags & DV_DFLT_MODE) {
647 			ASSERT((dv->dv_dflt_mode & ~S_IAMB) == 0);
648 			vap->va_mode &= ~S_IAMB;
649 			vap->va_mode |= dv->dv_dflt_mode;
650 			dcmn_err5(("%s: priv mode 0%o\n",
651 			    dv->dv_name, vap->va_mode));
652 		}
653 	}
654 }
655 
656 /*
657  * dv_shadow_node
658  *
659  * Given a VDIR dv_node, find/create the associated VDIR
660  * node in the shadow attribute filesystem.
661  *
662  * Given a VCHR/VBLK dv_node, find the associated VREG
663  * node in the shadow attribute filesystem.  These nodes
664  * are only created to persist non-default attributes.
665  * Lack of such a node implies the default permissions
666  * are sufficient.
667  *
668  * Managing the attribute file entries is slightly tricky (mostly
669  * because we can't intercept VN_HOLD and VN_RELE except on the last
670  * release).
671  *
672  * We assert that if the dv_attrvp pointer is non-NULL, it points
673  * to a singly-held (by us) vnode that represents the shadow entry
674  * in the underlying filesystem.  To avoid store-ordering issues,
675  * we assert that the pointer can only be tested under the dv_contents
676  * READERS lock.
677  */
678 
679 void
680 dv_shadow_node(
681 	struct vnode *dvp,	/* devfs parent directory vnode */
682 	char *nm,		/* name component */
683 	struct vnode *vp,	/* devfs vnode */
684 	struct pathname *pnp,	/* the path .. */
685 	struct vnode *rdir,	/* the root .. */
686 	struct cred *cred,	/* who's asking? */
687 	int flags)		/* optionally create shadow node */
688 {
689 	struct dv_node	*dv;	/* dv_node of named directory */
690 	struct vnode	*rdvp;	/* shadow parent directory vnode */
691 	struct vnode	*rvp;	/* shadow vnode */
692 	struct vnode	*rrvp;	/* realvp of shadow vnode */
693 	struct vattr	vattr;
694 	int		create_tried;
695 	int		error;
696 
697 	ASSERT(vp->v_type == VDIR || vp->v_type == VCHR || vp->v_type == VBLK);
698 	dv = VTODV(vp);
699 	dcmn_err3(("dv_shadow_node: name %s attr %p\n",
700 	    nm, (void *)dv->dv_attrvp));
701 
702 	if ((flags & DV_SHADOW_WRITE_HELD) == 0) {
703 		ASSERT(RW_READ_HELD(&dv->dv_contents));
704 		if (dv->dv_attrvp != NULLVP)
705 			return;
706 		if (!rw_tryupgrade(&dv->dv_contents)) {
707 			rw_exit(&dv->dv_contents);
708 			rw_enter(&dv->dv_contents, RW_WRITER);
709 			if (dv->dv_attrvp != NULLVP) {
710 				rw_downgrade(&dv->dv_contents);
711 				return;
712 			}
713 		}
714 	} else {
715 		ASSERT(RW_WRITE_HELD(&dv->dv_contents));
716 		if (dv->dv_attrvp != NULLVP)
717 			return;
718 	}
719 
720 	ASSERT(RW_WRITE_HELD(&dv->dv_contents) && dv->dv_attrvp == NULL);
721 
722 	rdvp = VTODV(dvp)->dv_attrvp;
723 	create_tried = 0;
724 lookup:
725 	if (rdvp && (dv->dv_flags & DV_NO_FSPERM) == 0) {
726 		error = VOP_LOOKUP(rdvp, nm, &rvp, pnp, LOOKUP_DIR, rdir, cred,
727 		    NULL, NULL, NULL);
728 
729 		/* factor out the snode since we only want the attribute node */
730 		if ((error == 0) && (VOP_REALVP(rvp, &rrvp, NULL) == 0)) {
731 			VN_HOLD(rrvp);
732 			VN_RELE(rvp);
733 			rvp = rrvp;
734 		}
735 	} else
736 		error = EROFS;		/* no parent, no entry */
737 
738 	/*
739 	 * All we want is the permissions (and maybe ACLs and
740 	 * extended attributes), and we want to perform lookups
741 	 * by name.  Drivers occasionally change their minor
742 	 * number space.  If something changes, there's no
743 	 * much we can do about it here.
744 	 */
745 
746 	/* The shadow node checks out. We are done */
747 	if (error == 0) {
748 		dv->dv_attrvp = rvp;	/* with one hold */
749 
750 		/*
751 		 * Determine if we have non-trivial ACLs on this node.
752 		 * It is not necessary to VOP_RWLOCK since fs_acl_nontrivial
753 		 * only does VOP_GETSECATTR.
754 		 */
755 		dv->dv_flags &= ~DV_ACL;
756 
757 		if (fs_acl_nontrivial(rvp, cred))
758 			dv->dv_flags |= DV_ACL;
759 
760 		/*
761 		 * If we have synced out the memory attributes, free
762 		 * them and switch back to using the persistent store.
763 		 */
764 		if (rvp && dv->dv_attr) {
765 			kmem_free(dv->dv_attr, sizeof (struct vattr));
766 			dv->dv_attr = NULL;
767 		}
768 		if ((flags & DV_SHADOW_WRITE_HELD) == 0)
769 			rw_downgrade(&dv->dv_contents);
770 		ASSERT(RW_LOCK_HELD(&dv->dv_contents));
771 		return;
772 	}
773 
774 	/*
775 	 * Failed to find attribute in persistent backing store,
776 	 * get default permission bits.
777 	 */
778 	devfs_get_defattr(vp, &vattr, NULL);
779 
780 	dv_vattr_merge(dv, &vattr);
781 	gethrestime(&vattr.va_atime);
782 	vattr.va_mtime = vattr.va_atime;
783 	vattr.va_ctime = vattr.va_atime;
784 
785 	/*
786 	 * Try to create shadow dir. This is necessary in case
787 	 * we need to create a shadow leaf node later, when user
788 	 * executes chmod.
789 	 */
790 	if ((error == ENOENT) && !create_tried) {
791 		switch (vp->v_type) {
792 		case VDIR:
793 			error = VOP_MKDIR(rdvp, nm, &vattr, &rvp, kcred,
794 			    NULL, 0, NULL);
795 			dsysdebug(error, ("vop_mkdir %s %s %d\n",
796 			    VTODV(dvp)->dv_name, nm, error));
797 			create_tried = 1;
798 			break;
799 
800 		case VCHR:
801 		case VBLK:
802 			/*
803 			 * Shadow nodes are only created on demand
804 			 */
805 			if (flags & DV_SHADOW_CREATE) {
806 				error = VOP_CREATE(rdvp, nm, &vattr, NONEXCL,
807 				    VREAD|VWRITE, &rvp, kcred, 0, NULL, NULL);
808 				dsysdebug(error, ("vop_create %s %s %d\n",
809 				    VTODV(dvp)->dv_name, nm, error));
810 				create_tried = 1;
811 			}
812 			break;
813 
814 		default:
815 			cmn_err(CE_PANIC, "devfs: %s: create", dvnm);
816 			/*NOTREACHED*/
817 		}
818 
819 		if (create_tried &&
820 		    (error == 0) || (error == EEXIST)) {
821 			VN_RELE(rvp);
822 			goto lookup;
823 		}
824 	}
825 
826 	/* Store attribute in memory */
827 	if (dv->dv_attr == NULL) {
828 		dv->dv_attr = kmem_alloc(sizeof (struct vattr), KM_SLEEP);
829 		*(dv->dv_attr) = vattr;
830 	}
831 
832 	if ((flags & DV_SHADOW_WRITE_HELD) == 0)
833 		rw_downgrade(&dv->dv_contents);
834 	ASSERT(RW_LOCK_HELD(&dv->dv_contents));
835 }
836 
837 /*
838  * Given a devinfo node, and a name, returns the appropriate
839  * minor information for that named node, if it exists.
840  */
841 static int
842 dv_find_leafnode(dev_info_t *devi, char *minor_nm, struct ddi_minor_data *r_mi)
843 {
844 	struct ddi_minor_data	*dmd;
845 
846 	ASSERT(i_ddi_devi_attached(devi));
847 
848 	dcmn_err3(("dv_find_leafnode: %s\n", minor_nm));
849 	ASSERT(DEVI_BUSY_OWNED(devi));
850 	for (dmd = DEVI(devi)->devi_minor; dmd; dmd = dmd->next) {
851 
852 		/*
853 		 * Skip alias nodes and nodes without a name.
854 		 */
855 		if ((dmd->type == DDM_ALIAS) || (dmd->ddm_name == NULL))
856 			continue;
857 
858 		dcmn_err4(("dv_find_leafnode: (%s,%s)\n",
859 		    minor_nm, dmd->ddm_name));
860 		if (strcmp(minor_nm, dmd->ddm_name) == 0) {
861 			r_mi->ddm_dev = dmd->ddm_dev;
862 			r_mi->ddm_spec_type = dmd->ddm_spec_type;
863 			r_mi->type = dmd->type;
864 			r_mi->ddm_flags = dmd->ddm_flags;
865 			r_mi->ddm_node_priv = dmd->ddm_node_priv;
866 			r_mi->ddm_priv_mode = dmd->ddm_priv_mode;
867 			if (r_mi->ddm_node_priv)
868 				dphold(r_mi->ddm_node_priv);
869 			return (0);
870 		}
871 	}
872 
873 	dcmn_err3(("dv_find_leafnode: %s: ENOENT\n", minor_nm));
874 	return (ENOENT);
875 }
876 
877 /*
878  * Special handling for clone node:
879  *	Clone minor name is a driver name, the minor number will
880  *	be the major number of the driver. There is no minor
881  *	node under the clone driver, so we'll manufacture the
882  *	dev_t.
883  */
884 static struct dv_node *
885 dv_clone_mknod(struct dv_node *ddv, char *drvname)
886 {
887 	major_t			major;
888 	struct dv_node		*dvp;
889 	char			*devnm;
890 	struct ddi_minor_data	*dmd;
891 
892 	/*
893 	 * Make sure drvname is a STREAMS driver. We load the driver,
894 	 * but don't attach to any instances. This makes stat(2)
895 	 * relatively cheap.
896 	 */
897 	major = ddi_name_to_major(drvname);
898 	if (major == DDI_MAJOR_T_NONE)
899 		return (NULL);
900 
901 	if (ddi_hold_driver(major) == NULL)
902 		return (NULL);
903 
904 	if (STREAMSTAB(major) == NULL) {
905 		ddi_rele_driver(major);
906 		return (NULL);
907 	}
908 
909 	ddi_rele_driver(major);
910 	devnm = kmem_alloc(MAXNAMELEN, KM_SLEEP);
911 	(void) snprintf(devnm, MAXNAMELEN, "clone@0:%s", drvname);
912 	dmd = kmem_zalloc(sizeof (*dmd), KM_SLEEP);
913 	dmd->ddm_dev = makedevice(clone_major, (minor_t)major);
914 	dmd->ddm_spec_type = S_IFCHR;
915 	dvp = dv_mknod(ddv, clone_dip, devnm, dmd);
916 	kmem_free(dmd, sizeof (*dmd));
917 	kmem_free(devnm, MAXNAMELEN);
918 	return (dvp);
919 }
920 
921 /*
922  * Given the parent directory node, and a name in it, returns the
923  * named dv_node to the caller (as a vnode).
924  *
925  * (We need pnp and rdir for doing shadow lookups; they can be NULL)
926  */
927 int
928 dv_find(struct dv_node *ddv, char *nm, struct vnode **vpp, struct pathname *pnp,
929 	struct vnode *rdir, struct cred *cred, uint_t ndi_flags)
930 {
931 	extern int isminiroot;	/* see modctl.c */
932 
933 	int			circ;
934 	int			rv = 0, was_busy = 0, nmlen, write_held = 0;
935 	struct vnode		*vp;
936 	struct dv_node		*dv, *dup;
937 	dev_info_t		*pdevi, *devi = NULL;
938 	char			*mnm;
939 	struct ddi_minor_data	*dmd;
940 
941 	dcmn_err3(("dv_find %s\n", nm));
942 
943 	rw_enter(&ddv->dv_contents, RW_READER);
944 start:
945 	if (DV_STALE(ddv)) {
946 		rw_exit(&ddv->dv_contents);
947 		return (ESTALE);
948 	}
949 
950 	/*
951 	 * Empty name or ., return node itself.
952 	 */
953 	nmlen = strlen(nm);
954 	if ((nmlen == 0) || ((nmlen == 1) && (nm[0] == '.'))) {
955 		*vpp = DVTOV(ddv);
956 		rw_exit(&ddv->dv_contents);
957 		VN_HOLD(*vpp);
958 		return (0);
959 	}
960 
961 	/*
962 	 * .., return the parent directory
963 	 */
964 	if ((nmlen == 2) && (strcmp(nm, "..") == 0)) {
965 		*vpp = DVTOV(ddv->dv_dotdot);
966 		rw_exit(&ddv->dv_contents);
967 		VN_HOLD(*vpp);
968 		return (0);
969 	}
970 
971 	/*
972 	 * Fail anything without a valid device name component
973 	 */
974 	if (nm[0] == '@' || nm[0] == ':') {
975 		dcmn_err3(("devfs: no driver '%s'\n", nm));
976 		rw_exit(&ddv->dv_contents);
977 		return (ENOENT);
978 	}
979 
980 	/*
981 	 * So, now we have to deal with the trickier stuff.
982 	 *
983 	 * (a) search the existing list of dv_nodes on this directory
984 	 */
985 	if ((dv = dv_findbyname(ddv, nm)) != NULL) {
986 founddv:
987 		ASSERT(RW_LOCK_HELD(&ddv->dv_contents));
988 
989 		if (!rw_tryenter(&dv->dv_contents, RW_READER)) {
990 			if (tsd_get(devfs_clean_key)) {
991 				VN_RELE(DVTOV(dv));
992 				rw_exit(&ddv->dv_contents);
993 				return (EBUSY);
994 			}
995 			rw_enter(&dv->dv_contents, RW_READER);
996 		}
997 
998 		vp = DVTOV(dv);
999 		if ((dv->dv_attrvp != NULLVP) ||
1000 		    (vp->v_type != VDIR && dv->dv_attr != NULL)) {
1001 			/*
1002 			 * Common case - we already have attributes
1003 			 */
1004 			rw_exit(&dv->dv_contents);
1005 			rw_exit(&ddv->dv_contents);
1006 			goto found;
1007 		}
1008 
1009 		/*
1010 		 * No attribute vp, try and build one.
1011 		 *
1012 		 * dv_shadow_node() can briefly drop &dv->dv_contents lock
1013 		 * if it is unable to upgrade it to a write lock. If the
1014 		 * current thread has come in through the bottom-up device
1015 		 * configuration devfs_clean() path, we may deadlock against
1016 		 * a thread performing top-down device configuration if it
1017 		 * grabs the contents lock. To avoid this, when we are on the
1018 		 * devfs_clean() path we attempt to upgrade the dv_contents
1019 		 * lock before we call dv_shadow_node().
1020 		 */
1021 		if (tsd_get(devfs_clean_key)) {
1022 			if (!rw_tryupgrade(&dv->dv_contents)) {
1023 				VN_RELE(DVTOV(dv));
1024 				rw_exit(&dv->dv_contents);
1025 				rw_exit(&ddv->dv_contents);
1026 				return (EBUSY);
1027 			}
1028 
1029 			write_held = DV_SHADOW_WRITE_HELD;
1030 		}
1031 
1032 		dv_shadow_node(DVTOV(ddv), nm, vp, pnp, rdir, cred,
1033 		    write_held);
1034 
1035 		rw_exit(&dv->dv_contents);
1036 		rw_exit(&ddv->dv_contents);
1037 		goto found;
1038 	}
1039 
1040 	/*
1041 	 * (b) Search the child devinfo nodes of our parent directory,
1042 	 * looking for the named node.  If we find it, build a new
1043 	 * node, then grab the writers lock, search the directory
1044 	 * if it's still not there, then insert it.
1045 	 *
1046 	 * We drop the devfs locks before accessing the device tree.
1047 	 * Take care to mark the node BUSY so that a forced devfs_clean
1048 	 * doesn't mark the directory node stale.
1049 	 *
1050 	 * Also, check if we are called as part of devfs_clean or
1051 	 * reset_perm. If so, simply return not found because there
1052 	 * is nothing to clean.
1053 	 */
1054 	if (tsd_get(devfs_clean_key)) {
1055 		rw_exit(&ddv->dv_contents);
1056 		return (ENOENT);
1057 	}
1058 
1059 	/*
1060 	 * We could be either READ or WRITE locked at
1061 	 * this point. Upgrade if we are read locked.
1062 	 */
1063 	ASSERT(RW_LOCK_HELD(&ddv->dv_contents));
1064 	if (rw_read_locked(&ddv->dv_contents) &&
1065 	    !rw_tryupgrade(&ddv->dv_contents)) {
1066 		rw_exit(&ddv->dv_contents);
1067 		rw_enter(&ddv->dv_contents, RW_WRITER);
1068 		/*
1069 		 * Things may have changed when we dropped
1070 		 * the contents lock, so start from top again
1071 		 */
1072 		goto start;
1073 	}
1074 	ddv->dv_busy++;		/* mark busy before dropping lock */
1075 	was_busy++;
1076 	rw_exit(&ddv->dv_contents);
1077 
1078 	pdevi = ddv->dv_devi;
1079 	ASSERT(pdevi != NULL);
1080 
1081 	mnm = strchr(nm, ':');
1082 	if (mnm)
1083 		*mnm = (char)0;
1084 
1085 	/*
1086 	 * Configure one nexus child, will call nexus's bus_ops
1087 	 * If successful, devi is held upon returning.
1088 	 * Note: devfs lookup should not be configuring grandchildren.
1089 	 */
1090 	ASSERT((ndi_flags & NDI_CONFIG) == 0);
1091 
1092 	rv = ndi_devi_config_one(pdevi, nm, &devi, ndi_flags | NDI_NO_EVENT);
1093 	if (mnm)
1094 		*mnm = ':';
1095 	if (rv != NDI_SUCCESS) {
1096 		rv = ENOENT;
1097 		goto notfound;
1098 	}
1099 
1100 	/*
1101 	 * Don't make vhci clients visible under phci, unless we
1102 	 * are in miniroot.
1103 	 */
1104 	if (isminiroot == 0 && ddi_get_parent(devi) != pdevi) {
1105 		ndi_rele_devi(devi);
1106 		rv = ENOENT;
1107 		goto notfound;
1108 	}
1109 
1110 	ASSERT(devi && i_ddi_devi_attached(devi));
1111 
1112 	/*
1113 	 * Invalidate cache to notice newly created minor nodes.
1114 	 */
1115 	rw_enter(&ddv->dv_contents, RW_WRITER);
1116 	ddv->dv_flags |= DV_BUILD;
1117 	rw_exit(&ddv->dv_contents);
1118 
1119 	/*
1120 	 * mkdir for nexus drivers and leaf nodes as well.  If we are racing
1121 	 * and create a duplicate, the duplicate will be destroyed below.
1122 	 */
1123 	if (mnm == NULL) {
1124 		dv = dv_mkdir(ddv, devi, nm);
1125 	} else {
1126 		/*
1127 		 * Allocate dmd first to avoid KM_SLEEP with active
1128 		 * ndi_devi_enter.
1129 		 */
1130 		dmd = kmem_zalloc(sizeof (*dmd), KM_SLEEP);
1131 		ndi_devi_enter(devi, &circ);
1132 		if (devi == clone_dip) {
1133 			/*
1134 			 * For clone minors, load the driver indicated by
1135 			 * minor name.
1136 			 */
1137 			dv = dv_clone_mknod(ddv, mnm + 1);
1138 		} else {
1139 			/*
1140 			 * Find minor node and make a dv_node
1141 			 */
1142 			if (dv_find_leafnode(devi, mnm + 1, dmd) == 0) {
1143 				dv = dv_mknod(ddv, devi, nm, dmd);
1144 				if (dmd->ddm_node_priv)
1145 					dpfree(dmd->ddm_node_priv);
1146 			}
1147 		}
1148 		ndi_devi_exit(devi, circ);
1149 		kmem_free(dmd, sizeof (*dmd));
1150 	}
1151 	/*
1152 	 * Release hold from ndi_devi_config_one()
1153 	 */
1154 	ndi_rele_devi(devi);
1155 
1156 	if (dv == NULL) {
1157 		rv = ENOENT;
1158 		goto notfound;
1159 	}
1160 
1161 	/*
1162 	 * We have released the dv_contents lock, need to check
1163 	 * if another thread already created a duplicate node
1164 	 */
1165 	rw_enter(&ddv->dv_contents, RW_WRITER);
1166 	if ((dup = dv_findbyname(ddv, nm)) == NULL) {
1167 		dv_insert(ddv, dv);
1168 	} else {
1169 		/*
1170 		 * Duplicate found, use the existing node
1171 		 */
1172 		VN_RELE(DVTOV(dv));
1173 		dv_destroy(dv, 0);
1174 		dv = dup;
1175 	}
1176 	goto founddv;
1177 	/*NOTREACHED*/
1178 
1179 found:
1180 	/*
1181 	 * Skip non-kernel lookups of internal nodes.
1182 	 * This use of kcred to distinguish between user and
1183 	 * internal kernel lookups is unfortunate.  The information
1184 	 * provided by the seg argument to lookupnameat should
1185 	 * evolve into a lookup flag for filesystems that need
1186 	 * this distinction.
1187 	 */
1188 	if ((dv->dv_flags & DV_INTERNAL) && (cred != kcred)) {
1189 		VN_RELE(vp);
1190 		rv = ENOENT;
1191 		goto notfound;
1192 	}
1193 
1194 	dcmn_err2(("dv_find: returning vp for nm %s\n", nm));
1195 	if (vp->v_type == VCHR || vp->v_type == VBLK) {
1196 		/*
1197 		 * If vnode is a device, return special vnode instead
1198 		 * (though it knows all about -us- via sp->s_realvp,
1199 		 * sp->s_devvp, and sp->s_dip)
1200 		 */
1201 		*vpp = specvp_devfs(vp, vp->v_rdev, vp->v_type, cred,
1202 		    dv->dv_devi);
1203 		VN_RELE(vp);
1204 		if (*vpp == NULLVP)
1205 			rv = ENOSYS;
1206 	} else
1207 		*vpp = vp;
1208 
1209 notfound:
1210 	rw_enter(&ddv->dv_contents, RW_WRITER);
1211 	if (was_busy)
1212 		ddv->dv_busy--;
1213 	rw_exit(&ddv->dv_contents);
1214 	return (rv);
1215 }
1216 
1217 /*
1218  * The given directory node is out-of-date; that is, it has been
1219  * marked as needing to be rebuilt, possibly because some new devinfo
1220  * node has come into existence, or possibly because this is the first
1221  * time we've been here.
1222  */
1223 void
1224 dv_filldir(struct dv_node *ddv)
1225 {
1226 	struct dv_node		*dv;
1227 	dev_info_t		*devi, *pdevi;
1228 	struct ddi_minor_data	*dmd;
1229 	char			devnm[MAXNAMELEN];
1230 	int			circ, ccirc;
1231 
1232 	ASSERT(DVTOV(ddv)->v_type == VDIR);
1233 	ASSERT(RW_WRITE_HELD(&ddv->dv_contents));
1234 	ASSERT(ddv->dv_flags & DV_BUILD);
1235 
1236 	dcmn_err3(("dv_filldir: %s\n", ddv->dv_name));
1237 	if (DV_STALE(ddv))
1238 		return;
1239 	pdevi = ddv->dv_devi;
1240 
1241 	if (ndi_devi_config(pdevi, NDI_NO_EVENT) != NDI_SUCCESS) {
1242 		dcmn_err3(("dv_filldir: config error %s\n", ddv->dv_name));
1243 	}
1244 
1245 	ndi_devi_enter(pdevi, &circ);
1246 	for (devi = ddi_get_child(pdevi); devi;
1247 	    devi = ddi_get_next_sibling(devi)) {
1248 		if (i_ddi_node_state(devi) < DS_PROBED)
1249 			continue;
1250 
1251 		dcmn_err3(("dv_filldir: node %s\n", ddi_node_name(devi)));
1252 
1253 		ndi_devi_enter(devi, &ccirc);
1254 		for (dmd = DEVI(devi)->devi_minor; dmd; dmd = dmd->next) {
1255 			char *addr;
1256 
1257 			/*
1258 			 * Skip alias nodes, internal nodes, and nodes
1259 			 * without a name.  We allow DDM_DEFAULT nodes
1260 			 * to appear in readdir.
1261 			 */
1262 			if ((dmd->type == DDM_ALIAS) ||
1263 			    (dmd->type == DDM_INTERNAL_PATH) ||
1264 			    (dmd->ddm_name == NULL))
1265 				continue;
1266 
1267 			addr = ddi_get_name_addr(devi);
1268 			if (addr && *addr)
1269 				(void) sprintf(devnm, "%s@%s:%s",
1270 				    ddi_node_name(devi), addr, dmd->ddm_name);
1271 			else
1272 				(void) sprintf(devnm, "%s:%s",
1273 				    ddi_node_name(devi), dmd->ddm_name);
1274 
1275 			if ((dv = dv_findbyname(ddv, devnm)) != NULL) {
1276 				/* dv_node already exists */
1277 				VN_RELE(DVTOV(dv));
1278 				continue;
1279 			}
1280 
1281 			dv = dv_mknod(ddv, devi, devnm, dmd);
1282 			dv_insert(ddv, dv);
1283 			VN_RELE(DVTOV(dv));
1284 		}
1285 		ndi_devi_exit(devi, ccirc);
1286 
1287 		(void) ddi_deviname(devi, devnm);
1288 		if ((dv = dv_findbyname(ddv, devnm + 1)) == NULL) {
1289 			/* directory doesn't exist */
1290 			dv = dv_mkdir(ddv, devi, devnm + 1);
1291 			dv_insert(ddv, dv);
1292 		}
1293 		VN_RELE(DVTOV(dv));
1294 	}
1295 	ndi_devi_exit(pdevi, circ);
1296 
1297 	ddv->dv_flags &= ~DV_BUILD;
1298 }
1299 
1300 /*
1301  * Given a directory node, clean out all the nodes beneath.
1302  *
1303  * VDIR:	Reinvoke to clean them, then delete the directory.
1304  * VCHR, VBLK:	Just blow them away.
1305  *
1306  * Mark the directories touched as in need of a rebuild, in case
1307  * we fall over part way through. When DV_CLEAN_FORCE is specified,
1308  * we mark referenced empty directories as stale to facilitate DR.
1309  */
1310 int
1311 dv_cleandir(struct dv_node *ddv, char *devnm, uint_t flags)
1312 {
1313 	struct dv_node	*dv;
1314 	struct dv_node	*next;
1315 	struct vnode	*vp;
1316 	int		busy = 0;
1317 
1318 	/*
1319 	 * We should always be holding the tsd_clean_key here: dv_cleandir()
1320 	 * will be called as a result of a devfs_clean request and the
1321 	 * tsd_clean_key will be set in either in devfs_clean() itself or in
1322 	 * devfs_clean_vhci().
1323 	 *
1324 	 * Since we are on the devfs_clean path, we return EBUSY if we cannot
1325 	 * get the contents lock: if we blocked here we might deadlock against
1326 	 * a thread performing top-down device configuration.
1327 	 */
1328 	ASSERT(tsd_get(devfs_clean_key));
1329 
1330 	dcmn_err3(("dv_cleandir: %s\n", ddv->dv_name));
1331 
1332 	if (!(flags & DV_CLEANDIR_LCK) &&
1333 	    !rw_tryenter(&ddv->dv_contents, RW_WRITER))
1334 		return (EBUSY);
1335 
1336 	for (dv = DV_FIRST_ENTRY(ddv); dv; dv = next) {
1337 		next = DV_NEXT_ENTRY(ddv, dv);
1338 
1339 		/*
1340 		 * If devnm is specified, the non-minor portion of the
1341 		 * name must match devnm.
1342 		 */
1343 		if (devnm &&
1344 		    (strncmp(devnm, dv->dv_name, strlen(devnm)) ||
1345 		    (dv->dv_name[strlen(devnm)] != ':' &&
1346 		    dv->dv_name[strlen(devnm)] != '\0')))
1347 			continue;
1348 
1349 		/* check type of what we are cleaning */
1350 		vp = DVTOV(dv);
1351 		if (vp->v_type == VDIR) {
1352 			/* recurse on directories */
1353 			rw_enter(&dv->dv_contents, RW_WRITER);
1354 			if (dv_cleandir(dv, NULL,
1355 			    flags | DV_CLEANDIR_LCK) == EBUSY) {
1356 				rw_exit(&dv->dv_contents);
1357 				goto set_busy;
1358 			}
1359 
1360 			/* A clean directory is an empty directory... */
1361 			ASSERT(dv->dv_nlink == 2);
1362 			mutex_enter(&vp->v_lock);
1363 			if (vp->v_count > 0) {
1364 				/*
1365 				 * ... but an empty directory can still have
1366 				 * references to it. If we have dv_busy or
1367 				 * DV_CLEAN_FORCE is *not* specified then a
1368 				 * referenced directory is considered busy.
1369 				 */
1370 				if (dv->dv_busy || !(flags & DV_CLEAN_FORCE)) {
1371 					mutex_exit(&vp->v_lock);
1372 					rw_exit(&dv->dv_contents);
1373 					goto set_busy;
1374 				}
1375 
1376 				/*
1377 				 * Mark referenced directory stale so that DR
1378 				 * will succeed even if a shell has
1379 				 * /devices/xxx as current directory (causing
1380 				 * VN_HOLD reference to an empty directory).
1381 				 */
1382 				ASSERT(!DV_STALE(dv));
1383 				ndi_rele_devi(dv->dv_devi);
1384 				dv->dv_devi = NULL;	/* mark DV_STALE */
1385 			}
1386 		} else {
1387 			ASSERT((vp->v_type == VCHR) || (vp->v_type == VBLK));
1388 			ASSERT(dv->dv_nlink == 1);	/* no hard links */
1389 			mutex_enter(&vp->v_lock);
1390 			if (vp->v_count > 0) {
1391 				mutex_exit(&vp->v_lock);
1392 				goto set_busy;
1393 			}
1394 		}
1395 
1396 		/* unlink from directory */
1397 		dv_unlink(ddv, dv);
1398 
1399 		/* drop locks */
1400 		mutex_exit(&vp->v_lock);
1401 		if (vp->v_type == VDIR)
1402 			rw_exit(&dv->dv_contents);
1403 
1404 		/* destroy vnode if ref count is zero */
1405 		if (vp->v_count == 0)
1406 			dv_destroy(dv, flags);
1407 
1408 		continue;
1409 
1410 		/*
1411 		 * If devnm is not NULL we return immediately on busy,
1412 		 * otherwise we continue destroying unused dv_node's.
1413 		 */
1414 set_busy:	busy++;
1415 		if (devnm)
1416 			break;
1417 	}
1418 
1419 	/*
1420 	 * This code may be invoked to inform devfs that a new node has
1421 	 * been created in the kernel device tree. So we always set
1422 	 * the DV_BUILD flag to allow the next dv_filldir() to pick
1423 	 * the new devinfo nodes.
1424 	 */
1425 	ddv->dv_flags |= DV_BUILD;
1426 
1427 	if (!(flags & DV_CLEANDIR_LCK))
1428 		rw_exit(&ddv->dv_contents);
1429 
1430 	return (busy ? EBUSY : 0);
1431 }
1432 
1433 /*
1434  * Walk through the devfs hierarchy, correcting the permissions of
1435  * devices with default permissions that do not match those specified
1436  * by minor perm.  This can only be done for all drivers for now.
1437  */
1438 static int
1439 dv_reset_perm_dir(struct dv_node *ddv, uint_t flags)
1440 {
1441 	struct dv_node	*dv;
1442 	struct vnode	*vp;
1443 	int		retval = 0;
1444 	struct vattr	*attrp;
1445 	mperm_t		mp;
1446 	char		*nm;
1447 	uid_t		old_uid;
1448 	gid_t		old_gid;
1449 	mode_t		old_mode;
1450 
1451 	rw_enter(&ddv->dv_contents, RW_WRITER);
1452 	for (dv = DV_FIRST_ENTRY(ddv); dv; dv = DV_NEXT_ENTRY(ddv, dv)) {
1453 		int error = 0;
1454 		nm = dv->dv_name;
1455 
1456 		rw_enter(&dv->dv_contents, RW_READER);
1457 		vp = DVTOV(dv);
1458 		if (vp->v_type == VDIR) {
1459 			rw_exit(&dv->dv_contents);
1460 			if (dv_reset_perm_dir(dv, flags) != 0) {
1461 				error = EBUSY;
1462 			}
1463 		} else {
1464 			ASSERT(vp->v_type == VCHR || vp->v_type == VBLK);
1465 
1466 			/*
1467 			 * Check for permissions from minor_perm
1468 			 * If there are none, we're done
1469 			 */
1470 			rw_exit(&dv->dv_contents);
1471 			if (dev_minorperm(dv->dv_devi, nm, &mp) != 0)
1472 				continue;
1473 
1474 			rw_enter(&dv->dv_contents, RW_READER);
1475 
1476 			/*
1477 			 * Allow a node's permissions to be altered
1478 			 * permanently from the defaults by chmod,
1479 			 * using the shadow node as backing store.
1480 			 * Otherwise, update node to minor_perm permissions.
1481 			 */
1482 			if (dv->dv_attrvp == NULLVP) {
1483 				/*
1484 				 * No attribute vp, try to find one.
1485 				 */
1486 				dv_shadow_node(DVTOV(ddv), nm, vp,
1487 				    NULL, NULLVP, kcred, 0);
1488 			}
1489 			if (dv->dv_attrvp != NULLVP || dv->dv_attr == NULL) {
1490 				rw_exit(&dv->dv_contents);
1491 				continue;
1492 			}
1493 
1494 			attrp = dv->dv_attr;
1495 
1496 			if (VATTRP_MP_CMP(attrp, mp) == 0) {
1497 				dcmn_err5(("%s: no perm change: "
1498 				    "%d %d 0%o\n", nm, attrp->va_uid,
1499 				    attrp->va_gid, attrp->va_mode));
1500 				rw_exit(&dv->dv_contents);
1501 				continue;
1502 			}
1503 
1504 			old_uid = attrp->va_uid;
1505 			old_gid = attrp->va_gid;
1506 			old_mode = attrp->va_mode;
1507 
1508 			VATTRP_MP_MERGE(attrp, mp);
1509 			mutex_enter(&vp->v_lock);
1510 			if (vp->v_count > 0) {
1511 				error = EBUSY;
1512 			}
1513 			mutex_exit(&vp->v_lock);
1514 
1515 			dcmn_err5(("%s: perm %d/%d/0%o -> %d/%d/0%o (%d)\n",
1516 			    nm, old_uid, old_gid, old_mode, attrp->va_uid,
1517 			    attrp->va_gid, attrp->va_mode, error));
1518 
1519 			rw_exit(&dv->dv_contents);
1520 		}
1521 
1522 		if (error != 0) {
1523 			retval = error;
1524 		}
1525 	}
1526 
1527 	ddv->dv_flags |= DV_BUILD;
1528 
1529 	rw_exit(&ddv->dv_contents);
1530 
1531 	return (retval);
1532 }
1533 
1534 int
1535 devfs_reset_perm(uint_t flags)
1536 {
1537 	struct dv_node	*dvp;
1538 	int		rval;
1539 
1540 	if ((dvp = devfs_dip_to_dvnode(ddi_root_node())) == NULL)
1541 		return (0);
1542 
1543 	VN_HOLD(DVTOV(dvp));
1544 	rval = dv_reset_perm_dir(dvp, flags);
1545 	VN_RELE(DVTOV(dvp));
1546 	return (rval);
1547 }
1548 
1549 /*
1550  * Clean up dangling devfs shadow nodes for removed
1551  * drivers so that, in the event the driver is re-added
1552  * to the system, newly created nodes won't incorrectly
1553  * pick up these stale shadow node permissions.
1554  *
1555  * This is accomplished by walking down the pathname
1556  * to the directory, starting at the root's attribute
1557  * node, then removing all minors matching the specified
1558  * node name.  Care must be taken to remove all entries
1559  * in a directory before the directory itself, so that
1560  * the clean-up associated with rem_drv'ing a nexus driver
1561  * does not inadvertently result in an inconsistent
1562  * filesystem underlying devfs.
1563  */
1564 
1565 static int
1566 devfs_remdrv_rmdir(vnode_t *dirvp, const char *dir, vnode_t *rvp)
1567 {
1568 	int		error;
1569 	vnode_t		*vp;
1570 	int		eof;
1571 	struct iovec	iov;
1572 	struct uio	uio;
1573 	struct dirent64	*dp;
1574 	dirent64_t	*dbuf;
1575 	size_t		dlen;
1576 	size_t		dbuflen;
1577 	int		ndirents = 64;
1578 	char		*nm;
1579 
1580 	VN_HOLD(dirvp);
1581 
1582 	dlen = ndirents * (sizeof (*dbuf));
1583 	dbuf = kmem_alloc(dlen, KM_SLEEP);
1584 
1585 	uio.uio_iov = &iov;
1586 	uio.uio_iovcnt = 1;
1587 	uio.uio_segflg = UIO_SYSSPACE;
1588 	uio.uio_fmode = 0;
1589 	uio.uio_extflg = UIO_COPY_CACHED;
1590 	uio.uio_loffset = 0;
1591 	uio.uio_llimit = MAXOFFSET_T;
1592 
1593 	eof = 0;
1594 	error = 0;
1595 	while (!error && !eof) {
1596 		uio.uio_resid = dlen;
1597 		iov.iov_base = (char *)dbuf;
1598 		iov.iov_len = dlen;
1599 
1600 		(void) VOP_RWLOCK(dirvp, V_WRITELOCK_FALSE, NULL);
1601 		error = VOP_READDIR(dirvp, &uio, kcred, &eof, NULL, 0);
1602 		VOP_RWUNLOCK(dirvp, V_WRITELOCK_FALSE, NULL);
1603 
1604 		dbuflen = dlen - uio.uio_resid;
1605 
1606 		if (error || dbuflen == 0)
1607 			break;
1608 
1609 		for (dp = dbuf; ((intptr_t)dp < (intptr_t)dbuf + dbuflen);
1610 		    dp = (dirent64_t *)((intptr_t)dp + dp->d_reclen)) {
1611 
1612 			nm = dp->d_name;
1613 
1614 			if (strcmp(nm, ".") == 0 || strcmp(nm, "..") == 0)
1615 				continue;
1616 
1617 			error = VOP_LOOKUP(dirvp, nm,
1618 			    &vp, NULL, 0, NULL, kcred, NULL, NULL, NULL);
1619 
1620 			dsysdebug(error,
1621 			    ("rem_drv %s/%s lookup (%d)\n",
1622 			    dir, nm, error));
1623 
1624 			if (error)
1625 				continue;
1626 
1627 			ASSERT(vp->v_type == VDIR ||
1628 			    vp->v_type == VCHR || vp->v_type == VBLK);
1629 
1630 			if (vp->v_type == VDIR) {
1631 				error = devfs_remdrv_rmdir(vp, nm, rvp);
1632 				if (error == 0) {
1633 					error = VOP_RMDIR(dirvp,
1634 					    (char *)nm, rvp, kcred, NULL, 0);
1635 					dsysdebug(error,
1636 					    ("rem_drv %s/%s rmdir (%d)\n",
1637 					    dir, nm, error));
1638 				}
1639 			} else {
1640 				error = VOP_REMOVE(dirvp, (char *)nm, kcred,
1641 				    NULL, 0);
1642 				dsysdebug(error,
1643 				    ("rem_drv %s/%s remove (%d)\n",
1644 				    dir, nm, error));
1645 			}
1646 
1647 			VN_RELE(vp);
1648 			if (error) {
1649 				goto exit;
1650 			}
1651 		}
1652 	}
1653 
1654 exit:
1655 	VN_RELE(dirvp);
1656 	kmem_free(dbuf, dlen);
1657 
1658 	return (error);
1659 }
1660 
1661 int
1662 devfs_remdrv_cleanup(const char *dir, const char *nodename)
1663 {
1664 	int		error;
1665 	vnode_t		*vp;
1666 	vnode_t		*dirvp;
1667 	int		eof;
1668 	struct iovec	iov;
1669 	struct uio	uio;
1670 	struct dirent64	*dp;
1671 	dirent64_t	*dbuf;
1672 	size_t		dlen;
1673 	size_t		dbuflen;
1674 	int		ndirents = 64;
1675 	int		nodenamelen = strlen(nodename);
1676 	char		*nm;
1677 	struct pathname	pn;
1678 	vnode_t		*rvp;	/* root node of the underlying attribute fs */
1679 
1680 	dcmn_err5(("devfs_remdrv_cleanup: %s %s\n", dir, nodename));
1681 
1682 	if (error = pn_get((char *)dir, UIO_SYSSPACE, &pn))
1683 		return (0);
1684 
1685 	rvp = dvroot->dv_attrvp;
1686 	ASSERT(rvp != NULL);
1687 	VN_HOLD(rvp);
1688 
1689 	pn_skipslash(&pn);
1690 	dirvp = rvp;
1691 	VN_HOLD(dirvp);
1692 
1693 	nm = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1694 
1695 	while (pn_pathleft(&pn)) {
1696 		ASSERT(dirvp->v_type == VDIR);
1697 		(void) pn_getcomponent(&pn, nm);
1698 		ASSERT((strcmp(nm, ".") != 0) && (strcmp(nm, "..") != 0));
1699 		error = VOP_LOOKUP(dirvp, nm, &vp, NULL, 0, rvp, kcred,
1700 		    NULL, NULL, NULL);
1701 		if (error) {
1702 			dcmn_err5(("remdrv_cleanup %s lookup error %d\n",
1703 			    nm, error));
1704 			VN_RELE(dirvp);
1705 			if (dirvp != rvp)
1706 				VN_RELE(rvp);
1707 			pn_free(&pn);
1708 			kmem_free(nm, MAXNAMELEN);
1709 			return (0);
1710 		}
1711 		VN_RELE(dirvp);
1712 		dirvp = vp;
1713 		pn_skipslash(&pn);
1714 	}
1715 
1716 	ASSERT(dirvp->v_type == VDIR);
1717 	if (dirvp != rvp)
1718 		VN_RELE(rvp);
1719 	pn_free(&pn);
1720 	kmem_free(nm, MAXNAMELEN);
1721 
1722 	dlen = ndirents * (sizeof (*dbuf));
1723 	dbuf = kmem_alloc(dlen, KM_SLEEP);
1724 
1725 	uio.uio_iov = &iov;
1726 	uio.uio_iovcnt = 1;
1727 	uio.uio_segflg = UIO_SYSSPACE;
1728 	uio.uio_fmode = 0;
1729 	uio.uio_extflg = UIO_COPY_CACHED;
1730 	uio.uio_loffset = 0;
1731 	uio.uio_llimit = MAXOFFSET_T;
1732 
1733 	eof = 0;
1734 	error = 0;
1735 	while (!error && !eof) {
1736 		uio.uio_resid = dlen;
1737 		iov.iov_base = (char *)dbuf;
1738 		iov.iov_len = dlen;
1739 
1740 		(void) VOP_RWLOCK(dirvp, V_WRITELOCK_FALSE, NULL);
1741 		error = VOP_READDIR(dirvp, &uio, kcred, &eof, NULL, 0);
1742 		VOP_RWUNLOCK(dirvp, V_WRITELOCK_FALSE, NULL);
1743 
1744 		dbuflen = dlen - uio.uio_resid;
1745 
1746 		if (error || dbuflen == 0)
1747 			break;
1748 
1749 		for (dp = dbuf; ((intptr_t)dp < (intptr_t)dbuf + dbuflen);
1750 		    dp = (dirent64_t *)((intptr_t)dp + dp->d_reclen)) {
1751 
1752 			nm = dp->d_name;
1753 
1754 			if (strcmp(nm, ".") == 0 || strcmp(nm, "..") == 0)
1755 				continue;
1756 
1757 			if (strncmp(nm, nodename, nodenamelen) != 0)
1758 				continue;
1759 
1760 			error = VOP_LOOKUP(dirvp, nm, &vp,
1761 			    NULL, 0, NULL, kcred, NULL, NULL, NULL);
1762 
1763 			dsysdebug(error,
1764 			    ("rem_drv %s/%s lookup (%d)\n",
1765 			    dir, nm, error));
1766 
1767 			if (error)
1768 				continue;
1769 
1770 			ASSERT(vp->v_type == VDIR ||
1771 			    vp->v_type == VCHR || vp->v_type == VBLK);
1772 
1773 			if (vp->v_type == VDIR) {
1774 				error = devfs_remdrv_rmdir(vp, nm, rvp);
1775 				if (error == 0) {
1776 					error = VOP_RMDIR(dirvp, (char *)nm,
1777 					    rvp, kcred, NULL, 0);
1778 					dsysdebug(error,
1779 					    ("rem_drv %s/%s rmdir (%d)\n",
1780 					    dir, nm, error));
1781 				}
1782 			} else {
1783 				error = VOP_REMOVE(dirvp, (char *)nm, kcred,
1784 				    NULL, 0);
1785 				dsysdebug(error,
1786 				    ("rem_drv %s/%s remove (%d)\n",
1787 				    dir, nm, error));
1788 			}
1789 
1790 			VN_RELE(vp);
1791 			if (error)
1792 				goto exit;
1793 		}
1794 	}
1795 
1796 exit:
1797 	VN_RELE(dirvp);
1798 
1799 	kmem_free(dbuf, dlen);
1800 
1801 	return (0);
1802 }
1803 
1804 struct dv_list {
1805 	struct dv_node	*dv;
1806 	struct dv_list	*next;
1807 };
1808 
1809 void
1810 dv_walk(
1811 	struct dv_node	*ddv,
1812 	char		*devnm,
1813 	void		(*callback)(struct dv_node *, void *),
1814 	void		*arg)
1815 {
1816 	struct vnode	*dvp;
1817 	struct dv_node	*dv;
1818 	struct dv_list	*head, *tail, *next;
1819 	int		len;
1820 
1821 	dcmn_err3(("dv_walk: ddv = %s, devnm = %s\n",
1822 	    ddv->dv_name, devnm ? devnm : "<null>"));
1823 
1824 	dvp = DVTOV(ddv);
1825 
1826 	ASSERT(dvp->v_type == VDIR);
1827 
1828 	head = tail = next = NULL;
1829 
1830 	rw_enter(&ddv->dv_contents, RW_READER);
1831 	mutex_enter(&dvp->v_lock);
1832 	for (dv = DV_FIRST_ENTRY(ddv); dv; dv = DV_NEXT_ENTRY(ddv, dv)) {
1833 		/*
1834 		 * If devnm is not NULL and is not the empty string,
1835 		 * select only dv_nodes with matching non-minor name
1836 		 */
1837 		if (devnm && (len = strlen(devnm)) &&
1838 		    (strncmp(devnm, dv->dv_name, len) ||
1839 		    (dv->dv_name[len] != ':' && dv->dv_name[len] != '\0')))
1840 			continue;
1841 
1842 		callback(dv, arg);
1843 
1844 		if (DVTOV(dv)->v_type != VDIR)
1845 			continue;
1846 
1847 		next = kmem_zalloc(sizeof (*next), KM_SLEEP);
1848 		next->dv = dv;
1849 
1850 		if (tail)
1851 			tail->next = next;
1852 		else
1853 			head = next;
1854 
1855 		tail = next;
1856 	}
1857 
1858 	while (head) {
1859 		dv_walk(head->dv, NULL, callback, arg);
1860 		next = head->next;
1861 		kmem_free(head, sizeof (*head));
1862 		head = next;
1863 	}
1864 	rw_exit(&ddv->dv_contents);
1865 	mutex_exit(&dvp->v_lock);
1866 }
1867