| 1 | |
| 2 | |
| 3 | * linux/fs/jbd2/checkpoint.c
|
| 4 | *
|
| 5 | * Written by Stephen C. Tweedie <[email protected]>, 1999
|
| 6 | *
|
| 7 | * Copyright 1999 Red Hat Software --- All Rights Reserved
|
| 8 | *
|
| 9 | * Checkpoint routines for the generic filesystem journaling code.
|
| 10 | * Part of the ext2fs journaling system.
|
| 11 | *
|
| 12 | * Checkpointing is the process of ensuring that a section of the log is
|
| 13 | * committed fully to disk, so that that portion of the log can be
|
| 14 | * reused.
|
| 15 |
|
| 16 |
|
| 17 | #include <linux/time.h>
|
| 18 | #include <linux/fs.h>
|
| 19 | #include <linux/jbd2.h>
|
| 20 | #include <linux/errno.h>
|
| 21 | #include <linux/slab.h>
|
| 22 | #include <linux/blkdev.h>
|
| 23 | #include <trace/events/jbd2.h>
|
| 24 |
|
| 25 | |
| 26 | * Unlink a buffer from a transaction checkpoint list.
|
| 27 | *
|
| 28 | * Called with j_list_lock held.
|
| 29 |
|
| 30 | static inline void __buffer_unlink(struct journal_head *jh)
|
| 31 | {
|
| 32 | transaction_t *transaction = jh->b_cp_transaction;
|
| 33 |
|
| 34 | jh->b_cpnext->b_cpprev = jh->b_cpprev;
|
| 35 | jh->b_cpprev->b_cpnext = jh->b_cpnext;
|
| 36 | if (transaction->t_checkpoint_list == jh) {
|
| 37 | transaction->t_checkpoint_list = jh->b_cpnext;
|
| 38 | if (transaction->t_checkpoint_list == jh)
|
| 39 | transaction->t_checkpoint_list = NULL;
|
| 40 | }
|
| 41 | }
|
| 42 |
|
| 43 | |
| 44 | * __jbd2_log_wait_for_space: wait until there is space in the journal.
|
| 45 | *
|
| 46 | * Called under j-state_lock *only*. It will be unlocked if we have to wait
|
| 47 | * for a checkpoint to free up some space in the log.
|
| 48 |
|
| 49 | void __jbd2_log_wait_for_space(journal_t *journal)
|
| 50 | __acquires(&journal->j_state_lock)
|
| 51 | __releases(&journal->j_state_lock)
|
| 52 | {
|
| 53 | int nblocks, space_left;
|
| 54 |
|
| 55 |
|
| 56 | nblocks = journal->j_max_transaction_buffers;
|
| 57 | while (jbd2_log_space_left(journal) < nblocks) {
|
| 58 | write_unlock(&journal->j_state_lock);
|
| 59 | mutex_lock_io(&journal->j_checkpoint_mutex);
|
| 60 |
|
| 61 | |
| 62 | * Test again, another process may have checkpointed while we
|
| 63 | * were waiting for the checkpoint lock. If there are no
|
| 64 | * transactions ready to be checkpointed, try to recover
|
| 65 | * journal space by calling cleanup_journal_tail(), and if
|
| 66 | * that doesn't work, by waiting for the currently committing
|
| 67 | * transaction to complete. If there is absolutely no way
|
| 68 | * to make progress, this is either a BUG or corrupted
|
| 69 | * filesystem, so abort the journal and leave a stack
|
| 70 | * trace for forensic evidence.
|
| 71 |
|
| 72 | write_lock(&journal->j_state_lock);
|
| 73 | if (journal->j_flags & JBD2_ABORT) {
|
| 74 | mutex_unlock(&journal->j_checkpoint_mutex);
|
| 75 | return;
|
| 76 | }
|
| 77 | spin_lock(&journal->j_list_lock);
|
| 78 | space_left = jbd2_log_space_left(journal);
|
| 79 | if (space_left < nblocks) {
|
| 80 | int chkpt = journal->j_checkpoint_transactions != NULL;
|
| 81 | tid_t tid = 0;
|
| 82 | bool has_transaction = false;
|
| 83 |
|
| 84 | if (journal->j_committing_transaction) {
|
| 85 | tid = journal->j_committing_transaction->t_tid;
|
| 86 | has_transaction = true;
|
| 87 | }
|
| 88 | spin_unlock(&journal->j_list_lock);
|
| 89 | write_unlock(&journal->j_state_lock);
|
| 90 | if (chkpt) {
|
| 91 | jbd2_log_do_checkpoint(journal);
|
| 92 | } else if (jbd2_cleanup_journal_tail(journal) <= 0) {
|
| 93 | |
| 94 | * We were able to recover space or the
|
| 95 | * journal was aborted due to an error.
|
| 96 |
|
| 97 | ;
|
| 98 | } else if (has_transaction) {
|
| 99 | |
| 100 | * jbd2_journal_commit_transaction() may want
|
| 101 | * to take the checkpoint_mutex if JBD2_FLUSHED
|
| 102 | * is set. So we need to temporarily drop it.
|
| 103 |
|
| 104 | mutex_unlock(&journal->j_checkpoint_mutex);
|
| 105 | jbd2_log_wait_commit(journal, tid);
|
| 106 | write_lock(&journal->j_state_lock);
|
| 107 | continue;
|
| 108 | } else {
|
| 109 | printk(KERN_ERR "%s: needed %d blocks and "
|
| 110 | "only had %d space available\n",
|
| 111 | __func__, nblocks, space_left);
|
| 112 | printk(KERN_ERR "%s: no way to get more "
|
| 113 | "journal space in %s\n", __func__,
|
| 114 | journal->j_devname);
|
| 115 | WARN_ON(1);
|
| 116 | jbd2_journal_abort(journal, -ENOSPC);
|
| 117 | }
|
| 118 | write_lock(&journal->j_state_lock);
|
| 119 | } else {
|
| 120 | spin_unlock(&journal->j_list_lock);
|
| 121 | }
|
| 122 | mutex_unlock(&journal->j_checkpoint_mutex);
|
| 123 | }
|
| 124 | }
|
| 125 |
|
| 126 | static void
|
| 127 | __flush_batch(journal_t *journal, int *batch_count)
|
| 128 | {
|
| 129 | int i;
|
| 130 | struct blk_plug plug;
|
| 131 |
|
| 132 | blk_start_plug(&plug);
|
| 133 | for (i = 0; i < *batch_count; i++)
|
| 134 | write_dirty_buffer(journal->j_chkpt_bhs[i], JBD2_JOURNAL_REQ_FLAGS);
|
| 135 | blk_finish_plug(&plug);
|
| 136 |
|
| 137 | for (i = 0; i < *batch_count; i++) {
|
| 138 | struct buffer_head *bh = journal->j_chkpt_bhs[i];
|
| 139 | BUFFER_TRACE(bh, "brelse");
|
| 140 | __brelse(bh);
|
| 141 | journal->j_chkpt_bhs[i] = NULL;
|
| 142 | }
|
| 143 | *batch_count = 0;
|
| 144 | }
|
| 145 |
|
| 146 | |
| 147 | * Perform an actual checkpoint. We take the first transaction on the
|
| 148 | * list of transactions to be checkpointed and send all its buffers
|
| 149 | * to disk. We submit larger chunks of data at once.
|
| 150 | *
|
| 151 | * The journal should be locked before calling this function.
|
| 152 | * Called with j_checkpoint_mutex held.
|
| 153 |
|
| 154 | int jbd2_log_do_checkpoint(journal_t *journal)
|
| 155 | {
|
| 156 | struct journal_head *jh;
|
| 157 | struct buffer_head *bh;
|
| 158 | transaction_t *transaction;
|
| 159 | tid_t this_tid;
|
| 160 | int result, batch_count = 0;
|
| 161 |
|
| 162 | jbd2_debug(1, "Start checkpoint\n");
|
| 163 |
|
| 164 | |
| 165 | * First thing: if there are any transactions in the log which
|
| 166 | * don't need checkpointing, just eliminate them from the
|
| 167 | * journal straight away.
|
| 168 |
|
| 169 | result = jbd2_cleanup_journal_tail(journal);
|
| 170 | trace_jbd2_checkpoint(journal, result);
|
| 171 | jbd2_debug(1, "cleanup_journal_tail returned %d\n", result);
|
| 172 | if (result <= 0)
|
| 173 | return result;
|
| 174 |
|
| 175 | |
| 176 | * OK, we need to start writing disk blocks. Take one transaction
|
| 177 | * and write it.
|
| 178 |
|
| 179 | spin_lock(&journal->j_list_lock);
|
| 180 | if (!journal->j_checkpoint_transactions)
|
| 181 | goto out;
|
| 182 | transaction = journal->j_checkpoint_transactions;
|
| 183 | if (transaction->t_chp_stats.cs_chp_time == 0)
|
| 184 | transaction->t_chp_stats.cs_chp_time = jiffies;
|
| 185 | this_tid = transaction->t_tid;
|
| 186 | restart:
|
| 187 | |
| 188 | * If someone cleaned up this transaction while we slept, we're
|
| 189 | * done (maybe it's a new transaction, but it fell at the same
|
| 190 | * address).
|
| 191 |
|
| 192 | if (journal->j_checkpoint_transactions != transaction ||
|
| 193 | transaction->t_tid != this_tid)
|
| 194 | goto out;
|
| 195 |
|
| 196 |
|
| 197 | while (transaction->t_checkpoint_list) {
|
| 198 | jh = transaction->t_checkpoint_list;
|
| 199 | bh = jh2bh(jh);
|
| 200 |
|
| 201 | if (jh->b_transaction != NULL) {
|
| 202 | transaction_t *t = jh->b_transaction;
|
| 203 | tid_t tid = t->t_tid;
|
| 204 |
|
| 205 | transaction->t_chp_stats.cs_forced_to_close++;
|
| 206 | spin_unlock(&journal->j_list_lock);
|
| 207 | if (unlikely(journal->j_flags & JBD2_UNMOUNT))
|
| 208 | |
| 209 | * The journal thread is dead; so
|
| 210 | * starting and waiting for a commit
|
| 211 | * to finish will cause us to wait for
|
| 212 | * a _very_ long time.
|
| 213 |
|
| 214 | printk(KERN_ERR
|
| 215 | "JBD2: %s: Waiting for Godot: block %llu\n",
|
| 216 | journal->j_devname, (unsigned long long) bh->b_blocknr);
|
| 217 |
|
| 218 | if (batch_count)
|
| 219 | __flush_batch(journal, &batch_count);
|
| 220 | jbd2_log_start_commit(journal, tid);
|
| 221 | |
| 222 | * jbd2_journal_commit_transaction() may want
|
| 223 | * to take the checkpoint_mutex if JBD2_FLUSHED
|
| 224 | * is set, jbd2_update_log_tail() called by
|
| 225 | * jbd2_journal_commit_transaction() may also take
|
| 226 | * checkpoint_mutex. So we need to temporarily
|
| 227 | * drop it.
|
| 228 |
|
| 229 | mutex_unlock(&journal->j_checkpoint_mutex);
|
| 230 | jbd2_log_wait_commit(journal, tid);
|
| 231 | mutex_lock_io(&journal->j_checkpoint_mutex);
|
| 232 | spin_lock(&journal->j_list_lock);
|
| 233 | goto restart;
|
| 234 | }
|
| 235 | if (!trylock_buffer(bh)) {
|
| 236 | |
| 237 | * The buffer is locked, it may be writing back, or
|
| 238 | * flushing out in the last couple of cycles, or
|
| 239 | * re-adding into a new transaction, need to check
|
| 240 | * it again until it's unlocked.
|
| 241 |
|
| 242 | get_bh(bh);
|
| 243 | spin_unlock(&journal->j_list_lock);
|
| 244 | wait_on_buffer(bh);
|
| 245 |
|
| 246 | BUFFER_TRACE(bh, "brelse");
|
| 247 | __brelse(bh);
|
| 248 | goto retry;
|
| 249 | } else if (!buffer_dirty(bh)) {
|
| 250 | unlock_buffer(bh);
|
| 251 | BUFFER_TRACE(bh, "remove from checkpoint");
|
| 252 | |
| 253 | * If the transaction was released or the checkpoint
|
| 254 | * list was empty, we're done.
|
| 255 |
|
| 256 | if (__jbd2_journal_remove_checkpoint(jh) ||
|
| 257 | !transaction->t_checkpoint_list)
|
| 258 | goto out;
|
| 259 | } else {
|
| 260 | unlock_buffer(bh);
|
| 261 | |
| 262 | * We are about to write the buffer, it could be
|
| 263 | * raced by some other transaction shrink or buffer
|
| 264 | * re-log logic once we release the j_list_lock,
|
| 265 | * leave it on the checkpoint list and check status
|
| 266 | * again to make sure it's clean.
|
| 267 |
|
| 268 | BUFFER_TRACE(bh, "queue");
|
| 269 | get_bh(bh);
|
| 270 | if (WARN_ON_ONCE(buffer_jwrite(bh))) {
|
| 271 | put_bh(bh);
|
| 272 | spin_unlock(&journal->j_list_lock);
|
| 273 |
|
| 274 | if (batch_count)
|
| 275 | __flush_batch(journal, &batch_count);
|
| 276 | jbd2_journal_abort(journal, -EFSCORRUPTED);
|
| 277 | return -EFSCORRUPTED;
|
| 278 | }
|
| 279 | journal->j_chkpt_bhs[batch_count++] = bh;
|
| 280 | transaction->t_chp_stats.cs_written++;
|
| 281 | transaction->t_checkpoint_list = jh->b_cpnext;
|
| 282 | }
|
| 283 |
|
| 284 | if ((batch_count == JBD2_NR_BATCH) ||
|
| 285 | need_resched() || spin_needbreak(&journal->j_list_lock) ||
|
| 286 | jh2bh(transaction->t_checkpoint_list) == journal->j_chkpt_bhs[0])
|
| 287 | goto unlock_and_flush;
|
| 288 | }
|
| 289 |
|
| 290 | if (batch_count) {
|
| 291 | unlock_and_flush:
|
| 292 | spin_unlock(&journal->j_list_lock);
|
| 293 | retry:
|
| 294 | if (batch_count)
|
| 295 | __flush_batch(journal, &batch_count);
|
| 296 | cond_resched();
|
| 297 | spin_lock(&journal->j_list_lock);
|
| 298 | goto restart;
|
| 299 | }
|
| 300 |
|
| 301 | out:
|
| 302 | spin_unlock(&journal->j_list_lock);
|
| 303 | result = jbd2_cleanup_journal_tail(journal);
|
| 304 |
|
| 305 | return (result < 0) ? result : 0;
|
| 306 | }
|
| 307 |
|
| 308 | |
| 309 | * Check the list of checkpoint transactions for the journal to see if
|
| 310 | * we have already got rid of any since the last update of the log tail
|
| 311 | * in the journal superblock. If so, we can instantly roll the
|
| 312 | * superblock forward to remove those transactions from the log.
|
| 313 | *
|
| 314 | * Return <0 on error, 0 on success, 1 if there was nothing to clean up.
|
| 315 | *
|
| 316 | * Called with the journal lock held.
|
| 317 | *
|
| 318 | * This is the only part of the journaling code which really needs to be
|
| 319 | * aware of transaction aborts. Checkpointing involves writing to the
|
| 320 | * main filesystem area rather than to the journal, so it can proceed
|
| 321 | * even in abort state, but we must not update the super block if
|
| 322 | * checkpointing may have failed. Otherwise, we would lose some metadata
|
| 323 | * buffers which should be written-back to the filesystem.
|
| 324 |
|
| 325 |
|
| 326 | int jbd2_cleanup_journal_tail(journal_t *journal)
|
| 327 | {
|
| 328 | tid_t first_tid;
|
| 329 | unsigned long blocknr;
|
| 330 |
|
| 331 | if (is_journal_aborted(journal))
|
| 332 | return -EIO;
|
| 333 |
|
| 334 | if (!jbd2_journal_get_log_tail(journal, &first_tid, &blocknr))
|
| 335 | return 1;
|
| 336 | if (WARN_ON_ONCE(blocknr == 0)) {
|
| 337 | jbd2_journal_abort(journal, -EFSCORRUPTED);
|
| 338 | return -EFSCORRUPTED;
|
| 339 | }
|
| 340 |
|
| 341 | |
| 342 | * We need to make sure that any blocks that were recently written out
|
| 343 | * --- perhaps by jbd2_log_do_checkpoint() --- are flushed out before
|
| 344 | * we drop the transactions from the journal. It's unlikely this will
|
| 345 | * be necessary, especially with an appropriately sized journal, but we
|
| 346 | * need this to guarantee correctness. Fortunately
|
| 347 | * jbd2_cleanup_journal_tail() doesn't get called all that often.
|
| 348 |
|
| 349 | if (journal->j_flags & JBD2_BARRIER)
|
| 350 | blkdev_issue_flush(journal->j_fs_dev);
|
| 351 |
|
| 352 | return __jbd2_update_log_tail(journal, first_tid, blocknr);
|
| 353 | }
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 | |
| 359 | * journal_shrink_one_cp_list
|
| 360 | *
|
| 361 | * Find written-back checkpoint buffers in the given list and try to release
|
| 362 | * them. If 'nr_to_scan' is set, scan at most that many buffers. If the whole
|
| 363 | * transaction is released, set the 'released' parameter. Return the number of
|
| 364 | * released checkpointed buffers.
|
| 365 | *
|
| 366 | * Called with j_list_lock held.
|
| 367 |
|
| 368 | static unsigned long journal_shrink_one_cp_list(struct journal_head *jh,
|
| 369 | enum jbd2_shrink_type type,
|
| 370 | unsigned long *nr_to_scan,
|
| 371 | bool *released)
|
| 372 | {
|
| 373 | struct journal_head *last_jh;
|
| 374 | struct journal_head *next_jh = jh;
|
| 375 | unsigned long nr_freed = 0;
|
| 376 | int ret;
|
| 377 |
|
| 378 | *released = false;
|
| 379 | if (!jh || (nr_to_scan && !*nr_to_scan))
|
| 380 | return 0;
|
| 381 |
|
| 382 | last_jh = jh->b_cpprev;
|
| 383 | do {
|
| 384 | jh = next_jh;
|
| 385 | next_jh = jh->b_cpnext;
|
| 386 | if (nr_to_scan)
|
| 387 | (*nr_to_scan)--;
|
| 388 |
|
| 389 | if (type == JBD2_SHRINK_DESTROY) {
|
| 390 | ret = __jbd2_journal_remove_checkpoint(jh);
|
| 391 | } else {
|
| 392 | ret = jbd2_journal_try_remove_checkpoint(jh);
|
| 393 | if (ret < 0) {
|
| 394 | if (type == JBD2_SHRINK_BUSY_SKIP)
|
| 395 | goto next;
|
| 396 | break;
|
| 397 | }
|
| 398 | }
|
| 399 |
|
| 400 | nr_freed++;
|
| 401 | if (ret) {
|
| 402 | *released = true;
|
| 403 | break;
|
| 404 | }
|
| 405 |
|
| 406 | next:
|
| 407 | if (need_resched())
|
| 408 | break;
|
| 409 | } while (jh != last_jh && (!nr_to_scan || *nr_to_scan));
|
| 410 |
|
| 411 | return nr_freed;
|
| 412 | }
|
| 413 |
|
| 414 | |
| 415 | * jbd2_journal_shrink_checkpoint_list
|
| 416 | *
|
| 417 | * Find 'nr_to_scan' written-back checkpoint buffers in the journal
|
| 418 | * and try to release them. Return the number of released checkpointed
|
| 419 | * buffers.
|
| 420 | *
|
| 421 | * Called with j_list_lock held.
|
| 422 |
|
| 423 | unsigned long jbd2_journal_shrink_checkpoint_list(journal_t *journal,
|
| 424 | unsigned long *nr_to_scan)
|
| 425 | {
|
| 426 | transaction_t *transaction, *last_transaction, *next_transaction;
|
| 427 | bool __maybe_unused released;
|
| 428 | tid_t first_tid = 0, last_tid = 0, next_tid = 0;
|
| 429 | tid_t tid = 0;
|
| 430 | unsigned long nr_freed = 0;
|
| 431 | bool first_set = false;
|
| 432 |
|
| 433 | again:
|
| 434 | spin_lock(&journal->j_list_lock);
|
| 435 | if (!journal->j_checkpoint_transactions) {
|
| 436 | spin_unlock(&journal->j_list_lock);
|
| 437 | goto out;
|
| 438 | }
|
| 439 |
|
| 440 | |
| 441 | * Get next shrink transaction, resume previous scan or start
|
| 442 | * over again. If some others do checkpoint and drop transaction
|
| 443 | * from the checkpoint list, we ignore saved j_shrink_transaction
|
| 444 | * and start over unconditionally.
|
| 445 |
|
| 446 | if (journal->j_shrink_transaction)
|
| 447 | transaction = journal->j_shrink_transaction;
|
| 448 | else
|
| 449 | transaction = journal->j_checkpoint_transactions;
|
| 450 |
|
| 451 | if (!first_set) {
|
| 452 | first_tid = transaction->t_tid;
|
| 453 | first_set = true;
|
| 454 | }
|
| 455 | last_transaction = journal->j_checkpoint_transactions->t_cpprev;
|
| 456 | next_transaction = transaction;
|
| 457 | last_tid = last_transaction->t_tid;
|
| 458 | do {
|
| 459 | transaction = next_transaction;
|
| 460 | next_transaction = transaction->t_cpnext;
|
| 461 | tid = transaction->t_tid;
|
| 462 |
|
| 463 | nr_freed += journal_shrink_one_cp_list(transaction->t_checkpoint_list,
|
| 464 | JBD2_SHRINK_BUSY_SKIP,
|
| 465 | nr_to_scan, &released);
|
| 466 | if (*nr_to_scan == 0)
|
| 467 | break;
|
| 468 | if (need_resched() || spin_needbreak(&journal->j_list_lock))
|
| 469 | break;
|
| 470 | } while (transaction != last_transaction);
|
| 471 |
|
| 472 | if (transaction != last_transaction) {
|
| 473 | journal->j_shrink_transaction = next_transaction;
|
| 474 | next_tid = next_transaction->t_tid;
|
| 475 | } else {
|
| 476 | journal->j_shrink_transaction = NULL;
|
| 477 | next_tid = 0;
|
| 478 | }
|
| 479 |
|
| 480 | spin_unlock(&journal->j_list_lock);
|
| 481 | cond_resched();
|
| 482 |
|
| 483 | if (*nr_to_scan && journal->j_shrink_transaction)
|
| 484 | goto again;
|
| 485 | out:
|
| 486 | trace_jbd2_shrink_checkpoint_list(journal, first_tid, tid, last_tid,
|
| 487 | nr_freed, next_tid);
|
| 488 |
|
| 489 | return nr_freed;
|
| 490 | }
|
| 491 |
|
| 492 | |
| 493 | * journal_clean_checkpoint_list
|
| 494 | *
|
| 495 | * Find all the written-back checkpoint buffers in the journal and release them.
|
| 496 | * If 'type' is JBD2_SHRINK_DESTROY, release all buffers unconditionally. If
|
| 497 | * 'type' is JBD2_SHRINK_BUSY_STOP, will stop release buffers if encounters a
|
| 498 | * busy buffer. To avoid wasting CPU cycles scanning the buffer list in some
|
| 499 | * cases, don't pass JBD2_SHRINK_BUSY_SKIP 'type' for this function.
|
| 500 | *
|
| 501 | * Called with j_list_lock held.
|
| 502 |
|
| 503 | void __jbd2_journal_clean_checkpoint_list(journal_t *journal,
|
| 504 | enum jbd2_shrink_type type)
|
| 505 | {
|
| 506 | transaction_t *transaction, *last_transaction, *next_transaction;
|
| 507 | bool released;
|
| 508 |
|
| 509 | WARN_ON_ONCE(type == JBD2_SHRINK_BUSY_SKIP);
|
| 510 |
|
| 511 | transaction = journal->j_checkpoint_transactions;
|
| 512 | if (!transaction)
|
| 513 | return;
|
| 514 |
|
| 515 | last_transaction = transaction->t_cpprev;
|
| 516 | next_transaction = transaction;
|
| 517 | do {
|
| 518 | transaction = next_transaction;
|
| 519 | next_transaction = transaction->t_cpnext;
|
| 520 | journal_shrink_one_cp_list(transaction->t_checkpoint_list,
|
| 521 | type, NULL, &released);
|
| 522 | |
| 523 | * This function only frees up some memory if possible so we
|
| 524 | * dont have an obligation to finish processing. Bail out if
|
| 525 | * preemption requested:
|
| 526 |
|
| 527 | if (need_resched())
|
| 528 | return;
|
| 529 | |
| 530 | * Stop scanning if we couldn't free the transaction. This
|
| 531 | * avoids pointless scanning of transactions which still
|
| 532 | * weren't checkpointed.
|
| 533 |
|
| 534 | if (!released)
|
| 535 | return;
|
| 536 | } while (transaction != last_transaction);
|
| 537 | }
|
| 538 |
|
| 539 | |
| 540 | * Remove buffers from all checkpoint lists as journal is aborted and we just
|
| 541 | * need to free memory
|
| 542 |
|
| 543 | void jbd2_journal_destroy_checkpoint(journal_t *journal)
|
| 544 | {
|
| 545 | |
| 546 | * We loop because __jbd2_journal_clean_checkpoint_list() may abort
|
| 547 | * early due to a need of rescheduling.
|
| 548 |
|
| 549 | while (1) {
|
| 550 | spin_lock(&journal->j_list_lock);
|
| 551 | if (!journal->j_checkpoint_transactions) {
|
| 552 | spin_unlock(&journal->j_list_lock);
|
| 553 | break;
|
| 554 | }
|
| 555 | __jbd2_journal_clean_checkpoint_list(journal, JBD2_SHRINK_DESTROY);
|
| 556 | spin_unlock(&journal->j_list_lock);
|
| 557 | cond_resched();
|
| 558 | }
|
| 559 | }
|
| 560 |
|
| 561 | |
| 562 | * journal_remove_checkpoint: called after a buffer has been committed
|
| 563 | * to disk (either by being write-back flushed to disk, or being
|
| 564 | * committed to the log).
|
| 565 | *
|
| 566 | * We cannot safely clean a transaction out of the log until all of the
|
| 567 | * buffer updates committed in that transaction have safely been stored
|
| 568 | * elsewhere on disk. To achieve this, all of the buffers in a
|
| 569 | * transaction need to be maintained on the transaction's checkpoint
|
| 570 | * lists until they have been rewritten, at which point this function is
|
| 571 | * called to remove the buffer from the existing transaction's
|
| 572 | * checkpoint lists.
|
| 573 | *
|
| 574 | * The function returns 1 if it frees the transaction, 0 otherwise.
|
| 575 | * The function can free jh and bh.
|
| 576 | *
|
| 577 | * This function is called with j_list_lock held.
|
| 578 |
|
| 579 | int __jbd2_journal_remove_checkpoint(struct journal_head *jh)
|
| 580 | {
|
| 581 | struct transaction_chp_stats_s *stats;
|
| 582 | transaction_t *transaction;
|
| 583 | journal_t *journal;
|
| 584 |
|
| 585 | JBUFFER_TRACE(jh, "entry");
|
| 586 |
|
| 587 | transaction = jh->b_cp_transaction;
|
| 588 | if (!transaction) {
|
| 589 | JBUFFER_TRACE(jh, "not on transaction");
|
| 590 | return 0;
|
| 591 | }
|
| 592 | journal = transaction->t_journal;
|
| 593 |
|
| 594 | JBUFFER_TRACE(jh, "removing from transaction");
|
| 595 |
|
| 596 | __buffer_unlink(jh);
|
| 597 | jh->b_cp_transaction = NULL;
|
| 598 | percpu_counter_dec(&journal->j_checkpoint_jh_count);
|
| 599 | jbd2_journal_put_journal_head(jh);
|
| 600 |
|
| 601 |
|
| 602 | if (transaction->t_checkpoint_list)
|
| 603 | return 0;
|
| 604 |
|
| 605 | |
| 606 | * There is one special case to worry about: if we have just pulled the
|
| 607 | * buffer off a running or committing transaction's checkpoing list,
|
| 608 | * then even if the checkpoint list is empty, the transaction obviously
|
| 609 | * cannot be dropped!
|
| 610 | *
|
| 611 | * The locking here around t_state is a bit sleazy.
|
| 612 | * See the comment at the end of jbd2_journal_commit_transaction().
|
| 613 |
|
| 614 | if (transaction->t_state != T_FINISHED)
|
| 615 | return 0;
|
| 616 |
|
| 617 | |
| 618 | * OK, that was the last buffer for the transaction, we can now
|
| 619 | * safely remove this transaction from the log.
|
| 620 |
|
| 621 | stats = &transaction->t_chp_stats;
|
| 622 | if (stats->cs_chp_time)
|
| 623 | stats->cs_chp_time = jbd2_time_diff(stats->cs_chp_time,
|
| 624 | jiffies);
|
| 625 | trace_jbd2_checkpoint_stats(journal->j_fs_dev->bd_dev,
|
| 626 | transaction->t_tid, stats);
|
| 627 |
|
| 628 | __jbd2_journal_drop_transaction(journal, transaction);
|
| 629 | jbd2_journal_free_transaction(transaction);
|
| 630 | return 1;
|
| 631 | }
|
| 632 |
|
| 633 | |
| 634 | * Check the checkpoint buffer and try to remove it from the checkpoint
|
| 635 | * list if it's clean. Returns -EBUSY if it is not clean, returns 1 if
|
| 636 | * it frees the transaction, 0 otherwise.
|
| 637 | *
|
| 638 | * This function is called with j_list_lock held.
|
| 639 |
|
| 640 | int jbd2_journal_try_remove_checkpoint(struct journal_head *jh)
|
| 641 | {
|
| 642 | struct buffer_head *bh = jh2bh(jh);
|
| 643 |
|
| 644 | if (jh->b_transaction)
|
| 645 | return -EBUSY;
|
| 646 | if (!trylock_buffer(bh))
|
| 647 | return -EBUSY;
|
| 648 | if (buffer_dirty(bh)) {
|
| 649 | unlock_buffer(bh);
|
| 650 | return -EBUSY;
|
| 651 | }
|
| 652 | unlock_buffer(bh);
|
| 653 |
|
| 654 | |
| 655 | * Buffer is clean and the IO has finished (we held the buffer
|
| 656 | * lock) so the checkpoint is done. We can safely remove the
|
| 657 | * buffer from this transaction.
|
| 658 |
|
| 659 | JBUFFER_TRACE(jh, "remove from checkpoint list");
|
| 660 | return __jbd2_journal_remove_checkpoint(jh);
|
| 661 | }
|
| 662 |
|
| 663 | |
| 664 | * journal_insert_checkpoint: put a committed buffer onto a checkpoint
|
| 665 | * list so that we know when it is safe to clean the transaction out of
|
| 666 | * the log.
|
| 667 | *
|
| 668 | * Called with the journal locked.
|
| 669 | * Called with j_list_lock held.
|
| 670 |
|
| 671 | void __jbd2_journal_insert_checkpoint(struct journal_head *jh,
|
| 672 | transaction_t *transaction)
|
| 673 | {
|
| 674 | JBUFFER_TRACE(jh, "entry");
|
| 675 | J_ASSERT_JH(jh, buffer_dirty(jh2bh(jh)) || buffer_jbddirty(jh2bh(jh)));
|
| 676 | J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
|
| 677 |
|
| 678 |
|
| 679 | jbd2_journal_grab_journal_head(jh2bh(jh));
|
| 680 | jh->b_cp_transaction = transaction;
|
| 681 |
|
| 682 | if (!transaction->t_checkpoint_list) {
|
| 683 | jh->b_cpnext = jh->b_cpprev = jh;
|
| 684 | } else {
|
| 685 | jh->b_cpnext = transaction->t_checkpoint_list;
|
| 686 | jh->b_cpprev = transaction->t_checkpoint_list->b_cpprev;
|
| 687 | jh->b_cpprev->b_cpnext = jh;
|
| 688 | jh->b_cpnext->b_cpprev = jh;
|
| 689 | }
|
| 690 | transaction->t_checkpoint_list = jh;
|
| 691 | percpu_counter_inc(&transaction->t_journal->j_checkpoint_jh_count);
|
| 692 | }
|
| 693 |
|
| 694 | |
| 695 | * We've finished with this transaction structure: adios...
|
| 696 | *
|
| 697 | * The transaction must have no links except for the checkpoint by this
|
| 698 | * point.
|
| 699 | *
|
| 700 | * Called with the journal locked.
|
| 701 | * Called with j_list_lock held.
|
| 702 |
|
| 703 |
|
| 704 | void __jbd2_journal_drop_transaction(journal_t *journal, transaction_t *transaction)
|
| 705 | {
|
| 706 | assert_spin_locked(&journal->j_list_lock);
|
| 707 |
|
| 708 | journal->j_shrink_transaction = NULL;
|
| 709 | if (transaction->t_cpnext) {
|
| 710 | transaction->t_cpnext->t_cpprev = transaction->t_cpprev;
|
| 711 | transaction->t_cpprev->t_cpnext = transaction->t_cpnext;
|
| 712 | if (journal->j_checkpoint_transactions == transaction)
|
| 713 | journal->j_checkpoint_transactions =
|
| 714 | transaction->t_cpnext;
|
| 715 | if (journal->j_checkpoint_transactions == transaction)
|
| 716 | journal->j_checkpoint_transactions = NULL;
|
| 717 | }
|
| 718 |
|
| 719 | J_ASSERT(transaction->t_state == T_FINISHED);
|
| 720 | J_ASSERT(transaction->t_buffers == NULL);
|
| 721 | J_ASSERT(transaction->t_forget == NULL);
|
| 722 | J_ASSERT(transaction->t_shadow_list == NULL);
|
| 723 | J_ASSERT(transaction->t_checkpoint_list == NULL);
|
| 724 | J_ASSERT(atomic_read(&transaction->t_updates) == 0);
|
| 725 | J_ASSERT(journal->j_committing_transaction != transaction);
|
| 726 | J_ASSERT(journal->j_running_transaction != transaction);
|
| 727 |
|
| 728 | trace_jbd2_drop_transaction(journal, transaction);
|
| 729 |
|
| 730 | jbd2_debug(1, "Dropping transaction %d, all done\n", transaction->t_tid);
|
| 731 | }
|