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