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