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