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>
55 #include <fs/nullfs/null.h>
57 static MALLOC_DEFINE(M_NULLFSMNT, "nullfs_mount", "NULLFS mount structure");
59 static vfs_fhtovp_t nullfs_fhtovp;
60 static vfs_mount_t nullfs_mount;
61 static vfs_quotactl_t nullfs_quotactl;
62 static vfs_root_t nullfs_root;
63 static vfs_sync_t nullfs_sync;
64 static vfs_statfs_t nullfs_statfs;
65 static vfs_unmount_t nullfs_unmount;
66 static vfs_vget_t nullfs_vget;
67 static vfs_extattrctl_t nullfs_extattrctl;
68 static vfs_reclaim_lowervp_t nullfs_reclaim_lowervp;
70 /* Mount options that we support. */
71 static const char *nullfs_opts[] = {
83 nullfs_mount(struct mount *mp)
86 struct vnode *lowerrootvp, *vp;
87 struct vnode *nullm_rootvp;
88 struct null_mount *xmp;
89 struct thread *td = curthread;
91 int isvnunlocked = 0, len;
92 struct nameidata nd, *ndp = &nd;
94 NULLFSDEBUG("nullfs_mount(mp = %p)\n", (void *)mp);
96 if (!prison_allow(td->td_ucred, PR_ALLOW_MOUNT_NULLFS))
98 if (mp->mnt_flag & MNT_ROOTFS)
100 if (vfs_filteropt(mp->mnt_optnew, nullfs_opts))
106 if (mp->mnt_flag & MNT_UPDATE) {
108 * Only support update mounts for NFS export.
110 if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0))
119 error = vfs_getopt(mp->mnt_optnew, "target", (void **)&target, &len);
120 if (error || target[len - 1] != '\0')
124 * Unlock lower node to avoid possible deadlock.
126 if ((mp->mnt_vnodecovered->v_op == &null_vnodeops) &&
127 VOP_ISLOCKED(mp->mnt_vnodecovered) == LK_EXCLUSIVE) {
128 VOP_UNLOCK(mp->mnt_vnodecovered, 0);
134 NDINIT(ndp, LOOKUP, FOLLOW|LOCKLEAF, UIO_SYSSPACE, target, curthread);
139 * XXXKIB This is deadlock-prone as well.
142 vn_lock(mp->mnt_vnodecovered, LK_EXCLUSIVE | LK_RETRY);
146 NDFREE(ndp, NDF_ONLY_PNBUF);
149 * Sanity check on lower vnode
151 lowerrootvp = ndp->ni_vp;
154 * Check multi null mount to avoid `lock against myself' panic.
156 if (lowerrootvp == VTONULL(mp->mnt_vnodecovered)->null_lowervp) {
157 NULLFSDEBUG("nullfs_mount: multi null mount?\n");
162 xmp = (struct null_mount *) malloc(sizeof(struct null_mount),
163 M_NULLFSMNT, M_WAITOK | M_ZERO);
166 * Save reference to underlying FS
168 xmp->nullm_vfs = lowerrootvp->v_mount;
171 * Save reference. Each mount also holds
172 * a reference on the root vnode.
174 error = null_nodeget(mp, lowerrootvp, &vp);
176 * Make sure the node alias worked
179 free(xmp, M_NULLFSMNT);
184 * Keep a held reference to the root vnode.
185 * It is vrele'd in nullfs_unmount.
188 nullm_rootvp->v_vflag |= VV_ROOT;
189 xmp->nullm_rootvp = nullm_rootvp;
192 * Unlock the node (either the lower or the alias)
196 if (NULLVPTOLOWERVP(nullm_rootvp)->v_mount->mnt_flag & MNT_LOCAL) {
198 mp->mnt_flag |= MNT_LOCAL;
202 xmp->nullm_flags |= NULLM_CACHE;
203 if (vfs_getopt(mp->mnt_optnew, "nocache", NULL, NULL) == 0)
204 xmp->nullm_flags &= ~NULLM_CACHE;
207 if ((xmp->nullm_flags & NULLM_CACHE) != 0) {
208 mp->mnt_kern_flag |= lowerrootvp->v_mount->mnt_kern_flag &
209 (MNTK_MPSAFE | MNTK_SHARED_WRITES | MNTK_LOOKUP_SHARED |
210 MNTK_EXTENDED_SHARED);
212 mp->mnt_kern_flag |= MNTK_LOOKUP_EXCL_DOTDOT;
216 if ((xmp->nullm_flags & NULLM_CACHE) != 0) {
217 MNT_ILOCK(xmp->nullm_vfs);
218 TAILQ_INSERT_TAIL(&xmp->nullm_vfs->mnt_uppers, mp,
220 MNT_IUNLOCK(xmp->nullm_vfs);
223 vfs_mountedfrom(mp, target);
225 NULLFSDEBUG("nullfs_mount: lower %s, alias at %s\n",
226 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
231 * Free reference to null layer
234 nullfs_unmount(mp, mntflags)
238 struct null_mount *mntdata;
242 NULLFSDEBUG("nullfs_unmount: mp = %p\n", (void *)mp);
244 if (mntflags & MNT_FORCE)
249 /* There is 1 extra root vnode reference (nullm_rootvp). */
250 error = vflush(mp, 1, flags, curthread);
255 * Finally, throw away the null_mount structure
257 mntdata = mp->mnt_data;
258 ump = mntdata->nullm_vfs;
259 if ((mntdata->nullm_flags & NULLM_CACHE) != 0) {
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);
265 TAILQ_REMOVE(&ump->mnt_uppers, mp, mnt_upper_link);
269 free(mntdata, M_NULLFSMNT);
274 nullfs_root(mp, flags, vpp)
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));
286 * Return locked reference to root.
288 vp = MOUNTTONULLMOUNT(mp)->nullm_rootvp;
291 ASSERT_VOP_UNLOCKED(vp, "root vnode is locked");
292 vn_lock(vp, flags | LK_RETRY);
298 nullfs_quotactl(mp, cmd, uid, arg)
304 return VFS_QUOTACTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, uid, arg);
308 nullfs_statfs(mp, sbp)
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));
319 bzero(&mstat, sizeof(mstat));
321 error = VFS_STATFS(MOUNTTONULLMOUNT(mp)->nullm_vfs, &mstat);
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;
339 nullfs_sync(mp, waitfor)
344 * XXX - Assumes no data cached at null layer.
350 nullfs_vget(mp, ino, flags, vpp)
358 KASSERT((flags & LK_TYPE_MASK) != 0,
359 ("nullfs_vget: no lock requested"));
361 error = VFS_VGET(MOUNTTONULLMOUNT(mp)->nullm_vfs, ino, flags, vpp);
364 return (null_nodeget(mp, *vpp, vpp));
368 nullfs_fhtovp(mp, fidp, flags, vpp)
376 error = VFS_FHTOVP(MOUNTTONULLMOUNT(mp)->nullm_vfs, fidp, flags,
380 return (null_nodeget(mp, *vpp, vpp));
384 nullfs_extattrctl(mp, cmd, filename_vp, namespace, attrname)
387 struct vnode *filename_vp;
389 const char *attrname;
392 return (VFS_EXTATTRCTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd,
393 filename_vp, namespace, attrname));
397 nullfs_reclaim_lowervp(struct mount *mp, struct vnode *lowervp)
401 vp = null_hashget(mp, lowervp);
405 vn_lock(lowervp, LK_EXCLUSIVE | LK_RETRY);
408 static 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,
423 VFS_SET(null_vfsops, nullfs, VFCF_LOOPBACK | VFCF_JAIL);