1 /*
2  * Copyright (c) 1992, 1993, 1995
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software donated to Berkeley by
6  * Jan-Simon Pendry.
7  *
8  * %sccs.include.redist.c%
9  *
10  *	@(#)null_vfsops.c	8.7 (Berkeley) 05/14/95
11  *
12  * @(#)lofs_vfsops.c	1.2 (Berkeley) 6/18/92
13  * $Id: lofs_vfsops.c,v 1.9 1992/05/30 10:26:24 jsp Exp jsp $
14  */
15 
16 /*
17  * Null Layer
18  * (See null_vnops.c for a description of what this does.)
19  */
20 
21 #include <sys/param.h>
22 #include <sys/systm.h>
23 #include <sys/proc.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, 0, p);
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 	vfs_getnewfsid(mp);
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 
162 #ifdef NULLFS_DIAGNOSTIC
163 	printf("nullfs_unmount(mp = %x)\n", mp);
164 #endif
165 
166 	if (mntflags & MNT_FORCE)
167 		flags |= FORCECLOSE;
168 
169 	/*
170 	 * Clear out buffer cache.  I don't think we
171 	 * ever get anything cached at this level at the
172 	 * moment, but who knows...
173 	 */
174 #if 0
175 	mntflushbuf(mp, 0);
176 	if (mntinvalbuf(mp, 1))
177 		return (EBUSY);
178 #endif
179 	if (nullm_rootvp->v_usecount > 1)
180 		return (EBUSY);
181 	if (error = vflush(mp, nullm_rootvp, flags))
182 		return (error);
183 
184 #ifdef NULLFS_DIAGNOSTIC
185 	vprint("alias root of lower", nullm_rootvp);
186 #endif
187 	/*
188 	 * Release reference on underlying root vnode
189 	 */
190 	vrele(nullm_rootvp);
191 	/*
192 	 * And blow it away for future re-use
193 	 */
194 	vgone(nullm_rootvp);
195 	/*
196 	 * Finally, throw away the null_mount structure
197 	 */
198 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
199 	mp->mnt_data = 0;
200 	return 0;
201 }
202 
203 int
204 nullfs_root(mp, vpp)
205 	struct mount *mp;
206 	struct vnode **vpp;
207 {
208 	struct proc *p = curproc;	/* XXX */
209 	struct vnode *vp;
210 
211 #ifdef NULLFS_DIAGNOSTIC
212 	printf("nullfs_root(mp = %x, vp = %x->%x)\n", mp,
213 			MOUNTTONULLMOUNT(mp)->nullm_rootvp,
214 			NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp)
215 			);
216 #endif
217 
218 	/*
219 	 * Return locked reference to root.
220 	 */
221 	vp = MOUNTTONULLMOUNT(mp)->nullm_rootvp;
222 	VREF(vp);
223 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
224 	*vpp = vp;
225 	return 0;
226 }
227 
228 int
229 nullfs_quotactl(mp, cmd, uid, arg, p)
230 	struct mount *mp;
231 	int cmd;
232 	uid_t uid;
233 	caddr_t arg;
234 	struct proc *p;
235 {
236 	return VFS_QUOTACTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, uid, arg, p);
237 }
238 
239 int
240 nullfs_statfs(mp, sbp, p)
241 	struct mount *mp;
242 	struct statfs *sbp;
243 	struct proc *p;
244 {
245 	int error;
246 	struct statfs mstat;
247 
248 #ifdef NULLFS_DIAGNOSTIC
249 	printf("nullfs_statfs(mp = %x, vp = %x->%x)\n", mp,
250 			MOUNTTONULLMOUNT(mp)->nullm_rootvp,
251 			NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp)
252 			);
253 #endif
254 
255 	bzero(&mstat, sizeof(mstat));
256 
257 	error = VFS_STATFS(MOUNTTONULLMOUNT(mp)->nullm_vfs, &mstat, p);
258 	if (error)
259 		return (error);
260 
261 	/* now copy across the "interesting" information and fake the rest */
262 	sbp->f_type = mstat.f_type;
263 	sbp->f_flags = mstat.f_flags;
264 	sbp->f_bsize = mstat.f_bsize;
265 	sbp->f_iosize = mstat.f_iosize;
266 	sbp->f_blocks = mstat.f_blocks;
267 	sbp->f_bfree = mstat.f_bfree;
268 	sbp->f_bavail = mstat.f_bavail;
269 	sbp->f_files = mstat.f_files;
270 	sbp->f_ffree = mstat.f_ffree;
271 	if (sbp != &mp->mnt_stat) {
272 		bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
273 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
274 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
275 	}
276 	return (0);
277 }
278 
279 int
280 nullfs_sync(mp, waitfor, cred, p)
281 	struct mount *mp;
282 	int waitfor;
283 	struct ucred *cred;
284 	struct proc *p;
285 {
286 	/*
287 	 * XXX - Assumes no data cached at null layer.
288 	 */
289 	return (0);
290 }
291 
292 int
293 nullfs_vget(mp, ino, vpp)
294 	struct mount *mp;
295 	ino_t ino;
296 	struct vnode **vpp;
297 {
298 
299 	return VFS_VGET(MOUNTTONULLMOUNT(mp)->nullm_vfs, ino, vpp);
300 }
301 
302 int
303 nullfs_fhtovp(mp, fidp, nam, vpp, exflagsp, credanonp)
304 	struct mount *mp;
305 	struct fid *fidp;
306 	struct mbuf *nam;
307 	struct vnode **vpp;
308 	int *exflagsp;
309 	struct ucred**credanonp;
310 {
311 
312 	return VFS_FHTOVP(MOUNTTONULLMOUNT(mp)->nullm_vfs, fidp, nam, vpp, exflagsp,credanonp);
313 }
314 
315 int
316 nullfs_vptofh(vp, fhp)
317 	struct vnode *vp;
318 	struct fid *fhp;
319 {
320 	return VFS_VPTOFH(NULLVPTOLOWERVP(vp), fhp);
321 }
322 
323 int nullfs_init __P((struct vfsconf *));
324 
325 #define nullfs_sysctl ((int (*) __P((int *, u_int, void *, size_t *, void *, \
326 	    size_t, struct proc *)))eopnotsupp)
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_vget,
337 	nullfs_fhtovp,
338 	nullfs_vptofh,
339 	nullfs_init,
340 	nullfs_sysctl,
341 };
342