2 * Copyright (c) 2011-2015 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 * TRANSACTION AND FLUSH HANDLING
38 * Deceptively simple but actually fairly difficult to implement properly is
39 * how I would describe it.
41 * Flushing generally occurs bottom-up but requires a top-down scan to
42 * locate chains with MODIFIED and/or UPDATE bits set. The ONFLUSH flag
43 * tells how to recurse downward to find these chains.
46 #include <sys/cdefs.h>
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/types.h>
57 #define HAMMER2_FLUSH_DEPTH_LIMIT 10 /* stack recursion limit */
61 * Recursively flush the specified chain. The chain is locked and
62 * referenced by the caller and will remain so on return. The chain
63 * will remain referenced throughout but can temporarily lose its
64 * lock during the recursion to avoid unnecessarily stalling user
67 struct hammer2_flush_info
{
68 hammer2_chain_t
*parent
;
73 struct h2_flush_list flushq
;
74 hammer2_chain_t
*debug
;
77 typedef struct hammer2_flush_info hammer2_flush_info_t
;
79 static void hammer2_flush_core(hammer2_flush_info_t
*info
,
80 hammer2_chain_t
*chain
, int flags
);
81 static int hammer2_flush_recurse(hammer2_chain_t
*child
, void *data
);
84 * Any per-pfs transaction initialization goes here.
87 hammer2_trans_manage_init(hammer2_pfs_t
*pmp
)
92 * Transaction support for any modifying operation. Transactions are used
93 * in the pmp layer by the frontend and in the spmp layer by the backend.
95 * 0 - Normal transaction, interlocked against flush
98 * TRANS_ISFLUSH - Flush transaction, interlocked against normal
101 * TRANS_BUFCACHE - Buffer cache transaction, no interlock.
103 * Initializing a new transaction allocates a transaction ID. Typically
104 * passed a pmp (hmp passed as NULL), indicating a cluster transaction. Can
105 * be passed a NULL pmp and non-NULL hmp to indicate a transaction on a single
106 * media target. The latter mode is used by the recovery code.
108 * TWO TRANSACTION IDs can run concurrently, where one is a flush and the
109 * other is a set of any number of concurrent filesystem operations. We
110 * can either have <running_fs_ops> + <waiting_flush> + <blocked_fs_ops>
111 * or we can have <running_flush> + <concurrent_fs_ops>.
113 * During a flush, new fs_ops are only blocked until the fs_ops prior to
114 * the flush complete. The new fs_ops can then run concurrent with the flush.
116 * Buffer-cache transactions operate as fs_ops but never block. A
117 * buffer-cache flush will run either before or after the current pending
118 * flush depending on its state.
121 hammer2_trans_init(hammer2_pfs_t
*pmp
, uint32_t flags
)
128 oflags
= pmp
->trans
.flags
;
132 if (flags
& HAMMER2_TRANS_ISFLUSH
) {
134 * Requesting flush transaction. Wait for all
135 * currently running transactions to finish.
136 * Afterwords, normal transactions will be
139 if (oflags
& HAMMER2_TRANS_MASK
) {
140 nflags
= oflags
| HAMMER2_TRANS_FPENDING
|
141 HAMMER2_TRANS_WAITING
;
144 nflags
= (oflags
| flags
) + 1;
146 } else if (flags
& HAMMER2_TRANS_BUFCACHE
) {
148 * Requesting strategy transaction from buffer-cache,
149 * or a VM getpages/putpages through the buffer cache.
150 * We must allow such transactions in all situations
151 * to avoid deadlocks.
153 nflags
= (oflags
| flags
) + 1;
156 * (old) previous code interlocked against the main
159 if ((oflags
& (HAMMER2_TRANS_ISFLUSH
|
160 HAMMER2_TRANS_PREFLUSH
)) ==
161 HAMMER2_TRANS_ISFLUSH
) {
162 nflags
= oflags
| HAMMER2_TRANS_WAITING
;
165 nflags
= (oflags
| flags
) + 1;
170 * Requesting normal modifying transaction (read-only
171 * operations do not use transactions). Waits for
172 * any flush to finish before allowing. Multiple
173 * modifying transactions can run concurrently.
175 if (oflags
& HAMMER2_TRANS_ISFLUSH
) {
176 nflags
= oflags
| HAMMER2_TRANS_WAITING
;
179 nflags
= (oflags
| flags
) + 1;
183 tsleep_interlock(&pmp
->trans
.sync_wait
, 0);
184 if (atomic_cmpset_int(&pmp
->trans
.flags
, oflags
, nflags
)) {
187 tsleep(&pmp
->trans
.sync_wait
, PINTERLOCKED
,
197 * Start a sub-transaction, there is no 'subdone' function. This will
198 * issue a new modify_tid (mtid) for the current transaction, which is a
199 * CLC (cluster level change) id and not a per-node id.
201 * This function must be called for each XOP when multiple XOPs are run in
202 * sequence within a transaction.
204 * Callers typically update the inode with the transaction mtid manually
205 * to enforce sequencing.
208 hammer2_trans_sub(hammer2_pfs_t
*pmp
)
212 mtid
= atomic_fetchadd_64(&pmp
->modify_tid
, 1);
218 hammer2_trans_done(hammer2_pfs_t
*pmp
)
224 oflags
= pmp
->trans
.flags
;
226 KKASSERT(oflags
& HAMMER2_TRANS_MASK
);
227 if ((oflags
& HAMMER2_TRANS_MASK
) == 1) {
229 * This was the last transaction
231 nflags
= (oflags
- 1) & ~(HAMMER2_TRANS_ISFLUSH
|
232 HAMMER2_TRANS_BUFCACHE
|
233 HAMMER2_TRANS_FPENDING
|
234 HAMMER2_TRANS_WAITING
);
237 * Still transactions pending
241 if (atomic_cmpset_int(&pmp
->trans
.flags
, oflags
, nflags
)) {
242 if ((nflags
& HAMMER2_TRANS_MASK
) == 0 &&
243 (oflags
& HAMMER2_TRANS_WAITING
)) {
244 wakeup(&pmp
->trans
.sync_wait
);
255 * Obtain new, unique inode number (not serialized by caller).
258 hammer2_trans_newinum(hammer2_pfs_t
*pmp
)
262 tid
= atomic_fetchadd_64(&pmp
->inode_tid
, 1);
268 * Assert that a strategy call is ok here. Currently we allow strategy
269 * calls in all situations, including during flushes. Previously:
270 * (old) (1) In a normal transaction.
271 * (old) (2) In a flush transaction only if PREFLUSH is also set.
274 hammer2_trans_assert_strategy(hammer2_pfs_t
*pmp
)
277 KKASSERT((pmp
->trans
.flags
& HAMMER2_TRANS_ISFLUSH
) == 0 ||
278 (pmp
->trans
.flags
& HAMMER2_TRANS_PREFLUSH
));
284 * Chains undergoing destruction are removed from the in-memory topology.
285 * To avoid getting lost these chains are placed on the delayed flush
286 * queue which will properly dispose of them.
288 * We do this instead of issuing an immediate flush in order to give
289 * recursive deletions (rm -rf, etc) a chance to remove more of the
290 * hierarchy, potentially allowing an enormous amount of write I/O to
294 hammer2_delayed_flush(hammer2_chain_t
*chain
)
296 if ((chain
->flags
& HAMMER2_CHAIN_DELAYED
) == 0) {
297 hammer2_spin_ex(&chain
->hmp
->list_spin
);
298 if ((chain
->flags
& (HAMMER2_CHAIN_DELAYED
|
299 HAMMER2_CHAIN_DEFERRED
)) == 0) {
300 atomic_set_int(&chain
->flags
, HAMMER2_CHAIN_DELAYED
|
301 HAMMER2_CHAIN_DEFERRED
);
302 TAILQ_INSERT_TAIL(&chain
->hmp
->flushq
,
304 hammer2_chain_ref(chain
);
306 hammer2_spin_unex(&chain
->hmp
->list_spin
);
307 hammer2_voldata_modify(chain
->hmp
);
312 * Flush the chain and all modified sub-chains through the specified
313 * synchronization point, propagating blockref updates back up. As
314 * part of this propagation, mirror_tid and inode/data usage statistics
315 * propagates back upward.
317 * modify_tid (clc - cluster level change) is not propagated.
319 * update_tid (clc) is used for validation and is not propagated by this
322 * This routine can be called from several places but the most important
323 * is from VFS_SYNC (frontend) via hammer2_inode_xop_flush (backend).
325 * chain is locked on call and will remain locked on return. The chain's
326 * UPDATE flag indicates that its parent's block table (which is not yet
327 * part of the flush) should be updated. The chain may be replaced by
328 * the call if it was modified.
331 hammer2_flush(hammer2_chain_t
*chain
, int flags
)
333 hammer2_chain_t
*scan
;
334 hammer2_flush_info_t info
;
339 * Execute the recursive flush and handle deferrals.
341 * Chains can be ridiculously long (thousands deep), so to
342 * avoid blowing out the kernel stack the recursive flush has a
343 * depth limit. Elements at the limit are placed on a list
344 * for re-execution after the stack has been popped.
346 bzero(&info
, sizeof(info
));
347 TAILQ_INIT(&info
.flushq
);
348 info
.cache_index
= -1;
349 info
.flags
= flags
& ~HAMMER2_FLUSH_TOP
;
352 * Calculate parent (can be NULL), if not NULL the flush core
353 * expects the parent to be referenced so it can easily lock/unlock
354 * it without it getting ripped up.
356 if ((info
.parent
= chain
->parent
) != NULL
)
357 hammer2_chain_ref(info
.parent
);
360 * Extra ref needed because flush_core expects it when replacing
363 hammer2_chain_ref(chain
);
369 * Move hmp->flushq to info.flushq if non-empty so it can
372 if (TAILQ_FIRST(&hmp
->flushq
) != NULL
) {
373 hammer2_spin_ex(&chain
->hmp
->list_spin
);
374 TAILQ_CONCAT(&info
.flushq
, &hmp
->flushq
, flush_node
);
375 hammer2_spin_unex(&chain
->hmp
->list_spin
);
379 * Unwind deep recursions which had been deferred. This
380 * can leave the FLUSH_* bits set for these chains, which
381 * will be handled when we [re]flush chain after the unwind.
383 while ((scan
= TAILQ_FIRST(&info
.flushq
)) != NULL
) {
384 KKASSERT(scan
->flags
& HAMMER2_CHAIN_DEFERRED
);
385 TAILQ_REMOVE(&info
.flushq
, scan
, flush_node
);
386 atomic_clear_int(&scan
->flags
, HAMMER2_CHAIN_DEFERRED
|
387 HAMMER2_CHAIN_DELAYED
);
390 * Now that we've popped back up we can do a secondary
391 * recursion on the deferred elements.
393 * NOTE: hammer2_flush() may replace scan.
395 if (hammer2_debug
& 0x0040)
396 kprintf("deferred flush %p\n", scan
);
397 hammer2_chain_lock(scan
, HAMMER2_RESOLVE_MAYBE
);
398 hammer2_flush(scan
, flags
& ~HAMMER2_FLUSH_TOP
);
399 hammer2_chain_unlock(scan
);
400 hammer2_chain_drop(scan
); /* ref from deferral */
406 info
.diddeferral
= 0;
407 hammer2_flush_core(&info
, chain
, flags
);
410 * Only loop if deep recursions have been deferred.
412 if (TAILQ_EMPTY(&info
.flushq
))
415 if (++loops
% 1000 == 0) {
416 kprintf("hammer2_flush: excessive loops on %p\n",
418 if (hammer2_debug
& 0x100000)
422 hammer2_chain_drop(chain
);
424 hammer2_chain_drop(info
.parent
);
428 * This is the core of the chain flushing code. The chain is locked by the
429 * caller and must also have an extra ref on it by the caller, and remains
430 * locked and will have an extra ref on return. info.parent is referenced
433 * Upon return, the caller can test the UPDATE bit on the chain to determine
434 * if the parent needs updating.
436 * (1) Determine if this node is a candidate for the flush, return if it is
437 * not. fchain and vchain are always candidates for the flush.
439 * (2) If we recurse too deep the chain is entered onto the deferral list and
440 * the current flush stack is aborted until after the deferral list is
443 * (3) Recursively flush live children (rbtree). This can create deferrals.
444 * A successful flush clears the MODIFIED and UPDATE bits on the children
445 * and typically causes the parent to be marked MODIFIED as the children
446 * update the parent's block table. A parent might already be marked
447 * MODIFIED due to a deletion (whos blocktable update in the parent is
448 * handled by the frontend), or if the parent itself is modified by the
449 * frontend for other reasons.
451 * (4) Permanently disconnected sub-trees are cleaned up by the front-end.
452 * Deleted-but-open inodes can still be individually flushed via the
455 * (5) Delete parents on the way back up if they are normal indirect blocks
456 * and have no children.
458 * (6) Note that an unmodified child may still need the block table in its
459 * parent updated (e.g. rename/move). The child will have UPDATE set
462 * WARNING ON BREF MODIFY_TID/MIRROR_TID
464 * blockref.modify_tid is consistent only within a PFS, and will not be
465 * consistent during synchronization. mirror_tid is consistent across the
466 * block device regardless of the PFS.
469 hammer2_flush_core(hammer2_flush_info_t
*info
, hammer2_chain_t
*chain
,
472 hammer2_chain_t
*parent
;
477 * (1) Optimize downward recursion to locate nodes needing action.
478 * Nothing to do if none of these flags are set.
480 if ((chain
->flags
& HAMMER2_CHAIN_FLUSH_MASK
) == 0) {
481 if (hammer2_debug
& 0x200) {
482 if (info
->debug
== NULL
)
490 diddeferral
= info
->diddeferral
;
491 parent
= info
->parent
; /* can be NULL */
492 KKASSERT(chain
->parent
== parent
);
495 * Downward search recursion
497 if (chain
->flags
& (HAMMER2_CHAIN_DEFERRED
| HAMMER2_CHAIN_DELAYED
)) {
502 } else if ((chain
->flags
& HAMMER2_CHAIN_PFSBOUNDARY
) &&
503 (flags
& HAMMER2_FLUSH_ALL
) == 0 &&
504 (flags
& HAMMER2_FLUSH_TOP
) == 0) {
506 * If FLUSH_ALL is not specified the caller does not want
507 * to recurse through PFS roots. The typical sequence is
508 * to flush dirty PFS's starting at their root downward,
509 * then flush the device root (vchain). It is this second
510 * flush that typically leaves out the ALL flag.
512 * However we must still process the PFSROOT chains for block
513 * table updates in their parent (which IS part of our flush).
515 * NOTE: The volume root, vchain, does not set PFSBOUNDARY.
517 * NOTE: This test must be done before the depth-limit test,
518 * else it might become the top on a flushq iteration.
520 * NOTE: We must re-set ONFLUSH in the parent to retain if
521 * this chain (that we are skipping) requires work.
523 if (chain
->flags
& (HAMMER2_CHAIN_ONFLUSH
|
524 HAMMER2_CHAIN_DESTROY
|
525 HAMMER2_CHAIN_MODIFIED
)) {
526 hammer2_chain_setflush(parent
);
528 } else if (info
->depth
== HAMMER2_FLUSH_DEPTH_LIMIT
) {
530 * Recursion depth reached.
532 KKASSERT((chain
->flags
& HAMMER2_CHAIN_DELAYED
) == 0);
533 hammer2_chain_ref(chain
);
534 TAILQ_INSERT_TAIL(&info
->flushq
, chain
, flush_node
);
535 atomic_set_int(&chain
->flags
, HAMMER2_CHAIN_DEFERRED
);
537 } else if (chain
->flags
& (HAMMER2_CHAIN_ONFLUSH
|
538 HAMMER2_CHAIN_DESTROY
)) {
540 * Downward recursion search (actual flush occurs bottom-up).
541 * pre-clear ONFLUSH. It can get set again due to races,
542 * which we want so the scan finds us again in the next flush.
544 * We must also recurse if DESTROY is set so we can finally
545 * get rid of the related children, otherwise the node will
546 * just get re-flushed on lastdrop.
548 * WARNING! The recursion will unlock/relock info->parent
549 * (which is 'chain'), potentially allowing it
552 atomic_clear_int(&chain
->flags
, HAMMER2_CHAIN_ONFLUSH
);
553 info
->parent
= chain
;
554 hammer2_spin_ex(&chain
->core
.spin
);
555 RB_SCAN(hammer2_chain_tree
, &chain
->core
.rbtree
,
556 NULL
, hammer2_flush_recurse
, info
);
557 hammer2_spin_unex(&chain
->core
.spin
);
558 info
->parent
= parent
;
559 if (info
->diddeferral
)
560 hammer2_chain_setflush(chain
);
563 * If we lost the parent->chain association we have to
564 * stop processing this chain because it is no longer
565 * in this recursion. If it moved, it will be handled
566 * by the ONFLUSH flag elsewhere.
568 if (chain
->parent
!= parent
) {
569 kprintf("LOST CHILD2 %p->%p (actual parent %p)\n",
570 parent
, chain
, chain
->parent
);
576 * Now we are in the bottom-up part of the recursion.
578 * Do not update chain if lower layers were deferred.
580 if (info
->diddeferral
)
584 * Both parent and chain must be locked in order to flush chain,
585 * in order to properly update the parent under certain conditions.
587 * In addition, we can't safely unlock/relock the chain once we
588 * start flushing the chain itself, which we would have to do later
589 * on in order to lock the parent if we didn't do that now.
591 hammer2_chain_unlock(chain
);
593 hammer2_chain_lock(parent
, HAMMER2_RESOLVE_ALWAYS
);
594 hammer2_chain_lock(chain
, HAMMER2_RESOLVE_MAYBE
);
595 if (chain
->parent
!= parent
) {
596 kprintf("LOST CHILD3 %p->%p (actual parent %p)\n",
597 parent
, chain
, chain
->parent
);
598 KKASSERT(parent
!= NULL
);
599 hammer2_chain_unlock(parent
);
600 if ((chain
->flags
& HAMMER2_CHAIN_DELAYED
) == 0) {
601 hammer2_chain_ref(chain
);
602 TAILQ_INSERT_TAIL(&info
->flushq
, chain
, flush_node
);
603 atomic_set_int(&chain
->flags
, HAMMER2_CHAIN_DEFERRED
);
610 * Propagate the DESTROY flag downwards. This dummies up the flush
611 * code and tries to invalidate related buffer cache buffers to
612 * avoid the disk write.
614 if (parent
&& (parent
->flags
& HAMMER2_CHAIN_DESTROY
))
615 atomic_set_int(&chain
->flags
, HAMMER2_CHAIN_DESTROY
);
618 * Chain was already modified or has become modified, flush it out.
620 if ((hammer2_debug
& 0x200) &&
622 (chain
->flags
& (HAMMER2_CHAIN_MODIFIED
| HAMMER2_CHAIN_UPDATE
))) {
623 hammer2_chain_t
*scan
= chain
;
625 kprintf("DISCONNECTED FLUSH %p->%p\n", info
->debug
, chain
);
627 kprintf(" chain %p [%08x] bref=%016jx:%02x\n",
629 scan
->bref
.key
, scan
->bref
.type
);
630 if (scan
== info
->debug
)
636 if (chain
->flags
& HAMMER2_CHAIN_MODIFIED
) {
638 * Dispose of the modified bit.
640 * If parent is present, the UPDATE bit should already be set.
641 * UPDATE should already be set.
642 * bref.mirror_tid should already be set.
644 KKASSERT((chain
->flags
& HAMMER2_CHAIN_UPDATE
) ||
645 chain
->parent
== NULL
);
646 if (hammer2_debug
& 0x800000) {
649 for (pp
= chain
; pp
->parent
; pp
= pp
->parent
)
651 kprintf("FLUSH CHAIN %p (p=%p pp=%p/%d) TYPE %d FLAGS %08x (%s)\n",
652 chain
, chain
->parent
, pp
, pp
->bref
.type
,
653 chain
->bref
.type
, chain
->flags
,
654 (chain
->bref
.type
== 1 ? (const char *)chain
->data
->ipdata
.filename
: "?")
659 atomic_clear_int(&chain
->flags
, HAMMER2_CHAIN_MODIFIED
);
660 atomic_add_long(&hammer2_count_modified_chains
, -1);
663 * Manage threads waiting for excessive dirty memory to
667 hammer2_pfs_memory_wakeup(chain
->pmp
);
670 if ((chain
->flags
& HAMMER2_CHAIN_UPDATE
) == 0 &&
671 chain
!= &hmp
->vchain
&&
672 chain
!= &hmp
->fchain
) {
674 * Set UPDATE bit indicating that the parent block
675 * table requires updating.
677 atomic_set_int(&chain
->flags
, HAMMER2_CHAIN_UPDATE
);
682 * Issue the flush. This is indirect via the DIO.
684 * NOTE: A DELETED node that reaches this point must be
685 * flushed for synchronization point consistency.
687 * NOTE: Even though MODIFIED was already set, the related DIO
688 * might not be dirty due to a system buffer cache
689 * flush and must be set dirty if we are going to make
690 * further modifications to the buffer. Chains with
691 * embedded data don't need this.
693 if (hammer2_debug
& 0x1000) {
694 kprintf("Flush %p.%d %016jx/%d data=%016jx\n",
695 chain
, chain
->bref
.type
,
696 (uintmax_t)chain
->bref
.key
,
698 (uintmax_t)chain
->bref
.data_off
);
700 if (hammer2_debug
& 0x2000) {
701 Debugger("Flush hell");
705 * Update chain CRCs for flush.
707 * NOTE: Volume headers are NOT flushed here as they require
708 * special processing.
710 switch(chain
->bref
.type
) {
711 case HAMMER2_BREF_TYPE_FREEMAP
:
713 * Update the volume header's freemap_tid to the
714 * freemap's flushing mirror_tid.
716 * (note: embedded data, do not call setdirty)
718 KKASSERT(hmp
->vchain
.flags
& HAMMER2_CHAIN_MODIFIED
);
719 KKASSERT(chain
== &hmp
->fchain
);
720 hmp
->voldata
.freemap_tid
= chain
->bref
.mirror_tid
;
721 if (hammer2_debug
& 0x8000) {
722 /* debug only, avoid syslogd loop */
723 kprintf("sync freemap mirror_tid %08jx\n",
724 (intmax_t)chain
->bref
.mirror_tid
);
728 * The freemap can be flushed independently of the
729 * main topology, but for the case where it is
730 * flushed in the same transaction, and flushed
731 * before vchain (a case we want to allow for
732 * performance reasons), make sure modifications
733 * made during the flush under vchain use a new
736 * Otherwise the mount recovery code will get confused.
738 ++hmp
->voldata
.mirror_tid
;
740 case HAMMER2_BREF_TYPE_VOLUME
:
742 * The free block table is flushed by
743 * hammer2_vfs_sync() before it flushes vchain.
744 * We must still hold fchain locked while copying
745 * voldata to volsync, however.
747 * (note: embedded data, do not call setdirty)
749 hammer2_chain_lock(&hmp
->fchain
,
750 HAMMER2_RESOLVE_ALWAYS
);
751 hammer2_voldata_lock(hmp
);
752 if (hammer2_debug
& 0x8000) {
753 /* debug only, avoid syslogd loop */
754 kprintf("sync volume mirror_tid %08jx\n",
755 (intmax_t)chain
->bref
.mirror_tid
);
759 * Update the volume header's mirror_tid to the
760 * main topology's flushing mirror_tid. It is
761 * possible that voldata.mirror_tid is already
762 * beyond bref.mirror_tid due to the bump we made
763 * above in BREF_TYPE_FREEMAP.
765 if (hmp
->voldata
.mirror_tid
< chain
->bref
.mirror_tid
) {
766 hmp
->voldata
.mirror_tid
=
767 chain
->bref
.mirror_tid
;
771 * The volume header is flushed manually by the
772 * syncer, not here. All we do here is adjust the
775 KKASSERT(chain
->data
!= NULL
);
776 KKASSERT(chain
->dio
== NULL
);
778 hmp
->voldata
.icrc_sects
[HAMMER2_VOL_ICRC_SECT1
]=
780 (char *)&hmp
->voldata
+
781 HAMMER2_VOLUME_ICRC1_OFF
,
782 HAMMER2_VOLUME_ICRC1_SIZE
);
783 hmp
->voldata
.icrc_sects
[HAMMER2_VOL_ICRC_SECT0
]=
785 (char *)&hmp
->voldata
+
786 HAMMER2_VOLUME_ICRC0_OFF
,
787 HAMMER2_VOLUME_ICRC0_SIZE
);
788 hmp
->voldata
.icrc_volheader
=
790 (char *)&hmp
->voldata
+
791 HAMMER2_VOLUME_ICRCVH_OFF
,
792 HAMMER2_VOLUME_ICRCVH_SIZE
);
794 if (hammer2_debug
& 0x8000) {
795 /* debug only, avoid syslogd loop */
796 kprintf("syncvolhdr %016jx %016jx\n",
797 hmp
->voldata
.mirror_tid
,
798 hmp
->vchain
.bref
.mirror_tid
);
800 hmp
->volsync
= hmp
->voldata
;
801 atomic_set_int(&chain
->flags
, HAMMER2_CHAIN_VOLUMESYNC
);
802 hammer2_voldata_unlock(hmp
);
803 hammer2_chain_unlock(&hmp
->fchain
);
805 case HAMMER2_BREF_TYPE_DATA
:
807 * Data elements have already been flushed via the
808 * logical file buffer cache. Their hash was set in
809 * the bref by the vop_write code. Do not re-dirty.
811 * Make sure any device buffer(s) have been flushed
812 * out here (there aren't usually any to flush) XXX.
815 case HAMMER2_BREF_TYPE_INDIRECT
:
816 case HAMMER2_BREF_TYPE_FREEMAP_NODE
:
817 case HAMMER2_BREF_TYPE_FREEMAP_LEAF
:
819 * Buffer I/O will be cleaned up when the volume is
820 * flushed (but the kernel is free to flush it before
823 KKASSERT((chain
->flags
& HAMMER2_CHAIN_EMBEDDED
) == 0);
824 hammer2_chain_setcheck(chain
, chain
->data
);
826 case HAMMER2_BREF_TYPE_DIRENT
:
828 * A directory entry can use the check area to store
829 * the filename for filenames <= 64 bytes, don't blow
832 KKASSERT((chain
->flags
& HAMMER2_CHAIN_EMBEDDED
) == 0);
834 hammer2_chain_setcheck(chain
, chain
->data
);
836 case HAMMER2_BREF_TYPE_INODE
:
838 * NOTE: We must call io_setdirty() to make any late
839 * changes to the inode data, the system might
840 * have already flushed the buffer.
842 if (chain
->data
->ipdata
.meta
.op_flags
&
843 HAMMER2_OPFLAG_PFSROOT
) {
845 * non-NULL pmp if mounted as a PFS. We must
846 * sync fields cached in the pmp? XXX
848 hammer2_inode_data_t
*ipdata
;
850 hammer2_io_setdirty(chain
->dio
);
851 ipdata
= &chain
->data
->ipdata
;
853 ipdata
->meta
.pfs_inum
=
854 chain
->pmp
->inode_tid
;
857 /* can't be mounted as a PFS */
860 KKASSERT((chain
->flags
& HAMMER2_CHAIN_EMBEDDED
) == 0);
861 hammer2_chain_setcheck(chain
, chain
->data
);
864 KKASSERT(chain
->flags
& HAMMER2_CHAIN_EMBEDDED
);
865 panic("hammer2_flush_core: unsupported "
872 * If the chain was destroyed try to avoid unnecessary I/O.
873 * The DIO system buffer may silently disallow the
876 if (chain
->flags
& HAMMER2_CHAIN_DESTROY
) {
880 hammer2_io_setinval(chain
->dio
,
881 chain
->bref
.data_off
,
883 } else if ((dio
= hammer2_io_getquick(hmp
,
884 chain
->bref
.data_off
,
885 chain
->bytes
)) != NULL
) {
886 hammer2_io_setinval(dio
,
887 chain
->bref
.data_off
,
889 hammer2_io_putblk(&dio
);
895 * If UPDATE is set the parent block table may need to be updated.
897 * NOTE: UPDATE may be set on vchain or fchain in which case
898 * parent could be NULL. It's easiest to allow the case
899 * and test for NULL. parent can also wind up being NULL
900 * due to a deletion so we need to handle the case anyway.
902 * If no parent exists we can just clear the UPDATE bit. If the
903 * chain gets reattached later on the bit will simply get set
906 if ((chain
->flags
& HAMMER2_CHAIN_UPDATE
) && parent
== NULL
)
907 atomic_clear_int(&chain
->flags
, HAMMER2_CHAIN_UPDATE
);
910 * The chain may need its blockrefs updated in the parent.
912 if (chain
->flags
& HAMMER2_CHAIN_UPDATE
) {
913 hammer2_blockref_t
*base
;
917 * Clear UPDATE flag, mark parent modified, update its
918 * modify_tid if necessary, and adjust the parent blockmap.
920 if (chain
->flags
& HAMMER2_CHAIN_UPDATE
)
921 atomic_clear_int(&chain
->flags
, HAMMER2_CHAIN_UPDATE
);
926 * Avoid actually modifying and updating the parent if it
927 * was flagged for destruction. This can greatly reduce
928 * disk I/O in large tree removals because the
929 * hammer2_io_setinval() call in the upward recursion
930 * (see MODIFIED code above) can only handle a few cases.
932 if (parent
->flags
& HAMMER2_CHAIN_DESTROY
) {
933 if (parent
->bref
.modify_tid
< chain
->bref
.modify_tid
) {
934 parent
->bref
.modify_tid
=
935 chain
->bref
.modify_tid
;
937 atomic_clear_int(&chain
->flags
, HAMMER2_CHAIN_BMAPPED
|
938 HAMMER2_CHAIN_BMAPUPD
);
943 * (semi-optional code)
945 * The flusher is responsible for deleting empty indirect
946 * blocks at this point. If we don't do this, no major harm
947 * will be done but the empty indirect blocks will stay in
948 * the topology and make it a bit messy.
950 if (chain
->bref
.type
== HAMMER2_BREF_TYPE_INDIRECT
&&
951 chain
->core
.live_count
== 0 &&
952 (chain
->flags
& (HAMMER2_CHAIN_INITIAL
|
953 HAMMER2_CHAIN_COUNTEDBREFS
)) == 0) {
954 base
= &chain
->data
->npdata
[0];
955 count
= chain
->bytes
/ sizeof(hammer2_blockref_t
);
956 hammer2_chain_countbrefs(chain
, base
, count
);
958 if (chain
->bref
.type
== HAMMER2_BREF_TYPE_INDIRECT
&&
959 chain
->core
.live_count
== 0) {
961 kprintf("DELETE CHAIN %016jx.%02x %016jx/%d refs=%d\n",
962 chain
->bref
.data_off
, chain
->bref
.type
,
963 chain
->bref
.key
, chain
->bref
.keybits
,
966 hammer2_chain_delete(parent
, chain
,
967 chain
->bref
.modify_tid
,
968 HAMMER2_DELETE_PERMANENT
);
973 * We are updating the parent's blockmap, the parent must
976 hammer2_chain_modify(parent
, 0, 0, 0);
977 if (parent
->bref
.modify_tid
< chain
->bref
.modify_tid
)
978 parent
->bref
.modify_tid
= chain
->bref
.modify_tid
;
981 * Calculate blockmap pointer
983 switch(parent
->bref
.type
) {
984 case HAMMER2_BREF_TYPE_INODE
:
986 * Access the inode's block array. However, there is
987 * no block array if the inode is flagged DIRECTDATA.
990 (parent
->data
->ipdata
.meta
.op_flags
&
991 HAMMER2_OPFLAG_DIRECTDATA
) == 0) {
992 base
= &parent
->data
->
993 ipdata
.u
.blockset
.blockref
[0];
997 count
= HAMMER2_SET_COUNT
;
999 case HAMMER2_BREF_TYPE_INDIRECT
:
1000 case HAMMER2_BREF_TYPE_FREEMAP_NODE
:
1002 base
= &parent
->data
->npdata
[0];
1005 count
= parent
->bytes
/ sizeof(hammer2_blockref_t
);
1007 case HAMMER2_BREF_TYPE_VOLUME
:
1008 base
= &chain
->hmp
->voldata
.sroot_blockset
.blockref
[0];
1009 count
= HAMMER2_SET_COUNT
;
1011 case HAMMER2_BREF_TYPE_FREEMAP
:
1012 base
= &parent
->data
->npdata
[0];
1013 count
= HAMMER2_SET_COUNT
;
1018 panic("hammer2_flush_core: "
1019 "unrecognized blockref type: %d",
1024 * Blocktable updates
1026 * We synchronize pending statistics at this time. Delta
1027 * adjustments designated for the current and upper level
1030 if (base
&& (chain
->flags
& HAMMER2_CHAIN_BMAPUPD
)) {
1031 if (chain
->flags
& HAMMER2_CHAIN_BMAPPED
) {
1032 hammer2_spin_ex(&parent
->core
.spin
);
1033 hammer2_base_delete(parent
, base
, count
,
1034 &info
->cache_index
, chain
);
1035 hammer2_spin_unex(&parent
->core
.spin
);
1036 /* base_delete clears both bits */
1038 atomic_clear_int(&chain
->flags
,
1039 HAMMER2_CHAIN_BMAPUPD
);
1042 if (base
&& (chain
->flags
& HAMMER2_CHAIN_BMAPPED
) == 0) {
1043 hammer2_spin_ex(&parent
->core
.spin
);
1044 hammer2_base_insert(parent
, base
, count
,
1045 &info
->cache_index
, chain
);
1046 hammer2_spin_unex(&parent
->core
.spin
);
1047 /* base_insert sets BMAPPED */
1052 hammer2_chain_unlock(parent
);
1055 * Final cleanup after flush
1058 KKASSERT(chain
->refs
> 0);
1059 if (hammer2_debug
& 0x200) {
1060 if (info
->debug
== chain
)
1066 * Flush recursion helper, called from flush_core, calls flush_core.
1068 * Flushes the children of the caller's chain (info->parent), restricted
1069 * by sync_tid. Set info->domodify if the child's blockref must propagate
1070 * back up to the parent.
1072 * Ripouts can move child from rbtree to dbtree or dbq but the caller's
1073 * flush scan order prevents any chains from being lost. A child can be
1074 * executes more than once.
1076 * WARNING! If we do not call hammer2_flush_core() we must update
1077 * bref.mirror_tid ourselves to indicate that the flush has
1078 * processed the child.
1080 * WARNING! parent->core spinlock is held on entry and return.
1083 hammer2_flush_recurse(hammer2_chain_t
*child
, void *data
)
1085 hammer2_flush_info_t
*info
= data
;
1086 hammer2_chain_t
*parent
= info
->parent
;
1089 * (child can never be fchain or vchain so a special check isn't
1092 * We must ref the child before unlocking the spinlock.
1094 * The caller has added a ref to the parent so we can temporarily
1095 * unlock it in order to lock the child. However, if it no longer
1096 * winds up being the child of the parent we must skip this child.
1098 hammer2_chain_ref(child
);
1099 hammer2_spin_unex(&parent
->core
.spin
);
1101 hammer2_chain_unlock(parent
);
1102 hammer2_chain_lock(child
, HAMMER2_RESOLVE_MAYBE
);
1103 if (child
->parent
!= parent
) {
1104 kprintf("LOST CHILD1 %p->%p (actual parent %p)\n",
1105 parent
, child
, child
->parent
);
1110 * Must propagate the DESTROY flag downwards, otherwise the
1111 * parent could end up never being removed because it will
1112 * be requeued to the flusher if it survives this run due to
1115 if (parent
&& (parent
->flags
& HAMMER2_CHAIN_DESTROY
))
1116 atomic_set_int(&child
->flags
, HAMMER2_CHAIN_DESTROY
);
1119 * Recurse and collect deferral data. We're in the media flush,
1120 * this can cross PFS boundaries.
1122 if (child
->flags
& HAMMER2_CHAIN_FLUSH_MASK
) {
1124 hammer2_flush_core(info
, child
, info
->flags
);
1126 } else if (hammer2_debug
& 0x200) {
1127 if (info
->debug
== NULL
)
1128 info
->debug
= child
;
1130 hammer2_flush_core(info
, child
, info
->flags
);
1132 if (info
->debug
== child
)
1138 * Relock to continue the loop
1140 hammer2_chain_unlock(child
);
1141 hammer2_chain_lock(parent
, HAMMER2_RESOLVE_MAYBE
);
1142 hammer2_chain_drop(child
);
1143 KKASSERT(info
->parent
== parent
);
1144 hammer2_spin_ex(&parent
->core
.spin
);
1150 * Flush helper (direct)
1152 * Quickly flushes any dirty chains for a device and returns a temporary
1153 * out-of-band copy of hmp->vchain that the caller can use as a stable
1156 * This function does not flush the actual volume root and does not flush dirty
1157 * device buffers. We don't care about pending work, per-say. This function
1158 * is primarily used by the bulkfree code to create a stable snapshot of
1162 hammer2_flush_quick(hammer2_dev_t
*hmp
)
1164 hammer2_chain_t
*chain
;
1165 hammer2_chain_t
*copy
;
1167 hammer2_trans_init(hmp
->spmp
, HAMMER2_TRANS_ISFLUSH
);
1169 hammer2_chain_ref(&hmp
->vchain
);
1170 hammer2_chain_lock(&hmp
->vchain
, HAMMER2_RESOLVE_ALWAYS
);
1171 if (hmp
->vchain
.flags
& HAMMER2_CHAIN_FLUSH_MASK
) {
1172 chain
= &hmp
->vchain
;
1173 hammer2_flush(chain
, HAMMER2_FLUSH_TOP
|
1175 KKASSERT(chain
== &hmp
->vchain
);
1177 copy
= hammer2_chain_bulksnap(&hmp
->vchain
);
1178 hammer2_chain_unlock(&hmp
->vchain
);
1179 hammer2_chain_drop(&hmp
->vchain
);
1181 hammer2_trans_done(hmp
->spmp
); /* spmp trans */
1187 * flush helper (backend threaded)
1189 * Flushes core chains, issues disk sync, flushes volume roots.
1191 * Primarily called from vfs_sync().
1194 hammer2_inode_xop_flush(hammer2_thread_t
*thr
, hammer2_xop_t
*arg
)
1196 hammer2_xop_flush_t
*xop
= &arg
->xop_flush
;
1197 hammer2_chain_t
*chain
;
1198 hammer2_chain_t
*parent
;
1201 int total_error
= 0;
1207 chain
= hammer2_inode_chain(xop
->head
.ip1
, thr
->clindex
,
1208 HAMMER2_RESOLVE_ALWAYS
);
1211 if ((chain
->flags
& HAMMER2_CHAIN_FLUSH_MASK
) ||
1212 TAILQ_FIRST(&hmp
->flushq
) != NULL
) {
1213 hammer2_flush(chain
, HAMMER2_FLUSH_TOP
);
1214 parent
= chain
->parent
;
1215 KKASSERT(chain
->pmp
!= parent
->pmp
);
1216 hammer2_chain_setflush(parent
);
1218 hammer2_chain_unlock(chain
);
1219 hammer2_chain_drop(chain
);
1226 * Flush volume roots. Avoid replication, we only want to
1227 * flush each hammer2_dev (hmp) once.
1229 for (j
= thr
->clindex
- 1; j
>= 0; --j
) {
1230 if ((chain
= xop
->head
.ip1
->cluster
.array
[j
].chain
) != NULL
) {
1231 if (chain
->hmp
== hmp
) {
1232 chain
= NULL
; /* safety */
1237 chain
= NULL
; /* safety */
1240 * spmp transaction. The super-root is never directly mounted so
1241 * there shouldn't be any vnodes, let alone any dirty vnodes
1242 * associated with it, so we shouldn't have to mess around with any
1243 * vnode flushes here.
1245 hammer2_trans_init(hmp
->spmp
, HAMMER2_TRANS_ISFLUSH
);
1248 * Media mounts have two 'roots', vchain for the topology
1249 * and fchain for the free block table. Flush both.
1251 * Note that the topology and free block table are handled
1252 * independently, so the free block table can wind up being
1253 * ahead of the topology. We depend on the bulk free scan
1254 * code to deal with any loose ends.
1256 hammer2_chain_ref(&hmp
->vchain
);
1257 hammer2_chain_lock(&hmp
->vchain
, HAMMER2_RESOLVE_ALWAYS
);
1258 hammer2_chain_ref(&hmp
->fchain
);
1259 hammer2_chain_lock(&hmp
->fchain
, HAMMER2_RESOLVE_ALWAYS
);
1260 if (hmp
->fchain
.flags
& HAMMER2_CHAIN_FLUSH_MASK
) {
1262 * This will also modify vchain as a side effect,
1263 * mark vchain as modified now.
1265 hammer2_voldata_modify(hmp
);
1266 chain
= &hmp
->fchain
;
1267 hammer2_flush(chain
, HAMMER2_FLUSH_TOP
);
1268 KKASSERT(chain
== &hmp
->fchain
);
1270 hammer2_chain_unlock(&hmp
->fchain
);
1271 hammer2_chain_unlock(&hmp
->vchain
);
1272 hammer2_chain_drop(&hmp
->fchain
);
1273 /* vchain dropped down below */
1275 hammer2_chain_lock(&hmp
->vchain
, HAMMER2_RESOLVE_ALWAYS
);
1276 if (hmp
->vchain
.flags
& HAMMER2_CHAIN_FLUSH_MASK
) {
1277 chain
= &hmp
->vchain
;
1278 hammer2_flush(chain
, HAMMER2_FLUSH_TOP
);
1279 KKASSERT(chain
== &hmp
->vchain
);
1281 hammer2_chain_unlock(&hmp
->vchain
);
1282 hammer2_chain_drop(&hmp
->vchain
);
1287 * We can't safely flush the volume header until we have
1288 * flushed any device buffers which have built up.
1290 * XXX this isn't being incremental
1292 vn_lock(hmp
->devvp
, LK_EXCLUSIVE
| LK_RETRY
);
1293 error
= VOP_FSYNC(hmp
->devvp
, MNT_WAIT
, 0);
1294 vn_unlock(hmp
->devvp
);
1297 * The flush code sets CHAIN_VOLUMESYNC to indicate that the
1298 * volume header needs synchronization via hmp->volsync.
1300 * XXX synchronize the flag & data with only this flush XXX
1303 (hmp
->vchain
.flags
& HAMMER2_CHAIN_VOLUMESYNC
)) {
1307 * Synchronize the disk before flushing the volume
1311 bp
->b_bio1
.bio_offset
= 0;
1314 bp
->b_cmd
= BUF_CMD_FLUSH
;
1315 bp
->b_bio1
.bio_done
= biodone_sync
;
1316 bp
->b_bio1
.bio_flags
|= BIO_SYNC
;
1317 vn_strategy(hmp
->devvp
, &bp
->b_bio1
);
1318 biowait(&bp
->b_bio1
, "h2vol");
1322 * Then we can safely flush the version of the
1323 * volume header synchronized by the flush code.
1325 j
= hmp
->volhdrno
+ 1;
1326 if (j
>= HAMMER2_NUM_VOLHDRS
)
1328 if (j
* HAMMER2_ZONE_BYTES64
+ HAMMER2_SEGSIZE
>
1329 hmp
->volsync
.volu_size
) {
1332 if (hammer2_debug
& 0x8000) {
1333 /* debug only, avoid syslogd loop */
1334 kprintf("sync volhdr %d %jd\n",
1335 j
, (intmax_t)hmp
->volsync
.volu_size
);
1337 bp
= getblk(hmp
->devvp
, j
* HAMMER2_ZONE_BYTES64
,
1338 HAMMER2_PBUFSIZE
, 0, 0);
1339 atomic_clear_int(&hmp
->vchain
.flags
,
1340 HAMMER2_CHAIN_VOLUMESYNC
);
1341 bcopy(&hmp
->volsync
, bp
->b_data
, HAMMER2_PBUFSIZE
);
1346 total_error
= error
;
1348 hammer2_trans_done(hmp
->spmp
); /* spmp trans */
1350 error
= hammer2_xop_feed(&xop
->head
, NULL
, thr
->clindex
, total_error
);