xref: /titanic_52/usr/src/uts/common/fs/devfs/devfs_vnops.c (revision b02e9a2d4d2071d770e5aa9ae8f83f2bbe1f2ced)
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 devfs
30  *
31  * For leaf vnode special files (VCHR|VBLK) specfs will always see the VOP
32  * first because dv_find always performs leaf vnode substitution, returning
33  * a specfs vnode with an s_realvp pointing to the devfs leaf vnode. This
34  * means that the only leaf special file VOP operations that devfs will see
35  * after VOP_LOOKUP are the ones that specfs forwards.
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/dirent.h>
57 #include <sys/pathname.h>
58 #include <sys/cmn_err.h>
59 #include <sys/debug.h>
60 #include <sys/policy.h>
61 #include <sys/modctl.h>
62 
63 #include <fs/fs_subr.h>
64 #include <sys/fs/dv_node.h>
65 
66 extern struct vattr	dv_vattr_dir, dv_vattr_file;
67 extern dev_t rconsdev;
68 
69 /*
70  * Open of devices (leaf nodes) is handled by specfs.
71  * There is nothing to do to open a directory
72  */
73 /*ARGSUSED*/
74 static int
75 devfs_open(struct vnode **vpp, int flag, struct cred *cred,
76     caller_context_t *ct)
77 {
78 	struct dv_node	*dv = VTODV(*vpp);
79 
80 	dcmn_err2(("devfs_open %s\n", dv->dv_name));
81 	ASSERT((*vpp)->v_type == VDIR);
82 	return (0);
83 }
84 
85 /*
86  * Close of devices (leaf nodes) is handled by specfs.
87  * There is nothing much to do inorder to close a directory.
88  */
89 /*ARGSUSED1*/
90 static int
91 devfs_close(struct vnode *vp, int flag, int count,
92     offset_t offset, struct cred *cred, caller_context_t *ct)
93 {
94 	struct dv_node	*dv = VTODV(vp);
95 
96 	dcmn_err2(("devfs_close %s\n", dv->dv_name));
97 	ASSERT(vp->v_type == VDIR);
98 
99 	cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
100 	cleanshares(vp, ttoproc(curthread)->p_pid);
101 	return (0);
102 }
103 
104 /*
105  * Read of devices (leaf nodes) is handled by specfs.
106  * Read of directories is not supported.
107  */
108 /*ARGSUSED*/
109 static int
110 devfs_read(struct vnode *vp, struct uio *uiop, int ioflag, struct cred *cred,
111 	struct caller_context *ct)
112 {
113 	dcmn_err2(("devfs_read %s\n", VTODV(vp)->dv_name));
114 	ASSERT(vp->v_type == VDIR);
115 	ASSERT(RW_READ_HELD(&VTODV(vp)->dv_contents));
116 	return (EISDIR);
117 }
118 
119 /*
120  * Write of devices (leaf nodes) is handled by specfs.
121  * Write of directories is not supported.
122  */
123 /*ARGSUSED*/
124 static int
125 devfs_write(struct vnode *vp, struct uio *uiop, int ioflag, struct cred *cred,
126 	struct caller_context *ct)
127 {
128 	dcmn_err2(("devfs_write %s\n", VTODV(vp)->dv_name));
129 	ASSERT(vp->v_type == VDIR);
130 	ASSERT(RW_WRITE_HELD(&VTODV(vp)->dv_contents));
131 	return (EISDIR);
132 }
133 
134 /*
135  * Ioctls to device (leaf nodes) is handled by specfs.
136  * Ioctl to directories is not supported.
137  */
138 /*ARGSUSED*/
139 static int
140 devfs_ioctl(struct vnode *vp, int cmd, intptr_t arg, int flag,
141     struct cred *cred, int *rvalp, caller_context_t *ct)
142 {
143 	dcmn_err2(("devfs_ioctl %s\n", VTODV(vp)->dv_name));
144 	ASSERT(vp->v_type == VDIR);
145 
146 	return (ENOTTY);	/* no ioctls supported */
147 }
148 
149 /*
150  * We can be asked directly about the attributes of directories, or
151  * (via sp->s_realvp) about the filesystem attributes of special files.
152  *
153  * For directories, we just believe the attribute store
154  * though we mangle the nodeid, fsid, and rdev to convince userland we
155  * really are a different filesystem.
156  *
157  * For special files, a little more fakery is required.
158  *
159  * If the attribute store is not there (read only root), we believe our
160  * memory based attributes.
161  */
162 static int
163 devfs_getattr(struct vnode *vp, struct vattr *vap, int flags, struct cred *cr,
164     caller_context_t *ct)
165 {
166 	struct dv_node	*dv = VTODV(vp);
167 	int		error = 0;
168 	uint_t		mask;
169 
170 	/*
171 	 * Message goes to console only. Otherwise, the message
172 	 * causes devfs_getattr to be invoked again... infinite loop
173 	 */
174 	dcmn_err2(("?devfs_getattr %s\n", dv->dv_name));
175 	ASSERT(dv->dv_attr || dv->dv_attrvp);
176 
177 	if (!(vp->v_type == VDIR || vp->v_type == VCHR || vp->v_type == VBLK)) {
178 		cmn_err(CE_WARN,	/* panic ? */
179 		    "?%s: getattr on vnode type %d", dvnm, vp->v_type);
180 		return (ENOENT);
181 	}
182 
183 	if (dv->dv_attr) {
184 		/*
185 		 * obtain from the memory version of attribute.
186 		 * preserve mask for those that optimize.
187 		 * devfs specific fields are already merged on creation.
188 		 */
189 		mask = vap->va_mask;
190 		*vap = *dv->dv_attr;
191 		vap->va_mask = mask;
192 	} else {
193 		/* obtain from attribute store and merge */
194 		error = VOP_GETATTR(dv->dv_attrvp, vap, flags, cr, ct);
195 		dsysdebug(error, ("vop_getattr %s %d\n", dv->dv_name, error));
196 		dv_vattr_merge(dv, vap);
197 	}
198 
199 	/*
200 	 * Restrict the permissions of the node fronting the console
201 	 * to 0600 with root as the owner.  This prevents a non-root
202 	 * user from gaining access to a serial terminal (like /dev/term/a)
203 	 * which is in reality serving as the console device (/dev/console).
204 	 */
205 	if (vp->v_rdev == rconsdev) {
206 		mode_t	rconsmask = S_IXUSR|S_IRWXG|S_IRWXO;
207 		vap->va_mode &= (~rconsmask);
208 		vap->va_uid = 0;
209 	}
210 
211 	return (error);
212 }
213 
214 static int devfs_unlocked_access(void *, int, struct cred *);
215 
216 /*ARGSUSED4*/
217 static int
218 devfs_setattr_dir(
219 	struct dv_node *dv,
220 	struct vnode *vp,
221 	struct vattr *vap,
222 	int flags,
223 	struct cred *cr)
224 {
225 	struct vattr	*map;
226 	long int	mask;
227 	int		error = 0;
228 	struct vattr	vattr;
229 
230 	ASSERT(dv->dv_attr || dv->dv_attrvp);
231 
232 	ASSERT(vp->v_type == VDIR);
233 	ASSERT((dv->dv_flags & DV_NO_FSPERM) == 0);
234 
235 	if (vap->va_mask & AT_NOSET)
236 		return (EINVAL);
237 
238 	/* to ensure consistency, single thread setting of attributes */
239 	rw_enter(&dv->dv_contents, RW_WRITER);
240 
241 again:	if (dv->dv_attr) {
242 
243 		error = secpolicy_vnode_setattr(cr, vp, vap,
244 				dv->dv_attr, flags, devfs_unlocked_access, dv);
245 
246 		if (error)
247 			goto out;
248 
249 		/*
250 		 * Apply changes to the memory based attribute. This code
251 		 * is modeled after the tmpfs implementation of memory
252 		 * based vnodes
253 		 */
254 		map = dv->dv_attr;
255 		mask = vap->va_mask;
256 
257 		/* Change file access modes. */
258 		if (mask & AT_MODE) {
259 			map->va_mode &= S_IFMT;
260 			map->va_mode |= vap->va_mode & ~S_IFMT;
261 		}
262 		if (mask & AT_UID)
263 			map->va_uid = vap->va_uid;
264 		if (mask & AT_GID)
265 			map->va_gid = vap->va_gid;
266 		if (mask & AT_ATIME)
267 			map->va_atime = vap->va_atime;
268 		if (mask & AT_MTIME)
269 			map->va_mtime = vap->va_mtime;
270 
271 		if (mask & (AT_MODE | AT_UID | AT_GID | AT_MTIME))
272 			gethrestime(&map->va_ctime);
273 	} else {
274 		/* use the backing attribute store */
275 		ASSERT(dv->dv_attrvp);
276 
277 		/*
278 		 * See if we are changing something we care about
279 		 * the persistence of - return success if we don't care.
280 		 */
281 		if (vap->va_mask & (AT_MODE|AT_UID|AT_GID|AT_ATIME|AT_MTIME)) {
282 			/* Set the attributes */
283 			error = VOP_SETATTR(dv->dv_attrvp,
284 				vap, flags, cr, NULL);
285 			dsysdebug(error,
286 				("vop_setattr %s %d\n", dv->dv_name, error));
287 
288 			/*
289 			 * Some file systems may return EROFS for a setattr
290 			 * on a readonly file system.  In this case we create
291 			 * our own memory based attribute.
292 			 */
293 			if (error == EROFS) {
294 				/*
295 				 * obtain attributes from existing file
296 				 * that we will modify and switch to memory
297 				 * based attribute until attribute store is
298 				 * read/write.
299 				 */
300 				vattr = dv_vattr_dir;
301 				if (VOP_GETATTR(dv->dv_attrvp,
302 				    &vattr, flags, cr, NULL) == 0) {
303 					dv->dv_attr = kmem_alloc(
304 					    sizeof (struct vattr), KM_SLEEP);
305 					*dv->dv_attr = vattr;
306 					dv_vattr_merge(dv, dv->dv_attr);
307 					goto again;
308 				}
309 			}
310 		}
311 	}
312 out:
313 	rw_exit(&dv->dv_contents);
314 	return (error);
315 }
316 
317 
318 /*
319  * Compare the uid/gid/mode changes requested for a setattr
320  * operation with the same details of a node's default minor
321  * perm information.  Return 0 if identical.
322  */
323 static int
324 dv_setattr_cmp(struct vattr *map, mperm_t *mp)
325 {
326 	if ((map->va_mode & S_IAMB) != (mp->mp_mode & S_IAMB))
327 		return (1);
328 	if (map->va_uid != mp->mp_uid)
329 		return (1);
330 	if (map->va_gid != mp->mp_gid)
331 		return (1);
332 	return (0);
333 }
334 
335 
336 /*ARGSUSED4*/
337 static int
338 devfs_setattr(
339 	struct vnode *vp,
340 	struct vattr *vap,
341 	int flags,
342 	struct cred *cr,
343 	caller_context_t *ct)
344 {
345 	struct dv_node	*dv = VTODV(vp);
346 	struct dv_node	*ddv;
347 	struct vnode	*dvp;
348 	struct vattr	*map;
349 	long int	mask;
350 	int		error = 0;
351 	struct vattr	*free_vattr = NULL;
352 	struct vattr	*vattrp = NULL;
353 	mperm_t		mp;
354 	int		persist;
355 
356 	/*
357 	 * Message goes to console only. Otherwise, the message
358 	 * causes devfs_getattr to be invoked again... infinite loop
359 	 */
360 	dcmn_err2(("?devfs_setattr %s\n", dv->dv_name));
361 	ASSERT(dv->dv_attr || dv->dv_attrvp);
362 
363 	if (!(vp->v_type == VDIR || vp->v_type == VCHR || vp->v_type == VBLK)) {
364 		cmn_err(CE_WARN,	/* panic ? */
365 		    "?%s: getattr on vnode type %d", dvnm, vp->v_type);
366 		return (ENOENT);
367 	}
368 
369 	if (vap->va_mask & AT_NOSET)
370 		return (EINVAL);
371 
372 	/*
373 	 * If we are changing something we don't care about
374 	 * the persistence of, return success.
375 	 */
376 	if ((vap->va_mask &
377 	    (AT_MODE|AT_UID|AT_GID|AT_ATIME|AT_MTIME)) == 0)
378 		return (0);
379 
380 	/*
381 	 * If driver overrides fs perm, disallow chmod
382 	 * and do not create attribute nodes.
383 	 */
384 	if (dv->dv_flags & DV_NO_FSPERM) {
385 		ASSERT(dv->dv_attr);
386 		if (vap->va_mask & (AT_MODE | AT_UID | AT_GID))
387 			return (EPERM);
388 		if ((vap->va_mask & (AT_ATIME|AT_MTIME)) == 0)
389 			return (0);
390 		rw_enter(&dv->dv_contents, RW_WRITER);
391 		if (vap->va_mask & AT_ATIME)
392 			dv->dv_attr->va_atime = vap->va_atime;
393 		if (vap->va_mask & AT_MTIME)
394 			dv->dv_attr->va_mtime = vap->va_mtime;
395 		rw_exit(&dv->dv_contents);
396 		return (0);
397 	}
398 
399 	/*
400 	 * Directories are always created but device nodes are
401 	 * only used to persist non-default permissions.
402 	 */
403 	if (vp->v_type == VDIR) {
404 		ASSERT(dv->dv_attr || dv->dv_attrvp);
405 		return (devfs_setattr_dir(dv, vp, vap, flags, cr));
406 	}
407 
408 	/*
409 	 * Allocate now before we take any locks
410 	 */
411 	vattrp = kmem_zalloc(sizeof (*vattrp), KM_SLEEP);
412 
413 	/* to ensure consistency, single thread setting of attributes */
414 	rw_enter(&dv->dv_contents, RW_WRITER);
415 
416 	/*
417 	 * We don't need to create an attribute node
418 	 * to persist access or modification times.
419 	 */
420 	persist = (vap->va_mask & (AT_MODE | AT_UID | AT_GID));
421 
422 	/*
423 	 * If persisting something, get the default permissions
424 	 * for this minor to compare against what the attributes
425 	 * are now being set to.  Default ordering is:
426 	 *	- minor_perm match for this minor
427 	 *	- mode supplied by ddi_create_priv_minor_node
428 	 *	- devfs defaults
429 	 */
430 	if (persist) {
431 		if (dev_minorperm(dv->dv_devi, dv->dv_name, &mp) != 0) {
432 			mp.mp_uid = dv_vattr_file.va_uid;
433 			mp.mp_gid = dv_vattr_file.va_gid;
434 			mp.mp_mode = dv_vattr_file.va_mode;
435 			if (dv->dv_flags & DV_DFLT_MODE) {
436 				ASSERT((dv->dv_dflt_mode & ~S_IAMB) == 0);
437 				mp.mp_mode &= ~S_IAMB;
438 				mp.mp_mode |= dv->dv_dflt_mode;
439 				dcmn_err5(("%s: setattr priv default 0%o\n",
440 				    dv->dv_name, mp.mp_mode));
441 			} else {
442 				dcmn_err5(("%s: setattr devfs default 0%o\n",
443 				    dv->dv_name, mp.mp_mode));
444 			}
445 		} else {
446 			dcmn_err5(("%s: setattr minor perm default 0%o\n",
447 			    dv->dv_name, mp.mp_mode));
448 		}
449 	}
450 
451 	/*
452 	 * If we don't have a vattr for this node, construct one.
453 	 */
454 	if (dv->dv_attr) {
455 		free_vattr = vattrp;
456 		vattrp = NULL;
457 	} else {
458 		ASSERT(dv->dv_attrvp);
459 		ASSERT(vp->v_type != VDIR);
460 		*vattrp = dv_vattr_file;
461 		error = VOP_GETATTR(dv->dv_attrvp, vattrp, 0, cr, ct);
462 		dsysdebug(error, ("vop_getattr %s %d\n",
463 			dv->dv_name, error));
464 		if (error)
465 			goto out;
466 		dv->dv_attr = vattrp;
467 		dv_vattr_merge(dv, dv->dv_attr);
468 		vattrp = NULL;
469 	}
470 
471 	error = secpolicy_vnode_setattr(cr, vp, vap, dv->dv_attr,
472 					flags, devfs_unlocked_access, dv);
473 	if (error) {
474 		dsysdebug(error, ("devfs_setattr %s secpolicy error %d\n",
475 			dv->dv_name, error));
476 		goto out;
477 	}
478 
479 	/*
480 	 * Apply changes to the memory based attribute. This code
481 	 * is modeled after the tmpfs implementation of memory
482 	 * based vnodes
483 	 */
484 	map = dv->dv_attr;
485 	mask = vap->va_mask;
486 
487 	/* Change file access modes. */
488 	if (mask & AT_MODE) {
489 		map->va_mode &= S_IFMT;
490 		map->va_mode |= vap->va_mode & ~S_IFMT;
491 	}
492 	if (mask & AT_UID)
493 		map->va_uid = vap->va_uid;
494 	if (mask & AT_GID)
495 		map->va_gid = vap->va_gid;
496 	if (mask & AT_ATIME)
497 		map->va_atime = vap->va_atime;
498 	if (mask & AT_MTIME)
499 		map->va_mtime = vap->va_mtime;
500 
501 	if (mask & (AT_MODE | AT_UID | AT_GID | AT_MTIME)) {
502 		gethrestime(&map->va_ctime);
503 	}
504 
505 	/*
506 	 * A setattr to defaults means we no longer need the
507 	 * shadow node as a persistent store, unless there
508 	 * are ACLs.  Otherwise create a shadow node if one
509 	 * doesn't exist yet.
510 	 */
511 	if (persist) {
512 		if ((dv_setattr_cmp(map, &mp) == 0) &&
513 		    ((dv->dv_flags & DV_ACL) == 0)) {
514 
515 			if (dv->dv_attrvp) {
516 				ddv = dv->dv_dotdot;
517 				ASSERT(ddv->dv_attrvp);
518 				error = VOP_REMOVE(ddv->dv_attrvp,
519 				    dv->dv_name, cr, ct, 0);
520 				dsysdebug(error,
521 				    ("vop_remove %s %s %d\n",
522 				    ddv->dv_name, dv->dv_name, error));
523 
524 				if (error == EROFS)
525 					error = 0;
526 				VN_RELE(dv->dv_attrvp);
527 				dv->dv_attrvp = NULL;
528 			}
529 			ASSERT(dv->dv_attr);
530 		} else {
531 			if (mask & AT_MODE)
532 				dcmn_err5(("%s persisting mode 0%o\n",
533 					dv->dv_name, vap->va_mode));
534 			if (mask & AT_UID)
535 				dcmn_err5(("%s persisting uid %d\n",
536 					dv->dv_name, vap->va_uid));
537 			if (mask & AT_GID)
538 				dcmn_err5(("%s persisting gid %d\n",
539 					dv->dv_name, vap->va_gid));
540 
541 			if (dv->dv_attrvp == NULL) {
542 				dvp = DVTOV(dv->dv_dotdot);
543 				dv_shadow_node(dvp, dv->dv_name, vp,
544 				    NULL, NULLVP, cr,
545 				    DV_SHADOW_CREATE | DV_SHADOW_WRITE_HELD);
546 			}
547 			if (dv->dv_attrvp) {
548 				error = VOP_SETATTR(dv->dv_attrvp,
549 				    vap, flags, cr, NULL);
550 				dsysdebug(error, ("vop_setattr %s %d\n",
551 				    dv->dv_name, error));
552 			}
553 			/*
554 			 * Some file systems may return EROFS for a setattr
555 			 * on a readonly file system.  In this case save
556 			 * as our own memory based attribute.
557 			 * NOTE: ufs is NOT one of these (see ufs_iupdat).
558 			 */
559 			if (dv->dv_attr && dv->dv_attrvp && error == 0) {
560 				vattrp = dv->dv_attr;
561 				dv->dv_attr = NULL;
562 			} else if (error == EROFS)
563 				error = 0;
564 		}
565 	}
566 
567 out:
568 	rw_exit(&dv->dv_contents);
569 
570 	if (vattrp)
571 		kmem_free(vattrp, sizeof (*vattrp));
572 	if (free_vattr)
573 		kmem_free(free_vattr, sizeof (*free_vattr));
574 	return (error);
575 }
576 
577 static int
578 devfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
579     caller_context_t *ct)
580 {
581 	switch (cmd) {
582 	case _PC_ACL_ENABLED:
583 		/*
584 		 * We rely on the underlying filesystem for ACLs,
585 		 * so direct the query for ACL support there.
586 		 * ACL support isn't relative to the file
587 		 * and we can't guarantee that the dv node
588 		 * has an attribute node, so any valid
589 		 * attribute node will suffice.
590 		 */
591 		ASSERT(dvroot);
592 		ASSERT(dvroot->dv_attrvp);
593 		return (VOP_PATHCONF(dvroot->dv_attrvp, cmd, valp, cr, ct));
594 		/*NOTREACHED*/
595 	}
596 
597 	return (fs_pathconf(vp, cmd, valp, cr, ct));
598 }
599 
600 /*
601  * Let avp handle security attributes (acl's).
602  */
603 static int
604 devfs_getsecattr(struct vnode *vp, struct vsecattr *vsap, int flags,
605     struct cred *cr, caller_context_t *ct)
606 {
607 	dvnode_t *dv = VTODV(vp);
608 	struct vnode *avp;
609 	int	error;
610 
611 	dcmn_err2(("devfs_getsecattr %s\n", dv->dv_name));
612 	ASSERT(vp->v_type == VDIR || vp->v_type == VCHR || vp->v_type == VBLK);
613 
614 	rw_enter(&dv->dv_contents, RW_READER);
615 
616 	avp = dv->dv_attrvp;
617 
618 	/* fabricate the acl */
619 	if (avp == NULL) {
620 		error = fs_fab_acl(vp, vsap, flags, cr, ct);
621 		rw_exit(&dv->dv_contents);
622 		return (error);
623 	}
624 
625 	error = VOP_GETSECATTR(avp, vsap, flags, cr, ct);
626 	dsysdebug(error, ("vop_getsecattr %s %d\n", VTODV(vp)->dv_name, error));
627 	rw_exit(&dv->dv_contents);
628 	return (error);
629 }
630 
631 /*
632  * Set security attributes (acl's)
633  *
634  * Note that the dv_contents lock has already been acquired
635  * by the caller's VOP_RWLOCK.
636  */
637 static int
638 devfs_setsecattr(struct vnode *vp, struct vsecattr *vsap, int flags,
639     struct cred *cr, caller_context_t *ct)
640 {
641 	dvnode_t *dv = VTODV(vp);
642 	struct vnode *avp;
643 	int	error;
644 
645 	dcmn_err2(("devfs_setsecattr %s\n", dv->dv_name));
646 	ASSERT(vp->v_type == VDIR || vp->v_type == VCHR || vp->v_type == VBLK);
647 	ASSERT(RW_LOCK_HELD(&dv->dv_contents));
648 
649 	/*
650 	 * Not a supported operation on drivers not providing
651 	 * file system based permissions.
652 	 */
653 	if (dv->dv_flags & DV_NO_FSPERM)
654 		return (ENOTSUP);
655 
656 	/*
657 	 * To complete, the setsecattr requires an underlying attribute node.
658 	 */
659 	if (dv->dv_attrvp == NULL) {
660 		ASSERT(vp->v_type == VCHR || vp->v_type == VBLK);
661 		dv_shadow_node(DVTOV(dv->dv_dotdot), dv->dv_name, vp,
662 		    NULL, NULLVP, cr, DV_SHADOW_CREATE | DV_SHADOW_WRITE_HELD);
663 	}
664 
665 	if ((avp = dv->dv_attrvp) == NULL) {
666 		dcmn_err2(("devfs_setsecattr %s: "
667 		    "cannot construct attribute node\n", dv->dv_name));
668 		return (fs_nosys());
669 	}
670 
671 	/*
672 	 * The acl(2) system call issues a VOP_RWLOCK before setting an ACL.
673 	 * Since backing file systems expect the lock to be held before seeing
674 	 * a VOP_SETSECATTR ACL, we need to issue the VOP_RWLOCK to the backing
675 	 * store before forwarding the ACL.
676 	 */
677 	(void) VOP_RWLOCK(avp, V_WRITELOCK_TRUE, NULL);
678 	error = VOP_SETSECATTR(avp, vsap, flags, cr, ct);
679 	dsysdebug(error, ("vop_setsecattr %s %d\n", VTODV(vp)->dv_name, error));
680 	VOP_RWUNLOCK(avp, V_WRITELOCK_TRUE, NULL);
681 
682 	/*
683 	 * Set DV_ACL if we have a non-trivial set of ACLs.  It is not
684 	 * necessary to hold VOP_RWLOCK since fs_acl_nontrivial only does
685 	 * VOP_GETSECATTR calls.
686 	 */
687 	if (fs_acl_nontrivial(avp, cr))
688 		dv->dv_flags |= DV_ACL;
689 	return (error);
690 }
691 
692 /*
693  * This function is used for secpolicy_setattr().  It must call an
694  * access() like function while it is already holding the
695  * dv_contents lock.  We only care about this when dv_attr != NULL;
696  * so the unlocked access call only concerns itself with that
697  * particular branch of devfs_access().
698  */
699 static int
700 devfs_unlocked_access(void *vdv, int mode, struct cred *cr)
701 {
702 	struct dv_node *dv = vdv;
703 	int shift = 0;
704 	uid_t owner = dv->dv_attr->va_uid;
705 
706 	/* Check access based on owner, group and public permissions. */
707 	if (crgetuid(cr) != owner) {
708 		shift += 3;
709 		if (groupmember(dv->dv_attr->va_gid, cr) == 0)
710 			shift += 3;
711 	}
712 
713 	/* compute missing mode bits */
714 	mode &= ~(dv->dv_attr->va_mode << shift);
715 
716 	if (mode == 0)
717 		return (0);
718 
719 	return (secpolicy_vnode_access(cr, DVTOV(dv), owner, mode));
720 }
721 
722 static int
723 devfs_access(struct vnode *vp, int mode, int flags, struct cred *cr,
724     caller_context_t *ct)
725 {
726 	struct dv_node	*dv = VTODV(vp);
727 	int		res;
728 
729 	dcmn_err2(("devfs_access %s\n", dv->dv_name));
730 	ASSERT(dv->dv_attr || dv->dv_attrvp);
731 
732 	/* restrict console access to privileged processes */
733 	if ((vp->v_rdev == rconsdev) && secpolicy_console(cr) != 0) {
734 		return (EACCES);
735 	}
736 
737 	if (dv->dv_attr && ((dv->dv_flags & DV_ACL) == 0)) {
738 		rw_enter(&dv->dv_contents, RW_READER);
739 		if (dv->dv_attr) {
740 			res = devfs_unlocked_access(dv, mode, cr);
741 			rw_exit(&dv->dv_contents);
742 			return (res);
743 		}
744 		rw_exit(&dv->dv_contents);
745 	}
746 	return (VOP_ACCESS(dv->dv_attrvp, mode, flags, cr, ct));
747 }
748 
749 /*
750  * Lookup
751  *
752  * Given the directory vnode and the name of the component, return
753  * the corresponding held vnode for that component.
754  *
755  * Of course in these fictional filesystems, nothing's ever quite
756  * -that- simple.
757  *
758  * devfs name	type		shadow (fs attributes)	type	comments
759  * -------------------------------------------------------------------------
760  * drv[@addr]	VDIR		drv[@addr]		VDIR	nexus driver
761  * drv[@addr]:m	VCHR/VBLK	drv[@addr]:m		VREG	leaf driver
762  * drv[@addr]	VCHR/VBLK	drv[@addr]:.default	VREG	leaf driver
763  * -------------------------------------------------------------------------
764  *
765  * The following names are reserved for the attribute filesystem (which
766  * could easily be another layer on top of this one - we simply need to
767  * hold the vnode of the thing we're looking at)
768  *
769  * attr name	type		shadow (fs attributes)	type	comments
770  * -------------------------------------------------------------------------
771  * drv[@addr]	VDIR		-			-	attribute dir
772  * minorname	VDIR		-			-	minorname
773  * attribute	VREG		-			-	attribute
774  * -------------------------------------------------------------------------
775  *
776  * Examples:
777  *
778  *	devfs:/devices/.../mm@0:zero		VCHR
779  *	shadow:/.devices/.../mm@0:zero		VREG, fs attrs
780  *	devfs:/devices/.../mm@0:/zero/attr	VREG, driver attribute
781  *
782  *	devfs:/devices/.../sd@0,0:a		VBLK
783  *	shadow:/.devices/.../sd@0,0:a		VREG, fs attrs
784  *	devfs:/devices/.../sd@0,0:/a/.type	VREG, "ddi_block:chan"
785  *
786  *	devfs:/devices/.../mm@0			VCHR
787  *	shadow:/.devices/.../mm@0:.default	VREG, fs attrs
788  *	devfs:/devices/.../mm@0:/.default/attr	VREG, driver attribute
789  *	devfs:/devices/.../mm@0:/.default/.type	VREG, "ddi_pseudo"
790  *
791  *	devfs:/devices/.../obio			VDIR
792  *	shadow:/devices/.../obio		VDIR, needed for fs attrs.
793  *	devfs:/devices/.../obio:/.default/attr	VDIR, driver attribute
794  *
795  * We also need to be able deal with "old" devices that have gone away,
796  * though I think that provided we return them with readdir, they can
797  * be removed (i.e. they don't have to respond to lookup, though it might
798  * be weird if they didn't ;-)
799  *
800  * Lookup has side-effects.
801  *
802  * - It will create directories and fs attribute files in the shadow hierarchy.
803  * - It should cause non-SID devices to be probed (ask the parent nexi).
804  */
805 /*ARGSUSED3*/
806 static int
807 devfs_lookup(struct vnode *dvp, char *nm, struct vnode **vpp,
808     struct pathname *pnp, int flags, struct vnode *rdir, struct cred *cred,
809     caller_context_t *ct, int *direntflags, pathname_t *realpnp)
810 {
811 	ASSERT(dvp->v_type == VDIR);
812 	dcmn_err2(("devfs_lookup: %s\n", nm));
813 	return (dv_find(VTODV(dvp), nm, vpp, pnp, rdir, cred, 0));
814 }
815 
816 /*
817  * devfs nodes can't really be created directly by userland - however,
818  * we do allow creates to find existing nodes:
819  *
820  * - any create fails if the node doesn't exist - EROFS.
821  * - creating an existing directory read-only succeeds, otherwise EISDIR.
822  * - exclusive creates fail if the node already exists - EEXIST.
823  * - failure to create the snode for an existing device - ENOSYS.
824  */
825 /*ARGSUSED2*/
826 static int
827 devfs_create(struct vnode *dvp, char *nm, struct vattr *vap, vcexcl_t excl,
828     int mode, struct vnode **vpp, struct cred *cred, int flag,
829     caller_context_t *ct, vsecattr_t *vsecp)
830 {
831 	int error;
832 	struct vnode *vp;
833 
834 	dcmn_err2(("devfs_create %s\n", nm));
835 	error = dv_find(VTODV(dvp), nm, &vp, NULL, NULLVP, cred, 0);
836 	if (error == 0) {
837 		if (excl == EXCL)
838 			error = EEXIST;
839 		else if (vp->v_type == VDIR && (mode & VWRITE))
840 			error = EISDIR;
841 		else
842 			error = VOP_ACCESS(vp, mode, 0, cred, ct);
843 
844 		if (error) {
845 			VN_RELE(vp);
846 		} else
847 			*vpp = vp;
848 	} else if (error == ENOENT)
849 		error = EROFS;
850 
851 	return (error);
852 }
853 
854 /*
855  * If DV_BUILD is set, we call into nexus driver to do a BUS_CONFIG_ALL.
856  * Otherwise, simply return cached dv_node's. Hotplug code always call
857  * devfs_clean() to invalid the dv_node cache.
858  */
859 /*ARGSUSED5*/
860 static int
861 devfs_readdir(struct vnode *dvp, struct uio *uiop, struct cred *cred, int *eofp,
862     caller_context_t *ct, int flags)
863 {
864 	struct dv_node *ddv, *dv;
865 	struct dirent64 *de, *bufp;
866 	offset_t diroff;
867 	offset_t	soff;
868 	size_t reclen, movesz;
869 	int error;
870 	struct vattr va;
871 	size_t bufsz;
872 
873 	ddv = VTODV(dvp);
874 	dcmn_err2(("devfs_readdir %s: offset %lld len %ld\n",
875 	    ddv->dv_name, uiop->uio_loffset, uiop->uio_iov->iov_len));
876 	ASSERT(ddv->dv_attr || ddv->dv_attrvp);
877 	ASSERT(RW_READ_HELD(&ddv->dv_contents));
878 
879 	if (uiop->uio_loffset >= MAXOFF_T) {
880 		if (eofp)
881 			*eofp = 1;
882 		return (0);
883 	}
884 
885 	if (uiop->uio_iovcnt != 1)
886 		return (EINVAL);
887 
888 	if (dvp->v_type != VDIR)
889 		return (ENOTDIR);
890 
891 	/* Load the initial contents */
892 	if (ddv->dv_flags & DV_BUILD) {
893 		if (!rw_tryupgrade(&ddv->dv_contents)) {
894 			rw_exit(&ddv->dv_contents);
895 			rw_enter(&ddv->dv_contents, RW_WRITER);
896 		}
897 
898 		/* recheck and fill */
899 		if (ddv->dv_flags & DV_BUILD)
900 			dv_filldir(ddv);
901 
902 		rw_downgrade(&ddv->dv_contents);
903 	}
904 
905 	soff = uiop->uio_loffset;
906 	bufsz = uiop->uio_iov->iov_len;
907 	de = bufp = kmem_alloc(bufsz, KM_SLEEP);
908 	movesz = 0;
909 	dv = (struct dv_node *)-1;
910 
911 	/*
912 	 * Move as many entries into the uio structure as it will take.
913 	 * Special case "." and "..".
914 	 */
915 	diroff = 0;
916 	if (soff == 0) {				/* . */
917 		reclen = DIRENT64_RECLEN(strlen("."));
918 		if ((movesz + reclen) > bufsz)
919 			goto full;
920 		de->d_ino = (ino64_t)ddv->dv_ino;
921 		de->d_off = (off64_t)diroff + 1;
922 		de->d_reclen = (ushort_t)reclen;
923 
924 		/* use strncpy(9f) to zero out uninitialized bytes */
925 
926 		(void) strncpy(de->d_name, ".", DIRENT64_NAMELEN(reclen));
927 		movesz += reclen;
928 		de = (dirent64_t *)(intptr_t)((char *)de + reclen);
929 		dcmn_err3(("devfs_readdir: A: diroff %lld, soff %lld: '%s' "
930 		    "reclen %lu\n", diroff, soff, ".", reclen));
931 	}
932 
933 	diroff++;
934 	if (soff <= 1) {				/* .. */
935 		reclen = DIRENT64_RECLEN(strlen(".."));
936 		if ((movesz + reclen) > bufsz)
937 			goto full;
938 		de->d_ino = (ino64_t)ddv->dv_dotdot->dv_ino;
939 		de->d_off = (off64_t)diroff + 1;
940 		de->d_reclen = (ushort_t)reclen;
941 
942 		/* use strncpy(9f) to zero out uninitialized bytes */
943 
944 		(void) strncpy(de->d_name, "..", DIRENT64_NAMELEN(reclen));
945 		movesz += reclen;
946 		de = (dirent64_t *)(intptr_t)((char *)de + reclen);
947 		dcmn_err3(("devfs_readdir: B: diroff %lld, soff %lld: '%s' "
948 		    "reclen %lu\n", diroff, soff, "..", reclen));
949 	}
950 
951 	diroff++;
952 	for (dv = ddv->dv_dot; dv; dv = dv->dv_next, diroff++) {
953 		/*
954 		 * although DDM_INTERNAL_PATH minor nodes are skipped for
955 		 * readdirs outside the kernel, they still occupy directory
956 		 * offsets
957 		 */
958 		if (diroff < soff ||
959 		    ((dv->dv_flags & DV_INTERNAL) && (cred != kcred)))
960 			continue;
961 
962 		reclen = DIRENT64_RECLEN(strlen(dv->dv_name));
963 		if ((movesz + reclen) > bufsz) {
964 			dcmn_err3(("devfs_readdir: C: diroff "
965 			    "%lld, soff %lld: '%s' reclen %lu\n",
966 			    diroff, soff, dv->dv_name, reclen));
967 			goto full;
968 		}
969 		de->d_ino = (ino64_t)dv->dv_ino;
970 		de->d_off = (off64_t)diroff + 1;
971 		de->d_reclen = (ushort_t)reclen;
972 
973 		/* use strncpy(9f) to zero out uninitialized bytes */
974 
975 		ASSERT(strlen(dv->dv_name) + 1 <=
976 		    DIRENT64_NAMELEN(reclen));
977 		(void) strncpy(de->d_name, dv->dv_name,
978 		    DIRENT64_NAMELEN(reclen));
979 
980 		movesz += reclen;
981 		de = (dirent64_t *)(intptr_t)((char *)de + reclen);
982 		dcmn_err4(("devfs_readdir: D: diroff "
983 		    "%lld, soff %lld: '%s' reclen %lu\n", diroff, soff,
984 		    dv->dv_name, reclen));
985 	}
986 
987 	/* the buffer is full, or we exhausted everything */
988 full:	dcmn_err3(("devfs_readdir: moving %lu bytes: "
989 	    "diroff %lld, soff %lld, dv %p\n",
990 	    movesz, diroff, soff, (void *)dv));
991 
992 	if ((movesz == 0) && dv)
993 		error = EINVAL;		/* cannot be represented */
994 	else {
995 		error = uiomove(bufp, movesz, UIO_READ, uiop);
996 		if (error == 0) {
997 			if (eofp)
998 				*eofp = dv ? 0 : 1;
999 			uiop->uio_loffset = diroff;
1000 		}
1001 
1002 		va.va_mask = AT_ATIME;
1003 		gethrestime(&va.va_atime);
1004 		rw_exit(&ddv->dv_contents);
1005 		(void) devfs_setattr(dvp, &va, 0, cred, ct);
1006 		rw_enter(&ddv->dv_contents, RW_READER);
1007 	}
1008 
1009 	kmem_free(bufp, bufsz);
1010 	return (error);
1011 }
1012 
1013 /*ARGSUSED*/
1014 static int
1015 devfs_fsync(struct vnode *vp, int syncflag, struct cred *cred,
1016     caller_context_t *ct)
1017 {
1018 	/*
1019 	 * Message goes to console only. Otherwise, the message
1020 	 * causes devfs_fsync to be invoked again... infinite loop
1021 	 */
1022 	dcmn_err2(("devfs_fsync %s\n", VTODV(vp)->dv_name));
1023 	return (0);
1024 }
1025 
1026 /*
1027  * Normally, we leave the dv_node here at count of 0.
1028  * The node will be destroyed when dv_cleandir() is called.
1029  *
1030  * Stale dv_node's are already unlinked from the fs tree,
1031  * so dv_cleandir() won't find them. We destroy such nodes
1032  * immediately.
1033  */
1034 /*ARGSUSED1*/
1035 static void
1036 devfs_inactive(struct vnode *vp, struct cred *cred, caller_context_t *ct)
1037 {
1038 	int destroy;
1039 	struct dv_node *dv = VTODV(vp);
1040 
1041 	dcmn_err2(("devfs_inactive: %s\n", dv->dv_name));
1042 	mutex_enter(&vp->v_lock);
1043 	ASSERT(vp->v_count >= 1);
1044 	--vp->v_count;
1045 	destroy = (DV_STALE(dv) && vp->v_count == 0);
1046 	mutex_exit(&vp->v_lock);
1047 
1048 	/* stale nodes cannot be rediscovered, destroy it here */
1049 	if (destroy)
1050 		dv_destroy(dv, 0);
1051 }
1052 
1053 /*
1054  * XXX Why do we need this?  NFS mounted /dev directories?
1055  * XXX Talk to peter staubach about this.
1056  */
1057 /*ARGSUSED2*/
1058 static int
1059 devfs_fid(struct vnode *vp, struct fid *fidp, caller_context_t *ct)
1060 {
1061 	struct dv_node	*dv = VTODV(vp);
1062 	struct dv_fid	*dv_fid;
1063 
1064 	if (fidp->fid_len < (sizeof (struct dv_fid) - sizeof (ushort_t))) {
1065 		fidp->fid_len = sizeof (struct dv_fid) - sizeof (ushort_t);
1066 		return (ENOSPC);
1067 	}
1068 
1069 	dv_fid = (struct dv_fid *)fidp;
1070 	bzero(dv_fid, sizeof (struct dv_fid));
1071 	dv_fid->dvfid_len = (int)sizeof (struct dv_fid) - sizeof (ushort_t);
1072 	dv_fid->dvfid_ino = dv->dv_ino;
1073 	/* dv_fid->dvfid_gen = dv->tn_gen; XXX ? */
1074 
1075 	return (0);
1076 }
1077 
1078 /*
1079  * This pair of routines bracket all VOP_READ, VOP_WRITE
1080  * and VOP_READDIR requests.  The contents lock stops things
1081  * moving around while we're looking at them.
1082  *
1083  * Also used by file and record locking.
1084  */
1085 /*ARGSUSED2*/
1086 static int
1087 devfs_rwlock(struct vnode *vp, int write_flag, caller_context_t *ct)
1088 {
1089 	dcmn_err2(("devfs_rwlock %s\n", VTODV(vp)->dv_name));
1090 	rw_enter(&VTODV(vp)->dv_contents, write_flag ? RW_WRITER : RW_READER);
1091 	return (write_flag);
1092 }
1093 
1094 /*ARGSUSED1*/
1095 static void
1096 devfs_rwunlock(struct vnode *vp, int write_flag, caller_context_t *ct)
1097 {
1098 	dcmn_err2(("devfs_rwunlock %s\n", VTODV(vp)->dv_name));
1099 	rw_exit(&VTODV(vp)->dv_contents);
1100 }
1101 
1102 /*
1103  * XXX	Should probably do a better job of computing the maximum
1104  *	offset available in the directory.
1105  */
1106 /*ARGSUSED1*/
1107 static int
1108 devfs_seek(struct vnode *vp, offset_t ooff, offset_t *noffp,
1109     caller_context_t *ct)
1110 {
1111 	ASSERT(vp->v_type == VDIR);
1112 	dcmn_err2(("devfs_seek %s\n", VTODV(vp)->dv_name));
1113 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
1114 }
1115 
1116 vnodeops_t *dv_vnodeops;
1117 
1118 const fs_operation_def_t dv_vnodeops_template[] = {
1119 	VOPNAME_OPEN,		{ .vop_open = devfs_open },
1120 	VOPNAME_CLOSE,		{ .vop_close = devfs_close },
1121 	VOPNAME_READ,		{ .vop_read = devfs_read },
1122 	VOPNAME_WRITE,		{ .vop_write = devfs_write },
1123 	VOPNAME_IOCTL,		{ .vop_ioctl = devfs_ioctl },
1124 	VOPNAME_GETATTR,	{ .vop_getattr = devfs_getattr },
1125 	VOPNAME_SETATTR,	{ .vop_setattr = devfs_setattr },
1126 	VOPNAME_ACCESS,		{ .vop_access = devfs_access },
1127 	VOPNAME_LOOKUP,		{ .vop_lookup = devfs_lookup },
1128 	VOPNAME_CREATE,		{ .vop_create = devfs_create },
1129 	VOPNAME_READDIR,	{ .vop_readdir = devfs_readdir },
1130 	VOPNAME_FSYNC,		{ .vop_fsync = devfs_fsync },
1131 	VOPNAME_INACTIVE,	{ .vop_inactive = devfs_inactive },
1132 	VOPNAME_FID,		{ .vop_fid = devfs_fid },
1133 	VOPNAME_RWLOCK,		{ .vop_rwlock = devfs_rwlock },
1134 	VOPNAME_RWUNLOCK,	{ .vop_rwunlock = devfs_rwunlock },
1135 	VOPNAME_SEEK,		{ .vop_seek = devfs_seek },
1136 	VOPNAME_PATHCONF,	{ .vop_pathconf = devfs_pathconf },
1137 	VOPNAME_DISPOSE,	{ .error = fs_error },
1138 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = devfs_setsecattr },
1139 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = devfs_getsecattr },
1140 	NULL,			NULL
1141 };
1142