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