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 MNT_ILOCK(mp); 90 mp->mnt_flag |= MNT_NOCLUSTERW; 91 MNT_IUNLOCK(mp); 92 93 if (mp->mnt_flag & MNT_ROOTFS) 94 return (EOPNOTSUPP); 95 /* 96 * Update is a no-op 97 */ 98 if (mp->mnt_flag & MNT_UPDATE) 99 /* 100 * Need to provide: 101 * 1. a way to convert between rdonly and rdwr mounts. 102 * 2. support for nfs exports. 103 */ 104 return (EOPNOTSUPP); 105 106 /* 107 * Get arguments. 108 */ 109 error = vfs_getopt(opts, "target", (void **)&target, &len); 110 if (error || target[len - 1] != '\0') 111 return (EINVAL); 112 113 op = 0; 114 if (vfs_getopt(opts, "below", NULL, NULL) == 0) 115 op = UNMNT_BELOW; 116 if (vfs_getopt(opts, "replace", NULL, NULL) == 0) { 117 /* These options are mutually exclusive. */ 118 if (op) 119 return (EINVAL); 120 op = UNMNT_REPLACE; 121 } 122 /* 123 * UNMNT_ABOVE is the default. 124 */ 125 if (op == 0) 126 op = UNMNT_ABOVE; 127 128 /* 129 * Obtain lower vnode. Vnode is stored in mp->mnt_vnodecovered. 130 * We need to reference it but not lock it. 131 */ 132 lowerrootvp = mp->mnt_vnodecovered; 133 VREF(lowerrootvp); 134 /* 135 * Obtain upper vnode by calling namei() on the path. The 136 * upperrootvp will be turned referenced and locked. 137 */ 138 NDINIT(ndp, LOOKUP, FOLLOW|LOCKLEAF, UIO_SYSSPACE, target, td); 139 error = namei(ndp); 140 if (error) 141 goto bad; 142 NDFREE(ndp, NDF_ONLY_PNBUF); 143 upperrootvp = ndp->ni_vp; 144 145 UDEBUG(("mount_root UPPERVP %p locked = %d\n", upperrootvp, 146 VOP_ISLOCKED(upperrootvp, NULL))); 147 148 /* 149 * Check multi union mount to avoid `lock myself again' panic. 150 * Also require that it be a directory. 151 */ 152 if (upperrootvp == VTOUNION(lowerrootvp)->un_uppervp) { 153 #ifdef DIAGNOSTIC 154 printf("union_mount: multi union mount?\n"); 155 #endif 156 error = EDEADLK; 157 goto bad; 158 } 159 160 if (upperrootvp->v_type != VDIR) { 161 error = EINVAL; 162 goto bad; 163 } 164 165 /* 166 * Allocate our union_mount structure and populate the fields. 167 * The vnode references are stored in the union_mount as held, 168 * unlocked references. Depending on the _BELOW flag, the 169 * filesystems are viewed in a different order. In effect this 170 * is the same as providing a mount-under option to the mount 171 * syscall. 172 */ 173 174 um = (struct union_mount *) malloc(sizeof(struct union_mount), 175 M_UNIONFSMNT, M_WAITOK | M_ZERO); 176 177 um->um_op = op; 178 179 error = VOP_GETATTR(upperrootvp, &va, td->td_ucred, td); 180 if (error) 181 goto bad; 182 183 um->um_upperdev = va.va_fsid; 184 185 switch (um->um_op) { 186 case UNMNT_ABOVE: 187 um->um_lowervp = lowerrootvp; 188 um->um_uppervp = upperrootvp; 189 upperrootvp = NULL; 190 lowerrootvp = NULL; 191 break; 192 193 case UNMNT_BELOW: 194 VOP_UNLOCK(upperrootvp, 0, td); 195 vn_lock(lowerrootvp, LK_RETRY|LK_EXCLUSIVE, td); 196 um->um_lowervp = upperrootvp; 197 um->um_uppervp = lowerrootvp; 198 upperrootvp = NULL; 199 lowerrootvp = NULL; 200 break; 201 202 case UNMNT_REPLACE: 203 vrele(lowerrootvp); 204 lowerrootvp = NULL; 205 um->um_uppervp = upperrootvp; 206 um->um_lowervp = lowerrootvp; 207 upperrootvp = NULL; 208 break; 209 210 default: 211 error = EINVAL; 212 goto bad; 213 } 214 215 /* 216 * Unless the mount is readonly, ensure that the top layer 217 * supports whiteout operations. 218 */ 219 if ((mp->mnt_flag & MNT_RDONLY) == 0) { 220 /* 221 * XXX Fake up a struct componentname with only cn_nameiop 222 * and cn_thread valid; union_whiteout() needs to use the 223 * thread pointer to lock the vnode. 224 */ 225 bzero(&fakecn, sizeof(fakecn)); 226 fakecn.cn_nameiop = LOOKUP; 227 fakecn.cn_thread = td; 228 error = VOP_WHITEOUT(um->um_uppervp, &fakecn, LOOKUP); 229 if (error) 230 goto bad; 231 } 232 VOP_UNLOCK(um->um_uppervp, 0, td); 233 234 um->um_cred = crhold(td->td_ucred); 235 FILEDESC_LOCK_FAST(td->td_proc->p_fd); 236 um->um_cmode = UN_DIRMODE &~ td->td_proc->p_fd->fd_cmask; 237 FILEDESC_UNLOCK_FAST(td->td_proc->p_fd); 238 239 /* 240 * Depending on what you think the MNT_LOCAL flag might mean, 241 * you may want the && to be || on the conditional below. 242 * At the moment it has been defined that the filesystem is 243 * only local if it is all local, ie the MNT_LOCAL flag implies 244 * that the entire namespace is local. If you think the MNT_LOCAL 245 * flag implies that some of the files might be stored locally 246 * then you will want to change the conditional. 247 */ 248 if (um->um_op == UNMNT_ABOVE) { 249 if (((um->um_lowervp == NULLVP) || 250 (um->um_lowervp->v_mount->mnt_flag & MNT_LOCAL)) && 251 (um->um_uppervp->v_mount->mnt_flag & MNT_LOCAL)) { 252 MNT_ILOCK(mp); 253 mp->mnt_flag |= MNT_LOCAL; 254 MNT_IUNLOCK(mp); 255 } 256 } 257 258 /* 259 * Copy in the upper layer's RDONLY flag. This is for the benefit 260 * of lookup() which explicitly checks the flag, rather than asking 261 * the filesystem for its own opinion. This means, that an update 262 * mount of the underlying filesystem to go from rdonly to rdwr 263 * will leave the unioned view as read-only. 264 */ 265 MNT_ILOCK(mp); 266 mp->mnt_flag |= (um->um_uppervp->v_mount->mnt_flag & MNT_RDONLY); 267 MNT_IUNLOCK(mp); 268 269 mp->mnt_data = (qaddr_t) um; 270 vfs_getnewfsid(mp); 271 272 switch (um->um_op) { 273 case UNMNT_ABOVE: 274 cp = "<above>:"; 275 break; 276 case UNMNT_BELOW: 277 cp = "<below>:"; 278 break; 279 case UNMNT_REPLACE: 280 cp = ""; 281 break; 282 } 283 len = strlen(cp); 284 bcopy(cp, mp->mnt_stat.f_mntfromname, len); 285 286 cp = mp->mnt_stat.f_mntfromname + len; 287 len = MNAMELEN - len; 288 289 (void) copystr(target, cp, len - 1, &size); 290 bzero(cp + size, len - size); 291 292 UDEBUG(("union_mount: from %s, on %s\n", 293 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname)); 294 return (0); 295 296 bad: 297 if (um) { 298 if (um->um_uppervp) 299 vput(um->um_uppervp); 300 if (um->um_lowervp) 301 vrele(um->um_lowervp); 302 /* XXX other fields */ 303 free(um, M_UNIONFSMNT); 304 } 305 if (upperrootvp) 306 vput(upperrootvp); 307 if (lowerrootvp) 308 vrele(lowerrootvp); 309 return (error); 310 } 311 312 /* 313 * Free reference to union layer. 314 */ 315 static int 316 union_unmount(mp, mntflags, td) 317 struct mount *mp; 318 int mntflags; 319 struct thread *td; 320 { 321 struct union_mount *um = MOUNTTOUNIONMOUNT(mp); 322 int error; 323 int freeing; 324 int flags = 0; 325 326 UDEBUG(("union_unmount(mp = %p)\n", (void *)mp)); 327 328 if (mntflags & MNT_FORCE) 329 flags |= FORCECLOSE; 330 331 /* 332 * Keep flushing vnodes from the mount list. 333 * This is needed because of the un_pvp held 334 * reference to the parent vnode. 335 * If more vnodes have been freed on a given pass, 336 * the try again. The loop will iterate at most 337 * (d) times, where (d) is the maximum tree depth 338 * in the filesystem. 339 */ 340 for (freeing = 0; (error = vflush(mp, 0, flags, td)) != 0;) { 341 int n; 342 343 /* count #vnodes held on mount list */ 344 n = mp->mnt_nvnodelistsize; 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 * If the most recent vflush failed, the filesystem is still busy. 356 */ 357 if (error) 358 return (error); 359 360 /* 361 * Discard references to upper and lower target vnodes. 362 */ 363 if (um->um_lowervp) 364 vrele(um->um_lowervp); 365 vrele(um->um_uppervp); 366 crfree(um->um_cred); 367 /* 368 * Finally, throw away the union_mount structure. 369 */ 370 free(mp->mnt_data, M_UNIONFSMNT); /* XXX */ 371 mp->mnt_data = 0; 372 return (0); 373 } 374 375 static int 376 union_root(mp, flags, vpp, td) 377 struct mount *mp; 378 int flags; 379 struct vnode **vpp; 380 struct thread *td; 381 { 382 struct union_mount *um = MOUNTTOUNIONMOUNT(mp); 383 int error; 384 385 /* 386 * Supply an unlocked reference to um_uppervp and to um_lowervp. It 387 * is possible for um_uppervp to be locked without the associated 388 * root union_node being locked. We let union_allocvp() deal with 389 * it. 390 */ 391 UDEBUG(("union_root UPPERVP %p locked = %d\n", um->um_uppervp, 392 VOP_ISLOCKED(um->um_uppervp, NULL))); 393 394 VREF(um->um_uppervp); 395 if (um->um_lowervp) 396 VREF(um->um_lowervp); 397 398 error = union_allocvp(vpp, mp, NULLVP, NULLVP, NULL, 399 um->um_uppervp, um->um_lowervp, 1); 400 UDEBUG(("error %d\n", error)); 401 UDEBUG(("union_root2 UPPERVP %p locked = %d\n", um->um_uppervp, 402 VOP_ISLOCKED(um->um_uppervp, NULL))); 403 404 return (error); 405 } 406 407 static int 408 union_statfs(mp, sbp, td) 409 struct mount *mp; 410 struct statfs *sbp; 411 struct thread *td; 412 { 413 int error; 414 struct union_mount *um = MOUNTTOUNIONMOUNT(mp); 415 struct statfs mstat; 416 int lbsize; 417 418 UDEBUG(("union_statfs(mp = %p, lvp = %p, uvp = %p)\n", 419 (void *)mp, (void *)um->um_lowervp, (void *)um->um_uppervp)); 420 421 bzero(&mstat, sizeof(mstat)); 422 423 if (um->um_lowervp) { 424 error = VFS_STATFS(um->um_lowervp->v_mount, &mstat, td); 425 if (error) 426 return (error); 427 } 428 429 /* 430 * Now copy across the "interesting" information and fake the rest. 431 */ 432 #if 0 433 sbp->f_type = mstat.f_type; 434 sbp->f_flags = mstat.f_flags; 435 sbp->f_bsize = mstat.f_bsize; 436 sbp->f_iosize = mstat.f_iosize; 437 #endif 438 lbsize = mstat.f_bsize; 439 sbp->f_blocks = mstat.f_blocks; 440 sbp->f_bfree = mstat.f_bfree; 441 sbp->f_bavail = mstat.f_bavail; 442 sbp->f_files = mstat.f_files; 443 sbp->f_ffree = mstat.f_ffree; 444 445 error = VFS_STATFS(um->um_uppervp->v_mount, &mstat, td); 446 if (error) 447 return (error); 448 449 sbp->f_flags = mstat.f_flags; 450 sbp->f_bsize = mstat.f_bsize; 451 sbp->f_iosize = mstat.f_iosize; 452 453 /* 454 * If the lower and upper blocksizes differ, then frig the 455 * block counts so that the sizes reported by df make some 456 * kind of sense. None of this makes sense though. 457 */ 458 459 if (mstat.f_bsize != lbsize) 460 sbp->f_blocks = ((off_t) sbp->f_blocks * lbsize) / mstat.f_bsize; 461 462 /* 463 * The "total" fields count total resources in all layers, 464 * the "free" fields count only those resources which are 465 * free in the upper layer (since only the upper layer 466 * is writeable). 467 */ 468 sbp->f_blocks += mstat.f_blocks; 469 sbp->f_bfree = mstat.f_bfree; 470 sbp->f_bavail = mstat.f_bavail; 471 sbp->f_files += mstat.f_files; 472 sbp->f_ffree = mstat.f_ffree; 473 return (0); 474 } 475 476 static struct vfsops union_vfsops = { 477 .vfs_init = union_init, 478 .vfs_mount = union_mount, 479 .vfs_root = union_root, 480 .vfs_statfs = union_statfs, 481 .vfs_unmount = union_unmount, 482 }; 483 484 VFS_SET(union_vfsops, unionfs, VFCF_LOOPBACK); 485