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