1 /* $NetBSD: umap_vfsops.c,v 1.32 2001/11/10 13:33:45 lukem Exp $ */ 2 3 /* 4 * Copyright (c) 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software donated to Berkeley by 8 * the UCLA Ficus project. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * from: @(#)null_vfsops.c 1.5 (Berkeley) 7/10/92 39 * @(#)umap_vfsops.c 8.8 (Berkeley) 5/14/95 40 */ 41 42 /* 43 * Umap Layer 44 * (See mount_umap(8) for a description of this layer.) 45 */ 46 47 #include <sys/cdefs.h> 48 __KERNEL_RCSID(0, "$NetBSD: umap_vfsops.c,v 1.32 2001/11/10 13:33:45 lukem Exp $"); 49 50 #include <sys/param.h> 51 #include <sys/systm.h> 52 #include <sys/proc.h> 53 #include <sys/time.h> 54 #include <sys/types.h> 55 #include <sys/vnode.h> 56 #include <sys/mount.h> 57 #include <sys/namei.h> 58 #include <sys/malloc.h> 59 #include <miscfs/umapfs/umap.h> 60 #include <miscfs/genfs/layer_extern.h> 61 62 int umapfs_mount __P((struct mount *, const char *, void *, 63 struct nameidata *, struct proc *)); 64 int umapfs_unmount __P((struct mount *, int, struct proc *)); 65 66 /* 67 * Mount umap layer 68 */ 69 int 70 umapfs_mount(mp, path, data, ndp, p) 71 struct mount *mp; 72 const char *path; 73 void *data; 74 struct nameidata *ndp; 75 struct proc *p; 76 { 77 struct umap_args args; 78 struct vnode *lowerrootvp, *vp; 79 struct umap_mount *amp; 80 size_t size; 81 int error; 82 #ifdef UMAPFS_DIAGNOSTIC 83 int i; 84 #endif 85 86 /* only for root */ 87 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 88 return error; 89 90 #ifdef UMAPFS_DIAGNOSTIC 91 printf("umapfs_mount(mp = %p)\n", mp); 92 #endif 93 94 /* 95 * Get argument 96 */ 97 error = copyin(data, (caddr_t)&args, sizeof(struct umap_args)); 98 if (error) 99 return (error); 100 101 /* 102 * Update only does export updating. 103 */ 104 if (mp->mnt_flag & MNT_UPDATE) { 105 amp = MOUNTTOUMAPMOUNT(mp); 106 if (args.umap_target == 0) 107 return (vfs_export(mp, &->umapm_export, 108 &args.umap_export)); 109 else 110 return (EOPNOTSUPP); 111 } 112 113 /* 114 * Find lower node 115 */ 116 NDINIT(ndp, LOOKUP, FOLLOW|LOCKLEAF, 117 UIO_USERSPACE, args.umap_target, p); 118 if ((error = namei(ndp)) != 0) 119 return (error); 120 121 /* 122 * Sanity check on lower vnode 123 */ 124 lowerrootvp = ndp->ni_vp; 125 #ifdef UMAPFS_DIAGNOSTIC 126 printf("vp = %p, check for VDIR...\n", lowerrootvp); 127 #endif 128 129 if (lowerrootvp->v_type != VDIR) { 130 vput(lowerrootvp); 131 return (EINVAL); 132 } 133 134 #ifdef UMAPFS_DIAGNOSTIC 135 printf("mp = %p\n", mp); 136 #endif 137 138 amp = (struct umap_mount *) malloc(sizeof(struct umap_mount), 139 M_UFSMNT, M_WAITOK); /* XXX */ 140 memset((caddr_t)amp, 0, sizeof(struct umap_mount)); 141 142 mp->mnt_data = (qaddr_t) amp; 143 amp->umapm_vfs = lowerrootvp->v_mount; 144 if (amp->umapm_vfs->mnt_flag & MNT_LOCAL) 145 mp->mnt_flag |= MNT_LOCAL; 146 147 /* 148 * Now copy in the number of entries and maps for umap mapping. 149 */ 150 if (args.nentries > MAPFILEENTRIES || args.gnentries > GMAPFILEENTRIES) { 151 vput(lowerrootvp); 152 return (error); 153 } 154 155 amp->info_nentries = args.nentries; 156 amp->info_gnentries = args.gnentries; 157 error = copyin(args.mapdata, (caddr_t)amp->info_mapdata, 158 2*sizeof(u_long)*args.nentries); 159 if (error) { 160 vput(lowerrootvp); 161 return (error); 162 } 163 164 #ifdef UMAPFS_DIAGNOSTIC 165 printf("umap_mount:nentries %d\n",args.nentries); 166 for (i = 0; i < args.nentries; i++) 167 printf(" %ld maps to %ld\n", amp->info_mapdata[i][0], 168 amp->info_mapdata[i][1]); 169 #endif 170 171 error = copyin(args.gmapdata, (caddr_t)amp->info_gmapdata, 172 2*sizeof(u_long)*args.gnentries); 173 if (error) { 174 vput(lowerrootvp); 175 return (error); 176 } 177 178 #ifdef UMAPFS_DIAGNOSTIC 179 printf("umap_mount:gnentries %d\n",args.gnentries); 180 for (i = 0; i < args.gnentries; i++) 181 printf("\tgroup %ld maps to %ld\n", 182 amp->info_gmapdata[i][0], 183 amp->info_gmapdata[i][1]); 184 #endif 185 186 /* 187 * Make sure the mount point's sufficiently initialized 188 * that the node create call will work. 189 */ 190 vfs_getnewfsid(mp); 191 amp->umapm_size = sizeof(struct umap_node); 192 amp->umapm_tag = VT_UMAP; 193 amp->umapm_bypass = umap_bypass; 194 amp->umapm_alloc = layer_node_alloc; /* the default alloc is fine */ 195 amp->umapm_vnodeop_p = umap_vnodeop_p; 196 simple_lock_init(&->umapm_hashlock); 197 amp->umapm_node_hashtbl = hashinit(NUMAPNODECACHE, HASH_LIST, M_CACHE, 198 M_WAITOK, &->umapm_node_hash); 199 200 201 /* 202 * fix up umap node for root vnode. 203 */ 204 error = layer_node_create(mp, lowerrootvp, &vp); 205 /* 206 * Make sure the node alias worked 207 */ 208 if (error) { 209 vput(lowerrootvp); 210 free(amp, M_UFSMNT); /* XXX */ 211 return (error); 212 } 213 /* 214 * Unlock the node (either the lower or the alias) 215 */ 216 VOP_UNLOCK(vp, 0); 217 218 /* 219 * Keep a held reference to the root vnode. 220 * It is vrele'd in umapfs_unmount. 221 */ 222 vp->v_flag |= VROOT; 223 amp->umapm_rootvp = vp; 224 225 (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size); 226 memset(mp->mnt_stat.f_mntonname + size, 0, MNAMELEN - size); 227 (void) copyinstr(args.umap_target, mp->mnt_stat.f_mntfromname, 228 MNAMELEN - 1, &size); 229 memset(mp->mnt_stat.f_mntfromname + size, 0, MNAMELEN - size); 230 #ifdef UMAPFS_DIAGNOSTIC 231 printf("umapfs_mount: lower %s, alias at %s\n", 232 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname); 233 #endif 234 return (0); 235 } 236 237 /* 238 * Free reference to umap layer 239 */ 240 int 241 umapfs_unmount(mp, mntflags, p) 242 struct mount *mp; 243 int mntflags; 244 struct proc *p; 245 { 246 struct vnode *rootvp = MOUNTTOUMAPMOUNT(mp)->umapm_rootvp; 247 int error; 248 int flags = 0; 249 250 #ifdef UMAPFS_DIAGNOSTIC 251 printf("umapfs_unmount(mp = %p)\n", mp); 252 #endif 253 254 if (mntflags & MNT_FORCE) 255 flags |= FORCECLOSE; 256 257 /* 258 * Clear out buffer cache. I don't think we 259 * ever get anything cached at this level at the 260 * moment, but who knows... 261 */ 262 #ifdef notyet 263 mntflushbuf(mp, 0); 264 if (mntinvalbuf(mp, 1)) 265 return (EBUSY); 266 #endif 267 if (rootvp->v_usecount > 1) 268 return (EBUSY); 269 if ((error = vflush(mp, rootvp, flags)) != 0) 270 return (error); 271 272 #ifdef UMAPFS_DIAGNOSTIC 273 vprint("alias root of lower", rootvp); 274 #endif 275 /* 276 * Release reference on underlying root vnode 277 */ 278 vrele(rootvp); 279 /* 280 * And blow it away for future re-use 281 */ 282 vgone(rootvp); 283 /* 284 * Finally, throw away the umap_mount structure 285 */ 286 free(mp->mnt_data, M_UFSMNT); /* XXX */ 287 mp->mnt_data = 0; 288 return (0); 289 } 290 291 extern const struct vnodeopv_desc umapfs_vnodeop_opv_desc; 292 293 const struct vnodeopv_desc * const umapfs_vnodeopv_descs[] = { 294 &umapfs_vnodeop_opv_desc, 295 NULL, 296 }; 297 298 struct vfsops umapfs_vfsops = { 299 MOUNT_UMAP, 300 umapfs_mount, 301 layerfs_start, 302 umapfs_unmount, 303 layerfs_root, 304 layerfs_quotactl, 305 layerfs_statfs, 306 layerfs_sync, 307 layerfs_vget, 308 layerfs_fhtovp, 309 layerfs_vptofh, 310 layerfs_init, 311 NULL, 312 layerfs_done, 313 layerfs_sysctl, 314 NULL, /* vfs_mountroot */ 315 layerfs_checkexp, 316 umapfs_vnodeopv_descs, 317 }; 318