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