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