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/syscallsubr.h>
59 #include <sys/unistd.h>
60 #include <sys/vnode.h>
62 #include <fs/fdescfs/fdesc.h>
65 #define FD_NHASH(ix) \
66 (&fdhashtbl[(ix) & fdhash])
67 static LIST_HEAD(fdhashhead, fdescnode) *fdhashtbl;
70 struct mtx fdesc_hashmtx;
72 static vop_getattr_t fdesc_getattr;
73 static vop_lookup_t fdesc_lookup;
74 static vop_open_t fdesc_open;
75 static vop_pathconf_t fdesc_pathconf;
76 static vop_readdir_t fdesc_readdir;
77 static vop_readlink_t fdesc_readlink;
78 static vop_reclaim_t fdesc_reclaim;
79 static vop_setattr_t fdesc_setattr;
81 static struct vop_vector fdesc_vnodeops = {
82 .vop_default = &default_vnodeops,
84 .vop_access = VOP_NULL,
85 .vop_getattr = fdesc_getattr,
86 .vop_lookup = fdesc_lookup,
87 .vop_open = fdesc_open,
88 .vop_pathconf = fdesc_pathconf,
89 .vop_readdir = fdesc_readdir,
90 .vop_readlink = fdesc_readlink,
91 .vop_reclaim = fdesc_reclaim,
92 .vop_setattr = fdesc_setattr,
94 VFS_VOP_VECTOR_REGISTER(fdesc_vnodeops);
96 static void fdesc_remove_entry(struct fdescnode *);
99 * Initialise cache headers
102 fdesc_init(struct vfsconf *vfsp)
105 mtx_init(&fdesc_hashmtx, "fdescfs_hash", NULL, MTX_DEF);
106 fdhashtbl = hashinit(NFDCACHE, M_CACHE, &fdhash);
111 * Uninit ready for unload.
114 fdesc_uninit(struct vfsconf *vfsp)
117 hashdestroy(fdhashtbl, M_CACHE, fdhash);
118 mtx_destroy(&fdesc_hashmtx);
123 * Remove an entry from the hash if it exists.
126 fdesc_remove_entry(struct fdescnode *fd)
128 struct fdhashhead *fc;
129 struct fdescnode *fd2;
131 fc = FD_NHASH(fd->fd_ix);
132 mtx_lock(&fdesc_hashmtx);
133 LIST_FOREACH(fd2, fc, fd_hash) {
135 LIST_REMOVE(fd, fd_hash);
139 mtx_unlock(&fdesc_hashmtx);
143 fdesc_allocvp(fdntype ftype, unsigned fd_fd, int ix, struct mount *mp,
146 struct fdescmount *fmp;
147 struct fdhashhead *fc;
148 struct fdescnode *fd, *fd2;
149 struct vnode *vp, *vp2;
155 mtx_lock(&fdesc_hashmtx);
157 * If a forced unmount is progressing, we need to drop it. The flags are
158 * protected by the hashmtx.
161 if (fmp == NULL || fmp->flags & FMNT_UNMOUNTF) {
162 mtx_unlock(&fdesc_hashmtx);
166 LIST_FOREACH(fd, fc, fd_hash) {
167 if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) {
168 /* Get reference to vnode in case it's being free'd */
171 mtx_unlock(&fdesc_hashmtx);
172 if (vget_finish(vp, LK_EXCLUSIVE, vgs) != 0)
178 mtx_unlock(&fdesc_hashmtx);
180 fd = malloc(sizeof(struct fdescnode), M_TEMP, M_WAITOK);
182 error = getnewvnode("fdescfs", mp, &fdesc_vnodeops, &vp);
187 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
193 if (ftype == Fdesc) {
194 if ((fmp->flags & FMNT_RDLNKF) != 0)
196 else if ((fmp->flags & FMNT_LINRDLNKF) != 0)
197 vp->v_vflag |= VV_READLINK;
199 error = insmntque1(vp, mp);
207 /* Make sure that someone didn't beat us when inserting the vnode. */
208 mtx_lock(&fdesc_hashmtx);
210 * If a forced unmount is progressing, we need to drop it. The flags are
211 * protected by the hashmtx.
214 if (fmp == NULL || fmp->flags & FMNT_UNMOUNTF) {
215 mtx_unlock(&fdesc_hashmtx);
222 LIST_FOREACH(fd2, fc, fd_hash) {
223 if (fd2->fd_ix == ix && fd2->fd_vnode->v_mount == mp) {
224 /* Get reference to vnode in case it's being free'd */
226 vgs = vget_prep(vp2);
227 mtx_unlock(&fdesc_hashmtx);
228 error = vget_finish(vp2, LK_EXCLUSIVE, vgs);
229 /* Someone beat us, dec use count and wait for reclaim */
232 /* If we didn't get it, return no vnode. */
240 /* If we came here, we can insert it safely. */
241 LIST_INSERT_HEAD(fc, fd, fd_hash);
242 mtx_unlock(&fdesc_hashmtx);
243 vn_set_state(vp, VSTATE_CONSTRUCTED);
248 struct fdesc_get_ino_args {
258 fdesc_get_ino_alloc(struct mount *mp, void *arg, int lkflags,
261 struct fdesc_get_ino_args *a;
262 struct fdescmount *fdm;
267 fdm = VFSTOFDESC(mp);
268 if ((fdm->flags & FMNT_NODUP) != 0 && a->fp->f_type == DTYPE_VNODE) {
270 vget(vp, lkflags | LK_RETRY);
274 error = fdesc_allocvp(a->ftype, a->fd_fd, a->ix, mp, rvp);
282 * vp is the current namei directory
283 * ndp is the name to locate in that directory...
286 fdesc_lookup(struct vop_lookup_args *ap)
288 struct vnode **vpp = ap->a_vpp;
289 struct vnode *dvp = ap->a_dvp;
290 struct componentname *cnp = ap->a_cnp;
291 char *pname = cnp->cn_nameptr;
292 struct thread *td = curthread;
294 struct fdesc_get_ino_args arg;
295 int nlen = cnp->cn_namelen;
300 if ((cnp->cn_flags & ISLASTCN) &&
301 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
306 if (cnp->cn_namelen == 1 && *pname == '.') {
312 if (VTOFDESC(dvp)->fd_type != Froot) {
318 /* the only time a leading 0 is acceptable is if it's "0" */
319 if (*pname == '0' && nlen != 1) {
324 if (*pname < '0' || *pname > '9') {
328 fd1 = 10 * fd + *pname++ - '0';
337 * No rights to check since 'fp' isn't actually used.
339 if ((error = fget(td, fd, &cap_no_rights, &fp)) != 0)
343 * Make sure we do not deadlock looking up the dvp itself.
345 * Unlock our root node (dvp) when doing this, since we might
346 * deadlock since the vnode might be locked by another thread
347 * and the root vnode lock will be obtained afterwards (in case
348 * we're looking up the fd of the root vnode), which will be the
349 * opposite lock order.
353 arg.ix = FD_DESC + fd;
356 arg.fdropped = false;
357 error = vn_vget_ino_gen(dvp, fdesc_get_ino_alloc, &arg,
362 * In case we're holding the last reference to the file, the dvp
363 * will be re-acquired.
368 vn_lock(dvp, LK_RETRY | LK_EXCLUSIVE);
370 if (error == 0 && VN_IS_DOOMED(dvp))
385 fdesc_open(struct vop_open_args *ap)
387 struct vnode *vp = ap->a_vp;
389 if (VTOFDESC(vp)->fd_type == Froot)
393 * XXX Kludge: set td->td_proc->p_dupfd to contain the value of the file
394 * descriptor being sought for duplication. The error return ensures
395 * that the vnode for this device will be released by vn_open. Open
396 * will detect this special error and take the actions in dupfdopen.
397 * Other callers of vn_open or VOP_OPEN will simply report the
400 ap->a_td->td_dupfd = VTOFDESC(vp)->fd_fd; /* XXX */
405 fdesc_pathconf(struct vop_pathconf_args *ap)
407 struct vnode *vp = ap->a_vp;
410 switch (ap->a_name) {
412 *ap->a_retval = NAME_MAX;
415 if (VTOFDESC(vp)->fd_type == Froot)
421 if (VTOFDESC(vp)->fd_type == Froot)
422 return (vop_stdpathconf(ap));
425 error = kern_fpathconf(curthread, VTOFDESC(vp)->fd_fd,
426 ap->a_name, ap->a_retval);
427 vn_lock(vp, LK_SHARED | LK_RETRY);
434 fdesc_getattr(struct vop_getattr_args *ap)
436 struct vnode *vp = ap->a_vp;
437 struct vattr *vap = ap->a_vap;
438 struct timeval boottime;
440 getboottime(&boottime);
441 vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
442 vap->va_fileid = VTOFDESC(vp)->fd_ix;
445 vap->va_blocksize = DEV_BSIZE;
446 vap->va_atime.tv_sec = boottime.tv_sec;
447 vap->va_atime.tv_nsec = 0;
448 vap->va_mtime = vap->va_atime;
449 vap->va_ctime = vap->va_mtime;
455 switch (VTOFDESC(vp)->fd_type) {
459 vap->va_size = DEV_BSIZE;
460 vap->va_rdev = NODEV;
464 vap->va_type = (VFSTOFDESC(vp->v_mount)->flags &
465 (FMNT_RDLNKF | FMNT_LINRDLNKF)) == 0 ? VCHR : VLNK;
468 vap->va_rdev = makedev(0, vap->va_fileid);
472 panic("fdesc_getattr");
476 vp->v_type = vap->va_type;
481 fdesc_setattr(struct vop_setattr_args *ap)
483 struct vattr *vap = ap->a_vap;
487 struct thread *td = curthread;
493 * Can't mess with the root vnode
495 if (VTOFDESC(ap->a_vp)->fd_type == Froot)
498 fd = VTOFDESC(ap->a_vp)->fd_fd;
501 * Allow setattr where there is an underlying vnode.
502 * For O_PATH descriptors, disallow truncate.
504 if (vap->va_size != VNOVAL) {
505 error = getvnode(td, fd,
506 cap_rights_init_one(&rights, CAP_EXTATTR_SET), &fp);
508 error = getvnode_path(td, fd,
509 cap_rights_init_one(&rights, CAP_EXTATTR_SET), &fp);
513 * getvnode() returns EINVAL if the file descriptor is not
514 * backed by a vnode. Silently drop all changes except
515 * chflags(2) in this case.
517 if (error == EINVAL) {
518 if (vap->va_flags != VNOVAL)
526 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) == 0) {
527 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
528 error = VOP_SETATTR(vp, ap->a_vap, ap->a_cred);
530 vn_finished_write(mp);
536 #define UIO_MX _GENERIC_DIRLEN(10) /* number of symbols in INT_MAX printout */
539 fdesc_readdir(struct vop_readdir_args *ap)
541 struct fdescmount *fmp;
542 struct uio *uio = ap->a_uio;
543 struct filedesc *fdp;
545 struct dirent *dp = &d;
546 int error, i, off, fcnt;
548 if (VTOFDESC(ap->a_vp)->fd_type != Froot)
549 panic("fdesc_readdir: not dir");
551 fmp = VFSTOFDESC(ap->a_vp->v_mount);
552 if (ap->a_ncookies != NULL)
555 off = (int)uio->uio_offset;
556 if (off != uio->uio_offset || off < 0 || (u_int)off % UIO_MX != 0 ||
557 uio->uio_resid < UIO_MX)
559 i = (u_int)off / UIO_MX;
560 fdp = uio->uio_td->td_proc->p_fd;
563 fcnt = i - 2; /* The first two nodes are `.' and `..' */
566 while (i < fdp->fd_nfiles + 2 && uio->uio_resid >= UIO_MX) {
567 bzero((caddr_t)dp, UIO_MX);
571 dp->d_fileno = i + FD_ROOT;
572 dp->d_namlen = i + 1;
573 dp->d_reclen = UIO_MX;
574 bcopy("..", dp->d_name, dp->d_namlen);
576 dirent_terminate(dp);
579 if (fdp->fd_ofiles[fcnt].fde_file == NULL)
581 dp->d_namlen = sprintf(dp->d_name, "%d", fcnt);
582 dp->d_reclen = UIO_MX;
583 dp->d_type = (fmp->flags & (FMNT_RDLNKF |
584 FMNT_LINRDLNKF)) == 0 ? DT_CHR : DT_LNK;
585 dp->d_fileno = i + FD_DESC;
586 dirent_terminate(dp);
589 /* NOTE: d_off is the offset of the *next* entry. */
590 dp->d_off = UIO_MX * (i + 1);
591 if (dp->d_namlen != 0) {
593 * And ship to userland
595 FILEDESC_SUNLOCK(fdp);
596 error = uiomove(dp, UIO_MX, uio);
604 FILEDESC_SUNLOCK(fdp);
607 uio->uio_offset = i * UIO_MX;
612 fdesc_reclaim(struct vop_reclaim_args *ap)
615 struct fdescnode *fd;
619 fdesc_remove_entry(fd);
620 free(vp->v_data, M_TEMP);
626 fdesc_readlink(struct vop_readlink_args *va)
628 struct vnode *vp, *vn;
632 char *freepath, *fullpath;
634 int lockflags, fd_fd;
639 if (VTOFDESC(vn)->fd_type != Fdesc)
640 panic("fdesc_readlink: not fdescfs link");
641 fd_fd = ((struct fdescnode *)vn->v_data)->fd_fd;
642 lockflags = VOP_ISLOCKED(vn);
646 error = fget_cap(td, fd_fd, &cap_no_rights, &fp, NULL);
650 switch (fp->f_type) {
653 error = vn_fullpath(vp, &fullpath, &freepath);
656 fullpath = "anon_inode:[unknown]";
661 pathlen = strlen(fullpath);
662 error = uiomove(fullpath, pathlen, uio);
664 if (freepath != NULL)
665 free(freepath, M_TEMP);
669 vn_lock(vn, lockflags | LK_RETRY);