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  *	@(#)null_vfsops.c	1.5 (Berkeley) 07/10/92
12  *
13  * @(#)lofs_vfsops.c	1.2 (Berkeley) 6/18/92
14  * $Id: lofs_vfsops.c,v 1.9 1992/05/30 10:26:24 jsp Exp jsp $
15  */
16 
17 /*
18  * Null Layer
19  * (See null_vnops.c for a description of what this does.)
20  */
21 
22 #include <sys/param.h>
23 #include <sys/systm.h>
24 #include <sys/time.h>
25 #include <sys/types.h>
26 #include <sys/vnode.h>
27 #include <sys/mount.h>
28 #include <sys/namei.h>
29 #include <sys/malloc.h>
30 #include <nullfs/null.h>
31 
32 /*
33  * Mount null layer
34  */
35 int
36 nullfs_mount(mp, path, data, ndp, p)
37 	struct mount *mp;
38 	char *path;
39 	caddr_t data;
40 	struct nameidata *ndp;
41 	struct proc *p;
42 {
43 	int error = 0;
44 	struct null_args args;
45 	struct vnode *lowerrootvp, *vp;
46 	struct vnode *nullm_rootvp;
47 	struct null_mount *amp;
48 	u_int size;
49 
50 #ifdef NULLFS_DIAGNOSTIC
51 	printf("nullfs_mount(mp = %x)\n", mp);
52 #endif
53 
54 	/*
55 	 * Update is a no-op
56 	 */
57 	if (mp->mnt_flag & MNT_UPDATE) {
58 		return (EOPNOTSUPP);
59 		/* return VFS_MOUNT(MOUNTTONULLMOUNT(mp)->nullm_vfs, path, data, ndp, p);*/
60 	}
61 
62 	/*
63 	 * Get argument
64 	 */
65 	if (error = copyin(data, (caddr_t)&args, sizeof(struct null_args)))
66 		return (error);
67 
68 	/*
69 	 * Find lower node
70 	 */
71 	NDINIT(ndp, LOOKUP, FOLLOW|WANTPARENT|LOCKLEAF,
72 		UIO_USERSPACE, args.target, p);
73 	if (error = namei(ndp))
74 		return (error);
75 
76 	/*
77 	 * Sanity check on lower vnode
78 	 */
79 	lowerrootvp = ndp->ni_vp;
80 #ifdef NULLFS_DIAGNOSTIC
81 	printf("vp = %x, check for VDIR...\n", lowerrootvp);
82 #endif
83 	vrele(ndp->ni_dvp);
84 	ndp->ni_dvp = 0;
85 
86 	/*
87 	 * NEEDSWORK: Is this check really necessary, or just not
88 	 * the way it's been?
89 	 */
90 	if (lowerrootvp->v_type != VDIR) {
91 		vput(lowerrootvp);
92 		return (EINVAL);
93 	}
94 
95 #ifdef NULLFS_DIAGNOSTIC
96 	printf("mp = %x\n", mp);
97 #endif
98 
99 	amp = (struct null_mount *) malloc(sizeof(struct null_mount),
100 				M_UFSMNT, M_WAITOK);	/* XXX */
101 
102 	/*
103 	 * Save reference to underlying lower FS
104 	 */
105 	amp->nullm_vfs = lowerrootvp->v_mount;
106 
107 	/*
108 	 * Save reference.  Each mount also holds
109 	 * a reference on the root vnode.
110 	 */
111 	error = null_node_create(mp, lowerrootvp, &vp);
112 	/*
113 	 * Unlock the node (either the lower or the alias)
114 	 */
115 	VOP_UNLOCK(vp);
116 	/*
117 	 * Make sure the node alias worked
118 	 */
119 	if (error) {
120 		vrele(lowerrootvp);
121 		free(amp, M_UFSMNT);	/* XXX */
122 		return (error);
123 	}
124 
125 	/*
126 	 * Keep a held reference to the root vnode.
127 	 * It is vrele'd in nullfs_unmount.
128 	 */
129 	nullm_rootvp = vp;
130 	nullm_rootvp->v_flag |= VROOT;
131 	amp->nullm_rootvp = nullm_rootvp;
132 	if (NULLVPTOLOWERVP(nullm_rootvp)->v_mount->mnt_flag & MNT_LOCAL)
133 		mp->mnt_flag |= MNT_LOCAL;
134 	mp->mnt_data = (qaddr_t) amp;
135 	getnewfsid(mp, MOUNT_LOFS);
136 
137 	(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
138 	bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
139 	(void) copyinstr(args.target, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
140 	    &size);
141 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
142 #ifdef NULLFS_DIAGNOSTIC
143 	printf("nullfs_mount: lower %s, alias at %s\n",
144 		mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
145 #endif
146 	return (0);
147 }
148 
149 /*
150  * VFS start.  Nothing needed here - the start routine
151  * on the underlying filesystem will have been called
152  * when that filesystem was mounted.
153  */
154 int
155 nullfs_start(mp, flags, p)
156 	struct mount *mp;
157 	int flags;
158 	struct proc *p;
159 {
160 	return (0);
161 	/* return VFS_START(MOUNTTONULLMOUNT(mp)->nullm_vfs, flags, p); */
162 }
163 
164 /*
165  * Free reference to null layer
166  */
167 int
168 nullfs_unmount(mp, mntflags, p)
169 	struct mount *mp;
170 	int mntflags;
171 	struct proc *p;
172 {
173 	struct vnode *nullm_rootvp = MOUNTTONULLMOUNT(mp)->nullm_rootvp;
174 	int error;
175 	int flags = 0;
176 	extern int doforce;
177 
178 #ifdef NULLFS_DIAGNOSTIC
179 	printf("nullfs_unmount(mp = %x)\n", mp);
180 #endif
181 
182 	if (mntflags & MNT_FORCE) {
183 		/* lofs can never be rootfs so don't check for it */
184 		if (!doforce)
185 			return (EINVAL);
186 		flags |= FORCECLOSE;
187 	}
188 
189 	/*
190 	 * Clear out buffer cache.  I don't think we
191 	 * ever get anything cached at this level at the
192 	 * moment, but who knows...
193 	 */
194 #if 0
195 	mntflushbuf(mp, 0);
196 	if (mntinvalbuf(mp, 1))
197 		return (EBUSY);
198 #endif
199 	if (nullm_rootvp->v_usecount > 1)
200 		return (EBUSY);
201 	if (error = vflush(mp, nullm_rootvp, flags))
202 		return (error);
203 
204 #ifdef NULLFS_DIAGNOSTIC
205 	/*
206 	 * Flush any remaining vnode references
207 	 */
208 	null_node_flushmp (mp);
209 #endif
210 
211 #ifdef NULLFS_DIAGNOSTIC
212 	vprint("alias root of lower", nullm_rootvp);
213 #endif
214 	/*
215 	 * Release reference on underlying root vnode
216 	 */
217 	vrele(nullm_rootvp);
218 	/*
219 	 * And blow it away for future re-use
220 	 */
221 	vgone(nullm_rootvp);
222 	/*
223 	 * Finally, throw away the null_mount structure
224 	 */
225 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
226 	mp->mnt_data = 0;
227 	return 0;
228 }
229 
230 int
231 nullfs_root(mp, vpp)
232 	struct mount *mp;
233 	struct vnode **vpp;
234 {
235 	struct vnode *vp;
236 
237 #ifdef NULLFS_DIAGNOSTIC
238 	printf("nullfs_root(mp = %x, vp = %x->%x)\n", mp,
239 			MOUNTTONULLMOUNT(mp)->nullm_rootvp,
240 			NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp)
241 			);
242 #endif
243 
244 	/*
245 	 * Return locked reference to root.
246 	 */
247 	vp = MOUNTTONULLMOUNT(mp)->nullm_rootvp;
248 	VREF(vp);
249 	VOP_LOCK(vp);
250 	*vpp = vp;
251 	return 0;
252 }
253 
254 int
255 nullfs_quotactl(mp, cmd, uid, arg, p)
256 	struct mount *mp;
257 	int cmd;
258 	uid_t uid;
259 	caddr_t arg;
260 	struct proc *p;
261 {
262 	return VFS_QUOTACTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, uid, arg, p);
263 }
264 
265 int
266 nullfs_statfs(mp, sbp, p)
267 	struct mount *mp;
268 	struct statfs *sbp;
269 	struct proc *p;
270 {
271 	int error;
272 	struct statfs mstat;
273 
274 #ifdef NULLFS_DIAGNOSTIC
275 	printf("nullfs_statfs(mp = %x, vp = %x->%x)\n", mp,
276 			MOUNTTONULLMOUNT(mp)->nullm_rootvp,
277 			NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp)
278 			);
279 #endif
280 
281 	bzero(&mstat, sizeof(mstat));
282 
283 	error = VFS_STATFS(MOUNTTONULLMOUNT(mp)->nullm_vfs, &mstat, p);
284 	if (error)
285 		return (error);
286 
287 	/* now copy across the "interesting" information and fake the rest */
288 	sbp->f_type = mstat.f_type;
289 	sbp->f_flags = mstat.f_flags;
290 	sbp->f_bsize = mstat.f_bsize;
291 	sbp->f_iosize = mstat.f_iosize;
292 	sbp->f_blocks = mstat.f_blocks;
293 	sbp->f_bfree = mstat.f_bfree;
294 	sbp->f_bavail = mstat.f_bavail;
295 	sbp->f_files = mstat.f_files;
296 	sbp->f_ffree = mstat.f_ffree;
297 	if (sbp != &mp->mnt_stat) {
298 		bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
299 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
300 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
301 	}
302 	return (0);
303 }
304 
305 int
306 nullfs_sync(mp, waitfor, cred, p)
307 	struct mount *mp;
308 	int waitfor;
309 	struct ucred *cred;
310 	struct proc *p;
311 {
312 	/*
313 	 * XXX - Assumes no data cached at null layer.
314 	 */
315 	return (0);
316 }
317 
318 int
319 nullfs_vget(mp, ino, vpp)
320 	struct mount *mp;
321 	ino_t ino;
322 	struct vnode **vpp;
323 {
324 
325 	return VFS_VGET(MOUNTTONULLMOUNT(mp)->nullm_vfs, ino, vpp);
326 }
327 
328 int
329 nullfs_fhtovp(mp, fidp, nam, vpp, exflagsp, credanonp)
330 	struct mount *mp;
331 	struct fid *fidp;
332 	struct mbuf *nam;
333 	struct vnode **vpp;
334 	int *exflagsp;
335 	struct ucred**credanonp;
336 {
337 
338 	return VFS_FHTOVP(MOUNTTONULLMOUNT(mp)->nullm_vfs, fidp, nam, vpp, exflagsp,credanonp);
339 }
340 
341 int
342 nullfs_vptofh(vp, fhp)
343 	struct vnode *vp;
344 	struct fid *fhp;
345 {
346 	return VFS_VPTOFH(NULLVPTOLOWERVP(vp), fhp);
347 }
348 
349 int nullfs_init __P((void));
350 
351 struct vfsops null_vfsops = {
352 	nullfs_mount,
353 	nullfs_start,
354 	nullfs_unmount,
355 	nullfs_root,
356 	nullfs_quotactl,
357 	nullfs_statfs,
358 	nullfs_sync,
359 	nullfs_vget,
360 	nullfs_fhtovp,
361 	nullfs_vptofh,
362 	nullfs_init,
363 };
364