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 which can cause ip's cached
67 * 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_core_t *core;
80 hammer2_inode_ref(ip);
81 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
86 if (chain->flags & HAMMER2_CHAIN_DUPLICATED) {
87 spin_lock(&core->cst.spin);
88 while (chain->flags & HAMMER2_CHAIN_DUPLICATED)
89 chain = TAILQ_NEXT(chain, core_entry);
90 hammer2_chain_ref(chain);
91 spin_unlock(&core->cst.spin);
92 hammer2_inode_repoint(ip, NULL, chain);
93 hammer2_chain_drop(chain);
95 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS);
96 if ((chain->flags & HAMMER2_CHAIN_DUPLICATED) == 0)
98 hammer2_chain_unlock(chain);
104 hammer2_inode_unlock_ex(hammer2_inode_t *ip, hammer2_chain_t *chain)
107 * XXX this will catch parent directories too which we don't
111 hammer2_chain_unlock(chain);
112 ccms_thread_unlock(&ip->topo_cst);
113 hammer2_inode_drop(ip);
117 * NOTE: We don't combine the inode/chain lock because putting away an
118 * inode would otherwise confuse multiple lock holders of the inode.
120 * Shared locks are especially sensitive to having too many shared
121 * lock counts (from the same thread) on certain paths which might
122 * need to upgrade them. Only one count of a shared lock can be
126 hammer2_inode_lock_sh(hammer2_inode_t *ip)
128 hammer2_chain_t *chain;
130 hammer2_inode_ref(ip);
132 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_SHARED);
135 KKASSERT(chain != NULL); /* for now */
136 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS |
137 HAMMER2_RESOLVE_SHARED);
140 * Resolve duplication races
142 if ((chain->flags & HAMMER2_CHAIN_DUPLICATED) == 0)
144 hammer2_chain_unlock(chain);
145 ccms_thread_unlock(&ip->topo_cst);
146 chain = hammer2_inode_lock_ex(ip);
147 hammer2_inode_unlock_ex(ip, chain);
153 hammer2_inode_unlock_sh(hammer2_inode_t *ip, hammer2_chain_t *chain)
156 hammer2_chain_unlock(chain);
157 ccms_thread_unlock(&ip->topo_cst);
158 hammer2_inode_drop(ip);
162 hammer2_inode_lock_temp_release(hammer2_inode_t *ip)
164 return(ccms_thread_lock_temp_release(&ip->topo_cst));
168 hammer2_inode_lock_temp_restore(hammer2_inode_t *ip, ccms_state_t ostate)
170 ccms_thread_lock_temp_restore(&ip->topo_cst, ostate);
174 hammer2_inode_lock_upgrade(hammer2_inode_t *ip)
176 return(ccms_thread_lock_upgrade(&ip->topo_cst));
180 hammer2_inode_lock_downgrade(hammer2_inode_t *ip, ccms_state_t ostate)
182 ccms_thread_lock_downgrade(&ip->topo_cst, ostate);
186 * Lookup an inode by inode number
189 hammer2_inode_lookup(hammer2_pfsmount_t *pmp, hammer2_tid_t inum)
194 spin_lock(&pmp->inum_spin);
195 ip = RB_LOOKUP(hammer2_inode_tree, &pmp->inum_tree, inum);
197 hammer2_inode_ref(ip);
198 spin_unlock(&pmp->inum_spin);
206 * Adding a ref to an inode is only legal if the inode already has at least
210 hammer2_inode_ref(hammer2_inode_t *ip)
212 atomic_add_int(&ip->refs, 1);
216 * Drop an inode reference, freeing the inode when the last reference goes
220 hammer2_inode_drop(hammer2_inode_t *ip)
222 hammer2_pfsmount_t *pmp;
223 hammer2_inode_t *pip;
231 * Transition to zero, must interlock with
232 * the inode inumber lookup tree (if applicable).
234 * NOTE: The super-root inode has no pmp.
238 spin_lock(&pmp->inum_spin);
240 if (atomic_cmpset_int(&ip->refs, 1, 0)) {
241 KKASSERT(ip->topo_cst.count == 0);
242 if (ip->flags & HAMMER2_INODE_ONRBTREE) {
243 atomic_clear_int(&ip->flags,
244 HAMMER2_INODE_ONRBTREE);
245 RB_REMOVE(hammer2_inode_tree,
246 &pmp->inum_tree, ip);
249 spin_unlock(&pmp->inum_spin);
256 * Cleaning out ip->chain isn't entirely
259 hammer2_inode_repoint(ip, NULL, NULL);
262 * We have to drop pip (if non-NULL) to
263 * dispose of our implied reference from
264 * ip->pip. We can simply loop on it.
267 KKASSERT((ip->flags &
268 HAMMER2_INODE_SROOT) == 0);
269 kfree(ip, pmp->minode);
270 atomic_add_long(&pmp->inmem_inodes, -1);
273 HAMMER2_INODE_SROOT);
274 kfree(ip, M_HAMMER2);
277 /* continue with pip (can be NULL) */
280 spin_unlock(&ip->pmp->inum_spin);
284 * Non zero transition
286 if (atomic_cmpset_int(&ip->refs, refs, refs - 1))
293 * Get the vnode associated with the given inode, allocating the vnode if
294 * necessary. The vnode will be returned exclusively locked.
296 * The caller must lock the inode (shared or exclusive).
298 * Great care must be taken to avoid deadlocks and vnode acquisition/reclaim
302 hammer2_igetv(hammer2_inode_t *ip, int *errorp)
304 hammer2_inode_data_t *ipdata;
305 hammer2_pfsmount_t *pmp;
310 KKASSERT(pmp != NULL);
312 ipdata = &ip->chain->data->ipdata;
316 * Attempt to reuse an existing vnode assignment. It is
317 * possible to race a reclaim so the vget() may fail. The
318 * inode must be unlocked during the vget() to avoid a
319 * deadlock against a reclaim.
324 * Inode must be unlocked during the vget() to avoid
325 * possible deadlocks, but leave the ip ref intact.
327 * vnode is held to prevent destruction during the
328 * vget(). The vget() can still fail if we lost
329 * a reclaim race on the vnode.
331 vhold_interlocked(vp);
332 ostate = hammer2_inode_lock_temp_release(ip);
333 if (vget(vp, LK_EXCLUSIVE)) {
335 hammer2_inode_lock_temp_restore(ip, ostate);
338 hammer2_inode_lock_temp_restore(ip, ostate);
340 /* vp still locked and ref from vget */
342 kprintf("hammer2: igetv race %p/%p\n",
352 * No vnode exists, allocate a new vnode. Beware of
353 * allocation races. This function will return an
354 * exclusively locked and referenced vnode.
356 *errorp = getnewvnode(VT_HAMMER2, pmp->mp, &vp, 0, 0);
358 kprintf("hammer2: igetv getnewvnode failed %d\n",
365 * Lock the inode and check for an allocation race.
367 ostate = hammer2_inode_lock_upgrade(ip);
368 if (ip->vp != NULL) {
371 hammer2_inode_lock_downgrade(ip, ostate);
375 switch (ipdata->type) {
376 case HAMMER2_OBJTYPE_DIRECTORY:
379 case HAMMER2_OBJTYPE_REGFILE:
381 vinitvmio(vp, ipdata->size,
383 (int)ipdata->size & HAMMER2_LBUFMASK);
385 case HAMMER2_OBJTYPE_SOFTLINK:
387 * XXX for now we are using the generic file_read
388 * and file_write code so we need a buffer cache
392 vinitvmio(vp, ipdata->size,
394 (int)ipdata->size & HAMMER2_LBUFMASK);
398 panic("hammer2: unhandled objtype %d", ipdata->type);
402 if (ip == pmp->iroot)
403 vsetflags(vp, VROOT);
407 hammer2_inode_ref(ip); /* vp association */
408 hammer2_inode_lock_downgrade(ip, ostate);
413 * Return non-NULL vp and *errorp == 0, or NULL vp and *errorp != 0.
415 if (hammer2_debug & 0x0002) {
416 kprintf("igetv vp %p refs %d aux %d\n",
417 vp, vp->v_sysref.refcnt, vp->v_auxrefs);
423 * The passed-in chain must be locked and the returned inode will also be
424 * locked. This routine typically locates or allocates the inode, assigns
425 * ip->chain (adding a ref to chain if necessary), and returns the inode.
427 * The hammer2_inode structure regulates the interface between the high level
428 * kernel VNOPS API and the filesystem backend (the chains).
430 * WARNING! This routine sucks up the chain's lock (makes it part of the
431 * inode lock from the point of view of the inode lock API),
432 * so callers need to be careful.
434 * WARNING! The mount code is allowed to pass dip == NULL for iroot and
435 * is allowed to pass pmp == NULL and dip == NULL for sroot.
438 hammer2_inode_get(hammer2_pfsmount_t *pmp, hammer2_inode_t *dip,
439 hammer2_chain_t *chain)
441 hammer2_inode_t *nip;
443 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_INODE);
446 * Interlocked lookup/ref of the inode. This code is only needed
447 * when looking up inodes with nlinks != 0 (TODO: optimize out
448 * otherwise and test for duplicates).
452 nip = hammer2_inode_lookup(pmp, chain->data->ipdata.inum);
455 ccms_thread_lock(&nip->topo_cst, CCMS_STATE_EXCLUSIVE);
456 if ((nip->flags & HAMMER2_INODE_ONRBTREE) == 0) { /* race */
457 ccms_thread_unlock(&nip->topo_cst);
458 hammer2_inode_drop(nip);
461 if (nip->chain != chain)
462 hammer2_inode_repoint(nip, NULL, chain);
465 * Consolidated nip/nip->chain is locked (chain locked
472 * We couldn't find the inode number, create a new inode.
475 nip = kmalloc(sizeof(*nip), pmp->minode, M_WAITOK | M_ZERO);
476 atomic_add_long(&pmp->inmem_inodes, 1);
477 hammer2_chain_memory_wakeup(pmp);
479 nip = kmalloc(sizeof(*nip), M_HAMMER2, M_WAITOK | M_ZERO);
480 nip->flags = HAMMER2_INODE_SROOT;
482 nip->inum = chain->data->ipdata.inum;
483 nip->size = chain->data->ipdata.size;
484 nip->mtime = chain->data->ipdata.mtime;
485 hammer2_inode_repoint(nip, NULL, chain);
486 nip->pip = dip; /* can be NULL */
488 hammer2_inode_ref(dip); /* ref dip for nip->pip */
493 * ref and lock on nip gives it state compatible to after a
494 * hammer2_inode_lock_ex() call.
497 ccms_cst_init(&nip->topo_cst, &nip->chain);
498 ccms_thread_lock(&nip->topo_cst, CCMS_STATE_EXCLUSIVE);
499 /* combination of thread lock and chain lock == inode lock */
502 * Attempt to add the inode. If it fails we raced another inode
503 * get. Undo all the work and try again.
506 spin_lock(&pmp->inum_spin);
507 if (RB_INSERT(hammer2_inode_tree, &pmp->inum_tree, nip)) {
508 spin_unlock(&pmp->inum_spin);
509 ccms_thread_unlock(&nip->topo_cst);
510 hammer2_inode_drop(nip);
513 atomic_set_int(&nip->flags, HAMMER2_INODE_ONRBTREE);
514 spin_unlock(&pmp->inum_spin);
521 * Create a new inode in the specified directory using the vattr to
522 * figure out the type of inode.
524 * If no error occurs the new inode with its chain locked is returned in
525 * *nipp, otherwise an error is returned and *nipp is set to NULL.
527 * If vap and/or cred are NULL the related fields are not set and the
528 * inode type defaults to a directory. This is used when creating PFSs
529 * under the super-root, so the inode number is set to 1 in this case.
531 * dip is not locked on entry.
534 hammer2_inode_create(hammer2_trans_t *trans, hammer2_inode_t *dip,
535 struct vattr *vap, struct ucred *cred,
536 const uint8_t *name, size_t name_len,
537 hammer2_chain_t **chainp, int *errorp)
539 hammer2_inode_data_t *dipdata;
540 hammer2_inode_data_t *nipdata;
541 hammer2_chain_t *chain;
542 hammer2_chain_t *parent;
543 hammer2_inode_t *nip;
544 hammer2_key_t key_dummy;
552 int cache_index = -1;
554 lhc = hammer2_dirhash(name, name_len);
558 * Locate the inode or indirect block to create the new
559 * entry in. At the same time check for key collisions
560 * and iterate until we don't get one.
562 * NOTE: hidden inodes do not have iterators.
565 parent = hammer2_inode_lock_ex(dip);
566 dipdata = &dip->chain->data->ipdata;
567 dip_uid = dipdata->uid;
568 dip_gid = dipdata->gid;
569 dip_mode = dipdata->mode;
570 dip_algo = dipdata->comp_algo;
574 chain = hammer2_chain_lookup(&parent, &key_dummy,
575 lhc, lhc, &cache_index, 0);
578 if ((lhc & HAMMER2_DIRHASH_VISIBLE) == 0)
580 if ((lhc & HAMMER2_DIRHASH_LOMASK) == HAMMER2_DIRHASH_LOMASK)
582 hammer2_chain_unlock(chain);
588 error = hammer2_chain_create(trans, &parent, &chain,
590 HAMMER2_BREF_TYPE_INODE,
591 HAMMER2_INODE_BYTES);
595 * Cleanup and handle retries.
597 if (error == EAGAIN) {
598 hammer2_chain_ref(parent);
599 hammer2_inode_unlock_ex(dip, parent);
600 hammer2_chain_wait(parent);
601 hammer2_chain_drop(parent);
604 hammer2_inode_unlock_ex(dip, parent);
607 KKASSERT(chain == NULL);
613 * Set up the new inode.
615 * NOTE: *_get() integrates chain's lock into the inode lock.
617 * NOTE: Only one new inode can currently be created per
618 * transaction. If the need arises we can adjust
619 * hammer2_trans_init() to allow more.
621 * NOTE: nipdata will have chain's blockset data.
623 chain->data->ipdata.inum = trans->sync_tid;
624 nip = hammer2_inode_get(dip->pmp, dip, chain);
625 nipdata = &chain->data->ipdata;
628 KKASSERT(trans->inodes_created == 0);
629 nipdata->type = hammer2_get_obj_type(vap->va_type);
630 nipdata->inum = trans->sync_tid;
631 ++trans->inodes_created;
633 nipdata->type = HAMMER2_OBJTYPE_DIRECTORY;
637 /* Inherit parent's inode compression mode. */
638 nip->comp_heuristic = 0;
639 nipdata->comp_algo = dip_algo;
640 nipdata->version = HAMMER2_INODE_VERSION_ONE;
641 hammer2_update_time(&nipdata->ctime);
642 nipdata->mtime = nipdata->ctime;
644 nipdata->mode = vap->va_mode;
648 xuid = hammer2_to_unix_xid(&dip_uid);
649 xuid = vop_helper_create_uid(dip->pmp->mp,
657 if (vap->va_vaflags & VA_UID_UUID_VALID)
658 nipdata->uid = vap->va_uid_uuid;
659 else if (vap->va_uid != (uid_t)VNOVAL)
660 hammer2_guid_to_uuid(&nipdata->uid, vap->va_uid);
662 hammer2_guid_to_uuid(&nipdata->uid, xuid);
664 if (vap->va_vaflags & VA_GID_UUID_VALID)
665 nipdata->gid = vap->va_gid_uuid;
666 else if (vap->va_gid != (gid_t)VNOVAL)
667 hammer2_guid_to_uuid(&nipdata->gid, vap->va_gid);
669 nipdata->gid = dip_gid;
673 * Regular files and softlinks allow a small amount of data to be
674 * directly embedded in the inode. This flag will be cleared if
675 * the size is extended past the embedded limit.
677 if (nipdata->type == HAMMER2_OBJTYPE_REGFILE ||
678 nipdata->type == HAMMER2_OBJTYPE_SOFTLINK) {
679 nipdata->op_flags |= HAMMER2_OPFLAG_DIRECTDATA;
682 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
683 bcopy(name, nipdata->filename, name_len);
684 nipdata->name_key = lhc;
685 nipdata->name_len = name_len;
692 * chain may have been moved around by the create.
696 hammer2_chain_refactor(hammer2_chain_t **chainp)
698 hammer2_chain_t *chain = *chainp;
699 hammer2_chain_core_t *core;
702 while (chain->flags & HAMMER2_CHAIN_DUPLICATED) {
703 spin_lock(&core->cst.spin);
704 chain = TAILQ_NEXT(chain, core_entry);
705 while (chain->flags & HAMMER2_CHAIN_DUPLICATED)
706 chain = TAILQ_NEXT(chain, core_entry);
707 hammer2_chain_ref(chain);
708 spin_unlock(&core->cst.spin);
709 KKASSERT(chain->core == core);
711 hammer2_chain_unlock(*chainp);
712 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS |
713 HAMMER2_RESOLVE_NOREF); /* eat ref */
719 * ochain represents the target file inode. We need to move it to the
720 * specified common parent directory (dip) and rename it to a special
721 * invisible "0xINODENUMBER" filename.
723 * We use chain_duplicate and duplicate ochain at the new location,
724 * renaming it appropriately. We create a temporary chain and
725 * then delete it to placemark where the duplicate will go. Both of
726 * these use the inode number for (lhc) (the key), generating the
727 * invisible filename.
731 hammer2_hardlink_shiftup(hammer2_trans_t *trans, hammer2_chain_t **ochainp,
732 hammer2_inode_t *dip, int *errorp)
734 hammer2_inode_data_t *nipdata;
735 hammer2_chain_t *parent;
736 hammer2_chain_t *ochain;
737 hammer2_chain_t *nchain;
738 hammer2_chain_t *tmp;
739 hammer2_key_t key_dummy;
741 hammer2_blockref_t bref;
742 int cache_index = -1;
746 lhc = ochain->data->ipdata.inum;
747 KKASSERT((lhc & HAMMER2_DIRHASH_VISIBLE) == 0);
750 * Locate the inode or indirect block to create the new
751 * entry in. lhc represents the inode number so there is
752 * no collision iteration.
754 * There should be no key collisions with invisible inode keys.
756 * WARNING! Must use inode_lock_ex() on dip to handle a stale
760 parent = hammer2_inode_lock_ex(dip);
761 /*parent = hammer2_chain_lookup_init(dip->chain, 0);*/
762 nchain = hammer2_chain_lookup(&parent, &key_dummy,
763 lhc, lhc, &cache_index, 0);
765 kprintf("X3 chain %p parent %p dip %p dip->chain %p\n",
766 nchain, parent, dip, dip->chain);
767 hammer2_chain_unlock(nchain);
776 * Create entry in common parent directory using the seek position
780 KKASSERT(nchain == NULL);
781 *errorp = hammer2_chain_create(trans, &parent, &nchain,
783 HAMMER2_BREF_TYPE_INODE,/* n/a */
784 HAMMER2_INODE_BYTES); /* n/a */
785 hammer2_chain_refactor(&ochain);
790 * Cleanup and handle retries.
792 if (*errorp == EAGAIN) {
793 hammer2_chain_ref(parent);
794 /* hammer2_chain_lookup_done(parent); */
795 hammer2_inode_unlock_ex(dip, parent);
796 hammer2_chain_wait(parent);
797 hammer2_chain_drop(parent);
802 * Handle the error case
805 KKASSERT(nchain == NULL);
806 hammer2_inode_unlock_ex(dip, parent);
807 /*hammer2_chain_lookup_done(parent);*/
812 * Use chain as a placeholder for (lhc), delete it and replace
813 * it with our duplication.
815 * Gain a second lock on ochain for the duplication function to
816 * unlock, maintain the caller's original lock across the call.
818 * This is a bit messy.
820 hammer2_chain_delete(trans, nchain, HAMMER2_DELETE_WILLDUP);
821 hammer2_chain_lock(ochain, HAMMER2_RESOLVE_ALWAYS);
824 bref.key = lhc; /* invisible dir entry key */
826 hammer2_chain_duplicate(trans, parent, &tmp, &bref);
827 hammer2_inode_unlock_ex(dip, parent);
828 /*hammer2_chain_lookup_done(parent);*/
829 hammer2_chain_unlock(nchain); /* no longer needed */
832 * Now set chain to our duplicate and modify it appropriately.
834 * Directory entries are inodes but this is a hidden hardlink
835 * target. The name isn't used but to ease debugging give it
836 * a name after its inode number.
839 tmp = NULL; /* safety */
841 hammer2_chain_modify(trans, &nchain, HAMMER2_MODIFY_ASSERTNOCOPY);
842 nipdata = &nchain->data->ipdata;
843 ksnprintf(nipdata->filename, sizeof(nipdata->filename),
844 "0x%016jx", (intmax_t)nipdata->inum);
845 nipdata->name_len = strlen(nipdata->filename);
846 nipdata->name_key = lhc;
852 * Connect the target inode represented by (*chainp) to the media topology
853 * at (dip, name, len).
855 * If hlink is TRUE this function creates an OBJTYPE_HARDLINK directory
856 * entry instead of connecting (*chainp).
858 * If hlink is FALSE this function uses chain_duplicate() to make a copy
859 * if (*chainp) in the directory entry. (*chainp) is likely to be deleted
860 * by the caller in this case (e.g. rename).
863 hammer2_inode_connect(hammer2_trans_t *trans, int hlink,
864 hammer2_inode_t *dip, hammer2_chain_t **chainp,
865 const uint8_t *name, size_t name_len)
867 hammer2_inode_data_t *ipdata;
868 hammer2_chain_t *nchain;
869 hammer2_chain_t *parent;
870 hammer2_chain_t *ochain;
871 hammer2_key_t key_dummy;
873 int cache_index = -1;
879 * Since ochain is either disconnected from the topology or represents
880 * a hardlink terminus which is always a parent of or equal to dip,
881 * we should be able to safely lock dip->chain for our setup.
883 * WARNING! Must use inode_lock_ex() on dip to handle a stale
886 parent = hammer2_inode_lock_ex(dip);
887 /*parent = hammer2_chain_lookup_init(dip->chain, 0);*/
889 lhc = hammer2_dirhash(name, name_len);
892 * Locate the inode or indirect block to create the new
893 * entry in. At the same time check for key collisions
894 * and iterate until we don't get one.
898 nchain = hammer2_chain_lookup(&parent, &key_dummy,
899 lhc, lhc, &cache_index, 0);
902 if ((lhc & HAMMER2_DIRHASH_LOMASK) == HAMMER2_DIRHASH_LOMASK)
904 hammer2_chain_unlock(nchain);
912 * Hardlink pointer needed, create totally fresh
915 KKASSERT(nchain == NULL);
916 error = hammer2_chain_create(trans, &parent, &nchain,
918 HAMMER2_BREF_TYPE_INODE,
919 HAMMER2_INODE_BYTES);
920 hammer2_chain_refactor(&ochain);
923 * Reconnect the original chain and rename. Use
924 * chain_duplicate(). The caller will likely delete
925 * or has already deleted the original chain in
928 * NOTE: chain_duplicate() generates a new chain
929 * with CHAIN_DELETED cleared (ochain typically
930 * has it set from the file unlink).
934 hammer2_chain_duplicate(trans, NULL, &nchain, NULL);
935 error = hammer2_chain_create(trans, &parent, &nchain,
937 HAMMER2_BREF_TYPE_INODE,
938 HAMMER2_INODE_BYTES);
945 KKASSERT(error != EAGAIN);
946 hammer2_inode_unlock_ex(dip, parent);
947 /*hammer2_chain_lookup_done(parent);*/
951 * nchain should be NULL on error, leave ochain (== *chainp) alone.
954 KKASSERT(nchain == NULL);
959 * Directory entries are inodes so if the name has changed we have
960 * to update the inode.
962 * When creating an OBJTYPE_HARDLINK entry remember to unlock the
963 * chain, the caller will access the hardlink via the actual hardlink
964 * target file and not the hardlink pointer entry, so we must still
967 if (hlink && hammer2_hardlink_enable >= 0) {
969 * Create the HARDLINK pointer. oip represents the hardlink
970 * target in this situation.
972 * We will return ochain (the hardlink target).
974 hammer2_chain_modify(trans, &nchain,
975 HAMMER2_MODIFY_ASSERTNOCOPY);
976 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
977 ipdata = &nchain->data->ipdata;
978 bcopy(name, ipdata->filename, name_len);
979 ipdata->name_key = lhc;
980 ipdata->name_len = name_len;
981 ipdata->target_type = ochain->data->ipdata.type;
982 ipdata->type = HAMMER2_OBJTYPE_HARDLINK;
983 ipdata->inum = ochain->data->ipdata.inum;
985 hammer2_chain_unlock(nchain);
988 } else if (hlink && hammer2_hardlink_enable < 0) {
990 * Create a snapshot (hardlink fake mode for debugging).
991 * (ochain already flushed above so we can just copy the
994 * Since this is a snapshot we return nchain in the fake
997 hammer2_chain_modify(trans, &nchain,
998 HAMMER2_MODIFY_ASSERTNOCOPY);
999 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
1000 ipdata = &nchain->data->ipdata;
1001 *ipdata = ochain->data->ipdata;
1002 bcopy(name, ipdata->filename, name_len);
1003 ipdata->name_key = lhc;
1004 ipdata->name_len = name_len;
1005 kprintf("created fake hardlink %*.*s\n",
1006 (int)name_len, (int)name_len, name);
1009 * nchain is a duplicate of ochain at the new location.
1010 * We must fixup the name stored in oip. The bref key
1011 * has already been set up.
1013 hammer2_chain_modify(trans, &nchain,
1014 HAMMER2_MODIFY_ASSERTNOCOPY);
1015 ipdata = &nchain->data->ipdata;
1017 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
1018 bcopy(name, ipdata->filename, name_len);
1019 ipdata->name_key = lhc;
1020 ipdata->name_len = name_len;
1025 * We are replacing ochain with nchain, unlock ochain. In the
1026 * case where ochain is left unchanged the code above sets
1027 * nchain to ochain and ochain to NULL, resulting in a NOP here.
1030 hammer2_chain_unlock(ochain);
1037 * Repoint ip->chain to nchain. Caller must hold the inode exclusively
1040 * ip->chain is set to nchain. The prior chain in ip->chain is dropped
1041 * and nchain is ref'd.
1044 hammer2_inode_repoint(hammer2_inode_t *ip, hammer2_inode_t *pip,
1045 hammer2_chain_t *nchain)
1047 hammer2_chain_t *ochain;
1048 hammer2_inode_t *opip;
1051 * Repoint ip->chain if requested.
1056 hammer2_chain_ref(nchain);
1058 hammer2_chain_drop(ochain);
1061 * Repoint ip->pip if requested (non-NULL pip).
1063 if (pip && ip->pip != pip) {
1065 hammer2_inode_ref(pip);
1068 hammer2_inode_drop(opip);
1073 * Unlink the file from the specified directory inode. The directory inode
1074 * does not need to be locked.
1076 * isdir determines whether a directory/non-directory check should be made.
1077 * No check is made if isdir is set to -1.
1079 * NOTE! The underlying file can still be active with open descriptors
1080 * or if the chain is being manually held (e.g. for rename).
1082 * The caller is responsible for fixing up ip->chain if e.g. a
1083 * rename occurs (see chain_duplicate()).
1086 hammer2_unlink_file(hammer2_trans_t *trans, hammer2_inode_t *dip,
1087 const uint8_t *name, size_t name_len,
1088 int isdir, int *hlinkp)
1090 hammer2_inode_data_t *ipdata;
1091 hammer2_chain_t *parent;
1092 hammer2_chain_t *ochain;
1093 hammer2_chain_t *chain;
1094 hammer2_chain_t *dparent;
1095 hammer2_chain_t *dchain;
1096 hammer2_key_t key_dummy;
1097 hammer2_key_t key_next;
1100 int cache_index = -1;
1105 lhc = hammer2_dirhash(name, name_len);
1108 * Search for the filename in the directory
1112 parent = hammer2_inode_lock_ex(dip);
1113 chain = hammer2_chain_lookup(&parent, &key_next,
1114 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1117 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
1118 name_len == chain->data->ipdata.name_len &&
1119 bcmp(name, chain->data->ipdata.filename, name_len) == 0) {
1122 chain = hammer2_chain_next(&parent, chain, &key_next,
1124 lhc + HAMMER2_DIRHASH_LOMASK,
1127 hammer2_inode_unlock_ex(dip, NULL); /* retain parent */
1130 * Not found or wrong type (isdir < 0 disables the type check).
1131 * If a hardlink pointer, type checks use the hardlink target.
1133 if (chain == NULL) {
1137 if ((type = chain->data->ipdata.type) == HAMMER2_OBJTYPE_HARDLINK) {
1140 type = chain->data->ipdata.target_type;
1143 if (type == HAMMER2_OBJTYPE_DIRECTORY && isdir == 0) {
1147 if (type != HAMMER2_OBJTYPE_DIRECTORY && isdir >= 1) {
1153 * Hardlink must be resolved. We can't hold parent locked while we
1154 * do this or we could deadlock.
1156 * On success chain will be adjusted to point at the hardlink target
1157 * and ochain will point to the hardlink pointer in the original
1158 * directory. Otherwise chain remains pointing to the original.
1160 if (chain->data->ipdata.type == HAMMER2_OBJTYPE_HARDLINK) {
1161 hammer2_chain_unlock(parent);
1163 error = hammer2_hardlink_find(dip, &chain, &ochain);
1167 * If this is a directory the directory must be empty. However, if
1168 * isdir < 0 we are doing a rename and the directory does not have
1169 * to be empty, and if isdir > 1 we are deleting a PFS/snapshot
1170 * and the directory does not have to be empty.
1172 * NOTE: We check the full key range here which covers both visible
1173 * and invisible entries. Theoretically there should be no
1174 * invisible (hardlink target) entries if there are no visible
1177 if (type == HAMMER2_OBJTYPE_DIRECTORY && isdir == 1) {
1178 dparent = hammer2_chain_lookup_init(chain, 0);
1179 dchain = hammer2_chain_lookup(&dparent, &key_dummy,
1180 0, (hammer2_key_t)-1,
1182 HAMMER2_LOOKUP_NODATA);
1184 hammer2_chain_unlock(dchain);
1185 hammer2_chain_lookup_done(dparent);
1189 hammer2_chain_lookup_done(dparent);
1195 * Ok, we can now unlink the chain. We always decrement nlinks even
1196 * if the entry can be deleted in case someone has the file open and
1199 * The chain itself will no longer be in the on-media topology but
1200 * can still be flushed to the media (e.g. if an open descriptor
1201 * remains). When the last vnode/ip ref goes away the chain will
1202 * be marked unmodified, avoiding any further (now unnecesary) I/O.
1204 * A non-NULL ochain indicates a hardlink.
1208 * Delete the original hardlink pointer.
1210 * NOTE: parent from above is NULL when ochain != NULL
1211 * so we can reuse it.
1213 hammer2_chain_lock(ochain, HAMMER2_RESOLVE_ALWAYS);
1214 hammer2_chain_delete(trans, ochain, 0);
1215 hammer2_chain_unlock(ochain);
1218 * Then decrement nlinks on hardlink target, deleting
1219 * the target when nlinks drops to 0.
1221 hammer2_chain_modify(trans, &chain, 0);
1222 --chain->data->ipdata.nlinks;
1223 if (chain->data->ipdata.nlinks == 0)
1224 hammer2_chain_delete(trans, chain, 0);
1227 * Otherwise this was not a hardlink and we can just
1228 * remove the entry and decrement nlinks.
1230 * NOTE: *_get() integrates chain's lock into the inode lock.
1232 hammer2_chain_modify(trans, &chain, 0);
1233 ipdata = &chain->data->ipdata;
1235 hammer2_chain_delete(trans, chain, 0);
1241 hammer2_chain_unlock(chain);
1243 hammer2_chain_lookup_done(parent);
1245 hammer2_chain_drop(ochain);
1251 * Given an exclusively locked inode we consolidate its chain for hardlink
1252 * creation, adding (nlinks) to the file's link count and potentially
1253 * relocating the inode to a directory common to ip->pip and tdip.
1255 * Replaces (*chainp) if consolidation occurred, unlocking the old chain
1256 * and returning a new locked chain.
1258 * NOTE! This function will also replace ip->chain.
1261 hammer2_hardlink_consolidate(hammer2_trans_t *trans, hammer2_inode_t *ip,
1262 hammer2_chain_t **chainp,
1263 hammer2_inode_t *tdip, int nlinks)
1265 hammer2_inode_data_t *ipdata;
1266 hammer2_inode_t *fdip;
1267 hammer2_inode_t *cdip;
1268 hammer2_chain_t *chain;
1269 hammer2_chain_t *nchain;
1273 if (nlinks == 0 && /* no hardlink needed */
1274 (chain->data->ipdata.name_key & HAMMER2_DIRHASH_VISIBLE)) {
1277 if (hammer2_hardlink_enable < 0) { /* fake hardlinks */
1281 if (hammer2_hardlink_enable == 0) { /* disallow hardlinks */
1282 hammer2_chain_unlock(chain);
1288 * cdip will be returned with a ref, but not locked.
1291 cdip = hammer2_inode_common_parent(fdip, tdip);
1294 * If no change in the hardlink's target directory is required and
1295 * this is already a hardlink target, all we need to do is adjust
1298 * XXX The common parent is a big wiggly due to duplication from
1299 * renames. Compare the core (RBTREE) pointer instead of the
1303 (chain->data->ipdata.name_key & HAMMER2_DIRHASH_VISIBLE) == 0) {
1305 hammer2_chain_modify(trans, &chain, 0);
1306 chain->data->ipdata.nlinks += nlinks;
1313 * We either have to move an existing hardlink target or we have
1314 * to create a fresh hardlink target.
1316 * Hardlink targets are hidden inodes in a parent directory common
1317 * to all directory entries referencing the hardlink.
1319 nchain = hammer2_hardlink_shiftup(trans, &chain, cdip, &error);
1323 * Bump nlinks on duplicated hidden inode, repoint
1326 hammer2_chain_modify(trans, &nchain, 0);
1327 nchain->data->ipdata.nlinks += nlinks;
1328 hammer2_inode_repoint(ip, cdip, nchain);
1331 * If the old chain is not a hardlink target then replace
1332 * it with a OBJTYPE_HARDLINK pointer.
1334 * If the old chain IS a hardlink target then delete it.
1336 if (chain->data->ipdata.name_key & HAMMER2_DIRHASH_VISIBLE) {
1338 * Replace original non-hardlink that's been dup'd
1339 * with a special hardlink directory entry. We must
1340 * set the DIRECTDATA flag to prevent sub-chains
1341 * from trying to synchronize to the inode if the
1342 * file is extended afterwords.
1344 * DELDUP_RECORE causes the new chain to NOT inherit
1345 * the old chain's core (sub-tree).
1347 /*hammer2_chain_modify(trans, &chain, 0);*/
1348 hammer2_chain_delete_duplicate(trans, &chain,
1349 HAMMER2_DELDUP_RECORE);
1350 hammer2_chain_modify(trans, &chain, 0);
1351 ipdata = &chain->data->ipdata;
1352 ipdata->target_type = ipdata->type;
1353 ipdata->type = HAMMER2_OBJTYPE_HARDLINK;
1361 bzero(&ipdata->uid, sizeof(ipdata->uid));
1362 bzero(&ipdata->gid, sizeof(ipdata->gid));
1363 ipdata->op_flags = HAMMER2_OPFLAG_DIRECTDATA;
1364 ipdata->cap_flags = 0;
1368 ipdata->iparent = 0; /* XXX */
1369 ipdata->pfs_type = 0;
1370 ipdata->pfs_inum = 0;
1371 bzero(&ipdata->pfs_clid, sizeof(ipdata->pfs_clid));
1372 bzero(&ipdata->pfs_fsid, sizeof(ipdata->pfs_fsid));
1373 ipdata->data_quota = 0;
1374 ipdata->data_count = 0;
1375 ipdata->inode_quota = 0;
1376 ipdata->inode_count = 0;
1377 ipdata->attr_tid = 0;
1378 ipdata->dirent_tid = 0;
1379 bzero(&ipdata->u, sizeof(ipdata->u));
1380 /* XXX transaction ids */
1382 hammer2_chain_delete(trans, chain, 0);
1386 * Return the new chain.
1388 hammer2_chain_unlock(chain);
1394 hammer2_chain_unlock(chain);
1399 * Cleanup, chain/nchain already dealt with.
1403 hammer2_inode_drop(cdip);
1409 * If (*ochainp) is non-NULL it points to the forward OBJTYPE_HARDLINK
1410 * inode while (*chainp) points to the resolved (hidden hardlink
1411 * target) inode. In this situation when nlinks is 1 we wish to
1412 * deconsolidate the hardlink, moving it back to the directory that now
1413 * represents the only remaining link.
1416 hammer2_hardlink_deconsolidate(hammer2_trans_t *trans,
1417 hammer2_inode_t *dip,
1418 hammer2_chain_t **chainp,
1419 hammer2_chain_t **ochainp)
1421 if (*ochainp == NULL)
1428 * The caller presents a locked *chainp pointing to a HAMMER2_BREF_TYPE_INODE
1429 * with an obj_type of HAMMER2_OBJTYPE_HARDLINK. This routine will gobble
1430 * the *chainp and return a new locked *chainp representing the file target
1431 * (the original *chainp will be unlocked).
1433 * When a match is found the chain representing the original HARDLINK
1434 * will be returned in *ochainp with a ref, but not locked.
1436 * When no match is found *chainp is set to NULL and EIO is returned.
1437 * (*ochainp) will still be set to the original chain with a ref but not
1441 hammer2_hardlink_find(hammer2_inode_t *dip, hammer2_chain_t **chainp,
1442 hammer2_chain_t **ochainp)
1444 hammer2_chain_t *chain = *chainp;
1445 hammer2_chain_t *parent;
1446 hammer2_inode_t *ip;
1447 hammer2_inode_t *pip;
1448 hammer2_key_t key_dummy;
1450 int cache_index = -1;
1453 hammer2_inode_ref(pip); /* for loop */
1454 hammer2_chain_ref(chain); /* for (*ochainp) */
1458 * Locate the hardlink. pip is referenced and not locked,
1463 lhc = chain->data->ipdata.inum;
1464 hammer2_chain_unlock(chain);
1467 while ((ip = pip) != NULL) {
1468 parent = hammer2_inode_lock_ex(ip);
1469 hammer2_inode_drop(ip); /* loop */
1470 KKASSERT(parent->bref.type == HAMMER2_BREF_TYPE_INODE);
1471 chain = hammer2_chain_lookup(&parent, &key_dummy,
1472 lhc, lhc, &cache_index, 0);
1473 hammer2_chain_lookup_done(parent); /* discard parent */
1476 pip = ip->pip; /* safe, ip held locked */
1478 hammer2_inode_ref(pip); /* loop */
1479 hammer2_inode_unlock_ex(ip, NULL);
1483 * chain is locked, ip is locked. Unlock ip, return the locked
1484 * chain. *ipp is already set w/a ref count and not locked.
1486 * (parent is already unlocked).
1489 hammer2_inode_unlock_ex(ip, NULL);
1492 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_INODE);
1493 /* already locked */
1501 * Find the directory common to both fdip and tdip, hold and return
1505 hammer2_inode_common_parent(hammer2_inode_t *fdip, hammer2_inode_t *tdip)
1507 hammer2_inode_t *scan1;
1508 hammer2_inode_t *scan2;
1511 * We used to have a depth field but it complicated matters too
1512 * much for directory renames. So now its ugly. Check for
1513 * simple cases before giving up and doing it the expensive way.
1515 * XXX need a bottom-up topology stability lock
1517 if (fdip == tdip || fdip == tdip->pip) {
1518 hammer2_inode_ref(fdip);
1521 if (fdip->pip == tdip) {
1522 hammer2_inode_ref(tdip);
1529 for (scan1 = fdip; scan1->pmp == fdip->pmp; scan1 = scan1->pip) {
1531 while (scan2->pmp == tdip->pmp) {
1532 if (scan1 == scan2) {
1533 hammer2_inode_ref(scan1);
1541 panic("hammer2_inode_common_parent: no common parent %p %p\n",
1548 * Synchronize the inode's frontend state with the chain state prior
1549 * to any explicit flush of the inode or any strategy write call.
1551 * Called with a locked inode.
1554 hammer2_inode_fsync(hammer2_trans_t *trans, hammer2_inode_t *ip,
1555 hammer2_chain_t **chainp)
1557 hammer2_inode_data_t *ipdata;
1558 hammer2_chain_t *parent;
1559 hammer2_chain_t *chain;
1560 hammer2_key_t lbase;
1561 hammer2_key_t key_next;
1564 ipdata = &ip->chain->data->ipdata;
1566 if (ip->flags & HAMMER2_INODE_MTIME) {
1567 ipdata = hammer2_chain_modify_ip(trans, ip, chainp, 0);
1568 atomic_clear_int(&ip->flags, HAMMER2_INODE_MTIME);
1569 ipdata->mtime = ip->mtime;
1571 if ((ip->flags & HAMMER2_INODE_RESIZED) && ip->size < ipdata->size) {
1572 ipdata = hammer2_chain_modify_ip(trans, ip, chainp, 0);
1573 ipdata->size = ip->size;
1574 atomic_clear_int(&ip->flags, HAMMER2_INODE_RESIZED);
1577 * We must delete any chains beyond the EOF. The chain
1578 * straddling the EOF will be pending in the bioq.
1580 lbase = (ipdata->size + HAMMER2_PBUFMASK64) &
1581 ~HAMMER2_PBUFMASK64;
1582 parent = hammer2_chain_lookup_init(ip->chain, 0);
1583 chain = hammer2_chain_lookup(&parent, &key_next,
1584 lbase, (hammer2_key_t)-1,
1586 HAMMER2_LOOKUP_NODATA);
1589 * Degenerate embedded case, nothing to loop on
1591 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
1592 hammer2_chain_unlock(chain);
1595 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
1596 hammer2_chain_delete(trans, chain, 0);
1598 chain = hammer2_chain_next(&parent, chain, &key_next,
1599 key_next, (hammer2_key_t)-1,
1601 HAMMER2_LOOKUP_NODATA);
1603 hammer2_chain_lookup_done(parent);
1605 if ((ip->flags & HAMMER2_INODE_RESIZED) && ip->size > ipdata->size) {
1606 ipdata = hammer2_chain_modify_ip(trans, ip, chainp, 0);
1607 ipdata->size = ip->size;
1608 atomic_clear_int(&ip->flags, HAMMER2_INODE_RESIZED);
1611 * When resizing larger we may not have any direct-data
1614 if ((ipdata->op_flags & HAMMER2_OPFLAG_DIRECTDATA) &&
1615 ip->size > HAMMER2_EMBEDDED_BYTES) {
1616 ipdata->op_flags &= ~HAMMER2_OPFLAG_DIRECTDATA;
1617 bzero(&ipdata->u.blockset, sizeof(ipdata->u.blockset));