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 * 3. 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;
73 nullfs_mount(struct mount *mp)
76 struct vnode *lowerrootvp, *vp;
77 struct vnode *nullm_rootvp;
78 struct null_mount *xmp;
79 struct thread *td = curthread;
81 int isvnunlocked = 0, len;
82 struct nameidata nd, *ndp = &nd;
84 NULLFSDEBUG("nullfs_mount(mp = %p)\n", (void *)mp);
86 if (!prison_allow(td->td_ucred, PR_ALLOW_MOUNT_NULLFS))
88 if (mp->mnt_flag & MNT_ROOTFS)
94 if (mp->mnt_flag & MNT_UPDATE) {
96 * Only support update mounts for NFS export.
98 if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0))
107 error = vfs_getopt(mp->mnt_optnew, "target", (void **)&target, &len);
108 if (error || target[len - 1] != '\0')
112 * Unlock lower node to avoid possible deadlock.
114 if ((mp->mnt_vnodecovered->v_op == &null_vnodeops) &&
115 VOP_ISLOCKED(mp->mnt_vnodecovered) == LK_EXCLUSIVE) {
116 VOP_UNLOCK(mp->mnt_vnodecovered, 0);
122 NDINIT(ndp, LOOKUP, FOLLOW|LOCKLEAF, UIO_SYSSPACE, target, curthread);
127 * XXXKIB This is deadlock-prone as well.
130 vn_lock(mp->mnt_vnodecovered, LK_EXCLUSIVE | LK_RETRY);
134 NDFREE(ndp, NDF_ONLY_PNBUF);
137 * Sanity check on lower vnode
139 lowerrootvp = ndp->ni_vp;
142 * Check multi null mount to avoid `lock against myself' panic.
144 if (lowerrootvp == VTONULL(mp->mnt_vnodecovered)->null_lowervp) {
145 NULLFSDEBUG("nullfs_mount: multi null mount?\n");
150 xmp = (struct null_mount *) malloc(sizeof(struct null_mount),
151 M_NULLFSMNT, M_WAITOK | M_ZERO);
154 * Save reference to underlying FS
156 xmp->nullm_vfs = lowerrootvp->v_mount;
159 * Save reference. Each mount also holds
160 * a reference on the root vnode.
162 error = null_nodeget(mp, lowerrootvp, &vp);
164 * Make sure the node alias worked
167 free(xmp, M_NULLFSMNT);
172 * Keep a held reference to the root vnode.
173 * It is vrele'd in nullfs_unmount.
176 nullm_rootvp->v_vflag |= VV_ROOT;
177 xmp->nullm_rootvp = nullm_rootvp;
180 * Unlock the node (either the lower or the alias)
184 if (NULLVPTOLOWERVP(nullm_rootvp)->v_mount->mnt_flag & MNT_LOCAL) {
186 mp->mnt_flag |= MNT_LOCAL;
190 xmp->nullm_flags |= NULLM_CACHE;
191 if (vfs_getopt(mp->mnt_optnew, "nocache", NULL, NULL) == 0 ||
192 (xmp->nullm_vfs->mnt_kern_flag & MNTK_NULL_NOCACHE) != 0)
193 xmp->nullm_flags &= ~NULLM_CACHE;
196 if ((xmp->nullm_flags & NULLM_CACHE) != 0) {
197 mp->mnt_kern_flag |= lowerrootvp->v_mount->mnt_kern_flag &
198 (MNTK_SHARED_WRITES | MNTK_LOOKUP_SHARED |
199 MNTK_EXTENDED_SHARED);
201 mp->mnt_kern_flag |= MNTK_LOOKUP_EXCL_DOTDOT;
202 mp->mnt_kern_flag |= lowerrootvp->v_mount->mnt_kern_flag &
203 (MNTK_USES_BCACHE | MNTK_NO_IOPF | MNTK_UNMAPPED_BUFS);
207 if ((xmp->nullm_flags & NULLM_CACHE) != 0) {
208 MNT_ILOCK(xmp->nullm_vfs);
209 TAILQ_INSERT_TAIL(&xmp->nullm_vfs->mnt_uppers, mp,
211 MNT_IUNLOCK(xmp->nullm_vfs);
214 vfs_mountedfrom(mp, target);
216 NULLFSDEBUG("nullfs_mount: lower %s, alias at %s\n",
217 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
222 * Free reference to null layer
225 nullfs_unmount(mp, mntflags)
229 struct null_mount *mntdata;
233 NULLFSDEBUG("nullfs_unmount: mp = %p\n", (void *)mp);
235 if (mntflags & MNT_FORCE)
240 /* There is 1 extra root vnode reference (nullm_rootvp). */
241 error = vflush(mp, 1, flags, curthread);
246 * Finally, throw away the null_mount structure
248 mntdata = mp->mnt_data;
249 ump = mntdata->nullm_vfs;
250 if ((mntdata->nullm_flags & NULLM_CACHE) != 0) {
252 while ((ump->mnt_kern_flag & MNTK_VGONE_UPPER) != 0) {
253 ump->mnt_kern_flag |= MNTK_VGONE_WAITER;
254 msleep(&ump->mnt_uppers, &ump->mnt_mtx, 0, "vgnupw", 0);
256 TAILQ_REMOVE(&ump->mnt_uppers, mp, mnt_upper_link);
260 free(mntdata, M_NULLFSMNT);
265 nullfs_root(mp, flags, vpp)
272 NULLFSDEBUG("nullfs_root(mp = %p, vp = %p->%p)\n", (void *)mp,
273 (void *)MOUNTTONULLMOUNT(mp)->nullm_rootvp,
274 (void *)NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp));
277 * Return locked reference to root.
279 vp = MOUNTTONULLMOUNT(mp)->nullm_rootvp;
282 ASSERT_VOP_UNLOCKED(vp, "root vnode is locked");
283 vn_lock(vp, flags | LK_RETRY);
289 nullfs_quotactl(mp, cmd, uid, arg)
295 return VFS_QUOTACTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, uid, arg);
299 nullfs_statfs(mp, sbp)
304 struct statfs *mstat;
306 NULLFSDEBUG("nullfs_statfs(mp = %p, vp = %p->%p)\n", (void *)mp,
307 (void *)MOUNTTONULLMOUNT(mp)->nullm_rootvp,
308 (void *)NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp));
310 mstat = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK | M_ZERO);
312 error = VFS_STATFS(MOUNTTONULLMOUNT(mp)->nullm_vfs, mstat);
314 free(mstat, M_STATFS);
318 /* now copy across the "interesting" information and fake the rest */
319 sbp->f_type = mstat->f_type;
320 sbp->f_flags = (sbp->f_flags & (MNT_RDONLY | MNT_NOEXEC | MNT_NOSUID |
321 MNT_UNION | MNT_NOSYMFOLLOW)) | (mstat->f_flags & ~MNT_ROOTFS);
322 sbp->f_bsize = mstat->f_bsize;
323 sbp->f_iosize = mstat->f_iosize;
324 sbp->f_blocks = mstat->f_blocks;
325 sbp->f_bfree = mstat->f_bfree;
326 sbp->f_bavail = mstat->f_bavail;
327 sbp->f_files = mstat->f_files;
328 sbp->f_ffree = mstat->f_ffree;
330 free(mstat, M_STATFS);
335 nullfs_sync(mp, waitfor)
340 * XXX - Assumes no data cached at null layer.
346 nullfs_vget(mp, ino, flags, vpp)
354 KASSERT((flags & LK_TYPE_MASK) != 0,
355 ("nullfs_vget: no lock requested"));
357 error = VFS_VGET(MOUNTTONULLMOUNT(mp)->nullm_vfs, ino, flags, vpp);
360 return (null_nodeget(mp, *vpp, vpp));
364 nullfs_fhtovp(mp, fidp, flags, vpp)
372 error = VFS_FHTOVP(MOUNTTONULLMOUNT(mp)->nullm_vfs, fidp, flags,
376 return (null_nodeget(mp, *vpp, vpp));
380 nullfs_extattrctl(mp, cmd, filename_vp, namespace, attrname)
383 struct vnode *filename_vp;
385 const char *attrname;
388 return (VFS_EXTATTRCTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd,
389 filename_vp, namespace, attrname));
393 nullfs_reclaim_lowervp(struct mount *mp, struct vnode *lowervp)
397 vp = null_hashget(mp, lowervp);
400 VTONULL(vp)->null_flags |= NULLV_NOUNLOCK;
406 nullfs_unlink_lowervp(struct mount *mp, struct vnode *lowervp)
409 struct null_node *xp;
411 vp = null_hashget(mp, lowervp);
415 xp->null_flags |= NULLV_DROP | NULLV_NOUNLOCK;
419 if (vp->v_usecount == 0) {
421 * If vunref() dropped the last use reference on the
422 * nullfs vnode, it must be reclaimed, and its lock
423 * was split from the lower vnode lock. Need to do
424 * extra unlock before allowing the final vdrop() to
427 KASSERT((vp->v_iflag & VI_DOOMED) != 0,
428 ("not reclaimed nullfs vnode %p", vp));
432 * Otherwise, the nullfs vnode still shares the lock
433 * with the lower vnode, and must not be unlocked.
434 * Also clear the NULLV_NOUNLOCK, the flag is not
435 * relevant for future reclamations.
437 ASSERT_VOP_ELOCKED(vp, "unlink_lowervp");
438 KASSERT((vp->v_iflag & VI_DOOMED) == 0,
439 ("reclaimed nullfs vnode %p", vp));
440 xp->null_flags &= ~NULLV_NOUNLOCK;
445 static struct vfsops null_vfsops = {
446 .vfs_extattrctl = nullfs_extattrctl,
447 .vfs_fhtovp = nullfs_fhtovp,
448 .vfs_init = nullfs_init,
449 .vfs_mount = nullfs_mount,
450 .vfs_quotactl = nullfs_quotactl,
451 .vfs_root = nullfs_root,
452 .vfs_statfs = nullfs_statfs,
453 .vfs_sync = nullfs_sync,
454 .vfs_uninit = nullfs_uninit,
455 .vfs_unmount = nullfs_unmount,
456 .vfs_vget = nullfs_vget,
457 .vfs_reclaim_lowervp = nullfs_reclaim_lowervp,
458 .vfs_unlink_lowervp = nullfs_unlink_lowervp,
461 VFS_SET(null_vfsops, nullfs, VFCF_LOOPBACK | VFCF_JAIL);