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