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