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