Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Uwe Kleine-König | 5886269 | 2007-05-09 07:51:49 +0200 | [diff] [blame] | 2 | * linux/fs/jbd/transaction.c |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 3 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * Written by Stephen C. Tweedie <sct@redhat.com>, 1998 |
| 5 | * |
| 6 | * Copyright 1998 Red Hat corp --- 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 | * Generic filesystem transaction handling code; part of the ext2fs |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 13 | * journaling system. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * |
| 15 | * This file manages transactions (compound commits managed by the |
| 16 | * journaling code) and handles (individual atomic operations by the |
| 17 | * filesystem). |
| 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/timer.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/mm.h> |
| 27 | #include <linux/highmem.h> |
| 28 | |
Adrian Bunk | d394e12 | 2006-12-06 20:38:26 -0800 | [diff] [blame] | 29 | static void __journal_temp_unlink_buffer(struct journal_head *jh); |
| 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | /* |
| 32 | * get_transaction: obtain a new transaction_t object. |
| 33 | * |
| 34 | * Simply allocate and initialise a new transaction. Create it in |
| 35 | * RUNNING state and add it to the current journal (which should not |
| 36 | * have an existing running transaction: we only make a new transaction |
| 37 | * once we have started to commit the old one). |
| 38 | * |
| 39 | * Preconditions: |
| 40 | * The journal MUST be locked. We don't perform atomic mallocs on the |
| 41 | * new transaction and we can't block without protecting against other |
| 42 | * processes trying to touch the journal while it is in transition. |
| 43 | * |
| 44 | * Called under j_state_lock |
| 45 | */ |
| 46 | |
| 47 | static transaction_t * |
| 48 | get_transaction(journal_t *journal, transaction_t *transaction) |
| 49 | { |
| 50 | transaction->t_journal = journal; |
| 51 | transaction->t_state = T_RUNNING; |
| 52 | transaction->t_tid = journal->j_transaction_sequence++; |
| 53 | transaction->t_expires = jiffies + journal->j_commit_interval; |
| 54 | spin_lock_init(&transaction->t_handle_lock); |
| 55 | |
| 56 | /* Set up the commit timer for the new transaction. */ |
Arjan van de Ven | 44d306e | 2006-12-10 02:21:26 -0800 | [diff] [blame] | 57 | journal->j_commit_timer.expires = round_jiffies(transaction->t_expires); |
Andrew Morton | e3df189 | 2006-03-25 03:06:53 -0800 | [diff] [blame] | 58 | add_timer(&journal->j_commit_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
| 60 | J_ASSERT(journal->j_running_transaction == NULL); |
| 61 | journal->j_running_transaction = transaction; |
| 62 | |
| 63 | return transaction; |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * Handle management. |
| 68 | * |
| 69 | * A handle_t is an object which represents a single atomic update to a |
| 70 | * filesystem, and which tracks all of the modifications which form part |
| 71 | * of that one update. |
| 72 | */ |
| 73 | |
| 74 | /* |
| 75 | * start_this_handle: Given a handle, deal with any locking or stalling |
| 76 | * needed to make sure that there is enough journal space for the handle |
| 77 | * to begin. Attach the handle to a transaction and set up the |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 78 | * transaction's buffer credits. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | */ |
| 80 | |
| 81 | static int start_this_handle(journal_t *journal, handle_t *handle) |
| 82 | { |
| 83 | transaction_t *transaction; |
| 84 | int needed; |
| 85 | int nblocks = handle->h_buffer_credits; |
| 86 | transaction_t *new_transaction = NULL; |
| 87 | int ret = 0; |
| 88 | |
| 89 | if (nblocks > journal->j_max_transaction_buffers) { |
| 90 | printk(KERN_ERR "JBD: %s wants too many credits (%d > %d)\n", |
| 91 | current->comm, nblocks, |
| 92 | journal->j_max_transaction_buffers); |
| 93 | ret = -ENOSPC; |
| 94 | goto out; |
| 95 | } |
| 96 | |
| 97 | alloc_transaction: |
| 98 | if (!journal->j_running_transaction) { |
| 99 | new_transaction = jbd_kmalloc(sizeof(*new_transaction), |
| 100 | GFP_NOFS); |
| 101 | if (!new_transaction) { |
| 102 | ret = -ENOMEM; |
| 103 | goto out; |
| 104 | } |
| 105 | memset(new_transaction, 0, sizeof(*new_transaction)); |
| 106 | } |
| 107 | |
| 108 | jbd_debug(3, "New handle %p going live.\n", handle); |
| 109 | |
| 110 | repeat: |
| 111 | |
| 112 | /* |
| 113 | * We need to hold j_state_lock until t_updates has been incremented, |
| 114 | * for proper journal barrier handling |
| 115 | */ |
| 116 | spin_lock(&journal->j_state_lock); |
| 117 | repeat_locked: |
| 118 | if (is_journal_aborted(journal) || |
| 119 | (journal->j_errno != 0 && !(journal->j_flags & JFS_ACK_ERR))) { |
| 120 | spin_unlock(&journal->j_state_lock); |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 121 | ret = -EROFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | goto out; |
| 123 | } |
| 124 | |
| 125 | /* Wait on the journal's transaction barrier if necessary */ |
| 126 | if (journal->j_barrier_count) { |
| 127 | spin_unlock(&journal->j_state_lock); |
| 128 | wait_event(journal->j_wait_transaction_locked, |
| 129 | journal->j_barrier_count == 0); |
| 130 | goto repeat; |
| 131 | } |
| 132 | |
| 133 | if (!journal->j_running_transaction) { |
| 134 | if (!new_transaction) { |
| 135 | spin_unlock(&journal->j_state_lock); |
| 136 | goto alloc_transaction; |
| 137 | } |
| 138 | get_transaction(journal, new_transaction); |
| 139 | new_transaction = NULL; |
| 140 | } |
| 141 | |
| 142 | transaction = journal->j_running_transaction; |
| 143 | |
| 144 | /* |
| 145 | * If the current transaction is locked down for commit, wait for the |
| 146 | * lock to be released. |
| 147 | */ |
| 148 | if (transaction->t_state == T_LOCKED) { |
| 149 | DEFINE_WAIT(wait); |
| 150 | |
| 151 | prepare_to_wait(&journal->j_wait_transaction_locked, |
| 152 | &wait, TASK_UNINTERRUPTIBLE); |
| 153 | spin_unlock(&journal->j_state_lock); |
| 154 | schedule(); |
| 155 | finish_wait(&journal->j_wait_transaction_locked, &wait); |
| 156 | goto repeat; |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * If there is not enough space left in the log to write all potential |
| 161 | * buffers requested by this operation, we need to stall pending a log |
| 162 | * checkpoint to free some more log space. |
| 163 | */ |
| 164 | spin_lock(&transaction->t_handle_lock); |
| 165 | needed = transaction->t_outstanding_credits + nblocks; |
| 166 | |
| 167 | if (needed > journal->j_max_transaction_buffers) { |
| 168 | /* |
| 169 | * If the current transaction is already too large, then start |
| 170 | * to commit it: we can then go back and attach this handle to |
| 171 | * a new transaction. |
| 172 | */ |
| 173 | DEFINE_WAIT(wait); |
| 174 | |
| 175 | jbd_debug(2, "Handle %p starting new commit...\n", handle); |
| 176 | spin_unlock(&transaction->t_handle_lock); |
| 177 | prepare_to_wait(&journal->j_wait_transaction_locked, &wait, |
| 178 | TASK_UNINTERRUPTIBLE); |
| 179 | __log_start_commit(journal, transaction->t_tid); |
| 180 | spin_unlock(&journal->j_state_lock); |
| 181 | schedule(); |
| 182 | finish_wait(&journal->j_wait_transaction_locked, &wait); |
| 183 | goto repeat; |
| 184 | } |
| 185 | |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 186 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | * The commit code assumes that it can get enough log space |
| 188 | * without forcing a checkpoint. This is *critical* for |
| 189 | * correctness: a checkpoint of a buffer which is also |
| 190 | * associated with a committing transaction creates a deadlock, |
| 191 | * so commit simply cannot force through checkpoints. |
| 192 | * |
| 193 | * We must therefore ensure the necessary space in the journal |
| 194 | * *before* starting to dirty potentially checkpointed buffers |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 195 | * in the new transaction. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | * |
| 197 | * The worst part is, any transaction currently committing can |
| 198 | * reduce the free space arbitrarily. Be careful to account for |
| 199 | * those buffers when checkpointing. |
| 200 | */ |
| 201 | |
| 202 | /* |
| 203 | * @@@ AKPM: This seems rather over-defensive. We're giving commit |
| 204 | * a _lot_ of headroom: 1/4 of the journal plus the size of |
| 205 | * the committing transaction. Really, we only need to give it |
| 206 | * committing_transaction->t_outstanding_credits plus "enough" for |
| 207 | * the log control blocks. |
| 208 | * Also, this test is inconsitent with the matching one in |
| 209 | * journal_extend(). |
| 210 | */ |
| 211 | if (__log_space_left(journal) < jbd_space_needed(journal)) { |
| 212 | jbd_debug(2, "Handle %p waiting for checkpoint...\n", handle); |
| 213 | spin_unlock(&transaction->t_handle_lock); |
| 214 | __log_wait_for_space(journal); |
| 215 | goto repeat_locked; |
| 216 | } |
| 217 | |
| 218 | /* OK, account for the buffers that this operation expects to |
| 219 | * use and add the handle to the running transaction. */ |
| 220 | |
| 221 | handle->h_transaction = transaction; |
| 222 | transaction->t_outstanding_credits += nblocks; |
| 223 | transaction->t_updates++; |
| 224 | transaction->t_handle_count++; |
| 225 | jbd_debug(4, "Handle %p given %d credits (total %d, free %d)\n", |
| 226 | handle, nblocks, transaction->t_outstanding_credits, |
| 227 | __log_space_left(journal)); |
| 228 | spin_unlock(&transaction->t_handle_lock); |
| 229 | spin_unlock(&journal->j_state_lock); |
| 230 | out: |
Andrew Morton | 304c4c8 | 2006-06-23 02:05:31 -0700 | [diff] [blame] | 231 | if (unlikely(new_transaction)) /* It's usually NULL */ |
| 232 | kfree(new_transaction); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | return ret; |
| 234 | } |
| 235 | |
Peter Zijlstra | 34a3d1e | 2007-10-11 22:11:12 +0200 | [diff] [blame] | 236 | static struct lock_class_key jbd_handle_key; |
| 237 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | /* Allocate a new handle. This should probably be in a slab... */ |
| 239 | static handle_t *new_handle(int nblocks) |
| 240 | { |
| 241 | handle_t *handle = jbd_alloc_handle(GFP_NOFS); |
| 242 | if (!handle) |
| 243 | return NULL; |
| 244 | memset(handle, 0, sizeof(*handle)); |
| 245 | handle->h_buffer_credits = nblocks; |
| 246 | handle->h_ref = 1; |
| 247 | |
Peter Zijlstra | 34a3d1e | 2007-10-11 22:11:12 +0200 | [diff] [blame] | 248 | lockdep_init_map(&handle->h_lockdep_map, "jbd_handle", &jbd_handle_key, 0); |
| 249 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | return handle; |
| 251 | } |
| 252 | |
| 253 | /** |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 254 | * handle_t *journal_start() - Obtain a new handle. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | * @journal: Journal to start transaction on. |
| 256 | * @nblocks: number of block buffer we might modify |
| 257 | * |
| 258 | * We make sure that the transaction can guarantee at least nblocks of |
| 259 | * modified buffers in the log. We block until the log can guarantee |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 260 | * that much space. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | * |
| 262 | * This function is visible to journal users (like ext3fs), so is not |
| 263 | * called with the journal already locked. |
| 264 | * |
| 265 | * Return a pointer to a newly allocated handle, or NULL on failure |
| 266 | */ |
| 267 | handle_t *journal_start(journal_t *journal, int nblocks) |
| 268 | { |
| 269 | handle_t *handle = journal_current_handle(); |
| 270 | int err; |
| 271 | |
| 272 | if (!journal) |
| 273 | return ERR_PTR(-EROFS); |
| 274 | |
| 275 | if (handle) { |
| 276 | J_ASSERT(handle->h_transaction->t_journal == journal); |
| 277 | handle->h_ref++; |
| 278 | return handle; |
| 279 | } |
| 280 | |
| 281 | handle = new_handle(nblocks); |
| 282 | if (!handle) |
| 283 | return ERR_PTR(-ENOMEM); |
| 284 | |
| 285 | current->journal_info = handle; |
| 286 | |
| 287 | err = start_this_handle(journal, handle); |
| 288 | if (err < 0) { |
| 289 | jbd_free_handle(handle); |
| 290 | current->journal_info = NULL; |
| 291 | handle = ERR_PTR(err); |
| 292 | } |
Peter Zijlstra | 34a3d1e | 2007-10-11 22:11:12 +0200 | [diff] [blame] | 293 | |
| 294 | lock_acquire(&handle->h_lockdep_map, 0, 0, 0, 2, _THIS_IP_); |
| 295 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | return handle; |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * int journal_extend() - extend buffer credits. |
| 301 | * @handle: handle to 'extend' |
| 302 | * @nblocks: nr blocks to try to extend by. |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 303 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | * Some transactions, such as large extends and truncates, can be done |
| 305 | * atomically all at once or in several stages. The operation requests |
| 306 | * a credit for a number of buffer modications in advance, but can |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 307 | * extend its credit if it needs more. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | * |
| 309 | * journal_extend tries to give the running handle more buffer credits. |
| 310 | * It does not guarantee that allocation - this is a best-effort only. |
| 311 | * The calling process MUST be able to deal cleanly with a failure to |
| 312 | * extend here. |
| 313 | * |
| 314 | * Return 0 on success, non-zero on failure. |
| 315 | * |
| 316 | * return code < 0 implies an error |
| 317 | * return code > 0 implies normal transaction-full status. |
| 318 | */ |
| 319 | int journal_extend(handle_t *handle, int nblocks) |
| 320 | { |
| 321 | transaction_t *transaction = handle->h_transaction; |
| 322 | journal_t *journal = transaction->t_journal; |
| 323 | int result; |
| 324 | int wanted; |
| 325 | |
| 326 | result = -EIO; |
| 327 | if (is_handle_aborted(handle)) |
| 328 | goto out; |
| 329 | |
| 330 | result = 1; |
| 331 | |
| 332 | spin_lock(&journal->j_state_lock); |
| 333 | |
| 334 | /* Don't extend a locked-down transaction! */ |
| 335 | if (handle->h_transaction->t_state != T_RUNNING) { |
| 336 | jbd_debug(3, "denied handle %p %d blocks: " |
| 337 | "transaction not running\n", handle, nblocks); |
| 338 | goto error_out; |
| 339 | } |
| 340 | |
| 341 | spin_lock(&transaction->t_handle_lock); |
| 342 | wanted = transaction->t_outstanding_credits + nblocks; |
| 343 | |
| 344 | if (wanted > journal->j_max_transaction_buffers) { |
| 345 | jbd_debug(3, "denied handle %p %d blocks: " |
| 346 | "transaction too large\n", handle, nblocks); |
| 347 | goto unlock; |
| 348 | } |
| 349 | |
| 350 | if (wanted > __log_space_left(journal)) { |
| 351 | jbd_debug(3, "denied handle %p %d blocks: " |
| 352 | "insufficient log space\n", handle, nblocks); |
| 353 | goto unlock; |
| 354 | } |
| 355 | |
| 356 | handle->h_buffer_credits += nblocks; |
| 357 | transaction->t_outstanding_credits += nblocks; |
| 358 | result = 0; |
| 359 | |
| 360 | jbd_debug(3, "extended handle %p by %d\n", handle, nblocks); |
| 361 | unlock: |
| 362 | spin_unlock(&transaction->t_handle_lock); |
| 363 | error_out: |
| 364 | spin_unlock(&journal->j_state_lock); |
| 365 | out: |
| 366 | return result; |
| 367 | } |
| 368 | |
| 369 | |
| 370 | /** |
| 371 | * int journal_restart() - restart a handle . |
| 372 | * @handle: handle to restart |
| 373 | * @nblocks: nr credits requested |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 374 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | * Restart a handle for a multi-transaction filesystem |
| 376 | * operation. |
| 377 | * |
| 378 | * If the journal_extend() call above fails to grant new buffer credits |
| 379 | * to a running handle, a call to journal_restart will commit the |
| 380 | * handle's transaction so far and reattach the handle to a new |
| 381 | * transaction capabable of guaranteeing the requested number of |
| 382 | * credits. |
| 383 | */ |
| 384 | |
| 385 | int journal_restart(handle_t *handle, int nblocks) |
| 386 | { |
| 387 | transaction_t *transaction = handle->h_transaction; |
| 388 | journal_t *journal = transaction->t_journal; |
| 389 | int ret; |
| 390 | |
| 391 | /* If we've had an abort of any type, don't even think about |
| 392 | * actually doing the restart! */ |
| 393 | if (is_handle_aborted(handle)) |
| 394 | return 0; |
| 395 | |
| 396 | /* |
| 397 | * First unlink the handle from its current transaction, and start the |
| 398 | * commit on that. |
| 399 | */ |
| 400 | J_ASSERT(transaction->t_updates > 0); |
| 401 | J_ASSERT(journal_current_handle() == handle); |
| 402 | |
| 403 | spin_lock(&journal->j_state_lock); |
| 404 | spin_lock(&transaction->t_handle_lock); |
| 405 | transaction->t_outstanding_credits -= handle->h_buffer_credits; |
| 406 | transaction->t_updates--; |
| 407 | |
| 408 | if (!transaction->t_updates) |
| 409 | wake_up(&journal->j_wait_updates); |
| 410 | spin_unlock(&transaction->t_handle_lock); |
| 411 | |
| 412 | jbd_debug(2, "restarting handle %p\n", handle); |
| 413 | __log_start_commit(journal, transaction->t_tid); |
| 414 | spin_unlock(&journal->j_state_lock); |
| 415 | |
| 416 | handle->h_buffer_credits = nblocks; |
| 417 | ret = start_this_handle(journal, handle); |
| 418 | return ret; |
| 419 | } |
| 420 | |
| 421 | |
| 422 | /** |
| 423 | * void journal_lock_updates () - establish a transaction barrier. |
| 424 | * @journal: Journal to establish a barrier on. |
| 425 | * |
| 426 | * This locks out any further updates from being started, and blocks |
| 427 | * until all existing updates have completed, returning only once the |
| 428 | * journal is in a quiescent state with no updates running. |
| 429 | * |
| 430 | * The journal lock should not be held on entry. |
| 431 | */ |
| 432 | void journal_lock_updates(journal_t *journal) |
| 433 | { |
| 434 | DEFINE_WAIT(wait); |
| 435 | |
| 436 | spin_lock(&journal->j_state_lock); |
| 437 | ++journal->j_barrier_count; |
| 438 | |
| 439 | /* Wait until there are no running updates */ |
| 440 | while (1) { |
| 441 | transaction_t *transaction = journal->j_running_transaction; |
| 442 | |
| 443 | if (!transaction) |
| 444 | break; |
| 445 | |
| 446 | spin_lock(&transaction->t_handle_lock); |
| 447 | if (!transaction->t_updates) { |
| 448 | spin_unlock(&transaction->t_handle_lock); |
| 449 | break; |
| 450 | } |
| 451 | prepare_to_wait(&journal->j_wait_updates, &wait, |
| 452 | TASK_UNINTERRUPTIBLE); |
| 453 | spin_unlock(&transaction->t_handle_lock); |
| 454 | spin_unlock(&journal->j_state_lock); |
| 455 | schedule(); |
| 456 | finish_wait(&journal->j_wait_updates, &wait); |
| 457 | spin_lock(&journal->j_state_lock); |
| 458 | } |
| 459 | spin_unlock(&journal->j_state_lock); |
| 460 | |
| 461 | /* |
| 462 | * We have now established a barrier against other normal updates, but |
| 463 | * we also need to barrier against other journal_lock_updates() calls |
| 464 | * to make sure that we serialise special journal-locked operations |
| 465 | * too. |
| 466 | */ |
Arjan van de Ven | 2c68ee7 | 2006-03-23 03:00:35 -0800 | [diff] [blame] | 467 | mutex_lock(&journal->j_barrier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | /** |
| 471 | * void journal_unlock_updates (journal_t* journal) - release barrier |
| 472 | * @journal: Journal to release the barrier on. |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 473 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | * Release a transaction barrier obtained with journal_lock_updates(). |
| 475 | * |
| 476 | * Should be called without the journal lock held. |
| 477 | */ |
| 478 | void journal_unlock_updates (journal_t *journal) |
| 479 | { |
| 480 | J_ASSERT(journal->j_barrier_count != 0); |
| 481 | |
Arjan van de Ven | 2c68ee7 | 2006-03-23 03:00:35 -0800 | [diff] [blame] | 482 | mutex_unlock(&journal->j_barrier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | spin_lock(&journal->j_state_lock); |
| 484 | --journal->j_barrier_count; |
| 485 | spin_unlock(&journal->j_state_lock); |
| 486 | wake_up(&journal->j_wait_transaction_locked); |
| 487 | } |
| 488 | |
| 489 | /* |
| 490 | * Report any unexpected dirty buffers which turn up. Normally those |
| 491 | * indicate an error, but they can occur if the user is running (say) |
| 492 | * tune2fs to modify the live filesystem, so we need the option of |
| 493 | * continuing as gracefully as possible. # |
| 494 | * |
| 495 | * The caller should already hold the journal lock and |
| 496 | * j_list_lock spinlock: most callers will need those anyway |
| 497 | * in order to probe the buffer's journaling state safely. |
| 498 | */ |
| 499 | static void jbd_unexpected_dirty_buffer(struct journal_head *jh) |
| 500 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | int jlist; |
| 502 | |
Jan Kara | 4407c2b | 2005-09-06 15:19:17 -0700 | [diff] [blame] | 503 | /* If this buffer is one which might reasonably be dirty |
| 504 | * --- ie. data, or not part of this journal --- then |
| 505 | * we're OK to leave it alone, but otherwise we need to |
| 506 | * move the dirty bit to the journal's own internal |
| 507 | * JBDDirty bit. */ |
| 508 | jlist = jh->b_jlist; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | |
Jan Kara | 4407c2b | 2005-09-06 15:19:17 -0700 | [diff] [blame] | 510 | if (jlist == BJ_Metadata || jlist == BJ_Reserved || |
| 511 | jlist == BJ_Shadow || jlist == BJ_Forget) { |
| 512 | struct buffer_head *bh = jh2bh(jh); |
| 513 | |
| 514 | if (test_clear_buffer_dirty(bh)) |
| 515 | set_buffer_jbddirty(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
| 519 | /* |
| 520 | * If the buffer is already part of the current transaction, then there |
| 521 | * is nothing we need to do. If it is already part of a prior |
| 522 | * transaction which we are still committing to disk, then we need to |
| 523 | * make sure that we do not overwrite the old copy: we do copy-out to |
| 524 | * preserve the copy going to disk. We also account the buffer against |
| 525 | * the handle's metadata buffer credits (unless the buffer is already |
| 526 | * part of the transaction, that is). |
| 527 | * |
| 528 | */ |
| 529 | static int |
| 530 | do_get_write_access(handle_t *handle, struct journal_head *jh, |
| 531 | int force_copy) |
| 532 | { |
| 533 | struct buffer_head *bh; |
| 534 | transaction_t *transaction; |
| 535 | journal_t *journal; |
| 536 | int error; |
| 537 | char *frozen_buffer = NULL; |
| 538 | int need_copy = 0; |
| 539 | |
| 540 | if (is_handle_aborted(handle)) |
| 541 | return -EROFS; |
| 542 | |
| 543 | transaction = handle->h_transaction; |
| 544 | journal = transaction->t_journal; |
| 545 | |
| 546 | jbd_debug(5, "buffer_head %p, force_copy %d\n", jh, force_copy); |
| 547 | |
| 548 | JBUFFER_TRACE(jh, "entry"); |
| 549 | repeat: |
| 550 | bh = jh2bh(jh); |
| 551 | |
| 552 | /* @@@ Need to check for errors here at some point. */ |
| 553 | |
| 554 | lock_buffer(bh); |
| 555 | jbd_lock_bh_state(bh); |
| 556 | |
| 557 | /* We now hold the buffer lock so it is safe to query the buffer |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 558 | * state. Is the buffer dirty? |
| 559 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | * If so, there are two possibilities. The buffer may be |
| 561 | * non-journaled, and undergoing a quite legitimate writeback. |
| 562 | * Otherwise, it is journaled, and we don't expect dirty buffers |
| 563 | * in that state (the buffers should be marked JBD_Dirty |
| 564 | * instead.) So either the IO is being done under our own |
| 565 | * control and this is a bug, or it's a third party IO such as |
| 566 | * dump(8) (which may leave the buffer scheduled for read --- |
| 567 | * ie. locked but not dirty) or tune2fs (which may actually have |
| 568 | * the buffer dirtied, ugh.) */ |
| 569 | |
| 570 | if (buffer_dirty(bh)) { |
| 571 | /* |
| 572 | * First question: is this buffer already part of the current |
| 573 | * transaction or the existing committing transaction? |
| 574 | */ |
| 575 | if (jh->b_transaction) { |
| 576 | J_ASSERT_JH(jh, |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 577 | jh->b_transaction == transaction || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | jh->b_transaction == |
| 579 | journal->j_committing_transaction); |
| 580 | if (jh->b_next_transaction) |
| 581 | J_ASSERT_JH(jh, jh->b_next_transaction == |
| 582 | transaction); |
Jan Kara | 4407c2b | 2005-09-06 15:19:17 -0700 | [diff] [blame] | 583 | } |
| 584 | /* |
| 585 | * In any case we need to clean the dirty flag and we must |
| 586 | * do it under the buffer lock to be sure we don't race |
| 587 | * with running write-out. |
| 588 | */ |
| 589 | JBUFFER_TRACE(jh, "Unexpected dirty buffer"); |
| 590 | jbd_unexpected_dirty_buffer(jh); |
Dave Kleikamp | e9ad562 | 2006-09-27 01:49:35 -0700 | [diff] [blame] | 591 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | |
| 593 | unlock_buffer(bh); |
| 594 | |
| 595 | error = -EROFS; |
| 596 | if (is_handle_aborted(handle)) { |
| 597 | jbd_unlock_bh_state(bh); |
| 598 | goto out; |
| 599 | } |
| 600 | error = 0; |
| 601 | |
| 602 | /* |
| 603 | * The buffer is already part of this transaction if b_transaction or |
| 604 | * b_next_transaction points to it |
| 605 | */ |
| 606 | if (jh->b_transaction == transaction || |
| 607 | jh->b_next_transaction == transaction) |
| 608 | goto done; |
| 609 | |
| 610 | /* |
| 611 | * If there is already a copy-out version of this buffer, then we don't |
| 612 | * need to make another one |
| 613 | */ |
| 614 | if (jh->b_frozen_data) { |
| 615 | JBUFFER_TRACE(jh, "has frozen data"); |
| 616 | J_ASSERT_JH(jh, jh->b_next_transaction == NULL); |
| 617 | jh->b_next_transaction = transaction; |
| 618 | goto done; |
| 619 | } |
| 620 | |
| 621 | /* Is there data here we need to preserve? */ |
| 622 | |
| 623 | if (jh->b_transaction && jh->b_transaction != transaction) { |
| 624 | JBUFFER_TRACE(jh, "owned by older transaction"); |
| 625 | J_ASSERT_JH(jh, jh->b_next_transaction == NULL); |
| 626 | J_ASSERT_JH(jh, jh->b_transaction == |
| 627 | journal->j_committing_transaction); |
| 628 | |
| 629 | /* There is one case we have to be very careful about. |
| 630 | * If the committing transaction is currently writing |
| 631 | * this buffer out to disk and has NOT made a copy-out, |
| 632 | * then we cannot modify the buffer contents at all |
| 633 | * right now. The essence of copy-out is that it is the |
| 634 | * extra copy, not the primary copy, which gets |
| 635 | * journaled. If the primary copy is already going to |
| 636 | * disk then we cannot do copy-out here. */ |
| 637 | |
| 638 | if (jh->b_jlist == BJ_Shadow) { |
| 639 | DEFINE_WAIT_BIT(wait, &bh->b_state, BH_Unshadow); |
| 640 | wait_queue_head_t *wqh; |
| 641 | |
| 642 | wqh = bit_waitqueue(&bh->b_state, BH_Unshadow); |
| 643 | |
| 644 | JBUFFER_TRACE(jh, "on shadow: sleep"); |
| 645 | jbd_unlock_bh_state(bh); |
| 646 | /* commit wakes up all shadow buffers after IO */ |
| 647 | for ( ; ; ) { |
| 648 | prepare_to_wait(wqh, &wait.wait, |
| 649 | TASK_UNINTERRUPTIBLE); |
| 650 | if (jh->b_jlist != BJ_Shadow) |
| 651 | break; |
| 652 | schedule(); |
| 653 | } |
| 654 | finish_wait(wqh, &wait.wait); |
| 655 | goto repeat; |
| 656 | } |
| 657 | |
| 658 | /* Only do the copy if the currently-owning transaction |
| 659 | * still needs it. If it is on the Forget list, the |
| 660 | * committing transaction is past that stage. The |
| 661 | * buffer had better remain locked during the kmalloc, |
| 662 | * but that should be true --- we hold the journal lock |
| 663 | * still and the buffer is already on the BUF_JOURNAL |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 664 | * list so won't be flushed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | * |
| 666 | * Subtle point, though: if this is a get_undo_access, |
| 667 | * then we will be relying on the frozen_data to contain |
| 668 | * the new value of the committed_data record after the |
| 669 | * transaction, so we HAVE to force the frozen_data copy |
| 670 | * in that case. */ |
| 671 | |
| 672 | if (jh->b_jlist != BJ_Forget || force_copy) { |
| 673 | JBUFFER_TRACE(jh, "generate frozen data"); |
| 674 | if (!frozen_buffer) { |
| 675 | JBUFFER_TRACE(jh, "allocate memory for buffer"); |
| 676 | jbd_unlock_bh_state(bh); |
Badari Pulavarty | ea81739 | 2006-08-27 01:23:52 -0700 | [diff] [blame] | 677 | frozen_buffer = |
Mingming Cao | c089d49 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 678 | jbd_alloc(jh2bh(jh)->b_size, |
Badari Pulavarty | ea81739 | 2006-08-27 01:23:52 -0700 | [diff] [blame] | 679 | GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | if (!frozen_buffer) { |
| 681 | printk(KERN_EMERG |
| 682 | "%s: OOM for frozen_buffer\n", |
| 683 | __FUNCTION__); |
| 684 | JBUFFER_TRACE(jh, "oom!"); |
| 685 | error = -ENOMEM; |
| 686 | jbd_lock_bh_state(bh); |
| 687 | goto done; |
| 688 | } |
| 689 | goto repeat; |
| 690 | } |
| 691 | jh->b_frozen_data = frozen_buffer; |
| 692 | frozen_buffer = NULL; |
| 693 | need_copy = 1; |
| 694 | } |
| 695 | jh->b_next_transaction = transaction; |
| 696 | } |
| 697 | |
| 698 | |
| 699 | /* |
| 700 | * Finally, if the buffer is not journaled right now, we need to make |
| 701 | * sure it doesn't get written to disk before the caller actually |
| 702 | * commits the new data |
| 703 | */ |
| 704 | if (!jh->b_transaction) { |
| 705 | JBUFFER_TRACE(jh, "no transaction"); |
| 706 | J_ASSERT_JH(jh, !jh->b_next_transaction); |
| 707 | jh->b_transaction = transaction; |
| 708 | JBUFFER_TRACE(jh, "file as BJ_Reserved"); |
| 709 | spin_lock(&journal->j_list_lock); |
| 710 | __journal_file_buffer(jh, transaction, BJ_Reserved); |
| 711 | spin_unlock(&journal->j_list_lock); |
| 712 | } |
| 713 | |
| 714 | done: |
| 715 | if (need_copy) { |
| 716 | struct page *page; |
| 717 | int offset; |
| 718 | char *source; |
| 719 | |
| 720 | J_EXPECT_JH(jh, buffer_uptodate(jh2bh(jh)), |
| 721 | "Possible IO failure.\n"); |
| 722 | page = jh2bh(jh)->b_page; |
| 723 | offset = ((unsigned long) jh2bh(jh)->b_data) & ~PAGE_MASK; |
| 724 | source = kmap_atomic(page, KM_USER0); |
| 725 | memcpy(jh->b_frozen_data, source+offset, jh2bh(jh)->b_size); |
| 726 | kunmap_atomic(source, KM_USER0); |
| 727 | } |
| 728 | jbd_unlock_bh_state(bh); |
| 729 | |
| 730 | /* |
| 731 | * If we are about to journal a buffer, then any revoke pending on it is |
| 732 | * no longer valid |
| 733 | */ |
| 734 | journal_cancel_revoke(handle, jh); |
| 735 | |
| 736 | out: |
Andrew Morton | 304c4c8 | 2006-06-23 02:05:31 -0700 | [diff] [blame] | 737 | if (unlikely(frozen_buffer)) /* It's usually NULL */ |
Mingming Cao | c089d49 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 738 | jbd_free(frozen_buffer, bh->b_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | |
| 740 | JBUFFER_TRACE(jh, "exit"); |
| 741 | return error; |
| 742 | } |
| 743 | |
| 744 | /** |
| 745 | * int journal_get_write_access() - notify intent to modify a buffer for metadata (not data) update. |
| 746 | * @handle: transaction to add buffer modifications to |
| 747 | * @bh: bh to be used for metadata writes |
| 748 | * @credits: variable that will receive credits for the buffer |
| 749 | * |
| 750 | * Returns an error code or 0 on success. |
| 751 | * |
| 752 | * In full data journalling mode the buffer may be of type BJ_AsyncData, |
| 753 | * because we're write()ing a buffer which is also part of a shared mapping. |
| 754 | */ |
| 755 | |
| 756 | int journal_get_write_access(handle_t *handle, struct buffer_head *bh) |
| 757 | { |
| 758 | struct journal_head *jh = journal_add_journal_head(bh); |
| 759 | int rc; |
| 760 | |
| 761 | /* We do not want to get caught playing with fields which the |
| 762 | * log thread also manipulates. Make sure that the buffer |
| 763 | * completes any outstanding IO before proceeding. */ |
| 764 | rc = do_get_write_access(handle, jh, 0); |
| 765 | journal_put_journal_head(jh); |
| 766 | return rc; |
| 767 | } |
| 768 | |
| 769 | |
| 770 | /* |
| 771 | * When the user wants to journal a newly created buffer_head |
| 772 | * (ie. getblk() returned a new buffer and we are going to populate it |
| 773 | * manually rather than reading off disk), then we need to keep the |
| 774 | * buffer_head locked until it has been completely filled with new |
| 775 | * data. In this case, we should be able to make the assertion that |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 776 | * the bh is not already part of an existing transaction. |
| 777 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | * The buffer should already be locked by the caller by this point. |
| 779 | * There is no lock ranking violation: it was a newly created, |
| 780 | * unlocked buffer beforehand. */ |
| 781 | |
| 782 | /** |
| 783 | * int journal_get_create_access () - notify intent to use newly created bh |
| 784 | * @handle: transaction to new buffer to |
| 785 | * @bh: new buffer. |
| 786 | * |
| 787 | * Call this if you create a new bh. |
| 788 | */ |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 789 | int journal_get_create_access(handle_t *handle, struct buffer_head *bh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | { |
| 791 | transaction_t *transaction = handle->h_transaction; |
| 792 | journal_t *journal = transaction->t_journal; |
| 793 | struct journal_head *jh = journal_add_journal_head(bh); |
| 794 | int err; |
| 795 | |
| 796 | jbd_debug(5, "journal_head %p\n", jh); |
| 797 | err = -EROFS; |
| 798 | if (is_handle_aborted(handle)) |
| 799 | goto out; |
| 800 | err = 0; |
| 801 | |
| 802 | JBUFFER_TRACE(jh, "entry"); |
| 803 | /* |
| 804 | * The buffer may already belong to this transaction due to pre-zeroing |
| 805 | * in the filesystem's new_block code. It may also be on the previous, |
| 806 | * committing transaction's lists, but it HAS to be in Forget state in |
| 807 | * that case: the transaction must have deleted the buffer for it to be |
| 808 | * reused here. |
| 809 | */ |
| 810 | jbd_lock_bh_state(bh); |
| 811 | spin_lock(&journal->j_list_lock); |
| 812 | J_ASSERT_JH(jh, (jh->b_transaction == transaction || |
| 813 | jh->b_transaction == NULL || |
| 814 | (jh->b_transaction == journal->j_committing_transaction && |
| 815 | jh->b_jlist == BJ_Forget))); |
| 816 | |
| 817 | J_ASSERT_JH(jh, jh->b_next_transaction == NULL); |
| 818 | J_ASSERT_JH(jh, buffer_locked(jh2bh(jh))); |
| 819 | |
| 820 | if (jh->b_transaction == NULL) { |
| 821 | jh->b_transaction = transaction; |
| 822 | JBUFFER_TRACE(jh, "file as BJ_Reserved"); |
| 823 | __journal_file_buffer(jh, transaction, BJ_Reserved); |
| 824 | } else if (jh->b_transaction == journal->j_committing_transaction) { |
| 825 | JBUFFER_TRACE(jh, "set next transaction"); |
| 826 | jh->b_next_transaction = transaction; |
| 827 | } |
| 828 | spin_unlock(&journal->j_list_lock); |
| 829 | jbd_unlock_bh_state(bh); |
| 830 | |
| 831 | /* |
| 832 | * akpm: I added this. ext3_alloc_branch can pick up new indirect |
| 833 | * blocks which contain freed but then revoked metadata. We need |
| 834 | * to cancel the revoke in case we end up freeing it yet again |
| 835 | * and the reallocating as data - this would cause a second revoke, |
| 836 | * which hits an assertion error. |
| 837 | */ |
| 838 | JBUFFER_TRACE(jh, "cancelling revoke"); |
| 839 | journal_cancel_revoke(handle, jh); |
| 840 | journal_put_journal_head(jh); |
| 841 | out: |
| 842 | return err; |
| 843 | } |
| 844 | |
| 845 | /** |
| 846 | * int journal_get_undo_access() - Notify intent to modify metadata with |
| 847 | * non-rewindable consequences |
| 848 | * @handle: transaction |
| 849 | * @bh: buffer to undo |
| 850 | * @credits: store the number of taken credits here (if not NULL) |
| 851 | * |
| 852 | * Sometimes there is a need to distinguish between metadata which has |
| 853 | * been committed to disk and that which has not. The ext3fs code uses |
| 854 | * this for freeing and allocating space, we have to make sure that we |
| 855 | * do not reuse freed space until the deallocation has been committed, |
| 856 | * since if we overwrote that space we would make the delete |
| 857 | * un-rewindable in case of a crash. |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 858 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | * To deal with that, journal_get_undo_access requests write access to a |
| 860 | * buffer for parts of non-rewindable operations such as delete |
| 861 | * operations on the bitmaps. The journaling code must keep a copy of |
| 862 | * the buffer's contents prior to the undo_access call until such time |
| 863 | * as we know that the buffer has definitely been committed to disk. |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 864 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | * We never need to know which transaction the committed data is part |
| 866 | * of, buffers touched here are guaranteed to be dirtied later and so |
| 867 | * will be committed to a new transaction in due course, at which point |
| 868 | * we can discard the old committed data pointer. |
| 869 | * |
| 870 | * Returns error number or 0 on success. |
| 871 | */ |
| 872 | int journal_get_undo_access(handle_t *handle, struct buffer_head *bh) |
| 873 | { |
| 874 | int err; |
| 875 | struct journal_head *jh = journal_add_journal_head(bh); |
| 876 | char *committed_data = NULL; |
| 877 | |
| 878 | JBUFFER_TRACE(jh, "entry"); |
| 879 | |
| 880 | /* |
| 881 | * Do this first --- it can drop the journal lock, so we want to |
| 882 | * make sure that obtaining the committed_data is done |
| 883 | * atomically wrt. completion of any outstanding commits. |
| 884 | */ |
| 885 | err = do_get_write_access(handle, jh, 1); |
| 886 | if (err) |
| 887 | goto out; |
| 888 | |
| 889 | repeat: |
| 890 | if (!jh->b_committed_data) { |
Mingming Cao | c089d49 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 891 | committed_data = jbd_alloc(jh2bh(jh)->b_size, GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | if (!committed_data) { |
| 893 | printk(KERN_EMERG "%s: No memory for committed data\n", |
| 894 | __FUNCTION__); |
| 895 | err = -ENOMEM; |
| 896 | goto out; |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | jbd_lock_bh_state(bh); |
| 901 | if (!jh->b_committed_data) { |
| 902 | /* Copy out the current buffer contents into the |
| 903 | * preserved, committed copy. */ |
| 904 | JBUFFER_TRACE(jh, "generate b_committed data"); |
| 905 | if (!committed_data) { |
| 906 | jbd_unlock_bh_state(bh); |
| 907 | goto repeat; |
| 908 | } |
| 909 | |
| 910 | jh->b_committed_data = committed_data; |
| 911 | committed_data = NULL; |
| 912 | memcpy(jh->b_committed_data, bh->b_data, bh->b_size); |
| 913 | } |
| 914 | jbd_unlock_bh_state(bh); |
| 915 | out: |
| 916 | journal_put_journal_head(jh); |
Andrew Morton | 304c4c8 | 2006-06-23 02:05:31 -0700 | [diff] [blame] | 917 | if (unlikely(committed_data)) |
Mingming Cao | c089d49 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 918 | jbd_free(committed_data, bh->b_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | return err; |
| 920 | } |
| 921 | |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 922 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | * int journal_dirty_data() - mark a buffer as containing dirty data which |
| 924 | * needs to be flushed before we can commit the |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 925 | * current transaction. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | * @handle: transaction |
| 927 | * @bh: bufferhead to mark |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 928 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | * The buffer is placed on the transaction's data list and is marked as |
| 930 | * belonging to the transaction. |
| 931 | * |
| 932 | * Returns error number or 0 on success. |
| 933 | * |
| 934 | * journal_dirty_data() can be called via page_launder->ext3_writepage |
| 935 | * by kswapd. |
| 936 | */ |
| 937 | int journal_dirty_data(handle_t *handle, struct buffer_head *bh) |
| 938 | { |
| 939 | journal_t *journal = handle->h_transaction->t_journal; |
| 940 | int need_brelse = 0; |
| 941 | struct journal_head *jh; |
| 942 | |
| 943 | if (is_handle_aborted(handle)) |
| 944 | return 0; |
| 945 | |
| 946 | jh = journal_add_journal_head(bh); |
| 947 | JBUFFER_TRACE(jh, "entry"); |
| 948 | |
| 949 | /* |
| 950 | * The buffer could *already* be dirty. Writeout can start |
| 951 | * at any time. |
| 952 | */ |
| 953 | jbd_debug(4, "jh: %p, tid:%d\n", jh, handle->h_transaction->t_tid); |
| 954 | |
| 955 | /* |
| 956 | * What if the buffer is already part of a running transaction? |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 957 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | * There are two cases: |
| 959 | * 1) It is part of the current running transaction. Refile it, |
| 960 | * just in case we have allocated it as metadata, deallocated |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 961 | * it, then reallocated it as data. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | * 2) It is part of the previous, still-committing transaction. |
| 963 | * If all we want to do is to guarantee that the buffer will be |
| 964 | * written to disk before this new transaction commits, then |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 965 | * being sure that the *previous* transaction has this same |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | * property is sufficient for us! Just leave it on its old |
| 967 | * transaction. |
| 968 | * |
| 969 | * In case (2), the buffer must not already exist as metadata |
| 970 | * --- that would violate write ordering (a transaction is free |
| 971 | * to write its data at any point, even before the previous |
| 972 | * committing transaction has committed). The caller must |
| 973 | * never, ever allow this to happen: there's nothing we can do |
| 974 | * about it in this layer. |
| 975 | */ |
| 976 | jbd_lock_bh_state(bh); |
| 977 | spin_lock(&journal->j_list_lock); |
Eric Sandeen | f58a74d | 2006-10-28 10:38:27 -0700 | [diff] [blame] | 978 | |
| 979 | /* Now that we have bh_state locked, are we really still mapped? */ |
| 980 | if (!buffer_mapped(bh)) { |
| 981 | JBUFFER_TRACE(jh, "unmapped buffer, bailing out"); |
| 982 | goto no_journal; |
| 983 | } |
| 984 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | if (jh->b_transaction) { |
| 986 | JBUFFER_TRACE(jh, "has transaction"); |
| 987 | if (jh->b_transaction != handle->h_transaction) { |
| 988 | JBUFFER_TRACE(jh, "belongs to older transaction"); |
| 989 | J_ASSERT_JH(jh, jh->b_transaction == |
| 990 | journal->j_committing_transaction); |
| 991 | |
| 992 | /* @@@ IS THIS TRUE ? */ |
| 993 | /* |
| 994 | * Not any more. Scenario: someone does a write() |
| 995 | * in data=journal mode. The buffer's transaction has |
| 996 | * moved into commit. Then someone does another |
| 997 | * write() to the file. We do the frozen data copyout |
| 998 | * and set b_next_transaction to point to j_running_t. |
| 999 | * And while we're in that state, someone does a |
| 1000 | * writepage() in an attempt to pageout the same area |
| 1001 | * of the file via a shared mapping. At present that |
| 1002 | * calls journal_dirty_data(), and we get right here. |
| 1003 | * It may be too late to journal the data. Simply |
| 1004 | * falling through to the next test will suffice: the |
| 1005 | * data will be dirty and wil be checkpointed. The |
| 1006 | * ordering comments in the next comment block still |
| 1007 | * apply. |
| 1008 | */ |
| 1009 | //J_ASSERT_JH(jh, jh->b_next_transaction == NULL); |
| 1010 | |
| 1011 | /* |
| 1012 | * If we're journalling data, and this buffer was |
| 1013 | * subject to a write(), it could be metadata, forget |
| 1014 | * or shadow against the committing transaction. Now, |
| 1015 | * someone has dirtied the same darn page via a mapping |
| 1016 | * and it is being writepage()'d. |
| 1017 | * We *could* just steal the page from commit, with some |
| 1018 | * fancy locking there. Instead, we just skip it - |
| 1019 | * don't tie the page's buffers to the new transaction |
| 1020 | * at all. |
| 1021 | * Implication: if we crash before the writepage() data |
| 1022 | * is written into the filesystem, recovery will replay |
| 1023 | * the write() data. |
| 1024 | */ |
| 1025 | if (jh->b_jlist != BJ_None && |
| 1026 | jh->b_jlist != BJ_SyncData && |
| 1027 | jh->b_jlist != BJ_Locked) { |
| 1028 | JBUFFER_TRACE(jh, "Not stealing"); |
| 1029 | goto no_journal; |
| 1030 | } |
| 1031 | |
| 1032 | /* |
| 1033 | * This buffer may be undergoing writeout in commit. We |
| 1034 | * can't return from here and let the caller dirty it |
| 1035 | * again because that can cause the write-out loop in |
| 1036 | * commit to never terminate. |
| 1037 | */ |
| 1038 | if (buffer_dirty(bh)) { |
| 1039 | get_bh(bh); |
| 1040 | spin_unlock(&journal->j_list_lock); |
| 1041 | jbd_unlock_bh_state(bh); |
| 1042 | need_brelse = 1; |
| 1043 | sync_dirty_buffer(bh); |
| 1044 | jbd_lock_bh_state(bh); |
| 1045 | spin_lock(&journal->j_list_lock); |
Eric Sandeen | f58a74d | 2006-10-28 10:38:27 -0700 | [diff] [blame] | 1046 | /* Since we dropped the lock... */ |
| 1047 | if (!buffer_mapped(bh)) { |
| 1048 | JBUFFER_TRACE(jh, "buffer got unmapped"); |
| 1049 | goto no_journal; |
| 1050 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | /* The buffer may become locked again at any |
| 1052 | time if it is redirtied */ |
| 1053 | } |
| 1054 | |
| 1055 | /* journal_clean_data_list() may have got there first */ |
| 1056 | if (jh->b_transaction != NULL) { |
| 1057 | JBUFFER_TRACE(jh, "unfile from commit"); |
| 1058 | __journal_temp_unlink_buffer(jh); |
| 1059 | /* It still points to the committing |
| 1060 | * transaction; move it to this one so |
| 1061 | * that the refile assert checks are |
| 1062 | * happy. */ |
| 1063 | jh->b_transaction = handle->h_transaction; |
| 1064 | } |
| 1065 | /* The buffer will be refiled below */ |
| 1066 | |
| 1067 | } |
| 1068 | /* |
| 1069 | * Special case --- the buffer might actually have been |
| 1070 | * allocated and then immediately deallocated in the previous, |
| 1071 | * committing transaction, so might still be left on that |
| 1072 | * transaction's metadata lists. |
| 1073 | */ |
| 1074 | if (jh->b_jlist != BJ_SyncData && jh->b_jlist != BJ_Locked) { |
| 1075 | JBUFFER_TRACE(jh, "not on correct data list: unfile"); |
| 1076 | J_ASSERT_JH(jh, jh->b_jlist != BJ_Shadow); |
| 1077 | __journal_temp_unlink_buffer(jh); |
| 1078 | jh->b_transaction = handle->h_transaction; |
| 1079 | JBUFFER_TRACE(jh, "file as data"); |
| 1080 | __journal_file_buffer(jh, handle->h_transaction, |
| 1081 | BJ_SyncData); |
| 1082 | } |
| 1083 | } else { |
| 1084 | JBUFFER_TRACE(jh, "not on a transaction"); |
| 1085 | __journal_file_buffer(jh, handle->h_transaction, BJ_SyncData); |
| 1086 | } |
| 1087 | no_journal: |
| 1088 | spin_unlock(&journal->j_list_lock); |
| 1089 | jbd_unlock_bh_state(bh); |
| 1090 | if (need_brelse) { |
| 1091 | BUFFER_TRACE(bh, "brelse"); |
| 1092 | __brelse(bh); |
| 1093 | } |
| 1094 | JBUFFER_TRACE(jh, "exit"); |
| 1095 | journal_put_journal_head(jh); |
| 1096 | return 0; |
| 1097 | } |
| 1098 | |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1099 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | * int journal_dirty_metadata() - mark a buffer as containing dirty metadata |
| 1101 | * @handle: transaction to add buffer to. |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1102 | * @bh: buffer to mark |
| 1103 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | * mark dirty metadata which needs to be journaled as part of the current |
| 1105 | * transaction. |
| 1106 | * |
| 1107 | * The buffer is placed on the transaction's metadata list and is marked |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1108 | * as belonging to the transaction. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | * |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1110 | * Returns error number or 0 on success. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | * |
| 1112 | * Special care needs to be taken if the buffer already belongs to the |
| 1113 | * current committing transaction (in which case we should have frozen |
| 1114 | * data present for that commit). In that case, we don't relink the |
| 1115 | * buffer: that only gets done when the old transaction finally |
| 1116 | * completes its commit. |
| 1117 | */ |
| 1118 | int journal_dirty_metadata(handle_t *handle, struct buffer_head *bh) |
| 1119 | { |
| 1120 | transaction_t *transaction = handle->h_transaction; |
| 1121 | journal_t *journal = transaction->t_journal; |
| 1122 | struct journal_head *jh = bh2jh(bh); |
| 1123 | |
| 1124 | jbd_debug(5, "journal_head %p\n", jh); |
| 1125 | JBUFFER_TRACE(jh, "entry"); |
| 1126 | if (is_handle_aborted(handle)) |
| 1127 | goto out; |
| 1128 | |
| 1129 | jbd_lock_bh_state(bh); |
| 1130 | |
| 1131 | if (jh->b_modified == 0) { |
| 1132 | /* |
| 1133 | * This buffer's got modified and becoming part |
| 1134 | * of the transaction. This needs to be done |
| 1135 | * once a transaction -bzzz |
| 1136 | */ |
| 1137 | jh->b_modified = 1; |
| 1138 | J_ASSERT_JH(jh, handle->h_buffer_credits > 0); |
| 1139 | handle->h_buffer_credits--; |
| 1140 | } |
| 1141 | |
| 1142 | /* |
| 1143 | * fastpath, to avoid expensive locking. If this buffer is already |
| 1144 | * on the running transaction's metadata list there is nothing to do. |
| 1145 | * Nobody can take it off again because there is a handle open. |
| 1146 | * I _think_ we're OK here with SMP barriers - a mistaken decision will |
| 1147 | * result in this test being false, so we go in and take the locks. |
| 1148 | */ |
| 1149 | if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) { |
| 1150 | JBUFFER_TRACE(jh, "fastpath"); |
| 1151 | J_ASSERT_JH(jh, jh->b_transaction == |
| 1152 | journal->j_running_transaction); |
| 1153 | goto out_unlock_bh; |
| 1154 | } |
| 1155 | |
| 1156 | set_buffer_jbddirty(bh); |
| 1157 | |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1158 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | * Metadata already on the current transaction list doesn't |
| 1160 | * need to be filed. Metadata on another transaction's list must |
| 1161 | * be committing, and will be refiled once the commit completes: |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1162 | * leave it alone for now. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | */ |
| 1164 | if (jh->b_transaction != transaction) { |
| 1165 | JBUFFER_TRACE(jh, "already on other transaction"); |
| 1166 | J_ASSERT_JH(jh, jh->b_transaction == |
| 1167 | journal->j_committing_transaction); |
| 1168 | J_ASSERT_JH(jh, jh->b_next_transaction == transaction); |
| 1169 | /* And this case is illegal: we can't reuse another |
| 1170 | * transaction's data buffer, ever. */ |
| 1171 | goto out_unlock_bh; |
| 1172 | } |
| 1173 | |
| 1174 | /* That test should have eliminated the following case: */ |
| 1175 | J_ASSERT_JH(jh, jh->b_frozen_data == 0); |
| 1176 | |
| 1177 | JBUFFER_TRACE(jh, "file as BJ_Metadata"); |
| 1178 | spin_lock(&journal->j_list_lock); |
| 1179 | __journal_file_buffer(jh, handle->h_transaction, BJ_Metadata); |
| 1180 | spin_unlock(&journal->j_list_lock); |
| 1181 | out_unlock_bh: |
| 1182 | jbd_unlock_bh_state(bh); |
| 1183 | out: |
| 1184 | JBUFFER_TRACE(jh, "exit"); |
| 1185 | return 0; |
| 1186 | } |
| 1187 | |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1188 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | * journal_release_buffer: undo a get_write_access without any buffer |
| 1190 | * updates, if the update decided in the end that it didn't need access. |
| 1191 | * |
| 1192 | */ |
| 1193 | void |
| 1194 | journal_release_buffer(handle_t *handle, struct buffer_head *bh) |
| 1195 | { |
| 1196 | BUFFER_TRACE(bh, "entry"); |
| 1197 | } |
| 1198 | |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1199 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | * void journal_forget() - bforget() for potentially-journaled buffers. |
| 1201 | * @handle: transaction handle |
| 1202 | * @bh: bh to 'forget' |
| 1203 | * |
| 1204 | * We can only do the bforget if there are no commits pending against the |
| 1205 | * buffer. If the buffer is dirty in the current running transaction we |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1206 | * can safely unlink it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | * |
| 1208 | * bh may not be a journalled buffer at all - it may be a non-JBD |
| 1209 | * buffer which came off the hashtable. Check for this. |
| 1210 | * |
| 1211 | * Decrements bh->b_count by one. |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1212 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | * Allow this call even if the handle has aborted --- it may be part of |
| 1214 | * the caller's cleanup after an abort. |
| 1215 | */ |
| 1216 | int journal_forget (handle_t *handle, struct buffer_head *bh) |
| 1217 | { |
| 1218 | transaction_t *transaction = handle->h_transaction; |
| 1219 | journal_t *journal = transaction->t_journal; |
| 1220 | struct journal_head *jh; |
| 1221 | int drop_reserve = 0; |
| 1222 | int err = 0; |
| 1223 | |
| 1224 | BUFFER_TRACE(bh, "entry"); |
| 1225 | |
| 1226 | jbd_lock_bh_state(bh); |
| 1227 | spin_lock(&journal->j_list_lock); |
| 1228 | |
| 1229 | if (!buffer_jbd(bh)) |
| 1230 | goto not_jbd; |
| 1231 | jh = bh2jh(bh); |
| 1232 | |
| 1233 | /* Critical error: attempting to delete a bitmap buffer, maybe? |
| 1234 | * Don't do any jbd operations, and return an error. */ |
| 1235 | if (!J_EXPECT_JH(jh, !jh->b_committed_data, |
| 1236 | "inconsistent data on disk")) { |
| 1237 | err = -EIO; |
| 1238 | goto not_jbd; |
| 1239 | } |
| 1240 | |
| 1241 | /* |
| 1242 | * The buffer's going from the transaction, we must drop |
| 1243 | * all references -bzzz |
| 1244 | */ |
| 1245 | jh->b_modified = 0; |
| 1246 | |
| 1247 | if (jh->b_transaction == handle->h_transaction) { |
| 1248 | J_ASSERT_JH(jh, !jh->b_frozen_data); |
| 1249 | |
| 1250 | /* If we are forgetting a buffer which is already part |
| 1251 | * of this transaction, then we can just drop it from |
| 1252 | * the transaction immediately. */ |
| 1253 | clear_buffer_dirty(bh); |
| 1254 | clear_buffer_jbddirty(bh); |
| 1255 | |
| 1256 | JBUFFER_TRACE(jh, "belongs to current transaction: unfile"); |
| 1257 | |
| 1258 | drop_reserve = 1; |
| 1259 | |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1260 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | * We are no longer going to journal this buffer. |
| 1262 | * However, the commit of this transaction is still |
| 1263 | * important to the buffer: the delete that we are now |
| 1264 | * processing might obsolete an old log entry, so by |
| 1265 | * committing, we can satisfy the buffer's checkpoint. |
| 1266 | * |
| 1267 | * So, if we have a checkpoint on the buffer, we should |
| 1268 | * now refile the buffer on our BJ_Forget list so that |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1269 | * we know to remove the checkpoint after we commit. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | */ |
| 1271 | |
| 1272 | if (jh->b_cp_transaction) { |
| 1273 | __journal_temp_unlink_buffer(jh); |
| 1274 | __journal_file_buffer(jh, transaction, BJ_Forget); |
| 1275 | } else { |
| 1276 | __journal_unfile_buffer(jh); |
| 1277 | journal_remove_journal_head(bh); |
| 1278 | __brelse(bh); |
| 1279 | if (!buffer_jbd(bh)) { |
| 1280 | spin_unlock(&journal->j_list_lock); |
| 1281 | jbd_unlock_bh_state(bh); |
| 1282 | __bforget(bh); |
| 1283 | goto drop; |
| 1284 | } |
| 1285 | } |
| 1286 | } else if (jh->b_transaction) { |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1287 | J_ASSERT_JH(jh, (jh->b_transaction == |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | journal->j_committing_transaction)); |
| 1289 | /* However, if the buffer is still owned by a prior |
| 1290 | * (committing) transaction, we can't drop it yet... */ |
| 1291 | JBUFFER_TRACE(jh, "belongs to older transaction"); |
| 1292 | /* ... but we CAN drop it from the new transaction if we |
| 1293 | * have also modified it since the original commit. */ |
| 1294 | |
| 1295 | if (jh->b_next_transaction) { |
| 1296 | J_ASSERT(jh->b_next_transaction == transaction); |
| 1297 | jh->b_next_transaction = NULL; |
| 1298 | drop_reserve = 1; |
| 1299 | } |
| 1300 | } |
| 1301 | |
| 1302 | not_jbd: |
| 1303 | spin_unlock(&journal->j_list_lock); |
| 1304 | jbd_unlock_bh_state(bh); |
| 1305 | __brelse(bh); |
| 1306 | drop: |
| 1307 | if (drop_reserve) { |
| 1308 | /* no need to reserve log space for this block -bzzz */ |
| 1309 | handle->h_buffer_credits++; |
| 1310 | } |
| 1311 | return err; |
| 1312 | } |
| 1313 | |
| 1314 | /** |
| 1315 | * int journal_stop() - complete a transaction |
| 1316 | * @handle: tranaction to complete. |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1317 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | * All done for a particular handle. |
| 1319 | * |
| 1320 | * There is not much action needed here. We just return any remaining |
| 1321 | * buffer credits to the transaction and remove the handle. The only |
| 1322 | * complication is that we need to start a commit operation if the |
| 1323 | * filesystem is marked for synchronous update. |
| 1324 | * |
| 1325 | * journal_stop itself will not usually return an error, but it may |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1326 | * do so in unusual circumstances. In particular, expect it to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | * return -EIO if a journal_abort has been executed since the |
| 1328 | * transaction began. |
| 1329 | */ |
| 1330 | int journal_stop(handle_t *handle) |
| 1331 | { |
| 1332 | transaction_t *transaction = handle->h_transaction; |
| 1333 | journal_t *journal = transaction->t_journal; |
| 1334 | int old_handle_count, err; |
Andrew Morton | fe1dcbc | 2006-02-04 23:27:54 -0800 | [diff] [blame] | 1335 | pid_t pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | J_ASSERT(journal_current_handle() == handle); |
| 1338 | |
| 1339 | if (is_handle_aborted(handle)) |
| 1340 | err = -EIO; |
OGAWA Hirofumi | 3e2a532 | 2006-10-19 23:29:11 -0700 | [diff] [blame] | 1341 | else { |
| 1342 | J_ASSERT(transaction->t_updates > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | err = 0; |
OGAWA Hirofumi | 3e2a532 | 2006-10-19 23:29:11 -0700 | [diff] [blame] | 1344 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | |
| 1346 | if (--handle->h_ref > 0) { |
| 1347 | jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1, |
| 1348 | handle->h_ref); |
| 1349 | return err; |
| 1350 | } |
| 1351 | |
| 1352 | jbd_debug(4, "Handle %p going down\n", handle); |
| 1353 | |
| 1354 | /* |
| 1355 | * Implement synchronous transaction batching. If the handle |
| 1356 | * was synchronous, don't force a commit immediately. Let's |
| 1357 | * yield and let another thread piggyback onto this transaction. |
| 1358 | * Keep doing that while new threads continue to arrive. |
| 1359 | * It doesn't cost much - we're about to run a commit and sleep |
| 1360 | * on IO anyway. Speeds up many-threaded, many-dir operations |
| 1361 | * by 30x or more... |
Andrew Morton | fe1dcbc | 2006-02-04 23:27:54 -0800 | [diff] [blame] | 1362 | * |
| 1363 | * But don't do this if this process was the most recent one to |
| 1364 | * perform a synchronous write. We do this to detect the case where a |
| 1365 | * single process is doing a stream of sync writes. No point in waiting |
| 1366 | * for joiners in that case. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | */ |
Andrew Morton | fe1dcbc | 2006-02-04 23:27:54 -0800 | [diff] [blame] | 1368 | pid = current->pid; |
| 1369 | if (handle->h_sync && journal->j_last_sync_writer != pid) { |
| 1370 | journal->j_last_sync_writer = pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | do { |
| 1372 | old_handle_count = transaction->t_handle_count; |
Nishanth Aravamudan | 041e0e3 | 2005-09-10 00:27:23 -0700 | [diff] [blame] | 1373 | schedule_timeout_uninterruptible(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | } while (old_handle_count != transaction->t_handle_count); |
| 1375 | } |
| 1376 | |
| 1377 | current->journal_info = NULL; |
| 1378 | spin_lock(&journal->j_state_lock); |
| 1379 | spin_lock(&transaction->t_handle_lock); |
| 1380 | transaction->t_outstanding_credits -= handle->h_buffer_credits; |
| 1381 | transaction->t_updates--; |
| 1382 | if (!transaction->t_updates) { |
| 1383 | wake_up(&journal->j_wait_updates); |
| 1384 | if (journal->j_barrier_count) |
| 1385 | wake_up(&journal->j_wait_transaction_locked); |
| 1386 | } |
| 1387 | |
| 1388 | /* |
| 1389 | * If the handle is marked SYNC, we need to set another commit |
| 1390 | * going! We also want to force a commit if the current |
| 1391 | * transaction is occupying too much of the log, or if the |
| 1392 | * transaction is too old now. |
| 1393 | */ |
| 1394 | if (handle->h_sync || |
| 1395 | transaction->t_outstanding_credits > |
| 1396 | journal->j_max_transaction_buffers || |
Dave Kleikamp | e9ad562 | 2006-09-27 01:49:35 -0700 | [diff] [blame] | 1397 | time_after_eq(jiffies, transaction->t_expires)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | /* Do this even for aborted journals: an abort still |
| 1399 | * completes the commit thread, it just doesn't write |
| 1400 | * anything to disk. */ |
| 1401 | tid_t tid = transaction->t_tid; |
| 1402 | |
| 1403 | spin_unlock(&transaction->t_handle_lock); |
| 1404 | jbd_debug(2, "transaction too old, requesting commit for " |
| 1405 | "handle %p\n", handle); |
| 1406 | /* This is non-blocking */ |
| 1407 | __log_start_commit(journal, transaction->t_tid); |
| 1408 | spin_unlock(&journal->j_state_lock); |
| 1409 | |
| 1410 | /* |
| 1411 | * Special case: JFS_SYNC synchronous updates require us |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1412 | * to wait for the commit to complete. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | */ |
| 1414 | if (handle->h_sync && !(current->flags & PF_MEMALLOC)) |
| 1415 | err = log_wait_commit(journal, tid); |
| 1416 | } else { |
| 1417 | spin_unlock(&transaction->t_handle_lock); |
| 1418 | spin_unlock(&journal->j_state_lock); |
| 1419 | } |
| 1420 | |
Peter Zijlstra | 34a3d1e | 2007-10-11 22:11:12 +0200 | [diff] [blame] | 1421 | lock_release(&handle->h_lockdep_map, 1, _THIS_IP_); |
| 1422 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | jbd_free_handle(handle); |
| 1424 | return err; |
| 1425 | } |
| 1426 | |
| 1427 | /**int journal_force_commit() - force any uncommitted transactions |
| 1428 | * @journal: journal to force |
| 1429 | * |
| 1430 | * For synchronous operations: force any uncommitted transactions |
| 1431 | * to disk. May seem kludgy, but it reuses all the handle batching |
| 1432 | * code in a very simple manner. |
| 1433 | */ |
| 1434 | int journal_force_commit(journal_t *journal) |
| 1435 | { |
| 1436 | handle_t *handle; |
| 1437 | int ret; |
| 1438 | |
| 1439 | handle = journal_start(journal, 1); |
| 1440 | if (IS_ERR(handle)) { |
| 1441 | ret = PTR_ERR(handle); |
| 1442 | } else { |
| 1443 | handle->h_sync = 1; |
| 1444 | ret = journal_stop(handle); |
| 1445 | } |
| 1446 | return ret; |
| 1447 | } |
| 1448 | |
| 1449 | /* |
| 1450 | * |
| 1451 | * List management code snippets: various functions for manipulating the |
| 1452 | * transaction buffer lists. |
| 1453 | * |
| 1454 | */ |
| 1455 | |
| 1456 | /* |
| 1457 | * Append a buffer to a transaction list, given the transaction's list head |
| 1458 | * pointer. |
| 1459 | * |
| 1460 | * j_list_lock is held. |
| 1461 | * |
| 1462 | * jbd_lock_bh_state(jh2bh(jh)) is held. |
| 1463 | */ |
| 1464 | |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1465 | static inline void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | __blist_add_buffer(struct journal_head **list, struct journal_head *jh) |
| 1467 | { |
| 1468 | if (!*list) { |
| 1469 | jh->b_tnext = jh->b_tprev = jh; |
| 1470 | *list = jh; |
| 1471 | } else { |
| 1472 | /* Insert at the tail of the list to preserve order */ |
| 1473 | struct journal_head *first = *list, *last = first->b_tprev; |
| 1474 | jh->b_tprev = last; |
| 1475 | jh->b_tnext = first; |
| 1476 | last->b_tnext = first->b_tprev = jh; |
| 1477 | } |
| 1478 | } |
| 1479 | |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1480 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | * Remove a buffer from a transaction list, given the transaction's list |
| 1482 | * head pointer. |
| 1483 | * |
| 1484 | * Called with j_list_lock held, and the journal may not be locked. |
| 1485 | * |
| 1486 | * jbd_lock_bh_state(jh2bh(jh)) is held. |
| 1487 | */ |
| 1488 | |
| 1489 | static inline void |
| 1490 | __blist_del_buffer(struct journal_head **list, struct journal_head *jh) |
| 1491 | { |
| 1492 | if (*list == jh) { |
| 1493 | *list = jh->b_tnext; |
| 1494 | if (*list == jh) |
| 1495 | *list = NULL; |
| 1496 | } |
| 1497 | jh->b_tprev->b_tnext = jh->b_tnext; |
| 1498 | jh->b_tnext->b_tprev = jh->b_tprev; |
| 1499 | } |
| 1500 | |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1501 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | * Remove a buffer from the appropriate transaction list. |
| 1503 | * |
| 1504 | * Note that this function can *change* the value of |
| 1505 | * bh->b_transaction->t_sync_datalist, t_buffers, t_forget, |
| 1506 | * t_iobuf_list, t_shadow_list, t_log_list or t_reserved_list. If the caller |
| 1507 | * is holding onto a copy of one of thee pointers, it could go bad. |
| 1508 | * Generally the caller needs to re-read the pointer from the transaction_t. |
| 1509 | * |
| 1510 | * Called under j_list_lock. The journal may not be locked. |
| 1511 | */ |
Adrian Bunk | d394e12 | 2006-12-06 20:38:26 -0800 | [diff] [blame] | 1512 | static void __journal_temp_unlink_buffer(struct journal_head *jh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | { |
| 1514 | struct journal_head **list = NULL; |
| 1515 | transaction_t *transaction; |
| 1516 | struct buffer_head *bh = jh2bh(jh); |
| 1517 | |
| 1518 | J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh)); |
| 1519 | transaction = jh->b_transaction; |
| 1520 | if (transaction) |
| 1521 | assert_spin_locked(&transaction->t_journal->j_list_lock); |
| 1522 | |
| 1523 | J_ASSERT_JH(jh, jh->b_jlist < BJ_Types); |
| 1524 | if (jh->b_jlist != BJ_None) |
| 1525 | J_ASSERT_JH(jh, transaction != 0); |
| 1526 | |
| 1527 | switch (jh->b_jlist) { |
| 1528 | case BJ_None: |
| 1529 | return; |
| 1530 | case BJ_SyncData: |
| 1531 | list = &transaction->t_sync_datalist; |
| 1532 | break; |
| 1533 | case BJ_Metadata: |
| 1534 | transaction->t_nr_buffers--; |
| 1535 | J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0); |
| 1536 | list = &transaction->t_buffers; |
| 1537 | break; |
| 1538 | case BJ_Forget: |
| 1539 | list = &transaction->t_forget; |
| 1540 | break; |
| 1541 | case BJ_IO: |
| 1542 | list = &transaction->t_iobuf_list; |
| 1543 | break; |
| 1544 | case BJ_Shadow: |
| 1545 | list = &transaction->t_shadow_list; |
| 1546 | break; |
| 1547 | case BJ_LogCtl: |
| 1548 | list = &transaction->t_log_list; |
| 1549 | break; |
| 1550 | case BJ_Reserved: |
| 1551 | list = &transaction->t_reserved_list; |
| 1552 | break; |
| 1553 | case BJ_Locked: |
| 1554 | list = &transaction->t_locked_list; |
| 1555 | break; |
| 1556 | } |
| 1557 | |
| 1558 | __blist_del_buffer(list, jh); |
| 1559 | jh->b_jlist = BJ_None; |
| 1560 | if (test_clear_buffer_jbddirty(bh)) |
| 1561 | mark_buffer_dirty(bh); /* Expose it to the VM */ |
| 1562 | } |
| 1563 | |
| 1564 | void __journal_unfile_buffer(struct journal_head *jh) |
| 1565 | { |
| 1566 | __journal_temp_unlink_buffer(jh); |
| 1567 | jh->b_transaction = NULL; |
| 1568 | } |
| 1569 | |
| 1570 | void journal_unfile_buffer(journal_t *journal, struct journal_head *jh) |
| 1571 | { |
| 1572 | jbd_lock_bh_state(jh2bh(jh)); |
| 1573 | spin_lock(&journal->j_list_lock); |
| 1574 | __journal_unfile_buffer(jh); |
| 1575 | spin_unlock(&journal->j_list_lock); |
| 1576 | jbd_unlock_bh_state(jh2bh(jh)); |
| 1577 | } |
| 1578 | |
| 1579 | /* |
| 1580 | * Called from journal_try_to_free_buffers(). |
| 1581 | * |
| 1582 | * Called under jbd_lock_bh_state(bh) |
| 1583 | */ |
| 1584 | static void |
| 1585 | __journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh) |
| 1586 | { |
| 1587 | struct journal_head *jh; |
| 1588 | |
| 1589 | jh = bh2jh(bh); |
| 1590 | |
| 1591 | if (buffer_locked(bh) || buffer_dirty(bh)) |
| 1592 | goto out; |
| 1593 | |
| 1594 | if (jh->b_next_transaction != 0) |
| 1595 | goto out; |
| 1596 | |
| 1597 | spin_lock(&journal->j_list_lock); |
| 1598 | if (jh->b_transaction != 0 && jh->b_cp_transaction == 0) { |
| 1599 | if (jh->b_jlist == BJ_SyncData || jh->b_jlist == BJ_Locked) { |
| 1600 | /* A written-back ordered data buffer */ |
| 1601 | JBUFFER_TRACE(jh, "release data"); |
| 1602 | __journal_unfile_buffer(jh); |
| 1603 | journal_remove_journal_head(bh); |
| 1604 | __brelse(bh); |
| 1605 | } |
| 1606 | } else if (jh->b_cp_transaction != 0 && jh->b_transaction == 0) { |
| 1607 | /* written-back checkpointed metadata buffer */ |
| 1608 | if (jh->b_jlist == BJ_None) { |
| 1609 | JBUFFER_TRACE(jh, "remove from checkpoint list"); |
| 1610 | __journal_remove_checkpoint(jh); |
| 1611 | journal_remove_journal_head(bh); |
| 1612 | __brelse(bh); |
| 1613 | } |
| 1614 | } |
| 1615 | spin_unlock(&journal->j_list_lock); |
| 1616 | out: |
| 1617 | return; |
| 1618 | } |
| 1619 | |
| 1620 | |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1621 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | * int journal_try_to_free_buffers() - try to free page buffers. |
| 1623 | * @journal: journal for operation |
| 1624 | * @page: to try and free |
| 1625 | * @unused_gfp_mask: unused |
| 1626 | * |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1627 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | * For all the buffers on this page, |
| 1629 | * if they are fully written out ordered data, move them onto BUF_CLEAN |
| 1630 | * so try_to_free_buffers() can reap them. |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1631 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | * This function returns non-zero if we wish try_to_free_buffers() |
| 1633 | * to be called. We do this if the page is releasable by try_to_free_buffers(). |
| 1634 | * We also do it if the page has locked or dirty buffers and the caller wants |
| 1635 | * us to perform sync or async writeout. |
| 1636 | * |
| 1637 | * This complicates JBD locking somewhat. We aren't protected by the |
| 1638 | * BKL here. We wish to remove the buffer from its committing or |
| 1639 | * running transaction's ->t_datalist via __journal_unfile_buffer. |
| 1640 | * |
| 1641 | * This may *change* the value of transaction_t->t_datalist, so anyone |
| 1642 | * who looks at t_datalist needs to lock against this function. |
| 1643 | * |
| 1644 | * Even worse, someone may be doing a journal_dirty_data on this |
| 1645 | * buffer. So we need to lock against that. journal_dirty_data() |
| 1646 | * will come out of the lock with the buffer dirty, which makes it |
| 1647 | * ineligible for release here. |
| 1648 | * |
| 1649 | * Who else is affected by this? hmm... Really the only contender |
| 1650 | * is do_get_write_access() - it could be looking at the buffer while |
| 1651 | * journal_try_to_free_buffer() is changing its state. But that |
| 1652 | * cannot happen because we never reallocate freed data as metadata |
| 1653 | * while the data is part of a transaction. Yes? |
| 1654 | */ |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1655 | int journal_try_to_free_buffers(journal_t *journal, |
Al Viro | 27496a8 | 2005-10-21 03:20:48 -0400 | [diff] [blame] | 1656 | struct page *page, gfp_t unused_gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | { |
| 1658 | struct buffer_head *head; |
| 1659 | struct buffer_head *bh; |
| 1660 | int ret = 0; |
| 1661 | |
| 1662 | J_ASSERT(PageLocked(page)); |
| 1663 | |
| 1664 | head = page_buffers(page); |
| 1665 | bh = head; |
| 1666 | do { |
| 1667 | struct journal_head *jh; |
| 1668 | |
| 1669 | /* |
| 1670 | * We take our own ref against the journal_head here to avoid |
| 1671 | * having to add tons of locking around each instance of |
| 1672 | * journal_remove_journal_head() and journal_put_journal_head(). |
| 1673 | */ |
| 1674 | jh = journal_grab_journal_head(bh); |
| 1675 | if (!jh) |
| 1676 | continue; |
| 1677 | |
| 1678 | jbd_lock_bh_state(bh); |
| 1679 | __journal_try_to_free_buffer(journal, bh); |
| 1680 | journal_put_journal_head(jh); |
| 1681 | jbd_unlock_bh_state(bh); |
| 1682 | if (buffer_jbd(bh)) |
| 1683 | goto busy; |
| 1684 | } while ((bh = bh->b_this_page) != head); |
| 1685 | ret = try_to_free_buffers(page); |
| 1686 | busy: |
| 1687 | return ret; |
| 1688 | } |
| 1689 | |
| 1690 | /* |
| 1691 | * This buffer is no longer needed. If it is on an older transaction's |
| 1692 | * checkpoint list we need to record it on this transaction's forget list |
| 1693 | * to pin this buffer (and hence its checkpointing transaction) down until |
| 1694 | * this transaction commits. If the buffer isn't on a checkpoint list, we |
| 1695 | * release it. |
| 1696 | * Returns non-zero if JBD no longer has an interest in the buffer. |
| 1697 | * |
| 1698 | * Called under j_list_lock. |
| 1699 | * |
| 1700 | * Called under jbd_lock_bh_state(bh). |
| 1701 | */ |
| 1702 | static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction) |
| 1703 | { |
| 1704 | int may_free = 1; |
| 1705 | struct buffer_head *bh = jh2bh(jh); |
| 1706 | |
| 1707 | __journal_unfile_buffer(jh); |
| 1708 | |
| 1709 | if (jh->b_cp_transaction) { |
| 1710 | JBUFFER_TRACE(jh, "on running+cp transaction"); |
| 1711 | __journal_file_buffer(jh, transaction, BJ_Forget); |
| 1712 | clear_buffer_jbddirty(bh); |
| 1713 | may_free = 0; |
| 1714 | } else { |
| 1715 | JBUFFER_TRACE(jh, "on running transaction"); |
| 1716 | journal_remove_journal_head(bh); |
| 1717 | __brelse(bh); |
| 1718 | } |
| 1719 | return may_free; |
| 1720 | } |
| 1721 | |
| 1722 | /* |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1723 | * journal_invalidatepage |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | * |
| 1725 | * This code is tricky. It has a number of cases to deal with. |
| 1726 | * |
| 1727 | * There are two invariants which this code relies on: |
| 1728 | * |
| 1729 | * i_size must be updated on disk before we start calling invalidatepage on the |
| 1730 | * data. |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1731 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | * This is done in ext3 by defining an ext3_setattr method which |
| 1733 | * updates i_size before truncate gets going. By maintaining this |
| 1734 | * invariant, we can be sure that it is safe to throw away any buffers |
| 1735 | * attached to the current transaction: once the transaction commits, |
| 1736 | * we know that the data will not be needed. |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1737 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | * Note however that we can *not* throw away data belonging to the |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1739 | * previous, committing transaction! |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | * |
| 1741 | * Any disk blocks which *are* part of the previous, committing |
| 1742 | * transaction (and which therefore cannot be discarded immediately) are |
| 1743 | * not going to be reused in the new running transaction |
| 1744 | * |
| 1745 | * The bitmap committed_data images guarantee this: any block which is |
| 1746 | * allocated in one transaction and removed in the next will be marked |
| 1747 | * as in-use in the committed_data bitmap, so cannot be reused until |
| 1748 | * the next transaction to delete the block commits. This means that |
| 1749 | * leaving committing buffers dirty is quite safe: the disk blocks |
| 1750 | * cannot be reallocated to a different file and so buffer aliasing is |
| 1751 | * not possible. |
| 1752 | * |
| 1753 | * |
| 1754 | * The above applies mainly to ordered data mode. In writeback mode we |
| 1755 | * don't make guarantees about the order in which data hits disk --- in |
| 1756 | * particular we don't guarantee that new dirty data is flushed before |
| 1757 | * transaction commit --- so it is always safe just to discard data |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1758 | * immediately in that mode. --sct |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1759 | */ |
| 1760 | |
| 1761 | /* |
| 1762 | * The journal_unmap_buffer helper function returns zero if the buffer |
| 1763 | * concerned remains pinned as an anonymous buffer belonging to an older |
| 1764 | * transaction. |
| 1765 | * |
| 1766 | * We're outside-transaction here. Either or both of j_running_transaction |
| 1767 | * and j_committing_transaction may be NULL. |
| 1768 | */ |
| 1769 | static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh) |
| 1770 | { |
| 1771 | transaction_t *transaction; |
| 1772 | struct journal_head *jh; |
| 1773 | int may_free = 1; |
| 1774 | int ret; |
| 1775 | |
| 1776 | BUFFER_TRACE(bh, "entry"); |
| 1777 | |
| 1778 | /* |
| 1779 | * It is safe to proceed here without the j_list_lock because the |
| 1780 | * buffers cannot be stolen by try_to_free_buffers as long as we are |
| 1781 | * holding the page lock. --sct |
| 1782 | */ |
| 1783 | |
| 1784 | if (!buffer_jbd(bh)) |
| 1785 | goto zap_buffer_unlocked; |
| 1786 | |
| 1787 | spin_lock(&journal->j_state_lock); |
| 1788 | jbd_lock_bh_state(bh); |
| 1789 | spin_lock(&journal->j_list_lock); |
| 1790 | |
| 1791 | jh = journal_grab_journal_head(bh); |
| 1792 | if (!jh) |
| 1793 | goto zap_buffer_no_jh; |
| 1794 | |
| 1795 | transaction = jh->b_transaction; |
| 1796 | if (transaction == NULL) { |
| 1797 | /* First case: not on any transaction. If it |
| 1798 | * has no checkpoint link, then we can zap it: |
| 1799 | * it's a writeback-mode buffer so we don't care |
| 1800 | * if it hits disk safely. */ |
| 1801 | if (!jh->b_cp_transaction) { |
| 1802 | JBUFFER_TRACE(jh, "not on any transaction: zap"); |
| 1803 | goto zap_buffer; |
| 1804 | } |
| 1805 | |
| 1806 | if (!buffer_dirty(bh)) { |
| 1807 | /* bdflush has written it. We can drop it now */ |
| 1808 | goto zap_buffer; |
| 1809 | } |
| 1810 | |
| 1811 | /* OK, it must be in the journal but still not |
| 1812 | * written fully to disk: it's metadata or |
| 1813 | * journaled data... */ |
| 1814 | |
| 1815 | if (journal->j_running_transaction) { |
| 1816 | /* ... and once the current transaction has |
| 1817 | * committed, the buffer won't be needed any |
| 1818 | * longer. */ |
| 1819 | JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget"); |
| 1820 | ret = __dispose_buffer(jh, |
| 1821 | journal->j_running_transaction); |
| 1822 | journal_put_journal_head(jh); |
| 1823 | spin_unlock(&journal->j_list_lock); |
| 1824 | jbd_unlock_bh_state(bh); |
| 1825 | spin_unlock(&journal->j_state_lock); |
| 1826 | return ret; |
| 1827 | } else { |
| 1828 | /* There is no currently-running transaction. So the |
| 1829 | * orphan record which we wrote for this file must have |
| 1830 | * passed into commit. We must attach this buffer to |
| 1831 | * the committing transaction, if it exists. */ |
| 1832 | if (journal->j_committing_transaction) { |
| 1833 | JBUFFER_TRACE(jh, "give to committing trans"); |
| 1834 | ret = __dispose_buffer(jh, |
| 1835 | journal->j_committing_transaction); |
| 1836 | journal_put_journal_head(jh); |
| 1837 | spin_unlock(&journal->j_list_lock); |
| 1838 | jbd_unlock_bh_state(bh); |
| 1839 | spin_unlock(&journal->j_state_lock); |
| 1840 | return ret; |
| 1841 | } else { |
| 1842 | /* The orphan record's transaction has |
| 1843 | * committed. We can cleanse this buffer */ |
| 1844 | clear_buffer_jbddirty(bh); |
| 1845 | goto zap_buffer; |
| 1846 | } |
| 1847 | } |
| 1848 | } else if (transaction == journal->j_committing_transaction) { |
Eric Sandeen | f58a74d | 2006-10-28 10:38:27 -0700 | [diff] [blame] | 1849 | JBUFFER_TRACE(jh, "on committing transaction"); |
akpm@osdl.org | d13df84 | 2005-04-16 15:26:36 -0700 | [diff] [blame] | 1850 | if (jh->b_jlist == BJ_Locked) { |
| 1851 | /* |
| 1852 | * The buffer is on the committing transaction's locked |
| 1853 | * list. We have the buffer locked, so I/O has |
| 1854 | * completed. So we can nail the buffer now. |
| 1855 | */ |
| 1856 | may_free = __dispose_buffer(jh, transaction); |
| 1857 | goto zap_buffer; |
| 1858 | } |
| 1859 | /* |
| 1860 | * If it is committing, we simply cannot touch it. We |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | * can remove it's next_transaction pointer from the |
| 1862 | * running transaction if that is set, but nothing |
| 1863 | * else. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1864 | set_buffer_freed(bh); |
| 1865 | if (jh->b_next_transaction) { |
| 1866 | J_ASSERT(jh->b_next_transaction == |
| 1867 | journal->j_running_transaction); |
| 1868 | jh->b_next_transaction = NULL; |
| 1869 | } |
| 1870 | journal_put_journal_head(jh); |
| 1871 | spin_unlock(&journal->j_list_lock); |
| 1872 | jbd_unlock_bh_state(bh); |
| 1873 | spin_unlock(&journal->j_state_lock); |
| 1874 | return 0; |
| 1875 | } else { |
| 1876 | /* Good, the buffer belongs to the running transaction. |
| 1877 | * We are writing our own transaction's data, not any |
| 1878 | * previous one's, so it is safe to throw it away |
| 1879 | * (remember that we expect the filesystem to have set |
| 1880 | * i_size already for this truncate so recovery will not |
| 1881 | * expose the disk blocks we are discarding here.) */ |
| 1882 | J_ASSERT_JH(jh, transaction == journal->j_running_transaction); |
Eric Sandeen | f58a74d | 2006-10-28 10:38:27 -0700 | [diff] [blame] | 1883 | JBUFFER_TRACE(jh, "on running transaction"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | may_free = __dispose_buffer(jh, transaction); |
| 1885 | } |
| 1886 | |
| 1887 | zap_buffer: |
| 1888 | journal_put_journal_head(jh); |
| 1889 | zap_buffer_no_jh: |
| 1890 | spin_unlock(&journal->j_list_lock); |
| 1891 | jbd_unlock_bh_state(bh); |
| 1892 | spin_unlock(&journal->j_state_lock); |
| 1893 | zap_buffer_unlocked: |
| 1894 | clear_buffer_dirty(bh); |
| 1895 | J_ASSERT_BH(bh, !buffer_jbddirty(bh)); |
| 1896 | clear_buffer_mapped(bh); |
| 1897 | clear_buffer_req(bh); |
| 1898 | clear_buffer_new(bh); |
| 1899 | bh->b_bdev = NULL; |
| 1900 | return may_free; |
| 1901 | } |
| 1902 | |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1903 | /** |
NeilBrown | 2ff28e2 | 2006-03-26 01:37:18 -0800 | [diff] [blame] | 1904 | * void journal_invalidatepage() |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1905 | * @journal: journal to use for flush... |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1906 | * @page: page to flush |
| 1907 | * @offset: length of page to invalidate. |
| 1908 | * |
| 1909 | * Reap page buffers containing data after offset in page. |
| 1910 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | */ |
NeilBrown | 2ff28e2 | 2006-03-26 01:37:18 -0800 | [diff] [blame] | 1912 | void journal_invalidatepage(journal_t *journal, |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1913 | struct page *page, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1914 | unsigned long offset) |
| 1915 | { |
| 1916 | struct buffer_head *head, *bh, *next; |
| 1917 | unsigned int curr_off = 0; |
| 1918 | int may_free = 1; |
| 1919 | |
| 1920 | if (!PageLocked(page)) |
| 1921 | BUG(); |
| 1922 | if (!page_has_buffers(page)) |
NeilBrown | 2ff28e2 | 2006-03-26 01:37:18 -0800 | [diff] [blame] | 1923 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1924 | |
| 1925 | /* We will potentially be playing with lists other than just the |
| 1926 | * data lists (especially for journaled data mode), so be |
| 1927 | * cautious in our locking. */ |
| 1928 | |
| 1929 | head = bh = page_buffers(page); |
| 1930 | do { |
| 1931 | unsigned int next_off = curr_off + bh->b_size; |
| 1932 | next = bh->b_this_page; |
| 1933 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1934 | if (offset <= curr_off) { |
Dave Kleikamp | e9ad562 | 2006-09-27 01:49:35 -0700 | [diff] [blame] | 1935 | /* This block is wholly outside the truncation point */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1936 | lock_buffer(bh); |
| 1937 | may_free &= journal_unmap_buffer(journal, bh); |
| 1938 | unlock_buffer(bh); |
| 1939 | } |
| 1940 | curr_off = next_off; |
| 1941 | bh = next; |
| 1942 | |
| 1943 | } while (bh != head); |
| 1944 | |
| 1945 | if (!offset) { |
NeilBrown | 2ff28e2 | 2006-03-26 01:37:18 -0800 | [diff] [blame] | 1946 | if (may_free && try_to_free_buffers(page)) |
| 1947 | J_ASSERT(!page_has_buffers(page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1948 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 | } |
| 1950 | |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1951 | /* |
| 1952 | * File a buffer on the given transaction list. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1953 | */ |
| 1954 | void __journal_file_buffer(struct journal_head *jh, |
| 1955 | transaction_t *transaction, int jlist) |
| 1956 | { |
| 1957 | struct journal_head **list = NULL; |
| 1958 | int was_dirty = 0; |
| 1959 | struct buffer_head *bh = jh2bh(jh); |
| 1960 | |
| 1961 | J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh)); |
| 1962 | assert_spin_locked(&transaction->t_journal->j_list_lock); |
| 1963 | |
| 1964 | J_ASSERT_JH(jh, jh->b_jlist < BJ_Types); |
| 1965 | J_ASSERT_JH(jh, jh->b_transaction == transaction || |
| 1966 | jh->b_transaction == 0); |
| 1967 | |
| 1968 | if (jh->b_transaction && jh->b_jlist == jlist) |
| 1969 | return; |
| 1970 | |
| 1971 | /* The following list of buffer states needs to be consistent |
| 1972 | * with __jbd_unexpected_dirty_buffer()'s handling of dirty |
| 1973 | * state. */ |
| 1974 | |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1975 | if (jlist == BJ_Metadata || jlist == BJ_Reserved || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | jlist == BJ_Shadow || jlist == BJ_Forget) { |
| 1977 | if (test_clear_buffer_dirty(bh) || |
| 1978 | test_clear_buffer_jbddirty(bh)) |
| 1979 | was_dirty = 1; |
| 1980 | } |
| 1981 | |
| 1982 | if (jh->b_transaction) |
| 1983 | __journal_temp_unlink_buffer(jh); |
| 1984 | jh->b_transaction = transaction; |
| 1985 | |
| 1986 | switch (jlist) { |
| 1987 | case BJ_None: |
| 1988 | J_ASSERT_JH(jh, !jh->b_committed_data); |
| 1989 | J_ASSERT_JH(jh, !jh->b_frozen_data); |
| 1990 | return; |
| 1991 | case BJ_SyncData: |
| 1992 | list = &transaction->t_sync_datalist; |
| 1993 | break; |
| 1994 | case BJ_Metadata: |
| 1995 | transaction->t_nr_buffers++; |
| 1996 | list = &transaction->t_buffers; |
| 1997 | break; |
| 1998 | case BJ_Forget: |
| 1999 | list = &transaction->t_forget; |
| 2000 | break; |
| 2001 | case BJ_IO: |
| 2002 | list = &transaction->t_iobuf_list; |
| 2003 | break; |
| 2004 | case BJ_Shadow: |
| 2005 | list = &transaction->t_shadow_list; |
| 2006 | break; |
| 2007 | case BJ_LogCtl: |
| 2008 | list = &transaction->t_log_list; |
| 2009 | break; |
| 2010 | case BJ_Reserved: |
| 2011 | list = &transaction->t_reserved_list; |
| 2012 | break; |
| 2013 | case BJ_Locked: |
| 2014 | list = &transaction->t_locked_list; |
| 2015 | break; |
| 2016 | } |
| 2017 | |
| 2018 | __blist_add_buffer(list, jh); |
| 2019 | jh->b_jlist = jlist; |
| 2020 | |
| 2021 | if (was_dirty) |
| 2022 | set_buffer_jbddirty(bh); |
| 2023 | } |
| 2024 | |
| 2025 | void journal_file_buffer(struct journal_head *jh, |
| 2026 | transaction_t *transaction, int jlist) |
| 2027 | { |
| 2028 | jbd_lock_bh_state(jh2bh(jh)); |
| 2029 | spin_lock(&transaction->t_journal->j_list_lock); |
| 2030 | __journal_file_buffer(jh, transaction, jlist); |
| 2031 | spin_unlock(&transaction->t_journal->j_list_lock); |
| 2032 | jbd_unlock_bh_state(jh2bh(jh)); |
| 2033 | } |
| 2034 | |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 2035 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2036 | * Remove a buffer from its current buffer list in preparation for |
| 2037 | * dropping it from its current transaction entirely. If the buffer has |
| 2038 | * already started to be used by a subsequent transaction, refile the |
| 2039 | * buffer on that transaction's metadata list. |
| 2040 | * |
| 2041 | * Called under journal->j_list_lock |
| 2042 | * |
| 2043 | * Called under jbd_lock_bh_state(jh2bh(jh)) |
| 2044 | */ |
| 2045 | void __journal_refile_buffer(struct journal_head *jh) |
| 2046 | { |
| 2047 | int was_dirty; |
| 2048 | struct buffer_head *bh = jh2bh(jh); |
| 2049 | |
| 2050 | J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh)); |
| 2051 | if (jh->b_transaction) |
| 2052 | assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock); |
| 2053 | |
| 2054 | /* If the buffer is now unused, just drop it. */ |
| 2055 | if (jh->b_next_transaction == NULL) { |
| 2056 | __journal_unfile_buffer(jh); |
| 2057 | return; |
| 2058 | } |
| 2059 | |
| 2060 | /* |
| 2061 | * It has been modified by a later transaction: add it to the new |
| 2062 | * transaction's metadata list. |
| 2063 | */ |
| 2064 | |
| 2065 | was_dirty = test_clear_buffer_jbddirty(bh); |
| 2066 | __journal_temp_unlink_buffer(jh); |
| 2067 | jh->b_transaction = jh->b_next_transaction; |
| 2068 | jh->b_next_transaction = NULL; |
Jan Kara | 9ada734 | 2006-06-23 02:05:25 -0700 | [diff] [blame] | 2069 | __journal_file_buffer(jh, jh->b_transaction, |
| 2070 | was_dirty ? BJ_Metadata : BJ_Reserved); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING); |
| 2072 | |
| 2073 | if (was_dirty) |
| 2074 | set_buffer_jbddirty(bh); |
| 2075 | } |
| 2076 | |
| 2077 | /* |
| 2078 | * For the unlocked version of this call, also make sure that any |
| 2079 | * hanging journal_head is cleaned up if necessary. |
| 2080 | * |
| 2081 | * __journal_refile_buffer is usually called as part of a single locked |
| 2082 | * operation on a buffer_head, in which the caller is probably going to |
| 2083 | * be hooking the journal_head onto other lists. In that case it is up |
| 2084 | * to the caller to remove the journal_head if necessary. For the |
| 2085 | * unlocked journal_refile_buffer call, the caller isn't going to be |
| 2086 | * doing anything else to the buffer so we need to do the cleanup |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 2087 | * ourselves to avoid a jh leak. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | * |
| 2089 | * *** The journal_head may be freed by this call! *** |
| 2090 | */ |
| 2091 | void journal_refile_buffer(journal_t *journal, struct journal_head *jh) |
| 2092 | { |
| 2093 | struct buffer_head *bh = jh2bh(jh); |
| 2094 | |
| 2095 | jbd_lock_bh_state(bh); |
| 2096 | spin_lock(&journal->j_list_lock); |
| 2097 | |
| 2098 | __journal_refile_buffer(jh); |
| 2099 | jbd_unlock_bh_state(bh); |
| 2100 | journal_remove_journal_head(bh); |
| 2101 | |
| 2102 | spin_unlock(&journal->j_list_lock); |
| 2103 | __brelse(bh); |
| 2104 | } |