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