1 /*-
2 * Copyright (c) 1992, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software donated to Berkeley by
6 * Jan-Simon Pendry.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
19 *
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
30 * SUCH DAMAGE.
31 *
32 * @(#)fdesc_vfsops.c 8.4 (Berkeley) 1/21/94
33 *
34 * $FreeBSD: releng/9.0/sys/fs/fdescfs/fdesc_vfsops.c 220400 2011-04-06 19:13:04Z trasz $
35 */
36
37 /*
38 * /dev/fd Filesystem
39 */
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/filedesc.h>
44 #include <sys/kernel.h>
45 #include <sys/lock.h>
46 #include <sys/mutex.h>
47 #include <sys/malloc.h>
48 #include <sys/mount.h>
49 #include <sys/proc.h>
50 #include <sys/racct.h>
51 #include <sys/resourcevar.h>
52 #include <sys/vnode.h>
53
54 #include <fs/fdescfs/fdesc.h>
55
56 static MALLOC_DEFINE(M_FDESCMNT, "fdesc_mount", "FDESC mount structure");
57
58 static vfs_cmount_t fdesc_cmount;
59 static vfs_mount_t fdesc_mount;
60 static vfs_unmount_t fdesc_unmount;
61 static vfs_statfs_t fdesc_statfs;
62 static vfs_root_t fdesc_root;
63
64 /*
65 * Compatibility shim for old mount(2) system call.
66 */
67 int
68 fdesc_cmount(struct mntarg *ma, void *data, int flags)
69 {
70 return kernel_mount(ma, flags);
71 }
72
73 /*
74 * Mount the per-process file descriptors (/dev/fd)
75 */
76 static int
77 fdesc_mount(struct mount *mp)
78 {
79 int error = 0;
80 struct fdescmount *fmp;
81 struct vnode *rvp;
82
83 /*
84 * Update is a no-op
85 */
86 if (mp->mnt_flag & (MNT_UPDATE | MNT_ROOTFS))
87 return (EOPNOTSUPP);
88
89 fmp = malloc(sizeof(struct fdescmount),
90 M_FDESCMNT, M_WAITOK); /* XXX */
91
92 /*
93 * We need to initialize a few bits of our local mount point struct to
94 * avoid confusion in allocvp.
95 */
96 mp->mnt_data = (qaddr_t) fmp;
97 fmp->flags = 0;
98 error = fdesc_allocvp(Froot, -1, FD_ROOT, mp, &rvp);
99 if (error) {
100 free(fmp, M_FDESCMNT);
101 mp->mnt_data = 0;
102 return (error);
103 }
104 rvp->v_type = VDIR;
105 rvp->v_vflag |= VV_ROOT;
106 fmp->f_root = rvp;
107 VOP_UNLOCK(rvp, 0);
108 /* XXX -- don't mark as local to work around fts() problems */
109 /*mp->mnt_flag |= MNT_LOCAL;*/
110 MNT_ILOCK(mp);
111 mp->mnt_kern_flag |= MNTK_MPSAFE;
112 MNT_IUNLOCK(mp);
113 vfs_getnewfsid(mp);
114
115 vfs_mountedfrom(mp, "fdescfs");
116 return (0);
117 }
118
119 static int
120 fdesc_unmount(mp, mntflags)
121 struct mount *mp;
122 int mntflags;
123 {
124 struct fdescmount *fmp;
125 caddr_t data;
126 int error;
127 int flags = 0;
128
129 fmp = (struct fdescmount *)mp->mnt_data;
130 if (mntflags & MNT_FORCE) {
131 /* The hash mutex protects the private mount flags. */
132 mtx_lock(&fdesc_hashmtx);
133 fmp->flags |= FMNT_UNMOUNTF;
134 mtx_unlock(&fdesc_hashmtx);
135 flags |= FORCECLOSE;
136 }
137
138 /*
139 * Clear out buffer cache. I don't think we
140 * ever get anything cached at this level at the
141 * moment, but who knows...
142 *
143 * There is 1 extra root vnode reference corresponding
144 * to f_root.
145 */
146 if ((error = vflush(mp, 1, flags, curthread)) != 0)
147 return (error);
148
149 /*
150 * Finally, throw away the fdescmount structure. Hold the hashmtx to
151 * protect the fdescmount structure.
152 */
153 mtx_lock(&fdesc_hashmtx);
154 data = mp->mnt_data;
155 mp->mnt_data = 0;
156 mtx_unlock(&fdesc_hashmtx);
157 free(data, M_FDESCMNT); /* XXX */
158
159 return (0);
160 }
161
162 static int
163 fdesc_root(mp, flags, vpp)
164 struct mount *mp;
165 int flags;
166 struct vnode **vpp;
167 {
168 struct vnode *vp;
169
170 /*
171 * Return locked reference to root.
172 */
173 vp = VFSTOFDESC(mp)->f_root;
174 vget(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
175 *vpp = vp;
176 return (0);
177 }
178
179 static int
180 fdesc_statfs(mp, sbp)
181 struct mount *mp;
182 struct statfs *sbp;
183 {
184 struct thread *td;
185 struct filedesc *fdp;
186 int lim;
187 int i;
188 int last;
189 int freefd;
190 uint64_t limit;
191
192 td = curthread;
193
194 /*
195 * Compute number of free file descriptors.
196 * [ Strange results will ensue if the open file
197 * limit is ever reduced below the current number
198 * of open files... ]
199 */
200 PROC_LOCK(td->td_proc);
201 lim = lim_cur(td->td_proc, RLIMIT_NOFILE);
202 PROC_UNLOCK(td->td_proc);
203 fdp = td->td_proc->p_fd;
204 FILEDESC_SLOCK(fdp);
205 limit = racct_get_limit(td->td_proc, RACCT_NOFILE);
206 if (lim > limit)
207 lim = limit;
208 last = min(fdp->fd_nfiles, lim);
209 freefd = 0;
210 for (i = fdp->fd_freefile; i < last; i++)
211 if (fdp->fd_ofiles[i] == NULL)
212 freefd++;
213
214 /*
215 * Adjust for the fact that the fdesc array may not
216 * have been fully allocated yet.
217 */
218 if (fdp->fd_nfiles < lim)
219 freefd += (lim - fdp->fd_nfiles);
220 FILEDESC_SUNLOCK(fdp);
221
222 sbp->f_flags = 0;
223 sbp->f_bsize = DEV_BSIZE;
224 sbp->f_iosize = DEV_BSIZE;
225 sbp->f_blocks = 2; /* 1K to keep df happy */
226 sbp->f_bfree = 0;
227 sbp->f_bavail = 0;
228 sbp->f_files = lim + 1; /* Allow for "." */
229 sbp->f_ffree = freefd; /* See comments above */
230 return (0);
231 }
232
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,
241 };
242
243 VFS_SET(fdesc_vfsops, fdescfs, VFCF_SYNTHETIC);
Cache object: 858478388a0048e895d475d25f41f8bb
|