null_vfsops.c revision 245004
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 245004 2013-01-03 19:17:57Z 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#include <sys/jail.h>
54
55#include <fs/nullfs/null.h>
56
57static MALLOC_DEFINE(M_NULLFSMNT, "nullfs_mount", "NULLFS mount structure");
58
59static vfs_fhtovp_t	nullfs_fhtovp;
60static vfs_mount_t	nullfs_mount;
61static vfs_quotactl_t	nullfs_quotactl;
62static vfs_root_t	nullfs_root;
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_extattrctl_t	nullfs_extattrctl;
68static vfs_reclaim_lowervp_t nullfs_reclaim_lowervp;
69
70/* Mount options that we support. */
71static const char *nullfs_opts[] = {
72	"cache",
73	"export",
74	"from",
75	"target",
76	NULL
77};
78
79/*
80 * Mount null layer
81 */
82static int
83nullfs_mount(struct mount *mp)
84{
85	int error = 0;
86	struct vnode *lowerrootvp, *vp;
87	struct vnode *nullm_rootvp;
88	struct null_mount *xmp;
89	struct thread *td = curthread;
90	char *target;
91	int isvnunlocked = 0, len;
92	struct nameidata nd, *ndp = &nd;
93
94	NULLFSDEBUG("nullfs_mount(mp = %p)\n", (void *)mp);
95
96	if (!prison_allow(td->td_ucred, PR_ALLOW_MOUNT_NULLFS))
97		return (EPERM);
98	if (mp->mnt_flag & MNT_ROOTFS)
99		return (EOPNOTSUPP);
100	if (vfs_filteropt(mp->mnt_optnew, nullfs_opts))
101		return (EINVAL);
102
103	/*
104	 * Update is a no-op
105	 */
106	if (mp->mnt_flag & MNT_UPDATE) {
107		/*
108		 * Only support update mounts for NFS export.
109		 */
110		if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0))
111			return (0);
112		else
113			return (EOPNOTSUPP);
114	}
115
116	/*
117	 * Get argument
118	 */
119	error = vfs_getopt(mp->mnt_optnew, "target", (void **)&target, &len);
120	if (error || target[len - 1] != '\0')
121		return (EINVAL);
122
123	/*
124	 * Unlock lower node to avoid possible deadlock.
125	 */
126	if ((mp->mnt_vnodecovered->v_op == &null_vnodeops) &&
127	    VOP_ISLOCKED(mp->mnt_vnodecovered) == LK_EXCLUSIVE) {
128		VOP_UNLOCK(mp->mnt_vnodecovered, 0);
129		isvnunlocked = 1;
130	}
131	/*
132	 * Find lower node
133	 */
134	NDINIT(ndp, LOOKUP, FOLLOW|LOCKLEAF, UIO_SYSSPACE, target, curthread);
135	error = namei(ndp);
136
137	/*
138	 * Re-lock vnode.
139	 * XXXKIB This is deadlock-prone as well.
140	 */
141	if (isvnunlocked)
142		vn_lock(mp->mnt_vnodecovered, LK_EXCLUSIVE | LK_RETRY);
143
144	if (error)
145		return (error);
146	NDFREE(ndp, NDF_ONLY_PNBUF);
147
148	/*
149	 * Sanity check on lower vnode
150	 */
151	lowerrootvp = ndp->ni_vp;
152
153	/*
154	 * Check multi null mount to avoid `lock against myself' panic.
155	 */
156	if (lowerrootvp == VTONULL(mp->mnt_vnodecovered)->null_lowervp) {
157		NULLFSDEBUG("nullfs_mount: multi null mount?\n");
158		vput(lowerrootvp);
159		return (EDEADLK);
160	}
161
162	xmp = (struct null_mount *) malloc(sizeof(struct null_mount),
163	    M_NULLFSMNT, M_WAITOK | M_ZERO);
164
165	/*
166	 * Save reference to underlying FS
167	 */
168	xmp->nullm_vfs = lowerrootvp->v_mount;
169
170	/*
171	 * Save reference.  Each mount also holds
172	 * a reference on the root vnode.
173	 */
174	error = null_nodeget(mp, lowerrootvp, &vp);
175	/*
176	 * Make sure the node alias worked
177	 */
178	if (error) {
179		free(xmp, M_NULLFSMNT);
180		return (error);
181	}
182
183	/*
184	 * Keep a held reference to the root vnode.
185	 * It is vrele'd in nullfs_unmount.
186	 */
187	nullm_rootvp = vp;
188	nullm_rootvp->v_vflag |= VV_ROOT;
189	xmp->nullm_rootvp = nullm_rootvp;
190
191	/*
192	 * Unlock the node (either the lower or the alias)
193	 */
194	VOP_UNLOCK(vp, 0);
195
196	if (NULLVPTOLOWERVP(nullm_rootvp)->v_mount->mnt_flag & MNT_LOCAL) {
197		MNT_ILOCK(mp);
198		mp->mnt_flag |= MNT_LOCAL;
199		MNT_IUNLOCK(mp);
200	}
201
202	xmp->nullm_flags |= NULLM_CACHE;
203	if (vfs_getopt(mp->mnt_optnew, "nocache", NULL, NULL) == 0)
204		xmp->nullm_flags &= ~NULLM_CACHE;
205
206	MNT_ILOCK(mp);
207	if ((xmp->nullm_flags & NULLM_CACHE) != 0) {
208		mp->mnt_kern_flag |= lowerrootvp->v_mount->mnt_kern_flag &
209		    (MNTK_SHARED_WRITES | MNTK_LOOKUP_SHARED |
210		    MNTK_EXTENDED_SHARED);
211	}
212	mp->mnt_kern_flag |= MNTK_LOOKUP_EXCL_DOTDOT;
213	MNT_IUNLOCK(mp);
214	mp->mnt_data = xmp;
215	vfs_getnewfsid(mp);
216	if ((xmp->nullm_flags & NULLM_CACHE) != 0) {
217		MNT_ILOCK(xmp->nullm_vfs);
218		TAILQ_INSERT_TAIL(&xmp->nullm_vfs->mnt_uppers, mp,
219		    mnt_upper_link);
220		MNT_IUNLOCK(xmp->nullm_vfs);
221	}
222
223	vfs_mountedfrom(mp, target);
224
225	NULLFSDEBUG("nullfs_mount: lower %s, alias at %s\n",
226		mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
227	return (0);
228}
229
230/*
231 * Free reference to null layer
232 */
233static int
234nullfs_unmount(mp, mntflags)
235	struct mount *mp;
236	int mntflags;
237{
238	struct null_mount *mntdata;
239	struct mount *ump;
240	int error, flags;
241
242	NULLFSDEBUG("nullfs_unmount: mp = %p\n", (void *)mp);
243
244	if (mntflags & MNT_FORCE)
245		flags = FORCECLOSE;
246	else
247		flags = 0;
248
249	/* There is 1 extra root vnode reference (nullm_rootvp). */
250	error = vflush(mp, 1, flags, curthread);
251	if (error)
252		return (error);
253
254	/*
255	 * Finally, throw away the null_mount structure
256	 */
257	mntdata = mp->mnt_data;
258	ump = mntdata->nullm_vfs;
259	if ((mntdata->nullm_flags & NULLM_CACHE) != 0) {
260		MNT_ILOCK(ump);
261		while ((ump->mnt_kern_flag & MNTK_VGONE_UPPER) != 0) {
262			ump->mnt_kern_flag |= MNTK_VGONE_WAITER;
263			msleep(&ump->mnt_uppers, &ump->mnt_mtx, 0, "vgnupw", 0);
264		}
265		TAILQ_REMOVE(&ump->mnt_uppers, mp, mnt_upper_link);
266		MNT_IUNLOCK(ump);
267	}
268	mp->mnt_data = NULL;
269	free(mntdata, M_NULLFSMNT);
270	return (0);
271}
272
273static int
274nullfs_root(mp, flags, vpp)
275	struct mount *mp;
276	int flags;
277	struct vnode **vpp;
278{
279	struct vnode *vp;
280
281	NULLFSDEBUG("nullfs_root(mp = %p, vp = %p->%p)\n", (void *)mp,
282	    (void *)MOUNTTONULLMOUNT(mp)->nullm_rootvp,
283	    (void *)NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp));
284
285	/*
286	 * Return locked reference to root.
287	 */
288	vp = MOUNTTONULLMOUNT(mp)->nullm_rootvp;
289	VREF(vp);
290
291	ASSERT_VOP_UNLOCKED(vp, "root vnode is locked");
292	vn_lock(vp, flags | LK_RETRY);
293	*vpp = vp;
294	return 0;
295}
296
297static int
298nullfs_quotactl(mp, cmd, uid, arg)
299	struct mount *mp;
300	int cmd;
301	uid_t uid;
302	void *arg;
303{
304	return VFS_QUOTACTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, uid, arg);
305}
306
307static int
308nullfs_statfs(mp, sbp)
309	struct mount *mp;
310	struct statfs *sbp;
311{
312	int error;
313	struct statfs mstat;
314
315	NULLFSDEBUG("nullfs_statfs(mp = %p, vp = %p->%p)\n", (void *)mp,
316	    (void *)MOUNTTONULLMOUNT(mp)->nullm_rootvp,
317	    (void *)NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp));
318
319	bzero(&mstat, sizeof(mstat));
320
321	error = VFS_STATFS(MOUNTTONULLMOUNT(mp)->nullm_vfs, &mstat);
322	if (error)
323		return (error);
324
325	/* now copy across the "interesting" information and fake the rest */
326	sbp->f_type = mstat.f_type;
327	sbp->f_flags = mstat.f_flags;
328	sbp->f_bsize = mstat.f_bsize;
329	sbp->f_iosize = mstat.f_iosize;
330	sbp->f_blocks = mstat.f_blocks;
331	sbp->f_bfree = mstat.f_bfree;
332	sbp->f_bavail = mstat.f_bavail;
333	sbp->f_files = mstat.f_files;
334	sbp->f_ffree = mstat.f_ffree;
335	return (0);
336}
337
338static int
339nullfs_sync(mp, waitfor)
340	struct mount *mp;
341	int waitfor;
342{
343	/*
344	 * XXX - Assumes no data cached at null layer.
345	 */
346	return (0);
347}
348
349static int
350nullfs_vget(mp, ino, flags, vpp)
351	struct mount *mp;
352	ino_t ino;
353	int flags;
354	struct vnode **vpp;
355{
356	int error;
357
358	KASSERT((flags & LK_TYPE_MASK) != 0,
359	    ("nullfs_vget: no lock requested"));
360
361	error = VFS_VGET(MOUNTTONULLMOUNT(mp)->nullm_vfs, ino, flags, vpp);
362	if (error != 0)
363		return (error);
364	return (null_nodeget(mp, *vpp, vpp));
365}
366
367static int
368nullfs_fhtovp(mp, fidp, flags, vpp)
369	struct mount *mp;
370	struct fid *fidp;
371	int flags;
372	struct vnode **vpp;
373{
374	int error;
375
376	error = VFS_FHTOVP(MOUNTTONULLMOUNT(mp)->nullm_vfs, fidp, flags,
377	    vpp);
378	if (error != 0)
379		return (error);
380	return (null_nodeget(mp, *vpp, vpp));
381}
382
383static int
384nullfs_extattrctl(mp, cmd, filename_vp, namespace, attrname)
385	struct mount *mp;
386	int cmd;
387	struct vnode *filename_vp;
388	int namespace;
389	const char *attrname;
390{
391
392	return (VFS_EXTATTRCTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd,
393	    filename_vp, namespace, attrname));
394}
395
396static void
397nullfs_reclaim_lowervp(struct mount *mp, struct vnode *lowervp)
398{
399	struct vnode *vp;
400
401	vp = null_hashget(mp, lowervp);
402	if (vp == NULL)
403		return;
404	vgone(vp);
405	vn_lock(lowervp, LK_EXCLUSIVE | LK_RETRY);
406}
407
408static struct vfsops null_vfsops = {
409	.vfs_extattrctl =	nullfs_extattrctl,
410	.vfs_fhtovp =		nullfs_fhtovp,
411	.vfs_init =		nullfs_init,
412	.vfs_mount =		nullfs_mount,
413	.vfs_quotactl =		nullfs_quotactl,
414	.vfs_root =		nullfs_root,
415	.vfs_statfs =		nullfs_statfs,
416	.vfs_sync =		nullfs_sync,
417	.vfs_uninit =		nullfs_uninit,
418	.vfs_unmount =		nullfs_unmount,
419	.vfs_vget =		nullfs_vget,
420	.vfs_reclaim_lowervp =	nullfs_reclaim_lowervp,
421};
422
423VFS_SET(null_vfsops, nullfs, VFCF_LOOPBACK | VFCF_JAIL);
424