2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software donated to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)fdesc_vnops.c 8.9 (Berkeley) 1/21/94
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/capsicum.h>
45 #include <sys/dirent.h>
46 #include <sys/filedesc.h>
47 #include <sys/kernel.h> /* boottime */
49 #include <sys/mutex.h>
50 #include <sys/malloc.h>
51 #include <sys/file.h> /* Must come after sys/malloc.h */
52 #include <sys/mount.h>
53 #include <sys/namei.h>
56 #include <sys/vnode.h>
58 #include <fs/fdescfs/fdesc.h>
61 #define FD_NHASH(ix) \
62 (&fdhashtbl[(ix) & fdhash])
63 static LIST_HEAD(fdhashhead, fdescnode) *fdhashtbl;
66 struct mtx fdesc_hashmtx;
68 static vop_getattr_t fdesc_getattr;
69 static vop_lookup_t fdesc_lookup;
70 static vop_open_t fdesc_open;
71 static vop_readdir_t fdesc_readdir;
72 static vop_readlink_t fdesc_readlink;
73 static vop_reclaim_t fdesc_reclaim;
74 static vop_setattr_t fdesc_setattr;
76 static struct vop_vector fdesc_vnodeops = {
77 .vop_default = &default_vnodeops,
79 .vop_access = VOP_NULL,
80 .vop_getattr = fdesc_getattr,
81 .vop_lookup = fdesc_lookup,
82 .vop_open = fdesc_open,
83 .vop_pathconf = vop_stdpathconf,
84 .vop_readdir = fdesc_readdir,
85 .vop_readlink = fdesc_readlink,
86 .vop_reclaim = fdesc_reclaim,
87 .vop_setattr = fdesc_setattr,
90 static void fdesc_insmntque_dtr(struct vnode *, void *);
91 static void fdesc_remove_entry(struct fdescnode *);
94 * Initialise cache headers
97 fdesc_init(struct vfsconf *vfsp)
100 mtx_init(&fdesc_hashmtx, "fdescfs_hash", NULL, MTX_DEF);
101 fdhashtbl = hashinit(NFDCACHE, M_CACHE, &fdhash);
106 * Uninit ready for unload.
109 fdesc_uninit(struct vfsconf *vfsp)
112 hashdestroy(fdhashtbl, M_CACHE, fdhash);
113 mtx_destroy(&fdesc_hashmtx);
118 * If allocating vnode fails, call this.
121 fdesc_insmntque_dtr(struct vnode *vp, void *arg)
129 * Remove an entry from the hash if it exists.
132 fdesc_remove_entry(struct fdescnode *fd)
134 struct fdhashhead *fc;
135 struct fdescnode *fd2;
137 fc = FD_NHASH(fd->fd_ix);
138 mtx_lock(&fdesc_hashmtx);
139 LIST_FOREACH(fd2, fc, fd_hash) {
141 LIST_REMOVE(fd, fd_hash);
145 mtx_unlock(&fdesc_hashmtx);
149 fdesc_allocvp(fdntype ftype, unsigned fd_fd, int ix, struct mount *mp,
152 struct fdescmount *fmp;
153 struct fdhashhead *fc;
154 struct fdescnode *fd, *fd2;
155 struct vnode *vp, *vp2;
162 mtx_lock(&fdesc_hashmtx);
164 * If a forced unmount is progressing, we need to drop it. The flags are
165 * protected by the hashmtx.
168 if (fmp == NULL || fmp->flags & FMNT_UNMOUNTF) {
169 mtx_unlock(&fdesc_hashmtx);
173 LIST_FOREACH(fd, fc, fd_hash) {
174 if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) {
175 /* Get reference to vnode in case it's being free'd */
178 mtx_unlock(&fdesc_hashmtx);
179 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td))
185 mtx_unlock(&fdesc_hashmtx);
187 fd = malloc(sizeof(struct fdescnode), M_TEMP, M_WAITOK);
189 error = getnewvnode("fdescfs", mp, &fdesc_vnodeops, &vp);
194 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
200 if (ftype == Fdesc && fmp->flags & FMNT_LINRDLNKF)
201 vp->v_vflag |= VV_READLINK;
202 error = insmntque1(vp, mp, fdesc_insmntque_dtr, NULL);
208 /* Make sure that someone didn't beat us when inserting the vnode. */
209 mtx_lock(&fdesc_hashmtx);
211 * If a forced unmount is progressing, we need to drop it. The flags are
212 * protected by the hashmtx.
215 if (fmp == NULL || fmp->flags & FMNT_UNMOUNTF) {
216 mtx_unlock(&fdesc_hashmtx);
223 LIST_FOREACH(fd2, fc, fd_hash) {
224 if (fd2->fd_ix == ix && fd2->fd_vnode->v_mount == mp) {
225 /* Get reference to vnode in case it's being free'd */
228 mtx_unlock(&fdesc_hashmtx);
229 error = vget(vp2, LK_EXCLUSIVE | LK_INTERLOCK, td);
230 /* Someone beat us, dec use count and wait for reclaim */
233 /* If we didn't get it, return no vnode. */
241 /* If we came here, we can insert it safely. */
242 LIST_INSERT_HEAD(fc, fd, fd_hash);
243 mtx_unlock(&fdesc_hashmtx);
248 struct fdesc_get_ino_args {
257 fdesc_get_ino_alloc(struct mount *mp, void *arg, int lkflags,
260 struct fdesc_get_ino_args *a;
264 error = fdesc_allocvp(a->ftype, a->fd_fd, a->ix, mp, rvp);
271 * vp is the current namei directory
272 * ndp is the name to locate in that directory...
275 fdesc_lookup(struct vop_lookup_args *ap)
277 struct vnode **vpp = ap->a_vpp;
278 struct vnode *dvp = ap->a_dvp;
279 struct componentname *cnp = ap->a_cnp;
280 char *pname = cnp->cn_nameptr;
281 struct thread *td = cnp->cn_thread;
283 struct fdesc_get_ino_args arg;
285 int nlen = cnp->cn_namelen;
290 if ((cnp->cn_flags & ISLASTCN) &&
291 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
296 if (cnp->cn_namelen == 1 && *pname == '.') {
302 if (VTOFDESC(dvp)->fd_type != Froot) {
308 /* the only time a leading 0 is acceptable is if it's "0" */
309 if (*pname == '0' && nlen != 1) {
314 if (*pname < '0' || *pname > '9') {
318 fd1 = 10 * fd + *pname++ - '0';
327 * No rights to check since 'fp' isn't actually used.
329 if ((error = fget(td, fd, cap_rights_init(&rights), &fp)) != 0)
332 /* Check if we're looking up ourselves. */
333 if (VTOFDESC(dvp)->fd_ix == FD_DESC + fd) {
335 * In case we're holding the last reference to the file, the dvp
336 * will be re-acquired.
342 /* Re-aquire the lock afterwards. */
343 vn_lock(dvp, LK_RETRY | LK_EXCLUSIVE);
346 if ((dvp->v_iflag & VI_DOOMED) != 0)
350 * Unlock our root node (dvp) when doing this, since we might
351 * deadlock since the vnode might be locked by another thread
352 * and the root vnode lock will be obtained afterwards (in case
353 * we're looking up the fd of the root vnode), which will be the
354 * opposite lock order. Vhold the root vnode first so we don't
359 arg.ix = FD_DESC + fd;
362 error = vn_vget_ino_gen(dvp, fdesc_get_ino_alloc, &arg,
377 fdesc_open(struct vop_open_args *ap)
379 struct vnode *vp = ap->a_vp;
381 if (VTOFDESC(vp)->fd_type == Froot)
385 * XXX Kludge: set td->td_proc->p_dupfd to contain the value of the file
386 * descriptor being sought for duplication. The error return ensures
387 * that the vnode for this device will be released by vn_open. Open
388 * will detect this special error and take the actions in dupfdopen.
389 * Other callers of vn_open or VOP_OPEN will simply report the
392 ap->a_td->td_dupfd = VTOFDESC(vp)->fd_fd; /* XXX */
397 fdesc_getattr(struct vop_getattr_args *ap)
399 struct vnode *vp = ap->a_vp;
400 struct vattr *vap = ap->a_vap;
401 struct timeval boottime;
403 getboottime(&boottime);
404 vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
405 vap->va_fileid = VTOFDESC(vp)->fd_ix;
408 vap->va_blocksize = DEV_BSIZE;
409 vap->va_atime.tv_sec = boottime.tv_sec;
410 vap->va_atime.tv_nsec = 0;
411 vap->va_mtime = vap->va_atime;
412 vap->va_ctime = vap->va_mtime;
418 switch (VTOFDESC(vp)->fd_type) {
422 vap->va_size = DEV_BSIZE;
423 vap->va_rdev = NODEV;
427 vap->va_type = (vp->v_vflag & VV_READLINK) == 0 ? VCHR : VLNK;
430 vap->va_rdev = makedev(0, vap->va_fileid);
434 panic("fdesc_getattr");
438 vp->v_type = vap->va_type;
443 fdesc_setattr(struct vop_setattr_args *ap)
445 struct vattr *vap = ap->a_vap;
449 struct thread *td = curthread;
455 * Can't mess with the root vnode
457 if (VTOFDESC(ap->a_vp)->fd_type == Froot)
460 fd = VTOFDESC(ap->a_vp)->fd_fd;
463 * Allow setattr where there is an underlying vnode.
465 error = getvnode(td, fd,
466 cap_rights_init(&rights, CAP_EXTATTR_SET), &fp);
469 * getvnode() returns EINVAL if the file descriptor is not
470 * backed by a vnode. Silently drop all changes except
471 * chflags(2) in this case.
473 if (error == EINVAL) {
474 if (vap->va_flags != VNOVAL)
482 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) == 0) {
483 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
484 error = VOP_SETATTR(vp, ap->a_vap, ap->a_cred);
486 vn_finished_write(mp);
492 #define UIO_MX _GENERIC_DIRLEN(10) /* number of symbols in INT_MAX printout */
495 fdesc_readdir(struct vop_readdir_args *ap)
497 struct fdescmount *fmp;
498 struct uio *uio = ap->a_uio;
499 struct filedesc *fdp;
501 struct dirent *dp = &d;
502 int error, i, off, fcnt;
504 if (VTOFDESC(ap->a_vp)->fd_type != Froot)
505 panic("fdesc_readdir: not dir");
507 fmp = VFSTOFDESC(ap->a_vp->v_mount);
508 if (ap->a_ncookies != NULL)
511 off = (int)uio->uio_offset;
512 if (off != uio->uio_offset || off < 0 || (u_int)off % UIO_MX != 0 ||
513 uio->uio_resid < UIO_MX)
515 i = (u_int)off / UIO_MX;
516 fdp = uio->uio_td->td_proc->p_fd;
519 fcnt = i - 2; /* The first two nodes are `.' and `..' */
522 while (i < fdp->fd_nfiles + 2 && uio->uio_resid >= UIO_MX) {
523 bzero((caddr_t)dp, UIO_MX);
527 dp->d_fileno = i + FD_ROOT;
528 dp->d_namlen = i + 1;
529 dp->d_reclen = UIO_MX;
530 bcopy("..", dp->d_name, dp->d_namlen);
531 dp->d_name[i + 1] = '\0';
535 if (fdp->fd_ofiles[fcnt].fde_file == NULL)
537 dp->d_namlen = sprintf(dp->d_name, "%d", fcnt);
538 dp->d_reclen = UIO_MX;
539 dp->d_type = (fmp->flags & FMNT_LINRDLNKF) == 0 ?
541 dp->d_fileno = i + FD_DESC;
544 if (dp->d_namlen != 0) {
546 * And ship to userland
548 FILEDESC_SUNLOCK(fdp);
549 error = uiomove(dp, UIO_MX, uio);
557 FILEDESC_SUNLOCK(fdp);
560 uio->uio_offset = i * UIO_MX;
565 fdesc_reclaim(struct vop_reclaim_args *ap)
568 struct fdescnode *fd;
572 fdesc_remove_entry(fd);
573 free(vp->v_data, M_TEMP);
579 fdesc_readlink(struct vop_readlink_args *va)
581 struct vnode *vp, *vn;
586 char *freepath, *fullpath;
588 int lockflags, fd_fd;
593 if (VTOFDESC(vn)->fd_type != Fdesc)
594 panic("fdesc_readlink: not fdescfs link");
595 fd_fd = ((struct fdescnode *)vn->v_data)->fd_fd;
596 lockflags = VOP_ISLOCKED(vn);
600 error = fget_cap(td, fd_fd, cap_rights_init(&rights), &fp, NULL);
604 switch (fp->f_type) {
607 error = vn_fullpath(td, vp, &fullpath, &freepath);
610 fullpath = "anon_inode:[unknown]";
615 pathlen = strlen(fullpath);
616 error = uiomove(fullpath, pathlen, uio);
618 if (freepath != NULL)
619 free(freepath, M_TEMP);
623 vn_lock(vn, lockflags | LK_RETRY);