2 * Copyright (c) 2011-2014 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>
46 RB_GENERATE2(hammer2_inode_tree, hammer2_inode, rbnode, hammer2_inode_cmp,
47 hammer2_tid_t, meta.inum);
50 hammer2_inode_cmp(hammer2_inode_t *ip1, hammer2_inode_t *ip2)
52 if (ip1->meta.inum < ip2->meta.inum)
54 if (ip1->meta.inum > ip2->meta.inum)
62 * HAMMER2 offers shared and exclusive locks on inodes. Pass a mask of
65 * - pass HAMMER2_RESOLVE_SHARED if a shared lock is desired. The
66 * inode locking function will automatically set the RDONLY flag.
68 * - pass HAMMER2_RESOLVE_ALWAYS if you need the inode's meta-data.
69 * Most front-end inode locks do.
71 * - pass HAMMER2_RESOLVE_NEVER if you do not want to require that
72 * the inode data be resolved. This is used by the syncthr because
73 * it can run on an unresolved/out-of-sync cluster, and also by the
74 * vnode reclamation code to avoid unnecessary I/O (particularly when
75 * disposing of hundreds of thousands of cached vnodes).
77 * The inode locking function locks the inode itself, resolves any stale
78 * chains in the inode's cluster, and allocates a fresh copy of the
79 * cluster with 1 ref and all the underlying chains locked.
81 * ip->cluster will be stable while the inode is locked.
83 * NOTE: We don't combine the inode/chain lock because putting away an
84 * inode would otherwise confuse multiple lock holders of the inode.
86 * NOTE: In-memory inodes always point to hardlink targets (the actual file),
87 * and never point to a hardlink pointer.
89 * NOTE: If caller passes HAMMER2_RESOLVE_RDONLY the exclusive locking code
90 * will feel free to reduce the chain set in the cluster as an
91 * optimization. It will still be validated against the quorum if
92 * appropriate, but the optimization might be able to reduce data
93 * accesses to one node. This flag is automatically set if the inode
94 * is locked with HAMMER2_RESOLVE_SHARED.
97 hammer2_inode_lock(hammer2_inode_t *ip, int how)
99 hammer2_inode_ref(ip);
102 * Inode structure mutex
104 if (how & HAMMER2_RESOLVE_SHARED) {
105 /*how |= HAMMER2_RESOLVE_RDONLY; not used */
106 hammer2_mtx_sh(&ip->lock);
108 hammer2_mtx_ex(&ip->lock);
113 * Select a chain out of an inode's cluster and lock it.
115 * The inode does not have to be locked.
118 hammer2_inode_chain(hammer2_inode_t *ip, int clindex, int how)
120 hammer2_chain_t *chain;
122 hammer2_spin_sh(&ip->cluster_spin);
123 if (clindex >= ip->cluster.nchains)
126 chain = ip->cluster.array[clindex].chain;
128 hammer2_chain_ref(chain);
129 hammer2_spin_unsh(&ip->cluster_spin);
130 hammer2_chain_lock(chain, how);
132 hammer2_spin_unsh(&ip->cluster_spin);
138 hammer2_inode_chain_and_parent(hammer2_inode_t *ip, int clindex,
139 hammer2_chain_t **parentp, int how)
141 hammer2_chain_t *chain;
142 hammer2_chain_t *parent;
145 hammer2_spin_sh(&ip->cluster_spin);
146 if (clindex >= ip->cluster.nchains)
149 chain = ip->cluster.array[clindex].chain;
151 hammer2_chain_ref(chain);
152 hammer2_spin_unsh(&ip->cluster_spin);
153 hammer2_chain_lock(chain, how);
155 hammer2_spin_unsh(&ip->cluster_spin);
159 * Get parent, lock order must be (parent, chain).
161 parent = chain->parent;
162 hammer2_chain_ref(parent);
163 hammer2_chain_unlock(chain);
164 hammer2_chain_lock(parent, how);
165 hammer2_chain_lock(chain, how);
166 if (ip->cluster.array[clindex].chain == chain &&
167 chain->parent == parent) {
174 hammer2_chain_unlock(chain);
175 hammer2_chain_drop(chain);
176 hammer2_chain_unlock(parent);
177 hammer2_chain_drop(parent);
185 hammer2_inode_unlock(hammer2_inode_t *ip)
187 hammer2_mtx_unlock(&ip->lock);
188 hammer2_inode_drop(ip);
192 * Temporarily release a lock held shared or exclusive. Caller must
193 * hold the lock shared or exclusive on call and lock will be released
196 * Restore a lock that was temporarily released.
199 hammer2_inode_lock_temp_release(hammer2_inode_t *ip)
201 return hammer2_mtx_temp_release(&ip->lock);
205 hammer2_inode_lock_temp_restore(hammer2_inode_t *ip, hammer2_mtx_state_t ostate)
207 hammer2_mtx_temp_restore(&ip->lock, ostate);
211 * Upgrade a shared inode lock to exclusive and return. If the inode lock
212 * is already held exclusively this is a NOP.
214 * The caller MUST hold the inode lock either shared or exclusive on call
215 * and will own the lock exclusively on return.
217 * Returns non-zero if the lock was already exclusive prior to the upgrade.
220 hammer2_inode_lock_upgrade(hammer2_inode_t *ip)
224 if (mtx_islocked_ex(&ip->lock)) {
227 hammer2_mtx_unlock(&ip->lock);
228 hammer2_mtx_ex(&ip->lock);
235 * Downgrade an inode lock from exclusive to shared only if the inode
236 * lock was previously shared. If the inode lock was previously exclusive,
240 hammer2_inode_lock_downgrade(hammer2_inode_t *ip, int wasexclusive)
242 if (wasexclusive == 0)
243 mtx_downgrade(&ip->lock);
247 * Lookup an inode by inode number
250 hammer2_inode_lookup(hammer2_pfs_t *pmp, hammer2_tid_t inum)
258 hammer2_spin_ex(&pmp->inum_spin);
259 ip = RB_LOOKUP(hammer2_inode_tree, &pmp->inum_tree, inum);
261 hammer2_inode_ref(ip);
262 hammer2_spin_unex(&pmp->inum_spin);
268 * Adding a ref to an inode is only legal if the inode already has at least
271 * (can be called with spinlock held)
274 hammer2_inode_ref(hammer2_inode_t *ip)
276 atomic_add_int(&ip->refs, 1);
280 * Drop an inode reference, freeing the inode when the last reference goes
284 hammer2_inode_drop(hammer2_inode_t *ip)
287 hammer2_inode_t *pip;
295 * Transition to zero, must interlock with
296 * the inode inumber lookup tree (if applicable).
297 * It should not be possible for anyone to race
298 * the transition to 0.
302 hammer2_spin_ex(&pmp->inum_spin);
304 if (atomic_cmpset_int(&ip->refs, 1, 0)) {
305 KKASSERT(hammer2_mtx_refs(&ip->lock) == 0);
306 if (ip->flags & HAMMER2_INODE_ONRBTREE) {
307 atomic_clear_int(&ip->flags,
308 HAMMER2_INODE_ONRBTREE);
309 RB_REMOVE(hammer2_inode_tree,
310 &pmp->inum_tree, ip);
312 hammer2_spin_unex(&pmp->inum_spin);
319 * Cleaning out ip->cluster isn't entirely
322 hammer2_inode_repoint(ip, NULL, NULL);
325 * We have to drop pip (if non-NULL) to
326 * dispose of our implied reference from
327 * ip->pip. We can simply loop on it.
329 kfree(ip, pmp->minode);
330 atomic_add_long(&pmp->inmem_inodes, -1);
332 /* continue with pip (can be NULL) */
334 hammer2_spin_unex(&ip->pmp->inum_spin);
338 * Non zero transition
340 if (atomic_cmpset_int(&ip->refs, refs, refs - 1))
347 * Get the vnode associated with the given inode, allocating the vnode if
348 * necessary. The vnode will be returned exclusively locked.
350 * The caller must lock the inode (shared or exclusive).
352 * Great care must be taken to avoid deadlocks and vnode acquisition/reclaim
356 hammer2_igetv(hammer2_inode_t *ip, int *errorp)
362 KKASSERT(pmp != NULL);
367 * Attempt to reuse an existing vnode assignment. It is
368 * possible to race a reclaim so the vget() may fail. The
369 * inode must be unlocked during the vget() to avoid a
370 * deadlock against a reclaim.
377 * Inode must be unlocked during the vget() to avoid
378 * possible deadlocks, but leave the ip ref intact.
380 * vnode is held to prevent destruction during the
381 * vget(). The vget() can still fail if we lost
382 * a reclaim race on the vnode.
384 hammer2_mtx_state_t ostate;
387 ostate = hammer2_inode_lock_temp_release(ip);
388 if (vget(vp, LK_EXCLUSIVE)) {
390 hammer2_inode_lock_temp_restore(ip, ostate);
393 hammer2_inode_lock_temp_restore(ip, ostate);
395 /* vp still locked and ref from vget */
397 kprintf("hammer2: igetv race %p/%p\n",
407 * No vnode exists, allocate a new vnode. Beware of
408 * allocation races. This function will return an
409 * exclusively locked and referenced vnode.
411 *errorp = getnewvnode(VT_HAMMER2, pmp->mp, &vp, 0, 0);
413 kprintf("hammer2: igetv getnewvnode failed %d\n",
420 * Lock the inode and check for an allocation race.
422 wasexclusive = hammer2_inode_lock_upgrade(ip);
423 if (ip->vp != NULL) {
426 hammer2_inode_lock_downgrade(ip, wasexclusive);
430 switch (ip->meta.type) {
431 case HAMMER2_OBJTYPE_DIRECTORY:
434 case HAMMER2_OBJTYPE_REGFILE:
436 vinitvmio(vp, ip->meta.size,
438 (int)ip->meta.size & HAMMER2_LBUFMASK);
440 case HAMMER2_OBJTYPE_SOFTLINK:
442 * XXX for now we are using the generic file_read
443 * and file_write code so we need a buffer cache
447 vinitvmio(vp, ip->meta.size,
449 (int)ip->meta.size & HAMMER2_LBUFMASK);
451 case HAMMER2_OBJTYPE_CDEV:
454 case HAMMER2_OBJTYPE_BDEV:
455 vp->v_ops = &pmp->mp->mnt_vn_spec_ops;
456 if (ip->meta.type != HAMMER2_OBJTYPE_CDEV)
462 case HAMMER2_OBJTYPE_FIFO:
464 vp->v_ops = &pmp->mp->mnt_vn_fifo_ops;
466 case HAMMER2_OBJTYPE_SOCKET:
470 panic("hammer2: unhandled objtype %d",
475 if (ip == pmp->iroot)
476 vsetflags(vp, VROOT);
480 hammer2_inode_ref(ip); /* vp association */
481 hammer2_inode_lock_downgrade(ip, wasexclusive);
486 * Return non-NULL vp and *errorp == 0, or NULL vp and *errorp != 0.
488 if (hammer2_debug & 0x0002) {
489 kprintf("igetv vp %p refs 0x%08x aux 0x%08x\n",
490 vp, vp->v_refcnt, vp->v_auxrefs);
496 * Returns the inode associated with the passed-in cluster, creating the
497 * inode if necessary and synchronizing it to the passed-in cluster otherwise.
498 * When synchronizing, if idx >= 0, only cluster index (idx) is synchronized.
499 * Otherwise the whole cluster is synchronized.
501 * The passed-in cluster must be locked and will remain locked on return.
502 * The returned inode will be locked and the caller may dispose of both
503 * via hammer2_inode_unlock() + hammer2_inode_drop(). However, if the caller
504 * needs to resolve a hardlink it must ref/unlock/relock/drop the inode.
506 * The hammer2_inode structure regulates the interface between the high level
507 * kernel VNOPS API and the filesystem backend (the chains).
509 * On return the inode is locked with the supplied cluster.
512 hammer2_inode_get(hammer2_pfs_t *pmp, hammer2_inode_t *dip,
513 hammer2_cluster_t *cluster, int idx)
515 hammer2_inode_t *nip;
516 const hammer2_inode_data_t *iptmp;
517 const hammer2_inode_data_t *nipdata;
519 KKASSERT(cluster == NULL ||
520 hammer2_cluster_type(cluster) == HAMMER2_BREF_TYPE_INODE);
524 * Interlocked lookup/ref of the inode. This code is only needed
525 * when looking up inodes with nlinks != 0 (TODO: optimize out
526 * otherwise and test for duplicates).
528 * Cluster can be NULL during the initial pfs allocation.
532 iptmp = &hammer2_cluster_rdata(cluster)->ipdata;
533 nip = hammer2_inode_lookup(pmp, iptmp->meta.inum);
537 hammer2_mtx_ex(&nip->lock);
540 * Handle SMP race (not applicable to the super-root spmp
541 * which can't index inodes due to duplicative inode numbers).
543 if (pmp->spmp_hmp == NULL &&
544 (nip->flags & HAMMER2_INODE_ONRBTREE) == 0) {
545 hammer2_mtx_unlock(&nip->lock);
546 hammer2_inode_drop(nip);
550 hammer2_inode_repoint_one(nip, cluster, idx);
552 hammer2_inode_repoint(nip, NULL, cluster);
558 * We couldn't find the inode number, create a new inode.
560 nip = kmalloc(sizeof(*nip), pmp->minode, M_WAITOK | M_ZERO);
561 spin_init(&nip->cluster_spin, "h2clspin");
562 atomic_add_long(&pmp->inmem_inodes, 1);
563 hammer2_pfs_memory_inc(pmp);
564 hammer2_pfs_memory_wakeup(pmp);
566 nip->flags = HAMMER2_INODE_SROOT;
569 * Initialize nip's cluster. A cluster is provided for normal
570 * inodes but typically not for the super-root or PFS inodes.
572 nip->cluster.refs = 1;
573 nip->cluster.pmp = pmp;
574 nip->cluster.flags |= HAMMER2_CLUSTER_INODE;
576 nipdata = &hammer2_cluster_rdata(cluster)->ipdata;
577 nip->meta = nipdata->meta;
578 atomic_set_int(&nip->flags, HAMMER2_INODE_METAGOOD);
579 hammer2_inode_repoint(nip, NULL, cluster);
581 nip->meta.inum = 1; /* PFS inum is always 1 XXX */
582 /* mtime will be updated when a cluster is available */
583 atomic_set_int(&nip->flags, HAMMER2_INODE_METAGOOD);/*XXX*/
586 nip->pip = dip; /* can be NULL */
588 hammer2_inode_ref(dip); /* ref dip for nip->pip */
593 * ref and lock on nip gives it state compatible to after a
594 * hammer2_inode_lock() call.
597 hammer2_mtx_init(&nip->lock, "h2inode");
598 hammer2_mtx_ex(&nip->lock);
599 /* combination of thread lock and chain lock == inode lock */
602 * Attempt to add the inode. If it fails we raced another inode
603 * get. Undo all the work and try again.
605 if (pmp->spmp_hmp == NULL) {
606 hammer2_spin_ex(&pmp->inum_spin);
607 if (RB_INSERT(hammer2_inode_tree, &pmp->inum_tree, nip)) {
608 hammer2_spin_unex(&pmp->inum_spin);
609 hammer2_mtx_unlock(&nip->lock);
610 hammer2_inode_drop(nip);
613 atomic_set_int(&nip->flags, HAMMER2_INODE_ONRBTREE);
614 hammer2_spin_unex(&pmp->inum_spin);
621 * Create a new inode in the specified directory using the vattr to
622 * figure out the type of inode.
624 * If no error occurs the new inode with its cluster locked is returned in
625 * *nipp, otherwise an error is returned and *nipp is set to NULL.
627 * If vap and/or cred are NULL the related fields are not set and the
628 * inode type defaults to a directory. This is used when creating PFSs
629 * under the super-root, so the inode number is set to 1 in this case.
631 * dip is not locked on entry.
633 * NOTE: When used to create a snapshot, the inode is temporarily associated
634 * with the super-root spmp. XXX should pass new pmp for snapshot.
637 hammer2_inode_create(hammer2_inode_t *dip,
638 struct vattr *vap, struct ucred *cred,
639 const uint8_t *name, size_t name_len, hammer2_key_t lhc,
640 hammer2_key_t inum, uint8_t type, uint8_t target_type,
641 int flags, int *errorp)
643 hammer2_xop_create_t *xop;
644 hammer2_inode_t *nip;
650 uint8_t dip_comp_algo;
651 uint8_t dip_check_algo;
654 lhc = hammer2_dirhash(name, name_len);
659 * Locate the inode or indirect block to create the new
660 * entry in. At the same time check for key collisions
661 * and iterate until we don't get one.
663 * NOTE: hidden inodes do not have iterators.
665 * Lock the directory exclusively for now to guarantee that
666 * we can find an unused lhc for the name. Due to collisions,
667 * two different creates can end up with the same lhc so we
668 * cannot depend on the OS to prevent the collision.
670 hammer2_inode_lock(dip, 0);
672 dip_uid = dip->meta.uid;
673 dip_gid = dip->meta.gid;
674 dip_mode = dip->meta.mode;
675 dip_comp_algo = dip->meta.comp_algo;
676 dip_check_algo = dip->meta.check_algo;
679 * If name specified, locate an unused key in the collision space.
680 * Otherwise use the passed-in lhc directly.
683 hammer2_xop_scanlhc_t *sxop;
684 hammer2_key_t lhcbase;
687 sxop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
689 hammer2_xop_start(&sxop->head, hammer2_xop_scanlhc);
690 while ((error = hammer2_xop_collect(&sxop->head, 0)) == 0) {
691 if (lhc != sxop->head.cluster.focus->bref.key)
695 hammer2_xop_retire(&sxop->head, HAMMER2_XOPMASK_VOP);
703 if ((lhcbase ^ lhc) & ~HAMMER2_DIRHASH_LOMASK) {
710 * Create the inode with the lhc as the key.
712 xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
715 bzero(&xop->meta, sizeof(xop->meta));
718 xop->meta.type = hammer2_get_obj_type(vap->va_type);
720 switch (xop->meta.type) {
721 case HAMMER2_OBJTYPE_CDEV:
722 case HAMMER2_OBJTYPE_BDEV:
723 xop->meta.rmajor = vap->va_rmajor;
724 xop->meta.rminor = vap->va_rminor;
729 type = xop->meta.type;
731 xop->meta.type = type;
732 xop->meta.target_type = target_type;
734 xop->meta.inum = inum;
736 /* Inherit parent's inode compression mode. */
737 xop->meta.comp_algo = dip_comp_algo;
738 xop->meta.check_algo = dip_check_algo;
739 xop->meta.version = HAMMER2_INODE_VERSION_ONE;
740 hammer2_update_time(&xop->meta.ctime);
741 xop->meta.mtime = xop->meta.ctime;
743 xop->meta.mode = vap->va_mode;
744 xop->meta.nlinks = 1;
746 if (dip && dip->pmp) {
747 xuid = hammer2_to_unix_xid(&dip_uid);
748 xuid = vop_helper_create_uid(dip->pmp->mp,
754 /* super-root has no dip and/or pmp */
757 if (vap->va_vaflags & VA_UID_UUID_VALID)
758 xop->meta.uid = vap->va_uid_uuid;
759 else if (vap->va_uid != (uid_t)VNOVAL)
760 hammer2_guid_to_uuid(&xop->meta.uid, vap->va_uid);
762 hammer2_guid_to_uuid(&xop->meta.uid, xuid);
764 if (vap->va_vaflags & VA_GID_UUID_VALID)
765 xop->meta.gid = vap->va_gid_uuid;
766 else if (vap->va_gid != (gid_t)VNOVAL)
767 hammer2_guid_to_uuid(&xop->meta.gid, vap->va_gid);
769 xop->meta.gid = dip_gid;
773 * Regular files and softlinks allow a small amount of data to be
774 * directly embedded in the inode. This flag will be cleared if
775 * the size is extended past the embedded limit.
777 if (xop->meta.type == HAMMER2_OBJTYPE_REGFILE ||
778 xop->meta.type == HAMMER2_OBJTYPE_SOFTLINK ||
779 xop->meta.type == HAMMER2_OBJTYPE_HARDLINK) {
780 xop->meta.op_flags |= HAMMER2_OPFLAG_DIRECTDATA;
783 hammer2_xop_setname(&xop->head, name, name_len);
784 xop->meta.name_len = name_len;
785 xop->meta.name_key = lhc;
786 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
788 hammer2_xop_start(&xop->head, hammer2_inode_xop_create);
790 error = hammer2_xop_collect(&xop->head, 0);
792 kprintf("CREATE INODE %*.*s\n",
793 (int)name_len, (int)name_len, name);
802 * Set up the new inode if not a hardlink pointer.
804 * NOTE: *_get() integrates chain's lock into the inode lock.
806 * NOTE: Only one new inode can currently be created per
807 * transaction. If the need arises we can adjust
808 * hammer2_trans_init() to allow more.
810 * NOTE: nipdata will have chain's blockset data.
812 if (type != HAMMER2_OBJTYPE_HARDLINK) {
813 nip = hammer2_inode_get(dip->pmp, dip, &xop->head.cluster, -1);
814 nip->comp_heuristic = 0;
820 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
822 hammer2_inode_unlock(dip);
828 * Connect the disconnected inode (ip) to the directory (dip) with the
829 * specified (name, name_len). If name is NULL, (lhc) will be used as
830 * the directory key and the inode's embedded name will not be modified
831 * for future recovery purposes.
833 * dip and ip must both be locked exclusively (dip in particular to avoid
837 hammer2_inode_connect(hammer2_inode_t *dip, hammer2_inode_t *ip,
838 const char *name, size_t name_len,
841 hammer2_xop_scanlhc_t *sxop;
842 hammer2_xop_connect_t *xop;
843 hammer2_inode_t *opip;
844 hammer2_key_t lhcbase;
848 * Calculate the lhc and resolve the collision space.
851 lhc = lhcbase = hammer2_dirhash(name, name_len);
852 sxop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
854 hammer2_xop_start(&sxop->head, hammer2_xop_scanlhc);
855 while ((error = hammer2_xop_collect(&sxop->head, 0)) == 0) {
856 if (lhc != sxop->head.cluster.focus->bref.key)
860 hammer2_xop_retire(&sxop->head, HAMMER2_XOPMASK_VOP);
868 if ((lhcbase ^ lhc) & ~HAMMER2_DIRHASH_LOMASK) {
877 * Formally reconnect the in-memory structure. ip must
878 * be locked exclusively to safely change ip->pip.
880 if (ip->pip != dip) {
881 hammer2_inode_ref(dip);
885 hammer2_inode_drop(opip);
891 xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
893 hammer2_xop_setname(&xop->head, name, name_len);
894 hammer2_xop_setip2(&xop->head, ip);
896 hammer2_xop_start(&xop->head, hammer2_inode_xop_connect);
897 error = hammer2_xop_collect(&xop->head, 0);
898 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
901 * On success make the same adjustments to ip->meta or the
902 * next flush may blow up the chain.
905 hammer2_inode_modify(ip);
906 ip->meta.name_key = lhc;
908 ip->meta.name_len = name_len;
915 * Repoint ip->cluster's chains to cluster's chains and fixup the default
916 * focus. All items, valid or invalid, are repointed. hammer2_xop_start()
917 * filters out invalid or non-matching elements.
919 * Caller must hold the inode and cluster exclusive locked, if not NULL,
920 * must also be locked.
922 * Cluster may be NULL to clean out any chains in ip->cluster.
925 hammer2_inode_repoint(hammer2_inode_t *ip, hammer2_inode_t *pip,
926 hammer2_cluster_t *cluster)
928 hammer2_chain_t *dropch[HAMMER2_MAXCLUSTER];
929 hammer2_chain_t *ochain;
930 hammer2_chain_t *nchain;
931 hammer2_inode_t *opip;
934 bzero(dropch, sizeof(dropch));
937 * Replace chains in ip->cluster with chains from cluster and
938 * adjust the focus if necessary.
940 * NOTE: nchain and/or ochain can be NULL due to gaps
941 * in the cluster arrays.
943 hammer2_spin_ex(&ip->cluster_spin);
944 for (i = 0; cluster && i < cluster->nchains; ++i) {
946 * Do not replace elements which are the same. Also handle
947 * element count discrepancies.
949 nchain = cluster->array[i].chain;
950 if (i < ip->cluster.nchains) {
951 ochain = ip->cluster.array[i].chain;
952 if (ochain == nchain)
961 ip->cluster.array[i].chain = nchain;
962 ip->cluster.array[i].flags &= ~HAMMER2_CITEM_INVALID;
963 ip->cluster.array[i].flags |= cluster->array[i].flags &
964 HAMMER2_CITEM_INVALID;
966 hammer2_chain_ref(nchain);
971 * Release any left-over chains in ip->cluster.
973 while (i < ip->cluster.nchains) {
974 nchain = ip->cluster.array[i].chain;
976 ip->cluster.array[i].chain = NULL;
977 ip->cluster.array[i].flags |= HAMMER2_CITEM_INVALID;
984 * Fixup fields. Note that the inode-embedded cluster is never
988 ip->cluster.nchains = cluster->nchains;
989 ip->cluster.focus = cluster->focus;
990 ip->cluster.flags = cluster->flags & ~HAMMER2_CLUSTER_LOCKED;
992 ip->cluster.nchains = 0;
993 ip->cluster.focus = NULL;
994 ip->cluster.flags &= ~HAMMER2_CLUSTER_ZFLAGS;
998 * Repoint ip->pip if requested (non-NULL pip).
1000 if (pip && ip->pip != pip) {
1002 hammer2_inode_ref(pip);
1007 hammer2_spin_unex(&ip->cluster_spin);
1010 * Cleanup outside of spinlock
1014 hammer2_chain_drop(dropch[i]);
1017 hammer2_inode_drop(opip);
1021 * Repoint a single element from the cluster to the ip. Used by the
1022 * synchronization threads to piecemeal update inodes. Does not change
1023 * focus and requires inode to be re-locked to clean-up flags (XXX).
1026 hammer2_inode_repoint_one(hammer2_inode_t *ip, hammer2_cluster_t *cluster,
1029 hammer2_chain_t *ochain;
1030 hammer2_chain_t *nchain;
1033 hammer2_spin_ex(&ip->cluster_spin);
1034 KKASSERT(idx < cluster->nchains);
1035 if (idx < ip->cluster.nchains) {
1036 ochain = ip->cluster.array[idx].chain;
1037 nchain = cluster->array[idx].chain;
1040 nchain = cluster->array[idx].chain;
1041 ip->cluster.nchains = idx + 1;
1042 for (i = ip->cluster.nchains; i <= idx; ++i) {
1043 bzero(&ip->cluster.array[i],
1044 sizeof(ip->cluster.array[i]));
1045 ip->cluster.array[i].flags |= HAMMER2_CITEM_INVALID;
1048 if (ochain != nchain) {
1052 ip->cluster.array[idx].chain = nchain;
1053 ip->cluster.array[idx].flags &= ~HAMMER2_CITEM_INVALID;
1054 ip->cluster.array[idx].flags |= cluster->array[idx].flags &
1055 HAMMER2_CITEM_INVALID;
1057 hammer2_spin_unex(&ip->cluster_spin);
1058 if (ochain != nchain) {
1060 hammer2_chain_ref(nchain);
1062 hammer2_chain_drop(ochain);
1067 * Called with a locked inode to finish unlinking an inode after xop_unlink
1068 * had been run. This function is responsible for decrementing nlinks and
1069 * moving deleted inodes to the hidden directory if they are still open.
1071 * We don't bother decrementing nlinks if the file is not open and this was
1074 * If the inode is a hardlink target it's chain has not yet been deleted,
1075 * otherwise it's chain has been deleted.
1077 * If isopen then any prior deletion was not permanent and the inode must
1078 * be moved to the hidden directory.
1081 hammer2_inode_unlink_finisher(hammer2_inode_t *ip, int isopen)
1089 * Decrement nlinks. If this is the last link and the file is
1090 * not open, the chain has already been removed and we don't bother
1091 * dirtying the inode.
1093 if (ip->meta.nlinks == 1) {
1094 atomic_set_int(&ip->flags, HAMMER2_INODE_ISUNLINKED);
1096 atomic_set_int(&ip->flags, HAMMER2_INODE_ISDELETED);
1101 hammer2_inode_modify(ip);
1103 if ((int64_t)ip->meta.nlinks < 0)
1104 ip->meta.nlinks = 0; /* safety */
1107 * If nlinks is not zero we are done. However, this should only be
1108 * possible with a hardlink target. If the inode is an embedded
1109 * hardlink nlinks should have dropped to zero, warn and proceed
1110 * with the next step.
1112 if (ip->meta.nlinks) {
1113 if ((ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE) == 0)
1115 kprintf("hammer2_inode_unlink: nlinks was not 0 (%jd)\n",
1116 (intmax_t)ip->meta.nlinks);
1121 * nlinks is now zero, the inode should have already been deleted.
1122 * If the file is open it was deleted non-permanently and must be
1123 * moved to the hidden directory.
1125 * When moving to the hidden directory we force the name_key to the
1126 * inode number to avoid collisions.
1129 hammer2_inode_lock(pmp->ihidden, 0);
1130 error = hammer2_inode_connect(pmp->ihidden, ip,
1131 NULL, 0, ip->meta.inum);
1132 hammer2_inode_unlock(pmp->ihidden);
1140 * This is called from the mount code to initialize pmp->ihidden
1143 hammer2_inode_install_hidden(hammer2_pfs_t *pmp)
1150 hammer2_trans_init(pmp, 0);
1151 hammer2_inode_lock(pmp->iroot, 0);
1154 * Find the hidden directory
1157 hammer2_xop_lookup_t *xop;
1159 xop = hammer2_xop_alloc(pmp->iroot, HAMMER2_XOP_MODIFYING);
1160 xop->lhc = HAMMER2_INODE_HIDDENDIR;
1161 hammer2_xop_start(&xop->head, hammer2_xop_lookup);
1162 error = hammer2_xop_collect(&xop->head, 0);
1166 * Found the hidden directory
1168 kprintf("PFS FOUND HIDDEN DIR\n");
1169 pmp->ihidden = hammer2_inode_get(pmp, pmp->iroot,
1172 hammer2_inode_ref(pmp->ihidden);
1173 hammer2_inode_unlock(pmp->ihidden);
1175 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1179 * Create the hidden directory if it could not be found.
1181 if (error == ENOENT) {
1182 kprintf("PFS CREATE HIDDEN DIR\n");
1184 pmp->ihidden = hammer2_inode_create(pmp->iroot, NULL, NULL,
1186 /* lhc */ HAMMER2_INODE_HIDDENDIR,
1187 /* inum */ HAMMER2_INODE_HIDDENDIR,
1188 /* type */ HAMMER2_OBJTYPE_DIRECTORY,
1189 /* target_type */ 0,
1193 hammer2_inode_ref(pmp->ihidden);
1194 hammer2_inode_unlock(pmp->ihidden);
1197 kprintf("PFS CREATE ERROR %d\n", error);
1201 * Scan the hidden directory on-mount and destroy its contents
1204 hammer2_xop_unlinkall_t *xop;
1206 hammer2_inode_lock(pmp->ihidden, 0);
1207 xop = hammer2_xop_alloc(pmp->ihidden, HAMMER2_XOP_MODIFYING);
1208 xop->key_beg = HAMMER2_KEY_MIN;
1209 xop->key_end = HAMMER2_KEY_MAX;
1210 hammer2_xop_start(&xop->head, hammer2_inode_xop_unlinkall);
1212 while ((error = hammer2_xop_collect(&xop->head, 0)) == 0) {
1215 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1216 hammer2_inode_unlock(pmp->ihidden);
1219 hammer2_inode_unlock(pmp->iroot);
1220 hammer2_trans_done(pmp);
1224 * Find the directory common to both fdip and tdip.
1226 * Returns a held but not locked inode. Caller typically locks the inode,
1227 * and when through unlocks AND drops it.
1230 hammer2_inode_common_parent(hammer2_inode_t *fdip, hammer2_inode_t *tdip)
1232 hammer2_inode_t *scan1;
1233 hammer2_inode_t *scan2;
1236 * We used to have a depth field but it complicated matters too
1237 * much for directory renames. So now its ugly. Check for
1238 * simple cases before giving up and doing it the expensive way.
1240 * XXX need a bottom-up topology stability lock
1242 if (fdip == tdip || fdip == tdip->pip) {
1243 hammer2_inode_ref(fdip);
1246 if (fdip->pip == tdip) {
1247 hammer2_inode_ref(tdip);
1254 for (scan1 = fdip; scan1->pmp == fdip->pmp; scan1 = scan1->pip) {
1256 while (scan2->pmp == tdip->pmp) {
1257 if (scan1 == scan2) {
1258 hammer2_inode_ref(scan1);
1266 panic("hammer2_inode_common_parent: no common parent %p %p\n",
1273 * Mark an inode as being modified, meaning that the caller will modify
1276 * NOTE: No mtid (modify_tid) is passed into this routine. The caller is
1277 * only modifying the in-memory inode. A modify_tid is synchronized
1278 * later when the inode gets flushed.
1281 hammer2_inode_modify(hammer2_inode_t *ip)
1283 atomic_set_int(&ip->flags, HAMMER2_INODE_MODIFIED);
1285 vsetisdirty(ip->vp);
1289 * Synchronize the inode's frontend state with the chain state prior
1290 * to any explicit flush of the inode or any strategy write call.
1292 * Called with a locked inode inside a transaction.
1295 hammer2_inode_fsync(hammer2_inode_t *ip)
1297 if (ip->flags & (HAMMER2_INODE_RESIZED | HAMMER2_INODE_MODIFIED)) {
1298 hammer2_xop_fsync_t *xop;
1301 xop = hammer2_xop_alloc(ip, HAMMER2_XOP_MODIFYING);
1302 xop->clear_directdata = 0;
1303 if (ip->flags & HAMMER2_INODE_RESIZED) {
1304 if ((ip->meta.op_flags & HAMMER2_OPFLAG_DIRECTDATA) &&
1305 ip->meta.size > HAMMER2_EMBEDDED_BYTES) {
1306 ip->meta.op_flags &= ~HAMMER2_OPFLAG_DIRECTDATA;
1307 xop->clear_directdata = 1;
1309 xop->osize = ip->osize;
1311 xop->osize = ip->meta.size; /* safety */
1313 xop->ipflags = ip->flags;
1314 xop->meta = ip->meta;
1316 atomic_clear_int(&ip->flags, HAMMER2_INODE_RESIZED |
1317 HAMMER2_INODE_MODIFIED);
1318 hammer2_xop_start(&xop->head, hammer2_inode_xop_fsync);
1319 error = hammer2_xop_collect(&xop->head, 0);
1320 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1321 if (error == ENOENT)
1324 kprintf("hammer2: unable to fsync inode %p\n", ip);
1326 atomic_set_int(&ip->flags,
1327 xop->ipflags & (HAMMER2_INODE_RESIZED |
1328 HAMMER2_INODE_MODIFIED));
1330 /* XXX return error somehow? */
1336 * This handles unlinked open files after the vnode is finally dereferenced.
1337 * To avoid deadlocks it cannot be called from the normal vnode recycling
1338 * path, so we call it (1) after a unlink, rmdir, or rename, (2) on every
1339 * flush, and (3) on umount.
1341 * Caller must be in a transaction.
1344 hammer2_inode_run_unlinkq(hammer2_pfs_t *pmp)
1346 hammer2_xop_destroy_t *xop;
1347 hammer2_inode_unlink_t *ipul;
1348 hammer2_inode_t *ip;
1351 if (TAILQ_EMPTY(&pmp->unlinkq))
1355 hammer2_spin_ex(&pmp->list_spin);
1356 while ((ipul = TAILQ_FIRST(&pmp->unlinkq)) != NULL) {
1357 TAILQ_REMOVE(&pmp->unlinkq, ipul, entry);
1358 hammer2_spin_unex(&pmp->list_spin);
1360 kfree(ipul, pmp->minode);
1362 hammer2_inode_lock(ip, 0);
1363 xop = hammer2_xop_alloc(ip, HAMMER2_XOP_MODIFYING);
1364 hammer2_xop_start(&xop->head, hammer2_inode_xop_destroy);
1365 error = hammer2_xop_collect(&xop->head, 0);
1366 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1368 atomic_clear_int(&ip->flags, HAMMER2_INODE_ISDELETED);
1370 hammer2_inode_unlock(ip);
1371 hammer2_inode_drop(ip); /* ipul ref */
1373 hammer2_spin_ex(&pmp->list_spin);
1375 hammer2_spin_unex(&pmp->list_spin);
1380 * Inode create helper (threaded, backend)
1382 * Used by ncreate, nmknod, nsymlink, nmkdir.
1383 * Used by nlink and rename to create HARDLINK pointers.
1385 * Frontend holds the parent directory ip locked exclusively. We
1386 * create the inode and feed the exclusively locked chain to the
1390 hammer2_inode_xop_create(hammer2_xop_t *arg, int clindex)
1392 hammer2_xop_create_t *xop = &arg->xop_create;
1393 hammer2_chain_t *parent;
1394 hammer2_chain_t *chain;
1395 hammer2_key_t key_next;
1396 int cache_index = -1;
1399 if (hammer2_debug & 0x0001)
1400 kprintf("inode_create lhc %016jx clindex %d\n",
1404 parent = hammer2_inode_chain(xop->head.ip, clindex,
1405 HAMMER2_RESOLVE_ALWAYS);
1406 if (parent == NULL) {
1410 chain = hammer2_chain_lookup(&parent, &key_next,
1414 hammer2_chain_unlock(chain);
1419 error = hammer2_chain_create(&parent, &chain,
1422 HAMMER2_BREF_TYPE_INODE,
1423 HAMMER2_INODE_BYTES,
1424 xop->head.mtid, xop->flags);
1426 hammer2_chain_modify(chain, xop->head.mtid, 0);
1427 chain->data->ipdata.meta = xop->meta;
1428 if (xop->head.name) {
1429 bcopy(xop->head.name,
1430 chain->data->ipdata.filename,
1431 xop->head.name_len);
1432 chain->data->ipdata.meta.name_len = xop->head.name_len;
1434 chain->data->ipdata.meta.name_key = xop->lhc;
1436 hammer2_chain_unlock(chain);
1437 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS |
1438 HAMMER2_RESOLVE_SHARED);
1441 hammer2_chain_unlock(parent);
1442 hammer2_chain_drop(parent);
1444 hammer2_xop_feed(&xop->head, chain, clindex, error);
1446 hammer2_chain_drop(chain);
1450 * Inode delete helper (backend, threaded)
1452 * Generally used by hammer2_run_unlinkq()
1455 hammer2_inode_xop_destroy(hammer2_xop_t *arg, int clindex)
1457 hammer2_xop_destroy_t *xop = &arg->xop_destroy;
1459 hammer2_chain_t *parent;
1460 hammer2_chain_t *chain;
1461 hammer2_inode_t *ip;
1465 * We need the precise parent chain to issue the deletion.
1471 parent = hammer2_inode_chain(ip, clindex, HAMMER2_RESOLVE_ALWAYS);
1473 hammer2_chain_getparent(&parent, HAMMER2_RESOLVE_ALWAYS);
1474 if (parent == NULL) {
1478 chain = hammer2_inode_chain(ip, clindex, HAMMER2_RESOLVE_ALWAYS);
1479 if (chain == NULL) {
1483 hammer2_chain_delete(parent, chain, xop->head.mtid, 0);
1486 hammer2_xop_feed(&xop->head, NULL, clindex, error);
1488 hammer2_chain_unlock(parent);
1489 hammer2_chain_drop(parent);
1492 hammer2_chain_unlock(chain);
1493 hammer2_chain_drop(chain);
1498 hammer2_inode_xop_unlinkall(hammer2_xop_t *arg, int clindex)
1500 hammer2_xop_unlinkall_t *xop = &arg->xop_unlinkall;
1501 hammer2_chain_t *parent;
1502 hammer2_chain_t *chain;
1503 hammer2_key_t key_next;
1504 int cache_index = -1;
1507 * We need the precise parent chain to issue the deletion.
1509 parent = hammer2_inode_chain(xop->head.ip, clindex,
1510 HAMMER2_RESOLVE_ALWAYS);
1512 if (parent == NULL) {
1516 chain = hammer2_chain_lookup(&parent, &key_next,
1517 xop->key_beg, xop->key_end,
1519 HAMMER2_LOOKUP_ALWAYS);
1521 hammer2_chain_delete(parent, chain,
1522 xop->head.mtid, HAMMER2_DELETE_PERMANENT);
1523 hammer2_chain_unlock(chain);
1524 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS |
1525 HAMMER2_RESOLVE_SHARED);
1526 hammer2_xop_feed(&xop->head, chain, clindex, chain->error);
1527 chain = hammer2_chain_next(&parent, chain, &key_next,
1528 key_next, xop->key_end,
1530 HAMMER2_LOOKUP_ALWAYS |
1531 HAMMER2_LOOKUP_NOUNLOCK);
1534 hammer2_xop_feed(&xop->head, NULL, clindex, ENOENT);
1536 hammer2_chain_unlock(parent);
1537 hammer2_chain_drop(parent);
1540 hammer2_chain_unlock(chain);
1541 hammer2_chain_drop(chain);
1546 hammer2_inode_xop_connect(hammer2_xop_t *arg, int clindex)
1548 hammer2_xop_connect_t *xop = &arg->xop_connect;
1549 hammer2_inode_data_t *wipdata;
1550 hammer2_chain_t *parent;
1551 hammer2_chain_t *chain;
1553 hammer2_key_t key_dummy;
1554 int cache_index = -1;
1558 * Get directory, then issue a lookup to prime the parent chain
1559 * for the create. The lookup is expected to fail.
1561 pmp = xop->head.ip->pmp;
1562 parent = hammer2_inode_chain(xop->head.ip, clindex,
1563 HAMMER2_RESOLVE_ALWAYS);
1564 if (parent == NULL) {
1569 chain = hammer2_chain_lookup(&parent, &key_dummy,
1573 hammer2_chain_unlock(chain);
1574 hammer2_chain_drop(chain);
1581 * Adjust the filename in the inode, set the name key.
1583 * NOTE: Frontend must also adjust ip2->meta on success, we can't
1586 chain = hammer2_inode_chain(xop->head.ip2, clindex,
1587 HAMMER2_RESOLVE_ALWAYS);
1588 hammer2_chain_modify(chain, xop->head.mtid, 0);
1589 wipdata = &chain->data->ipdata;
1591 hammer2_inode_modify(xop->head.ip2);
1592 if (xop->head.name) {
1593 bzero(wipdata->filename, sizeof(wipdata->filename));
1594 bcopy(xop->head.name, wipdata->filename, xop->head.name_len);
1595 wipdata->meta.name_len = xop->head.name_len;
1597 wipdata->meta.name_key = xop->lhc;
1600 * Reconnect the chain to the new parent directory
1602 error = hammer2_chain_create(&parent, &chain, pmp,
1604 HAMMER2_BREF_TYPE_INODE,
1605 HAMMER2_INODE_BYTES,
1612 hammer2_xop_feed(&xop->head, NULL, clindex, error);
1614 hammer2_chain_unlock(parent);
1615 hammer2_chain_drop(parent);
1618 hammer2_chain_unlock(chain);
1619 hammer2_chain_drop(chain);
1624 hammer2_inode_xop_fsync(hammer2_xop_t *arg, int clindex)
1626 hammer2_xop_fsync_t *xop = &arg->xop_fsync;
1627 hammer2_chain_t *parent;
1628 hammer2_chain_t *chain;
1631 parent = hammer2_inode_chain(xop->head.ip, clindex,
1632 HAMMER2_RESOLVE_ALWAYS);
1634 if (parent == NULL) {
1638 if (parent->error) {
1639 error = parent->error;
1645 if ((xop->ipflags & HAMMER2_INODE_RESIZED) == 0) {
1646 /* osize must be ignored */
1647 } else if (xop->meta.size < xop->osize) {
1649 * We must delete any chains beyond the EOF. The chain
1650 * straddling the EOF will be pending in the bioq.
1652 hammer2_key_t lbase;
1653 hammer2_key_t key_next;
1654 int cache_index = -1;
1656 lbase = (xop->meta.size + HAMMER2_PBUFMASK64) &
1657 ~HAMMER2_PBUFMASK64;
1658 chain = hammer2_chain_lookup(&parent, &key_next,
1659 lbase, HAMMER2_KEY_MAX,
1661 HAMMER2_LOOKUP_NODATA |
1662 HAMMER2_LOOKUP_NODIRECT);
1665 * Degenerate embedded case, nothing to loop on
1667 switch (chain->bref.type) {
1668 case HAMMER2_BREF_TYPE_INODE:
1671 case HAMMER2_BREF_TYPE_DATA:
1672 hammer2_chain_delete(parent, chain,
1674 HAMMER2_DELETE_PERMANENT);
1677 chain = hammer2_chain_next(&parent, chain, &key_next,
1678 key_next, HAMMER2_KEY_MAX,
1680 HAMMER2_LOOKUP_NODATA |
1681 HAMMER2_LOOKUP_NODIRECT);
1686 * Sync the inode meta-data, potentially clear the blockset area
1687 * of direct data so it can be used for blockrefs.
1689 hammer2_chain_modify(parent, xop->head.mtid, 0);
1690 parent->data->ipdata.meta = xop->meta;
1691 if (xop->clear_directdata) {
1692 bzero(&parent->data->ipdata.u.blockset,
1693 sizeof(parent->data->ipdata.u.blockset));
1697 hammer2_chain_unlock(chain);
1698 hammer2_chain_drop(chain);
1701 hammer2_chain_unlock(parent);
1702 hammer2_chain_drop(parent);
1704 hammer2_xop_feed(&xop->head, NULL, clindex, error);