2 * Copyright (c) 1992, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software donated to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
32 * @(#)null_vfsops.c 8.2 (Berkeley) 1/21/94
34 * @(#)lofs_vfsops.c 1.2 (Berkeley) 6/18/92
40 * (See null_vnops.c for a description of what this does.)
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/fcntl.h>
46 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/mount.h>
50 #include <sys/namei.h>
52 #include <sys/vnode.h>
54 #include <fs/nullfs/null.h>
56 static MALLOC_DEFINE(M_NULLFSMNT, "nullfs_mount", "NULLFS mount structure");
58 static vfs_fhtovp_t nullfs_fhtovp;
59 static vfs_mount_t nullfs_mount;
60 static vfs_quotactl_t nullfs_quotactl;
61 static vfs_root_t nullfs_root;
62 static vfs_sync_t nullfs_sync;
63 static vfs_statfs_t nullfs_statfs;
64 static vfs_unmount_t nullfs_unmount;
65 static vfs_vget_t nullfs_vget;
66 static vfs_extattrctl_t nullfs_extattrctl;
72 nullfs_mount(struct mount *mp)
75 struct vnode *lowerrootvp, *vp;
76 struct vnode *nullm_rootvp;
77 struct null_mount *xmp;
79 int isvnunlocked = 0, len;
80 struct nameidata nd, *ndp = &nd;
82 NULLFSDEBUG("nullfs_mount(mp = %p)\n", (void *)mp);
84 if (mp->mnt_flag & MNT_ROOTFS)
89 if (mp->mnt_flag & MNT_UPDATE) {
91 * Only support update mounts for NFS export.
93 if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0))
102 error = vfs_getopt(mp->mnt_optnew, "target", (void **)&target, &len);
103 if (error || target[len - 1] != '\0')
107 * Unlock lower node to avoid deadlock.
108 * (XXX) VOP_ISLOCKED is needed?
110 if ((mp->mnt_vnodecovered->v_op == &null_vnodeops) &&
111 VOP_ISLOCKED(mp->mnt_vnodecovered)) {
112 VOP_UNLOCK(mp->mnt_vnodecovered, 0);
118 NDINIT(ndp, LOOKUP, FOLLOW|LOCKLEAF, UIO_SYSSPACE, target, curthread);
123 if (isvnunlocked && !VOP_ISLOCKED(mp->mnt_vnodecovered))
124 vn_lock(mp->mnt_vnodecovered, LK_EXCLUSIVE | LK_RETRY);
128 NDFREE(ndp, NDF_ONLY_PNBUF);
131 * Sanity check on lower vnode
133 lowerrootvp = ndp->ni_vp;
136 * Check multi null mount to avoid `lock against myself' panic.
138 if (lowerrootvp == VTONULL(mp->mnt_vnodecovered)->null_lowervp) {
139 NULLFSDEBUG("nullfs_mount: multi null mount?\n");
144 xmp = (struct null_mount *) malloc(sizeof(struct null_mount),
145 M_NULLFSMNT, M_WAITOK); /* XXX */
148 * Save reference to underlying FS
150 xmp->nullm_vfs = lowerrootvp->v_mount;
153 * Save reference. Each mount also holds
154 * a reference on the root vnode.
156 error = null_nodeget(mp, lowerrootvp, &vp);
158 * Make sure the node alias worked
163 free(xmp, M_NULLFSMNT); /* XXX */
168 * Keep a held reference to the root vnode.
169 * It is vrele'd in nullfs_unmount.
172 nullm_rootvp->v_vflag |= VV_ROOT;
173 xmp->nullm_rootvp = nullm_rootvp;
176 * Unlock the node (either the lower or the alias)
180 if (NULLVPTOLOWERVP(nullm_rootvp)->v_mount->mnt_flag & MNT_LOCAL) {
182 mp->mnt_flag |= MNT_LOCAL;
186 mp->mnt_kern_flag |= lowerrootvp->v_mount->mnt_kern_flag & MNTK_MPSAFE;
191 vfs_mountedfrom(mp, target);
193 NULLFSDEBUG("nullfs_mount: lower %s, alias at %s\n",
194 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
199 * Free reference to null layer
202 nullfs_unmount(mp, mntflags)
210 NULLFSDEBUG("nullfs_unmount: mp = %p\n", (void *)mp);
212 if (mntflags & MNT_FORCE)
215 /* There is 1 extra root vnode reference (nullm_rootvp). */
216 error = vflush(mp, 1, flags, curthread);
221 * Finally, throw away the null_mount structure
223 mntdata = mp->mnt_data;
225 free(mntdata, M_NULLFSMNT);
230 nullfs_root(mp, flags, vpp)
237 NULLFSDEBUG("nullfs_root(mp = %p, vp = %p->%p)\n", (void *)mp,
238 (void *)MOUNTTONULLMOUNT(mp)->nullm_rootvp,
239 (void *)NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp));
242 * Return locked reference to root.
244 vp = MOUNTTONULLMOUNT(mp)->nullm_rootvp;
248 if (VOP_ISLOCKED(vp))
249 panic("root vnode is locked.\n");
251 vn_lock(vp, flags | LK_RETRY);
257 nullfs_quotactl(mp, cmd, uid, arg)
263 return VFS_QUOTACTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, uid, arg);
267 nullfs_statfs(mp, sbp)
274 NULLFSDEBUG("nullfs_statfs(mp = %p, vp = %p->%p)\n", (void *)mp,
275 (void *)MOUNTTONULLMOUNT(mp)->nullm_rootvp,
276 (void *)NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp));
278 bzero(&mstat, sizeof(mstat));
280 error = VFS_STATFS(MOUNTTONULLMOUNT(mp)->nullm_vfs, &mstat);
284 /* now copy across the "interesting" information and fake the rest */
285 sbp->f_type = mstat.f_type;
286 sbp->f_flags = mstat.f_flags;
287 sbp->f_bsize = mstat.f_bsize;
288 sbp->f_iosize = mstat.f_iosize;
289 sbp->f_blocks = mstat.f_blocks;
290 sbp->f_bfree = mstat.f_bfree;
291 sbp->f_bavail = mstat.f_bavail;
292 sbp->f_files = mstat.f_files;
293 sbp->f_ffree = mstat.f_ffree;
298 nullfs_sync(mp, waitfor)
303 * XXX - Assumes no data cached at null layer.
309 nullfs_vget(mp, ino, flags, vpp)
316 error = VFS_VGET(MOUNTTONULLMOUNT(mp)->nullm_vfs, ino, flags, vpp);
320 return (null_nodeget(mp, *vpp, vpp));
324 nullfs_fhtovp(mp, fidp, vpp)
330 error = VFS_FHTOVP(MOUNTTONULLMOUNT(mp)->nullm_vfs, fidp, vpp);
334 return (null_nodeget(mp, *vpp, vpp));
338 nullfs_extattrctl(mp, cmd, filename_vp, namespace, attrname)
341 struct vnode *filename_vp;
343 const char *attrname;
345 return VFS_EXTATTRCTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, filename_vp,
346 namespace, attrname);
350 static struct vfsops null_vfsops = {
351 .vfs_extattrctl = nullfs_extattrctl,
352 .vfs_fhtovp = nullfs_fhtovp,
353 .vfs_init = nullfs_init,
354 .vfs_mount = nullfs_mount,
355 .vfs_quotactl = nullfs_quotactl,
356 .vfs_root = nullfs_root,
357 .vfs_statfs = nullfs_statfs,
358 .vfs_sync = nullfs_sync,
359 .vfs_uninit = nullfs_uninit,
360 .vfs_unmount = nullfs_unmount,
361 .vfs_vget = nullfs_vget,
364 VFS_SET(null_vfsops, nullfs, VFCF_LOOPBACK);