1 /* $NetBSD: umap_vfsops.c,v 1.94 2014/08/11 14:02:14 maxv 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.94 2014/08/11 14:02:14 maxv 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 #include <sys/module.h> 57 58 #include <miscfs/umapfs/umap.h> 59 #include <miscfs/genfs/layer_extern.h> 60 61 MODULE(MODULE_CLASS_VFS, umap, "layerfs"); 62 63 VFS_PROTOS(umapfs); 64 65 static struct sysctllog *umapfs_sysctl_log; 66 67 /* 68 * Mount umap layer 69 */ 70 int 71 umapfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len) 72 { 73 struct lwp *l = curlwp; 74 struct pathbuf *pb; 75 struct nameidata nd; 76 struct umap_args *args = data; 77 struct vnode *lowerrootvp, *vp; 78 struct umap_mount *amp; 79 int error; 80 #ifdef UMAPFS_DIAGNOSTIC 81 int i; 82 #endif 83 84 if (args == NULL) 85 return EINVAL; 86 if (*data_len < sizeof *args) 87 return EINVAL; 88 89 if (mp->mnt_flag & MNT_GETARGS) { 90 amp = MOUNTTOUMAPMOUNT(mp); 91 if (amp == NULL) 92 return EIO; 93 args->la.target = NULL; 94 args->nentries = amp->info_nentries; 95 args->gnentries = amp->info_gnentries; 96 *data_len = sizeof *args; 97 return 0; 98 } 99 100 /* only for root */ 101 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT, 102 KAUTH_REQ_SYSTEM_MOUNT_UMAP, NULL, NULL, NULL); 103 if (error) 104 return error; 105 106 #ifdef UMAPFS_DIAGNOSTIC 107 printf("umapfs_mount(mp = %p)\n", mp); 108 #endif 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 error = pathbuf_copyin(args->umap_target, &pb); 120 if (error) { 121 return error; 122 } 123 NDINIT(&nd, LOOKUP, FOLLOW|LOCKLEAF, pb); 124 if ((error = namei(&nd)) != 0) { 125 pathbuf_destroy(pb); 126 return error; 127 } 128 129 /* 130 * Sanity check on lower vnode 131 */ 132 lowerrootvp = nd.ni_vp; 133 pathbuf_destroy(pb); 134 #ifdef UMAPFS_DIAGNOSTIC 135 printf("vp = %p, check for VDIR...\n", lowerrootvp); 136 #endif 137 138 if (lowerrootvp->v_type != VDIR) { 139 vput(lowerrootvp); 140 return (EINVAL); 141 } 142 143 #ifdef UMAPFS_DIAGNOSTIC 144 printf("mp = %p\n", mp); 145 #endif 146 147 amp = kmem_zalloc(sizeof(struct umap_mount), KM_SLEEP); 148 mp->mnt_data = amp; 149 amp->umapm_vfs = lowerrootvp->v_mount; 150 if (amp->umapm_vfs->mnt_flag & MNT_LOCAL) 151 mp->mnt_flag |= MNT_LOCAL; 152 153 /* 154 * Now copy in the number of entries and maps for umap mapping. 155 */ 156 if (args->nentries < 0 || args->nentries > MAPFILEENTRIES || 157 args->gnentries < 0 || args->gnentries > GMAPFILEENTRIES) { 158 vput(lowerrootvp); 159 return (EINVAL); 160 } 161 162 amp->info_nentries = args->nentries; 163 amp->info_gnentries = args->gnentries; 164 error = copyin(args->mapdata, amp->info_mapdata, 165 2*sizeof(u_long)*args->nentries); 166 if (error) { 167 vput(lowerrootvp); 168 return (error); 169 } 170 171 #ifdef UMAPFS_DIAGNOSTIC 172 printf("umap_mount:nentries %d\n",args->nentries); 173 for (i = 0; i < args->nentries; i++) 174 printf(" %ld maps to %ld\n", amp->info_mapdata[i][0], 175 amp->info_mapdata[i][1]); 176 #endif 177 178 error = copyin(args->gmapdata, amp->info_gmapdata, 179 2*sizeof(u_long)*args->gnentries); 180 if (error) { 181 vput(lowerrootvp); 182 return (error); 183 } 184 185 #ifdef UMAPFS_DIAGNOSTIC 186 printf("umap_mount:gnentries %d\n",args->gnentries); 187 for (i = 0; i < args->gnentries; i++) 188 printf("\tgroup %ld maps to %ld\n", 189 amp->info_gmapdata[i][0], 190 amp->info_gmapdata[i][1]); 191 #endif 192 193 /* 194 * Make sure the mount point's sufficiently initialized 195 * that the node create call will work. 196 */ 197 vfs_getnewfsid(mp); 198 amp->umapm_size = sizeof(struct umap_node); 199 amp->umapm_tag = VT_UMAP; 200 amp->umapm_bypass = umap_bypass; 201 amp->umapm_vnodeop_p = umap_vnodeop_p; 202 203 /* 204 * fix up umap node for root vnode. 205 */ 206 VOP_UNLOCK(lowerrootvp); 207 error = layer_node_create(mp, lowerrootvp, &vp); 208 /* 209 * Make sure the node alias worked 210 */ 211 if (error) { 212 vrele(lowerrootvp); 213 kmem_free(amp, sizeof(struct umap_mount)); 214 return error; 215 } 216 217 /* 218 * Keep a held reference to the root vnode. 219 * It is vrele'd in umapfs_unmount. 220 */ 221 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 222 vp->v_vflag |= VV_ROOT; 223 amp->umapm_rootvp = vp; 224 VOP_UNLOCK(vp); 225 226 error = set_statvfs_info(path, UIO_USERSPACE, args->umap_target, 227 UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l); 228 #ifdef UMAPFS_DIAGNOSTIC 229 printf("umapfs_mount: lower %s, alias at %s\n", 230 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname); 231 #endif 232 return error; 233 } 234 235 /* 236 * Free reference to umap layer 237 */ 238 int 239 umapfs_unmount(struct mount *mp, int mntflags) 240 { 241 struct umap_mount *amp = MOUNTTOUMAPMOUNT(mp); 242 struct vnode *rtvp = amp->umapm_rootvp; 243 int error; 244 int flags = 0; 245 246 #ifdef UMAPFS_DIAGNOSTIC 247 printf("umapfs_unmount(mp = %p)\n", mp); 248 #endif 249 250 if (mntflags & MNT_FORCE) 251 flags |= FORCECLOSE; 252 253 if (rtvp->v_usecount > 1 && (mntflags & MNT_FORCE) == 0) 254 return (EBUSY); 255 if ((error = vflush(mp, rtvp, flags)) != 0) 256 return (error); 257 258 #ifdef UMAPFS_DIAGNOSTIC 259 vprint("alias root of lower", rtvp); 260 #endif 261 /* 262 * Blow it away for future re-use 263 */ 264 vgone(rtvp); 265 /* 266 * Finally, throw away the umap_mount structure 267 */ 268 kmem_free(amp, sizeof(struct umap_mount)); 269 mp->mnt_data = NULL; 270 return 0; 271 } 272 273 extern const struct vnodeopv_desc umapfs_vnodeop_opv_desc; 274 275 const struct vnodeopv_desc * const umapfs_vnodeopv_descs[] = { 276 &umapfs_vnodeop_opv_desc, 277 NULL, 278 }; 279 280 struct vfsops umapfs_vfsops = { 281 .vfs_name = MOUNT_UMAP, 282 .vfs_min_mount_data = sizeof (struct umap_args), 283 .vfs_mount = umapfs_mount, 284 .vfs_start = layerfs_start, 285 .vfs_unmount = umapfs_unmount, 286 .vfs_root = layerfs_root, 287 .vfs_quotactl = layerfs_quotactl, 288 .vfs_statvfs = layerfs_statvfs, 289 .vfs_sync = layerfs_sync, 290 .vfs_loadvnode = layerfs_loadvnode, 291 .vfs_vget = layerfs_vget, 292 .vfs_fhtovp = layerfs_fhtovp, 293 .vfs_vptofh = layerfs_vptofh, 294 .vfs_init = layerfs_init, 295 .vfs_done = layerfs_done, 296 .vfs_snapshot = layerfs_snapshot, 297 .vfs_extattrctl = vfs_stdextattrctl, 298 .vfs_suspendctl = (void *)eopnotsupp, 299 .vfs_renamelock_enter = layerfs_renamelock_enter, 300 .vfs_renamelock_exit = layerfs_renamelock_exit, 301 .vfs_fsync = (void *)eopnotsupp, 302 .vfs_opv_descs = umapfs_vnodeopv_descs 303 }; 304 305 static int 306 umap_modcmd(modcmd_t cmd, void *arg) 307 { 308 int error; 309 310 switch (cmd) { 311 case MODULE_CMD_INIT: 312 error = vfs_attach(&umapfs_vfsops); 313 if (error != 0) 314 break; 315 sysctl_createv(&umapfs_sysctl_log, 0, NULL, NULL, 316 CTLFLAG_PERMANENT, 317 CTLTYPE_NODE, "umap", 318 SYSCTL_DESCR("UID/GID remapping file system"), 319 NULL, 0, NULL, 0, 320 CTL_VFS, 10, CTL_EOL); 321 /* 322 * XXX the "10" above could be dynamic, thereby eliminating 323 * one more instance of the "number to vfs" mapping problem, 324 * but "10" is the order as taken from sys/mount.h 325 */ 326 break; 327 case MODULE_CMD_FINI: 328 error = vfs_detach(&umapfs_vfsops); 329 if (error != 0) 330 break; 331 sysctl_teardown(&umapfs_sysctl_log); 332 break; 333 default: 334 error = ENOTTY; 335 break; 336 } 337 338 return (error); 339 } 340