1 /* 2 * Copyright (c) 2011-2014 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 #define INODE_DEBUG 0 45 46 RB_GENERATE2(hammer2_inode_tree, hammer2_inode, rbnode, hammer2_inode_cmp, 47 hammer2_tid_t, meta.inum); 48 49 int 50 hammer2_inode_cmp(hammer2_inode_t *ip1, hammer2_inode_t *ip2) 51 { 52 if (ip1->meta.inum < ip2->meta.inum) 53 return(-1); 54 if (ip1->meta.inum > ip2->meta.inum) 55 return(1); 56 return(0); 57 } 58 59 static 60 void 61 hammer2_inode_delayed_sideq(hammer2_inode_t *ip) 62 { 63 hammer2_inode_sideq_t *ipul; 64 hammer2_pfs_t *pmp = ip->pmp; 65 66 if ((ip->flags & HAMMER2_INODE_ONSIDEQ) == 0) { 67 ipul = kmalloc(sizeof(*ipul), pmp->minode, 68 M_WAITOK | M_ZERO); 69 ipul->ip = ip; 70 hammer2_spin_ex(&pmp->list_spin); 71 if ((ip->flags & HAMMER2_INODE_ONSIDEQ) == 0) { 72 hammer2_inode_ref(ip); 73 atomic_set_int(&ip->flags, 74 HAMMER2_INODE_ONSIDEQ); 75 TAILQ_INSERT_TAIL(&pmp->sideq, ipul, entry); 76 hammer2_spin_unex(&pmp->list_spin); 77 } else { 78 hammer2_spin_unex(&pmp->list_spin); 79 kfree(ipul, pmp->minode); 80 } 81 } 82 } 83 84 /* 85 * HAMMER2 inode locks 86 * 87 * HAMMER2 offers shared and exclusive locks on inodes. Pass a mask of 88 * flags for options: 89 * 90 * - pass HAMMER2_RESOLVE_SHARED if a shared lock is desired. The 91 * inode locking function will automatically set the RDONLY flag. 92 * 93 * - pass HAMMER2_RESOLVE_ALWAYS if you need the inode's meta-data. 94 * Most front-end inode locks do. 95 * 96 * - pass HAMMER2_RESOLVE_NEVER if you do not want to require that 97 * the inode data be resolved. This is used by the syncthr because 98 * it can run on an unresolved/out-of-sync cluster, and also by the 99 * vnode reclamation code to avoid unnecessary I/O (particularly when 100 * disposing of hundreds of thousands of cached vnodes). 101 * 102 * The inode locking function locks the inode itself, resolves any stale 103 * chains in the inode's cluster, and allocates a fresh copy of the 104 * cluster with 1 ref and all the underlying chains locked. 105 * 106 * ip->cluster will be stable while the inode is locked. 107 * 108 * NOTE: We don't combine the inode/chain lock because putting away an 109 * inode would otherwise confuse multiple lock holders of the inode. 110 * 111 * NOTE: In-memory inodes always point to hardlink targets (the actual file), 112 * and never point to a hardlink pointer. 113 * 114 * NOTE: If caller passes HAMMER2_RESOLVE_RDONLY the exclusive locking code 115 * will feel free to reduce the chain set in the cluster as an 116 * optimization. It will still be validated against the quorum if 117 * appropriate, but the optimization might be able to reduce data 118 * accesses to one node. This flag is automatically set if the inode 119 * is locked with HAMMER2_RESOLVE_SHARED. 120 */ 121 void 122 hammer2_inode_lock(hammer2_inode_t *ip, int how) 123 { 124 hammer2_inode_ref(ip); 125 126 /* 127 * Inode structure mutex 128 */ 129 if (how & HAMMER2_RESOLVE_SHARED) { 130 /*how |= HAMMER2_RESOLVE_RDONLY; not used */ 131 hammer2_mtx_sh(&ip->lock); 132 } else { 133 hammer2_mtx_ex(&ip->lock); 134 } 135 } 136 137 /* 138 * Select a chain out of an inode's cluster and lock it. 139 * 140 * The inode does not have to be locked. 141 */ 142 hammer2_chain_t * 143 hammer2_inode_chain(hammer2_inode_t *ip, int clindex, int how) 144 { 145 hammer2_chain_t *chain; 146 hammer2_cluster_t *cluster; 147 148 hammer2_spin_sh(&ip->cluster_spin); 149 #if 0 150 cluster = ip->cluster_cache; 151 if (cluster) { 152 if (clindex >= cluster->nchains) 153 chain = NULL; 154 else 155 chain = cluster->array[clindex].chain; 156 if (chain) { 157 hammer2_chain_ref(chain); 158 hammer2_spin_unsh(&ip->cluster_spin); 159 hammer2_chain_lock(chain, how); 160 return chain; 161 } 162 } 163 #endif 164 165 cluster = &ip->cluster; 166 if (clindex >= cluster->nchains) 167 chain = NULL; 168 else 169 chain = cluster->array[clindex].chain; 170 if (chain) { 171 hammer2_chain_ref(chain); 172 hammer2_spin_unsh(&ip->cluster_spin); 173 hammer2_chain_lock(chain, how); 174 } else { 175 hammer2_spin_unsh(&ip->cluster_spin); 176 } 177 return chain; 178 } 179 180 hammer2_chain_t * 181 hammer2_inode_chain_and_parent(hammer2_inode_t *ip, int clindex, 182 hammer2_chain_t **parentp, int how) 183 { 184 hammer2_chain_t *chain; 185 hammer2_chain_t *parent; 186 187 for (;;) { 188 hammer2_spin_sh(&ip->cluster_spin); 189 if (clindex >= ip->cluster.nchains) 190 chain = NULL; 191 else 192 chain = ip->cluster.array[clindex].chain; 193 if (chain) { 194 hammer2_chain_ref(chain); 195 hammer2_spin_unsh(&ip->cluster_spin); 196 hammer2_chain_lock(chain, how); 197 } else { 198 hammer2_spin_unsh(&ip->cluster_spin); 199 } 200 201 /* 202 * Get parent, lock order must be (parent, chain). 203 */ 204 parent = chain->parent; 205 hammer2_chain_ref(parent); 206 hammer2_chain_unlock(chain); 207 hammer2_chain_lock(parent, how); 208 hammer2_chain_lock(chain, how); 209 if (ip->cluster.array[clindex].chain == chain && 210 chain->parent == parent) { 211 break; 212 } 213 214 /* 215 * Retry 216 */ 217 hammer2_chain_unlock(chain); 218 hammer2_chain_drop(chain); 219 hammer2_chain_unlock(parent); 220 hammer2_chain_drop(parent); 221 } 222 *parentp = parent; 223 224 return chain; 225 } 226 227 void 228 hammer2_inode_unlock(hammer2_inode_t *ip) 229 { 230 hammer2_mtx_unlock(&ip->lock); 231 hammer2_inode_drop(ip); 232 } 233 234 /* 235 * Temporarily release a lock held shared or exclusive. Caller must 236 * hold the lock shared or exclusive on call and lock will be released 237 * on return. 238 * 239 * Restore a lock that was temporarily released. 240 */ 241 hammer2_mtx_state_t 242 hammer2_inode_lock_temp_release(hammer2_inode_t *ip) 243 { 244 return hammer2_mtx_temp_release(&ip->lock); 245 } 246 247 void 248 hammer2_inode_lock_temp_restore(hammer2_inode_t *ip, hammer2_mtx_state_t ostate) 249 { 250 hammer2_mtx_temp_restore(&ip->lock, ostate); 251 } 252 253 /* 254 * Upgrade a shared inode lock to exclusive and return. If the inode lock 255 * is already held exclusively this is a NOP. 256 * 257 * The caller MUST hold the inode lock either shared or exclusive on call 258 * and will own the lock exclusively on return. 259 * 260 * Returns non-zero if the lock was already exclusive prior to the upgrade. 261 */ 262 int 263 hammer2_inode_lock_upgrade(hammer2_inode_t *ip) 264 { 265 int wasexclusive; 266 267 if (mtx_islocked_ex(&ip->lock)) { 268 wasexclusive = 1; 269 } else { 270 hammer2_mtx_unlock(&ip->lock); 271 hammer2_mtx_ex(&ip->lock); 272 wasexclusive = 0; 273 } 274 return wasexclusive; 275 } 276 277 /* 278 * Downgrade an inode lock from exclusive to shared only if the inode 279 * lock was previously shared. If the inode lock was previously exclusive, 280 * this is a NOP. 281 */ 282 void 283 hammer2_inode_lock_downgrade(hammer2_inode_t *ip, int wasexclusive) 284 { 285 if (wasexclusive == 0) 286 mtx_downgrade(&ip->lock); 287 } 288 289 /* 290 * Lookup an inode by inode number 291 */ 292 hammer2_inode_t * 293 hammer2_inode_lookup(hammer2_pfs_t *pmp, hammer2_tid_t inum) 294 { 295 hammer2_inode_t *ip; 296 297 KKASSERT(pmp); 298 if (pmp->spmp_hmp) { 299 ip = NULL; 300 } else { 301 hammer2_spin_ex(&pmp->inum_spin); 302 ip = RB_LOOKUP(hammer2_inode_tree, &pmp->inum_tree, inum); 303 if (ip) 304 hammer2_inode_ref(ip); 305 hammer2_spin_unex(&pmp->inum_spin); 306 } 307 return(ip); 308 } 309 310 /* 311 * Adding a ref to an inode is only legal if the inode already has at least 312 * one ref. 313 * 314 * (can be called with spinlock held) 315 */ 316 void 317 hammer2_inode_ref(hammer2_inode_t *ip) 318 { 319 atomic_add_int(&ip->refs, 1); 320 if (hammer2_debug & 0x80000) { 321 kprintf("INODE+1 %p (%d->%d)\n", ip, ip->refs - 1, ip->refs); 322 print_backtrace(8); 323 } 324 } 325 326 /* 327 * Drop an inode reference, freeing the inode when the last reference goes 328 * away. 329 */ 330 void 331 hammer2_inode_drop(hammer2_inode_t *ip) 332 { 333 hammer2_pfs_t *pmp; 334 u_int refs; 335 336 while (ip) { 337 if (hammer2_debug & 0x80000) { 338 kprintf("INODE-1 %p (%d->%d)\n", 339 ip, ip->refs, ip->refs - 1); 340 print_backtrace(8); 341 } 342 refs = ip->refs; 343 cpu_ccfence(); 344 if (refs == 1) { 345 /* 346 * Transition to zero, must interlock with 347 * the inode inumber lookup tree (if applicable). 348 * It should not be possible for anyone to race 349 * the transition to 0. 350 */ 351 pmp = ip->pmp; 352 KKASSERT(pmp); 353 hammer2_spin_ex(&pmp->inum_spin); 354 355 if (atomic_cmpset_int(&ip->refs, 1, 0)) { 356 KKASSERT(hammer2_mtx_refs(&ip->lock) == 0); 357 if (ip->flags & HAMMER2_INODE_ONRBTREE) { 358 atomic_clear_int(&ip->flags, 359 HAMMER2_INODE_ONRBTREE); 360 RB_REMOVE(hammer2_inode_tree, 361 &pmp->inum_tree, ip); 362 } 363 hammer2_spin_unex(&pmp->inum_spin); 364 365 ip->pmp = NULL; 366 367 #if 0 368 /* 369 * Clean out the cluster cache 370 */ 371 hammer2_cluster_t *tmpclu; 372 tmpclu = ip->cluster_cache; 373 if (tmpclu) { 374 ip->cluster_cache = NULL; 375 hammer2_cluster_drop(tmpclu); 376 } 377 #endif 378 379 /* 380 * Cleaning out ip->cluster isn't entirely 381 * trivial. 382 */ 383 hammer2_inode_repoint(ip, NULL, NULL); 384 385 kfree(ip, pmp->minode); 386 atomic_add_long(&pmp->inmem_inodes, -1); 387 ip = NULL; /* will terminate loop */ 388 } else { 389 hammer2_spin_unex(&ip->pmp->inum_spin); 390 } 391 } else { 392 /* 393 * Non zero transition 394 */ 395 if (atomic_cmpset_int(&ip->refs, refs, refs - 1)) 396 break; 397 } 398 } 399 } 400 401 /* 402 * Get the vnode associated with the given inode, allocating the vnode if 403 * necessary. The vnode will be returned exclusively locked. 404 * 405 * The caller must lock the inode (shared or exclusive). 406 * 407 * Great care must be taken to avoid deadlocks and vnode acquisition/reclaim 408 * races. 409 */ 410 struct vnode * 411 hammer2_igetv(hammer2_inode_t *ip, int *errorp) 412 { 413 hammer2_pfs_t *pmp; 414 struct vnode *vp; 415 416 pmp = ip->pmp; 417 KKASSERT(pmp != NULL); 418 *errorp = 0; 419 420 for (;;) { 421 /* 422 * Attempt to reuse an existing vnode assignment. It is 423 * possible to race a reclaim so the vget() may fail. The 424 * inode must be unlocked during the vget() to avoid a 425 * deadlock against a reclaim. 426 */ 427 int wasexclusive; 428 429 vp = ip->vp; 430 if (vp) { 431 /* 432 * Inode must be unlocked during the vget() to avoid 433 * possible deadlocks, but leave the ip ref intact. 434 * 435 * vnode is held to prevent destruction during the 436 * vget(). The vget() can still fail if we lost 437 * a reclaim race on the vnode. 438 */ 439 hammer2_mtx_state_t ostate; 440 441 vhold(vp); 442 ostate = hammer2_inode_lock_temp_release(ip); 443 if (vget(vp, LK_EXCLUSIVE)) { 444 vdrop(vp); 445 hammer2_inode_lock_temp_restore(ip, ostate); 446 continue; 447 } 448 hammer2_inode_lock_temp_restore(ip, ostate); 449 vdrop(vp); 450 /* vp still locked and ref from vget */ 451 if (ip->vp != vp) { 452 kprintf("hammer2: igetv race %p/%p\n", 453 ip->vp, vp); 454 vput(vp); 455 continue; 456 } 457 *errorp = 0; 458 break; 459 } 460 461 /* 462 * No vnode exists, allocate a new vnode. Beware of 463 * allocation races. This function will return an 464 * exclusively locked and referenced vnode. 465 */ 466 *errorp = getnewvnode(VT_HAMMER2, pmp->mp, &vp, 0, 0); 467 if (*errorp) { 468 kprintf("hammer2: igetv getnewvnode failed %d\n", 469 *errorp); 470 vp = NULL; 471 break; 472 } 473 474 /* 475 * Lock the inode and check for an allocation race. 476 */ 477 wasexclusive = hammer2_inode_lock_upgrade(ip); 478 if (ip->vp != NULL) { 479 vp->v_type = VBAD; 480 vx_put(vp); 481 hammer2_inode_lock_downgrade(ip, wasexclusive); 482 continue; 483 } 484 485 switch (ip->meta.type) { 486 case HAMMER2_OBJTYPE_DIRECTORY: 487 vp->v_type = VDIR; 488 break; 489 case HAMMER2_OBJTYPE_REGFILE: 490 vp->v_type = VREG; 491 vinitvmio(vp, ip->meta.size, 492 HAMMER2_LBUFSIZE, 493 (int)ip->meta.size & HAMMER2_LBUFMASK); 494 break; 495 case HAMMER2_OBJTYPE_SOFTLINK: 496 /* 497 * XXX for now we are using the generic file_read 498 * and file_write code so we need a buffer cache 499 * association. 500 */ 501 vp->v_type = VLNK; 502 vinitvmio(vp, ip->meta.size, 503 HAMMER2_LBUFSIZE, 504 (int)ip->meta.size & HAMMER2_LBUFMASK); 505 break; 506 case HAMMER2_OBJTYPE_CDEV: 507 vp->v_type = VCHR; 508 /* fall through */ 509 case HAMMER2_OBJTYPE_BDEV: 510 vp->v_ops = &pmp->mp->mnt_vn_spec_ops; 511 if (ip->meta.type != HAMMER2_OBJTYPE_CDEV) 512 vp->v_type = VBLK; 513 addaliasu(vp, 514 ip->meta.rmajor, 515 ip->meta.rminor); 516 break; 517 case HAMMER2_OBJTYPE_FIFO: 518 vp->v_type = VFIFO; 519 vp->v_ops = &pmp->mp->mnt_vn_fifo_ops; 520 break; 521 case HAMMER2_OBJTYPE_SOCKET: 522 vp->v_type = VSOCK; 523 break; 524 default: 525 panic("hammer2: unhandled objtype %d", 526 ip->meta.type); 527 break; 528 } 529 530 if (ip == pmp->iroot) 531 vsetflags(vp, VROOT); 532 533 vp->v_data = ip; 534 ip->vp = vp; 535 hammer2_inode_ref(ip); /* vp association */ 536 hammer2_inode_lock_downgrade(ip, wasexclusive); 537 break; 538 } 539 540 /* 541 * Return non-NULL vp and *errorp == 0, or NULL vp and *errorp != 0. 542 */ 543 if (hammer2_debug & 0x0002) { 544 kprintf("igetv vp %p refs 0x%08x aux 0x%08x\n", 545 vp, vp->v_refcnt, vp->v_auxrefs); 546 } 547 return (vp); 548 } 549 550 /* 551 * Returns the inode associated with the passed-in cluster, creating the 552 * inode if necessary and synchronizing it to the passed-in cluster otherwise. 553 * When synchronizing, if idx >= 0, only cluster index (idx) is synchronized. 554 * Otherwise the whole cluster is synchronized. 555 * 556 * The passed-in cluster must be locked and will remain locked on return. 557 * The returned inode will be locked and the caller may dispose of both 558 * via hammer2_inode_unlock() + hammer2_inode_drop(). However, if the caller 559 * needs to resolve a hardlink it must ref/unlock/relock/drop the inode. 560 * 561 * The hammer2_inode structure regulates the interface between the high level 562 * kernel VNOPS API and the filesystem backend (the chains). 563 * 564 * On return the inode is locked with the supplied cluster. 565 */ 566 hammer2_inode_t * 567 hammer2_inode_get(hammer2_pfs_t *pmp, hammer2_inode_t *dip, 568 hammer2_cluster_t *cluster, int idx) 569 { 570 hammer2_inode_t *nip; 571 const hammer2_inode_data_t *iptmp; 572 const hammer2_inode_data_t *nipdata; 573 574 KKASSERT(cluster == NULL || 575 hammer2_cluster_type(cluster) == HAMMER2_BREF_TYPE_INODE); 576 KKASSERT(pmp); 577 578 /* 579 * Interlocked lookup/ref of the inode. This code is only needed 580 * when looking up inodes with nlinks != 0 (TODO: optimize out 581 * otherwise and test for duplicates). 582 * 583 * Cluster can be NULL during the initial pfs allocation. 584 */ 585 again: 586 while (cluster) { 587 iptmp = &hammer2_cluster_rdata(cluster)->ipdata; 588 nip = hammer2_inode_lookup(pmp, iptmp->meta.inum); 589 if (nip == NULL) 590 break; 591 592 hammer2_mtx_ex(&nip->lock); 593 594 /* 595 * Handle SMP race (not applicable to the super-root spmp 596 * which can't index inodes due to duplicative inode numbers). 597 */ 598 if (pmp->spmp_hmp == NULL && 599 (nip->flags & HAMMER2_INODE_ONRBTREE) == 0) { 600 hammer2_mtx_unlock(&nip->lock); 601 hammer2_inode_drop(nip); 602 continue; 603 } 604 if (idx >= 0) 605 hammer2_inode_repoint_one(nip, cluster, idx); 606 else 607 hammer2_inode_repoint(nip, NULL, cluster); 608 609 return nip; 610 } 611 612 /* 613 * We couldn't find the inode number, create a new inode. 614 */ 615 nip = kmalloc(sizeof(*nip), pmp->minode, M_WAITOK | M_ZERO); 616 spin_init(&nip->cluster_spin, "h2clspin"); 617 atomic_add_long(&pmp->inmem_inodes, 1); 618 hammer2_pfs_memory_inc(pmp); 619 hammer2_pfs_memory_wakeup(pmp); 620 if (pmp->spmp_hmp) 621 nip->flags = HAMMER2_INODE_SROOT; 622 623 /* 624 * Initialize nip's cluster. A cluster is provided for normal 625 * inodes but typically not for the super-root or PFS inodes. 626 */ 627 nip->cluster.refs = 1; 628 nip->cluster.pmp = pmp; 629 nip->cluster.flags |= HAMMER2_CLUSTER_INODE; 630 if (cluster) { 631 nipdata = &hammer2_cluster_rdata(cluster)->ipdata; 632 nip->meta = nipdata->meta; 633 atomic_set_int(&nip->flags, HAMMER2_INODE_METAGOOD); 634 hammer2_inode_repoint(nip, NULL, cluster); 635 } else { 636 nip->meta.inum = 1; /* PFS inum is always 1 XXX */ 637 /* mtime will be updated when a cluster is available */ 638 atomic_set_int(&nip->flags, HAMMER2_INODE_METAGOOD);/*XXX*/ 639 } 640 641 nip->pmp = pmp; 642 643 /* 644 * ref and lock on nip gives it state compatible to after a 645 * hammer2_inode_lock() call. 646 */ 647 nip->refs = 1; 648 hammer2_mtx_init(&nip->lock, "h2inode"); 649 hammer2_mtx_ex(&nip->lock); 650 /* combination of thread lock and chain lock == inode lock */ 651 652 /* 653 * Attempt to add the inode. If it fails we raced another inode 654 * get. Undo all the work and try again. 655 */ 656 if (pmp->spmp_hmp == NULL) { 657 hammer2_spin_ex(&pmp->inum_spin); 658 if (RB_INSERT(hammer2_inode_tree, &pmp->inum_tree, nip)) { 659 hammer2_spin_unex(&pmp->inum_spin); 660 hammer2_mtx_unlock(&nip->lock); 661 hammer2_inode_drop(nip); 662 goto again; 663 } 664 atomic_set_int(&nip->flags, HAMMER2_INODE_ONRBTREE); 665 hammer2_spin_unex(&pmp->inum_spin); 666 } 667 668 return (nip); 669 } 670 671 /* 672 * Create a new inode in the specified directory using the vattr to 673 * figure out the type. A non-zero type field overrides vattr. 674 * 675 * If no error occurs the new inode with its cluster locked is returned. 676 * However, when creating an OBJTYPE_HARDLINK, the caller can assume 677 * that NULL will be returned (that is, the caller already has the inode 678 * in-hand and is creating a hardlink to it, we do not need to return a 679 * representitive ip). 680 * 681 * If vap and/or cred are NULL the related fields are not set and the 682 * inode type defaults to a directory. This is used when creating PFSs 683 * under the super-root, so the inode number is set to 1 in this case. 684 * 685 * dip is not locked on entry. 686 * 687 * NOTE: This function is used to create all manners of inodes, including 688 * super-root entries for snapshots and PFSs. When used to create a 689 * snapshot the inode will be temporarily associated with the spmp. 690 * 691 * NOTE: When creating a normal file or directory the caller must call this 692 * function twice, once to create the actual inode and once to create 693 * the hardlink representing the directory entry. This function is 694 * only called once when creating a softlink. The softlink itself. 695 * 696 * NOTE: When creating a hardlink target (a real inode), name/name_len is 697 * passed as NULL/0, and caller should pass lhc as inum. 698 */ 699 hammer2_inode_t * 700 hammer2_inode_create(hammer2_inode_t *dip, hammer2_inode_t *pip, 701 struct vattr *vap, struct ucred *cred, 702 const uint8_t *name, size_t name_len, hammer2_key_t lhc, 703 hammer2_key_t inum, 704 uint8_t type, uint8_t target_type, 705 int flags, int *errorp) 706 { 707 hammer2_xop_create_t *xop; 708 hammer2_inode_t *nip; 709 int error; 710 uid_t xuid; 711 uuid_t pip_uid; 712 uuid_t pip_gid; 713 uint32_t pip_mode; 714 uint8_t pip_comp_algo; 715 uint8_t pip_check_algo; 716 hammer2_tid_t pip_inum; 717 718 if (name) 719 lhc = hammer2_dirhash(name, name_len); 720 *errorp = 0; 721 nip = NULL; 722 723 /* 724 * Locate the inode or indirect block to create the new 725 * entry in. At the same time check for key collisions 726 * and iterate until we don't get one. 727 * 728 * Lock the directory exclusively for now to guarantee that 729 * we can find an unused lhc for the name. Due to collisions, 730 * two different creates can end up with the same lhc so we 731 * cannot depend on the OS to prevent the collision. 732 */ 733 hammer2_inode_lock(dip, 0); 734 735 pip_uid = pip->meta.uid; 736 pip_gid = pip->meta.gid; 737 pip_mode = pip->meta.mode; 738 pip_comp_algo = pip->meta.comp_algo; 739 pip_check_algo = pip->meta.check_algo; 740 pip_inum = (pip == pip->pmp->iroot) ? 0 : pip->meta.inum; 741 742 /* 743 * If name specified, locate an unused key in the collision space. 744 * Otherwise use the passed-in lhc directly. 745 */ 746 if (name) { 747 hammer2_xop_scanlhc_t *sxop; 748 hammer2_key_t lhcbase; 749 750 lhcbase = lhc; 751 sxop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING); 752 sxop->lhc = lhc; 753 hammer2_xop_start(&sxop->head, hammer2_xop_scanlhc); 754 while ((error = hammer2_xop_collect(&sxop->head, 0)) == 0) { 755 if (lhc != sxop->head.cluster.focus->bref.key) 756 break; 757 ++lhc; 758 } 759 hammer2_xop_retire(&sxop->head, HAMMER2_XOPMASK_VOP); 760 761 if (error) { 762 if (error != ENOENT) 763 goto done2; 764 ++lhc; 765 error = 0; 766 } 767 if ((lhcbase ^ lhc) & ~HAMMER2_DIRHASH_LOMASK) { 768 error = ENOSPC; 769 goto done2; 770 } 771 } 772 773 /* 774 * Create the inode with the lhc as the key. 775 */ 776 xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING); 777 xop->lhc = lhc; 778 xop->flags = flags; 779 bzero(&xop->meta, sizeof(xop->meta)); 780 781 if (vap) { 782 xop->meta.type = hammer2_get_obj_type(vap->va_type); 783 784 switch (xop->meta.type) { 785 case HAMMER2_OBJTYPE_CDEV: 786 case HAMMER2_OBJTYPE_BDEV: 787 xop->meta.rmajor = vap->va_rmajor; 788 xop->meta.rminor = vap->va_rminor; 789 break; 790 default: 791 break; 792 } 793 type = xop->meta.type; 794 } else { 795 xop->meta.type = type; 796 xop->meta.target_type = target_type; 797 } 798 xop->meta.inum = inum; 799 xop->meta.iparent = pip_inum; 800 801 /* Inherit parent's inode compression mode. */ 802 xop->meta.comp_algo = pip_comp_algo; 803 xop->meta.check_algo = pip_check_algo; 804 xop->meta.version = HAMMER2_INODE_VERSION_ONE; 805 hammer2_update_time(&xop->meta.ctime); 806 xop->meta.mtime = xop->meta.ctime; 807 if (vap) 808 xop->meta.mode = vap->va_mode; 809 xop->meta.nlinks = 1; 810 if (vap) { 811 if (dip->pmp) { 812 xuid = hammer2_to_unix_xid(&pip_uid); 813 xuid = vop_helper_create_uid(dip->pmp->mp, 814 pip_mode, 815 xuid, 816 cred, 817 &vap->va_mode); 818 } else { 819 /* super-root has no dip and/or pmp */ 820 xuid = 0; 821 } 822 if (vap->va_vaflags & VA_UID_UUID_VALID) 823 xop->meta.uid = vap->va_uid_uuid; 824 else if (vap->va_uid != (uid_t)VNOVAL) 825 hammer2_guid_to_uuid(&xop->meta.uid, vap->va_uid); 826 else 827 hammer2_guid_to_uuid(&xop->meta.uid, xuid); 828 829 if (vap->va_vaflags & VA_GID_UUID_VALID) 830 xop->meta.gid = vap->va_gid_uuid; 831 else if (vap->va_gid != (gid_t)VNOVAL) 832 hammer2_guid_to_uuid(&xop->meta.gid, vap->va_gid); 833 else 834 xop->meta.gid = pip_gid; 835 } 836 837 /* 838 * Regular files and softlinks allow a small amount of data to be 839 * directly embedded in the inode. This flag will be cleared if 840 * the size is extended past the embedded limit. 841 */ 842 if (xop->meta.type == HAMMER2_OBJTYPE_REGFILE || 843 xop->meta.type == HAMMER2_OBJTYPE_SOFTLINK || 844 xop->meta.type == HAMMER2_OBJTYPE_HARDLINK) { 845 xop->meta.op_flags |= HAMMER2_OPFLAG_DIRECTDATA; 846 } 847 if (name) { 848 hammer2_xop_setname(&xop->head, name, name_len); 849 } else { 850 name_len = hammer2_xop_setname_inum(&xop->head, inum); 851 KKASSERT(lhc == inum); 852 } 853 xop->meta.name_len = name_len; 854 xop->meta.name_key = lhc; 855 KKASSERT(name_len < HAMMER2_INODE_MAXNAME); 856 857 hammer2_xop_start(&xop->head, hammer2_inode_xop_create); 858 859 error = hammer2_xop_collect(&xop->head, 0); 860 #if INODE_DEBUG 861 kprintf("CREATE INODE %*.*s\n", 862 (int)name_len, (int)name_len, name); 863 #endif 864 865 if (error) { 866 *errorp = error; 867 goto done; 868 } 869 870 /* 871 * Set up the new inode if not a hardlink pointer. 872 * 873 * NOTE: *_get() integrates chain's lock into the inode lock. 874 * 875 * NOTE: Only one new inode can currently be created per 876 * transaction. If the need arises we can adjust 877 * hammer2_trans_init() to allow more. 878 * 879 * NOTE: nipdata will have chain's blockset data. 880 */ 881 if (type != HAMMER2_OBJTYPE_HARDLINK) { 882 nip = hammer2_inode_get(dip->pmp, dip, &xop->head.cluster, -1); 883 nip->comp_heuristic = 0; 884 } else { 885 nip = NULL; 886 } 887 888 done: 889 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); 890 done2: 891 hammer2_inode_unlock(dip); 892 893 return (nip); 894 } 895 896 /* 897 * Repoint ip->cluster's chains to cluster's chains and fixup the default 898 * focus. All items, valid or invalid, are repointed. hammer2_xop_start() 899 * filters out invalid or non-matching elements. 900 * 901 * Caller must hold the inode and cluster exclusive locked, if not NULL, 902 * must also be locked. 903 * 904 * Cluster may be NULL to clean out any chains in ip->cluster. 905 */ 906 void 907 hammer2_inode_repoint(hammer2_inode_t *ip, hammer2_inode_t *pip, 908 hammer2_cluster_t *cluster) 909 { 910 hammer2_chain_t *dropch[HAMMER2_MAXCLUSTER]; 911 hammer2_chain_t *ochain; 912 hammer2_chain_t *nchain; 913 int i; 914 915 bzero(dropch, sizeof(dropch)); 916 917 /* 918 * Replace chains in ip->cluster with chains from cluster and 919 * adjust the focus if necessary. 920 * 921 * NOTE: nchain and/or ochain can be NULL due to gaps 922 * in the cluster arrays. 923 */ 924 hammer2_spin_ex(&ip->cluster_spin); 925 for (i = 0; cluster && i < cluster->nchains; ++i) { 926 /* 927 * Do not replace elements which are the same. Also handle 928 * element count discrepancies. 929 */ 930 nchain = cluster->array[i].chain; 931 if (i < ip->cluster.nchains) { 932 ochain = ip->cluster.array[i].chain; 933 if (ochain == nchain) 934 continue; 935 } else { 936 ochain = NULL; 937 } 938 939 /* 940 * Make adjustments 941 */ 942 ip->cluster.array[i].chain = nchain; 943 ip->cluster.array[i].flags &= ~HAMMER2_CITEM_INVALID; 944 ip->cluster.array[i].flags |= cluster->array[i].flags & 945 HAMMER2_CITEM_INVALID; 946 if (nchain) 947 hammer2_chain_ref(nchain); 948 dropch[i] = ochain; 949 } 950 951 /* 952 * Release any left-over chains in ip->cluster. 953 */ 954 while (i < ip->cluster.nchains) { 955 nchain = ip->cluster.array[i].chain; 956 if (nchain) { 957 ip->cluster.array[i].chain = NULL; 958 ip->cluster.array[i].flags |= HAMMER2_CITEM_INVALID; 959 } 960 dropch[i] = nchain; 961 ++i; 962 } 963 964 /* 965 * Fixup fields. Note that the inode-embedded cluster is never 966 * directly locked. 967 */ 968 if (cluster) { 969 ip->cluster.nchains = cluster->nchains; 970 ip->cluster.focus = cluster->focus; 971 ip->cluster.flags = cluster->flags & ~HAMMER2_CLUSTER_LOCKED; 972 } else { 973 ip->cluster.nchains = 0; 974 ip->cluster.focus = NULL; 975 ip->cluster.flags &= ~HAMMER2_CLUSTER_ZFLAGS; 976 } 977 978 hammer2_spin_unex(&ip->cluster_spin); 979 980 /* 981 * Cleanup outside of spinlock 982 */ 983 while (--i >= 0) { 984 if (dropch[i]) 985 hammer2_chain_drop(dropch[i]); 986 } 987 } 988 989 /* 990 * Repoint a single element from the cluster to the ip. Used by the 991 * synchronization threads to piecemeal update inodes. Does not change 992 * focus and requires inode to be re-locked to clean-up flags (XXX). 993 */ 994 void 995 hammer2_inode_repoint_one(hammer2_inode_t *ip, hammer2_cluster_t *cluster, 996 int idx) 997 { 998 hammer2_chain_t *ochain; 999 hammer2_chain_t *nchain; 1000 int i; 1001 1002 hammer2_spin_ex(&ip->cluster_spin); 1003 KKASSERT(idx < cluster->nchains); 1004 if (idx < ip->cluster.nchains) { 1005 ochain = ip->cluster.array[idx].chain; 1006 nchain = cluster->array[idx].chain; 1007 } else { 1008 ochain = NULL; 1009 nchain = cluster->array[idx].chain; 1010 ip->cluster.nchains = idx + 1; 1011 for (i = ip->cluster.nchains; i <= idx; ++i) { 1012 bzero(&ip->cluster.array[i], 1013 sizeof(ip->cluster.array[i])); 1014 ip->cluster.array[i].flags |= HAMMER2_CITEM_INVALID; 1015 } 1016 } 1017 if (ochain != nchain) { 1018 /* 1019 * Make adjustments. 1020 */ 1021 ip->cluster.array[idx].chain = nchain; 1022 ip->cluster.array[idx].flags &= ~HAMMER2_CITEM_INVALID; 1023 ip->cluster.array[idx].flags |= cluster->array[idx].flags & 1024 HAMMER2_CITEM_INVALID; 1025 } 1026 hammer2_spin_unex(&ip->cluster_spin); 1027 if (ochain != nchain) { 1028 if (nchain) 1029 hammer2_chain_ref(nchain); 1030 if (ochain) 1031 hammer2_chain_drop(ochain); 1032 } 1033 } 1034 1035 /* 1036 * Called with a locked inode to finish unlinking an inode after xop_unlink 1037 * had been run. This function is responsible for decrementing nlinks. 1038 * 1039 * We don't bother decrementing nlinks if the file is not open and this was 1040 * the last link. 1041 * 1042 * If the inode is a hardlink target it's chain has not yet been deleted, 1043 * otherwise it's chain has been deleted. 1044 * 1045 * If isopen then any prior deletion was not permanent and the inode is 1046 * left intact with nlinks == 0; 1047 */ 1048 int 1049 hammer2_inode_unlink_finisher(hammer2_inode_t *ip, int isopen) 1050 { 1051 hammer2_pfs_t *pmp; 1052 int error; 1053 1054 pmp = ip->pmp; 1055 1056 /* 1057 * Decrement nlinks. If this is the last link and the file is 1058 * not open we can just delete the inode and not bother dropping 1059 * nlinks to 0 (avoiding unnecessary block updates). 1060 */ 1061 if (ip->meta.nlinks == 1) { 1062 atomic_set_int(&ip->flags, HAMMER2_INODE_ISUNLINKED); 1063 if (isopen == 0) 1064 goto killit; 1065 } 1066 1067 hammer2_inode_modify(ip); 1068 --ip->meta.nlinks; 1069 if ((int64_t)ip->meta.nlinks < 0) 1070 ip->meta.nlinks = 0; /* safety */ 1071 1072 /* 1073 * If nlinks is not zero we are done. However, this should only be 1074 * possible with a hardlink target. If the inode is an embedded 1075 * hardlink nlinks should have dropped to zero, warn and proceed 1076 * with the next step. 1077 */ 1078 if (ip->meta.nlinks) { 1079 if ((ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE) == 0) 1080 return 0; 1081 kprintf("hammer2_inode_unlink: nlinks was not 0 (%jd)\n", 1082 (intmax_t)ip->meta.nlinks); 1083 return 0; 1084 } 1085 1086 /* 1087 * nlinks is now zero, delete the inode if not open. 1088 */ 1089 if (isopen == 0) { 1090 hammer2_xop_destroy_t *xop; 1091 1092 killit: 1093 atomic_set_int(&ip->flags, HAMMER2_INODE_ISDELETED); 1094 xop = hammer2_xop_alloc(ip, HAMMER2_XOP_MODIFYING); 1095 hammer2_xop_start(&xop->head, hammer2_inode_xop_destroy); 1096 error = hammer2_xop_collect(&xop->head, 0); 1097 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); 1098 } 1099 error = 0; 1100 return error; 1101 } 1102 1103 /* 1104 * Mark an inode as being modified, meaning that the caller will modify 1105 * ip->meta. 1106 * 1107 * If a vnode is present we set the vnode dirty and the nominal filesystem 1108 * sync will also handle synchronizing the inode meta-data. If no vnode 1109 * is present we must ensure that the inode is on pmp->sideq. 1110 * 1111 * NOTE: No mtid (modify_tid) is passed into this routine. The caller is 1112 * only modifying the in-memory inode. A modify_tid is synchronized 1113 * later when the inode gets flushed. 1114 */ 1115 void 1116 hammer2_inode_modify(hammer2_inode_t *ip) 1117 { 1118 hammer2_pfs_t *pmp; 1119 1120 atomic_set_int(&ip->flags, HAMMER2_INODE_MODIFIED); 1121 if (ip->vp) { 1122 vsetisdirty(ip->vp); 1123 } else if ((pmp = ip->pmp) != NULL) { 1124 hammer2_inode_delayed_sideq(ip); 1125 } 1126 } 1127 1128 /* 1129 * Synchronize the inode's frontend state with the chain state prior 1130 * to any explicit flush of the inode or any strategy write call. 1131 * 1132 * Called with a locked inode inside a transaction. 1133 */ 1134 void 1135 hammer2_inode_chain_sync(hammer2_inode_t *ip) 1136 { 1137 if (ip->flags & (HAMMER2_INODE_RESIZED | HAMMER2_INODE_MODIFIED)) { 1138 hammer2_xop_fsync_t *xop; 1139 int error; 1140 1141 xop = hammer2_xop_alloc(ip, HAMMER2_XOP_MODIFYING); 1142 xop->clear_directdata = 0; 1143 if (ip->flags & HAMMER2_INODE_RESIZED) { 1144 if ((ip->meta.op_flags & HAMMER2_OPFLAG_DIRECTDATA) && 1145 ip->meta.size > HAMMER2_EMBEDDED_BYTES) { 1146 ip->meta.op_flags &= ~HAMMER2_OPFLAG_DIRECTDATA; 1147 xop->clear_directdata = 1; 1148 } 1149 xop->osize = ip->osize; 1150 } else { 1151 xop->osize = ip->meta.size; /* safety */ 1152 } 1153 xop->ipflags = ip->flags; 1154 xop->meta = ip->meta; 1155 1156 atomic_clear_int(&ip->flags, HAMMER2_INODE_RESIZED | 1157 HAMMER2_INODE_MODIFIED); 1158 hammer2_xop_start(&xop->head, hammer2_inode_xop_chain_sync); 1159 error = hammer2_xop_collect(&xop->head, 0); 1160 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); 1161 if (error == ENOENT) 1162 error = 0; 1163 if (error) { 1164 kprintf("hammer2: unable to fsync inode %p\n", ip); 1165 /* 1166 atomic_set_int(&ip->flags, 1167 xop->ipflags & (HAMMER2_INODE_RESIZED | 1168 HAMMER2_INODE_MODIFIED)); 1169 */ 1170 /* XXX return error somehow? */ 1171 } 1172 } 1173 } 1174 1175 /* 1176 * The normal filesystem sync no longer has visibility to an inode structure 1177 * after its vnode has been reclaimed. In this situation an unlinked-but-open 1178 * inode or a dirty inode may require additional processing to synchronize 1179 * ip->meta to its underlying cluster nodes. 1180 * 1181 * In particular, reclaims can occur in almost any state (for example, when 1182 * doing operations on unrelated vnodes) and flushing the reclaimed inode 1183 * in the reclaim path itself is a non-starter. 1184 * 1185 * Caller must be in a transaction. 1186 */ 1187 void 1188 hammer2_inode_run_sideq(hammer2_pfs_t *pmp) 1189 { 1190 hammer2_xop_destroy_t *xop; 1191 hammer2_inode_sideq_t *ipul; 1192 hammer2_inode_t *ip; 1193 int error; 1194 1195 if (TAILQ_EMPTY(&pmp->sideq)) 1196 return; 1197 1198 LOCKSTART; 1199 hammer2_spin_ex(&pmp->list_spin); 1200 while ((ipul = TAILQ_FIRST(&pmp->sideq)) != NULL) { 1201 TAILQ_REMOVE(&pmp->sideq, ipul, entry); 1202 ip = ipul->ip; 1203 KKASSERT(ip->flags & HAMMER2_INODE_ONSIDEQ); 1204 atomic_clear_int(&ip->flags, HAMMER2_INODE_ONSIDEQ); 1205 hammer2_spin_unex(&pmp->list_spin); 1206 kfree(ipul, pmp->minode); 1207 1208 hammer2_inode_lock(ip, 0); 1209 if (ip->flags & HAMMER2_INODE_ISUNLINKED) { 1210 /* 1211 * The inode was unlinked while open. The inode must 1212 * be deleted and destroyed. 1213 */ 1214 xop = hammer2_xop_alloc(ip, HAMMER2_XOP_MODIFYING); 1215 hammer2_xop_start(&xop->head, 1216 hammer2_inode_xop_destroy); 1217 error = hammer2_xop_collect(&xop->head, 0); 1218 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP); 1219 } else { 1220 /* 1221 * The inode was dirty as-of the reclaim, requiring 1222 * synchronization of ip->meta with its underlying 1223 * chains. 1224 */ 1225 hammer2_inode_chain_sync(ip); 1226 } 1227 1228 hammer2_inode_unlock(ip); 1229 hammer2_inode_drop(ip); /* ipul ref */ 1230 1231 hammer2_spin_ex(&pmp->list_spin); 1232 } 1233 hammer2_spin_unex(&pmp->list_spin); 1234 LOCKSTOP; 1235 } 1236 1237 /* 1238 * Inode create helper (threaded, backend) 1239 * 1240 * Used by ncreate, nmknod, nsymlink, nmkdir. 1241 * Used by nlink and rename to create HARDLINK pointers. 1242 * 1243 * Frontend holds the parent directory ip locked exclusively. We 1244 * create the inode and feed the exclusively locked chain to the 1245 * frontend. 1246 */ 1247 void 1248 hammer2_inode_xop_create(hammer2_xop_t *arg, int clindex) 1249 { 1250 hammer2_xop_create_t *xop = &arg->xop_create; 1251 hammer2_chain_t *parent; 1252 hammer2_chain_t *chain; 1253 hammer2_key_t key_next; 1254 int cache_index = -1; 1255 int error; 1256 1257 if (hammer2_debug & 0x0001) 1258 kprintf("inode_create lhc %016jx clindex %d\n", 1259 xop->lhc, clindex); 1260 1261 parent = hammer2_inode_chain(xop->head.ip1, clindex, 1262 HAMMER2_RESOLVE_ALWAYS); 1263 if (parent == NULL) { 1264 error = EIO; 1265 chain = NULL; 1266 goto fail; 1267 } 1268 chain = hammer2_chain_lookup(&parent, &key_next, 1269 xop->lhc, xop->lhc, 1270 &cache_index, 0); 1271 if (chain) { 1272 error = EEXIST; 1273 goto fail; 1274 } 1275 1276 error = hammer2_chain_create(&parent, &chain, 1277 xop->head.ip1->pmp, HAMMER2_METH_DEFAULT, 1278 xop->lhc, 0, 1279 HAMMER2_BREF_TYPE_INODE, 1280 HAMMER2_INODE_BYTES, 1281 xop->head.mtid, 0, xop->flags); 1282 if (error == 0) { 1283 hammer2_chain_modify(chain, xop->head.mtid, 0, 0); 1284 chain->data->ipdata.meta = xop->meta; 1285 if (xop->head.name1) { 1286 bcopy(xop->head.name1, 1287 chain->data->ipdata.filename, 1288 xop->head.name1_len); 1289 chain->data->ipdata.meta.name_len = xop->head.name1_len; 1290 } 1291 chain->data->ipdata.meta.name_key = xop->lhc; 1292 } 1293 fail: 1294 if (parent) { 1295 hammer2_chain_unlock(parent); 1296 hammer2_chain_drop(parent); 1297 } 1298 hammer2_xop_feed(&xop->head, chain, clindex, error); 1299 if (chain) { 1300 hammer2_chain_unlock(chain); 1301 hammer2_chain_drop(chain); 1302 } 1303 } 1304 1305 /* 1306 * Inode delete helper (backend, threaded) 1307 * 1308 * Generally used by hammer2_run_sideq() 1309 */ 1310 void 1311 hammer2_inode_xop_destroy(hammer2_xop_t *arg, int clindex) 1312 { 1313 hammer2_xop_destroy_t *xop = &arg->xop_destroy; 1314 hammer2_pfs_t *pmp; 1315 hammer2_chain_t *parent; 1316 hammer2_chain_t *chain; 1317 hammer2_inode_t *ip; 1318 int error; 1319 1320 /* 1321 * We need the precise parent chain to issue the deletion. 1322 */ 1323 ip = xop->head.ip1; 1324 pmp = ip->pmp; 1325 chain = NULL; 1326 1327 again: 1328 parent = hammer2_inode_chain(ip, clindex, HAMMER2_RESOLVE_ALWAYS); 1329 if (parent) 1330 hammer2_chain_getparent(&parent, HAMMER2_RESOLVE_ALWAYS); 1331 if (parent == NULL) { 1332 error = EIO; 1333 goto done; 1334 } 1335 chain = hammer2_inode_chain(ip, clindex, HAMMER2_RESOLVE_ALWAYS); 1336 if (chain == NULL) { 1337 error = EIO; 1338 goto done; 1339 } 1340 if (chain->parent != parent) { 1341 kprintf("hammer2_inode_xop_destroy: " 1342 "parent changed %p->(%p,%p)\n", 1343 chain, parent, chain->parent); 1344 hammer2_chain_unlock(parent); 1345 hammer2_chain_drop(parent); 1346 hammer2_chain_unlock(chain); 1347 hammer2_chain_drop(chain); 1348 goto again; 1349 } 1350 1351 hammer2_chain_delete(parent, chain, xop->head.mtid, 0); 1352 error = 0; 1353 done: 1354 hammer2_xop_feed(&xop->head, NULL, clindex, error); 1355 if (parent) { 1356 hammer2_chain_unlock(parent); 1357 hammer2_chain_drop(parent); 1358 } 1359 if (chain) { 1360 hammer2_chain_unlock(chain); 1361 hammer2_chain_drop(chain); 1362 } 1363 } 1364 1365 void 1366 hammer2_inode_xop_unlinkall(hammer2_xop_t *arg, int clindex) 1367 { 1368 hammer2_xop_unlinkall_t *xop = &arg->xop_unlinkall; 1369 hammer2_chain_t *parent; 1370 hammer2_chain_t *chain; 1371 hammer2_key_t key_next; 1372 int cache_index = -1; 1373 1374 /* 1375 * We need the precise parent chain to issue the deletion. 1376 */ 1377 parent = hammer2_inode_chain(xop->head.ip1, clindex, 1378 HAMMER2_RESOLVE_ALWAYS); 1379 chain = NULL; 1380 if (parent == NULL) { 1381 /* XXX error */ 1382 goto done; 1383 } 1384 chain = hammer2_chain_lookup(&parent, &key_next, 1385 xop->key_beg, xop->key_end, 1386 &cache_index, 1387 HAMMER2_LOOKUP_ALWAYS); 1388 while (chain) { 1389 hammer2_chain_delete(parent, chain, 1390 xop->head.mtid, HAMMER2_DELETE_PERMANENT); 1391 hammer2_xop_feed(&xop->head, chain, clindex, chain->error); 1392 /* depend on function to unlock the shared lock */ 1393 chain = hammer2_chain_next(&parent, chain, &key_next, 1394 key_next, xop->key_end, 1395 &cache_index, 1396 HAMMER2_LOOKUP_ALWAYS); 1397 } 1398 done: 1399 hammer2_xop_feed(&xop->head, NULL, clindex, ENOENT); 1400 if (parent) { 1401 hammer2_chain_unlock(parent); 1402 hammer2_chain_drop(parent); 1403 } 1404 if (chain) { 1405 hammer2_chain_unlock(chain); 1406 hammer2_chain_drop(chain); 1407 } 1408 } 1409 1410 void 1411 hammer2_inode_xop_connect(hammer2_xop_t *arg, int clindex) 1412 { 1413 hammer2_xop_connect_t *xop = &arg->xop_connect; 1414 hammer2_inode_data_t *wipdata; 1415 hammer2_chain_t *parent; 1416 hammer2_chain_t *chain; 1417 hammer2_pfs_t *pmp; 1418 hammer2_key_t key_dummy; 1419 int cache_index = -1; 1420 int error; 1421 1422 /* 1423 * Get directory, then issue a lookup to prime the parent chain 1424 * for the create. The lookup is expected to fail. 1425 */ 1426 pmp = xop->head.ip1->pmp; 1427 parent = hammer2_inode_chain(xop->head.ip1, clindex, 1428 HAMMER2_RESOLVE_ALWAYS); 1429 if (parent == NULL) { 1430 chain = NULL; 1431 error = EIO; 1432 goto fail; 1433 } 1434 chain = hammer2_chain_lookup(&parent, &key_dummy, 1435 xop->lhc, xop->lhc, 1436 &cache_index, 0); 1437 if (chain) { 1438 hammer2_chain_unlock(chain); 1439 hammer2_chain_drop(chain); 1440 chain = NULL; 1441 error = EEXIST; 1442 goto fail; 1443 } 1444 1445 /* 1446 * Adjust the filename in the inode, set the name key. 1447 * 1448 * NOTE: Frontend must also adjust ip2->meta on success, we can't 1449 * do it here. 1450 */ 1451 chain = hammer2_inode_chain(xop->head.ip2, clindex, 1452 HAMMER2_RESOLVE_ALWAYS); 1453 hammer2_chain_modify(chain, xop->head.mtid, 0, 0); 1454 wipdata = &chain->data->ipdata; 1455 1456 hammer2_inode_modify(xop->head.ip2); 1457 if (xop->head.name1) { 1458 bzero(wipdata->filename, sizeof(wipdata->filename)); 1459 bcopy(xop->head.name1, wipdata->filename, xop->head.name1_len); 1460 wipdata->meta.name_len = xop->head.name1_len; 1461 } 1462 wipdata->meta.name_key = xop->lhc; 1463 1464 /* 1465 * Reconnect the chain to the new parent directory 1466 */ 1467 error = hammer2_chain_create(&parent, &chain, 1468 pmp, HAMMER2_METH_DEFAULT, 1469 xop->lhc, 0, 1470 HAMMER2_BREF_TYPE_INODE, 1471 HAMMER2_INODE_BYTES, 1472 xop->head.mtid, 0, 0); 1473 1474 /* 1475 * Feed result back. 1476 */ 1477 fail: 1478 hammer2_xop_feed(&xop->head, NULL, clindex, error); 1479 if (parent) { 1480 hammer2_chain_unlock(parent); 1481 hammer2_chain_drop(parent); 1482 } 1483 if (chain) { 1484 hammer2_chain_unlock(chain); 1485 hammer2_chain_drop(chain); 1486 } 1487 } 1488 1489 /* 1490 * Synchronize the in-memory inode with the chain. 1491 */ 1492 void 1493 hammer2_inode_xop_chain_sync(hammer2_xop_t *arg, int clindex) 1494 { 1495 hammer2_xop_fsync_t *xop = &arg->xop_fsync; 1496 hammer2_chain_t *parent; 1497 hammer2_chain_t *chain; 1498 int error; 1499 1500 parent = hammer2_inode_chain(xop->head.ip1, clindex, 1501 HAMMER2_RESOLVE_ALWAYS); 1502 chain = NULL; 1503 if (parent == NULL) { 1504 error = EIO; 1505 goto done; 1506 } 1507 if (parent->error) { 1508 error = parent->error; 1509 goto done; 1510 } 1511 1512 error = 0; 1513 1514 if ((xop->ipflags & HAMMER2_INODE_RESIZED) == 0) { 1515 /* osize must be ignored */ 1516 } else if (xop->meta.size < xop->osize) { 1517 /* 1518 * We must delete any chains beyond the EOF. The chain 1519 * straddling the EOF will be pending in the bioq. 1520 */ 1521 hammer2_key_t lbase; 1522 hammer2_key_t key_next; 1523 int cache_index = -1; 1524 1525 lbase = (xop->meta.size + HAMMER2_PBUFMASK64) & 1526 ~HAMMER2_PBUFMASK64; 1527 chain = hammer2_chain_lookup(&parent, &key_next, 1528 lbase, HAMMER2_KEY_MAX, 1529 &cache_index, 1530 HAMMER2_LOOKUP_NODATA | 1531 HAMMER2_LOOKUP_NODIRECT); 1532 while (chain) { 1533 /* 1534 * Degenerate embedded case, nothing to loop on 1535 */ 1536 switch (chain->bref.type) { 1537 case HAMMER2_BREF_TYPE_INODE: 1538 KKASSERT(0); 1539 break; 1540 case HAMMER2_BREF_TYPE_DATA: 1541 hammer2_chain_delete(parent, chain, 1542 xop->head.mtid, 1543 HAMMER2_DELETE_PERMANENT); 1544 break; 1545 } 1546 chain = hammer2_chain_next(&parent, chain, &key_next, 1547 key_next, HAMMER2_KEY_MAX, 1548 &cache_index, 1549 HAMMER2_LOOKUP_NODATA | 1550 HAMMER2_LOOKUP_NODIRECT); 1551 } 1552 1553 /* 1554 * Reset to point at inode for following code, if necessary. 1555 */ 1556 if (parent->bref.type != HAMMER2_BREF_TYPE_INODE) { 1557 hammer2_chain_unlock(parent); 1558 hammer2_chain_drop(parent); 1559 parent = hammer2_inode_chain(xop->head.ip1, clindex, 1560 HAMMER2_RESOLVE_ALWAYS); 1561 kprintf("hammer2: TRUNCATE RESET on '%s'\n", 1562 parent->data->ipdata.filename); 1563 } 1564 } 1565 1566 /* 1567 * Sync the inode meta-data, potentially clear the blockset area 1568 * of direct data so it can be used for blockrefs. 1569 */ 1570 hammer2_chain_modify(parent, xop->head.mtid, 0, 0); 1571 parent->data->ipdata.meta = xop->meta; 1572 if (xop->clear_directdata) { 1573 bzero(&parent->data->ipdata.u.blockset, 1574 sizeof(parent->data->ipdata.u.blockset)); 1575 } 1576 done: 1577 if (chain) { 1578 hammer2_chain_unlock(chain); 1579 hammer2_chain_drop(chain); 1580 } 1581 if (parent) { 1582 hammer2_chain_unlock(parent); 1583 hammer2_chain_drop(parent); 1584 } 1585 hammer2_xop_feed(&xop->head, NULL, clindex, error); 1586 } 1587 1588