null_vfsops.c revision 3496
1/* 2 * Copyright (c) 1992, 1993 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 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)null_vfsops.c 8.2 (Berkeley) 1/21/94 37 * 38 * @(#)lofs_vfsops.c 1.2 (Berkeley) 6/18/92 39 * $Id: null_vfsops.c,v 1.3 1994/09/22 19:38:14 wollman Exp $ 40 */ 41 42/* 43 * Null Layer 44 * (See null_vnops.c for a description of what this does.) 45 */ 46 47#include <sys/param.h> 48#include <sys/systm.h> 49#include <sys/kernel.h> 50#include <sys/time.h> 51#include <sys/types.h> 52#include <sys/vnode.h> 53#include <sys/mount.h> 54#include <sys/namei.h> 55#include <sys/malloc.h> 56#include <miscfs/nullfs/null.h> 57 58/* 59 * Mount null layer 60 */ 61int 62nullfs_mount(mp, path, data, ndp, p) 63 struct mount *mp; 64 char *path; 65 caddr_t data; 66 struct nameidata *ndp; 67 struct proc *p; 68{ 69 int error = 0; 70 struct null_args args; 71 struct vnode *lowerrootvp, *vp; 72 struct vnode *nullm_rootvp; 73 struct null_mount *xmp; 74 u_int size; 75 76#ifdef NULLFS_DIAGNOSTIC 77 printf("nullfs_mount(mp = %x)\n", mp); 78#endif 79 80 /* 81 * Update is a no-op 82 */ 83 if (mp->mnt_flag & MNT_UPDATE) { 84 return (EOPNOTSUPP); 85 /* return VFS_MOUNT(MOUNTTONULLMOUNT(mp)->nullm_vfs, path, data, ndp, p);*/ 86 } 87 88 /* 89 * Get argument 90 */ 91 error = copyin(data, (caddr_t)&args, sizeof(struct null_args)); 92 if (error) 93 return (error); 94 95 /* 96 * Find lower node 97 */ 98 NDINIT(ndp, LOOKUP, FOLLOW|WANTPARENT|LOCKLEAF, 99 UIO_USERSPACE, args.target, p); 100 error = namei(ndp); 101 if (error) 102 return (error); 103 104 /* 105 * Sanity check on lower vnode 106 */ 107 lowerrootvp = ndp->ni_vp; 108 109 vrele(ndp->ni_dvp); 110 ndp->ni_dvp = NULL; 111 112 xmp = (struct null_mount *) malloc(sizeof(struct null_mount), 113 M_UFSMNT, M_WAITOK); /* XXX */ 114 115 /* 116 * Save reference to underlying FS 117 */ 118 xmp->nullm_vfs = lowerrootvp->v_mount; 119 120 /* 121 * Save reference. Each mount also holds 122 * a reference on the root vnode. 123 */ 124 error = null_node_create(mp, lowerrootvp, &vp); 125 /* 126 * Unlock the node (either the lower or the alias) 127 */ 128 VOP_UNLOCK(vp); 129 /* 130 * Make sure the node alias worked 131 */ 132 if (error) { 133 vrele(lowerrootvp); 134 free(xmp, M_UFSMNT); /* XXX */ 135 return (error); 136 } 137 138 /* 139 * Keep a held reference to the root vnode. 140 * It is vrele'd in nullfs_unmount. 141 */ 142 nullm_rootvp = vp; 143 nullm_rootvp->v_flag |= VROOT; 144 xmp->nullm_rootvp = nullm_rootvp; 145 if (NULLVPTOLOWERVP(nullm_rootvp)->v_mount->mnt_flag & MNT_LOCAL) 146 mp->mnt_flag |= MNT_LOCAL; 147 mp->mnt_data = (qaddr_t) xmp; 148 getnewfsid(mp, MOUNT_LOFS); 149 150 (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size); 151 bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size); 152 (void) copyinstr(args.target, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 153 &size); 154 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 155#ifdef NULLFS_DIAGNOSTIC 156 printf("nullfs_mount: lower %s, alias at %s\n", 157 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname); 158#endif 159 return (0); 160} 161 162/* 163 * VFS start. Nothing needed here - the start routine 164 * on the underlying filesystem will have been called 165 * when that filesystem was mounted. 166 */ 167int 168nullfs_start(mp, flags, p) 169 struct mount *mp; 170 int flags; 171 struct proc *p; 172{ 173 return (0); 174 /* return VFS_START(MOUNTTONULLMOUNT(mp)->nullm_vfs, flags, p); */ 175} 176 177/* 178 * Free reference to null layer 179 */ 180int 181nullfs_unmount(mp, mntflags, p) 182 struct mount *mp; 183 int mntflags; 184 struct proc *p; 185{ 186 struct vnode *nullm_rootvp = MOUNTTONULLMOUNT(mp)->nullm_rootvp; 187 int error; 188 int flags = 0; 189 extern int doforce; 190 191#ifdef NULLFS_DIAGNOSTIC 192 printf("nullfs_unmount(mp = %x)\n", mp); 193#endif 194 195 if (mntflags & MNT_FORCE) { 196 /* lofs can never be rootfs so don't check for it */ 197 if (!doforce) 198 return (EINVAL); 199 flags |= FORCECLOSE; 200 } 201 202 /* 203 * Clear out buffer cache. I don't think we 204 * ever get anything cached at this level at the 205 * moment, but who knows... 206 */ 207#if 0 208 mntflushbuf(mp, 0); 209 if (mntinvalbuf(mp, 1)) 210 return (EBUSY); 211#endif 212 if (nullm_rootvp->v_usecount > 1) 213 return (EBUSY); 214 error = vflush(mp, nullm_rootvp, flags); 215 if (error) 216 return (error); 217 218#ifdef NULLFS_DIAGNOSTIC 219 vprint("alias root of lower", nullm_rootvp); 220#endif 221 /* 222 * Release reference on underlying root vnode 223 */ 224 vrele(nullm_rootvp); 225 /* 226 * And blow it away for future re-use 227 */ 228 vgone(nullm_rootvp); 229 /* 230 * Finally, throw away the null_mount structure 231 */ 232 free(mp->mnt_data, M_UFSMNT); /* XXX */ 233 mp->mnt_data = 0; 234 return 0; 235} 236 237int 238nullfs_root(mp, vpp) 239 struct mount *mp; 240 struct vnode **vpp; 241{ 242 struct vnode *vp; 243 244#ifdef NULLFS_DIAGNOSTIC 245 printf("nullfs_root(mp = %x, vp = %x->%x)\n", mp, 246 MOUNTTONULLMOUNT(mp)->nullm_rootvp, 247 NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp) 248 ); 249#endif 250 251 /* 252 * Return locked reference to root. 253 */ 254 vp = MOUNTTONULLMOUNT(mp)->nullm_rootvp; 255 VREF(vp); 256 VOP_LOCK(vp); 257 *vpp = vp; 258 return 0; 259} 260 261int 262nullfs_quotactl(mp, cmd, uid, arg, p) 263 struct mount *mp; 264 int cmd; 265 uid_t uid; 266 caddr_t arg; 267 struct proc *p; 268{ 269 return VFS_QUOTACTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, uid, arg, p); 270} 271 272int 273nullfs_statfs(mp, sbp, p) 274 struct mount *mp; 275 struct statfs *sbp; 276 struct proc *p; 277{ 278 int error; 279 struct statfs mstat; 280 281#ifdef NULLFS_DIAGNOSTIC 282 printf("nullfs_statfs(mp = %x, vp = %x->%x)\n", mp, 283 MOUNTTONULLMOUNT(mp)->nullm_rootvp, 284 NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp) 285 ); 286#endif 287 288 bzero(&mstat, sizeof(mstat)); 289 290 error = VFS_STATFS(MOUNTTONULLMOUNT(mp)->nullm_vfs, &mstat, p); 291 if (error) 292 return (error); 293 294 /* now copy across the "interesting" information and fake the rest */ 295 sbp->f_type = mstat.f_type; 296 sbp->f_flags = mstat.f_flags; 297 sbp->f_bsize = mstat.f_bsize; 298 sbp->f_iosize = mstat.f_iosize; 299 sbp->f_blocks = mstat.f_blocks; 300 sbp->f_bfree = mstat.f_bfree; 301 sbp->f_bavail = mstat.f_bavail; 302 sbp->f_files = mstat.f_files; 303 sbp->f_ffree = mstat.f_ffree; 304 if (sbp != &mp->mnt_stat) { 305 bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid)); 306 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN); 307 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN); 308 } 309 return (0); 310} 311 312int 313nullfs_sync(mp, waitfor, cred, p) 314 struct mount *mp; 315 int waitfor; 316 struct ucred *cred; 317 struct proc *p; 318{ 319 /* 320 * XXX - Assumes no data cached at null layer. 321 */ 322 return (0); 323} 324 325int 326nullfs_vget(mp, ino, vpp) 327 struct mount *mp; 328 ino_t ino; 329 struct vnode **vpp; 330{ 331 332 return VFS_VGET(MOUNTTONULLMOUNT(mp)->nullm_vfs, ino, vpp); 333} 334 335int 336nullfs_fhtovp(mp, fidp, nam, vpp, exflagsp, credanonp) 337 struct mount *mp; 338 struct fid *fidp; 339 struct mbuf *nam; 340 struct vnode **vpp; 341 int *exflagsp; 342 struct ucred**credanonp; 343{ 344 345 return VFS_FHTOVP(MOUNTTONULLMOUNT(mp)->nullm_vfs, fidp, nam, vpp, exflagsp,credanonp); 346} 347 348int 349nullfs_vptofh(vp, fhp) 350 struct vnode *vp; 351 struct fid *fhp; 352{ 353 return VFS_VPTOFH(NULLVPTOLOWERVP(vp), fhp); 354} 355 356int nullfs_init __P((void)); 357 358struct vfsops null_vfsops = { 359 nullfs_mount, 360 nullfs_start, 361 nullfs_unmount, 362 nullfs_root, 363 nullfs_quotactl, 364 nullfs_statfs, 365 nullfs_sync, 366 nullfs_vget, 367 nullfs_fhtovp, 368 nullfs_vptofh, 369 nullfs_init, 370}; 371 372VFS_SET(null_vfsops, null, MOUNT_NULL, 0); 373