xref: /freebsd/sys/fs/devfs/devfs_vnops.c (revision bc093719ca478fe10b938cef32c30b528042cbcd)
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_mode, 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 
470 	curthread->td_fpop = fp;
471 	error = vnops.fo_close(fp, td);
472 	curthread->td_fpop = NULL;
473 	return (error);
474 }
475 
476 /* ARGSUSED */
477 static int
478 devfs_fsync(struct vop_fsync_args *ap)
479 {
480 	if (!vn_isdisk(ap->a_vp, NULL))
481 		return (0);
482 
483 	return (vop_stdfsync(ap));
484 }
485 
486 static int
487 devfs_getattr(struct vop_getattr_args *ap)
488 {
489 	struct vnode *vp = ap->a_vp;
490 	struct vattr *vap = ap->a_vap;
491 	int error = 0;
492 	struct devfs_dirent *de;
493 	struct cdev *dev;
494 
495 	de = vp->v_data;
496 	KASSERT(de != NULL, ("Null dirent in devfs_getattr vp=%p", vp));
497 	if (vp->v_type == VDIR) {
498 		de = de->de_dir;
499 		KASSERT(de != NULL,
500 		    ("Null dir dirent in devfs_getattr vp=%p", vp));
501 	}
502 	bzero((caddr_t) vap, sizeof(*vap));
503 	vattr_null(vap);
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_nlink = de->de_links;
547 	vap->va_fileid = de->de_inode;
548 
549 	return (error);
550 }
551 
552 /* ARGSUSED */
553 static int
554 devfs_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred, struct thread *td)
555 {
556 	struct cdev *dev;
557 	struct cdevsw *dsw;
558 	struct vnode *vp;
559 	struct vnode *vpold;
560 	int error, i;
561 	const char *p;
562 	struct fiodgname_arg *fgn;
563 
564 	error = devfs_fp_check(fp, &dev, &dsw);
565 	if (error)
566 		return (error);
567 
568 	if (com == FIODTYPE) {
569 		*(int *)data = dsw->d_flags & D_TYPEMASK;
570 		td->td_fpop = NULL;
571 		dev_relthread(dev);
572 		return (0);
573 	} else if (com == FIODGNAME) {
574 		fgn = data;
575 		p = devtoname(dev);
576 		i = strlen(p) + 1;
577 		if (i > fgn->len)
578 			error = EINVAL;
579 		else
580 			error = copyout(p, fgn->buf, i);
581 		td->td_fpop = NULL;
582 		dev_relthread(dev);
583 		return (error);
584 	}
585 	error = dsw->d_ioctl(dev, com, data, fp->f_flag, td);
586 	td->td_fpop = NULL;
587 	dev_relthread(dev);
588 	if (error == ENOIOCTL)
589 		error = ENOTTY;
590 	if (error == 0 && com == TIOCSCTTY) {
591 		vp = fp->f_vnode;
592 
593 		/* Do nothing if reassigning same control tty */
594 		sx_slock(&proctree_lock);
595 		if (td->td_proc->p_session->s_ttyvp == vp) {
596 			sx_sunlock(&proctree_lock);
597 			return (0);
598 		}
599 
600 		vpold = td->td_proc->p_session->s_ttyvp;
601 		VREF(vp);
602 		SESS_LOCK(td->td_proc->p_session);
603 		td->td_proc->p_session->s_ttyvp = vp;
604 		SESS_UNLOCK(td->td_proc->p_session);
605 
606 		sx_sunlock(&proctree_lock);
607 
608 		/* Get rid of reference to old control tty */
609 		if (vpold)
610 			vrele(vpold);
611 	}
612 	return (error);
613 }
614 
615 /* ARGSUSED */
616 static int
617 devfs_kqfilter_f(struct file *fp, struct knote *kn)
618 {
619 	struct cdev *dev;
620 	struct cdevsw *dsw;
621 	int error;
622 
623 	error = devfs_fp_check(fp, &dev, &dsw);
624 	if (error)
625 		return (error);
626 	error = dsw->d_kqfilter(dev, kn);
627 	curthread->td_fpop = NULL;
628 	dev_relthread(dev);
629 	return (error);
630 }
631 
632 static int
633 devfs_lookupx(struct vop_lookup_args *ap, int *dm_unlock)
634 {
635 	struct componentname *cnp;
636 	struct vnode *dvp, **vpp;
637 	struct thread *td;
638 	struct devfs_dirent *de, *dd;
639 	struct devfs_dirent **dde;
640 	struct devfs_mount *dmp;
641 	struct cdev *cdev;
642 	int error, flags, nameiop;
643 	char specname[SPECNAMELEN + 1], *pname;
644 
645 	cnp = ap->a_cnp;
646 	vpp = ap->a_vpp;
647 	dvp = ap->a_dvp;
648 	pname = cnp->cn_nameptr;
649 	td = cnp->cn_thread;
650 	flags = cnp->cn_flags;
651 	nameiop = cnp->cn_nameiop;
652 	dmp = VFSTODEVFS(dvp->v_mount);
653 	dd = dvp->v_data;
654 	*vpp = NULLVP;
655 
656 	if ((flags & ISLASTCN) && nameiop == RENAME)
657 		return (EOPNOTSUPP);
658 
659 	if (dvp->v_type != VDIR)
660 		return (ENOTDIR);
661 
662 	if ((flags & ISDOTDOT) && (dvp->v_vflag & VV_ROOT))
663 		return (EIO);
664 
665 	error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td);
666 	if (error)
667 		return (error);
668 
669 	if (cnp->cn_namelen == 1 && *pname == '.') {
670 		if ((flags & ISLASTCN) && nameiop != LOOKUP)
671 			return (EINVAL);
672 		*vpp = dvp;
673 		VREF(dvp);
674 		return (0);
675 	}
676 
677 	if (flags & ISDOTDOT) {
678 		if ((flags & ISLASTCN) && nameiop != LOOKUP)
679 			return (EINVAL);
680 		VOP_UNLOCK(dvp, 0);
681 		de = TAILQ_FIRST(&dd->de_dlist);	/* "." */
682 		de = TAILQ_NEXT(de, de_list);		/* ".." */
683 		de = de->de_dir;
684 		error = devfs_allocv(de, dvp->v_mount, vpp, td);
685 		*dm_unlock = 0;
686 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
687 		return (error);
688 	}
689 
690 	DEVFS_DMP_HOLD(dmp);
691 	devfs_populate(dmp);
692 	if (DEVFS_DMP_DROP(dmp)) {
693 		*dm_unlock = 0;
694 		sx_xunlock(&dmp->dm_lock);
695 		devfs_unmount_final(dmp);
696 		return (ENOENT);
697 	}
698 	dd = dvp->v_data;
699 	de = devfs_find(dd, cnp->cn_nameptr, cnp->cn_namelen);
700 	while (de == NULL) {	/* While(...) so we can use break */
701 
702 		if (nameiop == DELETE)
703 			return (ENOENT);
704 
705 		/*
706 		 * OK, we didn't have an entry for the name we were asked for
707 		 * so we try to see if anybody can create it on demand.
708 		 */
709 		pname = devfs_fqpn(specname, dvp, cnp);
710 		if (pname == NULL)
711 			break;
712 
713 		cdev = NULL;
714 		DEVFS_DMP_HOLD(dmp);
715 		sx_xunlock(&dmp->dm_lock);
716 		sx_slock(&clone_drain_lock);
717 		EVENTHANDLER_INVOKE(dev_clone,
718 		    td->td_ucred, pname, strlen(pname), &cdev);
719 		sx_sunlock(&clone_drain_lock);
720 		sx_xlock(&dmp->dm_lock);
721 		if (DEVFS_DMP_DROP(dmp)) {
722 			*dm_unlock = 0;
723 			sx_xunlock(&dmp->dm_lock);
724 			devfs_unmount_final(dmp);
725 			return (ENOENT);
726 		}
727 		if (cdev == NULL)
728 			break;
729 
730 		DEVFS_DMP_HOLD(dmp);
731 		devfs_populate(dmp);
732 		if (DEVFS_DMP_DROP(dmp)) {
733 			*dm_unlock = 0;
734 			sx_xunlock(&dmp->dm_lock);
735 			devfs_unmount_final(dmp);
736 			return (ENOENT);
737 		}
738 
739 		dev_lock();
740 		dde = &cdev2priv(cdev)->cdp_dirents[dmp->dm_idx];
741 		if (dde != NULL && *dde != NULL)
742 			de = *dde;
743 		dev_unlock();
744 		dev_rel(cdev);
745 		break;
746 	}
747 
748 	if (de == NULL || de->de_flags & DE_WHITEOUT) {
749 		if ((nameiop == CREATE || nameiop == RENAME) &&
750 		    (flags & (LOCKPARENT | WANTPARENT)) && (flags & ISLASTCN)) {
751 			cnp->cn_flags |= SAVENAME;
752 			return (EJUSTRETURN);
753 		}
754 		return (ENOENT);
755 	}
756 
757 	if ((cnp->cn_nameiop == DELETE) && (flags & ISLASTCN)) {
758 		error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
759 		if (error)
760 			return (error);
761 		if (*vpp == dvp) {
762 			VREF(dvp);
763 			*vpp = dvp;
764 			return (0);
765 		}
766 	}
767 	error = devfs_allocv(de, dvp->v_mount, vpp, td);
768 	*dm_unlock = 0;
769 	return (error);
770 }
771 
772 static int
773 devfs_lookup(struct vop_lookup_args *ap)
774 {
775 	int j;
776 	struct devfs_mount *dmp;
777 	int dm_unlock;
778 
779 	dmp = VFSTODEVFS(ap->a_dvp->v_mount);
780 	dm_unlock = 1;
781 	sx_xlock(&dmp->dm_lock);
782 	j = devfs_lookupx(ap, &dm_unlock);
783 	if (dm_unlock == 1)
784 		sx_xunlock(&dmp->dm_lock);
785 	return (j);
786 }
787 
788 static int
789 devfs_mknod(struct vop_mknod_args *ap)
790 {
791 	struct componentname *cnp;
792 	struct vnode *dvp, **vpp;
793 	struct thread *td;
794 	struct devfs_dirent *dd, *de;
795 	struct devfs_mount *dmp;
796 	int error;
797 
798 	/*
799 	 * The only type of node we should be creating here is a
800 	 * character device, for anything else return EOPNOTSUPP.
801 	 */
802 	if (ap->a_vap->va_type != VCHR)
803 		return (EOPNOTSUPP);
804 	dvp = ap->a_dvp;
805 	dmp = VFSTODEVFS(dvp->v_mount);
806 
807 	cnp = ap->a_cnp;
808 	vpp = ap->a_vpp;
809 	td = cnp->cn_thread;
810 	dd = dvp->v_data;
811 
812 	error = ENOENT;
813 	sx_xlock(&dmp->dm_lock);
814 	TAILQ_FOREACH(de, &dd->de_dlist, de_list) {
815 		if (cnp->cn_namelen != de->de_dirent->d_namlen)
816 			continue;
817 		if (bcmp(cnp->cn_nameptr, de->de_dirent->d_name,
818 		    de->de_dirent->d_namlen) != 0)
819 			continue;
820 		if (de->de_flags & DE_WHITEOUT)
821 			break;
822 		goto notfound;
823 	}
824 	if (de == NULL)
825 		goto notfound;
826 	de->de_flags &= ~DE_WHITEOUT;
827 	error = devfs_allocv(de, dvp->v_mount, vpp, td);
828 	return (error);
829 notfound:
830 	sx_xunlock(&dmp->dm_lock);
831 	return (error);
832 }
833 
834 /* ARGSUSED */
835 static int
836 devfs_open(struct vop_open_args *ap)
837 {
838 	struct thread *td = ap->a_td;
839 	struct vnode *vp = ap->a_vp;
840 	struct cdev *dev = vp->v_rdev;
841 	struct file *fp = ap->a_fp;
842 	int error;
843 	struct cdevsw *dsw;
844 
845 	if (vp->v_type == VBLK)
846 		return (ENXIO);
847 
848 	if (dev == NULL)
849 		return (ENXIO);
850 
851 	/* Make this field valid before any I/O in d_open. */
852 	if (dev->si_iosize_max == 0)
853 		dev->si_iosize_max = DFLTPHYS;
854 
855 	dsw = dev_refthread(dev);
856 	if (dsw == NULL)
857 		return (ENXIO);
858 
859 	/* XXX: Special casing of ttys for deadfs.  Probably redundant. */
860 	if (dsw->d_flags & D_TTY)
861 		vp->v_vflag |= VV_ISTTY;
862 
863 	VOP_UNLOCK(vp, 0);
864 
865 	if (fp != NULL) {
866 		td->td_fpop = fp;
867 		fp->f_data = dev;
868 	}
869 	if (dsw->d_fdopen != NULL)
870 		error = dsw->d_fdopen(dev, ap->a_mode, td, fp);
871 	else
872 		error = dsw->d_open(dev, ap->a_mode, S_IFCHR, td);
873 	td->td_fpop = NULL;
874 
875 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
876 
877 	dev_relthread(dev);
878 
879 	if (error)
880 		return (error);
881 
882 #if 0	/* /dev/console */
883 	KASSERT(fp != NULL,
884 	     ("Could not vnode bypass device on NULL fp"));
885 #else
886 	if(fp == NULL)
887 		return (error);
888 #endif
889 	if (fp->f_ops == &badfileops)
890 		finit(fp, fp->f_flag, DTYPE_VNODE, dev, &devfs_ops_f);
891 	return (error);
892 }
893 
894 static int
895 devfs_pathconf(struct vop_pathconf_args *ap)
896 {
897 
898 	switch (ap->a_name) {
899 	case _PC_MAC_PRESENT:
900 #ifdef MAC
901 		/*
902 		 * If MAC is enabled, devfs automatically supports
903 		 * trivial non-persistant label storage.
904 		 */
905 		*ap->a_retval = 1;
906 #else
907 		*ap->a_retval = 0;
908 #endif
909 		return (0);
910 	default:
911 		return (vop_stdpathconf(ap));
912 	}
913 	/* NOTREACHED */
914 }
915 
916 /* ARGSUSED */
917 static int
918 devfs_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td)
919 {
920 	struct cdev *dev;
921 	struct cdevsw *dsw;
922 	int error;
923 
924 	error = devfs_fp_check(fp, &dev, &dsw);
925 	if (error)
926 		return (error);
927 	error = dsw->d_poll(dev, events, td);
928 	curthread->td_fpop = NULL;
929 	dev_relthread(dev);
930 	return(error);
931 }
932 
933 /*
934  * Print out the contents of a special device vnode.
935  */
936 static int
937 devfs_print(struct vop_print_args *ap)
938 {
939 
940 	printf("\tdev %s\n", devtoname(ap->a_vp->v_rdev));
941 	return (0);
942 }
943 
944 /* ARGSUSED */
945 static int
946 devfs_read_f(struct file *fp, struct uio *uio, struct ucred *cred, int flags, struct thread *td)
947 {
948 	struct cdev *dev;
949 	int ioflag, error, resid;
950 	struct cdevsw *dsw;
951 
952 	error = devfs_fp_check(fp, &dev, &dsw);
953 	if (error)
954 		return (error);
955 	resid = uio->uio_resid;
956 	ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT);
957 	if (ioflag & O_DIRECT)
958 		ioflag |= IO_DIRECT;
959 
960 	if ((flags & FOF_OFFSET) == 0)
961 		uio->uio_offset = fp->f_offset;
962 
963 	error = dsw->d_read(dev, uio, ioflag);
964 	if (uio->uio_resid != resid || (error == 0 && resid != 0))
965 		vfs_timestamp(&dev->si_atime);
966 	curthread->td_fpop = NULL;
967 	dev_relthread(dev);
968 
969 	if ((flags & FOF_OFFSET) == 0)
970 		fp->f_offset = uio->uio_offset;
971 	fp->f_nextoff = uio->uio_offset;
972 	return (error);
973 }
974 
975 static int
976 devfs_readdir(struct vop_readdir_args *ap)
977 {
978 	int error;
979 	struct uio *uio;
980 	struct dirent *dp;
981 	struct devfs_dirent *dd;
982 	struct devfs_dirent *de;
983 	struct devfs_mount *dmp;
984 	off_t off, oldoff;
985 	int *tmp_ncookies = NULL;
986 
987 	if (ap->a_vp->v_type != VDIR)
988 		return (ENOTDIR);
989 
990 	uio = ap->a_uio;
991 	if (uio->uio_offset < 0)
992 		return (EINVAL);
993 
994 	/*
995 	 * XXX: This is a temporary hack to get around this filesystem not
996 	 * supporting cookies. We store the location of the ncookies pointer
997 	 * in a temporary variable before calling vfs_subr.c:vfs_read_dirent()
998 	 * and set the number of cookies to 0. We then set the pointer to
999 	 * NULL so that vfs_read_dirent doesn't try to call realloc() on
1000 	 * ap->a_cookies. Later in this function, we restore the ap->a_ncookies
1001 	 * pointer to its original location before returning to the caller.
1002 	 */
1003 	if (ap->a_ncookies != NULL) {
1004 		tmp_ncookies = ap->a_ncookies;
1005 		*ap->a_ncookies = 0;
1006 		ap->a_ncookies = NULL;
1007 	}
1008 
1009 	dmp = VFSTODEVFS(ap->a_vp->v_mount);
1010 	sx_xlock(&dmp->dm_lock);
1011 	DEVFS_DMP_HOLD(dmp);
1012 	devfs_populate(dmp);
1013 	if (DEVFS_DMP_DROP(dmp)) {
1014 		sx_xunlock(&dmp->dm_lock);
1015 		devfs_unmount_final(dmp);
1016 		if (tmp_ncookies != NULL)
1017 			ap->a_ncookies = tmp_ncookies;
1018 		return (EIO);
1019 	}
1020 	error = 0;
1021 	de = ap->a_vp->v_data;
1022 	off = 0;
1023 	oldoff = uio->uio_offset;
1024 	TAILQ_FOREACH(dd, &de->de_dlist, de_list) {
1025 		KASSERT(dd->de_cdp != (void *)0xdeadc0de, ("%s %d\n", __func__, __LINE__));
1026 		if (dd->de_flags & DE_WHITEOUT)
1027 			continue;
1028 		if (dd->de_dirent->d_type == DT_DIR)
1029 			de = dd->de_dir;
1030 		else
1031 			de = dd;
1032 		dp = dd->de_dirent;
1033 		if (dp->d_reclen > uio->uio_resid)
1034 			break;
1035 		dp->d_fileno = de->de_inode;
1036 		if (off >= uio->uio_offset) {
1037 			error = vfs_read_dirent(ap, dp, off);
1038 			if (error)
1039 				break;
1040 		}
1041 		off += dp->d_reclen;
1042 	}
1043 	sx_xunlock(&dmp->dm_lock);
1044 	uio->uio_offset = off;
1045 
1046 	/*
1047 	 * Restore ap->a_ncookies if it wasn't originally NULL in the first
1048 	 * place.
1049 	 */
1050 	if (tmp_ncookies != NULL)
1051 		ap->a_ncookies = tmp_ncookies;
1052 
1053 	return (error);
1054 }
1055 
1056 static int
1057 devfs_readlink(struct vop_readlink_args *ap)
1058 {
1059 	struct devfs_dirent *de;
1060 
1061 	de = ap->a_vp->v_data;
1062 	return (uiomove(de->de_symlink, strlen(de->de_symlink), ap->a_uio));
1063 }
1064 
1065 static int
1066 devfs_reclaim(struct vop_reclaim_args *ap)
1067 {
1068 	struct vnode *vp = ap->a_vp;
1069 	struct devfs_dirent *de;
1070 	struct cdev *dev;
1071 
1072 	mtx_lock(&devfs_de_interlock);
1073 	de = vp->v_data;
1074 	if (de != NULL) {
1075 		de->de_vnode = NULL;
1076 		vp->v_data = NULL;
1077 	}
1078 	mtx_unlock(&devfs_de_interlock);
1079 
1080 	vnode_destroy_vobject(vp);
1081 
1082 	VI_LOCK(vp);
1083 	dev_lock();
1084 	dev = vp->v_rdev;
1085 	vp->v_rdev = NULL;
1086 
1087 	if (dev == NULL) {
1088 		dev_unlock();
1089 		VI_UNLOCK(vp);
1090 		return (0);
1091 	}
1092 
1093 	dev->si_usecount -= vp->v_usecount;
1094 	dev_unlock();
1095 	VI_UNLOCK(vp);
1096 	dev_rel(dev);
1097 	return (0);
1098 }
1099 
1100 static int
1101 devfs_remove(struct vop_remove_args *ap)
1102 {
1103 	struct vnode *vp = ap->a_vp;
1104 	struct devfs_dirent *dd;
1105 	struct devfs_dirent *de;
1106 	struct devfs_mount *dmp = VFSTODEVFS(vp->v_mount);
1107 
1108 	sx_xlock(&dmp->dm_lock);
1109 	dd = ap->a_dvp->v_data;
1110 	de = vp->v_data;
1111 	if (de->de_cdp == NULL) {
1112 		TAILQ_REMOVE(&dd->de_dlist, de, de_list);
1113 		devfs_delete(dmp, de, 1);
1114 	} else {
1115 		de->de_flags |= DE_WHITEOUT;
1116 	}
1117 	sx_xunlock(&dmp->dm_lock);
1118 	return (0);
1119 }
1120 
1121 /*
1122  * Revoke is called on a tty when a terminal session ends.  The vnode
1123  * is orphaned by setting v_op to deadfs so we need to let go of it
1124  * as well so that we create a new one next time around.
1125  *
1126  */
1127 static int
1128 devfs_revoke(struct vop_revoke_args *ap)
1129 {
1130 	struct vnode *vp = ap->a_vp, *vp2;
1131 	struct cdev *dev;
1132 	struct cdev_priv *cdp;
1133 	struct devfs_dirent *de;
1134 	int i;
1135 
1136 	KASSERT((ap->a_flags & REVOKEALL) != 0, ("devfs_revoke !REVOKEALL"));
1137 
1138 	dev = vp->v_rdev;
1139 	cdp = cdev2priv(dev);
1140 
1141 	dev_lock();
1142 	cdp->cdp_inuse++;
1143 	dev_unlock();
1144 
1145 	vhold(vp);
1146 	vgone(vp);
1147 	vdrop(vp);
1148 
1149 	VOP_UNLOCK(vp,0);
1150  loop:
1151 	for (;;) {
1152 		mtx_lock(&devfs_de_interlock);
1153 		dev_lock();
1154 		vp2 = NULL;
1155 		for (i = 0; i <= cdp->cdp_maxdirent; i++) {
1156 			de = cdp->cdp_dirents[i];
1157 			if (de == NULL)
1158 				continue;
1159 
1160 			vp2 = de->de_vnode;
1161 			if (vp2 != NULL) {
1162 				dev_unlock();
1163 				VI_LOCK(vp2);
1164 				mtx_unlock(&devfs_de_interlock);
1165 				if (vget(vp2, LK_EXCLUSIVE | LK_INTERLOCK,
1166 				    curthread))
1167 					goto loop;
1168 				vhold(vp2);
1169 				vgone(vp2);
1170 				vdrop(vp2);
1171 				vput(vp2);
1172 				break;
1173 			}
1174 		}
1175 		if (vp2 != NULL) {
1176 			continue;
1177 		}
1178 		dev_unlock();
1179 		mtx_unlock(&devfs_de_interlock);
1180 		break;
1181 	}
1182 	dev_lock();
1183 	cdp->cdp_inuse--;
1184 	if (!(cdp->cdp_flags & CDP_ACTIVE) && cdp->cdp_inuse == 0) {
1185 		TAILQ_REMOVE(&cdevp_list, cdp, cdp_list);
1186 		dev_unlock();
1187 		dev_rel(&cdp->cdp_c);
1188 	} else
1189 		dev_unlock();
1190 
1191 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1192 	return (0);
1193 }
1194 
1195 static int
1196 devfs_rioctl(struct vop_ioctl_args *ap)
1197 {
1198 	int error;
1199 	struct devfs_mount *dmp;
1200 
1201 	dmp = VFSTODEVFS(ap->a_vp->v_mount);
1202 	sx_xlock(&dmp->dm_lock);
1203 	DEVFS_DMP_HOLD(dmp);
1204 	devfs_populate(dmp);
1205 	if (DEVFS_DMP_DROP(dmp)) {
1206 		sx_xunlock(&dmp->dm_lock);
1207 		devfs_unmount_final(dmp);
1208 		return (ENOENT);
1209 	}
1210 	error = devfs_rules_ioctl(dmp, ap->a_command, ap->a_data, ap->a_td);
1211 	sx_xunlock(&dmp->dm_lock);
1212 	return (error);
1213 }
1214 
1215 static int
1216 devfs_rread(struct vop_read_args *ap)
1217 {
1218 
1219 	if (ap->a_vp->v_type != VDIR)
1220 		return (EINVAL);
1221 	return (VOP_READDIR(ap->a_vp, ap->a_uio, ap->a_cred, NULL, NULL, NULL));
1222 }
1223 
1224 static int
1225 devfs_setattr(struct vop_setattr_args *ap)
1226 {
1227 	struct devfs_dirent *de;
1228 	struct vattr *vap;
1229 	struct vnode *vp;
1230 	int c, error;
1231 	uid_t uid;
1232 	gid_t gid;
1233 
1234 	vap = ap->a_vap;
1235 	vp = ap->a_vp;
1236 	if ((vap->va_type != VNON) ||
1237 	    (vap->va_nlink != VNOVAL) ||
1238 	    (vap->va_fsid != VNOVAL) ||
1239 	    (vap->va_fileid != VNOVAL) ||
1240 	    (vap->va_blocksize != VNOVAL) ||
1241 	    (vap->va_flags != VNOVAL && vap->va_flags != 0) ||
1242 	    (vap->va_rdev != VNOVAL) ||
1243 	    ((int)vap->va_bytes != VNOVAL) ||
1244 	    (vap->va_gen != VNOVAL)) {
1245 		return (EINVAL);
1246 	}
1247 
1248 	de = vp->v_data;
1249 	if (vp->v_type == VDIR)
1250 		de = de->de_dir;
1251 
1252 	error = c = 0;
1253 	if (vap->va_uid == (uid_t)VNOVAL)
1254 		uid = de->de_uid;
1255 	else
1256 		uid = vap->va_uid;
1257 	if (vap->va_gid == (gid_t)VNOVAL)
1258 		gid = de->de_gid;
1259 	else
1260 		gid = vap->va_gid;
1261 	if (uid != de->de_uid || gid != de->de_gid) {
1262 		if ((ap->a_cred->cr_uid != de->de_uid) || uid != de->de_uid ||
1263 		    (gid != de->de_gid && !groupmember(gid, ap->a_cred))) {
1264 			error = priv_check(ap->a_td, PRIV_VFS_CHOWN);
1265 			if (error)
1266 				return (error);
1267 		}
1268 		de->de_uid = uid;
1269 		de->de_gid = gid;
1270 		c = 1;
1271 	}
1272 
1273 	if (vap->va_mode != (mode_t)VNOVAL) {
1274 		if (ap->a_cred->cr_uid != de->de_uid) {
1275 			error = priv_check(ap->a_td, PRIV_VFS_ADMIN);
1276 			if (error)
1277 				return (error);
1278 		}
1279 		de->de_mode = vap->va_mode;
1280 		c = 1;
1281 	}
1282 
1283 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1284 		/* See the comment in ufs_vnops::ufs_setattr(). */
1285 		if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, ap->a_td)) &&
1286 		    ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
1287 		    (error = VOP_ACCESS(vp, VWRITE, ap->a_cred, ap->a_td))))
1288 			return (error);
1289 		if (vap->va_atime.tv_sec != VNOVAL) {
1290 			if (vp->v_type == VCHR)
1291 				vp->v_rdev->si_atime = vap->va_atime;
1292 			else
1293 				de->de_atime = vap->va_atime;
1294 		}
1295 		if (vap->va_mtime.tv_sec != VNOVAL) {
1296 			if (vp->v_type == VCHR)
1297 				vp->v_rdev->si_mtime = vap->va_mtime;
1298 			else
1299 				de->de_mtime = vap->va_mtime;
1300 		}
1301 		c = 1;
1302 	}
1303 
1304 	if (c) {
1305 		if (vp->v_type == VCHR)
1306 			vfs_timestamp(&vp->v_rdev->si_ctime);
1307 		else
1308 			vfs_timestamp(&de->de_mtime);
1309 	}
1310 	return (0);
1311 }
1312 
1313 #ifdef MAC
1314 static int
1315 devfs_setlabel(struct vop_setlabel_args *ap)
1316 {
1317 	struct vnode *vp;
1318 	struct devfs_dirent *de;
1319 
1320 	vp = ap->a_vp;
1321 	de = vp->v_data;
1322 
1323 	mac_vnode_relabel(ap->a_cred, vp, ap->a_label);
1324 	mac_devfs_update(vp->v_mount, de, vp);
1325 
1326 	return (0);
1327 }
1328 #endif
1329 
1330 static int
1331 devfs_stat_f(struct file *fp, struct stat *sb, struct ucred *cred, struct thread *td)
1332 {
1333 
1334 	return (vnops.fo_stat(fp, sb, cred, td));
1335 }
1336 
1337 static int
1338 devfs_symlink(struct vop_symlink_args *ap)
1339 {
1340 	int i, error;
1341 	struct devfs_dirent *dd;
1342 	struct devfs_dirent *de;
1343 	struct devfs_mount *dmp;
1344 	struct thread *td;
1345 
1346 	td = ap->a_cnp->cn_thread;
1347 	KASSERT(td == curthread, ("devfs_symlink: td != curthread"));
1348 
1349 	error = priv_check(td, PRIV_DEVFS_SYMLINK);
1350 	if (error)
1351 		return(error);
1352 	dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1353 	dd = ap->a_dvp->v_data;
1354 	de = devfs_newdirent(ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen);
1355 	de->de_uid = 0;
1356 	de->de_gid = 0;
1357 	de->de_mode = 0755;
1358 	de->de_inode = alloc_unr(devfs_inos);
1359 	de->de_dirent->d_type = DT_LNK;
1360 	i = strlen(ap->a_target) + 1;
1361 	de->de_symlink = malloc(i, M_DEVFS, M_WAITOK);
1362 	bcopy(ap->a_target, de->de_symlink, i);
1363 	sx_xlock(&dmp->dm_lock);
1364 #ifdef MAC
1365 	mac_devfs_create_symlink(ap->a_cnp->cn_cred, dmp->dm_mount, dd, de);
1366 #endif
1367 	TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
1368 	return (devfs_allocv(de, ap->a_dvp->v_mount, ap->a_vpp, td));
1369 }
1370 
1371 static int
1372 devfs_truncate_f(struct file *fp, off_t length, struct ucred *cred, struct thread *td)
1373 {
1374 
1375 	return (vnops.fo_truncate(fp, length, cred, td));
1376 }
1377 
1378 /* ARGSUSED */
1379 static int
1380 devfs_write_f(struct file *fp, struct uio *uio, struct ucred *cred, int flags, struct thread *td)
1381 {
1382 	struct cdev *dev;
1383 	int error, ioflag, resid;
1384 	struct cdevsw *dsw;
1385 
1386 	error = devfs_fp_check(fp, &dev, &dsw);
1387 	if (error)
1388 		return (error);
1389 	KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", uio->uio_td, td));
1390 	ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT | O_FSYNC);
1391 	if (ioflag & O_DIRECT)
1392 		ioflag |= IO_DIRECT;
1393 	if ((flags & FOF_OFFSET) == 0)
1394 		uio->uio_offset = fp->f_offset;
1395 
1396 	resid = uio->uio_resid;
1397 
1398 	error = dsw->d_write(dev, uio, ioflag);
1399 	if (uio->uio_resid != resid || (error == 0 && resid != 0)) {
1400 		vfs_timestamp(&dev->si_ctime);
1401 		dev->si_mtime = dev->si_ctime;
1402 	}
1403 	curthread->td_fpop = NULL;
1404 	dev_relthread(dev);
1405 
1406 	if ((flags & FOF_OFFSET) == 0)
1407 		fp->f_offset = uio->uio_offset;
1408 	fp->f_nextoff = uio->uio_offset;
1409 	return (error);
1410 }
1411 
1412 dev_t
1413 dev2udev(struct cdev *x)
1414 {
1415 	if (x == NULL)
1416 		return (NODEV);
1417 	return (cdev2priv(x)->cdp_inode);
1418 }
1419 
1420 static struct fileops devfs_ops_f = {
1421 	.fo_read =	devfs_read_f,
1422 	.fo_write =	devfs_write_f,
1423 	.fo_truncate =	devfs_truncate_f,
1424 	.fo_ioctl =	devfs_ioctl_f,
1425 	.fo_poll =	devfs_poll_f,
1426 	.fo_kqfilter =	devfs_kqfilter_f,
1427 	.fo_stat =	devfs_stat_f,
1428 	.fo_close =	devfs_close_f,
1429 	.fo_flags =	DFLAG_PASSABLE | DFLAG_SEEKABLE
1430 };
1431 
1432 static struct vop_vector devfs_vnodeops = {
1433 	.vop_default =		&default_vnodeops,
1434 
1435 	.vop_access =		devfs_access,
1436 	.vop_getattr =		devfs_getattr,
1437 	.vop_ioctl =		devfs_rioctl,
1438 	.vop_lookup =		devfs_lookup,
1439 	.vop_mknod =		devfs_mknod,
1440 	.vop_pathconf =		devfs_pathconf,
1441 	.vop_read =		devfs_rread,
1442 	.vop_readdir =		devfs_readdir,
1443 	.vop_readlink =		devfs_readlink,
1444 	.vop_reclaim =		devfs_reclaim,
1445 	.vop_remove =		devfs_remove,
1446 	.vop_revoke =		devfs_revoke,
1447 	.vop_setattr =		devfs_setattr,
1448 #ifdef MAC
1449 	.vop_setlabel =		devfs_setlabel,
1450 #endif
1451 	.vop_symlink =		devfs_symlink,
1452 };
1453 
1454 static struct vop_vector devfs_specops = {
1455 	.vop_default =		&default_vnodeops,
1456 
1457 	.vop_access =		devfs_access,
1458 	.vop_advlock =		devfs_advlock,
1459 	.vop_bmap =		VOP_PANIC,
1460 	.vop_close =		devfs_close,
1461 	.vop_create =		VOP_PANIC,
1462 	.vop_fsync =		devfs_fsync,
1463 	.vop_getattr =		devfs_getattr,
1464 	.vop_lease =		VOP_NULL,
1465 	.vop_link =		VOP_PANIC,
1466 	.vop_mkdir =		VOP_PANIC,
1467 	.vop_mknod =		VOP_PANIC,
1468 	.vop_open =		devfs_open,
1469 	.vop_pathconf =		devfs_pathconf,
1470 	.vop_print =		devfs_print,
1471 	.vop_read =		VOP_PANIC,
1472 	.vop_readdir =		VOP_PANIC,
1473 	.vop_readlink =		VOP_PANIC,
1474 	.vop_reallocblks =	VOP_PANIC,
1475 	.vop_reclaim =		devfs_reclaim,
1476 	.vop_remove =		devfs_remove,
1477 	.vop_rename =		VOP_PANIC,
1478 	.vop_revoke =		devfs_revoke,
1479 	.vop_rmdir =		VOP_PANIC,
1480 	.vop_setattr =		devfs_setattr,
1481 #ifdef MAC
1482 	.vop_setlabel =		devfs_setlabel,
1483 #endif
1484 	.vop_strategy =		VOP_PANIC,
1485 	.vop_symlink =		VOP_PANIC,
1486 	.vop_write =		VOP_PANIC,
1487 };
1488 
1489 /*
1490  * Our calling convention to the device drivers used to be that we passed
1491  * vnode.h IO_* flags to read()/write(), but we're moving to fcntl.h O_
1492  * flags instead since that's what open(), close() and ioctl() takes and
1493  * we don't really want vnode.h in device drivers.
1494  * We solved the source compatibility by redefining some vnode flags to
1495  * be the same as the fcntl ones and by sending down the bitwise OR of
1496  * the respective fcntl/vnode flags.  These CTASSERTS make sure nobody
1497  * pulls the rug out under this.
1498  */
1499 CTASSERT(O_NONBLOCK == IO_NDELAY);
1500 CTASSERT(O_FSYNC == IO_SYNC);
1501