null_vfsops.c revision 166774
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 166774 2007-02-15 22:08:35Z pjd $
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/kdb.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, struct thread *td)
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 deadlock.
108	 * (XXX) VOP_ISLOCKED is needed?
109	 */
110	if ((mp->mnt_vnodecovered->v_op == &null_vnodeops) &&
111		VOP_ISLOCKED(mp->mnt_vnodecovered, NULL)) {
112		VOP_UNLOCK(mp->mnt_vnodecovered, 0, td);
113		isvnunlocked = 1;
114	}
115	/*
116	 * Find lower node
117	 */
118	NDINIT(ndp, LOOKUP, FOLLOW|LOCKLEAF,
119		UIO_SYSSPACE, target, td);
120	error = namei(ndp);
121	/*
122	 * Re-lock vnode.
123	 */
124	if (isvnunlocked && !VOP_ISLOCKED(mp->mnt_vnodecovered, NULL))
125		vn_lock(mp->mnt_vnodecovered, LK_EXCLUSIVE | LK_RETRY, td);
126
127	if (error)
128		return (error);
129	NDFREE(ndp, NDF_ONLY_PNBUF);
130
131	/*
132	 * Sanity check on lower vnode
133	 */
134	lowerrootvp = ndp->ni_vp;
135
136	/*
137	 * Check multi null mount to avoid `lock against myself' panic.
138	 */
139	if (lowerrootvp == VTONULL(mp->mnt_vnodecovered)->null_lowervp) {
140		NULLFSDEBUG("nullfs_mount: multi null mount?\n");
141		vput(lowerrootvp);
142		return (EDEADLK);
143	}
144
145	xmp = (struct null_mount *) malloc(sizeof(struct null_mount),
146				M_NULLFSMNT, M_WAITOK);	/* XXX */
147
148	/*
149	 * Save reference to underlying FS
150	 */
151	xmp->nullm_vfs = lowerrootvp->v_mount;
152
153	/*
154	 * Save reference.  Each mount also holds
155	 * a reference on the root vnode.
156	 */
157	error = null_nodeget(mp, lowerrootvp, &vp);
158	/*
159	 * Make sure the node alias worked
160	 */
161	if (error) {
162		VOP_UNLOCK(vp, 0, td);
163		vrele(lowerrootvp);
164		free(xmp, M_NULLFSMNT);	/* XXX */
165		return (error);
166	}
167
168	/*
169	 * Keep a held reference to the root vnode.
170	 * It is vrele'd in nullfs_unmount.
171	 */
172	nullm_rootvp = vp;
173	nullm_rootvp->v_vflag |= VV_ROOT;
174	xmp->nullm_rootvp = nullm_rootvp;
175
176	/*
177	 * Unlock the node (either the lower or the alias)
178	 */
179	VOP_UNLOCK(vp, 0, td);
180
181	if (NULLVPTOLOWERVP(nullm_rootvp)->v_mount->mnt_flag & MNT_LOCAL) {
182		MNT_ILOCK(mp);
183		mp->mnt_flag |= MNT_LOCAL;
184		MNT_IUNLOCK(mp);
185	}
186	MNT_ILOCK(mp);
187	mp->mnt_kern_flag |= lowerrootvp->v_mount->mnt_kern_flag & MNTK_MPSAFE;
188	MNT_IUNLOCK(mp);
189	mp->mnt_data = (qaddr_t) xmp;
190	vfs_getnewfsid(mp);
191
192	vfs_mountedfrom(mp, target);
193
194	NULLFSDEBUG("nullfs_mount: lower %s, alias at %s\n",
195		mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
196	return (0);
197}
198
199/*
200 * Free reference to null layer
201 */
202static int
203nullfs_unmount(mp, mntflags, td)
204	struct mount *mp;
205	int mntflags;
206	struct thread *td;
207{
208	void *mntdata;
209	int error;
210	int flags = 0;
211
212	NULLFSDEBUG("nullfs_unmount: mp = %p\n", (void *)mp);
213
214	if (mntflags & MNT_FORCE)
215		flags |= FORCECLOSE;
216
217	/* There is 1 extra root vnode reference (nullm_rootvp). */
218	error = vflush(mp, 1, flags, td);
219	if (error)
220		return (error);
221
222	/*
223	 * Finally, throw away the null_mount structure
224	 */
225	mntdata = mp->mnt_data;
226	mp->mnt_data = 0;
227	free(mntdata, M_NULLFSMNT);
228	return 0;
229}
230
231static int
232nullfs_root(mp, flags, vpp, td)
233	struct mount *mp;
234	int flags;
235	struct vnode **vpp;
236	struct thread *td;
237{
238	struct vnode *vp;
239
240	NULLFSDEBUG("nullfs_root(mp = %p, vp = %p->%p)\n", (void *)mp,
241	    (void *)MOUNTTONULLMOUNT(mp)->nullm_rootvp,
242	    (void *)NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp));
243
244	/*
245	 * Return locked reference to root.
246	 */
247	vp = MOUNTTONULLMOUNT(mp)->nullm_rootvp;
248	VREF(vp);
249
250#ifdef NULLFS_DEBUG
251	if (VOP_ISLOCKED(vp, NULL)) {
252		kdb_enter("root vnode is locked.\n");
253		vrele(vp);
254		return (EDEADLK);
255	}
256#endif
257	vn_lock(vp, flags | LK_RETRY, td);
258	*vpp = vp;
259	return 0;
260}
261
262static int
263nullfs_quotactl(mp, cmd, uid, arg, td)
264	struct mount *mp;
265	int cmd;
266	uid_t uid;
267	void *arg;
268	struct thread *td;
269{
270	return VFS_QUOTACTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, uid, arg, td);
271}
272
273static int
274nullfs_statfs(mp, sbp, td)
275	struct mount *mp;
276	struct statfs *sbp;
277	struct thread *td;
278{
279	int error;
280	struct statfs mstat;
281
282	NULLFSDEBUG("nullfs_statfs(mp = %p, vp = %p->%p)\n", (void *)mp,
283	    (void *)MOUNTTONULLMOUNT(mp)->nullm_rootvp,
284	    (void *)NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp));
285
286	bzero(&mstat, sizeof(mstat));
287
288	error = VFS_STATFS(MOUNTTONULLMOUNT(mp)->nullm_vfs, &mstat, td);
289	if (error)
290		return (error);
291
292	/* now copy across the "interesting" information and fake the rest */
293	sbp->f_type = mstat.f_type;
294	sbp->f_flags = mstat.f_flags;
295	sbp->f_bsize = mstat.f_bsize;
296	sbp->f_iosize = mstat.f_iosize;
297	sbp->f_blocks = mstat.f_blocks;
298	sbp->f_bfree = mstat.f_bfree;
299	sbp->f_bavail = mstat.f_bavail;
300	sbp->f_files = mstat.f_files;
301	sbp->f_ffree = mstat.f_ffree;
302	return (0);
303}
304
305static int
306nullfs_sync(mp, waitfor, td)
307	struct mount *mp;
308	int waitfor;
309	struct thread *td;
310{
311	/*
312	 * XXX - Assumes no data cached at null layer.
313	 */
314	return (0);
315}
316
317static int
318nullfs_vget(mp, ino, flags, vpp)
319	struct mount *mp;
320	ino_t ino;
321	int flags;
322	struct vnode **vpp;
323{
324	int error;
325	error = VFS_VGET(MOUNTTONULLMOUNT(mp)->nullm_vfs, ino, flags, vpp);
326	if (error)
327		return (error);
328
329	return (null_nodeget(mp, *vpp, vpp));
330}
331
332static int
333nullfs_fhtovp(mp, fidp, vpp)
334	struct mount *mp;
335	struct fid *fidp;
336	struct vnode **vpp;
337{
338	int error;
339	error = VFS_FHTOVP(MOUNTTONULLMOUNT(mp)->nullm_vfs, fidp, vpp);
340	if (error)
341		return (error);
342
343	return (null_nodeget(mp, *vpp, vpp));
344}
345
346static int
347nullfs_extattrctl(mp, cmd, filename_vp, namespace, attrname, td)
348	struct mount *mp;
349	int cmd;
350	struct vnode *filename_vp;
351	int namespace;
352	const char *attrname;
353	struct thread *td;
354{
355	return VFS_EXTATTRCTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, filename_vp,
356	    namespace, attrname, td);
357}
358
359
360static struct vfsops null_vfsops = {
361	.vfs_extattrctl =	nullfs_extattrctl,
362	.vfs_fhtovp =		nullfs_fhtovp,
363	.vfs_init =		nullfs_init,
364	.vfs_mount =		nullfs_mount,
365	.vfs_quotactl =		nullfs_quotactl,
366	.vfs_root =		nullfs_root,
367	.vfs_statfs =		nullfs_statfs,
368	.vfs_sync =		nullfs_sync,
369	.vfs_uninit =		nullfs_uninit,
370	.vfs_unmount =		nullfs_unmount,
371	.vfs_vget =		nullfs_vget,
372};
373
374VFS_SET(null_vfsops, nullfs, VFCF_LOOPBACK);
375