| Commit | Line | Data |
|---|---|---|
| 059819e3 MD |
1 | /* |
| 2 | * Copyright (c) 2008 The DragonFly Project. All rights reserved. | |
| 3 | * | |
| 4 | * This code is derived from software contributed to The DragonFly Project | |
| 5 | * by Matthew Dillon <dillon@backplane.com> | |
| 6 | * | |
| 7 | * Redistribution and use in source and binary forms, with or without | |
| 8 | * modification, are permitted provided that the following conditions | |
| 9 | * are met: | |
| 10 | * | |
| 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 | |
| 16 | * distribution. | |
| 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. | |
| 20 | * | |
| 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 | |
| 32 | * SUCH DAMAGE. | |
| 33 | * | |
| 4889cbd4 | 34 | * $DragonFly: src/sys/vfs/hammer/hammer_flusher.c,v 1.45 2008/07/31 04:42:04 dillon Exp $ |
| 059819e3 MD |
35 | */ |
| 36 | /* | |
| 37 | * HAMMER dependancy flusher thread | |
| 38 | * | |
| 39 | * Meta data updates create buffer dependancies which are arranged as a | |
| 40 | * hierarchy of lists. | |
| 41 | */ | |
| 42 | ||
| 43 | #include "hammer.h" | |
| 44 | ||
| da2da375 MD |
45 | static void hammer_flusher_master_thread(void *arg); |
| 46 | static void hammer_flusher_slave_thread(void *arg); | |
| 059819e3 | 47 | static void hammer_flusher_flush(hammer_mount_t hmp); |
| 9f5097dc MD |
48 | static void hammer_flusher_flush_inode(hammer_inode_t ip, |
| 49 | hammer_transaction_t trans); | |
| c9b9e29d | 50 | |
| ff003b11 MD |
51 | RB_GENERATE(hammer_fls_rb_tree, hammer_inode, rb_flsnode, |
| 52 | hammer_ino_rb_compare); | |
| 53 | ||
| 54 | /* | |
| 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. | |
| 59 | */ | |
| 60 | #define HAMMER_FLUSH_GROUP_SIZE 128 | |
| 61 | ||
| af209b0f MD |
62 | /* |
| 63 | * Support structures for the flusher threads. | |
| 64 | */ | |
| 65 | struct hammer_flusher_info { | |
| 7a61b85d | 66 | TAILQ_ENTRY(hammer_flusher_info) entry; |
| af209b0f MD |
67 | struct hammer_mount *hmp; |
| 68 | thread_t td; | |
| 7a61b85d MD |
69 | int runstate; |
| 70 | int count; | |
| 71 | hammer_flush_group_t flg; | |
| cb51be26 | 72 | hammer_inode_t work_array[HAMMER_FLUSH_GROUP_SIZE]; |
| af209b0f MD |
73 | }; |
| 74 | ||
| 75 | typedef struct hammer_flusher_info *hammer_flusher_info_t; | |
| 059819e3 | 76 | |
| 7bc5b8c2 | 77 | /* |
| 7a61b85d MD |
78 | * Sync all inodes pending on the flusher. |
| 79 | * | |
| 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! | |
| 7bc5b8c2 | 82 | */ |
| 059819e3 MD |
83 | void |
| 84 | hammer_flusher_sync(hammer_mount_t hmp) | |
| 85 | { | |
| 86 | int seq; | |
| 87 | ||
| 7a61b85d | 88 | seq = hammer_flusher_async(hmp, NULL); |
| f437a2ab | 89 | hammer_flusher_wait(hmp, seq); |
| 059819e3 MD |
90 | } |
| 91 | ||
| 7bc5b8c2 MD |
92 | /* |
| 93 | * Sync all inodes pending on the flusher - return immediately. | |
| 7a61b85d MD |
94 | * |
| 95 | * All flush groups will be flushed. | |
| 7bc5b8c2 | 96 | */ |
| 93291532 | 97 | int |
| 7a61b85d | 98 | hammer_flusher_async(hammer_mount_t hmp, hammer_flush_group_t close_flg) |
| 059819e3 | 99 | { |
| 7a61b85d MD |
100 | hammer_flush_group_t flg; |
| 101 | int seq = hmp->flusher.next; | |
| 102 | ||
| 103 | TAILQ_FOREACH(flg, &hmp->flush_group_list, flush_entry) { | |
| 104 | if (flg->running == 0) | |
| 105 | ++seq; | |
| 106 | flg->closed = 1; | |
| 107 | if (flg == close_flg) | |
| 108 | break; | |
| 109 | } | |
| da2da375 MD |
110 | if (hmp->flusher.td) { |
| 111 | if (hmp->flusher.signal++ == 0) | |
| 112 | wakeup(&hmp->flusher.signal); | |
| 93291532 MD |
113 | } else { |
| 114 | seq = hmp->flusher.done; | |
| f90dde4c | 115 | } |
| 93291532 MD |
116 | return(seq); |
| 117 | } | |
| 118 | ||
| 15e75dab MD |
119 | int |
| 120 | hammer_flusher_async_one(hammer_mount_t hmp) | |
| 121 | { | |
| 122 | int seq; | |
| 123 | ||
| 124 | if (hmp->flusher.td) { | |
| 125 | seq = hmp->flusher.next; | |
| 126 | if (hmp->flusher.signal++ == 0) | |
| 127 | wakeup(&hmp->flusher.signal); | |
| 128 | } else { | |
| 129 | seq = hmp->flusher.done; | |
| 130 | } | |
| 131 | return(seq); | |
| 132 | } | |
| 133 | ||
| f437a2ab MD |
134 | /* |
| 135 | * Wait for the flusher to get to the specified sequence number. | |
| 136 | * Signal the flusher as often as necessary to keep it going. | |
| 137 | */ | |
| 93291532 MD |
138 | void |
| 139 | hammer_flusher_wait(hammer_mount_t hmp, int seq) | |
| 140 | { | |
| cdb6e4e6 | 141 | while ((int)(seq - hmp->flusher.done) > 0) { |
| f437a2ab MD |
142 | if (hmp->flusher.act != seq) { |
| 143 | if (hmp->flusher.signal++ == 0) | |
| 144 | wakeup(&hmp->flusher.signal); | |
| 145 | } | |
| 93291532 | 146 | tsleep(&hmp->flusher.done, 0, "hmrfls", 0); |
| cdb6e4e6 | 147 | } |
| 059819e3 MD |
148 | } |
| 149 | ||
| 150 | void | |
| 82010f9f MD |
151 | hammer_flusher_wait_next(hammer_mount_t hmp) |
| 152 | { | |
| 153 | int seq; | |
| 154 | ||
| 155 | seq = hammer_flusher_async_one(hmp); | |
| 156 | hammer_flusher_wait(hmp, seq); | |
| 157 | } | |
| 158 | ||
| 159 | void | |
| 059819e3 MD |
160 | hammer_flusher_create(hammer_mount_t hmp) |
| 161 | { | |
| da2da375 MD |
162 | hammer_flusher_info_t info; |
| 163 | int i; | |
| 164 | ||
| 165 | hmp->flusher.signal = 0; | |
| 166 | hmp->flusher.act = 0; | |
| 167 | hmp->flusher.done = 0; | |
| 168 | hmp->flusher.next = 1; | |
| da2da375 | 169 | hammer_ref(&hmp->flusher.finalize_lock); |
| 7a61b85d MD |
170 | TAILQ_INIT(&hmp->flusher.run_list); |
| 171 | TAILQ_INIT(&hmp->flusher.ready_list); | |
| da2da375 MD |
172 | |
| 173 | lwkt_create(hammer_flusher_master_thread, hmp, | |
| 174 | &hmp->flusher.td, NULL, 0, -1, "hammer-M"); | |
| 175 | for (i = 0; i < HAMMER_MAX_FLUSHERS; ++i) { | |
| bac808fe | 176 | info = kmalloc(sizeof(*info), hmp->m_misc, M_WAITOK|M_ZERO); |
| da2da375 | 177 | info->hmp = hmp; |
| 7a61b85d | 178 | TAILQ_INSERT_TAIL(&hmp->flusher.ready_list, info, entry); |
| da2da375 MD |
179 | lwkt_create(hammer_flusher_slave_thread, info, |
| 180 | &info->td, NULL, 0, -1, "hammer-S%d", i); | |
| 181 | } | |
| 059819e3 MD |
182 | } |
| 183 | ||
| 184 | void | |
| 185 | hammer_flusher_destroy(hammer_mount_t hmp) | |
| 186 | { | |
| da2da375 | 187 | hammer_flusher_info_t info; |
| da2da375 MD |
188 | |
| 189 | /* | |
| 190 | * Kill the master | |
| 191 | */ | |
| 192 | hmp->flusher.exiting = 1; | |
| 193 | while (hmp->flusher.td) { | |
| 194 | ++hmp->flusher.signal; | |
| 195 | wakeup(&hmp->flusher.signal); | |
| 196 | tsleep(&hmp->flusher.exiting, 0, "hmrwex", hz); | |
| 197 | } | |
| 198 | ||
| 199 | /* | |
| 200 | * Kill the slaves | |
| 201 | */ | |
| 7a61b85d MD |
202 | while ((info = TAILQ_FIRST(&hmp->flusher.ready_list)) != NULL) { |
| 203 | KKASSERT(info->runstate == 0); | |
| 204 | TAILQ_REMOVE(&hmp->flusher.ready_list, info, entry); | |
| 205 | info->runstate = -1; | |
| 206 | wakeup(&info->runstate); | |
| 207 | while (info->td) | |
| 208 | tsleep(&info->td, 0, "hmrwwc", 0); | |
| bac808fe | 209 | kfree(info, hmp->m_misc); |
| f90dde4c | 210 | } |
| 059819e3 MD |
211 | } |
| 212 | ||
| af209b0f MD |
213 | /* |
| 214 | * The master flusher thread manages the flusher sequence id and | |
| 215 | * synchronization with the slave work threads. | |
| 216 | */ | |
| 059819e3 | 217 | static void |
| da2da375 | 218 | hammer_flusher_master_thread(void *arg) |
| 059819e3 | 219 | { |
| 7a61b85d MD |
220 | hammer_flush_group_t flg; |
| 221 | hammer_mount_t hmp; | |
| 0729c8c8 | 222 | |
| 7a61b85d | 223 | hmp = arg; |
| c32a6806 | 224 | |
| 7a61b85d | 225 | for (;;) { |
| c32a6806 | 226 | /* |
| 7a61b85d MD |
227 | * Do at least one flush cycle. We may have to update the |
| 228 | * UNDO FIFO even if no inodes are queued. | |
| c32a6806 | 229 | */ |
| 7a61b85d MD |
230 | for (;;) { |
| 231 | while (hmp->flusher.group_lock) | |
| 232 | tsleep(&hmp->flusher.group_lock, 0, "hmrhld", 0); | |
| 233 | hmp->flusher.act = hmp->flusher.next; | |
| 234 | ++hmp->flusher.next; | |
| 235 | hammer_flusher_clean_loose_ios(hmp); | |
| 236 | hammer_flusher_flush(hmp); | |
| 237 | hmp->flusher.done = hmp->flusher.act; | |
| 238 | wakeup(&hmp->flusher.done); | |
| 239 | flg = TAILQ_FIRST(&hmp->flush_group_list); | |
| 240 | if (flg == NULL || flg->closed == 0) | |
| 241 | break; | |
| cdb6e4e6 MD |
242 | if (hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR) |
| 243 | break; | |
| 7a61b85d | 244 | } |
| 1f07f686 | 245 | |
| c9b9e29d | 246 | /* |
| 7a61b85d | 247 | * Wait for activity. |
| c9b9e29d | 248 | */ |
| 7a61b85d MD |
249 | if (hmp->flusher.exiting && TAILQ_EMPTY(&hmp->flush_group_list)) |
| 250 | break; | |
| da2da375 MD |
251 | while (hmp->flusher.signal == 0) |
| 252 | tsleep(&hmp->flusher.signal, 0, "hmrwwa", 0); | |
| 4889cbd4 MD |
253 | |
| 254 | /* | |
| 255 | * Flush for each count on signal but only allow one extra | |
| 256 | * flush request to build up. | |
| 257 | */ | |
| 258 | if (--hmp->flusher.signal != 0) | |
| 259 | hmp->flusher.signal = 1; | |
| 059819e3 | 260 | } |
| da2da375 MD |
261 | |
| 262 | /* | |
| 263 | * And we are done. | |
| 264 | */ | |
| 265 | hmp->flusher.td = NULL; | |
| 266 | wakeup(&hmp->flusher.exiting); | |
| 267 | lwkt_exit(); | |
| 268 | } | |
| 269 | ||
| af209b0f | 270 | /* |
| 7a61b85d MD |
271 | * Flush all inodes in the current flush group. |
| 272 | */ | |
| 273 | static void | |
| 274 | hammer_flusher_flush(hammer_mount_t hmp) | |
| 275 | { | |
| 276 | hammer_flusher_info_t info; | |
| 277 | hammer_flush_group_t flg; | |
| 278 | hammer_reserve_t resv; | |
| 279 | hammer_inode_t ip; | |
| 280 | hammer_inode_t next_ip; | |
| 281 | int slave_index; | |
| 15e75dab | 282 | int count; |
| 7a61b85d MD |
283 | |
| 284 | /* | |
| 285 | * Just in-case there's a flush race on mount | |
| 286 | */ | |
| 287 | if (TAILQ_FIRST(&hmp->flusher.ready_list) == NULL) | |
| 288 | return; | |
| 289 | ||
| 290 | /* | |
| 291 | * We only do one flg but we may have to loop/retry. | |
| 292 | */ | |
| 15e75dab | 293 | count = 0; |
| 7a61b85d | 294 | while ((flg = TAILQ_FIRST(&hmp->flush_group_list)) != NULL) { |
| 15e75dab | 295 | ++count; |
| 7a61b85d MD |
296 | if (hammer_debug_general & 0x0001) { |
| 297 | kprintf("hammer_flush %d ttl=%d recs=%d\n", | |
| 298 | hmp->flusher.act, | |
| 299 | flg->total_count, flg->refs); | |
| 300 | } | |
| cdb6e4e6 MD |
301 | if (hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR) |
| 302 | break; | |
| 7a61b85d MD |
303 | hammer_start_transaction_fls(&hmp->flusher.trans, hmp); |
| 304 | ||
| 305 | /* | |
| 306 | * If the previous flush cycle just about exhausted our | |
| 307 | * UNDO space we may have to do a dummy cycle to move the | |
| 308 | * first_offset up before actually digging into a new cycle, | |
| 309 | * or the new cycle will not have sufficient undo space. | |
| 310 | */ | |
| 311 | if (hammer_flusher_undo_exhausted(&hmp->flusher.trans, 3)) | |
| 312 | hammer_flusher_finalize(&hmp->flusher.trans, 0); | |
| 313 | ||
| 314 | /* | |
| 7b6ccb11 MD |
315 | * Ok, we are running this flush group now (this prevents new |
| 316 | * additions to it). | |
| 317 | */ | |
| 318 | flg->running = 1; | |
| 319 | if (hmp->next_flush_group == flg) | |
| 320 | hmp->next_flush_group = TAILQ_NEXT(flg, flush_entry); | |
| 321 | ||
| 322 | /* | |
| ff003b11 | 323 | * Iterate the inodes in the flg's flush_tree and assign |
| 7a61b85d MD |
324 | * them to slaves. |
| 325 | */ | |
| 7a61b85d MD |
326 | slave_index = 0; |
| 327 | info = TAILQ_FIRST(&hmp->flusher.ready_list); | |
| ff003b11 | 328 | next_ip = RB_FIRST(hammer_fls_rb_tree, &flg->flush_tree); |
| 7a61b85d MD |
329 | |
| 330 | while ((ip = next_ip) != NULL) { | |
| ff003b11 MD |
331 | next_ip = RB_NEXT(hammer_fls_rb_tree, |
| 332 | &flg->flush_tree, ip); | |
| 7a61b85d | 333 | |
| 3e583440 MD |
334 | if (++hmp->check_yield > hammer_yield_check) { |
| 335 | hmp->check_yield = 0; | |
| 336 | lwkt_user_yield(); | |
| 337 | } | |
| 338 | ||
| 7a61b85d MD |
339 | /* |
| 340 | * Add ip to the slave's work array. The slave is | |
| 341 | * not currently running. | |
| 342 | */ | |
| 343 | info->work_array[info->count++] = ip; | |
| 344 | if (info->count != HAMMER_FLUSH_GROUP_SIZE) | |
| 345 | continue; | |
| 346 | ||
| 347 | /* | |
| 348 | * Get the slave running | |
| 349 | */ | |
| 350 | TAILQ_REMOVE(&hmp->flusher.ready_list, info, entry); | |
| 351 | TAILQ_INSERT_TAIL(&hmp->flusher.run_list, info, entry); | |
| 352 | info->flg = flg; | |
| 353 | info->runstate = 1; | |
| 354 | wakeup(&info->runstate); | |
| 355 | ||
| 356 | /* | |
| 357 | * Get a new slave. We may have to wait for one to | |
| 358 | * finish running. | |
| 359 | */ | |
| 360 | while ((info = TAILQ_FIRST(&hmp->flusher.ready_list)) == NULL) { | |
| 361 | tsleep(&hmp->flusher.ready_list, 0, "hmrfcc", 0); | |
| 362 | } | |
| 363 | } | |
| 364 | ||
| 365 | /* | |
| 366 | * Run the current slave if necessary | |
| 367 | */ | |
| 368 | if (info->count) { | |
| 369 | TAILQ_REMOVE(&hmp->flusher.ready_list, info, entry); | |
| 370 | TAILQ_INSERT_TAIL(&hmp->flusher.run_list, info, entry); | |
| 371 | info->flg = flg; | |
| 372 | info->runstate = 1; | |
| 373 | wakeup(&info->runstate); | |
| 374 | } | |
| 375 | ||
| 376 | /* | |
| 377 | * Wait for all slaves to finish running | |
| 378 | */ | |
| 379 | while (TAILQ_FIRST(&hmp->flusher.run_list) != NULL) | |
| 380 | tsleep(&hmp->flusher.ready_list, 0, "hmrfcc", 0); | |
| 381 | ||
| 382 | /* | |
| 383 | * Do the final finalization, clean up | |
| 384 | */ | |
| 385 | hammer_flusher_finalize(&hmp->flusher.trans, 1); | |
| 386 | hmp->flusher.tid = hmp->flusher.trans.tid; | |
| 387 | ||
| 388 | hammer_done_transaction(&hmp->flusher.trans); | |
| 389 | ||
| 390 | /* | |
| 391 | * Loop up on the same flg. If the flg is done clean it up | |
| 392 | * and break out. We only flush one flg. | |
| 393 | */ | |
| ff003b11 | 394 | if (RB_EMPTY(&flg->flush_tree)) { |
| 7a61b85d MD |
395 | KKASSERT(flg->refs == 0); |
| 396 | TAILQ_REMOVE(&hmp->flush_group_list, flg, flush_entry); | |
| bac808fe | 397 | kfree(flg, hmp->m_misc); |
| 7a61b85d MD |
398 | break; |
| 399 | } | |
| 400 | } | |
| 401 | ||
| 402 | /* | |
| 1b0ab2c3 MD |
403 | * We may have pure meta-data to flush, or we may have to finish |
| 404 | * cycling the UNDO FIFO, even if there were no flush groups. | |
| 15e75dab | 405 | */ |
| 1b0ab2c3 | 406 | if (count == 0 && hammer_flusher_haswork(hmp)) { |
| 15e75dab MD |
407 | hammer_start_transaction_fls(&hmp->flusher.trans, hmp); |
| 408 | hammer_flusher_finalize(&hmp->flusher.trans, 1); | |
| 409 | hammer_done_transaction(&hmp->flusher.trans); | |
| 410 | } | |
| 411 | ||
| 412 | /* | |
| 7a61b85d MD |
413 | * Clean up any freed big-blocks (typically zone-2). |
| 414 | * resv->flush_group is typically set several flush groups ahead | |
| 415 | * of the free to ensure that the freed block is not reused until | |
| 416 | * it can no longer be reused. | |
| 417 | */ | |
| 418 | while ((resv = TAILQ_FIRST(&hmp->delay_list)) != NULL) { | |
| 419 | if (resv->flush_group != hmp->flusher.act) | |
| 420 | break; | |
| 421 | hammer_reserve_clrdelay(hmp, resv); | |
| 422 | } | |
| 423 | } | |
| 424 | ||
| 425 | ||
| 426 | /* | |
| ff003b11 | 427 | * The slave flusher thread pulls work off the master flush list until no |
| af209b0f MD |
428 | * work is left. |
| 429 | */ | |
| da2da375 MD |
430 | static void |
| 431 | hammer_flusher_slave_thread(void *arg) | |
| 432 | { | |
| 7a61b85d | 433 | hammer_flush_group_t flg; |
| da2da375 MD |
434 | hammer_flusher_info_t info; |
| 435 | hammer_mount_t hmp; | |
| 436 | hammer_inode_t ip; | |
| cb51be26 | 437 | int i; |
| da2da375 MD |
438 | |
| 439 | info = arg; | |
| 440 | hmp = info->hmp; | |
| 441 | ||
| 442 | for (;;) { | |
| 7a61b85d MD |
443 | while (info->runstate == 0) |
| 444 | tsleep(&info->runstate, 0, "hmrssw", 0); | |
| 445 | if (info->runstate < 0) | |
| da2da375 | 446 | break; |
| 7a61b85d | 447 | flg = info->flg; |
| cb51be26 | 448 | |
| 7a61b85d MD |
449 | for (i = 0; i < info->count; ++i) { |
| 450 | ip = info->work_array[i]; | |
| 451 | hammer_flusher_flush_inode(ip, &hmp->flusher.trans); | |
| ce0138a6 | 452 | ++hammer_stats_inode_flushes; |
| cb51be26 | 453 | } |
| 7a61b85d MD |
454 | info->count = 0; |
| 455 | info->runstate = 0; | |
| 456 | TAILQ_REMOVE(&hmp->flusher.run_list, info, entry); | |
| 457 | TAILQ_INSERT_TAIL(&hmp->flusher.ready_list, info, entry); | |
| 458 | wakeup(&hmp->flusher.ready_list); | |
| da2da375 MD |
459 | } |
| 460 | info->td = NULL; | |
| 461 | wakeup(&info->td); | |
| 059819e3 MD |
462 | lwkt_exit(); |
| 463 | } | |
| 464 | ||
| 525aad3a | 465 | void |
| 10a5d1ba MD |
466 | hammer_flusher_clean_loose_ios(hammer_mount_t hmp) |
| 467 | { | |
| 468 | hammer_buffer_t buffer; | |
| 469 | hammer_io_t io; | |
| 470 | ||
| 471 | /* | |
| 472 | * loose ends - buffers without bp's aren't tracked by the kernel | |
| 473 | * and can build up, so clean them out. This can occur when an | |
| 474 | * IO completes on a buffer with no references left. | |
| 475 | */ | |
| 525aad3a MD |
476 | if ((io = TAILQ_FIRST(&hmp->lose_list)) != NULL) { |
| 477 | crit_enter(); /* biodone() race */ | |
| 478 | while ((io = TAILQ_FIRST(&hmp->lose_list)) != NULL) { | |
| 479 | KKASSERT(io->mod_list == &hmp->lose_list); | |
| 480 | TAILQ_REMOVE(&hmp->lose_list, io, mod_entry); | |
| 481 | io->mod_list = NULL; | |
| 482 | if (io->lock.refs == 0) | |
| 483 | ++hammer_count_refedbufs; | |
| 484 | hammer_ref(&io->lock); | |
| 485 | buffer = (void *)io; | |
| 486 | hammer_rel_buffer(buffer, 0); | |
| 487 | } | |
| 488 | crit_exit(); | |
| 10a5d1ba MD |
489 | } |
| 490 | } | |
| 491 | ||
| 059819e3 | 492 | /* |
| 9f5097dc | 493 | * Flush a single inode that is part of a flush group. |
| 06ad81ff | 494 | * |
| cdb6e4e6 MD |
495 | * Flusher errors are extremely serious, even ENOSPC shouldn't occur because |
| 496 | * the front-end should have reserved sufficient space on the media. Any | |
| 497 | * error other then EWOULDBLOCK will force the mount to be read-only. | |
| 9f5097dc MD |
498 | */ |
| 499 | static | |
| 500 | void | |
| 501 | hammer_flusher_flush_inode(hammer_inode_t ip, hammer_transaction_t trans) | |
| 502 | { | |
| 503 | hammer_mount_t hmp = ip->hmp; | |
| 06ad81ff | 504 | int error; |
| 9f5097dc | 505 | |
| 525aad3a | 506 | hammer_flusher_clean_loose_ios(hmp); |
| 02325004 | 507 | error = hammer_sync_inode(trans, ip); |
| cdb6e4e6 MD |
508 | |
| 509 | /* | |
| 510 | * EWOULDBLOCK can happen under normal operation, all other errors | |
| 511 | * are considered extremely serious. We must set WOULDBLOCK | |
| 512 | * mechanics to deal with the mess left over from the abort of the | |
| 513 | * previous flush. | |
| 514 | */ | |
| 515 | if (error) { | |
| 516 | ip->flags |= HAMMER_INODE_WOULDBLOCK; | |
| 517 | if (error == EWOULDBLOCK) | |
| 518 | error = 0; | |
| 519 | } | |
| 520 | hammer_flush_inode_done(ip, error); | |
| da2da375 MD |
521 | while (hmp->flusher.finalize_want) |
| 522 | tsleep(&hmp->flusher.finalize_want, 0, "hmrsxx", 0); | |
| 06ad81ff | 523 | if (hammer_flusher_undo_exhausted(trans, 1)) { |
| 5a930e66 | 524 | kprintf("HAMMER: Warning: UNDO area too small!\n"); |
| 9f5097dc | 525 | hammer_flusher_finalize(trans, 1); |
| 06ad81ff | 526 | } else if (hammer_flusher_meta_limit(trans->hmp)) { |
| 9f5097dc MD |
527 | hammer_flusher_finalize(trans, 0); |
| 528 | } | |
| 529 | } | |
| 530 | ||
| 531 | /* | |
| 06ad81ff MD |
532 | * Return non-zero if the UNDO area has less then (QUARTER / 4) of its |
| 533 | * space left. | |
| 534 | * | |
| 535 | * 1/4 - Emergency free undo space level. Below this point the flusher | |
| 536 | * will finalize even if directory dependancies have not been resolved. | |
| 537 | * | |
| 538 | * 2/4 - Used by the pruning and reblocking code. These functions may be | |
| 539 | * running in parallel with a flush and cannot be allowed to drop | |
| 540 | * available undo space to emergency levels. | |
| 541 | * | |
| 542 | * 3/4 - Used at the beginning of a flush to force-sync the volume header | |
| 543 | * to give the flush plenty of runway to work in. | |
| ec4e8497 | 544 | */ |
| ec4e8497 | 545 | int |
| 06ad81ff | 546 | hammer_flusher_undo_exhausted(hammer_transaction_t trans, int quarter) |
| ec4e8497 | 547 | { |
| 06ad81ff MD |
548 | if (hammer_undo_space(trans) < |
| 549 | hammer_undo_max(trans->hmp) * quarter / 4) { | |
| 1f07f686 | 550 | return(1); |
| ec4e8497 | 551 | } else { |
| 1f07f686 | 552 | return(0); |
| ec4e8497 | 553 | } |
| ec4e8497 MD |
554 | } |
| 555 | ||
| 556 | /* | |
| 9f5097dc MD |
557 | * Flush all pending UNDOs, wait for write completion, update the volume |
| 558 | * header with the new UNDO end position, and flush it. Then | |
| 559 | * asynchronously flush the meta-data. | |
| 10a5d1ba | 560 | * |
| 9f5097dc MD |
561 | * If this is the last finalization in a flush group we also synchronize |
| 562 | * our cached blockmap and set hmp->flusher_undo_start and our cached undo | |
| 563 | * fifo first_offset so the next flush resets the FIFO pointers. | |
| 6c1f89f4 MD |
564 | * |
| 565 | * If this is not final it is being called because too many dirty meta-data | |
| 566 | * buffers have built up and must be flushed with UNDO synchronization to | |
| 567 | * avoid a buffer cache deadlock. | |
| 10a5d1ba | 568 | */ |
| 10a5d1ba | 569 | void |
| 9f5097dc | 570 | hammer_flusher_finalize(hammer_transaction_t trans, int final) |
| 059819e3 | 571 | { |
| 9f5097dc MD |
572 | hammer_volume_t root_volume; |
| 573 | hammer_blockmap_t cundomap, dundomap; | |
| 574 | hammer_mount_t hmp; | |
| 10a5d1ba | 575 | hammer_io_t io; |
| c9b9e29d | 576 | int count; |
| 19619882 | 577 | int i; |
| 059819e3 | 578 | |
| 9f5097dc MD |
579 | hmp = trans->hmp; |
| 580 | root_volume = trans->rootvol; | |
| 581 | ||
| 47637bff | 582 | /* |
| 6c1f89f4 MD |
583 | * Exclusively lock the flusher. This guarantees that all dirty |
| 584 | * buffers will be idled (have a mod-count of 0). | |
| 585 | */ | |
| 586 | ++hmp->flusher.finalize_want; | |
| 587 | hammer_lock_ex(&hmp->flusher.finalize_lock); | |
| 588 | ||
| 589 | /* | |
| 590 | * If this isn't the final sync several threads may have hit the | |
| 591 | * meta-limit at the same time and raced. Only sync if we really | |
| 592 | * have to, after acquiring the lock. | |
| 593 | */ | |
| 594 | if (final == 0 && !hammer_flusher_meta_limit(hmp)) | |
| 595 | goto done; | |
| 596 | ||
| cdb6e4e6 MD |
597 | if (hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR) |
| 598 | goto done; | |
| 599 | ||
| 6c1f89f4 | 600 | /* |
| 47637bff | 601 | * Flush data buffers. This can occur asynchronously and at any |
| 9f5097dc MD |
602 | * time. We must interlock against the frontend direct-data write |
| 603 | * but do not have to acquire the sync-lock yet. | |
| 9192654c MD |
604 | * |
| 605 | * These data buffers have already been collected prior to the | |
| 606 | * related inode(s) getting queued to the flush group. | |
| 47637bff MD |
607 | */ |
| 608 | count = 0; | |
| 609 | while ((io = TAILQ_FIRST(&hmp->data_list)) != NULL) { | |
| cdb6e4e6 MD |
610 | if (io->ioerror) |
| 611 | break; | |
| a99b9ea2 MD |
612 | if (io->lock.refs == 0) |
| 613 | ++hammer_count_refedbufs; | |
| 47637bff | 614 | hammer_ref(&io->lock); |
| 9f5097dc | 615 | hammer_io_write_interlock(io); |
| 47637bff | 616 | KKASSERT(io->type != HAMMER_STRUCTURE_VOLUME); |
| 710733a6 | 617 | hammer_io_flush(io, 0); |
| 9f5097dc | 618 | hammer_io_done_interlock(io); |
| 47637bff MD |
619 | hammer_rel_buffer((hammer_buffer_t)io, 0); |
| 620 | ++count; | |
| 621 | } | |
| 47637bff | 622 | |
| 9f5097dc MD |
623 | /* |
| 624 | * The sync-lock is required for the remaining sequence. This lock | |
| 625 | * prevents meta-data from being modified. | |
| 626 | */ | |
| 2f85fa4d | 627 | hammer_sync_lock_ex(trans); |
| 9480ff55 | 628 | |
| 059819e3 | 629 | /* |
| 9f5097dc MD |
630 | * If we have been asked to finalize the volume header sync the |
| 631 | * cached blockmap to the on-disk blockmap. Generate an UNDO | |
| 632 | * record for the update. | |
| e8599db1 | 633 | */ |
| 9f5097dc MD |
634 | if (final) { |
| 635 | cundomap = &hmp->blockmap[0]; | |
| 636 | dundomap = &root_volume->ondisk->vol0_blockmap[0]; | |
| 637 | if (root_volume->io.modified) { | |
| 638 | hammer_modify_volume(trans, root_volume, | |
| 639 | dundomap, sizeof(hmp->blockmap)); | |
| 640 | for (i = 0; i < HAMMER_MAX_ZONES; ++i) | |
| 641 | hammer_crc_set_blockmap(&cundomap[i]); | |
| 642 | bcopy(cundomap, dundomap, sizeof(hmp->blockmap)); | |
| 643 | hammer_modify_volume_done(root_volume); | |
| 644 | } | |
| e8599db1 MD |
645 | } |
| 646 | ||
| 647 | /* | |
| 6048b411 MD |
648 | * Flush UNDOs. This also waits for I/Os to complete and flushes |
| 649 | * the cache on the target disk. | |
| 059819e3 | 650 | */ |
| 9192654c | 651 | hammer_flusher_flush_undos(hmp, HAMMER_FLUSH_UNDOS_FORCED); |
| 059819e3 | 652 | |
| cdb6e4e6 MD |
653 | if (hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR) |
| 654 | goto failed; | |
| 655 | ||
| 059819e3 | 656 | /* |
| 02428fb6 MD |
657 | * HAMMER VERSION < 4: |
| 658 | * Update the on-disk volume header with new UNDO FIFO end | |
| 659 | * position (do not generate new UNDO records for this change). | |
| 660 | * We have to do this for the UNDO FIFO whether (final) is | |
| 661 | * set or not in order for the UNDOs to be recognized on | |
| 662 | * recovery. | |
| 663 | * | |
| 664 | * HAMMER VERSION >= 4: | |
| 665 | * The UNDO FIFO data written above will be recognized on | |
| 666 | * recovery without us having to sync the volume header. | |
| 9f5097dc MD |
667 | * |
| 668 | * Also update the on-disk next_tid field. This does not require | |
| 669 | * an UNDO. However, because our TID is generated before we get | |
| 670 | * the sync lock another sync may have beat us to the punch. | |
| 671 | * | |
| 06ad81ff MD |
672 | * This also has the side effect of updating first_offset based on |
| 673 | * a prior finalization when the first finalization of the next flush | |
| 674 | * cycle occurs, removing any undo info from the prior finalization | |
| 675 | * from consideration. | |
| 676 | * | |
| 9f5097dc | 677 | * The volume header will be flushed out synchronously. |
| 059819e3 | 678 | */ |
| 9f5097dc MD |
679 | dundomap = &root_volume->ondisk->vol0_blockmap[HAMMER_ZONE_UNDO_INDEX]; |
| 680 | cundomap = &hmp->blockmap[HAMMER_ZONE_UNDO_INDEX]; | |
| 09ac686b | 681 | |
| 9f5097dc | 682 | if (dundomap->first_offset != cundomap->first_offset || |
| cdb6e4e6 | 683 | dundomap->next_offset != cundomap->next_offset) { |
| 0729c8c8 | 684 | hammer_modify_volume(NULL, root_volume, NULL, 0); |
| 9f5097dc MD |
685 | dundomap->first_offset = cundomap->first_offset; |
| 686 | dundomap->next_offset = cundomap->next_offset; | |
| 687 | hammer_crc_set_blockmap(dundomap); | |
| 0729c8c8 MD |
688 | hammer_modify_volume_done(root_volume); |
| 689 | } | |
| c9b9e29d | 690 | |
| 4889cbd4 MD |
691 | /* |
| 692 | * vol0_next_tid is used for TID selection and is updated without | |
| 693 | * an UNDO so we do not reuse a TID that may have been rolled-back. | |
| 694 | * | |
| 695 | * vol0_last_tid is the highest fully-synchronized TID. It is | |
| 696 | * set-up when the UNDO fifo is fully synced, later on (not here). | |
| 697 | */ | |
| 19619882 | 698 | if (root_volume->io.modified) { |
| adf01747 MD |
699 | hammer_modify_volume(NULL, root_volume, NULL, 0); |
| 700 | if (root_volume->ondisk->vol0_next_tid < trans->tid) | |
| 701 | root_volume->ondisk->vol0_next_tid = trans->tid; | |
| 702 | hammer_crc_set_volume(root_volume->ondisk); | |
| 703 | hammer_modify_volume_done(root_volume); | |
| 710733a6 | 704 | hammer_io_flush(&root_volume->io, 0); |
| 19619882 | 705 | } |
| 059819e3 MD |
706 | |
| 707 | /* | |
| 02428fb6 MD |
708 | * Wait for I/Os to complete. |
| 709 | * | |
| 710 | * For HAMMER VERSION 4+ filesystems we do not have to wait for | |
| 711 | * the I/O to complete as the new UNDO FIFO entries are recognized | |
| 712 | * even without the volume header update. This allows the volume | |
| 713 | * header to flushed along with meta-data, significantly reducing | |
| 714 | * flush overheads. | |
| 059819e3 | 715 | */ |
| a99b9ea2 | 716 | hammer_flusher_clean_loose_ios(hmp); |
| 02428fb6 MD |
717 | if (hmp->version < HAMMER_VOL_VERSION_FOUR) |
| 718 | hammer_io_wait_all(hmp, "hmrfl2"); | |
| 059819e3 | 719 | |
| cdb6e4e6 MD |
720 | if (hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR) |
| 721 | goto failed; | |
| 722 | ||
| 059819e3 | 723 | /* |
| e8599db1 | 724 | * Flush meta-data. The meta-data will be undone if we crash |
| 02428fb6 MD |
725 | * so we can safely flush it asynchronously. There is no need |
| 726 | * to wait for I/O to complete (or issue a synchronous disk flush). | |
| 9f5097dc | 727 | * |
| 02428fb6 MD |
728 | * In fact, even if we did wait the meta-data will still be undone |
| 729 | * by a crash up until the next flush cycle due to the first_offset | |
| 730 | * in the volume header for the UNDO FIFO not being adjusted until | |
| 731 | * the following flush cycle. | |
| 059819e3 | 732 | */ |
| c9b9e29d | 733 | count = 0; |
| 10a5d1ba | 734 | while ((io = TAILQ_FIRST(&hmp->meta_list)) != NULL) { |
| cdb6e4e6 MD |
735 | if (io->ioerror) |
| 736 | break; | |
| 10a5d1ba | 737 | KKASSERT(io->modify_refs == 0); |
| a99b9ea2 MD |
738 | if (io->lock.refs == 0) |
| 739 | ++hammer_count_refedbufs; | |
| 10a5d1ba MD |
740 | hammer_ref(&io->lock); |
| 741 | KKASSERT(io->type != HAMMER_STRUCTURE_VOLUME); | |
| 710733a6 | 742 | hammer_io_flush(io, 0); |
| 09ac686b | 743 | hammer_rel_buffer((hammer_buffer_t)io, 0); |
| c9b9e29d | 744 | ++count; |
| 059819e3 | 745 | } |
| 9f5097dc MD |
746 | |
| 747 | /* | |
| 748 | * If this is the final finalization for the flush group set | |
| 749 | * up for the next sequence by setting a new first_offset in | |
| 06ad81ff MD |
750 | * our cached blockmap and clearing the undo history. |
| 751 | * | |
| 752 | * Even though we have updated our cached first_offset, the on-disk | |
| 753 | * first_offset still governs available-undo-space calculations. | |
| 9f5097dc MD |
754 | */ |
| 755 | if (final) { | |
| 756 | cundomap = &hmp->blockmap[HAMMER_ZONE_UNDO_INDEX]; | |
| 1b0ab2c3 MD |
757 | if (cundomap->first_offset == cundomap->next_offset) { |
| 758 | hmp->hflags &= ~HMNT_UNDO_DIRTY; | |
| 759 | } else { | |
| 760 | cundomap->first_offset = cundomap->next_offset; | |
| 761 | hmp->hflags |= HMNT_UNDO_DIRTY; | |
| 762 | } | |
| 9f5097dc | 763 | hammer_clear_undo_history(hmp); |
| 4889cbd4 MD |
764 | |
| 765 | /* | |
| 766 | * Flush tid sequencing. flush_tid1 is fully synchronized, | |
| 767 | * meaning a crash will not roll it back. flush_tid2 has | |
| 768 | * been written out asynchronously and a crash will roll | |
| 769 | * it back. flush_tid1 is used for all mirroring masters. | |
| 770 | */ | |
| 771 | if (hmp->flush_tid1 != hmp->flush_tid2) { | |
| 772 | hmp->flush_tid1 = hmp->flush_tid2; | |
| 773 | wakeup(&hmp->flush_tid1); | |
| 774 | } | |
| 775 | hmp->flush_tid2 = trans->tid; | |
| 9f5097dc MD |
776 | } |
| 777 | ||
| cdb6e4e6 MD |
778 | /* |
| 779 | * Cleanup. Report any critical errors. | |
| 780 | */ | |
| 781 | failed: | |
| 2f85fa4d | 782 | hammer_sync_unlock(trans); |
| 6c1f89f4 | 783 | |
| cdb6e4e6 MD |
784 | if (hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR) { |
| 785 | kprintf("HAMMER(%s): Critical write error during flush, " | |
| 786 | "refusing to sync UNDO FIFO\n", | |
| 787 | root_volume->ondisk->vol_name); | |
| 788 | } | |
| 789 | ||
| 6c1f89f4 MD |
790 | done: |
| 791 | hammer_unlock(&hmp->flusher.finalize_lock); | |
| 4889cbd4 | 792 | |
| 6c1f89f4 MD |
793 | if (--hmp->flusher.finalize_want == 0) |
| 794 | wakeup(&hmp->flusher.finalize_want); | |
| ce0138a6 | 795 | hammer_stats_commits += final; |
| 059819e3 MD |
796 | } |
| 797 | ||
| 06ad81ff | 798 | /* |
| 9192654c | 799 | * Flush UNDOs. |
| 6048b411 MD |
800 | */ |
| 801 | void | |
| 9192654c | 802 | hammer_flusher_flush_undos(hammer_mount_t hmp, int mode) |
| 6048b411 MD |
803 | { |
| 804 | hammer_io_t io; | |
| 805 | int count; | |
| 806 | ||
| 6048b411 MD |
807 | count = 0; |
| 808 | while ((io = TAILQ_FIRST(&hmp->undo_list)) != NULL) { | |
| 809 | if (io->ioerror) | |
| 810 | break; | |
| 811 | KKASSERT(io->modify_refs == 0); | |
| 812 | if (io->lock.refs == 0) | |
| 813 | ++hammer_count_refedbufs; | |
| 814 | hammer_ref(&io->lock); | |
| 815 | KKASSERT(io->type != HAMMER_STRUCTURE_VOLUME); | |
| 816 | hammer_io_flush(io, hammer_undo_reclaim(io)); | |
| 817 | hammer_rel_buffer((hammer_buffer_t)io, 0); | |
| 818 | ++count; | |
| 819 | } | |
| 820 | hammer_flusher_clean_loose_ios(hmp); | |
| 9192654c MD |
821 | if (mode == HAMMER_FLUSH_UNDOS_FORCED || |
| 822 | (mode == HAMMER_FLUSH_UNDOS_AUTO && count)) { | |
| 823 | hammer_io_wait_all(hmp, "hmrfl1"); | |
| 824 | } | |
| 6048b411 MD |
825 | } |
| 826 | ||
| 827 | /* | |
| 06ad81ff MD |
828 | * Return non-zero if too many dirty meta-data buffers have built up. |
| 829 | * | |
| 830 | * Since we cannot allow such buffers to flush until we have dealt with | |
| 831 | * the UNDOs, we risk deadlocking the kernel's buffer cache. | |
| 832 | */ | |
| 833 | int | |
| 834 | hammer_flusher_meta_limit(hammer_mount_t hmp) | |
| 835 | { | |
| f5a07a7a MD |
836 | if (hmp->locked_dirty_space + hmp->io_running_space > |
| 837 | hammer_limit_dirtybufspace) { | |
| 06ad81ff MD |
838 | return(1); |
| 839 | } | |
| 840 | return(0); | |
| 841 | } | |
| 842 | ||
| 1b0ab2c3 MD |
843 | /* |
| 844 | * Return non-zero if too many dirty meta-data buffers have built up. | |
| 845 | * | |
| 846 | * This version is used by background operations (mirror, prune, reblock) | |
| 847 | * to leave room for foreground operations. | |
| 848 | */ | |
| 93291532 MD |
849 | int |
| 850 | hammer_flusher_meta_halflimit(hammer_mount_t hmp) | |
| 851 | { | |
| 852 | if (hmp->locked_dirty_space + hmp->io_running_space > | |
| 853 | hammer_limit_dirtybufspace / 2) { | |
| 854 | return(1); | |
| 855 | } | |
| 856 | return(0); | |
| 857 | } | |
| 858 | ||
| 1b0ab2c3 MD |
859 | /* |
| 860 | * Return non-zero if the flusher still has something to flush. | |
| 861 | */ | |
| 862 | int | |
| 863 | hammer_flusher_haswork(hammer_mount_t hmp) | |
| 864 | { | |
| cdb6e4e6 MD |
865 | if (hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR) |
| 866 | return(0); | |
| 1b0ab2c3 | 867 | if (TAILQ_FIRST(&hmp->flush_group_list) || /* dirty inodes */ |
| 83ec399b | 868 | TAILQ_FIRST(&hmp->volu_list) || /* dirty buffers */ |
| 1b0ab2c3 MD |
869 | TAILQ_FIRST(&hmp->undo_list) || |
| 870 | TAILQ_FIRST(&hmp->data_list) || | |
| 871 | TAILQ_FIRST(&hmp->meta_list) || | |
| 872 | (hmp->hflags & HMNT_UNDO_DIRTY) /* UNDO FIFO sync */ | |
| 873 | ) { | |
| 874 | return(1); | |
| 875 | } | |
| 876 | return(0); | |
| 877 | } | |
| 878 |