2 * Copyright (c) 1992, 1993, 1995
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 * 4. 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_vfsops.c 8.4 (Berkeley) 1/21/94
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/filedesc.h>
44 #include <sys/kernel.h>
47 #include <sys/mutex.h>
48 #include <sys/malloc.h>
49 #include <sys/mount.h>
51 #include <sys/racct.h>
52 #include <sys/resourcevar.h>
53 #include <sys/vnode.h>
55 #include <fs/fdescfs/fdesc.h>
57 static MALLOC_DEFINE(M_FDESCMNT, "fdesc_mount", "FDESC mount structure");
59 static vfs_cmount_t fdesc_cmount;
60 static vfs_mount_t fdesc_mount;
61 static vfs_unmount_t fdesc_unmount;
62 static vfs_statfs_t fdesc_statfs;
63 static vfs_root_t fdesc_root;
66 * Compatibility shim for old mount(2) system call.
69 fdesc_cmount(struct mntarg *ma, void *data, uint64_t flags)
71 return kernel_mount(ma, flags);
75 * Mount the per-process file descriptors (/dev/fd)
78 fdesc_mount(struct mount *mp)
81 struct fdescmount *fmp;
82 struct thread *td = curthread;
85 if (!prison_allow(td->td_ucred, PR_ALLOW_MOUNT_FDESCFS))
91 if (mp->mnt_flag & (MNT_UPDATE | MNT_ROOTFS))
94 fmp = malloc(sizeof(struct fdescmount),
95 M_FDESCMNT, M_WAITOK); /* XXX */
98 * We need to initialize a few bits of our local mount point struct to
99 * avoid confusion in allocvp.
101 mp->mnt_data = (qaddr_t) fmp;
103 error = fdesc_allocvp(Froot, -1, FD_ROOT, mp, &rvp);
105 free(fmp, M_FDESCMNT);
110 rvp->v_vflag |= VV_ROOT;
113 /* XXX -- don't mark as local to work around fts() problems */
114 /*mp->mnt_flag |= MNT_LOCAL;*/
117 vfs_mountedfrom(mp, "fdescfs");
122 fdesc_unmount(mp, mntflags)
126 struct fdescmount *fmp;
131 fmp = (struct fdescmount *)mp->mnt_data;
132 if (mntflags & MNT_FORCE) {
133 /* The hash mutex protects the private mount flags. */
134 mtx_lock(&fdesc_hashmtx);
135 fmp->flags |= FMNT_UNMOUNTF;
136 mtx_unlock(&fdesc_hashmtx);
141 * Clear out buffer cache. I don't think we
142 * ever get anything cached at this level at the
143 * moment, but who knows...
145 * There is 1 extra root vnode reference corresponding
148 if ((error = vflush(mp, 1, flags, curthread)) != 0)
152 * Finally, throw away the fdescmount structure. Hold the hashmtx to
153 * protect the fdescmount structure.
155 mtx_lock(&fdesc_hashmtx);
158 mtx_unlock(&fdesc_hashmtx);
159 free(data, M_FDESCMNT); /* XXX */
165 fdesc_root(mp, flags, vpp)
173 * Return locked reference to root.
175 vp = VFSTOFDESC(mp)->f_root;
176 vget(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
182 fdesc_statfs(mp, 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 PROC_LOCK(td->td_proc);
203 lim = lim_cur(td->td_proc, RLIMIT_NOFILE);
204 PROC_UNLOCK(td->td_proc);
205 fdp = td->td_proc->p_fd;
207 limit = racct_get_limit(td->td_proc, RACCT_NOFILE);
210 last = min(fdp->fd_nfiles, lim);
212 for (i = fdp->fd_freefile; i < last; i++)
213 if (fdp->fd_ofiles[i].fde_file == NULL)
217 * Adjust for the fact that the fdesc array may not
218 * have been fully allocated yet.
220 if (fdp->fd_nfiles < lim)
221 freefd += (lim - fdp->fd_nfiles);
222 FILEDESC_SUNLOCK(fdp);
225 sbp->f_bsize = DEV_BSIZE;
226 sbp->f_iosize = DEV_BSIZE;
227 sbp->f_blocks = 2; /* 1K to keep df happy */
230 sbp->f_files = lim + 1; /* Allow for "." */
231 sbp->f_ffree = freefd; /* See comments above */
235 static struct vfsops fdesc_vfsops = {
236 .vfs_cmount = fdesc_cmount,
237 .vfs_init = fdesc_init,
238 .vfs_mount = fdesc_mount,
239 .vfs_root = fdesc_root,
240 .vfs_statfs = fdesc_statfs,
241 .vfs_uninit = fdesc_uninit,
242 .vfs_unmount = fdesc_unmount,
245 VFS_SET(fdesc_vfsops, fdescfs, VFCF_SYNTHETIC | VFCF_JAIL);