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