blob: e214d68620ac167fb5ddeb71775ead1b6063a571 [file] [log] [blame]
Dave Kleikamp470decc2006-10-11 01:20:57 -07001/*
Uwe Kleine-König58862692007-05-09 07:51:49 +02002 * linux/fs/jbd2/transaction.c
Dave Kleikamp470decc2006-10-11 01:20:57 -07003 *
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 Caof7f4bcc2006-10-11 01:20:59 -070022#include <linux/jbd2.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070023#include <linux/errno.h>
24#include <linux/slab.h>
25#include <linux/timer.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070026#include <linux/mm.h>
27#include <linux/highmem.h>
Josef Bacike07f7182008-11-26 01:14:26 -050028#include <linux/hrtimer.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070029
Adrian Bunk7ddae862006-12-06 20:38:27 -080030static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh);
31
Dave Kleikamp470decc2006-10-11 01:20:57 -070032/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -070033 * jbd2_get_transaction: obtain a new transaction_t object.
Dave Kleikamp470decc2006-10-11 01:20:57 -070034 *
35 * Simply allocate and initialise a new transaction. Create it in
36 * RUNNING state and add it to the current journal (which should not
37 * have an existing running transaction: we only make a new transaction
38 * once we have started to commit the old one).
39 *
40 * Preconditions:
41 * The journal MUST be locked. We don't perform atomic mallocs on the
42 * new transaction and we can't block without protecting against other
43 * processes trying to touch the journal while it is in transition.
44 *
Dave Kleikamp470decc2006-10-11 01:20:57 -070045 */
46
47static transaction_t *
Mingming Caof7f4bcc2006-10-11 01:20:59 -070048jbd2_get_transaction(journal_t *journal, transaction_t *transaction)
Dave Kleikamp470decc2006-10-11 01:20:57 -070049{
50 transaction->t_journal = journal;
51 transaction->t_state = T_RUNNING;
Josef Bacike07f7182008-11-26 01:14:26 -050052 transaction->t_start_time = ktime_get();
Dave Kleikamp470decc2006-10-11 01:20:57 -070053 transaction->t_tid = journal->j_transaction_sequence++;
54 transaction->t_expires = jiffies + journal->j_commit_interval;
55 spin_lock_init(&transaction->t_handle_lock);
Jan Karac851ed52008-07-11 19:27:31 -040056 INIT_LIST_HEAD(&transaction->t_inode_list);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -040057 INIT_LIST_HEAD(&transaction->t_private_list);
Dave Kleikamp470decc2006-10-11 01:20:57 -070058
59 /* Set up the commit timer for the new transaction. */
Andreas Dilgerb1f485f2009-08-10 22:51:53 -040060 journal->j_commit_timer.expires = round_jiffies_up(transaction->t_expires);
Dave Kleikamp470decc2006-10-11 01:20:57 -070061 add_timer(&journal->j_commit_timer);
62
63 J_ASSERT(journal->j_running_transaction == NULL);
64 journal->j_running_transaction = transaction;
Johann Lombardi8e85fb32008-01-28 23:58:27 -050065 transaction->t_max_wait = 0;
66 transaction->t_start = jiffies;
Dave Kleikamp470decc2006-10-11 01:20:57 -070067
68 return transaction;
69}
70
71/*
72 * Handle management.
73 *
74 * A handle_t is an object which represents a single atomic update to a
75 * filesystem, and which tracks all of the modifications which form part
76 * of that one update.
77 */
78
79/*
80 * start_this_handle: Given a handle, deal with any locking or stalling
81 * needed to make sure that there is enough journal space for the handle
82 * to begin. Attach the handle to a transaction and set up the
83 * transaction's buffer credits.
84 */
85
86static int start_this_handle(journal_t *journal, handle_t *handle)
87{
88 transaction_t *transaction;
89 int needed;
90 int nblocks = handle->h_buffer_credits;
91 transaction_t *new_transaction = NULL;
92 int ret = 0;
Johann Lombardi8e85fb32008-01-28 23:58:27 -050093 unsigned long ts = jiffies;
Dave Kleikamp470decc2006-10-11 01:20:57 -070094
95 if (nblocks > journal->j_max_transaction_buffers) {
96 printk(KERN_ERR "JBD: %s wants too many credits (%d > %d)\n",
97 current->comm, nblocks,
98 journal->j_max_transaction_buffers);
99 ret = -ENOSPC;
100 goto out;
101 }
102
103alloc_transaction:
104 if (!journal->j_running_transaction) {
Mingming Caod802ffa2007-10-16 18:38:25 -0400105 new_transaction = kzalloc(sizeof(*new_transaction),
Mingming Cao2d9179692007-10-16 18:38:25 -0400106 GFP_NOFS|__GFP_NOFAIL);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700107 if (!new_transaction) {
108 ret = -ENOMEM;
109 goto out;
110 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700111 }
112
113 jbd_debug(3, "New handle %p going live.\n", handle);
114
115repeat:
116
117 /*
118 * We need to hold j_state_lock until t_updates has been incremented,
119 * for proper journal barrier handling
120 */
121 spin_lock(&journal->j_state_lock);
122repeat_locked:
123 if (is_journal_aborted(journal) ||
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700124 (journal->j_errno != 0 && !(journal->j_flags & JBD2_ACK_ERR))) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700125 spin_unlock(&journal->j_state_lock);
126 ret = -EROFS;
127 goto out;
128 }
129
130 /* Wait on the journal's transaction barrier if necessary */
131 if (journal->j_barrier_count) {
132 spin_unlock(&journal->j_state_lock);
133 wait_event(journal->j_wait_transaction_locked,
134 journal->j_barrier_count == 0);
135 goto repeat;
136 }
137
138 if (!journal->j_running_transaction) {
139 if (!new_transaction) {
140 spin_unlock(&journal->j_state_lock);
141 goto alloc_transaction;
142 }
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700143 jbd2_get_transaction(journal, new_transaction);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700144 new_transaction = NULL;
145 }
146
147 transaction = journal->j_running_transaction;
148
149 /*
150 * If the current transaction is locked down for commit, wait for the
151 * lock to be released.
152 */
153 if (transaction->t_state == T_LOCKED) {
154 DEFINE_WAIT(wait);
155
156 prepare_to_wait(&journal->j_wait_transaction_locked,
157 &wait, TASK_UNINTERRUPTIBLE);
158 spin_unlock(&journal->j_state_lock);
159 schedule();
160 finish_wait(&journal->j_wait_transaction_locked, &wait);
161 goto repeat;
162 }
163
164 /*
165 * If there is not enough space left in the log to write all potential
166 * buffers requested by this operation, we need to stall pending a log
167 * checkpoint to free some more log space.
168 */
169 spin_lock(&transaction->t_handle_lock);
170 needed = transaction->t_outstanding_credits + nblocks;
171
172 if (needed > journal->j_max_transaction_buffers) {
173 /*
174 * If the current transaction is already too large, then start
175 * to commit it: we can then go back and attach this handle to
176 * a new transaction.
177 */
178 DEFINE_WAIT(wait);
179
180 jbd_debug(2, "Handle %p starting new commit...\n", handle);
181 spin_unlock(&transaction->t_handle_lock);
182 prepare_to_wait(&journal->j_wait_transaction_locked, &wait,
183 TASK_UNINTERRUPTIBLE);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700184 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700185 spin_unlock(&journal->j_state_lock);
186 schedule();
187 finish_wait(&journal->j_wait_transaction_locked, &wait);
188 goto repeat;
189 }
190
191 /*
192 * The commit code assumes that it can get enough log space
193 * without forcing a checkpoint. This is *critical* for
194 * correctness: a checkpoint of a buffer which is also
195 * associated with a committing transaction creates a deadlock,
196 * so commit simply cannot force through checkpoints.
197 *
198 * We must therefore ensure the necessary space in the journal
199 * *before* starting to dirty potentially checkpointed buffers
200 * in the new transaction.
201 *
202 * The worst part is, any transaction currently committing can
203 * reduce the free space arbitrarily. Be careful to account for
204 * those buffers when checkpointing.
205 */
206
207 /*
208 * @@@ AKPM: This seems rather over-defensive. We're giving commit
209 * a _lot_ of headroom: 1/4 of the journal plus the size of
210 * the committing transaction. Really, we only need to give it
211 * committing_transaction->t_outstanding_credits plus "enough" for
212 * the log control blocks.
213 * Also, this test is inconsitent with the matching one in
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700214 * jbd2_journal_extend().
Dave Kleikamp470decc2006-10-11 01:20:57 -0700215 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700216 if (__jbd2_log_space_left(journal) < jbd_space_needed(journal)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700217 jbd_debug(2, "Handle %p waiting for checkpoint...\n", handle);
218 spin_unlock(&transaction->t_handle_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700219 __jbd2_log_wait_for_space(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700220 goto repeat_locked;
221 }
222
223 /* OK, account for the buffers that this operation expects to
224 * use and add the handle to the running transaction. */
225
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500226 if (time_after(transaction->t_start, ts)) {
227 ts = jbd2_time_diff(ts, transaction->t_start);
228 if (ts > transaction->t_max_wait)
229 transaction->t_max_wait = ts;
230 }
231
Dave Kleikamp470decc2006-10-11 01:20:57 -0700232 handle->h_transaction = transaction;
233 transaction->t_outstanding_credits += nblocks;
234 transaction->t_updates++;
235 transaction->t_handle_count++;
236 jbd_debug(4, "Handle %p given %d credits (total %d, free %d)\n",
237 handle, nblocks, transaction->t_outstanding_credits,
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700238 __jbd2_log_space_left(journal));
Dave Kleikamp470decc2006-10-11 01:20:57 -0700239 spin_unlock(&transaction->t_handle_lock);
240 spin_unlock(&journal->j_state_lock);
Jan Kara9599b0e2009-08-17 21:23:17 -0400241
242 lock_map_acquire(&handle->h_lockdep_map);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700243out:
244 if (unlikely(new_transaction)) /* It's usually NULL */
245 kfree(new_transaction);
246 return ret;
247}
248
Mingming Cao7b751062008-01-28 23:58:27 -0500249static struct lock_class_key jbd2_handle_key;
250
Dave Kleikamp470decc2006-10-11 01:20:57 -0700251/* Allocate a new handle. This should probably be in a slab... */
252static handle_t *new_handle(int nblocks)
253{
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400254 handle_t *handle = jbd2_alloc_handle(GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700255 if (!handle)
256 return NULL;
257 memset(handle, 0, sizeof(*handle));
258 handle->h_buffer_credits = nblocks;
259 handle->h_ref = 1;
260
Mingming Cao7b751062008-01-28 23:58:27 -0500261 lockdep_init_map(&handle->h_lockdep_map, "jbd2_handle",
262 &jbd2_handle_key, 0);
263
Dave Kleikamp470decc2006-10-11 01:20:57 -0700264 return handle;
265}
266
267/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700268 * handle_t *jbd2_journal_start() - Obtain a new handle.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700269 * @journal: Journal to start transaction on.
270 * @nblocks: number of block buffer we might modify
271 *
272 * We make sure that the transaction can guarantee at least nblocks of
273 * modified buffers in the log. We block until the log can guarantee
274 * that much space.
275 *
276 * This function is visible to journal users (like ext3fs), so is not
277 * called with the journal already locked.
278 *
279 * Return a pointer to a newly allocated handle, or NULL on failure
280 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700281handle_t *jbd2_journal_start(journal_t *journal, int nblocks)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700282{
283 handle_t *handle = journal_current_handle();
284 int err;
285
286 if (!journal)
287 return ERR_PTR(-EROFS);
288
289 if (handle) {
290 J_ASSERT(handle->h_transaction->t_journal == journal);
291 handle->h_ref++;
292 return handle;
293 }
294
295 handle = new_handle(nblocks);
296 if (!handle)
297 return ERR_PTR(-ENOMEM);
298
299 current->journal_info = handle;
300
301 err = start_this_handle(journal, handle);
302 if (err < 0) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400303 jbd2_free_handle(handle);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700304 current->journal_info = NULL;
305 handle = ERR_PTR(err);
Mingming Cao7b751062008-01-28 23:58:27 -0500306 goto out;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700307 }
Mingming Cao7b751062008-01-28 23:58:27 -0500308out:
Dave Kleikamp470decc2006-10-11 01:20:57 -0700309 return handle;
310}
311
312/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700313 * int jbd2_journal_extend() - extend buffer credits.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700314 * @handle: handle to 'extend'
315 * @nblocks: nr blocks to try to extend by.
316 *
317 * Some transactions, such as large extends and truncates, can be done
318 * atomically all at once or in several stages. The operation requests
319 * a credit for a number of buffer modications in advance, but can
320 * extend its credit if it needs more.
321 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700322 * jbd2_journal_extend tries to give the running handle more buffer credits.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700323 * It does not guarantee that allocation - this is a best-effort only.
324 * The calling process MUST be able to deal cleanly with a failure to
325 * extend here.
326 *
327 * Return 0 on success, non-zero on failure.
328 *
329 * return code < 0 implies an error
330 * return code > 0 implies normal transaction-full status.
331 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700332int jbd2_journal_extend(handle_t *handle, int nblocks)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700333{
334 transaction_t *transaction = handle->h_transaction;
335 journal_t *journal = transaction->t_journal;
336 int result;
337 int wanted;
338
339 result = -EIO;
340 if (is_handle_aborted(handle))
341 goto out;
342
343 result = 1;
344
345 spin_lock(&journal->j_state_lock);
346
347 /* Don't extend a locked-down transaction! */
348 if (handle->h_transaction->t_state != T_RUNNING) {
349 jbd_debug(3, "denied handle %p %d blocks: "
350 "transaction not running\n", handle, nblocks);
351 goto error_out;
352 }
353
354 spin_lock(&transaction->t_handle_lock);
355 wanted = transaction->t_outstanding_credits + nblocks;
356
357 if (wanted > journal->j_max_transaction_buffers) {
358 jbd_debug(3, "denied handle %p %d blocks: "
359 "transaction too large\n", handle, nblocks);
360 goto unlock;
361 }
362
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700363 if (wanted > __jbd2_log_space_left(journal)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700364 jbd_debug(3, "denied handle %p %d blocks: "
365 "insufficient log space\n", handle, nblocks);
366 goto unlock;
367 }
368
369 handle->h_buffer_credits += nblocks;
370 transaction->t_outstanding_credits += nblocks;
371 result = 0;
372
373 jbd_debug(3, "extended handle %p by %d\n", handle, nblocks);
374unlock:
375 spin_unlock(&transaction->t_handle_lock);
376error_out:
377 spin_unlock(&journal->j_state_lock);
378out:
379 return result;
380}
381
382
383/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700384 * int jbd2_journal_restart() - restart a handle .
Dave Kleikamp470decc2006-10-11 01:20:57 -0700385 * @handle: handle to restart
386 * @nblocks: nr credits requested
387 *
388 * Restart a handle for a multi-transaction filesystem
389 * operation.
390 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700391 * If the jbd2_journal_extend() call above fails to grant new buffer credits
392 * to a running handle, a call to jbd2_journal_restart will commit the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700393 * handle's transaction so far and reattach the handle to a new
394 * transaction capabable of guaranteeing the requested number of
395 * credits.
396 */
397
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700398int jbd2_journal_restart(handle_t *handle, int nblocks)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700399{
400 transaction_t *transaction = handle->h_transaction;
401 journal_t *journal = transaction->t_journal;
402 int ret;
403
404 /* If we've had an abort of any type, don't even think about
405 * actually doing the restart! */
406 if (is_handle_aborted(handle))
407 return 0;
408
409 /*
410 * First unlink the handle from its current transaction, and start the
411 * commit on that.
412 */
413 J_ASSERT(transaction->t_updates > 0);
414 J_ASSERT(journal_current_handle() == handle);
415
416 spin_lock(&journal->j_state_lock);
417 spin_lock(&transaction->t_handle_lock);
418 transaction->t_outstanding_credits -= handle->h_buffer_credits;
419 transaction->t_updates--;
420
421 if (!transaction->t_updates)
422 wake_up(&journal->j_wait_updates);
423 spin_unlock(&transaction->t_handle_lock);
424
425 jbd_debug(2, "restarting handle %p\n", handle);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700426 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700427 spin_unlock(&journal->j_state_lock);
428
Jan Kara9599b0e2009-08-17 21:23:17 -0400429 lock_map_release(&handle->h_lockdep_map);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700430 handle->h_buffer_credits = nblocks;
431 ret = start_this_handle(journal, handle);
432 return ret;
433}
434
435
436/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700437 * void jbd2_journal_lock_updates () - establish a transaction barrier.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700438 * @journal: Journal to establish a barrier on.
439 *
440 * This locks out any further updates from being started, and blocks
441 * until all existing updates have completed, returning only once the
442 * journal is in a quiescent state with no updates running.
443 *
444 * The journal lock should not be held on entry.
445 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700446void jbd2_journal_lock_updates(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700447{
448 DEFINE_WAIT(wait);
449
450 spin_lock(&journal->j_state_lock);
451 ++journal->j_barrier_count;
452
453 /* Wait until there are no running updates */
454 while (1) {
455 transaction_t *transaction = journal->j_running_transaction;
456
457 if (!transaction)
458 break;
459
460 spin_lock(&transaction->t_handle_lock);
461 if (!transaction->t_updates) {
462 spin_unlock(&transaction->t_handle_lock);
463 break;
464 }
465 prepare_to_wait(&journal->j_wait_updates, &wait,
466 TASK_UNINTERRUPTIBLE);
467 spin_unlock(&transaction->t_handle_lock);
468 spin_unlock(&journal->j_state_lock);
469 schedule();
470 finish_wait(&journal->j_wait_updates, &wait);
471 spin_lock(&journal->j_state_lock);
472 }
473 spin_unlock(&journal->j_state_lock);
474
475 /*
476 * We have now established a barrier against other normal updates, but
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700477 * we also need to barrier against other jbd2_journal_lock_updates() calls
Dave Kleikamp470decc2006-10-11 01:20:57 -0700478 * to make sure that we serialise special journal-locked operations
479 * too.
480 */
481 mutex_lock(&journal->j_barrier);
482}
483
484/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700485 * void jbd2_journal_unlock_updates (journal_t* journal) - release barrier
Dave Kleikamp470decc2006-10-11 01:20:57 -0700486 * @journal: Journal to release the barrier on.
487 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700488 * Release a transaction barrier obtained with jbd2_journal_lock_updates().
Dave Kleikamp470decc2006-10-11 01:20:57 -0700489 *
490 * Should be called without the journal lock held.
491 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700492void jbd2_journal_unlock_updates (journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700493{
494 J_ASSERT(journal->j_barrier_count != 0);
495
496 mutex_unlock(&journal->j_barrier);
497 spin_lock(&journal->j_state_lock);
498 --journal->j_barrier_count;
499 spin_unlock(&journal->j_state_lock);
500 wake_up(&journal->j_wait_transaction_locked);
501}
502
Jan Karaf91d1d02009-07-13 16:16:20 -0400503static void warn_dirty_buffer(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700504{
Jan Karaf91d1d02009-07-13 16:16:20 -0400505 char b[BDEVNAME_SIZE];
Dave Kleikamp470decc2006-10-11 01:20:57 -0700506
Jan Karaf91d1d02009-07-13 16:16:20 -0400507 printk(KERN_WARNING
508 "JBD: Spotted dirty metadata buffer (dev = %s, blocknr = %llu). "
509 "There's a risk of filesystem corruption in case of system "
510 "crash.\n",
511 bdevname(bh->b_bdev, b), (unsigned long long)bh->b_blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700512}
513
514/*
515 * If the buffer is already part of the current transaction, then there
516 * is nothing we need to do. If it is already part of a prior
517 * transaction which we are still committing to disk, then we need to
518 * make sure that we do not overwrite the old copy: we do copy-out to
519 * preserve the copy going to disk. We also account the buffer against
520 * the handle's metadata buffer credits (unless the buffer is already
521 * part of the transaction, that is).
522 *
523 */
524static int
525do_get_write_access(handle_t *handle, struct journal_head *jh,
526 int force_copy)
527{
528 struct buffer_head *bh;
529 transaction_t *transaction;
530 journal_t *journal;
531 int error;
532 char *frozen_buffer = NULL;
533 int need_copy = 0;
534
535 if (is_handle_aborted(handle))
536 return -EROFS;
537
538 transaction = handle->h_transaction;
539 journal = transaction->t_journal;
540
541 jbd_debug(5, "buffer_head %p, force_copy %d\n", jh, force_copy);
542
543 JBUFFER_TRACE(jh, "entry");
544repeat:
545 bh = jh2bh(jh);
546
547 /* @@@ Need to check for errors here at some point. */
548
549 lock_buffer(bh);
550 jbd_lock_bh_state(bh);
551
552 /* We now hold the buffer lock so it is safe to query the buffer
553 * state. Is the buffer dirty?
554 *
555 * If so, there are two possibilities. The buffer may be
556 * non-journaled, and undergoing a quite legitimate writeback.
557 * Otherwise, it is journaled, and we don't expect dirty buffers
558 * in that state (the buffers should be marked JBD_Dirty
559 * instead.) So either the IO is being done under our own
560 * control and this is a bug, or it's a third party IO such as
561 * dump(8) (which may leave the buffer scheduled for read ---
562 * ie. locked but not dirty) or tune2fs (which may actually have
563 * the buffer dirtied, ugh.) */
564
565 if (buffer_dirty(bh)) {
566 /*
567 * First question: is this buffer already part of the current
568 * transaction or the existing committing transaction?
569 */
570 if (jh->b_transaction) {
571 J_ASSERT_JH(jh,
572 jh->b_transaction == transaction ||
573 jh->b_transaction ==
574 journal->j_committing_transaction);
575 if (jh->b_next_transaction)
576 J_ASSERT_JH(jh, jh->b_next_transaction ==
577 transaction);
Jan Karaf91d1d02009-07-13 16:16:20 -0400578 warn_dirty_buffer(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700579 }
580 /*
581 * In any case we need to clean the dirty flag and we must
582 * do it under the buffer lock to be sure we don't race
583 * with running write-out.
584 */
Jan Karaf91d1d02009-07-13 16:16:20 -0400585 JBUFFER_TRACE(jh, "Journalling dirty buffer");
586 clear_buffer_dirty(bh);
587 set_buffer_jbddirty(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700588 }
589
590 unlock_buffer(bh);
591
592 error = -EROFS;
593 if (is_handle_aborted(handle)) {
594 jbd_unlock_bh_state(bh);
595 goto out;
596 }
597 error = 0;
598
599 /*
600 * The buffer is already part of this transaction if b_transaction or
601 * b_next_transaction points to it
602 */
603 if (jh->b_transaction == transaction ||
604 jh->b_next_transaction == transaction)
605 goto done;
606
607 /*
Josef Bacik9fc7c632008-04-17 10:38:59 -0400608 * this is the first time this transaction is touching this buffer,
609 * reset the modified flag
610 */
611 jh->b_modified = 0;
612
613 /*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700614 * If there is already a copy-out version of this buffer, then we don't
615 * need to make another one
616 */
617 if (jh->b_frozen_data) {
618 JBUFFER_TRACE(jh, "has frozen data");
619 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
620 jh->b_next_transaction = transaction;
621 goto done;
622 }
623
624 /* Is there data here we need to preserve? */
625
626 if (jh->b_transaction && jh->b_transaction != transaction) {
627 JBUFFER_TRACE(jh, "owned by older transaction");
628 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
629 J_ASSERT_JH(jh, jh->b_transaction ==
630 journal->j_committing_transaction);
631
632 /* There is one case we have to be very careful about.
633 * If the committing transaction is currently writing
634 * this buffer out to disk and has NOT made a copy-out,
635 * then we cannot modify the buffer contents at all
636 * right now. The essence of copy-out is that it is the
637 * extra copy, not the primary copy, which gets
638 * journaled. If the primary copy is already going to
639 * disk then we cannot do copy-out here. */
640
641 if (jh->b_jlist == BJ_Shadow) {
642 DEFINE_WAIT_BIT(wait, &bh->b_state, BH_Unshadow);
643 wait_queue_head_t *wqh;
644
645 wqh = bit_waitqueue(&bh->b_state, BH_Unshadow);
646
647 JBUFFER_TRACE(jh, "on shadow: sleep");
648 jbd_unlock_bh_state(bh);
649 /* commit wakes up all shadow buffers after IO */
650 for ( ; ; ) {
651 prepare_to_wait(wqh, &wait.wait,
652 TASK_UNINTERRUPTIBLE);
653 if (jh->b_jlist != BJ_Shadow)
654 break;
655 schedule();
656 }
657 finish_wait(wqh, &wait.wait);
658 goto repeat;
659 }
660
661 /* Only do the copy if the currently-owning transaction
662 * still needs it. If it is on the Forget list, the
663 * committing transaction is past that stage. The
664 * buffer had better remain locked during the kmalloc,
665 * but that should be true --- we hold the journal lock
666 * still and the buffer is already on the BUF_JOURNAL
667 * list so won't be flushed.
668 *
669 * Subtle point, though: if this is a get_undo_access,
670 * then we will be relying on the frozen_data to contain
671 * the new value of the committed_data record after the
672 * transaction, so we HAVE to force the frozen_data copy
673 * in that case. */
674
675 if (jh->b_jlist != BJ_Forget || force_copy) {
676 JBUFFER_TRACE(jh, "generate frozen data");
677 if (!frozen_buffer) {
678 JBUFFER_TRACE(jh, "allocate memory for buffer");
679 jbd_unlock_bh_state(bh);
680 frozen_buffer =
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400681 jbd2_alloc(jh2bh(jh)->b_size,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700682 GFP_NOFS);
683 if (!frozen_buffer) {
684 printk(KERN_EMERG
685 "%s: OOM for frozen_buffer\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400686 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700687 JBUFFER_TRACE(jh, "oom!");
688 error = -ENOMEM;
689 jbd_lock_bh_state(bh);
690 goto done;
691 }
692 goto repeat;
693 }
694 jh->b_frozen_data = frozen_buffer;
695 frozen_buffer = NULL;
696 need_copy = 1;
697 }
698 jh->b_next_transaction = transaction;
699 }
700
701
702 /*
703 * Finally, if the buffer is not journaled right now, we need to make
704 * sure it doesn't get written to disk before the caller actually
705 * commits the new data
706 */
707 if (!jh->b_transaction) {
708 JBUFFER_TRACE(jh, "no transaction");
709 J_ASSERT_JH(jh, !jh->b_next_transaction);
710 jh->b_transaction = transaction;
711 JBUFFER_TRACE(jh, "file as BJ_Reserved");
712 spin_lock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700713 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700714 spin_unlock(&journal->j_list_lock);
715 }
716
717done:
718 if (need_copy) {
719 struct page *page;
720 int offset;
721 char *source;
722
723 J_EXPECT_JH(jh, buffer_uptodate(jh2bh(jh)),
724 "Possible IO failure.\n");
725 page = jh2bh(jh)->b_page;
726 offset = ((unsigned long) jh2bh(jh)->b_data) & ~PAGE_MASK;
727 source = kmap_atomic(page, KM_USER0);
728 memcpy(jh->b_frozen_data, source+offset, jh2bh(jh)->b_size);
729 kunmap_atomic(source, KM_USER0);
Joel Beckere06c8222008-09-11 15:35:47 -0700730
731 /*
732 * Now that the frozen data is saved off, we need to store
733 * any matching triggers.
734 */
735 jh->b_frozen_triggers = jh->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700736 }
737 jbd_unlock_bh_state(bh);
738
739 /*
740 * If we are about to journal a buffer, then any revoke pending on it is
741 * no longer valid
742 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700743 jbd2_journal_cancel_revoke(handle, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700744
745out:
746 if (unlikely(frozen_buffer)) /* It's usually NULL */
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400747 jbd2_free(frozen_buffer, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700748
749 JBUFFER_TRACE(jh, "exit");
750 return error;
751}
752
753/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700754 * int jbd2_journal_get_write_access() - notify intent to modify a buffer for metadata (not data) update.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700755 * @handle: transaction to add buffer modifications to
756 * @bh: bh to be used for metadata writes
757 * @credits: variable that will receive credits for the buffer
758 *
759 * Returns an error code or 0 on success.
760 *
761 * In full data journalling mode the buffer may be of type BJ_AsyncData,
762 * because we're write()ing a buffer which is also part of a shared mapping.
763 */
764
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700765int jbd2_journal_get_write_access(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700766{
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700767 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700768 int rc;
769
770 /* We do not want to get caught playing with fields which the
771 * log thread also manipulates. Make sure that the buffer
772 * completes any outstanding IO before proceeding. */
773 rc = do_get_write_access(handle, jh, 0);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700774 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700775 return rc;
776}
777
778
779/*
780 * When the user wants to journal a newly created buffer_head
781 * (ie. getblk() returned a new buffer and we are going to populate it
782 * manually rather than reading off disk), then we need to keep the
783 * buffer_head locked until it has been completely filled with new
784 * data. In this case, we should be able to make the assertion that
785 * the bh is not already part of an existing transaction.
786 *
787 * The buffer should already be locked by the caller by this point.
788 * There is no lock ranking violation: it was a newly created,
789 * unlocked buffer beforehand. */
790
791/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700792 * int jbd2_journal_get_create_access () - notify intent to use newly created bh
Dave Kleikamp470decc2006-10-11 01:20:57 -0700793 * @handle: transaction to new buffer to
794 * @bh: new buffer.
795 *
796 * Call this if you create a new bh.
797 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700798int jbd2_journal_get_create_access(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700799{
800 transaction_t *transaction = handle->h_transaction;
801 journal_t *journal = transaction->t_journal;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700802 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700803 int err;
804
805 jbd_debug(5, "journal_head %p\n", jh);
806 err = -EROFS;
807 if (is_handle_aborted(handle))
808 goto out;
809 err = 0;
810
811 JBUFFER_TRACE(jh, "entry");
812 /*
813 * The buffer may already belong to this transaction due to pre-zeroing
814 * in the filesystem's new_block code. It may also be on the previous,
815 * committing transaction's lists, but it HAS to be in Forget state in
816 * that case: the transaction must have deleted the buffer for it to be
817 * reused here.
818 */
819 jbd_lock_bh_state(bh);
820 spin_lock(&journal->j_list_lock);
821 J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
822 jh->b_transaction == NULL ||
823 (jh->b_transaction == journal->j_committing_transaction &&
824 jh->b_jlist == BJ_Forget)));
825
826 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
827 J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
828
829 if (jh->b_transaction == NULL) {
Jan Karaf91d1d02009-07-13 16:16:20 -0400830 /*
831 * Previous jbd2_journal_forget() could have left the buffer
832 * with jbddirty bit set because it was being committed. When
833 * the commit finished, we've filed the buffer for
834 * checkpointing and marked it dirty. Now we are reallocating
835 * the buffer so the transaction freeing it must have
836 * committed and so it's safe to clear the dirty bit.
837 */
838 clear_buffer_dirty(jh2bh(jh));
Dave Kleikamp470decc2006-10-11 01:20:57 -0700839 jh->b_transaction = transaction;
Josef Bacik9fc7c632008-04-17 10:38:59 -0400840
841 /* first access by this transaction */
842 jh->b_modified = 0;
843
Dave Kleikamp470decc2006-10-11 01:20:57 -0700844 JBUFFER_TRACE(jh, "file as BJ_Reserved");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700845 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700846 } else if (jh->b_transaction == journal->j_committing_transaction) {
Josef Bacik9fc7c632008-04-17 10:38:59 -0400847 /* first access by this transaction */
848 jh->b_modified = 0;
849
Dave Kleikamp470decc2006-10-11 01:20:57 -0700850 JBUFFER_TRACE(jh, "set next transaction");
851 jh->b_next_transaction = transaction;
852 }
853 spin_unlock(&journal->j_list_lock);
854 jbd_unlock_bh_state(bh);
855
856 /*
857 * akpm: I added this. ext3_alloc_branch can pick up new indirect
858 * blocks which contain freed but then revoked metadata. We need
859 * to cancel the revoke in case we end up freeing it yet again
860 * and the reallocating as data - this would cause a second revoke,
861 * which hits an assertion error.
862 */
863 JBUFFER_TRACE(jh, "cancelling revoke");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700864 jbd2_journal_cancel_revoke(handle, jh);
865 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700866out:
867 return err;
868}
869
870/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700871 * int jbd2_journal_get_undo_access() - Notify intent to modify metadata with
Dave Kleikamp470decc2006-10-11 01:20:57 -0700872 * non-rewindable consequences
873 * @handle: transaction
874 * @bh: buffer to undo
875 * @credits: store the number of taken credits here (if not NULL)
876 *
877 * Sometimes there is a need to distinguish between metadata which has
878 * been committed to disk and that which has not. The ext3fs code uses
879 * this for freeing and allocating space, we have to make sure that we
880 * do not reuse freed space until the deallocation has been committed,
881 * since if we overwrote that space we would make the delete
882 * un-rewindable in case of a crash.
883 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700884 * To deal with that, jbd2_journal_get_undo_access requests write access to a
Dave Kleikamp470decc2006-10-11 01:20:57 -0700885 * buffer for parts of non-rewindable operations such as delete
886 * operations on the bitmaps. The journaling code must keep a copy of
887 * the buffer's contents prior to the undo_access call until such time
888 * as we know that the buffer has definitely been committed to disk.
889 *
890 * We never need to know which transaction the committed data is part
891 * of, buffers touched here are guaranteed to be dirtied later and so
892 * will be committed to a new transaction in due course, at which point
893 * we can discard the old committed data pointer.
894 *
895 * Returns error number or 0 on success.
896 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700897int jbd2_journal_get_undo_access(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700898{
899 int err;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700900 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700901 char *committed_data = NULL;
902
903 JBUFFER_TRACE(jh, "entry");
904
905 /*
906 * Do this first --- it can drop the journal lock, so we want to
907 * make sure that obtaining the committed_data is done
908 * atomically wrt. completion of any outstanding commits.
909 */
910 err = do_get_write_access(handle, jh, 1);
911 if (err)
912 goto out;
913
914repeat:
915 if (!jh->b_committed_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400916 committed_data = jbd2_alloc(jh2bh(jh)->b_size, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700917 if (!committed_data) {
918 printk(KERN_EMERG "%s: No memory for committed data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400919 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700920 err = -ENOMEM;
921 goto out;
922 }
923 }
924
925 jbd_lock_bh_state(bh);
926 if (!jh->b_committed_data) {
927 /* Copy out the current buffer contents into the
928 * preserved, committed copy. */
929 JBUFFER_TRACE(jh, "generate b_committed data");
930 if (!committed_data) {
931 jbd_unlock_bh_state(bh);
932 goto repeat;
933 }
934
935 jh->b_committed_data = committed_data;
936 committed_data = NULL;
937 memcpy(jh->b_committed_data, bh->b_data, bh->b_size);
938 }
939 jbd_unlock_bh_state(bh);
940out:
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700941 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700942 if (unlikely(committed_data))
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400943 jbd2_free(committed_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700944 return err;
945}
946
947/**
Joel Beckere06c8222008-09-11 15:35:47 -0700948 * void jbd2_journal_set_triggers() - Add triggers for commit writeout
949 * @bh: buffer to trigger on
950 * @type: struct jbd2_buffer_trigger_type containing the trigger(s).
951 *
952 * Set any triggers on this journal_head. This is always safe, because
953 * triggers for a committing buffer will be saved off, and triggers for
954 * a running transaction will match the buffer in that transaction.
955 *
956 * Call with NULL to clear the triggers.
957 */
958void jbd2_journal_set_triggers(struct buffer_head *bh,
959 struct jbd2_buffer_trigger_type *type)
960{
961 struct journal_head *jh = bh2jh(bh);
962
963 jh->b_triggers = type;
964}
965
966void jbd2_buffer_commit_trigger(struct journal_head *jh, void *mapped_data,
967 struct jbd2_buffer_trigger_type *triggers)
968{
969 struct buffer_head *bh = jh2bh(jh);
970
971 if (!triggers || !triggers->t_commit)
972 return;
973
974 triggers->t_commit(triggers, bh, mapped_data, bh->b_size);
975}
976
977void jbd2_buffer_abort_trigger(struct journal_head *jh,
978 struct jbd2_buffer_trigger_type *triggers)
979{
980 if (!triggers || !triggers->t_abort)
981 return;
982
983 triggers->t_abort(triggers, jh2bh(jh));
984}
985
986
987
988/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700989 * int jbd2_journal_dirty_metadata() - mark a buffer as containing dirty metadata
Dave Kleikamp470decc2006-10-11 01:20:57 -0700990 * @handle: transaction to add buffer to.
991 * @bh: buffer to mark
992 *
993 * mark dirty metadata which needs to be journaled as part of the current
994 * transaction.
995 *
996 * The buffer is placed on the transaction's metadata list and is marked
997 * as belonging to the transaction.
998 *
999 * Returns error number or 0 on success.
1000 *
1001 * Special care needs to be taken if the buffer already belongs to the
1002 * current committing transaction (in which case we should have frozen
1003 * data present for that commit). In that case, we don't relink the
1004 * buffer: that only gets done when the old transaction finally
1005 * completes its commit.
1006 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001007int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001008{
1009 transaction_t *transaction = handle->h_transaction;
1010 journal_t *journal = transaction->t_journal;
1011 struct journal_head *jh = bh2jh(bh);
1012
1013 jbd_debug(5, "journal_head %p\n", jh);
1014 JBUFFER_TRACE(jh, "entry");
1015 if (is_handle_aborted(handle))
1016 goto out;
1017
1018 jbd_lock_bh_state(bh);
1019
1020 if (jh->b_modified == 0) {
1021 /*
1022 * This buffer's got modified and becoming part
1023 * of the transaction. This needs to be done
1024 * once a transaction -bzzz
1025 */
1026 jh->b_modified = 1;
1027 J_ASSERT_JH(jh, handle->h_buffer_credits > 0);
1028 handle->h_buffer_credits--;
1029 }
1030
1031 /*
1032 * fastpath, to avoid expensive locking. If this buffer is already
1033 * on the running transaction's metadata list there is nothing to do.
1034 * Nobody can take it off again because there is a handle open.
1035 * I _think_ we're OK here with SMP barriers - a mistaken decision will
1036 * result in this test being false, so we go in and take the locks.
1037 */
1038 if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) {
1039 JBUFFER_TRACE(jh, "fastpath");
1040 J_ASSERT_JH(jh, jh->b_transaction ==
1041 journal->j_running_transaction);
1042 goto out_unlock_bh;
1043 }
1044
1045 set_buffer_jbddirty(bh);
1046
1047 /*
1048 * Metadata already on the current transaction list doesn't
1049 * need to be filed. Metadata on another transaction's list must
1050 * be committing, and will be refiled once the commit completes:
1051 * leave it alone for now.
1052 */
1053 if (jh->b_transaction != transaction) {
1054 JBUFFER_TRACE(jh, "already on other transaction");
1055 J_ASSERT_JH(jh, jh->b_transaction ==
1056 journal->j_committing_transaction);
1057 J_ASSERT_JH(jh, jh->b_next_transaction == transaction);
1058 /* And this case is illegal: we can't reuse another
1059 * transaction's data buffer, ever. */
1060 goto out_unlock_bh;
1061 }
1062
1063 /* That test should have eliminated the following case: */
Mingming Cao40191912008-01-28 23:58:27 -05001064 J_ASSERT_JH(jh, jh->b_frozen_data == NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001065
1066 JBUFFER_TRACE(jh, "file as BJ_Metadata");
1067 spin_lock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001068 __jbd2_journal_file_buffer(jh, handle->h_transaction, BJ_Metadata);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001069 spin_unlock(&journal->j_list_lock);
1070out_unlock_bh:
1071 jbd_unlock_bh_state(bh);
1072out:
1073 JBUFFER_TRACE(jh, "exit");
1074 return 0;
1075}
1076
1077/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001078 * jbd2_journal_release_buffer: undo a get_write_access without any buffer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001079 * updates, if the update decided in the end that it didn't need access.
1080 *
1081 */
1082void
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001083jbd2_journal_release_buffer(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001084{
1085 BUFFER_TRACE(bh, "entry");
1086}
1087
1088/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001089 * void jbd2_journal_forget() - bforget() for potentially-journaled buffers.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001090 * @handle: transaction handle
1091 * @bh: bh to 'forget'
1092 *
1093 * We can only do the bforget if there are no commits pending against the
1094 * buffer. If the buffer is dirty in the current running transaction we
1095 * can safely unlink it.
1096 *
1097 * bh may not be a journalled buffer at all - it may be a non-JBD
1098 * buffer which came off the hashtable. Check for this.
1099 *
1100 * Decrements bh->b_count by one.
1101 *
1102 * Allow this call even if the handle has aborted --- it may be part of
1103 * the caller's cleanup after an abort.
1104 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001105int jbd2_journal_forget (handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001106{
1107 transaction_t *transaction = handle->h_transaction;
1108 journal_t *journal = transaction->t_journal;
1109 struct journal_head *jh;
1110 int drop_reserve = 0;
1111 int err = 0;
Josef Bacik1dfc3222008-04-17 10:38:59 -04001112 int was_modified = 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001113
1114 BUFFER_TRACE(bh, "entry");
1115
1116 jbd_lock_bh_state(bh);
1117 spin_lock(&journal->j_list_lock);
1118
1119 if (!buffer_jbd(bh))
1120 goto not_jbd;
1121 jh = bh2jh(bh);
1122
1123 /* Critical error: attempting to delete a bitmap buffer, maybe?
1124 * Don't do any jbd operations, and return an error. */
1125 if (!J_EXPECT_JH(jh, !jh->b_committed_data,
1126 "inconsistent data on disk")) {
1127 err = -EIO;
1128 goto not_jbd;
1129 }
1130
Josef Bacik1dfc3222008-04-17 10:38:59 -04001131 /* keep track of wether or not this transaction modified us */
1132 was_modified = jh->b_modified;
1133
Dave Kleikamp470decc2006-10-11 01:20:57 -07001134 /*
1135 * The buffer's going from the transaction, we must drop
1136 * all references -bzzz
1137 */
1138 jh->b_modified = 0;
1139
1140 if (jh->b_transaction == handle->h_transaction) {
1141 J_ASSERT_JH(jh, !jh->b_frozen_data);
1142
1143 /* If we are forgetting a buffer which is already part
1144 * of this transaction, then we can just drop it from
1145 * the transaction immediately. */
1146 clear_buffer_dirty(bh);
1147 clear_buffer_jbddirty(bh);
1148
1149 JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
1150
Josef Bacik1dfc3222008-04-17 10:38:59 -04001151 /*
1152 * we only want to drop a reference if this transaction
1153 * modified the buffer
1154 */
1155 if (was_modified)
1156 drop_reserve = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001157
1158 /*
1159 * We are no longer going to journal this buffer.
1160 * However, the commit of this transaction is still
1161 * important to the buffer: the delete that we are now
1162 * processing might obsolete an old log entry, so by
1163 * committing, we can satisfy the buffer's checkpoint.
1164 *
1165 * So, if we have a checkpoint on the buffer, we should
1166 * now refile the buffer on our BJ_Forget list so that
1167 * we know to remove the checkpoint after we commit.
1168 */
1169
1170 if (jh->b_cp_transaction) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001171 __jbd2_journal_temp_unlink_buffer(jh);
1172 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001173 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001174 __jbd2_journal_unfile_buffer(jh);
1175 jbd2_journal_remove_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001176 __brelse(bh);
1177 if (!buffer_jbd(bh)) {
1178 spin_unlock(&journal->j_list_lock);
1179 jbd_unlock_bh_state(bh);
1180 __bforget(bh);
1181 goto drop;
1182 }
1183 }
1184 } else if (jh->b_transaction) {
1185 J_ASSERT_JH(jh, (jh->b_transaction ==
1186 journal->j_committing_transaction));
1187 /* However, if the buffer is still owned by a prior
1188 * (committing) transaction, we can't drop it yet... */
1189 JBUFFER_TRACE(jh, "belongs to older transaction");
1190 /* ... but we CAN drop it from the new transaction if we
1191 * have also modified it since the original commit. */
1192
1193 if (jh->b_next_transaction) {
1194 J_ASSERT(jh->b_next_transaction == transaction);
1195 jh->b_next_transaction = NULL;
Josef Bacik1dfc3222008-04-17 10:38:59 -04001196
1197 /*
1198 * only drop a reference if this transaction modified
1199 * the buffer
1200 */
1201 if (was_modified)
1202 drop_reserve = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001203 }
1204 }
1205
1206not_jbd:
1207 spin_unlock(&journal->j_list_lock);
1208 jbd_unlock_bh_state(bh);
1209 __brelse(bh);
1210drop:
1211 if (drop_reserve) {
1212 /* no need to reserve log space for this block -bzzz */
1213 handle->h_buffer_credits++;
1214 }
1215 return err;
1216}
1217
1218/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001219 * int jbd2_journal_stop() - complete a transaction
Dave Kleikamp470decc2006-10-11 01:20:57 -07001220 * @handle: tranaction to complete.
1221 *
1222 * All done for a particular handle.
1223 *
1224 * There is not much action needed here. We just return any remaining
1225 * buffer credits to the transaction and remove the handle. The only
1226 * complication is that we need to start a commit operation if the
1227 * filesystem is marked for synchronous update.
1228 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001229 * jbd2_journal_stop itself will not usually return an error, but it may
Dave Kleikamp470decc2006-10-11 01:20:57 -07001230 * do so in unusual circumstances. In particular, expect it to
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001231 * return -EIO if a jbd2_journal_abort has been executed since the
Dave Kleikamp470decc2006-10-11 01:20:57 -07001232 * transaction began.
1233 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001234int jbd2_journal_stop(handle_t *handle)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001235{
1236 transaction_t *transaction = handle->h_transaction;
1237 journal_t *journal = transaction->t_journal;
Josef Bacike07f7182008-11-26 01:14:26 -05001238 int err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001239 pid_t pid;
1240
Dave Kleikamp470decc2006-10-11 01:20:57 -07001241 J_ASSERT(journal_current_handle() == handle);
1242
1243 if (is_handle_aborted(handle))
1244 err = -EIO;
OGAWA Hirofumi3e2a5322006-10-19 23:29:11 -07001245 else {
1246 J_ASSERT(transaction->t_updates > 0);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001247 err = 0;
OGAWA Hirofumi3e2a5322006-10-19 23:29:11 -07001248 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001249
1250 if (--handle->h_ref > 0) {
1251 jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
1252 handle->h_ref);
1253 return err;
1254 }
1255
1256 jbd_debug(4, "Handle %p going down\n", handle);
1257
1258 /*
1259 * Implement synchronous transaction batching. If the handle
1260 * was synchronous, don't force a commit immediately. Let's
Josef Bacike07f7182008-11-26 01:14:26 -05001261 * yield and let another thread piggyback onto this
1262 * transaction. Keep doing that while new threads continue to
1263 * arrive. It doesn't cost much - we're about to run a commit
1264 * and sleep on IO anyway. Speeds up many-threaded, many-dir
1265 * operations by 30x or more...
Dave Kleikamp470decc2006-10-11 01:20:57 -07001266 *
Josef Bacike07f7182008-11-26 01:14:26 -05001267 * We try and optimize the sleep time against what the
1268 * underlying disk can do, instead of having a static sleep
1269 * time. This is useful for the case where our storage is so
1270 * fast that it is more optimal to go ahead and force a flush
1271 * and wait for the transaction to be committed than it is to
1272 * wait for an arbitrary amount of time for new writers to
1273 * join the transaction. We achieve this by measuring how
1274 * long it takes to commit a transaction, and compare it with
1275 * how long this transaction has been running, and if run time
1276 * < commit time then we sleep for the delta and commit. This
1277 * greatly helps super fast disks that would see slowdowns as
1278 * more threads started doing fsyncs.
1279 *
1280 * But don't do this if this process was the most recent one
1281 * to perform a synchronous write. We do this to detect the
1282 * case where a single process is doing a stream of sync
1283 * writes. No point in waiting for joiners in that case.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001284 */
1285 pid = current->pid;
1286 if (handle->h_sync && journal->j_last_sync_writer != pid) {
Josef Bacike07f7182008-11-26 01:14:26 -05001287 u64 commit_time, trans_time;
1288
Dave Kleikamp470decc2006-10-11 01:20:57 -07001289 journal->j_last_sync_writer = pid;
Josef Bacike07f7182008-11-26 01:14:26 -05001290
1291 spin_lock(&journal->j_state_lock);
1292 commit_time = journal->j_average_commit_time;
1293 spin_unlock(&journal->j_state_lock);
1294
1295 trans_time = ktime_to_ns(ktime_sub(ktime_get(),
1296 transaction->t_start_time));
1297
Theodore Ts'o30773842009-01-03 20:27:38 -05001298 commit_time = max_t(u64, commit_time,
1299 1000*journal->j_min_batch_time);
Josef Bacike07f7182008-11-26 01:14:26 -05001300 commit_time = min_t(u64, commit_time,
Theodore Ts'o30773842009-01-03 20:27:38 -05001301 1000*journal->j_max_batch_time);
Josef Bacike07f7182008-11-26 01:14:26 -05001302
1303 if (trans_time < commit_time) {
1304 ktime_t expires = ktime_add_ns(ktime_get(),
1305 commit_time);
1306 set_current_state(TASK_UNINTERRUPTIBLE);
1307 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1308 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001309 }
1310
Theodore Ts'o70585482009-03-25 23:35:46 -04001311 if (handle->h_sync)
1312 transaction->t_synchronous_commit = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001313 current->journal_info = NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001314 spin_lock(&transaction->t_handle_lock);
1315 transaction->t_outstanding_credits -= handle->h_buffer_credits;
1316 transaction->t_updates--;
1317 if (!transaction->t_updates) {
1318 wake_up(&journal->j_wait_updates);
1319 if (journal->j_barrier_count)
1320 wake_up(&journal->j_wait_transaction_locked);
1321 }
1322
1323 /*
1324 * If the handle is marked SYNC, we need to set another commit
1325 * going! We also want to force a commit if the current
1326 * transaction is occupying too much of the log, or if the
1327 * transaction is too old now.
1328 */
1329 if (handle->h_sync ||
1330 transaction->t_outstanding_credits >
1331 journal->j_max_transaction_buffers ||
1332 time_after_eq(jiffies, transaction->t_expires)) {
1333 /* Do this even for aborted journals: an abort still
1334 * completes the commit thread, it just doesn't write
1335 * anything to disk. */
1336 tid_t tid = transaction->t_tid;
1337
1338 spin_unlock(&transaction->t_handle_lock);
1339 jbd_debug(2, "transaction too old, requesting commit for "
1340 "handle %p\n", handle);
1341 /* This is non-blocking */
Theodore Ts'oc35a56a2010-05-16 05:00:00 -04001342 jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001343
1344 /*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001345 * Special case: JBD2_SYNC synchronous updates require us
Dave Kleikamp470decc2006-10-11 01:20:57 -07001346 * to wait for the commit to complete.
1347 */
1348 if (handle->h_sync && !(current->flags & PF_MEMALLOC))
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001349 err = jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001350 } else {
1351 spin_unlock(&transaction->t_handle_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001352 }
1353
Ingo Molnar3295f0e2008-08-11 10:30:30 +02001354 lock_map_release(&handle->h_lockdep_map);
Mingming Cao7b751062008-01-28 23:58:27 -05001355
Mingming Caoaf1e76d2007-10-16 18:38:25 -04001356 jbd2_free_handle(handle);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001357 return err;
1358}
1359
Randy Dunlap5648ba52008-04-17 10:38:59 -04001360/**
1361 * int jbd2_journal_force_commit() - force any uncommitted transactions
Dave Kleikamp470decc2006-10-11 01:20:57 -07001362 * @journal: journal to force
1363 *
1364 * For synchronous operations: force any uncommitted transactions
1365 * to disk. May seem kludgy, but it reuses all the handle batching
1366 * code in a very simple manner.
1367 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001368int jbd2_journal_force_commit(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001369{
1370 handle_t *handle;
1371 int ret;
1372
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001373 handle = jbd2_journal_start(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001374 if (IS_ERR(handle)) {
1375 ret = PTR_ERR(handle);
1376 } else {
1377 handle->h_sync = 1;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001378 ret = jbd2_journal_stop(handle);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001379 }
1380 return ret;
1381}
1382
1383/*
1384 *
1385 * List management code snippets: various functions for manipulating the
1386 * transaction buffer lists.
1387 *
1388 */
1389
1390/*
1391 * Append a buffer to a transaction list, given the transaction's list head
1392 * pointer.
1393 *
1394 * j_list_lock is held.
1395 *
1396 * jbd_lock_bh_state(jh2bh(jh)) is held.
1397 */
1398
1399static inline void
1400__blist_add_buffer(struct journal_head **list, struct journal_head *jh)
1401{
1402 if (!*list) {
1403 jh->b_tnext = jh->b_tprev = jh;
1404 *list = jh;
1405 } else {
1406 /* Insert at the tail of the list to preserve order */
1407 struct journal_head *first = *list, *last = first->b_tprev;
1408 jh->b_tprev = last;
1409 jh->b_tnext = first;
1410 last->b_tnext = first->b_tprev = jh;
1411 }
1412}
1413
1414/*
1415 * Remove a buffer from a transaction list, given the transaction's list
1416 * head pointer.
1417 *
1418 * Called with j_list_lock held, and the journal may not be locked.
1419 *
1420 * jbd_lock_bh_state(jh2bh(jh)) is held.
1421 */
1422
1423static inline void
1424__blist_del_buffer(struct journal_head **list, struct journal_head *jh)
1425{
1426 if (*list == jh) {
1427 *list = jh->b_tnext;
1428 if (*list == jh)
1429 *list = NULL;
1430 }
1431 jh->b_tprev->b_tnext = jh->b_tnext;
1432 jh->b_tnext->b_tprev = jh->b_tprev;
1433}
1434
1435/*
1436 * Remove a buffer from the appropriate transaction list.
1437 *
1438 * Note that this function can *change* the value of
Jan Kara87c89c22008-07-11 19:27:31 -04001439 * bh->b_transaction->t_buffers, t_forget, t_iobuf_list, t_shadow_list,
1440 * t_log_list or t_reserved_list. If the caller is holding onto a copy of one
1441 * of these pointers, it could go bad. Generally the caller needs to re-read
1442 * the pointer from the transaction_t.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001443 *
1444 * Called under j_list_lock. The journal may not be locked.
1445 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001446void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001447{
1448 struct journal_head **list = NULL;
1449 transaction_t *transaction;
1450 struct buffer_head *bh = jh2bh(jh);
1451
1452 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1453 transaction = jh->b_transaction;
1454 if (transaction)
1455 assert_spin_locked(&transaction->t_journal->j_list_lock);
1456
1457 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1458 if (jh->b_jlist != BJ_None)
Mingming Cao40191912008-01-28 23:58:27 -05001459 J_ASSERT_JH(jh, transaction != NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001460
1461 switch (jh->b_jlist) {
1462 case BJ_None:
1463 return;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001464 case BJ_Metadata:
1465 transaction->t_nr_buffers--;
1466 J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
1467 list = &transaction->t_buffers;
1468 break;
1469 case BJ_Forget:
1470 list = &transaction->t_forget;
1471 break;
1472 case BJ_IO:
1473 list = &transaction->t_iobuf_list;
1474 break;
1475 case BJ_Shadow:
1476 list = &transaction->t_shadow_list;
1477 break;
1478 case BJ_LogCtl:
1479 list = &transaction->t_log_list;
1480 break;
1481 case BJ_Reserved:
1482 list = &transaction->t_reserved_list;
1483 break;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001484 }
1485
1486 __blist_del_buffer(list, jh);
1487 jh->b_jlist = BJ_None;
1488 if (test_clear_buffer_jbddirty(bh))
1489 mark_buffer_dirty(bh); /* Expose it to the VM */
1490}
1491
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001492void __jbd2_journal_unfile_buffer(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001493{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001494 __jbd2_journal_temp_unlink_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001495 jh->b_transaction = NULL;
1496}
1497
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001498void jbd2_journal_unfile_buffer(journal_t *journal, struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001499{
1500 jbd_lock_bh_state(jh2bh(jh));
1501 spin_lock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001502 __jbd2_journal_unfile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001503 spin_unlock(&journal->j_list_lock);
1504 jbd_unlock_bh_state(jh2bh(jh));
1505}
1506
1507/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001508 * Called from jbd2_journal_try_to_free_buffers().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001509 *
1510 * Called under jbd_lock_bh_state(bh)
1511 */
1512static void
1513__journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh)
1514{
1515 struct journal_head *jh;
1516
1517 jh = bh2jh(bh);
1518
1519 if (buffer_locked(bh) || buffer_dirty(bh))
1520 goto out;
1521
Mingming Cao40191912008-01-28 23:58:27 -05001522 if (jh->b_next_transaction != NULL)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001523 goto out;
1524
1525 spin_lock(&journal->j_list_lock);
Jan Kara87c89c22008-07-11 19:27:31 -04001526 if (jh->b_cp_transaction != NULL && jh->b_transaction == NULL) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001527 /* written-back checkpointed metadata buffer */
1528 if (jh->b_jlist == BJ_None) {
1529 JBUFFER_TRACE(jh, "remove from checkpoint list");
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001530 __jbd2_journal_remove_checkpoint(jh);
1531 jbd2_journal_remove_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001532 __brelse(bh);
1533 }
1534 }
1535 spin_unlock(&journal->j_list_lock);
1536out:
1537 return;
1538}
1539
Dave Kleikamp470decc2006-10-11 01:20:57 -07001540/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001541 * int jbd2_journal_try_to_free_buffers() - try to free page buffers.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001542 * @journal: journal for operation
1543 * @page: to try and free
Mingming Cao530576b2008-07-13 21:06:39 -04001544 * @gfp_mask: we use the mask to detect how hard should we try to release
1545 * buffers. If __GFP_WAIT and __GFP_FS is set, we wait for commit code to
1546 * release the buffers.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001547 *
1548 *
1549 * For all the buffers on this page,
1550 * if they are fully written out ordered data, move them onto BUF_CLEAN
1551 * so try_to_free_buffers() can reap them.
1552 *
1553 * This function returns non-zero if we wish try_to_free_buffers()
1554 * to be called. We do this if the page is releasable by try_to_free_buffers().
1555 * We also do it if the page has locked or dirty buffers and the caller wants
1556 * us to perform sync or async writeout.
1557 *
1558 * This complicates JBD locking somewhat. We aren't protected by the
1559 * BKL here. We wish to remove the buffer from its committing or
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001560 * running transaction's ->t_datalist via __jbd2_journal_unfile_buffer.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001561 *
1562 * This may *change* the value of transaction_t->t_datalist, so anyone
1563 * who looks at t_datalist needs to lock against this function.
1564 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001565 * Even worse, someone may be doing a jbd2_journal_dirty_data on this
1566 * buffer. So we need to lock against that. jbd2_journal_dirty_data()
Dave Kleikamp470decc2006-10-11 01:20:57 -07001567 * will come out of the lock with the buffer dirty, which makes it
1568 * ineligible for release here.
1569 *
1570 * Who else is affected by this? hmm... Really the only contender
1571 * is do_get_write_access() - it could be looking at the buffer while
1572 * journal_try_to_free_buffer() is changing its state. But that
1573 * cannot happen because we never reallocate freed data as metadata
1574 * while the data is part of a transaction. Yes?
Mingming Cao530576b2008-07-13 21:06:39 -04001575 *
1576 * Return 0 on failure, 1 on success
Dave Kleikamp470decc2006-10-11 01:20:57 -07001577 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001578int jbd2_journal_try_to_free_buffers(journal_t *journal,
Mingming Cao530576b2008-07-13 21:06:39 -04001579 struct page *page, gfp_t gfp_mask)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001580{
1581 struct buffer_head *head;
1582 struct buffer_head *bh;
1583 int ret = 0;
1584
1585 J_ASSERT(PageLocked(page));
1586
1587 head = page_buffers(page);
1588 bh = head;
1589 do {
1590 struct journal_head *jh;
1591
1592 /*
1593 * We take our own ref against the journal_head here to avoid
1594 * having to add tons of locking around each instance of
Mingming Cao530576b2008-07-13 21:06:39 -04001595 * jbd2_journal_remove_journal_head() and
1596 * jbd2_journal_put_journal_head().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001597 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001598 jh = jbd2_journal_grab_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001599 if (!jh)
1600 continue;
1601
1602 jbd_lock_bh_state(bh);
1603 __journal_try_to_free_buffer(journal, bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001604 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001605 jbd_unlock_bh_state(bh);
1606 if (buffer_jbd(bh))
1607 goto busy;
1608 } while ((bh = bh->b_this_page) != head);
Mingming Cao530576b2008-07-13 21:06:39 -04001609
Dave Kleikamp470decc2006-10-11 01:20:57 -07001610 ret = try_to_free_buffers(page);
Mingming Cao530576b2008-07-13 21:06:39 -04001611
Dave Kleikamp470decc2006-10-11 01:20:57 -07001612busy:
1613 return ret;
1614}
1615
1616/*
1617 * This buffer is no longer needed. If it is on an older transaction's
1618 * checkpoint list we need to record it on this transaction's forget list
1619 * to pin this buffer (and hence its checkpointing transaction) down until
1620 * this transaction commits. If the buffer isn't on a checkpoint list, we
1621 * release it.
1622 * Returns non-zero if JBD no longer has an interest in the buffer.
1623 *
1624 * Called under j_list_lock.
1625 *
1626 * Called under jbd_lock_bh_state(bh).
1627 */
1628static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
1629{
1630 int may_free = 1;
1631 struct buffer_head *bh = jh2bh(jh);
1632
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001633 __jbd2_journal_unfile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001634
1635 if (jh->b_cp_transaction) {
1636 JBUFFER_TRACE(jh, "on running+cp transaction");
Jan Karaf91d1d02009-07-13 16:16:20 -04001637 /*
1638 * We don't want to write the buffer anymore, clear the
1639 * bit so that we don't confuse checks in
1640 * __journal_file_buffer
1641 */
1642 clear_buffer_dirty(bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001643 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001644 may_free = 0;
1645 } else {
1646 JBUFFER_TRACE(jh, "on running transaction");
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001647 jbd2_journal_remove_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001648 __brelse(bh);
1649 }
1650 return may_free;
1651}
1652
1653/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001654 * jbd2_journal_invalidatepage
Dave Kleikamp470decc2006-10-11 01:20:57 -07001655 *
1656 * This code is tricky. It has a number of cases to deal with.
1657 *
1658 * There are two invariants which this code relies on:
1659 *
1660 * i_size must be updated on disk before we start calling invalidatepage on the
1661 * data.
1662 *
1663 * This is done in ext3 by defining an ext3_setattr method which
1664 * updates i_size before truncate gets going. By maintaining this
1665 * invariant, we can be sure that it is safe to throw away any buffers
1666 * attached to the current transaction: once the transaction commits,
1667 * we know that the data will not be needed.
1668 *
1669 * Note however that we can *not* throw away data belonging to the
1670 * previous, committing transaction!
1671 *
1672 * Any disk blocks which *are* part of the previous, committing
1673 * transaction (and which therefore cannot be discarded immediately) are
1674 * not going to be reused in the new running transaction
1675 *
1676 * The bitmap committed_data images guarantee this: any block which is
1677 * allocated in one transaction and removed in the next will be marked
1678 * as in-use in the committed_data bitmap, so cannot be reused until
1679 * the next transaction to delete the block commits. This means that
1680 * leaving committing buffers dirty is quite safe: the disk blocks
1681 * cannot be reallocated to a different file and so buffer aliasing is
1682 * not possible.
1683 *
1684 *
1685 * The above applies mainly to ordered data mode. In writeback mode we
1686 * don't make guarantees about the order in which data hits disk --- in
1687 * particular we don't guarantee that new dirty data is flushed before
1688 * transaction commit --- so it is always safe just to discard data
1689 * immediately in that mode. --sct
1690 */
1691
1692/*
1693 * The journal_unmap_buffer helper function returns zero if the buffer
1694 * concerned remains pinned as an anonymous buffer belonging to an older
1695 * transaction.
1696 *
1697 * We're outside-transaction here. Either or both of j_running_transaction
1698 * and j_committing_transaction may be NULL.
1699 */
1700static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh)
1701{
1702 transaction_t *transaction;
1703 struct journal_head *jh;
1704 int may_free = 1;
1705 int ret;
1706
1707 BUFFER_TRACE(bh, "entry");
1708
1709 /*
1710 * It is safe to proceed here without the j_list_lock because the
1711 * buffers cannot be stolen by try_to_free_buffers as long as we are
1712 * holding the page lock. --sct
1713 */
1714
1715 if (!buffer_jbd(bh))
1716 goto zap_buffer_unlocked;
1717
Jan Kara87c89c22008-07-11 19:27:31 -04001718 /* OK, we have data buffer in journaled mode */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001719 spin_lock(&journal->j_state_lock);
1720 jbd_lock_bh_state(bh);
1721 spin_lock(&journal->j_list_lock);
1722
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001723 jh = jbd2_journal_grab_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001724 if (!jh)
1725 goto zap_buffer_no_jh;
1726
dingdinghuaba869022010-02-15 16:35:42 -05001727 /*
1728 * We cannot remove the buffer from checkpoint lists until the
1729 * transaction adding inode to orphan list (let's call it T)
1730 * is committed. Otherwise if the transaction changing the
1731 * buffer would be cleaned from the journal before T is
1732 * committed, a crash will cause that the correct contents of
1733 * the buffer will be lost. On the other hand we have to
1734 * clear the buffer dirty bit at latest at the moment when the
1735 * transaction marking the buffer as freed in the filesystem
1736 * structures is committed because from that moment on the
1737 * buffer can be reallocated and used by a different page.
1738 * Since the block hasn't been freed yet but the inode has
1739 * already been added to orphan list, it is safe for us to add
1740 * the buffer to BJ_Forget list of the newest transaction.
1741 */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001742 transaction = jh->b_transaction;
1743 if (transaction == NULL) {
1744 /* First case: not on any transaction. If it
1745 * has no checkpoint link, then we can zap it:
1746 * it's a writeback-mode buffer so we don't care
1747 * if it hits disk safely. */
1748 if (!jh->b_cp_transaction) {
1749 JBUFFER_TRACE(jh, "not on any transaction: zap");
1750 goto zap_buffer;
1751 }
1752
1753 if (!buffer_dirty(bh)) {
1754 /* bdflush has written it. We can drop it now */
1755 goto zap_buffer;
1756 }
1757
1758 /* OK, it must be in the journal but still not
1759 * written fully to disk: it's metadata or
1760 * journaled data... */
1761
1762 if (journal->j_running_transaction) {
1763 /* ... and once the current transaction has
1764 * committed, the buffer won't be needed any
1765 * longer. */
1766 JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
1767 ret = __dispose_buffer(jh,
1768 journal->j_running_transaction);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001769 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001770 spin_unlock(&journal->j_list_lock);
1771 jbd_unlock_bh_state(bh);
1772 spin_unlock(&journal->j_state_lock);
1773 return ret;
1774 } else {
1775 /* There is no currently-running transaction. So the
1776 * orphan record which we wrote for this file must have
1777 * passed into commit. We must attach this buffer to
1778 * the committing transaction, if it exists. */
1779 if (journal->j_committing_transaction) {
1780 JBUFFER_TRACE(jh, "give to committing trans");
1781 ret = __dispose_buffer(jh,
1782 journal->j_committing_transaction);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001783 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001784 spin_unlock(&journal->j_list_lock);
1785 jbd_unlock_bh_state(bh);
1786 spin_unlock(&journal->j_state_lock);
1787 return ret;
1788 } else {
1789 /* The orphan record's transaction has
1790 * committed. We can cleanse this buffer */
1791 clear_buffer_jbddirty(bh);
1792 goto zap_buffer;
1793 }
1794 }
1795 } else if (transaction == journal->j_committing_transaction) {
Eric Sandeen9b579882006-10-28 10:38:28 -07001796 JBUFFER_TRACE(jh, "on committing transaction");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001797 /*
dingdinghuaba869022010-02-15 16:35:42 -05001798 * The buffer is committing, we simply cannot touch
1799 * it. So we just set j_next_transaction to the
1800 * running transaction (if there is one) and mark
1801 * buffer as freed so that commit code knows it should
1802 * clear dirty bits when it is done with the buffer.
1803 */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001804 set_buffer_freed(bh);
dingdinghuaba869022010-02-15 16:35:42 -05001805 if (journal->j_running_transaction && buffer_jbddirty(bh))
1806 jh->b_next_transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001807 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001808 spin_unlock(&journal->j_list_lock);
1809 jbd_unlock_bh_state(bh);
1810 spin_unlock(&journal->j_state_lock);
1811 return 0;
1812 } else {
1813 /* Good, the buffer belongs to the running transaction.
1814 * We are writing our own transaction's data, not any
1815 * previous one's, so it is safe to throw it away
1816 * (remember that we expect the filesystem to have set
1817 * i_size already for this truncate so recovery will not
1818 * expose the disk blocks we are discarding here.) */
1819 J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
Eric Sandeen9b579882006-10-28 10:38:28 -07001820 JBUFFER_TRACE(jh, "on running transaction");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001821 may_free = __dispose_buffer(jh, transaction);
1822 }
1823
1824zap_buffer:
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001825 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001826zap_buffer_no_jh:
1827 spin_unlock(&journal->j_list_lock);
1828 jbd_unlock_bh_state(bh);
1829 spin_unlock(&journal->j_state_lock);
1830zap_buffer_unlocked:
1831 clear_buffer_dirty(bh);
1832 J_ASSERT_BH(bh, !buffer_jbddirty(bh));
1833 clear_buffer_mapped(bh);
1834 clear_buffer_req(bh);
1835 clear_buffer_new(bh);
1836 bh->b_bdev = NULL;
1837 return may_free;
1838}
1839
1840/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001841 * void jbd2_journal_invalidatepage()
Dave Kleikamp470decc2006-10-11 01:20:57 -07001842 * @journal: journal to use for flush...
1843 * @page: page to flush
1844 * @offset: length of page to invalidate.
1845 *
1846 * Reap page buffers containing data after offset in page.
1847 *
1848 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001849void jbd2_journal_invalidatepage(journal_t *journal,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001850 struct page *page,
1851 unsigned long offset)
1852{
1853 struct buffer_head *head, *bh, *next;
1854 unsigned int curr_off = 0;
1855 int may_free = 1;
1856
1857 if (!PageLocked(page))
1858 BUG();
1859 if (!page_has_buffers(page))
1860 return;
1861
1862 /* We will potentially be playing with lists other than just the
1863 * data lists (especially for journaled data mode), so be
1864 * cautious in our locking. */
1865
1866 head = bh = page_buffers(page);
1867 do {
1868 unsigned int next_off = curr_off + bh->b_size;
1869 next = bh->b_this_page;
1870
1871 if (offset <= curr_off) {
1872 /* This block is wholly outside the truncation point */
1873 lock_buffer(bh);
1874 may_free &= journal_unmap_buffer(journal, bh);
1875 unlock_buffer(bh);
1876 }
1877 curr_off = next_off;
1878 bh = next;
1879
1880 } while (bh != head);
1881
1882 if (!offset) {
1883 if (may_free && try_to_free_buffers(page))
1884 J_ASSERT(!page_has_buffers(page));
1885 }
1886}
1887
1888/*
1889 * File a buffer on the given transaction list.
1890 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001891void __jbd2_journal_file_buffer(struct journal_head *jh,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001892 transaction_t *transaction, int jlist)
1893{
1894 struct journal_head **list = NULL;
1895 int was_dirty = 0;
1896 struct buffer_head *bh = jh2bh(jh);
1897
1898 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1899 assert_spin_locked(&transaction->t_journal->j_list_lock);
1900
1901 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1902 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
Mingming Cao40191912008-01-28 23:58:27 -05001903 jh->b_transaction == NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001904
1905 if (jh->b_transaction && jh->b_jlist == jlist)
1906 return;
1907
Dave Kleikamp470decc2006-10-11 01:20:57 -07001908 if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
1909 jlist == BJ_Shadow || jlist == BJ_Forget) {
Jan Karaf91d1d02009-07-13 16:16:20 -04001910 /*
1911 * For metadata buffers, we track dirty bit in buffer_jbddirty
1912 * instead of buffer_dirty. We should not see a dirty bit set
1913 * here because we clear it in do_get_write_access but e.g.
1914 * tune2fs can modify the sb and set the dirty bit at any time
1915 * so we try to gracefully handle that.
1916 */
1917 if (buffer_dirty(bh))
1918 warn_dirty_buffer(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001919 if (test_clear_buffer_dirty(bh) ||
1920 test_clear_buffer_jbddirty(bh))
1921 was_dirty = 1;
1922 }
1923
1924 if (jh->b_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001925 __jbd2_journal_temp_unlink_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001926 jh->b_transaction = transaction;
1927
1928 switch (jlist) {
1929 case BJ_None:
1930 J_ASSERT_JH(jh, !jh->b_committed_data);
1931 J_ASSERT_JH(jh, !jh->b_frozen_data);
1932 return;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001933 case BJ_Metadata:
1934 transaction->t_nr_buffers++;
1935 list = &transaction->t_buffers;
1936 break;
1937 case BJ_Forget:
1938 list = &transaction->t_forget;
1939 break;
1940 case BJ_IO:
1941 list = &transaction->t_iobuf_list;
1942 break;
1943 case BJ_Shadow:
1944 list = &transaction->t_shadow_list;
1945 break;
1946 case BJ_LogCtl:
1947 list = &transaction->t_log_list;
1948 break;
1949 case BJ_Reserved:
1950 list = &transaction->t_reserved_list;
1951 break;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001952 }
1953
1954 __blist_add_buffer(list, jh);
1955 jh->b_jlist = jlist;
1956
1957 if (was_dirty)
1958 set_buffer_jbddirty(bh);
1959}
1960
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001961void jbd2_journal_file_buffer(struct journal_head *jh,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001962 transaction_t *transaction, int jlist)
1963{
1964 jbd_lock_bh_state(jh2bh(jh));
1965 spin_lock(&transaction->t_journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001966 __jbd2_journal_file_buffer(jh, transaction, jlist);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001967 spin_unlock(&transaction->t_journal->j_list_lock);
1968 jbd_unlock_bh_state(jh2bh(jh));
1969}
1970
1971/*
1972 * Remove a buffer from its current buffer list in preparation for
1973 * dropping it from its current transaction entirely. If the buffer has
1974 * already started to be used by a subsequent transaction, refile the
1975 * buffer on that transaction's metadata list.
1976 *
1977 * Called under journal->j_list_lock
1978 *
1979 * Called under jbd_lock_bh_state(jh2bh(jh))
1980 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001981void __jbd2_journal_refile_buffer(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001982{
dingdinghuaba869022010-02-15 16:35:42 -05001983 int was_dirty, jlist;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001984 struct buffer_head *bh = jh2bh(jh);
1985
1986 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1987 if (jh->b_transaction)
1988 assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
1989
1990 /* If the buffer is now unused, just drop it. */
1991 if (jh->b_next_transaction == NULL) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001992 __jbd2_journal_unfile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001993 return;
1994 }
1995
1996 /*
1997 * It has been modified by a later transaction: add it to the new
1998 * transaction's metadata list.
1999 */
2000
2001 was_dirty = test_clear_buffer_jbddirty(bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002002 __jbd2_journal_temp_unlink_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002003 jh->b_transaction = jh->b_next_transaction;
2004 jh->b_next_transaction = NULL;
dingdinghuaba869022010-02-15 16:35:42 -05002005 if (buffer_freed(bh))
2006 jlist = BJ_Forget;
2007 else if (jh->b_modified)
2008 jlist = BJ_Metadata;
2009 else
2010 jlist = BJ_Reserved;
2011 __jbd2_journal_file_buffer(jh, jh->b_transaction, jlist);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002012 J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
2013
2014 if (was_dirty)
2015 set_buffer_jbddirty(bh);
2016}
2017
2018/*
2019 * For the unlocked version of this call, also make sure that any
2020 * hanging journal_head is cleaned up if necessary.
2021 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002022 * __jbd2_journal_refile_buffer is usually called as part of a single locked
Dave Kleikamp470decc2006-10-11 01:20:57 -07002023 * operation on a buffer_head, in which the caller is probably going to
2024 * be hooking the journal_head onto other lists. In that case it is up
2025 * to the caller to remove the journal_head if necessary. For the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002026 * unlocked jbd2_journal_refile_buffer call, the caller isn't going to be
Dave Kleikamp470decc2006-10-11 01:20:57 -07002027 * doing anything else to the buffer so we need to do the cleanup
2028 * ourselves to avoid a jh leak.
2029 *
2030 * *** The journal_head may be freed by this call! ***
2031 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002032void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002033{
2034 struct buffer_head *bh = jh2bh(jh);
2035
2036 jbd_lock_bh_state(bh);
2037 spin_lock(&journal->j_list_lock);
2038
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002039 __jbd2_journal_refile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002040 jbd_unlock_bh_state(bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002041 jbd2_journal_remove_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002042
2043 spin_unlock(&journal->j_list_lock);
2044 __brelse(bh);
2045}
Jan Karac851ed52008-07-11 19:27:31 -04002046
2047/*
2048 * File inode in the inode list of the handle's transaction
2049 */
2050int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode)
2051{
2052 transaction_t *transaction = handle->h_transaction;
2053 journal_t *journal = transaction->t_journal;
2054
2055 if (is_handle_aborted(handle))
2056 return -EIO;
2057
2058 jbd_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino,
2059 transaction->t_tid);
2060
2061 /*
2062 * First check whether inode isn't already on the transaction's
2063 * lists without taking the lock. Note that this check is safe
2064 * without the lock as we cannot race with somebody removing inode
2065 * from the transaction. The reason is that we remove inode from the
2066 * transaction only in journal_release_jbd_inode() and when we commit
2067 * the transaction. We are guarded from the first case by holding
2068 * a reference to the inode. We are safe against the second case
2069 * because if jinode->i_transaction == transaction, commit code
2070 * cannot touch the transaction because we hold reference to it,
2071 * and if jinode->i_next_transaction == transaction, commit code
2072 * will only file the inode where we want it.
2073 */
2074 if (jinode->i_transaction == transaction ||
2075 jinode->i_next_transaction == transaction)
2076 return 0;
2077
2078 spin_lock(&journal->j_list_lock);
2079
2080 if (jinode->i_transaction == transaction ||
2081 jinode->i_next_transaction == transaction)
2082 goto done;
2083
2084 /* On some different transaction's list - should be
2085 * the committing one */
2086 if (jinode->i_transaction) {
2087 J_ASSERT(jinode->i_next_transaction == NULL);
2088 J_ASSERT(jinode->i_transaction ==
2089 journal->j_committing_transaction);
2090 jinode->i_next_transaction = transaction;
2091 goto done;
2092 }
2093 /* Not on any transaction list... */
2094 J_ASSERT(!jinode->i_next_transaction);
2095 jinode->i_transaction = transaction;
2096 list_add(&jinode->i_list, &transaction->t_inode_list);
2097done:
2098 spin_unlock(&journal->j_list_lock);
2099
2100 return 0;
2101}
2102
2103/*
Jan Kara7f5aa212009-02-10 11:15:34 -05002104 * File truncate and transaction commit interact with each other in a
2105 * non-trivial way. If a transaction writing data block A is
2106 * committing, we cannot discard the data by truncate until we have
2107 * written them. Otherwise if we crashed after the transaction with
2108 * write has committed but before the transaction with truncate has
2109 * committed, we could see stale data in block A. This function is a
2110 * helper to solve this problem. It starts writeout of the truncated
2111 * part in case it is in the committing transaction.
2112 *
2113 * Filesystem code must call this function when inode is journaled in
2114 * ordered mode before truncation happens and after the inode has been
2115 * placed on orphan list with the new inode size. The second condition
2116 * avoids the race that someone writes new data and we start
2117 * committing the transaction after this function has been called but
2118 * before a transaction for truncate is started (and furthermore it
2119 * allows us to optimize the case where the addition to orphan list
2120 * happens in the same transaction as write --- we don't have to write
2121 * any data in such case).
Jan Karac851ed52008-07-11 19:27:31 -04002122 */
Jan Kara7f5aa212009-02-10 11:15:34 -05002123int jbd2_journal_begin_ordered_truncate(journal_t *journal,
2124 struct jbd2_inode *jinode,
Jan Karac851ed52008-07-11 19:27:31 -04002125 loff_t new_size)
2126{
Jan Kara7f5aa212009-02-10 11:15:34 -05002127 transaction_t *inode_trans, *commit_trans;
Jan Karac851ed52008-07-11 19:27:31 -04002128 int ret = 0;
2129
Jan Kara7f5aa212009-02-10 11:15:34 -05002130 /* This is a quick check to avoid locking if not necessary */
2131 if (!jinode->i_transaction)
Jan Karac851ed52008-07-11 19:27:31 -04002132 goto out;
Jan Kara7f5aa212009-02-10 11:15:34 -05002133 /* Locks are here just to force reading of recent values, it is
2134 * enough that the transaction was not committing before we started
2135 * a transaction adding the inode to orphan list */
Jan Karac851ed52008-07-11 19:27:31 -04002136 spin_lock(&journal->j_state_lock);
2137 commit_trans = journal->j_committing_transaction;
2138 spin_unlock(&journal->j_state_lock);
Jan Kara7f5aa212009-02-10 11:15:34 -05002139 spin_lock(&journal->j_list_lock);
2140 inode_trans = jinode->i_transaction;
2141 spin_unlock(&journal->j_list_lock);
2142 if (inode_trans == commit_trans) {
2143 ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping,
Jan Karac851ed52008-07-11 19:27:31 -04002144 new_size, LLONG_MAX);
2145 if (ret)
2146 jbd2_journal_abort(journal, ret);
2147 }
2148out:
2149 return ret;
2150}