xref: /titanic_44/usr/src/uts/common/fs/dev/sdev_subr.c (revision b81bb599d2182818b41e3bbe6d4d1771b9d484d4)
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 2006 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  * utility routines for the /dev fs
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/mode.h>
54 #include <sys/policy.h>
55 #include <fs/fs_subr.h>
56 #include <sys/mount.h>
57 #include <sys/fs/snode.h>
58 #include <sys/fs/dv_node.h>
59 #include <sys/fs/sdev_impl.h>
60 #include <sys/fs/sdev_node.h>
61 #include <sys/sunndi.h>
62 #include <sys/sunmdi.h>
63 #include <sys/conf.h>
64 #include <sys/proc.h>
65 #include <sys/user.h>
66 #include <sys/modctl.h>
67 
68 #ifdef DEBUG
69 int sdev_debug = 0x00000001;
70 int sdev_debug_cache_flags = 0;
71 #endif
72 
73 /*
74  * globals
75  */
76 /* prototype memory vattrs */
77 vattr_t sdev_vattr_dir = {
78 	AT_TYPE|AT_MODE|AT_UID|AT_GID,		/* va_mask */
79 	VDIR,					/* va_type */
80 	SDEV_DIRMODE_DEFAULT,			/* va_mode */
81 	SDEV_UID_DEFAULT,			/* va_uid */
82 	SDEV_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_vcode */
94 };
95 
96 vattr_t sdev_vattr_lnk = {
97 	AT_TYPE|AT_MODE,			/* va_mask */
98 	VLNK,					/* va_type */
99 	SDEV_LNKMODE_DEFAULT,			/* va_mode */
100 	SDEV_UID_DEFAULT,			/* va_uid */
101 	SDEV_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_vcode */
113 };
114 
115 vattr_t sdev_vattr_blk = {
116 	AT_TYPE|AT_MODE|AT_UID|AT_GID,		/* va_mask */
117 	VBLK,					/* va_type */
118 	S_IFBLK | SDEV_DEVMODE_DEFAULT,		/* va_mode */
119 	SDEV_UID_DEFAULT,			/* va_uid */
120 	SDEV_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_vcode */
132 };
133 
134 vattr_t sdev_vattr_chr = {
135 	AT_TYPE|AT_MODE|AT_UID|AT_GID,		/* va_mask */
136 	VCHR,					/* va_type */
137 	S_IFCHR | SDEV_DEVMODE_DEFAULT,		/* va_mode */
138 	SDEV_UID_DEFAULT,			/* va_uid */
139 	SDEV_GID_DEFAULT,			/* va_gid */
140 	0,					/* va_fsid */
141 	0,					/* va_nodeid */
142 	0,					/* va_nlink */
143 	0,					/* va_size */
144 	0,					/* va_atime */
145 	0,					/* va_mtime */
146 	0,					/* va_ctime */
147 	0,					/* va_rdev */
148 	0,					/* va_blksize */
149 	0,					/* va_nblocks */
150 	0					/* va_vcode */
151 };
152 
153 kmem_cache_t	*sdev_node_cache;	/* sdev_node cache */
154 int		devtype;		/* fstype */
155 
156 struct devname_ops *devname_ns_ops;	/* default name service directory ops */
157 kmutex_t devname_nsmaps_lock;	/* protect devname_nsmaps */
158 
159 /* static */
160 static struct devname_nsmap *devname_nsmaps = NULL;
161 				/* contents from /etc/dev/devname_master */
162 static int devname_nsmaps_invalidated = 0; /* "devfsadm -m" has run */
163 
164 static struct vnodeops *sdev_get_vop(struct sdev_node *);
165 static void sdev_set_no_nocache(struct sdev_node *);
166 static int sdev_get_moduleops(struct sdev_node *);
167 static void sdev_handle_alloc(struct sdev_node *);
168 static fs_operation_def_t *sdev_merge_vtab(const fs_operation_def_t []);
169 static void sdev_free_vtab(fs_operation_def_t *);
170 
171 static void
172 sdev_prof_free(struct sdev_node *dv)
173 {
174 	ASSERT(!SDEV_IS_GLOBAL(dv));
175 	if (dv->sdev_prof.dev_name)
176 		nvlist_free(dv->sdev_prof.dev_name);
177 	if (dv->sdev_prof.dev_map)
178 		nvlist_free(dv->sdev_prof.dev_map);
179 	if (dv->sdev_prof.dev_symlink)
180 		nvlist_free(dv->sdev_prof.dev_symlink);
181 	if (dv->sdev_prof.dev_glob_incdir)
182 		nvlist_free(dv->sdev_prof.dev_glob_incdir);
183 	if (dv->sdev_prof.dev_glob_excdir)
184 		nvlist_free(dv->sdev_prof.dev_glob_excdir);
185 	bzero(&dv->sdev_prof, sizeof (dv->sdev_prof));
186 }
187 
188 /*
189  * sdev_node cache constructor
190  */
191 /*ARGSUSED1*/
192 static int
193 i_sdev_node_ctor(void *buf, void *cfarg, int flag)
194 {
195 	struct sdev_node *dv = (struct sdev_node *)buf;
196 	struct vnode *vp;
197 
198 	ASSERT(flag == KM_SLEEP);
199 
200 	bzero(buf, sizeof (struct sdev_node));
201 	rw_init(&dv->sdev_contents, NULL, RW_DEFAULT, NULL);
202 	dv->sdev_vnode = vn_alloc(KM_SLEEP);
203 	vp = SDEVTOV(dv);
204 	vp->v_data = (caddr_t)dv;
205 	return (0);
206 }
207 
208 /* sdev_node destructor for kmem cache */
209 /*ARGSUSED1*/
210 static void
211 i_sdev_node_dtor(void *buf, void *arg)
212 {
213 	struct sdev_node *dv = (struct sdev_node *)buf;
214 	struct vnode *vp = SDEVTOV(dv);
215 
216 	rw_destroy(&dv->sdev_contents);
217 	vn_free(vp);
218 }
219 
220 /* initialize sdev_node cache */
221 void
222 sdev_node_cache_init()
223 {
224 	int flags = 0;
225 
226 #ifdef	DEBUG
227 	flags = sdev_debug_cache_flags;
228 	if (flags)
229 		sdcmn_err(("cache debug flags 0x%x\n", flags));
230 #endif	/* DEBUG */
231 
232 	ASSERT(sdev_node_cache == NULL);
233 	sdev_node_cache = kmem_cache_create("sdev_node_cache",
234 	    sizeof (struct sdev_node), 0, i_sdev_node_ctor, i_sdev_node_dtor,
235 	    NULL, NULL, NULL, flags);
236 }
237 
238 /* destroy sdev_node cache */
239 void
240 sdev_node_cache_fini()
241 {
242 	ASSERT(sdev_node_cache != NULL);
243 	kmem_cache_destroy(sdev_node_cache);
244 	sdev_node_cache = NULL;
245 }
246 
247 void
248 sdev_set_nodestate(struct sdev_node *dv, sdev_node_state_t state)
249 {
250 	ASSERT(dv);
251 	ASSERT(RW_WRITE_HELD(&dv->sdev_contents));
252 	dv->sdev_state = state;
253 }
254 
255 static void
256 sdev_attrinit(struct sdev_node *dv, vattr_t *vap)
257 {
258 	timestruc_t now;
259 
260 	ASSERT(vap);
261 
262 	dv->sdev_attr = kmem_zalloc(sizeof (struct vattr), KM_SLEEP);
263 	*dv->sdev_attr = *vap;
264 
265 	dv->sdev_attr->va_mode = MAKEIMODE(vap->va_type, vap->va_mode);
266 
267 	gethrestime(&now);
268 	dv->sdev_attr->va_atime = now;
269 	dv->sdev_attr->va_mtime = now;
270 	dv->sdev_attr->va_ctime = now;
271 }
272 
273 /* alloc and initialize a sdev_node */
274 int
275 sdev_nodeinit(struct sdev_node *ddv, char *nm, struct sdev_node **newdv,
276     vattr_t *vap)
277 {
278 	struct sdev_node *dv = NULL;
279 	struct vnode *vp;
280 	size_t nmlen, len;
281 	devname_handle_t  *dhl;
282 
283 	nmlen = strlen(nm) + 1;
284 	if (nmlen > MAXNAMELEN) {
285 		sdcmn_err9(("sdev_nodeinit: node name %s"
286 		    " too long\n", nm));
287 		*newdv = NULL;
288 		return (ENAMETOOLONG);
289 	}
290 
291 	dv = kmem_cache_alloc(sdev_node_cache, KM_SLEEP);
292 
293 	dv->sdev_name = kmem_alloc(nmlen, KM_SLEEP);
294 	bcopy(nm, dv->sdev_name, nmlen);
295 	dv->sdev_namelen = nmlen - 1;	/* '\0' not included */
296 	len = strlen(ddv->sdev_path) + strlen(nm) + 2;
297 	dv->sdev_path = kmem_alloc(len, KM_SLEEP);
298 	(void) snprintf(dv->sdev_path, len, "%s/%s", ddv->sdev_path, nm);
299 	/* overwritten for VLNK nodes */
300 	dv->sdev_symlink = NULL;
301 
302 	vp = SDEVTOV(dv);
303 	vn_reinit(vp);
304 	vp->v_vfsp = SDEVTOV(ddv)->v_vfsp;
305 	if (vap)
306 		vp->v_type = vap->va_type;
307 
308 	/*
309 	 * initialized to the parent's vnodeops.
310 	 * maybe overwriten for a VDIR
311 	 */
312 	vn_setops(vp, vn_getops(SDEVTOV(ddv)));
313 	vn_exists(vp);
314 
315 	dv->sdev_dotdot = NULL;
316 	dv->sdev_dot = NULL;
317 	dv->sdev_next = NULL;
318 	dv->sdev_attrvp = NULL;
319 	if (vap) {
320 		sdev_attrinit(dv, vap);
321 	} else {
322 		dv->sdev_attr = NULL;
323 	}
324 
325 	dv->sdev_ino = sdev_mkino(dv);
326 	dv->sdev_nlink = 0;		/* updated on insert */
327 	dv->sdev_flags = ddv->sdev_flags; /* inherit from the parent first */
328 	dv->sdev_flags |= SDEV_BUILD;
329 	mutex_init(&dv->sdev_lookup_lock, NULL, MUTEX_DEFAULT, NULL);
330 	cv_init(&dv->sdev_lookup_cv, NULL, CV_DEFAULT, NULL);
331 	if (SDEV_IS_GLOBAL(ddv)) {
332 		dv->sdev_flags |= SDEV_GLOBAL;
333 		dv->sdev_mapinfo = NULL;
334 		dhl = &(dv->sdev_handle);
335 		dhl->dh_data = dv;
336 		dhl->dh_spec = DEVNAME_NS_NONE;
337 		dhl->dh_args = NULL;
338 		sdev_set_no_nocache(dv);
339 		dv->sdev_gdir_gen = 0;
340 	} else {
341 		dv->sdev_flags &= ~SDEV_GLOBAL;
342 		dv->sdev_origin = NULL; /* set later */
343 		bzero(&dv->sdev_prof, sizeof (dv->sdev_prof));
344 		dv->sdev_ldir_gen = 0;
345 		dv->sdev_devtree_gen = 0;
346 	}
347 
348 	rw_enter(&dv->sdev_contents, RW_WRITER);
349 	sdev_set_nodestate(dv, SDEV_INIT);
350 	rw_exit(&dv->sdev_contents);
351 	*newdv = dv;
352 
353 	return (0);
354 }
355 
356 /*
357  * transition a sdev_node into SDEV_READY state
358  */
359 int
360 sdev_nodeready(struct sdev_node *dv, struct vattr *vap, struct vnode *avp,
361     void *args, struct cred *cred)
362 {
363 	int error = 0;
364 	struct vnode *vp = SDEVTOV(dv);
365 	vtype_t type;
366 
367 	ASSERT(dv && (dv->sdev_state != SDEV_READY) && vap);
368 
369 	type = vap->va_type;
370 	vp->v_type = type;
371 	vp->v_rdev = vap->va_rdev;
372 	rw_enter(&dv->sdev_contents, RW_WRITER);
373 	if (type == VDIR) {
374 		dv->sdev_nlink = 2;
375 		dv->sdev_flags &= ~SDEV_PERSIST;
376 		dv->sdev_flags &= ~SDEV_DYNAMIC;
377 		vn_setops(vp, sdev_get_vop(dv)); /* from internal vtab */
378 		error = sdev_get_moduleops(dv); /* from plug-in module */
379 		ASSERT(dv->sdev_dotdot);
380 		ASSERT(SDEVTOV(dv->sdev_dotdot)->v_type == VDIR);
381 		vp->v_rdev = SDEVTOV(dv->sdev_dotdot)->v_rdev;
382 	} else if (type == VLNK) {
383 		ASSERT(args);
384 		dv->sdev_nlink = 1;
385 		dv->sdev_symlink = i_ddi_strdup((char *)args, KM_SLEEP);
386 	} else {
387 		dv->sdev_nlink = 1;
388 	}
389 
390 	if (!(SDEV_IS_GLOBAL(dv))) {
391 		dv->sdev_origin = (struct sdev_node *)args;
392 		dv->sdev_flags &= ~SDEV_PERSIST;
393 	}
394 
395 	/*
396 	 * shadow node is created here OR
397 	 * if failed (indicated by dv->sdev_attrvp == NULL),
398 	 * created later in sdev_setattr
399 	 */
400 	if (avp) {
401 		dv->sdev_attrvp = avp;
402 	} else {
403 		if (dv->sdev_attr == NULL)
404 			sdev_attrinit(dv, vap);
405 		else
406 			*dv->sdev_attr = *vap;
407 
408 		if ((SDEV_IS_PERSIST(dv) && (dv->sdev_attrvp == NULL)) ||
409 		    ((SDEVTOV(dv)->v_type == VDIR) &&
410 		    (dv->sdev_attrvp == NULL)))
411 			error = sdev_shadow_node(dv, cred);
412 	}
413 
414 	/* transition to READY state */
415 	sdev_set_nodestate(dv, SDEV_READY);
416 	sdev_nc_node_exists(dv);
417 	rw_exit(&dv->sdev_contents);
418 	return (error);
419 }
420 
421 /*
422  * setting ZOMBIE state
423  */
424 static int
425 sdev_nodezombied(struct sdev_node *dv)
426 {
427 	rw_enter(&dv->sdev_contents, RW_WRITER);
428 	sdev_set_nodestate(dv, SDEV_ZOMBIE);
429 	rw_exit(&dv->sdev_contents);
430 	return (0);
431 }
432 
433 /*
434  * Build the VROOT sdev_node.
435  */
436 /*ARGSUSED*/
437 struct sdev_node *
438 sdev_mkroot(struct vfs *vfsp, dev_t devdev, struct vnode *mvp,
439     struct vnode *avp, struct cred *cred)
440 {
441 	struct sdev_node *dv;
442 	struct vnode *vp;
443 	char devdir[] = "/dev";
444 
445 	ASSERT(sdev_node_cache != NULL);
446 	ASSERT(avp);
447 	dv = kmem_cache_alloc(sdev_node_cache, KM_SLEEP);
448 	vp = SDEVTOV(dv);
449 	vn_reinit(vp);
450 	vp->v_flag |= VROOT;
451 	vp->v_vfsp = vfsp;
452 	vp->v_type = VDIR;
453 	vp->v_rdev = devdev;
454 	vn_setops(vp, sdev_vnodeops); /* apply the default vnodeops at /dev */
455 	vn_exists(vp);
456 
457 	if (vfsp->vfs_mntpt)
458 		dv->sdev_name = i_ddi_strdup(
459 		    (char *)refstr_value(vfsp->vfs_mntpt), KM_SLEEP);
460 	else
461 		/* vfs_mountdev1 set mount point later */
462 		dv->sdev_name = i_ddi_strdup("/dev", KM_SLEEP);
463 	dv->sdev_namelen = strlen(dv->sdev_name); /* '\0' not included */
464 	dv->sdev_path = i_ddi_strdup(devdir, KM_SLEEP);
465 	dv->sdev_ino = SDEV_ROOTINO;
466 	dv->sdev_nlink = 2;		/* name + . (no sdev_insert) */
467 	dv->sdev_dotdot = dv;		/* .. == self */
468 	dv->sdev_attrvp = avp;
469 	dv->sdev_attr = NULL;
470 	mutex_init(&dv->sdev_lookup_lock, NULL, MUTEX_DEFAULT, NULL);
471 	cv_init(&dv->sdev_lookup_cv, NULL, CV_DEFAULT, NULL);
472 	if (strcmp(dv->sdev_name, "/dev") == 0) {
473 		mutex_init(&devname_nsmaps_lock, NULL, MUTEX_DEFAULT, NULL);
474 		dv->sdev_mapinfo = NULL;
475 		dv->sdev_flags = SDEV_BUILD|SDEV_GLOBAL|SDEV_PERSIST;
476 		bzero(&dv->sdev_handle, sizeof (dv->sdev_handle));
477 		dv->sdev_gdir_gen = 0;
478 	} else {
479 		dv->sdev_flags = SDEV_BUILD;
480 		dv->sdev_flags &= ~SDEV_PERSIST;
481 		bzero(&dv->sdev_prof, sizeof (dv->sdev_prof));
482 		dv->sdev_ldir_gen = 0;
483 		dv->sdev_devtree_gen = 0;
484 	}
485 
486 	rw_enter(&dv->sdev_contents, RW_WRITER);
487 	sdev_set_nodestate(dv, SDEV_READY);
488 	rw_exit(&dv->sdev_contents);
489 	sdev_nc_node_exists(dv);
490 	return (dv);
491 }
492 
493 /*
494  *  1. load the module
495  *  2. modload invokes sdev_module_register, which in turn sets
496  *     the dv->sdev_mapinfo->dir_ops
497  *
498  * note: locking order:
499  *	dv->sdev_contents -> map->dir_lock
500  */
501 static int
502 sdev_get_moduleops(struct sdev_node *dv)
503 {
504 	int error = 0;
505 	struct devname_nsmap *map = NULL;
506 	char *module;
507 	char *path;
508 	int load = 1;
509 
510 	ASSERT(SDEVTOV(dv)->v_type == VDIR);
511 
512 	if (devname_nsmaps == NULL)
513 		return (0);
514 
515 	if (!sdev_nsmaps_loaded() && !sdev_nsmaps_reloaded())
516 		return (0);
517 
518 
519 	path = dv->sdev_path;
520 	if ((map = sdev_get_nsmap_by_dir(path, 0))) {
521 		rw_enter(&map->dir_lock, RW_READER);
522 		if (map->dir_invalid) {
523 			if (map->dir_module && map->dir_newmodule &&
524 			    (strcmp(map->dir_module,
525 					map->dir_newmodule) == 0)) {
526 				load = 0;
527 			}
528 			sdev_replace_nsmap(map, map->dir_newmodule,
529 			    map->dir_newmap);
530 		}
531 
532 		module = map->dir_module;
533 		if (module && load) {
534 			sdcmn_err6(("sdev_get_moduleops: "
535 			    "load module %s", module));
536 			rw_exit(&map->dir_lock);
537 			error = modload("devname", module);
538 			sdcmn_err6(("sdev_get_moduleops: error %d\n", error));
539 			if (error < 0) {
540 				return (-1);
541 			}
542 		} else if (module == NULL) {
543 			/*
544 			 * loading the module ops for name services
545 			 */
546 			if (devname_ns_ops == NULL) {
547 				sdcmn_err6((
548 				    "sdev_get_moduleops: modload default\n"));
549 				error = modload("devname", DEVNAME_NSCONFIG);
550 				sdcmn_err6((
551 				    "sdev_get_moduleops: error %d\n", error));
552 				if (error < 0) {
553 					return (-1);
554 				}
555 			}
556 
557 			if (!rw_tryupgrade(&map->dir_lock)) {
558 				rw_exit(&map->dir_lock);
559 				rw_enter(&map->dir_lock, RW_WRITER);
560 			}
561 			ASSERT(devname_ns_ops);
562 			map->dir_ops = devname_ns_ops;
563 			rw_exit(&map->dir_lock);
564 		}
565 	}
566 
567 	dv->sdev_mapinfo = map;
568 	return (0);
569 }
570 
571 /* directory dependent vop table */
572 struct sdev_vop_table {
573 	char *vt_name;				/* subdirectory name */
574 	const fs_operation_def_t *vt_service;	/* vnodeops table */
575 	struct vnodeops *vt_vops;		/* constructed vop */
576 	struct vnodeops **vt_global_vops;	/* global container for vop */
577 	int (*vt_vtor)(struct sdev_node *);	/* validate sdev_node */
578 	int vt_flags;
579 };
580 
581 /*
582  * A nice improvement would be to provide a plug-in mechanism
583  * for this table instead of a const table.
584  */
585 static struct sdev_vop_table vtab[] =
586 {
587 	{ "pts", devpts_vnodeops_tbl, NULL, &devpts_vnodeops, devpts_validate,
588 	SDEV_DYNAMIC | SDEV_VTOR },
589 
590 	{ "zcons", NULL, NULL, NULL, NULL, SDEV_NO_NCACHE },
591 
592 	{ NULL, NULL, NULL, NULL, NULL, 0}
593 };
594 
595 
596 /*
597  *  sets a directory's vnodeops if the directory is in the vtab;
598  */
599 static struct vnodeops *
600 sdev_get_vop(struct sdev_node *dv)
601 {
602 	int i;
603 	char *path;
604 
605 	path = dv->sdev_path;
606 	ASSERT(path);
607 
608 	/* gets the relative path to /dev/ */
609 	path += 5;
610 
611 	/* gets the vtab entry if matches */
612 	for (i = 0; vtab[i].vt_name; i++) {
613 		if (strcmp(vtab[i].vt_name, path) != 0)
614 			continue;
615 		dv->sdev_flags |= vtab[i].vt_flags;
616 
617 		if (vtab[i].vt_vops) {
618 			if (vtab[i].vt_global_vops)
619 				*(vtab[i].vt_global_vops) = vtab[i].vt_vops;
620 			return (vtab[i].vt_vops);
621 		}
622 
623 		if (vtab[i].vt_service) {
624 			fs_operation_def_t *templ;
625 			templ = sdev_merge_vtab(vtab[i].vt_service);
626 			if (vn_make_ops(vtab[i].vt_name,
627 			    (const fs_operation_def_t *)templ,
628 			    &vtab[i].vt_vops) != 0) {
629 				cmn_err(CE_PANIC, "%s: malformed vnode ops\n",
630 				    vtab[i].vt_name);
631 				/*NOTREACHED*/
632 			}
633 			if (vtab[i].vt_global_vops) {
634 				*(vtab[i].vt_global_vops) = vtab[i].vt_vops;
635 			}
636 			sdev_free_vtab(templ);
637 			return (vtab[i].vt_vops);
638 		}
639 		return (sdev_vnodeops);
640 	}
641 
642 	/* child inherits the persistence of the parent */
643 	if (SDEV_IS_PERSIST(dv->sdev_dotdot))
644 		dv->sdev_flags |= SDEV_PERSIST;
645 
646 	return (sdev_vnodeops);
647 }
648 
649 static void
650 sdev_set_no_nocache(struct sdev_node *dv)
651 {
652 	int i;
653 	char *path;
654 
655 	ASSERT(dv->sdev_path);
656 	path = dv->sdev_path + strlen("/dev/");
657 
658 	for (i = 0; vtab[i].vt_name; i++) {
659 		if (strcmp(vtab[i].vt_name, path) == 0) {
660 			if (vtab[i].vt_flags & SDEV_NO_NCACHE)
661 				dv->sdev_flags |= SDEV_NO_NCACHE;
662 			break;
663 		}
664 	}
665 }
666 
667 void *
668 sdev_get_vtor(struct sdev_node *dv)
669 {
670 	int i;
671 
672 	for (i = 0; vtab[i].vt_name; i++) {
673 		if (strcmp(vtab[i].vt_name, dv->sdev_name) != 0)
674 			continue;
675 		return ((void *)vtab[i].vt_vtor);
676 	}
677 	return (NULL);
678 }
679 
680 /*
681  * Build the base root inode
682  */
683 ino_t
684 sdev_mkino(struct sdev_node *dv)
685 {
686 	ino_t	ino;
687 
688 	/*
689 	 * for now, follow the lead of tmpfs here
690 	 * need to someday understand the requirements here
691 	 */
692 	ino = (ino_t)(uint32_t)((uintptr_t)dv >> 3);
693 	ino += SDEV_ROOTINO + 1;
694 
695 	return (ino);
696 }
697 
698 static int
699 sdev_getlink(struct vnode *linkvp, char **link)
700 {
701 	int err;
702 	char *buf;
703 	struct uio uio = {0};
704 	struct iovec iov = {0};
705 
706 	if (linkvp == NULL)
707 		return (ENOENT);
708 	ASSERT(linkvp->v_type == VLNK);
709 
710 	buf = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
711 	iov.iov_base = buf;
712 	iov.iov_len = MAXPATHLEN;
713 	uio.uio_iov = &iov;
714 	uio.uio_iovcnt = 1;
715 	uio.uio_resid = MAXPATHLEN;
716 	uio.uio_segflg = UIO_SYSSPACE;
717 	uio.uio_llimit = MAXOFFSET_T;
718 
719 	err = VOP_READLINK(linkvp, &uio, kcred);
720 	if (err) {
721 		cmn_err(CE_WARN, "readlink %s failed in dev\n", buf);
722 		kmem_free(buf, MAXPATHLEN);
723 		return (ENOENT);
724 	}
725 
726 	/* mission complete */
727 	*link = i_ddi_strdup(buf, KM_SLEEP);
728 	kmem_free(buf, MAXPATHLEN);
729 	return (0);
730 }
731 
732 /*
733  * A convenient wrapper to get the devfs node vnode for a device
734  * minor functionality: readlink() of a /dev symlink
735  * Place the link into dv->sdev_symlink
736  */
737 static int
738 sdev_follow_link(struct sdev_node *dv)
739 {
740 	int err;
741 	struct vnode *linkvp;
742 	char *link = NULL;
743 
744 	linkvp = SDEVTOV(dv);
745 	if (linkvp == NULL)
746 		return (ENOENT);
747 	ASSERT(linkvp->v_type == VLNK);
748 	err = sdev_getlink(linkvp, &link);
749 	if (err) {
750 		(void) sdev_nodezombied(dv);
751 		dv->sdev_symlink = NULL;
752 		return (ENOENT);
753 	}
754 
755 	ASSERT(link != NULL);
756 	dv->sdev_symlink = link;
757 	return (0);
758 }
759 
760 static int
761 sdev_node_check(struct sdev_node *dv, struct vattr *nvap, void *nargs)
762 {
763 	vtype_t otype = SDEVTOV(dv)->v_type;
764 
765 	/*
766 	 * existing sdev_node has a different type.
767 	 */
768 	if (otype != nvap->va_type) {
769 		sdcmn_err9(("sdev_node_check: existing node "
770 		    "  %s type %d does not match new node type %d\n",
771 		    dv->sdev_name, otype, nvap->va_type));
772 		return (EEXIST);
773 	}
774 
775 	/*
776 	 * For a symlink, the target should be the same.
777 	 */
778 	if (otype == VLNK) {
779 		ASSERT(nargs != NULL);
780 		ASSERT(dv->sdev_symlink != NULL);
781 		if (strcmp(dv->sdev_symlink, (char *)nargs) != 0) {
782 			sdcmn_err9(("sdev_node_check: existing node "
783 			    " %s has different symlink %s as new node "
784 			    " %s\n", dv->sdev_name, dv->sdev_symlink,
785 			    (char *)nargs));
786 			return (EEXIST);
787 		}
788 	}
789 
790 	return (0);
791 }
792 
793 /*
794  * sdev_mknode - a wrapper for sdev_nodeinit(), sdev_nodeready()
795  *
796  * arguments:
797  *	- ddv (parent)
798  *	- nm (child name)
799  *	- newdv (sdev_node for nm is returned here)
800  *	- vap (vattr for the node to be created, va_type should be set.
801  *	  the defaults should be used if unknown)
802  *	- cred
803  *	- args
804  *	    . tnm (for VLNK)
805  *	    . global sdev_node (for !SDEV_GLOBAL)
806  * 	- state: SDEV_INIT, SDEV_READY
807  *
808  * only ddv, nm, newddv, vap, cred are required for sdev_mknode(SDEV_INIT)
809  *
810  * NOTE:  directory contents writers lock needs to be held before
811  *	  calling this routine.
812  */
813 int
814 sdev_mknode(struct sdev_node *ddv, char *nm, struct sdev_node **newdv,
815     struct vattr *vap, struct vnode *avp, void *args, struct cred *cred,
816     sdev_node_state_t state)
817 {
818 	int error = 0;
819 	sdev_node_state_t node_state;
820 	struct sdev_node *dv = NULL;
821 
822 	ASSERT(state != SDEV_ZOMBIE);
823 	ASSERT(RW_WRITE_HELD(&ddv->sdev_contents));
824 
825 	if (*newdv) {
826 		dv = *newdv;
827 	} else {
828 		/* allocate and initialize a sdev_node */
829 		if (ddv->sdev_state == SDEV_ZOMBIE) {
830 			sdcmn_err9(("sdev_mknode: parent %s ZOMBIEd\n",
831 			    ddv->sdev_path));
832 			return (ENOENT);
833 		}
834 
835 		error = sdev_nodeinit(ddv, nm, &dv, vap);
836 		if (error != 0) {
837 			sdcmn_err9(("sdev_mknode: error %d,"
838 			    " name %s can not be initialized\n",
839 			    error, nm));
840 			return (ENOENT);
841 		}
842 		ASSERT(dv);
843 
844 		/* insert into the directory cache */
845 		error = sdev_cache_update(ddv, &dv, nm, SDEV_CACHE_ADD);
846 		if (error) {
847 			sdcmn_err9(("sdev_mknode: node %s can not"
848 			    " be added into directory cache\n", nm));
849 			return (ENOENT);
850 		}
851 	}
852 
853 	ASSERT(dv);
854 	node_state = dv->sdev_state;
855 	ASSERT(node_state != SDEV_ZOMBIE);
856 
857 	if (state == SDEV_READY) {
858 		switch (node_state) {
859 		case SDEV_INIT:
860 			error = sdev_nodeready(dv, vap, avp, args, cred);
861 			/*
862 			 * masking the errors with ENOENT
863 			 */
864 			if (error) {
865 				sdcmn_err9(("sdev_mknode: node %s can NOT"
866 				    " be transitioned into READY state, "
867 				    "error %d\n", nm, error));
868 				error = ENOENT;
869 			}
870 			break;
871 		case SDEV_READY:
872 			/*
873 			 * Do some sanity checking to make sure
874 			 * the existing sdev_node is what has been
875 			 * asked for.
876 			 */
877 			error = sdev_node_check(dv, vap, args);
878 			break;
879 		default:
880 			break;
881 		}
882 	}
883 
884 	if (!error) {
885 		*newdv = dv;
886 		ASSERT((*newdv)->sdev_state != SDEV_ZOMBIE);
887 	} else {
888 		SDEV_SIMPLE_RELE(dv);
889 		*newdv = NULL;
890 	}
891 
892 	return (error);
893 }
894 
895 /*
896  * convenient wrapper to change vp's ATIME, CTIME and ATIME
897  */
898 void
899 sdev_update_timestamps(struct vnode *vp, cred_t *cred, uint_t mask)
900 {
901 	struct vattr attr;
902 	timestruc_t now;
903 	int err;
904 
905 	ASSERT(vp);
906 	gethrestime(&now);
907 	if (mask & AT_CTIME)
908 		attr.va_ctime = now;
909 	if (mask & AT_MTIME)
910 		attr.va_mtime = now;
911 	if (mask & AT_ATIME)
912 		attr.va_atime = now;
913 
914 	attr.va_mask = (mask & AT_TIMES);
915 	err = VOP_SETATTR(vp, &attr, 0, cred, NULL);
916 	if (err && (err != EROFS)) {
917 		sdcmn_err(("update timestamps error %d\n", err));
918 	}
919 }
920 
921 /*
922  * the backing store vnode is released here
923  */
924 /*ARGSUSED1*/
925 void
926 sdev_nodedestroy(struct sdev_node *dv, uint_t flags)
927 {
928 	/* no references */
929 	ASSERT(dv->sdev_nlink == 0);
930 
931 	if (dv->sdev_attrvp != NULLVP) {
932 		VN_RELE(dv->sdev_attrvp);
933 		/*
934 		 * reset the attrvp so that no more
935 		 * references can be made on this already
936 		 * vn_rele() vnode
937 		 */
938 		dv->sdev_attrvp = NULLVP;
939 	}
940 
941 	if (dv->sdev_attr != NULL) {
942 		kmem_free(dv->sdev_attr, sizeof (struct vattr));
943 		dv->sdev_attr = NULL;
944 	}
945 
946 	if (dv->sdev_name != NULL) {
947 		kmem_free(dv->sdev_name, dv->sdev_namelen + 1);
948 		dv->sdev_name = NULL;
949 	}
950 
951 	if (dv->sdev_symlink != NULL) {
952 		kmem_free(dv->sdev_symlink, strlen(dv->sdev_symlink) + 1);
953 		dv->sdev_symlink = NULL;
954 	}
955 
956 	if (dv->sdev_path) {
957 		kmem_free(dv->sdev_path, strlen(dv->sdev_path) + 1);
958 		dv->sdev_path = NULL;
959 	}
960 
961 	if (!SDEV_IS_GLOBAL(dv))
962 		sdev_prof_free(dv);
963 
964 	mutex_destroy(&dv->sdev_lookup_lock);
965 	cv_destroy(&dv->sdev_lookup_cv);
966 
967 	/* return node to initial state as per constructor */
968 	(void) memset((void *)&dv->sdev_instance_data, 0,
969 	    sizeof (dv->sdev_instance_data));
970 	vn_invalid(SDEVTOV(dv));
971 	kmem_cache_free(sdev_node_cache, dv);
972 }
973 
974 /*
975  * DIRECTORY CACHE lookup
976  */
977 struct sdev_node *
978 sdev_findbyname(struct sdev_node *ddv, char *nm)
979 {
980 	struct sdev_node *dv;
981 	size_t	nmlen = strlen(nm);
982 
983 	ASSERT(RW_LOCK_HELD(&ddv->sdev_contents));
984 	for (dv = ddv->sdev_dot; dv; dv = dv->sdev_next) {
985 		if (dv->sdev_namelen != nmlen) {
986 			continue;
987 		}
988 
989 		/*
990 		 * Can't lookup stale nodes
991 		 */
992 		if (dv->sdev_flags & SDEV_STALE) {
993 			sdcmn_err9((
994 			    "sdev_findbyname: skipped stale node: %s\n",
995 			    dv->sdev_name));
996 			continue;
997 		}
998 
999 		if (strcmp(dv->sdev_name, nm) == 0) {
1000 			SDEV_HOLD(dv);
1001 			return (dv);
1002 		}
1003 	}
1004 	return (NULL);
1005 }
1006 
1007 /*
1008  * Inserts a new sdev_node in a parent directory
1009  */
1010 void
1011 sdev_direnter(struct sdev_node *ddv, struct sdev_node *dv)
1012 {
1013 	ASSERT(RW_WRITE_HELD(&ddv->sdev_contents));
1014 	ASSERT(SDEVTOV(ddv)->v_type == VDIR);
1015 	ASSERT(ddv->sdev_nlink >= 2);
1016 	ASSERT(dv->sdev_nlink == 0);
1017 
1018 	dv->sdev_dotdot = ddv;
1019 	dv->sdev_next = ddv->sdev_dot;
1020 	ddv->sdev_dot = dv;
1021 	ddv->sdev_nlink++;
1022 }
1023 
1024 /*
1025  * The following check is needed because while sdev_nodes are linked
1026  * in SDEV_INIT state, they have their link counts incremented only
1027  * in SDEV_READY state.
1028  */
1029 static void
1030 decr_link(struct sdev_node *dv)
1031 {
1032 	if (dv->sdev_state != SDEV_INIT)
1033 		dv->sdev_nlink--;
1034 	else
1035 		ASSERT(dv->sdev_nlink == 0);
1036 }
1037 
1038 /*
1039  * Delete an existing dv from directory cache
1040  *
1041  * In the case of a node is still held by non-zero reference count,
1042  *     the node is put into ZOMBIE state. Once the reference count
1043  *     reaches "0", the node is unlinked and destroyed,
1044  *     in sdev_inactive().
1045  */
1046 static int
1047 sdev_dirdelete(struct sdev_node *ddv, struct sdev_node *dv)
1048 {
1049 	struct sdev_node *idv;
1050 	struct sdev_node *prev = NULL;
1051 	struct vnode *vp;
1052 
1053 	ASSERT(RW_WRITE_HELD(&ddv->sdev_contents));
1054 
1055 	vp = SDEVTOV(dv);
1056 	mutex_enter(&vp->v_lock);
1057 
1058 	/* dv is held still */
1059 	if (vp->v_count > 1) {
1060 		rw_enter(&dv->sdev_contents, RW_WRITER);
1061 		if (dv->sdev_state == SDEV_READY) {
1062 			sdcmn_err9((
1063 			    "sdev_delete: node %s busy with count %d\n",
1064 			    dv->sdev_name, vp->v_count));
1065 			dv->sdev_state = SDEV_ZOMBIE;
1066 		}
1067 		rw_exit(&dv->sdev_contents);
1068 		--vp->v_count;
1069 		mutex_exit(&vp->v_lock);
1070 		return (EBUSY);
1071 	}
1072 	ASSERT(vp->v_count == 1);
1073 
1074 	/* unlink from the memory cache */
1075 	ddv->sdev_nlink--;	/* .. to above */
1076 	if (vp->v_type == VDIR) {
1077 		decr_link(dv);		/* . to self */
1078 	}
1079 
1080 	for (idv = ddv->sdev_dot; idv && idv != dv;
1081 	    prev = idv, idv = idv->sdev_next)
1082 		;
1083 	ASSERT(idv == dv);	/* node to be deleted must exist */
1084 	if (prev == NULL)
1085 		ddv->sdev_dot = dv->sdev_next;
1086 	else
1087 		prev->sdev_next = dv->sdev_next;
1088 	dv->sdev_next = NULL;
1089 	decr_link(dv);	/* name, back to zero */
1090 	vp->v_count--;
1091 	mutex_exit(&vp->v_lock);
1092 
1093 	/* destroy the node */
1094 	sdev_nodedestroy(dv, 0);
1095 	return (0);
1096 }
1097 
1098 /*
1099  * check if the source is in the path of the target
1100  *
1101  * source and target are different
1102  */
1103 /*ARGSUSED2*/
1104 static int
1105 sdev_checkpath(struct sdev_node *sdv, struct sdev_node *tdv, struct cred *cred)
1106 {
1107 	int error = 0;
1108 	struct sdev_node *dotdot, *dir;
1109 
1110 	dotdot = tdv->sdev_dotdot;
1111 	ASSERT(dotdot);
1112 
1113 	/* fs root */
1114 	if (dotdot == tdv) {
1115 		return (0);
1116 	}
1117 
1118 	for (;;) {
1119 		/*
1120 		 * avoid error cases like
1121 		 *	mv a a/b
1122 		 *	mv a a/b/c
1123 		 *	etc.
1124 		 */
1125 		if (dotdot == sdv) {
1126 			error = EINVAL;
1127 			break;
1128 		}
1129 
1130 		dir = dotdot;
1131 		dotdot = dir->sdev_dotdot;
1132 
1133 		/* done checking because root is reached */
1134 		if (dir == dotdot) {
1135 			break;
1136 		}
1137 	}
1138 	return (error);
1139 }
1140 
1141 int
1142 sdev_rnmnode(struct sdev_node *oddv, struct sdev_node *odv,
1143     struct sdev_node *nddv, struct sdev_node **ndvp, char *nnm,
1144     struct cred *cred)
1145 {
1146 	int error = 0;
1147 	struct vnode *ovp = SDEVTOV(odv);
1148 	struct vnode *nvp;
1149 	struct vattr vattr;
1150 	int doingdir = (ovp->v_type == VDIR);
1151 	char *link = NULL;
1152 	int samedir = (oddv == nddv) ? 1 : 0;
1153 	int bkstore = 0;
1154 	struct sdev_node *idv = NULL;
1155 	struct sdev_node *ndv = NULL;
1156 	timestruc_t now;
1157 
1158 	vattr.va_mask = AT_MODE|AT_UID|AT_GID;
1159 	error = VOP_GETATTR(ovp, &vattr, 0, cred);
1160 	if (error)
1161 		return (error);
1162 
1163 	if (!samedir)
1164 		rw_enter(&oddv->sdev_contents, RW_WRITER);
1165 	rw_enter(&nddv->sdev_contents, RW_WRITER);
1166 
1167 	/*
1168 	 * the source may have been deleted by another thread before
1169 	 * we gets here.
1170 	 */
1171 	if (odv->sdev_state != SDEV_READY) {
1172 		error = ENOENT;
1173 		goto err_out;
1174 	}
1175 
1176 	if (doingdir && (odv == nddv)) {
1177 		error = EINVAL;
1178 		goto err_out;
1179 	}
1180 
1181 	/*
1182 	 * If renaming a directory, and the parents are different (".." must be
1183 	 * changed) then the source dir must not be in the dir hierarchy above
1184 	 * the target since it would orphan everything below the source dir.
1185 	 */
1186 	if (doingdir && (oddv != nddv)) {
1187 		error = sdev_checkpath(odv, nddv, cred);
1188 		if (error)
1189 			goto err_out;
1190 	}
1191 
1192 	/* destination existing */
1193 	if (*ndvp) {
1194 		nvp = SDEVTOV(*ndvp);
1195 		ASSERT(nvp);
1196 
1197 		/* handling renaming to itself */
1198 		if (odv == *ndvp) {
1199 			error = 0;
1200 			goto err_out;
1201 		}
1202 
1203 		if (nvp->v_type == VDIR) {
1204 			if (!doingdir) {
1205 				error = EISDIR;
1206 				goto err_out;
1207 			}
1208 
1209 			if (vn_vfswlock(nvp)) {
1210 				error = EBUSY;
1211 				goto err_out;
1212 			}
1213 
1214 			if (vn_mountedvfs(nvp) != NULL) {
1215 				vn_vfsunlock(nvp);
1216 				error = EBUSY;
1217 				goto err_out;
1218 			}
1219 
1220 			/* in case dir1 exists in dir2 and "mv dir1 dir2" */
1221 			if ((*ndvp)->sdev_nlink > 2) {
1222 				vn_vfsunlock(nvp);
1223 				error = EEXIST;
1224 				goto err_out;
1225 			}
1226 			vn_vfsunlock(nvp);
1227 
1228 			(void) sdev_dirdelete(nddv, *ndvp);
1229 			*ndvp = NULL;
1230 			error = VOP_RMDIR(nddv->sdev_attrvp, nnm,
1231 				    nddv->sdev_attrvp, cred);
1232 			if (error)
1233 				goto err_out;
1234 		} else {
1235 			if (doingdir) {
1236 				error = ENOTDIR;
1237 				goto err_out;
1238 			}
1239 
1240 			if (SDEV_IS_PERSIST((*ndvp))) {
1241 				bkstore = 1;
1242 			}
1243 
1244 			/*
1245 			 * get rid of the node from the directory cache
1246 			 * note, in case EBUSY is returned, the ZOMBIE
1247 			 * node is taken care in sdev_mknode.
1248 			 */
1249 			(void) sdev_dirdelete(nddv, *ndvp);
1250 			*ndvp = NULL;
1251 			if (bkstore) {
1252 				error = VOP_REMOVE(nddv->sdev_attrvp,
1253 				    nnm, cred);
1254 				if (error)
1255 				    goto err_out;
1256 			}
1257 		}
1258 	}
1259 
1260 	/* fix the source for a symlink */
1261 	if (vattr.va_type == VLNK) {
1262 		if (odv->sdev_symlink == NULL) {
1263 			error = sdev_follow_link(odv);
1264 			if (error) {
1265 				error = ENOENT;
1266 				goto err_out;
1267 			}
1268 		}
1269 		ASSERT(odv->sdev_symlink);
1270 		link = i_ddi_strdup(odv->sdev_symlink, KM_SLEEP);
1271 	}
1272 
1273 	/*
1274 	 * make a fresh node from the source attrs
1275 	 */
1276 	ASSERT(RW_WRITE_HELD(&nddv->sdev_contents));
1277 	error = sdev_mknode(nddv, nnm, ndvp, &vattr,
1278 	    NULL, (void *)link, cred, SDEV_READY);
1279 
1280 	if (link)
1281 		kmem_free(link, strlen(link) + 1);
1282 
1283 	if (error)
1284 		goto err_out;
1285 	ASSERT(*ndvp);
1286 	ASSERT((*ndvp)->sdev_state == SDEV_READY);
1287 
1288 	/* move dir contents */
1289 	if (doingdir) {
1290 		for (idv = odv->sdev_dot; idv; idv = idv->sdev_next) {
1291 			error = sdev_rnmnode(odv, idv,
1292 			    (struct sdev_node *)(*ndvp), &ndv,
1293 			    idv->sdev_name, cred);
1294 
1295 			if (error)
1296 				goto err_out;
1297 			ndv = NULL;
1298 		}
1299 
1300 	}
1301 
1302 	if ((*ndvp)->sdev_attrvp) {
1303 		sdev_update_timestamps((*ndvp)->sdev_attrvp, kcred,
1304 		    AT_CTIME|AT_ATIME);
1305 	} else {
1306 		ASSERT((*ndvp)->sdev_attr);
1307 		gethrestime(&now);
1308 		(*ndvp)->sdev_attr->va_ctime = now;
1309 		(*ndvp)->sdev_attr->va_atime = now;
1310 	}
1311 
1312 	if (nddv->sdev_attrvp) {
1313 		sdev_update_timestamps(nddv->sdev_attrvp, kcred,
1314 		    AT_MTIME|AT_ATIME);
1315 	} else {
1316 		ASSERT(nddv->sdev_attr);
1317 		gethrestime(&now);
1318 		nddv->sdev_attr->va_mtime = now;
1319 		nddv->sdev_attr->va_atime = now;
1320 	}
1321 	rw_exit(&nddv->sdev_contents);
1322 	if (!samedir)
1323 		rw_exit(&oddv->sdev_contents);
1324 
1325 	SDEV_RELE(*ndvp);
1326 	return (error);
1327 
1328 err_out:
1329 	rw_exit(&nddv->sdev_contents);
1330 	if (!samedir)
1331 		rw_exit(&oddv->sdev_contents);
1332 	return (error);
1333 }
1334 
1335 /*
1336  * Merge sdev_node specific information into an attribute structure.
1337  *
1338  * note: sdev_node is not locked here
1339  */
1340 void
1341 sdev_vattr_merge(struct sdev_node *dv, struct vattr *vap)
1342 {
1343 	struct vnode *vp = SDEVTOV(dv);
1344 
1345 	vap->va_nlink = dv->sdev_nlink;
1346 	vap->va_nodeid = dv->sdev_ino;
1347 	vap->va_fsid = SDEVTOV(dv->sdev_dotdot)->v_rdev;
1348 	vap->va_type = vp->v_type;
1349 
1350 	if (vp->v_type == VDIR) {
1351 		vap->va_rdev = 0;
1352 		vap->va_fsid = vp->v_rdev;
1353 	} else if (vp->v_type == VLNK) {
1354 		vap->va_rdev = 0;
1355 		vap->va_mode  &= ~S_IFMT;
1356 		vap->va_mode |= S_IFLNK;
1357 	} else if ((vp->v_type == VCHR) || (vp->v_type == VBLK)) {
1358 		vap->va_rdev = vp->v_rdev;
1359 		vap->va_mode &= ~S_IFMT;
1360 		if (vap->va_type == VCHR)
1361 			vap->va_mode |= S_IFCHR;
1362 		else
1363 			vap->va_mode |= S_IFBLK;
1364 	} else {
1365 		vap->va_rdev = 0;
1366 	}
1367 }
1368 
1369 static struct vattr *
1370 sdev_getdefault_attr(enum vtype type)
1371 {
1372 	if (type == VDIR)
1373 		return (&sdev_vattr_dir);
1374 	else if (type == VCHR)
1375 		return (&sdev_vattr_chr);
1376 	else if (type == VBLK)
1377 		return (&sdev_vattr_blk);
1378 	else if (type == VLNK)
1379 		return (&sdev_vattr_lnk);
1380 	else
1381 		return (NULL);
1382 }
1383 int
1384 sdev_to_vp(struct sdev_node *dv, struct vnode **vpp)
1385 {
1386 	int rv = 0;
1387 	struct vnode *vp = SDEVTOV(dv);
1388 
1389 	switch (vp->v_type) {
1390 	case VCHR:
1391 	case VBLK:
1392 		/*
1393 		 * If vnode is a device, return special vnode instead
1394 		 * (though it knows all about -us- via sp->s_realvp)
1395 		 */
1396 		*vpp = specvp(vp, vp->v_rdev, vp->v_type, kcred);
1397 		VN_RELE(vp);
1398 		if (*vpp == NULLVP)
1399 			rv = ENOSYS;
1400 		break;
1401 	default:	/* most types are returned as is */
1402 		*vpp = vp;
1403 		break;
1404 	}
1405 	return (rv);
1406 }
1407 
1408 /*
1409  * loopback into sdev_lookup()
1410  */
1411 static struct vnode *
1412 devname_find_by_devpath(char *devpath, struct vattr *vattr)
1413 {
1414 	int error = 0;
1415 	struct vnode *vp;
1416 
1417 	error = lookupname(devpath, UIO_SYSSPACE, NO_FOLLOW, NULLVPP, &vp);
1418 	if (error) {
1419 		return (NULL);
1420 	}
1421 
1422 	if (vattr)
1423 		(void) VOP_GETATTR(vp, vattr, 0, kcred);
1424 	return (vp);
1425 }
1426 
1427 /*
1428  * the junction between devname and devfs
1429  */
1430 static struct vnode *
1431 devname_configure_by_path(char *physpath, struct vattr *vattr)
1432 {
1433 	int error = 0;
1434 	struct vnode *vp;
1435 
1436 	ASSERT(strncmp(physpath, "/devices/", sizeof ("/devices/") - 1)
1437 	    == 0);
1438 
1439 	error = devfs_lookupname(physpath + sizeof ("/devices/") - 1,
1440 	    NULLVPP, &vp);
1441 	if (error != 0) {
1442 		if (error == ENODEV) {
1443 			cmn_err(CE_CONT, "%s: not found (line %d)\n",
1444 			    physpath, __LINE__);
1445 		}
1446 
1447 		return (NULL);
1448 	}
1449 
1450 	if (vattr)
1451 		(void) VOP_GETATTR(vp, vattr, 0, kcred);
1452 	return (vp);
1453 }
1454 
1455 /*
1456  * junction between devname and root file system, e.g. ufs
1457  */
1458 int
1459 devname_backstore_lookup(struct sdev_node *ddv, char *nm, struct vnode **rvp)
1460 {
1461 	struct vnode *rdvp = ddv->sdev_attrvp;
1462 	int rval = 0;
1463 
1464 	ASSERT(rdvp);
1465 
1466 	rval = VOP_LOOKUP(rdvp, nm, rvp, NULL, 0, NULL, kcred);
1467 	return (rval);
1468 }
1469 
1470 static int
1471 sdev_filldir_from_store(struct sdev_node *ddv, int dlen, struct cred *cred)
1472 {
1473 	struct sdev_node *dv = NULL;
1474 	char	*nm;
1475 	struct vnode *dirvp;
1476 	int	error;
1477 	vnode_t	*vp;
1478 	int eof;
1479 	struct iovec iov;
1480 	struct uio uio;
1481 	struct dirent64 *dp;
1482 	dirent64_t *dbuf;
1483 	size_t dbuflen;
1484 	struct vattr vattr;
1485 	char *link = NULL;
1486 
1487 	if (ddv->sdev_attrvp == NULL)
1488 		return (0);
1489 	if (!(ddv->sdev_flags & SDEV_BUILD))
1490 		return (0);
1491 
1492 	dirvp = ddv->sdev_attrvp;
1493 	VN_HOLD(dirvp);
1494 	dbuf = kmem_zalloc(dlen, KM_SLEEP);
1495 
1496 	uio.uio_iov = &iov;
1497 	uio.uio_iovcnt = 1;
1498 	uio.uio_segflg = UIO_SYSSPACE;
1499 	uio.uio_fmode = 0;
1500 	uio.uio_extflg = UIO_COPY_CACHED;
1501 	uio.uio_loffset = 0;
1502 	uio.uio_llimit = MAXOFFSET_T;
1503 
1504 	eof = 0;
1505 	error = 0;
1506 	while (!error && !eof) {
1507 		uio.uio_resid = dlen;
1508 		iov.iov_base = (char *)dbuf;
1509 		iov.iov_len = dlen;
1510 		(void) VOP_RWLOCK(dirvp, V_WRITELOCK_FALSE, NULL);
1511 		error = VOP_READDIR(dirvp, &uio, kcred, &eof);
1512 		VOP_RWUNLOCK(dirvp, V_WRITELOCK_FALSE, NULL);
1513 
1514 		dbuflen = dlen - uio.uio_resid;
1515 		if (error || dbuflen == 0)
1516 			break;
1517 
1518 		if (!(ddv->sdev_flags & SDEV_BUILD)) {
1519 			error = 0;
1520 			break;
1521 		}
1522 
1523 		for (dp = dbuf; ((intptr_t)dp <
1524 		    (intptr_t)dbuf + dbuflen);
1525 		    dp = (dirent64_t *)((intptr_t)dp + dp->d_reclen)) {
1526 			nm = dp->d_name;
1527 
1528 			if (strcmp(nm, ".") == 0 ||
1529 			    strcmp(nm, "..") == 0)
1530 				continue;
1531 
1532 			vp = NULLVP;
1533 			dv = sdev_cache_lookup(ddv, nm);
1534 			if (dv) {
1535 				if (dv->sdev_state != SDEV_ZOMBIE) {
1536 					SDEV_SIMPLE_RELE(dv);
1537 				} else {
1538 					/*
1539 					 * A ZOMBIE node may not have been
1540 					 * cleaned up from the backing store,
1541 					 * bypass this entry in this case,
1542 					 * and clean it up from the directory
1543 					 * cache if this is the last call.
1544 					 */
1545 					(void) sdev_dirdelete(ddv, dv);
1546 				}
1547 				continue;
1548 			}
1549 
1550 			/* refill the cache if not already */
1551 			error = devname_backstore_lookup(ddv, nm, &vp);
1552 			if (error)
1553 				continue;
1554 
1555 			vattr.va_mask = AT_MODE|AT_UID|AT_GID;
1556 			error = VOP_GETATTR(vp, &vattr, 0, cred);
1557 			if (error)
1558 				continue;
1559 
1560 			if (vattr.va_type == VLNK) {
1561 				error = sdev_getlink(vp, &link);
1562 				if (error) {
1563 					continue;
1564 				}
1565 				ASSERT(link != NULL);
1566 			}
1567 
1568 			if (!rw_tryupgrade(&ddv->sdev_contents)) {
1569 				rw_exit(&ddv->sdev_contents);
1570 				rw_enter(&ddv->sdev_contents, RW_WRITER);
1571 			}
1572 			error = sdev_mknode(ddv, nm, &dv, &vattr, vp, link,
1573 			    cred, SDEV_READY);
1574 			rw_downgrade(&ddv->sdev_contents);
1575 
1576 			if (link != NULL) {
1577 				kmem_free(link, strlen(link) + 1);
1578 				link = NULL;
1579 			}
1580 
1581 			if (!error) {
1582 				ASSERT(dv);
1583 				ASSERT(dv->sdev_state != SDEV_ZOMBIE);
1584 				SDEV_SIMPLE_RELE(dv);
1585 			}
1586 			vp = NULL;
1587 			dv = NULL;
1588 		}
1589 	}
1590 
1591 done:
1592 	VN_RELE(dirvp);
1593 	kmem_free(dbuf, dlen);
1594 
1595 	return (error);
1596 }
1597 
1598 static int
1599 sdev_filldir_dynamic(struct sdev_node *ddv)
1600 {
1601 	int error;
1602 	int i;
1603 	struct vattr *vap;
1604 	char *nm = NULL;
1605 	struct sdev_node *dv = NULL;
1606 
1607 	if (!(ddv->sdev_flags & SDEV_BUILD)) {
1608 		return (0);
1609 	}
1610 
1611 	ASSERT(RW_READ_HELD(&ddv->sdev_contents));
1612 	if (!rw_tryupgrade(&ddv->sdev_contents)) {
1613 		rw_exit(&ddv->sdev_contents);
1614 		rw_enter(&ddv->sdev_contents, RW_WRITER);
1615 	}
1616 
1617 	vap = sdev_getdefault_attr(VDIR);
1618 	for (i = 0; vtab[i].vt_name != NULL; i++) {
1619 		nm = vtab[i].vt_name;
1620 		ASSERT(RW_WRITE_HELD(&ddv->sdev_contents));
1621 		error = sdev_mknode(ddv, nm, &dv, vap, NULL,
1622 		    NULL, kcred, SDEV_READY);
1623 		if (error)
1624 			continue;
1625 		ASSERT(dv);
1626 		ASSERT(dv->sdev_state != SDEV_ZOMBIE);
1627 		SDEV_SIMPLE_RELE(dv);
1628 		dv = NULL;
1629 	}
1630 	rw_downgrade(&ddv->sdev_contents);
1631 	return (0);
1632 }
1633 
1634 /*
1635  * Creating a backing store entry based on sdev_attr.
1636  * This is called either as part of node creation in a persistent directory
1637  * or from setattr/setsecattr to persist access attributes across reboot.
1638  */
1639 int
1640 sdev_shadow_node(struct sdev_node *dv, struct cred *cred)
1641 {
1642 	int error = 0;
1643 	struct vnode *dvp = SDEVTOV(dv->sdev_dotdot);
1644 	struct vnode *rdvp = VTOSDEV(dvp)->sdev_attrvp;
1645 	struct vattr *vap = dv->sdev_attr;
1646 	char *nm = dv->sdev_name;
1647 	struct vnode *tmpvp, **rvp = &tmpvp, *rrvp = NULL;
1648 
1649 	ASSERT(dv && dv->sdev_name && rdvp);
1650 	ASSERT(RW_WRITE_HELD(&dv->sdev_contents) && dv->sdev_attrvp == NULL);
1651 
1652 lookup:
1653 	/* try to find it in the backing store */
1654 	error = VOP_LOOKUP(rdvp, nm, rvp, NULL, 0, NULL, cred);
1655 	if (error == 0) {
1656 		if (VOP_REALVP(*rvp, &rrvp) == 0) {
1657 			VN_HOLD(rrvp);
1658 			VN_RELE(*rvp);
1659 			*rvp = rrvp;
1660 		}
1661 
1662 		kmem_free(dv->sdev_attr, sizeof (vattr_t));
1663 		dv->sdev_attr = NULL;
1664 		dv->sdev_attrvp = *rvp;
1665 		return (0);
1666 	}
1667 
1668 	/* let's try to persist the node */
1669 	gethrestime(&vap->va_atime);
1670 	vap->va_mtime = vap->va_atime;
1671 	vap->va_ctime = vap->va_atime;
1672 	vap->va_mask |= AT_TYPE|AT_MODE;
1673 	switch (vap->va_type) {
1674 	case VDIR:
1675 		error = VOP_MKDIR(rdvp, nm, vap, rvp, cred);
1676 		sdcmn_err9(("sdev_shadow_node: mkdir vp %p error %d\n",
1677 		    (void *)(*rvp), error));
1678 		break;
1679 	case VCHR:
1680 	case VBLK:
1681 	case VREG:
1682 	case VDOOR:
1683 		error = VOP_CREATE(rdvp, nm, vap, NONEXCL, VREAD|VWRITE,
1684 		    rvp, cred, 0);
1685 		sdcmn_err9(("sdev_shadow_node: create vp %p, error %d\n",
1686 		    (void *)(*rvp), error));
1687 		if (!error)
1688 			VN_RELE(*rvp);
1689 		break;
1690 	case VLNK:
1691 		ASSERT(dv->sdev_symlink);
1692 		error = VOP_SYMLINK(rdvp, nm, vap, dv->sdev_symlink, cred);
1693 		sdcmn_err9(("sdev_shadow_node: create symlink error %d\n",
1694 		    error));
1695 		break;
1696 	default:
1697 		cmn_err(CE_PANIC, "dev: %s: sdev_shadow_node "
1698 		    "create\n", nm);
1699 		/*NOTREACHED*/
1700 	}
1701 
1702 	/* go back to lookup to factor out spec node and set attrvp */
1703 	if (error == 0)
1704 		goto lookup;
1705 
1706 	return (error);
1707 }
1708 
1709 static int
1710 sdev_cache_add(struct sdev_node *ddv, struct sdev_node **dv, char *nm)
1711 {
1712 	int error = 0;
1713 	struct sdev_node *dup = NULL;
1714 
1715 	ASSERT(RW_WRITE_HELD(&ddv->sdev_contents));
1716 	if ((dup = sdev_findbyname(ddv, nm)) == NULL) {
1717 		sdev_direnter(ddv, *dv);
1718 	} else {
1719 		if (dup->sdev_state == SDEV_ZOMBIE) {
1720 			error = sdev_dirdelete(ddv, dup);
1721 			/*
1722 			 * The ZOMBIE node is still hanging
1723 			 * around with more than one reference counts.
1724 			 * Fail the new node creation so that
1725 			 * the directory cache won't have
1726 			 * duplicate entries for the same named node
1727 			 */
1728 			if (error == EBUSY) {
1729 				SDEV_SIMPLE_RELE(*dv);
1730 				sdev_nodedestroy(*dv, 0);
1731 				*dv = NULL;
1732 				return (error);
1733 			}
1734 			sdev_direnter(ddv, *dv);
1735 		} else {
1736 			ASSERT((*dv)->sdev_state != SDEV_ZOMBIE);
1737 			SDEV_SIMPLE_RELE(*dv);
1738 			sdev_nodedestroy(*dv, 0);
1739 			*dv = dup;
1740 		}
1741 	}
1742 
1743 	return (0);
1744 }
1745 
1746 static int
1747 sdev_cache_delete(struct sdev_node *ddv, struct sdev_node **dv)
1748 {
1749 	ASSERT(RW_WRITE_HELD(&ddv->sdev_contents));
1750 	return (sdev_dirdelete(ddv, *dv));
1751 }
1752 
1753 /*
1754  * update the in-core directory cache
1755  */
1756 int
1757 sdev_cache_update(struct sdev_node *ddv, struct sdev_node **dv, char *nm,
1758     sdev_cache_ops_t ops)
1759 {
1760 	int error = 0;
1761 
1762 	ASSERT((SDEV_HELD(*dv)));
1763 
1764 	ASSERT(RW_WRITE_HELD(&ddv->sdev_contents));
1765 	switch (ops) {
1766 	case SDEV_CACHE_ADD:
1767 		error = sdev_cache_add(ddv, dv, nm);
1768 		break;
1769 	case SDEV_CACHE_DELETE:
1770 		error = sdev_cache_delete(ddv, dv);
1771 		break;
1772 	default:
1773 		break;
1774 	}
1775 
1776 	return (error);
1777 }
1778 
1779 /*
1780  * retrive the named entry from the directory cache
1781  */
1782 struct sdev_node *
1783 sdev_cache_lookup(struct sdev_node *ddv, char *nm)
1784 {
1785 	struct sdev_node *dv = NULL;
1786 
1787 	ASSERT(RW_LOCK_HELD(&ddv->sdev_contents));
1788 	dv = sdev_findbyname(ddv, nm);
1789 
1790 	return (dv);
1791 }
1792 
1793 /*
1794  * Implicit reconfig for nodes constructed by a link generator
1795  * Start devfsadm if needed, or if devfsadm is in progress,
1796  * prepare to block on devfsadm either completing or
1797  * constructing the desired node.  As devfsadmd is global
1798  * in scope, constructing all necessary nodes, we only
1799  * need to initiate it once.
1800  */
1801 static int
1802 sdev_call_devfsadmd(struct sdev_node *ddv, struct sdev_node *dv, char *nm)
1803 {
1804 	int error = 0;
1805 
1806 	if (DEVNAME_DEVFSADM_IS_RUNNING(devfsadm_state)) {
1807 		sdcmn_err6(("lookup: waiting for %s/%s, 0x%x\n",
1808 		    ddv->sdev_name, nm, devfsadm_state));
1809 		mutex_enter(&dv->sdev_lookup_lock);
1810 		SDEV_BLOCK_OTHERS(dv, (SDEV_LOOKUP | SDEV_LGWAITING));
1811 		mutex_exit(&dv->sdev_lookup_lock);
1812 		error = 0;
1813 	} else if (!DEVNAME_DEVFSADM_HAS_RUN(devfsadm_state)) {
1814 		sdcmn_err6(("lookup %s/%s starting devfsadm, 0x%x\n",
1815 			ddv->sdev_name, nm, devfsadm_state));
1816 
1817 		sdev_devfsadmd_thread(ddv, dv, kcred);
1818 		mutex_enter(&dv->sdev_lookup_lock);
1819 		SDEV_BLOCK_OTHERS(dv,
1820 		    (SDEV_LOOKUP | SDEV_LGWAITING));
1821 		mutex_exit(&dv->sdev_lookup_lock);
1822 		error = 0;
1823 	} else {
1824 		error = -1;
1825 	}
1826 
1827 	return (error);
1828 }
1829 
1830 static int
1831 sdev_call_modulelookup(struct sdev_node *ddv, struct sdev_node **dvp, char *nm,
1832     int (*fn)(char *, devname_handle_t *, struct cred *), struct cred *cred)
1833 {
1834 	struct vnode *rvp = NULL;
1835 	int error = 0;
1836 	struct vattr *vap;
1837 	devname_spec_t spec;
1838 	devname_handle_t *hdl;
1839 	void *args = NULL;
1840 	struct sdev_node *dv = *dvp;
1841 
1842 	ASSERT(dv && ddv);
1843 	hdl = &(dv->sdev_handle);
1844 	ASSERT(hdl->dh_data == dv);
1845 	mutex_enter(&dv->sdev_lookup_lock);
1846 	SDEV_BLOCK_OTHERS(dv, SDEV_LOOKUP);
1847 	mutex_exit(&dv->sdev_lookup_lock);
1848 	error = (*fn)(nm, hdl, cred);
1849 	if (error) {
1850 		return (error);
1851 	}
1852 
1853 	spec = hdl->dh_spec;
1854 	args = hdl->dh_args;
1855 	ASSERT(args);
1856 
1857 	switch (spec) {
1858 	case DEVNAME_NS_PATH:
1859 		/*
1860 		 * symlink of:
1861 		 *	/dev/dir/nm -> /device/...
1862 		 */
1863 		rvp = devname_configure_by_path((char *)args, NULL);
1864 		break;
1865 	case DEVNAME_NS_DEV:
1866 		/*
1867 		 * symlink of:
1868 		 *	/dev/dir/nm -> /dev/...
1869 		 */
1870 		rvp = devname_find_by_devpath((char *)args, NULL);
1871 		break;
1872 	default:
1873 		if (args)
1874 			kmem_free((char *)args, strlen(args) + 1);
1875 		return (ENOENT);
1876 
1877 	}
1878 
1879 	if (rvp == NULL) {
1880 		if (args)
1881 			kmem_free((char *)args, strlen(args) + 1);
1882 		return (ENOENT);
1883 	} else {
1884 		vap = sdev_getdefault_attr(VLNK);
1885 		ASSERT(RW_READ_HELD(&ddv->sdev_contents));
1886 		/*
1887 		 * Could sdev_mknode return a different dv_node
1888 		 * once the lock is dropped?
1889 		 */
1890 		if (!rw_tryupgrade(&ddv->sdev_contents)) {
1891 			rw_exit(&ddv->sdev_contents);
1892 			rw_enter(&ddv->sdev_contents, RW_WRITER);
1893 		}
1894 		error = sdev_mknode(ddv, nm, &dv, vap, NULL, args, cred,
1895 		    SDEV_READY);
1896 		rw_downgrade(&ddv->sdev_contents);
1897 		if (error) {
1898 			if (args)
1899 				kmem_free((char *)args, strlen(args) + 1);
1900 			return (error);
1901 		} else {
1902 			mutex_enter(&dv->sdev_lookup_lock);
1903 			SDEV_UNBLOCK_OTHERS(dv, SDEV_LOOKUP);
1904 			mutex_exit(&dv->sdev_lookup_lock);
1905 			error = 0;
1906 		}
1907 	}
1908 
1909 	if (args)
1910 		kmem_free((char *)args, strlen(args) + 1);
1911 
1912 	*dvp = dv;
1913 	return (0);
1914 }
1915 
1916 /*
1917  *  Support for specialized device naming construction mechanisms
1918  */
1919 static int
1920 sdev_call_dircallback(struct sdev_node *ddv, struct sdev_node **dvp, char *nm,
1921     int (*callback)(struct sdev_node *, char *, void **, struct cred *,
1922     void *, char *), int flags, struct cred *cred)
1923 {
1924 	int rv = 0;
1925 	char *physpath = NULL;
1926 	struct vnode *rvp = NULL;
1927 	struct vattr vattr;
1928 	struct vattr *vap;
1929 	struct sdev_node *dv = *dvp;
1930 
1931 	mutex_enter(&dv->sdev_lookup_lock);
1932 	SDEV_BLOCK_OTHERS(dv, SDEV_LOOKUP);
1933 	mutex_exit(&dv->sdev_lookup_lock);
1934 
1935 	/* for non-devfsadm devices */
1936 	if (flags & SDEV_PATH) {
1937 		physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
1938 		rv = callback(ddv, nm, (void *)&physpath, kcred, NULL,
1939 		    NULL);
1940 		if (rv) {
1941 			kmem_free(physpath, MAXPATHLEN);
1942 			return (-1);
1943 		}
1944 
1945 		ASSERT(physpath);
1946 		rvp = devname_configure_by_path(physpath, NULL);
1947 		if (rvp == NULL) {
1948 			sdcmn_err3(("devname_configure_by_path: "
1949 			    "failed for /dev/%s/%s\n",
1950 			    ddv->sdev_name, nm));
1951 			kmem_free(physpath, MAXPATHLEN);
1952 			rv = -1;
1953 		} else {
1954 			vap = sdev_getdefault_attr(VLNK);
1955 			ASSERT(RW_READ_HELD(&ddv->sdev_contents));
1956 
1957 			/*
1958 			 * Sdev_mknode may return back a different sdev_node
1959 			 * that was created by another thread that
1960 			 * raced to the directroy cache before this thread.
1961 			 *
1962 			 * With current directory cache mechanism
1963 			 * (linked list with the sdev_node name as
1964 			 * the entity key), this is a way to make sure
1965 			 * only one entry exists for the same name
1966 			 * in the same directory. The outcome is
1967 			 * the winner wins.
1968 			 */
1969 			if (!rw_tryupgrade(&ddv->sdev_contents)) {
1970 				rw_exit(&ddv->sdev_contents);
1971 				rw_enter(&ddv->sdev_contents, RW_WRITER);
1972 			}
1973 			rv = sdev_mknode(ddv, nm, &dv, vap, NULL,
1974 			    (void *)physpath, cred, SDEV_READY);
1975 			rw_downgrade(&ddv->sdev_contents);
1976 			kmem_free(physpath, MAXPATHLEN);
1977 			if (rv) {
1978 				return (rv);
1979 			} else {
1980 				mutex_enter(&dv->sdev_lookup_lock);
1981 				SDEV_UNBLOCK_OTHERS(dv, SDEV_LOOKUP);
1982 				mutex_exit(&dv->sdev_lookup_lock);
1983 				return (0);
1984 			}
1985 		}
1986 	} else if (flags & SDEV_VNODE) {
1987 		/*
1988 		 * DBNR has its own way to create the device
1989 		 * and return a backing store vnode in rvp
1990 		 */
1991 		ASSERT(callback);
1992 		rv = callback(ddv, nm, (void *)&rvp, kcred, NULL, NULL);
1993 		if (rv || (rvp == NULL)) {
1994 			sdcmn_err3(("devname_lookup_func: SDEV_VNODE "
1995 			    "callback failed \n"));
1996 			return (-1);
1997 		}
1998 		vap = sdev_getdefault_attr(rvp->v_type);
1999 		if (vap == NULL)
2000 			return (-1);
2001 
2002 		ASSERT(RW_READ_HELD(&ddv->sdev_contents));
2003 		if (!rw_tryupgrade(&ddv->sdev_contents)) {
2004 			rw_exit(&ddv->sdev_contents);
2005 			rw_enter(&ddv->sdev_contents, RW_WRITER);
2006 		}
2007 		rv = sdev_mknode(ddv, nm, &dv, vap, rvp, NULL,
2008 		    cred, SDEV_READY);
2009 		rw_downgrade(&ddv->sdev_contents);
2010 		if (rv)
2011 			return (rv);
2012 
2013 		mutex_enter(&dv->sdev_lookup_lock);
2014 		SDEV_UNBLOCK_OTHERS(dv, SDEV_LOOKUP);
2015 		mutex_exit(&dv->sdev_lookup_lock);
2016 		return (0);
2017 	} else if (flags & SDEV_VATTR) {
2018 		/*
2019 		 * /dev/pts
2020 		 *
2021 		 * DBNR has its own way to create the device
2022 		 * "0" is returned upon success.
2023 		 *
2024 		 * callback is responsible to set the basic attributes,
2025 		 * e.g. va_type/va_uid/va_gid/
2026 		 *    dev_t if VCHR or VBLK/
2027 		 */
2028 		ASSERT(callback);
2029 		rv = callback(ddv, nm, (void *)&vattr, kcred, NULL, NULL);
2030 		if (rv) {
2031 			sdcmn_err3(("devname_lookup_func: SDEV_NONE "
2032 			    "callback failed \n"));
2033 			return (-1);
2034 		}
2035 
2036 		ASSERT(RW_READ_HELD(&ddv->sdev_contents));
2037 		if (!rw_tryupgrade(&ddv->sdev_contents)) {
2038 			rw_exit(&ddv->sdev_contents);
2039 			rw_enter(&ddv->sdev_contents, RW_WRITER);
2040 		}
2041 		rv = sdev_mknode(ddv, nm, &dv, &vattr, NULL, NULL,
2042 		    cred, SDEV_READY);
2043 		rw_downgrade(&ddv->sdev_contents);
2044 
2045 		if (rv)
2046 			return (rv);
2047 
2048 		mutex_enter(&dv->sdev_lookup_lock);
2049 		SDEV_UNBLOCK_OTHERS(dv, SDEV_LOOKUP);
2050 		mutex_exit(&dv->sdev_lookup_lock);
2051 		return (0);
2052 	} else {
2053 		impossible(("lookup: %s/%s by %s not supported (%d)\n",
2054 		    SDEVTOV(ddv)->v_path, nm, curproc->p_user.u_comm,
2055 		    __LINE__));
2056 		rv = -1;
2057 	}
2058 
2059 	*dvp = dv;
2060 	return (rv);
2061 }
2062 
2063 static int
2064 is_devfsadm_thread(char *exec_name)
2065 {
2066 	/*
2067 	 * note: because devfsadmd -> /usr/sbin/devfsadm
2068 	 * it is safe to use "devfsadm" to capture the lookups
2069 	 * from devfsadm and its daemon version.
2070 	 */
2071 	if (strcmp(exec_name, "devfsadm") == 0)
2072 		return (1);
2073 	return (0);
2074 }
2075 
2076 
2077 /*
2078  * Lookup Order:
2079  *	sdev_node cache;
2080  *	backing store (SDEV_PERSIST);
2081  *	DBNR: a. dir_ops implemented in the loadable modules;
2082  *	      b. vnode ops in vtab.
2083  */
2084 int
2085 devname_lookup_func(struct sdev_node *ddv, char *nm, struct vnode **vpp,
2086     struct cred *cred, int (*callback)(struct sdev_node *, char *, void **,
2087     struct cred *, void *, char *), int flags)
2088 {
2089 	int rv = 0, nmlen;
2090 	struct vnode *rvp = NULL;
2091 	struct sdev_node *dv = NULL;
2092 	int	retried = 0;
2093 	int	error = 0;
2094 	struct devname_nsmap *map = NULL;
2095 	struct devname_ops *dirops = NULL;
2096 	int (*fn)(char *, devname_handle_t *, struct cred *) = NULL;
2097 	struct vattr vattr;
2098 	char *lookup_thread = curproc->p_user.u_comm;
2099 	int failed_flags = 0;
2100 	int (*vtor)(struct sdev_node *) = NULL;
2101 	int state;
2102 	int parent_state;
2103 	char *link = NULL;
2104 
2105 	if (SDEVTOV(ddv)->v_type != VDIR)
2106 		return (ENOTDIR);
2107 
2108 	/*
2109 	 * Empty name or ., return node itself.
2110 	 */
2111 	nmlen = strlen(nm);
2112 	if ((nmlen == 0) || ((nmlen == 1) && (nm[0] == '.'))) {
2113 		*vpp = SDEVTOV(ddv);
2114 		VN_HOLD(*vpp);
2115 		return (0);
2116 	}
2117 
2118 	/*
2119 	 * .., return the parent directory
2120 	 */
2121 	if ((nmlen == 2) && (strcmp(nm, "..") == 0)) {
2122 		*vpp = SDEVTOV(ddv->sdev_dotdot);
2123 		VN_HOLD(*vpp);
2124 		return (0);
2125 	}
2126 
2127 	rw_enter(&ddv->sdev_contents, RW_READER);
2128 	if (ddv->sdev_flags & SDEV_VTOR) {
2129 		vtor = (int (*)(struct sdev_node *))sdev_get_vtor(ddv);
2130 		ASSERT(vtor);
2131 	}
2132 
2133 tryagain:
2134 	/*
2135 	 * (a) directory cache lookup:
2136 	 */
2137 	ASSERT(RW_READ_HELD(&ddv->sdev_contents));
2138 	parent_state = ddv->sdev_state;
2139 	dv = sdev_cache_lookup(ddv, nm);
2140 	if (dv) {
2141 		state = dv->sdev_state;
2142 		switch (state) {
2143 		case SDEV_INIT:
2144 			if (is_devfsadm_thread(lookup_thread))
2145 				break;
2146 
2147 			/* ZOMBIED parent won't allow node creation */
2148 			if (parent_state == SDEV_ZOMBIE) {
2149 				SD_TRACE_FAILED_LOOKUP(ddv, nm,
2150 				    retried);
2151 				goto nolock_notfound;
2152 			}
2153 
2154 			mutex_enter(&dv->sdev_lookup_lock);
2155 			/* compensate the threads started after devfsadm */
2156 			if (DEVNAME_DEVFSADM_IS_RUNNING(devfsadm_state) &&
2157 			    !(SDEV_IS_LOOKUP(dv)))
2158 				SDEV_BLOCK_OTHERS(dv,
2159 				    (SDEV_LOOKUP | SDEV_LGWAITING));
2160 
2161 			if (SDEV_IS_LOOKUP(dv)) {
2162 				failed_flags |= SLF_REBUILT;
2163 				rw_exit(&ddv->sdev_contents);
2164 				error = sdev_wait4lookup(dv, SDEV_LOOKUP);
2165 				mutex_exit(&dv->sdev_lookup_lock);
2166 				rw_enter(&ddv->sdev_contents, RW_READER);
2167 
2168 				if (error != 0) {
2169 					SD_TRACE_FAILED_LOOKUP(ddv, nm,
2170 					    retried);
2171 					goto nolock_notfound;
2172 				}
2173 
2174 				state = dv->sdev_state;
2175 				if (state == SDEV_INIT) {
2176 					SD_TRACE_FAILED_LOOKUP(ddv, nm,
2177 					    retried);
2178 					goto nolock_notfound;
2179 				} else if (state == SDEV_READY) {
2180 					goto found;
2181 				} else if (state == SDEV_ZOMBIE) {
2182 					rw_exit(&ddv->sdev_contents);
2183 					SD_TRACE_FAILED_LOOKUP(ddv, nm,
2184 					    retried);
2185 					SDEV_RELE(dv);
2186 					goto lookup_failed;
2187 				}
2188 			} else {
2189 				mutex_exit(&dv->sdev_lookup_lock);
2190 			}
2191 			break;
2192 		case SDEV_READY:
2193 			goto found;
2194 		case SDEV_ZOMBIE:
2195 			rw_exit(&ddv->sdev_contents);
2196 			SD_TRACE_FAILED_LOOKUP(ddv, nm, retried);
2197 			SDEV_RELE(dv);
2198 			goto lookup_failed;
2199 		default:
2200 			rw_exit(&ddv->sdev_contents);
2201 			SD_TRACE_FAILED_LOOKUP(ddv, nm, retried);
2202 			sdev_lookup_failed(ddv, nm, failed_flags);
2203 			*vpp = NULLVP;
2204 			return (ENOENT);
2205 		}
2206 	}
2207 	ASSERT(RW_READ_HELD(&ddv->sdev_contents));
2208 
2209 	/*
2210 	 * ZOMBIED parent does not allow new node creation.
2211 	 * bail out early
2212 	 */
2213 	if (parent_state == SDEV_ZOMBIE) {
2214 		rw_exit(&ddv->sdev_contents);
2215 		*vpp = NULL;
2216 		SD_TRACE_FAILED_LOOKUP(ddv, nm, retried);
2217 		return (ENOENT);
2218 	}
2219 
2220 	/*
2221 	 * (b0): backing store lookup
2222 	 *	SDEV_PERSIST is default except:
2223 	 *		1) pts nodes
2224 	 *		2) non-chmod'ed local nodes
2225 	 */
2226 	if (SDEV_IS_PERSIST(ddv)) {
2227 		error = devname_backstore_lookup(ddv, nm, &rvp);
2228 
2229 		if (!error) {
2230 			sdcmn_err3(("devname_backstore_lookup: "
2231 			    "found attrvp %p for %s\n", (void *)rvp, nm));
2232 
2233 			vattr.va_mask = AT_MODE|AT_UID|AT_GID;
2234 			error = VOP_GETATTR(rvp, &vattr, 0, cred);
2235 			if (error) {
2236 				rw_exit(&ddv->sdev_contents);
2237 				if (dv)
2238 					SDEV_RELE(dv);
2239 				SD_TRACE_FAILED_LOOKUP(ddv, nm, retried);
2240 				sdev_lookup_failed(ddv, nm, failed_flags);
2241 				*vpp = NULLVP;
2242 				return (ENOENT);
2243 			}
2244 
2245 			if (vattr.va_type == VLNK) {
2246 				error = sdev_getlink(rvp, &link);
2247 				if (error) {
2248 					rw_exit(&ddv->sdev_contents);
2249 					if (dv)
2250 						SDEV_RELE(dv);
2251 					SD_TRACE_FAILED_LOOKUP(ddv, nm,
2252 					    retried);
2253 					sdev_lookup_failed(ddv, nm,
2254 					    failed_flags);
2255 					*vpp = NULLVP;
2256 					return (ENOENT);
2257 				}
2258 				ASSERT(link != NULL);
2259 			}
2260 
2261 			if (!rw_tryupgrade(&ddv->sdev_contents)) {
2262 				rw_exit(&ddv->sdev_contents);
2263 				rw_enter(&ddv->sdev_contents, RW_WRITER);
2264 			}
2265 			error = sdev_mknode(ddv, nm, &dv, &vattr,
2266 			    rvp, link, cred, SDEV_READY);
2267 			rw_downgrade(&ddv->sdev_contents);
2268 
2269 			if (link != NULL) {
2270 				kmem_free(link, strlen(link) + 1);
2271 				link = NULL;
2272 			}
2273 
2274 			if (error) {
2275 				SD_TRACE_FAILED_LOOKUP(ddv, nm, retried);
2276 				rw_exit(&ddv->sdev_contents);
2277 				if (dv)
2278 					SDEV_RELE(dv);
2279 				goto lookup_failed;
2280 			} else {
2281 				goto found;
2282 			}
2283 		} else if (retried) {
2284 			rw_exit(&ddv->sdev_contents);
2285 			sdcmn_err3(("retry of lookup of %s/%s: failed\n",
2286 			    ddv->sdev_name, nm));
2287 			if (dv)
2288 				SDEV_RELE(dv);
2289 			SD_TRACE_FAILED_LOOKUP(ddv, nm, retried);
2290 			sdev_lookup_failed(ddv, nm, failed_flags);
2291 			*vpp = NULLVP;
2292 			return (ENOENT);
2293 		}
2294 	}
2295 
2296 
2297 	/* first thread that is doing the lookup on this node */
2298 	if (!dv) {
2299 		if (!rw_tryupgrade(&ddv->sdev_contents)) {
2300 			rw_exit(&ddv->sdev_contents);
2301 			rw_enter(&ddv->sdev_contents, RW_WRITER);
2302 		}
2303 		error = sdev_mknode(ddv, nm, &dv, NULL, NULL, NULL,
2304 		    cred, SDEV_INIT);
2305 		if (!dv) {
2306 			rw_exit(&ddv->sdev_contents);
2307 			SD_TRACE_FAILED_LOOKUP(ddv, nm, retried);
2308 			sdev_lookup_failed(ddv, nm, failed_flags);
2309 			*vpp = NULLVP;
2310 			return (ENOENT);
2311 		}
2312 		rw_downgrade(&ddv->sdev_contents);
2313 	}
2314 	ASSERT(dv);
2315 	ASSERT(SDEV_HELD(dv));
2316 
2317 	if (SDEV_IS_NO_NCACHE(dv)) {
2318 		failed_flags |= SLF_NO_NCACHE;
2319 	}
2320 
2321 	if (SDEV_IS_GLOBAL(ddv)) {
2322 		map = sdev_get_map(ddv, 1);
2323 		dirops = map ? map->dir_ops : NULL;
2324 		fn = dirops ? dirops->devnops_lookup : NULL;
2325 	}
2326 
2327 	/*
2328 	 * (b1) invoking devfsadm once per life time for devfsadm nodes
2329 	 */
2330 	if ((fn == NULL) && !callback) {
2331 
2332 		if (sdev_reconfig_boot || !i_ddi_io_initialized() ||
2333 		    SDEV_IS_DYNAMIC(ddv) || SDEV_IS_NO_NCACHE(dv) ||
2334 		    ((moddebug & MODDEBUG_FINI_EBUSY) != 0)) {
2335 			ASSERT(SDEV_HELD(dv));
2336 			SD_TRACE_FAILED_LOOKUP(ddv, nm, retried);
2337 			goto nolock_notfound;
2338 		}
2339 
2340 		/*
2341 		 * filter out known non-existent devices recorded
2342 		 * during initial reconfiguration boot for which
2343 		 * reconfig should not be done and lookup may
2344 		 * be short-circuited now.
2345 		 */
2346 		if (sdev_lookup_filter(ddv, nm)) {
2347 			SD_TRACE_FAILED_LOOKUP(ddv, nm, retried);
2348 			goto nolock_notfound;
2349 		}
2350 
2351 		/* bypassing devfsadm internal nodes */
2352 		if (is_devfsadm_thread(lookup_thread)) {
2353 			SD_TRACE_FAILED_LOOKUP(ddv, nm, retried);
2354 			goto nolock_notfound;
2355 		}
2356 
2357 		if (sdev_reconfig_disable) {
2358 			SD_TRACE_FAILED_LOOKUP(ddv, nm, retried);
2359 			goto nolock_notfound;
2360 		}
2361 
2362 		error = sdev_call_devfsadmd(ddv, dv, nm);
2363 		if (error == 0) {
2364 			sdcmn_err8(("lookup of %s/%s by %s: reconfig\n",
2365 			    ddv->sdev_name, nm, curproc->p_user.u_comm));
2366 			if (sdev_reconfig_verbose) {
2367 				cmn_err(CE_CONT,
2368 				    "?lookup of %s/%s by %s: reconfig\n",
2369 				    ddv->sdev_name, nm, curproc->p_user.u_comm);
2370 			}
2371 			retried = 1;
2372 			failed_flags |= SLF_REBUILT;
2373 			ASSERT(dv->sdev_state != SDEV_ZOMBIE);
2374 			SDEV_SIMPLE_RELE(dv);
2375 			goto tryagain;
2376 		} else {
2377 			SD_TRACE_FAILED_LOOKUP(ddv, nm, retried);
2378 			goto nolock_notfound;
2379 		}
2380 	}
2381 
2382 	/*
2383 	 * (b2) Directory Based Name Resolution (DBNR):
2384 	 *	ddv	- parent
2385 	 *	nm	- /dev/(ddv->sdev_name)/nm
2386 	 *
2387 	 *	note: module vnode ops take precedence than the build-in ones
2388 	 */
2389 	if (fn) {
2390 		error = sdev_call_modulelookup(ddv, &dv, nm, fn, cred);
2391 		if (error) {
2392 			SD_TRACE_FAILED_LOOKUP(ddv, nm, retried);
2393 			goto notfound;
2394 		} else {
2395 			goto found;
2396 		}
2397 	} else if (callback) {
2398 		error = sdev_call_dircallback(ddv, &dv, nm, callback,
2399 		    flags, cred);
2400 		if (error == 0) {
2401 			goto found;
2402 		} else {
2403 			SD_TRACE_FAILED_LOOKUP(ddv, nm, retried);
2404 			goto notfound;
2405 		}
2406 	}
2407 	ASSERT(rvp);
2408 
2409 found:
2410 	ASSERT(!(dv->sdev_flags & SDEV_STALE));
2411 	ASSERT(dv->sdev_state == SDEV_READY);
2412 	if (vtor) {
2413 		/*
2414 		 * Check validity of returned node
2415 		 */
2416 		switch (vtor(dv)) {
2417 		case SDEV_VTOR_VALID:
2418 			break;
2419 		case SDEV_VTOR_INVALID:
2420 			SD_TRACE_FAILED_LOOKUP(ddv, nm, retried);
2421 			sdcmn_err7(("lookup: destroy invalid "
2422 			    "node: %s(%p)\n", dv->sdev_name, (void *)dv));
2423 			goto nolock_notfound;
2424 		case SDEV_VTOR_SKIP:
2425 			sdcmn_err7(("lookup: node not applicable - "
2426 			    "skipping: %s(%p)\n", dv->sdev_name, (void *)dv));
2427 			rw_exit(&ddv->sdev_contents);
2428 			SD_TRACE_FAILED_LOOKUP(ddv, nm, retried);
2429 			SDEV_RELE(dv);
2430 			goto lookup_failed;
2431 		default:
2432 			cmn_err(CE_PANIC,
2433 			    "dev fs: validator failed: %s(%p)\n",
2434 			    dv->sdev_name, (void *)dv);
2435 			break;
2436 			/*NOTREACHED*/
2437 		}
2438 	}
2439 
2440 	if ((SDEVTOV(dv)->v_type == VDIR) && SDEV_IS_GLOBAL(dv)) {
2441 		rw_enter(&dv->sdev_contents, RW_READER);
2442 		(void) sdev_get_map(dv, 1);
2443 		rw_exit(&dv->sdev_contents);
2444 	}
2445 	rw_exit(&ddv->sdev_contents);
2446 	rv = sdev_to_vp(dv, vpp);
2447 	sdcmn_err3(("devname_lookup_func: returning vp %p v_count %d state %d "
2448 	    "for nm %s, error %d\n", (void *)*vpp, (*vpp)->v_count,
2449 	    dv->sdev_state, nm, rv));
2450 	return (rv);
2451 
2452 notfound:
2453 	mutex_enter(&dv->sdev_lookup_lock);
2454 	SDEV_UNBLOCK_OTHERS(dv, SDEV_LOOKUP);
2455 	mutex_exit(&dv->sdev_lookup_lock);
2456 nolock_notfound:
2457 	/*
2458 	 * Destroy the node that is created for synchronization purposes.
2459 	 */
2460 	sdcmn_err3(("devname_lookup_func: %s with state %d\n",
2461 	    nm, dv->sdev_state));
2462 	ASSERT(RW_READ_HELD(&ddv->sdev_contents));
2463 	if (dv->sdev_state == SDEV_INIT) {
2464 		if (!rw_tryupgrade(&ddv->sdev_contents)) {
2465 			rw_exit(&ddv->sdev_contents);
2466 			rw_enter(&ddv->sdev_contents, RW_WRITER);
2467 		}
2468 
2469 		/*
2470 		 * Node state may have changed during the lock
2471 		 * changes. Re-check.
2472 		 */
2473 		if (dv->sdev_state == SDEV_INIT) {
2474 			(void) sdev_dirdelete(ddv, dv);
2475 			rw_exit(&ddv->sdev_contents);
2476 			sdev_lookup_failed(ddv, nm, failed_flags);
2477 			*vpp = NULL;
2478 			return (ENOENT);
2479 		}
2480 	}
2481 
2482 	rw_exit(&ddv->sdev_contents);
2483 	SDEV_RELE(dv);
2484 
2485 lookup_failed:
2486 	sdev_lookup_failed(ddv, nm, failed_flags);
2487 	*vpp = NULL;
2488 	return (ENOENT);
2489 }
2490 
2491 /*
2492  * Given a directory node, mark all nodes beneath as
2493  * STALE, i.e. nodes that don't exist as far as new
2494  * consumers are concerned
2495  */
2496 void
2497 sdev_stale(struct sdev_node *ddv)
2498 {
2499 	struct sdev_node *dv;
2500 	struct vnode *vp;
2501 
2502 	ASSERT(SDEVTOV(ddv)->v_type == VDIR);
2503 
2504 	rw_enter(&ddv->sdev_contents, RW_WRITER);
2505 	for (dv = ddv->sdev_dot; dv; dv = dv->sdev_next) {
2506 		vp = SDEVTOV(dv);
2507 		if (vp->v_type == VDIR)
2508 			sdev_stale(dv);
2509 
2510 		sdcmn_err9(("sdev_stale: setting stale %s\n",
2511 		    dv->sdev_name));
2512 		dv->sdev_flags |= SDEV_STALE;
2513 	}
2514 	ddv->sdev_flags |= SDEV_BUILD;
2515 	rw_exit(&ddv->sdev_contents);
2516 }
2517 
2518 /*
2519  * Given a directory node, clean out all the nodes beneath.
2520  * If expr is specified, clean node with names matching expr.
2521  * If SDEV_ENFORCE is specified in flags, busy nodes are made stale,
2522  *	so they are excluded from future lookups.
2523  */
2524 int
2525 sdev_cleandir(struct sdev_node *ddv, char *expr, uint_t flags)
2526 {
2527 	int error = 0;
2528 	int busy = 0;
2529 	struct vnode *vp;
2530 	struct sdev_node *dv, *next = NULL;
2531 	int bkstore = 0;
2532 	int len = 0;
2533 	char *bks_name = NULL;
2534 
2535 	ASSERT(SDEVTOV(ddv)->v_type == VDIR);
2536 
2537 	/*
2538 	 * We try our best to destroy all unused sdev_node's
2539 	 */
2540 	rw_enter(&ddv->sdev_contents, RW_WRITER);
2541 	for (dv = ddv->sdev_dot; dv; dv = next) {
2542 		next = dv->sdev_next;
2543 		vp = SDEVTOV(dv);
2544 
2545 		if (expr && gmatch(dv->sdev_name, expr) == 0)
2546 			continue;
2547 
2548 		if (vp->v_type == VDIR &&
2549 		    sdev_cleandir(dv, NULL, flags) != 0) {
2550 			sdcmn_err9(("sdev_cleandir: dir %s busy\n",
2551 			    dv->sdev_name));
2552 			busy++;
2553 			continue;
2554 		}
2555 
2556 		if (vp->v_count > 0 && (flags & SDEV_ENFORCE) == 0) {
2557 			sdcmn_err9(("sdev_cleandir: dir %s busy\n",
2558 			    dv->sdev_name));
2559 			busy++;
2560 			continue;
2561 		}
2562 
2563 		/*
2564 		 * at this point, either dv is not held or SDEV_ENFORCE
2565 		 * is specified. In either case, dv needs to be deleted
2566 		 */
2567 		SDEV_HOLD(dv);
2568 
2569 		bkstore = SDEV_IS_PERSIST(dv) ? 1 : 0;
2570 		if (bkstore && (vp->v_type == VDIR))
2571 			bkstore += 1;
2572 
2573 		if (bkstore) {
2574 			len = strlen(dv->sdev_name) + 1;
2575 			bks_name = kmem_alloc(len, KM_SLEEP);
2576 			bcopy(dv->sdev_name, bks_name, len);
2577 		}
2578 
2579 		error = sdev_dirdelete(ddv, dv);
2580 
2581 		if (error == EBUSY) {
2582 			sdcmn_err9(("sdev_cleandir: dir busy\n"));
2583 			busy++;
2584 		}
2585 
2586 		/* take care the backing store clean up */
2587 		if (bkstore && (error == 0)) {
2588 			ASSERT(bks_name);
2589 			ASSERT(ddv->sdev_attrvp);
2590 
2591 			if (bkstore == 1) {
2592 				error = VOP_REMOVE(ddv->sdev_attrvp,
2593 				    bks_name, kcred);
2594 			} else if (bkstore == 2) {
2595 				error = VOP_RMDIR(ddv->sdev_attrvp,
2596 				    bks_name, ddv->sdev_attrvp, kcred);
2597 			}
2598 
2599 			/* do not propagate the backing store errors */
2600 			if (error) {
2601 				sdcmn_err9(("sdev_cleandir: backing store"
2602 				    "not cleaned\n"));
2603 				error = 0;
2604 			}
2605 
2606 			bkstore = 0;
2607 			kmem_free(bks_name, len);
2608 			bks_name = NULL;
2609 			len = 0;
2610 		}
2611 	}
2612 
2613 	ddv->sdev_flags |= SDEV_BUILD;
2614 	rw_exit(&ddv->sdev_contents);
2615 
2616 	if (busy) {
2617 		error = EBUSY;
2618 	}
2619 
2620 	return (error);
2621 }
2622 
2623 /*
2624  * a convenient wrapper for readdir() funcs
2625  */
2626 size_t
2627 add_dir_entry(dirent64_t *de, char *nm, size_t size, ino_t ino, offset_t off)
2628 {
2629 	size_t reclen = DIRENT64_RECLEN(strlen(nm));
2630 	if (reclen > size)
2631 		return (0);
2632 
2633 	de->d_ino = (ino64_t)ino;
2634 	de->d_off = (off64_t)off + 1;
2635 	de->d_reclen = (ushort_t)reclen;
2636 	(void) strncpy(de->d_name, nm, DIRENT64_NAMELEN(reclen));
2637 	return (reclen);
2638 }
2639 
2640 /*
2641  * sdev_mount service routines
2642  */
2643 int
2644 sdev_copyin_mountargs(struct mounta *uap, struct sdev_mountargs *args)
2645 {
2646 	int	error;
2647 
2648 	if (uap->datalen != sizeof (*args))
2649 		return (EINVAL);
2650 
2651 	if (error = copyin(uap->dataptr, args, sizeof (*args))) {
2652 		cmn_err(CE_WARN, "sdev_copyin_mountargs: can not"
2653 		    "get user data. error %d\n", error);
2654 		return (EFAULT);
2655 	}
2656 
2657 	return (0);
2658 }
2659 
2660 #ifdef nextdp
2661 #undef nextdp
2662 #endif
2663 #define	nextdp(dp)	((struct dirent64 *)((char *)(dp) + (dp)->d_reclen))
2664 
2665 /*
2666  * readdir helper func
2667  */
2668 int
2669 devname_readdir_func(vnode_t *vp, uio_t *uiop, cred_t *cred, int *eofp,
2670     int flags)
2671 {
2672 	struct sdev_node *ddv = VTOSDEV(vp);
2673 	struct sdev_node *dv;
2674 	dirent64_t	*dp;
2675 	ulong_t		outcount = 0;
2676 	size_t		namelen;
2677 	ulong_t		alloc_count;
2678 	void		*outbuf;
2679 	struct iovec	*iovp;
2680 	int		error = 0;
2681 	size_t		reclen;
2682 	offset_t	diroff;
2683 	offset_t	soff;
2684 	int		this_reclen;
2685 	struct devname_nsmap	*map = NULL;
2686 	struct devname_ops	*dirops = NULL;
2687 	int (*fn)(devname_handle_t *, struct cred *) = NULL;
2688 	int (*vtor)(struct sdev_node *) = NULL;
2689 	struct vattr attr;
2690 	timestruc_t now;
2691 
2692 	ASSERT(ddv->sdev_attr || ddv->sdev_attrvp);
2693 	ASSERT(RW_READ_HELD(&ddv->sdev_contents));
2694 
2695 	if (uiop->uio_loffset >= MAXOFF_T) {
2696 		if (eofp)
2697 			*eofp = 1;
2698 		return (0);
2699 	}
2700 
2701 	if (uiop->uio_iovcnt != 1)
2702 		return (EINVAL);
2703 
2704 	if (vp->v_type != VDIR)
2705 		return (ENOTDIR);
2706 
2707 	if (ddv->sdev_flags & SDEV_VTOR) {
2708 		vtor = (int (*)(struct sdev_node *))sdev_get_vtor(ddv);
2709 		ASSERT(vtor);
2710 	}
2711 
2712 	if (eofp != NULL)
2713 		*eofp = 0;
2714 
2715 	soff = uiop->uio_offset;
2716 	iovp = uiop->uio_iov;
2717 	alloc_count = iovp->iov_len;
2718 	dp = outbuf = kmem_alloc(alloc_count, KM_SLEEP);
2719 	outcount = 0;
2720 
2721 	if (ddv->sdev_state == SDEV_ZOMBIE)
2722 		goto get_cache;
2723 
2724 	if (SDEV_IS_GLOBAL(ddv)) {
2725 		map = sdev_get_map(ddv, 0);
2726 		dirops = map ? map->dir_ops : NULL;
2727 		fn = dirops ? dirops->devnops_readdir : NULL;
2728 
2729 		if (map && map->dir_map) {
2730 			/*
2731 			 * load the name mapping rule database
2732 			 * through invoking devfsadm and symlink
2733 			 * all the entries in the map
2734 			 */
2735 			devname_rdr_result_t rdr_result;
2736 			int do_thread = 0;
2737 
2738 			rw_enter(&map->dir_lock, RW_READER);
2739 			do_thread = map->dir_maploaded ? 0 : 1;
2740 			rw_exit(&map->dir_lock);
2741 
2742 			if (do_thread) {
2743 				mutex_enter(&ddv->sdev_lookup_lock);
2744 				SDEV_BLOCK_OTHERS(ddv, SDEV_READDIR);
2745 				mutex_exit(&ddv->sdev_lookup_lock);
2746 
2747 				sdev_dispatch_to_nsrdr_thread(ddv,
2748 				    map->dir_map, &rdr_result);
2749 			}
2750 		} else if ((sdev_boot_state == SDEV_BOOT_STATE_COMPLETE) &&
2751 		    !sdev_reconfig_boot && (flags & SDEV_BROWSE) &&
2752 		    !SDEV_IS_DYNAMIC(ddv) && !SDEV_IS_NO_NCACHE(ddv) &&
2753 		    ((moddebug & MODDEBUG_FINI_EBUSY) == 0) &&
2754 		    !DEVNAME_DEVFSADM_HAS_RUN(devfsadm_state) &&
2755 		    !DEVNAME_DEVFSADM_IS_RUNNING(devfsadm_state) &&
2756 		    !sdev_reconfig_disable) {
2757 			/*
2758 			 * invoking "devfsadm" to do system device reconfig
2759 			 */
2760 			mutex_enter(&ddv->sdev_lookup_lock);
2761 			SDEV_BLOCK_OTHERS(ddv,
2762 			    (SDEV_READDIR|SDEV_LGWAITING));
2763 			mutex_exit(&ddv->sdev_lookup_lock);
2764 
2765 			sdcmn_err8(("readdir of %s by %s: reconfig\n",
2766 			    ddv->sdev_path, curproc->p_user.u_comm));
2767 			if (sdev_reconfig_verbose) {
2768 				cmn_err(CE_CONT,
2769 				    "?readdir of %s by %s: reconfig\n",
2770 				    ddv->sdev_path, curproc->p_user.u_comm);
2771 			}
2772 
2773 			sdev_devfsadmd_thread(ddv, NULL, kcred);
2774 		} else if (DEVNAME_DEVFSADM_IS_RUNNING(devfsadm_state)) {
2775 			/*
2776 			 * compensate the "ls" started later than "devfsadm"
2777 			 */
2778 			mutex_enter(&ddv->sdev_lookup_lock);
2779 			SDEV_BLOCK_OTHERS(ddv, (SDEV_READDIR|SDEV_LGWAITING));
2780 			mutex_exit(&ddv->sdev_lookup_lock);
2781 		}
2782 
2783 		/*
2784 		 * release the contents lock so that
2785 		 * the cache maybe updated by devfsadmd
2786 		 */
2787 		rw_exit(&ddv->sdev_contents);
2788 		mutex_enter(&ddv->sdev_lookup_lock);
2789 		if (SDEV_IS_READDIR(ddv))
2790 			(void) sdev_wait4lookup(ddv, SDEV_READDIR);
2791 		mutex_exit(&ddv->sdev_lookup_lock);
2792 		rw_enter(&ddv->sdev_contents, RW_READER);
2793 
2794 		sdcmn_err4(("readdir of directory %s by %s\n",
2795 		    ddv->sdev_name, curproc->p_user.u_comm));
2796 		while (ddv->sdev_flags & SDEV_BUILD) {
2797 			if (SDEV_IS_PERSIST(ddv)) {
2798 				error = sdev_filldir_from_store(ddv,
2799 				    alloc_count, cred);
2800 			}
2801 
2802 			/*
2803 			 * pre-creating the directories
2804 			 * defined in vtab
2805 			 */
2806 			if (SDEVTOV(ddv)->v_flag & VROOT) {
2807 				error = sdev_filldir_dynamic(ddv);
2808 			}
2809 
2810 			if (!error)
2811 				ddv->sdev_flags &= ~SDEV_BUILD;
2812 		}
2813 	}
2814 
2815 get_cache:
2816 	/* handle "." and ".." */
2817 	diroff = 0;
2818 	if (soff == 0) {
2819 		/* first time */
2820 		this_reclen = DIRENT64_RECLEN(1);
2821 		if (alloc_count < this_reclen) {
2822 			error = EINVAL;
2823 			goto done;
2824 		}
2825 
2826 		dp->d_ino = (ino64_t)ddv->sdev_ino;
2827 		dp->d_off = (off64_t)1;
2828 		dp->d_reclen = (ushort_t)this_reclen;
2829 
2830 		(void) strncpy(dp->d_name, ".",
2831 		    DIRENT64_NAMELEN(this_reclen));
2832 		outcount += dp->d_reclen;
2833 		dp = nextdp(dp);
2834 	}
2835 
2836 	diroff++;
2837 	if (soff <= 1) {
2838 		this_reclen = DIRENT64_RECLEN(2);
2839 		if (alloc_count < outcount + this_reclen) {
2840 			error = EINVAL;
2841 			goto done;
2842 		}
2843 
2844 		dp->d_reclen = (ushort_t)this_reclen;
2845 		dp->d_ino = (ino64_t)ddv->sdev_dotdot->sdev_ino;
2846 		dp->d_off = (off64_t)2;
2847 
2848 		(void) strncpy(dp->d_name, "..",
2849 		    DIRENT64_NAMELEN(this_reclen));
2850 		outcount += dp->d_reclen;
2851 
2852 		dp = nextdp(dp);
2853 	}
2854 
2855 
2856 	/* gets the cache */
2857 	diroff++;
2858 	for (dv = ddv->sdev_dot; dv; dv = dv->sdev_next, diroff++) {
2859 		sdcmn_err3(("sdev_readdir: diroff %lld soff %lld for '%s' \n",
2860 		    diroff, soff, dv->sdev_name));
2861 
2862 		/* bypassing pre-matured nodes */
2863 		if (diroff < soff || (dv->sdev_state != SDEV_READY)) {
2864 			sdcmn_err3(("sdev_readdir: pre-mature node  "
2865 			    "%s\n", dv->sdev_name));
2866 			continue;
2867 		}
2868 
2869 		/* don't list stale nodes */
2870 		if (dv->sdev_flags & SDEV_STALE) {
2871 			sdcmn_err4(("sdev_readdir: STALE node  "
2872 			    "%s\n", dv->sdev_name));
2873 			continue;
2874 		}
2875 
2876 		/*
2877 		 * Check validity of node
2878 		 */
2879 		if (vtor) {
2880 			switch (vtor(dv)) {
2881 			case SDEV_VTOR_VALID:
2882 				break;
2883 			case SDEV_VTOR_INVALID:
2884 			case SDEV_VTOR_SKIP:
2885 				continue;
2886 			default:
2887 				cmn_err(CE_PANIC,
2888 				    "dev fs: validator failed: %s(%p)\n",
2889 				    dv->sdev_name, (void *)dv);
2890 				break;
2891 			/*NOTREACHED*/
2892 			}
2893 		}
2894 
2895 		/*
2896 		 * call back into the module for the validity/bookkeeping
2897 		 * of this entry
2898 		 */
2899 		if (fn) {
2900 			error = (*fn)(&(dv->sdev_handle), cred);
2901 			if (error) {
2902 				sdcmn_err4(("sdev_readdir: module did not "
2903 				    "validate %s\n", dv->sdev_name));
2904 				continue;
2905 			}
2906 		}
2907 
2908 		namelen = strlen(dv->sdev_name);
2909 		reclen = DIRENT64_RECLEN(namelen);
2910 		if (outcount + reclen > alloc_count) {
2911 			goto full;
2912 		}
2913 		dp->d_reclen = (ushort_t)reclen;
2914 		dp->d_ino = (ino64_t)dv->sdev_ino;
2915 		dp->d_off = (off64_t)diroff + 1;
2916 		(void) strncpy(dp->d_name, dv->sdev_name,
2917 		    DIRENT64_NAMELEN(reclen));
2918 		outcount += reclen;
2919 		dp = nextdp(dp);
2920 	}
2921 
2922 full:
2923 	sdcmn_err4(("sdev_readdir: moving %lu bytes: "
2924 	    "diroff %lld, soff %lld, dv %p\n", outcount, diroff, soff,
2925 	    (void *)dv));
2926 
2927 	if (outcount)
2928 		error = uiomove(outbuf, outcount, UIO_READ, uiop);
2929 
2930 	if (!error) {
2931 		uiop->uio_offset = diroff;
2932 		if (eofp)
2933 			*eofp = dv ? 0 : 1;
2934 	}
2935 
2936 
2937 	if (ddv->sdev_attrvp) {
2938 		gethrestime(&now);
2939 		attr.va_ctime = now;
2940 		attr.va_atime = now;
2941 		attr.va_mask = AT_CTIME|AT_ATIME;
2942 
2943 		(void) VOP_SETATTR(ddv->sdev_attrvp, &attr, 0, kcred, NULL);
2944 	}
2945 done:
2946 	kmem_free(outbuf, alloc_count);
2947 	return (error);
2948 }
2949 
2950 
2951 static int
2952 sdev_modctl_lookup(const char *path, vnode_t **r_vp)
2953 {
2954 	vnode_t *vp;
2955 	vnode_t *cvp;
2956 	struct sdev_node *svp;
2957 	char *nm;
2958 	struct pathname pn;
2959 	int error;
2960 	int persisted = 0;
2961 
2962 	if (error = pn_get((char *)path, UIO_SYSSPACE, &pn))
2963 		return (error);
2964 	nm = kmem_alloc(MAXNAMELEN, KM_SLEEP);
2965 
2966 	vp = rootdir;
2967 	VN_HOLD(vp);
2968 
2969 	while (pn_pathleft(&pn)) {
2970 		ASSERT(vp->v_type == VDIR);
2971 		(void) pn_getcomponent(&pn, nm);
2972 		error = VOP_LOOKUP(vp, nm, &cvp, NULL, 0, NULL, kcred);
2973 		VN_RELE(vp);
2974 
2975 		if (error)
2976 			break;
2977 
2978 		/* traverse mount points encountered on our journey */
2979 		if (vn_ismntpt(cvp) && (error = traverse(&cvp)) != 0) {
2980 			VN_RELE(cvp);
2981 			break;
2982 		}
2983 
2984 		/*
2985 		 * Direct the operation to the persisting filesystem
2986 		 * underlying /dev.  Bail if we encounter a
2987 		 * non-persistent dev entity here.
2988 		 */
2989 		if (cvp->v_vfsp->vfs_fstype == devtype) {
2990 
2991 			if ((VTOSDEV(cvp)->sdev_flags & SDEV_PERSIST) == 0) {
2992 				error = ENOENT;
2993 				VN_RELE(cvp);
2994 				break;
2995 			}
2996 
2997 			if (VTOSDEV(cvp) == NULL) {
2998 				error = ENOENT;
2999 				VN_RELE(cvp);
3000 				break;
3001 			}
3002 			svp = VTOSDEV(cvp);
3003 			if ((vp = svp->sdev_attrvp) == NULL) {
3004 				error = ENOENT;
3005 				VN_RELE(cvp);
3006 				break;
3007 			}
3008 			persisted = 1;
3009 			VN_HOLD(vp);
3010 			VN_RELE(cvp);
3011 			cvp = vp;
3012 		}
3013 
3014 		vp = cvp;
3015 		pn_skipslash(&pn);
3016 	}
3017 
3018 	kmem_free(nm, MAXNAMELEN);
3019 	pn_free(&pn);
3020 
3021 	if (error)
3022 		return (error);
3023 
3024 	/*
3025 	 * Only return persisted nodes in the filesystem underlying /dev.
3026 	 */
3027 	if (!persisted) {
3028 		VN_RELE(vp);
3029 		return (ENOENT);
3030 	}
3031 
3032 	*r_vp = vp;
3033 	return (0);
3034 }
3035 
3036 int
3037 sdev_modctl_readdir(const char *dir, char ***dirlistp,
3038 	int *npathsp, int *npathsp_alloc)
3039 {
3040 	char	**pathlist = NULL;
3041 	char	**newlist = NULL;
3042 	int	npaths = 0;
3043 	int	npaths_alloc = 0;
3044 	dirent64_t *dbuf = NULL;
3045 	int	n;
3046 	char	*s;
3047 	int error;
3048 	vnode_t *vp;
3049 	int eof;
3050 	struct iovec iov;
3051 	struct uio uio;
3052 	struct dirent64 *dp;
3053 	size_t dlen;
3054 	size_t dbuflen;
3055 	int ndirents = 64;
3056 	char *nm;
3057 
3058 	error = sdev_modctl_lookup(dir, &vp);
3059 	sdcmn_err11(("modctl readdir: %s by %s: %s\n",
3060 	    dir, curproc->p_user.u_comm,
3061 	    (error == 0) ? "ok" : "failed"));
3062 	if (error)
3063 		return (error);
3064 
3065 	dlen = ndirents * (sizeof (*dbuf));
3066 	dbuf = kmem_alloc(dlen, KM_SLEEP);
3067 
3068 	uio.uio_iov = &iov;
3069 	uio.uio_iovcnt = 1;
3070 	uio.uio_segflg = UIO_SYSSPACE;
3071 	uio.uio_fmode = 0;
3072 	uio.uio_extflg = UIO_COPY_CACHED;
3073 	uio.uio_loffset = 0;
3074 	uio.uio_llimit = MAXOFFSET_T;
3075 
3076 	eof = 0;
3077 	error = 0;
3078 	while (!error && !eof) {
3079 		uio.uio_resid = dlen;
3080 		iov.iov_base = (char *)dbuf;
3081 		iov.iov_len = dlen;
3082 
3083 		(void) VOP_RWLOCK(vp, V_WRITELOCK_FALSE, NULL);
3084 		error = VOP_READDIR(vp, &uio, kcred, &eof);
3085 		VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, NULL);
3086 
3087 		dbuflen = dlen - uio.uio_resid;
3088 
3089 		if (error || dbuflen == 0)
3090 			break;
3091 
3092 		for (dp = dbuf; ((intptr_t)dp < (intptr_t)dbuf + dbuflen);
3093 			dp = (dirent64_t *)((intptr_t)dp + dp->d_reclen)) {
3094 
3095 			nm = dp->d_name;
3096 
3097 			if (strcmp(nm, ".") == 0 || strcmp(nm, "..") == 0)
3098 				continue;
3099 
3100 			if (npaths == npaths_alloc) {
3101 				npaths_alloc += 64;
3102 				newlist = (char **)
3103 				    kmem_zalloc((npaths_alloc + 1) *
3104 					sizeof (char *), KM_SLEEP);
3105 				if (pathlist) {
3106 					bcopy(pathlist, newlist,
3107 					    npaths * sizeof (char *));
3108 					kmem_free(pathlist,
3109 					    (npaths + 1) * sizeof (char *));
3110 				}
3111 				pathlist = newlist;
3112 			}
3113 			n = strlen(nm) + 1;
3114 			s = kmem_alloc(n, KM_SLEEP);
3115 			bcopy(nm, s, n);
3116 			pathlist[npaths++] = s;
3117 			sdcmn_err11(("  %s/%s\n", dir, s));
3118 		}
3119 	}
3120 
3121 exit:
3122 	VN_RELE(vp);
3123 
3124 	if (dbuf)
3125 		kmem_free(dbuf, dlen);
3126 
3127 	if (error)
3128 		return (error);
3129 
3130 	*dirlistp = pathlist;
3131 	*npathsp = npaths;
3132 	*npathsp_alloc = npaths_alloc;
3133 
3134 	return (0);
3135 }
3136 
3137 void
3138 sdev_modctl_readdir_free(char **pathlist, int npaths, int npaths_alloc)
3139 {
3140 	int	i, n;
3141 
3142 	for (i = 0; i < npaths; i++) {
3143 		n = strlen(pathlist[i]) + 1;
3144 		kmem_free(pathlist[i], n);
3145 	}
3146 
3147 	kmem_free(pathlist, (npaths_alloc + 1) * sizeof (char *));
3148 }
3149 
3150 int
3151 sdev_modctl_devexists(const char *path)
3152 {
3153 	vnode_t *vp;
3154 	int error;
3155 
3156 	error = sdev_modctl_lookup(path, &vp);
3157 	sdcmn_err11(("modctl dev exists: %s by %s: %s\n",
3158 	    path, curproc->p_user.u_comm,
3159 	    (error == 0) ? "ok" : "failed"));
3160 	if (error == 0)
3161 		VN_RELE(vp);
3162 
3163 	return (error);
3164 }
3165 
3166 void
3167 sdev_update_newnsmap(struct devname_nsmap *map, char *module, char *mapname)
3168 {
3169 	rw_enter(&map->dir_lock, RW_WRITER);
3170 	if (module) {
3171 		ASSERT(map->dir_newmodule == NULL);
3172 		map->dir_newmodule = i_ddi_strdup(module, KM_SLEEP);
3173 	}
3174 	if (mapname) {
3175 		ASSERT(map->dir_newmap == NULL);
3176 		map->dir_newmap = i_ddi_strdup(mapname, KM_SLEEP);
3177 	}
3178 
3179 	map->dir_invalid = 1;
3180 	rw_exit(&map->dir_lock);
3181 }
3182 
3183 void
3184 sdev_replace_nsmap(struct devname_nsmap *map, char *module, char *mapname)
3185 {
3186 	char *old_module = NULL;
3187 	char *old_map = NULL;
3188 
3189 	ASSERT(RW_LOCK_HELD(&map->dir_lock));
3190 	if (!rw_tryupgrade(&map->dir_lock)) {
3191 		rw_exit(&map->dir_lock);
3192 		rw_enter(&map->dir_lock, RW_WRITER);
3193 	}
3194 
3195 	old_module = map->dir_module;
3196 	if (module) {
3197 		if (old_module && strcmp(old_module, module) != 0) {
3198 			kmem_free(old_module, strlen(old_module) + 1);
3199 		}
3200 		map->dir_module = module;
3201 		map->dir_newmodule = NULL;
3202 	}
3203 
3204 	old_map = map->dir_map;
3205 	if (mapname) {
3206 		if (old_map && strcmp(old_map, mapname) != 0) {
3207 			kmem_free(old_map, strlen(old_map) + 1);
3208 		}
3209 
3210 		map->dir_map = mapname;
3211 		map->dir_newmap = NULL;
3212 	}
3213 	map->dir_maploaded = 0;
3214 	map->dir_invalid = 0;
3215 	rw_downgrade(&map->dir_lock);
3216 }
3217 
3218 /*
3219  * dir_name should have at least one attribute,
3220  *	dir_module
3221  *	or dir_map
3222  *	or both
3223  * caller holds the devname_nsmaps_lock
3224  */
3225 void
3226 sdev_insert_nsmap(char *dir_name, char *dir_module, char *dir_map)
3227 {
3228 	struct devname_nsmap *map;
3229 	int len = 0;
3230 
3231 	ASSERT(dir_name);
3232 	ASSERT(dir_module || dir_map);
3233 	ASSERT(MUTEX_HELD(&devname_nsmaps_lock));
3234 
3235 	if (map = sdev_get_nsmap_by_dir(dir_name, 1)) {
3236 		sdev_update_newnsmap(map, dir_module, dir_map);
3237 		return;
3238 	}
3239 
3240 	map = (struct devname_nsmap *)kmem_zalloc(sizeof (*map), KM_SLEEP);
3241 	map->dir_name = i_ddi_strdup(dir_name, KM_SLEEP);
3242 	if (dir_module) {
3243 		map->dir_module = i_ddi_strdup(dir_module, KM_SLEEP);
3244 	}
3245 
3246 	if (dir_map) {
3247 		if (dir_map[0] != '/') {
3248 			len = strlen(ETC_DEV_DIR) + strlen(dir_map) + 2;
3249 			map->dir_map = kmem_zalloc(len, KM_SLEEP);
3250 			(void) snprintf(map->dir_map, len, "%s/%s", ETC_DEV_DIR,
3251 			    dir_map);
3252 		} else {
3253 			map->dir_map = i_ddi_strdup(dir_map, KM_SLEEP);
3254 		}
3255 	}
3256 
3257 	map->dir_ops = NULL;
3258 	map->dir_maploaded = 0;
3259 	map->dir_invalid = 0;
3260 	rw_init(&map->dir_lock, NULL, RW_DEFAULT, NULL);
3261 
3262 	map->next = devname_nsmaps;
3263 	map->prev = NULL;
3264 	if (devname_nsmaps) {
3265 		devname_nsmaps->prev = map;
3266 	}
3267 	devname_nsmaps = map;
3268 }
3269 
3270 struct devname_nsmap *
3271 sdev_get_nsmap_by_dir(char *dir_path, int locked)
3272 {
3273 	struct devname_nsmap *map = NULL;
3274 
3275 	if (!locked)
3276 		mutex_enter(&devname_nsmaps_lock);
3277 	for (map = devname_nsmaps; map; map = map->next) {
3278 		sdcmn_err6(("sdev_get_nsmap_by_dir: dir %s\n", map->dir_name));
3279 		if (strcmp(map->dir_name, dir_path) == 0) {
3280 			if (!locked)
3281 				mutex_exit(&devname_nsmaps_lock);
3282 			return (map);
3283 		}
3284 	}
3285 	if (!locked)
3286 		mutex_exit(&devname_nsmaps_lock);
3287 	return (NULL);
3288 }
3289 
3290 struct devname_nsmap *
3291 sdev_get_nsmap_by_module(char *mod_name)
3292 {
3293 	struct devname_nsmap *map = NULL;
3294 
3295 	mutex_enter(&devname_nsmaps_lock);
3296 	for (map = devname_nsmaps; map; map = map->next) {
3297 		sdcmn_err7(("sdev_get_nsmap_by_module: module %s\n",
3298 		    map->dir_module));
3299 		if (map->dir_module && strcmp(map->dir_module, mod_name) == 0) {
3300 			mutex_exit(&devname_nsmaps_lock);
3301 			return (map);
3302 		}
3303 	}
3304 	mutex_exit(&devname_nsmaps_lock);
3305 	return (NULL);
3306 }
3307 
3308 void
3309 sdev_invalidate_nsmaps()
3310 {
3311 	struct devname_nsmap *map = NULL;
3312 
3313 	ASSERT(MUTEX_HELD(&devname_nsmaps_lock));
3314 
3315 	if (devname_nsmaps == NULL)
3316 		return;
3317 
3318 	for (map = devname_nsmaps; map; map = map->next) {
3319 		rw_enter(&map->dir_lock, RW_WRITER);
3320 		map->dir_invalid = 1;
3321 		rw_exit(&map->dir_lock);
3322 	}
3323 	devname_nsmaps_invalidated = 1;
3324 }
3325 
3326 
3327 int
3328 sdev_nsmaps_loaded()
3329 {
3330 	int ret = 0;
3331 
3332 	mutex_enter(&devname_nsmaps_lock);
3333 	if (devname_nsmaps_loaded)
3334 		ret = 1;
3335 
3336 	mutex_exit(&devname_nsmaps_lock);
3337 	return (ret);
3338 }
3339 
3340 int
3341 sdev_nsmaps_reloaded()
3342 {
3343 	int ret = 0;
3344 
3345 	mutex_enter(&devname_nsmaps_lock);
3346 	if (devname_nsmaps_invalidated)
3347 		ret = 1;
3348 
3349 	mutex_exit(&devname_nsmaps_lock);
3350 	return (ret);
3351 }
3352 
3353 static void
3354 sdev_free_nsmap(struct devname_nsmap *map)
3355 {
3356 	ASSERT(map);
3357 	if (map->dir_name)
3358 		kmem_free(map->dir_name, strlen(map->dir_name) + 1);
3359 	if (map->dir_module)
3360 		kmem_free(map->dir_module, strlen(map->dir_module) + 1);
3361 	if (map->dir_map)
3362 		kmem_free(map->dir_map, strlen(map->dir_map) + 1);
3363 	rw_destroy(&map->dir_lock);
3364 	kmem_free(map, sizeof (*map));
3365 }
3366 
3367 void
3368 sdev_validate_nsmaps()
3369 {
3370 	struct devname_nsmap *map = NULL;
3371 	struct devname_nsmap *oldmap = NULL;
3372 
3373 	ASSERT(MUTEX_HELD(&devname_nsmaps_lock));
3374 	map = devname_nsmaps;
3375 	while (map) {
3376 		rw_enter(&map->dir_lock, RW_READER);
3377 		if ((map->dir_invalid == 1) && (map->dir_newmodule == NULL) &&
3378 		    (map->dir_newmap == NULL)) {
3379 			oldmap = map;
3380 			rw_exit(&map->dir_lock);
3381 			if (map->prev)
3382 				map->prev->next = oldmap->next;
3383 			if (map == devname_nsmaps)
3384 				devname_nsmaps = oldmap->next;
3385 
3386 			map = oldmap->next;
3387 			if (map)
3388 				map->prev = oldmap->prev;
3389 			sdev_free_nsmap(oldmap);
3390 			oldmap = NULL;
3391 		} else {
3392 			rw_exit(&map->dir_lock);
3393 			map = map->next;
3394 		}
3395 	}
3396 	devname_nsmaps_invalidated = 0;
3397 }
3398 
3399 static int
3400 sdev_map_is_invalid(struct devname_nsmap *map)
3401 {
3402 	int ret = 0;
3403 
3404 	ASSERT(map);
3405 	rw_enter(&map->dir_lock, RW_READER);
3406 	if (map->dir_invalid)
3407 		ret = 1;
3408 	rw_exit(&map->dir_lock);
3409 	return (ret);
3410 }
3411 
3412 static int
3413 sdev_check_map(struct devname_nsmap *map)
3414 {
3415 	struct devname_nsmap *mapp;
3416 
3417 	mutex_enter(&devname_nsmaps_lock);
3418 	if (devname_nsmaps == NULL) {
3419 		mutex_exit(&devname_nsmaps_lock);
3420 		return (1);
3421 	}
3422 
3423 	for (mapp = devname_nsmaps; mapp; mapp = mapp->next) {
3424 		if (mapp == map) {
3425 			mutex_exit(&devname_nsmaps_lock);
3426 			return (0);
3427 		}
3428 	}
3429 
3430 	mutex_exit(&devname_nsmaps_lock);
3431 	return (1);
3432 
3433 }
3434 
3435 struct devname_nsmap *
3436 sdev_get_map(struct sdev_node *dv, int validate)
3437 {
3438 	struct devname_nsmap *map;
3439 	int error;
3440 
3441 	ASSERT(RW_READ_HELD(&dv->sdev_contents));
3442 	map = dv->sdev_mapinfo;
3443 	if (map && sdev_check_map(map)) {
3444 		if (!rw_tryupgrade(&dv->sdev_contents)) {
3445 			rw_exit(&dv->sdev_contents);
3446 			rw_enter(&dv->sdev_contents, RW_WRITER);
3447 		}
3448 		dv->sdev_mapinfo = NULL;
3449 		rw_downgrade(&dv->sdev_contents);
3450 		return (NULL);
3451 	}
3452 
3453 	if (validate && (!map || (map && sdev_map_is_invalid(map)))) {
3454 		if (!rw_tryupgrade(&dv->sdev_contents)) {
3455 			rw_exit(&dv->sdev_contents);
3456 			rw_enter(&dv->sdev_contents, RW_WRITER);
3457 		}
3458 		error = sdev_get_moduleops(dv);
3459 		if (!error)
3460 			map = dv->sdev_mapinfo;
3461 		rw_downgrade(&dv->sdev_contents);
3462 	}
3463 	return (map);
3464 }
3465 
3466 void
3467 sdev_handle_alloc(struct sdev_node *dv)
3468 {
3469 	rw_enter(&dv->sdev_contents, RW_WRITER);
3470 	dv->sdev_handle.dh_data = dv;
3471 	rw_exit(&dv->sdev_contents);
3472 }
3473 
3474 
3475 extern int sdev_vnodeops_tbl_size;
3476 
3477 /*
3478  * construct a new template with overrides from vtab
3479  */
3480 static fs_operation_def_t *
3481 sdev_merge_vtab(const fs_operation_def_t tab[])
3482 {
3483 	fs_operation_def_t *new;
3484 	const fs_operation_def_t *tab_entry;
3485 
3486 	/* make a copy of standard vnode ops table */
3487 	new = kmem_alloc(sdev_vnodeops_tbl_size, KM_SLEEP);
3488 	bcopy((void *)sdev_vnodeops_tbl, new, sdev_vnodeops_tbl_size);
3489 
3490 	/* replace the overrides from tab */
3491 	for (tab_entry = tab; tab_entry->name != NULL; tab_entry++) {
3492 		fs_operation_def_t *std_entry = new;
3493 		while (std_entry->name) {
3494 			if (strcmp(tab_entry->name, std_entry->name) == 0) {
3495 				std_entry->func = tab_entry->func;
3496 				break;
3497 			}
3498 			std_entry++;
3499 		}
3500 		if (std_entry->name == NULL)
3501 			cmn_err(CE_NOTE, "sdev_merge_vtab: entry %s unused.",
3502 			    tab_entry->name);
3503 	}
3504 
3505 	return (new);
3506 }
3507 
3508 /* free memory allocated by sdev_merge_vtab */
3509 static void
3510 sdev_free_vtab(fs_operation_def_t *new)
3511 {
3512 	kmem_free(new, sdev_vnodeops_tbl_size);
3513 }
3514 
3515 void
3516 devname_get_vnode(devname_handle_t *hdl, vnode_t **vpp)
3517 {
3518 	struct sdev_node *dv = hdl->dh_data;
3519 
3520 	ASSERT(dv);
3521 
3522 	rw_enter(&dv->sdev_contents, RW_READER);
3523 	*vpp = SDEVTOV(dv);
3524 	rw_exit(&dv->sdev_contents);
3525 }
3526 
3527 int
3528 devname_get_path(devname_handle_t *hdl, char **path)
3529 {
3530 	struct sdev_node *dv = hdl->dh_data;
3531 
3532 	ASSERT(dv);
3533 
3534 	rw_enter(&dv->sdev_contents, RW_READER);
3535 	*path = dv->sdev_path;
3536 	rw_exit(&dv->sdev_contents);
3537 	return (0);
3538 }
3539 
3540 int
3541 devname_get_name(devname_handle_t *hdl, char **entry)
3542 {
3543 	struct sdev_node *dv = hdl->dh_data;
3544 
3545 	ASSERT(dv);
3546 	rw_enter(&dv->sdev_contents, RW_READER);
3547 	*entry = dv->sdev_name;
3548 	rw_exit(&dv->sdev_contents);
3549 	return (0);
3550 }
3551 
3552 void
3553 devname_get_dir_vnode(devname_handle_t *hdl, vnode_t **vpp)
3554 {
3555 	struct sdev_node *dv = hdl->dh_data->sdev_dotdot;
3556 
3557 	ASSERT(dv);
3558 
3559 	rw_enter(&dv->sdev_contents, RW_READER);
3560 	*vpp = SDEVTOV(dv);
3561 	rw_exit(&dv->sdev_contents);
3562 }
3563 
3564 int
3565 devname_get_dir_path(devname_handle_t *hdl, char **path)
3566 {
3567 	struct sdev_node *dv = hdl->dh_data->sdev_dotdot;
3568 
3569 	ASSERT(dv);
3570 	rw_enter(&dv->sdev_contents, RW_READER);
3571 	*path = dv->sdev_path;
3572 	rw_exit(&dv->sdev_contents);
3573 	return (0);
3574 }
3575 
3576 int
3577 devname_get_dir_name(devname_handle_t *hdl, char **entry)
3578 {
3579 	struct sdev_node *dv = hdl->dh_data->sdev_dotdot;
3580 
3581 	ASSERT(dv);
3582 	rw_enter(&dv->sdev_contents, RW_READER);
3583 	*entry = dv->sdev_name;
3584 	rw_exit(&dv->sdev_contents);
3585 	return (0);
3586 }
3587 
3588 int
3589 devname_get_dir_nsmap(devname_handle_t *hdl, struct devname_nsmap **map)
3590 {
3591 	struct sdev_node *dv = hdl->dh_data->sdev_dotdot;
3592 
3593 	ASSERT(dv);
3594 	rw_enter(&dv->sdev_contents, RW_READER);
3595 	*map = dv->sdev_mapinfo;
3596 	rw_exit(&dv->sdev_contents);
3597 	return (0);
3598 }
3599 
3600 int
3601 devname_get_dir_handle(devname_handle_t *hdl, devname_handle_t **dir_hdl)
3602 {
3603 	struct sdev_node *dv = hdl->dh_data->sdev_dotdot;
3604 
3605 	ASSERT(dv);
3606 	rw_enter(&dv->sdev_contents, RW_READER);
3607 	*dir_hdl = &(dv->sdev_handle);
3608 	rw_exit(&dv->sdev_contents);
3609 	return (0);
3610 }
3611 
3612 void
3613 devname_set_nodetype(devname_handle_t *hdl, void *args, int spec)
3614 {
3615 	struct sdev_node *dv = hdl->dh_data;
3616 
3617 	ASSERT(dv);
3618 	rw_enter(&dv->sdev_contents, RW_WRITER);
3619 	hdl->dh_spec = (devname_spec_t)spec;
3620 	hdl->dh_args = (void *)i_ddi_strdup((char *)args, KM_SLEEP);
3621 	rw_exit(&dv->sdev_contents);
3622 }
3623 
3624 /*
3625  * a generic setattr() function
3626  *
3627  * note: flags only supports AT_UID and AT_GID.
3628  *	 Future enhancements can be done for other types, e.g. AT_MODE
3629  */
3630 int
3631 devname_setattr_func(struct vnode *vp, struct vattr *vap, int flags,
3632     struct cred *cred, int (*callback)(struct sdev_node *, struct vattr *,
3633     int), int protocol)
3634 {
3635 	struct sdev_node	*dv = VTOSDEV(vp);
3636 	struct sdev_node	*parent = dv->sdev_dotdot;
3637 	struct vattr		*get;
3638 	uint_t			mask = vap->va_mask;
3639 	int 			error;
3640 
3641 	/* some sanity checks */
3642 	if (vap->va_mask & AT_NOSET)
3643 		return (EINVAL);
3644 
3645 	if (vap->va_mask & AT_SIZE) {
3646 		if (vp->v_type == VDIR) {
3647 			return (EISDIR);
3648 		}
3649 	}
3650 
3651 	/* no need to set attribute, but do not fail either */
3652 	ASSERT(parent);
3653 	rw_enter(&parent->sdev_contents, RW_READER);
3654 	if (dv->sdev_state == SDEV_ZOMBIE) {
3655 		rw_exit(&parent->sdev_contents);
3656 		return (0);
3657 	}
3658 
3659 	/* If backing store exists, just set it. */
3660 	if (dv->sdev_attrvp) {
3661 		rw_exit(&parent->sdev_contents);
3662 		return (VOP_SETATTR(dv->sdev_attrvp, vap, flags, cred, NULL));
3663 	}
3664 
3665 	/*
3666 	 * Otherwise, for nodes with the persistence attribute, create it.
3667 	 */
3668 	ASSERT(dv->sdev_attr);
3669 	if (SDEV_IS_PERSIST(dv) ||
3670 	    ((vap->va_mask & ~AT_TIMES) != 0 && !SDEV_IS_DYNAMIC(dv))) {
3671 		sdev_vattr_merge(dv, vap);
3672 		rw_enter(&dv->sdev_contents, RW_WRITER);
3673 		error = sdev_shadow_node(dv, cred);
3674 		rw_exit(&dv->sdev_contents);
3675 		rw_exit(&parent->sdev_contents);
3676 
3677 		if (error)
3678 			return (error);
3679 		return (VOP_SETATTR(dv->sdev_attrvp, vap, flags, cred, NULL));
3680 	}
3681 
3682 
3683 	/*
3684 	 * sdev_attr was allocated in sdev_mknode
3685 	 */
3686 	rw_enter(&dv->sdev_contents, RW_WRITER);
3687 	error = secpolicy_vnode_setattr(cred, vp, vap, dv->sdev_attr,
3688 	    flags, sdev_unlocked_access, dv);
3689 	if (error) {
3690 		rw_exit(&dv->sdev_contents);
3691 		rw_exit(&parent->sdev_contents);
3692 		return (error);
3693 	}
3694 
3695 	get = dv->sdev_attr;
3696 	if (mask & AT_MODE) {
3697 		get->va_mode &= S_IFMT;
3698 		get->va_mode |= vap->va_mode & ~S_IFMT;
3699 	}
3700 
3701 	if ((mask & AT_UID) || (mask & AT_GID)) {
3702 		if (mask & AT_UID)
3703 			get->va_uid = vap->va_uid;
3704 		if (mask & AT_GID)
3705 			get->va_gid = vap->va_gid;
3706 		/*
3707 		 * a callback must be provided if the protocol is set
3708 		 */
3709 		if ((protocol & AT_UID) || (protocol & AT_GID)) {
3710 			ASSERT(callback);
3711 			error = callback(dv, get, protocol);
3712 			if (error) {
3713 				rw_exit(&dv->sdev_contents);
3714 				rw_exit(&parent->sdev_contents);
3715 				return (error);
3716 			}
3717 		}
3718 	}
3719 
3720 	if (mask & AT_ATIME)
3721 		get->va_atime = vap->va_atime;
3722 	if (mask & AT_MTIME)
3723 		get->va_mtime = vap->va_mtime;
3724 	if (mask & (AT_MODE | AT_UID | AT_GID | AT_CTIME)) {
3725 		gethrestime(&get->va_ctime);
3726 	}
3727 
3728 	sdev_vattr_merge(dv, get);
3729 	rw_exit(&dv->sdev_contents);
3730 	rw_exit(&parent->sdev_contents);
3731 	return (0);
3732 }
3733