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