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)
61 hammer2_inode_delayed_sideq(hammer2_inode_t *ip)
63 hammer2_inode_sideq_t *ipul;
64 hammer2_pfs_t *pmp = ip->pmp;
66 if ((ip->flags & HAMMER2_INODE_ONSIDEQ) == 0) {
67 ipul = kmalloc(sizeof(*ipul), pmp->minode,
70 hammer2_spin_ex(&pmp->list_spin);
71 if ((ip->flags & HAMMER2_INODE_ONSIDEQ) == 0) {
72 hammer2_inode_ref(ip);
73 atomic_set_int(&ip->flags,
74 HAMMER2_INODE_ONSIDEQ);
75 TAILQ_INSERT_TAIL(&pmp->sideq, ipul, entry);
76 hammer2_spin_unex(&pmp->list_spin);
78 hammer2_spin_unex(&pmp->list_spin);
79 kfree(ipul, pmp->minode);
87 * HAMMER2 offers shared and exclusive locks on inodes. Pass a mask of
90 * - pass HAMMER2_RESOLVE_SHARED if a shared lock is desired. The
91 * inode locking function will automatically set the RDONLY flag.
93 * - pass HAMMER2_RESOLVE_ALWAYS if you need the inode's meta-data.
94 * Most front-end inode locks do.
96 * - pass HAMMER2_RESOLVE_NEVER if you do not want to require that
97 * the inode data be resolved. This is used by the syncthr because
98 * it can run on an unresolved/out-of-sync cluster, and also by the
99 * vnode reclamation code to avoid unnecessary I/O (particularly when
100 * disposing of hundreds of thousands of cached vnodes).
102 * The inode locking function locks the inode itself, resolves any stale
103 * chains in the inode's cluster, and allocates a fresh copy of the
104 * cluster with 1 ref and all the underlying chains locked.
106 * ip->cluster will be stable while the inode is locked.
108 * NOTE: We don't combine the inode/chain lock because putting away an
109 * inode would otherwise confuse multiple lock holders of the inode.
111 * NOTE: In-memory inodes always point to hardlink targets (the actual file),
112 * and never point to a hardlink pointer.
114 * NOTE: If caller passes HAMMER2_RESOLVE_RDONLY the exclusive locking code
115 * will feel free to reduce the chain set in the cluster as an
116 * optimization. It will still be validated against the quorum if
117 * appropriate, but the optimization might be able to reduce data
118 * accesses to one node. This flag is automatically set if the inode
119 * is locked with HAMMER2_RESOLVE_SHARED.
122 hammer2_inode_lock(hammer2_inode_t *ip, int how)
124 hammer2_inode_ref(ip);
127 * Inode structure mutex
129 if (how & HAMMER2_RESOLVE_SHARED) {
130 /*how |= HAMMER2_RESOLVE_RDONLY; not used */
131 hammer2_mtx_sh(&ip->lock);
133 hammer2_mtx_ex(&ip->lock);
138 * Select a chain out of an inode's cluster and lock it.
140 * The inode does not have to be locked.
143 hammer2_inode_chain(hammer2_inode_t *ip, int clindex, int how)
145 hammer2_chain_t *chain;
146 hammer2_cluster_t *cluster;
148 hammer2_spin_sh(&ip->cluster_spin);
150 cluster = ip->cluster_cache;
152 if (clindex >= cluster->nchains)
155 chain = cluster->array[clindex].chain;
157 hammer2_chain_ref(chain);
158 hammer2_spin_unsh(&ip->cluster_spin);
159 hammer2_chain_lock(chain, how);
165 cluster = &ip->cluster;
166 if (clindex >= cluster->nchains)
169 chain = cluster->array[clindex].chain;
171 hammer2_chain_ref(chain);
172 hammer2_spin_unsh(&ip->cluster_spin);
173 hammer2_chain_lock(chain, how);
175 hammer2_spin_unsh(&ip->cluster_spin);
181 hammer2_inode_chain_and_parent(hammer2_inode_t *ip, int clindex,
182 hammer2_chain_t **parentp, int how)
184 hammer2_chain_t *chain;
185 hammer2_chain_t *parent;
188 hammer2_spin_sh(&ip->cluster_spin);
189 if (clindex >= ip->cluster.nchains)
192 chain = ip->cluster.array[clindex].chain;
194 hammer2_chain_ref(chain);
195 hammer2_spin_unsh(&ip->cluster_spin);
196 hammer2_chain_lock(chain, how);
198 hammer2_spin_unsh(&ip->cluster_spin);
202 * Get parent, lock order must be (parent, chain).
204 parent = chain->parent;
205 hammer2_chain_ref(parent);
206 hammer2_chain_unlock(chain);
207 hammer2_chain_lock(parent, how);
208 hammer2_chain_lock(chain, how);
209 if (ip->cluster.array[clindex].chain == chain &&
210 chain->parent == parent) {
217 hammer2_chain_unlock(chain);
218 hammer2_chain_drop(chain);
219 hammer2_chain_unlock(parent);
220 hammer2_chain_drop(parent);
228 hammer2_inode_unlock(hammer2_inode_t *ip)
230 hammer2_mtx_unlock(&ip->lock);
231 hammer2_inode_drop(ip);
235 * Temporarily release a lock held shared or exclusive. Caller must
236 * hold the lock shared or exclusive on call and lock will be released
239 * Restore a lock that was temporarily released.
242 hammer2_inode_lock_temp_release(hammer2_inode_t *ip)
244 return hammer2_mtx_temp_release(&ip->lock);
248 hammer2_inode_lock_temp_restore(hammer2_inode_t *ip, hammer2_mtx_state_t ostate)
250 hammer2_mtx_temp_restore(&ip->lock, ostate);
254 * Upgrade a shared inode lock to exclusive and return. If the inode lock
255 * is already held exclusively this is a NOP.
257 * The caller MUST hold the inode lock either shared or exclusive on call
258 * and will own the lock exclusively on return.
260 * Returns non-zero if the lock was already exclusive prior to the upgrade.
263 hammer2_inode_lock_upgrade(hammer2_inode_t *ip)
267 if (mtx_islocked_ex(&ip->lock)) {
270 hammer2_mtx_unlock(&ip->lock);
271 hammer2_mtx_ex(&ip->lock);
278 * Downgrade an inode lock from exclusive to shared only if the inode
279 * lock was previously shared. If the inode lock was previously exclusive,
283 hammer2_inode_lock_downgrade(hammer2_inode_t *ip, int wasexclusive)
285 if (wasexclusive == 0)
286 mtx_downgrade(&ip->lock);
290 * Lookup an inode by inode number
293 hammer2_inode_lookup(hammer2_pfs_t *pmp, hammer2_tid_t inum)
301 hammer2_spin_ex(&pmp->inum_spin);
302 ip = RB_LOOKUP(hammer2_inode_tree, &pmp->inum_tree, inum);
304 hammer2_inode_ref(ip);
305 hammer2_spin_unex(&pmp->inum_spin);
311 * Adding a ref to an inode is only legal if the inode already has at least
314 * (can be called with spinlock held)
317 hammer2_inode_ref(hammer2_inode_t *ip)
319 atomic_add_int(&ip->refs, 1);
320 if (hammer2_debug & 0x80000) {
321 kprintf("INODE+1 %p (%d->%d)\n", ip, ip->refs - 1, ip->refs);
327 * Drop an inode reference, freeing the inode when the last reference goes
331 hammer2_inode_drop(hammer2_inode_t *ip)
334 hammer2_inode_t *pip;
338 if (hammer2_debug & 0x80000) {
339 kprintf("INODE-1 %p (%d->%d)\n",
340 ip, ip->refs, ip->refs - 1);
347 * Transition to zero, must interlock with
348 * the inode inumber lookup tree (if applicable).
349 * It should not be possible for anyone to race
350 * the transition to 0.
354 hammer2_spin_ex(&pmp->inum_spin);
356 if (atomic_cmpset_int(&ip->refs, 1, 0)) {
357 KKASSERT(hammer2_mtx_refs(&ip->lock) == 0);
358 if (ip->flags & HAMMER2_INODE_ONRBTREE) {
359 atomic_clear_int(&ip->flags,
360 HAMMER2_INODE_ONRBTREE);
361 RB_REMOVE(hammer2_inode_tree,
362 &pmp->inum_tree, ip);
364 hammer2_spin_unex(&pmp->inum_spin);
372 * Clean out the cluster cache
374 hammer2_cluster_t *tmpclu;
375 tmpclu = ip->cluster_cache;
377 ip->cluster_cache = NULL;
378 hammer2_cluster_drop(tmpclu);
383 * Cleaning out ip->cluster isn't entirely
386 hammer2_inode_repoint(ip, NULL, NULL);
389 * We have to drop pip (if non-NULL) to
390 * dispose of our implied reference from
391 * ip->pip. We can simply loop on it.
393 kfree(ip, pmp->minode);
394 atomic_add_long(&pmp->inmem_inodes, -1);
396 /* continue with pip (can be NULL) */
398 hammer2_spin_unex(&ip->pmp->inum_spin);
402 * Non zero transition
404 if (atomic_cmpset_int(&ip->refs, refs, refs - 1))
411 * Get the vnode associated with the given inode, allocating the vnode if
412 * necessary. The vnode will be returned exclusively locked.
414 * The caller must lock the inode (shared or exclusive).
416 * Great care must be taken to avoid deadlocks and vnode acquisition/reclaim
420 hammer2_igetv(hammer2_inode_t *ip, int *errorp)
426 KKASSERT(pmp != NULL);
431 * Attempt to reuse an existing vnode assignment. It is
432 * possible to race a reclaim so the vget() may fail. The
433 * inode must be unlocked during the vget() to avoid a
434 * deadlock against a reclaim.
441 * Inode must be unlocked during the vget() to avoid
442 * possible deadlocks, but leave the ip ref intact.
444 * vnode is held to prevent destruction during the
445 * vget(). The vget() can still fail if we lost
446 * a reclaim race on the vnode.
448 hammer2_mtx_state_t ostate;
451 ostate = hammer2_inode_lock_temp_release(ip);
452 if (vget(vp, LK_EXCLUSIVE)) {
454 hammer2_inode_lock_temp_restore(ip, ostate);
457 hammer2_inode_lock_temp_restore(ip, ostate);
459 /* vp still locked and ref from vget */
461 kprintf("hammer2: igetv race %p/%p\n",
471 * No vnode exists, allocate a new vnode. Beware of
472 * allocation races. This function will return an
473 * exclusively locked and referenced vnode.
475 *errorp = getnewvnode(VT_HAMMER2, pmp->mp, &vp, 0, 0);
477 kprintf("hammer2: igetv getnewvnode failed %d\n",
484 * Lock the inode and check for an allocation race.
486 wasexclusive = hammer2_inode_lock_upgrade(ip);
487 if (ip->vp != NULL) {
490 hammer2_inode_lock_downgrade(ip, wasexclusive);
494 switch (ip->meta.type) {
495 case HAMMER2_OBJTYPE_DIRECTORY:
498 case HAMMER2_OBJTYPE_REGFILE:
500 vinitvmio(vp, ip->meta.size,
502 (int)ip->meta.size & HAMMER2_LBUFMASK);
504 case HAMMER2_OBJTYPE_SOFTLINK:
506 * XXX for now we are using the generic file_read
507 * and file_write code so we need a buffer cache
511 vinitvmio(vp, ip->meta.size,
513 (int)ip->meta.size & HAMMER2_LBUFMASK);
515 case HAMMER2_OBJTYPE_CDEV:
518 case HAMMER2_OBJTYPE_BDEV:
519 vp->v_ops = &pmp->mp->mnt_vn_spec_ops;
520 if (ip->meta.type != HAMMER2_OBJTYPE_CDEV)
526 case HAMMER2_OBJTYPE_FIFO:
528 vp->v_ops = &pmp->mp->mnt_vn_fifo_ops;
530 case HAMMER2_OBJTYPE_SOCKET:
534 panic("hammer2: unhandled objtype %d",
539 if (ip == pmp->iroot)
540 vsetflags(vp, VROOT);
544 hammer2_inode_ref(ip); /* vp association */
545 hammer2_inode_lock_downgrade(ip, wasexclusive);
550 * Return non-NULL vp and *errorp == 0, or NULL vp and *errorp != 0.
552 if (hammer2_debug & 0x0002) {
553 kprintf("igetv vp %p refs 0x%08x aux 0x%08x\n",
554 vp, vp->v_refcnt, vp->v_auxrefs);
560 * Returns the inode associated with the passed-in cluster, creating the
561 * inode if necessary and synchronizing it to the passed-in cluster otherwise.
562 * When synchronizing, if idx >= 0, only cluster index (idx) is synchronized.
563 * Otherwise the whole cluster is synchronized.
565 * The passed-in cluster must be locked and will remain locked on return.
566 * The returned inode will be locked and the caller may dispose of both
567 * via hammer2_inode_unlock() + hammer2_inode_drop(). However, if the caller
568 * needs to resolve a hardlink it must ref/unlock/relock/drop the inode.
570 * The hammer2_inode structure regulates the interface between the high level
571 * kernel VNOPS API and the filesystem backend (the chains).
573 * On return the inode is locked with the supplied cluster.
576 hammer2_inode_get(hammer2_pfs_t *pmp, hammer2_inode_t *dip,
577 hammer2_cluster_t *cluster, int idx)
579 hammer2_inode_t *nip;
580 const hammer2_inode_data_t *iptmp;
581 const hammer2_inode_data_t *nipdata;
583 KKASSERT(cluster == NULL ||
584 hammer2_cluster_type(cluster) == HAMMER2_BREF_TYPE_INODE);
588 * Interlocked lookup/ref of the inode. This code is only needed
589 * when looking up inodes with nlinks != 0 (TODO: optimize out
590 * otherwise and test for duplicates).
592 * Cluster can be NULL during the initial pfs allocation.
596 iptmp = &hammer2_cluster_rdata(cluster)->ipdata;
597 nip = hammer2_inode_lookup(pmp, iptmp->meta.inum);
601 hammer2_mtx_ex(&nip->lock);
604 * Handle SMP race (not applicable to the super-root spmp
605 * which can't index inodes due to duplicative inode numbers).
607 if (pmp->spmp_hmp == NULL &&
608 (nip->flags & HAMMER2_INODE_ONRBTREE) == 0) {
609 hammer2_mtx_unlock(&nip->lock);
610 hammer2_inode_drop(nip);
614 hammer2_inode_repoint_one(nip, cluster, idx);
616 hammer2_inode_repoint(nip, NULL, cluster);
622 * We couldn't find the inode number, create a new inode.
624 nip = kmalloc(sizeof(*nip), pmp->minode, M_WAITOK | M_ZERO);
625 spin_init(&nip->cluster_spin, "h2clspin");
626 atomic_add_long(&pmp->inmem_inodes, 1);
627 hammer2_pfs_memory_inc(pmp);
628 hammer2_pfs_memory_wakeup(pmp);
630 nip->flags = HAMMER2_INODE_SROOT;
633 * Initialize nip's cluster. A cluster is provided for normal
634 * inodes but typically not for the super-root or PFS inodes.
636 nip->cluster.refs = 1;
637 nip->cluster.pmp = pmp;
638 nip->cluster.flags |= HAMMER2_CLUSTER_INODE;
640 nipdata = &hammer2_cluster_rdata(cluster)->ipdata;
641 nip->meta = nipdata->meta;
642 atomic_set_int(&nip->flags, HAMMER2_INODE_METAGOOD);
643 hammer2_inode_repoint(nip, NULL, cluster);
645 nip->meta.inum = 1; /* PFS inum is always 1 XXX */
646 /* mtime will be updated when a cluster is available */
647 atomic_set_int(&nip->flags, HAMMER2_INODE_METAGOOD);/*XXX*/
650 nip->pip = dip; /* can be NULL */
652 hammer2_inode_ref(dip); /* ref dip for nip->pip */
657 * ref and lock on nip gives it state compatible to after a
658 * hammer2_inode_lock() call.
661 hammer2_mtx_init(&nip->lock, "h2inode");
662 hammer2_mtx_ex(&nip->lock);
663 /* combination of thread lock and chain lock == inode lock */
666 * Attempt to add the inode. If it fails we raced another inode
667 * get. Undo all the work and try again.
669 if (pmp->spmp_hmp == NULL) {
670 hammer2_spin_ex(&pmp->inum_spin);
671 if (RB_INSERT(hammer2_inode_tree, &pmp->inum_tree, nip)) {
672 hammer2_spin_unex(&pmp->inum_spin);
673 hammer2_mtx_unlock(&nip->lock);
674 hammer2_inode_drop(nip);
677 atomic_set_int(&nip->flags, HAMMER2_INODE_ONRBTREE);
678 hammer2_spin_unex(&pmp->inum_spin);
685 * Resolve the parent inode for ip using ip->meta.iparent.
687 * Called with locked inode as argument
690 hammer2_inode_resolve_pip(hammer2_inode_t *ip)
692 hammer2_pfs_t *pmp = ip->pmp;
693 hammer2_inode_t *pip;
694 hammer2_xop_lookup_t *xop;
698 while (ip->pip == NULL && ip != pmp->iroot &&
699 ip->meta.iparent != 0) {
700 pip = hammer2_inode_lookup(ip->pmp, ip->meta.iparent);
702 ip->pip = pip; /* transfer ref to ip->pip */
705 xop = hammer2_xop_alloc(pmp->iroot, 0);
706 xop->lhc = ip->meta.iparent;
707 hammer2_xop_start(&xop->head, hammer2_xop_lookup);
708 error = hammer2_xop_collect(&xop->head, 0);
711 kprintf("hammer2_inode_resolve_pip: "
712 "cannot find %016jx\n",
714 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
717 kprintf("NFS load inum %016jx iparent %016jx\n",
718 ip->meta.inum, ip->meta.iparent);
719 pip = hammer2_inode_get(pmp, NULL, &xop->head.cluster, -1);
720 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
723 hammer2_inode_ref(pip);
725 kprintf("Unable to load inode!\n");
729 hammer2_inode_unlock(ip);
734 hammer2_inode_unlock(ip);
738 * Create a new inode in the specified directory using the vattr to
739 * figure out the type. A non-zero type field overrides vattr.
741 * If no error occurs the new inode with its cluster locked is returned.
742 * However, when creating an OBJTYPE_HARDLINK, the caller can assume
743 * that NULL will be returned (that is, the caller already has the inode
744 * in-hand and is creating a hardlink to it, we do not need to return a
745 * representitive ip).
747 * If vap and/or cred are NULL the related fields are not set and the
748 * inode type defaults to a directory. This is used when creating PFSs
749 * under the super-root, so the inode number is set to 1 in this case.
751 * dip is not locked on entry.
753 * NOTE: This function is used to create all manners of inodes, including
754 * super-root entries for snapshots and PFSs. When used to create a
755 * snapshot the inode will be temporarily associated with the spmp.
757 * NOTE: When creating a normal file or directory the caller must call this
758 * function twice, once to create the actual inode and once to create
759 * the hardlink representing the directory entry. This function is
760 * only called once when creating a softlink. The softlink itself.
762 * NOTE: When creating a hardlink target (a real inode), name/name_len is
763 * passed as NULL/0, and caller should pass lhc as inum.
766 hammer2_inode_create(hammer2_inode_t *dip,
767 struct vattr *vap, struct ucred *cred,
768 const uint8_t *name, size_t name_len, hammer2_key_t lhc,
769 hammer2_key_t inum, uint8_t type, uint8_t target_type,
770 int flags, int *errorp)
772 hammer2_xop_create_t *xop;
773 hammer2_inode_t *nip;
779 uint8_t dip_comp_algo;
780 uint8_t dip_check_algo;
783 lhc = hammer2_dirhash(name, name_len);
788 * Locate the inode or indirect block to create the new
789 * entry in. At the same time check for key collisions
790 * and iterate until we don't get one.
792 * NOTE: hidden inodes do not have iterators.
794 * Lock the directory exclusively for now to guarantee that
795 * we can find an unused lhc for the name. Due to collisions,
796 * two different creates can end up with the same lhc so we
797 * cannot depend on the OS to prevent the collision.
799 hammer2_inode_lock(dip, 0);
801 dip_uid = dip->meta.uid;
802 dip_gid = dip->meta.gid;
803 dip_mode = dip->meta.mode;
804 dip_comp_algo = dip->meta.comp_algo;
805 dip_check_algo = dip->meta.check_algo;
808 * If name specified, locate an unused key in the collision space.
809 * Otherwise use the passed-in lhc directly.
812 hammer2_xop_scanlhc_t *sxop;
813 hammer2_key_t lhcbase;
816 sxop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
818 hammer2_xop_start(&sxop->head, hammer2_xop_scanlhc);
819 while ((error = hammer2_xop_collect(&sxop->head, 0)) == 0) {
820 if (lhc != sxop->head.cluster.focus->bref.key)
824 hammer2_xop_retire(&sxop->head, HAMMER2_XOPMASK_VOP);
832 if ((lhcbase ^ lhc) & ~HAMMER2_DIRHASH_LOMASK) {
839 * Create the inode with the lhc as the key.
841 xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
844 bzero(&xop->meta, sizeof(xop->meta));
847 xop->meta.type = hammer2_get_obj_type(vap->va_type);
849 switch (xop->meta.type) {
850 case HAMMER2_OBJTYPE_CDEV:
851 case HAMMER2_OBJTYPE_BDEV:
852 xop->meta.rmajor = vap->va_rmajor;
853 xop->meta.rminor = vap->va_rminor;
858 type = xop->meta.type;
860 xop->meta.type = type;
861 xop->meta.target_type = target_type;
863 xop->meta.inum = inum;
864 xop->meta.iparent = dip->meta.inum;
866 /* Inherit parent's inode compression mode. */
867 xop->meta.comp_algo = dip_comp_algo;
868 xop->meta.check_algo = dip_check_algo;
869 xop->meta.version = HAMMER2_INODE_VERSION_ONE;
870 hammer2_update_time(&xop->meta.ctime);
871 xop->meta.mtime = xop->meta.ctime;
873 xop->meta.mode = vap->va_mode;
874 xop->meta.nlinks = 1;
876 if (dip && dip->pmp) {
877 xuid = hammer2_to_unix_xid(&dip_uid);
878 xuid = vop_helper_create_uid(dip->pmp->mp,
884 /* super-root has no dip and/or pmp */
887 if (vap->va_vaflags & VA_UID_UUID_VALID)
888 xop->meta.uid = vap->va_uid_uuid;
889 else if (vap->va_uid != (uid_t)VNOVAL)
890 hammer2_guid_to_uuid(&xop->meta.uid, vap->va_uid);
892 hammer2_guid_to_uuid(&xop->meta.uid, xuid);
894 if (vap->va_vaflags & VA_GID_UUID_VALID)
895 xop->meta.gid = vap->va_gid_uuid;
896 else if (vap->va_gid != (gid_t)VNOVAL)
897 hammer2_guid_to_uuid(&xop->meta.gid, vap->va_gid);
899 xop->meta.gid = dip_gid;
903 * Regular files and softlinks allow a small amount of data to be
904 * directly embedded in the inode. This flag will be cleared if
905 * the size is extended past the embedded limit.
907 if (xop->meta.type == HAMMER2_OBJTYPE_REGFILE ||
908 xop->meta.type == HAMMER2_OBJTYPE_SOFTLINK ||
909 xop->meta.type == HAMMER2_OBJTYPE_HARDLINK) {
910 xop->meta.op_flags |= HAMMER2_OPFLAG_DIRECTDATA;
913 hammer2_xop_setname(&xop->head, name, name_len);
915 name_len = hammer2_xop_setname_inum(&xop->head, inum);
916 KKASSERT(lhc == inum);
918 xop->meta.name_len = name_len;
919 xop->meta.name_key = lhc;
920 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
922 hammer2_xop_start(&xop->head, hammer2_inode_xop_create);
924 error = hammer2_xop_collect(&xop->head, 0);
926 kprintf("CREATE INODE %*.*s\n",
927 (int)name_len, (int)name_len, name);
936 * Set up the new inode if not a hardlink pointer.
938 * NOTE: *_get() integrates chain's lock into the inode lock.
940 * NOTE: Only one new inode can currently be created per
941 * transaction. If the need arises we can adjust
942 * hammer2_trans_init() to allow more.
944 * NOTE: nipdata will have chain's blockset data.
946 if (type != HAMMER2_OBJTYPE_HARDLINK) {
947 nip = hammer2_inode_get(dip->pmp, dip, &xop->head.cluster, -1);
948 nip->comp_heuristic = 0;
954 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
956 hammer2_inode_unlock(dip);
962 * Connect the disconnected inode (ip) to the directory (dip) with the
963 * specified (name, name_len). If name is NULL, (lhc) will be used as
964 * the directory key and the inode's embedded name will not be modified
965 * for future recovery purposes.
967 * dip and ip must both be locked exclusively (dip in particular to avoid
971 hammer2_inode_connect(hammer2_inode_t *dip, hammer2_inode_t *ip,
972 const char *name, size_t name_len,
975 hammer2_xop_scanlhc_t *sxop;
976 hammer2_xop_connect_t *xop;
977 hammer2_inode_t *opip;
978 hammer2_key_t lhcbase;
982 * Calculate the lhc and resolve the collision space.
985 lhc = lhcbase = hammer2_dirhash(name, name_len);
986 sxop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
988 hammer2_xop_start(&sxop->head, hammer2_xop_scanlhc);
989 while ((error = hammer2_xop_collect(&sxop->head, 0)) == 0) {
990 if (lhc != sxop->head.cluster.focus->bref.key)
994 hammer2_xop_retire(&sxop->head, HAMMER2_XOPMASK_VOP);
1002 if ((lhcbase ^ lhc) & ~HAMMER2_DIRHASH_LOMASK) {
1011 * Formally reconnect the in-memory structure. ip must
1012 * be locked exclusively to safely change ip->pip.
1014 if (ip->pip != dip) {
1015 hammer2_inode_ref(dip);
1019 hammer2_inode_drop(opip);
1025 xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
1027 hammer2_xop_setname(&xop->head, name, name_len);
1028 hammer2_xop_setip2(&xop->head, ip);
1030 hammer2_xop_start(&xop->head, hammer2_inode_xop_connect);
1031 error = hammer2_xop_collect(&xop->head, 0);
1032 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1035 * On success make the same adjustments to ip->meta or the
1036 * next flush may blow up the chain.
1039 hammer2_inode_modify(ip);
1040 ip->meta.name_key = lhc;
1042 ip->meta.name_len = name_len;
1049 * Repoint ip->cluster's chains to cluster's chains and fixup the default
1050 * focus. All items, valid or invalid, are repointed. hammer2_xop_start()
1051 * filters out invalid or non-matching elements.
1053 * Caller must hold the inode and cluster exclusive locked, if not NULL,
1054 * must also be locked.
1056 * Cluster may be NULL to clean out any chains in ip->cluster.
1059 hammer2_inode_repoint(hammer2_inode_t *ip, hammer2_inode_t *pip,
1060 hammer2_cluster_t *cluster)
1062 hammer2_chain_t *dropch[HAMMER2_MAXCLUSTER];
1063 hammer2_chain_t *ochain;
1064 hammer2_chain_t *nchain;
1065 hammer2_inode_t *opip;
1068 bzero(dropch, sizeof(dropch));
1071 * Replace chains in ip->cluster with chains from cluster and
1072 * adjust the focus if necessary.
1074 * NOTE: nchain and/or ochain can be NULL due to gaps
1075 * in the cluster arrays.
1077 hammer2_spin_ex(&ip->cluster_spin);
1078 for (i = 0; cluster && i < cluster->nchains; ++i) {
1080 * Do not replace elements which are the same. Also handle
1081 * element count discrepancies.
1083 nchain = cluster->array[i].chain;
1084 if (i < ip->cluster.nchains) {
1085 ochain = ip->cluster.array[i].chain;
1086 if (ochain == nchain)
1095 ip->cluster.array[i].chain = nchain;
1096 ip->cluster.array[i].flags &= ~HAMMER2_CITEM_INVALID;
1097 ip->cluster.array[i].flags |= cluster->array[i].flags &
1098 HAMMER2_CITEM_INVALID;
1100 hammer2_chain_ref(nchain);
1105 * Release any left-over chains in ip->cluster.
1107 while (i < ip->cluster.nchains) {
1108 nchain = ip->cluster.array[i].chain;
1110 ip->cluster.array[i].chain = NULL;
1111 ip->cluster.array[i].flags |= HAMMER2_CITEM_INVALID;
1118 * Fixup fields. Note that the inode-embedded cluster is never
1122 ip->cluster.nchains = cluster->nchains;
1123 ip->cluster.focus = cluster->focus;
1124 ip->cluster.flags = cluster->flags & ~HAMMER2_CLUSTER_LOCKED;
1126 ip->cluster.nchains = 0;
1127 ip->cluster.focus = NULL;
1128 ip->cluster.flags &= ~HAMMER2_CLUSTER_ZFLAGS;
1132 * Repoint ip->pip if requested (non-NULL pip).
1134 if (pip && ip->pip != pip) {
1136 hammer2_inode_ref(pip);
1141 hammer2_spin_unex(&ip->cluster_spin);
1144 * Cleanup outside of spinlock
1148 hammer2_chain_drop(dropch[i]);
1151 hammer2_inode_drop(opip);
1155 * Repoint a single element from the cluster to the ip. Used by the
1156 * synchronization threads to piecemeal update inodes. Does not change
1157 * focus and requires inode to be re-locked to clean-up flags (XXX).
1160 hammer2_inode_repoint_one(hammer2_inode_t *ip, hammer2_cluster_t *cluster,
1163 hammer2_chain_t *ochain;
1164 hammer2_chain_t *nchain;
1167 hammer2_spin_ex(&ip->cluster_spin);
1168 KKASSERT(idx < cluster->nchains);
1169 if (idx < ip->cluster.nchains) {
1170 ochain = ip->cluster.array[idx].chain;
1171 nchain = cluster->array[idx].chain;
1174 nchain = cluster->array[idx].chain;
1175 ip->cluster.nchains = idx + 1;
1176 for (i = ip->cluster.nchains; i <= idx; ++i) {
1177 bzero(&ip->cluster.array[i],
1178 sizeof(ip->cluster.array[i]));
1179 ip->cluster.array[i].flags |= HAMMER2_CITEM_INVALID;
1182 if (ochain != nchain) {
1186 ip->cluster.array[idx].chain = nchain;
1187 ip->cluster.array[idx].flags &= ~HAMMER2_CITEM_INVALID;
1188 ip->cluster.array[idx].flags |= cluster->array[idx].flags &
1189 HAMMER2_CITEM_INVALID;
1191 hammer2_spin_unex(&ip->cluster_spin);
1192 if (ochain != nchain) {
1194 hammer2_chain_ref(nchain);
1196 hammer2_chain_drop(ochain);
1201 * Called with a locked inode to finish unlinking an inode after xop_unlink
1202 * had been run. This function is responsible for decrementing nlinks and
1203 * moving deleted inodes to the hidden directory if they are still open.
1205 * We don't bother decrementing nlinks if the file is not open and this was
1208 * If the inode is a hardlink target it's chain has not yet been deleted,
1209 * otherwise it's chain has been deleted.
1211 * If isopen then any prior deletion was not permanent and the inode must
1212 * be moved to the hidden directory.
1215 hammer2_inode_unlink_finisher(hammer2_inode_t *ip, int isopen)
1223 * Decrement nlinks. If this is the last link and the file is
1224 * not open, the chain has already been removed and we don't bother
1225 * dirtying the inode.
1227 if (ip->meta.nlinks == 1) {
1228 atomic_set_int(&ip->flags, HAMMER2_INODE_ISUNLINKED);
1230 atomic_set_int(&ip->flags, HAMMER2_INODE_ISDELETED);
1235 hammer2_inode_modify(ip);
1237 if ((int64_t)ip->meta.nlinks < 0)
1238 ip->meta.nlinks = 0; /* safety */
1241 * If nlinks is not zero we are done. However, this should only be
1242 * possible with a hardlink target. If the inode is an embedded
1243 * hardlink nlinks should have dropped to zero, warn and proceed
1244 * with the next step.
1246 if (ip->meta.nlinks) {
1247 if ((ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE) == 0)
1249 kprintf("hammer2_inode_unlink: nlinks was not 0 (%jd)\n",
1250 (intmax_t)ip->meta.nlinks);
1255 * nlinks is now zero, the inode should have already been deleted.
1256 * If the file is open it was deleted non-permanently and must be
1257 * moved to the hidden directory.
1259 * When moving to the hidden directory we force the name_key to the
1260 * inode number to avoid collisions.
1263 hammer2_inode_lock(pmp->ihidden, 0);
1264 error = hammer2_inode_connect(pmp->ihidden, ip,
1265 NULL, 0, ip->meta.inum);
1266 hammer2_inode_unlock(pmp->ihidden);
1274 * This is called from the mount code to initialize pmp->ihidden
1277 hammer2_inode_install_hidden(hammer2_pfs_t *pmp)
1284 hammer2_trans_init(pmp, 0);
1285 hammer2_inode_lock(pmp->iroot, 0);
1288 * Find the hidden directory
1291 hammer2_xop_lookup_t *xop;
1293 xop = hammer2_xop_alloc(pmp->iroot, HAMMER2_XOP_MODIFYING);
1294 xop->lhc = HAMMER2_INODE_HIDDENDIR;
1295 hammer2_xop_start(&xop->head, hammer2_xop_lookup);
1296 error = hammer2_xop_collect(&xop->head, 0);
1300 * Found the hidden directory
1302 kprintf("PFS FOUND HIDDEN DIR\n");
1303 pmp->ihidden = hammer2_inode_get(pmp, pmp->iroot,
1306 hammer2_inode_ref(pmp->ihidden);
1307 hammer2_inode_unlock(pmp->ihidden);
1309 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1313 * Create the hidden directory if it could not be found.
1315 if (error == ENOENT) {
1316 kprintf("PFS CREATE HIDDEN DIR\n");
1318 pmp->ihidden = hammer2_inode_create(pmp->iroot, NULL, NULL,
1320 /* lhc */ HAMMER2_INODE_HIDDENDIR,
1321 /* inum */ HAMMER2_INODE_HIDDENDIR,
1322 /* type */ HAMMER2_OBJTYPE_DIRECTORY,
1323 /* target_type */ 0,
1327 hammer2_inode_ref(pmp->ihidden);
1328 hammer2_inode_unlock(pmp->ihidden);
1331 kprintf("PFS CREATE ERROR %d\n", error);
1335 * Scan the hidden directory on-mount and destroy its contents
1338 hammer2_xop_unlinkall_t *xop;
1340 hammer2_inode_lock(pmp->ihidden, 0);
1341 xop = hammer2_xop_alloc(pmp->ihidden, HAMMER2_XOP_MODIFYING);
1342 xop->key_beg = HAMMER2_KEY_MIN;
1343 xop->key_end = HAMMER2_KEY_MAX;
1344 hammer2_xop_start(&xop->head, hammer2_inode_xop_unlinkall);
1346 while ((error = hammer2_xop_collect(&xop->head, 0)) == 0) {
1349 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1350 hammer2_inode_unlock(pmp->ihidden);
1353 hammer2_inode_unlock(pmp->iroot);
1354 hammer2_trans_done(pmp);
1359 * REMOVED - No longer applicable now that we are indexing inodes under
1362 * Find the directory common to both fdip and tdip that satisfies the
1363 * conditions. The common directory is not allowed to cross a XLINK
1364 * boundary. If ishardlink is non-zero and we successfully find the
1365 * common parent, we will continue to iterate parents until we hit a
1368 * Returns a held but not locked inode. Caller typically locks the inode,
1369 * and when through unlocks AND drops it.
1372 hammer2_inode_common_parent(hammer2_inode_t *fdip, hammer2_inode_t *tdip,
1373 int *errorp, int ishardlink)
1375 hammer2_inode_t *scan1;
1376 hammer2_inode_t *scan2;
1380 * We used to have a depth field but it complicated matters too
1381 * much for directory renames. So now its ugly. Check for
1382 * simple cases before giving up and doing it the expensive way.
1384 * XXX need a bottom-up topology stability lock
1387 hammer2_inode_ref(fdip);
1394 * state: -1 sub-scan failed
1396 * +1 sub-scan succeeded (find xlink boundary if rename)
1398 for (scan1 = fdip; scan1->pmp == fdip->pmp; scan1 = scan1->pip) {
1401 while (scan2->pmp == tdip->pmp) {
1402 if (state == 0 && scan1 == scan2) {
1404 * Found common parent, stop here on rename,
1405 * continue if creating a hardlink.
1407 if (ishardlink == 0) {
1408 hammer2_inode_ref(scan1);
1415 * Search for XLINK boundary when hardlink.
1417 if ((scan2->meta.uflags &
1418 (SF_XLINK | UF_XLINK)) ||
1419 scan2->pip == NULL ||
1420 scan2->pip->pmp != scan1->pmp) {
1421 hammer2_inode_ref(scan2);
1425 if (scan2->meta.uflags & (SF_XLINK | UF_XLINK))
1431 if (scan1->meta.uflags & (SF_XLINK | UF_XLINK))
1440 * Mark an inode as being modified, meaning that the caller will modify
1443 * If a vnode is present we set the vnode dirty and the nominal filesystem
1444 * sync will also handle synchronizing the inode meta-data. If no vnode
1445 * is present we must ensure that the inode is on pmp->sideq.
1447 * NOTE: No mtid (modify_tid) is passed into this routine. The caller is
1448 * only modifying the in-memory inode. A modify_tid is synchronized
1449 * later when the inode gets flushed.
1452 hammer2_inode_modify(hammer2_inode_t *ip)
1456 atomic_set_int(&ip->flags, HAMMER2_INODE_MODIFIED);
1458 vsetisdirty(ip->vp);
1459 } else if ((pmp = ip->pmp) != NULL) {
1460 hammer2_inode_delayed_sideq(ip);
1465 * Synchronize the inode's frontend state with the chain state prior
1466 * to any explicit flush of the inode or any strategy write call.
1468 * Called with a locked inode inside a transaction.
1471 hammer2_inode_chain_sync(hammer2_inode_t *ip)
1473 if (ip->flags & (HAMMER2_INODE_RESIZED | HAMMER2_INODE_MODIFIED)) {
1474 hammer2_xop_fsync_t *xop;
1477 xop = hammer2_xop_alloc(ip, HAMMER2_XOP_MODIFYING);
1478 xop->clear_directdata = 0;
1479 if (ip->flags & HAMMER2_INODE_RESIZED) {
1480 if ((ip->meta.op_flags & HAMMER2_OPFLAG_DIRECTDATA) &&
1481 ip->meta.size > HAMMER2_EMBEDDED_BYTES) {
1482 ip->meta.op_flags &= ~HAMMER2_OPFLAG_DIRECTDATA;
1483 xop->clear_directdata = 1;
1485 xop->osize = ip->osize;
1487 xop->osize = ip->meta.size; /* safety */
1489 xop->ipflags = ip->flags;
1490 xop->meta = ip->meta;
1492 atomic_clear_int(&ip->flags, HAMMER2_INODE_RESIZED |
1493 HAMMER2_INODE_MODIFIED);
1494 hammer2_xop_start(&xop->head, hammer2_inode_xop_chain_sync);
1495 error = hammer2_xop_collect(&xop->head, 0);
1496 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1497 if (error == ENOENT)
1500 kprintf("hammer2: unable to fsync inode %p\n", ip);
1502 atomic_set_int(&ip->flags,
1503 xop->ipflags & (HAMMER2_INODE_RESIZED |
1504 HAMMER2_INODE_MODIFIED));
1506 /* XXX return error somehow? */
1512 * The normal filesystem sync no longer has visibility to an inode structure
1513 * after its vnode has been reclaimed. In this situation an unlinked-but-open
1514 * inode or a dirty inode may require additional processing to synchronize
1515 * ip->meta to its underlying cluster nodes.
1517 * In particular, reclaims can occur in almost any state (for example, when
1518 * doing operations on unrelated vnodes) and flushing the reclaimed inode
1519 * in the reclaim path itself is a non-starter.
1521 * Caller must be in a transaction.
1524 hammer2_inode_run_sideq(hammer2_pfs_t *pmp)
1526 hammer2_xop_destroy_t *xop;
1527 hammer2_inode_sideq_t *ipul;
1528 hammer2_inode_t *ip;
1531 if (TAILQ_EMPTY(&pmp->sideq))
1535 hammer2_spin_ex(&pmp->list_spin);
1536 while ((ipul = TAILQ_FIRST(&pmp->sideq)) != NULL) {
1537 TAILQ_REMOVE(&pmp->sideq, ipul, entry);
1539 KKASSERT(ip->flags & HAMMER2_INODE_ONSIDEQ);
1540 atomic_clear_int(&ip->flags, HAMMER2_INODE_ONSIDEQ);
1541 hammer2_spin_unex(&pmp->list_spin);
1542 kfree(ipul, pmp->minode);
1544 hammer2_inode_lock(ip, 0);
1545 if (ip->flags & HAMMER2_INODE_ISUNLINKED) {
1547 * The inode was unlinked while open, causing H2
1548 * to relink it to a hidden directory to allow
1549 * cluster operations to continue until close.
1551 * The inode must be deleted and destroyed.
1553 xop = hammer2_xop_alloc(ip, HAMMER2_XOP_MODIFYING);
1554 hammer2_xop_start(&xop->head,
1555 hammer2_inode_xop_destroy);
1556 error = hammer2_xop_collect(&xop->head, 0);
1557 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1559 atomic_clear_int(&ip->flags, HAMMER2_INODE_ISDELETED);
1562 * The inode was dirty as-of the reclaim, requiring
1563 * synchronization of ip->meta with its underlying
1566 hammer2_inode_chain_sync(ip);
1569 hammer2_inode_unlock(ip);
1570 hammer2_inode_drop(ip); /* ipul ref */
1572 hammer2_spin_ex(&pmp->list_spin);
1574 hammer2_spin_unex(&pmp->list_spin);
1579 * Inode create helper (threaded, backend)
1581 * Used by ncreate, nmknod, nsymlink, nmkdir.
1582 * Used by nlink and rename to create HARDLINK pointers.
1584 * Frontend holds the parent directory ip locked exclusively. We
1585 * create the inode and feed the exclusively locked chain to the
1589 hammer2_inode_xop_create(hammer2_xop_t *arg, int clindex)
1591 hammer2_xop_create_t *xop = &arg->xop_create;
1592 hammer2_chain_t *parent;
1593 hammer2_chain_t *chain;
1594 hammer2_key_t key_next;
1595 int cache_index = -1;
1598 if (hammer2_debug & 0x0001)
1599 kprintf("inode_create lhc %016jx clindex %d\n",
1602 parent = hammer2_inode_chain(xop->head.ip1, clindex,
1603 HAMMER2_RESOLVE_ALWAYS);
1604 if (parent == NULL) {
1609 chain = hammer2_chain_lookup(&parent, &key_next,
1617 error = hammer2_chain_create(&parent, &chain,
1620 HAMMER2_BREF_TYPE_INODE,
1621 HAMMER2_INODE_BYTES,
1622 xop->head.mtid, 0, xop->flags);
1624 hammer2_chain_modify(chain, xop->head.mtid, 0, 0);
1625 chain->data->ipdata.meta = xop->meta;
1626 if (xop->head.name1) {
1627 bcopy(xop->head.name1,
1628 chain->data->ipdata.filename,
1629 xop->head.name1_len);
1630 chain->data->ipdata.meta.name_len = xop->head.name1_len;
1632 chain->data->ipdata.meta.name_key = xop->lhc;
1636 hammer2_chain_unlock(parent);
1637 hammer2_chain_drop(parent);
1639 hammer2_xop_feed(&xop->head, chain, clindex, error);
1641 hammer2_chain_unlock(chain);
1642 hammer2_chain_drop(chain);
1647 * Inode delete helper (backend, threaded)
1649 * Generally used by hammer2_run_sideq()
1652 hammer2_inode_xop_destroy(hammer2_xop_t *arg, int clindex)
1654 hammer2_xop_destroy_t *xop = &arg->xop_destroy;
1656 hammer2_chain_t *parent;
1657 hammer2_chain_t *chain;
1658 hammer2_inode_t *ip;
1662 * We need the precise parent chain to issue the deletion.
1668 parent = hammer2_inode_chain(ip, clindex, HAMMER2_RESOLVE_ALWAYS);
1670 hammer2_chain_getparent(&parent, HAMMER2_RESOLVE_ALWAYS);
1671 if (parent == NULL) {
1675 chain = hammer2_inode_chain(ip, clindex, HAMMER2_RESOLVE_ALWAYS);
1676 if (chain == NULL) {
1680 hammer2_chain_delete(parent, chain, xop->head.mtid, 0);
1683 hammer2_xop_feed(&xop->head, NULL, clindex, error);
1685 hammer2_chain_unlock(parent);
1686 hammer2_chain_drop(parent);
1689 hammer2_chain_unlock(chain);
1690 hammer2_chain_drop(chain);
1695 hammer2_inode_xop_unlinkall(hammer2_xop_t *arg, int clindex)
1697 hammer2_xop_unlinkall_t *xop = &arg->xop_unlinkall;
1698 hammer2_chain_t *parent;
1699 hammer2_chain_t *chain;
1700 hammer2_key_t key_next;
1701 int cache_index = -1;
1704 * We need the precise parent chain to issue the deletion.
1706 parent = hammer2_inode_chain(xop->head.ip1, clindex,
1707 HAMMER2_RESOLVE_ALWAYS);
1709 if (parent == NULL) {
1713 chain = hammer2_chain_lookup(&parent, &key_next,
1714 xop->key_beg, xop->key_end,
1716 HAMMER2_LOOKUP_ALWAYS);
1718 hammer2_chain_delete(parent, chain,
1719 xop->head.mtid, HAMMER2_DELETE_PERMANENT);
1720 hammer2_xop_feed(&xop->head, chain, clindex, chain->error);
1721 /* depend on function to unlock the shared lock */
1722 chain = hammer2_chain_next(&parent, chain, &key_next,
1723 key_next, xop->key_end,
1725 HAMMER2_LOOKUP_ALWAYS);
1728 hammer2_xop_feed(&xop->head, NULL, clindex, ENOENT);
1730 hammer2_chain_unlock(parent);
1731 hammer2_chain_drop(parent);
1734 hammer2_chain_unlock(chain);
1735 hammer2_chain_drop(chain);
1740 hammer2_inode_xop_connect(hammer2_xop_t *arg, int clindex)
1742 hammer2_xop_connect_t *xop = &arg->xop_connect;
1743 hammer2_inode_data_t *wipdata;
1744 hammer2_chain_t *parent;
1745 hammer2_chain_t *chain;
1747 hammer2_key_t key_dummy;
1748 int cache_index = -1;
1752 * Get directory, then issue a lookup to prime the parent chain
1753 * for the create. The lookup is expected to fail.
1755 pmp = xop->head.ip1->pmp;
1756 parent = hammer2_inode_chain(xop->head.ip1, clindex,
1757 HAMMER2_RESOLVE_ALWAYS);
1758 if (parent == NULL) {
1763 chain = hammer2_chain_lookup(&parent, &key_dummy,
1767 hammer2_chain_unlock(chain);
1768 hammer2_chain_drop(chain);
1775 * Adjust the filename in the inode, set the name key.
1777 * NOTE: Frontend must also adjust ip2->meta on success, we can't
1780 chain = hammer2_inode_chain(xop->head.ip2, clindex,
1781 HAMMER2_RESOLVE_ALWAYS);
1782 hammer2_chain_modify(chain, xop->head.mtid, 0, 0);
1783 wipdata = &chain->data->ipdata;
1785 hammer2_inode_modify(xop->head.ip2);
1786 if (xop->head.name1) {
1787 bzero(wipdata->filename, sizeof(wipdata->filename));
1788 bcopy(xop->head.name1, wipdata->filename, xop->head.name1_len);
1789 wipdata->meta.name_len = xop->head.name1_len;
1791 wipdata->meta.name_key = xop->lhc;
1794 * Reconnect the chain to the new parent directory
1796 error = hammer2_chain_create(&parent, &chain, pmp,
1798 HAMMER2_BREF_TYPE_INODE,
1799 HAMMER2_INODE_BYTES,
1800 xop->head.mtid, 0, 0);
1806 hammer2_xop_feed(&xop->head, NULL, clindex, error);
1808 hammer2_chain_unlock(parent);
1809 hammer2_chain_drop(parent);
1812 hammer2_chain_unlock(chain);
1813 hammer2_chain_drop(chain);
1818 * Synchronize the in-memory inode with the chain.
1821 hammer2_inode_xop_chain_sync(hammer2_xop_t *arg, int clindex)
1823 hammer2_xop_fsync_t *xop = &arg->xop_fsync;
1824 hammer2_chain_t *parent;
1825 hammer2_chain_t *chain;
1828 parent = hammer2_inode_chain(xop->head.ip1, clindex,
1829 HAMMER2_RESOLVE_ALWAYS);
1831 if (parent == NULL) {
1835 if (parent->error) {
1836 error = parent->error;
1842 if ((xop->ipflags & HAMMER2_INODE_RESIZED) == 0) {
1843 /* osize must be ignored */
1844 } else if (xop->meta.size < xop->osize) {
1846 * We must delete any chains beyond the EOF. The chain
1847 * straddling the EOF will be pending in the bioq.
1849 hammer2_key_t lbase;
1850 hammer2_key_t key_next;
1851 int cache_index = -1;
1853 lbase = (xop->meta.size + HAMMER2_PBUFMASK64) &
1854 ~HAMMER2_PBUFMASK64;
1855 chain = hammer2_chain_lookup(&parent, &key_next,
1856 lbase, HAMMER2_KEY_MAX,
1858 HAMMER2_LOOKUP_NODATA |
1859 HAMMER2_LOOKUP_NODIRECT);
1862 * Degenerate embedded case, nothing to loop on
1864 switch (chain->bref.type) {
1865 case HAMMER2_BREF_TYPE_INODE:
1868 case HAMMER2_BREF_TYPE_DATA:
1869 hammer2_chain_delete(parent, chain,
1871 HAMMER2_DELETE_PERMANENT);
1874 chain = hammer2_chain_next(&parent, chain, &key_next,
1875 key_next, HAMMER2_KEY_MAX,
1877 HAMMER2_LOOKUP_NODATA |
1878 HAMMER2_LOOKUP_NODIRECT);
1882 * Reset to point at inode for following code, if necessary.
1884 if (parent->bref.type != HAMMER2_BREF_TYPE_INODE) {
1885 hammer2_chain_unlock(parent);
1886 hammer2_chain_drop(parent);
1887 parent = hammer2_inode_chain(xop->head.ip1, clindex,
1888 HAMMER2_RESOLVE_ALWAYS);
1889 kprintf("hammer2: TRUNCATE RESET on '%s'\n",
1890 parent->data->ipdata.filename);
1895 * Sync the inode meta-data, potentially clear the blockset area
1896 * of direct data so it can be used for blockrefs.
1898 hammer2_chain_modify(parent, xop->head.mtid, 0, 0);
1899 parent->data->ipdata.meta = xop->meta;
1900 if (xop->clear_directdata) {
1901 bzero(&parent->data->ipdata.u.blockset,
1902 sizeof(parent->data->ipdata.u.blockset));
1906 hammer2_chain_unlock(chain);
1907 hammer2_chain_drop(chain);
1910 hammer2_chain_unlock(parent);
1911 hammer2_chain_drop(parent);
1913 hammer2_xop_feed(&xop->head, NULL, clindex, error);