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