xref: /netbsd-src/sys/miscfs/fdesc/fdesc_vfsops.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /*	$NetBSD: fdesc_vfsops.c,v 1.39 2003/04/16 21:44:22 christos 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.39 2003/04/16 21:44:22 christos 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 	struct fdescmount *fmp;
95 	struct vnode *rvp;
96 
97 	if (mp->mnt_flag & MNT_GETARGS)
98 		return 0;
99 	/*
100 	 * Update is a no-op
101 	 */
102 	if (mp->mnt_flag & MNT_UPDATE)
103 		return (EOPNOTSUPP);
104 
105 	error = fdesc_allocvp(Froot, FD_ROOT, mp, &rvp);
106 	if (error)
107 		return (error);
108 
109 	MALLOC(fmp, struct fdescmount *, sizeof(struct fdescmount),
110 				M_UFSMNT, M_WAITOK);	/* XXX */
111 	rvp->v_type = VDIR;
112 	rvp->v_flag |= VROOT;
113 	fmp->f_root = rvp;
114 	mp->mnt_flag |= MNT_LOCAL;
115 	mp->mnt_data = fmp;
116 	vfs_getnewfsid(mp);
117 
118 	error = set_statfs_info(path, UIO_USERSPACE, "fdesc", UIO_SYSSPACE,
119 	    mp, p);
120 	VOP_UNLOCK(rvp, 0);
121 	return error;
122 }
123 
124 int
125 fdesc_start(mp, flags, p)
126 	struct mount *mp;
127 	int flags;
128 	struct proc *p;
129 {
130 	return (0);
131 }
132 
133 int
134 fdesc_unmount(mp, mntflags, p)
135 	struct mount *mp;
136 	int mntflags;
137 	struct proc *p;
138 {
139 	int error;
140 	int flags = 0;
141 	struct vnode *rootvp = VFSTOFDESC(mp)->f_root;
142 
143 	if (mntflags & MNT_FORCE)
144 		flags |= FORCECLOSE;
145 
146 	/*
147 	 * Clear out buffer cache.  I don't think we
148 	 * ever get anything cached at this level at the
149 	 * moment, but who knows...
150 	 */
151 	if (rootvp->v_usecount > 1)
152 		return (EBUSY);
153 	if ((error = vflush(mp, rootvp, flags)) != 0)
154 		return (error);
155 
156 	/*
157 	 * Release reference on underlying root vnode
158 	 */
159 	vrele(rootvp);
160 	/*
161 	 * And blow it away for future re-use
162 	 */
163 	vgone(rootvp);
164 	/*
165 	 * Finally, throw away the fdescmount structure
166 	 */
167 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
168 	mp->mnt_data = 0;
169 
170 	return (0);
171 }
172 
173 int
174 fdesc_root(mp, vpp)
175 	struct mount *mp;
176 	struct vnode **vpp;
177 {
178 	struct vnode *vp;
179 
180 	/*
181 	 * Return locked reference to root.
182 	 */
183 	vp = VFSTOFDESC(mp)->f_root;
184 	VREF(vp);
185 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
186 	*vpp = vp;
187 	return (0);
188 }
189 
190 int
191 fdesc_quotactl(mp, cmd, uid, arg, p)
192 	struct mount *mp;
193 	int cmd;
194 	uid_t uid;
195 	caddr_t arg;
196 	struct proc *p;
197 {
198 
199 	return (EOPNOTSUPP);
200 }
201 
202 int
203 fdesc_statfs(mp, sbp, p)
204 	struct mount *mp;
205 	struct statfs *sbp;
206 	struct proc *p;
207 {
208 	struct filedesc *fdp;
209 	int lim;
210 	int i;
211 	int last;
212 	int freefd;
213 
214 	/*
215 	 * Compute number of free file descriptors.
216 	 * [ Strange results will ensue if the open file
217 	 * limit is ever reduced below the current number
218 	 * of open files... ]
219 	 */
220 	lim = p->p_rlimit[RLIMIT_NOFILE].rlim_cur;
221 	fdp = p->p_fd;
222 	last = min(fdp->fd_nfiles, lim);
223 	freefd = 0;
224 	for (i = fdp->fd_freefile; i < last; i++)
225 		if (fdp->fd_ofiles[i] == NULL)
226 			freefd++;
227 
228 	/*
229 	 * Adjust for the fact that the fdesc array may not
230 	 * have been fully allocated yet.
231 	 */
232 	if (fdp->fd_nfiles < lim)
233 		freefd += (lim - fdp->fd_nfiles);
234 
235 	sbp->f_bsize = 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_files = lim + 1;		/* Allow for "." */
241 	sbp->f_ffree = freefd;		/* See comments above */
242 #ifdef COMPAT_09
243 	sbp->f_type = 6;
244 #else
245 	sbp->f_type = 0;
246 #endif
247 	copy_statfs_info(sbp, mp);
248 	return (0);
249 }
250 
251 /*ARGSUSED*/
252 int
253 fdesc_sync(mp, waitfor, uc, p)
254 	struct mount *mp;
255 	int waitfor;
256 	struct ucred *uc;
257 	struct proc *p;
258 {
259 
260 	return (0);
261 }
262 
263 /*
264  * Fdesc flat namespace lookup.
265  * Currently unsupported.
266  */
267 int
268 fdesc_vget(mp, ino, vpp)
269 	struct mount *mp;
270 	ino_t ino;
271 	struct vnode **vpp;
272 {
273 
274 	return (EOPNOTSUPP);
275 }
276 
277 
278 /*ARGSUSED*/
279 int
280 fdesc_fhtovp(mp, fhp, vpp)
281 	struct mount *mp;
282 	struct fid *fhp;
283 	struct vnode **vpp;
284 {
285 
286 	return (EOPNOTSUPP);
287 }
288 
289 /*ARGSUSED*/
290 int
291 fdesc_checkexp(mp, nam, exflagsp, credanonp)
292 	struct mount *mp;
293 	struct mbuf *nam;
294 	int *exflagsp;
295 	struct ucred **credanonp;
296 {
297 
298 	return (EOPNOTSUPP);
299 }
300 
301 /*ARGSUSED*/
302 int
303 fdesc_vptofh(vp, fhp)
304 	struct vnode *vp;
305 	struct fid *fhp;
306 {
307 	return (EOPNOTSUPP);
308 }
309 
310 int
311 fdesc_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
312 	int *name;
313 	u_int namelen;
314 	void *oldp;
315 	size_t *oldlenp;
316 	void *newp;
317 	size_t newlen;
318 	struct proc *p;
319 {
320 	return (EOPNOTSUPP);
321 }
322 
323 extern const struct vnodeopv_desc fdesc_vnodeop_opv_desc;
324 
325 const struct vnodeopv_desc * const fdesc_vnodeopv_descs[] = {
326 	&fdesc_vnodeop_opv_desc,
327 	NULL,
328 };
329 
330 struct vfsops fdesc_vfsops = {
331 	MOUNT_FDESC,
332 	fdesc_mount,
333 	fdesc_start,
334 	fdesc_unmount,
335 	fdesc_root,
336 	fdesc_quotactl,
337 	fdesc_statfs,
338 	fdesc_sync,
339 	fdesc_vget,
340 	fdesc_fhtovp,
341 	fdesc_vptofh,
342 	fdesc_init,
343 	NULL,
344 	fdesc_done,
345 	fdesc_sysctl,
346 	NULL,				/* vfs_mountroot */
347 	fdesc_checkexp,
348 	fdesc_vnodeopv_descs,
349 };
350