devfs_vnops.c revision 327060
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: stable/10/sys/fs/devfs/devfs_vnops.c 327060 2017-12-21 13:32:49Z kib $
35 */
36
37/*
38 * TODO:
39 *	mkdir: want it ?
40 */
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/conf.h>
45#include <sys/dirent.h>
46#include <sys/fcntl.h>
47#include <sys/file.h>
48#include <sys/filedesc.h>
49#include <sys/filio.h>
50#include <sys/jail.h>
51#include <sys/kernel.h>
52#include <sys/lock.h>
53#include <sys/malloc.h>
54#include <sys/mount.h>
55#include <sys/namei.h>
56#include <sys/priv.h>
57#include <sys/proc.h>
58#include <sys/stat.h>
59#include <sys/sx.h>
60#include <sys/sysctl.h>
61#include <sys/time.h>
62#include <sys/ttycom.h>
63#include <sys/unistd.h>
64#include <sys/vnode.h>
65
66static struct vop_vector devfs_vnodeops;
67static struct vop_vector devfs_specops;
68static struct fileops devfs_ops_f;
69
70#include <fs/devfs/devfs.h>
71#include <fs/devfs/devfs_int.h>
72
73#include <security/mac/mac_framework.h>
74
75static MALLOC_DEFINE(M_CDEVPDATA, "DEVFSP", "Metainfo for cdev-fp data");
76
77struct mtx	devfs_de_interlock;
78MTX_SYSINIT(devfs_de_interlock, &devfs_de_interlock, "devfs interlock", MTX_DEF);
79struct sx	clone_drain_lock;
80SX_SYSINIT(clone_drain_lock, &clone_drain_lock, "clone events drain lock");
81struct mtx	cdevpriv_mtx;
82MTX_SYSINIT(cdevpriv_mtx, &cdevpriv_mtx, "cdevpriv lock", MTX_DEF);
83
84SYSCTL_DECL(_vfs_devfs);
85
86static int devfs_dotimes;
87SYSCTL_INT(_vfs_devfs, OID_AUTO, dotimes, CTLFLAG_RW,
88    &devfs_dotimes, 0, "Update timestamps on DEVFS with default precision");
89
90/*
91 * Update devfs node timestamp.  Note that updates are unlocked and
92 * stat(2) could see partially updated times.
93 */
94static void
95devfs_timestamp(struct timespec *tsp)
96{
97	time_t ts;
98
99	if (devfs_dotimes) {
100		vfs_timestamp(tsp);
101	} else {
102		ts = time_second;
103		if (tsp->tv_sec != ts) {
104			tsp->tv_sec = ts;
105			tsp->tv_nsec = 0;
106		}
107	}
108}
109
110static int
111devfs_fp_check(struct file *fp, struct cdev **devp, struct cdevsw **dswp,
112    int *ref)
113{
114
115	*dswp = devvn_refthread(fp->f_vnode, devp, ref);
116	if (*devp != fp->f_data) {
117		if (*dswp != NULL)
118			dev_relthread(*devp, *ref);
119		return (ENXIO);
120	}
121	KASSERT((*devp)->si_refcount > 0,
122	    ("devfs: un-referenced struct cdev *(%s)", devtoname(*devp)));
123	if (*dswp == NULL)
124		return (ENXIO);
125	curthread->td_fpop = fp;
126	return (0);
127}
128
129int
130devfs_get_cdevpriv(void **datap)
131{
132	struct file *fp;
133	struct cdev_privdata *p;
134	int error;
135
136	fp = curthread->td_fpop;
137	if (fp == NULL)
138		return (EBADF);
139	p = fp->f_cdevpriv;
140	if (p != NULL) {
141		error = 0;
142		*datap = p->cdpd_data;
143	} else
144		error = ENOENT;
145	return (error);
146}
147
148int
149devfs_set_cdevpriv(void *priv, d_priv_dtor_t *priv_dtr)
150{
151	struct file *fp;
152	struct cdev_priv *cdp;
153	struct cdev_privdata *p;
154	int error;
155
156	fp = curthread->td_fpop;
157	if (fp == NULL)
158		return (ENOENT);
159	cdp = cdev2priv((struct cdev *)fp->f_data);
160	p = malloc(sizeof(struct cdev_privdata), M_CDEVPDATA, M_WAITOK);
161	p->cdpd_data = priv;
162	p->cdpd_dtr = priv_dtr;
163	p->cdpd_fp = fp;
164	mtx_lock(&cdevpriv_mtx);
165	if (fp->f_cdevpriv == NULL) {
166		LIST_INSERT_HEAD(&cdp->cdp_fdpriv, p, cdpd_list);
167		fp->f_cdevpriv = p;
168		mtx_unlock(&cdevpriv_mtx);
169		error = 0;
170	} else {
171		mtx_unlock(&cdevpriv_mtx);
172		free(p, M_CDEVPDATA);
173		error = EBUSY;
174	}
175	return (error);
176}
177
178void
179devfs_destroy_cdevpriv(struct cdev_privdata *p)
180{
181
182	mtx_assert(&cdevpriv_mtx, MA_OWNED);
183	p->cdpd_fp->f_cdevpriv = NULL;
184	LIST_REMOVE(p, cdpd_list);
185	mtx_unlock(&cdevpriv_mtx);
186	(p->cdpd_dtr)(p->cdpd_data);
187	free(p, M_CDEVPDATA);
188}
189
190void
191devfs_fpdrop(struct file *fp)
192{
193	struct cdev_privdata *p;
194
195	mtx_lock(&cdevpriv_mtx);
196	if ((p = fp->f_cdevpriv) == NULL) {
197		mtx_unlock(&cdevpriv_mtx);
198		return;
199	}
200	devfs_destroy_cdevpriv(p);
201}
202
203void
204devfs_clear_cdevpriv(void)
205{
206	struct file *fp;
207
208	fp = curthread->td_fpop;
209	if (fp == NULL)
210		return;
211	devfs_fpdrop(fp);
212}
213
214/*
215 * On success devfs_populate_vp() returns with dmp->dm_lock held.
216 */
217static int
218devfs_populate_vp(struct vnode *vp)
219{
220	struct devfs_dirent *de;
221	struct devfs_mount *dmp;
222	int locked;
223
224	ASSERT_VOP_LOCKED(vp, "devfs_populate_vp");
225
226	dmp = VFSTODEVFS(vp->v_mount);
227	locked = VOP_ISLOCKED(vp);
228
229	sx_xlock(&dmp->dm_lock);
230	DEVFS_DMP_HOLD(dmp);
231
232	/* Can't call devfs_populate() with the vnode lock held. */
233	VOP_UNLOCK(vp, 0);
234	devfs_populate(dmp);
235
236	sx_xunlock(&dmp->dm_lock);
237	vn_lock(vp, locked | LK_RETRY);
238	sx_xlock(&dmp->dm_lock);
239	if (DEVFS_DMP_DROP(dmp)) {
240		sx_xunlock(&dmp->dm_lock);
241		devfs_unmount_final(dmp);
242		return (ERESTART);
243	}
244	if ((vp->v_iflag & VI_DOOMED) != 0) {
245		sx_xunlock(&dmp->dm_lock);
246		return (ERESTART);
247	}
248	de = vp->v_data;
249	KASSERT(de != NULL,
250	    ("devfs_populate_vp: vp->v_data == NULL but vnode not doomed"));
251	if ((de->de_flags & DE_DOOMED) != 0) {
252		sx_xunlock(&dmp->dm_lock);
253		return (ERESTART);
254	}
255
256	return (0);
257}
258
259static int
260devfs_vptocnp(struct vop_vptocnp_args *ap)
261{
262	struct vnode *vp = ap->a_vp;
263	struct vnode **dvp = ap->a_vpp;
264	struct devfs_mount *dmp;
265	char *buf = ap->a_buf;
266	int *buflen = ap->a_buflen;
267	struct devfs_dirent *dd, *de;
268	int i, error;
269
270	dmp = VFSTODEVFS(vp->v_mount);
271
272	error = devfs_populate_vp(vp);
273	if (error != 0)
274		return (error);
275
276	i = *buflen;
277	dd = vp->v_data;
278
279	if (vp->v_type == VCHR) {
280		i -= strlen(dd->de_cdp->cdp_c.si_name);
281		if (i < 0) {
282			error = ENOMEM;
283			goto finished;
284		}
285		bcopy(dd->de_cdp->cdp_c.si_name, buf + i,
286		    strlen(dd->de_cdp->cdp_c.si_name));
287		de = dd->de_dir;
288	} else if (vp->v_type == VDIR) {
289		if (dd == dmp->dm_rootdir) {
290			*dvp = vp;
291			vref(*dvp);
292			goto finished;
293		}
294		i -= dd->de_dirent->d_namlen;
295		if (i < 0) {
296			error = ENOMEM;
297			goto finished;
298		}
299		bcopy(dd->de_dirent->d_name, buf + i,
300		    dd->de_dirent->d_namlen);
301		de = dd;
302	} else {
303		error = ENOENT;
304		goto finished;
305	}
306	*buflen = i;
307	de = devfs_parent_dirent(de);
308	if (de == NULL) {
309		error = ENOENT;
310		goto finished;
311	}
312	mtx_lock(&devfs_de_interlock);
313	*dvp = de->de_vnode;
314	if (*dvp != NULL) {
315		VI_LOCK(*dvp);
316		mtx_unlock(&devfs_de_interlock);
317		vholdl(*dvp);
318		VI_UNLOCK(*dvp);
319		vref(*dvp);
320		vdrop(*dvp);
321	} else {
322		mtx_unlock(&devfs_de_interlock);
323		error = ENOENT;
324	}
325finished:
326	sx_xunlock(&dmp->dm_lock);
327	return (error);
328}
329
330/*
331 * Construct the fully qualified path name relative to the mountpoint.
332 * If a NULL cnp is provided, no '/' is appended to the resulting path.
333 */
334char *
335devfs_fqpn(char *buf, struct devfs_mount *dmp, struct devfs_dirent *dd,
336    struct componentname *cnp)
337{
338	int i;
339	struct devfs_dirent *de;
340
341	sx_assert(&dmp->dm_lock, SA_LOCKED);
342
343	i = SPECNAMELEN;
344	buf[i] = '\0';
345	if (cnp != NULL)
346		i -= cnp->cn_namelen;
347	if (i < 0)
348		 return (NULL);
349	if (cnp != NULL)
350		bcopy(cnp->cn_nameptr, buf + i, cnp->cn_namelen);
351	de = dd;
352	while (de != dmp->dm_rootdir) {
353		if (cnp != NULL || i < SPECNAMELEN) {
354			i--;
355			if (i < 0)
356				 return (NULL);
357			buf[i] = '/';
358		}
359		i -= de->de_dirent->d_namlen;
360		if (i < 0)
361			 return (NULL);
362		bcopy(de->de_dirent->d_name, buf + i,
363		    de->de_dirent->d_namlen);
364		de = devfs_parent_dirent(de);
365		if (de == NULL)
366			return (NULL);
367	}
368	return (buf + i);
369}
370
371static int
372devfs_allocv_drop_refs(int drop_dm_lock, struct devfs_mount *dmp,
373	struct devfs_dirent *de)
374{
375	int not_found;
376
377	not_found = 0;
378	if (de->de_flags & DE_DOOMED)
379		not_found = 1;
380	if (DEVFS_DE_DROP(de)) {
381		KASSERT(not_found == 1, ("DEVFS de dropped but not doomed"));
382		devfs_dirent_free(de);
383	}
384	if (DEVFS_DMP_DROP(dmp)) {
385		KASSERT(not_found == 1,
386			("DEVFS mount struct freed before dirent"));
387		not_found = 2;
388		sx_xunlock(&dmp->dm_lock);
389		devfs_unmount_final(dmp);
390	}
391	if (not_found == 1 || (drop_dm_lock && not_found != 2))
392		sx_unlock(&dmp->dm_lock);
393	return (not_found);
394}
395
396static void
397devfs_insmntque_dtr(struct vnode *vp, void *arg)
398{
399	struct devfs_dirent *de;
400
401	de = (struct devfs_dirent *)arg;
402	mtx_lock(&devfs_de_interlock);
403	vp->v_data = NULL;
404	de->de_vnode = NULL;
405	mtx_unlock(&devfs_de_interlock);
406	vgone(vp);
407	vput(vp);
408}
409
410/*
411 * devfs_allocv shall be entered with dmp->dm_lock held, and it drops
412 * it on return.
413 */
414int
415devfs_allocv(struct devfs_dirent *de, struct mount *mp, int lockmode,
416    struct vnode **vpp)
417{
418	int error;
419	struct vnode *vp;
420	struct cdev *dev;
421	struct devfs_mount *dmp;
422	struct cdevsw *dsw;
423
424	dmp = VFSTODEVFS(mp);
425	if (de->de_flags & DE_DOOMED) {
426		sx_xunlock(&dmp->dm_lock);
427		return (ENOENT);
428	}
429loop:
430	DEVFS_DE_HOLD(de);
431	DEVFS_DMP_HOLD(dmp);
432	mtx_lock(&devfs_de_interlock);
433	vp = de->de_vnode;
434	if (vp != NULL) {
435		VI_LOCK(vp);
436		mtx_unlock(&devfs_de_interlock);
437		sx_xunlock(&dmp->dm_lock);
438		vget(vp, lockmode | LK_INTERLOCK | LK_RETRY, curthread);
439		sx_xlock(&dmp->dm_lock);
440		if (devfs_allocv_drop_refs(0, dmp, de)) {
441			vput(vp);
442			return (ENOENT);
443		}
444		else if ((vp->v_iflag & VI_DOOMED) != 0) {
445			mtx_lock(&devfs_de_interlock);
446			if (de->de_vnode == vp) {
447				de->de_vnode = NULL;
448				vp->v_data = NULL;
449			}
450			mtx_unlock(&devfs_de_interlock);
451			vput(vp);
452			goto loop;
453		}
454		sx_xunlock(&dmp->dm_lock);
455		*vpp = vp;
456		return (0);
457	}
458	mtx_unlock(&devfs_de_interlock);
459	if (de->de_dirent->d_type == DT_CHR) {
460		if (!(de->de_cdp->cdp_flags & CDP_ACTIVE)) {
461			devfs_allocv_drop_refs(1, dmp, de);
462			return (ENOENT);
463		}
464		dev = &de->de_cdp->cdp_c;
465	} else {
466		dev = NULL;
467	}
468	error = getnewvnode("devfs", mp, &devfs_vnodeops, &vp);
469	if (error != 0) {
470		devfs_allocv_drop_refs(1, dmp, de);
471		printf("devfs_allocv: failed to allocate new vnode\n");
472		return (error);
473	}
474
475	if (de->de_dirent->d_type == DT_CHR) {
476		vp->v_type = VCHR;
477		VI_LOCK(vp);
478		dev_lock();
479		dev_refl(dev);
480		/* XXX: v_rdev should be protect by vnode lock */
481		vp->v_rdev = dev;
482		KASSERT(vp->v_usecount == 1,
483		    ("%s %d (%d)\n", __func__, __LINE__, vp->v_usecount));
484		dev->si_usecount += vp->v_usecount;
485		/* Special casing of ttys for deadfs.  Probably redundant. */
486		dsw = dev->si_devsw;
487		if (dsw != NULL && (dsw->d_flags & D_TTY) != 0)
488			vp->v_vflag |= VV_ISTTY;
489		dev_unlock();
490		VI_UNLOCK(vp);
491		if ((dev->si_flags & SI_ETERNAL) != 0)
492			vp->v_vflag |= VV_ETERNALDEV;
493		vp->v_op = &devfs_specops;
494	} else if (de->de_dirent->d_type == DT_DIR) {
495		vp->v_type = VDIR;
496	} else if (de->de_dirent->d_type == DT_LNK) {
497		vp->v_type = VLNK;
498	} else {
499		vp->v_type = VBAD;
500	}
501	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_NOWITNESS);
502	VN_LOCK_ASHARE(vp);
503	mtx_lock(&devfs_de_interlock);
504	vp->v_data = de;
505	de->de_vnode = vp;
506	mtx_unlock(&devfs_de_interlock);
507	error = insmntque1(vp, mp, devfs_insmntque_dtr, de);
508	if (error != 0) {
509		(void) devfs_allocv_drop_refs(1, dmp, de);
510		return (error);
511	}
512	if (devfs_allocv_drop_refs(0, dmp, de)) {
513		vput(vp);
514		return (ENOENT);
515	}
516#ifdef MAC
517	mac_devfs_vnode_associate(mp, de, vp);
518#endif
519	sx_xunlock(&dmp->dm_lock);
520	*vpp = vp;
521	return (0);
522}
523
524static int
525devfs_access(struct vop_access_args *ap)
526{
527	struct vnode *vp = ap->a_vp;
528	struct devfs_dirent *de;
529	int error;
530
531	de = vp->v_data;
532	if (vp->v_type == VDIR)
533		de = de->de_dir;
534
535	error = vaccess(vp->v_type, de->de_mode, de->de_uid, de->de_gid,
536	    ap->a_accmode, ap->a_cred, NULL);
537	if (error == 0)
538		return (0);
539	if (error != EACCES)
540		return (error);
541	/* We do, however, allow access to the controlling terminal */
542	if (!(ap->a_td->td_proc->p_flag & P_CONTROLT))
543		return (error);
544	if (ap->a_td->td_proc->p_session->s_ttydp == de->de_cdp)
545		return (0);
546	return (error);
547}
548
549/* ARGSUSED */
550static int
551devfs_close(struct vop_close_args *ap)
552{
553	struct vnode *vp = ap->a_vp, *oldvp;
554	struct thread *td = ap->a_td;
555	struct cdev *dev = vp->v_rdev;
556	struct cdevsw *dsw;
557	int vp_locked, error, ref;
558
559	/*
560	 * XXX: Don't call d_close() if we were called because of
561	 * XXX: insmntque1() failure.
562	 */
563	if (vp->v_data == NULL)
564		return (0);
565
566	/*
567	 * Hack: a tty device that is a controlling terminal
568	 * has a reference from the session structure.
569	 * We cannot easily tell that a character device is
570	 * a controlling terminal, unless it is the closing
571	 * process' controlling terminal.  In that case,
572	 * if the reference count is 2 (this last descriptor
573	 * plus the session), release the reference from the session.
574	 */
575	oldvp = NULL;
576	sx_xlock(&proctree_lock);
577	if (td && vp == td->td_proc->p_session->s_ttyvp) {
578		SESS_LOCK(td->td_proc->p_session);
579		VI_LOCK(vp);
580		if (count_dev(dev) == 2 && (vp->v_iflag & VI_DOOMED) == 0) {
581			td->td_proc->p_session->s_ttyvp = NULL;
582			td->td_proc->p_session->s_ttydp = NULL;
583			oldvp = vp;
584		}
585		VI_UNLOCK(vp);
586		SESS_UNLOCK(td->td_proc->p_session);
587	}
588	sx_xunlock(&proctree_lock);
589	if (oldvp != NULL)
590		vrele(oldvp);
591	/*
592	 * We do not want to really close the device if it
593	 * is still in use unless we are trying to close it
594	 * forcibly. Since every use (buffer, vnode, swap, cmap)
595	 * holds a reference to the vnode, and because we mark
596	 * any other vnodes that alias this device, when the
597	 * sum of the reference counts on all the aliased
598	 * vnodes descends to one, we are on last close.
599	 */
600	dsw = dev_refthread(dev, &ref);
601	if (dsw == NULL)
602		return (ENXIO);
603	VI_LOCK(vp);
604	if (vp->v_iflag & VI_DOOMED) {
605		/* Forced close. */
606	} else if (dsw->d_flags & D_TRACKCLOSE) {
607		/* Keep device updated on status. */
608	} else if (count_dev(dev) > 1) {
609		VI_UNLOCK(vp);
610		dev_relthread(dev, ref);
611		return (0);
612	}
613	vholdl(vp);
614	VI_UNLOCK(vp);
615	vp_locked = VOP_ISLOCKED(vp);
616	VOP_UNLOCK(vp, 0);
617	KASSERT(dev->si_refcount > 0,
618	    ("devfs_close() on un-referenced struct cdev *(%s)", devtoname(dev)));
619	error = dsw->d_close(dev, ap->a_fflag, S_IFCHR, td);
620	dev_relthread(dev, ref);
621	vn_lock(vp, vp_locked | LK_RETRY);
622	vdrop(vp);
623	return (error);
624}
625
626static int
627devfs_close_f(struct file *fp, struct thread *td)
628{
629	int error;
630	struct file *fpop;
631
632	/*
633	 * NB: td may be NULL if this descriptor is closed due to
634	 * garbage collection from a closed UNIX domain socket.
635	 */
636	fpop = curthread->td_fpop;
637	curthread->td_fpop = fp;
638	error = vnops.fo_close(fp, td);
639	curthread->td_fpop = fpop;
640
641	/*
642	 * The f_cdevpriv cannot be assigned non-NULL value while we
643	 * are destroying the file.
644	 */
645	if (fp->f_cdevpriv != NULL)
646		devfs_fpdrop(fp);
647	return (error);
648}
649
650static int
651devfs_fsync(struct vop_fsync_args *ap)
652{
653	int error;
654	struct bufobj *bo;
655	struct devfs_dirent *de;
656
657	if (!vn_isdisk(ap->a_vp, &error)) {
658		bo = &ap->a_vp->v_bufobj;
659		de = ap->a_vp->v_data;
660		if (error == ENXIO && bo->bo_dirty.bv_cnt > 0) {
661			printf("Device %s went missing before all of the data "
662			    "could be written to it; expect data loss.\n",
663			    de->de_dirent->d_name);
664
665			error = vop_stdfsync(ap);
666			if (bo->bo_dirty.bv_cnt != 0 || error != 0)
667				panic("devfs_fsync: vop_stdfsync failed.");
668		}
669
670		return (0);
671	}
672
673	return (vop_stdfsync(ap));
674}
675
676static int
677devfs_getattr(struct vop_getattr_args *ap)
678{
679	struct vnode *vp = ap->a_vp;
680	struct vattr *vap = ap->a_vap;
681	int error;
682	struct devfs_dirent *de;
683	struct devfs_mount *dmp;
684	struct cdev *dev;
685
686	error = devfs_populate_vp(vp);
687	if (error != 0)
688		return (error);
689
690	dmp = VFSTODEVFS(vp->v_mount);
691	sx_xunlock(&dmp->dm_lock);
692
693	de = vp->v_data;
694	KASSERT(de != NULL, ("Null dirent in devfs_getattr vp=%p", vp));
695	if (vp->v_type == VDIR) {
696		de = de->de_dir;
697		KASSERT(de != NULL,
698		    ("Null dir dirent in devfs_getattr vp=%p", vp));
699	}
700	vap->va_uid = de->de_uid;
701	vap->va_gid = de->de_gid;
702	vap->va_mode = de->de_mode;
703	if (vp->v_type == VLNK)
704		vap->va_size = strlen(de->de_symlink);
705	else if (vp->v_type == VDIR)
706		vap->va_size = vap->va_bytes = DEV_BSIZE;
707	else
708		vap->va_size = 0;
709	if (vp->v_type != VDIR)
710		vap->va_bytes = 0;
711	vap->va_blocksize = DEV_BSIZE;
712	vap->va_type = vp->v_type;
713
714#define fix(aa)							\
715	do {							\
716		if ((aa).tv_sec <= 3600) {			\
717			(aa).tv_sec = boottime.tv_sec;		\
718			(aa).tv_nsec = boottime.tv_usec * 1000; \
719		}						\
720	} while (0)
721
722	if (vp->v_type != VCHR)  {
723		fix(de->de_atime);
724		vap->va_atime = de->de_atime;
725		fix(de->de_mtime);
726		vap->va_mtime = de->de_mtime;
727		fix(de->de_ctime);
728		vap->va_ctime = de->de_ctime;
729	} else {
730		dev = vp->v_rdev;
731		fix(dev->si_atime);
732		vap->va_atime = dev->si_atime;
733		fix(dev->si_mtime);
734		vap->va_mtime = dev->si_mtime;
735		fix(dev->si_ctime);
736		vap->va_ctime = dev->si_ctime;
737
738		vap->va_rdev = cdev2priv(dev)->cdp_inode;
739	}
740	vap->va_gen = 0;
741	vap->va_flags = 0;
742	vap->va_filerev = 0;
743	vap->va_nlink = de->de_links;
744	vap->va_fileid = de->de_inode;
745
746	return (error);
747}
748
749/* ARGSUSED */
750static int
751devfs_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred, struct thread *td)
752{
753	struct cdev *dev;
754	struct cdevsw *dsw;
755	struct vnode *vp;
756	struct vnode *vpold;
757	int error, i, ref;
758	const char *p;
759	struct fiodgname_arg *fgn;
760	struct file *fpop;
761
762	fpop = td->td_fpop;
763	error = devfs_fp_check(fp, &dev, &dsw, &ref);
764	if (error != 0) {
765		error = vnops.fo_ioctl(fp, com, data, cred, td);
766		return (error);
767	}
768
769	if (com == FIODTYPE) {
770		*(int *)data = dsw->d_flags & D_TYPEMASK;
771		td->td_fpop = fpop;
772		dev_relthread(dev, ref);
773		return (0);
774	} else if (com == FIODGNAME) {
775		fgn = data;
776		p = devtoname(dev);
777		i = strlen(p) + 1;
778		if (i > fgn->len)
779			error = EINVAL;
780		else
781			error = copyout(p, fgn->buf, i);
782		td->td_fpop = fpop;
783		dev_relthread(dev, ref);
784		return (error);
785	}
786	error = dsw->d_ioctl(dev, com, data, fp->f_flag, td);
787	td->td_fpop = NULL;
788	dev_relthread(dev, ref);
789	if (error == ENOIOCTL)
790		error = ENOTTY;
791	if (error == 0 && com == TIOCSCTTY) {
792		vp = fp->f_vnode;
793
794		/* Do nothing if reassigning same control tty */
795		sx_slock(&proctree_lock);
796		if (td->td_proc->p_session->s_ttyvp == vp) {
797			sx_sunlock(&proctree_lock);
798			return (0);
799		}
800
801		vpold = td->td_proc->p_session->s_ttyvp;
802		VREF(vp);
803		SESS_LOCK(td->td_proc->p_session);
804		td->td_proc->p_session->s_ttyvp = vp;
805		td->td_proc->p_session->s_ttydp = cdev2priv(dev);
806		SESS_UNLOCK(td->td_proc->p_session);
807
808		sx_sunlock(&proctree_lock);
809
810		/* Get rid of reference to old control tty */
811		if (vpold)
812			vrele(vpold);
813	}
814	return (error);
815}
816
817/* ARGSUSED */
818static int
819devfs_kqfilter_f(struct file *fp, struct knote *kn)
820{
821	struct cdev *dev;
822	struct cdevsw *dsw;
823	int error, ref;
824	struct file *fpop;
825	struct thread *td;
826
827	td = curthread;
828	fpop = td->td_fpop;
829	error = devfs_fp_check(fp, &dev, &dsw, &ref);
830	if (error)
831		return (error);
832	error = dsw->d_kqfilter(dev, kn);
833	td->td_fpop = fpop;
834	dev_relthread(dev, ref);
835	return (error);
836}
837
838static inline int
839devfs_prison_check(struct devfs_dirent *de, struct thread *td)
840{
841	struct cdev_priv *cdp;
842	struct ucred *dcr;
843	int error;
844
845	cdp = de->de_cdp;
846	if (cdp == NULL)
847		return (0);
848	dcr = cdp->cdp_c.si_cred;
849	if (dcr == NULL)
850		return (0);
851
852	error = prison_check(td->td_ucred, dcr);
853	if (error == 0)
854		return (0);
855	/* We do, however, allow access to the controlling terminal */
856	if (!(td->td_proc->p_flag & P_CONTROLT))
857		return (error);
858	if (td->td_proc->p_session->s_ttydp == cdp)
859		return (0);
860	return (error);
861}
862
863static int
864devfs_lookupx(struct vop_lookup_args *ap, int *dm_unlock)
865{
866	struct componentname *cnp;
867	struct vnode *dvp, **vpp;
868	struct thread *td;
869	struct devfs_dirent *de, *dd;
870	struct devfs_dirent **dde;
871	struct devfs_mount *dmp;
872	struct mount *mp;
873	struct cdev *cdev;
874	int error, flags, nameiop, dvplocked;
875	char specname[SPECNAMELEN + 1], *pname;
876
877	cnp = ap->a_cnp;
878	vpp = ap->a_vpp;
879	dvp = ap->a_dvp;
880	pname = cnp->cn_nameptr;
881	td = cnp->cn_thread;
882	flags = cnp->cn_flags;
883	nameiop = cnp->cn_nameiop;
884	mp = dvp->v_mount;
885	dmp = VFSTODEVFS(mp);
886	dd = dvp->v_data;
887	*vpp = NULLVP;
888
889	if ((flags & ISLASTCN) && nameiop == RENAME)
890		return (EOPNOTSUPP);
891
892	if (dvp->v_type != VDIR)
893		return (ENOTDIR);
894
895	if ((flags & ISDOTDOT) && (dvp->v_vflag & VV_ROOT))
896		return (EIO);
897
898	error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td);
899	if (error)
900		return (error);
901
902	if (cnp->cn_namelen == 1 && *pname == '.') {
903		if ((flags & ISLASTCN) && nameiop != LOOKUP)
904			return (EINVAL);
905		*vpp = dvp;
906		VREF(dvp);
907		return (0);
908	}
909
910	if (flags & ISDOTDOT) {
911		if ((flags & ISLASTCN) && nameiop != LOOKUP)
912			return (EINVAL);
913		de = devfs_parent_dirent(dd);
914		if (de == NULL)
915			return (ENOENT);
916		dvplocked = VOP_ISLOCKED(dvp);
917		VOP_UNLOCK(dvp, 0);
918		error = devfs_allocv(de, mp, cnp->cn_lkflags & LK_TYPE_MASK,
919		    vpp);
920		*dm_unlock = 0;
921		vn_lock(dvp, dvplocked | LK_RETRY);
922		return (error);
923	}
924
925	dd = dvp->v_data;
926	de = devfs_find(dd, cnp->cn_nameptr, cnp->cn_namelen, 0);
927	while (de == NULL) {	/* While(...) so we can use break */
928
929		if (nameiop == DELETE)
930			return (ENOENT);
931
932		/*
933		 * OK, we didn't have an entry for the name we were asked for
934		 * so we try to see if anybody can create it on demand.
935		 */
936		pname = devfs_fqpn(specname, dmp, dd, cnp);
937		if (pname == NULL)
938			break;
939
940		cdev = NULL;
941		DEVFS_DMP_HOLD(dmp);
942		sx_xunlock(&dmp->dm_lock);
943		sx_slock(&clone_drain_lock);
944		EVENTHANDLER_INVOKE(dev_clone,
945		    td->td_ucred, pname, strlen(pname), &cdev);
946		sx_sunlock(&clone_drain_lock);
947
948		if (cdev == NULL)
949			sx_xlock(&dmp->dm_lock);
950		else if (devfs_populate_vp(dvp) != 0) {
951			*dm_unlock = 0;
952			sx_xlock(&dmp->dm_lock);
953			if (DEVFS_DMP_DROP(dmp)) {
954				sx_xunlock(&dmp->dm_lock);
955				devfs_unmount_final(dmp);
956			} else
957				sx_xunlock(&dmp->dm_lock);
958			dev_rel(cdev);
959			return (ENOENT);
960		}
961		if (DEVFS_DMP_DROP(dmp)) {
962			*dm_unlock = 0;
963			sx_xunlock(&dmp->dm_lock);
964			devfs_unmount_final(dmp);
965			if (cdev != NULL)
966				dev_rel(cdev);
967			return (ENOENT);
968		}
969
970		if (cdev == NULL)
971			break;
972
973		dev_lock();
974		dde = &cdev2priv(cdev)->cdp_dirents[dmp->dm_idx];
975		if (dde != NULL && *dde != NULL)
976			de = *dde;
977		dev_unlock();
978		dev_rel(cdev);
979		break;
980	}
981
982	if (de == NULL || de->de_flags & DE_WHITEOUT) {
983		if ((nameiop == CREATE || nameiop == RENAME) &&
984		    (flags & (LOCKPARENT | WANTPARENT)) && (flags & ISLASTCN)) {
985			cnp->cn_flags |= SAVENAME;
986			return (EJUSTRETURN);
987		}
988		return (ENOENT);
989	}
990
991	if (devfs_prison_check(de, td))
992		return (ENOENT);
993
994	if ((cnp->cn_nameiop == DELETE) && (flags & ISLASTCN)) {
995		error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
996		if (error)
997			return (error);
998		if (*vpp == dvp) {
999			VREF(dvp);
1000			*vpp = dvp;
1001			return (0);
1002		}
1003	}
1004	error = devfs_allocv(de, mp, cnp->cn_lkflags & LK_TYPE_MASK, vpp);
1005	*dm_unlock = 0;
1006	return (error);
1007}
1008
1009static int
1010devfs_lookup(struct vop_lookup_args *ap)
1011{
1012	int j;
1013	struct devfs_mount *dmp;
1014	int dm_unlock;
1015
1016	if (devfs_populate_vp(ap->a_dvp) != 0)
1017		return (ENOTDIR);
1018
1019	dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1020	dm_unlock = 1;
1021	j = devfs_lookupx(ap, &dm_unlock);
1022	if (dm_unlock == 1)
1023		sx_xunlock(&dmp->dm_lock);
1024	return (j);
1025}
1026
1027static int
1028devfs_mknod(struct vop_mknod_args *ap)
1029{
1030	struct componentname *cnp;
1031	struct vnode *dvp, **vpp;
1032	struct devfs_dirent *dd, *de;
1033	struct devfs_mount *dmp;
1034	int error;
1035
1036	/*
1037	 * The only type of node we should be creating here is a
1038	 * character device, for anything else return EOPNOTSUPP.
1039	 */
1040	if (ap->a_vap->va_type != VCHR)
1041		return (EOPNOTSUPP);
1042	dvp = ap->a_dvp;
1043	dmp = VFSTODEVFS(dvp->v_mount);
1044
1045	cnp = ap->a_cnp;
1046	vpp = ap->a_vpp;
1047	dd = dvp->v_data;
1048
1049	error = ENOENT;
1050	sx_xlock(&dmp->dm_lock);
1051	TAILQ_FOREACH(de, &dd->de_dlist, de_list) {
1052		if (cnp->cn_namelen != de->de_dirent->d_namlen)
1053			continue;
1054		if (de->de_dirent->d_type == DT_CHR &&
1055		    (de->de_cdp->cdp_flags & CDP_ACTIVE) == 0)
1056			continue;
1057		if (bcmp(cnp->cn_nameptr, de->de_dirent->d_name,
1058		    de->de_dirent->d_namlen) != 0)
1059			continue;
1060		if (de->de_flags & DE_WHITEOUT)
1061			break;
1062		goto notfound;
1063	}
1064	if (de == NULL)
1065		goto notfound;
1066	de->de_flags &= ~DE_WHITEOUT;
1067	error = devfs_allocv(de, dvp->v_mount, LK_EXCLUSIVE, vpp);
1068	return (error);
1069notfound:
1070	sx_xunlock(&dmp->dm_lock);
1071	return (error);
1072}
1073
1074/* ARGSUSED */
1075static int
1076devfs_open(struct vop_open_args *ap)
1077{
1078	struct thread *td = ap->a_td;
1079	struct vnode *vp = ap->a_vp;
1080	struct cdev *dev = vp->v_rdev;
1081	struct file *fp = ap->a_fp;
1082	int error, ref, vlocked;
1083	struct cdevsw *dsw;
1084	struct file *fpop;
1085	struct mtx *mtxp;
1086
1087	if (vp->v_type == VBLK)
1088		return (ENXIO);
1089
1090	if (dev == NULL)
1091		return (ENXIO);
1092
1093	/* Make this field valid before any I/O in d_open. */
1094	if (dev->si_iosize_max == 0)
1095		dev->si_iosize_max = DFLTPHYS;
1096
1097	dsw = dev_refthread(dev, &ref);
1098	if (dsw == NULL)
1099		return (ENXIO);
1100	if (fp == NULL && dsw->d_fdopen != NULL) {
1101		dev_relthread(dev, ref);
1102		return (ENXIO);
1103	}
1104
1105	vlocked = VOP_ISLOCKED(vp);
1106	VOP_UNLOCK(vp, 0);
1107
1108	fpop = td->td_fpop;
1109	td->td_fpop = fp;
1110	if (fp != NULL) {
1111		fp->f_data = dev;
1112		fp->f_vnode = vp;
1113	}
1114	if (dsw->d_fdopen != NULL)
1115		error = dsw->d_fdopen(dev, ap->a_mode, td, fp);
1116	else
1117		error = dsw->d_open(dev, ap->a_mode, S_IFCHR, td);
1118	/* Clean up any cdevpriv upon error. */
1119	if (error != 0)
1120		devfs_clear_cdevpriv();
1121	td->td_fpop = fpop;
1122
1123	vn_lock(vp, vlocked | LK_RETRY);
1124	dev_relthread(dev, ref);
1125	if (error != 0) {
1126		if (error == ERESTART)
1127			error = EINTR;
1128		return (error);
1129	}
1130
1131#if 0	/* /dev/console */
1132	KASSERT(fp != NULL, ("Could not vnode bypass device on NULL fp"));
1133#else
1134	if (fp == NULL)
1135		return (error);
1136#endif
1137	if (fp->f_ops == &badfileops)
1138		finit(fp, fp->f_flag, DTYPE_VNODE, dev, &devfs_ops_f);
1139	mtxp = mtx_pool_find(mtxpool_sleep, fp);
1140
1141	/*
1142	 * Hint to the dofilewrite() to not force the buffer draining
1143	 * on the writer to the file.  Most likely, the write would
1144	 * not need normal buffers.
1145	 */
1146	mtx_lock(mtxp);
1147	fp->f_vnread_flags |= FDEVFS_VNODE;
1148	mtx_unlock(mtxp);
1149	return (error);
1150}
1151
1152static int
1153devfs_pathconf(struct vop_pathconf_args *ap)
1154{
1155
1156	switch (ap->a_name) {
1157	case _PC_MAC_PRESENT:
1158#ifdef MAC
1159		/*
1160		 * If MAC is enabled, devfs automatically supports
1161		 * trivial non-persistant label storage.
1162		 */
1163		*ap->a_retval = 1;
1164#else
1165		*ap->a_retval = 0;
1166#endif
1167		return (0);
1168	default:
1169		return (vop_stdpathconf(ap));
1170	}
1171	/* NOTREACHED */
1172}
1173
1174/* ARGSUSED */
1175static int
1176devfs_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td)
1177{
1178	struct cdev *dev;
1179	struct cdevsw *dsw;
1180	int error, ref;
1181	struct file *fpop;
1182
1183	fpop = td->td_fpop;
1184	error = devfs_fp_check(fp, &dev, &dsw, &ref);
1185	if (error != 0) {
1186		error = vnops.fo_poll(fp, events, cred, td);
1187		return (error);
1188	}
1189	error = dsw->d_poll(dev, events, td);
1190	td->td_fpop = fpop;
1191	dev_relthread(dev, ref);
1192	return(error);
1193}
1194
1195/*
1196 * Print out the contents of a special device vnode.
1197 */
1198static int
1199devfs_print(struct vop_print_args *ap)
1200{
1201
1202	printf("\tdev %s\n", devtoname(ap->a_vp->v_rdev));
1203	return (0);
1204}
1205
1206static int
1207devfs_read_f(struct file *fp, struct uio *uio, struct ucred *cred,
1208    int flags, struct thread *td)
1209{
1210	struct cdev *dev;
1211	int ioflag, error, ref;
1212	ssize_t resid;
1213	struct cdevsw *dsw;
1214	struct file *fpop;
1215
1216	if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1217		return (EINVAL);
1218	fpop = td->td_fpop;
1219	error = devfs_fp_check(fp, &dev, &dsw, &ref);
1220	if (error != 0) {
1221		error = vnops.fo_read(fp, uio, cred, flags, td);
1222		return (error);
1223	}
1224	resid = uio->uio_resid;
1225	ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT);
1226	if (ioflag & O_DIRECT)
1227		ioflag |= IO_DIRECT;
1228
1229	foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1230	error = dsw->d_read(dev, uio, ioflag);
1231	if (uio->uio_resid != resid || (error == 0 && resid != 0))
1232		devfs_timestamp(&dev->si_atime);
1233	td->td_fpop = fpop;
1234	dev_relthread(dev, ref);
1235
1236	foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF);
1237	return (error);
1238}
1239
1240static int
1241devfs_readdir(struct vop_readdir_args *ap)
1242{
1243	int error;
1244	struct uio *uio;
1245	struct dirent *dp;
1246	struct devfs_dirent *dd;
1247	struct devfs_dirent *de;
1248	struct devfs_mount *dmp;
1249	off_t off;
1250	int *tmp_ncookies = NULL;
1251
1252	if (ap->a_vp->v_type != VDIR)
1253		return (ENOTDIR);
1254
1255	uio = ap->a_uio;
1256	if (uio->uio_offset < 0)
1257		return (EINVAL);
1258
1259	/*
1260	 * XXX: This is a temporary hack to get around this filesystem not
1261	 * supporting cookies. We store the location of the ncookies pointer
1262	 * in a temporary variable before calling vfs_subr.c:vfs_read_dirent()
1263	 * and set the number of cookies to 0. We then set the pointer to
1264	 * NULL so that vfs_read_dirent doesn't try to call realloc() on
1265	 * ap->a_cookies. Later in this function, we restore the ap->a_ncookies
1266	 * pointer to its original location before returning to the caller.
1267	 */
1268	if (ap->a_ncookies != NULL) {
1269		tmp_ncookies = ap->a_ncookies;
1270		*ap->a_ncookies = 0;
1271		ap->a_ncookies = NULL;
1272	}
1273
1274	dmp = VFSTODEVFS(ap->a_vp->v_mount);
1275	if (devfs_populate_vp(ap->a_vp) != 0) {
1276		if (tmp_ncookies != NULL)
1277			ap->a_ncookies = tmp_ncookies;
1278		return (EIO);
1279	}
1280	error = 0;
1281	de = ap->a_vp->v_data;
1282	off = 0;
1283	TAILQ_FOREACH(dd, &de->de_dlist, de_list) {
1284		KASSERT(dd->de_cdp != (void *)0xdeadc0de, ("%s %d\n", __func__, __LINE__));
1285		if (dd->de_flags & (DE_COVERED | DE_WHITEOUT))
1286			continue;
1287		if (devfs_prison_check(dd, uio->uio_td))
1288			continue;
1289		if (dd->de_dirent->d_type == DT_DIR)
1290			de = dd->de_dir;
1291		else
1292			de = dd;
1293		dp = dd->de_dirent;
1294		if (dp->d_reclen > uio->uio_resid)
1295			break;
1296		dp->d_fileno = de->de_inode;
1297		if (off >= uio->uio_offset) {
1298			error = vfs_read_dirent(ap, dp, off);
1299			if (error)
1300				break;
1301		}
1302		off += dp->d_reclen;
1303	}
1304	sx_xunlock(&dmp->dm_lock);
1305	uio->uio_offset = off;
1306
1307	/*
1308	 * Restore ap->a_ncookies if it wasn't originally NULL in the first
1309	 * place.
1310	 */
1311	if (tmp_ncookies != NULL)
1312		ap->a_ncookies = tmp_ncookies;
1313
1314	return (error);
1315}
1316
1317static int
1318devfs_readlink(struct vop_readlink_args *ap)
1319{
1320	struct devfs_dirent *de;
1321
1322	de = ap->a_vp->v_data;
1323	return (uiomove(de->de_symlink, strlen(de->de_symlink), ap->a_uio));
1324}
1325
1326static int
1327devfs_reclaim(struct vop_reclaim_args *ap)
1328{
1329	struct vnode *vp;
1330	struct devfs_dirent *de;
1331
1332	vp = ap->a_vp;
1333	mtx_lock(&devfs_de_interlock);
1334	de = vp->v_data;
1335	if (de != NULL) {
1336		de->de_vnode = NULL;
1337		vp->v_data = NULL;
1338	}
1339	mtx_unlock(&devfs_de_interlock);
1340	vnode_destroy_vobject(vp);
1341	return (0);
1342}
1343
1344static int
1345devfs_reclaim_vchr(struct vop_reclaim_args *ap)
1346{
1347	struct vnode *vp;
1348	struct cdev *dev;
1349
1350	vp = ap->a_vp;
1351	MPASS(vp->v_type == VCHR);
1352
1353	devfs_reclaim(ap);
1354
1355	VI_LOCK(vp);
1356	dev_lock();
1357	dev = vp->v_rdev;
1358	vp->v_rdev = NULL;
1359	if (dev != NULL)
1360		dev->si_usecount -= vp->v_usecount;
1361	dev_unlock();
1362	VI_UNLOCK(vp);
1363	if (dev != NULL)
1364		dev_rel(dev);
1365	return (0);
1366}
1367
1368static int
1369devfs_remove(struct vop_remove_args *ap)
1370{
1371	struct vnode *dvp = ap->a_dvp;
1372	struct vnode *vp = ap->a_vp;
1373	struct devfs_dirent *dd;
1374	struct devfs_dirent *de, *de_covered;
1375	struct devfs_mount *dmp = VFSTODEVFS(vp->v_mount);
1376
1377	ASSERT_VOP_ELOCKED(dvp, "devfs_remove");
1378	ASSERT_VOP_ELOCKED(vp, "devfs_remove");
1379
1380	sx_xlock(&dmp->dm_lock);
1381	dd = ap->a_dvp->v_data;
1382	de = vp->v_data;
1383	if (de->de_cdp == NULL) {
1384		TAILQ_REMOVE(&dd->de_dlist, de, de_list);
1385		if (de->de_dirent->d_type == DT_LNK) {
1386			de_covered = devfs_find(dd, de->de_dirent->d_name,
1387			    de->de_dirent->d_namlen, 0);
1388			if (de_covered != NULL)
1389				de_covered->de_flags &= ~DE_COVERED;
1390		}
1391		/* We need to unlock dvp because devfs_delete() may lock it. */
1392		VOP_UNLOCK(vp, 0);
1393		if (dvp != vp)
1394			VOP_UNLOCK(dvp, 0);
1395		devfs_delete(dmp, de, 0);
1396		sx_xunlock(&dmp->dm_lock);
1397		if (dvp != vp)
1398			vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1399		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1400	} else {
1401		de->de_flags |= DE_WHITEOUT;
1402		sx_xunlock(&dmp->dm_lock);
1403	}
1404	return (0);
1405}
1406
1407/*
1408 * Revoke is called on a tty when a terminal session ends.  The vnode
1409 * is orphaned by setting v_op to deadfs so we need to let go of it
1410 * as well so that we create a new one next time around.
1411 *
1412 */
1413static int
1414devfs_revoke(struct vop_revoke_args *ap)
1415{
1416	struct vnode *vp = ap->a_vp, *vp2;
1417	struct cdev *dev;
1418	struct cdev_priv *cdp;
1419	struct devfs_dirent *de;
1420	u_int i;
1421
1422	KASSERT((ap->a_flags & REVOKEALL) != 0, ("devfs_revoke !REVOKEALL"));
1423
1424	dev = vp->v_rdev;
1425	cdp = cdev2priv(dev);
1426
1427	dev_lock();
1428	cdp->cdp_inuse++;
1429	dev_unlock();
1430
1431	vhold(vp);
1432	vgone(vp);
1433	vdrop(vp);
1434
1435	VOP_UNLOCK(vp,0);
1436 loop:
1437	for (;;) {
1438		mtx_lock(&devfs_de_interlock);
1439		dev_lock();
1440		vp2 = NULL;
1441		for (i = 0; i <= cdp->cdp_maxdirent; i++) {
1442			de = cdp->cdp_dirents[i];
1443			if (de == NULL)
1444				continue;
1445
1446			vp2 = de->de_vnode;
1447			if (vp2 != NULL) {
1448				dev_unlock();
1449				VI_LOCK(vp2);
1450				mtx_unlock(&devfs_de_interlock);
1451				if (vget(vp2, LK_EXCLUSIVE | LK_INTERLOCK,
1452				    curthread))
1453					goto loop;
1454				vhold(vp2);
1455				vgone(vp2);
1456				vdrop(vp2);
1457				vput(vp2);
1458				break;
1459			}
1460		}
1461		if (vp2 != NULL) {
1462			continue;
1463		}
1464		dev_unlock();
1465		mtx_unlock(&devfs_de_interlock);
1466		break;
1467	}
1468	dev_lock();
1469	cdp->cdp_inuse--;
1470	if (!(cdp->cdp_flags & CDP_ACTIVE) && cdp->cdp_inuse == 0) {
1471		TAILQ_REMOVE(&cdevp_list, cdp, cdp_list);
1472		dev_unlock();
1473		dev_rel(&cdp->cdp_c);
1474	} else
1475		dev_unlock();
1476
1477	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1478	return (0);
1479}
1480
1481static int
1482devfs_rioctl(struct vop_ioctl_args *ap)
1483{
1484	struct vnode *vp;
1485	struct devfs_mount *dmp;
1486	int error;
1487
1488	vp = ap->a_vp;
1489	vn_lock(vp, LK_SHARED | LK_RETRY);
1490	if (vp->v_iflag & VI_DOOMED) {
1491		VOP_UNLOCK(vp, 0);
1492		return (EBADF);
1493	}
1494	dmp = VFSTODEVFS(vp->v_mount);
1495	sx_xlock(&dmp->dm_lock);
1496	VOP_UNLOCK(vp, 0);
1497	DEVFS_DMP_HOLD(dmp);
1498	devfs_populate(dmp);
1499	if (DEVFS_DMP_DROP(dmp)) {
1500		sx_xunlock(&dmp->dm_lock);
1501		devfs_unmount_final(dmp);
1502		return (ENOENT);
1503	}
1504	error = devfs_rules_ioctl(dmp, ap->a_command, ap->a_data, ap->a_td);
1505	sx_xunlock(&dmp->dm_lock);
1506	return (error);
1507}
1508
1509static int
1510devfs_rread(struct vop_read_args *ap)
1511{
1512
1513	if (ap->a_vp->v_type != VDIR)
1514		return (EINVAL);
1515	return (VOP_READDIR(ap->a_vp, ap->a_uio, ap->a_cred, NULL, NULL, NULL));
1516}
1517
1518static int
1519devfs_setattr(struct vop_setattr_args *ap)
1520{
1521	struct devfs_dirent *de;
1522	struct vattr *vap;
1523	struct vnode *vp;
1524	struct thread *td;
1525	int c, error;
1526	uid_t uid;
1527	gid_t gid;
1528
1529	vap = ap->a_vap;
1530	vp = ap->a_vp;
1531	td = curthread;
1532	if ((vap->va_type != VNON) ||
1533	    (vap->va_nlink != VNOVAL) ||
1534	    (vap->va_fsid != VNOVAL) ||
1535	    (vap->va_fileid != VNOVAL) ||
1536	    (vap->va_blocksize != VNOVAL) ||
1537	    (vap->va_flags != VNOVAL && vap->va_flags != 0) ||
1538	    (vap->va_rdev != VNOVAL) ||
1539	    ((int)vap->va_bytes != VNOVAL) ||
1540	    (vap->va_gen != VNOVAL)) {
1541		return (EINVAL);
1542	}
1543
1544	error = devfs_populate_vp(vp);
1545	if (error != 0)
1546		return (error);
1547
1548	de = vp->v_data;
1549	if (vp->v_type == VDIR)
1550		de = de->de_dir;
1551
1552	c = 0;
1553	if (vap->va_uid == (uid_t)VNOVAL)
1554		uid = de->de_uid;
1555	else
1556		uid = vap->va_uid;
1557	if (vap->va_gid == (gid_t)VNOVAL)
1558		gid = de->de_gid;
1559	else
1560		gid = vap->va_gid;
1561	if (uid != de->de_uid || gid != de->de_gid) {
1562		if ((ap->a_cred->cr_uid != de->de_uid) || uid != de->de_uid ||
1563		    (gid != de->de_gid && !groupmember(gid, ap->a_cred))) {
1564			error = priv_check(td, PRIV_VFS_CHOWN);
1565			if (error != 0)
1566				goto ret;
1567		}
1568		de->de_uid = uid;
1569		de->de_gid = gid;
1570		c = 1;
1571	}
1572
1573	if (vap->va_mode != (mode_t)VNOVAL) {
1574		if (ap->a_cred->cr_uid != de->de_uid) {
1575			error = priv_check(td, PRIV_VFS_ADMIN);
1576			if (error != 0)
1577				goto ret;
1578		}
1579		de->de_mode = vap->va_mode;
1580		c = 1;
1581	}
1582
1583	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1584		error = vn_utimes_perm(vp, vap, ap->a_cred, td);
1585		if (error != 0)
1586			goto ret;
1587		if (vap->va_atime.tv_sec != VNOVAL) {
1588			if (vp->v_type == VCHR)
1589				vp->v_rdev->si_atime = vap->va_atime;
1590			else
1591				de->de_atime = vap->va_atime;
1592		}
1593		if (vap->va_mtime.tv_sec != VNOVAL) {
1594			if (vp->v_type == VCHR)
1595				vp->v_rdev->si_mtime = vap->va_mtime;
1596			else
1597				de->de_mtime = vap->va_mtime;
1598		}
1599		c = 1;
1600	}
1601
1602	if (c) {
1603		if (vp->v_type == VCHR)
1604			vfs_timestamp(&vp->v_rdev->si_ctime);
1605		else
1606			vfs_timestamp(&de->de_mtime);
1607	}
1608
1609ret:
1610	sx_xunlock(&VFSTODEVFS(vp->v_mount)->dm_lock);
1611	return (error);
1612}
1613
1614#ifdef MAC
1615static int
1616devfs_setlabel(struct vop_setlabel_args *ap)
1617{
1618	struct vnode *vp;
1619	struct devfs_dirent *de;
1620
1621	vp = ap->a_vp;
1622	de = vp->v_data;
1623
1624	mac_vnode_relabel(ap->a_cred, vp, ap->a_label);
1625	mac_devfs_update(vp->v_mount, de, vp);
1626
1627	return (0);
1628}
1629#endif
1630
1631static int
1632devfs_stat_f(struct file *fp, struct stat *sb, struct ucred *cred, struct thread *td)
1633{
1634
1635	return (vnops.fo_stat(fp, sb, cred, td));
1636}
1637
1638static int
1639devfs_symlink(struct vop_symlink_args *ap)
1640{
1641	int i, error;
1642	struct devfs_dirent *dd;
1643	struct devfs_dirent *de, *de_covered, *de_dotdot;
1644	struct devfs_mount *dmp;
1645
1646	error = priv_check(curthread, PRIV_DEVFS_SYMLINK);
1647	if (error)
1648		return(error);
1649	dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1650	if (devfs_populate_vp(ap->a_dvp) != 0)
1651		return (ENOENT);
1652
1653	dd = ap->a_dvp->v_data;
1654	de = devfs_newdirent(ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen);
1655	de->de_flags = DE_USER;
1656	de->de_uid = 0;
1657	de->de_gid = 0;
1658	de->de_mode = 0755;
1659	de->de_inode = alloc_unr(devfs_inos);
1660	de->de_dir = dd;
1661	de->de_dirent->d_type = DT_LNK;
1662	i = strlen(ap->a_target) + 1;
1663	de->de_symlink = malloc(i, M_DEVFS, M_WAITOK);
1664	bcopy(ap->a_target, de->de_symlink, i);
1665#ifdef MAC
1666	mac_devfs_create_symlink(ap->a_cnp->cn_cred, dmp->dm_mount, dd, de);
1667#endif
1668	de_covered = devfs_find(dd, de->de_dirent->d_name,
1669	    de->de_dirent->d_namlen, 0);
1670	if (de_covered != NULL) {
1671		if ((de_covered->de_flags & DE_USER) != 0) {
1672			devfs_delete(dmp, de, DEVFS_DEL_NORECURSE);
1673			sx_xunlock(&dmp->dm_lock);
1674			return (EEXIST);
1675		}
1676		KASSERT((de_covered->de_flags & DE_COVERED) == 0,
1677		    ("devfs_symlink: entry %p already covered", de_covered));
1678		de_covered->de_flags |= DE_COVERED;
1679	}
1680
1681	de_dotdot = TAILQ_FIRST(&dd->de_dlist);		/* "." */
1682	de_dotdot = TAILQ_NEXT(de_dotdot, de_list);	/* ".." */
1683	TAILQ_INSERT_AFTER(&dd->de_dlist, de_dotdot, de, de_list);
1684	devfs_dir_ref_de(dmp, dd);
1685	devfs_rules_apply(dmp, de);
1686
1687	return (devfs_allocv(de, ap->a_dvp->v_mount, LK_EXCLUSIVE, ap->a_vpp));
1688}
1689
1690static int
1691devfs_truncate_f(struct file *fp, off_t length, struct ucred *cred, struct thread *td)
1692{
1693
1694	return (vnops.fo_truncate(fp, length, cred, td));
1695}
1696
1697static int
1698devfs_write_f(struct file *fp, struct uio *uio, struct ucred *cred,
1699    int flags, struct thread *td)
1700{
1701	struct cdev *dev;
1702	int error, ioflag, ref;
1703	ssize_t resid;
1704	struct cdevsw *dsw;
1705	struct file *fpop;
1706
1707	if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1708		return (EINVAL);
1709	fpop = td->td_fpop;
1710	error = devfs_fp_check(fp, &dev, &dsw, &ref);
1711	if (error != 0) {
1712		error = vnops.fo_write(fp, uio, cred, flags, td);
1713		return (error);
1714	}
1715	KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", uio->uio_td, td));
1716	ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT | O_FSYNC);
1717	if (ioflag & O_DIRECT)
1718		ioflag |= IO_DIRECT;
1719	foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1720
1721	resid = uio->uio_resid;
1722
1723	error = dsw->d_write(dev, uio, ioflag);
1724	if (uio->uio_resid != resid || (error == 0 && resid != 0)) {
1725		devfs_timestamp(&dev->si_ctime);
1726		dev->si_mtime = dev->si_ctime;
1727	}
1728	td->td_fpop = fpop;
1729	dev_relthread(dev, ref);
1730
1731	foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF);
1732	return (error);
1733}
1734
1735dev_t
1736dev2udev(struct cdev *x)
1737{
1738	if (x == NULL)
1739		return (NODEV);
1740	return (cdev2priv(x)->cdp_inode);
1741}
1742
1743static struct fileops devfs_ops_f = {
1744	.fo_read =	devfs_read_f,
1745	.fo_write =	devfs_write_f,
1746	.fo_truncate =	devfs_truncate_f,
1747	.fo_ioctl =	devfs_ioctl_f,
1748	.fo_poll =	devfs_poll_f,
1749	.fo_kqfilter =	devfs_kqfilter_f,
1750	.fo_stat =	devfs_stat_f,
1751	.fo_close =	devfs_close_f,
1752	.fo_chmod =	vn_chmod,
1753	.fo_chown =	vn_chown,
1754	.fo_sendfile =	vn_sendfile,
1755	.fo_seek =	vn_seek,
1756	.fo_flags =	DFLAG_PASSABLE | DFLAG_SEEKABLE
1757};
1758
1759static struct vop_vector devfs_vnodeops = {
1760	.vop_default =		&default_vnodeops,
1761
1762	.vop_access =		devfs_access,
1763	.vop_getattr =		devfs_getattr,
1764	.vop_ioctl =		devfs_rioctl,
1765	.vop_lookup =		devfs_lookup,
1766	.vop_mknod =		devfs_mknod,
1767	.vop_pathconf =		devfs_pathconf,
1768	.vop_read =		devfs_rread,
1769	.vop_readdir =		devfs_readdir,
1770	.vop_readlink =		devfs_readlink,
1771	.vop_reclaim =		devfs_reclaim,
1772	.vop_remove =		devfs_remove,
1773	.vop_revoke =		devfs_revoke,
1774	.vop_setattr =		devfs_setattr,
1775#ifdef MAC
1776	.vop_setlabel =		devfs_setlabel,
1777#endif
1778	.vop_symlink =		devfs_symlink,
1779	.vop_vptocnp =		devfs_vptocnp,
1780};
1781
1782static struct vop_vector devfs_specops = {
1783	.vop_default =		&default_vnodeops,
1784
1785	.vop_access =		devfs_access,
1786	.vop_bmap =		VOP_PANIC,
1787	.vop_close =		devfs_close,
1788	.vop_create =		VOP_PANIC,
1789	.vop_fsync =		devfs_fsync,
1790	.vop_getattr =		devfs_getattr,
1791	.vop_link =		VOP_PANIC,
1792	.vop_mkdir =		VOP_PANIC,
1793	.vop_mknod =		VOP_PANIC,
1794	.vop_open =		devfs_open,
1795	.vop_pathconf =		devfs_pathconf,
1796	.vop_poll =		dead_poll,
1797	.vop_print =		devfs_print,
1798	.vop_read =		dead_read,
1799	.vop_readdir =		VOP_PANIC,
1800	.vop_readlink =		VOP_PANIC,
1801	.vop_reallocblks =	VOP_PANIC,
1802	.vop_reclaim =		devfs_reclaim_vchr,
1803	.vop_remove =		devfs_remove,
1804	.vop_rename =		VOP_PANIC,
1805	.vop_revoke =		devfs_revoke,
1806	.vop_rmdir =		VOP_PANIC,
1807	.vop_setattr =		devfs_setattr,
1808#ifdef MAC
1809	.vop_setlabel =		devfs_setlabel,
1810#endif
1811	.vop_strategy =		VOP_PANIC,
1812	.vop_symlink =		VOP_PANIC,
1813	.vop_vptocnp =		devfs_vptocnp,
1814	.vop_write =		dead_write,
1815};
1816
1817/*
1818 * Our calling convention to the device drivers used to be that we passed
1819 * vnode.h IO_* flags to read()/write(), but we're moving to fcntl.h O_
1820 * flags instead since that's what open(), close() and ioctl() takes and
1821 * we don't really want vnode.h in device drivers.
1822 * We solved the source compatibility by redefining some vnode flags to
1823 * be the same as the fcntl ones and by sending down the bitwise OR of
1824 * the respective fcntl/vnode flags.  These CTASSERTS make sure nobody
1825 * pulls the rug out under this.
1826 */
1827CTASSERT(O_NONBLOCK == IO_NDELAY);
1828CTASSERT(O_FSYNC == IO_SYNC);
1829