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