1 /*
2  * Copyright (c) 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
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  *	@(#)portal_vfsops.c	8.4 (Berkeley) 01/04/94
12  *
13  * $Id: portal_vfsops.c,v 1.5 1992/05/30 10:25:27 jsp Exp jsp $
14  */
15 
16 /*
17  * Portal 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/filedesc.h>
26 #include <sys/file.h>
27 #include <sys/vnode.h>
28 #include <sys/mount.h>
29 #include <sys/namei.h>
30 #include <sys/malloc.h>
31 #include <sys/mbuf.h>
32 #include <sys/socket.h>
33 #include <sys/socketvar.h>
34 #include <sys/protosw.h>
35 #include <sys/domain.h>
36 #include <sys/un.h>
37 #include <miscfs/portal/portal.h>
38 
39 int
40 portal_init()
41 {
42 
43 #ifdef PORTAL_DIAGNOSTIC
44 	printf("portal_init\n");		/* printed during system boot */
45 #endif
46 }
47 
48 /*
49  * Mount the per-process file descriptors (/dev/fd)
50  */
51 portal_mount(mp, path, data, ndp, p)
52 	struct mount *mp;
53 	char *path;
54 	caddr_t data;
55 	struct nameidata *ndp;
56 	struct proc *p;
57 {
58 	struct file *fp;
59 	struct portal_args args;
60 	struct portalmount *fmp;
61 	struct socket *so;
62 	struct vnode *rvp;
63 	u_int size;
64 	int error;
65 
66 #ifdef PORTAL_DIAGNOSTIC
67 	printf("portal_mount(mp = %x)\n", mp);
68 #endif
69 
70 	/*
71 	 * Update is a no-op
72 	 */
73 	if (mp->mnt_flag & MNT_UPDATE)
74 		return (EOPNOTSUPP);
75 
76 	if (error = copyin(data, (caddr_t) &args, sizeof(struct portal_args)))
77 		return (error);
78 
79 	if (error = getsock(p->p_fd, args.pa_socket, &fp))
80 		return (error);
81 	so = (struct socket *) fp->f_data;
82 	if (so->so_proto->pr_domain->dom_family != AF_UNIX)
83 		return (ESOCKTNOSUPPORT);
84 
85 	error = getnewvnode(VT_PORTAL, mp, portal_vnodeop_p, &rvp); /* XXX */
86 	if (error)
87 		return (error);
88 	MALLOC(rvp->v_data, void *, sizeof(struct portalnode),
89 		M_TEMP, M_WAITOK);
90 
91 	fmp = (struct portalmount *) malloc(sizeof(struct portalmount),
92 				 M_UFSMNT, M_WAITOK);	/* XXX */
93 	rvp->v_type = VDIR;
94 	rvp->v_flag |= VROOT;
95 	VTOPORTAL(rvp)->pt_arg = 0;
96 	VTOPORTAL(rvp)->pt_size = 0;
97 	VTOPORTAL(rvp)->pt_fileid = PORTAL_ROOTFILEID;
98 #ifdef PORTAL_DIAGNOSTIC
99 	printf("portal_mount: root vp = %x\n", rvp);
100 #endif
101 	fmp->pm_root = rvp;
102 	fmp->pm_server = fp; fp->f_count++;
103 
104 	mp->mnt_flag |= MNT_LOCAL;
105 	mp->mnt_data = (qaddr_t) fmp;
106 	getnewfsid(mp, MOUNT_PORTAL);
107 
108 	(void)copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
109 	bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
110 	(void)copyinstr(args.pa_config,
111 	    mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
112 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
113 
114 #ifdef notdef
115 	bzero(mp->mnt_stat.f_mntfromname, MNAMELEN);
116 	bcopy("portal", mp->mnt_stat.f_mntfromname, sizeof("portal"));
117 #endif
118 
119 #ifdef PORTAL_DIAGNOSTIC
120 	printf("portal_mount: config %s at %s\n",
121 	    mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
122 #endif
123 	return (0);
124 }
125 
126 portal_start(mp, flags, p)
127 	struct mount *mp;
128 	int flags;
129 	struct proc *p;
130 {
131 
132 	return (0);
133 }
134 
135 portal_unmount(mp, mntflags, p)
136 	struct mount *mp;
137 	int mntflags;
138 	struct proc *p;
139 {
140 	extern int doforce;
141 	struct vnode *rootvp = VFSTOPORTAL(mp)->pm_root;
142 	int error, flags = 0;
143 
144 #ifdef PORTAL_DIAGNOSTIC
145 	printf("portal_unmount(mp = %x)\n", mp);
146 #endif
147 
148 	if (mntflags & MNT_FORCE) {
149 		/* portal can never be rootfs so don't check for it */
150 		if (!doforce)
151 			return (EINVAL);
152 		flags |= FORCECLOSE;
153 	}
154 
155 	/*
156 	 * Clear out buffer cache.  I don't think we
157 	 * ever get anything cached at this level at the
158 	 * moment, but who knows...
159 	 */
160 #ifdef notyet
161 #ifdef PORTAL_DIAGNOSTIC
162 	printf("portal_unmount: calling mntflushbuf\n");
163 #endif
164 	mntflushbuf(mp, 0);
165 #ifdef PORTAL_DIAGNOSTIC
166 	printf("portal_unmount: calling mntinvalbuf\n");
167 #endif
168 	if (mntinvalbuf(mp, 1))
169 		return (EBUSY);
170 #endif
171 	if (rootvp->v_usecount > 1)
172 		return (EBUSY);
173 #ifdef PORTAL_DIAGNOSTIC
174 	printf("portal_unmount: calling vflush\n");
175 #endif
176 	if (error = vflush(mp, rootvp, flags))
177 		return (error);
178 
179 #ifdef PORTAL_DIAGNOSTIC
180 	vprint("portal root", rootvp);
181 #endif
182 	/*
183 	 * Release reference on underlying root vnode
184 	 */
185 	vrele(rootvp);
186 	/*
187 	 * And blow it away for future re-use
188 	 */
189 	vgone(rootvp);
190 	/*
191 	 * Shutdown the socket.  This will cause the select in the
192 	 * daemon to wake up, and then the accept will get ECONNABORTED
193 	 * which it interprets as a request to go and bury itself.
194 	 */
195 #ifdef PORTAL_DIAGNOSTIC
196 	printf("portal_unmount: shutdown socket\n");
197 #endif
198 	soshutdown((struct socket *) VFSTOPORTAL(mp)->pm_server->f_data, 2);
199 	/*
200 	 * Discard reference to underlying file.  Must call closef because
201 	 * this may be the last reference.
202 	 */
203 #ifdef PORTAL_DIAGNOSTIC
204 	printf("portal_unmount: closef(%x)\n", VFSTOPORTAL(mp)->pm_server);
205 #endif
206 	closef(VFSTOPORTAL(mp)->pm_server, (struct proc *) 0);
207 	/*
208 	 * Finally, throw away the portalmount structure
209 	 */
210 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
211 	mp->mnt_data = 0;
212 	return (0);
213 }
214 
215 portal_root(mp, vpp)
216 	struct mount *mp;
217 	struct vnode **vpp;
218 {
219 	struct vnode *vp;
220 
221 #ifdef PORTAL_DIAGNOSTIC
222 	printf("portal_root(mp = %x)\n", mp);
223 #endif
224 
225 	/*
226 	 * Return locked reference to root.
227 	 */
228 	vp = VFSTOPORTAL(mp)->pm_root;
229 	VREF(vp);
230 	VOP_LOCK(vp);
231 	*vpp = vp;
232 	return (0);
233 }
234 
235 portal_quotactl(mp, cmd, uid, arg, p)
236 	struct mount *mp;
237 	int cmd;
238 	uid_t uid;
239 	caddr_t arg;
240 	struct proc *p;
241 {
242 
243 	return (EOPNOTSUPP);
244 }
245 
246 portal_statfs(mp, sbp, p)
247 	struct mount *mp;
248 	struct statfs *sbp;
249 	struct proc *p;
250 {
251 #ifdef PORTAL_DIAGNOSTIC
252 	printf("portal_statfs(mp = %x)\n", mp);
253 #endif
254 
255 	sbp->f_type = MOUNT_PORTAL;
256 	sbp->f_flags = 0;
257 	sbp->f_bsize = DEV_BSIZE;
258 	sbp->f_iosize = DEV_BSIZE;
259 	sbp->f_blocks = 2;		/* 1K to keep df happy */
260 	sbp->f_bfree = 0;
261 	sbp->f_bavail = 0;
262 	sbp->f_files = 1;		/* Allow for "." */
263 	sbp->f_ffree = 0;		/* See comments above */
264 	if (sbp != &mp->mnt_stat) {
265 		bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
266 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
267 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
268 	}
269 	return (0);
270 }
271 
272 portal_sync(mp, waitfor)
273 	struct mount *mp;
274 	int waitfor;
275 {
276 
277 	return (0);
278 }
279 
280 portal_vget(mp, ino, vpp)
281 	struct mount *mp;
282 	ino_t ino;
283 	struct vnode **vpp;
284 {
285 
286 	return (EOPNOTSUPP);
287 }
288 
289 portal_fhtovp(mp, fhp, vpp)
290 	struct mount *mp;
291 	struct fid *fhp;
292 	struct vnode **vpp;
293 {
294 
295 	return (EOPNOTSUPP);
296 }
297 
298 portal_vptofh(vp, fhp)
299 	struct vnode *vp;
300 	struct fid *fhp;
301 {
302 
303 	return (EOPNOTSUPP);
304 }
305 
306 struct vfsops portal_vfsops = {
307 	portal_mount,
308 	portal_start,
309 	portal_unmount,
310 	portal_root,
311 	portal_quotactl,
312 	portal_statfs,
313 	portal_sync,
314 	portal_vget,
315 	portal_fhtovp,
316 	portal_vptofh,
317 	portal_init,
318 };
319