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