2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sys/vfs/hammer/hammer_flusher.c,v 1.45 2008/07/31 04:42:04 dillon Exp $
37 * HAMMER dependancy flusher thread
39 * Meta data updates create buffer dependancies which are arranged as a
45 static void hammer_flusher_master_thread(void *arg);
46 static void hammer_flusher_slave_thread(void *arg);
47 static int hammer_flusher_flush(hammer_mount_t hmp, int *nomorep);
48 static void hammer_flusher_flush_inode(hammer_inode_t ip,
49 hammer_transaction_t trans);
51 RB_GENERATE(hammer_fls_rb_tree, hammer_inode, rb_flsnode,
52 hammer_ino_rb_compare);
55 * Inodes are sorted and assigned to slave threads in groups of 128.
56 * We want a flush group size large enough such that the slave threads
57 * are not likely to interfere with each other when accessing the B-Tree,
58 * but not so large that we lose concurrency.
60 #define HAMMER_FLUSH_GROUP_SIZE 128
63 * Support structures for the flusher threads.
65 struct hammer_flusher_info {
66 TAILQ_ENTRY(hammer_flusher_info) entry;
67 struct hammer_mount *hmp;
71 hammer_flush_group_t flg;
72 hammer_inode_t work_array[HAMMER_FLUSH_GROUP_SIZE];
75 typedef struct hammer_flusher_info *hammer_flusher_info_t;
78 * Sync all inodes pending on the flusher.
80 * All flush groups will be flushed. This does not queue dirty inodes
81 * to the flush groups, it just flushes out what has already been queued!
84 hammer_flusher_sync(hammer_mount_t hmp)
88 seq = hammer_flusher_async(hmp, NULL);
89 hammer_flusher_wait(hmp, seq);
93 * Sync all flush groups through to close_flg - return immediately.
94 * If close_flg is NULL all flush groups are synced.
96 * Returns the sequence number of the last closed flush group,
97 * which may be close_flg. When syncing to the end if there
98 * are no flush groups pending we still cycle the flusher, and
99 * must allocate a sequence number to placemark the spot even
100 * though no flush group will ever be associated with it.
103 hammer_flusher_async(hammer_mount_t hmp, hammer_flush_group_t close_flg)
105 hammer_flush_group_t flg;
111 if (close_flg && close_flg->closed)
112 return(close_flg->seq);
115 * Close flush groups until we hit the end of the list
118 while ((flg = hmp->next_flush_group) != NULL) {
119 KKASSERT(flg->closed == 0 && flg->running == 0);
121 hmp->next_flush_group = TAILQ_NEXT(flg, flush_entry);
122 if (flg == close_flg)
126 if (hmp->flusher.td) {
127 if (hmp->flusher.signal++ == 0)
128 wakeup(&hmp->flusher.signal);
132 seq = hmp->flusher.next;
136 seq = hmp->flusher.done;
142 * Flush the current/next flushable flg. This function is typically called
143 * in a loop along with hammer_flusher_wait(hmp, returned_seq) to iterate
144 * flush groups until specific conditions are met.
146 * If a flush is currently in progress its seq is returned.
148 * If no flush is currently in progress the next available flush group
149 * will be flushed and its seq returned.
151 * If no flush groups are present a dummy seq will be allocated and
152 * returned and the flusher will be activated (e.g. to flush the
153 * undo/redo and the volume header).
156 hammer_flusher_async_one(hammer_mount_t hmp)
158 hammer_flush_group_t flg;
161 if (hmp->flusher.td) {
162 flg = TAILQ_FIRST(&hmp->flush_group_list);
163 seq = hammer_flusher_async(hmp, flg);
165 seq = hmp->flusher.done;
171 * Wait for the flusher to finish flushing the specified sequence
172 * number. The flush is already running and will signal us on
176 hammer_flusher_wait(hammer_mount_t hmp, int seq)
178 while ((int)(seq - hmp->flusher.done) > 0)
179 tsleep(&hmp->flusher.done, 0, "hmrfls", 0);
183 hammer_flusher_wait_next(hammer_mount_t hmp)
187 seq = hammer_flusher_async_one(hmp);
188 hammer_flusher_wait(hmp, seq);
192 hammer_flusher_create(hammer_mount_t hmp)
194 hammer_flusher_info_t info;
197 hmp->flusher.signal = 0;
198 hmp->flusher.done = 0;
199 hmp->flusher.next = 1;
200 hammer_ref(&hmp->flusher.finalize_lock);
201 TAILQ_INIT(&hmp->flusher.run_list);
202 TAILQ_INIT(&hmp->flusher.ready_list);
204 lwkt_create(hammer_flusher_master_thread, hmp,
205 &hmp->flusher.td, NULL, 0, -1, "hammer-M");
206 for (i = 0; i < HAMMER_MAX_FLUSHERS; ++i) {
207 info = kmalloc(sizeof(*info), hmp->m_misc, M_WAITOK|M_ZERO);
209 TAILQ_INSERT_TAIL(&hmp->flusher.ready_list, info, entry);
210 lwkt_create(hammer_flusher_slave_thread, info,
211 &info->td, NULL, 0, -1, "hammer-S%d", i);
216 hammer_flusher_destroy(hammer_mount_t hmp)
218 hammer_flusher_info_t info;
223 hmp->flusher.exiting = 1;
224 while (hmp->flusher.td) {
225 ++hmp->flusher.signal;
226 wakeup(&hmp->flusher.signal);
227 tsleep(&hmp->flusher.exiting, 0, "hmrwex", hz);
233 while ((info = TAILQ_FIRST(&hmp->flusher.ready_list)) != NULL) {
234 KKASSERT(info->runstate == 0);
235 TAILQ_REMOVE(&hmp->flusher.ready_list, info, entry);
237 wakeup(&info->runstate);
239 tsleep(&info->td, 0, "hmrwwc", 0);
240 kfree(info, hmp->m_misc);
245 * The master flusher thread manages the flusher sequence id and
246 * synchronization with the slave work threads.
249 hammer_flusher_master_thread(void *arg)
257 lwkt_gettoken(&hmp->fs_token);
261 * Flush all sequence numbers up to but not including .next,
262 * or until an open flush group is encountered.
265 while (hmp->flusher.group_lock)
266 tsleep(&hmp->flusher.group_lock, 0, "hmrhld",0);
267 hammer_flusher_clean_loose_ios(hmp);
269 seq = hammer_flusher_flush(hmp, &nomore);
270 hmp->flusher.done = seq;
271 wakeup(&hmp->flusher.done);
273 if (hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR)
282 if (hmp->flusher.exiting && TAILQ_EMPTY(&hmp->flush_group_list))
284 while (hmp->flusher.signal == 0)
285 tsleep(&hmp->flusher.signal, 0, "hmrwwa", 0);
286 hmp->flusher.signal = 0;
292 hmp->flusher.td = NULL;
293 wakeup(&hmp->flusher.exiting);
294 lwkt_reltoken(&hmp->fs_token);
299 * Flush the next sequence number until an open flush group is encountered
300 * or we reach (next). Not all sequence numbers will have flush groups
301 * associated with them. These require that the UNDO/REDO FIFO still be
302 * flushed since it can take at least one additional run to synchronize
303 * the FIFO, and more to also synchronize the reserve structures.
306 hammer_flusher_flush(hammer_mount_t hmp, int *nomorep)
308 hammer_flusher_info_t info;
309 hammer_flush_group_t flg;
310 hammer_reserve_t resv;
312 hammer_inode_t next_ip;
318 * Just in-case there's a flush race on mount. Seq number
321 if (TAILQ_FIRST(&hmp->flusher.ready_list) == NULL) {
323 return (hmp->flusher.done);
328 * Flush the next sequence number. Sequence numbers can exist
329 * without an assigned flush group, indicating that just a FIFO flush
332 seq = hmp->flusher.done + 1;
333 flg = TAILQ_FIRST(&hmp->flush_group_list);
335 if (seq == hmp->flusher.next) {
337 return (hmp->flusher.done);
339 } else if (seq == flg->seq) {
341 KKASSERT(flg->running == 0);
343 if (hmp->fill_flush_group == flg) {
344 hmp->fill_flush_group =
345 TAILQ_NEXT(flg, flush_entry);
349 return (hmp->flusher.done);
352 KKASSERT((int)(flg->seq - seq) > 0);
357 * We only do one flg but we may have to loop/retry.
359 * Due to various races it is possible to come across a flush
360 * group which as not yet been closed.
363 while (flg && flg->running) {
365 if (hammer_debug_general & 0x0001) {
366 kprintf("hammer_flush %d ttl=%d recs=%d\n",
367 flg->seq, flg->total_count, flg->refs);
369 if (hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR)
371 hammer_start_transaction_fls(&hmp->flusher.trans, hmp);
374 * If the previous flush cycle just about exhausted our
375 * UNDO space we may have to do a dummy cycle to move the
376 * first_offset up before actually digging into a new cycle,
377 * or the new cycle will not have sufficient undo space.
379 if (hammer_flusher_undo_exhausted(&hmp->flusher.trans, 3))
380 hammer_flusher_finalize(&hmp->flusher.trans, 0);
382 KKASSERT(hmp->next_flush_group != flg);
385 * Iterate the inodes in the flg's flush_tree and assign
389 info = TAILQ_FIRST(&hmp->flusher.ready_list);
390 next_ip = RB_FIRST(hammer_fls_rb_tree, &flg->flush_tree);
392 while ((ip = next_ip) != NULL) {
393 next_ip = RB_NEXT(hammer_fls_rb_tree,
394 &flg->flush_tree, ip);
396 if (++hmp->check_yield > hammer_yield_check) {
397 hmp->check_yield = 0;
402 * Add ip to the slave's work array. The slave is
403 * not currently running.
405 info->work_array[info->count++] = ip;
406 if (info->count != HAMMER_FLUSH_GROUP_SIZE)
410 * Get the slave running
412 TAILQ_REMOVE(&hmp->flusher.ready_list, info, entry);
413 TAILQ_INSERT_TAIL(&hmp->flusher.run_list, info, entry);
416 wakeup(&info->runstate);
419 * Get a new slave. We may have to wait for one to
422 while ((info = TAILQ_FIRST(&hmp->flusher.ready_list)) == NULL) {
423 tsleep(&hmp->flusher.ready_list, 0, "hmrfcc", 0);
428 * Run the current slave if necessary
431 TAILQ_REMOVE(&hmp->flusher.ready_list, info, entry);
432 TAILQ_INSERT_TAIL(&hmp->flusher.run_list, info, entry);
435 wakeup(&info->runstate);
439 * Wait for all slaves to finish running
441 while (TAILQ_FIRST(&hmp->flusher.run_list) != NULL)
442 tsleep(&hmp->flusher.ready_list, 0, "hmrfcc", 0);
445 * Do the final finalization, clean up
447 hammer_flusher_finalize(&hmp->flusher.trans, 1);
448 hmp->flusher.tid = hmp->flusher.trans.tid;
450 hammer_done_transaction(&hmp->flusher.trans);
453 * Loop up on the same flg. If the flg is done clean it up
454 * and break out. We only flush one flg.
456 if (RB_EMPTY(&flg->flush_tree)) {
457 KKASSERT(flg->refs == 0);
458 TAILQ_REMOVE(&hmp->flush_group_list, flg, flush_entry);
459 kfree(flg, hmp->m_misc);
462 KKASSERT(TAILQ_FIRST(&hmp->flush_group_list) == flg);
466 * We may have pure meta-data to flush, or we may have to finish
467 * cycling the UNDO FIFO, even if there were no flush groups.
469 if (count == 0 && hammer_flusher_haswork(hmp)) {
470 hammer_start_transaction_fls(&hmp->flusher.trans, hmp);
471 hammer_flusher_finalize(&hmp->flusher.trans, 1);
472 hammer_done_transaction(&hmp->flusher.trans);
476 * Clean up any freed big-blocks (typically zone-2).
477 * resv->flush_group is typically set several flush groups ahead
478 * of the free to ensure that the freed block is not reused until
479 * it can no longer be reused.
481 while ((resv = TAILQ_FIRST(&hmp->delay_list)) != NULL) {
482 if ((int)(resv->flush_group - seq) > 0)
484 hammer_reserve_clrdelay(hmp, resv);
491 * The slave flusher thread pulls work off the master flush list until no
495 hammer_flusher_slave_thread(void *arg)
497 hammer_flush_group_t flg;
498 hammer_flusher_info_t info;
505 lwkt_gettoken(&hmp->fs_token);
508 while (info->runstate == 0)
509 tsleep(&info->runstate, 0, "hmrssw", 0);
510 if (info->runstate < 0)
514 for (i = 0; i < info->count; ++i) {
515 ip = info->work_array[i];
516 hammer_flusher_flush_inode(ip, &hmp->flusher.trans);
517 ++hammer_stats_inode_flushes;
521 TAILQ_REMOVE(&hmp->flusher.run_list, info, entry);
522 TAILQ_INSERT_TAIL(&hmp->flusher.ready_list, info, entry);
523 wakeup(&hmp->flusher.ready_list);
527 lwkt_reltoken(&hmp->fs_token);
532 hammer_flusher_clean_loose_ios(hammer_mount_t hmp)
534 hammer_buffer_t buffer;
538 * loose ends - buffers without bp's aren't tracked by the kernel
539 * and can build up, so clean them out. This can occur when an
540 * IO completes on a buffer with no references left.
542 * The io_token is needed to protect the list.
544 if ((io = RB_ROOT(&hmp->lose_root)) != NULL) {
545 lwkt_gettoken(&hmp->io_token);
546 while ((io = RB_ROOT(&hmp->lose_root)) != NULL) {
547 KKASSERT(io->mod_root == &hmp->lose_root);
548 RB_REMOVE(hammer_mod_rb_tree, io->mod_root, io);
550 hammer_ref(&io->lock);
552 hammer_rel_buffer(buffer, 0);
554 lwkt_reltoken(&hmp->io_token);
559 * Flush a single inode that is part of a flush group.
561 * Flusher errors are extremely serious, even ENOSPC shouldn't occur because
562 * the front-end should have reserved sufficient space on the media. Any
563 * error other then EWOULDBLOCK will force the mount to be read-only.
567 hammer_flusher_flush_inode(hammer_inode_t ip, hammer_transaction_t trans)
569 hammer_mount_t hmp = ip->hmp;
572 hammer_flusher_clean_loose_ios(hmp);
573 error = hammer_sync_inode(trans, ip);
576 * EWOULDBLOCK can happen under normal operation, all other errors
577 * are considered extremely serious. We must set WOULDBLOCK
578 * mechanics to deal with the mess left over from the abort of the
582 ip->flags |= HAMMER_INODE_WOULDBLOCK;
583 if (error == EWOULDBLOCK)
586 hammer_flush_inode_done(ip, error);
587 while (hmp->flusher.finalize_want)
588 tsleep(&hmp->flusher.finalize_want, 0, "hmrsxx", 0);
589 if (hammer_flusher_undo_exhausted(trans, 1)) {
590 kprintf("HAMMER: Warning: UNDO area too small!\n");
591 hammer_flusher_finalize(trans, 1);
592 } else if (hammer_flusher_meta_limit(trans->hmp)) {
593 hammer_flusher_finalize(trans, 0);
598 * Return non-zero if the UNDO area has less then (QUARTER / 4) of its
601 * 1/4 - Emergency free undo space level. Below this point the flusher
602 * will finalize even if directory dependancies have not been resolved.
604 * 2/4 - Used by the pruning and reblocking code. These functions may be
605 * running in parallel with a flush and cannot be allowed to drop
606 * available undo space to emergency levels.
608 * 3/4 - Used at the beginning of a flush to force-sync the volume header
609 * to give the flush plenty of runway to work in.
612 hammer_flusher_undo_exhausted(hammer_transaction_t trans, int quarter)
614 if (hammer_undo_space(trans) <
615 hammer_undo_max(trans->hmp) * quarter / 4) {
623 * Flush all pending UNDOs, wait for write completion, update the volume
624 * header with the new UNDO end position, and flush it. Then
625 * asynchronously flush the meta-data.
627 * If this is the last finalization in a flush group we also synchronize
628 * our cached blockmap and set hmp->flusher_undo_start and our cached undo
629 * fifo first_offset so the next flush resets the FIFO pointers.
631 * If this is not final it is being called because too many dirty meta-data
632 * buffers have built up and must be flushed with UNDO synchronization to
633 * avoid a buffer cache deadlock.
636 hammer_flusher_finalize(hammer_transaction_t trans, int final)
638 hammer_volume_t root_volume;
639 hammer_blockmap_t cundomap, dundomap;
642 hammer_off_t save_undo_next_offset;
647 root_volume = trans->rootvol;
650 * Exclusively lock the flusher. This guarantees that all dirty
651 * buffers will be idled (have a mod-count of 0).
653 ++hmp->flusher.finalize_want;
654 hammer_lock_ex(&hmp->flusher.finalize_lock);
657 * If this isn't the final sync several threads may have hit the
658 * meta-limit at the same time and raced. Only sync if we really
659 * have to, after acquiring the lock.
661 if (final == 0 && !hammer_flusher_meta_limit(hmp))
664 if (hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR)
668 * Flush data buffers. This can occur asynchronously and at any
669 * time. We must interlock against the frontend direct-data write
670 * but do not have to acquire the sync-lock yet.
672 * These data buffers have already been collected prior to the
673 * related inode(s) getting queued to the flush group.
676 while ((io = RB_FIRST(hammer_mod_rb_tree, &hmp->data_root)) != NULL) {
679 hammer_ref(&io->lock);
680 hammer_io_write_interlock(io);
681 KKASSERT(io->type != HAMMER_STRUCTURE_VOLUME);
682 hammer_io_flush(io, 0);
683 hammer_io_done_interlock(io);
684 hammer_rel_buffer((hammer_buffer_t)io, 0);
685 hammer_io_limit_backlog(hmp);
690 * The sync-lock is required for the remaining sequence. This lock
691 * prevents meta-data from being modified.
693 hammer_sync_lock_ex(trans);
696 * If we have been asked to finalize the volume header sync the
697 * cached blockmap to the on-disk blockmap. Generate an UNDO
698 * record for the update.
701 cundomap = &hmp->blockmap[0];
702 dundomap = &root_volume->ondisk->vol0_blockmap[0];
703 if (root_volume->io.modified) {
704 hammer_modify_volume(trans, root_volume,
705 dundomap, sizeof(hmp->blockmap));
706 for (i = 0; i < HAMMER_MAX_ZONES; ++i)
707 hammer_crc_set_blockmap(&cundomap[i]);
708 bcopy(cundomap, dundomap, sizeof(hmp->blockmap));
709 hammer_modify_volume_done(root_volume);
714 * Flush UNDOs. This can occur concurrently with the data flush
715 * because data writes never overwrite.
717 * This also waits for I/Os to complete and flushes the cache on
720 * Record the UNDO append point as this can continue to change
721 * after we have flushed the UNDOs.
723 cundomap = &hmp->blockmap[HAMMER_ZONE_UNDO_INDEX];
724 hammer_lock_ex(&hmp->undo_lock);
725 save_undo_next_offset = cundomap->next_offset;
726 hammer_unlock(&hmp->undo_lock);
727 hammer_flusher_flush_undos(hmp, HAMMER_FLUSH_UNDOS_FORCED);
729 if (hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR)
733 * HAMMER VERSION < 4:
734 * Update the on-disk volume header with new UNDO FIFO end
735 * position (do not generate new UNDO records for this change).
736 * We have to do this for the UNDO FIFO whether (final) is
737 * set or not in order for the UNDOs to be recognized on
740 * HAMMER VERSION >= 4:
741 * The UNDO FIFO data written above will be recognized on
742 * recovery without us having to sync the volume header.
744 * Also update the on-disk next_tid field. This does not require
745 * an UNDO. However, because our TID is generated before we get
746 * the sync lock another sync may have beat us to the punch.
748 * This also has the side effect of updating first_offset based on
749 * a prior finalization when the first finalization of the next flush
750 * cycle occurs, removing any undo info from the prior finalization
751 * from consideration.
753 * The volume header will be flushed out synchronously.
755 dundomap = &root_volume->ondisk->vol0_blockmap[HAMMER_ZONE_UNDO_INDEX];
756 cundomap = &hmp->blockmap[HAMMER_ZONE_UNDO_INDEX];
758 if (dundomap->first_offset != cundomap->first_offset ||
759 dundomap->next_offset != save_undo_next_offset) {
760 hammer_modify_volume(NULL, root_volume, NULL, 0);
761 dundomap->first_offset = cundomap->first_offset;
762 dundomap->next_offset = save_undo_next_offset;
763 hammer_crc_set_blockmap(dundomap);
764 hammer_modify_volume_done(root_volume);
768 * vol0_next_tid is used for TID selection and is updated without
769 * an UNDO so we do not reuse a TID that may have been rolled-back.
771 * vol0_last_tid is the highest fully-synchronized TID. It is
772 * set-up when the UNDO fifo is fully synced, later on (not here).
774 * The root volume can be open for modification by other threads
775 * generating UNDO or REDO records. For example, reblocking,
776 * pruning, REDO mode fast-fsyncs, so the write interlock is
779 if (root_volume->io.modified) {
780 hammer_modify_volume(NULL, root_volume, NULL, 0);
781 if (root_volume->ondisk->vol0_next_tid < trans->tid)
782 root_volume->ondisk->vol0_next_tid = trans->tid;
783 hammer_crc_set_volume(root_volume->ondisk);
784 hammer_modify_volume_done(root_volume);
785 hammer_io_write_interlock(&root_volume->io);
786 hammer_io_flush(&root_volume->io, 0);
787 hammer_io_done_interlock(&root_volume->io);
791 * Wait for I/Os to complete.
793 * For HAMMER VERSION 4+ filesystems we do not have to wait for
794 * the I/O to complete as the new UNDO FIFO entries are recognized
795 * even without the volume header update. This allows the volume
796 * header to flushed along with meta-data, significantly reducing
799 hammer_flusher_clean_loose_ios(hmp);
800 if (hmp->version < HAMMER_VOL_VERSION_FOUR)
801 hammer_io_wait_all(hmp, "hmrfl3", 1);
803 if (hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR)
807 * Flush meta-data. The meta-data will be undone if we crash
808 * so we can safely flush it asynchronously. There is no need
809 * to wait for I/O to complete (or issue a synchronous disk flush).
811 * In fact, even if we did wait the meta-data will still be undone
812 * by a crash up until the next flush cycle due to the first_offset
813 * in the volume header for the UNDO FIFO not being adjusted until
814 * the following flush cycle.
816 * No io interlock is needed, bioops callbacks will not mess with
820 while ((io = RB_FIRST(hammer_mod_rb_tree, &hmp->meta_root)) != NULL) {
823 KKASSERT(io->modify_refs == 0);
824 hammer_ref(&io->lock);
825 KKASSERT(io->type != HAMMER_STRUCTURE_VOLUME);
826 hammer_io_flush(io, 0);
827 hammer_rel_buffer((hammer_buffer_t)io, 0);
828 hammer_io_limit_backlog(hmp);
833 * If this is the final finalization for the flush group set
834 * up for the next sequence by setting a new first_offset in
835 * our cached blockmap and clearing the undo history.
837 * Even though we have updated our cached first_offset, the on-disk
838 * first_offset still governs available-undo-space calculations.
840 * We synchronize to save_undo_next_offset rather than
841 * cundomap->next_offset because that is what we flushed out
844 * NOTE! UNDOs can only be added with the sync_lock held
845 * so we can clear the undo history without racing.
846 * REDOs can be added at any time which is why we
847 * have to be careful and use save_undo_next_offset
848 * when setting the new first_offset.
851 cundomap = &hmp->blockmap[HAMMER_ZONE_UNDO_INDEX];
852 if (cundomap->first_offset != save_undo_next_offset) {
853 cundomap->first_offset = save_undo_next_offset;
854 hmp->hflags |= HMNT_UNDO_DIRTY;
855 } else if (cundomap->first_offset != cundomap->next_offset) {
856 hmp->hflags |= HMNT_UNDO_DIRTY;
858 hmp->hflags &= ~HMNT_UNDO_DIRTY;
860 hammer_clear_undo_history(hmp);
863 * Flush tid sequencing. flush_tid1 is fully synchronized,
864 * meaning a crash will not roll it back. flush_tid2 has
865 * been written out asynchronously and a crash will roll
866 * it back. flush_tid1 is used for all mirroring masters.
868 if (hmp->flush_tid1 != hmp->flush_tid2) {
869 hmp->flush_tid1 = hmp->flush_tid2;
870 wakeup(&hmp->flush_tid1);
872 hmp->flush_tid2 = trans->tid;
875 * Clear the REDO SYNC flag. This flag is used to ensure
876 * that the recovery span in the UNDO/REDO FIFO contains
877 * at least one REDO SYNC record.
879 hmp->flags &= ~HAMMER_MOUNT_REDO_SYNC;
883 * Cleanup. Report any critical errors.
886 hammer_sync_unlock(trans);
888 if (hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR) {
889 kprintf("HAMMER(%s): Critical write error during flush, "
890 "refusing to sync UNDO FIFO\n",
891 root_volume->ondisk->vol_name);
895 hammer_unlock(&hmp->flusher.finalize_lock);
897 if (--hmp->flusher.finalize_want == 0)
898 wakeup(&hmp->flusher.finalize_want);
899 hammer_stats_commits += final;
906 hammer_flusher_flush_undos(hammer_mount_t hmp, int mode)
912 while ((io = RB_FIRST(hammer_mod_rb_tree, &hmp->undo_root)) != NULL) {
915 hammer_ref(&io->lock);
916 KKASSERT(io->type != HAMMER_STRUCTURE_VOLUME);
917 hammer_io_write_interlock(io);
918 hammer_io_flush(io, hammer_undo_reclaim(io));
919 hammer_io_done_interlock(io);
920 hammer_rel_buffer((hammer_buffer_t)io, 0);
921 hammer_io_limit_backlog(hmp);
924 hammer_flusher_clean_loose_ios(hmp);
925 if (mode == HAMMER_FLUSH_UNDOS_FORCED ||
926 (mode == HAMMER_FLUSH_UNDOS_AUTO && count)) {
927 hammer_io_wait_all(hmp, "hmrfl1", 1);
929 hammer_io_wait_all(hmp, "hmrfl2", 0);
934 * Return non-zero if too many dirty meta-data buffers have built up.
936 * Since we cannot allow such buffers to flush until we have dealt with
937 * the UNDOs, we risk deadlocking the kernel's buffer cache.
940 hammer_flusher_meta_limit(hammer_mount_t hmp)
942 if (hmp->locked_dirty_space + hmp->io_running_space >
943 hammer_limit_dirtybufspace) {
950 * Return non-zero if too many dirty meta-data buffers have built up.
952 * This version is used by background operations (mirror, prune, reblock)
953 * to leave room for foreground operations.
956 hammer_flusher_meta_halflimit(hammer_mount_t hmp)
958 if (hmp->locked_dirty_space + hmp->io_running_space >
959 hammer_limit_dirtybufspace / 2) {
966 * Return non-zero if the flusher still has something to flush.
969 hammer_flusher_haswork(hammer_mount_t hmp)
973 if (hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR)
975 if (TAILQ_FIRST(&hmp->flush_group_list) || /* dirty inodes */
976 RB_ROOT(&hmp->volu_root) || /* dirty buffers */
977 RB_ROOT(&hmp->undo_root) ||
978 RB_ROOT(&hmp->data_root) ||
979 RB_ROOT(&hmp->meta_root) ||
980 (hmp->hflags & HMNT_UNDO_DIRTY) /* UNDO FIFO sync */