2 * Copyright (c) 2011-2013 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>
44 RB_GENERATE2(hammer2_inode_tree, hammer2_inode, rbnode, hammer2_inode_cmp,
48 hammer2_inode_cmp(hammer2_inode_t *ip1, hammer2_inode_t *ip2)
50 if (ip1->inum < ip2->inum)
52 if (ip1->inum > ip2->inum)
58 hammer2_inode_lookup(hammer2_pfsmount_t *pmp, hammer2_tid_t inum)
63 spin_lock(&pmp->inum_spin);
64 ip = RB_LOOKUP(hammer2_inode_tree, &pmp->inum_tree, inum);
66 hammer2_inode_ref(ip);
67 spin_unlock(&pmp->inum_spin);
75 * Adding a ref to an inode is only legal if the inode already has at least
79 hammer2_inode_ref(hammer2_inode_t *ip)
81 atomic_add_int(&ip->refs, 1);
85 * Drop an inode reference, freeing the inode when the last reference goes
89 hammer2_inode_drop(hammer2_inode_t *ip)
93 hammer2_chain_t *chain;
101 * Transition to zero, must interlock with
102 * the inode inumber lookup tree (if applicable).
104 * NOTE: The super-root inode has no pmp.
107 spin_lock(&ip->pmp->inum_spin);
109 if (atomic_cmpset_int(&ip->refs, 1, 0)) {
110 KKASSERT(ip->topo_cst.count == 0);
111 if (ip->flags & HAMMER2_INODE_ONRBTREE) {
112 atomic_clear_int(&ip->flags,
113 HAMMER2_INODE_ONRBTREE);
114 RB_REMOVE(hammer2_inode_tree,
119 spin_unlock(&ip->pmp->inum_spin);
128 hammer2_chain_drop(chain);
131 * We have to drop pip (if non-NULL) to
132 * dispose of our implied reference from
133 * ip->pip. We can simply loop on it.
135 kfree(ip, hmp->minode);
137 /* continue with pip (can be NULL) */
140 spin_unlock(&ip->pmp->inum_spin);
144 * Non zero transition
146 if (atomic_cmpset_int(&ip->refs, refs, refs - 1))
153 * Get the vnode associated with the given inode, allocating the vnode if
154 * necessary. The vnode will be returned exclusively locked.
156 * The caller must lock the inode (shared or exclusive).
158 * Great care must be taken to avoid deadlocks and vnode acquisition/reclaim
162 hammer2_igetv(hammer2_inode_t *ip, int *errorp)
164 hammer2_inode_data_t *ipdata;
165 hammer2_pfsmount_t *pmp;
170 KKASSERT(pmp != NULL);
172 ipdata = &ip->chain->data->ipdata;
176 * Attempt to reuse an existing vnode assignment. It is
177 * possible to race a reclaim so the vget() may fail. The
178 * inode must be unlocked during the vget() to avoid a
179 * deadlock against a reclaim.
184 * Inode must be unlocked during the vget() to avoid
185 * possible deadlocks, but leave the ip ref intact.
187 * vnode is held to prevent destruction during the
188 * vget(). The vget() can still fail if we lost
189 * a reclaim race on the vnode.
191 vhold_interlocked(vp);
192 ostate = hammer2_inode_lock_temp_release(ip);
193 if (vget(vp, LK_EXCLUSIVE)) {
195 hammer2_inode_lock_temp_restore(ip, ostate);
198 hammer2_inode_lock_temp_restore(ip, ostate);
200 /* vp still locked and ref from vget */
202 kprintf("hammer2: igetv race %p/%p\n",
212 * No vnode exists, allocate a new vnode. Beware of
213 * allocation races. This function will return an
214 * exclusively locked and referenced vnode.
216 *errorp = getnewvnode(VT_HAMMER2, pmp->mp, &vp, 0, 0);
218 kprintf("hammer2: igetv getnewvnode failed %d\n",
225 * Lock the inode and check for an allocation race.
227 ostate = hammer2_inode_lock_upgrade(ip);
228 if (ip->vp != NULL) {
231 hammer2_inode_lock_downgrade(ip, ostate);
235 switch (ipdata->type) {
236 case HAMMER2_OBJTYPE_DIRECTORY:
239 case HAMMER2_OBJTYPE_REGFILE:
241 vinitvmio(vp, ipdata->size,
243 (int)ipdata->size & HAMMER2_LBUFMASK);
245 case HAMMER2_OBJTYPE_SOFTLINK:
247 * XXX for now we are using the generic file_read
248 * and file_write code so we need a buffer cache
252 vinitvmio(vp, ipdata->size,
254 (int)ipdata->size & HAMMER2_LBUFMASK);
258 panic("hammer2: unhandled objtype %d", ipdata->type);
262 if (ip == pmp->iroot)
263 vsetflags(vp, VROOT);
267 hammer2_inode_ref(ip); /* vp association */
268 hammer2_inode_lock_downgrade(ip, ostate);
273 * Return non-NULL vp and *errorp == 0, or NULL vp and *errorp != 0.
275 if (hammer2_debug & 0x0002) {
276 kprintf("igetv vp %p refs %d aux %d\n",
277 vp, vp->v_sysref.refcnt, vp->v_auxrefs);
283 * The passed-in chain must be locked and the returned inode will also be
284 * locked. A ref is added to both the chain and the inode. The chain lock
285 * is inherited by the inode structure and should not be separately released.
287 * The hammer2_inode structure regulates the interface between the high level
288 * kernel VNOPS API and the filesystem backend (the chains).
290 * WARNING! This routine sucks up the chain's lock (makes it part of the
291 * inode lock from the point of view of the inode lock API),
292 * so callers need to be careful.
294 * WARNING! The mount code is allowed to pass dip == NULL for iroot and
295 * is allowed to pass pmp == NULL and dip == NULL for sroot.
298 hammer2_inode_get(hammer2_mount_t *hmp, hammer2_pfsmount_t *pmp,
299 hammer2_inode_t *dip, hammer2_chain_t *chain)
301 hammer2_inode_t *nip;
302 hammer2_chain_t *ochain;
304 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_INODE);
307 * Interlocked lookup/ref of the inode. This code is only needed
308 * when looking up inodes with nlinks != 0 (TODO: optimize out
309 * otherwise and test for duplicates).
313 nip = hammer2_inode_lookup(pmp, chain->data->ipdata.inum);
316 ccms_thread_lock(&nip->topo_cst, CCMS_STATE_EXCLUSIVE);
317 if ((nip->flags & HAMMER2_INODE_ONRBTREE) == 0) {
318 ccms_thread_unlock(&nip->topo_cst);
319 hammer2_inode_drop(nip);
322 if (nip->chain != chain) {
323 hammer2_chain_ref(chain); /* new nip->chain */
325 nip->chain = chain; /* fully locked */
326 hammer2_chain_drop(ochain); /* old nip->chain */
329 * Consolidated nip/nip->chain is locked (chain locked
336 * We couldn't find the inode number, create a new inode.
338 nip = kmalloc(sizeof(*nip), hmp->minode, M_WAITOK | M_ZERO);
339 nip->inum = chain->data->ipdata.inum;
341 hammer2_chain_ref(chain); /* nip->chain */
342 nip->pip = dip; /* can be NULL */
344 hammer2_inode_ref(dip); /* ref dip for nip->pip */
350 * ref and lock on nip gives it state compatible to after a
351 * hammer2_inode_lock_ex() call.
354 ccms_cst_init(&nip->topo_cst, &nip->chain);
355 ccms_thread_lock(&nip->topo_cst, CCMS_STATE_EXCLUSIVE);
356 /* combination of thread lock and chain lock == inode lock */
359 * Attempt to add the inode. If it fails we raced another inode
360 * get. Undo all the work and try again.
363 spin_lock(&pmp->inum_spin);
364 if (RB_INSERT(hammer2_inode_tree, &pmp->inum_tree, nip)) {
365 spin_unlock(&pmp->inum_spin);
366 ccms_thread_unlock(&nip->topo_cst);
367 hammer2_inode_drop(nip);
370 atomic_set_int(&nip->flags, HAMMER2_INODE_ONRBTREE);
371 spin_unlock(&pmp->inum_spin);
378 * Put away an inode, unlocking it and disconnecting it from its chain.
380 * The inode must be exclusively locked on call and non-recursed, with
381 * at least 2 refs (one belonging to the exclusive lock, and one additional
382 * ref belonging to the caller).
384 * Upon return the inode typically has one ref remaining which the caller
388 hammer2_inode_put(hammer2_inode_t *ip)
390 hammer2_inode_t *pip;
391 hammer2_chain_t *chain;
394 * Disconnect and unlock chain
396 KKASSERT(ip->refs >= 2);
397 KKASSERT(ip->topo_cst.count == -1); /* one excl lock allowed */
398 if ((chain = ip->chain) != NULL) {
400 hammer2_inode_unlock_ex(ip);
401 hammer2_chain_unlock(chain); /* because ip->chain now NULL */
402 hammer2_chain_drop(chain); /* from *_get() */
408 if ((pip = ip->pip) != NULL) {
410 hammer2_inode_drop(pip);
415 * Create a new inode in the specified directory using the vattr to
416 * figure out the type of inode.
418 * If no error occurs the new inode with its chain locked is returned in
419 * *nipp, otherwise an error is returned and *nipp is set to NULL.
421 * If vap and/or cred are NULL the related fields are not set and the
422 * inode type defaults to a directory. This is used when creating PFSs
423 * under the super-root, so the inode number is set to 1 in this case.
425 * dip is not locked on entry.
428 hammer2_inode_create(hammer2_trans_t *trans, hammer2_inode_t *dip,
429 struct vattr *vap, struct ucred *cred,
430 const uint8_t *name, size_t name_len,
433 hammer2_inode_data_t *dipdata;
434 hammer2_inode_data_t *nipdata;
435 hammer2_mount_t *hmp;
436 hammer2_chain_t *chain;
437 hammer2_chain_t *parent;
438 hammer2_inode_t *nip;
447 lhc = hammer2_dirhash(name, name_len);
451 * Locate the inode or indirect block to create the new
452 * entry in. At the same time check for key collisions
453 * and iterate until we don't get one.
455 * NOTE: hidden inodes do not have iterators.
458 hammer2_inode_lock_ex(dip);
459 dipdata = &dip->chain->data->ipdata;
460 dip_uid = dipdata->uid;
461 dip_gid = dipdata->gid;
462 dip_mode = dipdata->mode;
464 parent = hammer2_chain_lookup_init(dip->chain, 0);
467 chain = hammer2_chain_lookup(&parent, lhc, lhc, 0);
470 if ((lhc & HAMMER2_DIRHASH_VISIBLE) == 0)
472 if ((lhc & HAMMER2_DIRHASH_LOMASK) == HAMMER2_DIRHASH_LOMASK)
474 hammer2_chain_unlock(chain);
479 error = hammer2_chain_create(trans, &parent, &chain,
481 HAMMER2_BREF_TYPE_INODE,
482 HAMMER2_INODE_BYTES);
486 * Cleanup and handle retries.
488 if (error == EAGAIN) {
489 hammer2_chain_ref(parent);
490 hammer2_chain_lookup_done(parent);
491 hammer2_inode_unlock_ex(dip);
492 hammer2_chain_wait(parent);
493 hammer2_chain_drop(parent);
496 hammer2_chain_lookup_done(parent);
497 hammer2_inode_unlock_ex(dip);
500 KKASSERT(chain == NULL);
506 * Set up the new inode.
508 * NOTE: *_get() integrates chain's lock into the inode lock.
510 * NOTE: Only one new inode can currently be created per
511 * transaction. If the need arises we can adjust
512 * hammer2_trans_init() to allow more.
514 chain->data->ipdata.inum = trans->sync_tid;
515 nip = hammer2_inode_get(dip->hmp, dip->pmp, dip, chain);
516 nipdata = &chain->data->ipdata;
519 KKASSERT(trans->inodes_created == 0);
520 nipdata->type = hammer2_get_obj_type(vap->va_type);
521 nipdata->inum = trans->sync_tid;
522 ++trans->inodes_created;
524 nipdata->type = HAMMER2_OBJTYPE_DIRECTORY;
527 nipdata->version = HAMMER2_INODE_VERSION_ONE;
528 hammer2_update_time(&nipdata->ctime);
529 nipdata->mtime = nipdata->ctime;
531 nipdata->mode = vap->va_mode;
535 xuid = hammer2_to_unix_xid(&dip_uid);
536 xuid = vop_helper_create_uid(dip->pmp->mp,
544 if (vap->va_vaflags & VA_UID_UUID_VALID)
545 nipdata->uid = vap->va_uid_uuid;
546 else if (vap->va_uid != (uid_t)VNOVAL)
547 hammer2_guid_to_uuid(&nipdata->uid, vap->va_uid);
549 hammer2_guid_to_uuid(&nipdata->uid, xuid);
551 if (vap->va_vaflags & VA_GID_UUID_VALID)
552 nipdata->gid = vap->va_gid_uuid;
553 else if (vap->va_gid != (gid_t)VNOVAL)
554 hammer2_guid_to_uuid(&nipdata->gid, vap->va_gid);
556 nipdata->gid = dip_gid;
560 * Regular files and softlinks allow a small amount of data to be
561 * directly embedded in the inode. This flag will be cleared if
562 * the size is extended past the embedded limit.
564 if (nipdata->type == HAMMER2_OBJTYPE_REGFILE ||
565 nipdata->type == HAMMER2_OBJTYPE_SOFTLINK) {
566 nipdata->op_flags |= HAMMER2_OPFLAG_DIRECTDATA;
569 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
570 bcopy(name, nipdata->filename, name_len);
571 nipdata->name_key = lhc;
572 nipdata->name_len = name_len;
578 * chain may have been moved around by the create.
582 hammer2_chain_refactor(hammer2_chain_t **chainp)
584 hammer2_chain_t *chain = *chainp;
585 hammer2_chain_t *tmp;
587 while (chain->duplink && (chain->flags & HAMMER2_CHAIN_DELETED)) {
588 tmp = chain->duplink;
589 while (tmp->duplink && (tmp->flags & HAMMER2_CHAIN_DELETED))
591 hammer2_chain_ref(chain);
592 hammer2_chain_unlock(chain);
593 hammer2_chain_lock(tmp, HAMMER2_RESOLVE_ALWAYS);
594 hammer2_chain_drop(chain);
602 * ochain represents the target file inode. We need to move it to the
603 * specified common parent directory (dip) and rename it to a special
604 * invisible "0xINODENUMBER" filename.
606 * We use chain_duplicate and duplicate ochain at the new location,
607 * renaming it appropriately. We create a temporary chain and
608 * then delete it to placemark where the duplicate will go. Both of
609 * these use the inode number for (lhc) (the key), generating the
610 * invisible filename.
614 hammer2_hardlink_shiftup(hammer2_trans_t *trans, hammer2_chain_t **ochainp,
615 hammer2_inode_t *dip, int *errorp)
617 hammer2_inode_data_t *nipdata;
618 hammer2_mount_t *hmp;
619 hammer2_chain_t *parent;
620 hammer2_chain_t *ochain;
621 hammer2_chain_t *nchain;
622 hammer2_chain_t *tmp;
624 hammer2_blockref_t bref;
629 lhc = ochain->data->ipdata.inum;
630 KKASSERT((lhc & HAMMER2_DIRHASH_VISIBLE) == 0);
633 * Locate the inode or indirect block to create the new
634 * entry in. lhc represents the inode number so there is
635 * no collision iteration.
637 * There should be no key collisions with invisible inode keys.
640 parent = hammer2_chain_lookup_init(dip->chain, 0);
641 nchain = hammer2_chain_lookup(&parent, lhc, lhc, 0);
643 kprintf("X3 chain %p parent %p dip %p dip->chain %p\n",
644 nchain, parent, dip, dip->chain);
645 hammer2_chain_unlock(nchain);
654 * Create entry in common parent directory using the seek position
658 KKASSERT(nchain == NULL);
659 *errorp = hammer2_chain_create(trans, &parent, &nchain,
661 HAMMER2_BREF_TYPE_INODE,/* n/a */
662 HAMMER2_INODE_BYTES); /* n/a */
663 hammer2_chain_refactor(&ochain);
668 * Cleanup and handle retries.
670 if (*errorp == EAGAIN) {
671 hammer2_chain_ref(parent);
672 hammer2_chain_lookup_done(parent);
673 hammer2_chain_wait(parent);
674 hammer2_chain_drop(parent);
679 * Handle the error case
682 KKASSERT(nchain == NULL);
683 hammer2_chain_lookup_done(parent);
688 * Use chain as a placeholder for (lhc), delete it and replace
689 * it with our duplication.
691 * Gain a second lock on ochain for the duplication function to
692 * unlock, maintain the caller's original lock across the call.
694 * This is a bit messy.
696 hammer2_chain_delete(trans, parent, nchain);
697 hammer2_chain_lock(ochain, HAMMER2_RESOLVE_ALWAYS);
700 bref.key = lhc; /* invisible dir entry key */
702 hammer2_chain_duplicate(trans, parent, nchain->index, &tmp, &bref);
703 hammer2_chain_lookup_done(parent);
704 hammer2_chain_unlock(nchain); /* no longer needed */
707 * Now set chain to our duplicate and modify it appropriately.
709 * Directory entries are inodes but this is a hidden hardlink
710 * target. The name isn't used but to ease debugging give it
711 * a name after its inode number.
714 tmp = NULL; /* safety */
716 hammer2_chain_modify(trans, &nchain, HAMMER2_MODIFY_ASSERTNOCOPY);
717 nipdata = &nchain->data->ipdata;
718 ksnprintf(nipdata->filename, sizeof(nipdata->filename),
719 "0x%016jx", (intmax_t)nipdata->inum);
720 nipdata->name_len = strlen(nipdata->filename);
721 nipdata->name_key = lhc;
727 * Connect the target inode represented by (*chainp) to the media topology
728 * at (dip, name, len).
730 * If hlink is TRUE this function creates an OBJTYPE_HARDLINK directory
731 * entry instead of connecting (*chainp).
733 * If hlink is FALSE this function uses chain_duplicate() to make a copy
734 * if (*chainp) in the directory entry. (*chainp) is likely to be deleted
735 * by the caller in this case (e.g. rename).
738 hammer2_inode_connect(hammer2_trans_t *trans, int hlink,
739 hammer2_inode_t *dip, hammer2_chain_t **chainp,
740 const uint8_t *name, size_t name_len)
742 hammer2_inode_data_t *ipdata;
743 hammer2_mount_t *hmp;
744 hammer2_chain_t *nchain;
745 hammer2_chain_t *parent;
746 hammer2_chain_t *ochain;
755 * Since ochain is either disconnected from the topology or represents
756 * a hardlink terminus which is always a parent of or equal to dip,
757 * we should be able to safely lock dip->chain for our setup.
759 parent = hammer2_chain_lookup_init(dip->chain, 0);
761 lhc = hammer2_dirhash(name, name_len);
764 * Locate the inode or indirect block to create the new
765 * entry in. At the same time check for key collisions
766 * and iterate until we don't get one.
770 nchain = hammer2_chain_lookup(&parent, lhc, lhc, 0);
773 if ((lhc & HAMMER2_DIRHASH_LOMASK) == HAMMER2_DIRHASH_LOMASK)
775 hammer2_chain_unlock(nchain);
783 * Hardlink pointer needed, create totally fresh
786 KKASSERT(nchain == NULL);
787 error = hammer2_chain_create(trans, &parent, &nchain,
789 HAMMER2_BREF_TYPE_INODE,
790 HAMMER2_INODE_BYTES);
791 hammer2_chain_refactor(&ochain);
794 * Reconnect the original chain and rename. Use
795 * chain_duplicate(). The caller will likely delete
796 * or has already deleted the original chain in
801 hammer2_chain_duplicate(trans, NULL, -1, &nchain, NULL);
802 error = hammer2_chain_create(trans, &parent, &nchain,
804 HAMMER2_BREF_TYPE_INODE,
805 HAMMER2_INODE_BYTES);
812 KKASSERT(error != EAGAIN);
813 hammer2_chain_lookup_done(parent);
817 * nchain should be NULL on error, leave ochain (== *chainp) alone.
820 KKASSERT(nchain == NULL);
825 * Directory entries are inodes so if the name has changed we have
826 * to update the inode.
828 * When creating an OBJTYPE_HARDLINK entry remember to unlock the
829 * chain, the caller will access the hardlink via the actual hardlink
830 * target file and not the hardlink pointer entry, so we must still
833 if (hlink && hammer2_hardlink_enable >= 0) {
835 * Create the HARDLINK pointer. oip represents the hardlink
836 * target in this situation.
838 * We will return ochain (the hardlink target).
840 hammer2_chain_modify(trans, &nchain,
841 HAMMER2_MODIFY_ASSERTNOCOPY);
842 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
843 ipdata = &nchain->data->ipdata;
844 bcopy(name, ipdata->filename, name_len);
845 ipdata->name_key = lhc;
846 ipdata->name_len = name_len;
847 ipdata->target_type = ochain->data->ipdata.type;
848 ipdata->type = HAMMER2_OBJTYPE_HARDLINK;
849 ipdata->inum = ochain->data->ipdata.inum;
851 hammer2_chain_unlock(nchain);
854 } else if (hlink && hammer2_hardlink_enable < 0) {
856 * Create a snapshot (hardlink fake mode for debugging).
857 * (ochain already flushed above so we can just copy the
860 * Since this is a snapshot we return nchain in the fake
863 hammer2_chain_modify(trans, &nchain,
864 HAMMER2_MODIFY_ASSERTNOCOPY);
865 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
866 ipdata = &nchain->data->ipdata;
867 *ipdata = ochain->data->ipdata;
868 bcopy(name, ipdata->filename, name_len);
869 ipdata->name_key = lhc;
870 ipdata->name_len = name_len;
871 kprintf("created fake hardlink %*.*s\n",
872 (int)name_len, (int)name_len, name);
875 * nchain is a duplicate of ochain at the new location.
876 * We must fixup the name stored in oip. The bref key
877 * has already been set up.
879 hammer2_chain_modify(trans, &nchain,
880 HAMMER2_MODIFY_ASSERTNOCOPY);
881 ipdata = &nchain->data->ipdata;
883 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
884 bcopy(name, ipdata->filename, name_len);
885 ipdata->name_key = lhc;
886 ipdata->name_len = name_len;
891 * We are replacing ochain with nchain, unlock ochain. In the
892 * case where ochain is left unchanged the code above sets
893 * nchain to ochain and ochain to NULL, resulting in a NOP here.
896 hammer2_chain_unlock(ochain);
903 * Caller must hold exactly ONE exclusive lock on the inode. *nchainp
904 * must be exclusive locked (its own exclusive lock even if it is the
905 * same as ip->chain).
907 * This function replaces ip->chain. The exclusive lock on the passed
908 * nchain is inherited by the inode and the caller becomes responsible
909 * for unlocking it when the caller unlocks the inode.
911 * ochain was locked by the caller indirectly via the inode lock. Since
912 * ip->chain is being repointed, we become responsible for cleaning up
915 * Return *nchainp = NULL as a safety.
918 hammer2_inode_repoint(hammer2_inode_t *ip, hammer2_inode_t *pip,
919 hammer2_chain_t *nchain)
921 hammer2_chain_t *ochain;
922 hammer2_inode_t *opip;
925 * ip->chain points to the hardlink target, not the hardlink psuedo
926 * inode. Do not repoint nchain to the pseudo-node.
928 if (nchain->data->ipdata.type == HAMMER2_OBJTYPE_HARDLINK)
932 * Repoint ip->chain if necessary.
934 * (Inode must be locked exclusively by parent)
937 if (ochain != nchain) {
938 hammer2_chain_ref(nchain); /* for ip->chain */
941 hammer2_chain_unlock(ochain);
942 hammer2_chain_drop(ochain); /* for ip->chain */
944 /* replace locked chain in ip (additional lock) */
945 hammer2_chain_lock(nchain, HAMMER2_RESOLVE_ALWAYS);
947 if (ip->pip != pip) {
950 hammer2_inode_ref(pip);
953 hammer2_inode_drop(opip);
958 * Unlink the file from the specified directory inode. The directory inode
959 * does not need to be locked. The caller should pass a non-NULL (ip)
960 * representing the object being removed only if the related vnode is
961 * potentially inactive (not referenced in the caller's active path),
962 * so we can vref/vrele it to trigger the VOP_INACTIVE path and properly
965 * isdir determines whether a directory/non-directory check should be made.
966 * No check is made if isdir is set to -1.
968 * NOTE! This function does not prevent the underlying file from still
969 * being used if it has other refs (such as from an inode, or if it's
970 * chain is manually held). However, the caller is responsible for
971 * fixing up ip->chain if e.g. a rename occurs (see chain_duplicate()).
974 hammer2_unlink_file(hammer2_trans_t *trans, hammer2_inode_t *dip,
975 const uint8_t *name, size_t name_len,
976 int isdir, int *hlinkp)
978 hammer2_inode_data_t *ipdata;
979 hammer2_mount_t *hmp;
980 hammer2_chain_t *parent;
981 hammer2_chain_t *ochain;
982 hammer2_chain_t *chain;
983 hammer2_chain_t *dparent;
984 hammer2_chain_t *dchain;
994 lhc = hammer2_dirhash(name, name_len);
997 * Search for the filename in the directory
1001 hammer2_inode_lock_ex(dip);
1003 parent = hammer2_chain_lookup_init(dip->chain, 0);
1004 chain = hammer2_chain_lookup(&parent,
1005 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1008 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
1009 name_len == chain->data->ipdata.name_len &&
1010 bcmp(name, chain->data->ipdata.filename, name_len) == 0) {
1013 chain = hammer2_chain_next(&parent, chain,
1014 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1017 hammer2_inode_unlock_ex(dip); /* retain parent */
1020 * Not found or wrong type (isdir < 0 disables the type check).
1021 * If a hardlink pointer, type checks use the hardlink target.
1023 if (chain == NULL) {
1027 if ((type = chain->data->ipdata.type) == HAMMER2_OBJTYPE_HARDLINK) {
1030 type = chain->data->ipdata.target_type;
1033 if (type == HAMMER2_OBJTYPE_DIRECTORY && isdir == 0) {
1037 if (type != HAMMER2_OBJTYPE_DIRECTORY && isdir >= 1) {
1043 * Hardlink must be resolved. We can't hold parent locked while we
1044 * do this or we could deadlock.
1046 * On success chain will be adjusted to point at the hardlink target
1047 * and ochain will point to the hardlink pointer in the original
1048 * directory. Otherwise chain remains pointing to the original.
1050 if (chain->data->ipdata.type == HAMMER2_OBJTYPE_HARDLINK) {
1051 KKASSERT(parent_ref == 0);
1052 hammer2_chain_unlock(parent);
1054 error = hammer2_hardlink_find(dip, &chain, &ochain);
1058 * If this is a directory the directory must be empty. However, if
1059 * isdir < 0 we are doing a rename and the directory does not have
1060 * to be empty, and if isdir > 1 we are deleting a PFS/snapshot
1061 * and the directory does not have to be empty.
1063 * NOTE: We check the full key range here which covers both visible
1064 * and invisible entries. Theoretically there should be no
1065 * invisible (hardlink target) entries if there are no visible
1068 if (type == HAMMER2_OBJTYPE_DIRECTORY && isdir == 1) {
1069 dparent = hammer2_chain_lookup_init(chain, 0);
1070 dchain = hammer2_chain_lookup(&dparent,
1071 0, (hammer2_key_t)-1,
1072 HAMMER2_LOOKUP_NODATA);
1074 hammer2_chain_unlock(dchain);
1075 hammer2_chain_lookup_done(dparent);
1079 hammer2_chain_lookup_done(dparent);
1085 * Ok, we can now unlink the chain. We always decrement nlinks even
1086 * if the entry can be deleted in case someone has the file open and
1089 * The chain itself will no longer be in the on-media topology but
1090 * can still be flushed to the media (e.g. if an open descriptor
1091 * remains). When the last vnode/ip ref goes away the chain will
1092 * be marked unmodified, avoiding any further (now unnecesary) I/O.
1094 * A non-NULL ochain indicates a hardlink.
1098 * Delete the original hardlink pointer.
1100 * NOTE: parent from above is NULL when ochain != NULL
1101 * so we can reuse it.
1103 hammer2_chain_lock(ochain, HAMMER2_RESOLVE_ALWAYS);
1106 parent = ochain->parent;
1107 hammer2_chain_ref(parent);
1108 hammer2_chain_unlock(ochain);
1109 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
1110 hammer2_chain_lock(ochain, HAMMER2_RESOLVE_ALWAYS);
1111 if (ochain->parent == parent)
1113 hammer2_chain_unlock(parent);
1114 hammer2_chain_drop(parent);
1117 hammer2_chain_delete(trans, parent, ochain);
1118 hammer2_chain_unlock(ochain);
1119 hammer2_chain_unlock(parent);
1120 hammer2_chain_drop(parent);
1124 * Then decrement nlinks on hardlink target, deleting
1125 * the target when nlinks drops to 0.
1127 if (chain->data->ipdata.nlinks == 1) {
1128 dparent = chain->parent;
1129 hammer2_chain_ref(chain);
1130 hammer2_chain_unlock(chain);
1131 hammer2_chain_lock(dparent, HAMMER2_RESOLVE_ALWAYS);
1132 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS);
1133 hammer2_chain_drop(chain);
1134 hammer2_chain_modify(trans, &chain, 0);
1135 --chain->data->ipdata.nlinks;
1136 hammer2_chain_delete(trans, dparent, chain);
1137 hammer2_chain_unlock(dparent);
1139 hammer2_chain_modify(trans, &chain, 0);
1140 --chain->data->ipdata.nlinks;
1144 * Otherwise this was not a hardlink and we can just
1145 * remove the entry and decrement nlinks.
1147 * NOTE: *_get() integrates chain's lock into the inode lock.
1149 hammer2_chain_modify(trans, &chain, 0);
1150 ipdata = &chain->data->ipdata;
1152 hammer2_chain_delete(trans, parent, chain);
1158 hammer2_chain_unlock(chain);
1160 hammer2_chain_lookup_done(parent);
1162 hammer2_chain_drop(parent);
1165 hammer2_chain_drop(ochain);
1171 * Calculate the allocation size for the file fragment straddling EOF
1174 hammer2_inode_calc_alloc(hammer2_key_t filesize)
1176 int frag = (int)filesize & HAMMER2_PBUFMASK;
1181 for (radix = HAMMER2_MINALLOCRADIX; frag > (1 << radix); ++radix)
1187 * Given an exclusively locked inode we consolidate its chain for hardlink
1188 * creation, adding (nlinks) to the file's link count and potentially
1189 * relocating the inode to a directory common to ip->pip and tdip.
1191 * Returns a locked chain in (*chainp) (the chain's lock is in addition to
1192 * any lock it might already have due to the inode being locked). *chainp
1193 * is set unconditionally and its previous contents can be garbage.
1195 * The caller is responsible for replacing ip->chain, not us. For certain
1196 * operations such as renames the caller may do additional manipulation
1197 * of the chain before replacing ip->chain.
1200 hammer2_hardlink_consolidate(hammer2_trans_t *trans, hammer2_inode_t *ip,
1201 hammer2_chain_t **chainp,
1202 hammer2_inode_t *tdip, int nlinks)
1204 hammer2_inode_data_t *ipdata;
1205 hammer2_mount_t *hmp;
1206 hammer2_inode_t *fdip;
1207 hammer2_inode_t *cdip;
1208 hammer2_chain_t *chain;
1209 hammer2_chain_t *nchain;
1210 hammer2_chain_t *parent;
1214 * Extra lock on chain so it can be returned locked.
1219 error = hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS);
1220 KKASSERT(error == 0);
1222 if (nlinks == 0 && /* no hardlink needed */
1223 (chain->data->ipdata.name_key & HAMMER2_DIRHASH_VISIBLE)) {
1227 if (hammer2_hardlink_enable < 0) { /* fake hardlinks */
1232 if (hammer2_hardlink_enable == 0) { /* disallow hardlinks */
1233 hammer2_chain_unlock(chain);
1239 * cdip will be returned with a ref, but not locked.
1242 cdip = hammer2_inode_common_parent(fdip, tdip);
1245 * If no change in the hardlink's target directory is required and
1246 * this is already a hardlink target, all we need to do is adjust
1249 * XXX The common parent is a big wiggly due to duplication from
1250 * renames. Compare the core (RBTREE) pointer instead of the
1254 (chain->data->ipdata.name_key & HAMMER2_DIRHASH_VISIBLE) == 0) {
1256 hammer2_chain_modify(trans, &chain, 0);
1257 chain->data->ipdata.nlinks += nlinks;
1265 * We either have to move an existing hardlink target or we have
1266 * to create a fresh hardlink target.
1268 * Hardlink targets are hidden inodes in a parent directory common
1269 * to all directory entries referencing the hardlink.
1271 nchain = hammer2_hardlink_shiftup(trans, &chain, cdip, &error);
1275 * Bump nlinks on duplicated hidden inode, repoint
1278 hammer2_chain_modify(trans, &nchain, 0);
1279 nchain->data->ipdata.nlinks += nlinks;
1280 hammer2_inode_repoint(ip, cdip, nchain);
1283 * If the old chain is not a hardlink target then replace
1284 * it with a OBJTYPE_HARDLINK pointer.
1286 * If the old chain IS a hardlink target then delete it.
1288 if (chain->data->ipdata.name_key & HAMMER2_DIRHASH_VISIBLE) {
1290 * Replace original non-hardlink that's been dup'd
1291 * with a special hardlink directory entry. We must
1292 * set the DIRECTDATA flag to prevent sub-chains
1293 * from trying to synchronize to the inode if the
1294 * file is extended afterwords.
1296 hammer2_chain_modify(trans, &chain, 0);
1297 ipdata = &chain->data->ipdata;
1298 ipdata->target_type = ipdata->type;
1299 ipdata->type = HAMMER2_OBJTYPE_HARDLINK;
1307 bzero(&ipdata->uid, sizeof(ipdata->uid));
1308 bzero(&ipdata->gid, sizeof(ipdata->gid));
1309 ipdata->op_flags = HAMMER2_OPFLAG_DIRECTDATA;
1310 ipdata->cap_flags = 0;
1314 ipdata->iparent = 0; /* XXX */
1315 ipdata->pfs_type = 0;
1316 ipdata->pfs_inum = 0;
1317 bzero(&ipdata->pfs_clid, sizeof(ipdata->pfs_clid));
1318 bzero(&ipdata->pfs_fsid, sizeof(ipdata->pfs_fsid));
1319 ipdata->data_quota = 0;
1320 ipdata->data_count = 0;
1321 ipdata->inode_quota = 0;
1322 ipdata->inode_count = 0;
1323 ipdata->attr_tid = 0;
1324 ipdata->dirent_tid = 0;
1325 bzero(&ipdata->u, sizeof(ipdata->u));
1326 /* XXX transaction ids */
1328 kprintf("DELETE INVISIBLE\n");
1330 parent = chain->parent;
1331 hammer2_chain_ref(parent);
1332 hammer2_chain_ref(chain);
1333 hammer2_chain_unlock(chain);
1334 hammer2_chain_lock(parent,
1335 HAMMER2_RESOLVE_ALWAYS);
1336 hammer2_chain_lock(chain,
1337 HAMMER2_RESOLVE_ALWAYS);
1338 hammer2_chain_drop(chain);
1339 if (chain->parent == parent)
1341 hammer2_chain_unlock(parent);
1342 hammer2_chain_drop(parent);
1344 hammer2_chain_delete(trans, parent, chain);
1345 hammer2_chain_unlock(parent);
1346 hammer2_chain_drop(parent);
1350 * Return the new chain.
1352 hammer2_chain_unlock(chain);
1358 hammer2_chain_unlock(chain);
1363 * Cleanup, chain/nchain already dealt with.
1366 hammer2_inode_drop(cdip);
1372 * If (*ochainp) is non-NULL it points to the forward OBJTYPE_HARDLINK
1373 * inode while (*chainp) points to the resolved (hidden hardlink
1374 * target) inode. In this situation when nlinks is 1 we wish to
1375 * deconsolidate the hardlink, moving it back to the directory that now
1376 * represents the only remaining link.
1379 hammer2_hardlink_deconsolidate(hammer2_trans_t *trans,
1380 hammer2_inode_t *dip,
1381 hammer2_chain_t **chainp,
1382 hammer2_chain_t **ochainp)
1384 if (*ochainp == NULL)
1391 * The caller presents a locked *chainp pointing to a HAMMER2_BREF_TYPE_INODE
1392 * with an obj_type of HAMMER2_OBJTYPE_HARDLINK. This routine will gobble
1393 * the *chainp and return a new locked *chainp representing the file target
1394 * (the original *chainp will be unlocked).
1396 * When a match is found the chain representing the original HARDLINK
1397 * will be returned in *ochainp with a ref, but not locked.
1399 * When no match is found *chainp is set to NULL and EIO is returned.
1400 * (*ochainp) will still be set to the original chain with a ref but not
1404 hammer2_hardlink_find(hammer2_inode_t *dip, hammer2_chain_t **chainp,
1405 hammer2_chain_t **ochainp)
1407 hammer2_chain_t *chain = *chainp;
1408 hammer2_chain_t *parent;
1409 hammer2_inode_t *ip;
1410 hammer2_inode_t *pip;
1414 hammer2_inode_ref(pip); /* for loop */
1415 hammer2_chain_ref(chain); /* for (*ochainp) */
1419 * Locate the hardlink. pip is referenced and not locked,
1424 lhc = chain->data->ipdata.inum;
1425 hammer2_chain_unlock(chain);
1428 while ((ip = pip) != NULL) {
1429 hammer2_inode_lock_ex(ip);
1430 parent = hammer2_chain_lookup_init(ip->chain, 0);
1431 hammer2_inode_drop(ip); /* loop */
1432 KKASSERT(parent->bref.type == HAMMER2_BREF_TYPE_INODE);
1433 chain = hammer2_chain_lookup(&parent, lhc, lhc, 0);
1434 hammer2_chain_lookup_done(parent);
1437 pip = ip->pip; /* safe, ip held locked */
1439 hammer2_inode_ref(pip); /* loop */
1440 hammer2_inode_unlock_ex(ip);
1444 * chain is locked, ip is locked. Unlock ip, return the locked
1445 * chain. *ipp is already set w/a ref count and not locked.
1447 * (parent is already unlocked).
1450 hammer2_inode_unlock_ex(ip);
1453 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_INODE);
1454 /* already locked */
1462 * Find the directory common to both fdip and tdip, hold and return
1466 hammer2_inode_common_parent(hammer2_inode_t *fdip, hammer2_inode_t *tdip)
1468 hammer2_inode_t *scan1;
1469 hammer2_inode_t *scan2;
1472 * We used to have a depth field but it complicated matters too
1473 * much for directory renames. So now its ugly. Check for
1474 * simple cases before giving up and doing it the expensive way.
1476 * XXX need a bottom-up topology stability lock
1478 if (fdip == tdip || fdip == tdip->pip) {
1479 hammer2_inode_ref(fdip);
1482 if (fdip->pip == tdip) {
1483 hammer2_inode_ref(tdip);
1490 for (scan1 = fdip; scan1->pmp == fdip->pmp; scan1 = scan1->pip) {
1492 while (scan2->pmp == tdip->pmp) {
1493 if (scan1 == scan2) {
1494 hammer2_inode_ref(scan1);
1502 panic("hammer2_inode_common_parent: no common parent %p %p\n",