xref: /netbsd-src/sys/miscfs/nullfs/null_vfsops.c (revision 1f2744e6e4915c9da2a3f980279398c4cf7d5e6d)
1 /*	$NetBSD: null_vfsops.c,v 1.8 1995/03/09 12:05:56 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 	size_t 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 #ifdef NULLFS_DIAGNOSTIC
154 	printf("nullfs_mount: lower %s, alias at %s\n",
155 		mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
156 #endif
157 	return (0);
158 }
159 
160 /*
161  * VFS start.  Nothing needed here - the start routine
162  * on the underlying filesystem will have been called
163  * when that filesystem was mounted.
164  */
165 int
166 nullfs_start(mp, flags, p)
167 	struct mount *mp;
168 	int flags;
169 	struct proc *p;
170 {
171 
172 	return (0);
173 	/* return VFS_START(MOUNTTONULLMOUNT(mp)->nullm_vfs, flags, p); */
174 }
175 
176 /*
177  * Free reference to null layer
178  */
179 int
180 nullfs_unmount(mp, mntflags, p)
181 	struct mount *mp;
182 	int mntflags;
183 	struct proc *p;
184 {
185 	struct vnode *nullm_rootvp = MOUNTTONULLMOUNT(mp)->nullm_rootvp;
186 	int error;
187 	int flags = 0;
188 	extern int doforce;
189 
190 #ifdef NULLFS_DIAGNOSTIC
191 	printf("nullfs_unmount(mp = %x)\n", mp);
192 #endif
193 
194 	if (mntflags & MNT_FORCE) {
195 		/* lofs can never be rootfs so don't check for it */
196 		if (!doforce)
197 			return (EINVAL);
198 		flags |= FORCECLOSE;
199 	}
200 
201 	/*
202 	 * Clear out buffer cache.  I don't think we
203 	 * ever get anything cached at this level at the
204 	 * moment, but who knows...
205 	 */
206 #if 0
207 	mntflushbuf(mp, 0);
208 	if (mntinvalbuf(mp, 1))
209 		return (EBUSY);
210 #endif
211 	if (nullm_rootvp->v_usecount > 1)
212 		return (EBUSY);
213 	if (error = vflush(mp, nullm_rootvp, flags))
214 		return (error);
215 
216 #ifdef NULLFS_DIAGNOSTIC
217 	vprint("alias root of lower", nullm_rootvp);
218 #endif
219 	/*
220 	 * Release reference on underlying root vnode
221 	 */
222 	vrele(nullm_rootvp);
223 	/*
224 	 * And blow it away for future re-use
225 	 */
226 	vgone(nullm_rootvp);
227 	/*
228 	 * Finally, throw away the null_mount structure
229 	 */
230 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
231 	mp->mnt_data = 0;
232 	return 0;
233 }
234 
235 int
236 nullfs_root(mp, vpp)
237 	struct mount *mp;
238 	struct vnode **vpp;
239 {
240 	struct vnode *vp;
241 
242 #ifdef NULLFS_DIAGNOSTIC
243 	printf("nullfs_root(mp = %x, vp = %x->%x)\n", mp,
244 			MOUNTTONULLMOUNT(mp)->nullm_rootvp,
245 			NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp)
246 			);
247 #endif
248 
249 	/*
250 	 * Return locked reference to root.
251 	 */
252 	vp = MOUNTTONULLMOUNT(mp)->nullm_rootvp;
253 	VREF(vp);
254 	VOP_LOCK(vp);
255 	*vpp = vp;
256 	return 0;
257 }
258 
259 int
260 nullfs_quotactl(mp, cmd, uid, arg, p)
261 	struct mount *mp;
262 	int cmd;
263 	uid_t uid;
264 	caddr_t arg;
265 	struct proc *p;
266 {
267 
268 	return VFS_QUOTACTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, uid, arg, p);
269 }
270 
271 int
272 nullfs_statfs(mp, sbp, p)
273 	struct mount *mp;
274 	struct statfs *sbp;
275 	struct proc *p;
276 {
277 	int error;
278 	struct statfs mstat;
279 
280 #ifdef NULLFS_DIAGNOSTIC
281 	printf("nullfs_statfs(mp = %x, vp = %x->%x)\n", mp,
282 			MOUNTTONULLMOUNT(mp)->nullm_rootvp,
283 			NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp)
284 			);
285 #endif
286 
287 	bzero(&mstat, sizeof(mstat));
288 
289 	error = VFS_STATFS(MOUNTTONULLMOUNT(mp)->nullm_vfs, &mstat, p);
290 	if (error)
291 		return (error);
292 
293 	/* now copy across the "interesting" information and fake the rest */
294 	sbp->f_type = mstat.f_type;
295 	sbp->f_flags = mstat.f_flags;
296 	sbp->f_bsize = mstat.f_bsize;
297 	sbp->f_iosize = mstat.f_iosize;
298 	sbp->f_blocks = mstat.f_blocks;
299 	sbp->f_bfree = mstat.f_bfree;
300 	sbp->f_bavail = mstat.f_bavail;
301 	sbp->f_files = mstat.f_files;
302 	sbp->f_ffree = mstat.f_ffree;
303 	if (sbp != &mp->mnt_stat) {
304 		bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
305 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
306 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
307 	}
308 	strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
309 	sbp->f_fstypename[MFSNAMELEN] = '\0';
310 	return (0);
311 }
312 
313 int
314 nullfs_sync(mp, waitfor, cred, p)
315 	struct mount *mp;
316 	int waitfor;
317 	struct ucred *cred;
318 	struct proc *p;
319 {
320 
321 	/*
322 	 * XXX - Assumes no data cached at null layer.
323 	 */
324 	return (0);
325 }
326 
327 int
328 nullfs_vget(mp, ino, vpp)
329 	struct mount *mp;
330 	ino_t ino;
331 	struct vnode **vpp;
332 {
333 
334 	return VFS_VGET(MOUNTTONULLMOUNT(mp)->nullm_vfs, ino, vpp);
335 }
336 
337 int
338 nullfs_fhtovp(mp, fidp, nam, vpp, exflagsp, credanonp)
339 	struct mount *mp;
340 	struct fid *fidp;
341 	struct mbuf *nam;
342 	struct vnode **vpp;
343 	int *exflagsp;
344 	struct ucred**credanonp;
345 {
346 
347 	return (EOPNOTSUPP);
348 }
349 
350 int
351 nullfs_vptofh(vp, fhp)
352 	struct vnode *vp;
353 	struct fid *fhp;
354 {
355 
356 	return (EOPNOTSUPP);
357 }
358 
359 int nullfs_init __P((void));
360 
361 struct vfsops null_vfsops = {
362 	MOUNT_NULL,
363 	nullfs_mount,
364 	nullfs_start,
365 	nullfs_unmount,
366 	nullfs_root,
367 	nullfs_quotactl,
368 	nullfs_statfs,
369 	nullfs_sync,
370 	nullfs_vget,
371 	nullfs_fhtovp,
372 	nullfs_vptofh,
373 	nullfs_init,
374 };
375