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