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