usr.sbin/makefs: Sync with sys/vfs/hammer2
[dragonfly.git] / usr.sbin / makefs / hammer2 / hammer2_flush.c
blobb36c03314c9447ea6c269498e013d0dcb65ab1d0
1 /*
2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2022 Tomohiro Kusumi <tkusumi@netbsd.org>
5 * Copyright (c) 2011-2022 The DragonFly Project. All rights reserved.
7 * This code is derived from software contributed to The DragonFly Project
8 * by Matthew Dillon <dillon@dragonflybsd.org>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 * 3. Neither the name of The DragonFly Project nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific, prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
32 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
34 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
38 * TRANSACTION AND FLUSH HANDLING
40 * Deceptively simple but actually fairly difficult to implement properly is
41 * how I would describe it.
43 * Flushing generally occurs bottom-up but requires a top-down scan to
44 * locate chains with MODIFIED and/or UPDATE bits set. The ONFLUSH flag
45 * tells how to recurse downward to find these chains.
49 #include <sys/cdefs.h>
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/types.h>
53 #include <sys/lock.h>
54 #include <sys/vnode.h>
55 #include <sys/buf.h>
58 #include "hammer2.h"
60 #define HAMMER2_FLUSH_DEPTH_LIMIT 60 /* stack recursion limit */
64 * Recursively flush the specified chain. The chain is locked and
65 * referenced by the caller and will remain so on return. The chain
66 * will remain referenced throughout but can temporarily lose its
67 * lock during the recursion to avoid unnecessarily stalling user
68 * processes.
70 struct hammer2_flush_info {
71 hammer2_chain_t *parent;
72 int depth;
73 int error; /* cumulative error */
74 int flags;
75 #ifdef HAMMER2_SCAN_DEBUG
76 long scan_count;
77 long scan_mod_count;
78 long scan_upd_count;
79 long scan_onf_count;
80 long scan_del_count;
81 long scan_btype[7];
82 #endif
85 typedef struct hammer2_flush_info hammer2_flush_info_t;
87 static int hammer2_flush_core(hammer2_flush_info_t *info,
88 hammer2_chain_t *chain, int flags);
89 static int hammer2_flush_recurse(hammer2_chain_t *child, void *data);
92 * Any per-pfs transaction initialization goes here.
94 void
95 hammer2_trans_manage_init(hammer2_pfs_t *pmp)
100 * Transaction support for any modifying operation. Transactions are used
101 * in the pmp layer by the frontend and in the spmp layer by the backend.
103 * 0 - Normal transaction. Interlocks against just the
104 * COPYQ portion of an ISFLUSH transaction.
106 * TRANS_ISFLUSH - Flush transaction. Interlocks against other flush
107 * transactions.
109 * When COPYQ is also specified, waits for the count
110 * to drop to 1.
112 * TRANS_BUFCACHE - Buffer cache transaction. No interlock.
114 * TRANS_SIDEQ - Run the sideq (only tested in trans_done())
116 * Initializing a new transaction allocates a transaction ID. Typically
117 * passed a pmp (hmp passed as NULL), indicating a cluster transaction. Can
118 * be passed a NULL pmp and non-NULL hmp to indicate a transaction on a single
119 * media target. The latter mode is used by the recovery code.
121 void
122 hammer2_trans_init(hammer2_pfs_t *pmp, uint32_t flags)
124 uint32_t oflags;
125 uint32_t nflags;
126 int dowait;
128 for (;;) {
129 oflags = pmp->trans.flags;
130 cpu_ccfence();
131 dowait = 0;
133 if (flags & HAMMER2_TRANS_ISFLUSH) {
135 * Interlock against other flush transactions.
137 if (oflags & HAMMER2_TRANS_ISFLUSH) {
138 nflags = oflags | HAMMER2_TRANS_WAITING;
139 dowait = 1;
140 } else {
141 nflags = (oflags | flags) + 1;
143 } else if (flags & HAMMER2_TRANS_BUFCACHE) {
145 * Requesting strategy transaction from buffer-cache,
146 * or a VM getpages/putpages through the buffer cache.
147 * We must allow such transactions in all situations
148 * to avoid deadlocks.
150 nflags = (oflags | flags) + 1;
151 } else {
153 * Normal transaction. We do not interlock against
154 * BUFCACHE or ISFLUSH.
156 * Note that vnode locks may be held going into
157 * this call.
159 * NOTE: Remember that non-modifying operations
160 * such as read, stat, readdir, etc, do
161 * not use transactions.
163 nflags = (oflags | flags) + 1;
165 if (dowait)
166 tsleep_interlock(&pmp->trans.sync_wait, 0);
167 if (atomic_cmpset_int(&pmp->trans.flags, oflags, nflags)) {
168 if (dowait == 0)
169 break;
170 tsleep(&pmp->trans.sync_wait, PINTERLOCKED,
171 "h2trans", hz);
172 /* retry */
173 } else {
174 cpu_pause();
175 /* retry */
177 /* retry */
180 #if 0
182 * When entering a FLUSH transaction with COPYQ set, wait for the
183 * transaction count to drop to 1 (our flush transaction only)
184 * before proceeding.
186 * This waits for all non-flush transactions to complete and blocks
187 * new non-flush transactions from starting until COPYQ is cleared.
188 * (the flush will then proceed after clearing COPYQ). This should
189 * be a very short stall on modifying operations.
191 while ((flags & HAMMER2_TRANS_ISFLUSH) &&
192 (flags & HAMMER2_TRANS_COPYQ)) {
193 oflags = pmp->trans.flags;
194 cpu_ccfence();
195 if ((oflags & HAMMER2_TRANS_MASK) == 1)
196 break;
197 nflags = oflags | HAMMER2_TRANS_WAITING;
198 tsleep_interlock(&pmp->trans.sync_wait, 0);
199 if (atomic_cmpset_int(&pmp->trans.flags, oflags, nflags)) {
200 tsleep(&pmp->trans.sync_wait, PINTERLOCKED,
201 "h2trans2", hz);
204 #endif
208 * Start a sub-transaction, there is no 'subdone' function. This will
209 * issue a new modify_tid (mtid) for the current transaction, which is a
210 * CLC (cluster level change) id and not a per-node id.
212 * This function must be called for each XOP when multiple XOPs are run in
213 * sequence within a transaction.
215 * Callers typically update the inode with the transaction mtid manually
216 * to enforce sequencing.
218 hammer2_tid_t
219 hammer2_trans_sub(hammer2_pfs_t *pmp)
221 hammer2_tid_t mtid;
223 mtid = atomic_fetchadd_64(&pmp->modify_tid, 1);
225 return (mtid);
228 void
229 hammer2_trans_setflags(hammer2_pfs_t *pmp, uint32_t flags)
231 atomic_set_int(&pmp->trans.flags, flags);
235 * Typically used to clear trans flags asynchronously. If TRANS_WAITING
236 * is in the mask, and was previously set, this function will wake up
237 * any waiters.
239 void
240 hammer2_trans_clearflags(hammer2_pfs_t *pmp, uint32_t flags)
242 uint32_t oflags;
243 uint32_t nflags;
245 for (;;) {
246 oflags = pmp->trans.flags;
247 cpu_ccfence();
248 nflags = oflags & ~flags;
249 if (atomic_cmpset_int(&pmp->trans.flags, oflags, nflags)) {
250 if ((oflags ^ nflags) & HAMMER2_TRANS_WAITING)
251 wakeup(&pmp->trans.sync_wait);
252 break;
254 cpu_pause();
255 /* retry */
259 void
260 hammer2_trans_done(hammer2_pfs_t *pmp, uint32_t flags)
262 uint32_t oflags;
263 uint32_t nflags;
265 #if 0
267 * Modifying ops on the front-end can cause dirty inodes to
268 * build up in the sideq. We don't flush these on inactive/reclaim
269 * due to potential deadlocks, so we have to deal with them from
270 * inside other nominal modifying front-end transactions.
272 if ((flags & HAMMER2_TRANS_SIDEQ) &&
273 pmp->sideq_count > hammer2_limit_dirty_inodes / 2 &&
274 pmp->sideq_count > (pmp->inum_count >> 3) &&
275 pmp->mp) {
276 speedup_syncer(pmp->mp);
278 #endif
281 * Clean-up the transaction. Wakeup any waiters when finishing
282 * a flush transaction or transitioning the non-flush transaction
283 * count from 2->1 while a flush transaction is pending.
285 for (;;) {
286 oflags = pmp->trans.flags;
287 cpu_ccfence();
288 KKASSERT(oflags & HAMMER2_TRANS_MASK);
290 nflags = (oflags - 1) & ~flags;
291 if (flags & HAMMER2_TRANS_ISFLUSH) {
292 nflags &= ~HAMMER2_TRANS_WAITING;
294 if ((oflags & (HAMMER2_TRANS_ISFLUSH|HAMMER2_TRANS_MASK)) ==
295 (HAMMER2_TRANS_ISFLUSH|2)) {
296 nflags &= ~HAMMER2_TRANS_WAITING;
298 if (atomic_cmpset_int(&pmp->trans.flags, oflags, nflags)) {
299 if ((oflags ^ nflags) & HAMMER2_TRANS_WAITING)
300 wakeup(&pmp->trans.sync_wait);
301 break;
303 cpu_pause();
304 /* retry */
309 * Obtain new, unique inode number (not serialized by caller).
311 hammer2_tid_t
312 hammer2_trans_newinum(hammer2_pfs_t *pmp)
314 hammer2_tid_t tid;
316 tid = atomic_fetchadd_64(&pmp->inode_tid, 1);
318 return tid;
322 * Assert that a strategy call is ok here. Currently we allow strategy
323 * calls in all situations, including during flushes. Previously:
324 * (old) (1) In a normal transaction.
326 void
327 hammer2_trans_assert_strategy(hammer2_pfs_t *pmp)
329 #if 0
330 KKASSERT((pmp->trans.flags & HAMMER2_TRANS_ISFLUSH) == 0);
331 #endif
335 * Flush the chain and all modified sub-chains through the specified
336 * synchronization point, propagating blockref updates back up. As
337 * part of this propagation, mirror_tid and inode/data usage statistics
338 * propagates back upward.
340 * Returns a HAMMER2 error code, 0 if no error. Note that I/O errors from
341 * buffers dirtied during the flush operation can occur later.
343 * modify_tid (clc - cluster level change) is not propagated.
345 * update_tid (clc) is used for validation and is not propagated by this
346 * function.
348 * This routine can be called from several places but the most important
349 * is from VFS_SYNC (frontend) via hammer2_xop_inode_flush (backend).
351 * chain is locked on call and will remain locked on return. The chain's
352 * UPDATE flag indicates that its parent's block table (which is not yet
353 * part of the flush) should be updated.
355 * flags:
356 * HAMMER2_FLUSH_TOP Indicates that this is the top of the flush.
357 * Is cleared for the recursion.
359 * HAMMER2_FLUSH_ALL Recurse everything
361 * HAMMER2_FLUSH_INODE_STOP
362 * Stop at PFS inode or normal inode boundary
365 hammer2_flush(hammer2_chain_t *chain, int flags)
367 hammer2_flush_info_t info;
368 int loops;
371 * Execute the recursive flush and handle deferrals.
373 * Chains can be ridiculously long (thousands deep), so to
374 * avoid blowing out the kernel stack the recursive flush has a
375 * depth limit. Elements at the limit are placed on a list
376 * for re-execution after the stack has been popped.
378 bzero(&info, sizeof(info));
379 info.flags = flags & ~HAMMER2_FLUSH_TOP;
382 * Calculate parent (can be NULL), if not NULL the flush core
383 * expects the parent to be referenced so it can easily lock/unlock
384 * it without it getting ripped up.
386 if ((info.parent = chain->parent) != NULL)
387 hammer2_chain_ref(info.parent);
390 * Extra ref needed because flush_core expects it when replacing
391 * chain.
393 hammer2_chain_ref(chain);
394 loops = 0;
396 for (;;) {
398 * [re]flush chain as the deep recursion may have generated
399 * additional modifications.
401 if (info.parent != chain->parent) {
402 if (hammer2_debug & 0x0040) {
403 kprintf("LOST CHILD4 %p->%p "
404 "(actual parent %p)\n",
405 info.parent, chain, chain->parent);
407 hammer2_chain_drop(info.parent);
408 info.parent = chain->parent;
409 hammer2_chain_ref(info.parent);
411 if (hammer2_flush_core(&info, chain, flags) == 0)
412 break;
414 if (++loops % 1000 == 0) {
415 kprintf("hammer2_flush: excessive loops on %p\n",
416 chain);
417 if (hammer2_debug & 0x100000)
418 Debugger("hell4");
421 #ifdef HAMMER2_SCAN_DEBUG
422 if (info.scan_count >= 10)
423 kprintf("hammer2_flush: scan_count %ld (%ld,%ld,%ld,%ld) "
424 "bt(%ld,%ld,%ld,%ld,%ld,%ld)\n",
425 info.scan_count,
426 info.scan_mod_count,
427 info.scan_upd_count,
428 info.scan_onf_count,
429 info.scan_del_count,
430 info.scan_btype[1],
431 info.scan_btype[2],
432 info.scan_btype[3],
433 info.scan_btype[4],
434 info.scan_btype[5],
435 info.scan_btype[6]);
436 #endif
437 hammer2_chain_drop(chain);
438 if (info.parent)
439 hammer2_chain_drop(info.parent);
440 return (info.error);
444 * This is the core of the chain flushing code. The chain is locked by the
445 * caller and must also have an extra ref on it by the caller, and remains
446 * locked and will have an extra ref on return. info.parent is referenced
447 * but not locked.
449 * Upon return, the caller can test the UPDATE bit on the chain to determine
450 * if the parent needs updating.
452 * If non-zero is returned, the chain's parent changed during the flush and
453 * the caller must retry the operation.
455 * (1) Determine if this node is a candidate for the flush, return if it is
456 * not. fchain and vchain are always candidates for the flush.
458 * (2) If we recurse too deep the chain is entered onto the deferral list and
459 * the current flush stack is aborted until after the deferral list is
460 * run.
462 * (3) Recursively flush live children (rbtree). This can create deferrals.
463 * A successful flush clears the MODIFIED and UPDATE bits on the children
464 * and typically causes the parent to be marked MODIFIED as the children
465 * update the parent's block table. A parent might already be marked
466 * MODIFIED due to a deletion (whos blocktable update in the parent is
467 * handled by the frontend), or if the parent itself is modified by the
468 * frontend for other reasons.
470 * (4) Permanently disconnected sub-trees are cleaned up by the front-end.
471 * Deleted-but-open inodes can still be individually flushed via the
472 * filesystem syncer.
474 * (5) Delete parents on the way back up if they are normal indirect blocks
475 * and have no children.
477 * (6) Note that an unmodified child may still need the block table in its
478 * parent updated (e.g. rename/move). The child will have UPDATE set
479 * in this case.
481 * WARNING ON BREF MODIFY_TID/MIRROR_TID
483 * blockref.modify_tid is consistent only within a PFS, and will not be
484 * consistent during synchronization. mirror_tid is consistent across the
485 * block device regardless of the PFS.
487 static int
488 hammer2_flush_core(hammer2_flush_info_t *info, hammer2_chain_t *chain,
489 int flags)
491 hammer2_chain_t *parent;
492 hammer2_dev_t *hmp;
493 int save_error;
494 int retry;
496 retry = 0;
499 * (1) Optimize downward recursion to locate nodes needing action.
500 * Nothing to do if none of these flags are set.
502 if ((chain->flags & HAMMER2_CHAIN_FLUSH_MASK) == 0)
503 return 0;
505 hmp = chain->hmp;
508 * NOTE: parent can be NULL, usually due to destroy races.
510 parent = info->parent;
511 KKASSERT(chain->parent == parent);
514 * Downward search recursion
516 * We must be careful on cold stops, which often occur on inode
517 * boundaries due to the way hammer2_vfs_sync() sequences the flush.
518 * Be sure to issue an appropriate chain_setflush()
520 if ((chain->flags & HAMMER2_CHAIN_PFSBOUNDARY) &&
521 (flags & HAMMER2_FLUSH_ALL) == 0 &&
522 (flags & HAMMER2_FLUSH_TOP) == 0 &&
523 chain->pmp && chain->pmp->mp) {
525 * If FLUSH_ALL is not specified the caller does not want
526 * to recurse through PFS roots that have been mounted.
528 * (If the PFS has not been mounted there may not be
529 * anything monitoring its chains and its up to us
530 * to flush it).
532 * The typical sequence is to flush dirty PFS's starting at
533 * their root downward, then flush the device root (vchain).
534 * It is this second flush that typically leaves out the
535 * ALL flag.
537 * However we must still process the PFSROOT chains for block
538 * table updates in their parent (which IS part of our flush).
540 * NOTE: The volume root, vchain, does not set PFSBOUNDARY.
542 * NOTE: We must re-set ONFLUSH in the parent to retain if
543 * this chain (that we are skipping) requires work.
545 if (chain->flags & (HAMMER2_CHAIN_ONFLUSH |
546 HAMMER2_CHAIN_DESTROY |
547 HAMMER2_CHAIN_MODIFIED)) {
548 hammer2_chain_setflush(parent);
550 goto done;
551 } else if (chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
552 (flags & HAMMER2_FLUSH_INODE_STOP) &&
553 (flags & HAMMER2_FLUSH_ALL) == 0 &&
554 (flags & HAMMER2_FLUSH_TOP) == 0 &&
555 chain->pmp && chain->pmp->mp) {
557 * When FLUSH_INODE_STOP is specified we are being asked not
558 * to include any inode changes for inodes we encounter,
559 * with the exception of the inode that the flush began with.
560 * So: INODE, INODE_STOP, and TOP==0 basically.
562 * Dirty inodes are flushed based on the hammer2_inode
563 * in-memory structure, issuing a chain_setflush() here
564 * will only cause unnecessary traversals of the topology.
566 goto done;
567 #if 0
569 * If FLUSH_INODE_STOP is specified and both ALL and TOP
570 * are clear, we must not flush the chain. The chain should
571 * have already been flushed and any further ONFLUSH/UPDATE
572 * setting will be related to the next flush.
574 * This features allows us to flush inodes independently of
575 * each other and meta-data above the inodes separately.
577 if (chain->flags & (HAMMER2_CHAIN_ONFLUSH |
578 HAMMER2_CHAIN_DESTROY |
579 HAMMER2_CHAIN_MODIFIED)) {
580 if (parent)
581 hammer2_chain_setflush(parent);
583 #endif
584 } else if (info->depth == HAMMER2_FLUSH_DEPTH_LIMIT) {
586 * Recursion depth reached.
588 panic("hammer2: flush depth limit");
589 } else if (chain->flags & (HAMMER2_CHAIN_ONFLUSH |
590 HAMMER2_CHAIN_DESTROY)) {
592 * Downward recursion search (actual flush occurs bottom-up).
593 * pre-clear ONFLUSH. It can get set again due to races or
594 * flush errors, which we want so the scan finds us again in
595 * the next flush.
597 * We must also recurse if DESTROY is set so we can finally
598 * get rid of the related children, otherwise the node will
599 * just get re-flushed on lastdrop.
601 * WARNING! The recursion will unlock/relock info->parent
602 * (which is 'chain'), potentially allowing it
603 * to be ripped up.
605 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONFLUSH);
606 save_error = info->error;
607 info->error = 0;
608 info->parent = chain;
611 * We may have to do this twice to catch any indirect
612 * block maintenance that occurs.
614 hammer2_spin_ex(&chain->core.spin);
615 RB_SCAN(hammer2_chain_tree, &chain->core.rbtree,
616 NULL, hammer2_flush_recurse, info);
617 if (chain->flags & HAMMER2_CHAIN_ONFLUSH) {
618 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONFLUSH);
619 RB_SCAN(hammer2_chain_tree, &chain->core.rbtree,
620 NULL, hammer2_flush_recurse, info);
622 hammer2_spin_unex(&chain->core.spin);
623 info->parent = parent;
626 * Re-set the flush bits if the flush was incomplete or
627 * an error occurred. If an error occurs it is typically
628 * an allocation error. Errors do not cause deferrals.
630 if (info->error)
631 hammer2_chain_setflush(chain);
632 info->error |= save_error;
635 * If we lost the parent->chain association we have to
636 * stop processing this chain because it is no longer
637 * in this recursion. If it moved, it will be handled
638 * by the ONFLUSH flag elsewhere.
640 if (chain->parent != parent) {
641 kprintf("LOST CHILD2 %p->%p (actual parent %p)\n",
642 parent, chain, chain->parent);
643 goto done;
648 * Now we are in the bottom-up part of the recursion.
650 * We continue to try to update the chain on lower-level errors, but
651 * the flush code may decide not to flush the volume root.
653 * XXX should we continue to try to update the chain if an error
654 * occurred?
658 * Both parent and chain must be locked in order to flush chain,
659 * in order to properly update the parent under certain conditions.
661 * In addition, we can't safely unlock/relock the chain once we
662 * start flushing the chain itself, which we would have to do later
663 * on in order to lock the parent if we didn't do that now.
665 hammer2_chain_ref_hold(chain);
666 hammer2_chain_unlock(chain);
667 if (parent)
668 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
669 hammer2_chain_lock(chain, HAMMER2_RESOLVE_MAYBE);
670 hammer2_chain_drop_unhold(chain);
673 * Can't process if we can't access their content.
675 if ((parent && parent->error) || chain->error) {
676 kprintf("hammer2: chain error during flush\n");
677 info->error |= chain->error;
678 if (parent) {
679 info->error |= parent->error;
680 hammer2_chain_unlock(parent);
682 goto done;
685 if (chain->parent != parent) {
686 if (hammer2_debug & 0x0040) {
687 kprintf("LOST CHILD3 %p->%p (actual parent %p)\n",
688 parent, chain, chain->parent);
690 KKASSERT(parent != NULL);
691 hammer2_chain_unlock(parent);
692 retry = 1;
693 goto done;
697 * Propagate the DESTROY flag downwards. This dummies up the flush
698 * code and tries to invalidate related buffer cache buffers to
699 * avoid the disk write.
701 if (parent && (parent->flags & HAMMER2_CHAIN_DESTROY))
702 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DESTROY);
705 * Dispose of the modified bit.
707 * If parent is present, the UPDATE bit should already be set.
708 * UPDATE should already be set.
709 * bref.mirror_tid should already be set.
711 if (chain->flags & HAMMER2_CHAIN_MODIFIED) {
712 KKASSERT((chain->flags & HAMMER2_CHAIN_UPDATE) ||
713 chain->parent == NULL);
714 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_MODIFIED);
715 atomic_add_long(&hammer2_count_modified_chains, -1);
718 * Manage threads waiting for excessive dirty memory to
719 * be retired.
721 if (chain->pmp)
722 hammer2_pfs_memory_wakeup(chain->pmp, -1);
724 #if 0
725 if ((chain->flags & HAMMER2_CHAIN_UPDATE) == 0 &&
726 chain != &hmp->vchain &&
727 chain != &hmp->fchain) {
729 * Set UPDATE bit indicating that the parent block
730 * table requires updating.
732 atomic_set_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
734 #endif
737 * Issue the flush. This is indirect via the DIO.
739 * NOTE: A DELETED node that reaches this point must be
740 * flushed for synchronization point consistency.
742 * NOTE: Even though MODIFIED was already set, the related DIO
743 * might not be dirty due to a system buffer cache
744 * flush and must be set dirty if we are going to make
745 * further modifications to the buffer. Chains with
746 * embedded data don't need this.
748 if (hammer2_debug & 0x1000) {
749 kprintf("Flush %p.%d %016jx/%d data=%016jx\n",
750 chain, chain->bref.type,
751 (uintmax_t)chain->bref.key,
752 chain->bref.keybits,
753 (uintmax_t)chain->bref.data_off);
757 * Update chain CRCs for flush.
759 * NOTE: Volume headers are NOT flushed here as they require
760 * special processing.
762 switch(chain->bref.type) {
763 case HAMMER2_BREF_TYPE_FREEMAP:
765 * Update the volume header's freemap_tid to the
766 * freemap's flushing mirror_tid.
768 * (note: embedded data, do not call setdirty)
770 KKASSERT(hmp->vchain.flags & HAMMER2_CHAIN_MODIFIED);
771 KKASSERT(chain == &hmp->fchain);
772 hmp->voldata.freemap_tid = chain->bref.mirror_tid;
773 if (hammer2_debug & 0x8000) {
774 /* debug only, avoid syslogd loop */
775 kprintf("sync freemap mirror_tid %08jx\n",
776 (intmax_t)chain->bref.mirror_tid);
780 * The freemap can be flushed independently of the
781 * main topology, but for the case where it is
782 * flushed in the same transaction, and flushed
783 * before vchain (a case we want to allow for
784 * performance reasons), make sure modifications
785 * made during the flush under vchain use a new
786 * transaction id.
788 * Otherwise the mount recovery code will get confused.
790 ++hmp->voldata.mirror_tid;
791 break;
792 case HAMMER2_BREF_TYPE_VOLUME:
794 * The free block table is flushed by
795 * hammer2_vfs_sync() before it flushes vchain.
796 * We must still hold fchain locked while copying
797 * voldata to volsync, however.
799 * These do not error per-say since their data does
800 * not need to be re-read from media on lock.
802 * (note: embedded data, do not call setdirty)
804 hammer2_chain_lock(&hmp->fchain,
805 HAMMER2_RESOLVE_ALWAYS);
806 hammer2_voldata_lock(hmp);
807 if (hammer2_debug & 0x8000) {
808 /* debug only, avoid syslogd loop */
809 kprintf("sync volume mirror_tid %08jx\n",
810 (intmax_t)chain->bref.mirror_tid);
814 * Update the volume header's mirror_tid to the
815 * main topology's flushing mirror_tid. It is
816 * possible that voldata.mirror_tid is already
817 * beyond bref.mirror_tid due to the bump we made
818 * above in BREF_TYPE_FREEMAP.
820 if (hmp->voldata.mirror_tid < chain->bref.mirror_tid) {
821 hmp->voldata.mirror_tid =
822 chain->bref.mirror_tid;
826 * The volume header is flushed manually by the
827 * syncer, not here. All we do here is adjust the
828 * crc's.
830 KKASSERT(chain->data != NULL);
831 KKASSERT(chain->dio == NULL);
833 hmp->voldata.icrc_sects[HAMMER2_VOL_ICRC_SECT1]=
834 hammer2_icrc32(
835 (char *)&hmp->voldata +
836 HAMMER2_VOLUME_ICRC1_OFF,
837 HAMMER2_VOLUME_ICRC1_SIZE);
838 hmp->voldata.icrc_sects[HAMMER2_VOL_ICRC_SECT0]=
839 hammer2_icrc32(
840 (char *)&hmp->voldata +
841 HAMMER2_VOLUME_ICRC0_OFF,
842 HAMMER2_VOLUME_ICRC0_SIZE);
843 hmp->voldata.icrc_volheader =
844 hammer2_icrc32(
845 (char *)&hmp->voldata +
846 HAMMER2_VOLUME_ICRCVH_OFF,
847 HAMMER2_VOLUME_ICRCVH_SIZE);
849 if (hammer2_debug & 0x8000) {
850 /* debug only, avoid syslogd loop */
851 kprintf("syncvolhdr %016jx %016jx\n",
852 hmp->voldata.mirror_tid,
853 hmp->vchain.bref.mirror_tid);
855 hmp->volsync = hmp->voldata;
856 atomic_set_int(&chain->flags, HAMMER2_CHAIN_VOLUMESYNC);
857 hammer2_voldata_unlock(hmp);
858 hammer2_chain_unlock(&hmp->fchain);
859 break;
860 case HAMMER2_BREF_TYPE_DATA:
862 * Data elements have already been flushed via the
863 * logical file buffer cache. Their hash was set in
864 * the bref by the vop_write code. Do not re-dirty.
866 * Make sure any device buffer(s) have been flushed
867 * out here (there aren't usually any to flush) XXX.
869 break;
870 case HAMMER2_BREF_TYPE_INDIRECT:
871 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
872 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
874 * Buffer I/O will be cleaned up when the volume is
875 * flushed (but the kernel is free to flush it before
876 * then, as well).
878 hammer2_chain_setcheck(chain, chain->data);
879 break;
880 case HAMMER2_BREF_TYPE_DIRENT:
882 * A directory entry can use the check area to store
883 * the filename for filenames <= 64 bytes, don't blow
884 * it up!
886 if (chain->bytes)
887 hammer2_chain_setcheck(chain, chain->data);
888 break;
889 case HAMMER2_BREF_TYPE_INODE:
891 * NOTE: We must call io_setdirty() to make any late
892 * changes to the inode data, the system might
893 * have already flushed the buffer.
895 if (chain->data->ipdata.meta.op_flags &
896 HAMMER2_OPFLAG_PFSROOT) {
898 * non-NULL pmp if mounted as a PFS. We must
899 * sync fields cached in the pmp? XXX
901 hammer2_inode_data_t *ipdata;
903 hammer2_io_setdirty(chain->dio);
904 ipdata = &chain->data->ipdata;
905 if (chain->pmp) {
906 ipdata->meta.pfs_inum =
907 chain->pmp->inode_tid;
909 } else {
910 /* can't be mounted as a PFS */
913 hammer2_chain_setcheck(chain, chain->data);
914 break;
915 default:
916 panic("hammer2_flush_core: unsupported "
917 "embedded bref %d",
918 chain->bref.type);
919 /* NOT REACHED */
923 * If the chain was destroyed try to avoid unnecessary I/O
924 * that might not have yet occurred. Remove the data range
925 * from dedup candidacy and attempt to invalidation that
926 * potentially dirty portion of the I/O buffer.
928 if (chain->flags & HAMMER2_CHAIN_DESTROY) {
929 hammer2_io_dedup_delete(hmp,
930 chain->bref.type,
931 chain->bref.data_off,
932 chain->bytes);
933 #if 0
934 hammer2_io_t *dio;
935 if (chain->dio) {
936 hammer2_io_inval(chain->dio,
937 chain->bref.data_off,
938 chain->bytes);
939 } else if ((dio = hammer2_io_getquick(hmp,
940 chain->bref.data_off,
941 chain->bytes,
942 1)) != NULL) {
943 hammer2_io_inval(dio,
944 chain->bref.data_off,
945 chain->bytes);
946 hammer2_io_putblk(&dio);
948 #endif
953 * If UPDATE is set the parent block table may need to be updated.
954 * This can fail if the hammer2_chain_modify() fails.
956 * NOTE: UPDATE may be set on vchain or fchain in which case
957 * parent could be NULL, or on an inode that has not yet
958 * been inserted into the radix tree. It's easiest to allow
959 * the case and test for NULL. parent can also wind up being
960 * NULL due to a deletion so we need to handle the case anyway.
962 * NOTE: UPDATE can be set when chains are renamed into or out of
963 * an indirect block, without the chain itself being flagged
964 * MODIFIED.
966 * If no parent exists we can just clear the UPDATE bit. If the
967 * chain gets reattached later on the bit will simply get set
968 * again.
970 if ((chain->flags & HAMMER2_CHAIN_UPDATE) && parent == NULL)
971 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
974 * When flushing an inode outside of a FLUSH_FSSYNC we must NOT
975 * update the parent block table to point at the flushed inode.
976 * The block table should only ever be updated by the filesystem
977 * sync code. If we do, inode<->inode dependencies (such as
978 * directory entries vs inode nlink count) can wind up not being
979 * flushed together and result in a broken topology if a crash/reboot
980 * occurs at the wrong time.
982 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
983 (flags & HAMMER2_FLUSH_INODE_STOP) &&
984 (flags & HAMMER2_FLUSH_FSSYNC) == 0 &&
985 (flags & HAMMER2_FLUSH_ALL) == 0 &&
986 chain->pmp && chain->pmp->mp) {
987 #ifdef HAMMER2_DEBUG_SYNC
988 kprintf("inum %ld do not update parent, non-fssync\n",
989 (long)chain->bref.key);
990 #endif
991 goto skipupdate;
993 #ifdef HAMMER2_DEBUG_SYNC
994 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE)
995 kprintf("inum %ld update parent\n", (long)chain->bref.key);
996 #endif
999 * The chain may need its blockrefs updated in the parent, normal
1000 * path.
1002 if (chain->flags & HAMMER2_CHAIN_UPDATE) {
1003 hammer2_blockref_t *base;
1004 int count;
1007 * Clear UPDATE flag, mark parent modified, update its
1008 * modify_tid if necessary, and adjust the parent blockmap.
1010 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
1013 * (optional code)
1015 * Avoid actually modifying and updating the parent if it
1016 * was flagged for destruction. This can greatly reduce
1017 * disk I/O in large tree removals because the
1018 * hammer2_io_setinval() call in the upward recursion
1019 * (see MODIFIED code above) can only handle a few cases.
1021 if (parent->flags & HAMMER2_CHAIN_DESTROY) {
1022 if (parent->bref.modify_tid < chain->bref.modify_tid) {
1023 parent->bref.modify_tid =
1024 chain->bref.modify_tid;
1026 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_BLKMAPPED |
1027 HAMMER2_CHAIN_BLKMAPUPD);
1028 goto skipupdate;
1032 * The flusher is responsible for deleting empty indirect
1033 * blocks at this point. If we don't do this, no major harm
1034 * will be done but the empty indirect blocks will stay in
1035 * the topology and make it a messy and inefficient.
1037 * The flusher is also responsible for collapsing the
1038 * content of an indirect block into its parent whenever
1039 * possible (with some hysteresis). Not doing this will also
1040 * not harm the topology, but would make it messy and
1041 * inefficient.
1043 if (chain->bref.type == HAMMER2_BREF_TYPE_INDIRECT) {
1044 if (hammer2_chain_indirect_maintenance(parent, chain))
1045 goto skipupdate;
1049 * We are updating the parent's blockmap, the parent must
1050 * be set modified. If this fails we re-set the UPDATE flag
1051 * in the child.
1053 * NOTE! A modification error can be ENOSPC. We still want
1054 * to flush modified chains recursively, not break out,
1055 * so we just skip the update in this situation and
1056 * continue. That is, we still need to try to clean
1057 * out dirty chains and buffers.
1059 * This may not help bulkfree though. XXX
1061 save_error = hammer2_chain_modify(parent, 0, 0, 0);
1062 if (save_error) {
1063 info->error |= save_error;
1064 kprintf("hammer2_flush: %016jx.%02x error=%08x\n",
1065 parent->bref.data_off, parent->bref.type,
1066 save_error);
1067 atomic_set_int(&chain->flags, HAMMER2_CHAIN_UPDATE);
1068 goto skipupdate;
1070 if (parent->bref.modify_tid < chain->bref.modify_tid)
1071 parent->bref.modify_tid = chain->bref.modify_tid;
1074 * Calculate blockmap pointer
1076 switch(parent->bref.type) {
1077 case HAMMER2_BREF_TYPE_INODE:
1079 * Access the inode's block array. However, there is
1080 * no block array if the inode is flagged DIRECTDATA.
1082 if (parent->data &&
1083 (parent->data->ipdata.meta.op_flags &
1084 HAMMER2_OPFLAG_DIRECTDATA) == 0) {
1085 base = &parent->data->
1086 ipdata.u.blockset.blockref[0];
1087 } else {
1088 base = NULL;
1090 count = HAMMER2_SET_COUNT;
1091 break;
1092 case HAMMER2_BREF_TYPE_INDIRECT:
1093 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1094 if (parent->data)
1095 base = &parent->data->npdata[0];
1096 else
1097 base = NULL;
1098 count = parent->bytes / sizeof(hammer2_blockref_t);
1099 break;
1100 case HAMMER2_BREF_TYPE_VOLUME:
1101 base = &chain->hmp->voldata.sroot_blockset.blockref[0];
1102 count = HAMMER2_SET_COUNT;
1103 break;
1104 case HAMMER2_BREF_TYPE_FREEMAP:
1105 base = &parent->data->npdata[0];
1106 count = HAMMER2_SET_COUNT;
1107 break;
1108 default:
1109 base = NULL;
1110 count = 0;
1111 panic("hammer2_flush_core: "
1112 "unrecognized blockref type: %d",
1113 parent->bref.type);
1114 break;
1118 * Blocktable updates
1120 * We synchronize pending statistics at this time. Delta
1121 * adjustments designated for the current and upper level
1122 * are synchronized.
1124 if (base && (chain->flags & HAMMER2_CHAIN_BLKMAPUPD)) {
1125 if (chain->flags & HAMMER2_CHAIN_BLKMAPPED) {
1126 hammer2_spin_ex(&parent->core.spin);
1127 hammer2_base_delete(parent, base, count, chain,
1128 NULL);
1129 hammer2_spin_unex(&parent->core.spin);
1130 /* base_delete clears both bits */
1131 } else {
1132 atomic_clear_int(&chain->flags,
1133 HAMMER2_CHAIN_BLKMAPUPD);
1136 if (base && (chain->flags & HAMMER2_CHAIN_BLKMAPPED) == 0) {
1137 hammer2_spin_ex(&parent->core.spin);
1138 hammer2_base_insert(parent, base, count,
1139 chain, &chain->bref);
1140 hammer2_spin_unex(&parent->core.spin);
1141 /* base_insert sets BLKMAPPED */
1144 skipupdate:
1145 if (parent)
1146 hammer2_chain_unlock(parent);
1149 * Final cleanup after flush
1151 done:
1152 KKASSERT(chain->refs > 0);
1154 return retry;
1158 * Flush recursion helper, called from flush_core, calls flush_core.
1160 * Flushes the children of the caller's chain (info->parent), restricted
1161 * by sync_tid.
1163 * This function may set info->error as a side effect.
1165 * WARNING! If we do not call hammer2_flush_core() we must update
1166 * bref.mirror_tid ourselves to indicate that the flush has
1167 * processed the child.
1169 * WARNING! parent->core spinlock is held on entry and return.
1171 static int
1172 hammer2_flush_recurse(hammer2_chain_t *child, void *data)
1174 hammer2_flush_info_t *info = data;
1175 hammer2_chain_t *parent = info->parent;
1177 #ifdef HAMMER2_SCAN_DEBUG
1178 ++info->scan_count;
1179 if (child->flags & HAMMER2_CHAIN_MODIFIED)
1180 ++info->scan_mod_count;
1181 if (child->flags & HAMMER2_CHAIN_UPDATE)
1182 ++info->scan_upd_count;
1183 if (child->flags & HAMMER2_CHAIN_ONFLUSH)
1184 ++info->scan_onf_count;
1185 #endif
1188 * (child can never be fchain or vchain so a special check isn't
1189 * needed).
1191 * We must ref the child before unlocking the spinlock.
1193 * The caller has added a ref to the parent so we can temporarily
1194 * unlock it in order to lock the child. However, if it no longer
1195 * winds up being the child of the parent we must skip this child.
1197 * NOTE! chain locking errors are fatal. They are never out-of-space
1198 * errors.
1200 hammer2_chain_ref(child);
1201 hammer2_spin_unex(&parent->core.spin);
1203 hammer2_chain_ref_hold(parent);
1204 hammer2_chain_unlock(parent);
1205 hammer2_chain_lock(child, HAMMER2_RESOLVE_MAYBE);
1206 if (child->parent != parent) {
1207 kprintf("LOST CHILD1 %p->%p (actual parent %p)\n",
1208 parent, child, child->parent);
1209 goto done;
1211 if (child->error) {
1212 kprintf("CHILD ERROR DURING FLUSH LOCK %p->%p\n",
1213 parent, child);
1214 info->error |= child->error;
1215 goto done;
1219 * Must propagate the DESTROY flag downwards, otherwise the
1220 * parent could end up never being removed because it will
1221 * be requeued to the flusher if it survives this run due to
1222 * the flag.
1224 if (parent && (parent->flags & HAMMER2_CHAIN_DESTROY))
1225 atomic_set_int(&child->flags, HAMMER2_CHAIN_DESTROY);
1226 #ifdef HAMMER2_SCAN_DEBUG
1227 if (child->flags & HAMMER2_CHAIN_DESTROY)
1228 ++info->scan_del_count;
1229 #endif
1231 * Special handling of the root inode. Because the root inode
1232 * contains an index of all the inodes in the PFS in addition to
1233 * its normal directory entries, any flush that is not part of a
1234 * filesystem sync must only flush the directory entries, and not
1235 * anything else.
1237 * The child might be an indirect block, but H2 guarantees that
1238 * the key-range will fully partition the inode index from the
1239 * directory entries so the case just works naturally.
1241 if ((parent->bref.flags & HAMMER2_BREF_FLAG_PFSROOT) &&
1242 (child->flags & HAMMER2_CHAIN_DESTROY) == 0 &&
1243 parent->bref.type == HAMMER2_BREF_TYPE_INODE &&
1244 (info->flags & HAMMER2_FLUSH_FSSYNC) == 0) {
1245 if ((child->bref.key & HAMMER2_DIRHASH_VISIBLE) == 0) {
1246 if (child->flags & HAMMER2_CHAIN_FLUSH_MASK) {
1247 hammer2_chain_setflush(parent);
1249 goto done;
1254 * Recurse and collect deferral data. We're in the media flush,
1255 * this can cross PFS boundaries.
1257 if (child->flags & HAMMER2_CHAIN_FLUSH_MASK) {
1258 #ifdef HAMMER2_SCAN_DEBUG
1259 if (child->bref.type < 7)
1260 ++info->scan_btype[child->bref.type];
1261 #endif
1262 ++info->depth;
1263 hammer2_flush_core(info, child, info->flags);
1264 --info->depth;
1267 done:
1269 * Relock to continue the loop.
1271 hammer2_chain_unlock(child);
1272 hammer2_chain_lock(parent, HAMMER2_RESOLVE_MAYBE);
1273 hammer2_chain_drop_unhold(parent);
1274 if (parent->error) {
1275 kprintf("PARENT ERROR DURING FLUSH LOCK %p->%p\n",
1276 parent, child);
1277 info->error |= parent->error;
1279 hammer2_chain_drop(child);
1280 KKASSERT(info->parent == parent);
1281 hammer2_spin_ex(&parent->core.spin);
1283 return (0);
1287 * flush helper (backend threaded)
1289 * Flushes chain topology for the specified inode.
1291 * HAMMER2_XOP_INODE_STOP The flush recursion stops at inode boundaries.
1292 * Inodes belonging to the same flush are flushed
1293 * separately.
1295 * chain->parent can be NULL, usually due to destroy races or detached inodes.
1297 * Primarily called from vfs_sync().
1299 void
1300 hammer2_xop_inode_flush(hammer2_xop_t *arg, void *scratch __unused, int clindex)
1302 hammer2_xop_flush_t *xop = &arg->xop_flush;
1303 hammer2_chain_t *chain;
1304 hammer2_inode_t *ip;
1305 hammer2_dev_t *hmp;
1306 hammer2_pfs_t *pmp;
1307 hammer2_devvp_t *e;
1308 struct m_vnode *devvp;
1309 int flush_error = 0;
1310 int fsync_error = 0;
1311 int total_error = 0;
1312 int j;
1313 int xflags;
1314 int ispfsroot = 0;
1316 xflags = HAMMER2_FLUSH_TOP;
1317 if (xop->head.flags & HAMMER2_XOP_INODE_STOP)
1318 xflags |= HAMMER2_FLUSH_INODE_STOP;
1319 if (xop->head.flags & HAMMER2_XOP_FSSYNC)
1320 xflags |= HAMMER2_FLUSH_FSSYNC;
1323 * Flush core chains
1325 ip = xop->head.ip1;
1326 pmp = ip->pmp;
1327 chain = hammer2_inode_chain(ip, clindex, HAMMER2_RESOLVE_ALWAYS);
1328 if (chain) {
1329 hmp = chain->hmp;
1330 if (chain->flags & HAMMER2_CHAIN_FLUSH_MASK) {
1332 * Due to flush partitioning the chain topology
1333 * above the inode's chain may no longer be flagged.
1334 * When asked to flush an inode, remark the topology
1335 * leading to that inode.
1337 if (chain->parent)
1338 hammer2_chain_setflush(chain->parent);
1339 hammer2_flush(chain, xflags);
1341 /* XXX cluster */
1342 if (ip == pmp->iroot && pmp != hmp->spmp) {
1343 hammer2_spin_ex(&pmp->blockset_spin);
1344 pmp->pfs_iroot_blocksets[clindex] =
1345 chain->data->ipdata.u.blockset;
1346 hammer2_spin_unex(&pmp->blockset_spin);
1349 #if 0
1351 * Propogate upwards but only cross an inode boundary
1352 * for inodes associated with the current filesystem
1353 * sync.
1355 if ((xop->head.flags & HAMMER2_XOP_PARENTONFLUSH) ||
1356 chain->bref.type != HAMMER2_BREF_TYPE_INODE) {
1357 parent = chain->parent;
1358 if (parent)
1359 hammer2_chain_setflush(parent);
1361 #endif
1363 if (chain->flags & HAMMER2_CHAIN_PFSBOUNDARY)
1364 ispfsroot = 1;
1365 hammer2_chain_unlock(chain);
1366 hammer2_chain_drop(chain);
1367 chain = NULL;
1368 } else {
1369 hmp = NULL;
1373 * Only flush the volume header if asked to, plus the inode must also
1374 * be the PFS root.
1376 if ((xop->head.flags & HAMMER2_XOP_VOLHDR) == 0)
1377 goto skip;
1378 if (ispfsroot == 0)
1379 goto skip;
1382 * Flush volume roots. Avoid replication, we only want to
1383 * flush each hammer2_dev (hmp) once.
1385 for (j = clindex - 1; j >= 0; --j) {
1386 if ((chain = ip->cluster.array[j].chain) != NULL) {
1387 if (chain->hmp == hmp) {
1388 chain = NULL; /* safety */
1389 goto skip;
1393 chain = NULL; /* safety */
1396 * spmp transaction. The super-root is never directly mounted so
1397 * there shouldn't be any vnodes, let alone any dirty vnodes
1398 * associated with it, so we shouldn't have to mess around with any
1399 * vnode flushes here.
1401 hammer2_trans_init(hmp->spmp, HAMMER2_TRANS_ISFLUSH);
1404 * We must flush the superroot down to the PFS iroot. Remember
1405 * that hammer2_chain_setflush() stops at inode boundaries, so
1406 * the pmp->iroot has been flushed and flagged down to the superroot,
1407 * but the volume root (vchain) probably has not yet been flagged.
1409 if (hmp->spmp->iroot) {
1410 chain = hmp->spmp->iroot->cluster.array[0].chain;
1411 if (chain) {
1412 hammer2_chain_ref(chain);
1413 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS);
1414 flush_error |=
1415 hammer2_flush(chain,
1416 HAMMER2_FLUSH_TOP |
1417 HAMMER2_FLUSH_INODE_STOP |
1418 HAMMER2_FLUSH_FSSYNC);
1419 hammer2_chain_unlock(chain);
1420 hammer2_chain_drop(chain);
1425 * Media mounts have two 'roots', vchain for the topology
1426 * and fchain for the free block table. Flush both.
1428 * Note that the topology and free block table are handled
1429 * independently, so the free block table can wind up being
1430 * ahead of the topology. We depend on the bulk free scan
1431 * code to deal with any loose ends.
1433 * vchain and fchain do not error on-lock since their data does
1434 * not have to be re-read from media.
1436 hammer2_chain_ref(&hmp->vchain);
1437 hammer2_chain_lock(&hmp->vchain, HAMMER2_RESOLVE_ALWAYS);
1438 hammer2_chain_ref(&hmp->fchain);
1439 hammer2_chain_lock(&hmp->fchain, HAMMER2_RESOLVE_ALWAYS);
1440 if (hmp->fchain.flags & HAMMER2_CHAIN_FLUSH_MASK) {
1442 * This will also modify vchain as a side effect,
1443 * mark vchain as modified now.
1445 hammer2_voldata_modify(hmp);
1446 chain = &hmp->fchain;
1447 flush_error |= hammer2_flush(chain, HAMMER2_FLUSH_TOP);
1448 KKASSERT(chain == &hmp->fchain);
1450 hammer2_chain_unlock(&hmp->fchain);
1451 hammer2_chain_unlock(&hmp->vchain);
1452 hammer2_chain_drop(&hmp->fchain);
1453 /* vchain dropped down below */
1455 hammer2_chain_lock(&hmp->vchain, HAMMER2_RESOLVE_ALWAYS);
1456 if (hmp->vchain.flags & HAMMER2_CHAIN_FLUSH_MASK) {
1457 chain = &hmp->vchain;
1458 flush_error |= hammer2_flush(chain, HAMMER2_FLUSH_TOP);
1459 KKASSERT(chain == &hmp->vchain);
1461 hammer2_chain_unlock(&hmp->vchain);
1462 hammer2_chain_drop(&hmp->vchain);
1465 * We can't safely flush the volume header until we have
1466 * flushed any device buffers which have built up.
1468 * XXX this isn't being incremental
1470 TAILQ_FOREACH(e, &hmp->devvpl, entry) {
1471 devvp = e->devvp;
1472 KKASSERT(devvp);
1473 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1474 fsync_error = VOP_FSYNC(devvp, MNT_WAIT, 0);
1475 vn_unlock(devvp);
1476 if (fsync_error || flush_error) {
1477 kprintf("hammer2: sync error fsync=%d h2flush=0x%04x dev=%s\n",
1478 fsync_error, flush_error, e->path);
1483 * The flush code sets CHAIN_VOLUMESYNC to indicate that the
1484 * volume header needs synchronization via hmp->volsync.
1486 * XXX synchronize the flag & data with only this flush XXX
1488 if (fsync_error == 0 && flush_error == 0 &&
1489 (hmp->vchain.flags & HAMMER2_CHAIN_VOLUMESYNC)) {
1490 struct m_buf *bp;
1491 int vol_error = 0;
1494 * Synchronize the disk before flushing the volume
1495 * header.
1498 bp = getpbuf(NULL);
1499 bp->b_bio1.bio_offset = 0;
1500 bp->b_bufsize = 0;
1501 bp->b_bcount = 0;
1502 bp->b_cmd = BUF_CMD_FLUSH;
1503 bp->b_bio1.bio_done = biodone_sync;
1504 bp->b_bio1.bio_flags |= BIO_SYNC;
1505 vn_strategy(hmp->devvp, &bp->b_bio1);
1506 fsync_error = biowait(&bp->b_bio1, "h2vol");
1507 relpbuf(bp, NULL);
1511 * Then we can safely flush the version of the
1512 * volume header synchronized by the flush code.
1514 j = hmp->volhdrno + 1;
1515 if (j < 0)
1516 j = 0;
1517 if (j >= HAMMER2_NUM_VOLHDRS)
1518 j = 0;
1519 if (j * HAMMER2_ZONE_BYTES64 + HAMMER2_SEGSIZE >
1520 hmp->volsync.volu_size) {
1521 j = 0;
1523 if (hammer2_debug & 0x8000) {
1524 /* debug only, avoid syslogd loop */
1525 kprintf("sync volhdr %d %jd\n",
1526 j, (intmax_t)hmp->volsync.volu_size);
1528 bp = getblkx(hmp->devvp, j * HAMMER2_ZONE_BYTES64,
1529 HAMMER2_VOLUME_BYTES, GETBLK_KVABIO, 0);
1530 atomic_clear_int(&hmp->vchain.flags,
1531 HAMMER2_CHAIN_VOLUMESYNC);
1532 bkvasync(bp);
1533 bcopy(&hmp->volsync, bp->b_data, HAMMER2_VOLUME_BYTES);
1534 vol_error = bwrite(bp);
1535 hmp->volhdrno = j;
1536 if (vol_error)
1537 fsync_error = vol_error;
1539 if (flush_error)
1540 total_error = flush_error;
1541 if (fsync_error)
1542 total_error = hammer2_errno_to_error(fsync_error);
1544 /* spmp trans */
1545 hammer2_trans_done(hmp->spmp, HAMMER2_TRANS_ISFLUSH);
1546 skip:
1547 hammer2_xop_feed(&xop->head, NULL, clindex, total_error);