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