fdesc_vfsops.c revision 1.43.2.7 1 /* $NetBSD: fdesc_vfsops.c,v 1.43.2.7 2005/01/17 19:32:38 skrll 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.43.2.7 2005/01/17 19:32:38 skrll 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/sysctl.h>
53 #include <sys/time.h>
54 #include <sys/proc.h>
55 #include <sys/resourcevar.h>
56 #include <sys/filedesc.h>
57 #include <sys/vnode.h>
58 #include <sys/mount.h>
59 #include <sys/dirent.h>
60 #include <sys/namei.h>
61 #include <sys/malloc.h>
62 #include <miscfs/fdesc/fdesc.h>
63
64 int fdesc_mount __P((struct mount *, const char *, void *,
65 struct nameidata *, struct lwp *));
66 int fdesc_start __P((struct mount *, int, struct lwp *));
67 int fdesc_unmount __P((struct mount *, int, struct lwp *));
68 int fdesc_quotactl __P((struct mount *, int, uid_t, void *,
69 struct lwp *));
70 int fdesc_statvfs __P((struct mount *, struct statvfs *, struct lwp *));
71 int fdesc_sync __P((struct mount *, int, struct ucred *, struct lwp *));
72 int fdesc_vget __P((struct mount *, ino_t, struct vnode **));
73 int fdesc_fhtovp __P((struct mount *, struct fid *, struct vnode **));
74 int fdesc_checkexp __P((struct mount *, struct mbuf *, int *,
75 struct ucred **));
76 int fdesc_vptofh __P((struct vnode *, struct fid *));
77
78 /*
79 * Mount the per-process file descriptors (/dev/fd)
80 */
81 int
82 fdesc_mount(mp, path, data, ndp, l)
83 struct mount *mp;
84 const char *path;
85 void *data;
86 struct nameidata *ndp;
87 struct lwp *l;
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_stat.f_namemax = MAXNAMLEN;
111 mp->mnt_flag |= MNT_LOCAL;
112 mp->mnt_data = fmp;
113 vfs_getnewfsid(mp);
114
115 error = set_statvfs_info(path, UIO_USERSPACE, "fdesc", UIO_SYSSPACE,
116 mp, l);
117 VOP_UNLOCK(rvp, 0);
118 return error;
119 }
120
121 int
122 fdesc_start(mp, flags, l)
123 struct mount *mp;
124 int flags;
125 struct lwp *l;
126 {
127 return (0);
128 }
129
130 int
131 fdesc_unmount(mp, mntflags, l)
132 struct mount *mp;
133 int mntflags;
134 struct lwp *l;
135 {
136 int error;
137 int flags = 0;
138 struct vnode *rootvp = VFSTOFDESC(mp)->f_root;
139
140 if (mntflags & MNT_FORCE)
141 flags |= FORCECLOSE;
142
143 /*
144 * Clear out buffer cache. I don't think we
145 * ever get anything cached at this level at the
146 * moment, but who knows...
147 */
148 if (rootvp->v_usecount > 1)
149 return (EBUSY);
150 if ((error = vflush(mp, rootvp, flags)) != 0)
151 return (error);
152
153 /*
154 * Release reference on underlying root vnode
155 */
156 vrele(rootvp);
157 /*
158 * And blow it away for future re-use
159 */
160 vgone(rootvp);
161 /*
162 * Finally, throw away the fdescmount structure
163 */
164 free(mp->mnt_data, M_UFSMNT); /* XXX */
165 mp->mnt_data = 0;
166
167 return (0);
168 }
169
170 int
171 fdesc_root(mp, vpp)
172 struct mount *mp;
173 struct vnode **vpp;
174 {
175 struct vnode *vp;
176
177 /*
178 * Return locked reference to root.
179 */
180 vp = VFSTOFDESC(mp)->f_root;
181 VREF(vp);
182 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
183 *vpp = vp;
184 return (0);
185 }
186
187 int
188 fdesc_quotactl(mp, cmd, uid, arg, l)
189 struct mount *mp;
190 int cmd;
191 uid_t uid;
192 void *arg;
193 struct lwp *l;
194 {
195
196 return (EOPNOTSUPP);
197 }
198
199 int
200 fdesc_statvfs(mp, sbp, l)
201 struct mount *mp;
202 struct statvfs *sbp;
203 struct lwp *l;
204 {
205 struct filedesc *fdp;
206 struct proc *p;
207 int lim;
208 int i;
209 int last;
210 int freefd;
211
212 /*
213 * Compute number of free file descriptors.
214 * [ Strange results will ensue if the open file
215 * limit is ever reduced below the current number
216 * of open files... ]
217 */
218 p = l->l_proc;
219 lim = p->p_rlimit[RLIMIT_NOFILE].rlim_cur;
220 fdp = p->p_fd;
221 last = min(fdp->fd_nfiles, lim);
222 freefd = 0;
223 for (i = fdp->fd_freefile; i < last; i++)
224 if (fdp->fd_ofiles[i] == NULL)
225 freefd++;
226
227 /*
228 * Adjust for the fact that the fdesc array may not
229 * have been fully allocated yet.
230 */
231 if (fdp->fd_nfiles < lim)
232 freefd += (lim - fdp->fd_nfiles);
233
234 sbp->f_bsize = DEV_BSIZE;
235 sbp->f_frsize = DEV_BSIZE;
236 sbp->f_iosize = DEV_BSIZE;
237 sbp->f_blocks = 2; /* 1K to keep df happy */
238 sbp->f_bfree = 0;
239 sbp->f_bavail = 0;
240 sbp->f_bresvd = 0;
241 sbp->f_files = lim + 1; /* Allow for "." */
242 sbp->f_ffree = freefd; /* See comments above */
243 sbp->f_favail = freefd; /* See comments above */
244 sbp->f_fresvd = 0;
245 copy_statvfs_info(sbp, mp);
246 return (0);
247 }
248
249 /*ARGSUSED*/
250 int
251 fdesc_sync(mp, waitfor, uc, l)
252 struct mount *mp;
253 int waitfor;
254 struct ucred *uc;
255 struct lwp *l;
256 {
257
258 return (0);
259 }
260
261 /*
262 * Fdesc flat namespace lookup.
263 * Currently unsupported.
264 */
265 int
266 fdesc_vget(mp, ino, vpp)
267 struct mount *mp;
268 ino_t ino;
269 struct vnode **vpp;
270 {
271
272 return (EOPNOTSUPP);
273 }
274
275
276 /*ARGSUSED*/
277 int
278 fdesc_fhtovp(mp, fhp, vpp)
279 struct mount *mp;
280 struct fid *fhp;
281 struct vnode **vpp;
282 {
283
284 return (EOPNOTSUPP);
285 }
286
287 /*ARGSUSED*/
288 int
289 fdesc_checkexp(mp, nam, exflagsp, credanonp)
290 struct mount *mp;
291 struct mbuf *nam;
292 int *exflagsp;
293 struct ucred **credanonp;
294 {
295
296 return (EOPNOTSUPP);
297 }
298
299 /*ARGSUSED*/
300 int
301 fdesc_vptofh(vp, fhp)
302 struct vnode *vp;
303 struct fid *fhp;
304 {
305 return (EOPNOTSUPP);
306 }
307
308 SYSCTL_SETUP(sysctl_vfs_fdesc_setup, "sysctl vfs.fdesc subtree setup")
309 {
310
311 sysctl_createv(clog, 0, NULL, NULL,
312 CTLFLAG_PERMANENT,
313 CTLTYPE_NODE, "vfs", NULL,
314 NULL, 0, NULL, 0,
315 CTL_VFS, CTL_EOL);
316 sysctl_createv(clog, 0, NULL, NULL,
317 CTLFLAG_PERMANENT,
318 CTLTYPE_NODE, "fdesc",
319 SYSCTL_DESCR("File-descriptor file system"),
320 NULL, 0, NULL, 0,
321 CTL_VFS, 7, CTL_EOL);
322 /*
323 * XXX the "7" above could be dynamic, thereby eliminating one
324 * more instance of the "number to vfs" mapping problem, but
325 * "7" is the order as taken from sys/mount.h
326 */
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_statvfs,
344 fdesc_sync,
345 fdesc_vget,
346 fdesc_fhtovp,
347 fdesc_vptofh,
348 fdesc_init,
349 NULL,
350 fdesc_done,
351 NULL,
352 NULL, /* vfs_mountroot */
353 fdesc_checkexp,
354 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
355 vfs_stdextattrctl,
356 fdesc_vnodeopv_descs,
357 };
358