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 error = hammer2_hardlink_find(ip->pip, &chain, &ochain);
108 hammer2_chain_drop(ochain);
109 KKASSERT(error == 0);
110 /* XXX error handling */
116 hammer2_inode_unlock_ex(hammer2_inode_t *ip, hammer2_chain_t *chain)
119 * XXX this will catch parent directories too which we don't
123 hammer2_chain_unlock(chain);
124 ccms_thread_unlock(&ip->topo_cst);
125 hammer2_inode_drop(ip);
129 * NOTE: We don't combine the inode/chain lock because putting away an
130 * inode would otherwise confuse multiple lock holders of the inode.
132 * Shared locks are especially sensitive to having too many shared
133 * lock counts (from the same thread) on certain paths which might
134 * need to upgrade them. Only one count of a shared lock can be
138 hammer2_inode_lock_sh(hammer2_inode_t *ip)
140 hammer2_chain_t *chain;
142 hammer2_inode_ref(ip);
144 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_SHARED);
147 KKASSERT(chain != NULL); /* for now */
148 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS |
149 HAMMER2_RESOLVE_SHARED);
152 * Resolve duplication races, resolve hardlinks by giving
153 * up and cycling an exclusive lock.
155 if ((chain->flags & HAMMER2_CHAIN_DUPLICATED) == 0 &&
156 chain->data->ipdata.type != HAMMER2_OBJTYPE_HARDLINK) {
159 hammer2_chain_unlock(chain);
160 ccms_thread_unlock(&ip->topo_cst);
161 chain = hammer2_inode_lock_ex(ip);
162 hammer2_inode_unlock_ex(ip, chain);
168 hammer2_inode_unlock_sh(hammer2_inode_t *ip, hammer2_chain_t *chain)
171 hammer2_chain_unlock(chain);
172 ccms_thread_unlock(&ip->topo_cst);
173 hammer2_inode_drop(ip);
177 hammer2_inode_lock_temp_release(hammer2_inode_t *ip)
179 return(ccms_thread_lock_temp_release(&ip->topo_cst));
183 hammer2_inode_lock_temp_restore(hammer2_inode_t *ip, ccms_state_t ostate)
185 ccms_thread_lock_temp_restore(&ip->topo_cst, ostate);
189 hammer2_inode_lock_upgrade(hammer2_inode_t *ip)
191 return(ccms_thread_lock_upgrade(&ip->topo_cst));
195 hammer2_inode_lock_downgrade(hammer2_inode_t *ip, ccms_state_t ostate)
197 ccms_thread_lock_downgrade(&ip->topo_cst, ostate);
201 * Lookup an inode by inode number
204 hammer2_inode_lookup(hammer2_pfsmount_t *pmp, hammer2_tid_t inum)
209 spin_lock(&pmp->inum_spin);
210 ip = RB_LOOKUP(hammer2_inode_tree, &pmp->inum_tree, inum);
212 hammer2_inode_ref(ip);
213 spin_unlock(&pmp->inum_spin);
221 * Adding a ref to an inode is only legal if the inode already has at least
225 hammer2_inode_ref(hammer2_inode_t *ip)
227 atomic_add_int(&ip->refs, 1);
231 * Drop an inode reference, freeing the inode when the last reference goes
235 hammer2_inode_drop(hammer2_inode_t *ip)
237 hammer2_pfsmount_t *pmp;
238 hammer2_inode_t *pip;
246 * Transition to zero, must interlock with
247 * the inode inumber lookup tree (if applicable).
249 * NOTE: The super-root inode has no pmp.
253 spin_lock(&pmp->inum_spin);
255 if (atomic_cmpset_int(&ip->refs, 1, 0)) {
256 KKASSERT(ip->topo_cst.count == 0);
257 if (ip->flags & HAMMER2_INODE_ONRBTREE) {
258 atomic_clear_int(&ip->flags,
259 HAMMER2_INODE_ONRBTREE);
260 RB_REMOVE(hammer2_inode_tree,
261 &pmp->inum_tree, ip);
264 spin_unlock(&pmp->inum_spin);
271 * Cleaning out ip->chain isn't entirely
274 hammer2_inode_repoint(ip, NULL, NULL);
277 * We have to drop pip (if non-NULL) to
278 * dispose of our implied reference from
279 * ip->pip. We can simply loop on it.
282 KKASSERT((ip->flags &
283 HAMMER2_INODE_SROOT) == 0);
284 kfree(ip, pmp->minode);
285 atomic_add_long(&pmp->inmem_inodes, -1);
288 HAMMER2_INODE_SROOT);
289 kfree(ip, M_HAMMER2);
292 /* continue with pip (can be NULL) */
295 spin_unlock(&ip->pmp->inum_spin);
299 * Non zero transition
301 if (atomic_cmpset_int(&ip->refs, refs, refs - 1))
308 * Get the vnode associated with the given inode, allocating the vnode if
309 * necessary. The vnode will be returned exclusively locked.
311 * The caller must lock the inode (shared or exclusive).
313 * Great care must be taken to avoid deadlocks and vnode acquisition/reclaim
317 hammer2_igetv(hammer2_inode_t *ip, int *errorp)
319 hammer2_inode_data_t *ipdata;
320 hammer2_pfsmount_t *pmp;
325 KKASSERT(pmp != NULL);
327 ipdata = &ip->chain->data->ipdata;
331 * Attempt to reuse an existing vnode assignment. It is
332 * possible to race a reclaim so the vget() may fail. The
333 * inode must be unlocked during the vget() to avoid a
334 * deadlock against a reclaim.
339 * Inode must be unlocked during the vget() to avoid
340 * possible deadlocks, but leave the ip ref intact.
342 * vnode is held to prevent destruction during the
343 * vget(). The vget() can still fail if we lost
344 * a reclaim race on the vnode.
347 ostate = hammer2_inode_lock_temp_release(ip);
348 if (vget(vp, LK_EXCLUSIVE)) {
350 hammer2_inode_lock_temp_restore(ip, ostate);
353 hammer2_inode_lock_temp_restore(ip, ostate);
355 /* vp still locked and ref from vget */
357 kprintf("hammer2: igetv race %p/%p\n",
367 * No vnode exists, allocate a new vnode. Beware of
368 * allocation races. This function will return an
369 * exclusively locked and referenced vnode.
371 *errorp = getnewvnode(VT_HAMMER2, pmp->mp, &vp, 0, 0);
373 kprintf("hammer2: igetv getnewvnode failed %d\n",
380 * Lock the inode and check for an allocation race.
382 ostate = hammer2_inode_lock_upgrade(ip);
383 if (ip->vp != NULL) {
386 hammer2_inode_lock_downgrade(ip, ostate);
390 switch (ipdata->type) {
391 case HAMMER2_OBJTYPE_DIRECTORY:
394 case HAMMER2_OBJTYPE_REGFILE:
396 vinitvmio(vp, ipdata->size,
398 (int)ipdata->size & HAMMER2_LBUFMASK);
400 case HAMMER2_OBJTYPE_SOFTLINK:
402 * XXX for now we are using the generic file_read
403 * and file_write code so we need a buffer cache
407 vinitvmio(vp, ipdata->size,
409 (int)ipdata->size & HAMMER2_LBUFMASK);
411 case HAMMER2_OBJTYPE_CDEV:
414 case HAMMER2_OBJTYPE_BDEV:
415 vp->v_ops = &pmp->mp->mnt_vn_spec_ops;
416 if (ipdata->type != HAMMER2_OBJTYPE_CDEV)
418 addaliasu(vp, ipdata->rmajor, ipdata->rminor);
420 case HAMMER2_OBJTYPE_FIFO:
422 vp->v_ops = &pmp->mp->mnt_vn_fifo_ops;
425 panic("hammer2: unhandled objtype %d", ipdata->type);
429 if (ip == pmp->iroot)
430 vsetflags(vp, VROOT);
434 hammer2_inode_ref(ip); /* vp association */
435 hammer2_inode_lock_downgrade(ip, ostate);
440 * Return non-NULL vp and *errorp == 0, or NULL vp and *errorp != 0.
442 if (hammer2_debug & 0x0002) {
443 kprintf("igetv vp %p refs 0x%08x aux 0x%08x\n",
444 vp, vp->v_refcnt, vp->v_auxrefs);
450 * The passed-in chain must be locked and the returned inode will also be
451 * locked. This routine typically locates or allocates the inode, assigns
452 * ip->chain (adding a ref to chain if necessary), and returns the inode.
454 * The hammer2_inode structure regulates the interface between the high level
455 * kernel VNOPS API and the filesystem backend (the chains).
457 * WARNING! This routine sucks up the chain's lock (makes it part of the
458 * inode lock from the point of view of the inode lock API),
459 * so callers need to be careful.
461 * WARNING! The mount code is allowed to pass dip == NULL for iroot and
462 * is allowed to pass pmp == NULL and dip == NULL for sroot.
465 hammer2_inode_get(hammer2_pfsmount_t *pmp, hammer2_inode_t *dip,
466 hammer2_chain_t *chain)
468 hammer2_inode_t *nip;
470 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_INODE);
473 * Interlocked lookup/ref of the inode. This code is only needed
474 * when looking up inodes with nlinks != 0 (TODO: optimize out
475 * otherwise and test for duplicates).
479 nip = hammer2_inode_lookup(pmp, chain->data->ipdata.inum);
482 ccms_thread_lock(&nip->topo_cst, CCMS_STATE_EXCLUSIVE);
483 if ((nip->flags & HAMMER2_INODE_ONRBTREE) == 0) { /* race */
484 ccms_thread_unlock(&nip->topo_cst);
485 hammer2_inode_drop(nip);
488 if (nip->chain != chain)
489 hammer2_inode_repoint(nip, NULL, chain);
492 * Consolidated nip/nip->chain is locked (chain locked
499 * We couldn't find the inode number, create a new inode.
502 nip = kmalloc(sizeof(*nip), pmp->minode, M_WAITOK | M_ZERO);
503 atomic_add_long(&pmp->inmem_inodes, 1);
504 hammer2_chain_memory_inc(pmp);
505 hammer2_chain_memory_wakeup(pmp);
507 nip = kmalloc(sizeof(*nip), M_HAMMER2, M_WAITOK | M_ZERO);
508 nip->flags = HAMMER2_INODE_SROOT;
510 nip->inum = chain->data->ipdata.inum;
511 nip->size = chain->data->ipdata.size;
512 nip->mtime = chain->data->ipdata.mtime;
513 hammer2_inode_repoint(nip, NULL, chain);
514 nip->pip = dip; /* can be NULL */
516 hammer2_inode_ref(dip); /* ref dip for nip->pip */
521 * ref and lock on nip gives it state compatible to after a
522 * hammer2_inode_lock_ex() call.
525 ccms_cst_init(&nip->topo_cst, &nip->chain);
526 ccms_thread_lock(&nip->topo_cst, CCMS_STATE_EXCLUSIVE);
527 /* combination of thread lock and chain lock == inode lock */
530 * Attempt to add the inode. If it fails we raced another inode
531 * get. Undo all the work and try again.
534 spin_lock(&pmp->inum_spin);
535 if (RB_INSERT(hammer2_inode_tree, &pmp->inum_tree, nip)) {
536 spin_unlock(&pmp->inum_spin);
537 ccms_thread_unlock(&nip->topo_cst);
538 hammer2_inode_drop(nip);
541 atomic_set_int(&nip->flags, HAMMER2_INODE_ONRBTREE);
542 spin_unlock(&pmp->inum_spin);
549 * Create a new inode in the specified directory using the vattr to
550 * figure out the type of inode.
552 * If no error occurs the new inode with its chain locked is returned in
553 * *nipp, otherwise an error is returned and *nipp is set to NULL.
555 * If vap and/or cred are NULL the related fields are not set and the
556 * inode type defaults to a directory. This is used when creating PFSs
557 * under the super-root, so the inode number is set to 1 in this case.
559 * dip is not locked on entry.
562 hammer2_inode_create(hammer2_trans_t *trans, hammer2_inode_t *dip,
563 struct vattr *vap, struct ucred *cred,
564 const uint8_t *name, size_t name_len,
565 hammer2_chain_t **chainp, int *errorp)
567 hammer2_inode_data_t *dipdata;
568 hammer2_inode_data_t *nipdata;
569 hammer2_chain_t *chain;
570 hammer2_chain_t *parent;
571 hammer2_inode_t *nip;
572 hammer2_key_t key_dummy;
580 int cache_index = -1;
582 lhc = hammer2_dirhash(name, name_len);
586 * Locate the inode or indirect block to create the new
587 * entry in. At the same time check for key collisions
588 * and iterate until we don't get one.
590 * NOTE: hidden inodes do not have iterators.
593 parent = hammer2_inode_lock_ex(dip);
594 dipdata = &dip->chain->data->ipdata;
595 dip_uid = dipdata->uid;
596 dip_gid = dipdata->gid;
597 dip_mode = dipdata->mode;
598 dip_algo = dipdata->comp_algo;
602 chain = hammer2_chain_lookup(&parent, &key_dummy,
603 lhc, lhc, &cache_index, 0);
606 if ((lhc & HAMMER2_DIRHASH_VISIBLE) == 0)
608 if ((lhc & HAMMER2_DIRHASH_LOMASK) == HAMMER2_DIRHASH_LOMASK)
610 hammer2_chain_unlock(chain);
616 error = hammer2_chain_create(trans, &parent, &chain,
618 HAMMER2_BREF_TYPE_INODE,
619 HAMMER2_INODE_BYTES);
623 * Cleanup and handle retries.
625 if (error == EAGAIN) {
626 hammer2_chain_ref(parent);
627 hammer2_inode_unlock_ex(dip, parent);
628 hammer2_chain_wait(parent);
629 hammer2_chain_drop(parent);
632 hammer2_inode_unlock_ex(dip, parent);
635 KKASSERT(chain == NULL);
641 * Set up the new inode.
643 * NOTE: *_get() integrates chain's lock into the inode lock.
645 * NOTE: Only one new inode can currently be created per
646 * transaction. If the need arises we can adjust
647 * hammer2_trans_init() to allow more.
649 * NOTE: nipdata will have chain's blockset data.
651 chain->data->ipdata.inum = trans->inode_tid;
652 nip = hammer2_inode_get(dip->pmp, dip, chain);
653 nipdata = &chain->data->ipdata;
656 KKASSERT(trans->inodes_created == 0);
657 nipdata->type = hammer2_get_obj_type(vap->va_type);
658 nipdata->inum = trans->inode_tid;
659 ++trans->inodes_created;
661 switch (nipdata->type) {
662 case HAMMER2_OBJTYPE_CDEV:
663 case HAMMER2_OBJTYPE_BDEV:
664 nipdata->rmajor = vap->va_rmajor;
665 nipdata->rminor = vap->va_rminor;
671 nipdata->type = HAMMER2_OBJTYPE_DIRECTORY;
675 /* Inherit parent's inode compression mode. */
676 nip->comp_heuristic = 0;
677 nipdata->comp_algo = dip_algo;
678 nipdata->version = HAMMER2_INODE_VERSION_ONE;
679 hammer2_update_time(&nipdata->ctime);
680 nipdata->mtime = nipdata->ctime;
682 nipdata->mode = vap->va_mode;
685 if (dip && dip->pmp) {
686 xuid = hammer2_to_unix_xid(&dip_uid);
687 xuid = vop_helper_create_uid(dip->pmp->mp,
693 /* super-root has no dip and/or pmp */
696 if (vap->va_vaflags & VA_UID_UUID_VALID)
697 nipdata->uid = vap->va_uid_uuid;
698 else if (vap->va_uid != (uid_t)VNOVAL)
699 hammer2_guid_to_uuid(&nipdata->uid, vap->va_uid);
701 hammer2_guid_to_uuid(&nipdata->uid, xuid);
703 if (vap->va_vaflags & VA_GID_UUID_VALID)
704 nipdata->gid = vap->va_gid_uuid;
705 else if (vap->va_gid != (gid_t)VNOVAL)
706 hammer2_guid_to_uuid(&nipdata->gid, vap->va_gid);
708 nipdata->gid = dip_gid;
712 * Regular files and softlinks allow a small amount of data to be
713 * directly embedded in the inode. This flag will be cleared if
714 * the size is extended past the embedded limit.
716 if (nipdata->type == HAMMER2_OBJTYPE_REGFILE ||
717 nipdata->type == HAMMER2_OBJTYPE_SOFTLINK) {
718 nipdata->op_flags |= HAMMER2_OPFLAG_DIRECTDATA;
721 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
722 bcopy(name, nipdata->filename, name_len);
723 nipdata->name_key = lhc;
724 nipdata->name_len = name_len;
731 * chain may have been moved around by the create.
734 hammer2_chain_refactor(hammer2_chain_t **chainp)
736 hammer2_chain_t *chain = *chainp;
737 hammer2_chain_core_t *core;
740 while (chain->flags & HAMMER2_CHAIN_DUPLICATED) {
741 spin_lock(&core->cst.spin);
742 chain = TAILQ_NEXT(chain, core_entry);
743 while (chain->flags & HAMMER2_CHAIN_DUPLICATED)
744 chain = TAILQ_NEXT(chain, core_entry);
745 hammer2_chain_ref(chain);
746 spin_unlock(&core->cst.spin);
747 KKASSERT(chain->core == core);
749 hammer2_chain_unlock(*chainp);
750 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS |
751 HAMMER2_RESOLVE_NOREF); /* eat ref */
757 * Shift *chainp up to the specified directory, change the filename
758 * to "0xINODENUMBER", and adjust the key. The chain becomes the
759 * invisible hardlink target.
761 * The original *chainp has already been marked deleted.
765 hammer2_hardlink_shiftup(hammer2_trans_t *trans, hammer2_chain_t **chainp,
766 hammer2_inode_t *dip, int nlinks, int *errorp)
768 hammer2_inode_data_t *nipdata;
769 hammer2_chain_t *parent;
770 hammer2_chain_t *chain;
771 hammer2_chain_t *xchain;
772 hammer2_key_t key_dummy;
774 hammer2_blockref_t bref;
775 int cache_index = -1;
778 lhc = chain->data->ipdata.inum;
779 KKASSERT((lhc & HAMMER2_DIRHASH_VISIBLE) == 0);
782 * Locate the inode or indirect block to create the new
783 * entry in. lhc represents the inode number so there is
784 * no collision iteration.
786 * There should be no key collisions with invisible inode keys.
788 * WARNING! Must use inode_lock_ex() on dip to handle a stale
793 parent = hammer2_inode_lock_ex(dip);
794 /*parent = hammer2_chain_lookup_init(dip->chain, 0);*/
795 xchain = hammer2_chain_lookup(&parent, &key_dummy,
796 lhc, lhc, &cache_index, 0);
798 kprintf("X3 chain %p parent %p dip %p dip->chain %p\n",
799 xchain, parent, dip, dip->chain);
800 hammer2_chain_unlock(xchain);
809 * Create entry in common parent directory using the seek position
812 * We must refactor chain because it might have been shifted into
813 * an indirect chain by the create.
816 KKASSERT(xchain == NULL);
818 *errorp = hammer2_chain_create(trans, &parent, &xchain,
820 HAMMER2_BREF_TYPE_INODE,/* n/a */
821 HAMMER2_INODE_BYTES); /* n/a */
823 /*XXX this somehow isn't working on chain XXX*/
828 * Cleanup and handle retries.
830 if (*errorp == EAGAIN) {
831 hammer2_chain_ref(parent);
832 /* hammer2_chain_lookup_done(parent); */
833 hammer2_inode_unlock_ex(dip, parent);
834 hammer2_chain_wait(parent);
835 hammer2_chain_drop(parent);
840 * Handle the error case
844 KKASSERT(xchain == NULL);
845 hammer2_inode_unlock_ex(dip, parent);
846 /*hammer2_chain_lookup_done(parent);*/
851 * Use xchain as a placeholder for (lhc). Duplicate chain to the
852 * same target bref as xchain and then delete xchain. The duplication
853 * occurs after xchain in flush order even though xchain is deleted
854 * after the duplication. XXX
856 * WARNING! Duplications (to a different parent) can cause indirect
857 * blocks to be inserted, refactor xchain.
860 bref.key = lhc; /* invisible dir entry key */
863 hammer2_chain_delete(trans, xchain, 0);
865 hammer2_chain_duplicate(trans, &parent, &chain, &bref, 0, 2);
867 hammer2_chain_refactor(&xchain);
868 /*hammer2_chain_delete(trans, xchain, 0);*/
871 hammer2_inode_unlock_ex(dip, parent);
872 /*hammer2_chain_lookup_done(parent);*/
874 hammer2_chain_unlock(xchain); /* no longer needed */
878 * chain is now 'live' again.. adjust the filename.
880 * Directory entries are inodes but this is a hidden hardlink
881 * target. The name isn't used but to ease debugging give it
882 * a name after its inode number.
884 hammer2_chain_modify(trans, &chain, 0);
885 nipdata = &chain->data->ipdata;
886 ksnprintf(nipdata->filename, sizeof(nipdata->filename),
887 "0x%016jx", (intmax_t)nipdata->inum);
888 nipdata->name_len = strlen(nipdata->filename);
889 nipdata->name_key = lhc;
890 nipdata->nlinks += nlinks;
896 * Connect the target inode represented by (*chainp) to the media topology
897 * at (dip, name, len).
899 * If hlink is TRUE this function creates an OBJTYPE_HARDLINK directory
900 * entry instead of connecting (*chainp).
902 * If hlink is FALSE this function uses chain_duplicate() to make a copy
903 * if (*chainp) in the directory entry. (*chainp) is likely to be deleted
904 * by the caller in this case (e.g. rename).
907 hammer2_inode_connect(hammer2_trans_t *trans, int hlink,
908 hammer2_inode_t *dip, hammer2_chain_t **chainp,
909 const uint8_t *name, size_t name_len,
912 hammer2_inode_data_t *ipdata;
913 hammer2_chain_t *nchain;
914 hammer2_chain_t *parent;
915 hammer2_chain_t *ochain;
916 hammer2_key_t key_dummy;
917 int cache_index = -1;
921 * Since ochain is either disconnected from the topology or represents
922 * a hardlink terminus which is always a parent of or equal to dip,
923 * we should be able to safely lock dip->chain for our setup.
925 * WARNING! Must use inode_lock_ex() on dip to handle a stale
929 parent = hammer2_inode_lock_ex(dip);
930 /*parent = hammer2_chain_lookup_init(dip->chain, 0);*/
933 * If name is non-NULL we calculate lhc, else we use the passed-in
937 lhc = hammer2_dirhash(name, name_len);
940 * Locate the inode or indirect block to create the new
941 * entry in. At the same time check for key collisions
942 * and iterate until we don't get one.
946 nchain = hammer2_chain_lookup(&parent, &key_dummy,
951 if ((lhc & HAMMER2_DIRHASH_LOMASK) ==
952 HAMMER2_DIRHASH_LOMASK) {
955 hammer2_chain_unlock(nchain);
964 * Hardlink pointer needed, create totally fresh
967 * We must refactor ochain because it might have
968 * been shifted into an indirect chain by the
971 KKASSERT(nchain == NULL);
972 error = hammer2_chain_create(trans, &parent, &nchain,
974 HAMMER2_BREF_TYPE_INODE,
975 HAMMER2_INODE_BYTES);
976 hammer2_chain_refactor(&ochain);
979 * Reconnect the original chain and rename. Use
980 * chain_duplicate(). The caller will likely delete
981 * or has already deleted the original chain in
984 * NOTE: chain_duplicate() generates a new chain
985 * with CHAIN_DELETED cleared (ochain typically
986 * has it set from the file unlink).
988 * WARNING! Can cause held-over chains to require a
989 * refactor. Fortunately we have none (our
990 * locked chains are passed into and
991 * modified by the call).
995 hammer2_chain_duplicate(trans, NULL, &nchain, NULL,
997 error = hammer2_chain_create(trans, &parent, &nchain,
999 HAMMER2_BREF_TYPE_INODE,
1000 HAMMER2_INODE_BYTES);
1007 KKASSERT(error != EAGAIN);
1008 hammer2_inode_unlock_ex(dip, parent);
1009 /*hammer2_chain_lookup_done(parent);*/
1013 * nchain should be NULL on error, leave ochain (== *chainp) alone.
1016 KKASSERT(nchain == NULL);
1021 * Directory entries are inodes so if the name has changed we have
1022 * to update the inode.
1024 * When creating an OBJTYPE_HARDLINK entry remember to unlock the
1025 * chain, the caller will access the hardlink via the actual hardlink
1026 * target file and not the hardlink pointer entry, so we must still
1029 if (hlink && hammer2_hardlink_enable >= 0) {
1031 * Create the HARDLINK pointer. oip represents the hardlink
1032 * target in this situation.
1034 * We will return ochain (the hardlink target).
1036 hammer2_chain_modify(trans, &nchain, 0);
1037 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
1038 ipdata = &nchain->data->ipdata;
1039 bcopy(name, ipdata->filename, name_len);
1040 ipdata->name_key = lhc;
1041 ipdata->name_len = name_len;
1042 ipdata->target_type = ochain->data->ipdata.type;
1043 ipdata->type = HAMMER2_OBJTYPE_HARDLINK;
1044 ipdata->inum = ochain->data->ipdata.inum;
1046 hammer2_chain_unlock(nchain);
1049 } else if (hlink && hammer2_hardlink_enable < 0) {
1051 * Create a snapshot (hardlink fake mode for debugging).
1052 * (ochain already flushed above so we can just copy the
1055 * Since this is a snapshot we return nchain in the fake
1058 hammer2_chain_modify(trans, &nchain, 0);
1059 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
1060 ipdata = &nchain->data->ipdata;
1061 *ipdata = ochain->data->ipdata;
1062 bcopy(name, ipdata->filename, name_len);
1063 ipdata->name_key = lhc;
1064 ipdata->name_len = name_len;
1065 atomic_clear_int(&nchain->core->flags,
1066 HAMMER2_CORE_COUNTEDBREFS);
1067 kprintf("created fake hardlink %*.*s\n",
1068 (int)name_len, (int)name_len, name);
1071 * nchain is a duplicate of ochain at the new location.
1072 * We must fixup the name stored in oip. The bref key
1073 * has already been set up.
1075 hammer2_chain_modify(trans, &nchain, 0);
1076 ipdata = &nchain->data->ipdata;
1078 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
1079 bcopy(name, ipdata->filename, name_len);
1080 ipdata->name_key = lhc;
1081 ipdata->name_len = name_len;
1086 * We are replacing ochain with nchain, unlock ochain. In the
1087 * case where ochain is left unchanged the code above sets
1088 * nchain to ochain and ochain to NULL, resulting in a NOP here.
1091 hammer2_chain_unlock(ochain);
1098 * Repoint ip->chain to nchain. Caller must hold the inode exclusively
1101 * ip->chain is set to nchain. The prior chain in ip->chain is dropped
1102 * and nchain is ref'd.
1105 hammer2_inode_repoint(hammer2_inode_t *ip, hammer2_inode_t *pip,
1106 hammer2_chain_t *nchain)
1108 hammer2_chain_t *ochain;
1109 hammer2_inode_t *opip;
1112 * Repoint ip->chain if requested.
1117 hammer2_chain_ref(nchain);
1119 hammer2_chain_drop(ochain);
1122 * Repoint ip->pip if requested (non-NULL pip).
1124 if (pip && ip->pip != pip) {
1126 hammer2_inode_ref(pip);
1129 hammer2_inode_drop(opip);
1134 * Unlink the file from the specified directory inode. The directory inode
1135 * does not need to be locked.
1137 * isdir determines whether a directory/non-directory check should be made.
1138 * No check is made if isdir is set to -1.
1140 * isopen specifies whether special unlink-with-open-descriptor handling
1141 * must be performed. If set to -1 the caller is deleting a PFS and we
1142 * check whether the chain is mounted or not (chain->pmp != NULL). 1 is
1143 * implied if it is mounted.
1145 * If isopen is 1 and nlinks drops to 0 this function must move the chain
1146 * to a special hidden directory until last-close occurs on the file.
1148 * NOTE! The underlying file can still be active with open descriptors
1149 * or if the chain is being manually held (e.g. for rename).
1151 * The caller is responsible for fixing up ip->chain if e.g. a
1152 * rename occurs (see chain_duplicate()).
1155 hammer2_unlink_file(hammer2_trans_t *trans, hammer2_inode_t *dip,
1156 const uint8_t *name, size_t name_len,
1157 int isdir, int *hlinkp, struct nchandle *nch)
1159 hammer2_inode_data_t *ipdata;
1160 hammer2_chain_t *parent;
1161 hammer2_chain_t *ochain;
1162 hammer2_chain_t *chain;
1163 hammer2_chain_t *dparent;
1164 hammer2_chain_t *dchain;
1165 hammer2_key_t key_dummy;
1166 hammer2_key_t key_next;
1169 int cache_index = -1;
1174 lhc = hammer2_dirhash(name, name_len);
1177 * Search for the filename in the directory
1181 parent = hammer2_inode_lock_ex(dip);
1182 chain = hammer2_chain_lookup(&parent, &key_next,
1183 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1186 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
1187 name_len == chain->data->ipdata.name_len &&
1188 bcmp(name, chain->data->ipdata.filename, name_len) == 0) {
1191 chain = hammer2_chain_next(&parent, chain, &key_next,
1193 lhc + HAMMER2_DIRHASH_LOMASK,
1196 hammer2_inode_unlock_ex(dip, NULL); /* retain parent */
1199 * Not found or wrong type (isdir < 0 disables the type check).
1200 * If a hardlink pointer, type checks use the hardlink target.
1202 if (chain == NULL) {
1206 if ((type = chain->data->ipdata.type) == HAMMER2_OBJTYPE_HARDLINK) {
1209 type = chain->data->ipdata.target_type;
1212 if (type == HAMMER2_OBJTYPE_DIRECTORY && isdir == 0) {
1216 if (type != HAMMER2_OBJTYPE_DIRECTORY && isdir >= 1) {
1222 * Hardlink must be resolved. We can't hold parent locked while we
1223 * do this or we could deadlock.
1225 * On success chain will be adjusted to point at the hardlink target
1226 * and ochain will point to the hardlink pointer in the original
1227 * directory. Otherwise chain remains pointing to the original.
1229 if (chain->data->ipdata.type == HAMMER2_OBJTYPE_HARDLINK) {
1230 hammer2_chain_unlock(parent);
1232 error = hammer2_hardlink_find(dip, &chain, &ochain);
1236 * If this is a directory the directory must be empty. However, if
1237 * isdir < 0 we are doing a rename and the directory does not have
1238 * to be empty, and if isdir > 1 we are deleting a PFS/snapshot
1239 * and the directory does not have to be empty.
1241 * NOTE: We check the full key range here which covers both visible
1242 * and invisible entries. Theoretically there should be no
1243 * invisible (hardlink target) entries if there are no visible
1246 if (type == HAMMER2_OBJTYPE_DIRECTORY && isdir == 1) {
1247 dparent = hammer2_chain_lookup_init(chain, 0);
1248 dchain = hammer2_chain_lookup(&dparent, &key_dummy,
1249 0, (hammer2_key_t)-1,
1251 HAMMER2_LOOKUP_NODATA);
1253 hammer2_chain_unlock(dchain);
1254 hammer2_chain_lookup_done(dparent);
1258 hammer2_chain_lookup_done(dparent);
1264 * Ok, we can now unlink the chain. We always decrement nlinks even
1265 * if the entry can be deleted in case someone has the file open and
1268 * The chain itself will no longer be in the on-media topology but
1269 * can still be flushed to the media (e.g. if an open descriptor
1270 * remains). When the last vnode/ip ref goes away the chain will
1271 * be marked unmodified, avoiding any further (now unnecesary) I/O.
1273 * A non-NULL ochain indicates a hardlink.
1277 * Delete the original hardlink pointer unconditionally.
1278 * (any open descriptors will migrate to the hardlink
1279 * target and have no affect on this operation).
1281 * NOTE: parent from above is NULL when ochain != NULL
1282 * so we can reuse it.
1284 hammer2_chain_lock(ochain, HAMMER2_RESOLVE_ALWAYS);
1285 hammer2_chain_delete(trans, ochain, 0);
1286 hammer2_chain_unlock(ochain);
1290 * Decrement nlinks on the hardlink target (or original file if
1291 * there it was not hardlinked). Delete the target when nlinks
1292 * reaches 0 with special handling if (isopen) is set.
1294 * NOTE! In DragonFly the vnops function calls cache_unlink() after
1295 * calling us here to clean out the namecache association,
1296 * (which does not represent a ref for the open-test), and to
1297 * force finalization of the vnode if/when the last ref gets
1300 hammer2_chain_modify(trans, &chain, 0);
1301 ipdata = &chain->data->ipdata;
1303 kprintf("file %s nlinks %ld\n", ipdata->filename, ipdata->nlinks);
1304 if ((int64_t)ipdata->nlinks < 0) /* XXX debugging */
1306 if (ipdata->nlinks == 0) {
1307 if ((chain->flags & HAMMER2_CHAIN_PFSROOT) && chain->pmp) {
1309 kprintf("hammer2: PFS \"%s\" cannot be deleted "
1310 "while still mounted\n",
1314 if (nch && cache_isopen(nch)) {
1315 kprintf("WARNING: unlinking open file\n");
1316 atomic_set_int(&chain->flags, HAMMER2_CHAIN_UNLINKED);
1317 hammer2_inode_move_to_hidden(trans, &chain,
1320 hammer2_chain_delete(trans, chain, 0);
1326 hammer2_chain_unlock(chain);
1328 hammer2_chain_lookup_done(parent);
1330 hammer2_chain_drop(ochain);
1336 * This is called from the mount code to initialize pmp->ihidden
1339 hammer2_inode_install_hidden(hammer2_pfsmount_t *pmp)
1341 hammer2_trans_t trans;
1342 hammer2_chain_t *parent;
1343 hammer2_chain_t *chain;
1344 hammer2_chain_t *scan;
1345 hammer2_inode_data_t *ipdata;
1346 hammer2_key_t key_dummy;
1347 hammer2_key_t key_next;
1356 * Find the hidden directory
1358 bzero(&key_dummy, sizeof(key_dummy));
1359 hammer2_trans_init(&trans, pmp, NULL, 0);
1361 parent = hammer2_inode_lock_ex(pmp->iroot);
1362 chain = hammer2_chain_lookup(&parent, &key_dummy,
1363 HAMMER2_INODE_HIDDENDIR,
1364 HAMMER2_INODE_HIDDENDIR,
1367 pmp->ihidden = hammer2_inode_get(pmp, pmp->iroot, chain);
1368 hammer2_inode_ref(pmp->ihidden);
1371 * Remove any unlinked files which were left open as-of
1375 scan = hammer2_chain_lookup(&chain, &key_next,
1378 HAMMER2_LOOKUP_NODATA);
1380 if (scan->bref.type == HAMMER2_BREF_TYPE_INODE) {
1381 hammer2_chain_delete(&trans, scan, 0);
1384 scan = hammer2_chain_next(&chain, scan, &key_next,
1387 HAMMER2_LOOKUP_NODATA);
1390 hammer2_inode_unlock_ex(pmp->ihidden, chain);
1391 hammer2_inode_unlock_ex(pmp->iroot, parent);
1392 hammer2_trans_done(&trans);
1393 kprintf("hammer2: PFS loaded hidden dir, "
1394 "removed %d dead entries\n", count);
1399 * Create the hidden directory
1401 error = hammer2_chain_create(&trans, &parent, &chain,
1402 HAMMER2_INODE_HIDDENDIR, 0,
1403 HAMMER2_BREF_TYPE_INODE,
1404 HAMMER2_INODE_BYTES);
1405 hammer2_inode_unlock_ex(pmp->iroot, parent);
1406 hammer2_chain_modify(&trans, &chain, 0);
1407 ipdata = &chain->data->ipdata;
1408 ipdata->type = HAMMER2_OBJTYPE_DIRECTORY;
1409 ipdata->inum = HAMMER2_INODE_HIDDENDIR;
1411 kprintf("hammer2: PFS root missing hidden directory, creating\n");
1413 pmp->ihidden = hammer2_inode_get(pmp, pmp->iroot, chain);
1414 hammer2_inode_ref(pmp->ihidden);
1415 hammer2_inode_unlock_ex(pmp->ihidden, chain);
1416 hammer2_trans_done(&trans);
1420 * If an open file is unlinked H2 needs to retain the file in the topology
1421 * to ensure that its backing store is not recovered by the bulk free scan.
1422 * This also allows us to avoid having to special-case the CHAIN_DELETED flag.
1424 * To do this the file is moved to a hidden directory in the PFS root and
1425 * renamed. The hidden directory must be created if it does not exist.
1429 hammer2_inode_move_to_hidden(hammer2_trans_t *trans, hammer2_chain_t **chainp,
1432 hammer2_chain_t *chain;
1433 hammer2_pfsmount_t *pmp;
1438 KKASSERT(pmp != NULL);
1439 KKASSERT(pmp->ihidden != NULL);
1441 hammer2_chain_delete(trans, chain, 0);
1442 error = hammer2_inode_connect(trans, 0,
1443 pmp->ihidden, chainp,
1445 KKASSERT(error == 0);
1449 * Given an exclusively locked inode and chain we consolidate its chain
1450 * for hardlink creation, adding (nlinks) to the file's link count and
1451 * potentially relocating the inode to a directory common to ip->pip and tdip.
1453 * Replaces (*chainp) if consolidation occurred, unlocking the old chain
1454 * and returning a new locked chain.
1456 * NOTE! This function will also replace ip->chain.
1459 hammer2_hardlink_consolidate(hammer2_trans_t *trans, hammer2_inode_t *ip,
1460 hammer2_chain_t **chainp,
1461 hammer2_inode_t *tdip, int nlinks)
1463 hammer2_inode_data_t *ipdata;
1464 hammer2_inode_t *fdip;
1465 hammer2_inode_t *cdip;
1466 hammer2_chain_t *chain;
1467 hammer2_chain_t *nchain;
1471 if (nlinks == 0 && /* no hardlink needed */
1472 (chain->data->ipdata.name_key & HAMMER2_DIRHASH_VISIBLE)) {
1475 if (hammer2_hardlink_enable < 0) { /* fake hardlinks */
1479 if (hammer2_hardlink_enable == 0) { /* disallow hardlinks */
1480 hammer2_chain_unlock(chain);
1486 * cdip will be returned with a ref, but not locked.
1489 cdip = hammer2_inode_common_parent(fdip, tdip);
1492 * If no change in the hardlink's target directory is required and
1493 * this is already a hardlink target, all we need to do is adjust
1496 * XXX The common parent is a big wiggly due to duplication from
1497 * renames. Compare the core (RBTREE) pointer instead of the
1501 (chain->data->ipdata.name_key & HAMMER2_DIRHASH_VISIBLE) == 0) {
1503 hammer2_chain_modify(trans, &chain, 0);
1504 chain->data->ipdata.nlinks += nlinks;
1512 * chain is the real inode. If it's visible we have to convert it
1513 * to a hardlink pointer. If it is not visible then it is already
1514 * a hardlink target and only needs to be deleted.
1516 KKASSERT((chain->flags & HAMMER2_CHAIN_DELETED) == 0);
1517 KKASSERT(chain->data->ipdata.type != HAMMER2_OBJTYPE_HARDLINK);
1518 if (chain->data->ipdata.name_key & HAMMER2_DIRHASH_VISIBLE) {
1520 * We are going to duplicate chain later, causing its
1521 * media block to be shifted to the duplicate. Even though
1522 * we are delete-duplicating nchain here it might decide not
1523 * to reallocate the block. Set FORCECOW to force it to.
1526 hammer2_chain_lock(nchain, HAMMER2_RESOLVE_ALWAYS);
1527 atomic_set_int(&nchain->flags, HAMMER2_CHAIN_FORCECOW);
1528 hammer2_chain_delete_duplicate(trans, &nchain,
1529 HAMMER2_DELDUP_RECORE);
1530 KKASSERT((chain->flags & HAMMER2_CHAIN_DUPLICATED) == 0);
1532 ipdata = &nchain->data->ipdata;
1533 ipdata->target_type = ipdata->type;
1534 ipdata->type = HAMMER2_OBJTYPE_HARDLINK;
1542 bzero(&ipdata->uid, sizeof(ipdata->uid));
1543 bzero(&ipdata->gid, sizeof(ipdata->gid));
1544 ipdata->op_flags = HAMMER2_OPFLAG_DIRECTDATA;
1545 ipdata->cap_flags = 0;
1549 ipdata->iparent = 0; /* XXX */
1550 ipdata->pfs_type = 0;
1551 ipdata->pfs_inum = 0;
1552 bzero(&ipdata->pfs_clid, sizeof(ipdata->pfs_clid));
1553 bzero(&ipdata->pfs_fsid, sizeof(ipdata->pfs_fsid));
1554 ipdata->data_quota = 0;
1555 ipdata->data_count = 0;
1556 ipdata->inode_quota = 0;
1557 ipdata->inode_count = 0;
1558 ipdata->attr_tid = 0;
1559 ipdata->dirent_tid = 0;
1560 bzero(&ipdata->u, sizeof(ipdata->u));
1561 /* XXX transaction ids */
1563 hammer2_chain_delete(trans, chain, 0);
1568 * chain represents the hardlink target and is now flagged deleted.
1569 * duplicate it to the parent directory and adjust nlinks.
1571 * WARNING! The shiftup() call can cause nchain to be moved into
1572 * an indirect block, and our nchain will wind up pointing
1573 * to the older/original version.
1575 KKASSERT(chain->flags & HAMMER2_CHAIN_DELETED);
1576 hammer2_hardlink_shiftup(trans, &chain, cdip, nlinks, &error);
1579 hammer2_inode_repoint(ip, cdip, chain);
1582 * Unlock the original chain last as the lock blocked races against
1583 * the creation of the new hardlink target.
1586 hammer2_chain_unlock(nchain);
1590 * Cleanup, chain/nchain already dealt with.
1593 hammer2_inode_drop(cdip);
1599 * If (*ochainp) is non-NULL it points to the forward OBJTYPE_HARDLINK
1600 * inode while (*chainp) points to the resolved (hidden hardlink
1601 * target) inode. In this situation when nlinks is 1 we wish to
1602 * deconsolidate the hardlink, moving it back to the directory that now
1603 * represents the only remaining link.
1606 hammer2_hardlink_deconsolidate(hammer2_trans_t *trans,
1607 hammer2_inode_t *dip,
1608 hammer2_chain_t **chainp,
1609 hammer2_chain_t **ochainp)
1611 if (*ochainp == NULL)
1618 * The caller presents a locked *chainp pointing to a HAMMER2_BREF_TYPE_INODE
1619 * with an obj_type of HAMMER2_OBJTYPE_HARDLINK. This routine will gobble
1620 * the *chainp and return a new locked *chainp representing the file target
1621 * (the original *chainp will be unlocked).
1623 * When a match is found the chain representing the original HARDLINK
1624 * will be returned in *ochainp with a ref, but not locked.
1626 * When no match is found *chainp is set to NULL and EIO is returned.
1627 * (*ochainp) will still be set to the original chain with a ref but not
1631 hammer2_hardlink_find(hammer2_inode_t *dip, hammer2_chain_t **chainp,
1632 hammer2_chain_t **ochainp)
1634 hammer2_chain_t *chain = *chainp;
1635 hammer2_chain_t *parent;
1636 hammer2_inode_t *ip;
1637 hammer2_inode_t *pip;
1638 hammer2_key_t key_dummy;
1640 int cache_index = -1;
1643 hammer2_inode_ref(pip); /* for loop */
1644 hammer2_chain_ref(chain); /* for (*ochainp) */
1648 * Locate the hardlink. pip is referenced and not locked,
1653 lhc = chain->data->ipdata.inum;
1654 hammer2_chain_unlock(chain);
1657 while ((ip = pip) != NULL) {
1658 parent = hammer2_inode_lock_ex(ip);
1659 hammer2_inode_drop(ip); /* loop */
1660 KKASSERT(parent->bref.type == HAMMER2_BREF_TYPE_INODE);
1661 chain = hammer2_chain_lookup(&parent, &key_dummy,
1662 lhc, lhc, &cache_index, 0);
1663 hammer2_chain_lookup_done(parent); /* discard parent */
1666 pip = ip->pip; /* safe, ip held locked */
1668 hammer2_inode_ref(pip); /* loop */
1669 hammer2_inode_unlock_ex(ip, NULL);
1673 * chain is locked, ip is locked. Unlock ip, return the locked
1674 * chain. *ipp is already set w/a ref count and not locked.
1676 * (parent is already unlocked).
1679 hammer2_inode_unlock_ex(ip, NULL);
1682 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_INODE);
1683 /* already locked */
1691 * Find the directory common to both fdip and tdip, hold and return
1695 hammer2_inode_common_parent(hammer2_inode_t *fdip, hammer2_inode_t *tdip)
1697 hammer2_inode_t *scan1;
1698 hammer2_inode_t *scan2;
1701 * We used to have a depth field but it complicated matters too
1702 * much for directory renames. So now its ugly. Check for
1703 * simple cases before giving up and doing it the expensive way.
1705 * XXX need a bottom-up topology stability lock
1707 if (fdip == tdip || fdip == tdip->pip) {
1708 hammer2_inode_ref(fdip);
1711 if (fdip->pip == tdip) {
1712 hammer2_inode_ref(tdip);
1719 for (scan1 = fdip; scan1->pmp == fdip->pmp; scan1 = scan1->pip) {
1721 while (scan2->pmp == tdip->pmp) {
1722 if (scan1 == scan2) {
1723 hammer2_inode_ref(scan1);
1731 panic("hammer2_inode_common_parent: no common parent %p %p\n",
1738 * Synchronize the inode's frontend state with the chain state prior
1739 * to any explicit flush of the inode or any strategy write call.
1741 * Called with a locked inode.
1744 hammer2_inode_fsync(hammer2_trans_t *trans, hammer2_inode_t *ip,
1745 hammer2_chain_t **chainp)
1747 hammer2_inode_data_t *ipdata;
1748 hammer2_chain_t *parent;
1749 hammer2_chain_t *chain;
1750 hammer2_key_t lbase;
1751 hammer2_key_t key_next;
1754 ipdata = &ip->chain->data->ipdata;
1756 if (ip->flags & HAMMER2_INODE_MTIME) {
1757 ipdata = hammer2_chain_modify_ip(trans, ip, chainp, 0);
1758 atomic_clear_int(&ip->flags, HAMMER2_INODE_MTIME);
1759 ipdata->mtime = ip->mtime;
1761 if ((ip->flags & HAMMER2_INODE_RESIZED) && ip->size < ipdata->size) {
1762 ipdata = hammer2_chain_modify_ip(trans, ip, chainp, 0);
1763 ipdata->size = ip->size;
1764 atomic_clear_int(&ip->flags, HAMMER2_INODE_RESIZED);
1767 * We must delete any chains beyond the EOF. The chain
1768 * straddling the EOF will be pending in the bioq.
1770 lbase = (ipdata->size + HAMMER2_PBUFMASK64) &
1771 ~HAMMER2_PBUFMASK64;
1772 parent = hammer2_chain_lookup_init(ip->chain, 0);
1773 chain = hammer2_chain_lookup(&parent, &key_next,
1774 lbase, (hammer2_key_t)-1,
1776 HAMMER2_LOOKUP_NODATA);
1779 * Degenerate embedded case, nothing to loop on
1781 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
1782 hammer2_chain_unlock(chain);
1785 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
1786 hammer2_chain_delete(trans, chain, 0);
1788 chain = hammer2_chain_next(&parent, chain, &key_next,
1789 key_next, (hammer2_key_t)-1,
1791 HAMMER2_LOOKUP_NODATA);
1793 hammer2_chain_lookup_done(parent);
1795 if ((ip->flags & HAMMER2_INODE_RESIZED) && ip->size > ipdata->size) {
1796 ipdata = hammer2_chain_modify_ip(trans, ip, chainp, 0);
1797 ipdata->size = ip->size;
1798 atomic_clear_int(&ip->flags, HAMMER2_INODE_RESIZED);
1801 * When resizing larger we may not have any direct-data
1804 if ((ipdata->op_flags & HAMMER2_OPFLAG_DIRECTDATA) &&
1805 ip->size > HAMMER2_EMBEDDED_BYTES) {
1806 ipdata->op_flags &= ~HAMMER2_OPFLAG_DIRECTDATA;
1807 bzero(&ipdata->u.blockset, sizeof(ipdata->u.blockset));