null_vfsops.c revision 128019
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 * 4. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 *	@(#)null_vfsops.c	8.2 (Berkeley) 1/21/94
33 *
34 * @(#)lofs_vfsops.c	1.2 (Berkeley) 6/18/92
35 * $FreeBSD: head/sys/fs/nullfs/null_vfsops.c 128019 2004-04-07 20:46:16Z imp $
36 */
37
38/*
39 * Null Layer
40 * (See null_vnops.c for a description of what this does.)
41 */
42
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/kernel.h>
46#include <sys/lock.h>
47#include <sys/malloc.h>
48#include <sys/mount.h>
49#include <sys/namei.h>
50#include <sys/proc.h>
51#include <sys/vnode.h>
52
53#include <fs/nullfs/null.h>
54
55static MALLOC_DEFINE(M_NULLFSMNT, "NULLFS mount", "NULLFS mount structure");
56
57static vfs_fhtovp_t	nullfs_fhtovp;
58static vfs_checkexp_t	nullfs_checkexp;
59static vfs_nmount_t	nullfs_mount;
60static vfs_quotactl_t	nullfs_quotactl;
61static vfs_root_t	nullfs_root;
62static vfs_start_t	nullfs_start;
63static vfs_sync_t	nullfs_sync;
64static vfs_statfs_t	nullfs_statfs;
65static vfs_unmount_t	nullfs_unmount;
66static vfs_vget_t	nullfs_vget;
67static vfs_vptofh_t	nullfs_vptofh;
68static vfs_extattrctl_t	nullfs_extattrctl;
69
70/*
71 * Mount null layer
72 */
73static int
74nullfs_mount(mp, ndp, td)
75	struct mount *mp;
76	struct nameidata *ndp;
77	struct thread *td;
78{
79	int error = 0;
80	struct vnode *lowerrootvp, *vp;
81	struct vnode *nullm_rootvp;
82	struct null_mount *xmp;
83	char *target;
84	size_t size;
85	int isvnunlocked = 0, len;
86
87	NULLFSDEBUG("nullfs_mount(mp = %p)\n", (void *)mp);
88
89	/*
90	 * Update is a no-op
91	 */
92	if (mp->mnt_flag & MNT_UPDATE) {
93		return (EOPNOTSUPP);
94		/* return VFS_MOUNT(MOUNTTONULLMOUNT(mp)->nullm_vfs, path, data, ndp, td);*/
95	}
96
97	/*
98	 * Get argument
99	 */
100	error = vfs_getopt(mp->mnt_optnew, "target", (void **)&target, &len);
101	if (error || target[len - 1] != '\0')
102		return (EINVAL);
103
104	/*
105	 * Unlock lower node to avoid deadlock.
106	 * (XXX) VOP_ISLOCKED is needed?
107	 */
108	if ((mp->mnt_vnodecovered->v_op == null_vnodeop_p) &&
109		VOP_ISLOCKED(mp->mnt_vnodecovered, NULL)) {
110		VOP_UNLOCK(mp->mnt_vnodecovered, 0, td);
111		isvnunlocked = 1;
112	}
113	/*
114	 * Find lower node
115	 */
116	NDINIT(ndp, LOOKUP, FOLLOW|WANTPARENT|LOCKLEAF,
117		UIO_SYSSPACE, target, td);
118	error = namei(ndp);
119	/*
120	 * Re-lock vnode.
121	 */
122	if (isvnunlocked && !VOP_ISLOCKED(mp->mnt_vnodecovered, NULL))
123		vn_lock(mp->mnt_vnodecovered, LK_EXCLUSIVE | LK_RETRY, td);
124
125	if (error)
126		return (error);
127	NDFREE(ndp, NDF_ONLY_PNBUF);
128
129	/*
130	 * Sanity check on lower vnode
131	 */
132	lowerrootvp = ndp->ni_vp;
133
134	vrele(ndp->ni_dvp);
135	ndp->ni_dvp = NULLVP;
136
137	/*
138	 * Check multi null mount to avoid `lock against myself' panic.
139	 */
140	if (lowerrootvp == VTONULL(mp->mnt_vnodecovered)->null_lowervp) {
141		NULLFSDEBUG("nullfs_mount: multi null mount?\n");
142		vput(lowerrootvp);
143		return (EDEADLK);
144	}
145
146	xmp = (struct null_mount *) malloc(sizeof(struct null_mount),
147				M_NULLFSMNT, M_WAITOK);	/* XXX */
148
149	/*
150	 * Save reference to underlying FS
151	 */
152	xmp->nullm_vfs = lowerrootvp->v_mount;
153
154	/*
155	 * Save reference.  Each mount also holds
156	 * a reference on the root vnode.
157	 */
158	error = null_nodeget(mp, lowerrootvp, &vp);
159	/*
160	 * Make sure the node alias worked
161	 */
162	if (error) {
163		VOP_UNLOCK(vp, 0, td);
164		vrele(lowerrootvp);
165		free(xmp, M_NULLFSMNT);	/* XXX */
166		return (error);
167	}
168
169	/*
170	 * Keep a held reference to the root vnode.
171	 * It is vrele'd in nullfs_unmount.
172	 */
173	nullm_rootvp = vp;
174	nullm_rootvp->v_vflag |= VV_ROOT;
175	xmp->nullm_rootvp = nullm_rootvp;
176
177	/*
178	 * Unlock the node (either the lower or the alias)
179	 */
180	VOP_UNLOCK(vp, 0, td);
181
182	if (NULLVPTOLOWERVP(nullm_rootvp)->v_mount->mnt_flag & MNT_LOCAL)
183		mp->mnt_flag |= MNT_LOCAL;
184	mp->mnt_data = (qaddr_t) xmp;
185	vfs_getnewfsid(mp);
186
187	(void) copystr(target, mp->mnt_stat.f_mntfromname,
188	    MNAMELEN - 1, &size);
189	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
190	(void)nullfs_statfs(mp, &mp->mnt_stat, td);
191	NULLFSDEBUG("nullfs_mount: lower %s, alias at %s\n",
192		mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
193	return (0);
194}
195
196/*
197 * VFS start.  Nothing needed here - the start routine
198 * on the underlying filesystem will have been called
199 * when that filesystem was mounted.
200 */
201static int
202nullfs_start(mp, flags, td)
203	struct mount *mp;
204	int flags;
205	struct thread *td;
206{
207	return (0);
208	/* return VFS_START(MOUNTTONULLMOUNT(mp)->nullm_vfs, flags, td); */
209}
210
211/*
212 * Free reference to null layer
213 */
214static int
215nullfs_unmount(mp, mntflags, td)
216	struct mount *mp;
217	int mntflags;
218	struct thread *td;
219{
220	void *mntdata;
221	int error;
222	int flags = 0;
223
224	NULLFSDEBUG("nullfs_unmount: mp = %p\n", (void *)mp);
225
226	if (mntflags & MNT_FORCE)
227		flags |= FORCECLOSE;
228
229	/* There is 1 extra root vnode reference (nullm_rootvp). */
230	error = vflush(mp, 1, flags);
231	if (error)
232		return (error);
233
234	/*
235	 * Finally, throw away the null_mount structure
236	 */
237	mntdata = mp->mnt_data;
238	mp->mnt_data = 0;
239	free(mntdata, M_NULLFSMNT);
240	return 0;
241}
242
243static int
244nullfs_root(mp, vpp)
245	struct mount *mp;
246	struct vnode **vpp;
247{
248	struct thread *td = curthread;	/* XXX */
249	struct vnode *vp;
250
251	NULLFSDEBUG("nullfs_root(mp = %p, vp = %p->%p)\n", (void *)mp,
252	    (void *)MOUNTTONULLMOUNT(mp)->nullm_rootvp,
253	    (void *)NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp));
254
255	/*
256	 * Return locked reference to root.
257	 */
258	vp = MOUNTTONULLMOUNT(mp)->nullm_rootvp;
259	VREF(vp);
260
261#ifdef NULLFS_DEBUG
262	if (VOP_ISLOCKED(vp, NULL)) {
263		Debugger("root vnode is locked.\n");
264		vrele(vp);
265		return (EDEADLK);
266	}
267#endif
268	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
269	*vpp = vp;
270	return 0;
271}
272
273static int
274nullfs_quotactl(mp, cmd, uid, arg, td)
275	struct mount *mp;
276	int cmd;
277	uid_t uid;
278	caddr_t arg;
279	struct thread *td;
280{
281	return VFS_QUOTACTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, uid, arg, td);
282}
283
284static int
285nullfs_statfs(mp, sbp, td)
286	struct mount *mp;
287	struct statfs *sbp;
288	struct thread *td;
289{
290	int error;
291	struct statfs mstat;
292
293	NULLFSDEBUG("nullfs_statfs(mp = %p, vp = %p->%p)\n", (void *)mp,
294	    (void *)MOUNTTONULLMOUNT(mp)->nullm_rootvp,
295	    (void *)NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp));
296
297	bzero(&mstat, sizeof(mstat));
298
299	error = VFS_STATFS(MOUNTTONULLMOUNT(mp)->nullm_vfs, &mstat, td);
300	if (error)
301		return (error);
302
303	/* now copy across the "interesting" information and fake the rest */
304	sbp->f_type = mstat.f_type;
305	sbp->f_flags = mstat.f_flags;
306	sbp->f_bsize = mstat.f_bsize;
307	sbp->f_iosize = mstat.f_iosize;
308	sbp->f_blocks = mstat.f_blocks;
309	sbp->f_bfree = mstat.f_bfree;
310	sbp->f_bavail = mstat.f_bavail;
311	sbp->f_files = mstat.f_files;
312	sbp->f_ffree = mstat.f_ffree;
313	if (sbp != &mp->mnt_stat) {
314		bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
315		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
316		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
317	}
318	return (0);
319}
320
321static int
322nullfs_sync(mp, waitfor, cred, td)
323	struct mount *mp;
324	int waitfor;
325	struct ucred *cred;
326	struct thread *td;
327{
328	/*
329	 * XXX - Assumes no data cached at null layer.
330	 */
331	return (0);
332}
333
334static int
335nullfs_vget(mp, ino, flags, vpp)
336	struct mount *mp;
337	ino_t ino;
338	int flags;
339	struct vnode **vpp;
340{
341	int error;
342	error = VFS_VGET(MOUNTTONULLMOUNT(mp)->nullm_vfs, ino, flags, vpp);
343	if (error)
344		return (error);
345
346	return (null_nodeget(mp, *vpp, vpp));
347}
348
349static int
350nullfs_fhtovp(mp, fidp, vpp)
351	struct mount *mp;
352	struct fid *fidp;
353	struct vnode **vpp;
354{
355	int error;
356	error = VFS_FHTOVP(MOUNTTONULLMOUNT(mp)->nullm_vfs, fidp, vpp);
357	if (error)
358		return (error);
359
360	return (null_nodeget(mp, *vpp, vpp));
361}
362
363static int
364nullfs_checkexp(mp, nam, extflagsp, credanonp)
365	struct mount *mp;
366	struct sockaddr *nam;
367	int *extflagsp;
368	struct ucred **credanonp;
369{
370
371	return VFS_CHECKEXP(MOUNTTONULLMOUNT(mp)->nullm_vfs, nam,
372		extflagsp, credanonp);
373}
374
375static int
376nullfs_vptofh(vp, fhp)
377	struct vnode *vp;
378	struct fid *fhp;
379{
380	struct vnode *lvp;
381
382	lvp = NULLVPTOLOWERVP(vp);
383	return VFS_VPTOFH(lvp, fhp);
384}
385
386static int
387nullfs_extattrctl(mp, cmd, filename_vp, namespace, attrname, td)
388	struct mount *mp;
389	int cmd;
390	struct vnode *filename_vp;
391	int namespace;
392	const char *attrname;
393	struct thread *td;
394{
395	return VFS_EXTATTRCTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, filename_vp,
396	    namespace, attrname, td);
397}
398
399
400static struct vfsops null_vfsops = {
401	.vfs_checkexp =		nullfs_checkexp,
402	.vfs_extattrctl =	nullfs_extattrctl,
403	.vfs_fhtovp =		nullfs_fhtovp,
404	.vfs_init =		nullfs_init,
405	.vfs_nmount =		nullfs_mount,
406	.vfs_quotactl =		nullfs_quotactl,
407	.vfs_root =		nullfs_root,
408	.vfs_start =		nullfs_start,
409	.vfs_statfs =		nullfs_statfs,
410	.vfs_sync =		nullfs_sync,
411	.vfs_uninit =		nullfs_uninit,
412	.vfs_unmount =		nullfs_unmount,
413	.vfs_vget =		nullfs_vget,
414	.vfs_vptofh =		nullfs_vptofh,
415};
416
417VFS_SET(null_vfsops, nullfs, VFCF_LOOPBACK);
418