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. This routine typically locates or allocates the inode, assigns
285 * ip->chain (adding a ref to chain if necessary), and returns the inode.
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 */
330 * Consolidated nip/nip->chain is locked (chain locked
337 * We couldn't find the inode number, create a new inode.
339 nip = kmalloc(sizeof(*nip), hmp->minode, M_WAITOK | M_ZERO);
340 nip->inum = chain->data->ipdata.inum;
342 hammer2_chain_ref(chain); /* nip->chain */
343 nip->pip = dip; /* can be NULL */
345 hammer2_inode_ref(dip); /* ref dip for nip->pip */
351 * ref and lock on nip gives it state compatible to after a
352 * hammer2_inode_lock_ex() call.
355 ccms_cst_init(&nip->topo_cst, &nip->chain);
356 ccms_thread_lock(&nip->topo_cst, CCMS_STATE_EXCLUSIVE);
357 /* combination of thread lock and chain lock == inode lock */
360 * Attempt to add the inode. If it fails we raced another inode
361 * get. Undo all the work and try again.
364 spin_lock(&pmp->inum_spin);
365 if (RB_INSERT(hammer2_inode_tree, &pmp->inum_tree, nip)) {
366 spin_unlock(&pmp->inum_spin);
367 ccms_thread_unlock(&nip->topo_cst);
368 hammer2_inode_drop(nip);
371 atomic_set_int(&nip->flags, HAMMER2_INODE_ONRBTREE);
372 spin_unlock(&pmp->inum_spin);
379 * Put away an inode, unlocking it and disconnecting it from its chain.
381 * The inode must be exclusively locked on call and non-recursed, with
382 * at least 2 refs (one belonging to the exclusive lock, and one additional
383 * ref belonging to the caller).
385 * Upon return the inode typically has one ref remaining which the caller
389 hammer2_inode_put(hammer2_inode_t *ip, hammer2_chain_t *chain)
391 hammer2_inode_t *pip;
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_chain_drop(chain); /* from *_get() */
408 hammer2_inode_unlock_ex(ip, chain);
411 * Cleanup delayed actions
414 hammer2_inode_drop(pip);
418 * Create a new inode in the specified directory using the vattr to
419 * figure out the type of inode.
421 * If no error occurs the new inode with its chain locked is returned in
422 * *nipp, otherwise an error is returned and *nipp is set to NULL.
424 * If vap and/or cred are NULL the related fields are not set and the
425 * inode type defaults to a directory. This is used when creating PFSs
426 * under the super-root, so the inode number is set to 1 in this case.
428 * dip is not locked on entry.
431 hammer2_inode_create(hammer2_trans_t *trans, hammer2_inode_t *dip,
432 struct vattr *vap, struct ucred *cred,
433 const uint8_t *name, size_t name_len,
434 hammer2_chain_t **chainp, int *errorp)
436 hammer2_inode_data_t *dipdata;
437 hammer2_inode_data_t *nipdata;
438 hammer2_mount_t *hmp;
439 hammer2_chain_t *chain;
440 hammer2_chain_t *parent;
441 hammer2_inode_t *nip;
450 lhc = hammer2_dirhash(name, name_len);
454 * Locate the inode or indirect block to create the new
455 * entry in. At the same time check for key collisions
456 * and iterate until we don't get one.
458 * NOTE: hidden inodes do not have iterators.
461 parent = hammer2_inode_lock_ex(dip);
462 dipdata = &dip->chain->data->ipdata;
463 dip_uid = dipdata->uid;
464 dip_gid = dipdata->gid;
465 dip_mode = dipdata->mode;
469 chain = hammer2_chain_lookup(&parent, lhc, lhc, 0);
472 if ((lhc & HAMMER2_DIRHASH_VISIBLE) == 0)
474 if ((lhc & HAMMER2_DIRHASH_LOMASK) == HAMMER2_DIRHASH_LOMASK)
476 hammer2_chain_unlock(chain);
481 error = hammer2_chain_create(trans, &parent, &chain,
483 HAMMER2_BREF_TYPE_INODE,
484 HAMMER2_INODE_BYTES);
488 * Cleanup and handle retries.
490 if (error == EAGAIN) {
491 hammer2_chain_ref(parent);
492 hammer2_inode_unlock_ex(dip, parent);
493 hammer2_chain_wait(parent);
494 hammer2_chain_drop(parent);
497 hammer2_inode_unlock_ex(dip, parent);
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;
579 * chain may have been moved around by the create.
583 hammer2_chain_refactor(hammer2_chain_t **chainp)
585 hammer2_chain_t *chain = *chainp;
586 hammer2_chain_t *tmp;
588 while (chain->duplink && (chain->flags & HAMMER2_CHAIN_DELETED)) {
589 tmp = chain->duplink;
590 while (tmp->duplink && (tmp->flags & HAMMER2_CHAIN_DELETED))
592 hammer2_chain_ref(chain);
593 hammer2_chain_unlock(chain);
594 hammer2_chain_lock(tmp, HAMMER2_RESOLVE_ALWAYS);
595 hammer2_chain_drop(chain);
603 * ochain represents the target file inode. We need to move it to the
604 * specified common parent directory (dip) and rename it to a special
605 * invisible "0xINODENUMBER" filename.
607 * We use chain_duplicate and duplicate ochain at the new location,
608 * renaming it appropriately. We create a temporary chain and
609 * then delete it to placemark where the duplicate will go. Both of
610 * these use the inode number for (lhc) (the key), generating the
611 * invisible filename.
615 hammer2_hardlink_shiftup(hammer2_trans_t *trans, hammer2_chain_t **ochainp,
616 hammer2_inode_t *dip, int *errorp)
618 hammer2_inode_data_t *nipdata;
619 hammer2_mount_t *hmp;
620 hammer2_chain_t *parent;
621 hammer2_chain_t *ochain;
622 hammer2_chain_t *nchain;
623 hammer2_chain_t *tmp;
625 hammer2_blockref_t bref;
630 lhc = ochain->data->ipdata.inum;
631 KKASSERT((lhc & HAMMER2_DIRHASH_VISIBLE) == 0);
634 * Locate the inode or indirect block to create the new
635 * entry in. lhc represents the inode number so there is
636 * no collision iteration.
638 * There should be no key collisions with invisible inode keys.
641 parent = hammer2_chain_lookup_init(dip->chain, 0);
642 nchain = hammer2_chain_lookup(&parent, lhc, lhc, 0);
644 kprintf("X3 chain %p parent %p dip %p dip->chain %p\n",
645 nchain, parent, dip, dip->chain);
646 hammer2_chain_unlock(nchain);
655 * Create entry in common parent directory using the seek position
659 KKASSERT(nchain == NULL);
660 *errorp = hammer2_chain_create(trans, &parent, &nchain,
662 HAMMER2_BREF_TYPE_INODE,/* n/a */
663 HAMMER2_INODE_BYTES); /* n/a */
664 hammer2_chain_refactor(&ochain);
669 * Cleanup and handle retries.
671 if (*errorp == EAGAIN) {
672 hammer2_chain_ref(parent);
673 hammer2_chain_lookup_done(parent);
674 hammer2_chain_wait(parent);
675 hammer2_chain_drop(parent);
680 * Handle the error case
683 KKASSERT(nchain == NULL);
684 hammer2_chain_lookup_done(parent);
689 * Use chain as a placeholder for (lhc), delete it and replace
690 * it with our duplication.
692 * Gain a second lock on ochain for the duplication function to
693 * unlock, maintain the caller's original lock across the call.
695 * This is a bit messy.
697 hammer2_chain_delete(trans, parent, nchain);
698 hammer2_chain_lock(ochain, HAMMER2_RESOLVE_ALWAYS);
701 bref.key = lhc; /* invisible dir entry key */
703 hammer2_chain_duplicate(trans, parent, nchain->index, &tmp, &bref);
704 hammer2_chain_lookup_done(parent);
705 hammer2_chain_unlock(nchain); /* no longer needed */
708 * Now set chain to our duplicate and modify it appropriately.
710 * Directory entries are inodes but this is a hidden hardlink
711 * target. The name isn't used but to ease debugging give it
712 * a name after its inode number.
715 tmp = NULL; /* safety */
717 hammer2_chain_modify(trans, &nchain, HAMMER2_MODIFY_ASSERTNOCOPY);
718 nipdata = &nchain->data->ipdata;
719 ksnprintf(nipdata->filename, sizeof(nipdata->filename),
720 "0x%016jx", (intmax_t)nipdata->inum);
721 nipdata->name_len = strlen(nipdata->filename);
722 nipdata->name_key = lhc;
728 * Connect the target inode represented by (*chainp) to the media topology
729 * at (dip, name, len).
731 * If hlink is TRUE this function creates an OBJTYPE_HARDLINK directory
732 * entry instead of connecting (*chainp).
734 * If hlink is FALSE this function uses chain_duplicate() to make a copy
735 * if (*chainp) in the directory entry. (*chainp) is likely to be deleted
736 * by the caller in this case (e.g. rename).
739 hammer2_inode_connect(hammer2_trans_t *trans, int hlink,
740 hammer2_inode_t *dip, hammer2_chain_t **chainp,
741 const uint8_t *name, size_t name_len)
743 hammer2_inode_data_t *ipdata;
744 hammer2_mount_t *hmp;
745 hammer2_chain_t *nchain;
746 hammer2_chain_t *parent;
747 hammer2_chain_t *ochain;
756 * Since ochain is either disconnected from the topology or represents
757 * a hardlink terminus which is always a parent of or equal to dip,
758 * we should be able to safely lock dip->chain for our setup.
760 parent = hammer2_chain_lookup_init(dip->chain, 0);
762 lhc = hammer2_dirhash(name, name_len);
765 * Locate the inode or indirect block to create the new
766 * entry in. At the same time check for key collisions
767 * and iterate until we don't get one.
771 nchain = hammer2_chain_lookup(&parent, lhc, lhc, 0);
774 if ((lhc & HAMMER2_DIRHASH_LOMASK) == HAMMER2_DIRHASH_LOMASK)
776 hammer2_chain_unlock(nchain);
784 * Hardlink pointer needed, create totally fresh
787 KKASSERT(nchain == NULL);
788 error = hammer2_chain_create(trans, &parent, &nchain,
790 HAMMER2_BREF_TYPE_INODE,
791 HAMMER2_INODE_BYTES);
792 hammer2_chain_refactor(&ochain);
795 * Reconnect the original chain and rename. Use
796 * chain_duplicate(). The caller will likely delete
797 * or has already deleted the original chain in
802 hammer2_chain_duplicate(trans, NULL, -1, &nchain, NULL);
803 error = hammer2_chain_create(trans, &parent, &nchain,
805 HAMMER2_BREF_TYPE_INODE,
806 HAMMER2_INODE_BYTES);
813 KKASSERT(error != EAGAIN);
814 hammer2_chain_lookup_done(parent);
818 * nchain should be NULL on error, leave ochain (== *chainp) alone.
821 KKASSERT(nchain == NULL);
826 * Directory entries are inodes so if the name has changed we have
827 * to update the inode.
829 * When creating an OBJTYPE_HARDLINK entry remember to unlock the
830 * chain, the caller will access the hardlink via the actual hardlink
831 * target file and not the hardlink pointer entry, so we must still
834 if (hlink && hammer2_hardlink_enable >= 0) {
836 * Create the HARDLINK pointer. oip represents the hardlink
837 * target in this situation.
839 * We will return ochain (the hardlink target).
841 hammer2_chain_modify(trans, &nchain,
842 HAMMER2_MODIFY_ASSERTNOCOPY);
843 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
844 ipdata = &nchain->data->ipdata;
845 bcopy(name, ipdata->filename, name_len);
846 ipdata->name_key = lhc;
847 ipdata->name_len = name_len;
848 ipdata->target_type = ochain->data->ipdata.type;
849 ipdata->type = HAMMER2_OBJTYPE_HARDLINK;
850 ipdata->inum = ochain->data->ipdata.inum;
852 hammer2_chain_unlock(nchain);
855 } else if (hlink && hammer2_hardlink_enable < 0) {
857 * Create a snapshot (hardlink fake mode for debugging).
858 * (ochain already flushed above so we can just copy the
861 * Since this is a snapshot we return nchain in the fake
864 hammer2_chain_modify(trans, &nchain,
865 HAMMER2_MODIFY_ASSERTNOCOPY);
866 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
867 ipdata = &nchain->data->ipdata;
868 *ipdata = ochain->data->ipdata;
869 bcopy(name, ipdata->filename, name_len);
870 ipdata->name_key = lhc;
871 ipdata->name_len = name_len;
872 kprintf("created fake hardlink %*.*s\n",
873 (int)name_len, (int)name_len, name);
876 * nchain is a duplicate of ochain at the new location.
877 * We must fixup the name stored in oip. The bref key
878 * has already been set up.
880 hammer2_chain_modify(trans, &nchain,
881 HAMMER2_MODIFY_ASSERTNOCOPY);
882 ipdata = &nchain->data->ipdata;
884 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
885 bcopy(name, ipdata->filename, name_len);
886 ipdata->name_key = lhc;
887 ipdata->name_len = name_len;
892 * We are replacing ochain with nchain, unlock ochain. In the
893 * case where ochain is left unchanged the code above sets
894 * nchain to ochain and ochain to NULL, resulting in a NOP here.
897 hammer2_chain_unlock(ochain);
904 * Caller must hold exactly ONE exclusive lock on the inode. *nchainp
905 * must be exclusive locked (its own exclusive lock even if it is the
906 * same as ip->chain).
908 * This function replaces ip->chain. The exclusive lock on the passed
909 * nchain is inherited by the inode and the caller becomes responsible
910 * for unlocking it when the caller unlocks the inode.
912 * ochain was locked by the caller indirectly via the inode lock. Since
913 * ip->chain is being repointed, we become responsible for cleaning up
916 * Return *nchainp = NULL as a safety.
919 hammer2_inode_repoint(hammer2_inode_t *ip, hammer2_inode_t *pip,
920 hammer2_chain_t *nchain)
922 hammer2_chain_t *ochain;
923 hammer2_inode_t *opip;
926 * ip->chain points to the hardlink target, not the hardlink psuedo
927 * inode. Do not repoint nchain to the pseudo-node.
929 if (nchain->data->ipdata.type == HAMMER2_OBJTYPE_HARDLINK)
933 * Repoint ip->chain if necessary.
935 * (Inode must be locked exclusively by parent)
938 if (ochain != nchain) {
939 hammer2_chain_ref(nchain); /* for ip->chain */
942 hammer2_chain_unlock(ochain);
943 hammer2_chain_drop(ochain); /* for ip->chain */
945 /* replace locked chain in ip (additional lock) */
946 hammer2_chain_lock(nchain, HAMMER2_RESOLVE_ALWAYS);
948 if (ip->pip != pip) {
951 hammer2_inode_ref(pip);
954 hammer2_inode_drop(opip);
959 * Unlink the file from the specified directory inode. The directory inode
960 * does not need to be locked. The caller should pass a non-NULL (ip)
961 * representing the object being removed only if the related vnode is
962 * potentially inactive (not referenced in the caller's active path),
963 * so we can vref/vrele it to trigger the VOP_INACTIVE path and properly
966 * isdir determines whether a directory/non-directory check should be made.
967 * No check is made if isdir is set to -1.
969 * NOTE! This function does not prevent the underlying file from still
970 * being used if it has other refs (such as from an inode, or if it's
971 * chain is manually held). However, the caller is responsible for
972 * fixing up ip->chain if e.g. a rename occurs (see chain_duplicate()).
975 hammer2_unlink_file(hammer2_trans_t *trans, hammer2_inode_t *dip,
976 const uint8_t *name, size_t name_len,
977 int isdir, int *hlinkp)
979 hammer2_inode_data_t *ipdata;
980 hammer2_mount_t *hmp;
981 hammer2_chain_t *parent;
982 hammer2_chain_t *ochain;
983 hammer2_chain_t *chain;
984 hammer2_chain_t *dparent;
985 hammer2_chain_t *dchain;
995 lhc = hammer2_dirhash(name, name_len);
998 * Search for the filename in the directory
1002 parent = hammer2_inode_lock_ex(dip);
1003 chain = hammer2_chain_lookup(&parent,
1004 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1007 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
1008 name_len == chain->data->ipdata.name_len &&
1009 bcmp(name, chain->data->ipdata.filename, name_len) == 0) {
1012 chain = hammer2_chain_next(&parent, chain,
1013 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1016 hammer2_inode_unlock_ex(dip, NULL); /* retain parent */
1019 * Not found or wrong type (isdir < 0 disables the type check).
1020 * If a hardlink pointer, type checks use the hardlink target.
1022 if (chain == NULL) {
1026 if ((type = chain->data->ipdata.type) == HAMMER2_OBJTYPE_HARDLINK) {
1029 type = chain->data->ipdata.target_type;
1032 if (type == HAMMER2_OBJTYPE_DIRECTORY && isdir == 0) {
1036 if (type != HAMMER2_OBJTYPE_DIRECTORY && isdir >= 1) {
1042 * Hardlink must be resolved. We can't hold parent locked while we
1043 * do this or we could deadlock.
1045 * On success chain will be adjusted to point at the hardlink target
1046 * and ochain will point to the hardlink pointer in the original
1047 * directory. Otherwise chain remains pointing to the original.
1049 if (chain->data->ipdata.type == HAMMER2_OBJTYPE_HARDLINK) {
1050 KKASSERT(parent_ref == 0);
1051 hammer2_chain_unlock(parent);
1053 error = hammer2_hardlink_find(dip, &chain, &ochain);
1057 * If this is a directory the directory must be empty. However, if
1058 * isdir < 0 we are doing a rename and the directory does not have
1059 * to be empty, and if isdir > 1 we are deleting a PFS/snapshot
1060 * and the directory does not have to be empty.
1062 * NOTE: We check the full key range here which covers both visible
1063 * and invisible entries. Theoretically there should be no
1064 * invisible (hardlink target) entries if there are no visible
1067 if (type == HAMMER2_OBJTYPE_DIRECTORY && isdir == 1) {
1068 dparent = hammer2_chain_lookup_init(chain, 0);
1069 dchain = hammer2_chain_lookup(&dparent,
1070 0, (hammer2_key_t)-1,
1071 HAMMER2_LOOKUP_NODATA);
1073 hammer2_chain_unlock(dchain);
1074 hammer2_chain_lookup_done(dparent);
1078 hammer2_chain_lookup_done(dparent);
1084 * Ok, we can now unlink the chain. We always decrement nlinks even
1085 * if the entry can be deleted in case someone has the file open and
1088 * The chain itself will no longer be in the on-media topology but
1089 * can still be flushed to the media (e.g. if an open descriptor
1090 * remains). When the last vnode/ip ref goes away the chain will
1091 * be marked unmodified, avoiding any further (now unnecesary) I/O.
1093 * A non-NULL ochain indicates a hardlink.
1097 * Delete the original hardlink pointer.
1099 * NOTE: parent from above is NULL when ochain != NULL
1100 * so we can reuse it.
1102 hammer2_chain_lock(ochain, HAMMER2_RESOLVE_ALWAYS);
1105 parent = ochain->parent;
1106 hammer2_chain_ref(parent);
1107 hammer2_chain_unlock(ochain);
1108 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
1109 hammer2_chain_lock(ochain, HAMMER2_RESOLVE_ALWAYS);
1110 if (ochain->parent == parent)
1112 hammer2_chain_unlock(parent);
1113 hammer2_chain_drop(parent);
1116 hammer2_chain_delete(trans, parent, ochain);
1117 hammer2_chain_unlock(ochain);
1118 hammer2_chain_unlock(parent);
1119 hammer2_chain_drop(parent);
1123 * Then decrement nlinks on hardlink target, deleting
1124 * the target when nlinks drops to 0.
1126 if (chain->data->ipdata.nlinks == 1) {
1127 dparent = chain->parent;
1128 hammer2_chain_ref(chain);
1129 hammer2_chain_unlock(chain);
1130 hammer2_chain_lock(dparent, HAMMER2_RESOLVE_ALWAYS);
1131 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS);
1132 hammer2_chain_drop(chain);
1133 hammer2_chain_modify(trans, &chain, 0);
1134 --chain->data->ipdata.nlinks;
1135 hammer2_chain_delete(trans, dparent, chain);
1136 hammer2_chain_unlock(dparent);
1138 hammer2_chain_modify(trans, &chain, 0);
1139 --chain->data->ipdata.nlinks;
1143 * Otherwise this was not a hardlink and we can just
1144 * remove the entry and decrement nlinks.
1146 * NOTE: *_get() integrates chain's lock into the inode lock.
1148 hammer2_chain_modify(trans, &chain, 0);
1149 ipdata = &chain->data->ipdata;
1151 hammer2_chain_delete(trans, parent, chain);
1157 hammer2_chain_unlock(chain);
1159 hammer2_chain_lookup_done(parent);
1161 hammer2_chain_drop(parent);
1164 hammer2_chain_drop(ochain);
1170 * Calculate the allocation size for the file fragment straddling EOF
1173 hammer2_inode_calc_alloc(hammer2_key_t filesize)
1175 int frag = (int)filesize & HAMMER2_PBUFMASK;
1180 for (radix = HAMMER2_MINALLOCRADIX; frag > (1 << radix); ++radix)
1186 * Given an exclusively locked inode we consolidate its chain for hardlink
1187 * creation, adding (nlinks) to the file's link count and potentially
1188 * relocating the inode to a directory common to ip->pip and tdip.
1190 * Replaces (*chainp) if consolidation occurred, unlocking the old chain
1191 * and returning a new locked chain.
1193 * NOTE! This function will also replace ip->chain.
1196 hammer2_hardlink_consolidate(hammer2_trans_t *trans, hammer2_inode_t *ip,
1197 hammer2_chain_t **chainp,
1198 hammer2_inode_t *tdip, int nlinks)
1200 hammer2_inode_data_t *ipdata;
1201 hammer2_mount_t *hmp;
1202 hammer2_inode_t *fdip;
1203 hammer2_inode_t *cdip;
1204 hammer2_chain_t *chain;
1205 hammer2_chain_t *nchain;
1206 hammer2_chain_t *parent;
1212 if (nlinks == 0 && /* no hardlink needed */
1213 (chain->data->ipdata.name_key & HAMMER2_DIRHASH_VISIBLE)) {
1216 if (hammer2_hardlink_enable < 0) { /* fake hardlinks */
1220 if (hammer2_hardlink_enable == 0) { /* disallow hardlinks */
1221 hammer2_chain_unlock(chain);
1227 * cdip will be returned with a ref, but not locked.
1230 cdip = hammer2_inode_common_parent(fdip, tdip);
1233 * If no change in the hardlink's target directory is required and
1234 * this is already a hardlink target, all we need to do is adjust
1237 * XXX The common parent is a big wiggly due to duplication from
1238 * renames. Compare the core (RBTREE) pointer instead of the
1242 (chain->data->ipdata.name_key & HAMMER2_DIRHASH_VISIBLE) == 0) {
1244 hammer2_chain_modify(trans, &chain, 0);
1245 chain->data->ipdata.nlinks += nlinks;
1252 * We either have to move an existing hardlink target or we have
1253 * to create a fresh hardlink target.
1255 * Hardlink targets are hidden inodes in a parent directory common
1256 * to all directory entries referencing the hardlink.
1258 nchain = hammer2_hardlink_shiftup(trans, &chain, cdip, &error);
1262 * Bump nlinks on duplicated hidden inode, repoint
1265 hammer2_chain_modify(trans, &nchain, 0);
1266 nchain->data->ipdata.nlinks += nlinks;
1267 hammer2_inode_repoint(ip, cdip, nchain);
1270 * If the old chain is not a hardlink target then replace
1271 * it with a OBJTYPE_HARDLINK pointer.
1273 * If the old chain IS a hardlink target then delete it.
1275 if (chain->data->ipdata.name_key & HAMMER2_DIRHASH_VISIBLE) {
1277 * Replace original non-hardlink that's been dup'd
1278 * with a special hardlink directory entry. We must
1279 * set the DIRECTDATA flag to prevent sub-chains
1280 * from trying to synchronize to the inode if the
1281 * file is extended afterwords.
1283 hammer2_chain_modify(trans, &chain, 0);
1284 ipdata = &chain->data->ipdata;
1285 ipdata->target_type = ipdata->type;
1286 ipdata->type = HAMMER2_OBJTYPE_HARDLINK;
1294 bzero(&ipdata->uid, sizeof(ipdata->uid));
1295 bzero(&ipdata->gid, sizeof(ipdata->gid));
1296 ipdata->op_flags = HAMMER2_OPFLAG_DIRECTDATA;
1297 ipdata->cap_flags = 0;
1301 ipdata->iparent = 0; /* XXX */
1302 ipdata->pfs_type = 0;
1303 ipdata->pfs_inum = 0;
1304 bzero(&ipdata->pfs_clid, sizeof(ipdata->pfs_clid));
1305 bzero(&ipdata->pfs_fsid, sizeof(ipdata->pfs_fsid));
1306 ipdata->data_quota = 0;
1307 ipdata->data_count = 0;
1308 ipdata->inode_quota = 0;
1309 ipdata->inode_count = 0;
1310 ipdata->attr_tid = 0;
1311 ipdata->dirent_tid = 0;
1312 bzero(&ipdata->u, sizeof(ipdata->u));
1313 /* XXX transaction ids */
1315 kprintf("DELETE INVISIBLE\n");
1317 parent = chain->parent;
1318 hammer2_chain_ref(parent);
1319 hammer2_chain_ref(chain);
1320 hammer2_chain_unlock(chain);
1321 hammer2_chain_lock(parent,
1322 HAMMER2_RESOLVE_ALWAYS);
1323 hammer2_chain_lock(chain,
1324 HAMMER2_RESOLVE_ALWAYS);
1325 hammer2_chain_drop(chain);
1326 if (chain->parent == parent)
1328 hammer2_chain_unlock(parent);
1329 hammer2_chain_drop(parent);
1331 hammer2_chain_delete(trans, parent, chain);
1332 hammer2_chain_unlock(parent);
1333 hammer2_chain_drop(parent);
1337 * Return the new chain.
1339 hammer2_chain_unlock(chain);
1345 hammer2_chain_unlock(chain);
1350 * Cleanup, chain/nchain already dealt with.
1354 hammer2_inode_drop(cdip);
1360 * If (*ochainp) is non-NULL it points to the forward OBJTYPE_HARDLINK
1361 * inode while (*chainp) points to the resolved (hidden hardlink
1362 * target) inode. In this situation when nlinks is 1 we wish to
1363 * deconsolidate the hardlink, moving it back to the directory that now
1364 * represents the only remaining link.
1367 hammer2_hardlink_deconsolidate(hammer2_trans_t *trans,
1368 hammer2_inode_t *dip,
1369 hammer2_chain_t **chainp,
1370 hammer2_chain_t **ochainp)
1372 if (*ochainp == NULL)
1379 * The caller presents a locked *chainp pointing to a HAMMER2_BREF_TYPE_INODE
1380 * with an obj_type of HAMMER2_OBJTYPE_HARDLINK. This routine will gobble
1381 * the *chainp and return a new locked *chainp representing the file target
1382 * (the original *chainp will be unlocked).
1384 * When a match is found the chain representing the original HARDLINK
1385 * will be returned in *ochainp with a ref, but not locked.
1387 * When no match is found *chainp is set to NULL and EIO is returned.
1388 * (*ochainp) will still be set to the original chain with a ref but not
1392 hammer2_hardlink_find(hammer2_inode_t *dip, hammer2_chain_t **chainp,
1393 hammer2_chain_t **ochainp)
1395 hammer2_chain_t *chain = *chainp;
1396 hammer2_chain_t *parent;
1397 hammer2_inode_t *ip;
1398 hammer2_inode_t *pip;
1402 hammer2_inode_ref(pip); /* for loop */
1403 hammer2_chain_ref(chain); /* for (*ochainp) */
1407 * Locate the hardlink. pip is referenced and not locked,
1412 lhc = chain->data->ipdata.inum;
1413 hammer2_chain_unlock(chain);
1416 while ((ip = pip) != NULL) {
1417 parent = hammer2_inode_lock_ex(ip);
1418 hammer2_inode_drop(ip); /* loop */
1419 KKASSERT(parent->bref.type == HAMMER2_BREF_TYPE_INODE);
1420 chain = hammer2_chain_lookup(&parent, lhc, lhc, 0);
1421 hammer2_chain_lookup_done(parent); /* discard parent */
1424 pip = ip->pip; /* safe, ip held locked */
1426 hammer2_inode_ref(pip); /* loop */
1427 hammer2_inode_unlock_ex(ip, NULL);
1431 * chain is locked, ip is locked. Unlock ip, return the locked
1432 * chain. *ipp is already set w/a ref count and not locked.
1434 * (parent is already unlocked).
1437 hammer2_inode_unlock_ex(ip, NULL);
1440 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_INODE);
1441 /* already locked */
1449 * Find the directory common to both fdip and tdip, hold and return
1453 hammer2_inode_common_parent(hammer2_inode_t *fdip, hammer2_inode_t *tdip)
1455 hammer2_inode_t *scan1;
1456 hammer2_inode_t *scan2;
1459 * We used to have a depth field but it complicated matters too
1460 * much for directory renames. So now its ugly. Check for
1461 * simple cases before giving up and doing it the expensive way.
1463 * XXX need a bottom-up topology stability lock
1465 if (fdip == tdip || fdip == tdip->pip) {
1466 hammer2_inode_ref(fdip);
1469 if (fdip->pip == tdip) {
1470 hammer2_inode_ref(tdip);
1477 for (scan1 = fdip; scan1->pmp == fdip->pmp; scan1 = scan1->pip) {
1479 while (scan2->pmp == tdip->pmp) {
1480 if (scan1 == scan2) {
1481 hammer2_inode_ref(scan1);
1489 panic("hammer2_inode_common_parent: no common parent %p %p\n",