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 static void hammer2_inode_move_to_hidden(hammer2_trans_t *trans,
47 hammer2_cluster_t **clusterp,
50 RB_GENERATE2(hammer2_inode_tree, hammer2_inode, rbnode, hammer2_inode_cmp,
54 hammer2_inode_cmp(hammer2_inode_t *ip1, hammer2_inode_t *ip2)
56 if (ip1->inum < ip2->inum)
58 if (ip1->inum > ip2->inum)
66 * HAMMER2 offers shared locks and exclusive locks on inodes.
68 * The inode locking function locks the inode itself, resolves any stale
69 * chains in the inode's cluster, and allocates a fresh copy of the
70 * cluster with 1 ref and all the underlying chains locked. Duplication
71 * races are handled by this function.
73 * ip->cluster will be stable while the inode is locked.
75 * NOTE: We don't combine the inode/chain lock because putting away an
76 * inode would otherwise confuse multiple lock holders of the inode.
78 * NOTE: Hardlinks are followed in the returned cluster but not in the
79 * inode's internal cluster (ip->cluster).
82 hammer2_inode_lock_ex(hammer2_inode_t *ip)
84 const hammer2_inode_data_t *ipdata;
85 hammer2_cluster_t *cluster;
86 hammer2_chain_t *chain;
87 hammer2_chain_t *ochain;
88 hammer2_chain_core_t *core;
92 hammer2_inode_ref(ip);
93 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
94 cluster = hammer2_cluster_copy(&ip->cluster, 0);
96 ip->cluster.focus = NULL;
97 cluster->focus = NULL;
99 for (i = 0; i < cluster->nchains; ++i) {
100 chain = ip->cluster.array[i];
103 if (chain->flags & HAMMER2_CHAIN_DUPLICATED) {
104 spin_lock(&core->cst.spin);
105 while (chain->flags & HAMMER2_CHAIN_DUPLICATED)
106 chain = TAILQ_NEXT(chain, core_entry);
107 hammer2_chain_ref(chain);
108 spin_unlock(&core->cst.spin);
109 ochain = ip->cluster.array[i];
110 ip->cluster.array[i] = chain;
111 if (ip->cluster.focus == NULL)
112 ip->cluster.focus = chain;
113 hammer2_chain_drop(ochain);
115 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS);
116 if ((chain->flags & HAMMER2_CHAIN_DUPLICATED) == 0)
118 hammer2_chain_unlock(chain);
120 cluster->array[i] = chain;
121 if (cluster->focus == NULL)
122 cluster->focus = chain;
123 if (ip->cluster.focus == NULL)
124 ip->cluster.focus = chain;
128 * Returned cluster must resolve hardlink pointers
130 ipdata = &hammer2_cluster_data(cluster)->ipdata;
131 if (ipdata->type == HAMMER2_OBJTYPE_HARDLINK &&
132 (cluster->focus->flags & HAMMER2_CHAIN_DELETED) == 0) {
133 error = hammer2_hardlink_find(ip->pip, cluster);
134 KKASSERT((cluster->focus->flags &
135 HAMMER2_CHAIN_DUPLICATED) == 0);
136 KKASSERT(error == 0);
143 hammer2_inode_unlock_ex(hammer2_inode_t *ip, hammer2_cluster_t *cluster)
146 hammer2_cluster_unlock(cluster);
147 ccms_thread_unlock(&ip->topo_cst);
148 hammer2_inode_drop(ip);
152 * NOTE: We don't combine the inode/chain lock because putting away an
153 * inode would otherwise confuse multiple lock holders of the inode.
155 * Shared locks are especially sensitive to having too many shared
156 * lock counts (from the same thread) on certain paths which might
157 * need to upgrade them. Only one count of a shared lock can be
161 hammer2_inode_lock_sh(hammer2_inode_t *ip)
163 const hammer2_inode_data_t *ipdata;
164 hammer2_cluster_t *cluster;
165 hammer2_chain_core_t *core;
166 hammer2_chain_t *chain;
170 hammer2_inode_ref(ip);
171 cluster = hammer2_cluster_copy(&ip->cluster, 0);
172 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_SHARED);
174 cluster->focus = NULL;
176 for (i = 0; i < cluster->nchains; ++i) {
177 chain = ip->cluster.array[i];
180 if (chain->flags & HAMMER2_CHAIN_DUPLICATED)
182 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS |
183 HAMMER2_RESOLVE_SHARED);
184 if (chain->flags & HAMMER2_CHAIN_DUPLICATED) {
185 hammer2_chain_unlock(chain);
188 * Cycle exclusive inode lock and start the loop
193 chain = cluster->array[i];
194 cluster->array[i] = NULL;
195 hammer2_chain_unlock(chain);
197 ccms_thread_unlock(&ip->topo_cst);
198 hammer2_inode_unlock_ex(ip, hammer2_inode_lock_ex(ip));
199 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_SHARED);
200 cluster->focus = NULL;
201 continue; /* restart at i=-1 -> i=0 on loop */
203 cluster->array[i] = chain;
204 if (cluster->focus == NULL)
205 cluster->focus = chain;
209 * Returned cluster must resolve hardlink pointers
211 ipdata = &hammer2_cluster_data(cluster)->ipdata;
212 if (ipdata->type == HAMMER2_OBJTYPE_HARDLINK &&
213 (cluster->focus->flags & HAMMER2_CHAIN_DELETED) == 0) {
214 error = hammer2_hardlink_find(ip->pip, cluster);
215 KKASSERT((cluster->focus->flags &
216 HAMMER2_CHAIN_DUPLICATED) == 0);
217 KKASSERT(error == 0);
224 hammer2_inode_unlock_sh(hammer2_inode_t *ip, hammer2_cluster_t *cluster)
227 hammer2_cluster_unlock(cluster);
228 ccms_thread_unlock(&ip->topo_cst);
229 hammer2_inode_drop(ip);
233 hammer2_inode_lock_temp_release(hammer2_inode_t *ip)
235 return(ccms_thread_lock_temp_release(&ip->topo_cst));
239 hammer2_inode_lock_temp_restore(hammer2_inode_t *ip, ccms_state_t ostate)
241 ccms_thread_lock_temp_restore(&ip->topo_cst, ostate);
245 hammer2_inode_lock_upgrade(hammer2_inode_t *ip)
247 return(ccms_thread_lock_upgrade(&ip->topo_cst));
251 hammer2_inode_lock_downgrade(hammer2_inode_t *ip, ccms_state_t ostate)
253 ccms_thread_lock_downgrade(&ip->topo_cst, ostate);
257 * Lookup an inode by inode number
260 hammer2_inode_lookup(hammer2_pfsmount_t *pmp, hammer2_tid_t inum)
268 spin_lock(&pmp->inum_spin);
269 ip = RB_LOOKUP(hammer2_inode_tree, &pmp->inum_tree, inum);
271 hammer2_inode_ref(ip);
272 spin_unlock(&pmp->inum_spin);
278 * Adding a ref to an inode is only legal if the inode already has at least
282 hammer2_inode_ref(hammer2_inode_t *ip)
284 atomic_add_int(&ip->refs, 1);
288 * Drop an inode reference, freeing the inode when the last reference goes
292 hammer2_inode_drop(hammer2_inode_t *ip)
294 hammer2_pfsmount_t *pmp;
295 hammer2_inode_t *pip;
303 * Transition to zero, must interlock with
304 * the inode inumber lookup tree (if applicable).
308 spin_lock(&pmp->inum_spin);
310 if (atomic_cmpset_int(&ip->refs, 1, 0)) {
311 KKASSERT(ip->topo_cst.count == 0);
312 if (ip->flags & HAMMER2_INODE_ONRBTREE) {
313 atomic_clear_int(&ip->flags,
314 HAMMER2_INODE_ONRBTREE);
315 RB_REMOVE(hammer2_inode_tree,
316 &pmp->inum_tree, ip);
318 spin_unlock(&pmp->inum_spin);
325 * Cleaning out ip->cluster isn't entirely
328 hammer2_inode_repoint(ip, NULL, NULL);
331 * We have to drop pip (if non-NULL) to
332 * dispose of our implied reference from
333 * ip->pip. We can simply loop on it.
335 kfree(ip, pmp->minode);
336 atomic_add_long(&pmp->inmem_inodes, -1);
338 /* continue with pip (can be NULL) */
340 spin_unlock(&ip->pmp->inum_spin);
344 * Non zero transition
346 if (atomic_cmpset_int(&ip->refs, refs, refs - 1))
353 * Get the vnode associated with the given inode, allocating the vnode if
354 * necessary. The vnode will be returned exclusively locked.
356 * The caller must lock the inode (shared or exclusive).
358 * Great care must be taken to avoid deadlocks and vnode acquisition/reclaim
362 hammer2_igetv(hammer2_inode_t *ip, hammer2_cluster_t *cparent, int *errorp)
364 const hammer2_inode_data_t *ipdata;
365 hammer2_pfsmount_t *pmp;
370 KKASSERT(pmp != NULL);
373 ipdata = &hammer2_cluster_data(cparent)->ipdata;
377 * Attempt to reuse an existing vnode assignment. It is
378 * possible to race a reclaim so the vget() may fail. The
379 * inode must be unlocked during the vget() to avoid a
380 * deadlock against a reclaim.
385 * Inode must be unlocked during the vget() to avoid
386 * possible deadlocks, but leave the ip ref intact.
388 * vnode is held to prevent destruction during the
389 * vget(). The vget() can still fail if we lost
390 * a reclaim race on the vnode.
393 ostate = hammer2_inode_lock_temp_release(ip);
394 if (vget(vp, LK_EXCLUSIVE)) {
396 hammer2_inode_lock_temp_restore(ip, ostate);
399 hammer2_inode_lock_temp_restore(ip, ostate);
401 /* vp still locked and ref from vget */
403 kprintf("hammer2: igetv race %p/%p\n",
413 * No vnode exists, allocate a new vnode. Beware of
414 * allocation races. This function will return an
415 * exclusively locked and referenced vnode.
417 *errorp = getnewvnode(VT_HAMMER2, pmp->mp, &vp, 0, 0);
419 kprintf("hammer2: igetv getnewvnode failed %d\n",
426 * Lock the inode and check for an allocation race.
428 ostate = hammer2_inode_lock_upgrade(ip);
429 if (ip->vp != NULL) {
432 hammer2_inode_lock_downgrade(ip, ostate);
436 switch (ipdata->type) {
437 case HAMMER2_OBJTYPE_DIRECTORY:
440 case HAMMER2_OBJTYPE_REGFILE:
442 vinitvmio(vp, ipdata->size,
444 (int)ipdata->size & HAMMER2_LBUFMASK);
446 case HAMMER2_OBJTYPE_SOFTLINK:
448 * XXX for now we are using the generic file_read
449 * and file_write code so we need a buffer cache
453 vinitvmio(vp, ipdata->size,
455 (int)ipdata->size & HAMMER2_LBUFMASK);
457 case HAMMER2_OBJTYPE_CDEV:
460 case HAMMER2_OBJTYPE_BDEV:
461 vp->v_ops = &pmp->mp->mnt_vn_spec_ops;
462 if (ipdata->type != HAMMER2_OBJTYPE_CDEV)
464 addaliasu(vp, ipdata->rmajor, ipdata->rminor);
466 case HAMMER2_OBJTYPE_FIFO:
468 vp->v_ops = &pmp->mp->mnt_vn_fifo_ops;
471 panic("hammer2: unhandled objtype %d", ipdata->type);
475 if (ip == pmp->iroot)
476 vsetflags(vp, VROOT);
480 hammer2_inode_ref(ip); /* vp association */
481 hammer2_inode_lock_downgrade(ip, ostate);
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.
499 * The passed-in chain must be locked and will remain locked on return.
500 * The returned inode will be locked and the caller may dispose of both
501 * via hammer2_inode_unlock_ex(). However, if the caller needs to resolve
502 * a hardlink it must ref/unlock/relock/drop the inode.
504 * The hammer2_inode structure regulates the interface between the high level
505 * kernel VNOPS API and the filesystem backend (the chains).
508 hammer2_inode_get(hammer2_pfsmount_t *pmp, hammer2_inode_t *dip,
509 hammer2_cluster_t *cluster)
511 hammer2_inode_t *nip;
512 const hammer2_inode_data_t *iptmp;
513 const hammer2_inode_data_t *nipdata;
515 KKASSERT(hammer2_cluster_type(cluster) == HAMMER2_BREF_TYPE_INODE);
519 * Interlocked lookup/ref of the inode. This code is only needed
520 * when looking up inodes with nlinks != 0 (TODO: optimize out
521 * otherwise and test for duplicates).
525 iptmp = &hammer2_cluster_data(cluster)->ipdata;
526 nip = hammer2_inode_lookup(pmp, iptmp->inum);
530 ccms_thread_lock(&nip->topo_cst, CCMS_STATE_EXCLUSIVE);
533 * Handle SMP race (not applicable to the super-root spmp
534 * which can't index inodes due to duplicative inode numbers).
536 if (pmp->spmp_hmp == NULL &&
537 (nip->flags & HAMMER2_INODE_ONRBTREE) == 0) {
538 ccms_thread_unlock(&nip->topo_cst);
539 hammer2_inode_drop(nip);
542 hammer2_inode_repoint(nip, NULL, cluster);
547 * We couldn't find the inode number, create a new inode.
549 nip = kmalloc(sizeof(*nip), pmp->minode, M_WAITOK | M_ZERO);
550 atomic_add_long(&pmp->inmem_inodes, 1);
551 hammer2_pfs_memory_inc(pmp);
552 hammer2_pfs_memory_wakeup(pmp);
554 nip->flags = HAMMER2_INODE_SROOT;
557 * Initialize nip's cluster
559 nip->cluster.refs = 1;
560 nip->cluster.pmp = pmp;
561 nip->cluster.flags |= HAMMER2_CLUSTER_INODE;
562 hammer2_cluster_replace(&nip->cluster, cluster);
564 nipdata = &hammer2_cluster_data(cluster)->ipdata;
565 nip->inum = nipdata->inum;
566 nip->size = nipdata->size;
567 nip->mtime = nipdata->mtime;
568 hammer2_inode_repoint(nip, NULL, cluster);
569 nip->pip = dip; /* can be NULL */
571 hammer2_inode_ref(dip); /* ref dip for nip->pip */
576 * ref and lock on nip gives it state compatible to after a
577 * hammer2_inode_lock_ex() call.
580 ccms_cst_init(&nip->topo_cst, &nip->cluster);
581 ccms_thread_lock(&nip->topo_cst, CCMS_STATE_EXCLUSIVE);
582 /* combination of thread lock and chain lock == inode lock */
585 * Attempt to add the inode. If it fails we raced another inode
586 * get. Undo all the work and try again.
588 if (pmp->spmp_hmp == NULL) {
589 spin_lock(&pmp->inum_spin);
590 if (RB_INSERT(hammer2_inode_tree, &pmp->inum_tree, nip)) {
591 spin_unlock(&pmp->inum_spin);
592 ccms_thread_unlock(&nip->topo_cst);
593 hammer2_inode_drop(nip);
596 atomic_set_int(&nip->flags, HAMMER2_INODE_ONRBTREE);
597 spin_unlock(&pmp->inum_spin);
604 * Create a new inode in the specified directory using the vattr to
605 * figure out the type of inode.
607 * If no error occurs the new inode with its cluster locked is returned in
608 * *nipp, otherwise an error is returned and *nipp is set to NULL.
610 * If vap and/or cred are NULL the related fields are not set and the
611 * inode type defaults to a directory. This is used when creating PFSs
612 * under the super-root, so the inode number is set to 1 in this case.
614 * dip is not locked on entry.
616 * NOTE: When used to create a snapshot, the inode is temporarily associated
617 * with the super-root spmp. XXX should pass new pmp for snapshot.
620 hammer2_inode_create(hammer2_trans_t *trans, hammer2_inode_t *dip,
621 struct vattr *vap, struct ucred *cred,
622 const uint8_t *name, size_t name_len,
623 hammer2_cluster_t **clusterp, int *errorp)
625 const hammer2_inode_data_t *dipdata;
626 hammer2_inode_data_t *nipdata;
627 hammer2_cluster_t *cluster;
628 hammer2_cluster_t *cparent;
629 hammer2_inode_t *nip;
630 hammer2_key_t key_dummy;
640 lhc = hammer2_dirhash(name, name_len);
644 * Locate the inode or indirect block to create the new
645 * entry in. At the same time check for key collisions
646 * and iterate until we don't get one.
648 * NOTE: hidden inodes do not have iterators.
651 cparent = hammer2_inode_lock_ex(dip);
652 dipdata = &hammer2_cluster_data(cparent)->ipdata;
653 dip_uid = dipdata->uid;
654 dip_gid = dipdata->gid;
655 dip_mode = dipdata->mode;
656 dip_algo = dipdata->comp_algo;
660 cluster = hammer2_cluster_lookup(cparent, &key_dummy,
661 lhc, lhc, 0, &ddflag);
664 if ((lhc & HAMMER2_DIRHASH_VISIBLE) == 0)
666 if ((lhc & HAMMER2_DIRHASH_LOMASK) == HAMMER2_DIRHASH_LOMASK)
668 hammer2_cluster_unlock(cluster);
674 error = hammer2_cluster_create(trans, cparent, &cluster,
676 HAMMER2_BREF_TYPE_INODE,
677 HAMMER2_INODE_BYTES);
680 kprintf("CREATE INODE %*.*s chain=%p\n",
681 (int)name_len, (int)name_len, name,
682 (cluster ? cluster->focus : NULL));
686 * Cleanup and handle retries.
688 if (error == EAGAIN) {
689 hammer2_cluster_ref(cparent);
690 hammer2_inode_unlock_ex(dip, cparent);
691 hammer2_cluster_wait(cparent);
692 hammer2_cluster_drop(cparent);
695 hammer2_inode_unlock_ex(dip, cparent);
699 KKASSERT(cluster == NULL);
705 * Set up the new inode.
707 * NOTE: *_get() integrates chain's lock into the inode lock.
709 * NOTE: Only one new inode can currently be created per
710 * transaction. If the need arises we can adjust
711 * hammer2_trans_init() to allow more.
713 * NOTE: nipdata will have chain's blockset data.
715 KKASSERT(cluster->focus->flags & HAMMER2_CHAIN_MODIFIED);
716 nipdata = &hammer2_cluster_wdata(cluster)->ipdata;
717 nipdata->inum = trans->inode_tid;
718 nip = hammer2_inode_get(dip->pmp, dip, cluster);
719 nipdata = &hammer2_cluster_wdata(cluster)->ipdata;
722 KKASSERT(trans->inodes_created == 0);
723 nipdata->type = hammer2_get_obj_type(vap->va_type);
724 nipdata->inum = trans->inode_tid;
725 ++trans->inodes_created;
727 switch (nipdata->type) {
728 case HAMMER2_OBJTYPE_CDEV:
729 case HAMMER2_OBJTYPE_BDEV:
730 nipdata->rmajor = vap->va_rmajor;
731 nipdata->rminor = vap->va_rminor;
737 nipdata->type = HAMMER2_OBJTYPE_DIRECTORY;
741 /* Inherit parent's inode compression mode. */
742 nip->comp_heuristic = 0;
743 nipdata->comp_algo = dip_algo;
744 nipdata->version = HAMMER2_INODE_VERSION_ONE;
745 hammer2_update_time(&nipdata->ctime);
746 nipdata->mtime = nipdata->ctime;
748 nipdata->mode = vap->va_mode;
751 if (dip && dip->pmp) {
752 xuid = hammer2_to_unix_xid(&dip_uid);
753 xuid = vop_helper_create_uid(dip->pmp->mp,
759 /* super-root has no dip and/or pmp */
762 if (vap->va_vaflags & VA_UID_UUID_VALID)
763 nipdata->uid = vap->va_uid_uuid;
764 else if (vap->va_uid != (uid_t)VNOVAL)
765 hammer2_guid_to_uuid(&nipdata->uid, vap->va_uid);
767 hammer2_guid_to_uuid(&nipdata->uid, xuid);
769 if (vap->va_vaflags & VA_GID_UUID_VALID)
770 nipdata->gid = vap->va_gid_uuid;
771 else if (vap->va_gid != (gid_t)VNOVAL)
772 hammer2_guid_to_uuid(&nipdata->gid, vap->va_gid);
774 nipdata->gid = dip_gid;
778 * Regular files and softlinks allow a small amount of data to be
779 * directly embedded in the inode. This flag will be cleared if
780 * the size is extended past the embedded limit.
782 if (nipdata->type == HAMMER2_OBJTYPE_REGFILE ||
783 nipdata->type == HAMMER2_OBJTYPE_SOFTLINK) {
784 nipdata->op_flags |= HAMMER2_OPFLAG_DIRECTDATA;
787 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
788 bcopy(name, nipdata->filename, name_len);
789 nipdata->name_key = lhc;
790 nipdata->name_len = name_len;
797 * Shift *chainp up to the specified directory, change the filename
798 * to "0xINODENUMBER", and adjust the key. The chain becomes the
799 * invisible hardlink target.
801 * The original *chainp has already been marked deleted.
805 hammer2_hardlink_shiftup(hammer2_trans_t *trans, hammer2_cluster_t *cluster,
806 hammer2_inode_t *dip, hammer2_cluster_t *dcluster,
807 int nlinks, int *errorp)
809 const hammer2_inode_data_t *iptmp;
810 hammer2_inode_data_t *nipdata;
811 hammer2_cluster_t *xcluster;
812 hammer2_key_t key_dummy;
814 hammer2_blockref_t bref;
817 iptmp = &hammer2_cluster_data(cluster)->ipdata;
819 KKASSERT((lhc & HAMMER2_DIRHASH_VISIBLE) == 0);
822 * Locate the inode or indirect block to create the new
823 * entry in. lhc represents the inode number so there is
824 * no collision iteration.
826 * There should be no key collisions with invisible inode keys.
828 * WARNING! Must use inode_lock_ex() on dip to handle a stale
829 * dip->cluster cache.
833 xcluster = hammer2_cluster_lookup(dcluster, &key_dummy,
834 lhc, lhc, 0, &ddflag);
836 kprintf("X3 chain %p dip %p dchain %p dip->chain %p\n",
837 xcluster->focus, dip, dcluster->focus,
839 hammer2_cluster_unlock(xcluster);
848 * Create entry in common parent directory using the seek position
851 * We must refactor cluster because it might have been shifted into
852 * an indirect cluster by the create.
855 KKASSERT(xcluster == NULL);
857 *errorp = hammer2_cluster_create(trans, dcluster, &xcluster,
859 HAMMER2_BREF_TYPE_INODE,/* n/a */
860 HAMMER2_INODE_BYTES); /* n/a */
862 /*XXX this somehow isn't working on cluster XXX*/
867 * Cleanup and handle retries.
869 if (*errorp == EAGAIN) {
871 hammer2_cluster_wait(dcluster);
872 hammer2_cluster_drop(dcluster);
877 * Handle the error case
881 KKASSERT(xcluster == NULL);
886 * Use xcluster as a placeholder for (lhc). Duplicate cluster to the
887 * same target bref as xcluster and then delete xcluster. The
888 * duplication occurs after xcluster in flush order even though
889 * xcluster is deleted after the duplication. XXX
891 * WARNING! Duplications (to a different parent) can cause indirect
892 * blocks to be inserted, refactor xcluster.
894 * WARNING! Only key and keybits is extracted from a passed-in bref.
896 hammer2_cluster_bref(cluster, &bref);
897 bref.key = lhc; /* invisible dir entry key */
899 hammer2_cluster_duplicate(trans, dcluster, cluster, &bref, 0, 2);
902 * cluster is now 'live' again.. adjust the filename.
904 * Directory entries are inodes but this is a hidden hardlink
905 * target. The name isn't used but to ease debugging give it
906 * a name after its inode number.
908 hammer2_cluster_modify(trans, cluster, 0);
909 nipdata = &hammer2_cluster_wdata(cluster)->ipdata;
910 ksnprintf(nipdata->filename, sizeof(nipdata->filename),
911 "0x%016jx", (intmax_t)nipdata->inum);
912 nipdata->name_len = strlen(nipdata->filename);
913 nipdata->name_key = lhc;
914 nipdata->nlinks += nlinks;
915 hammer2_cluster_modsync(cluster);
919 * Connect the target inode represented by (*chainp) to the media topology
920 * at (dip, name, len). The caller can pass a rough *chainp, this function
921 * will issue lookup()s to position the parent chain properly for the
924 * If hlink is TRUE this function creates an OBJTYPE_HARDLINK directory
925 * entry instead of connecting (*chainp).
927 * If hlink is FALSE this function uses chain_duplicate() to make a copy
928 * if (*chainp) in the directory entry. (*chainp) is likely to be deleted
929 * by the caller in this case (e.g. rename).
932 hammer2_inode_connect(hammer2_trans_t *trans,
933 hammer2_cluster_t **clusterp, int hlink,
934 hammer2_inode_t *dip, hammer2_cluster_t *dcluster,
935 const uint8_t *name, size_t name_len,
938 hammer2_inode_data_t *wipdata;
939 hammer2_cluster_t *ocluster;
940 hammer2_cluster_t *ncluster;
941 hammer2_key_t key_dummy;
946 * Since ocluster is either disconnected from the topology or
947 * represents a hardlink terminus which is always a parent of or
948 * equal to dip, we should be able to safely lock dip->chain for
951 * WARNING! Must use inode_lock_ex() on dip to handle a stale
954 ocluster = *clusterp;
957 * If name is non-NULL we calculate lhc, else we use the passed-in
961 lhc = hammer2_dirhash(name, name_len);
964 * Locate the inode or indirect block to create the new
965 * entry in. At the same time check for key collisions
966 * and iterate until we don't get one.
970 ncluster = hammer2_cluster_lookup(dcluster, &key_dummy,
973 if (ncluster == NULL)
975 if ((lhc & HAMMER2_DIRHASH_LOMASK) ==
976 HAMMER2_DIRHASH_LOMASK) {
979 hammer2_cluster_unlock(ncluster);
985 * Reconnect to specific key (used when moving
986 * unlinked-but-open files into the hidden directory).
988 ncluster = hammer2_cluster_lookup(dcluster, &key_dummy,
991 KKASSERT(ncluster == NULL);
997 * Hardlink pointer needed, create totally fresh
1000 * We must refactor ocluster because it might have
1001 * been shifted into an indirect cluster by the
1004 KKASSERT(ncluster == NULL);
1005 error = hammer2_cluster_create(trans,
1006 dcluster, &ncluster,
1008 HAMMER2_BREF_TYPE_INODE,
1009 HAMMER2_INODE_BYTES);
1010 hammer2_cluster_refactor(ocluster);
1013 * Reconnect the original cluster and rename. Use
1014 * cluster_duplicate(). The caller will likely delete
1015 * or has already deleted the original chain in
1018 * NOTE: cluster_duplicate() generates a new cluster
1019 * with CHAIN_DELETED cleared (ocluster typically
1020 * has it set from the file unlink).
1022 * WARNING! Can cause held-over clusters to require a
1023 * refactor. Fortunately we have none (our
1024 * locked clusters are passed into and
1025 * modified by the call).
1027 ncluster = ocluster;
1029 hammer2_cluster_duplicate(trans, NULL, ncluster, NULL,
1031 error = hammer2_cluster_create(trans,
1032 dcluster, &ncluster,
1034 HAMMER2_BREF_TYPE_INODE,
1035 HAMMER2_INODE_BYTES);
1042 KKASSERT(error != EAGAIN);
1045 * ncluster should be NULL on error, leave ocluster
1046 * (ocluster == *clusterp) alone.
1049 KKASSERT(ncluster == NULL);
1054 * Directory entries are inodes so if the name has changed we have
1055 * to update the inode.
1057 * When creating an OBJTYPE_HARDLINK entry remember to unlock the
1058 * cluster, the caller will access the hardlink via the actual hardlink
1059 * target file and not the hardlink pointer entry, so we must still
1062 if (hlink && hammer2_hardlink_enable >= 0) {
1064 * Create the HARDLINK pointer. oip represents the hardlink
1065 * target in this situation.
1067 * We will return ocluster (the hardlink target).
1069 hammer2_cluster_modify(trans, ncluster, 0);
1070 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
1071 wipdata = &hammer2_cluster_wdata(ncluster)->ipdata;
1072 bcopy(name, wipdata->filename, name_len);
1073 wipdata->name_key = lhc;
1074 wipdata->name_len = name_len;
1075 wipdata->target_type =
1076 hammer2_cluster_data(ocluster)->ipdata.type;
1077 wipdata->type = HAMMER2_OBJTYPE_HARDLINK;
1078 wipdata->inum = hammer2_cluster_data(ocluster)->ipdata.inum;
1079 wipdata->nlinks = 1;
1080 hammer2_cluster_modsync(ncluster);
1081 hammer2_cluster_unlock(ncluster);
1082 ncluster = ocluster;
1086 * ncluster is a duplicate of ocluster at the new location.
1087 * We must fixup the name stored in oip. The bref key
1088 * has already been set up.
1090 hammer2_cluster_modify(trans, ncluster, 0);
1091 wipdata = &hammer2_cluster_wdata(ncluster)->ipdata;
1093 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
1094 bcopy(name, wipdata->filename, name_len);
1095 wipdata->name_key = lhc;
1096 wipdata->name_len = name_len;
1097 wipdata->nlinks = 1;
1098 hammer2_cluster_modsync(ncluster);
1102 * We are replacing ocluster with ncluster, unlock ocluster. In the
1103 * case where ocluster is left unchanged the code above sets
1104 * ncluster to ocluster and ocluster to NULL, resulting in a NOP here.
1107 hammer2_cluster_unlock(ocluster);
1108 *clusterp = ncluster;
1114 * Repoint ip->cluster's chains to cluster's chains. Caller must hold
1115 * the inode exclusively locked. cluster may be NULL to clean out any
1116 * chains in ip->cluster.
1119 hammer2_inode_repoint(hammer2_inode_t *ip, hammer2_inode_t *pip,
1120 hammer2_cluster_t *cluster)
1122 hammer2_chain_t *ochain;
1123 hammer2_chain_t *nchain;
1124 hammer2_inode_t *opip;
1128 * Replace chains in ip->cluster with chains from cluster and
1129 * adjust the focus if necessary.
1131 * NOTE: nchain and/or ochain can be NULL due to gaps
1132 * in the cluster arrays.
1134 ip->cluster.focus = NULL;
1135 for (i = 0; cluster && i < cluster->nchains; ++i) {
1136 nchain = cluster->array[i];
1137 if (i < ip->cluster.nchains) {
1138 ochain = ip->cluster.array[i];
1139 if (ochain == nchain) {
1140 if (ip->cluster.focus == NULL)
1141 ip->cluster.focus = nchain;
1151 ip->cluster.array[i] = nchain;
1152 if (ip->cluster.focus == NULL)
1153 ip->cluster.focus = nchain;
1155 hammer2_chain_ref(nchain);
1157 hammer2_chain_drop(ochain);
1161 * Release any left-over chains in ip->cluster.
1163 while (i < ip->cluster.nchains) {
1164 nchain = ip->cluster.array[i];
1166 ip->cluster.array[i] = NULL;
1167 hammer2_chain_drop(nchain);
1171 ip->cluster.nchains = cluster ? cluster->nchains : 0;
1174 * Repoint ip->pip if requested (non-NULL pip).
1176 if (pip && ip->pip != pip) {
1178 hammer2_inode_ref(pip);
1181 hammer2_inode_drop(opip);
1186 * Unlink the file from the specified directory inode. The directory inode
1187 * does not need to be locked.
1189 * isdir determines whether a directory/non-directory check should be made.
1190 * No check is made if isdir is set to -1.
1192 * isopen specifies whether special unlink-with-open-descriptor handling
1193 * must be performed. If set to -1 the caller is deleting a PFS and we
1194 * check whether the chain is mounted or not (chain->pmp != NULL). 1 is
1195 * implied if it is mounted.
1197 * If isopen is 1 and nlinks drops to 0 this function must move the chain
1198 * to a special hidden directory until last-close occurs on the file.
1200 * NOTE! The underlying file can still be active with open descriptors
1201 * or if the chain is being manually held (e.g. for rename).
1203 * The caller is responsible for fixing up ip->chain if e.g. a
1204 * rename occurs (see chain_duplicate()).
1207 hammer2_unlink_file(hammer2_trans_t *trans, hammer2_inode_t *dip,
1208 const uint8_t *name, size_t name_len,
1209 int isdir, int *hlinkp, struct nchandle *nch)
1211 const hammer2_inode_data_t *ripdata;
1212 hammer2_inode_data_t *wipdata;
1213 hammer2_cluster_t *cparent;
1214 hammer2_cluster_t *ocluster;
1215 hammer2_cluster_t *cluster;
1216 hammer2_cluster_t *dparent;
1217 hammer2_cluster_t *dcluster;
1218 hammer2_key_t key_dummy;
1219 hammer2_key_t key_next;
1227 lhc = hammer2_dirhash(name, name_len);
1230 * Search for the filename in the directory
1234 cparent = hammer2_inode_lock_ex(dip);
1235 cluster = hammer2_cluster_lookup(cparent, &key_next,
1236 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1239 if (hammer2_cluster_type(cluster) == HAMMER2_BREF_TYPE_INODE) {
1240 ripdata = &hammer2_cluster_data(cluster)->ipdata;
1241 if (ripdata->name_len == name_len &&
1242 bcmp(ripdata->filename, name, name_len) == 0) {
1246 cluster = hammer2_cluster_next(cparent, cluster, &key_next,
1248 lhc + HAMMER2_DIRHASH_LOMASK,
1251 hammer2_inode_unlock_ex(dip, NULL); /* retain parent */
1254 * Not found or wrong type (isdir < 0 disables the type check).
1255 * If a hardlink pointer, type checks use the hardlink target.
1257 if (cluster == NULL) {
1261 ripdata = &hammer2_cluster_data(cluster)->ipdata;
1262 type = ripdata->type;
1263 if (type == HAMMER2_OBJTYPE_HARDLINK) {
1266 type = ripdata->target_type;
1269 if (type == HAMMER2_OBJTYPE_DIRECTORY && isdir == 0) {
1273 if (type != HAMMER2_OBJTYPE_DIRECTORY && isdir >= 1) {
1279 * Hardlink must be resolved. We can't hold the parent locked
1280 * while we do this or we could deadlock.
1282 * On success cluster will be adjusted to point at the hardlink target
1283 * and ocluster will point to the hardlink pointer in the original
1284 * directory. Otherwise cluster remains pointing to the original.
1286 * Lock ownership is transfered to cluster. ocluster is merely
1289 if (ripdata->type == HAMMER2_OBJTYPE_HARDLINK) {
1290 hammer2_cluster_unlock(cparent);
1294 cluster = hammer2_cluster_copy(ocluster,
1295 HAMMER2_CLUSTER_COPY_CHAINS);
1296 error = hammer2_hardlink_find(dip, cluster);
1297 KKASSERT(error == 0);
1301 * If this is a directory the directory must be empty. However, if
1302 * isdir < 0 we are doing a rename and the directory does not have
1303 * to be empty, and if isdir > 1 we are deleting a PFS/snapshot
1304 * and the directory does not have to be empty.
1306 * NOTE: We check the full key range here which covers both visible
1307 * and invisible entries. Theoretically there should be no
1308 * invisible (hardlink target) entries if there are no visible
1311 if (type == HAMMER2_OBJTYPE_DIRECTORY && isdir == 1) {
1312 dparent = hammer2_cluster_lookup_init(cluster, 0);
1313 dcluster = hammer2_cluster_lookup(dparent, &key_dummy,
1314 0, (hammer2_key_t)-1,
1315 HAMMER2_LOOKUP_NODATA,
1318 hammer2_cluster_unlock(dcluster);
1319 hammer2_cluster_lookup_done(dparent);
1323 hammer2_cluster_lookup_done(dparent);
1329 * Ok, we can now unlink the cluster. We always decrement nlinks even
1330 * if the entry can be deleted in case someone has the file open and
1333 * The cluster itself will no longer be in the on-media topology but
1334 * can still be flushed to the media (e.g. if an open descriptor
1335 * remains). When the last vnode/ip ref goes away the cluster will
1336 * be marked unmodified, avoiding any further (now unnecesary) I/O.
1338 * A non-NULL ocluster indicates a hardlink.
1342 * Delete the original hardlink pointer unconditionally.
1343 * (any open descriptors will migrate to the hardlink
1344 * target and have no affect on this operation).
1346 * NOTE: parent from above is NULL when ocluster != NULL
1347 * so we can reuse it.
1349 hammer2_cluster_lock(ocluster, HAMMER2_RESOLVE_ALWAYS);
1350 hammer2_cluster_delete(trans, ocluster, 0);
1351 hammer2_cluster_unlock(ocluster);
1355 * Decrement nlinks on the hardlink target (or original file if
1356 * there it was not hardlinked). Delete the target when nlinks
1357 * reaches 0 with special handling if (isopen) is set.
1359 * NOTE! In DragonFly the vnops function calls cache_unlink() after
1360 * calling us here to clean out the namecache association,
1361 * (which does not represent a ref for the open-test), and to
1362 * force finalization of the vnode if/when the last ref gets
1365 * NOTE! Files are unlinked by rename and then relinked. nch will be
1366 * passed as NULL in this situation. hammer2_inode_connect()
1369 KKASSERT(cluster != NULL);
1370 hammer2_cluster_modify(trans, cluster, 0);
1371 wipdata = &hammer2_cluster_wdata(cluster)->ipdata;
1374 if ((int64_t)wipdata->nlinks < 0) { /* XXX debugging */
1375 wipdata->nlinks = 0;
1377 hammer2_cluster_modsync(cluster);
1379 if (wipdata->nlinks == 0) {
1380 if ((cluster->focus->flags & HAMMER2_CHAIN_PFSROOT) &&
1383 kprintf("hammer2: PFS \"%s\" cannot be deleted "
1384 "while still mounted\n",
1388 if (nch && cache_isopen(nch)) {
1389 kprintf("WARNING: unlinking open file\n");
1390 hammer2_cluster_set_chainflags(cluster,
1391 HAMMER2_CHAIN_UNLINKED);
1392 hammer2_inode_move_to_hidden(trans, &cluster,
1395 hammer2_cluster_delete(trans, cluster, 0);
1401 hammer2_cluster_unlock(cluster);
1403 hammer2_cluster_lookup_done(cparent);
1405 hammer2_cluster_drop(ocluster);
1412 * This is called from the mount code to initialize pmp->ihidden
1415 hammer2_inode_install_hidden(hammer2_pfsmount_t *pmp)
1417 hammer2_trans_t trans;
1418 hammer2_cluster_t *cparent;
1419 hammer2_cluster_t *cluster;
1420 hammer2_cluster_t *scan;
1421 hammer2_inode_data_t *wipdata;
1422 hammer2_key_t key_dummy;
1423 hammer2_key_t key_next;
1432 * Find the hidden directory
1434 bzero(&key_dummy, sizeof(key_dummy));
1435 hammer2_trans_init(&trans, pmp, 0);
1437 cparent = hammer2_inode_lock_ex(pmp->iroot);
1438 cluster = hammer2_cluster_lookup(cparent, &key_dummy,
1439 HAMMER2_INODE_HIDDENDIR,
1440 HAMMER2_INODE_HIDDENDIR,
1443 pmp->ihidden = hammer2_inode_get(pmp, pmp->iroot, cluster);
1444 hammer2_inode_ref(pmp->ihidden);
1447 * Remove any unlinked files which were left open as-of
1451 scan = hammer2_cluster_lookup(cluster, &key_next,
1453 HAMMER2_LOOKUP_NODATA, &ddflag);
1455 if (hammer2_cluster_type(scan) ==
1456 HAMMER2_BREF_TYPE_INODE) {
1457 hammer2_cluster_delete(&trans, scan, 0);
1460 scan = hammer2_cluster_next(cluster, scan, &key_next,
1462 HAMMER2_LOOKUP_NODATA);
1465 hammer2_inode_unlock_ex(pmp->ihidden, cluster);
1466 hammer2_inode_unlock_ex(pmp->iroot, cparent);
1467 hammer2_trans_done(&trans);
1468 kprintf("hammer2: PFS loaded hidden dir, "
1469 "removed %d dead entries\n", count);
1474 * Create the hidden directory
1476 error = hammer2_cluster_create(&trans, cparent, &cluster,
1477 HAMMER2_INODE_HIDDENDIR, 0,
1478 HAMMER2_BREF_TYPE_INODE,
1479 HAMMER2_INODE_BYTES);
1480 hammer2_inode_unlock_ex(pmp->iroot, cparent);
1482 hammer2_cluster_modify(&trans, cluster, 0);
1483 wipdata = &hammer2_cluster_wdata(cluster)->ipdata;
1484 wipdata->type = HAMMER2_OBJTYPE_DIRECTORY;
1485 wipdata->inum = HAMMER2_INODE_HIDDENDIR;
1486 wipdata->nlinks = 1;
1487 hammer2_cluster_modsync(cluster);
1488 kprintf("hammer2: PFS root missing hidden directory, creating\n");
1490 pmp->ihidden = hammer2_inode_get(pmp, pmp->iroot, cluster);
1491 hammer2_inode_ref(pmp->ihidden);
1492 hammer2_inode_unlock_ex(pmp->ihidden, cluster);
1493 hammer2_trans_done(&trans);
1497 * If an open file is unlinked H2 needs to retain the file in the topology
1498 * to ensure that its backing store is not recovered by the bulk free scan.
1499 * This also allows us to avoid having to special-case the CHAIN_DELETED flag.
1501 * To do this the file is moved to a hidden directory in the PFS root and
1502 * renamed. The hidden directory must be created if it does not exist.
1506 hammer2_inode_move_to_hidden(hammer2_trans_t *trans,
1507 hammer2_cluster_t **clusterp, hammer2_tid_t inum)
1509 hammer2_cluster_t *dcluster;
1510 hammer2_pfsmount_t *pmp;
1513 pmp = (*clusterp)->pmp;
1514 KKASSERT(pmp != NULL);
1515 KKASSERT(pmp->ihidden != NULL);
1517 hammer2_cluster_delete(trans, *clusterp, 0);
1518 dcluster = hammer2_inode_lock_ex(pmp->ihidden);
1519 error = hammer2_inode_connect(trans, clusterp, 0,
1520 pmp->ihidden, dcluster,
1522 hammer2_inode_unlock_ex(pmp->ihidden, dcluster);
1523 KKASSERT(error == 0);
1527 * Given an exclusively locked inode and cluster we consolidate its cluster
1528 * for hardlink creation, adding (nlinks) to the file's link count and
1529 * potentially relocating the inode to a directory common to ip->pip and tdip.
1531 * Replaces (*clusterp) if consolidation occurred, unlocking the old cluster
1532 * and returning a new locked cluster.
1534 * NOTE! This function will also replace ip->cluster.
1537 hammer2_hardlink_consolidate(hammer2_trans_t *trans,
1538 hammer2_inode_t *ip,
1539 hammer2_cluster_t **clusterp,
1540 hammer2_inode_t *cdip,
1541 hammer2_cluster_t *cdcluster,
1544 const hammer2_inode_data_t *ripdata;
1545 hammer2_inode_data_t *wipdata;
1546 hammer2_cluster_t *cluster;
1547 hammer2_cluster_t *ncluster;
1550 cluster = *clusterp;
1551 ripdata = &hammer2_cluster_data(cluster)->ipdata;
1552 if (nlinks == 0 && /* no hardlink needed */
1553 (ripdata->name_key & HAMMER2_DIRHASH_VISIBLE)) {
1557 if (hammer2_hardlink_enable == 0) { /* disallow hardlinks */
1558 hammer2_cluster_unlock(cluster);
1564 * If no change in the hardlink's target directory is required and
1565 * this is already a hardlink target, all we need to do is adjust
1568 ripdata = &hammer2_cluster_data(cluster)->ipdata;
1569 if (cdip == ip->pip &&
1570 (ripdata->name_key & HAMMER2_DIRHASH_VISIBLE) == 0) {
1572 hammer2_cluster_modify(trans, cluster, 0);
1573 wipdata = &hammer2_cluster_wdata(cluster)->ipdata;
1574 wipdata->nlinks += nlinks;
1575 hammer2_cluster_modsync(cluster);
1584 * cluster is the real inode. If it's visible we have to convert it
1585 * to a hardlink pointer. If it is not visible then it is already
1586 * a hardlink target and only needs to be deleted.
1588 KKASSERT((cluster->focus->flags & HAMMER2_CHAIN_DELETED) == 0);
1589 ripdata = &hammer2_cluster_data(cluster)->ipdata;
1590 KKASSERT(ripdata->type != HAMMER2_OBJTYPE_HARDLINK);
1591 if (ripdata->name_key & HAMMER2_DIRHASH_VISIBLE) {
1593 * We are going to duplicate cluster later, causing its
1594 * media block to be shifted to the duplicate. Even though
1595 * we are delete-duplicating ncluster here it might decide not
1596 * to reallocate the block. Set FORCECOW to force it to.
1598 ncluster = hammer2_cluster_copy(cluster,
1599 HAMMER2_CLUSTER_COPY_CHAINS |
1600 HAMMER2_CLUSTER_COPY_NOREF);
1601 hammer2_cluster_lock(ncluster, HAMMER2_RESOLVE_ALWAYS);
1602 hammer2_cluster_set_chainflags(ncluster,
1603 HAMMER2_CHAIN_FORCECOW);
1604 hammer2_cluster_delete_duplicate(trans, ncluster,
1605 HAMMER2_DELDUP_RECORE);
1606 KKASSERT((ncluster->focus->flags &
1607 HAMMER2_CHAIN_DUPLICATED) == 0);
1608 wipdata = &hammer2_cluster_wdata(ncluster)->ipdata;
1609 wipdata->target_type = wipdata->type;
1610 wipdata->type = HAMMER2_OBJTYPE_HARDLINK;
1611 wipdata->uflags = 0;
1612 wipdata->rmajor = 0;
1613 wipdata->rminor = 0;
1618 bzero(&wipdata->uid, sizeof(wipdata->uid));
1619 bzero(&wipdata->gid, sizeof(wipdata->gid));
1620 wipdata->op_flags = HAMMER2_OPFLAG_DIRECTDATA;
1621 wipdata->cap_flags = 0;
1624 wipdata->nlinks = 1;
1625 wipdata->iparent = 0; /* XXX */
1626 wipdata->pfs_type = 0;
1627 wipdata->pfs_inum = 0;
1628 bzero(&wipdata->pfs_clid, sizeof(wipdata->pfs_clid));
1629 bzero(&wipdata->pfs_fsid, sizeof(wipdata->pfs_fsid));
1630 wipdata->data_quota = 0;
1631 wipdata->data_count = 0;
1632 wipdata->inode_quota = 0;
1633 wipdata->inode_count = 0;
1634 wipdata->attr_tid = 0;
1635 wipdata->dirent_tid = 0;
1636 bzero(&wipdata->u, sizeof(wipdata->u));
1637 /* XXX transaction ids */
1638 hammer2_cluster_modsync(ncluster);
1640 hammer2_cluster_delete(trans, cluster, 0);
1646 * cluster represents the hardlink target and is now flagged deleted.
1647 * duplicate it to the parent directory and adjust nlinks.
1649 * WARNING! The shiftup() call can cause ncluster to be moved into
1650 * an indirect block, and our ncluster will wind up pointing
1651 * to the older/original version.
1653 KKASSERT(cluster->focus->flags & HAMMER2_CHAIN_DELETED);
1654 hammer2_hardlink_shiftup(trans, cluster, cdip, cdcluster,
1658 hammer2_inode_repoint(ip, cdip, cluster);
1661 * Unlock and destroy ncluster.
1662 * Return the shifted cluster in *clusterp.
1665 hammer2_cluster_unlock(ncluster);
1669 * Cleanup, cluster/ncluster already dealt with.
1671 *clusterp = cluster;
1672 hammer2_inode_drop(cdip);
1678 * If (*ochainp) is non-NULL it points to the forward OBJTYPE_HARDLINK
1679 * inode while (*chainp) points to the resolved (hidden hardlink
1680 * target) inode. In this situation when nlinks is 1 we wish to
1681 * deconsolidate the hardlink, moving it back to the directory that now
1682 * represents the only remaining link.
1685 hammer2_hardlink_deconsolidate(hammer2_trans_t *trans,
1686 hammer2_inode_t *dip,
1687 hammer2_chain_t **chainp,
1688 hammer2_chain_t **ochainp)
1690 if (*ochainp == NULL)
1697 * The caller presents a locked *chainp pointing to a HAMMER2_BREF_TYPE_INODE
1698 * with an obj_type of HAMMER2_OBJTYPE_HARDLINK. This routine will gobble
1699 * the *chainp and return a new locked *chainp representing the file target
1700 * (the original *chainp will be unlocked).
1702 * When a match is found the chain representing the original HARDLINK
1703 * will be returned in *ochainp with a ref, but not locked.
1705 * When no match is found *chainp is set to NULL and EIO is returned.
1706 * (*ochainp) will still be set to the original chain with a ref but not
1710 hammer2_hardlink_find(hammer2_inode_t *dip, hammer2_cluster_t *cluster)
1712 const hammer2_inode_data_t *ipdata;
1713 hammer2_cluster_t *cparent;
1714 hammer2_cluster_t *rcluster;
1715 hammer2_inode_t *ip;
1716 hammer2_inode_t *pip;
1717 hammer2_key_t key_dummy;
1722 hammer2_inode_ref(pip); /* for loop */
1725 * Locate the hardlink. pip is referenced and not locked.
1727 ipdata = &hammer2_cluster_data(cluster)->ipdata;
1731 * We don't need the cluster's chains, but we need to retain the
1732 * cluster structure itself so we can load the hardlink search
1735 KKASSERT(cluster->refs == 1);
1736 atomic_add_int(&cluster->refs, 1);
1737 hammer2_cluster_unlock(cluster); /* hack */
1738 cluster->nchains = 0; /* hack */
1742 while ((ip = pip) != NULL) {
1743 cparent = hammer2_inode_lock_ex(ip);
1744 hammer2_inode_drop(ip); /* loop */
1745 KKASSERT(hammer2_cluster_type(cparent) ==
1746 HAMMER2_BREF_TYPE_INODE);
1747 rcluster = hammer2_cluster_lookup(cparent, &key_dummy,
1748 lhc, lhc, 0, &ddflag);
1749 hammer2_cluster_lookup_done(cparent); /* discard parent */
1752 pip = ip->pip; /* safe, ip held locked */
1754 hammer2_inode_ref(pip); /* loop */
1755 hammer2_inode_unlock_ex(ip, NULL);
1759 * chain is locked, ip is locked. Unlock ip, return the locked
1760 * chain. *ipp is already set w/a ref count and not locked.
1762 * (cparent is already unlocked).
1765 hammer2_inode_unlock_ex(ip, NULL);
1768 hammer2_cluster_replace(cluster, rcluster);
1769 hammer2_cluster_drop(rcluster);
1777 * Find the directory common to both fdip and tdip, hold and return
1781 hammer2_inode_common_parent(hammer2_inode_t *fdip, hammer2_inode_t *tdip)
1783 hammer2_inode_t *scan1;
1784 hammer2_inode_t *scan2;
1787 * We used to have a depth field but it complicated matters too
1788 * much for directory renames. So now its ugly. Check for
1789 * simple cases before giving up and doing it the expensive way.
1791 * XXX need a bottom-up topology stability lock
1793 if (fdip == tdip || fdip == tdip->pip) {
1794 hammer2_inode_ref(fdip);
1797 if (fdip->pip == tdip) {
1798 hammer2_inode_ref(tdip);
1805 for (scan1 = fdip; scan1->pmp == fdip->pmp; scan1 = scan1->pip) {
1807 while (scan2->pmp == tdip->pmp) {
1808 if (scan1 == scan2) {
1809 hammer2_inode_ref(scan1);
1817 panic("hammer2_inode_common_parent: no common parent %p %p\n",
1824 * Synchronize the inode's frontend state with the chain state prior
1825 * to any explicit flush of the inode or any strategy write call.
1827 * Called with a locked inode.
1830 hammer2_inode_fsync(hammer2_trans_t *trans, hammer2_inode_t *ip,
1831 hammer2_cluster_t *cparent)
1833 const hammer2_inode_data_t *ripdata;
1834 hammer2_inode_data_t *wipdata;
1835 hammer2_cluster_t *dparent;
1836 hammer2_cluster_t *cluster;
1837 hammer2_key_t lbase;
1838 hammer2_key_t key_next;
1842 ripdata = &hammer2_cluster_data(cparent)->ipdata; /* target file */
1844 if (ip->flags & HAMMER2_INODE_MTIME) {
1845 wipdata = hammer2_cluster_modify_ip(trans, ip, cparent, 0);
1846 atomic_clear_int(&ip->flags, HAMMER2_INODE_MTIME);
1847 wipdata->mtime = ip->mtime;
1851 if ((ip->flags & HAMMER2_INODE_RESIZED) && ip->size < ripdata->size) {
1852 wipdata = hammer2_cluster_modify_ip(trans, ip, cparent, 0);
1853 wipdata->size = ip->size;
1856 atomic_clear_int(&ip->flags, HAMMER2_INODE_RESIZED);
1859 * We must delete any chains beyond the EOF. The chain
1860 * straddling the EOF will be pending in the bioq.
1862 lbase = (ripdata->size + HAMMER2_PBUFMASK64) &
1863 ~HAMMER2_PBUFMASK64;
1864 dparent = hammer2_cluster_lookup_init(&ip->cluster, 0);
1865 cluster = hammer2_cluster_lookup(dparent, &key_next,
1866 lbase, (hammer2_key_t)-1,
1867 HAMMER2_LOOKUP_NODATA,
1871 * Degenerate embedded case, nothing to loop on
1873 switch (hammer2_cluster_type(cluster)) {
1874 case HAMMER2_BREF_TYPE_INODE:
1875 hammer2_cluster_unlock(cluster);
1878 case HAMMER2_BREF_TYPE_DATA:
1879 hammer2_cluster_delete(trans, cluster, 0);
1882 cluster = hammer2_cluster_next(dparent, cluster,
1884 key_next, (hammer2_key_t)-1,
1885 HAMMER2_LOOKUP_NODATA);
1889 hammer2_cluster_lookup_done(dparent);
1891 if ((ip->flags & HAMMER2_INODE_RESIZED) && ip->size > ripdata->size) {
1892 wipdata = hammer2_cluster_modify_ip(trans, ip, cparent, 0);
1893 wipdata->size = ip->size;
1894 atomic_clear_int(&ip->flags, HAMMER2_INODE_RESIZED);
1897 * When resizing larger we may not have any direct-data
1900 if ((wipdata->op_flags & HAMMER2_OPFLAG_DIRECTDATA) &&
1901 ip->size > HAMMER2_EMBEDDED_BYTES) {
1902 wipdata->op_flags &= ~HAMMER2_OPFLAG_DIRECTDATA;
1903 bzero(&wipdata->u.blockset,
1904 sizeof(wipdata->u.blockset));
1910 hammer2_cluster_modsync(cparent);