xref: /freebsd/sys/fs/nullfs/null_vfsops.c (revision 5ebc7e6281887681c3a348a5a4c902e262ccd656)
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.5 1995/03/16 18:13:31 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 /*
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 	error = copyin(data, (caddr_t)&args, sizeof(struct null_args));
92 	if (error)
93 		return (error);
94 
95 	/*
96 	 * Find lower node
97 	 */
98 	NDINIT(ndp, LOOKUP, FOLLOW|WANTPARENT|LOCKLEAF,
99 		UIO_USERSPACE, args.target, p);
100 	error = namei(ndp);
101 	if (error)
102 		return (error);
103 
104 	/*
105 	 * Sanity check on lower vnode
106 	 */
107 	lowerrootvp = ndp->ni_vp;
108 
109 	vrele(ndp->ni_dvp);
110 	ndp->ni_dvp = NULL;
111 
112 	xmp = (struct null_mount *) malloc(sizeof(struct null_mount),
113 				M_UFSMNT, M_WAITOK);	/* XXX */
114 
115 	/*
116 	 * Save reference to underlying FS
117 	 */
118 	xmp->nullm_vfs = lowerrootvp->v_mount;
119 
120 	/*
121 	 * Save reference.  Each mount also holds
122 	 * a reference on the root vnode.
123 	 */
124 	error = null_node_create(mp, lowerrootvp, &vp);
125 	/*
126 	 * Unlock the node (either the lower or the alias)
127 	 */
128 	VOP_UNLOCK(vp);
129 	/*
130 	 * Make sure the node alias worked
131 	 */
132 	if (error) {
133 		vrele(lowerrootvp);
134 		free(xmp, M_UFSMNT);	/* XXX */
135 		return (error);
136 	}
137 
138 	/*
139 	 * Keep a held reference to the root vnode.
140 	 * It is vrele'd in nullfs_unmount.
141 	 */
142 	nullm_rootvp = vp;
143 	nullm_rootvp->v_flag |= VROOT;
144 	xmp->nullm_rootvp = nullm_rootvp;
145 	if (NULLVPTOLOWERVP(nullm_rootvp)->v_mount->mnt_flag & MNT_LOCAL)
146 		mp->mnt_flag |= MNT_LOCAL;
147 	mp->mnt_data = (qaddr_t) xmp;
148 	getnewfsid(mp, MOUNT_LOFS);
149 
150 	(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
151 	bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
152 	(void) copyinstr(args.target, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
153 	    &size);
154 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
155 #ifdef NULLFS_DIAGNOSTIC
156 	printf("nullfs_mount: lower %s, alias at %s\n",
157 		mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
158 #endif
159 	return (0);
160 }
161 
162 /*
163  * VFS start.  Nothing needed here - the start routine
164  * on the underlying filesystem will have been called
165  * when that filesystem was mounted.
166  */
167 int
168 nullfs_start(mp, flags, p)
169 	struct mount *mp;
170 	int flags;
171 	struct proc *p;
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 
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 	error = vflush(mp, nullm_rootvp, flags);
214 	if (error)
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 	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 	return (0);
309 }
310 
311 int
312 nullfs_sync(mp, waitfor, cred, p)
313 	struct mount *mp;
314 	int waitfor;
315 	struct ucred *cred;
316 	struct proc *p;
317 {
318 	/*
319 	 * XXX - Assumes no data cached at null layer.
320 	 */
321 	return (0);
322 }
323 
324 int
325 nullfs_vget(mp, ino, vpp)
326 	struct mount *mp;
327 	ino_t ino;
328 	struct vnode **vpp;
329 {
330 
331 	return VFS_VGET(MOUNTTONULLMOUNT(mp)->nullm_vfs, ino, vpp);
332 }
333 
334 int
335 nullfs_fhtovp(mp, fidp, nam, vpp, exflagsp, credanonp)
336 	struct mount *mp;
337 	struct fid *fidp;
338 	struct mbuf *nam;
339 	struct vnode **vpp;
340 	int *exflagsp;
341 	struct ucred**credanonp;
342 {
343 
344 	return VFS_FHTOVP(MOUNTTONULLMOUNT(mp)->nullm_vfs, fidp, nam, vpp, exflagsp,credanonp);
345 }
346 
347 int
348 nullfs_vptofh(vp, fhp)
349 	struct vnode *vp;
350 	struct fid *fhp;
351 {
352 	return VFS_VPTOFH(NULLVPTOLOWERVP(vp), fhp);
353 }
354 
355 int nullfs_init __P((void));
356 
357 struct vfsops null_vfsops = {
358 	nullfs_mount,
359 	nullfs_start,
360 	nullfs_unmount,
361 	nullfs_root,
362 	nullfs_quotactl,
363 	nullfs_statfs,
364 	nullfs_sync,
365 	nullfs_vget,
366 	nullfs_fhtovp,
367 	nullfs_vptofh,
368 	nullfs_init,
369 };
370 
371 VFS_SET(null_vfsops, null, MOUNT_NULL, VFCF_LOOPBACK);
372