xref: /freebsd/sys/fs/devfs/devfs_vnops.c (revision 535af610)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 		vgone(vp);
617 		vput(vp);
618 		return (ENOENT);
619 	}
620 #ifdef MAC
621 	mac_devfs_vnode_associate(mp, de, vp);
622 #endif
623 	sx_xunlock(&dmp->dm_lock);
624 	vn_set_state(vp, VSTATE_CONSTRUCTED);
625 	*vpp = vp;
626 	return (0);
627 }
628 
629 static int
630 devfs_access(struct vop_access_args *ap)
631 {
632 	struct vnode *vp = ap->a_vp;
633 	struct devfs_dirent *de;
634 	struct proc *p;
635 	int error;
636 
637 	de = vp->v_data;
638 	if (vp->v_type == VDIR)
639 		de = de->de_dir;
640 
641 	error = vaccess(vp->v_type, de->de_mode, de->de_uid, de->de_gid,
642 	    ap->a_accmode, ap->a_cred);
643 	if (error == 0)
644 		return (0);
645 	if (error != EACCES)
646 		return (error);
647 	p = ap->a_td->td_proc;
648 	/* We do, however, allow access to the controlling terminal */
649 	PROC_LOCK(p);
650 	if (!(p->p_flag & P_CONTROLT)) {
651 		PROC_UNLOCK(p);
652 		return (error);
653 	}
654 	if (p->p_session->s_ttydp == de->de_cdp)
655 		error = 0;
656 	PROC_UNLOCK(p);
657 	return (error);
658 }
659 
660 _Static_assert(((FMASK | FCNTLFLAGS) & (FLASTCLOSE | FREVOKE)) == 0,
661     "devfs-only flag reuse failed");
662 
663 static int
664 devfs_close(struct vop_close_args *ap)
665 {
666 	struct vnode *vp = ap->a_vp, *oldvp;
667 	struct thread *td = ap->a_td;
668 	struct proc *p;
669 	struct cdev *dev = vp->v_rdev;
670 	struct cdevsw *dsw;
671 	struct devfs_dirent *de = vp->v_data;
672 	int dflags, error, ref, vp_locked;
673 
674 	/*
675 	 * XXX: Don't call d_close() if we were called because of
676 	 * XXX: insmntque() failure.
677 	 */
678 	if (vp->v_data == NULL)
679 		return (0);
680 
681 	/*
682 	 * Hack: a tty device that is a controlling terminal
683 	 * has a reference from the session structure.
684 	 * We cannot easily tell that a character device is
685 	 * a controlling terminal, unless it is the closing
686 	 * process' controlling terminal.  In that case,
687 	 * if the reference count is 2 (this last descriptor
688 	 * plus the session), release the reference from the session.
689 	 */
690 	if (de->de_usecount == 2 && td != NULL) {
691 		p = td->td_proc;
692 		PROC_LOCK(p);
693 		if (vp == p->p_session->s_ttyvp) {
694 			PROC_UNLOCK(p);
695 			oldvp = NULL;
696 			sx_xlock(&proctree_lock);
697 			if (vp == p->p_session->s_ttyvp) {
698 				SESS_LOCK(p->p_session);
699 				mtx_lock(&devfs_de_interlock);
700 				VI_LOCK(vp);
701 				if (devfs_usecountl(vp) == 2 && !VN_IS_DOOMED(vp)) {
702 					p->p_session->s_ttyvp = NULL;
703 					p->p_session->s_ttydp = NULL;
704 					oldvp = vp;
705 				}
706 				VI_UNLOCK(vp);
707 				mtx_unlock(&devfs_de_interlock);
708 				SESS_UNLOCK(p->p_session);
709 			}
710 			sx_xunlock(&proctree_lock);
711 			if (oldvp != NULL)
712 				devfs_ctty_unref(oldvp);
713 		} else
714 			PROC_UNLOCK(p);
715 	}
716 	/*
717 	 * We do not want to really close the device if it
718 	 * is still in use unless we are trying to close it
719 	 * forcibly. Since every use (buffer, vnode, swap, cmap)
720 	 * holds a reference to the vnode, and because we mark
721 	 * any other vnodes that alias this device, when the
722 	 * sum of the reference counts on all the aliased
723 	 * vnodes descends to one, we are on last close.
724 	 */
725 	dsw = dev_refthread(dev, &ref);
726 	if (dsw == NULL)
727 		return (ENXIO);
728 	dflags = 0;
729 	mtx_lock(&devfs_de_interlock);
730 	VI_LOCK(vp);
731 	if (devfs_usecountl(vp) == 1)
732 		dflags |= FLASTCLOSE;
733 	devfs_usecount_subl(vp);
734 	mtx_unlock(&devfs_de_interlock);
735 	if (VN_IS_DOOMED(vp)) {
736 		/* Forced close. */
737 		dflags |= FREVOKE | FNONBLOCK;
738 	} else if (dsw->d_flags & D_TRACKCLOSE) {
739 		/* Keep device updated on status. */
740 	} else if ((dflags & FLASTCLOSE) == 0) {
741 		VI_UNLOCK(vp);
742 		dev_relthread(dev, ref);
743 		return (0);
744 	}
745 	vholdnz(vp);
746 	VI_UNLOCK(vp);
747 	vp_locked = VOP_ISLOCKED(vp);
748 	VOP_UNLOCK(vp);
749 	KASSERT(dev->si_refcount > 0,
750 	    ("devfs_close() on un-referenced struct cdev *(%s)", devtoname(dev)));
751 	error = dsw->d_close(dev, ap->a_fflag | dflags, S_IFCHR, td);
752 	dev_relthread(dev, ref);
753 	vn_lock(vp, vp_locked | LK_RETRY);
754 	vdrop(vp);
755 	return (error);
756 }
757 
758 static int
759 devfs_close_f(struct file *fp, struct thread *td)
760 {
761 	int error;
762 	struct file *fpop;
763 
764 	/*
765 	 * NB: td may be NULL if this descriptor is closed due to
766 	 * garbage collection from a closed UNIX domain socket.
767 	 */
768 	fpop = curthread->td_fpop;
769 	curthread->td_fpop = fp;
770 	error = vnops.fo_close(fp, td);
771 	curthread->td_fpop = fpop;
772 
773 	/*
774 	 * The f_cdevpriv cannot be assigned non-NULL value while we
775 	 * are destroying the file.
776 	 */
777 	if (fp->f_cdevpriv != NULL)
778 		devfs_fpdrop(fp);
779 	return (error);
780 }
781 
782 static int
783 devfs_getattr(struct vop_getattr_args *ap)
784 {
785 	struct vnode *vp = ap->a_vp;
786 	struct vattr *vap = ap->a_vap;
787 	struct devfs_dirent *de;
788 	struct devfs_mount *dmp;
789 	struct cdev *dev;
790 	struct timeval boottime;
791 	int error;
792 
793 	error = devfs_populate_vp(vp);
794 	if (error != 0)
795 		return (error);
796 
797 	dmp = VFSTODEVFS(vp->v_mount);
798 	sx_xunlock(&dmp->dm_lock);
799 
800 	de = vp->v_data;
801 	KASSERT(de != NULL, ("Null dirent in devfs_getattr vp=%p", vp));
802 	if (vp->v_type == VDIR) {
803 		de = de->de_dir;
804 		KASSERT(de != NULL,
805 		    ("Null dir dirent in devfs_getattr vp=%p", vp));
806 	}
807 	vap->va_uid = de->de_uid;
808 	vap->va_gid = de->de_gid;
809 	vap->va_mode = de->de_mode;
810 	if (vp->v_type == VLNK)
811 		vap->va_size = strlen(de->de_symlink);
812 	else if (vp->v_type == VDIR)
813 		vap->va_size = vap->va_bytes = DEV_BSIZE;
814 	else
815 		vap->va_size = 0;
816 	if (vp->v_type != VDIR)
817 		vap->va_bytes = 0;
818 	vap->va_blocksize = DEV_BSIZE;
819 	vap->va_type = vp->v_type;
820 
821 	getboottime(&boottime);
822 #define fix(aa)							\
823 	do {							\
824 		if ((aa).tv_sec <= 3600) {			\
825 			(aa).tv_sec = boottime.tv_sec;		\
826 			(aa).tv_nsec = boottime.tv_usec * 1000; \
827 		}						\
828 	} while (0)
829 
830 	if (vp->v_type != VCHR)  {
831 		fix(de->de_atime);
832 		vap->va_atime = de->de_atime;
833 		fix(de->de_mtime);
834 		vap->va_mtime = de->de_mtime;
835 		fix(de->de_ctime);
836 		vap->va_ctime = de->de_ctime;
837 	} else {
838 		dev = vp->v_rdev;
839 		fix(dev->si_atime);
840 		vap->va_atime = dev->si_atime;
841 		fix(dev->si_mtime);
842 		vap->va_mtime = dev->si_mtime;
843 		fix(dev->si_ctime);
844 		vap->va_ctime = dev->si_ctime;
845 
846 		vap->va_rdev = cdev2priv(dev)->cdp_inode;
847 	}
848 	vap->va_gen = 0;
849 	vap->va_flags = 0;
850 	vap->va_filerev = 0;
851 	vap->va_nlink = de->de_links;
852 	vap->va_fileid = de->de_inode;
853 
854 	return (error);
855 }
856 
857 /* ARGSUSED */
858 static int
859 devfs_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred, struct thread *td)
860 {
861 	struct file *fpop;
862 	int error;
863 
864 	fpop = td->td_fpop;
865 	td->td_fpop = fp;
866 	error = vnops.fo_ioctl(fp, com, data, cred, td);
867 	td->td_fpop = fpop;
868 	return (error);
869 }
870 
871 void *
872 fiodgname_buf_get_ptr(void *fgnp, u_long com)
873 {
874 	union {
875 		struct fiodgname_arg	fgn;
876 #ifdef COMPAT_FREEBSD32
877 		struct fiodgname_arg32	fgn32;
878 #endif
879 	} *fgnup;
880 
881 	fgnup = fgnp;
882 	switch (com) {
883 	case FIODGNAME:
884 		return (fgnup->fgn.buf);
885 #ifdef COMPAT_FREEBSD32
886 	case FIODGNAME_32:
887 		return ((void *)(uintptr_t)fgnup->fgn32.buf);
888 #endif
889 	default:
890 		panic("Unhandled ioctl command %ld", com);
891 	}
892 }
893 
894 static int
895 devfs_ioctl(struct vop_ioctl_args *ap)
896 {
897 	struct fiodgname_arg *fgn;
898 	struct vnode *vpold, *vp;
899 	struct cdevsw *dsw;
900 	struct thread *td;
901 	struct session *sess;
902 	struct cdev *dev;
903 	int error, ref, i;
904 	const char *p;
905 	u_long com;
906 
907 	vp = ap->a_vp;
908 	com = ap->a_command;
909 	td = ap->a_td;
910 
911 	dsw = devvn_refthread(vp, &dev, &ref);
912 	if (dsw == NULL)
913 		return (ENXIO);
914 	KASSERT(dev->si_refcount > 0,
915 	    ("devfs: un-referenced struct cdev *(%s)", devtoname(dev)));
916 
917 	switch (com) {
918 	case FIODTYPE:
919 		*(int *)ap->a_data = dsw->d_flags & D_TYPEMASK;
920 		error = 0;
921 		break;
922 	case FIODGNAME:
923 #ifdef	COMPAT_FREEBSD32
924 	case FIODGNAME_32:
925 #endif
926 		fgn = ap->a_data;
927 		p = devtoname(dev);
928 		i = strlen(p) + 1;
929 		if (i > fgn->len)
930 			error = EINVAL;
931 		else
932 			error = copyout(p, fiodgname_buf_get_ptr(fgn, com), i);
933 		break;
934 	default:
935 		error = dsw->d_ioctl(dev, com, ap->a_data, ap->a_fflag, td);
936 	}
937 
938 	dev_relthread(dev, ref);
939 	if (error == ENOIOCTL)
940 		error = ENOTTY;
941 
942 	if (error == 0 && com == TIOCSCTTY) {
943 		/*
944 		 * Do nothing if reassigning same control tty, or if the
945 		 * control tty has already disappeared.  If it disappeared,
946 		 * it's because we were racing with TIOCNOTTY.  TIOCNOTTY
947 		 * already took care of releasing the old vnode and we have
948 		 * nothing left to do.
949 		 */
950 		sx_slock(&proctree_lock);
951 		sess = td->td_proc->p_session;
952 		if (sess->s_ttyvp == vp || sess->s_ttyp == NULL) {
953 			sx_sunlock(&proctree_lock);
954 			return (0);
955 		}
956 
957 		devfs_ctty_ref(vp);
958 		SESS_LOCK(sess);
959 		vpold = sess->s_ttyvp;
960 		sess->s_ttyvp = vp;
961 		sess->s_ttydp = cdev2priv(dev);
962 		SESS_UNLOCK(sess);
963 
964 		sx_sunlock(&proctree_lock);
965 
966 		/* Get rid of reference to old control tty */
967 		if (vpold)
968 			devfs_ctty_unref(vpold);
969 	}
970 	return (error);
971 }
972 
973 /* ARGSUSED */
974 static int
975 devfs_kqfilter_f(struct file *fp, struct knote *kn)
976 {
977 	struct cdev *dev;
978 	struct cdevsw *dsw;
979 	int error, ref;
980 	struct file *fpop;
981 	struct thread *td;
982 
983 	td = curthread;
984 	fpop = td->td_fpop;
985 	error = devfs_fp_check(fp, &dev, &dsw, &ref);
986 	if (error)
987 		return (error);
988 	error = dsw->d_kqfilter(dev, kn);
989 	td->td_fpop = fpop;
990 	dev_relthread(dev, ref);
991 	return (error);
992 }
993 
994 static inline int
995 devfs_prison_check(struct devfs_dirent *de, struct thread *td)
996 {
997 	struct cdev_priv *cdp;
998 	struct ucred *dcr;
999 	struct proc *p;
1000 	int error;
1001 
1002 	cdp = de->de_cdp;
1003 	if (cdp == NULL)
1004 		return (0);
1005 	dcr = cdp->cdp_c.si_cred;
1006 	if (dcr == NULL)
1007 		return (0);
1008 
1009 	error = prison_check(td->td_ucred, dcr);
1010 	if (error == 0)
1011 		return (0);
1012 	/* We do, however, allow access to the controlling terminal */
1013 	p = td->td_proc;
1014 	PROC_LOCK(p);
1015 	if (!(p->p_flag & P_CONTROLT)) {
1016 		PROC_UNLOCK(p);
1017 		return (error);
1018 	}
1019 	if (p->p_session->s_ttydp == cdp)
1020 		error = 0;
1021 	PROC_UNLOCK(p);
1022 	return (error);
1023 }
1024 
1025 static int
1026 devfs_lookupx(struct vop_lookup_args *ap, int *dm_unlock)
1027 {
1028 	struct componentname *cnp;
1029 	struct vnode *dvp, **vpp;
1030 	struct thread *td;
1031 	struct devfs_dirent *de, *dd;
1032 	struct devfs_dirent **dde;
1033 	struct devfs_mount *dmp;
1034 	struct mount *mp;
1035 	struct cdev *cdev;
1036 	int error, flags, nameiop, dvplocked;
1037 	char specname[SPECNAMELEN + 1], *pname;
1038 
1039 	td = curthread;
1040 	cnp = ap->a_cnp;
1041 	vpp = ap->a_vpp;
1042 	dvp = ap->a_dvp;
1043 	pname = cnp->cn_nameptr;
1044 	flags = cnp->cn_flags;
1045 	nameiop = cnp->cn_nameiop;
1046 	mp = dvp->v_mount;
1047 	dmp = VFSTODEVFS(mp);
1048 	dd = dvp->v_data;
1049 	*vpp = NULLVP;
1050 
1051 	if ((flags & ISLASTCN) && nameiop == RENAME)
1052 		return (EOPNOTSUPP);
1053 
1054 	if (dvp->v_type != VDIR)
1055 		return (ENOTDIR);
1056 
1057 	if ((flags & ISDOTDOT) && (dvp->v_vflag & VV_ROOT))
1058 		return (EIO);
1059 
1060 	error = vn_dir_check_exec(dvp, cnp);
1061 	if (error != 0)
1062 		return (error);
1063 
1064 	if (cnp->cn_namelen == 1 && *pname == '.') {
1065 		if ((flags & ISLASTCN) && nameiop != LOOKUP)
1066 			return (EINVAL);
1067 		*vpp = dvp;
1068 		VREF(dvp);
1069 		return (0);
1070 	}
1071 
1072 	if (flags & ISDOTDOT) {
1073 		if ((flags & ISLASTCN) && nameiop != LOOKUP)
1074 			return (EINVAL);
1075 		de = devfs_parent_dirent(dd);
1076 		if (de == NULL)
1077 			return (ENOENT);
1078 		dvplocked = VOP_ISLOCKED(dvp);
1079 		VOP_UNLOCK(dvp);
1080 		error = devfs_allocv(de, mp, cnp->cn_lkflags & LK_TYPE_MASK,
1081 		    vpp);
1082 		*dm_unlock = 0;
1083 		vn_lock(dvp, dvplocked | LK_RETRY);
1084 		return (error);
1085 	}
1086 
1087 	dd = dvp->v_data;
1088 	de = devfs_find(dd, cnp->cn_nameptr, cnp->cn_namelen, 0);
1089 	while (de == NULL) {	/* While(...) so we can use break */
1090 
1091 		if (nameiop == DELETE)
1092 			return (ENOENT);
1093 
1094 		/*
1095 		 * OK, we didn't have an entry for the name we were asked for
1096 		 * so we try to see if anybody can create it on demand.
1097 		 */
1098 		pname = devfs_fqpn(specname, dmp, dd, cnp);
1099 		if (pname == NULL)
1100 			break;
1101 
1102 		cdev = NULL;
1103 		DEVFS_DMP_HOLD(dmp);
1104 		sx_xunlock(&dmp->dm_lock);
1105 		EVENTHANDLER_INVOKE(dev_clone,
1106 		    td->td_ucred, pname, strlen(pname), &cdev);
1107 
1108 		if (cdev == NULL)
1109 			sx_xlock(&dmp->dm_lock);
1110 		else if (devfs_populate_vp(dvp) != 0) {
1111 			*dm_unlock = 0;
1112 			sx_xlock(&dmp->dm_lock);
1113 			if (DEVFS_DMP_DROP(dmp)) {
1114 				sx_xunlock(&dmp->dm_lock);
1115 				devfs_unmount_final(dmp);
1116 			} else
1117 				sx_xunlock(&dmp->dm_lock);
1118 			dev_rel(cdev);
1119 			return (ENOENT);
1120 		}
1121 		if (DEVFS_DMP_DROP(dmp)) {
1122 			*dm_unlock = 0;
1123 			sx_xunlock(&dmp->dm_lock);
1124 			devfs_unmount_final(dmp);
1125 			if (cdev != NULL)
1126 				dev_rel(cdev);
1127 			return (ENOENT);
1128 		}
1129 
1130 		if (cdev == NULL)
1131 			break;
1132 
1133 		dev_lock();
1134 		dde = &cdev2priv(cdev)->cdp_dirents[dmp->dm_idx];
1135 		if (dde != NULL && *dde != NULL)
1136 			de = *dde;
1137 		dev_unlock();
1138 		dev_rel(cdev);
1139 		break;
1140 	}
1141 
1142 	if (de == NULL || de->de_flags & DE_WHITEOUT) {
1143 		if ((nameiop == CREATE || nameiop == RENAME) &&
1144 		    (flags & (LOCKPARENT | WANTPARENT)) && (flags & ISLASTCN)) {
1145 			return (EJUSTRETURN);
1146 		}
1147 		return (ENOENT);
1148 	}
1149 
1150 	if (devfs_prison_check(de, td))
1151 		return (ENOENT);
1152 
1153 	if ((cnp->cn_nameiop == DELETE) && (flags & ISLASTCN)) {
1154 		error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
1155 		if (error)
1156 			return (error);
1157 		if (*vpp == dvp) {
1158 			VREF(dvp);
1159 			*vpp = dvp;
1160 			return (0);
1161 		}
1162 	}
1163 	error = devfs_allocv(de, mp, cnp->cn_lkflags & LK_TYPE_MASK, vpp);
1164 	*dm_unlock = 0;
1165 	return (error);
1166 }
1167 
1168 static int
1169 devfs_lookup(struct vop_lookup_args *ap)
1170 {
1171 	int j;
1172 	struct devfs_mount *dmp;
1173 	int dm_unlock;
1174 
1175 	if (devfs_populate_vp(ap->a_dvp) != 0)
1176 		return (ENOTDIR);
1177 
1178 	dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1179 	dm_unlock = 1;
1180 	j = devfs_lookupx(ap, &dm_unlock);
1181 	if (dm_unlock == 1)
1182 		sx_xunlock(&dmp->dm_lock);
1183 	return (j);
1184 }
1185 
1186 static int
1187 devfs_mknod(struct vop_mknod_args *ap)
1188 {
1189 	struct componentname *cnp;
1190 	struct vnode *dvp, **vpp;
1191 	struct devfs_dirent *dd, *de;
1192 	struct devfs_mount *dmp;
1193 	int error;
1194 
1195 	/*
1196 	 * The only type of node we should be creating here is a
1197 	 * character device, for anything else return EOPNOTSUPP.
1198 	 */
1199 	if (ap->a_vap->va_type != VCHR)
1200 		return (EOPNOTSUPP);
1201 	dvp = ap->a_dvp;
1202 	dmp = VFSTODEVFS(dvp->v_mount);
1203 
1204 	cnp = ap->a_cnp;
1205 	vpp = ap->a_vpp;
1206 	dd = dvp->v_data;
1207 
1208 	error = ENOENT;
1209 	sx_xlock(&dmp->dm_lock);
1210 	TAILQ_FOREACH(de, &dd->de_dlist, de_list) {
1211 		if (cnp->cn_namelen != de->de_dirent->d_namlen)
1212 			continue;
1213 		if (de->de_dirent->d_type == DT_CHR &&
1214 		    (de->de_cdp->cdp_flags & CDP_ACTIVE) == 0)
1215 			continue;
1216 		if (bcmp(cnp->cn_nameptr, de->de_dirent->d_name,
1217 		    de->de_dirent->d_namlen) != 0)
1218 			continue;
1219 		if (de->de_flags & DE_WHITEOUT)
1220 			break;
1221 		goto notfound;
1222 	}
1223 	if (de == NULL)
1224 		goto notfound;
1225 	de->de_flags &= ~DE_WHITEOUT;
1226 	error = devfs_allocv(de, dvp->v_mount, LK_EXCLUSIVE, vpp);
1227 	return (error);
1228 notfound:
1229 	sx_xunlock(&dmp->dm_lock);
1230 	return (error);
1231 }
1232 
1233 /* ARGSUSED */
1234 static int
1235 devfs_open(struct vop_open_args *ap)
1236 {
1237 	struct thread *td = ap->a_td;
1238 	struct vnode *vp = ap->a_vp;
1239 	struct cdev *dev = vp->v_rdev;
1240 	struct file *fp = ap->a_fp;
1241 	int error, ref, vlocked;
1242 	struct cdevsw *dsw;
1243 	struct file *fpop;
1244 
1245 	if (vp->v_type == VBLK)
1246 		return (ENXIO);
1247 
1248 	if (dev == NULL)
1249 		return (ENXIO);
1250 
1251 	/* Make this field valid before any I/O in d_open. */
1252 	if (dev->si_iosize_max == 0)
1253 		dev->si_iosize_max = DFLTPHYS;
1254 
1255 	dsw = dev_refthread(dev, &ref);
1256 	if (dsw == NULL)
1257 		return (ENXIO);
1258 	if (fp == NULL && dsw->d_fdopen != NULL) {
1259 		dev_relthread(dev, ref);
1260 		return (ENXIO);
1261 	}
1262 
1263 	if (vp->v_type == VCHR)
1264 		devfs_usecount_add(vp);
1265 
1266 	vlocked = VOP_ISLOCKED(vp);
1267 	VOP_UNLOCK(vp);
1268 
1269 	fpop = td->td_fpop;
1270 	td->td_fpop = fp;
1271 	if (fp != NULL) {
1272 		fp->f_data = dev;
1273 		fp->f_vnode = vp;
1274 	}
1275 	if (dsw->d_fdopen != NULL)
1276 		error = dsw->d_fdopen(dev, ap->a_mode, td, fp);
1277 	else
1278 		error = dsw->d_open(dev, ap->a_mode, S_IFCHR, td);
1279 	/* Clean up any cdevpriv upon error. */
1280 	if (error != 0)
1281 		devfs_clear_cdevpriv();
1282 	td->td_fpop = fpop;
1283 
1284 	vn_lock(vp, vlocked | LK_RETRY);
1285 	if (error != 0 && vp->v_type == VCHR)
1286 		devfs_usecount_sub(vp);
1287 
1288 	dev_relthread(dev, ref);
1289 	if (error != 0) {
1290 		if (error == ERESTART)
1291 			error = EINTR;
1292 		return (error);
1293 	}
1294 
1295 #if 0	/* /dev/console */
1296 	KASSERT(fp != NULL, ("Could not vnode bypass device on NULL fp"));
1297 #else
1298 	if (fp == NULL)
1299 		return (error);
1300 #endif
1301 	if (fp->f_ops == &badfileops)
1302 		finit(fp, fp->f_flag, DTYPE_VNODE, dev, &devfs_ops_f);
1303 	return (error);
1304 }
1305 
1306 static int
1307 devfs_pathconf(struct vop_pathconf_args *ap)
1308 {
1309 
1310 	switch (ap->a_name) {
1311 	case _PC_FILESIZEBITS:
1312 		*ap->a_retval = 64;
1313 		return (0);
1314 	case _PC_NAME_MAX:
1315 		*ap->a_retval = NAME_MAX;
1316 		return (0);
1317 	case _PC_LINK_MAX:
1318 		*ap->a_retval = INT_MAX;
1319 		return (0);
1320 	case _PC_SYMLINK_MAX:
1321 		*ap->a_retval = MAXPATHLEN;
1322 		return (0);
1323 	case _PC_MAX_CANON:
1324 		if (ap->a_vp->v_vflag & VV_ISTTY) {
1325 			*ap->a_retval = MAX_CANON;
1326 			return (0);
1327 		}
1328 		return (EINVAL);
1329 	case _PC_MAX_INPUT:
1330 		if (ap->a_vp->v_vflag & VV_ISTTY) {
1331 			*ap->a_retval = MAX_INPUT;
1332 			return (0);
1333 		}
1334 		return (EINVAL);
1335 	case _PC_VDISABLE:
1336 		if (ap->a_vp->v_vflag & VV_ISTTY) {
1337 			*ap->a_retval = _POSIX_VDISABLE;
1338 			return (0);
1339 		}
1340 		return (EINVAL);
1341 	case _PC_MAC_PRESENT:
1342 #ifdef MAC
1343 		/*
1344 		 * If MAC is enabled, devfs automatically supports
1345 		 * trivial non-persistent label storage.
1346 		 */
1347 		*ap->a_retval = 1;
1348 #else
1349 		*ap->a_retval = 0;
1350 #endif
1351 		return (0);
1352 	case _PC_CHOWN_RESTRICTED:
1353 		*ap->a_retval = 1;
1354 		return (0);
1355 	default:
1356 		return (vop_stdpathconf(ap));
1357 	}
1358 	/* NOTREACHED */
1359 }
1360 
1361 /* ARGSUSED */
1362 static int
1363 devfs_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td)
1364 {
1365 	struct cdev *dev;
1366 	struct cdevsw *dsw;
1367 	int error, ref;
1368 	struct file *fpop;
1369 
1370 	fpop = td->td_fpop;
1371 	error = devfs_fp_check(fp, &dev, &dsw, &ref);
1372 	if (error != 0) {
1373 		error = vnops.fo_poll(fp, events, cred, td);
1374 		return (error);
1375 	}
1376 	error = dsw->d_poll(dev, events, td);
1377 	td->td_fpop = fpop;
1378 	dev_relthread(dev, ref);
1379 	return(error);
1380 }
1381 
1382 /*
1383  * Print out the contents of a special device vnode.
1384  */
1385 static int
1386 devfs_print(struct vop_print_args *ap)
1387 {
1388 
1389 	printf("\tdev %s\n", devtoname(ap->a_vp->v_rdev));
1390 	return (0);
1391 }
1392 
1393 static int
1394 devfs_read_f(struct file *fp, struct uio *uio, struct ucred *cred,
1395     int flags, struct thread *td)
1396 {
1397 	struct cdev *dev;
1398 	int ioflag, error, ref;
1399 	ssize_t resid;
1400 	struct cdevsw *dsw;
1401 	struct file *fpop;
1402 
1403 	if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1404 		return (EINVAL);
1405 	fpop = td->td_fpop;
1406 	error = devfs_fp_check(fp, &dev, &dsw, &ref);
1407 	if (error != 0) {
1408 		error = vnops.fo_read(fp, uio, cred, flags, td);
1409 		return (error);
1410 	}
1411 	resid = uio->uio_resid;
1412 	ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT);
1413 	if (ioflag & O_DIRECT)
1414 		ioflag |= IO_DIRECT;
1415 
1416 	foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1417 	error = dsw->d_read(dev, uio, ioflag);
1418 	if (uio->uio_resid != resid || (error == 0 && resid != 0))
1419 		devfs_timestamp(&dev->si_atime);
1420 	td->td_fpop = fpop;
1421 	dev_relthread(dev, ref);
1422 
1423 	foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF_R);
1424 	return (error);
1425 }
1426 
1427 static int
1428 devfs_readdir(struct vop_readdir_args *ap)
1429 {
1430 	int error;
1431 	struct uio *uio;
1432 	struct dirent *dp;
1433 	struct devfs_dirent *dd;
1434 	struct devfs_dirent *de;
1435 	struct devfs_mount *dmp;
1436 	off_t off;
1437 	int *tmp_ncookies = NULL;
1438 
1439 	if (ap->a_vp->v_type != VDIR)
1440 		return (ENOTDIR);
1441 
1442 	uio = ap->a_uio;
1443 	if (uio->uio_offset < 0)
1444 		return (EINVAL);
1445 
1446 	/*
1447 	 * XXX: This is a temporary hack to get around this filesystem not
1448 	 * supporting cookies. We store the location of the ncookies pointer
1449 	 * in a temporary variable before calling vfs_subr.c:vfs_read_dirent()
1450 	 * and set the number of cookies to 0. We then set the pointer to
1451 	 * NULL so that vfs_read_dirent doesn't try to call realloc() on
1452 	 * ap->a_cookies. Later in this function, we restore the ap->a_ncookies
1453 	 * pointer to its original location before returning to the caller.
1454 	 */
1455 	if (ap->a_ncookies != NULL) {
1456 		tmp_ncookies = ap->a_ncookies;
1457 		*ap->a_ncookies = 0;
1458 		ap->a_ncookies = NULL;
1459 	}
1460 
1461 	dmp = VFSTODEVFS(ap->a_vp->v_mount);
1462 	if (devfs_populate_vp(ap->a_vp) != 0) {
1463 		if (tmp_ncookies != NULL)
1464 			ap->a_ncookies = tmp_ncookies;
1465 		return (EIO);
1466 	}
1467 	error = 0;
1468 	de = ap->a_vp->v_data;
1469 	off = 0;
1470 	TAILQ_FOREACH(dd, &de->de_dlist, de_list) {
1471 		KASSERT(dd->de_cdp != (void *)0xdeadc0de, ("%s %d\n", __func__, __LINE__));
1472 		if (dd->de_flags & (DE_COVERED | DE_WHITEOUT))
1473 			continue;
1474 		if (devfs_prison_check(dd, uio->uio_td))
1475 			continue;
1476 		if (dd->de_dirent->d_type == DT_DIR)
1477 			de = dd->de_dir;
1478 		else
1479 			de = dd;
1480 		dp = dd->de_dirent;
1481 		MPASS(dp->d_reclen == GENERIC_DIRSIZ(dp));
1482 		if (dp->d_reclen > uio->uio_resid)
1483 			break;
1484 		dp->d_fileno = de->de_inode;
1485 		/* NOTE: d_off is the offset for the *next* entry. */
1486 		dp->d_off = off + dp->d_reclen;
1487 		if (off >= uio->uio_offset) {
1488 			error = vfs_read_dirent(ap, dp, off);
1489 			if (error)
1490 				break;
1491 		}
1492 		off += dp->d_reclen;
1493 	}
1494 	sx_xunlock(&dmp->dm_lock);
1495 	uio->uio_offset = off;
1496 
1497 	/*
1498 	 * Restore ap->a_ncookies if it wasn't originally NULL in the first
1499 	 * place.
1500 	 */
1501 	if (tmp_ncookies != NULL)
1502 		ap->a_ncookies = tmp_ncookies;
1503 
1504 	return (error);
1505 }
1506 
1507 static int
1508 devfs_readlink(struct vop_readlink_args *ap)
1509 {
1510 	struct devfs_dirent *de;
1511 
1512 	de = ap->a_vp->v_data;
1513 	return (uiomove(de->de_symlink, strlen(de->de_symlink), ap->a_uio));
1514 }
1515 
1516 static void
1517 devfs_reclaiml(struct vnode *vp)
1518 {
1519 	struct devfs_dirent *de;
1520 
1521 	mtx_assert(&devfs_de_interlock, MA_OWNED);
1522 	de = vp->v_data;
1523 	if (de != NULL) {
1524 		MPASS(de->de_usecount == 0);
1525 		de->de_vnode = NULL;
1526 		vp->v_data = NULL;
1527 	}
1528 }
1529 
1530 static int
1531 devfs_reclaim(struct vop_reclaim_args *ap)
1532 {
1533 	struct vnode *vp;
1534 
1535 	vp = ap->a_vp;
1536 	mtx_lock(&devfs_de_interlock);
1537 	devfs_reclaiml(vp);
1538 	mtx_unlock(&devfs_de_interlock);
1539 	return (0);
1540 }
1541 
1542 static int
1543 devfs_reclaim_vchr(struct vop_reclaim_args *ap)
1544 {
1545 	struct vnode *vp;
1546 	struct cdev *dev;
1547 
1548 	vp = ap->a_vp;
1549 	MPASS(vp->v_type == VCHR);
1550 
1551 	mtx_lock(&devfs_de_interlock);
1552 	VI_LOCK(vp);
1553 	devfs_usecount_subl(vp);
1554 	devfs_reclaiml(vp);
1555 	mtx_unlock(&devfs_de_interlock);
1556 	dev_lock();
1557 	dev = vp->v_rdev;
1558 	vp->v_rdev = NULL;
1559 	dev_unlock();
1560 	VI_UNLOCK(vp);
1561 	if (dev != NULL)
1562 		dev_rel(dev);
1563 	return (0);
1564 }
1565 
1566 static int
1567 devfs_remove(struct vop_remove_args *ap)
1568 {
1569 	struct vnode *dvp = ap->a_dvp;
1570 	struct vnode *vp = ap->a_vp;
1571 	struct devfs_dirent *dd;
1572 	struct devfs_dirent *de, *de_covered;
1573 	struct devfs_mount *dmp = VFSTODEVFS(vp->v_mount);
1574 
1575 	ASSERT_VOP_ELOCKED(dvp, "devfs_remove");
1576 	ASSERT_VOP_ELOCKED(vp, "devfs_remove");
1577 
1578 	sx_xlock(&dmp->dm_lock);
1579 	dd = ap->a_dvp->v_data;
1580 	de = vp->v_data;
1581 	if (de->de_cdp == NULL) {
1582 		TAILQ_REMOVE(&dd->de_dlist, de, de_list);
1583 		if (de->de_dirent->d_type == DT_LNK) {
1584 			de_covered = devfs_find(dd, de->de_dirent->d_name,
1585 			    de->de_dirent->d_namlen, 0);
1586 			if (de_covered != NULL)
1587 				de_covered->de_flags &= ~DE_COVERED;
1588 		}
1589 		/* We need to unlock dvp because devfs_delete() may lock it. */
1590 		VOP_UNLOCK(vp);
1591 		if (dvp != vp)
1592 			VOP_UNLOCK(dvp);
1593 		devfs_delete(dmp, de, 0);
1594 		sx_xunlock(&dmp->dm_lock);
1595 		if (dvp != vp)
1596 			vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1597 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1598 	} else {
1599 		de->de_flags |= DE_WHITEOUT;
1600 		sx_xunlock(&dmp->dm_lock);
1601 	}
1602 	return (0);
1603 }
1604 
1605 /*
1606  * Revoke is called on a tty when a terminal session ends.  The vnode
1607  * is orphaned by setting v_op to deadfs so we need to let go of it
1608  * as well so that we create a new one next time around.
1609  *
1610  */
1611 static int
1612 devfs_revoke(struct vop_revoke_args *ap)
1613 {
1614 	struct vnode *vp = ap->a_vp, *vp2;
1615 	struct cdev *dev;
1616 	struct cdev_priv *cdp;
1617 	struct devfs_dirent *de;
1618 	enum vgetstate vs;
1619 	u_int i;
1620 
1621 	KASSERT((ap->a_flags & REVOKEALL) != 0, ("devfs_revoke !REVOKEALL"));
1622 
1623 	dev = vp->v_rdev;
1624 	cdp = cdev2priv(dev);
1625 
1626 	dev_lock();
1627 	cdp->cdp_inuse++;
1628 	dev_unlock();
1629 
1630 	vhold(vp);
1631 	vgone(vp);
1632 	vdrop(vp);
1633 
1634 	VOP_UNLOCK(vp);
1635  loop:
1636 	for (;;) {
1637 		mtx_lock(&devfs_de_interlock);
1638 		dev_lock();
1639 		vp2 = NULL;
1640 		for (i = 0; i <= cdp->cdp_maxdirent; i++) {
1641 			de = cdp->cdp_dirents[i];
1642 			if (de == NULL)
1643 				continue;
1644 
1645 			vp2 = de->de_vnode;
1646 			if (vp2 != NULL) {
1647 				dev_unlock();
1648 				vs = vget_prep(vp2);
1649 				mtx_unlock(&devfs_de_interlock);
1650 				if (vget_finish(vp2, LK_EXCLUSIVE, vs) != 0)
1651 					goto loop;
1652 				vhold(vp2);
1653 				vgone(vp2);
1654 				vdrop(vp2);
1655 				vput(vp2);
1656 				break;
1657 			}
1658 		}
1659 		if (vp2 != NULL) {
1660 			continue;
1661 		}
1662 		dev_unlock();
1663 		mtx_unlock(&devfs_de_interlock);
1664 		break;
1665 	}
1666 	dev_lock();
1667 	cdp->cdp_inuse--;
1668 	if (!(cdp->cdp_flags & CDP_ACTIVE) && cdp->cdp_inuse == 0) {
1669 		TAILQ_REMOVE(&cdevp_list, cdp, cdp_list);
1670 		dev_unlock();
1671 		dev_rel(&cdp->cdp_c);
1672 	} else
1673 		dev_unlock();
1674 
1675 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1676 	return (0);
1677 }
1678 
1679 static int
1680 devfs_rioctl(struct vop_ioctl_args *ap)
1681 {
1682 	struct vnode *vp;
1683 	struct devfs_mount *dmp;
1684 	int error;
1685 
1686 	vp = ap->a_vp;
1687 	vn_lock(vp, LK_SHARED | LK_RETRY);
1688 	if (VN_IS_DOOMED(vp)) {
1689 		VOP_UNLOCK(vp);
1690 		return (EBADF);
1691 	}
1692 	dmp = VFSTODEVFS(vp->v_mount);
1693 	sx_xlock(&dmp->dm_lock);
1694 	VOP_UNLOCK(vp);
1695 	DEVFS_DMP_HOLD(dmp);
1696 	devfs_populate(dmp);
1697 	if (DEVFS_DMP_DROP(dmp)) {
1698 		sx_xunlock(&dmp->dm_lock);
1699 		devfs_unmount_final(dmp);
1700 		return (ENOENT);
1701 	}
1702 	error = devfs_rules_ioctl(dmp, ap->a_command, ap->a_data, ap->a_td);
1703 	sx_xunlock(&dmp->dm_lock);
1704 	return (error);
1705 }
1706 
1707 static int
1708 devfs_rread(struct vop_read_args *ap)
1709 {
1710 
1711 	if (ap->a_vp->v_type != VDIR)
1712 		return (EINVAL);
1713 	return (VOP_READDIR(ap->a_vp, ap->a_uio, ap->a_cred, NULL, NULL, NULL));
1714 }
1715 
1716 static int
1717 devfs_setattr(struct vop_setattr_args *ap)
1718 {
1719 	struct devfs_dirent *de;
1720 	struct vattr *vap;
1721 	struct vnode *vp;
1722 	struct thread *td;
1723 	int c, error;
1724 	uid_t uid;
1725 	gid_t gid;
1726 
1727 	vap = ap->a_vap;
1728 	vp = ap->a_vp;
1729 	td = curthread;
1730 	if ((vap->va_type != VNON) ||
1731 	    (vap->va_nlink != VNOVAL) ||
1732 	    (vap->va_fsid != VNOVAL) ||
1733 	    (vap->va_fileid != VNOVAL) ||
1734 	    (vap->va_blocksize != VNOVAL) ||
1735 	    (vap->va_flags != VNOVAL && vap->va_flags != 0) ||
1736 	    (vap->va_rdev != VNOVAL) ||
1737 	    ((int)vap->va_bytes != VNOVAL) ||
1738 	    (vap->va_gen != VNOVAL)) {
1739 		return (EINVAL);
1740 	}
1741 
1742 	error = devfs_populate_vp(vp);
1743 	if (error != 0)
1744 		return (error);
1745 
1746 	de = vp->v_data;
1747 	if (vp->v_type == VDIR)
1748 		de = de->de_dir;
1749 
1750 	c = 0;
1751 	if (vap->va_uid == (uid_t)VNOVAL)
1752 		uid = de->de_uid;
1753 	else
1754 		uid = vap->va_uid;
1755 	if (vap->va_gid == (gid_t)VNOVAL)
1756 		gid = de->de_gid;
1757 	else
1758 		gid = vap->va_gid;
1759 	if (uid != de->de_uid || gid != de->de_gid) {
1760 		if ((ap->a_cred->cr_uid != de->de_uid) || uid != de->de_uid ||
1761 		    (gid != de->de_gid && !groupmember(gid, ap->a_cred))) {
1762 			error = priv_check(td, PRIV_VFS_CHOWN);
1763 			if (error != 0)
1764 				goto ret;
1765 		}
1766 		de->de_uid = uid;
1767 		de->de_gid = gid;
1768 		c = 1;
1769 	}
1770 
1771 	if (vap->va_mode != (mode_t)VNOVAL) {
1772 		if (ap->a_cred->cr_uid != de->de_uid) {
1773 			error = priv_check(td, PRIV_VFS_ADMIN);
1774 			if (error != 0)
1775 				goto ret;
1776 		}
1777 		de->de_mode = vap->va_mode;
1778 		c = 1;
1779 	}
1780 
1781 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1782 		error = vn_utimes_perm(vp, vap, ap->a_cred, td);
1783 		if (error != 0)
1784 			goto ret;
1785 		if (vap->va_atime.tv_sec != VNOVAL) {
1786 			if (vp->v_type == VCHR)
1787 				vp->v_rdev->si_atime = vap->va_atime;
1788 			else
1789 				de->de_atime = vap->va_atime;
1790 		}
1791 		if (vap->va_mtime.tv_sec != VNOVAL) {
1792 			if (vp->v_type == VCHR)
1793 				vp->v_rdev->si_mtime = vap->va_mtime;
1794 			else
1795 				de->de_mtime = vap->va_mtime;
1796 		}
1797 		c = 1;
1798 	}
1799 
1800 	if (c) {
1801 		if (vp->v_type == VCHR)
1802 			vfs_timestamp(&vp->v_rdev->si_ctime);
1803 		else
1804 			vfs_timestamp(&de->de_mtime);
1805 	}
1806 
1807 ret:
1808 	sx_xunlock(&VFSTODEVFS(vp->v_mount)->dm_lock);
1809 	return (error);
1810 }
1811 
1812 #ifdef MAC
1813 static int
1814 devfs_setlabel(struct vop_setlabel_args *ap)
1815 {
1816 	struct vnode *vp;
1817 	struct devfs_dirent *de;
1818 
1819 	vp = ap->a_vp;
1820 	de = vp->v_data;
1821 
1822 	mac_vnode_relabel(ap->a_cred, vp, ap->a_label);
1823 	mac_devfs_update(vp->v_mount, de, vp);
1824 
1825 	return (0);
1826 }
1827 #endif
1828 
1829 static int
1830 devfs_stat_f(struct file *fp, struct stat *sb, struct ucred *cred)
1831 {
1832 
1833 	return (vnops.fo_stat(fp, sb, cred));
1834 }
1835 
1836 static int
1837 devfs_symlink(struct vop_symlink_args *ap)
1838 {
1839 	int i, error;
1840 	struct devfs_dirent *dd;
1841 	struct devfs_dirent *de, *de_covered, *de_dotdot;
1842 	struct devfs_mount *dmp;
1843 
1844 	error = priv_check(curthread, PRIV_DEVFS_SYMLINK);
1845 	if (error)
1846 		return(error);
1847 	dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1848 	if (devfs_populate_vp(ap->a_dvp) != 0)
1849 		return (ENOENT);
1850 
1851 	dd = ap->a_dvp->v_data;
1852 	de = devfs_newdirent(ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen);
1853 	de->de_flags = DE_USER;
1854 	de->de_uid = 0;
1855 	de->de_gid = 0;
1856 	de->de_mode = 0755;
1857 	de->de_inode = alloc_unr(devfs_inos);
1858 	de->de_dir = dd;
1859 	de->de_dirent->d_type = DT_LNK;
1860 	i = strlen(ap->a_target) + 1;
1861 	de->de_symlink = malloc(i, M_DEVFS, M_WAITOK);
1862 	bcopy(ap->a_target, de->de_symlink, i);
1863 #ifdef MAC
1864 	mac_devfs_create_symlink(ap->a_cnp->cn_cred, dmp->dm_mount, dd, de);
1865 #endif
1866 	de_covered = devfs_find(dd, de->de_dirent->d_name,
1867 	    de->de_dirent->d_namlen, 0);
1868 	if (de_covered != NULL) {
1869 		if ((de_covered->de_flags & DE_USER) != 0) {
1870 			devfs_delete(dmp, de, DEVFS_DEL_NORECURSE);
1871 			sx_xunlock(&dmp->dm_lock);
1872 			return (EEXIST);
1873 		}
1874 		KASSERT((de_covered->de_flags & DE_COVERED) == 0,
1875 		    ("devfs_symlink: entry %p already covered", de_covered));
1876 		de_covered->de_flags |= DE_COVERED;
1877 	}
1878 
1879 	de_dotdot = TAILQ_FIRST(&dd->de_dlist);		/* "." */
1880 	de_dotdot = TAILQ_NEXT(de_dotdot, de_list);	/* ".." */
1881 	TAILQ_INSERT_AFTER(&dd->de_dlist, de_dotdot, de, de_list);
1882 	devfs_dir_ref_de(dmp, dd);
1883 	devfs_rules_apply(dmp, de);
1884 
1885 	return (devfs_allocv(de, ap->a_dvp->v_mount, LK_EXCLUSIVE, ap->a_vpp));
1886 }
1887 
1888 static int
1889 devfs_truncate_f(struct file *fp, off_t length, struct ucred *cred, struct thread *td)
1890 {
1891 
1892 	return (vnops.fo_truncate(fp, length, cred, td));
1893 }
1894 
1895 static int
1896 devfs_write_f(struct file *fp, struct uio *uio, struct ucred *cred,
1897     int flags, struct thread *td)
1898 {
1899 	struct cdev *dev;
1900 	int error, ioflag, ref;
1901 	ssize_t resid;
1902 	struct cdevsw *dsw;
1903 	struct file *fpop;
1904 
1905 	if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1906 		return (EINVAL);
1907 	fpop = td->td_fpop;
1908 	error = devfs_fp_check(fp, &dev, &dsw, &ref);
1909 	if (error != 0) {
1910 		error = vnops.fo_write(fp, uio, cred, flags, td);
1911 		return (error);
1912 	}
1913 	KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", uio->uio_td, td));
1914 	ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT | O_FSYNC);
1915 	if (ioflag & O_DIRECT)
1916 		ioflag |= IO_DIRECT;
1917 	foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1918 
1919 	resid = uio->uio_resid;
1920 
1921 	error = dsw->d_write(dev, uio, ioflag);
1922 	if (uio->uio_resid != resid || (error == 0 && resid != 0)) {
1923 		devfs_timestamp(&dev->si_ctime);
1924 		dev->si_mtime = dev->si_ctime;
1925 	}
1926 	td->td_fpop = fpop;
1927 	dev_relthread(dev, ref);
1928 
1929 	foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF_W);
1930 	return (error);
1931 }
1932 
1933 static int
1934 devfs_mmap_f(struct file *fp, vm_map_t map, vm_offset_t *addr, vm_size_t size,
1935     vm_prot_t prot, vm_prot_t cap_maxprot, int flags, vm_ooffset_t foff,
1936     struct thread *td)
1937 {
1938 	struct cdev *dev;
1939 	struct cdevsw *dsw;
1940 	struct mount *mp;
1941 	struct vnode *vp;
1942 	struct file *fpop;
1943 	vm_object_t object;
1944 	vm_prot_t maxprot;
1945 	int error, ref;
1946 
1947 	vp = fp->f_vnode;
1948 
1949 	/*
1950 	 * Ensure that file and memory protections are
1951 	 * compatible.
1952 	 */
1953 	mp = vp->v_mount;
1954 	if (mp != NULL && (mp->mnt_flag & MNT_NOEXEC) != 0) {
1955 		maxprot = VM_PROT_NONE;
1956 		if ((prot & VM_PROT_EXECUTE) != 0)
1957 			return (EACCES);
1958 	} else
1959 		maxprot = VM_PROT_EXECUTE;
1960 	if ((fp->f_flag & FREAD) != 0)
1961 		maxprot |= VM_PROT_READ;
1962 	else if ((prot & VM_PROT_READ) != 0)
1963 		return (EACCES);
1964 
1965 	/*
1966 	 * If we are sharing potential changes via MAP_SHARED and we
1967 	 * are trying to get write permission although we opened it
1968 	 * without asking for it, bail out.
1969 	 *
1970 	 * Note that most character devices always share mappings.
1971 	 * The one exception is that D_MMAP_ANON devices
1972 	 * (i.e. /dev/zero) permit private writable mappings.
1973 	 *
1974 	 * Rely on vm_mmap_cdev() to fail invalid MAP_PRIVATE requests
1975 	 * as well as updating maxprot to permit writing for
1976 	 * D_MMAP_ANON devices rather than doing that here.
1977 	 */
1978 	if ((flags & MAP_SHARED) != 0) {
1979 		if ((fp->f_flag & FWRITE) != 0)
1980 			maxprot |= VM_PROT_WRITE;
1981 		else if ((prot & VM_PROT_WRITE) != 0)
1982 			return (EACCES);
1983 	}
1984 	maxprot &= cap_maxprot;
1985 
1986 	fpop = td->td_fpop;
1987 	error = devfs_fp_check(fp, &dev, &dsw, &ref);
1988 	if (error != 0)
1989 		return (error);
1990 
1991 	error = vm_mmap_cdev(td, size, prot, &maxprot, &flags, dev, dsw, &foff,
1992 	    &object);
1993 	td->td_fpop = fpop;
1994 	dev_relthread(dev, ref);
1995 	if (error != 0)
1996 		return (error);
1997 
1998 	error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object,
1999 	    foff, FALSE, td);
2000 	if (error != 0)
2001 		vm_object_deallocate(object);
2002 	return (error);
2003 }
2004 
2005 dev_t
2006 dev2udev(struct cdev *x)
2007 {
2008 	if (x == NULL)
2009 		return (NODEV);
2010 	return (cdev2priv(x)->cdp_inode);
2011 }
2012 
2013 static struct fileops devfs_ops_f = {
2014 	.fo_read =	devfs_read_f,
2015 	.fo_write =	devfs_write_f,
2016 	.fo_truncate =	devfs_truncate_f,
2017 	.fo_ioctl =	devfs_ioctl_f,
2018 	.fo_poll =	devfs_poll_f,
2019 	.fo_kqfilter =	devfs_kqfilter_f,
2020 	.fo_stat =	devfs_stat_f,
2021 	.fo_close =	devfs_close_f,
2022 	.fo_chmod =	vn_chmod,
2023 	.fo_chown =	vn_chown,
2024 	.fo_sendfile =	vn_sendfile,
2025 	.fo_seek =	vn_seek,
2026 	.fo_fill_kinfo = vn_fill_kinfo,
2027 	.fo_mmap =	devfs_mmap_f,
2028 	.fo_flags =	DFLAG_PASSABLE | DFLAG_SEEKABLE
2029 };
2030 
2031 /* Vops for non-CHR vnodes in /dev. */
2032 static struct vop_vector devfs_vnodeops = {
2033 	.vop_default =		&default_vnodeops,
2034 
2035 	.vop_access =		devfs_access,
2036 	.vop_getattr =		devfs_getattr,
2037 	.vop_ioctl =		devfs_rioctl,
2038 	.vop_lookup =		devfs_lookup,
2039 	.vop_mknod =		devfs_mknod,
2040 	.vop_pathconf =		devfs_pathconf,
2041 	.vop_read =		devfs_rread,
2042 	.vop_readdir =		devfs_readdir,
2043 	.vop_readlink =		devfs_readlink,
2044 	.vop_reclaim =		devfs_reclaim,
2045 	.vop_remove =		devfs_remove,
2046 	.vop_revoke =		devfs_revoke,
2047 	.vop_setattr =		devfs_setattr,
2048 #ifdef MAC
2049 	.vop_setlabel =		devfs_setlabel,
2050 #endif
2051 	.vop_symlink =		devfs_symlink,
2052 	.vop_vptocnp =		devfs_vptocnp,
2053 	.vop_lock1 =		vop_lock,
2054 	.vop_unlock =		vop_unlock,
2055 	.vop_islocked =		vop_islocked,
2056 	.vop_add_writecount =	vop_stdadd_writecount_nomsync,
2057 };
2058 VFS_VOP_VECTOR_REGISTER(devfs_vnodeops);
2059 
2060 /* Vops for VCHR vnodes in /dev. */
2061 static struct vop_vector devfs_specops = {
2062 	.vop_default =		&default_vnodeops,
2063 
2064 	.vop_access =		devfs_access,
2065 	.vop_bmap =		VOP_PANIC,
2066 	.vop_close =		devfs_close,
2067 	.vop_create =		VOP_PANIC,
2068 	.vop_fsync =		vop_stdfsync,
2069 	.vop_getattr =		devfs_getattr,
2070 	.vop_ioctl =		devfs_ioctl,
2071 	.vop_link =		VOP_PANIC,
2072 	.vop_mkdir =		VOP_PANIC,
2073 	.vop_mknod =		VOP_PANIC,
2074 	.vop_open =		devfs_open,
2075 	.vop_pathconf =		devfs_pathconf,
2076 	.vop_poll =		dead_poll,
2077 	.vop_print =		devfs_print,
2078 	.vop_read =		dead_read,
2079 	.vop_readdir =		VOP_PANIC,
2080 	.vop_readlink =		VOP_PANIC,
2081 	.vop_reallocblks =	VOP_PANIC,
2082 	.vop_reclaim =		devfs_reclaim_vchr,
2083 	.vop_remove =		devfs_remove,
2084 	.vop_rename =		VOP_PANIC,
2085 	.vop_revoke =		devfs_revoke,
2086 	.vop_rmdir =		VOP_PANIC,
2087 	.vop_setattr =		devfs_setattr,
2088 #ifdef MAC
2089 	.vop_setlabel =		devfs_setlabel,
2090 #endif
2091 	.vop_strategy =		VOP_PANIC,
2092 	.vop_symlink =		VOP_PANIC,
2093 	.vop_vptocnp =		devfs_vptocnp,
2094 	.vop_write =		dead_write,
2095 	.vop_lock1 =		vop_lock,
2096 	.vop_unlock =		vop_unlock,
2097 	.vop_islocked =		vop_islocked,
2098 	.vop_add_writecount =	vop_stdadd_writecount_nomsync,
2099 };
2100 VFS_VOP_VECTOR_REGISTER(devfs_specops);
2101 
2102 /*
2103  * Our calling convention to the device drivers used to be that we passed
2104  * vnode.h IO_* flags to read()/write(), but we're moving to fcntl.h O_
2105  * flags instead since that's what open(), close() and ioctl() takes and
2106  * we don't really want vnode.h in device drivers.
2107  * We solved the source compatibility by redefining some vnode flags to
2108  * be the same as the fcntl ones and by sending down the bitwise OR of
2109  * the respective fcntl/vnode flags.  These CTASSERTS make sure nobody
2110  * pulls the rug out under this.
2111  */
2112 CTASSERT(O_NONBLOCK == IO_NDELAY);
2113 CTASSERT(O_FSYNC == IO_SYNC);
2114