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 * The underlying chain is also locked and returned.
68 * NOTE: We don't combine the inode/chain lock because putting away an
69 * inode would otherwise confuse multiple lock holders of the inode.
72 hammer2_inode_lock_ex(hammer2_inode_t *ip)
74 hammer2_chain_t *chain;
76 hammer2_inode_ref(ip);
77 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
80 * ip->chain fixup. Certain duplications used to move inodes
81 * into indirect blocks (for example) can cause ip->chain to
86 if (hammer2_chain_refactor_test(chain, 1)) {
87 spin_lock(&chain->core->cst.spin);
88 while (hammer2_chain_refactor_test(chain, 1))
89 chain = chain->next_parent;
90 if (ip->chain != chain) {
91 hammer2_chain_ref(chain);
92 spin_unlock(&chain->core->cst.spin);
93 hammer2_inode_repoint(ip, NULL, chain);
94 hammer2_chain_drop(chain);
96 spin_unlock(&chain->core->cst.spin);
100 KKASSERT(chain != NULL); /* for now */
101 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS);
104 * Resolve duplication races
106 if (hammer2_chain_refactor_test(chain, 1)) {
107 hammer2_chain_unlock(chain);
114 hammer2_inode_unlock_ex(hammer2_inode_t *ip, hammer2_chain_t *chain)
117 * XXX this will catch parent directories too which we don't
121 hammer2_chain_unlock(chain);
122 ccms_thread_unlock(&ip->topo_cst);
123 hammer2_inode_drop(ip);
127 * NOTE: We don't combine the inode/chain lock because putting away an
128 * inode would otherwise confuse multiple lock holders of the inode.
130 * Shared locks are especially sensitive to having too many shared
131 * lock counts (from the same thread) on certain paths which might
132 * need to upgrade them. Only one count of a shared lock can be
136 hammer2_inode_lock_sh(hammer2_inode_t *ip)
138 hammer2_chain_t *chain;
140 hammer2_inode_ref(ip);
142 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_SHARED);
145 KKASSERT(chain != NULL); /* for now */
146 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS |
147 HAMMER2_RESOLVE_SHARED);
150 * Resolve duplication races
152 if (hammer2_chain_refactor_test(chain, 1)) {
153 hammer2_chain_unlock(chain);
154 ccms_thread_unlock(&ip->topo_cst);
155 chain = hammer2_inode_lock_ex(ip);
156 hammer2_inode_unlock_ex(ip, chain);
163 hammer2_inode_unlock_sh(hammer2_inode_t *ip, hammer2_chain_t *chain)
166 hammer2_chain_unlock(chain);
167 ccms_thread_unlock(&ip->topo_cst);
168 hammer2_inode_drop(ip);
172 hammer2_inode_lock_temp_release(hammer2_inode_t *ip)
174 return(ccms_thread_lock_temp_release(&ip->topo_cst));
178 hammer2_inode_lock_temp_restore(hammer2_inode_t *ip, ccms_state_t ostate)
180 ccms_thread_lock_temp_restore(&ip->topo_cst, ostate);
184 hammer2_inode_lock_upgrade(hammer2_inode_t *ip)
186 return(ccms_thread_lock_upgrade(&ip->topo_cst));
190 hammer2_inode_lock_downgrade(hammer2_inode_t *ip, ccms_state_t ostate)
192 ccms_thread_lock_downgrade(&ip->topo_cst, ostate);
196 * Lookup an inode by inode number
199 hammer2_inode_lookup(hammer2_pfsmount_t *pmp, hammer2_tid_t inum)
204 spin_lock(&pmp->inum_spin);
205 ip = RB_LOOKUP(hammer2_inode_tree, &pmp->inum_tree, inum);
207 hammer2_inode_ref(ip);
208 spin_unlock(&pmp->inum_spin);
216 * Adding a ref to an inode is only legal if the inode already has at least
220 hammer2_inode_ref(hammer2_inode_t *ip)
222 atomic_add_int(&ip->refs, 1);
226 * Drop an inode reference, freeing the inode when the last reference goes
230 hammer2_inode_drop(hammer2_inode_t *ip)
232 hammer2_mount_t *hmp;
233 hammer2_inode_t *pip;
241 * Transition to zero, must interlock with
242 * the inode inumber lookup tree (if applicable).
244 * NOTE: The super-root inode has no pmp.
247 spin_lock(&ip->pmp->inum_spin);
249 if (atomic_cmpset_int(&ip->refs, 1, 0)) {
250 KKASSERT(ip->topo_cst.count == 0);
251 if (ip->flags & HAMMER2_INODE_ONRBTREE) {
252 atomic_clear_int(&ip->flags,
253 HAMMER2_INODE_ONRBTREE);
254 RB_REMOVE(hammer2_inode_tree,
259 spin_unlock(&ip->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.
277 kfree(ip, hmp->minode);
279 /* continue with pip (can be NULL) */
282 spin_unlock(&ip->pmp->inum_spin);
286 * Non zero transition
288 if (atomic_cmpset_int(&ip->refs, refs, refs - 1))
295 * Get the vnode associated with the given inode, allocating the vnode if
296 * necessary. The vnode will be returned exclusively locked.
298 * The caller must lock the inode (shared or exclusive).
300 * Great care must be taken to avoid deadlocks and vnode acquisition/reclaim
304 hammer2_igetv(hammer2_inode_t *ip, int *errorp)
306 hammer2_inode_data_t *ipdata;
307 hammer2_pfsmount_t *pmp;
312 KKASSERT(pmp != NULL);
314 ipdata = &ip->chain->data->ipdata;
318 * Attempt to reuse an existing vnode assignment. It is
319 * possible to race a reclaim so the vget() may fail. The
320 * inode must be unlocked during the vget() to avoid a
321 * deadlock against a reclaim.
326 * Inode must be unlocked during the vget() to avoid
327 * possible deadlocks, but leave the ip ref intact.
329 * vnode is held to prevent destruction during the
330 * vget(). The vget() can still fail if we lost
331 * a reclaim race on the vnode.
333 vhold_interlocked(vp);
334 ostate = hammer2_inode_lock_temp_release(ip);
335 if (vget(vp, LK_EXCLUSIVE)) {
337 hammer2_inode_lock_temp_restore(ip, ostate);
340 hammer2_inode_lock_temp_restore(ip, ostate);
342 /* vp still locked and ref from vget */
344 kprintf("hammer2: igetv race %p/%p\n",
354 * No vnode exists, allocate a new vnode. Beware of
355 * allocation races. This function will return an
356 * exclusively locked and referenced vnode.
358 *errorp = getnewvnode(VT_HAMMER2, pmp->mp, &vp, 0, 0);
360 kprintf("hammer2: igetv getnewvnode failed %d\n",
367 * Lock the inode and check for an allocation race.
369 ostate = hammer2_inode_lock_upgrade(ip);
370 if (ip->vp != NULL) {
373 hammer2_inode_lock_downgrade(ip, ostate);
377 switch (ipdata->type) {
378 case HAMMER2_OBJTYPE_DIRECTORY:
381 case HAMMER2_OBJTYPE_REGFILE:
383 vinitvmio(vp, ipdata->size,
385 (int)ipdata->size & HAMMER2_LBUFMASK);
387 case HAMMER2_OBJTYPE_SOFTLINK:
389 * XXX for now we are using the generic file_read
390 * and file_write code so we need a buffer cache
394 vinitvmio(vp, ipdata->size,
396 (int)ipdata->size & HAMMER2_LBUFMASK);
400 panic("hammer2: unhandled objtype %d", ipdata->type);
404 if (ip == pmp->iroot)
405 vsetflags(vp, VROOT);
409 hammer2_inode_ref(ip); /* vp association */
410 hammer2_inode_lock_downgrade(ip, ostate);
415 * Return non-NULL vp and *errorp == 0, or NULL vp and *errorp != 0.
417 if (hammer2_debug & 0x0002) {
418 kprintf("igetv vp %p refs %d aux %d\n",
419 vp, vp->v_sysref.refcnt, vp->v_auxrefs);
425 * The passed-in chain must be locked and the returned inode will also be
426 * locked. This routine typically locates or allocates the inode, assigns
427 * ip->chain (adding a ref to chain if necessary), and returns the inode.
429 * The hammer2_inode structure regulates the interface between the high level
430 * kernel VNOPS API and the filesystem backend (the chains).
432 * WARNING! This routine sucks up the chain's lock (makes it part of the
433 * inode lock from the point of view of the inode lock API),
434 * so callers need to be careful.
436 * WARNING! The mount code is allowed to pass dip == NULL for iroot and
437 * is allowed to pass pmp == NULL and dip == NULL for sroot.
440 hammer2_inode_get(hammer2_mount_t *hmp, hammer2_pfsmount_t *pmp,
441 hammer2_inode_t *dip, hammer2_chain_t *chain)
443 hammer2_inode_t *nip;
445 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_INODE);
448 * Interlocked lookup/ref of the inode. This code is only needed
449 * when looking up inodes with nlinks != 0 (TODO: optimize out
450 * otherwise and test for duplicates).
454 nip = hammer2_inode_lookup(pmp, chain->data->ipdata.inum);
457 ccms_thread_lock(&nip->topo_cst, CCMS_STATE_EXCLUSIVE);
458 if ((nip->flags & HAMMER2_INODE_ONRBTREE) == 0) { /* race */
459 ccms_thread_unlock(&nip->topo_cst);
460 hammer2_inode_drop(nip);
463 if (nip->chain != chain)
464 hammer2_inode_repoint(nip, NULL, chain);
467 * Consolidated nip/nip->chain is locked (chain locked
474 * We couldn't find the inode number, create a new inode.
476 nip = kmalloc(sizeof(*nip), hmp->minode, M_WAITOK | M_ZERO);
477 nip->inum = chain->data->ipdata.inum;
478 hammer2_inode_repoint(nip, NULL, chain);
479 nip->pip = dip; /* can be NULL */
481 hammer2_inode_ref(dip); /* ref dip for nip->pip */
487 * ref and lock on nip gives it state compatible to after a
488 * hammer2_inode_lock_ex() call.
491 ccms_cst_init(&nip->topo_cst, &nip->chain);
492 ccms_thread_lock(&nip->topo_cst, CCMS_STATE_EXCLUSIVE);
493 /* combination of thread lock and chain lock == inode lock */
496 * Attempt to add the inode. If it fails we raced another inode
497 * get. Undo all the work and try again.
500 spin_lock(&pmp->inum_spin);
501 if (RB_INSERT(hammer2_inode_tree, &pmp->inum_tree, nip)) {
502 spin_unlock(&pmp->inum_spin);
503 ccms_thread_unlock(&nip->topo_cst);
504 hammer2_inode_drop(nip);
507 atomic_set_int(&nip->flags, HAMMER2_INODE_ONRBTREE);
508 spin_unlock(&pmp->inum_spin);
515 * Create a new inode in the specified directory using the vattr to
516 * figure out the type of inode.
518 * If no error occurs the new inode with its chain locked is returned in
519 * *nipp, otherwise an error is returned and *nipp is set to NULL.
521 * If vap and/or cred are NULL the related fields are not set and the
522 * inode type defaults to a directory. This is used when creating PFSs
523 * under the super-root, so the inode number is set to 1 in this case.
525 * dip is not locked on entry.
528 hammer2_inode_create(hammer2_trans_t *trans, hammer2_inode_t *dip,
529 struct vattr *vap, struct ucred *cred,
530 const uint8_t *name, size_t name_len,
531 hammer2_chain_t **chainp, int *errorp)
533 hammer2_inode_data_t *dipdata;
534 hammer2_inode_data_t *nipdata;
535 hammer2_mount_t *hmp;
536 hammer2_chain_t *chain;
537 hammer2_chain_t *parent;
538 hammer2_inode_t *nip;
547 lhc = hammer2_dirhash(name, name_len);
551 * Locate the inode or indirect block to create the new
552 * entry in. At the same time check for key collisions
553 * and iterate until we don't get one.
555 * NOTE: hidden inodes do not have iterators.
558 parent = hammer2_inode_lock_ex(dip);
559 dipdata = &dip->chain->data->ipdata;
560 dip_uid = dipdata->uid;
561 dip_gid = dipdata->gid;
562 dip_mode = dipdata->mode;
566 chain = hammer2_chain_lookup(&parent, lhc, lhc, 0);
569 if ((lhc & HAMMER2_DIRHASH_VISIBLE) == 0)
571 if ((lhc & HAMMER2_DIRHASH_LOMASK) == HAMMER2_DIRHASH_LOMASK)
573 hammer2_chain_unlock(chain);
578 error = hammer2_chain_create(trans, &parent, &chain,
580 HAMMER2_BREF_TYPE_INODE,
581 HAMMER2_INODE_BYTES);
585 * Cleanup and handle retries.
587 if (error == EAGAIN) {
588 hammer2_chain_ref(parent);
589 hammer2_inode_unlock_ex(dip, parent);
590 hammer2_chain_wait(parent);
591 hammer2_chain_drop(parent);
594 hammer2_inode_unlock_ex(dip, parent);
597 KKASSERT(chain == NULL);
603 * Set up the new inode.
605 * NOTE: *_get() integrates chain's lock into the inode lock.
607 * NOTE: Only one new inode can currently be created per
608 * transaction. If the need arises we can adjust
609 * hammer2_trans_init() to allow more.
611 chain->data->ipdata.inum = trans->sync_tid;
612 nip = hammer2_inode_get(dip->hmp, dip->pmp, dip, chain);
613 nipdata = &chain->data->ipdata;
616 KKASSERT(trans->inodes_created == 0);
617 nipdata->type = hammer2_get_obj_type(vap->va_type);
618 nipdata->inum = trans->sync_tid;
619 ++trans->inodes_created;
621 nipdata->type = HAMMER2_OBJTYPE_DIRECTORY;
624 nipdata->version = HAMMER2_INODE_VERSION_ONE;
625 hammer2_update_time(&nipdata->ctime);
626 nipdata->mtime = nipdata->ctime;
628 nipdata->mode = vap->va_mode;
632 xuid = hammer2_to_unix_xid(&dip_uid);
633 xuid = vop_helper_create_uid(dip->pmp->mp,
641 if (vap->va_vaflags & VA_UID_UUID_VALID)
642 nipdata->uid = vap->va_uid_uuid;
643 else if (vap->va_uid != (uid_t)VNOVAL)
644 hammer2_guid_to_uuid(&nipdata->uid, vap->va_uid);
646 hammer2_guid_to_uuid(&nipdata->uid, xuid);
648 if (vap->va_vaflags & VA_GID_UUID_VALID)
649 nipdata->gid = vap->va_gid_uuid;
650 else if (vap->va_gid != (gid_t)VNOVAL)
651 hammer2_guid_to_uuid(&nipdata->gid, vap->va_gid);
653 nipdata->gid = dip_gid;
657 * Regular files and softlinks allow a small amount of data to be
658 * directly embedded in the inode. This flag will be cleared if
659 * the size is extended past the embedded limit.
661 if (nipdata->type == HAMMER2_OBJTYPE_REGFILE ||
662 nipdata->type == HAMMER2_OBJTYPE_SOFTLINK) {
663 nipdata->op_flags |= HAMMER2_OPFLAG_DIRECTDATA;
666 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
667 bcopy(name, nipdata->filename, name_len);
668 nipdata->name_key = lhc;
669 nipdata->name_len = name_len;
676 * chain may have been moved around by the create.
680 hammer2_chain_refactor(hammer2_chain_t **chainp)
682 hammer2_chain_t *chain = *chainp;
683 hammer2_chain_core_t *core;
686 spin_lock(&core->cst.spin);
687 while (hammer2_chain_refactor_test(chain, 1)) {
688 chain = chain->next_parent;
689 while (hammer2_chain_refactor_test(chain, 1))
690 chain = chain->next_parent;
691 hammer2_chain_ref(chain);
692 spin_unlock(&core->cst.spin);
694 hammer2_chain_unlock(*chainp);
695 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS |
696 HAMMER2_RESOLVE_NOREF); /* eat ref */
698 spin_lock(&core->cst.spin);
700 spin_unlock(&core->cst.spin);
704 * ochain represents the target file inode. We need to move it to the
705 * specified common parent directory (dip) and rename it to a special
706 * invisible "0xINODENUMBER" filename.
708 * We use chain_duplicate and duplicate ochain at the new location,
709 * renaming it appropriately. We create a temporary chain and
710 * then delete it to placemark where the duplicate will go. Both of
711 * these use the inode number for (lhc) (the key), generating the
712 * invisible filename.
716 hammer2_hardlink_shiftup(hammer2_trans_t *trans, hammer2_chain_t **ochainp,
717 hammer2_inode_t *dip, int *errorp)
719 hammer2_inode_data_t *nipdata;
720 hammer2_mount_t *hmp;
721 hammer2_chain_t *parent;
722 hammer2_chain_t *ochain;
723 hammer2_chain_t *nchain;
724 hammer2_chain_t *tmp;
726 hammer2_blockref_t bref;
731 lhc = ochain->data->ipdata.inum;
732 KKASSERT((lhc & HAMMER2_DIRHASH_VISIBLE) == 0);
735 * Locate the inode or indirect block to create the new
736 * entry in. lhc represents the inode number so there is
737 * no collision iteration.
739 * There should be no key collisions with invisible inode keys.
742 parent = hammer2_chain_lookup_init(dip->chain, 0);
743 nchain = hammer2_chain_lookup(&parent, lhc, lhc, 0);
745 kprintf("X3 chain %p parent %p dip %p dip->chain %p\n",
746 nchain, parent, dip, dip->chain);
747 hammer2_chain_unlock(nchain);
756 * Create entry in common parent directory using the seek position
760 KKASSERT(nchain == NULL);
761 *errorp = hammer2_chain_create(trans, &parent, &nchain,
763 HAMMER2_BREF_TYPE_INODE,/* n/a */
764 HAMMER2_INODE_BYTES); /* n/a */
765 hammer2_chain_refactor(&ochain);
770 * Cleanup and handle retries.
772 if (*errorp == EAGAIN) {
773 hammer2_chain_ref(parent);
774 hammer2_chain_lookup_done(parent);
775 hammer2_chain_wait(parent);
776 hammer2_chain_drop(parent);
781 * Handle the error case
784 KKASSERT(nchain == NULL);
785 hammer2_chain_lookup_done(parent);
790 * Use chain as a placeholder for (lhc), delete it and replace
791 * it with our duplication.
793 * Gain a second lock on ochain for the duplication function to
794 * unlock, maintain the caller's original lock across the call.
796 * This is a bit messy.
798 hammer2_chain_delete(trans, nchain);
799 hammer2_chain_lock(ochain, HAMMER2_RESOLVE_ALWAYS);
802 bref.key = lhc; /* invisible dir entry key */
804 hammer2_chain_duplicate(trans, parent, nchain->index, &tmp, &bref);
805 hammer2_chain_lookup_done(parent);
806 hammer2_chain_unlock(nchain); /* no longer needed */
809 * Now set chain to our duplicate and modify it appropriately.
811 * Directory entries are inodes but this is a hidden hardlink
812 * target. The name isn't used but to ease debugging give it
813 * a name after its inode number.
816 tmp = NULL; /* safety */
818 hammer2_chain_modify(trans, &nchain, HAMMER2_MODIFY_ASSERTNOCOPY);
819 nipdata = &nchain->data->ipdata;
820 ksnprintf(nipdata->filename, sizeof(nipdata->filename),
821 "0x%016jx", (intmax_t)nipdata->inum);
822 nipdata->name_len = strlen(nipdata->filename);
823 nipdata->name_key = lhc;
829 * Connect the target inode represented by (*chainp) to the media topology
830 * at (dip, name, len).
832 * If hlink is TRUE this function creates an OBJTYPE_HARDLINK directory
833 * entry instead of connecting (*chainp).
835 * If hlink is FALSE this function uses chain_duplicate() to make a copy
836 * if (*chainp) in the directory entry. (*chainp) is likely to be deleted
837 * by the caller in this case (e.g. rename).
840 hammer2_inode_connect(hammer2_trans_t *trans, int hlink,
841 hammer2_inode_t *dip, hammer2_chain_t **chainp,
842 const uint8_t *name, size_t name_len)
844 hammer2_inode_data_t *ipdata;
845 hammer2_mount_t *hmp;
846 hammer2_chain_t *nchain;
847 hammer2_chain_t *parent;
848 hammer2_chain_t *ochain;
857 * Since ochain is either disconnected from the topology or represents
858 * a hardlink terminus which is always a parent of or equal to dip,
859 * we should be able to safely lock dip->chain for our setup.
861 parent = hammer2_chain_lookup_init(dip->chain, 0);
863 lhc = hammer2_dirhash(name, name_len);
866 * Locate the inode or indirect block to create the new
867 * entry in. At the same time check for key collisions
868 * and iterate until we don't get one.
872 nchain = hammer2_chain_lookup(&parent, lhc, lhc, 0);
875 if ((lhc & HAMMER2_DIRHASH_LOMASK) == HAMMER2_DIRHASH_LOMASK)
877 hammer2_chain_unlock(nchain);
885 * Hardlink pointer needed, create totally fresh
888 KKASSERT(nchain == NULL);
889 error = hammer2_chain_create(trans, &parent, &nchain,
891 HAMMER2_BREF_TYPE_INODE,
892 HAMMER2_INODE_BYTES);
893 hammer2_chain_refactor(&ochain);
896 * Reconnect the original chain and rename. Use
897 * chain_duplicate(). The caller will likely delete
898 * or has already deleted the original chain in
901 * NOTE: chain_duplicate() generates a new chain
902 * with CHAIN_DELETED cleared (ochain typically
903 * has it set from the file unlink).
907 hammer2_chain_duplicate(trans, NULL, -1, &nchain, NULL);
908 error = hammer2_chain_create(trans, &parent, &nchain,
910 HAMMER2_BREF_TYPE_INODE,
911 HAMMER2_INODE_BYTES);
918 KKASSERT(error != EAGAIN);
919 hammer2_chain_lookup_done(parent);
923 * nchain should be NULL on error, leave ochain (== *chainp) alone.
926 KKASSERT(nchain == NULL);
931 * Directory entries are inodes so if the name has changed we have
932 * to update the inode.
934 * When creating an OBJTYPE_HARDLINK entry remember to unlock the
935 * chain, the caller will access the hardlink via the actual hardlink
936 * target file and not the hardlink pointer entry, so we must still
939 if (hlink && hammer2_hardlink_enable >= 0) {
941 * Create the HARDLINK pointer. oip represents the hardlink
942 * target in this situation.
944 * We will return ochain (the hardlink target).
946 hammer2_chain_modify(trans, &nchain,
947 HAMMER2_MODIFY_ASSERTNOCOPY);
948 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
949 ipdata = &nchain->data->ipdata;
950 bcopy(name, ipdata->filename, name_len);
951 ipdata->name_key = lhc;
952 ipdata->name_len = name_len;
953 ipdata->target_type = ochain->data->ipdata.type;
954 ipdata->type = HAMMER2_OBJTYPE_HARDLINK;
955 ipdata->inum = ochain->data->ipdata.inum;
957 hammer2_chain_unlock(nchain);
960 } else if (hlink && hammer2_hardlink_enable < 0) {
962 * Create a snapshot (hardlink fake mode for debugging).
963 * (ochain already flushed above so we can just copy the
966 * Since this is a snapshot we return nchain in the fake
969 hammer2_chain_modify(trans, &nchain,
970 HAMMER2_MODIFY_ASSERTNOCOPY);
971 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
972 ipdata = &nchain->data->ipdata;
973 *ipdata = ochain->data->ipdata;
974 bcopy(name, ipdata->filename, name_len);
975 ipdata->name_key = lhc;
976 ipdata->name_len = name_len;
977 kprintf("created fake hardlink %*.*s\n",
978 (int)name_len, (int)name_len, name);
981 * nchain is a duplicate of ochain at the new location.
982 * We must fixup the name stored in oip. The bref key
983 * has already been set up.
985 hammer2_chain_modify(trans, &nchain,
986 HAMMER2_MODIFY_ASSERTNOCOPY);
987 ipdata = &nchain->data->ipdata;
989 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
990 bcopy(name, ipdata->filename, name_len);
991 ipdata->name_key = lhc;
992 ipdata->name_len = name_len;
997 * We are replacing ochain with nchain, unlock ochain. In the
998 * case where ochain is left unchanged the code above sets
999 * nchain to ochain and ochain to NULL, resulting in a NOP here.
1002 hammer2_chain_unlock(ochain);
1009 * Repoint ip->chain to nchain. Caller must hold the inode exclusively
1012 * ip->chain is set to nchain. The prior chain in ip->chain is dropped
1013 * and nchain is ref'd.
1015 * This function is somewhat more complex. When changing ip->chain it
1016 * must iterate through chain->next_parent and adjust CHAIN_IPACTIVE
1017 * appropriately. CHAIN_IPACTIVE will also be set on nchain if necessary
1018 * (caller should pass a locked nchain in that case). nchain will often
1019 * already have the bit set as part of a delete/duplicate sequence if
1020 * the deleted chain had it set. Refs must also be adjusted for this flag.
1023 hammer2_inode_repoint(hammer2_inode_t *ip, hammer2_inode_t *pip,
1024 hammer2_chain_t *nchain)
1026 hammer2_chain_t *ochain;
1027 hammer2_chain_t *xchain;
1028 hammer2_inode_t *opip;
1032 * Repoint ip->chain if requested.
1034 if (ip->chain == NULL) {
1036 * ip->chain is NULL, we don't have to worry about having
1037 * to clear the IPACTIVE flag along the (non-existant) chain.
1038 * Just assign nchain and set the flag on nchain as necessary.
1041 spin_lock(&nchain->core->cst.spin);
1042 if ((nchain->flags & HAMMER2_CHAIN_IPACTIVE) == 0) {
1043 atomic_set_int(&nchain->flags,
1044 HAMMER2_CHAIN_IPACTIVE);
1045 hammer2_chain_ref(nchain);
1047 spin_unlock(&nchain->core->cst.spin);
1049 hammer2_chain_ref(nchain);
1051 } else if (ip->chain != nchain) {
1053 * Iterate the non-NULL ip->chain clearing IPACTIVE on the
1054 * old chains and setting it on the new ones, until we reach
1057 while ((ochain = ip->chain) != NULL) {
1058 spin_lock(&ochain->core->cst.spin);
1059 xchain = ochain->next_parent;
1062 if (xchain->flags & HAMMER2_CHAIN_SNAPSHOT) {
1064 spin_unlock(&ochain->core->cst.spin);
1067 if (!(xchain->flags & HAMMER2_CHAIN_IPACTIVE)) {
1068 atomic_set_int(&xchain->flags,
1069 HAMMER2_CHAIN_IPACTIVE);
1070 hammer2_chain_ref(xchain);
1072 hammer2_chain_ref(xchain);
1073 KKASSERT(ochain->core == xchain->core);
1075 oflags = ochain->flags;
1076 atomic_clear_int(&ochain->flags,
1077 HAMMER2_CHAIN_IPACTIVE);
1078 spin_unlock(&ochain->core->cst.spin);
1080 if (oflags & HAMMER2_CHAIN_IPACTIVE)
1081 hammer2_chain_drop(ochain);
1082 hammer2_chain_drop(ochain);
1084 if (xchain == nchain) {
1085 ochain = xchain; /* for match below */
1091 * We should always match at the end of the iteration. If
1092 * not then nchain somehow got severely disconnected from
1093 * the inode's existing chain sequence.
1095 * Complain loudly but try to recover the situation.
1097 if (ochain != nchain) {
1098 kprintf("hammer2_inode_repoint: lost IPACTIVE seq "
1099 "ip=%p nchain=%p\n", ip, nchain);
1100 spin_lock(&nchain->core->cst.spin);
1101 if ((nchain->flags & HAMMER2_CHAIN_IPACTIVE) == 0) {
1102 atomic_set_int(&nchain->flags,
1103 HAMMER2_CHAIN_IPACTIVE);
1104 hammer2_chain_ref(nchain);
1106 spin_unlock(&nchain->core->cst.spin);
1108 hammer2_chain_ref(nchain);
1113 * Repoint ip->pip if requested (non-NULL pip).
1115 if (pip && ip->pip != pip) {
1117 hammer2_inode_ref(pip);
1120 hammer2_inode_drop(opip);
1125 * Unlink the file from the specified directory inode. The directory inode
1126 * does not need to be locked.
1128 * isdir determines whether a directory/non-directory check should be made.
1129 * No check is made if isdir is set to -1.
1131 * NOTE! This function does not prevent the underlying file from still
1132 * being used if it has other refs (such as from an inode, or if it's
1133 * chain is manually held). However, the caller is responsible for
1134 * fixing up ip->chain if e.g. a rename occurs (see chain_duplicate()).
1137 hammer2_unlink_file(hammer2_trans_t *trans, hammer2_inode_t *dip,
1138 const uint8_t *name, size_t name_len,
1139 int isdir, int *hlinkp)
1141 hammer2_inode_data_t *ipdata;
1142 hammer2_mount_t *hmp;
1143 hammer2_chain_t *parent;
1144 hammer2_chain_t *ochain;
1145 hammer2_chain_t *chain;
1146 hammer2_chain_t *dparent;
1147 hammer2_chain_t *dchain;
1155 lhc = hammer2_dirhash(name, name_len);
1158 * Search for the filename in the directory
1162 parent = hammer2_inode_lock_ex(dip);
1163 chain = hammer2_chain_lookup(&parent,
1164 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1167 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
1168 name_len == chain->data->ipdata.name_len &&
1169 bcmp(name, chain->data->ipdata.filename, name_len) == 0) {
1172 chain = hammer2_chain_next(&parent, chain,
1173 lhc, 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,
1229 0, (hammer2_key_t)-1,
1230 HAMMER2_LOOKUP_NODATA);
1232 hammer2_chain_unlock(dchain);
1233 hammer2_chain_lookup_done(dparent);
1237 hammer2_chain_lookup_done(dparent);
1243 * Ok, we can now unlink the chain. We always decrement nlinks even
1244 * if the entry can be deleted in case someone has the file open and
1247 * The chain itself will no longer be in the on-media topology but
1248 * can still be flushed to the media (e.g. if an open descriptor
1249 * remains). When the last vnode/ip ref goes away the chain will
1250 * be marked unmodified, avoiding any further (now unnecesary) I/O.
1252 * A non-NULL ochain indicates a hardlink.
1256 * Delete the original hardlink pointer.
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);
1263 hammer2_chain_unlock(ochain);
1266 * Then decrement nlinks on hardlink target, deleting
1267 * the target when nlinks drops to 0.
1269 hammer2_chain_modify(trans, &chain, 0);
1270 --chain->data->ipdata.nlinks;
1271 if (chain->data->ipdata.nlinks == 0)
1272 hammer2_chain_delete(trans, chain);
1275 * Otherwise this was not a hardlink and we can just
1276 * remove the entry and decrement nlinks.
1278 * NOTE: *_get() integrates chain's lock into the inode lock.
1280 hammer2_chain_modify(trans, &chain, 0);
1281 ipdata = &chain->data->ipdata;
1283 hammer2_chain_delete(trans, chain);
1289 hammer2_chain_unlock(chain);
1291 hammer2_chain_lookup_done(parent);
1293 hammer2_chain_drop(ochain);
1299 * Calculate the allocation size for the file fragment straddling EOF
1302 hammer2_inode_calc_alloc(hammer2_key_t filesize)
1304 int frag = (int)filesize & HAMMER2_PBUFMASK;
1309 for (radix = HAMMER2_MINALLOCRADIX; frag > (1 << radix); ++radix)
1315 * Given an exclusively locked inode we consolidate its chain for hardlink
1316 * creation, adding (nlinks) to the file's link count and potentially
1317 * relocating the inode to a directory common to ip->pip and tdip.
1319 * Replaces (*chainp) if consolidation occurred, unlocking the old chain
1320 * and returning a new locked chain.
1322 * NOTE! This function will also replace ip->chain.
1325 hammer2_hardlink_consolidate(hammer2_trans_t *trans, hammer2_inode_t *ip,
1326 hammer2_chain_t **chainp,
1327 hammer2_inode_t *tdip, int nlinks)
1329 hammer2_inode_data_t *ipdata;
1330 hammer2_mount_t *hmp;
1331 hammer2_inode_t *fdip;
1332 hammer2_inode_t *cdip;
1333 hammer2_chain_t *chain;
1334 hammer2_chain_t *nchain;
1340 if (nlinks == 0 && /* no hardlink needed */
1341 (chain->data->ipdata.name_key & HAMMER2_DIRHASH_VISIBLE)) {
1344 if (hammer2_hardlink_enable < 0) { /* fake hardlinks */
1348 if (hammer2_hardlink_enable == 0) { /* disallow hardlinks */
1349 hammer2_chain_unlock(chain);
1355 * cdip will be returned with a ref, but not locked.
1358 cdip = hammer2_inode_common_parent(fdip, tdip);
1361 * If no change in the hardlink's target directory is required and
1362 * this is already a hardlink target, all we need to do is adjust
1365 * XXX The common parent is a big wiggly due to duplication from
1366 * renames. Compare the core (RBTREE) pointer instead of the
1370 (chain->data->ipdata.name_key & HAMMER2_DIRHASH_VISIBLE) == 0) {
1372 hammer2_chain_modify(trans, &chain, 0);
1373 chain->data->ipdata.nlinks += nlinks;
1380 * We either have to move an existing hardlink target or we have
1381 * to create a fresh hardlink target.
1383 * Hardlink targets are hidden inodes in a parent directory common
1384 * to all directory entries referencing the hardlink.
1386 nchain = hammer2_hardlink_shiftup(trans, &chain, cdip, &error);
1390 * Bump nlinks on duplicated hidden inode, repoint
1393 hammer2_chain_modify(trans, &nchain, 0);
1394 nchain->data->ipdata.nlinks += nlinks;
1395 hammer2_inode_repoint(ip, cdip, nchain);
1398 * If the old chain is not a hardlink target then replace
1399 * it with a OBJTYPE_HARDLINK pointer.
1401 * If the old chain IS a hardlink target then delete it.
1403 if (chain->data->ipdata.name_key & HAMMER2_DIRHASH_VISIBLE) {
1405 * Replace original non-hardlink that's been dup'd
1406 * with a special hardlink directory entry. We must
1407 * set the DIRECTDATA flag to prevent sub-chains
1408 * from trying to synchronize to the inode if the
1409 * file is extended afterwords.
1411 hammer2_chain_modify(trans, &chain, 0);
1412 ipdata = &chain->data->ipdata;
1413 ipdata->target_type = ipdata->type;
1414 ipdata->type = HAMMER2_OBJTYPE_HARDLINK;
1422 bzero(&ipdata->uid, sizeof(ipdata->uid));
1423 bzero(&ipdata->gid, sizeof(ipdata->gid));
1424 ipdata->op_flags = HAMMER2_OPFLAG_DIRECTDATA;
1425 ipdata->cap_flags = 0;
1429 ipdata->iparent = 0; /* XXX */
1430 ipdata->pfs_type = 0;
1431 ipdata->pfs_inum = 0;
1432 bzero(&ipdata->pfs_clid, sizeof(ipdata->pfs_clid));
1433 bzero(&ipdata->pfs_fsid, sizeof(ipdata->pfs_fsid));
1434 ipdata->data_quota = 0;
1435 ipdata->data_count = 0;
1436 ipdata->inode_quota = 0;
1437 ipdata->inode_count = 0;
1438 ipdata->attr_tid = 0;
1439 ipdata->dirent_tid = 0;
1440 bzero(&ipdata->u, sizeof(ipdata->u));
1441 /* XXX transaction ids */
1443 kprintf("DELETE INVISIBLE\n");
1444 hammer2_chain_delete(trans, chain);
1448 * Return the new chain.
1450 hammer2_chain_unlock(chain);
1456 hammer2_chain_unlock(chain);
1461 * Cleanup, chain/nchain already dealt with.
1465 hammer2_inode_drop(cdip);
1471 * If (*ochainp) is non-NULL it points to the forward OBJTYPE_HARDLINK
1472 * inode while (*chainp) points to the resolved (hidden hardlink
1473 * target) inode. In this situation when nlinks is 1 we wish to
1474 * deconsolidate the hardlink, moving it back to the directory that now
1475 * represents the only remaining link.
1478 hammer2_hardlink_deconsolidate(hammer2_trans_t *trans,
1479 hammer2_inode_t *dip,
1480 hammer2_chain_t **chainp,
1481 hammer2_chain_t **ochainp)
1483 if (*ochainp == NULL)
1490 * The caller presents a locked *chainp pointing to a HAMMER2_BREF_TYPE_INODE
1491 * with an obj_type of HAMMER2_OBJTYPE_HARDLINK. This routine will gobble
1492 * the *chainp and return a new locked *chainp representing the file target
1493 * (the original *chainp will be unlocked).
1495 * When a match is found the chain representing the original HARDLINK
1496 * will be returned in *ochainp with a ref, but not locked.
1498 * When no match is found *chainp is set to NULL and EIO is returned.
1499 * (*ochainp) will still be set to the original chain with a ref but not
1503 hammer2_hardlink_find(hammer2_inode_t *dip, hammer2_chain_t **chainp,
1504 hammer2_chain_t **ochainp)
1506 hammer2_chain_t *chain = *chainp;
1507 hammer2_chain_t *parent;
1508 hammer2_inode_t *ip;
1509 hammer2_inode_t *pip;
1513 hammer2_inode_ref(pip); /* for loop */
1514 hammer2_chain_ref(chain); /* for (*ochainp) */
1518 * Locate the hardlink. pip is referenced and not locked,
1523 lhc = chain->data->ipdata.inum;
1524 hammer2_chain_unlock(chain);
1527 while ((ip = pip) != NULL) {
1528 parent = hammer2_inode_lock_ex(ip);
1529 hammer2_inode_drop(ip); /* loop */
1530 KKASSERT(parent->bref.type == HAMMER2_BREF_TYPE_INODE);
1531 chain = hammer2_chain_lookup(&parent, lhc, lhc, 0);
1532 hammer2_chain_lookup_done(parent); /* discard parent */
1535 pip = ip->pip; /* safe, ip held locked */
1537 hammer2_inode_ref(pip); /* loop */
1538 hammer2_inode_unlock_ex(ip, NULL);
1542 * chain is locked, ip is locked. Unlock ip, return the locked
1543 * chain. *ipp is already set w/a ref count and not locked.
1545 * (parent is already unlocked).
1548 hammer2_inode_unlock_ex(ip, NULL);
1551 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_INODE);
1552 /* already locked */
1560 * Find the directory common to both fdip and tdip, hold and return
1564 hammer2_inode_common_parent(hammer2_inode_t *fdip, hammer2_inode_t *tdip)
1566 hammer2_inode_t *scan1;
1567 hammer2_inode_t *scan2;
1570 * We used to have a depth field but it complicated matters too
1571 * much for directory renames. So now its ugly. Check for
1572 * simple cases before giving up and doing it the expensive way.
1574 * XXX need a bottom-up topology stability lock
1576 if (fdip == tdip || fdip == tdip->pip) {
1577 hammer2_inode_ref(fdip);
1580 if (fdip->pip == tdip) {
1581 hammer2_inode_ref(tdip);
1588 for (scan1 = fdip; scan1->pmp == fdip->pmp; scan1 = scan1->pip) {
1590 while (scan2->pmp == tdip->pmp) {
1591 if (scan1 == scan2) {
1592 hammer2_inode_ref(scan1);
1600 panic("hammer2_inode_common_parent: no common parent %p %p\n",