1 /*- 2 * Copyright (c) 1994, 1995 The Regents of the University of California. 3 * Copyright (c) 1994, 1995 Jan-Simon Pendry. 4 * Copyright (c) 2005, 2006 Masanori Ozawa <ozawa@ongs.co.jp>, ONGS Inc. 5 * Copyright (c) 2006 Daichi Goto <daichi@freebsd.org> 6 * All rights reserved. 7 * 8 * This code is derived from software donated to Berkeley by 9 * Jan-Simon Pendry. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)union_vfsops.c 8.20 (Berkeley) 5/20/95 36 * $FreeBSD$ 37 */ 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/kdb.h> 42 #include <sys/fcntl.h> 43 #include <sys/kernel.h> 44 #include <sys/lock.h> 45 #include <sys/malloc.h> 46 #include <sys/mount.h> 47 #include <sys/namei.h> 48 #include <sys/proc.h> 49 #include <sys/vnode.h> 50 #include <sys/stat.h> 51 52 #include <fs/unionfs/union.h> 53 54 static MALLOC_DEFINE(M_UNIONFSMNT, "UNIONFS mount", "UNIONFS mount structure"); 55 56 static vfs_fhtovp_t unionfs_fhtovp; 57 static vfs_checkexp_t unionfs_checkexp; 58 static vfs_mount_t unionfs_domount; 59 static vfs_quotactl_t unionfs_quotactl; 60 static vfs_root_t unionfs_root; 61 static vfs_sync_t unionfs_sync; 62 static vfs_statfs_t unionfs_statfs; 63 static vfs_unmount_t unionfs_unmount; 64 static vfs_vget_t unionfs_vget; 65 static vfs_extattrctl_t unionfs_extattrctl; 66 67 static struct vfsops unionfs_vfsops; 68 69 /* 70 * Mount unionfs layer. 71 */ 72 static int 73 unionfs_domount(struct mount *mp) 74 { 75 int error; 76 struct vnode *lowerrootvp; 77 struct vnode *upperrootvp; 78 struct unionfs_mount *ump; 79 struct thread *td; 80 char *target; 81 char *tmp; 82 char *ep; 83 int len; 84 size_t done; 85 int below; 86 uid_t uid; 87 gid_t gid; 88 u_short udir; 89 u_short ufile; 90 unionfs_copymode copymode; 91 unionfs_whitemode whitemode; 92 struct componentname fakecn; 93 struct nameidata nd, *ndp; 94 struct vattr va; 95 96 UNIONFSDEBUG("unionfs_mount(mp = %p)\n", (void *)mp); 97 98 error = 0; 99 below = 0; 100 uid = 0; 101 gid = 0; 102 udir = 0; 103 ufile = 0; 104 copymode = UNIONFS_TRANSPARENT; /* default */ 105 whitemode = UNIONFS_WHITE_ALWAYS; 106 ndp = &nd; 107 td = curthread; 108 109 if (mp->mnt_flag & MNT_ROOTFS) { 110 vfs_mount_error(mp, "Cannot union mount root filesystem"); 111 return (EOPNOTSUPP); 112 } 113 114 /* 115 * Update is a no operation. 116 */ 117 if (mp->mnt_flag & MNT_UPDATE) { 118 vfs_mount_error(mp, "unionfs does not support mount update"); 119 return (EOPNOTSUPP); 120 } 121 122 /* 123 * Get argument 124 */ 125 error = vfs_getopt(mp->mnt_optnew, "target", (void **)&target, &len); 126 if (error) 127 error = vfs_getopt(mp->mnt_optnew, "from", (void **)&target, 128 &len); 129 if (error || target[len - 1] != '\0') { 130 vfs_mount_error(mp, "Invalid target"); 131 return (EINVAL); 132 } 133 if (vfs_getopt(mp->mnt_optnew, "below", NULL, NULL) == 0) 134 below = 1; 135 if (vfs_getopt(mp->mnt_optnew, "udir", (void **)&tmp, NULL) == 0) { 136 if (tmp != NULL) 137 udir = (mode_t)strtol(tmp, &ep, 8); 138 if (tmp == NULL || *ep) { 139 vfs_mount_error(mp, "Invalid udir"); 140 return (EINVAL); 141 } 142 udir &= S_IRWXU | S_IRWXG | S_IRWXO; 143 } 144 if (vfs_getopt(mp->mnt_optnew, "ufile", (void **)&tmp, NULL) == 0) { 145 if (tmp != NULL) 146 ufile = (mode_t)strtol(tmp, &ep, 8); 147 if (tmp == NULL || *ep) { 148 vfs_mount_error(mp, "Invalid ufile"); 149 return (EINVAL); 150 } 151 ufile &= S_IRWXU | S_IRWXG | S_IRWXO; 152 } 153 /* check umask, uid and gid */ 154 if (udir == 0 && ufile != 0) 155 udir = ufile; 156 if (ufile == 0 && udir != 0) 157 ufile = udir; 158 159 vn_lock(mp->mnt_vnodecovered, LK_SHARED | LK_RETRY); 160 error = VOP_GETATTR(mp->mnt_vnodecovered, &va, mp->mnt_cred); 161 if (!error) { 162 if (udir == 0) 163 udir = va.va_mode; 164 if (ufile == 0) 165 ufile = va.va_mode; 166 uid = va.va_uid; 167 gid = va.va_gid; 168 } 169 VOP_UNLOCK(mp->mnt_vnodecovered, 0); 170 if (error) 171 return (error); 172 173 if (mp->mnt_cred->cr_ruid == 0) { /* root only */ 174 if (vfs_getopt(mp->mnt_optnew, "uid", (void **)&tmp, 175 NULL) == 0) { 176 if (tmp != NULL) 177 uid = (uid_t)strtol(tmp, &ep, 10); 178 if (tmp == NULL || *ep) { 179 vfs_mount_error(mp, "Invalid uid"); 180 return (EINVAL); 181 } 182 } 183 if (vfs_getopt(mp->mnt_optnew, "gid", (void **)&tmp, 184 NULL) == 0) { 185 if (tmp != NULL) 186 gid = (gid_t)strtol(tmp, &ep, 10); 187 if (tmp == NULL || *ep) { 188 vfs_mount_error(mp, "Invalid gid"); 189 return (EINVAL); 190 } 191 } 192 if (vfs_getopt(mp->mnt_optnew, "copymode", (void **)&tmp, 193 NULL) == 0) { 194 if (tmp == NULL) { 195 vfs_mount_error(mp, "Invalid copymode"); 196 return (EINVAL); 197 } else if (strcasecmp(tmp, "traditional") == 0) 198 copymode = UNIONFS_TRADITIONAL; 199 else if (strcasecmp(tmp, "transparent") == 0) 200 copymode = UNIONFS_TRANSPARENT; 201 else if (strcasecmp(tmp, "masquerade") == 0) 202 copymode = UNIONFS_MASQUERADE; 203 else { 204 vfs_mount_error(mp, "Invalid copymode"); 205 return (EINVAL); 206 } 207 } 208 if (vfs_getopt(mp->mnt_optnew, "whiteout", (void **)&tmp, 209 NULL) == 0) { 210 if (tmp == NULL) { 211 vfs_mount_error(mp, "Invalid whiteout mode"); 212 return (EINVAL); 213 } else if (strcasecmp(tmp, "always") == 0) 214 whitemode = UNIONFS_WHITE_ALWAYS; 215 else if (strcasecmp(tmp, "whenneeded") == 0) 216 whitemode = UNIONFS_WHITE_WHENNEEDED; 217 else { 218 vfs_mount_error(mp, "Invalid whiteout mode"); 219 return (EINVAL); 220 } 221 } 222 } 223 /* If copymode is UNIONFS_TRADITIONAL, uid/gid is mounted user. */ 224 if (copymode == UNIONFS_TRADITIONAL) { 225 uid = mp->mnt_cred->cr_ruid; 226 gid = mp->mnt_cred->cr_rgid; 227 } 228 229 UNIONFSDEBUG("unionfs_mount: uid=%d, gid=%d\n", uid, gid); 230 UNIONFSDEBUG("unionfs_mount: udir=0%03o, ufile=0%03o\n", udir, ufile); 231 UNIONFSDEBUG("unionfs_mount: copymode=%d\n", copymode); 232 233 /* 234 * Find upper node 235 */ 236 NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, target, td); 237 if ((error = namei(ndp))) 238 return (error); 239 240 NDFREE(ndp, NDF_ONLY_PNBUF); 241 242 /* get root vnodes */ 243 lowerrootvp = mp->mnt_vnodecovered; 244 upperrootvp = ndp->ni_vp; 245 246 /* create unionfs_mount */ 247 ump = (struct unionfs_mount *)malloc(sizeof(struct unionfs_mount), 248 M_UNIONFSMNT, M_WAITOK | M_ZERO); 249 250 /* 251 * Save reference 252 */ 253 if (below) { 254 VOP_UNLOCK(upperrootvp, 0); 255 vn_lock(lowerrootvp, LK_EXCLUSIVE | LK_RETRY); 256 ump->um_lowervp = upperrootvp; 257 ump->um_uppervp = lowerrootvp; 258 } else { 259 ump->um_lowervp = lowerrootvp; 260 ump->um_uppervp = upperrootvp; 261 } 262 ump->um_rootvp = NULLVP; 263 ump->um_uid = uid; 264 ump->um_gid = gid; 265 ump->um_udir = udir; 266 ump->um_ufile = ufile; 267 ump->um_copymode = copymode; 268 ump->um_whitemode = whitemode; 269 270 MNT_ILOCK(mp); 271 if ((lowerrootvp->v_mount->mnt_kern_flag & MNTK_MPSAFE) && 272 (upperrootvp->v_mount->mnt_kern_flag & MNTK_MPSAFE)) 273 mp->mnt_kern_flag |= MNTK_MPSAFE; 274 MNT_IUNLOCK(mp); 275 mp->mnt_data = ump; 276 277 /* 278 * Copy upper layer's RDONLY flag. 279 */ 280 mp->mnt_flag |= ump->um_uppervp->v_mount->mnt_flag & MNT_RDONLY; 281 282 /* 283 * Check whiteout 284 */ 285 if ((mp->mnt_flag & MNT_RDONLY) == 0) { 286 memset(&fakecn, 0, sizeof(fakecn)); 287 fakecn.cn_nameiop = LOOKUP; 288 fakecn.cn_thread = td; 289 error = VOP_WHITEOUT(ump->um_uppervp, &fakecn, LOOKUP); 290 if (error) { 291 if (below) { 292 VOP_UNLOCK(ump->um_uppervp, 0); 293 vrele(upperrootvp); 294 } else 295 vput(ump->um_uppervp); 296 free(ump, M_UNIONFSMNT); 297 mp->mnt_data = NULL; 298 return (error); 299 } 300 } 301 302 /* 303 * Unlock the node 304 */ 305 VOP_UNLOCK(ump->um_uppervp, 0); 306 307 /* 308 * Get the unionfs root vnode. 309 */ 310 error = unionfs_nodeget(mp, ump->um_uppervp, ump->um_lowervp, 311 NULLVP, &(ump->um_rootvp), NULL, td); 312 vrele(upperrootvp); 313 if (error) { 314 free(ump, M_UNIONFSMNT); 315 mp->mnt_data = NULL; 316 return (error); 317 } 318 319 /* 320 * Check mnt_flag 321 */ 322 if ((ump->um_lowervp->v_mount->mnt_flag & MNT_LOCAL) && 323 (ump->um_uppervp->v_mount->mnt_flag & MNT_LOCAL)) 324 mp->mnt_flag |= MNT_LOCAL; 325 326 /* 327 * Get new fsid 328 */ 329 vfs_getnewfsid(mp); 330 331 len = MNAMELEN - 1; 332 tmp = mp->mnt_stat.f_mntfromname; 333 copystr((below ? "<below>:" : "<above>:"), tmp, len, &done); 334 len -= done - 1; 335 tmp += done - 1; 336 copystr(target, tmp, len, NULL); 337 338 UNIONFSDEBUG("unionfs_mount: from %s, on %s\n", 339 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname); 340 341 return (0); 342 } 343 344 /* 345 * Free reference to unionfs layer 346 */ 347 static int 348 unionfs_unmount(struct mount *mp, int mntflags) 349 { 350 struct unionfs_mount *ump; 351 int error; 352 int num; 353 int freeing; 354 int flags; 355 356 UNIONFSDEBUG("unionfs_unmount: mp = %p\n", (void *)mp); 357 358 ump = MOUNTTOUNIONFSMOUNT(mp); 359 flags = 0; 360 361 if (mntflags & MNT_FORCE) 362 flags |= FORCECLOSE; 363 364 /* vflush (no need to call vrele) */ 365 for (freeing = 0; (error = vflush(mp, 1, flags, curthread)) != 0;) { 366 num = mp->mnt_nvnodelistsize; 367 if (num == freeing) 368 break; 369 freeing = num; 370 } 371 372 if (error) 373 return (error); 374 375 free(ump, M_UNIONFSMNT); 376 mp->mnt_data = 0; 377 378 return (0); 379 } 380 381 static int 382 unionfs_root(struct mount *mp, int flags, struct vnode **vpp) 383 { 384 struct unionfs_mount *ump; 385 struct vnode *vp; 386 387 ump = MOUNTTOUNIONFSMOUNT(mp); 388 vp = ump->um_rootvp; 389 390 UNIONFSDEBUG("unionfs_root: rootvp=%p locked=%x\n", 391 vp, VOP_ISLOCKED(vp)); 392 393 vref(vp); 394 if (flags & LK_TYPE_MASK) 395 vn_lock(vp, flags); 396 397 *vpp = vp; 398 399 return (0); 400 } 401 402 static int 403 unionfs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg) 404 { 405 struct unionfs_mount *ump; 406 407 ump = MOUNTTOUNIONFSMOUNT(mp); 408 409 /* 410 * Writing is always performed to upper vnode. 411 */ 412 return (VFS_QUOTACTL(ump->um_uppervp->v_mount, cmd, uid, arg)); 413 } 414 415 static int 416 unionfs_statfs(struct mount *mp, struct statfs *sbp) 417 { 418 struct unionfs_mount *ump; 419 int error; 420 struct statfs mstat; 421 uint64_t lbsize; 422 423 ump = MOUNTTOUNIONFSMOUNT(mp); 424 425 UNIONFSDEBUG("unionfs_statfs(mp = %p, lvp = %p, uvp = %p)\n", 426 (void *)mp, (void *)ump->um_lowervp, (void *)ump->um_uppervp); 427 428 bzero(&mstat, sizeof(mstat)); 429 430 error = VFS_STATFS(ump->um_lowervp->v_mount, &mstat); 431 if (error) 432 return (error); 433 434 /* now copy across the "interesting" information and fake the rest */ 435 sbp->f_blocks = mstat.f_blocks; 436 sbp->f_files = mstat.f_files; 437 438 lbsize = mstat.f_bsize; 439 440 error = VFS_STATFS(ump->um_uppervp->v_mount, &mstat); 441 if (error) 442 return (error); 443 444 /* 445 * The FS type etc is copy from upper vfs. 446 * (write able vfs have priority) 447 */ 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 453 if (mstat.f_bsize != lbsize) 454 sbp->f_blocks = ((off_t)sbp->f_blocks * lbsize) / mstat.f_bsize; 455 456 sbp->f_blocks += mstat.f_blocks; 457 sbp->f_bfree = mstat.f_bfree; 458 sbp->f_bavail = mstat.f_bavail; 459 sbp->f_files += mstat.f_files; 460 sbp->f_ffree = mstat.f_ffree; 461 return (0); 462 } 463 464 static int 465 unionfs_sync(struct mount *mp, int waitfor) 466 { 467 /* nothing to do */ 468 return (0); 469 } 470 471 static int 472 unionfs_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp) 473 { 474 return (EOPNOTSUPP); 475 } 476 477 static int 478 unionfs_fhtovp(struct mount *mp, struct fid *fidp, struct vnode **vpp) 479 { 480 return (EOPNOTSUPP); 481 } 482 483 static int 484 unionfs_checkexp(struct mount *mp, struct sockaddr *nam, int *extflagsp, 485 struct ucred **credanonp, int *numsecflavors, int **secflavors) 486 { 487 return (EOPNOTSUPP); 488 } 489 490 static int 491 unionfs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp, 492 int namespace, const char *attrname) 493 { 494 struct unionfs_mount *ump; 495 struct unionfs_node *unp; 496 497 ump = MOUNTTOUNIONFSMOUNT(mp); 498 unp = VTOUNIONFS(filename_vp); 499 500 if (unp->un_uppervp != NULLVP) { 501 return (VFS_EXTATTRCTL(ump->um_uppervp->v_mount, cmd, 502 unp->un_uppervp, namespace, attrname)); 503 } else { 504 return (VFS_EXTATTRCTL(ump->um_lowervp->v_mount, cmd, 505 unp->un_lowervp, namespace, attrname)); 506 } 507 } 508 509 static struct vfsops unionfs_vfsops = { 510 .vfs_checkexp = unionfs_checkexp, 511 .vfs_extattrctl = unionfs_extattrctl, 512 .vfs_fhtovp = unionfs_fhtovp, 513 .vfs_init = unionfs_init, 514 .vfs_mount = unionfs_domount, 515 .vfs_quotactl = unionfs_quotactl, 516 .vfs_root = unionfs_root, 517 .vfs_statfs = unionfs_statfs, 518 .vfs_sync = unionfs_sync, 519 .vfs_uninit = unionfs_uninit, 520 .vfs_unmount = unionfs_unmount, 521 .vfs_vget = unionfs_vget, 522 }; 523 524 VFS_SET(unionfs_vfsops, unionfs, VFCF_LOOPBACK); 525