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