xref: /freebsd/sys/fs/fdescfs/fdesc_vfsops.c (revision 8e6b01171e30297084bb0b4457c4183c2746aacc)
1 /*
2  * Copyright (c) 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software donated to Berkeley by
6  * Jan-Simon Pendry.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)fdesc_vfsops.c	8.4 (Berkeley) 1/21/94
37  *
38  * $Id: fdesc_vfsops.c,v 1.4 1995/03/16 20:23:38 wollman Exp $
39  */
40 
41 /*
42  * /dev/fd Filesystem
43  */
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/time.h>
49 #include <sys/types.h>
50 #include <sys/proc.h>
51 #include <sys/resourcevar.h>
52 #include <sys/filedesc.h>
53 #include <sys/vnode.h>
54 #include <sys/mount.h>
55 #include <sys/namei.h>
56 #include <sys/malloc.h>
57 #include <miscfs/fdesc/fdesc.h>
58 
59 int fdesc_statfs __P((struct mount *, struct statfs *, struct proc *));
60 
61 /*
62  * Mount the per-process file descriptors (/dev/fd)
63  */
64 int
65 fdesc_mount(mp, path, data, ndp, p)
66 	struct mount *mp;
67 	char *path;
68 	caddr_t data;
69 	struct nameidata *ndp;
70 	struct proc *p;
71 {
72 	int error = 0;
73 	u_int size;
74 	struct fdescmount *fmp;
75 	struct vnode *rvp;
76 
77 	/*
78 	 * Update is a no-op
79 	 */
80 	if (mp->mnt_flag & MNT_UPDATE)
81 		return (EOPNOTSUPP);
82 
83 	error = fdesc_allocvp(Froot, FD_ROOT, mp, &rvp);
84 	if (error)
85 		return (error);
86 
87 	MALLOC(fmp, struct fdescmount *, sizeof(struct fdescmount),
88 				M_UFSMNT, M_WAITOK);	/* XXX */
89 	rvp->v_type = VDIR;
90 	rvp->v_flag |= VROOT;
91 	fmp->f_root = rvp;
92 	/* XXX -- don't mark as local to work around fts() problems */
93 	/*mp->mnt_flag |= MNT_LOCAL;*/
94 	mp->mnt_data = (qaddr_t) fmp;
95 	getnewfsid(mp, MOUNT_FDESC);
96 
97 	(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
98 	bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
99 	bzero(mp->mnt_stat.f_mntfromname, MNAMELEN);
100 	bcopy("fdesc", mp->mnt_stat.f_mntfromname, sizeof("fdesc"));
101 	(void)fdesc_statfs(mp, &mp->mnt_stat, p);
102 	return (0);
103 }
104 
105 int
106 fdesc_start(mp, flags, p)
107 	struct mount *mp;
108 	int flags;
109 	struct proc *p;
110 {
111 	return (0);
112 }
113 
114 int
115 fdesc_unmount(mp, mntflags, p)
116 	struct mount *mp;
117 	int mntflags;
118 	struct proc *p;
119 {
120 	int error;
121 	int flags = 0;
122 	struct vnode *rootvp = VFSTOFDESC(mp)->f_root;
123 
124 	if (mntflags & MNT_FORCE) {
125 		/* fdesc can never be rootfs so don't check for it */
126 		if (!doforce)
127 			return (EINVAL);
128 		flags |= FORCECLOSE;
129 	}
130 
131 	/*
132 	 * Clear out buffer cache.  I don't think we
133 	 * ever get anything cached at this level at the
134 	 * moment, but who knows...
135 	 */
136 	if (rootvp->v_usecount > 1)
137 		return (EBUSY);
138 	if (error = vflush(mp, rootvp, flags))
139 		return (error);
140 
141 	/*
142 	 * Release reference on underlying root vnode
143 	 */
144 	vrele(rootvp);
145 	/*
146 	 * And blow it away for future re-use
147 	 */
148 	vgone(rootvp);
149 	/*
150 	 * Finally, throw away the fdescmount structure
151 	 */
152 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
153 	mp->mnt_data = 0;
154 
155 	return (0);
156 }
157 
158 int
159 fdesc_root(mp, vpp)
160 	struct mount *mp;
161 	struct vnode **vpp;
162 {
163 	struct vnode *vp;
164 
165 	/*
166 	 * Return locked reference to root.
167 	 */
168 	vp = VFSTOFDESC(mp)->f_root;
169 	VREF(vp);
170 	VOP_LOCK(vp);
171 	*vpp = vp;
172 	return (0);
173 }
174 
175 int
176 fdesc_quotactl(mp, cmd, uid, arg, p)
177 	struct mount *mp;
178 	int cmd;
179 	uid_t uid;
180 	caddr_t arg;
181 	struct proc *p;
182 {
183 
184 	return (EOPNOTSUPP);
185 }
186 
187 int
188 fdesc_statfs(mp, sbp, p)
189 	struct mount *mp;
190 	struct statfs *sbp;
191 	struct proc *p;
192 {
193 	struct filedesc *fdp;
194 	int lim;
195 	int i;
196 	int last;
197 	int freefd;
198 
199 	/*
200 	 * Compute number of free file descriptors.
201 	 * [ Strange results will ensue if the open file
202 	 * limit is ever reduced below the current number
203 	 * of open files... ]
204 	 */
205 	lim = p->p_rlimit[RLIMIT_NOFILE].rlim_cur;
206 	fdp = p->p_fd;
207 	last = min(fdp->fd_nfiles, lim);
208 	freefd = 0;
209 	for (i = fdp->fd_freefile; i < last; i++)
210 		if (fdp->fd_ofiles[i] == NULL)
211 			freefd++;
212 
213 	/*
214 	 * Adjust for the fact that the fdesc array may not
215 	 * have been fully allocated yet.
216 	 */
217 	if (fdp->fd_nfiles < lim)
218 		freefd += (lim - fdp->fd_nfiles);
219 
220 	sbp->f_type = MOUNT_FDESC;
221 	sbp->f_flags = 0;
222 	sbp->f_bsize = DEV_BSIZE;
223 	sbp->f_iosize = DEV_BSIZE;
224 	sbp->f_blocks = 2;		/* 1K to keep df happy */
225 	sbp->f_bfree = 0;
226 	sbp->f_bavail = 0;
227 	sbp->f_files = lim + 1;		/* Allow for "." */
228 	sbp->f_ffree = freefd;		/* See comments above */
229 	if (sbp != &mp->mnt_stat) {
230 		bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
231 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
232 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
233 	}
234 	return (0);
235 }
236 
237 int
238 fdesc_sync(mp, waitfor)
239 	struct mount *mp;
240 	int waitfor;
241 {
242 
243 	return (0);
244 }
245 
246 /*
247  * Fdesc flat namespace lookup.
248  * Currently unsupported.
249  */
250 int
251 fdesc_vget(mp, ino, vpp)
252 	struct mount *mp;
253 	ino_t ino;
254 	struct vnode **vpp;
255 {
256 
257 	return (EOPNOTSUPP);
258 }
259 
260 int
261 fdesc_fhtovp(mp, fhp, setgen, vpp)
262 	struct mount *mp;
263 	struct fid *fhp;
264 	int setgen;
265 	struct vnode **vpp;
266 {
267 	return (EOPNOTSUPP);
268 }
269 
270 int
271 fdesc_vptofh(vp, fhp)
272 	struct vnode *vp;
273 	struct fid *fhp;
274 {
275 
276 	return (EOPNOTSUPP);
277 }
278 
279 struct vfsops fdesc_vfsops = {
280 	fdesc_mount,
281 	fdesc_start,
282 	fdesc_unmount,
283 	fdesc_root,
284 	fdesc_quotactl,
285 	fdesc_statfs,
286 	fdesc_sync,
287 	fdesc_vget,
288 	fdesc_fhtovp,
289 	fdesc_vptofh,
290 	fdesc_init,
291 };
292 
293 VFS_SET(fdesc_vfsops, fdesc, MOUNT_FDESC, VFCF_SYNTHETIC);
294