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_nmount_t nullfs_mount; 61 static vfs_quotactl_t nullfs_quotactl; 62 static vfs_root_t nullfs_root; 63 static vfs_start_t nullfs_start; 64 static vfs_sync_t nullfs_sync; 65 static vfs_statfs_t nullfs_statfs; 66 static vfs_unmount_t nullfs_unmount; 67 static vfs_vget_t nullfs_vget; 68 static vfs_vptofh_t nullfs_vptofh; 69 static vfs_extattrctl_t nullfs_extattrctl; 70 71 /* 72 * Mount null layer 73 */ 74 static int 75 nullfs_mount(mp, ndp, td) 76 struct mount *mp; 77 struct nameidata *ndp; 78 struct thread *td; 79 { 80 int error = 0; 81 struct vnode *lowerrootvp, *vp; 82 struct vnode *nullm_rootvp; 83 struct null_mount *xmp; 84 char *target; 85 size_t size; 86 int isvnunlocked = 0, len; 87 88 NULLFSDEBUG("nullfs_mount(mp = %p)\n", (void *)mp); 89 90 /* 91 * Update is a no-op 92 */ 93 if (mp->mnt_flag & MNT_UPDATE) { 94 return (EOPNOTSUPP); 95 /* return VFS_MOUNT(MOUNTTONULLMOUNT(mp)->nullm_vfs, path, data, ndp, td);*/ 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_vnodeop_p) && 110 VOP_ISLOCKED(mp->mnt_vnodecovered, NULL)) { 111 VOP_UNLOCK(mp->mnt_vnodecovered, 0, td); 112 isvnunlocked = 1; 113 } 114 /* 115 * Find lower node 116 */ 117 NDINIT(ndp, LOOKUP, FOLLOW|WANTPARENT|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, td); 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 vrele(ndp->ni_dvp); 136 ndp->ni_dvp = NULLVP; 137 138 /* 139 * Check multi null mount to avoid `lock against myself' panic. 140 */ 141 if (lowerrootvp == VTONULL(mp->mnt_vnodecovered)->null_lowervp) { 142 NULLFSDEBUG("nullfs_mount: multi null mount?\n"); 143 vput(lowerrootvp); 144 return (EDEADLK); 145 } 146 147 xmp = (struct null_mount *) malloc(sizeof(struct null_mount), 148 M_NULLFSMNT, M_WAITOK); /* XXX */ 149 150 /* 151 * Save reference to underlying FS 152 */ 153 xmp->nullm_vfs = lowerrootvp->v_mount; 154 155 /* 156 * Save reference. Each mount also holds 157 * a reference on the root vnode. 158 */ 159 error = null_nodeget(mp, lowerrootvp, &vp); 160 /* 161 * Make sure the node alias worked 162 */ 163 if (error) { 164 VOP_UNLOCK(vp, 0, td); 165 vrele(lowerrootvp); 166 free(xmp, M_NULLFSMNT); /* XXX */ 167 return (error); 168 } 169 170 /* 171 * Keep a held reference to the root vnode. 172 * It is vrele'd in nullfs_unmount. 173 */ 174 nullm_rootvp = vp; 175 nullm_rootvp->v_vflag |= VV_ROOT; 176 xmp->nullm_rootvp = nullm_rootvp; 177 178 /* 179 * Unlock the node (either the lower or the alias) 180 */ 181 VOP_UNLOCK(vp, 0, td); 182 183 if (NULLVPTOLOWERVP(nullm_rootvp)->v_mount->mnt_flag & MNT_LOCAL) 184 mp->mnt_flag |= MNT_LOCAL; 185 mp->mnt_data = (qaddr_t) xmp; 186 vfs_getnewfsid(mp); 187 188 (void) copystr(target, mp->mnt_stat.f_mntfromname, 189 MNAMELEN - 1, &size); 190 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 191 (void)nullfs_statfs(mp, &mp->mnt_stat, td); 192 NULLFSDEBUG("nullfs_mount: lower %s, alias at %s\n", 193 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname); 194 return (0); 195 } 196 197 /* 198 * VFS start. Nothing needed here - the start routine 199 * on the underlying filesystem will have been called 200 * when that filesystem was mounted. 201 */ 202 static int 203 nullfs_start(mp, flags, td) 204 struct mount *mp; 205 int flags; 206 struct thread *td; 207 { 208 return (0); 209 /* return VFS_START(MOUNTTONULLMOUNT(mp)->nullm_vfs, flags, td); */ 210 } 211 212 /* 213 * Free reference to null layer 214 */ 215 static int 216 nullfs_unmount(mp, mntflags, td) 217 struct mount *mp; 218 int mntflags; 219 struct thread *td; 220 { 221 void *mntdata; 222 int error; 223 int flags = 0; 224 225 NULLFSDEBUG("nullfs_unmount: mp = %p\n", (void *)mp); 226 227 if (mntflags & MNT_FORCE) 228 flags |= FORCECLOSE; 229 230 /* There is 1 extra root vnode reference (nullm_rootvp). */ 231 error = vflush(mp, 1, flags, td); 232 if (error) 233 return (error); 234 235 /* 236 * Finally, throw away the null_mount structure 237 */ 238 mntdata = mp->mnt_data; 239 mp->mnt_data = 0; 240 free(mntdata, M_NULLFSMNT); 241 return 0; 242 } 243 244 static int 245 nullfs_root(mp, vpp, td) 246 struct mount *mp; 247 struct vnode **vpp; 248 struct thread *td; 249 { 250 struct vnode *vp; 251 252 NULLFSDEBUG("nullfs_root(mp = %p, vp = %p->%p)\n", (void *)mp, 253 (void *)MOUNTTONULLMOUNT(mp)->nullm_rootvp, 254 (void *)NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp)); 255 256 /* 257 * Return locked reference to root. 258 */ 259 vp = MOUNTTONULLMOUNT(mp)->nullm_rootvp; 260 VREF(vp); 261 262 #ifdef NULLFS_DEBUG 263 if (VOP_ISLOCKED(vp, NULL)) { 264 kdb_enter("root vnode is locked.\n"); 265 vrele(vp); 266 return (EDEADLK); 267 } 268 #endif 269 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 270 *vpp = vp; 271 return 0; 272 } 273 274 static int 275 nullfs_quotactl(mp, cmd, uid, arg, td) 276 struct mount *mp; 277 int cmd; 278 uid_t uid; 279 caddr_t arg; 280 struct thread *td; 281 { 282 return VFS_QUOTACTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, uid, arg, td); 283 } 284 285 static int 286 nullfs_statfs(mp, sbp, td) 287 struct mount *mp; 288 struct statfs *sbp; 289 struct thread *td; 290 { 291 int error; 292 struct statfs mstat; 293 294 NULLFSDEBUG("nullfs_statfs(mp = %p, vp = %p->%p)\n", (void *)mp, 295 (void *)MOUNTTONULLMOUNT(mp)->nullm_rootvp, 296 (void *)NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp)); 297 298 bzero(&mstat, sizeof(mstat)); 299 300 error = VFS_STATFS(MOUNTTONULLMOUNT(mp)->nullm_vfs, &mstat, td); 301 if (error) 302 return (error); 303 304 /* now copy across the "interesting" information and fake the rest */ 305 sbp->f_type = mstat.f_type; 306 sbp->f_flags = mstat.f_flags; 307 sbp->f_bsize = mstat.f_bsize; 308 sbp->f_iosize = mstat.f_iosize; 309 sbp->f_blocks = mstat.f_blocks; 310 sbp->f_bfree = mstat.f_bfree; 311 sbp->f_bavail = mstat.f_bavail; 312 sbp->f_files = mstat.f_files; 313 sbp->f_ffree = mstat.f_ffree; 314 if (sbp != &mp->mnt_stat) { 315 bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid)); 316 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN); 317 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN); 318 } 319 return (0); 320 } 321 322 static int 323 nullfs_sync(mp, waitfor, cred, td) 324 struct mount *mp; 325 int waitfor; 326 struct ucred *cred; 327 struct thread *td; 328 { 329 /* 330 * XXX - Assumes no data cached at null layer. 331 */ 332 return (0); 333 } 334 335 static int 336 nullfs_vget(mp, ino, flags, vpp) 337 struct mount *mp; 338 ino_t ino; 339 int flags; 340 struct vnode **vpp; 341 { 342 int error; 343 error = VFS_VGET(MOUNTTONULLMOUNT(mp)->nullm_vfs, ino, flags, vpp); 344 if (error) 345 return (error); 346 347 return (null_nodeget(mp, *vpp, vpp)); 348 } 349 350 static int 351 nullfs_fhtovp(mp, fidp, vpp) 352 struct mount *mp; 353 struct fid *fidp; 354 struct vnode **vpp; 355 { 356 int error; 357 error = VFS_FHTOVP(MOUNTTONULLMOUNT(mp)->nullm_vfs, fidp, vpp); 358 if (error) 359 return (error); 360 361 return (null_nodeget(mp, *vpp, vpp)); 362 } 363 364 static int 365 nullfs_checkexp(mp, nam, extflagsp, credanonp) 366 struct mount *mp; 367 struct sockaddr *nam; 368 int *extflagsp; 369 struct ucred **credanonp; 370 { 371 372 return VFS_CHECKEXP(MOUNTTONULLMOUNT(mp)->nullm_vfs, nam, 373 extflagsp, credanonp); 374 } 375 376 static int 377 nullfs_vptofh(vp, fhp) 378 struct vnode *vp; 379 struct fid *fhp; 380 { 381 struct vnode *lvp; 382 383 lvp = NULLVPTOLOWERVP(vp); 384 return VFS_VPTOFH(lvp, fhp); 385 } 386 387 static int 388 nullfs_extattrctl(mp, cmd, filename_vp, namespace, attrname, td) 389 struct mount *mp; 390 int cmd; 391 struct vnode *filename_vp; 392 int namespace; 393 const char *attrname; 394 struct thread *td; 395 { 396 return VFS_EXTATTRCTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, filename_vp, 397 namespace, attrname, td); 398 } 399 400 401 static struct vfsops null_vfsops = { 402 .vfs_checkexp = nullfs_checkexp, 403 .vfs_extattrctl = nullfs_extattrctl, 404 .vfs_fhtovp = nullfs_fhtovp, 405 .vfs_init = nullfs_init, 406 .vfs_nmount = nullfs_mount, 407 .vfs_quotactl = nullfs_quotactl, 408 .vfs_root = nullfs_root, 409 .vfs_start = nullfs_start, 410 .vfs_statfs = nullfs_statfs, 411 .vfs_sync = nullfs_sync, 412 .vfs_uninit = nullfs_uninit, 413 .vfs_unmount = nullfs_unmount, 414 .vfs_vget = nullfs_vget, 415 .vfs_vptofh = nullfs_vptofh, 416 }; 417 418 VFS_SET(null_vfsops, nullfs, VFCF_LOOPBACK); 419