xref: /freebsd/sys/fs/devfs/devfs_vnops.c (revision 7b54cdda4e5d7bf71170366d59f76889ead62c9e)
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  *	mknod: hunt down DE_DELETED, compare name, reinstantiate.
41  *	mkdir: want it ?
42  */
43 
44 #include <opt_devfs.h>
45 #include <opt_mac.h>
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/conf.h>
50 #include <sys/dirent.h>
51 #include <sys/fcntl.h>
52 #include <sys/file.h>
53 #include <sys/filedesc.h>
54 #include <sys/filio.h>
55 #include <sys/kernel.h>
56 #include <sys/lock.h>
57 #include <sys/mac.h>
58 #include <sys/malloc.h>
59 #include <sys/mount.h>
60 #include <sys/namei.h>
61 #include <sys/proc.h>
62 #include <sys/stat.h>
63 #include <sys/sx.h>
64 #include <sys/time.h>
65 #include <sys/ttycom.h>
66 #include <sys/unistd.h>
67 #include <sys/vnode.h>
68 
69 #include <fs/devfs/devfs.h>
70 
71 static fo_rdwr_t	devfs_read_f;
72 static fo_rdwr_t	devfs_write_f;
73 static fo_ioctl_t	devfs_ioctl_f;
74 static fo_poll_t	devfs_poll_f;
75 static fo_kqfilter_t	devfs_kqfilter_f;
76 static fo_stat_t	devfs_stat_f;
77 static fo_close_t	devfs_close_f;
78 
79 struct fileops devfs_ops_f = {
80 	.fo_read =	devfs_read_f,
81 	.fo_write =	devfs_write_f,
82 	.fo_ioctl =	devfs_ioctl_f,
83 	.fo_poll =	devfs_poll_f,
84 	.fo_kqfilter =	devfs_kqfilter_f,
85 	.fo_stat =	devfs_stat_f,
86 	.fo_close =	devfs_close_f,
87 	.fo_flags =	DFLAG_PASSABLE | DFLAG_SEEKABLE
88 };
89 
90 static int	devfs_access(struct vop_access_args *ap);
91 static int	devfs_advlock(struct vop_advlock_args *ap);
92 static int	devfs_close(struct vop_close_args *ap);
93 static int	devfs_fsync(struct vop_fsync_args *ap);
94 static int	devfs_getattr(struct vop_getattr_args *ap);
95 static int	devfs_lookupx(struct vop_lookup_args *ap);
96 static int	devfs_mknod(struct vop_mknod_args *ap);
97 static int	devfs_open(struct vop_open_args *ap);
98 static int	devfs_pathconf(struct vop_pathconf_args *ap);
99 static int	devfs_print(struct vop_print_args *ap);
100 static int	devfs_readdir(struct vop_readdir_args *ap);
101 static int	devfs_readlink(struct vop_readlink_args *ap);
102 static int	devfs_reclaim(struct vop_reclaim_args *ap);
103 static int	devfs_remove(struct vop_remove_args *ap);
104 static int	devfs_revoke(struct vop_revoke_args *ap);
105 static int	devfs_rioctl(struct vop_ioctl_args *ap);
106 static int	devfs_rread(struct vop_read_args *ap);
107 static int	devfs_setattr(struct vop_setattr_args *ap);
108 #ifdef MAC
109 static int	devfs_setlabel(struct vop_setlabel_args *ap);
110 #endif
111 static int	devfs_symlink(struct vop_symlink_args *ap);
112 
113 static vop_t **devfs_vnodeop_p;
114 vop_t **devfs_specop_p;
115 
116 static int
117 devfs_fp_check(struct file *fp, struct cdev **devp, struct cdevsw **dswp)
118 {
119 
120 	*devp = fp->f_vnode->v_rdev;
121 	if (*devp != fp->f_data)
122 		return (ENXIO);
123 	KASSERT((*devp)->si_refcount > 0,
124 	    ("devfs: un-referenced struct cdev *(%s)", devtoname(*devp)));
125 	*dswp = dev_refthread(*devp);
126 	if (*dswp == NULL)
127 		return (ENXIO);
128 	return (0);
129 }
130 
131 /*
132  * Construct the fully qualified path name relative to the mountpoint
133  */
134 static char *
135 devfs_fqpn(char *buf, struct vnode *dvp, struct componentname *cnp)
136 {
137 	int i;
138 	struct devfs_dirent *de, *dd;
139 	struct devfs_mount *dmp;
140 
141 	dmp = VFSTODEVFS(dvp->v_mount);
142 	dd = dvp->v_data;
143 	i = SPECNAMELEN;
144 	buf[i] = '\0';
145 	i -= cnp->cn_namelen;
146 	if (i < 0)
147 		 return (NULL);
148 	bcopy(cnp->cn_nameptr, buf + i, cnp->cn_namelen);
149 	de = dd;
150 	while (de != dmp->dm_basedir) {
151 		i--;
152 		if (i < 0)
153 			 return (NULL);
154 		buf[i] = '/';
155 		i -= de->de_dirent->d_namlen;
156 		if (i < 0)
157 			 return (NULL);
158 		bcopy(de->de_dirent->d_name, buf + i,
159 		    de->de_dirent->d_namlen);
160 		de = TAILQ_FIRST(&de->de_dlist);	/* "." */
161 		de = TAILQ_NEXT(de, de_list);		/* ".." */
162 		de = de->de_dir;
163 	}
164 	return (buf + i);
165 }
166 
167 int
168 devfs_allocv(struct devfs_dirent *de, struct mount *mp, struct vnode **vpp, struct thread *td)
169 {
170 	int error;
171 	struct vnode *vp;
172 	struct cdev *dev;
173 
174 	KASSERT(td == curthread, ("devfs_allocv: td != curthread"));
175 loop:
176 	vp = de->de_vnode;
177 	if (vp != NULL) {
178 		if (vget(vp, LK_EXCLUSIVE, td))
179 			goto loop;
180 		*vpp = vp;
181 		return (0);
182 	}
183 	if (de->de_dirent->d_type == DT_CHR) {
184 		dev = *devfs_itod(de->de_inode);
185 		if (dev == NULL)
186 			return (ENOENT);
187 	} else {
188 		dev = NULL;
189 	}
190 	error = getnewvnode("devfs", mp, devfs_vnodeop_p, &vp);
191 	if (error != 0) {
192 		printf("devfs_allocv: failed to allocate new vnode\n");
193 		return (error);
194 	}
195 
196 	if (de->de_dirent->d_type == DT_CHR) {
197 		vp->v_type = VCHR;
198 		vp = addaliasu(vp, dev->si_udev);
199 		vp->v_op = devfs_specop_p;
200 	} else if (de->de_dirent->d_type == DT_DIR) {
201 		vp->v_type = VDIR;
202 	} else if (de->de_dirent->d_type == DT_LNK) {
203 		vp->v_type = VLNK;
204 	} else {
205 		vp->v_type = VBAD;
206 	}
207 	vp->v_data = de;
208 	de->de_vnode = vp;
209 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
210 #ifdef MAC
211 	mac_associate_vnode_devfs(mp, de, vp);
212 #endif
213 	*vpp = vp;
214 	return (0);
215 }
216 
217 static int
218 devfs_access(ap)
219 	struct vop_access_args /* {
220 		struct vnode *a_vp;
221 		int  a_mode;
222 		struct ucred *a_cred;
223 		struct thread *a_td;
224 	} */ *ap;
225 {
226 	struct vnode *vp = ap->a_vp;
227 	struct devfs_dirent *de;
228 	int error;
229 
230 	de = vp->v_data;
231 	if (vp->v_type == VDIR)
232 		de = de->de_dir;
233 
234 	error = vaccess(vp->v_type, de->de_mode, de->de_uid, de->de_gid,
235 	    ap->a_mode, ap->a_cred, NULL);
236 	if (!error)
237 		return (error);
238 	if (error != EACCES)
239 		return (error);
240 	/* We do, however, allow access to the controlling terminal */
241 	if (!(ap->a_td->td_proc->p_flag & P_CONTROLT))
242 		return (error);
243 	if (ap->a_td->td_proc->p_session->s_ttyvp == de->de_vnode)
244 		return (0);
245 	return (error);
246 }
247 
248 /*
249  * Special device advisory byte-level locks.
250  */
251 /* ARGSUSED */
252 static int
253 devfs_advlock(ap)
254 	struct vop_advlock_args /* {
255 		struct vnode *a_vp;
256 		caddr_t  a_id;
257 		int  a_op;
258 		struct flock *a_fl;
259 		int  a_flags;
260 	} */ *ap;
261 {
262 
263 	return (ap->a_flags & F_FLOCK ? EOPNOTSUPP : EINVAL);
264 }
265 
266 /*
267  * Device close routine
268  */
269 /* ARGSUSED */
270 static int
271 devfs_close(ap)
272 	struct vop_close_args /* {
273 		struct vnode *a_vp;
274 		int  a_fflag;
275 		struct ucred *a_cred;
276 		struct thread *a_td;
277 	} */ *ap;
278 {
279 	struct vnode *vp = ap->a_vp, *oldvp;
280 	struct thread *td = ap->a_td;
281 	struct cdev *dev = vp->v_rdev;
282 	struct cdevsw *dsw;
283 	int error;
284 
285 	/*
286 	 * Hack: a tty device that is a controlling terminal
287 	 * has a reference from the session structure.
288 	 * We cannot easily tell that a character device is
289 	 * a controlling terminal, unless it is the closing
290 	 * process' controlling terminal.  In that case,
291 	 * if the reference count is 2 (this last descriptor
292 	 * plus the session), release the reference from the session.
293 	 */
294 
295 	/*
296 	 * This needs to be rewritten to take the vp interlock into
297 	 * consideration.
298 	 */
299 
300 	oldvp = NULL;
301 	sx_xlock(&proctree_lock);
302 	if (td && vp == td->td_proc->p_session->s_ttyvp) {
303 		SESS_LOCK(td->td_proc->p_session);
304 		VI_LOCK(vp);
305 		if (count_dev(dev) == 2 && (vp->v_iflag & VI_XLOCK) == 0) {
306 			td->td_proc->p_session->s_ttyvp = NULL;
307 			oldvp = vp;
308 		}
309 		VI_UNLOCK(vp);
310 		SESS_UNLOCK(td->td_proc->p_session);
311 	}
312 	sx_xunlock(&proctree_lock);
313 	if (oldvp != NULL)
314 		vrele(oldvp);
315 	/*
316 	 * We do not want to really close the device if it
317 	 * is still in use unless we are trying to close it
318 	 * forcibly. Since every use (buffer, vnode, swap, cmap)
319 	 * holds a reference to the vnode, and because we mark
320 	 * any other vnodes that alias this device, when the
321 	 * sum of the reference counts on all the aliased
322 	 * vnodes descends to one, we are on last close.
323 	 */
324 	dsw = dev_refthread(dev);
325 	if (dsw == NULL)
326 		return (ENXIO);
327 	VI_LOCK(vp);
328 	if (vp->v_iflag & VI_XLOCK) {
329 		/* Forced close. */
330 	} else if (dsw->d_flags & D_TRACKCLOSE) {
331 		/* Keep device updated on status. */
332 	} else if (count_dev(dev) > 1) {
333 		VI_UNLOCK(vp);
334 		dev_relthread(dev);
335 		return (0);
336 	}
337 	VI_UNLOCK(vp);
338 	KASSERT(dev->si_refcount > 0,
339 	    ("devfs_close() on un-referenced struct cdev *(%s)", devtoname(dev)));
340 	if (!(dsw->d_flags & D_NEEDGIANT)) {
341 		DROP_GIANT();
342 		error = dsw->d_close(dev, ap->a_fflag, S_IFCHR, td);
343 		PICKUP_GIANT();
344 	} else
345 		error = dsw->d_close(dev, ap->a_fflag, S_IFCHR, td);
346 	dev_relthread(dev);
347 	return (error);
348 }
349 
350 static int
351 devfs_close_f(struct file *fp, struct thread *td)
352 {
353 
354 	return (vnops.fo_close(fp, td));
355 }
356 
357 /*
358  * Synch buffers associated with a block device
359  */
360 /* ARGSUSED */
361 static int
362 devfs_fsync(ap)
363 	struct vop_fsync_args /* {
364 		struct vnode *a_vp;
365 		struct ucred *a_cred;
366 		int  a_waitfor;
367 		struct thread *a_td;
368 	} */ *ap;
369 {
370 	if (!vn_isdisk(ap->a_vp, NULL))
371 		return (0);
372 
373 	return (vop_stdfsync(ap));
374 }
375 
376 static int
377 devfs_getattr(ap)
378 	struct vop_getattr_args /* {
379 		struct vnode *a_vp;
380 		struct vattr *a_vap;
381 		struct ucred *a_cred;
382 		struct thread *a_td;
383 	} */ *ap;
384 {
385 	struct vnode *vp = ap->a_vp;
386 	struct vattr *vap = ap->a_vap;
387 	int error = 0;
388 	struct devfs_dirent *de;
389 	struct cdev *dev;
390 
391 	de = vp->v_data;
392 	if (vp->v_type == VDIR)
393 		de = de->de_dir;
394 	bzero((caddr_t) vap, sizeof(*vap));
395 	vattr_null(vap);
396 	vap->va_uid = de->de_uid;
397 	vap->va_gid = de->de_gid;
398 	vap->va_mode = de->de_mode;
399 	if (vp->v_type == VLNK)
400 		vap->va_size = strlen(de->de_symlink);
401 	else if (vp->v_type == VDIR)
402 		vap->va_size = vap->va_bytes = DEV_BSIZE;
403 	else
404 		vap->va_size = 0;
405 	if (vp->v_type != VDIR)
406 		vap->va_bytes = 0;
407 	vap->va_blocksize = DEV_BSIZE;
408 	vap->va_type = vp->v_type;
409 
410 #define fix(aa)							\
411 	do {							\
412 		if ((aa).tv_sec == 0) {				\
413 			(aa).tv_sec = boottime.tv_sec;		\
414 			(aa).tv_nsec = boottime.tv_usec * 1000; \
415 		}						\
416 	} while (0)
417 
418 	if (vp->v_type != VCHR)  {
419 		fix(de->de_atime);
420 		vap->va_atime = de->de_atime;
421 		fix(de->de_mtime);
422 		vap->va_mtime = de->de_mtime;
423 		fix(de->de_ctime);
424 		vap->va_ctime = de->de_ctime;
425 	} else {
426 		dev = vp->v_rdev;
427 		fix(dev->si_atime);
428 		vap->va_atime = dev->si_atime;
429 		fix(dev->si_mtime);
430 		vap->va_mtime = dev->si_mtime;
431 		fix(dev->si_ctime);
432 		vap->va_ctime = dev->si_ctime;
433 		vap->va_rdev = dev->si_udev;
434 	}
435 	vap->va_gen = 0;
436 	vap->va_flags = 0;
437 	vap->va_nlink = de->de_links;
438 	vap->va_fileid = de->de_inode;
439 
440 	return (error);
441 }
442 
443 /*
444  * Device ioctl operation.
445  */
446 /* ARGSUSED */
447 static int
448 devfs_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred, struct thread *td)
449 {
450 	struct cdev *dev;
451 	struct cdevsw *dsw;
452 	struct vnode *vp;
453 	struct vnode *vpold;
454 	int error;
455 
456 	error = devfs_fp_check(fp, &dev, &dsw);
457 	if (error)
458 		return (error);
459 
460 	if (com == FIODTYPE) {
461 		*(int *)data = dsw->d_flags & D_TYPEMASK;
462 		dev_relthread(dev);
463 		return (0);
464 	}
465 	if (dsw->d_flags & D_NEEDGIANT)
466 		mtx_lock(&Giant);
467 	error = dsw->d_ioctl(dev, com, data, fp->f_flag, td);
468 	if (dsw->d_flags & D_NEEDGIANT)
469 		mtx_unlock(&Giant);
470 	dev_relthread(dev);
471 	if (error == ENOIOCTL)
472 		error = ENOTTY;
473 	if (error == 0 && com == TIOCSCTTY) {
474 		vp = fp->f_vnode;
475 
476 		/* Do nothing if reassigning same control tty */
477 		sx_slock(&proctree_lock);
478 		if (td->td_proc->p_session->s_ttyvp == vp) {
479 			sx_sunlock(&proctree_lock);
480 			return (0);
481 		}
482 
483 		mtx_lock(&Giant);
484 
485 		vpold = td->td_proc->p_session->s_ttyvp;
486 		VREF(vp);
487 		SESS_LOCK(td->td_proc->p_session);
488 		td->td_proc->p_session->s_ttyvp = vp;
489 		SESS_UNLOCK(td->td_proc->p_session);
490 
491 		sx_sunlock(&proctree_lock);
492 
493 		/* Get rid of reference to old control tty */
494 		if (vpold)
495 			vrele(vpold);
496 		mtx_unlock(&Giant);
497 	}
498 	return (error);
499 }
500 
501 
502 /* ARGSUSED */
503 static int
504 devfs_kqfilter_f(struct file *fp, struct knote *kn)
505 {
506 	struct cdev *dev;
507 	struct cdevsw *dsw;
508 	int error;
509 
510 	error = devfs_fp_check(fp, &dev, &dsw);
511 	if (error)
512 		return (error);
513 	if (dsw->d_flags & D_NEEDGIANT)
514 		mtx_lock(&Giant);
515 	error = dsw->d_kqfilter(dev, kn);
516 	if (dsw->d_flags & D_NEEDGIANT)
517 		mtx_unlock(&Giant);
518 	dev_relthread(dev);
519 	return (error);
520 }
521 
522 static int
523 devfs_lookupx(ap)
524 	struct vop_lookup_args /* {
525 		struct vnode * a_dvp;
526 		struct vnode ** a_vpp;
527 		struct componentname * a_cnp;
528 	} */ *ap;
529 {
530 	struct componentname *cnp;
531 	struct vnode *dvp, **vpp;
532 	struct thread *td;
533 	struct devfs_dirent *de, *dd;
534 	struct devfs_dirent **dde;
535 	struct devfs_mount *dmp;
536 	struct cdev *cdev;
537 	int error, flags, nameiop;
538 	char specname[SPECNAMELEN + 1], *pname;
539 
540 	cnp = ap->a_cnp;
541 	vpp = ap->a_vpp;
542 	dvp = ap->a_dvp;
543 	pname = cnp->cn_nameptr;
544 	td = cnp->cn_thread;
545 	flags = cnp->cn_flags;
546 	nameiop = cnp->cn_nameiop;
547 	dmp = VFSTODEVFS(dvp->v_mount);
548 	dd = dvp->v_data;
549 
550 	*vpp = NULLVP;
551 	cnp->cn_flags &= ~PDIRUNLOCK;
552 
553 	if ((flags & ISLASTCN) && nameiop == RENAME)
554 		return (EOPNOTSUPP);
555 
556 	if (dvp->v_type != VDIR)
557 		return (ENOTDIR);
558 
559 	if ((flags & ISDOTDOT) && (dvp->v_vflag & VV_ROOT))
560 		return (EIO);
561 
562 	error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td);
563 	if (error)
564 		return (error);
565 
566 	if (cnp->cn_namelen == 1 && *pname == '.') {
567 		if ((flags & ISLASTCN) && nameiop != LOOKUP)
568 			return (EINVAL);
569 		*vpp = dvp;
570 		VREF(dvp);
571 		return (0);
572 	}
573 
574 	if (flags & ISDOTDOT) {
575 		if ((flags & ISLASTCN) && nameiop != LOOKUP)
576 			return (EINVAL);
577 		VOP_UNLOCK(dvp, 0, td);
578 		cnp->cn_flags |= PDIRUNLOCK;
579 		de = TAILQ_FIRST(&dd->de_dlist);	/* "." */
580 		de = TAILQ_NEXT(de, de_list);		/* ".." */
581 		de = de->de_dir;
582 		error = devfs_allocv(de, dvp->v_mount, vpp, td);
583 		if (error || ((flags & LOCKPARENT) && (flags & ISLASTCN))) {
584 			vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
585 			cnp->cn_flags &= ~PDIRUNLOCK;
586 		}
587 		return (error);
588 	}
589 
590 	devfs_populate(dmp);
591 	dd = dvp->v_data;
592 	TAILQ_FOREACH(de, &dd->de_dlist, de_list) {
593 		if (cnp->cn_namelen != de->de_dirent->d_namlen)
594 			continue;
595 		if (bcmp(cnp->cn_nameptr, de->de_dirent->d_name,
596 		    de->de_dirent->d_namlen) != 0)
597 			continue;
598 		if (de->de_flags & DE_WHITEOUT)
599 			goto notfound;
600 		goto found;
601 	}
602 
603 	if (nameiop == DELETE)
604 		goto notfound;
605 
606 	/*
607 	 * OK, we didn't have an entry for the name we were asked for
608 	 * so we try to see if anybody can create it on demand.
609 	 */
610 	pname = devfs_fqpn(specname, dvp, cnp);
611 	if (pname == NULL)
612 		goto notfound;
613 
614 	cdev = NULL;
615 	EVENTHANDLER_INVOKE(dev_clone, pname, strlen(pname), &cdev);
616 	if (cdev == NULL)
617 		goto notfound;
618 
619 	devfs_populate(dmp);
620 
621 	dde = devfs_itode(dmp, cdev->si_inode);
622 
623 	if (dde == NULL || *dde == NULL || *dde == DE_DELETED)
624 		goto notfound;
625 
626 	if ((*dde)->de_flags & DE_WHITEOUT)
627 		goto notfound;
628 
629 	de = *dde;
630 	goto found;
631 
632 notfound:
633 
634 	if ((nameiop == CREATE || nameiop == RENAME) &&
635 	    (flags & (LOCKPARENT | WANTPARENT)) && (flags & ISLASTCN)) {
636 		cnp->cn_flags |= SAVENAME;
637 		if (!(flags & LOCKPARENT)) {
638 			VOP_UNLOCK(dvp, 0, td);
639 			cnp->cn_flags |= PDIRUNLOCK;
640 		}
641 		return (EJUSTRETURN);
642 	}
643 	return (ENOENT);
644 
645 
646 found:
647 
648 	if ((cnp->cn_nameiop == DELETE) && (flags & ISLASTCN)) {
649 		error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
650 		if (error)
651 			return (error);
652 		if (*vpp == dvp) {
653 			VREF(dvp);
654 			*vpp = dvp;
655 			return (0);
656 		}
657 		error = devfs_allocv(de, dvp->v_mount, vpp, td);
658 		if (error)
659 			return (error);
660 		if (!(flags & LOCKPARENT)) {
661 			VOP_UNLOCK(dvp, 0, td);
662 			cnp->cn_flags |= PDIRUNLOCK;
663 		}
664 		return (0);
665 	}
666 	error = devfs_allocv(de, dvp->v_mount, vpp, td);
667 	if (error)
668 		return (error);
669 	if (!(flags & LOCKPARENT) || !(flags & ISLASTCN)) {
670 		VOP_UNLOCK(dvp, 0, td);
671 		cnp->cn_flags |= PDIRUNLOCK;
672 	}
673 	return (0);
674 }
675 
676 static int
677 devfs_lookup(struct vop_lookup_args *ap)
678 {
679 	int j;
680 	struct devfs_mount *dmp;
681 
682 	dmp = VFSTODEVFS(ap->a_dvp->v_mount);
683 	lockmgr(&dmp->dm_lock, LK_SHARED, 0, curthread);
684 	j = devfs_lookupx(ap);
685 	lockmgr(&dmp->dm_lock, LK_RELEASE, 0, curthread);
686 	return (j);
687 }
688 
689 static int
690 devfs_mknod(struct vop_mknod_args *ap)
691 	/*
692 	struct vop_mknod_args {
693 		struct vnodeop_desc *a_desc;
694 		struct vnode *a_dvp;
695 		struct vnode **a_vpp;
696 		struct componentname *a_cnp;
697 		struct vattr *a_vap;
698 	}; */
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 	dvp = ap->a_dvp;
708 	dmp = VFSTODEVFS(dvp->v_mount);
709 	lockmgr(&dmp->dm_lock, LK_EXCLUSIVE, 0, curthread);
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 	TAILQ_FOREACH(de, &dd->de_dlist, de_list) {
718 		if (cnp->cn_namelen != de->de_dirent->d_namlen)
719 			continue;
720 		if (bcmp(cnp->cn_nameptr, de->de_dirent->d_name,
721 		    de->de_dirent->d_namlen) != 0)
722 			continue;
723 		if (de->de_flags & DE_WHITEOUT)
724 			break;
725 		goto notfound;
726 	}
727 	if (de == NULL)
728 		goto notfound;
729 	de->de_flags &= ~DE_WHITEOUT;
730 	error = devfs_allocv(de, dvp->v_mount, vpp, td);
731 notfound:
732 	lockmgr(&dmp->dm_lock, LK_RELEASE, 0, curthread);
733 	return (error);
734 }
735 
736 /*
737  * Open a special file.
738  */
739 /* ARGSUSED */
740 static int
741 devfs_open(ap)
742 	struct vop_open_args /* {
743 		struct vnode *a_vp;
744 		int  a_mode;
745 		struct ucred *a_cred;
746 		struct thread *a_td;
747 		int a_fdidx;
748 	} */ *ap;
749 {
750 	struct thread *td = ap->a_td;
751 	struct vnode *vp = ap->a_vp;
752 	struct cdev *dev = vp->v_rdev;
753 	struct file *fp;
754 	int error;
755 	struct cdevsw *dsw;
756 
757 	if (vp->v_type == VBLK)
758 		return (ENXIO);
759 
760 	if (dev == NULL)
761 		return (ENXIO);
762 
763 	/* Make this field valid before any I/O in d_open. */
764 	if (dev->si_iosize_max == 0)
765 		dev->si_iosize_max = DFLTPHYS;
766 
767 	if (vn_isdisk(vp, NULL) &&
768 	    ap->a_cred != FSCRED && (ap->a_mode & FWRITE)) {
769 		/*
770 		* When running in very secure mode, do not allow
771 		* opens for writing of any disks.
772 		* XXX: should be in geom_dev.c, but we lack the cred there.
773 		*/
774 		error = securelevel_ge(td->td_ucred, 2);
775 		if (error)
776 			return (error);
777 	}
778 
779 	dsw = dev_refthread(dev);
780 	if (dsw == NULL)
781 		return (ENXIO);
782 
783 	/* XXX: Special casing of ttys for deadfs.  Probably redundant. */
784 	if (dsw->d_flags & D_TTY)
785 		vp->v_vflag |= VV_ISTTY;
786 
787 	VOP_UNLOCK(vp, 0, td);
788 
789 	if(!(dsw->d_flags & D_NEEDGIANT)) {
790 		DROP_GIANT();
791 		if (dsw->d_fdopen != NULL)
792 			error = dsw->d_fdopen(dev, ap->a_mode, td, ap->a_fdidx);
793 		else
794 			error = dsw->d_open(dev, ap->a_mode, S_IFCHR, td);
795 		PICKUP_GIANT();
796 	} else if (dsw->d_fdopen != NULL)
797 		error = dsw->d_fdopen(dev, ap->a_mode, td, ap->a_fdidx);
798 	else
799 		error = dsw->d_open(dev, ap->a_mode, S_IFCHR, td);
800 
801 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
802 
803 	dev_relthread(dev);
804 
805 	if (error)
806 		return (error);
807 
808 	if (ap->a_fdidx >= 0) {
809 		/*
810 		 * This is a pretty disgustingly long chain, but I am not
811 		 * sure there is any better way.  Passing the fdidx into
812 		 * VOP_OPEN() offers us more information than just passing
813 		 * the file *.
814 		 */
815 		fp = ap->a_td->td_proc->p_fd->fd_ofiles[ap->a_fdidx];
816 		if (fp->f_ops == &badfileops) {
817 #if 0
818 			printf("devfs_open(%s)\n", devtoname(dev));
819 #endif
820 			fp->f_ops = &devfs_ops_f;
821 			fp->f_data = dev;
822 		}
823 	}
824 
825 	return (error);
826 }
827 
828 static int
829 devfs_pathconf(ap)
830 	struct vop_pathconf_args /* {
831 		struct vnode *a_vp;
832 		int a_name;
833 		int *a_retval;
834 	} */ *ap;
835 {
836 
837 	switch (ap->a_name) {
838 	case _PC_NAME_MAX:
839 		*ap->a_retval = NAME_MAX;
840 		return (0);
841 	case _PC_PATH_MAX:
842 		*ap->a_retval = PATH_MAX;
843 		return (0);
844 	case _PC_MAC_PRESENT:
845 #ifdef MAC
846 		/*
847 		 * If MAC is enabled, devfs automatically supports
848 		 * trivial non-persistant label storage.
849 		 */
850 		*ap->a_retval = 1;
851 #else
852 		*ap->a_retval = 0;
853 #endif
854 		return (0);
855 	default:
856 		return (vop_stdpathconf(ap));
857 	}
858 	/* NOTREACHED */
859 }
860 
861 /* ARGSUSED */
862 static int
863 devfs_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td)
864 {
865 	struct cdev *dev;
866 	struct cdevsw *dsw;
867 	int error;
868 
869 	error = devfs_fp_check(fp, &dev, &dsw);
870 	if (error)
871 		return (error);
872 	if (dsw->d_flags & D_NEEDGIANT)
873 		mtx_lock(&Giant);
874 	error = dsw->d_poll(dev, events, td);
875 	if (dsw->d_flags & D_NEEDGIANT)
876 		mtx_unlock(&Giant);
877 	dev_relthread(dev);
878 	return(error);
879 }
880 
881 /*
882  * Print out the contents of a special device vnode.
883  */
884 static int
885 devfs_print(ap)
886 	struct vop_print_args /* {
887 		struct vnode *a_vp;
888 	} */ *ap;
889 {
890 
891 	printf("\tdev %s\n", devtoname(ap->a_vp->v_rdev));
892 	return (0);
893 }
894 
895 /*
896  * Vnode op for read
897  */
898 /* ARGSUSED */
899 static int
900 devfs_read_f(struct file *fp, struct uio *uio, struct ucred *cred, int flags, struct thread *td)
901 {
902 	struct cdev *dev;
903 	int ioflag, error, resid;
904 	struct cdevsw *dsw;
905 
906 	error = devfs_fp_check(fp, &dev, &dsw);
907 	if (error)
908 		return (error);
909 	resid = uio->uio_resid;
910 	ioflag = 0;
911 	if (fp->f_flag & FNONBLOCK)
912 		ioflag |= IO_NDELAY;
913 	if (fp->f_flag & O_DIRECT)
914 		ioflag |= IO_DIRECT;
915 
916 	if ((flags & FOF_OFFSET) == 0)
917 		uio->uio_offset = fp->f_offset;
918 
919 	if (dsw->d_flags & D_NEEDGIANT)
920 		mtx_lock(&Giant);
921 	error = dsw->d_read(dev, uio, ioflag);
922 	if (dsw->d_flags & D_NEEDGIANT)
923 		mtx_unlock(&Giant);
924 	dev_relthread(dev);
925 	if (uio->uio_resid != resid || (error == 0 && resid != 0))
926 		vfs_timestamp(&dev->si_atime);
927 
928 	if ((flags & FOF_OFFSET) == 0)
929 		fp->f_offset = uio->uio_offset;
930 	fp->f_nextoff = uio->uio_offset;
931 	return (error);
932 }
933 
934 static int
935 devfs_readdir(ap)
936 	struct vop_readdir_args /* {
937 		struct vnode *a_vp;
938 		struct uio *a_uio;
939 		struct ucred *a_cred;
940 		int *a_eofflag;
941 		int *a_ncookies;
942 		u_long **a_cookies;
943 	} */ *ap;
944 {
945 	int error;
946 	struct uio *uio;
947 	struct dirent *dp;
948 	struct devfs_dirent *dd;
949 	struct devfs_dirent *de;
950 	struct devfs_mount *dmp;
951 	off_t off, oldoff;
952 	int ncookies = 0;
953 	u_long *cookiebuf, *cookiep;
954 	struct dirent *dps, *dpe;
955 
956 	if (ap->a_vp->v_type != VDIR)
957 		return (ENOTDIR);
958 
959 	uio = ap->a_uio;
960 	if (uio->uio_offset < 0)
961 		return (EINVAL);
962 
963 	dmp = VFSTODEVFS(ap->a_vp->v_mount);
964 	lockmgr(&dmp->dm_lock, LK_SHARED, 0, curthread);
965 	devfs_populate(dmp);
966 	error = 0;
967 	de = ap->a_vp->v_data;
968 	off = 0;
969 	oldoff = uio->uio_offset;
970 	TAILQ_FOREACH(dd, &de->de_dlist, de_list) {
971 		if (dd->de_flags & DE_WHITEOUT)
972 			continue;
973 		if (dd->de_dirent->d_type == DT_DIR)
974 			de = dd->de_dir;
975 		else
976 			de = dd;
977 		dp = dd->de_dirent;
978 		if (dp->d_reclen > uio->uio_resid)
979 			break;
980 		dp->d_fileno = de->de_inode;
981 		if (off >= uio->uio_offset) {
982 			ncookies++;
983 			error = uiomove(dp, dp->d_reclen, uio);
984 			if (error)
985 				break;
986 		}
987 		off += dp->d_reclen;
988 	}
989 	if( !error && ap->a_ncookies != NULL && ap->a_cookies != NULL ) {
990 		MALLOC(cookiebuf, u_long *, ncookies * sizeof(u_long),
991 		       M_TEMP, M_WAITOK);
992 		cookiep = cookiebuf;
993 		dps = (struct dirent *)((char *)uio->uio_iov->iov_base -
994 		    (uio->uio_offset - oldoff));
995 		dpe = (struct dirent *) uio->uio_iov->iov_base;
996 		for( dp = dps;
997 			dp < dpe;
998 			dp = (struct dirent *)((caddr_t) dp + dp->d_reclen)) {
999 				oldoff += dp->d_reclen;
1000 				*cookiep++ = (u_long) oldoff;
1001 		}
1002 		*ap->a_ncookies = ncookies;
1003 		*ap->a_cookies = cookiebuf;
1004 	}
1005 	lockmgr(&dmp->dm_lock, LK_RELEASE, 0, curthread);
1006 	uio->uio_offset = off;
1007 	return (error);
1008 }
1009 
1010 static int
1011 devfs_readlink(ap)
1012 	struct vop_readlink_args /* {
1013 		struct vnode *a_vp;
1014 		struct uio *a_uio;
1015 		struct ucred *a_cead;
1016 	} */ *ap;
1017 {
1018 	int error;
1019 	struct devfs_dirent *de;
1020 
1021 	de = ap->a_vp->v_data;
1022 	error = uiomove(de->de_symlink, strlen(de->de_symlink), ap->a_uio);
1023 	return (error);
1024 }
1025 
1026 static int
1027 devfs_reclaim(ap)
1028 	struct vop_reclaim_args /* {
1029 		struct vnode *a_vp;
1030 	} */ *ap;
1031 {
1032 	struct vnode *vp = ap->a_vp;
1033 	struct devfs_dirent *de;
1034 	int i;
1035 
1036 	de = vp->v_data;
1037 	if (de != NULL)
1038 		de->de_vnode = NULL;
1039 	vp->v_data = NULL;
1040 	if (vp->v_rdev != NULL) {
1041 		i = vcount(vp);
1042 		if ((vp->v_rdev->si_flags & SI_CHEAPCLONE) && i == 0 &&
1043 		    (vp->v_rdev->si_flags & SI_NAMED))
1044 			destroy_dev(vp->v_rdev);
1045 	}
1046 	return (0);
1047 }
1048 
1049 static int
1050 devfs_remove(ap)
1051 	struct vop_remove_args /* {
1052 		struct vnode *a_dvp;
1053 		struct vnode *a_vp;
1054 		struct componentname *a_cnp;
1055 	} */ *ap;
1056 {
1057 	struct vnode *vp = ap->a_vp;
1058 	struct devfs_dirent *dd;
1059 	struct devfs_dirent *de;
1060 	struct devfs_mount *dmp = VFSTODEVFS(vp->v_mount);
1061 
1062 	lockmgr(&dmp->dm_lock, LK_EXCLUSIVE, 0, curthread);
1063 	dd = ap->a_dvp->v_data;
1064 	de = vp->v_data;
1065 	if (de->de_dirent->d_type == DT_LNK) {
1066 		TAILQ_REMOVE(&dd->de_dlist, de, de_list);
1067 		if (de->de_vnode)
1068 			de->de_vnode->v_data = NULL;
1069 #ifdef MAC
1070 		mac_destroy_devfsdirent(de);
1071 #endif
1072 		FREE(de, M_DEVFS);
1073 	} else {
1074 		de->de_flags |= DE_WHITEOUT;
1075 	}
1076 	lockmgr(&dmp->dm_lock, LK_RELEASE, 0, curthread);
1077 	return (0);
1078 }
1079 
1080 /*
1081  * Revoke is called on a tty when a terminal session ends.  The vnode
1082  * is orphaned by setting v_op to deadfs so we need to let go of it
1083  * as well so that we create a new one next time around.
1084  */
1085 static int
1086 devfs_revoke(ap)
1087 	struct vop_revoke_args /* {
1088 		struct vnode *a_vp;
1089 		int a_flags;
1090 	} */ *ap;
1091 {
1092 	struct vnode *vp = ap->a_vp;
1093 	struct vnode *vq;
1094 	struct devfs_dirent *de;
1095 	struct cdev *dev;
1096 
1097 	KASSERT((ap->a_flags & REVOKEALL) != 0, ("devfs_revoke !REVOKEALL"));
1098 	de = vp->v_data;
1099 	de->de_vnode = NULL;
1100 
1101 	VI_LOCK(vp);
1102 	/*
1103 	 * If a vgone (or vclean) is already in progress,
1104 	 * wait until it is done and return.
1105 	 */
1106 	if (vp->v_iflag & VI_XLOCK) {
1107 		vp->v_iflag |= VI_XWANT;
1108 		msleep(vp, VI_MTX(vp), PINOD | PDROP, "vop_revokeall", 0);
1109 		return (0);
1110 	}
1111 	VI_UNLOCK(vp);
1112 	dev = vp->v_rdev;
1113 	for (;;) {
1114 		dev_lock();
1115 		vq = SLIST_FIRST(&dev->si_hlist);
1116 		dev_unlock();
1117 		if (vq == NULL)
1118 			break;
1119 		vgone(vq);
1120 	}
1121 	return (0);
1122 }
1123 
1124 static int
1125 devfs_rioctl(ap)
1126 	struct vop_ioctl_args /* {
1127 		struct vnode *a_vp;
1128 		u_long  a_command;
1129 		caddr_t  a_data;
1130 		int  a_fflag;
1131 		struct ucred *a_cred;
1132 		struct thread *a_td;
1133 	} */ *ap;
1134 {
1135 	int error;
1136 	struct devfs_mount *dmp;
1137 
1138 	dmp = VFSTODEVFS(ap->a_vp->v_mount);
1139 	lockmgr(&dmp->dm_lock, LK_SHARED, 0, curthread);
1140 	devfs_populate(dmp);
1141 	lockmgr(&dmp->dm_lock, LK_RELEASE, 0, curthread);
1142 	error = devfs_rules_ioctl(ap->a_vp->v_mount, ap->a_command, ap->a_data,
1143 	    ap->a_td);
1144 	return (error);
1145 }
1146 
1147 static int
1148 devfs_rread(ap)
1149 	struct vop_read_args /* {
1150 		struct vnode *a_vp;
1151 		struct uio *a_uio;
1152 		int a_ioflag;
1153 		struct ucred *a_cred;
1154 	} */ *ap;
1155 {
1156 
1157 	if (ap->a_vp->v_type != VDIR)
1158 		return (EINVAL);
1159 	return (VOP_READDIR(ap->a_vp, ap->a_uio, ap->a_cred, NULL, NULL, NULL));
1160 }
1161 
1162 static int
1163 devfs_setattr(ap)
1164 	struct vop_setattr_args /* {
1165 		struct vnode *a_vp;
1166 		struct vattr *a_vap;
1167 		struct ucred *a_cred;
1168 		struct proc *a_p;
1169 	} */ *ap;
1170 {
1171 	struct devfs_dirent *de;
1172 	struct vattr *vap;
1173 	struct vnode *vp;
1174 	int c, error;
1175 	uid_t uid;
1176 	gid_t gid;
1177 
1178 	vap = ap->a_vap;
1179 	vp = ap->a_vp;
1180 	if ((vap->va_type != VNON) ||
1181 	    (vap->va_nlink != VNOVAL) ||
1182 	    (vap->va_fsid != VNOVAL) ||
1183 	    (vap->va_fileid != VNOVAL) ||
1184 	    (vap->va_blocksize != VNOVAL) ||
1185 	    (vap->va_flags != VNOVAL && vap->va_flags != 0) ||
1186 	    (vap->va_rdev != VNOVAL) ||
1187 	    ((int)vap->va_bytes != VNOVAL) ||
1188 	    (vap->va_gen != VNOVAL)) {
1189 		return (EINVAL);
1190 	}
1191 
1192 	de = vp->v_data;
1193 	if (vp->v_type == VDIR)
1194 		de = de->de_dir;
1195 
1196 	error = c = 0;
1197 	if (vap->va_uid == (uid_t)VNOVAL)
1198 		uid = de->de_uid;
1199 	else
1200 		uid = vap->va_uid;
1201 	if (vap->va_gid == (gid_t)VNOVAL)
1202 		gid = de->de_gid;
1203 	else
1204 		gid = vap->va_gid;
1205 	if (uid != de->de_uid || gid != de->de_gid) {
1206 		if (((ap->a_cred->cr_uid != de->de_uid) || uid != de->de_uid ||
1207 		    (gid != de->de_gid && !groupmember(gid, ap->a_cred))) &&
1208 		    (error = suser_cred(ap->a_td->td_ucred, SUSER_ALLOWJAIL)) != 0)
1209 			return (error);
1210 		de->de_uid = uid;
1211 		de->de_gid = gid;
1212 		c = 1;
1213 	}
1214 
1215 	if (vap->va_mode != (mode_t)VNOVAL) {
1216 		if ((ap->a_cred->cr_uid != de->de_uid) &&
1217 		    (error = suser_cred(ap->a_td->td_ucred, SUSER_ALLOWJAIL)))
1218 			return (error);
1219 		de->de_mode = vap->va_mode;
1220 		c = 1;
1221 	}
1222 
1223 	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1224 		/* See the comment in ufs_vnops::ufs_setattr(). */
1225 		if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, ap->a_td)) &&
1226 		    ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
1227 		    (error = VOP_ACCESS(vp, VWRITE, ap->a_cred, ap->a_td))))
1228 			return (error);
1229 		if (vap->va_atime.tv_sec != VNOVAL) {
1230 			if (vp->v_type == VCHR)
1231 				vp->v_rdev->si_atime = vap->va_atime;
1232 			else
1233 				de->de_atime = vap->va_atime;
1234 		}
1235 		if (vap->va_mtime.tv_sec != VNOVAL) {
1236 			if (vp->v_type == VCHR)
1237 				vp->v_rdev->si_mtime = vap->va_mtime;
1238 			else
1239 				de->de_mtime = vap->va_mtime;
1240 		}
1241 		c = 1;
1242 	}
1243 
1244 	if (c) {
1245 		if (vp->v_type == VCHR)
1246 			vfs_timestamp(&vp->v_rdev->si_ctime);
1247 		else
1248 			vfs_timestamp(&de->de_mtime);
1249 	}
1250 	return (0);
1251 }
1252 
1253 #ifdef MAC
1254 static int
1255 devfs_setlabel(ap)
1256 	struct vop_setlabel_args /* {
1257 		struct vnode *a_vp;
1258 		struct mac *a_label;
1259 		struct ucred *a_cred;
1260 		struct thread *a_td;
1261 	} */ *ap;
1262 {
1263 	struct vnode *vp;
1264 	struct devfs_dirent *de;
1265 
1266 	vp = ap->a_vp;
1267 	de = vp->v_data;
1268 
1269 	mac_relabel_vnode(ap->a_cred, vp, ap->a_label);
1270 	mac_update_devfsdirent(vp->v_mount, de, vp);
1271 
1272 	return (0);
1273 }
1274 #endif
1275 
1276 static int
1277 devfs_stat_f(struct file *fp, struct stat *sb, struct ucred *cred, struct thread *td)
1278 {
1279 
1280 	return (vnops.fo_stat(fp, sb, cred, td));
1281 }
1282 
1283 static int
1284 devfs_symlink(ap)
1285 	struct vop_symlink_args /* {
1286 		struct vnode *a_dvp;
1287 		struct vnode **a_vpp;
1288 		struct componentname *a_cnp;
1289 		struct vattr *a_vap;
1290 		char *a_target;
1291 	} */ *ap;
1292 {
1293 	int i, error;
1294 	struct devfs_dirent *dd;
1295 	struct devfs_dirent *de;
1296 	struct devfs_mount *dmp;
1297 	struct thread *td;
1298 
1299 	td = ap->a_cnp->cn_thread;
1300 	KASSERT(td == curthread, ("devfs_symlink: td != curthread"));
1301 	error = suser(td);
1302 	if (error)
1303 		return(error);
1304 	dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1305 	dd = ap->a_dvp->v_data;
1306 	de = devfs_newdirent(ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen);
1307 	de->de_uid = 0;
1308 	de->de_gid = 0;
1309 	de->de_mode = 0755;
1310 	de->de_inode = dmp->dm_inode++;
1311 	de->de_dirent->d_type = DT_LNK;
1312 	i = strlen(ap->a_target) + 1;
1313 	MALLOC(de->de_symlink, char *, i, M_DEVFS, M_WAITOK);
1314 	bcopy(ap->a_target, de->de_symlink, i);
1315 	lockmgr(&dmp->dm_lock, LK_EXCLUSIVE, 0, td);
1316 #ifdef MAC
1317 	mac_create_devfs_symlink(ap->a_cnp->cn_cred, dmp->dm_mount, dd, de);
1318 #endif
1319 	TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
1320 	devfs_allocv(de, ap->a_dvp->v_mount, ap->a_vpp, td);
1321 	lockmgr(&dmp->dm_lock, LK_RELEASE, 0, td);
1322 	return (0);
1323 }
1324 
1325 /*
1326  * Vnode op for write
1327  */
1328 /* ARGSUSED */
1329 static int
1330 devfs_write_f(struct file *fp, struct uio *uio, struct ucred *cred, int flags, struct thread *td)
1331 {
1332 	struct cdev *dev;
1333 	struct vnode *vp;
1334 	int error, ioflag, resid;
1335 	struct cdevsw *dsw;
1336 
1337 	error = devfs_fp_check(fp, &dev, &dsw);
1338 	if (error)
1339 		return (error);
1340 	KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", uio->uio_td, td));
1341 	vp = fp->f_vnode;
1342 	ioflag = IO_UNIT;
1343 	if (fp->f_flag & FNONBLOCK)
1344 		ioflag |= IO_NDELAY;
1345 	if (fp->f_flag & O_DIRECT)
1346 		ioflag |= IO_DIRECT;
1347 	if ((fp->f_flag & O_FSYNC) ||
1348 	    (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
1349 		ioflag |= IO_SYNC;
1350 	if ((flags & FOF_OFFSET) == 0)
1351 		uio->uio_offset = fp->f_offset;
1352 
1353 	resid = uio->uio_resid;
1354 
1355 	if (dsw->d_flags & D_NEEDGIANT)
1356 		mtx_lock(&Giant);
1357 	error = dsw->d_write(dev, uio, ioflag);
1358 	if (dsw->d_flags & D_NEEDGIANT)
1359 		mtx_unlock(&Giant);
1360 	dev_relthread(dev);
1361 	if (uio->uio_resid != resid || (error == 0 && resid != 0)) {
1362 		vfs_timestamp(&dev->si_ctime);
1363 		dev->si_mtime = dev->si_ctime;
1364 	}
1365 
1366 	if ((flags & FOF_OFFSET) == 0)
1367 		fp->f_offset = uio->uio_offset;
1368 	fp->f_nextoff = uio->uio_offset;
1369 	return (error);
1370 }
1371 
1372 static struct vnodeopv_entry_desc devfs_vnodeop_entries[] = {
1373 	{ &vop_default_desc,		(vop_t *) vop_defaultop },
1374 	{ &vop_access_desc,		(vop_t *) devfs_access },
1375 	{ &vop_getattr_desc,		(vop_t *) devfs_getattr },
1376 	{ &vop_ioctl_desc,		(vop_t *) devfs_rioctl },
1377 	{ &vop_lookup_desc,		(vop_t *) devfs_lookup },
1378 	{ &vop_mknod_desc,		(vop_t *) devfs_mknod },
1379 	{ &vop_pathconf_desc,		(vop_t *) devfs_pathconf },
1380 	{ &vop_read_desc,		(vop_t *) devfs_rread },
1381 	{ &vop_readdir_desc,		(vop_t *) devfs_readdir },
1382 	{ &vop_readlink_desc,		(vop_t *) devfs_readlink },
1383 	{ &vop_reclaim_desc,		(vop_t *) devfs_reclaim },
1384 	{ &vop_remove_desc,		(vop_t *) devfs_remove },
1385 	{ &vop_revoke_desc,		(vop_t *) devfs_revoke },
1386 	{ &vop_setattr_desc,		(vop_t *) devfs_setattr },
1387 #ifdef MAC
1388 	{ &vop_setlabel_desc,		(vop_t *) devfs_setlabel },
1389 #endif
1390 	{ &vop_symlink_desc,		(vop_t *) devfs_symlink },
1391 	{ NULL, NULL }
1392 };
1393 
1394 static struct vnodeopv_desc devfs_vnodeop_opv_desc =
1395 	{ &devfs_vnodeop_p, devfs_vnodeop_entries };
1396 
1397 VNODEOP_SET(devfs_vnodeop_opv_desc);
1398 
1399 static struct vnodeopv_entry_desc devfs_specop_entries[] = {
1400 	{ &vop_default_desc,		(vop_t *) vop_defaultop },
1401 	{ &vop_access_desc,		(vop_t *) devfs_access },
1402 	{ &vop_advlock_desc,		(vop_t *) devfs_advlock },
1403 	{ &vop_bmap_desc,		(vop_t *) vop_panic },
1404 	{ &vop_close_desc,		(vop_t *) devfs_close },
1405 	{ &vop_create_desc,		(vop_t *) vop_panic },
1406 	{ &vop_fsync_desc,		(vop_t *) devfs_fsync },
1407 	{ &vop_getattr_desc,		(vop_t *) devfs_getattr },
1408 	{ &vop_lease_desc,		(vop_t *) vop_null },
1409 	{ &vop_link_desc,		(vop_t *) vop_panic },
1410 	{ &vop_mkdir_desc,		(vop_t *) vop_panic },
1411 	{ &vop_mknod_desc,		(vop_t *) vop_panic },
1412 	{ &vop_open_desc,		(vop_t *) devfs_open },
1413 	{ &vop_pathconf_desc,		(vop_t *) devfs_pathconf },
1414 	{ &vop_print_desc,		(vop_t *) devfs_print },
1415 	{ &vop_readdir_desc,		(vop_t *) vop_panic },
1416 	{ &vop_readlink_desc,		(vop_t *) vop_panic },
1417 	{ &vop_reallocblks_desc,	(vop_t *) vop_panic },
1418 	{ &vop_reclaim_desc,		(vop_t *) devfs_reclaim },
1419 	{ &vop_remove_desc,		(vop_t *) devfs_remove },
1420 	{ &vop_rename_desc,		(vop_t *) vop_panic },
1421 	{ &vop_revoke_desc,		(vop_t *) devfs_revoke },
1422 	{ &vop_rmdir_desc,		(vop_t *) vop_panic },
1423 	{ &vop_setattr_desc,		(vop_t *) devfs_setattr },
1424 #ifdef MAC
1425 	{ &vop_setlabel_desc,		(vop_t *) devfs_setlabel },
1426 #endif
1427 	{ &vop_strategy_desc,		(vop_t *) vop_panic },
1428 	{ &vop_symlink_desc,		(vop_t *) vop_panic },
1429 	{ NULL, NULL }
1430 };
1431 
1432 static struct vnodeopv_desc devfs_specop_opv_desc =
1433 	{ &devfs_specop_p, devfs_specop_entries };
1434 
1435 VNODEOP_SET(devfs_specop_opv_desc);
1436