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