1 /* $NetBSD: umap_vfsops.c,v 1.73 2007/12/08 19:29:51 pooka 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.73 2007/12/08 19:29:51 pooka 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 VFS_PROTOS(umapfs); 61 62 /* 63 * Mount umap layer 64 */ 65 int 66 umapfs_mount(mp, path, data, data_len) 67 struct mount *mp; 68 const char *path; 69 void *data; 70 size_t *data_len; 71 { 72 struct lwp *l = curlwp; 73 struct nameidata nd; 74 struct umap_args *args = data; 75 struct vnode *lowerrootvp, *vp; 76 struct umap_mount *amp; 77 int error; 78 #ifdef UMAPFS_DIAGNOSTIC 79 int i; 80 #endif 81 82 if (*data_len < sizeof *args) 83 return EINVAL; 84 85 if (mp->mnt_flag & MNT_GETARGS) { 86 amp = MOUNTTOUMAPMOUNT(mp); 87 if (amp == NULL) 88 return EIO; 89 args->la.target = NULL; 90 args->nentries = amp->info_nentries; 91 args->gnentries = amp->info_gnentries; 92 *data_len = sizeof *args; 93 return 0; 94 } 95 96 /* only for root */ 97 if ((error = kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER, 98 NULL)) != 0) 99 return error; 100 101 #ifdef UMAPFS_DIAGNOSTIC 102 printf("umapfs_mount(mp = %p)\n", mp); 103 #endif 104 105 /* 106 * Update is not supported 107 */ 108 if (mp->mnt_flag & MNT_UPDATE) 109 return EOPNOTSUPP; 110 111 /* 112 * Find lower node 113 */ 114 NDINIT(&nd, LOOKUP, FOLLOW|LOCKLEAF, 115 UIO_USERSPACE, args->umap_target); 116 if ((error = namei(&nd)) != 0) 117 return (error); 118 119 /* 120 * Sanity check on lower vnode 121 */ 122 lowerrootvp = nd.ni_vp; 123 #ifdef UMAPFS_DIAGNOSTIC 124 printf("vp = %p, check for VDIR...\n", lowerrootvp); 125 #endif 126 127 if (lowerrootvp->v_type != VDIR) { 128 vput(lowerrootvp); 129 return (EINVAL); 130 } 131 132 #ifdef UMAPFS_DIAGNOSTIC 133 printf("mp = %p\n", mp); 134 #endif 135 136 amp = (struct umap_mount *) malloc(sizeof(struct umap_mount), 137 M_UFSMNT, M_WAITOK); /* XXX */ 138 memset(amp, 0, sizeof(struct umap_mount)); 139 140 mp->mnt_data = amp; 141 amp->umapm_vfs = lowerrootvp->v_mount; 142 if (amp->umapm_vfs->mnt_flag & MNT_LOCAL) 143 mp->mnt_flag |= MNT_LOCAL; 144 145 /* 146 * Now copy in the number of entries and maps for umap mapping. 147 */ 148 if (args->nentries > MAPFILEENTRIES || args->gnentries > GMAPFILEENTRIES) { 149 vput(lowerrootvp); 150 return (error); 151 } 152 153 amp->info_nentries = args->nentries; 154 amp->info_gnentries = args->gnentries; 155 error = copyin(args->mapdata, amp->info_mapdata, 156 2*sizeof(u_long)*args->nentries); 157 if (error) { 158 vput(lowerrootvp); 159 return (error); 160 } 161 162 #ifdef UMAPFS_DIAGNOSTIC 163 printf("umap_mount:nentries %d\n",args->nentries); 164 for (i = 0; i < args->nentries; i++) 165 printf(" %ld maps to %ld\n", amp->info_mapdata[i][0], 166 amp->info_mapdata[i][1]); 167 #endif 168 169 error = copyin(args->gmapdata, amp->info_gmapdata, 170 2*sizeof(u_long)*args->gnentries); 171 if (error) { 172 vput(lowerrootvp); 173 return (error); 174 } 175 176 #ifdef UMAPFS_DIAGNOSTIC 177 printf("umap_mount:gnentries %d\n",args->gnentries); 178 for (i = 0; i < args->gnentries; i++) 179 printf("\tgroup %ld maps to %ld\n", 180 amp->info_gmapdata[i][0], 181 amp->info_gmapdata[i][1]); 182 #endif 183 184 /* 185 * Make sure the mount point's sufficiently initialized 186 * that the node create call will work. 187 */ 188 vfs_getnewfsid(mp); 189 amp->umapm_size = sizeof(struct umap_node); 190 amp->umapm_tag = VT_UMAP; 191 amp->umapm_bypass = umap_bypass; 192 amp->umapm_alloc = layer_node_alloc; /* the default alloc is fine */ 193 amp->umapm_vnodeop_p = umap_vnodeop_p; 194 mutex_init(&->umapm_hashlock, MUTEX_DEFAULT, IPL_NONE); 195 amp->umapm_node_hashtbl = hashinit(NUMAPNODECACHE, HASH_LIST, M_CACHE, 196 M_WAITOK, &->umapm_node_hash); 197 198 199 /* 200 * fix up umap node for root vnode. 201 */ 202 error = layer_node_create(mp, lowerrootvp, &vp); 203 /* 204 * Make sure the node alias worked 205 */ 206 if (error) { 207 vput(lowerrootvp); 208 free(amp, M_UFSMNT); /* XXX */ 209 return (error); 210 } 211 /* 212 * Unlock the node (either the lower or the alias) 213 */ 214 vp->v_vflag |= VV_ROOT; 215 VOP_UNLOCK(vp, 0); 216 217 /* 218 * Keep a held reference to the root vnode. 219 * It is vrele'd in umapfs_unmount. 220 */ 221 amp->umapm_rootvp = vp; 222 223 error = set_statvfs_info(path, UIO_USERSPACE, args->umap_target, 224 UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l); 225 #ifdef UMAPFS_DIAGNOSTIC 226 printf("umapfs_mount: lower %s, alias at %s\n", 227 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname); 228 #endif 229 return error; 230 } 231 232 /* 233 * Free reference to umap layer 234 */ 235 int 236 umapfs_unmount(struct mount *mp, int mntflags) 237 { 238 struct umap_mount *amp = MOUNTTOUMAPMOUNT(mp); 239 struct vnode *rtvp = amp->umapm_rootvp; 240 int error; 241 int flags = 0; 242 243 #ifdef UMAPFS_DIAGNOSTIC 244 printf("umapfs_unmount(mp = %p)\n", mp); 245 #endif 246 247 if (mntflags & MNT_FORCE) 248 flags |= FORCECLOSE; 249 250 if (rtvp->v_usecount > 1 && (mntflags & MNT_FORCE) == 0) 251 return (EBUSY); 252 if ((error = vflush(mp, rtvp, flags)) != 0) 253 return (error); 254 255 #ifdef UMAPFS_DIAGNOSTIC 256 vprint("alias root of lower", rtvp); 257 #endif 258 /* 259 * Release reference on underlying root vnode 260 */ 261 vrele(rtvp); 262 /* 263 * And blow it away for future re-use 264 */ 265 vgone(rtvp); 266 /* 267 * Finally, throw away the umap_mount structure 268 */ 269 mutex_destroy(&->umapm_hashlock); 270 free(mp->mnt_data, M_UFSMNT); /* XXX */ 271 mp->mnt_data = 0; 272 return (0); 273 } 274 275 SYSCTL_SETUP(sysctl_vfs_umap_setup, "sysctl vfs.umap subtree setup") 276 { 277 278 sysctl_createv(clog, 0, NULL, NULL, 279 CTLFLAG_PERMANENT, 280 CTLTYPE_NODE, "vfs", NULL, 281 NULL, 0, NULL, 0, 282 CTL_VFS, CTL_EOL); 283 sysctl_createv(clog, 0, NULL, NULL, 284 CTLFLAG_PERMANENT, 285 CTLTYPE_NODE, "umap", 286 SYSCTL_DESCR("UID/GID remapping file system"), 287 NULL, 0, NULL, 0, 288 CTL_VFS, 10, CTL_EOL); 289 /* 290 * XXX the "10" above could be dynamic, thereby eliminating 291 * one more instance of the "number to vfs" mapping problem, 292 * but "10" is the order as taken from sys/mount.h 293 */ 294 } 295 296 extern const struct vnodeopv_desc umapfs_vnodeop_opv_desc; 297 298 const struct vnodeopv_desc * const umapfs_vnodeopv_descs[] = { 299 &umapfs_vnodeop_opv_desc, 300 NULL, 301 }; 302 303 struct vfsops umapfs_vfsops = { 304 MOUNT_UMAP, 305 sizeof (struct umap_args), 306 umapfs_mount, 307 layerfs_start, 308 umapfs_unmount, 309 layerfs_root, 310 layerfs_quotactl, 311 layerfs_statvfs, 312 layerfs_sync, 313 layerfs_vget, 314 layerfs_fhtovp, 315 layerfs_vptofh, 316 layerfs_init, 317 NULL, 318 layerfs_done, 319 NULL, /* vfs_mountroot */ 320 layerfs_snapshot, 321 vfs_stdextattrctl, 322 (void *)eopnotsupp, /* vfs_suspendctl */ 323 umapfs_vnodeopv_descs, 324 0, /* vfs_refcount */ 325 { NULL, NULL }, 326 }; 327 VFS_ATTACH(umapfs_vfsops); 328