xref: /netbsd-src/sys/miscfs/fdesc/fdesc_vfsops.c (revision 89c5a767f8fc7a4633b2d409966e2becbb98ff92)
1 /*	$NetBSD: fdesc_vfsops.c,v 1.28 1999/07/08 01:26:26 wrstuden 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) && !defined(_LKM)
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_root __P((struct mount *, struct vnode **));
69 int	fdesc_quotactl __P((struct mount *, int, uid_t, caddr_t,
70 			    struct proc *));
71 int	fdesc_statfs __P((struct mount *, struct statfs *, struct proc *));
72 int	fdesc_sync __P((struct mount *, int, struct ucred *, struct proc *));
73 int	fdesc_vget __P((struct mount *, ino_t, struct vnode **));
74 int	fdesc_fhtovp __P((struct mount *, struct fid *, struct vnode **));
75 int	fdesc_checkexp __P((struct mount *, struct mbuf *, int *,
76 			    struct ucred **));
77 int	fdesc_vptofh __P((struct vnode *, struct fid *));
78 int	fdesc_sysctl __P((int *, u_int, void *, size_t *, void *, size_t,
79 			  struct proc *));
80 
81 /*
82  * Mount the per-process file descriptors (/dev/fd)
83  */
84 int
85 fdesc_mount(mp, path, data, ndp, p)
86 	struct mount *mp;
87 	const char *path;
88 	void *data;
89 	struct nameidata *ndp;
90 	struct proc *p;
91 {
92 	int error = 0;
93 	size_t size;
94 	struct fdescmount *fmp;
95 	struct vnode *rvp;
96 
97 	/*
98 	 * Update is a no-op
99 	 */
100 	if (mp->mnt_flag & MNT_UPDATE)
101 		return (EOPNOTSUPP);
102 
103 	error = fdesc_allocvp(Froot, FD_ROOT, mp, &rvp);
104 	if (error)
105 		return (error);
106 
107 	MALLOC(fmp, struct fdescmount *, sizeof(struct fdescmount),
108 				M_UFSMNT, M_WAITOK);	/* XXX */
109 	rvp->v_type = VDIR;
110 	rvp->v_flag |= VROOT;
111 	fmp->f_root = rvp;
112 	mp->mnt_flag |= MNT_LOCAL;
113 	mp->mnt_data = (qaddr_t)fmp;
114 	vfs_getnewfsid(mp, MOUNT_FDESC);
115 
116 	(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
117 	memset(mp->mnt_stat.f_mntonname + size, 0, MNAMELEN - size);
118 	memset(mp->mnt_stat.f_mntfromname, 0, MNAMELEN);
119 	memcpy(mp->mnt_stat.f_mntfromname, "fdesc", sizeof("fdesc"));
120 	VOP_UNLOCK(rvp, 0);
121 	return (0);
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 	if (sbp != &mp->mnt_stat) {
248 		memcpy(&sbp->f_fsid, &mp->mnt_stat.f_fsid, sizeof(sbp->f_fsid));
249 		memcpy(sbp->f_mntonname, mp->mnt_stat.f_mntonname, MNAMELEN);
250 		memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, MNAMELEN);
251 	}
252 	strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
253 	return (0);
254 }
255 
256 /*ARGSUSED*/
257 int
258 fdesc_sync(mp, waitfor, uc, p)
259 	struct mount *mp;
260 	int waitfor;
261 	struct ucred *uc;
262 	struct proc *p;
263 {
264 
265 	return (0);
266 }
267 
268 /*
269  * Fdesc flat namespace lookup.
270  * Currently unsupported.
271  */
272 int
273 fdesc_vget(mp, ino, vpp)
274 	struct mount *mp;
275 	ino_t ino;
276 	struct vnode **vpp;
277 {
278 
279 	return (EOPNOTSUPP);
280 }
281 
282 
283 /*ARGSUSED*/
284 int
285 fdesc_fhtovp(mp, fhp, vpp)
286 	struct mount *mp;
287 	struct fid *fhp;
288 	struct vnode **vpp;
289 {
290 
291 	return (EOPNOTSUPP);
292 }
293 
294 /*ARGSUSED*/
295 int
296 fdesc_checkexp(mp, nam, exflagsp, credanonp)
297 	struct mount *mp;
298 	struct mbuf *nam;
299 	int *exflagsp;
300 	struct ucred **credanonp;
301 {
302 
303 	return (EOPNOTSUPP);
304 }
305 
306 /*ARGSUSED*/
307 int
308 fdesc_vptofh(vp, fhp)
309 	struct vnode *vp;
310 	struct fid *fhp;
311 {
312 	return (EOPNOTSUPP);
313 }
314 
315 int
316 fdesc_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
317 	int *name;
318 	u_int namelen;
319 	void *oldp;
320 	size_t *oldlenp;
321 	void *newp;
322 	size_t newlen;
323 	struct proc *p;
324 {
325 	return (EOPNOTSUPP);
326 }
327 
328 extern struct vnodeopv_desc fdesc_vnodeop_opv_desc;
329 
330 struct vnodeopv_desc *fdesc_vnodeopv_descs[] = {
331 	&fdesc_vnodeop_opv_desc,
332 	NULL,
333 };
334 
335 struct vfsops fdesc_vfsops = {
336 	MOUNT_FDESC,
337 	fdesc_mount,
338 	fdesc_start,
339 	fdesc_unmount,
340 	fdesc_root,
341 	fdesc_quotactl,
342 	fdesc_statfs,
343 	fdesc_sync,
344 	fdesc_vget,
345 	fdesc_fhtovp,
346 	fdesc_vptofh,
347 	fdesc_init,
348 	fdesc_sysctl,
349 	NULL,				/* vfs_mountroot */
350 	fdesc_checkexp,
351 	fdesc_vnodeopv_descs,
352 };
353