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