2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software donated to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)fdesc_vnops.c 8.9 (Berkeley) 1/21/94
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/capsicum.h>
47 #include <sys/dirent.h>
48 #include <sys/filedesc.h>
49 #include <sys/kernel.h> /* boottime */
51 #include <sys/mutex.h>
52 #include <sys/malloc.h>
53 #include <sys/file.h> /* Must come after sys/malloc.h */
54 #include <sys/mount.h>
55 #include <sys/namei.h>
58 #include <sys/vnode.h>
60 #include <fs/fdescfs/fdesc.h>
63 #define FD_NHASH(ix) \
64 (&fdhashtbl[(ix) & fdhash])
65 static LIST_HEAD(fdhashhead, fdescnode) *fdhashtbl;
68 struct mtx fdesc_hashmtx;
70 static vop_getattr_t fdesc_getattr;
71 static vop_lookup_t fdesc_lookup;
72 static vop_open_t fdesc_open;
73 static vop_readdir_t fdesc_readdir;
74 static vop_readlink_t fdesc_readlink;
75 static vop_reclaim_t fdesc_reclaim;
76 static vop_setattr_t fdesc_setattr;
78 static struct vop_vector fdesc_vnodeops = {
79 .vop_default = &default_vnodeops,
81 .vop_access = VOP_NULL,
82 .vop_getattr = fdesc_getattr,
83 .vop_lookup = fdesc_lookup,
84 .vop_open = fdesc_open,
85 .vop_pathconf = vop_stdpathconf,
86 .vop_readdir = fdesc_readdir,
87 .vop_readlink = fdesc_readlink,
88 .vop_reclaim = fdesc_reclaim,
89 .vop_setattr = fdesc_setattr,
92 static void fdesc_insmntque_dtr(struct vnode *, void *);
93 static void fdesc_remove_entry(struct fdescnode *);
96 * Initialise cache headers
99 fdesc_init(struct vfsconf *vfsp)
102 mtx_init(&fdesc_hashmtx, "fdescfs_hash", NULL, MTX_DEF);
103 fdhashtbl = hashinit(NFDCACHE, M_CACHE, &fdhash);
108 * Uninit ready for unload.
111 fdesc_uninit(struct vfsconf *vfsp)
114 hashdestroy(fdhashtbl, M_CACHE, fdhash);
115 mtx_destroy(&fdesc_hashmtx);
120 * If allocating vnode fails, call this.
123 fdesc_insmntque_dtr(struct vnode *vp, void *arg)
131 * Remove an entry from the hash if it exists.
134 fdesc_remove_entry(struct fdescnode *fd)
136 struct fdhashhead *fc;
137 struct fdescnode *fd2;
139 fc = FD_NHASH(fd->fd_ix);
140 mtx_lock(&fdesc_hashmtx);
141 LIST_FOREACH(fd2, fc, fd_hash) {
143 LIST_REMOVE(fd, fd_hash);
147 mtx_unlock(&fdesc_hashmtx);
151 fdesc_allocvp(fdntype ftype, unsigned fd_fd, int ix, struct mount *mp,
154 struct fdescmount *fmp;
155 struct fdhashhead *fc;
156 struct fdescnode *fd, *fd2;
157 struct vnode *vp, *vp2;
164 mtx_lock(&fdesc_hashmtx);
166 * If a forced unmount is progressing, we need to drop it. The flags are
167 * protected by the hashmtx.
170 if (fmp == NULL || fmp->flags & FMNT_UNMOUNTF) {
171 mtx_unlock(&fdesc_hashmtx);
175 LIST_FOREACH(fd, fc, fd_hash) {
176 if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) {
177 /* Get reference to vnode in case it's being free'd */
180 mtx_unlock(&fdesc_hashmtx);
181 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td))
187 mtx_unlock(&fdesc_hashmtx);
189 fd = malloc(sizeof(struct fdescnode), M_TEMP, M_WAITOK);
191 error = getnewvnode("fdescfs", mp, &fdesc_vnodeops, &vp);
196 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
202 if (ftype == Fdesc && fmp->flags & FMNT_LINRDLNKF)
203 vp->v_vflag |= VV_READLINK;
204 error = insmntque1(vp, mp, fdesc_insmntque_dtr, NULL);
210 /* Make sure that someone didn't beat us when inserting the vnode. */
211 mtx_lock(&fdesc_hashmtx);
213 * If a forced unmount is progressing, we need to drop it. The flags are
214 * protected by the hashmtx.
217 if (fmp == NULL || fmp->flags & FMNT_UNMOUNTF) {
218 mtx_unlock(&fdesc_hashmtx);
225 LIST_FOREACH(fd2, fc, fd_hash) {
226 if (fd2->fd_ix == ix && fd2->fd_vnode->v_mount == mp) {
227 /* Get reference to vnode in case it's being free'd */
230 mtx_unlock(&fdesc_hashmtx);
231 error = vget(vp2, LK_EXCLUSIVE | LK_INTERLOCK, td);
232 /* Someone beat us, dec use count and wait for reclaim */
235 /* If we didn't get it, return no vnode. */
243 /* If we came here, we can insert it safely. */
244 LIST_INSERT_HEAD(fc, fd, fd_hash);
245 mtx_unlock(&fdesc_hashmtx);
250 struct fdesc_get_ino_args {
259 fdesc_get_ino_alloc(struct mount *mp, void *arg, int lkflags,
262 struct fdesc_get_ino_args *a;
266 error = fdesc_allocvp(a->ftype, a->fd_fd, a->ix, mp, rvp);
273 * vp is the current namei directory
274 * ndp is the name to locate in that directory...
277 fdesc_lookup(struct vop_lookup_args *ap)
279 struct vnode **vpp = ap->a_vpp;
280 struct vnode *dvp = ap->a_dvp;
281 struct componentname *cnp = ap->a_cnp;
282 char *pname = cnp->cn_nameptr;
283 struct thread *td = cnp->cn_thread;
285 struct fdesc_get_ino_args arg;
287 int nlen = cnp->cn_namelen;
292 if ((cnp->cn_flags & ISLASTCN) &&
293 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
298 if (cnp->cn_namelen == 1 && *pname == '.') {
304 if (VTOFDESC(dvp)->fd_type != Froot) {
310 /* the only time a leading 0 is acceptable is if it's "0" */
311 if (*pname == '0' && nlen != 1) {
316 if (*pname < '0' || *pname > '9') {
320 fd1 = 10 * fd + *pname++ - '0';
329 * No rights to check since 'fp' isn't actually used.
331 if ((error = fget(td, fd, cap_rights_init(&rights), &fp)) != 0)
334 /* Check if we're looking up ourselves. */
335 if (VTOFDESC(dvp)->fd_ix == FD_DESC + fd) {
337 * In case we're holding the last reference to the file, the dvp
338 * will be re-acquired.
344 /* Re-aquire the lock afterwards. */
345 vn_lock(dvp, LK_RETRY | LK_EXCLUSIVE);
348 if ((dvp->v_iflag & VI_DOOMED) != 0)
352 * Unlock our root node (dvp) when doing this, since we might
353 * deadlock since the vnode might be locked by another thread
354 * and the root vnode lock will be obtained afterwards (in case
355 * we're looking up the fd of the root vnode), which will be the
356 * opposite lock order. Vhold the root vnode first so we don't
361 arg.ix = FD_DESC + fd;
364 error = vn_vget_ino_gen(dvp, fdesc_get_ino_alloc, &arg,
379 fdesc_open(struct vop_open_args *ap)
381 struct vnode *vp = ap->a_vp;
383 if (VTOFDESC(vp)->fd_type == Froot)
387 * XXX Kludge: set td->td_proc->p_dupfd to contain the value of the file
388 * descriptor being sought for duplication. The error return ensures
389 * that the vnode for this device will be released by vn_open. Open
390 * will detect this special error and take the actions in dupfdopen.
391 * Other callers of vn_open or VOP_OPEN will simply report the
394 ap->a_td->td_dupfd = VTOFDESC(vp)->fd_fd; /* XXX */
399 fdesc_getattr(struct vop_getattr_args *ap)
401 struct vnode *vp = ap->a_vp;
402 struct vattr *vap = ap->a_vap;
403 struct timeval boottime;
405 getboottime(&boottime);
406 vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
407 vap->va_fileid = VTOFDESC(vp)->fd_ix;
410 vap->va_blocksize = DEV_BSIZE;
411 vap->va_atime.tv_sec = boottime.tv_sec;
412 vap->va_atime.tv_nsec = 0;
413 vap->va_mtime = vap->va_atime;
414 vap->va_ctime = vap->va_mtime;
420 switch (VTOFDESC(vp)->fd_type) {
424 vap->va_size = DEV_BSIZE;
425 vap->va_rdev = NODEV;
429 vap->va_type = (vp->v_vflag & VV_READLINK) == 0 ? VCHR : VLNK;
432 vap->va_rdev = makedev(0, vap->va_fileid);
436 panic("fdesc_getattr");
440 vp->v_type = vap->va_type;
445 fdesc_setattr(struct vop_setattr_args *ap)
447 struct vattr *vap = ap->a_vap;
451 struct thread *td = curthread;
457 * Can't mess with the root vnode
459 if (VTOFDESC(ap->a_vp)->fd_type == Froot)
462 fd = VTOFDESC(ap->a_vp)->fd_fd;
465 * Allow setattr where there is an underlying vnode.
467 error = getvnode(td, fd,
468 cap_rights_init(&rights, CAP_EXTATTR_SET), &fp);
471 * getvnode() returns EINVAL if the file descriptor is not
472 * backed by a vnode. Silently drop all changes except
473 * chflags(2) in this case.
475 if (error == EINVAL) {
476 if (vap->va_flags != VNOVAL)
484 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) == 0) {
485 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
486 error = VOP_SETATTR(vp, ap->a_vap, ap->a_cred);
488 vn_finished_write(mp);
494 #define UIO_MX _GENERIC_DIRLEN(10) /* number of symbols in INT_MAX printout */
497 fdesc_readdir(struct vop_readdir_args *ap)
499 struct fdescmount *fmp;
500 struct uio *uio = ap->a_uio;
501 struct filedesc *fdp;
503 struct dirent *dp = &d;
504 int error, i, off, fcnt;
506 if (VTOFDESC(ap->a_vp)->fd_type != Froot)
507 panic("fdesc_readdir: not dir");
509 fmp = VFSTOFDESC(ap->a_vp->v_mount);
510 if (ap->a_ncookies != NULL)
513 off = (int)uio->uio_offset;
514 if (off != uio->uio_offset || off < 0 || (u_int)off % UIO_MX != 0 ||
515 uio->uio_resid < UIO_MX)
517 i = (u_int)off / UIO_MX;
518 fdp = uio->uio_td->td_proc->p_fd;
521 fcnt = i - 2; /* The first two nodes are `.' and `..' */
524 while (i < fdp->fd_nfiles + 2 && uio->uio_resid >= UIO_MX) {
525 bzero((caddr_t)dp, UIO_MX);
529 dp->d_fileno = i + FD_ROOT;
530 dp->d_namlen = i + 1;
531 dp->d_reclen = UIO_MX;
532 bcopy("..", dp->d_name, dp->d_namlen);
533 dp->d_name[i + 1] = '\0';
537 if (fdp->fd_ofiles[fcnt].fde_file == NULL)
539 dp->d_namlen = sprintf(dp->d_name, "%d", fcnt);
540 dp->d_reclen = UIO_MX;
541 dp->d_type = (fmp->flags & FMNT_LINRDLNKF) == 0 ?
543 dp->d_fileno = i + FD_DESC;
546 if (dp->d_namlen != 0) {
548 * And ship to userland
550 FILEDESC_SUNLOCK(fdp);
551 error = uiomove(dp, UIO_MX, uio);
559 FILEDESC_SUNLOCK(fdp);
562 uio->uio_offset = i * UIO_MX;
567 fdesc_reclaim(struct vop_reclaim_args *ap)
570 struct fdescnode *fd;
574 fdesc_remove_entry(fd);
575 free(vp->v_data, M_TEMP);
581 fdesc_readlink(struct vop_readlink_args *va)
583 struct vnode *vp, *vn;
588 char *freepath, *fullpath;
590 int lockflags, fd_fd;
595 if (VTOFDESC(vn)->fd_type != Fdesc)
596 panic("fdesc_readlink: not fdescfs link");
597 fd_fd = ((struct fdescnode *)vn->v_data)->fd_fd;
598 lockflags = VOP_ISLOCKED(vn);
602 error = fget_cap(td, fd_fd, cap_rights_init(&rights), &fp, NULL);
606 switch (fp->f_type) {
609 error = vn_fullpath(td, vp, &fullpath, &freepath);
612 fullpath = "anon_inode:[unknown]";
617 pathlen = strlen(fullpath);
618 error = uiomove(fullpath, pathlen, uio);
620 if (freepath != NULL)
621 free(freepath, M_TEMP);
625 vn_lock(vn, lockflags | LK_RETRY);