1 /* $NetBSD: union_vfsops.c,v 1.68 2012/04/30 22:51:27 rmind Exp $ */ 2 3 /* 4 * Copyright (c) 1994 The Regents of the University of California. 5 * All rights reserved. 6 * 7 * This code is derived from software donated to Berkeley by 8 * Jan-Simon Pendry. 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 * @(#)union_vfsops.c 8.20 (Berkeley) 5/20/95 35 */ 36 37 /* 38 * Copyright (c) 1994 Jan-Simon Pendry. 39 * All rights reserved. 40 * 41 * This code is derived from software donated to Berkeley by 42 * Jan-Simon Pendry. 43 * 44 * Redistribution and use in source and binary forms, with or without 45 * modification, are permitted provided that the following conditions 46 * are met: 47 * 1. Redistributions of source code must retain the above copyright 48 * notice, this list of conditions and the following disclaimer. 49 * 2. Redistributions in binary form must reproduce the above copyright 50 * notice, this list of conditions and the following disclaimer in the 51 * documentation and/or other materials provided with the distribution. 52 * 3. All advertising materials mentioning features or use of this software 53 * must display the following acknowledgement: 54 * This product includes software developed by the University of 55 * California, Berkeley and its contributors. 56 * 4. Neither the name of the University nor the names of its contributors 57 * may be used to endorse or promote products derived from this software 58 * without specific prior written permission. 59 * 60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 70 * SUCH DAMAGE. 71 * 72 * @(#)union_vfsops.c 8.20 (Berkeley) 5/20/95 73 */ 74 75 /* 76 * Union Layer 77 */ 78 79 #include <sys/cdefs.h> 80 __KERNEL_RCSID(0, "$NetBSD: union_vfsops.c,v 1.68 2012/04/30 22:51:27 rmind Exp $"); 81 82 #include <sys/param.h> 83 #include <sys/systm.h> 84 #include <sys/sysctl.h> 85 #include <sys/time.h> 86 #include <sys/proc.h> 87 #include <sys/vnode.h> 88 #include <sys/mount.h> 89 #include <sys/namei.h> 90 #include <sys/malloc.h> 91 #include <sys/filedesc.h> 92 #include <sys/queue.h> 93 #include <sys/stat.h> 94 #include <sys/kauth.h> 95 #include <sys/module.h> 96 97 #include <fs/union/union.h> 98 99 MODULE(MODULE_CLASS_VFS, union, NULL); 100 101 VFS_PROTOS(union); 102 103 static struct sysctllog *union_sysctl_log; 104 105 /* 106 * Mount union filesystem 107 */ 108 int 109 union_mount(struct mount *mp, const char *path, void *data, size_t *data_len) 110 { 111 struct lwp *l = curlwp; 112 int error = 0; 113 struct union_args *args = data; 114 struct vnode *lowerrootvp = NULLVP; 115 struct vnode *upperrootvp = NULLVP; 116 struct union_mount *um = 0; 117 const char *cp; 118 char *xp; 119 int len; 120 size_t size; 121 122 if (*data_len < sizeof *args) 123 return EINVAL; 124 125 #ifdef UNION_DIAGNOSTIC 126 printf("union_mount(mp = %p)\n", mp); 127 #endif 128 129 if (mp->mnt_flag & MNT_GETARGS) { 130 um = MOUNTTOUNIONMOUNT(mp); 131 if (um == NULL) 132 return EIO; 133 args->target = NULL; 134 args->mntflags = um->um_op; 135 *data_len = sizeof *args; 136 return 0; 137 } 138 /* 139 * Update is a no-op 140 */ 141 if (mp->mnt_flag & MNT_UPDATE) { 142 /* 143 * Need to provide. 144 * 1. a way to convert between rdonly and rdwr mounts. 145 * 2. support for nfs exports. 146 */ 147 error = EOPNOTSUPP; 148 goto bad; 149 } 150 151 lowerrootvp = mp->mnt_vnodecovered; 152 vref(lowerrootvp); 153 154 /* 155 * Find upper node. 156 */ 157 error = namei_simple_user(args->target, 158 NSM_FOLLOW_NOEMULROOT, &upperrootvp); 159 if (error != 0) 160 goto bad; 161 162 if (upperrootvp->v_type != VDIR) { 163 error = EINVAL; 164 goto bad; 165 } 166 167 um = kmem_zalloc(sizeof(struct union_mount), KM_SLEEP); 168 169 /* 170 * Keep a held reference to the target vnodes. 171 * They are vrele'd in union_unmount. 172 * 173 * Depending on the _BELOW flag, the filesystems are 174 * viewed in a different order. In effect, this is the 175 * same as providing a mount under option to the mount syscall. 176 */ 177 178 um->um_op = args->mntflags & UNMNT_OPMASK; 179 switch (um->um_op) { 180 case UNMNT_ABOVE: 181 um->um_lowervp = lowerrootvp; 182 um->um_uppervp = upperrootvp; 183 break; 184 185 case UNMNT_BELOW: 186 um->um_lowervp = upperrootvp; 187 um->um_uppervp = lowerrootvp; 188 break; 189 190 case UNMNT_REPLACE: 191 vrele(lowerrootvp); 192 lowerrootvp = NULLVP; 193 um->um_uppervp = upperrootvp; 194 um->um_lowervp = lowerrootvp; 195 break; 196 197 default: 198 error = EINVAL; 199 goto bad; 200 } 201 202 mp->mnt_iflag |= IMNT_MPSAFE; 203 204 /* 205 * Unless the mount is readonly, ensure that the top layer 206 * supports whiteout operations 207 */ 208 if ((mp->mnt_flag & MNT_RDONLY) == 0) { 209 vn_lock(um->um_uppervp, LK_EXCLUSIVE | LK_RETRY); 210 error = VOP_WHITEOUT(um->um_uppervp, 211 (struct componentname *) 0, LOOKUP); 212 VOP_UNLOCK(um->um_uppervp); 213 if (error) 214 goto bad; 215 } 216 217 um->um_cred = l->l_cred; 218 kauth_cred_hold(um->um_cred); 219 um->um_cmode = UN_DIRMODE &~ l->l_proc->p_cwdi->cwdi_cmask; 220 221 /* 222 * Depending on what you think the MNT_LOCAL flag might mean, 223 * you may want the && to be || on the conditional below. 224 * At the moment it has been defined that the filesystem is 225 * only local if it is all local, ie the MNT_LOCAL flag implies 226 * that the entire namespace is local. If you think the MNT_LOCAL 227 * flag implies that some of the files might be stored locally 228 * then you will want to change the conditional. 229 */ 230 if (um->um_op == UNMNT_ABOVE) { 231 if (((um->um_lowervp == NULLVP) || 232 (um->um_lowervp->v_mount->mnt_flag & MNT_LOCAL)) && 233 (um->um_uppervp->v_mount->mnt_flag & MNT_LOCAL)) 234 mp->mnt_flag |= MNT_LOCAL; 235 } 236 237 /* 238 * Copy in the upper layer's RDONLY flag. This is for the benefit 239 * of lookup() which explicitly checks the flag, rather than asking 240 * the filesystem for it's own opinion. This means, that an update 241 * mount of the underlying filesystem to go from rdonly to rdwr 242 * will leave the unioned view as read-only. 243 */ 244 mp->mnt_flag |= (um->um_uppervp->v_mount->mnt_flag & MNT_RDONLY); 245 246 mp->mnt_data = um; 247 vfs_getnewfsid(mp); 248 249 error = set_statvfs_info( path, UIO_USERSPACE, NULL, UIO_USERSPACE, 250 mp->mnt_op->vfs_name, mp, l); 251 if (error) 252 goto bad; 253 254 switch (um->um_op) { 255 case UNMNT_ABOVE: 256 cp = "<above>:"; 257 break; 258 case UNMNT_BELOW: 259 cp = "<below>:"; 260 break; 261 case UNMNT_REPLACE: 262 cp = ""; 263 break; 264 default: 265 cp = "<invalid>:"; 266 #ifdef DIAGNOSTIC 267 panic("union_mount: bad um_op"); 268 #endif 269 break; 270 } 271 len = strlen(cp); 272 memcpy(mp->mnt_stat.f_mntfromname, cp, len); 273 274 xp = mp->mnt_stat.f_mntfromname + len; 275 len = MNAMELEN - len; 276 277 (void) copyinstr(args->target, xp, len - 1, &size); 278 memset(xp + size, 0, len - size); 279 280 #ifdef UNION_DIAGNOSTIC 281 printf("union_mount: from %s, on %s\n", 282 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname); 283 #endif 284 285 /* Setup the readdir hook if it's not set already */ 286 if (!vn_union_readdir_hook) 287 vn_union_readdir_hook = union_readdirhook; 288 289 return (0); 290 291 bad: 292 if (um) 293 kmem_free(um, sizeof(struct union_mount)); 294 if (upperrootvp) 295 vrele(upperrootvp); 296 if (lowerrootvp) 297 vrele(lowerrootvp); 298 return (error); 299 } 300 301 /* 302 * VFS start. Nothing needed here - the start routine 303 * on the underlying filesystem(s) will have been called 304 * when that filesystem was mounted. 305 */ 306 /*ARGSUSED*/ 307 int 308 union_start(struct mount *mp, int flags) 309 { 310 311 return (0); 312 } 313 314 /* 315 * Free reference to union layer 316 */ 317 int 318 union_unmount(struct mount *mp, int mntflags) 319 { 320 struct union_mount *um = MOUNTTOUNIONMOUNT(mp); 321 int freeing; 322 int error; 323 324 #ifdef UNION_DIAGNOSTIC 325 printf("union_unmount(mp = %p)\n", mp); 326 #endif 327 328 /* 329 * Keep flushing vnodes from the mount list. 330 * This is needed because of the un_pvp held 331 * reference to the parent vnode. 332 * If more vnodes have been freed on a given pass, 333 * the try again. The loop will iterate at most 334 * (d) times, where (d) is the maximum tree depth 335 * in the filesystem. 336 */ 337 for (freeing = 0; (error = vflush(mp, NULL, 0)) != 0;) { 338 struct vnode *vp; 339 int n; 340 341 /* count #vnodes held on mount list */ 342 n = 0; 343 TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) 344 n++; 345 346 /* if this is unchanged then stop */ 347 if (n == freeing) 348 break; 349 350 /* otherwise try once more time */ 351 freeing = n; 352 } 353 354 /* 355 * Ok, now that we've tried doing it gently, get out the hammer. 356 */ 357 358 if (mntflags & MNT_FORCE) 359 error = vflush(mp, NULL, FORCECLOSE); 360 361 if (error) 362 return error; 363 364 /* 365 * Discard references to upper and lower target vnodes. 366 */ 367 if (um->um_lowervp) 368 vrele(um->um_lowervp); 369 vrele(um->um_uppervp); 370 kauth_cred_free(um->um_cred); 371 /* 372 * Finally, throw away the union_mount structure 373 */ 374 kmem_free(um, sizeof(struct union_mount)); 375 mp->mnt_data = NULL; 376 return 0; 377 } 378 379 int 380 union_root(struct mount *mp, struct vnode **vpp) 381 { 382 struct union_mount *um = MOUNTTOUNIONMOUNT(mp); 383 int error; 384 385 /* 386 * Return locked reference to root. 387 */ 388 vref(um->um_uppervp); 389 vn_lock(um->um_uppervp, LK_EXCLUSIVE | LK_RETRY); 390 if (um->um_lowervp) 391 vref(um->um_lowervp); 392 error = union_allocvp(vpp, mp, NULL, NULL, NULL, 393 um->um_uppervp, um->um_lowervp, 1); 394 395 if (error) { 396 vput(um->um_uppervp); 397 if (um->um_lowervp) 398 vrele(um->um_lowervp); 399 } 400 401 return (error); 402 } 403 404 int 405 union_statvfs(struct mount *mp, struct statvfs *sbp) 406 { 407 int error; 408 struct union_mount *um = MOUNTTOUNIONMOUNT(mp); 409 struct statvfs *sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK | M_ZERO); 410 unsigned long lbsize; 411 412 #ifdef UNION_DIAGNOSTIC 413 printf("union_statvfs(mp = %p, lvp = %p, uvp = %p)\n", mp, 414 um->um_lowervp, um->um_uppervp); 415 #endif 416 417 if (um->um_lowervp) { 418 error = VFS_STATVFS(um->um_lowervp->v_mount, sbuf); 419 if (error) 420 goto done; 421 } 422 423 /* now copy across the "interesting" information and fake the rest */ 424 lbsize = sbuf->f_bsize; 425 sbp->f_blocks = sbuf->f_blocks - sbuf->f_bfree; 426 sbp->f_files = sbuf->f_files - sbuf->f_ffree; 427 428 error = VFS_STATVFS(um->um_uppervp->v_mount, sbuf); 429 if (error) 430 goto done; 431 432 sbp->f_flag = sbuf->f_flag; 433 sbp->f_bsize = sbuf->f_bsize; 434 sbp->f_frsize = sbuf->f_frsize; 435 sbp->f_iosize = sbuf->f_iosize; 436 437 /* 438 * The "total" fields count total resources in all layers, 439 * the "free" fields count only those resources which are 440 * free in the upper layer (since only the upper layer 441 * is writable). 442 */ 443 444 if (sbuf->f_bsize != lbsize) 445 sbp->f_blocks = sbp->f_blocks * lbsize / sbuf->f_bsize; 446 sbp->f_blocks += sbuf->f_blocks; 447 sbp->f_bfree = sbuf->f_bfree; 448 sbp->f_bavail = sbuf->f_bavail; 449 sbp->f_bresvd = sbuf->f_bresvd; 450 sbp->f_files += sbuf->f_files; 451 sbp->f_ffree = sbuf->f_ffree; 452 sbp->f_favail = sbuf->f_favail; 453 sbp->f_fresvd = sbuf->f_fresvd; 454 455 copy_statvfs_info(sbp, mp); 456 done: 457 free(sbuf, M_TEMP); 458 return error; 459 } 460 461 /*ARGSUSED*/ 462 int 463 union_sync(struct mount *mp, int waitfor, 464 kauth_cred_t cred) 465 { 466 467 /* 468 * XXX - Assumes no data cached at union layer. 469 */ 470 return (0); 471 } 472 473 /*ARGSUSED*/ 474 int 475 union_vget(struct mount *mp, ino_t ino, 476 struct vnode **vpp) 477 { 478 479 return (EOPNOTSUPP); 480 } 481 482 static int 483 union_renamelock_enter(struct mount *mp) 484 { 485 struct union_mount *um = MOUNTTOUNIONMOUNT(mp); 486 487 /* Lock just the upper fs, where the action happens. */ 488 return VFS_RENAMELOCK_ENTER(um->um_uppervp->v_mount); 489 } 490 491 static void 492 union_renamelock_exit(struct mount *mp) 493 { 494 struct union_mount *um = MOUNTTOUNIONMOUNT(mp); 495 496 VFS_RENAMELOCK_EXIT(um->um_uppervp->v_mount); 497 } 498 499 extern const struct vnodeopv_desc union_vnodeop_opv_desc; 500 501 const struct vnodeopv_desc * const union_vnodeopv_descs[] = { 502 &union_vnodeop_opv_desc, 503 NULL, 504 }; 505 506 struct vfsops union_vfsops = { 507 MOUNT_UNION, 508 sizeof (struct union_args), 509 union_mount, 510 union_start, 511 union_unmount, 512 union_root, 513 (void *)eopnotsupp, /* vfs_quotactl */ 514 union_statvfs, 515 union_sync, 516 union_vget, 517 (void *)eopnotsupp, /* vfs_fhtovp */ 518 (void *)eopnotsupp, /* vfs_vptofh */ 519 union_init, 520 union_reinit, 521 union_done, 522 NULL, /* vfs_mountroot */ 523 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp, 524 vfs_stdextattrctl, 525 (void *)eopnotsupp, /* vfs_suspendctl */ 526 union_renamelock_enter, 527 union_renamelock_exit, 528 (void *)eopnotsupp, 529 union_vnodeopv_descs, 530 0, /* vfs_refcount */ 531 { NULL, NULL }, 532 }; 533 534 static int 535 union_modcmd(modcmd_t cmd, void *arg) 536 { 537 int error; 538 539 switch (cmd) { 540 case MODULE_CMD_INIT: 541 error = vfs_attach(&union_vfsops); 542 if (error != 0) 543 break; 544 sysctl_createv(&union_sysctl_log, 0, NULL, NULL, 545 CTLFLAG_PERMANENT, 546 CTLTYPE_NODE, "vfs", NULL, 547 NULL, 0, NULL, 0, 548 CTL_VFS, CTL_EOL); 549 sysctl_createv(&union_sysctl_log, 0, NULL, NULL, 550 CTLFLAG_PERMANENT, 551 CTLTYPE_NODE, "union", 552 SYSCTL_DESCR("Union file system"), 553 NULL, 0, NULL, 0, 554 CTL_VFS, 15, CTL_EOL); 555 /* 556 * XXX the "15" above could be dynamic, thereby eliminating 557 * one more instance of the "number to vfs" mapping problem, 558 * but "15" is the order as taken from sys/mount.h 559 */ 560 break; 561 case MODULE_CMD_FINI: 562 error = vfs_detach(&union_vfsops); 563 if (error != 0) 564 break; 565 sysctl_teardown(&union_sysctl_log); 566 break; 567 default: 568 error = ENOTTY; 569 break; 570 } 571 572 return (error); 573 } 574