xref: /illumos-gate/usr/src/uts/common/fs/dev/sdev_vnops.c (revision dd4eeefd)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * vnode ops for the /dev filesystem
30  *
31  * - VDIR, VCHR, CBLK, and VLNK are considered must supported files
32  * - VREG and VDOOR are used for some internal implementations in
33  *    the global zone, e.g. devname and devfsadm communication
34  * - other file types are unusual in this namespace and
35  *    not supported for now
36  */
37 
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/t_lock.h>
41 #include <sys/systm.h>
42 #include <sys/sysmacros.h>
43 #include <sys/user.h>
44 #include <sys/time.h>
45 #include <sys/vfs.h>
46 #include <sys/vnode.h>
47 #include <sys/vfs_opreg.h>
48 #include <sys/file.h>
49 #include <sys/fcntl.h>
50 #include <sys/flock.h>
51 #include <sys/kmem.h>
52 #include <sys/uio.h>
53 #include <sys/errno.h>
54 #include <sys/stat.h>
55 #include <sys/cred.h>
56 #include <sys/cred_impl.h>
57 #include <sys/dirent.h>
58 #include <sys/pathname.h>
59 #include <sys/cmn_err.h>
60 #include <sys/debug.h>
61 #include <sys/policy.h>
62 #include <vm/hat.h>
63 #include <vm/seg_vn.h>
64 #include <vm/seg_map.h>
65 #include <vm/seg.h>
66 #include <vm/as.h>
67 #include <vm/page.h>
68 #include <sys/proc.h>
69 #include <sys/mode.h>
70 #include <sys/sunndi.h>
71 #include <sys/ptms.h>
72 #include <fs/fs_subr.h>
73 #include <sys/fs/dv_node.h>
74 #include <sys/fs/sdev_impl.h>
75 #include <sys/fs/sdev_node.h>
76 
77 /*ARGSUSED*/
78 static int
79 sdev_open(struct vnode **vpp, int flag, struct cred *cred)
80 {
81 	struct sdev_node *dv = VTOSDEV(*vpp);
82 	struct sdev_node *ddv = dv->sdev_dotdot;
83 	int error = 0;
84 
85 	if ((*vpp)->v_type == VDIR)
86 		return (0);
87 
88 	if (!SDEV_IS_GLOBAL(dv))
89 		return (ENOTSUP);
90 
91 	ASSERT((*vpp)->v_type == VREG);
92 	if ((*vpp)->v_type != VREG)
93 		return (ENOTSUP);
94 
95 	ASSERT(ddv);
96 	rw_enter(&ddv->sdev_contents, RW_READER);
97 	if (dv->sdev_attrvp == NULL) {
98 		rw_exit(&ddv->sdev_contents);
99 		return (ENOENT);
100 	}
101 	error = VOP_OPEN(&(dv->sdev_attrvp), flag, cred);
102 	rw_exit(&ddv->sdev_contents);
103 	return (error);
104 }
105 
106 /*ARGSUSED1*/
107 static int
108 sdev_close(struct vnode *vp, int flag, int count,
109     offset_t offset, struct cred *cred)
110 {
111 	struct sdev_node *dv = VTOSDEV(vp);
112 
113 	if (vp->v_type == VDIR) {
114 		cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
115 		cleanshares(vp, ttoproc(curthread)->p_pid);
116 		return (0);
117 	}
118 
119 	if (!SDEV_IS_GLOBAL(dv))
120 		return (ENOTSUP);
121 
122 	ASSERT(vp->v_type == VREG);
123 	if (vp->v_type != VREG)
124 		return (ENOTSUP);
125 
126 	ASSERT(dv->sdev_attrvp);
127 	return (VOP_CLOSE(dv->sdev_attrvp, flag, count, offset, cred));
128 }
129 
130 /*ARGSUSED*/
131 static int
132 sdev_read(struct vnode *vp, struct uio *uio, int ioflag, struct cred *cred,
133 	struct caller_context *ct)
134 {
135 	struct sdev_node *dv = (struct sdev_node *)VTOSDEV(vp);
136 	int	error;
137 
138 	if (!SDEV_IS_GLOBAL(dv))
139 		return (EINVAL);
140 
141 	if (vp->v_type == VDIR)
142 		return (EISDIR);
143 
144 	/* only supporting regular files in /dev */
145 	ASSERT(vp->v_type == VREG);
146 	if (vp->v_type != VREG)
147 		return (EINVAL);
148 
149 	ASSERT(RW_READ_HELD(&VTOSDEV(vp)->sdev_contents));
150 	ASSERT(dv->sdev_attrvp);
151 	(void) VOP_RWLOCK(dv->sdev_attrvp, 0, NULL);
152 	error = VOP_READ(dv->sdev_attrvp, uio, ioflag, cred, ct);
153 	VOP_RWUNLOCK(dv->sdev_attrvp, 0, NULL);
154 	return (error);
155 }
156 
157 /*ARGSUSED*/
158 static int
159 sdev_write(struct vnode *vp, struct uio *uio, int ioflag, struct cred *cred,
160 	struct caller_context *ct)
161 {
162 	struct sdev_node *dv = VTOSDEV(vp);
163 	int	error = 0;
164 
165 	if (!SDEV_IS_GLOBAL(dv))
166 		return (EINVAL);
167 
168 	if (vp->v_type == VDIR)
169 		return (EISDIR);
170 
171 	/* only supporting regular files in /dev */
172 	ASSERT(vp->v_type == VREG);
173 	if (vp->v_type != VREG)
174 		return (EINVAL);
175 
176 	ASSERT(dv->sdev_attrvp);
177 
178 	(void) VOP_RWLOCK(dv->sdev_attrvp, 1, NULL);
179 	error = VOP_WRITE(dv->sdev_attrvp, uio, ioflag, cred, ct);
180 	VOP_RWUNLOCK(dv->sdev_attrvp, 1, NULL);
181 	if (error == 0) {
182 		sdev_update_timestamps(dv->sdev_attrvp, kcred,
183 		    AT_MTIME);
184 	}
185 	return (error);
186 }
187 
188 /*ARGSUSED*/
189 static int
190 sdev_ioctl(struct vnode *vp, int cmd, intptr_t arg, int flag,
191     struct cred *cred, int *rvalp)
192 {
193 	struct sdev_node *dv = VTOSDEV(vp);
194 
195 	if (!SDEV_IS_GLOBAL(dv) || (vp->v_type == VDIR))
196 		return (ENOTTY);
197 
198 	ASSERT(vp->v_type == VREG);
199 	if (vp->v_type != VREG)
200 		return (EINVAL);
201 
202 	ASSERT(dv->sdev_attrvp);
203 	return (VOP_IOCTL(dv->sdev_attrvp, cmd, arg, flag, cred, rvalp));
204 }
205 
206 static int
207 sdev_getattr(struct vnode *vp, struct vattr *vap, int flags, struct cred *cr)
208 {
209 	int			error = 0;
210 	struct sdev_node	*dv = VTOSDEV(vp);
211 	struct sdev_node	*parent = dv->sdev_dotdot;
212 	struct devname_nsmap *map = NULL;
213 	struct devname_ops	*dirops = NULL;
214 	int (*fn)(devname_handle_t *, struct vattr *, struct cred *);
215 
216 	ASSERT(parent);
217 
218 	rw_enter(&parent->sdev_contents, RW_READER);
219 	ASSERT(dv->sdev_attr || dv->sdev_attrvp);
220 	if (SDEV_IS_GLOBAL(dv) && (dv->sdev_state != SDEV_ZOMBIE)) {
221 		map = sdev_get_map(parent, 0);
222 		dirops = map ? map->dir_ops : NULL;
223 	}
224 
225 	/*
226 	 * search order:
227 	 * 	- for persistent nodes (SDEV_PERSIST): backstore
228 	 *	- for non-persistent nodes: module ops if global, then memory
229 	 */
230 	if (dv->sdev_attrvp) {
231 		rw_exit(&parent->sdev_contents);
232 		error = VOP_GETATTR(dv->sdev_attrvp, vap, flags, cr);
233 		sdev_vattr_merge(dv, vap);
234 	} else if (dirops && (fn = dirops->devnops_getattr)) {
235 		sdev_vattr_merge(dv, vap);
236 		rw_exit(&parent->sdev_contents);
237 		error = (*fn)(&(dv->sdev_handle), vap, cr);
238 	} else {
239 		ASSERT(dv->sdev_attr);
240 		*vap = *dv->sdev_attr;
241 		sdev_vattr_merge(dv, vap);
242 		rw_exit(&parent->sdev_contents);
243 	}
244 
245 	return (error);
246 }
247 
248 /*ARGSUSED4*/
249 static int
250 sdev_setattr(struct vnode *vp, struct vattr *vap, int flags,
251     struct cred *cred, caller_context_t *ctp)
252 {
253 	return (devname_setattr_func(vp, vap, flags, cred, NULL, 0));
254 }
255 
256 static int
257 sdev_getsecattr(struct vnode *vp, struct vsecattr *vsap, int flags,
258     struct cred *cr)
259 {
260 	int	error;
261 	struct sdev_node *dv = VTOSDEV(vp);
262 	struct vnode *avp = dv->sdev_attrvp;
263 
264 	if (avp == NULL) {
265 		/* return fs_fab_acl() if flavor matches, else do nothing */
266 		if ((SDEV_ACL_FLAVOR(vp) == _ACL_ACLENT_ENABLED &&
267 		    (vsap->vsa_mask & (VSA_ACLCNT | VSA_DFACLCNT))) ||
268 		    (SDEV_ACL_FLAVOR(vp) == _ACL_ACE_ENABLED &&
269 		    (vsap->vsa_mask & (VSA_ACECNT | VSA_ACE))))
270 			return (fs_fab_acl(vp, vsap, flags, cr));
271 
272 		return (ENOSYS);
273 	}
274 
275 	(void) VOP_RWLOCK(avp, 1, NULL);
276 	error = VOP_GETSECATTR(avp, vsap, flags, cr);
277 	VOP_RWUNLOCK(avp, 1, NULL);
278 	return (error);
279 }
280 
281 static int
282 sdev_setsecattr(struct vnode *vp, struct vsecattr *vsap, int flags,
283     struct cred *cr)
284 {
285 	int	error;
286 	struct sdev_node *dv = VTOSDEV(vp);
287 	struct vnode *avp = dv->sdev_attrvp;
288 
289 	if (dv->sdev_state == SDEV_ZOMBIE)
290 		return (0);
291 
292 	if (avp == NULL) {
293 		if (SDEV_IS_GLOBAL(dv) && !SDEV_IS_PERSIST(dv))
294 			return (fs_nosys());
295 		ASSERT(dv->sdev_attr);
296 		/*
297 		 * if coming in directly, the acl system call will
298 		 * have held the read-write lock via VOP_RWLOCK()
299 		 * If coming in via specfs, specfs will have
300 		 * held the rw lock on the realvp i.e. us.
301 		 */
302 		ASSERT(RW_WRITE_HELD(&dv->sdev_contents));
303 		sdev_vattr_merge(dv, dv->sdev_attr);
304 		error =  sdev_shadow_node(dv, cr);
305 		if (error) {
306 			return (fs_nosys());
307 		}
308 
309 		ASSERT(dv->sdev_attrvp);
310 		/* clean out the memory copy if any */
311 		if (dv->sdev_attr) {
312 			kmem_free(dv->sdev_attr, sizeof (struct vattr));
313 			dv->sdev_attr = NULL;
314 		}
315 		avp = dv->sdev_attrvp;
316 	}
317 	ASSERT(avp);
318 
319 	(void) VOP_RWLOCK(avp, V_WRITELOCK_TRUE, NULL);
320 	error = VOP_SETSECATTR(avp, vsap, flags, cr);
321 	VOP_RWUNLOCK(avp, V_WRITELOCK_TRUE, NULL);
322 	return (error);
323 }
324 
325 int
326 sdev_unlocked_access(void *vdv, int mode, struct cred *cr)
327 {
328 	struct sdev_node	*dv = vdv;
329 	int			shift = 0;
330 	uid_t			owner = dv->sdev_attr->va_uid;
331 
332 	if (crgetuid(cr) != owner) {
333 		shift += 3;
334 		if (groupmember(dv->sdev_attr->va_gid, cr) == 0)
335 			shift += 3;
336 	}
337 
338 	mode &= ~(dv->sdev_attr->va_mode << shift);
339 	if (mode == 0)
340 		return (0);
341 
342 	return (secpolicy_vnode_access(cr, SDEVTOV(dv), owner, mode));
343 }
344 
345 static int
346 sdev_access(struct vnode *vp, int mode, int flags, struct cred *cr)
347 {
348 	struct sdev_node	*dv = VTOSDEV(vp);
349 	int ret = 0;
350 
351 	ASSERT(dv->sdev_attr || dv->sdev_attrvp);
352 
353 	if (dv->sdev_attrvp) {
354 		ret = VOP_ACCESS(dv->sdev_attrvp, mode, flags, cr);
355 	} else if (dv->sdev_attr) {
356 		rw_enter(&dv->sdev_contents, RW_READER);
357 		ret = sdev_unlocked_access(dv, mode, cr);
358 		if (ret)
359 			ret = EACCES;
360 		rw_exit(&dv->sdev_contents);
361 	}
362 
363 	return (ret);
364 }
365 
366 /*
367  * Lookup
368  */
369 /*ARGSUSED3*/
370 static int
371 sdev_lookup(struct vnode *dvp, char *nm, struct vnode **vpp,
372     struct pathname *pnp, int flags, struct vnode *rdir, struct cred *cred)
373 {
374 	struct sdev_node *parent;
375 	int error;
376 
377 	parent = VTOSDEV(dvp);
378 	ASSERT(parent);
379 
380 	/* execute access is required to search the directory */
381 	if ((error = VOP_ACCESS(dvp, VEXEC, 0, cred)) != 0)
382 		return (error);
383 
384 	if (!SDEV_IS_GLOBAL(parent))
385 		return (prof_lookup(dvp, nm, vpp, cred));
386 	return (devname_lookup_func(parent, nm, vpp, cred, NULL, 0));
387 }
388 
389 /*ARGSUSED2*/
390 static int
391 sdev_create(struct vnode *dvp, char *nm, struct vattr *vap, vcexcl_t excl,
392     int mode, struct vnode **vpp, struct cred *cred, int flag)
393 {
394 	struct vnode		*vp = NULL;
395 	struct vnode		*avp;
396 	struct sdev_node	*parent;
397 	struct sdev_node	*self = NULL;
398 	int			error = 0;
399 	vtype_t			type = vap->va_type;
400 
401 	ASSERT(type != VNON && type != VBAD);
402 
403 	if ((type == VFIFO) || (type == VSOCK) ||
404 	    (type == VPROC) || (type == VPORT))
405 		return (ENOTSUP);
406 
407 	parent = VTOSDEV(dvp);
408 	ASSERT(parent);
409 
410 	rw_enter(&parent->sdev_dotdot->sdev_contents, RW_READER);
411 	if (parent->sdev_state == SDEV_ZOMBIE) {
412 		rw_exit(&parent->sdev_dotdot->sdev_contents);
413 		return (ENOENT);
414 	}
415 
416 	/* non-global do not allow pure node creation */
417 	if (!SDEV_IS_GLOBAL(parent)) {
418 		rw_exit(&parent->sdev_dotdot->sdev_contents);
419 		return (prof_lookup(dvp, nm, vpp, cred));
420 	}
421 	rw_exit(&parent->sdev_dotdot->sdev_contents);
422 
423 	/* execute access is required to search the directory */
424 	if ((error = VOP_ACCESS(dvp, VEXEC|VWRITE, 0, cred)) != 0)
425 		return (error);
426 
427 	/* check existing name */
428 	error = VOP_LOOKUP(dvp, nm, &vp, NULL, 0, NULL, cred);
429 
430 	/* name found */
431 	if (error == 0) {
432 		ASSERT(vp);
433 		if (excl == EXCL) {
434 			error = EEXIST;
435 		} else if ((vp->v_type == VDIR) && (mode & VWRITE)) {
436 			/* allowing create/read-only an existing directory */
437 			error = EISDIR;
438 		} else {
439 			error = VOP_ACCESS(vp, mode, flag, cred);
440 		}
441 
442 		if (error) {
443 			VN_RELE(vp);
444 			return (error);
445 		}
446 
447 		/* truncation first */
448 		if ((vp->v_type == VREG) && (vap->va_mask & AT_SIZE) &&
449 		    (vap->va_size == 0)) {
450 			ASSERT(parent->sdev_attrvp);
451 			error = VOP_CREATE(parent->sdev_attrvp,
452 			    nm, vap, excl, mode, &avp, cred, flag);
453 
454 			if (error) {
455 				VN_RELE(vp);
456 				return (error);
457 			}
458 		}
459 
460 		sdev_update_timestamps(vp, kcred,
461 		    AT_CTIME|AT_MTIME|AT_ATIME);
462 		*vpp = vp;
463 		return (0);
464 	}
465 
466 	/* bail out early */
467 	if (error != ENOENT)
468 		return (error);
469 
470 	/*
471 	 * For memory-based (ROFS) directory:
472 	 * 	- either disallow node creation;
473 	 *	- or implement VOP_CREATE of its own
474 	 */
475 	rw_enter(&parent->sdev_contents, RW_WRITER);
476 	if (!SDEV_IS_PERSIST(parent)) {
477 		rw_exit(&parent->sdev_contents);
478 		return (ENOTSUP);
479 	}
480 	ASSERT(parent->sdev_attrvp);
481 	error = sdev_mknode(parent, nm, &self, vap, NULL, NULL,
482 	    cred, SDEV_READY);
483 	if (error) {
484 		rw_exit(&parent->sdev_contents);
485 		if (self)
486 			SDEV_RELE(self);
487 		return (error);
488 	}
489 	rw_exit(&parent->sdev_contents);
490 
491 	ASSERT(self);
492 	/* take care the timestamps for the node and its parent */
493 	sdev_update_timestamps(SDEVTOV(self), kcred,
494 	    AT_CTIME|AT_MTIME|AT_ATIME);
495 	sdev_update_timestamps(dvp, kcred, AT_MTIME|AT_ATIME);
496 	if (SDEV_IS_GLOBAL(parent))
497 		atomic_inc_ulong(&parent->sdev_gdir_gen);
498 
499 	/* wake up other threads blocked on looking up this node */
500 	mutex_enter(&self->sdev_lookup_lock);
501 	SDEV_UNBLOCK_OTHERS(self, SDEV_LOOKUP);
502 	mutex_exit(&self->sdev_lookup_lock);
503 	error = sdev_to_vp(self, vpp);
504 	return (error);
505 }
506 
507 static int
508 sdev_remove(struct vnode *dvp, char *nm, struct cred *cred)
509 {
510 	int	error;
511 	struct sdev_node *parent = (struct sdev_node *)VTOSDEV(dvp);
512 	struct vnode *vp = NULL;
513 	struct sdev_node *dv = NULL;
514 	struct devname_nsmap *map = NULL;
515 	struct devname_ops *dirops = NULL;
516 	int (*fn)(devname_handle_t *);
517 	int len;
518 	int bkstore = 0;
519 
520 	/* bail out early */
521 	len = strlen(nm);
522 	if (nm[0] == '.') {
523 		if (len == 1) {
524 			return (EINVAL);
525 		} else if (len == 2 && nm[1] == '.') {
526 			return (EEXIST);
527 		}
528 	}
529 
530 	ASSERT(parent);
531 	rw_enter(&parent->sdev_contents, RW_READER);
532 	if (!SDEV_IS_GLOBAL(parent)) {
533 		rw_exit(&parent->sdev_contents);
534 		return (ENOTSUP);
535 	}
536 
537 	/* execute access is required to search the directory */
538 	if ((error = VOP_ACCESS(dvp, VEXEC, 0, cred)) != 0) {
539 		rw_exit(&parent->sdev_contents);
540 		return (error);
541 	}
542 
543 	/* check existence first */
544 	dv = sdev_cache_lookup(parent, nm);
545 	if (dv == NULL) {
546 		rw_exit(&parent->sdev_contents);
547 		return (ENOENT);
548 	}
549 
550 	vp = SDEVTOV(dv);
551 	if ((dv->sdev_state == SDEV_INIT) ||
552 	    (dv->sdev_state == SDEV_ZOMBIE)) {
553 		rw_exit(&parent->sdev_contents);
554 		VN_RELE(vp);
555 		return (ENOENT);
556 	}
557 
558 	/* write access is required to remove an entry */
559 	if ((error = VOP_ACCESS(dvp, VWRITE, 0, cred)) != 0) {
560 		rw_exit(&parent->sdev_contents);
561 		VN_RELE(vp);
562 		return (error);
563 	}
564 
565 	/* the module may record/reject removing a device node */
566 	map = sdev_get_map(parent, 0);
567 	dirops = map ? map->dir_ops : NULL;
568 	if (dirops && ((fn = dirops->devnops_remove) != NULL)) {
569 		error = (*fn)(&(dv->sdev_handle));
570 		if (error) {
571 			rw_exit(&parent->sdev_contents);
572 			VN_RELE(vp);
573 			return (error);
574 		}
575 	}
576 
577 	/*
578 	 * sdev_dirdelete does the real job of:
579 	 *  - make sure no open ref count
580 	 *  - destroying the sdev_node
581 	 *  - releasing the hold on attrvp
582 	 */
583 	bkstore = SDEV_IS_PERSIST(dv) ? 1 : 0;
584 	if (!rw_tryupgrade(&parent->sdev_contents)) {
585 		rw_exit(&parent->sdev_contents);
586 		rw_enter(&parent->sdev_contents, RW_WRITER);
587 	}
588 	error = sdev_cache_update(parent, &dv, nm, SDEV_CACHE_DELETE);
589 	rw_exit(&parent->sdev_contents);
590 
591 	sdcmn_err2(("sdev_remove: cache_update error %d\n", error));
592 	if (error && (error != EBUSY)) {
593 		/* report errors other than EBUSY */
594 		VN_RELE(vp);
595 	} else {
596 		sdcmn_err2(("sdev_remove: cleaning node %s from cache "
597 		    " with error %d\n", nm, error));
598 
599 		/*
600 		 * best efforts clean up the backing store
601 		 */
602 		if (bkstore) {
603 			ASSERT(parent->sdev_attrvp);
604 			error = VOP_REMOVE(parent->sdev_attrvp, nm, cred);
605 			/*
606 			 * do not report BUSY error
607 			 * because the backing store ref count is released
608 			 * when the last ref count on the sdev_node is
609 			 * released.
610 			 */
611 			if (error == EBUSY) {
612 				sdcmn_err2(("sdev_remove: device %s is still on"
613 				    "disk %s\n", nm, parent->sdev_path));
614 				error = 0;
615 			}
616 		}
617 
618 		if (error == EBUSY)
619 			error = 0;
620 	}
621 
622 	return (error);
623 }
624 
625 /*
626  * Some restrictions for this file system:
627  *  - both oldnm and newnm are in the scope of /dev file system,
628  *    to simply the namespace management model.
629  */
630 static int
631 sdev_rename(struct vnode *odvp, char *onm, struct vnode *ndvp, char *nnm,
632     struct cred *cred)
633 {
634 	struct sdev_node	*fromparent = NULL;
635 	struct vattr		vattr;
636 	struct sdev_node	*toparent;
637 	struct sdev_node	*fromdv = NULL;	/* source node */
638 	struct vnode 		*ovp = NULL;	/* source vnode */
639 	struct sdev_node	*todv = NULL;	/* destination node */
640 	struct vnode 		*nvp = NULL;	/* destination vnode */
641 	int			samedir = 0;	/* set if odvp == ndvp */
642 	struct vnode		*realvp;
643 	int			len;
644 	char			nnm_path[MAXPATHLEN];
645 	struct devname_nsmap 	*omap = NULL;
646 	struct devname_ops	*odirops = NULL;
647 	int (*fn)(devname_handle_t *, char *);
648 	int (*rmfn)(devname_handle_t *);
649 	int error = 0;
650 	dev_t fsid;
651 	int bkstore = 0;
652 	vtype_t type;
653 
654 	/* prevent modifying "." and ".." */
655 	if ((onm[0] == '.' &&
656 	    (onm[1] == '\0' || (onm[1] == '.' && onm[2] == '\0'))) ||
657 	    (nnm[0] == '.' &&
658 	    (nnm[1] == '\0' || (nnm[1] == '.' && nnm[2] == '\0')))) {
659 		return (EINVAL);
660 	}
661 
662 	fromparent = VTOSDEV(odvp);
663 	toparent = VTOSDEV(ndvp);
664 
665 	/* ZOMBIE parent doesn't allow new node creation */
666 	rw_enter(&fromparent->sdev_dotdot->sdev_contents, RW_READER);
667 	if (fromparent->sdev_state == SDEV_ZOMBIE) {
668 		rw_exit(&fromparent->sdev_dotdot->sdev_contents);
669 		return (ENOENT);
670 	}
671 
672 	/* renaming only supported for global device nodes */
673 	if (!SDEV_IS_GLOBAL(fromparent)) {
674 		rw_exit(&fromparent->sdev_dotdot->sdev_contents);
675 		return (ENOTSUP);
676 	}
677 	rw_exit(&fromparent->sdev_dotdot->sdev_contents);
678 
679 	rw_enter(&toparent->sdev_dotdot->sdev_contents, RW_READER);
680 	if (toparent->sdev_state == SDEV_ZOMBIE) {
681 		rw_exit(&toparent->sdev_dotdot->sdev_contents);
682 		return (ENOENT);
683 	}
684 	rw_exit(&toparent->sdev_dotdot->sdev_contents);
685 
686 	/*
687 	 * acquire the global lock to prevent
688 	 * mount/unmount/other rename activities.
689 	 */
690 	mutex_enter(&sdev_lock);
691 
692 	/* check existence of the source node */
693 	error = VOP_LOOKUP(odvp, onm, &ovp, NULL, 0, NULL, cred);
694 	if (error) {
695 		sdcmn_err2(("sdev_rename: the source node %s exists\n",
696 		    onm));
697 		mutex_exit(&sdev_lock);
698 		return (error);
699 	}
700 
701 	if (VOP_REALVP(ovp, &realvp) == 0) {
702 		VN_HOLD(realvp);
703 		VN_RELE(ovp);
704 		ovp = realvp;
705 	}
706 
707 	/* check existence of destination */
708 	error = VOP_LOOKUP(ndvp, nnm, &nvp, NULL, 0, NULL, cred);
709 	if (error && (error != ENOENT)) {
710 		mutex_exit(&sdev_lock);
711 		VN_RELE(ovp);
712 		return (error);
713 	}
714 
715 	if (nvp && (VOP_REALVP(nvp, &realvp) == 0)) {
716 		VN_HOLD(realvp);
717 		VN_RELE(nvp);
718 		nvp = realvp;
719 	}
720 
721 	/*
722 	 * make sure the source and the destination are
723 	 * in the same dev filesystem
724 	 */
725 	if (odvp != ndvp) {
726 		vattr.va_mask = AT_FSID;
727 		if (error = VOP_GETATTR(odvp, &vattr, 0, cred)) {
728 			mutex_exit(&sdev_lock);
729 			VN_RELE(ovp);
730 			return (error);
731 		}
732 		fsid = vattr.va_fsid;
733 		vattr.va_mask = AT_FSID;
734 		if (error = VOP_GETATTR(ndvp, &vattr, 0, cred)) {
735 			mutex_exit(&sdev_lock);
736 			VN_RELE(ovp);
737 			return (error);
738 		}
739 		if (fsid != vattr.va_fsid) {
740 			mutex_exit(&sdev_lock);
741 			VN_RELE(ovp);
742 			return (EXDEV);
743 		}
744 	}
745 
746 	/* make sure the old entry can be deleted */
747 	error = VOP_ACCESS(odvp, VWRITE, 0, cred);
748 	if (error) {
749 		mutex_exit(&sdev_lock);
750 		VN_RELE(ovp);
751 		return (error);
752 	}
753 
754 	/* make sure the destination allows creation */
755 	samedir = (fromparent == toparent);
756 	if (!samedir) {
757 		error = VOP_ACCESS(ndvp, VEXEC|VWRITE, 0, cred);
758 		if (error) {
759 			mutex_exit(&sdev_lock);
760 			VN_RELE(ovp);
761 			return (error);
762 		}
763 	}
764 
765 	fromdv = VTOSDEV(ovp);
766 	ASSERT(fromdv);
767 
768 	/* check with the plug-in modules for the source directory */
769 	rw_enter(&fromparent->sdev_contents, RW_READER);
770 	omap = sdev_get_map(fromparent, 0);
771 	rw_exit(&fromparent->sdev_contents);
772 	odirops = omap ? omap->dir_ops : NULL;
773 	if (odirops && ((fn = odirops->devnops_rename) != NULL)) {
774 		if (samedir) {
775 			error = (*fn)(&(fromdv->sdev_handle), nnm);
776 		} else {
777 			len = strlen(nnm) + strlen(toparent->sdev_name) + 2;
778 			(void) snprintf(nnm_path, len, "%s/%s",
779 			    toparent->sdev_name, nnm);
780 			error = (*fn)(&(fromdv->sdev_handle), nnm);
781 		}
782 
783 		if (error) {
784 			mutex_exit(&sdev_lock);
785 			sdcmn_err2(("sdev_rename: DBNR doesn't "
786 			    "allow rename, error %d", error));
787 			VN_RELE(ovp);
788 			return (error);
789 		}
790 	}
791 
792 	/* destination file exists */
793 	if (nvp) {
794 		todv = VTOSDEV(nvp);
795 		ASSERT(todv);
796 	}
797 
798 	/*
799 	 * link source to new target in the memory
800 	 */
801 	error = sdev_rnmnode(fromparent, fromdv, toparent, &todv, nnm, cred);
802 	if (error) {
803 		sdcmn_err2(("sdev_rename: renaming %s to %s failed "
804 		    " with error %d\n", onm, nnm, error));
805 		mutex_exit(&sdev_lock);
806 		if (nvp)
807 			VN_RELE(nvp);
808 		VN_RELE(ovp);
809 		return (error);
810 	}
811 
812 	/* notify the DBNR module the node is going away */
813 	if (odirops && ((rmfn = odirops->devnops_remove) != NULL)) {
814 		(void) (*rmfn)(&(fromdv->sdev_handle));
815 	}
816 
817 	/*
818 	 * unlink from source
819 	 */
820 	rw_enter(&fromparent->sdev_contents, RW_READER);
821 	fromdv = sdev_cache_lookup(fromparent, onm);
822 	if (fromdv == NULL) {
823 		rw_exit(&fromparent->sdev_contents);
824 		mutex_exit(&sdev_lock);
825 		sdcmn_err2(("sdev_rename: the source is deleted already\n"));
826 		return (0);
827 	}
828 
829 	if (fromdv->sdev_state == SDEV_ZOMBIE) {
830 		rw_exit(&fromparent->sdev_contents);
831 		mutex_exit(&sdev_lock);
832 		VN_RELE(SDEVTOV(fromdv));
833 		sdcmn_err2(("sdev_rename: the source is being deleted\n"));
834 		return (0);
835 	}
836 	rw_exit(&fromparent->sdev_contents);
837 	ASSERT(SDEVTOV(fromdv) == ovp);
838 	VN_RELE(ovp);
839 
840 	/* clean out the directory contents before it can be removed */
841 	type = SDEVTOV(fromdv)->v_type;
842 	if (type == VDIR) {
843 		error = sdev_cleandir(fromdv, NULL, 0);
844 		sdcmn_err2(("sdev_rename: cleandir finished with %d\n",
845 		    error));
846 		if (error == EBUSY)
847 			error = 0;
848 	}
849 
850 	rw_enter(&fromparent->sdev_contents, RW_WRITER);
851 	bkstore = SDEV_IS_PERSIST(fromdv) ? 1 : 0;
852 	error = sdev_cache_update(fromparent, &fromdv, onm,
853 	    SDEV_CACHE_DELETE);
854 
855 	/* best effforts clean up the backing store */
856 	if (bkstore) {
857 		ASSERT(fromparent->sdev_attrvp);
858 		if (type != VDIR) {
859 			error = VOP_REMOVE(fromparent->sdev_attrvp,
860 			    onm, kcred);
861 		} else {
862 			error = VOP_RMDIR(fromparent->sdev_attrvp,
863 			    onm, fromparent->sdev_attrvp, kcred);
864 		}
865 
866 		if (error) {
867 			sdcmn_err2(("sdev_rename: device %s is "
868 			    "still on disk %s\n", onm,
869 			    fromparent->sdev_path));
870 			error = 0;
871 		}
872 	}
873 	rw_exit(&fromparent->sdev_contents);
874 	mutex_exit(&sdev_lock);
875 
876 	/* once reached to this point, the rename is regarded successful */
877 	return (0);
878 }
879 
880 /*
881  * dev-fs version of "ln -s path dev-name"
882  *	tnm - path, e.g. /devices/... or /dev/...
883  *	lnm - dev_name
884  */
885 static int
886 sdev_symlink(struct vnode *dvp, char *lnm, struct vattr *tva,
887     char *tnm, struct cred *cred)
888 {
889 	int error;
890 	struct vnode *vp = NULL;
891 	struct sdev_node *parent = (struct sdev_node *)VTOSDEV(dvp);
892 	struct sdev_node *self = (struct sdev_node *)NULL;
893 
894 	ASSERT(parent);
895 	rw_enter(&parent->sdev_dotdot->sdev_contents, RW_READER);
896 	if (parent->sdev_state == SDEV_ZOMBIE) {
897 		rw_exit(&parent->sdev_dotdot->sdev_contents);
898 		sdcmn_err2(("sdev_symlink: parent %s is ZOMBIED \n",
899 		    parent->sdev_name));
900 		return (ENOENT);
901 	}
902 
903 	if (!SDEV_IS_GLOBAL(parent)) {
904 		rw_exit(&parent->sdev_dotdot->sdev_contents);
905 		return (ENOTSUP);
906 	}
907 	rw_exit(&parent->sdev_dotdot->sdev_contents);
908 
909 	/* execute access is required to search a directory */
910 	if ((error = VOP_ACCESS(dvp, VEXEC, 0, cred)) != 0)
911 		return (error);
912 
913 	/* find existing name */
914 	error = VOP_LOOKUP(dvp, lnm, &vp, NULL, 0, NULL, cred);
915 	if (error == 0) {
916 		ASSERT(vp);
917 		VN_RELE(vp);
918 		sdcmn_err2(("sdev_symlink: node %s already exists\n", lnm));
919 		return (EEXIST);
920 	}
921 	if (error != ENOENT)
922 		return (error);
923 
924 	/* write access is required to create a symlink */
925 	if ((error = VOP_ACCESS(dvp, VWRITE, 0, cred)) != 0)
926 		return (error);
927 
928 	/* put it into memory cache */
929 	rw_enter(&parent->sdev_contents, RW_WRITER);
930 	error = sdev_mknode(parent, lnm, &self, tva, NULL, (void *)tnm,
931 	    cred, SDEV_READY);
932 	if (error) {
933 		rw_exit(&parent->sdev_contents);
934 		sdcmn_err2(("sdev_symlink: node %s creation failed\n", lnm));
935 		if (self)
936 			SDEV_RELE(self);
937 
938 		return (error);
939 	}
940 	ASSERT(self && (self->sdev_state == SDEV_READY));
941 	rw_exit(&parent->sdev_contents);
942 
943 	/* take care the timestamps for the node and its parent */
944 	sdev_update_timestamps(SDEVTOV(self), kcred,
945 	    AT_CTIME|AT_MTIME|AT_ATIME);
946 	sdev_update_timestamps(dvp, kcred, AT_MTIME|AT_ATIME);
947 	if (SDEV_IS_GLOBAL(parent))
948 		atomic_inc_ulong(&parent->sdev_gdir_gen);
949 
950 	/* wake up other threads blocked on looking up this node */
951 	mutex_enter(&self->sdev_lookup_lock);
952 	SDEV_UNBLOCK_OTHERS(self, SDEV_LOOKUP);
953 	mutex_exit(&self->sdev_lookup_lock);
954 	SDEV_RELE(self);	/* don't return with vnode held */
955 	return (0);
956 }
957 
958 static int
959 sdev_mkdir(struct vnode *dvp, char *nm, struct vattr *va, struct vnode **vpp,
960     struct cred *cred)
961 {
962 	int error;
963 	struct sdev_node *parent = (struct sdev_node *)VTOSDEV(dvp);
964 	struct sdev_node *self = NULL;
965 	struct vnode	*vp = NULL;
966 
967 	ASSERT(parent && parent->sdev_dotdot);
968 	rw_enter(&parent->sdev_dotdot->sdev_contents, RW_READER);
969 	if (parent->sdev_state == SDEV_ZOMBIE) {
970 		rw_exit(&parent->sdev_dotdot->sdev_contents);
971 		return (ENOENT);
972 	}
973 
974 	/* non-global do not allow pure directory creation */
975 	if (!SDEV_IS_GLOBAL(parent)) {
976 		rw_exit(&parent->sdev_dotdot->sdev_contents);
977 		return (prof_lookup(dvp, nm, vpp, cred));
978 	}
979 	rw_exit(&parent->sdev_dotdot->sdev_contents);
980 
981 	/* execute access is required to search the directory */
982 	if ((error = VOP_ACCESS(dvp, VEXEC, 0, cred)) != 0) {
983 		return (error);
984 	}
985 
986 	/* find existing name */
987 	error = VOP_LOOKUP(dvp, nm, &vp, NULL, 0, NULL, cred);
988 	if (error == 0) {
989 		VN_RELE(vp);
990 		return (EEXIST);
991 	}
992 	if (error != ENOENT)
993 		return (error);
994 
995 	/* require write access to create a directory */
996 	if ((error = VOP_ACCESS(dvp, VWRITE, 0, cred)) != 0) {
997 		return (error);
998 	}
999 
1000 	/* put it into memory */
1001 	rw_enter(&parent->sdev_contents, RW_WRITER);
1002 	error = sdev_mknode(parent, nm, &self,
1003 	    va, NULL, NULL, cred, SDEV_READY);
1004 	if (error) {
1005 		rw_exit(&parent->sdev_contents);
1006 		if (self)
1007 			SDEV_RELE(self);
1008 		return (error);
1009 	}
1010 	ASSERT(self && (self->sdev_state == SDEV_READY));
1011 	rw_exit(&parent->sdev_contents);
1012 
1013 	/* take care the timestamps for the node and its parent */
1014 	sdev_update_timestamps(SDEVTOV(self), kcred,
1015 	    AT_CTIME|AT_MTIME|AT_ATIME);
1016 	sdev_update_timestamps(dvp, kcred, AT_MTIME|AT_ATIME);
1017 	if (SDEV_IS_GLOBAL(parent))
1018 		atomic_inc_ulong(&parent->sdev_gdir_gen);
1019 
1020 	/* wake up other threads blocked on looking up this node */
1021 	mutex_enter(&self->sdev_lookup_lock);
1022 	SDEV_UNBLOCK_OTHERS(self, SDEV_LOOKUP);
1023 	mutex_exit(&self->sdev_lookup_lock);
1024 	*vpp = SDEVTOV(self);
1025 	return (0);
1026 }
1027 
1028 /*
1029  * allowing removing an empty directory under /dev
1030  */
1031 /*ARGSUSED*/
1032 static int
1033 sdev_rmdir(struct vnode *dvp, char *nm, struct vnode *cdir, struct cred *cred)
1034 {
1035 	int error = 0;
1036 	struct sdev_node *parent = (struct sdev_node *)VTOSDEV(dvp);
1037 	struct sdev_node *self = NULL;
1038 	struct vnode *vp = NULL;
1039 
1040 	/* bail out early */
1041 	if (strcmp(nm, ".") == 0)
1042 		return (EINVAL);
1043 	if (strcmp(nm, "..") == 0)
1044 		return (EEXIST); /* should be ENOTEMPTY */
1045 
1046 	/* no destruction of non-global node */
1047 	ASSERT(parent && parent->sdev_dotdot);
1048 	rw_enter(&parent->sdev_dotdot->sdev_contents, RW_READER);
1049 	if (!SDEV_IS_GLOBAL(parent)) {
1050 		rw_exit(&parent->sdev_dotdot->sdev_contents);
1051 		return (ENOTSUP);
1052 	}
1053 	rw_exit(&parent->sdev_dotdot->sdev_contents);
1054 
1055 	/* execute access is required to search the directory */
1056 	if ((error = VOP_ACCESS(dvp, VEXEC, 0, cred)) != 0)
1057 		return (error);
1058 
1059 	/* check existing name */
1060 	rw_enter(&parent->sdev_contents, RW_WRITER);
1061 	self = sdev_cache_lookup(parent, nm);
1062 	if (self == NULL) {
1063 		rw_exit(&parent->sdev_contents);
1064 		return (ENOENT);
1065 	}
1066 
1067 	vp = SDEVTOV(self);
1068 	if ((self->sdev_state == SDEV_INIT) ||
1069 	    (self->sdev_state == SDEV_ZOMBIE)) {
1070 		rw_exit(&parent->sdev_contents);
1071 		VN_RELE(vp);
1072 		return (ENOENT);
1073 	}
1074 
1075 	/* write access is required to remove a directory */
1076 	if ((error = VOP_ACCESS(dvp, VWRITE, 0, cred)) != 0) {
1077 		rw_exit(&parent->sdev_contents);
1078 		VN_RELE(vp);
1079 		return (error);
1080 	}
1081 
1082 	/* some sanity checks */
1083 	if (vp == dvp || vp == cdir) {
1084 		rw_exit(&parent->sdev_contents);
1085 		VN_RELE(vp);
1086 		return (EINVAL);
1087 	}
1088 
1089 	if (vp->v_type != VDIR) {
1090 		rw_exit(&parent->sdev_contents);
1091 		VN_RELE(vp);
1092 		return (ENOTDIR);
1093 	}
1094 
1095 	if (vn_vfswlock(vp)) {
1096 		rw_exit(&parent->sdev_contents);
1097 		VN_RELE(vp);
1098 		return (EBUSY);
1099 	}
1100 
1101 	if (vn_mountedvfs(vp) != NULL) {
1102 		rw_exit(&parent->sdev_contents);
1103 		vn_vfsunlock(vp);
1104 		VN_RELE(vp);
1105 		return (EBUSY);
1106 	}
1107 
1108 	self = VTOSDEV(vp);
1109 	/* bail out on a non-empty directory */
1110 	rw_enter(&self->sdev_contents, RW_READER);
1111 	if (self->sdev_nlink > 2) {
1112 		rw_exit(&self->sdev_contents);
1113 		rw_exit(&parent->sdev_contents);
1114 		vn_vfsunlock(vp);
1115 		VN_RELE(vp);
1116 		return (ENOTEMPTY);
1117 	}
1118 	rw_exit(&self->sdev_contents);
1119 
1120 	/* unlink it from the directory cache */
1121 	error = sdev_cache_update(parent, &self, nm, SDEV_CACHE_DELETE);
1122 	rw_exit(&parent->sdev_contents);
1123 	vn_vfsunlock(vp);
1124 
1125 	if (error && (error != EBUSY)) {
1126 		VN_RELE(vp);
1127 	} else {
1128 		sdcmn_err2(("sdev_rmdir: cleaning node %s from directory "
1129 		    " cache with error %d\n", nm, error));
1130 
1131 		/* best effort to clean up the backing store */
1132 		if (SDEV_IS_PERSIST(parent)) {
1133 			ASSERT(parent->sdev_attrvp);
1134 			error = VOP_RMDIR(parent->sdev_attrvp, nm,
1135 			    parent->sdev_attrvp, kcred);
1136 			sdcmn_err2(("sdev_rmdir: cleaning device %s is on"
1137 			    " disk error %d\n", parent->sdev_path, error));
1138 		}
1139 
1140 		if (error == EBUSY)
1141 			error = 0;
1142 	}
1143 
1144 	return (error);
1145 }
1146 
1147 /*
1148  * read the contents of a symbolic link
1149  */
1150 static int
1151 sdev_readlink(struct vnode *vp, struct uio *uiop, struct cred *cred)
1152 {
1153 	struct sdev_node *dv;
1154 	int	error = 0;
1155 
1156 	ASSERT(vp->v_type == VLNK);
1157 
1158 	dv = VTOSDEV(vp);
1159 
1160 	if (dv->sdev_attrvp) {
1161 		/* non-NULL attrvp implys a persisted node at READY state */
1162 		return (VOP_READLINK(dv->sdev_attrvp, uiop, cred));
1163 	} else if (dv->sdev_symlink != NULL) {
1164 		/* memory nodes, e.g. local nodes */
1165 		rw_enter(&dv->sdev_contents, RW_READER);
1166 		sdcmn_err2(("sdev_readlink link is %s\n", dv->sdev_symlink));
1167 		error = uiomove(dv->sdev_symlink, strlen(dv->sdev_symlink),
1168 		    UIO_READ, uiop);
1169 		rw_exit(&dv->sdev_contents);
1170 		return (error);
1171 	}
1172 
1173 	return (ENOENT);
1174 }
1175 
1176 static int
1177 sdev_readdir(struct vnode *dvp, struct uio *uiop, struct cred *cred, int *eofp)
1178 {
1179 	struct sdev_node *parent = VTOSDEV(dvp);
1180 	int error;
1181 
1182 	/* execute access is required to search the directory */
1183 	if ((error = VOP_ACCESS(dvp, VEXEC, 0, cred)) != 0)
1184 		return (error);
1185 
1186 	ASSERT(parent);
1187 	if (!SDEV_IS_GLOBAL(parent))
1188 		prof_filldir(parent);
1189 	return (devname_readdir_func(dvp, uiop, cred, eofp, SDEV_BROWSE));
1190 }
1191 
1192 /*ARGSUSED1*/
1193 static void
1194 sdev_inactive(struct vnode *vp, struct cred *cred)
1195 {
1196 	int clean;
1197 	struct sdev_node *dv = VTOSDEV(vp);
1198 	struct sdev_node *ddv = dv->sdev_dotdot;
1199 	struct sdev_node *idv;
1200 	struct sdev_node *prev = NULL;
1201 	int state;
1202 	struct devname_nsmap *map = NULL;
1203 	struct devname_ops	*dirops = NULL;
1204 	void (*fn)(devname_handle_t *, struct cred *) = NULL;
1205 
1206 	rw_enter(&ddv->sdev_contents, RW_WRITER);
1207 	state = dv->sdev_state;
1208 
1209 	mutex_enter(&vp->v_lock);
1210 	ASSERT(vp->v_count >= 1);
1211 
1212 	clean = (vp->v_count == 1) && (state == SDEV_ZOMBIE);
1213 
1214 	/*
1215 	 * last ref count on the ZOMBIE node is released.
1216 	 * clean up the sdev_node, and
1217 	 * release the hold on the backing store node so that
1218 	 * the ZOMBIE backing stores also cleaned out.
1219 	 */
1220 	if (clean) {
1221 		ASSERT(ddv);
1222 		if (SDEV_IS_GLOBAL(dv)) {
1223 			map = ddv->sdev_mapinfo;
1224 			dirops = map ? map->dir_ops : NULL;
1225 			if (dirops && (fn = dirops->devnops_inactive))
1226 				(*fn)(&(dv->sdev_handle), cred);
1227 		}
1228 
1229 		ddv->sdev_nlink--;
1230 		if (vp->v_type == VDIR) {
1231 			dv->sdev_nlink--;
1232 		}
1233 		for (idv = ddv->sdev_dot; idv && idv != dv;
1234 		    prev = idv, idv = idv->sdev_next)
1235 			;
1236 		ASSERT(idv == dv);
1237 		if (prev == NULL)
1238 			ddv->sdev_dot = dv->sdev_next;
1239 		else
1240 			prev->sdev_next = dv->sdev_next;
1241 		dv->sdev_next = NULL;
1242 		dv->sdev_nlink--;
1243 		--vp->v_count;
1244 		mutex_exit(&vp->v_lock);
1245 		sdev_nodedestroy(dv, 0);
1246 	} else {
1247 		--vp->v_count;
1248 		mutex_exit(&vp->v_lock);
1249 	}
1250 	rw_exit(&ddv->sdev_contents);
1251 }
1252 
1253 static int
1254 sdev_fid(struct vnode *vp, struct fid *fidp)
1255 {
1256 	struct sdev_node	*dv = VTOSDEV(vp);
1257 	struct sdev_fid	*sdev_fid;
1258 
1259 	if (fidp->fid_len < (sizeof (struct sdev_fid) - sizeof (ushort_t))) {
1260 		fidp->fid_len = sizeof (struct sdev_fid) - sizeof (ushort_t);
1261 		return (ENOSPC);
1262 	}
1263 
1264 	sdev_fid = (struct sdev_fid *)fidp;
1265 	bzero(sdev_fid, sizeof (struct sdev_fid));
1266 	sdev_fid->sdevfid_len =
1267 	    (int)sizeof (struct sdev_fid) - sizeof (ushort_t);
1268 	sdev_fid->sdevfid_ino = dv->sdev_ino;
1269 
1270 	return (0);
1271 }
1272 
1273 /*
1274  * This pair of routines bracket all VOP_READ, VOP_WRITE
1275  * and VOP_READDIR requests.  The contents lock stops things
1276  * moving around while we're looking at them.
1277  */
1278 /*ARGSUSED2*/
1279 static int
1280 sdev_rwlock(struct vnode *vp, int write_flag, caller_context_t *ctp)
1281 {
1282 	rw_enter(&VTOSDEV(vp)->sdev_contents,
1283 	    write_flag ? RW_WRITER : RW_READER);
1284 	return (write_flag ? V_WRITELOCK_TRUE : V_WRITELOCK_FALSE);
1285 }
1286 
1287 /*ARGSUSED1*/
1288 static void
1289 sdev_rwunlock(struct vnode *vp, int write_flag, caller_context_t *ctp)
1290 {
1291 	rw_exit(&VTOSDEV(vp)->sdev_contents);
1292 }
1293 
1294 /*ARGSUSED1*/
1295 static int
1296 sdev_seek(struct vnode *vp, offset_t ooff, offset_t *noffp)
1297 {
1298 	struct vnode *attrvp = VTOSDEV(vp)->sdev_attrvp;
1299 
1300 	ASSERT(vp->v_type != VCHR &&
1301 	    vp->v_type != VBLK && vp->v_type != VLNK);
1302 
1303 	if (vp->v_type == VDIR)
1304 		return (fs_seek(vp, ooff, noffp));
1305 
1306 	ASSERT(attrvp);
1307 	return (VOP_SEEK(attrvp, ooff, noffp));
1308 }
1309 
1310 /*ARGSUSED1*/
1311 static int
1312 sdev_frlock(struct vnode *vp, int cmd, struct flock64 *bfp, int flag,
1313     offset_t offset, struct flk_callback *flk_cbp, struct cred *cr)
1314 {
1315 	int error;
1316 	struct sdev_node *dv = VTOSDEV(vp);
1317 
1318 	ASSERT(dv);
1319 	ASSERT(dv->sdev_attrvp);
1320 	error = VOP_FRLOCK(dv->sdev_attrvp, cmd, bfp, flag, offset,
1321 	    flk_cbp, cr);
1322 
1323 	return (error);
1324 }
1325 
1326 static int
1327 sdev_setfl(struct vnode *vp, int oflags, int nflags, cred_t *cr)
1328 {
1329 	struct sdev_node *dv = VTOSDEV(vp);
1330 	ASSERT(dv);
1331 	ASSERT(dv->sdev_attrvp);
1332 
1333 	return (VOP_SETFL(dv->sdev_attrvp, oflags, nflags, cr));
1334 }
1335 
1336 static int
1337 sdev_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr)
1338 {
1339 	switch (cmd) {
1340 	case _PC_ACL_ENABLED:
1341 		*valp = SDEV_ACL_FLAVOR(vp);
1342 		return (0);
1343 	}
1344 
1345 	return (fs_pathconf(vp, cmd, valp, cr));
1346 }
1347 
1348 vnodeops_t *sdev_vnodeops;
1349 
1350 const fs_operation_def_t sdev_vnodeops_tbl[] = {
1351 	VOPNAME_OPEN,		{ .vop_open = sdev_open },
1352 	VOPNAME_CLOSE,		{ .vop_close = sdev_close },
1353 	VOPNAME_READ,		{ .vop_read = sdev_read },
1354 	VOPNAME_WRITE,		{ .vop_write = sdev_write },
1355 	VOPNAME_IOCTL,		{ .vop_ioctl = sdev_ioctl },
1356 	VOPNAME_GETATTR,	{ .vop_getattr = sdev_getattr },
1357 	VOPNAME_SETATTR,	{ .vop_setattr = sdev_setattr },
1358 	VOPNAME_ACCESS,		{ .vop_access = sdev_access },
1359 	VOPNAME_LOOKUP,		{ .vop_lookup = sdev_lookup },
1360 	VOPNAME_CREATE,		{ .vop_create = sdev_create },
1361 	VOPNAME_RENAME,		{ .vop_rename = sdev_rename },
1362 	VOPNAME_REMOVE,		{ .vop_remove = sdev_remove },
1363 	VOPNAME_MKDIR,		{ .vop_mkdir = sdev_mkdir },
1364 	VOPNAME_RMDIR,		{ .vop_rmdir = sdev_rmdir },
1365 	VOPNAME_READDIR,	{ .vop_readdir = sdev_readdir },
1366 	VOPNAME_SYMLINK,	{ .vop_symlink = sdev_symlink },
1367 	VOPNAME_READLINK,	{ .vop_readlink = sdev_readlink },
1368 	VOPNAME_INACTIVE,	{ .vop_inactive = sdev_inactive },
1369 	VOPNAME_FID,		{ .vop_fid = sdev_fid },
1370 	VOPNAME_RWLOCK,		{ .vop_rwlock = sdev_rwlock },
1371 	VOPNAME_RWUNLOCK,	{ .vop_rwunlock = sdev_rwunlock },
1372 	VOPNAME_SEEK,		{ .vop_seek = sdev_seek },
1373 	VOPNAME_FRLOCK,		{ .vop_frlock = sdev_frlock },
1374 	VOPNAME_PATHCONF,	{ .vop_pathconf = sdev_pathconf },
1375 	VOPNAME_SETFL,		{ .vop_setfl = sdev_setfl },
1376 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = sdev_setsecattr },
1377 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = sdev_getsecattr },
1378 	NULL,			NULL
1379 };
1380 
1381 int sdev_vnodeops_tbl_size = sizeof (sdev_vnodeops_tbl);
1382