xref: /freebsd/sys/fs/devfs/devfs_vnops.c (revision b197d4b893974c9eb4d7b38704c6d5c486235d6f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2000-2004
5  *	Poul-Henning Kamp.  All rights reserved.
6  * Copyright (c) 1989, 1992-1993, 1995
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * This code is derived from software donated to Berkeley by
10  * Jan-Simon Pendry.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)kernfs_vnops.c	8.15 (Berkeley) 5/21/95
34  * From: FreeBSD: src/sys/miscfs/kernfs/kernfs_vnops.c 1.43
35  *
36  * $FreeBSD$
37  */
38 
39 /*
40  * TODO:
41  *	mkdir: want it ?
42  */
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/conf.h>
47 #include <sys/dirent.h>
48 #include <sys/eventhandler.h>
49 #include <sys/fcntl.h>
50 #include <sys/file.h>
51 #include <sys/filedesc.h>
52 #include <sys/filio.h>
53 #include <sys/jail.h>
54 #include <sys/kernel.h>
55 #include <sys/limits.h>
56 #include <sys/lock.h>
57 #include <sys/malloc.h>
58 #include <sys/mman.h>
59 #include <sys/mount.h>
60 #include <sys/namei.h>
61 #include <sys/priv.h>
62 #include <sys/proc.h>
63 #include <sys/stat.h>
64 #include <sys/sx.h>
65 #include <sys/sysctl.h>
66 #include <sys/time.h>
67 #include <sys/ttycom.h>
68 #include <sys/unistd.h>
69 #include <sys/vnode.h>
70 
71 static struct vop_vector devfs_vnodeops;
72 static struct vop_vector devfs_specops;
73 static struct fileops devfs_ops_f;
74 
75 #include <fs/devfs/devfs.h>
76 #include <fs/devfs/devfs_int.h>
77 
78 #include <security/mac/mac_framework.h>
79 
80 #include <vm/vm.h>
81 #include <vm/vm_extern.h>
82 #include <vm/vm_object.h>
83 
84 static MALLOC_DEFINE(M_CDEVPDATA, "DEVFSP", "Metainfo for cdev-fp data");
85 
86 struct mtx	devfs_de_interlock;
87 MTX_SYSINIT(devfs_de_interlock, &devfs_de_interlock, "devfs interlock", MTX_DEF);
88 struct mtx	cdevpriv_mtx;
89 MTX_SYSINIT(cdevpriv_mtx, &cdevpriv_mtx, "cdevpriv lock", MTX_DEF);
90 
91 SYSCTL_DECL(_vfs_devfs);
92 
93 static int devfs_dotimes;
94 SYSCTL_INT(_vfs_devfs, OID_AUTO, dotimes, CTLFLAG_RW,
95     &devfs_dotimes, 0, "Update timestamps on DEVFS with default precision");
96 
97 /*
98  * Update devfs node timestamp.  Note that updates are unlocked and
99  * stat(2) could see partially updated times.
100  */
101 static void
102 devfs_timestamp(struct timespec *tsp)
103 {
104 	time_t ts;
105 
106 	if (devfs_dotimes) {
107 		vfs_timestamp(tsp);
108 	} else {
109 		ts = time_second;
110 		if (tsp->tv_sec != ts) {
111 			tsp->tv_sec = ts;
112 			tsp->tv_nsec = 0;
113 		}
114 	}
115 }
116 
117 static int
118 devfs_fp_check(struct file *fp, struct cdev **devp, struct cdevsw **dswp,
119     int *ref)
120 {
121 	*dswp = devvn_refthread(fp->f_vnode, devp, ref);
122 	if (*dswp == NULL || *devp != fp->f_data) {
123 		if (*dswp != NULL)
124 			dev_relthread(*devp, *ref);
125 		return (ENXIO);
126 	}
127 	KASSERT((*devp)->si_refcount > 0,
128 	    ("devfs: un-referenced struct cdev *(%s)", devtoname(*devp)));
129 	if (*dswp == NULL)
130 		return (ENXIO);
131 	curthread->td_fpop = fp;
132 	return (0);
133 }
134 
135 int
136 devfs_get_cdevpriv(void **datap)
137 {
138 	struct file *fp;
139 	struct cdev_privdata *p;
140 	int error;
141 
142 	fp = curthread->td_fpop;
143 	if (fp == NULL)
144 		return (EBADF);
145 	p = fp->f_cdevpriv;
146 	if (p != NULL) {
147 		error = 0;
148 		*datap = p->cdpd_data;
149 	} else
150 		error = ENOENT;
151 	return (error);
152 }
153 
154 int
155 devfs_set_cdevpriv(void *priv, d_priv_dtor_t *priv_dtr)
156 {
157 	struct file *fp;
158 	struct cdev_priv *cdp;
159 	struct cdev_privdata *p;
160 	int error;
161 
162 	fp = curthread->td_fpop;
163 	if (fp == NULL)
164 		return (ENOENT);
165 	cdp = cdev2priv((struct cdev *)fp->f_data);
166 	p = malloc(sizeof(struct cdev_privdata), M_CDEVPDATA, M_WAITOK);
167 	p->cdpd_data = priv;
168 	p->cdpd_dtr = priv_dtr;
169 	p->cdpd_fp = fp;
170 	mtx_lock(&cdevpriv_mtx);
171 	if (fp->f_cdevpriv == NULL) {
172 		LIST_INSERT_HEAD(&cdp->cdp_fdpriv, p, cdpd_list);
173 		fp->f_cdevpriv = p;
174 		mtx_unlock(&cdevpriv_mtx);
175 		error = 0;
176 	} else {
177 		mtx_unlock(&cdevpriv_mtx);
178 		free(p, M_CDEVPDATA);
179 		error = EBUSY;
180 	}
181 	return (error);
182 }
183 
184 void
185 devfs_destroy_cdevpriv(struct cdev_privdata *p)
186 {
187 
188 	mtx_assert(&cdevpriv_mtx, MA_OWNED);
189 	KASSERT(p->cdpd_fp->f_cdevpriv == p,
190 	    ("devfs_destoy_cdevpriv %p != %p", p->cdpd_fp->f_cdevpriv, p));
191 	p->cdpd_fp->f_cdevpriv = NULL;
192 	LIST_REMOVE(p, cdpd_list);
193 	mtx_unlock(&cdevpriv_mtx);
194 	(p->cdpd_dtr)(p->cdpd_data);
195 	free(p, M_CDEVPDATA);
196 }
197 
198 static void
199 devfs_fpdrop(struct file *fp)
200 {
201 	struct cdev_privdata *p;
202 
203 	mtx_lock(&cdevpriv_mtx);
204 	if ((p = fp->f_cdevpriv) == NULL) {
205 		mtx_unlock(&cdevpriv_mtx);
206 		return;
207 	}
208 	devfs_destroy_cdevpriv(p);
209 }
210 
211 void
212 devfs_clear_cdevpriv(void)
213 {
214 	struct file *fp;
215 
216 	fp = curthread->td_fpop;
217 	if (fp == NULL)
218 		return;
219 	devfs_fpdrop(fp);
220 }
221 
222 static void
223 devfs_usecount_add(struct vnode *vp)
224 {
225 	struct devfs_dirent *de;
226 	struct cdev *dev;
227 
228 	mtx_lock(&devfs_de_interlock);
229 	VI_LOCK(vp);
230 	VNPASS(vp->v_type == VCHR || vp->v_type == VBAD, vp);
231 	if (VN_IS_DOOMED(vp)) {
232 		goto out_unlock;
233 	}
234 
235 	de = vp->v_data;
236 	dev = vp->v_rdev;
237 	MPASS(de != NULL);
238 	MPASS(dev != NULL);
239 	dev->si_usecount++;
240 	de->de_usecount++;
241 out_unlock:
242 	VI_UNLOCK(vp);
243 	mtx_unlock(&devfs_de_interlock);
244 }
245 
246 static void
247 devfs_usecount_subl(struct vnode *vp)
248 {
249 	struct devfs_dirent *de;
250 	struct cdev *dev;
251 
252 	mtx_assert(&devfs_de_interlock, MA_OWNED);
253 	ASSERT_VI_LOCKED(vp, __func__);
254 	VNPASS(vp->v_type == VCHR || vp->v_type == VBAD, vp);
255 
256 	de = vp->v_data;
257 	dev = vp->v_rdev;
258 	if (de == NULL)
259 		return;
260 	if (dev == NULL) {
261 		MPASS(de->de_usecount == 0);
262 		return;
263 	}
264 	if (dev->si_usecount < de->de_usecount)
265 		panic("%s: si_usecount underflow for dev %p "
266 		    "(has %ld, dirent has %d)\n",
267 		    __func__, dev, dev->si_usecount, de->de_usecount);
268 	if (VN_IS_DOOMED(vp)) {
269 		dev->si_usecount -= de->de_usecount;
270 		de->de_usecount = 0;
271 	} else {
272 		if (de->de_usecount == 0)
273 			panic("%s: de_usecount underflow for dev %p\n",
274 			    __func__, dev);
275 		dev->si_usecount--;
276 		de->de_usecount--;
277 	}
278 }
279 
280 static void
281 devfs_usecount_sub(struct vnode *vp)
282 {
283 
284 	mtx_lock(&devfs_de_interlock);
285 	VI_LOCK(vp);
286 	devfs_usecount_subl(vp);
287 	VI_UNLOCK(vp);
288 	mtx_unlock(&devfs_de_interlock);
289 }
290 
291 static int
292 devfs_usecountl(struct vnode *vp)
293 {
294 
295 	VNPASS(vp->v_type == VCHR, vp);
296 	mtx_assert(&devfs_de_interlock, MA_OWNED);
297 	ASSERT_VI_LOCKED(vp, __func__);
298 	return (vp->v_rdev->si_usecount);
299 }
300 
301 int
302 devfs_usecount(struct vnode *vp)
303 {
304 	int count;
305 
306 	VNPASS(vp->v_type == VCHR, vp);
307 	mtx_lock(&devfs_de_interlock);
308 	VI_LOCK(vp);
309 	count = devfs_usecountl(vp);
310 	VI_UNLOCK(vp);
311 	mtx_unlock(&devfs_de_interlock);
312 	return (count);
313 }
314 
315 void
316 devfs_ctty_ref(struct vnode *vp)
317 {
318 
319 	vrefact(vp);
320 	devfs_usecount_add(vp);
321 }
322 
323 void
324 devfs_ctty_unref(struct vnode *vp)
325 {
326 
327 	devfs_usecount_sub(vp);
328 	vrele(vp);
329 }
330 
331 /*
332  * On success devfs_populate_vp() returns with dmp->dm_lock held.
333  */
334 static int
335 devfs_populate_vp(struct vnode *vp)
336 {
337 	struct devfs_dirent *de;
338 	struct devfs_mount *dmp;
339 	int locked;
340 
341 	ASSERT_VOP_LOCKED(vp, "devfs_populate_vp");
342 
343 	dmp = VFSTODEVFS(vp->v_mount);
344 	if (!devfs_populate_needed(dmp)) {
345 		sx_xlock(&dmp->dm_lock);
346 		goto out_nopopulate;
347 	}
348 
349 	locked = VOP_ISLOCKED(vp);
350 
351 	sx_xlock(&dmp->dm_lock);
352 	DEVFS_DMP_HOLD(dmp);
353 
354 	/* Can't call devfs_populate() with the vnode lock held. */
355 	VOP_UNLOCK(vp);
356 	devfs_populate(dmp);
357 
358 	sx_xunlock(&dmp->dm_lock);
359 	vn_lock(vp, locked | LK_RETRY);
360 	sx_xlock(&dmp->dm_lock);
361 	if (DEVFS_DMP_DROP(dmp)) {
362 		sx_xunlock(&dmp->dm_lock);
363 		devfs_unmount_final(dmp);
364 		return (ERESTART);
365 	}
366 out_nopopulate:
367 	if (VN_IS_DOOMED(vp)) {
368 		sx_xunlock(&dmp->dm_lock);
369 		return (ERESTART);
370 	}
371 	de = vp->v_data;
372 	KASSERT(de != NULL,
373 	    ("devfs_populate_vp: vp->v_data == NULL but vnode not doomed"));
374 	if ((de->de_flags & DE_DOOMED) != 0) {
375 		sx_xunlock(&dmp->dm_lock);
376 		return (ERESTART);
377 	}
378 
379 	return (0);
380 }
381 
382 static int
383 devfs_vptocnp(struct vop_vptocnp_args *ap)
384 {
385 	struct vnode *vp = ap->a_vp;
386 	struct vnode **dvp = ap->a_vpp;
387 	struct devfs_mount *dmp;
388 	char *buf = ap->a_buf;
389 	size_t *buflen = ap->a_buflen;
390 	struct devfs_dirent *dd, *de;
391 	int i, error;
392 
393 	dmp = VFSTODEVFS(vp->v_mount);
394 
395 	error = devfs_populate_vp(vp);
396 	if (error != 0)
397 		return (error);
398 
399 	if (vp->v_type != VCHR && vp->v_type != VDIR) {
400 		error = ENOENT;
401 		goto finished;
402 	}
403 
404 	dd = vp->v_data;
405 	if (vp->v_type == VDIR && dd == dmp->dm_rootdir) {
406 		*dvp = vp;
407 		vref(*dvp);
408 		goto finished;
409 	}
410 
411 	i = *buflen;
412 	i -= dd->de_dirent->d_namlen;
413 	if (i < 0) {
414 		error = ENOMEM;
415 		goto finished;
416 	}
417 	bcopy(dd->de_dirent->d_name, buf + i, dd->de_dirent->d_namlen);
418 	*buflen = i;
419 	de = devfs_parent_dirent(dd);
420 	if (de == NULL) {
421 		error = ENOENT;
422 		goto finished;
423 	}
424 	mtx_lock(&devfs_de_interlock);
425 	*dvp = de->de_vnode;
426 	if (*dvp != NULL) {
427 		VI_LOCK(*dvp);
428 		mtx_unlock(&devfs_de_interlock);
429 		vholdl(*dvp);
430 		VI_UNLOCK(*dvp);
431 		vref(*dvp);
432 		vdrop(*dvp);
433 	} else {
434 		mtx_unlock(&devfs_de_interlock);
435 		error = ENOENT;
436 	}
437 finished:
438 	sx_xunlock(&dmp->dm_lock);
439 	return (error);
440 }
441 
442 /*
443  * Construct the fully qualified path name relative to the mountpoint.
444  * If a NULL cnp is provided, no '/' is appended to the resulting path.
445  */
446 char *
447 devfs_fqpn(char *buf, struct devfs_mount *dmp, struct devfs_dirent *dd,
448     struct componentname *cnp)
449 {
450 	int i;
451 	struct devfs_dirent *de;
452 
453 	sx_assert(&dmp->dm_lock, SA_LOCKED);
454 
455 	i = SPECNAMELEN;
456 	buf[i] = '\0';
457 	if (cnp != NULL)
458 		i -= cnp->cn_namelen;
459 	if (i < 0)
460 		 return (NULL);
461 	if (cnp != NULL)
462 		bcopy(cnp->cn_nameptr, buf + i, cnp->cn_namelen);
463 	de = dd;
464 	while (de != dmp->dm_rootdir) {
465 		if (cnp != NULL || i < SPECNAMELEN) {
466 			i--;
467 			if (i < 0)
468 				 return (NULL);
469 			buf[i] = '/';
470 		}
471 		i -= de->de_dirent->d_namlen;
472 		if (i < 0)
473 			 return (NULL);
474 		bcopy(de->de_dirent->d_name, buf + i,
475 		    de->de_dirent->d_namlen);
476 		de = devfs_parent_dirent(de);
477 		if (de == NULL)
478 			return (NULL);
479 	}
480 	return (buf + i);
481 }
482 
483 static int
484 devfs_allocv_drop_refs(int drop_dm_lock, struct devfs_mount *dmp,
485 	struct devfs_dirent *de)
486 {
487 	int not_found;
488 
489 	not_found = 0;
490 	if (de->de_flags & DE_DOOMED)
491 		not_found = 1;
492 	if (DEVFS_DE_DROP(de)) {
493 		KASSERT(not_found == 1, ("DEVFS de dropped but not doomed"));
494 		devfs_dirent_free(de);
495 	}
496 	if (DEVFS_DMP_DROP(dmp)) {
497 		KASSERT(not_found == 1,
498 			("DEVFS mount struct freed before dirent"));
499 		not_found = 2;
500 		sx_xunlock(&dmp->dm_lock);
501 		devfs_unmount_final(dmp);
502 	}
503 	if (not_found == 1 || (drop_dm_lock && not_found != 2))
504 		sx_unlock(&dmp->dm_lock);
505 	return (not_found);
506 }
507 
508 /*
509  * devfs_allocv shall be entered with dmp->dm_lock held, and it drops
510  * it on return.
511  */
512 int
513 devfs_allocv(struct devfs_dirent *de, struct mount *mp, int lockmode,
514     struct vnode **vpp)
515 {
516 	int error;
517 	struct vnode *vp;
518 	struct cdev *dev;
519 	struct devfs_mount *dmp;
520 	struct cdevsw *dsw;
521 	enum vgetstate vs;
522 
523 	dmp = VFSTODEVFS(mp);
524 	if (de->de_flags & DE_DOOMED) {
525 		sx_xunlock(&dmp->dm_lock);
526 		return (ENOENT);
527 	}
528 loop:
529 	DEVFS_DE_HOLD(de);
530 	DEVFS_DMP_HOLD(dmp);
531 	mtx_lock(&devfs_de_interlock);
532 	vp = de->de_vnode;
533 	if (vp != NULL) {
534 		vs = vget_prep(vp);
535 		mtx_unlock(&devfs_de_interlock);
536 		sx_xunlock(&dmp->dm_lock);
537 		vget_finish(vp, lockmode | LK_RETRY, vs);
538 		sx_xlock(&dmp->dm_lock);
539 		if (devfs_allocv_drop_refs(0, dmp, de)) {
540 			vput(vp);
541 			return (ENOENT);
542 		}
543 		else if (VN_IS_DOOMED(vp)) {
544 			mtx_lock(&devfs_de_interlock);
545 			if (de->de_vnode == vp) {
546 				de->de_vnode = NULL;
547 				vp->v_data = NULL;
548 			}
549 			mtx_unlock(&devfs_de_interlock);
550 			vput(vp);
551 			goto loop;
552 		}
553 		sx_xunlock(&dmp->dm_lock);
554 		*vpp = vp;
555 		return (0);
556 	}
557 	mtx_unlock(&devfs_de_interlock);
558 	if (de->de_dirent->d_type == DT_CHR) {
559 		if (!(de->de_cdp->cdp_flags & CDP_ACTIVE)) {
560 			devfs_allocv_drop_refs(1, dmp, de);
561 			return (ENOENT);
562 		}
563 		dev = &de->de_cdp->cdp_c;
564 	} else {
565 		dev = NULL;
566 	}
567 	error = getnewvnode("devfs", mp, &devfs_vnodeops, &vp);
568 	if (error != 0) {
569 		devfs_allocv_drop_refs(1, dmp, de);
570 		printf("devfs_allocv: failed to allocate new vnode\n");
571 		return (error);
572 	}
573 
574 	if (de->de_dirent->d_type == DT_CHR) {
575 		vp->v_type = VCHR;
576 		VI_LOCK(vp);
577 		dev_lock();
578 		dev_refl(dev);
579 		/* XXX: v_rdev should be protect by vnode lock */
580 		vp->v_rdev = dev;
581 		VNPASS(vp->v_usecount == 1, vp);
582 		/* Special casing of ttys for deadfs.  Probably redundant. */
583 		dsw = dev->si_devsw;
584 		if (dsw != NULL && (dsw->d_flags & D_TTY) != 0)
585 			vp->v_vflag |= VV_ISTTY;
586 		dev_unlock();
587 		VI_UNLOCK(vp);
588 		if ((dev->si_flags & SI_ETERNAL) != 0)
589 			vp->v_vflag |= VV_ETERNALDEV;
590 		vp->v_op = &devfs_specops;
591 	} else if (de->de_dirent->d_type == DT_DIR) {
592 		vp->v_type = VDIR;
593 	} else if (de->de_dirent->d_type == DT_LNK) {
594 		vp->v_type = VLNK;
595 	} else {
596 		vp->v_type = VBAD;
597 	}
598 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_NOWITNESS);
599 	VN_LOCK_ASHARE(vp);
600 	mtx_lock(&devfs_de_interlock);
601 	vp->v_data = de;
602 	de->de_vnode = vp;
603 	mtx_unlock(&devfs_de_interlock);
604 	error = insmntque1(vp, mp);
605 	if (error != 0) {
606 		mtx_lock(&devfs_de_interlock);
607 		vp->v_data = NULL;
608 		de->de_vnode = NULL;
609 		mtx_unlock(&devfs_de_interlock);
610 		vgone(vp);
611 		vput(vp);
612 		(void) devfs_allocv_drop_refs(1, dmp, de);
613 		return (error);
614 	}
615 	if (devfs_allocv_drop_refs(0, dmp, de)) {
616 		vput(vp);
617 		return (ENOENT);
618 	}
619 #ifdef MAC
620 	mac_devfs_vnode_associate(mp, de, vp);
621 #endif
622 	sx_xunlock(&dmp->dm_lock);
623 	*vpp = vp;
624 	return (0);
625 }
626 
627 static int
628 devfs_access(struct vop_access_args *ap)
629 {
630 	struct vnode *vp = ap->a_vp;
631 	struct devfs_dirent *de;
632 	struct proc *p;
633 	int error;
634 
635 	de = vp->v_data;
636 	if (vp->v_type == VDIR)
637 		de = de->de_dir;
638 
639 	error = vaccess(vp->v_type, de->de_mode, de->de_uid, de->de_gid,
640 	    ap->a_accmode, ap->a_cred);
641 	if (error == 0)
642 		return (0);
643 	if (error != EACCES)
644 		return (error);
645 	p = ap->a_td->td_proc;
646 	/* We do, however, allow access to the controlling terminal */
647 	PROC_LOCK(p);
648 	if (!(p->p_flag & P_CONTROLT)) {
649 		PROC_UNLOCK(p);
650 		return (error);
651 	}
652 	if (p->p_session->s_ttydp == de->de_cdp)
653 		error = 0;
654 	PROC_UNLOCK(p);
655 	return (error);
656 }
657 
658 _Static_assert(((FMASK | FCNTLFLAGS) & (FLASTCLOSE | FREVOKE)) == 0,
659     "devfs-only flag reuse failed");
660 
661 static int
662 devfs_close(struct vop_close_args *ap)
663 {
664 	struct vnode *vp = ap->a_vp, *oldvp;
665 	struct thread *td = ap->a_td;
666 	struct proc *p;
667 	struct cdev *dev = vp->v_rdev;
668 	struct cdevsw *dsw;
669 	struct devfs_dirent *de = vp->v_data;
670 	int dflags, error, ref, vp_locked;
671 
672 	/*
673 	 * XXX: Don't call d_close() if we were called because of
674 	 * XXX: insmntque() failure.
675 	 */
676 	if (vp->v_data == NULL)
677 		return (0);
678 
679 	/*
680 	 * Hack: a tty device that is a controlling terminal
681 	 * has a reference from the session structure.
682 	 * We cannot easily tell that a character device is
683 	 * a controlling terminal, unless it is the closing
684 	 * process' controlling terminal.  In that case,
685 	 * if the reference count is 2 (this last descriptor
686 	 * plus the session), release the reference from the session.
687 	 */
688 	if (de->de_usecount == 2 && td != NULL) {
689 		p = td->td_proc;
690 		PROC_LOCK(p);
691 		if (vp == p->p_session->s_ttyvp) {
692 			PROC_UNLOCK(p);
693 			oldvp = NULL;
694 			sx_xlock(&proctree_lock);
695 			if (vp == p->p_session->s_ttyvp) {
696 				SESS_LOCK(p->p_session);
697 				mtx_lock(&devfs_de_interlock);
698 				VI_LOCK(vp);
699 				if (devfs_usecountl(vp) == 2 && !VN_IS_DOOMED(vp)) {
700 					p->p_session->s_ttyvp = NULL;
701 					p->p_session->s_ttydp = NULL;
702 					oldvp = vp;
703 				}
704 				VI_UNLOCK(vp);
705 				mtx_unlock(&devfs_de_interlock);
706 				SESS_UNLOCK(p->p_session);
707 			}
708 			sx_xunlock(&proctree_lock);
709 			if (oldvp != NULL)
710 				devfs_ctty_unref(oldvp);
711 		} else
712 			PROC_UNLOCK(p);
713 	}
714 	/*
715 	 * We do not want to really close the device if it
716 	 * is still in use unless we are trying to close it
717 	 * forcibly. Since every use (buffer, vnode, swap, cmap)
718 	 * holds a reference to the vnode, and because we mark
719 	 * any other vnodes that alias this device, when the
720 	 * sum of the reference counts on all the aliased
721 	 * vnodes descends to one, we are on last close.
722 	 */
723 	dsw = dev_refthread(dev, &ref);
724 	if (dsw == NULL)
725 		return (ENXIO);
726 	dflags = 0;
727 	mtx_lock(&devfs_de_interlock);
728 	VI_LOCK(vp);
729 	if (devfs_usecountl(vp) == 1)
730 		dflags |= FLASTCLOSE;
731 	devfs_usecount_subl(vp);
732 	mtx_unlock(&devfs_de_interlock);
733 	if (VN_IS_DOOMED(vp)) {
734 		/* Forced close. */
735 		dflags |= FREVOKE | FNONBLOCK;
736 	} else if (dsw->d_flags & D_TRACKCLOSE) {
737 		/* Keep device updated on status. */
738 	} else if ((dflags & FLASTCLOSE) == 0) {
739 		VI_UNLOCK(vp);
740 		dev_relthread(dev, ref);
741 		return (0);
742 	}
743 	vholdnz(vp);
744 	VI_UNLOCK(vp);
745 	vp_locked = VOP_ISLOCKED(vp);
746 	VOP_UNLOCK(vp);
747 	KASSERT(dev->si_refcount > 0,
748 	    ("devfs_close() on un-referenced struct cdev *(%s)", devtoname(dev)));
749 	error = dsw->d_close(dev, ap->a_fflag | dflags, S_IFCHR, td);
750 	dev_relthread(dev, ref);
751 	vn_lock(vp, vp_locked | LK_RETRY);
752 	vdrop(vp);
753 	return (error);
754 }
755 
756 static int
757 devfs_close_f(struct file *fp, struct thread *td)
758 {
759 	int error;
760 	struct file *fpop;
761 
762 	/*
763 	 * NB: td may be NULL if this descriptor is closed due to
764 	 * garbage collection from a closed UNIX domain socket.
765 	 */
766 	fpop = curthread->td_fpop;
767 	curthread->td_fpop = fp;
768 	error = vnops.fo_close(fp, td);
769 	curthread->td_fpop = fpop;
770 
771 	/*
772 	 * The f_cdevpriv cannot be assigned non-NULL value while we
773 	 * are destroying the file.
774 	 */
775 	if (fp->f_cdevpriv != NULL)
776 		devfs_fpdrop(fp);
777 	return (error);
778 }
779 
780 static int
781 devfs_getattr(struct vop_getattr_args *ap)
782 {
783 	struct vnode *vp = ap->a_vp;
784 	struct vattr *vap = ap->a_vap;
785 	struct devfs_dirent *de;
786 	struct devfs_mount *dmp;
787 	struct cdev *dev;
788 	struct timeval boottime;
789 	int error;
790 
791 	error = devfs_populate_vp(vp);
792 	if (error != 0)
793 		return (error);
794 
795 	dmp = VFSTODEVFS(vp->v_mount);
796 	sx_xunlock(&dmp->dm_lock);
797 
798 	de = vp->v_data;
799 	KASSERT(de != NULL, ("Null dirent in devfs_getattr vp=%p", vp));
800 	if (vp->v_type == VDIR) {
801 		de = de->de_dir;
802 		KASSERT(de != NULL,
803 		    ("Null dir dirent in devfs_getattr vp=%p", vp));
804 	}
805 	vap->va_uid = de->de_uid;
806 	vap->va_gid = de->de_gid;
807 	vap->va_mode = de->de_mode;
808 	if (vp->v_type == VLNK)
809 		vap->va_size = strlen(de->de_symlink);
810 	else if (vp->v_type == VDIR)
811 		vap->va_size = vap->va_bytes = DEV_BSIZE;
812 	else
813 		vap->va_size = 0;
814 	if (vp->v_type != VDIR)
815 		vap->va_bytes = 0;
816 	vap->va_blocksize = DEV_BSIZE;
817 	vap->va_type = vp->v_type;
818 
819 	getboottime(&boottime);
820 #define fix(aa)							\
821 	do {							\
822 		if ((aa).tv_sec <= 3600) {			\
823 			(aa).tv_sec = boottime.tv_sec;		\
824 			(aa).tv_nsec = boottime.tv_usec * 1000; \
825 		}						\
826 	} while (0)
827 
828 	if (vp->v_type != VCHR)  {
829 		fix(de->de_atime);
830 		vap->va_atime = de->de_atime;
831 		fix(de->de_mtime);
832 		vap->va_mtime = de->de_mtime;
833 		fix(de->de_ctime);
834 		vap->va_ctime = de->de_ctime;
835 	} else {
836 		dev = vp->v_rdev;
837 		fix(dev->si_atime);
838 		vap->va_atime = dev->si_atime;
839 		fix(dev->si_mtime);
840 		vap->va_mtime = dev->si_mtime;
841 		fix(dev->si_ctime);
842 		vap->va_ctime = dev->si_ctime;
843 
844 		vap->va_rdev = cdev2priv(dev)->cdp_inode;
845 	}
846 	vap->va_gen = 0;
847 	vap->va_flags = 0;
848 	vap->va_filerev = 0;
849 	vap->va_nlink = de->de_links;
850 	vap->va_fileid = de->de_inode;
851 
852 	return (error);
853 }
854 
855 /* ARGSUSED */
856 static int
857 devfs_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred, struct thread *td)
858 {
859 	struct file *fpop;
860 	int error;
861 
862 	fpop = td->td_fpop;
863 	td->td_fpop = fp;
864 	error = vnops.fo_ioctl(fp, com, data, cred, td);
865 	td->td_fpop = fpop;
866 	return (error);
867 }
868 
869 void *
870 fiodgname_buf_get_ptr(void *fgnp, u_long com)
871 {
872 	union {
873 		struct fiodgname_arg	fgn;
874 #ifdef COMPAT_FREEBSD32
875 		struct fiodgname_arg32	fgn32;
876 #endif
877 	} *fgnup;
878 
879 	fgnup = fgnp;
880 	switch (com) {
881 	case FIODGNAME:
882 		return (fgnup->fgn.buf);
883 #ifdef COMPAT_FREEBSD32
884 	case FIODGNAME_32:
885 		return ((void *)(uintptr_t)fgnup->fgn32.buf);
886 #endif
887 	default:
888 		panic("Unhandled ioctl command %ld", com);
889 	}
890 }
891 
892 static int
893 devfs_ioctl(struct vop_ioctl_args *ap)
894 {
895 	struct fiodgname_arg *fgn;
896 	struct vnode *vpold, *vp;
897 	struct cdevsw *dsw;
898 	struct thread *td;
899 	struct session *sess;
900 	struct cdev *dev;
901 	int error, ref, i;
902 	const char *p;
903 	u_long com;
904 
905 	vp = ap->a_vp;
906 	com = ap->a_command;
907 	td = ap->a_td;
908 
909 	dsw = devvn_refthread(vp, &dev, &ref);
910 	if (dsw == NULL)
911 		return (ENXIO);
912 	KASSERT(dev->si_refcount > 0,
913 	    ("devfs: un-referenced struct cdev *(%s)", devtoname(dev)));
914 
915 	switch (com) {
916 	case FIODTYPE:
917 		*(int *)ap->a_data = dsw->d_flags & D_TYPEMASK;
918 		error = 0;
919 		break;
920 	case FIODGNAME:
921 #ifdef	COMPAT_FREEBSD32
922 	case FIODGNAME_32:
923 #endif
924 		fgn = ap->a_data;
925 		p = devtoname(dev);
926 		i = strlen(p) + 1;
927 		if (i > fgn->len)
928 			error = EINVAL;
929 		else
930 			error = copyout(p, fiodgname_buf_get_ptr(fgn, com), i);
931 		break;
932 	default:
933 		error = dsw->d_ioctl(dev, com, ap->a_data, ap->a_fflag, td);
934 	}
935 
936 	dev_relthread(dev, ref);
937 	if (error == ENOIOCTL)
938 		error = ENOTTY;
939 
940 	if (error == 0 && com == TIOCSCTTY) {
941 		/*
942 		 * Do nothing if reassigning same control tty, or if the
943 		 * control tty has already disappeared.  If it disappeared,
944 		 * it's because we were racing with TIOCNOTTY.  TIOCNOTTY
945 		 * already took care of releasing the old vnode and we have
946 		 * nothing left to do.
947 		 */
948 		sx_slock(&proctree_lock);
949 		sess = td->td_proc->p_session;
950 		if (sess->s_ttyvp == vp || sess->s_ttyp == NULL) {
951 			sx_sunlock(&proctree_lock);
952 			return (0);
953 		}
954 
955 		devfs_ctty_ref(vp);
956 		SESS_LOCK(sess);
957 		vpold = sess->s_ttyvp;
958 		sess->s_ttyvp = vp;
959 		sess->s_ttydp = cdev2priv(dev);
960 		SESS_UNLOCK(sess);
961 
962 		sx_sunlock(&proctree_lock);
963 
964 		/* Get rid of reference to old control tty */
965 		if (vpold)
966 			devfs_ctty_unref(vpold);
967 	}
968 	return (error);
969 }
970 
971 /* ARGSUSED */
972 static int
973 devfs_kqfilter_f(struct file *fp, struct knote *kn)
974 {
975 	struct cdev *dev;
976 	struct cdevsw *dsw;
977 	int error, ref;
978 	struct file *fpop;
979 	struct thread *td;
980 
981 	td = curthread;
982 	fpop = td->td_fpop;
983 	error = devfs_fp_check(fp, &dev, &dsw, &ref);
984 	if (error)
985 		return (error);
986 	error = dsw->d_kqfilter(dev, kn);
987 	td->td_fpop = fpop;
988 	dev_relthread(dev, ref);
989 	return (error);
990 }
991 
992 static inline int
993 devfs_prison_check(struct devfs_dirent *de, struct thread *td)
994 {
995 	struct cdev_priv *cdp;
996 	struct ucred *dcr;
997 	struct proc *p;
998 	int error;
999 
1000 	cdp = de->de_cdp;
1001 	if (cdp == NULL)
1002 		return (0);
1003 	dcr = cdp->cdp_c.si_cred;
1004 	if (dcr == NULL)
1005 		return (0);
1006 
1007 	error = prison_check(td->td_ucred, dcr);
1008 	if (error == 0)
1009 		return (0);
1010 	/* We do, however, allow access to the controlling terminal */
1011 	p = td->td_proc;
1012 	PROC_LOCK(p);
1013 	if (!(p->p_flag & P_CONTROLT)) {
1014 		PROC_UNLOCK(p);
1015 		return (error);
1016 	}
1017 	if (p->p_session->s_ttydp == cdp)
1018 		error = 0;
1019 	PROC_UNLOCK(p);
1020 	return (error);
1021 }
1022 
1023 static int
1024 devfs_lookupx(struct vop_lookup_args *ap, int *dm_unlock)
1025 {
1026 	struct componentname *cnp;
1027 	struct vnode *dvp, **vpp;
1028 	struct thread *td;
1029 	struct devfs_dirent *de, *dd;
1030 	struct devfs_dirent **dde;
1031 	struct devfs_mount *dmp;
1032 	struct mount *mp;
1033 	struct cdev *cdev;
1034 	int error, flags, nameiop, dvplocked;
1035 	char specname[SPECNAMELEN + 1], *pname;
1036 
1037 	td = curthread;
1038 	cnp = ap->a_cnp;
1039 	vpp = ap->a_vpp;
1040 	dvp = ap->a_dvp;
1041 	pname = cnp->cn_nameptr;
1042 	flags = cnp->cn_flags;
1043 	nameiop = cnp->cn_nameiop;
1044 	mp = dvp->v_mount;
1045 	dmp = VFSTODEVFS(mp);
1046 	dd = dvp->v_data;
1047 	*vpp = NULLVP;
1048 
1049 	if ((flags & ISLASTCN) && nameiop == RENAME)
1050 		return (EOPNOTSUPP);
1051 
1052 	if (dvp->v_type != VDIR)
1053 		return (ENOTDIR);
1054 
1055 	if ((flags & ISDOTDOT) && (dvp->v_vflag & VV_ROOT))
1056 		return (EIO);
1057 
1058 	error = vn_dir_check_exec(dvp, cnp);
1059 	if (error != 0)
1060 		return (error);
1061 
1062 	if (cnp->cn_namelen == 1 && *pname == '.') {
1063 		if ((flags & ISLASTCN) && nameiop != LOOKUP)
1064 			return (EINVAL);
1065 		*vpp = dvp;
1066 		VREF(dvp);
1067 		return (0);
1068 	}
1069 
1070 	if (flags & ISDOTDOT) {
1071 		if ((flags & ISLASTCN) && nameiop != LOOKUP)
1072 			return (EINVAL);
1073 		de = devfs_parent_dirent(dd);
1074 		if (de == NULL)
1075 			return (ENOENT);
1076 		dvplocked = VOP_ISLOCKED(dvp);
1077 		VOP_UNLOCK(dvp);
1078 		error = devfs_allocv(de, mp, cnp->cn_lkflags & LK_TYPE_MASK,
1079 		    vpp);
1080 		*dm_unlock = 0;
1081 		vn_lock(dvp, dvplocked | LK_RETRY);
1082 		return (error);
1083 	}
1084 
1085 	dd = dvp->v_data;
1086 	de = devfs_find(dd, cnp->cn_nameptr, cnp->cn_namelen, 0);
1087 	while (de == NULL) {	/* While(...) so we can use break */
1088 
1089 		if (nameiop == DELETE)
1090 			return (ENOENT);
1091 
1092 		/*
1093 		 * OK, we didn't have an entry for the name we were asked for
1094 		 * so we try to see if anybody can create it on demand.
1095 		 */
1096 		pname = devfs_fqpn(specname, dmp, dd, cnp);
1097 		if (pname == NULL)
1098 			break;
1099 
1100 		cdev = NULL;
1101 		DEVFS_DMP_HOLD(dmp);
1102 		sx_xunlock(&dmp->dm_lock);
1103 		EVENTHANDLER_INVOKE(dev_clone,
1104 		    td->td_ucred, pname, strlen(pname), &cdev);
1105 
1106 		if (cdev == NULL)
1107 			sx_xlock(&dmp->dm_lock);
1108 		else if (devfs_populate_vp(dvp) != 0) {
1109 			*dm_unlock = 0;
1110 			sx_xlock(&dmp->dm_lock);
1111 			if (DEVFS_DMP_DROP(dmp)) {
1112 				sx_xunlock(&dmp->dm_lock);
1113 				devfs_unmount_final(dmp);
1114 			} else
1115 				sx_xunlock(&dmp->dm_lock);
1116 			dev_rel(cdev);
1117 			return (ENOENT);
1118 		}
1119 		if (DEVFS_DMP_DROP(dmp)) {
1120 			*dm_unlock = 0;
1121 			sx_xunlock(&dmp->dm_lock);
1122 			devfs_unmount_final(dmp);
1123 			if (cdev != NULL)
1124 				dev_rel(cdev);
1125 			return (ENOENT);
1126 		}
1127 
1128 		if (cdev == NULL)
1129 			break;
1130 
1131 		dev_lock();
1132 		dde = &cdev2priv(cdev)->cdp_dirents[dmp->dm_idx];
1133 		if (dde != NULL && *dde != NULL)
1134 			de = *dde;
1135 		dev_unlock();
1136 		dev_rel(cdev);
1137 		break;
1138 	}
1139 
1140 	if (de == NULL || de->de_flags & DE_WHITEOUT) {
1141 		if ((nameiop == CREATE || nameiop == RENAME) &&
1142 		    (flags & (LOCKPARENT | WANTPARENT)) && (flags & ISLASTCN)) {
1143 			cnp->cn_flags |= SAVENAME;
1144 			return (EJUSTRETURN);
1145 		}
1146 		return (ENOENT);
1147 	}
1148 
1149 	if (devfs_prison_check(de, td))
1150 		return (ENOENT);
1151 
1152 	if ((cnp->cn_nameiop == DELETE) && (flags & ISLASTCN)) {
1153 		error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
1154 		if (error)
1155 			return (error);
1156 		if (*vpp == dvp) {
1157 			VREF(dvp);
1158 			*vpp = dvp;
1159 			return (0);
1160 		}
1161 	}
1162 	error = devfs_allocv(de, mp, cnp->cn_lkflags & LK_TYPE_MASK, vpp);
1163 	*dm_unlock = 0;
1164 	return (error);
1165 }
1166 
1167 static int
1168 devfs_lookup(struct vop_lookup_args *ap)
1169 {
1170 	int j;
1171 	struct devfs_mount *dmp;
1172 	int dm_unlock;
1173 
1174 	if (devfs_populate_vp(ap->a_dvp) != 0)
1175 		return (ENOTDIR);
1176 
1177 	dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1178 	dm_unlock = 1;
1179 	j = devfs_lookupx(ap, &dm_unlock);
1180 	if (dm_unlock == 1)
1181 		sx_xunlock(&dmp->dm_lock);
1182 	return (j);
1183 }
1184 
1185 static int
1186 devfs_mknod(struct vop_mknod_args *ap)
1187 {
1188 	struct componentname *cnp;
1189 	struct vnode *dvp, **vpp;
1190 	struct devfs_dirent *dd, *de;
1191 	struct devfs_mount *dmp;
1192 	int error;
1193 
1194 	/*
1195 	 * The only type of node we should be creating here is a
1196 	 * character device, for anything else return EOPNOTSUPP.
1197 	 */
1198 	if (ap->a_vap->va_type != VCHR)
1199 		return (EOPNOTSUPP);
1200 	dvp = ap->a_dvp;
1201 	dmp = VFSTODEVFS(dvp->v_mount);
1202 
1203 	cnp = ap->a_cnp;
1204 	vpp = ap->a_vpp;
1205 	dd = dvp->v_data;
1206 
1207 	error = ENOENT;
1208 	sx_xlock(&dmp->dm_lock);
1209 	TAILQ_FOREACH(de, &dd->de_dlist, de_list) {
1210 		if (cnp->cn_namelen != de->de_dirent->d_namlen)
1211 			continue;
1212 		if (de->de_dirent->d_type == DT_CHR &&
1213 		    (de->de_cdp->cdp_flags & CDP_ACTIVE) == 0)
1214 			continue;
1215 		if (bcmp(cnp->cn_nameptr, de->de_dirent->d_name,
1216 		    de->de_dirent->d_namlen) != 0)
1217 			continue;
1218 		if (de->de_flags & DE_WHITEOUT)
1219 			break;
1220 		goto notfound;
1221 	}
1222 	if (de == NULL)
1223 		goto notfound;
1224 	de->de_flags &= ~DE_WHITEOUT;
1225 	error = devfs_allocv(de, dvp->v_mount, LK_EXCLUSIVE, vpp);
1226 	return (error);
1227 notfound:
1228 	sx_xunlock(&dmp->dm_lock);
1229 	return (error);
1230 }
1231 
1232 /* ARGSUSED */
1233 static int
1234 devfs_open(struct vop_open_args *ap)
1235 {
1236 	struct thread *td = ap->a_td;
1237 	struct vnode *vp = ap->a_vp;
1238 	struct cdev *dev = vp->v_rdev;
1239 	struct file *fp = ap->a_fp;
1240 	int error, ref, vlocked;
1241 	struct cdevsw *dsw;
1242 	struct file *fpop;
1243 
1244 	if (vp->v_type == VBLK)
1245 		return (ENXIO);
1246 
1247 	if (dev == NULL)
1248 		return (ENXIO);
1249 
1250 	/* Make this field valid before any I/O in d_open. */
1251 	if (dev->si_iosize_max == 0)
1252 		dev->si_iosize_max = DFLTPHYS;
1253 
1254 	dsw = dev_refthread(dev, &ref);
1255 	if (dsw == NULL)
1256 		return (ENXIO);
1257 	if (fp == NULL && dsw->d_fdopen != NULL) {
1258 		dev_relthread(dev, ref);
1259 		return (ENXIO);
1260 	}
1261 
1262 	if (vp->v_type == VCHR)
1263 		devfs_usecount_add(vp);
1264 
1265 	vlocked = VOP_ISLOCKED(vp);
1266 	VOP_UNLOCK(vp);
1267 
1268 	fpop = td->td_fpop;
1269 	td->td_fpop = fp;
1270 	if (fp != NULL) {
1271 		fp->f_data = dev;
1272 		fp->f_vnode = vp;
1273 	}
1274 	if (dsw->d_fdopen != NULL)
1275 		error = dsw->d_fdopen(dev, ap->a_mode, td, fp);
1276 	else
1277 		error = dsw->d_open(dev, ap->a_mode, S_IFCHR, td);
1278 	/* Clean up any cdevpriv upon error. */
1279 	if (error != 0)
1280 		devfs_clear_cdevpriv();
1281 	td->td_fpop = fpop;
1282 
1283 	vn_lock(vp, vlocked | LK_RETRY);
1284 	if (error != 0 && vp->v_type == VCHR)
1285 		devfs_usecount_sub(vp);
1286 
1287 	dev_relthread(dev, ref);
1288 	if (error != 0) {
1289 		if (error == ERESTART)
1290 			error = EINTR;
1291 		return (error);
1292 	}
1293 
1294 #if 0	/* /dev/console */
1295 	KASSERT(fp != NULL, ("Could not vnode bypass device on NULL fp"));
1296 #else
1297 	if (fp == NULL)
1298 		return (error);
1299 #endif
1300 	if (fp->f_ops == &badfileops)
1301 		finit(fp, fp->f_flag, DTYPE_VNODE, dev, &devfs_ops_f);
1302 	return (error);
1303 }
1304 
1305 static int
1306 devfs_pathconf(struct vop_pathconf_args *ap)
1307 {
1308 
1309 	switch (ap->a_name) {
1310 	case _PC_FILESIZEBITS:
1311 		*ap->a_retval = 64;
1312 		return (0);
1313 	case _PC_NAME_MAX:
1314 		*ap->a_retval = NAME_MAX;
1315 		return (0);
1316 	case _PC_LINK_MAX:
1317 		*ap->a_retval = INT_MAX;
1318 		return (0);
1319 	case _PC_SYMLINK_MAX:
1320 		*ap->a_retval = MAXPATHLEN;
1321 		return (0);
1322 	case _PC_MAX_CANON:
1323 		if (ap->a_vp->v_vflag & VV_ISTTY) {
1324 			*ap->a_retval = MAX_CANON;
1325 			return (0);
1326 		}
1327 		return (EINVAL);
1328 	case _PC_MAX_INPUT:
1329 		if (ap->a_vp->v_vflag & VV_ISTTY) {
1330 			*ap->a_retval = MAX_INPUT;
1331 			return (0);
1332 		}
1333 		return (EINVAL);
1334 	case _PC_VDISABLE:
1335 		if (ap->a_vp->v_vflag & VV_ISTTY) {
1336 			*ap->a_retval = _POSIX_VDISABLE;
1337 			return (0);
1338 		}
1339 		return (EINVAL);
1340 	case _PC_MAC_PRESENT:
1341 #ifdef MAC
1342 		/*
1343 		 * If MAC is enabled, devfs automatically supports
1344 		 * trivial non-persistent label storage.
1345 		 */
1346 		*ap->a_retval = 1;
1347 #else
1348 		*ap->a_retval = 0;
1349 #endif
1350 		return (0);
1351 	case _PC_CHOWN_RESTRICTED:
1352 		*ap->a_retval = 1;
1353 		return (0);
1354 	default:
1355 		return (vop_stdpathconf(ap));
1356 	}
1357 	/* NOTREACHED */
1358 }
1359 
1360 /* ARGSUSED */
1361 static int
1362 devfs_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td)
1363 {
1364 	struct cdev *dev;
1365 	struct cdevsw *dsw;
1366 	int error, ref;
1367 	struct file *fpop;
1368 
1369 	fpop = td->td_fpop;
1370 	error = devfs_fp_check(fp, &dev, &dsw, &ref);
1371 	if (error != 0) {
1372 		error = vnops.fo_poll(fp, events, cred, td);
1373 		return (error);
1374 	}
1375 	error = dsw->d_poll(dev, events, td);
1376 	td->td_fpop = fpop;
1377 	dev_relthread(dev, ref);
1378 	return(error);
1379 }
1380 
1381 /*
1382  * Print out the contents of a special device vnode.
1383  */
1384 static int
1385 devfs_print(struct vop_print_args *ap)
1386 {
1387 
1388 	printf("\tdev %s\n", devtoname(ap->a_vp->v_rdev));
1389 	return (0);
1390 }
1391 
1392 static int
1393 devfs_read_f(struct file *fp, struct uio *uio, struct ucred *cred,
1394     int flags, struct thread *td)
1395 {
1396 	struct cdev *dev;
1397 	int ioflag, error, ref;
1398 	ssize_t resid;
1399 	struct cdevsw *dsw;
1400 	struct file *fpop;
1401 
1402 	if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1403 		return (EINVAL);
1404 	fpop = td->td_fpop;
1405 	error = devfs_fp_check(fp, &dev, &dsw, &ref);
1406 	if (error != 0) {
1407 		error = vnops.fo_read(fp, uio, cred, flags, td);
1408 		return (error);
1409 	}
1410 	resid = uio->uio_resid;
1411 	ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT);
1412 	if (ioflag & O_DIRECT)
1413 		ioflag |= IO_DIRECT;
1414 
1415 	foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1416 	error = dsw->d_read(dev, uio, ioflag);
1417 	if (uio->uio_resid != resid || (error == 0 && resid != 0))
1418 		devfs_timestamp(&dev->si_atime);
1419 	td->td_fpop = fpop;
1420 	dev_relthread(dev, ref);
1421 
1422 	foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF_R);
1423 	return (error);
1424 }
1425 
1426 static int
1427 devfs_readdir(struct vop_readdir_args *ap)
1428 {
1429 	int error;
1430 	struct uio *uio;
1431 	struct dirent *dp;
1432 	struct devfs_dirent *dd;
1433 	struct devfs_dirent *de;
1434 	struct devfs_mount *dmp;
1435 	off_t off;
1436 	int *tmp_ncookies = NULL;
1437 
1438 	if (ap->a_vp->v_type != VDIR)
1439 		return (ENOTDIR);
1440 
1441 	uio = ap->a_uio;
1442 	if (uio->uio_offset < 0)
1443 		return (EINVAL);
1444 
1445 	/*
1446 	 * XXX: This is a temporary hack to get around this filesystem not
1447 	 * supporting cookies. We store the location of the ncookies pointer
1448 	 * in a temporary variable before calling vfs_subr.c:vfs_read_dirent()
1449 	 * and set the number of cookies to 0. We then set the pointer to
1450 	 * NULL so that vfs_read_dirent doesn't try to call realloc() on
1451 	 * ap->a_cookies. Later in this function, we restore the ap->a_ncookies
1452 	 * pointer to its original location before returning to the caller.
1453 	 */
1454 	if (ap->a_ncookies != NULL) {
1455 		tmp_ncookies = ap->a_ncookies;
1456 		*ap->a_ncookies = 0;
1457 		ap->a_ncookies = NULL;
1458 	}
1459 
1460 	dmp = VFSTODEVFS(ap->a_vp->v_mount);
1461 	if (devfs_populate_vp(ap->a_vp) != 0) {
1462 		if (tmp_ncookies != NULL)
1463 			ap->a_ncookies = tmp_ncookies;
1464 		return (EIO);
1465 	}
1466 	error = 0;
1467 	de = ap->a_vp->v_data;
1468 	off = 0;
1469 	TAILQ_FOREACH(dd, &de->de_dlist, de_list) {
1470 		KASSERT(dd->de_cdp != (void *)0xdeadc0de, ("%s %d\n", __func__, __LINE__));
1471 		if (dd->de_flags & (DE_COVERED | DE_WHITEOUT))
1472 			continue;
1473 		if (devfs_prison_check(dd, uio->uio_td))
1474 			continue;
1475 		if (dd->de_dirent->d_type == DT_DIR)
1476 			de = dd->de_dir;
1477 		else
1478 			de = dd;
1479 		dp = dd->de_dirent;
1480 		MPASS(dp->d_reclen == GENERIC_DIRSIZ(dp));
1481 		if (dp->d_reclen > uio->uio_resid)
1482 			break;
1483 		dp->d_fileno = de->de_inode;
1484 		/* NOTE: d_off is the offset for the *next* entry. */
1485 		dp->d_off = off + dp->d_reclen;
1486 		if (off >= uio->uio_offset) {
1487 			error = vfs_read_dirent(ap, dp, off);
1488 			if (error)
1489 				break;
1490 		}
1491 		off += dp->d_reclen;
1492 	}
1493 	sx_xunlock(&dmp->dm_lock);
1494 	uio->uio_offset = off;
1495 
1496 	/*
1497 	 * Restore ap->a_ncookies if it wasn't originally NULL in the first
1498 	 * place.
1499 	 */
1500 	if (tmp_ncookies != NULL)
1501 		ap->a_ncookies = tmp_ncookies;
1502 
1503 	return (error);
1504 }
1505 
1506 static int
1507 devfs_readlink(struct vop_readlink_args *ap)
1508 {
1509 	struct devfs_dirent *de;
1510 
1511 	de = ap->a_vp->v_data;
1512 	return (uiomove(de->de_symlink, strlen(de->de_symlink), ap->a_uio));
1513 }
1514 
1515 static void
1516 devfs_reclaiml(struct vnode *vp)
1517 {
1518 	struct devfs_dirent *de;
1519 
1520 	mtx_assert(&devfs_de_interlock, MA_OWNED);
1521 	de = vp->v_data;
1522 	if (de != NULL) {
1523 		MPASS(de->de_usecount == 0);
1524 		de->de_vnode = NULL;
1525 		vp->v_data = NULL;
1526 	}
1527 }
1528 
1529 static int
1530 devfs_reclaim(struct vop_reclaim_args *ap)
1531 {
1532 	struct vnode *vp;
1533 
1534 	vp = ap->a_vp;
1535 	mtx_lock(&devfs_de_interlock);
1536 	devfs_reclaiml(vp);
1537 	mtx_unlock(&devfs_de_interlock);
1538 	return (0);
1539 }
1540 
1541 static int
1542 devfs_reclaim_vchr(struct vop_reclaim_args *ap)
1543 {
1544 	struct vnode *vp;
1545 	struct cdev *dev;
1546 
1547 	vp = ap->a_vp;
1548 	MPASS(vp->v_type == VCHR);
1549 
1550 	mtx_lock(&devfs_de_interlock);
1551 	VI_LOCK(vp);
1552 	devfs_usecount_subl(vp);
1553 	devfs_reclaiml(vp);
1554 	mtx_unlock(&devfs_de_interlock);
1555 	dev_lock();
1556 	dev = vp->v_rdev;
1557 	vp->v_rdev = NULL;
1558 	dev_unlock();
1559 	VI_UNLOCK(vp);
1560 	if (dev != NULL)
1561 		dev_rel(dev);
1562 	return (0);
1563 }
1564 
1565 static int
1566 devfs_remove(struct vop_remove_args *ap)
1567 {
1568 	struct vnode *dvp = ap->a_dvp;
1569 	struct vnode *vp = ap->a_vp;
1570 	struct devfs_dirent *dd;
1571 	struct devfs_dirent *de, *de_covered;
1572 	struct devfs_mount *dmp = VFSTODEVFS(vp->v_mount);
1573 
1574 	ASSERT_VOP_ELOCKED(dvp, "devfs_remove");
1575 	ASSERT_VOP_ELOCKED(vp, "devfs_remove");
1576 
1577 	sx_xlock(&dmp->dm_lock);
1578 	dd = ap->a_dvp->v_data;
1579 	de = vp->v_data;
1580 	if (de->de_cdp == NULL) {
1581 		TAILQ_REMOVE(&dd->de_dlist, de, de_list);
1582 		if (de->de_dirent->d_type == DT_LNK) {
1583 			de_covered = devfs_find(dd, de->de_dirent->d_name,
1584 			    de->de_dirent->d_namlen, 0);
1585 			if (de_covered != NULL)
1586 				de_covered->de_flags &= ~DE_COVERED;
1587 		}
1588 		/* We need to unlock dvp because devfs_delete() may lock it. */
1589 		VOP_UNLOCK(vp);
1590 		if (dvp != vp)
1591 			VOP_UNLOCK(dvp);
1592 		devfs_delete(dmp, de, 0);
1593 		sx_xunlock(&dmp->dm_lock);
1594 		if (dvp != vp)
1595 			vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1596 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1597 	} else {
1598 		de->de_flags |= DE_WHITEOUT;
1599 		sx_xunlock(&dmp->dm_lock);
1600 	}
1601 	return (0);
1602 }
1603 
1604 /*
1605  * Revoke is called on a tty when a terminal session ends.  The vnode
1606  * is orphaned by setting v_op to deadfs so we need to let go of it
1607  * as well so that we create a new one next time around.
1608  *
1609  */
1610 static int
1611 devfs_revoke(struct vop_revoke_args *ap)
1612 {
1613 	struct vnode *vp = ap->a_vp, *vp2;
1614 	struct cdev *dev;
1615 	struct cdev_priv *cdp;
1616 	struct devfs_dirent *de;
1617 	enum vgetstate vs;
1618 	u_int i;
1619 
1620 	KASSERT((ap->a_flags & REVOKEALL) != 0, ("devfs_revoke !REVOKEALL"));
1621 
1622 	dev = vp->v_rdev;
1623 	cdp = cdev2priv(dev);
1624 
1625 	dev_lock();
1626 	cdp->cdp_inuse++;
1627 	dev_unlock();
1628 
1629 	vhold(vp);
1630 	vgone(vp);
1631 	vdrop(vp);
1632 
1633 	VOP_UNLOCK(vp);
1634  loop:
1635 	for (;;) {
1636 		mtx_lock(&devfs_de_interlock);
1637 		dev_lock();
1638 		vp2 = NULL;
1639 		for (i = 0; i <= cdp->cdp_maxdirent; i++) {
1640 			de = cdp->cdp_dirents[i];
1641 			if (de == NULL)
1642 				continue;
1643 
1644 			vp2 = de->de_vnode;
1645 			if (vp2 != NULL) {
1646 				dev_unlock();
1647 				vs = vget_prep(vp2);
1648 				mtx_unlock(&devfs_de_interlock);
1649 				if (vget_finish(vp2, LK_EXCLUSIVE, vs) != 0)
1650 					goto loop;
1651 				vhold(vp2);
1652 				vgone(vp2);
1653 				vdrop(vp2);
1654 				vput(vp2);
1655 				break;
1656 			}
1657 		}
1658 		if (vp2 != NULL) {
1659 			continue;
1660 		}
1661 		dev_unlock();
1662 		mtx_unlock(&devfs_de_interlock);
1663 		break;
1664 	}
1665 	dev_lock();
1666 	cdp->cdp_inuse--;
1667 	if (!(cdp->cdp_flags & CDP_ACTIVE) && cdp->cdp_inuse == 0) {
1668 		TAILQ_REMOVE(&cdevp_list, cdp, cdp_list);
1669 		dev_unlock();
1670 		dev_rel(&cdp->cdp_c);
1671 	} else
1672 		dev_unlock();
1673 
1674 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1675 	return (0);
1676 }
1677 
1678 static int
1679 devfs_rioctl(struct vop_ioctl_args *ap)
1680 {
1681 	struct vnode *vp;
1682 	struct devfs_mount *dmp;
1683 	int error;
1684 
1685 	vp = ap->a_vp;
1686 	vn_lock(vp, LK_SHARED | LK_RETRY);
1687 	if (VN_IS_DOOMED(vp)) {
1688 		VOP_UNLOCK(vp);
1689 		return (EBADF);
1690 	}
1691 	dmp = VFSTODEVFS(vp->v_mount);
1692 	sx_xlock(&dmp->dm_lock);
1693 	VOP_UNLOCK(vp);
1694 	DEVFS_DMP_HOLD(dmp);
1695 	devfs_populate(dmp);
1696 	if (DEVFS_DMP_DROP(dmp)) {
1697 		sx_xunlock(&dmp->dm_lock);
1698 		devfs_unmount_final(dmp);
1699 		return (ENOENT);
1700 	}
1701 	error = devfs_rules_ioctl(dmp, ap->a_command, ap->a_data, ap->a_td);
1702 	sx_xunlock(&dmp->dm_lock);
1703 	return (error);
1704 }
1705 
1706 static int
1707 devfs_rread(struct vop_read_args *ap)
1708 {
1709 
1710 	if (ap->a_vp->v_type != VDIR)
1711 		return (EINVAL);
1712 	return (VOP_READDIR(ap->a_vp, ap->a_uio, ap->a_cred, NULL, NULL, NULL));
1713 }
1714 
1715 static int
1716 devfs_setattr(struct vop_setattr_args *ap)
1717 {
1718 	struct devfs_dirent *de;
1719 	struct vattr *vap;
1720 	struct vnode *vp;
1721 	struct thread *td;
1722 	int c, error;
1723 	uid_t uid;
1724 	gid_t gid;
1725 
1726 	vap = ap->a_vap;
1727 	vp = ap->a_vp;
1728 	td = curthread;
1729 	if ((vap->va_type != VNON) ||
1730 	    (vap->va_nlink != VNOVAL) ||
1731 	    (vap->va_fsid != VNOVAL) ||
1732 	    (vap->va_fileid != VNOVAL) ||
1733 	    (vap->va_blocksize != VNOVAL) ||
1734 	    (vap->va_flags != VNOVAL && vap->va_flags != 0) ||
1735 	    (vap->va_rdev != VNOVAL) ||
1736 	    ((int)vap->va_bytes != VNOVAL) ||
1737 	    (vap->va_gen != VNOVAL)) {
1738 		return (EINVAL);
1739 	}
1740 
1741 	error = devfs_populate_vp(vp);
1742 	if (error != 0)
1743 		return (error);
1744 
1745 	de = vp->v_data;
1746 	if (vp->v_type == VDIR)
1747 		de = de->de_dir;
1748 
1749 	c = 0;
1750 	if (vap->va_uid == (uid_t)VNOVAL)
1751 		uid = de->de_uid;
1752 	else
1753 		uid = vap->va_uid;
1754 	if (vap->va_gid == (gid_t)VNOVAL)
1755 		gid = de->de_gid;
1756 	else
1757 		gid = vap->va_gid;
1758 	if (uid != de->de_uid || gid != de->de_gid) {
1759 		if ((ap->a_cred->cr_uid != de->de_uid) || uid != de->de_uid ||
1760 		    (gid != de->de_gid && !groupmember(gid, ap->a_cred))) {
1761 			error = priv_check(td, PRIV_VFS_CHOWN);
1762 			if (error != 0)
1763 				goto ret;
1764 		}
1765 		de->de_uid = uid;
1766 		de->de_gid = gid;
1767 		c = 1;
1768 	}
1769 
1770 	if (vap->va_mode != (mode_t)VNOVAL) {
1771 		if (ap->a_cred->cr_uid != de->de_uid) {
1772 			error = priv_check(td, PRIV_VFS_ADMIN);
1773 			if (error != 0)
1774 				goto ret;
1775 		}
1776 		de->de_mode = vap->va_mode;
1777 		c = 1;
1778 	}
1779 
1780 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1781 		error = vn_utimes_perm(vp, vap, ap->a_cred, td);
1782 		if (error != 0)
1783 			goto ret;
1784 		if (vap->va_atime.tv_sec != VNOVAL) {
1785 			if (vp->v_type == VCHR)
1786 				vp->v_rdev->si_atime = vap->va_atime;
1787 			else
1788 				de->de_atime = vap->va_atime;
1789 		}
1790 		if (vap->va_mtime.tv_sec != VNOVAL) {
1791 			if (vp->v_type == VCHR)
1792 				vp->v_rdev->si_mtime = vap->va_mtime;
1793 			else
1794 				de->de_mtime = vap->va_mtime;
1795 		}
1796 		c = 1;
1797 	}
1798 
1799 	if (c) {
1800 		if (vp->v_type == VCHR)
1801 			vfs_timestamp(&vp->v_rdev->si_ctime);
1802 		else
1803 			vfs_timestamp(&de->de_mtime);
1804 	}
1805 
1806 ret:
1807 	sx_xunlock(&VFSTODEVFS(vp->v_mount)->dm_lock);
1808 	return (error);
1809 }
1810 
1811 #ifdef MAC
1812 static int
1813 devfs_setlabel(struct vop_setlabel_args *ap)
1814 {
1815 	struct vnode *vp;
1816 	struct devfs_dirent *de;
1817 
1818 	vp = ap->a_vp;
1819 	de = vp->v_data;
1820 
1821 	mac_vnode_relabel(ap->a_cred, vp, ap->a_label);
1822 	mac_devfs_update(vp->v_mount, de, vp);
1823 
1824 	return (0);
1825 }
1826 #endif
1827 
1828 static int
1829 devfs_stat_f(struct file *fp, struct stat *sb, struct ucred *cred)
1830 {
1831 
1832 	return (vnops.fo_stat(fp, sb, cred));
1833 }
1834 
1835 static int
1836 devfs_symlink(struct vop_symlink_args *ap)
1837 {
1838 	int i, error;
1839 	struct devfs_dirent *dd;
1840 	struct devfs_dirent *de, *de_covered, *de_dotdot;
1841 	struct devfs_mount *dmp;
1842 
1843 	error = priv_check(curthread, PRIV_DEVFS_SYMLINK);
1844 	if (error)
1845 		return(error);
1846 	dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1847 	if (devfs_populate_vp(ap->a_dvp) != 0)
1848 		return (ENOENT);
1849 
1850 	dd = ap->a_dvp->v_data;
1851 	de = devfs_newdirent(ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen);
1852 	de->de_flags = DE_USER;
1853 	de->de_uid = 0;
1854 	de->de_gid = 0;
1855 	de->de_mode = 0755;
1856 	de->de_inode = alloc_unr(devfs_inos);
1857 	de->de_dir = dd;
1858 	de->de_dirent->d_type = DT_LNK;
1859 	i = strlen(ap->a_target) + 1;
1860 	de->de_symlink = malloc(i, M_DEVFS, M_WAITOK);
1861 	bcopy(ap->a_target, de->de_symlink, i);
1862 #ifdef MAC
1863 	mac_devfs_create_symlink(ap->a_cnp->cn_cred, dmp->dm_mount, dd, de);
1864 #endif
1865 	de_covered = devfs_find(dd, de->de_dirent->d_name,
1866 	    de->de_dirent->d_namlen, 0);
1867 	if (de_covered != NULL) {
1868 		if ((de_covered->de_flags & DE_USER) != 0) {
1869 			devfs_delete(dmp, de, DEVFS_DEL_NORECURSE);
1870 			sx_xunlock(&dmp->dm_lock);
1871 			return (EEXIST);
1872 		}
1873 		KASSERT((de_covered->de_flags & DE_COVERED) == 0,
1874 		    ("devfs_symlink: entry %p already covered", de_covered));
1875 		de_covered->de_flags |= DE_COVERED;
1876 	}
1877 
1878 	de_dotdot = TAILQ_FIRST(&dd->de_dlist);		/* "." */
1879 	de_dotdot = TAILQ_NEXT(de_dotdot, de_list);	/* ".." */
1880 	TAILQ_INSERT_AFTER(&dd->de_dlist, de_dotdot, de, de_list);
1881 	devfs_dir_ref_de(dmp, dd);
1882 	devfs_rules_apply(dmp, de);
1883 
1884 	return (devfs_allocv(de, ap->a_dvp->v_mount, LK_EXCLUSIVE, ap->a_vpp));
1885 }
1886 
1887 static int
1888 devfs_truncate_f(struct file *fp, off_t length, struct ucred *cred, struct thread *td)
1889 {
1890 
1891 	return (vnops.fo_truncate(fp, length, cred, td));
1892 }
1893 
1894 static int
1895 devfs_write_f(struct file *fp, struct uio *uio, struct ucred *cred,
1896     int flags, struct thread *td)
1897 {
1898 	struct cdev *dev;
1899 	int error, ioflag, ref;
1900 	ssize_t resid;
1901 	struct cdevsw *dsw;
1902 	struct file *fpop;
1903 
1904 	if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1905 		return (EINVAL);
1906 	fpop = td->td_fpop;
1907 	error = devfs_fp_check(fp, &dev, &dsw, &ref);
1908 	if (error != 0) {
1909 		error = vnops.fo_write(fp, uio, cred, flags, td);
1910 		return (error);
1911 	}
1912 	KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", uio->uio_td, td));
1913 	ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT | O_FSYNC);
1914 	if (ioflag & O_DIRECT)
1915 		ioflag |= IO_DIRECT;
1916 	foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1917 
1918 	resid = uio->uio_resid;
1919 
1920 	error = dsw->d_write(dev, uio, ioflag);
1921 	if (uio->uio_resid != resid || (error == 0 && resid != 0)) {
1922 		devfs_timestamp(&dev->si_ctime);
1923 		dev->si_mtime = dev->si_ctime;
1924 	}
1925 	td->td_fpop = fpop;
1926 	dev_relthread(dev, ref);
1927 
1928 	foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF_W);
1929 	return (error);
1930 }
1931 
1932 static int
1933 devfs_mmap_f(struct file *fp, vm_map_t map, vm_offset_t *addr, vm_size_t size,
1934     vm_prot_t prot, vm_prot_t cap_maxprot, int flags, vm_ooffset_t foff,
1935     struct thread *td)
1936 {
1937 	struct cdev *dev;
1938 	struct cdevsw *dsw;
1939 	struct mount *mp;
1940 	struct vnode *vp;
1941 	struct file *fpop;
1942 	vm_object_t object;
1943 	vm_prot_t maxprot;
1944 	int error, ref;
1945 
1946 	vp = fp->f_vnode;
1947 
1948 	/*
1949 	 * Ensure that file and memory protections are
1950 	 * compatible.
1951 	 */
1952 	mp = vp->v_mount;
1953 	if (mp != NULL && (mp->mnt_flag & MNT_NOEXEC) != 0) {
1954 		maxprot = VM_PROT_NONE;
1955 		if ((prot & VM_PROT_EXECUTE) != 0)
1956 			return (EACCES);
1957 	} else
1958 		maxprot = VM_PROT_EXECUTE;
1959 	if ((fp->f_flag & FREAD) != 0)
1960 		maxprot |= VM_PROT_READ;
1961 	else if ((prot & VM_PROT_READ) != 0)
1962 		return (EACCES);
1963 
1964 	/*
1965 	 * If we are sharing potential changes via MAP_SHARED and we
1966 	 * are trying to get write permission although we opened it
1967 	 * without asking for it, bail out.
1968 	 *
1969 	 * Note that most character devices always share mappings.
1970 	 * The one exception is that D_MMAP_ANON devices
1971 	 * (i.e. /dev/zero) permit private writable mappings.
1972 	 *
1973 	 * Rely on vm_mmap_cdev() to fail invalid MAP_PRIVATE requests
1974 	 * as well as updating maxprot to permit writing for
1975 	 * D_MMAP_ANON devices rather than doing that here.
1976 	 */
1977 	if ((flags & MAP_SHARED) != 0) {
1978 		if ((fp->f_flag & FWRITE) != 0)
1979 			maxprot |= VM_PROT_WRITE;
1980 		else if ((prot & VM_PROT_WRITE) != 0)
1981 			return (EACCES);
1982 	}
1983 	maxprot &= cap_maxprot;
1984 
1985 	fpop = td->td_fpop;
1986 	error = devfs_fp_check(fp, &dev, &dsw, &ref);
1987 	if (error != 0)
1988 		return (error);
1989 
1990 	error = vm_mmap_cdev(td, size, prot, &maxprot, &flags, dev, dsw, &foff,
1991 	    &object);
1992 	td->td_fpop = fpop;
1993 	dev_relthread(dev, ref);
1994 	if (error != 0)
1995 		return (error);
1996 
1997 	error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object,
1998 	    foff, FALSE, td);
1999 	if (error != 0)
2000 		vm_object_deallocate(object);
2001 	return (error);
2002 }
2003 
2004 dev_t
2005 dev2udev(struct cdev *x)
2006 {
2007 	if (x == NULL)
2008 		return (NODEV);
2009 	return (cdev2priv(x)->cdp_inode);
2010 }
2011 
2012 static struct fileops devfs_ops_f = {
2013 	.fo_read =	devfs_read_f,
2014 	.fo_write =	devfs_write_f,
2015 	.fo_truncate =	devfs_truncate_f,
2016 	.fo_ioctl =	devfs_ioctl_f,
2017 	.fo_poll =	devfs_poll_f,
2018 	.fo_kqfilter =	devfs_kqfilter_f,
2019 	.fo_stat =	devfs_stat_f,
2020 	.fo_close =	devfs_close_f,
2021 	.fo_chmod =	vn_chmod,
2022 	.fo_chown =	vn_chown,
2023 	.fo_sendfile =	vn_sendfile,
2024 	.fo_seek =	vn_seek,
2025 	.fo_fill_kinfo = vn_fill_kinfo,
2026 	.fo_mmap =	devfs_mmap_f,
2027 	.fo_flags =	DFLAG_PASSABLE | DFLAG_SEEKABLE
2028 };
2029 
2030 /* Vops for non-CHR vnodes in /dev. */
2031 static struct vop_vector devfs_vnodeops = {
2032 	.vop_default =		&default_vnodeops,
2033 
2034 	.vop_access =		devfs_access,
2035 	.vop_getattr =		devfs_getattr,
2036 	.vop_ioctl =		devfs_rioctl,
2037 	.vop_lookup =		devfs_lookup,
2038 	.vop_mknod =		devfs_mknod,
2039 	.vop_pathconf =		devfs_pathconf,
2040 	.vop_read =		devfs_rread,
2041 	.vop_readdir =		devfs_readdir,
2042 	.vop_readlink =		devfs_readlink,
2043 	.vop_reclaim =		devfs_reclaim,
2044 	.vop_remove =		devfs_remove,
2045 	.vop_revoke =		devfs_revoke,
2046 	.vop_setattr =		devfs_setattr,
2047 #ifdef MAC
2048 	.vop_setlabel =		devfs_setlabel,
2049 #endif
2050 	.vop_symlink =		devfs_symlink,
2051 	.vop_vptocnp =		devfs_vptocnp,
2052 	.vop_lock1 =		vop_lock,
2053 	.vop_unlock =		vop_unlock,
2054 	.vop_islocked =		vop_islocked,
2055 	.vop_add_writecount =	vop_stdadd_writecount_nomsync,
2056 };
2057 VFS_VOP_VECTOR_REGISTER(devfs_vnodeops);
2058 
2059 /* Vops for VCHR vnodes in /dev. */
2060 static struct vop_vector devfs_specops = {
2061 	.vop_default =		&default_vnodeops,
2062 
2063 	.vop_access =		devfs_access,
2064 	.vop_bmap =		VOP_PANIC,
2065 	.vop_close =		devfs_close,
2066 	.vop_create =		VOP_PANIC,
2067 	.vop_fsync =		vop_stdfsync,
2068 	.vop_getattr =		devfs_getattr,
2069 	.vop_ioctl =		devfs_ioctl,
2070 	.vop_link =		VOP_PANIC,
2071 	.vop_mkdir =		VOP_PANIC,
2072 	.vop_mknod =		VOP_PANIC,
2073 	.vop_open =		devfs_open,
2074 	.vop_pathconf =		devfs_pathconf,
2075 	.vop_poll =		dead_poll,
2076 	.vop_print =		devfs_print,
2077 	.vop_read =		dead_read,
2078 	.vop_readdir =		VOP_PANIC,
2079 	.vop_readlink =		VOP_PANIC,
2080 	.vop_reallocblks =	VOP_PANIC,
2081 	.vop_reclaim =		devfs_reclaim_vchr,
2082 	.vop_remove =		devfs_remove,
2083 	.vop_rename =		VOP_PANIC,
2084 	.vop_revoke =		devfs_revoke,
2085 	.vop_rmdir =		VOP_PANIC,
2086 	.vop_setattr =		devfs_setattr,
2087 #ifdef MAC
2088 	.vop_setlabel =		devfs_setlabel,
2089 #endif
2090 	.vop_strategy =		VOP_PANIC,
2091 	.vop_symlink =		VOP_PANIC,
2092 	.vop_vptocnp =		devfs_vptocnp,
2093 	.vop_write =		dead_write,
2094 	.vop_lock1 =		vop_lock,
2095 	.vop_unlock =		vop_unlock,
2096 	.vop_islocked =		vop_islocked,
2097 	.vop_add_writecount =	vop_stdadd_writecount_nomsync,
2098 };
2099 VFS_VOP_VECTOR_REGISTER(devfs_specops);
2100 
2101 /*
2102  * Our calling convention to the device drivers used to be that we passed
2103  * vnode.h IO_* flags to read()/write(), but we're moving to fcntl.h O_
2104  * flags instead since that's what open(), close() and ioctl() takes and
2105  * we don't really want vnode.h in device drivers.
2106  * We solved the source compatibility by redefining some vnode flags to
2107  * be the same as the fcntl ones and by sending down the bitwise OR of
2108  * the respective fcntl/vnode flags.  These CTASSERTS make sure nobody
2109  * pulls the rug out under this.
2110  */
2111 CTASSERT(O_NONBLOCK == IO_NDELAY);
2112 CTASSERT(O_FSYNC == IO_SYNC);
2113