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