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: If caller passes HAMMER2_RESOLVE_RDONLY the exclusive locking code
95 * will feel free to reduce the chain set in the cluster as an
96 * optimization. It will still be validated against the quorum if
97 * appropriate, but the optimization might be able to reduce data
98 * accesses to one node. This flag is automatically set if the inode
99 * is locked with HAMMER2_RESOLVE_SHARED.
102 hammer2_inode_lock(hammer2_inode_t *ip, int how)
104 hammer2_cluster_t *cluster;
106 hammer2_inode_ref(ip);
109 * Inode structure mutex
111 if (how & HAMMER2_RESOLVE_SHARED) {
112 how |= HAMMER2_RESOLVE_RDONLY;
113 hammer2_mtx_sh(&ip->lock);
115 hammer2_mtx_ex(&ip->lock);
119 * Create a copy of ip->cluster and lock it. Note that the copy
120 * will have a ref on the cluster AND its chains and we don't want
121 * a second ref to either when we lock it.
123 * The copy will not have a focus until it is locked.
125 * Exclusive inode locks set the template focus chain in (ip)
126 * as a hint. Cluster locks can ALWAYS replace the focus in the
127 * working copy if the hint does not work out, so beware.
129 cluster = hammer2_cluster_copy(&ip->cluster);
130 hammer2_cluster_lock(cluster, how);
131 hammer2_cluster_resolve(cluster);
134 * cluster->focus will be set if resolving RESOLVE_ALWAYS, but
135 * only update the cached focus in the inode structure when taking
136 * out an exclusive lock.
138 if ((how & HAMMER2_RESOLVE_SHARED) == 0)
139 ip->cluster.focus = cluster->focus;
142 * Initialize pmp->inode_tid and pmp->modify_tid on first access
143 * to the root of mount that resolves good.
144 * XXX probably not the best place for this.
146 if (ip->pmp->inode_tid == 0 &&
147 cluster->error == 0 && cluster->focus) {
148 const hammer2_inode_data_t *ripdata;
149 hammer2_pfs_t *pmp = ip->pmp;
150 hammer2_blockref_t bref;
152 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
153 hammer2_cluster_bref(cluster, &bref);
154 pmp->inode_tid = ripdata->pfs_inum + 1;
155 pmp->modify_tid = bref.modify_tid;
156 kprintf("PMP focus good set nextino=%ld mod=%016jx\n",
157 pmp->inode_tid, pmp->modify_tid);
164 hammer2_inode_unlock(hammer2_inode_t *ip, hammer2_cluster_t *cluster)
167 hammer2_cluster_unlock(cluster);
168 hammer2_cluster_drop(cluster);
170 hammer2_mtx_unlock(&ip->lock);
171 hammer2_inode_drop(ip);
175 * Temporarily release a lock held shared or exclusive. Caller must
176 * hold the lock shared or exclusive on call and lock will be released
179 * Restore a lock that was temporarily released.
182 hammer2_inode_lock_temp_release(hammer2_inode_t *ip)
184 return hammer2_mtx_temp_release(&ip->lock);
188 hammer2_inode_lock_temp_restore(hammer2_inode_t *ip, hammer2_mtx_state_t ostate)
190 hammer2_mtx_temp_restore(&ip->lock, ostate);
194 * Upgrade a shared inode lock to exclusive and return. If the inode lock
195 * is already held exclusively this is a NOP.
197 * The caller MUST hold the inode lock either shared or exclusive on call
198 * and will own the lock exclusively on return.
200 * Returns non-zero if the lock was already exclusive prior to the upgrade.
203 hammer2_inode_lock_upgrade(hammer2_inode_t *ip)
207 if (mtx_islocked_ex(&ip->lock)) {
210 hammer2_mtx_unlock(&ip->lock);
211 hammer2_mtx_ex(&ip->lock);
218 * Downgrade an inode lock from exclusive to shared only if the inode
219 * lock was previously shared. If the inode lock was previously exclusive,
223 hammer2_inode_lock_downgrade(hammer2_inode_t *ip, int wasexclusive)
225 if (wasexclusive == 0)
226 mtx_downgrade(&ip->lock);
230 * Lookup an inode by inode number
233 hammer2_inode_lookup(hammer2_pfs_t *pmp, hammer2_tid_t inum)
241 hammer2_spin_ex(&pmp->inum_spin);
242 ip = RB_LOOKUP(hammer2_inode_tree, &pmp->inum_tree, inum);
244 hammer2_inode_ref(ip);
245 hammer2_spin_unex(&pmp->inum_spin);
251 * Adding a ref to an inode is only legal if the inode already has at least
254 * (can be called with spinlock held)
257 hammer2_inode_ref(hammer2_inode_t *ip)
259 atomic_add_int(&ip->refs, 1);
263 * Drop an inode reference, freeing the inode when the last reference goes
267 hammer2_inode_drop(hammer2_inode_t *ip)
270 hammer2_inode_t *pip;
278 * Transition to zero, must interlock with
279 * the inode inumber lookup tree (if applicable).
280 * It should not be possible for anyone to race
281 * the transition to 0.
286 hammer2_spin_ex(&pmp->inum_spin);
288 if (atomic_cmpset_int(&ip->refs, 1, 0)) {
289 KKASSERT(hammer2_mtx_refs(&ip->lock) == 0);
290 if (ip->flags & HAMMER2_INODE_ONRBTREE) {
291 atomic_clear_int(&ip->flags,
292 HAMMER2_INODE_ONRBTREE);
293 RB_REMOVE(hammer2_inode_tree,
294 &pmp->inum_tree, ip);
296 hammer2_spin_unex(&pmp->inum_spin);
303 * Cleaning out ip->cluster isn't entirely
306 hammer2_inode_repoint(ip, NULL, NULL);
309 * We have to drop pip (if non-NULL) to
310 * dispose of our implied reference from
311 * ip->pip. We can simply loop on it.
313 kfree(ip, pmp->minode);
314 atomic_add_long(&pmp->inmem_inodes, -1);
316 /* continue with pip (can be NULL) */
318 hammer2_spin_unex(&ip->pmp->inum_spin);
322 * Non zero transition
324 if (atomic_cmpset_int(&ip->refs, refs, refs - 1))
331 * Get the vnode associated with the given inode, allocating the vnode if
332 * necessary. The vnode will be returned exclusively locked.
334 * The caller must lock the inode (shared or exclusive).
336 * Great care must be taken to avoid deadlocks and vnode acquisition/reclaim
340 hammer2_igetv(hammer2_inode_t *ip, hammer2_cluster_t *cparent, int *errorp)
342 const hammer2_inode_data_t *ripdata;
347 KKASSERT(pmp != NULL);
350 ripdata = &hammer2_cluster_rdata(cparent)->ipdata;
354 * Attempt to reuse an existing vnode assignment. It is
355 * possible to race a reclaim so the vget() may fail. The
356 * inode must be unlocked during the vget() to avoid a
357 * deadlock against a reclaim.
364 * Inode must be unlocked during the vget() to avoid
365 * possible deadlocks, but leave the ip ref intact.
367 * vnode is held to prevent destruction during the
368 * vget(). The vget() can still fail if we lost
369 * a reclaim race on the vnode.
371 hammer2_mtx_state_t ostate;
374 ostate = hammer2_inode_lock_temp_release(ip);
375 if (vget(vp, LK_EXCLUSIVE)) {
377 hammer2_inode_lock_temp_restore(ip, ostate);
380 hammer2_inode_lock_temp_restore(ip, ostate);
382 /* vp still locked and ref from vget */
384 kprintf("hammer2: igetv race %p/%p\n",
394 * No vnode exists, allocate a new vnode. Beware of
395 * allocation races. This function will return an
396 * exclusively locked and referenced vnode.
398 *errorp = getnewvnode(VT_HAMMER2, pmp->mp, &vp, 0, 0);
400 kprintf("hammer2: igetv getnewvnode failed %d\n",
407 * Lock the inode and check for an allocation race.
409 wasexclusive = hammer2_inode_lock_upgrade(ip);
410 if (ip->vp != NULL) {
413 hammer2_inode_lock_downgrade(ip, wasexclusive);
417 switch (ripdata->type) {
418 case HAMMER2_OBJTYPE_DIRECTORY:
421 case HAMMER2_OBJTYPE_REGFILE:
423 vinitvmio(vp, ripdata->size,
425 (int)ripdata->size & HAMMER2_LBUFMASK);
427 case HAMMER2_OBJTYPE_SOFTLINK:
429 * XXX for now we are using the generic file_read
430 * and file_write code so we need a buffer cache
434 vinitvmio(vp, ripdata->size,
436 (int)ripdata->size & HAMMER2_LBUFMASK);
438 case HAMMER2_OBJTYPE_CDEV:
441 case HAMMER2_OBJTYPE_BDEV:
442 vp->v_ops = &pmp->mp->mnt_vn_spec_ops;
443 if (ripdata->type != HAMMER2_OBJTYPE_CDEV)
445 addaliasu(vp, ripdata->rmajor, ripdata->rminor);
447 case HAMMER2_OBJTYPE_FIFO:
449 vp->v_ops = &pmp->mp->mnt_vn_fifo_ops;
452 panic("hammer2: unhandled objtype %d", ripdata->type);
456 if (ip == pmp->iroot)
457 vsetflags(vp, VROOT);
461 hammer2_inode_ref(ip); /* vp association */
462 hammer2_inode_lock_downgrade(ip, wasexclusive);
467 * Return non-NULL vp and *errorp == 0, or NULL vp and *errorp != 0.
469 if (hammer2_debug & 0x0002) {
470 kprintf("igetv vp %p refs 0x%08x aux 0x%08x\n",
471 vp, vp->v_refcnt, vp->v_auxrefs);
477 * Returns the inode associated with the passed-in cluster, creating the
478 * inode if necessary and synchronizing it to the passed-in cluster otherwise.
480 * The passed-in cluster must be locked and will remain locked on return.
481 * The returned inode will be locked and the caller may dispose of both
482 * via hammer2_inode_unlock_ex(). However, if the caller needs to resolve
483 * a hardlink it must ref/unlock/relock/drop the inode.
485 * The hammer2_inode structure regulates the interface between the high level
486 * kernel VNOPS API and the filesystem backend (the chains).
488 * On return the inode is locked with the supplied cluster.
491 hammer2_inode_get(hammer2_pfs_t *pmp, hammer2_inode_t *dip,
492 hammer2_cluster_t *cluster)
494 hammer2_inode_t *nip;
495 const hammer2_inode_data_t *iptmp;
496 const hammer2_inode_data_t *nipdata;
498 KKASSERT(cluster == NULL ||
499 hammer2_cluster_type(cluster) == HAMMER2_BREF_TYPE_INODE);
503 * Interlocked lookup/ref of the inode. This code is only needed
504 * when looking up inodes with nlinks != 0 (TODO: optimize out
505 * otherwise and test for duplicates).
507 * Cluster can be NULL during the initial pfs allocation.
511 iptmp = &hammer2_cluster_rdata(cluster)->ipdata;
512 nip = hammer2_inode_lookup(pmp, iptmp->inum);
516 hammer2_mtx_ex(&nip->lock);
519 * Handle SMP race (not applicable to the super-root spmp
520 * which can't index inodes due to duplicative inode numbers).
522 if (pmp->spmp_hmp == NULL &&
523 (nip->flags & HAMMER2_INODE_ONRBTREE) == 0) {
524 hammer2_mtx_unlock(&nip->lock);
525 hammer2_inode_drop(nip);
528 hammer2_inode_repoint(nip, NULL, cluster);
534 * We couldn't find the inode number, create a new inode.
536 nip = kmalloc(sizeof(*nip), pmp->minode, M_WAITOK | M_ZERO);
537 spin_init(&nip->cluster_spin, "h2clspin");
538 atomic_add_long(&pmp->inmem_inodes, 1);
539 hammer2_pfs_memory_inc(pmp);
540 hammer2_pfs_memory_wakeup(pmp);
542 nip->flags = HAMMER2_INODE_SROOT;
545 * Initialize nip's cluster. A cluster is provided for normal
546 * inodes but typically not for the super-root or PFS inodes.
548 nip->cluster.refs = 1;
549 nip->cluster.pmp = pmp;
550 nip->cluster.flags |= HAMMER2_CLUSTER_INODE;
552 nipdata = &hammer2_cluster_rdata(cluster)->ipdata;
553 nip->inum = nipdata->inum;
554 nip->size = nipdata->size;
555 nip->mtime = nipdata->mtime;
556 hammer2_inode_repoint(nip, NULL, cluster);
558 nip->inum = 1; /* PFS inum is always 1 XXX */
559 /* mtime will be updated when a cluster is available */
562 nip->pip = dip; /* can be NULL */
564 hammer2_inode_ref(dip); /* ref dip for nip->pip */
569 * ref and lock on nip gives it state compatible to after a
570 * hammer2_inode_lock() call.
573 hammer2_mtx_init(&nip->lock, "h2inode");
574 hammer2_mtx_ex(&nip->lock);
575 /* combination of thread lock and chain lock == inode lock */
578 * Attempt to add the inode. If it fails we raced another inode
579 * get. Undo all the work and try again.
581 if (pmp->spmp_hmp == NULL) {
582 hammer2_spin_ex(&pmp->inum_spin);
583 if (RB_INSERT(hammer2_inode_tree, &pmp->inum_tree, nip)) {
584 hammer2_spin_unex(&pmp->inum_spin);
585 hammer2_mtx_unlock(&nip->lock);
586 hammer2_inode_drop(nip);
589 atomic_set_int(&nip->flags, HAMMER2_INODE_ONRBTREE);
590 hammer2_spin_unex(&pmp->inum_spin);
597 * Create a new inode in the specified directory using the vattr to
598 * figure out the type of inode.
600 * If no error occurs the new inode with its cluster locked is returned in
601 * *nipp, otherwise an error is returned and *nipp is set to NULL.
603 * If vap and/or cred are NULL the related fields are not set and the
604 * inode type defaults to a directory. This is used when creating PFSs
605 * under the super-root, so the inode number is set to 1 in this case.
607 * dip is not locked on entry.
609 * NOTE: When used to create a snapshot, the inode is temporarily associated
610 * with the super-root spmp. XXX should pass new pmp for snapshot.
613 hammer2_inode_create(hammer2_trans_t *trans, hammer2_inode_t *dip,
614 struct vattr *vap, struct ucred *cred,
615 const uint8_t *name, size_t name_len,
616 hammer2_cluster_t **clusterp,
617 int flags, int *errorp)
619 const hammer2_inode_data_t *dipdata;
620 hammer2_inode_data_t *nipdata;
621 hammer2_cluster_t *cluster;
622 hammer2_cluster_t *cparent;
623 hammer2_inode_t *nip;
624 hammer2_key_t key_dummy;
631 uint8_t dip_comp_algo;
632 uint8_t dip_check_algo;
634 lhc = hammer2_dirhash(name, name_len);
638 * Locate the inode or indirect block to create the new
639 * entry in. At the same time check for key collisions
640 * and iterate until we don't get one.
642 * NOTE: hidden inodes do not have iterators.
645 cparent = hammer2_inode_lock(dip, HAMMER2_RESOLVE_ALWAYS);
646 dipdata = &hammer2_cluster_rdata(cparent)->ipdata;
647 dip_uid = dipdata->uid;
648 dip_gid = dipdata->gid;
649 dip_mode = dipdata->mode;
650 dip_comp_algo = dipdata->comp_algo;
651 dip_check_algo = dipdata->check_algo;
655 cluster = hammer2_cluster_lookup(cparent, &key_dummy,
659 if ((lhc & HAMMER2_DIRHASH_VISIBLE) == 0)
661 if ((lhc & HAMMER2_DIRHASH_LOMASK) == HAMMER2_DIRHASH_LOMASK)
663 hammer2_cluster_unlock(cluster);
664 hammer2_cluster_drop(cluster);
670 error = hammer2_cluster_create(trans, cparent, &cluster,
672 HAMMER2_BREF_TYPE_INODE,
677 kprintf("CREATE INODE %*.*s chain=%p\n",
678 (int)name_len, (int)name_len, name,
679 (cluster ? cluster->focus : NULL));
683 * Cleanup and handle retries.
685 if (error == EAGAIN) {
686 hammer2_cluster_ref(cparent);
687 hammer2_inode_unlock(dip, cparent);
688 hammer2_cluster_wait(cparent);
689 hammer2_cluster_drop(cparent);
692 hammer2_inode_unlock(dip, cparent);
696 KKASSERT(cluster == NULL);
702 * Set up the new inode.
704 * NOTE: *_get() integrates chain's lock into the inode lock.
706 * NOTE: Only one new inode can currently be created per
707 * transaction. If the need arises we can adjust
708 * hammer2_trans_init() to allow more.
710 * NOTE: nipdata will have chain's blockset data.
712 KKASSERT(cluster->focus->flags & HAMMER2_CHAIN_MODIFIED);
713 nipdata = &hammer2_cluster_wdata(cluster)->ipdata;
714 nipdata->inum = trans->inode_tid;
715 hammer2_cluster_modsync(cluster);
716 nip = hammer2_inode_get(dip->pmp, dip, cluster);
717 nipdata = &hammer2_cluster_wdata(cluster)->ipdata;
720 KKASSERT(trans->inodes_created == 0);
721 nipdata->type = hammer2_get_obj_type(vap->va_type);
722 nipdata->inum = trans->inode_tid;
723 ++trans->inodes_created;
725 switch (nipdata->type) {
726 case HAMMER2_OBJTYPE_CDEV:
727 case HAMMER2_OBJTYPE_BDEV:
728 nipdata->rmajor = vap->va_rmajor;
729 nipdata->rminor = vap->va_rminor;
735 nipdata->type = HAMMER2_OBJTYPE_DIRECTORY;
739 /* Inherit parent's inode compression mode. */
740 nip->comp_heuristic = 0;
741 nipdata->comp_algo = dip_comp_algo;
742 nipdata->check_algo = dip_check_algo;
743 nipdata->version = HAMMER2_INODE_VERSION_ONE;
744 hammer2_update_time(&nipdata->ctime);
745 nipdata->mtime = nipdata->ctime;
747 nipdata->mode = vap->va_mode;
750 if (dip && dip->pmp) {
751 xuid = hammer2_to_unix_xid(&dip_uid);
752 xuid = vop_helper_create_uid(dip->pmp->mp,
758 /* super-root has no dip and/or pmp */
761 if (vap->va_vaflags & VA_UID_UUID_VALID)
762 nipdata->uid = vap->va_uid_uuid;
763 else if (vap->va_uid != (uid_t)VNOVAL)
764 hammer2_guid_to_uuid(&nipdata->uid, vap->va_uid);
766 hammer2_guid_to_uuid(&nipdata->uid, xuid);
768 if (vap->va_vaflags & VA_GID_UUID_VALID)
769 nipdata->gid = vap->va_gid_uuid;
770 else if (vap->va_gid != (gid_t)VNOVAL)
771 hammer2_guid_to_uuid(&nipdata->gid, vap->va_gid);
773 nipdata->gid = dip_gid;
777 * Regular files and softlinks allow a small amount of data to be
778 * directly embedded in the inode. This flag will be cleared if
779 * the size is extended past the embedded limit.
781 if (nipdata->type == HAMMER2_OBJTYPE_REGFILE ||
782 nipdata->type == HAMMER2_OBJTYPE_SOFTLINK) {
783 nipdata->op_flags |= HAMMER2_OPFLAG_DIRECTDATA;
786 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
787 bcopy(name, nipdata->filename, name_len);
788 nipdata->name_key = lhc;
789 nipdata->name_len = name_len;
790 hammer2_cluster_modsync(cluster);
797 * The cluster has been removed from the original directory and replaced
798 * with a hardlink pointer. Move the cluster to the specified parent
799 * directory, change the filename to "0xINODENUMBER", and adjust the key.
800 * The cluster becomes our invisible hardlink target.
802 * The original cluster must be deleted on entry.
806 hammer2_hardlink_shiftup(hammer2_trans_t *trans, hammer2_cluster_t *cluster,
807 hammer2_inode_t *dip, hammer2_cluster_t *dcluster,
808 int nlinks, int *errorp)
810 const hammer2_inode_data_t *iptmp;
811 hammer2_inode_data_t *nipdata;
812 hammer2_cluster_t *xcluster;
813 hammer2_key_t key_dummy;
815 hammer2_blockref_t bref;
817 iptmp = &hammer2_cluster_rdata(cluster)->ipdata;
819 KKASSERT((lhc & HAMMER2_DIRHASH_VISIBLE) == 0);
822 * Locate the inode or indirect block to create the new
823 * entry in. lhc represents the inode number so there is
824 * no collision iteration.
826 * There should be no key collisions with invisible inode keys.
828 * WARNING! Must use inode_lock_ex() on dip to handle a stale
829 * dip->cluster cache.
832 xcluster = hammer2_cluster_lookup(dcluster, &key_dummy,
835 kprintf("X3 chain %p dip %p dchain %p dip->chain %p\n",
836 xcluster->focus, dip, dcluster->focus,
838 hammer2_cluster_unlock(xcluster);
839 hammer2_cluster_drop(xcluster);
848 * Handle the error case
852 KKASSERT(xcluster == NULL);
857 * Use xcluster as a placeholder for (lhc). Duplicate cluster to the
858 * same target bref as xcluster and then delete xcluster. The
859 * duplication occurs after xcluster in flush order even though
860 * xcluster is deleted after the duplication. XXX
862 * WARNING! Duplications (to a different parent) can cause indirect
863 * blocks to be inserted, refactor xcluster.
865 * WARNING! Only key and keybits is extracted from a passed-in bref.
867 hammer2_cluster_bref(cluster, &bref);
868 bref.key = lhc; /* invisible dir entry key */
870 hammer2_cluster_rename(trans, &bref, dcluster, cluster, 0);
873 * cluster is now 'live' again.. adjust the filename.
875 * Directory entries are inodes but this is a hidden hardlink
876 * target. The name isn't used but to ease debugging give it
877 * a name after its inode number.
879 hammer2_cluster_modify(trans, cluster, 0);
880 nipdata = &hammer2_cluster_wdata(cluster)->ipdata;
881 ksnprintf(nipdata->filename, sizeof(nipdata->filename),
882 "0x%016jx", (intmax_t)nipdata->inum);
883 nipdata->name_len = strlen(nipdata->filename);
884 nipdata->name_key = lhc;
885 nipdata->nlinks += nlinks;
886 hammer2_cluster_modsync(cluster);
890 * Connect the target inode represented by (cluster) to the media topology
891 * at (dip, name, len). The caller can pass a rough *chainp, this function
892 * will issue lookup()s to position the parent chain properly for the
895 * If hlink is TRUE this function creates an OBJTYPE_HARDLINK directory
896 * entry instead of connecting (cluster).
898 * If hlink is FALSE this function expects (cluster) to be unparented.
901 hammer2_inode_connect(hammer2_trans_t *trans,
902 hammer2_cluster_t **clusterp, int hlink,
903 hammer2_inode_t *dip, hammer2_cluster_t *dcluster,
904 const uint8_t *name, size_t name_len,
907 hammer2_inode_data_t *wipdata;
908 hammer2_cluster_t *ocluster;
909 hammer2_cluster_t *ncluster;
910 hammer2_key_t key_dummy;
914 * Since ocluster is either disconnected from the topology or
915 * represents a hardlink terminus which is always a parent of or
916 * equal to dip, we should be able to safely lock dip->chain for
919 * WARNING! Must use inode_lock_ex() on dip to handle a stale
922 * If name is non-NULL we calculate lhc, else we use the passed-in
925 ocluster = *clusterp;
928 lhc = hammer2_dirhash(name, name_len);
931 * Locate the inode or indirect block to create the new
932 * entry in. At the same time check for key collisions
933 * and iterate until we don't get one.
937 ncluster = hammer2_cluster_lookup(dcluster, &key_dummy,
939 if (ncluster == NULL)
941 if ((lhc & HAMMER2_DIRHASH_LOMASK) ==
942 HAMMER2_DIRHASH_LOMASK) {
945 hammer2_cluster_unlock(ncluster);
946 hammer2_cluster_drop(ncluster);
952 * Reconnect to specific key (used when moving
953 * unlinked-but-open files into the hidden directory).
955 ncluster = hammer2_cluster_lookup(dcluster, &key_dummy,
957 KKASSERT(ncluster == NULL);
964 * Hardlink pointer needed, create totally fresh
967 * We must refactor ocluster because it might have
968 * been shifted into an indirect cluster by the
971 KKASSERT(ncluster == NULL);
972 error = hammer2_cluster_create(trans,
975 HAMMER2_BREF_TYPE_INODE,
980 * Reconnect the original cluster under the new name.
981 * Original cluster must have already been deleted by
984 * WARNING! Can cause held-over clusters to require a
985 * refactor. Fortunately we have none (our
986 * locked clusters are passed into and
987 * modified by the call).
991 error = hammer2_cluster_create(trans,
994 HAMMER2_BREF_TYPE_INODE,
1003 KKASSERT(error != EAGAIN);
1006 * ncluster should be NULL on error, leave ocluster
1007 * (ocluster == *clusterp) alone.
1010 KKASSERT(ncluster == NULL);
1015 * Directory entries are inodes so if the name has changed we have
1016 * to update the inode.
1018 * When creating an OBJTYPE_HARDLINK entry remember to unlock the
1019 * cluster, the caller will access the hardlink via the actual hardlink
1020 * target file and not the hardlink pointer entry, so we must still
1023 if (hlink && hammer2_hardlink_enable >= 0) {
1025 * Create the HARDLINK pointer. oip represents the hardlink
1026 * target in this situation.
1028 * We will return ocluster (the hardlink target).
1030 hammer2_cluster_modify(trans, ncluster, 0);
1031 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
1032 wipdata = &hammer2_cluster_wdata(ncluster)->ipdata;
1033 bcopy(name, wipdata->filename, name_len);
1034 wipdata->name_key = lhc;
1035 wipdata->name_len = name_len;
1036 wipdata->target_type =
1037 hammer2_cluster_rdata(ocluster)->ipdata.type;
1038 wipdata->type = HAMMER2_OBJTYPE_HARDLINK;
1039 wipdata->inum = hammer2_cluster_rdata(ocluster)->ipdata.inum;
1040 wipdata->version = HAMMER2_INODE_VERSION_ONE;
1041 wipdata->nlinks = 1;
1042 wipdata->op_flags = HAMMER2_OPFLAG_DIRECTDATA;
1043 hammer2_cluster_modsync(ncluster);
1044 hammer2_cluster_unlock(ncluster);
1045 hammer2_cluster_drop(ncluster);
1046 ncluster = ocluster;
1050 * ncluster is a duplicate of ocluster at the new location.
1051 * We must fixup the name stored in the inode data.
1052 * The bref key has already been adjusted by inode_connect().
1054 hammer2_cluster_modify(trans, ncluster, 0);
1055 wipdata = &hammer2_cluster_wdata(ncluster)->ipdata;
1057 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
1058 bcopy(name, wipdata->filename, name_len);
1059 wipdata->name_key = lhc;
1060 wipdata->name_len = name_len;
1061 wipdata->nlinks = 1;
1062 hammer2_cluster_modsync(ncluster);
1066 * We are replacing ocluster with ncluster, unlock ocluster. In the
1067 * case where ocluster is left unchanged the code above sets
1068 * ncluster to ocluster and ocluster to NULL, resulting in a NOP here.
1071 hammer2_cluster_unlock(ocluster);
1072 hammer2_cluster_drop(ocluster);
1074 *clusterp = ncluster;
1080 * Repoint ip->cluster's chains to cluster's chains and fixup the default
1081 * focus. Only valid elements are repointed. Invalid elements have to be
1082 * adjusted by the appropriate slave sync threads.
1084 * Caller must hold the inode and cluster exclusive locked, if not NULL,
1085 * must also be locked.
1087 * Cluster may be NULL to clean out any chains in ip->cluster.
1090 hammer2_inode_repoint(hammer2_inode_t *ip, hammer2_inode_t *pip,
1091 hammer2_cluster_t *cluster)
1093 hammer2_chain_t *dropch[HAMMER2_MAXCLUSTER];
1094 hammer2_chain_t *ochain;
1095 hammer2_chain_t *nchain;
1096 hammer2_inode_t *opip;
1099 bzero(dropch, sizeof(dropch));
1102 * Replace chains in ip->cluster with chains from cluster and
1103 * adjust the focus if necessary.
1105 * NOTE: nchain and/or ochain can be NULL due to gaps
1106 * in the cluster arrays.
1108 hammer2_spin_ex(&ip->cluster_spin);
1109 for (i = 0; cluster && i < cluster->nchains; ++i) {
1111 * Do not replace invalid elements as this might race
1112 * syncthr replacements.
1114 if (cluster->array[i].flags & HAMMER2_CITEM_INVALID)
1118 * Do not replace elements which are the same. Also handle
1119 * element count discrepancies.
1121 nchain = cluster->array[i].chain;
1122 if (i < ip->cluster.nchains) {
1123 ochain = ip->cluster.array[i].chain;
1124 if (ochain == nchain)
1133 ip->cluster.array[i].chain = nchain;
1134 ip->cluster.array[i].flags &= ~HAMMER2_CITEM_INVALID;
1135 ip->cluster.array[i].flags |= cluster->array[i].flags &
1136 HAMMER2_CITEM_INVALID;
1138 hammer2_chain_ref(nchain);
1143 * Release any left-over chains in ip->cluster.
1145 while (i < ip->cluster.nchains) {
1146 nchain = ip->cluster.array[i].chain;
1148 ip->cluster.array[i].chain = NULL;
1149 ip->cluster.array[i].flags |= HAMMER2_CITEM_INVALID;
1156 * Fixup fields. Note that the inode-embedded cluster is never
1160 ip->cluster.nchains = cluster->nchains;
1161 ip->cluster.focus = cluster->focus;
1162 ip->cluster.flags = cluster->flags & ~HAMMER2_CLUSTER_LOCKED;
1164 ip->cluster.nchains = 0;
1165 ip->cluster.focus = NULL;
1166 ip->cluster.flags &= ~HAMMER2_CLUSTER_ZFLAGS;
1170 * Repoint ip->pip if requested (non-NULL pip).
1172 if (pip && ip->pip != pip) {
1174 hammer2_inode_ref(pip);
1179 hammer2_spin_unex(&ip->cluster_spin);
1182 * Cleanup outside of spinlock
1186 hammer2_chain_drop(dropch[i]);
1189 hammer2_inode_drop(opip);
1193 * Repoint a single element from the cluster to the ip. Used by the
1194 * synchronization threads to piecemeal update inodes. Does not change
1195 * focus and requires inode to be re-locked to clean-up flags (XXX).
1198 hammer2_inode_repoint_one(hammer2_inode_t *ip, hammer2_cluster_t *cluster,
1201 hammer2_chain_t *ochain;
1202 hammer2_chain_t *nchain;
1205 hammer2_spin_ex(&ip->cluster_spin);
1206 KKASSERT(idx < cluster->nchains);
1207 if (idx < ip->cluster.nchains) {
1208 ochain = ip->cluster.array[idx].chain;
1209 nchain = cluster->array[idx].chain;
1212 nchain = cluster->array[idx].chain;
1213 ip->cluster.nchains = idx + 1;
1214 for (i = ip->cluster.nchains; i <= idx; ++i) {
1215 bzero(&ip->cluster.array[i],
1216 sizeof(ip->cluster.array[i]));
1217 ip->cluster.array[i].flags |= HAMMER2_CITEM_INVALID;
1220 if (ochain != nchain) {
1224 ip->cluster.array[idx].chain = nchain;
1225 ip->cluster.array[idx].flags &= ~HAMMER2_CITEM_INVALID;
1226 ip->cluster.array[idx].flags |= cluster->array[idx].flags &
1227 HAMMER2_CITEM_INVALID;
1229 hammer2_spin_unex(&ip->cluster_spin);
1230 if (ochain != nchain) {
1232 hammer2_chain_ref(nchain);
1234 hammer2_chain_drop(ochain);
1239 * Unlink the file from the specified directory inode. The directory inode
1240 * does not need to be locked.
1242 * isdir determines whether a directory/non-directory check should be made.
1243 * No check is made if isdir is set to -1.
1245 * isopen specifies whether special unlink-with-open-descriptor handling
1246 * must be performed. If set to -1 the caller is deleting a PFS and we
1247 * check whether the chain is mounted or not (chain->pmp != NULL). 1 is
1248 * implied if it is mounted.
1250 * If isopen is 1 and nlinks drops to 0 this function must move the chain
1251 * to a special hidden directory until last-close occurs on the file.
1253 * NOTE! The underlying file can still be active with open descriptors
1254 * or if the chain is being manually held (e.g. for rename).
1256 * The caller is responsible for fixing up ip->chain if e.g. a
1257 * rename occurs (see chain_duplicate()).
1259 * NOTE! The chain is not deleted if it is moved to the hidden directory,
1260 * but otherwise will be deleted.
1263 hammer2_unlink_file(hammer2_trans_t *trans, hammer2_inode_t *dip,
1264 const uint8_t *name, size_t name_len,
1265 int isdir, int *hlinkp, struct nchandle *nch,
1268 const hammer2_inode_data_t *ripdata;
1269 hammer2_inode_data_t *wipdata;
1270 hammer2_cluster_t *cparent;
1271 hammer2_cluster_t *hcluster;
1272 hammer2_cluster_t *hparent;
1273 hammer2_cluster_t *cluster;
1274 hammer2_cluster_t *dparent;
1275 hammer2_cluster_t *dcluster;
1276 hammer2_key_t key_dummy;
1277 hammer2_key_t key_next;
1288 lhc = hammer2_dirhash(name, name_len);
1292 * Search for the filename in the directory
1294 cparent = hammer2_inode_lock(dip, HAMMER2_RESOLVE_ALWAYS);
1295 cluster = hammer2_cluster_lookup(cparent, &key_next,
1296 lhc, lhc + HAMMER2_DIRHASH_LOMASK, 0);
1298 if (hammer2_cluster_type(cluster) == HAMMER2_BREF_TYPE_INODE) {
1299 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
1300 if (ripdata->name_len == name_len &&
1301 bcmp(ripdata->filename, name, name_len) == 0) {
1305 cluster = hammer2_cluster_next(cparent, cluster, &key_next,
1307 lhc + HAMMER2_DIRHASH_LOMASK,
1310 hammer2_inode_unlock(dip, NULL); /* retain cparent */
1313 * Not found or wrong type (isdir < 0 disables the type check).
1314 * If a hardlink pointer, type checks use the hardlink target.
1316 if (cluster == NULL) {
1320 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
1321 type = ripdata->type;
1322 if (type == HAMMER2_OBJTYPE_HARDLINK) {
1324 type = ripdata->target_type;
1327 if (type == HAMMER2_OBJTYPE_DIRECTORY && isdir == 0) {
1331 if (type != HAMMER2_OBJTYPE_DIRECTORY && isdir >= 1) {
1337 * Hardlink must be resolved. We can't hold the parent locked
1338 * while we do this or we could deadlock. The physical file will
1339 * be located at or above the current directory.
1341 * We loop to reacquire the hardlink origination.
1343 * NOTE: hammer2_hardlink_find() will locate the hardlink target,
1344 * returning a modified hparent and hcluster.
1346 if (ripdata->type == HAMMER2_OBJTYPE_HARDLINK) {
1347 if (hcluster == NULL) {
1349 cluster = NULL; /* safety */
1350 hammer2_cluster_unlock(cparent);
1351 hammer2_cluster_drop(cparent);
1352 cparent = NULL; /* safety */
1353 ripdata = NULL; /* safety (associated w/cparent) */
1354 error = hammer2_hardlink_find(dip, &hparent, &hcluster);
1357 * If we couldn't find the hardlink target then some
1358 * parent directory containing the hardlink pointer
1359 * probably got renamed to above the original target,
1360 * a case not yet handled by H2.
1363 kprintf("H2 unlink_file: hardlink target for "
1364 "\"%s\" not found\n",
1366 kprintf("(likely due to known directory "
1375 * If this is a directory the directory must be empty. However, if
1376 * isdir < 0 we are doing a rename and the directory does not have
1377 * to be empty, and if isdir > 1 we are deleting a PFS/snapshot
1378 * and the directory does not have to be empty.
1380 * NOTE: We check the full key range here which covers both visible
1381 * and invisible entries. Theoretically there should be no
1382 * invisible (hardlink target) entries if there are no visible
1385 if (type == HAMMER2_OBJTYPE_DIRECTORY && isdir == 1) {
1386 dparent = hammer2_cluster_lookup_init(cluster, 0);
1387 dcluster = hammer2_cluster_lookup(dparent, &key_dummy,
1388 0, (hammer2_key_t)-1,
1389 HAMMER2_LOOKUP_NODATA);
1391 hammer2_cluster_unlock(dcluster);
1392 hammer2_cluster_drop(dcluster);
1393 hammer2_cluster_lookup_done(dparent);
1397 hammer2_cluster_lookup_done(dparent);
1403 * If this was a hardlink then (cparent, cluster) is the hardlink
1404 * pointer, which we can simply destroy outright. Discard the
1405 * clusters and replace with the hardlink target.
1408 hammer2_cluster_delete(trans, cparent, cluster,
1409 HAMMER2_DELETE_PERMANENT);
1410 hammer2_cluster_unlock(cparent);
1411 hammer2_cluster_drop(cparent);
1412 hammer2_cluster_unlock(cluster);
1413 hammer2_cluster_drop(cluster);
1421 * This leaves us with the hardlink target or non-hardlinked file
1422 * or directory in (cparent, cluster).
1424 * Delete the target when nlinks reaches 0 with special handling
1425 * to avoid I/O (to avoid actually updating the inode) for the 1->0
1426 * transition, if possible. This optimization makes rm -rf very
1429 * NOTE! In DragonFly the vnops function calls cache_unlink() after
1430 * calling us here to clean out the namecache association,
1431 * (which does not represent a ref for the open-test), and to
1432 * force finalization of the vnode if/when the last ref gets
1435 * NOTE! Files are unlinked by rename and then relinked. nch will be
1436 * passed as NULL in this situation. hammer2_inode_connect()
1439 KKASSERT(cluster != NULL);
1442 * Note: nlinks is negative when decrementing, positive when
1445 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
1446 last_link = (ripdata->nlinks + nlinks == 0);
1450 * Target nlinks has reached 0, file now unlinked (but may
1453 * nlinks will be -1 for a normal remove(). If this is the
1454 * last link we must flag the inode on deactivation. XXX race ?
1456 hammer2_inode_t *ip;
1459 ip = hammer2_inode_lookup(trans->pmp, ripdata->inum);
1461 atomic_set_int(&ip->flags,
1462 HAMMER2_INODE_ISUNLINKED);
1463 hammer2_inode_drop(ip);
1467 if (nch && cache_isopen(nch)) {
1469 * If an unlinked file is still open we must update
1470 * the inodes link count.
1472 hammer2_cluster_modify(trans, cluster, 0);
1473 wipdata = &hammer2_cluster_wdata(cluster)->ipdata;
1475 wipdata->nlinks += nlinks;
1477 if ((int64_t)wipdata->nlinks < 0) {
1478 wipdata->nlinks = 0;
1480 hammer2_cluster_modsync(cluster);
1481 hammer2_inode_move_to_hidden(trans, &cparent, &cluster,
1485 * This won't get everything if a vnode is still
1486 * present, but the cache_unlink() call the caller
1489 hammer2_cluster_delete(trans, cparent, cluster,
1490 HAMMER2_DELETE_PERMANENT);
1492 } else if (hlink == 0) {
1494 * In this situation a normal non-hardlinked file (which can
1495 * only have nlinks == 1) still has a non-zero nlinks, the
1496 * caller must be doing a RENAME operation and so is passing
1497 * a nlinks adjustment of 0, and only wishes to remove file
1498 * in order to be able to reconnect it under a different name.
1500 * In this situation we do a temporary deletion of the
1501 * chain in order to allow the file to be reconnected in
1502 * a different location.
1504 KKASSERT(nlinks == 0);
1505 hammer2_cluster_delete(trans, cparent, cluster, 0);
1508 * Links remain, must update the inode link count.
1510 hammer2_cluster_modify(trans, cluster, 0);
1511 wipdata = &hammer2_cluster_wdata(cluster)->ipdata;
1513 wipdata->nlinks += nlinks;
1514 if ((int64_t)wipdata->nlinks < 0) { /* XXX debugging */
1515 wipdata->nlinks = 0;
1517 hammer2_cluster_modsync(cluster);
1523 hammer2_cluster_unlock(cparent);
1524 hammer2_cluster_drop(cparent);
1527 hammer2_cluster_unlock(cluster);
1528 hammer2_cluster_drop(cluster);
1531 hammer2_cluster_unlock(hparent);
1532 hammer2_cluster_drop(hparent);
1535 hammer2_cluster_unlock(hcluster);
1536 hammer2_cluster_drop(hcluster);
1545 * This is called from the mount code to initialize pmp->ihidden
1548 hammer2_inode_install_hidden(hammer2_pfs_t *pmp)
1550 hammer2_trans_t trans;
1551 hammer2_cluster_t *cparent;
1552 hammer2_cluster_t *cluster;
1553 hammer2_cluster_t *scan;
1554 const hammer2_inode_data_t *ripdata;
1555 hammer2_inode_data_t *wipdata;
1556 hammer2_key_t key_dummy;
1557 hammer2_key_t key_next;
1567 * Find the hidden directory
1569 bzero(&key_dummy, sizeof(key_dummy));
1570 hammer2_trans_init(&trans, pmp, 0);
1573 * Setup for lookup, retrieve iroot's check and compression
1574 * algorithm request which was likely generated by newfs_hammer2.
1576 * The check/comp fields will probably never be used since inodes
1577 * are renamed into the hidden directory and not created relative to
1578 * the hidden directory, chain creation inherits from bref.methods,
1579 * and data chains inherit from their respective file inode *_algo
1582 cparent = hammer2_inode_lock(pmp->iroot, HAMMER2_RESOLVE_ALWAYS);
1583 ripdata = &hammer2_cluster_rdata(cparent)->ipdata;
1584 dip_check_algo = ripdata->check_algo;
1585 dip_comp_algo = ripdata->comp_algo;
1588 cluster = hammer2_cluster_lookup(cparent, &key_dummy,
1589 HAMMER2_INODE_HIDDENDIR,
1590 HAMMER2_INODE_HIDDENDIR,
1593 pmp->ihidden = hammer2_inode_get(pmp, pmp->iroot, cluster);
1594 hammer2_inode_ref(pmp->ihidden);
1597 * Remove any unlinked files which were left open as-of
1600 * Don't pass NODATA, we need the inode data so the delete
1601 * can do proper statistics updates.
1604 scan = hammer2_cluster_lookup(cluster, &key_next,
1605 0, HAMMER2_TID_MAX, 0);
1607 if (hammer2_cluster_type(scan) ==
1608 HAMMER2_BREF_TYPE_INODE) {
1609 hammer2_cluster_delete(&trans, cluster, scan,
1610 HAMMER2_DELETE_PERMANENT);
1613 scan = hammer2_cluster_next(cluster, scan, &key_next,
1614 0, HAMMER2_TID_MAX, 0);
1617 hammer2_inode_unlock(pmp->ihidden, cluster);
1618 hammer2_inode_unlock(pmp->iroot, cparent);
1619 hammer2_trans_done(&trans);
1620 kprintf("hammer2: PFS loaded hidden dir, "
1621 "removed %d dead entries\n", count);
1626 * Create the hidden directory
1628 error = hammer2_cluster_create(&trans, cparent, &cluster,
1629 HAMMER2_INODE_HIDDENDIR, 0,
1630 HAMMER2_BREF_TYPE_INODE,
1631 HAMMER2_INODE_BYTES,
1633 hammer2_inode_unlock(pmp->iroot, cparent);
1635 hammer2_cluster_modify(&trans, cluster, 0);
1636 wipdata = &hammer2_cluster_wdata(cluster)->ipdata;
1637 wipdata->type = HAMMER2_OBJTYPE_DIRECTORY;
1638 wipdata->inum = HAMMER2_INODE_HIDDENDIR;
1639 wipdata->nlinks = 1;
1640 wipdata->comp_algo = dip_comp_algo;
1641 wipdata->check_algo = dip_check_algo;
1642 hammer2_cluster_modsync(cluster);
1643 kprintf("hammer2: PFS root missing hidden directory, creating\n");
1645 pmp->ihidden = hammer2_inode_get(pmp, pmp->iroot, cluster);
1646 hammer2_inode_ref(pmp->ihidden);
1647 hammer2_inode_unlock(pmp->ihidden, cluster);
1648 hammer2_trans_done(&trans);
1652 * If an open file is unlinked H2 needs to retain the file in the topology
1653 * to ensure that its backing store is not recovered by the bulk free scan.
1654 * This also allows us to avoid having to special-case the CHAIN_DELETED flag.
1656 * To do this the file is moved to a hidden directory in the PFS root and
1657 * renamed. The hidden directory must be created if it does not exist.
1661 hammer2_inode_move_to_hidden(hammer2_trans_t *trans,
1662 hammer2_cluster_t **cparentp,
1663 hammer2_cluster_t **clusterp,
1666 hammer2_cluster_t *dcluster;
1670 pmp = (*clusterp)->pmp;
1671 KKASSERT(pmp != NULL);
1672 KKASSERT(pmp->ihidden != NULL);
1674 hammer2_cluster_delete(trans, *cparentp, *clusterp, 0);
1675 dcluster = hammer2_inode_lock(pmp->ihidden, HAMMER2_RESOLVE_ALWAYS);
1676 error = hammer2_inode_connect(trans, clusterp, 0,
1677 pmp->ihidden, dcluster,
1679 hammer2_inode_unlock(pmp->ihidden, dcluster);
1680 KKASSERT(error == 0);
1684 * Given an exclusively locked inode and cluster we consolidate the cluster
1685 * for hardlink creation, adding (nlinks) to the file's link count and
1686 * potentially relocating the inode to (cdip) which is a parent directory
1687 * common to both the current location of the inode and the intended new
1690 * Replaces (*clusterp) if consolidation occurred, unlocking the old cluster
1691 * and returning a new locked cluster.
1693 * NOTE! This function will also replace ip->cluster.
1696 hammer2_hardlink_consolidate(hammer2_trans_t *trans,
1697 hammer2_inode_t *ip,
1698 hammer2_cluster_t **clusterp,
1699 hammer2_inode_t *cdip,
1700 hammer2_cluster_t *cdcluster,
1703 const hammer2_inode_data_t *ripdata;
1704 hammer2_inode_data_t *wipdata;
1705 hammer2_cluster_t *cluster;
1706 hammer2_cluster_t *cparent;
1709 cluster = *clusterp;
1710 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
1711 if (nlinks == 0 && /* no hardlink needed */
1712 (ripdata->name_key & HAMMER2_DIRHASH_VISIBLE)) {
1716 if (hammer2_hardlink_enable == 0) { /* disallow hardlinks */
1717 hammer2_cluster_unlock(cluster);
1718 hammer2_cluster_drop(cluster);
1726 * If no change in the hardlink's target directory is required and
1727 * this is already a hardlink target, all we need to do is adjust
1730 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
1731 if (cdip == ip->pip &&
1732 (ripdata->name_key & HAMMER2_DIRHASH_VISIBLE) == 0) {
1734 hammer2_cluster_modify(trans, cluster, 0);
1735 wipdata = &hammer2_cluster_wdata(cluster)->ipdata;
1736 wipdata->nlinks += nlinks;
1737 hammer2_cluster_modsync(cluster);
1745 * Cluster is the real inode. The originating directory is locked
1746 * by the caller so we can manipulate it without worrying about races
1747 * against other lookups.
1749 * If cluster is visible we need to delete it from the current
1750 * location and create a hardlink pointer in its place. If it is
1751 * not visible we need only delete it. Then later cluster will be
1752 * renamed to a parent directory and converted (if necessary) to
1753 * a hidden inode (via shiftup).
1755 * NOTE! We must hold cparent locked through the delete/create/rename
1756 * operation to ensure that other threads block resolving to
1757 * the same hardlink, otherwise the other threads may not see
1760 KKASSERT((cluster->focus->flags & HAMMER2_CHAIN_DELETED) == 0);
1761 cparent = hammer2_cluster_parent(cluster);
1763 hammer2_cluster_delete(trans, cparent, cluster, 0);
1765 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
1766 KKASSERT(ripdata->type != HAMMER2_OBJTYPE_HARDLINK);
1767 if (ripdata->name_key & HAMMER2_DIRHASH_VISIBLE) {
1768 hammer2_cluster_t *ncluster;
1772 lhc = cluster->focus->bref.key;
1773 error = hammer2_cluster_create(trans, cparent, &ncluster,
1775 HAMMER2_BREF_TYPE_INODE,
1776 HAMMER2_INODE_BYTES,
1778 hammer2_cluster_modify(trans, ncluster, 0);
1779 wipdata = &hammer2_cluster_wdata(ncluster)->ipdata;
1781 /* wipdata->comp_algo = ripdata->comp_algo; */
1782 wipdata->comp_algo = 0;
1783 wipdata->check_algo = 0;
1784 wipdata->version = HAMMER2_INODE_VERSION_ONE;
1785 wipdata->inum = ripdata->inum;
1786 wipdata->target_type = ripdata->type;
1787 wipdata->type = HAMMER2_OBJTYPE_HARDLINK;
1788 wipdata->uflags = 0;
1789 wipdata->rmajor = 0;
1790 wipdata->rminor = 0;
1795 bzero(&wipdata->uid, sizeof(wipdata->uid));
1796 bzero(&wipdata->gid, sizeof(wipdata->gid));
1797 wipdata->op_flags = HAMMER2_OPFLAG_DIRECTDATA;
1798 wipdata->cap_flags = 0;
1801 wipdata->nlinks = 1;
1802 wipdata->iparent = 0; /* XXX */
1803 wipdata->pfs_type = 0;
1804 wipdata->pfs_inum = 0;
1805 bzero(&wipdata->pfs_clid, sizeof(wipdata->pfs_clid));
1806 bzero(&wipdata->pfs_fsid, sizeof(wipdata->pfs_fsid));
1807 wipdata->data_quota = 0;
1808 /* wipdata->data_count = 0; */
1809 wipdata->inode_quota = 0;
1810 /* wipdata->inode_count = 0; */
1811 wipdata->attr_tid = 0;
1812 wipdata->dirent_tid = 0;
1813 bzero(&wipdata->u, sizeof(wipdata->u));
1814 bcopy(ripdata->filename, wipdata->filename, ripdata->name_len);
1815 wipdata->name_key = ncluster->focus->bref.key;
1816 wipdata->name_len = ripdata->name_len;
1817 /* XXX transaction ids */
1818 hammer2_cluster_modsync(ncluster);
1819 hammer2_cluster_unlock(ncluster);
1820 hammer2_cluster_drop(ncluster);
1825 * cluster represents the hardlink target and is now flagged deleted.
1826 * duplicate it to the parent directory and adjust nlinks.
1828 * WARNING! The shiftup() call can cause ncluster to be moved into
1829 * an indirect block, and our ncluster will wind up pointing
1830 * to the older/original version.
1832 KKASSERT(cluster->focus->flags & HAMMER2_CHAIN_DELETED);
1833 hammer2_hardlink_shiftup(trans, cluster, cdip, cdcluster,
1837 hammer2_inode_repoint(ip, cdip, cluster);
1841 * Cleanup, cluster/ncluster already dealt with.
1843 * Return the shifted cluster in *clusterp.
1846 hammer2_cluster_unlock(cparent);
1847 hammer2_cluster_drop(cparent);
1849 *clusterp = cluster;
1855 * If (*ochainp) is non-NULL it points to the forward OBJTYPE_HARDLINK
1856 * inode while (*chainp) points to the resolved (hidden hardlink
1857 * target) inode. In this situation when nlinks is 1 we wish to
1858 * deconsolidate the hardlink, moving it back to the directory that now
1859 * represents the only remaining link.
1862 hammer2_hardlink_deconsolidate(hammer2_trans_t *trans,
1863 hammer2_inode_t *dip,
1864 hammer2_chain_t **chainp,
1865 hammer2_chain_t **ochainp)
1867 if (*ochainp == NULL)
1874 * The caller presents a locked cluster with an obj_type of
1875 * HAMMER2_OBJTYPE_HARDLINK in (*clusterp). This routine will locate
1876 * the inode and replace (*clusterp) with a new locked cluster containing
1877 * the target hardlink, also locked. The original cluster will be
1878 * unlocked and released.
1880 * If cparentp is not NULL a locked cluster representing the hardlink's
1881 * parent is also returned.
1883 * If we are unable to locate the hardlink target EIO is returned,
1884 * (*cparentp) is set to NULL, the original passed-in (*clusterp)
1885 * will be unlocked and released and (*clusterp) will be set to NULL
1889 hammer2_hardlink_find(hammer2_inode_t *dip,
1890 hammer2_cluster_t **cparentp,
1891 hammer2_cluster_t **clusterp)
1893 const hammer2_inode_data_t *ipdata;
1894 hammer2_cluster_t *cluster;
1895 hammer2_cluster_t *cparent;
1896 hammer2_cluster_t *rcluster;
1897 hammer2_inode_t *ip;
1898 hammer2_inode_t *pip;
1899 hammer2_key_t key_dummy;
1902 cluster = *clusterp;
1904 hammer2_inode_ref(pip); /* for loop */
1907 * Locate the hardlink. pip is referenced and not locked.
1908 * Unlock and release (*clusterp) after extracting the needed
1911 ipdata = &hammer2_cluster_rdata(cluster)->ipdata;
1913 ipdata = NULL; /* safety */
1914 hammer2_cluster_unlock(cluster);
1915 hammer2_cluster_drop(cluster);
1916 *clusterp = NULL; /* safety */
1921 while ((ip = pip) != NULL) {
1922 cparent = hammer2_inode_lock(ip, HAMMER2_RESOLVE_ALWAYS);
1923 hammer2_inode_drop(ip); /* loop */
1924 KKASSERT(hammer2_cluster_type(cparent) ==
1925 HAMMER2_BREF_TYPE_INODE);
1926 rcluster = hammer2_cluster_lookup(cparent, &key_dummy,
1930 hammer2_cluster_lookup_done(cparent); /* discard parent */
1931 cparent = NULL; /* safety */
1932 pip = ip->pip; /* safe, ip held locked */
1934 hammer2_inode_ref(pip); /* loop */
1935 hammer2_inode_unlock(ip, NULL);
1939 * chain is locked, ip is locked. Unlock ip, return the locked
1940 * chain. *ipp is already set w/a ref count and not locked.
1942 * (cparent is already unlocked).
1944 *clusterp = rcluster;
1947 *cparentp = cparent;
1948 hammer2_inode_unlock(ip, NULL);
1950 hammer2_inode_unlock(ip, cparent);
1957 hammer2_inode_unlock(ip, cparent);
1963 * Find the directory common to both fdip and tdip.
1965 * Returns a held but not locked inode. Caller typically locks the inode,
1966 * and when through unlocks AND drops it.
1969 hammer2_inode_common_parent(hammer2_inode_t *fdip, hammer2_inode_t *tdip)
1971 hammer2_inode_t *scan1;
1972 hammer2_inode_t *scan2;
1975 * We used to have a depth field but it complicated matters too
1976 * much for directory renames. So now its ugly. Check for
1977 * simple cases before giving up and doing it the expensive way.
1979 * XXX need a bottom-up topology stability lock
1981 if (fdip == tdip || fdip == tdip->pip) {
1982 hammer2_inode_ref(fdip);
1985 if (fdip->pip == tdip) {
1986 hammer2_inode_ref(tdip);
1993 for (scan1 = fdip; scan1->pmp == fdip->pmp; scan1 = scan1->pip) {
1995 while (scan2->pmp == tdip->pmp) {
1996 if (scan1 == scan2) {
1997 hammer2_inode_ref(scan1);
2005 panic("hammer2_inode_common_parent: no common parent %p %p\n",
2012 * Synchronize the inode's frontend state with the chain state prior
2013 * to any explicit flush of the inode or any strategy write call.
2015 * Called with a locked inode.
2018 hammer2_inode_fsync(hammer2_trans_t *trans, hammer2_inode_t *ip,
2019 hammer2_cluster_t *cparent)
2021 const hammer2_inode_data_t *ripdata;
2022 hammer2_inode_data_t *wipdata;
2023 hammer2_cluster_t *dparent;
2024 hammer2_cluster_t *cluster;
2025 hammer2_key_t lbase;
2026 hammer2_key_t key_next;
2029 ripdata = &hammer2_cluster_rdata(cparent)->ipdata; /* target file */
2031 if (ip->flags & HAMMER2_INODE_MTIME) {
2032 wipdata = hammer2_cluster_modify_ip(trans, ip, cparent, 0);
2033 atomic_clear_int(&ip->flags, HAMMER2_INODE_MTIME);
2034 wipdata->mtime = ip->mtime;
2038 if ((ip->flags & HAMMER2_INODE_RESIZED) && ip->size < ripdata->size) {
2039 wipdata = hammer2_cluster_modify_ip(trans, ip, cparent, 0);
2040 wipdata->size = ip->size;
2043 atomic_clear_int(&ip->flags, HAMMER2_INODE_RESIZED);
2046 * We must delete any chains beyond the EOF. The chain
2047 * straddling the EOF will be pending in the bioq.
2049 lbase = (ripdata->size + HAMMER2_PBUFMASK64) &
2050 ~HAMMER2_PBUFMASK64;
2051 dparent = hammer2_cluster_lookup_init(&ip->cluster, 0);
2052 cluster = hammer2_cluster_lookup(dparent, &key_next,
2053 lbase, (hammer2_key_t)-1,
2054 HAMMER2_LOOKUP_NODATA);
2057 * Degenerate embedded case, nothing to loop on
2059 switch (hammer2_cluster_type(cluster)) {
2060 case HAMMER2_BREF_TYPE_INODE:
2061 hammer2_cluster_unlock(cluster);
2062 hammer2_cluster_drop(cluster);
2065 case HAMMER2_BREF_TYPE_DATA:
2066 hammer2_cluster_delete(trans, dparent, cluster,
2067 HAMMER2_DELETE_PERMANENT);
2070 cluster = hammer2_cluster_next(dparent, cluster,
2072 key_next, (hammer2_key_t)-1,
2073 HAMMER2_LOOKUP_NODATA);
2077 hammer2_cluster_lookup_done(dparent);
2079 if ((ip->flags & HAMMER2_INODE_RESIZED) && ip->size > ripdata->size) {
2080 wipdata = hammer2_cluster_modify_ip(trans, ip, cparent, 0);
2081 wipdata->size = ip->size;
2082 atomic_clear_int(&ip->flags, HAMMER2_INODE_RESIZED);
2085 * When resizing larger we may not have any direct-data
2088 if ((wipdata->op_flags & HAMMER2_OPFLAG_DIRECTDATA) &&
2089 ip->size > HAMMER2_EMBEDDED_BYTES) {
2090 wipdata->op_flags &= ~HAMMER2_OPFLAG_DIRECTDATA;
2091 bzero(&wipdata->u.blockset,
2092 sizeof(wipdata->u.blockset));
2098 hammer2_cluster_modsync(cparent);