null_vfsops.c revision 229431
117680Spst/*-
217680Spst * Copyright (c) 1992, 1993, 1995
317680Spst *	The Regents of the University of California.  All rights reserved.
417680Spst *
517680Spst * This code is derived from software donated to Berkeley by
617680Spst * Jan-Simon Pendry.
717680Spst *
817680Spst * Redistribution and use in source and binary forms, with or without
917680Spst * modification, are permitted provided that the following conditions
1017680Spst * are met:
1117680Spst * 1. Redistributions of source code must retain the above copyright
1217680Spst *    notice, this list of conditions and the following disclaimer.
1317680Spst * 2. Redistributions in binary form must reproduce the above copyright
1417680Spst *    notice, this list of conditions and the following disclaimer in the
1517680Spst *    documentation and/or other materials provided with the distribution.
1617680Spst * 4. Neither the name of the University nor the names of its contributors
1717680Spst *    may be used to endorse or promote products derived from this software
1817680Spst *    without specific prior written permission.
1917680Spst *
2017680Spst * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2117680Spst * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2217680Spst * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2317680Spst * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2417680Spst * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2517680Spst * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2617680Spst * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2717680Spst * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2817680Spst * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2917680Spst * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3017680Spst * SUCH DAMAGE.
3117680Spst *
3217680Spst *	@(#)null_vfsops.c	8.2 (Berkeley) 1/21/94
3317680Spst *
3417680Spst * @(#)lofs_vfsops.c	1.2 (Berkeley) 6/18/92
3517680Spst * $FreeBSD: head/sys/fs/nullfs/null_vfsops.c 229431 2012-01-03 21:09:07Z kib $
3621262Swollman */
3717680Spst
3817680Spst/*
3917680Spst * Null Layer
4017680Spst * (See null_vnops.c for a description of what this does.)
4117680Spst */
4217680Spst
4317680Spst#include <sys/param.h>
4417680Spst#include <sys/systm.h>
4517680Spst#include <sys/fcntl.h>
4617680Spst#include <sys/kernel.h>
4717680Spst#include <sys/lock.h>
4817680Spst#include <sys/malloc.h>
4917680Spst#include <sys/mount.h>
5017680Spst#include <sys/namei.h>
5117680Spst#include <sys/proc.h>
5217680Spst#include <sys/vnode.h>
5317680Spst
5417680Spst#include <fs/nullfs/null.h>
5517680Spst
5617680Spststatic MALLOC_DEFINE(M_NULLFSMNT, "nullfs_mount", "NULLFS mount structure");
5717680Spst
5817680Spststatic vfs_fhtovp_t	nullfs_fhtovp;
5917680Spststatic vfs_mount_t	nullfs_mount;
6017680Spststatic vfs_quotactl_t	nullfs_quotactl;
6117680Spststatic vfs_root_t	nullfs_root;
6217680Spststatic vfs_sync_t	nullfs_sync;
6317680Spststatic vfs_statfs_t	nullfs_statfs;
6417680Spststatic vfs_unmount_t	nullfs_unmount;
6517680Spststatic vfs_vget_t	nullfs_vget;
6617680Spststatic vfs_extattrctl_t	nullfs_extattrctl;
6717680Spst
6817680Spst/*
6917680Spst * Mount null layer
7017680Spst */
7117680Spststatic int
7217680Spstnullfs_mount(struct mount *mp)
7317680Spst{
7417680Spst	int error = 0;
7517680Spst	struct vnode *lowerrootvp, *vp;
7617680Spst	struct vnode *nullm_rootvp;
7717680Spst	struct null_mount *xmp;
7817680Spst	char *target;
7917680Spst	int isvnunlocked = 0, len;
8017680Spst	struct nameidata nd, *ndp = &nd;
8117680Spst
8217680Spst	NULLFSDEBUG("nullfs_mount(mp = %p)\n", (void *)mp);
8317680Spst
8417680Spst	if (mp->mnt_flag & MNT_ROOTFS)
8517680Spst		return (EOPNOTSUPP);
8617680Spst	/*
8717680Spst	 * Update is a no-op
8817680Spst	 */
8917680Spst	if (mp->mnt_flag & MNT_UPDATE) {
9017680Spst		/*
9117680Spst		 * Only support update mounts for NFS export.
9217680Spst		 */
9317680Spst		if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0))
9417680Spst			return (0);
9517680Spst		else
9617680Spst			return (EOPNOTSUPP);
9717680Spst	}
9817680Spst
9917680Spst	/*
10017680Spst	 * Get argument
10117680Spst	 */
10217680Spst	error = vfs_getopt(mp->mnt_optnew, "target", (void **)&target, &len);
10317680Spst	if (error || target[len - 1] != '\0')
10417680Spst		return (EINVAL);
10517680Spst
10617680Spst	/*
10717680Spst	 * Unlock lower node to avoid possible deadlock.
10817680Spst	 */
10917680Spst	if ((mp->mnt_vnodecovered->v_op == &null_vnodeops) &&
11017680Spst	    VOP_ISLOCKED(mp->mnt_vnodecovered) == LK_EXCLUSIVE) {
11117680Spst		VOP_UNLOCK(mp->mnt_vnodecovered, 0);
11217680Spst		isvnunlocked = 1;
11317680Spst	}
11417680Spst	/*
11517680Spst	 * Find lower node
11617680Spst	 */
11717680Spst	NDINIT(ndp, LOOKUP, FOLLOW|LOCKLEAF, UIO_SYSSPACE, target, curthread);
11817680Spst	error = namei(ndp);
11917680Spst	/*
12017680Spst	 * Re-lock vnode.
12117680Spst	 */
12217680Spst	if (isvnunlocked)
12317680Spst		vn_lock(mp->mnt_vnodecovered, LK_EXCLUSIVE | LK_RETRY);
12417680Spst
12517680Spst	if (error)
12617680Spst		return (error);
12717680Spst	NDFREE(ndp, NDF_ONLY_PNBUF);
12817680Spst
12917680Spst	/*
13017680Spst	 * Sanity check on lower vnode
13117680Spst	 */
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		free(xmp, M_NULLFSMNT);
161		return (error);
162	}
163
164	/*
165	 * Keep a held reference to the root vnode.
166	 * It is vrele'd in nullfs_unmount.
167	 */
168	nullm_rootvp = vp;
169	nullm_rootvp->v_vflag |= VV_ROOT;
170	xmp->nullm_rootvp = nullm_rootvp;
171
172	/*
173	 * Unlock the node (either the lower or the alias)
174	 */
175	VOP_UNLOCK(vp, 0);
176
177	if (NULLVPTOLOWERVP(nullm_rootvp)->v_mount->mnt_flag & MNT_LOCAL) {
178		MNT_ILOCK(mp);
179		mp->mnt_flag |= MNT_LOCAL;
180		MNT_IUNLOCK(mp);
181	}
182	MNT_ILOCK(mp);
183	mp->mnt_kern_flag |= lowerrootvp->v_mount->mnt_kern_flag & MNTK_MPSAFE;
184	MNT_IUNLOCK(mp);
185	mp->mnt_data =  xmp;
186	vfs_getnewfsid(mp);
187
188	vfs_mountedfrom(mp, target);
189
190	NULLFSDEBUG("nullfs_mount: lower %s, alias at %s\n",
191		mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
192	return (0);
193}
194
195/*
196 * Free reference to null layer
197 */
198static int
199nullfs_unmount(mp, mntflags)
200	struct mount *mp;
201	int mntflags;
202{
203	void *mntdata;
204	int error;
205	int flags = 0;
206
207	NULLFSDEBUG("nullfs_unmount: mp = %p\n", (void *)mp);
208
209	if (mntflags & MNT_FORCE)
210		flags |= FORCECLOSE;
211
212	/* There is 1 extra root vnode reference (nullm_rootvp). */
213	error = vflush(mp, 1, flags, curthread);
214	if (error)
215		return (error);
216
217	/*
218	 * Finally, throw away the null_mount structure
219	 */
220	mntdata = mp->mnt_data;
221	mp->mnt_data = 0;
222	free(mntdata, M_NULLFSMNT);
223	return 0;
224}
225
226static int
227nullfs_root(mp, flags, vpp)
228	struct mount *mp;
229	int flags;
230	struct vnode **vpp;
231{
232	struct vnode *vp;
233
234	NULLFSDEBUG("nullfs_root(mp = %p, vp = %p->%p)\n", (void *)mp,
235	    (void *)MOUNTTONULLMOUNT(mp)->nullm_rootvp,
236	    (void *)NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp));
237
238	/*
239	 * Return locked reference to root.
240	 */
241	vp = MOUNTTONULLMOUNT(mp)->nullm_rootvp;
242	VREF(vp);
243
244	ASSERT_VOP_UNLOCKED(vp, "root vnode is locked");
245	vn_lock(vp, flags | LK_RETRY);
246	*vpp = vp;
247	return 0;
248}
249
250static int
251nullfs_quotactl(mp, cmd, uid, arg)
252	struct mount *mp;
253	int cmd;
254	uid_t uid;
255	void *arg;
256{
257	return VFS_QUOTACTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, uid, arg);
258}
259
260static int
261nullfs_statfs(mp, sbp)
262	struct mount *mp;
263	struct statfs *sbp;
264{
265	int error;
266	struct statfs mstat;
267
268	NULLFSDEBUG("nullfs_statfs(mp = %p, vp = %p->%p)\n", (void *)mp,
269	    (void *)MOUNTTONULLMOUNT(mp)->nullm_rootvp,
270	    (void *)NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp));
271
272	bzero(&mstat, sizeof(mstat));
273
274	error = VFS_STATFS(MOUNTTONULLMOUNT(mp)->nullm_vfs, &mstat);
275	if (error)
276		return (error);
277
278	/* now copy across the "interesting" information and fake the rest */
279	sbp->f_type = mstat.f_type;
280	sbp->f_flags = mstat.f_flags;
281	sbp->f_bsize = mstat.f_bsize;
282	sbp->f_iosize = mstat.f_iosize;
283	sbp->f_blocks = mstat.f_blocks;
284	sbp->f_bfree = mstat.f_bfree;
285	sbp->f_bavail = mstat.f_bavail;
286	sbp->f_files = mstat.f_files;
287	sbp->f_ffree = mstat.f_ffree;
288	return (0);
289}
290
291static int
292nullfs_sync(mp, waitfor)
293	struct mount *mp;
294	int waitfor;
295{
296	/*
297	 * XXX - Assumes no data cached at null layer.
298	 */
299	return (0);
300}
301
302static int
303nullfs_vget(mp, ino, flags, vpp)
304	struct mount *mp;
305	ino_t ino;
306	int flags;
307	struct vnode **vpp;
308{
309	int error;
310	error = VFS_VGET(MOUNTTONULLMOUNT(mp)->nullm_vfs, ino, flags, vpp);
311	if (error)
312		return (error);
313
314	return (null_nodeget(mp, *vpp, vpp));
315}
316
317static int
318nullfs_fhtovp(mp, fidp, flags, vpp)
319	struct mount *mp;
320	struct fid *fidp;
321	int flags;
322	struct vnode **vpp;
323{
324	int error;
325	error = VFS_FHTOVP(MOUNTTONULLMOUNT(mp)->nullm_vfs, fidp, LK_EXCLUSIVE,
326	    vpp);
327	if (error)
328		return (error);
329
330	return (null_nodeget(mp, *vpp, vpp));
331}
332
333static int
334nullfs_extattrctl(mp, cmd, filename_vp, namespace, attrname)
335	struct mount *mp;
336	int cmd;
337	struct vnode *filename_vp;
338	int namespace;
339	const char *attrname;
340{
341	return VFS_EXTATTRCTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, filename_vp,
342	    namespace, attrname);
343}
344
345
346static struct vfsops null_vfsops = {
347	.vfs_extattrctl =	nullfs_extattrctl,
348	.vfs_fhtovp =		nullfs_fhtovp,
349	.vfs_init =		nullfs_init,
350	.vfs_mount =		nullfs_mount,
351	.vfs_quotactl =		nullfs_quotactl,
352	.vfs_root =		nullfs_root,
353	.vfs_statfs =		nullfs_statfs,
354	.vfs_sync =		nullfs_sync,
355	.vfs_uninit =		nullfs_uninit,
356	.vfs_unmount =		nullfs_unmount,
357	.vfs_vget =		nullfs_vget,
358};
359
360VFS_SET(null_vfsops, nullfs, VFCF_LOOPBACK);
361