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