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