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