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