null_vfsops.c revision 159019
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: head/sys/fs/nullfs/null_vfsops.c 159019 2006-05-28 20:09:18Z rodrigc $ 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 56static MALLOC_DEFINE(M_NULLFSMNT, "nullfs_mount", "NULLFS mount structure"); 57 58static vfs_fhtovp_t nullfs_fhtovp; 59static vfs_checkexp_t nullfs_checkexp; 60static vfs_mount_t nullfs_mount; 61static vfs_quotactl_t nullfs_quotactl; 62static vfs_root_t nullfs_root; 63static vfs_sync_t nullfs_sync; 64static vfs_statfs_t nullfs_statfs; 65static vfs_unmount_t nullfs_unmount; 66static vfs_vget_t nullfs_vget; 67static vfs_vptofh_t nullfs_vptofh; 68static vfs_extattrctl_t nullfs_extattrctl; 69 70/* 71 * Mount null layer 72 */ 73static int 74nullfs_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 /* 93 * Only support update mounts for NFS export. 94 */ 95 if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0)) 96 return (0); 97 else 98 return (EOPNOTSUPP); 99 } 100 101 /* 102 * Get argument 103 */ 104 error = vfs_getopt(mp->mnt_optnew, "target", (void **)&target, &len); 105 if (error || target[len - 1] != '\0') 106 return (EINVAL); 107 108 /* 109 * Unlock lower node to avoid deadlock. 110 * (XXX) VOP_ISLOCKED is needed? 111 */ 112 if ((mp->mnt_vnodecovered->v_op == &null_vnodeops) && 113 VOP_ISLOCKED(mp->mnt_vnodecovered, NULL)) { 114 VOP_UNLOCK(mp->mnt_vnodecovered, 0, td); 115 isvnunlocked = 1; 116 } 117 /* 118 * Find lower node 119 */ 120 NDINIT(ndp, LOOKUP, FOLLOW|LOCKLEAF, 121 UIO_SYSSPACE, target, td); 122 error = namei(ndp); 123 /* 124 * Re-lock vnode. 125 */ 126 if (isvnunlocked && !VOP_ISLOCKED(mp->mnt_vnodecovered, NULL)) 127 vn_lock(mp->mnt_vnodecovered, LK_EXCLUSIVE | LK_RETRY, td); 128 129 if (error) 130 return (error); 131 NDFREE(ndp, NDF_ONLY_PNBUF); 132 133 /* 134 * Sanity check on lower vnode 135 */ 136 lowerrootvp = ndp->ni_vp; 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_kern_flag |= lowerrootvp->v_mount->mnt_kern_flag & MNTK_MPSAFE; 186 mp->mnt_data = (qaddr_t) xmp; 187 vfs_getnewfsid(mp); 188 189 vfs_mountedfrom(mp, target); 190 191 NULLFSDEBUG("nullfs_mount: lower %s, alias at %s\n", 192 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname); 193 return (0); 194} 195 196/* 197 * Free reference to null layer 198 */ 199static int 200nullfs_unmount(mp, mntflags, td) 201 struct mount *mp; 202 int mntflags; 203 struct thread *td; 204{ 205 void *mntdata; 206 int error; 207 int flags = 0; 208 209 NULLFSDEBUG("nullfs_unmount: mp = %p\n", (void *)mp); 210 211 if (mntflags & MNT_FORCE) 212 flags |= FORCECLOSE; 213 214 /* There is 1 extra root vnode reference (nullm_rootvp). */ 215 error = vflush(mp, 1, flags, td); 216 if (error) 217 return (error); 218 219 /* 220 * Finally, throw away the null_mount structure 221 */ 222 mntdata = mp->mnt_data; 223 mp->mnt_data = 0; 224 free(mntdata, M_NULLFSMNT); 225 return 0; 226} 227 228static int 229nullfs_root(mp, flags, vpp, td) 230 struct mount *mp; 231 int flags; 232 struct vnode **vpp; 233 struct thread *td; 234{ 235 struct vnode *vp; 236 237 NULLFSDEBUG("nullfs_root(mp = %p, vp = %p->%p)\n", (void *)mp, 238 (void *)MOUNTTONULLMOUNT(mp)->nullm_rootvp, 239 (void *)NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp)); 240 241 /* 242 * Return locked reference to root. 243 */ 244 vp = MOUNTTONULLMOUNT(mp)->nullm_rootvp; 245 VREF(vp); 246 247#ifdef NULLFS_DEBUG 248 if (VOP_ISLOCKED(vp, NULL)) { 249 kdb_enter("root vnode is locked.\n"); 250 vrele(vp); 251 return (EDEADLK); 252 } 253#endif 254 vn_lock(vp, flags | LK_RETRY, td); 255 *vpp = vp; 256 return 0; 257} 258 259static int 260nullfs_quotactl(mp, cmd, uid, arg, td) 261 struct mount *mp; 262 int cmd; 263 uid_t uid; 264 void *arg; 265 struct thread *td; 266{ 267 return VFS_QUOTACTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, uid, arg, td); 268} 269 270static int 271nullfs_statfs(mp, sbp, td) 272 struct mount *mp; 273 struct statfs *sbp; 274 struct thread *td; 275{ 276 int error; 277 struct statfs mstat; 278 279 NULLFSDEBUG("nullfs_statfs(mp = %p, vp = %p->%p)\n", (void *)mp, 280 (void *)MOUNTTONULLMOUNT(mp)->nullm_rootvp, 281 (void *)NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp)); 282 283 bzero(&mstat, sizeof(mstat)); 284 285 error = VFS_STATFS(MOUNTTONULLMOUNT(mp)->nullm_vfs, &mstat, td); 286 if (error) 287 return (error); 288 289 /* now copy across the "interesting" information and fake the rest */ 290 sbp->f_type = mstat.f_type; 291 sbp->f_flags = mstat.f_flags; 292 sbp->f_bsize = mstat.f_bsize; 293 sbp->f_iosize = mstat.f_iosize; 294 sbp->f_blocks = mstat.f_blocks; 295 sbp->f_bfree = mstat.f_bfree; 296 sbp->f_bavail = mstat.f_bavail; 297 sbp->f_files = mstat.f_files; 298 sbp->f_ffree = mstat.f_ffree; 299 return (0); 300} 301 302static int 303nullfs_sync(mp, waitfor, td) 304 struct mount *mp; 305 int waitfor; 306 struct thread *td; 307{ 308 /* 309 * XXX - Assumes no data cached at null layer. 310 */ 311 return (0); 312} 313 314static int 315nullfs_vget(mp, ino, flags, vpp) 316 struct mount *mp; 317 ino_t ino; 318 int flags; 319 struct vnode **vpp; 320{ 321 int error; 322 error = VFS_VGET(MOUNTTONULLMOUNT(mp)->nullm_vfs, ino, flags, vpp); 323 if (error) 324 return (error); 325 326 return (null_nodeget(mp, *vpp, vpp)); 327} 328 329static int 330nullfs_fhtovp(mp, fidp, vpp) 331 struct mount *mp; 332 struct fid *fidp; 333 struct vnode **vpp; 334{ 335 int error; 336 error = VFS_FHTOVP(MOUNTTONULLMOUNT(mp)->nullm_vfs, fidp, vpp); 337 if (error) 338 return (error); 339 340 return (null_nodeget(mp, *vpp, vpp)); 341} 342 343static int 344nullfs_checkexp(mp, nam, extflagsp, credanonp) 345 struct mount *mp; 346 struct sockaddr *nam; 347 int *extflagsp; 348 struct ucred **credanonp; 349{ 350 351 return VFS_CHECKEXP(MOUNTTONULLMOUNT(mp)->nullm_vfs, nam, 352 extflagsp, credanonp); 353} 354 355static int 356nullfs_vptofh(vp, fhp) 357 struct vnode *vp; 358 struct fid *fhp; 359{ 360 struct vnode *lvp; 361 362 lvp = NULLVPTOLOWERVP(vp); 363 return VFS_VPTOFH(lvp, fhp); 364} 365 366static int 367nullfs_extattrctl(mp, cmd, filename_vp, namespace, attrname, td) 368 struct mount *mp; 369 int cmd; 370 struct vnode *filename_vp; 371 int namespace; 372 const char *attrname; 373 struct thread *td; 374{ 375 return VFS_EXTATTRCTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, filename_vp, 376 namespace, attrname, td); 377} 378 379 380static struct vfsops null_vfsops = { 381 .vfs_checkexp = nullfs_checkexp, 382 .vfs_extattrctl = nullfs_extattrctl, 383 .vfs_fhtovp = nullfs_fhtovp, 384 .vfs_init = nullfs_init, 385 .vfs_mount = nullfs_mount, 386 .vfs_quotactl = nullfs_quotactl, 387 .vfs_root = nullfs_root, 388 .vfs_statfs = nullfs_statfs, 389 .vfs_sync = nullfs_sync, 390 .vfs_uninit = nullfs_uninit, 391 .vfs_unmount = nullfs_unmount, 392 .vfs_vget = nullfs_vget, 393 .vfs_vptofh = nullfs_vptofh, 394}; 395 396VFS_SET(null_vfsops, nullfs, VFCF_LOOPBACK); 397