xref: /titanic_50/usr/src/uts/common/fs/dev/sdev_subr.c (revision 0209230bf1261579beab4f55226bb509e6b850cb)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * utility routines for the /dev fs
30  */
31 
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/t_lock.h>
35 #include <sys/systm.h>
36 #include <sys/sysmacros.h>
37 #include <sys/user.h>
38 #include <sys/time.h>
39 #include <sys/vfs.h>
40 #include <sys/vnode.h>
41 #include <sys/file.h>
42 #include <sys/fcntl.h>
43 #include <sys/flock.h>
44 #include <sys/kmem.h>
45 #include <sys/uio.h>
46 #include <sys/errno.h>
47 #include <sys/stat.h>
48 #include <sys/cred.h>
49 #include <sys/dirent.h>
50 #include <sys/pathname.h>
51 #include <sys/cmn_err.h>
52 #include <sys/debug.h>
53 #include <sys/mode.h>
54 #include <sys/policy.h>
55 #include <fs/fs_subr.h>
56 #include <sys/mount.h>
57 #include <sys/fs/snode.h>
58 #include <sys/fs/dv_node.h>
59 #include <sys/fs/sdev_impl.h>
60 #include <sys/fs/sdev_node.h>
61 #include <sys/sunndi.h>
62 #include <sys/sunmdi.h>
63 #include <sys/conf.h>
64 #include <sys/proc.h>
65 #include <sys/user.h>
66 #include <sys/modctl.h>
67 
68 #ifdef DEBUG
69 int sdev_debug = 0x00000001;
70 int sdev_debug_cache_flags = 0;
71 #endif
72 
73 /*
74  * globals
75  */
76 /* prototype memory vattrs */
77 vattr_t sdev_vattr_dir = {
78 	AT_TYPE|AT_MODE|AT_UID|AT_GID,		/* va_mask */
79 	VDIR,					/* va_type */
80 	SDEV_DIRMODE_DEFAULT,			/* va_mode */
81 	SDEV_UID_DEFAULT,			/* va_uid */
82 	SDEV_GID_DEFAULT,			/* va_gid */
83 	0,					/* va_fsid */
84 	0,					/* va_nodeid */
85 	0,					/* va_nlink */
86 	0,					/* va_size */
87 	0,					/* va_atime */
88 	0,					/* va_mtime */
89 	0,					/* va_ctime */
90 	0,					/* va_rdev */
91 	0,					/* va_blksize */
92 	0,					/* va_nblocks */
93 	0					/* va_vcode */
94 };
95 
96 vattr_t sdev_vattr_lnk = {
97 	AT_TYPE|AT_MODE,			/* va_mask */
98 	VLNK,					/* va_type */
99 	SDEV_LNKMODE_DEFAULT,			/* va_mode */
100 	SDEV_UID_DEFAULT,			/* va_uid */
101 	SDEV_GID_DEFAULT,			/* va_gid */
102 	0,					/* va_fsid */
103 	0,					/* va_nodeid */
104 	0,					/* va_nlink */
105 	0,					/* va_size */
106 	0,					/* va_atime */
107 	0,					/* va_mtime */
108 	0,					/* va_ctime */
109 	0,					/* va_rdev */
110 	0,					/* va_blksize */
111 	0,					/* va_nblocks */
112 	0					/* va_vcode */
113 };
114 
115 vattr_t sdev_vattr_blk = {
116 	AT_TYPE|AT_MODE|AT_UID|AT_GID,		/* va_mask */
117 	VBLK,					/* va_type */
118 	S_IFBLK | SDEV_DEVMODE_DEFAULT,		/* va_mode */
119 	SDEV_UID_DEFAULT,			/* va_uid */
120 	SDEV_GID_DEFAULT,			/* va_gid */
121 	0,					/* va_fsid */
122 	0,					/* va_nodeid */
123 	0,					/* va_nlink */
124 	0,					/* va_size */
125 	0,					/* va_atime */
126 	0,					/* va_mtime */
127 	0,					/* va_ctime */
128 	0,					/* va_rdev */
129 	0,					/* va_blksize */
130 	0,					/* va_nblocks */
131 	0					/* va_vcode */
132 };
133 
134 vattr_t sdev_vattr_chr = {
135 	AT_TYPE|AT_MODE|AT_UID|AT_GID,		/* va_mask */
136 	VCHR,					/* va_type */
137 	S_IFCHR | SDEV_DEVMODE_DEFAULT,		/* va_mode */
138 	SDEV_UID_DEFAULT,			/* va_uid */
139 	SDEV_GID_DEFAULT,			/* va_gid */
140 	0,					/* va_fsid */
141 	0,					/* va_nodeid */
142 	0,					/* va_nlink */
143 	0,					/* va_size */
144 	0,					/* va_atime */
145 	0,					/* va_mtime */
146 	0,					/* va_ctime */
147 	0,					/* va_rdev */
148 	0,					/* va_blksize */
149 	0,					/* va_nblocks */
150 	0					/* va_vcode */
151 };
152 
153 kmem_cache_t	*sdev_node_cache;	/* sdev_node cache */
154 int		devtype;		/* fstype */
155 
156 struct devname_ops *devname_ns_ops;	/* default name service directory ops */
157 kmutex_t devname_nsmaps_lock;	/* protect devname_nsmaps */
158 
159 /* static */
160 static struct devname_nsmap *devname_nsmaps = NULL;
161 				/* contents from /etc/dev/devname_master */
162 static int devname_nsmaps_invalidated = 0; /* "devfsadm -m" has run */
163 
164 static struct vnodeops *sdev_get_vop(struct sdev_node *);
165 static void sdev_set_no_nocache(struct sdev_node *);
166 static int sdev_get_moduleops(struct sdev_node *);
167 static 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 static int
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 	if (!(ddv->sdev_flags & SDEV_BUILD)) {
1607 		return (0);
1608 	}
1609 
1610 	ASSERT(RW_READ_HELD(&ddv->sdev_contents));
1611 	if (!rw_tryupgrade(&ddv->sdev_contents)) {
1612 		rw_exit(&ddv->sdev_contents);
1613 		rw_enter(&ddv->sdev_contents, RW_WRITER);
1614 	}
1615 
1616 	vap = sdev_getdefault_attr(VDIR);
1617 	for (i = 0; vtab[i].vt_name != NULL; i++) {
1618 		nm = vtab[i].vt_name;
1619 		ASSERT(RW_WRITE_HELD(&ddv->sdev_contents));
1620 		error = sdev_mknode(ddv, nm, &dv, vap, NULL,
1621 		    NULL, kcred, SDEV_READY);
1622 		if (error)
1623 			continue;
1624 		ASSERT(dv);
1625 		ASSERT(dv->sdev_state != SDEV_ZOMBIE);
1626 		SDEV_SIMPLE_RELE(dv);
1627 		dv = NULL;
1628 	}
1629 	rw_downgrade(&ddv->sdev_contents);
1630 	return (0);
1631 }
1632 
1633 /*
1634  * Creating a backing store entry based on sdev_attr.
1635  * This is called either as part of node creation in a persistent directory
1636  * or from setattr/setsecattr to persist access attributes across reboot.
1637  */
1638 int
1639 sdev_shadow_node(struct sdev_node *dv, struct cred *cred)
1640 {
1641 	int error = 0;
1642 	struct vnode *dvp = SDEVTOV(dv->sdev_dotdot);
1643 	struct vnode *rdvp = VTOSDEV(dvp)->sdev_attrvp;
1644 	struct vattr *vap = dv->sdev_attr;
1645 	char *nm = dv->sdev_name;
1646 	struct vnode *tmpvp, **rvp = &tmpvp, *rrvp = NULL;
1647 
1648 	ASSERT(dv && dv->sdev_name && rdvp);
1649 	ASSERT(RW_WRITE_HELD(&dv->sdev_contents) && dv->sdev_attrvp == NULL);
1650 
1651 lookup:
1652 	/* try to find it in the backing store */
1653 	error = VOP_LOOKUP(rdvp, nm, rvp, NULL, 0, NULL, cred);
1654 	if (error == 0) {
1655 		if (VOP_REALVP(*rvp, &rrvp) == 0) {
1656 			VN_HOLD(rrvp);
1657 			VN_RELE(*rvp);
1658 			*rvp = rrvp;
1659 		}
1660 
1661 		kmem_free(dv->sdev_attr, sizeof (vattr_t));
1662 		dv->sdev_attr = NULL;
1663 		dv->sdev_attrvp = *rvp;
1664 		return (0);
1665 	}
1666 
1667 	/* let's try to persist the node */
1668 	gethrestime(&vap->va_atime);
1669 	vap->va_mtime = vap->va_atime;
1670 	vap->va_ctime = vap->va_atime;
1671 	vap->va_mask |= AT_TYPE|AT_MODE;
1672 	switch (vap->va_type) {
1673 	case VDIR:
1674 		error = VOP_MKDIR(rdvp, nm, vap, rvp, cred);
1675 		sdcmn_err9(("sdev_shadow_node: mkdir vp %p error %d\n",
1676 		    (void *)(*rvp), error));
1677 		break;
1678 	case VCHR:
1679 	case VBLK:
1680 	case VREG:
1681 	case VDOOR:
1682 		error = VOP_CREATE(rdvp, nm, vap, NONEXCL, VREAD|VWRITE,
1683 		    rvp, cred, 0);
1684 		sdcmn_err9(("sdev_shadow_node: create vp %p, error %d\n",
1685 		    (void *)(*rvp), error));
1686 		if (!error)
1687 			VN_RELE(*rvp);
1688 		break;
1689 	case VLNK:
1690 		ASSERT(dv->sdev_symlink);
1691 		error = VOP_SYMLINK(rdvp, nm, vap, dv->sdev_symlink, cred);
1692 		sdcmn_err9(("sdev_shadow_node: create symlink error %d\n",
1693 		    error));
1694 		break;
1695 	default:
1696 		cmn_err(CE_PANIC, "dev: %s: sdev_shadow_node "
1697 		    "create\n", nm);
1698 		/*NOTREACHED*/
1699 	}
1700 
1701 	/* go back to lookup to factor out spec node and set attrvp */
1702 	if (error == 0)
1703 		goto lookup;
1704 
1705 	return (error);
1706 }
1707 
1708 static int
1709 sdev_cache_add(struct sdev_node *ddv, struct sdev_node **dv, char *nm)
1710 {
1711 	int error = 0;
1712 	struct sdev_node *dup = NULL;
1713 
1714 	ASSERT(RW_WRITE_HELD(&ddv->sdev_contents));
1715 	if ((dup = sdev_findbyname(ddv, nm)) == NULL) {
1716 		sdev_direnter(ddv, *dv);
1717 	} else {
1718 		if (dup->sdev_state == SDEV_ZOMBIE) {
1719 			error = sdev_dirdelete(ddv, dup);
1720 			/*
1721 			 * The ZOMBIE node is still hanging
1722 			 * around with more than one reference counts.
1723 			 * Fail the new node creation so that
1724 			 * the directory cache won't have
1725 			 * duplicate entries for the same named node
1726 			 */
1727 			if (error == EBUSY) {
1728 				SDEV_SIMPLE_RELE(*dv);
1729 				sdev_nodedestroy(*dv, 0);
1730 				*dv = NULL;
1731 				return (error);
1732 			}
1733 			sdev_direnter(ddv, *dv);
1734 		} else {
1735 			ASSERT((*dv)->sdev_state != SDEV_ZOMBIE);
1736 			SDEV_SIMPLE_RELE(*dv);
1737 			sdev_nodedestroy(*dv, 0);
1738 			*dv = dup;
1739 		}
1740 	}
1741 
1742 	return (0);
1743 }
1744 
1745 static int
1746 sdev_cache_delete(struct sdev_node *ddv, struct sdev_node **dv)
1747 {
1748 	ASSERT(RW_WRITE_HELD(&ddv->sdev_contents));
1749 	return (sdev_dirdelete(ddv, *dv));
1750 }
1751 
1752 /*
1753  * update the in-core directory cache
1754  */
1755 int
1756 sdev_cache_update(struct sdev_node *ddv, struct sdev_node **dv, char *nm,
1757     sdev_cache_ops_t ops)
1758 {
1759 	int error = 0;
1760 
1761 	ASSERT((SDEV_HELD(*dv)));
1762 
1763 	ASSERT(RW_WRITE_HELD(&ddv->sdev_contents));
1764 	switch (ops) {
1765 	case SDEV_CACHE_ADD:
1766 		error = sdev_cache_add(ddv, dv, nm);
1767 		break;
1768 	case SDEV_CACHE_DELETE:
1769 		error = sdev_cache_delete(ddv, dv);
1770 		break;
1771 	default:
1772 		break;
1773 	}
1774 
1775 	return (error);
1776 }
1777 
1778 /*
1779  * retrive the named entry from the directory cache
1780  */
1781 struct sdev_node *
1782 sdev_cache_lookup(struct sdev_node *ddv, char *nm)
1783 {
1784 	struct sdev_node *dv = NULL;
1785 
1786 	ASSERT(RW_LOCK_HELD(&ddv->sdev_contents));
1787 	dv = sdev_findbyname(ddv, nm);
1788 
1789 	return (dv);
1790 }
1791 
1792 /*
1793  * Implicit reconfig for nodes constructed by a link generator
1794  * Start devfsadm if needed, or if devfsadm is in progress,
1795  * prepare to block on devfsadm either completing or
1796  * constructing the desired node.  As devfsadmd is global
1797  * in scope, constructing all necessary nodes, we only
1798  * need to initiate it once.
1799  */
1800 static int
1801 sdev_call_devfsadmd(struct sdev_node *ddv, struct sdev_node *dv, char *nm)
1802 {
1803 	int error = 0;
1804 
1805 	if (DEVNAME_DEVFSADM_IS_RUNNING(devfsadm_state)) {
1806 		sdcmn_err6(("lookup: waiting for %s/%s, 0x%x\n",
1807 		    ddv->sdev_name, nm, devfsadm_state));
1808 		mutex_enter(&dv->sdev_lookup_lock);
1809 		SDEV_BLOCK_OTHERS(dv, (SDEV_LOOKUP | SDEV_LGWAITING));
1810 		mutex_exit(&dv->sdev_lookup_lock);
1811 		error = 0;
1812 	} else if (!DEVNAME_DEVFSADM_HAS_RUN(devfsadm_state)) {
1813 		sdcmn_err6(("lookup %s/%s starting devfsadm, 0x%x\n",
1814 			ddv->sdev_name, nm, devfsadm_state));
1815 
1816 		sdev_devfsadmd_thread(ddv, dv, kcred);
1817 		mutex_enter(&dv->sdev_lookup_lock);
1818 		SDEV_BLOCK_OTHERS(dv,
1819 		    (SDEV_LOOKUP | SDEV_LGWAITING));
1820 		mutex_exit(&dv->sdev_lookup_lock);
1821 		error = 0;
1822 	} else {
1823 		error = -1;
1824 	}
1825 
1826 	return (error);
1827 }
1828 
1829 static int
1830 sdev_call_modulelookup(struct sdev_node *ddv, struct sdev_node **dvp, char *nm,
1831     int (*fn)(char *, devname_handle_t *, struct cred *), struct cred *cred)
1832 {
1833 	struct vnode *rvp = NULL;
1834 	int error = 0;
1835 	struct vattr *vap;
1836 	devname_spec_t spec;
1837 	devname_handle_t *hdl;
1838 	void *args = NULL;
1839 	struct sdev_node *dv = *dvp;
1840 
1841 	ASSERT(dv && ddv);
1842 	hdl = &(dv->sdev_handle);
1843 	ASSERT(hdl->dh_data == dv);
1844 	mutex_enter(&dv->sdev_lookup_lock);
1845 	SDEV_BLOCK_OTHERS(dv, SDEV_LOOKUP);
1846 	mutex_exit(&dv->sdev_lookup_lock);
1847 	error = (*fn)(nm, hdl, cred);
1848 	if (error) {
1849 		return (error);
1850 	}
1851 
1852 	spec = hdl->dh_spec;
1853 	args = hdl->dh_args;
1854 	ASSERT(args);
1855 
1856 	switch (spec) {
1857 	case DEVNAME_NS_PATH:
1858 		/*
1859 		 * symlink of:
1860 		 *	/dev/dir/nm -> /device/...
1861 		 */
1862 		rvp = devname_configure_by_path((char *)args, NULL);
1863 		break;
1864 	case DEVNAME_NS_DEV:
1865 		/*
1866 		 * symlink of:
1867 		 *	/dev/dir/nm -> /dev/...
1868 		 */
1869 		rvp = devname_find_by_devpath((char *)args, NULL);
1870 		break;
1871 	default:
1872 		if (args)
1873 			kmem_free((char *)args, strlen(args) + 1);
1874 		return (ENOENT);
1875 
1876 	}
1877 
1878 	if (rvp == NULL) {
1879 		if (args)
1880 			kmem_free((char *)args, strlen(args) + 1);
1881 		return (ENOENT);
1882 	} else {
1883 		vap = sdev_getdefault_attr(VLNK);
1884 		ASSERT(RW_READ_HELD(&ddv->sdev_contents));
1885 		/*
1886 		 * Could sdev_mknode return a different dv_node
1887 		 * once the lock is dropped?
1888 		 */
1889 		if (!rw_tryupgrade(&ddv->sdev_contents)) {
1890 			rw_exit(&ddv->sdev_contents);
1891 			rw_enter(&ddv->sdev_contents, RW_WRITER);
1892 		}
1893 		error = sdev_mknode(ddv, nm, &dv, vap, NULL, args, cred,
1894 		    SDEV_READY);
1895 		rw_downgrade(&ddv->sdev_contents);
1896 		if (error) {
1897 			if (args)
1898 				kmem_free((char *)args, strlen(args) + 1);
1899 			return (error);
1900 		} else {
1901 			mutex_enter(&dv->sdev_lookup_lock);
1902 			SDEV_UNBLOCK_OTHERS(dv, SDEV_LOOKUP);
1903 			mutex_exit(&dv->sdev_lookup_lock);
1904 			error = 0;
1905 		}
1906 	}
1907 
1908 	if (args)
1909 		kmem_free((char *)args, strlen(args) + 1);
1910 
1911 	*dvp = dv;
1912 	return (0);
1913 }
1914 
1915 /*
1916  *  Support for specialized device naming construction mechanisms
1917  */
1918 static int
1919 sdev_call_dircallback(struct sdev_node *ddv, struct sdev_node **dvp, char *nm,
1920     int (*callback)(struct sdev_node *, char *, void **, struct cred *,
1921     void *, char *), int flags, struct cred *cred)
1922 {
1923 	int rv = 0;
1924 	char *physpath = NULL;
1925 	struct vnode *rvp = NULL;
1926 	struct vattr vattr;
1927 	struct vattr *vap;
1928 	struct sdev_node *dv = *dvp;
1929 
1930 	mutex_enter(&dv->sdev_lookup_lock);
1931 	SDEV_BLOCK_OTHERS(dv, SDEV_LOOKUP);
1932 	mutex_exit(&dv->sdev_lookup_lock);
1933 
1934 	/* for non-devfsadm devices */
1935 	if (flags & SDEV_PATH) {
1936 		physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
1937 		rv = callback(ddv, nm, (void *)&physpath, kcred, NULL,
1938 		    NULL);
1939 		if (rv) {
1940 			kmem_free(physpath, MAXPATHLEN);
1941 			return (-1);
1942 		}
1943 
1944 		ASSERT(physpath);
1945 		rvp = devname_configure_by_path(physpath, NULL);
1946 		if (rvp == NULL) {
1947 			sdcmn_err3(("devname_configure_by_path: "
1948 			    "failed for /dev/%s/%s\n",
1949 			    ddv->sdev_name, nm));
1950 			kmem_free(physpath, MAXPATHLEN);
1951 			rv = -1;
1952 		} else {
1953 			vap = sdev_getdefault_attr(VLNK);
1954 			ASSERT(RW_READ_HELD(&ddv->sdev_contents));
1955 
1956 			/*
1957 			 * Sdev_mknode may return back a different sdev_node
1958 			 * that was created by another thread that
1959 			 * raced to the directroy cache before this thread.
1960 			 *
1961 			 * With current directory cache mechanism
1962 			 * (linked list with the sdev_node name as
1963 			 * the entity key), this is a way to make sure
1964 			 * only one entry exists for the same name
1965 			 * in the same directory. The outcome is
1966 			 * the winner wins.
1967 			 */
1968 			if (!rw_tryupgrade(&ddv->sdev_contents)) {
1969 				rw_exit(&ddv->sdev_contents);
1970 				rw_enter(&ddv->sdev_contents, RW_WRITER);
1971 			}
1972 			rv = sdev_mknode(ddv, nm, &dv, vap, NULL,
1973 			    (void *)physpath, cred, SDEV_READY);
1974 			rw_downgrade(&ddv->sdev_contents);
1975 			kmem_free(physpath, MAXPATHLEN);
1976 			if (rv) {
1977 				return (rv);
1978 			} else {
1979 				mutex_enter(&dv->sdev_lookup_lock);
1980 				SDEV_UNBLOCK_OTHERS(dv, SDEV_LOOKUP);
1981 				mutex_exit(&dv->sdev_lookup_lock);
1982 				return (0);
1983 			}
1984 		}
1985 	} else if (flags & SDEV_VNODE) {
1986 		/*
1987 		 * DBNR has its own way to create the device
1988 		 * and return a backing store vnode in rvp
1989 		 */
1990 		ASSERT(callback);
1991 		rv = callback(ddv, nm, (void *)&rvp, kcred, NULL, NULL);
1992 		if (rv || (rvp == NULL)) {
1993 			sdcmn_err3(("devname_lookup_func: SDEV_VNODE "
1994 			    "callback failed \n"));
1995 			return (-1);
1996 		}
1997 		vap = sdev_getdefault_attr(rvp->v_type);
1998 		if (vap == NULL)
1999 			return (-1);
2000 
2001 		ASSERT(RW_READ_HELD(&ddv->sdev_contents));
2002 		if (!rw_tryupgrade(&ddv->sdev_contents)) {
2003 			rw_exit(&ddv->sdev_contents);
2004 			rw_enter(&ddv->sdev_contents, RW_WRITER);
2005 		}
2006 		rv = sdev_mknode(ddv, nm, &dv, vap, rvp, NULL,
2007 		    cred, SDEV_READY);
2008 		rw_downgrade(&ddv->sdev_contents);
2009 		if (rv)
2010 			return (rv);
2011 
2012 		mutex_enter(&dv->sdev_lookup_lock);
2013 		SDEV_UNBLOCK_OTHERS(dv, SDEV_LOOKUP);
2014 		mutex_exit(&dv->sdev_lookup_lock);
2015 		return (0);
2016 	} else if (flags & SDEV_VATTR) {
2017 		/*
2018 		 * /dev/pts
2019 		 *
2020 		 * DBNR has its own way to create the device
2021 		 * "0" is returned upon success.
2022 		 *
2023 		 * callback is responsible to set the basic attributes,
2024 		 * e.g. va_type/va_uid/va_gid/
2025 		 *    dev_t if VCHR or VBLK/
2026 		 */
2027 		ASSERT(callback);
2028 		rv = callback(ddv, nm, (void *)&vattr, kcred, NULL, NULL);
2029 		if (rv) {
2030 			sdcmn_err3(("devname_lookup_func: SDEV_NONE "
2031 			    "callback failed \n"));
2032 			return (-1);
2033 		}
2034 
2035 		ASSERT(RW_READ_HELD(&ddv->sdev_contents));
2036 		if (!rw_tryupgrade(&ddv->sdev_contents)) {
2037 			rw_exit(&ddv->sdev_contents);
2038 			rw_enter(&ddv->sdev_contents, RW_WRITER);
2039 		}
2040 		rv = sdev_mknode(ddv, nm, &dv, &vattr, NULL, NULL,
2041 		    cred, SDEV_READY);
2042 		rw_downgrade(&ddv->sdev_contents);
2043 
2044 		if (rv)
2045 			return (rv);
2046 
2047 		mutex_enter(&dv->sdev_lookup_lock);
2048 		SDEV_UNBLOCK_OTHERS(dv, SDEV_LOOKUP);
2049 		mutex_exit(&dv->sdev_lookup_lock);
2050 		return (0);
2051 	} else {
2052 		impossible(("lookup: %s/%s by %s not supported (%d)\n",
2053 		    SDEVTOV(ddv)->v_path, nm, curproc->p_user.u_comm,
2054 		    __LINE__));
2055 		rv = -1;
2056 	}
2057 
2058 	*dvp = dv;
2059 	return (rv);
2060 }
2061 
2062 static int
2063 is_devfsadm_thread(char *exec_name)
2064 {
2065 	/*
2066 	 * note: because devfsadmd -> /usr/sbin/devfsadm
2067 	 * it is safe to use "devfsadm" to capture the lookups
2068 	 * from devfsadm and its daemon version.
2069 	 */
2070 	if (strcmp(exec_name, "devfsadm") == 0)
2071 		return (1);
2072 	return (0);
2073 }
2074 
2075 
2076 /*
2077  * Lookup Order:
2078  *	sdev_node cache;
2079  *	backing store (SDEV_PERSIST);
2080  *	DBNR: a. dir_ops implemented in the loadable modules;
2081  *	      b. vnode ops in vtab.
2082  */
2083 int
2084 devname_lookup_func(struct sdev_node *ddv, char *nm, struct vnode **vpp,
2085     struct cred *cred, int (*callback)(struct sdev_node *, char *, void **,
2086     struct cred *, void *, char *), int flags)
2087 {
2088 	int rv = 0, nmlen;
2089 	struct vnode *rvp = NULL;
2090 	struct sdev_node *dv = NULL;
2091 	int	retried = 0;
2092 	int	error = 0;
2093 	struct devname_nsmap *map = NULL;
2094 	struct devname_ops *dirops = NULL;
2095 	int (*fn)(char *, devname_handle_t *, struct cred *) = NULL;
2096 	struct vattr vattr;
2097 	char *lookup_thread = curproc->p_user.u_comm;
2098 	int failed_flags = 0;
2099 	int (*vtor)(struct sdev_node *) = NULL;
2100 	int state;
2101 	int parent_state;
2102 	char *link = NULL;
2103 
2104 	if (SDEVTOV(ddv)->v_type != VDIR)
2105 		return (ENOTDIR);
2106 
2107 	/*
2108 	 * Empty name or ., return node itself.
2109 	 */
2110 	nmlen = strlen(nm);
2111 	if ((nmlen == 0) || ((nmlen == 1) && (nm[0] == '.'))) {
2112 		*vpp = SDEVTOV(ddv);
2113 		VN_HOLD(*vpp);
2114 		return (0);
2115 	}
2116 
2117 	/*
2118 	 * .., return the parent directory
2119 	 */
2120 	if ((nmlen == 2) && (strcmp(nm, "..") == 0)) {
2121 		*vpp = SDEVTOV(ddv->sdev_dotdot);
2122 		VN_HOLD(*vpp);
2123 		return (0);
2124 	}
2125 
2126 	rw_enter(&ddv->sdev_contents, RW_READER);
2127 	if (ddv->sdev_flags & SDEV_VTOR) {
2128 		vtor = (int (*)(struct sdev_node *))sdev_get_vtor(ddv);
2129 		ASSERT(vtor);
2130 	}
2131 
2132 tryagain:
2133 	/*
2134 	 * (a) directory cache lookup:
2135 	 */
2136 	ASSERT(RW_READ_HELD(&ddv->sdev_contents));
2137 	parent_state = ddv->sdev_state;
2138 	dv = sdev_cache_lookup(ddv, nm);
2139 	if (dv) {
2140 		state = dv->sdev_state;
2141 		switch (state) {
2142 		case SDEV_INIT:
2143 			if (is_devfsadm_thread(lookup_thread))
2144 				break;
2145 
2146 			/* ZOMBIED parent won't allow node creation */
2147 			if (parent_state == SDEV_ZOMBIE) {
2148 				SD_TRACE_FAILED_LOOKUP(ddv, nm,
2149 				    retried);
2150 				goto nolock_notfound;
2151 			}
2152 
2153 			mutex_enter(&dv->sdev_lookup_lock);
2154 			/* compensate the threads started after devfsadm */
2155 			if (DEVNAME_DEVFSADM_IS_RUNNING(devfsadm_state) &&
2156 			    !(SDEV_IS_LOOKUP(dv)))
2157 				SDEV_BLOCK_OTHERS(dv,
2158 				    (SDEV_LOOKUP | SDEV_LGWAITING));
2159 
2160 			if (SDEV_IS_LOOKUP(dv)) {
2161 				failed_flags |= SLF_REBUILT;
2162 				rw_exit(&ddv->sdev_contents);
2163 				error = sdev_wait4lookup(dv, SDEV_LOOKUP);
2164 				mutex_exit(&dv->sdev_lookup_lock);
2165 				rw_enter(&ddv->sdev_contents, RW_READER);
2166 
2167 				if (error != 0) {
2168 					SD_TRACE_FAILED_LOOKUP(ddv, nm,
2169 					    retried);
2170 					goto nolock_notfound;
2171 				}
2172 
2173 				state = dv->sdev_state;
2174 				if (state == SDEV_INIT) {
2175 					SD_TRACE_FAILED_LOOKUP(ddv, nm,
2176 					    retried);
2177 					goto nolock_notfound;
2178 				} else if (state == SDEV_READY) {
2179 					goto found;
2180 				} else if (state == SDEV_ZOMBIE) {
2181 					rw_exit(&ddv->sdev_contents);
2182 					SD_TRACE_FAILED_LOOKUP(ddv, nm,
2183 					    retried);
2184 					SDEV_RELE(dv);
2185 					goto lookup_failed;
2186 				}
2187 			} else {
2188 				mutex_exit(&dv->sdev_lookup_lock);
2189 			}
2190 			break;
2191 		case SDEV_READY:
2192 			goto found;
2193 		case SDEV_ZOMBIE:
2194 			rw_exit(&ddv->sdev_contents);
2195 			SD_TRACE_FAILED_LOOKUP(ddv, nm, retried);
2196 			SDEV_RELE(dv);
2197 			goto lookup_failed;
2198 		default:
2199 			rw_exit(&ddv->sdev_contents);
2200 			SD_TRACE_FAILED_LOOKUP(ddv, nm, retried);
2201 			sdev_lookup_failed(ddv, nm, failed_flags);
2202 			*vpp = NULLVP;
2203 			return (ENOENT);
2204 		}
2205 	}
2206 	ASSERT(RW_READ_HELD(&ddv->sdev_contents));
2207 
2208 	/*
2209 	 * ZOMBIED parent does not allow new node creation.
2210 	 * bail out early
2211 	 */
2212 	if (parent_state == SDEV_ZOMBIE) {
2213 		rw_exit(&ddv->sdev_contents);
2214 		*vpp = NULL;
2215 		SD_TRACE_FAILED_LOOKUP(ddv, nm, retried);
2216 		return (ENOENT);
2217 	}
2218 
2219 	/*
2220 	 * (b0): backing store lookup
2221 	 *	SDEV_PERSIST is default except:
2222 	 *		1) pts nodes
2223 	 *		2) non-chmod'ed local nodes
2224 	 */
2225 	if (SDEV_IS_PERSIST(ddv)) {
2226 		error = devname_backstore_lookup(ddv, nm, &rvp);
2227 
2228 		if (!error) {
2229 			sdcmn_err3(("devname_backstore_lookup: "
2230 			    "found attrvp %p for %s\n", (void *)rvp, nm));
2231 
2232 			vattr.va_mask = AT_MODE|AT_UID|AT_GID;
2233 			error = VOP_GETATTR(rvp, &vattr, 0, cred);
2234 			if (error) {
2235 				rw_exit(&ddv->sdev_contents);
2236 				if (dv)
2237 					SDEV_RELE(dv);
2238 				SD_TRACE_FAILED_LOOKUP(ddv, nm, retried);
2239 				sdev_lookup_failed(ddv, nm, failed_flags);
2240 				*vpp = NULLVP;
2241 				return (ENOENT);
2242 			}
2243 
2244 			if (vattr.va_type == VLNK) {
2245 				error = sdev_getlink(rvp, &link);
2246 				if (error) {
2247 					rw_exit(&ddv->sdev_contents);
2248 					if (dv)
2249 						SDEV_RELE(dv);
2250 					SD_TRACE_FAILED_LOOKUP(ddv, nm,
2251 					    retried);
2252 					sdev_lookup_failed(ddv, nm,
2253 					    failed_flags);
2254 					*vpp = NULLVP;
2255 					return (ENOENT);
2256 				}
2257 				ASSERT(link != NULL);
2258 			}
2259 
2260 			if (!rw_tryupgrade(&ddv->sdev_contents)) {
2261 				rw_exit(&ddv->sdev_contents);
2262 				rw_enter(&ddv->sdev_contents, RW_WRITER);
2263 			}
2264 			error = sdev_mknode(ddv, nm, &dv, &vattr,
2265 			    rvp, link, cred, SDEV_READY);
2266 			rw_downgrade(&ddv->sdev_contents);
2267 
2268 			if (link != NULL) {
2269 				kmem_free(link, strlen(link) + 1);
2270 				link = NULL;
2271 			}
2272 
2273 			if (error) {
2274 				SD_TRACE_FAILED_LOOKUP(ddv, nm, retried);
2275 				rw_exit(&ddv->sdev_contents);
2276 				if (dv)
2277 					SDEV_RELE(dv);
2278 				goto lookup_failed;
2279 			} else {
2280 				goto found;
2281 			}
2282 		} else if (retried) {
2283 			rw_exit(&ddv->sdev_contents);
2284 			sdcmn_err3(("retry of lookup of %s/%s: failed\n",
2285 			    ddv->sdev_name, nm));
2286 			if (dv)
2287 				SDEV_RELE(dv);
2288 			SD_TRACE_FAILED_LOOKUP(ddv, nm, retried);
2289 			sdev_lookup_failed(ddv, nm, failed_flags);
2290 			*vpp = NULLVP;
2291 			return (ENOENT);
2292 		}
2293 	}
2294 
2295 
2296 	/* first thread that is doing the lookup on this node */
2297 	if (!dv) {
2298 		if (!rw_tryupgrade(&ddv->sdev_contents)) {
2299 			rw_exit(&ddv->sdev_contents);
2300 			rw_enter(&ddv->sdev_contents, RW_WRITER);
2301 		}
2302 		error = sdev_mknode(ddv, nm, &dv, NULL, NULL, NULL,
2303 		    cred, SDEV_INIT);
2304 		if (!dv) {
2305 			rw_exit(&ddv->sdev_contents);
2306 			SD_TRACE_FAILED_LOOKUP(ddv, nm, retried);
2307 			sdev_lookup_failed(ddv, nm, failed_flags);
2308 			*vpp = NULLVP;
2309 			return (ENOENT);
2310 		}
2311 		rw_downgrade(&ddv->sdev_contents);
2312 	}
2313 	ASSERT(dv);
2314 	ASSERT(SDEV_HELD(dv));
2315 
2316 	if (SDEV_IS_NO_NCACHE(dv)) {
2317 		failed_flags |= SLF_NO_NCACHE;
2318 	}
2319 
2320 	if (SDEV_IS_GLOBAL(ddv)) {
2321 		map = sdev_get_map(ddv, 1);
2322 		dirops = map ? map->dir_ops : NULL;
2323 		fn = dirops ? dirops->devnops_lookup : NULL;
2324 	}
2325 
2326 	/*
2327 	 * (b1) invoking devfsadm once per life time for devfsadm nodes
2328 	 */
2329 	if ((fn == NULL) && !callback) {
2330 
2331 		if (sdev_reconfig_boot || !i_ddi_io_initialized() ||
2332 		    SDEV_IS_DYNAMIC(ddv) || SDEV_IS_NO_NCACHE(dv) ||
2333 		    ((moddebug & MODDEBUG_FINI_EBUSY) != 0)) {
2334 			ASSERT(SDEV_HELD(dv));
2335 			SD_TRACE_FAILED_LOOKUP(ddv, nm, retried);
2336 			goto nolock_notfound;
2337 		}
2338 
2339 		/*
2340 		 * filter out known non-existent devices recorded
2341 		 * during initial reconfiguration boot for which
2342 		 * reconfig should not be done and lookup may
2343 		 * be short-circuited now.
2344 		 */
2345 		if (sdev_lookup_filter(ddv, nm)) {
2346 			SD_TRACE_FAILED_LOOKUP(ddv, nm, retried);
2347 			goto nolock_notfound;
2348 		}
2349 
2350 		/* bypassing devfsadm internal nodes */
2351 		if (is_devfsadm_thread(lookup_thread)) {
2352 			SD_TRACE_FAILED_LOOKUP(ddv, nm, retried);
2353 			goto nolock_notfound;
2354 		}
2355 
2356 		if (sdev_reconfig_disable) {
2357 			SD_TRACE_FAILED_LOOKUP(ddv, nm, retried);
2358 			goto nolock_notfound;
2359 		}
2360 
2361 		error = sdev_call_devfsadmd(ddv, dv, nm);
2362 		if (error == 0) {
2363 			sdcmn_err8(("lookup of %s/%s by %s: reconfig\n",
2364 			    ddv->sdev_name, nm, curproc->p_user.u_comm));
2365 			if (sdev_reconfig_verbose) {
2366 				cmn_err(CE_CONT,
2367 				    "?lookup of %s/%s by %s: reconfig\n",
2368 				    ddv->sdev_name, nm, curproc->p_user.u_comm);
2369 			}
2370 			retried = 1;
2371 			failed_flags |= SLF_REBUILT;
2372 			ASSERT(dv->sdev_state != SDEV_ZOMBIE);
2373 			SDEV_SIMPLE_RELE(dv);
2374 			goto tryagain;
2375 		} else {
2376 			SD_TRACE_FAILED_LOOKUP(ddv, nm, retried);
2377 			goto nolock_notfound;
2378 		}
2379 	}
2380 
2381 	/*
2382 	 * (b2) Directory Based Name Resolution (DBNR):
2383 	 *	ddv	- parent
2384 	 *	nm	- /dev/(ddv->sdev_name)/nm
2385 	 *
2386 	 *	note: module vnode ops take precedence than the build-in ones
2387 	 */
2388 	if (fn) {
2389 		error = sdev_call_modulelookup(ddv, &dv, nm, fn, cred);
2390 		if (error) {
2391 			SD_TRACE_FAILED_LOOKUP(ddv, nm, retried);
2392 			goto notfound;
2393 		} else {
2394 			goto found;
2395 		}
2396 	} else if (callback) {
2397 		error = sdev_call_dircallback(ddv, &dv, nm, callback,
2398 		    flags, cred);
2399 		if (error == 0) {
2400 			goto found;
2401 		} else {
2402 			SD_TRACE_FAILED_LOOKUP(ddv, nm, retried);
2403 			goto notfound;
2404 		}
2405 	}
2406 	ASSERT(rvp);
2407 
2408 found:
2409 	ASSERT(!(dv->sdev_flags & SDEV_STALE));
2410 	ASSERT(dv->sdev_state == SDEV_READY);
2411 	if (vtor) {
2412 		/*
2413 		 * Check validity of returned node
2414 		 */
2415 		switch (vtor(dv)) {
2416 		case SDEV_VTOR_VALID:
2417 			break;
2418 		case SDEV_VTOR_INVALID:
2419 			SD_TRACE_FAILED_LOOKUP(ddv, nm, retried);
2420 			sdcmn_err7(("lookup: destroy invalid "
2421 			    "node: %s(%p)\n", dv->sdev_name, (void *)dv));
2422 			goto nolock_notfound;
2423 		case SDEV_VTOR_SKIP:
2424 			sdcmn_err7(("lookup: node not applicable - "
2425 			    "skipping: %s(%p)\n", dv->sdev_name, (void *)dv));
2426 			rw_exit(&ddv->sdev_contents);
2427 			SD_TRACE_FAILED_LOOKUP(ddv, nm, retried);
2428 			SDEV_RELE(dv);
2429 			goto lookup_failed;
2430 		default:
2431 			cmn_err(CE_PANIC,
2432 			    "dev fs: validator failed: %s(%p)\n",
2433 			    dv->sdev_name, (void *)dv);
2434 			break;
2435 			/*NOTREACHED*/
2436 		}
2437 	}
2438 
2439 	if ((SDEVTOV(dv)->v_type == VDIR) && SDEV_IS_GLOBAL(dv)) {
2440 		rw_enter(&dv->sdev_contents, RW_READER);
2441 		(void) sdev_get_map(dv, 1);
2442 		rw_exit(&dv->sdev_contents);
2443 	}
2444 	rw_exit(&ddv->sdev_contents);
2445 	rv = sdev_to_vp(dv, vpp);
2446 	sdcmn_err3(("devname_lookup_func: returning vp %p v_count %d state %d "
2447 	    "for nm %s, error %d\n", (void *)*vpp, (*vpp)->v_count,
2448 	    dv->sdev_state, nm, rv));
2449 	return (rv);
2450 
2451 notfound:
2452 	mutex_enter(&dv->sdev_lookup_lock);
2453 	SDEV_UNBLOCK_OTHERS(dv, SDEV_LOOKUP);
2454 	mutex_exit(&dv->sdev_lookup_lock);
2455 nolock_notfound:
2456 	/*
2457 	 * Destroy the node that is created for synchronization purposes.
2458 	 */
2459 	sdcmn_err3(("devname_lookup_func: %s with state %d\n",
2460 	    nm, dv->sdev_state));
2461 	ASSERT(RW_READ_HELD(&ddv->sdev_contents));
2462 	if (dv->sdev_state == SDEV_INIT) {
2463 		if (!rw_tryupgrade(&ddv->sdev_contents)) {
2464 			rw_exit(&ddv->sdev_contents);
2465 			rw_enter(&ddv->sdev_contents, RW_WRITER);
2466 		}
2467 
2468 		/*
2469 		 * Node state may have changed during the lock
2470 		 * changes. Re-check.
2471 		 */
2472 		if (dv->sdev_state == SDEV_INIT) {
2473 			(void) sdev_dirdelete(ddv, dv);
2474 			rw_exit(&ddv->sdev_contents);
2475 			sdev_lookup_failed(ddv, nm, failed_flags);
2476 			*vpp = NULL;
2477 			return (ENOENT);
2478 		}
2479 	}
2480 
2481 	rw_exit(&ddv->sdev_contents);
2482 	SDEV_RELE(dv);
2483 
2484 lookup_failed:
2485 	sdev_lookup_failed(ddv, nm, failed_flags);
2486 	*vpp = NULL;
2487 	return (ENOENT);
2488 }
2489 
2490 /*
2491  * Given a directory node, mark all nodes beneath as
2492  * STALE, i.e. nodes that don't exist as far as new
2493  * consumers are concerned
2494  */
2495 void
2496 sdev_stale(struct sdev_node *ddv)
2497 {
2498 	struct sdev_node *dv;
2499 	struct vnode *vp;
2500 
2501 	ASSERT(SDEVTOV(ddv)->v_type == VDIR);
2502 
2503 	rw_enter(&ddv->sdev_contents, RW_WRITER);
2504 	for (dv = ddv->sdev_dot; dv; dv = dv->sdev_next) {
2505 		vp = SDEVTOV(dv);
2506 		if (vp->v_type == VDIR)
2507 			sdev_stale(dv);
2508 
2509 		sdcmn_err9(("sdev_stale: setting stale %s\n",
2510 		    dv->sdev_name));
2511 		dv->sdev_flags |= SDEV_STALE;
2512 	}
2513 	ddv->sdev_flags |= SDEV_BUILD;
2514 	rw_exit(&ddv->sdev_contents);
2515 }
2516 
2517 /*
2518  * Given a directory node, clean out all the nodes beneath.
2519  * If expr is specified, clean node with names matching expr.
2520  * If SDEV_ENFORCE is specified in flags, busy nodes are made stale,
2521  *	so they are excluded from future lookups.
2522  */
2523 int
2524 sdev_cleandir(struct sdev_node *ddv, char *expr, uint_t flags)
2525 {
2526 	int error = 0;
2527 	int busy = 0;
2528 	struct vnode *vp;
2529 	struct sdev_node *dv, *next = NULL;
2530 	int bkstore = 0;
2531 	int len = 0;
2532 	char *bks_name = NULL;
2533 
2534 	ASSERT(SDEVTOV(ddv)->v_type == VDIR);
2535 
2536 	/*
2537 	 * We try our best to destroy all unused sdev_node's
2538 	 */
2539 	rw_enter(&ddv->sdev_contents, RW_WRITER);
2540 	for (dv = ddv->sdev_dot; dv; dv = next) {
2541 		next = dv->sdev_next;
2542 		vp = SDEVTOV(dv);
2543 
2544 		if (expr && gmatch(dv->sdev_name, expr) == 0)
2545 			continue;
2546 
2547 		if (vp->v_type == VDIR &&
2548 		    sdev_cleandir(dv, NULL, flags) != 0) {
2549 			sdcmn_err9(("sdev_cleandir: dir %s busy\n",
2550 			    dv->sdev_name));
2551 			busy++;
2552 			continue;
2553 		}
2554 
2555 		if (vp->v_count > 0 && (flags & SDEV_ENFORCE) == 0) {
2556 			sdcmn_err9(("sdev_cleandir: dir %s busy\n",
2557 			    dv->sdev_name));
2558 			busy++;
2559 			continue;
2560 		}
2561 
2562 		/*
2563 		 * at this point, either dv is not held or SDEV_ENFORCE
2564 		 * is specified. In either case, dv needs to be deleted
2565 		 */
2566 		SDEV_HOLD(dv);
2567 
2568 		bkstore = SDEV_IS_PERSIST(dv) ? 1 : 0;
2569 		if (bkstore && (vp->v_type == VDIR))
2570 			bkstore += 1;
2571 
2572 		if (bkstore) {
2573 			len = strlen(dv->sdev_name) + 1;
2574 			bks_name = kmem_alloc(len, KM_SLEEP);
2575 			bcopy(dv->sdev_name, bks_name, len);
2576 		}
2577 
2578 		error = sdev_dirdelete(ddv, dv);
2579 
2580 		if (error == EBUSY) {
2581 			sdcmn_err9(("sdev_cleandir: dir busy\n"));
2582 			busy++;
2583 		}
2584 
2585 		/* take care the backing store clean up */
2586 		if (bkstore && (error == 0)) {
2587 			ASSERT(bks_name);
2588 			ASSERT(ddv->sdev_attrvp);
2589 
2590 			if (bkstore == 1) {
2591 				error = VOP_REMOVE(ddv->sdev_attrvp,
2592 				    bks_name, kcred);
2593 			} else if (bkstore == 2) {
2594 				error = VOP_RMDIR(ddv->sdev_attrvp,
2595 				    bks_name, ddv->sdev_attrvp, kcred);
2596 			}
2597 
2598 			/* do not propagate the backing store errors */
2599 			if (error) {
2600 				sdcmn_err9(("sdev_cleandir: backing store"
2601 				    "not cleaned\n"));
2602 				error = 0;
2603 			}
2604 
2605 			bkstore = 0;
2606 			kmem_free(bks_name, len);
2607 			bks_name = NULL;
2608 			len = 0;
2609 		}
2610 	}
2611 
2612 	ddv->sdev_flags |= SDEV_BUILD;
2613 	rw_exit(&ddv->sdev_contents);
2614 
2615 	if (busy) {
2616 		error = EBUSY;
2617 	}
2618 
2619 	return (error);
2620 }
2621 
2622 /*
2623  * a convenient wrapper for readdir() funcs
2624  */
2625 size_t
2626 add_dir_entry(dirent64_t *de, char *nm, size_t size, ino_t ino, offset_t off)
2627 {
2628 	size_t reclen = DIRENT64_RECLEN(strlen(nm));
2629 	if (reclen > size)
2630 		return (0);
2631 
2632 	de->d_ino = (ino64_t)ino;
2633 	de->d_off = (off64_t)off + 1;
2634 	de->d_reclen = (ushort_t)reclen;
2635 	(void) strncpy(de->d_name, nm, DIRENT64_NAMELEN(reclen));
2636 	return (reclen);
2637 }
2638 
2639 /*
2640  * sdev_mount service routines
2641  */
2642 int
2643 sdev_copyin_mountargs(struct mounta *uap, struct sdev_mountargs *args)
2644 {
2645 	int	error;
2646 
2647 	if (uap->datalen != sizeof (*args))
2648 		return (EINVAL);
2649 
2650 	if (error = copyin(uap->dataptr, args, sizeof (*args))) {
2651 		cmn_err(CE_WARN, "sdev_copyin_mountargs: can not"
2652 		    "get user data. error %d\n", error);
2653 		return (EFAULT);
2654 	}
2655 
2656 	return (0);
2657 }
2658 
2659 #ifdef nextdp
2660 #undef nextdp
2661 #endif
2662 #define	nextdp(dp)	((struct dirent64 *) \
2663 			    (intptr_t)((char *)(dp) + (dp)->d_reclen))
2664 
2665 /*
2666  * readdir helper func
2667  */
2668 int
2669 devname_readdir_func(vnode_t *vp, uio_t *uiop, cred_t *cred, int *eofp,
2670     int flags)
2671 {
2672 	struct sdev_node *ddv = VTOSDEV(vp);
2673 	struct sdev_node *dv;
2674 	dirent64_t	*dp;
2675 	ulong_t		outcount = 0;
2676 	size_t		namelen;
2677 	ulong_t		alloc_count;
2678 	void		*outbuf;
2679 	struct iovec	*iovp;
2680 	int		error = 0;
2681 	size_t		reclen;
2682 	offset_t	diroff;
2683 	offset_t	soff;
2684 	int		this_reclen;
2685 	struct devname_nsmap	*map = NULL;
2686 	struct devname_ops	*dirops = NULL;
2687 	int (*fn)(devname_handle_t *, struct cred *) = NULL;
2688 	int (*vtor)(struct sdev_node *) = NULL;
2689 	struct vattr attr;
2690 	timestruc_t now;
2691 
2692 	ASSERT(ddv->sdev_attr || ddv->sdev_attrvp);
2693 	ASSERT(RW_READ_HELD(&ddv->sdev_contents));
2694 
2695 	if (uiop->uio_loffset >= MAXOFF_T) {
2696 		if (eofp)
2697 			*eofp = 1;
2698 		return (0);
2699 	}
2700 
2701 	if (uiop->uio_iovcnt != 1)
2702 		return (EINVAL);
2703 
2704 	if (vp->v_type != VDIR)
2705 		return (ENOTDIR);
2706 
2707 	if (ddv->sdev_flags & SDEV_VTOR) {
2708 		vtor = (int (*)(struct sdev_node *))sdev_get_vtor(ddv);
2709 		ASSERT(vtor);
2710 	}
2711 
2712 	if (eofp != NULL)
2713 		*eofp = 0;
2714 
2715 	soff = uiop->uio_loffset;
2716 	iovp = uiop->uio_iov;
2717 	alloc_count = iovp->iov_len;
2718 	dp = outbuf = kmem_alloc(alloc_count, KM_SLEEP);
2719 	outcount = 0;
2720 
2721 	if (ddv->sdev_state == SDEV_ZOMBIE)
2722 		goto get_cache;
2723 
2724 	if (SDEV_IS_GLOBAL(ddv)) {
2725 		map = sdev_get_map(ddv, 0);
2726 		dirops = map ? map->dir_ops : NULL;
2727 		fn = dirops ? dirops->devnops_readdir : NULL;
2728 
2729 		if (map && map->dir_map) {
2730 			/*
2731 			 * load the name mapping rule database
2732 			 * through invoking devfsadm and symlink
2733 			 * all the entries in the map
2734 			 */
2735 			devname_rdr_result_t rdr_result;
2736 			int do_thread = 0;
2737 
2738 			rw_enter(&map->dir_lock, RW_READER);
2739 			do_thread = map->dir_maploaded ? 0 : 1;
2740 			rw_exit(&map->dir_lock);
2741 
2742 			if (do_thread) {
2743 				mutex_enter(&ddv->sdev_lookup_lock);
2744 				SDEV_BLOCK_OTHERS(ddv, SDEV_READDIR);
2745 				mutex_exit(&ddv->sdev_lookup_lock);
2746 
2747 				sdev_dispatch_to_nsrdr_thread(ddv,
2748 				    map->dir_map, &rdr_result);
2749 			}
2750 		} else if ((sdev_boot_state == SDEV_BOOT_STATE_COMPLETE) &&
2751 		    !sdev_reconfig_boot && (flags & SDEV_BROWSE) &&
2752 		    !SDEV_IS_DYNAMIC(ddv) && !SDEV_IS_NO_NCACHE(ddv) &&
2753 		    ((moddebug & MODDEBUG_FINI_EBUSY) == 0) &&
2754 		    !DEVNAME_DEVFSADM_HAS_RUN(devfsadm_state) &&
2755 		    !DEVNAME_DEVFSADM_IS_RUNNING(devfsadm_state) &&
2756 		    !sdev_reconfig_disable) {
2757 			/*
2758 			 * invoking "devfsadm" to do system device reconfig
2759 			 */
2760 			mutex_enter(&ddv->sdev_lookup_lock);
2761 			SDEV_BLOCK_OTHERS(ddv,
2762 			    (SDEV_READDIR|SDEV_LGWAITING));
2763 			mutex_exit(&ddv->sdev_lookup_lock);
2764 
2765 			sdcmn_err8(("readdir of %s by %s: reconfig\n",
2766 			    ddv->sdev_path, curproc->p_user.u_comm));
2767 			if (sdev_reconfig_verbose) {
2768 				cmn_err(CE_CONT,
2769 				    "?readdir of %s by %s: reconfig\n",
2770 				    ddv->sdev_path, curproc->p_user.u_comm);
2771 			}
2772 
2773 			sdev_devfsadmd_thread(ddv, NULL, kcred);
2774 		} else if (DEVNAME_DEVFSADM_IS_RUNNING(devfsadm_state)) {
2775 			/*
2776 			 * compensate the "ls" started later than "devfsadm"
2777 			 */
2778 			mutex_enter(&ddv->sdev_lookup_lock);
2779 			SDEV_BLOCK_OTHERS(ddv, (SDEV_READDIR|SDEV_LGWAITING));
2780 			mutex_exit(&ddv->sdev_lookup_lock);
2781 		}
2782 
2783 		/*
2784 		 * release the contents lock so that
2785 		 * the cache maybe updated by devfsadmd
2786 		 */
2787 		rw_exit(&ddv->sdev_contents);
2788 		mutex_enter(&ddv->sdev_lookup_lock);
2789 		if (SDEV_IS_READDIR(ddv))
2790 			(void) sdev_wait4lookup(ddv, SDEV_READDIR);
2791 		mutex_exit(&ddv->sdev_lookup_lock);
2792 		rw_enter(&ddv->sdev_contents, RW_READER);
2793 
2794 		sdcmn_err4(("readdir of directory %s by %s\n",
2795 		    ddv->sdev_name, curproc->p_user.u_comm));
2796 		while (ddv->sdev_flags & SDEV_BUILD) {
2797 			if (SDEV_IS_PERSIST(ddv)) {
2798 				error = sdev_filldir_from_store(ddv,
2799 				    alloc_count, cred);
2800 			}
2801 
2802 			/*
2803 			 * pre-creating the directories
2804 			 * defined in vtab
2805 			 */
2806 			if (SDEVTOV(ddv)->v_flag & VROOT) {
2807 				error = sdev_filldir_dynamic(ddv);
2808 			}
2809 
2810 			if (!error)
2811 				ddv->sdev_flags &= ~SDEV_BUILD;
2812 		}
2813 	}
2814 
2815 get_cache:
2816 	/* handle "." and ".." */
2817 	diroff = 0;
2818 	if (soff == 0) {
2819 		/* first time */
2820 		this_reclen = DIRENT64_RECLEN(1);
2821 		if (alloc_count < this_reclen) {
2822 			error = EINVAL;
2823 			goto done;
2824 		}
2825 
2826 		dp->d_ino = (ino64_t)ddv->sdev_ino;
2827 		dp->d_off = (off64_t)1;
2828 		dp->d_reclen = (ushort_t)this_reclen;
2829 
2830 		(void) strncpy(dp->d_name, ".",
2831 		    DIRENT64_NAMELEN(this_reclen));
2832 		outcount += dp->d_reclen;
2833 		dp = nextdp(dp);
2834 	}
2835 
2836 	diroff++;
2837 	if (soff <= 1) {
2838 		this_reclen = DIRENT64_RECLEN(2);
2839 		if (alloc_count < outcount + this_reclen) {
2840 			error = EINVAL;
2841 			goto done;
2842 		}
2843 
2844 		dp->d_reclen = (ushort_t)this_reclen;
2845 		dp->d_ino = (ino64_t)ddv->sdev_dotdot->sdev_ino;
2846 		dp->d_off = (off64_t)2;
2847 
2848 		(void) strncpy(dp->d_name, "..",
2849 		    DIRENT64_NAMELEN(this_reclen));
2850 		outcount += dp->d_reclen;
2851 
2852 		dp = nextdp(dp);
2853 	}
2854 
2855 
2856 	/* gets the cache */
2857 	diroff++;
2858 	for (dv = ddv->sdev_dot; dv; dv = dv->sdev_next, diroff++) {
2859 		sdcmn_err3(("sdev_readdir: diroff %lld soff %lld for '%s' \n",
2860 		    diroff, soff, dv->sdev_name));
2861 
2862 		/* bypassing pre-matured nodes */
2863 		if (diroff < soff || (dv->sdev_state != SDEV_READY)) {
2864 			sdcmn_err3(("sdev_readdir: pre-mature node  "
2865 			    "%s\n", dv->sdev_name));
2866 			continue;
2867 		}
2868 
2869 		/* don't list stale nodes */
2870 		if (dv->sdev_flags & SDEV_STALE) {
2871 			sdcmn_err4(("sdev_readdir: STALE node  "
2872 			    "%s\n", dv->sdev_name));
2873 			continue;
2874 		}
2875 
2876 		/*
2877 		 * Check validity of node
2878 		 */
2879 		if (vtor) {
2880 			switch (vtor(dv)) {
2881 			case SDEV_VTOR_VALID:
2882 				break;
2883 			case SDEV_VTOR_INVALID:
2884 			case SDEV_VTOR_SKIP:
2885 				continue;
2886 			default:
2887 				cmn_err(CE_PANIC,
2888 				    "dev fs: validator failed: %s(%p)\n",
2889 				    dv->sdev_name, (void *)dv);
2890 				break;
2891 			/*NOTREACHED*/
2892 			}
2893 		}
2894 
2895 		/*
2896 		 * call back into the module for the validity/bookkeeping
2897 		 * of this entry
2898 		 */
2899 		if (fn) {
2900 			error = (*fn)(&(dv->sdev_handle), cred);
2901 			if (error) {
2902 				sdcmn_err4(("sdev_readdir: module did not "
2903 				    "validate %s\n", dv->sdev_name));
2904 				continue;
2905 			}
2906 		}
2907 
2908 		namelen = strlen(dv->sdev_name);
2909 		reclen = DIRENT64_RECLEN(namelen);
2910 		if (outcount + reclen > alloc_count) {
2911 			goto full;
2912 		}
2913 		dp->d_reclen = (ushort_t)reclen;
2914 		dp->d_ino = (ino64_t)dv->sdev_ino;
2915 		dp->d_off = (off64_t)diroff + 1;
2916 		(void) strncpy(dp->d_name, dv->sdev_name,
2917 		    DIRENT64_NAMELEN(reclen));
2918 		outcount += reclen;
2919 		dp = nextdp(dp);
2920 	}
2921 
2922 full:
2923 	sdcmn_err4(("sdev_readdir: moving %lu bytes: "
2924 	    "diroff %lld, soff %lld, dv %p\n", outcount, diroff, soff,
2925 	    (void *)dv));
2926 
2927 	if (outcount)
2928 		error = uiomove(outbuf, outcount, UIO_READ, uiop);
2929 
2930 	if (!error) {
2931 		uiop->uio_loffset = diroff;
2932 		if (eofp)
2933 			*eofp = dv ? 0 : 1;
2934 	}
2935 
2936 
2937 	if (ddv->sdev_attrvp) {
2938 		gethrestime(&now);
2939 		attr.va_ctime = now;
2940 		attr.va_atime = now;
2941 		attr.va_mask = AT_CTIME|AT_ATIME;
2942 
2943 		(void) VOP_SETATTR(ddv->sdev_attrvp, &attr, 0, kcred, NULL);
2944 	}
2945 done:
2946 	kmem_free(outbuf, alloc_count);
2947 	return (error);
2948 }
2949 
2950 
2951 static int
2952 sdev_modctl_lookup(const char *path, vnode_t **r_vp)
2953 {
2954 	vnode_t *vp;
2955 	vnode_t *cvp;
2956 	struct sdev_node *svp;
2957 	char *nm;
2958 	struct pathname pn;
2959 	int error;
2960 	int persisted = 0;
2961 
2962 	if (error = pn_get((char *)path, UIO_SYSSPACE, &pn))
2963 		return (error);
2964 	nm = kmem_alloc(MAXNAMELEN, KM_SLEEP);
2965 
2966 	vp = rootdir;
2967 	VN_HOLD(vp);
2968 
2969 	while (pn_pathleft(&pn)) {
2970 		ASSERT(vp->v_type == VDIR);
2971 		(void) pn_getcomponent(&pn, nm);
2972 		error = VOP_LOOKUP(vp, nm, &cvp, NULL, 0, NULL, kcred);
2973 		VN_RELE(vp);
2974 
2975 		if (error)
2976 			break;
2977 
2978 		/* traverse mount points encountered on our journey */
2979 		if (vn_ismntpt(cvp) && (error = traverse(&cvp)) != 0) {
2980 			VN_RELE(cvp);
2981 			break;
2982 		}
2983 
2984 		/*
2985 		 * Direct the operation to the persisting filesystem
2986 		 * underlying /dev.  Bail if we encounter a
2987 		 * non-persistent dev entity here.
2988 		 */
2989 		if (cvp->v_vfsp->vfs_fstype == devtype) {
2990 
2991 			if ((VTOSDEV(cvp)->sdev_flags & SDEV_PERSIST) == 0) {
2992 				error = ENOENT;
2993 				VN_RELE(cvp);
2994 				break;
2995 			}
2996 
2997 			if (VTOSDEV(cvp) == NULL) {
2998 				error = ENOENT;
2999 				VN_RELE(cvp);
3000 				break;
3001 			}
3002 			svp = VTOSDEV(cvp);
3003 			if ((vp = svp->sdev_attrvp) == NULL) {
3004 				error = ENOENT;
3005 				VN_RELE(cvp);
3006 				break;
3007 			}
3008 			persisted = 1;
3009 			VN_HOLD(vp);
3010 			VN_RELE(cvp);
3011 			cvp = vp;
3012 		}
3013 
3014 		vp = cvp;
3015 		pn_skipslash(&pn);
3016 	}
3017 
3018 	kmem_free(nm, MAXNAMELEN);
3019 	pn_free(&pn);
3020 
3021 	if (error)
3022 		return (error);
3023 
3024 	/*
3025 	 * Only return persisted nodes in the filesystem underlying /dev.
3026 	 */
3027 	if (!persisted) {
3028 		VN_RELE(vp);
3029 		return (ENOENT);
3030 	}
3031 
3032 	*r_vp = vp;
3033 	return (0);
3034 }
3035 
3036 int
3037 sdev_modctl_readdir(const char *dir, char ***dirlistp,
3038 	int *npathsp, int *npathsp_alloc)
3039 {
3040 	char	**pathlist = NULL;
3041 	char	**newlist = NULL;
3042 	int	npaths = 0;
3043 	int	npaths_alloc = 0;
3044 	dirent64_t *dbuf = NULL;
3045 	int	n;
3046 	char	*s;
3047 	int error;
3048 	vnode_t *vp;
3049 	int eof;
3050 	struct iovec iov;
3051 	struct uio uio;
3052 	struct dirent64 *dp;
3053 	size_t dlen;
3054 	size_t dbuflen;
3055 	int ndirents = 64;
3056 	char *nm;
3057 
3058 	error = sdev_modctl_lookup(dir, &vp);
3059 	sdcmn_err11(("modctl readdir: %s by %s: %s\n",
3060 	    dir, curproc->p_user.u_comm,
3061 	    (error == 0) ? "ok" : "failed"));
3062 	if (error)
3063 		return (error);
3064 
3065 	dlen = ndirents * (sizeof (*dbuf));
3066 	dbuf = kmem_alloc(dlen, KM_SLEEP);
3067 
3068 	uio.uio_iov = &iov;
3069 	uio.uio_iovcnt = 1;
3070 	uio.uio_segflg = UIO_SYSSPACE;
3071 	uio.uio_fmode = 0;
3072 	uio.uio_extflg = UIO_COPY_CACHED;
3073 	uio.uio_loffset = 0;
3074 	uio.uio_llimit = MAXOFFSET_T;
3075 
3076 	eof = 0;
3077 	error = 0;
3078 	while (!error && !eof) {
3079 		uio.uio_resid = dlen;
3080 		iov.iov_base = (char *)dbuf;
3081 		iov.iov_len = dlen;
3082 
3083 		(void) VOP_RWLOCK(vp, V_WRITELOCK_FALSE, NULL);
3084 		error = VOP_READDIR(vp, &uio, kcred, &eof);
3085 		VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, NULL);
3086 
3087 		dbuflen = dlen - uio.uio_resid;
3088 
3089 		if (error || dbuflen == 0)
3090 			break;
3091 
3092 		for (dp = dbuf; ((intptr_t)dp < (intptr_t)dbuf + dbuflen);
3093 			dp = (dirent64_t *)((intptr_t)dp + dp->d_reclen)) {
3094 
3095 			nm = dp->d_name;
3096 
3097 			if (strcmp(nm, ".") == 0 || strcmp(nm, "..") == 0)
3098 				continue;
3099 
3100 			if (npaths == npaths_alloc) {
3101 				npaths_alloc += 64;
3102 				newlist = (char **)
3103 				    kmem_zalloc((npaths_alloc + 1) *
3104 					sizeof (char *), KM_SLEEP);
3105 				if (pathlist) {
3106 					bcopy(pathlist, newlist,
3107 					    npaths * sizeof (char *));
3108 					kmem_free(pathlist,
3109 					    (npaths + 1) * sizeof (char *));
3110 				}
3111 				pathlist = newlist;
3112 			}
3113 			n = strlen(nm) + 1;
3114 			s = kmem_alloc(n, KM_SLEEP);
3115 			bcopy(nm, s, n);
3116 			pathlist[npaths++] = s;
3117 			sdcmn_err11(("  %s/%s\n", dir, s));
3118 		}
3119 	}
3120 
3121 exit:
3122 	VN_RELE(vp);
3123 
3124 	if (dbuf)
3125 		kmem_free(dbuf, dlen);
3126 
3127 	if (error)
3128 		return (error);
3129 
3130 	*dirlistp = pathlist;
3131 	*npathsp = npaths;
3132 	*npathsp_alloc = npaths_alloc;
3133 
3134 	return (0);
3135 }
3136 
3137 void
3138 sdev_modctl_readdir_free(char **pathlist, int npaths, int npaths_alloc)
3139 {
3140 	int	i, n;
3141 
3142 	for (i = 0; i < npaths; i++) {
3143 		n = strlen(pathlist[i]) + 1;
3144 		kmem_free(pathlist[i], n);
3145 	}
3146 
3147 	kmem_free(pathlist, (npaths_alloc + 1) * sizeof (char *));
3148 }
3149 
3150 int
3151 sdev_modctl_devexists(const char *path)
3152 {
3153 	vnode_t *vp;
3154 	int error;
3155 
3156 	error = sdev_modctl_lookup(path, &vp);
3157 	sdcmn_err11(("modctl dev exists: %s by %s: %s\n",
3158 	    path, curproc->p_user.u_comm,
3159 	    (error == 0) ? "ok" : "failed"));
3160 	if (error == 0)
3161 		VN_RELE(vp);
3162 
3163 	return (error);
3164 }
3165 
3166 void
3167 sdev_update_newnsmap(struct devname_nsmap *map, char *module, char *mapname)
3168 {
3169 	rw_enter(&map->dir_lock, RW_WRITER);
3170 	if (module) {
3171 		ASSERT(map->dir_newmodule == NULL);
3172 		map->dir_newmodule = i_ddi_strdup(module, KM_SLEEP);
3173 	}
3174 	if (mapname) {
3175 		ASSERT(map->dir_newmap == NULL);
3176 		map->dir_newmap = i_ddi_strdup(mapname, KM_SLEEP);
3177 	}
3178 
3179 	map->dir_invalid = 1;
3180 	rw_exit(&map->dir_lock);
3181 }
3182 
3183 void
3184 sdev_replace_nsmap(struct devname_nsmap *map, char *module, char *mapname)
3185 {
3186 	char *old_module = NULL;
3187 	char *old_map = NULL;
3188 
3189 	ASSERT(RW_LOCK_HELD(&map->dir_lock));
3190 	if (!rw_tryupgrade(&map->dir_lock)) {
3191 		rw_exit(&map->dir_lock);
3192 		rw_enter(&map->dir_lock, RW_WRITER);
3193 	}
3194 
3195 	old_module = map->dir_module;
3196 	if (module) {
3197 		if (old_module && strcmp(old_module, module) != 0) {
3198 			kmem_free(old_module, strlen(old_module) + 1);
3199 		}
3200 		map->dir_module = module;
3201 		map->dir_newmodule = NULL;
3202 	}
3203 
3204 	old_map = map->dir_map;
3205 	if (mapname) {
3206 		if (old_map && strcmp(old_map, mapname) != 0) {
3207 			kmem_free(old_map, strlen(old_map) + 1);
3208 		}
3209 
3210 		map->dir_map = mapname;
3211 		map->dir_newmap = NULL;
3212 	}
3213 	map->dir_maploaded = 0;
3214 	map->dir_invalid = 0;
3215 	rw_downgrade(&map->dir_lock);
3216 }
3217 
3218 /*
3219  * dir_name should have at least one attribute,
3220  *	dir_module
3221  *	or dir_map
3222  *	or both
3223  * caller holds the devname_nsmaps_lock
3224  */
3225 void
3226 sdev_insert_nsmap(char *dir_name, char *dir_module, char *dir_map)
3227 {
3228 	struct devname_nsmap *map;
3229 	int len = 0;
3230 
3231 	ASSERT(dir_name);
3232 	ASSERT(dir_module || dir_map);
3233 	ASSERT(MUTEX_HELD(&devname_nsmaps_lock));
3234 
3235 	if (map = sdev_get_nsmap_by_dir(dir_name, 1)) {
3236 		sdev_update_newnsmap(map, dir_module, dir_map);
3237 		return;
3238 	}
3239 
3240 	map = (struct devname_nsmap *)kmem_zalloc(sizeof (*map), KM_SLEEP);
3241 	map->dir_name = i_ddi_strdup(dir_name, KM_SLEEP);
3242 	if (dir_module) {
3243 		map->dir_module = i_ddi_strdup(dir_module, KM_SLEEP);
3244 	}
3245 
3246 	if (dir_map) {
3247 		if (dir_map[0] != '/') {
3248 			len = strlen(ETC_DEV_DIR) + strlen(dir_map) + 2;
3249 			map->dir_map = kmem_zalloc(len, KM_SLEEP);
3250 			(void) snprintf(map->dir_map, len, "%s/%s", ETC_DEV_DIR,
3251 			    dir_map);
3252 		} else {
3253 			map->dir_map = i_ddi_strdup(dir_map, KM_SLEEP);
3254 		}
3255 	}
3256 
3257 	map->dir_ops = NULL;
3258 	map->dir_maploaded = 0;
3259 	map->dir_invalid = 0;
3260 	rw_init(&map->dir_lock, NULL, RW_DEFAULT, NULL);
3261 
3262 	map->next = devname_nsmaps;
3263 	map->prev = NULL;
3264 	if (devname_nsmaps) {
3265 		devname_nsmaps->prev = map;
3266 	}
3267 	devname_nsmaps = map;
3268 }
3269 
3270 struct devname_nsmap *
3271 sdev_get_nsmap_by_dir(char *dir_path, int locked)
3272 {
3273 	struct devname_nsmap *map = NULL;
3274 
3275 	if (!locked)
3276 		mutex_enter(&devname_nsmaps_lock);
3277 	for (map = devname_nsmaps; map; map = map->next) {
3278 		sdcmn_err6(("sdev_get_nsmap_by_dir: dir %s\n", map->dir_name));
3279 		if (strcmp(map->dir_name, dir_path) == 0) {
3280 			if (!locked)
3281 				mutex_exit(&devname_nsmaps_lock);
3282 			return (map);
3283 		}
3284 	}
3285 	if (!locked)
3286 		mutex_exit(&devname_nsmaps_lock);
3287 	return (NULL);
3288 }
3289 
3290 struct devname_nsmap *
3291 sdev_get_nsmap_by_module(char *mod_name)
3292 {
3293 	struct devname_nsmap *map = NULL;
3294 
3295 	mutex_enter(&devname_nsmaps_lock);
3296 	for (map = devname_nsmaps; map; map = map->next) {
3297 		sdcmn_err7(("sdev_get_nsmap_by_module: module %s\n",
3298 		    map->dir_module));
3299 		if (map->dir_module && strcmp(map->dir_module, mod_name) == 0) {
3300 			mutex_exit(&devname_nsmaps_lock);
3301 			return (map);
3302 		}
3303 	}
3304 	mutex_exit(&devname_nsmaps_lock);
3305 	return (NULL);
3306 }
3307 
3308 void
3309 sdev_invalidate_nsmaps()
3310 {
3311 	struct devname_nsmap *map = NULL;
3312 
3313 	ASSERT(MUTEX_HELD(&devname_nsmaps_lock));
3314 
3315 	if (devname_nsmaps == NULL)
3316 		return;
3317 
3318 	for (map = devname_nsmaps; map; map = map->next) {
3319 		rw_enter(&map->dir_lock, RW_WRITER);
3320 		map->dir_invalid = 1;
3321 		rw_exit(&map->dir_lock);
3322 	}
3323 	devname_nsmaps_invalidated = 1;
3324 }
3325 
3326 
3327 int
3328 sdev_nsmaps_loaded()
3329 {
3330 	int ret = 0;
3331 
3332 	mutex_enter(&devname_nsmaps_lock);
3333 	if (devname_nsmaps_loaded)
3334 		ret = 1;
3335 
3336 	mutex_exit(&devname_nsmaps_lock);
3337 	return (ret);
3338 }
3339 
3340 int
3341 sdev_nsmaps_reloaded()
3342 {
3343 	int ret = 0;
3344 
3345 	mutex_enter(&devname_nsmaps_lock);
3346 	if (devname_nsmaps_invalidated)
3347 		ret = 1;
3348 
3349 	mutex_exit(&devname_nsmaps_lock);
3350 	return (ret);
3351 }
3352 
3353 static void
3354 sdev_free_nsmap(struct devname_nsmap *map)
3355 {
3356 	ASSERT(map);
3357 	if (map->dir_name)
3358 		kmem_free(map->dir_name, strlen(map->dir_name) + 1);
3359 	if (map->dir_module)
3360 		kmem_free(map->dir_module, strlen(map->dir_module) + 1);
3361 	if (map->dir_map)
3362 		kmem_free(map->dir_map, strlen(map->dir_map) + 1);
3363 	rw_destroy(&map->dir_lock);
3364 	kmem_free(map, sizeof (*map));
3365 }
3366 
3367 void
3368 sdev_validate_nsmaps()
3369 {
3370 	struct devname_nsmap *map = NULL;
3371 	struct devname_nsmap *oldmap = NULL;
3372 
3373 	ASSERT(MUTEX_HELD(&devname_nsmaps_lock));
3374 	map = devname_nsmaps;
3375 	while (map) {
3376 		rw_enter(&map->dir_lock, RW_READER);
3377 		if ((map->dir_invalid == 1) && (map->dir_newmodule == NULL) &&
3378 		    (map->dir_newmap == NULL)) {
3379 			oldmap = map;
3380 			rw_exit(&map->dir_lock);
3381 			if (map->prev)
3382 				map->prev->next = oldmap->next;
3383 			if (map == devname_nsmaps)
3384 				devname_nsmaps = oldmap->next;
3385 
3386 			map = oldmap->next;
3387 			if (map)
3388 				map->prev = oldmap->prev;
3389 			sdev_free_nsmap(oldmap);
3390 			oldmap = NULL;
3391 		} else {
3392 			rw_exit(&map->dir_lock);
3393 			map = map->next;
3394 		}
3395 	}
3396 	devname_nsmaps_invalidated = 0;
3397 }
3398 
3399 static int
3400 sdev_map_is_invalid(struct devname_nsmap *map)
3401 {
3402 	int ret = 0;
3403 
3404 	ASSERT(map);
3405 	rw_enter(&map->dir_lock, RW_READER);
3406 	if (map->dir_invalid)
3407 		ret = 1;
3408 	rw_exit(&map->dir_lock);
3409 	return (ret);
3410 }
3411 
3412 static int
3413 sdev_check_map(struct devname_nsmap *map)
3414 {
3415 	struct devname_nsmap *mapp;
3416 
3417 	mutex_enter(&devname_nsmaps_lock);
3418 	if (devname_nsmaps == NULL) {
3419 		mutex_exit(&devname_nsmaps_lock);
3420 		return (1);
3421 	}
3422 
3423 	for (mapp = devname_nsmaps; mapp; mapp = mapp->next) {
3424 		if (mapp == map) {
3425 			mutex_exit(&devname_nsmaps_lock);
3426 			return (0);
3427 		}
3428 	}
3429 
3430 	mutex_exit(&devname_nsmaps_lock);
3431 	return (1);
3432 
3433 }
3434 
3435 struct devname_nsmap *
3436 sdev_get_map(struct sdev_node *dv, int validate)
3437 {
3438 	struct devname_nsmap *map;
3439 	int error;
3440 
3441 	ASSERT(RW_READ_HELD(&dv->sdev_contents));
3442 	map = dv->sdev_mapinfo;
3443 	if (map && sdev_check_map(map)) {
3444 		if (!rw_tryupgrade(&dv->sdev_contents)) {
3445 			rw_exit(&dv->sdev_contents);
3446 			rw_enter(&dv->sdev_contents, RW_WRITER);
3447 		}
3448 		dv->sdev_mapinfo = NULL;
3449 		rw_downgrade(&dv->sdev_contents);
3450 		return (NULL);
3451 	}
3452 
3453 	if (validate && (!map || (map && sdev_map_is_invalid(map)))) {
3454 		if (!rw_tryupgrade(&dv->sdev_contents)) {
3455 			rw_exit(&dv->sdev_contents);
3456 			rw_enter(&dv->sdev_contents, RW_WRITER);
3457 		}
3458 		error = sdev_get_moduleops(dv);
3459 		if (!error)
3460 			map = dv->sdev_mapinfo;
3461 		rw_downgrade(&dv->sdev_contents);
3462 	}
3463 	return (map);
3464 }
3465 
3466 extern int sdev_vnodeops_tbl_size;
3467 
3468 /*
3469  * construct a new template with overrides from vtab
3470  */
3471 static fs_operation_def_t *
3472 sdev_merge_vtab(const fs_operation_def_t tab[])
3473 {
3474 	fs_operation_def_t *new;
3475 	const fs_operation_def_t *tab_entry;
3476 
3477 	/* make a copy of standard vnode ops table */
3478 	new = kmem_alloc(sdev_vnodeops_tbl_size, KM_SLEEP);
3479 	bcopy((void *)sdev_vnodeops_tbl, new, sdev_vnodeops_tbl_size);
3480 
3481 	/* replace the overrides from tab */
3482 	for (tab_entry = tab; tab_entry->name != NULL; tab_entry++) {
3483 		fs_operation_def_t *std_entry = new;
3484 		while (std_entry->name) {
3485 			if (strcmp(tab_entry->name, std_entry->name) == 0) {
3486 				std_entry->func = tab_entry->func;
3487 				break;
3488 			}
3489 			std_entry++;
3490 		}
3491 		if (std_entry->name == NULL)
3492 			cmn_err(CE_NOTE, "sdev_merge_vtab: entry %s unused.",
3493 			    tab_entry->name);
3494 	}
3495 
3496 	return (new);
3497 }
3498 
3499 /* free memory allocated by sdev_merge_vtab */
3500 static void
3501 sdev_free_vtab(fs_operation_def_t *new)
3502 {
3503 	kmem_free(new, sdev_vnodeops_tbl_size);
3504 }
3505 
3506 void
3507 devname_get_vnode(devname_handle_t *hdl, vnode_t **vpp)
3508 {
3509 	struct sdev_node *dv = hdl->dh_data;
3510 
3511 	ASSERT(dv);
3512 
3513 	rw_enter(&dv->sdev_contents, RW_READER);
3514 	*vpp = SDEVTOV(dv);
3515 	rw_exit(&dv->sdev_contents);
3516 }
3517 
3518 int
3519 devname_get_path(devname_handle_t *hdl, char **path)
3520 {
3521 	struct sdev_node *dv = hdl->dh_data;
3522 
3523 	ASSERT(dv);
3524 
3525 	rw_enter(&dv->sdev_contents, RW_READER);
3526 	*path = dv->sdev_path;
3527 	rw_exit(&dv->sdev_contents);
3528 	return (0);
3529 }
3530 
3531 int
3532 devname_get_name(devname_handle_t *hdl, char **entry)
3533 {
3534 	struct sdev_node *dv = hdl->dh_data;
3535 
3536 	ASSERT(dv);
3537 	rw_enter(&dv->sdev_contents, RW_READER);
3538 	*entry = dv->sdev_name;
3539 	rw_exit(&dv->sdev_contents);
3540 	return (0);
3541 }
3542 
3543 void
3544 devname_get_dir_vnode(devname_handle_t *hdl, vnode_t **vpp)
3545 {
3546 	struct sdev_node *dv = hdl->dh_data->sdev_dotdot;
3547 
3548 	ASSERT(dv);
3549 
3550 	rw_enter(&dv->sdev_contents, RW_READER);
3551 	*vpp = SDEVTOV(dv);
3552 	rw_exit(&dv->sdev_contents);
3553 }
3554 
3555 int
3556 devname_get_dir_path(devname_handle_t *hdl, char **path)
3557 {
3558 	struct sdev_node *dv = hdl->dh_data->sdev_dotdot;
3559 
3560 	ASSERT(dv);
3561 	rw_enter(&dv->sdev_contents, RW_READER);
3562 	*path = dv->sdev_path;
3563 	rw_exit(&dv->sdev_contents);
3564 	return (0);
3565 }
3566 
3567 int
3568 devname_get_dir_name(devname_handle_t *hdl, char **entry)
3569 {
3570 	struct sdev_node *dv = hdl->dh_data->sdev_dotdot;
3571 
3572 	ASSERT(dv);
3573 	rw_enter(&dv->sdev_contents, RW_READER);
3574 	*entry = dv->sdev_name;
3575 	rw_exit(&dv->sdev_contents);
3576 	return (0);
3577 }
3578 
3579 int
3580 devname_get_dir_nsmap(devname_handle_t *hdl, struct devname_nsmap **map)
3581 {
3582 	struct sdev_node *dv = hdl->dh_data->sdev_dotdot;
3583 
3584 	ASSERT(dv);
3585 	rw_enter(&dv->sdev_contents, RW_READER);
3586 	*map = dv->sdev_mapinfo;
3587 	rw_exit(&dv->sdev_contents);
3588 	return (0);
3589 }
3590 
3591 int
3592 devname_get_dir_handle(devname_handle_t *hdl, devname_handle_t **dir_hdl)
3593 {
3594 	struct sdev_node *dv = hdl->dh_data->sdev_dotdot;
3595 
3596 	ASSERT(dv);
3597 	rw_enter(&dv->sdev_contents, RW_READER);
3598 	*dir_hdl = &(dv->sdev_handle);
3599 	rw_exit(&dv->sdev_contents);
3600 	return (0);
3601 }
3602 
3603 void
3604 devname_set_nodetype(devname_handle_t *hdl, void *args, int spec)
3605 {
3606 	struct sdev_node *dv = hdl->dh_data;
3607 
3608 	ASSERT(dv);
3609 	rw_enter(&dv->sdev_contents, RW_WRITER);
3610 	hdl->dh_spec = (devname_spec_t)spec;
3611 	hdl->dh_args = (void *)i_ddi_strdup((char *)args, KM_SLEEP);
3612 	rw_exit(&dv->sdev_contents);
3613 }
3614 
3615 /*
3616  * a generic setattr() function
3617  *
3618  * note: flags only supports AT_UID and AT_GID.
3619  *	 Future enhancements can be done for other types, e.g. AT_MODE
3620  */
3621 int
3622 devname_setattr_func(struct vnode *vp, struct vattr *vap, int flags,
3623     struct cred *cred, int (*callback)(struct sdev_node *, struct vattr *,
3624     int), int protocol)
3625 {
3626 	struct sdev_node	*dv = VTOSDEV(vp);
3627 	struct sdev_node	*parent = dv->sdev_dotdot;
3628 	struct vattr		*get;
3629 	uint_t			mask = vap->va_mask;
3630 	int 			error;
3631 
3632 	/* some sanity checks */
3633 	if (vap->va_mask & AT_NOSET)
3634 		return (EINVAL);
3635 
3636 	if (vap->va_mask & AT_SIZE) {
3637 		if (vp->v_type == VDIR) {
3638 			return (EISDIR);
3639 		}
3640 	}
3641 
3642 	/* no need to set attribute, but do not fail either */
3643 	ASSERT(parent);
3644 	rw_enter(&parent->sdev_contents, RW_READER);
3645 	if (dv->sdev_state == SDEV_ZOMBIE) {
3646 		rw_exit(&parent->sdev_contents);
3647 		return (0);
3648 	}
3649 
3650 	/* If backing store exists, just set it. */
3651 	if (dv->sdev_attrvp) {
3652 		rw_exit(&parent->sdev_contents);
3653 		return (VOP_SETATTR(dv->sdev_attrvp, vap, flags, cred, NULL));
3654 	}
3655 
3656 	/*
3657 	 * Otherwise, for nodes with the persistence attribute, create it.
3658 	 */
3659 	ASSERT(dv->sdev_attr);
3660 	if (SDEV_IS_PERSIST(dv) ||
3661 	    ((vap->va_mask & ~AT_TIMES) != 0 && !SDEV_IS_DYNAMIC(dv))) {
3662 		sdev_vattr_merge(dv, vap);
3663 		rw_enter(&dv->sdev_contents, RW_WRITER);
3664 		error = sdev_shadow_node(dv, cred);
3665 		rw_exit(&dv->sdev_contents);
3666 		rw_exit(&parent->sdev_contents);
3667 
3668 		if (error)
3669 			return (error);
3670 		return (VOP_SETATTR(dv->sdev_attrvp, vap, flags, cred, NULL));
3671 	}
3672 
3673 
3674 	/*
3675 	 * sdev_attr was allocated in sdev_mknode
3676 	 */
3677 	rw_enter(&dv->sdev_contents, RW_WRITER);
3678 	error = secpolicy_vnode_setattr(cred, vp, vap, dv->sdev_attr,
3679 	    flags, sdev_unlocked_access, dv);
3680 	if (error) {
3681 		rw_exit(&dv->sdev_contents);
3682 		rw_exit(&parent->sdev_contents);
3683 		return (error);
3684 	}
3685 
3686 	get = dv->sdev_attr;
3687 	if (mask & AT_MODE) {
3688 		get->va_mode &= S_IFMT;
3689 		get->va_mode |= vap->va_mode & ~S_IFMT;
3690 	}
3691 
3692 	if ((mask & AT_UID) || (mask & AT_GID)) {
3693 		if (mask & AT_UID)
3694 			get->va_uid = vap->va_uid;
3695 		if (mask & AT_GID)
3696 			get->va_gid = vap->va_gid;
3697 		/*
3698 		 * a callback must be provided if the protocol is set
3699 		 */
3700 		if ((protocol & AT_UID) || (protocol & AT_GID)) {
3701 			ASSERT(callback);
3702 			error = callback(dv, get, protocol);
3703 			if (error) {
3704 				rw_exit(&dv->sdev_contents);
3705 				rw_exit(&parent->sdev_contents);
3706 				return (error);
3707 			}
3708 		}
3709 	}
3710 
3711 	if (mask & AT_ATIME)
3712 		get->va_atime = vap->va_atime;
3713 	if (mask & AT_MTIME)
3714 		get->va_mtime = vap->va_mtime;
3715 	if (mask & (AT_MODE | AT_UID | AT_GID | AT_CTIME)) {
3716 		gethrestime(&get->va_ctime);
3717 	}
3718 
3719 	sdev_vattr_merge(dv, get);
3720 	rw_exit(&dv->sdev_contents);
3721 	rw_exit(&parent->sdev_contents);
3722 	return (0);
3723 }
3724