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  *	@(#)null_vfsops.c	8.1 (Berkeley) 06/10/93
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 <miscfs/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 *xmp;
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 
81 	vrele(ndp->ni_dvp);
82 	ndp->ni_dvp = NULL;
83 
84 	xmp = (struct null_mount *) malloc(sizeof(struct null_mount),
85 				M_UFSMNT, M_WAITOK);	/* XXX */
86 
87 	/*
88 	 * Save reference to underlying FS
89 	 */
90 	xmp->nullm_vfs = lowerrootvp->v_mount;
91 
92 	/*
93 	 * Save reference.  Each mount also holds
94 	 * a reference on the root vnode.
95 	 */
96 	error = null_node_create(mp, lowerrootvp, &vp);
97 	/*
98 	 * Unlock the node (either the lower or the alias)
99 	 */
100 	VOP_UNLOCK(vp);
101 	/*
102 	 * Make sure the node alias worked
103 	 */
104 	if (error) {
105 		vrele(lowerrootvp);
106 		free(xmp, M_UFSMNT);	/* XXX */
107 		return (error);
108 	}
109 
110 	/*
111 	 * Keep a held reference to the root vnode.
112 	 * It is vrele'd in nullfs_unmount.
113 	 */
114 	nullm_rootvp = vp;
115 	nullm_rootvp->v_flag |= VROOT;
116 	xmp->nullm_rootvp = nullm_rootvp;
117 	if (NULLVPTOLOWERVP(nullm_rootvp)->v_mount->mnt_flag & MNT_LOCAL)
118 		mp->mnt_flag |= MNT_LOCAL;
119 	mp->mnt_data = (qaddr_t) xmp;
120 	getnewfsid(mp, MOUNT_LOFS);
121 
122 	(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
123 	bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
124 	(void) copyinstr(args.target, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
125 	    &size);
126 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
127 #ifdef NULLFS_DIAGNOSTIC
128 	printf("nullfs_mount: lower %s, alias at %s\n",
129 		mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
130 #endif
131 	return (0);
132 }
133 
134 /*
135  * VFS start.  Nothing needed here - the start routine
136  * on the underlying filesystem will have been called
137  * when that filesystem was mounted.
138  */
139 int
140 nullfs_start(mp, flags, p)
141 	struct mount *mp;
142 	int flags;
143 	struct proc *p;
144 {
145 	return (0);
146 	/* return VFS_START(MOUNTTONULLMOUNT(mp)->nullm_vfs, flags, p); */
147 }
148 
149 /*
150  * Free reference to null layer
151  */
152 int
153 nullfs_unmount(mp, mntflags, p)
154 	struct mount *mp;
155 	int mntflags;
156 	struct proc *p;
157 {
158 	struct vnode *nullm_rootvp = MOUNTTONULLMOUNT(mp)->nullm_rootvp;
159 	int error;
160 	int flags = 0;
161 	extern int doforce;
162 
163 #ifdef NULLFS_DIAGNOSTIC
164 	printf("nullfs_unmount(mp = %x)\n", mp);
165 #endif
166 
167 	if (mntflags & MNT_FORCE) {
168 		/* lofs can never be rootfs so don't check for it */
169 		if (!doforce)
170 			return (EINVAL);
171 		flags |= FORCECLOSE;
172 	}
173 
174 	/*
175 	 * Clear out buffer cache.  I don't think we
176 	 * ever get anything cached at this level at the
177 	 * moment, but who knows...
178 	 */
179 #if 0
180 	mntflushbuf(mp, 0);
181 	if (mntinvalbuf(mp, 1))
182 		return (EBUSY);
183 #endif
184 	if (nullm_rootvp->v_usecount > 1)
185 		return (EBUSY);
186 	if (error = vflush(mp, nullm_rootvp, flags))
187 		return (error);
188 
189 #ifdef NULLFS_DIAGNOSTIC
190 	vprint("alias root of lower", nullm_rootvp);
191 #endif
192 	/*
193 	 * Release reference on underlying root vnode
194 	 */
195 	vrele(nullm_rootvp);
196 	/*
197 	 * And blow it away for future re-use
198 	 */
199 	vgone(nullm_rootvp);
200 	/*
201 	 * Finally, throw away the null_mount structure
202 	 */
203 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
204 	mp->mnt_data = 0;
205 	return 0;
206 }
207 
208 int
209 nullfs_root(mp, vpp)
210 	struct mount *mp;
211 	struct vnode **vpp;
212 {
213 	struct vnode *vp;
214 
215 #ifdef NULLFS_DIAGNOSTIC
216 	printf("nullfs_root(mp = %x, vp = %x->%x)\n", mp,
217 			MOUNTTONULLMOUNT(mp)->nullm_rootvp,
218 			NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp)
219 			);
220 #endif
221 
222 	/*
223 	 * Return locked reference to root.
224 	 */
225 	vp = MOUNTTONULLMOUNT(mp)->nullm_rootvp;
226 	VREF(vp);
227 	VOP_LOCK(vp);
228 	*vpp = vp;
229 	return 0;
230 }
231 
232 int
233 nullfs_quotactl(mp, cmd, uid, arg, p)
234 	struct mount *mp;
235 	int cmd;
236 	uid_t uid;
237 	caddr_t arg;
238 	struct proc *p;
239 {
240 	return VFS_QUOTACTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, uid, arg, p);
241 }
242 
243 int
244 nullfs_statfs(mp, sbp, p)
245 	struct mount *mp;
246 	struct statfs *sbp;
247 	struct proc *p;
248 {
249 	int error;
250 	struct statfs mstat;
251 
252 #ifdef NULLFS_DIAGNOSTIC
253 	printf("nullfs_statfs(mp = %x, vp = %x->%x)\n", mp,
254 			MOUNTTONULLMOUNT(mp)->nullm_rootvp,
255 			NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp)
256 			);
257 #endif
258 
259 	bzero(&mstat, sizeof(mstat));
260 
261 	error = VFS_STATFS(MOUNTTONULLMOUNT(mp)->nullm_vfs, &mstat, p);
262 	if (error)
263 		return (error);
264 
265 	/* now copy across the "interesting" information and fake the rest */
266 	sbp->f_type = mstat.f_type;
267 	sbp->f_flags = mstat.f_flags;
268 	sbp->f_bsize = mstat.f_bsize;
269 	sbp->f_iosize = mstat.f_iosize;
270 	sbp->f_blocks = mstat.f_blocks;
271 	sbp->f_bfree = mstat.f_bfree;
272 	sbp->f_bavail = mstat.f_bavail;
273 	sbp->f_files = mstat.f_files;
274 	sbp->f_ffree = mstat.f_ffree;
275 	if (sbp != &mp->mnt_stat) {
276 		bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
277 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
278 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
279 	}
280 	return (0);
281 }
282 
283 int
284 nullfs_sync(mp, waitfor, cred, p)
285 	struct mount *mp;
286 	int waitfor;
287 	struct ucred *cred;
288 	struct proc *p;
289 {
290 	/*
291 	 * XXX - Assumes no data cached at null layer.
292 	 */
293 	return (0);
294 }
295 
296 int
297 nullfs_vget(mp, ino, vpp)
298 	struct mount *mp;
299 	ino_t ino;
300 	struct vnode **vpp;
301 {
302 
303 	return VFS_VGET(MOUNTTONULLMOUNT(mp)->nullm_vfs, ino, vpp);
304 }
305 
306 int
307 nullfs_fhtovp(mp, fidp, nam, vpp, exflagsp, credanonp)
308 	struct mount *mp;
309 	struct fid *fidp;
310 	struct mbuf *nam;
311 	struct vnode **vpp;
312 	int *exflagsp;
313 	struct ucred**credanonp;
314 {
315 
316 	return VFS_FHTOVP(MOUNTTONULLMOUNT(mp)->nullm_vfs, fidp, nam, vpp, exflagsp,credanonp);
317 }
318 
319 int
320 nullfs_vptofh(vp, fhp)
321 	struct vnode *vp;
322 	struct fid *fhp;
323 {
324 	return VFS_VPTOFH(NULLVPTOLOWERVP(vp), fhp);
325 }
326 
327 int nullfs_init __P((void));
328 
329 struct vfsops null_vfsops = {
330 	nullfs_mount,
331 	nullfs_start,
332 	nullfs_unmount,
333 	nullfs_root,
334 	nullfs_quotactl,
335 	nullfs_statfs,
336 	nullfs_sync,
337 	nullfs_vget,
338 	nullfs_fhtovp,
339 	nullfs_vptofh,
340 	nullfs_init,
341 };
342