1 /*
2  * Copyright (c) 1992 The Regents of the University of California
3  * Copyright (c) 1990, 1992 Jan-Simon Pendry
4  * All rights reserved.
5  *
6  * This code is derived from software donated to Berkeley by
7  * Jan-Simon Pendry.
8  *
9  * %sccs.include.redist.c%
10  *
11  *	@(#)fdesc_vfsops.c	1.1 (Berkeley) 06/03/92
12  *
13  * $Id: fdesc_vfsops.c,v 1.6 1992/05/30 10:25:59 jsp Exp jsp $
14  */
15 
16 /*
17  * /dev/fd Filesystem
18  */
19 
20 #include <sys/param.h>
21 #include <sys/systm.h>
22 #include <sys/time.h>
23 #include <sys/types.h>
24 #include <sys/proc.h>
25 #include <sys/resourcevar.h>
26 #include <sys/filedesc.h>
27 #include <sys/vnode.h>
28 #include <sys/mount.h>
29 #include <sys/namei.h>
30 #include <sys/malloc.h>
31 #include <fdesc/fdesc.h>
32 
33 static u_short fdesc_mntid;
34 
35 fdesc_init()
36 {
37 #ifdef FDESC_DIAGNOSTIC
38 	printf("fdesc_init\n");		/* printed during system boot */
39 #endif
40 }
41 
42 /*
43  * Mount the per-process file descriptors (/dev/fd)
44  */
45 fdesc_mount(mp, path, data, ndp, p)
46 	struct mount *mp;
47 	char *path;
48 	caddr_t data;
49 	struct nameidata *ndp;
50 	struct proc *p;
51 {
52 	int error = 0;
53 	u_int size;
54 	struct fdescmount *fmp;
55 	struct vnode *rvp;
56 
57 #ifdef FDESC_DIAGNOSTIC
58 	printf("fdesc_mount(mp = %x)\n", mp);
59 #endif
60 
61 	/*
62 	 * Update is a no-op
63 	 */
64 	if (mp->mnt_flag & MNT_UPDATE)
65 		return (EOPNOTSUPP);
66 
67 	error = getnewvnode(VT_UFS, mp, fdesc_vnodeop_p, &rvp);	/* XXX */
68 	if (error)
69 		return (error);
70 
71 	MALLOC(fmp, struct fdescmount *, sizeof(struct fdescmount),
72 				M_UFSMNT, M_WAITOK);	/* XXX */
73 	rvp->v_type = VDIR;
74 	rvp->v_flag |= VROOT;
75 #ifdef FDESC_DIAGNOSTIC
76 	printf("fdesc_mount: root vp = %x\n", rvp);
77 #endif
78 	fmp->f_root = rvp;
79 	mp->mnt_flag |= MNT_LOCAL;
80 	mp->mnt_data = (qaddr_t) fmp;
81 	getnewfsid(mp, MOUNT_FDESC);
82 
83 	(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
84 	bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
85 	bzero(mp->mnt_stat.f_mntfromname, MNAMELEN);
86 	bcopy("fdesc", mp->mnt_stat.f_mntfromname, sizeof("fdesc"));
87 #ifdef FDESC_DIAGNOSTIC
88 	printf("fdesc_mount: at %s\n", mp->mnt_stat.f_mntonname);
89 #endif
90 	return (0);
91 }
92 
93 fdesc_start(mp, flags, p)
94 	struct mount *mp;
95 	int flags;
96 	struct proc *p;
97 {
98 	return (0);
99 }
100 
101 fdesc_unmount(mp, mntflags, p)
102 	struct mount *mp;
103 	int mntflags;
104 	struct proc *p;
105 {
106 	int error;
107 	int flags = 0;
108 	extern int doforce;
109 	struct vnode *rootvp = VFSTOFDESC(mp)->f_root;
110 
111 #ifdef FDESC_DIAGNOSTIC
112 	printf("fdesc_unmount(mp = %x)\n", mp);
113 #endif
114 
115 	if (mntflags & MNT_FORCE) {
116 		/* fdesc can never be rootfs so don't check for it */
117 		if (!doforce)
118 			return (EINVAL);
119 		flags |= FORCECLOSE;
120 	}
121 
122 	/*
123 	 * Clear out buffer cache.  I don't think we
124 	 * ever get anything cached at this level at the
125 	 * moment, but who knows...
126 	 */
127 #ifdef FDESC_DIAGNOSTIC
128 	printf("fdesc_unmount: calling mntflushbuf\n");
129 #endif
130 	mntflushbuf(mp, 0);
131 #ifdef FDESC_DIAGNOSTIC
132 	printf("fdesc_unmount: calling mntinvalbuf\n");
133 #endif
134 	if (mntinvalbuf(mp, 1))
135 		return (EBUSY);
136 	if (rootvp->v_usecount > 1)
137 		return (EBUSY);
138 #ifdef FDESC_DIAGNOSTIC
139 	printf("fdesc_unmount: calling vflush\n");
140 #endif
141 	if (error = vflush(mp, rootvp, flags))
142 		return (error);
143 
144 #ifdef FDESC_DIAGNOSTIC
145 	vprint("fdesc root", rootvp);
146 #endif
147 	/*
148 	 * Release reference on underlying root vnode
149 	 */
150 	vrele(rootvp);
151 	/*
152 	 * And blow it away for future re-use
153 	 */
154 	vgone(rootvp);
155 	/*
156 	 * Finally, throw away the fdescmount structure
157 	 */
158 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
159 	mp->mnt_data = 0;
160 	return 0;
161 }
162 
163 fdesc_root(mp, vpp)
164 	struct mount *mp;
165 	struct vnode **vpp;
166 {
167 	USES_VOP_LOCK;
168 	struct vnode *vp;
169 	int error;
170 
171 #ifdef FDESC_DIAGNOSTIC
172 	printf("fdesc_root(mp = %x)\n", mp);
173 #endif
174 
175 	/*
176 	 * Return locked reference to root.
177 	 */
178 #ifdef FDESC_DIAGNOSTIC
179 	printf("fdesc_root: fdesc = %x\n", VFSTOFDESC(mp));
180 #endif
181 	vp = VFSTOFDESC(mp)->f_root;
182 #ifdef FDESC_DIAGNOSTIC
183 	printf("fdesc_root: root = %x\n", vp);
184 #endif
185 	VREF(vp);
186 #ifdef FDESC_DIAGNOSTIC
187 	printf("fdesc_root: calling VOP_LOCK\n");
188 #endif
189 	VOP_LOCK(vp);
190 #ifdef FDESC_DIAGNOSTIC
191 	printf("fdesc_root: back from VOP_LOCK\n");
192 #endif
193 	*vpp = vp;
194 #ifdef FDESC_DIAGNOSTIC
195 	printf("fdesc_root(mp = %x) = %x\n", mp, vp);
196 #endif
197 	return (0);
198 }
199 
200 fdesc_quotactl(mp, cmd, uid, arg, p)
201 	struct mount *mp;
202 	int cmd;
203 	uid_t uid;
204 	caddr_t arg;
205 	struct proc *p;
206 {
207 	return (EOPNOTSUPP);
208 }
209 
210 fdesc_statfs(mp, sbp, p)
211 	struct mount *mp;
212 	struct statfs *sbp;
213 	struct proc *p;
214 {
215 	struct filedesc *fdp;
216 	int lim;
217 	int i;
218 	int last;
219 	int freefd;
220 
221 #ifdef FDESC_DIAGNOSTIC
222 	printf("fdesc_statfs(mp = %x)\n", mp);
223 #endif
224 
225 	/*
226 	 * Compute number of free file descriptors.
227 	 * [ Strange results will ensue if the open file
228 	 * limit is ever reduced below the current number
229 	 * of open files... ]
230 	 */
231 	lim = p->p_rlimit[RLIMIT_OFILE].rlim_cur;
232 	fdp = p->p_fd;
233 	last = min(fdp->fd_nfiles, lim);
234 	freefd = 0;
235 	for (i = fdp->fd_freefile; i < last; i++)
236 		if (fdp->fd_ofiles[i] == NULL)
237 			freefd++;
238 
239 	/*
240 	 * Adjust for the fact that the fdesc array may not
241 	 * have been fully allocated yet.
242 	 */
243 	if (fdp->fd_nfiles < lim)
244 		freefd += (lim - fdp->fd_nfiles);
245 
246 	sbp->f_type = MOUNT_FDESC;
247 	sbp->f_flags = 0;
248 	sbp->f_bsize = DEV_BSIZE;
249 	sbp->f_iosize = DEV_BSIZE;
250 	sbp->f_blocks = 2;		/* 1K to keep df happy */
251 	sbp->f_bfree = 0;
252 	sbp->f_bavail = 0;
253 	sbp->f_files = lim + 1;		/* Allow for "." */
254 	sbp->f_ffree = freefd;		/* See comments above */
255 	if (sbp != &mp->mnt_stat) {
256 		bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
257 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
258 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
259 	}
260 	return (0);
261 }
262 
263 fdesc_sync(mp, waitfor)
264 	struct mount *mp;
265 	int waitfor;
266 {
267 	return (0);
268 }
269 
270 fdesc_fhtovp(mp, fhp, setgen, vpp)
271 	struct mount *mp;
272 	struct fid *fhp;
273 	int setgen;
274 	struct vnode **vpp;
275 {
276 	return (EOPNOTSUPP);
277 }
278 
279 fdesc_vptofh(vp, fhp)
280 	struct vnode *vp;
281 	struct fid *fhp;
282 {
283 	return (EOPNOTSUPP);
284 }
285 
286 struct vfsops fdesc_vfsops = {
287 	fdesc_mount,
288 	fdesc_start,
289 	fdesc_unmount,
290 	fdesc_root,
291 	fdesc_quotactl,
292 	fdesc_statfs,
293 	fdesc_sync,
294 	fdesc_fhtovp,
295 	fdesc_vptofh,
296 	fdesc_init,
297 };
298