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 and exclusive locks on inodes. Pass a mask of
70 * - pass HAMMER2_RESOLVE_SHARED if a shared lock is desired. The
71 * inode locking function will automatically set the RDONLY flag.
73 * - pass HAMMER2_RESOLVE_ALWAYS if you need the inode's meta-data.
74 * Most front-end inode locks do.
76 * - pass HAMMER2_RESOLVE_NEVER if you do not want to require that
77 * the inode data be resolved. This is used by the syncthr because
78 * it can run on an unresolved/out-of-sync cluster, and also by the
79 * vnode reclamation code to avoid unnecessary I/O (particularly when
80 * disposing of hundreds of thousands of cached vnodes).
82 * The inode locking function locks the inode itself, resolves any stale
83 * chains in the inode's cluster, and allocates a fresh copy of the
84 * cluster with 1 ref and all the underlying chains locked.
86 * ip->cluster will be stable while the inode is locked.
88 * NOTE: We don't combine the inode/chain lock because putting away an
89 * inode would otherwise confuse multiple lock holders of the inode.
91 * NOTE: In-memory inodes always point to hardlink targets (the actual file),
92 * and never point to a hardlink pointer.
94 * NOTE: Caller must not passed HAMMER2_RESOLVE_NOREF because we use it
95 * internally and refs confusion will ensue.
97 * NOTE: If caller passes HAMMER2_RESOLVE_RDONLY the exclusive locking code
98 * will feel free to reduce the chain set in the cluster as an
99 * optimization. It will still be validated against the quorum if
100 * appropriate, but the optimization might be able to reduce data
101 * accesses to one node. This flag is automatically set if the inode
102 * is locked with HAMMER2_RESOLVE_SHARED.
105 hammer2_inode_lock(hammer2_inode_t *ip, int how)
107 hammer2_cluster_t *cluster;
109 KKASSERT((how & HAMMER2_RESOLVE_NOREF) == 0);
111 hammer2_inode_ref(ip);
114 * Inode structure mutex
116 if (how & HAMMER2_RESOLVE_SHARED) {
117 how |= HAMMER2_RESOLVE_RDONLY;
118 hammer2_mtx_sh(&ip->lock);
120 hammer2_mtx_ex(&ip->lock);
124 * Create a copy of ip->cluster and lock it. Note that the copy
125 * will have a ref on the cluster AND its chains and we don't want
126 * a second ref to either when we lock it.
128 * The copy will not have a focus until it is locked.
130 * Exclusive inode locks set the template focus chain in (ip)
131 * as a hint. Cluster locks can ALWAYS replace the focus in the
132 * working copy if the hint does not work out, so beware.
134 cluster = hammer2_cluster_copy(&ip->cluster);
135 hammer2_cluster_lock(cluster, how | HAMMER2_RESOLVE_NOREF);
138 * cluster->focus will be set if resolving RESOLVE_ALWAYS, but
139 * only update the cached focus in the inode structure when taking
140 * out an exclusive lock.
142 if ((how & HAMMER2_RESOLVE_SHARED) == 0)
143 ip->cluster.focus = cluster->focus;
146 * Returned cluster must resolve hardlink pointers.
148 if ((how & HAMMER2_RESOLVE_MASK) == HAMMER2_RESOLVE_ALWAYS &&
149 cluster->error == 0) {
150 const hammer2_inode_data_t *ripdata;
152 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
153 KKASSERT(ripdata->type != HAMMER2_OBJTYPE_HARDLINK);
159 hammer2_inode_unlock(hammer2_inode_t *ip, hammer2_cluster_t *cluster)
162 hammer2_cluster_unlock(cluster);
163 hammer2_mtx_unlock(&ip->lock);
164 hammer2_inode_drop(ip);
168 * Temporarily release a lock held shared or exclusive. Caller must
169 * hold the lock shared or exclusive on call and lock will be released
172 * Restore a lock that was temporarily released.
175 hammer2_inode_lock_temp_release(hammer2_inode_t *ip)
177 return hammer2_mtx_temp_release(&ip->lock);
181 hammer2_inode_lock_temp_restore(hammer2_inode_t *ip, hammer2_mtx_state_t ostate)
183 hammer2_mtx_temp_restore(&ip->lock, ostate);
187 * Upgrade a shared inode lock to exclusive and return. If the inode lock
188 * is already held exclusively this is a NOP.
190 * The caller MUST hold the inode lock either shared or exclusive on call
191 * and will own the lock exclusively on return.
193 * Returns non-zero if the lock was already exclusive prior to the upgrade.
196 hammer2_inode_lock_upgrade(hammer2_inode_t *ip)
200 if (mtx_islocked_ex(&ip->lock)) {
203 hammer2_mtx_unlock(&ip->lock);
204 hammer2_mtx_ex(&ip->lock);
211 * Downgrade an inode lock from exclusive to shared only if the inode
212 * lock was previously shared. If the inode lock was previously exclusive,
216 hammer2_inode_lock_downgrade(hammer2_inode_t *ip, int wasexclusive)
218 if (wasexclusive == 0)
219 mtx_downgrade(&ip->lock);
223 * Lookup an inode by inode number
226 hammer2_inode_lookup(hammer2_pfs_t *pmp, hammer2_tid_t inum)
234 hammer2_spin_ex(&pmp->inum_spin);
235 ip = RB_LOOKUP(hammer2_inode_tree, &pmp->inum_tree, inum);
237 hammer2_inode_ref(ip);
238 hammer2_spin_unex(&pmp->inum_spin);
244 * Adding a ref to an inode is only legal if the inode already has at least
247 * (can be called with spinlock held)
250 hammer2_inode_ref(hammer2_inode_t *ip)
252 atomic_add_int(&ip->refs, 1);
256 * Drop an inode reference, freeing the inode when the last reference goes
260 hammer2_inode_drop(hammer2_inode_t *ip)
263 hammer2_inode_t *pip;
271 * Transition to zero, must interlock with
272 * the inode inumber lookup tree (if applicable).
273 * It should not be possible for anyone to race
274 * the transition to 0.
279 hammer2_spin_ex(&pmp->inum_spin);
281 if (atomic_cmpset_int(&ip->refs, 1, 0)) {
282 KKASSERT(hammer2_mtx_refs(&ip->lock) == 0);
283 if (ip->flags & HAMMER2_INODE_ONRBTREE) {
284 atomic_clear_int(&ip->flags,
285 HAMMER2_INODE_ONRBTREE);
286 RB_REMOVE(hammer2_inode_tree,
287 &pmp->inum_tree, ip);
289 hammer2_spin_unex(&pmp->inum_spin);
296 * Cleaning out ip->cluster isn't entirely
299 hammer2_inode_repoint(ip, NULL, NULL);
302 * We have to drop pip (if non-NULL) to
303 * dispose of our implied reference from
304 * ip->pip. We can simply loop on it.
306 kfree(ip, pmp->minode);
307 atomic_add_long(&pmp->inmem_inodes, -1);
309 /* continue with pip (can be NULL) */
311 hammer2_spin_unex(&ip->pmp->inum_spin);
315 * Non zero transition
317 if (atomic_cmpset_int(&ip->refs, refs, refs - 1))
324 * Get the vnode associated with the given inode, allocating the vnode if
325 * necessary. The vnode will be returned exclusively locked.
327 * The caller must lock the inode (shared or exclusive).
329 * Great care must be taken to avoid deadlocks and vnode acquisition/reclaim
333 hammer2_igetv(hammer2_inode_t *ip, hammer2_cluster_t *cparent, int *errorp)
335 const hammer2_inode_data_t *ripdata;
340 KKASSERT(pmp != NULL);
343 ripdata = &hammer2_cluster_rdata(cparent)->ipdata;
347 * Attempt to reuse an existing vnode assignment. It is
348 * possible to race a reclaim so the vget() may fail. The
349 * inode must be unlocked during the vget() to avoid a
350 * 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.
364 hammer2_mtx_state_t ostate;
367 ostate = hammer2_inode_lock_temp_release(ip);
368 if (vget(vp, LK_EXCLUSIVE)) {
370 hammer2_inode_lock_temp_restore(ip, ostate);
373 hammer2_inode_lock_temp_restore(ip, ostate);
375 /* vp still locked and ref from vget */
377 kprintf("hammer2: igetv race %p/%p\n",
387 * No vnode exists, allocate a new vnode. Beware of
388 * allocation races. This function will return an
389 * exclusively locked and referenced vnode.
391 *errorp = getnewvnode(VT_HAMMER2, pmp->mp, &vp, 0, 0);
393 kprintf("hammer2: igetv getnewvnode failed %d\n",
400 * Lock the inode and check for an allocation race.
402 wasexclusive = hammer2_inode_lock_upgrade(ip);
403 if (ip->vp != NULL) {
406 hammer2_inode_lock_downgrade(ip, wasexclusive);
410 switch (ripdata->type) {
411 case HAMMER2_OBJTYPE_DIRECTORY:
414 case HAMMER2_OBJTYPE_REGFILE:
416 vinitvmio(vp, ripdata->size,
418 (int)ripdata->size & HAMMER2_LBUFMASK);
420 case HAMMER2_OBJTYPE_SOFTLINK:
422 * XXX for now we are using the generic file_read
423 * and file_write code so we need a buffer cache
427 vinitvmio(vp, ripdata->size,
429 (int)ripdata->size & HAMMER2_LBUFMASK);
431 case HAMMER2_OBJTYPE_CDEV:
434 case HAMMER2_OBJTYPE_BDEV:
435 vp->v_ops = &pmp->mp->mnt_vn_spec_ops;
436 if (ripdata->type != HAMMER2_OBJTYPE_CDEV)
438 addaliasu(vp, ripdata->rmajor, ripdata->rminor);
440 case HAMMER2_OBJTYPE_FIFO:
442 vp->v_ops = &pmp->mp->mnt_vn_fifo_ops;
445 panic("hammer2: unhandled objtype %d", ripdata->type);
449 if (ip == pmp->iroot)
450 vsetflags(vp, VROOT);
454 hammer2_inode_ref(ip); /* vp association */
455 hammer2_inode_lock_downgrade(ip, wasexclusive);
460 * Return non-NULL vp and *errorp == 0, or NULL vp and *errorp != 0.
462 if (hammer2_debug & 0x0002) {
463 kprintf("igetv vp %p refs 0x%08x aux 0x%08x\n",
464 vp, vp->v_refcnt, vp->v_auxrefs);
470 * Returns the inode associated with the passed-in cluster, creating the
471 * inode if necessary and synchronizing it to the passed-in cluster otherwise.
473 * The passed-in cluster must be locked and will remain locked on return.
474 * The returned inode will be locked and the caller may dispose of both
475 * via hammer2_inode_unlock_ex(). However, if the caller needs to resolve
476 * a hardlink it must ref/unlock/relock/drop the inode.
478 * The hammer2_inode structure regulates the interface between the high level
479 * kernel VNOPS API and the filesystem backend (the chains).
481 * On return the inode is locked with the supplied cluster.
484 hammer2_inode_get(hammer2_pfs_t *pmp, hammer2_inode_t *dip,
485 hammer2_cluster_t *cluster)
487 hammer2_inode_t *nip;
488 const hammer2_inode_data_t *iptmp;
489 const hammer2_inode_data_t *nipdata;
491 KKASSERT(cluster == NULL ||
492 hammer2_cluster_type(cluster) == HAMMER2_BREF_TYPE_INODE);
496 * Interlocked lookup/ref of the inode. This code is only needed
497 * when looking up inodes with nlinks != 0 (TODO: optimize out
498 * otherwise and test for duplicates).
500 * Cluster can be NULL during the initial pfs allocation.
504 iptmp = &hammer2_cluster_rdata(cluster)->ipdata;
505 nip = hammer2_inode_lookup(pmp, iptmp->inum);
509 hammer2_mtx_ex(&nip->lock);
512 * Handle SMP race (not applicable to the super-root spmp
513 * which can't index inodes due to duplicative inode numbers).
515 if (pmp->spmp_hmp == NULL &&
516 (nip->flags & HAMMER2_INODE_ONRBTREE) == 0) {
517 hammer2_mtx_unlock(&nip->lock);
518 hammer2_inode_drop(nip);
521 hammer2_inode_repoint(nip, NULL, cluster);
527 * We couldn't find the inode number, create a new inode.
529 nip = kmalloc(sizeof(*nip), pmp->minode, M_WAITOK | M_ZERO);
530 atomic_add_long(&pmp->inmem_inodes, 1);
531 hammer2_pfs_memory_inc(pmp);
532 hammer2_pfs_memory_wakeup(pmp);
534 nip->flags = HAMMER2_INODE_SROOT;
537 * Initialize nip's cluster. A cluster is provided for normal
538 * inodes but typically not for the super-root or PFS inodes.
540 nip->cluster.refs = 1;
541 nip->cluster.pmp = pmp;
542 nip->cluster.flags |= HAMMER2_CLUSTER_INODE;
544 nipdata = &hammer2_cluster_rdata(cluster)->ipdata;
545 nip->inum = nipdata->inum;
546 nip->size = nipdata->size;
547 nip->mtime = nipdata->mtime;
548 hammer2_inode_repoint(nip, NULL, cluster);
550 nip->inum = 1; /* PFS inum is always 1 XXX */
551 /* mtime will be updated when a cluster is available */
554 nip->pip = dip; /* can be NULL */
556 hammer2_inode_ref(dip); /* ref dip for nip->pip */
561 * ref and lock on nip gives it state compatible to after a
562 * hammer2_inode_lock() call.
565 hammer2_mtx_init(&nip->lock, "h2inode");
566 hammer2_mtx_ex(&nip->lock);
567 /* combination of thread lock and chain lock == inode lock */
570 * Attempt to add the inode. If it fails we raced another inode
571 * get. Undo all the work and try again.
573 if (pmp->spmp_hmp == NULL) {
574 hammer2_spin_ex(&pmp->inum_spin);
575 if (RB_INSERT(hammer2_inode_tree, &pmp->inum_tree, nip)) {
576 hammer2_spin_unex(&pmp->inum_spin);
577 hammer2_mtx_unlock(&nip->lock);
578 hammer2_inode_drop(nip);
581 atomic_set_int(&nip->flags, HAMMER2_INODE_ONRBTREE);
582 hammer2_spin_unex(&pmp->inum_spin);
589 * Create a new inode in the specified directory using the vattr to
590 * figure out the type of inode.
592 * If no error occurs the new inode with its cluster locked is returned in
593 * *nipp, otherwise an error is returned and *nipp is set to NULL.
595 * If vap and/or cred are NULL the related fields are not set and the
596 * inode type defaults to a directory. This is used when creating PFSs
597 * under the super-root, so the inode number is set to 1 in this case.
599 * dip is not locked on entry.
601 * NOTE: When used to create a snapshot, the inode is temporarily associated
602 * with the super-root spmp. XXX should pass new pmp for snapshot.
605 hammer2_inode_create(hammer2_trans_t *trans, hammer2_inode_t *dip,
606 struct vattr *vap, struct ucred *cred,
607 const uint8_t *name, size_t name_len,
608 hammer2_cluster_t **clusterp,
609 int flags, int *errorp)
611 const hammer2_inode_data_t *dipdata;
612 hammer2_inode_data_t *nipdata;
613 hammer2_cluster_t *cluster;
614 hammer2_cluster_t *cparent;
615 hammer2_inode_t *nip;
616 hammer2_key_t key_dummy;
623 uint8_t dip_comp_algo;
624 uint8_t dip_check_algo;
626 lhc = hammer2_dirhash(name, name_len);
630 * Locate the inode or indirect block to create the new
631 * entry in. At the same time check for key collisions
632 * and iterate until we don't get one.
634 * NOTE: hidden inodes do not have iterators.
637 cparent = hammer2_inode_lock(dip, HAMMER2_RESOLVE_ALWAYS);
638 dipdata = &hammer2_cluster_rdata(cparent)->ipdata;
639 dip_uid = dipdata->uid;
640 dip_gid = dipdata->gid;
641 dip_mode = dipdata->mode;
642 dip_comp_algo = dipdata->comp_algo;
643 dip_check_algo = dipdata->check_algo;
647 cluster = hammer2_cluster_lookup(cparent, &key_dummy,
651 if ((lhc & HAMMER2_DIRHASH_VISIBLE) == 0)
653 if ((lhc & HAMMER2_DIRHASH_LOMASK) == HAMMER2_DIRHASH_LOMASK)
655 hammer2_cluster_unlock(cluster);
661 error = hammer2_cluster_create(trans, cparent, &cluster,
663 HAMMER2_BREF_TYPE_INODE,
668 kprintf("CREATE INODE %*.*s chain=%p\n",
669 (int)name_len, (int)name_len, name,
670 (cluster ? cluster->focus : NULL));
674 * Cleanup and handle retries.
676 if (error == EAGAIN) {
677 hammer2_cluster_ref(cparent);
678 hammer2_inode_unlock(dip, cparent);
679 hammer2_cluster_wait(cparent);
680 hammer2_cluster_drop(cparent);
683 hammer2_inode_unlock(dip, cparent);
687 KKASSERT(cluster == NULL);
693 * Set up the new inode.
695 * NOTE: *_get() integrates chain's lock into the inode lock.
697 * NOTE: Only one new inode can currently be created per
698 * transaction. If the need arises we can adjust
699 * hammer2_trans_init() to allow more.
701 * NOTE: nipdata will have chain's blockset data.
703 KKASSERT(cluster->focus->flags & HAMMER2_CHAIN_MODIFIED);
704 nipdata = &hammer2_cluster_wdata(cluster)->ipdata;
705 nipdata->inum = trans->inode_tid;
706 hammer2_cluster_modsync(cluster);
707 nip = hammer2_inode_get(dip->pmp, dip, cluster);
708 nipdata = &hammer2_cluster_wdata(cluster)->ipdata;
711 KKASSERT(trans->inodes_created == 0);
712 nipdata->type = hammer2_get_obj_type(vap->va_type);
713 nipdata->inum = trans->inode_tid;
714 ++trans->inodes_created;
716 switch (nipdata->type) {
717 case HAMMER2_OBJTYPE_CDEV:
718 case HAMMER2_OBJTYPE_BDEV:
719 nipdata->rmajor = vap->va_rmajor;
720 nipdata->rminor = vap->va_rminor;
726 nipdata->type = HAMMER2_OBJTYPE_DIRECTORY;
730 /* Inherit parent's inode compression mode. */
731 nip->comp_heuristic = 0;
732 nipdata->comp_algo = dip_comp_algo;
733 nipdata->check_algo = dip_check_algo;
734 nipdata->version = HAMMER2_INODE_VERSION_ONE;
735 hammer2_update_time(&nipdata->ctime);
736 nipdata->mtime = nipdata->ctime;
738 nipdata->mode = vap->va_mode;
741 if (dip && dip->pmp) {
742 xuid = hammer2_to_unix_xid(&dip_uid);
743 xuid = vop_helper_create_uid(dip->pmp->mp,
749 /* super-root has no dip and/or pmp */
752 if (vap->va_vaflags & VA_UID_UUID_VALID)
753 nipdata->uid = vap->va_uid_uuid;
754 else if (vap->va_uid != (uid_t)VNOVAL)
755 hammer2_guid_to_uuid(&nipdata->uid, vap->va_uid);
757 hammer2_guid_to_uuid(&nipdata->uid, xuid);
759 if (vap->va_vaflags & VA_GID_UUID_VALID)
760 nipdata->gid = vap->va_gid_uuid;
761 else if (vap->va_gid != (gid_t)VNOVAL)
762 hammer2_guid_to_uuid(&nipdata->gid, vap->va_gid);
764 nipdata->gid = dip_gid;
768 * Regular files and softlinks allow a small amount of data to be
769 * directly embedded in the inode. This flag will be cleared if
770 * the size is extended past the embedded limit.
772 if (nipdata->type == HAMMER2_OBJTYPE_REGFILE ||
773 nipdata->type == HAMMER2_OBJTYPE_SOFTLINK) {
774 nipdata->op_flags |= HAMMER2_OPFLAG_DIRECTDATA;
777 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
778 bcopy(name, nipdata->filename, name_len);
779 nipdata->name_key = lhc;
780 nipdata->name_len = name_len;
781 hammer2_cluster_modsync(cluster);
788 * The cluster has been removed from the original directory and replaced
789 * with a hardlink pointer. Move the cluster to the specified parent
790 * directory, change the filename to "0xINODENUMBER", and adjust the key.
791 * The cluster becomes our invisible hardlink target.
793 * The original cluster must be deleted on entry.
797 hammer2_hardlink_shiftup(hammer2_trans_t *trans, hammer2_cluster_t *cluster,
798 hammer2_inode_t *dip, hammer2_cluster_t *dcluster,
799 int nlinks, int *errorp)
801 const hammer2_inode_data_t *iptmp;
802 hammer2_inode_data_t *nipdata;
803 hammer2_cluster_t *xcluster;
804 hammer2_key_t key_dummy;
806 hammer2_blockref_t bref;
808 iptmp = &hammer2_cluster_rdata(cluster)->ipdata;
810 KKASSERT((lhc & HAMMER2_DIRHASH_VISIBLE) == 0);
813 * Locate the inode or indirect block to create the new
814 * entry in. lhc represents the inode number so there is
815 * no collision iteration.
817 * There should be no key collisions with invisible inode keys.
819 * WARNING! Must use inode_lock_ex() on dip to handle a stale
820 * dip->cluster cache.
823 xcluster = hammer2_cluster_lookup(dcluster, &key_dummy,
826 kprintf("X3 chain %p dip %p dchain %p dip->chain %p\n",
827 xcluster->focus, dip, dcluster->focus,
829 hammer2_cluster_unlock(xcluster);
838 * Handle the error case
842 KKASSERT(xcluster == NULL);
847 * Use xcluster as a placeholder for (lhc). Duplicate cluster to the
848 * same target bref as xcluster and then delete xcluster. The
849 * duplication occurs after xcluster in flush order even though
850 * xcluster is deleted after the duplication. XXX
852 * WARNING! Duplications (to a different parent) can cause indirect
853 * blocks to be inserted, refactor xcluster.
855 * WARNING! Only key and keybits is extracted from a passed-in bref.
857 hammer2_cluster_bref(cluster, &bref);
858 bref.key = lhc; /* invisible dir entry key */
860 hammer2_cluster_rename(trans, &bref, dcluster, cluster, 0);
863 * cluster is now 'live' again.. adjust the filename.
865 * Directory entries are inodes but this is a hidden hardlink
866 * target. The name isn't used but to ease debugging give it
867 * a name after its inode number.
869 hammer2_cluster_modify(trans, cluster, 0);
870 nipdata = &hammer2_cluster_wdata(cluster)->ipdata;
871 ksnprintf(nipdata->filename, sizeof(nipdata->filename),
872 "0x%016jx", (intmax_t)nipdata->inum);
873 nipdata->name_len = strlen(nipdata->filename);
874 nipdata->name_key = lhc;
875 nipdata->nlinks += nlinks;
876 hammer2_cluster_modsync(cluster);
880 * Connect the target inode represented by (cluster) to the media topology
881 * at (dip, name, len). The caller can pass a rough *chainp, this function
882 * will issue lookup()s to position the parent chain properly for the
885 * If hlink is TRUE this function creates an OBJTYPE_HARDLINK directory
886 * entry instead of connecting (cluster).
888 * If hlink is FALSE this function expects (cluster) to be unparented.
891 hammer2_inode_connect(hammer2_trans_t *trans,
892 hammer2_cluster_t **clusterp, int hlink,
893 hammer2_inode_t *dip, hammer2_cluster_t *dcluster,
894 const uint8_t *name, size_t name_len,
897 hammer2_inode_data_t *wipdata;
898 hammer2_cluster_t *ocluster;
899 hammer2_cluster_t *ncluster;
900 hammer2_key_t key_dummy;
904 * Since ocluster is either disconnected from the topology or
905 * represents a hardlink terminus which is always a parent of or
906 * equal to dip, we should be able to safely lock dip->chain for
909 * WARNING! Must use inode_lock_ex() on dip to handle a stale
912 * If name is non-NULL we calculate lhc, else we use the passed-in
915 ocluster = *clusterp;
918 lhc = hammer2_dirhash(name, name_len);
921 * Locate the inode or indirect block to create the new
922 * entry in. At the same time check for key collisions
923 * and iterate until we don't get one.
927 ncluster = hammer2_cluster_lookup(dcluster, &key_dummy,
929 if (ncluster == NULL)
931 if ((lhc & HAMMER2_DIRHASH_LOMASK) ==
932 HAMMER2_DIRHASH_LOMASK) {
935 hammer2_cluster_unlock(ncluster);
941 * Reconnect to specific key (used when moving
942 * unlinked-but-open files into the hidden directory).
944 ncluster = hammer2_cluster_lookup(dcluster, &key_dummy,
946 KKASSERT(ncluster == NULL);
952 * Hardlink pointer needed, create totally fresh
955 * We must refactor ocluster because it might have
956 * been shifted into an indirect cluster by the
959 KKASSERT(ncluster == NULL);
960 error = hammer2_cluster_create(trans,
963 HAMMER2_BREF_TYPE_INODE,
968 * Reconnect the original cluster under the new name.
969 * Original cluster must have already been deleted by
972 * WARNING! Can cause held-over clusters to require a
973 * refactor. Fortunately we have none (our
974 * locked clusters are passed into and
975 * modified by the call).
979 error = hammer2_cluster_create(trans,
982 HAMMER2_BREF_TYPE_INODE,
991 KKASSERT(error != EAGAIN);
994 * ncluster should be NULL on error, leave ocluster
995 * (ocluster == *clusterp) alone.
998 KKASSERT(ncluster == NULL);
1003 * Directory entries are inodes so if the name has changed we have
1004 * to update the inode.
1006 * When creating an OBJTYPE_HARDLINK entry remember to unlock the
1007 * cluster, the caller will access the hardlink via the actual hardlink
1008 * target file and not the hardlink pointer entry, so we must still
1011 if (hlink && hammer2_hardlink_enable >= 0) {
1013 * Create the HARDLINK pointer. oip represents the hardlink
1014 * target in this situation.
1016 * We will return ocluster (the hardlink target).
1018 hammer2_cluster_modify(trans, ncluster, 0);
1019 hammer2_cluster_clr_chainflags(ncluster,
1020 HAMMER2_CHAIN_UNLINKED);
1021 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
1022 wipdata = &hammer2_cluster_wdata(ncluster)->ipdata;
1023 bcopy(name, wipdata->filename, name_len);
1024 wipdata->name_key = lhc;
1025 wipdata->name_len = name_len;
1026 wipdata->target_type =
1027 hammer2_cluster_rdata(ocluster)->ipdata.type;
1028 wipdata->type = HAMMER2_OBJTYPE_HARDLINK;
1029 wipdata->inum = hammer2_cluster_rdata(ocluster)->ipdata.inum;
1030 wipdata->version = HAMMER2_INODE_VERSION_ONE;
1031 wipdata->nlinks = 1;
1032 wipdata->op_flags = HAMMER2_OPFLAG_DIRECTDATA;
1033 hammer2_cluster_modsync(ncluster);
1034 hammer2_cluster_unlock(ncluster);
1035 ncluster = ocluster;
1039 * ncluster is a duplicate of ocluster at the new location.
1040 * We must fixup the name stored in the inode data.
1041 * The bref key has already been adjusted by inode_connect().
1043 hammer2_cluster_modify(trans, ncluster, 0);
1044 hammer2_cluster_clr_chainflags(ncluster,
1045 HAMMER2_CHAIN_UNLINKED);
1046 wipdata = &hammer2_cluster_wdata(ncluster)->ipdata;
1048 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
1049 bcopy(name, wipdata->filename, name_len);
1050 wipdata->name_key = lhc;
1051 wipdata->name_len = name_len;
1052 wipdata->nlinks = 1;
1053 hammer2_cluster_modsync(ncluster);
1057 * We are replacing ocluster with ncluster, unlock ocluster. In the
1058 * case where ocluster is left unchanged the code above sets
1059 * ncluster to ocluster and ocluster to NULL, resulting in a NOP here.
1062 hammer2_cluster_unlock(ocluster);
1063 *clusterp = ncluster;
1069 * Repoint ip->cluster's chains to cluster's chains and fixup the default
1072 * Caller must hold the inode and cluster exclusive locked, if not NULL,
1073 * must also be locked.
1075 * Cluster may be NULL to clean out any chains in ip->cluster.
1078 hammer2_inode_repoint(hammer2_inode_t *ip, hammer2_inode_t *pip,
1079 hammer2_cluster_t *cluster)
1081 hammer2_chain_t *ochain;
1082 hammer2_chain_t *nchain;
1083 hammer2_inode_t *opip;
1087 * Replace chains in ip->cluster with chains from cluster and
1088 * adjust the focus if necessary.
1090 * NOTE: nchain and/or ochain can be NULL due to gaps
1091 * in the cluster arrays.
1093 for (i = 0; cluster && i < cluster->nchains; ++i) {
1094 nchain = cluster->array[i].chain;
1095 if (i < ip->cluster.nchains) {
1096 ochain = ip->cluster.array[i].chain;
1097 if (ochain == nchain)
1106 ip->cluster.array[i].chain = nchain;
1108 hammer2_chain_ref(nchain);
1110 hammer2_chain_drop(ochain);
1114 * Release any left-over chains in ip->cluster.
1116 while (i < ip->cluster.nchains) {
1117 nchain = ip->cluster.array[i].chain;
1119 ip->cluster.array[i].chain = NULL;
1120 hammer2_chain_drop(nchain);
1126 * Fixup fields. Note that the inode-embedded cluster is never
1130 ip->cluster.nchains = cluster->nchains;
1131 ip->cluster.focus = cluster->focus;
1132 ip->cluster.flags = cluster->flags & ~HAMMER2_CLUSTER_LOCKED;
1134 ip->cluster.nchains = 0;
1135 ip->cluster.focus = NULL;
1136 ip->cluster.flags &= ~HAMMER2_CLUSTER_ZFLAGS;
1140 * Repoint ip->pip if requested (non-NULL pip).
1142 if (pip && ip->pip != pip) {
1144 hammer2_inode_ref(pip);
1147 hammer2_inode_drop(opip);
1152 * Unlink the file from the specified directory inode. The directory inode
1153 * does not need to be locked.
1155 * isdir determines whether a directory/non-directory check should be made.
1156 * No check is made if isdir is set to -1.
1158 * isopen specifies whether special unlink-with-open-descriptor handling
1159 * must be performed. If set to -1 the caller is deleting a PFS and we
1160 * check whether the chain is mounted or not (chain->pmp != NULL). 1 is
1161 * implied if it is mounted.
1163 * If isopen is 1 and nlinks drops to 0 this function must move the chain
1164 * to a special hidden directory until last-close occurs on the file.
1166 * NOTE! The underlying file can still be active with open descriptors
1167 * or if the chain is being manually held (e.g. for rename).
1169 * The caller is responsible for fixing up ip->chain if e.g. a
1170 * rename occurs (see chain_duplicate()).
1172 * NOTE! The chain is not deleted if it is moved to the hidden directory,
1173 * but otherwise will be deleted.
1176 hammer2_unlink_file(hammer2_trans_t *trans, hammer2_inode_t *dip,
1177 const uint8_t *name, size_t name_len,
1178 int isdir, int *hlinkp, struct nchandle *nch,
1181 const hammer2_inode_data_t *ripdata;
1182 hammer2_inode_data_t *wipdata;
1183 hammer2_cluster_t *cparent;
1184 hammer2_cluster_t *hcluster;
1185 hammer2_cluster_t *hparent;
1186 hammer2_cluster_t *cluster;
1187 hammer2_cluster_t *dparent;
1188 hammer2_cluster_t *dcluster;
1189 hammer2_key_t key_dummy;
1190 hammer2_key_t key_next;
1200 lhc = hammer2_dirhash(name, name_len);
1204 * Search for the filename in the directory
1206 cparent = hammer2_inode_lock(dip, HAMMER2_RESOLVE_ALWAYS);
1207 cluster = hammer2_cluster_lookup(cparent, &key_next,
1208 lhc, lhc + HAMMER2_DIRHASH_LOMASK, 0);
1210 if (hammer2_cluster_type(cluster) == HAMMER2_BREF_TYPE_INODE) {
1211 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
1212 if (ripdata->name_len == name_len &&
1213 bcmp(ripdata->filename, name, name_len) == 0) {
1217 cluster = hammer2_cluster_next(cparent, cluster, &key_next,
1219 lhc + HAMMER2_DIRHASH_LOMASK,
1222 hammer2_inode_unlock(dip, NULL); /* retain cparent */
1225 * Not found or wrong type (isdir < 0 disables the type check).
1226 * If a hardlink pointer, type checks use the hardlink target.
1228 if (cluster == NULL) {
1232 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
1233 type = ripdata->type;
1234 if (type == HAMMER2_OBJTYPE_HARDLINK) {
1236 type = ripdata->target_type;
1239 if (type == HAMMER2_OBJTYPE_DIRECTORY && isdir == 0) {
1243 if (type != HAMMER2_OBJTYPE_DIRECTORY && isdir >= 1) {
1249 * Hardlink must be resolved. We can't hold the parent locked
1250 * while we do this or we could deadlock. The physical file will
1251 * be located at or above the current directory.
1253 * We loop to reacquire the hardlink origination.
1255 * NOTE: hammer2_hardlink_find() will locate the hardlink target,
1256 * returning a modified hparent and hcluster.
1258 if (ripdata->type == HAMMER2_OBJTYPE_HARDLINK) {
1259 if (hcluster == NULL) {
1261 cluster = NULL; /* safety */
1262 hammer2_cluster_unlock(cparent);
1263 cparent = NULL; /* safety */
1264 ripdata = NULL; /* safety (associated w/cparent) */
1265 error = hammer2_hardlink_find(dip, &hparent, &hcluster);
1268 * If we couldn't find the hardlink target then some
1269 * parent directory containing the hardlink pointer
1270 * probably got renamed to above the original target,
1271 * a case not yet handled by H2.
1274 kprintf("H2 unlink_file: hardlink target for "
1275 "\"%s\" not found\n",
1277 kprintf("(likely due to known directory "
1286 * If this is a directory the directory must be empty. However, if
1287 * isdir < 0 we are doing a rename and the directory does not have
1288 * to be empty, and if isdir > 1 we are deleting a PFS/snapshot
1289 * and the directory does not have to be empty.
1291 * NOTE: We check the full key range here which covers both visible
1292 * and invisible entries. Theoretically there should be no
1293 * invisible (hardlink target) entries if there are no visible
1296 if (type == HAMMER2_OBJTYPE_DIRECTORY && isdir == 1) {
1297 dparent = hammer2_cluster_lookup_init(cluster, 0);
1298 dcluster = hammer2_cluster_lookup(dparent, &key_dummy,
1299 0, (hammer2_key_t)-1,
1300 HAMMER2_LOOKUP_NODATA);
1302 hammer2_cluster_unlock(dcluster);
1303 hammer2_cluster_lookup_done(dparent);
1307 hammer2_cluster_lookup_done(dparent);
1313 * If this was a hardlink then (cparent, cluster) is the hardlink
1314 * pointer, which we can simply destroy outright. Discard the
1315 * clusters and replace with the hardlink target.
1318 hammer2_cluster_delete(trans, cparent, cluster,
1319 HAMMER2_DELETE_PERMANENT);
1320 hammer2_cluster_unlock(cparent);
1321 hammer2_cluster_unlock(cluster);
1329 * This leaves us with the hardlink target or non-hardlinked file
1330 * or directory in (cparent, cluster).
1332 * Delete the target when nlinks reaches 0 with special handling
1333 * if (isopen) is set.
1335 * NOTE! In DragonFly the vnops function calls cache_unlink() after
1336 * calling us here to clean out the namecache association,
1337 * (which does not represent a ref for the open-test), and to
1338 * force finalization of the vnode if/when the last ref gets
1341 * NOTE! Files are unlinked by rename and then relinked. nch will be
1342 * passed as NULL in this situation. hammer2_inode_connect()
1345 KKASSERT(cluster != NULL);
1346 hammer2_cluster_modify(trans, cluster, 0);
1347 wipdata = &hammer2_cluster_wdata(cluster)->ipdata;
1349 wipdata->nlinks += nlinks;
1350 if ((int64_t)wipdata->nlinks < 0) { /* XXX debugging */
1351 wipdata->nlinks = 0;
1353 hammer2_cluster_modsync(cluster);
1355 if (wipdata->nlinks == 0) {
1357 * Target nlinks has reached 0, file now unlinked (but may
1360 /* XXX need interlock if mounted
1361 if ((cluster->focus->flags & HAMMER2_CHAIN_PFSROOT) &&
1364 kprintf("hammer2: PFS \"%s\" cannot be deleted "
1365 "while still mounted\n",
1370 hammer2_cluster_set_chainflags(cluster, HAMMER2_CHAIN_UNLINKED);
1371 if (nch && cache_isopen(nch)) {
1372 hammer2_inode_move_to_hidden(trans, &cparent, &cluster,
1376 * This won't get everything if a vnode is still
1377 * present, but the cache_unlink() call the caller
1380 hammer2_cluster_delete(trans, cparent, cluster,
1381 HAMMER2_DELETE_PERMANENT);
1383 } else if (hlink == 0) {
1385 * In this situation a normal non-hardlinked file (which can
1386 * only have nlinks == 1) still has a non-zero nlinks, the
1387 * caller must be doing a RENAME operation and so is passing
1388 * a nlinks adjustment of 0, and only wishes to remove file
1389 * in order to be able to reconnect it under a different name.
1391 * In this situation we do a non-permanent deletion of the
1392 * chain in order to allow the file to be reconnected in
1393 * a different location.
1395 KKASSERT(nlinks == 0);
1396 hammer2_cluster_delete(trans, cparent, cluster, 0);
1401 hammer2_cluster_unlock(cparent);
1403 hammer2_cluster_unlock(cluster);
1405 hammer2_cluster_unlock(hparent);
1407 hammer2_cluster_unlock(hcluster);
1415 * This is called from the mount code to initialize pmp->ihidden
1418 hammer2_inode_install_hidden(hammer2_pfs_t *pmp)
1420 hammer2_trans_t trans;
1421 hammer2_cluster_t *cparent;
1422 hammer2_cluster_t *cluster;
1423 hammer2_cluster_t *scan;
1424 const hammer2_inode_data_t *ripdata;
1425 hammer2_inode_data_t *wipdata;
1426 hammer2_key_t key_dummy;
1427 hammer2_key_t key_next;
1437 * Find the hidden directory
1439 bzero(&key_dummy, sizeof(key_dummy));
1440 hammer2_trans_init(&trans, pmp, 0);
1443 * Setup for lookup, retrieve iroot's check and compression
1444 * algorithm request which was likely generated by newfs_hammer2.
1446 * The check/comp fields will probably never be used since inodes
1447 * are renamed into the hidden directory and not created relative to
1448 * the hidden directory, chain creation inherits from bref.methods,
1449 * and data chains inherit from their respective file inode *_algo
1452 cparent = hammer2_inode_lock(pmp->iroot, HAMMER2_RESOLVE_ALWAYS);
1453 ripdata = &hammer2_cluster_rdata(cparent)->ipdata;
1454 dip_check_algo = ripdata->check_algo;
1455 dip_comp_algo = ripdata->comp_algo;
1458 cluster = hammer2_cluster_lookup(cparent, &key_dummy,
1459 HAMMER2_INODE_HIDDENDIR,
1460 HAMMER2_INODE_HIDDENDIR,
1463 pmp->ihidden = hammer2_inode_get(pmp, pmp->iroot, cluster);
1464 hammer2_inode_ref(pmp->ihidden);
1467 * Remove any unlinked files which were left open as-of
1470 * Don't pass NODATA, we need the inode data so the delete
1471 * can do proper statistics updates.
1474 scan = hammer2_cluster_lookup(cluster, &key_next,
1475 0, HAMMER2_TID_MAX, 0);
1477 if (hammer2_cluster_type(scan) ==
1478 HAMMER2_BREF_TYPE_INODE) {
1479 hammer2_cluster_delete(&trans, cluster, scan,
1480 HAMMER2_DELETE_PERMANENT);
1483 scan = hammer2_cluster_next(cluster, scan, &key_next,
1484 0, HAMMER2_TID_MAX, 0);
1487 hammer2_inode_unlock(pmp->ihidden, cluster);
1488 hammer2_inode_unlock(pmp->iroot, cparent);
1489 hammer2_trans_done(&trans);
1490 kprintf("hammer2: PFS loaded hidden dir, "
1491 "removed %d dead entries\n", count);
1496 * Create the hidden directory
1498 error = hammer2_cluster_create(&trans, cparent, &cluster,
1499 HAMMER2_INODE_HIDDENDIR, 0,
1500 HAMMER2_BREF_TYPE_INODE,
1501 HAMMER2_INODE_BYTES,
1503 hammer2_inode_unlock(pmp->iroot, cparent);
1505 hammer2_cluster_modify(&trans, cluster, 0);
1506 wipdata = &hammer2_cluster_wdata(cluster)->ipdata;
1507 wipdata->type = HAMMER2_OBJTYPE_DIRECTORY;
1508 wipdata->inum = HAMMER2_INODE_HIDDENDIR;
1509 wipdata->nlinks = 1;
1510 wipdata->comp_algo = dip_comp_algo;
1511 wipdata->check_algo = dip_check_algo;
1512 hammer2_cluster_modsync(cluster);
1513 kprintf("hammer2: PFS root missing hidden directory, creating\n");
1515 pmp->ihidden = hammer2_inode_get(pmp, pmp->iroot, cluster);
1516 hammer2_inode_ref(pmp->ihidden);
1517 hammer2_inode_unlock(pmp->ihidden, cluster);
1518 hammer2_trans_done(&trans);
1522 * If an open file is unlinked H2 needs to retain the file in the topology
1523 * to ensure that its backing store is not recovered by the bulk free scan.
1524 * This also allows us to avoid having to special-case the CHAIN_DELETED flag.
1526 * To do this the file is moved to a hidden directory in the PFS root and
1527 * renamed. The hidden directory must be created if it does not exist.
1531 hammer2_inode_move_to_hidden(hammer2_trans_t *trans,
1532 hammer2_cluster_t **cparentp,
1533 hammer2_cluster_t **clusterp,
1536 hammer2_cluster_t *dcluster;
1540 pmp = (*clusterp)->pmp;
1541 KKASSERT(pmp != NULL);
1542 KKASSERT(pmp->ihidden != NULL);
1544 hammer2_cluster_delete(trans, *cparentp, *clusterp, 0);
1545 dcluster = hammer2_inode_lock(pmp->ihidden, HAMMER2_RESOLVE_ALWAYS);
1546 error = hammer2_inode_connect(trans, clusterp, 0,
1547 pmp->ihidden, dcluster,
1549 hammer2_inode_unlock(pmp->ihidden, dcluster);
1550 KKASSERT(error == 0);
1554 * Given an exclusively locked inode and cluster we consolidate the cluster
1555 * for hardlink creation, adding (nlinks) to the file's link count and
1556 * potentially relocating the inode to (cdip) which is a parent directory
1557 * common to both the current location of the inode and the intended new
1560 * Replaces (*clusterp) if consolidation occurred, unlocking the old cluster
1561 * and returning a new locked cluster.
1563 * NOTE! This function will also replace ip->cluster.
1566 hammer2_hardlink_consolidate(hammer2_trans_t *trans,
1567 hammer2_inode_t *ip,
1568 hammer2_cluster_t **clusterp,
1569 hammer2_inode_t *cdip,
1570 hammer2_cluster_t *cdcluster,
1573 const hammer2_inode_data_t *ripdata;
1574 hammer2_inode_data_t *wipdata;
1575 hammer2_cluster_t *cluster;
1576 hammer2_cluster_t *cparent;
1579 cluster = *clusterp;
1580 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
1581 if (nlinks == 0 && /* no hardlink needed */
1582 (ripdata->name_key & HAMMER2_DIRHASH_VISIBLE)) {
1586 if (hammer2_hardlink_enable == 0) { /* disallow hardlinks */
1587 hammer2_cluster_unlock(cluster);
1595 * If no change in the hardlink's target directory is required and
1596 * this is already a hardlink target, all we need to do is adjust
1599 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
1600 if (cdip == ip->pip &&
1601 (ripdata->name_key & HAMMER2_DIRHASH_VISIBLE) == 0) {
1603 hammer2_cluster_modify(trans, cluster, 0);
1604 wipdata = &hammer2_cluster_wdata(cluster)->ipdata;
1605 wipdata->nlinks += nlinks;
1606 hammer2_cluster_modsync(cluster);
1614 * Cluster is the real inode. The originating directory is locked
1615 * by the caller so we can manipulate it without worrying about races
1616 * against other lookups.
1618 * If cluster is visible we need to delete it from the current
1619 * location and create a hardlink pointer in its place. If it is
1620 * not visible we need only delete it. Then later cluster will be
1621 * renamed to a parent directory and converted (if necessary) to
1622 * a hidden inode (via shiftup).
1624 * NOTE! We must hold cparent locked through the delete/create/rename
1625 * operation to ensure that other threads block resolving to
1626 * the same hardlink, otherwise the other threads may not see
1629 KKASSERT((cluster->focus->flags & HAMMER2_CHAIN_DELETED) == 0);
1630 cparent = hammer2_cluster_parent(cluster);
1632 hammer2_cluster_delete(trans, cparent, cluster, 0);
1634 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
1635 KKASSERT(ripdata->type != HAMMER2_OBJTYPE_HARDLINK);
1636 if (ripdata->name_key & HAMMER2_DIRHASH_VISIBLE) {
1637 hammer2_cluster_t *ncluster;
1641 lhc = cluster->focus->bref.key;
1642 error = hammer2_cluster_create(trans, cparent, &ncluster,
1644 HAMMER2_BREF_TYPE_INODE,
1645 HAMMER2_INODE_BYTES,
1647 hammer2_cluster_modify(trans, ncluster, 0);
1648 wipdata = &hammer2_cluster_wdata(ncluster)->ipdata;
1650 /* wipdata->comp_algo = ripdata->comp_algo; */
1651 wipdata->comp_algo = 0;
1652 wipdata->check_algo = 0;
1653 wipdata->version = HAMMER2_INODE_VERSION_ONE;
1654 wipdata->inum = ripdata->inum;
1655 wipdata->target_type = ripdata->type;
1656 wipdata->type = HAMMER2_OBJTYPE_HARDLINK;
1657 wipdata->uflags = 0;
1658 wipdata->rmajor = 0;
1659 wipdata->rminor = 0;
1664 bzero(&wipdata->uid, sizeof(wipdata->uid));
1665 bzero(&wipdata->gid, sizeof(wipdata->gid));
1666 wipdata->op_flags = HAMMER2_OPFLAG_DIRECTDATA;
1667 wipdata->cap_flags = 0;
1670 wipdata->nlinks = 1;
1671 wipdata->iparent = 0; /* XXX */
1672 wipdata->pfs_type = 0;
1673 wipdata->pfs_inum = 0;
1674 bzero(&wipdata->pfs_clid, sizeof(wipdata->pfs_clid));
1675 bzero(&wipdata->pfs_fsid, sizeof(wipdata->pfs_fsid));
1676 wipdata->data_quota = 0;
1677 wipdata->data_count = 0;
1678 wipdata->inode_quota = 0;
1679 wipdata->inode_count = 0;
1680 wipdata->attr_tid = 0;
1681 wipdata->dirent_tid = 0;
1682 bzero(&wipdata->u, sizeof(wipdata->u));
1683 bcopy(ripdata->filename, wipdata->filename, ripdata->name_len);
1684 wipdata->name_key = ncluster->focus->bref.key;
1685 wipdata->name_len = ripdata->name_len;
1686 /* XXX transaction ids */
1687 hammer2_cluster_modsync(ncluster);
1688 hammer2_cluster_unlock(ncluster);
1693 * cluster represents the hardlink target and is now flagged deleted.
1694 * duplicate it to the parent directory and adjust nlinks.
1696 * WARNING! The shiftup() call can cause ncluster to be moved into
1697 * an indirect block, and our ncluster will wind up pointing
1698 * to the older/original version.
1700 KKASSERT(cluster->focus->flags & HAMMER2_CHAIN_DELETED);
1701 hammer2_hardlink_shiftup(trans, cluster, cdip, cdcluster,
1705 hammer2_inode_repoint(ip, cdip, cluster);
1709 * Cleanup, cluster/ncluster already dealt with.
1711 * Return the shifted cluster in *clusterp.
1714 hammer2_cluster_unlock(cparent);
1715 *clusterp = cluster;
1721 * If (*ochainp) is non-NULL it points to the forward OBJTYPE_HARDLINK
1722 * inode while (*chainp) points to the resolved (hidden hardlink
1723 * target) inode. In this situation when nlinks is 1 we wish to
1724 * deconsolidate the hardlink, moving it back to the directory that now
1725 * represents the only remaining link.
1728 hammer2_hardlink_deconsolidate(hammer2_trans_t *trans,
1729 hammer2_inode_t *dip,
1730 hammer2_chain_t **chainp,
1731 hammer2_chain_t **ochainp)
1733 if (*ochainp == NULL)
1740 * The caller presents a locked cluster with an obj_type of
1741 * HAMMER2_OBJTYPE_HARDLINK in (*clusterp). This routine will locate
1742 * the inode and replace (*clusterp) with a new locked cluster containing
1743 * the target hardlink, also locked. The original cluster will be
1744 * unlocked and released.
1746 * If cparentp is not NULL a locked cluster representing the hardlink's
1747 * parent is also returned.
1749 * If we are unable to locate the hardlink target EIO is returned,
1750 * (*cparentp) is set to NULL, the original passed-in (*clusterp)
1751 * will be unlocked and released and (*clusterp) will be set to NULL
1755 hammer2_hardlink_find(hammer2_inode_t *dip,
1756 hammer2_cluster_t **cparentp,
1757 hammer2_cluster_t **clusterp)
1759 const hammer2_inode_data_t *ipdata;
1760 hammer2_cluster_t *cluster;
1761 hammer2_cluster_t *cparent;
1762 hammer2_cluster_t *rcluster;
1763 hammer2_inode_t *ip;
1764 hammer2_inode_t *pip;
1765 hammer2_key_t key_dummy;
1768 cluster = *clusterp;
1770 hammer2_inode_ref(pip); /* for loop */
1773 * Locate the hardlink. pip is referenced and not locked.
1774 * Unlock and release (*clusterp) after extracting the needed
1777 ipdata = &hammer2_cluster_rdata(cluster)->ipdata;
1779 ipdata = NULL; /* safety */
1780 hammer2_cluster_unlock(cluster);
1781 *clusterp = NULL; /* safety */
1786 while ((ip = pip) != NULL) {
1787 cparent = hammer2_inode_lock(ip, HAMMER2_RESOLVE_ALWAYS);
1788 hammer2_inode_drop(ip); /* loop */
1789 KKASSERT(hammer2_cluster_type(cparent) ==
1790 HAMMER2_BREF_TYPE_INODE);
1791 rcluster = hammer2_cluster_lookup(cparent, &key_dummy,
1795 hammer2_cluster_lookup_done(cparent); /* discard parent */
1796 cparent = NULL; /* safety */
1797 pip = ip->pip; /* safe, ip held locked */
1799 hammer2_inode_ref(pip); /* loop */
1800 hammer2_inode_unlock(ip, NULL);
1804 * chain is locked, ip is locked. Unlock ip, return the locked
1805 * chain. *ipp is already set w/a ref count and not locked.
1807 * (cparent is already unlocked).
1809 *clusterp = rcluster;
1812 *cparentp = cparent;
1813 hammer2_inode_unlock(ip, NULL);
1815 hammer2_inode_unlock(ip, cparent);
1822 hammer2_inode_unlock(ip, cparent);
1828 * Find the directory common to both fdip and tdip.
1830 * Returns a held but not locked inode. Caller typically locks the inode,
1831 * and when through unlocks AND drops it.
1834 hammer2_inode_common_parent(hammer2_inode_t *fdip, hammer2_inode_t *tdip)
1836 hammer2_inode_t *scan1;
1837 hammer2_inode_t *scan2;
1840 * We used to have a depth field but it complicated matters too
1841 * much for directory renames. So now its ugly. Check for
1842 * simple cases before giving up and doing it the expensive way.
1844 * XXX need a bottom-up topology stability lock
1846 if (fdip == tdip || fdip == tdip->pip) {
1847 hammer2_inode_ref(fdip);
1850 if (fdip->pip == tdip) {
1851 hammer2_inode_ref(tdip);
1858 for (scan1 = fdip; scan1->pmp == fdip->pmp; scan1 = scan1->pip) {
1860 while (scan2->pmp == tdip->pmp) {
1861 if (scan1 == scan2) {
1862 hammer2_inode_ref(scan1);
1870 panic("hammer2_inode_common_parent: no common parent %p %p\n",
1877 * Synchronize the inode's frontend state with the chain state prior
1878 * to any explicit flush of the inode or any strategy write call.
1880 * Called with a locked inode.
1883 hammer2_inode_fsync(hammer2_trans_t *trans, hammer2_inode_t *ip,
1884 hammer2_cluster_t *cparent)
1886 const hammer2_inode_data_t *ripdata;
1887 hammer2_inode_data_t *wipdata;
1888 hammer2_cluster_t *dparent;
1889 hammer2_cluster_t *cluster;
1890 hammer2_key_t lbase;
1891 hammer2_key_t key_next;
1894 ripdata = &hammer2_cluster_rdata(cparent)->ipdata; /* target file */
1896 if (ip->flags & HAMMER2_INODE_MTIME) {
1897 wipdata = hammer2_cluster_modify_ip(trans, ip, cparent, 0);
1898 atomic_clear_int(&ip->flags, HAMMER2_INODE_MTIME);
1899 wipdata->mtime = ip->mtime;
1903 if ((ip->flags & HAMMER2_INODE_RESIZED) && ip->size < ripdata->size) {
1904 wipdata = hammer2_cluster_modify_ip(trans, ip, cparent, 0);
1905 wipdata->size = ip->size;
1908 atomic_clear_int(&ip->flags, HAMMER2_INODE_RESIZED);
1911 * We must delete any chains beyond the EOF. The chain
1912 * straddling the EOF will be pending in the bioq.
1914 lbase = (ripdata->size + HAMMER2_PBUFMASK64) &
1915 ~HAMMER2_PBUFMASK64;
1916 dparent = hammer2_cluster_lookup_init(&ip->cluster, 0);
1917 cluster = hammer2_cluster_lookup(dparent, &key_next,
1918 lbase, (hammer2_key_t)-1,
1919 HAMMER2_LOOKUP_NODATA);
1922 * Degenerate embedded case, nothing to loop on
1924 switch (hammer2_cluster_type(cluster)) {
1925 case HAMMER2_BREF_TYPE_INODE:
1926 hammer2_cluster_unlock(cluster);
1929 case HAMMER2_BREF_TYPE_DATA:
1930 hammer2_cluster_delete(trans, dparent, cluster,
1931 HAMMER2_DELETE_PERMANENT);
1934 cluster = hammer2_cluster_next(dparent, cluster,
1936 key_next, (hammer2_key_t)-1,
1937 HAMMER2_LOOKUP_NODATA);
1941 hammer2_cluster_lookup_done(dparent);
1943 if ((ip->flags & HAMMER2_INODE_RESIZED) && ip->size > ripdata->size) {
1944 wipdata = hammer2_cluster_modify_ip(trans, ip, cparent, 0);
1945 wipdata->size = ip->size;
1946 atomic_clear_int(&ip->flags, HAMMER2_INODE_RESIZED);
1949 * When resizing larger we may not have any direct-data
1952 if ((wipdata->op_flags & HAMMER2_OPFLAG_DIRECTDATA) &&
1953 ip->size > HAMMER2_EMBEDDED_BYTES) {
1954 wipdata->op_flags &= ~HAMMER2_OPFLAG_DIRECTDATA;
1955 bzero(&wipdata->u.blockset,
1956 sizeof(wipdata->u.blockset));
1962 hammer2_cluster_modsync(cparent);