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 RB_GENERATE2(hammer2_inode_tree, hammer2_inode, rbnode, hammer2_inode_cmp,
48 hammer2_inode_cmp(hammer2_inode_t *ip1, hammer2_inode_t *ip2)
50 if (ip1->inum < ip2->inum)
52 if (ip1->inum > ip2->inum)
60 * HAMMER2 offers shared locks and exclusive locks on inodes.
62 * An inode's ip->chain pointer is resolved and stable while an inode is
63 * locked, and can be cleaned out at any time (become NULL) when an inode
66 * This function handles duplication races and hardlink replacement races
67 * which can cause ip's cached chain to become stale.
69 * The underlying chain is also locked and returned.
71 * NOTE: We don't combine the inode/chain lock because putting away an
72 * inode would otherwise confuse multiple lock holders of the inode.
75 hammer2_inode_lock_ex(hammer2_inode_t *ip)
77 hammer2_chain_t *chain;
78 hammer2_chain_t *ochain;
79 hammer2_chain_core_t *core;
82 hammer2_inode_ref(ip);
83 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
88 if (chain->flags & HAMMER2_CHAIN_DUPLICATED) {
89 spin_lock(&core->cst.spin);
90 while (chain->flags & HAMMER2_CHAIN_DUPLICATED)
91 chain = TAILQ_NEXT(chain, core_entry);
92 hammer2_chain_ref(chain);
93 spin_unlock(&core->cst.spin);
94 hammer2_inode_repoint(ip, NULL, chain);
95 hammer2_chain_drop(chain);
97 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS);
98 if ((chain->flags & HAMMER2_CHAIN_DUPLICATED) == 0)
100 hammer2_chain_unlock(chain);
102 if (chain->data->ipdata.type == HAMMER2_OBJTYPE_HARDLINK) {
103 error = hammer2_hardlink_find(ip->pip, &chain, &ochain);
104 hammer2_chain_drop(ochain);
105 KKASSERT(error == 0);
106 /* XXX error handling */
112 hammer2_inode_unlock_ex(hammer2_inode_t *ip, hammer2_chain_t *chain)
115 * XXX this will catch parent directories too which we don't
119 hammer2_chain_unlock(chain);
120 ccms_thread_unlock(&ip->topo_cst);
121 hammer2_inode_drop(ip);
125 * NOTE: We don't combine the inode/chain lock because putting away an
126 * inode would otherwise confuse multiple lock holders of the inode.
128 * Shared locks are especially sensitive to having too many shared
129 * lock counts (from the same thread) on certain paths which might
130 * need to upgrade them. Only one count of a shared lock can be
134 hammer2_inode_lock_sh(hammer2_inode_t *ip)
136 hammer2_chain_t *chain;
138 hammer2_inode_ref(ip);
140 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_SHARED);
143 KKASSERT(chain != NULL); /* for now */
144 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS |
145 HAMMER2_RESOLVE_SHARED);
148 * Resolve duplication races, resolve hardlinks by giving
149 * up and cycling an exclusive lock.
151 if ((chain->flags & HAMMER2_CHAIN_DUPLICATED) == 0 &&
152 chain->data->ipdata.type != HAMMER2_OBJTYPE_HARDLINK) {
155 hammer2_chain_unlock(chain);
156 ccms_thread_unlock(&ip->topo_cst);
157 chain = hammer2_inode_lock_ex(ip);
158 hammer2_inode_unlock_ex(ip, chain);
164 hammer2_inode_unlock_sh(hammer2_inode_t *ip, hammer2_chain_t *chain)
167 hammer2_chain_unlock(chain);
168 ccms_thread_unlock(&ip->topo_cst);
169 hammer2_inode_drop(ip);
173 hammer2_inode_lock_temp_release(hammer2_inode_t *ip)
175 return(ccms_thread_lock_temp_release(&ip->topo_cst));
179 hammer2_inode_lock_temp_restore(hammer2_inode_t *ip, ccms_state_t ostate)
181 ccms_thread_lock_temp_restore(&ip->topo_cst, ostate);
185 hammer2_inode_lock_upgrade(hammer2_inode_t *ip)
187 return(ccms_thread_lock_upgrade(&ip->topo_cst));
191 hammer2_inode_lock_downgrade(hammer2_inode_t *ip, ccms_state_t ostate)
193 ccms_thread_lock_downgrade(&ip->topo_cst, ostate);
197 * Lookup an inode by inode number
200 hammer2_inode_lookup(hammer2_pfsmount_t *pmp, hammer2_tid_t inum)
205 spin_lock(&pmp->inum_spin);
206 ip = RB_LOOKUP(hammer2_inode_tree, &pmp->inum_tree, inum);
208 hammer2_inode_ref(ip);
209 spin_unlock(&pmp->inum_spin);
217 * Adding a ref to an inode is only legal if the inode already has at least
221 hammer2_inode_ref(hammer2_inode_t *ip)
223 atomic_add_int(&ip->refs, 1);
227 * Drop an inode reference, freeing the inode when the last reference goes
231 hammer2_inode_drop(hammer2_inode_t *ip)
233 hammer2_pfsmount_t *pmp;
234 hammer2_inode_t *pip;
242 * Transition to zero, must interlock with
243 * the inode inumber lookup tree (if applicable).
245 * NOTE: The super-root inode has no pmp.
249 spin_lock(&pmp->inum_spin);
251 if (atomic_cmpset_int(&ip->refs, 1, 0)) {
252 KKASSERT(ip->topo_cst.count == 0);
253 if (ip->flags & HAMMER2_INODE_ONRBTREE) {
254 atomic_clear_int(&ip->flags,
255 HAMMER2_INODE_ONRBTREE);
256 RB_REMOVE(hammer2_inode_tree,
257 &pmp->inum_tree, ip);
260 spin_unlock(&pmp->inum_spin);
267 * Cleaning out ip->chain isn't entirely
270 hammer2_inode_repoint(ip, NULL, NULL);
273 * We have to drop pip (if non-NULL) to
274 * dispose of our implied reference from
275 * ip->pip. We can simply loop on it.
278 KKASSERT((ip->flags &
279 HAMMER2_INODE_SROOT) == 0);
280 kfree(ip, pmp->minode);
281 atomic_add_long(&pmp->inmem_inodes, -1);
284 HAMMER2_INODE_SROOT);
285 kfree(ip, M_HAMMER2);
288 /* continue with pip (can be NULL) */
291 spin_unlock(&ip->pmp->inum_spin);
295 * Non zero transition
297 if (atomic_cmpset_int(&ip->refs, refs, refs - 1))
304 * Get the vnode associated with the given inode, allocating the vnode if
305 * necessary. The vnode will be returned exclusively locked.
307 * The caller must lock the inode (shared or exclusive).
309 * Great care must be taken to avoid deadlocks and vnode acquisition/reclaim
313 hammer2_igetv(hammer2_inode_t *ip, int *errorp)
315 hammer2_inode_data_t *ipdata;
316 hammer2_pfsmount_t *pmp;
321 KKASSERT(pmp != NULL);
323 ipdata = &ip->chain->data->ipdata;
327 * Attempt to reuse an existing vnode assignment. It is
328 * possible to race a reclaim so the vget() may fail. The
329 * inode must be unlocked during the vget() to avoid a
330 * deadlock against a reclaim.
335 * Inode must be unlocked during the vget() to avoid
336 * possible deadlocks, but leave the ip ref intact.
338 * vnode is held to prevent destruction during the
339 * vget(). The vget() can still fail if we lost
340 * a reclaim race on the vnode.
343 ostate = hammer2_inode_lock_temp_release(ip);
344 if (vget(vp, LK_EXCLUSIVE)) {
346 hammer2_inode_lock_temp_restore(ip, ostate);
349 hammer2_inode_lock_temp_restore(ip, ostate);
351 /* vp still locked and ref from vget */
353 kprintf("hammer2: igetv race %p/%p\n",
363 * No vnode exists, allocate a new vnode. Beware of
364 * allocation races. This function will return an
365 * exclusively locked and referenced vnode.
367 *errorp = getnewvnode(VT_HAMMER2, pmp->mp, &vp, 0, 0);
369 kprintf("hammer2: igetv getnewvnode failed %d\n",
376 * Lock the inode and check for an allocation race.
378 ostate = hammer2_inode_lock_upgrade(ip);
379 if (ip->vp != NULL) {
382 hammer2_inode_lock_downgrade(ip, ostate);
386 switch (ipdata->type) {
387 case HAMMER2_OBJTYPE_DIRECTORY:
390 case HAMMER2_OBJTYPE_REGFILE:
392 vinitvmio(vp, ipdata->size,
394 (int)ipdata->size & HAMMER2_LBUFMASK);
396 case HAMMER2_OBJTYPE_SOFTLINK:
398 * XXX for now we are using the generic file_read
399 * and file_write code so we need a buffer cache
403 vinitvmio(vp, ipdata->size,
405 (int)ipdata->size & HAMMER2_LBUFMASK);
407 case HAMMER2_OBJTYPE_CDEV:
410 case HAMMER2_OBJTYPE_BDEV:
411 vp->v_ops = &pmp->mp->mnt_vn_spec_ops;
412 if (ipdata->type != HAMMER2_OBJTYPE_CDEV)
414 addaliasu(vp, ipdata->rmajor, ipdata->rminor);
416 case HAMMER2_OBJTYPE_FIFO:
418 vp->v_ops = &pmp->mp->mnt_vn_fifo_ops;
421 panic("hammer2: unhandled objtype %d", ipdata->type);
425 if (ip == pmp->iroot)
426 vsetflags(vp, VROOT);
430 hammer2_inode_ref(ip); /* vp association */
431 hammer2_inode_lock_downgrade(ip, ostate);
436 * Return non-NULL vp and *errorp == 0, or NULL vp and *errorp != 0.
438 if (hammer2_debug & 0x0002) {
439 kprintf("igetv vp %p refs 0x%08x aux 0x%08x\n",
440 vp, vp->v_refcnt, vp->v_auxrefs);
446 * The passed-in chain must be locked and the returned inode will also be
447 * locked. This routine typically locates or allocates the inode, assigns
448 * ip->chain (adding a ref to chain if necessary), and returns the inode.
450 * The hammer2_inode structure regulates the interface between the high level
451 * kernel VNOPS API and the filesystem backend (the chains).
453 * WARNING! This routine sucks up the chain's lock (makes it part of the
454 * inode lock from the point of view of the inode lock API),
455 * so callers need to be careful.
457 * WARNING! The mount code is allowed to pass dip == NULL for iroot and
458 * is allowed to pass pmp == NULL and dip == NULL for sroot.
461 hammer2_inode_get(hammer2_pfsmount_t *pmp, hammer2_inode_t *dip,
462 hammer2_chain_t *chain)
464 hammer2_inode_t *nip;
466 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_INODE);
469 * Interlocked lookup/ref of the inode. This code is only needed
470 * when looking up inodes with nlinks != 0 (TODO: optimize out
471 * otherwise and test for duplicates).
475 nip = hammer2_inode_lookup(pmp, chain->data->ipdata.inum);
478 ccms_thread_lock(&nip->topo_cst, CCMS_STATE_EXCLUSIVE);
479 if ((nip->flags & HAMMER2_INODE_ONRBTREE) == 0) { /* race */
480 ccms_thread_unlock(&nip->topo_cst);
481 hammer2_inode_drop(nip);
484 if (nip->chain != chain)
485 hammer2_inode_repoint(nip, NULL, chain);
488 * Consolidated nip/nip->chain is locked (chain locked
495 * We couldn't find the inode number, create a new inode.
498 nip = kmalloc(sizeof(*nip), pmp->minode, M_WAITOK | M_ZERO);
499 atomic_add_long(&pmp->inmem_inodes, 1);
500 hammer2_chain_memory_inc(pmp);
501 hammer2_chain_memory_wakeup(pmp);
503 nip = kmalloc(sizeof(*nip), M_HAMMER2, M_WAITOK | M_ZERO);
504 nip->flags = HAMMER2_INODE_SROOT;
506 nip->inum = chain->data->ipdata.inum;
507 nip->size = chain->data->ipdata.size;
508 nip->mtime = chain->data->ipdata.mtime;
509 hammer2_inode_repoint(nip, NULL, chain);
510 nip->pip = dip; /* can be NULL */
512 hammer2_inode_ref(dip); /* ref dip for nip->pip */
517 * ref and lock on nip gives it state compatible to after a
518 * hammer2_inode_lock_ex() call.
521 ccms_cst_init(&nip->topo_cst, &nip->chain);
522 ccms_thread_lock(&nip->topo_cst, CCMS_STATE_EXCLUSIVE);
523 /* combination of thread lock and chain lock == inode lock */
526 * Attempt to add the inode. If it fails we raced another inode
527 * get. Undo all the work and try again.
530 spin_lock(&pmp->inum_spin);
531 if (RB_INSERT(hammer2_inode_tree, &pmp->inum_tree, nip)) {
532 spin_unlock(&pmp->inum_spin);
533 ccms_thread_unlock(&nip->topo_cst);
534 hammer2_inode_drop(nip);
537 atomic_set_int(&nip->flags, HAMMER2_INODE_ONRBTREE);
538 spin_unlock(&pmp->inum_spin);
545 * Create a new inode in the specified directory using the vattr to
546 * figure out the type of inode.
548 * If no error occurs the new inode with its chain locked is returned in
549 * *nipp, otherwise an error is returned and *nipp is set to NULL.
551 * If vap and/or cred are NULL the related fields are not set and the
552 * inode type defaults to a directory. This is used when creating PFSs
553 * under the super-root, so the inode number is set to 1 in this case.
555 * dip is not locked on entry.
558 hammer2_inode_create(hammer2_trans_t *trans, hammer2_inode_t *dip,
559 struct vattr *vap, struct ucred *cred,
560 const uint8_t *name, size_t name_len,
561 hammer2_chain_t **chainp, int *errorp)
563 hammer2_inode_data_t *dipdata;
564 hammer2_inode_data_t *nipdata;
565 hammer2_chain_t *chain;
566 hammer2_chain_t *parent;
567 hammer2_inode_t *nip;
568 hammer2_key_t key_dummy;
576 int cache_index = -1;
578 lhc = hammer2_dirhash(name, name_len);
582 * Locate the inode or indirect block to create the new
583 * entry in. At the same time check for key collisions
584 * and iterate until we don't get one.
586 * NOTE: hidden inodes do not have iterators.
589 parent = hammer2_inode_lock_ex(dip);
590 dipdata = &dip->chain->data->ipdata;
591 dip_uid = dipdata->uid;
592 dip_gid = dipdata->gid;
593 dip_mode = dipdata->mode;
594 dip_algo = dipdata->comp_algo;
598 chain = hammer2_chain_lookup(&parent, &key_dummy,
599 lhc, lhc, &cache_index, 0);
602 if ((lhc & HAMMER2_DIRHASH_VISIBLE) == 0)
604 if ((lhc & HAMMER2_DIRHASH_LOMASK) == HAMMER2_DIRHASH_LOMASK)
606 hammer2_chain_unlock(chain);
612 error = hammer2_chain_create(trans, &parent, &chain,
614 HAMMER2_BREF_TYPE_INODE,
615 HAMMER2_INODE_BYTES);
619 * Cleanup and handle retries.
621 if (error == EAGAIN) {
622 hammer2_chain_ref(parent);
623 hammer2_inode_unlock_ex(dip, parent);
624 hammer2_chain_wait(parent);
625 hammer2_chain_drop(parent);
628 hammer2_inode_unlock_ex(dip, parent);
631 KKASSERT(chain == NULL);
637 * Set up the new inode.
639 * NOTE: *_get() integrates chain's lock into the inode lock.
641 * NOTE: Only one new inode can currently be created per
642 * transaction. If the need arises we can adjust
643 * hammer2_trans_init() to allow more.
645 * NOTE: nipdata will have chain's blockset data.
647 chain->data->ipdata.inum = trans->inode_tid;
648 nip = hammer2_inode_get(dip->pmp, dip, chain);
649 nipdata = &chain->data->ipdata;
652 KKASSERT(trans->inodes_created == 0);
653 nipdata->type = hammer2_get_obj_type(vap->va_type);
654 nipdata->inum = trans->inode_tid;
655 ++trans->inodes_created;
657 switch (nipdata->type) {
658 case HAMMER2_OBJTYPE_CDEV:
659 case HAMMER2_OBJTYPE_BDEV:
660 nipdata->rmajor = vap->va_rmajor;
661 nipdata->rminor = vap->va_rminor;
667 nipdata->type = HAMMER2_OBJTYPE_DIRECTORY;
671 /* Inherit parent's inode compression mode. */
672 nip->comp_heuristic = 0;
673 nipdata->comp_algo = dip_algo;
674 nipdata->version = HAMMER2_INODE_VERSION_ONE;
675 hammer2_update_time(&nipdata->ctime);
676 nipdata->mtime = nipdata->ctime;
678 nipdata->mode = vap->va_mode;
681 if (dip && dip->pmp) {
682 xuid = hammer2_to_unix_xid(&dip_uid);
683 xuid = vop_helper_create_uid(dip->pmp->mp,
689 /* super-root has no dip and/or pmp */
692 if (vap->va_vaflags & VA_UID_UUID_VALID)
693 nipdata->uid = vap->va_uid_uuid;
694 else if (vap->va_uid != (uid_t)VNOVAL)
695 hammer2_guid_to_uuid(&nipdata->uid, vap->va_uid);
697 hammer2_guid_to_uuid(&nipdata->uid, xuid);
699 if (vap->va_vaflags & VA_GID_UUID_VALID)
700 nipdata->gid = vap->va_gid_uuid;
701 else if (vap->va_gid != (gid_t)VNOVAL)
702 hammer2_guid_to_uuid(&nipdata->gid, vap->va_gid);
704 nipdata->gid = dip_gid;
708 * Regular files and softlinks allow a small amount of data to be
709 * directly embedded in the inode. This flag will be cleared if
710 * the size is extended past the embedded limit.
712 if (nipdata->type == HAMMER2_OBJTYPE_REGFILE ||
713 nipdata->type == HAMMER2_OBJTYPE_SOFTLINK) {
714 nipdata->op_flags |= HAMMER2_OPFLAG_DIRECTDATA;
717 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
718 bcopy(name, nipdata->filename, name_len);
719 nipdata->name_key = lhc;
720 nipdata->name_len = name_len;
727 * chain may have been moved around by the create.
730 hammer2_chain_refactor(hammer2_chain_t **chainp)
732 hammer2_chain_t *chain = *chainp;
733 hammer2_chain_core_t *core;
736 while (chain->flags & HAMMER2_CHAIN_DUPLICATED) {
737 spin_lock(&core->cst.spin);
738 chain = TAILQ_NEXT(chain, core_entry);
739 while (chain->flags & HAMMER2_CHAIN_DUPLICATED)
740 chain = TAILQ_NEXT(chain, core_entry);
741 hammer2_chain_ref(chain);
742 spin_unlock(&core->cst.spin);
743 KKASSERT(chain->core == core);
745 hammer2_chain_unlock(*chainp);
746 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS |
747 HAMMER2_RESOLVE_NOREF); /* eat ref */
753 * Shift *chainp up to the specified directory, change the filename
754 * to "0xINODENUMBER", and adjust the key. The chain becomes the
755 * invisible hardlink target.
757 * The original *chainp has already been marked deleted.
761 hammer2_hardlink_shiftup(hammer2_trans_t *trans, hammer2_chain_t **chainp,
762 hammer2_inode_t *dip, int nlinks, int *errorp)
764 hammer2_inode_data_t *nipdata;
765 hammer2_chain_t *parent;
766 hammer2_chain_t *chain;
767 hammer2_chain_t *xchain;
768 hammer2_key_t key_dummy;
770 hammer2_blockref_t bref;
771 int cache_index = -1;
774 lhc = chain->data->ipdata.inum;
775 KKASSERT((lhc & HAMMER2_DIRHASH_VISIBLE) == 0);
778 * Locate the inode or indirect block to create the new
779 * entry in. lhc represents the inode number so there is
780 * no collision iteration.
782 * There should be no key collisions with invisible inode keys.
784 * WARNING! Must use inode_lock_ex() on dip to handle a stale
789 parent = hammer2_inode_lock_ex(dip);
790 /*parent = hammer2_chain_lookup_init(dip->chain, 0);*/
791 xchain = hammer2_chain_lookup(&parent, &key_dummy,
792 lhc, lhc, &cache_index, 0);
794 kprintf("X3 chain %p parent %p dip %p dip->chain %p\n",
795 xchain, parent, dip, dip->chain);
796 hammer2_chain_unlock(xchain);
805 * Create entry in common parent directory using the seek position
808 * We must refactor chain because it might have been shifted into
809 * an indirect chain by the create.
812 KKASSERT(xchain == NULL);
814 *errorp = hammer2_chain_create(trans, &parent, &xchain,
816 HAMMER2_BREF_TYPE_INODE,/* n/a */
817 HAMMER2_INODE_BYTES); /* n/a */
819 /*XXX this somehow isn't working on chain XXX*/
824 * Cleanup and handle retries.
826 if (*errorp == EAGAIN) {
827 hammer2_chain_ref(parent);
828 /* hammer2_chain_lookup_done(parent); */
829 hammer2_inode_unlock_ex(dip, parent);
830 hammer2_chain_wait(parent);
831 hammer2_chain_drop(parent);
836 * Handle the error case
840 KKASSERT(xchain == NULL);
841 hammer2_inode_unlock_ex(dip, parent);
842 /*hammer2_chain_lookup_done(parent);*/
847 * Use xchain as a placeholder for (lhc). Duplicate chain to the
848 * same target bref as xchain and then delete xchain. The duplication
849 * occurs after xchain in flush order even though xchain is deleted
850 * after the duplication. XXX
852 * WARNING! Duplications (to a different parent) can cause indirect
853 * blocks to be inserted, refactor xchain.
856 bref.key = lhc; /* invisible dir entry key */
859 hammer2_chain_delete(trans, xchain, 0);
861 hammer2_chain_duplicate(trans, &parent, &chain, &bref, 0, 2);
863 hammer2_chain_refactor(&xchain);
864 /*hammer2_chain_delete(trans, xchain, 0);*/
867 hammer2_inode_unlock_ex(dip, parent);
868 /*hammer2_chain_lookup_done(parent);*/
870 hammer2_chain_unlock(xchain); /* no longer needed */
874 * chain is now 'live' again.. adjust the filename.
876 * Directory entries are inodes but this is a hidden hardlink
877 * target. The name isn't used but to ease debugging give it
878 * a name after its inode number.
880 hammer2_chain_modify(trans, &chain, 0);
881 nipdata = &chain->data->ipdata;
882 ksnprintf(nipdata->filename, sizeof(nipdata->filename),
883 "0x%016jx", (intmax_t)nipdata->inum);
884 nipdata->name_len = strlen(nipdata->filename);
885 nipdata->name_key = lhc;
886 nipdata->nlinks += nlinks;
892 * Connect the target inode represented by (*chainp) to the media topology
893 * at (dip, name, len).
895 * If hlink is TRUE this function creates an OBJTYPE_HARDLINK directory
896 * entry instead of connecting (*chainp).
898 * If hlink is FALSE this function uses chain_duplicate() to make a copy
899 * if (*chainp) in the directory entry. (*chainp) is likely to be deleted
900 * by the caller in this case (e.g. rename).
903 hammer2_inode_connect(hammer2_trans_t *trans, int hlink,
904 hammer2_inode_t *dip, hammer2_chain_t **chainp,
905 const uint8_t *name, size_t name_len)
907 hammer2_inode_data_t *ipdata;
908 hammer2_chain_t *nchain;
909 hammer2_chain_t *parent;
910 hammer2_chain_t *ochain;
911 hammer2_key_t key_dummy;
913 int cache_index = -1;
919 * Since ochain is either disconnected from the topology or represents
920 * a hardlink terminus which is always a parent of or equal to dip,
921 * we should be able to safely lock dip->chain for our setup.
923 * WARNING! Must use inode_lock_ex() on dip to handle a stale
926 parent = hammer2_inode_lock_ex(dip);
927 /*parent = hammer2_chain_lookup_init(dip->chain, 0);*/
929 lhc = hammer2_dirhash(name, name_len);
932 * Locate the inode or indirect block to create the new
933 * entry in. At the same time check for key collisions
934 * and iterate until we don't get one.
938 nchain = hammer2_chain_lookup(&parent, &key_dummy,
939 lhc, lhc, &cache_index, 0);
942 if ((lhc & HAMMER2_DIRHASH_LOMASK) == HAMMER2_DIRHASH_LOMASK)
944 hammer2_chain_unlock(nchain);
952 * Hardlink pointer needed, create totally fresh
955 * We must refactor ochain because it might have
956 * been shifted into an indirect chain by the
959 KKASSERT(nchain == NULL);
960 error = hammer2_chain_create(trans, &parent, &nchain,
962 HAMMER2_BREF_TYPE_INODE,
963 HAMMER2_INODE_BYTES);
964 hammer2_chain_refactor(&ochain);
967 * Reconnect the original chain and rename. Use
968 * chain_duplicate(). The caller will likely delete
969 * or has already deleted the original chain in
972 * NOTE: chain_duplicate() generates a new chain
973 * with CHAIN_DELETED cleared (ochain typically
974 * has it set from the file unlink).
976 * WARNING! Can cause held-over chains to require a
977 * refactor. Fortunately we have none (our
978 * locked chains are passed into and
979 * modified by the call).
983 hammer2_chain_duplicate(trans, NULL, &nchain, NULL,
985 error = hammer2_chain_create(trans, &parent, &nchain,
987 HAMMER2_BREF_TYPE_INODE,
988 HAMMER2_INODE_BYTES);
995 KKASSERT(error != EAGAIN);
996 hammer2_inode_unlock_ex(dip, parent);
997 /*hammer2_chain_lookup_done(parent);*/
1001 * nchain should be NULL on error, leave ochain (== *chainp) alone.
1004 KKASSERT(nchain == NULL);
1009 * Directory entries are inodes so if the name has changed we have
1010 * to update the inode.
1012 * When creating an OBJTYPE_HARDLINK entry remember to unlock the
1013 * chain, the caller will access the hardlink via the actual hardlink
1014 * target file and not the hardlink pointer entry, so we must still
1017 if (hlink && hammer2_hardlink_enable >= 0) {
1019 * Create the HARDLINK pointer. oip represents the hardlink
1020 * target in this situation.
1022 * We will return ochain (the hardlink target).
1024 hammer2_chain_modify(trans, &nchain, 0);
1025 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
1026 ipdata = &nchain->data->ipdata;
1027 bcopy(name, ipdata->filename, name_len);
1028 ipdata->name_key = lhc;
1029 ipdata->name_len = name_len;
1030 ipdata->target_type = ochain->data->ipdata.type;
1031 ipdata->type = HAMMER2_OBJTYPE_HARDLINK;
1032 ipdata->inum = ochain->data->ipdata.inum;
1034 hammer2_chain_unlock(nchain);
1037 } else if (hlink && hammer2_hardlink_enable < 0) {
1039 * Create a snapshot (hardlink fake mode for debugging).
1040 * (ochain already flushed above so we can just copy the
1043 * Since this is a snapshot we return nchain in the fake
1046 hammer2_chain_modify(trans, &nchain, 0);
1047 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
1048 ipdata = &nchain->data->ipdata;
1049 *ipdata = ochain->data->ipdata;
1050 bcopy(name, ipdata->filename, name_len);
1051 ipdata->name_key = lhc;
1052 ipdata->name_len = name_len;
1053 atomic_clear_int(&nchain->core->flags,
1054 HAMMER2_CORE_COUNTEDBREFS);
1055 kprintf("created fake hardlink %*.*s\n",
1056 (int)name_len, (int)name_len, name);
1059 * nchain is a duplicate of ochain at the new location.
1060 * We must fixup the name stored in oip. The bref key
1061 * has already been set up.
1063 hammer2_chain_modify(trans, &nchain, 0);
1064 ipdata = &nchain->data->ipdata;
1066 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
1067 bcopy(name, ipdata->filename, name_len);
1068 ipdata->name_key = lhc;
1069 ipdata->name_len = name_len;
1074 * We are replacing ochain with nchain, unlock ochain. In the
1075 * case where ochain is left unchanged the code above sets
1076 * nchain to ochain and ochain to NULL, resulting in a NOP here.
1079 hammer2_chain_unlock(ochain);
1086 * Repoint ip->chain to nchain. Caller must hold the inode exclusively
1089 * ip->chain is set to nchain. The prior chain in ip->chain is dropped
1090 * and nchain is ref'd.
1093 hammer2_inode_repoint(hammer2_inode_t *ip, hammer2_inode_t *pip,
1094 hammer2_chain_t *nchain)
1096 hammer2_chain_t *ochain;
1097 hammer2_inode_t *opip;
1100 * Repoint ip->chain if requested.
1105 hammer2_chain_ref(nchain);
1107 hammer2_chain_drop(ochain);
1110 * Repoint ip->pip if requested (non-NULL pip).
1112 if (pip && ip->pip != pip) {
1114 hammer2_inode_ref(pip);
1117 hammer2_inode_drop(opip);
1122 * Unlink the file from the specified directory inode. The directory inode
1123 * does not need to be locked.
1125 * isdir determines whether a directory/non-directory check should be made.
1126 * No check is made if isdir is set to -1.
1128 * NOTE! The underlying file can still be active with open descriptors
1129 * or if the chain is being manually held (e.g. for rename).
1131 * The caller is responsible for fixing up ip->chain if e.g. a
1132 * rename occurs (see chain_duplicate()).
1135 hammer2_unlink_file(hammer2_trans_t *trans, hammer2_inode_t *dip,
1136 const uint8_t *name, size_t name_len,
1137 int isdir, int *hlinkp)
1139 hammer2_inode_data_t *ipdata;
1140 hammer2_chain_t *parent;
1141 hammer2_chain_t *ochain;
1142 hammer2_chain_t *chain;
1143 hammer2_chain_t *dparent;
1144 hammer2_chain_t *dchain;
1145 hammer2_key_t key_dummy;
1146 hammer2_key_t key_next;
1149 int cache_index = -1;
1154 lhc = hammer2_dirhash(name, name_len);
1157 * Search for the filename in the directory
1161 parent = hammer2_inode_lock_ex(dip);
1162 chain = hammer2_chain_lookup(&parent, &key_next,
1163 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1166 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
1167 name_len == chain->data->ipdata.name_len &&
1168 bcmp(name, chain->data->ipdata.filename, name_len) == 0) {
1171 chain = hammer2_chain_next(&parent, chain, &key_next,
1173 lhc + HAMMER2_DIRHASH_LOMASK,
1176 hammer2_inode_unlock_ex(dip, NULL); /* retain parent */
1179 * Not found or wrong type (isdir < 0 disables the type check).
1180 * If a hardlink pointer, type checks use the hardlink target.
1182 if (chain == NULL) {
1186 if ((type = chain->data->ipdata.type) == HAMMER2_OBJTYPE_HARDLINK) {
1189 type = chain->data->ipdata.target_type;
1192 if (type == HAMMER2_OBJTYPE_DIRECTORY && isdir == 0) {
1196 if (type != HAMMER2_OBJTYPE_DIRECTORY && isdir >= 1) {
1202 * Hardlink must be resolved. We can't hold parent locked while we
1203 * do this or we could deadlock.
1205 * On success chain will be adjusted to point at the hardlink target
1206 * and ochain will point to the hardlink pointer in the original
1207 * directory. Otherwise chain remains pointing to the original.
1209 if (chain->data->ipdata.type == HAMMER2_OBJTYPE_HARDLINK) {
1210 hammer2_chain_unlock(parent);
1212 error = hammer2_hardlink_find(dip, &chain, &ochain);
1216 * If this is a directory the directory must be empty. However, if
1217 * isdir < 0 we are doing a rename and the directory does not have
1218 * to be empty, and if isdir > 1 we are deleting a PFS/snapshot
1219 * and the directory does not have to be empty.
1221 * NOTE: We check the full key range here which covers both visible
1222 * and invisible entries. Theoretically there should be no
1223 * invisible (hardlink target) entries if there are no visible
1226 if (type == HAMMER2_OBJTYPE_DIRECTORY && isdir == 1) {
1227 dparent = hammer2_chain_lookup_init(chain, 0);
1228 dchain = hammer2_chain_lookup(&dparent, &key_dummy,
1229 0, (hammer2_key_t)-1,
1231 HAMMER2_LOOKUP_NODATA);
1233 hammer2_chain_unlock(dchain);
1234 hammer2_chain_lookup_done(dparent);
1238 hammer2_chain_lookup_done(dparent);
1244 * Ok, we can now unlink the chain. We always decrement nlinks even
1245 * if the entry can be deleted in case someone has the file open and
1248 * The chain itself will no longer be in the on-media topology but
1249 * can still be flushed to the media (e.g. if an open descriptor
1250 * remains). When the last vnode/ip ref goes away the chain will
1251 * be marked unmodified, avoiding any further (now unnecesary) I/O.
1253 * A non-NULL ochain indicates a hardlink.
1257 * Delete the original hardlink pointer.
1259 * NOTE: parent from above is NULL when ochain != NULL
1260 * so we can reuse it.
1262 hammer2_chain_lock(ochain, HAMMER2_RESOLVE_ALWAYS);
1263 hammer2_chain_delete(trans, ochain, 0);
1264 hammer2_chain_unlock(ochain);
1267 * Then decrement nlinks on hardlink target, deleting
1268 * the target when nlinks drops to 0.
1270 hammer2_chain_modify(trans, &chain, 0);
1271 --chain->data->ipdata.nlinks;
1272 if (chain->data->ipdata.nlinks == 0)
1273 hammer2_chain_delete(trans, chain, 0);
1276 * Otherwise this was not a hardlink and we can just
1277 * remove the entry and decrement nlinks.
1279 * NOTE: *_get() integrates chain's lock into the inode lock.
1281 hammer2_chain_modify(trans, &chain, 0);
1282 ipdata = &chain->data->ipdata;
1284 hammer2_chain_delete(trans, chain, 0);
1290 hammer2_chain_unlock(chain);
1292 hammer2_chain_lookup_done(parent);
1294 hammer2_chain_drop(ochain);
1300 * Given an exclusively locked inode and chain we consolidate its chain
1301 * for hardlink creation, adding (nlinks) to the file's link count and
1302 * potentially relocating the inode to a directory common to ip->pip and tdip.
1304 * Replaces (*chainp) if consolidation occurred, unlocking the old chain
1305 * and returning a new locked chain.
1307 * NOTE! This function will also replace ip->chain.
1310 hammer2_hardlink_consolidate(hammer2_trans_t *trans, hammer2_inode_t *ip,
1311 hammer2_chain_t **chainp,
1312 hammer2_inode_t *tdip, int nlinks)
1314 hammer2_inode_data_t *ipdata;
1315 hammer2_inode_t *fdip;
1316 hammer2_inode_t *cdip;
1317 hammer2_chain_t *chain;
1318 hammer2_chain_t *nchain;
1322 if (nlinks == 0 && /* no hardlink needed */
1323 (chain->data->ipdata.name_key & HAMMER2_DIRHASH_VISIBLE)) {
1326 if (hammer2_hardlink_enable < 0) { /* fake hardlinks */
1330 if (hammer2_hardlink_enable == 0) { /* disallow hardlinks */
1331 hammer2_chain_unlock(chain);
1337 * cdip will be returned with a ref, but not locked.
1340 cdip = hammer2_inode_common_parent(fdip, tdip);
1343 * If no change in the hardlink's target directory is required and
1344 * this is already a hardlink target, all we need to do is adjust
1347 * XXX The common parent is a big wiggly due to duplication from
1348 * renames. Compare the core (RBTREE) pointer instead of the
1352 (chain->data->ipdata.name_key & HAMMER2_DIRHASH_VISIBLE) == 0) {
1354 hammer2_chain_modify(trans, &chain, 0);
1355 chain->data->ipdata.nlinks += nlinks;
1363 * chain is the real inode. If it's visible we have to convert it
1364 * to a hardlink pointer. If it is not visible then it is already
1365 * a hardlink target and only needs to be deleted.
1367 KKASSERT((chain->flags & HAMMER2_CHAIN_DELETED) == 0);
1368 KKASSERT(chain->data->ipdata.type != HAMMER2_OBJTYPE_HARDLINK);
1369 if (chain->data->ipdata.name_key & HAMMER2_DIRHASH_VISIBLE) {
1371 * We are going to duplicate chain later, causing its
1372 * media block to be shifted to the duplicate. Even though
1373 * we are delete-duplicating nchain here it might decide not
1374 * to reallocate the block. Set FORCECOW to force it to.
1377 hammer2_chain_lock(nchain, HAMMER2_RESOLVE_ALWAYS);
1378 atomic_set_int(&nchain->flags, HAMMER2_CHAIN_FORCECOW);
1379 hammer2_chain_delete_duplicate(trans, &nchain,
1380 HAMMER2_DELDUP_RECORE);
1381 KKASSERT((chain->flags & HAMMER2_CHAIN_DUPLICATED) == 0);
1383 ipdata = &nchain->data->ipdata;
1384 ipdata->target_type = ipdata->type;
1385 ipdata->type = HAMMER2_OBJTYPE_HARDLINK;
1393 bzero(&ipdata->uid, sizeof(ipdata->uid));
1394 bzero(&ipdata->gid, sizeof(ipdata->gid));
1395 ipdata->op_flags = HAMMER2_OPFLAG_DIRECTDATA;
1396 ipdata->cap_flags = 0;
1400 ipdata->iparent = 0; /* XXX */
1401 ipdata->pfs_type = 0;
1402 ipdata->pfs_inum = 0;
1403 bzero(&ipdata->pfs_clid, sizeof(ipdata->pfs_clid));
1404 bzero(&ipdata->pfs_fsid, sizeof(ipdata->pfs_fsid));
1405 ipdata->data_quota = 0;
1406 ipdata->data_count = 0;
1407 ipdata->inode_quota = 0;
1408 ipdata->inode_count = 0;
1409 ipdata->attr_tid = 0;
1410 ipdata->dirent_tid = 0;
1411 bzero(&ipdata->u, sizeof(ipdata->u));
1412 /* XXX transaction ids */
1414 hammer2_chain_delete(trans, chain, 0);
1419 * chain represents the hardlink target and is now flagged deleted.
1420 * duplicate it to the parent directory and adjust nlinks.
1422 * WARNING! The shiftup() call can cause nchain to be moved into
1423 * an indirect block, and our nchain will wind up pointing
1424 * to the older/original version.
1426 KKASSERT(chain->flags & HAMMER2_CHAIN_DELETED);
1427 hammer2_hardlink_shiftup(trans, &chain, cdip, nlinks, &error);
1430 hammer2_inode_repoint(ip, cdip, chain);
1433 * Unlock the original chain last as the lock blocked races against
1434 * the creation of the new hardlink target.
1437 hammer2_chain_unlock(nchain);
1441 * Cleanup, chain/nchain already dealt with.
1444 hammer2_inode_drop(cdip);
1450 * If (*ochainp) is non-NULL it points to the forward OBJTYPE_HARDLINK
1451 * inode while (*chainp) points to the resolved (hidden hardlink
1452 * target) inode. In this situation when nlinks is 1 we wish to
1453 * deconsolidate the hardlink, moving it back to the directory that now
1454 * represents the only remaining link.
1457 hammer2_hardlink_deconsolidate(hammer2_trans_t *trans,
1458 hammer2_inode_t *dip,
1459 hammer2_chain_t **chainp,
1460 hammer2_chain_t **ochainp)
1462 if (*ochainp == NULL)
1469 * The caller presents a locked *chainp pointing to a HAMMER2_BREF_TYPE_INODE
1470 * with an obj_type of HAMMER2_OBJTYPE_HARDLINK. This routine will gobble
1471 * the *chainp and return a new locked *chainp representing the file target
1472 * (the original *chainp will be unlocked).
1474 * When a match is found the chain representing the original HARDLINK
1475 * will be returned in *ochainp with a ref, but not locked.
1477 * When no match is found *chainp is set to NULL and EIO is returned.
1478 * (*ochainp) will still be set to the original chain with a ref but not
1482 hammer2_hardlink_find(hammer2_inode_t *dip, hammer2_chain_t **chainp,
1483 hammer2_chain_t **ochainp)
1485 hammer2_chain_t *chain = *chainp;
1486 hammer2_chain_t *parent;
1487 hammer2_inode_t *ip;
1488 hammer2_inode_t *pip;
1489 hammer2_key_t key_dummy;
1491 int cache_index = -1;
1494 hammer2_inode_ref(pip); /* for loop */
1495 hammer2_chain_ref(chain); /* for (*ochainp) */
1499 * Locate the hardlink. pip is referenced and not locked,
1504 lhc = chain->data->ipdata.inum;
1505 hammer2_chain_unlock(chain);
1508 while ((ip = pip) != NULL) {
1509 parent = hammer2_inode_lock_ex(ip);
1510 hammer2_inode_drop(ip); /* loop */
1511 KKASSERT(parent->bref.type == HAMMER2_BREF_TYPE_INODE);
1512 chain = hammer2_chain_lookup(&parent, &key_dummy,
1513 lhc, lhc, &cache_index, 0);
1514 hammer2_chain_lookup_done(parent); /* discard parent */
1517 pip = ip->pip; /* safe, ip held locked */
1519 hammer2_inode_ref(pip); /* loop */
1520 hammer2_inode_unlock_ex(ip, NULL);
1524 * chain is locked, ip is locked. Unlock ip, return the locked
1525 * chain. *ipp is already set w/a ref count and not locked.
1527 * (parent is already unlocked).
1530 hammer2_inode_unlock_ex(ip, NULL);
1533 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_INODE);
1534 /* already locked */
1542 * Find the directory common to both fdip and tdip, hold and return
1546 hammer2_inode_common_parent(hammer2_inode_t *fdip, hammer2_inode_t *tdip)
1548 hammer2_inode_t *scan1;
1549 hammer2_inode_t *scan2;
1552 * We used to have a depth field but it complicated matters too
1553 * much for directory renames. So now its ugly. Check for
1554 * simple cases before giving up and doing it the expensive way.
1556 * XXX need a bottom-up topology stability lock
1558 if (fdip == tdip || fdip == tdip->pip) {
1559 hammer2_inode_ref(fdip);
1562 if (fdip->pip == tdip) {
1563 hammer2_inode_ref(tdip);
1570 for (scan1 = fdip; scan1->pmp == fdip->pmp; scan1 = scan1->pip) {
1572 while (scan2->pmp == tdip->pmp) {
1573 if (scan1 == scan2) {
1574 hammer2_inode_ref(scan1);
1582 panic("hammer2_inode_common_parent: no common parent %p %p\n",
1589 * Synchronize the inode's frontend state with the chain state prior
1590 * to any explicit flush of the inode or any strategy write call.
1592 * Called with a locked inode.
1595 hammer2_inode_fsync(hammer2_trans_t *trans, hammer2_inode_t *ip,
1596 hammer2_chain_t **chainp)
1598 hammer2_inode_data_t *ipdata;
1599 hammer2_chain_t *parent;
1600 hammer2_chain_t *chain;
1601 hammer2_key_t lbase;
1602 hammer2_key_t key_next;
1605 ipdata = &ip->chain->data->ipdata;
1607 if (ip->flags & HAMMER2_INODE_MTIME) {
1608 ipdata = hammer2_chain_modify_ip(trans, ip, chainp, 0);
1609 atomic_clear_int(&ip->flags, HAMMER2_INODE_MTIME);
1610 ipdata->mtime = ip->mtime;
1612 if ((ip->flags & HAMMER2_INODE_RESIZED) && ip->size < ipdata->size) {
1613 ipdata = hammer2_chain_modify_ip(trans, ip, chainp, 0);
1614 ipdata->size = ip->size;
1615 atomic_clear_int(&ip->flags, HAMMER2_INODE_RESIZED);
1618 * We must delete any chains beyond the EOF. The chain
1619 * straddling the EOF will be pending in the bioq.
1621 lbase = (ipdata->size + HAMMER2_PBUFMASK64) &
1622 ~HAMMER2_PBUFMASK64;
1623 parent = hammer2_chain_lookup_init(ip->chain, 0);
1624 chain = hammer2_chain_lookup(&parent, &key_next,
1625 lbase, (hammer2_key_t)-1,
1627 HAMMER2_LOOKUP_NODATA);
1630 * Degenerate embedded case, nothing to loop on
1632 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
1633 hammer2_chain_unlock(chain);
1636 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
1637 hammer2_chain_delete(trans, chain, 0);
1639 chain = hammer2_chain_next(&parent, chain, &key_next,
1640 key_next, (hammer2_key_t)-1,
1642 HAMMER2_LOOKUP_NODATA);
1644 hammer2_chain_lookup_done(parent);
1646 if ((ip->flags & HAMMER2_INODE_RESIZED) && ip->size > ipdata->size) {
1647 ipdata = hammer2_chain_modify_ip(trans, ip, chainp, 0);
1648 ipdata->size = ip->size;
1649 atomic_clear_int(&ip->flags, HAMMER2_INODE_RESIZED);
1652 * When resizing larger we may not have any direct-data
1655 if ((ipdata->op_flags & HAMMER2_OPFLAG_DIRECTDATA) &&
1656 ip->size > HAMMER2_EMBEDDED_BYTES) {
1657 ipdata->op_flags &= ~HAMMER2_OPFLAG_DIRECTDATA;
1658 bzero(&ipdata->u.blockset, sizeof(ipdata->u.blockset));