2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1992, 1993, 1995
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_vfsops.c 8.4 (Berkeley) 1/21/94
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/filedesc.h>
46 #include <sys/kernel.h>
49 #include <sys/mutex.h>
50 #include <sys/malloc.h>
51 #include <sys/mount.h>
53 #include <sys/racct.h>
54 #include <sys/resourcevar.h>
55 #include <sys/vnode.h>
57 #include <fs/fdescfs/fdesc.h>
59 static MALLOC_DEFINE(M_FDESCMNT, "fdesc_mount", "FDESC mount structure");
61 static vfs_cmount_t fdesc_cmount;
62 static vfs_mount_t fdesc_mount;
63 static vfs_unmount_t fdesc_unmount;
64 static vfs_statfs_t fdesc_statfs;
65 static vfs_root_t fdesc_root;
68 * Compatibility shim for old mount(2) system call.
71 fdesc_cmount(struct mntarg *ma, void *data, uint64_t flags)
74 return kernel_mount(ma, flags);
78 * Mount the per-process file descriptors (/dev/fd)
81 fdesc_mount(struct mount *mp)
83 struct fdescmount *fmp;
90 if (mp->mnt_flag & (MNT_UPDATE | MNT_ROOTFS))
93 fmp = malloc(sizeof(struct fdescmount), M_FDESCMNT, M_WAITOK);
96 * We need to initialize a few bits of our local mount point struct to
97 * avoid confusion in allocvp.
101 if (vfs_getopt(mp->mnt_optnew, "linrdlnk", NULL, NULL) == 0)
102 fmp->flags |= FMNT_LINRDLNKF;
103 if (vfs_getopt(mp->mnt_optnew, "nodup", NULL, NULL) == 0)
104 fmp->flags |= FMNT_NODUP;
105 error = fdesc_allocvp(Froot, -1, FD_ROOT, mp, &rvp);
107 free(fmp, M_FDESCMNT);
113 rvp->v_vflag |= VV_ROOT;
118 /* XXX -- don't mark as local to work around fts() problems */
119 /*mp->mnt_flag |= MNT_LOCAL;*/
121 * Enable shared locking so that there is no contention on the root
122 * vnode. Note only root vnode enables shared locking for itself,
123 * so this end up being a nop for the rest.
125 mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED;
130 vfs_mountedfrom(mp, "fdescfs");
135 fdesc_unmount(struct mount *mp, int mntflags)
137 struct fdescmount *fmp;
142 if (mntflags & MNT_FORCE) {
143 /* The hash mutex protects the private mount flags. */
144 mtx_lock(&fdesc_hashmtx);
145 fmp->flags |= FMNT_UNMOUNTF;
146 mtx_unlock(&fdesc_hashmtx);
151 * Clear out buffer cache. I don't think we
152 * ever get anything cached at this level at the
153 * moment, but who knows...
155 * There is 1 extra root vnode reference corresponding
158 if ((error = vflush(mp, 1, flags, curthread)) != 0)
162 * Finally, throw away the fdescmount structure.
165 free(fmp, M_FDESCMNT);
170 fdesc_root(struct mount *mp, int flags, struct vnode **vpp)
175 * Return locked reference to root.
177 vp = VFSTOFDESC(mp)->f_root;
178 vget(vp, flags | LK_RETRY);
184 fdesc_statfs(struct mount *mp, struct statfs *sbp)
187 struct filedesc *fdp;
197 * Compute number of free file descriptors.
198 * [ Strange results will ensue if the open file
199 * limit is ever reduced below the current number
202 lim = lim_cur(td, RLIMIT_NOFILE);
203 fdp = td->td_proc->p_fd;
205 limit = racct_get_limit(td->td_proc, RACCT_NOFILE);
208 last = min(fdp->fd_nfiles, lim);
210 for (i = fdp->fd_freefile; i < last; i++)
211 if (fdp->fd_ofiles[i].fde_file == NULL)
215 * Adjust for the fact that the fdesc array may not
216 * have been fully allocated yet.
218 if (fdp->fd_nfiles < lim)
219 freefd += (lim - fdp->fd_nfiles);
220 FILEDESC_SUNLOCK(fdp);
223 sbp->f_bsize = DEV_BSIZE;
224 sbp->f_iosize = DEV_BSIZE;
225 sbp->f_blocks = 2; /* 1K to keep df happy */
228 sbp->f_files = lim + 1; /* Allow for "." */
229 sbp->f_ffree = freefd; /* See comments above */
233 static struct vfsops fdesc_vfsops = {
234 .vfs_cmount = fdesc_cmount,
235 .vfs_init = fdesc_init,
236 .vfs_mount = fdesc_mount,
237 .vfs_root = fdesc_root,
238 .vfs_statfs = fdesc_statfs,
239 .vfs_uninit = fdesc_uninit,
240 .vfs_unmount = fdesc_unmount,
243 VFS_SET(fdesc_vfsops, fdescfs, VFCF_SYNTHETIC | VFCF_JAIL);