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