1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1994 Jan-Simon Pendry 5 * Copyright (c) 1994 6 * The Regents of the University of California. All rights reserved. 7 * Copyright (c) 2005, 2006, 2012 Masanori Ozawa <ozawa@ongs.co.jp>, ONGS Inc. 8 * Copyright (c) 2006, 2012 Daichi Goto <daichi@freebsd.org> 9 * 10 * This code is derived from software contributed to Berkeley by 11 * Jan-Simon Pendry. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. 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_subr.c 8.20 (Berkeley) 5/20/95 38 * $FreeBSD$ 39 */ 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/kernel.h> 44 #include <sys/ktr.h> 45 #include <sys/lock.h> 46 #include <sys/mutex.h> 47 #include <sys/malloc.h> 48 #include <sys/mount.h> 49 #include <sys/namei.h> 50 #include <sys/proc.h> 51 #include <sys/vnode.h> 52 #include <sys/dirent.h> 53 #include <sys/fcntl.h> 54 #include <sys/filedesc.h> 55 #include <sys/stat.h> 56 #include <sys/sysctl.h> 57 #include <sys/taskqueue.h> 58 #include <sys/resourcevar.h> 59 60 #include <security/mac/mac_framework.h> 61 62 #include <vm/uma.h> 63 64 #include <fs/unionfs/union.h> 65 66 #define NUNIONFSNODECACHE 16 67 68 static MALLOC_DEFINE(M_UNIONFSHASH, "UNIONFS hash", "UNIONFS hash table"); 69 MALLOC_DEFINE(M_UNIONFSNODE, "UNIONFS node", "UNIONFS vnode private part"); 70 MALLOC_DEFINE(M_UNIONFSPATH, "UNIONFS path", "UNIONFS path private part"); 71 72 static struct task unionfs_deferred_rele_task; 73 static struct mtx unionfs_deferred_rele_lock; 74 static STAILQ_HEAD(, unionfs_node) unionfs_deferred_rele_list = 75 STAILQ_HEAD_INITIALIZER(unionfs_deferred_rele_list); 76 static TASKQUEUE_DEFINE_THREAD(unionfs_rele); 77 78 unsigned int unionfs_ndeferred = 0; 79 SYSCTL_UINT(_vfs, OID_AUTO, unionfs_ndeferred, CTLFLAG_RD, 80 &unionfs_ndeferred, 0, "unionfs deferred vnode release"); 81 82 static void unionfs_deferred_rele(void *, int); 83 84 /* 85 * Initialize 86 */ 87 int 88 unionfs_init(struct vfsconf *vfsp) 89 { 90 UNIONFSDEBUG("unionfs_init\n"); /* printed during system boot */ 91 TASK_INIT(&unionfs_deferred_rele_task, 0, unionfs_deferred_rele, NULL); 92 mtx_init(&unionfs_deferred_rele_lock, "uniondefr", NULL, MTX_DEF); 93 return (0); 94 } 95 96 /* 97 * Uninitialize 98 */ 99 int 100 unionfs_uninit(struct vfsconf *vfsp) 101 { 102 taskqueue_quiesce(taskqueue_unionfs_rele); 103 taskqueue_free(taskqueue_unionfs_rele); 104 mtx_destroy(&unionfs_deferred_rele_lock); 105 return (0); 106 } 107 108 static void 109 unionfs_deferred_rele(void *arg __unused, int pending __unused) 110 { 111 STAILQ_HEAD(, unionfs_node) local_rele_list; 112 struct unionfs_node *unp, *tunp; 113 unsigned int ndeferred; 114 115 ndeferred = 0; 116 STAILQ_INIT(&local_rele_list); 117 mtx_lock(&unionfs_deferred_rele_lock); 118 STAILQ_CONCAT(&local_rele_list, &unionfs_deferred_rele_list); 119 mtx_unlock(&unionfs_deferred_rele_lock); 120 STAILQ_FOREACH_SAFE(unp, &local_rele_list, un_rele, tunp) { 121 ++ndeferred; 122 MPASS(unp->un_dvp != NULL); 123 vrele(unp->un_dvp); 124 free(unp, M_UNIONFSNODE); 125 } 126 127 /* We expect this function to be single-threaded, thus no atomic */ 128 unionfs_ndeferred += ndeferred; 129 } 130 131 static struct unionfs_node_hashhead * 132 unionfs_get_hashhead(struct vnode *dvp, char *path) 133 { 134 int count; 135 char hash; 136 struct unionfs_node *unp; 137 138 hash = 0; 139 unp = VTOUNIONFS(dvp); 140 if (path != NULL) { 141 for (count = 0; path[count]; count++) 142 hash += path[count]; 143 } 144 145 return (&(unp->un_hashtbl[hash & (unp->un_hashmask)])); 146 } 147 148 /* 149 * Get the cached vnode. 150 */ 151 static struct vnode * 152 unionfs_get_cached_vnode(struct vnode *uvp, struct vnode *lvp, 153 struct vnode *dvp, char *path) 154 { 155 struct unionfs_node_hashhead *hd; 156 struct unionfs_node *unp; 157 struct vnode *vp; 158 159 KASSERT((uvp == NULLVP || uvp->v_type == VDIR), 160 ("unionfs_get_cached_vnode: v_type != VDIR")); 161 KASSERT((lvp == NULLVP || lvp->v_type == VDIR), 162 ("unionfs_get_cached_vnode: v_type != VDIR")); 163 164 VI_LOCK(dvp); 165 hd = unionfs_get_hashhead(dvp, path); 166 LIST_FOREACH(unp, hd, un_hash) { 167 if (!strcmp(unp->un_path, path)) { 168 vp = UNIONFSTOV(unp); 169 VI_LOCK_FLAGS(vp, MTX_DUPOK); 170 VI_UNLOCK(dvp); 171 vp->v_iflag &= ~VI_OWEINACT; 172 if (VN_IS_DOOMED(vp) || 173 ((vp->v_iflag & VI_DOINGINACT) != 0)) { 174 VI_UNLOCK(vp); 175 vp = NULLVP; 176 } else 177 VI_UNLOCK(vp); 178 return (vp); 179 } 180 } 181 VI_UNLOCK(dvp); 182 183 return (NULLVP); 184 } 185 186 /* 187 * Add the new vnode into cache. 188 */ 189 static struct vnode * 190 unionfs_ins_cached_vnode(struct unionfs_node *uncp, 191 struct vnode *dvp, char *path) 192 { 193 struct unionfs_node_hashhead *hd; 194 struct unionfs_node *unp; 195 struct vnode *vp; 196 197 KASSERT((uncp->un_uppervp==NULLVP || uncp->un_uppervp->v_type==VDIR), 198 ("unionfs_ins_cached_vnode: v_type != VDIR")); 199 KASSERT((uncp->un_lowervp==NULLVP || uncp->un_lowervp->v_type==VDIR), 200 ("unionfs_ins_cached_vnode: v_type != VDIR")); 201 202 VI_LOCK(dvp); 203 hd = unionfs_get_hashhead(dvp, path); 204 LIST_FOREACH(unp, hd, un_hash) { 205 if (!strcmp(unp->un_path, path)) { 206 vp = UNIONFSTOV(unp); 207 VI_LOCK_FLAGS(vp, MTX_DUPOK); 208 vp->v_iflag &= ~VI_OWEINACT; 209 if (VN_IS_DOOMED(vp) || 210 ((vp->v_iflag & VI_DOINGINACT) != 0)) { 211 LIST_INSERT_HEAD(hd, uncp, un_hash); 212 VI_UNLOCK(vp); 213 vp = NULLVP; 214 } else 215 VI_UNLOCK(vp); 216 VI_UNLOCK(dvp); 217 return (vp); 218 } 219 } 220 221 LIST_INSERT_HEAD(hd, uncp, un_hash); 222 VI_UNLOCK(dvp); 223 224 return (NULLVP); 225 } 226 227 /* 228 * Remove the vnode. 229 */ 230 static void 231 unionfs_rem_cached_vnode(struct unionfs_node *unp, struct vnode *dvp) 232 { 233 KASSERT((unp != NULL), ("unionfs_rem_cached_vnode: null node")); 234 KASSERT((dvp != NULLVP), 235 ("unionfs_rem_cached_vnode: null parent vnode")); 236 KASSERT((unp->un_hash.le_prev != NULL), 237 ("unionfs_rem_cached_vnode: null hash")); 238 239 VI_LOCK(dvp); 240 LIST_REMOVE(unp, un_hash); 241 unp->un_hash.le_next = NULL; 242 unp->un_hash.le_prev = NULL; 243 VI_UNLOCK(dvp); 244 } 245 246 /* 247 * Make a new or get existing unionfs node. 248 * 249 * uppervp and lowervp should be unlocked. Because if new unionfs vnode is 250 * locked, uppervp or lowervp is locked too. In order to prevent dead lock, 251 * you should not lock plurality simultaneously. 252 */ 253 int 254 unionfs_nodeget(struct mount *mp, struct vnode *uppervp, 255 struct vnode *lowervp, struct vnode *dvp, 256 struct vnode **vpp, struct componentname *cnp, 257 struct thread *td) 258 { 259 struct unionfs_mount *ump; 260 struct unionfs_node *unp; 261 struct vnode *vp; 262 int error; 263 int lkflags; 264 enum vtype vt; 265 char *path; 266 267 ump = MOUNTTOUNIONFSMOUNT(mp); 268 lkflags = (cnp ? cnp->cn_lkflags : 0); 269 path = (cnp ? cnp->cn_nameptr : NULL); 270 *vpp = NULLVP; 271 272 if (uppervp == NULLVP && lowervp == NULLVP) 273 panic("unionfs_nodeget: upper and lower is null"); 274 275 vt = (uppervp != NULLVP ? uppervp->v_type : lowervp->v_type); 276 277 /* If it has no ISLASTCN flag, path check is skipped. */ 278 if (cnp && !(cnp->cn_flags & ISLASTCN)) 279 path = NULL; 280 281 /* check the cache */ 282 if (path != NULL && dvp != NULLVP && vt == VDIR) { 283 vp = unionfs_get_cached_vnode(uppervp, lowervp, dvp, path); 284 if (vp != NULLVP) { 285 vref(vp); 286 *vpp = vp; 287 goto unionfs_nodeget_out; 288 } 289 } 290 291 if ((uppervp == NULLVP || ump->um_uppervp != uppervp) || 292 (lowervp == NULLVP || ump->um_lowervp != lowervp)) { 293 /* dvp will be NULLVP only in case of root vnode. */ 294 if (dvp == NULLVP) 295 return (EINVAL); 296 } 297 unp = malloc(sizeof(struct unionfs_node), 298 M_UNIONFSNODE, M_WAITOK | M_ZERO); 299 300 error = getnewvnode("unionfs", mp, &unionfs_vnodeops, &vp); 301 if (error != 0) { 302 free(unp, M_UNIONFSNODE); 303 return (error); 304 } 305 error = insmntque(vp, mp); /* XXX: Too early for mpsafe fs */ 306 if (error != 0) { 307 free(unp, M_UNIONFSNODE); 308 return (error); 309 } 310 if (dvp != NULLVP) 311 vref(dvp); 312 if (uppervp != NULLVP) 313 vref(uppervp); 314 if (lowervp != NULLVP) 315 vref(lowervp); 316 317 if (vt == VDIR) 318 unp->un_hashtbl = hashinit(NUNIONFSNODECACHE, M_UNIONFSHASH, 319 &(unp->un_hashmask)); 320 321 unp->un_vnode = vp; 322 unp->un_uppervp = uppervp; 323 unp->un_lowervp = lowervp; 324 unp->un_dvp = dvp; 325 if (uppervp != NULLVP) 326 vp->v_vnlock = uppervp->v_vnlock; 327 else 328 vp->v_vnlock = lowervp->v_vnlock; 329 330 if (path != NULL) { 331 unp->un_path = (char *) 332 malloc(cnp->cn_namelen +1, M_UNIONFSPATH, M_WAITOK|M_ZERO); 333 bcopy(cnp->cn_nameptr, unp->un_path, cnp->cn_namelen); 334 unp->un_path[cnp->cn_namelen] = '\0'; 335 } 336 vp->v_type = vt; 337 vp->v_data = unp; 338 339 if ((uppervp != NULLVP && ump->um_uppervp == uppervp) && 340 (lowervp != NULLVP && ump->um_lowervp == lowervp)) 341 vp->v_vflag |= VV_ROOT; 342 343 if (path != NULL && dvp != NULLVP && vt == VDIR) 344 *vpp = unionfs_ins_cached_vnode(unp, dvp, path); 345 if ((*vpp) != NULLVP) { 346 if (dvp != NULLVP) 347 vrele(dvp); 348 if (uppervp != NULLVP) 349 vrele(uppervp); 350 if (lowervp != NULLVP) 351 vrele(lowervp); 352 353 unp->un_uppervp = NULLVP; 354 unp->un_lowervp = NULLVP; 355 unp->un_dvp = NULLVP; 356 vrele(vp); 357 vp = *vpp; 358 vref(vp); 359 } else 360 *vpp = vp; 361 362 unionfs_nodeget_out: 363 if (lkflags & LK_TYPE_MASK) 364 vn_lock(vp, lkflags | LK_RETRY); 365 366 return (0); 367 } 368 369 /* 370 * Clean up the unionfs node. 371 */ 372 void 373 unionfs_noderem(struct vnode *vp, struct thread *td) 374 { 375 int count; 376 struct unionfs_node *unp, *unp_t1, *unp_t2; 377 struct unionfs_node_hashhead *hd; 378 struct unionfs_node_status *unsp, *unsp_tmp; 379 struct vnode *lvp; 380 struct vnode *uvp; 381 struct vnode *dvp; 382 383 /* 384 * Use the interlock to protect the clearing of v_data to 385 * prevent faults in unionfs_lock(). 386 */ 387 VI_LOCK(vp); 388 unp = VTOUNIONFS(vp); 389 lvp = unp->un_lowervp; 390 uvp = unp->un_uppervp; 391 dvp = unp->un_dvp; 392 unp->un_lowervp = unp->un_uppervp = NULLVP; 393 vp->v_vnlock = &(vp->v_lock); 394 vp->v_data = NULL; 395 vp->v_object = NULL; 396 if (vp->v_writecount > 0) { 397 if (uvp != NULL) 398 VOP_ADD_WRITECOUNT(uvp, -vp->v_writecount); 399 else if (lvp != NULL) 400 VOP_ADD_WRITECOUNT(lvp, -vp->v_writecount); 401 } else if (vp->v_writecount < 0) 402 vp->v_writecount = 0; 403 VI_UNLOCK(vp); 404 405 if (lvp != NULLVP) 406 VOP_UNLOCK(lvp); 407 if (uvp != NULLVP) 408 VOP_UNLOCK(uvp); 409 410 if (dvp != NULLVP && unp->un_hash.le_prev != NULL) 411 unionfs_rem_cached_vnode(unp, dvp); 412 413 if (lockmgr(vp->v_vnlock, LK_EXCLUSIVE, VI_MTX(vp)) != 0) 414 panic("the lock for deletion is unacquirable."); 415 416 if (lvp != NULLVP) 417 vrele(lvp); 418 if (uvp != NULLVP) 419 vrele(uvp); 420 if (unp->un_path != NULL) { 421 free(unp->un_path, M_UNIONFSPATH); 422 unp->un_path = NULL; 423 } 424 425 if (unp->un_hashtbl != NULL) { 426 for (count = 0; count <= unp->un_hashmask; count++) { 427 hd = unp->un_hashtbl + count; 428 LIST_FOREACH_SAFE(unp_t1, hd, un_hash, unp_t2) { 429 LIST_REMOVE(unp_t1, un_hash); 430 unp_t1->un_hash.le_next = NULL; 431 unp_t1->un_hash.le_prev = NULL; 432 } 433 } 434 hashdestroy(unp->un_hashtbl, M_UNIONFSHASH, unp->un_hashmask); 435 } 436 437 LIST_FOREACH_SAFE(unsp, &(unp->un_unshead), uns_list, unsp_tmp) { 438 LIST_REMOVE(unsp, uns_list); 439 free(unsp, M_TEMP); 440 } 441 if (dvp != NULLVP) { 442 mtx_lock(&unionfs_deferred_rele_lock); 443 STAILQ_INSERT_TAIL(&unionfs_deferred_rele_list, unp, un_rele); 444 mtx_unlock(&unionfs_deferred_rele_lock); 445 taskqueue_enqueue(taskqueue_unionfs_rele, 446 &unionfs_deferred_rele_task); 447 } else 448 free(unp, M_UNIONFSNODE); 449 } 450 451 /* 452 * Get the unionfs node status. 453 * You need exclusive lock this vnode. 454 */ 455 void 456 unionfs_get_node_status(struct unionfs_node *unp, struct thread *td, 457 struct unionfs_node_status **unspp) 458 { 459 struct unionfs_node_status *unsp; 460 pid_t pid = td->td_proc->p_pid; 461 462 KASSERT(NULL != unspp, ("null pointer")); 463 ASSERT_VOP_ELOCKED(UNIONFSTOV(unp), "unionfs_get_node_status"); 464 465 LIST_FOREACH(unsp, &(unp->un_unshead), uns_list) { 466 if (unsp->uns_pid == pid) { 467 *unspp = unsp; 468 return; 469 } 470 } 471 472 /* create a new unionfs node status */ 473 unsp = malloc(sizeof(struct unionfs_node_status), 474 M_TEMP, M_WAITOK | M_ZERO); 475 476 unsp->uns_pid = pid; 477 LIST_INSERT_HEAD(&(unp->un_unshead), unsp, uns_list); 478 479 *unspp = unsp; 480 } 481 482 /* 483 * Remove the unionfs node status, if you can. 484 * You need exclusive lock this vnode. 485 */ 486 void 487 unionfs_tryrem_node_status(struct unionfs_node *unp, 488 struct unionfs_node_status *unsp) 489 { 490 KASSERT(NULL != unsp, ("null pointer")); 491 ASSERT_VOP_ELOCKED(UNIONFSTOV(unp), "unionfs_get_node_status"); 492 493 if (0 < unsp->uns_lower_opencnt || 0 < unsp->uns_upper_opencnt) 494 return; 495 496 LIST_REMOVE(unsp, uns_list); 497 free(unsp, M_TEMP); 498 } 499 500 /* 501 * Create upper node attr. 502 */ 503 void 504 unionfs_create_uppervattr_core(struct unionfs_mount *ump, 505 struct vattr *lva, 506 struct vattr *uva, 507 struct thread *td) 508 { 509 VATTR_NULL(uva); 510 uva->va_type = lva->va_type; 511 uva->va_atime = lva->va_atime; 512 uva->va_mtime = lva->va_mtime; 513 uva->va_ctime = lva->va_ctime; 514 515 switch (ump->um_copymode) { 516 case UNIONFS_TRANSPARENT: 517 uva->va_mode = lva->va_mode; 518 uva->va_uid = lva->va_uid; 519 uva->va_gid = lva->va_gid; 520 break; 521 case UNIONFS_MASQUERADE: 522 if (ump->um_uid == lva->va_uid) { 523 uva->va_mode = lva->va_mode & 077077; 524 uva->va_mode |= (lva->va_type == VDIR ? ump->um_udir : ump->um_ufile) & 0700; 525 uva->va_uid = lva->va_uid; 526 uva->va_gid = lva->va_gid; 527 } else { 528 uva->va_mode = (lva->va_type == VDIR ? ump->um_udir : ump->um_ufile); 529 uva->va_uid = ump->um_uid; 530 uva->va_gid = ump->um_gid; 531 } 532 break; 533 default: /* UNIONFS_TRADITIONAL */ 534 uva->va_mode = 0777 & ~td->td_proc->p_pd->pd_cmask; 535 uva->va_uid = ump->um_uid; 536 uva->va_gid = ump->um_gid; 537 break; 538 } 539 } 540 541 /* 542 * Create upper node attr. 543 */ 544 int 545 unionfs_create_uppervattr(struct unionfs_mount *ump, 546 struct vnode *lvp, 547 struct vattr *uva, 548 struct ucred *cred, 549 struct thread *td) 550 { 551 int error; 552 struct vattr lva; 553 554 if ((error = VOP_GETATTR(lvp, &lva, cred))) 555 return (error); 556 557 unionfs_create_uppervattr_core(ump, &lva, uva, td); 558 559 return (error); 560 } 561 562 /* 563 * relookup 564 * 565 * dvp should be locked on entry and will be locked on return. 566 * 567 * If an error is returned, *vpp will be invalid, otherwise it will hold a 568 * locked, referenced vnode. If *vpp == dvp then remember that only one 569 * LK_EXCLUSIVE lock is held. 570 */ 571 int 572 unionfs_relookup(struct vnode *dvp, struct vnode **vpp, 573 struct componentname *cnp, struct componentname *cn, 574 struct thread *td, char *path, int pathlen, u_long nameiop) 575 { 576 int error; 577 578 cn->cn_namelen = pathlen; 579 cn->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK); 580 bcopy(path, cn->cn_pnbuf, pathlen); 581 cn->cn_pnbuf[pathlen] = '\0'; 582 583 cn->cn_nameiop = nameiop; 584 cn->cn_flags = (LOCKPARENT | LOCKLEAF | HASBUF | SAVENAME | ISLASTCN); 585 cn->cn_lkflags = LK_EXCLUSIVE; 586 cn->cn_thread = td; 587 cn->cn_cred = cnp->cn_cred; 588 589 cn->cn_nameptr = cn->cn_pnbuf; 590 591 if (nameiop == DELETE) 592 cn->cn_flags |= (cnp->cn_flags & (DOWHITEOUT | SAVESTART)); 593 else if (RENAME == nameiop) 594 cn->cn_flags |= (cnp->cn_flags & SAVESTART); 595 else if (nameiop == CREATE) 596 cn->cn_flags |= NOCACHE; 597 598 vref(dvp); 599 VOP_UNLOCK(dvp); 600 601 if ((error = relookup(dvp, vpp, cn))) { 602 uma_zfree(namei_zone, cn->cn_pnbuf); 603 cn->cn_flags &= ~HASBUF; 604 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY); 605 } else 606 vrele(dvp); 607 608 return (error); 609 } 610 611 /* 612 * relookup for CREATE namei operation. 613 * 614 * dvp is unionfs vnode. dvp should be locked. 615 * 616 * If it called 'unionfs_copyfile' function by unionfs_link etc, 617 * VOP_LOOKUP information is broken. 618 * So it need relookup in order to create link etc. 619 */ 620 int 621 unionfs_relookup_for_create(struct vnode *dvp, struct componentname *cnp, 622 struct thread *td) 623 { 624 int error; 625 struct vnode *udvp; 626 struct vnode *vp; 627 struct componentname cn; 628 629 udvp = UNIONFSVPTOUPPERVP(dvp); 630 vp = NULLVP; 631 632 error = unionfs_relookup(udvp, &vp, cnp, &cn, td, cnp->cn_nameptr, 633 strlen(cnp->cn_nameptr), CREATE); 634 if (error) 635 return (error); 636 637 if (vp != NULLVP) { 638 if (udvp == vp) 639 vrele(vp); 640 else 641 vput(vp); 642 643 error = EEXIST; 644 } 645 646 if (cn.cn_flags & HASBUF) { 647 uma_zfree(namei_zone, cn.cn_pnbuf); 648 cn.cn_flags &= ~HASBUF; 649 } 650 651 if (!error) { 652 cn.cn_flags |= (cnp->cn_flags & HASBUF); 653 cnp->cn_flags = cn.cn_flags; 654 } 655 656 return (error); 657 } 658 659 /* 660 * relookup for DELETE namei operation. 661 * 662 * dvp is unionfs vnode. dvp should be locked. 663 */ 664 int 665 unionfs_relookup_for_delete(struct vnode *dvp, struct componentname *cnp, 666 struct thread *td) 667 { 668 int error; 669 struct vnode *udvp; 670 struct vnode *vp; 671 struct componentname cn; 672 673 udvp = UNIONFSVPTOUPPERVP(dvp); 674 vp = NULLVP; 675 676 error = unionfs_relookup(udvp, &vp, cnp, &cn, td, cnp->cn_nameptr, 677 strlen(cnp->cn_nameptr), DELETE); 678 if (error) 679 return (error); 680 681 if (vp == NULLVP) 682 error = ENOENT; 683 else { 684 if (udvp == vp) 685 vrele(vp); 686 else 687 vput(vp); 688 } 689 690 if (cn.cn_flags & HASBUF) { 691 uma_zfree(namei_zone, cn.cn_pnbuf); 692 cn.cn_flags &= ~HASBUF; 693 } 694 695 if (!error) { 696 cn.cn_flags |= (cnp->cn_flags & HASBUF); 697 cnp->cn_flags = cn.cn_flags; 698 } 699 700 return (error); 701 } 702 703 /* 704 * relookup for RENAME namei operation. 705 * 706 * dvp is unionfs vnode. dvp should be locked. 707 */ 708 int 709 unionfs_relookup_for_rename(struct vnode *dvp, struct componentname *cnp, 710 struct thread *td) 711 { 712 int error; 713 struct vnode *udvp; 714 struct vnode *vp; 715 struct componentname cn; 716 717 udvp = UNIONFSVPTOUPPERVP(dvp); 718 vp = NULLVP; 719 720 error = unionfs_relookup(udvp, &vp, cnp, &cn, td, cnp->cn_nameptr, 721 strlen(cnp->cn_nameptr), RENAME); 722 if (error) 723 return (error); 724 725 if (vp != NULLVP) { 726 if (udvp == vp) 727 vrele(vp); 728 else 729 vput(vp); 730 } 731 732 if (cn.cn_flags & HASBUF) { 733 uma_zfree(namei_zone, cn.cn_pnbuf); 734 cn.cn_flags &= ~HASBUF; 735 } 736 737 if (!error) { 738 cn.cn_flags |= (cnp->cn_flags & HASBUF); 739 cnp->cn_flags = cn.cn_flags; 740 } 741 742 return (error); 743 744 } 745 746 /* 747 * Update the unionfs_node. 748 * 749 * uvp is new locked upper vnode. unionfs vnode's lock will be exchanged to the 750 * uvp's lock and lower's lock will be unlocked. 751 */ 752 static void 753 unionfs_node_update(struct unionfs_node *unp, struct vnode *uvp, 754 struct thread *td) 755 { 756 unsigned count, lockrec; 757 struct vnode *vp; 758 struct vnode *lvp; 759 struct vnode *dvp; 760 761 vp = UNIONFSTOV(unp); 762 lvp = unp->un_lowervp; 763 ASSERT_VOP_ELOCKED(lvp, "unionfs_node_update"); 764 dvp = unp->un_dvp; 765 766 /* 767 * lock update 768 */ 769 VI_LOCK(vp); 770 unp->un_uppervp = uvp; 771 vp->v_vnlock = uvp->v_vnlock; 772 VI_UNLOCK(vp); 773 lockrec = lvp->v_vnlock->lk_recurse; 774 for (count = 0; count < lockrec; count++) 775 vn_lock(uvp, LK_EXCLUSIVE | LK_CANRECURSE | LK_RETRY); 776 777 /* 778 * cache update 779 */ 780 if (unp->un_path != NULL && dvp != NULLVP && vp->v_type == VDIR) { 781 static struct unionfs_node_hashhead *hd; 782 783 VI_LOCK(dvp); 784 hd = unionfs_get_hashhead(dvp, unp->un_path); 785 LIST_REMOVE(unp, un_hash); 786 LIST_INSERT_HEAD(hd, unp, un_hash); 787 VI_UNLOCK(dvp); 788 } 789 } 790 791 /* 792 * Create a new shadow dir. 793 * 794 * udvp should be locked on entry and will be locked on return. 795 * 796 * If no error returned, unp will be updated. 797 */ 798 int 799 unionfs_mkshadowdir(struct unionfs_mount *ump, struct vnode *udvp, 800 struct unionfs_node *unp, struct componentname *cnp, 801 struct thread *td) 802 { 803 int error; 804 struct vnode *lvp; 805 struct vnode *uvp; 806 struct vattr va; 807 struct vattr lva; 808 struct nameidata nd; 809 struct mount *mp; 810 struct ucred *cred; 811 struct ucred *credbk; 812 struct uidinfo *rootinfo; 813 814 if (unp->un_uppervp != NULLVP) 815 return (EEXIST); 816 817 lvp = unp->un_lowervp; 818 uvp = NULLVP; 819 credbk = cnp->cn_cred; 820 821 /* Authority change to root */ 822 rootinfo = uifind((uid_t)0); 823 cred = crdup(cnp->cn_cred); 824 /* 825 * The calls to chgproccnt() are needed to compensate for change_ruid() 826 * calling chgproccnt(). 827 */ 828 chgproccnt(cred->cr_ruidinfo, 1, 0); 829 change_euid(cred, rootinfo); 830 change_ruid(cred, rootinfo); 831 change_svuid(cred, (uid_t)0); 832 uifree(rootinfo); 833 cnp->cn_cred = cred; 834 835 memset(&nd.ni_cnd, 0, sizeof(struct componentname)); 836 NDPREINIT(&nd); 837 838 if ((error = VOP_GETATTR(lvp, &lva, cnp->cn_cred))) 839 goto unionfs_mkshadowdir_abort; 840 841 if ((error = unionfs_relookup(udvp, &uvp, cnp, &nd.ni_cnd, td, 842 cnp->cn_nameptr, cnp->cn_namelen, CREATE))) 843 goto unionfs_mkshadowdir_abort; 844 if (uvp != NULLVP) { 845 if (udvp == uvp) 846 vrele(uvp); 847 else 848 vput(uvp); 849 850 error = EEXIST; 851 goto unionfs_mkshadowdir_free_out; 852 } 853 854 if ((error = vn_start_write(udvp, &mp, V_WAIT | PCATCH))) 855 goto unionfs_mkshadowdir_free_out; 856 unionfs_create_uppervattr_core(ump, &lva, &va, td); 857 858 error = VOP_MKDIR(udvp, &uvp, &nd.ni_cnd, &va); 859 860 if (!error) { 861 unionfs_node_update(unp, uvp, td); 862 863 /* 864 * XXX The bug which cannot set uid/gid was corrected. 865 * Ignore errors. 866 */ 867 va.va_type = VNON; 868 VOP_SETATTR(uvp, &va, nd.ni_cnd.cn_cred); 869 } 870 vn_finished_write(mp); 871 872 unionfs_mkshadowdir_free_out: 873 if (nd.ni_cnd.cn_flags & HASBUF) { 874 uma_zfree(namei_zone, nd.ni_cnd.cn_pnbuf); 875 nd.ni_cnd.cn_flags &= ~HASBUF; 876 } 877 878 unionfs_mkshadowdir_abort: 879 cnp->cn_cred = credbk; 880 chgproccnt(cred->cr_ruidinfo, -1, 0); 881 crfree(cred); 882 883 return (error); 884 } 885 886 /* 887 * Create a new whiteout. 888 * 889 * dvp should be locked on entry and will be locked on return. 890 */ 891 int 892 unionfs_mkwhiteout(struct vnode *dvp, struct componentname *cnp, 893 struct thread *td, char *path) 894 { 895 int error; 896 struct vnode *wvp; 897 struct nameidata nd; 898 struct mount *mp; 899 900 if (path == NULL) 901 path = cnp->cn_nameptr; 902 903 wvp = NULLVP; 904 NDPREINIT(&nd); 905 if ((error = unionfs_relookup(dvp, &wvp, cnp, &nd.ni_cnd, td, path, 906 strlen(path), CREATE))) 907 return (error); 908 if (wvp != NULLVP) { 909 if (nd.ni_cnd.cn_flags & HASBUF) { 910 uma_zfree(namei_zone, nd.ni_cnd.cn_pnbuf); 911 nd.ni_cnd.cn_flags &= ~HASBUF; 912 } 913 if (dvp == wvp) 914 vrele(wvp); 915 else 916 vput(wvp); 917 918 return (EEXIST); 919 } 920 921 if ((error = vn_start_write(dvp, &mp, V_WAIT | PCATCH))) 922 goto unionfs_mkwhiteout_free_out; 923 error = VOP_WHITEOUT(dvp, &nd.ni_cnd, CREATE); 924 925 vn_finished_write(mp); 926 927 unionfs_mkwhiteout_free_out: 928 if (nd.ni_cnd.cn_flags & HASBUF) { 929 uma_zfree(namei_zone, nd.ni_cnd.cn_pnbuf); 930 nd.ni_cnd.cn_flags &= ~HASBUF; 931 } 932 933 return (error); 934 } 935 936 /* 937 * Create a new vnode for create a new shadow file. 938 * 939 * If an error is returned, *vpp will be invalid, otherwise it will hold a 940 * locked, referenced and opened vnode. 941 * 942 * unp is never updated. 943 */ 944 static int 945 unionfs_vn_create_on_upper(struct vnode **vpp, struct vnode *udvp, 946 struct unionfs_node *unp, struct vattr *uvap, 947 struct thread *td) 948 { 949 struct unionfs_mount *ump; 950 struct vnode *vp; 951 struct vnode *lvp; 952 struct ucred *cred; 953 struct vattr lva; 954 int fmode; 955 int error; 956 struct nameidata nd; 957 958 ump = MOUNTTOUNIONFSMOUNT(UNIONFSTOV(unp)->v_mount); 959 vp = NULLVP; 960 lvp = unp->un_lowervp; 961 cred = td->td_ucred; 962 fmode = FFLAGS(O_WRONLY | O_CREAT | O_TRUNC | O_EXCL); 963 error = 0; 964 965 if ((error = VOP_GETATTR(lvp, &lva, cred)) != 0) 966 return (error); 967 unionfs_create_uppervattr_core(ump, &lva, uvap, td); 968 969 if (unp->un_path == NULL) 970 panic("unionfs: un_path is null"); 971 972 nd.ni_cnd.cn_namelen = strlen(unp->un_path); 973 nd.ni_cnd.cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK); 974 bcopy(unp->un_path, nd.ni_cnd.cn_pnbuf, nd.ni_cnd.cn_namelen + 1); 975 nd.ni_cnd.cn_nameiop = CREATE; 976 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | HASBUF | SAVENAME | 977 ISLASTCN; 978 nd.ni_cnd.cn_lkflags = LK_EXCLUSIVE; 979 nd.ni_cnd.cn_thread = td; 980 nd.ni_cnd.cn_cred = cred; 981 nd.ni_cnd.cn_nameptr = nd.ni_cnd.cn_pnbuf; 982 NDPREINIT(&nd); 983 984 vref(udvp); 985 if ((error = relookup(udvp, &vp, &nd.ni_cnd)) != 0) 986 goto unionfs_vn_create_on_upper_free_out2; 987 vrele(udvp); 988 989 if (vp != NULLVP) { 990 if (vp == udvp) 991 vrele(vp); 992 else 993 vput(vp); 994 error = EEXIST; 995 goto unionfs_vn_create_on_upper_free_out1; 996 } 997 998 if ((error = VOP_CREATE(udvp, &vp, &nd.ni_cnd, uvap)) != 0) 999 goto unionfs_vn_create_on_upper_free_out1; 1000 1001 if ((error = VOP_OPEN(vp, fmode, cred, td, NULL)) != 0) { 1002 vput(vp); 1003 goto unionfs_vn_create_on_upper_free_out1; 1004 } 1005 error = VOP_ADD_WRITECOUNT(vp, 1); 1006 CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d", __func__, vp, 1007 vp->v_writecount); 1008 if (error == 0) { 1009 *vpp = vp; 1010 } else { 1011 VOP_CLOSE(vp, fmode, cred, td); 1012 } 1013 1014 unionfs_vn_create_on_upper_free_out1: 1015 VOP_UNLOCK(udvp); 1016 1017 unionfs_vn_create_on_upper_free_out2: 1018 if (nd.ni_cnd.cn_flags & HASBUF) { 1019 uma_zfree(namei_zone, nd.ni_cnd.cn_pnbuf); 1020 nd.ni_cnd.cn_flags &= ~HASBUF; 1021 } 1022 1023 return (error); 1024 } 1025 1026 /* 1027 * Copy from lvp to uvp. 1028 * 1029 * lvp and uvp should be locked and opened on entry and will be locked and 1030 * opened on return. 1031 */ 1032 static int 1033 unionfs_copyfile_core(struct vnode *lvp, struct vnode *uvp, 1034 struct ucred *cred, struct thread *td) 1035 { 1036 int error; 1037 off_t offset; 1038 int count; 1039 int bufoffset; 1040 char *buf; 1041 struct uio uio; 1042 struct iovec iov; 1043 1044 error = 0; 1045 memset(&uio, 0, sizeof(uio)); 1046 1047 uio.uio_td = td; 1048 uio.uio_segflg = UIO_SYSSPACE; 1049 uio.uio_offset = 0; 1050 1051 buf = malloc(MAXBSIZE, M_TEMP, M_WAITOK); 1052 1053 while (error == 0) { 1054 offset = uio.uio_offset; 1055 1056 uio.uio_iov = &iov; 1057 uio.uio_iovcnt = 1; 1058 iov.iov_base = buf; 1059 iov.iov_len = MAXBSIZE; 1060 uio.uio_resid = iov.iov_len; 1061 uio.uio_rw = UIO_READ; 1062 1063 if ((error = VOP_READ(lvp, &uio, 0, cred)) != 0) 1064 break; 1065 if ((count = MAXBSIZE - uio.uio_resid) == 0) 1066 break; 1067 1068 bufoffset = 0; 1069 while (bufoffset < count) { 1070 uio.uio_iov = &iov; 1071 uio.uio_iovcnt = 1; 1072 iov.iov_base = buf + bufoffset; 1073 iov.iov_len = count - bufoffset; 1074 uio.uio_offset = offset + bufoffset; 1075 uio.uio_resid = iov.iov_len; 1076 uio.uio_rw = UIO_WRITE; 1077 1078 if ((error = VOP_WRITE(uvp, &uio, 0, cred)) != 0) 1079 break; 1080 1081 bufoffset += (count - bufoffset) - uio.uio_resid; 1082 } 1083 1084 uio.uio_offset = offset + bufoffset; 1085 } 1086 1087 free(buf, M_TEMP); 1088 1089 return (error); 1090 } 1091 1092 /* 1093 * Copy file from lower to upper. 1094 * 1095 * If you need copy of the contents, set 1 to docopy. Otherwise, set 0 to 1096 * docopy. 1097 * 1098 * If no error returned, unp will be updated. 1099 */ 1100 int 1101 unionfs_copyfile(struct unionfs_node *unp, int docopy, struct ucred *cred, 1102 struct thread *td) 1103 { 1104 int error; 1105 struct mount *mp; 1106 struct vnode *udvp; 1107 struct vnode *lvp; 1108 struct vnode *uvp; 1109 struct vattr uva; 1110 1111 lvp = unp->un_lowervp; 1112 uvp = NULLVP; 1113 1114 if ((UNIONFSTOV(unp)->v_mount->mnt_flag & MNT_RDONLY)) 1115 return (EROFS); 1116 if (unp->un_dvp == NULLVP) 1117 return (EINVAL); 1118 if (unp->un_uppervp != NULLVP) 1119 return (EEXIST); 1120 udvp = VTOUNIONFS(unp->un_dvp)->un_uppervp; 1121 if (udvp == NULLVP) 1122 return (EROFS); 1123 if ((udvp->v_mount->mnt_flag & MNT_RDONLY)) 1124 return (EROFS); 1125 1126 error = VOP_ACCESS(lvp, VREAD, cred, td); 1127 if (error != 0) 1128 return (error); 1129 1130 if ((error = vn_start_write(udvp, &mp, V_WAIT | PCATCH)) != 0) 1131 return (error); 1132 error = unionfs_vn_create_on_upper(&uvp, udvp, unp, &uva, td); 1133 if (error != 0) { 1134 vn_finished_write(mp); 1135 return (error); 1136 } 1137 1138 if (docopy != 0) { 1139 error = VOP_OPEN(lvp, FREAD, cred, td, NULL); 1140 if (error == 0) { 1141 error = unionfs_copyfile_core(lvp, uvp, cred, td); 1142 VOP_CLOSE(lvp, FREAD, cred, td); 1143 } 1144 } 1145 VOP_CLOSE(uvp, FWRITE, cred, td); 1146 VOP_ADD_WRITECOUNT_CHECKED(uvp, -1); 1147 CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d", __func__, uvp, 1148 uvp->v_writecount); 1149 1150 vn_finished_write(mp); 1151 1152 if (error == 0) { 1153 /* Reset the attributes. Ignore errors. */ 1154 uva.va_type = VNON; 1155 VOP_SETATTR(uvp, &uva, cred); 1156 } 1157 1158 unionfs_node_update(unp, uvp, td); 1159 1160 return (error); 1161 } 1162 1163 /* 1164 * It checks whether vp can rmdir. (check empty) 1165 * 1166 * vp is unionfs vnode. 1167 * vp should be locked. 1168 */ 1169 int 1170 unionfs_check_rmdir(struct vnode *vp, struct ucred *cred, struct thread *td) 1171 { 1172 int error; 1173 int eofflag; 1174 int lookuperr; 1175 struct vnode *uvp; 1176 struct vnode *lvp; 1177 struct vnode *tvp; 1178 struct vattr va; 1179 struct componentname cn; 1180 /* 1181 * The size of buf needs to be larger than DIRBLKSIZ. 1182 */ 1183 char buf[256 * 6]; 1184 struct dirent *dp; 1185 struct dirent *edp; 1186 struct uio uio; 1187 struct iovec iov; 1188 1189 ASSERT_VOP_ELOCKED(vp, "unionfs_check_rmdir"); 1190 1191 eofflag = 0; 1192 uvp = UNIONFSVPTOUPPERVP(vp); 1193 lvp = UNIONFSVPTOLOWERVP(vp); 1194 1195 /* check opaque */ 1196 if ((error = VOP_GETATTR(uvp, &va, cred)) != 0) 1197 return (error); 1198 if (va.va_flags & OPAQUE) 1199 return (0); 1200 1201 /* open vnode */ 1202 #ifdef MAC 1203 if ((error = mac_vnode_check_open(cred, vp, VEXEC|VREAD)) != 0) 1204 return (error); 1205 #endif 1206 if ((error = VOP_ACCESS(vp, VEXEC|VREAD, cred, td)) != 0) 1207 return (error); 1208 if ((error = VOP_OPEN(vp, FREAD, cred, td, NULL)) != 0) 1209 return (error); 1210 1211 uio.uio_rw = UIO_READ; 1212 uio.uio_segflg = UIO_SYSSPACE; 1213 uio.uio_td = td; 1214 uio.uio_offset = 0; 1215 1216 #ifdef MAC 1217 error = mac_vnode_check_readdir(td->td_ucred, lvp); 1218 #endif 1219 while (!error && !eofflag) { 1220 iov.iov_base = buf; 1221 iov.iov_len = sizeof(buf); 1222 uio.uio_iov = &iov; 1223 uio.uio_iovcnt = 1; 1224 uio.uio_resid = iov.iov_len; 1225 1226 error = VOP_READDIR(lvp, &uio, cred, &eofflag, NULL, NULL); 1227 if (error != 0) 1228 break; 1229 if (eofflag == 0 && uio.uio_resid == sizeof(buf)) { 1230 #ifdef DIAGNOSTIC 1231 panic("bad readdir response from lower FS."); 1232 #endif 1233 break; 1234 } 1235 1236 edp = (struct dirent*)&buf[sizeof(buf) - uio.uio_resid]; 1237 for (dp = (struct dirent*)buf; !error && dp < edp; 1238 dp = (struct dirent*)((caddr_t)dp + dp->d_reclen)) { 1239 if (dp->d_type == DT_WHT || dp->d_fileno == 0 || 1240 (dp->d_namlen == 1 && dp->d_name[0] == '.') || 1241 (dp->d_namlen == 2 && !bcmp(dp->d_name, "..", 2))) 1242 continue; 1243 1244 cn.cn_namelen = dp->d_namlen; 1245 cn.cn_pnbuf = NULL; 1246 cn.cn_nameptr = dp->d_name; 1247 cn.cn_nameiop = LOOKUP; 1248 cn.cn_flags = (LOCKPARENT | LOCKLEAF | SAVENAME | RDONLY | ISLASTCN); 1249 cn.cn_lkflags = LK_EXCLUSIVE; 1250 cn.cn_thread = td; 1251 cn.cn_cred = cred; 1252 1253 /* 1254 * check entry in lower. 1255 * Sometimes, readdir function returns 1256 * wrong entry. 1257 */ 1258 lookuperr = VOP_LOOKUP(lvp, &tvp, &cn); 1259 1260 if (!lookuperr) 1261 vput(tvp); 1262 else 1263 continue; /* skip entry */ 1264 1265 /* 1266 * check entry 1267 * If it has no exist/whiteout entry in upper, 1268 * directory is not empty. 1269 */ 1270 cn.cn_flags = (LOCKPARENT | LOCKLEAF | SAVENAME | RDONLY | ISLASTCN); 1271 lookuperr = VOP_LOOKUP(uvp, &tvp, &cn); 1272 1273 if (!lookuperr) 1274 vput(tvp); 1275 1276 /* ignore exist or whiteout entry */ 1277 if (!lookuperr || 1278 (lookuperr == ENOENT && (cn.cn_flags & ISWHITEOUT))) 1279 continue; 1280 1281 error = ENOTEMPTY; 1282 } 1283 } 1284 1285 /* close vnode */ 1286 VOP_CLOSE(vp, FREAD, cred, td); 1287 1288 return (error); 1289 } 1290 1291 #ifdef DIAGNOSTIC 1292 1293 struct vnode * 1294 unionfs_checkuppervp(struct vnode *vp, char *fil, int lno) 1295 { 1296 struct unionfs_node *unp; 1297 1298 unp = VTOUNIONFS(vp); 1299 1300 #ifdef notyet 1301 if (vp->v_op != unionfs_vnodeop_p) { 1302 printf("unionfs_checkuppervp: on non-unionfs-node.\n"); 1303 #ifdef KDB 1304 kdb_enter(KDB_WHY_UNIONFS, 1305 "unionfs_checkuppervp: on non-unionfs-node.\n"); 1306 #endif 1307 panic("unionfs_checkuppervp"); 1308 } 1309 #endif 1310 return (unp->un_uppervp); 1311 } 1312 1313 struct vnode * 1314 unionfs_checklowervp(struct vnode *vp, char *fil, int lno) 1315 { 1316 struct unionfs_node *unp; 1317 1318 unp = VTOUNIONFS(vp); 1319 1320 #ifdef notyet 1321 if (vp->v_op != unionfs_vnodeop_p) { 1322 printf("unionfs_checklowervp: on non-unionfs-node.\n"); 1323 #ifdef KDB 1324 kdb_enter(KDB_WHY_UNIONFS, 1325 "unionfs_checklowervp: on non-unionfs-node.\n"); 1326 #endif 1327 panic("unionfs_checklowervp"); 1328 } 1329 #endif 1330 return (unp->un_lowervp); 1331 } 1332 #endif 1333