1 /* 2 * Copyright (c) 2011-2013 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Matthew Dillon <dillon@dragonflybsd.org> 6 * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org> 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 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 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * 3. Neither the name of The DragonFly Project nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific, prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 #include <sys/cdefs.h> 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/types.h> 39 #include <sys/lock.h> 40 #include <sys/uuid.h> 41 42 #include "hammer2.h" 43 44 RB_GENERATE2(hammer2_inode_tree, hammer2_inode, rbnode, hammer2_inode_cmp, 45 hammer2_tid_t, inum); 46 47 int 48 hammer2_inode_cmp(hammer2_inode_t *ip1, hammer2_inode_t *ip2) 49 { 50 if (ip1->inum < ip2->inum) 51 return(-1); 52 if (ip1->inum > ip2->inum) 53 return(1); 54 return(0); 55 } 56 57 /* 58 * HAMMER2 inode locks 59 * 60 * HAMMER2 offers shared locks and exclusive locks on inodes. 61 * 62 * An inode's ip->chain pointer is resolved and stable while an inode is 63 * locked, and can be cleaned out at any time (become NULL) when an inode 64 * is not locked. 65 * 66 * The underlying chain is also locked and returned. 67 * 68 * NOTE: We don't combine the inode/chain lock because putting away an 69 * inode would otherwise confuse multiple lock holders of the inode. 70 */ 71 hammer2_chain_t * 72 hammer2_inode_lock_ex(hammer2_inode_t *ip) 73 { 74 hammer2_chain_t *chain; 75 76 hammer2_inode_ref(ip); 77 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE); 78 79 /* 80 * ip->chain fixup. Certain duplications used to move inodes 81 * into indirect blocks (for example) can cause ip->chain to 82 * become stale. 83 */ 84 again: 85 chain = ip->chain; 86 if (hammer2_chain_refactor_test(chain, 1)) { 87 spin_lock(&chain->core->cst.spin); 88 while (hammer2_chain_refactor_test(chain, 1)) 89 chain = chain->next_parent; 90 if (ip->chain != chain) { 91 hammer2_chain_ref(chain); 92 spin_unlock(&chain->core->cst.spin); 93 hammer2_inode_repoint(ip, NULL, chain); 94 hammer2_chain_drop(chain); 95 } else { 96 spin_unlock(&chain->core->cst.spin); 97 } 98 } 99 100 KKASSERT(chain != NULL); /* for now */ 101 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS); 102 103 /* 104 * Resolve duplication races 105 */ 106 if (hammer2_chain_refactor_test(chain, 1)) { 107 hammer2_chain_unlock(chain); 108 goto again; 109 } 110 return (chain); 111 } 112 113 void 114 hammer2_inode_unlock_ex(hammer2_inode_t *ip, hammer2_chain_t *chain) 115 { 116 /* 117 * XXX this will catch parent directories too which we don't 118 * really want. 119 */ 120 if (chain) 121 hammer2_chain_unlock(chain); 122 ccms_thread_unlock(&ip->topo_cst); 123 hammer2_inode_drop(ip); 124 } 125 126 /* 127 * NOTE: We don't combine the inode/chain lock because putting away an 128 * inode would otherwise confuse multiple lock holders of the inode. 129 * 130 * Shared locks are especially sensitive to having too many shared 131 * lock counts (from the same thread) on certain paths which might 132 * need to upgrade them. Only one count of a shared lock can be 133 * upgraded. 134 */ 135 hammer2_chain_t * 136 hammer2_inode_lock_sh(hammer2_inode_t *ip) 137 { 138 hammer2_chain_t *chain; 139 140 hammer2_inode_ref(ip); 141 again: 142 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_SHARED); 143 144 chain = ip->chain; 145 KKASSERT(chain != NULL); /* for now */ 146 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS | 147 HAMMER2_RESOLVE_SHARED); 148 149 /* 150 * Resolve duplication races 151 */ 152 if (hammer2_chain_refactor_test(chain, 1)) { 153 hammer2_chain_unlock(chain); 154 ccms_thread_unlock(&ip->topo_cst); 155 chain = hammer2_inode_lock_ex(ip); 156 hammer2_inode_unlock_ex(ip, chain); 157 goto again; 158 } 159 return (chain); 160 } 161 162 void 163 hammer2_inode_unlock_sh(hammer2_inode_t *ip, hammer2_chain_t *chain) 164 { 165 if (chain) 166 hammer2_chain_unlock(chain); 167 ccms_thread_unlock(&ip->topo_cst); 168 hammer2_inode_drop(ip); 169 } 170 171 ccms_state_t 172 hammer2_inode_lock_temp_release(hammer2_inode_t *ip) 173 { 174 return(ccms_thread_lock_temp_release(&ip->topo_cst)); 175 } 176 177 void 178 hammer2_inode_lock_temp_restore(hammer2_inode_t *ip, ccms_state_t ostate) 179 { 180 ccms_thread_lock_temp_restore(&ip->topo_cst, ostate); 181 } 182 183 ccms_state_t 184 hammer2_inode_lock_upgrade(hammer2_inode_t *ip) 185 { 186 return(ccms_thread_lock_upgrade(&ip->topo_cst)); 187 } 188 189 void 190 hammer2_inode_lock_downgrade(hammer2_inode_t *ip, ccms_state_t ostate) 191 { 192 ccms_thread_lock_downgrade(&ip->topo_cst, ostate); 193 } 194 195 /* 196 * Lookup an inode by inode number 197 */ 198 hammer2_inode_t * 199 hammer2_inode_lookup(hammer2_pfsmount_t *pmp, hammer2_tid_t inum) 200 { 201 hammer2_inode_t *ip; 202 203 if (pmp) { 204 spin_lock(&pmp->inum_spin); 205 ip = RB_LOOKUP(hammer2_inode_tree, &pmp->inum_tree, inum); 206 if (ip) 207 hammer2_inode_ref(ip); 208 spin_unlock(&pmp->inum_spin); 209 } else { 210 ip = NULL; 211 } 212 return(ip); 213 } 214 215 /* 216 * Adding a ref to an inode is only legal if the inode already has at least 217 * one ref. 218 */ 219 void 220 hammer2_inode_ref(hammer2_inode_t *ip) 221 { 222 atomic_add_int(&ip->refs, 1); 223 } 224 225 /* 226 * Drop an inode reference, freeing the inode when the last reference goes 227 * away. 228 */ 229 void 230 hammer2_inode_drop(hammer2_inode_t *ip) 231 { 232 hammer2_pfsmount_t *pmp; 233 hammer2_inode_t *pip; 234 u_int refs; 235 236 while (ip) { 237 refs = ip->refs; 238 cpu_ccfence(); 239 if (refs == 1) { 240 /* 241 * Transition to zero, must interlock with 242 * the inode inumber lookup tree (if applicable). 243 * 244 * NOTE: The super-root inode has no pmp. 245 */ 246 pmp = ip->pmp; 247 if (pmp) 248 spin_lock(&pmp->inum_spin); 249 250 if (atomic_cmpset_int(&ip->refs, 1, 0)) { 251 KKASSERT(ip->topo_cst.count == 0); 252 if (ip->flags & HAMMER2_INODE_ONRBTREE) { 253 atomic_clear_int(&ip->flags, 254 HAMMER2_INODE_ONRBTREE); 255 RB_REMOVE(hammer2_inode_tree, 256 &pmp->inum_tree, ip); 257 } 258 if (pmp) 259 spin_unlock(&pmp->inum_spin); 260 261 pip = ip->pip; 262 ip->pip = NULL; 263 ip->pmp = NULL; 264 265 /* 266 * Cleaning out ip->chain isn't entirely 267 * trivial. 268 */ 269 hammer2_inode_repoint(ip, NULL, NULL); 270 271 /* 272 * We have to drop pip (if non-NULL) to 273 * dispose of our implied reference from 274 * ip->pip. We can simply loop on it. 275 */ 276 if (pmp) { 277 KKASSERT((ip->flags & 278 HAMMER2_INODE_SROOT) == 0); 279 kfree(ip, pmp->minode); 280 } else { 281 KKASSERT(ip->flags & 282 HAMMER2_INODE_SROOT); 283 kfree(ip, M_HAMMER2); 284 } 285 ip = pip; 286 /* continue with pip (can be NULL) */ 287 } else { 288 if (pmp) 289 spin_unlock(&ip->pmp->inum_spin); 290 } 291 } else { 292 /* 293 * Non zero transition 294 */ 295 if (atomic_cmpset_int(&ip->refs, refs, refs - 1)) 296 break; 297 } 298 } 299 } 300 301 /* 302 * Get the vnode associated with the given inode, allocating the vnode if 303 * necessary. The vnode will be returned exclusively locked. 304 * 305 * The caller must lock the inode (shared or exclusive). 306 * 307 * Great care must be taken to avoid deadlocks and vnode acquisition/reclaim 308 * races. 309 */ 310 struct vnode * 311 hammer2_igetv(hammer2_inode_t *ip, int *errorp) 312 { 313 hammer2_inode_data_t *ipdata; 314 hammer2_pfsmount_t *pmp; 315 struct vnode *vp; 316 ccms_state_t ostate; 317 318 pmp = ip->pmp; 319 KKASSERT(pmp != NULL); 320 *errorp = 0; 321 ipdata = &ip->chain->data->ipdata; 322 323 for (;;) { 324 /* 325 * Attempt to reuse an existing vnode assignment. It is 326 * possible to race a reclaim so the vget() may fail. The 327 * inode must be unlocked during the vget() to avoid a 328 * deadlock against a reclaim. 329 */ 330 vp = ip->vp; 331 if (vp) { 332 /* 333 * Inode must be unlocked during the vget() to avoid 334 * possible deadlocks, but leave the ip ref intact. 335 * 336 * vnode is held to prevent destruction during the 337 * vget(). The vget() can still fail if we lost 338 * a reclaim race on the vnode. 339 */ 340 vhold_interlocked(vp); 341 ostate = hammer2_inode_lock_temp_release(ip); 342 if (vget(vp, LK_EXCLUSIVE)) { 343 vdrop(vp); 344 hammer2_inode_lock_temp_restore(ip, ostate); 345 continue; 346 } 347 hammer2_inode_lock_temp_restore(ip, ostate); 348 vdrop(vp); 349 /* vp still locked and ref from vget */ 350 if (ip->vp != vp) { 351 kprintf("hammer2: igetv race %p/%p\n", 352 ip->vp, vp); 353 vput(vp); 354 continue; 355 } 356 *errorp = 0; 357 break; 358 } 359 360 /* 361 * No vnode exists, allocate a new vnode. Beware of 362 * allocation races. This function will return an 363 * exclusively locked and referenced vnode. 364 */ 365 *errorp = getnewvnode(VT_HAMMER2, pmp->mp, &vp, 0, 0); 366 if (*errorp) { 367 kprintf("hammer2: igetv getnewvnode failed %d\n", 368 *errorp); 369 vp = NULL; 370 break; 371 } 372 373 /* 374 * Lock the inode and check for an allocation race. 375 */ 376 ostate = hammer2_inode_lock_upgrade(ip); 377 if (ip->vp != NULL) { 378 vp->v_type = VBAD; 379 vx_put(vp); 380 hammer2_inode_lock_downgrade(ip, ostate); 381 continue; 382 } 383 384 switch (ipdata->type) { 385 case HAMMER2_OBJTYPE_DIRECTORY: 386 vp->v_type = VDIR; 387 break; 388 case HAMMER2_OBJTYPE_REGFILE: 389 vp->v_type = VREG; 390 vinitvmio(vp, ipdata->size, 391 HAMMER2_LBUFSIZE, 392 (int)ipdata->size & HAMMER2_LBUFMASK); 393 break; 394 case HAMMER2_OBJTYPE_SOFTLINK: 395 /* 396 * XXX for now we are using the generic file_read 397 * and file_write code so we need a buffer cache 398 * association. 399 */ 400 vp->v_type = VLNK; 401 vinitvmio(vp, ipdata->size, 402 HAMMER2_LBUFSIZE, 403 (int)ipdata->size & HAMMER2_LBUFMASK); 404 break; 405 /* XXX FIFO */ 406 default: 407 panic("hammer2: unhandled objtype %d", ipdata->type); 408 break; 409 } 410 411 if (ip == pmp->iroot) 412 vsetflags(vp, VROOT); 413 414 vp->v_data = ip; 415 ip->vp = vp; 416 hammer2_inode_ref(ip); /* vp association */ 417 hammer2_inode_lock_downgrade(ip, ostate); 418 break; 419 } 420 421 /* 422 * Return non-NULL vp and *errorp == 0, or NULL vp and *errorp != 0. 423 */ 424 if (hammer2_debug & 0x0002) { 425 kprintf("igetv vp %p refs %d aux %d\n", 426 vp, vp->v_sysref.refcnt, vp->v_auxrefs); 427 } 428 return (vp); 429 } 430 431 /* 432 * The passed-in chain must be locked and the returned inode will also be 433 * locked. This routine typically locates or allocates the inode, assigns 434 * ip->chain (adding a ref to chain if necessary), and returns the inode. 435 * 436 * The hammer2_inode structure regulates the interface between the high level 437 * kernel VNOPS API and the filesystem backend (the chains). 438 * 439 * WARNING! This routine sucks up the chain's lock (makes it part of the 440 * inode lock from the point of view of the inode lock API), 441 * so callers need to be careful. 442 * 443 * WARNING! The mount code is allowed to pass dip == NULL for iroot and 444 * is allowed to pass pmp == NULL and dip == NULL for sroot. 445 */ 446 hammer2_inode_t * 447 hammer2_inode_get(hammer2_pfsmount_t *pmp, hammer2_inode_t *dip, 448 hammer2_chain_t *chain) 449 { 450 hammer2_inode_t *nip; 451 452 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_INODE); 453 454 /* 455 * Interlocked lookup/ref of the inode. This code is only needed 456 * when looking up inodes with nlinks != 0 (TODO: optimize out 457 * otherwise and test for duplicates). 458 */ 459 again: 460 for (;;) { 461 nip = hammer2_inode_lookup(pmp, chain->data->ipdata.inum); 462 if (nip == NULL) 463 break; 464 ccms_thread_lock(&nip->topo_cst, CCMS_STATE_EXCLUSIVE); 465 if ((nip->flags & HAMMER2_INODE_ONRBTREE) == 0) { /* race */ 466 ccms_thread_unlock(&nip->topo_cst); 467 hammer2_inode_drop(nip); 468 continue; 469 } 470 if (nip->chain != chain) 471 hammer2_inode_repoint(nip, NULL, chain); 472 473 /* 474 * Consolidated nip/nip->chain is locked (chain locked 475 * by caller). 476 */ 477 return nip; 478 } 479 480 /* 481 * We couldn't find the inode number, create a new inode. 482 */ 483 if (pmp) { 484 nip = kmalloc(sizeof(*nip), pmp->minode, M_WAITOK | M_ZERO); 485 } else { 486 nip = kmalloc(sizeof(*nip), M_HAMMER2, M_WAITOK | M_ZERO); 487 nip->flags = HAMMER2_INODE_SROOT; 488 } 489 nip->inum = chain->data->ipdata.inum; 490 hammer2_inode_repoint(nip, NULL, chain); 491 nip->pip = dip; /* can be NULL */ 492 if (dip) 493 hammer2_inode_ref(dip); /* ref dip for nip->pip */ 494 495 nip->pmp = pmp; 496 497 /* 498 * ref and lock on nip gives it state compatible to after a 499 * hammer2_inode_lock_ex() call. 500 */ 501 nip->refs = 1; 502 ccms_cst_init(&nip->topo_cst, &nip->chain); 503 ccms_thread_lock(&nip->topo_cst, CCMS_STATE_EXCLUSIVE); 504 /* combination of thread lock and chain lock == inode lock */ 505 506 /* 507 * Attempt to add the inode. If it fails we raced another inode 508 * get. Undo all the work and try again. 509 */ 510 if (pmp) { 511 spin_lock(&pmp->inum_spin); 512 if (RB_INSERT(hammer2_inode_tree, &pmp->inum_tree, nip)) { 513 spin_unlock(&pmp->inum_spin); 514 ccms_thread_unlock(&nip->topo_cst); 515 hammer2_inode_drop(nip); 516 goto again; 517 } 518 atomic_set_int(&nip->flags, HAMMER2_INODE_ONRBTREE); 519 spin_unlock(&pmp->inum_spin); 520 } 521 522 return (nip); 523 } 524 525 /* 526 * Create a new inode in the specified directory using the vattr to 527 * figure out the type of inode. 528 * 529 * If no error occurs the new inode with its chain locked is returned in 530 * *nipp, otherwise an error is returned and *nipp is set to NULL. 531 * 532 * If vap and/or cred are NULL the related fields are not set and the 533 * inode type defaults to a directory. This is used when creating PFSs 534 * under the super-root, so the inode number is set to 1 in this case. 535 * 536 * dip is not locked on entry. 537 */ 538 hammer2_inode_t * 539 hammer2_inode_create(hammer2_trans_t *trans, hammer2_inode_t *dip, 540 struct vattr *vap, struct ucred *cred, 541 const uint8_t *name, size_t name_len, 542 hammer2_chain_t **chainp, int *errorp) 543 { 544 hammer2_inode_data_t *dipdata; 545 hammer2_inode_data_t *nipdata; 546 hammer2_chain_t *chain; 547 hammer2_chain_t *parent; 548 hammer2_inode_t *nip; 549 hammer2_key_t lhc; 550 int error; 551 uid_t xuid; 552 uuid_t dip_uid; 553 uuid_t dip_gid; 554 uint32_t dip_mode; 555 556 lhc = hammer2_dirhash(name, name_len); 557 *errorp = 0; 558 559 /* 560 * Locate the inode or indirect block to create the new 561 * entry in. At the same time check for key collisions 562 * and iterate until we don't get one. 563 * 564 * NOTE: hidden inodes do not have iterators. 565 */ 566 retry: 567 parent = hammer2_inode_lock_ex(dip); 568 dipdata = &dip->chain->data->ipdata; 569 dip_uid = dipdata->uid; 570 dip_gid = dipdata->gid; 571 dip_mode = dipdata->mode; 572 573 error = 0; 574 while (error == 0) { 575 chain = hammer2_chain_lookup(&parent, lhc, lhc, 0); 576 if (chain == NULL) 577 break; 578 if ((lhc & HAMMER2_DIRHASH_VISIBLE) == 0) 579 error = ENOSPC; 580 if ((lhc & HAMMER2_DIRHASH_LOMASK) == HAMMER2_DIRHASH_LOMASK) 581 error = ENOSPC; 582 hammer2_chain_unlock(chain); 583 chain = NULL; 584 ++lhc; 585 } 586 if (error == 0) { 587 error = hammer2_chain_create(trans, &parent, &chain, 588 lhc, 0, 589 HAMMER2_BREF_TYPE_INODE, 590 HAMMER2_INODE_BYTES); 591 } 592 593 /* 594 * Cleanup and handle retries. 595 */ 596 if (error == EAGAIN) { 597 hammer2_chain_ref(parent); 598 hammer2_inode_unlock_ex(dip, parent); 599 hammer2_chain_wait(parent); 600 hammer2_chain_drop(parent); 601 goto retry; 602 } 603 hammer2_inode_unlock_ex(dip, parent); 604 605 if (error) { 606 KKASSERT(chain == NULL); 607 *errorp = error; 608 return (NULL); 609 } 610 611 /* 612 * Set up the new inode. 613 * 614 * NOTE: *_get() integrates chain's lock into the inode lock. 615 * 616 * NOTE: Only one new inode can currently be created per 617 * transaction. If the need arises we can adjust 618 * hammer2_trans_init() to allow more. 619 */ 620 chain->data->ipdata.inum = trans->sync_tid; 621 nip = hammer2_inode_get(dip->pmp, dip, chain); 622 nipdata = &chain->data->ipdata; 623 624 if (vap) { 625 KKASSERT(trans->inodes_created == 0); 626 nipdata->type = hammer2_get_obj_type(vap->va_type); 627 nipdata->inum = trans->sync_tid; 628 ++trans->inodes_created; 629 } else { 630 nipdata->type = HAMMER2_OBJTYPE_DIRECTORY; 631 nipdata->inum = 1; 632 } 633 nipdata->version = HAMMER2_INODE_VERSION_ONE; 634 hammer2_update_time(&nipdata->ctime); 635 nipdata->mtime = nipdata->ctime; 636 if (vap) 637 nipdata->mode = vap->va_mode; 638 nipdata->nlinks = 1; 639 if (vap) { 640 if (dip) { 641 xuid = hammer2_to_unix_xid(&dip_uid); 642 xuid = vop_helper_create_uid(dip->pmp->mp, 643 dip_mode, 644 xuid, 645 cred, 646 &vap->va_mode); 647 } else { 648 xuid = 0; 649 } 650 if (vap->va_vaflags & VA_UID_UUID_VALID) 651 nipdata->uid = vap->va_uid_uuid; 652 else if (vap->va_uid != (uid_t)VNOVAL) 653 hammer2_guid_to_uuid(&nipdata->uid, vap->va_uid); 654 else 655 hammer2_guid_to_uuid(&nipdata->uid, xuid); 656 657 if (vap->va_vaflags & VA_GID_UUID_VALID) 658 nipdata->gid = vap->va_gid_uuid; 659 else if (vap->va_gid != (gid_t)VNOVAL) 660 hammer2_guid_to_uuid(&nipdata->gid, vap->va_gid); 661 else if (dip) 662 nipdata->gid = dip_gid; 663 } 664 665 /* 666 * Regular files and softlinks allow a small amount of data to be 667 * directly embedded in the inode. This flag will be cleared if 668 * the size is extended past the embedded limit. 669 */ 670 if (nipdata->type == HAMMER2_OBJTYPE_REGFILE || 671 nipdata->type == HAMMER2_OBJTYPE_SOFTLINK) { 672 nipdata->op_flags |= HAMMER2_OPFLAG_DIRECTDATA; 673 } 674 675 KKASSERT(name_len < HAMMER2_INODE_MAXNAME); 676 bcopy(name, nipdata->filename, name_len); 677 nipdata->name_key = lhc; 678 nipdata->name_len = name_len; 679 *chainp = chain; 680 681 return (nip); 682 } 683 684 /* 685 * chain may have been moved around by the create. 686 */ 687 static 688 void 689 hammer2_chain_refactor(hammer2_chain_t **chainp) 690 { 691 hammer2_chain_t *chain = *chainp; 692 hammer2_chain_core_t *core; 693 694 core = chain->core; 695 spin_lock(&core->cst.spin); 696 while (hammer2_chain_refactor_test(chain, 1)) { 697 chain = chain->next_parent; 698 while (hammer2_chain_refactor_test(chain, 1)) 699 chain = chain->next_parent; 700 hammer2_chain_ref(chain); 701 spin_unlock(&core->cst.spin); 702 703 hammer2_chain_unlock(*chainp); 704 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS | 705 HAMMER2_RESOLVE_NOREF); /* eat ref */ 706 *chainp = chain; 707 spin_lock(&core->cst.spin); 708 } 709 spin_unlock(&core->cst.spin); 710 } 711 712 /* 713 * ochain represents the target file inode. We need to move it to the 714 * specified common parent directory (dip) and rename it to a special 715 * invisible "0xINODENUMBER" filename. 716 * 717 * We use chain_duplicate and duplicate ochain at the new location, 718 * renaming it appropriately. We create a temporary chain and 719 * then delete it to placemark where the duplicate will go. Both of 720 * these use the inode number for (lhc) (the key), generating the 721 * invisible filename. 722 */ 723 static 724 hammer2_chain_t * 725 hammer2_hardlink_shiftup(hammer2_trans_t *trans, hammer2_chain_t **ochainp, 726 hammer2_inode_t *dip, int *errorp) 727 { 728 hammer2_inode_data_t *nipdata; 729 hammer2_chain_t *parent; 730 hammer2_chain_t *ochain; 731 hammer2_chain_t *nchain; 732 hammer2_chain_t *tmp; 733 hammer2_key_t lhc; 734 hammer2_blockref_t bref; 735 736 ochain = *ochainp; 737 *errorp = 0; 738 lhc = ochain->data->ipdata.inum; 739 KKASSERT((lhc & HAMMER2_DIRHASH_VISIBLE) == 0); 740 741 /* 742 * Locate the inode or indirect block to create the new 743 * entry in. lhc represents the inode number so there is 744 * no collision iteration. 745 * 746 * There should be no key collisions with invisible inode keys. 747 */ 748 retry: 749 parent = hammer2_chain_lookup_init(dip->chain, 0); 750 nchain = hammer2_chain_lookup(&parent, lhc, lhc, 0); 751 if (nchain) { 752 kprintf("X3 chain %p parent %p dip %p dip->chain %p\n", 753 nchain, parent, dip, dip->chain); 754 hammer2_chain_unlock(nchain); 755 nchain = NULL; 756 *errorp = ENOSPC; 757 #if 1 758 Debugger("X3"); 759 #endif 760 } 761 762 /* 763 * Create entry in common parent directory using the seek position 764 * calculated above. 765 */ 766 if (*errorp == 0) { 767 KKASSERT(nchain == NULL); 768 *errorp = hammer2_chain_create(trans, &parent, &nchain, 769 lhc, 0, 770 HAMMER2_BREF_TYPE_INODE,/* n/a */ 771 HAMMER2_INODE_BYTES); /* n/a */ 772 hammer2_chain_refactor(&ochain); 773 *ochainp = ochain; 774 } 775 776 /* 777 * Cleanup and handle retries. 778 */ 779 if (*errorp == EAGAIN) { 780 hammer2_chain_ref(parent); 781 hammer2_chain_lookup_done(parent); 782 hammer2_chain_wait(parent); 783 hammer2_chain_drop(parent); 784 goto retry; 785 } 786 787 /* 788 * Handle the error case 789 */ 790 if (*errorp) { 791 KKASSERT(nchain == NULL); 792 hammer2_chain_lookup_done(parent); 793 return (NULL); 794 } 795 796 /* 797 * Use chain as a placeholder for (lhc), delete it and replace 798 * it with our duplication. 799 * 800 * Gain a second lock on ochain for the duplication function to 801 * unlock, maintain the caller's original lock across the call. 802 * 803 * This is a bit messy. 804 */ 805 hammer2_chain_delete(trans, nchain); 806 hammer2_chain_lock(ochain, HAMMER2_RESOLVE_ALWAYS); 807 tmp = ochain; 808 bref = tmp->bref; 809 bref.key = lhc; /* invisible dir entry key */ 810 bref.keybits = 0; 811 hammer2_chain_duplicate(trans, parent, nchain->index, &tmp, &bref); 812 hammer2_chain_lookup_done(parent); 813 hammer2_chain_unlock(nchain); /* no longer needed */ 814 815 /* 816 * Now set chain to our duplicate and modify it appropriately. 817 * 818 * Directory entries are inodes but this is a hidden hardlink 819 * target. The name isn't used but to ease debugging give it 820 * a name after its inode number. 821 */ 822 nchain = tmp; 823 tmp = NULL; /* safety */ 824 825 hammer2_chain_modify(trans, &nchain, HAMMER2_MODIFY_ASSERTNOCOPY); 826 nipdata = &nchain->data->ipdata; 827 ksnprintf(nipdata->filename, sizeof(nipdata->filename), 828 "0x%016jx", (intmax_t)nipdata->inum); 829 nipdata->name_len = strlen(nipdata->filename); 830 nipdata->name_key = lhc; 831 832 return (nchain); 833 } 834 835 /* 836 * Connect the target inode represented by (*chainp) to the media topology 837 * at (dip, name, len). 838 * 839 * If hlink is TRUE this function creates an OBJTYPE_HARDLINK directory 840 * entry instead of connecting (*chainp). 841 * 842 * If hlink is FALSE this function uses chain_duplicate() to make a copy 843 * if (*chainp) in the directory entry. (*chainp) is likely to be deleted 844 * by the caller in this case (e.g. rename). 845 */ 846 int 847 hammer2_inode_connect(hammer2_trans_t *trans, int hlink, 848 hammer2_inode_t *dip, hammer2_chain_t **chainp, 849 const uint8_t *name, size_t name_len) 850 { 851 hammer2_inode_data_t *ipdata; 852 hammer2_chain_t *nchain; 853 hammer2_chain_t *parent; 854 hammer2_chain_t *ochain; 855 hammer2_key_t lhc; 856 int error; 857 858 ochain = *chainp; 859 860 /* 861 * Since ochain is either disconnected from the topology or represents 862 * a hardlink terminus which is always a parent of or equal to dip, 863 * we should be able to safely lock dip->chain for our setup. 864 */ 865 parent = hammer2_chain_lookup_init(dip->chain, 0); 866 867 lhc = hammer2_dirhash(name, name_len); 868 869 /* 870 * Locate the inode or indirect block to create the new 871 * entry in. At the same time check for key collisions 872 * and iterate until we don't get one. 873 */ 874 error = 0; 875 while (error == 0) { 876 nchain = hammer2_chain_lookup(&parent, lhc, lhc, 0); 877 if (nchain == NULL) 878 break; 879 if ((lhc & HAMMER2_DIRHASH_LOMASK) == HAMMER2_DIRHASH_LOMASK) 880 error = ENOSPC; 881 hammer2_chain_unlock(nchain); 882 nchain = NULL; 883 ++lhc; 884 } 885 886 if (error == 0) { 887 if (hlink) { 888 /* 889 * Hardlink pointer needed, create totally fresh 890 * directory entry. 891 */ 892 KKASSERT(nchain == NULL); 893 error = hammer2_chain_create(trans, &parent, &nchain, 894 lhc, 0, 895 HAMMER2_BREF_TYPE_INODE, 896 HAMMER2_INODE_BYTES); 897 hammer2_chain_refactor(&ochain); 898 } else { 899 /* 900 * Reconnect the original chain and rename. Use 901 * chain_duplicate(). The caller will likely delete 902 * or has already deleted the original chain in 903 * this case. 904 * 905 * NOTE: chain_duplicate() generates a new chain 906 * with CHAIN_DELETED cleared (ochain typically 907 * has it set from the file unlink). 908 */ 909 nchain = ochain; 910 ochain = NULL; 911 hammer2_chain_duplicate(trans, NULL, -1, &nchain, NULL); 912 error = hammer2_chain_create(trans, &parent, &nchain, 913 lhc, 0, 914 HAMMER2_BREF_TYPE_INODE, 915 HAMMER2_INODE_BYTES); 916 } 917 } 918 919 /* 920 * Unlock stuff. 921 */ 922 KKASSERT(error != EAGAIN); 923 hammer2_chain_lookup_done(parent); 924 parent = NULL; 925 926 /* 927 * nchain should be NULL on error, leave ochain (== *chainp) alone. 928 */ 929 if (error) { 930 KKASSERT(nchain == NULL); 931 return (error); 932 } 933 934 /* 935 * Directory entries are inodes so if the name has changed we have 936 * to update the inode. 937 * 938 * When creating an OBJTYPE_HARDLINK entry remember to unlock the 939 * chain, the caller will access the hardlink via the actual hardlink 940 * target file and not the hardlink pointer entry, so we must still 941 * return ochain. 942 */ 943 if (hlink && hammer2_hardlink_enable >= 0) { 944 /* 945 * Create the HARDLINK pointer. oip represents the hardlink 946 * target in this situation. 947 * 948 * We will return ochain (the hardlink target). 949 */ 950 hammer2_chain_modify(trans, &nchain, 951 HAMMER2_MODIFY_ASSERTNOCOPY); 952 KKASSERT(name_len < HAMMER2_INODE_MAXNAME); 953 ipdata = &nchain->data->ipdata; 954 bcopy(name, ipdata->filename, name_len); 955 ipdata->name_key = lhc; 956 ipdata->name_len = name_len; 957 ipdata->target_type = ochain->data->ipdata.type; 958 ipdata->type = HAMMER2_OBJTYPE_HARDLINK; 959 ipdata->inum = ochain->data->ipdata.inum; 960 ipdata->nlinks = 1; 961 hammer2_chain_unlock(nchain); 962 nchain = ochain; 963 ochain = NULL; 964 } else if (hlink && hammer2_hardlink_enable < 0) { 965 /* 966 * Create a snapshot (hardlink fake mode for debugging). 967 * (ochain already flushed above so we can just copy the 968 * bref XXX). 969 * 970 * Since this is a snapshot we return nchain in the fake 971 * hardlink case. 972 */ 973 hammer2_chain_modify(trans, &nchain, 974 HAMMER2_MODIFY_ASSERTNOCOPY); 975 KKASSERT(name_len < HAMMER2_INODE_MAXNAME); 976 ipdata = &nchain->data->ipdata; 977 *ipdata = ochain->data->ipdata; 978 bcopy(name, ipdata->filename, name_len); 979 ipdata->name_key = lhc; 980 ipdata->name_len = name_len; 981 kprintf("created fake hardlink %*.*s\n", 982 (int)name_len, (int)name_len, name); 983 } else { 984 /* 985 * nchain is a duplicate of ochain at the new location. 986 * We must fixup the name stored in oip. The bref key 987 * has already been set up. 988 */ 989 hammer2_chain_modify(trans, &nchain, 990 HAMMER2_MODIFY_ASSERTNOCOPY); 991 ipdata = &nchain->data->ipdata; 992 993 KKASSERT(name_len < HAMMER2_INODE_MAXNAME); 994 bcopy(name, ipdata->filename, name_len); 995 ipdata->name_key = lhc; 996 ipdata->name_len = name_len; 997 ipdata->nlinks = 1; 998 } 999 1000 /* 1001 * We are replacing ochain with nchain, unlock ochain. In the 1002 * case where ochain is left unchanged the code above sets 1003 * nchain to ochain and ochain to NULL, resulting in a NOP here. 1004 */ 1005 if (ochain) 1006 hammer2_chain_unlock(ochain); 1007 *chainp = nchain; 1008 1009 return (0); 1010 } 1011 1012 /* 1013 * Repoint ip->chain to nchain. Caller must hold the inode exclusively 1014 * locked. 1015 * 1016 * ip->chain is set to nchain. The prior chain in ip->chain is dropped 1017 * and nchain is ref'd. 1018 */ 1019 void 1020 hammer2_inode_repoint(hammer2_inode_t *ip, hammer2_inode_t *pip, 1021 hammer2_chain_t *nchain) 1022 { 1023 hammer2_chain_t *ochain; 1024 hammer2_inode_t *opip; 1025 1026 /* 1027 * Repoint ip->chain if requested. 1028 */ 1029 ochain = ip->chain; 1030 ip->chain = nchain; 1031 if (nchain) 1032 hammer2_chain_ref(nchain); 1033 if (ochain) 1034 hammer2_chain_drop(ochain); 1035 1036 /* 1037 * Repoint ip->pip if requested (non-NULL pip). 1038 */ 1039 if (pip && ip->pip != pip) { 1040 opip = ip->pip; 1041 hammer2_inode_ref(pip); 1042 ip->pip = pip; 1043 if (opip) 1044 hammer2_inode_drop(opip); 1045 } 1046 } 1047 1048 /* 1049 * Unlink the file from the specified directory inode. The directory inode 1050 * does not need to be locked. 1051 * 1052 * isdir determines whether a directory/non-directory check should be made. 1053 * No check is made if isdir is set to -1. 1054 * 1055 * NOTE! This function does not prevent the underlying file from still 1056 * being used if it has other refs (such as from an inode, or if it's 1057 * chain is manually held). However, the caller is responsible for 1058 * fixing up ip->chain if e.g. a rename occurs (see chain_duplicate()). 1059 */ 1060 int 1061 hammer2_unlink_file(hammer2_trans_t *trans, hammer2_inode_t *dip, 1062 const uint8_t *name, size_t name_len, 1063 int isdir, int *hlinkp) 1064 { 1065 hammer2_inode_data_t *ipdata; 1066 hammer2_chain_t *parent; 1067 hammer2_chain_t *ochain; 1068 hammer2_chain_t *chain; 1069 hammer2_chain_t *dparent; 1070 hammer2_chain_t *dchain; 1071 hammer2_key_t lhc; 1072 int error; 1073 uint8_t type; 1074 1075 error = 0; 1076 ochain = NULL; 1077 lhc = hammer2_dirhash(name, name_len); 1078 1079 /* 1080 * Search for the filename in the directory 1081 */ 1082 if (hlinkp) 1083 *hlinkp = 0; 1084 parent = hammer2_inode_lock_ex(dip); 1085 chain = hammer2_chain_lookup(&parent, 1086 lhc, lhc + HAMMER2_DIRHASH_LOMASK, 1087 0); 1088 while (chain) { 1089 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE && 1090 name_len == chain->data->ipdata.name_len && 1091 bcmp(name, chain->data->ipdata.filename, name_len) == 0) { 1092 break; 1093 } 1094 chain = hammer2_chain_next(&parent, chain, 1095 lhc, lhc + HAMMER2_DIRHASH_LOMASK, 1096 0); 1097 } 1098 hammer2_inode_unlock_ex(dip, NULL); /* retain parent */ 1099 1100 /* 1101 * Not found or wrong type (isdir < 0 disables the type check). 1102 * If a hardlink pointer, type checks use the hardlink target. 1103 */ 1104 if (chain == NULL) { 1105 error = ENOENT; 1106 goto done; 1107 } 1108 if ((type = chain->data->ipdata.type) == HAMMER2_OBJTYPE_HARDLINK) { 1109 if (hlinkp) 1110 *hlinkp = 1; 1111 type = chain->data->ipdata.target_type; 1112 } 1113 1114 if (type == HAMMER2_OBJTYPE_DIRECTORY && isdir == 0) { 1115 error = ENOTDIR; 1116 goto done; 1117 } 1118 if (type != HAMMER2_OBJTYPE_DIRECTORY && isdir >= 1) { 1119 error = EISDIR; 1120 goto done; 1121 } 1122 1123 /* 1124 * Hardlink must be resolved. We can't hold parent locked while we 1125 * do this or we could deadlock. 1126 * 1127 * On success chain will be adjusted to point at the hardlink target 1128 * and ochain will point to the hardlink pointer in the original 1129 * directory. Otherwise chain remains pointing to the original. 1130 */ 1131 if (chain->data->ipdata.type == HAMMER2_OBJTYPE_HARDLINK) { 1132 hammer2_chain_unlock(parent); 1133 parent = NULL; 1134 error = hammer2_hardlink_find(dip, &chain, &ochain); 1135 } 1136 1137 /* 1138 * If this is a directory the directory must be empty. However, if 1139 * isdir < 0 we are doing a rename and the directory does not have 1140 * to be empty, and if isdir > 1 we are deleting a PFS/snapshot 1141 * and the directory does not have to be empty. 1142 * 1143 * NOTE: We check the full key range here which covers both visible 1144 * and invisible entries. Theoretically there should be no 1145 * invisible (hardlink target) entries if there are no visible 1146 * entries. 1147 */ 1148 if (type == HAMMER2_OBJTYPE_DIRECTORY && isdir == 1) { 1149 dparent = hammer2_chain_lookup_init(chain, 0); 1150 dchain = hammer2_chain_lookup(&dparent, 1151 0, (hammer2_key_t)-1, 1152 HAMMER2_LOOKUP_NODATA); 1153 if (dchain) { 1154 hammer2_chain_unlock(dchain); 1155 hammer2_chain_lookup_done(dparent); 1156 error = ENOTEMPTY; 1157 goto done; 1158 } 1159 hammer2_chain_lookup_done(dparent); 1160 dparent = NULL; 1161 /* dchain NULL */ 1162 } 1163 1164 /* 1165 * Ok, we can now unlink the chain. We always decrement nlinks even 1166 * if the entry can be deleted in case someone has the file open and 1167 * does an fstat(). 1168 * 1169 * The chain itself will no longer be in the on-media topology but 1170 * can still be flushed to the media (e.g. if an open descriptor 1171 * remains). When the last vnode/ip ref goes away the chain will 1172 * be marked unmodified, avoiding any further (now unnecesary) I/O. 1173 * 1174 * A non-NULL ochain indicates a hardlink. 1175 */ 1176 if (ochain) { 1177 /* 1178 * Delete the original hardlink pointer. 1179 * 1180 * NOTE: parent from above is NULL when ochain != NULL 1181 * so we can reuse it. 1182 */ 1183 hammer2_chain_lock(ochain, HAMMER2_RESOLVE_ALWAYS); 1184 hammer2_chain_delete(trans, ochain); 1185 hammer2_chain_unlock(ochain); 1186 1187 /* 1188 * Then decrement nlinks on hardlink target, deleting 1189 * the target when nlinks drops to 0. 1190 */ 1191 hammer2_chain_modify(trans, &chain, 0); 1192 --chain->data->ipdata.nlinks; 1193 if (chain->data->ipdata.nlinks == 0) 1194 hammer2_chain_delete(trans, chain); 1195 } else { 1196 /* 1197 * Otherwise this was not a hardlink and we can just 1198 * remove the entry and decrement nlinks. 1199 * 1200 * NOTE: *_get() integrates chain's lock into the inode lock. 1201 */ 1202 hammer2_chain_modify(trans, &chain, 0); 1203 ipdata = &chain->data->ipdata; 1204 --ipdata->nlinks; 1205 hammer2_chain_delete(trans, chain); 1206 } 1207 1208 error = 0; 1209 done: 1210 if (chain) 1211 hammer2_chain_unlock(chain); 1212 if (parent) 1213 hammer2_chain_lookup_done(parent); 1214 if (ochain) 1215 hammer2_chain_drop(ochain); 1216 1217 return error; 1218 } 1219 1220 /* 1221 * Given an exclusively locked inode we consolidate its chain for hardlink 1222 * creation, adding (nlinks) to the file's link count and potentially 1223 * relocating the inode to a directory common to ip->pip and tdip. 1224 * 1225 * Replaces (*chainp) if consolidation occurred, unlocking the old chain 1226 * and returning a new locked chain. 1227 * 1228 * NOTE! This function will also replace ip->chain. 1229 */ 1230 int 1231 hammer2_hardlink_consolidate(hammer2_trans_t *trans, hammer2_inode_t *ip, 1232 hammer2_chain_t **chainp, 1233 hammer2_inode_t *tdip, int nlinks) 1234 { 1235 hammer2_inode_data_t *ipdata; 1236 hammer2_inode_t *fdip; 1237 hammer2_inode_t *cdip; 1238 hammer2_chain_t *chain; 1239 hammer2_chain_t *nchain; 1240 int error; 1241 1242 chain = *chainp; 1243 if (nlinks == 0 && /* no hardlink needed */ 1244 (chain->data->ipdata.name_key & HAMMER2_DIRHASH_VISIBLE)) { 1245 return (0); 1246 } 1247 if (hammer2_hardlink_enable < 0) { /* fake hardlinks */ 1248 return (0); 1249 } 1250 1251 if (hammer2_hardlink_enable == 0) { /* disallow hardlinks */ 1252 hammer2_chain_unlock(chain); 1253 *chainp = NULL; 1254 return (ENOTSUP); 1255 } 1256 1257 /* 1258 * cdip will be returned with a ref, but not locked. 1259 */ 1260 fdip = ip->pip; 1261 cdip = hammer2_inode_common_parent(fdip, tdip); 1262 1263 /* 1264 * If no change in the hardlink's target directory is required and 1265 * this is already a hardlink target, all we need to do is adjust 1266 * the link count. 1267 * 1268 * XXX The common parent is a big wiggly due to duplication from 1269 * renames. Compare the core (RBTREE) pointer instead of the 1270 * ip's. 1271 */ 1272 if (cdip == fdip && 1273 (chain->data->ipdata.name_key & HAMMER2_DIRHASH_VISIBLE) == 0) { 1274 if (nlinks) { 1275 hammer2_chain_modify(trans, &chain, 0); 1276 chain->data->ipdata.nlinks += nlinks; 1277 } 1278 error = 0; 1279 goto done; 1280 } 1281 1282 /* 1283 * We either have to move an existing hardlink target or we have 1284 * to create a fresh hardlink target. 1285 * 1286 * Hardlink targets are hidden inodes in a parent directory common 1287 * to all directory entries referencing the hardlink. 1288 */ 1289 nchain = hammer2_hardlink_shiftup(trans, &chain, cdip, &error); 1290 1291 if (error == 0) { 1292 /* 1293 * Bump nlinks on duplicated hidden inode, repoint 1294 * ip->chain. 1295 */ 1296 hammer2_chain_modify(trans, &nchain, 0); 1297 nchain->data->ipdata.nlinks += nlinks; 1298 hammer2_inode_repoint(ip, cdip, nchain); 1299 1300 /* 1301 * If the old chain is not a hardlink target then replace 1302 * it with a OBJTYPE_HARDLINK pointer. 1303 * 1304 * If the old chain IS a hardlink target then delete it. 1305 */ 1306 if (chain->data->ipdata.name_key & HAMMER2_DIRHASH_VISIBLE) { 1307 /* 1308 * Replace original non-hardlink that's been dup'd 1309 * with a special hardlink directory entry. We must 1310 * set the DIRECTDATA flag to prevent sub-chains 1311 * from trying to synchronize to the inode if the 1312 * file is extended afterwords. 1313 */ 1314 hammer2_chain_modify(trans, &chain, 0); 1315 hammer2_chain_delete_duplicate(trans, &chain, 1316 HAMMER2_DELDUP_RECORE); 1317 ipdata = &chain->data->ipdata; 1318 ipdata->target_type = ipdata->type; 1319 ipdata->type = HAMMER2_OBJTYPE_HARDLINK; 1320 ipdata->uflags = 0; 1321 ipdata->rmajor = 0; 1322 ipdata->rminor = 0; 1323 ipdata->ctime = 0; 1324 ipdata->mtime = 0; 1325 ipdata->atime = 0; 1326 ipdata->btime = 0; 1327 bzero(&ipdata->uid, sizeof(ipdata->uid)); 1328 bzero(&ipdata->gid, sizeof(ipdata->gid)); 1329 ipdata->op_flags = HAMMER2_OPFLAG_DIRECTDATA; 1330 ipdata->cap_flags = 0; 1331 ipdata->mode = 0; 1332 ipdata->size = 0; 1333 ipdata->nlinks = 1; 1334 ipdata->iparent = 0; /* XXX */ 1335 ipdata->pfs_type = 0; 1336 ipdata->pfs_inum = 0; 1337 bzero(&ipdata->pfs_clid, sizeof(ipdata->pfs_clid)); 1338 bzero(&ipdata->pfs_fsid, sizeof(ipdata->pfs_fsid)); 1339 ipdata->data_quota = 0; 1340 ipdata->data_count = 0; 1341 ipdata->inode_quota = 0; 1342 ipdata->inode_count = 0; 1343 ipdata->attr_tid = 0; 1344 ipdata->dirent_tid = 0; 1345 bzero(&ipdata->u, sizeof(ipdata->u)); 1346 /* XXX transaction ids */ 1347 } else { 1348 hammer2_chain_delete(trans, chain); 1349 } 1350 1351 /* 1352 * Return the new chain. 1353 */ 1354 hammer2_chain_unlock(chain); 1355 chain = nchain; 1356 } else { 1357 /* 1358 * Return an error 1359 */ 1360 hammer2_chain_unlock(chain); 1361 chain = NULL; 1362 } 1363 1364 /* 1365 * Cleanup, chain/nchain already dealt with. 1366 */ 1367 done: 1368 *chainp = chain; 1369 hammer2_inode_drop(cdip); 1370 1371 return (error); 1372 } 1373 1374 /* 1375 * If (*ochainp) is non-NULL it points to the forward OBJTYPE_HARDLINK 1376 * inode while (*chainp) points to the resolved (hidden hardlink 1377 * target) inode. In this situation when nlinks is 1 we wish to 1378 * deconsolidate the hardlink, moving it back to the directory that now 1379 * represents the only remaining link. 1380 */ 1381 int 1382 hammer2_hardlink_deconsolidate(hammer2_trans_t *trans, 1383 hammer2_inode_t *dip, 1384 hammer2_chain_t **chainp, 1385 hammer2_chain_t **ochainp) 1386 { 1387 if (*ochainp == NULL) 1388 return (0); 1389 /* XXX */ 1390 return (0); 1391 } 1392 1393 /* 1394 * The caller presents a locked *chainp pointing to a HAMMER2_BREF_TYPE_INODE 1395 * with an obj_type of HAMMER2_OBJTYPE_HARDLINK. This routine will gobble 1396 * the *chainp and return a new locked *chainp representing the file target 1397 * (the original *chainp will be unlocked). 1398 * 1399 * When a match is found the chain representing the original HARDLINK 1400 * will be returned in *ochainp with a ref, but not locked. 1401 * 1402 * When no match is found *chainp is set to NULL and EIO is returned. 1403 * (*ochainp) will still be set to the original chain with a ref but not 1404 * locked. 1405 */ 1406 int 1407 hammer2_hardlink_find(hammer2_inode_t *dip, hammer2_chain_t **chainp, 1408 hammer2_chain_t **ochainp) 1409 { 1410 hammer2_chain_t *chain = *chainp; 1411 hammer2_chain_t *parent; 1412 hammer2_inode_t *ip; 1413 hammer2_inode_t *pip; 1414 hammer2_key_t lhc; 1415 1416 pip = dip; 1417 hammer2_inode_ref(pip); /* for loop */ 1418 hammer2_chain_ref(chain); /* for (*ochainp) */ 1419 *ochainp = chain; 1420 1421 /* 1422 * Locate the hardlink. pip is referenced and not locked, 1423 * ipp. 1424 * 1425 * chain is reused. 1426 */ 1427 lhc = chain->data->ipdata.inum; 1428 hammer2_chain_unlock(chain); 1429 chain = NULL; 1430 1431 while ((ip = pip) != NULL) { 1432 parent = hammer2_inode_lock_ex(ip); 1433 hammer2_inode_drop(ip); /* loop */ 1434 KKASSERT(parent->bref.type == HAMMER2_BREF_TYPE_INODE); 1435 chain = hammer2_chain_lookup(&parent, lhc, lhc, 0); 1436 hammer2_chain_lookup_done(parent); /* discard parent */ 1437 if (chain) 1438 break; 1439 pip = ip->pip; /* safe, ip held locked */ 1440 if (pip) 1441 hammer2_inode_ref(pip); /* loop */ 1442 hammer2_inode_unlock_ex(ip, NULL); 1443 } 1444 1445 /* 1446 * chain is locked, ip is locked. Unlock ip, return the locked 1447 * chain. *ipp is already set w/a ref count and not locked. 1448 * 1449 * (parent is already unlocked). 1450 */ 1451 if (ip) 1452 hammer2_inode_unlock_ex(ip, NULL); 1453 *chainp = chain; 1454 if (chain) { 1455 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_INODE); 1456 /* already locked */ 1457 return (0); 1458 } else { 1459 return (EIO); 1460 } 1461 } 1462 1463 /* 1464 * Find the directory common to both fdip and tdip, hold and return 1465 * its inode. 1466 */ 1467 hammer2_inode_t * 1468 hammer2_inode_common_parent(hammer2_inode_t *fdip, hammer2_inode_t *tdip) 1469 { 1470 hammer2_inode_t *scan1; 1471 hammer2_inode_t *scan2; 1472 1473 /* 1474 * We used to have a depth field but it complicated matters too 1475 * much for directory renames. So now its ugly. Check for 1476 * simple cases before giving up and doing it the expensive way. 1477 * 1478 * XXX need a bottom-up topology stability lock 1479 */ 1480 if (fdip == tdip || fdip == tdip->pip) { 1481 hammer2_inode_ref(fdip); 1482 return(fdip); 1483 } 1484 if (fdip->pip == tdip) { 1485 hammer2_inode_ref(tdip); 1486 return(tdip); 1487 } 1488 1489 /* 1490 * XXX not MPSAFE 1491 */ 1492 for (scan1 = fdip; scan1->pmp == fdip->pmp; scan1 = scan1->pip) { 1493 scan2 = tdip; 1494 while (scan2->pmp == tdip->pmp) { 1495 if (scan1 == scan2) { 1496 hammer2_inode_ref(scan1); 1497 return(scan1); 1498 } 1499 scan2 = scan2->pip; 1500 if (scan2 == NULL) 1501 break; 1502 } 1503 } 1504 panic("hammer2_inode_common_parent: no common parent %p %p\n", 1505 fdip, tdip); 1506 /* NOT REACHED */ 1507 return(NULL); 1508 } 1509