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