hammer2 - Stabilization
[dragonfly.git] / sys / vfs / hammer2 / hammer2_flush.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
36 #include <sys/cdefs.h>
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/types.h>
40 #include <sys/lock.h>
41 #include <sys/uuid.h>
42
43 #include "hammer2.h"
44
45 #define FLUSH_DEBUG 0
46
47 /*
48  * Recursively flush the specified chain.  The chain is locked and
49  * referenced by the caller and will remain so on return.  The chain
50  * will remain referenced throughout but can temporarily lose its
51  * lock during the recursion to avoid unnecessarily stalling user
52  * processes.
53  */
54 struct hammer2_flush_info {
55         hammer2_chain_t *parent;
56         hammer2_trans_t *trans;
57         int             depth;
58         int             diddeferral;
59         int             pass;
60         int             cache_index;
61         int             domodify;
62         struct h2_flush_deferral_list flush_list;
63         hammer2_tid_t   sync_tid;       /* flush synchronization point */
64 };
65
66 typedef struct hammer2_flush_info hammer2_flush_info_t;
67
68 static void hammer2_chain_flush_core(hammer2_flush_info_t *info,
69                                 hammer2_chain_t **chainp);
70 static int hammer2_chain_flush_scan1(hammer2_chain_t *child, void *data);
71 static int hammer2_chain_flush_scan2(hammer2_chain_t *child, void *data);
72 static void hammer2_rollup_stats(hammer2_chain_t *parent,
73                                 hammer2_chain_t *child, int how);
74
75 #if 0
76 static __inline
77 void
78 hammer2_updatestats(hammer2_flush_info_t *info, hammer2_blockref_t *bref,
79                     int how)
80 {
81         hammer2_key_t bytes;
82
83         if (bref->type != 0) {
84                 bytes = 1 << (bref->data_off & HAMMER2_OFF_MASK_RADIX);
85                 if (bref->type == HAMMER2_BREF_TYPE_INODE)
86                         info->inode_count += how;
87                 if (how < 0)
88                         info->data_count -= bytes;
89                 else
90                         info->data_count += bytes;
91         }
92 }
93 #endif
94
95 /*
96  * Transaction support functions for writing to the filesystem.
97  *
98  * Initializing a new transaction allocates a transaction ID.  We
99  * don't bother marking the volume header MODIFIED.  Instead, the volume
100  * will be synchronized at a later time as part of a larger flush sequence.
101  *
102  * Non-flush transactions can typically run concurrently.  However if
103  * there are non-flush transaction both before AND after a flush trans,
104  * the transactions after stall until the ones before finish.
105  *
106  * Non-flush transactions occuring after a flush pointer can run concurrently
107  * with that flush.  They only have to wait for transactions prior to the
108  * flush trans to complete before they unstall.
109  *
110  * WARNING! Transaction ids are only allocated when the transaction becomes
111  *          active, which allows other transactions to insert ahead of us
112  *          if we are forced to block (only bioq transactions do that).
113  *
114  * WARNING! Modifications to the root volume cannot dup the root volume
115  *          header to handle synchronization points, so alloc_tid can
116  *          wind up (harmlessly) more advanced on flush.
117  */
118 void
119 hammer2_trans_init(hammer2_trans_t *trans, hammer2_pfsmount_t *pmp, int flags)
120 {
121         hammer2_mount_t *hmp;
122         hammer2_trans_t *head;
123
124         bzero(trans, sizeof(*trans));
125         trans->pmp = pmp;
126         hmp = pmp->cluster.chains[0]->hmp;      /* XXX */
127
128         hammer2_voldata_lock(hmp);
129         trans->flags = flags;
130         trans->td = curthread;
131         /*trans->delete_gen = 0;*/      /* multiple deletions within trans */
132
133         if (flags & HAMMER2_TRANS_ISFLUSH) {
134                 /*
135                  * If multiple flushes are trying to run we have to
136                  * wait until it is our turn.  All flushes are serialized.
137                  *
138                  * We queue ourselves and then wait to become the head
139                  * of the queue, allowing all prior flushes to complete.
140                  */
141                 ++hmp->flushcnt;
142                 trans->sync_tid = hmp->voldata.alloc_tid++;
143                 trans->real_tid = trans->sync_tid;
144                 TAILQ_INSERT_TAIL(&hmp->transq, trans, entry);
145                 if (TAILQ_FIRST(&hmp->transq) != trans) {
146                         trans->blocked = 1;
147                         while (trans->blocked) {
148                                 lksleep(&trans->sync_tid, &hmp->voldatalk,
149                                         0, "h2multf", hz);
150                         }
151                 }
152         } else if (hmp->flushcnt == 0) {
153                 /*
154                  * No flushes are pending, we can go.
155                  */
156                 TAILQ_INSERT_TAIL(&hmp->transq, trans, entry);
157                 trans->sync_tid = hmp->voldata.alloc_tid;
158                 trans->real_tid = trans->sync_tid;
159
160                 /* XXX improve/optimize inode allocation */
161         } else {
162                 /*
163                  * One or more flushes are pending.  We insert after
164                  * the current flush and may block.  We have priority
165                  * over any flushes that are not the current flush.
166                  *
167                  * TRANS_BUFCACHE transactions cannot block.
168                  */
169                 TAILQ_FOREACH(head, &hmp->transq, entry) {
170                         if (head->flags & HAMMER2_TRANS_ISFLUSH)
171                                 break;
172                 }
173                 KKASSERT(head);
174                 TAILQ_INSERT_AFTER(&hmp->transq, head, trans, entry);
175                 trans->sync_tid = head->real_tid + 1;
176                 trans->real_tid = trans->sync_tid;
177
178                 if ((trans->flags & HAMMER2_TRANS_BUFCACHE) == 0) {
179                         if (TAILQ_FIRST(&hmp->transq) != head) {
180                                 trans->blocked = 1;
181                                 while (trans->blocked) {
182                                         lksleep(&trans->sync_tid,
183                                                 &hmp->voldatalk, 0,
184                                                 "h2multf", hz);
185                                 }
186                         }
187                 }
188         }
189         if (flags & HAMMER2_TRANS_NEWINODE)
190                 trans->inode_tid = hmp->voldata.inode_tid++;
191         hammer2_voldata_unlock(hmp, 0);
192 }
193
194 void
195 hammer2_trans_done(hammer2_trans_t *trans)
196 {
197         hammer2_mount_t *hmp;
198         hammer2_trans_t *head;
199         hammer2_trans_t *scan;
200
201         hmp = trans->pmp->cluster.chains[0]->hmp;
202
203         /*
204          * Remove and adjust flushcnt
205          */
206         hammer2_voldata_lock(hmp);
207         TAILQ_REMOVE(&hmp->transq, trans, entry);
208         if (trans->flags & HAMMER2_TRANS_ISFLUSH)
209                 --hmp->flushcnt;
210
211         /*
212          * Unblock the head of the queue and any additional transactions
213          * up to the next flush.
214          */
215         head = TAILQ_FIRST(&hmp->transq);
216         if (head && head->blocked) {
217                 head->blocked = 0;
218                 wakeup(&head->sync_tid);
219
220                 scan = TAILQ_NEXT(head, entry);
221                 while (scan && (scan->flags & HAMMER2_TRANS_ISFLUSH) == 0) {
222                         if (scan->blocked) {
223                                 scan->blocked = 0;
224                                 wakeup(&scan->sync_tid);
225                         }
226                         scan = TAILQ_NEXT(scan, entry);
227                 }
228         }
229         hammer2_voldata_unlock(hmp, 0);
230 }
231
232 /*
233  * Flush the chain and all modified sub-chains through the specified
234  * synchronization point (sync_tid), propagating parent chain modifications
235  * and mirror_tid updates back up as needed.  Since we are recursing downward
236  * we do not have to deal with the complexities of multi-homed chains (chains
237  * with multiple parents).
238  *
239  * Caller must have interlocked against any non-flush-related modifying
240  * operations in progress whos modify_tid values are less than or equal
241  * to the passed sync_tid.
242  *
243  * Caller must have already vetted synchronization points to ensure they
244  * are properly flushed.  Only snapshots and cluster flushes can create
245  * these sorts of synchronization points.
246  *
247  * This routine can be called from several places but the most important
248  * is from the hammer2_vop_reclaim() function.  We want to try to completely
249  * clean out the inode structure to prevent disconnected inodes from
250  * building up and blowing out the kmalloc pool.  However, it is not actually
251  * necessary to flush reclaimed inodes to maintain HAMMER2's crash recovery
252  * capability.
253  *
254  * chain is locked on call and will remain locked on return.  If a flush
255  * occured, the chain's MOVED bit will be set indicating that its parent
256  * (which is not part of the flush) should be updated.  The chain may be
257  * replaced by the call.
258  */
259 void
260 hammer2_chain_flush(hammer2_trans_t *trans, hammer2_chain_t **chainp)
261 {
262         hammer2_chain_t *chain = *chainp;
263         hammer2_chain_t *scan;
264         hammer2_chain_core_t *core;
265         hammer2_flush_info_t info;
266         int loops;
267
268         /*
269          * Execute the recursive flush and handle deferrals.
270          *
271          * Chains can be ridiculously long (thousands deep), so to
272          * avoid blowing out the kernel stack the recursive flush has a
273          * depth limit.  Elements at the limit are placed on a list
274          * for re-execution after the stack has been popped.
275          */
276         bzero(&info, sizeof(info));
277         TAILQ_INIT(&info.flush_list);
278         info.trans = trans;
279         info.sync_tid = trans->sync_tid;
280         info.cache_index = -1;
281
282         core = chain->core;
283 #if FLUSH_DEBUG
284         kprintf("CHAIN FLUSH trans %p.%016jx chain %p.%d mod %016jx upd %016jx\n", trans, trans->sync_tid, chain, chain->bref.type, chain->modify_tid, core->update_lo);
285 #endif
286
287         /*
288          * Extra ref needed because flush_core expects it when replacing
289          * chain.
290          */
291         hammer2_chain_ref(chain);
292         loops = 0;
293
294         for (;;) {
295                 /*
296                  * Unwind deep recursions which had been deferred.  This
297                  * can leave MOVED set for these chains, which will be
298                  * handled when we [re]flush chain after the unwind.
299                  */
300                 while ((scan = TAILQ_FIRST(&info.flush_list)) != NULL) {
301                         KKASSERT(scan->flags & HAMMER2_CHAIN_DEFERRED);
302                         TAILQ_REMOVE(&info.flush_list, scan, flush_node);
303                         atomic_clear_int(&scan->flags, HAMMER2_CHAIN_DEFERRED);
304
305                         /*
306                          * Now that we've popped back up we can do a secondary
307                          * recursion on the deferred elements.
308                          *
309                          * NOTE: hammer2_chain_flush() may replace scan.
310                          */
311                         if (hammer2_debug & 0x0040)
312                                 kprintf("deferred flush %p\n", scan);
313                         hammer2_chain_lock(scan, HAMMER2_RESOLVE_MAYBE);
314                         hammer2_chain_drop(scan);       /* ref from deferral */
315                         hammer2_chain_flush(trans, &scan);
316                         hammer2_chain_unlock(scan);
317                 }
318
319                 /*
320                  * [re]flush chain.
321                  */
322                 info.diddeferral = 0;
323                 hammer2_chain_flush_core(&info, &chain);
324 #if FLUSH_DEBUG
325                 kprintf("flush_core_done parent=<base> chain=%p.%d %08x\n",
326                         chain, chain->bref.type, chain->flags);
327 #endif
328
329                 /*
330                  * Only loop if deep recursions have been deferred.
331                  */
332                 if (TAILQ_EMPTY(&info.flush_list))
333                         break;
334
335                 if (++loops % 1000 == 0) {
336                         kprintf("hammer2_chain_flush: excessive loops on %p\n",
337                                 chain);
338                         if (hammer2_debug & 0x100000)
339                                 Debugger("hell4");
340                 }
341         }
342         hammer2_chain_drop(chain);
343         *chainp = chain;
344 }
345
346 /*
347  * This is the core of the chain flushing code.  The chain is locked by the
348  * caller and must also have an extra ref on it by the caller, and remains
349  * locked and will have an extra ref on return.
350  *
351  * If the flush accomplished any work chain will be flagged MOVED
352  * indicating a copy-on-write propagation back up is required.
353  * Deep sub-nodes may also have been entered onto the deferral list.
354  * MOVED is never set on the volume root.
355  *
356  * NOTE: modify_tid is different from MODIFIED.  modify_tid is updated
357  *       only when a chain is specifically modified, and not updated
358  *       for copy-on-write propagations.  MODIFIED is set on any modification
359  *       including copy-on-write propagations.
360  *
361  * NOTE: We are responsible for updating chain->bref.mirror_tid and
362  *       core->update_lo  The caller is responsible for processing us into
363  *       our parent (if any).
364  *
365  *       We are also responsible for updating chain->core->update_lo to
366  *       prevent repeated recursions due to deferrals.
367  */
368 static void
369 hammer2_chain_flush_core(hammer2_flush_info_t *info, hammer2_chain_t **chainp)
370 {
371         hammer2_chain_t *chain = *chainp;
372         hammer2_mount_t *hmp;
373         hammer2_blockref_t *bref;
374         hammer2_off_t pbase;
375         hammer2_off_t pmask;
376 #if 0
377         hammer2_trans_t *trans = info->trans;
378 #endif
379         hammer2_chain_core_t *core;
380         size_t psize;
381         size_t boff;
382         char *bdata;
383         struct buf *bp;
384         int error;
385         int wasmodified;
386         int diddeferral;
387
388         hmp = chain->hmp;
389         core = chain->core;
390         diddeferral = info->diddeferral;
391
392 #if FLUSH_DEBUG
393         if (info->parent)
394                 kprintf("flush_core %p->%p.%d %08x (%s)\n",
395                         info->parent, chain, chain->bref.type,
396                         chain->flags,
397                         ((chain->bref.type == HAMMER2_BREF_TYPE_INODE) ?
398                                 (char *)chain->data->ipdata.filename : "?"));
399         else
400                 kprintf("flush_core NULL->%p.%d %08x (%s)\n",
401                         chain, chain->bref.type,
402                         chain->flags,
403                         ((chain->bref.type == HAMMER2_BREF_TYPE_INODE) ?
404                                 (char *)chain->data->ipdata.filename : "?"));
405         kprintf("PUSH   %p.%d %08x mod=%016jx del=%016jx mirror=%016jx (sync %016jx, update_lo %016jx)\n", chain, chain->bref.type, chain->flags, chain->modify_tid, chain->delete_tid, chain->bref.mirror_tid, info->sync_tid, core->update_lo);
406 #endif
407
408         /*
409          * Check if we even have any work to do.
410          *
411          * We do not update bref.mirror_tid if nothing is being modified.
412          * We do not update core->update_lo because there might be other
413          * paths to the core and we haven't actually checked it.
414          *
415          * This bit of code is capable of short-cutting entire sub-trees
416          * if they have not been touched.
417          */
418         if ((chain->flags & HAMMER2_CHAIN_MODIFIED) == 0 &&
419             (core->update_lo >= info->sync_tid ||
420              chain->bref.mirror_tid >= info->sync_tid ||
421              chain->bref.mirror_tid >= core->update_hi)) {
422                 return;
423         }
424
425         /*
426          * Ignore chains modified beyond the current flush point.  These
427          * will be treated as if they did not exist.  Subchains with lower
428          * modify_tid's will still be accessible via other parents.
429          *
430          * Do not update bref.mirror_tid here, it will interfere with
431          * synchronization.  e.g. inode flush tid 1, concurrent D-D tid 2,
432          * then later on inode flush tid 2.  If we were to set mirror_tid
433          * to 1 during inode flush tid 1 the blockrefs would only be partially
434          * updated (and likely panic).
435          *
436          * Do not update core->update_lo here, there might be other paths
437          * to the core and we haven't actually flushed it.
438          *
439          * (vchain and fchain are exceptions since they cannot be duplicated)
440          */
441         if (chain->modify_tid > info->sync_tid &&
442             chain != &hmp->fchain && chain != &hmp->vchain) {
443                 chain->debug_reason = (chain->debug_reason & ~255) | 5;
444                 /* do not update bref.mirror_tid */
445                 /* do not update core->update_lo, there may be another path */
446                 return;
447         }
448
449 retry:
450         /*
451          * Early handling of deleted chains is required to avoid double
452          * recursions.  If the deleted chain has been duplicated then the
453          * flush will have visibility into chain->core via some other chain
454          * and we can safely terminate the operation right here.
455          *
456          * If the deleted chain has not been duplicated then the deletion
457          * is terminal and we must recurse to deal with any dirty chains
458          * under the deletion, including possibly flushing them out (e.g.
459          * open descriptor on an unlinked file).
460          *
461          * Do not update bref.mirror_tid here, the chain still has a data
462          * state based on mirror_tid and might be duplicated again (though
463          * I don't think this can occur).
464          */
465         if (chain->delete_tid <= info->sync_tid &&
466             (chain->flags & HAMMER2_CHAIN_DUPLICATED)) {
467                 chain->debug_reason = (chain->debug_reason & ~255) | 9;
468                 if (chain->flags & HAMMER2_CHAIN_MODIFIED) {
469                         if (chain->bp) {
470                                 if (chain->bytes == chain->bp->b_bufsize)
471                                         chain->bp->b_flags |= B_INVAL|B_RELBUF;
472                         }
473                         if ((chain->flags & HAMMER2_CHAIN_MOVED) == 0) {
474                                 hammer2_chain_ref(chain);
475                                 atomic_set_int(&chain->flags,
476                                                HAMMER2_CHAIN_MOVED);
477                         }
478                         atomic_clear_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
479                         hammer2_chain_drop(chain);
480                 }
481                 if (chain->bref.mirror_tid < info->sync_tid)
482                         chain->bref.mirror_tid = info->sync_tid;
483                 /* do not update core->update_lo, there may be another path */
484                 return;
485         }
486
487         /*
488          * Recurse if we are not up-to-date.  Once we are done we will
489          * update update_lo if there were no deferrals.  update_lo can become
490          * higher than update_hi and is used to prevent re-recursions during
491          * the same flush cycle.
492          *
493          * update_hi was already checked and prevents initial recursions on
494          * subtrees which have not been modified.
495          *
496          * NOTE: We must recurse whether chain is flagged DELETED or not.
497          *       However, if it is flagged DELETED we limit sync_tid to
498          *       delete_tid to ensure that the chain's bref.mirror_tid is
499          *       not fully updated and causes it to miss the non-DELETED
500          *       path.
501          *
502          * NOTE: If a deferral occurs hammer2_chain_flush() will flush the
503          *       deferred chain independently which will update it's
504          *       bref.mirror_tid and prevent it from deferred again.
505          */
506         if (chain->bref.mirror_tid < info->sync_tid &&
507             chain->bref.mirror_tid < core->update_hi) {
508                 hammer2_chain_t *saved_parent;
509                 hammer2_chain_layer_t *layer;
510                 int saved_domodify;
511                 int save_gen;
512
513                 /*
514                  * Races will bump update_hi above trans->sync_tid causing
515                  * us to catch the issue in a later flush.
516                  *
517                  * We don't want to set our chain to MODIFIED gratuitously.
518                  *
519                  * We need an extra ref on chain because we are going to
520                  * release its lock temporarily in our child loop.
521                  */
522
523                 /*
524                  * Run two passes.  The first pass handles MODIFIED and
525                  * update_lo recursions while the second pass handles
526                  * MOVED chains on the way back up.
527                  *
528                  * If the stack gets too deep we defer the chain.   Since
529                  * hammer2_chain_core's can be shared at multiple levels
530                  * in the tree, we may encounter a chain that we had already
531                  * deferred.  We could undefer it but it will probably just
532                  * defer again so it is best to leave it deferred.
533                  *
534                  * Scan1 is recursive.
535                  *
536                  * NOTE: The act of handling a modified/submodified chain can
537                  *       cause the MOVED Flag to be set.  It can also be set
538                  *       via hammer2_chain_delete() and in other situations.
539                  *
540                  * NOTE: RB_SCAN() must be used instead of RB_FOREACH()
541                  *       because children can be physically removed during
542                  *       the scan.
543                  *
544                  * NOTE: We would normally not care about insertions except
545                  *       that some insertions might occur from the flush
546                  *       itself, so loop on generation number changes.
547                  */
548                 saved_parent = info->parent;
549                 saved_domodify = info->domodify;
550                 info->parent = chain;
551                 info->domodify = 0;
552                 chain->debug_reason = (chain->debug_reason & ~255) | 6;
553
554                 if (chain->flags & HAMMER2_CHAIN_DEFERRED) {
555                         ++info->diddeferral;
556                 } else if (info->depth == HAMMER2_FLUSH_DEPTH_LIMIT) {
557                         if ((chain->flags & HAMMER2_CHAIN_DEFERRED) == 0) {
558                                 hammer2_chain_ref(chain);
559                                 TAILQ_INSERT_TAIL(&info->flush_list,
560                                                   chain, flush_node);
561                                 atomic_set_int(&chain->flags,
562                                                HAMMER2_CHAIN_DEFERRED);
563                         }
564                         ++info->diddeferral;
565                 } else {
566                         spin_lock(&core->cst.spin);
567                         KKASSERT(core->good == 0x1234 && core->sharecnt > 0);
568                         do {
569                                 save_gen = core->generation;
570                                 TAILQ_FOREACH_REVERSE(layer, &core->layerq,
571                                                       h2_layer_list, entry) {
572                                         ++layer->refs;
573                                         KKASSERT(layer->good == 0xABCD);
574                                         RB_SCAN(hammer2_chain_tree,
575                                                 &layer->rbtree,
576                                                 NULL, hammer2_chain_flush_scan1,
577                                                 info);
578                                         --layer->refs;
579                                 }
580                         } while (core->generation != save_gen);
581                         spin_unlock(&core->cst.spin);
582                 }
583
584                 if (info->parent != chain) {
585                         kprintf("ZZZ\n");
586                         hammer2_chain_drop(chain);
587                         hammer2_chain_ref(info->parent);
588                 }
589                 chain = info->parent;
590
591                 /*
592                  * We unlock the parent during the scan1 recursion, parent
593                  * may have been deleted out from under us.
594                  * parent may have been destroyed out from under us
595                  */
596                 if (chain->delete_tid <= info->sync_tid &&
597                     (chain->flags & HAMMER2_CHAIN_DUPLICATED)) {
598                         kprintf("xxx\n");
599                         goto retry;
600                 }
601                 if (chain->bref.mirror_tid >= info->sync_tid ||
602                     chain->bref.mirror_tid >= core->update_hi) {
603                         kprintf("yyy\n");
604                         goto retry;
605                 }
606
607                 /*
608                  * Blockrefs are only updated on live chains.
609                  *
610                  * We are possibly causing a delete-duplicate from inside the
611                  * flush itself.  The parent might be live or might have been
612                  * deleted concurrently in a post-flush transaction.  If
613                  * the parent was deleted our modified chain will also be
614                  * marked deleted, but since it inherits the parent's
615                  * delete_tid it will still appear to be 'live' for the
616                  * purposes of the flush.
617                  *
618                  * There may also be a side-effect due to the freemap
619                  * allocation when flushing the freemap.  See freemap_alloc(),
620                  * and it is also possible that a shared core causes parent
621                  * to have already been delete-duplicated.
622                  */
623                 if (info->domodify && chain->delete_tid > info->sync_tid) {
624                         hammer2_chain_modify(info->trans, &info->parent,
625                                              HAMMER2_MODIFY_NO_MODIFY_TID);
626                         if (info->parent != chain) {
627                                 hammer2_chain_drop(chain);
628                                 hammer2_chain_ref(info->parent);
629                         }
630                         chain = info->parent;
631                 }
632                 if (info->domodify)
633                         KKASSERT(chain->modify_tid == info->sync_tid);
634                 chain->debug_reason = (chain->debug_reason & ~255) | 7;
635
636                 KKASSERT(chain == info->parent);
637
638                 /*
639                  * Handle successfully flushed children who are in the MOVED
640                  * state on the way back up the recursion.  This can have
641                  * the side-effect of clearing MOVED.
642                  *
643                  * Scan2 may replace info->parent.  If it does it will also
644                  * replace the extra ref we made.
645                  *
646                  * Scan2 is non-recursive.
647                  */
648                 if (diddeferral != info->diddeferral) {
649                         spin_lock(&core->cst.spin);
650                 } else {
651                         spin_lock(&core->cst.spin);
652                         KKASSERT(core->good == 0x1234 && core->sharecnt > 0);
653                         KKASSERT(info->parent->core == core);
654                         TAILQ_FOREACH_REVERSE(layer, &core->layerq,
655                                               h2_layer_list, entry) {
656                                 info->pass = 1;
657                                 ++layer->refs;
658                                 KKASSERT(layer->good == 0xABCD);
659                                 RB_SCAN(hammer2_chain_tree, &layer->rbtree,
660                                         NULL, hammer2_chain_flush_scan2, info);
661                                 info->pass = 2;
662                                 RB_SCAN(hammer2_chain_tree, &layer->rbtree,
663                                         NULL, hammer2_chain_flush_scan2, info);
664                                 --layer->refs;
665                         }
666
667                         /*
668                          * chain is now considered up-to-date, adjust
669                          * bref.mirror_tid and update_lo before running
670                          * pass3.
671                          *
672                          * (no deferral in this path)
673                          */
674                         if (chain->bref.mirror_tid < info->sync_tid)
675                                 chain->bref.mirror_tid = info->sync_tid;
676                         if (core->update_lo < info->sync_tid)
677                                 core->update_lo = info->sync_tid;
678
679                         TAILQ_FOREACH_REVERSE(layer, &core->layerq,
680                                               h2_layer_list, entry) {
681                                 info->pass = 3;
682                                 ++layer->refs;
683                                 KKASSERT(layer->good == 0xABCD);
684                                 RB_SCAN(hammer2_chain_tree, &layer->rbtree,
685                                         NULL, hammer2_chain_flush_scan2, info);
686                                 --layer->refs;
687                                 KKASSERT(info->parent->core == core);
688                         }
689                 }
690
691                 /*
692                  * info->parent must not have been replaced again
693                  */
694                 KKASSERT(info->parent == chain);
695
696                 chain->debug_reason = (chain->debug_reason & ~255) | 8;
697                 *chainp = chain;
698
699                 hammer2_chain_layer_check_locked(chain->hmp, core);
700                 spin_unlock(&core->cst.spin);
701
702                 info->parent = saved_parent;
703                 info->domodify = saved_domodify;
704                 KKASSERT(chain->refs > 1);
705         } else {
706                 /*
707                  * chain is now considered up-to-date, adjust
708                  * bref.mirror_tid and update_lo.
709                  *
710                  * (no deferral in this path)
711                  */
712                 if (chain->bref.mirror_tid < info->sync_tid)
713                         chain->bref.mirror_tid = info->sync_tid;
714                 if (core->update_lo < info->sync_tid)
715                         core->update_lo = info->sync_tid;
716
717         }
718
719 #if FLUSH_DEBUG
720         kprintf("POP    %p.%d defer=%d\n", chain, chain->bref.type, diddeferral);
721 #endif
722
723         /*
724          * Do not flush chain if there were any deferrals.  It will be
725          * retried later after the deferrals are independently handled.
726          * Do not update update_lo or bref.mirror_tid.
727          */
728         if (diddeferral != info->diddeferral) {
729                 chain->debug_reason = (chain->debug_reason & ~255) | 99;
730                 if (hammer2_debug & 0x0008) {
731                         kprintf("%*.*s} %p/%d %04x (deferred)",
732                                 info->depth, info->depth, "",
733                                 chain, chain->refs, chain->flags);
734                 }
735                 /* do not update core->update_lo */
736                 return;
737         }
738
739         /*
740          * non-deferral path - mirror_tid and update_lo have been updated
741          * at this point.
742          *
743          * Deal with deleted chains on the way back up.  This occurs when
744          * the chain is terminal (which required us to recurse on it above).
745          * We had to flush the contents but we don't have to synchronize the
746          * parent.
747          *
748          * NOTE:  scan2 has already executed above so statistics have
749          *        already been rolled up.
750          *
751          * NOTE:  Deletions do not prevent flush recursion as a deleted
752          *        inode (removed file) which is still open may still require
753          *        on-media storage to be able to clean related pages out from
754          *        the system caches.
755          */
756         if (chain->delete_tid <= info->sync_tid) {
757                 chain->debug_reason = (chain->debug_reason & ~255) | 9;
758                 if (chain->flags & HAMMER2_CHAIN_MODIFIED) {
759                         if (chain->bp) {
760                                 if (chain->bytes == chain->bp->b_bufsize)
761                                         chain->bp->b_flags |= B_INVAL|B_RELBUF;
762                         }
763                         if ((chain->flags & HAMMER2_CHAIN_MOVED) == 0) {
764                                 hammer2_chain_ref(chain);
765                                 atomic_set_int(&chain->flags,
766                                                HAMMER2_CHAIN_MOVED);
767                         }
768                         atomic_clear_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
769                         hammer2_chain_drop(chain);
770                 }
771                 return;
772         }
773 #if 0
774         if ((chain->flags & HAMMER2_CHAIN_DESTROYED) &&
775             (chain->flags & HAMMER2_CHAIN_DELETED) &&
776             (trans->flags & HAMMER2_TRANS_RESTRICTED) == 0) {
777                 /*
778                  * Throw-away the MODIFIED flag
779                  */
780                 if (chain->flags & HAMMER2_CHAIN_MODIFIED) {
781                         if (chain->bp) {
782                                 if (chain->bytes == chain->bp->b_bufsize)
783                                         chain->bp->b_flags |= B_INVAL|B_RELBUF;
784                         }
785                         atomic_clear_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
786                         hammer2_chain_drop(chain);
787                 }
788                 return;
789         }
790 #endif
791
792         /*
793          * A degenerate flush might not have flushed anything and thus not
794          * processed modified blocks on the way back up.  Detect the case.
795          * bref.mirror_tid may still propagate upward but won't be flushed
796          * if no modifications were actually made.
797          *
798          * Note that MOVED can be set without MODIFIED being set due to
799          * a deletion, in which case it is handled by Scan2 later on.
800          *
801          * Both bits can be set along with DELETED due to a deletion if
802          * modified data within the synchronization zone and the chain
803          * was then deleted beyond the zone, in which case we still have
804          * to flush for synchronization point consistency.  Otherwise though
805          * DELETED and MODIFIED are treated as separate flags.
806          */
807         if ((chain->flags & HAMMER2_CHAIN_MODIFIED) == 0) {
808                 kprintf("chain %p.%d %08x recursed but wasn't modified mirr=%016jx update_lo=%016jx synctid=%016jx\n",
809                         chain, chain->bref.type, chain->flags, chain->bref.mirror_tid, core->update_lo, info->sync_tid);
810                 if ((chain->flags & HAMMER2_CHAIN_MOVED) == 0) {
811                         hammer2_chain_ref(chain);
812                         atomic_set_int(&chain->flags, HAMMER2_CHAIN_MOVED);
813                 }
814                 chain->debug_reason = (chain->debug_reason & ~255) | 10;
815                 return;
816         }
817         chain->debug_reason = (chain->debug_reason & ~255) | 11;
818
819         /*
820          * Issue flush.
821          *
822          * A DESTROYED node that reaches this point must be flushed for
823          * synchronization point consistency.
824          */
825
826         /*
827          * Update mirror_tid, clear MODIFIED, and set MOVED.
828          *
829          * The caller will update the parent's reference to this chain
830          * by testing MOVED as long as the modification was in-bounds.
831          *
832          * MOVED is never set on the volume root as there is no parent
833          * to adjust.
834          */
835         if (hammer2_debug & 0x1000) {
836                 kprintf("Flush %p.%d %016jx/%d sync_tid %016jx\n",
837                         chain, chain->bref.type,
838                         chain->bref.key, chain->bref.keybits,
839                         info->sync_tid);
840         }
841         if (hammer2_debug & 0x2000) {
842                 Debugger("Flush hell");
843         }
844         wasmodified = (chain->flags & HAMMER2_CHAIN_MODIFIED) != 0;
845         atomic_clear_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
846         if (chain == &hmp->vchain)
847                 kprintf("(FLUSHED VOLUME HEADER)\n");
848         if (chain == &hmp->fchain)
849                 kprintf("(FLUSHED FREEMAP HEADER)\n");
850
851         if ((chain->flags & HAMMER2_CHAIN_MOVED) ||
852             chain == &hmp->vchain ||
853             chain == &hmp->fchain) {
854                 /*
855                  * Drop the ref from the MODIFIED bit we cleared.
856                  * Net is -0 or -1 ref depending.
857                  */
858                 if (wasmodified)
859                         hammer2_chain_drop(chain);
860         } else {
861                 /*
862                  * Drop the ref from the MODIFIED bit we cleared and
863                  * set a ref for the MOVED bit we are setting.  Net
864                  * is +0 or +1 ref depending.
865                  */
866                 if (wasmodified == 0)
867                         hammer2_chain_ref(chain);
868                 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MOVED);
869         }
870
871         /*
872          * If this is part of a recursive flush we can go ahead and write
873          * out the buffer cache buffer and pass a new bref back up the chain
874          * via the MOVED bit.
875          *
876          * Volume headers are NOT flushed here as they require special
877          * processing.
878          */
879         switch(chain->bref.type) {
880         case HAMMER2_BREF_TYPE_FREEMAP:
881                 hammer2_modify_volume(hmp);
882                 break;
883         case HAMMER2_BREF_TYPE_VOLUME:
884                 /*
885                  * We should flush the free block table before we calculate
886                  * CRCs and copy voldata -> volsync.
887                  *
888                  * To prevent SMP races, fchain must remain locked until
889                  * voldata is copied to volsync.
890                  */
891                 hammer2_chain_lock(&hmp->fchain, HAMMER2_RESOLVE_ALWAYS);
892                 if ((hmp->fchain.flags & HAMMER2_CHAIN_MODIFIED) ||
893                     hmp->voldata.freemap_tid < info->trans->sync_tid) {
894                         /* this will modify vchain as a side effect */
895                         hammer2_chain_t *tmp = &hmp->fchain;
896                         hammer2_chain_flush(info->trans, &tmp);
897                         KKASSERT(tmp == &hmp->fchain);
898                 }
899
900                 /*
901                  * The volume header is flushed manually by the syncer, not
902                  * here.  All we do is adjust the crc's.
903                  */
904                 KKASSERT(chain->data != NULL);
905                 KKASSERT(chain->bp == NULL);
906
907                 hmp->voldata.icrc_sects[HAMMER2_VOL_ICRC_SECT1]=
908                         hammer2_icrc32(
909                                 (char *)&hmp->voldata +
910                                  HAMMER2_VOLUME_ICRC1_OFF,
911                                 HAMMER2_VOLUME_ICRC1_SIZE);
912                 hmp->voldata.icrc_sects[HAMMER2_VOL_ICRC_SECT0]=
913                         hammer2_icrc32(
914                                 (char *)&hmp->voldata +
915                                  HAMMER2_VOLUME_ICRC0_OFF,
916                                 HAMMER2_VOLUME_ICRC0_SIZE);
917                 hmp->voldata.icrc_volheader =
918                         hammer2_icrc32(
919                                 (char *)&hmp->voldata +
920                                  HAMMER2_VOLUME_ICRCVH_OFF,
921                                 HAMMER2_VOLUME_ICRCVH_SIZE);
922                 hmp->volsync = hmp->voldata;
923                 atomic_set_int(&chain->flags, HAMMER2_CHAIN_VOLUMESYNC);
924                 hammer2_chain_unlock(&hmp->fchain);
925                 break;
926         case HAMMER2_BREF_TYPE_DATA:
927                 /*
928                  * Data elements have already been flushed via the logical
929                  * file buffer cache.  Their hash was set in the bref by
930                  * the vop_write code.
931                  *
932                  * Make sure any device buffer(s) have been flushed out here.
933                  * (there aren't usually any to flush).
934                  */
935                 psize = hammer2_devblksize(chain->bytes);
936                 pmask = (hammer2_off_t)psize - 1;
937                 pbase = chain->bref.data_off & ~pmask;
938                 boff = chain->bref.data_off & (HAMMER2_OFF_MASK & pmask);
939
940                 bp = getblk(hmp->devvp, pbase, psize, GETBLK_NOWAIT, 0);
941                 if (bp) {
942                         if ((bp->b_flags & (B_CACHE | B_DIRTY)) ==
943                             (B_CACHE | B_DIRTY)) {
944                                 cluster_awrite(bp);
945                         } else {
946                                 bp->b_flags |= B_RELBUF;
947                                 brelse(bp);
948                         }
949                 }
950                 break;
951 #if 0
952         case HAMMER2_BREF_TYPE_INDIRECT:
953                 /*
954                  * Indirect blocks may be in an INITIAL state.  Use the
955                  * chain_lock() call to ensure that the buffer has been
956                  * instantiated (even though it is already locked the buffer
957                  * might not have been instantiated).
958                  *
959                  * Only write the buffer out if it is dirty, it is possible
960                  * the operating system had already written out the buffer.
961                  */
962                 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS);
963                 KKASSERT(chain->bp != NULL);
964
965                 bp = chain->bp;
966                 if ((chain->flags & HAMMER2_CHAIN_DIRTYBP) ||
967                     (bp->b_flags & B_DIRTY)) {
968                         bdwrite(chain->bp);
969                 } else {
970                         brelse(chain->bp);
971                 }
972                 chain->bp = NULL;
973                 chain->data = NULL;
974                 hammer2_chain_unlock(chain);
975                 break;
976 #endif
977         case HAMMER2_BREF_TYPE_INDIRECT:
978         case HAMMER2_BREF_TYPE_FREEMAP_NODE:
979                 /*
980                  * Device-backed.  Buffer will be flushed by the sync
981                  * code XXX.
982                  */
983                 KKASSERT((chain->flags & HAMMER2_CHAIN_EMBEDDED) == 0);
984                 break;
985         case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
986         default:
987                 /*
988                  * Embedded elements have to be flushed out.
989                  * (Basically just BREF_TYPE_INODE).
990                  */
991                 KKASSERT(chain->flags & HAMMER2_CHAIN_EMBEDDED);
992                 KKASSERT(chain->data != NULL);
993                 KKASSERT(chain->bp == NULL);
994                 bref = &chain->bref;
995
996                 KKASSERT((bref->data_off & HAMMER2_OFF_MASK) != 0);
997                 KKASSERT(HAMMER2_DEC_CHECK(chain->bref.methods) ==
998                          HAMMER2_CHECK_ISCSI32 ||
999                          HAMMER2_DEC_CHECK(chain->bref.methods) ==
1000                          HAMMER2_CHECK_FREEMAP);
1001
1002                 /*
1003                  * The data is embedded, we have to acquire the
1004                  * buffer cache buffer and copy the data into it.
1005                  */
1006                 psize = hammer2_devblksize(chain->bytes);
1007                 pmask = (hammer2_off_t)psize - 1;
1008                 pbase = bref->data_off & ~pmask;
1009                 boff = bref->data_off & (HAMMER2_OFF_MASK & pmask);
1010
1011                 /*
1012                  * The getblk() optimization can only be used if the
1013                  * physical block size matches the request.
1014                  */
1015                 error = bread(hmp->devvp, pbase, psize, &bp);
1016                 KKASSERT(error == 0);
1017
1018                 bdata = (char *)bp->b_data + boff;
1019
1020                 /*
1021                  * Copy the data to the buffer, mark the buffer
1022                  * dirty, and convert the chain to unmodified.
1023                  */
1024                 bcopy(chain->data, bdata, chain->bytes);
1025                 bp->b_flags |= B_CLUSTEROK;
1026                 bdwrite(bp);
1027                 bp = NULL;
1028
1029                 switch(HAMMER2_DEC_CHECK(chain->bref.methods)) {
1030                 case HAMMER2_CHECK_FREEMAP:
1031                         chain->bref.check.freemap.icrc32 =
1032                                 hammer2_icrc32(chain->data, chain->bytes);
1033                         break;
1034                 case HAMMER2_CHECK_ISCSI32:
1035                         chain->bref.check.iscsi32.value =
1036                                 hammer2_icrc32(chain->data, chain->bytes);
1037                         break;
1038                 default:
1039                         panic("hammer2_flush_core: bad crc type");
1040                         break; /* NOT REACHED */
1041                 }
1042                 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE)
1043                         ++hammer2_iod_meta_write;
1044                 else
1045                         ++hammer2_iod_indr_write;
1046         }
1047 }
1048
1049 /*
1050  * Flush helper scan1 (recursive)
1051  *
1052  * Flushes the children of the caller's chain (parent) and updates
1053  * the blockref, restricted by sync_tid.
1054  *
1055  * Ripouts during the loop should not cause any problems.  Because we are
1056  * flushing to a synchronization point, modification races will occur after
1057  * sync_tid and do not have to be flushed anyway.
1058  *
1059  * It is also ok if the parent is chain_duplicate()'d while unlocked because
1060  * the delete/duplication will install a delete_tid that is still larger than
1061  * our current sync_tid.
1062  *
1063  * WARNING! If we do not call chain_flush_core we must update bref.mirror_tid
1064  *          ourselves.
1065  */
1066 static int
1067 hammer2_chain_flush_scan1(hammer2_chain_t *child, void *data)
1068 {
1069         hammer2_flush_info_t *info = data;
1070         hammer2_trans_t *trans = info->trans;
1071         hammer2_chain_t *parent = info->parent;
1072         int     diddeferral;
1073
1074         if (hammer2_debug & 0x80000)
1075                 Debugger("hell3");
1076         diddeferral = info->diddeferral;
1077
1078         /*
1079          * Child is beyond the flush synchronization zone, don't persue.
1080          * Remember that modifications generally delete-duplicate so if the
1081          * sub-tree is dirty another child will get us there.  But not this
1082          * one.
1083          *
1084          * Or MODIFIED is not set and child is already fully synchronized
1085          * with its sub-tree.  Don't persue.
1086          */
1087         if (child->modify_tid > trans->sync_tid) {
1088                 KKASSERT(child->delete_tid >= child->modify_tid);
1089                 child->debug_reason = (child->debug_reason & ~255) | 1;
1090                 /* do not update child->core->update_lo, core not flushed */
1091                 /* do not update core->update_lo, there may be another path */
1092                 return (0);
1093         }
1094
1095         /*
1096          * We must ref the child before unlocking the spinlock.
1097          *
1098          * The caller has added a ref to the parent so we can temporarily
1099          * unlock it in order to lock the child.
1100          */
1101         hammer2_chain_ref(child);
1102         spin_unlock(&parent->core->cst.spin);
1103
1104         hammer2_chain_unlock(parent);
1105         hammer2_chain_lock(child, HAMMER2_RESOLVE_MAYBE);
1106
1107         /*
1108          * The DESTROYED flag can only be initially set on an unreferenced
1109          * deleted inode and will propagate downward via the mechanic below.
1110          * Such inode chains have been deleted for good and should no longer
1111          * be subject to delete/duplication.
1112          *
1113          * This optimization allows the inode reclaim (destroy unlinked file
1114          * on vnode reclamation after last close) to be flagged by just
1115          * setting HAMMER2_CHAIN_DESTROYED at the top level and then will
1116          * cause the chains to be terminated and related buffers to be
1117          * invalidated and not flushed out.
1118          *
1119          * We have to be careful not to propagate the DESTROYED flag if
1120          * the destruction occurred after our flush sync_tid.
1121          */
1122         if ((parent->flags & HAMMER2_CHAIN_DESTROYED) &&
1123             (child->flags & HAMMER2_CHAIN_DELETED) &&
1124             (child->flags & HAMMER2_CHAIN_DESTROYED) == 0) {
1125                 /*
1126                  * Force downward recursion by bringing update_hi up to
1127                  * at least sync_tid.  Parent's mirror_tid has not yet
1128                  * been updated.
1129                  *
1130                  * Vnode reclamation may have forced update_hi to MAX_TID
1131                  * (we do this because there was no transaction at the time).
1132                  * In this situation bring it down to something reasonable
1133                  * so the elements being destroyed can be retired.
1134                  */
1135                 atomic_set_int(&child->flags, HAMMER2_CHAIN_DESTROYED);
1136                 spin_lock(&child->core->cst.spin);
1137                 if (child->core->update_hi < trans->sync_tid)
1138                         child->core->update_hi = trans->sync_tid;
1139                 spin_unlock(&child->core->cst.spin);
1140         }
1141
1142         if ((child->flags & HAMMER2_CHAIN_MODIFIED) == 0 &&
1143             child->core->update_lo >= info->sync_tid) {
1144                 child->debug_reason = (child->debug_reason & ~255) | 2;
1145                 goto skip;
1146         }
1147
1148         /*
1149          * Re-check the flags before continuing.
1150          */
1151         if (child->modify_tid > trans->sync_tid) {
1152                 child->debug_reason = (child->debug_reason & ~255) | 3;
1153                 hammer2_chain_unlock(child);
1154                 hammer2_chain_drop(child);
1155                 hammer2_chain_lock(parent, HAMMER2_RESOLVE_MAYBE);
1156                 spin_lock(&parent->core->cst.spin);
1157                 return (0);
1158         }
1159
1160         if ((child->flags & HAMMER2_CHAIN_MODIFIED) == 0 &&
1161             child->core->update_lo >= info->sync_tid) {
1162                 child->debug_reason = (child->debug_reason & ~255) | 4;
1163                 goto skip;
1164         }
1165
1166         /*
1167          * Recurse and collect deferral data.
1168          */
1169         ++info->depth;
1170         hammer2_chain_flush_core(info, &child);
1171         --info->depth;
1172
1173 skip:
1174         /*
1175          * Consider us flushed if there was no deferral.  This will have
1176          * already been handled by hammer2_chain_flush_core() but we also
1177          * have to deal with anyone who goto'd skip.
1178          */
1179         if (diddeferral == info->diddeferral) {
1180                 if (child->bref.mirror_tid < info->sync_tid)
1181                         child->bref.mirror_tid = info->sync_tid;
1182         }
1183
1184         /*
1185          * Check the conditions that could cause SCAN2 to modify the parent.
1186          * Modify the parent here instead of in SCAN2, which would cause
1187          * rollup chicken-and-egg races.
1188          *
1189          * Scan2 is expected to update bref.mirror_tid in the domodify case,
1190          * but will skip the child otherwise giving us the responsibility to
1191          * update bref.mirror_tid.
1192          */
1193         if (child->delete_tid <= trans->sync_tid &&
1194             child->delete_tid > parent->bref.mirror_tid &&
1195             child->modify_tid <= parent->bref.mirror_tid) {
1196                 info->domodify = 1;             /* base deletion */
1197         } else if (child->delete_tid > trans->sync_tid &&
1198                    child->modify_tid > parent->bref.mirror_tid) {
1199                 info->domodify = 1;             /* base insertion */
1200         }
1201
1202         /*
1203          * Relock to continue the loop
1204          */
1205         hammer2_chain_unlock(child);
1206         hammer2_chain_lock(parent, HAMMER2_RESOLVE_MAYBE);
1207         hammer2_chain_drop(child);
1208         KKASSERT(info->parent == parent);
1209
1210         spin_lock(&parent->core->cst.spin);
1211         return (0);
1212 }
1213
1214 /*
1215  * Flush helper scan2 (non-recursive)
1216  *
1217  * This pass on a chain's children propagates any MOVED or DELETED
1218  * elements back up the chain towards the root after those elements have
1219  * been fully flushed.  Unlike scan1, this function is NOT recursive and
1220  * the parent remains locked across the entire scan.
1221  *
1222  * SCAN2 is called twice, once with pass set to 1 and once with it set to 2.
1223  * We have to do this so base[] elements can be deleted in pass 1 to make
1224  * room for adding new elements in pass 2.
1225  *
1226  * This function also rolls up storage statistics.
1227  *
1228  * NOTE!  A deletion is a visbility issue, there can still be references to
1229  *        deleted elements (for example, to an unlinked file which is still
1230  *        open), and there can also be multiple chains pointing to the same
1231  *        bref where some are deleted and some are not (for example due to
1232  *        a rename).   So a chain marked for deletion is basically considered
1233  *        to be live until it is explicitly destroyed or until its ref-count
1234  *        reaches zero (also implying that MOVED and MODIFIED are clear).
1235  *
1236  * NOTE!  Info->parent will be locked but will only be instantiated/modified
1237  *        if it is either MODIFIED or if scan1 determined that block table
1238  *        updates will occur.
1239  *
1240  * NOTE!  SCAN2 is responsible for updating child->bref.mirror_tid only in
1241  *        the case where it modifies the parent (does a base insertion
1242  *        or deletion).  SCAN1 handled all other cases.
1243  */
1244 static int
1245 hammer2_chain_flush_scan2(hammer2_chain_t *child, void *data)
1246 {
1247         hammer2_flush_info_t *info = data;
1248         hammer2_chain_t *parent = info->parent;
1249         hammer2_chain_core_t *above = child->above;
1250         hammer2_mount_t *hmp = child->hmp;
1251         hammer2_trans_t *trans = info->trans;
1252         hammer2_blockref_t *base;
1253         int count;
1254         int ok;
1255
1256 #if FLUSH_DEBUG
1257         kprintf("SCAN2 %p.%d %08x mod=%016jx del=%016jx trans=%016jx\n", child, child->bref.type, child->flags, child->modify_tid, child->delete_tid, info->trans->sync_tid);
1258 #endif
1259         /*
1260          * Inodes with stale children that have been converted to DIRECTDATA
1261          * mode (file extension or hardlink conversion typically) need to
1262          * skipped right now before we start messing with a non-existant
1263          * block table.
1264          */
1265 #if 0
1266         if (parent->bref.type == HAMMER2_BREF_TYPE_INODE &&
1267             (parent->data->ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA)) {
1268                 goto finalize;
1269         }
1270 #endif
1271
1272         /*
1273          * Ignore children created after our flush point, treating them as
1274          * if they did not exist).  These children will not cause the parent
1275          * to be updated.
1276          *
1277          * Children deleted after our flush point are treated as having been
1278          * created for the purposes of the flush.  The parent's update_hi
1279          * will already be higher than our trans->sync_tid so the path for
1280          * the next flush is left intact.
1281          *
1282          * When we encounter such children and the parent chain has not been
1283          * deleted, delete/duplicated, or delete/duplicated-for-move, then
1284          * the parent may be used to funnel through several flush points.
1285          * These chains will still be visible to later flushes due to having
1286          * a higher update_hi than we can set in the current flush.
1287          */
1288         if (child->modify_tid > trans->sync_tid) {
1289                 KKASSERT(child->delete_tid >= child->modify_tid);
1290                 goto finalize;
1291         }
1292
1293 #if 0
1294         /*
1295          * Ignore children which have not changed.  The parent's block table
1296          * is already correct.
1297          *
1298          * XXX The MOVED bit is only cleared when all multi-homed parents
1299          *     have flushed, creating a situation where a re-flush can occur
1300          *     via a parent which has already flushed.  The hammer2_base_*()
1301          *     functions currently have a hack to deal with this case but
1302          *     we need something better.
1303          */
1304         if ((child->flags & HAMMER2_CHAIN_MOVED) == 0) {
1305                 KKASSERT((child->flags & HAMMER2_CHAIN_MODIFIED) == 0);
1306                 goto finalize;
1307         }
1308 #endif
1309
1310         /*
1311          * Make sure child is referenced before we unlock.
1312          */
1313         hammer2_chain_ref(child);
1314         spin_unlock(&above->cst.spin);
1315
1316         /*
1317          * Parent reflushed after the child has passed them by should skip
1318          * due to the modify_tid test. XXX
1319          */
1320         hammer2_chain_lock(child, HAMMER2_RESOLVE_NEVER);
1321         KKASSERT(child->above == above);
1322         KKASSERT(parent->core == above);
1323
1324         /*
1325          * The parent's blockref to the child must be deleted or updated.
1326          *
1327          * This point is not reached on successful DESTROYED optimizations
1328          * but can be reached on recursive deletions and restricted flushes.
1329          *
1330          * The chain_modify here may delete-duplicate the block.  This can
1331          * cause a multitude of issues if the block was already modified
1332          * by a later (post-flush) transaction.  Primarily blockrefs in
1333          * the later block can be out-of-date, so if the situation occurs
1334          * we can't throw away the MOVED bit on the current blocks until
1335          * the later blocks are flushed (so as to be able to regenerate all
1336          * the changes that were made).
1337          *
1338          * Because flushes are ordered we do not have to make a
1339          * modify/duplicate of indirect blocks.  That is, the flush
1340          * code does not have to kmalloc or duplicate anything.  We
1341          * can adjust the indirect block table in-place and reuse the
1342          * chain.  It IS possible that the chain has already been duplicated
1343          * or may wind up being duplicated on-the-fly by modifying code
1344          * on the frontend.  We simply use the original and ignore such
1345          * chains.  However, it does mean we can't clear the MOVED bit.
1346          *
1347          * XXX recursive deletions not optimized.
1348          */
1349
1350         switch(parent->bref.type) {
1351         case HAMMER2_BREF_TYPE_INODE:
1352                 /*
1353                  * XXX Should assert that OPFLAG_DIRECTDATA is 0 once we
1354                  * properly duplicate the inode headers and do proper flush
1355                  * range checks (all the children should be beyond the flush
1356                  * point).  For now just don't sync the non-applicable
1357                  * children.
1358                  *
1359                  * XXX Can also occur due to hardlink consolidation.  We
1360                  * set OPFLAG_DIRECTDATA to prevent the indirect and data
1361                  * blocks from syncing ot the hardlink pointer.
1362                  */
1363                 if (parent->data)
1364                         base = &parent->data->ipdata.u.blockset.blockref[0];
1365                 else
1366                         base = NULL;
1367                 count = HAMMER2_SET_COUNT;
1368                 break;
1369         case HAMMER2_BREF_TYPE_INDIRECT:
1370         case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1371                 if (parent->data)
1372                         base = &parent->data->npdata[0];
1373                 else
1374                         base = NULL;
1375                 count = parent->bytes / sizeof(hammer2_blockref_t);
1376                 break;
1377         case HAMMER2_BREF_TYPE_VOLUME:
1378                 base = &hmp->voldata.sroot_blockset.blockref[0];
1379                 count = HAMMER2_SET_COUNT;
1380                 break;
1381         case HAMMER2_BREF_TYPE_FREEMAP:
1382                 base = &parent->data->npdata[0];
1383                 count = HAMMER2_SET_COUNT;
1384                 break;
1385         default:
1386                 base = NULL;
1387                 count = 0;
1388                 panic("hammer2_chain_flush_scan2: "
1389                       "unrecognized blockref type: %d",
1390                       parent->bref.type);
1391         }
1392
1393         /*
1394          * Don't bother updating a deleted parent's blockrefs (caller will
1395          * optimize-out the disk write).  Note that this is not optional,
1396          * a deleted parent's blockref array might not be synchronized at
1397          * all so calling hammer2_base*() functions could result in a panic.
1398          *
1399          * Otherwise, we need to be COUNTEDBREFS synchronized for the
1400          * hammer2_base_*() functions.
1401          */
1402 #if FLUSH_DEBUG
1403         kprintf("SCAN2 base=%p pass=%d PARENT %p.%d DTID=%016jx SYNC=%016jx\n",
1404                 base,
1405                 info->pass, parent, parent->bref.type, parent->delete_tid, trans->sync_tid);
1406 #endif
1407         if (parent->delete_tid <= trans->sync_tid)
1408                 base = NULL;
1409         else if ((parent->core->flags & HAMMER2_CORE_COUNTEDBREFS) == 0)
1410                 hammer2_chain_countbrefs(parent, base, count);
1411
1412         /*
1413          * Update the parent's blockref table and propagate mirror_tid.
1414          *
1415          * NOTE! Children with modify_tid's beyond our flush point are
1416          *       considered to not exist for the purposes of updating the
1417          *       parent's blockref array.
1418          *
1419          * NOTE! SCAN1 has already put the parent in a modified state
1420          *       so if it isn't we panic.
1421          *
1422          * NOTE! chain->modify_tid vs chain->bref.modify_tid.  The chain's
1423          *       internal modify_tid is always updated based on creation
1424          *       or delete-duplicate.  However, the bref.modify_tid is NOT
1425          *       updated due to simple blockref updates.
1426          */
1427 #if FLUSH_DEBUG
1428         kprintf("chain %p->%p pass %d trans %016jx sync %p.%d %016jx/%d C=%016jx D=%016jx PMIRROR %016jx\n",
1429                 parent, child,
1430                 info->pass, trans->sync_tid,
1431                 child, child->bref.type,
1432                 child->bref.key, child->bref.keybits,
1433                 child->modify_tid, child->delete_tid, parent->bref.mirror_tid);
1434 #endif
1435
1436         if (info->pass == 1 && child->delete_tid <= trans->sync_tid) {
1437                 /*
1438                  * Deleting.  The block array is expected to contain the
1439                  * child's entry if:
1440                  *
1441                  * (1) The deletion occurred after the parent's block table
1442                  *     was last synchronized (delete_tid), and
1443                  *
1444                  * (2) The creation occurred before or during the parent's
1445                  *     last block table synchronization.
1446                  */
1447 #if FLUSH_DEBUG
1448                 kprintf("S2A %p b=%p d/b=%016jx/%016jx m/b=%016jx/%016jx\n",
1449                         child, base, child->delete_tid, parent->bref.mirror_tid,
1450                         child->modify_tid, parent->bref.mirror_tid);
1451 #endif
1452                 ok = 1;
1453                 if (base &&
1454                     child->delete_tid > parent->bref.mirror_tid &&
1455                     child->modify_tid <= parent->bref.mirror_tid) {
1456                         KKASSERT(child->flags & HAMMER2_CHAIN_MOVED);
1457                         KKASSERT(parent->modify_tid == trans->sync_tid);
1458                         hammer2_rollup_stats(parent, child, -1);
1459                         spin_lock(&above->cst.spin);
1460 #if FLUSH_DEBUG
1461                         kprintf("trans %jx parent %p.%d child %p.%d m/d %016jx/%016jx "
1462                                 "flg=%08x %016jx/%d delete\n",
1463                                 trans->sync_tid,
1464                                 parent, parent->bref.type,
1465                                 child, child->bref.type,
1466                                 child->modify_tid, child->delete_tid,
1467                                 child->flags,
1468                                 child->bref.key, child->bref.keybits);
1469 #endif
1470                         hammer2_base_delete(parent, base, count,
1471                                             &info->cache_index, child);
1472                         spin_unlock(&above->cst.spin);
1473                 }
1474         } else if (info->pass == 2 && child->delete_tid > trans->sync_tid) {
1475                 /*
1476                  * Inserting.  The block array is expected to NOT contain
1477                  * the child's entry if:
1478                  *
1479                  * (1) The creation occurred after the parent's block table
1480                  *     was last synchronized (modify_tid), and
1481                  *
1482                  * (2) The child is not being deleted in the same
1483                  *     transaction.
1484                  */
1485                 ok = 1;
1486                 if (base &&
1487                     child->modify_tid > parent->bref.mirror_tid) {
1488                         KKASSERT(child->flags & HAMMER2_CHAIN_MOVED);
1489                         KKASSERT(parent->modify_tid == trans->sync_tid);
1490                         hammer2_rollup_stats(parent, child, 1);
1491                         spin_lock(&above->cst.spin);
1492 #if FLUSH_DEBUG
1493                         kprintf("trans %jx parent %p.%d child %p.%d m/d %016jx/%016jx "
1494                                 "flg=%08x %016jx/%d insert\n",
1495                                 trans->sync_tid,
1496                                 parent, parent->bref.type,
1497                                 child, child->bref.type,
1498                                 child->modify_tid, child->delete_tid,
1499                                 child->flags,
1500                                 child->bref.key, child->bref.keybits);
1501 #endif
1502                         hammer2_base_insert(parent, base, count,
1503                                             &info->cache_index, child);
1504                         spin_unlock(&above->cst.spin);
1505                 }
1506         } else if (info->pass == 3 && (child->flags & HAMMER2_CHAIN_MOVED)) {
1507                 /*
1508                  * Only clear MOVED once all possible parents have been
1509                  * flushed.  When can we safely clear the MOVED flag?
1510                  * Flushes down duplicate paths can occur out of order,
1511                  * for example if an inode is moved as part of a hardlink
1512                  * consolidation or if an inode is moved into an indirect
1513                  * block indexed before the inode.
1514                  */
1515                 hammer2_chain_t *scan;
1516
1517                 if (hammer2_debug & 0x4000)
1518                         kprintf("CHECKMOVED %p (parent=%p)", child, parent);
1519
1520                 ok = 1;
1521                 spin_lock(&above->cst.spin);
1522                 TAILQ_FOREACH(scan, &above->ownerq, core_entry) {
1523                         /*
1524                          * Can't clear child's MOVED until all parent's have
1525                          * synchronized with it.
1526                          *
1527                          * ignore any parents which have been deleted as-of
1528                          * our transaction id (their block array doesn't get
1529                          * updated).
1530                          */
1531                         if (scan->delete_tid <= trans->sync_tid)
1532                                 continue;
1533
1534                         /*
1535                          * parent not synchronized if child modified or
1536                          * deleted after the parent's last sync point.
1537                          *
1538                          * (For the purpose of clearing the MOVED bit
1539                          *  we do not restrict the tests to just flush
1540                          *  transactions).
1541                          */
1542                         if (scan->bref.mirror_tid < child->modify_tid ||
1543                             ((child->flags & HAMMER2_CHAIN_DELETED) &&
1544                              scan->bref.mirror_tid < child->delete_tid)) {
1545                                 if (hammer2_debug & 0x4000)
1546                                         kprintf("(fail scan %p %016jx/%016jx)",
1547                                                 scan, scan->bref.mirror_tid,
1548                                                 child->modify_tid);
1549                                 ok = 0;
1550                         }
1551                 }
1552                 if (hammer2_debug & 0x4000)
1553                         kprintf("\n");
1554                 spin_unlock(&above->cst.spin);
1555
1556                 /*
1557                  * Can we finally clear MOVED?
1558                  */
1559                 if (ok) {
1560                         if (hammer2_debug & 0x4000)
1561                                 kprintf("clear moved %p.%d %016jx/%d\n",
1562                                         child, child->bref.type,
1563                                         child->bref.key, child->bref.keybits);
1564                         if (child->modify_tid <= trans->sync_tid &&
1565                             (child->delete_tid == HAMMER2_MAX_TID ||
1566                              child->delete_tid <= trans->sync_tid)) {
1567                                 atomic_clear_int(&child->flags,
1568                                                  HAMMER2_CHAIN_MOVED);
1569                                 hammer2_chain_drop(child);      /* flag */
1570                                 KKASSERT((child->flags &
1571                                                 HAMMER2_CHAIN_MODIFIED) == 0);
1572                         } else {
1573                                 kprintf("ok problem child %p %016jx/%016jx vs %016jx\n", child, child->modify_tid, child->delete_tid, trans->sync_tid);
1574                         }
1575                 } else {
1576                         if (hammer2_debug & 0x4000)
1577                                 kprintf("keep  moved %p.%d %016jx/%d\n",
1578                                         child, child->bref.type,
1579                                         child->bref.key, child->bref.keybits);
1580                 }
1581         }
1582
1583         /*
1584          * Unlock the child.  This can wind up dropping the child's
1585          * last ref, removing it from the parent's RB tree, and deallocating
1586          * the structure.  The RB_SCAN() our caller is doing handles the
1587          * situation.
1588          */
1589         hammer2_chain_unlock(child);
1590         hammer2_chain_drop(child);
1591         spin_lock(&above->cst.spin);
1592
1593         /*
1594          * The parent may have been delete-duplicated.
1595          */
1596         info->parent = parent;
1597 finalize:
1598         return (0);
1599 }
1600
1601 static
1602 void
1603 hammer2_rollup_stats(hammer2_chain_t *parent, hammer2_chain_t *child, int how)
1604 {
1605 #if 0
1606         hammer2_chain_t *grandp;
1607 #endif
1608
1609         parent->data_count += child->data_count;
1610         parent->inode_count += child->inode_count;
1611         child->data_count = 0;
1612         child->inode_count = 0;
1613         if (how < 0) {
1614                 parent->data_count -= child->bytes;
1615                 if (child->bref.type == HAMMER2_BREF_TYPE_INODE) {
1616                         parent->inode_count -= 1;
1617 #if 0
1618                         /* XXX child->data may be NULL atm */
1619                         parent->data_count -= child->data->ipdata.data_count;
1620                         parent->inode_count -= child->data->ipdata.inode_count;
1621 #endif
1622                 }
1623         } else if (how > 0) {
1624                 parent->data_count += child->bytes;
1625                 if (child->bref.type == HAMMER2_BREF_TYPE_INODE) {
1626                         parent->inode_count += 1;
1627 #if 0
1628                         /* XXX child->data may be NULL atm */
1629                         parent->data_count += child->data->ipdata.data_count;
1630                         parent->inode_count += child->data->ipdata.inode_count;
1631 #endif
1632                 }
1633         }
1634         if (parent->bref.type == HAMMER2_BREF_TYPE_INODE) {
1635                 parent->data->ipdata.data_count += parent->data_count;
1636                 parent->data->ipdata.inode_count += parent->inode_count;
1637 #if 0
1638                 for (grandp = parent->above->first_parent;
1639                      grandp;
1640                      grandp = grandp->next_parent) {
1641                         grandp->data_count += parent->data_count;
1642                         grandp->inode_count += parent->inode_count;
1643                 }
1644 #endif
1645                 parent->data_count = 0;
1646                 parent->inode_count = 0;
1647         }
1648 }