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