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