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