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/kernel.h> 43 #include <sys/lock.h> 44 #include <sys/malloc.h> 45 #include <sys/mount.h> 46 #include <sys/namei.h> 47 #include <sys/proc.h> 48 #include <sys/vnode.h> 49 #include <sys/stat.h> 50 51 #include <fs/unionfs/union.h> 52 53 static MALLOC_DEFINE(M_UNIONFSMNT, "UNIONFS mount", "UNIONFS mount structure"); 54 55 static vfs_fhtovp_t unionfs_fhtovp; 56 static vfs_checkexp_t unionfs_checkexp; 57 static vfs_mount_t unionfs_domount; 58 static vfs_quotactl_t unionfs_quotactl; 59 static vfs_root_t unionfs_root; 60 static vfs_sync_t unionfs_sync; 61 static vfs_statfs_t unionfs_statfs; 62 static vfs_unmount_t unionfs_unmount; 63 static vfs_vget_t unionfs_vget; 64 static vfs_extattrctl_t unionfs_extattrctl; 65 66 static struct vfsops unionfs_vfsops; 67 68 /* 69 * Exchange from userland file mode to vmode. 70 */ 71 static u_short 72 mode2vmode(mode_t mode) 73 { 74 u_short ret; 75 76 ret = 0; 77 78 /* other */ 79 if (mode & S_IXOTH) 80 ret |= VEXEC >> 6; 81 if (mode & S_IWOTH) 82 ret |= VWRITE >> 6; 83 if (mode & S_IROTH) 84 ret |= VREAD >> 6; 85 86 /* group */ 87 if (mode & S_IXGRP) 88 ret |= VEXEC >> 3; 89 if (mode & S_IWGRP) 90 ret |= VWRITE >> 3; 91 if (mode & S_IRGRP) 92 ret |= VREAD >> 3; 93 94 /* owner */ 95 if (mode & S_IXUSR) 96 ret |= VEXEC; 97 if (mode & S_IWUSR) 98 ret |= VWRITE; 99 if (mode & S_IRUSR) 100 ret |= VREAD; 101 102 return (ret); 103 } 104 105 /* 106 * Mount unionfs layer. 107 */ 108 static int 109 unionfs_domount(struct mount *mp, struct thread *td) 110 { 111 int error; 112 struct vnode *lowerrootvp; 113 struct vnode *upperrootvp; 114 struct unionfs_mount *ump; 115 char *target; 116 char *tmp; 117 char *ep; 118 int len; 119 size_t done; 120 int below; 121 uid_t uid; 122 gid_t gid; 123 u_short udir; 124 u_short ufile; 125 unionfs_copymode copymode; 126 struct componentname fakecn; 127 struct nameidata nd, *ndp; 128 struct vattr va; 129 130 UNIONFSDEBUG("unionfs_mount(mp = %p)\n", (void *)mp); 131 132 error = 0; 133 below = 0; 134 uid = 0; 135 gid = 0; 136 udir = 0; 137 ufile = 0; 138 copymode = UNIONFS_TRADITIONAL; /* default */ 139 ndp = &nd; 140 141 if (mp->mnt_flag & MNT_ROOTFS) { 142 vfs_mount_error(mp, "Cannot union mount root filesystem"); 143 return (EOPNOTSUPP); 144 } 145 146 /* 147 * Update is a no operation. 148 */ 149 if (mp->mnt_flag & MNT_UPDATE) { 150 vfs_mount_error(mp, "unionfs does not support mount update"); 151 return (EOPNOTSUPP); 152 } 153 154 /* 155 * Get argument 156 */ 157 error = vfs_getopt(mp->mnt_optnew, "target", (void **)&target, &len); 158 if (error) 159 error = vfs_getopt(mp->mnt_optnew, "from", (void **)&target, 160 &len); 161 if (error || target[len - 1] != '\0') { 162 vfs_mount_error(mp, "Invalid target"); 163 return (EINVAL); 164 } 165 if (vfs_getopt(mp->mnt_optnew, "below", NULL, NULL) == 0) 166 below = 1; 167 if (vfs_getopt(mp->mnt_optnew, "udir", (void **)&tmp, NULL) == 0) { 168 if (tmp != NULL) 169 udir = (mode_t)strtol(tmp, &ep, 8); 170 if (tmp == NULL || *ep) { 171 vfs_mount_error(mp, "Invalid udir"); 172 return (EINVAL); 173 } 174 udir = mode2vmode(udir); 175 } 176 if (vfs_getopt(mp->mnt_optnew, "ufile", (void **)&tmp, NULL) == 0) { 177 if (tmp != NULL) 178 ufile = (mode_t)strtol(tmp, &ep, 8); 179 if (tmp == NULL || *ep) { 180 vfs_mount_error(mp, "Invalid ufile"); 181 return (EINVAL); 182 } 183 ufile = mode2vmode(ufile); 184 } 185 /* check umask, uid and gid */ 186 if (udir == 0 && ufile != 0) 187 udir = ufile; 188 if (ufile == 0 && udir != 0) 189 ufile = udir; 190 191 vn_lock(mp->mnt_vnodecovered, LK_SHARED | LK_RETRY, td); 192 error = VOP_GETATTR(mp->mnt_vnodecovered, &va, mp->mnt_cred, td); 193 if (!error) { 194 if (udir == 0) 195 udir = va.va_mode; 196 if (ufile == 0) 197 ufile = va.va_mode; 198 uid = va.va_uid; 199 gid = va.va_gid; 200 } 201 VOP_UNLOCK(mp->mnt_vnodecovered, 0, td); 202 if (error) 203 return (error); 204 205 if (mp->mnt_cred->cr_ruid == 0) { /* root only */ 206 if (vfs_getopt(mp->mnt_optnew, "uid", (void **)&tmp, 207 NULL) == 0) { 208 if (tmp != NULL) 209 uid = (uid_t)strtol(tmp, &ep, 10); 210 if (tmp == NULL || *ep) { 211 vfs_mount_error(mp, "Invalid uid"); 212 return (EINVAL); 213 } 214 } 215 if (vfs_getopt(mp->mnt_optnew, "gid", (void **)&tmp, 216 NULL) == 0) { 217 if (tmp != NULL) 218 gid = (gid_t)strtol(tmp, &ep, 10); 219 if (tmp == NULL || *ep) { 220 vfs_mount_error(mp, "Invalid gid"); 221 return (EINVAL); 222 } 223 } 224 if (vfs_getopt(mp->mnt_optnew, "copymode", (void **)&tmp, 225 NULL) == 0) { 226 if (tmp == NULL) { 227 vfs_mount_error(mp, "Invalid copymode"); 228 return (EINVAL); 229 } else if (strcasecmp(tmp, "traditional") == 0) 230 copymode = UNIONFS_TRADITIONAL; 231 else if (strcasecmp(tmp, "transparent") == 0) 232 copymode = UNIONFS_TRANSPARENT; 233 else if (strcasecmp(tmp, "masquerade") == 0) 234 copymode = UNIONFS_MASQUERADE; 235 else { 236 vfs_mount_error(mp, "Invalid copymode"); 237 return (EINVAL); 238 } 239 } 240 } 241 /* If copymode is UNIONFS_TRADITIONAL, uid/gid is mounted user. */ 242 if (copymode == UNIONFS_TRADITIONAL) { 243 uid = mp->mnt_cred->cr_ruid; 244 gid = mp->mnt_cred->cr_rgid; 245 } 246 247 UNIONFSDEBUG("unionfs_mount: uid=%d, gid=%d\n", uid, gid); 248 UNIONFSDEBUG("unionfs_mount: udir=0%03o, ufile=0%03o\n", udir, ufile); 249 UNIONFSDEBUG("unionfs_mount: copymode=%d\n", copymode); 250 251 /* 252 * Find upper node 253 */ 254 NDINIT(ndp, LOOKUP, FOLLOW | WANTPARENT | LOCKLEAF, UIO_SYSSPACE, target, td); 255 if ((error = namei(ndp))) 256 return (error); 257 258 NDFREE(ndp, NDF_ONLY_PNBUF); 259 260 /* get root vnodes */ 261 lowerrootvp = mp->mnt_vnodecovered; 262 upperrootvp = ndp->ni_vp; 263 264 vrele(ndp->ni_dvp); 265 ndp->ni_dvp = NULLVP; 266 267 /* create unionfs_mount */ 268 ump = (struct unionfs_mount *)malloc(sizeof(struct unionfs_mount), 269 M_UNIONFSMNT, M_WAITOK | M_ZERO); 270 271 /* 272 * Save reference 273 */ 274 if (below) { 275 VOP_UNLOCK(upperrootvp, 0, td); 276 vn_lock(lowerrootvp, LK_EXCLUSIVE | LK_RETRY, td); 277 ump->um_lowervp = upperrootvp; 278 ump->um_uppervp = lowerrootvp; 279 } else { 280 ump->um_lowervp = lowerrootvp; 281 ump->um_uppervp = upperrootvp; 282 } 283 ump->um_rootvp = NULLVP; 284 ump->um_uid = uid; 285 ump->um_gid = gid; 286 ump->um_udir = udir; 287 ump->um_ufile = ufile; 288 ump->um_copymode = copymode; 289 290 mp->mnt_data = (qaddr_t)ump; 291 292 /* 293 * Copy upper layer's RDONLY flag. 294 */ 295 mp->mnt_flag |= ump->um_uppervp->v_mount->mnt_flag & MNT_RDONLY; 296 297 /* 298 * Check whiteout 299 */ 300 if ((mp->mnt_flag & MNT_RDONLY) == 0) { 301 memset(&fakecn, 0, sizeof(fakecn)); 302 fakecn.cn_nameiop = LOOKUP; 303 fakecn.cn_thread = td; 304 error = VOP_WHITEOUT(ump->um_uppervp, &fakecn, LOOKUP); 305 if (error) { 306 if (below) { 307 VOP_UNLOCK(ump->um_uppervp, 0, td); 308 vrele(upperrootvp); 309 } else 310 vput(ump->um_uppervp); 311 free(ump, M_UNIONFSMNT); 312 mp->mnt_data = NULL; 313 return (error); 314 } 315 } 316 317 /* 318 * Unlock the node 319 */ 320 VOP_UNLOCK(ump->um_uppervp, 0, td); 321 322 /* 323 * Get the unionfs root vnode. 324 */ 325 error = unionfs_nodeget(mp, ump->um_uppervp, ump->um_lowervp, 326 NULLVP, &(ump->um_rootvp), NULL, td); 327 if (error) { 328 vrele(upperrootvp); 329 free(ump, M_UNIONFSMNT); 330 mp->mnt_data = NULL; 331 return (error); 332 } 333 334 /* 335 * Check mnt_flag 336 */ 337 if ((ump->um_lowervp->v_mount->mnt_flag & MNT_LOCAL) && 338 (ump->um_uppervp->v_mount->mnt_flag & MNT_LOCAL)) 339 mp->mnt_flag |= MNT_LOCAL; 340 341 /* 342 * Get new fsid 343 */ 344 vfs_getnewfsid(mp); 345 346 len = MNAMELEN - 1; 347 tmp = mp->mnt_stat.f_mntfromname; 348 copystr((below ? "<below>:" : "<above>:"), tmp, len, &done); 349 len -= done - 1; 350 tmp += done - 1; 351 copystr(target, tmp, len, NULL); 352 353 UNIONFSDEBUG("unionfs_mount: from %s, on %s\n", 354 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname); 355 356 return (0); 357 } 358 359 /* 360 * Free reference to unionfs layer 361 */ 362 static int 363 unionfs_unmount(struct mount *mp, int mntflags, struct thread *td) 364 { 365 struct unionfs_mount *ump; 366 int error; 367 int num; 368 int freeing; 369 int flags; 370 371 UNIONFSDEBUG("unionfs_unmount: mp = %p\n", (void *)mp); 372 373 ump = MOUNTTOUNIONFSMOUNT(mp); 374 flags = 0; 375 376 if (mntflags & MNT_FORCE) 377 flags |= FORCECLOSE; 378 379 /* vflush (no need to call vrele) */ 380 for (freeing = 0; (error = vflush(mp, 1, flags, td)) != 0;) { 381 num = mp->mnt_nvnodelistsize; 382 if (num == freeing) 383 break; 384 freeing = num; 385 } 386 387 if (error) 388 return (error); 389 390 free(ump, M_UNIONFSMNT); 391 mp->mnt_data = 0; 392 393 return (0); 394 } 395 396 static int 397 unionfs_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td) 398 { 399 struct unionfs_mount *ump; 400 struct vnode *vp; 401 402 ump = MOUNTTOUNIONFSMOUNT(mp); 403 vp = ump->um_rootvp; 404 405 UNIONFSDEBUG("unionfs_root: rootvp=%p locked=%x\n", 406 vp, VOP_ISLOCKED(vp, td)); 407 408 vref(vp); 409 if (flags & LK_TYPE_MASK) 410 vn_lock(vp, flags, td); 411 412 *vpp = vp; 413 414 return (0); 415 } 416 417 static int 418 unionfs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg, 419 struct thread *td) 420 { 421 struct unionfs_mount *ump; 422 423 ump = MOUNTTOUNIONFSMOUNT(mp); 424 425 /* 426 * Writing is always performed to upper vnode. 427 */ 428 return (VFS_QUOTACTL(ump->um_uppervp->v_mount, cmd, uid, arg, td)); 429 } 430 431 static int 432 unionfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td) 433 { 434 struct unionfs_mount *ump; 435 int error; 436 struct statfs mstat; 437 uint64_t lbsize; 438 439 ump = MOUNTTOUNIONFSMOUNT(mp); 440 441 UNIONFSDEBUG("unionfs_statfs(mp = %p, lvp = %p, uvp = %p)\n", 442 (void *)mp, (void *)ump->um_lowervp, (void *)ump->um_uppervp); 443 444 bzero(&mstat, sizeof(mstat)); 445 446 error = VFS_STATFS(ump->um_lowervp->v_mount, &mstat, td); 447 if (error) 448 return (error); 449 450 /* now copy across the "interesting" information and fake the rest */ 451 sbp->f_blocks = mstat.f_blocks; 452 sbp->f_files = mstat.f_files; 453 454 lbsize = mstat.f_bsize; 455 456 error = VFS_STATFS(ump->um_uppervp->v_mount, &mstat, td); 457 if (error) 458 return (error); 459 460 /* 461 * The FS type etc is copy from upper vfs. 462 * (write able vfs have priority) 463 */ 464 sbp->f_type = mstat.f_type; 465 sbp->f_flags = mstat.f_flags; 466 sbp->f_bsize = mstat.f_bsize; 467 sbp->f_iosize = mstat.f_iosize; 468 469 if (mstat.f_bsize != lbsize) 470 sbp->f_blocks = ((off_t)sbp->f_blocks * lbsize) / mstat.f_bsize; 471 472 sbp->f_blocks += mstat.f_blocks; 473 sbp->f_bfree = mstat.f_bfree; 474 sbp->f_bavail = mstat.f_bavail; 475 sbp->f_files += mstat.f_files; 476 sbp->f_ffree = mstat.f_ffree; 477 return (0); 478 } 479 480 static int 481 unionfs_sync(struct mount *mp, int waitfor, struct thread *td) 482 { 483 /* nothing to do */ 484 return (0); 485 } 486 487 static int 488 unionfs_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp) 489 { 490 return (EOPNOTSUPP); 491 } 492 493 static int 494 unionfs_fhtovp(struct mount *mp, struct fid *fidp, struct vnode **vpp) 495 { 496 return (EOPNOTSUPP); 497 } 498 499 static int 500 unionfs_checkexp(struct mount *mp, struct sockaddr *nam, int *extflagsp, 501 struct ucred **credanonp) 502 { 503 return (EOPNOTSUPP); 504 } 505 506 static int 507 unionfs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp, 508 int namespace, const char *attrname, struct thread *td) 509 { 510 struct unionfs_mount *ump; 511 struct unionfs_node *unp; 512 513 ump = MOUNTTOUNIONFSMOUNT(mp); 514 unp = VTOUNIONFS(filename_vp); 515 516 if (unp->un_uppervp != NULLVP) { 517 return (VFS_EXTATTRCTL(ump->um_uppervp->v_mount, cmd, 518 unp->un_uppervp, namespace, attrname, td)); 519 } else { 520 return (VFS_EXTATTRCTL(ump->um_lowervp->v_mount, cmd, 521 unp->un_lowervp, namespace, attrname, td)); 522 } 523 } 524 525 static struct vfsops unionfs_vfsops = { 526 .vfs_checkexp = unionfs_checkexp, 527 .vfs_extattrctl = unionfs_extattrctl, 528 .vfs_fhtovp = unionfs_fhtovp, 529 .vfs_init = unionfs_init, 530 .vfs_mount = unionfs_domount, 531 .vfs_quotactl = unionfs_quotactl, 532 .vfs_root = unionfs_root, 533 .vfs_statfs = unionfs_statfs, 534 .vfs_sync = unionfs_sync, 535 .vfs_uninit = unionfs_uninit, 536 .vfs_unmount = unionfs_unmount, 537 .vfs_vget = unionfs_vget, 538 }; 539 540 VFS_SET(unionfs_vfsops, unionfs, VFCF_LOOPBACK); 541