2 * Copyright (c) 2011-2012 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>
45 * Adding a ref to an inode is only legal if the inode already has at least
49 hammer2_inode_ref(hammer2_inode_t *ip)
51 hammer2_chain_ref(ip->hmp, &ip->chain);
55 * Drop an inode reference, freeing the inode when the last reference goes
59 hammer2_inode_drop(hammer2_inode_t *ip)
61 hammer2_chain_drop(ip->hmp, &ip->chain);
65 * Get the vnode associated with the given inode, allocating the vnode if
66 * necessary. The vnode will be returned exclusively locked.
68 * The caller must lock the inode (shared or exclusive).
70 * Great care must be taken to avoid deadlocks and vnode acquisition/reclaim
74 hammer2_igetv(hammer2_inode_t *ip, int *errorp)
77 hammer2_pfsmount_t *pmp;
81 KKASSERT(pmp != NULL);
86 * Attempt to reuse an existing vnode assignment. It is
87 * possible to race a reclaim so the vget() may fail. The
88 * inode must be unlocked during the vget() to avoid a
89 * deadlock against a reclaim.
94 * Inode must be unlocked during the vget() to avoid
95 * possible deadlocks, vnode is held to prevent
96 * destruction during the vget(). The vget() can
97 * still fail if we lost a reclaim race on the vnode.
99 vhold_interlocked(vp);
100 ccms_thread_unlock(&ip->chain.cst);
101 if (vget(vp, LK_EXCLUSIVE)) {
103 ccms_thread_lock(&ip->chain.cst,
104 CCMS_STATE_EXCLUSIVE);
107 ccms_thread_lock(&ip->chain.cst, CCMS_STATE_EXCLUSIVE);
109 /* vp still locked and ref from vget */
115 * No vnode exists, allocate a new vnode. Beware of
116 * allocation races. This function will return an
117 * exclusively locked and referenced vnode.
119 *errorp = getnewvnode(VT_HAMMER2, pmp->mp, &vp, 0, 0);
126 * Lock the inode and check for an allocation race.
128 ostate = ccms_thread_lock_upgrade(&ip->chain.cst);
129 if (ip->vp != NULL) {
132 ccms_thread_lock_restore(&ip->chain.cst, ostate);
136 switch (ip->ip_data.type) {
137 case HAMMER2_OBJTYPE_DIRECTORY:
140 case HAMMER2_OBJTYPE_REGFILE:
142 vinitvmio(vp, ip->ip_data.size,
144 (int)ip->ip_data.size & HAMMER2_LBUFMASK);
146 case HAMMER2_OBJTYPE_SOFTLINK:
148 * XXX for now we are using the generic file_read
149 * and file_write code so we need a buffer cache
153 vinitvmio(vp, ip->ip_data.size,
155 (int)ip->ip_data.size & HAMMER2_LBUFMASK);
159 panic("hammer2: unhandled objtype %d",
164 if (ip == pmp->iroot)
165 vsetflags(vp, VROOT);
169 hammer2_chain_ref(ip->hmp, &ip->chain); /* vp association */
170 ccms_thread_lock_restore(&ip->chain.cst, ostate);
175 * Return non-NULL vp and *errorp == 0, or NULL vp and *errorp != 0.
177 if (hammer2_debug & 0x0002) {
178 kprintf("igetv vp %p refs %d aux %d\n",
179 vp, vp->v_sysref.refcnt, vp->v_auxrefs);
185 * Create a new inode in the specified directory using the vattr to
186 * figure out the type of inode.
188 * If no error occurs the new inode with its chain locked is returned in
189 * *nipp, otherwise an error is returned and *nipp is set to NULL.
191 * If vap and/or cred are NULL the related fields are not set and the
192 * inode type defaults to a directory. This is used when creating PFSs
193 * under the super-root, so the inode number is set to 1 in this case.
196 hammer2_inode_create(hammer2_inode_t *dip,
197 struct vattr *vap, struct ucred *cred,
198 const uint8_t *name, size_t name_len,
199 hammer2_inode_t **nipp)
201 hammer2_mount_t *hmp = dip->hmp;
202 hammer2_chain_t *chain;
203 hammer2_chain_t *parent;
204 hammer2_inode_t *nip;
209 lhc = hammer2_dirhash(name, name_len);
212 * Locate the inode or indirect block to create the new
213 * entry in. At the same time check for key collisions
214 * and iterate until we don't get one.
216 parent = &dip->chain;
217 hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
221 chain = hammer2_chain_lookup(hmp, &parent, lhc, lhc, 0);
224 if ((lhc & HAMMER2_DIRHASH_VISIBLE) == 0)
226 if ((lhc & HAMMER2_DIRHASH_LOMASK) == HAMMER2_DIRHASH_LOMASK)
228 hammer2_chain_unlock(hmp, chain);
233 chain = hammer2_chain_create(hmp, parent, NULL, lhc, 0,
234 HAMMER2_BREF_TYPE_INODE,
235 HAMMER2_INODE_BYTES);
239 hammer2_chain_unlock(hmp, parent);
242 * Handle the error case
245 KKASSERT(chain == NULL);
251 * Set up the new inode
256 hammer2_voldata_lock(hmp);
258 nip->ip_data.type = hammer2_get_obj_type(vap->va_type);
259 nip->ip_data.inum = hmp->voldata.alloc_tid++;
260 /* XXX modify/lock */
262 nip->ip_data.type = HAMMER2_OBJTYPE_DIRECTORY;
263 nip->ip_data.inum = 1;
265 hammer2_voldata_unlock(hmp);
266 nip->ip_data.version = HAMMER2_INODE_VERSION_ONE;
267 hammer2_update_time(&nip->ip_data.ctime);
268 nip->ip_data.mtime = nip->ip_data.ctime;
270 nip->ip_data.mode = vap->va_mode;
271 nip->ip_data.nlinks = 1;
274 xuid = hammer2_to_unix_xid(&dip->ip_data.uid);
275 xuid = vop_helper_create_uid(dip->pmp->mp,
283 if (vap->va_vaflags & VA_UID_UUID_VALID)
284 nip->ip_data.uid = vap->va_uid_uuid;
285 else if (vap->va_uid != (uid_t)VNOVAL)
286 hammer2_guid_to_uuid(&nip->ip_data.uid, vap->va_uid);
288 hammer2_guid_to_uuid(&nip->ip_data.uid, xuid);
290 if (vap->va_vaflags & VA_GID_UUID_VALID)
291 nip->ip_data.gid = vap->va_gid_uuid;
292 else if (vap->va_gid != (gid_t)VNOVAL)
293 hammer2_guid_to_uuid(&nip->ip_data.gid, vap->va_gid);
295 nip->ip_data.gid = dip->ip_data.gid;
299 * Regular files and softlinks allow a small amount of data to be
300 * directly embedded in the inode. This flag will be cleared if
301 * the size is extended past the embedded limit.
303 if (nip->ip_data.type == HAMMER2_OBJTYPE_REGFILE ||
304 nip->ip_data.type == HAMMER2_OBJTYPE_SOFTLINK) {
305 nip->ip_data.op_flags |= HAMMER2_OPFLAG_DIRECTDATA;
308 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
309 bcopy(name, nip->ip_data.filename, name_len);
310 nip->ip_data.name_key = lhc;
311 nip->ip_data.name_len = name_len;
317 * Duplicate the specified existing inode in the specified target directory.
318 * If name is NULL the inode is duplicated as a hidden directory entry.
320 * Returns the new inode. The old inode is left alone.
322 * XXX name needs to be NULL for now.
325 hammer2_inode_duplicate(hammer2_inode_t *dip, hammer2_inode_t *oip,
326 hammer2_inode_t **nipp,
327 const uint8_t *name, size_t name_len)
329 hammer2_mount_t *hmp = dip->hmp;
330 hammer2_inode_t *nip;
331 hammer2_chain_t *parent;
332 hammer2_chain_t *chain;
337 lhc = hammer2_dirhash(name, name_len);
339 lhc = oip->ip_data.inum;
340 KKASSERT((lhc & HAMMER2_DIRHASH_VISIBLE) == 0);
344 * Locate the inode or indirect block to create the new
345 * entry in. At the same time check for key collisions
346 * and iterate until we don't get one.
349 parent = &dip->chain;
350 hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
354 chain = hammer2_chain_lookup(hmp, &parent, lhc, lhc, 0);
357 /* XXX bcmp name if not NULL */
358 if ((lhc & HAMMER2_DIRHASH_LOMASK) == HAMMER2_DIRHASH_LOMASK)
360 if ((lhc & HAMMER2_DIRHASH_VISIBLE) == 0) /* shouldn't happen */
362 hammer2_chain_unlock(hmp, chain);
368 * Create entry in common parent directory.
371 chain = hammer2_chain_create(hmp, parent, NULL, lhc, 0,
372 HAMMER2_BREF_TYPE_INODE /* n/a */,
373 HAMMER2_INODE_BYTES); /* n/a */
377 hammer2_chain_unlock(hmp, parent);
380 * Handle the error case
383 KKASSERT(chain == NULL);
388 * XXX This is currently a horrible hack. Well, if we wanted to
389 * duplicate a file, i.e. as in a snapshot, we definitely
390 * would have to flush it first.
392 * For hardlink target generation we can theoretically move any
393 * active chain structures without flushing, but that gets really
394 * iffy for code which follows chain->parent and ip->pip links.
396 * XXX only works with files. Duplicating a directory hierarchy
397 * requires a flush but doesn't deal with races post-flush.
398 * Well, it would work I guess, but you might catch some files
401 * We cannot leave oip with any in-memory chains because (for a
402 * hardlink), oip will become a OBJTYPE_HARDLINK which is just a
403 * pointer to the real hardlink's inum and can't have any sub-chains.
404 * XXX might be 0-ref chains left.
406 hammer2_inode_lock_ex(oip);
407 hammer2_chain_flush(hmp, &oip->chain, 0);
408 hammer2_inode_unlock_ex(oip);
409 /*KKASSERT(RB_EMPTY(&oip->chain.rbhead));*/
412 hammer2_chain_modify(hmp, chain, 0);
413 nip->ip_data = oip->ip_data; /* sync media data after flush */
417 * Directory entries are inodes so if the name has changed
418 * we have to update the inode.
420 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
421 bcopy(name, nip->ip_data.filename, name_len);
422 nip->ip_data.name_key = lhc;
423 nip->ip_data.name_len = name_len;
426 * Directory entries are inodes but this is a hidden hardlink
427 * target. The name isn't used but to ease debugging give it
428 * a name after its inode number.
430 ksnprintf(nip->ip_data.filename, sizeof(nip->ip_data.filename),
431 "0x%016jx", (intmax_t)nip->ip_data.inum);
432 nip->ip_data.name_len = strlen(nip->ip_data.filename);
433 nip->ip_data.name_key = lhc;
442 * Connect inode (oip) to the specified directory using the specified name.
443 * (oip) must be locked.
445 * If (oip) is not currently connected we simply connect it up.
447 * If (oip) is already connected we create a OBJTYPE_HARDLINK entry which
448 * points to (oip)'s inode number. (oip) is expected to be the terminus of
449 * the hardlink sitting as a hidden file in a common parent directory
450 * in this situation (thus the lock order is correct).
453 hammer2_inode_connect(hammer2_inode_t *dip, hammer2_inode_t *oip,
454 const uint8_t *name, size_t name_len)
456 hammer2_mount_t *hmp = dip->hmp;
457 hammer2_chain_t *chain;
458 hammer2_chain_t *parent;
459 hammer2_inode_t *nip;
464 lhc = hammer2_dirhash(name, name_len);
465 hlink = (oip->chain.parent != NULL);
468 * In fake mode flush oip so we can just snapshot it downbelow.
470 if (hlink && hammer2_hardlink_enable < 0)
471 hammer2_chain_flush(hmp, &oip->chain, 0);
474 * Locate the inode or indirect block to create the new
475 * entry in. At the same time check for key collisions
476 * and iterate until we don't get one.
478 parent = &dip->chain;
479 hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
483 chain = hammer2_chain_lookup(hmp, &parent, lhc, lhc, 0);
486 if ((lhc & HAMMER2_DIRHASH_LOMASK) == HAMMER2_DIRHASH_LOMASK)
488 hammer2_chain_unlock(hmp, chain);
494 * Passing a non-NULL chain to hammer2_chain_create() reconnects the
495 * existing chain instead of creating a new one. The chain's bref
496 * will be properly updated.
500 chain = hammer2_chain_create(hmp, parent,
502 HAMMER2_BREF_TYPE_INODE,
503 HAMMER2_INODE_BYTES);
505 chain = hammer2_chain_create(hmp, parent,
507 HAMMER2_BREF_TYPE_INODE,
508 HAMMER2_INODE_BYTES);
510 KKASSERT(chain == &oip->chain);
515 hammer2_chain_unlock(hmp, parent);
518 * Handle the error case
521 KKASSERT(chain == NULL);
526 * Directory entries are inodes so if the name has changed we have
527 * to update the inode.
529 * When creating an OBJTYPE_HARDLINK entry remember to unlock the
530 * chain, the caller will access the hardlink via the actual hardlink
531 * target file and not the hardlink pointer entry.
533 if (hlink && hammer2_hardlink_enable >= 0) {
535 * Create the HARDLINK pointer. oip represents the hardlink
536 * target in this situation.
539 hammer2_chain_modify(hmp, chain, 0);
540 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
541 bcopy(name, nip->ip_data.filename, name_len);
542 nip->ip_data.name_key = lhc;
543 nip->ip_data.name_len = name_len;
544 nip->ip_data.target_type = oip->ip_data.type;
545 nip->ip_data.type = HAMMER2_OBJTYPE_HARDLINK;
546 nip->ip_data.inum = oip->ip_data.inum;
547 nip->ip_data.nlinks = 1;
548 kprintf("created hardlink %*.*s\n",
549 (int)name_len, (int)name_len, name);
550 hammer2_chain_unlock(hmp, chain);
551 } else if (hlink && hammer2_hardlink_enable < 0) {
553 * Create a snapshot (hardlink fake mode for debugging).
556 nip->ip_data = oip->ip_data;
557 hammer2_chain_modify(hmp, chain, 0);
558 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
559 bcopy(name, nip->ip_data.filename, name_len);
560 nip->ip_data.name_key = lhc;
561 nip->ip_data.name_len = name_len;
562 kprintf("created fake hardlink %*.*s\n",
563 (int)name_len, (int)name_len, name);
564 hammer2_chain_unlock(hmp, chain);
567 * Normally disconnected inode (e.g. during a rename) that
568 * was reconnected. We must fixup the name stored in
571 * We are using oip as chain, already locked by caller,
574 hammer2_chain_modify(hmp, chain, 0);
575 if (oip->ip_data.name_len != name_len ||
576 bcmp(oip->ip_data.filename, name, name_len) != 0) {
577 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
578 bcopy(name, oip->ip_data.filename, name_len);
579 oip->ip_data.name_key = lhc;
580 oip->ip_data.name_len = name_len;
582 oip->ip_data.nlinks = 1;
589 * Unlink the file from the specified directory inode. The directory inode
590 * does not need to be locked.
592 * isdir determines whether a directory/non-directory check should be made.
593 * No check is made if isdir is set to -1.
596 hammer2_unlink_file(hammer2_inode_t *dip,
597 const uint8_t *name, size_t name_len,
598 int isdir, hammer2_inode_t *retain_ip)
600 hammer2_mount_t *hmp;
601 hammer2_chain_t *parent;
602 hammer2_chain_t *chain;
603 hammer2_chain_t *dparent;
604 hammer2_chain_t *dchain;
607 hammer2_inode_t *oip;
614 lhc = hammer2_dirhash(name, name_len);
617 * Search for the filename in the directory
619 parent = &dip->chain;
620 hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
621 chain = hammer2_chain_lookup(hmp, &parent,
622 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
625 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
627 name_len == chain->data->ipdata.name_len &&
628 bcmp(name, chain->data->ipdata.filename, name_len) == 0) {
631 chain = hammer2_chain_next(hmp, &parent, chain,
632 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
637 * Not found or wrong type (isdir < 0 disables the type check).
640 hammer2_chain_unlock(hmp, parent);
643 if ((type = chain->data->ipdata.type) == HAMMER2_OBJTYPE_HARDLINK)
644 type = chain->data->ipdata.target_type;
646 if (type == HAMMER2_OBJTYPE_DIRECTORY && isdir == 0) {
650 if (type != HAMMER2_OBJTYPE_DIRECTORY && isdir == 1) {
656 * Hardlink must be resolved. We can't hold parent locked while we
657 * do this or we could deadlock.
659 if (chain->data->ipdata.type == HAMMER2_OBJTYPE_HARDLINK) {
660 hammer2_chain_unlock(hmp, parent);
662 error = hammer2_hardlink_find(dip, &chain, &oip);
666 * If this is a directory the directory must be empty. However, if
667 * isdir < 0 we are doing a rename and the directory does not have
670 * NOTE: We check the full key range here which covers both visible
671 * and invisible entries. Theoretically there should be no
672 * invisible (hardlink target) entries if there are no visible
675 if (type == HAMMER2_OBJTYPE_DIRECTORY && isdir >= 0) {
677 hammer2_chain_lock(hmp, dparent, HAMMER2_RESOLVE_ALWAYS);
678 dchain = hammer2_chain_lookup(hmp, &dparent,
679 0, (hammer2_key_t)-1,
680 HAMMER2_LOOKUP_NODATA);
682 hammer2_chain_unlock(hmp, dchain);
683 hammer2_chain_unlock(hmp, dparent);
687 hammer2_chain_unlock(hmp, dparent);
693 * Ok, we can now unlink the chain. We always decrement nlinks even
694 * if the entry can be deleted in case someone has the file open and
697 * The chain itself will no longer be in the on-media topology but
698 * can still be flushed to the media (e.g. if an open descriptor
699 * remains). When the last vnode/ip ref goes away the chain will
700 * be marked unmodified, avoiding any further (now unnecesary) I/O.
704 * If this was a hardlink we first delete the hardlink
707 parent = oip->chain.parent;
708 hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
709 hammer2_chain_lock(hmp, &oip->chain, HAMMER2_RESOLVE_ALWAYS);
710 hammer2_chain_delete(hmp, parent, &oip->chain,
712 hammer2_chain_unlock(hmp, &oip->chain);
713 hammer2_chain_unlock(hmp, parent);
717 * Then decrement nlinks on hardlink target.
720 if (ip->ip_data.nlinks == 1) {
721 dparent = chain->parent;
722 hammer2_chain_ref(hmp, chain);
723 hammer2_chain_unlock(hmp, chain);
724 hammer2_chain_lock(hmp, dparent,
725 HAMMER2_RESOLVE_ALWAYS);
726 hammer2_chain_lock(hmp, chain, HAMMER2_RESOLVE_ALWAYS);
727 hammer2_chain_drop(hmp, chain);
728 hammer2_chain_modify(hmp, chain, 0);
729 --ip->ip_data.nlinks;
730 hammer2_chain_delete(hmp, dparent, chain, 0);
731 hammer2_chain_unlock(hmp, dparent);
733 hammer2_chain_modify(hmp, chain, 0);
734 --ip->ip_data.nlinks;
738 * Otherwise this was not a hardlink and we can just
739 * remove the entry and decrement nlinks.
742 hammer2_chain_modify(hmp, chain, 0);
743 --ip->ip_data.nlinks;
744 hammer2_chain_delete(hmp, parent, chain,
752 hammer2_chain_unlock(hmp, chain);
754 hammer2_chain_unlock(hmp, parent);
756 hammer2_chain_drop(oip->hmp, &oip->chain);
762 * Calculate the allocation size for the file fragment straddling EOF
765 hammer2_inode_calc_alloc(hammer2_key_t filesize)
767 int frag = (int)filesize & HAMMER2_PBUFMASK;
772 for (radix = HAMMER2_MINALLOCRADIX; frag > (1 << radix); ++radix)
778 hammer2_inode_lock_nlinks(hammer2_inode_t *ip)
780 hammer2_chain_ref(ip->hmp, &ip->chain);
784 hammer2_inode_unlock_nlinks(hammer2_inode_t *ip)
786 hammer2_chain_drop(ip->hmp, &ip->chain);
790 * Consolidate for hard link creation. This moves the specified terminal
791 * hardlink inode to a directory common to its current directory and tdip
792 * if necessary, replacing *ipp with the new inode chain element and
793 * modifying the original inode chain element to OBJTYPE_HARDLINK.
795 * If the original inode chain element was a prior incarnation of a hidden
796 * inode it can simply be deleted instead of converted.
798 * (*ipp)'s nlinks field is locked on entry and the new (*ipp)'s nlinks
799 * field will be locked on return (with the original's unlocked).
801 * The link count is bumped if requested.
804 hammer2_hardlink_consolidate(hammer2_inode_t **ipp, hammer2_inode_t *tdip)
806 hammer2_mount_t *hmp;
807 hammer2_inode_t *oip = *ipp;
808 hammer2_inode_t *nip = NULL;
809 hammer2_inode_t *fdip;
810 hammer2_chain_t *parent;
815 if (hammer2_hardlink_enable < 0)
817 if (hammer2_hardlink_enable == 0)
821 * Find the common parent directory
824 while (fdip->depth > tdip->depth) {
826 KKASSERT(fdip != NULL);
828 while (tdip->depth > fdip->depth) {
830 KKASSERT(tdip != NULL);
832 while (fdip != tdip) {
835 KKASSERT(fdip != NULL);
836 KKASSERT(tdip != NULL);
840 * Nothing to do (except bump the link count) if the hardlink has
841 * already been consolidated in the correct place.
843 if (oip->pip == fdip &&
844 (oip->ip_data.name_key & HAMMER2_DIRHASH_VISIBLE) == 0) {
845 kprintf("hardlink already consolidated correctly\n");
847 hammer2_inode_lock_ex(nip);
848 hammer2_chain_modify(hmp, &nip->chain, 0);
849 ++nip->ip_data.nlinks;
850 hammer2_inode_unlock_ex(nip);
855 * Create a hidden inode directory entry in the parent, copying
856 * (*oip)'s state. Then replace oip with OBJTYPE_HARDLINK.
858 * The duplication function will either flush or move any chains
859 * under oip to the new hardlink target inode, retiring all chains
860 * related to oip before returning. XXX vp->ip races.
862 error = hammer2_inode_duplicate(fdip, oip, &nip, NULL, 0);
865 * Bump nlinks on duplicated hidden inode.
867 kprintf("hardlink consolidation success in parent dir %s\n",
868 fdip->ip_data.filename);
869 hammer2_inode_lock_nlinks(nip);
870 hammer2_inode_unlock_nlinks(oip);
871 hammer2_chain_modify(hmp, &nip->chain, 0);
872 ++nip->ip_data.nlinks;
873 hammer2_inode_unlock_ex(nip);
875 if (oip->ip_data.name_key & HAMMER2_DIRHASH_VISIBLE) {
877 * Replace the old inode with an OBJTYPE_HARDLINK
880 hammer2_inode_lock_ex(oip);
881 hammer2_chain_modify(hmp, &oip->chain, 0);
882 oip->ip_data.target_type = oip->ip_data.type;
883 oip->ip_data.type = HAMMER2_OBJTYPE_HARDLINK;
884 oip->ip_data.uflags = 0;
885 oip->ip_data.rmajor = 0;
886 oip->ip_data.rminor = 0;
887 oip->ip_data.ctime = 0;
888 oip->ip_data.mtime = 0;
889 oip->ip_data.atime = 0;
890 oip->ip_data.btime = 0;
891 bzero(&oip->ip_data.uid, sizeof(oip->ip_data.uid));
892 bzero(&oip->ip_data.gid, sizeof(oip->ip_data.gid));
893 oip->ip_data.op_flags = HAMMER2_OPFLAG_DIRECTDATA;
894 oip->ip_data.cap_flags = 0;
895 oip->ip_data.mode = 0;
896 oip->ip_data.size = 0;
897 oip->ip_data.nlinks = 1;
898 oip->ip_data.iparent = 0; /* XXX */
899 oip->ip_data.pfs_type = 0;
900 oip->ip_data.pfs_inum = 0;
901 bzero(&oip->ip_data.pfs_id,
902 sizeof(oip->ip_data.pfs_id));
903 bzero(&oip->ip_data.pfs_fsid,
904 sizeof(oip->ip_data.pfs_fsid));
905 oip->ip_data.data_quota = 0;
906 oip->ip_data.data_count = 0;
907 oip->ip_data.inode_quota = 0;
908 oip->ip_data.inode_count = 0;
909 oip->ip_data.attr_tid = 0;
910 oip->ip_data.dirent_tid = 0;
911 bzero(&oip->ip_data.u, sizeof(oip->ip_data.u));
912 /* XXX transaction ids */
914 hammer2_inode_unlock_ex(oip);
917 * The old inode was a hardlink target, which we
918 * have now moved. We must delete it so the new
919 * hardlink target at a higher directory level
920 * becomes the only hardlink target for this inode.
922 kprintf("DELETE INVISIBLE\n");
923 parent = oip->chain.parent;
924 hammer2_chain_lock(hmp, parent,
925 HAMMER2_RESOLVE_ALWAYS);
926 hammer2_chain_lock(hmp, &oip->chain,
927 HAMMER2_RESOLVE_ALWAYS);
928 hammer2_chain_delete(hmp, parent, &oip->chain, 0);
929 hammer2_chain_unlock(hmp, &oip->chain);
930 hammer2_chain_unlock(hmp, parent);
934 KKASSERT(nip == NULL);
941 * If (*ipp) is non-NULL it points to the forward OBJTYPE_HARDLINK inode while
942 * (*chainp) points to the resolved (hidden hardlink target) inode. In this
943 * situation when nlinks is 1 we wish to deconsolidate the hardlink, moving
944 * it back to the directory that now represents the only remaining link.
947 hammer2_hardlink_deconsolidate(hammer2_inode_t *dip, hammer2_chain_t **chainp,
948 hammer2_inode_t **ipp)
957 * When presented with a (*chainp) representing an inode of type
958 * OBJTYPE_HARDLINK this code will save the original inode (with a ref)
959 * in (*ipp), and then locate the hidden hardlink target in (dip) or
960 * any parent directory above (dip). The locked (*chainp) is replaced
961 * with a new locked (*chainp) representing the hardlink target.
964 hammer2_hardlink_find(hammer2_inode_t *dip, hammer2_chain_t **chainp,
965 hammer2_inode_t **ipp)
967 hammer2_mount_t *hmp = dip->hmp;
968 hammer2_chain_t *chain = *chainp;
969 hammer2_chain_t *parent;
970 hammer2_inode_t *pip;
974 hammer2_inode_ref(chain->u.ip);
975 lhc = chain->u.ip->ip_data.inum;
977 hammer2_inode_unlock_ex(chain->u.ip);
978 pip = chain->u.ip->pip;
982 parent = &pip->chain;
983 KKASSERT(parent->bref.type == HAMMER2_BREF_TYPE_INODE);
985 hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
986 chain = hammer2_chain_lookup(hmp, &parent, lhc, lhc, 0);
987 hammer2_chain_unlock(hmp, parent);
994 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_INODE);