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.3 (Berkeley) 07/07/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 <lofs/lofs.h>
31 
32 /*
33  * Mount null layer
34  */
35 nullfs_mount(mp, path, data, ndp, p)
36 	struct mount *mp;
37 	char *path;
38 	caddr_t data;
39 	struct nameidata *ndp;
40 	struct proc *p;
41 {
42 	USES_VOP_UNLOCK;
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 target 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 target 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 really bad, 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 target 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 = make_null_node(mp, lowerrootvp, &vp);
112 	/*
113 	 * Unlock the node (either the target 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 (NULLTOLOWERVP(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: target %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 nullfs_start(mp, flags, p)
155 	struct mount *mp;
156 	int flags;
157 	struct proc *p;
158 {
159 	return (0);
160 	/* return VFS_START(MOUNTTONULLMOUNT(mp)->nullm_vfs, flags, p); */
161 }
162 
163 /*
164  * Free reference to null layer
165  */
166 nullfs_unmount(mp, mntflags, p)
167 	struct mount *mp;
168 	int mntflags;
169 	struct proc *p;
170 {
171 	struct vnode *nullm_rootvp = MOUNTTONULLMOUNT(mp)->nullm_rootvp;
172 	int error;
173 	int flags = 0;
174 	extern int doforce;
175 
176 #ifdef NULLFS_DIAGNOSTIC
177 	printf("nullfs_unmount(mp = %x)\n", mp);
178 #endif
179 
180 	if (mntflags & MNT_FORCE) {
181 		/* lofs can never be rootfs so don't check for it */
182 		if (!doforce)
183 			return (EINVAL);
184 		flags |= FORCECLOSE;
185 	}
186 
187 	/*
188 	 * Clear out buffer cache.  I don't think we
189 	 * ever get anything cached at this level at the
190 	 * moment, but who knows...
191 	 */
192 	mntflushbuf(mp, 0);
193 	if (mntinvalbuf(mp, 1))
194 		return (EBUSY);
195 	if (nullm_rootvp->v_usecount > 1)
196 		return (EBUSY);
197 	if (error = vflush(mp, nullm_rootvp, flags))
198 		return (error);
199 
200 #ifdef NULLFS_DIAGNOSTIC
201 	/*
202 	 * Flush any remaining vnode references
203 	 */
204 	null_node_flushmp (mp);
205 #endif
206 
207 #ifdef NULLFS_DIAGNOSTIC
208 	vprint("alias root of target", nullm_rootvp);
209 #endif
210 	/*
211 	 * Release reference on underlying root vnode
212 	 */
213 	vrele(nullm_rootvp);
214 	/*
215 	 * And blow it away for future re-use
216 	 */
217 	vgone(nullm_rootvp);
218 	/*
219 	 * Finally, throw away the null_mount structure
220 	 */
221 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
222 	mp->mnt_data = 0;
223 	return 0;
224 }
225 
226 nullfs_root(mp, vpp)
227 	struct mount *mp;
228 	struct vnode **vpp;
229 {
230 	USES_VOP_LOCK;
231 	struct vnode *vp;
232 
233 #ifdef NULLFS_DIAGNOSTIC
234 	printf("nullfs_root(mp = %x, vp = %x->%x)\n", mp,
235 			MOUNTTONULLMOUNT(mp)->nullm_rootvp,
236 			NULLTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp)
237 			);
238 #endif
239 
240 	/*
241 	 * Return locked reference to root.
242 	 */
243 	vp = MOUNTTONULLMOUNT(mp)->nullm_rootvp;
244 	VREF(vp);
245 	VOP_LOCK(vp);
246 	*vpp = vp;
247 	return 0;
248 }
249 
250 nullfs_quotactl(mp, cmd, uid, arg, p)
251 	struct mount *mp;
252 	int cmd;
253 	uid_t uid;
254 	caddr_t arg;
255 	struct proc *p;
256 {
257 	return VFS_QUOTACTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, uid, arg, p);
258 }
259 
260 nullfs_statfs(mp, sbp, p)
261 	struct mount *mp;
262 	struct statfs *sbp;
263 	struct proc *p;
264 {
265 	int error;
266 	struct statfs mstat;
267 
268 #ifdef NULLFS_DIAGNOSTIC
269 	printf("nullfs_statfs(mp = %x, vp = %x->%x)\n", mp,
270 			MOUNTTONULLMOUNT(mp)->nullm_rootvp,
271 			NULLTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp)
272 			);
273 #endif
274 
275 	bzero(&mstat, sizeof(mstat));
276 
277 	error = VFS_STATFS(MOUNTTONULLMOUNT(mp)->nullm_vfs, &mstat, p);
278 	if (error)
279 		return (error);
280 
281 	/* now copy across the "interesting" information and fake the rest */
282 	sbp->f_type = mstat.f_type;
283 	sbp->f_flags = mstat.f_flags;
284 	sbp->f_bsize = mstat.f_bsize;
285 	sbp->f_iosize = mstat.f_iosize;
286 	sbp->f_blocks = mstat.f_blocks;
287 	sbp->f_bfree = mstat.f_bfree;
288 	sbp->f_bavail = mstat.f_bavail;
289 	sbp->f_files = mstat.f_files;
290 	sbp->f_ffree = mstat.f_ffree;
291 	if (sbp != &mp->mnt_stat) {
292 		bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
293 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
294 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
295 	}
296 	return (0);
297 }
298 
299 nullfs_sync(mp, waitfor)
300 struct mount *mp;
301 int waitfor;
302 {
303 	/*
304 	 * NEEDSWORK: Assumes no data cached at null layer.
305 	 * Is this true?
306 	 */
307 	return (0);
308 }
309 
310 nullfs_fhtovp(mp, fhp, setgen, vpp)
311 	struct mount *mp;
312 	struct fid *fhp;
313 	int setgen;
314 	struct vnode **vpp;
315 {
316 	return VFS_FHTOVP(MOUNTTONULLMOUNT(mp)->nullm_vfs, fhp, setgen, vpp);
317 }
318 
319 nullfs_vptofh(vp, fhp)
320 	struct vnode *vp;
321 	struct fid *fhp;
322 {
323 	return VFS_VPTOFH(NULLTOLOWERVP(vp), fhp);
324 }
325 
326 int nullfs_init __P((void));
327 
328 struct vfsops null_vfsops = {
329 	nullfs_mount,
330 	nullfs_start,
331 	nullfs_unmount,
332 	nullfs_root,
333 	nullfs_quotactl,
334 	nullfs_statfs,
335 	nullfs_sync,
336 	nullfs_fhtovp,
337 	nullfs_vptofh,
338 	nullfs_init,
339 };
340