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