fdesc_vfsops.c revision 1.40 1 /* $NetBSD: fdesc_vfsops.c,v 1.40 2003/06/28 14:22:01 darrenr Exp $ */
2
3 /*
4 * Copyright (c) 1992, 1993, 1995
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software donated to Berkeley by
8 * Jan-Simon Pendry.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)fdesc_vfsops.c 8.10 (Berkeley) 5/14/95
39 *
40 * #Id: fdesc_vfsops.c,v 1.9 1993/04/06 15:28:33 jsp Exp #
41 */
42
43 /*
44 * /dev/fd Filesystem
45 */
46
47 #include <sys/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: fdesc_vfsops.c,v 1.40 2003/06/28 14:22:01 darrenr Exp $");
49
50 #if defined(_KERNEL_OPT)
51 #include "opt_compat_netbsd.h"
52 #endif
53
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/time.h>
57 #include <sys/proc.h>
58 #include <sys/resourcevar.h>
59 #include <sys/filedesc.h>
60 #include <sys/vnode.h>
61 #include <sys/mount.h>
62 #include <sys/namei.h>
63 #include <sys/malloc.h>
64 #include <miscfs/fdesc/fdesc.h>
65
66 int fdesc_mount __P((struct mount *, const char *, void *,
67 struct nameidata *, struct lwp *));
68 int fdesc_start __P((struct mount *, int, struct lwp *));
69 int fdesc_unmount __P((struct mount *, int, struct lwp *));
70 int fdesc_quotactl __P((struct mount *, int, uid_t, caddr_t,
71 struct lwp *));
72 int fdesc_statfs __P((struct mount *, struct statfs *, struct lwp *));
73 int fdesc_sync __P((struct mount *, int, struct ucred *, struct lwp *));
74 int fdesc_vget __P((struct mount *, ino_t, struct vnode **, struct lwp *));
75 int fdesc_fhtovp __P((struct mount *, struct fid *, struct vnode **,
76 struct lwp *));
77 int fdesc_checkexp __P((struct mount *, struct mbuf *, int *,
78 struct ucred **));
79 int fdesc_vptofh __P((struct vnode *, struct fid *));
80 int fdesc_sysctl __P((int *, u_int, void *, size_t *, void *, size_t,
81 struct lwp *));
82
83 /*
84 * Mount the per-process file descriptors (/dev/fd)
85 */
86 int
87 fdesc_mount(mp, path, data, ndp, l)
88 struct mount *mp;
89 const char *path;
90 void *data;
91 struct nameidata *ndp;
92 struct lwp *l;
93 {
94 int error = 0;
95 struct fdescmount *fmp;
96 struct vnode *rvp;
97
98 if (mp->mnt_flag & MNT_GETARGS)
99 return 0;
100 /*
101 * Update is a no-op
102 */
103 if (mp->mnt_flag & MNT_UPDATE)
104 return (EOPNOTSUPP);
105
106 error = fdesc_allocvp(Froot, FD_ROOT, mp, &rvp);
107 if (error)
108 return (error);
109
110 MALLOC(fmp, struct fdescmount *, sizeof(struct fdescmount),
111 M_UFSMNT, M_WAITOK); /* XXX */
112 rvp->v_type = VDIR;
113 rvp->v_flag |= VROOT;
114 fmp->f_root = rvp;
115 mp->mnt_flag |= MNT_LOCAL;
116 mp->mnt_data = fmp;
117 vfs_getnewfsid(mp);
118
119 error = set_statfs_info(path, UIO_USERSPACE, "fdesc", UIO_SYSSPACE,
120 mp, p);
121 VOP_UNLOCK(rvp, 0);
122 return error;
123 }
124
125 int
126 fdesc_start(mp, flags, l)
127 struct mount *mp;
128 int flags;
129 struct lwp *l;
130 {
131 return (0);
132 }
133
134 int
135 fdesc_unmount(mp, mntflags, l)
136 struct mount *mp;
137 int mntflags;
138 struct lwp *l;
139 {
140 int error;
141 int flags = 0;
142 struct vnode *rootvp = VFSTOFDESC(mp)->f_root;
143
144 if (mntflags & MNT_FORCE)
145 flags |= FORCECLOSE;
146
147 /*
148 * Clear out buffer cache. I don't think we
149 * ever get anything cached at this level at the
150 * moment, but who knows...
151 */
152 if (rootvp->v_usecount > 1)
153 return (EBUSY);
154 if ((error = vflush(mp, rootvp, flags)) != 0)
155 return (error);
156
157 /*
158 * Release reference on underlying root vnode
159 */
160 vrele(rootvp);
161 /*
162 * And blow it away for future re-use
163 */
164 vgone(rootvp);
165 /*
166 * Finally, throw away the fdescmount structure
167 */
168 free(mp->mnt_data, M_UFSMNT); /* XXX */
169 mp->mnt_data = 0;
170
171 return (0);
172 }
173
174 int
175 fdesc_root(mp, vpp, l)
176 struct mount *mp;
177 struct vnode **vpp;
178 struct lwp *l;
179 {
180 struct vnode *vp;
181
182 /*
183 * Return locked reference to root.
184 */
185 vp = VFSTOFDESC(mp)->f_root;
186 VREF(vp);
187 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
188 *vpp = vp;
189 return (0);
190 }
191
192 int
193 fdesc_quotactl(mp, cmd, uid, arg, l)
194 struct mount *mp;
195 int cmd;
196 uid_t uid;
197 caddr_t arg;
198 struct lwp *l;
199 {
200
201 return (EOPNOTSUPP);
202 }
203
204 int
205 fdesc_statfs(mp, sbp, l)
206 struct mount *mp;
207 struct statfs *sbp;
208 struct lwp *l;
209 {
210 struct filedesc *fdp;
211 struct proc *p;
212 int lim;
213 int i;
214 int last;
215 int freefd;
216
217 /*
218 * Compute number of free file descriptors.
219 * [ Strange results will ensue if the open file
220 * limit is ever reduced below the current number
221 * of open files... ]
222 */
223 p = l->l_proc;
224 lim = p->p_rlimit[RLIMIT_NOFILE].rlim_cur;
225 fdp = p->p_fd;
226 last = min(fdp->fd_nfiles, lim);
227 freefd = 0;
228 for (i = fdp->fd_freefile; i < last; i++)
229 if (fdp->fd_ofiles[i] == NULL)
230 freefd++;
231
232 /*
233 * Adjust for the fact that the fdesc array may not
234 * have been fully allocated yet.
235 */
236 if (fdp->fd_nfiles < lim)
237 freefd += (lim - fdp->fd_nfiles);
238
239 sbp->f_bsize = DEV_BSIZE;
240 sbp->f_iosize = DEV_BSIZE;
241 sbp->f_blocks = 2; /* 1K to keep df happy */
242 sbp->f_bfree = 0;
243 sbp->f_bavail = 0;
244 sbp->f_files = lim + 1; /* Allow for "." */
245 sbp->f_ffree = freefd; /* See comments above */
246 #ifdef COMPAT_09
247 sbp->f_type = 6;
248 #else
249 sbp->f_type = 0;
250 #endif
251 copy_statfs_info(sbp, mp);
252 return (0);
253 }
254
255 /*ARGSUSED*/
256 int
257 fdesc_sync(mp, waitfor, uc, l)
258 struct mount *mp;
259 int waitfor;
260 struct ucred *uc;
261 struct lwp *l;
262 {
263
264 return (0);
265 }
266
267 /*
268 * Fdesc flat namespace lookup.
269 * Currently unsupported.
270 */
271 int
272 fdesc_vget(mp, ino, vpp, l)
273 struct mount *mp;
274 ino_t ino;
275 struct vnode **vpp;
276 struct lwp *l;
277 {
278
279 return (EOPNOTSUPP);
280 }
281
282
283 /*ARGSUSED*/
284 int
285 fdesc_fhtovp(mp, fhp, vpp, l)
286 struct mount *mp;
287 struct fid *fhp;
288 struct vnode **vpp;
289 struct lwp *l;
290 {
291
292 return (EOPNOTSUPP);
293 }
294
295 /*ARGSUSED*/
296 int
297 fdesc_checkexp(mp, nam, exflagsp, credanonp)
298 struct mount *mp;
299 struct mbuf *nam;
300 int *exflagsp;
301 struct ucred **credanonp;
302 {
303
304 return (EOPNOTSUPP);
305 }
306
307 /*ARGSUSED*/
308 int
309 fdesc_vptofh(vp, fhp)
310 struct vnode *vp;
311 struct fid *fhp;
312 {
313 return (EOPNOTSUPP);
314 }
315
316 int
317 fdesc_sysctl(name, namelen, oldp, oldlenp, newp, newlen, l)
318 int *name;
319 u_int namelen;
320 void *oldp;
321 size_t *oldlenp;
322 void *newp;
323 size_t newlen;
324 struct lwp *l;
325 {
326 return (EOPNOTSUPP);
327 }
328
329 extern const struct vnodeopv_desc fdesc_vnodeop_opv_desc;
330
331 const struct vnodeopv_desc * const fdesc_vnodeopv_descs[] = {
332 &fdesc_vnodeop_opv_desc,
333 NULL,
334 };
335
336 struct vfsops fdesc_vfsops = {
337 MOUNT_FDESC,
338 fdesc_mount,
339 fdesc_start,
340 fdesc_unmount,
341 fdesc_root,
342 fdesc_quotactl,
343 fdesc_statfs,
344 fdesc_sync,
345 fdesc_vget,
346 fdesc_fhtovp,
347 fdesc_vptofh,
348 fdesc_init,
349 NULL,
350 fdesc_done,
351 fdesc_sysctl,
352 NULL, /* vfs_mountroot */
353 fdesc_checkexp,
354 fdesc_vnodeopv_descs,
355 };
356