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