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