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