xref: /freebsd/sys/fs/devfs/devfs_vnops.c (revision dbd5678dca91abcefe8d046aa2f9b66497a95ffb)
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 			return (EJUSTRETURN);
1144 		}
1145 		return (ENOENT);
1146 	}
1147 
1148 	if (devfs_prison_check(de, td))
1149 		return (ENOENT);
1150 
1151 	if ((cnp->cn_nameiop == DELETE) && (flags & ISLASTCN)) {
1152 		error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
1153 		if (error)
1154 			return (error);
1155 		if (*vpp == dvp) {
1156 			VREF(dvp);
1157 			*vpp = dvp;
1158 			return (0);
1159 		}
1160 	}
1161 	error = devfs_allocv(de, mp, cnp->cn_lkflags & LK_TYPE_MASK, vpp);
1162 	*dm_unlock = 0;
1163 	return (error);
1164 }
1165 
1166 static int
1167 devfs_lookup(struct vop_lookup_args *ap)
1168 {
1169 	int j;
1170 	struct devfs_mount *dmp;
1171 	int dm_unlock;
1172 
1173 	if (devfs_populate_vp(ap->a_dvp) != 0)
1174 		return (ENOTDIR);
1175 
1176 	dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1177 	dm_unlock = 1;
1178 	j = devfs_lookupx(ap, &dm_unlock);
1179 	if (dm_unlock == 1)
1180 		sx_xunlock(&dmp->dm_lock);
1181 	return (j);
1182 }
1183 
1184 static int
1185 devfs_mknod(struct vop_mknod_args *ap)
1186 {
1187 	struct componentname *cnp;
1188 	struct vnode *dvp, **vpp;
1189 	struct devfs_dirent *dd, *de;
1190 	struct devfs_mount *dmp;
1191 	int error;
1192 
1193 	/*
1194 	 * The only type of node we should be creating here is a
1195 	 * character device, for anything else return EOPNOTSUPP.
1196 	 */
1197 	if (ap->a_vap->va_type != VCHR)
1198 		return (EOPNOTSUPP);
1199 	dvp = ap->a_dvp;
1200 	dmp = VFSTODEVFS(dvp->v_mount);
1201 
1202 	cnp = ap->a_cnp;
1203 	vpp = ap->a_vpp;
1204 	dd = dvp->v_data;
1205 
1206 	error = ENOENT;
1207 	sx_xlock(&dmp->dm_lock);
1208 	TAILQ_FOREACH(de, &dd->de_dlist, de_list) {
1209 		if (cnp->cn_namelen != de->de_dirent->d_namlen)
1210 			continue;
1211 		if (de->de_dirent->d_type == DT_CHR &&
1212 		    (de->de_cdp->cdp_flags & CDP_ACTIVE) == 0)
1213 			continue;
1214 		if (bcmp(cnp->cn_nameptr, de->de_dirent->d_name,
1215 		    de->de_dirent->d_namlen) != 0)
1216 			continue;
1217 		if (de->de_flags & DE_WHITEOUT)
1218 			break;
1219 		goto notfound;
1220 	}
1221 	if (de == NULL)
1222 		goto notfound;
1223 	de->de_flags &= ~DE_WHITEOUT;
1224 	error = devfs_allocv(de, dvp->v_mount, LK_EXCLUSIVE, vpp);
1225 	return (error);
1226 notfound:
1227 	sx_xunlock(&dmp->dm_lock);
1228 	return (error);
1229 }
1230 
1231 /* ARGSUSED */
1232 static int
1233 devfs_open(struct vop_open_args *ap)
1234 {
1235 	struct thread *td = ap->a_td;
1236 	struct vnode *vp = ap->a_vp;
1237 	struct cdev *dev = vp->v_rdev;
1238 	struct file *fp = ap->a_fp;
1239 	int error, ref, vlocked;
1240 	struct cdevsw *dsw;
1241 	struct file *fpop;
1242 
1243 	if (vp->v_type == VBLK)
1244 		return (ENXIO);
1245 
1246 	if (dev == NULL)
1247 		return (ENXIO);
1248 
1249 	/* Make this field valid before any I/O in d_open. */
1250 	if (dev->si_iosize_max == 0)
1251 		dev->si_iosize_max = DFLTPHYS;
1252 
1253 	dsw = dev_refthread(dev, &ref);
1254 	if (dsw == NULL)
1255 		return (ENXIO);
1256 	if (fp == NULL && dsw->d_fdopen != NULL) {
1257 		dev_relthread(dev, ref);
1258 		return (ENXIO);
1259 	}
1260 
1261 	if (vp->v_type == VCHR)
1262 		devfs_usecount_add(vp);
1263 
1264 	vlocked = VOP_ISLOCKED(vp);
1265 	VOP_UNLOCK(vp);
1266 
1267 	fpop = td->td_fpop;
1268 	td->td_fpop = fp;
1269 	if (fp != NULL) {
1270 		fp->f_data = dev;
1271 		fp->f_vnode = vp;
1272 	}
1273 	if (dsw->d_fdopen != NULL)
1274 		error = dsw->d_fdopen(dev, ap->a_mode, td, fp);
1275 	else
1276 		error = dsw->d_open(dev, ap->a_mode, S_IFCHR, td);
1277 	/* Clean up any cdevpriv upon error. */
1278 	if (error != 0)
1279 		devfs_clear_cdevpriv();
1280 	td->td_fpop = fpop;
1281 
1282 	vn_lock(vp, vlocked | LK_RETRY);
1283 	if (error != 0 && vp->v_type == VCHR)
1284 		devfs_usecount_sub(vp);
1285 
1286 	dev_relthread(dev, ref);
1287 	if (error != 0) {
1288 		if (error == ERESTART)
1289 			error = EINTR;
1290 		return (error);
1291 	}
1292 
1293 #if 0	/* /dev/console */
1294 	KASSERT(fp != NULL, ("Could not vnode bypass device on NULL fp"));
1295 #else
1296 	if (fp == NULL)
1297 		return (error);
1298 #endif
1299 	if (fp->f_ops == &badfileops)
1300 		finit(fp, fp->f_flag, DTYPE_VNODE, dev, &devfs_ops_f);
1301 	return (error);
1302 }
1303 
1304 static int
1305 devfs_pathconf(struct vop_pathconf_args *ap)
1306 {
1307 
1308 	switch (ap->a_name) {
1309 	case _PC_FILESIZEBITS:
1310 		*ap->a_retval = 64;
1311 		return (0);
1312 	case _PC_NAME_MAX:
1313 		*ap->a_retval = NAME_MAX;
1314 		return (0);
1315 	case _PC_LINK_MAX:
1316 		*ap->a_retval = INT_MAX;
1317 		return (0);
1318 	case _PC_SYMLINK_MAX:
1319 		*ap->a_retval = MAXPATHLEN;
1320 		return (0);
1321 	case _PC_MAX_CANON:
1322 		if (ap->a_vp->v_vflag & VV_ISTTY) {
1323 			*ap->a_retval = MAX_CANON;
1324 			return (0);
1325 		}
1326 		return (EINVAL);
1327 	case _PC_MAX_INPUT:
1328 		if (ap->a_vp->v_vflag & VV_ISTTY) {
1329 			*ap->a_retval = MAX_INPUT;
1330 			return (0);
1331 		}
1332 		return (EINVAL);
1333 	case _PC_VDISABLE:
1334 		if (ap->a_vp->v_vflag & VV_ISTTY) {
1335 			*ap->a_retval = _POSIX_VDISABLE;
1336 			return (0);
1337 		}
1338 		return (EINVAL);
1339 	case _PC_MAC_PRESENT:
1340 #ifdef MAC
1341 		/*
1342 		 * If MAC is enabled, devfs automatically supports
1343 		 * trivial non-persistent label storage.
1344 		 */
1345 		*ap->a_retval = 1;
1346 #else
1347 		*ap->a_retval = 0;
1348 #endif
1349 		return (0);
1350 	case _PC_CHOWN_RESTRICTED:
1351 		*ap->a_retval = 1;
1352 		return (0);
1353 	default:
1354 		return (vop_stdpathconf(ap));
1355 	}
1356 	/* NOTREACHED */
1357 }
1358 
1359 /* ARGSUSED */
1360 static int
1361 devfs_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td)
1362 {
1363 	struct cdev *dev;
1364 	struct cdevsw *dsw;
1365 	int error, ref;
1366 	struct file *fpop;
1367 
1368 	fpop = td->td_fpop;
1369 	error = devfs_fp_check(fp, &dev, &dsw, &ref);
1370 	if (error != 0) {
1371 		error = vnops.fo_poll(fp, events, cred, td);
1372 		return (error);
1373 	}
1374 	error = dsw->d_poll(dev, events, td);
1375 	td->td_fpop = fpop;
1376 	dev_relthread(dev, ref);
1377 	return(error);
1378 }
1379 
1380 /*
1381  * Print out the contents of a special device vnode.
1382  */
1383 static int
1384 devfs_print(struct vop_print_args *ap)
1385 {
1386 
1387 	printf("\tdev %s\n", devtoname(ap->a_vp->v_rdev));
1388 	return (0);
1389 }
1390 
1391 static int
1392 devfs_read_f(struct file *fp, struct uio *uio, struct ucred *cred,
1393     int flags, struct thread *td)
1394 {
1395 	struct cdev *dev;
1396 	int ioflag, error, ref;
1397 	ssize_t resid;
1398 	struct cdevsw *dsw;
1399 	struct file *fpop;
1400 
1401 	if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1402 		return (EINVAL);
1403 	fpop = td->td_fpop;
1404 	error = devfs_fp_check(fp, &dev, &dsw, &ref);
1405 	if (error != 0) {
1406 		error = vnops.fo_read(fp, uio, cred, flags, td);
1407 		return (error);
1408 	}
1409 	resid = uio->uio_resid;
1410 	ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT);
1411 	if (ioflag & O_DIRECT)
1412 		ioflag |= IO_DIRECT;
1413 
1414 	foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1415 	error = dsw->d_read(dev, uio, ioflag);
1416 	if (uio->uio_resid != resid || (error == 0 && resid != 0))
1417 		devfs_timestamp(&dev->si_atime);
1418 	td->td_fpop = fpop;
1419 	dev_relthread(dev, ref);
1420 
1421 	foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF_R);
1422 	return (error);
1423 }
1424 
1425 static int
1426 devfs_readdir(struct vop_readdir_args *ap)
1427 {
1428 	int error;
1429 	struct uio *uio;
1430 	struct dirent *dp;
1431 	struct devfs_dirent *dd;
1432 	struct devfs_dirent *de;
1433 	struct devfs_mount *dmp;
1434 	off_t off;
1435 	int *tmp_ncookies = NULL;
1436 
1437 	if (ap->a_vp->v_type != VDIR)
1438 		return (ENOTDIR);
1439 
1440 	uio = ap->a_uio;
1441 	if (uio->uio_offset < 0)
1442 		return (EINVAL);
1443 
1444 	/*
1445 	 * XXX: This is a temporary hack to get around this filesystem not
1446 	 * supporting cookies. We store the location of the ncookies pointer
1447 	 * in a temporary variable before calling vfs_subr.c:vfs_read_dirent()
1448 	 * and set the number of cookies to 0. We then set the pointer to
1449 	 * NULL so that vfs_read_dirent doesn't try to call realloc() on
1450 	 * ap->a_cookies. Later in this function, we restore the ap->a_ncookies
1451 	 * pointer to its original location before returning to the caller.
1452 	 */
1453 	if (ap->a_ncookies != NULL) {
1454 		tmp_ncookies = ap->a_ncookies;
1455 		*ap->a_ncookies = 0;
1456 		ap->a_ncookies = NULL;
1457 	}
1458 
1459 	dmp = VFSTODEVFS(ap->a_vp->v_mount);
1460 	if (devfs_populate_vp(ap->a_vp) != 0) {
1461 		if (tmp_ncookies != NULL)
1462 			ap->a_ncookies = tmp_ncookies;
1463 		return (EIO);
1464 	}
1465 	error = 0;
1466 	de = ap->a_vp->v_data;
1467 	off = 0;
1468 	TAILQ_FOREACH(dd, &de->de_dlist, de_list) {
1469 		KASSERT(dd->de_cdp != (void *)0xdeadc0de, ("%s %d\n", __func__, __LINE__));
1470 		if (dd->de_flags & (DE_COVERED | DE_WHITEOUT))
1471 			continue;
1472 		if (devfs_prison_check(dd, uio->uio_td))
1473 			continue;
1474 		if (dd->de_dirent->d_type == DT_DIR)
1475 			de = dd->de_dir;
1476 		else
1477 			de = dd;
1478 		dp = dd->de_dirent;
1479 		MPASS(dp->d_reclen == GENERIC_DIRSIZ(dp));
1480 		if (dp->d_reclen > uio->uio_resid)
1481 			break;
1482 		dp->d_fileno = de->de_inode;
1483 		/* NOTE: d_off is the offset for the *next* entry. */
1484 		dp->d_off = off + dp->d_reclen;
1485 		if (off >= uio->uio_offset) {
1486 			error = vfs_read_dirent(ap, dp, off);
1487 			if (error)
1488 				break;
1489 		}
1490 		off += dp->d_reclen;
1491 	}
1492 	sx_xunlock(&dmp->dm_lock);
1493 	uio->uio_offset = off;
1494 
1495 	/*
1496 	 * Restore ap->a_ncookies if it wasn't originally NULL in the first
1497 	 * place.
1498 	 */
1499 	if (tmp_ncookies != NULL)
1500 		ap->a_ncookies = tmp_ncookies;
1501 
1502 	return (error);
1503 }
1504 
1505 static int
1506 devfs_readlink(struct vop_readlink_args *ap)
1507 {
1508 	struct devfs_dirent *de;
1509 
1510 	de = ap->a_vp->v_data;
1511 	return (uiomove(de->de_symlink, strlen(de->de_symlink), ap->a_uio));
1512 }
1513 
1514 static void
1515 devfs_reclaiml(struct vnode *vp)
1516 {
1517 	struct devfs_dirent *de;
1518 
1519 	mtx_assert(&devfs_de_interlock, MA_OWNED);
1520 	de = vp->v_data;
1521 	if (de != NULL) {
1522 		MPASS(de->de_usecount == 0);
1523 		de->de_vnode = NULL;
1524 		vp->v_data = NULL;
1525 	}
1526 }
1527 
1528 static int
1529 devfs_reclaim(struct vop_reclaim_args *ap)
1530 {
1531 	struct vnode *vp;
1532 
1533 	vp = ap->a_vp;
1534 	mtx_lock(&devfs_de_interlock);
1535 	devfs_reclaiml(vp);
1536 	mtx_unlock(&devfs_de_interlock);
1537 	return (0);
1538 }
1539 
1540 static int
1541 devfs_reclaim_vchr(struct vop_reclaim_args *ap)
1542 {
1543 	struct vnode *vp;
1544 	struct cdev *dev;
1545 
1546 	vp = ap->a_vp;
1547 	MPASS(vp->v_type == VCHR);
1548 
1549 	mtx_lock(&devfs_de_interlock);
1550 	VI_LOCK(vp);
1551 	devfs_usecount_subl(vp);
1552 	devfs_reclaiml(vp);
1553 	mtx_unlock(&devfs_de_interlock);
1554 	dev_lock();
1555 	dev = vp->v_rdev;
1556 	vp->v_rdev = NULL;
1557 	dev_unlock();
1558 	VI_UNLOCK(vp);
1559 	if (dev != NULL)
1560 		dev_rel(dev);
1561 	return (0);
1562 }
1563 
1564 static int
1565 devfs_remove(struct vop_remove_args *ap)
1566 {
1567 	struct vnode *dvp = ap->a_dvp;
1568 	struct vnode *vp = ap->a_vp;
1569 	struct devfs_dirent *dd;
1570 	struct devfs_dirent *de, *de_covered;
1571 	struct devfs_mount *dmp = VFSTODEVFS(vp->v_mount);
1572 
1573 	ASSERT_VOP_ELOCKED(dvp, "devfs_remove");
1574 	ASSERT_VOP_ELOCKED(vp, "devfs_remove");
1575 
1576 	sx_xlock(&dmp->dm_lock);
1577 	dd = ap->a_dvp->v_data;
1578 	de = vp->v_data;
1579 	if (de->de_cdp == NULL) {
1580 		TAILQ_REMOVE(&dd->de_dlist, de, de_list);
1581 		if (de->de_dirent->d_type == DT_LNK) {
1582 			de_covered = devfs_find(dd, de->de_dirent->d_name,
1583 			    de->de_dirent->d_namlen, 0);
1584 			if (de_covered != NULL)
1585 				de_covered->de_flags &= ~DE_COVERED;
1586 		}
1587 		/* We need to unlock dvp because devfs_delete() may lock it. */
1588 		VOP_UNLOCK(vp);
1589 		if (dvp != vp)
1590 			VOP_UNLOCK(dvp);
1591 		devfs_delete(dmp, de, 0);
1592 		sx_xunlock(&dmp->dm_lock);
1593 		if (dvp != vp)
1594 			vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1595 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1596 	} else {
1597 		de->de_flags |= DE_WHITEOUT;
1598 		sx_xunlock(&dmp->dm_lock);
1599 	}
1600 	return (0);
1601 }
1602 
1603 /*
1604  * Revoke is called on a tty when a terminal session ends.  The vnode
1605  * is orphaned by setting v_op to deadfs so we need to let go of it
1606  * as well so that we create a new one next time around.
1607  *
1608  */
1609 static int
1610 devfs_revoke(struct vop_revoke_args *ap)
1611 {
1612 	struct vnode *vp = ap->a_vp, *vp2;
1613 	struct cdev *dev;
1614 	struct cdev_priv *cdp;
1615 	struct devfs_dirent *de;
1616 	enum vgetstate vs;
1617 	u_int i;
1618 
1619 	KASSERT((ap->a_flags & REVOKEALL) != 0, ("devfs_revoke !REVOKEALL"));
1620 
1621 	dev = vp->v_rdev;
1622 	cdp = cdev2priv(dev);
1623 
1624 	dev_lock();
1625 	cdp->cdp_inuse++;
1626 	dev_unlock();
1627 
1628 	vhold(vp);
1629 	vgone(vp);
1630 	vdrop(vp);
1631 
1632 	VOP_UNLOCK(vp);
1633  loop:
1634 	for (;;) {
1635 		mtx_lock(&devfs_de_interlock);
1636 		dev_lock();
1637 		vp2 = NULL;
1638 		for (i = 0; i <= cdp->cdp_maxdirent; i++) {
1639 			de = cdp->cdp_dirents[i];
1640 			if (de == NULL)
1641 				continue;
1642 
1643 			vp2 = de->de_vnode;
1644 			if (vp2 != NULL) {
1645 				dev_unlock();
1646 				vs = vget_prep(vp2);
1647 				mtx_unlock(&devfs_de_interlock);
1648 				if (vget_finish(vp2, LK_EXCLUSIVE, vs) != 0)
1649 					goto loop;
1650 				vhold(vp2);
1651 				vgone(vp2);
1652 				vdrop(vp2);
1653 				vput(vp2);
1654 				break;
1655 			}
1656 		}
1657 		if (vp2 != NULL) {
1658 			continue;
1659 		}
1660 		dev_unlock();
1661 		mtx_unlock(&devfs_de_interlock);
1662 		break;
1663 	}
1664 	dev_lock();
1665 	cdp->cdp_inuse--;
1666 	if (!(cdp->cdp_flags & CDP_ACTIVE) && cdp->cdp_inuse == 0) {
1667 		TAILQ_REMOVE(&cdevp_list, cdp, cdp_list);
1668 		dev_unlock();
1669 		dev_rel(&cdp->cdp_c);
1670 	} else
1671 		dev_unlock();
1672 
1673 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1674 	return (0);
1675 }
1676 
1677 static int
1678 devfs_rioctl(struct vop_ioctl_args *ap)
1679 {
1680 	struct vnode *vp;
1681 	struct devfs_mount *dmp;
1682 	int error;
1683 
1684 	vp = ap->a_vp;
1685 	vn_lock(vp, LK_SHARED | LK_RETRY);
1686 	if (VN_IS_DOOMED(vp)) {
1687 		VOP_UNLOCK(vp);
1688 		return (EBADF);
1689 	}
1690 	dmp = VFSTODEVFS(vp->v_mount);
1691 	sx_xlock(&dmp->dm_lock);
1692 	VOP_UNLOCK(vp);
1693 	DEVFS_DMP_HOLD(dmp);
1694 	devfs_populate(dmp);
1695 	if (DEVFS_DMP_DROP(dmp)) {
1696 		sx_xunlock(&dmp->dm_lock);
1697 		devfs_unmount_final(dmp);
1698 		return (ENOENT);
1699 	}
1700 	error = devfs_rules_ioctl(dmp, ap->a_command, ap->a_data, ap->a_td);
1701 	sx_xunlock(&dmp->dm_lock);
1702 	return (error);
1703 }
1704 
1705 static int
1706 devfs_rread(struct vop_read_args *ap)
1707 {
1708 
1709 	if (ap->a_vp->v_type != VDIR)
1710 		return (EINVAL);
1711 	return (VOP_READDIR(ap->a_vp, ap->a_uio, ap->a_cred, NULL, NULL, NULL));
1712 }
1713 
1714 static int
1715 devfs_setattr(struct vop_setattr_args *ap)
1716 {
1717 	struct devfs_dirent *de;
1718 	struct vattr *vap;
1719 	struct vnode *vp;
1720 	struct thread *td;
1721 	int c, error;
1722 	uid_t uid;
1723 	gid_t gid;
1724 
1725 	vap = ap->a_vap;
1726 	vp = ap->a_vp;
1727 	td = curthread;
1728 	if ((vap->va_type != VNON) ||
1729 	    (vap->va_nlink != VNOVAL) ||
1730 	    (vap->va_fsid != VNOVAL) ||
1731 	    (vap->va_fileid != VNOVAL) ||
1732 	    (vap->va_blocksize != VNOVAL) ||
1733 	    (vap->va_flags != VNOVAL && vap->va_flags != 0) ||
1734 	    (vap->va_rdev != VNOVAL) ||
1735 	    ((int)vap->va_bytes != VNOVAL) ||
1736 	    (vap->va_gen != VNOVAL)) {
1737 		return (EINVAL);
1738 	}
1739 
1740 	error = devfs_populate_vp(vp);
1741 	if (error != 0)
1742 		return (error);
1743 
1744 	de = vp->v_data;
1745 	if (vp->v_type == VDIR)
1746 		de = de->de_dir;
1747 
1748 	c = 0;
1749 	if (vap->va_uid == (uid_t)VNOVAL)
1750 		uid = de->de_uid;
1751 	else
1752 		uid = vap->va_uid;
1753 	if (vap->va_gid == (gid_t)VNOVAL)
1754 		gid = de->de_gid;
1755 	else
1756 		gid = vap->va_gid;
1757 	if (uid != de->de_uid || gid != de->de_gid) {
1758 		if ((ap->a_cred->cr_uid != de->de_uid) || uid != de->de_uid ||
1759 		    (gid != de->de_gid && !groupmember(gid, ap->a_cred))) {
1760 			error = priv_check(td, PRIV_VFS_CHOWN);
1761 			if (error != 0)
1762 				goto ret;
1763 		}
1764 		de->de_uid = uid;
1765 		de->de_gid = gid;
1766 		c = 1;
1767 	}
1768 
1769 	if (vap->va_mode != (mode_t)VNOVAL) {
1770 		if (ap->a_cred->cr_uid != de->de_uid) {
1771 			error = priv_check(td, PRIV_VFS_ADMIN);
1772 			if (error != 0)
1773 				goto ret;
1774 		}
1775 		de->de_mode = vap->va_mode;
1776 		c = 1;
1777 	}
1778 
1779 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1780 		error = vn_utimes_perm(vp, vap, ap->a_cred, td);
1781 		if (error != 0)
1782 			goto ret;
1783 		if (vap->va_atime.tv_sec != VNOVAL) {
1784 			if (vp->v_type == VCHR)
1785 				vp->v_rdev->si_atime = vap->va_atime;
1786 			else
1787 				de->de_atime = vap->va_atime;
1788 		}
1789 		if (vap->va_mtime.tv_sec != VNOVAL) {
1790 			if (vp->v_type == VCHR)
1791 				vp->v_rdev->si_mtime = vap->va_mtime;
1792 			else
1793 				de->de_mtime = vap->va_mtime;
1794 		}
1795 		c = 1;
1796 	}
1797 
1798 	if (c) {
1799 		if (vp->v_type == VCHR)
1800 			vfs_timestamp(&vp->v_rdev->si_ctime);
1801 		else
1802 			vfs_timestamp(&de->de_mtime);
1803 	}
1804 
1805 ret:
1806 	sx_xunlock(&VFSTODEVFS(vp->v_mount)->dm_lock);
1807 	return (error);
1808 }
1809 
1810 #ifdef MAC
1811 static int
1812 devfs_setlabel(struct vop_setlabel_args *ap)
1813 {
1814 	struct vnode *vp;
1815 	struct devfs_dirent *de;
1816 
1817 	vp = ap->a_vp;
1818 	de = vp->v_data;
1819 
1820 	mac_vnode_relabel(ap->a_cred, vp, ap->a_label);
1821 	mac_devfs_update(vp->v_mount, de, vp);
1822 
1823 	return (0);
1824 }
1825 #endif
1826 
1827 static int
1828 devfs_stat_f(struct file *fp, struct stat *sb, struct ucred *cred)
1829 {
1830 
1831 	return (vnops.fo_stat(fp, sb, cred));
1832 }
1833 
1834 static int
1835 devfs_symlink(struct vop_symlink_args *ap)
1836 {
1837 	int i, error;
1838 	struct devfs_dirent *dd;
1839 	struct devfs_dirent *de, *de_covered, *de_dotdot;
1840 	struct devfs_mount *dmp;
1841 
1842 	error = priv_check(curthread, PRIV_DEVFS_SYMLINK);
1843 	if (error)
1844 		return(error);
1845 	dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1846 	if (devfs_populate_vp(ap->a_dvp) != 0)
1847 		return (ENOENT);
1848 
1849 	dd = ap->a_dvp->v_data;
1850 	de = devfs_newdirent(ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen);
1851 	de->de_flags = DE_USER;
1852 	de->de_uid = 0;
1853 	de->de_gid = 0;
1854 	de->de_mode = 0755;
1855 	de->de_inode = alloc_unr(devfs_inos);
1856 	de->de_dir = dd;
1857 	de->de_dirent->d_type = DT_LNK;
1858 	i = strlen(ap->a_target) + 1;
1859 	de->de_symlink = malloc(i, M_DEVFS, M_WAITOK);
1860 	bcopy(ap->a_target, de->de_symlink, i);
1861 #ifdef MAC
1862 	mac_devfs_create_symlink(ap->a_cnp->cn_cred, dmp->dm_mount, dd, de);
1863 #endif
1864 	de_covered = devfs_find(dd, de->de_dirent->d_name,
1865 	    de->de_dirent->d_namlen, 0);
1866 	if (de_covered != NULL) {
1867 		if ((de_covered->de_flags & DE_USER) != 0) {
1868 			devfs_delete(dmp, de, DEVFS_DEL_NORECURSE);
1869 			sx_xunlock(&dmp->dm_lock);
1870 			return (EEXIST);
1871 		}
1872 		KASSERT((de_covered->de_flags & DE_COVERED) == 0,
1873 		    ("devfs_symlink: entry %p already covered", de_covered));
1874 		de_covered->de_flags |= DE_COVERED;
1875 	}
1876 
1877 	de_dotdot = TAILQ_FIRST(&dd->de_dlist);		/* "." */
1878 	de_dotdot = TAILQ_NEXT(de_dotdot, de_list);	/* ".." */
1879 	TAILQ_INSERT_AFTER(&dd->de_dlist, de_dotdot, de, de_list);
1880 	devfs_dir_ref_de(dmp, dd);
1881 	devfs_rules_apply(dmp, de);
1882 
1883 	return (devfs_allocv(de, ap->a_dvp->v_mount, LK_EXCLUSIVE, ap->a_vpp));
1884 }
1885 
1886 static int
1887 devfs_truncate_f(struct file *fp, off_t length, struct ucred *cred, struct thread *td)
1888 {
1889 
1890 	return (vnops.fo_truncate(fp, length, cred, td));
1891 }
1892 
1893 static int
1894 devfs_write_f(struct file *fp, struct uio *uio, struct ucred *cred,
1895     int flags, struct thread *td)
1896 {
1897 	struct cdev *dev;
1898 	int error, ioflag, ref;
1899 	ssize_t resid;
1900 	struct cdevsw *dsw;
1901 	struct file *fpop;
1902 
1903 	if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1904 		return (EINVAL);
1905 	fpop = td->td_fpop;
1906 	error = devfs_fp_check(fp, &dev, &dsw, &ref);
1907 	if (error != 0) {
1908 		error = vnops.fo_write(fp, uio, cred, flags, td);
1909 		return (error);
1910 	}
1911 	KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", uio->uio_td, td));
1912 	ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT | O_FSYNC);
1913 	if (ioflag & O_DIRECT)
1914 		ioflag |= IO_DIRECT;
1915 	foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1916 
1917 	resid = uio->uio_resid;
1918 
1919 	error = dsw->d_write(dev, uio, ioflag);
1920 	if (uio->uio_resid != resid || (error == 0 && resid != 0)) {
1921 		devfs_timestamp(&dev->si_ctime);
1922 		dev->si_mtime = dev->si_ctime;
1923 	}
1924 	td->td_fpop = fpop;
1925 	dev_relthread(dev, ref);
1926 
1927 	foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF_W);
1928 	return (error);
1929 }
1930 
1931 static int
1932 devfs_mmap_f(struct file *fp, vm_map_t map, vm_offset_t *addr, vm_size_t size,
1933     vm_prot_t prot, vm_prot_t cap_maxprot, int flags, vm_ooffset_t foff,
1934     struct thread *td)
1935 {
1936 	struct cdev *dev;
1937 	struct cdevsw *dsw;
1938 	struct mount *mp;
1939 	struct vnode *vp;
1940 	struct file *fpop;
1941 	vm_object_t object;
1942 	vm_prot_t maxprot;
1943 	int error, ref;
1944 
1945 	vp = fp->f_vnode;
1946 
1947 	/*
1948 	 * Ensure that file and memory protections are
1949 	 * compatible.
1950 	 */
1951 	mp = vp->v_mount;
1952 	if (mp != NULL && (mp->mnt_flag & MNT_NOEXEC) != 0) {
1953 		maxprot = VM_PROT_NONE;
1954 		if ((prot & VM_PROT_EXECUTE) != 0)
1955 			return (EACCES);
1956 	} else
1957 		maxprot = VM_PROT_EXECUTE;
1958 	if ((fp->f_flag & FREAD) != 0)
1959 		maxprot |= VM_PROT_READ;
1960 	else if ((prot & VM_PROT_READ) != 0)
1961 		return (EACCES);
1962 
1963 	/*
1964 	 * If we are sharing potential changes via MAP_SHARED and we
1965 	 * are trying to get write permission although we opened it
1966 	 * without asking for it, bail out.
1967 	 *
1968 	 * Note that most character devices always share mappings.
1969 	 * The one exception is that D_MMAP_ANON devices
1970 	 * (i.e. /dev/zero) permit private writable mappings.
1971 	 *
1972 	 * Rely on vm_mmap_cdev() to fail invalid MAP_PRIVATE requests
1973 	 * as well as updating maxprot to permit writing for
1974 	 * D_MMAP_ANON devices rather than doing that here.
1975 	 */
1976 	if ((flags & MAP_SHARED) != 0) {
1977 		if ((fp->f_flag & FWRITE) != 0)
1978 			maxprot |= VM_PROT_WRITE;
1979 		else if ((prot & VM_PROT_WRITE) != 0)
1980 			return (EACCES);
1981 	}
1982 	maxprot &= cap_maxprot;
1983 
1984 	fpop = td->td_fpop;
1985 	error = devfs_fp_check(fp, &dev, &dsw, &ref);
1986 	if (error != 0)
1987 		return (error);
1988 
1989 	error = vm_mmap_cdev(td, size, prot, &maxprot, &flags, dev, dsw, &foff,
1990 	    &object);
1991 	td->td_fpop = fpop;
1992 	dev_relthread(dev, ref);
1993 	if (error != 0)
1994 		return (error);
1995 
1996 	error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object,
1997 	    foff, FALSE, td);
1998 	if (error != 0)
1999 		vm_object_deallocate(object);
2000 	return (error);
2001 }
2002 
2003 dev_t
2004 dev2udev(struct cdev *x)
2005 {
2006 	if (x == NULL)
2007 		return (NODEV);
2008 	return (cdev2priv(x)->cdp_inode);
2009 }
2010 
2011 static struct fileops devfs_ops_f = {
2012 	.fo_read =	devfs_read_f,
2013 	.fo_write =	devfs_write_f,
2014 	.fo_truncate =	devfs_truncate_f,
2015 	.fo_ioctl =	devfs_ioctl_f,
2016 	.fo_poll =	devfs_poll_f,
2017 	.fo_kqfilter =	devfs_kqfilter_f,
2018 	.fo_stat =	devfs_stat_f,
2019 	.fo_close =	devfs_close_f,
2020 	.fo_chmod =	vn_chmod,
2021 	.fo_chown =	vn_chown,
2022 	.fo_sendfile =	vn_sendfile,
2023 	.fo_seek =	vn_seek,
2024 	.fo_fill_kinfo = vn_fill_kinfo,
2025 	.fo_mmap =	devfs_mmap_f,
2026 	.fo_flags =	DFLAG_PASSABLE | DFLAG_SEEKABLE
2027 };
2028 
2029 /* Vops for non-CHR vnodes in /dev. */
2030 static struct vop_vector devfs_vnodeops = {
2031 	.vop_default =		&default_vnodeops,
2032 
2033 	.vop_access =		devfs_access,
2034 	.vop_getattr =		devfs_getattr,
2035 	.vop_ioctl =		devfs_rioctl,
2036 	.vop_lookup =		devfs_lookup,
2037 	.vop_mknod =		devfs_mknod,
2038 	.vop_pathconf =		devfs_pathconf,
2039 	.vop_read =		devfs_rread,
2040 	.vop_readdir =		devfs_readdir,
2041 	.vop_readlink =		devfs_readlink,
2042 	.vop_reclaim =		devfs_reclaim,
2043 	.vop_remove =		devfs_remove,
2044 	.vop_revoke =		devfs_revoke,
2045 	.vop_setattr =		devfs_setattr,
2046 #ifdef MAC
2047 	.vop_setlabel =		devfs_setlabel,
2048 #endif
2049 	.vop_symlink =		devfs_symlink,
2050 	.vop_vptocnp =		devfs_vptocnp,
2051 	.vop_lock1 =		vop_lock,
2052 	.vop_unlock =		vop_unlock,
2053 	.vop_islocked =		vop_islocked,
2054 	.vop_add_writecount =	vop_stdadd_writecount_nomsync,
2055 };
2056 VFS_VOP_VECTOR_REGISTER(devfs_vnodeops);
2057 
2058 /* Vops for VCHR vnodes in /dev. */
2059 static struct vop_vector devfs_specops = {
2060 	.vop_default =		&default_vnodeops,
2061 
2062 	.vop_access =		devfs_access,
2063 	.vop_bmap =		VOP_PANIC,
2064 	.vop_close =		devfs_close,
2065 	.vop_create =		VOP_PANIC,
2066 	.vop_fsync =		vop_stdfsync,
2067 	.vop_getattr =		devfs_getattr,
2068 	.vop_ioctl =		devfs_ioctl,
2069 	.vop_link =		VOP_PANIC,
2070 	.vop_mkdir =		VOP_PANIC,
2071 	.vop_mknod =		VOP_PANIC,
2072 	.vop_open =		devfs_open,
2073 	.vop_pathconf =		devfs_pathconf,
2074 	.vop_poll =		dead_poll,
2075 	.vop_print =		devfs_print,
2076 	.vop_read =		dead_read,
2077 	.vop_readdir =		VOP_PANIC,
2078 	.vop_readlink =		VOP_PANIC,
2079 	.vop_reallocblks =	VOP_PANIC,
2080 	.vop_reclaim =		devfs_reclaim_vchr,
2081 	.vop_remove =		devfs_remove,
2082 	.vop_rename =		VOP_PANIC,
2083 	.vop_revoke =		devfs_revoke,
2084 	.vop_rmdir =		VOP_PANIC,
2085 	.vop_setattr =		devfs_setattr,
2086 #ifdef MAC
2087 	.vop_setlabel =		devfs_setlabel,
2088 #endif
2089 	.vop_strategy =		VOP_PANIC,
2090 	.vop_symlink =		VOP_PANIC,
2091 	.vop_vptocnp =		devfs_vptocnp,
2092 	.vop_write =		dead_write,
2093 	.vop_lock1 =		vop_lock,
2094 	.vop_unlock =		vop_unlock,
2095 	.vop_islocked =		vop_islocked,
2096 	.vop_add_writecount =	vop_stdadd_writecount_nomsync,
2097 };
2098 VFS_VOP_VECTOR_REGISTER(devfs_specops);
2099 
2100 /*
2101  * Our calling convention to the device drivers used to be that we passed
2102  * vnode.h IO_* flags to read()/write(), but we're moving to fcntl.h O_
2103  * flags instead since that's what open(), close() and ioctl() takes and
2104  * we don't really want vnode.h in device drivers.
2105  * We solved the source compatibility by redefining some vnode flags to
2106  * be the same as the fcntl ones and by sending down the bitwise OR of
2107  * the respective fcntl/vnode flags.  These CTASSERTS make sure nobody
2108  * pulls the rug out under this.
2109  */
2110 CTASSERT(O_NONBLOCK == IO_NDELAY);
2111 CTASSERT(O_FSYNC == IO_SYNC);
2112