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