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