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 **cparentp,
48 hammer2_cluster_t **clusterp,
51 RB_GENERATE2(hammer2_inode_tree, hammer2_inode, rbnode, hammer2_inode_cmp,
55 hammer2_inode_cmp(hammer2_inode_t *ip1, hammer2_inode_t *ip2)
57 if (ip1->inum < ip2->inum)
59 if (ip1->inum > ip2->inum)
67 * HAMMER2 offers shared locks and exclusive locks on inodes.
69 * The inode locking function locks the inode itself, resolves any stale
70 * chains in the inode's cluster, and allocates a fresh copy of the
71 * cluster with 1 ref and all the underlying chains locked. Duplication
72 * races are handled by this function.
74 * ip->cluster will be stable while the inode is locked.
76 * NOTE: We don't combine the inode/chain lock because putting away an
77 * inode would otherwise confuse multiple lock holders of the inode.
79 * NOTE: In-memory inodes always point to hardlink targets (the actual file),
80 * and never point to a hardlink pointer.
83 hammer2_inode_lock_ex(hammer2_inode_t *ip)
85 const hammer2_inode_data_t *ipdata;
86 hammer2_cluster_t *cluster;
87 hammer2_chain_t *chain;
90 hammer2_inode_ref(ip);
91 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
92 cluster = hammer2_cluster_copy(&ip->cluster,
93 HAMMER2_CLUSTER_COPY_NOCHAINS);
95 ip->cluster.focus = NULL;
96 cluster->focus = NULL;
98 for (i = 0; i < cluster->nchains; ++i) {
99 chain = ip->cluster.array[i];
101 kprintf("inode_lock: %p: missing chain\n", ip);
105 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS);
106 cluster->array[i] = chain;
107 if (cluster->focus == NULL)
108 cluster->focus = chain;
109 if (ip->cluster.focus == NULL)
110 ip->cluster.focus = chain;
114 * Returned cluster must resolve hardlink pointers
116 ipdata = &hammer2_cluster_data(cluster)->ipdata;
117 KKASSERT(ipdata->type != HAMMER2_OBJTYPE_HARDLINK);
119 if (ipdata->type == HAMMER2_OBJTYPE_HARDLINK &&
120 (cluster->focus->flags & HAMMER2_CHAIN_DELETED) == 0) {
121 error = hammer2_hardlink_find(ip->pip, NULL, cluster);
122 KKASSERT(error == 0);
130 hammer2_inode_unlock_ex(hammer2_inode_t *ip, hammer2_cluster_t *cluster)
133 hammer2_cluster_unlock(cluster);
134 ccms_thread_unlock(&ip->topo_cst);
135 hammer2_inode_drop(ip);
139 * NOTE: We don't combine the inode/chain lock because putting away an
140 * inode would otherwise confuse multiple lock holders of the inode.
142 * Shared locks are especially sensitive to having too many shared
143 * lock counts (from the same thread) on certain paths which might
144 * need to upgrade them. Only one count of a shared lock can be
148 hammer2_inode_lock_sh(hammer2_inode_t *ip)
150 const hammer2_inode_data_t *ipdata;
151 hammer2_cluster_t *cluster;
152 hammer2_chain_t *chain;
155 hammer2_inode_ref(ip);
156 cluster = hammer2_cluster_copy(&ip->cluster,
157 HAMMER2_CLUSTER_COPY_NOCHAINS);
158 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_SHARED);
160 cluster->focus = NULL;
162 for (i = 0; i < cluster->nchains; ++i) {
163 chain = ip->cluster.array[i];
166 kprintf("inode_lock: %p: missing chain\n", ip);
170 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS |
171 HAMMER2_RESOLVE_SHARED);
172 cluster->array[i] = chain;
173 if (cluster->focus == NULL)
174 cluster->focus = chain;
178 * Returned cluster must resolve hardlink pointers
180 ipdata = &hammer2_cluster_data(cluster)->ipdata;
181 KKASSERT(ipdata->type != HAMMER2_OBJTYPE_HARDLINK);
183 if (ipdata->type == HAMMER2_OBJTYPE_HARDLINK &&
184 (cluster->focus->flags & HAMMER2_CHAIN_DELETED) == 0) {
185 error = hammer2_hardlink_find(ip->pip, NULL, cluster);
186 KKASSERT(error == 0);
194 hammer2_inode_unlock_sh(hammer2_inode_t *ip, hammer2_cluster_t *cluster)
197 hammer2_cluster_unlock(cluster);
198 ccms_thread_unlock(&ip->topo_cst);
199 hammer2_inode_drop(ip);
203 hammer2_inode_lock_temp_release(hammer2_inode_t *ip)
205 return(ccms_thread_lock_temp_release(&ip->topo_cst));
209 hammer2_inode_lock_temp_restore(hammer2_inode_t *ip, ccms_state_t ostate)
211 ccms_thread_lock_temp_restore(&ip->topo_cst, ostate);
215 hammer2_inode_lock_upgrade(hammer2_inode_t *ip)
217 return(ccms_thread_lock_upgrade(&ip->topo_cst));
221 hammer2_inode_lock_downgrade(hammer2_inode_t *ip, ccms_state_t ostate)
223 ccms_thread_lock_downgrade(&ip->topo_cst, ostate);
227 * Lookup an inode by inode number
230 hammer2_inode_lookup(hammer2_pfsmount_t *pmp, hammer2_tid_t inum)
238 spin_lock(&pmp->inum_spin);
239 ip = RB_LOOKUP(hammer2_inode_tree, &pmp->inum_tree, inum);
241 hammer2_inode_ref(ip);
242 spin_unlock(&pmp->inum_spin);
248 * Adding a ref to an inode is only legal if the inode already has at least
251 * (can be called with spinlock held)
254 hammer2_inode_ref(hammer2_inode_t *ip)
256 atomic_add_int(&ip->refs, 1);
260 * Drop an inode reference, freeing the inode when the last reference goes
264 hammer2_inode_drop(hammer2_inode_t *ip)
266 hammer2_pfsmount_t *pmp;
267 hammer2_inode_t *pip;
275 * Transition to zero, must interlock with
276 * the inode inumber lookup tree (if applicable).
280 spin_lock(&pmp->inum_spin);
282 if (atomic_cmpset_int(&ip->refs, 1, 0)) {
283 KKASSERT(ip->topo_cst.count == 0);
284 if (ip->flags & HAMMER2_INODE_ONRBTREE) {
285 atomic_clear_int(&ip->flags,
286 HAMMER2_INODE_ONRBTREE);
287 RB_REMOVE(hammer2_inode_tree,
288 &pmp->inum_tree, ip);
290 spin_unlock(&pmp->inum_spin);
297 * Cleaning out ip->cluster isn't entirely
300 hammer2_inode_repoint(ip, NULL, NULL);
303 * We have to drop pip (if non-NULL) to
304 * dispose of our implied reference from
305 * ip->pip. We can simply loop on it.
307 kfree(ip, pmp->minode);
308 atomic_add_long(&pmp->inmem_inodes, -1);
310 /* continue with pip (can be NULL) */
312 spin_unlock(&ip->pmp->inum_spin);
316 * Non zero transition
318 if (atomic_cmpset_int(&ip->refs, refs, refs - 1))
325 * Get the vnode associated with the given inode, allocating the vnode if
326 * necessary. The vnode will be returned exclusively locked.
328 * The caller must lock the inode (shared or exclusive).
330 * Great care must be taken to avoid deadlocks and vnode acquisition/reclaim
334 hammer2_igetv(hammer2_inode_t *ip, hammer2_cluster_t *cparent, int *errorp)
336 const hammer2_inode_data_t *ipdata;
337 hammer2_pfsmount_t *pmp;
342 KKASSERT(pmp != NULL);
345 ipdata = &hammer2_cluster_data(cparent)->ipdata;
349 * Attempt to reuse an existing vnode assignment. It is
350 * possible to race a reclaim so the vget() may fail. The
351 * inode must be unlocked during the vget() to avoid a
352 * deadlock against a reclaim.
357 * Inode must be unlocked during the vget() to avoid
358 * possible deadlocks, but leave the ip ref intact.
360 * vnode is held to prevent destruction during the
361 * vget(). The vget() can still fail if we lost
362 * a reclaim race on the vnode.
365 ostate = hammer2_inode_lock_temp_release(ip);
366 if (vget(vp, LK_EXCLUSIVE)) {
368 hammer2_inode_lock_temp_restore(ip, ostate);
371 hammer2_inode_lock_temp_restore(ip, ostate);
373 /* vp still locked and ref from vget */
375 kprintf("hammer2: igetv race %p/%p\n",
385 * No vnode exists, allocate a new vnode. Beware of
386 * allocation races. This function will return an
387 * exclusively locked and referenced vnode.
389 *errorp = getnewvnode(VT_HAMMER2, pmp->mp, &vp, 0, 0);
391 kprintf("hammer2: igetv getnewvnode failed %d\n",
398 * Lock the inode and check for an allocation race.
400 ostate = hammer2_inode_lock_upgrade(ip);
401 if (ip->vp != NULL) {
404 hammer2_inode_lock_downgrade(ip, ostate);
408 switch (ipdata->type) {
409 case HAMMER2_OBJTYPE_DIRECTORY:
412 case HAMMER2_OBJTYPE_REGFILE:
414 vinitvmio(vp, ipdata->size,
416 (int)ipdata->size & HAMMER2_LBUFMASK);
418 case HAMMER2_OBJTYPE_SOFTLINK:
420 * XXX for now we are using the generic file_read
421 * and file_write code so we need a buffer cache
425 vinitvmio(vp, ipdata->size,
427 (int)ipdata->size & HAMMER2_LBUFMASK);
429 case HAMMER2_OBJTYPE_CDEV:
432 case HAMMER2_OBJTYPE_BDEV:
433 vp->v_ops = &pmp->mp->mnt_vn_spec_ops;
434 if (ipdata->type != HAMMER2_OBJTYPE_CDEV)
436 addaliasu(vp, ipdata->rmajor, ipdata->rminor);
438 case HAMMER2_OBJTYPE_FIFO:
440 vp->v_ops = &pmp->mp->mnt_vn_fifo_ops;
443 panic("hammer2: unhandled objtype %d", ipdata->type);
447 if (ip == pmp->iroot)
448 vsetflags(vp, VROOT);
452 hammer2_inode_ref(ip); /* vp association */
453 hammer2_inode_lock_downgrade(ip, ostate);
458 * Return non-NULL vp and *errorp == 0, or NULL vp and *errorp != 0.
460 if (hammer2_debug & 0x0002) {
461 kprintf("igetv vp %p refs 0x%08x aux 0x%08x\n",
462 vp, vp->v_refcnt, vp->v_auxrefs);
468 * Returns the inode associated with the passed-in cluster, creating the
469 * inode if necessary and synchronizing it to the passed-in cluster otherwise.
471 * The passed-in chain must be locked and will remain locked on return.
472 * The returned inode will be locked and the caller may dispose of both
473 * via hammer2_inode_unlock_ex(). However, if the caller needs to resolve
474 * a hardlink it must ref/unlock/relock/drop the inode.
476 * The hammer2_inode structure regulates the interface between the high level
477 * kernel VNOPS API and the filesystem backend (the chains).
480 hammer2_inode_get(hammer2_pfsmount_t *pmp, hammer2_inode_t *dip,
481 hammer2_cluster_t *cluster)
483 hammer2_inode_t *nip;
484 const hammer2_inode_data_t *iptmp;
485 const hammer2_inode_data_t *nipdata;
487 KKASSERT(hammer2_cluster_type(cluster) == HAMMER2_BREF_TYPE_INODE);
491 * Interlocked lookup/ref of the inode. This code is only needed
492 * when looking up inodes with nlinks != 0 (TODO: optimize out
493 * otherwise and test for duplicates).
497 iptmp = &hammer2_cluster_data(cluster)->ipdata;
498 nip = hammer2_inode_lookup(pmp, iptmp->inum);
502 ccms_thread_lock(&nip->topo_cst, CCMS_STATE_EXCLUSIVE);
505 * Handle SMP race (not applicable to the super-root spmp
506 * which can't index inodes due to duplicative inode numbers).
508 if (pmp->spmp_hmp == NULL &&
509 (nip->flags & HAMMER2_INODE_ONRBTREE) == 0) {
510 ccms_thread_unlock(&nip->topo_cst);
511 hammer2_inode_drop(nip);
514 hammer2_inode_repoint(nip, NULL, cluster);
519 * We couldn't find the inode number, create a new inode.
521 nip = kmalloc(sizeof(*nip), pmp->minode, M_WAITOK | M_ZERO);
522 atomic_add_long(&pmp->inmem_inodes, 1);
523 hammer2_pfs_memory_inc(pmp);
524 hammer2_pfs_memory_wakeup(pmp);
526 nip->flags = HAMMER2_INODE_SROOT;
529 * Initialize nip's cluster
531 nip->cluster.refs = 1;
532 nip->cluster.pmp = pmp;
533 nip->cluster.flags |= HAMMER2_CLUSTER_INODE;
534 hammer2_cluster_replace(&nip->cluster, cluster);
536 nipdata = &hammer2_cluster_data(cluster)->ipdata;
537 nip->inum = nipdata->inum;
538 nip->size = nipdata->size;
539 nip->mtime = nipdata->mtime;
540 hammer2_inode_repoint(nip, NULL, cluster);
541 nip->pip = dip; /* can be NULL */
543 hammer2_inode_ref(dip); /* ref dip for nip->pip */
548 * ref and lock on nip gives it state compatible to after a
549 * hammer2_inode_lock_ex() call.
552 ccms_cst_init(&nip->topo_cst, &nip->cluster);
553 ccms_thread_lock(&nip->topo_cst, CCMS_STATE_EXCLUSIVE);
554 /* combination of thread lock and chain lock == inode lock */
557 * Attempt to add the inode. If it fails we raced another inode
558 * get. Undo all the work and try again.
560 if (pmp->spmp_hmp == NULL) {
561 spin_lock(&pmp->inum_spin);
562 if (RB_INSERT(hammer2_inode_tree, &pmp->inum_tree, nip)) {
563 spin_unlock(&pmp->inum_spin);
564 ccms_thread_unlock(&nip->topo_cst);
565 hammer2_inode_drop(nip);
568 atomic_set_int(&nip->flags, HAMMER2_INODE_ONRBTREE);
569 spin_unlock(&pmp->inum_spin);
576 * Create a new inode in the specified directory using the vattr to
577 * figure out the type of inode.
579 * If no error occurs the new inode with its cluster locked is returned in
580 * *nipp, otherwise an error is returned and *nipp is set to NULL.
582 * If vap and/or cred are NULL the related fields are not set and the
583 * inode type defaults to a directory. This is used when creating PFSs
584 * under the super-root, so the inode number is set to 1 in this case.
586 * dip is not locked on entry.
588 * NOTE: When used to create a snapshot, the inode is temporarily associated
589 * with the super-root spmp. XXX should pass new pmp for snapshot.
592 hammer2_inode_create(hammer2_trans_t *trans, hammer2_inode_t *dip,
593 struct vattr *vap, struct ucred *cred,
594 const uint8_t *name, size_t name_len,
595 hammer2_cluster_t **clusterp, int *errorp)
597 const hammer2_inode_data_t *dipdata;
598 hammer2_inode_data_t *nipdata;
599 hammer2_cluster_t *cluster;
600 hammer2_cluster_t *cparent;
601 hammer2_inode_t *nip;
602 hammer2_key_t key_dummy;
612 lhc = hammer2_dirhash(name, name_len);
616 * Locate the inode or indirect block to create the new
617 * entry in. At the same time check for key collisions
618 * and iterate until we don't get one.
620 * NOTE: hidden inodes do not have iterators.
623 cparent = hammer2_inode_lock_ex(dip);
624 dipdata = &hammer2_cluster_data(cparent)->ipdata;
625 dip_uid = dipdata->uid;
626 dip_gid = dipdata->gid;
627 dip_mode = dipdata->mode;
628 dip_algo = dipdata->comp_algo;
632 cluster = hammer2_cluster_lookup(cparent, &key_dummy,
633 lhc, lhc, 0, &ddflag);
636 if ((lhc & HAMMER2_DIRHASH_VISIBLE) == 0)
638 if ((lhc & HAMMER2_DIRHASH_LOMASK) == HAMMER2_DIRHASH_LOMASK)
640 hammer2_cluster_unlock(cluster);
646 error = hammer2_cluster_create(trans, cparent, &cluster,
648 HAMMER2_BREF_TYPE_INODE,
653 kprintf("CREATE INODE %*.*s chain=%p\n",
654 (int)name_len, (int)name_len, name,
655 (cluster ? cluster->focus : NULL));
659 * Cleanup and handle retries.
661 if (error == EAGAIN) {
662 hammer2_cluster_ref(cparent);
663 hammer2_inode_unlock_ex(dip, cparent);
664 hammer2_cluster_wait(cparent);
665 hammer2_cluster_drop(cparent);
668 hammer2_inode_unlock_ex(dip, cparent);
672 KKASSERT(cluster == NULL);
678 * Set up the new inode.
680 * NOTE: *_get() integrates chain's lock into the inode lock.
682 * NOTE: Only one new inode can currently be created per
683 * transaction. If the need arises we can adjust
684 * hammer2_trans_init() to allow more.
686 * NOTE: nipdata will have chain's blockset data.
688 KKASSERT(cluster->focus->flags & HAMMER2_CHAIN_MODIFIED);
689 nipdata = &hammer2_cluster_wdata(cluster)->ipdata;
690 nipdata->inum = trans->inode_tid;
691 hammer2_cluster_modsync(cluster);
692 nip = hammer2_inode_get(dip->pmp, dip, cluster);
693 nipdata = &hammer2_cluster_wdata(cluster)->ipdata;
696 KKASSERT(trans->inodes_created == 0);
697 nipdata->type = hammer2_get_obj_type(vap->va_type);
698 nipdata->inum = trans->inode_tid;
699 ++trans->inodes_created;
701 switch (nipdata->type) {
702 case HAMMER2_OBJTYPE_CDEV:
703 case HAMMER2_OBJTYPE_BDEV:
704 nipdata->rmajor = vap->va_rmajor;
705 nipdata->rminor = vap->va_rminor;
711 nipdata->type = HAMMER2_OBJTYPE_DIRECTORY;
715 /* Inherit parent's inode compression mode. */
716 nip->comp_heuristic = 0;
717 nipdata->comp_algo = dip_algo;
718 nipdata->version = HAMMER2_INODE_VERSION_ONE;
719 hammer2_update_time(&nipdata->ctime);
720 nipdata->mtime = nipdata->ctime;
722 nipdata->mode = vap->va_mode;
725 if (dip && dip->pmp) {
726 xuid = hammer2_to_unix_xid(&dip_uid);
727 xuid = vop_helper_create_uid(dip->pmp->mp,
733 /* super-root has no dip and/or pmp */
736 if (vap->va_vaflags & VA_UID_UUID_VALID)
737 nipdata->uid = vap->va_uid_uuid;
738 else if (vap->va_uid != (uid_t)VNOVAL)
739 hammer2_guid_to_uuid(&nipdata->uid, vap->va_uid);
741 hammer2_guid_to_uuid(&nipdata->uid, xuid);
743 if (vap->va_vaflags & VA_GID_UUID_VALID)
744 nipdata->gid = vap->va_gid_uuid;
745 else if (vap->va_gid != (gid_t)VNOVAL)
746 hammer2_guid_to_uuid(&nipdata->gid, vap->va_gid);
748 nipdata->gid = dip_gid;
752 * Regular files and softlinks allow a small amount of data to be
753 * directly embedded in the inode. This flag will be cleared if
754 * the size is extended past the embedded limit.
756 if (nipdata->type == HAMMER2_OBJTYPE_REGFILE ||
757 nipdata->type == HAMMER2_OBJTYPE_SOFTLINK) {
758 nipdata->op_flags |= HAMMER2_OPFLAG_DIRECTDATA;
761 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
762 bcopy(name, nipdata->filename, name_len);
763 nipdata->name_key = lhc;
764 nipdata->name_len = name_len;
765 hammer2_cluster_modsync(cluster);
772 * The cluster has been removed from the original directory and replaced
773 * with a hardlink pointer. Move the cluster to the specified parent
774 * directory, change the filename to "0xINODENUMBER", and adjust the key.
775 * The cluster becomes our invisible hardlink target.
777 * The original cluster must be deleted on entry.
781 hammer2_hardlink_shiftup(hammer2_trans_t *trans, hammer2_cluster_t *cluster,
782 hammer2_inode_t *dip, hammer2_cluster_t *dcluster,
783 int nlinks, int *errorp)
785 const hammer2_inode_data_t *iptmp;
786 hammer2_inode_data_t *nipdata;
787 hammer2_cluster_t *xcluster;
788 hammer2_key_t key_dummy;
790 hammer2_blockref_t bref;
793 iptmp = &hammer2_cluster_data(cluster)->ipdata;
795 KKASSERT((lhc & HAMMER2_DIRHASH_VISIBLE) == 0);
798 * Locate the inode or indirect block to create the new
799 * entry in. lhc represents the inode number so there is
800 * no collision iteration.
802 * There should be no key collisions with invisible inode keys.
804 * WARNING! Must use inode_lock_ex() on dip to handle a stale
805 * dip->cluster cache.
808 xcluster = hammer2_cluster_lookup(dcluster, &key_dummy,
809 lhc, lhc, 0, &ddflag);
811 kprintf("X3 chain %p dip %p dchain %p dip->chain %p\n",
812 xcluster->focus, dip, dcluster->focus,
814 hammer2_cluster_unlock(xcluster);
823 * Handle the error case
827 KKASSERT(xcluster == NULL);
832 * Use xcluster as a placeholder for (lhc). Duplicate cluster to the
833 * same target bref as xcluster and then delete xcluster. The
834 * duplication occurs after xcluster in flush order even though
835 * xcluster is deleted after the duplication. XXX
837 * WARNING! Duplications (to a different parent) can cause indirect
838 * blocks to be inserted, refactor xcluster.
840 * WARNING! Only key and keybits is extracted from a passed-in bref.
842 hammer2_cluster_bref(cluster, &bref);
843 bref.key = lhc; /* invisible dir entry key */
845 hammer2_cluster_rename(trans, &bref, dcluster, cluster, 0);
848 * cluster is now 'live' again.. adjust the filename.
850 * Directory entries are inodes but this is a hidden hardlink
851 * target. The name isn't used but to ease debugging give it
852 * a name after its inode number.
854 hammer2_cluster_modify(trans, cluster, 0);
855 nipdata = &hammer2_cluster_wdata(cluster)->ipdata;
856 ksnprintf(nipdata->filename, sizeof(nipdata->filename),
857 "0x%016jx", (intmax_t)nipdata->inum);
858 nipdata->name_len = strlen(nipdata->filename);
859 nipdata->name_key = lhc;
860 nipdata->nlinks += nlinks;
861 hammer2_cluster_modsync(cluster);
865 * Connect the target inode represented by (cluster) to the media topology
866 * at (dip, name, len). The caller can pass a rough *chainp, this function
867 * will issue lookup()s to position the parent chain properly for the
870 * If hlink is TRUE this function creates an OBJTYPE_HARDLINK directory
871 * entry instead of connecting (cluster).
873 * If hlink is FALSE this function expects (cluster) to be unparented.
876 hammer2_inode_connect(hammer2_trans_t *trans,
877 hammer2_cluster_t **clusterp, int hlink,
878 hammer2_inode_t *dip, hammer2_cluster_t *dcluster,
879 const uint8_t *name, size_t name_len,
882 hammer2_inode_data_t *wipdata;
883 hammer2_cluster_t *ocluster;
884 hammer2_cluster_t *ncluster;
885 hammer2_key_t key_dummy;
890 * Since ocluster is either disconnected from the topology or
891 * represents a hardlink terminus which is always a parent of or
892 * equal to dip, we should be able to safely lock dip->chain for
895 * WARNING! Must use inode_lock_ex() on dip to handle a stale
898 * If name is non-NULL we calculate lhc, else we use the passed-in
901 ocluster = *clusterp;
904 lhc = hammer2_dirhash(name, name_len);
907 * Locate the inode or indirect block to create the new
908 * entry in. At the same time check for key collisions
909 * and iterate until we don't get one.
913 ncluster = hammer2_cluster_lookup(dcluster, &key_dummy,
916 if (ncluster == NULL)
918 if ((lhc & HAMMER2_DIRHASH_LOMASK) ==
919 HAMMER2_DIRHASH_LOMASK) {
922 hammer2_cluster_unlock(ncluster);
928 * Reconnect to specific key (used when moving
929 * unlinked-but-open files into the hidden directory).
931 ncluster = hammer2_cluster_lookup(dcluster, &key_dummy,
934 KKASSERT(ncluster == NULL);
940 * Hardlink pointer needed, create totally fresh
943 * We must refactor ocluster because it might have
944 * been shifted into an indirect cluster by the
947 KKASSERT(ncluster == NULL);
948 error = hammer2_cluster_create(trans,
951 HAMMER2_BREF_TYPE_INODE,
956 * Reconnect the original cluster under the new name.
957 * Original cluster must have already been deleted by
960 * WARNING! Can cause held-over clusters to require a
961 * refactor. Fortunately we have none (our
962 * locked clusters are passed into and
963 * modified by the call).
967 error = hammer2_cluster_create(trans,
970 HAMMER2_BREF_TYPE_INODE,
979 KKASSERT(error != EAGAIN);
982 * ncluster should be NULL on error, leave ocluster
983 * (ocluster == *clusterp) alone.
986 KKASSERT(ncluster == NULL);
991 * Directory entries are inodes so if the name has changed we have
992 * to update the inode.
994 * When creating an OBJTYPE_HARDLINK entry remember to unlock the
995 * cluster, the caller will access the hardlink via the actual hardlink
996 * target file and not the hardlink pointer entry, so we must still
999 if (hlink && hammer2_hardlink_enable >= 0) {
1001 * Create the HARDLINK pointer. oip represents the hardlink
1002 * target in this situation.
1004 * We will return ocluster (the hardlink target).
1006 hammer2_cluster_modify(trans, ncluster, 0);
1007 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
1008 wipdata = &hammer2_cluster_wdata(ncluster)->ipdata;
1009 bcopy(name, wipdata->filename, name_len);
1010 wipdata->name_key = lhc;
1011 wipdata->name_len = name_len;
1012 wipdata->target_type =
1013 hammer2_cluster_data(ocluster)->ipdata.type;
1014 wipdata->type = HAMMER2_OBJTYPE_HARDLINK;
1015 wipdata->inum = hammer2_cluster_data(ocluster)->ipdata.inum;
1016 wipdata->nlinks = 1;
1017 hammer2_cluster_modsync(ncluster);
1018 hammer2_cluster_unlock(ncluster);
1019 ncluster = ocluster;
1023 * ncluster is a duplicate of ocluster at the new location.
1024 * We must fixup the name stored in the inode data.
1025 * The bref key has already been adjusted by inode_connect().
1027 hammer2_cluster_modify(trans, ncluster, 0);
1028 wipdata = &hammer2_cluster_wdata(ncluster)->ipdata;
1030 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
1031 bcopy(name, wipdata->filename, name_len);
1032 wipdata->name_key = lhc;
1033 wipdata->name_len = name_len;
1034 wipdata->nlinks = 1;
1035 hammer2_cluster_modsync(ncluster);
1039 * We are replacing ocluster with ncluster, unlock ocluster. In the
1040 * case where ocluster is left unchanged the code above sets
1041 * ncluster to ocluster and ocluster to NULL, resulting in a NOP here.
1044 hammer2_cluster_unlock(ocluster);
1045 *clusterp = ncluster;
1051 * Repoint ip->cluster's chains to cluster's chains. Caller must hold
1052 * the inode exclusively locked. cluster may be NULL to clean out any
1053 * chains in ip->cluster.
1056 hammer2_inode_repoint(hammer2_inode_t *ip, hammer2_inode_t *pip,
1057 hammer2_cluster_t *cluster)
1059 hammer2_chain_t *ochain;
1060 hammer2_chain_t *nchain;
1061 hammer2_inode_t *opip;
1065 * Replace chains in ip->cluster with chains from cluster and
1066 * adjust the focus if necessary.
1068 * NOTE: nchain and/or ochain can be NULL due to gaps
1069 * in the cluster arrays.
1071 ip->cluster.focus = NULL;
1072 for (i = 0; cluster && i < cluster->nchains; ++i) {
1073 nchain = cluster->array[i];
1074 if (i < ip->cluster.nchains) {
1075 ochain = ip->cluster.array[i];
1076 if (ochain == nchain) {
1077 if (ip->cluster.focus == NULL)
1078 ip->cluster.focus = nchain;
1088 ip->cluster.array[i] = nchain;
1089 if (ip->cluster.focus == NULL)
1090 ip->cluster.focus = nchain;
1092 hammer2_chain_ref(nchain);
1094 hammer2_chain_drop(ochain);
1098 * Release any left-over chains in ip->cluster.
1100 while (i < ip->cluster.nchains) {
1101 nchain = ip->cluster.array[i];
1103 ip->cluster.array[i] = NULL;
1104 hammer2_chain_drop(nchain);
1108 ip->cluster.nchains = cluster ? cluster->nchains : 0;
1111 * Repoint ip->pip if requested (non-NULL pip).
1113 if (pip && ip->pip != pip) {
1115 hammer2_inode_ref(pip);
1118 hammer2_inode_drop(opip);
1123 * Unlink the file from the specified directory inode. The directory inode
1124 * does not need to be locked.
1126 * isdir determines whether a directory/non-directory check should be made.
1127 * No check is made if isdir is set to -1.
1129 * isopen specifies whether special unlink-with-open-descriptor handling
1130 * must be performed. If set to -1 the caller is deleting a PFS and we
1131 * check whether the chain is mounted or not (chain->pmp != NULL). 1 is
1132 * implied if it is mounted.
1134 * If isopen is 1 and nlinks drops to 0 this function must move the chain
1135 * to a special hidden directory until last-close occurs on the file.
1137 * NOTE! The underlying file can still be active with open descriptors
1138 * or if the chain is being manually held (e.g. for rename).
1140 * The caller is responsible for fixing up ip->chain if e.g. a
1141 * rename occurs (see chain_duplicate()).
1143 * NOTE! The chain is not deleted if it is moved to the hidden directory,
1144 * but otherwise will be deleted.
1147 hammer2_unlink_file(hammer2_trans_t *trans, hammer2_inode_t *dip,
1148 const uint8_t *name, size_t name_len,
1149 int isdir, int *hlinkp, struct nchandle *nch,
1152 const hammer2_inode_data_t *ripdata;
1153 hammer2_inode_data_t *wipdata;
1154 hammer2_cluster_t *cparent;
1155 hammer2_cluster_t *hcluster;
1156 hammer2_cluster_t *hparent;
1157 hammer2_cluster_t *cluster;
1158 hammer2_cluster_t *dparent;
1159 hammer2_cluster_t *dcluster;
1160 hammer2_key_t key_dummy;
1161 hammer2_key_t key_next;
1172 lhc = hammer2_dirhash(name, name_len);
1176 * Search for the filename in the directory
1178 cparent = hammer2_inode_lock_ex(dip);
1179 cluster = hammer2_cluster_lookup(cparent, &key_next,
1180 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1183 if (hammer2_cluster_type(cluster) == HAMMER2_BREF_TYPE_INODE) {
1184 ripdata = &hammer2_cluster_data(cluster)->ipdata;
1185 if (ripdata->name_len == name_len &&
1186 bcmp(ripdata->filename, name, name_len) == 0) {
1190 cluster = hammer2_cluster_next(cparent, cluster, &key_next,
1192 lhc + HAMMER2_DIRHASH_LOMASK,
1195 hammer2_inode_unlock_ex(dip, NULL); /* retain cparent */
1198 * Not found or wrong type (isdir < 0 disables the type check).
1199 * If a hardlink pointer, type checks use the hardlink target.
1201 if (cluster == NULL) {
1205 ripdata = &hammer2_cluster_data(cluster)->ipdata;
1206 type = ripdata->type;
1207 if (type == HAMMER2_OBJTYPE_HARDLINK) {
1209 type = ripdata->target_type;
1212 if (type == HAMMER2_OBJTYPE_DIRECTORY && isdir == 0) {
1216 if (type != HAMMER2_OBJTYPE_DIRECTORY && isdir >= 1) {
1222 * Hardlink must be resolved. We can't hold the parent locked
1223 * while we do this or we could deadlock. The physical file will
1224 * be located at or above the current directory.
1226 * We loop to reacquire the hardlink origination.
1228 * NOTE: hammer2_hardlink_find() will locate the hardlink target,
1229 * returning a modified hparent and hcluster.
1231 if (ripdata->type == HAMMER2_OBJTYPE_HARDLINK) {
1232 if (hcluster == NULL) {
1234 cluster = NULL; /* safety */
1235 hammer2_cluster_unlock(cparent);
1236 cparent = NULL; /* safety */
1237 ripdata = NULL; /* safety (associated w/cparent) */
1238 error = hammer2_hardlink_find(dip, &hparent, hcluster);
1241 * If we couldn't find the hardlink target then some
1242 * parent directory containing the hardlink pointer
1243 * probably got renamed to above the original target,
1244 * a case not yet handled by H2.
1247 kprintf("H2 unlink_file: hardlink target for "
1248 "\"%s\" not found\n",
1250 kprintf("(likely due to known directory "
1259 * If this is a directory the directory must be empty. However, if
1260 * isdir < 0 we are doing a rename and the directory does not have
1261 * to be empty, and if isdir > 1 we are deleting a PFS/snapshot
1262 * and the directory does not have to be empty.
1264 * NOTE: We check the full key range here which covers both visible
1265 * and invisible entries. Theoretically there should be no
1266 * invisible (hardlink target) entries if there are no visible
1269 if (type == HAMMER2_OBJTYPE_DIRECTORY && isdir == 1) {
1270 dparent = hammer2_cluster_lookup_init(cluster, 0);
1271 dcluster = hammer2_cluster_lookup(dparent, &key_dummy,
1272 0, (hammer2_key_t)-1,
1273 HAMMER2_LOOKUP_NODATA,
1276 hammer2_cluster_unlock(dcluster);
1277 hammer2_cluster_lookup_done(dparent);
1281 hammer2_cluster_lookup_done(dparent);
1287 * If this was a hardlink then (cparent, cluster) is the hardlink
1288 * pointer, which we can simply destroy outright. Discard the
1289 * clusters and replace with the hardlink target.
1292 hammer2_cluster_delete(trans, cparent, cluster,
1293 HAMMER2_DELETE_PERMANENT);
1294 hammer2_cluster_unlock(cparent);
1295 hammer2_cluster_unlock(cluster);
1303 * This leaves us with the hardlink target or non-hardlinked file
1304 * or directory in (cparent, cluster).
1306 * Delete the target when nlinks reaches 0 with special handling
1307 * if (isopen) is set.
1309 * NOTE! In DragonFly the vnops function calls cache_unlink() after
1310 * calling us here to clean out the namecache association,
1311 * (which does not represent a ref for the open-test), and to
1312 * force finalization of the vnode if/when the last ref gets
1315 * NOTE! Files are unlinked by rename and then relinked. nch will be
1316 * passed as NULL in this situation. hammer2_inode_connect()
1319 KKASSERT(cluster != NULL);
1320 hammer2_cluster_modify(trans, cluster, 0);
1321 wipdata = &hammer2_cluster_wdata(cluster)->ipdata;
1323 wipdata->nlinks += nlinks;
1324 if ((int64_t)wipdata->nlinks < 0) { /* XXX debugging */
1325 wipdata->nlinks = 0;
1327 hammer2_cluster_modsync(cluster);
1329 if (wipdata->nlinks == 0) {
1331 * Target nlinks has reached 0, file now unlinked (but may
1334 if ((cluster->focus->flags & HAMMER2_CHAIN_PFSROOT) &&
1337 kprintf("hammer2: PFS \"%s\" cannot be deleted "
1338 "while still mounted\n",
1342 if (nch && cache_isopen(nch)) {
1343 hammer2_inode_move_to_hidden(trans, &cparent, &cluster,
1347 * This won't get everything if a vnode is still
1348 * present, but the cache_unlink() call the caller
1351 hammer2_cluster_delete(trans, cparent, cluster,
1352 HAMMER2_DELETE_PERMANENT);
1354 } else if (hlink == 0) {
1356 * In this situation a normal non-hardlinked file (which can
1357 * only have nlinks == 1) still has a non-zero nlinks, the
1358 * caller must be doing a RENAME operation and so is passing
1359 * a nlinks adjustment of 0, and only wishes to remove file
1360 * in order to be able to reconnect it under a different name.
1362 * In this situation we do a non-permanent deletion of the
1363 * chain in order to allow the file to be reconnected in
1364 * a different location.
1366 KKASSERT(nlinks == 0);
1367 hammer2_cluster_delete(trans, cparent, cluster, 0);
1372 hammer2_cluster_unlock(cparent);
1374 hammer2_cluster_unlock(cluster);
1376 hammer2_cluster_unlock(hparent);
1378 hammer2_cluster_unlock(hcluster);
1386 * This is called from the mount code to initialize pmp->ihidden
1389 hammer2_inode_install_hidden(hammer2_pfsmount_t *pmp)
1391 hammer2_trans_t trans;
1392 hammer2_cluster_t *cparent;
1393 hammer2_cluster_t *cluster;
1394 hammer2_cluster_t *scan;
1395 hammer2_inode_data_t *wipdata;
1396 hammer2_key_t key_dummy;
1397 hammer2_key_t key_next;
1406 * Find the hidden directory
1408 bzero(&key_dummy, sizeof(key_dummy));
1409 hammer2_trans_init(&trans, pmp, 0);
1411 cparent = hammer2_inode_lock_ex(pmp->iroot);
1412 cluster = hammer2_cluster_lookup(cparent, &key_dummy,
1413 HAMMER2_INODE_HIDDENDIR,
1414 HAMMER2_INODE_HIDDENDIR,
1417 pmp->ihidden = hammer2_inode_get(pmp, pmp->iroot, cluster);
1418 hammer2_inode_ref(pmp->ihidden);
1421 * Remove any unlinked files which were left open as-of
1424 * Don't pass NODATA, we need the inode data so the delete
1425 * can do proper statistics updates.
1428 scan = hammer2_cluster_lookup(cluster, &key_next,
1432 if (hammer2_cluster_type(scan) ==
1433 HAMMER2_BREF_TYPE_INODE) {
1434 hammer2_cluster_delete(&trans, cluster, scan,
1435 HAMMER2_DELETE_PERMANENT);
1438 scan = hammer2_cluster_next(cluster, scan, &key_next,
1439 0, HAMMER2_TID_MAX, 0);
1442 hammer2_inode_unlock_ex(pmp->ihidden, cluster);
1443 hammer2_inode_unlock_ex(pmp->iroot, cparent);
1444 hammer2_trans_done(&trans);
1445 kprintf("hammer2: PFS loaded hidden dir, "
1446 "removed %d dead entries\n", count);
1451 * Create the hidden directory
1453 error = hammer2_cluster_create(&trans, cparent, &cluster,
1454 HAMMER2_INODE_HIDDENDIR, 0,
1455 HAMMER2_BREF_TYPE_INODE,
1456 HAMMER2_INODE_BYTES,
1458 hammer2_inode_unlock_ex(pmp->iroot, cparent);
1460 hammer2_cluster_modify(&trans, cluster, 0);
1461 wipdata = &hammer2_cluster_wdata(cluster)->ipdata;
1462 wipdata->type = HAMMER2_OBJTYPE_DIRECTORY;
1463 wipdata->inum = HAMMER2_INODE_HIDDENDIR;
1464 wipdata->nlinks = 1;
1465 hammer2_cluster_modsync(cluster);
1466 kprintf("hammer2: PFS root missing hidden directory, creating\n");
1468 pmp->ihidden = hammer2_inode_get(pmp, pmp->iroot, cluster);
1469 hammer2_inode_ref(pmp->ihidden);
1470 hammer2_inode_unlock_ex(pmp->ihidden, cluster);
1471 hammer2_trans_done(&trans);
1475 * If an open file is unlinked H2 needs to retain the file in the topology
1476 * to ensure that its backing store is not recovered by the bulk free scan.
1477 * This also allows us to avoid having to special-case the CHAIN_DELETED flag.
1479 * To do this the file is moved to a hidden directory in the PFS root and
1480 * renamed. The hidden directory must be created if it does not exist.
1484 hammer2_inode_move_to_hidden(hammer2_trans_t *trans,
1485 hammer2_cluster_t **cparentp,
1486 hammer2_cluster_t **clusterp,
1489 hammer2_cluster_t *dcluster;
1490 hammer2_pfsmount_t *pmp;
1493 pmp = (*clusterp)->pmp;
1494 KKASSERT(pmp != NULL);
1495 KKASSERT(pmp->ihidden != NULL);
1497 hammer2_cluster_delete(trans, *cparentp, *clusterp, 0);
1498 dcluster = hammer2_inode_lock_ex(pmp->ihidden);
1499 error = hammer2_inode_connect(trans, clusterp, 0,
1500 pmp->ihidden, dcluster,
1502 hammer2_inode_unlock_ex(pmp->ihidden, dcluster);
1503 KKASSERT(error == 0);
1507 * Given an exclusively locked inode and cluster we consolidate the cluster
1508 * for hardlink creation, adding (nlinks) to the file's link count and
1509 * potentially relocating the inode to (cdip) which is a parent directory
1510 * common to both the current location of the inode and the intended new
1513 * Replaces (*clusterp) if consolidation occurred, unlocking the old cluster
1514 * and returning a new locked cluster.
1516 * NOTE! This function will also replace ip->cluster.
1519 hammer2_hardlink_consolidate(hammer2_trans_t *trans,
1520 hammer2_inode_t *ip,
1521 hammer2_cluster_t **clusterp,
1522 hammer2_inode_t *cdip,
1523 hammer2_cluster_t *cdcluster,
1526 const hammer2_inode_data_t *ripdata;
1527 hammer2_inode_data_t *wipdata;
1528 hammer2_cluster_t *cluster;
1529 hammer2_cluster_t *cparent;
1532 cluster = *clusterp;
1533 ripdata = &hammer2_cluster_data(cluster)->ipdata;
1534 if (nlinks == 0 && /* no hardlink needed */
1535 (ripdata->name_key & HAMMER2_DIRHASH_VISIBLE)) {
1539 if (hammer2_hardlink_enable == 0) { /* disallow hardlinks */
1540 hammer2_cluster_unlock(cluster);
1548 * If no change in the hardlink's target directory is required and
1549 * this is already a hardlink target, all we need to do is adjust
1552 ripdata = &hammer2_cluster_data(cluster)->ipdata;
1553 if (cdip == ip->pip &&
1554 (ripdata->name_key & HAMMER2_DIRHASH_VISIBLE) == 0) {
1556 hammer2_cluster_modify(trans, cluster, 0);
1557 wipdata = &hammer2_cluster_wdata(cluster)->ipdata;
1558 wipdata->nlinks += nlinks;
1559 hammer2_cluster_modsync(cluster);
1567 * Cluster is the real inode. The originating directory is locked
1568 * by the caller so we can manipulate it without worrying about races
1569 * against other lookups.
1571 * If cluster is visible we need to delete it from the current
1572 * location and create a hardlink pointer in its place. If it is
1573 * not visible we need only delete it. Then later cluster will be
1574 * renamed to a parent directory and converted (if necessary) to
1575 * a hidden inode (via shiftup).
1577 * NOTE! We must hold cparent locked through the delete/create/rename
1578 * operation to ensure that other threads block resolving to
1579 * the same hardlink, otherwise the other threads may not see
1582 KKASSERT((cluster->focus->flags & HAMMER2_CHAIN_DELETED) == 0);
1583 cparent = hammer2_cluster_parent(cluster);
1585 hammer2_cluster_delete(trans, cparent, cluster, 0);
1587 ripdata = &hammer2_cluster_data(cluster)->ipdata;
1588 KKASSERT(ripdata->type != HAMMER2_OBJTYPE_HARDLINK);
1589 if (ripdata->name_key & HAMMER2_DIRHASH_VISIBLE) {
1590 hammer2_cluster_t *ncluster;
1594 lhc = cluster->focus->bref.key;
1595 error = hammer2_cluster_create(trans, cparent, &ncluster,
1597 HAMMER2_BREF_TYPE_INODE,
1598 HAMMER2_INODE_BYTES,
1600 hammer2_cluster_modify(trans, ncluster, 0);
1601 wipdata = &hammer2_cluster_wdata(ncluster)->ipdata;
1603 wipdata->comp_algo = ripdata->comp_algo;
1604 wipdata->version = HAMMER2_INODE_VERSION_ONE;
1605 wipdata->inum = ripdata->inum;
1606 wipdata->target_type = ripdata->type;
1607 wipdata->type = HAMMER2_OBJTYPE_HARDLINK;
1608 wipdata->uflags = 0;
1609 wipdata->rmajor = 0;
1610 wipdata->rminor = 0;
1615 bzero(&wipdata->uid, sizeof(wipdata->uid));
1616 bzero(&wipdata->gid, sizeof(wipdata->gid));
1617 wipdata->op_flags = HAMMER2_OPFLAG_DIRECTDATA;
1618 wipdata->cap_flags = 0;
1621 wipdata->nlinks = 1;
1622 wipdata->iparent = 0; /* XXX */
1623 wipdata->pfs_type = 0;
1624 wipdata->pfs_inum = 0;
1625 bzero(&wipdata->pfs_clid, sizeof(wipdata->pfs_clid));
1626 bzero(&wipdata->pfs_fsid, sizeof(wipdata->pfs_fsid));
1627 wipdata->data_quota = 0;
1628 wipdata->data_count = 0;
1629 wipdata->inode_quota = 0;
1630 wipdata->inode_count = 0;
1631 wipdata->attr_tid = 0;
1632 wipdata->dirent_tid = 0;
1633 bzero(&wipdata->u, sizeof(wipdata->u));
1634 bcopy(ripdata->filename, wipdata->filename, ripdata->name_len);
1635 wipdata->name_key = ncluster->focus->bref.key;
1636 wipdata->name_len = ripdata->name_len;
1637 /* XXX transaction ids */
1638 hammer2_cluster_modsync(ncluster);
1639 hammer2_cluster_unlock(ncluster);
1644 * cluster represents the hardlink target and is now flagged deleted.
1645 * duplicate it to the parent directory and adjust nlinks.
1647 * WARNING! The shiftup() call can cause ncluster to be moved into
1648 * an indirect block, and our ncluster will wind up pointing
1649 * to the older/original version.
1651 KKASSERT(cluster->focus->flags & HAMMER2_CHAIN_DELETED);
1652 hammer2_hardlink_shiftup(trans, cluster, cdip, cdcluster,
1656 hammer2_inode_repoint(ip, cdip, cluster);
1660 * Cleanup, cluster/ncluster already dealt with.
1662 * Return the shifted cluster in *clusterp.
1665 hammer2_cluster_unlock(cparent);
1666 *clusterp = cluster;
1672 * If (*ochainp) is non-NULL it points to the forward OBJTYPE_HARDLINK
1673 * inode while (*chainp) points to the resolved (hidden hardlink
1674 * target) inode. In this situation when nlinks is 1 we wish to
1675 * deconsolidate the hardlink, moving it back to the directory that now
1676 * represents the only remaining link.
1679 hammer2_hardlink_deconsolidate(hammer2_trans_t *trans,
1680 hammer2_inode_t *dip,
1681 hammer2_chain_t **chainp,
1682 hammer2_chain_t **ochainp)
1684 if (*ochainp == NULL)
1691 * The caller presents a locked cluster with an obj_type of
1692 * HAMMER2_OBJTYPE_HARDLINK. This routine will locate and replace the
1693 * cluster with the target hardlink, also locked.
1695 * If cparentp is not NULL a locked cluster representing the hardlink's
1696 * parent is also returned.
1698 * If we are unable to locate the hardlink target EIO is returned and
1699 * (*cparentp) is set to NULL. The passed-in cluster still needs to be
1700 * unlocked by the caller but will be degenerate... not have any chains.
1703 hammer2_hardlink_find(hammer2_inode_t *dip,
1704 hammer2_cluster_t **cparentp, hammer2_cluster_t *cluster)
1706 const hammer2_inode_data_t *ipdata;
1707 hammer2_cluster_t *cparent;
1708 hammer2_cluster_t *rcluster;
1709 hammer2_inode_t *ip;
1710 hammer2_inode_t *pip;
1711 hammer2_key_t key_dummy;
1716 hammer2_inode_ref(pip); /* for loop */
1719 * Locate the hardlink. pip is referenced and not locked.
1721 ipdata = &hammer2_cluster_data(cluster)->ipdata;
1725 * We don't need the cluster's chains, but we need to retain the
1726 * cluster structure itself so we can load the hardlink search
1729 KKASSERT(cluster->refs == 1);
1730 atomic_add_int(&cluster->refs, 1);
1731 hammer2_cluster_unlock(cluster); /* hack */
1732 cluster->nchains = 0; /* hack */
1737 while ((ip = pip) != NULL) {
1738 cparent = hammer2_inode_lock_ex(ip);
1739 hammer2_inode_drop(ip); /* loop */
1740 KKASSERT(hammer2_cluster_type(cparent) ==
1741 HAMMER2_BREF_TYPE_INODE);
1742 rcluster = hammer2_cluster_lookup(cparent, &key_dummy,
1743 lhc, lhc, 0, &ddflag);
1746 hammer2_cluster_lookup_done(cparent); /* discard parent */
1747 cparent = NULL; /* safety */
1748 pip = ip->pip; /* safe, ip held locked */
1750 hammer2_inode_ref(pip); /* loop */
1751 hammer2_inode_unlock_ex(ip, NULL);
1755 * chain is locked, ip is locked. Unlock ip, return the locked
1756 * chain. *ipp is already set w/a ref count and not locked.
1758 * (cparent is already unlocked).
1761 hammer2_cluster_replace(cluster, rcluster);
1762 hammer2_cluster_drop(rcluster);
1764 *cparentp = cparent;
1765 hammer2_inode_unlock_ex(ip, NULL);
1767 hammer2_inode_unlock_ex(ip, cparent);
1774 hammer2_inode_unlock_ex(ip, cparent);
1780 * Find the directory common to both fdip and tdip.
1782 * Returns a held but not locked inode. Caller typically locks the inode,
1783 * and when through unlocks AND drops it.
1786 hammer2_inode_common_parent(hammer2_inode_t *fdip, hammer2_inode_t *tdip)
1788 hammer2_inode_t *scan1;
1789 hammer2_inode_t *scan2;
1792 * We used to have a depth field but it complicated matters too
1793 * much for directory renames. So now its ugly. Check for
1794 * simple cases before giving up and doing it the expensive way.
1796 * XXX need a bottom-up topology stability lock
1798 if (fdip == tdip || fdip == tdip->pip) {
1799 hammer2_inode_ref(fdip);
1802 if (fdip->pip == tdip) {
1803 hammer2_inode_ref(tdip);
1810 for (scan1 = fdip; scan1->pmp == fdip->pmp; scan1 = scan1->pip) {
1812 while (scan2->pmp == tdip->pmp) {
1813 if (scan1 == scan2) {
1814 hammer2_inode_ref(scan1);
1822 panic("hammer2_inode_common_parent: no common parent %p %p\n",
1829 * Synchronize the inode's frontend state with the chain state prior
1830 * to any explicit flush of the inode or any strategy write call.
1832 * Called with a locked inode.
1835 hammer2_inode_fsync(hammer2_trans_t *trans, hammer2_inode_t *ip,
1836 hammer2_cluster_t *cparent)
1838 const hammer2_inode_data_t *ripdata;
1839 hammer2_inode_data_t *wipdata;
1840 hammer2_cluster_t *dparent;
1841 hammer2_cluster_t *cluster;
1842 hammer2_key_t lbase;
1843 hammer2_key_t key_next;
1847 ripdata = &hammer2_cluster_data(cparent)->ipdata; /* target file */
1849 if (ip->flags & HAMMER2_INODE_MTIME) {
1850 wipdata = hammer2_cluster_modify_ip(trans, ip, cparent, 0);
1851 atomic_clear_int(&ip->flags, HAMMER2_INODE_MTIME);
1852 wipdata->mtime = ip->mtime;
1856 if ((ip->flags & HAMMER2_INODE_RESIZED) && ip->size < ripdata->size) {
1857 wipdata = hammer2_cluster_modify_ip(trans, ip, cparent, 0);
1858 wipdata->size = ip->size;
1861 atomic_clear_int(&ip->flags, HAMMER2_INODE_RESIZED);
1864 * We must delete any chains beyond the EOF. The chain
1865 * straddling the EOF will be pending in the bioq.
1867 lbase = (ripdata->size + HAMMER2_PBUFMASK64) &
1868 ~HAMMER2_PBUFMASK64;
1869 dparent = hammer2_cluster_lookup_init(&ip->cluster, 0);
1870 cluster = hammer2_cluster_lookup(dparent, &key_next,
1871 lbase, (hammer2_key_t)-1,
1872 HAMMER2_LOOKUP_NODATA,
1876 * Degenerate embedded case, nothing to loop on
1878 switch (hammer2_cluster_type(cluster)) {
1879 case HAMMER2_BREF_TYPE_INODE:
1880 hammer2_cluster_unlock(cluster);
1883 case HAMMER2_BREF_TYPE_DATA:
1884 hammer2_cluster_delete(trans, dparent, cluster,
1885 HAMMER2_DELETE_PERMANENT);
1888 cluster = hammer2_cluster_next(dparent, cluster,
1890 key_next, (hammer2_key_t)-1,
1891 HAMMER2_LOOKUP_NODATA);
1895 hammer2_cluster_lookup_done(dparent);
1897 if ((ip->flags & HAMMER2_INODE_RESIZED) && ip->size > ripdata->size) {
1898 wipdata = hammer2_cluster_modify_ip(trans, ip, cparent, 0);
1899 wipdata->size = ip->size;
1900 atomic_clear_int(&ip->flags, HAMMER2_INODE_RESIZED);
1903 * When resizing larger we may not have any direct-data
1906 if ((wipdata->op_flags & HAMMER2_OPFLAG_DIRECTDATA) &&
1907 ip->size > HAMMER2_EMBEDDED_BYTES) {
1908 wipdata->op_flags &= ~HAMMER2_OPFLAG_DIRECTDATA;
1909 bzero(&wipdata->u.blockset,
1910 sizeof(wipdata->u.blockset));
1916 hammer2_cluster_modsync(cparent);