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