hammer2 - GSOC cleanup pass
[dragonfly.git] / sys / vfs / hammer2 / hammer2_inode.c
1 /*
2  * Copyright (c) 2011-2013 The DragonFly Project.  All rights reserved.
3  *
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>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
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
17  *    distribution.
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.
21  *
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
33  * SUCH DAMAGE.
34  */
35 #include <sys/cdefs.h>
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/types.h>
39 #include <sys/lock.h>
40 #include <sys/uuid.h>
41
42 #include "hammer2.h"
43
44 RB_GENERATE2(hammer2_inode_tree, hammer2_inode, rbnode, hammer2_inode_cmp,
45              hammer2_tid_t, inum);
46
47 int
48 hammer2_inode_cmp(hammer2_inode_t *ip1, hammer2_inode_t *ip2)
49 {
50         if (ip1->inum < ip2->inum)
51                 return(-1);
52         if (ip1->inum > ip2->inum)
53                 return(1);
54         return(0);
55 }
56
57 /*
58  * HAMMER2 inode locks
59  *
60  * HAMMER2 offers shared locks and exclusive locks on inodes.
61  *
62  * An inode's ip->chain pointer is resolved and stable while an inode is
63  * locked, and can be cleaned out at any time (become NULL) when an inode
64  * is not locked.
65  *
66  * The underlying chain is also locked and returned.
67  *
68  * NOTE: We don't combine the inode/chain lock because putting away an
69  *       inode would otherwise confuse multiple lock holders of the inode.
70  */
71 hammer2_chain_t *
72 hammer2_inode_lock_ex(hammer2_inode_t *ip)
73 {
74         hammer2_chain_t *chain;
75
76         hammer2_inode_ref(ip);
77         ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
78
79         /*
80          * ip->chain fixup.  Certain duplications used to move inodes
81          * into indirect blocks (for example) can cause ip->chain to
82          * become stale.
83          */
84 again:
85         chain = ip->chain;
86         spin_lock(&chain->core->cst.spin);
87         if (hammer2_chain_refactor_test(chain, 1)) {
88                 while (hammer2_chain_refactor_test(chain, 1))
89                         chain = chain->next_parent;
90                 if (ip->chain != chain) {
91                         hammer2_chain_ref(chain);
92                         spin_unlock(&chain->core->cst.spin);
93                         hammer2_inode_repoint(ip, NULL, chain);
94                         hammer2_chain_drop(chain);
95                 } else {
96                         spin_unlock(&chain->core->cst.spin);
97                 }
98         } else {
99                 spin_unlock(&chain->core->cst.spin);
100         }
101
102         KKASSERT(chain != NULL);        /* for now */
103         hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS);
104
105         /*
106          * Resolve duplication races
107          */
108         if (hammer2_chain_refactor_test(chain, 1)) {
109                 hammer2_chain_unlock(chain);
110                 goto again;
111         }
112         return (chain);
113 }
114
115 void
116 hammer2_inode_unlock_ex(hammer2_inode_t *ip, hammer2_chain_t *chain)
117 {
118         /*
119          * XXX this will catch parent directories too which we don't
120          *     really want.
121          */
122         if (chain)
123                 hammer2_chain_unlock(chain);
124         ccms_thread_unlock(&ip->topo_cst);
125         hammer2_inode_drop(ip);
126 }
127
128 /*
129  * NOTE: We don't combine the inode/chain lock because putting away an
130  *       inode would otherwise confuse multiple lock holders of the inode.
131  *
132  *       Shared locks are especially sensitive to having too many shared
133  *       lock counts (from the same thread) on certain paths which might
134  *       need to upgrade them.  Only one count of a shared lock can be
135  *       upgraded.
136  */
137 hammer2_chain_t *
138 hammer2_inode_lock_sh(hammer2_inode_t *ip)
139 {
140         hammer2_chain_t *chain;
141
142         hammer2_inode_ref(ip);
143 again:
144         ccms_thread_lock(&ip->topo_cst, CCMS_STATE_SHARED);
145
146         chain = ip->chain;
147         KKASSERT(chain != NULL);        /* for now */
148         hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS |
149                                   HAMMER2_RESOLVE_SHARED);
150
151         /*
152          * Resolve duplication races
153          */
154         if (hammer2_chain_refactor_test(chain, 1)) {
155                 hammer2_chain_unlock(chain);
156                 ccms_thread_unlock(&ip->topo_cst);
157                 chain = hammer2_inode_lock_ex(ip);
158                 hammer2_inode_unlock_ex(ip, chain);
159                 goto again;
160         }
161         return (chain);
162 }
163
164 void
165 hammer2_inode_unlock_sh(hammer2_inode_t *ip, hammer2_chain_t *chain)
166 {
167         if (chain)
168                 hammer2_chain_unlock(chain);
169         ccms_thread_unlock(&ip->topo_cst);
170         hammer2_inode_drop(ip);
171 }
172
173 ccms_state_t
174 hammer2_inode_lock_temp_release(hammer2_inode_t *ip)
175 {
176         return(ccms_thread_lock_temp_release(&ip->topo_cst));
177 }
178
179 void
180 hammer2_inode_lock_temp_restore(hammer2_inode_t *ip, ccms_state_t ostate)
181 {
182         ccms_thread_lock_temp_restore(&ip->topo_cst, ostate);
183 }
184
185 ccms_state_t
186 hammer2_inode_lock_upgrade(hammer2_inode_t *ip)
187 {
188         return(ccms_thread_lock_upgrade(&ip->topo_cst));
189 }
190
191 void
192 hammer2_inode_lock_downgrade(hammer2_inode_t *ip, ccms_state_t ostate)
193 {
194         ccms_thread_lock_downgrade(&ip->topo_cst, ostate);
195 }
196
197 /*
198  * Lookup an inode by inode number
199  */
200 hammer2_inode_t *
201 hammer2_inode_lookup(hammer2_pfsmount_t *pmp, hammer2_tid_t inum)
202 {
203         hammer2_inode_t *ip;
204
205         if (pmp) {
206                 spin_lock(&pmp->inum_spin);
207                 ip = RB_LOOKUP(hammer2_inode_tree, &pmp->inum_tree, inum);
208                 if (ip)
209                         hammer2_inode_ref(ip);
210                 spin_unlock(&pmp->inum_spin);
211         } else {
212                 ip = NULL;
213         }
214         return(ip);
215 }
216
217 /*
218  * Adding a ref to an inode is only legal if the inode already has at least
219  * one ref.
220  */
221 void
222 hammer2_inode_ref(hammer2_inode_t *ip)
223 {
224         atomic_add_int(&ip->refs, 1);
225 }
226
227 /*
228  * Drop an inode reference, freeing the inode when the last reference goes
229  * away.
230  */
231 void
232 hammer2_inode_drop(hammer2_inode_t *ip)
233 {
234         hammer2_pfsmount_t *pmp;
235         hammer2_inode_t *pip;
236         u_int refs;
237
238         while (ip) {
239                 refs = ip->refs;
240                 cpu_ccfence();
241                 if (refs == 1) {
242                         /*
243                          * Transition to zero, must interlock with
244                          * the inode inumber lookup tree (if applicable).
245                          *
246                          * NOTE: The super-root inode has no pmp.
247                          */
248                         pmp = ip->pmp;
249                         if (pmp)
250                                 spin_lock(&pmp->inum_spin);
251
252                         if (atomic_cmpset_int(&ip->refs, 1, 0)) {
253                                 KKASSERT(ip->topo_cst.count == 0);
254                                 if (ip->flags & HAMMER2_INODE_ONRBTREE) {
255                                         atomic_clear_int(&ip->flags,
256                                                      HAMMER2_INODE_ONRBTREE);
257                                         RB_REMOVE(hammer2_inode_tree,
258                                                   &pmp->inum_tree, ip);
259                                 }
260                                 if (pmp)
261                                         spin_unlock(&pmp->inum_spin);
262
263                                 pip = ip->pip;
264                                 ip->pip = NULL;
265                                 ip->pmp = NULL;
266
267                                 /*
268                                  * Cleaning out ip->chain isn't entirely
269                                  * trivial.
270                                  */
271                                 hammer2_inode_repoint(ip, NULL, NULL);
272
273                                 /*
274                                  * We have to drop pip (if non-NULL) to
275                                  * dispose of our implied reference from
276                                  * ip->pip.  We can simply loop on it.
277                                  */
278                                 if (pmp) {
279                                         KKASSERT((ip->flags &
280                                                   HAMMER2_INODE_SROOT) == 0);
281                                         kfree(ip, pmp->minode);
282                                         atomic_add_long(&pmp->inmem_inodes, -1);
283                                 } else {
284                                         KKASSERT(ip->flags &
285                                                  HAMMER2_INODE_SROOT);
286                                         kfree(ip, M_HAMMER2);
287                                 }
288                                 ip = pip;
289                                 /* continue with pip (can be NULL) */
290                         } else {
291                                 if (pmp)
292                                         spin_unlock(&ip->pmp->inum_spin);
293                         }
294                 } else {
295                         /*
296                          * Non zero transition
297                          */
298                         if (atomic_cmpset_int(&ip->refs, refs, refs - 1))
299                                 break;
300                 }
301         }
302 }
303
304 /*
305  * Get the vnode associated with the given inode, allocating the vnode if
306  * necessary.  The vnode will be returned exclusively locked.
307  *
308  * The caller must lock the inode (shared or exclusive).
309  *
310  * Great care must be taken to avoid deadlocks and vnode acquisition/reclaim
311  * races.
312  */
313 struct vnode *
314 hammer2_igetv(hammer2_inode_t *ip, int *errorp)
315 {
316         hammer2_inode_data_t *ipdata;
317         hammer2_pfsmount_t *pmp;
318         struct vnode *vp;
319         ccms_state_t ostate;
320
321         pmp = ip->pmp;
322         KKASSERT(pmp != NULL);
323         *errorp = 0;
324         ipdata = &ip->chain->data->ipdata;
325
326         for (;;) {
327                 /*
328                  * Attempt to reuse an existing vnode assignment.  It is
329                  * possible to race a reclaim so the vget() may fail.  The
330                  * inode must be unlocked during the vget() to avoid a
331                  * deadlock against a reclaim.
332                  */
333                 vp = ip->vp;
334                 if (vp) {
335                         /*
336                          * Inode must be unlocked during the vget() to avoid
337                          * possible deadlocks, but leave the ip ref intact.
338                          *
339                          * vnode is held to prevent destruction during the
340                          * vget().  The vget() can still fail if we lost
341                          * a reclaim race on the vnode.
342                          */
343                         vhold_interlocked(vp);
344                         ostate = hammer2_inode_lock_temp_release(ip);
345                         if (vget(vp, LK_EXCLUSIVE)) {
346                                 vdrop(vp);
347                                 hammer2_inode_lock_temp_restore(ip, ostate);
348                                 continue;
349                         }
350                         hammer2_inode_lock_temp_restore(ip, ostate);
351                         vdrop(vp);
352                         /* vp still locked and ref from vget */
353                         if (ip->vp != vp) {
354                                 kprintf("hammer2: igetv race %p/%p\n",
355                                         ip->vp, vp);
356                                 vput(vp);
357                                 continue;
358                         }
359                         *errorp = 0;
360                         break;
361                 }
362
363                 /*
364                  * No vnode exists, allocate a new vnode.  Beware of
365                  * allocation races.  This function will return an
366                  * exclusively locked and referenced vnode.
367                  */
368                 *errorp = getnewvnode(VT_HAMMER2, pmp->mp, &vp, 0, 0);
369                 if (*errorp) {
370                         kprintf("hammer2: igetv getnewvnode failed %d\n",
371                                 *errorp);
372                         vp = NULL;
373                         break;
374                 }
375
376                 /*
377                  * Lock the inode and check for an allocation race.
378                  */
379                 ostate = hammer2_inode_lock_upgrade(ip);
380                 if (ip->vp != NULL) {
381                         vp->v_type = VBAD;
382                         vx_put(vp);
383                         hammer2_inode_lock_downgrade(ip, ostate);
384                         continue;
385                 }
386
387                 switch (ipdata->type) {
388                 case HAMMER2_OBJTYPE_DIRECTORY:
389                         vp->v_type = VDIR;
390                         break;
391                 case HAMMER2_OBJTYPE_REGFILE:
392                         vp->v_type = VREG;
393                         vinitvmio(vp, ipdata->size,
394                                   HAMMER2_LBUFSIZE,
395                                   (int)ipdata->size & HAMMER2_LBUFMASK);
396                         break;
397                 case HAMMER2_OBJTYPE_SOFTLINK:
398                         /*
399                          * XXX for now we are using the generic file_read
400                          * and file_write code so we need a buffer cache
401                          * association.
402                          */
403                         vp->v_type = VLNK;
404                         vinitvmio(vp, ipdata->size,
405                                   HAMMER2_LBUFSIZE,
406                                   (int)ipdata->size & HAMMER2_LBUFMASK);
407                         break;
408                 /* XXX FIFO */
409                 default:
410                         panic("hammer2: unhandled objtype %d", ipdata->type);
411                         break;
412                 }
413
414                 if (ip == pmp->iroot)
415                         vsetflags(vp, VROOT);
416
417                 vp->v_data = ip;
418                 ip->vp = vp;
419                 hammer2_inode_ref(ip);          /* vp association */
420                 hammer2_inode_lock_downgrade(ip, ostate);
421                 break;
422         }
423
424         /*
425          * Return non-NULL vp and *errorp == 0, or NULL vp and *errorp != 0.
426          */
427         if (hammer2_debug & 0x0002) {
428                 kprintf("igetv vp %p refs %d aux %d\n",
429                         vp, vp->v_sysref.refcnt, vp->v_auxrefs);
430         }
431         return (vp);
432 }
433
434 /*
435  * The passed-in chain must be locked and the returned inode will also be
436  * locked.  This routine typically locates or allocates the inode, assigns
437  * ip->chain (adding a ref to chain if necessary), and returns the inode.
438  *
439  * The hammer2_inode structure regulates the interface between the high level
440  * kernel VNOPS API and the filesystem backend (the chains).
441  *
442  * WARNING!  This routine sucks up the chain's lock (makes it part of the
443  *           inode lock from the point of view of the inode lock API),
444  *           so callers need to be careful.
445  *
446  * WARNING!  The mount code is allowed to pass dip == NULL for iroot and
447  *           is allowed to pass pmp == NULL and dip == NULL for sroot.
448  */
449 hammer2_inode_t *
450 hammer2_inode_get(hammer2_pfsmount_t *pmp, hammer2_inode_t *dip,
451                   hammer2_chain_t *chain)
452 {
453         hammer2_inode_t *nip;
454
455         KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_INODE);
456
457         /*
458          * Interlocked lookup/ref of the inode.  This code is only needed
459          * when looking up inodes with nlinks != 0 (TODO: optimize out
460          * otherwise and test for duplicates).
461          */
462 again:
463         for (;;) {
464                 nip = hammer2_inode_lookup(pmp, chain->data->ipdata.inum);
465                 if (nip == NULL)
466                         break;
467                 ccms_thread_lock(&nip->topo_cst, CCMS_STATE_EXCLUSIVE);
468                 if ((nip->flags & HAMMER2_INODE_ONRBTREE) == 0) { /* race */
469                         ccms_thread_unlock(&nip->topo_cst);
470                         hammer2_inode_drop(nip);
471                         continue;
472                 }
473                 if (nip->chain != chain)
474                         hammer2_inode_repoint(nip, NULL, chain);
475
476                 /*
477                  * Consolidated nip/nip->chain is locked (chain locked
478                  * by caller).
479                  */
480                 return nip;
481         }
482
483         /*
484          * We couldn't find the inode number, create a new inode.
485          */
486         if (pmp) {
487                 nip = kmalloc(sizeof(*nip), pmp->minode, M_WAITOK | M_ZERO);
488                 atomic_add_long(&pmp->inmem_inodes, 1);
489                 hammer2_chain_memory_wakeup(pmp);
490         } else {
491                 nip = kmalloc(sizeof(*nip), M_HAMMER2, M_WAITOK | M_ZERO);
492                 nip->flags = HAMMER2_INODE_SROOT;
493         }
494         nip->inum = chain->data->ipdata.inum;
495         nip->size = chain->data->ipdata.size;
496         nip->mtime = chain->data->ipdata.mtime;
497         hammer2_inode_repoint(nip, NULL, chain);
498         nip->pip = dip;                         /* can be NULL */
499         if (dip)
500                 hammer2_inode_ref(dip); /* ref dip for nip->pip */
501
502         nip->pmp = pmp;
503
504         /*
505          * ref and lock on nip gives it state compatible to after a
506          * hammer2_inode_lock_ex() call.
507          */
508         nip->refs = 1;
509         ccms_cst_init(&nip->topo_cst, &nip->chain);
510         ccms_thread_lock(&nip->topo_cst, CCMS_STATE_EXCLUSIVE);
511         /* combination of thread lock and chain lock == inode lock */
512
513         /*
514          * Attempt to add the inode.  If it fails we raced another inode
515          * get.  Undo all the work and try again.
516          */
517         if (pmp) {
518                 spin_lock(&pmp->inum_spin);
519                 if (RB_INSERT(hammer2_inode_tree, &pmp->inum_tree, nip)) {
520                         spin_unlock(&pmp->inum_spin);
521                         ccms_thread_unlock(&nip->topo_cst);
522                         hammer2_inode_drop(nip);
523                         goto again;
524                 }
525                 atomic_set_int(&nip->flags, HAMMER2_INODE_ONRBTREE);
526                 spin_unlock(&pmp->inum_spin);
527         }
528
529         return (nip);
530 }
531
532 /*
533  * Create a new inode in the specified directory using the vattr to
534  * figure out the type of inode.
535  *
536  * If no error occurs the new inode with its chain locked is returned in
537  * *nipp, otherwise an error is returned and *nipp is set to NULL.
538  *
539  * If vap and/or cred are NULL the related fields are not set and the
540  * inode type defaults to a directory.  This is used when creating PFSs
541  * under the super-root, so the inode number is set to 1 in this case.
542  *
543  * dip is not locked on entry.
544  */
545 hammer2_inode_t *
546 hammer2_inode_create(hammer2_trans_t *trans, hammer2_inode_t *dip,
547                      struct vattr *vap, struct ucred *cred,
548                      const uint8_t *name, size_t name_len,
549                      hammer2_chain_t **chainp, int *errorp)
550 {
551         hammer2_inode_data_t *dipdata;
552         hammer2_inode_data_t *nipdata;
553         hammer2_chain_t *chain;
554         hammer2_chain_t *parent;
555         hammer2_inode_t *nip;
556         hammer2_key_t lhc;
557         int error;
558         uid_t xuid;
559         uuid_t dip_uid;
560         uuid_t dip_gid;
561         uint32_t dip_mode;
562
563         lhc = hammer2_dirhash(name, name_len);
564         *errorp = 0;
565
566         /*
567          * Locate the inode or indirect block to create the new
568          * entry in.  At the same time check for key collisions
569          * and iterate until we don't get one.
570          *
571          * NOTE: hidden inodes do not have iterators.
572          */
573 retry:
574         parent = hammer2_inode_lock_ex(dip);
575         dipdata = &dip->chain->data->ipdata;
576         dip_uid = dipdata->uid;
577         dip_gid = dipdata->gid;
578         dip_mode = dipdata->mode;
579
580         error = 0;
581         while (error == 0) {
582                 chain = hammer2_chain_lookup(&parent, lhc, lhc, 0);
583                 if (chain == NULL)
584                         break;
585                 if ((lhc & HAMMER2_DIRHASH_VISIBLE) == 0)
586                         error = ENOSPC;
587                 if ((lhc & HAMMER2_DIRHASH_LOMASK) == HAMMER2_DIRHASH_LOMASK)
588                         error = ENOSPC;
589                 hammer2_chain_unlock(chain);
590                 chain = NULL;
591                 ++lhc;
592         }
593
594         if (error == 0) {
595                 error = hammer2_chain_create(trans, &parent, &chain,
596                                              lhc, 0,
597                                              HAMMER2_BREF_TYPE_INODE,
598                                              HAMMER2_INODE_BYTES);
599         }
600
601         /*
602          * Cleanup and handle retries.
603          */
604         if (error == EAGAIN) {
605                 hammer2_chain_ref(parent);
606                 hammer2_inode_unlock_ex(dip, parent);
607                 hammer2_chain_wait(parent);
608                 hammer2_chain_drop(parent);
609                 goto retry;
610         }
611         hammer2_inode_unlock_ex(dip, parent);
612
613         if (error) {
614                 KKASSERT(chain == NULL);
615                 *errorp = error;
616                 return (NULL);
617         }
618
619         /*
620          * Set up the new inode.
621          *
622          * NOTE: *_get() integrates chain's lock into the inode lock.
623          *
624          * NOTE: Only one new inode can currently be created per
625          *       transaction.  If the need arises we can adjust
626          *       hammer2_trans_init() to allow more.
627          *
628          * NOTE: nipdata will have chain's blockset data.
629          */
630         chain->data->ipdata.inum = trans->sync_tid;
631         nip = hammer2_inode_get(dip->pmp, dip, chain);
632         nipdata = &chain->data->ipdata;
633
634         if (vap) {
635                 KKASSERT(trans->inodes_created == 0);
636                 nipdata->type = hammer2_get_obj_type(vap->va_type);
637                 nipdata->inum = trans->sync_tid;
638                 ++trans->inodes_created;
639         } else {
640                 nipdata->type = HAMMER2_OBJTYPE_DIRECTORY;
641                 nipdata->inum = 1;
642         }
643         
644         /* Inherit parent's inode compression mode. */
645         nip->comp_heuristic = 0;
646         nipdata->comp_algo = dipdata->comp_algo;
647         nipdata->version = HAMMER2_INODE_VERSION_ONE;
648         hammer2_update_time(&nipdata->ctime);
649         nipdata->mtime = nipdata->ctime;
650         if (vap)
651                 nipdata->mode = vap->va_mode;
652         nipdata->nlinks = 1;
653         if (vap) {
654                 if (dip) {
655                         xuid = hammer2_to_unix_xid(&dip_uid);
656                         xuid = vop_helper_create_uid(dip->pmp->mp,
657                                                      dip_mode,
658                                                      xuid,
659                                                      cred,
660                                                      &vap->va_mode);
661                 } else {
662                         xuid = 0;
663                 }
664                 if (vap->va_vaflags & VA_UID_UUID_VALID)
665                         nipdata->uid = vap->va_uid_uuid;
666                 else if (vap->va_uid != (uid_t)VNOVAL)
667                         hammer2_guid_to_uuid(&nipdata->uid, vap->va_uid);
668                 else
669                         hammer2_guid_to_uuid(&nipdata->uid, xuid);
670
671                 if (vap->va_vaflags & VA_GID_UUID_VALID)
672                         nipdata->gid = vap->va_gid_uuid;
673                 else if (vap->va_gid != (gid_t)VNOVAL)
674                         hammer2_guid_to_uuid(&nipdata->gid, vap->va_gid);
675                 else if (dip)
676                         nipdata->gid = dip_gid;
677         }
678
679         /*
680          * Regular files and softlinks allow a small amount of data to be
681          * directly embedded in the inode.  This flag will be cleared if
682          * the size is extended past the embedded limit.
683          */
684         if (nipdata->type == HAMMER2_OBJTYPE_REGFILE ||
685             nipdata->type == HAMMER2_OBJTYPE_SOFTLINK) {
686                 nipdata->op_flags |= HAMMER2_OPFLAG_DIRECTDATA;
687         }
688
689         KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
690         bcopy(name, nipdata->filename, name_len);
691         nipdata->name_key = lhc;
692         nipdata->name_len = name_len;
693         *chainp = chain;
694
695         return (nip);
696 }
697
698 /*
699  * chain may have been moved around by the create.
700  */
701 static
702 void
703 hammer2_chain_refactor(hammer2_chain_t **chainp)
704 {
705         hammer2_chain_t *chain = *chainp;
706         hammer2_chain_core_t *core;
707
708         core = chain->core;
709         spin_lock(&core->cst.spin);
710         while (hammer2_chain_refactor_test(chain, 1)) {
711                 chain = chain->next_parent;
712                 while (hammer2_chain_refactor_test(chain, 1))
713                         chain = chain->next_parent;
714                 hammer2_chain_ref(chain);
715                 spin_unlock(&core->cst.spin);
716
717                 hammer2_chain_unlock(*chainp);
718                 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS |
719                                           HAMMER2_RESOLVE_NOREF); /* eat ref */
720                 *chainp = chain;
721                 spin_lock(&core->cst.spin);
722         }
723         spin_unlock(&core->cst.spin);
724 }
725
726 /*
727  * ochain represents the target file inode.  We need to move it to the
728  * specified common parent directory (dip) and rename it to a special
729  * invisible "0xINODENUMBER" filename.
730  *
731  * We use chain_duplicate and duplicate ochain at the new location,
732  * renaming it appropriately.  We create a temporary chain and
733  * then delete it to placemark where the duplicate will go.  Both of
734  * these use the inode number for (lhc) (the key), generating the
735  * invisible filename.
736  */
737 static
738 hammer2_chain_t *
739 hammer2_hardlink_shiftup(hammer2_trans_t *trans, hammer2_chain_t **ochainp,
740                         hammer2_inode_t *dip, int *errorp)
741 {
742         hammer2_inode_data_t *nipdata;
743         hammer2_chain_t *parent;
744         hammer2_chain_t *ochain;
745         hammer2_chain_t *nchain;
746         hammer2_chain_t *tmp;
747         hammer2_key_t lhc;
748         hammer2_blockref_t bref;
749
750         ochain = *ochainp;
751         *errorp = 0;
752         lhc = ochain->data->ipdata.inum;
753         KKASSERT((lhc & HAMMER2_DIRHASH_VISIBLE) == 0);
754
755         /*
756          * Locate the inode or indirect block to create the new
757          * entry in.  lhc represents the inode number so there is
758          * no collision iteration.
759          *
760          * There should be no key collisions with invisible inode keys.
761          */
762 retry:
763         parent = hammer2_chain_lookup_init(dip->chain, 0);
764         nchain = hammer2_chain_lookup(&parent, lhc, lhc, 0);
765         if (nchain) {
766                 kprintf("X3 chain %p parent %p dip %p dip->chain %p\n",
767                         nchain, parent, dip, dip->chain);
768                 hammer2_chain_unlock(nchain);
769                 nchain = NULL;
770                 *errorp = ENOSPC;
771 #if 1
772                 Debugger("X3");
773 #endif
774         }
775
776         /*
777          * Create entry in common parent directory using the seek position
778          * calculated above.
779          */
780         if (*errorp == 0) {
781                 KKASSERT(nchain == NULL);
782                 *errorp = hammer2_chain_create(trans, &parent, &nchain,
783                                                lhc, 0,
784                                                HAMMER2_BREF_TYPE_INODE,/* n/a */
785                                                HAMMER2_INODE_BYTES);   /* n/a */
786                 hammer2_chain_refactor(&ochain);
787                 *ochainp = ochain;
788         }
789
790         /*
791          * Cleanup and handle retries.
792          */
793         if (*errorp == EAGAIN) {
794                 hammer2_chain_ref(parent);
795                 hammer2_chain_lookup_done(parent);
796                 hammer2_chain_wait(parent);
797                 hammer2_chain_drop(parent);
798                 goto retry;
799         }
800
801         /*
802          * Handle the error case
803          */
804         if (*errorp) {
805                 KKASSERT(nchain == NULL);
806                 hammer2_chain_lookup_done(parent);
807                 return (NULL);
808         }
809
810         /*
811          * Use chain as a placeholder for (lhc), delete it and replace
812          * it with our duplication.
813          *
814          * Gain a second lock on ochain for the duplication function to
815          * unlock, maintain the caller's original lock across the call.
816          *
817          * This is a bit messy.
818          */
819         hammer2_chain_delete(trans, nchain, HAMMER2_DELETE_WILLDUP);
820         hammer2_chain_lock(ochain, HAMMER2_RESOLVE_ALWAYS);
821         tmp = ochain;
822         bref = tmp->bref;
823         bref.key = lhc;                 /* invisible dir entry key */
824         bref.keybits = 0;
825         hammer2_chain_duplicate(trans, parent, nchain->index, &tmp, &bref);
826         hammer2_chain_lookup_done(parent);
827         hammer2_chain_unlock(nchain);   /* no longer needed */
828
829         /*
830          * Now set chain to our duplicate and modify it appropriately.
831          *
832          * Directory entries are inodes but this is a hidden hardlink
833          * target.  The name isn't used but to ease debugging give it
834          * a name after its inode number.
835          */
836         nchain = tmp;
837         tmp = NULL;     /* safety */
838
839         hammer2_chain_modify(trans, &nchain, HAMMER2_MODIFY_ASSERTNOCOPY);
840         nipdata = &nchain->data->ipdata;
841         ksnprintf(nipdata->filename, sizeof(nipdata->filename),
842                   "0x%016jx", (intmax_t)nipdata->inum);
843         nipdata->name_len = strlen(nipdata->filename);
844         nipdata->name_key = lhc;
845
846         return (nchain);
847 }
848
849 /*
850  * Connect the target inode represented by (*chainp) to the media topology
851  * at (dip, name, len).
852  *
853  * If hlink is TRUE this function creates an OBJTYPE_HARDLINK directory
854  * entry instead of connecting (*chainp).
855  *
856  * If hlink is FALSE this function uses chain_duplicate() to make a copy
857  * if (*chainp) in the directory entry.  (*chainp) is likely to be deleted
858  * by the caller in this case (e.g. rename).
859  */
860 int
861 hammer2_inode_connect(hammer2_trans_t *trans, int hlink,
862                       hammer2_inode_t *dip, hammer2_chain_t **chainp,
863                       const uint8_t *name, size_t name_len)
864 {
865         hammer2_inode_data_t *ipdata;
866         hammer2_chain_t *nchain;
867         hammer2_chain_t *parent;
868         hammer2_chain_t *ochain;
869         hammer2_key_t lhc;
870         int error;
871
872         ochain = *chainp;
873
874         /*
875          * Since ochain is either disconnected from the topology or represents
876          * a hardlink terminus which is always a parent of or equal to dip,
877          * we should be able to safely lock dip->chain for our setup.
878          */
879         parent = hammer2_chain_lookup_init(dip->chain, 0);
880
881         lhc = hammer2_dirhash(name, name_len);
882
883         /*
884          * Locate the inode or indirect block to create the new
885          * entry in.  At the same time check for key collisions
886          * and iterate until we don't get one.
887          */
888         error = 0;
889         while (error == 0) {
890                 nchain = hammer2_chain_lookup(&parent, lhc, lhc, 0);
891                 if (nchain == NULL)
892                         break;
893                 if ((lhc & HAMMER2_DIRHASH_LOMASK) == HAMMER2_DIRHASH_LOMASK)
894                         error = ENOSPC;
895                 hammer2_chain_unlock(nchain);
896                 nchain = NULL;
897                 ++lhc;
898         }
899
900         if (error == 0) {
901                 if (hlink) {
902                         /*
903                          * Hardlink pointer needed, create totally fresh
904                          * directory entry.
905                          */
906                         KKASSERT(nchain == NULL);
907                         error = hammer2_chain_create(trans, &parent, &nchain,
908                                                      lhc, 0,
909                                                      HAMMER2_BREF_TYPE_INODE,
910                                                      HAMMER2_INODE_BYTES);
911                         hammer2_chain_refactor(&ochain);
912                 } else {
913                         /*
914                          * Reconnect the original chain and rename.  Use
915                          * chain_duplicate().  The caller will likely delete
916                          * or has already deleted the original chain in
917                          * this case.
918                          *
919                          * NOTE: chain_duplicate() generates a new chain
920                          *       with CHAIN_DELETED cleared (ochain typically
921                          *       has it set from the file unlink).
922                          */
923                         nchain = ochain;
924                         ochain = NULL;
925                         hammer2_chain_duplicate(trans, NULL, -1, &nchain, NULL);
926                         error = hammer2_chain_create(trans, &parent, &nchain,
927                                                      lhc, 0,
928                                                      HAMMER2_BREF_TYPE_INODE,
929                                                      HAMMER2_INODE_BYTES);
930                 }
931         }
932
933         /*
934          * Unlock stuff.
935          */
936         KKASSERT(error != EAGAIN);
937         hammer2_chain_lookup_done(parent);
938         parent = NULL;
939
940         /*
941          * nchain should be NULL on error, leave ochain (== *chainp) alone.
942          */
943         if (error) {
944                 KKASSERT(nchain == NULL);
945                 return (error);
946         }
947
948         /*
949          * Directory entries are inodes so if the name has changed we have
950          * to update the inode.
951          *
952          * When creating an OBJTYPE_HARDLINK entry remember to unlock the
953          * chain, the caller will access the hardlink via the actual hardlink
954          * target file and not the hardlink pointer entry, so we must still
955          * return ochain.
956          */
957         if (hlink && hammer2_hardlink_enable >= 0) {
958                 /*
959                  * Create the HARDLINK pointer.  oip represents the hardlink
960                  * target in this situation.
961                  *
962                  * We will return ochain (the hardlink target).
963                  */
964                 hammer2_chain_modify(trans, &nchain,
965                                      HAMMER2_MODIFY_ASSERTNOCOPY);
966                 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
967                 ipdata = &nchain->data->ipdata;
968                 atomic_set_int(&nchain->flags, HAMMER2_CHAIN_HARDLINK);
969                 bcopy(name, ipdata->filename, name_len);
970                 ipdata->name_key = lhc;
971                 ipdata->name_len = name_len;
972                 ipdata->target_type = ochain->data->ipdata.type;
973                 ipdata->type = HAMMER2_OBJTYPE_HARDLINK;
974                 ipdata->inum = ochain->data->ipdata.inum;
975                 ipdata->nlinks = 1;
976                 hammer2_chain_unlock(nchain);
977                 nchain = ochain;
978                 ochain = NULL;
979         } else if (hlink && hammer2_hardlink_enable < 0) {
980                 /*
981                  * Create a snapshot (hardlink fake mode for debugging).
982                  * (ochain already flushed above so we can just copy the
983                  * bref XXX).
984                  *
985                  * Since this is a snapshot we return nchain in the fake
986                  * hardlink case.
987                  */
988                 hammer2_chain_modify(trans, &nchain,
989                                      HAMMER2_MODIFY_ASSERTNOCOPY);
990                 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
991                 ipdata = &nchain->data->ipdata;
992                 *ipdata = ochain->data->ipdata;
993                 bcopy(name, ipdata->filename, name_len);
994                 ipdata->name_key = lhc;
995                 ipdata->name_len = name_len;
996                 kprintf("created fake hardlink %*.*s\n",
997                         (int)name_len, (int)name_len, name);
998         } else {
999                 /*
1000                  * nchain is a duplicate of ochain at the new location.
1001                  * We must fixup the name stored in oip.  The bref key
1002                  * has already been set up.
1003                  */
1004                 hammer2_chain_modify(trans, &nchain,
1005                                      HAMMER2_MODIFY_ASSERTNOCOPY);
1006                 ipdata = &nchain->data->ipdata;
1007
1008                 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
1009                 bcopy(name, ipdata->filename, name_len);
1010                 ipdata->name_key = lhc;
1011                 ipdata->name_len = name_len;
1012                 ipdata->nlinks = 1;
1013         }
1014
1015         /*
1016          * We are replacing ochain with nchain, unlock ochain.  In the
1017          * case where ochain is left unchanged the code above sets
1018          * nchain to ochain and ochain to NULL, resulting in a NOP here.
1019          */
1020         if (ochain)
1021                 hammer2_chain_unlock(ochain);
1022         *chainp = nchain;
1023
1024         return (0);
1025 }
1026
1027 /*
1028  * Repoint ip->chain to nchain.  Caller must hold the inode exclusively
1029  * locked.
1030  *
1031  * ip->chain is set to nchain.  The prior chain in ip->chain is dropped
1032  * and nchain is ref'd.
1033  */
1034 void
1035 hammer2_inode_repoint(hammer2_inode_t *ip, hammer2_inode_t *pip,
1036                       hammer2_chain_t *nchain)
1037 {
1038         hammer2_chain_t *ochain;
1039         hammer2_inode_t *opip;
1040
1041         /*
1042          * Repoint ip->chain if requested.
1043          */
1044         ochain = ip->chain;
1045         ip->chain = nchain;
1046         if (nchain)
1047                 hammer2_chain_ref(nchain);
1048         if (ochain)
1049                 hammer2_chain_drop(ochain);
1050
1051         /*
1052          * Flag the chain for the refactor test
1053          */
1054         if (nchain && nchain->data && nchain->data->ipdata.type == HAMMER2_OBJTYPE_HARDLINK)
1055                 atomic_set_int(&nchain->flags, HAMMER2_CHAIN_HARDLINK);
1056
1057         /*
1058          * Repoint ip->pip if requested (non-NULL pip).
1059          */
1060         if (pip && ip->pip != pip) {
1061                 opip = ip->pip;
1062                 hammer2_inode_ref(pip);
1063                 ip->pip = pip;
1064                 if (opip)
1065                         hammer2_inode_drop(opip);
1066         }
1067 }
1068
1069 /*
1070  * Unlink the file from the specified directory inode.  The directory inode
1071  * does not need to be locked.
1072  *
1073  * isdir determines whether a directory/non-directory check should be made.
1074  * No check is made if isdir is set to -1.
1075  *
1076  * NOTE!  The underlying file can still be active with open descriptors
1077  *        or if the chain is being manually held (e.g. for rename).
1078  *
1079  *        The caller is responsible for fixing up ip->chain if e.g. a
1080  *        rename occurs (see chain_duplicate()).
1081  */
1082 int
1083 hammer2_unlink_file(hammer2_trans_t *trans, hammer2_inode_t *dip,
1084                     const uint8_t *name, size_t name_len,
1085                     int isdir, int *hlinkp)
1086 {
1087         hammer2_inode_data_t *ipdata;
1088         hammer2_chain_t *parent;
1089         hammer2_chain_t *ochain;
1090         hammer2_chain_t *chain;
1091         hammer2_chain_t *dparent;
1092         hammer2_chain_t *dchain;
1093         hammer2_key_t lhc;
1094         int error;
1095         uint8_t type;
1096
1097         error = 0;
1098         ochain = NULL;
1099         lhc = hammer2_dirhash(name, name_len);
1100
1101         /*
1102          * Search for the filename in the directory
1103          */
1104         if (hlinkp)
1105                 *hlinkp = 0;
1106         parent = hammer2_inode_lock_ex(dip);
1107         chain = hammer2_chain_lookup(&parent,
1108                                      lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1109                                      0);
1110         while (chain) {
1111                 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
1112                     name_len == chain->data->ipdata.name_len &&
1113                     bcmp(name, chain->data->ipdata.filename, name_len) == 0) {
1114                         break;
1115                 }
1116                 chain = hammer2_chain_next(&parent, chain,
1117                                            lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1118                                            0);
1119         }
1120         hammer2_inode_unlock_ex(dip, NULL);     /* retain parent */
1121
1122         /*
1123          * Not found or wrong type (isdir < 0 disables the type check).
1124          * If a hardlink pointer, type checks use the hardlink target.
1125          */
1126         if (chain == NULL) {
1127                 error = ENOENT;
1128                 goto done;
1129         }
1130         if ((type = chain->data->ipdata.type) == HAMMER2_OBJTYPE_HARDLINK) {
1131                 if (hlinkp)
1132                         *hlinkp = 1;
1133                 type = chain->data->ipdata.target_type;
1134         }
1135
1136         if (type == HAMMER2_OBJTYPE_DIRECTORY && isdir == 0) {
1137                 error = ENOTDIR;
1138                 goto done;
1139         }
1140         if (type != HAMMER2_OBJTYPE_DIRECTORY && isdir >= 1) {
1141                 error = EISDIR;
1142                 goto done;
1143         }
1144
1145         /*
1146          * Hardlink must be resolved.  We can't hold parent locked while we
1147          * do this or we could deadlock.
1148          *
1149          * On success chain will be adjusted to point at the hardlink target
1150          * and ochain will point to the hardlink pointer in the original
1151          * directory.  Otherwise chain remains pointing to the original.
1152          */
1153         if (chain->data->ipdata.type == HAMMER2_OBJTYPE_HARDLINK) {
1154                 hammer2_chain_unlock(parent);
1155                 parent = NULL;
1156                 error = hammer2_hardlink_find(dip, &chain, &ochain);
1157         }
1158
1159         /*
1160          * If this is a directory the directory must be empty.  However, if
1161          * isdir < 0 we are doing a rename and the directory does not have
1162          * to be empty, and if isdir > 1 we are deleting a PFS/snapshot
1163          * and the directory does not have to be empty.
1164          *
1165          * NOTE: We check the full key range here which covers both visible
1166          *       and invisible entries.  Theoretically there should be no
1167          *       invisible (hardlink target) entries if there are no visible
1168          *       entries.
1169          */
1170         if (type == HAMMER2_OBJTYPE_DIRECTORY && isdir == 1) {
1171                 dparent = hammer2_chain_lookup_init(chain, 0);
1172                 dchain = hammer2_chain_lookup(&dparent,
1173                                               0, (hammer2_key_t)-1,
1174                                               HAMMER2_LOOKUP_NODATA);
1175                 if (dchain) {
1176                         hammer2_chain_unlock(dchain);
1177                         hammer2_chain_lookup_done(dparent);
1178                         error = ENOTEMPTY;
1179                         goto done;
1180                 }
1181                 hammer2_chain_lookup_done(dparent);
1182                 dparent = NULL;
1183                 /* dchain NULL */
1184         }
1185
1186         /*
1187          * Ok, we can now unlink the chain.  We always decrement nlinks even
1188          * if the entry can be deleted in case someone has the file open and
1189          * does an fstat().
1190          *
1191          * The chain itself will no longer be in the on-media topology but
1192          * can still be flushed to the media (e.g. if an open descriptor
1193          * remains).  When the last vnode/ip ref goes away the chain will
1194          * be marked unmodified, avoiding any further (now unnecesary) I/O.
1195          *
1196          * A non-NULL ochain indicates a hardlink.
1197          */
1198         if (ochain) {
1199                 /*
1200                  * Delete the original hardlink pointer.
1201                  *
1202                  * NOTE: parent from above is NULL when ochain != NULL
1203                  *       so we can reuse it.
1204                  */
1205                 hammer2_chain_lock(ochain, HAMMER2_RESOLVE_ALWAYS);
1206                 hammer2_chain_delete(trans, ochain, 0);
1207                 hammer2_chain_unlock(ochain);
1208
1209                 /*
1210                  * Then decrement nlinks on hardlink target, deleting
1211                  * the target when nlinks drops to 0.
1212                  */
1213                 hammer2_chain_modify(trans, &chain, 0);
1214                 --chain->data->ipdata.nlinks;
1215                 if (chain->data->ipdata.nlinks == 0)
1216                         hammer2_chain_delete(trans, chain, 0);
1217         } else {
1218                 /*
1219                  * Otherwise this was not a hardlink and we can just
1220                  * remove the entry and decrement nlinks.
1221                  *
1222                  * NOTE: *_get() integrates chain's lock into the inode lock.
1223                  */
1224                 hammer2_chain_modify(trans, &chain, 0);
1225                 ipdata = &chain->data->ipdata;
1226                 --ipdata->nlinks;
1227                 hammer2_chain_delete(trans, chain, 0);
1228         }
1229
1230         error = 0;
1231 done:
1232         if (chain)
1233                 hammer2_chain_unlock(chain);
1234         if (parent)
1235                 hammer2_chain_lookup_done(parent);
1236         if (ochain)
1237                 hammer2_chain_drop(ochain);
1238
1239         return error;
1240 }
1241
1242 /*
1243  * Given an exclusively locked inode we consolidate its chain for hardlink
1244  * creation, adding (nlinks) to the file's link count and potentially
1245  * relocating the inode to a directory common to ip->pip and tdip.
1246  *
1247  * Replaces (*chainp) if consolidation occurred, unlocking the old chain
1248  * and returning a new locked chain.
1249  *
1250  * NOTE!  This function will also replace ip->chain.
1251  */
1252 int
1253 hammer2_hardlink_consolidate(hammer2_trans_t *trans, hammer2_inode_t *ip,
1254                              hammer2_chain_t **chainp,
1255                              hammer2_inode_t *tdip, int nlinks)
1256 {
1257         hammer2_inode_data_t *ipdata;
1258         hammer2_inode_t *fdip;
1259         hammer2_inode_t *cdip;
1260         hammer2_chain_t *chain;
1261         hammer2_chain_t *nchain;
1262         int error;
1263
1264         chain = *chainp;
1265         if (nlinks == 0 &&                      /* no hardlink needed */
1266             (chain->data->ipdata.name_key & HAMMER2_DIRHASH_VISIBLE)) {
1267                 return (0);
1268         }
1269         if (hammer2_hardlink_enable < 0) {      /* fake hardlinks */
1270                 return (0);
1271         }
1272
1273         if (hammer2_hardlink_enable == 0) {     /* disallow hardlinks */
1274                 hammer2_chain_unlock(chain);
1275                 *chainp = NULL;
1276                 return (ENOTSUP);
1277         }
1278
1279         /*
1280          * cdip will be returned with a ref, but not locked.
1281          */
1282         fdip = ip->pip;
1283         cdip = hammer2_inode_common_parent(fdip, tdip);
1284
1285         /*
1286          * If no change in the hardlink's target directory is required and
1287          * this is already a hardlink target, all we need to do is adjust
1288          * the link count.
1289          *
1290          * XXX The common parent is a big wiggly due to duplication from
1291          *     renames.  Compare the core (RBTREE) pointer instead of the
1292          *     ip's.
1293          */
1294         if (cdip == fdip &&
1295             (chain->data->ipdata.name_key & HAMMER2_DIRHASH_VISIBLE) == 0) {
1296                 if (nlinks) {
1297                         hammer2_chain_modify(trans, &chain, 0);
1298                         chain->data->ipdata.nlinks += nlinks;
1299                 }
1300                 error = 0;
1301                 goto done;
1302         }
1303
1304         /*
1305          * We either have to move an existing hardlink target or we have
1306          * to create a fresh hardlink target.
1307          *
1308          * Hardlink targets are hidden inodes in a parent directory common
1309          * to all directory entries referencing the hardlink.
1310          */
1311         nchain = hammer2_hardlink_shiftup(trans, &chain, cdip, &error);
1312
1313         if (error == 0) {
1314                 /*
1315                  * Bump nlinks on duplicated hidden inode, repoint
1316                  * ip->chain.
1317                  */
1318                 hammer2_chain_modify(trans, &nchain, 0);
1319                 nchain->data->ipdata.nlinks += nlinks;
1320                 hammer2_inode_repoint(ip, cdip, nchain);
1321
1322                 /*
1323                  * If the old chain is not a hardlink target then replace
1324                  * it with a OBJTYPE_HARDLINK pointer.
1325                  *
1326                  * If the old chain IS a hardlink target then delete it.
1327                  */
1328                 if (chain->data->ipdata.name_key & HAMMER2_DIRHASH_VISIBLE) {
1329                         /*
1330                          * Replace original non-hardlink that's been dup'd
1331                          * with a special hardlink directory entry.  We must
1332                          * set the DIRECTDATA flag to prevent sub-chains
1333                          * from trying to synchronize to the inode if the
1334                          * file is extended afterwords.
1335                          */
1336                         hammer2_chain_modify(trans, &chain, 0);
1337                         hammer2_chain_delete_duplicate(trans, &chain,
1338                                                        HAMMER2_DELDUP_RECORE);
1339                         atomic_set_int(&chain->flags, HAMMER2_CHAIN_HARDLINK);
1340                         ipdata = &chain->data->ipdata;
1341                         ipdata->target_type = ipdata->type;
1342                         ipdata->type = HAMMER2_OBJTYPE_HARDLINK;
1343                         ipdata->uflags = 0;
1344                         ipdata->rmajor = 0;
1345                         ipdata->rminor = 0;
1346                         ipdata->ctime = 0;
1347                         ipdata->mtime = 0;
1348                         ipdata->atime = 0;
1349                         ipdata->btime = 0;
1350                         bzero(&ipdata->uid, sizeof(ipdata->uid));
1351                         bzero(&ipdata->gid, sizeof(ipdata->gid));
1352                         ipdata->op_flags = HAMMER2_OPFLAG_DIRECTDATA;
1353                         ipdata->cap_flags = 0;
1354                         ipdata->mode = 0;
1355                         ipdata->size = 0;
1356                         ipdata->nlinks = 1;
1357                         ipdata->iparent = 0;    /* XXX */
1358                         ipdata->pfs_type = 0;
1359                         ipdata->pfs_inum = 0;
1360                         bzero(&ipdata->pfs_clid, sizeof(ipdata->pfs_clid));
1361                         bzero(&ipdata->pfs_fsid, sizeof(ipdata->pfs_fsid));
1362                         ipdata->data_quota = 0;
1363                         ipdata->data_count = 0;
1364                         ipdata->inode_quota = 0;
1365                         ipdata->inode_count = 0;
1366                         ipdata->attr_tid = 0;
1367                         ipdata->dirent_tid = 0;
1368                         bzero(&ipdata->u, sizeof(ipdata->u));
1369                         /* XXX transaction ids */
1370                 } else {
1371                         hammer2_chain_delete(trans, chain, 0);
1372                 }
1373
1374                 /*
1375                  * Return the new chain.
1376                  */
1377                 hammer2_chain_unlock(chain);
1378                 chain = nchain;
1379         } else {
1380                 /*
1381                  * Return an error
1382                  */
1383                 hammer2_chain_unlock(chain);
1384                 chain = NULL;
1385         }
1386
1387         /*
1388          * Cleanup, chain/nchain already dealt with.
1389          */
1390 done:
1391         *chainp = chain;
1392         hammer2_inode_drop(cdip);
1393
1394         return (error);
1395 }
1396
1397 /*
1398  * If (*ochainp) is non-NULL it points to the forward OBJTYPE_HARDLINK
1399  * inode while (*chainp) points to the resolved (hidden hardlink
1400  * target) inode.  In this situation when nlinks is 1 we wish to
1401  * deconsolidate the hardlink, moving it back to the directory that now
1402  * represents the only remaining link.
1403  */
1404 int
1405 hammer2_hardlink_deconsolidate(hammer2_trans_t *trans,
1406                                hammer2_inode_t *dip,
1407                                hammer2_chain_t **chainp,
1408                                hammer2_chain_t **ochainp)
1409 {
1410         if (*ochainp == NULL)
1411                 return (0);
1412         /* XXX */
1413         return (0);
1414 }
1415
1416 /*
1417  * The caller presents a locked *chainp pointing to a HAMMER2_BREF_TYPE_INODE
1418  * with an obj_type of HAMMER2_OBJTYPE_HARDLINK.  This routine will gobble
1419  * the *chainp and return a new locked *chainp representing the file target
1420  * (the original *chainp will be unlocked).
1421  *
1422  * When a match is found the chain representing the original HARDLINK
1423  * will be returned in *ochainp with a ref, but not locked.
1424  *
1425  * When no match is found *chainp is set to NULL and EIO is returned.
1426  * (*ochainp) will still be set to the original chain with a ref but not
1427  * locked.
1428  */
1429 int
1430 hammer2_hardlink_find(hammer2_inode_t *dip, hammer2_chain_t **chainp,
1431                       hammer2_chain_t **ochainp)
1432 {
1433         hammer2_chain_t *chain = *chainp;
1434         hammer2_chain_t *parent;
1435         hammer2_inode_t *ip;
1436         hammer2_inode_t *pip;
1437         hammer2_key_t lhc;
1438
1439         pip = dip;
1440         hammer2_inode_ref(pip);         /* for loop */
1441         hammer2_chain_ref(chain);       /* for (*ochainp) */
1442         *ochainp = chain;
1443
1444         /*
1445          * Locate the hardlink.  pip is referenced and not locked,
1446          * ipp.
1447          *
1448          * chain is reused.
1449          */
1450         lhc = chain->data->ipdata.inum;
1451         hammer2_chain_unlock(chain);
1452         chain = NULL;
1453
1454         while ((ip = pip) != NULL) {
1455                 parent = hammer2_inode_lock_ex(ip);
1456                 hammer2_inode_drop(ip);                 /* loop */
1457                 KKASSERT(parent->bref.type == HAMMER2_BREF_TYPE_INODE);
1458                 chain = hammer2_chain_lookup(&parent, lhc, lhc, 0);
1459                 hammer2_chain_lookup_done(parent);      /* discard parent */
1460                 if (chain)
1461                         break;
1462                 pip = ip->pip;          /* safe, ip held locked */
1463                 if (pip)
1464                         hammer2_inode_ref(pip);         /* loop */
1465                 hammer2_inode_unlock_ex(ip, NULL);
1466         }
1467
1468         /*
1469          * chain is locked, ip is locked.  Unlock ip, return the locked
1470          * chain.  *ipp is already set w/a ref count and not locked.
1471          *
1472          * (parent is already unlocked).
1473          */
1474         if (ip)
1475                 hammer2_inode_unlock_ex(ip, NULL);
1476         *chainp = chain;
1477         if (chain) {
1478                 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_INODE);
1479                 /* already locked */
1480                 return (0);
1481         } else {
1482                 return (EIO);
1483         }
1484 }
1485
1486 /*
1487  * Find the directory common to both fdip and tdip, hold and return
1488  * its inode.
1489  */
1490 hammer2_inode_t *
1491 hammer2_inode_common_parent(hammer2_inode_t *fdip, hammer2_inode_t *tdip)
1492 {
1493         hammer2_inode_t *scan1;
1494         hammer2_inode_t *scan2;
1495
1496         /*
1497          * We used to have a depth field but it complicated matters too
1498          * much for directory renames.  So now its ugly.  Check for
1499          * simple cases before giving up and doing it the expensive way.
1500          *
1501          * XXX need a bottom-up topology stability lock
1502          */
1503         if (fdip == tdip || fdip == tdip->pip) {
1504                 hammer2_inode_ref(fdip);
1505                 return(fdip);
1506         }
1507         if (fdip->pip == tdip) {
1508                 hammer2_inode_ref(tdip);
1509                 return(tdip);
1510         }
1511
1512         /*
1513          * XXX not MPSAFE
1514          */
1515         for (scan1 = fdip; scan1->pmp == fdip->pmp; scan1 = scan1->pip) {
1516                 scan2 = tdip;
1517                 while (scan2->pmp == tdip->pmp) {
1518                         if (scan1 == scan2) {
1519                                 hammer2_inode_ref(scan1);
1520                                 return(scan1);
1521                         }
1522                         scan2 = scan2->pip;
1523                         if (scan2 == NULL)
1524                                 break;
1525                 }
1526         }
1527         panic("hammer2_inode_common_parent: no common parent %p %p\n",
1528               fdip, tdip);
1529         /* NOT REACHED */
1530         return(NULL);
1531 }
1532
1533 /*
1534  * Synchronize the inode's frontend state with the chain state prior
1535  * to any explicit flush of the inode or any strategy write call.
1536  *
1537  * Called with a locked inode.
1538  */
1539 void
1540 hammer2_inode_fsync(hammer2_trans_t *trans, hammer2_inode_t *ip, 
1541                     hammer2_chain_t **chainp)
1542 {
1543         hammer2_inode_data_t *ipdata;
1544         hammer2_chain_t *parent;
1545         hammer2_chain_t *chain;
1546         hammer2_key_t lbase;
1547
1548         ipdata = &ip->chain->data->ipdata;
1549
1550         if (ip->flags & HAMMER2_INODE_MTIME) {
1551                 ipdata = hammer2_chain_modify_ip(trans, ip, chainp, 0);
1552                 atomic_clear_int(&ip->flags, HAMMER2_INODE_MTIME);
1553                 ipdata->mtime = ip->mtime;
1554         }
1555         if ((ip->flags & HAMMER2_INODE_RESIZED) && ip->size < ipdata->size) {
1556                 ipdata = hammer2_chain_modify_ip(trans, ip, chainp, 0);
1557                 ipdata->size = ip->size;
1558                 atomic_clear_int(&ip->flags, HAMMER2_INODE_RESIZED);
1559
1560                 /*
1561                  * We must delete any chains beyond the EOF.  The chain
1562                  * straddling the EOF will be pending in the bioq.
1563                  */
1564                 lbase = (ipdata->size + HAMMER2_PBUFMASK64) &
1565                         ~HAMMER2_PBUFMASK64;
1566                 parent = hammer2_chain_lookup_init(ip->chain, 0);
1567                 chain = hammer2_chain_lookup(&parent,
1568                                              lbase, (hammer2_key_t)-1,
1569                                              HAMMER2_LOOKUP_NODATA);
1570                 while (chain) {
1571                         /*
1572                          * Degenerate embedded case, nothing to loop on
1573                          */
1574                         if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
1575                                 hammer2_chain_unlock(chain);
1576                                 break;
1577                         }
1578                         if (chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
1579                                 hammer2_chain_delete(trans, chain, 0);
1580                         }
1581                         chain = hammer2_chain_next(&parent, chain,
1582                                                    lbase, (hammer2_key_t)-1,
1583                                                    HAMMER2_LOOKUP_NODATA);
1584                 }
1585                 hammer2_chain_lookup_done(parent);
1586         } else
1587         if ((ip->flags & HAMMER2_INODE_RESIZED) && ip->size > ipdata->size) {
1588                 ipdata = hammer2_chain_modify_ip(trans, ip, chainp, 0);
1589                 ipdata->size = ip->size;
1590                 atomic_clear_int(&ip->flags, HAMMER2_INODE_RESIZED);
1591
1592                 /*
1593                  * When resizing larger we may not have any direct-data
1594                  * available.
1595                  */
1596                 if ((ipdata->op_flags & HAMMER2_OPFLAG_DIRECTDATA) &&
1597                     ip->size > HAMMER2_EMBEDDED_BYTES) {
1598                         ipdata->op_flags &= ~HAMMER2_OPFLAG_DIRECTDATA;
1599                         bzero(&ipdata->u.blockset, sizeof(ipdata->u.blockset));
1600                 }
1601         }
1602 }