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