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