1 /* $NetBSD: union_vfsops.c,v 1.64 2011/08/28 08:27:57 hannken 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.64 2011/08/28 08:27:57 hannken 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 static const char *warn_user = 105 "WARNING: the union file system is experimental\n" 106 "WARNING: it can cause crashes and file system corruption\n"; 107 108 /* 109 * Mount union filesystem 110 */ 111 int 112 union_mount(struct mount *mp, const char *path, void *data, size_t *data_len) 113 { 114 struct lwp *l = curlwp; 115 int error = 0; 116 struct union_args *args = data; 117 struct vnode *lowerrootvp = NULLVP; 118 struct vnode *upperrootvp = NULLVP; 119 struct union_mount *um = 0; 120 const char *cp; 121 char *xp; 122 int len; 123 size_t size; 124 125 if (*data_len < sizeof *args) 126 return EINVAL; 127 128 #ifdef UNION_DIAGNOSTIC 129 printf("union_mount(mp = %p)\n", mp); 130 #endif 131 132 if (mp->mnt_flag & MNT_GETARGS) { 133 um = MOUNTTOUNIONMOUNT(mp); 134 if (um == NULL) 135 return EIO; 136 args->target = NULL; 137 args->mntflags = um->um_op; 138 *data_len = sizeof *args; 139 return 0; 140 } 141 /* 142 * Update is a no-op 143 */ 144 if (mp->mnt_flag & MNT_UPDATE) { 145 /* 146 * Need to provide. 147 * 1. a way to convert between rdonly and rdwr mounts. 148 * 2. support for nfs exports. 149 */ 150 error = EOPNOTSUPP; 151 goto bad; 152 } 153 154 if (warn_user != NULL) { 155 printf("%s", warn_user); 156 warn_user = NULL; 157 } 158 159 lowerrootvp = mp->mnt_vnodecovered; 160 vref(lowerrootvp); 161 162 /* 163 * Find upper node. 164 */ 165 error = namei_simple_user(args->target, 166 NSM_FOLLOW_NOEMULROOT, &upperrootvp); 167 if (error != 0) 168 goto bad; 169 170 if (upperrootvp->v_type != VDIR) { 171 error = EINVAL; 172 goto bad; 173 } 174 175 um = (struct union_mount *) malloc(sizeof(struct union_mount), 176 M_UFSMNT, M_WAITOK); /* XXX */ 177 178 /* 179 * Keep a held reference to the target vnodes. 180 * They are vrele'd in union_unmount. 181 * 182 * Depending on the _BELOW flag, the filesystems are 183 * viewed in a different order. In effect, this is the 184 * same as providing a mount under option to the mount syscall. 185 */ 186 187 um->um_op = args->mntflags & UNMNT_OPMASK; 188 switch (um->um_op) { 189 case UNMNT_ABOVE: 190 um->um_lowervp = lowerrootvp; 191 um->um_uppervp = upperrootvp; 192 break; 193 194 case UNMNT_BELOW: 195 um->um_lowervp = upperrootvp; 196 um->um_uppervp = lowerrootvp; 197 break; 198 199 case UNMNT_REPLACE: 200 vrele(lowerrootvp); 201 lowerrootvp = NULLVP; 202 um->um_uppervp = upperrootvp; 203 um->um_lowervp = lowerrootvp; 204 break; 205 206 default: 207 error = EINVAL; 208 goto bad; 209 } 210 211 /* 212 * Unless the mount is readonly, ensure that the top layer 213 * supports whiteout operations 214 */ 215 if ((mp->mnt_flag & MNT_RDONLY) == 0) { 216 vn_lock(um->um_uppervp, LK_EXCLUSIVE | LK_RETRY); 217 error = VOP_WHITEOUT(um->um_uppervp, 218 (struct componentname *) 0, LOOKUP); 219 VOP_UNLOCK(um->um_uppervp); 220 if (error) 221 goto bad; 222 } 223 224 um->um_cred = l->l_cred; 225 kauth_cred_hold(um->um_cred); 226 um->um_cmode = UN_DIRMODE &~ l->l_proc->p_cwdi->cwdi_cmask; 227 228 /* 229 * Depending on what you think the MNT_LOCAL flag might mean, 230 * you may want the && to be || on the conditional below. 231 * At the moment it has been defined that the filesystem is 232 * only local if it is all local, ie the MNT_LOCAL flag implies 233 * that the entire namespace is local. If you think the MNT_LOCAL 234 * flag implies that some of the files might be stored locally 235 * then you will want to change the conditional. 236 */ 237 if (um->um_op == UNMNT_ABOVE) { 238 if (((um->um_lowervp == NULLVP) || 239 (um->um_lowervp->v_mount->mnt_flag & MNT_LOCAL)) && 240 (um->um_uppervp->v_mount->mnt_flag & MNT_LOCAL)) 241 mp->mnt_flag |= MNT_LOCAL; 242 } 243 244 /* 245 * Copy in the upper layer's RDONLY flag. This is for the benefit 246 * of lookup() which explicitly checks the flag, rather than asking 247 * the filesystem for it's own opinion. This means, that an update 248 * mount of the underlying filesystem to go from rdonly to rdwr 249 * will leave the unioned view as read-only. 250 */ 251 mp->mnt_flag |= (um->um_uppervp->v_mount->mnt_flag & MNT_RDONLY); 252 253 mp->mnt_data = um; 254 vfs_getnewfsid(mp); 255 256 error = set_statvfs_info( path, UIO_USERSPACE, NULL, UIO_USERSPACE, 257 mp->mnt_op->vfs_name, mp, l); 258 if (error) 259 goto bad; 260 261 switch (um->um_op) { 262 case UNMNT_ABOVE: 263 cp = "<above>:"; 264 break; 265 case UNMNT_BELOW: 266 cp = "<below>:"; 267 break; 268 case UNMNT_REPLACE: 269 cp = ""; 270 break; 271 default: 272 cp = "<invalid>:"; 273 #ifdef DIAGNOSTIC 274 panic("union_mount: bad um_op"); 275 #endif 276 break; 277 } 278 len = strlen(cp); 279 memcpy(mp->mnt_stat.f_mntfromname, cp, len); 280 281 xp = mp->mnt_stat.f_mntfromname + len; 282 len = MNAMELEN - len; 283 284 (void) copyinstr(args->target, xp, len - 1, &size); 285 memset(xp + size, 0, len - size); 286 287 #ifdef UNION_DIAGNOSTIC 288 printf("union_mount: from %s, on %s\n", 289 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname); 290 #endif 291 292 /* Setup the readdir hook if it's not set already */ 293 if (!vn_union_readdir_hook) 294 vn_union_readdir_hook = union_readdirhook; 295 296 return (0); 297 298 bad: 299 if (um) 300 free(um, M_UFSMNT); 301 if (upperrootvp) 302 vrele(upperrootvp); 303 if (lowerrootvp) 304 vrele(lowerrootvp); 305 return (error); 306 } 307 308 /* 309 * VFS start. Nothing needed here - the start routine 310 * on the underlying filesystem(s) will have been called 311 * when that filesystem was mounted. 312 */ 313 /*ARGSUSED*/ 314 int 315 union_start(struct mount *mp, int flags) 316 { 317 318 return (0); 319 } 320 321 /* 322 * Free reference to union layer 323 */ 324 int 325 union_unmount(struct mount *mp, int mntflags) 326 { 327 struct union_mount *um = MOUNTTOUNIONMOUNT(mp); 328 int freeing; 329 int error; 330 331 #ifdef UNION_DIAGNOSTIC 332 printf("union_unmount(mp = %p)\n", mp); 333 #endif 334 335 /* 336 * Keep flushing vnodes from the mount list. 337 * This is needed because of the un_pvp held 338 * reference to the parent vnode. 339 * If more vnodes have been freed on a given pass, 340 * the try again. The loop will iterate at most 341 * (d) times, where (d) is the maximum tree depth 342 * in the filesystem. 343 */ 344 for (freeing = 0; (error = vflush(mp, NULL, 0)) != 0;) { 345 struct vnode *vp; 346 int n; 347 348 /* count #vnodes held on mount list */ 349 n = 0; 350 TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) 351 n++; 352 353 /* if this is unchanged then stop */ 354 if (n == freeing) 355 break; 356 357 /* otherwise try once more time */ 358 freeing = n; 359 } 360 361 /* 362 * Ok, now that we've tried doing it gently, get out the hammer. 363 */ 364 365 if (mntflags & MNT_FORCE) 366 error = vflush(mp, NULL, FORCECLOSE); 367 368 if (error) 369 return error; 370 371 /* 372 * Discard references to upper and lower target vnodes. 373 */ 374 if (um->um_lowervp) 375 vrele(um->um_lowervp); 376 vrele(um->um_uppervp); 377 kauth_cred_free(um->um_cred); 378 /* 379 * Finally, throw away the union_mount structure 380 */ 381 free(mp->mnt_data, M_UFSMNT); /* XXX */ 382 mp->mnt_data = NULL; 383 return (0); 384 } 385 386 int 387 union_root(struct mount *mp, struct vnode **vpp) 388 { 389 struct union_mount *um = MOUNTTOUNIONMOUNT(mp); 390 int error; 391 392 /* 393 * Return locked reference to root. 394 */ 395 vref(um->um_uppervp); 396 vn_lock(um->um_uppervp, LK_EXCLUSIVE | LK_RETRY); 397 if (um->um_lowervp) 398 vref(um->um_lowervp); 399 error = union_allocvp(vpp, mp, NULL, NULL, NULL, 400 um->um_uppervp, um->um_lowervp, 1); 401 402 if (error) { 403 vput(um->um_uppervp); 404 if (um->um_lowervp) 405 vrele(um->um_lowervp); 406 } 407 408 return (error); 409 } 410 411 int 412 union_statvfs(struct mount *mp, struct statvfs *sbp) 413 { 414 int error; 415 struct union_mount *um = MOUNTTOUNIONMOUNT(mp); 416 struct statvfs *sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK | M_ZERO); 417 unsigned long lbsize; 418 419 #ifdef UNION_DIAGNOSTIC 420 printf("union_statvfs(mp = %p, lvp = %p, uvp = %p)\n", mp, 421 um->um_lowervp, um->um_uppervp); 422 #endif 423 424 if (um->um_lowervp) { 425 error = VFS_STATVFS(um->um_lowervp->v_mount, sbuf); 426 if (error) 427 goto done; 428 } 429 430 /* now copy across the "interesting" information and fake the rest */ 431 lbsize = sbuf->f_bsize; 432 sbp->f_blocks = sbuf->f_blocks - sbuf->f_bfree; 433 sbp->f_files = sbuf->f_files - sbuf->f_ffree; 434 435 error = VFS_STATVFS(um->um_uppervp->v_mount, sbuf); 436 if (error) 437 goto done; 438 439 sbp->f_flag = sbuf->f_flag; 440 sbp->f_bsize = sbuf->f_bsize; 441 sbp->f_frsize = sbuf->f_frsize; 442 sbp->f_iosize = sbuf->f_iosize; 443 444 /* 445 * The "total" fields count total resources in all layers, 446 * the "free" fields count only those resources which are 447 * free in the upper layer (since only the upper layer 448 * is writable). 449 */ 450 451 if (sbuf->f_bsize != lbsize) 452 sbp->f_blocks = sbp->f_blocks * lbsize / sbuf->f_bsize; 453 sbp->f_blocks += sbuf->f_blocks; 454 sbp->f_bfree = sbuf->f_bfree; 455 sbp->f_bavail = sbuf->f_bavail; 456 sbp->f_bresvd = sbuf->f_bresvd; 457 sbp->f_files += sbuf->f_files; 458 sbp->f_ffree = sbuf->f_ffree; 459 sbp->f_favail = sbuf->f_favail; 460 sbp->f_fresvd = sbuf->f_fresvd; 461 462 copy_statvfs_info(sbp, mp); 463 done: 464 free(sbuf, M_TEMP); 465 return error; 466 } 467 468 /*ARGSUSED*/ 469 int 470 union_sync(struct mount *mp, int waitfor, 471 kauth_cred_t cred) 472 { 473 474 /* 475 * XXX - Assumes no data cached at union layer. 476 */ 477 return (0); 478 } 479 480 /*ARGSUSED*/ 481 int 482 union_vget(struct mount *mp, ino_t ino, 483 struct vnode **vpp) 484 { 485 486 return (EOPNOTSUPP); 487 } 488 489 static int 490 union_renamelock_enter(struct mount *mp) 491 { 492 struct union_mount *um = MOUNTTOUNIONMOUNT(mp); 493 494 /* Lock just the upper fs, where the action happens. */ 495 return VFS_RENAMELOCK_ENTER(um->um_uppervp->v_mount); 496 } 497 498 static void 499 union_renamelock_exit(struct mount *mp) 500 { 501 struct union_mount *um = MOUNTTOUNIONMOUNT(mp); 502 503 VFS_RENAMELOCK_EXIT(um->um_uppervp->v_mount); 504 } 505 506 extern const struct vnodeopv_desc union_vnodeop_opv_desc; 507 508 const struct vnodeopv_desc * const union_vnodeopv_descs[] = { 509 &union_vnodeop_opv_desc, 510 NULL, 511 }; 512 513 struct vfsops union_vfsops = { 514 MOUNT_UNION, 515 sizeof (struct union_args), 516 union_mount, 517 union_start, 518 union_unmount, 519 union_root, 520 (void *)eopnotsupp, /* vfs_quotactl */ 521 union_statvfs, 522 union_sync, 523 union_vget, 524 (void *)eopnotsupp, /* vfs_fhtovp */ 525 (void *)eopnotsupp, /* vfs_vptofh */ 526 union_init, 527 NULL, /* vfs_reinit */ 528 union_done, 529 NULL, /* vfs_mountroot */ 530 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp, 531 vfs_stdextattrctl, 532 (void *)eopnotsupp, /* vfs_suspendctl */ 533 union_renamelock_enter, 534 union_renamelock_exit, 535 (void *)eopnotsupp, 536 union_vnodeopv_descs, 537 0, /* vfs_refcount */ 538 { NULL, NULL }, 539 }; 540 541 static int 542 union_modcmd(modcmd_t cmd, void *arg) 543 { 544 int error; 545 546 switch (cmd) { 547 case MODULE_CMD_INIT: 548 error = vfs_attach(&union_vfsops); 549 if (error != 0) 550 break; 551 sysctl_createv(&union_sysctl_log, 0, NULL, NULL, 552 CTLFLAG_PERMANENT, 553 CTLTYPE_NODE, "vfs", NULL, 554 NULL, 0, NULL, 0, 555 CTL_VFS, CTL_EOL); 556 sysctl_createv(&union_sysctl_log, 0, NULL, NULL, 557 CTLFLAG_PERMANENT, 558 CTLTYPE_NODE, "union", 559 SYSCTL_DESCR("Union file system"), 560 NULL, 0, NULL, 0, 561 CTL_VFS, 15, CTL_EOL); 562 /* 563 * XXX the "15" above could be dynamic, thereby eliminating 564 * one more instance of the "number to vfs" mapping problem, 565 * but "15" is the order as taken from sys/mount.h 566 */ 567 break; 568 case MODULE_CMD_FINI: 569 error = vfs_detach(&union_vfsops); 570 if (error != 0) 571 break; 572 sysctl_teardown(&union_sysctl_log); 573 break; 574 default: 575 error = ENOTTY; 576 break; 577 } 578 579 return (error); 580 } 581