1 /* 2 * Copyright (c) 1994, 1995 The Regents of the University of California. 3 * Copyright (c) 1994, 1995 Jan-Simon Pendry. 4 * All rights reserved. 5 * 6 * This code is derived from software donated to Berkeley by 7 * Jan-Simon Pendry. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the University of 20 * California, Berkeley and its contributors. 21 * 4. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * @(#)union_vfsops.c 8.20 (Berkeley) 5/20/95 38 * $FreeBSD$ 39 */ 40 41 /* 42 * Union Layer 43 */ 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/kernel.h> 48 #include <sys/lock.h> 49 #include <sys/mutex.h> 50 #include <sys/proc.h> 51 #include <sys/vnode.h> 52 #include <sys/mount.h> 53 #include <sys/namei.h> 54 #include <sys/malloc.h> 55 #include <sys/filedesc.h> 56 #include <fs/unionfs/union.h> 57 58 static MALLOC_DEFINE(M_UNIONFSMNT, "UNION mount", "UNION mount structure"); 59 60 extern int union_init(struct vfsconf *); 61 static int union_mount(struct mount *mp, struct nameidata *ndp, 62 struct thread *td); 63 static int union_root(struct mount *mp, struct vnode **vpp); 64 static int union_statfs(struct mount *mp, struct statfs *sbp, 65 struct thread *td); 66 static int union_unmount(struct mount *mp, int mntflags, 67 struct thread *td); 68 69 /* 70 * Mount union filesystem. 71 */ 72 static int 73 union_mount(mp, ndp, td) 74 struct mount *mp; 75 struct nameidata *ndp; 76 struct thread *td; 77 { 78 int error = 0; 79 struct vfsoptlist *opts; 80 struct vnode *lowerrootvp = NULLVP; 81 struct vnode *upperrootvp = NULLVP; 82 struct union_mount *um = 0; 83 struct ucred *cred = 0; 84 char *cp = 0, *target; 85 int op; 86 int len; 87 size_t size; 88 struct componentname fakecn; 89 90 UDEBUG(("union_mount(mp = %p)\n", (void *)mp)); 91 92 opts = mp->mnt_optnew; 93 /* 94 * Disable clustered write, otherwise system becomes unstable. 95 */ 96 mp->mnt_flag |= MNT_NOCLUSTERW; 97 98 /* 99 * Update is a no-op 100 */ 101 if (mp->mnt_flag & MNT_UPDATE) 102 /* 103 * Need to provide: 104 * 1. a way to convert between rdonly and rdwr mounts. 105 * 2. support for nfs exports. 106 */ 107 return (EOPNOTSUPP); 108 109 /* 110 * Get arguments. 111 */ 112 error = vfs_getopt(opts, "target", (void **)&target, &len); 113 if (error || target[len - 1] != '\0') 114 return (EINVAL); 115 116 op = 0; 117 if (vfs_getopt(opts, "below", NULL, NULL) == 0) 118 op = UNMNT_BELOW; 119 if (vfs_getopt(opts, "replace", NULL, NULL) == 0) { 120 /* These options are mutually exclusive. */ 121 if (op) 122 return (EINVAL); 123 op = UNMNT_REPLACE; 124 } 125 /* 126 * UNMNT_ABOVE is the default. 127 */ 128 if (op == 0) 129 op = UNMNT_ABOVE; 130 131 /* 132 * Obtain lower vnode. Vnode is stored in mp->mnt_vnodecovered. 133 * We need to reference it but not lock it. 134 */ 135 136 lowerrootvp = mp->mnt_vnodecovered; 137 VREF(lowerrootvp); 138 139 #if 0 140 /* 141 * Unlock lower node to avoid deadlock. 142 */ 143 if (lowerrootvp->v_op == union_vnodeop_p) 144 VOP_UNLOCK(lowerrootvp, 0, td); 145 #endif 146 147 /* 148 * Obtain upper vnode by calling namei() on the path. The 149 * upperrootvp will be turned referenced but not locked. 150 */ 151 NDINIT(ndp, LOOKUP, FOLLOW|WANTPARENT, UIO_SYSSPACE, target, td); 152 153 error = namei(ndp); 154 155 #if 0 156 if (lowerrootvp->v_op == union_vnodeop_p) 157 vn_lock(lowerrootvp, LK_EXCLUSIVE | LK_RETRY, td); 158 #endif 159 if (error) 160 goto bad; 161 162 NDFREE(ndp, NDF_ONLY_PNBUF); 163 upperrootvp = ndp->ni_vp; 164 vrele(ndp->ni_dvp); 165 ndp->ni_dvp = NULL; 166 167 UDEBUG(("mount_root UPPERVP %p locked = %d\n", upperrootvp, 168 VOP_ISLOCKED(upperrootvp, NULL))); 169 170 /* 171 * Check multi union mount to avoid `lock myself again' panic. 172 * Also require that it be a directory. 173 */ 174 if (upperrootvp == VTOUNION(lowerrootvp)->un_uppervp) { 175 #ifdef DIAGNOSTIC 176 printf("union_mount: multi union mount?\n"); 177 #endif 178 error = EDEADLK; 179 goto bad; 180 } 181 182 if (upperrootvp->v_type != VDIR) { 183 error = EINVAL; 184 goto bad; 185 } 186 187 /* 188 * Allocate our union_mount structure and populate the fields. 189 * The vnode references are stored in the union_mount as held, 190 * unlocked references. Depending on the _BELOW flag, the 191 * filesystems are viewed in a different order. In effect this 192 * is the same as providing a mount-under option to the mount 193 * syscall. 194 */ 195 196 um = (struct union_mount *) malloc(sizeof(struct union_mount), 197 M_UNIONFSMNT, M_WAITOK | M_ZERO); 198 199 um->um_op = op; 200 201 switch (um->um_op) { 202 case UNMNT_ABOVE: 203 um->um_lowervp = lowerrootvp; 204 um->um_uppervp = upperrootvp; 205 upperrootvp = NULL; 206 lowerrootvp = NULL; 207 break; 208 209 case UNMNT_BELOW: 210 um->um_lowervp = upperrootvp; 211 um->um_uppervp = lowerrootvp; 212 upperrootvp = NULL; 213 lowerrootvp = NULL; 214 break; 215 216 case UNMNT_REPLACE: 217 vrele(lowerrootvp); 218 lowerrootvp = NULL; 219 um->um_uppervp = upperrootvp; 220 um->um_lowervp = lowerrootvp; 221 upperrootvp = NULL; 222 break; 223 224 default: 225 error = EINVAL; 226 goto bad; 227 } 228 229 /* 230 * Unless the mount is readonly, ensure that the top layer 231 * supports whiteout operations. 232 */ 233 if ((mp->mnt_flag & MNT_RDONLY) == 0) { 234 /* 235 * XXX Fake up a struct componentname with only cn_nameiop 236 * and cn_thread valid; union_whiteout() needs to use the 237 * thread pointer to lock the vnode. 238 */ 239 bzero(&fakecn, sizeof(fakecn)); 240 fakecn.cn_nameiop = LOOKUP; 241 fakecn.cn_thread = td; 242 error = VOP_WHITEOUT(um->um_uppervp, &fakecn, LOOKUP); 243 if (error) 244 goto bad; 245 } 246 247 um->um_cred = crhold(td->td_ucred); 248 FILEDESC_LOCK(td->td_proc->p_fd); 249 um->um_cmode = UN_DIRMODE &~ td->td_proc->p_fd->fd_cmask; 250 FILEDESC_UNLOCK(td->td_proc->p_fd); 251 252 /* 253 * Depending on what you think the MNT_LOCAL flag might mean, 254 * you may want the && to be || on the conditional below. 255 * At the moment it has been defined that the filesystem is 256 * only local if it is all local, ie the MNT_LOCAL flag implies 257 * that the entire namespace is local. If you think the MNT_LOCAL 258 * flag implies that some of the files might be stored locally 259 * then you will want to change the conditional. 260 */ 261 if (um->um_op == UNMNT_ABOVE) { 262 if (((um->um_lowervp == NULLVP) || 263 (um->um_lowervp->v_mount->mnt_flag & MNT_LOCAL)) && 264 (um->um_uppervp->v_mount->mnt_flag & MNT_LOCAL)) 265 mp->mnt_flag |= MNT_LOCAL; 266 } 267 268 /* 269 * Copy in the upper layer's RDONLY flag. This is for the benefit 270 * of lookup() which explicitly checks the flag, rather than asking 271 * the filesystem for its own opinion. This means, that an update 272 * mount of the underlying filesystem to go from rdonly to rdwr 273 * will leave the unioned view as read-only. 274 */ 275 mp->mnt_flag |= (um->um_uppervp->v_mount->mnt_flag & MNT_RDONLY); 276 277 mp->mnt_data = (qaddr_t) um; 278 vfs_getnewfsid(mp); 279 280 switch (um->um_op) { 281 case UNMNT_ABOVE: 282 cp = "<above>:"; 283 break; 284 case UNMNT_BELOW: 285 cp = "<below>:"; 286 break; 287 case UNMNT_REPLACE: 288 cp = ""; 289 break; 290 } 291 len = strlen(cp); 292 bcopy(cp, mp->mnt_stat.f_mntfromname, len); 293 294 cp = mp->mnt_stat.f_mntfromname + len; 295 len = MNAMELEN - len; 296 297 (void) copystr(target, cp, len - 1, &size); 298 bzero(cp + size, len - size); 299 300 (void)union_statfs(mp, &mp->mnt_stat, td); 301 302 UDEBUG(("union_mount: from %s, on %s\n", 303 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname)); 304 return (0); 305 306 bad: 307 if (um) { 308 if (um->um_uppervp) 309 vrele(um->um_uppervp); 310 if (um->um_lowervp) 311 vrele(um->um_lowervp); 312 /* XXX other fields */ 313 free(um, M_UNIONFSMNT); 314 } 315 if (cred) 316 crfree(cred); 317 if (upperrootvp) 318 vrele(upperrootvp); 319 if (lowerrootvp) 320 vrele(lowerrootvp); 321 return (error); 322 } 323 324 /* 325 * Free reference to union layer. 326 */ 327 static int 328 union_unmount(mp, mntflags, td) 329 struct mount *mp; 330 int mntflags; 331 struct thread *td; 332 { 333 struct union_mount *um = MOUNTTOUNIONMOUNT(mp); 334 int error; 335 int freeing; 336 int flags = 0; 337 338 UDEBUG(("union_unmount(mp = %p)\n", (void *)mp)); 339 340 if (mntflags & MNT_FORCE) 341 flags |= FORCECLOSE; 342 343 /* 344 * Keep flushing vnodes from the mount list. 345 * This is needed because of the un_pvp held 346 * reference to the parent vnode. 347 * If more vnodes have been freed on a given pass, 348 * the try again. The loop will iterate at most 349 * (d) times, where (d) is the maximum tree depth 350 * in the filesystem. 351 */ 352 for (freeing = 0; (error = vflush(mp, 0, flags)) != 0;) { 353 struct vnode *vp; 354 int n; 355 356 /* count #vnodes held on mount list */ 357 mtx_lock(&mntvnode_mtx); 358 n = 0; 359 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) 360 n++; 361 mtx_unlock(&mntvnode_mtx); 362 363 /* if this is unchanged then stop */ 364 if (n == freeing) 365 break; 366 367 /* otherwise try once more time */ 368 freeing = n; 369 } 370 371 /* 372 * If the most recent vflush failed, the filesystem is still busy. 373 */ 374 if (error) 375 return (error); 376 377 /* 378 * Discard references to upper and lower target vnodes. 379 */ 380 if (um->um_lowervp) 381 vrele(um->um_lowervp); 382 vrele(um->um_uppervp); 383 crfree(um->um_cred); 384 /* 385 * Finally, throw away the union_mount structure. 386 */ 387 free(mp->mnt_data, M_UNIONFSMNT); /* XXX */ 388 mp->mnt_data = 0; 389 return (0); 390 } 391 392 static int 393 union_root(mp, vpp) 394 struct mount *mp; 395 struct vnode **vpp; 396 { 397 struct union_mount *um = MOUNTTOUNIONMOUNT(mp); 398 int error; 399 400 /* 401 * Supply an unlocked reference to um_uppervp and to um_lowervp. It 402 * is possible for um_uppervp to be locked without the associated 403 * root union_node being locked. We let union_allocvp() deal with 404 * it. 405 */ 406 UDEBUG(("union_root UPPERVP %p locked = %d\n", um->um_uppervp, 407 VOP_ISLOCKED(um->um_uppervp, NULL))); 408 409 VREF(um->um_uppervp); 410 if (um->um_lowervp) 411 VREF(um->um_lowervp); 412 413 error = union_allocvp(vpp, mp, NULLVP, NULLVP, NULL, 414 um->um_uppervp, um->um_lowervp, 1); 415 UDEBUG(("error %d\n", error)); 416 UDEBUG(("union_root2 UPPERVP %p locked = %d\n", um->um_uppervp, 417 VOP_ISLOCKED(um->um_uppervp, NULL))); 418 419 return (error); 420 } 421 422 static int 423 union_statfs(mp, sbp, td) 424 struct mount *mp; 425 struct statfs *sbp; 426 struct thread *td; 427 { 428 int error; 429 struct union_mount *um = MOUNTTOUNIONMOUNT(mp); 430 struct statfs mstat; 431 int lbsize; 432 433 UDEBUG(("union_statfs(mp = %p, lvp = %p, uvp = %p)\n", 434 (void *)mp, (void *)um->um_lowervp, (void *)um->um_uppervp)); 435 436 bzero(&mstat, sizeof(mstat)); 437 438 if (um->um_lowervp) { 439 error = VFS_STATFS(um->um_lowervp->v_mount, &mstat, td); 440 if (error) 441 return (error); 442 } 443 444 /* 445 * Now copy across the "interesting" information and fake the rest. 446 */ 447 #if 0 448 sbp->f_type = mstat.f_type; 449 sbp->f_flags = mstat.f_flags; 450 sbp->f_bsize = mstat.f_bsize; 451 sbp->f_iosize = mstat.f_iosize; 452 #endif 453 lbsize = mstat.f_bsize; 454 sbp->f_blocks = mstat.f_blocks; 455 sbp->f_bfree = mstat.f_bfree; 456 sbp->f_bavail = mstat.f_bavail; 457 sbp->f_files = mstat.f_files; 458 sbp->f_ffree = mstat.f_ffree; 459 460 error = VFS_STATFS(um->um_uppervp->v_mount, &mstat, td); 461 if (error) 462 return (error); 463 464 sbp->f_flags = mstat.f_flags; 465 sbp->f_bsize = mstat.f_bsize; 466 sbp->f_iosize = mstat.f_iosize; 467 468 /* 469 * If the lower and upper blocksizes differ, then frig the 470 * block counts so that the sizes reported by df make some 471 * kind of sense. None of this makes sense though. 472 */ 473 474 if (mstat.f_bsize != lbsize) 475 sbp->f_blocks = ((off_t) sbp->f_blocks * lbsize) / mstat.f_bsize; 476 477 /* 478 * The "total" fields count total resources in all layers, 479 * the "free" fields count only those resources which are 480 * free in the upper layer (since only the upper layer 481 * is writeable). 482 */ 483 sbp->f_blocks += mstat.f_blocks; 484 sbp->f_bfree = mstat.f_bfree; 485 sbp->f_bavail = mstat.f_bavail; 486 sbp->f_files += mstat.f_files; 487 sbp->f_ffree = mstat.f_ffree; 488 489 if (sbp != &mp->mnt_stat) { 490 sbp->f_type = mp->mnt_vfc->vfc_typenum; 491 bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid)); 492 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN); 493 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN); 494 } 495 return (0); 496 } 497 498 static struct vfsops union_vfsops = { 499 NULL, 500 vfs_stdstart, /* underlying start already done */ 501 union_unmount, 502 union_root, 503 vfs_stdquotactl, 504 union_statfs, 505 vfs_stdsync, /* XXX assumes no cached data on union level */ 506 vfs_stdvget, 507 vfs_stdfhtovp, 508 vfs_stdcheckexp, 509 vfs_stdvptofh, 510 union_init, 511 vfs_stduninit, 512 vfs_stdextattrctl, 513 union_mount, 514 }; 515 516 VFS_SET(union_vfsops, unionfs, VFCF_LOOPBACK); 517