xref: /titanic_50/usr/src/uts/common/fs/devfs/devfs_subr.c (revision fb60e41df3cdb2164356bc09f05d3b842c1a8365)
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 /*
139  * dv_node cache constructor, destructor, can cache creation
140  */
141 /*ARGSUSED1*/
142 static int
143 i_dv_node_ctor(void *buf, void *cfarg, int flag)
144 {
145 	struct dv_node	*dv = (struct dv_node *)buf;
146 	struct vnode	*vp;
147 
148 	bzero(buf, sizeof (struct dv_node));
149 
150 	/* initialize persistent parts of dv_node */
151 	rw_init(&dv->dv_contents, NULL, RW_DEFAULT, NULL);
152 
153 	/* allocate vnode and initialize link back to dv_node */
154 	dv->dv_vnode = vn_alloc(KM_SLEEP);
155 	vp = DVTOV(dv);
156 	vp->v_data = (caddr_t)dv;
157 	return (0);
158 }
159 
160 /* dev_info node destructor for kmem cache */
161 /*ARGSUSED1*/
162 static void
163 i_dv_node_dtor(void *buf, void *arg)
164 {
165 	struct dv_node	*dv = (struct dv_node *)buf;
166 	struct vnode	*vp = DVTOV(dv);
167 
168 	rw_destroy(&dv->dv_contents);
169 	vn_invalid(vp);
170 	vn_free(vp);
171 }
172 
173 
174 /* initialize dev_info node cache */
175 void
176 dv_node_cache_init()
177 {
178 	ASSERT(dv_node_cache == NULL);
179 	dv_node_cache = kmem_cache_create("dv_node_cache",
180 	    sizeof (struct dv_node), 0, i_dv_node_ctor, i_dv_node_dtor,
181 	    NULL, NULL, NULL, 0);
182 
183 	tsd_create(&devfs_clean_key, NULL);
184 }
185 
186 /* initialize dev_info node cache */
187 void
188 dv_node_cache_fini()
189 {
190 	ASSERT(dv_node_cache != NULL);
191 	kmem_cache_destroy(dv_node_cache);
192 	dv_node_cache = NULL;
193 
194 	tsd_destroy(&devfs_clean_key);
195 }
196 
197 /*
198  * dv_mkino - Generate a unique inode number for devfs nodes.
199  *
200  * Although ino_t is 64 bits, the inode number is truncated to 32 bits for 32
201  * bit non-LARGEFILE applications. This means that there is a requirement to
202  * maintain the inode number as a 32 bit value or applications will have
203  * stat(2) calls fail with EOVERFLOW.  We form a 32 bit inode number from the
204  * dev_t. but if the minor number is larger than L_MAXMIN32 we fold extra minor
205  *
206  * To generate inode numbers for directories, we assume that we will never use
207  * more than half the major space - this allows for ~8190 drivers. We use this
208  * upper major number space to allocate inode numbers for directories by
209  * encoding the major and instance into this space.
210  *
211  * We also skew the result so that inode 2 is reserved for the root of the file
212  * system.
213  *
214  * As part of the future support for 64-bit dev_t APIs, the upper minor bits
215  * should be folded into the high inode bits by adding the following code
216  * after "ino |= 1":
217  *
218  * #if (L_BITSMINOR32 != L_BITSMINOR)
219  *		|* fold overflow minor bits into high bits of inode number *|
220  *		ino |= ((ino_t)(minor >> L_BITSMINOR32)) << L_BITSMINOR;
221  * #endif |* (L_BITSMINOR32 != L_BITSMINOR) *|
222  *
223  * This way only applications that use devices that overflow their minor
224  * space will have an application level impact.
225  */
226 static ino_t
227 dv_mkino(dev_info_t *devi, vtype_t typ, dev_t dev)
228 {
229 	major_t		major;
230 	minor_t		minor;
231 	ino_t		ino;
232 	static int	warn;
233 
234 	if (typ == VDIR) {
235 		major = ((L_MAXMAJ32 + 1) >> 1) + DEVI(devi)->devi_major;
236 		minor = ddi_get_instance(devi);
237 
238 		/* makedevice32 in high half of major number space */
239 		ino = (ino_t)((major << L_BITSMINOR32) | (minor & L_MAXMIN32));
240 
241 		major = DEVI(devi)->devi_major;
242 	} else {
243 		major = getmajor(dev);
244 		minor = getminor(dev);
245 
246 		/* makedevice32 */
247 		ino = (ino_t)((major << L_BITSMINOR32) | (minor & L_MAXMIN32));
248 
249 		/* make ino for VCHR different than VBLK */
250 		ino <<= 1;
251 		if (typ == VCHR)
252 			ino |= 1;
253 	}
254 
255 	ino += DV_ROOTINO + 1;		/* skew */
256 
257 	/*
258 	 * diagnose things a little early because adding the skew to a large
259 	 * minor number could roll over the major.
260 	 */
261 	if ((major >= (L_MAXMAJ32 >> 1)) && (warn == 0)) {
262 		warn = 1;
263 		cmn_err(CE_WARN, "%s: inode numbers are not unique", dvnm);
264 	}
265 
266 	return (ino);
267 }
268 
269 /*
270  * Compare two nodes lexographically to balance avl tree
271  */
272 static int
273 dv_compare_nodes(const struct dv_node *dv1, const struct dv_node *dv2)
274 {
275 	int rv;
276 	if ((rv = strcmp(dv1->dv_name, dv2->dv_name)) == 0)
277 		return (0);
278 	return ((rv < 0) ? -1 : 1);
279 }
280 
281 /*
282  * dv_mkroot
283  *
284  * Build the first VDIR dv_node.
285  */
286 struct dv_node *
287 dv_mkroot(struct vfs *vfsp, dev_t devfsdev)
288 {
289 	struct dv_node *dv;
290 	struct vnode *vp;
291 
292 	ASSERT(ddi_root_node() != NULL);
293 	ASSERT(dv_node_cache != NULL);
294 
295 	dcmn_err3(("dv_mkroot\n"));
296 	dv = kmem_cache_alloc(dv_node_cache, KM_SLEEP);
297 	vp = DVTOV(dv);
298 	vn_reinit(vp);
299 	vp->v_flag = VROOT;
300 	vp->v_vfsp = vfsp;
301 	vp->v_type = VDIR;
302 	vp->v_rdev = devfsdev;
303 	vn_setops(vp, dv_vnodeops);
304 	vn_exists(vp);
305 
306 	dvroot = dv;
307 
308 	dv->dv_name = NULL;		/* not needed */
309 	dv->dv_namelen = 0;
310 
311 	dv->dv_devi = ddi_root_node();
312 
313 	dv->dv_ino = DV_ROOTINO;
314 	dv->dv_nlink = 2;		/* name + . (no dv_insert) */
315 	dv->dv_dotdot = dv;		/* .. == self */
316 	dv->dv_attrvp = NULLVP;
317 	dv->dv_attr = NULL;
318 	dv->dv_flags = DV_BUILD;
319 	dv->dv_priv = NULL;
320 	dv->dv_busy = 0;
321 	dv->dv_dflt_mode = 0;
322 
323 	avl_create(&dv->dv_entries,
324 	    (int (*)(const void *, const void *))dv_compare_nodes,
325 	    sizeof (struct dv_node), offsetof(struct dv_node, dv_avllink));
326 
327 	return (dv);
328 }
329 
330 /*
331  * dv_mkdir
332  *
333  * Given an probed or attached nexus node, create a VDIR dv_node.
334  * No dv_attrvp is created at this point.
335  */
336 struct dv_node *
337 dv_mkdir(struct dv_node *ddv, dev_info_t *devi, char *nm)
338 {
339 	struct dv_node *dv;
340 	struct vnode *vp;
341 	size_t nmlen;
342 
343 	ASSERT((devi));
344 	dcmn_err4(("dv_mkdir: %s\n", nm));
345 
346 	dv = kmem_cache_alloc(dv_node_cache, KM_SLEEP);
347 	nmlen = strlen(nm) + 1;
348 	dv->dv_name = kmem_alloc(nmlen, KM_SLEEP);
349 	bcopy(nm, dv->dv_name, nmlen);
350 	dv->dv_namelen = nmlen - 1;	/* '\0' not included */
351 
352 	vp = DVTOV(dv);
353 	vn_reinit(vp);
354 	vp->v_flag = 0;
355 	vp->v_vfsp = DVTOV(ddv)->v_vfsp;
356 	vp->v_type = VDIR;
357 	vp->v_rdev = DVTOV(ddv)->v_rdev;
358 	vn_setops(vp, vn_getops(DVTOV(ddv)));
359 	vn_exists(vp);
360 
361 	dv->dv_devi = devi;
362 	ndi_hold_devi(devi);
363 
364 	dv->dv_ino = dv_mkino(devi, VDIR, NODEV);
365 	dv->dv_nlink = 0;		/* updated on insert */
366 	dv->dv_dotdot = ddv;
367 	dv->dv_attrvp = NULLVP;
368 	dv->dv_attr = NULL;
369 	dv->dv_flags = DV_BUILD;
370 	dv->dv_priv = NULL;
371 	dv->dv_busy = 0;
372 	dv->dv_dflt_mode = 0;
373 
374 	avl_create(&dv->dv_entries,
375 	    (int (*)(const void *, const void *))dv_compare_nodes,
376 	    sizeof (struct dv_node), offsetof(struct dv_node, dv_avllink));
377 
378 	return (dv);
379 }
380 
381 /*
382  * dv_mknod
383  *
384  * Given a minor node, create a VCHR or VBLK dv_node.
385  * No dv_attrvp is created at this point.
386  */
387 static struct dv_node *
388 dv_mknod(struct dv_node *ddv, dev_info_t *devi, char *nm,
389 	struct ddi_minor_data *dmd)
390 {
391 	struct dv_node *dv;
392 	struct vnode *vp;
393 	size_t nmlen;
394 
395 	dcmn_err4(("dv_mknod: %s\n", nm));
396 
397 	dv = kmem_cache_alloc(dv_node_cache, KM_SLEEP);
398 	nmlen = strlen(nm) + 1;
399 	dv->dv_name = kmem_alloc(nmlen, KM_SLEEP);
400 	bcopy(nm, dv->dv_name, nmlen);
401 	dv->dv_namelen = nmlen - 1;	/* no '\0' */
402 
403 	vp = DVTOV(dv);
404 	vn_reinit(vp);
405 	vp->v_flag = 0;
406 	vp->v_vfsp = DVTOV(ddv)->v_vfsp;
407 	vp->v_type = dmd->ddm_spec_type == S_IFCHR ? VCHR : VBLK;
408 	vp->v_rdev = dmd->ddm_dev;
409 	vn_setops(vp, vn_getops(DVTOV(ddv)));
410 	vn_exists(vp);
411 
412 	ASSERT(MUTEX_HELD(&DEVI(devi)->devi_lock));
413 	dv->dv_devi = devi;
414 	DEVI(devi)->devi_ref++;
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 	ASSERT(MUTEX_HELD(&DEVI(devi)->devi_lock));
848 
849 	dcmn_err3(("dv_find_leafnode: %s\n", minor_nm));
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 == (major_t)-1)
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 rv = 0, was_busy = 0, nmlen, write_held = 0;
934 	struct vnode *vp;
935 	struct dv_node *dv, *dup;
936 	dev_info_t *pdevi, *devi = NULL;
937 	char *mnm;
938 	struct ddi_minor_data *dmd;
939 
940 	dcmn_err3(("dv_find %s\n", nm));
941 
942 	rw_enter(&ddv->dv_contents, RW_READER);
943 start:
944 	if (DV_STALE(ddv)) {
945 		rw_exit(&ddv->dv_contents);
946 		return (ESTALE);
947 	}
948 
949 	/*
950 	 * Empty name or ., return node itself.
951 	 */
952 	nmlen = strlen(nm);
953 	if ((nmlen == 0) || ((nmlen == 1) && (nm[0] == '.'))) {
954 		*vpp = DVTOV(ddv);
955 		rw_exit(&ddv->dv_contents);
956 		VN_HOLD(*vpp);
957 		return (0);
958 	}
959 
960 	/*
961 	 * .., return the parent directory
962 	 */
963 	if ((nmlen == 2) && (strcmp(nm, "..") == 0)) {
964 		*vpp = DVTOV(ddv->dv_dotdot);
965 		rw_exit(&ddv->dv_contents);
966 		VN_HOLD(*vpp);
967 		return (0);
968 	}
969 
970 	/*
971 	 * Fail anything without a valid device name component
972 	 */
973 	if (nm[0] == '@' || nm[0] == ':') {
974 		dcmn_err3(("devfs: no driver '%s'\n", nm));
975 		rw_exit(&ddv->dv_contents);
976 		return (ENOENT);
977 	}
978 
979 	/*
980 	 * So, now we have to deal with the trickier stuff.
981 	 *
982 	 * (a) search the existing list of dv_nodes on this directory
983 	 */
984 	if ((dv = dv_findbyname(ddv, nm)) != NULL) {
985 founddv:
986 		ASSERT(RW_LOCK_HELD(&ddv->dv_contents));
987 
988 		if (!rw_tryenter(&dv->dv_contents, RW_READER)) {
989 			if (tsd_get(devfs_clean_key)) {
990 				VN_RELE(DVTOV(dv));
991 				rw_exit(&ddv->dv_contents);
992 				return (EBUSY);
993 			}
994 			rw_enter(&dv->dv_contents, RW_READER);
995 		}
996 
997 		vp = DVTOV(dv);
998 		if ((dv->dv_attrvp != NULLVP) ||
999 		    (vp->v_type != VDIR && dv->dv_attr != NULL)) {
1000 			/*
1001 			 * Common case - we already have attributes
1002 			 */
1003 			rw_exit(&dv->dv_contents);
1004 			rw_exit(&ddv->dv_contents);
1005 			goto found;
1006 		}
1007 
1008 		/*
1009 		 * No attribute vp, try and build one.
1010 		 *
1011 		 * dv_shadow_node() can briefly drop &dv->dv_contents lock
1012 		 * if it is unable to upgrade it to a write lock. If the
1013 		 * current thread has come in through the bottom-up device
1014 		 * configuration devfs_clean() path, we may deadlock against
1015 		 * a thread performing top-down device configuration if it
1016 		 * grabs the contents lock. To avoid this, when we are on the
1017 		 * devfs_clean() path we attempt to upgrade the dv_contents
1018 		 * lock before we call dv_shadow_node().
1019 		 */
1020 		if (tsd_get(devfs_clean_key)) {
1021 			if (!rw_tryupgrade(&dv->dv_contents)) {
1022 				VN_RELE(DVTOV(dv));
1023 				rw_exit(&dv->dv_contents);
1024 				rw_exit(&ddv->dv_contents);
1025 				return (EBUSY);
1026 			}
1027 
1028 			write_held = DV_SHADOW_WRITE_HELD;
1029 		}
1030 
1031 		dv_shadow_node(DVTOV(ddv), nm, vp, pnp, rdir, cred,
1032 		    write_held);
1033 
1034 		rw_exit(&dv->dv_contents);
1035 		rw_exit(&ddv->dv_contents);
1036 		goto found;
1037 	}
1038 
1039 	/*
1040 	 * (b) Search the child devinfo nodes of our parent directory,
1041 	 * looking for the named node.  If we find it, build a new
1042 	 * node, then grab the writers lock, search the directory
1043 	 * if it's still not there, then insert it.
1044 	 *
1045 	 * We drop the devfs locks before accessing the device tree.
1046 	 * Take care to mark the node BUSY so that a forced devfs_clean
1047 	 * doesn't mark the directory node stale.
1048 	 *
1049 	 * Also, check if we are called as part of devfs_clean or
1050 	 * reset_perm. If so, simply return not found because there
1051 	 * is nothing to clean.
1052 	 */
1053 	if (tsd_get(devfs_clean_key)) {
1054 		rw_exit(&ddv->dv_contents);
1055 		return (ENOENT);
1056 	}
1057 
1058 	/*
1059 	 * We could be either READ or WRITE locked at
1060 	 * this point. Upgrade if we are read locked.
1061 	 */
1062 	ASSERT(RW_LOCK_HELD(&ddv->dv_contents));
1063 	if (rw_read_locked(&ddv->dv_contents) &&
1064 	    !rw_tryupgrade(&ddv->dv_contents)) {
1065 		rw_exit(&ddv->dv_contents);
1066 		rw_enter(&ddv->dv_contents, RW_WRITER);
1067 		/*
1068 		 * Things may have changed when we dropped
1069 		 * the contents lock, so start from top again
1070 		 */
1071 		goto start;
1072 	}
1073 	ddv->dv_busy++;		/* mark busy before dropping lock */
1074 	was_busy++;
1075 	rw_exit(&ddv->dv_contents);
1076 
1077 	pdevi = ddv->dv_devi;
1078 	ASSERT(pdevi != NULL);
1079 
1080 	mnm = strchr(nm, ':');
1081 	if (mnm)
1082 		*mnm = (char)0;
1083 
1084 	/*
1085 	 * Configure one nexus child, will call nexus's bus_ops
1086 	 * If successful, devi is held upon returning.
1087 	 * Note: devfs lookup should not be configuring grandchildren.
1088 	 */
1089 	ASSERT((ndi_flags & NDI_CONFIG) == 0);
1090 
1091 	rv = ndi_devi_config_one(pdevi, nm, &devi, ndi_flags | NDI_NO_EVENT);
1092 	if (mnm)
1093 		*mnm = ':';
1094 	if (rv != NDI_SUCCESS) {
1095 		rv = ENOENT;
1096 		goto notfound;
1097 	}
1098 
1099 	/*
1100 	 * Don't make vhci clients visible under phci, unless we
1101 	 * are in miniroot.
1102 	 */
1103 	if (isminiroot == 0 && ddi_get_parent(devi) != pdevi) {
1104 		ndi_rele_devi(devi);
1105 		rv = ENOENT;
1106 		goto notfound;
1107 	}
1108 
1109 	ASSERT(devi && i_ddi_devi_attached(devi));
1110 
1111 	/*
1112 	 * Invalidate cache to notice newly created minor nodes.
1113 	 */
1114 	rw_enter(&ddv->dv_contents, RW_WRITER);
1115 	ddv->dv_flags |= DV_BUILD;
1116 	rw_exit(&ddv->dv_contents);
1117 
1118 	/*
1119 	 * mkdir for nexus drivers and leaf nodes as well.  If we are racing
1120 	 * and create a duplicate, the duplicate will be destroyed below.
1121 	 */
1122 	if (mnm == NULL) {
1123 		dv = dv_mkdir(ddv, devi, nm);
1124 	} else {
1125 		/*
1126 		 * For clone minors, load the driver indicated by minor name.
1127 		 */
1128 		mutex_enter(&DEVI(devi)->devi_lock);
1129 		if (devi == clone_dip) {
1130 			dv = dv_clone_mknod(ddv, mnm + 1);
1131 		} else {
1132 			/*
1133 			 * Find minor node and make a dv_node
1134 			 */
1135 			dmd = kmem_zalloc(sizeof (*dmd), KM_SLEEP);
1136 			if (dv_find_leafnode(devi, mnm + 1, dmd) == 0) {
1137 				dv = dv_mknod(ddv, devi, nm, dmd);
1138 				if (dmd->ddm_node_priv)
1139 					dpfree(dmd->ddm_node_priv);
1140 			}
1141 			kmem_free(dmd, sizeof (*dmd));
1142 		}
1143 		mutex_exit(&DEVI(devi)->devi_lock);
1144 	}
1145 	/*
1146 	 * Release hold from ndi_devi_config_one()
1147 	 */
1148 	ndi_rele_devi(devi);
1149 
1150 	if (dv == NULL) {
1151 		rv = ENOENT;
1152 		goto notfound;
1153 	}
1154 
1155 	/*
1156 	 * We have released the dv_contents lock, need to check
1157 	 * if another thread already created a duplicate node
1158 	 */
1159 	rw_enter(&ddv->dv_contents, RW_WRITER);
1160 	if ((dup = dv_findbyname(ddv, nm)) == NULL) {
1161 		dv_insert(ddv, dv);
1162 	} else {
1163 		/*
1164 		 * Duplicate found, use the existing node
1165 		 */
1166 		VN_RELE(DVTOV(dv));
1167 		dv_destroy(dv, 0);
1168 		dv = dup;
1169 	}
1170 	goto founddv;
1171 	/*NOTREACHED*/
1172 
1173 found:
1174 	/*
1175 	 * Skip non-kernel lookups of internal nodes.
1176 	 * This use of kcred to distinguish between user and
1177 	 * internal kernel lookups is unfortunate.  The information
1178 	 * provided by the seg argument to lookupnameat should
1179 	 * evolve into a lookup flag for filesystems that need
1180 	 * this distinction.
1181 	 */
1182 	if ((dv->dv_flags & DV_INTERNAL) && (cred != kcred)) {
1183 		VN_RELE(vp);
1184 		rv = ENOENT;
1185 		goto notfound;
1186 	}
1187 
1188 	dcmn_err2(("dv_find: returning vp for nm %s\n", nm));
1189 	if (vp->v_type == VCHR || vp->v_type == VBLK) {
1190 		/*
1191 		 * If vnode is a device, return special vnode instead
1192 		 * (though it knows all about -us- via sp->s_realvp,
1193 		 * sp->s_devvp, and sp->s_dip)
1194 		 */
1195 		*vpp = specvp_devfs(vp, vp->v_rdev, vp->v_type, cred,
1196 		    dv->dv_devi);
1197 		VN_RELE(vp);
1198 		if (*vpp == NULLVP)
1199 			rv = ENOSYS;
1200 	} else
1201 		*vpp = vp;
1202 
1203 notfound:
1204 	rw_enter(&ddv->dv_contents, RW_WRITER);
1205 	if (was_busy)
1206 		ddv->dv_busy--;
1207 	rw_exit(&ddv->dv_contents);
1208 	return (rv);
1209 }
1210 
1211 /*
1212  * The given directory node is out-of-date; that is, it has been
1213  * marked as needing to be rebuilt, possibly because some new devinfo
1214  * node has come into existence, or possibly because this is the first
1215  * time we've been here.
1216  */
1217 void
1218 dv_filldir(struct dv_node *ddv)
1219 {
1220 	struct dv_node *dv;
1221 	dev_info_t *devi, *pdevi;
1222 	struct ddi_minor_data *dmd;
1223 	char devnm[MAXNAMELEN];
1224 	int circ;
1225 
1226 	ASSERT(DVTOV(ddv)->v_type == VDIR);
1227 	ASSERT(RW_WRITE_HELD(&ddv->dv_contents));
1228 	ASSERT(ddv->dv_flags & DV_BUILD);
1229 
1230 	dcmn_err3(("dv_filldir: %s\n", ddv->dv_name));
1231 	if (DV_STALE(ddv))
1232 		return;
1233 	pdevi = ddv->dv_devi;
1234 
1235 	if (ndi_devi_config(pdevi, NDI_NO_EVENT) != NDI_SUCCESS) {
1236 		dcmn_err3(("dv_filldir: config error %s\n",
1237 		    ddv->dv_name));
1238 	}
1239 
1240 	ndi_devi_enter(pdevi, &circ);
1241 	for (devi = ddi_get_child(pdevi); devi;
1242 	    devi = ddi_get_next_sibling(devi)) {
1243 		if (i_ddi_node_state(devi) < DS_PROBED)
1244 			continue;
1245 
1246 		dcmn_err3(("dv_filldir: node %s\n", ddi_node_name(devi)));
1247 
1248 		mutex_enter(&DEVI(devi)->devi_lock);
1249 		for (dmd = DEVI(devi)->devi_minor; dmd; dmd = dmd->next) {
1250 			char *addr;
1251 
1252 			/*
1253 			 * Skip alias nodes, internal nodes, and nodes
1254 			 * without a name.  We allow DDM_DEFAULT nodes
1255 			 * to appear in readdir.
1256 			 */
1257 			if ((dmd->type == DDM_ALIAS) ||
1258 			    (dmd->type == DDM_INTERNAL_PATH) ||
1259 			    (dmd->ddm_name == NULL))
1260 				continue;
1261 
1262 			addr = ddi_get_name_addr(devi);
1263 			if (addr && *addr)
1264 				(void) sprintf(devnm, "%s@%s:%s",
1265 				    ddi_node_name(devi), addr, dmd->ddm_name);
1266 			else
1267 				(void) sprintf(devnm, "%s:%s",
1268 				    ddi_node_name(devi), dmd->ddm_name);
1269 
1270 			if ((dv = dv_findbyname(ddv, devnm)) != NULL) {
1271 				/* dv_node already exists */
1272 				VN_RELE(DVTOV(dv));
1273 				continue;
1274 			}
1275 
1276 			dv = dv_mknod(ddv, devi, devnm, dmd);
1277 			dv_insert(ddv, dv);
1278 			VN_RELE(DVTOV(dv));
1279 		}
1280 		mutex_exit(&DEVI(devi)->devi_lock);
1281 
1282 		(void) ddi_deviname(devi, devnm);
1283 		if ((dv = dv_findbyname(ddv, devnm + 1)) == NULL) {
1284 			/* directory doesn't exist */
1285 			dv = dv_mkdir(ddv, devi, devnm + 1);
1286 			dv_insert(ddv, dv);
1287 		}
1288 		VN_RELE(DVTOV(dv));
1289 	}
1290 	ndi_devi_exit(pdevi, circ);
1291 
1292 	ddv->dv_flags &= ~DV_BUILD;
1293 }
1294 
1295 /*
1296  * Given a directory node, clean out all the nodes beneath.
1297  *
1298  * VDIR:	Reinvoke to clean them, then delete the directory.
1299  * VCHR, VBLK:	Just blow them away.
1300  *
1301  * Mark the directories touched as in need of a rebuild, in case
1302  * we fall over part way through. When DV_CLEAN_FORCE is specified,
1303  * we mark referenced empty directories as stale to facilitate DR.
1304  */
1305 int
1306 dv_cleandir(struct dv_node *ddv, char *devnm, uint_t flags)
1307 {
1308 	struct dv_node *dv;
1309 	struct dv_node *next;
1310 	struct vnode *vp;
1311 	int busy = 0;
1312 
1313 	/*
1314 	 * We should always be holding the tsd_clean_key here: dv_cleandir()
1315 	 * will be called as a result of a devfs_clean request and the
1316 	 * tsd_clean_key will be set in either in devfs_clean() itself or in
1317 	 * devfs_clean_vhci().
1318 	 *
1319 	 * Since we are on the devfs_clean path, we return EBUSY if we cannot
1320 	 * get the contents lock: if we blocked here we might deadlock against
1321 	 * a thread performing top-down device configuration.
1322 	 */
1323 	ASSERT(tsd_get(devfs_clean_key));
1324 
1325 	dcmn_err3(("dv_cleandir: %s\n", ddv->dv_name));
1326 
1327 	if (!(flags & DV_CLEANDIR_LCK) &&
1328 	    !rw_tryenter(&ddv->dv_contents, RW_WRITER))
1329 		return (EBUSY);
1330 
1331 	for (dv = DV_FIRST_ENTRY(ddv); dv; dv = next) {
1332 		next = DV_NEXT_ENTRY(ddv, dv);
1333 
1334 		/*
1335 		 * If devnm is specified, the non-minor portion of the
1336 		 * name must match devnm.
1337 		 */
1338 		if (devnm &&
1339 		    (strncmp(devnm, dv->dv_name, strlen(devnm)) ||
1340 		    (dv->dv_name[strlen(devnm)] != ':' &&
1341 		    dv->dv_name[strlen(devnm)] != '\0')))
1342 			continue;
1343 
1344 		/* check type of what we are cleaning */
1345 		vp = DVTOV(dv);
1346 		if (vp->v_type == VDIR) {
1347 			/* recurse on directories */
1348 			rw_enter(&dv->dv_contents, RW_WRITER);
1349 			if (dv_cleandir(dv, NULL,
1350 			    flags | DV_CLEANDIR_LCK) == EBUSY) {
1351 				rw_exit(&dv->dv_contents);
1352 				goto set_busy;
1353 			}
1354 
1355 			/* A clean directory is an empty directory... */
1356 			ASSERT(dv->dv_nlink == 2);
1357 			mutex_enter(&vp->v_lock);
1358 			if (vp->v_count > 0) {
1359 				/*
1360 				 * ... but an empty directory can still have
1361 				 * references to it. If we have dv_busy or
1362 				 * DV_CLEAN_FORCE is *not* specified then a
1363 				 * referenced directory is considered busy.
1364 				 */
1365 				if (dv->dv_busy || !(flags & DV_CLEAN_FORCE)) {
1366 					mutex_exit(&vp->v_lock);
1367 					rw_exit(&dv->dv_contents);
1368 					goto set_busy;
1369 				}
1370 
1371 				/*
1372 				 * Mark referenced directory stale so that DR
1373 				 * will succeed even if a shell has
1374 				 * /devices/xxx as current directory (causing
1375 				 * VN_HOLD reference to an empty directory).
1376 				 */
1377 				ASSERT(!DV_STALE(dv));
1378 				ndi_rele_devi(dv->dv_devi);
1379 				dv->dv_devi = NULL;	/* mark DV_STALE */
1380 			}
1381 		} else {
1382 			ASSERT((vp->v_type == VCHR) || (vp->v_type == VBLK));
1383 			ASSERT(dv->dv_nlink == 1);	/* no hard links */
1384 			mutex_enter(&vp->v_lock);
1385 			if (vp->v_count > 0) {
1386 				mutex_exit(&vp->v_lock);
1387 				goto set_busy;
1388 			}
1389 		}
1390 
1391 		/* unlink from directory */
1392 		dv_unlink(ddv, dv);
1393 
1394 		/* drop locks */
1395 		mutex_exit(&vp->v_lock);
1396 		if (vp->v_type == VDIR)
1397 			rw_exit(&dv->dv_contents);
1398 
1399 		/* destroy vnode if ref count is zero */
1400 		if (vp->v_count == 0)
1401 			dv_destroy(dv, flags);
1402 
1403 		continue;
1404 
1405 		/*
1406 		 * If devnm is not NULL we return immediately on busy,
1407 		 * otherwise we continue destroying unused dv_node's.
1408 		 */
1409 set_busy:	busy++;
1410 		if (devnm)
1411 			break;
1412 	}
1413 
1414 	/*
1415 	 * This code may be invoked to inform devfs that a new node has
1416 	 * been created in the kernel device tree. So we always set
1417 	 * the DV_BUILD flag to allow the next dv_filldir() to pick
1418 	 * the new devinfo nodes.
1419 	 */
1420 	ddv->dv_flags |= DV_BUILD;
1421 
1422 	if (!(flags & DV_CLEANDIR_LCK))
1423 		rw_exit(&ddv->dv_contents);
1424 
1425 	return (busy ? EBUSY : 0);
1426 }
1427 
1428 /*
1429  * Walk through the devfs hierarchy, correcting the permissions of
1430  * devices with default permissions that do not match those specified
1431  * by minor perm.  This can only be done for all drivers for now.
1432  */
1433 static int
1434 dv_reset_perm_dir(struct dv_node *ddv, uint_t flags)
1435 {
1436 	struct dv_node *dv;
1437 	struct vnode *vp;
1438 	int retval = 0;
1439 	struct vattr *attrp;
1440 	mperm_t mp;
1441 	char *nm;
1442 	uid_t old_uid;
1443 	gid_t old_gid;
1444 	mode_t old_mode;
1445 
1446 	rw_enter(&ddv->dv_contents, RW_WRITER);
1447 	for (dv = DV_FIRST_ENTRY(ddv); dv; dv = DV_NEXT_ENTRY(ddv, dv)) {
1448 		int error = 0;
1449 		nm = dv->dv_name;
1450 
1451 		rw_enter(&dv->dv_contents, RW_READER);
1452 		vp = DVTOV(dv);
1453 		if (vp->v_type == VDIR) {
1454 			rw_exit(&dv->dv_contents);
1455 			if (dv_reset_perm_dir(dv, flags) != 0) {
1456 				error = EBUSY;
1457 			}
1458 		} else {
1459 			ASSERT(vp->v_type == VCHR || vp->v_type == VBLK);
1460 
1461 			/*
1462 			 * Check for permissions from minor_perm
1463 			 * If there are none, we're done
1464 			 */
1465 			rw_exit(&dv->dv_contents);
1466 			if (dev_minorperm(dv->dv_devi, nm, &mp) != 0)
1467 				continue;
1468 
1469 			rw_enter(&dv->dv_contents, RW_READER);
1470 
1471 			/*
1472 			 * Allow a node's permissions to be altered
1473 			 * permanently from the defaults by chmod,
1474 			 * using the shadow node as backing store.
1475 			 * Otherwise, update node to minor_perm permissions.
1476 			 */
1477 			if (dv->dv_attrvp == NULLVP) {
1478 				/*
1479 				 * No attribute vp, try to find one.
1480 				 */
1481 				dv_shadow_node(DVTOV(ddv), nm, vp,
1482 				    NULL, NULLVP, kcred, 0);
1483 			}
1484 			if (dv->dv_attrvp != NULLVP || dv->dv_attr == NULL) {
1485 				rw_exit(&dv->dv_contents);
1486 				continue;
1487 			}
1488 
1489 			attrp = dv->dv_attr;
1490 
1491 			if (VATTRP_MP_CMP(attrp, mp) == 0) {
1492 				dcmn_err5(("%s: no perm change: "
1493 				    "%d %d 0%o\n", nm, attrp->va_uid,
1494 				    attrp->va_gid, attrp->va_mode));
1495 				rw_exit(&dv->dv_contents);
1496 				continue;
1497 			}
1498 
1499 			old_uid = attrp->va_uid;
1500 			old_gid = attrp->va_gid;
1501 			old_mode = attrp->va_mode;
1502 
1503 			VATTRP_MP_MERGE(attrp, mp);
1504 			mutex_enter(&vp->v_lock);
1505 			if (vp->v_count > 0) {
1506 				error = EBUSY;
1507 			}
1508 			mutex_exit(&vp->v_lock);
1509 
1510 			dcmn_err5(("%s: perm %d/%d/0%o -> %d/%d/0%o (%d)\n",
1511 			    nm, old_uid, old_gid, old_mode, attrp->va_uid,
1512 			    attrp->va_gid, attrp->va_mode, error));
1513 
1514 			rw_exit(&dv->dv_contents);
1515 		}
1516 
1517 		if (error != 0) {
1518 			retval = error;
1519 		}
1520 	}
1521 
1522 	ddv->dv_flags |= DV_BUILD;
1523 
1524 	rw_exit(&ddv->dv_contents);
1525 
1526 	return (retval);
1527 }
1528 
1529 int
1530 devfs_reset_perm(uint_t flags)
1531 {
1532 	struct dv_node *dvp;
1533 	int rval;
1534 
1535 	if ((dvp = devfs_dip_to_dvnode(ddi_root_node())) == NULL)
1536 		return (0);
1537 
1538 	VN_HOLD(DVTOV(dvp));
1539 	rval = dv_reset_perm_dir(dvp, flags);
1540 	VN_RELE(DVTOV(dvp));
1541 	return (rval);
1542 }
1543 
1544 /*
1545  * Clean up dangling devfs shadow nodes for removed
1546  * drivers so that, in the event the driver is re-added
1547  * to the system, newly created nodes won't incorrectly
1548  * pick up these stale shadow node permissions.
1549  *
1550  * This is accomplished by walking down the pathname
1551  * to the directory, starting at the root's attribute
1552  * node, then removing all minors matching the specified
1553  * node name.  Care must be taken to remove all entries
1554  * in a directory before the directory itself, so that
1555  * the clean-up associated with rem_drv'ing a nexus driver
1556  * does not inadvertently result in an inconsistent
1557  * filesystem underlying devfs.
1558  */
1559 
1560 static int
1561 devfs_remdrv_rmdir(vnode_t *dirvp, const char *dir, vnode_t *rvp)
1562 {
1563 	int error;
1564 	vnode_t *vp;
1565 	int eof;
1566 	struct iovec iov;
1567 	struct uio uio;
1568 	struct dirent64 *dp;
1569 	dirent64_t *dbuf;
1570 	size_t dlen;
1571 	size_t dbuflen;
1572 	int ndirents = 64;
1573 	char *nm;
1574 
1575 	VN_HOLD(dirvp);
1576 
1577 	dlen = ndirents * (sizeof (*dbuf));
1578 	dbuf = kmem_alloc(dlen, KM_SLEEP);
1579 
1580 	uio.uio_iov = &iov;
1581 	uio.uio_iovcnt = 1;
1582 	uio.uio_segflg = UIO_SYSSPACE;
1583 	uio.uio_fmode = 0;
1584 	uio.uio_extflg = UIO_COPY_CACHED;
1585 	uio.uio_loffset = 0;
1586 	uio.uio_llimit = MAXOFFSET_T;
1587 
1588 	eof = 0;
1589 	error = 0;
1590 	while (!error && !eof) {
1591 		uio.uio_resid = dlen;
1592 		iov.iov_base = (char *)dbuf;
1593 		iov.iov_len = dlen;
1594 
1595 		(void) VOP_RWLOCK(dirvp, V_WRITELOCK_FALSE, NULL);
1596 		error = VOP_READDIR(dirvp, &uio, kcred, &eof, NULL, 0);
1597 		VOP_RWUNLOCK(dirvp, V_WRITELOCK_FALSE, NULL);
1598 
1599 		dbuflen = dlen - uio.uio_resid;
1600 
1601 		if (error || dbuflen == 0)
1602 			break;
1603 
1604 		for (dp = dbuf; ((intptr_t)dp < (intptr_t)dbuf + dbuflen);
1605 		    dp = (dirent64_t *)((intptr_t)dp + dp->d_reclen)) {
1606 
1607 			nm = dp->d_name;
1608 
1609 			if (strcmp(nm, ".") == 0 || strcmp(nm, "..") == 0)
1610 				continue;
1611 
1612 			error = VOP_LOOKUP(dirvp, nm,
1613 			    &vp, NULL, 0, NULL, kcred, NULL, NULL, NULL);
1614 
1615 			dsysdebug(error,
1616 			    ("rem_drv %s/%s lookup (%d)\n",
1617 			    dir, nm, error));
1618 
1619 			if (error)
1620 				continue;
1621 
1622 			ASSERT(vp->v_type == VDIR ||
1623 			    vp->v_type == VCHR || vp->v_type == VBLK);
1624 
1625 			if (vp->v_type == VDIR) {
1626 				error = devfs_remdrv_rmdir(vp, nm, rvp);
1627 				if (error == 0) {
1628 					error = VOP_RMDIR(dirvp,
1629 					    (char *)nm, rvp, kcred, NULL, 0);
1630 					dsysdebug(error,
1631 					    ("rem_drv %s/%s rmdir (%d)\n",
1632 					    dir, nm, error));
1633 				}
1634 			} else {
1635 				error = VOP_REMOVE(dirvp, (char *)nm, kcred,
1636 				    NULL, 0);
1637 				dsysdebug(error,
1638 				    ("rem_drv %s/%s remove (%d)\n",
1639 				    dir, nm, error));
1640 			}
1641 
1642 			VN_RELE(vp);
1643 			if (error) {
1644 				goto exit;
1645 			}
1646 		}
1647 	}
1648 
1649 exit:
1650 	VN_RELE(dirvp);
1651 	kmem_free(dbuf, dlen);
1652 
1653 	return (error);
1654 }
1655 
1656 int
1657 devfs_remdrv_cleanup(const char *dir, const char *nodename)
1658 {
1659 	int error;
1660 	vnode_t *vp;
1661 	vnode_t *dirvp;
1662 	int eof;
1663 	struct iovec iov;
1664 	struct uio uio;
1665 	struct dirent64 *dp;
1666 	dirent64_t *dbuf;
1667 	size_t dlen;
1668 	size_t dbuflen;
1669 	int ndirents = 64;
1670 	int nodenamelen = strlen(nodename);
1671 	char *nm;
1672 	struct pathname pn;
1673 	vnode_t *rvp;		/* root node of the underlying attribute fs */
1674 
1675 	dcmn_err5(("devfs_remdrv_cleanup: %s %s\n", dir, nodename));
1676 
1677 	if (error = pn_get((char *)dir, UIO_SYSSPACE, &pn))
1678 		return (0);
1679 
1680 	rvp = dvroot->dv_attrvp;
1681 	ASSERT(rvp != NULL);
1682 	VN_HOLD(rvp);
1683 
1684 	pn_skipslash(&pn);
1685 	dirvp = rvp;
1686 	VN_HOLD(dirvp);
1687 
1688 	nm = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1689 
1690 	while (pn_pathleft(&pn)) {
1691 		ASSERT(dirvp->v_type == VDIR);
1692 		(void) pn_getcomponent(&pn, nm);
1693 		ASSERT((strcmp(nm, ".") != 0) && (strcmp(nm, "..") != 0));
1694 		error = VOP_LOOKUP(dirvp, nm, &vp, NULL, 0, rvp, kcred,
1695 		    NULL, NULL, NULL);
1696 		if (error) {
1697 			dcmn_err5(("remdrv_cleanup %s lookup error %d\n",
1698 			    nm, error));
1699 			VN_RELE(dirvp);
1700 			if (dirvp != rvp)
1701 				VN_RELE(rvp);
1702 			pn_free(&pn);
1703 			kmem_free(nm, MAXNAMELEN);
1704 			return (0);
1705 		}
1706 		VN_RELE(dirvp);
1707 		dirvp = vp;
1708 		pn_skipslash(&pn);
1709 	}
1710 
1711 	ASSERT(dirvp->v_type == VDIR);
1712 	if (dirvp != rvp)
1713 		VN_RELE(rvp);
1714 	pn_free(&pn);
1715 	kmem_free(nm, MAXNAMELEN);
1716 
1717 	dlen = ndirents * (sizeof (*dbuf));
1718 	dbuf = kmem_alloc(dlen, KM_SLEEP);
1719 
1720 	uio.uio_iov = &iov;
1721 	uio.uio_iovcnt = 1;
1722 	uio.uio_segflg = UIO_SYSSPACE;
1723 	uio.uio_fmode = 0;
1724 	uio.uio_extflg = UIO_COPY_CACHED;
1725 	uio.uio_loffset = 0;
1726 	uio.uio_llimit = MAXOFFSET_T;
1727 
1728 	eof = 0;
1729 	error = 0;
1730 	while (!error && !eof) {
1731 		uio.uio_resid = dlen;
1732 		iov.iov_base = (char *)dbuf;
1733 		iov.iov_len = dlen;
1734 
1735 		(void) VOP_RWLOCK(dirvp, V_WRITELOCK_FALSE, NULL);
1736 		error = VOP_READDIR(dirvp, &uio, kcred, &eof, NULL, 0);
1737 		VOP_RWUNLOCK(dirvp, V_WRITELOCK_FALSE, NULL);
1738 
1739 		dbuflen = dlen - uio.uio_resid;
1740 
1741 		if (error || dbuflen == 0)
1742 			break;
1743 
1744 		for (dp = dbuf; ((intptr_t)dp < (intptr_t)dbuf + dbuflen);
1745 		    dp = (dirent64_t *)((intptr_t)dp + dp->d_reclen)) {
1746 
1747 			nm = dp->d_name;
1748 
1749 			if (strcmp(nm, ".") == 0 || strcmp(nm, "..") == 0)
1750 				continue;
1751 
1752 			if (strncmp(nm, nodename, nodenamelen) != 0)
1753 				continue;
1754 
1755 			error = VOP_LOOKUP(dirvp, nm, &vp,
1756 			    NULL, 0, NULL, kcred, NULL, NULL, NULL);
1757 
1758 			dsysdebug(error,
1759 			    ("rem_drv %s/%s lookup (%d)\n",
1760 			    dir, nm, error));
1761 
1762 			if (error)
1763 				continue;
1764 
1765 			ASSERT(vp->v_type == VDIR ||
1766 			    vp->v_type == VCHR || vp->v_type == VBLK);
1767 
1768 			if (vp->v_type == VDIR) {
1769 				error = devfs_remdrv_rmdir(vp, nm, rvp);
1770 				if (error == 0) {
1771 					error = VOP_RMDIR(dirvp, (char *)nm,
1772 					    rvp, kcred, NULL, 0);
1773 					dsysdebug(error,
1774 					    ("rem_drv %s/%s rmdir (%d)\n",
1775 					    dir, nm, error));
1776 				}
1777 			} else {
1778 				error = VOP_REMOVE(dirvp, (char *)nm, kcred,
1779 				    NULL, 0);
1780 				dsysdebug(error,
1781 				    ("rem_drv %s/%s remove (%d)\n",
1782 				    dir, nm, error));
1783 			}
1784 
1785 			VN_RELE(vp);
1786 			if (error)
1787 				goto exit;
1788 		}
1789 	}
1790 
1791 exit:
1792 	VN_RELE(dirvp);
1793 
1794 	kmem_free(dbuf, dlen);
1795 
1796 	return (0);
1797 }
1798 
1799 struct dv_list {
1800 	struct dv_node	*dv;
1801 	struct dv_list	*next;
1802 };
1803 
1804 void
1805 dv_walk(
1806 	struct dv_node	*ddv,
1807 	char		*devnm,
1808 	void		(*callback)(struct dv_node *, void *),
1809 	void		*arg)
1810 {
1811 	struct vnode	*dvp;
1812 	struct dv_node	*dv;
1813 	struct dv_list	*head, *tail, *next;
1814 	int		len;
1815 
1816 	dcmn_err3(("dv_walk: ddv = %s, devnm = %s\n",
1817 	    ddv->dv_name, devnm ? devnm : "<null>"));
1818 
1819 	dvp = DVTOV(ddv);
1820 
1821 	ASSERT(dvp->v_type == VDIR);
1822 
1823 	head = tail = next = NULL;
1824 
1825 	rw_enter(&ddv->dv_contents, RW_READER);
1826 	mutex_enter(&dvp->v_lock);
1827 	for (dv = DV_FIRST_ENTRY(ddv); dv; dv = DV_NEXT_ENTRY(ddv, dv)) {
1828 		/*
1829 		 * If devnm is not NULL and is not the empty string,
1830 		 * select only dv_nodes with matching non-minor name
1831 		 */
1832 		if (devnm && (len = strlen(devnm)) &&
1833 		    (strncmp(devnm, dv->dv_name, len) ||
1834 		    (dv->dv_name[len] != ':' && dv->dv_name[len] != '\0')))
1835 			continue;
1836 
1837 		callback(dv, arg);
1838 
1839 		if (DVTOV(dv)->v_type != VDIR)
1840 			continue;
1841 
1842 		next = kmem_zalloc(sizeof (*next), KM_SLEEP);
1843 		next->dv = dv;
1844 
1845 		if (tail)
1846 			tail->next = next;
1847 		else
1848 			head = next;
1849 
1850 		tail = next;
1851 	}
1852 
1853 	while (head) {
1854 		dv_walk(head->dv, NULL, callback, arg);
1855 		next = head->next;
1856 		kmem_free(head, sizeof (*head));
1857 		head = next;
1858 	}
1859 	rw_exit(&ddv->dv_contents);
1860 	mutex_exit(&dvp->v_lock);
1861 }
1862