2 * Copyright (c) 2011-2013 The DragonFly Project. All rights reserved.
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>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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.
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
35 #include <sys/cdefs.h>
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/types.h>
44 static void hammer2_inode_move_to_hidden(hammer2_trans_t *trans,
45 hammer2_chain_t **chainp,
48 RB_GENERATE2(hammer2_inode_tree, hammer2_inode, rbnode, hammer2_inode_cmp,
52 hammer2_inode_cmp(hammer2_inode_t *ip1, hammer2_inode_t *ip2)
54 if (ip1->inum < ip2->inum)
56 if (ip1->inum > ip2->inum)
64 * HAMMER2 offers shared locks and exclusive locks on inodes.
66 * An inode's ip->chain pointer is resolved and stable while an inode is
67 * locked, and can be cleaned out at any time (become NULL) when an inode
70 * This function handles duplication races and hardlink replacement races
71 * which can cause ip's cached chain to become stale.
73 * The underlying chain is also locked and returned.
75 * NOTE: We don't combine the inode/chain lock because putting away an
76 * inode would otherwise confuse multiple lock holders of the inode.
79 hammer2_inode_lock_ex(hammer2_inode_t *ip)
81 hammer2_chain_t *chain;
82 hammer2_chain_t *ochain;
83 hammer2_chain_core_t *core;
86 hammer2_inode_ref(ip);
87 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
92 if (chain->flags & HAMMER2_CHAIN_DUPLICATED) {
93 spin_lock(&core->cst.spin);
94 while (chain->flags & HAMMER2_CHAIN_DUPLICATED)
95 chain = TAILQ_NEXT(chain, core_entry);
96 hammer2_chain_ref(chain);
97 spin_unlock(&core->cst.spin);
98 hammer2_inode_repoint(ip, NULL, chain);
99 hammer2_chain_drop(chain);
101 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS);
102 if ((chain->flags & HAMMER2_CHAIN_DUPLICATED) == 0)
104 hammer2_chain_unlock(chain);
106 if (chain->data->ipdata.type == HAMMER2_OBJTYPE_HARDLINK &&
107 (chain->flags & HAMMER2_CHAIN_DELETED) == 0) {
108 error = hammer2_hardlink_find(ip->pip, &chain, &ochain);
109 hammer2_chain_drop(ochain);
110 KKASSERT(error == 0);
111 /* XXX error handling */
117 hammer2_inode_unlock_ex(hammer2_inode_t *ip, hammer2_chain_t *chain)
120 * XXX this will catch parent directories too which we don't
124 hammer2_chain_unlock(chain);
125 ccms_thread_unlock(&ip->topo_cst);
126 hammer2_inode_drop(ip);
130 * NOTE: We don't combine the inode/chain lock because putting away an
131 * inode would otherwise confuse multiple lock holders of the inode.
133 * Shared locks are especially sensitive to having too many shared
134 * lock counts (from the same thread) on certain paths which might
135 * need to upgrade them. Only one count of a shared lock can be
139 hammer2_inode_lock_sh(hammer2_inode_t *ip)
141 hammer2_chain_t *chain;
143 hammer2_inode_ref(ip);
145 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_SHARED);
148 KKASSERT(chain != NULL); /* for now */
149 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS |
150 HAMMER2_RESOLVE_SHARED);
153 * Resolve duplication races, resolve hardlinks by giving
154 * up and cycling an exclusive lock.
156 if ((chain->flags & HAMMER2_CHAIN_DUPLICATED) == 0 &&
157 chain->data->ipdata.type != HAMMER2_OBJTYPE_HARDLINK) {
160 hammer2_chain_unlock(chain);
161 ccms_thread_unlock(&ip->topo_cst);
162 chain = hammer2_inode_lock_ex(ip);
163 hammer2_inode_unlock_ex(ip, chain);
169 hammer2_inode_unlock_sh(hammer2_inode_t *ip, hammer2_chain_t *chain)
172 hammer2_chain_unlock(chain);
173 ccms_thread_unlock(&ip->topo_cst);
174 hammer2_inode_drop(ip);
178 hammer2_inode_lock_temp_release(hammer2_inode_t *ip)
180 return(ccms_thread_lock_temp_release(&ip->topo_cst));
184 hammer2_inode_lock_temp_restore(hammer2_inode_t *ip, ccms_state_t ostate)
186 ccms_thread_lock_temp_restore(&ip->topo_cst, ostate);
190 hammer2_inode_lock_upgrade(hammer2_inode_t *ip)
192 return(ccms_thread_lock_upgrade(&ip->topo_cst));
196 hammer2_inode_lock_downgrade(hammer2_inode_t *ip, ccms_state_t ostate)
198 ccms_thread_lock_downgrade(&ip->topo_cst, ostate);
202 * Lookup an inode by inode number
205 hammer2_inode_lookup(hammer2_pfsmount_t *pmp, hammer2_tid_t inum)
210 spin_lock(&pmp->inum_spin);
211 ip = RB_LOOKUP(hammer2_inode_tree, &pmp->inum_tree, inum);
213 hammer2_inode_ref(ip);
214 spin_unlock(&pmp->inum_spin);
222 * Adding a ref to an inode is only legal if the inode already has at least
226 hammer2_inode_ref(hammer2_inode_t *ip)
228 atomic_add_int(&ip->refs, 1);
232 * Drop an inode reference, freeing the inode when the last reference goes
236 hammer2_inode_drop(hammer2_inode_t *ip)
238 hammer2_pfsmount_t *pmp;
239 hammer2_inode_t *pip;
247 * Transition to zero, must interlock with
248 * the inode inumber lookup tree (if applicable).
250 * NOTE: The super-root inode has no pmp.
254 spin_lock(&pmp->inum_spin);
256 if (atomic_cmpset_int(&ip->refs, 1, 0)) {
257 KKASSERT(ip->topo_cst.count == 0);
258 if (ip->flags & HAMMER2_INODE_ONRBTREE) {
259 atomic_clear_int(&ip->flags,
260 HAMMER2_INODE_ONRBTREE);
261 RB_REMOVE(hammer2_inode_tree,
262 &pmp->inum_tree, ip);
265 spin_unlock(&pmp->inum_spin);
272 * Cleaning out ip->chain isn't entirely
275 hammer2_inode_repoint(ip, NULL, NULL);
278 * We have to drop pip (if non-NULL) to
279 * dispose of our implied reference from
280 * ip->pip. We can simply loop on it.
283 KKASSERT((ip->flags &
284 HAMMER2_INODE_SROOT) == 0);
285 kfree(ip, pmp->minode);
286 atomic_add_long(&pmp->inmem_inodes, -1);
289 HAMMER2_INODE_SROOT);
290 kfree(ip, M_HAMMER2);
293 /* continue with pip (can be NULL) */
296 spin_unlock(&ip->pmp->inum_spin);
300 * Non zero transition
302 if (atomic_cmpset_int(&ip->refs, refs, refs - 1))
309 * Get the vnode associated with the given inode, allocating the vnode if
310 * necessary. The vnode will be returned exclusively locked.
312 * The caller must lock the inode (shared or exclusive).
314 * Great care must be taken to avoid deadlocks and vnode acquisition/reclaim
318 hammer2_igetv(hammer2_inode_t *ip, int *errorp)
320 hammer2_inode_data_t *ipdata;
321 hammer2_pfsmount_t *pmp;
326 KKASSERT(pmp != NULL);
328 ipdata = &ip->chain->data->ipdata;
332 * Attempt to reuse an existing vnode assignment. It is
333 * possible to race a reclaim so the vget() may fail. The
334 * inode must be unlocked during the vget() to avoid a
335 * deadlock against a reclaim.
340 * Inode must be unlocked during the vget() to avoid
341 * possible deadlocks, but leave the ip ref intact.
343 * vnode is held to prevent destruction during the
344 * vget(). The vget() can still fail if we lost
345 * a reclaim race on the vnode.
348 ostate = hammer2_inode_lock_temp_release(ip);
349 if (vget(vp, LK_EXCLUSIVE)) {
351 hammer2_inode_lock_temp_restore(ip, ostate);
354 hammer2_inode_lock_temp_restore(ip, ostate);
356 /* vp still locked and ref from vget */
358 kprintf("hammer2: igetv race %p/%p\n",
368 * No vnode exists, allocate a new vnode. Beware of
369 * allocation races. This function will return an
370 * exclusively locked and referenced vnode.
372 *errorp = getnewvnode(VT_HAMMER2, pmp->mp, &vp, 0, 0);
374 kprintf("hammer2: igetv getnewvnode failed %d\n",
381 * Lock the inode and check for an allocation race.
383 ostate = hammer2_inode_lock_upgrade(ip);
384 if (ip->vp != NULL) {
387 hammer2_inode_lock_downgrade(ip, ostate);
391 switch (ipdata->type) {
392 case HAMMER2_OBJTYPE_DIRECTORY:
395 case HAMMER2_OBJTYPE_REGFILE:
397 vinitvmio(vp, ipdata->size,
399 (int)ipdata->size & HAMMER2_LBUFMASK);
401 case HAMMER2_OBJTYPE_SOFTLINK:
403 * XXX for now we are using the generic file_read
404 * and file_write code so we need a buffer cache
408 vinitvmio(vp, ipdata->size,
410 (int)ipdata->size & HAMMER2_LBUFMASK);
412 case HAMMER2_OBJTYPE_CDEV:
415 case HAMMER2_OBJTYPE_BDEV:
416 vp->v_ops = &pmp->mp->mnt_vn_spec_ops;
417 if (ipdata->type != HAMMER2_OBJTYPE_CDEV)
419 addaliasu(vp, ipdata->rmajor, ipdata->rminor);
421 case HAMMER2_OBJTYPE_FIFO:
423 vp->v_ops = &pmp->mp->mnt_vn_fifo_ops;
426 panic("hammer2: unhandled objtype %d", ipdata->type);
430 if (ip == pmp->iroot)
431 vsetflags(vp, VROOT);
435 hammer2_inode_ref(ip); /* vp association */
436 hammer2_inode_lock_downgrade(ip, ostate);
441 * Return non-NULL vp and *errorp == 0, or NULL vp and *errorp != 0.
443 if (hammer2_debug & 0x0002) {
444 kprintf("igetv vp %p refs 0x%08x aux 0x%08x\n",
445 vp, vp->v_refcnt, vp->v_auxrefs);
451 * The passed-in chain must be locked and the returned inode will also be
452 * locked. This routine typically locates or allocates the inode, assigns
453 * ip->chain (adding a ref to chain if necessary), and returns the inode.
455 * The hammer2_inode structure regulates the interface between the high level
456 * kernel VNOPS API and the filesystem backend (the chains).
458 * WARNING! This routine sucks up the chain's lock (makes it part of the
459 * inode lock from the point of view of the inode lock API),
460 * so callers need to be careful.
462 * WARNING! The mount code is allowed to pass dip == NULL for iroot and
463 * is allowed to pass pmp == NULL and dip == NULL for sroot.
466 hammer2_inode_get(hammer2_pfsmount_t *pmp, hammer2_inode_t *dip,
467 hammer2_chain_t *chain)
469 hammer2_inode_t *nip;
471 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_INODE);
474 * Interlocked lookup/ref of the inode. This code is only needed
475 * when looking up inodes with nlinks != 0 (TODO: optimize out
476 * otherwise and test for duplicates).
480 nip = hammer2_inode_lookup(pmp, chain->data->ipdata.inum);
483 ccms_thread_lock(&nip->topo_cst, CCMS_STATE_EXCLUSIVE);
484 if ((nip->flags & HAMMER2_INODE_ONRBTREE) == 0) { /* race */
485 ccms_thread_unlock(&nip->topo_cst);
486 hammer2_inode_drop(nip);
489 if (nip->chain != chain)
490 hammer2_inode_repoint(nip, NULL, chain);
493 * Consolidated nip/nip->chain is locked (chain locked
500 * We couldn't find the inode number, create a new inode.
503 nip = kmalloc(sizeof(*nip), pmp->minode, M_WAITOK | M_ZERO);
504 atomic_add_long(&pmp->inmem_inodes, 1);
505 hammer2_chain_memory_inc(pmp);
506 hammer2_chain_memory_wakeup(pmp);
508 nip = kmalloc(sizeof(*nip), M_HAMMER2, M_WAITOK | M_ZERO);
509 nip->flags = HAMMER2_INODE_SROOT;
511 nip->inum = chain->data->ipdata.inum;
512 nip->size = chain->data->ipdata.size;
513 nip->mtime = chain->data->ipdata.mtime;
514 hammer2_inode_repoint(nip, NULL, chain);
515 nip->pip = dip; /* can be NULL */
517 hammer2_inode_ref(dip); /* ref dip for nip->pip */
522 * ref and lock on nip gives it state compatible to after a
523 * hammer2_inode_lock_ex() call.
526 ccms_cst_init(&nip->topo_cst, &nip->chain);
527 ccms_thread_lock(&nip->topo_cst, CCMS_STATE_EXCLUSIVE);
528 /* combination of thread lock and chain lock == inode lock */
531 * Attempt to add the inode. If it fails we raced another inode
532 * get. Undo all the work and try again.
535 spin_lock(&pmp->inum_spin);
536 if (RB_INSERT(hammer2_inode_tree, &pmp->inum_tree, nip)) {
537 spin_unlock(&pmp->inum_spin);
538 ccms_thread_unlock(&nip->topo_cst);
539 hammer2_inode_drop(nip);
542 atomic_set_int(&nip->flags, HAMMER2_INODE_ONRBTREE);
543 spin_unlock(&pmp->inum_spin);
550 * Create a new inode in the specified directory using the vattr to
551 * figure out the type of inode.
553 * If no error occurs the new inode with its chain locked is returned in
554 * *nipp, otherwise an error is returned and *nipp is set to NULL.
556 * If vap and/or cred are NULL the related fields are not set and the
557 * inode type defaults to a directory. This is used when creating PFSs
558 * under the super-root, so the inode number is set to 1 in this case.
560 * dip is not locked on entry.
563 hammer2_inode_create(hammer2_trans_t *trans, hammer2_inode_t *dip,
564 struct vattr *vap, struct ucred *cred,
565 const uint8_t *name, size_t name_len,
566 hammer2_chain_t **chainp, int *errorp)
568 hammer2_inode_data_t *dipdata;
569 hammer2_inode_data_t *nipdata;
570 hammer2_chain_t *chain;
571 hammer2_chain_t *parent;
572 hammer2_inode_t *nip;
573 hammer2_key_t key_dummy;
581 int cache_index = -1;
583 lhc = hammer2_dirhash(name, name_len);
587 * Locate the inode or indirect block to create the new
588 * entry in. At the same time check for key collisions
589 * and iterate until we don't get one.
591 * NOTE: hidden inodes do not have iterators.
594 parent = hammer2_inode_lock_ex(dip);
595 dipdata = &dip->chain->data->ipdata;
596 dip_uid = dipdata->uid;
597 dip_gid = dipdata->gid;
598 dip_mode = dipdata->mode;
599 dip_algo = dipdata->comp_algo;
603 chain = hammer2_chain_lookup(&parent, &key_dummy,
604 lhc, lhc, &cache_index, 0);
607 if ((lhc & HAMMER2_DIRHASH_VISIBLE) == 0)
609 if ((lhc & HAMMER2_DIRHASH_LOMASK) == HAMMER2_DIRHASH_LOMASK)
611 hammer2_chain_unlock(chain);
617 error = hammer2_chain_create(trans, &parent, &chain,
619 HAMMER2_BREF_TYPE_INODE,
620 HAMMER2_INODE_BYTES);
624 * Cleanup and handle retries.
626 if (error == EAGAIN) {
627 hammer2_chain_ref(parent);
628 hammer2_inode_unlock_ex(dip, parent);
629 hammer2_chain_wait(parent);
630 hammer2_chain_drop(parent);
633 hammer2_inode_unlock_ex(dip, parent);
636 KKASSERT(chain == NULL);
642 * Set up the new inode.
644 * NOTE: *_get() integrates chain's lock into the inode lock.
646 * NOTE: Only one new inode can currently be created per
647 * transaction. If the need arises we can adjust
648 * hammer2_trans_init() to allow more.
650 * NOTE: nipdata will have chain's blockset data.
652 chain->data->ipdata.inum = trans->inode_tid;
653 nip = hammer2_inode_get(dip->pmp, dip, chain);
654 nipdata = &chain->data->ipdata;
657 KKASSERT(trans->inodes_created == 0);
658 nipdata->type = hammer2_get_obj_type(vap->va_type);
659 nipdata->inum = trans->inode_tid;
660 ++trans->inodes_created;
662 switch (nipdata->type) {
663 case HAMMER2_OBJTYPE_CDEV:
664 case HAMMER2_OBJTYPE_BDEV:
665 nipdata->rmajor = vap->va_rmajor;
666 nipdata->rminor = vap->va_rminor;
672 nipdata->type = HAMMER2_OBJTYPE_DIRECTORY;
676 /* Inherit parent's inode compression mode. */
677 nip->comp_heuristic = 0;
678 nipdata->comp_algo = dip_algo;
679 nipdata->version = HAMMER2_INODE_VERSION_ONE;
680 hammer2_update_time(&nipdata->ctime);
681 nipdata->mtime = nipdata->ctime;
683 nipdata->mode = vap->va_mode;
686 if (dip && dip->pmp) {
687 xuid = hammer2_to_unix_xid(&dip_uid);
688 xuid = vop_helper_create_uid(dip->pmp->mp,
694 /* super-root has no dip and/or pmp */
697 if (vap->va_vaflags & VA_UID_UUID_VALID)
698 nipdata->uid = vap->va_uid_uuid;
699 else if (vap->va_uid != (uid_t)VNOVAL)
700 hammer2_guid_to_uuid(&nipdata->uid, vap->va_uid);
702 hammer2_guid_to_uuid(&nipdata->uid, xuid);
704 if (vap->va_vaflags & VA_GID_UUID_VALID)
705 nipdata->gid = vap->va_gid_uuid;
706 else if (vap->va_gid != (gid_t)VNOVAL)
707 hammer2_guid_to_uuid(&nipdata->gid, vap->va_gid);
709 nipdata->gid = dip_gid;
713 * Regular files and softlinks allow a small amount of data to be
714 * directly embedded in the inode. This flag will be cleared if
715 * the size is extended past the embedded limit.
717 if (nipdata->type == HAMMER2_OBJTYPE_REGFILE ||
718 nipdata->type == HAMMER2_OBJTYPE_SOFTLINK) {
719 nipdata->op_flags |= HAMMER2_OPFLAG_DIRECTDATA;
722 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
723 bcopy(name, nipdata->filename, name_len);
724 nipdata->name_key = lhc;
725 nipdata->name_len = name_len;
732 * chain may have been moved around by the create.
735 hammer2_chain_refactor(hammer2_chain_t **chainp)
737 hammer2_chain_t *chain = *chainp;
738 hammer2_chain_core_t *core;
741 while (chain->flags & HAMMER2_CHAIN_DUPLICATED) {
742 spin_lock(&core->cst.spin);
743 chain = TAILQ_NEXT(chain, core_entry);
744 while (chain->flags & HAMMER2_CHAIN_DUPLICATED)
745 chain = TAILQ_NEXT(chain, core_entry);
746 hammer2_chain_ref(chain);
747 spin_unlock(&core->cst.spin);
748 KKASSERT(chain->core == core);
750 hammer2_chain_unlock(*chainp);
751 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS |
752 HAMMER2_RESOLVE_NOREF); /* eat ref */
758 * Shift *chainp up to the specified directory, change the filename
759 * to "0xINODENUMBER", and adjust the key. The chain becomes the
760 * invisible hardlink target.
762 * The original *chainp has already been marked deleted.
766 hammer2_hardlink_shiftup(hammer2_trans_t *trans, hammer2_chain_t **chainp,
767 hammer2_inode_t *dip, hammer2_chain_t **dchainp,
768 int nlinks, int *errorp)
770 hammer2_inode_data_t *nipdata;
771 hammer2_chain_t *chain;
772 hammer2_chain_t *xchain;
773 hammer2_key_t key_dummy;
775 hammer2_blockref_t bref;
776 int cache_index = -1;
779 lhc = chain->data->ipdata.inum;
780 KKASSERT((lhc & HAMMER2_DIRHASH_VISIBLE) == 0);
783 * Locate the inode or indirect block to create the new
784 * entry in. lhc represents the inode number so there is
785 * no collision iteration.
787 * There should be no key collisions with invisible inode keys.
789 * WARNING! Must use inode_lock_ex() on dip to handle a stale
794 xchain = hammer2_chain_lookup(dchainp, &key_dummy,
795 lhc, lhc, &cache_index, 0);
797 kprintf("X3 chain %p dip %p dchain %p dip->chain %p\n",
798 xchain, dip, *dchainp, dip->chain);
799 hammer2_chain_unlock(xchain);
808 * Create entry in common parent directory using the seek position
811 * We must refactor chain because it might have been shifted into
812 * an indirect chain by the create.
815 KKASSERT(xchain == NULL);
817 *errorp = hammer2_chain_create(trans, dchainp, &xchain,
819 HAMMER2_BREF_TYPE_INODE,/* n/a */
820 HAMMER2_INODE_BYTES); /* n/a */
822 /*XXX this somehow isn't working on chain XXX*/
827 * Cleanup and handle retries.
829 if (*errorp == EAGAIN) {
831 hammer2_chain_wait(*dchainp);
832 hammer2_chain_drop(*dchainp);
837 * Handle the error case
841 KKASSERT(xchain == NULL);
846 * Use xchain as a placeholder for (lhc). Duplicate chain to the
847 * same target bref as xchain and then delete xchain. The duplication
848 * occurs after xchain in flush order even though xchain is deleted
849 * after the duplication. XXX
851 * WARNING! Duplications (to a different parent) can cause indirect
852 * blocks to be inserted, refactor xchain.
855 bref.key = lhc; /* invisible dir entry key */
857 hammer2_chain_duplicate(trans, dchainp, &chain, &bref, 0, 2);
860 * chain is now 'live' again.. adjust the filename.
862 * Directory entries are inodes but this is a hidden hardlink
863 * target. The name isn't used but to ease debugging give it
864 * a name after its inode number.
866 hammer2_chain_modify(trans, &chain, 0);
867 nipdata = &chain->data->ipdata;
868 ksnprintf(nipdata->filename, sizeof(nipdata->filename),
869 "0x%016jx", (intmax_t)nipdata->inum);
870 nipdata->name_len = strlen(nipdata->filename);
871 nipdata->name_key = lhc;
872 nipdata->nlinks += nlinks;
878 * Connect the target inode represented by (*chainp) to the media topology
879 * at (dip, name, len).
881 * If hlink is TRUE this function creates an OBJTYPE_HARDLINK directory
882 * entry instead of connecting (*chainp).
884 * If hlink is FALSE this function uses chain_duplicate() to make a copy
885 * if (*chainp) in the directory entry. (*chainp) is likely to be deleted
886 * by the caller in this case (e.g. rename).
889 hammer2_inode_connect(hammer2_trans_t *trans,
890 hammer2_chain_t **chainp, int hlink,
891 hammer2_inode_t *dip, hammer2_chain_t **dchainp,
892 const uint8_t *name, size_t name_len,
895 hammer2_inode_data_t *ipdata;
896 hammer2_chain_t *nchain;
897 hammer2_chain_t *ochain;
898 hammer2_key_t key_dummy;
899 int cache_index = -1;
903 * Since ochain is either disconnected from the topology or represents
904 * a hardlink terminus which is always a parent of or equal to dip,
905 * we should be able to safely lock dip->chain for our setup.
907 * WARNING! Must use inode_lock_ex() on dip to handle a stale
913 * If name is non-NULL we calculate lhc, else we use the passed-in
917 lhc = hammer2_dirhash(name, name_len);
920 * Locate the inode or indirect block to create the new
921 * entry in. At the same time check for key collisions
922 * and iterate until we don't get one.
926 nchain = hammer2_chain_lookup(dchainp, &key_dummy,
931 if ((lhc & HAMMER2_DIRHASH_LOMASK) ==
932 HAMMER2_DIRHASH_LOMASK) {
935 hammer2_chain_unlock(nchain);
944 * Hardlink pointer needed, create totally fresh
947 * We must refactor ochain because it might have
948 * been shifted into an indirect chain by the
951 KKASSERT(nchain == NULL);
952 error = hammer2_chain_create(trans, dchainp, &nchain,
954 HAMMER2_BREF_TYPE_INODE,
955 HAMMER2_INODE_BYTES);
956 hammer2_chain_refactor(&ochain);
959 * Reconnect the original chain and rename. Use
960 * chain_duplicate(). The caller will likely delete
961 * or has already deleted the original chain in
964 * NOTE: chain_duplicate() generates a new chain
965 * with CHAIN_DELETED cleared (ochain typically
966 * has it set from the file unlink).
968 * WARNING! Can cause held-over chains to require a
969 * refactor. Fortunately we have none (our
970 * locked chains are passed into and
971 * modified by the call).
975 hammer2_chain_duplicate(trans, NULL, &nchain, NULL,
977 error = hammer2_chain_create(trans, dchainp, &nchain,
979 HAMMER2_BREF_TYPE_INODE,
980 HAMMER2_INODE_BYTES);
987 KKASSERT(error != EAGAIN);
990 * nchain should be NULL on error, leave ochain (== *chainp) alone.
993 KKASSERT(nchain == NULL);
998 * Directory entries are inodes so if the name has changed we have
999 * to update the inode.
1001 * When creating an OBJTYPE_HARDLINK entry remember to unlock the
1002 * chain, the caller will access the hardlink via the actual hardlink
1003 * target file and not the hardlink pointer entry, so we must still
1006 if (hlink && hammer2_hardlink_enable >= 0) {
1008 * Create the HARDLINK pointer. oip represents the hardlink
1009 * target in this situation.
1011 * We will return ochain (the hardlink target).
1013 hammer2_chain_modify(trans, &nchain, 0);
1014 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
1015 ipdata = &nchain->data->ipdata;
1016 bcopy(name, ipdata->filename, name_len);
1017 ipdata->name_key = lhc;
1018 ipdata->name_len = name_len;
1019 ipdata->target_type = ochain->data->ipdata.type;
1020 ipdata->type = HAMMER2_OBJTYPE_HARDLINK;
1021 ipdata->inum = ochain->data->ipdata.inum;
1023 hammer2_chain_unlock(nchain);
1026 } else if (hlink && hammer2_hardlink_enable < 0) {
1028 * Create a snapshot (hardlink fake mode for debugging).
1029 * (ochain already flushed above so we can just copy the
1032 * Since this is a snapshot we return nchain in the fake
1035 hammer2_chain_modify(trans, &nchain, 0);
1036 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
1037 ipdata = &nchain->data->ipdata;
1038 *ipdata = ochain->data->ipdata;
1039 bcopy(name, ipdata->filename, name_len);
1040 ipdata->name_key = lhc;
1041 ipdata->name_len = name_len;
1042 atomic_clear_int(&nchain->core->flags,
1043 HAMMER2_CORE_COUNTEDBREFS);
1044 kprintf("created fake hardlink %*.*s\n",
1045 (int)name_len, (int)name_len, name);
1048 * nchain is a duplicate of ochain at the new location.
1049 * We must fixup the name stored in oip. The bref key
1050 * has already been set up.
1052 hammer2_chain_modify(trans, &nchain, 0);
1053 ipdata = &nchain->data->ipdata;
1055 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
1056 bcopy(name, ipdata->filename, name_len);
1057 ipdata->name_key = lhc;
1058 ipdata->name_len = name_len;
1063 * We are replacing ochain with nchain, unlock ochain. In the
1064 * case where ochain is left unchanged the code above sets
1065 * nchain to ochain and ochain to NULL, resulting in a NOP here.
1068 hammer2_chain_unlock(ochain);
1075 * Repoint ip->chain to nchain. Caller must hold the inode exclusively
1078 * ip->chain is set to nchain. The prior chain in ip->chain is dropped
1079 * and nchain is ref'd.
1082 hammer2_inode_repoint(hammer2_inode_t *ip, hammer2_inode_t *pip,
1083 hammer2_chain_t *nchain)
1085 hammer2_chain_t *ochain;
1086 hammer2_inode_t *opip;
1089 * Repoint ip->chain if requested.
1094 hammer2_chain_ref(nchain);
1096 hammer2_chain_drop(ochain);
1099 * Repoint ip->pip if requested (non-NULL pip).
1101 if (pip && ip->pip != pip) {
1103 hammer2_inode_ref(pip);
1106 hammer2_inode_drop(opip);
1111 * Unlink the file from the specified directory inode. The directory inode
1112 * does not need to be locked.
1114 * isdir determines whether a directory/non-directory check should be made.
1115 * No check is made if isdir is set to -1.
1117 * isopen specifies whether special unlink-with-open-descriptor handling
1118 * must be performed. If set to -1 the caller is deleting a PFS and we
1119 * check whether the chain is mounted or not (chain->pmp != NULL). 1 is
1120 * implied if it is mounted.
1122 * If isopen is 1 and nlinks drops to 0 this function must move the chain
1123 * to a special hidden directory until last-close occurs on the file.
1125 * NOTE! The underlying file can still be active with open descriptors
1126 * or if the chain is being manually held (e.g. for rename).
1128 * The caller is responsible for fixing up ip->chain if e.g. a
1129 * rename occurs (see chain_duplicate()).
1132 hammer2_unlink_file(hammer2_trans_t *trans, hammer2_inode_t *dip,
1133 const uint8_t *name, size_t name_len,
1134 int isdir, int *hlinkp, struct nchandle *nch)
1136 hammer2_inode_data_t *ipdata;
1137 hammer2_chain_t *parent;
1138 hammer2_chain_t *ochain;
1139 hammer2_chain_t *chain;
1140 hammer2_chain_t *dparent;
1141 hammer2_chain_t *dchain;
1142 hammer2_key_t key_dummy;
1143 hammer2_key_t key_next;
1146 int cache_index = -1;
1151 lhc = hammer2_dirhash(name, name_len);
1154 * Search for the filename in the directory
1158 parent = hammer2_inode_lock_ex(dip);
1159 chain = hammer2_chain_lookup(&parent, &key_next,
1160 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1163 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
1164 name_len == chain->data->ipdata.name_len &&
1165 bcmp(name, chain->data->ipdata.filename, name_len) == 0) {
1168 chain = hammer2_chain_next(&parent, chain, &key_next,
1170 lhc + HAMMER2_DIRHASH_LOMASK,
1173 hammer2_inode_unlock_ex(dip, NULL); /* retain parent */
1176 * Not found or wrong type (isdir < 0 disables the type check).
1177 * If a hardlink pointer, type checks use the hardlink target.
1179 if (chain == NULL) {
1183 if ((type = chain->data->ipdata.type) == HAMMER2_OBJTYPE_HARDLINK) {
1186 type = chain->data->ipdata.target_type;
1189 if (type == HAMMER2_OBJTYPE_DIRECTORY && isdir == 0) {
1193 if (type != HAMMER2_OBJTYPE_DIRECTORY && isdir >= 1) {
1199 * Hardlink must be resolved. We can't hold the parent locked
1200 * while we do this or we could deadlock.
1202 * On success chain will be adjusted to point at the hardlink target
1203 * and ochain will point to the hardlink pointer in the original
1204 * directory. Otherwise chain remains pointing to the original.
1206 if (chain->data->ipdata.type == HAMMER2_OBJTYPE_HARDLINK) {
1207 hammer2_chain_unlock(parent);
1209 error = hammer2_hardlink_find(dip, &chain, &ochain);
1213 * If this is a directory the directory must be empty. However, if
1214 * isdir < 0 we are doing a rename and the directory does not have
1215 * to be empty, and if isdir > 1 we are deleting a PFS/snapshot
1216 * and the directory does not have to be empty.
1218 * NOTE: We check the full key range here which covers both visible
1219 * and invisible entries. Theoretically there should be no
1220 * invisible (hardlink target) entries if there are no visible
1223 if (type == HAMMER2_OBJTYPE_DIRECTORY && isdir == 1) {
1224 dparent = hammer2_chain_lookup_init(chain, 0);
1225 dchain = hammer2_chain_lookup(&dparent, &key_dummy,
1226 0, (hammer2_key_t)-1,
1228 HAMMER2_LOOKUP_NODATA);
1230 hammer2_chain_unlock(dchain);
1231 hammer2_chain_lookup_done(dparent);
1235 hammer2_chain_lookup_done(dparent);
1241 * Ok, we can now unlink the chain. We always decrement nlinks even
1242 * if the entry can be deleted in case someone has the file open and
1245 * The chain itself will no longer be in the on-media topology but
1246 * can still be flushed to the media (e.g. if an open descriptor
1247 * remains). When the last vnode/ip ref goes away the chain will
1248 * be marked unmodified, avoiding any further (now unnecesary) I/O.
1250 * A non-NULL ochain indicates a hardlink.
1254 * Delete the original hardlink pointer unconditionally.
1255 * (any open descriptors will migrate to the hardlink
1256 * target and have no affect on this operation).
1258 * NOTE: parent from above is NULL when ochain != NULL
1259 * so we can reuse it.
1261 hammer2_chain_lock(ochain, HAMMER2_RESOLVE_ALWAYS);
1262 hammer2_chain_delete(trans, ochain, 0);
1263 hammer2_chain_unlock(ochain);
1267 * Decrement nlinks on the hardlink target (or original file if
1268 * there it was not hardlinked). Delete the target when nlinks
1269 * reaches 0 with special handling if (isopen) is set.
1271 * NOTE! In DragonFly the vnops function calls cache_unlink() after
1272 * calling us here to clean out the namecache association,
1273 * (which does not represent a ref for the open-test), and to
1274 * force finalization of the vnode if/when the last ref gets
1277 * NOTE! Files are unlinked by rename and then relinked. nch will be
1278 * passed as NULL in this situation. hammer2_inode_connect()
1281 KKASSERT(chain != NULL);
1282 hammer2_chain_modify(trans, &chain, 0);
1283 ipdata = &chain->data->ipdata;
1285 if ((int64_t)ipdata->nlinks < 0) /* XXX debugging */
1287 if (ipdata->nlinks == 0) {
1288 if ((chain->flags & HAMMER2_CHAIN_PFSROOT) && chain->pmp) {
1290 kprintf("hammer2: PFS \"%s\" cannot be deleted "
1291 "while still mounted\n",
1295 if (nch && cache_isopen(nch)) {
1296 kprintf("WARNING: unlinking open file\n");
1297 atomic_set_int(&chain->flags, HAMMER2_CHAIN_UNLINKED);
1298 hammer2_inode_move_to_hidden(trans, &chain,
1301 hammer2_chain_delete(trans, chain, 0);
1307 hammer2_chain_unlock(chain);
1309 hammer2_chain_lookup_done(parent);
1311 hammer2_chain_drop(ochain);
1317 * This is called from the mount code to initialize pmp->ihidden
1320 hammer2_inode_install_hidden(hammer2_pfsmount_t *pmp)
1322 hammer2_trans_t trans;
1323 hammer2_chain_t *parent;
1324 hammer2_chain_t *chain;
1325 hammer2_chain_t *scan;
1326 hammer2_inode_data_t *ipdata;
1327 hammer2_key_t key_dummy;
1328 hammer2_key_t key_next;
1337 * Find the hidden directory
1339 bzero(&key_dummy, sizeof(key_dummy));
1340 hammer2_trans_init(&trans, pmp, NULL, 0);
1342 parent = hammer2_inode_lock_ex(pmp->iroot);
1343 chain = hammer2_chain_lookup(&parent, &key_dummy,
1344 HAMMER2_INODE_HIDDENDIR,
1345 HAMMER2_INODE_HIDDENDIR,
1348 pmp->ihidden = hammer2_inode_get(pmp, pmp->iroot, chain);
1349 hammer2_inode_ref(pmp->ihidden);
1352 * Remove any unlinked files which were left open as-of
1356 scan = hammer2_chain_lookup(&chain, &key_next,
1359 HAMMER2_LOOKUP_NODATA);
1361 if (scan->bref.type == HAMMER2_BREF_TYPE_INODE) {
1362 hammer2_chain_delete(&trans, scan, 0);
1365 scan = hammer2_chain_next(&chain, scan, &key_next,
1368 HAMMER2_LOOKUP_NODATA);
1371 hammer2_inode_unlock_ex(pmp->ihidden, chain);
1372 hammer2_inode_unlock_ex(pmp->iroot, parent);
1373 hammer2_trans_done(&trans);
1374 kprintf("hammer2: PFS loaded hidden dir, "
1375 "removed %d dead entries\n", count);
1380 * Create the hidden directory
1382 error = hammer2_chain_create(&trans, &parent, &chain,
1383 HAMMER2_INODE_HIDDENDIR, 0,
1384 HAMMER2_BREF_TYPE_INODE,
1385 HAMMER2_INODE_BYTES);
1386 hammer2_inode_unlock_ex(pmp->iroot, parent);
1387 hammer2_chain_modify(&trans, &chain, 0);
1388 ipdata = &chain->data->ipdata;
1389 ipdata->type = HAMMER2_OBJTYPE_DIRECTORY;
1390 ipdata->inum = HAMMER2_INODE_HIDDENDIR;
1392 kprintf("hammer2: PFS root missing hidden directory, creating\n");
1394 pmp->ihidden = hammer2_inode_get(pmp, pmp->iroot, chain);
1395 hammer2_inode_ref(pmp->ihidden);
1396 hammer2_inode_unlock_ex(pmp->ihidden, chain);
1397 hammer2_trans_done(&trans);
1401 * If an open file is unlinked H2 needs to retain the file in the topology
1402 * to ensure that its backing store is not recovered by the bulk free scan.
1403 * This also allows us to avoid having to special-case the CHAIN_DELETED flag.
1405 * To do this the file is moved to a hidden directory in the PFS root and
1406 * renamed. The hidden directory must be created if it does not exist.
1410 hammer2_inode_move_to_hidden(hammer2_trans_t *trans, hammer2_chain_t **chainp,
1413 hammer2_chain_t *chain;
1414 hammer2_chain_t *dchain;
1415 hammer2_pfsmount_t *pmp;
1420 KKASSERT(pmp != NULL);
1421 KKASSERT(pmp->ihidden != NULL);
1423 hammer2_chain_delete(trans, chain, 0);
1424 dchain = hammer2_inode_lock_ex(pmp->ihidden);
1425 error = hammer2_inode_connect(trans, chainp, 0,
1426 pmp->ihidden, &dchain,
1428 hammer2_inode_unlock_ex(pmp->ihidden, dchain);
1429 KKASSERT(error == 0);
1433 * Given an exclusively locked inode and chain we consolidate its chain
1434 * for hardlink creation, adding (nlinks) to the file's link count and
1435 * potentially relocating the inode to a directory common to ip->pip and tdip.
1437 * Replaces (*chainp) if consolidation occurred, unlocking the old chain
1438 * and returning a new locked chain.
1440 * NOTE! This function will also replace ip->chain.
1443 hammer2_hardlink_consolidate(hammer2_trans_t *trans,
1444 hammer2_inode_t *ip, hammer2_chain_t **chainp,
1445 hammer2_inode_t *cdip, hammer2_chain_t **cdchainp,
1448 hammer2_inode_data_t *ipdata;
1449 hammer2_chain_t *chain;
1450 hammer2_chain_t *nchain;
1454 if (nlinks == 0 && /* no hardlink needed */
1455 (chain->data->ipdata.name_key & HAMMER2_DIRHASH_VISIBLE)) {
1458 if (hammer2_hardlink_enable < 0) { /* fake hardlinks */
1462 if (hammer2_hardlink_enable == 0) { /* disallow hardlinks */
1463 hammer2_chain_unlock(chain);
1469 * If no change in the hardlink's target directory is required and
1470 * this is already a hardlink target, all we need to do is adjust
1473 if (cdip == ip->pip &&
1474 (chain->data->ipdata.name_key & HAMMER2_DIRHASH_VISIBLE) == 0) {
1476 hammer2_chain_modify(trans, &chain, 0);
1477 chain->data->ipdata.nlinks += nlinks;
1485 * chain is the real inode. If it's visible we have to convert it
1486 * to a hardlink pointer. If it is not visible then it is already
1487 * a hardlink target and only needs to be deleted.
1489 KKASSERT((chain->flags & HAMMER2_CHAIN_DELETED) == 0);
1490 KKASSERT(chain->data->ipdata.type != HAMMER2_OBJTYPE_HARDLINK);
1491 if (chain->data->ipdata.name_key & HAMMER2_DIRHASH_VISIBLE) {
1493 * We are going to duplicate chain later, causing its
1494 * media block to be shifted to the duplicate. Even though
1495 * we are delete-duplicating nchain here it might decide not
1496 * to reallocate the block. Set FORCECOW to force it to.
1499 hammer2_chain_lock(nchain, HAMMER2_RESOLVE_ALWAYS);
1500 atomic_set_int(&nchain->flags, HAMMER2_CHAIN_FORCECOW);
1501 hammer2_chain_delete_duplicate(trans, &nchain,
1502 HAMMER2_DELDUP_RECORE);
1503 KKASSERT((chain->flags & HAMMER2_CHAIN_DUPLICATED) == 0);
1505 ipdata = &nchain->data->ipdata;
1506 ipdata->target_type = ipdata->type;
1507 ipdata->type = HAMMER2_OBJTYPE_HARDLINK;
1515 bzero(&ipdata->uid, sizeof(ipdata->uid));
1516 bzero(&ipdata->gid, sizeof(ipdata->gid));
1517 ipdata->op_flags = HAMMER2_OPFLAG_DIRECTDATA;
1518 ipdata->cap_flags = 0;
1522 ipdata->iparent = 0; /* XXX */
1523 ipdata->pfs_type = 0;
1524 ipdata->pfs_inum = 0;
1525 bzero(&ipdata->pfs_clid, sizeof(ipdata->pfs_clid));
1526 bzero(&ipdata->pfs_fsid, sizeof(ipdata->pfs_fsid));
1527 ipdata->data_quota = 0;
1528 ipdata->data_count = 0;
1529 ipdata->inode_quota = 0;
1530 ipdata->inode_count = 0;
1531 ipdata->attr_tid = 0;
1532 ipdata->dirent_tid = 0;
1533 bzero(&ipdata->u, sizeof(ipdata->u));
1534 /* XXX transaction ids */
1536 hammer2_chain_delete(trans, chain, 0);
1541 * chain represents the hardlink target and is now flagged deleted.
1542 * duplicate it to the parent directory and adjust nlinks.
1544 * WARNING! The shiftup() call can cause nchain to be moved into
1545 * an indirect block, and our nchain will wind up pointing
1546 * to the older/original version.
1548 KKASSERT(chain->flags & HAMMER2_CHAIN_DELETED);
1549 hammer2_hardlink_shiftup(trans, &chain, cdip, cdchainp, nlinks, &error);
1552 hammer2_inode_repoint(ip, cdip, chain);
1555 * Unlock the original chain last as the lock blocked races against
1556 * the creation of the new hardlink target.
1559 hammer2_chain_unlock(nchain);
1563 * Cleanup, chain/nchain already dealt with.
1566 hammer2_inode_drop(cdip);
1572 * If (*ochainp) is non-NULL it points to the forward OBJTYPE_HARDLINK
1573 * inode while (*chainp) points to the resolved (hidden hardlink
1574 * target) inode. In this situation when nlinks is 1 we wish to
1575 * deconsolidate the hardlink, moving it back to the directory that now
1576 * represents the only remaining link.
1579 hammer2_hardlink_deconsolidate(hammer2_trans_t *trans,
1580 hammer2_inode_t *dip,
1581 hammer2_chain_t **chainp,
1582 hammer2_chain_t **ochainp)
1584 if (*ochainp == NULL)
1591 * The caller presents a locked *chainp pointing to a HAMMER2_BREF_TYPE_INODE
1592 * with an obj_type of HAMMER2_OBJTYPE_HARDLINK. This routine will gobble
1593 * the *chainp and return a new locked *chainp representing the file target
1594 * (the original *chainp will be unlocked).
1596 * When a match is found the chain representing the original HARDLINK
1597 * will be returned in *ochainp with a ref, but not locked.
1599 * When no match is found *chainp is set to NULL and EIO is returned.
1600 * (*ochainp) will still be set to the original chain with a ref but not
1604 hammer2_hardlink_find(hammer2_inode_t *dip, hammer2_chain_t **chainp,
1605 hammer2_chain_t **ochainp)
1607 hammer2_chain_t *chain = *chainp;
1608 hammer2_chain_t *parent;
1609 hammer2_inode_t *ip;
1610 hammer2_inode_t *pip;
1611 hammer2_key_t key_dummy;
1613 int cache_index = -1;
1616 hammer2_inode_ref(pip); /* for loop */
1617 hammer2_chain_ref(chain); /* for (*ochainp) */
1621 * Locate the hardlink. pip is referenced and not locked,
1626 lhc = chain->data->ipdata.inum;
1627 hammer2_chain_unlock(chain);
1630 while ((ip = pip) != NULL) {
1631 parent = hammer2_inode_lock_ex(ip);
1632 hammer2_inode_drop(ip); /* loop */
1633 KKASSERT(parent->bref.type == HAMMER2_BREF_TYPE_INODE);
1634 chain = hammer2_chain_lookup(&parent, &key_dummy,
1635 lhc, lhc, &cache_index, 0);
1636 hammer2_chain_lookup_done(parent); /* discard parent */
1639 pip = ip->pip; /* safe, ip held locked */
1641 hammer2_inode_ref(pip); /* loop */
1642 hammer2_inode_unlock_ex(ip, NULL);
1646 * chain is locked, ip is locked. Unlock ip, return the locked
1647 * chain. *ipp is already set w/a ref count and not locked.
1649 * (parent is already unlocked).
1652 hammer2_inode_unlock_ex(ip, NULL);
1655 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_INODE);
1656 /* already locked */
1664 * Find the directory common to both fdip and tdip, hold and return
1668 hammer2_inode_common_parent(hammer2_inode_t *fdip, hammer2_inode_t *tdip)
1670 hammer2_inode_t *scan1;
1671 hammer2_inode_t *scan2;
1674 * We used to have a depth field but it complicated matters too
1675 * much for directory renames. So now its ugly. Check for
1676 * simple cases before giving up and doing it the expensive way.
1678 * XXX need a bottom-up topology stability lock
1680 if (fdip == tdip || fdip == tdip->pip) {
1681 hammer2_inode_ref(fdip);
1684 if (fdip->pip == tdip) {
1685 hammer2_inode_ref(tdip);
1692 for (scan1 = fdip; scan1->pmp == fdip->pmp; scan1 = scan1->pip) {
1694 while (scan2->pmp == tdip->pmp) {
1695 if (scan1 == scan2) {
1696 hammer2_inode_ref(scan1);
1704 panic("hammer2_inode_common_parent: no common parent %p %p\n",
1711 * Synchronize the inode's frontend state with the chain state prior
1712 * to any explicit flush of the inode or any strategy write call.
1714 * Called with a locked inode.
1717 hammer2_inode_fsync(hammer2_trans_t *trans, hammer2_inode_t *ip,
1718 hammer2_chain_t **chainp)
1720 hammer2_inode_data_t *ipdata;
1721 hammer2_chain_t *parent;
1722 hammer2_chain_t *chain;
1723 hammer2_key_t lbase;
1724 hammer2_key_t key_next;
1727 ipdata = &ip->chain->data->ipdata;
1729 if (ip->flags & HAMMER2_INODE_MTIME) {
1730 ipdata = hammer2_chain_modify_ip(trans, ip, chainp, 0);
1731 atomic_clear_int(&ip->flags, HAMMER2_INODE_MTIME);
1732 ipdata->mtime = ip->mtime;
1734 if ((ip->flags & HAMMER2_INODE_RESIZED) && ip->size < ipdata->size) {
1735 ipdata = hammer2_chain_modify_ip(trans, ip, chainp, 0);
1736 ipdata->size = ip->size;
1737 atomic_clear_int(&ip->flags, HAMMER2_INODE_RESIZED);
1740 * We must delete any chains beyond the EOF. The chain
1741 * straddling the EOF will be pending in the bioq.
1743 lbase = (ipdata->size + HAMMER2_PBUFMASK64) &
1744 ~HAMMER2_PBUFMASK64;
1745 parent = hammer2_chain_lookup_init(ip->chain, 0);
1746 chain = hammer2_chain_lookup(&parent, &key_next,
1747 lbase, (hammer2_key_t)-1,
1749 HAMMER2_LOOKUP_NODATA);
1752 * Degenerate embedded case, nothing to loop on
1754 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
1755 hammer2_chain_unlock(chain);
1758 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
1759 hammer2_chain_delete(trans, chain, 0);
1761 chain = hammer2_chain_next(&parent, chain, &key_next,
1762 key_next, (hammer2_key_t)-1,
1764 HAMMER2_LOOKUP_NODATA);
1766 hammer2_chain_lookup_done(parent);
1768 if ((ip->flags & HAMMER2_INODE_RESIZED) && ip->size > ipdata->size) {
1769 ipdata = hammer2_chain_modify_ip(trans, ip, chainp, 0);
1770 ipdata->size = ip->size;
1771 atomic_clear_int(&ip->flags, HAMMER2_INODE_RESIZED);
1774 * When resizing larger we may not have any direct-data
1777 if ((ipdata->op_flags & HAMMER2_OPFLAG_DIRECTDATA) &&
1778 ip->size > HAMMER2_EMBEDDED_BYTES) {
1779 ipdata->op_flags &= ~HAMMER2_OPFLAG_DIRECTDATA;
1780 bzero(&ipdata->u.blockset, sizeof(ipdata->u.blockset));