xref: /netbsd-src/sys/miscfs/nullfs/null_vfsops.c (revision 6ea46cb5e46c49111a6ecf3bcbe3c7e2730fe9f6)
1 /*	$NetBSD: null_vfsops.c,v 1.3 1994/09/15 03:42:40 mycroft Exp $	*/
2 
3 /*
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software donated to Berkeley by
8  * Jan-Simon Pendry.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	from: Id: lofs_vfsops.c,v 1.9 1992/05/30 10:26:24 jsp Exp
39  *	from: @(#)lofs_vfsops.c	1.2 (Berkeley) 6/18/92
40  *	@(#)null_vfsops.c	8.2 (Berkeley) 1/21/94
41  */
42 
43 /*
44  * Null Layer
45  * (See null_vnops.c for a description of what this does.)
46  */
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/time.h>
51 #include <sys/types.h>
52 #include <sys/vnode.h>
53 #include <sys/mount.h>
54 #include <sys/namei.h>
55 #include <sys/malloc.h>
56 #include <miscfs/nullfs/null.h>
57 
58 /*
59  * Mount null layer
60  */
61 int
62 nullfs_mount(mp, path, data, ndp, p)
63 	struct mount *mp;
64 	char *path;
65 	caddr_t data;
66 	struct nameidata *ndp;
67 	struct proc *p;
68 {
69 	int error = 0;
70 	struct null_args args;
71 	struct vnode *lowerrootvp, *vp;
72 	struct vnode *nullm_rootvp;
73 	struct null_mount *xmp;
74 	u_int size;
75 
76 #ifdef NULLFS_DIAGNOSTIC
77 	printf("nullfs_mount(mp = %x)\n", mp);
78 #endif
79 
80 	/*
81 	 * Update is a no-op
82 	 */
83 	if (mp->mnt_flag & MNT_UPDATE) {
84 		return (EOPNOTSUPP);
85 		/* return VFS_MOUNT(MOUNTTONULLMOUNT(mp)->nullm_vfs, path, data, ndp, p);*/
86 	}
87 
88 	/*
89 	 * Get argument
90 	 */
91 	if (error = copyin(data, (caddr_t)&args, sizeof(struct null_args)))
92 		return (error);
93 
94 	/*
95 	 * Find lower node
96 	 */
97 	NDINIT(ndp, LOOKUP, FOLLOW|WANTPARENT|LOCKLEAF,
98 		UIO_USERSPACE, args.target, p);
99 	if (error = namei(ndp))
100 		return (error);
101 
102 	/*
103 	 * Sanity check on lower vnode
104 	 */
105 	lowerrootvp = ndp->ni_vp;
106 
107 	vrele(ndp->ni_dvp);
108 	ndp->ni_dvp = NULL;
109 
110 	xmp = (struct null_mount *) malloc(sizeof(struct null_mount),
111 				M_UFSMNT, M_WAITOK);	/* XXX */
112 
113 	/*
114 	 * Save reference to underlying FS
115 	 */
116 	xmp->nullm_vfs = lowerrootvp->v_mount;
117 
118 	/*
119 	 * Save reference.  Each mount also holds
120 	 * a reference on the root vnode.
121 	 */
122 	error = null_node_create(mp, lowerrootvp, &vp);
123 	/*
124 	 * Unlock the node (either the lower or the alias)
125 	 */
126 	VOP_UNLOCK(vp);
127 	/*
128 	 * Make sure the node alias worked
129 	 */
130 	if (error) {
131 		vrele(lowerrootvp);
132 		free(xmp, M_UFSMNT);	/* XXX */
133 		return (error);
134 	}
135 
136 	/*
137 	 * Keep a held reference to the root vnode.
138 	 * It is vrele'd in nullfs_unmount.
139 	 */
140 	nullm_rootvp = vp;
141 	nullm_rootvp->v_flag |= VROOT;
142 	xmp->nullm_rootvp = nullm_rootvp;
143 	if (NULLVPTOLOWERVP(nullm_rootvp)->v_mount->mnt_flag & MNT_LOCAL)
144 		mp->mnt_flag |= MNT_LOCAL;
145 	mp->mnt_data = (qaddr_t) xmp;
146 	getnewfsid(mp, makefstype(MOUNT_LOFS));
147 
148 	(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
149 	bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
150 	(void) copyinstr(args.target, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
151 	    &size);
152 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
153 	(void)nullfs_statfs(mp, &mp->mnt_stat, p);
154 #ifdef NULLFS_DIAGNOSTIC
155 	printf("nullfs_mount: lower %s, alias at %s\n",
156 		mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
157 #endif
158 	return (0);
159 }
160 
161 /*
162  * VFS start.  Nothing needed here - the start routine
163  * on the underlying filesystem will have been called
164  * when that filesystem was mounted.
165  */
166 int
167 nullfs_start(mp, flags, p)
168 	struct mount *mp;
169 	int flags;
170 	struct proc *p;
171 {
172 
173 	return (0);
174 	/* return VFS_START(MOUNTTONULLMOUNT(mp)->nullm_vfs, flags, p); */
175 }
176 
177 /*
178  * Free reference to null layer
179  */
180 int
181 nullfs_unmount(mp, mntflags, p)
182 	struct mount *mp;
183 	int mntflags;
184 	struct proc *p;
185 {
186 	struct vnode *nullm_rootvp = MOUNTTONULLMOUNT(mp)->nullm_rootvp;
187 	int error;
188 	int flags = 0;
189 	extern int doforce;
190 
191 #ifdef NULLFS_DIAGNOSTIC
192 	printf("nullfs_unmount(mp = %x)\n", mp);
193 #endif
194 
195 	if (mntflags & MNT_FORCE) {
196 		/* lofs can never be rootfs so don't check for it */
197 		if (!doforce)
198 			return (EINVAL);
199 		flags |= FORCECLOSE;
200 	}
201 
202 	/*
203 	 * Clear out buffer cache.  I don't think we
204 	 * ever get anything cached at this level at the
205 	 * moment, but who knows...
206 	 */
207 #if 0
208 	mntflushbuf(mp, 0);
209 	if (mntinvalbuf(mp, 1))
210 		return (EBUSY);
211 #endif
212 	if (nullm_rootvp->v_usecount > 1)
213 		return (EBUSY);
214 	if (error = vflush(mp, nullm_rootvp, flags))
215 		return (error);
216 
217 #ifdef NULLFS_DIAGNOSTIC
218 	vprint("alias root of lower", nullm_rootvp);
219 #endif
220 	/*
221 	 * Release reference on underlying root vnode
222 	 */
223 	vrele(nullm_rootvp);
224 	/*
225 	 * And blow it away for future re-use
226 	 */
227 	vgone(nullm_rootvp);
228 	/*
229 	 * Finally, throw away the null_mount structure
230 	 */
231 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
232 	mp->mnt_data = 0;
233 	return 0;
234 }
235 
236 int
237 nullfs_root(mp, vpp)
238 	struct mount *mp;
239 	struct vnode **vpp;
240 {
241 	struct vnode *vp;
242 
243 #ifdef NULLFS_DIAGNOSTIC
244 	printf("nullfs_root(mp = %x, vp = %x->%x)\n", mp,
245 			MOUNTTONULLMOUNT(mp)->nullm_rootvp,
246 			NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp)
247 			);
248 #endif
249 
250 	/*
251 	 * Return locked reference to root.
252 	 */
253 	vp = MOUNTTONULLMOUNT(mp)->nullm_rootvp;
254 	VREF(vp);
255 	VOP_LOCK(vp);
256 	*vpp = vp;
257 	return 0;
258 }
259 
260 int
261 nullfs_quotactl(mp, cmd, uid, arg, p)
262 	struct mount *mp;
263 	int cmd;
264 	uid_t uid;
265 	caddr_t arg;
266 	struct proc *p;
267 {
268 
269 	return VFS_QUOTACTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, uid, arg, p);
270 }
271 
272 int
273 nullfs_statfs(mp, sbp, p)
274 	struct mount *mp;
275 	struct statfs *sbp;
276 	struct proc *p;
277 {
278 	int error;
279 	struct statfs mstat;
280 
281 #ifdef NULLFS_DIAGNOSTIC
282 	printf("nullfs_statfs(mp = %x, vp = %x->%x)\n", mp,
283 			MOUNTTONULLMOUNT(mp)->nullm_rootvp,
284 			NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp)
285 			);
286 #endif
287 
288 	bzero(&mstat, sizeof(mstat));
289 
290 	error = VFS_STATFS(MOUNTTONULLMOUNT(mp)->nullm_vfs, &mstat, p);
291 	if (error)
292 		return (error);
293 
294 	/* now copy across the "interesting" information and fake the rest */
295 	sbp->f_type = mstat.f_type;
296 	sbp->f_flags = mstat.f_flags;
297 	sbp->f_bsize = mstat.f_bsize;
298 	sbp->f_iosize = mstat.f_iosize;
299 	sbp->f_blocks = mstat.f_blocks;
300 	sbp->f_bfree = mstat.f_bfree;
301 	sbp->f_bavail = mstat.f_bavail;
302 	sbp->f_files = mstat.f_files;
303 	sbp->f_ffree = mstat.f_ffree;
304 	if (sbp != &mp->mnt_stat) {
305 		bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
306 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
307 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
308 	}
309 	strncpy(&sbp->f_fstypename[0], mp->mnt_op->vfs_name, MFSNAMELEN);
310 	sbp->f_fstypename[MFSNAMELEN] = '\0';
311 	return (0);
312 }
313 
314 int
315 nullfs_sync(mp, waitfor, cred, p)
316 	struct mount *mp;
317 	int waitfor;
318 	struct ucred *cred;
319 	struct proc *p;
320 {
321 
322 	/*
323 	 * XXX - Assumes no data cached at null layer.
324 	 */
325 	return (0);
326 }
327 
328 int
329 nullfs_vget(mp, ino, vpp)
330 	struct mount *mp;
331 	ino_t ino;
332 	struct vnode **vpp;
333 {
334 
335 	return VFS_VGET(MOUNTTONULLMOUNT(mp)->nullm_vfs, ino, vpp);
336 }
337 
338 int
339 nullfs_fhtovp(mp, fidp, nam, vpp, exflagsp, credanonp)
340 	struct mount *mp;
341 	struct fid *fidp;
342 	struct mbuf *nam;
343 	struct vnode **vpp;
344 	int *exflagsp;
345 	struct ucred**credanonp;
346 {
347 
348 	return VFS_FHTOVP(MOUNTTONULLMOUNT(mp)->nullm_vfs, fidp, nam, vpp, exflagsp,credanonp);
349 }
350 
351 int
352 nullfs_vptofh(vp, fhp)
353 	struct vnode *vp;
354 	struct fid *fhp;
355 {
356 
357 	return VFS_VPTOFH(NULLVPTOLOWERVP(vp), fhp);
358 }
359 
360 int nullfs_init __P((void));
361 
362 struct vfsops null_vfsops = {
363 	MOUNT_NULL,
364 	nullfs_mount,
365 	nullfs_start,
366 	nullfs_unmount,
367 	nullfs_root,
368 	nullfs_quotactl,
369 	nullfs_statfs,
370 	nullfs_sync,
371 	nullfs_vget,
372 	nullfs_fhtovp,
373 	nullfs_vptofh,
374 	nullfs_init,
375 };
376