fdesc_vfsops.c revision 1.44 1 /* $NetBSD: fdesc_vfsops.c,v 1.44 2003/08/07 16:32:33 agc 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. 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.
21 *
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
32 * SUCH DAMAGE.
33 *
34 * @(#)fdesc_vfsops.c 8.10 (Berkeley) 5/14/95
35 *
36 * #Id: fdesc_vfsops.c,v 1.9 1993/04/06 15:28:33 jsp Exp #
37 */
38
39 /*
40 * /dev/fd Filesystem
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: fdesc_vfsops.c,v 1.44 2003/08/07 16:32:33 agc Exp $");
45
46 #if defined(_KERNEL_OPT)
47 #include "opt_compat_netbsd.h"
48 #endif
49
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/time.h>
53 #include <sys/proc.h>
54 #include <sys/resourcevar.h>
55 #include <sys/filedesc.h>
56 #include <sys/vnode.h>
57 #include <sys/mount.h>
58 #include <sys/namei.h>
59 #include <sys/malloc.h>
60 #include <miscfs/fdesc/fdesc.h>
61
62 int fdesc_mount __P((struct mount *, const char *, void *,
63 struct nameidata *, struct proc *));
64 int fdesc_start __P((struct mount *, int, struct proc *));
65 int fdesc_unmount __P((struct mount *, int, struct proc *));
66 int fdesc_quotactl __P((struct mount *, int, uid_t, caddr_t,
67 struct proc *));
68 int fdesc_statfs __P((struct mount *, struct statfs *, struct proc *));
69 int fdesc_sync __P((struct mount *, int, struct ucred *, struct proc *));
70 int fdesc_vget __P((struct mount *, ino_t, struct vnode **));
71 int fdesc_fhtovp __P((struct mount *, struct fid *, struct vnode **));
72 int fdesc_checkexp __P((struct mount *, struct mbuf *, int *,
73 struct ucred **));
74 int fdesc_vptofh __P((struct vnode *, struct fid *));
75 int fdesc_sysctl __P((int *, u_int, void *, size_t *, void *, size_t,
76 struct proc *));
77
78 /*
79 * Mount the per-process file descriptors (/dev/fd)
80 */
81 int
82 fdesc_mount(mp, path, data, ndp, p)
83 struct mount *mp;
84 const char *path;
85 void *data;
86 struct nameidata *ndp;
87 struct proc *p;
88 {
89 int error = 0;
90 struct fdescmount *fmp;
91 struct vnode *rvp;
92
93 if (mp->mnt_flag & MNT_GETARGS)
94 return 0;
95 /*
96 * Update is a no-op
97 */
98 if (mp->mnt_flag & MNT_UPDATE)
99 return (EOPNOTSUPP);
100
101 error = fdesc_allocvp(Froot, FD_ROOT, mp, &rvp);
102 if (error)
103 return (error);
104
105 MALLOC(fmp, struct fdescmount *, sizeof(struct fdescmount),
106 M_UFSMNT, M_WAITOK); /* XXX */
107 rvp->v_type = VDIR;
108 rvp->v_flag |= VROOT;
109 fmp->f_root = rvp;
110 mp->mnt_flag |= MNT_LOCAL;
111 mp->mnt_data = fmp;
112 vfs_getnewfsid(mp);
113
114 error = set_statfs_info(path, UIO_USERSPACE, "fdesc", UIO_SYSSPACE,
115 mp, p);
116 VOP_UNLOCK(rvp, 0);
117 return error;
118 }
119
120 int
121 fdesc_start(mp, flags, p)
122 struct mount *mp;
123 int flags;
124 struct proc *p;
125 {
126 return (0);
127 }
128
129 int
130 fdesc_unmount(mp, mntflags, p)
131 struct mount *mp;
132 int mntflags;
133 struct proc *p;
134 {
135 int error;
136 int flags = 0;
137 struct vnode *rootvp = VFSTOFDESC(mp)->f_root;
138
139 if (mntflags & MNT_FORCE)
140 flags |= FORCECLOSE;
141
142 /*
143 * Clear out buffer cache. I don't think we
144 * ever get anything cached at this level at the
145 * moment, but who knows...
146 */
147 if (rootvp->v_usecount > 1)
148 return (EBUSY);
149 if ((error = vflush(mp, rootvp, flags)) != 0)
150 return (error);
151
152 /*
153 * Release reference on underlying root vnode
154 */
155 vrele(rootvp);
156 /*
157 * And blow it away for future re-use
158 */
159 vgone(rootvp);
160 /*
161 * Finally, throw away the fdescmount structure
162 */
163 free(mp->mnt_data, M_UFSMNT); /* XXX */
164 mp->mnt_data = 0;
165
166 return (0);
167 }
168
169 int
170 fdesc_root(mp, vpp)
171 struct mount *mp;
172 struct vnode **vpp;
173 {
174 struct vnode *vp;
175
176 /*
177 * Return locked reference to root.
178 */
179 vp = VFSTOFDESC(mp)->f_root;
180 VREF(vp);
181 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
182 *vpp = vp;
183 return (0);
184 }
185
186 int
187 fdesc_quotactl(mp, cmd, uid, arg, p)
188 struct mount *mp;
189 int cmd;
190 uid_t uid;
191 caddr_t arg;
192 struct proc *p;
193 {
194
195 return (EOPNOTSUPP);
196 }
197
198 int
199 fdesc_statfs(mp, sbp, p)
200 struct mount *mp;
201 struct statfs *sbp;
202 struct proc *p;
203 {
204 struct filedesc *fdp;
205 int lim;
206 int i;
207 int last;
208 int freefd;
209
210 /*
211 * Compute number of free file descriptors.
212 * [ Strange results will ensue if the open file
213 * limit is ever reduced below the current number
214 * of open files... ]
215 */
216 lim = p->p_rlimit[RLIMIT_NOFILE].rlim_cur;
217 fdp = p->p_fd;
218 last = min(fdp->fd_nfiles, lim);
219 freefd = 0;
220 for (i = fdp->fd_freefile; i < last; i++)
221 if (fdp->fd_ofiles[i] == NULL)
222 freefd++;
223
224 /*
225 * Adjust for the fact that the fdesc array may not
226 * have been fully allocated yet.
227 */
228 if (fdp->fd_nfiles < lim)
229 freefd += (lim - fdp->fd_nfiles);
230
231 sbp->f_bsize = DEV_BSIZE;
232 sbp->f_iosize = DEV_BSIZE;
233 sbp->f_blocks = 2; /* 1K to keep df happy */
234 sbp->f_bfree = 0;
235 sbp->f_bavail = 0;
236 sbp->f_files = lim + 1; /* Allow for "." */
237 sbp->f_ffree = freefd; /* See comments above */
238 #ifdef COMPAT_09
239 sbp->f_type = 6;
240 #else
241 sbp->f_type = 0;
242 #endif
243 copy_statfs_info(sbp, mp);
244 return (0);
245 }
246
247 /*ARGSUSED*/
248 int
249 fdesc_sync(mp, waitfor, uc, p)
250 struct mount *mp;
251 int waitfor;
252 struct ucred *uc;
253 struct proc *p;
254 {
255
256 return (0);
257 }
258
259 /*
260 * Fdesc flat namespace lookup.
261 * Currently unsupported.
262 */
263 int
264 fdesc_vget(mp, ino, vpp)
265 struct mount *mp;
266 ino_t ino;
267 struct vnode **vpp;
268 {
269
270 return (EOPNOTSUPP);
271 }
272
273
274 /*ARGSUSED*/
275 int
276 fdesc_fhtovp(mp, fhp, vpp)
277 struct mount *mp;
278 struct fid *fhp;
279 struct vnode **vpp;
280 {
281
282 return (EOPNOTSUPP);
283 }
284
285 /*ARGSUSED*/
286 int
287 fdesc_checkexp(mp, nam, exflagsp, credanonp)
288 struct mount *mp;
289 struct mbuf *nam;
290 int *exflagsp;
291 struct ucred **credanonp;
292 {
293
294 return (EOPNOTSUPP);
295 }
296
297 /*ARGSUSED*/
298 int
299 fdesc_vptofh(vp, fhp)
300 struct vnode *vp;
301 struct fid *fhp;
302 {
303 return (EOPNOTSUPP);
304 }
305
306 int
307 fdesc_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
308 int *name;
309 u_int namelen;
310 void *oldp;
311 size_t *oldlenp;
312 void *newp;
313 size_t newlen;
314 struct proc *p;
315 {
316 return (EOPNOTSUPP);
317 }
318
319 extern const struct vnodeopv_desc fdesc_vnodeop_opv_desc;
320
321 const struct vnodeopv_desc * const fdesc_vnodeopv_descs[] = {
322 &fdesc_vnodeop_opv_desc,
323 NULL,
324 };
325
326 struct vfsops fdesc_vfsops = {
327 MOUNT_FDESC,
328 fdesc_mount,
329 fdesc_start,
330 fdesc_unmount,
331 fdesc_root,
332 fdesc_quotactl,
333 fdesc_statfs,
334 fdesc_sync,
335 fdesc_vget,
336 fdesc_fhtovp,
337 fdesc_vptofh,
338 fdesc_init,
339 NULL,
340 fdesc_done,
341 fdesc_sysctl,
342 NULL, /* vfs_mountroot */
343 fdesc_checkexp,
344 fdesc_vnodeopv_descs,
345 };
346