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.9 2008/05/02 01:00:42 dillon Exp $
37 * HAMMER dependancy flusher thread
39 * Meta data updates create buffer dependancies which are arranged as a
45 static void hammer_flusher_thread(void *arg);
46 static void hammer_flusher_clean_loose_ios(hammer_mount_t hmp);
47 static void hammer_flusher_flush(hammer_mount_t hmp);
48 static int hammer_must_finalize_undo(hammer_mount_t hmp);
49 static void hammer_flusher_finalize(hammer_mount_t hmp,
50 hammer_volume_t root_volume, hammer_off_t start_offset);
53 hammer_flusher_sync(hammer_mount_t hmp)
57 if (hmp->flusher_td) {
58 seq = hmp->flusher_next;
59 if (hmp->flusher_signal == 0) {
60 hmp->flusher_signal = 1;
61 wakeup(&hmp->flusher_signal);
63 while ((int)(seq - hmp->flusher_done) > 0)
64 tsleep(&hmp->flusher_done, 0, "hmrfls", 0);
69 hammer_flusher_async(hammer_mount_t hmp)
71 if (hmp->flusher_td) {
72 if (hmp->flusher_signal == 0) {
73 hmp->flusher_signal = 1;
74 wakeup(&hmp->flusher_signal);
80 hammer_flusher_create(hammer_mount_t hmp)
82 hmp->flusher_signal = 0;
84 hmp->flusher_done = 0;
85 hmp->flusher_next = 1;
86 lwkt_create(hammer_flusher_thread, hmp, &hmp->flusher_td, NULL,
91 hammer_flusher_destroy(hammer_mount_t hmp)
93 if (hmp->flusher_td) {
94 hmp->flusher_exiting = 1;
95 while (hmp->flusher_td) {
96 hmp->flusher_signal = 1;
97 wakeup(&hmp->flusher_signal);
98 tsleep(&hmp->flusher_exiting, 0, "hmrwex", 0);
104 hammer_flusher_thread(void *arg)
106 hammer_mount_t hmp = arg;
109 hmp->flusher_act = hmp->flusher_next;
112 hammer_flusher_clean_loose_ios(hmp);
113 hammer_flusher_flush(hmp);
114 hammer_flusher_clean_loose_ios(hmp);
115 hmp->flusher_done = hmp->flusher_act;
117 wakeup(&hmp->flusher_done);
122 if (hmp->flusher_exiting && TAILQ_EMPTY(&hmp->flush_list))
126 while (hmp->flusher_signal == 0 &&
127 TAILQ_EMPTY(&hmp->flush_list)) {
128 tsleep(&hmp->flusher_signal, 0, "hmrwwa", 0);
130 hmp->flusher_signal = 0;
132 hmp->flusher_td = NULL;
133 wakeup(&hmp->flusher_exiting);
138 hammer_flusher_clean_loose_ios(hammer_mount_t hmp)
140 hammer_buffer_t buffer;
144 * loose ends - buffers without bp's aren't tracked by the kernel
145 * and can build up, so clean them out. This can occur when an
146 * IO completes on a buffer with no references left.
148 while ((io = TAILQ_FIRST(&hmp->lose_list)) != NULL) {
149 KKASSERT(io->mod_list == &hmp->lose_list);
150 TAILQ_REMOVE(io->mod_list, io, mod_entry);
152 hammer_ref(&io->lock);
154 hammer_rel_buffer(buffer, 0);
162 hammer_flusher_flush(hammer_mount_t hmp)
164 hammer_volume_t root_volume;
165 hammer_blockmap_t rootmap;
167 hammer_off_t start_offset;
170 root_volume = hammer_get_root_volume(hmp, &error);
171 rootmap = &hmp->blockmap[HAMMER_ZONE_UNDO_INDEX];
172 start_offset = rootmap->next_offset;
174 while ((ip = TAILQ_FIRST(&hmp->flush_list)) != NULL) {
176 * Stop when we hit a different flush group
178 if (ip->flush_group != hmp->flusher_act)
182 * Remove the inode from the flush list and inherit
183 * its reference, sync, and clean-up.
185 TAILQ_REMOVE(&hmp->flush_list, ip, flush_entry);
187 ip->error = hammer_sync_inode(ip);
188 hammer_flush_inode_done(ip);
191 * XXX this breaks atomicy
193 if (hammer_must_finalize_undo(hmp)) {
194 Debugger("Too many undos!!");
195 hammer_flusher_finalize(hmp, root_volume, start_offset);
196 start_offset = rootmap->next_offset;
199 hammer_flusher_finalize(hmp, root_volume, start_offset);
200 hammer_rel_volume(root_volume, 0);
204 * If the UNDO area gets over half full we have to flush it. We can't
205 * afford the UNDO area becoming completely full as that would break
206 * the crash recovery atomicy.
210 hammer_must_finalize_undo(hammer_mount_t hmp)
212 if (hammer_undo_space(hmp) < hammer_undo_max(hmp) / 2) {
221 * To finalize the flush we finish flushing all undo and data buffers
222 * still present, then we update the volume header and flush it,
223 * then we flush out the mata-data (that can now be undone).
225 * Note that as long as the undo fifo's start and end points do not
226 * match, we always must at least update the volume header.
228 * The sync_lock is used by other threads to issue modifying operations
229 * to HAMMER media without crossing a synchronization boundary or messing
230 * up the media synchronization operation. Specifically, the pruning
231 * the reblocking ioctls, and allowing the frontend strategy code to
232 * allocate media data space.
236 hammer_flusher_finalize(hammer_mount_t hmp, hammer_volume_t root_volume,
237 hammer_off_t start_offset)
239 hammer_blockmap_t rootmap;
242 hammer_lock_ex(&hmp->sync_lock);
247 while ((io = TAILQ_FIRST(&hmp->undo_list)) != NULL) {
248 KKASSERT(io->modify_refs == 0);
249 hammer_ref(&io->lock);
250 KKASSERT(io->type != HAMMER_STRUCTURE_VOLUME);
252 hammer_rel_buffer((hammer_buffer_t)io, 1);
258 while ((io = TAILQ_FIRST(&hmp->data_list)) != NULL) {
259 KKASSERT(io->modify_refs == 0);
260 hammer_ref(&io->lock);
261 KKASSERT(io->type != HAMMER_STRUCTURE_VOLUME);
263 hammer_rel_buffer((hammer_buffer_t)io, 1);
267 * Wait for I/O to complete
270 while (hmp->io_running_count) {
271 kprintf("W[%d]", hmp->io_running_count);
272 tsleep(&hmp->io_running_count, 0, "hmrfl1", 0);
277 * Update the volume header
279 rootmap = &hmp->blockmap[HAMMER_ZONE_UNDO_INDEX];
280 if (rootmap->first_offset != start_offset) {
281 hammer_modify_volume(NULL, root_volume, NULL, 0);
282 rootmap->first_offset = start_offset;
283 hammer_modify_volume_done(root_volume);
285 if (root_volume->ondisk->vol0_next_tid != hmp->next_tid) {
286 hammer_modify_volume(NULL, root_volume, NULL, 0);
287 root_volume->ondisk->vol0_next_tid = hmp->next_tid;
288 hammer_modify_volume_done(root_volume);
292 * Sync our cached blockmap array with the one in the root
295 if (root_volume->io.modified) {
296 bcopy(hmp->blockmap, root_volume->ondisk->vol0_blockmap,
297 sizeof(hmp->blockmap));
298 hammer_io_flush(&root_volume->io);
302 * Wait for I/O to complete
305 while (hmp->io_running_count) {
306 tsleep(&hmp->io_running_count, 0, "hmrfl2", 0);
313 while ((io = TAILQ_FIRST(&hmp->meta_list)) != NULL) {
314 KKASSERT(io->modify_refs == 0);
315 hammer_ref(&io->lock);
316 KKASSERT(io->type != HAMMER_STRUCTURE_VOLUME);
318 hammer_rel_buffer((hammer_buffer_t)io, 1);
320 hammer_unlock(&hmp->sync_lock);