hammer2 - freemap and data check code
[dragonfly.git] / sys / vfs / hammer2 / hammer2_inode.c
1 /*
2  * Copyright (c) 2011-2014 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 #define INODE_DEBUG     0
45
46 static void hammer2_inode_move_to_hidden(hammer2_trans_t *trans,
47                                          hammer2_cluster_t **cparentp,
48                                          hammer2_cluster_t **clusterp,
49                                          hammer2_tid_t inum);
50
51 RB_GENERATE2(hammer2_inode_tree, hammer2_inode, rbnode, hammer2_inode_cmp,
52              hammer2_tid_t, inum);
53
54 int
55 hammer2_inode_cmp(hammer2_inode_t *ip1, hammer2_inode_t *ip2)
56 {
57         if (ip1->inum < ip2->inum)
58                 return(-1);
59         if (ip1->inum > ip2->inum)
60                 return(1);
61         return(0);
62 }
63
64 /*
65  * HAMMER2 inode locks
66  *
67  * HAMMER2 offers shared locks and exclusive locks on inodes.
68  *
69  * The inode locking function locks the inode itself, resolves any stale
70  * chains in the inode's cluster, and allocates a fresh copy of the
71  * cluster with 1 ref and all the underlying chains locked.  Duplication
72  * races are handled by this function.
73  *
74  * ip->cluster will be stable while the inode is locked.
75  *
76  * NOTE: We don't combine the inode/chain lock because putting away an
77  *       inode would otherwise confuse multiple lock holders of the inode.
78  *
79  * NOTE: In-memory inodes always point to hardlink targets (the actual file),
80  *       and never point to a hardlink pointer.
81  */
82 hammer2_cluster_t *
83 hammer2_inode_lock_ex(hammer2_inode_t *ip)
84 {
85         const hammer2_inode_data_t *ipdata;
86         hammer2_cluster_t *cluster;
87         hammer2_chain_t *chain;
88         int i;
89
90         hammer2_inode_ref(ip);
91         ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
92         cluster = hammer2_cluster_copy(&ip->cluster,
93                                        HAMMER2_CLUSTER_COPY_NOCHAINS);
94
95         ip->cluster.focus = NULL;
96         cluster->focus = NULL;
97
98         for (i = 0; i < cluster->nchains; ++i) {
99                 chain = ip->cluster.array[i];
100                 if (chain == NULL) {
101                         kprintf("inode_lock: %p: missing chain\n", ip);
102                         continue;
103                 }
104
105                 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS);
106                 cluster->array[i] = chain;
107                 if (cluster->focus == NULL)
108                         cluster->focus = chain;
109                 if (ip->cluster.focus == NULL)
110                         ip->cluster.focus = chain;
111         }
112
113         /*
114          * Returned cluster must resolve hardlink pointers
115          */
116         ipdata = &hammer2_cluster_data(cluster)->ipdata;
117         KKASSERT(ipdata->type != HAMMER2_OBJTYPE_HARDLINK);
118         /*
119         if (ipdata->type == HAMMER2_OBJTYPE_HARDLINK &&
120             (cluster->focus->flags & HAMMER2_CHAIN_DELETED) == 0) {
121                 error = hammer2_hardlink_find(ip->pip, NULL, cluster);
122                 KKASSERT(error == 0);
123         }
124         */
125
126         return (cluster);
127 }
128
129 void
130 hammer2_inode_unlock_ex(hammer2_inode_t *ip, hammer2_cluster_t *cluster)
131 {
132         if (cluster)
133                 hammer2_cluster_unlock(cluster);
134         ccms_thread_unlock(&ip->topo_cst);
135         hammer2_inode_drop(ip);
136 }
137
138 /*
139  * NOTE: We don't combine the inode/chain lock because putting away an
140  *       inode would otherwise confuse multiple lock holders of the inode.
141  *
142  *       Shared locks are especially sensitive to having too many shared
143  *       lock counts (from the same thread) on certain paths which might
144  *       need to upgrade them.  Only one count of a shared lock can be
145  *       upgraded.
146  */
147 hammer2_cluster_t *
148 hammer2_inode_lock_sh(hammer2_inode_t *ip)
149 {
150         const hammer2_inode_data_t *ipdata;
151         hammer2_cluster_t *cluster;
152         hammer2_chain_t *chain;
153         int i;
154
155         hammer2_inode_ref(ip);
156         cluster = hammer2_cluster_copy(&ip->cluster,
157                                        HAMMER2_CLUSTER_COPY_NOCHAINS);
158         ccms_thread_lock(&ip->topo_cst, CCMS_STATE_SHARED);
159
160         cluster->focus = NULL;
161
162         for (i = 0; i < cluster->nchains; ++i) {
163                 chain = ip->cluster.array[i];
164
165                 if (chain == NULL) {
166                         kprintf("inode_lock: %p: missing chain\n", ip);
167                         continue;
168                 }
169
170                 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS |
171                                           HAMMER2_RESOLVE_SHARED);
172                 cluster->array[i] = chain;
173                 if (cluster->focus == NULL)
174                         cluster->focus = chain;
175         }
176
177         /*
178          * Returned cluster must resolve hardlink pointers
179          */
180         ipdata = &hammer2_cluster_data(cluster)->ipdata;
181         KKASSERT(ipdata->type != HAMMER2_OBJTYPE_HARDLINK);
182         /*
183         if (ipdata->type == HAMMER2_OBJTYPE_HARDLINK &&
184             (cluster->focus->flags & HAMMER2_CHAIN_DELETED) == 0) {
185                 error = hammer2_hardlink_find(ip->pip, NULL, cluster);
186                 KKASSERT(error == 0);
187         }
188         */
189
190         return (cluster);
191 }
192
193 void
194 hammer2_inode_unlock_sh(hammer2_inode_t *ip, hammer2_cluster_t *cluster)
195 {
196         if (cluster)
197                 hammer2_cluster_unlock(cluster);
198         ccms_thread_unlock(&ip->topo_cst);
199         hammer2_inode_drop(ip);
200 }
201
202 ccms_state_t
203 hammer2_inode_lock_temp_release(hammer2_inode_t *ip)
204 {
205         return(ccms_thread_lock_temp_release(&ip->topo_cst));
206 }
207
208 void
209 hammer2_inode_lock_temp_restore(hammer2_inode_t *ip, ccms_state_t ostate)
210 {
211         ccms_thread_lock_temp_restore(&ip->topo_cst, ostate);
212 }
213
214 ccms_state_t
215 hammer2_inode_lock_upgrade(hammer2_inode_t *ip)
216 {
217         return(ccms_thread_lock_upgrade(&ip->topo_cst));
218 }
219
220 void
221 hammer2_inode_lock_downgrade(hammer2_inode_t *ip, ccms_state_t ostate)
222 {
223         ccms_thread_lock_downgrade(&ip->topo_cst, ostate);
224 }
225
226 /*
227  * Lookup an inode by inode number
228  */
229 hammer2_inode_t *
230 hammer2_inode_lookup(hammer2_pfsmount_t *pmp, hammer2_tid_t inum)
231 {
232         hammer2_inode_t *ip;
233
234         KKASSERT(pmp);
235         if (pmp->spmp_hmp) {
236                 ip = NULL;
237         } else {
238                 spin_lock(&pmp->inum_spin);
239                 ip = RB_LOOKUP(hammer2_inode_tree, &pmp->inum_tree, inum);
240                 if (ip)
241                         hammer2_inode_ref(ip);
242                 spin_unlock(&pmp->inum_spin);
243         }
244         return(ip);
245 }
246
247 /*
248  * Adding a ref to an inode is only legal if the inode already has at least
249  * one ref.
250  *
251  * (can be called with spinlock held)
252  */
253 void
254 hammer2_inode_ref(hammer2_inode_t *ip)
255 {
256         atomic_add_int(&ip->refs, 1);
257 }
258
259 /*
260  * Drop an inode reference, freeing the inode when the last reference goes
261  * away.
262  */
263 void
264 hammer2_inode_drop(hammer2_inode_t *ip)
265 {
266         hammer2_pfsmount_t *pmp;
267         hammer2_inode_t *pip;
268         u_int refs;
269
270         while (ip) {
271                 refs = ip->refs;
272                 cpu_ccfence();
273                 if (refs == 1) {
274                         /*
275                          * Transition to zero, must interlock with
276                          * the inode inumber lookup tree (if applicable).
277                          */
278                         pmp = ip->pmp;
279                         KKASSERT(pmp);
280                         spin_lock(&pmp->inum_spin);
281
282                         if (atomic_cmpset_int(&ip->refs, 1, 0)) {
283                                 KKASSERT(ip->topo_cst.count == 0);
284                                 if (ip->flags & HAMMER2_INODE_ONRBTREE) {
285                                         atomic_clear_int(&ip->flags,
286                                                      HAMMER2_INODE_ONRBTREE);
287                                         RB_REMOVE(hammer2_inode_tree,
288                                                   &pmp->inum_tree, ip);
289                                 }
290                                 spin_unlock(&pmp->inum_spin);
291
292                                 pip = ip->pip;
293                                 ip->pip = NULL;
294                                 ip->pmp = NULL;
295
296                                 /*
297                                  * Cleaning out ip->cluster isn't entirely
298                                  * trivial.
299                                  */
300                                 hammer2_inode_repoint(ip, NULL, NULL);
301
302                                 /*
303                                  * We have to drop pip (if non-NULL) to
304                                  * dispose of our implied reference from
305                                  * ip->pip.  We can simply loop on it.
306                                  */
307                                 kfree(ip, pmp->minode);
308                                 atomic_add_long(&pmp->inmem_inodes, -1);
309                                 ip = pip;
310                                 /* continue with pip (can be NULL) */
311                         } else {
312                                 spin_unlock(&ip->pmp->inum_spin);
313                         }
314                 } else {
315                         /*
316                          * Non zero transition
317                          */
318                         if (atomic_cmpset_int(&ip->refs, refs, refs - 1))
319                                 break;
320                 }
321         }
322 }
323
324 /*
325  * Get the vnode associated with the given inode, allocating the vnode if
326  * necessary.  The vnode will be returned exclusively locked.
327  *
328  * The caller must lock the inode (shared or exclusive).
329  *
330  * Great care must be taken to avoid deadlocks and vnode acquisition/reclaim
331  * races.
332  */
333 struct vnode *
334 hammer2_igetv(hammer2_inode_t *ip, hammer2_cluster_t *cparent, int *errorp)
335 {
336         const hammer2_inode_data_t *ipdata;
337         hammer2_pfsmount_t *pmp;
338         struct vnode *vp;
339         ccms_state_t ostate;
340
341         pmp = ip->pmp;
342         KKASSERT(pmp != NULL);
343         *errorp = 0;
344
345         ipdata = &hammer2_cluster_data(cparent)->ipdata;
346
347         for (;;) {
348                 /*
349                  * Attempt to reuse an existing vnode assignment.  It is
350                  * possible to race a reclaim so the vget() may fail.  The
351                  * inode must be unlocked during the vget() to avoid a
352                  * deadlock against a reclaim.
353                  */
354                 vp = ip->vp;
355                 if (vp) {
356                         /*
357                          * Inode must be unlocked during the vget() to avoid
358                          * possible deadlocks, but leave the ip ref intact.
359                          *
360                          * vnode is held to prevent destruction during the
361                          * vget().  The vget() can still fail if we lost
362                          * a reclaim race on the vnode.
363                          */
364                         vhold(vp);
365                         ostate = hammer2_inode_lock_temp_release(ip);
366                         if (vget(vp, LK_EXCLUSIVE)) {
367                                 vdrop(vp);
368                                 hammer2_inode_lock_temp_restore(ip, ostate);
369                                 continue;
370                         }
371                         hammer2_inode_lock_temp_restore(ip, ostate);
372                         vdrop(vp);
373                         /* vp still locked and ref from vget */
374                         if (ip->vp != vp) {
375                                 kprintf("hammer2: igetv race %p/%p\n",
376                                         ip->vp, vp);
377                                 vput(vp);
378                                 continue;
379                         }
380                         *errorp = 0;
381                         break;
382                 }
383
384                 /*
385                  * No vnode exists, allocate a new vnode.  Beware of
386                  * allocation races.  This function will return an
387                  * exclusively locked and referenced vnode.
388                  */
389                 *errorp = getnewvnode(VT_HAMMER2, pmp->mp, &vp, 0, 0);
390                 if (*errorp) {
391                         kprintf("hammer2: igetv getnewvnode failed %d\n",
392                                 *errorp);
393                         vp = NULL;
394                         break;
395                 }
396
397                 /*
398                  * Lock the inode and check for an allocation race.
399                  */
400                 ostate = hammer2_inode_lock_upgrade(ip);
401                 if (ip->vp != NULL) {
402                         vp->v_type = VBAD;
403                         vx_put(vp);
404                         hammer2_inode_lock_downgrade(ip, ostate);
405                         continue;
406                 }
407
408                 switch (ipdata->type) {
409                 case HAMMER2_OBJTYPE_DIRECTORY:
410                         vp->v_type = VDIR;
411                         break;
412                 case HAMMER2_OBJTYPE_REGFILE:
413                         vp->v_type = VREG;
414                         vinitvmio(vp, ipdata->size,
415                                   HAMMER2_LBUFSIZE,
416                                   (int)ipdata->size & HAMMER2_LBUFMASK);
417                         break;
418                 case HAMMER2_OBJTYPE_SOFTLINK:
419                         /*
420                          * XXX for now we are using the generic file_read
421                          * and file_write code so we need a buffer cache
422                          * association.
423                          */
424                         vp->v_type = VLNK;
425                         vinitvmio(vp, ipdata->size,
426                                   HAMMER2_LBUFSIZE,
427                                   (int)ipdata->size & HAMMER2_LBUFMASK);
428                         break;
429                 case HAMMER2_OBJTYPE_CDEV:
430                         vp->v_type = VCHR;
431                         /* fall through */
432                 case HAMMER2_OBJTYPE_BDEV:
433                         vp->v_ops = &pmp->mp->mnt_vn_spec_ops;
434                         if (ipdata->type != HAMMER2_OBJTYPE_CDEV)
435                                 vp->v_type = VBLK;
436                         addaliasu(vp, ipdata->rmajor, ipdata->rminor);
437                         break;
438                 case HAMMER2_OBJTYPE_FIFO:
439                         vp->v_type = VFIFO;
440                         vp->v_ops = &pmp->mp->mnt_vn_fifo_ops;
441                         break;
442                 default:
443                         panic("hammer2: unhandled objtype %d", ipdata->type);
444                         break;
445                 }
446
447                 if (ip == pmp->iroot)
448                         vsetflags(vp, VROOT);
449
450                 vp->v_data = ip;
451                 ip->vp = vp;
452                 hammer2_inode_ref(ip);          /* vp association */
453                 hammer2_inode_lock_downgrade(ip, ostate);
454                 break;
455         }
456
457         /*
458          * Return non-NULL vp and *errorp == 0, or NULL vp and *errorp != 0.
459          */
460         if (hammer2_debug & 0x0002) {
461                 kprintf("igetv vp %p refs 0x%08x aux 0x%08x\n",
462                         vp, vp->v_refcnt, vp->v_auxrefs);
463         }
464         return (vp);
465 }
466
467 /*
468  * Returns the inode associated with the passed-in cluster, creating the
469  * inode if necessary and synchronizing it to the passed-in cluster otherwise.
470  *
471  * The passed-in chain must be locked and will remain locked on return.
472  * The returned inode will be locked and the caller may dispose of both
473  * via hammer2_inode_unlock_ex().  However, if the caller needs to resolve
474  * a hardlink it must ref/unlock/relock/drop the inode.
475  *
476  * The hammer2_inode structure regulates the interface between the high level
477  * kernel VNOPS API and the filesystem backend (the chains).
478  */
479 hammer2_inode_t *
480 hammer2_inode_get(hammer2_pfsmount_t *pmp, hammer2_inode_t *dip,
481                   hammer2_cluster_t *cluster)
482 {
483         hammer2_inode_t *nip;
484         const hammer2_inode_data_t *iptmp;
485         const hammer2_inode_data_t *nipdata;
486
487         KKASSERT(hammer2_cluster_type(cluster) == HAMMER2_BREF_TYPE_INODE);
488         KKASSERT(pmp);
489
490         /*
491          * Interlocked lookup/ref of the inode.  This code is only needed
492          * when looking up inodes with nlinks != 0 (TODO: optimize out
493          * otherwise and test for duplicates).
494          */
495 again:
496         for (;;) {
497                 iptmp = &hammer2_cluster_data(cluster)->ipdata;
498                 nip = hammer2_inode_lookup(pmp, iptmp->inum);
499                 if (nip == NULL)
500                         break;
501
502                 ccms_thread_lock(&nip->topo_cst, CCMS_STATE_EXCLUSIVE);
503
504                 /*
505                  * Handle SMP race (not applicable to the super-root spmp
506                  * which can't index inodes due to duplicative inode numbers).
507                  */
508                 if (pmp->spmp_hmp == NULL &&
509                     (nip->flags & HAMMER2_INODE_ONRBTREE) == 0) {
510                         ccms_thread_unlock(&nip->topo_cst);
511                         hammer2_inode_drop(nip);
512                         continue;
513                 }
514                 hammer2_inode_repoint(nip, NULL, cluster);
515                 return nip;
516         }
517
518         /*
519          * We couldn't find the inode number, create a new inode.
520          */
521         nip = kmalloc(sizeof(*nip), pmp->minode, M_WAITOK | M_ZERO);
522         atomic_add_long(&pmp->inmem_inodes, 1);
523         hammer2_pfs_memory_inc(pmp);
524         hammer2_pfs_memory_wakeup(pmp);
525         if (pmp->spmp_hmp)
526                 nip->flags = HAMMER2_INODE_SROOT;
527
528         /*
529          * Initialize nip's cluster
530          */
531         nip->cluster.refs = 1;
532         nip->cluster.pmp = pmp;
533         nip->cluster.flags |= HAMMER2_CLUSTER_INODE;
534         hammer2_cluster_replace(&nip->cluster, cluster);
535
536         nipdata = &hammer2_cluster_data(cluster)->ipdata;
537         nip->inum = nipdata->inum;
538         nip->size = nipdata->size;
539         nip->mtime = nipdata->mtime;
540         hammer2_inode_repoint(nip, NULL, cluster);
541         nip->pip = dip;                         /* can be NULL */
542         if (dip)
543                 hammer2_inode_ref(dip); /* ref dip for nip->pip */
544
545         nip->pmp = pmp;
546
547         /*
548          * ref and lock on nip gives it state compatible to after a
549          * hammer2_inode_lock_ex() call.
550          */
551         nip->refs = 1;
552         ccms_cst_init(&nip->topo_cst, &nip->cluster);
553         ccms_thread_lock(&nip->topo_cst, CCMS_STATE_EXCLUSIVE);
554         /* combination of thread lock and chain lock == inode lock */
555
556         /*
557          * Attempt to add the inode.  If it fails we raced another inode
558          * get.  Undo all the work and try again.
559          */
560         if (pmp->spmp_hmp == NULL) {
561                 spin_lock(&pmp->inum_spin);
562                 if (RB_INSERT(hammer2_inode_tree, &pmp->inum_tree, nip)) {
563                         spin_unlock(&pmp->inum_spin);
564                         ccms_thread_unlock(&nip->topo_cst);
565                         hammer2_inode_drop(nip);
566                         goto again;
567                 }
568                 atomic_set_int(&nip->flags, HAMMER2_INODE_ONRBTREE);
569                 spin_unlock(&pmp->inum_spin);
570         }
571
572         return (nip);
573 }
574
575 /*
576  * Create a new inode in the specified directory using the vattr to
577  * figure out the type of inode.
578  *
579  * If no error occurs the new inode with its cluster locked is returned in
580  * *nipp, otherwise an error is returned and *nipp is set to NULL.
581  *
582  * If vap and/or cred are NULL the related fields are not set and the
583  * inode type defaults to a directory.  This is used when creating PFSs
584  * under the super-root, so the inode number is set to 1 in this case.
585  *
586  * dip is not locked on entry.
587  *
588  * NOTE: When used to create a snapshot, the inode is temporarily associated
589  *       with the super-root spmp. XXX should pass new pmp for snapshot.
590  */
591 hammer2_inode_t *
592 hammer2_inode_create(hammer2_trans_t *trans, hammer2_inode_t *dip,
593                      struct vattr *vap, struct ucred *cred,
594                      const uint8_t *name, size_t name_len,
595                      hammer2_cluster_t **clusterp, int *errorp)
596 {
597         const hammer2_inode_data_t *dipdata;
598         hammer2_inode_data_t *nipdata;
599         hammer2_cluster_t *cluster;
600         hammer2_cluster_t *cparent;
601         hammer2_inode_t *nip;
602         hammer2_key_t key_dummy;
603         hammer2_key_t lhc;
604         int error;
605         uid_t xuid;
606         uuid_t dip_uid;
607         uuid_t dip_gid;
608         uint32_t dip_mode;
609         uint8_t dip_algo;
610         int ddflag;
611
612         lhc = hammer2_dirhash(name, name_len);
613         *errorp = 0;
614
615         /*
616          * Locate the inode or indirect block to create the new
617          * entry in.  At the same time check for key collisions
618          * and iterate until we don't get one.
619          *
620          * NOTE: hidden inodes do not have iterators.
621          */
622 retry:
623         cparent = hammer2_inode_lock_ex(dip);
624         dipdata = &hammer2_cluster_data(cparent)->ipdata;
625         dip_uid = dipdata->uid;
626         dip_gid = dipdata->gid;
627         dip_mode = dipdata->mode;
628         dip_algo = dipdata->comp_algo;
629
630         error = 0;
631         while (error == 0) {
632                 cluster = hammer2_cluster_lookup(cparent, &key_dummy,
633                                                  lhc, lhc, 0, &ddflag);
634                 if (cluster == NULL)
635                         break;
636                 if ((lhc & HAMMER2_DIRHASH_VISIBLE) == 0)
637                         error = ENOSPC;
638                 if ((lhc & HAMMER2_DIRHASH_LOMASK) == HAMMER2_DIRHASH_LOMASK)
639                         error = ENOSPC;
640                 hammer2_cluster_unlock(cluster);
641                 cluster = NULL;
642                 ++lhc;
643         }
644
645         if (error == 0) {
646                 error = hammer2_cluster_create(trans, cparent, &cluster,
647                                              lhc, 0,
648                                              HAMMER2_BREF_TYPE_INODE,
649                                              HAMMER2_INODE_BYTES,
650                                              0);
651         }
652 #if INODE_DEBUG
653         kprintf("CREATE INODE %*.*s chain=%p\n",
654                 (int)name_len, (int)name_len, name,
655                 (cluster ? cluster->focus : NULL));
656 #endif
657
658         /*
659          * Cleanup and handle retries.
660          */
661         if (error == EAGAIN) {
662                 hammer2_cluster_ref(cparent);
663                 hammer2_inode_unlock_ex(dip, cparent);
664                 hammer2_cluster_wait(cparent);
665                 hammer2_cluster_drop(cparent);
666                 goto retry;
667         }
668         hammer2_inode_unlock_ex(dip, cparent);
669         cparent = NULL;
670
671         if (error) {
672                 KKASSERT(cluster == NULL);
673                 *errorp = error;
674                 return (NULL);
675         }
676
677         /*
678          * Set up the new inode.
679          *
680          * NOTE: *_get() integrates chain's lock into the inode lock.
681          *
682          * NOTE: Only one new inode can currently be created per
683          *       transaction.  If the need arises we can adjust
684          *       hammer2_trans_init() to allow more.
685          *
686          * NOTE: nipdata will have chain's blockset data.
687          */
688         KKASSERT(cluster->focus->flags & HAMMER2_CHAIN_MODIFIED);
689         nipdata = &hammer2_cluster_wdata(cluster)->ipdata;
690         nipdata->inum = trans->inode_tid;
691         hammer2_cluster_modsync(cluster);
692         nip = hammer2_inode_get(dip->pmp, dip, cluster);
693         nipdata = &hammer2_cluster_wdata(cluster)->ipdata;
694
695         if (vap) {
696                 KKASSERT(trans->inodes_created == 0);
697                 nipdata->type = hammer2_get_obj_type(vap->va_type);
698                 nipdata->inum = trans->inode_tid;
699                 ++trans->inodes_created;
700
701                 switch (nipdata->type) {
702                 case HAMMER2_OBJTYPE_CDEV:
703                 case HAMMER2_OBJTYPE_BDEV:
704                         nipdata->rmajor = vap->va_rmajor;
705                         nipdata->rminor = vap->va_rminor;
706                         break;
707                 default:
708                         break;
709                 }
710         } else {
711                 nipdata->type = HAMMER2_OBJTYPE_DIRECTORY;
712                 nipdata->inum = 1;
713         }
714         
715         /* Inherit parent's inode compression mode. */
716         nip->comp_heuristic = 0;
717         nipdata->comp_algo = dip_algo;
718         nipdata->version = HAMMER2_INODE_VERSION_ONE;
719         hammer2_update_time(&nipdata->ctime);
720         nipdata->mtime = nipdata->ctime;
721         if (vap)
722                 nipdata->mode = vap->va_mode;
723         nipdata->nlinks = 1;
724         if (vap) {
725                 if (dip && dip->pmp) {
726                         xuid = hammer2_to_unix_xid(&dip_uid);
727                         xuid = vop_helper_create_uid(dip->pmp->mp,
728                                                      dip_mode,
729                                                      xuid,
730                                                      cred,
731                                                      &vap->va_mode);
732                 } else {
733                         /* super-root has no dip and/or pmp */
734                         xuid = 0;
735                 }
736                 if (vap->va_vaflags & VA_UID_UUID_VALID)
737                         nipdata->uid = vap->va_uid_uuid;
738                 else if (vap->va_uid != (uid_t)VNOVAL)
739                         hammer2_guid_to_uuid(&nipdata->uid, vap->va_uid);
740                 else
741                         hammer2_guid_to_uuid(&nipdata->uid, xuid);
742
743                 if (vap->va_vaflags & VA_GID_UUID_VALID)
744                         nipdata->gid = vap->va_gid_uuid;
745                 else if (vap->va_gid != (gid_t)VNOVAL)
746                         hammer2_guid_to_uuid(&nipdata->gid, vap->va_gid);
747                 else if (dip)
748                         nipdata->gid = dip_gid;
749         }
750
751         /*
752          * Regular files and softlinks allow a small amount of data to be
753          * directly embedded in the inode.  This flag will be cleared if
754          * the size is extended past the embedded limit.
755          */
756         if (nipdata->type == HAMMER2_OBJTYPE_REGFILE ||
757             nipdata->type == HAMMER2_OBJTYPE_SOFTLINK) {
758                 nipdata->op_flags |= HAMMER2_OPFLAG_DIRECTDATA;
759         }
760
761         KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
762         bcopy(name, nipdata->filename, name_len);
763         nipdata->name_key = lhc;
764         nipdata->name_len = name_len;
765         hammer2_cluster_modsync(cluster);
766         *clusterp = cluster;
767
768         return (nip);
769 }
770
771 /*
772  * The cluster has been removed from the original directory and replaced
773  * with a hardlink pointer.  Move the cluster to the specified parent
774  * directory, change the filename to "0xINODENUMBER", and adjust the key.
775  * The cluster becomes our invisible hardlink target.
776  *
777  * The original cluster must be deleted on entry.
778  */
779 static
780 void
781 hammer2_hardlink_shiftup(hammer2_trans_t *trans, hammer2_cluster_t *cluster,
782                         hammer2_inode_t *dip, hammer2_cluster_t *dcluster,
783                         int nlinks, int *errorp)
784 {
785         const hammer2_inode_data_t *iptmp;
786         hammer2_inode_data_t *nipdata;
787         hammer2_cluster_t *xcluster;
788         hammer2_key_t key_dummy;
789         hammer2_key_t lhc;
790         hammer2_blockref_t bref;
791         int ddflag;
792
793         iptmp = &hammer2_cluster_data(cluster)->ipdata;
794         lhc = iptmp->inum;
795         KKASSERT((lhc & HAMMER2_DIRHASH_VISIBLE) == 0);
796
797         /*
798          * Locate the inode or indirect block to create the new
799          * entry in.  lhc represents the inode number so there is
800          * no collision iteration.
801          *
802          * There should be no key collisions with invisible inode keys.
803          *
804          * WARNING! Must use inode_lock_ex() on dip to handle a stale
805          *          dip->cluster cache.
806          */
807         *errorp = 0;
808         xcluster = hammer2_cluster_lookup(dcluster, &key_dummy,
809                                       lhc, lhc, 0, &ddflag);
810         if (xcluster) {
811                 kprintf("X3 chain %p dip %p dchain %p dip->chain %p\n",
812                         xcluster->focus, dip, dcluster->focus,
813                         dip->cluster.focus);
814                 hammer2_cluster_unlock(xcluster);
815                 xcluster = NULL;
816                 *errorp = ENOSPC;
817 #if 0
818                 Debugger("X3");
819 #endif
820         }
821
822         /*
823          * Handle the error case
824          */
825         if (*errorp) {
826                 panic("error2");
827                 KKASSERT(xcluster == NULL);
828                 return;
829         }
830
831         /*
832          * Use xcluster as a placeholder for (lhc).  Duplicate cluster to the
833          * same target bref as xcluster and then delete xcluster.  The
834          * duplication occurs after xcluster in flush order even though
835          * xcluster is deleted after the duplication. XXX
836          *
837          * WARNING! Duplications (to a different parent) can cause indirect
838          *          blocks to be inserted, refactor xcluster.
839          *
840          * WARNING! Only key and keybits is extracted from a passed-in bref.
841          */
842         hammer2_cluster_bref(cluster, &bref);
843         bref.key = lhc;                 /* invisible dir entry key */
844         bref.keybits = 0;
845         hammer2_cluster_rename(trans, &bref, dcluster, cluster, 0);
846
847         /*
848          * cluster is now 'live' again.. adjust the filename.
849          *
850          * Directory entries are inodes but this is a hidden hardlink
851          * target.  The name isn't used but to ease debugging give it
852          * a name after its inode number.
853          */
854         hammer2_cluster_modify(trans, cluster, 0);
855         nipdata = &hammer2_cluster_wdata(cluster)->ipdata;
856         ksnprintf(nipdata->filename, sizeof(nipdata->filename),
857                   "0x%016jx", (intmax_t)nipdata->inum);
858         nipdata->name_len = strlen(nipdata->filename);
859         nipdata->name_key = lhc;
860         nipdata->nlinks += nlinks;
861         hammer2_cluster_modsync(cluster);
862 }
863
864 /*
865  * Connect the target inode represented by (cluster) to the media topology
866  * at (dip, name, len).  The caller can pass a rough *chainp, this function
867  * will issue lookup()s to position the parent chain properly for the
868  * chain insertion.
869  *
870  * If hlink is TRUE this function creates an OBJTYPE_HARDLINK directory
871  * entry instead of connecting (cluster).
872  *
873  * If hlink is FALSE this function expects (cluster) to be unparented.
874  */
875 int
876 hammer2_inode_connect(hammer2_trans_t *trans,
877                       hammer2_cluster_t **clusterp, int hlink,
878                       hammer2_inode_t *dip, hammer2_cluster_t *dcluster,
879                       const uint8_t *name, size_t name_len,
880                       hammer2_key_t lhc)
881 {
882         hammer2_inode_data_t *wipdata;
883         hammer2_cluster_t *ocluster;
884         hammer2_cluster_t *ncluster;
885         hammer2_key_t key_dummy;
886         int ddflag;
887         int error;
888
889         /*
890          * Since ocluster is either disconnected from the topology or
891          * represents a hardlink terminus which is always a parent of or
892          * equal to dip, we should be able to safely lock dip->chain for
893          * our setup.
894          *
895          * WARNING! Must use inode_lock_ex() on dip to handle a stale
896          *          dip->cluster.
897          *
898          * If name is non-NULL we calculate lhc, else we use the passed-in
899          * lhc.
900          */
901         ocluster = *clusterp;
902
903         if (name) {
904                 lhc = hammer2_dirhash(name, name_len);
905
906                 /*
907                  * Locate the inode or indirect block to create the new
908                  * entry in.  At the same time check for key collisions
909                  * and iterate until we don't get one.
910                  */
911                 error = 0;
912                 while (error == 0) {
913                         ncluster = hammer2_cluster_lookup(dcluster, &key_dummy,
914                                                       lhc, lhc,
915                                                       0, &ddflag);
916                         if (ncluster == NULL)
917                                 break;
918                         if ((lhc & HAMMER2_DIRHASH_LOMASK) ==
919                             HAMMER2_DIRHASH_LOMASK) {
920                                 error = ENOSPC;
921                         }
922                         hammer2_cluster_unlock(ncluster);
923                         ncluster = NULL;
924                         ++lhc;
925                 }
926         } else {
927                 /*
928                  * Reconnect to specific key (used when moving
929                  * unlinked-but-open files into the hidden directory).
930                  */
931                 ncluster = hammer2_cluster_lookup(dcluster, &key_dummy,
932                                                   lhc, lhc,
933                                                   0, &ddflag);
934                 KKASSERT(ncluster == NULL);
935         }
936
937         if (error == 0) {
938                 if (hlink) {
939                         /*
940                          * Hardlink pointer needed, create totally fresh
941                          * directory entry.
942                          *
943                          * We must refactor ocluster because it might have
944                          * been shifted into an indirect cluster by the
945                          * create.
946                          */
947                         KKASSERT(ncluster == NULL);
948                         error = hammer2_cluster_create(trans,
949                                                        dcluster, &ncluster,
950                                                        lhc, 0,
951                                                        HAMMER2_BREF_TYPE_INODE,
952                                                        HAMMER2_INODE_BYTES,
953                                                        0);
954                 } else {
955                         /*
956                          * Reconnect the original cluster under the new name.
957                          * Original cluster must have already been deleted by
958                          * teh caller.
959                          *
960                          * WARNING! Can cause held-over clusters to require a
961                          *          refactor.  Fortunately we have none (our
962                          *          locked clusters are passed into and
963                          *          modified by the call).
964                          */
965                         ncluster = ocluster;
966                         ocluster = NULL;
967                         error = hammer2_cluster_create(trans,
968                                                        dcluster, &ncluster,
969                                                        lhc, 0,
970                                                        HAMMER2_BREF_TYPE_INODE,
971                                                        HAMMER2_INODE_BYTES,
972                                                        0);
973                 }
974         }
975
976         /*
977          * Unlock stuff.
978          */
979         KKASSERT(error != EAGAIN);
980
981         /*
982          * ncluster should be NULL on error, leave ocluster
983          * (ocluster == *clusterp) alone.
984          */
985         if (error) {
986                 KKASSERT(ncluster == NULL);
987                 return (error);
988         }
989
990         /*
991          * Directory entries are inodes so if the name has changed we have
992          * to update the inode.
993          *
994          * When creating an OBJTYPE_HARDLINK entry remember to unlock the
995          * cluster, the caller will access the hardlink via the actual hardlink
996          * target file and not the hardlink pointer entry, so we must still
997          * return ocluster.
998          */
999         if (hlink && hammer2_hardlink_enable >= 0) {
1000                 /*
1001                  * Create the HARDLINK pointer.  oip represents the hardlink
1002                  * target in this situation.
1003                  *
1004                  * We will return ocluster (the hardlink target).
1005                  */
1006                 hammer2_cluster_modify(trans, ncluster, 0);
1007                 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
1008                 wipdata = &hammer2_cluster_wdata(ncluster)->ipdata;
1009                 bcopy(name, wipdata->filename, name_len);
1010                 wipdata->name_key = lhc;
1011                 wipdata->name_len = name_len;
1012                 wipdata->target_type =
1013                                 hammer2_cluster_data(ocluster)->ipdata.type;
1014                 wipdata->type = HAMMER2_OBJTYPE_HARDLINK;
1015                 wipdata->inum = hammer2_cluster_data(ocluster)->ipdata.inum;
1016                 wipdata->version = HAMMER2_INODE_VERSION_ONE;
1017                 wipdata->nlinks = 1;
1018                 wipdata->op_flags = HAMMER2_OPFLAG_DIRECTDATA;
1019                 hammer2_cluster_modsync(ncluster);
1020                 hammer2_cluster_unlock(ncluster);
1021                 ncluster = ocluster;
1022                 ocluster = NULL;
1023         } else {
1024                 /*
1025                  * ncluster is a duplicate of ocluster at the new location.
1026                  * We must fixup the name stored in the inode data.
1027                  * The bref key has already been adjusted by inode_connect().
1028                  */
1029                 hammer2_cluster_modify(trans, ncluster, 0);
1030                 wipdata = &hammer2_cluster_wdata(ncluster)->ipdata;
1031
1032                 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
1033                 bcopy(name, wipdata->filename, name_len);
1034                 wipdata->name_key = lhc;
1035                 wipdata->name_len = name_len;
1036                 wipdata->nlinks = 1;
1037                 hammer2_cluster_modsync(ncluster);
1038         }
1039
1040         /*
1041          * We are replacing ocluster with ncluster, unlock ocluster.  In the
1042          * case where ocluster is left unchanged the code above sets
1043          * ncluster to ocluster and ocluster to NULL, resulting in a NOP here.
1044          */
1045         if (ocluster)
1046                 hammer2_cluster_unlock(ocluster);
1047         *clusterp = ncluster;
1048
1049         return (0);
1050 }
1051
1052 /*
1053  * Repoint ip->cluster's chains to cluster's chains.  Caller must hold
1054  * the inode exclusively locked.  cluster may be NULL to clean out any
1055  * chains in ip->cluster.
1056  */
1057 void
1058 hammer2_inode_repoint(hammer2_inode_t *ip, hammer2_inode_t *pip,
1059                       hammer2_cluster_t *cluster)
1060 {
1061         hammer2_chain_t *ochain;
1062         hammer2_chain_t *nchain;
1063         hammer2_inode_t *opip;
1064         int i;
1065
1066         /*
1067          * Replace chains in ip->cluster with chains from cluster and
1068          * adjust the focus if necessary.
1069          *
1070          * NOTE: nchain and/or ochain can be NULL due to gaps
1071          *       in the cluster arrays.
1072          */
1073         ip->cluster.focus = NULL;
1074         for (i = 0; cluster && i < cluster->nchains; ++i) {
1075                 nchain = cluster->array[i];
1076                 if (i < ip->cluster.nchains) {
1077                         ochain = ip->cluster.array[i];
1078                         if (ochain == nchain) {
1079                                 if (ip->cluster.focus == NULL)
1080                                         ip->cluster.focus = nchain;
1081                                 continue;
1082                         }
1083                 } else {
1084                         ochain = NULL;
1085                 }
1086
1087                 /*
1088                  * Make adjustments
1089                  */
1090                 ip->cluster.array[i] = nchain;
1091                 if (ip->cluster.focus == NULL)
1092                         ip->cluster.focus = nchain;
1093                 if (nchain)
1094                         hammer2_chain_ref(nchain);
1095                 if (ochain)
1096                         hammer2_chain_drop(ochain);
1097         }
1098
1099         /*
1100          * Release any left-over chains in ip->cluster.
1101          */
1102         while (i < ip->cluster.nchains) {
1103                 nchain = ip->cluster.array[i];
1104                 if (nchain) {
1105                         ip->cluster.array[i] = NULL;
1106                         hammer2_chain_drop(nchain);
1107                 }
1108                 ++i;
1109         }
1110         ip->cluster.nchains = cluster ? cluster->nchains : 0;
1111
1112         /*
1113          * Repoint ip->pip if requested (non-NULL pip).
1114          */
1115         if (pip && ip->pip != pip) {
1116                 opip = ip->pip;
1117                 hammer2_inode_ref(pip);
1118                 ip->pip = pip;
1119                 if (opip)
1120                         hammer2_inode_drop(opip);
1121         }
1122 }
1123
1124 /*
1125  * Unlink the file from the specified directory inode.  The directory inode
1126  * does not need to be locked.
1127  *
1128  * isdir determines whether a directory/non-directory check should be made.
1129  * No check is made if isdir is set to -1.
1130  *
1131  * isopen specifies whether special unlink-with-open-descriptor handling
1132  * must be performed.  If set to -1 the caller is deleting a PFS and we
1133  * check whether the chain is mounted or not (chain->pmp != NULL).  1 is
1134  * implied if it is mounted.
1135  *
1136  * If isopen is 1 and nlinks drops to 0 this function must move the chain
1137  * to a special hidden directory until last-close occurs on the file.
1138  *
1139  * NOTE!  The underlying file can still be active with open descriptors
1140  *        or if the chain is being manually held (e.g. for rename).
1141  *
1142  *        The caller is responsible for fixing up ip->chain if e.g. a
1143  *        rename occurs (see chain_duplicate()).
1144  *
1145  * NOTE!  The chain is not deleted if it is moved to the hidden directory,
1146  *        but otherwise will be deleted.
1147  */
1148 int
1149 hammer2_unlink_file(hammer2_trans_t *trans, hammer2_inode_t *dip,
1150                     const uint8_t *name, size_t name_len,
1151                     int isdir, int *hlinkp, struct nchandle *nch,
1152                     int nlinks)
1153 {
1154         const hammer2_inode_data_t *ripdata;
1155         hammer2_inode_data_t *wipdata;
1156         hammer2_cluster_t *cparent;
1157         hammer2_cluster_t *hcluster;
1158         hammer2_cluster_t *hparent;
1159         hammer2_cluster_t *cluster;
1160         hammer2_cluster_t *dparent;
1161         hammer2_cluster_t *dcluster;
1162         hammer2_key_t key_dummy;
1163         hammer2_key_t key_next;
1164         hammer2_key_t lhc;
1165         int error;
1166         int ddflag;
1167         int hlink;
1168         uint8_t type;
1169
1170         error = 0;
1171         hlink = 0;
1172         hcluster = NULL;
1173         hparent = NULL;
1174         lhc = hammer2_dirhash(name, name_len);
1175
1176 again:
1177         /*
1178          * Search for the filename in the directory
1179          */
1180         cparent = hammer2_inode_lock_ex(dip);
1181         cluster = hammer2_cluster_lookup(cparent, &key_next,
1182                                      lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1183                                      0, &ddflag);
1184         while (cluster) {
1185                 if (hammer2_cluster_type(cluster) == HAMMER2_BREF_TYPE_INODE) {
1186                         ripdata = &hammer2_cluster_data(cluster)->ipdata;
1187                         if (ripdata->name_len == name_len &&
1188                             bcmp(ripdata->filename, name, name_len) == 0) {
1189                                 break;
1190                         }
1191                 }
1192                 cluster = hammer2_cluster_next(cparent, cluster, &key_next,
1193                                                key_next,
1194                                                lhc + HAMMER2_DIRHASH_LOMASK,
1195                                                0);
1196         }
1197         hammer2_inode_unlock_ex(dip, NULL);     /* retain cparent */
1198
1199         /*
1200          * Not found or wrong type (isdir < 0 disables the type check).
1201          * If a hardlink pointer, type checks use the hardlink target.
1202          */
1203         if (cluster == NULL) {
1204                 error = ENOENT;
1205                 goto done;
1206         }
1207         ripdata = &hammer2_cluster_data(cluster)->ipdata;
1208         type = ripdata->type;
1209         if (type == HAMMER2_OBJTYPE_HARDLINK) {
1210                 hlink = 1;
1211                 type = ripdata->target_type;
1212         }
1213
1214         if (type == HAMMER2_OBJTYPE_DIRECTORY && isdir == 0) {
1215                 error = ENOTDIR;
1216                 goto done;
1217         }
1218         if (type != HAMMER2_OBJTYPE_DIRECTORY && isdir >= 1) {
1219                 error = EISDIR;
1220                 goto done;
1221         }
1222
1223         /*
1224          * Hardlink must be resolved.  We can't hold the parent locked
1225          * while we do this or we could deadlock.  The physical file will
1226          * be located at or above the current directory.
1227          *
1228          * We loop to reacquire the hardlink origination.
1229          *
1230          * NOTE: hammer2_hardlink_find() will locate the hardlink target,
1231          *       returning a modified hparent and hcluster.
1232          */
1233         if (ripdata->type == HAMMER2_OBJTYPE_HARDLINK) {
1234                 if (hcluster == NULL) {
1235                         hcluster = cluster;
1236                         cluster = NULL; /* safety */
1237                         hammer2_cluster_unlock(cparent);
1238                         cparent = NULL; /* safety */
1239                         ripdata = NULL; /* safety (associated w/cparent) */
1240                         error = hammer2_hardlink_find(dip, &hparent, hcluster);
1241
1242                         /*
1243                          * If we couldn't find the hardlink target then some
1244                          * parent directory containing the hardlink pointer
1245                          * probably got renamed to above the original target,
1246                          * a case not yet handled by H2.
1247                          */
1248                         if (error) {
1249                                 kprintf("H2 unlink_file: hardlink target for "
1250                                         "\"%s\" not found\n",
1251                                         name);
1252                                 kprintf("(likely due to known directory "
1253                                         "rename bug)\n");
1254                                 goto done;
1255                         }
1256                         goto again;
1257                 }
1258         }
1259
1260         /*
1261          * If this is a directory the directory must be empty.  However, if
1262          * isdir < 0 we are doing a rename and the directory does not have
1263          * to be empty, and if isdir > 1 we are deleting a PFS/snapshot
1264          * and the directory does not have to be empty.
1265          *
1266          * NOTE: We check the full key range here which covers both visible
1267          *       and invisible entries.  Theoretically there should be no
1268          *       invisible (hardlink target) entries if there are no visible
1269          *       entries.
1270          */
1271         if (type == HAMMER2_OBJTYPE_DIRECTORY && isdir == 1) {
1272                 dparent = hammer2_cluster_lookup_init(cluster, 0);
1273                 dcluster = hammer2_cluster_lookup(dparent, &key_dummy,
1274                                                   0, (hammer2_key_t)-1,
1275                                                   HAMMER2_LOOKUP_NODATA,
1276                                                   &ddflag);
1277                 if (dcluster) {
1278                         hammer2_cluster_unlock(dcluster);
1279                         hammer2_cluster_lookup_done(dparent);
1280                         error = ENOTEMPTY;
1281                         goto done;
1282                 }
1283                 hammer2_cluster_lookup_done(dparent);
1284                 dparent = NULL;
1285                 /* dcluster NULL */
1286         }
1287
1288         /*
1289          * If this was a hardlink then (cparent, cluster) is the hardlink
1290          * pointer, which we can simply destroy outright.  Discard the
1291          * clusters and replace with the hardlink target.
1292          */
1293         if (hcluster) {
1294                 hammer2_cluster_delete(trans, cparent, cluster,
1295                                        HAMMER2_DELETE_PERMANENT);
1296                 hammer2_cluster_unlock(cparent);
1297                 hammer2_cluster_unlock(cluster);
1298                 cparent = hparent;
1299                 cluster = hcluster;
1300                 hparent = NULL;
1301                 hcluster = NULL;
1302         }
1303
1304         /*
1305          * This leaves us with the hardlink target or non-hardlinked file
1306          * or directory in (cparent, cluster).
1307          *
1308          * Delete the target when nlinks reaches 0 with special handling
1309          * if (isopen) is set.
1310          *
1311          * NOTE! In DragonFly the vnops function calls cache_unlink() after
1312          *       calling us here to clean out the namecache association,
1313          *       (which does not represent a ref for the open-test), and to
1314          *       force finalization of the vnode if/when the last ref gets
1315          *       dropped.
1316          *
1317          * NOTE! Files are unlinked by rename and then relinked.  nch will be
1318          *       passed as NULL in this situation.  hammer2_inode_connect()
1319          *       will bump nlinks.
1320          */
1321         KKASSERT(cluster != NULL);
1322         hammer2_cluster_modify(trans, cluster, 0);
1323         wipdata = &hammer2_cluster_wdata(cluster)->ipdata;
1324         ripdata = wipdata;
1325         wipdata->nlinks += nlinks;
1326         if ((int64_t)wipdata->nlinks < 0) {     /* XXX debugging */
1327                 wipdata->nlinks = 0;
1328         }
1329         hammer2_cluster_modsync(cluster);
1330
1331         if (wipdata->nlinks == 0) {
1332                 /*
1333                  * Target nlinks has reached 0, file now unlinked (but may
1334                  * still be open).
1335                  */
1336                 /* XXX need interlock if mounted
1337                 if ((cluster->focus->flags & HAMMER2_CHAIN_PFSROOT) &&
1338                     cluster->pmp) {
1339                         error = EINVAL;
1340                         kprintf("hammer2: PFS \"%s\" cannot be deleted "
1341                                 "while still mounted\n",
1342                                 wipdata->filename);
1343                         goto done;
1344                 }
1345                 */
1346                 if (nch && cache_isopen(nch)) {
1347                         hammer2_inode_move_to_hidden(trans, &cparent, &cluster,
1348                                                      wipdata->inum);
1349                 } else {
1350                         /*
1351                          * This won't get everything if a vnode is still
1352                          * present, but the cache_unlink() call the caller
1353                          * makes will.
1354                          */
1355                         hammer2_cluster_delete(trans, cparent, cluster,
1356                                                HAMMER2_DELETE_PERMANENT);
1357                 }
1358         } else if (hlink == 0) {
1359                 /*
1360                  * In this situation a normal non-hardlinked file (which can
1361                  * only have nlinks == 1) still has a non-zero nlinks, the
1362                  * caller must be doing a RENAME operation and so is passing
1363                  * a nlinks adjustment of 0, and only wishes to remove file
1364                  * in order to be able to reconnect it under a different name.
1365                  *
1366                  * In this situation we do a non-permanent deletion of the
1367                  * chain in order to allow the file to be reconnected in
1368                  * a different location.
1369                  */
1370                 KKASSERT(nlinks == 0);
1371                 hammer2_cluster_delete(trans, cparent, cluster, 0);
1372         }
1373         error = 0;
1374 done:
1375         if (cparent)
1376                 hammer2_cluster_unlock(cparent);
1377         if (cluster)
1378                 hammer2_cluster_unlock(cluster);
1379         if (hparent)
1380                 hammer2_cluster_unlock(hparent);
1381         if (hcluster)
1382                 hammer2_cluster_unlock(hcluster);
1383         if (hlinkp)
1384                 *hlinkp = hlink;
1385
1386         return error;
1387 }
1388
1389 /*
1390  * This is called from the mount code to initialize pmp->ihidden
1391  */
1392 void
1393 hammer2_inode_install_hidden(hammer2_pfsmount_t *pmp)
1394 {
1395         hammer2_trans_t trans;
1396         hammer2_cluster_t *cparent;
1397         hammer2_cluster_t *cluster;
1398         hammer2_cluster_t *scan;
1399         hammer2_inode_data_t *wipdata;
1400         hammer2_key_t key_dummy;
1401         hammer2_key_t key_next;
1402         int ddflag;
1403         int error;
1404         int count;
1405
1406         if (pmp->ihidden)
1407                 return;
1408
1409         /*
1410          * Find the hidden directory
1411          */
1412         bzero(&key_dummy, sizeof(key_dummy));
1413         hammer2_trans_init(&trans, pmp, 0);
1414
1415         cparent = hammer2_inode_lock_ex(pmp->iroot);
1416         cluster = hammer2_cluster_lookup(cparent, &key_dummy,
1417                                          HAMMER2_INODE_HIDDENDIR,
1418                                          HAMMER2_INODE_HIDDENDIR,
1419                                          0, &ddflag);
1420         if (cluster) {
1421                 pmp->ihidden = hammer2_inode_get(pmp, pmp->iroot, cluster);
1422                 hammer2_inode_ref(pmp->ihidden);
1423
1424                 /*
1425                  * Remove any unlinked files which were left open as-of
1426                  * any system crash.
1427                  *
1428                  * Don't pass NODATA, we need the inode data so the delete
1429                  * can do proper statistics updates.
1430                  */
1431                 count = 0;
1432                 scan = hammer2_cluster_lookup(cluster, &key_next,
1433                                               0, HAMMER2_TID_MAX,
1434                                               0, &ddflag);
1435                 while (scan) {
1436                         if (hammer2_cluster_type(scan) ==
1437                             HAMMER2_BREF_TYPE_INODE) {
1438                                 hammer2_cluster_delete(&trans, cluster, scan,
1439                                                    HAMMER2_DELETE_PERMANENT);
1440                                 ++count;
1441                         }
1442                         scan = hammer2_cluster_next(cluster, scan, &key_next,
1443                                                     0, HAMMER2_TID_MAX, 0);
1444                 }
1445
1446                 hammer2_inode_unlock_ex(pmp->ihidden, cluster);
1447                 hammer2_inode_unlock_ex(pmp->iroot, cparent);
1448                 hammer2_trans_done(&trans);
1449                 kprintf("hammer2: PFS loaded hidden dir, "
1450                         "removed %d dead entries\n", count);
1451                 return;
1452         }
1453
1454         /*
1455          * Create the hidden directory
1456          */
1457         error = hammer2_cluster_create(&trans, cparent, &cluster,
1458                                        HAMMER2_INODE_HIDDENDIR, 0,
1459                                        HAMMER2_BREF_TYPE_INODE,
1460                                        HAMMER2_INODE_BYTES,
1461                                        0);
1462         hammer2_inode_unlock_ex(pmp->iroot, cparent);
1463
1464         hammer2_cluster_modify(&trans, cluster, 0);
1465         wipdata = &hammer2_cluster_wdata(cluster)->ipdata;
1466         wipdata->type = HAMMER2_OBJTYPE_DIRECTORY;
1467         wipdata->inum = HAMMER2_INODE_HIDDENDIR;
1468         wipdata->nlinks = 1;
1469         hammer2_cluster_modsync(cluster);
1470         kprintf("hammer2: PFS root missing hidden directory, creating\n");
1471
1472         pmp->ihidden = hammer2_inode_get(pmp, pmp->iroot, cluster);
1473         hammer2_inode_ref(pmp->ihidden);
1474         hammer2_inode_unlock_ex(pmp->ihidden, cluster);
1475         hammer2_trans_done(&trans);
1476 }
1477
1478 /*
1479  * If an open file is unlinked H2 needs to retain the file in the topology
1480  * to ensure that its backing store is not recovered by the bulk free scan.
1481  * This also allows us to avoid having to special-case the CHAIN_DELETED flag.
1482  *
1483  * To do this the file is moved to a hidden directory in the PFS root and
1484  * renamed.  The hidden directory must be created if it does not exist.
1485  */
1486 static
1487 void
1488 hammer2_inode_move_to_hidden(hammer2_trans_t *trans,
1489                              hammer2_cluster_t **cparentp,
1490                              hammer2_cluster_t **clusterp,
1491                              hammer2_tid_t inum)
1492 {
1493         hammer2_cluster_t *dcluster;
1494         hammer2_pfsmount_t *pmp;
1495         int error;
1496
1497         pmp = (*clusterp)->pmp;
1498         KKASSERT(pmp != NULL);
1499         KKASSERT(pmp->ihidden != NULL);
1500
1501         hammer2_cluster_delete(trans, *cparentp, *clusterp, 0);
1502         dcluster = hammer2_inode_lock_ex(pmp->ihidden);
1503         error = hammer2_inode_connect(trans, clusterp, 0,
1504                                       pmp->ihidden, dcluster,
1505                                       NULL, 0, inum);
1506         hammer2_inode_unlock_ex(pmp->ihidden, dcluster);
1507         KKASSERT(error == 0);
1508 }
1509
1510 /*
1511  * Given an exclusively locked inode and cluster we consolidate the cluster
1512  * for hardlink creation, adding (nlinks) to the file's link count and
1513  * potentially relocating the inode to (cdip) which is a parent directory
1514  * common to both the current location of the inode and the intended new
1515  * hardlink.
1516  *
1517  * Replaces (*clusterp) if consolidation occurred, unlocking the old cluster
1518  * and returning a new locked cluster.
1519  *
1520  * NOTE!  This function will also replace ip->cluster.
1521  */
1522 int
1523 hammer2_hardlink_consolidate(hammer2_trans_t *trans,
1524                              hammer2_inode_t *ip,
1525                              hammer2_cluster_t **clusterp,
1526                              hammer2_inode_t *cdip,
1527                              hammer2_cluster_t *cdcluster,
1528                              int nlinks)
1529 {
1530         const hammer2_inode_data_t *ripdata;
1531         hammer2_inode_data_t *wipdata;
1532         hammer2_cluster_t *cluster;
1533         hammer2_cluster_t *cparent;
1534         int error;
1535
1536         cluster = *clusterp;
1537         ripdata = &hammer2_cluster_data(cluster)->ipdata;
1538         if (nlinks == 0 &&                      /* no hardlink needed */
1539             (ripdata->name_key & HAMMER2_DIRHASH_VISIBLE)) {
1540                 return (0);
1541         }
1542
1543         if (hammer2_hardlink_enable == 0) {     /* disallow hardlinks */
1544                 hammer2_cluster_unlock(cluster);
1545                 *clusterp = NULL;
1546                 return (ENOTSUP);
1547         }
1548
1549         cparent = NULL;
1550
1551         /*
1552          * If no change in the hardlink's target directory is required and
1553          * this is already a hardlink target, all we need to do is adjust
1554          * the link count.
1555          */
1556         ripdata = &hammer2_cluster_data(cluster)->ipdata;
1557         if (cdip == ip->pip &&
1558             (ripdata->name_key & HAMMER2_DIRHASH_VISIBLE) == 0) {
1559                 if (nlinks) {
1560                         hammer2_cluster_modify(trans, cluster, 0);
1561                         wipdata = &hammer2_cluster_wdata(cluster)->ipdata;
1562                         wipdata->nlinks += nlinks;
1563                         hammer2_cluster_modsync(cluster);
1564                         ripdata = wipdata;
1565                 }
1566                 error = 0;
1567                 goto done;
1568         }
1569
1570         /*
1571          * Cluster is the real inode.  The originating directory is locked
1572          * by the caller so we can manipulate it without worrying about races
1573          * against other lookups.
1574          *
1575          * If cluster is visible we need to delete it from the current
1576          * location and create a hardlink pointer in its place.  If it is
1577          * not visible we need only delete it.  Then later cluster will be
1578          * renamed to a parent directory and converted (if necessary) to
1579          * a hidden inode (via shiftup).
1580          *
1581          * NOTE! We must hold cparent locked through the delete/create/rename
1582          *       operation to ensure that other threads block resolving to
1583          *       the same hardlink, otherwise the other threads may not see
1584          *       the hardlink.
1585          */
1586         KKASSERT((cluster->focus->flags & HAMMER2_CHAIN_DELETED) == 0);
1587         cparent = hammer2_cluster_parent(cluster);
1588
1589         hammer2_cluster_delete(trans, cparent, cluster, 0);
1590
1591         ripdata = &hammer2_cluster_data(cluster)->ipdata;
1592         KKASSERT(ripdata->type != HAMMER2_OBJTYPE_HARDLINK);
1593         if (ripdata->name_key & HAMMER2_DIRHASH_VISIBLE) {
1594                 hammer2_cluster_t *ncluster;
1595                 hammer2_key_t lhc;
1596
1597                 ncluster = NULL;
1598                 lhc = cluster->focus->bref.key;
1599                 error = hammer2_cluster_create(trans, cparent, &ncluster,
1600                                              lhc, 0,
1601                                              HAMMER2_BREF_TYPE_INODE,
1602                                              HAMMER2_INODE_BYTES,
1603                                              0);
1604                 hammer2_cluster_modify(trans, ncluster, 0);
1605                 wipdata = &hammer2_cluster_wdata(ncluster)->ipdata;
1606
1607                 /* wipdata->comp_algo = ripdata->comp_algo; */
1608                 wipdata->comp_algo = 0;
1609                 wipdata->version = HAMMER2_INODE_VERSION_ONE;
1610                 wipdata->inum = ripdata->inum;
1611                 wipdata->target_type = ripdata->type;
1612                 wipdata->type = HAMMER2_OBJTYPE_HARDLINK;
1613                 wipdata->uflags = 0;
1614                 wipdata->rmajor = 0;
1615                 wipdata->rminor = 0;
1616                 wipdata->ctime = 0;
1617                 wipdata->mtime = 0;
1618                 wipdata->atime = 0;
1619                 wipdata->btime = 0;
1620                 bzero(&wipdata->uid, sizeof(wipdata->uid));
1621                 bzero(&wipdata->gid, sizeof(wipdata->gid));
1622                 wipdata->op_flags = HAMMER2_OPFLAG_DIRECTDATA;
1623                 wipdata->cap_flags = 0;
1624                 wipdata->mode = 0;
1625                 wipdata->size = 0;
1626                 wipdata->nlinks = 1;
1627                 wipdata->iparent = 0;   /* XXX */
1628                 wipdata->pfs_type = 0;
1629                 wipdata->pfs_inum = 0;
1630                 bzero(&wipdata->pfs_clid, sizeof(wipdata->pfs_clid));
1631                 bzero(&wipdata->pfs_fsid, sizeof(wipdata->pfs_fsid));
1632                 wipdata->data_quota = 0;
1633                 wipdata->data_count = 0;
1634                 wipdata->inode_quota = 0;
1635                 wipdata->inode_count = 0;
1636                 wipdata->attr_tid = 0;
1637                 wipdata->dirent_tid = 0;
1638                 bzero(&wipdata->u, sizeof(wipdata->u));
1639                 bcopy(ripdata->filename, wipdata->filename, ripdata->name_len);
1640                 wipdata->name_key = ncluster->focus->bref.key;
1641                 wipdata->name_len = ripdata->name_len;
1642                 /* XXX transaction ids */
1643                 hammer2_cluster_modsync(ncluster);
1644                 hammer2_cluster_unlock(ncluster);
1645         }
1646         ripdata = wipdata;
1647
1648         /*
1649          * cluster represents the hardlink target and is now flagged deleted.
1650          * duplicate it to the parent directory and adjust nlinks.
1651          *
1652          * WARNING! The shiftup() call can cause ncluster to be moved into
1653          *          an indirect block, and our ncluster will wind up pointing
1654          *          to the older/original version.
1655          */
1656         KKASSERT(cluster->focus->flags & HAMMER2_CHAIN_DELETED);
1657         hammer2_hardlink_shiftup(trans, cluster, cdip, cdcluster,
1658                                  nlinks, &error);
1659
1660         if (error == 0)
1661                 hammer2_inode_repoint(ip, cdip, cluster);
1662
1663 done:
1664         /*
1665          * Cleanup, cluster/ncluster already dealt with.
1666          *
1667          * Return the shifted cluster in *clusterp.
1668          */
1669         if (cparent)
1670                 hammer2_cluster_unlock(cparent);
1671         *clusterp = cluster;
1672
1673         return (error);
1674 }
1675
1676 /*
1677  * If (*ochainp) is non-NULL it points to the forward OBJTYPE_HARDLINK
1678  * inode while (*chainp) points to the resolved (hidden hardlink
1679  * target) inode.  In this situation when nlinks is 1 we wish to
1680  * deconsolidate the hardlink, moving it back to the directory that now
1681  * represents the only remaining link.
1682  */
1683 int
1684 hammer2_hardlink_deconsolidate(hammer2_trans_t *trans,
1685                                hammer2_inode_t *dip,
1686                                hammer2_chain_t **chainp,
1687                                hammer2_chain_t **ochainp)
1688 {
1689         if (*ochainp == NULL)
1690                 return (0);
1691         /* XXX */
1692         return (0);
1693 }
1694
1695 /*
1696  * The caller presents a locked cluster with an obj_type of
1697  * HAMMER2_OBJTYPE_HARDLINK.  This routine will locate and replace the
1698  * cluster with the target hardlink, also locked.
1699  *
1700  * If cparentp is not NULL a locked cluster representing the hardlink's
1701  * parent is also returned.
1702  *
1703  * If we are unable to locate the hardlink target EIO is returned and
1704  * (*cparentp) is set to NULL.  The passed-in cluster still needs to be
1705  * unlocked by the caller but will be degenerate... not have any chains.
1706  */
1707 int
1708 hammer2_hardlink_find(hammer2_inode_t *dip,
1709                       hammer2_cluster_t **cparentp, hammer2_cluster_t *cluster)
1710 {
1711         const hammer2_inode_data_t *ipdata;
1712         hammer2_cluster_t *cparent;
1713         hammer2_cluster_t *rcluster;
1714         hammer2_inode_t *ip;
1715         hammer2_inode_t *pip;
1716         hammer2_key_t key_dummy;
1717         hammer2_key_t lhc;
1718         int ddflag;
1719
1720         pip = dip;
1721         hammer2_inode_ref(pip);         /* for loop */
1722
1723         /*
1724          * Locate the hardlink.  pip is referenced and not locked.
1725          */
1726         ipdata = &hammer2_cluster_data(cluster)->ipdata;
1727         lhc = ipdata->inum;
1728
1729         /*
1730          * We don't need the cluster's chains, but we need to retain the
1731          * cluster structure itself so we can load the hardlink search
1732          * result into it.
1733          */
1734         KKASSERT(cluster->refs == 1);
1735         atomic_add_int(&cluster->refs, 1);
1736         hammer2_cluster_unlock(cluster);        /* hack */
1737         cluster->nchains = 0;                   /* hack */
1738
1739         rcluster = NULL;
1740         cparent = NULL;
1741
1742         while ((ip = pip) != NULL) {
1743                 cparent = hammer2_inode_lock_ex(ip);
1744                 hammer2_inode_drop(ip);                 /* loop */
1745                 KKASSERT(hammer2_cluster_type(cparent) ==
1746                          HAMMER2_BREF_TYPE_INODE);
1747                 rcluster = hammer2_cluster_lookup(cparent, &key_dummy,
1748                                              lhc, lhc, 0, &ddflag);
1749                 if (rcluster)
1750                         break;
1751                 hammer2_cluster_lookup_done(cparent);   /* discard parent */
1752                 cparent = NULL;                         /* safety */
1753                 pip = ip->pip;          /* safe, ip held locked */
1754                 if (pip)
1755                         hammer2_inode_ref(pip);         /* loop */
1756                 hammer2_inode_unlock_ex(ip, NULL);
1757         }
1758
1759         /*
1760          * chain is locked, ip is locked.  Unlock ip, return the locked
1761          * chain.  *ipp is already set w/a ref count and not locked.
1762          *
1763          * (cparent is already unlocked).
1764          */
1765         if (rcluster) {
1766                 hammer2_cluster_replace(cluster, rcluster);
1767                 hammer2_cluster_drop(rcluster);
1768                 if (cparentp) {
1769                         *cparentp = cparent;
1770                         hammer2_inode_unlock_ex(ip, NULL);
1771                 } else {
1772                         hammer2_inode_unlock_ex(ip, cparent);
1773                 }
1774                 return (0);
1775         } else {
1776                 if (cparentp)
1777                         *cparentp = NULL;
1778                 if (ip)
1779                         hammer2_inode_unlock_ex(ip, cparent);
1780                 return (EIO);
1781         }
1782 }
1783
1784 /*
1785  * Find the directory common to both fdip and tdip.
1786  *
1787  * Returns a held but not locked inode.  Caller typically locks the inode,
1788  * and when through unlocks AND drops it.
1789  */
1790 hammer2_inode_t *
1791 hammer2_inode_common_parent(hammer2_inode_t *fdip, hammer2_inode_t *tdip)
1792 {
1793         hammer2_inode_t *scan1;
1794         hammer2_inode_t *scan2;
1795
1796         /*
1797          * We used to have a depth field but it complicated matters too
1798          * much for directory renames.  So now its ugly.  Check for
1799          * simple cases before giving up and doing it the expensive way.
1800          *
1801          * XXX need a bottom-up topology stability lock
1802          */
1803         if (fdip == tdip || fdip == tdip->pip) {
1804                 hammer2_inode_ref(fdip);
1805                 return(fdip);
1806         }
1807         if (fdip->pip == tdip) {
1808                 hammer2_inode_ref(tdip);
1809                 return(tdip);
1810         }
1811
1812         /*
1813          * XXX not MPSAFE
1814          */
1815         for (scan1 = fdip; scan1->pmp == fdip->pmp; scan1 = scan1->pip) {
1816                 scan2 = tdip;
1817                 while (scan2->pmp == tdip->pmp) {
1818                         if (scan1 == scan2) {
1819                                 hammer2_inode_ref(scan1);
1820                                 return(scan1);
1821                         }
1822                         scan2 = scan2->pip;
1823                         if (scan2 == NULL)
1824                                 break;
1825                 }
1826         }
1827         panic("hammer2_inode_common_parent: no common parent %p %p\n",
1828               fdip, tdip);
1829         /* NOT REACHED */
1830         return(NULL);
1831 }
1832
1833 /*
1834  * Synchronize the inode's frontend state with the chain state prior
1835  * to any explicit flush of the inode or any strategy write call.
1836  *
1837  * Called with a locked inode.
1838  */
1839 void
1840 hammer2_inode_fsync(hammer2_trans_t *trans, hammer2_inode_t *ip, 
1841                     hammer2_cluster_t *cparent)
1842 {
1843         const hammer2_inode_data_t *ripdata;
1844         hammer2_inode_data_t *wipdata;
1845         hammer2_cluster_t *dparent;
1846         hammer2_cluster_t *cluster;
1847         hammer2_key_t lbase;
1848         hammer2_key_t key_next;
1849         int dosync = 0;
1850         int ddflag;
1851
1852         ripdata = &hammer2_cluster_data(cparent)->ipdata;    /* target file */
1853
1854         if (ip->flags & HAMMER2_INODE_MTIME) {
1855                 wipdata = hammer2_cluster_modify_ip(trans, ip, cparent, 0);
1856                 atomic_clear_int(&ip->flags, HAMMER2_INODE_MTIME);
1857                 wipdata->mtime = ip->mtime;
1858                 dosync = 1;
1859                 ripdata = wipdata;
1860         }
1861         if ((ip->flags & HAMMER2_INODE_RESIZED) && ip->size < ripdata->size) {
1862                 wipdata = hammer2_cluster_modify_ip(trans, ip, cparent, 0);
1863                 wipdata->size = ip->size;
1864                 dosync = 1;
1865                 ripdata = wipdata;
1866                 atomic_clear_int(&ip->flags, HAMMER2_INODE_RESIZED);
1867
1868                 /*
1869                  * We must delete any chains beyond the EOF.  The chain
1870                  * straddling the EOF will be pending in the bioq.
1871                  */
1872                 lbase = (ripdata->size + HAMMER2_PBUFMASK64) &
1873                         ~HAMMER2_PBUFMASK64;
1874                 dparent = hammer2_cluster_lookup_init(&ip->cluster, 0);
1875                 cluster = hammer2_cluster_lookup(dparent, &key_next,
1876                                                  lbase, (hammer2_key_t)-1,
1877                                                  HAMMER2_LOOKUP_NODATA,
1878                                                  &ddflag);
1879                 while (cluster) {
1880                         /*
1881                          * Degenerate embedded case, nothing to loop on
1882                          */
1883                         switch (hammer2_cluster_type(cluster)) {
1884                         case HAMMER2_BREF_TYPE_INODE:
1885                                 hammer2_cluster_unlock(cluster);
1886                                 cluster = NULL;
1887                                 break;
1888                         case HAMMER2_BREF_TYPE_DATA:
1889                                 hammer2_cluster_delete(trans, dparent, cluster,
1890                                                    HAMMER2_DELETE_PERMANENT);
1891                                 /* fall through */
1892                         default:
1893                                 cluster = hammer2_cluster_next(dparent, cluster,
1894                                                    &key_next,
1895                                                    key_next, (hammer2_key_t)-1,
1896                                                    HAMMER2_LOOKUP_NODATA);
1897                                 break;
1898                         }
1899                 }
1900                 hammer2_cluster_lookup_done(dparent);
1901         } else
1902         if ((ip->flags & HAMMER2_INODE_RESIZED) && ip->size > ripdata->size) {
1903                 wipdata = hammer2_cluster_modify_ip(trans, ip, cparent, 0);
1904                 wipdata->size = ip->size;
1905                 atomic_clear_int(&ip->flags, HAMMER2_INODE_RESIZED);
1906
1907                 /*
1908                  * When resizing larger we may not have any direct-data
1909                  * available.
1910                  */
1911                 if ((wipdata->op_flags & HAMMER2_OPFLAG_DIRECTDATA) &&
1912                     ip->size > HAMMER2_EMBEDDED_BYTES) {
1913                         wipdata->op_flags &= ~HAMMER2_OPFLAG_DIRECTDATA;
1914                         bzero(&wipdata->u.blockset,
1915                               sizeof(wipdata->u.blockset));
1916                 }
1917                 dosync = 1;
1918                 ripdata = wipdata;
1919         }
1920         if (dosync)
1921                 hammer2_cluster_modsync(cparent);
1922 }