1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2000-2004
5 * Poul-Henning Kamp. All rights reserved.
6 * Copyright (c) 1989, 1992-1993, 1995
7 * The Regents of the University of California. All rights reserved.
8 *
9 * This code is derived from software donated to Berkeley by
10 * Jan-Simon Pendry.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)kernfs_vnops.c 8.15 (Berkeley) 5/21/95
34 * From: FreeBSD: src/sys/miscfs/kernfs/kernfs_vnops.c 1.43
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/eventhandler.h>
47 #include <sys/fcntl.h>
48 #include <sys/file.h>
49 #include <sys/filedesc.h>
50 #include <sys/filio.h>
51 #include <sys/jail.h>
52 #include <sys/kernel.h>
53 #include <sys/limits.h>
54 #include <sys/lock.h>
55 #include <sys/malloc.h>
56 #include <sys/mman.h>
57 #include <sys/mount.h>
58 #include <sys/namei.h>
59 #include <sys/priv.h>
60 #include <sys/proc.h>
61 #include <sys/stat.h>
62 #include <sys/sx.h>
63 #include <sys/sysctl.h>
64 #include <sys/time.h>
65 #include <sys/ttycom.h>
66 #include <sys/unistd.h>
67 #include <sys/vnode.h>
68
69 static struct vop_vector devfs_vnodeops;
70 static struct vop_vector devfs_specops;
71 static struct fileops devfs_ops_f;
72
73 #include <fs/devfs/devfs.h>
74 #include <fs/devfs/devfs_int.h>
75
76 #include <security/mac/mac_framework.h>
77
78 #include <vm/vm.h>
79 #include <vm/vm_extern.h>
80 #include <vm/vm_object.h>
81
82 static MALLOC_DEFINE(M_CDEVPDATA, "DEVFSP", "Metainfo for cdev-fp data");
83
84 struct mtx devfs_de_interlock;
85 MTX_SYSINIT(devfs_de_interlock, &devfs_de_interlock, "devfs interlock", MTX_DEF);
86 struct sx clone_drain_lock;
87 SX_SYSINIT(clone_drain_lock, &clone_drain_lock, "clone events drain lock");
88 struct mtx cdevpriv_mtx;
89 MTX_SYSINIT(cdevpriv_mtx, &cdevpriv_mtx, "cdevpriv lock", MTX_DEF);
90
91 SYSCTL_DECL(_vfs_devfs);
92
93 static int devfs_dotimes;
94 SYSCTL_INT(_vfs_devfs, OID_AUTO, dotimes, CTLFLAG_RW,
95 &devfs_dotimes, 0, "Update timestamps on DEVFS with default precision");
96
97 /*
98 * Update devfs node timestamp. Note that updates are unlocked and
99 * stat(2) could see partially updated times.
100 */
101 static void
devfs_timestamp(struct timespec * tsp)102 devfs_timestamp(struct timespec *tsp)
103 {
104 time_t ts;
105
106 if (devfs_dotimes) {
107 vfs_timestamp(tsp);
108 } else {
109 ts = time_second;
110 if (tsp->tv_sec != ts) {
111 tsp->tv_sec = ts;
112 tsp->tv_nsec = 0;
113 }
114 }
115 }
116
117 static int
devfs_fp_check(struct file * fp,struct cdev ** devp,struct cdevsw ** dswp,int * ref)118 devfs_fp_check(struct file *fp, struct cdev **devp, struct cdevsw **dswp,
119 int *ref)
120 {
121
122 *dswp = devvn_refthread(fp->f_vnode, devp, ref);
123 if (*devp != fp->f_data) {
124 if (*dswp != NULL)
125 dev_relthread(*devp, *ref);
126 return (ENXIO);
127 }
128 KASSERT((*devp)->si_refcount > 0,
129 ("devfs: un-referenced struct cdev *(%s)", devtoname(*devp)));
130 if (*dswp == NULL)
131 return (ENXIO);
132 curthread->td_fpop = fp;
133 return (0);
134 }
135
136 int
devfs_get_cdevpriv(void ** datap)137 devfs_get_cdevpriv(void **datap)
138 {
139 struct file *fp;
140 struct cdev_privdata *p;
141 int error;
142
143 fp = curthread->td_fpop;
144 if (fp == NULL)
145 return (EBADF);
146 p = fp->f_cdevpriv;
147 if (p != NULL) {
148 error = 0;
149 *datap = p->cdpd_data;
150 } else
151 error = ENOENT;
152 return (error);
153 }
154
155 int
devfs_set_cdevpriv(void * priv,d_priv_dtor_t * priv_dtr)156 devfs_set_cdevpriv(void *priv, d_priv_dtor_t *priv_dtr)
157 {
158 struct file *fp;
159 struct cdev_priv *cdp;
160 struct cdev_privdata *p;
161 int error;
162
163 fp = curthread->td_fpop;
164 if (fp == NULL)
165 return (ENOENT);
166 cdp = cdev2priv((struct cdev *)fp->f_data);
167 p = malloc(sizeof(struct cdev_privdata), M_CDEVPDATA, M_WAITOK);
168 p->cdpd_data = priv;
169 p->cdpd_dtr = priv_dtr;
170 p->cdpd_fp = fp;
171 mtx_lock(&cdevpriv_mtx);
172 if (fp->f_cdevpriv == NULL) {
173 LIST_INSERT_HEAD(&cdp->cdp_fdpriv, p, cdpd_list);
174 fp->f_cdevpriv = p;
175 mtx_unlock(&cdevpriv_mtx);
176 error = 0;
177 } else {
178 mtx_unlock(&cdevpriv_mtx);
179 free(p, M_CDEVPDATA);
180 error = EBUSY;
181 }
182 return (error);
183 }
184
185 int
devfs_foreach_cdevpriv(struct cdev * dev,int (* cb)(void * data,void * arg),void * arg)186 devfs_foreach_cdevpriv(struct cdev *dev, int (*cb)(void *data, void *arg),
187 void *arg)
188 {
189 struct cdev_priv *cdp;
190 struct cdev_privdata *p;
191 int error;
192
193 cdp = cdev2priv(dev);
194 error = 0;
195 mtx_lock(&cdevpriv_mtx);
196 LIST_FOREACH(p, &cdp->cdp_fdpriv, cdpd_list) {
197 error = cb(p->cdpd_data, arg);
198 if (error != 0)
199 break;
200 }
201 mtx_unlock(&cdevpriv_mtx);
202 return (error);
203 }
204
205 void
devfs_destroy_cdevpriv(struct cdev_privdata * p)206 devfs_destroy_cdevpriv(struct cdev_privdata *p)
207 {
208
209 mtx_assert(&cdevpriv_mtx, MA_OWNED);
210 KASSERT(p->cdpd_fp->f_cdevpriv == p,
211 ("devfs_destoy_cdevpriv %p != %p", p->cdpd_fp->f_cdevpriv, p));
212 p->cdpd_fp->f_cdevpriv = NULL;
213 LIST_REMOVE(p, cdpd_list);
214 mtx_unlock(&cdevpriv_mtx);
215 (p->cdpd_dtr)(p->cdpd_data);
216 free(p, M_CDEVPDATA);
217 }
218
219 static void
devfs_fpdrop(struct file * fp)220 devfs_fpdrop(struct file *fp)
221 {
222 struct cdev_privdata *p;
223
224 mtx_lock(&cdevpriv_mtx);
225 if ((p = fp->f_cdevpriv) == NULL) {
226 mtx_unlock(&cdevpriv_mtx);
227 return;
228 }
229 devfs_destroy_cdevpriv(p);
230 }
231
232 void
devfs_clear_cdevpriv(void)233 devfs_clear_cdevpriv(void)
234 {
235 struct file *fp;
236
237 fp = curthread->td_fpop;
238 if (fp == NULL)
239 return;
240 devfs_fpdrop(fp);
241 }
242
243 static void
devfs_usecount_add(struct vnode * vp)244 devfs_usecount_add(struct vnode *vp)
245 {
246 struct devfs_dirent *de;
247 struct cdev *dev;
248
249 mtx_lock(&devfs_de_interlock);
250 VI_LOCK(vp);
251 VNPASS(vp->v_type == VCHR || vp->v_type == VBAD, vp);
252 if (VN_IS_DOOMED(vp)) {
253 goto out_unlock;
254 }
255
256 de = vp->v_data;
257 dev = vp->v_rdev;
258 MPASS(de != NULL);
259 MPASS(dev != NULL);
260 dev->si_usecount++;
261 de->de_usecount++;
262 out_unlock:
263 VI_UNLOCK(vp);
264 mtx_unlock(&devfs_de_interlock);
265 }
266
267 static void
devfs_usecount_subl(struct vnode * vp)268 devfs_usecount_subl(struct vnode *vp)
269 {
270 struct devfs_dirent *de;
271 struct cdev *dev;
272
273 mtx_assert(&devfs_de_interlock, MA_OWNED);
274 ASSERT_VI_LOCKED(vp, __func__);
275 VNPASS(vp->v_type == VCHR || vp->v_type == VBAD, vp);
276
277 de = vp->v_data;
278 dev = vp->v_rdev;
279 if (de == NULL)
280 return;
281 if (dev == NULL) {
282 MPASS(de->de_usecount == 0);
283 return;
284 }
285 if (dev->si_usecount < de->de_usecount)
286 panic("%s: si_usecount underflow for dev %p "
287 "(has %ld, dirent has %d)\n",
288 __func__, dev, dev->si_usecount, de->de_usecount);
289 if (VN_IS_DOOMED(vp)) {
290 dev->si_usecount -= de->de_usecount;
291 de->de_usecount = 0;
292 } else {
293 if (de->de_usecount == 0)
294 panic("%s: de_usecount underflow for dev %p\n",
295 __func__, dev);
296 dev->si_usecount--;
297 de->de_usecount--;
298 }
299 }
300
301 static void
devfs_usecount_sub(struct vnode * vp)302 devfs_usecount_sub(struct vnode *vp)
303 {
304
305 mtx_lock(&devfs_de_interlock);
306 VI_LOCK(vp);
307 devfs_usecount_subl(vp);
308 VI_UNLOCK(vp);
309 mtx_unlock(&devfs_de_interlock);
310 }
311
312 static int
devfs_usecountl(struct vnode * vp)313 devfs_usecountl(struct vnode *vp)
314 {
315
316 VNPASS(vp->v_type == VCHR, vp);
317 mtx_assert(&devfs_de_interlock, MA_OWNED);
318 ASSERT_VI_LOCKED(vp, __func__);
319 return (vp->v_rdev->si_usecount);
320 }
321
322 int
devfs_usecount(struct vnode * vp)323 devfs_usecount(struct vnode *vp)
324 {
325 int count;
326
327 VNPASS(vp->v_type == VCHR, vp);
328 mtx_lock(&devfs_de_interlock);
329 VI_LOCK(vp);
330 count = devfs_usecountl(vp);
331 VI_UNLOCK(vp);
332 mtx_unlock(&devfs_de_interlock);
333 return (count);
334 }
335
336 void
devfs_ctty_ref(struct vnode * vp)337 devfs_ctty_ref(struct vnode *vp)
338 {
339
340 vrefact(vp);
341 devfs_usecount_add(vp);
342 }
343
344 void
devfs_ctty_unref(struct vnode * vp)345 devfs_ctty_unref(struct vnode *vp)
346 {
347
348 devfs_usecount_sub(vp);
349 vrele(vp);
350 }
351
352 /*
353 * On success devfs_populate_vp() returns with dmp->dm_lock held.
354 */
355 static int
devfs_populate_vp(struct vnode * vp)356 devfs_populate_vp(struct vnode *vp)
357 {
358 struct devfs_dirent *de;
359 struct devfs_mount *dmp;
360 int locked;
361
362 ASSERT_VOP_LOCKED(vp, "devfs_populate_vp");
363
364 dmp = VFSTODEVFS(vp->v_mount);
365 if (!devfs_populate_needed(dmp)) {
366 sx_xlock(&dmp->dm_lock);
367 goto out_nopopulate;
368 }
369
370 locked = VOP_ISLOCKED(vp);
371
372 sx_xlock(&dmp->dm_lock);
373 DEVFS_DMP_HOLD(dmp);
374
375 /* Can't call devfs_populate() with the vnode lock held. */
376 VOP_UNLOCK(vp);
377 devfs_populate(dmp);
378
379 sx_xunlock(&dmp->dm_lock);
380 vn_lock(vp, locked | LK_RETRY);
381 sx_xlock(&dmp->dm_lock);
382 if (DEVFS_DMP_DROP(dmp)) {
383 sx_xunlock(&dmp->dm_lock);
384 devfs_unmount_final(dmp);
385 return (ERESTART);
386 }
387 out_nopopulate:
388 if (VN_IS_DOOMED(vp)) {
389 sx_xunlock(&dmp->dm_lock);
390 return (ERESTART);
391 }
392 de = vp->v_data;
393 KASSERT(de != NULL,
394 ("devfs_populate_vp: vp->v_data == NULL but vnode not doomed"));
395 if ((de->de_flags & DE_DOOMED) != 0) {
396 sx_xunlock(&dmp->dm_lock);
397 return (ERESTART);
398 }
399
400 return (0);
401 }
402
403 static int
devfs_vptocnp(struct vop_vptocnp_args * ap)404 devfs_vptocnp(struct vop_vptocnp_args *ap)
405 {
406 struct vnode *vp = ap->a_vp;
407 struct vnode **dvp = ap->a_vpp;
408 struct devfs_mount *dmp;
409 char *buf = ap->a_buf;
410 size_t *buflen = ap->a_buflen;
411 struct devfs_dirent *dd, *de;
412 int i, error;
413
414 dmp = VFSTODEVFS(vp->v_mount);
415
416 error = devfs_populate_vp(vp);
417 if (error != 0)
418 return (error);
419
420 if (vp->v_type != VCHR && vp->v_type != VDIR) {
421 error = ENOENT;
422 goto finished;
423 }
424
425 dd = vp->v_data;
426 if (vp->v_type == VDIR && dd == dmp->dm_rootdir) {
427 *dvp = vp;
428 vref(*dvp);
429 goto finished;
430 }
431
432 i = *buflen;
433 i -= dd->de_dirent->d_namlen;
434 if (i < 0) {
435 error = ENOMEM;
436 goto finished;
437 }
438 bcopy(dd->de_dirent->d_name, buf + i, dd->de_dirent->d_namlen);
439 *buflen = i;
440 de = devfs_parent_dirent(dd);
441 if (de == NULL) {
442 error = ENOENT;
443 goto finished;
444 }
445 mtx_lock(&devfs_de_interlock);
446 *dvp = de->de_vnode;
447 if (*dvp != NULL) {
448 VI_LOCK(*dvp);
449 mtx_unlock(&devfs_de_interlock);
450 vholdl(*dvp);
451 VI_UNLOCK(*dvp);
452 vref(*dvp);
453 vdrop(*dvp);
454 } else {
455 mtx_unlock(&devfs_de_interlock);
456 error = ENOENT;
457 }
458 finished:
459 sx_xunlock(&dmp->dm_lock);
460 return (error);
461 }
462
463 /*
464 * Construct the fully qualified path name relative to the mountpoint.
465 * If a NULL cnp is provided, no '/' is appended to the resulting path.
466 */
467 char *
devfs_fqpn(char * buf,struct devfs_mount * dmp,struct devfs_dirent * dd,struct componentname * cnp)468 devfs_fqpn(char *buf, struct devfs_mount *dmp, struct devfs_dirent *dd,
469 struct componentname *cnp)
470 {
471 int i;
472 struct devfs_dirent *de;
473
474 sx_assert(&dmp->dm_lock, SA_LOCKED);
475
476 i = SPECNAMELEN;
477 buf[i] = '\0';
478 if (cnp != NULL)
479 i -= cnp->cn_namelen;
480 if (i < 0)
481 return (NULL);
482 if (cnp != NULL)
483 bcopy(cnp->cn_nameptr, buf + i, cnp->cn_namelen);
484 de = dd;
485 while (de != dmp->dm_rootdir) {
486 if (cnp != NULL || i < SPECNAMELEN) {
487 i--;
488 if (i < 0)
489 return (NULL);
490 buf[i] = '/';
491 }
492 i -= de->de_dirent->d_namlen;
493 if (i < 0)
494 return (NULL);
495 bcopy(de->de_dirent->d_name, buf + i,
496 de->de_dirent->d_namlen);
497 de = devfs_parent_dirent(de);
498 if (de == NULL)
499 return (NULL);
500 }
501 return (buf + i);
502 }
503
504 static int
devfs_allocv_drop_refs(int drop_dm_lock,struct devfs_mount * dmp,struct devfs_dirent * de)505 devfs_allocv_drop_refs(int drop_dm_lock, struct devfs_mount *dmp,
506 struct devfs_dirent *de)
507 {
508 int not_found;
509
510 not_found = 0;
511 if (de->de_flags & DE_DOOMED)
512 not_found = 1;
513 if (DEVFS_DE_DROP(de)) {
514 KASSERT(not_found == 1, ("DEVFS de dropped but not doomed"));
515 devfs_dirent_free(de);
516 }
517 if (DEVFS_DMP_DROP(dmp)) {
518 KASSERT(not_found == 1,
519 ("DEVFS mount struct freed before dirent"));
520 not_found = 2;
521 sx_xunlock(&dmp->dm_lock);
522 devfs_unmount_final(dmp);
523 }
524 if (not_found == 1 || (drop_dm_lock && not_found != 2))
525 sx_unlock(&dmp->dm_lock);
526 return (not_found);
527 }
528
529 static void
devfs_insmntque_dtr(struct vnode * vp,void * arg)530 devfs_insmntque_dtr(struct vnode *vp, void *arg)
531 {
532 struct devfs_dirent *de;
533
534 de = (struct devfs_dirent *)arg;
535 mtx_lock(&devfs_de_interlock);
536 vp->v_data = NULL;
537 de->de_vnode = NULL;
538 mtx_unlock(&devfs_de_interlock);
539 vgone(vp);
540 vput(vp);
541 }
542
543 /*
544 * devfs_allocv shall be entered with dmp->dm_lock held, and it drops
545 * it on return.
546 */
547 int
devfs_allocv(struct devfs_dirent * de,struct mount * mp,int lockmode,struct vnode ** vpp)548 devfs_allocv(struct devfs_dirent *de, struct mount *mp, int lockmode,
549 struct vnode **vpp)
550 {
551 int error;
552 struct vnode *vp;
553 struct cdev *dev;
554 struct devfs_mount *dmp;
555 struct cdevsw *dsw;
556 enum vgetstate vs;
557
558 dmp = VFSTODEVFS(mp);
559 if (de->de_flags & DE_DOOMED) {
560 sx_xunlock(&dmp->dm_lock);
561 return (ENOENT);
562 }
563 loop:
564 DEVFS_DE_HOLD(de);
565 DEVFS_DMP_HOLD(dmp);
566 mtx_lock(&devfs_de_interlock);
567 vp = de->de_vnode;
568 if (vp != NULL) {
569 vs = vget_prep(vp);
570 mtx_unlock(&devfs_de_interlock);
571 sx_xunlock(&dmp->dm_lock);
572 vget_finish(vp, lockmode | LK_RETRY, vs);
573 sx_xlock(&dmp->dm_lock);
574 if (devfs_allocv_drop_refs(0, dmp, de)) {
575 vput(vp);
576 return (ENOENT);
577 } else if (VN_IS_DOOMED(vp)) {
578 mtx_lock(&devfs_de_interlock);
579 if (de->de_vnode == vp) {
580 de->de_vnode = NULL;
581 vp->v_data = NULL;
582 }
583 mtx_unlock(&devfs_de_interlock);
584 vput(vp);
585 goto loop;
586 }
587 sx_xunlock(&dmp->dm_lock);
588 *vpp = vp;
589 return (0);
590 }
591 mtx_unlock(&devfs_de_interlock);
592 if (de->de_dirent->d_type == DT_CHR) {
593 if (!(de->de_cdp->cdp_flags & CDP_ACTIVE)) {
594 devfs_allocv_drop_refs(1, dmp, de);
595 return (ENOENT);
596 }
597 dev = &de->de_cdp->cdp_c;
598 } else {
599 dev = NULL;
600 }
601 error = getnewvnode("devfs", mp, &devfs_vnodeops, &vp);
602 if (error != 0) {
603 devfs_allocv_drop_refs(1, dmp, de);
604 printf("devfs_allocv: failed to allocate new vnode\n");
605 return (error);
606 }
607
608 if (de->de_dirent->d_type == DT_CHR) {
609 vp->v_type = VCHR;
610 VI_LOCK(vp);
611 dev_lock();
612 dev_refl(dev);
613 /* XXX: v_rdev should be protect by vnode lock */
614 vp->v_rdev = dev;
615 VNPASS(vp->v_usecount == 1, vp);
616 /* Special casing of ttys for deadfs. Probably redundant. */
617 dsw = dev->si_devsw;
618 if (dsw != NULL && (dsw->d_flags & D_TTY) != 0)
619 vp->v_vflag |= VV_ISTTY;
620 dev_unlock();
621 VI_UNLOCK(vp);
622 if ((dev->si_flags & SI_ETERNAL) != 0)
623 vp->v_vflag |= VV_ETERNALDEV;
624 vp->v_op = &devfs_specops;
625 } else if (de->de_dirent->d_type == DT_DIR) {
626 vp->v_type = VDIR;
627 } else if (de->de_dirent->d_type == DT_LNK) {
628 vp->v_type = VLNK;
629 } else {
630 vp->v_type = VBAD;
631 }
632 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_NOWITNESS);
633 VN_LOCK_ASHARE(vp);
634 mtx_lock(&devfs_de_interlock);
635 vp->v_data = de;
636 de->de_vnode = vp;
637 mtx_unlock(&devfs_de_interlock);
638 error = insmntque1(vp, mp, devfs_insmntque_dtr, de);
639 if (error != 0) {
640 (void) devfs_allocv_drop_refs(1, dmp, de);
641 return (error);
642 }
643 if (devfs_allocv_drop_refs(0, dmp, de)) {
644 vput(vp);
645 return (ENOENT);
646 }
647 #ifdef MAC
648 mac_devfs_vnode_associate(mp, de, vp);
649 #endif
650 sx_xunlock(&dmp->dm_lock);
651 *vpp = vp;
652 return (0);
653 }
654
655 static int
devfs_access(struct vop_access_args * ap)656 devfs_access(struct vop_access_args *ap)
657 {
658 struct vnode *vp = ap->a_vp;
659 struct devfs_dirent *de;
660 struct proc *p;
661 int error;
662
663 de = vp->v_data;
664 if (vp->v_type == VDIR)
665 de = de->de_dir;
666
667 error = vaccess(vp->v_type, de->de_mode, de->de_uid, de->de_gid,
668 ap->a_accmode, ap->a_cred);
669 if (error == 0)
670 return (0);
671 if (error != EACCES)
672 return (error);
673 p = ap->a_td->td_proc;
674 /* We do, however, allow access to the controlling terminal */
675 PROC_LOCK(p);
676 if (!(p->p_flag & P_CONTROLT)) {
677 PROC_UNLOCK(p);
678 return (error);
679 }
680 if (p->p_session->s_ttydp == de->de_cdp)
681 error = 0;
682 PROC_UNLOCK(p);
683 return (error);
684 }
685
686 _Static_assert(((FMASK | FCNTLFLAGS) & (FLASTCLOSE | FREVOKE)) == 0,
687 "devfs-only flag reuse failed");
688
689 static int
devfs_close(struct vop_close_args * ap)690 devfs_close(struct vop_close_args *ap)
691 {
692 struct vnode *vp = ap->a_vp, *oldvp;
693 struct thread *td = ap->a_td;
694 struct proc *p;
695 struct cdev *dev = vp->v_rdev;
696 struct cdevsw *dsw;
697 struct devfs_dirent *de = vp->v_data;
698 int dflags, error, ref, vp_locked;
699
700 /*
701 * XXX: Don't call d_close() if we were called because of
702 * XXX: insmntque1() failure.
703 */
704 if (vp->v_data == NULL)
705 return (0);
706
707 /*
708 * Hack: a tty device that is a controlling terminal
709 * has a reference from the session structure.
710 * We cannot easily tell that a character device is
711 * a controlling terminal, unless it is the closing
712 * process' controlling terminal. In that case,
713 * if the reference count is 2 (this last descriptor
714 * plus the session), release the reference from the session.
715 */
716 if (de->de_usecount == 2 && td != NULL) {
717 p = td->td_proc;
718 PROC_LOCK(p);
719 if (vp == p->p_session->s_ttyvp) {
720 PROC_UNLOCK(p);
721 oldvp = NULL;
722 sx_xlock(&proctree_lock);
723 if (vp == p->p_session->s_ttyvp) {
724 SESS_LOCK(p->p_session);
725 mtx_lock(&devfs_de_interlock);
726 VI_LOCK(vp);
727 if (devfs_usecountl(vp) == 2 && !VN_IS_DOOMED(vp)) {
728 p->p_session->s_ttyvp = NULL;
729 p->p_session->s_ttydp = NULL;
730 oldvp = vp;
731 }
732 VI_UNLOCK(vp);
733 mtx_unlock(&devfs_de_interlock);
734 SESS_UNLOCK(p->p_session);
735 }
736 sx_xunlock(&proctree_lock);
737 if (oldvp != NULL)
738 devfs_ctty_unref(oldvp);
739 } else
740 PROC_UNLOCK(p);
741 }
742 /*
743 * We do not want to really close the device if it
744 * is still in use unless we are trying to close it
745 * forcibly. Since every use (buffer, vnode, swap, cmap)
746 * holds a reference to the vnode, and because we mark
747 * any other vnodes that alias this device, when the
748 * sum of the reference counts on all the aliased
749 * vnodes descends to one, we are on last close.
750 */
751 dsw = dev_refthread(dev, &ref);
752 if (dsw == NULL)
753 return (ENXIO);
754 dflags = 0;
755 mtx_lock(&devfs_de_interlock);
756 VI_LOCK(vp);
757 if (devfs_usecountl(vp) == 1)
758 dflags |= FLASTCLOSE;
759 devfs_usecount_subl(vp);
760 mtx_unlock(&devfs_de_interlock);
761 if (VN_IS_DOOMED(vp)) {
762 /* Forced close. */
763 dflags |= FREVOKE | FNONBLOCK;
764 } else if (dsw->d_flags & D_TRACKCLOSE) {
765 /* Keep device updated on status. */
766 } else if ((dflags & FLASTCLOSE) == 0) {
767 VI_UNLOCK(vp);
768 dev_relthread(dev, ref);
769 return (0);
770 }
771 vholdnz(vp);
772 VI_UNLOCK(vp);
773 vp_locked = VOP_ISLOCKED(vp);
774 VOP_UNLOCK(vp);
775 KASSERT(dev->si_refcount > 0,
776 ("devfs_close() on un-referenced struct cdev *(%s)", devtoname(dev)));
777 error = dsw->d_close(dev, ap->a_fflag | dflags, S_IFCHR, td);
778 dev_relthread(dev, ref);
779 vn_lock(vp, vp_locked | LK_RETRY);
780 vdrop(vp);
781 return (error);
782 }
783
784 static int
devfs_close_f(struct file * fp,struct thread * td)785 devfs_close_f(struct file *fp, struct thread *td)
786 {
787 int error;
788 struct file *fpop;
789
790 /*
791 * NB: td may be NULL if this descriptor is closed due to
792 * garbage collection from a closed UNIX domain socket.
793 */
794 fpop = curthread->td_fpop;
795 curthread->td_fpop = fp;
796 error = vnops.fo_close(fp, td);
797 curthread->td_fpop = fpop;
798
799 /*
800 * The f_cdevpriv cannot be assigned non-NULL value while we
801 * are destroying the file.
802 */
803 if (fp->f_cdevpriv != NULL)
804 devfs_fpdrop(fp);
805 return (error);
806 }
807
808 static int
devfs_getattr(struct vop_getattr_args * ap)809 devfs_getattr(struct vop_getattr_args *ap)
810 {
811 struct vnode *vp = ap->a_vp;
812 struct vattr *vap = ap->a_vap;
813 struct devfs_dirent *de;
814 struct devfs_mount *dmp;
815 struct cdev *dev;
816 struct timeval boottime;
817 int error;
818
819 error = devfs_populate_vp(vp);
820 if (error != 0)
821 return (error);
822
823 dmp = VFSTODEVFS(vp->v_mount);
824 sx_xunlock(&dmp->dm_lock);
825
826 de = vp->v_data;
827 KASSERT(de != NULL, ("Null dirent in devfs_getattr vp=%p", vp));
828 if (vp->v_type == VDIR) {
829 de = de->de_dir;
830 KASSERT(de != NULL,
831 ("Null dir dirent in devfs_getattr vp=%p", vp));
832 }
833 vap->va_uid = de->de_uid;
834 vap->va_gid = de->de_gid;
835 vap->va_mode = de->de_mode;
836 if (vp->v_type == VLNK)
837 vap->va_size = strlen(de->de_symlink);
838 else if (vp->v_type == VDIR)
839 vap->va_size = vap->va_bytes = DEV_BSIZE;
840 else
841 vap->va_size = 0;
842 if (vp->v_type != VDIR)
843 vap->va_bytes = 0;
844 vap->va_blocksize = DEV_BSIZE;
845 vap->va_type = vp->v_type;
846
847 getboottime(&boottime);
848 #define fix(aa) \
849 do { \
850 if ((aa).tv_sec <= 3600) { \
851 (aa).tv_sec = boottime.tv_sec; \
852 (aa).tv_nsec = boottime.tv_usec * 1000; \
853 } \
854 } while (0)
855
856 if (vp->v_type != VCHR) {
857 fix(de->de_atime);
858 vap->va_atime = de->de_atime;
859 fix(de->de_mtime);
860 vap->va_mtime = de->de_mtime;
861 fix(de->de_ctime);
862 vap->va_ctime = de->de_ctime;
863 } else {
864 dev = vp->v_rdev;
865 fix(dev->si_atime);
866 vap->va_atime = dev->si_atime;
867 fix(dev->si_mtime);
868 vap->va_mtime = dev->si_mtime;
869 fix(dev->si_ctime);
870 vap->va_ctime = dev->si_ctime;
871
872 vap->va_rdev = cdev2priv(dev)->cdp_inode;
873 }
874 vap->va_gen = 0;
875 vap->va_flags = 0;
876 vap->va_filerev = 0;
877 vap->va_nlink = de->de_links;
878 vap->va_fileid = de->de_inode;
879
880 return (error);
881 }
882
883 /* ARGSUSED */
884 static int
devfs_ioctl_f(struct file * fp,u_long com,void * data,struct ucred * cred,struct thread * td)885 devfs_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred, struct thread *td)
886 {
887 struct file *fpop;
888 int error;
889
890 fpop = td->td_fpop;
891 td->td_fpop = fp;
892 error = vnops.fo_ioctl(fp, com, data, cred, td);
893 td->td_fpop = fpop;
894 return (error);
895 }
896
897 void *
fiodgname_buf_get_ptr(void * fgnp,u_long com)898 fiodgname_buf_get_ptr(void *fgnp, u_long com)
899 {
900 union {
901 struct fiodgname_arg fgn;
902 #ifdef COMPAT_FREEBSD32
903 struct fiodgname_arg32 fgn32;
904 #endif
905 } *fgnup;
906
907 fgnup = fgnp;
908 switch (com) {
909 case FIODGNAME:
910 return (fgnup->fgn.buf);
911 #ifdef COMPAT_FREEBSD32
912 case FIODGNAME_32:
913 return ((void *)(uintptr_t)fgnup->fgn32.buf);
914 #endif
915 default:
916 panic("Unhandled ioctl command %ld", com);
917 }
918 }
919
920 static int
devfs_ioctl(struct vop_ioctl_args * ap)921 devfs_ioctl(struct vop_ioctl_args *ap)
922 {
923 struct fiodgname_arg *fgn;
924 struct vnode *vpold, *vp;
925 struct cdevsw *dsw;
926 struct thread *td;
927 struct session *sess;
928 struct cdev *dev;
929 int error, ref, i;
930 const char *p;
931 u_long com;
932
933 vp = ap->a_vp;
934 com = ap->a_command;
935 td = ap->a_td;
936
937 dsw = devvn_refthread(vp, &dev, &ref);
938 if (dsw == NULL)
939 return (ENXIO);
940 KASSERT(dev->si_refcount > 0,
941 ("devfs: un-referenced struct cdev *(%s)", devtoname(dev)));
942
943 switch (com) {
944 case FIODTYPE:
945 *(int *)ap->a_data = dsw->d_flags & D_TYPEMASK;
946 error = 0;
947 break;
948 case FIODGNAME:
949 #ifdef COMPAT_FREEBSD32
950 case FIODGNAME_32:
951 #endif
952 fgn = ap->a_data;
953 p = devtoname(dev);
954 i = strlen(p) + 1;
955 if (i > fgn->len)
956 error = EINVAL;
957 else
958 error = copyout(p, fiodgname_buf_get_ptr(fgn, com), i);
959 break;
960 default:
961 error = dsw->d_ioctl(dev, com, ap->a_data, ap->a_fflag, td);
962 }
963
964 dev_relthread(dev, ref);
965 if (error == ENOIOCTL)
966 error = ENOTTY;
967
968 if (error == 0 && com == TIOCSCTTY) {
969 /*
970 * Do nothing if reassigning same control tty, or if the
971 * control tty has already disappeared. If it disappeared,
972 * it's because we were racing with TIOCNOTTY. TIOCNOTTY
973 * already took care of releasing the old vnode and we have
974 * nothing left to do.
975 */
976 sx_slock(&proctree_lock);
977 sess = td->td_proc->p_session;
978 if (sess->s_ttyvp == vp || sess->s_ttyp == NULL) {
979 sx_sunlock(&proctree_lock);
980 return (0);
981 }
982
983 devfs_ctty_ref(vp);
984 SESS_LOCK(sess);
985 vpold = sess->s_ttyvp;
986 sess->s_ttyvp = vp;
987 sess->s_ttydp = cdev2priv(dev);
988 SESS_UNLOCK(sess);
989
990 sx_sunlock(&proctree_lock);
991
992 /* Get rid of reference to old control tty */
993 if (vpold)
994 devfs_ctty_unref(vpold);
995 }
996 return (error);
997 }
998
999 /* ARGSUSED */
1000 static int
devfs_kqfilter_f(struct file * fp,struct knote * kn)1001 devfs_kqfilter_f(struct file *fp, struct knote *kn)
1002 {
1003 struct cdev *dev;
1004 struct cdevsw *dsw;
1005 int error, ref;
1006 struct file *fpop;
1007 struct thread *td;
1008
1009 td = curthread;
1010 fpop = td->td_fpop;
1011 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1012 if (error)
1013 return (error);
1014 error = dsw->d_kqfilter(dev, kn);
1015 td->td_fpop = fpop;
1016 dev_relthread(dev, ref);
1017 return (error);
1018 }
1019
1020 static inline int
devfs_prison_check(struct devfs_dirent * de,struct thread * td)1021 devfs_prison_check(struct devfs_dirent *de, struct thread *td)
1022 {
1023 struct cdev_priv *cdp;
1024 struct ucred *dcr;
1025 struct proc *p;
1026 int error;
1027
1028 cdp = de->de_cdp;
1029 if (cdp == NULL)
1030 return (0);
1031 dcr = cdp->cdp_c.si_cred;
1032 if (dcr == NULL)
1033 return (0);
1034
1035 error = prison_check(td->td_ucred, dcr);
1036 if (error == 0)
1037 return (0);
1038 /* We do, however, allow access to the controlling terminal */
1039 p = td->td_proc;
1040 PROC_LOCK(p);
1041 if (!(p->p_flag & P_CONTROLT)) {
1042 PROC_UNLOCK(p);
1043 return (error);
1044 }
1045 if (p->p_session->s_ttydp == cdp)
1046 error = 0;
1047 PROC_UNLOCK(p);
1048 return (error);
1049 }
1050
1051 static int
devfs_lookupx(struct vop_lookup_args * ap,int * dm_unlock)1052 devfs_lookupx(struct vop_lookup_args *ap, int *dm_unlock)
1053 {
1054 struct componentname *cnp;
1055 struct vnode *dvp, **vpp;
1056 struct thread *td;
1057 struct devfs_dirent *de, *dd;
1058 struct devfs_dirent **dde;
1059 struct devfs_mount *dmp;
1060 struct mount *mp;
1061 struct cdev *cdev;
1062 int error, flags, nameiop, dvplocked;
1063 char specname[SPECNAMELEN + 1], *pname;
1064
1065 cnp = ap->a_cnp;
1066 vpp = ap->a_vpp;
1067 dvp = ap->a_dvp;
1068 pname = cnp->cn_nameptr;
1069 td = cnp->cn_thread;
1070 flags = cnp->cn_flags;
1071 nameiop = cnp->cn_nameiop;
1072 mp = dvp->v_mount;
1073 dmp = VFSTODEVFS(mp);
1074 dd = dvp->v_data;
1075 *vpp = NULLVP;
1076
1077 if ((flags & ISLASTCN) && nameiop == RENAME)
1078 return (EOPNOTSUPP);
1079
1080 if (dvp->v_type != VDIR)
1081 return (ENOTDIR);
1082
1083 if ((flags & ISDOTDOT) && (dvp->v_vflag & VV_ROOT))
1084 return (EIO);
1085
1086 error = vn_dir_check_exec(dvp, cnp);
1087 if (error != 0)
1088 return (error);
1089
1090 if (cnp->cn_namelen == 1 && *pname == '.') {
1091 if ((flags & ISLASTCN) && nameiop != LOOKUP)
1092 return (EINVAL);
1093 *vpp = dvp;
1094 VREF(dvp);
1095 return (0);
1096 }
1097
1098 if (flags & ISDOTDOT) {
1099 if ((flags & ISLASTCN) && nameiop != LOOKUP)
1100 return (EINVAL);
1101 de = devfs_parent_dirent(dd);
1102 if (de == NULL)
1103 return (ENOENT);
1104 dvplocked = VOP_ISLOCKED(dvp);
1105 VOP_UNLOCK(dvp);
1106 error = devfs_allocv(de, mp, cnp->cn_lkflags & LK_TYPE_MASK,
1107 vpp);
1108 *dm_unlock = 0;
1109 vn_lock(dvp, dvplocked | LK_RETRY);
1110 return (error);
1111 }
1112
1113 dd = dvp->v_data;
1114 de = devfs_find(dd, cnp->cn_nameptr, cnp->cn_namelen, 0);
1115 while (de == NULL) { /* While(...) so we can use break */
1116
1117 if (nameiop == DELETE)
1118 return (ENOENT);
1119
1120 /*
1121 * OK, we didn't have an entry for the name we were asked for
1122 * so we try to see if anybody can create it on demand.
1123 */
1124 pname = devfs_fqpn(specname, dmp, dd, cnp);
1125 if (pname == NULL)
1126 break;
1127
1128 cdev = NULL;
1129 DEVFS_DMP_HOLD(dmp);
1130 sx_xunlock(&dmp->dm_lock);
1131 sx_slock(&clone_drain_lock);
1132 EVENTHANDLER_INVOKE(dev_clone,
1133 td->td_ucred, pname, strlen(pname), &cdev);
1134 sx_sunlock(&clone_drain_lock);
1135
1136 if (cdev == NULL)
1137 sx_xlock(&dmp->dm_lock);
1138 else if (devfs_populate_vp(dvp) != 0) {
1139 *dm_unlock = 0;
1140 sx_xlock(&dmp->dm_lock);
1141 if (DEVFS_DMP_DROP(dmp)) {
1142 sx_xunlock(&dmp->dm_lock);
1143 devfs_unmount_final(dmp);
1144 } else
1145 sx_xunlock(&dmp->dm_lock);
1146 dev_rel(cdev);
1147 return (ENOENT);
1148 }
1149 if (DEVFS_DMP_DROP(dmp)) {
1150 *dm_unlock = 0;
1151 sx_xunlock(&dmp->dm_lock);
1152 devfs_unmount_final(dmp);
1153 if (cdev != NULL)
1154 dev_rel(cdev);
1155 return (ENOENT);
1156 }
1157
1158 if (cdev == NULL)
1159 break;
1160
1161 dev_lock();
1162 dde = &cdev2priv(cdev)->cdp_dirents[dmp->dm_idx];
1163 if (dde != NULL && *dde != NULL)
1164 de = *dde;
1165 dev_unlock();
1166 dev_rel(cdev);
1167 break;
1168 }
1169
1170 if (de == NULL || de->de_flags & DE_WHITEOUT) {
1171 if ((nameiop == CREATE || nameiop == RENAME) &&
1172 (flags & (LOCKPARENT | WANTPARENT)) && (flags & ISLASTCN)) {
1173 cnp->cn_flags |= SAVENAME;
1174 return (EJUSTRETURN);
1175 }
1176 return (ENOENT);
1177 }
1178
1179 if (devfs_prison_check(de, td))
1180 return (ENOENT);
1181
1182 if ((cnp->cn_nameiop == DELETE) && (flags & ISLASTCN)) {
1183 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
1184 if (error)
1185 return (error);
1186 if (*vpp == dvp) {
1187 VREF(dvp);
1188 *vpp = dvp;
1189 return (0);
1190 }
1191 }
1192 error = devfs_allocv(de, mp, cnp->cn_lkflags & LK_TYPE_MASK, vpp);
1193 *dm_unlock = 0;
1194 return (error);
1195 }
1196
1197 static int
devfs_lookup(struct vop_lookup_args * ap)1198 devfs_lookup(struct vop_lookup_args *ap)
1199 {
1200 int j;
1201 struct devfs_mount *dmp;
1202 int dm_unlock;
1203
1204 if (devfs_populate_vp(ap->a_dvp) != 0)
1205 return (ENOTDIR);
1206
1207 dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1208 dm_unlock = 1;
1209 j = devfs_lookupx(ap, &dm_unlock);
1210 if (dm_unlock == 1)
1211 sx_xunlock(&dmp->dm_lock);
1212 return (j);
1213 }
1214
1215 static int
devfs_mknod(struct vop_mknod_args * ap)1216 devfs_mknod(struct vop_mknod_args *ap)
1217 {
1218 struct componentname *cnp;
1219 struct vnode *dvp, **vpp;
1220 struct devfs_dirent *dd, *de;
1221 struct devfs_mount *dmp;
1222 int error;
1223
1224 /*
1225 * The only type of node we should be creating here is a
1226 * character device, for anything else return EOPNOTSUPP.
1227 */
1228 if (ap->a_vap->va_type != VCHR)
1229 return (EOPNOTSUPP);
1230 dvp = ap->a_dvp;
1231 dmp = VFSTODEVFS(dvp->v_mount);
1232
1233 cnp = ap->a_cnp;
1234 vpp = ap->a_vpp;
1235 dd = dvp->v_data;
1236
1237 error = ENOENT;
1238 sx_xlock(&dmp->dm_lock);
1239 TAILQ_FOREACH(de, &dd->de_dlist, de_list) {
1240 if (cnp->cn_namelen != de->de_dirent->d_namlen)
1241 continue;
1242 if (de->de_dirent->d_type == DT_CHR &&
1243 (de->de_cdp->cdp_flags & CDP_ACTIVE) == 0)
1244 continue;
1245 if (bcmp(cnp->cn_nameptr, de->de_dirent->d_name,
1246 de->de_dirent->d_namlen) != 0)
1247 continue;
1248 if (de->de_flags & DE_WHITEOUT)
1249 break;
1250 goto notfound;
1251 }
1252 if (de == NULL)
1253 goto notfound;
1254 de->de_flags &= ~DE_WHITEOUT;
1255 error = devfs_allocv(de, dvp->v_mount, LK_EXCLUSIVE, vpp);
1256 return (error);
1257 notfound:
1258 sx_xunlock(&dmp->dm_lock);
1259 return (error);
1260 }
1261
1262 /* ARGSUSED */
1263 static int
devfs_open(struct vop_open_args * ap)1264 devfs_open(struct vop_open_args *ap)
1265 {
1266 struct thread *td = ap->a_td;
1267 struct vnode *vp = ap->a_vp;
1268 struct cdev *dev = vp->v_rdev;
1269 struct file *fp = ap->a_fp;
1270 int error, ref, vlocked;
1271 struct cdevsw *dsw;
1272 struct file *fpop;
1273
1274 if (vp->v_type == VBLK)
1275 return (ENXIO);
1276
1277 if (dev == NULL)
1278 return (ENXIO);
1279
1280 /* Make this field valid before any I/O in d_open. */
1281 if (dev->si_iosize_max == 0)
1282 dev->si_iosize_max = DFLTPHYS;
1283
1284 dsw = dev_refthread(dev, &ref);
1285 if (dsw == NULL)
1286 return (ENXIO);
1287 if (fp == NULL && dsw->d_fdopen != NULL) {
1288 dev_relthread(dev, ref);
1289 return (ENXIO);
1290 }
1291
1292 if (vp->v_type == VCHR)
1293 devfs_usecount_add(vp);
1294
1295 vlocked = VOP_ISLOCKED(vp);
1296 VOP_UNLOCK(vp);
1297
1298 fpop = td->td_fpop;
1299 td->td_fpop = fp;
1300 if (fp != NULL) {
1301 fp->f_data = dev;
1302 fp->f_vnode = vp;
1303 }
1304 if (dsw->d_fdopen != NULL)
1305 error = dsw->d_fdopen(dev, ap->a_mode, td, fp);
1306 else
1307 error = dsw->d_open(dev, ap->a_mode, S_IFCHR, td);
1308 /* Clean up any cdevpriv upon error. */
1309 if (error != 0)
1310 devfs_clear_cdevpriv();
1311 td->td_fpop = fpop;
1312
1313 vn_lock(vp, vlocked | LK_RETRY);
1314 if (error != 0 && vp->v_type == VCHR)
1315 devfs_usecount_sub(vp);
1316
1317 dev_relthread(dev, ref);
1318 if (error != 0) {
1319 if (error == ERESTART)
1320 error = EINTR;
1321 return (error);
1322 }
1323
1324 #if 0 /* /dev/console */
1325 KASSERT(fp != NULL, ("Could not vnode bypass device on NULL fp"));
1326 #else
1327 if (fp == NULL)
1328 return (error);
1329 #endif
1330 if (fp->f_ops == &badfileops)
1331 finit(fp, fp->f_flag, DTYPE_VNODE, dev, &devfs_ops_f);
1332 return (error);
1333 }
1334
1335 static int
devfs_pathconf(struct vop_pathconf_args * ap)1336 devfs_pathconf(struct vop_pathconf_args *ap)
1337 {
1338
1339 switch (ap->a_name) {
1340 case _PC_FILESIZEBITS:
1341 *ap->a_retval = 64;
1342 return (0);
1343 case _PC_NAME_MAX:
1344 *ap->a_retval = NAME_MAX;
1345 return (0);
1346 case _PC_LINK_MAX:
1347 *ap->a_retval = INT_MAX;
1348 return (0);
1349 case _PC_SYMLINK_MAX:
1350 *ap->a_retval = MAXPATHLEN;
1351 return (0);
1352 case _PC_MAX_CANON:
1353 if (ap->a_vp->v_vflag & VV_ISTTY) {
1354 *ap->a_retval = MAX_CANON;
1355 return (0);
1356 }
1357 return (EINVAL);
1358 case _PC_MAX_INPUT:
1359 if (ap->a_vp->v_vflag & VV_ISTTY) {
1360 *ap->a_retval = MAX_INPUT;
1361 return (0);
1362 }
1363 return (EINVAL);
1364 case _PC_VDISABLE:
1365 if (ap->a_vp->v_vflag & VV_ISTTY) {
1366 *ap->a_retval = _POSIX_VDISABLE;
1367 return (0);
1368 }
1369 return (EINVAL);
1370 case _PC_MAC_PRESENT:
1371 #ifdef MAC
1372 /*
1373 * If MAC is enabled, devfs automatically supports
1374 * trivial non-persistent label storage.
1375 */
1376 *ap->a_retval = 1;
1377 #else
1378 *ap->a_retval = 0;
1379 #endif
1380 return (0);
1381 case _PC_CHOWN_RESTRICTED:
1382 *ap->a_retval = 1;
1383 return (0);
1384 default:
1385 return (vop_stdpathconf(ap));
1386 }
1387 /* NOTREACHED */
1388 }
1389
1390 /* ARGSUSED */
1391 static int
devfs_poll_f(struct file * fp,int events,struct ucred * cred,struct thread * td)1392 devfs_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td)
1393 {
1394 struct cdev *dev;
1395 struct cdevsw *dsw;
1396 int error, ref;
1397 struct file *fpop;
1398
1399 fpop = td->td_fpop;
1400 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1401 if (error != 0) {
1402 error = vnops.fo_poll(fp, events, cred, td);
1403 return (error);
1404 }
1405 error = dsw->d_poll(dev, events, td);
1406 td->td_fpop = fpop;
1407 dev_relthread(dev, ref);
1408 return(error);
1409 }
1410
1411 /*
1412 * Print out the contents of a special device vnode.
1413 */
1414 static int
devfs_print(struct vop_print_args * ap)1415 devfs_print(struct vop_print_args *ap)
1416 {
1417
1418 printf("\tdev %s\n", devtoname(ap->a_vp->v_rdev));
1419 return (0);
1420 }
1421
1422 static int
devfs_read_f(struct file * fp,struct uio * uio,struct ucred * cred,int flags,struct thread * td)1423 devfs_read_f(struct file *fp, struct uio *uio, struct ucred *cred,
1424 int flags, struct thread *td)
1425 {
1426 struct cdev *dev;
1427 int ioflag, error, ref;
1428 ssize_t resid;
1429 struct cdevsw *dsw;
1430 struct file *fpop;
1431
1432 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1433 return (EINVAL);
1434 fpop = td->td_fpop;
1435 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1436 if (error != 0) {
1437 error = vnops.fo_read(fp, uio, cred, flags, td);
1438 return (error);
1439 }
1440 resid = uio->uio_resid;
1441 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT);
1442 if (ioflag & O_DIRECT)
1443 ioflag |= IO_DIRECT;
1444
1445 foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1446 error = dsw->d_read(dev, uio, ioflag);
1447 if (uio->uio_resid != resid || (error == 0 && resid != 0))
1448 devfs_timestamp(&dev->si_atime);
1449 td->td_fpop = fpop;
1450 dev_relthread(dev, ref);
1451
1452 foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF_R);
1453 return (error);
1454 }
1455
1456 static int
devfs_readdir(struct vop_readdir_args * ap)1457 devfs_readdir(struct vop_readdir_args *ap)
1458 {
1459 int error;
1460 struct uio *uio;
1461 struct dirent *dp;
1462 struct devfs_dirent *dd;
1463 struct devfs_dirent *de;
1464 struct devfs_mount *dmp;
1465 off_t off;
1466 int *tmp_ncookies = NULL;
1467
1468 if (ap->a_vp->v_type != VDIR)
1469 return (ENOTDIR);
1470
1471 uio = ap->a_uio;
1472 if (uio->uio_offset < 0)
1473 return (EINVAL);
1474
1475 /*
1476 * XXX: This is a temporary hack to get around this filesystem not
1477 * supporting cookies. We store the location of the ncookies pointer
1478 * in a temporary variable before calling vfs_subr.c:vfs_read_dirent()
1479 * and set the number of cookies to 0. We then set the pointer to
1480 * NULL so that vfs_read_dirent doesn't try to call realloc() on
1481 * ap->a_cookies. Later in this function, we restore the ap->a_ncookies
1482 * pointer to its original location before returning to the caller.
1483 */
1484 if (ap->a_ncookies != NULL) {
1485 tmp_ncookies = ap->a_ncookies;
1486 *ap->a_ncookies = 0;
1487 ap->a_ncookies = NULL;
1488 }
1489
1490 dmp = VFSTODEVFS(ap->a_vp->v_mount);
1491 if (devfs_populate_vp(ap->a_vp) != 0) {
1492 if (tmp_ncookies != NULL)
1493 ap->a_ncookies = tmp_ncookies;
1494 return (EIO);
1495 }
1496 error = 0;
1497 de = ap->a_vp->v_data;
1498 off = 0;
1499 TAILQ_FOREACH(dd, &de->de_dlist, de_list) {
1500 KASSERT(dd->de_cdp != (void *)0xdeadc0de, ("%s %d\n", __func__, __LINE__));
1501 if (dd->de_flags & (DE_COVERED | DE_WHITEOUT))
1502 continue;
1503 if (devfs_prison_check(dd, uio->uio_td))
1504 continue;
1505 if (dd->de_dirent->d_type == DT_DIR)
1506 de = dd->de_dir;
1507 else
1508 de = dd;
1509 dp = dd->de_dirent;
1510 MPASS(dp->d_reclen == GENERIC_DIRSIZ(dp));
1511 if (dp->d_reclen > uio->uio_resid)
1512 break;
1513 dp->d_fileno = de->de_inode;
1514 /* NOTE: d_off is the offset for the *next* entry. */
1515 dp->d_off = off + dp->d_reclen;
1516 if (off >= uio->uio_offset) {
1517 error = vfs_read_dirent(ap, dp, off);
1518 if (error)
1519 break;
1520 }
1521 off += dp->d_reclen;
1522 }
1523 sx_xunlock(&dmp->dm_lock);
1524 uio->uio_offset = off;
1525
1526 /*
1527 * Restore ap->a_ncookies if it wasn't originally NULL in the first
1528 * place.
1529 */
1530 if (tmp_ncookies != NULL)
1531 ap->a_ncookies = tmp_ncookies;
1532
1533 return (error);
1534 }
1535
1536 static int
devfs_readlink(struct vop_readlink_args * ap)1537 devfs_readlink(struct vop_readlink_args *ap)
1538 {
1539 struct devfs_dirent *de;
1540
1541 de = ap->a_vp->v_data;
1542 return (uiomove(de->de_symlink, strlen(de->de_symlink), ap->a_uio));
1543 }
1544
1545 static void
devfs_reclaiml(struct vnode * vp)1546 devfs_reclaiml(struct vnode *vp)
1547 {
1548 struct devfs_dirent *de;
1549
1550 mtx_assert(&devfs_de_interlock, MA_OWNED);
1551 de = vp->v_data;
1552 if (de != NULL) {
1553 MPASS(de->de_usecount == 0);
1554 de->de_vnode = NULL;
1555 vp->v_data = NULL;
1556 }
1557 }
1558
1559 static int
devfs_reclaim(struct vop_reclaim_args * ap)1560 devfs_reclaim(struct vop_reclaim_args *ap)
1561 {
1562 struct vnode *vp;
1563
1564 vp = ap->a_vp;
1565 mtx_lock(&devfs_de_interlock);
1566 devfs_reclaiml(vp);
1567 mtx_unlock(&devfs_de_interlock);
1568 return (0);
1569 }
1570
1571 static int
devfs_reclaim_vchr(struct vop_reclaim_args * ap)1572 devfs_reclaim_vchr(struct vop_reclaim_args *ap)
1573 {
1574 struct vnode *vp;
1575 struct cdev *dev;
1576
1577 vp = ap->a_vp;
1578 MPASS(vp->v_type == VCHR);
1579
1580 mtx_lock(&devfs_de_interlock);
1581 VI_LOCK(vp);
1582 devfs_usecount_subl(vp);
1583 devfs_reclaiml(vp);
1584 mtx_unlock(&devfs_de_interlock);
1585 dev_lock();
1586 dev = vp->v_rdev;
1587 vp->v_rdev = NULL;
1588 dev_unlock();
1589 VI_UNLOCK(vp);
1590 if (dev != NULL)
1591 dev_rel(dev);
1592 return (0);
1593 }
1594
1595 static int
devfs_remove(struct vop_remove_args * ap)1596 devfs_remove(struct vop_remove_args *ap)
1597 {
1598 struct vnode *dvp = ap->a_dvp;
1599 struct vnode *vp = ap->a_vp;
1600 struct devfs_dirent *dd;
1601 struct devfs_dirent *de, *de_covered;
1602 struct devfs_mount *dmp = VFSTODEVFS(vp->v_mount);
1603
1604 ASSERT_VOP_ELOCKED(dvp, "devfs_remove");
1605 ASSERT_VOP_ELOCKED(vp, "devfs_remove");
1606
1607 sx_xlock(&dmp->dm_lock);
1608 dd = ap->a_dvp->v_data;
1609 de = vp->v_data;
1610 if (de->de_cdp == NULL) {
1611 TAILQ_REMOVE(&dd->de_dlist, de, de_list);
1612 if (de->de_dirent->d_type == DT_LNK) {
1613 de_covered = devfs_find(dd, de->de_dirent->d_name,
1614 de->de_dirent->d_namlen, 0);
1615 if (de_covered != NULL)
1616 de_covered->de_flags &= ~DE_COVERED;
1617 }
1618 /* We need to unlock dvp because devfs_delete() may lock it. */
1619 VOP_UNLOCK(vp);
1620 if (dvp != vp)
1621 VOP_UNLOCK(dvp);
1622 devfs_delete(dmp, de, 0);
1623 sx_xunlock(&dmp->dm_lock);
1624 if (dvp != vp)
1625 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1626 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1627 } else {
1628 de->de_flags |= DE_WHITEOUT;
1629 sx_xunlock(&dmp->dm_lock);
1630 }
1631 return (0);
1632 }
1633
1634 /*
1635 * Revoke is called on a tty when a terminal session ends. The vnode
1636 * is orphaned by setting v_op to deadfs so we need to let go of it
1637 * as well so that we create a new one next time around.
1638 *
1639 */
1640 static int
devfs_revoke(struct vop_revoke_args * ap)1641 devfs_revoke(struct vop_revoke_args *ap)
1642 {
1643 struct vnode *vp = ap->a_vp, *vp2;
1644 struct cdev *dev;
1645 struct cdev_priv *cdp;
1646 struct devfs_dirent *de;
1647 enum vgetstate vs;
1648 u_int i;
1649
1650 KASSERT((ap->a_flags & REVOKEALL) != 0, ("devfs_revoke !REVOKEALL"));
1651
1652 dev = vp->v_rdev;
1653 cdp = cdev2priv(dev);
1654
1655 dev_lock();
1656 cdp->cdp_inuse++;
1657 dev_unlock();
1658
1659 vhold(vp);
1660 vgone(vp);
1661 vdrop(vp);
1662
1663 VOP_UNLOCK(vp);
1664 loop:
1665 for (;;) {
1666 mtx_lock(&devfs_de_interlock);
1667 dev_lock();
1668 vp2 = NULL;
1669 for (i = 0; i <= cdp->cdp_maxdirent; i++) {
1670 de = cdp->cdp_dirents[i];
1671 if (de == NULL)
1672 continue;
1673
1674 vp2 = de->de_vnode;
1675 if (vp2 != NULL) {
1676 dev_unlock();
1677 vs = vget_prep(vp2);
1678 mtx_unlock(&devfs_de_interlock);
1679 if (vget_finish(vp2, LK_EXCLUSIVE, vs) != 0)
1680 goto loop;
1681 vhold(vp2);
1682 vgone(vp2);
1683 vdrop(vp2);
1684 vput(vp2);
1685 break;
1686 }
1687 }
1688 if (vp2 != NULL) {
1689 continue;
1690 }
1691 dev_unlock();
1692 mtx_unlock(&devfs_de_interlock);
1693 break;
1694 }
1695 dev_lock();
1696 cdp->cdp_inuse--;
1697 if (!(cdp->cdp_flags & CDP_ACTIVE) && cdp->cdp_inuse == 0) {
1698 KASSERT((cdp->cdp_flags & CDP_ON_ACTIVE_LIST) != 0,
1699 ("%s: cdp %p (%s) not on active list",
1700 __func__, cdp, dev->si_name));
1701 cdp->cdp_flags &= ~CDP_ON_ACTIVE_LIST;
1702 TAILQ_REMOVE(&cdevp_list, cdp, cdp_list);
1703 dev_unlock();
1704 dev_rel(&cdp->cdp_c);
1705 } else
1706 dev_unlock();
1707
1708 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1709 return (0);
1710 }
1711
1712 static int
devfs_rioctl(struct vop_ioctl_args * ap)1713 devfs_rioctl(struct vop_ioctl_args *ap)
1714 {
1715 struct vnode *vp;
1716 struct devfs_mount *dmp;
1717 int error;
1718
1719 vp = ap->a_vp;
1720 vn_lock(vp, LK_SHARED | LK_RETRY);
1721 if (VN_IS_DOOMED(vp)) {
1722 VOP_UNLOCK(vp);
1723 return (EBADF);
1724 }
1725 dmp = VFSTODEVFS(vp->v_mount);
1726 sx_xlock(&dmp->dm_lock);
1727 VOP_UNLOCK(vp);
1728 DEVFS_DMP_HOLD(dmp);
1729 devfs_populate(dmp);
1730 if (DEVFS_DMP_DROP(dmp)) {
1731 sx_xunlock(&dmp->dm_lock);
1732 devfs_unmount_final(dmp);
1733 return (ENOENT);
1734 }
1735 error = devfs_rules_ioctl(dmp, ap->a_command, ap->a_data, ap->a_td);
1736 sx_xunlock(&dmp->dm_lock);
1737 return (error);
1738 }
1739
1740 static int
devfs_rread(struct vop_read_args * ap)1741 devfs_rread(struct vop_read_args *ap)
1742 {
1743
1744 if (ap->a_vp->v_type != VDIR)
1745 return (EINVAL);
1746 return (VOP_READDIR(ap->a_vp, ap->a_uio, ap->a_cred, NULL, NULL, NULL));
1747 }
1748
1749 static int
devfs_setattr(struct vop_setattr_args * ap)1750 devfs_setattr(struct vop_setattr_args *ap)
1751 {
1752 struct devfs_dirent *de;
1753 struct vattr *vap;
1754 struct vnode *vp;
1755 struct thread *td;
1756 int c, error;
1757 uid_t uid;
1758 gid_t gid;
1759
1760 vap = ap->a_vap;
1761 vp = ap->a_vp;
1762 td = curthread;
1763 if ((vap->va_type != VNON) ||
1764 (vap->va_nlink != VNOVAL) ||
1765 (vap->va_fsid != VNOVAL) ||
1766 (vap->va_fileid != VNOVAL) ||
1767 (vap->va_blocksize != VNOVAL) ||
1768 (vap->va_flags != VNOVAL && vap->va_flags != 0) ||
1769 (vap->va_rdev != VNOVAL) ||
1770 ((int)vap->va_bytes != VNOVAL) ||
1771 (vap->va_gen != VNOVAL)) {
1772 return (EINVAL);
1773 }
1774
1775 error = devfs_populate_vp(vp);
1776 if (error != 0)
1777 return (error);
1778
1779 de = vp->v_data;
1780 if (vp->v_type == VDIR)
1781 de = de->de_dir;
1782
1783 c = 0;
1784 if (vap->va_uid == (uid_t)VNOVAL)
1785 uid = de->de_uid;
1786 else
1787 uid = vap->va_uid;
1788 if (vap->va_gid == (gid_t)VNOVAL)
1789 gid = de->de_gid;
1790 else
1791 gid = vap->va_gid;
1792 if (uid != de->de_uid || gid != de->de_gid) {
1793 if ((ap->a_cred->cr_uid != de->de_uid) || uid != de->de_uid ||
1794 (gid != de->de_gid && !groupmember(gid, ap->a_cred))) {
1795 error = priv_check(td, PRIV_VFS_CHOWN);
1796 if (error != 0)
1797 goto ret;
1798 }
1799 de->de_uid = uid;
1800 de->de_gid = gid;
1801 c = 1;
1802 }
1803
1804 if (vap->va_mode != (mode_t)VNOVAL) {
1805 if (ap->a_cred->cr_uid != de->de_uid) {
1806 error = priv_check(td, PRIV_VFS_ADMIN);
1807 if (error != 0)
1808 goto ret;
1809 }
1810 de->de_mode = vap->va_mode;
1811 c = 1;
1812 }
1813
1814 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1815 error = vn_utimes_perm(vp, vap, ap->a_cred, td);
1816 if (error != 0)
1817 goto ret;
1818 if (vap->va_atime.tv_sec != VNOVAL) {
1819 if (vp->v_type == VCHR)
1820 vp->v_rdev->si_atime = vap->va_atime;
1821 else
1822 de->de_atime = vap->va_atime;
1823 }
1824 if (vap->va_mtime.tv_sec != VNOVAL) {
1825 if (vp->v_type == VCHR)
1826 vp->v_rdev->si_mtime = vap->va_mtime;
1827 else
1828 de->de_mtime = vap->va_mtime;
1829 }
1830 c = 1;
1831 }
1832
1833 if (c) {
1834 if (vp->v_type == VCHR)
1835 vfs_timestamp(&vp->v_rdev->si_ctime);
1836 else
1837 vfs_timestamp(&de->de_mtime);
1838 }
1839
1840 ret:
1841 sx_xunlock(&VFSTODEVFS(vp->v_mount)->dm_lock);
1842 return (error);
1843 }
1844
1845 #ifdef MAC
1846 static int
devfs_setlabel(struct vop_setlabel_args * ap)1847 devfs_setlabel(struct vop_setlabel_args *ap)
1848 {
1849 struct vnode *vp;
1850 struct devfs_dirent *de;
1851
1852 vp = ap->a_vp;
1853 de = vp->v_data;
1854
1855 mac_vnode_relabel(ap->a_cred, vp, ap->a_label);
1856 mac_devfs_update(vp->v_mount, de, vp);
1857
1858 return (0);
1859 }
1860 #endif
1861
1862 static int
devfs_stat_f(struct file * fp,struct stat * sb,struct ucred * cred,struct thread * td)1863 devfs_stat_f(struct file *fp, struct stat *sb, struct ucred *cred, struct thread *td)
1864 {
1865
1866 return (vnops.fo_stat(fp, sb, cred, td));
1867 }
1868
1869 static int
devfs_symlink(struct vop_symlink_args * ap)1870 devfs_symlink(struct vop_symlink_args *ap)
1871 {
1872 int i, error;
1873 struct devfs_dirent *dd;
1874 struct devfs_dirent *de, *de_covered, *de_dotdot;
1875 struct devfs_mount *dmp;
1876
1877 error = priv_check(curthread, PRIV_DEVFS_SYMLINK);
1878 if (error)
1879 return(error);
1880 dmp = VFSTODEVFS(ap->a_dvp->v_mount);
1881 if (devfs_populate_vp(ap->a_dvp) != 0)
1882 return (ENOENT);
1883
1884 dd = ap->a_dvp->v_data;
1885 de = devfs_newdirent(ap->a_cnp->cn_nameptr, ap->a_cnp->cn_namelen);
1886 de->de_flags = DE_USER;
1887 de->de_uid = 0;
1888 de->de_gid = 0;
1889 de->de_mode = 0755;
1890 de->de_inode = alloc_unr(devfs_inos);
1891 de->de_dir = dd;
1892 de->de_dirent->d_type = DT_LNK;
1893 i = strlen(ap->a_target) + 1;
1894 de->de_symlink = malloc(i, M_DEVFS, M_WAITOK);
1895 bcopy(ap->a_target, de->de_symlink, i);
1896 #ifdef MAC
1897 mac_devfs_create_symlink(ap->a_cnp->cn_cred, dmp->dm_mount, dd, de);
1898 #endif
1899 de_covered = devfs_find(dd, de->de_dirent->d_name,
1900 de->de_dirent->d_namlen, 0);
1901 if (de_covered != NULL) {
1902 if ((de_covered->de_flags & DE_USER) != 0) {
1903 devfs_delete(dmp, de, DEVFS_DEL_NORECURSE);
1904 sx_xunlock(&dmp->dm_lock);
1905 return (EEXIST);
1906 }
1907 KASSERT((de_covered->de_flags & DE_COVERED) == 0,
1908 ("devfs_symlink: entry %p already covered", de_covered));
1909 de_covered->de_flags |= DE_COVERED;
1910 }
1911
1912 de_dotdot = TAILQ_FIRST(&dd->de_dlist); /* "." */
1913 de_dotdot = TAILQ_NEXT(de_dotdot, de_list); /* ".." */
1914 TAILQ_INSERT_AFTER(&dd->de_dlist, de_dotdot, de, de_list);
1915 devfs_dir_ref_de(dmp, dd);
1916 devfs_rules_apply(dmp, de);
1917
1918 return (devfs_allocv(de, ap->a_dvp->v_mount, LK_EXCLUSIVE, ap->a_vpp));
1919 }
1920
1921 static int
devfs_truncate_f(struct file * fp,off_t length,struct ucred * cred,struct thread * td)1922 devfs_truncate_f(struct file *fp, off_t length, struct ucred *cred, struct thread *td)
1923 {
1924
1925 return (vnops.fo_truncate(fp, length, cred, td));
1926 }
1927
1928 static int
devfs_write_f(struct file * fp,struct uio * uio,struct ucred * cred,int flags,struct thread * td)1929 devfs_write_f(struct file *fp, struct uio *uio, struct ucred *cred,
1930 int flags, struct thread *td)
1931 {
1932 struct cdev *dev;
1933 int error, ioflag, ref;
1934 ssize_t resid;
1935 struct cdevsw *dsw;
1936 struct file *fpop;
1937
1938 if (uio->uio_resid > DEVFS_IOSIZE_MAX)
1939 return (EINVAL);
1940 fpop = td->td_fpop;
1941 error = devfs_fp_check(fp, &dev, &dsw, &ref);
1942 if (error != 0) {
1943 error = vnops.fo_write(fp, uio, cred, flags, td);
1944 return (error);
1945 }
1946 KASSERT(uio->uio_td == td, ("uio_td %p is not td %p", uio->uio_td, td));
1947 ioflag = fp->f_flag & (O_NONBLOCK | O_DIRECT | O_FSYNC);
1948 if (ioflag & O_DIRECT)
1949 ioflag |= IO_DIRECT;
1950 foffset_lock_uio(fp, uio, flags | FOF_NOLOCK);
1951
1952 resid = uio->uio_resid;
1953
1954 error = dsw->d_write(dev, uio, ioflag);
1955 if (uio->uio_resid != resid || (error == 0 && resid != 0)) {
1956 devfs_timestamp(&dev->si_ctime);
1957 dev->si_mtime = dev->si_ctime;
1958 }
1959 td->td_fpop = fpop;
1960 dev_relthread(dev, ref);
1961
1962 foffset_unlock_uio(fp, uio, flags | FOF_NOLOCK | FOF_NEXTOFF_W);
1963 return (error);
1964 }
1965
1966 static int
devfs_mmap_f(struct file * fp,vm_map_t map,vm_offset_t * addr,vm_size_t size,vm_prot_t prot,vm_prot_t cap_maxprot,int flags,vm_ooffset_t foff,struct thread * td)1967 devfs_mmap_f(struct file *fp, vm_map_t map, vm_offset_t *addr, vm_size_t size,
1968 vm_prot_t prot, vm_prot_t cap_maxprot, int flags, vm_ooffset_t foff,
1969 struct thread *td)
1970 {
1971 struct cdev *dev;
1972 struct cdevsw *dsw;
1973 struct mount *mp;
1974 struct vnode *vp;
1975 struct file *fpop;
1976 vm_object_t object;
1977 vm_prot_t maxprot;
1978 int error, ref;
1979
1980 vp = fp->f_vnode;
1981
1982 /*
1983 * Ensure that file and memory protections are
1984 * compatible.
1985 */
1986 mp = vp->v_mount;
1987 if (mp != NULL && (mp->mnt_flag & MNT_NOEXEC) != 0) {
1988 maxprot = VM_PROT_NONE;
1989 if ((prot & VM_PROT_EXECUTE) != 0)
1990 return (EACCES);
1991 } else
1992 maxprot = VM_PROT_EXECUTE;
1993 if ((fp->f_flag & FREAD) != 0)
1994 maxprot |= VM_PROT_READ;
1995 else if ((prot & VM_PROT_READ) != 0)
1996 return (EACCES);
1997
1998 /*
1999 * If we are sharing potential changes via MAP_SHARED and we
2000 * are trying to get write permission although we opened it
2001 * without asking for it, bail out.
2002 *
2003 * Note that most character devices always share mappings.
2004 * The one exception is that D_MMAP_ANON devices
2005 * (i.e. /dev/zero) permit private writable mappings.
2006 *
2007 * Rely on vm_mmap_cdev() to fail invalid MAP_PRIVATE requests
2008 * as well as updating maxprot to permit writing for
2009 * D_MMAP_ANON devices rather than doing that here.
2010 */
2011 if ((flags & MAP_SHARED) != 0) {
2012 if ((fp->f_flag & FWRITE) != 0)
2013 maxprot |= VM_PROT_WRITE;
2014 else if ((prot & VM_PROT_WRITE) != 0)
2015 return (EACCES);
2016 }
2017 maxprot &= cap_maxprot;
2018
2019 fpop = td->td_fpop;
2020 error = devfs_fp_check(fp, &dev, &dsw, &ref);
2021 if (error != 0)
2022 return (error);
2023
2024 error = vm_mmap_cdev(td, size, prot, &maxprot, &flags, dev, dsw, &foff,
2025 &object);
2026 td->td_fpop = fpop;
2027 dev_relthread(dev, ref);
2028 if (error != 0)
2029 return (error);
2030
2031 error = vm_mmap_object(map, addr, size, prot, maxprot, flags, object,
2032 foff, FALSE, td);
2033 if (error != 0)
2034 vm_object_deallocate(object);
2035 return (error);
2036 }
2037
2038 dev_t
dev2udev(struct cdev * x)2039 dev2udev(struct cdev *x)
2040 {
2041 if (x == NULL)
2042 return (NODEV);
2043 return (cdev2priv(x)->cdp_inode);
2044 }
2045
2046 static int
devfs_cmp_f(struct file * fp1,struct file * fp2,struct thread * td)2047 devfs_cmp_f(struct file *fp1, struct file *fp2, struct thread *td)
2048 {
2049 if (fp2->f_type != DTYPE_VNODE || fp2->f_ops != &devfs_ops_f)
2050 return (3);
2051 return (kcmp_cmp((uintptr_t)fp1->f_data, (uintptr_t)fp2->f_data));
2052 }
2053
2054 static struct fileops devfs_ops_f = {
2055 .fo_read = devfs_read_f,
2056 .fo_write = devfs_write_f,
2057 .fo_truncate = devfs_truncate_f,
2058 .fo_ioctl = devfs_ioctl_f,
2059 .fo_poll = devfs_poll_f,
2060 .fo_kqfilter = devfs_kqfilter_f,
2061 .fo_stat = devfs_stat_f,
2062 .fo_close = devfs_close_f,
2063 .fo_chmod = vn_chmod,
2064 .fo_chown = vn_chown,
2065 .fo_sendfile = vn_sendfile,
2066 .fo_seek = vn_seek,
2067 .fo_fill_kinfo = vn_fill_kinfo,
2068 .fo_mmap = devfs_mmap_f,
2069 .fo_cmp = devfs_cmp_f,
2070 .fo_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE
2071 };
2072
2073 /* Vops for non-CHR vnodes in /dev. */
2074 static struct vop_vector devfs_vnodeops = {
2075 .vop_default = &default_vnodeops,
2076
2077 .vop_access = devfs_access,
2078 .vop_getattr = devfs_getattr,
2079 .vop_ioctl = devfs_rioctl,
2080 .vop_lookup = devfs_lookup,
2081 .vop_mknod = devfs_mknod,
2082 .vop_pathconf = devfs_pathconf,
2083 .vop_read = devfs_rread,
2084 .vop_readdir = devfs_readdir,
2085 .vop_readlink = devfs_readlink,
2086 .vop_reclaim = devfs_reclaim,
2087 .vop_remove = devfs_remove,
2088 .vop_revoke = devfs_revoke,
2089 .vop_setattr = devfs_setattr,
2090 #ifdef MAC
2091 .vop_setlabel = devfs_setlabel,
2092 #endif
2093 .vop_symlink = devfs_symlink,
2094 .vop_vptocnp = devfs_vptocnp,
2095 .vop_lock1 = vop_lock,
2096 .vop_unlock = vop_unlock,
2097 .vop_islocked = vop_islocked,
2098 };
2099 VFS_VOP_VECTOR_REGISTER(devfs_vnodeops);
2100
2101 /* Vops for VCHR vnodes in /dev. */
2102 static struct vop_vector devfs_specops = {
2103 .vop_default = &default_vnodeops,
2104
2105 .vop_access = devfs_access,
2106 .vop_bmap = VOP_PANIC,
2107 .vop_close = devfs_close,
2108 .vop_create = VOP_PANIC,
2109 .vop_fsync = vop_stdfsync,
2110 .vop_getattr = devfs_getattr,
2111 .vop_ioctl = devfs_ioctl,
2112 .vop_link = VOP_PANIC,
2113 .vop_mkdir = VOP_PANIC,
2114 .vop_mknod = VOP_PANIC,
2115 .vop_open = devfs_open,
2116 .vop_pathconf = devfs_pathconf,
2117 .vop_poll = dead_poll,
2118 .vop_print = devfs_print,
2119 .vop_read = dead_read,
2120 .vop_readdir = VOP_PANIC,
2121 .vop_readlink = VOP_PANIC,
2122 .vop_reallocblks = VOP_PANIC,
2123 .vop_reclaim = devfs_reclaim_vchr,
2124 .vop_remove = devfs_remove,
2125 .vop_rename = VOP_PANIC,
2126 .vop_revoke = devfs_revoke,
2127 .vop_rmdir = VOP_PANIC,
2128 .vop_setattr = devfs_setattr,
2129 #ifdef MAC
2130 .vop_setlabel = devfs_setlabel,
2131 #endif
2132 .vop_strategy = VOP_PANIC,
2133 .vop_symlink = VOP_PANIC,
2134 .vop_vptocnp = devfs_vptocnp,
2135 .vop_write = dead_write,
2136 .vop_lock1 = vop_lock,
2137 .vop_unlock = vop_unlock,
2138 .vop_islocked = vop_islocked,
2139 };
2140 VFS_VOP_VECTOR_REGISTER(devfs_specops);
2141
2142 /*
2143 * Our calling convention to the device drivers used to be that we passed
2144 * vnode.h IO_* flags to read()/write(), but we're moving to fcntl.h O_
2145 * flags instead since that's what open(), close() and ioctl() takes and
2146 * we don't really want vnode.h in device drivers.
2147 * We solved the source compatibility by redefining some vnode flags to
2148 * be the same as the fcntl ones and by sending down the bitwise OR of
2149 * the respective fcntl/vnode flags. These CTASSERTS make sure nobody
2150 * pulls the rug out under this.
2151 */
2152 CTASSERT(O_NONBLOCK == IO_NDELAY);
2153 CTASSERT(O_FSYNC == IO_SYNC);
2154