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