lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * linux/fs/jbd/checkpoint.c |
| 3 | * |
| 4 | * Written by Stephen C. Tweedie <sct@redhat.com>, 1999 |
| 5 | * |
| 6 | * Copyright 1999 Red Hat Software --- All Rights Reserved |
| 7 | * |
| 8 | * This file is part of the Linux kernel and is made available under |
| 9 | * the terms of the GNU General Public License, version 2, or at your |
| 10 | * option, any later version, incorporated herein by reference. |
| 11 | * |
| 12 | * Checkpoint routines for the generic filesystem journaling code. |
| 13 | * Part of the ext2fs journaling system. |
| 14 | * |
| 15 | * Checkpointing is the process of ensuring that a section of the log is |
| 16 | * committed fully to disk, so that that portion of the log can be |
| 17 | * reused. |
| 18 | */ |
| 19 | |
| 20 | #include <linux/time.h> |
| 21 | #include <linux/fs.h> |
| 22 | #include <linux/jbd.h> |
| 23 | #include <linux/errno.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/blkdev.h> |
| 26 | #include <trace/events/jbd.h> |
| 27 | |
| 28 | /* |
| 29 | * Unlink a buffer from a transaction checkpoint list. |
| 30 | * |
| 31 | * Called with j_list_lock held. |
| 32 | */ |
| 33 | static inline void __buffer_unlink_first(struct journal_head *jh) |
| 34 | { |
| 35 | transaction_t *transaction = jh->b_cp_transaction; |
| 36 | |
| 37 | jh->b_cpnext->b_cpprev = jh->b_cpprev; |
| 38 | jh->b_cpprev->b_cpnext = jh->b_cpnext; |
| 39 | if (transaction->t_checkpoint_list == jh) { |
| 40 | transaction->t_checkpoint_list = jh->b_cpnext; |
| 41 | if (transaction->t_checkpoint_list == jh) |
| 42 | transaction->t_checkpoint_list = NULL; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /* |
| 47 | * Unlink a buffer from a transaction checkpoint(io) list. |
| 48 | * |
| 49 | * Called with j_list_lock held. |
| 50 | */ |
| 51 | static inline void __buffer_unlink(struct journal_head *jh) |
| 52 | { |
| 53 | transaction_t *transaction = jh->b_cp_transaction; |
| 54 | |
| 55 | __buffer_unlink_first(jh); |
| 56 | if (transaction->t_checkpoint_io_list == jh) { |
| 57 | transaction->t_checkpoint_io_list = jh->b_cpnext; |
| 58 | if (transaction->t_checkpoint_io_list == jh) |
| 59 | transaction->t_checkpoint_io_list = NULL; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | * Move a buffer from the checkpoint list to the checkpoint io list |
| 65 | * |
| 66 | * Called with j_list_lock held |
| 67 | */ |
| 68 | static inline void __buffer_relink_io(struct journal_head *jh) |
| 69 | { |
| 70 | transaction_t *transaction = jh->b_cp_transaction; |
| 71 | |
| 72 | __buffer_unlink_first(jh); |
| 73 | |
| 74 | if (!transaction->t_checkpoint_io_list) { |
| 75 | jh->b_cpnext = jh->b_cpprev = jh; |
| 76 | } else { |
| 77 | jh->b_cpnext = transaction->t_checkpoint_io_list; |
| 78 | jh->b_cpprev = transaction->t_checkpoint_io_list->b_cpprev; |
| 79 | jh->b_cpprev->b_cpnext = jh; |
| 80 | jh->b_cpnext->b_cpprev = jh; |
| 81 | } |
| 82 | transaction->t_checkpoint_io_list = jh; |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * Try to release a checkpointed buffer from its transaction. |
| 87 | * Returns 1 if we released it and 2 if we also released the |
| 88 | * whole transaction. |
| 89 | * |
| 90 | * Requires j_list_lock |
| 91 | * Called under jbd_lock_bh_state(jh2bh(jh)), and drops it |
| 92 | */ |
| 93 | static int __try_to_free_cp_buf(struct journal_head *jh) |
| 94 | { |
| 95 | int ret = 0; |
| 96 | struct buffer_head *bh = jh2bh(jh); |
| 97 | |
| 98 | if (jh->b_jlist == BJ_None && !buffer_locked(bh) && |
| 99 | !buffer_dirty(bh) && !buffer_write_io_error(bh)) { |
| 100 | /* |
| 101 | * Get our reference so that bh cannot be freed before |
| 102 | * we unlock it |
| 103 | */ |
| 104 | get_bh(bh); |
| 105 | JBUFFER_TRACE(jh, "remove from checkpoint list"); |
| 106 | ret = __journal_remove_checkpoint(jh) + 1; |
| 107 | jbd_unlock_bh_state(bh); |
| 108 | BUFFER_TRACE(bh, "release"); |
| 109 | __brelse(bh); |
| 110 | } else { |
| 111 | jbd_unlock_bh_state(bh); |
| 112 | } |
| 113 | return ret; |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | * __log_wait_for_space: wait until there is space in the journal. |
| 118 | * |
| 119 | * Called under j-state_lock *only*. It will be unlocked if we have to wait |
| 120 | * for a checkpoint to free up some space in the log. |
| 121 | */ |
| 122 | void __log_wait_for_space(journal_t *journal) |
| 123 | { |
| 124 | int nblocks, space_left; |
| 125 | assert_spin_locked(&journal->j_state_lock); |
| 126 | |
| 127 | nblocks = jbd_space_needed(journal); |
| 128 | while (__log_space_left(journal) < nblocks) { |
| 129 | if (journal->j_flags & JFS_ABORT) |
| 130 | return; |
| 131 | spin_unlock(&journal->j_state_lock); |
| 132 | if (current->plug) |
| 133 | io_schedule(); |
| 134 | mutex_lock(&journal->j_checkpoint_mutex); |
| 135 | |
| 136 | /* |
| 137 | * Test again, another process may have checkpointed while we |
| 138 | * were waiting for the checkpoint lock. If there are no |
| 139 | * transactions ready to be checkpointed, try to recover |
| 140 | * journal space by calling cleanup_journal_tail(), and if |
| 141 | * that doesn't work, by waiting for the currently committing |
| 142 | * transaction to complete. If there is absolutely no way |
| 143 | * to make progress, this is either a BUG or corrupted |
| 144 | * filesystem, so abort the journal and leave a stack |
| 145 | * trace for forensic evidence. |
| 146 | */ |
| 147 | spin_lock(&journal->j_state_lock); |
| 148 | spin_lock(&journal->j_list_lock); |
| 149 | nblocks = jbd_space_needed(journal); |
| 150 | space_left = __log_space_left(journal); |
| 151 | if (space_left < nblocks) { |
| 152 | int chkpt = journal->j_checkpoint_transactions != NULL; |
| 153 | tid_t tid = 0; |
| 154 | |
| 155 | if (journal->j_committing_transaction) |
| 156 | tid = journal->j_committing_transaction->t_tid; |
| 157 | spin_unlock(&journal->j_list_lock); |
| 158 | spin_unlock(&journal->j_state_lock); |
| 159 | if (chkpt) { |
| 160 | log_do_checkpoint(journal); |
| 161 | } else if (cleanup_journal_tail(journal) == 0) { |
| 162 | /* We were able to recover space; yay! */ |
| 163 | ; |
| 164 | } else if (tid) { |
| 165 | log_wait_commit(journal, tid); |
| 166 | } else { |
| 167 | printk(KERN_ERR "%s: needed %d blocks and " |
| 168 | "only had %d space available\n", |
| 169 | __func__, nblocks, space_left); |
| 170 | printk(KERN_ERR "%s: no way to get more " |
| 171 | "journal space\n", __func__); |
| 172 | WARN_ON(1); |
| 173 | journal_abort(journal, 0); |
| 174 | } |
| 175 | spin_lock(&journal->j_state_lock); |
| 176 | } else { |
| 177 | spin_unlock(&journal->j_list_lock); |
| 178 | } |
| 179 | mutex_unlock(&journal->j_checkpoint_mutex); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * We were unable to perform jbd_trylock_bh_state() inside j_list_lock. |
| 185 | * The caller must restart a list walk. Wait for someone else to run |
| 186 | * jbd_unlock_bh_state(). |
| 187 | */ |
| 188 | static void jbd_sync_bh(journal_t *journal, struct buffer_head *bh) |
| 189 | __releases(journal->j_list_lock) |
| 190 | { |
| 191 | get_bh(bh); |
| 192 | spin_unlock(&journal->j_list_lock); |
| 193 | jbd_lock_bh_state(bh); |
| 194 | jbd_unlock_bh_state(bh); |
| 195 | put_bh(bh); |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * Clean up transaction's list of buffers submitted for io. |
| 200 | * We wait for any pending IO to complete and remove any clean |
| 201 | * buffers. Note that we take the buffers in the opposite ordering |
| 202 | * from the one in which they were submitted for IO. |
| 203 | * |
| 204 | * Return 0 on success, and return <0 if some buffers have failed |
| 205 | * to be written out. |
| 206 | * |
| 207 | * Called with j_list_lock held. |
| 208 | */ |
| 209 | static int __wait_cp_io(journal_t *journal, transaction_t *transaction) |
| 210 | { |
| 211 | struct journal_head *jh; |
| 212 | struct buffer_head *bh; |
| 213 | tid_t this_tid; |
| 214 | int released = 0; |
| 215 | int ret = 0; |
| 216 | |
| 217 | this_tid = transaction->t_tid; |
| 218 | restart: |
| 219 | /* Did somebody clean up the transaction in the meanwhile? */ |
| 220 | if (journal->j_checkpoint_transactions != transaction || |
| 221 | transaction->t_tid != this_tid) |
| 222 | return ret; |
| 223 | while (!released && transaction->t_checkpoint_io_list) { |
| 224 | jh = transaction->t_checkpoint_io_list; |
| 225 | bh = jh2bh(jh); |
| 226 | if (!jbd_trylock_bh_state(bh)) { |
| 227 | jbd_sync_bh(journal, bh); |
| 228 | spin_lock(&journal->j_list_lock); |
| 229 | goto restart; |
| 230 | } |
| 231 | get_bh(bh); |
| 232 | if (buffer_locked(bh)) { |
| 233 | spin_unlock(&journal->j_list_lock); |
| 234 | jbd_unlock_bh_state(bh); |
| 235 | wait_on_buffer(bh); |
| 236 | /* the journal_head may have gone by now */ |
| 237 | BUFFER_TRACE(bh, "brelse"); |
| 238 | __brelse(bh); |
| 239 | spin_lock(&journal->j_list_lock); |
| 240 | goto restart; |
| 241 | } |
| 242 | if (unlikely(buffer_write_io_error(bh))) |
| 243 | ret = -EIO; |
| 244 | |
| 245 | /* |
| 246 | * Now in whatever state the buffer currently is, we know that |
| 247 | * it has been written out and so we can drop it from the list |
| 248 | */ |
| 249 | released = __journal_remove_checkpoint(jh); |
| 250 | jbd_unlock_bh_state(bh); |
| 251 | __brelse(bh); |
| 252 | } |
| 253 | |
| 254 | return ret; |
| 255 | } |
| 256 | |
| 257 | #define NR_BATCH 64 |
| 258 | |
| 259 | static void |
| 260 | __flush_batch(journal_t *journal, struct buffer_head **bhs, int *batch_count) |
| 261 | { |
| 262 | int i; |
| 263 | struct blk_plug plug; |
| 264 | |
| 265 | blk_start_plug(&plug); |
| 266 | for (i = 0; i < *batch_count; i++) |
| 267 | write_dirty_buffer(bhs[i], WRITE_SYNC); |
| 268 | blk_finish_plug(&plug); |
| 269 | |
| 270 | for (i = 0; i < *batch_count; i++) { |
| 271 | struct buffer_head *bh = bhs[i]; |
| 272 | clear_buffer_jwrite(bh); |
| 273 | BUFFER_TRACE(bh, "brelse"); |
| 274 | __brelse(bh); |
| 275 | } |
| 276 | *batch_count = 0; |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | * Try to flush one buffer from the checkpoint list to disk. |
| 281 | * |
| 282 | * Return 1 if something happened which requires us to abort the current |
| 283 | * scan of the checkpoint list. Return <0 if the buffer has failed to |
| 284 | * be written out. |
| 285 | * |
| 286 | * Called with j_list_lock held and drops it if 1 is returned |
| 287 | * Called under jbd_lock_bh_state(jh2bh(jh)), and drops it |
| 288 | */ |
| 289 | static int __process_buffer(journal_t *journal, struct journal_head *jh, |
| 290 | struct buffer_head **bhs, int *batch_count) |
| 291 | { |
| 292 | struct buffer_head *bh = jh2bh(jh); |
| 293 | int ret = 0; |
| 294 | |
| 295 | if (buffer_locked(bh)) { |
| 296 | get_bh(bh); |
| 297 | spin_unlock(&journal->j_list_lock); |
| 298 | jbd_unlock_bh_state(bh); |
| 299 | wait_on_buffer(bh); |
| 300 | /* the journal_head may have gone by now */ |
| 301 | BUFFER_TRACE(bh, "brelse"); |
| 302 | __brelse(bh); |
| 303 | ret = 1; |
| 304 | } else if (jh->b_transaction != NULL) { |
| 305 | transaction_t *t = jh->b_transaction; |
| 306 | tid_t tid = t->t_tid; |
| 307 | |
| 308 | spin_unlock(&journal->j_list_lock); |
| 309 | jbd_unlock_bh_state(bh); |
| 310 | log_start_commit(journal, tid); |
| 311 | log_wait_commit(journal, tid); |
| 312 | ret = 1; |
| 313 | } else if (!buffer_dirty(bh)) { |
| 314 | ret = 1; |
| 315 | if (unlikely(buffer_write_io_error(bh))) |
| 316 | ret = -EIO; |
| 317 | get_bh(bh); |
| 318 | J_ASSERT_JH(jh, !buffer_jbddirty(bh)); |
| 319 | BUFFER_TRACE(bh, "remove from checkpoint"); |
| 320 | __journal_remove_checkpoint(jh); |
| 321 | spin_unlock(&journal->j_list_lock); |
| 322 | jbd_unlock_bh_state(bh); |
| 323 | __brelse(bh); |
| 324 | } else { |
| 325 | /* |
| 326 | * Important: we are about to write the buffer, and |
| 327 | * possibly block, while still holding the journal lock. |
| 328 | * We cannot afford to let the transaction logic start |
| 329 | * messing around with this buffer before we write it to |
| 330 | * disk, as that would break recoverability. |
| 331 | */ |
| 332 | BUFFER_TRACE(bh, "queue"); |
| 333 | get_bh(bh); |
| 334 | J_ASSERT_BH(bh, !buffer_jwrite(bh)); |
| 335 | set_buffer_jwrite(bh); |
| 336 | bhs[*batch_count] = bh; |
| 337 | __buffer_relink_io(jh); |
| 338 | jbd_unlock_bh_state(bh); |
| 339 | (*batch_count)++; |
| 340 | if (*batch_count == NR_BATCH) { |
| 341 | spin_unlock(&journal->j_list_lock); |
| 342 | __flush_batch(journal, bhs, batch_count); |
| 343 | ret = 1; |
| 344 | } |
| 345 | } |
| 346 | return ret; |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | * Perform an actual checkpoint. We take the first transaction on the |
| 351 | * list of transactions to be checkpointed and send all its buffers |
| 352 | * to disk. We submit larger chunks of data at once. |
| 353 | * |
| 354 | * The journal should be locked before calling this function. |
| 355 | * Called with j_checkpoint_mutex held. |
| 356 | */ |
| 357 | int log_do_checkpoint(journal_t *journal) |
| 358 | { |
| 359 | transaction_t *transaction; |
| 360 | tid_t this_tid; |
| 361 | int result; |
| 362 | |
| 363 | jbd_debug(1, "Start checkpoint\n"); |
| 364 | |
| 365 | /* |
| 366 | * First thing: if there are any transactions in the log which |
| 367 | * don't need checkpointing, just eliminate them from the |
| 368 | * journal straight away. |
| 369 | */ |
| 370 | result = cleanup_journal_tail(journal); |
| 371 | trace_jbd_checkpoint(journal, result); |
| 372 | jbd_debug(1, "cleanup_journal_tail returned %d\n", result); |
| 373 | if (result <= 0) |
| 374 | return result; |
| 375 | |
| 376 | /* |
| 377 | * OK, we need to start writing disk blocks. Take one transaction |
| 378 | * and write it. |
| 379 | */ |
| 380 | result = 0; |
| 381 | spin_lock(&journal->j_list_lock); |
| 382 | if (!journal->j_checkpoint_transactions) |
| 383 | goto out; |
| 384 | transaction = journal->j_checkpoint_transactions; |
| 385 | this_tid = transaction->t_tid; |
| 386 | restart: |
| 387 | /* |
| 388 | * If someone cleaned up this transaction while we slept, we're |
| 389 | * done (maybe it's a new transaction, but it fell at the same |
| 390 | * address). |
| 391 | */ |
| 392 | if (journal->j_checkpoint_transactions == transaction && |
| 393 | transaction->t_tid == this_tid) { |
| 394 | int batch_count = 0; |
| 395 | struct buffer_head *bhs[NR_BATCH]; |
| 396 | struct journal_head *jh; |
| 397 | int retry = 0, err; |
| 398 | |
| 399 | while (!retry && transaction->t_checkpoint_list) { |
| 400 | struct buffer_head *bh; |
| 401 | |
| 402 | jh = transaction->t_checkpoint_list; |
| 403 | bh = jh2bh(jh); |
| 404 | if (!jbd_trylock_bh_state(bh)) { |
| 405 | jbd_sync_bh(journal, bh); |
| 406 | retry = 1; |
| 407 | break; |
| 408 | } |
| 409 | retry = __process_buffer(journal, jh, bhs,&batch_count); |
| 410 | if (retry < 0 && !result) |
| 411 | result = retry; |
| 412 | if (!retry && (need_resched() || |
| 413 | spin_needbreak(&journal->j_list_lock))) { |
| 414 | spin_unlock(&journal->j_list_lock); |
| 415 | retry = 1; |
| 416 | break; |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | if (batch_count) { |
| 421 | if (!retry) { |
| 422 | spin_unlock(&journal->j_list_lock); |
| 423 | retry = 1; |
| 424 | } |
| 425 | __flush_batch(journal, bhs, &batch_count); |
| 426 | } |
| 427 | |
| 428 | if (retry) { |
| 429 | spin_lock(&journal->j_list_lock); |
| 430 | goto restart; |
| 431 | } |
| 432 | /* |
| 433 | * Now we have cleaned up the first transaction's checkpoint |
| 434 | * list. Let's clean up the second one |
| 435 | */ |
| 436 | err = __wait_cp_io(journal, transaction); |
| 437 | if (!result) |
| 438 | result = err; |
| 439 | } |
| 440 | out: |
| 441 | spin_unlock(&journal->j_list_lock); |
| 442 | if (result < 0) |
| 443 | journal_abort(journal, result); |
| 444 | else |
| 445 | result = cleanup_journal_tail(journal); |
| 446 | |
| 447 | return (result < 0) ? result : 0; |
| 448 | } |
| 449 | |
| 450 | /* |
| 451 | * Check the list of checkpoint transactions for the journal to see if |
| 452 | * we have already got rid of any since the last update of the log tail |
| 453 | * in the journal superblock. If so, we can instantly roll the |
| 454 | * superblock forward to remove those transactions from the log. |
| 455 | * |
| 456 | * Return <0 on error, 0 on success, 1 if there was nothing to clean up. |
| 457 | * |
| 458 | * This is the only part of the journaling code which really needs to be |
| 459 | * aware of transaction aborts. Checkpointing involves writing to the |
| 460 | * main filesystem area rather than to the journal, so it can proceed |
| 461 | * even in abort state, but we must not update the super block if |
| 462 | * checkpointing may have failed. Otherwise, we would lose some metadata |
| 463 | * buffers which should be written-back to the filesystem. |
| 464 | */ |
| 465 | |
| 466 | int cleanup_journal_tail(journal_t *journal) |
| 467 | { |
| 468 | transaction_t * transaction; |
| 469 | tid_t first_tid; |
| 470 | unsigned int blocknr, freed; |
| 471 | |
| 472 | if (is_journal_aborted(journal)) |
| 473 | return 1; |
| 474 | |
| 475 | /* |
| 476 | * OK, work out the oldest transaction remaining in the log, and |
| 477 | * the log block it starts at. |
| 478 | * |
| 479 | * If the log is now empty, we need to work out which is the |
| 480 | * next transaction ID we will write, and where it will |
| 481 | * start. |
| 482 | */ |
| 483 | spin_lock(&journal->j_state_lock); |
| 484 | spin_lock(&journal->j_list_lock); |
| 485 | transaction = journal->j_checkpoint_transactions; |
| 486 | if (transaction) { |
| 487 | first_tid = transaction->t_tid; |
| 488 | blocknr = transaction->t_log_start; |
| 489 | } else if ((transaction = journal->j_committing_transaction) != NULL) { |
| 490 | first_tid = transaction->t_tid; |
| 491 | blocknr = transaction->t_log_start; |
| 492 | } else if ((transaction = journal->j_running_transaction) != NULL) { |
| 493 | first_tid = transaction->t_tid; |
| 494 | blocknr = journal->j_head; |
| 495 | } else { |
| 496 | first_tid = journal->j_transaction_sequence; |
| 497 | blocknr = journal->j_head; |
| 498 | } |
| 499 | spin_unlock(&journal->j_list_lock); |
| 500 | J_ASSERT(blocknr != 0); |
| 501 | |
| 502 | /* If the oldest pinned transaction is at the tail of the log |
| 503 | already then there's not much we can do right now. */ |
| 504 | if (journal->j_tail_sequence == first_tid) { |
| 505 | spin_unlock(&journal->j_state_lock); |
| 506 | return 1; |
| 507 | } |
| 508 | spin_unlock(&journal->j_state_lock); |
| 509 | |
| 510 | /* |
| 511 | * We need to make sure that any blocks that were recently written out |
| 512 | * --- perhaps by log_do_checkpoint() --- are flushed out before we |
| 513 | * drop the transactions from the journal. It's unlikely this will be |
| 514 | * necessary, especially with an appropriately sized journal, but we |
| 515 | * need this to guarantee correctness. Fortunately |
| 516 | * cleanup_journal_tail() doesn't get called all that often. |
| 517 | */ |
| 518 | if (journal->j_flags & JFS_BARRIER) |
| 519 | blkdev_issue_flush(journal->j_fs_dev, GFP_KERNEL, NULL); |
| 520 | |
| 521 | spin_lock(&journal->j_state_lock); |
| 522 | if (!tid_gt(first_tid, journal->j_tail_sequence)) { |
| 523 | spin_unlock(&journal->j_state_lock); |
| 524 | /* Someone else cleaned up journal so return 0 */ |
| 525 | return 0; |
| 526 | } |
| 527 | /* OK, update the superblock to recover the freed space. |
| 528 | * Physical blocks come first: have we wrapped beyond the end of |
| 529 | * the log? */ |
| 530 | freed = blocknr - journal->j_tail; |
| 531 | if (blocknr < journal->j_tail) |
| 532 | freed = freed + journal->j_last - journal->j_first; |
| 533 | |
| 534 | trace_jbd_cleanup_journal_tail(journal, first_tid, blocknr, freed); |
| 535 | jbd_debug(1, |
| 536 | "Cleaning journal tail from %d to %d (offset %u), " |
| 537 | "freeing %u\n", |
| 538 | journal->j_tail_sequence, first_tid, blocknr, freed); |
| 539 | |
| 540 | journal->j_free += freed; |
| 541 | journal->j_tail_sequence = first_tid; |
| 542 | journal->j_tail = blocknr; |
| 543 | spin_unlock(&journal->j_state_lock); |
| 544 | if (!(journal->j_flags & JFS_ABORT)) |
| 545 | journal_update_superblock(journal, 1); |
| 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | |
| 550 | /* Checkpoint list management */ |
| 551 | |
| 552 | /* |
| 553 | * journal_clean_one_cp_list |
| 554 | * |
| 555 | * Find all the written-back checkpoint buffers in the given list and release |
| 556 | * them. |
| 557 | * |
| 558 | * Called with j_list_lock held. |
| 559 | * Returns number of buffers reaped (for debug) |
| 560 | */ |
| 561 | |
| 562 | static int journal_clean_one_cp_list(struct journal_head *jh, int *released) |
| 563 | { |
| 564 | struct journal_head *last_jh; |
| 565 | struct journal_head *next_jh = jh; |
| 566 | int ret, freed = 0; |
| 567 | |
| 568 | *released = 0; |
| 569 | if (!jh) |
| 570 | return 0; |
| 571 | |
| 572 | last_jh = jh->b_cpprev; |
| 573 | do { |
| 574 | jh = next_jh; |
| 575 | next_jh = jh->b_cpnext; |
| 576 | /* Use trylock because of the ranking */ |
| 577 | if (jbd_trylock_bh_state(jh2bh(jh))) { |
| 578 | ret = __try_to_free_cp_buf(jh); |
| 579 | if (ret) { |
| 580 | freed++; |
| 581 | if (ret == 2) { |
| 582 | *released = 1; |
| 583 | return freed; |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | /* |
| 588 | * This function only frees up some memory |
| 589 | * if possible so we dont have an obligation |
| 590 | * to finish processing. Bail out if preemption |
| 591 | * requested: |
| 592 | */ |
| 593 | if (need_resched()) |
| 594 | return freed; |
| 595 | } while (jh != last_jh); |
| 596 | |
| 597 | return freed; |
| 598 | } |
| 599 | |
| 600 | /* |
| 601 | * journal_clean_checkpoint_list |
| 602 | * |
| 603 | * Find all the written-back checkpoint buffers in the journal and release them. |
| 604 | * |
| 605 | * Called with the journal locked. |
| 606 | * Called with j_list_lock held. |
| 607 | * Returns number of buffers reaped (for debug) |
| 608 | */ |
| 609 | |
| 610 | int __journal_clean_checkpoint_list(journal_t *journal) |
| 611 | { |
| 612 | transaction_t *transaction, *last_transaction, *next_transaction; |
| 613 | int ret = 0; |
| 614 | int released; |
| 615 | |
| 616 | transaction = journal->j_checkpoint_transactions; |
| 617 | if (!transaction) |
| 618 | goto out; |
| 619 | |
| 620 | last_transaction = transaction->t_cpprev; |
| 621 | next_transaction = transaction; |
| 622 | do { |
| 623 | transaction = next_transaction; |
| 624 | next_transaction = transaction->t_cpnext; |
| 625 | ret += journal_clean_one_cp_list(transaction-> |
| 626 | t_checkpoint_list, &released); |
| 627 | /* |
| 628 | * This function only frees up some memory if possible so we |
| 629 | * dont have an obligation to finish processing. Bail out if |
| 630 | * preemption requested: |
| 631 | */ |
| 632 | if (need_resched()) |
| 633 | goto out; |
| 634 | if (released) |
| 635 | continue; |
| 636 | /* |
| 637 | * It is essential that we are as careful as in the case of |
| 638 | * t_checkpoint_list with removing the buffer from the list as |
| 639 | * we can possibly see not yet submitted buffers on io_list |
| 640 | */ |
| 641 | ret += journal_clean_one_cp_list(transaction-> |
| 642 | t_checkpoint_io_list, &released); |
| 643 | if (need_resched()) |
| 644 | goto out; |
| 645 | } while (transaction != last_transaction); |
| 646 | out: |
| 647 | return ret; |
| 648 | } |
| 649 | |
| 650 | /* |
| 651 | * journal_remove_checkpoint: called after a buffer has been committed |
| 652 | * to disk (either by being write-back flushed to disk, or being |
| 653 | * committed to the log). |
| 654 | * |
| 655 | * We cannot safely clean a transaction out of the log until all of the |
| 656 | * buffer updates committed in that transaction have safely been stored |
| 657 | * elsewhere on disk. To achieve this, all of the buffers in a |
| 658 | * transaction need to be maintained on the transaction's checkpoint |
| 659 | * lists until they have been rewritten, at which point this function is |
| 660 | * called to remove the buffer from the existing transaction's |
| 661 | * checkpoint lists. |
| 662 | * |
| 663 | * The function returns 1 if it frees the transaction, 0 otherwise. |
| 664 | * The function can free jh and bh. |
| 665 | * |
| 666 | * This function is called with j_list_lock held. |
| 667 | * This function is called with jbd_lock_bh_state(jh2bh(jh)) |
| 668 | */ |
| 669 | |
| 670 | int __journal_remove_checkpoint(struct journal_head *jh) |
| 671 | { |
| 672 | transaction_t *transaction; |
| 673 | journal_t *journal; |
| 674 | int ret = 0; |
| 675 | |
| 676 | JBUFFER_TRACE(jh, "entry"); |
| 677 | |
| 678 | if ((transaction = jh->b_cp_transaction) == NULL) { |
| 679 | JBUFFER_TRACE(jh, "not on transaction"); |
| 680 | goto out; |
| 681 | } |
| 682 | journal = transaction->t_journal; |
| 683 | |
| 684 | JBUFFER_TRACE(jh, "removing from transaction"); |
| 685 | __buffer_unlink(jh); |
| 686 | jh->b_cp_transaction = NULL; |
| 687 | journal_put_journal_head(jh); |
| 688 | |
| 689 | if (transaction->t_checkpoint_list != NULL || |
| 690 | transaction->t_checkpoint_io_list != NULL) |
| 691 | goto out; |
| 692 | |
| 693 | /* |
| 694 | * There is one special case to worry about: if we have just pulled the |
| 695 | * buffer off a running or committing transaction's checkpoing list, |
| 696 | * then even if the checkpoint list is empty, the transaction obviously |
| 697 | * cannot be dropped! |
| 698 | * |
| 699 | * The locking here around t_state is a bit sleazy. |
| 700 | * See the comment at the end of journal_commit_transaction(). |
| 701 | */ |
| 702 | if (transaction->t_state != T_FINISHED) |
| 703 | goto out; |
| 704 | |
| 705 | /* OK, that was the last buffer for the transaction: we can now |
| 706 | safely remove this transaction from the log */ |
| 707 | |
| 708 | __journal_drop_transaction(journal, transaction); |
| 709 | |
| 710 | /* Just in case anybody was waiting for more transactions to be |
| 711 | checkpointed... */ |
| 712 | wake_up(&journal->j_wait_logspace); |
| 713 | ret = 1; |
| 714 | out: |
| 715 | return ret; |
| 716 | } |
| 717 | |
| 718 | /* |
| 719 | * journal_insert_checkpoint: put a committed buffer onto a checkpoint |
| 720 | * list so that we know when it is safe to clean the transaction out of |
| 721 | * the log. |
| 722 | * |
| 723 | * Called with the journal locked. |
| 724 | * Called with j_list_lock held. |
| 725 | */ |
| 726 | void __journal_insert_checkpoint(struct journal_head *jh, |
| 727 | transaction_t *transaction) |
| 728 | { |
| 729 | JBUFFER_TRACE(jh, "entry"); |
| 730 | J_ASSERT_JH(jh, buffer_dirty(jh2bh(jh)) || buffer_jbddirty(jh2bh(jh))); |
| 731 | J_ASSERT_JH(jh, jh->b_cp_transaction == NULL); |
| 732 | |
| 733 | /* Get reference for checkpointing transaction */ |
| 734 | journal_grab_journal_head(jh2bh(jh)); |
| 735 | jh->b_cp_transaction = transaction; |
| 736 | |
| 737 | if (!transaction->t_checkpoint_list) { |
| 738 | jh->b_cpnext = jh->b_cpprev = jh; |
| 739 | } else { |
| 740 | jh->b_cpnext = transaction->t_checkpoint_list; |
| 741 | jh->b_cpprev = transaction->t_checkpoint_list->b_cpprev; |
| 742 | jh->b_cpprev->b_cpnext = jh; |
| 743 | jh->b_cpnext->b_cpprev = jh; |
| 744 | } |
| 745 | transaction->t_checkpoint_list = jh; |
| 746 | } |
| 747 | |
| 748 | /* |
| 749 | * We've finished with this transaction structure: adios... |
| 750 | * |
| 751 | * The transaction must have no links except for the checkpoint by this |
| 752 | * point. |
| 753 | * |
| 754 | * Called with the journal locked. |
| 755 | * Called with j_list_lock held. |
| 756 | */ |
| 757 | |
| 758 | void __journal_drop_transaction(journal_t *journal, transaction_t *transaction) |
| 759 | { |
| 760 | assert_spin_locked(&journal->j_list_lock); |
| 761 | if (transaction->t_cpnext) { |
| 762 | transaction->t_cpnext->t_cpprev = transaction->t_cpprev; |
| 763 | transaction->t_cpprev->t_cpnext = transaction->t_cpnext; |
| 764 | if (journal->j_checkpoint_transactions == transaction) |
| 765 | journal->j_checkpoint_transactions = |
| 766 | transaction->t_cpnext; |
| 767 | if (journal->j_checkpoint_transactions == transaction) |
| 768 | journal->j_checkpoint_transactions = NULL; |
| 769 | } |
| 770 | |
| 771 | J_ASSERT(transaction->t_state == T_FINISHED); |
| 772 | J_ASSERT(transaction->t_buffers == NULL); |
| 773 | J_ASSERT(transaction->t_sync_datalist == NULL); |
| 774 | J_ASSERT(transaction->t_forget == NULL); |
| 775 | J_ASSERT(transaction->t_iobuf_list == NULL); |
| 776 | J_ASSERT(transaction->t_shadow_list == NULL); |
| 777 | J_ASSERT(transaction->t_log_list == NULL); |
| 778 | J_ASSERT(transaction->t_checkpoint_list == NULL); |
| 779 | J_ASSERT(transaction->t_checkpoint_io_list == NULL); |
| 780 | J_ASSERT(transaction->t_updates == 0); |
| 781 | J_ASSERT(journal->j_committing_transaction != transaction); |
| 782 | J_ASSERT(journal->j_running_transaction != transaction); |
| 783 | |
| 784 | trace_jbd_drop_transaction(journal, transaction); |
| 785 | jbd_debug(1, "Dropping transaction %d, all done\n", transaction->t_tid); |
| 786 | kfree(transaction); |
| 787 | } |