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 hammer2_cluster_clr_chainflags(ncluster
,
1032 HAMMER2_CHAIN_UNLINKED
);
1033 KKASSERT(name_len
< HAMMER2_INODE_MAXNAME
);
1034 wipdata
= &hammer2_cluster_wdata(ncluster
)->ipdata
;
1035 bcopy(name
, wipdata
->filename
, name_len
);
1036 wipdata
->name_key
= lhc
;
1037 wipdata
->name_len
= name_len
;
1038 wipdata
->target_type
=
1039 hammer2_cluster_rdata(ocluster
)->ipdata
.type
;
1040 wipdata
->type
= HAMMER2_OBJTYPE_HARDLINK
;
1041 wipdata
->inum
= hammer2_cluster_rdata(ocluster
)->ipdata
.inum
;
1042 wipdata
->version
= HAMMER2_INODE_VERSION_ONE
;
1043 wipdata
->nlinks
= 1;
1044 wipdata
->op_flags
= HAMMER2_OPFLAG_DIRECTDATA
;
1045 hammer2_cluster_modsync(ncluster
);
1046 hammer2_cluster_unlock(ncluster
);
1047 hammer2_cluster_drop(ncluster
);
1048 ncluster
= ocluster
;
1052 * ncluster is a duplicate of ocluster at the new location.
1053 * We must fixup the name stored in the inode data.
1054 * The bref key has already been adjusted by inode_connect().
1056 hammer2_cluster_modify(trans
, ncluster
, 0);
1057 hammer2_cluster_clr_chainflags(ncluster
,
1058 HAMMER2_CHAIN_UNLINKED
);
1059 wipdata
= &hammer2_cluster_wdata(ncluster
)->ipdata
;
1061 KKASSERT(name_len
< HAMMER2_INODE_MAXNAME
);
1062 bcopy(name
, wipdata
->filename
, name_len
);
1063 wipdata
->name_key
= lhc
;
1064 wipdata
->name_len
= name_len
;
1065 wipdata
->nlinks
= 1;
1066 hammer2_cluster_modsync(ncluster
);
1070 * We are replacing ocluster with ncluster, unlock ocluster. In the
1071 * case where ocluster is left unchanged the code above sets
1072 * ncluster to ocluster and ocluster to NULL, resulting in a NOP here.
1075 hammer2_cluster_unlock(ocluster
);
1076 hammer2_cluster_drop(ocluster
);
1078 *clusterp
= ncluster
;
1084 * Repoint ip->cluster's chains to cluster's chains and fixup the default
1085 * focus. Only valid elements are repointed. Invalid elements have to be
1086 * adjusted by the appropriate slave sync threads.
1088 * Caller must hold the inode and cluster exclusive locked, if not NULL,
1089 * must also be locked.
1091 * Cluster may be NULL to clean out any chains in ip->cluster.
1094 hammer2_inode_repoint(hammer2_inode_t
*ip
, hammer2_inode_t
*pip
,
1095 hammer2_cluster_t
*cluster
)
1097 hammer2_chain_t
*dropch
[HAMMER2_MAXCLUSTER
];
1098 hammer2_chain_t
*ochain
;
1099 hammer2_chain_t
*nchain
;
1100 hammer2_inode_t
*opip
;
1103 bzero(dropch
, sizeof(dropch
));
1106 * Replace chains in ip->cluster with chains from cluster and
1107 * adjust the focus if necessary.
1109 * NOTE: nchain and/or ochain can be NULL due to gaps
1110 * in the cluster arrays.
1112 hammer2_spin_ex(&ip
->cluster_spin
);
1113 for (i
= 0; cluster
&& i
< cluster
->nchains
; ++i
) {
1115 * Do not replace invalid elements as this might race
1116 * syncthr replacements.
1118 if (cluster
->array
[i
].flags
& HAMMER2_CITEM_INVALID
)
1122 * Do not replace elements which are the same. Also handle
1123 * element count discrepancies.
1125 nchain
= cluster
->array
[i
].chain
;
1126 if (i
< ip
->cluster
.nchains
) {
1127 ochain
= ip
->cluster
.array
[i
].chain
;
1128 if (ochain
== nchain
)
1137 ip
->cluster
.array
[i
].chain
= nchain
;
1138 ip
->cluster
.array
[i
].flags
&= ~HAMMER2_CITEM_INVALID
;
1139 ip
->cluster
.array
[i
].flags
|= cluster
->array
[i
].flags
&
1140 HAMMER2_CITEM_INVALID
;
1142 hammer2_chain_ref(nchain
);
1147 * Release any left-over chains in ip->cluster.
1149 while (i
< ip
->cluster
.nchains
) {
1150 nchain
= ip
->cluster
.array
[i
].chain
;
1152 ip
->cluster
.array
[i
].chain
= NULL
;
1153 ip
->cluster
.array
[i
].flags
|= HAMMER2_CITEM_INVALID
;
1160 * Fixup fields. Note that the inode-embedded cluster is never
1164 ip
->cluster
.nchains
= cluster
->nchains
;
1165 ip
->cluster
.focus
= cluster
->focus
;
1166 ip
->cluster
.flags
= cluster
->flags
& ~HAMMER2_CLUSTER_LOCKED
;
1168 ip
->cluster
.nchains
= 0;
1169 ip
->cluster
.focus
= NULL
;
1170 ip
->cluster
.flags
&= ~HAMMER2_CLUSTER_ZFLAGS
;
1174 * Repoint ip->pip if requested (non-NULL pip).
1176 if (pip
&& ip
->pip
!= pip
) {
1178 hammer2_inode_ref(pip
);
1183 hammer2_spin_unex(&ip
->cluster_spin
);
1186 * Cleanup outside of spinlock
1190 hammer2_chain_drop(dropch
[i
]);
1193 hammer2_inode_drop(opip
);
1197 * Repoint a single element from the cluster to the ip. Used by the
1198 * synchronization threads to piecemeal update inodes. Does not change
1199 * focus and requires inode to be re-locked to clean-up flags (XXX).
1202 hammer2_inode_repoint_one(hammer2_inode_t
*ip
, hammer2_cluster_t
*cluster
,
1205 hammer2_chain_t
*ochain
;
1206 hammer2_chain_t
*nchain
;
1209 hammer2_spin_ex(&ip
->cluster_spin
);
1210 KKASSERT(idx
< cluster
->nchains
);
1211 if (idx
< ip
->cluster
.nchains
) {
1212 ochain
= ip
->cluster
.array
[idx
].chain
;
1213 nchain
= cluster
->array
[idx
].chain
;
1216 nchain
= cluster
->array
[idx
].chain
;
1217 ip
->cluster
.nchains
= idx
+ 1;
1218 for (i
= ip
->cluster
.nchains
; i
<= idx
; ++i
) {
1219 bzero(&ip
->cluster
.array
[i
],
1220 sizeof(ip
->cluster
.array
[i
]));
1221 ip
->cluster
.array
[i
].flags
|= HAMMER2_CITEM_INVALID
;
1224 if (ochain
!= nchain
) {
1228 ip
->cluster
.array
[idx
].chain
= nchain
;
1229 ip
->cluster
.array
[idx
].flags
&= ~HAMMER2_CITEM_INVALID
;
1230 ip
->cluster
.array
[idx
].flags
|= cluster
->array
[idx
].flags
&
1231 HAMMER2_CITEM_INVALID
;
1233 hammer2_spin_unex(&ip
->cluster_spin
);
1234 if (ochain
!= nchain
) {
1236 hammer2_chain_ref(nchain
);
1238 hammer2_chain_drop(ochain
);
1243 * Unlink the file from the specified directory inode. The directory inode
1244 * does not need to be locked.
1246 * isdir determines whether a directory/non-directory check should be made.
1247 * No check is made if isdir is set to -1.
1249 * isopen specifies whether special unlink-with-open-descriptor handling
1250 * must be performed. If set to -1 the caller is deleting a PFS and we
1251 * check whether the chain is mounted or not (chain->pmp != NULL). 1 is
1252 * implied if it is mounted.
1254 * If isopen is 1 and nlinks drops to 0 this function must move the chain
1255 * to a special hidden directory until last-close occurs on the file.
1257 * NOTE! The underlying file can still be active with open descriptors
1258 * or if the chain is being manually held (e.g. for rename).
1260 * The caller is responsible for fixing up ip->chain if e.g. a
1261 * rename occurs (see chain_duplicate()).
1263 * NOTE! The chain is not deleted if it is moved to the hidden directory,
1264 * but otherwise will be deleted.
1267 hammer2_unlink_file(hammer2_trans_t
*trans
, hammer2_inode_t
*dip
,
1268 const uint8_t *name
, size_t name_len
,
1269 int isdir
, int *hlinkp
, struct nchandle
*nch
,
1272 const hammer2_inode_data_t
*ripdata
;
1273 hammer2_inode_data_t
*wipdata
;
1274 hammer2_cluster_t
*cparent
;
1275 hammer2_cluster_t
*hcluster
;
1276 hammer2_cluster_t
*hparent
;
1277 hammer2_cluster_t
*cluster
;
1278 hammer2_cluster_t
*dparent
;
1279 hammer2_cluster_t
*dcluster
;
1280 hammer2_key_t key_dummy
;
1281 hammer2_key_t key_next
;
1292 lhc
= hammer2_dirhash(name
, name_len
);
1296 * Search for the filename in the directory
1298 cparent
= hammer2_inode_lock(dip
, HAMMER2_RESOLVE_ALWAYS
);
1299 cluster
= hammer2_cluster_lookup(cparent
, &key_next
,
1300 lhc
, lhc
+ HAMMER2_DIRHASH_LOMASK
, 0);
1302 if (hammer2_cluster_type(cluster
) == HAMMER2_BREF_TYPE_INODE
) {
1303 ripdata
= &hammer2_cluster_rdata(cluster
)->ipdata
;
1304 if (ripdata
->name_len
== name_len
&&
1305 bcmp(ripdata
->filename
, name
, name_len
) == 0) {
1309 cluster
= hammer2_cluster_next(cparent
, cluster
, &key_next
,
1311 lhc
+ HAMMER2_DIRHASH_LOMASK
,
1314 hammer2_inode_unlock(dip
, NULL
); /* retain cparent */
1317 * Not found or wrong type (isdir < 0 disables the type check).
1318 * If a hardlink pointer, type checks use the hardlink target.
1320 if (cluster
== NULL
) {
1324 ripdata
= &hammer2_cluster_rdata(cluster
)->ipdata
;
1325 type
= ripdata
->type
;
1326 if (type
== HAMMER2_OBJTYPE_HARDLINK
) {
1328 type
= ripdata
->target_type
;
1331 if (type
== HAMMER2_OBJTYPE_DIRECTORY
&& isdir
== 0) {
1335 if (type
!= HAMMER2_OBJTYPE_DIRECTORY
&& isdir
>= 1) {
1341 * Hardlink must be resolved. We can't hold the parent locked
1342 * while we do this or we could deadlock. The physical file will
1343 * be located at or above the current directory.
1345 * We loop to reacquire the hardlink origination.
1347 * NOTE: hammer2_hardlink_find() will locate the hardlink target,
1348 * returning a modified hparent and hcluster.
1350 if (ripdata
->type
== HAMMER2_OBJTYPE_HARDLINK
) {
1351 if (hcluster
== NULL
) {
1353 cluster
= NULL
; /* safety */
1354 hammer2_cluster_unlock(cparent
);
1355 hammer2_cluster_drop(cparent
);
1356 cparent
= NULL
; /* safety */
1357 ripdata
= NULL
; /* safety (associated w/cparent) */
1358 error
= hammer2_hardlink_find(dip
, &hparent
, &hcluster
);
1361 * If we couldn't find the hardlink target then some
1362 * parent directory containing the hardlink pointer
1363 * probably got renamed to above the original target,
1364 * a case not yet handled by H2.
1367 kprintf("H2 unlink_file: hardlink target for "
1368 "\"%s\" not found\n",
1370 kprintf("(likely due to known directory "
1379 * If this is a directory the directory must be empty. However, if
1380 * isdir < 0 we are doing a rename and the directory does not have
1381 * to be empty, and if isdir > 1 we are deleting a PFS/snapshot
1382 * and the directory does not have to be empty.
1384 * NOTE: We check the full key range here which covers both visible
1385 * and invisible entries. Theoretically there should be no
1386 * invisible (hardlink target) entries if there are no visible
1389 if (type
== HAMMER2_OBJTYPE_DIRECTORY
&& isdir
== 1) {
1390 dparent
= hammer2_cluster_lookup_init(cluster
, 0);
1391 dcluster
= hammer2_cluster_lookup(dparent
, &key_dummy
,
1392 0, (hammer2_key_t
)-1,
1393 HAMMER2_LOOKUP_NODATA
);
1395 hammer2_cluster_unlock(dcluster
);
1396 hammer2_cluster_drop(dcluster
);
1397 hammer2_cluster_lookup_done(dparent
);
1401 hammer2_cluster_lookup_done(dparent
);
1407 * If this was a hardlink then (cparent, cluster) is the hardlink
1408 * pointer, which we can simply destroy outright. Discard the
1409 * clusters and replace with the hardlink target.
1412 hammer2_cluster_delete(trans
, cparent
, cluster
,
1413 HAMMER2_DELETE_PERMANENT
);
1414 hammer2_cluster_unlock(cparent
);
1415 hammer2_cluster_drop(cparent
);
1416 hammer2_cluster_unlock(cluster
);
1417 hammer2_cluster_drop(cluster
);
1425 * This leaves us with the hardlink target or non-hardlinked file
1426 * or directory in (cparent, cluster).
1428 * Delete the target when nlinks reaches 0 with special handling
1429 * to avoid I/O (to avoid actually updating the inode) for the 1->0
1430 * transition, if possible. This optimization makes rm -rf very
1433 * NOTE! In DragonFly the vnops function calls cache_unlink() after
1434 * calling us here to clean out the namecache association,
1435 * (which does not represent a ref for the open-test), and to
1436 * force finalization of the vnode if/when the last ref gets
1439 * NOTE! Files are unlinked by rename and then relinked. nch will be
1440 * passed as NULL in this situation. hammer2_inode_connect()
1443 KKASSERT(cluster
!= NULL
);
1446 * Note: nlinks is negative when decrementing, positive when
1449 ripdata
= &hammer2_cluster_rdata(cluster
)->ipdata
;
1450 last_link
= (ripdata
->nlinks
+ nlinks
== 0);
1454 * Target nlinks has reached 0, file now unlinked (but may
1457 /* XXX need interlock if mounted
1458 if ((cluster->focus->flags & HAMMER2_CHAIN_PFSROOT) &&
1461 kprintf("hammer2: PFS \"%s\" cannot be deleted "
1462 "while still mounted\n",
1467 hammer2_cluster_set_chainflags(cluster
, HAMMER2_CHAIN_UNLINKED
);
1468 if (nch
&& cache_isopen(nch
)) {
1470 * If an unlinked file is still open we must update
1471 * the inodes link count.
1473 hammer2_cluster_modify(trans
, cluster
, 0);
1474 wipdata
= &hammer2_cluster_wdata(cluster
)->ipdata
;
1476 wipdata
->nlinks
+= nlinks
;
1478 if ((int64_t)wipdata
->nlinks
< 0) {
1479 wipdata
->nlinks
= 0;
1481 hammer2_cluster_modsync(cluster
);
1482 hammer2_inode_move_to_hidden(trans
, &cparent
, &cluster
,
1486 * This won't get everything if a vnode is still
1487 * present, but the cache_unlink() call the caller
1490 hammer2_cluster_delete(trans
, cparent
, cluster
,
1491 HAMMER2_DELETE_PERMANENT
);
1493 } else if (hlink
== 0) {
1495 * In this situation a normal non-hardlinked file (which can
1496 * only have nlinks == 1) still has a non-zero nlinks, the
1497 * caller must be doing a RENAME operation and so is passing
1498 * a nlinks adjustment of 0, and only wishes to remove file
1499 * in order to be able to reconnect it under a different name.
1501 * In this situation we do a temporary deletion of the
1502 * chain in order to allow the file to be reconnected in
1503 * a different location.
1505 KKASSERT(nlinks
== 0);
1506 hammer2_cluster_delete(trans
, cparent
, cluster
, 0);
1509 * Links remain, must update the inode link count.
1511 hammer2_cluster_modify(trans
, cluster
, 0);
1512 wipdata
= &hammer2_cluster_wdata(cluster
)->ipdata
;
1514 wipdata
->nlinks
+= nlinks
;
1515 if ((int64_t)wipdata
->nlinks
< 0) { /* XXX debugging */
1516 wipdata
->nlinks
= 0;
1518 hammer2_cluster_modsync(cluster
);
1524 hammer2_cluster_unlock(cparent
);
1525 hammer2_cluster_drop(cparent
);
1528 hammer2_cluster_unlock(cluster
);
1529 hammer2_cluster_drop(cluster
);
1532 hammer2_cluster_unlock(hparent
);
1533 hammer2_cluster_drop(hparent
);
1536 hammer2_cluster_unlock(hcluster
);
1537 hammer2_cluster_drop(hcluster
);
1546 * This is called from the mount code to initialize pmp->ihidden
1549 hammer2_inode_install_hidden(hammer2_pfs_t
*pmp
)
1551 hammer2_trans_t trans
;
1552 hammer2_cluster_t
*cparent
;
1553 hammer2_cluster_t
*cluster
;
1554 hammer2_cluster_t
*scan
;
1555 const hammer2_inode_data_t
*ripdata
;
1556 hammer2_inode_data_t
*wipdata
;
1557 hammer2_key_t key_dummy
;
1558 hammer2_key_t key_next
;
1568 * Find the hidden directory
1570 bzero(&key_dummy
, sizeof(key_dummy
));
1571 hammer2_trans_init(&trans
, pmp
, 0);
1574 * Setup for lookup, retrieve iroot's check and compression
1575 * algorithm request which was likely generated by newfs_hammer2.
1577 * The check/comp fields will probably never be used since inodes
1578 * are renamed into the hidden directory and not created relative to
1579 * the hidden directory, chain creation inherits from bref.methods,
1580 * and data chains inherit from their respective file inode *_algo
1583 cparent
= hammer2_inode_lock(pmp
->iroot
, HAMMER2_RESOLVE_ALWAYS
);
1584 ripdata
= &hammer2_cluster_rdata(cparent
)->ipdata
;
1585 dip_check_algo
= ripdata
->check_algo
;
1586 dip_comp_algo
= ripdata
->comp_algo
;
1589 cluster
= hammer2_cluster_lookup(cparent
, &key_dummy
,
1590 HAMMER2_INODE_HIDDENDIR
,
1591 HAMMER2_INODE_HIDDENDIR
,
1594 pmp
->ihidden
= hammer2_inode_get(pmp
, pmp
->iroot
, cluster
);
1595 hammer2_inode_ref(pmp
->ihidden
);
1598 * Remove any unlinked files which were left open as-of
1601 * Don't pass NODATA, we need the inode data so the delete
1602 * can do proper statistics updates.
1605 scan
= hammer2_cluster_lookup(cluster
, &key_next
,
1606 0, HAMMER2_TID_MAX
, 0);
1608 if (hammer2_cluster_type(scan
) ==
1609 HAMMER2_BREF_TYPE_INODE
) {
1610 hammer2_cluster_delete(&trans
, cluster
, scan
,
1611 HAMMER2_DELETE_PERMANENT
);
1614 scan
= hammer2_cluster_next(cluster
, scan
, &key_next
,
1615 0, HAMMER2_TID_MAX
, 0);
1618 hammer2_inode_unlock(pmp
->ihidden
, cluster
);
1619 hammer2_inode_unlock(pmp
->iroot
, cparent
);
1620 hammer2_trans_done(&trans
);
1621 kprintf("hammer2: PFS loaded hidden dir, "
1622 "removed %d dead entries\n", count
);
1627 * Create the hidden directory
1629 error
= hammer2_cluster_create(&trans
, cparent
, &cluster
,
1630 HAMMER2_INODE_HIDDENDIR
, 0,
1631 HAMMER2_BREF_TYPE_INODE
,
1632 HAMMER2_INODE_BYTES
,
1634 hammer2_inode_unlock(pmp
->iroot
, cparent
);
1636 hammer2_cluster_modify(&trans
, cluster
, 0);
1637 wipdata
= &hammer2_cluster_wdata(cluster
)->ipdata
;
1638 wipdata
->type
= HAMMER2_OBJTYPE_DIRECTORY
;
1639 wipdata
->inum
= HAMMER2_INODE_HIDDENDIR
;
1640 wipdata
->nlinks
= 1;
1641 wipdata
->comp_algo
= dip_comp_algo
;
1642 wipdata
->check_algo
= dip_check_algo
;
1643 hammer2_cluster_modsync(cluster
);
1644 kprintf("hammer2: PFS root missing hidden directory, creating\n");
1646 pmp
->ihidden
= hammer2_inode_get(pmp
, pmp
->iroot
, cluster
);
1647 hammer2_inode_ref(pmp
->ihidden
);
1648 hammer2_inode_unlock(pmp
->ihidden
, cluster
);
1649 hammer2_trans_done(&trans
);
1653 * If an open file is unlinked H2 needs to retain the file in the topology
1654 * to ensure that its backing store is not recovered by the bulk free scan.
1655 * This also allows us to avoid having to special-case the CHAIN_DELETED flag.
1657 * To do this the file is moved to a hidden directory in the PFS root and
1658 * renamed. The hidden directory must be created if it does not exist.
1662 hammer2_inode_move_to_hidden(hammer2_trans_t
*trans
,
1663 hammer2_cluster_t
**cparentp
,
1664 hammer2_cluster_t
**clusterp
,
1667 hammer2_cluster_t
*dcluster
;
1671 pmp
= (*clusterp
)->pmp
;
1672 KKASSERT(pmp
!= NULL
);
1673 KKASSERT(pmp
->ihidden
!= NULL
);
1675 hammer2_cluster_delete(trans
, *cparentp
, *clusterp
, 0);
1676 dcluster
= hammer2_inode_lock(pmp
->ihidden
, HAMMER2_RESOLVE_ALWAYS
);
1677 error
= hammer2_inode_connect(trans
, clusterp
, 0,
1678 pmp
->ihidden
, dcluster
,
1680 hammer2_inode_unlock(pmp
->ihidden
, dcluster
);
1681 KKASSERT(error
== 0);
1685 * Given an exclusively locked inode and cluster we consolidate the cluster
1686 * for hardlink creation, adding (nlinks) to the file's link count and
1687 * potentially relocating the inode to (cdip) which is a parent directory
1688 * common to both the current location of the inode and the intended new
1691 * Replaces (*clusterp) if consolidation occurred, unlocking the old cluster
1692 * and returning a new locked cluster.
1694 * NOTE! This function will also replace ip->cluster.
1697 hammer2_hardlink_consolidate(hammer2_trans_t
*trans
,
1698 hammer2_inode_t
*ip
,
1699 hammer2_cluster_t
**clusterp
,
1700 hammer2_inode_t
*cdip
,
1701 hammer2_cluster_t
*cdcluster
,
1704 const hammer2_inode_data_t
*ripdata
;
1705 hammer2_inode_data_t
*wipdata
;
1706 hammer2_cluster_t
*cluster
;
1707 hammer2_cluster_t
*cparent
;
1710 cluster
= *clusterp
;
1711 ripdata
= &hammer2_cluster_rdata(cluster
)->ipdata
;
1712 if (nlinks
== 0 && /* no hardlink needed */
1713 (ripdata
->name_key
& HAMMER2_DIRHASH_VISIBLE
)) {
1717 if (hammer2_hardlink_enable
== 0) { /* disallow hardlinks */
1718 hammer2_cluster_unlock(cluster
);
1719 hammer2_cluster_drop(cluster
);
1727 * If no change in the hardlink's target directory is required and
1728 * this is already a hardlink target, all we need to do is adjust
1731 ripdata
= &hammer2_cluster_rdata(cluster
)->ipdata
;
1732 if (cdip
== ip
->pip
&&
1733 (ripdata
->name_key
& HAMMER2_DIRHASH_VISIBLE
) == 0) {
1735 hammer2_cluster_modify(trans
, cluster
, 0);
1736 wipdata
= &hammer2_cluster_wdata(cluster
)->ipdata
;
1737 wipdata
->nlinks
+= nlinks
;
1738 hammer2_cluster_modsync(cluster
);
1746 * Cluster is the real inode. The originating directory is locked
1747 * by the caller so we can manipulate it without worrying about races
1748 * against other lookups.
1750 * If cluster is visible we need to delete it from the current
1751 * location and create a hardlink pointer in its place. If it is
1752 * not visible we need only delete it. Then later cluster will be
1753 * renamed to a parent directory and converted (if necessary) to
1754 * a hidden inode (via shiftup).
1756 * NOTE! We must hold cparent locked through the delete/create/rename
1757 * operation to ensure that other threads block resolving to
1758 * the same hardlink, otherwise the other threads may not see
1761 KKASSERT((cluster
->focus
->flags
& HAMMER2_CHAIN_DELETED
) == 0);
1762 cparent
= hammer2_cluster_parent(cluster
);
1764 hammer2_cluster_delete(trans
, cparent
, cluster
, 0);
1766 ripdata
= &hammer2_cluster_rdata(cluster
)->ipdata
;
1767 KKASSERT(ripdata
->type
!= HAMMER2_OBJTYPE_HARDLINK
);
1768 if (ripdata
->name_key
& HAMMER2_DIRHASH_VISIBLE
) {
1769 hammer2_cluster_t
*ncluster
;
1773 lhc
= cluster
->focus
->bref
.key
;
1774 error
= hammer2_cluster_create(trans
, cparent
, &ncluster
,
1776 HAMMER2_BREF_TYPE_INODE
,
1777 HAMMER2_INODE_BYTES
,
1779 hammer2_cluster_modify(trans
, ncluster
, 0);
1780 wipdata
= &hammer2_cluster_wdata(ncluster
)->ipdata
;
1782 /* wipdata->comp_algo = ripdata->comp_algo; */
1783 wipdata
->comp_algo
= 0;
1784 wipdata
->check_algo
= 0;
1785 wipdata
->version
= HAMMER2_INODE_VERSION_ONE
;
1786 wipdata
->inum
= ripdata
->inum
;
1787 wipdata
->target_type
= ripdata
->type
;
1788 wipdata
->type
= HAMMER2_OBJTYPE_HARDLINK
;
1789 wipdata
->uflags
= 0;
1790 wipdata
->rmajor
= 0;
1791 wipdata
->rminor
= 0;
1796 bzero(&wipdata
->uid
, sizeof(wipdata
->uid
));
1797 bzero(&wipdata
->gid
, sizeof(wipdata
->gid
));
1798 wipdata
->op_flags
= HAMMER2_OPFLAG_DIRECTDATA
;
1799 wipdata
->cap_flags
= 0;
1802 wipdata
->nlinks
= 1;
1803 wipdata
->iparent
= 0; /* XXX */
1804 wipdata
->pfs_type
= 0;
1805 wipdata
->pfs_inum
= 0;
1806 bzero(&wipdata
->pfs_clid
, sizeof(wipdata
->pfs_clid
));
1807 bzero(&wipdata
->pfs_fsid
, sizeof(wipdata
->pfs_fsid
));
1808 wipdata
->data_quota
= 0;
1809 /* wipdata->data_count = 0; */
1810 wipdata
->inode_quota
= 0;
1811 /* wipdata->inode_count = 0; */
1812 wipdata
->attr_tid
= 0;
1813 wipdata
->dirent_tid
= 0;
1814 bzero(&wipdata
->u
, sizeof(wipdata
->u
));
1815 bcopy(ripdata
->filename
, wipdata
->filename
, ripdata
->name_len
);
1816 wipdata
->name_key
= ncluster
->focus
->bref
.key
;
1817 wipdata
->name_len
= ripdata
->name_len
;
1818 /* XXX transaction ids */
1819 hammer2_cluster_modsync(ncluster
);
1820 hammer2_cluster_unlock(ncluster
);
1821 hammer2_cluster_drop(ncluster
);
1826 * cluster represents the hardlink target and is now flagged deleted.
1827 * duplicate it to the parent directory and adjust nlinks.
1829 * WARNING! The shiftup() call can cause ncluster to be moved into
1830 * an indirect block, and our ncluster will wind up pointing
1831 * to the older/original version.
1833 KKASSERT(cluster
->focus
->flags
& HAMMER2_CHAIN_DELETED
);
1834 hammer2_hardlink_shiftup(trans
, cluster
, cdip
, cdcluster
,
1838 hammer2_inode_repoint(ip
, cdip
, cluster
);
1842 * Cleanup, cluster/ncluster already dealt with.
1844 * Return the shifted cluster in *clusterp.
1847 hammer2_cluster_unlock(cparent
);
1848 hammer2_cluster_drop(cparent
);
1850 *clusterp
= cluster
;
1856 * If (*ochainp) is non-NULL it points to the forward OBJTYPE_HARDLINK
1857 * inode while (*chainp) points to the resolved (hidden hardlink
1858 * target) inode. In this situation when nlinks is 1 we wish to
1859 * deconsolidate the hardlink, moving it back to the directory that now
1860 * represents the only remaining link.
1863 hammer2_hardlink_deconsolidate(hammer2_trans_t
*trans
,
1864 hammer2_inode_t
*dip
,
1865 hammer2_chain_t
**chainp
,
1866 hammer2_chain_t
**ochainp
)
1868 if (*ochainp
== NULL
)
1875 * The caller presents a locked cluster with an obj_type of
1876 * HAMMER2_OBJTYPE_HARDLINK in (*clusterp). This routine will locate
1877 * the inode and replace (*clusterp) with a new locked cluster containing
1878 * the target hardlink, also locked. The original cluster will be
1879 * unlocked and released.
1881 * If cparentp is not NULL a locked cluster representing the hardlink's
1882 * parent is also returned.
1884 * If we are unable to locate the hardlink target EIO is returned,
1885 * (*cparentp) is set to NULL, the original passed-in (*clusterp)
1886 * will be unlocked and released and (*clusterp) will be set to NULL
1890 hammer2_hardlink_find(hammer2_inode_t
*dip
,
1891 hammer2_cluster_t
**cparentp
,
1892 hammer2_cluster_t
**clusterp
)
1894 const hammer2_inode_data_t
*ipdata
;
1895 hammer2_cluster_t
*cluster
;
1896 hammer2_cluster_t
*cparent
;
1897 hammer2_cluster_t
*rcluster
;
1898 hammer2_inode_t
*ip
;
1899 hammer2_inode_t
*pip
;
1900 hammer2_key_t key_dummy
;
1903 cluster
= *clusterp
;
1905 hammer2_inode_ref(pip
); /* for loop */
1908 * Locate the hardlink. pip is referenced and not locked.
1909 * Unlock and release (*clusterp) after extracting the needed
1912 ipdata
= &hammer2_cluster_rdata(cluster
)->ipdata
;
1914 ipdata
= NULL
; /* safety */
1915 hammer2_cluster_unlock(cluster
);
1916 hammer2_cluster_drop(cluster
);
1917 *clusterp
= NULL
; /* safety */
1922 while ((ip
= pip
) != NULL
) {
1923 cparent
= hammer2_inode_lock(ip
, HAMMER2_RESOLVE_ALWAYS
);
1924 hammer2_inode_drop(ip
); /* loop */
1925 KKASSERT(hammer2_cluster_type(cparent
) ==
1926 HAMMER2_BREF_TYPE_INODE
);
1927 rcluster
= hammer2_cluster_lookup(cparent
, &key_dummy
,
1931 hammer2_cluster_lookup_done(cparent
); /* discard parent */
1932 cparent
= NULL
; /* safety */
1933 pip
= ip
->pip
; /* safe, ip held locked */
1935 hammer2_inode_ref(pip
); /* loop */
1936 hammer2_inode_unlock(ip
, NULL
);
1940 * chain is locked, ip is locked. Unlock ip, return the locked
1941 * chain. *ipp is already set w/a ref count and not locked.
1943 * (cparent is already unlocked).
1945 *clusterp
= rcluster
;
1948 *cparentp
= cparent
;
1949 hammer2_inode_unlock(ip
, NULL
);
1951 hammer2_inode_unlock(ip
, cparent
);
1958 hammer2_inode_unlock(ip
, cparent
);
1964 * Find the directory common to both fdip and tdip.
1966 * Returns a held but not locked inode. Caller typically locks the inode,
1967 * and when through unlocks AND drops it.
1970 hammer2_inode_common_parent(hammer2_inode_t
*fdip
, hammer2_inode_t
*tdip
)
1972 hammer2_inode_t
*scan1
;
1973 hammer2_inode_t
*scan2
;
1976 * We used to have a depth field but it complicated matters too
1977 * much for directory renames. So now its ugly. Check for
1978 * simple cases before giving up and doing it the expensive way.
1980 * XXX need a bottom-up topology stability lock
1982 if (fdip
== tdip
|| fdip
== tdip
->pip
) {
1983 hammer2_inode_ref(fdip
);
1986 if (fdip
->pip
== tdip
) {
1987 hammer2_inode_ref(tdip
);
1994 for (scan1
= fdip
; scan1
->pmp
== fdip
->pmp
; scan1
= scan1
->pip
) {
1996 while (scan2
->pmp
== tdip
->pmp
) {
1997 if (scan1
== scan2
) {
1998 hammer2_inode_ref(scan1
);
2006 panic("hammer2_inode_common_parent: no common parent %p %p\n",
2013 * Synchronize the inode's frontend state with the chain state prior
2014 * to any explicit flush of the inode or any strategy write call.
2016 * Called with a locked inode.
2019 hammer2_inode_fsync(hammer2_trans_t
*trans
, hammer2_inode_t
*ip
,
2020 hammer2_cluster_t
*cparent
)
2022 const hammer2_inode_data_t
*ripdata
;
2023 hammer2_inode_data_t
*wipdata
;
2024 hammer2_cluster_t
*dparent
;
2025 hammer2_cluster_t
*cluster
;
2026 hammer2_key_t lbase
;
2027 hammer2_key_t key_next
;
2030 ripdata
= &hammer2_cluster_rdata(cparent
)->ipdata
; /* target file */
2032 if (ip
->flags
& HAMMER2_INODE_MTIME
) {
2033 wipdata
= hammer2_cluster_modify_ip(trans
, ip
, cparent
, 0);
2034 atomic_clear_int(&ip
->flags
, HAMMER2_INODE_MTIME
);
2035 wipdata
->mtime
= ip
->mtime
;
2039 if ((ip
->flags
& HAMMER2_INODE_RESIZED
) && ip
->size
< ripdata
->size
) {
2040 wipdata
= hammer2_cluster_modify_ip(trans
, ip
, cparent
, 0);
2041 wipdata
->size
= ip
->size
;
2044 atomic_clear_int(&ip
->flags
, HAMMER2_INODE_RESIZED
);
2047 * We must delete any chains beyond the EOF. The chain
2048 * straddling the EOF will be pending in the bioq.
2050 lbase
= (ripdata
->size
+ HAMMER2_PBUFMASK64
) &
2051 ~HAMMER2_PBUFMASK64
;
2052 dparent
= hammer2_cluster_lookup_init(&ip
->cluster
, 0);
2053 cluster
= hammer2_cluster_lookup(dparent
, &key_next
,
2054 lbase
, (hammer2_key_t
)-1,
2055 HAMMER2_LOOKUP_NODATA
);
2058 * Degenerate embedded case, nothing to loop on
2060 switch (hammer2_cluster_type(cluster
)) {
2061 case HAMMER2_BREF_TYPE_INODE
:
2062 hammer2_cluster_unlock(cluster
);
2063 hammer2_cluster_drop(cluster
);
2066 case HAMMER2_BREF_TYPE_DATA
:
2067 hammer2_cluster_delete(trans
, dparent
, cluster
,
2068 HAMMER2_DELETE_PERMANENT
);
2071 cluster
= hammer2_cluster_next(dparent
, cluster
,
2073 key_next
, (hammer2_key_t
)-1,
2074 HAMMER2_LOOKUP_NODATA
);
2078 hammer2_cluster_lookup_done(dparent
);
2080 if ((ip
->flags
& HAMMER2_INODE_RESIZED
) && ip
->size
> ripdata
->size
) {
2081 wipdata
= hammer2_cluster_modify_ip(trans
, ip
, cparent
, 0);
2082 wipdata
->size
= ip
->size
;
2083 atomic_clear_int(&ip
->flags
, HAMMER2_INODE_RESIZED
);
2086 * When resizing larger we may not have any direct-data
2089 if ((wipdata
->op_flags
& HAMMER2_OPFLAG_DIRECTDATA
) &&
2090 ip
->size
> HAMMER2_EMBEDDED_BYTES
) {
2091 wipdata
->op_flags
&= ~HAMMER2_OPFLAG_DIRECTDATA
;
2092 bzero(&wipdata
->u
.blockset
,
2093 sizeof(wipdata
->u
.blockset
));
2099 hammer2_cluster_modsync(cparent
);