null_vfsops.c revision 226688
1275970Scy/*-
2275970Scy * Copyright (c) 1992, 1993, 1995
3275970Scy *	The Regents of the University of California.  All rights reserved.
4275970Scy *
5275970Scy * This code is derived from software donated to Berkeley by
6275970Scy * Jan-Simon Pendry.
7275970Scy *
8275970Scy * Redistribution and use in source and binary forms, with or without
9275970Scy * modification, are permitted provided that the following conditions
10275970Scy * are met:
11275970Scy * 1. Redistributions of source code must retain the above copyright
12275970Scy *    notice, this list of conditions and the following disclaimer.
13275970Scy * 2. Redistributions in binary form must reproduce the above copyright
14275970Scy *    notice, this list of conditions and the following disclaimer in the
15275970Scy *    documentation and/or other materials provided with the distribution.
16275970Scy * 4. Neither the name of the University nor the names of its contributors
17275970Scy *    may be used to endorse or promote products derived from this software
18275970Scy *    without specific prior written permission.
19275970Scy *
20275970Scy * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21275970Scy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22275970Scy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23275970Scy * 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 226688 2011-10-24 13:56:31Z kib $
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/fcntl.h>
46#include <sys/kernel.h>
47#include <sys/lock.h>
48#include <sys/malloc.h>
49#include <sys/mount.h>
50#include <sys/namei.h>
51#include <sys/proc.h>
52#include <sys/vnode.h>
53
54#include <fs/nullfs/null.h>
55
56static MALLOC_DEFINE(M_NULLFSMNT, "nullfs_mount", "NULLFS mount structure");
57
58static vfs_fhtovp_t	nullfs_fhtovp;
59static vfs_mount_t	nullfs_mount;
60static vfs_quotactl_t	nullfs_quotactl;
61static vfs_root_t	nullfs_root;
62static vfs_sync_t	nullfs_sync;
63static vfs_statfs_t	nullfs_statfs;
64static vfs_unmount_t	nullfs_unmount;
65static vfs_vget_t	nullfs_vget;
66static vfs_extattrctl_t	nullfs_extattrctl;
67
68/*
69 * Mount null layer
70 */
71static int
72nullfs_mount(struct mount *mp)
73{
74	int error = 0;
75	struct vnode *lowerrootvp, *vp;
76	struct vnode *nullm_rootvp;
77	struct null_mount *xmp;
78	char *target;
79	int isvnunlocked = 0, len;
80	struct nameidata nd, *ndp = &nd;
81
82	NULLFSDEBUG("nullfs_mount(mp = %p)\n", (void *)mp);
83
84	if (mp->mnt_flag & MNT_ROOTFS)
85		return (EOPNOTSUPP);
86	/*
87	 * Update is a no-op
88	 */
89	if (mp->mnt_flag & MNT_UPDATE) {
90		/*
91		 * Only support update mounts for NFS export.
92		 */
93		if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0))
94			return (0);
95		else
96			return (EOPNOTSUPP);
97	}
98
99	/*
100	 * Get argument
101	 */
102	error = vfs_getopt(mp->mnt_optnew, "target", (void **)&target, &len);
103	if (error || target[len - 1] != '\0')
104		return (EINVAL);
105
106	/*
107	 * Unlock lower node to avoid possible deadlock.
108	 */
109	if ((mp->mnt_vnodecovered->v_op == &null_vnodeops) &&
110	    VOP_ISLOCKED(mp->mnt_vnodecovered) == LK_EXCLUSIVE) {
111		VOP_UNLOCK(mp->mnt_vnodecovered, 0);
112		isvnunlocked = 1;
113	}
114	/*
115	 * Find lower node
116	 */
117	NDINIT(ndp, LOOKUP, FOLLOW|LOCKLEAF, UIO_SYSSPACE, target, curthread);
118	error = namei(ndp);
119	/*
120	 * Re-lock vnode.
121	 */
122	if (isvnunlocked)
123		vn_lock(mp->mnt_vnodecovered, LK_EXCLUSIVE | LK_RETRY);
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	/*
135	 * Check multi null mount to avoid `lock against myself' panic.
136	 */
137	if (lowerrootvp == VTONULL(mp->mnt_vnodecovered)->null_lowervp) {
138		NULLFSDEBUG("nullfs_mount: multi null mount?\n");
139		vput(lowerrootvp);
140		return (EDEADLK);
141	}
142
143	xmp = (struct null_mount *) malloc(sizeof(struct null_mount),
144				M_NULLFSMNT, M_WAITOK);	/* XXX */
145
146	/*
147	 * Save reference to underlying FS
148	 */
149	xmp->nullm_vfs = lowerrootvp->v_mount;
150
151	/*
152	 * Save reference.  Each mount also holds
153	 * a reference on the root vnode.
154	 */
155	error = null_nodeget(mp, lowerrootvp, &vp);
156	/*
157	 * Make sure the node alias worked
158	 */
159	if (error) {
160		vrele(lowerrootvp);
161		free(xmp, M_NULLFSMNT);	/* XXX */
162		return (error);
163	}
164
165	/*
166	 * Keep a held reference to the root vnode.
167	 * It is vrele'd in nullfs_unmount.
168	 */
169	nullm_rootvp = vp;
170	nullm_rootvp->v_vflag |= VV_ROOT;
171	xmp->nullm_rootvp = nullm_rootvp;
172
173	/*
174	 * Unlock the node (either the lower or the alias)
175	 */
176	VOP_UNLOCK(vp, 0);
177
178	if (NULLVPTOLOWERVP(nullm_rootvp)->v_mount->mnt_flag & MNT_LOCAL) {
179		MNT_ILOCK(mp);
180		mp->mnt_flag |= MNT_LOCAL;
181		MNT_IUNLOCK(mp);
182	}
183	MNT_ILOCK(mp);
184	mp->mnt_kern_flag |= lowerrootvp->v_mount->mnt_kern_flag & MNTK_MPSAFE;
185	MNT_IUNLOCK(mp);
186	mp->mnt_data =  xmp;
187	vfs_getnewfsid(mp);
188
189	vfs_mountedfrom(mp, target);
190
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 * Free reference to null layer
198 */
199static int
200nullfs_unmount(mp, mntflags)
201	struct mount *mp;
202	int mntflags;
203{
204	void *mntdata;
205	int error;
206	int flags = 0;
207
208	NULLFSDEBUG("nullfs_unmount: mp = %p\n", (void *)mp);
209
210	if (mntflags & MNT_FORCE)
211		flags |= FORCECLOSE;
212
213	/* There is 1 extra root vnode reference (nullm_rootvp). */
214	error = vflush(mp, 1, flags, curthread);
215	if (error)
216		return (error);
217
218	/*
219	 * Finally, throw away the null_mount structure
220	 */
221	mntdata = mp->mnt_data;
222	mp->mnt_data = 0;
223	free(mntdata, M_NULLFSMNT);
224	return 0;
225}
226
227static int
228nullfs_root(mp, flags, vpp)
229	struct mount *mp;
230	int flags;
231	struct vnode **vpp;
232{
233	struct vnode *vp;
234
235	NULLFSDEBUG("nullfs_root(mp = %p, vp = %p->%p)\n", (void *)mp,
236	    (void *)MOUNTTONULLMOUNT(mp)->nullm_rootvp,
237	    (void *)NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp));
238
239	/*
240	 * Return locked reference to root.
241	 */
242	vp = MOUNTTONULLMOUNT(mp)->nullm_rootvp;
243	VREF(vp);
244
245	ASSERT_VOP_UNLOCKED(vp, "root vnode is locked");
246	vn_lock(vp, flags | LK_RETRY);
247	*vpp = vp;
248	return 0;
249}
250
251static int
252nullfs_quotactl(mp, cmd, uid, arg)
253	struct mount *mp;
254	int cmd;
255	uid_t uid;
256	void *arg;
257{
258	return VFS_QUOTACTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, uid, arg);
259}
260
261static int
262nullfs_statfs(mp, sbp)
263	struct mount *mp;
264	struct statfs *sbp;
265{
266	int error;
267	struct statfs mstat;
268
269	NULLFSDEBUG("nullfs_statfs(mp = %p, vp = %p->%p)\n", (void *)mp,
270	    (void *)MOUNTTONULLMOUNT(mp)->nullm_rootvp,
271	    (void *)NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp));
272
273	bzero(&mstat, sizeof(mstat));
274
275	error = VFS_STATFS(MOUNTTONULLMOUNT(mp)->nullm_vfs, &mstat);
276	if (error)
277		return (error);
278
279	/* now copy across the "interesting" information and fake the rest */
280	sbp->f_type = mstat.f_type;
281	sbp->f_flags = mstat.f_flags;
282	sbp->f_bsize = mstat.f_bsize;
283	sbp->f_iosize = mstat.f_iosize;
284	sbp->f_blocks = mstat.f_blocks;
285	sbp->f_bfree = mstat.f_bfree;
286	sbp->f_bavail = mstat.f_bavail;
287	sbp->f_files = mstat.f_files;
288	sbp->f_ffree = mstat.f_ffree;
289	return (0);
290}
291
292static int
293nullfs_sync(mp, waitfor)
294	struct mount *mp;
295	int waitfor;
296{
297	/*
298	 * XXX - Assumes no data cached at null layer.
299	 */
300	return (0);
301}
302
303static int
304nullfs_vget(mp, ino, flags, vpp)
305	struct mount *mp;
306	ino_t ino;
307	int flags;
308	struct vnode **vpp;
309{
310	int error;
311	error = VFS_VGET(MOUNTTONULLMOUNT(mp)->nullm_vfs, ino, flags, vpp);
312	if (error)
313		return (error);
314
315	return (null_nodeget(mp, *vpp, vpp));
316}
317
318static int
319nullfs_fhtovp(mp, fidp, flags, vpp)
320	struct mount *mp;
321	struct fid *fidp;
322	int flags;
323	struct vnode **vpp;
324{
325	int error;
326	error = VFS_FHTOVP(MOUNTTONULLMOUNT(mp)->nullm_vfs, fidp, LK_EXCLUSIVE,
327	    vpp);
328	if (error)
329		return (error);
330
331	return (null_nodeget(mp, *vpp, vpp));
332}
333
334static int
335nullfs_extattrctl(mp, cmd, filename_vp, namespace, attrname)
336	struct mount *mp;
337	int cmd;
338	struct vnode *filename_vp;
339	int namespace;
340	const char *attrname;
341{
342	return VFS_EXTATTRCTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, filename_vp,
343	    namespace, attrname);
344}
345
346
347static struct vfsops null_vfsops = {
348	.vfs_extattrctl =	nullfs_extattrctl,
349	.vfs_fhtovp =		nullfs_fhtovp,
350	.vfs_init =		nullfs_init,
351	.vfs_mount =		nullfs_mount,
352	.vfs_quotactl =		nullfs_quotactl,
353	.vfs_root =		nullfs_root,
354	.vfs_statfs =		nullfs_statfs,
355	.vfs_sync =		nullfs_sync,
356	.vfs_uninit =		nullfs_uninit,
357	.vfs_unmount =		nullfs_unmount,
358	.vfs_vget =		nullfs_vget,
359};
360
361VFS_SET(null_vfsops, nullfs, VFCF_LOOPBACK);
362