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