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