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