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