2 * Copyright (c) 2011-2013 The DragonFly Project. All rights reserved.
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>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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.
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
36 #include <sys/cdefs.h>
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/types.h>
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
54 struct hammer2_flush_info {
55 hammer2_chain_t *parent;
56 hammer2_trans_t *trans;
62 struct h2_flush_deferral_list flush_list;
63 hammer2_tid_t sync_tid; /* flush synchronization point */
66 typedef struct hammer2_flush_info hammer2_flush_info_t;
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);
78 hammer2_updatestats(hammer2_flush_info_t *info, hammer2_blockref_t *bref,
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;
88 info->data_count -= bytes;
90 info->data_count += bytes;
96 * Transaction support functions for writing to the filesystem.
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.
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.
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.
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).
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.
119 hammer2_trans_init(hammer2_trans_t *trans, hammer2_pfsmount_t *pmp, int flags)
121 hammer2_mount_t *hmp;
122 hammer2_trans_t *head;
124 bzero(trans, sizeof(*trans));
126 hmp = pmp->cluster.chains[0]->hmp; /* XXX */
128 hammer2_voldata_lock(hmp);
129 trans->flags = flags;
130 trans->td = curthread;
131 /*trans->delete_gen = 0;*/ /* multiple deletions within trans */
133 if (flags & HAMMER2_TRANS_ISFLUSH) {
135 * If multiple flushes are trying to run we have to
136 * wait until it is our turn. All flushes are serialized.
138 * We queue ourselves and then wait to become the head
139 * of the queue, allowing all prior flushes to complete.
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) {
147 while (trans->blocked) {
148 lksleep(&trans->sync_tid, &hmp->voldatalk,
152 } else if (hmp->flushcnt == 0) {
154 * No flushes are pending, we can go.
156 TAILQ_INSERT_TAIL(&hmp->transq, trans, entry);
157 trans->sync_tid = hmp->voldata.alloc_tid;
158 trans->real_tid = trans->sync_tid;
160 /* XXX improve/optimize inode allocation */
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.
167 * TRANS_BUFCACHE transactions cannot block.
169 TAILQ_FOREACH(head, &hmp->transq, entry) {
170 if (head->flags & HAMMER2_TRANS_ISFLUSH)
174 TAILQ_INSERT_AFTER(&hmp->transq, head, trans, entry);
175 trans->sync_tid = head->real_tid + 1;
176 trans->real_tid = trans->sync_tid;
178 if ((trans->flags & HAMMER2_TRANS_BUFCACHE) == 0) {
179 if (TAILQ_FIRST(&hmp->transq) != head) {
181 while (trans->blocked) {
182 lksleep(&trans->sync_tid,
189 if (flags & HAMMER2_TRANS_NEWINODE)
190 trans->inode_tid = hmp->voldata.inode_tid++;
191 hammer2_voldata_unlock(hmp, 0);
195 hammer2_trans_done(hammer2_trans_t *trans)
197 hammer2_mount_t *hmp;
198 hammer2_trans_t *head;
199 hammer2_trans_t *scan;
201 hmp = trans->pmp->cluster.chains[0]->hmp;
204 * Remove and adjust flushcnt
206 hammer2_voldata_lock(hmp);
207 TAILQ_REMOVE(&hmp->transq, trans, entry);
208 if (trans->flags & HAMMER2_TRANS_ISFLUSH)
212 * Unblock the head of the queue and any additional transactions
213 * up to the next flush.
215 head = TAILQ_FIRST(&hmp->transq);
216 if (head && head->blocked) {
218 wakeup(&head->sync_tid);
220 scan = TAILQ_NEXT(head, entry);
221 while (scan && (scan->flags & HAMMER2_TRANS_ISFLUSH) == 0) {
224 wakeup(&scan->sync_tid);
226 scan = TAILQ_NEXT(scan, entry);
229 hammer2_voldata_unlock(hmp, 0);
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).
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.
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.
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
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.
260 hammer2_chain_flush(hammer2_trans_t *trans, hammer2_chain_t **chainp)
262 hammer2_chain_t *chain = *chainp;
263 hammer2_chain_t *scan;
264 hammer2_chain_core_t *core;
265 hammer2_flush_info_t info;
269 * Execute the recursive flush and handle deferrals.
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.
276 bzero(&info, sizeof(info));
277 TAILQ_INIT(&info.flush_list);
279 info.sync_tid = trans->sync_tid;
280 info.cache_index = -1;
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);
288 * Extra ref needed because flush_core expects it when replacing
291 hammer2_chain_ref(chain);
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.
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);
306 * Now that we've popped back up we can do a secondary
307 * recursion on the deferred elements.
309 * NOTE: hammer2_chain_flush() may replace scan.
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);
322 info.diddeferral = 0;
323 hammer2_chain_flush_core(&info, &chain);
325 kprintf("flush_core_done parent=<base> chain=%p.%d %08x\n",
326 chain, chain->bref.type, chain->flags);
330 * Only loop if deep recursions have been deferred.
332 if (TAILQ_EMPTY(&info.flush_list))
335 if (++loops % 1000 == 0) {
336 kprintf("hammer2_chain_flush: excessive loops on %p\n",
338 if (hammer2_debug & 0x100000)
342 hammer2_chain_drop(chain);
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.
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.
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.
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).
365 * We are also responsible for updating chain->core->update_lo to
366 * prevent repeated recursions due to deferrals.
369 hammer2_chain_flush_core(hammer2_flush_info_t *info, hammer2_chain_t **chainp)
371 hammer2_chain_t *chain = *chainp;
372 hammer2_mount_t *hmp;
373 hammer2_blockref_t *bref;
377 hammer2_trans_t *trans = info->trans;
379 hammer2_chain_core_t *core;
390 diddeferral = info->diddeferral;
394 kprintf("flush_core %p->%p.%d %08x (%s)\n",
395 info->parent, chain, chain->bref.type,
397 ((chain->bref.type == HAMMER2_BREF_TYPE_INODE) ?
398 (char *)chain->data->ipdata.filename : "?"));
400 kprintf("flush_core NULL->%p.%d %08x (%s)\n",
401 chain, chain->bref.type,
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);
409 * Check if we even have any work to do.
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.
415 * This bit of code is capable of short-cutting entire sub-trees
416 * if they have not been touched.
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)) {
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.
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).
436 * Do not update core->update_lo here, there might be other paths
437 * to the core and we haven't actually flushed it.
439 * (vchain and fchain are exceptions since they cannot be duplicated)
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 */
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.
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).
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).
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) {
470 if (chain->bytes == chain->bp->b_bufsize)
471 chain->bp->b_flags |= B_INVAL|B_RELBUF;
473 if ((chain->flags & HAMMER2_CHAIN_MOVED) == 0) {
474 hammer2_chain_ref(chain);
475 atomic_set_int(&chain->flags,
476 HAMMER2_CHAIN_MOVED);
478 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
479 hammer2_chain_drop(chain);
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 */
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.
493 * update_hi was already checked and prevents initial recursions on
494 * subtrees which have not been modified.
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
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.
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;
514 * Races will bump update_hi above trans->sync_tid causing
515 * us to catch the issue in a later flush.
517 * We don't want to set our chain to MODIFIED gratuitously.
519 * We need an extra ref on chain because we are going to
520 * release its lock temporarily in our child loop.
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.
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.
534 * Scan1 is recursive.
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.
540 * NOTE: RB_SCAN() must be used instead of RB_FOREACH()
541 * because children can be physically removed during
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.
548 saved_parent = info->parent;
549 saved_domodify = info->domodify;
550 info->parent = chain;
552 chain->debug_reason = (chain->debug_reason & ~255) | 6;
554 if (chain->flags & HAMMER2_CHAIN_DEFERRED) {
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,
561 atomic_set_int(&chain->flags,
562 HAMMER2_CHAIN_DEFERRED);
566 spin_lock(&core->cst.spin);
567 KKASSERT(core->good == 0x1234 && core->sharecnt > 0);
569 save_gen = core->generation;
570 TAILQ_FOREACH_REVERSE(layer, &core->layerq,
571 h2_layer_list, entry) {
573 KKASSERT(layer->good == 0xABCD);
574 RB_SCAN(hammer2_chain_tree,
576 NULL, hammer2_chain_flush_scan1,
580 } while (core->generation != save_gen);
581 spin_unlock(&core->cst.spin);
584 if (info->parent != chain) {
586 hammer2_chain_drop(chain);
587 hammer2_chain_ref(info->parent);
589 chain = info->parent;
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
596 if (chain->delete_tid <= info->sync_tid &&
597 (chain->flags & HAMMER2_CHAIN_DUPLICATED)) {
601 if (chain->bref.mirror_tid >= info->sync_tid ||
602 chain->bref.mirror_tid >= core->update_hi) {
608 * Blockrefs are only updated on live chains.
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.
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.
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);
630 chain = info->parent;
633 KKASSERT(chain->modify_tid == info->sync_tid);
634 chain->debug_reason = (chain->debug_reason & ~255) | 7;
636 KKASSERT(chain == info->parent);
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.
643 * Scan2 may replace info->parent. If it does it will also
644 * replace the extra ref we made.
646 * Scan2 is non-recursive.
648 if (diddeferral != info->diddeferral) {
649 spin_lock(&core->cst.spin);
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) {
658 KKASSERT(layer->good == 0xABCD);
659 RB_SCAN(hammer2_chain_tree, &layer->rbtree,
660 NULL, hammer2_chain_flush_scan2, info);
662 RB_SCAN(hammer2_chain_tree, &layer->rbtree,
663 NULL, hammer2_chain_flush_scan2, info);
668 * chain is now considered up-to-date, adjust
669 * bref.mirror_tid and update_lo before running
672 * (no deferral in this path)
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;
679 TAILQ_FOREACH_REVERSE(layer, &core->layerq,
680 h2_layer_list, entry) {
683 KKASSERT(layer->good == 0xABCD);
684 RB_SCAN(hammer2_chain_tree, &layer->rbtree,
685 NULL, hammer2_chain_flush_scan2, info);
687 KKASSERT(info->parent->core == core);
692 * info->parent must not have been replaced again
694 KKASSERT(info->parent == chain);
696 chain->debug_reason = (chain->debug_reason & ~255) | 8;
699 hammer2_chain_layer_check_locked(chain->hmp, core);
700 spin_unlock(&core->cst.spin);
702 info->parent = saved_parent;
703 info->domodify = saved_domodify;
704 KKASSERT(chain->refs > 1);
707 * chain is now considered up-to-date, adjust
708 * bref.mirror_tid and update_lo.
710 * (no deferral in this path)
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;
720 kprintf("POP %p.%d defer=%d\n", chain, chain->bref.type, diddeferral);
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.
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);
735 /* do not update core->update_lo */
740 * non-deferral path - mirror_tid and update_lo have been updated
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
748 * NOTE: scan2 has already executed above so statistics have
749 * already been rolled up.
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
756 if (chain->delete_tid <= info->sync_tid) {
757 chain->debug_reason = (chain->debug_reason & ~255) | 9;
758 if (chain->flags & HAMMER2_CHAIN_MODIFIED) {
760 if (chain->bytes == chain->bp->b_bufsize)
761 chain->bp->b_flags |= B_INVAL|B_RELBUF;
763 if ((chain->flags & HAMMER2_CHAIN_MOVED) == 0) {
764 hammer2_chain_ref(chain);
765 atomic_set_int(&chain->flags,
766 HAMMER2_CHAIN_MOVED);
768 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
769 hammer2_chain_drop(chain);
774 if ((chain->flags & HAMMER2_CHAIN_DESTROYED) &&
775 (chain->flags & HAMMER2_CHAIN_DELETED) &&
776 (trans->flags & HAMMER2_TRANS_RESTRICTED) == 0) {
778 * Throw-away the MODIFIED flag
780 if (chain->flags & HAMMER2_CHAIN_MODIFIED) {
782 if (chain->bytes == chain->bp->b_bufsize)
783 chain->bp->b_flags |= B_INVAL|B_RELBUF;
785 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
786 hammer2_chain_drop(chain);
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.
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.
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.
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);
814 chain->debug_reason = (chain->debug_reason & ~255) | 10;
817 chain->debug_reason = (chain->debug_reason & ~255) | 11;
822 * A DESTROYED node that reaches this point must be flushed for
823 * synchronization point consistency.
827 * Update mirror_tid, clear MODIFIED, and set MOVED.
829 * The caller will update the parent's reference to this chain
830 * by testing MOVED as long as the modification was in-bounds.
832 * MOVED is never set on the volume root as there is no parent
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,
841 if (hammer2_debug & 0x2000) {
842 Debugger("Flush hell");
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");
851 if ((chain->flags & HAMMER2_CHAIN_MOVED) ||
852 chain == &hmp->vchain ||
853 chain == &hmp->fchain) {
855 * Drop the ref from the MODIFIED bit we cleared.
856 * Net is -0 or -1 ref depending.
859 hammer2_chain_drop(chain);
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.
866 if (wasmodified == 0)
867 hammer2_chain_ref(chain);
868 atomic_set_int(&chain->flags, HAMMER2_CHAIN_MOVED);
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
876 * Volume headers are NOT flushed here as they require special
879 switch(chain->bref.type) {
880 case HAMMER2_BREF_TYPE_FREEMAP:
881 hammer2_modify_volume(hmp);
883 case HAMMER2_BREF_TYPE_VOLUME:
885 * We should flush the free block table before we calculate
886 * CRCs and copy voldata -> volsync.
888 * To prevent SMP races, fchain must remain locked until
889 * voldata is copied to volsync.
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);
901 * The volume header is flushed manually by the syncer, not
902 * here. All we do is adjust the crc's.
904 KKASSERT(chain->data != NULL);
905 KKASSERT(chain->bp == NULL);
907 hmp->voldata.icrc_sects[HAMMER2_VOL_ICRC_SECT1]=
909 (char *)&hmp->voldata +
910 HAMMER2_VOLUME_ICRC1_OFF,
911 HAMMER2_VOLUME_ICRC1_SIZE);
912 hmp->voldata.icrc_sects[HAMMER2_VOL_ICRC_SECT0]=
914 (char *)&hmp->voldata +
915 HAMMER2_VOLUME_ICRC0_OFF,
916 HAMMER2_VOLUME_ICRC0_SIZE);
917 hmp->voldata.icrc_volheader =
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);
926 case HAMMER2_BREF_TYPE_DATA:
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.
932 * Make sure any device buffer(s) have been flushed out here.
933 * (there aren't usually any to flush).
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);
940 bp = getblk(hmp->devvp, pbase, psize, GETBLK_NOWAIT, 0);
942 if ((bp->b_flags & (B_CACHE | B_DIRTY)) ==
943 (B_CACHE | B_DIRTY)) {
946 bp->b_flags |= B_RELBUF;
952 case HAMMER2_BREF_TYPE_INDIRECT:
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).
959 * Only write the buffer out if it is dirty, it is possible
960 * the operating system had already written out the buffer.
962 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS);
963 KKASSERT(chain->bp != NULL);
966 if ((chain->flags & HAMMER2_CHAIN_DIRTYBP) ||
967 (bp->b_flags & B_DIRTY)) {
974 hammer2_chain_unlock(chain);
977 case HAMMER2_BREF_TYPE_INDIRECT:
978 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
980 * Device-backed. Buffer will be flushed by the sync
983 KKASSERT((chain->flags & HAMMER2_CHAIN_EMBEDDED) == 0);
985 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
988 * Embedded elements have to be flushed out.
989 * (Basically just BREF_TYPE_INODE).
991 KKASSERT(chain->flags & HAMMER2_CHAIN_EMBEDDED);
992 KKASSERT(chain->data != NULL);
993 KKASSERT(chain->bp == NULL);
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);
1003 * The data is embedded, we have to acquire the
1004 * buffer cache buffer and copy the data into it.
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);
1012 * The getblk() optimization can only be used if the
1013 * physical block size matches the request.
1015 error = bread(hmp->devvp, pbase, psize, &bp);
1016 KKASSERT(error == 0);
1018 bdata = (char *)bp->b_data + boff;
1021 * Copy the data to the buffer, mark the buffer
1022 * dirty, and convert the chain to unmodified.
1024 bcopy(chain->data, bdata, chain->bytes);
1025 bp->b_flags |= B_CLUSTEROK;
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);
1034 case HAMMER2_CHECK_ISCSI32:
1035 chain->bref.check.iscsi32.value =
1036 hammer2_icrc32(chain->data, chain->bytes);
1039 panic("hammer2_flush_core: bad crc type");
1040 break; /* NOT REACHED */
1042 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE)
1043 ++hammer2_iod_meta_write;
1045 ++hammer2_iod_indr_write;
1050 * Flush helper scan1 (recursive)
1052 * Flushes the children of the caller's chain (parent) and updates
1053 * the blockref, restricted by sync_tid.
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.
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.
1063 * WARNING! If we do not call chain_flush_core we must update bref.mirror_tid
1067 hammer2_chain_flush_scan1(hammer2_chain_t *child, void *data)
1069 hammer2_flush_info_t *info = data;
1070 hammer2_trans_t *trans = info->trans;
1071 hammer2_chain_t *parent = info->parent;
1074 if (hammer2_debug & 0x80000)
1076 diddeferral = info->diddeferral;
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
1084 * Or MODIFIED is not set and child is already fully synchronized
1085 * with its sub-tree. Don't persue.
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 */
1096 * We must ref the child before unlocking the spinlock.
1098 * The caller has added a ref to the parent so we can temporarily
1099 * unlock it in order to lock the child.
1101 hammer2_chain_ref(child);
1102 spin_unlock(&parent->core->cst.spin);
1104 hammer2_chain_unlock(parent);
1105 hammer2_chain_lock(child, HAMMER2_RESOLVE_MAYBE);
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.
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.
1119 * We have to be careful not to propagate the DESTROYED flag if
1120 * the destruction occurred after our flush sync_tid.
1122 if ((parent->flags & HAMMER2_CHAIN_DESTROYED) &&
1123 (child->flags & HAMMER2_CHAIN_DELETED) &&
1124 (child->flags & HAMMER2_CHAIN_DESTROYED) == 0) {
1126 * Force downward recursion by bringing update_hi up to
1127 * at least sync_tid. Parent's mirror_tid has not yet
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.
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);
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;
1149 * Re-check the flags before continuing.
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);
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;
1167 * Recurse and collect deferral data.
1170 hammer2_chain_flush_core(info, &child);
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.
1179 if (diddeferral == info->diddeferral) {
1180 if (child->bref.mirror_tid < info->sync_tid)
1181 child->bref.mirror_tid = info->sync_tid;
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.
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.
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 */
1203 * Relock to continue the loop
1205 hammer2_chain_unlock(child);
1206 hammer2_chain_lock(parent, HAMMER2_RESOLVE_MAYBE);
1207 hammer2_chain_drop(child);
1208 KKASSERT(info->parent == parent);
1210 spin_lock(&parent->core->cst.spin);
1215 * Flush helper scan2 (non-recursive)
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.
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.
1226 * This function also rolls up storage statistics.
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).
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.
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.
1245 hammer2_chain_flush_scan2(hammer2_chain_t *child, void *data)
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;
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);
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
1266 if (parent->bref.type == HAMMER2_BREF_TYPE_INODE &&
1267 (parent->data->ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA)) {
1273 * Ignore children created after our flush point, treating them as
1274 * if they did not exist). These children will not cause the parent
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.
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.
1288 if (child->modify_tid > trans->sync_tid) {
1289 KKASSERT(child->delete_tid >= child->modify_tid);
1295 * Ignore children which have not changed. The parent's block table
1296 * is already correct.
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.
1304 if ((child->flags & HAMMER2_CHAIN_MOVED) == 0) {
1305 KKASSERT((child->flags & HAMMER2_CHAIN_MODIFIED) == 0);
1311 * Make sure child is referenced before we unlock.
1313 hammer2_chain_ref(child);
1314 spin_unlock(&above->cst.spin);
1317 * Parent reflushed after the child has passed them by should skip
1318 * due to the modify_tid test. XXX
1320 hammer2_chain_lock(child, HAMMER2_RESOLVE_NEVER);
1321 KKASSERT(child->above == above);
1322 KKASSERT(parent->core == above);
1325 * The parent's blockref to the child must be deleted or updated.
1327 * This point is not reached on successful DESTROYED optimizations
1328 * but can be reached on recursive deletions and restricted flushes.
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).
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.
1347 * XXX recursive deletions not optimized.
1350 switch(parent->bref.type) {
1351 case HAMMER2_BREF_TYPE_INODE:
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
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.
1364 base = &parent->data->ipdata.u.blockset.blockref[0];
1367 count = HAMMER2_SET_COUNT;
1369 case HAMMER2_BREF_TYPE_INDIRECT:
1370 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1372 base = &parent->data->npdata[0];
1375 count = parent->bytes / sizeof(hammer2_blockref_t);
1377 case HAMMER2_BREF_TYPE_VOLUME:
1378 base = &hmp->voldata.sroot_blockset.blockref[0];
1379 count = HAMMER2_SET_COUNT;
1381 case HAMMER2_BREF_TYPE_FREEMAP:
1382 base = &parent->data->npdata[0];
1383 count = HAMMER2_SET_COUNT;
1388 panic("hammer2_chain_flush_scan2: "
1389 "unrecognized blockref type: %d",
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.
1399 * Otherwise, we need to be COUNTEDBREFS synchronized for the
1400 * hammer2_base_*() functions.
1403 kprintf("SCAN2 base=%p pass=%d PARENT %p.%d DTID=%016jx SYNC=%016jx\n",
1405 info->pass, parent, parent->bref.type, parent->delete_tid, trans->sync_tid);
1407 if (parent->delete_tid <= trans->sync_tid)
1409 else if ((parent->core->flags & HAMMER2_CORE_COUNTEDBREFS) == 0)
1410 hammer2_chain_countbrefs(parent, base, count);
1413 * Update the parent's blockref table and propagate mirror_tid.
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.
1419 * NOTE! SCAN1 has already put the parent in a modified state
1420 * so if it isn't we panic.
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.
1428 kprintf("chain %p->%p pass %d trans %016jx sync %p.%d %016jx/%d C=%016jx D=%016jx PMIRROR %016jx\n",
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);
1436 if (info->pass == 1 && child->delete_tid <= trans->sync_tid) {
1438 * Deleting. The block array is expected to contain the
1441 * (1) The deletion occurred after the parent's block table
1442 * was last synchronized (delete_tid), and
1444 * (2) The creation occurred before or during the parent's
1445 * last block table synchronization.
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);
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);
1461 kprintf("trans %jx parent %p.%d child %p.%d m/d %016jx/%016jx "
1462 "flg=%08x %016jx/%d delete\n",
1464 parent, parent->bref.type,
1465 child, child->bref.type,
1466 child->modify_tid, child->delete_tid,
1468 child->bref.key, child->bref.keybits);
1470 hammer2_base_delete(parent, base, count,
1471 &info->cache_index, child);
1472 spin_unlock(&above->cst.spin);
1474 } else if (info->pass == 2 && child->delete_tid > trans->sync_tid) {
1476 * Inserting. The block array is expected to NOT contain
1477 * the child's entry if:
1479 * (1) The creation occurred after the parent's block table
1480 * was last synchronized (modify_tid), and
1482 * (2) The child is not being deleted in the same
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);
1493 kprintf("trans %jx parent %p.%d child %p.%d m/d %016jx/%016jx "
1494 "flg=%08x %016jx/%d insert\n",
1496 parent, parent->bref.type,
1497 child, child->bref.type,
1498 child->modify_tid, child->delete_tid,
1500 child->bref.key, child->bref.keybits);
1502 hammer2_base_insert(parent, base, count,
1503 &info->cache_index, child);
1504 spin_unlock(&above->cst.spin);
1506 } else if (info->pass == 3 && (child->flags & HAMMER2_CHAIN_MOVED)) {
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.
1515 hammer2_chain_t *scan;
1517 if (hammer2_debug & 0x4000)
1518 kprintf("CHECKMOVED %p (parent=%p)", child, parent);
1521 spin_lock(&above->cst.spin);
1522 TAILQ_FOREACH(scan, &above->ownerq, core_entry) {
1524 * Can't clear child's MOVED until all parent's have
1525 * synchronized with it.
1527 * ignore any parents which have been deleted as-of
1528 * our transaction id (their block array doesn't get
1531 if (scan->delete_tid <= trans->sync_tid)
1535 * parent not synchronized if child modified or
1536 * deleted after the parent's last sync point.
1538 * (For the purpose of clearing the MOVED bit
1539 * we do not restrict the tests to just flush
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,
1552 if (hammer2_debug & 0x4000)
1554 spin_unlock(&above->cst.spin);
1557 * Can we finally clear MOVED?
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);
1573 kprintf("ok problem child %p %016jx/%016jx vs %016jx\n", child, child->modify_tid, child->delete_tid, trans->sync_tid);
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);
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
1589 hammer2_chain_unlock(child);
1590 hammer2_chain_drop(child);
1591 spin_lock(&above->cst.spin);
1594 * The parent may have been delete-duplicated.
1596 info->parent = parent;
1603 hammer2_rollup_stats(hammer2_chain_t *parent, hammer2_chain_t *child, int how)
1606 hammer2_chain_t *grandp;
1609 parent->data_count += child->data_count;
1610 parent->inode_count += child->inode_count;
1611 child->data_count = 0;
1612 child->inode_count = 0;
1614 parent->data_count -= child->bytes;
1615 if (child->bref.type == HAMMER2_BREF_TYPE_INODE) {
1616 parent->inode_count -= 1;
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;
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;
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;
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;
1638 for (grandp = parent->above->first_parent;
1640 grandp = grandp->next_parent) {
1641 grandp->data_count += parent->data_count;
1642 grandp->inode_count += parent->inode_count;
1645 parent->data_count = 0;
1646 parent->inode_count = 0;