null_vfsops.c revision 24987
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 * $Id: null_vfsops.c,v 1.14 1997/02/22 09:40:21 peter 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/proc.h>
51#include <sys/time.h>
52#include <sys/types.h>
53#include <sys/vnode.h>
54#include <sys/mount.h>
55#include <sys/namei.h>
56#include <sys/malloc.h>
57#include <miscfs/nullfs/null.h>
58
59static int	nullfs_fhtovp __P((struct mount *mp, struct fid *fidp,
60				   struct mbuf *nam, struct vnode **vpp,
61				   int *exflagsp, struct ucred **credanonp));
62static int	nullfs_mount __P((struct mount *mp, char *path, caddr_t data,
63				  struct nameidata *ndp, struct proc *p));
64static int	nullfs_quotactl __P((struct mount *mp, int cmd, uid_t uid,
65				     caddr_t arg, struct proc *p));
66static int	nullfs_root __P((struct mount *mp, struct vnode **vpp));
67static int	nullfs_start __P((struct mount *mp, int flags, struct proc *p));
68static int	nullfs_statfs __P((struct mount *mp, struct statfs *sbp,
69				   struct proc *p));
70static int	nullfs_sync __P((struct mount *mp, int waitfor,
71				 struct ucred *cred, struct proc *p));
72static int	nullfs_unmount __P((struct mount *mp, int mntflags,
73				    struct proc *p));
74static int	nullfs_vget __P((struct mount *mp, ino_t ino,
75				 struct vnode **vpp));
76static int	nullfs_vptofh __P((struct vnode *vp, struct fid *fhp));
77
78/*
79 * Mount null layer
80 */
81static int
82nullfs_mount(mp, path, data, ndp, p)
83	struct mount *mp;
84	char *path;
85	caddr_t data;
86	struct nameidata *ndp;
87	struct proc *p;
88{
89	int error = 0;
90	struct null_args args;
91	struct vnode *lowerrootvp, *vp;
92	struct vnode *nullm_rootvp;
93	struct null_mount *xmp;
94	u_int size;
95
96#ifdef NULLFS_DIAGNOSTIC
97	printf("nullfs_mount(mp = %x)\n", mp);
98#endif
99
100	/*
101	 * Update is a no-op
102	 */
103	if (mp->mnt_flag & MNT_UPDATE) {
104		return (EOPNOTSUPP);
105		/* return VFS_MOUNT(MOUNTTONULLMOUNT(mp)->nullm_vfs, path, data, ndp, p);*/
106	}
107
108	/*
109	 * Get argument
110	 */
111	error = copyin(data, (caddr_t)&args, sizeof(struct null_args));
112	if (error)
113		return (error);
114
115	/*
116	 * Find lower node
117	 */
118	NDINIT(ndp, LOOKUP, FOLLOW|WANTPARENT|LOCKLEAF,
119		UIO_USERSPACE, args.target, p);
120	error = namei(ndp);
121	if (error)
122		return (error);
123
124	/*
125	 * Sanity check on lower vnode
126	 */
127	lowerrootvp = ndp->ni_vp;
128
129	vrele(ndp->ni_dvp);
130	ndp->ni_dvp = NULLVP;
131
132	xmp = (struct null_mount *) malloc(sizeof(struct null_mount),
133				M_UFSMNT, M_WAITOK);	/* XXX */
134
135	/*
136	 * Save reference to underlying FS
137	 */
138	xmp->nullm_vfs = lowerrootvp->v_mount;
139
140	/*
141	 * Save reference.  Each mount also holds
142	 * a reference on the root vnode.
143	 */
144	error = null_node_create(mp, lowerrootvp, &vp);
145	/*
146	 * Unlock the node (either the lower or the alias)
147	 */
148	VOP_UNLOCK(vp, 0, p);
149	/*
150	 * Make sure the node alias worked
151	 */
152	if (error) {
153		vrele(lowerrootvp);
154		free(xmp, M_UFSMNT);	/* XXX */
155		return (error);
156	}
157
158	/*
159	 * Keep a held reference to the root vnode.
160	 * It is vrele'd in nullfs_unmount.
161	 */
162	nullm_rootvp = vp;
163	nullm_rootvp->v_flag |= VROOT;
164	xmp->nullm_rootvp = nullm_rootvp;
165	if (NULLVPTOLOWERVP(nullm_rootvp)->v_mount->mnt_flag & MNT_LOCAL)
166		mp->mnt_flag |= MNT_LOCAL;
167	mp->mnt_data = (qaddr_t) xmp;
168	vfs_getnewfsid(mp);
169
170	(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
171	bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
172	(void) copyinstr(args.target, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
173	    &size);
174	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
175#ifdef NULLFS_DIAGNOSTIC
176	printf("nullfs_mount: lower %s, alias at %s\n",
177		mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
178#endif
179	return (0);
180}
181
182/*
183 * VFS start.  Nothing needed here - the start routine
184 * on the underlying filesystem will have been called
185 * when that filesystem was mounted.
186 */
187static int
188nullfs_start(mp, flags, p)
189	struct mount *mp;
190	int flags;
191	struct proc *p;
192{
193	return (0);
194	/* return VFS_START(MOUNTTONULLMOUNT(mp)->nullm_vfs, flags, p); */
195}
196
197/*
198 * Free reference to null layer
199 */
200static int
201nullfs_unmount(mp, mntflags, p)
202	struct mount *mp;
203	int mntflags;
204	struct proc *p;
205{
206	struct vnode *nullm_rootvp = MOUNTTONULLMOUNT(mp)->nullm_rootvp;
207	int error;
208	int flags = 0;
209
210#ifdef NULLFS_DIAGNOSTIC
211	printf("nullfs_unmount(mp = %x)\n", mp);
212#endif
213
214	if (mntflags & MNT_FORCE)
215		flags |= FORCECLOSE;
216
217	/*
218	 * Clear out buffer cache.  I don't think we
219	 * ever get anything cached at this level at the
220	 * moment, but who knows...
221	 */
222#if 0
223	mntflushbuf(mp, 0);
224	if (mntinvalbuf(mp, 1))
225		return (EBUSY);
226#endif
227	if (nullm_rootvp->v_usecount > 1)
228		return (EBUSY);
229	error = vflush(mp, nullm_rootvp, flags);
230	if (error)
231		return (error);
232
233#ifdef NULLFS_DIAGNOSTIC
234	vprint("alias root of lower", nullm_rootvp);
235#endif
236	/*
237	 * Release reference on underlying root vnode
238	 */
239	vrele(nullm_rootvp);
240	/*
241	 * And blow it away for future re-use
242	 */
243	vgone(nullm_rootvp);
244	/*
245	 * Finally, throw away the null_mount structure
246	 */
247	free(mp->mnt_data, M_UFSMNT);	/* XXX */
248	mp->mnt_data = 0;
249	return 0;
250}
251
252static int
253nullfs_root(mp, vpp)
254	struct mount *mp;
255	struct vnode **vpp;
256{
257	struct proc *p = curproc;	/* XXX */
258	struct vnode *vp;
259
260#ifdef NULLFS_DIAGNOSTIC
261	printf("nullfs_root(mp = %x, vp = %x->%x)\n", mp,
262			MOUNTTONULLMOUNT(mp)->nullm_rootvp,
263			NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp)
264			);
265#endif
266
267	/*
268	 * Return locked reference to root.
269	 */
270	vp = MOUNTTONULLMOUNT(mp)->nullm_rootvp;
271	VREF(vp);
272	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
273	*vpp = vp;
274	return 0;
275}
276
277static int
278nullfs_quotactl(mp, cmd, uid, arg, p)
279	struct mount *mp;
280	int cmd;
281	uid_t uid;
282	caddr_t arg;
283	struct proc *p;
284{
285	return VFS_QUOTACTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, uid, arg, p);
286}
287
288static int
289nullfs_statfs(mp, sbp, p)
290	struct mount *mp;
291	struct statfs *sbp;
292	struct proc *p;
293{
294	int error;
295	struct statfs mstat;
296
297#ifdef NULLFS_DIAGNOSTIC
298	printf("nullfs_statfs(mp = %x, vp = %x->%x)\n", mp,
299			MOUNTTONULLMOUNT(mp)->nullm_rootvp,
300			NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp)
301			);
302#endif
303
304	bzero(&mstat, sizeof(mstat));
305
306	error = VFS_STATFS(MOUNTTONULLMOUNT(mp)->nullm_vfs, &mstat, p);
307	if (error)
308		return (error);
309
310	/* now copy across the "interesting" information and fake the rest */
311	sbp->f_type = mstat.f_type;
312	sbp->f_flags = mstat.f_flags;
313	sbp->f_bsize = mstat.f_bsize;
314	sbp->f_iosize = mstat.f_iosize;
315	sbp->f_blocks = mstat.f_blocks;
316	sbp->f_bfree = mstat.f_bfree;
317	sbp->f_bavail = mstat.f_bavail;
318	sbp->f_files = mstat.f_files;
319	sbp->f_ffree = mstat.f_ffree;
320	if (sbp != &mp->mnt_stat) {
321		bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
322		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
323		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
324	}
325	return (0);
326}
327
328static int
329nullfs_sync(mp, waitfor, cred, p)
330	struct mount *mp;
331	int waitfor;
332	struct ucred *cred;
333	struct proc *p;
334{
335	/*
336	 * XXX - Assumes no data cached at null layer.
337	 */
338	return (0);
339}
340
341static int
342nullfs_vget(mp, ino, vpp)
343	struct mount *mp;
344	ino_t ino;
345	struct vnode **vpp;
346{
347
348	return VFS_VGET(MOUNTTONULLMOUNT(mp)->nullm_vfs, ino, vpp);
349}
350
351static int
352nullfs_fhtovp(mp, fidp, nam, vpp, exflagsp, credanonp)
353	struct mount *mp;
354	struct fid *fidp;
355	struct mbuf *nam;
356	struct vnode **vpp;
357	int *exflagsp;
358	struct ucred**credanonp;
359{
360
361	return VFS_FHTOVP(MOUNTTONULLMOUNT(mp)->nullm_vfs, fidp, nam, vpp, exflagsp,credanonp);
362}
363
364static int
365nullfs_vptofh(vp, fhp)
366	struct vnode *vp;
367	struct fid *fhp;
368{
369	return VFS_VPTOFH(NULLVPTOLOWERVP(vp), fhp);
370}
371
372static struct vfsops null_vfsops = {
373	nullfs_mount,
374	nullfs_start,
375	nullfs_unmount,
376	nullfs_root,
377	nullfs_quotactl,
378	nullfs_statfs,
379	nullfs_sync,
380	nullfs_vget,
381	nullfs_fhtovp,
382	nullfs_vptofh,
383	nullfs_init,
384};
385
386VFS_SET(null_vfsops, null, MOUNT_NULL, VFCF_LOOPBACK);
387