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