null_vfsops.c revision 22521
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 * 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 * $FreeBSD: head/sys/fs/nullfs/null_vfsops.c 22521 1997-02-10 02:22:35Z dyson $
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/proc.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
58extern int	nullfs_init __P((struct vfsconf *));
59
60static int	nullfs_fhtovp __P((struct mount *mp, struct fid *fidp,
61				   struct mbuf *nam, struct vnode **vpp,
62				   int *exflagsp, struct ucred **credanonp));
63static int	nullfs_mount __P((struct mount *mp, char *path, caddr_t data,
64				  struct nameidata *ndp, struct proc *p));
65static int	nullfs_quotactl __P((struct mount *mp, int cmd, uid_t uid,
66				     caddr_t arg, struct proc *p));
67static int	nullfs_root __P((struct mount *mp, struct vnode **vpp));
68static int	nullfs_start __P((struct mount *mp, int flags, struct proc *p));
69static int	nullfs_statfs __P((struct mount *mp, struct statfs *sbp,
70				   struct proc *p));
71static int	nullfs_sync __P((struct mount *mp, int waitfor,
72				 struct ucred *cred, struct proc *p));
73static int	nullfs_unmount __P((struct mount *mp, int mntflags,
74				    struct proc *p));
75static int	nullfs_vget __P((struct mount *mp, ino_t ino,
76				 struct vnode **vpp));
77static int	nullfs_vptofh __P((struct vnode *vp, struct fid *fhp));
78
79/*
80 * Mount null layer
81 */
82static int
83nullfs_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, 0, p);
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	vfs_getnewfsid(mp);
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 */
188static int
189nullfs_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 */
201static int
202nullfs_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		flags |= FORCECLOSE;
217
218	/*
219	 * Clear out buffer cache.  I don't think we
220	 * ever get anything cached at this level at the
221	 * moment, but who knows...
222	 */
223#if 0
224	mntflushbuf(mp, 0);
225	if (mntinvalbuf(mp, 1))
226		return (EBUSY);
227#endif
228	if (nullm_rootvp->v_usecount > 1)
229		return (EBUSY);
230	error = vflush(mp, nullm_rootvp, flags);
231	if (error)
232		return (error);
233
234#ifdef NULLFS_DIAGNOSTIC
235	vprint("alias root of lower", nullm_rootvp);
236#endif
237	/*
238	 * Release reference on underlying root vnode
239	 */
240	vrele(nullm_rootvp);
241	/*
242	 * And blow it away for future re-use
243	 */
244	vgone(nullm_rootvp);
245	/*
246	 * Finally, throw away the null_mount structure
247	 */
248	free(mp->mnt_data, M_UFSMNT);	/* XXX */
249	mp->mnt_data = 0;
250	return 0;
251}
252
253static int
254nullfs_root(mp, vpp)
255	struct mount *mp;
256	struct vnode **vpp;
257{
258	struct proc *p = curproc;	/* XXX */
259	struct vnode *vp;
260
261#ifdef NULLFS_DIAGNOSTIC
262	printf("nullfs_root(mp = %x, vp = %x->%x)\n", mp,
263			MOUNTTONULLMOUNT(mp)->nullm_rootvp,
264			NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp)
265			);
266#endif
267
268	/*
269	 * Return locked reference to root.
270	 */
271	vp = MOUNTTONULLMOUNT(mp)->nullm_rootvp;
272	VREF(vp);
273	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
274	*vpp = vp;
275	return 0;
276}
277
278static int
279nullfs_quotactl(mp, cmd, uid, arg, p)
280	struct mount *mp;
281	int cmd;
282	uid_t uid;
283	caddr_t arg;
284	struct proc *p;
285{
286	return VFS_QUOTACTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, uid, arg, p);
287}
288
289static int
290nullfs_statfs(mp, sbp, p)
291	struct mount *mp;
292	struct statfs *sbp;
293	struct proc *p;
294{
295	int error;
296	struct statfs mstat;
297
298#ifdef NULLFS_DIAGNOSTIC
299	printf("nullfs_statfs(mp = %x, vp = %x->%x)\n", mp,
300			MOUNTTONULLMOUNT(mp)->nullm_rootvp,
301			NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp)
302			);
303#endif
304
305	bzero(&mstat, sizeof(mstat));
306
307	error = VFS_STATFS(MOUNTTONULLMOUNT(mp)->nullm_vfs, &mstat, p);
308	if (error)
309		return (error);
310
311	/* now copy across the "interesting" information and fake the rest */
312	sbp->f_type = mstat.f_type;
313	sbp->f_flags = mstat.f_flags;
314	sbp->f_bsize = mstat.f_bsize;
315	sbp->f_iosize = mstat.f_iosize;
316	sbp->f_blocks = mstat.f_blocks;
317	sbp->f_bfree = mstat.f_bfree;
318	sbp->f_bavail = mstat.f_bavail;
319	sbp->f_files = mstat.f_files;
320	sbp->f_ffree = mstat.f_ffree;
321	if (sbp != &mp->mnt_stat) {
322		bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
323		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
324		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
325	}
326	return (0);
327}
328
329static int
330nullfs_sync(mp, waitfor, cred, p)
331	struct mount *mp;
332	int waitfor;
333	struct ucred *cred;
334	struct proc *p;
335{
336	/*
337	 * XXX - Assumes no data cached at null layer.
338	 */
339	return (0);
340}
341
342static int
343nullfs_vget(mp, ino, vpp)
344	struct mount *mp;
345	ino_t ino;
346	struct vnode **vpp;
347{
348
349	return VFS_VGET(MOUNTTONULLMOUNT(mp)->nullm_vfs, ino, vpp);
350}
351
352static int
353nullfs_fhtovp(mp, fidp, nam, vpp, exflagsp, credanonp)
354	struct mount *mp;
355	struct fid *fidp;
356	struct mbuf *nam;
357	struct vnode **vpp;
358	int *exflagsp;
359	struct ucred**credanonp;
360{
361
362	return VFS_FHTOVP(MOUNTTONULLMOUNT(mp)->nullm_vfs, fidp, nam, vpp, exflagsp,credanonp);
363}
364
365static int
366nullfs_vptofh(vp, fhp)
367	struct vnode *vp;
368	struct fid *fhp;
369{
370	return VFS_VPTOFH(NULLVPTOLOWERVP(vp), fhp);
371}
372
373static struct vfsops null_vfsops = {
374	nullfs_mount,
375	nullfs_start,
376	nullfs_unmount,
377	nullfs_root,
378	nullfs_quotactl,
379	nullfs_statfs,
380	nullfs_sync,
381	nullfs_vget,
382	nullfs_fhtovp,
383	nullfs_vptofh,
384	nullfs_init,
385};
386
387VFS_SET(null_vfsops, null, MOUNT_NULL, VFCF_LOOPBACK);
388