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;
467 panic("hammer2: unhandled objtype %d",
472 if (ip == pmp->iroot)
473 vsetflags(vp, VROOT);
477 hammer2_inode_ref(ip); /* vp association */
478 hammer2_inode_lock_downgrade(ip, wasexclusive);
483 * Return non-NULL vp and *errorp == 0, or NULL vp and *errorp != 0.
485 if (hammer2_debug & 0x0002) {
486 kprintf("igetv vp %p refs 0x%08x aux 0x%08x\n",
487 vp, vp->v_refcnt, vp->v_auxrefs);
493 * Returns the inode associated with the passed-in cluster, creating the
494 * inode if necessary and synchronizing it to the passed-in cluster otherwise.
495 * When synchronizing, if idx >= 0, only cluster index (idx) is synchronized.
496 * Otherwise the whole cluster is synchronized.
498 * The passed-in cluster must be locked and will remain locked on return.
499 * The returned inode will be locked and the caller may dispose of both
500 * via hammer2_inode_unlock() + hammer2_inode_drop(). However, if the caller
501 * needs to resolve a hardlink it must ref/unlock/relock/drop the inode.
503 * The hammer2_inode structure regulates the interface between the high level
504 * kernel VNOPS API and the filesystem backend (the chains).
506 * On return the inode is locked with the supplied cluster.
509 hammer2_inode_get(hammer2_pfs_t *pmp, hammer2_inode_t *dip,
510 hammer2_cluster_t *cluster, int idx)
512 hammer2_inode_t *nip;
513 const hammer2_inode_data_t *iptmp;
514 const hammer2_inode_data_t *nipdata;
516 KKASSERT(cluster == NULL ||
517 hammer2_cluster_type(cluster) == HAMMER2_BREF_TYPE_INODE);
521 * Interlocked lookup/ref of the inode. This code is only needed
522 * when looking up inodes with nlinks != 0 (TODO: optimize out
523 * otherwise and test for duplicates).
525 * Cluster can be NULL during the initial pfs allocation.
529 iptmp = &hammer2_cluster_rdata(cluster)->ipdata;
530 nip = hammer2_inode_lookup(pmp, iptmp->meta.inum);
534 hammer2_mtx_ex(&nip->lock);
537 * Handle SMP race (not applicable to the super-root spmp
538 * which can't index inodes due to duplicative inode numbers).
540 if (pmp->spmp_hmp == NULL &&
541 (nip->flags & HAMMER2_INODE_ONRBTREE) == 0) {
542 hammer2_mtx_unlock(&nip->lock);
543 hammer2_inode_drop(nip);
547 hammer2_inode_repoint_one(nip, cluster, idx);
549 hammer2_inode_repoint(nip, NULL, cluster);
555 * We couldn't find the inode number, create a new inode.
557 nip = kmalloc(sizeof(*nip), pmp->minode, M_WAITOK | M_ZERO);
558 spin_init(&nip->cluster_spin, "h2clspin");
559 atomic_add_long(&pmp->inmem_inodes, 1);
560 hammer2_pfs_memory_inc(pmp);
561 hammer2_pfs_memory_wakeup(pmp);
563 nip->flags = HAMMER2_INODE_SROOT;
566 * Initialize nip's cluster. A cluster is provided for normal
567 * inodes but typically not for the super-root or PFS inodes.
569 nip->cluster.refs = 1;
570 nip->cluster.pmp = pmp;
571 nip->cluster.flags |= HAMMER2_CLUSTER_INODE;
573 nipdata = &hammer2_cluster_rdata(cluster)->ipdata;
574 nip->meta = nipdata->meta;
575 atomic_set_int(&nip->flags, HAMMER2_INODE_METAGOOD);
576 hammer2_inode_repoint(nip, NULL, cluster);
578 nip->meta.inum = 1; /* PFS inum is always 1 XXX */
579 /* mtime will be updated when a cluster is available */
580 atomic_set_int(&nip->flags, HAMMER2_INODE_METAGOOD);/*XXX*/
583 nip->pip = dip; /* can be NULL */
585 hammer2_inode_ref(dip); /* ref dip for nip->pip */
590 * ref and lock on nip gives it state compatible to after a
591 * hammer2_inode_lock() call.
594 hammer2_mtx_init(&nip->lock, "h2inode");
595 hammer2_mtx_ex(&nip->lock);
596 /* combination of thread lock and chain lock == inode lock */
599 * Attempt to add the inode. If it fails we raced another inode
600 * get. Undo all the work and try again.
602 if (pmp->spmp_hmp == NULL) {
603 hammer2_spin_ex(&pmp->inum_spin);
604 if (RB_INSERT(hammer2_inode_tree, &pmp->inum_tree, nip)) {
605 hammer2_spin_unex(&pmp->inum_spin);
606 hammer2_mtx_unlock(&nip->lock);
607 hammer2_inode_drop(nip);
610 atomic_set_int(&nip->flags, HAMMER2_INODE_ONRBTREE);
611 hammer2_spin_unex(&pmp->inum_spin);
618 * Create a new inode in the specified directory using the vattr to
619 * figure out the type of inode.
621 * If no error occurs the new inode with its cluster locked is returned in
622 * *nipp, otherwise an error is returned and *nipp is set to NULL.
624 * If vap and/or cred are NULL the related fields are not set and the
625 * inode type defaults to a directory. This is used when creating PFSs
626 * under the super-root, so the inode number is set to 1 in this case.
628 * dip is not locked on entry.
630 * NOTE: When used to create a snapshot, the inode is temporarily associated
631 * with the super-root spmp. XXX should pass new pmp for snapshot.
634 hammer2_inode_create(hammer2_inode_t *dip,
635 struct vattr *vap, struct ucred *cred,
636 const uint8_t *name, size_t name_len, hammer2_key_t lhc,
637 hammer2_key_t inum, uint8_t type, uint8_t target_type,
638 int flags, int *errorp)
640 hammer2_xop_create_t *xop;
641 hammer2_inode_t *nip;
647 uint8_t dip_comp_algo;
648 uint8_t dip_check_algo;
651 lhc = hammer2_dirhash(name, name_len);
656 * Locate the inode or indirect block to create the new
657 * entry in. At the same time check for key collisions
658 * and iterate until we don't get one.
660 * NOTE: hidden inodes do not have iterators.
662 * Lock the directory exclusively for now to guarantee that
663 * we can find an unused lhc for the name. Due to collisions,
664 * two different creates can end up with the same lhc so we
665 * cannot depend on the OS to prevent the collision.
667 hammer2_inode_lock(dip, 0);
669 dip_uid = dip->meta.uid;
670 dip_gid = dip->meta.gid;
671 dip_mode = dip->meta.mode;
672 dip_comp_algo = dip->meta.comp_algo;
673 dip_check_algo = dip->meta.check_algo;
676 * If name specified, locate an unused key in the collision space.
677 * Otherwise use the passed-in lhc directly.
680 hammer2_xop_scanlhc_t *sxop;
681 hammer2_key_t lhcbase;
684 sxop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
686 hammer2_xop_start(&sxop->head, hammer2_xop_scanlhc);
687 while ((error = hammer2_xop_collect(&sxop->head, 0)) == 0) {
688 if (lhc != sxop->head.cluster.focus->bref.key)
692 hammer2_xop_retire(&sxop->head, HAMMER2_XOPMASK_VOP);
700 if ((lhcbase ^ lhc) & ~HAMMER2_DIRHASH_LOMASK) {
707 * Create the inode with the lhc as the key.
709 xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
712 bzero(&xop->meta, sizeof(xop->meta));
715 xop->meta.type = hammer2_get_obj_type(vap->va_type);
717 switch (xop->meta.type) {
718 case HAMMER2_OBJTYPE_CDEV:
719 case HAMMER2_OBJTYPE_BDEV:
720 xop->meta.rmajor = vap->va_rmajor;
721 xop->meta.rminor = vap->va_rminor;
726 type = xop->meta.type;
728 xop->meta.type = type;
729 xop->meta.target_type = target_type;
731 xop->meta.inum = inum;
733 /* Inherit parent's inode compression mode. */
734 xop->meta.comp_algo = dip_comp_algo;
735 xop->meta.check_algo = dip_check_algo;
736 xop->meta.version = HAMMER2_INODE_VERSION_ONE;
737 hammer2_update_time(&xop->meta.ctime);
738 xop->meta.mtime = xop->meta.ctime;
740 xop->meta.mode = vap->va_mode;
741 xop->meta.nlinks = 1;
743 if (dip && dip->pmp) {
744 xuid = hammer2_to_unix_xid(&dip_uid);
745 xuid = vop_helper_create_uid(dip->pmp->mp,
751 /* super-root has no dip and/or pmp */
754 if (vap->va_vaflags & VA_UID_UUID_VALID)
755 xop->meta.uid = vap->va_uid_uuid;
756 else if (vap->va_uid != (uid_t)VNOVAL)
757 hammer2_guid_to_uuid(&xop->meta.uid, vap->va_uid);
759 hammer2_guid_to_uuid(&xop->meta.uid, xuid);
761 if (vap->va_vaflags & VA_GID_UUID_VALID)
762 xop->meta.gid = vap->va_gid_uuid;
763 else if (vap->va_gid != (gid_t)VNOVAL)
764 hammer2_guid_to_uuid(&xop->meta.gid, vap->va_gid);
766 xop->meta.gid = dip_gid;
770 * Regular files and softlinks allow a small amount of data to be
771 * directly embedded in the inode. This flag will be cleared if
772 * the size is extended past the embedded limit.
774 if (xop->meta.type == HAMMER2_OBJTYPE_REGFILE ||
775 xop->meta.type == HAMMER2_OBJTYPE_SOFTLINK ||
776 xop->meta.type == HAMMER2_OBJTYPE_HARDLINK) {
777 xop->meta.op_flags |= HAMMER2_OPFLAG_DIRECTDATA;
780 hammer2_xop_setname(&xop->head, name, name_len);
781 xop->meta.name_len = name_len;
782 xop->meta.name_key = lhc;
783 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
785 hammer2_xop_start(&xop->head, hammer2_inode_xop_create);
787 error = hammer2_xop_collect(&xop->head, 0);
789 kprintf("CREATE INODE %*.*s\n",
790 (int)name_len, (int)name_len, name);
799 * Set up the new inode if not a hardlink pointer.
801 * NOTE: *_get() integrates chain's lock into the inode lock.
803 * NOTE: Only one new inode can currently be created per
804 * transaction. If the need arises we can adjust
805 * hammer2_trans_init() to allow more.
807 * NOTE: nipdata will have chain's blockset data.
809 if (type != HAMMER2_OBJTYPE_HARDLINK) {
810 nip = hammer2_inode_get(dip->pmp, dip, &xop->head.cluster, -1);
811 nip->comp_heuristic = 0;
817 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
819 hammer2_inode_unlock(dip);
825 * Connect the disconnected inode (ip) to the directory (dip) with the
826 * specified (name, name_len). If name is NULL, (lhc) will be used as
827 * the directory key and the inode's embedded name will not be modified
828 * for future recovery purposes.
830 * dip and ip must both be locked exclusively (dip in particular to avoid
834 hammer2_inode_connect(hammer2_inode_t *dip, hammer2_inode_t *ip,
835 const char *name, size_t name_len,
838 hammer2_xop_scanlhc_t *sxop;
839 hammer2_xop_connect_t *xop;
840 hammer2_inode_t *opip;
841 hammer2_key_t lhcbase;
845 * Calculate the lhc and resolve the collision space.
848 lhc = lhcbase = hammer2_dirhash(name, name_len);
849 sxop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
851 hammer2_xop_start(&sxop->head, hammer2_xop_scanlhc);
852 while ((error = hammer2_xop_collect(&sxop->head, 0)) == 0) {
853 if (lhc != sxop->head.cluster.focus->bref.key)
857 hammer2_xop_retire(&sxop->head, HAMMER2_XOPMASK_VOP);
865 if ((lhcbase ^ lhc) & ~HAMMER2_DIRHASH_LOMASK) {
874 * Formally reconnect the in-memory structure. ip must
875 * be locked exclusively to safely change ip->pip.
877 if (ip->pip != dip) {
878 hammer2_inode_ref(dip);
882 hammer2_inode_drop(opip);
888 xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
890 hammer2_xop_setname(&xop->head, name, name_len);
891 hammer2_xop_setip2(&xop->head, ip);
893 hammer2_xop_start(&xop->head, hammer2_inode_xop_connect);
894 error = hammer2_xop_collect(&xop->head, 0);
895 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
898 * On success make the same adjustments to ip->meta or the
899 * next flush may blow up the chain.
902 hammer2_inode_modify(ip);
903 ip->meta.name_key = lhc;
905 ip->meta.name_len = name_len;
912 * Repoint ip->cluster's chains to cluster's chains and fixup the default
913 * focus. All items, valid or invalid, are repointed. hammer2_xop_start()
914 * filters out invalid or non-matching elements.
916 * Caller must hold the inode and cluster exclusive locked, if not NULL,
917 * must also be locked.
919 * Cluster may be NULL to clean out any chains in ip->cluster.
922 hammer2_inode_repoint(hammer2_inode_t *ip, hammer2_inode_t *pip,
923 hammer2_cluster_t *cluster)
925 hammer2_chain_t *dropch[HAMMER2_MAXCLUSTER];
926 hammer2_chain_t *ochain;
927 hammer2_chain_t *nchain;
928 hammer2_inode_t *opip;
931 bzero(dropch, sizeof(dropch));
934 * Replace chains in ip->cluster with chains from cluster and
935 * adjust the focus if necessary.
937 * NOTE: nchain and/or ochain can be NULL due to gaps
938 * in the cluster arrays.
940 hammer2_spin_ex(&ip->cluster_spin);
941 for (i = 0; cluster && i < cluster->nchains; ++i) {
943 * Do not replace elements which are the same. Also handle
944 * element count discrepancies.
946 nchain = cluster->array[i].chain;
947 if (i < ip->cluster.nchains) {
948 ochain = ip->cluster.array[i].chain;
949 if (ochain == nchain)
958 ip->cluster.array[i].chain = nchain;
959 ip->cluster.array[i].flags &= ~HAMMER2_CITEM_INVALID;
960 ip->cluster.array[i].flags |= cluster->array[i].flags &
961 HAMMER2_CITEM_INVALID;
963 hammer2_chain_ref(nchain);
968 * Release any left-over chains in ip->cluster.
970 while (i < ip->cluster.nchains) {
971 nchain = ip->cluster.array[i].chain;
973 ip->cluster.array[i].chain = NULL;
974 ip->cluster.array[i].flags |= HAMMER2_CITEM_INVALID;
981 * Fixup fields. Note that the inode-embedded cluster is never
985 ip->cluster.nchains = cluster->nchains;
986 ip->cluster.focus = cluster->focus;
987 ip->cluster.flags = cluster->flags & ~HAMMER2_CLUSTER_LOCKED;
989 ip->cluster.nchains = 0;
990 ip->cluster.focus = NULL;
991 ip->cluster.flags &= ~HAMMER2_CLUSTER_ZFLAGS;
995 * Repoint ip->pip if requested (non-NULL pip).
997 if (pip && ip->pip != pip) {
999 hammer2_inode_ref(pip);
1004 hammer2_spin_unex(&ip->cluster_spin);
1007 * Cleanup outside of spinlock
1011 hammer2_chain_drop(dropch[i]);
1014 hammer2_inode_drop(opip);
1018 * Repoint a single element from the cluster to the ip. Used by the
1019 * synchronization threads to piecemeal update inodes. Does not change
1020 * focus and requires inode to be re-locked to clean-up flags (XXX).
1023 hammer2_inode_repoint_one(hammer2_inode_t *ip, hammer2_cluster_t *cluster,
1026 hammer2_chain_t *ochain;
1027 hammer2_chain_t *nchain;
1030 hammer2_spin_ex(&ip->cluster_spin);
1031 KKASSERT(idx < cluster->nchains);
1032 if (idx < ip->cluster.nchains) {
1033 ochain = ip->cluster.array[idx].chain;
1034 nchain = cluster->array[idx].chain;
1037 nchain = cluster->array[idx].chain;
1038 ip->cluster.nchains = idx + 1;
1039 for (i = ip->cluster.nchains; i <= idx; ++i) {
1040 bzero(&ip->cluster.array[i],
1041 sizeof(ip->cluster.array[i]));
1042 ip->cluster.array[i].flags |= HAMMER2_CITEM_INVALID;
1045 if (ochain != nchain) {
1049 ip->cluster.array[idx].chain = nchain;
1050 ip->cluster.array[idx].flags &= ~HAMMER2_CITEM_INVALID;
1051 ip->cluster.array[idx].flags |= cluster->array[idx].flags &
1052 HAMMER2_CITEM_INVALID;
1054 hammer2_spin_unex(&ip->cluster_spin);
1055 if (ochain != nchain) {
1057 hammer2_chain_ref(nchain);
1059 hammer2_chain_drop(ochain);
1064 * Called with a locked inode to finish unlinking an inode after xop_unlink
1065 * had been run. This function is responsible for decrementing nlinks and
1066 * moving deleted inodes to the hidden directory if they are still open.
1068 * We don't bother decrementing nlinks if the file is not open and this was
1071 * If the inode is a hardlink target it's chain has not yet been deleted,
1072 * otherwise it's chain has been deleted.
1074 * If isopen then any prior deletion was not permanent and the inode must
1075 * be moved to the hidden directory.
1078 hammer2_inode_unlink_finisher(hammer2_inode_t *ip, int isopen)
1086 * Decrement nlinks. If this is the last link and the file is
1087 * not open, the chain has already been removed and we don't bother
1088 * dirtying the inode.
1090 if (ip->meta.nlinks == 1) {
1091 atomic_set_int(&ip->flags, HAMMER2_INODE_ISUNLINKED);
1096 hammer2_inode_modify(ip);
1098 if ((int64_t)ip->meta.nlinks < 0)
1099 ip->meta.nlinks = 0; /* safety */
1102 * If nlinks is not zero we are done. However, this should only be
1103 * possible with a hardlink target. If the inode is an embedded
1104 * hardlink nlinks should have dropped to zero, warn and proceed
1105 * with the next step.
1107 if (ip->meta.nlinks) {
1108 if ((ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE) == 0)
1110 kprintf("hammer2_inode_unlink: nlinks was not 0 (%jd)\n",
1111 (intmax_t)ip->meta.nlinks);
1116 * nlinks is now zero, the inode should have already been deleted.
1117 * If the file is open it was deleted non-permanently and must be
1118 * moved to the hidden directory.
1120 * When moving to the hidden directory we force the name_key to the
1121 * inode number to avoid collisions.
1124 hammer2_inode_lock(pmp->ihidden, 0);
1125 error = hammer2_inode_connect(pmp->ihidden, ip,
1126 NULL, 0, ip->meta.inum);
1127 hammer2_inode_unlock(pmp->ihidden);
1135 * This is called from the mount code to initialize pmp->ihidden
1138 hammer2_inode_install_hidden(hammer2_pfs_t *pmp)
1145 hammer2_trans_init(pmp, 0);
1146 hammer2_inode_lock(pmp->iroot, 0);
1149 * Find the hidden directory
1152 hammer2_xop_lookup_t *xop;
1154 xop = hammer2_xop_alloc(pmp->iroot, HAMMER2_XOP_MODIFYING);
1155 xop->lhc = HAMMER2_INODE_HIDDENDIR;
1156 hammer2_xop_start(&xop->head, hammer2_xop_lookup);
1157 error = hammer2_xop_collect(&xop->head, 0);
1161 * Found the hidden directory
1163 kprintf("PFS FOUND HIDDEN DIR\n");
1164 pmp->ihidden = hammer2_inode_get(pmp, pmp->iroot,
1167 hammer2_inode_ref(pmp->ihidden);
1168 hammer2_inode_unlock(pmp->ihidden);
1170 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1174 * Create the hidden directory if it could not be found.
1176 if (error == ENOENT) {
1177 kprintf("PFS CREATE HIDDEN DIR\n");
1179 pmp->ihidden = hammer2_inode_create(pmp->iroot, NULL, NULL,
1181 /* lhc */ HAMMER2_INODE_HIDDENDIR,
1182 /* inum */ HAMMER2_INODE_HIDDENDIR,
1183 /* type */ HAMMER2_OBJTYPE_DIRECTORY,
1184 /* target_type */ 0,
1188 hammer2_inode_ref(pmp->ihidden);
1189 hammer2_inode_unlock(pmp->ihidden);
1192 kprintf("PFS CREATE ERROR %d\n", error);
1196 * Scan the hidden directory on-mount and destroy its contents
1199 hammer2_xop_unlinkall_t *xop;
1201 hammer2_inode_lock(pmp->ihidden, 0);
1202 xop = hammer2_xop_alloc(pmp->ihidden, HAMMER2_XOP_MODIFYING);
1203 xop->key_beg = HAMMER2_KEY_MIN;
1204 xop->key_end = HAMMER2_KEY_MAX;
1205 hammer2_xop_start(&xop->head, hammer2_inode_xop_unlinkall);
1207 while ((error = hammer2_xop_collect(&xop->head, 0)) == 0) {
1210 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1211 hammer2_inode_unlock(pmp->ihidden);
1214 hammer2_inode_unlock(pmp->iroot);
1215 hammer2_trans_done(pmp);
1219 * Find the directory common to both fdip and tdip.
1221 * Returns a held but not locked inode. Caller typically locks the inode,
1222 * and when through unlocks AND drops it.
1225 hammer2_inode_common_parent(hammer2_inode_t *fdip, hammer2_inode_t *tdip)
1227 hammer2_inode_t *scan1;
1228 hammer2_inode_t *scan2;
1231 * We used to have a depth field but it complicated matters too
1232 * much for directory renames. So now its ugly. Check for
1233 * simple cases before giving up and doing it the expensive way.
1235 * XXX need a bottom-up topology stability lock
1237 if (fdip == tdip || fdip == tdip->pip) {
1238 hammer2_inode_ref(fdip);
1241 if (fdip->pip == tdip) {
1242 hammer2_inode_ref(tdip);
1249 for (scan1 = fdip; scan1->pmp == fdip->pmp; scan1 = scan1->pip) {
1251 while (scan2->pmp == tdip->pmp) {
1252 if (scan1 == scan2) {
1253 hammer2_inode_ref(scan1);
1261 panic("hammer2_inode_common_parent: no common parent %p %p\n",
1268 * Mark an inode as being modified, meaning that the caller will modify
1271 * NOTE: No mtid (modify_tid) is passed into this routine. The caller is
1272 * only modifying the in-memory inode. A modify_tid is synchronized
1273 * later when the inode gets flushed.
1276 hammer2_inode_modify(hammer2_inode_t *ip)
1278 atomic_set_int(&ip->flags, HAMMER2_INODE_MODIFIED);
1280 vsetisdirty(ip->vp);
1284 * Synchronize the inode's frontend state with the chain state prior
1285 * to any explicit flush of the inode or any strategy write call.
1287 * Called with a locked inode inside a transaction.
1290 hammer2_inode_fsync(hammer2_inode_t *ip)
1292 if (ip->flags & (HAMMER2_INODE_RESIZED | HAMMER2_INODE_MODIFIED)) {
1293 hammer2_xop_fsync_t *xop;
1296 xop = hammer2_xop_alloc(ip, HAMMER2_XOP_MODIFYING);
1297 xop->clear_directdata = 0;
1298 if (ip->flags & HAMMER2_INODE_RESIZED) {
1299 if ((ip->meta.op_flags & HAMMER2_OPFLAG_DIRECTDATA) &&
1300 ip->meta.size > HAMMER2_EMBEDDED_BYTES) {
1301 ip->meta.op_flags &= ~HAMMER2_OPFLAG_DIRECTDATA;
1302 xop->clear_directdata = 1;
1304 xop->osize = ip->osize;
1306 xop->osize = ip->meta.size; /* safety */
1308 xop->ipflags = ip->flags;
1309 xop->meta = ip->meta;
1311 atomic_clear_int(&ip->flags, HAMMER2_INODE_RESIZED |
1312 HAMMER2_INODE_MODIFIED);
1313 hammer2_xop_start(&xop->head, hammer2_inode_xop_fsync);
1314 error = hammer2_xop_collect(&xop->head, 0);
1315 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1316 if (error == ENOENT)
1319 kprintf("hammer2: unable to fsync inode %p\n", ip);
1321 atomic_set_int(&ip->flags,
1322 xop->ipflags & (HAMMER2_INODE_RESIZED |
1323 HAMMER2_INODE_MODIFIED));
1325 /* XXX return error somehow? */
1331 * This handles unlinked open files after the vnode is finally dereferenced.
1332 * To avoid deadlocks it cannot be called from the normal vnode recycling
1333 * path, so we call it (1) after a unlink, rmdir, or rename, (2) on every
1334 * flush, and (3) on umount.
1336 * Caller must be in a transaction.
1339 hammer2_inode_run_unlinkq(hammer2_pfs_t *pmp)
1341 hammer2_xop_destroy_t *xop;
1342 hammer2_inode_unlink_t *ipul;
1343 hammer2_inode_t *ip;
1346 if (TAILQ_EMPTY(&pmp->unlinkq))
1350 hammer2_spin_ex(&pmp->list_spin);
1351 while ((ipul = TAILQ_FIRST(&pmp->unlinkq)) != NULL) {
1352 TAILQ_REMOVE(&pmp->unlinkq, ipul, entry);
1353 hammer2_spin_unex(&pmp->list_spin);
1355 kfree(ipul, pmp->minode);
1357 hammer2_inode_lock(ip, 0);
1358 xop = hammer2_xop_alloc(ip, HAMMER2_XOP_MODIFYING);
1359 hammer2_xop_start(&xop->head, hammer2_inode_xop_destroy);
1360 error = hammer2_xop_collect(&xop->head, 0);
1361 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1363 hammer2_inode_unlock(ip);
1364 hammer2_inode_drop(ip); /* ipul ref */
1366 hammer2_spin_ex(&pmp->list_spin);
1368 hammer2_spin_unex(&pmp->list_spin);
1373 * Inode create helper (threaded, backend)
1375 * Used by ncreate, nmknod, nsymlink, nmkdir.
1376 * Used by nlink and rename to create HARDLINK pointers.
1378 * Frontend holds the parent directory ip locked exclusively. We
1379 * create the inode and feed the exclusively locked chain to the
1383 hammer2_inode_xop_create(hammer2_xop_t *arg, int clindex)
1385 hammer2_xop_create_t *xop = &arg->xop_create;
1386 hammer2_chain_t *parent;
1387 hammer2_chain_t *chain;
1388 hammer2_key_t key_next;
1389 int cache_index = -1;
1392 kprintf("inode_create lhc %016jx clindex %d\n",
1396 parent = hammer2_inode_chain(xop->head.ip, clindex,
1397 HAMMER2_RESOLVE_ALWAYS);
1398 if (parent == NULL) {
1402 chain = hammer2_chain_lookup(&parent, &key_next,
1406 hammer2_chain_unlock(chain);
1411 error = hammer2_chain_create(&parent, &chain,
1414 HAMMER2_BREF_TYPE_INODE,
1415 HAMMER2_INODE_BYTES,
1416 xop->head.mtid, xop->flags);
1418 hammer2_chain_modify(chain, xop->head.mtid, 0);
1419 chain->data->ipdata.meta = xop->meta;
1420 if (xop->head.name) {
1421 bcopy(xop->head.name,
1422 chain->data->ipdata.filename,
1423 xop->head.name_len);
1424 chain->data->ipdata.meta.name_len = xop->head.name_len;
1426 chain->data->ipdata.meta.name_key = xop->lhc;
1428 hammer2_chain_unlock(chain);
1429 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS |
1430 HAMMER2_RESOLVE_SHARED);
1433 hammer2_chain_unlock(parent);
1434 hammer2_chain_drop(parent);
1436 hammer2_xop_feed(&xop->head, chain, clindex, error);
1438 hammer2_chain_drop(chain);
1442 * Inode delete helper (backend, threaded)
1444 * Generally used by hammer2_run_unlinkq()
1447 hammer2_inode_xop_destroy(hammer2_xop_t *arg, int clindex)
1449 hammer2_xop_destroy_t *xop = &arg->xop_destroy;
1451 hammer2_chain_t *parent;
1452 hammer2_chain_t *chain;
1453 hammer2_inode_t *ip;
1457 * We need the precise parent chain to issue the deletion.
1463 parent = hammer2_inode_chain(ip, clindex, HAMMER2_RESOLVE_ALWAYS);
1465 hammer2_chain_getparent(&parent, HAMMER2_RESOLVE_ALWAYS);
1466 if (parent == NULL) {
1470 chain = hammer2_inode_chain(ip, clindex, HAMMER2_RESOLVE_ALWAYS);
1471 if (chain == NULL) {
1475 hammer2_chain_delete(parent, chain, xop->head.mtid, 0);
1478 hammer2_xop_feed(&xop->head, NULL, clindex, error);
1480 hammer2_chain_unlock(parent);
1481 hammer2_chain_drop(parent);
1484 hammer2_chain_unlock(chain);
1485 hammer2_chain_drop(chain);
1490 hammer2_inode_xop_unlinkall(hammer2_xop_t *arg, int clindex)
1492 hammer2_xop_unlinkall_t *xop = &arg->xop_unlinkall;
1493 hammer2_chain_t *parent;
1494 hammer2_chain_t *chain;
1495 hammer2_key_t key_next;
1496 int cache_index = -1;
1499 * We need the precise parent chain to issue the deletion.
1501 parent = hammer2_inode_chain(xop->head.ip, clindex,
1502 HAMMER2_RESOLVE_ALWAYS);
1504 if (parent == NULL) {
1508 chain = hammer2_chain_lookup(&parent, &key_next,
1509 xop->key_beg, xop->key_end,
1511 HAMMER2_LOOKUP_ALWAYS);
1513 hammer2_chain_delete(parent, chain,
1514 xop->head.mtid, HAMMER2_DELETE_PERMANENT);
1515 hammer2_chain_unlock(chain);
1516 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS |
1517 HAMMER2_RESOLVE_SHARED);
1518 hammer2_xop_feed(&xop->head, chain, clindex, chain->error);
1519 chain = hammer2_chain_next(&parent, chain, &key_next,
1520 key_next, xop->key_end,
1522 HAMMER2_LOOKUP_ALWAYS |
1523 HAMMER2_LOOKUP_NOUNLOCK);
1526 hammer2_xop_feed(&xop->head, NULL, clindex, ENOENT);
1528 hammer2_chain_unlock(parent);
1529 hammer2_chain_drop(parent);
1532 hammer2_chain_unlock(chain);
1533 hammer2_chain_drop(chain);
1538 hammer2_inode_xop_connect(hammer2_xop_t *arg, int clindex)
1540 hammer2_xop_connect_t *xop = &arg->xop_connect;
1541 hammer2_inode_data_t *wipdata;
1542 hammer2_chain_t *parent;
1543 hammer2_chain_t *chain;
1545 hammer2_key_t key_dummy;
1546 int cache_index = -1;
1550 * Get directory, then issue a lookup to prime the parent chain
1551 * for the create. The lookup is expected to fail.
1553 pmp = xop->head.ip->pmp;
1554 parent = hammer2_inode_chain(xop->head.ip, clindex,
1555 HAMMER2_RESOLVE_ALWAYS);
1556 if (parent == NULL) {
1561 chain = hammer2_chain_lookup(&parent, &key_dummy,
1565 hammer2_chain_unlock(chain);
1566 hammer2_chain_drop(chain);
1573 * Adjust the filename in the inode, set the name key.
1575 * NOTE: Frontend must also adjust ip2->meta on success, we can't
1578 chain = hammer2_inode_chain(xop->head.ip2, clindex,
1579 HAMMER2_RESOLVE_ALWAYS);
1580 hammer2_chain_modify(chain, xop->head.mtid, 0);
1581 wipdata = &chain->data->ipdata;
1583 hammer2_inode_modify(xop->head.ip2);
1584 if (xop->head.name) {
1585 bzero(wipdata->filename, sizeof(wipdata->filename));
1586 bcopy(xop->head.name, wipdata->filename, xop->head.name_len);
1587 wipdata->meta.name_len = xop->head.name_len;
1589 wipdata->meta.name_key = xop->lhc;
1592 * Reconnect the chain to the new parent directory
1594 error = hammer2_chain_create(&parent, &chain, pmp,
1596 HAMMER2_BREF_TYPE_INODE,
1597 HAMMER2_INODE_BYTES,
1604 hammer2_xop_feed(&xop->head, NULL, clindex, error);
1606 hammer2_chain_unlock(parent);
1607 hammer2_chain_drop(parent);
1610 hammer2_chain_unlock(chain);
1611 hammer2_chain_drop(chain);
1616 hammer2_inode_xop_fsync(hammer2_xop_t *arg, int clindex)
1618 hammer2_xop_fsync_t *xop = &arg->xop_fsync;
1619 hammer2_chain_t *parent;
1620 hammer2_chain_t *chain;
1623 parent = hammer2_inode_chain(xop->head.ip, clindex,
1624 HAMMER2_RESOLVE_ALWAYS);
1626 if (parent == NULL) {
1630 if (parent->error) {
1631 error = parent->error;
1637 if ((xop->ipflags & HAMMER2_INODE_RESIZED) == 0) {
1638 /* osize must be ignored */
1639 } else if (xop->meta.size < xop->osize) {
1641 * We must delete any chains beyond the EOF. The chain
1642 * straddling the EOF will be pending in the bioq.
1644 hammer2_key_t lbase;
1645 hammer2_key_t key_next;
1646 int cache_index = -1;
1648 lbase = (xop->meta.size + HAMMER2_PBUFMASK64) &
1649 ~HAMMER2_PBUFMASK64;
1650 chain = hammer2_chain_lookup(&parent, &key_next,
1651 lbase, HAMMER2_KEY_MAX,
1653 HAMMER2_LOOKUP_NODATA |
1654 HAMMER2_LOOKUP_NODIRECT);
1657 * Degenerate embedded case, nothing to loop on
1659 switch (chain->bref.type) {
1660 case HAMMER2_BREF_TYPE_INODE:
1663 case HAMMER2_BREF_TYPE_DATA:
1664 hammer2_chain_delete(parent, chain,
1666 HAMMER2_DELETE_PERMANENT);
1669 chain = hammer2_chain_next(&parent, chain, &key_next,
1670 key_next, HAMMER2_KEY_MAX,
1672 HAMMER2_LOOKUP_NODATA |
1673 HAMMER2_LOOKUP_NODIRECT);
1678 * Sync the inode meta-data, potentially clear the blockset area
1679 * of direct data so it can be used for blockrefs.
1681 hammer2_chain_modify(parent, xop->head.mtid, 0);
1682 parent->data->ipdata.meta = xop->meta;
1683 if (xop->clear_directdata) {
1684 bzero(&parent->data->ipdata.u.blockset,
1685 sizeof(parent->data->ipdata.u.blockset));
1689 hammer2_chain_unlock(chain);
1690 hammer2_chain_drop(chain);
1693 hammer2_chain_unlock(parent);
1694 hammer2_chain_drop(parent);
1696 hammer2_xop_feed(&xop->head, NULL, clindex, error);