hammer2 - flush sequencing part 4 - stabilization and cleanup, flush sep
[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.  This routine typically locates or allocates the inode, assigns
285  * ip->chain (adding a ref to chain if necessary), and returns the inode.
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                 /*
330                  * Consolidated nip/nip->chain is locked (chain locked
331                  * by caller).
332                  */
333                 return nip;
334         }
335
336         /*
337          * We couldn't find the inode number, create a new inode.
338          */
339         nip = kmalloc(sizeof(*nip), hmp->minode, M_WAITOK | M_ZERO);
340         nip->inum = chain->data->ipdata.inum;
341         nip->chain = chain;
342         hammer2_chain_ref(chain);               /* nip->chain */
343         nip->pip = dip;                         /* can be NULL */
344         if (dip)
345                 hammer2_inode_ref(dip); /* ref dip for nip->pip */
346
347         nip->pmp = pmp;
348         nip->hmp = hmp;
349
350         /*
351          * ref and lock on nip gives it state compatible to after a
352          * hammer2_inode_lock_ex() call.
353          */
354         nip->refs = 1;
355         ccms_cst_init(&nip->topo_cst, &nip->chain);
356         ccms_thread_lock(&nip->topo_cst, CCMS_STATE_EXCLUSIVE);
357         /* combination of thread lock and chain lock == inode lock */
358
359         /*
360          * Attempt to add the inode.  If it fails we raced another inode
361          * get.  Undo all the work and try again.
362          */
363         if (pmp) {
364                 spin_lock(&pmp->inum_spin);
365                 if (RB_INSERT(hammer2_inode_tree, &pmp->inum_tree, nip)) {
366                         spin_unlock(&pmp->inum_spin);
367                         ccms_thread_unlock(&nip->topo_cst);
368                         hammer2_inode_drop(nip);
369                         goto again;
370                 }
371                 atomic_set_int(&nip->flags, HAMMER2_INODE_ONRBTREE);
372                 spin_unlock(&pmp->inum_spin);
373         }
374
375         return (nip);
376 }
377
378 /*
379  * Put away an inode, unlocking it and disconnecting it from its chain.
380  *
381  * The inode must be exclusively locked on call and non-recursed, with
382  * at least 2 refs (one belonging to the exclusive lock, and one additional
383  * ref belonging to the caller).
384  *
385  * Upon return the inode typically has one ref remaining which the caller
386  * drops.
387  */
388 void
389 hammer2_inode_put(hammer2_inode_t *ip, hammer2_chain_t *chain)
390 {
391         hammer2_inode_t *pip;
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_chain_drop(chain);      /* from *_get() */
401         }
402
403         /*
404          * Disconnect pip
405          */
406         pip = ip->pip;
407         ip->pip = NULL;
408         hammer2_inode_unlock_ex(ip, chain);
409
410         /*
411          * Cleanup delayed actions
412          */
413         if (pip)
414                 hammer2_inode_drop(pip);
415 }
416
417 /*
418  * Create a new inode in the specified directory using the vattr to
419  * figure out the type of inode.
420  *
421  * If no error occurs the new inode with its chain locked is returned in
422  * *nipp, otherwise an error is returned and *nipp is set to NULL.
423  *
424  * If vap and/or cred are NULL the related fields are not set and the
425  * inode type defaults to a directory.  This is used when creating PFSs
426  * under the super-root, so the inode number is set to 1 in this case.
427  *
428  * dip is not locked on entry.
429  */
430 hammer2_inode_t *
431 hammer2_inode_create(hammer2_trans_t *trans, hammer2_inode_t *dip,
432                      struct vattr *vap, struct ucred *cred,
433                      const uint8_t *name, size_t name_len,
434                      hammer2_chain_t **chainp, int *errorp)
435 {
436         hammer2_inode_data_t *dipdata;
437         hammer2_inode_data_t *nipdata;
438         hammer2_mount_t *hmp;
439         hammer2_chain_t *chain;
440         hammer2_chain_t *parent;
441         hammer2_inode_t *nip;
442         hammer2_key_t lhc;
443         int error;
444         uid_t xuid;
445         uuid_t dip_uid;
446         uuid_t dip_gid;
447         uint32_t dip_mode;
448
449         hmp = dip->hmp;
450         lhc = hammer2_dirhash(name, name_len);
451         *errorp = 0;
452
453         /*
454          * Locate the inode or indirect block to create the new
455          * entry in.  At the same time check for key collisions
456          * and iterate until we don't get one.
457          *
458          * NOTE: hidden inodes do not have iterators.
459          */
460 retry:
461         parent = hammer2_inode_lock_ex(dip);
462         dipdata = &dip->chain->data->ipdata;
463         dip_uid = dipdata->uid;
464         dip_gid = dipdata->gid;
465         dip_mode = dipdata->mode;
466
467         error = 0;
468         while (error == 0) {
469                 chain = hammer2_chain_lookup(&parent, lhc, lhc, 0);
470                 if (chain == NULL)
471                         break;
472                 if ((lhc & HAMMER2_DIRHASH_VISIBLE) == 0)
473                         error = ENOSPC;
474                 if ((lhc & HAMMER2_DIRHASH_LOMASK) == HAMMER2_DIRHASH_LOMASK)
475                         error = ENOSPC;
476                 hammer2_chain_unlock(chain);
477                 chain = NULL;
478                 ++lhc;
479         }
480         if (error == 0) {
481                 error = hammer2_chain_create(trans, &parent, &chain,
482                                              lhc, 0,
483                                              HAMMER2_BREF_TYPE_INODE,
484                                              HAMMER2_INODE_BYTES);
485         }
486
487         /*
488          * Cleanup and handle retries.
489          */
490         if (error == EAGAIN) {
491                 hammer2_chain_ref(parent);
492                 hammer2_inode_unlock_ex(dip, parent);
493                 hammer2_chain_wait(parent);
494                 hammer2_chain_drop(parent);
495                 goto retry;
496         }
497         hammer2_inode_unlock_ex(dip, parent);
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         *chainp = chain;
574
575         return (nip);
576 }
577
578 /*
579  * chain may have been moved around by the create.
580  */
581 static
582 void
583 hammer2_chain_refactor(hammer2_chain_t **chainp)
584 {
585         hammer2_chain_t *chain = *chainp;
586         hammer2_chain_t *tmp;
587
588         while (chain->duplink && (chain->flags & HAMMER2_CHAIN_DELETED)) {
589                 tmp = chain->duplink;
590                 while (tmp->duplink && (tmp->flags & HAMMER2_CHAIN_DELETED))
591                         tmp = tmp->duplink;
592                 hammer2_chain_ref(chain);
593                 hammer2_chain_unlock(chain);
594                 hammer2_chain_lock(tmp, HAMMER2_RESOLVE_ALWAYS);
595                 hammer2_chain_drop(chain);
596                 chain = tmp;
597                 *chainp = chain;
598         }
599 }
600
601
602 /*
603  * ochain represents the target file inode.  We need to move it to the
604  * specified common parent directory (dip) and rename it to a special
605  * invisible "0xINODENUMBER" filename.
606  *
607  * We use chain_duplicate and duplicate ochain at the new location,
608  * renaming it appropriately.  We create a temporary chain and
609  * then delete it to placemark where the duplicate will go.  Both of
610  * these use the inode number for (lhc) (the key), generating the
611  * invisible filename.
612  */
613 static
614 hammer2_chain_t *
615 hammer2_hardlink_shiftup(hammer2_trans_t *trans, hammer2_chain_t **ochainp,
616                         hammer2_inode_t *dip, int *errorp)
617 {
618         hammer2_inode_data_t *nipdata;
619         hammer2_mount_t *hmp;
620         hammer2_chain_t *parent;
621         hammer2_chain_t *ochain;
622         hammer2_chain_t *nchain;
623         hammer2_chain_t *tmp;
624         hammer2_key_t lhc;
625         hammer2_blockref_t bref;
626
627         ochain = *ochainp;
628         *errorp = 0;
629         hmp = dip->hmp;
630         lhc = ochain->data->ipdata.inum;
631         KKASSERT((lhc & HAMMER2_DIRHASH_VISIBLE) == 0);
632
633         /*
634          * Locate the inode or indirect block to create the new
635          * entry in.  lhc represents the inode number so there is
636          * no collision iteration.
637          *
638          * There should be no key collisions with invisible inode keys.
639          */
640 retry:
641         parent = hammer2_chain_lookup_init(dip->chain, 0);
642         nchain = hammer2_chain_lookup(&parent, lhc, lhc, 0);
643         if (nchain) {
644                 kprintf("X3 chain %p parent %p dip %p dip->chain %p\n",
645                         nchain, parent, dip, dip->chain);
646                 hammer2_chain_unlock(nchain);
647                 nchain = NULL;
648                 *errorp = ENOSPC;
649 #if 0
650                 Debugger("X3");
651 #endif
652         }
653
654         /*
655          * Create entry in common parent directory using the seek position
656          * calculated above.
657          */
658         if (*errorp == 0) {
659                 KKASSERT(nchain == NULL);
660                 *errorp = hammer2_chain_create(trans, &parent, &nchain,
661                                                lhc, 0,
662                                                HAMMER2_BREF_TYPE_INODE,/* n/a */
663                                                HAMMER2_INODE_BYTES);   /* n/a */
664                 hammer2_chain_refactor(&ochain);
665                 *ochainp = ochain;
666         }
667
668         /*
669          * Cleanup and handle retries.
670          */
671         if (*errorp == EAGAIN) {
672                 hammer2_chain_ref(parent);
673                 hammer2_chain_lookup_done(parent);
674                 hammer2_chain_wait(parent);
675                 hammer2_chain_drop(parent);
676                 goto retry;
677         }
678
679         /*
680          * Handle the error case
681          */
682         if (*errorp) {
683                 KKASSERT(nchain == NULL);
684                 hammer2_chain_lookup_done(parent);
685                 return (NULL);
686         }
687
688         /*
689          * Use chain as a placeholder for (lhc), delete it and replace
690          * it with our duplication.
691          *
692          * Gain a second lock on ochain for the duplication function to
693          * unlock, maintain the caller's original lock across the call.
694          *
695          * This is a bit messy.
696          */
697         hammer2_chain_delete(trans, parent, nchain);
698         hammer2_chain_lock(ochain, HAMMER2_RESOLVE_ALWAYS);
699         tmp = ochain;
700         bref = tmp->bref;
701         bref.key = lhc;                 /* invisible dir entry key */
702         bref.keybits = 0;
703         hammer2_chain_duplicate(trans, parent, nchain->index, &tmp, &bref);
704         hammer2_chain_lookup_done(parent);
705         hammer2_chain_unlock(nchain);   /* no longer needed */
706
707         /*
708          * Now set chain to our duplicate and modify it appropriately.
709          *
710          * Directory entries are inodes but this is a hidden hardlink
711          * target.  The name isn't used but to ease debugging give it
712          * a name after its inode number.
713          */
714         nchain = tmp;
715         tmp = NULL;     /* safety */
716
717         hammer2_chain_modify(trans, &nchain, HAMMER2_MODIFY_ASSERTNOCOPY);
718         nipdata = &nchain->data->ipdata;
719         ksnprintf(nipdata->filename, sizeof(nipdata->filename),
720                   "0x%016jx", (intmax_t)nipdata->inum);
721         nipdata->name_len = strlen(nipdata->filename);
722         nipdata->name_key = lhc;
723
724         return (nchain);
725 }
726
727 /*
728  * Connect the target inode represented by (*chainp) to the media topology
729  * at (dip, name, len).
730  *
731  * If hlink is TRUE this function creates an OBJTYPE_HARDLINK directory
732  * entry instead of connecting (*chainp).
733  *
734  * If hlink is FALSE this function uses chain_duplicate() to make a copy
735  * if (*chainp) in the directory entry.  (*chainp) is likely to be deleted
736  * by the caller in this case (e.g. rename).
737  */
738 int
739 hammer2_inode_connect(hammer2_trans_t *trans, int hlink,
740                       hammer2_inode_t *dip, hammer2_chain_t **chainp,
741                       const uint8_t *name, size_t name_len)
742 {
743         hammer2_inode_data_t *ipdata;
744         hammer2_mount_t *hmp;
745         hammer2_chain_t *nchain;
746         hammer2_chain_t *parent;
747         hammer2_chain_t *ochain;
748         hammer2_key_t lhc;
749         int error;
750
751         hmp = dip->hmp;
752
753         ochain = *chainp;
754
755         /*
756          * Since ochain is either disconnected from the topology or represents
757          * a hardlink terminus which is always a parent of or equal to dip,
758          * we should be able to safely lock dip->chain for our setup.
759          */
760         parent = hammer2_chain_lookup_init(dip->chain, 0);
761
762         lhc = hammer2_dirhash(name, name_len);
763
764         /*
765          * Locate the inode or indirect block to create the new
766          * entry in.  At the same time check for key collisions
767          * and iterate until we don't get one.
768          */
769         error = 0;
770         while (error == 0) {
771                 nchain = hammer2_chain_lookup(&parent, lhc, lhc, 0);
772                 if (nchain == NULL)
773                         break;
774                 if ((lhc & HAMMER2_DIRHASH_LOMASK) == HAMMER2_DIRHASH_LOMASK)
775                         error = ENOSPC;
776                 hammer2_chain_unlock(nchain);
777                 nchain = NULL;
778                 ++lhc;
779         }
780
781         if (error == 0) {
782                 if (hlink) {
783                         /*
784                          * Hardlink pointer needed, create totally fresh
785                          * directory entry.
786                          */
787                         KKASSERT(nchain == NULL);
788                         error = hammer2_chain_create(trans, &parent, &nchain,
789                                                      lhc, 0,
790                                                      HAMMER2_BREF_TYPE_INODE,
791                                                      HAMMER2_INODE_BYTES);
792                         hammer2_chain_refactor(&ochain);
793                 } else {
794                         /*
795                          * Reconnect the original chain and rename.  Use
796                          * chain_duplicate().  The caller will likely delete
797                          * or has already deleted the original chain in
798                          * this case.
799                          */
800                         nchain = ochain;
801                         ochain = NULL;
802                         hammer2_chain_duplicate(trans, NULL, -1, &nchain, NULL);
803                         error = hammer2_chain_create(trans, &parent, &nchain,
804                                                      lhc, 0,
805                                                      HAMMER2_BREF_TYPE_INODE,
806                                                      HAMMER2_INODE_BYTES);
807                 }
808         }
809
810         /*
811          * Unlock stuff.
812          */
813         KKASSERT(error != EAGAIN);
814         hammer2_chain_lookup_done(parent);
815         parent = NULL;
816
817         /*
818          * nchain should be NULL on error, leave ochain (== *chainp) alone.
819          */
820         if (error) {
821                 KKASSERT(nchain == NULL);
822                 return (error);
823         }
824
825         /*
826          * Directory entries are inodes so if the name has changed we have
827          * to update the inode.
828          *
829          * When creating an OBJTYPE_HARDLINK entry remember to unlock the
830          * chain, the caller will access the hardlink via the actual hardlink
831          * target file and not the hardlink pointer entry, so we must still
832          * return ochain.
833          */
834         if (hlink && hammer2_hardlink_enable >= 0) {
835                 /*
836                  * Create the HARDLINK pointer.  oip represents the hardlink
837                  * target in this situation.
838                  *
839                  * We will return ochain (the hardlink target).
840                  */
841                 hammer2_chain_modify(trans, &nchain,
842                                      HAMMER2_MODIFY_ASSERTNOCOPY);
843                 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
844                 ipdata = &nchain->data->ipdata;
845                 bcopy(name, ipdata->filename, name_len);
846                 ipdata->name_key = lhc;
847                 ipdata->name_len = name_len;
848                 ipdata->target_type = ochain->data->ipdata.type;
849                 ipdata->type = HAMMER2_OBJTYPE_HARDLINK;
850                 ipdata->inum = ochain->data->ipdata.inum;
851                 ipdata->nlinks = 1;
852                 hammer2_chain_unlock(nchain);
853                 nchain = ochain;
854                 ochain = NULL;
855         } else if (hlink && hammer2_hardlink_enable < 0) {
856                 /*
857                  * Create a snapshot (hardlink fake mode for debugging).
858                  * (ochain already flushed above so we can just copy the
859                  * bref XXX).
860                  *
861                  * Since this is a snapshot we return nchain in the fake
862                  * hardlink case.
863                  */
864                 hammer2_chain_modify(trans, &nchain,
865                                      HAMMER2_MODIFY_ASSERTNOCOPY);
866                 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
867                 ipdata = &nchain->data->ipdata;
868                 *ipdata = ochain->data->ipdata;
869                 bcopy(name, ipdata->filename, name_len);
870                 ipdata->name_key = lhc;
871                 ipdata->name_len = name_len;
872                 kprintf("created fake hardlink %*.*s\n",
873                         (int)name_len, (int)name_len, name);
874         } else {
875                 /*
876                  * nchain is a duplicate of ochain at the new location.
877                  * We must fixup the name stored in oip.  The bref key
878                  * has already been set up.
879                  */
880                 hammer2_chain_modify(trans, &nchain,
881                                      HAMMER2_MODIFY_ASSERTNOCOPY);
882                 ipdata = &nchain->data->ipdata;
883
884                 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
885                 bcopy(name, ipdata->filename, name_len);
886                 ipdata->name_key = lhc;
887                 ipdata->name_len = name_len;
888                 ipdata->nlinks = 1;
889         }
890
891         /*
892          * We are replacing ochain with nchain, unlock ochain.  In the
893          * case where ochain is left unchanged the code above sets
894          * nchain to ochain and ochain to NULL, resulting in a NOP here.
895          */
896         if (ochain)
897                 hammer2_chain_unlock(ochain);
898         *chainp = nchain;
899
900         return (0);
901 }
902
903 /*
904  * Caller must hold exactly ONE exclusive lock on the inode.  *nchainp
905  * must be exclusive locked (its own exclusive lock even if it is the
906  * same as ip->chain).
907  *
908  * This function replaces ip->chain.  The exclusive lock on the passed
909  * nchain is inherited by the inode and the caller becomes responsible
910  * for unlocking it when the caller unlocks the inode.
911  *
912  * ochain was locked by the caller indirectly via the inode lock.  Since
913  * ip->chain is being repointed, we become responsible for cleaning up
914  * that lock.
915  *
916  * Return *nchainp = NULL as a safety.
917  */
918 void
919 hammer2_inode_repoint(hammer2_inode_t *ip, hammer2_inode_t *pip,
920                       hammer2_chain_t *nchain)
921 {
922         hammer2_chain_t *ochain;
923         hammer2_inode_t *opip;
924
925         /*
926          * ip->chain points to the hardlink target, not the hardlink psuedo
927          * inode.  Do not repoint nchain to the pseudo-node.
928          */
929         if (nchain->data->ipdata.type == HAMMER2_OBJTYPE_HARDLINK)
930                 return;
931
932         /*
933          * Repoint ip->chain if necessary.
934          *
935          * (Inode must be locked exclusively by parent)
936          */
937         ochain = ip->chain;
938         if (ochain != nchain) {
939                 hammer2_chain_ref(nchain);              /* for ip->chain */
940                 ip->chain = nchain;
941                 if (ochain) {
942                         hammer2_chain_unlock(ochain);
943                         hammer2_chain_drop(ochain);     /* for ip->chain */
944                 }
945                 /* replace locked chain in ip (additional lock) */
946                 hammer2_chain_lock(nchain, HAMMER2_RESOLVE_ALWAYS);
947         }
948         if (ip->pip != pip) {
949                 opip = ip->pip;
950                 if (pip)
951                         hammer2_inode_ref(pip);
952                 ip->pip = pip;
953                 if (opip)
954                         hammer2_inode_drop(opip);
955         }
956 }
957
958 /*
959  * Unlink the file from the specified directory inode.  The directory inode
960  * does not need to be locked.  The caller should pass a non-NULL (ip)
961  * representing the object being removed only if the related vnode is
962  * potentially inactive (not referenced in the caller's active path),
963  * so we can vref/vrele it to trigger the VOP_INACTIVE path and properly
964  * recycle it.
965  *
966  * isdir determines whether a directory/non-directory check should be made.
967  * No check is made if isdir is set to -1.
968  *
969  * NOTE!  This function does not prevent the underlying file from still
970  *        being used if it has other refs (such as from an inode, or if it's
971  *        chain is manually held).  However, the caller is responsible for
972  *        fixing up ip->chain if e.g. a rename occurs (see chain_duplicate()).
973  */
974 int
975 hammer2_unlink_file(hammer2_trans_t *trans, hammer2_inode_t *dip,
976                     const uint8_t *name, size_t name_len,
977                     int isdir, int *hlinkp)
978 {
979         hammer2_inode_data_t *ipdata;
980         hammer2_mount_t *hmp;
981         hammer2_chain_t *parent;
982         hammer2_chain_t *ochain;
983         hammer2_chain_t *chain;
984         hammer2_chain_t *dparent;
985         hammer2_chain_t *dchain;
986         hammer2_key_t lhc;
987         int error;
988         int parent_ref;
989         uint8_t type;
990
991         parent_ref = 0;
992         error = 0;
993         ochain = NULL;
994         hmp = dip->hmp;
995         lhc = hammer2_dirhash(name, name_len);
996
997         /*
998          * Search for the filename in the directory
999          */
1000         if (hlinkp)
1001                 *hlinkp = 0;
1002         parent = hammer2_inode_lock_ex(dip);
1003         chain = hammer2_chain_lookup(&parent,
1004                                      lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1005                                      0);
1006         while (chain) {
1007                 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
1008                     name_len == chain->data->ipdata.name_len &&
1009                     bcmp(name, chain->data->ipdata.filename, name_len) == 0) {
1010                         break;
1011                 }
1012                 chain = hammer2_chain_next(&parent, chain,
1013                                            lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1014                                            0);
1015         }
1016         hammer2_inode_unlock_ex(dip, NULL);     /* retain parent */
1017
1018         /*
1019          * Not found or wrong type (isdir < 0 disables the type check).
1020          * If a hardlink pointer, type checks use the hardlink target.
1021          */
1022         if (chain == NULL) {
1023                 error = ENOENT;
1024                 goto done;
1025         }
1026         if ((type = chain->data->ipdata.type) == HAMMER2_OBJTYPE_HARDLINK) {
1027                 if (hlinkp)
1028                         *hlinkp = 1;
1029                 type = chain->data->ipdata.target_type;
1030         }
1031
1032         if (type == HAMMER2_OBJTYPE_DIRECTORY && isdir == 0) {
1033                 error = ENOTDIR;
1034                 goto done;
1035         }
1036         if (type != HAMMER2_OBJTYPE_DIRECTORY && isdir >= 1) {
1037                 error = EISDIR;
1038                 goto done;
1039         }
1040
1041         /*
1042          * Hardlink must be resolved.  We can't hold parent locked while we
1043          * do this or we could deadlock.
1044          *
1045          * On success chain will be adjusted to point at the hardlink target
1046          * and ochain will point to the hardlink pointer in the original
1047          * directory.  Otherwise chain remains pointing to the original.
1048          */
1049         if (chain->data->ipdata.type == HAMMER2_OBJTYPE_HARDLINK) {
1050                 KKASSERT(parent_ref == 0);
1051                 hammer2_chain_unlock(parent);
1052                 parent = NULL;
1053                 error = hammer2_hardlink_find(dip, &chain, &ochain);
1054         }
1055
1056         /*
1057          * If this is a directory the directory must be empty.  However, if
1058          * isdir < 0 we are doing a rename and the directory does not have
1059          * to be empty, and if isdir > 1 we are deleting a PFS/snapshot
1060          * and the directory does not have to be empty.
1061          *
1062          * NOTE: We check the full key range here which covers both visible
1063          *       and invisible entries.  Theoretically there should be no
1064          *       invisible (hardlink target) entries if there are no visible
1065          *       entries.
1066          */
1067         if (type == HAMMER2_OBJTYPE_DIRECTORY && isdir == 1) {
1068                 dparent = hammer2_chain_lookup_init(chain, 0);
1069                 dchain = hammer2_chain_lookup(&dparent,
1070                                               0, (hammer2_key_t)-1,
1071                                               HAMMER2_LOOKUP_NODATA);
1072                 if (dchain) {
1073                         hammer2_chain_unlock(dchain);
1074                         hammer2_chain_lookup_done(dparent);
1075                         error = ENOTEMPTY;
1076                         goto done;
1077                 }
1078                 hammer2_chain_lookup_done(dparent);
1079                 dparent = NULL;
1080                 /* dchain NULL */
1081         }
1082
1083         /*
1084          * Ok, we can now unlink the chain.  We always decrement nlinks even
1085          * if the entry can be deleted in case someone has the file open and
1086          * does an fstat().
1087          *
1088          * The chain itself will no longer be in the on-media topology but
1089          * can still be flushed to the media (e.g. if an open descriptor
1090          * remains).  When the last vnode/ip ref goes away the chain will
1091          * be marked unmodified, avoiding any further (now unnecesary) I/O.
1092          *
1093          * A non-NULL ochain indicates a hardlink.
1094          */
1095         if (ochain) {
1096                 /*
1097                  * Delete the original hardlink pointer.
1098                  *
1099                  * NOTE: parent from above is NULL when ochain != NULL
1100                  *       so we can reuse it.
1101                  */
1102                 hammer2_chain_lock(ochain, HAMMER2_RESOLVE_ALWAYS);
1103                 parent_ref = 1;
1104                 for (;;) {
1105                         parent = ochain->parent;
1106                         hammer2_chain_ref(parent);
1107                         hammer2_chain_unlock(ochain);
1108                         hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
1109                         hammer2_chain_lock(ochain, HAMMER2_RESOLVE_ALWAYS);
1110                         if (ochain->parent == parent)
1111                                 break;
1112                         hammer2_chain_unlock(parent);
1113                         hammer2_chain_drop(parent);
1114                 }
1115
1116                 hammer2_chain_delete(trans, parent, ochain);
1117                 hammer2_chain_unlock(ochain);
1118                 hammer2_chain_unlock(parent);
1119                 hammer2_chain_drop(parent);
1120                 parent = NULL;
1121
1122                 /*
1123                  * Then decrement nlinks on hardlink target, deleting
1124                  * the target when nlinks drops to 0.
1125                  */
1126                 if (chain->data->ipdata.nlinks == 1) {
1127                         dparent = chain->parent;
1128                         hammer2_chain_ref(chain);
1129                         hammer2_chain_unlock(chain);
1130                         hammer2_chain_lock(dparent, HAMMER2_RESOLVE_ALWAYS);
1131                         hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS);
1132                         hammer2_chain_drop(chain);
1133                         hammer2_chain_modify(trans, &chain, 0);
1134                         --chain->data->ipdata.nlinks;
1135                         hammer2_chain_delete(trans, dparent, chain);
1136                         hammer2_chain_unlock(dparent);
1137                 } else {
1138                         hammer2_chain_modify(trans, &chain, 0);
1139                         --chain->data->ipdata.nlinks;
1140                 }
1141         } else {
1142                 /*
1143                  * Otherwise this was not a hardlink and we can just
1144                  * remove the entry and decrement nlinks.
1145                  *
1146                  * NOTE: *_get() integrates chain's lock into the inode lock.
1147                  */
1148                 hammer2_chain_modify(trans, &chain, 0);
1149                 ipdata = &chain->data->ipdata;
1150                 --ipdata->nlinks;
1151                 hammer2_chain_delete(trans, parent, chain);
1152         }
1153
1154         error = 0;
1155 done:
1156         if (chain)
1157                 hammer2_chain_unlock(chain);
1158         if (parent) {
1159                 hammer2_chain_lookup_done(parent);
1160                 if (parent_ref)
1161                         hammer2_chain_drop(parent);
1162         }
1163         if (ochain)
1164                 hammer2_chain_drop(ochain);
1165
1166         return error;
1167 }
1168
1169 /*
1170  * Calculate the allocation size for the file fragment straddling EOF
1171  */
1172 int
1173 hammer2_inode_calc_alloc(hammer2_key_t filesize)
1174 {
1175         int frag = (int)filesize & HAMMER2_PBUFMASK;
1176         int radix;
1177
1178         if (frag == 0)
1179                 return(0);
1180         for (radix = HAMMER2_MINALLOCRADIX; frag > (1 << radix); ++radix)
1181                 ;
1182         return (radix);
1183 }
1184
1185 /*
1186  * Given an exclusively locked inode we consolidate its chain for hardlink
1187  * creation, adding (nlinks) to the file's link count and potentially
1188  * relocating the inode to a directory common to ip->pip and tdip.
1189  *
1190  * Replaces (*chainp) if consolidation occurred, unlocking the old chain
1191  * and returning a new locked chain.
1192  *
1193  * NOTE!  This function will also replace 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         hmp = tdip->hmp;
1210
1211         chain = *chainp;
1212         if (nlinks == 0 &&                      /* no hardlink needed */
1213             (chain->data->ipdata.name_key & HAMMER2_DIRHASH_VISIBLE)) {
1214                 return (0);
1215         }
1216         if (hammer2_hardlink_enable < 0) {      /* fake hardlinks */
1217                 return (0);
1218         }
1219
1220         if (hammer2_hardlink_enable == 0) {     /* disallow hardlinks */
1221                 hammer2_chain_unlock(chain);
1222                 *chainp = NULL;
1223                 return (ENOTSUP);
1224         }
1225
1226         /*
1227          * cdip will be returned with a ref, but not locked.
1228          */
1229         fdip = ip->pip;
1230         cdip = hammer2_inode_common_parent(fdip, tdip);
1231
1232         /*
1233          * If no change in the hardlink's target directory is required and
1234          * this is already a hardlink target, all we need to do is adjust
1235          * the link count.
1236          *
1237          * XXX The common parent is a big wiggly due to duplication from
1238          *     renames.  Compare the core (RBTREE) pointer instead of the
1239          *     ip's.
1240          */
1241         if (cdip == fdip &&
1242             (chain->data->ipdata.name_key & HAMMER2_DIRHASH_VISIBLE) == 0) {
1243                 if (nlinks) {
1244                         hammer2_chain_modify(trans, &chain, 0);
1245                         chain->data->ipdata.nlinks += nlinks;
1246                 }
1247                 error = 0;
1248                 goto done;
1249         }
1250
1251         /*
1252          * We either have to move an existing hardlink target or we have
1253          * to create a fresh hardlink target.
1254          *
1255          * Hardlink targets are hidden inodes in a parent directory common
1256          * to all directory entries referencing the hardlink.
1257          */
1258         nchain = hammer2_hardlink_shiftup(trans, &chain, cdip, &error);
1259
1260         if (error == 0) {
1261                 /*
1262                  * Bump nlinks on duplicated hidden inode, repoint
1263                  * ip->chain.
1264                  */
1265                 hammer2_chain_modify(trans, &nchain, 0);
1266                 nchain->data->ipdata.nlinks += nlinks;
1267                 hammer2_inode_repoint(ip, cdip, nchain);
1268
1269                 /*
1270                  * If the old chain is not a hardlink target then replace
1271                  * it with a OBJTYPE_HARDLINK pointer.
1272                  *
1273                  * If the old chain IS a hardlink target then delete it.
1274                  */
1275                 if (chain->data->ipdata.name_key & HAMMER2_DIRHASH_VISIBLE) {
1276                         /*
1277                          * Replace original non-hardlink that's been dup'd
1278                          * with a special hardlink directory entry.  We must
1279                          * set the DIRECTDATA flag to prevent sub-chains
1280                          * from trying to synchronize to the inode if the
1281                          * file is extended afterwords.
1282                          */
1283                         hammer2_chain_modify(trans, &chain, 0);
1284                         ipdata = &chain->data->ipdata;
1285                         ipdata->target_type = ipdata->type;
1286                         ipdata->type = HAMMER2_OBJTYPE_HARDLINK;
1287                         ipdata->uflags = 0;
1288                         ipdata->rmajor = 0;
1289                         ipdata->rminor = 0;
1290                         ipdata->ctime = 0;
1291                         ipdata->mtime = 0;
1292                         ipdata->atime = 0;
1293                         ipdata->btime = 0;
1294                         bzero(&ipdata->uid, sizeof(ipdata->uid));
1295                         bzero(&ipdata->gid, sizeof(ipdata->gid));
1296                         ipdata->op_flags = HAMMER2_OPFLAG_DIRECTDATA;
1297                         ipdata->cap_flags = 0;
1298                         ipdata->mode = 0;
1299                         ipdata->size = 0;
1300                         ipdata->nlinks = 1;
1301                         ipdata->iparent = 0;    /* XXX */
1302                         ipdata->pfs_type = 0;
1303                         ipdata->pfs_inum = 0;
1304                         bzero(&ipdata->pfs_clid, sizeof(ipdata->pfs_clid));
1305                         bzero(&ipdata->pfs_fsid, sizeof(ipdata->pfs_fsid));
1306                         ipdata->data_quota = 0;
1307                         ipdata->data_count = 0;
1308                         ipdata->inode_quota = 0;
1309                         ipdata->inode_count = 0;
1310                         ipdata->attr_tid = 0;
1311                         ipdata->dirent_tid = 0;
1312                         bzero(&ipdata->u, sizeof(ipdata->u));
1313                         /* XXX transaction ids */
1314                 } else {
1315                         kprintf("DELETE INVISIBLE\n");
1316                         for (;;) {
1317                                 parent = chain->parent;
1318                                 hammer2_chain_ref(parent);
1319                                 hammer2_chain_ref(chain);
1320                                 hammer2_chain_unlock(chain);
1321                                 hammer2_chain_lock(parent,
1322                                                    HAMMER2_RESOLVE_ALWAYS);
1323                                 hammer2_chain_lock(chain,
1324                                                    HAMMER2_RESOLVE_ALWAYS);
1325                                 hammer2_chain_drop(chain);
1326                                 if (chain->parent == parent)
1327                                         break;
1328                                 hammer2_chain_unlock(parent);
1329                                 hammer2_chain_drop(parent);
1330                         }
1331                         hammer2_chain_delete(trans, parent, chain);
1332                         hammer2_chain_unlock(parent);
1333                         hammer2_chain_drop(parent);
1334                 }
1335
1336                 /*
1337                  * Return the new chain.
1338                  */
1339                 hammer2_chain_unlock(chain);
1340                 chain = nchain;
1341         } else {
1342                 /*
1343                  * Return an error
1344                  */
1345                 hammer2_chain_unlock(chain);
1346                 chain = NULL;
1347         }
1348
1349         /*
1350          * Cleanup, chain/nchain already dealt with.
1351          */
1352 done:
1353         *chainp = chain;
1354         hammer2_inode_drop(cdip);
1355
1356         return (error);
1357 }
1358
1359 /*
1360  * If (*ochainp) is non-NULL it points to the forward OBJTYPE_HARDLINK
1361  * inode while (*chainp) points to the resolved (hidden hardlink
1362  * target) inode.  In this situation when nlinks is 1 we wish to
1363  * deconsolidate the hardlink, moving it back to the directory that now
1364  * represents the only remaining link.
1365  */
1366 int
1367 hammer2_hardlink_deconsolidate(hammer2_trans_t *trans,
1368                                hammer2_inode_t *dip,
1369                                hammer2_chain_t **chainp,
1370                                hammer2_chain_t **ochainp)
1371 {
1372         if (*ochainp == NULL)
1373                 return (0);
1374         /* XXX */
1375         return (0);
1376 }
1377
1378 /*
1379  * The caller presents a locked *chainp pointing to a HAMMER2_BREF_TYPE_INODE
1380  * with an obj_type of HAMMER2_OBJTYPE_HARDLINK.  This routine will gobble
1381  * the *chainp and return a new locked *chainp representing the file target
1382  * (the original *chainp will be unlocked).
1383  *
1384  * When a match is found the chain representing the original HARDLINK
1385  * will be returned in *ochainp with a ref, but not locked.
1386  *
1387  * When no match is found *chainp is set to NULL and EIO is returned.
1388  * (*ochainp) will still be set to the original chain with a ref but not
1389  * locked.
1390  */
1391 int
1392 hammer2_hardlink_find(hammer2_inode_t *dip, hammer2_chain_t **chainp,
1393                       hammer2_chain_t **ochainp)
1394 {
1395         hammer2_chain_t *chain = *chainp;
1396         hammer2_chain_t *parent;
1397         hammer2_inode_t *ip;
1398         hammer2_inode_t *pip;
1399         hammer2_key_t lhc;
1400
1401         pip = dip;
1402         hammer2_inode_ref(pip);         /* for loop */
1403         hammer2_chain_ref(chain);       /* for (*ochainp) */
1404         *ochainp = chain;
1405
1406         /*
1407          * Locate the hardlink.  pip is referenced and not locked,
1408          * ipp.
1409          *
1410          * chain is reused.
1411          */
1412         lhc = chain->data->ipdata.inum;
1413         hammer2_chain_unlock(chain);
1414         chain = NULL;
1415
1416         while ((ip = pip) != NULL) {
1417                 parent = hammer2_inode_lock_ex(ip);
1418                 hammer2_inode_drop(ip);                 /* loop */
1419                 KKASSERT(parent->bref.type == HAMMER2_BREF_TYPE_INODE);
1420                 chain = hammer2_chain_lookup(&parent, lhc, lhc, 0);
1421                 hammer2_chain_lookup_done(parent);      /* discard parent */
1422                 if (chain)
1423                         break;
1424                 pip = ip->pip;          /* safe, ip held locked */
1425                 if (pip)
1426                         hammer2_inode_ref(pip);         /* loop */
1427                 hammer2_inode_unlock_ex(ip, NULL);
1428         }
1429
1430         /*
1431          * chain is locked, ip is locked.  Unlock ip, return the locked
1432          * chain.  *ipp is already set w/a ref count and not locked.
1433          *
1434          * (parent is already unlocked).
1435          */
1436         if (ip)
1437                 hammer2_inode_unlock_ex(ip, NULL);
1438         *chainp = chain;
1439         if (chain) {
1440                 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_INODE);
1441                 /* already locked */
1442                 return (0);
1443         } else {
1444                 return (EIO);
1445         }
1446 }
1447
1448 /*
1449  * Find the directory common to both fdip and tdip, hold and return
1450  * its inode.
1451  */
1452 hammer2_inode_t *
1453 hammer2_inode_common_parent(hammer2_inode_t *fdip, hammer2_inode_t *tdip)
1454 {
1455         hammer2_inode_t *scan1;
1456         hammer2_inode_t *scan2;
1457
1458         /*
1459          * We used to have a depth field but it complicated matters too
1460          * much for directory renames.  So now its ugly.  Check for
1461          * simple cases before giving up and doing it the expensive way.
1462          *
1463          * XXX need a bottom-up topology stability lock
1464          */
1465         if (fdip == tdip || fdip == tdip->pip) {
1466                 hammer2_inode_ref(fdip);
1467                 return(fdip);
1468         }
1469         if (fdip->pip == tdip) {
1470                 hammer2_inode_ref(tdip);
1471                 return(tdip);
1472         }
1473
1474         /*
1475          * XXX not MPSAFE
1476          */
1477         for (scan1 = fdip; scan1->pmp == fdip->pmp; scan1 = scan1->pip) {
1478                 scan2 = tdip;
1479                 while (scan2->pmp == tdip->pmp) {
1480                         if (scan1 == scan2) {
1481                                 hammer2_inode_ref(scan1);
1482                                 return(scan1);
1483                         }
1484                         scan2 = scan2->pip;
1485                         if (scan2 == NULL)
1486                                 break;
1487                 }
1488         }
1489         panic("hammer2_inode_common_parent: no common parent %p %p\n",
1490               fdip, tdip);
1491         /* NOT REACHED */
1492         return(NULL);
1493 }