| 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: src/sys/miscfs/union/union_vfsops.c,v 1.39.2.2 2001/10/25 19:18:53 dillon Exp $ |
| 39 | * $DragonFly: src/sys/vfs/union/union_vfsops.c,v 1.29 2006/12/23 00:41:30 swildner Exp $ |
| 40 | */ |
| 41 | |
| 42 | /* |
| 43 | * Union Layer |
| 44 | */ |
| 45 | |
| 46 | #include <sys/param.h> |
| 47 | #include <sys/systm.h> |
| 48 | #include <sys/kernel.h> |
| 49 | #include <sys/proc.h> |
| 50 | #include <sys/vnode.h> |
| 51 | #include <sys/mount.h> |
| 52 | #include <sys/nlookup.h> |
| 53 | #include <sys/namei.h> |
| 54 | #include <sys/malloc.h> |
| 55 | #include <sys/filedesc.h> |
| 56 | #include "union.h" |
| 57 | #include <vm/vm_zone.h> |
| 58 | |
| 59 | extern struct vop_ops union_vnode_vops; |
| 60 | |
| 61 | static MALLOC_DEFINE(M_UNIONFSMNT, "UNION mount", "UNION mount structure"); |
| 62 | |
| 63 | extern int union_init (struct vfsconf *); |
| 64 | static int union_mount (struct mount *mp, char *path, caddr_t data, |
| 65 | struct ucred *cred); |
| 66 | static int union_root (struct mount *mp, struct vnode **vpp); |
| 67 | static int union_statfs (struct mount *mp, struct statfs *sbp, |
| 68 | struct ucred *cred); |
| 69 | static int union_unmount (struct mount *mp, int mntflags); |
| 70 | |
| 71 | /* |
| 72 | * Mount union filesystem |
| 73 | */ |
| 74 | static int |
| 75 | union_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred) |
| 76 | { |
| 77 | int error = 0; |
| 78 | struct union_args args; |
| 79 | struct vnode *lowerrootvp = NULLVP; |
| 80 | struct vnode *upperrootvp = NULLVP; |
| 81 | struct union_mount *um = 0; |
| 82 | struct ucred *cred = 0; |
| 83 | struct nlookupdata nd; |
| 84 | char *cp = 0; |
| 85 | int len; |
| 86 | u_int size; |
| 87 | |
| 88 | UDEBUG(("union_mount(mp = %p)\n", (void *)mp)); |
| 89 | |
| 90 | /* |
| 91 | * Disable clustered write, otherwise system becomes unstable. |
| 92 | */ |
| 93 | mp->mnt_flag |= MNT_NOCLUSTERW; |
| 94 | |
| 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 | error = EOPNOTSUPP; |
| 105 | goto bad; |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * Get argument |
| 110 | */ |
| 111 | error = copyin(data, (caddr_t)&args, sizeof(struct union_args)); |
| 112 | if (error) |
| 113 | goto bad; |
| 114 | |
| 115 | /* |
| 116 | * Obtain lower vnode. |
| 117 | */ |
| 118 | |
| 119 | lowerrootvp = mp->mnt_vnodecovered; |
| 120 | vref(lowerrootvp); |
| 121 | |
| 122 | #if 0 |
| 123 | /* |
| 124 | * Unlock lower node to avoid deadlock. |
| 125 | */ |
| 126 | if (lowerrootvp->v_tag == VT_UNION) |
| 127 | vn_unlock(lowerrootvp); |
| 128 | #endif |
| 129 | |
| 130 | /* |
| 131 | * Obtain upper vnode by calling nlookup() on the path. The |
| 132 | * upperrootvp will be turned referenced but not locked. |
| 133 | */ |
| 134 | error = nlookup_init(&nd, args.target, UIO_USERSPACE, NLC_FOLLOW); |
| 135 | if (error == 0) |
| 136 | error = nlookup(&nd); |
| 137 | if (error == 0) |
| 138 | error = cache_vref(&nd.nl_nch, nd.nl_cred, &upperrootvp); |
| 139 | nlookup_done(&nd); |
| 140 | if (error) |
| 141 | goto bad; |
| 142 | |
| 143 | UDEBUG(("mount_root UPPERVP %p locked = %d\n", upperrootvp, |
| 144 | vn_islocked(upperrootvp))); |
| 145 | |
| 146 | /* |
| 147 | * Check multi union mount to avoid `lock myself again' panic. |
| 148 | * Also require that it be a directory. |
| 149 | */ |
| 150 | if (upperrootvp == VTOUNION(lowerrootvp)->un_uppervp) { |
| 151 | #ifdef DIAGNOSTIC |
| 152 | kprintf("union_mount: multi union mount?\n"); |
| 153 | #endif |
| 154 | error = EDEADLK; |
| 155 | goto bad; |
| 156 | } |
| 157 | |
| 158 | if (upperrootvp->v_type != VDIR) { |
| 159 | error = EINVAL; |
| 160 | goto bad; |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * Allocate our union_mount structure and populate the fields. |
| 165 | * The vnode references are stored in the union_mount as held, |
| 166 | * unlocked references. Depending on the _BELOW flag, the |
| 167 | * filesystems are viewed in a different order. In effect this |
| 168 | * is the same as providing a mount-under option to the mount |
| 169 | * syscall. |
| 170 | */ |
| 171 | |
| 172 | um = (struct union_mount *) malloc(sizeof(struct union_mount), |
| 173 | M_UNIONFSMNT, M_WAITOK); |
| 174 | |
| 175 | bzero(um, sizeof(struct union_mount)); |
| 176 | |
| 177 | um->um_op = args.mntflags & UNMNT_OPMASK; |
| 178 | |
| 179 | switch (um->um_op) { |
| 180 | case UNMNT_ABOVE: |
| 181 | um->um_lowervp = lowerrootvp; |
| 182 | um->um_uppervp = upperrootvp; |
| 183 | upperrootvp = NULL; |
| 184 | lowerrootvp = NULL; |
| 185 | break; |
| 186 | |
| 187 | case UNMNT_BELOW: |
| 188 | um->um_lowervp = upperrootvp; |
| 189 | um->um_uppervp = lowerrootvp; |
| 190 | upperrootvp = NULL; |
| 191 | lowerrootvp = NULL; |
| 192 | break; |
| 193 | |
| 194 | case UNMNT_REPLACE: |
| 195 | vrele(lowerrootvp); |
| 196 | lowerrootvp = NULL; |
| 197 | um->um_uppervp = upperrootvp; |
| 198 | um->um_lowervp = lowerrootvp; |
| 199 | upperrootvp = NULL; |
| 200 | break; |
| 201 | |
| 202 | default: |
| 203 | error = EINVAL; |
| 204 | goto bad; |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * Unless the mount is readonly, ensure that the top layer |
| 209 | * supports whiteout operations |
| 210 | */ |
| 211 | if ((mp->mnt_flag & MNT_RDONLY) == 0) { |
| 212 | error = VOP_WHITEOUT(um->um_uppervp, NULL, NAMEI_LOOKUP); |
| 213 | if (error) |
| 214 | goto bad; |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * File creds and modes for shadowed files are based on the user |
| 219 | * that did the mount. |
| 220 | */ |
| 221 | um->um_cred = crhold(cred); |
| 222 | um->um_cmode = UN_DIRMODE; |
| 223 | if (curproc) |
| 224 | um->um_cmode &= ~curproc->p_fd->fd_cmask; |
| 225 | |
| 226 | /* |
| 227 | * Depending on what you think the MNT_LOCAL flag might mean, |
| 228 | * you may want the && to be || on the conditional below. |
| 229 | * At the moment it has been defined that the filesystem is |
| 230 | * only local if it is all local, ie the MNT_LOCAL flag implies |
| 231 | * that the entire namespace is local. If you think the MNT_LOCAL |
| 232 | * flag implies that some of the files might be stored locally |
| 233 | * then you will want to change the conditional. |
| 234 | */ |
| 235 | if (um->um_op == UNMNT_ABOVE) { |
| 236 | if (((um->um_lowervp == NULLVP) || |
| 237 | (um->um_lowervp->v_mount->mnt_flag & MNT_LOCAL)) && |
| 238 | (um->um_uppervp->v_mount->mnt_flag & MNT_LOCAL)) |
| 239 | mp->mnt_flag |= MNT_LOCAL; |
| 240 | } |
| 241 | |
| 242 | /* |
| 243 | * Copy in the upper layer's RDONLY flag. This is for the benefit |
| 244 | * of lookup() which explicitly checks the flag, rather than asking |
| 245 | * the filesystem for its own opinion. This means, that an update |
| 246 | * mount of the underlying filesystem to go from rdonly to rdwr |
| 247 | * will leave the unioned view as read-only. |
| 248 | */ |
| 249 | mp->mnt_flag |= (um->um_uppervp->v_mount->mnt_flag & MNT_RDONLY); |
| 250 | |
| 251 | mp->mnt_data = (qaddr_t) um; |
| 252 | vfs_getnewfsid(mp); |
| 253 | |
| 254 | switch (um->um_op) { |
| 255 | case UNMNT_ABOVE: |
| 256 | cp = "<above>:"; |
| 257 | break; |
| 258 | case UNMNT_BELOW: |
| 259 | cp = "<below>:"; |
| 260 | break; |
| 261 | case UNMNT_REPLACE: |
| 262 | cp = ""; |
| 263 | break; |
| 264 | } |
| 265 | len = strlen(cp); |
| 266 | bcopy(cp, mp->mnt_stat.f_mntfromname, len); |
| 267 | |
| 268 | cp = mp->mnt_stat.f_mntfromname + len; |
| 269 | len = MNAMELEN - len; |
| 270 | |
| 271 | (void) copyinstr(args.target, cp, len - 1, &size); |
| 272 | bzero(cp + size, len - size); |
| 273 | |
| 274 | vfs_add_vnodeops(mp, &union_vnode_vops, &mp->mnt_vn_norm_ops); |
| 275 | |
| 276 | (void)union_statfs(mp, &mp->mnt_stat, cred); |
| 277 | |
| 278 | return (0); |
| 279 | |
| 280 | bad: |
| 281 | if (um) { |
| 282 | if (um->um_uppervp) |
| 283 | vrele(um->um_uppervp); |
| 284 | if (um->um_lowervp) |
| 285 | vrele(um->um_lowervp); |
| 286 | /* XXX other fields */ |
| 287 | kfree(um, M_UNIONFSMNT); |
| 288 | } |
| 289 | if (cred) |
| 290 | crfree(cred); |
| 291 | if (upperrootvp) |
| 292 | vrele(upperrootvp); |
| 293 | if (lowerrootvp) |
| 294 | vrele(lowerrootvp); |
| 295 | return (error); |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | * Free reference to union layer |
| 300 | */ |
| 301 | static int |
| 302 | union_unmount(struct mount *mp, int mntflags) |
| 303 | { |
| 304 | struct union_mount *um = MOUNTTOUNIONMOUNT(mp); |
| 305 | int error; |
| 306 | int freeing; |
| 307 | int flags = 0; |
| 308 | |
| 309 | UDEBUG(("union_unmount(mp = %p)\n", (void *)mp)); |
| 310 | |
| 311 | if (mntflags & MNT_FORCE) |
| 312 | flags |= FORCECLOSE; |
| 313 | |
| 314 | /* |
| 315 | * Keep flushing vnodes from the mount list. |
| 316 | * This is needed because of the un_pvp held |
| 317 | * reference to the parent vnode. |
| 318 | * If more vnodes have been freed on a given pass, |
| 319 | * the try again. The loop will iterate at most |
| 320 | * (d) times, where (d) is the maximum tree depth |
| 321 | * in the filesystem. |
| 322 | */ |
| 323 | for (freeing = 0; (error = vflush(mp, 0, flags)) != 0;) { |
| 324 | int n = mp->mnt_nvnodelistsize; |
| 325 | |
| 326 | /* if this is unchanged then stop */ |
| 327 | if (n == freeing) |
| 328 | break; |
| 329 | |
| 330 | /* otherwise try once more time */ |
| 331 | freeing = n; |
| 332 | } |
| 333 | |
| 334 | /* If the most recent vflush failed, the filesystem is still busy. */ |
| 335 | if (error) |
| 336 | return (error); |
| 337 | |
| 338 | /* |
| 339 | * Discard references to upper and lower target vnodes. |
| 340 | */ |
| 341 | if (um->um_lowervp) |
| 342 | vrele(um->um_lowervp); |
| 343 | vrele(um->um_uppervp); |
| 344 | crfree(um->um_cred); |
| 345 | /* |
| 346 | * Finally, throw away the union_mount structure |
| 347 | */ |
| 348 | kfree(mp->mnt_data, M_UNIONFSMNT); /* XXX */ |
| 349 | mp->mnt_data = 0; |
| 350 | return (0); |
| 351 | } |
| 352 | |
| 353 | static int |
| 354 | union_root(struct mount *mp, struct vnode **vpp) |
| 355 | { |
| 356 | struct union_mount *um = MOUNTTOUNIONMOUNT(mp); |
| 357 | int error; |
| 358 | |
| 359 | /* |
| 360 | * Supply an unlocked reference to um_uppervp and to um_lowervp. It |
| 361 | * is possible for um_uppervp to be locked without the associated |
| 362 | * root union_node being locked. We let union_allocvp() deal with |
| 363 | * it. |
| 364 | */ |
| 365 | UDEBUG(("union_root UPPERVP %p locked = %d\n", um->um_uppervp, |
| 366 | vn_islocked(um->um_uppervp))); |
| 367 | |
| 368 | vref(um->um_uppervp); |
| 369 | if (um->um_lowervp) |
| 370 | vref(um->um_lowervp); |
| 371 | |
| 372 | error = union_allocvp(vpp, mp, NULLVP, NULLVP, NULL, |
| 373 | um->um_uppervp, um->um_lowervp, 1); |
| 374 | UDEBUG(("error %d\n", error)); |
| 375 | UDEBUG(("union_root2 UPPERVP %p locked = %d\n", um->um_uppervp, |
| 376 | vn_islocked(um->um_uppervp))); |
| 377 | |
| 378 | return (error); |
| 379 | } |
| 380 | |
| 381 | static int |
| 382 | union_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred) |
| 383 | { |
| 384 | int error; |
| 385 | struct union_mount *um = MOUNTTOUNIONMOUNT(mp); |
| 386 | struct statfs mstat; |
| 387 | int lbsize; |
| 388 | |
| 389 | UDEBUG(("union_statfs(mp = %p, lvp = %p, uvp = %p)\n", |
| 390 | (void *)mp, (void *)um->um_lowervp, (void *)um->um_uppervp)); |
| 391 | |
| 392 | bzero(&mstat, sizeof(mstat)); |
| 393 | |
| 394 | if (um->um_lowervp) { |
| 395 | error = VFS_STATFS(um->um_lowervp->v_mount, &mstat, cred); |
| 396 | if (error) |
| 397 | return (error); |
| 398 | } |
| 399 | |
| 400 | /* now copy across the "interesting" information and fake the rest */ |
| 401 | #if 0 |
| 402 | sbp->f_type = mstat.f_type; |
| 403 | sbp->f_flags = mstat.f_flags; |
| 404 | sbp->f_bsize = mstat.f_bsize; |
| 405 | sbp->f_iosize = mstat.f_iosize; |
| 406 | #endif |
| 407 | lbsize = mstat.f_bsize; |
| 408 | sbp->f_blocks = mstat.f_blocks; |
| 409 | sbp->f_bfree = mstat.f_bfree; |
| 410 | sbp->f_bavail = mstat.f_bavail; |
| 411 | sbp->f_files = mstat.f_files; |
| 412 | sbp->f_ffree = mstat.f_ffree; |
| 413 | |
| 414 | error = VFS_STATFS(um->um_uppervp->v_mount, &mstat, cred); |
| 415 | if (error) |
| 416 | return (error); |
| 417 | |
| 418 | sbp->f_flags = mstat.f_flags; |
| 419 | sbp->f_bsize = mstat.f_bsize; |
| 420 | sbp->f_iosize = mstat.f_iosize; |
| 421 | |
| 422 | /* |
| 423 | * if the lower and upper blocksizes differ, then frig the |
| 424 | * block counts so that the sizes reported by df make some |
| 425 | * kind of sense. none of this makes sense though. |
| 426 | */ |
| 427 | |
| 428 | if (mstat.f_bsize != lbsize) |
| 429 | sbp->f_blocks = ((off_t) sbp->f_blocks * lbsize) / mstat.f_bsize; |
| 430 | |
| 431 | /* |
| 432 | * The "total" fields count total resources in all layers, |
| 433 | * the "free" fields count only those resources which are |
| 434 | * free in the upper layer (since only the upper layer |
| 435 | * is writeable). |
| 436 | */ |
| 437 | sbp->f_blocks += mstat.f_blocks; |
| 438 | sbp->f_bfree = mstat.f_bfree; |
| 439 | sbp->f_bavail = mstat.f_bavail; |
| 440 | sbp->f_files += mstat.f_files; |
| 441 | sbp->f_ffree = mstat.f_ffree; |
| 442 | |
| 443 | if (sbp != &mp->mnt_stat) { |
| 444 | sbp->f_type = mp->mnt_vfc->vfc_typenum; |
| 445 | bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid)); |
| 446 | bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN); |
| 447 | } |
| 448 | return (0); |
| 449 | } |
| 450 | |
| 451 | static struct vfsops union_vfsops = { |
| 452 | .vfs_mount = union_mount, |
| 453 | .vfs_unmount = union_unmount, |
| 454 | .vfs_root = union_root, |
| 455 | .vfs_statfs = union_statfs, |
| 456 | .vfs_sync = vfs_stdsync, |
| 457 | .vfs_init = union_init |
| 458 | }; |
| 459 | |
| 460 | VFS_SET(union_vfsops, union, VFCF_LOOPBACK); |