xref: /netbsd/sys/miscfs/fdesc/fdesc_vfsops.c (revision bf9ec67e)
1 /*	$NetBSD: fdesc_vfsops.c,v 1.36 2001/11/15 09:48:21 lukem 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.36 2001/11/15 09:48:21 lukem 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 proc *));
68 int	fdesc_start __P((struct mount *, int, struct proc *));
69 int	fdesc_unmount __P((struct mount *, int, struct proc *));
70 int	fdesc_quotactl __P((struct mount *, int, uid_t, caddr_t,
71 			    struct proc *));
72 int	fdesc_statfs __P((struct mount *, struct statfs *, struct proc *));
73 int	fdesc_sync __P((struct mount *, int, struct ucred *, struct proc *));
74 int	fdesc_vget __P((struct mount *, ino_t, struct vnode **));
75 int	fdesc_fhtovp __P((struct mount *, struct fid *, struct vnode **));
76 int	fdesc_checkexp __P((struct mount *, struct mbuf *, int *,
77 			    struct ucred **));
78 int	fdesc_vptofh __P((struct vnode *, struct fid *));
79 int	fdesc_sysctl __P((int *, u_int, void *, size_t *, void *, size_t,
80 			  struct proc *));
81 
82 /*
83  * Mount the per-process file descriptors (/dev/fd)
84  */
85 int
86 fdesc_mount(mp, path, data, ndp, p)
87 	struct mount *mp;
88 	const char *path;
89 	void *data;
90 	struct nameidata *ndp;
91 	struct proc *p;
92 {
93 	int error = 0;
94 	size_t size;
95 	struct fdescmount *fmp;
96 	struct vnode *rvp;
97 
98 	/*
99 	 * Update is a no-op
100 	 */
101 	if (mp->mnt_flag & MNT_UPDATE)
102 		return (EOPNOTSUPP);
103 
104 	error = fdesc_allocvp(Froot, FD_ROOT, mp, &rvp);
105 	if (error)
106 		return (error);
107 
108 	MALLOC(fmp, struct fdescmount *, sizeof(struct fdescmount),
109 				M_UFSMNT, M_WAITOK);	/* XXX */
110 	rvp->v_type = VDIR;
111 	rvp->v_flag |= VROOT;
112 	fmp->f_root = rvp;
113 	mp->mnt_flag |= MNT_LOCAL;
114 	mp->mnt_data = (qaddr_t)fmp;
115 	vfs_getnewfsid(mp);
116 
117 	(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
118 	memset(mp->mnt_stat.f_mntonname + size, 0, MNAMELEN - size);
119 	memset(mp->mnt_stat.f_mntfromname, 0, MNAMELEN);
120 	memcpy(mp->mnt_stat.f_mntfromname, "fdesc", sizeof("fdesc"));
121 	VOP_UNLOCK(rvp, 0);
122 	return (0);
123 }
124 
125 int
126 fdesc_start(mp, flags, p)
127 	struct mount *mp;
128 	int flags;
129 	struct proc *p;
130 {
131 	return (0);
132 }
133 
134 int
135 fdesc_unmount(mp, mntflags, p)
136 	struct mount *mp;
137 	int mntflags;
138 	struct proc *p;
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)
176 	struct mount *mp;
177 	struct vnode **vpp;
178 {
179 	struct vnode *vp;
180 
181 	/*
182 	 * Return locked reference to root.
183 	 */
184 	vp = VFSTOFDESC(mp)->f_root;
185 	VREF(vp);
186 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
187 	*vpp = vp;
188 	return (0);
189 }
190 
191 int
192 fdesc_quotactl(mp, cmd, uid, arg, p)
193 	struct mount *mp;
194 	int cmd;
195 	uid_t uid;
196 	caddr_t arg;
197 	struct proc *p;
198 {
199 
200 	return (EOPNOTSUPP);
201 }
202 
203 int
204 fdesc_statfs(mp, sbp, p)
205 	struct mount *mp;
206 	struct statfs *sbp;
207 	struct proc *p;
208 {
209 	struct filedesc *fdp;
210 	int lim;
211 	int i;
212 	int last;
213 	int freefd;
214 
215 	/*
216 	 * Compute number of free file descriptors.
217 	 * [ Strange results will ensue if the open file
218 	 * limit is ever reduced below the current number
219 	 * of open files... ]
220 	 */
221 	lim = p->p_rlimit[RLIMIT_NOFILE].rlim_cur;
222 	fdp = p->p_fd;
223 	last = min(fdp->fd_nfiles, lim);
224 	freefd = 0;
225 	for (i = fdp->fd_freefile; i < last; i++)
226 		if (fdp->fd_ofiles[i] == NULL)
227 			freefd++;
228 
229 	/*
230 	 * Adjust for the fact that the fdesc array may not
231 	 * have been fully allocated yet.
232 	 */
233 	if (fdp->fd_nfiles < lim)
234 		freefd += (lim - fdp->fd_nfiles);
235 
236 	sbp->f_bsize = DEV_BSIZE;
237 	sbp->f_iosize = DEV_BSIZE;
238 	sbp->f_blocks = 2;		/* 1K to keep df happy */
239 	sbp->f_bfree = 0;
240 	sbp->f_bavail = 0;
241 	sbp->f_files = lim + 1;		/* Allow for "." */
242 	sbp->f_ffree = freefd;		/* See comments above */
243 #ifdef COMPAT_09
244 	sbp->f_type = 6;
245 #else
246 	sbp->f_type = 0;
247 #endif
248 	if (sbp != &mp->mnt_stat) {
249 		memcpy(&sbp->f_fsid, &mp->mnt_stat.f_fsid, sizeof(sbp->f_fsid));
250 		memcpy(sbp->f_mntonname, mp->mnt_stat.f_mntonname, MNAMELEN);
251 		memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, MNAMELEN);
252 	}
253 	strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
254 	return (0);
255 }
256 
257 /*ARGSUSED*/
258 int
259 fdesc_sync(mp, waitfor, uc, p)
260 	struct mount *mp;
261 	int waitfor;
262 	struct ucred *uc;
263 	struct proc *p;
264 {
265 
266 	return (0);
267 }
268 
269 /*
270  * Fdesc flat namespace lookup.
271  * Currently unsupported.
272  */
273 int
274 fdesc_vget(mp, ino, vpp)
275 	struct mount *mp;
276 	ino_t ino;
277 	struct vnode **vpp;
278 {
279 
280 	return (EOPNOTSUPP);
281 }
282 
283 
284 /*ARGSUSED*/
285 int
286 fdesc_fhtovp(mp, fhp, vpp)
287 	struct mount *mp;
288 	struct fid *fhp;
289 	struct vnode **vpp;
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, p)
318 	int *name;
319 	u_int namelen;
320 	void *oldp;
321 	size_t *oldlenp;
322 	void *newp;
323 	size_t newlen;
324 	struct proc *p;
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