blob: 996ffda06bf306fde630e833f91332b745107689 [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. */
Mingming Caodb857da2008-01-28 23:58:27 -050060 journal->j_commit_timer.expires = round_jiffies(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);
241out:
242 if (unlikely(new_transaction)) /* It's usually NULL */
243 kfree(new_transaction);
244 return ret;
245}
246
Mingming Cao7b751062008-01-28 23:58:27 -0500247static struct lock_class_key jbd2_handle_key;
248
Dave Kleikamp470decc2006-10-11 01:20:57 -0700249/* Allocate a new handle. This should probably be in a slab... */
250static handle_t *new_handle(int nblocks)
251{
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400252 handle_t *handle = jbd2_alloc_handle(GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700253 if (!handle)
254 return NULL;
255 memset(handle, 0, sizeof(*handle));
256 handle->h_buffer_credits = nblocks;
257 handle->h_ref = 1;
258
Mingming Cao7b751062008-01-28 23:58:27 -0500259 lockdep_init_map(&handle->h_lockdep_map, "jbd2_handle",
260 &jbd2_handle_key, 0);
261
Dave Kleikamp470decc2006-10-11 01:20:57 -0700262 return handle;
263}
264
265/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700266 * handle_t *jbd2_journal_start() - Obtain a new handle.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700267 * @journal: Journal to start transaction on.
268 * @nblocks: number of block buffer we might modify
269 *
270 * We make sure that the transaction can guarantee at least nblocks of
271 * modified buffers in the log. We block until the log can guarantee
272 * that much space.
273 *
274 * This function is visible to journal users (like ext3fs), so is not
275 * called with the journal already locked.
276 *
277 * Return a pointer to a newly allocated handle, or NULL on failure
278 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700279handle_t *jbd2_journal_start(journal_t *journal, int nblocks)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700280{
281 handle_t *handle = journal_current_handle();
282 int err;
283
284 if (!journal)
285 return ERR_PTR(-EROFS);
286
287 if (handle) {
288 J_ASSERT(handle->h_transaction->t_journal == journal);
289 handle->h_ref++;
290 return handle;
291 }
292
293 handle = new_handle(nblocks);
294 if (!handle)
295 return ERR_PTR(-ENOMEM);
296
297 current->journal_info = handle;
298
299 err = start_this_handle(journal, handle);
300 if (err < 0) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400301 jbd2_free_handle(handle);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700302 current->journal_info = NULL;
303 handle = ERR_PTR(err);
Mingming Cao7b751062008-01-28 23:58:27 -0500304 goto out;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700305 }
Mingming Cao7b751062008-01-28 23:58:27 -0500306
Ingo Molnar3295f0e2008-08-11 10:30:30 +0200307 lock_map_acquire(&handle->h_lockdep_map);
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
429 handle->h_buffer_credits = nblocks;
430 ret = start_this_handle(journal, handle);
431 return ret;
432}
433
434
435/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700436 * void jbd2_journal_lock_updates () - establish a transaction barrier.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700437 * @journal: Journal to establish a barrier on.
438 *
439 * This locks out any further updates from being started, and blocks
440 * until all existing updates have completed, returning only once the
441 * journal is in a quiescent state with no updates running.
442 *
443 * The journal lock should not be held on entry.
444 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700445void jbd2_journal_lock_updates(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700446{
447 DEFINE_WAIT(wait);
448
449 spin_lock(&journal->j_state_lock);
450 ++journal->j_barrier_count;
451
452 /* Wait until there are no running updates */
453 while (1) {
454 transaction_t *transaction = journal->j_running_transaction;
455
456 if (!transaction)
457 break;
458
459 spin_lock(&transaction->t_handle_lock);
460 if (!transaction->t_updates) {
461 spin_unlock(&transaction->t_handle_lock);
462 break;
463 }
464 prepare_to_wait(&journal->j_wait_updates, &wait,
465 TASK_UNINTERRUPTIBLE);
466 spin_unlock(&transaction->t_handle_lock);
467 spin_unlock(&journal->j_state_lock);
468 schedule();
469 finish_wait(&journal->j_wait_updates, &wait);
470 spin_lock(&journal->j_state_lock);
471 }
472 spin_unlock(&journal->j_state_lock);
473
474 /*
475 * We have now established a barrier against other normal updates, but
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700476 * we also need to barrier against other jbd2_journal_lock_updates() calls
Dave Kleikamp470decc2006-10-11 01:20:57 -0700477 * to make sure that we serialise special journal-locked operations
478 * too.
479 */
480 mutex_lock(&journal->j_barrier);
481}
482
483/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700484 * void jbd2_journal_unlock_updates (journal_t* journal) - release barrier
Dave Kleikamp470decc2006-10-11 01:20:57 -0700485 * @journal: Journal to release the barrier on.
486 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700487 * Release a transaction barrier obtained with jbd2_journal_lock_updates().
Dave Kleikamp470decc2006-10-11 01:20:57 -0700488 *
489 * Should be called without the journal lock held.
490 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700491void jbd2_journal_unlock_updates (journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700492{
493 J_ASSERT(journal->j_barrier_count != 0);
494
495 mutex_unlock(&journal->j_barrier);
496 spin_lock(&journal->j_state_lock);
497 --journal->j_barrier_count;
498 spin_unlock(&journal->j_state_lock);
499 wake_up(&journal->j_wait_transaction_locked);
500}
501
502/*
503 * Report any unexpected dirty buffers which turn up. Normally those
504 * indicate an error, but they can occur if the user is running (say)
505 * tune2fs to modify the live filesystem, so we need the option of
506 * continuing as gracefully as possible. #
507 *
508 * The caller should already hold the journal lock and
509 * j_list_lock spinlock: most callers will need those anyway
510 * in order to probe the buffer's journaling state safely.
511 */
512static void jbd_unexpected_dirty_buffer(struct journal_head *jh)
513{
514 int jlist;
515
516 /* If this buffer is one which might reasonably be dirty
517 * --- ie. data, or not part of this journal --- then
518 * we're OK to leave it alone, but otherwise we need to
519 * move the dirty bit to the journal's own internal
520 * JBDDirty bit. */
521 jlist = jh->b_jlist;
522
523 if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
524 jlist == BJ_Shadow || jlist == BJ_Forget) {
525 struct buffer_head *bh = jh2bh(jh);
526
527 if (test_clear_buffer_dirty(bh))
528 set_buffer_jbddirty(bh);
529 }
530}
531
532/*
533 * If the buffer is already part of the current transaction, then there
534 * is nothing we need to do. If it is already part of a prior
535 * transaction which we are still committing to disk, then we need to
536 * make sure that we do not overwrite the old copy: we do copy-out to
537 * preserve the copy going to disk. We also account the buffer against
538 * the handle's metadata buffer credits (unless the buffer is already
539 * part of the transaction, that is).
540 *
541 */
542static int
543do_get_write_access(handle_t *handle, struct journal_head *jh,
544 int force_copy)
545{
546 struct buffer_head *bh;
547 transaction_t *transaction;
548 journal_t *journal;
549 int error;
550 char *frozen_buffer = NULL;
551 int need_copy = 0;
552
553 if (is_handle_aborted(handle))
554 return -EROFS;
555
556 transaction = handle->h_transaction;
557 journal = transaction->t_journal;
558
559 jbd_debug(5, "buffer_head %p, force_copy %d\n", jh, force_copy);
560
561 JBUFFER_TRACE(jh, "entry");
562repeat:
563 bh = jh2bh(jh);
564
565 /* @@@ Need to check for errors here at some point. */
566
567 lock_buffer(bh);
568 jbd_lock_bh_state(bh);
569
570 /* We now hold the buffer lock so it is safe to query the buffer
571 * state. Is the buffer dirty?
572 *
573 * If so, there are two possibilities. The buffer may be
574 * non-journaled, and undergoing a quite legitimate writeback.
575 * Otherwise, it is journaled, and we don't expect dirty buffers
576 * in that state (the buffers should be marked JBD_Dirty
577 * instead.) So either the IO is being done under our own
578 * control and this is a bug, or it's a third party IO such as
579 * dump(8) (which may leave the buffer scheduled for read ---
580 * ie. locked but not dirty) or tune2fs (which may actually have
581 * the buffer dirtied, ugh.) */
582
583 if (buffer_dirty(bh)) {
584 /*
585 * First question: is this buffer already part of the current
586 * transaction or the existing committing transaction?
587 */
588 if (jh->b_transaction) {
589 J_ASSERT_JH(jh,
590 jh->b_transaction == transaction ||
591 jh->b_transaction ==
592 journal->j_committing_transaction);
593 if (jh->b_next_transaction)
594 J_ASSERT_JH(jh, jh->b_next_transaction ==
595 transaction);
596 }
597 /*
598 * In any case we need to clean the dirty flag and we must
599 * do it under the buffer lock to be sure we don't race
600 * with running write-out.
601 */
602 JBUFFER_TRACE(jh, "Unexpected dirty buffer");
603 jbd_unexpected_dirty_buffer(jh);
604 }
605
606 unlock_buffer(bh);
607
608 error = -EROFS;
609 if (is_handle_aborted(handle)) {
610 jbd_unlock_bh_state(bh);
611 goto out;
612 }
613 error = 0;
614
615 /*
616 * The buffer is already part of this transaction if b_transaction or
617 * b_next_transaction points to it
618 */
619 if (jh->b_transaction == transaction ||
620 jh->b_next_transaction == transaction)
621 goto done;
622
623 /*
Josef Bacik9fc7c632008-04-17 10:38:59 -0400624 * this is the first time this transaction is touching this buffer,
625 * reset the modified flag
626 */
627 jh->b_modified = 0;
628
629 /*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700630 * If there is already a copy-out version of this buffer, then we don't
631 * need to make another one
632 */
633 if (jh->b_frozen_data) {
634 JBUFFER_TRACE(jh, "has frozen data");
635 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
636 jh->b_next_transaction = transaction;
637 goto done;
638 }
639
640 /* Is there data here we need to preserve? */
641
642 if (jh->b_transaction && jh->b_transaction != transaction) {
643 JBUFFER_TRACE(jh, "owned by older transaction");
644 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
645 J_ASSERT_JH(jh, jh->b_transaction ==
646 journal->j_committing_transaction);
647
648 /* There is one case we have to be very careful about.
649 * If the committing transaction is currently writing
650 * this buffer out to disk and has NOT made a copy-out,
651 * then we cannot modify the buffer contents at all
652 * right now. The essence of copy-out is that it is the
653 * extra copy, not the primary copy, which gets
654 * journaled. If the primary copy is already going to
655 * disk then we cannot do copy-out here. */
656
657 if (jh->b_jlist == BJ_Shadow) {
658 DEFINE_WAIT_BIT(wait, &bh->b_state, BH_Unshadow);
659 wait_queue_head_t *wqh;
660
661 wqh = bit_waitqueue(&bh->b_state, BH_Unshadow);
662
663 JBUFFER_TRACE(jh, "on shadow: sleep");
664 jbd_unlock_bh_state(bh);
665 /* commit wakes up all shadow buffers after IO */
666 for ( ; ; ) {
667 prepare_to_wait(wqh, &wait.wait,
668 TASK_UNINTERRUPTIBLE);
669 if (jh->b_jlist != BJ_Shadow)
670 break;
671 schedule();
672 }
673 finish_wait(wqh, &wait.wait);
674 goto repeat;
675 }
676
677 /* Only do the copy if the currently-owning transaction
678 * still needs it. If it is on the Forget list, the
679 * committing transaction is past that stage. The
680 * buffer had better remain locked during the kmalloc,
681 * but that should be true --- we hold the journal lock
682 * still and the buffer is already on the BUF_JOURNAL
683 * list so won't be flushed.
684 *
685 * Subtle point, though: if this is a get_undo_access,
686 * then we will be relying on the frozen_data to contain
687 * the new value of the committed_data record after the
688 * transaction, so we HAVE to force the frozen_data copy
689 * in that case. */
690
691 if (jh->b_jlist != BJ_Forget || force_copy) {
692 JBUFFER_TRACE(jh, "generate frozen data");
693 if (!frozen_buffer) {
694 JBUFFER_TRACE(jh, "allocate memory for buffer");
695 jbd_unlock_bh_state(bh);
696 frozen_buffer =
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400697 jbd2_alloc(jh2bh(jh)->b_size,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700698 GFP_NOFS);
699 if (!frozen_buffer) {
700 printk(KERN_EMERG
701 "%s: OOM for frozen_buffer\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400702 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700703 JBUFFER_TRACE(jh, "oom!");
704 error = -ENOMEM;
705 jbd_lock_bh_state(bh);
706 goto done;
707 }
708 goto repeat;
709 }
710 jh->b_frozen_data = frozen_buffer;
711 frozen_buffer = NULL;
712 need_copy = 1;
713 }
714 jh->b_next_transaction = transaction;
715 }
716
717
718 /*
719 * Finally, if the buffer is not journaled right now, we need to make
720 * sure it doesn't get written to disk before the caller actually
721 * commits the new data
722 */
723 if (!jh->b_transaction) {
724 JBUFFER_TRACE(jh, "no transaction");
725 J_ASSERT_JH(jh, !jh->b_next_transaction);
726 jh->b_transaction = transaction;
727 JBUFFER_TRACE(jh, "file as BJ_Reserved");
728 spin_lock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700729 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700730 spin_unlock(&journal->j_list_lock);
731 }
732
733done:
734 if (need_copy) {
735 struct page *page;
736 int offset;
737 char *source;
738
739 J_EXPECT_JH(jh, buffer_uptodate(jh2bh(jh)),
740 "Possible IO failure.\n");
741 page = jh2bh(jh)->b_page;
742 offset = ((unsigned long) jh2bh(jh)->b_data) & ~PAGE_MASK;
743 source = kmap_atomic(page, KM_USER0);
744 memcpy(jh->b_frozen_data, source+offset, jh2bh(jh)->b_size);
745 kunmap_atomic(source, KM_USER0);
Joel Beckere06c8222008-09-11 15:35:47 -0700746
747 /*
748 * Now that the frozen data is saved off, we need to store
749 * any matching triggers.
750 */
751 jh->b_frozen_triggers = jh->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700752 }
753 jbd_unlock_bh_state(bh);
754
755 /*
756 * If we are about to journal a buffer, then any revoke pending on it is
757 * no longer valid
758 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700759 jbd2_journal_cancel_revoke(handle, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700760
761out:
762 if (unlikely(frozen_buffer)) /* It's usually NULL */
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400763 jbd2_free(frozen_buffer, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700764
765 JBUFFER_TRACE(jh, "exit");
766 return error;
767}
768
769/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700770 * int jbd2_journal_get_write_access() - notify intent to modify a buffer for metadata (not data) update.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700771 * @handle: transaction to add buffer modifications to
772 * @bh: bh to be used for metadata writes
773 * @credits: variable that will receive credits for the buffer
774 *
775 * Returns an error code or 0 on success.
776 *
777 * In full data journalling mode the buffer may be of type BJ_AsyncData,
778 * because we're write()ing a buffer which is also part of a shared mapping.
779 */
780
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700781int jbd2_journal_get_write_access(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700782{
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700783 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700784 int rc;
785
786 /* We do not want to get caught playing with fields which the
787 * log thread also manipulates. Make sure that the buffer
788 * completes any outstanding IO before proceeding. */
789 rc = do_get_write_access(handle, jh, 0);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700790 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700791 return rc;
792}
793
794
795/*
796 * When the user wants to journal a newly created buffer_head
797 * (ie. getblk() returned a new buffer and we are going to populate it
798 * manually rather than reading off disk), then we need to keep the
799 * buffer_head locked until it has been completely filled with new
800 * data. In this case, we should be able to make the assertion that
801 * the bh is not already part of an existing transaction.
802 *
803 * The buffer should already be locked by the caller by this point.
804 * There is no lock ranking violation: it was a newly created,
805 * unlocked buffer beforehand. */
806
807/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700808 * int jbd2_journal_get_create_access () - notify intent to use newly created bh
Dave Kleikamp470decc2006-10-11 01:20:57 -0700809 * @handle: transaction to new buffer to
810 * @bh: new buffer.
811 *
812 * Call this if you create a new bh.
813 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700814int jbd2_journal_get_create_access(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700815{
816 transaction_t *transaction = handle->h_transaction;
817 journal_t *journal = transaction->t_journal;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700818 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700819 int err;
820
821 jbd_debug(5, "journal_head %p\n", jh);
822 err = -EROFS;
823 if (is_handle_aborted(handle))
824 goto out;
825 err = 0;
826
827 JBUFFER_TRACE(jh, "entry");
828 /*
829 * The buffer may already belong to this transaction due to pre-zeroing
830 * in the filesystem's new_block code. It may also be on the previous,
831 * committing transaction's lists, but it HAS to be in Forget state in
832 * that case: the transaction must have deleted the buffer for it to be
833 * reused here.
834 */
835 jbd_lock_bh_state(bh);
836 spin_lock(&journal->j_list_lock);
837 J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
838 jh->b_transaction == NULL ||
839 (jh->b_transaction == journal->j_committing_transaction &&
840 jh->b_jlist == BJ_Forget)));
841
842 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
843 J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
844
845 if (jh->b_transaction == NULL) {
846 jh->b_transaction = transaction;
Josef Bacik9fc7c632008-04-17 10:38:59 -0400847
848 /* first access by this transaction */
849 jh->b_modified = 0;
850
Dave Kleikamp470decc2006-10-11 01:20:57 -0700851 JBUFFER_TRACE(jh, "file as BJ_Reserved");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700852 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700853 } else if (jh->b_transaction == journal->j_committing_transaction) {
Josef Bacik9fc7c632008-04-17 10:38:59 -0400854 /* first access by this transaction */
855 jh->b_modified = 0;
856
Dave Kleikamp470decc2006-10-11 01:20:57 -0700857 JBUFFER_TRACE(jh, "set next transaction");
858 jh->b_next_transaction = transaction;
859 }
860 spin_unlock(&journal->j_list_lock);
861 jbd_unlock_bh_state(bh);
862
863 /*
864 * akpm: I added this. ext3_alloc_branch can pick up new indirect
865 * blocks which contain freed but then revoked metadata. We need
866 * to cancel the revoke in case we end up freeing it yet again
867 * and the reallocating as data - this would cause a second revoke,
868 * which hits an assertion error.
869 */
870 JBUFFER_TRACE(jh, "cancelling revoke");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700871 jbd2_journal_cancel_revoke(handle, jh);
872 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700873out:
874 return err;
875}
876
877/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700878 * int jbd2_journal_get_undo_access() - Notify intent to modify metadata with
Dave Kleikamp470decc2006-10-11 01:20:57 -0700879 * non-rewindable consequences
880 * @handle: transaction
881 * @bh: buffer to undo
882 * @credits: store the number of taken credits here (if not NULL)
883 *
884 * Sometimes there is a need to distinguish between metadata which has
885 * been committed to disk and that which has not. The ext3fs code uses
886 * this for freeing and allocating space, we have to make sure that we
887 * do not reuse freed space until the deallocation has been committed,
888 * since if we overwrote that space we would make the delete
889 * un-rewindable in case of a crash.
890 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700891 * To deal with that, jbd2_journal_get_undo_access requests write access to a
Dave Kleikamp470decc2006-10-11 01:20:57 -0700892 * buffer for parts of non-rewindable operations such as delete
893 * operations on the bitmaps. The journaling code must keep a copy of
894 * the buffer's contents prior to the undo_access call until such time
895 * as we know that the buffer has definitely been committed to disk.
896 *
897 * We never need to know which transaction the committed data is part
898 * of, buffers touched here are guaranteed to be dirtied later and so
899 * will be committed to a new transaction in due course, at which point
900 * we can discard the old committed data pointer.
901 *
902 * Returns error number or 0 on success.
903 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700904int jbd2_journal_get_undo_access(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700905{
906 int err;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700907 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700908 char *committed_data = NULL;
909
910 JBUFFER_TRACE(jh, "entry");
911
912 /*
913 * Do this first --- it can drop the journal lock, so we want to
914 * make sure that obtaining the committed_data is done
915 * atomically wrt. completion of any outstanding commits.
916 */
917 err = do_get_write_access(handle, jh, 1);
918 if (err)
919 goto out;
920
921repeat:
922 if (!jh->b_committed_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400923 committed_data = jbd2_alloc(jh2bh(jh)->b_size, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700924 if (!committed_data) {
925 printk(KERN_EMERG "%s: No memory for committed data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400926 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700927 err = -ENOMEM;
928 goto out;
929 }
930 }
931
932 jbd_lock_bh_state(bh);
933 if (!jh->b_committed_data) {
934 /* Copy out the current buffer contents into the
935 * preserved, committed copy. */
936 JBUFFER_TRACE(jh, "generate b_committed data");
937 if (!committed_data) {
938 jbd_unlock_bh_state(bh);
939 goto repeat;
940 }
941
942 jh->b_committed_data = committed_data;
943 committed_data = NULL;
944 memcpy(jh->b_committed_data, bh->b_data, bh->b_size);
945 }
946 jbd_unlock_bh_state(bh);
947out:
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700948 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700949 if (unlikely(committed_data))
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400950 jbd2_free(committed_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700951 return err;
952}
953
954/**
Joel Beckere06c8222008-09-11 15:35:47 -0700955 * void jbd2_journal_set_triggers() - Add triggers for commit writeout
956 * @bh: buffer to trigger on
957 * @type: struct jbd2_buffer_trigger_type containing the trigger(s).
958 *
959 * Set any triggers on this journal_head. This is always safe, because
960 * triggers for a committing buffer will be saved off, and triggers for
961 * a running transaction will match the buffer in that transaction.
962 *
963 * Call with NULL to clear the triggers.
964 */
965void jbd2_journal_set_triggers(struct buffer_head *bh,
966 struct jbd2_buffer_trigger_type *type)
967{
968 struct journal_head *jh = bh2jh(bh);
969
970 jh->b_triggers = type;
971}
972
973void jbd2_buffer_commit_trigger(struct journal_head *jh, void *mapped_data,
974 struct jbd2_buffer_trigger_type *triggers)
975{
976 struct buffer_head *bh = jh2bh(jh);
977
978 if (!triggers || !triggers->t_commit)
979 return;
980
981 triggers->t_commit(triggers, bh, mapped_data, bh->b_size);
982}
983
984void jbd2_buffer_abort_trigger(struct journal_head *jh,
985 struct jbd2_buffer_trigger_type *triggers)
986{
987 if (!triggers || !triggers->t_abort)
988 return;
989
990 triggers->t_abort(triggers, jh2bh(jh));
991}
992
993
994
995/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700996 * int jbd2_journal_dirty_metadata() - mark a buffer as containing dirty metadata
Dave Kleikamp470decc2006-10-11 01:20:57 -0700997 * @handle: transaction to add buffer to.
998 * @bh: buffer to mark
999 *
1000 * mark dirty metadata which needs to be journaled as part of the current
1001 * transaction.
1002 *
1003 * The buffer is placed on the transaction's metadata list and is marked
1004 * as belonging to the transaction.
1005 *
1006 * Returns error number or 0 on success.
1007 *
1008 * Special care needs to be taken if the buffer already belongs to the
1009 * current committing transaction (in which case we should have frozen
1010 * data present for that commit). In that case, we don't relink the
1011 * buffer: that only gets done when the old transaction finally
1012 * completes its commit.
1013 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001014int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001015{
1016 transaction_t *transaction = handle->h_transaction;
1017 journal_t *journal = transaction->t_journal;
1018 struct journal_head *jh = bh2jh(bh);
1019
1020 jbd_debug(5, "journal_head %p\n", jh);
1021 JBUFFER_TRACE(jh, "entry");
1022 if (is_handle_aborted(handle))
1023 goto out;
1024
1025 jbd_lock_bh_state(bh);
1026
1027 if (jh->b_modified == 0) {
1028 /*
1029 * This buffer's got modified and becoming part
1030 * of the transaction. This needs to be done
1031 * once a transaction -bzzz
1032 */
1033 jh->b_modified = 1;
1034 J_ASSERT_JH(jh, handle->h_buffer_credits > 0);
1035 handle->h_buffer_credits--;
1036 }
1037
1038 /*
1039 * fastpath, to avoid expensive locking. If this buffer is already
1040 * on the running transaction's metadata list there is nothing to do.
1041 * Nobody can take it off again because there is a handle open.
1042 * I _think_ we're OK here with SMP barriers - a mistaken decision will
1043 * result in this test being false, so we go in and take the locks.
1044 */
1045 if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) {
1046 JBUFFER_TRACE(jh, "fastpath");
1047 J_ASSERT_JH(jh, jh->b_transaction ==
1048 journal->j_running_transaction);
1049 goto out_unlock_bh;
1050 }
1051
1052 set_buffer_jbddirty(bh);
1053
1054 /*
1055 * Metadata already on the current transaction list doesn't
1056 * need to be filed. Metadata on another transaction's list must
1057 * be committing, and will be refiled once the commit completes:
1058 * leave it alone for now.
1059 */
1060 if (jh->b_transaction != transaction) {
1061 JBUFFER_TRACE(jh, "already on other transaction");
1062 J_ASSERT_JH(jh, jh->b_transaction ==
1063 journal->j_committing_transaction);
1064 J_ASSERT_JH(jh, jh->b_next_transaction == transaction);
1065 /* And this case is illegal: we can't reuse another
1066 * transaction's data buffer, ever. */
1067 goto out_unlock_bh;
1068 }
1069
1070 /* That test should have eliminated the following case: */
Mingming Cao40191912008-01-28 23:58:27 -05001071 J_ASSERT_JH(jh, jh->b_frozen_data == NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001072
1073 JBUFFER_TRACE(jh, "file as BJ_Metadata");
1074 spin_lock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001075 __jbd2_journal_file_buffer(jh, handle->h_transaction, BJ_Metadata);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001076 spin_unlock(&journal->j_list_lock);
1077out_unlock_bh:
1078 jbd_unlock_bh_state(bh);
1079out:
1080 JBUFFER_TRACE(jh, "exit");
1081 return 0;
1082}
1083
1084/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001085 * jbd2_journal_release_buffer: undo a get_write_access without any buffer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001086 * updates, if the update decided in the end that it didn't need access.
1087 *
1088 */
1089void
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001090jbd2_journal_release_buffer(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001091{
1092 BUFFER_TRACE(bh, "entry");
1093}
1094
1095/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001096 * void jbd2_journal_forget() - bforget() for potentially-journaled buffers.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001097 * @handle: transaction handle
1098 * @bh: bh to 'forget'
1099 *
1100 * We can only do the bforget if there are no commits pending against the
1101 * buffer. If the buffer is dirty in the current running transaction we
1102 * can safely unlink it.
1103 *
1104 * bh may not be a journalled buffer at all - it may be a non-JBD
1105 * buffer which came off the hashtable. Check for this.
1106 *
1107 * Decrements bh->b_count by one.
1108 *
1109 * Allow this call even if the handle has aborted --- it may be part of
1110 * the caller's cleanup after an abort.
1111 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001112int jbd2_journal_forget (handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001113{
1114 transaction_t *transaction = handle->h_transaction;
1115 journal_t *journal = transaction->t_journal;
1116 struct journal_head *jh;
1117 int drop_reserve = 0;
1118 int err = 0;
Josef Bacik1dfc3222008-04-17 10:38:59 -04001119 int was_modified = 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001120
1121 BUFFER_TRACE(bh, "entry");
1122
1123 jbd_lock_bh_state(bh);
1124 spin_lock(&journal->j_list_lock);
1125
1126 if (!buffer_jbd(bh))
1127 goto not_jbd;
1128 jh = bh2jh(bh);
1129
1130 /* Critical error: attempting to delete a bitmap buffer, maybe?
1131 * Don't do any jbd operations, and return an error. */
1132 if (!J_EXPECT_JH(jh, !jh->b_committed_data,
1133 "inconsistent data on disk")) {
1134 err = -EIO;
1135 goto not_jbd;
1136 }
1137
Josef Bacik1dfc3222008-04-17 10:38:59 -04001138 /* keep track of wether or not this transaction modified us */
1139 was_modified = jh->b_modified;
1140
Dave Kleikamp470decc2006-10-11 01:20:57 -07001141 /*
1142 * The buffer's going from the transaction, we must drop
1143 * all references -bzzz
1144 */
1145 jh->b_modified = 0;
1146
1147 if (jh->b_transaction == handle->h_transaction) {
1148 J_ASSERT_JH(jh, !jh->b_frozen_data);
1149
1150 /* If we are forgetting a buffer which is already part
1151 * of this transaction, then we can just drop it from
1152 * the transaction immediately. */
1153 clear_buffer_dirty(bh);
1154 clear_buffer_jbddirty(bh);
1155
1156 JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
1157
Josef Bacik1dfc3222008-04-17 10:38:59 -04001158 /*
1159 * we only want to drop a reference if this transaction
1160 * modified the buffer
1161 */
1162 if (was_modified)
1163 drop_reserve = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001164
1165 /*
1166 * We are no longer going to journal this buffer.
1167 * However, the commit of this transaction is still
1168 * important to the buffer: the delete that we are now
1169 * processing might obsolete an old log entry, so by
1170 * committing, we can satisfy the buffer's checkpoint.
1171 *
1172 * So, if we have a checkpoint on the buffer, we should
1173 * now refile the buffer on our BJ_Forget list so that
1174 * we know to remove the checkpoint after we commit.
1175 */
1176
1177 if (jh->b_cp_transaction) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001178 __jbd2_journal_temp_unlink_buffer(jh);
1179 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001180 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001181 __jbd2_journal_unfile_buffer(jh);
1182 jbd2_journal_remove_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001183 __brelse(bh);
1184 if (!buffer_jbd(bh)) {
1185 spin_unlock(&journal->j_list_lock);
1186 jbd_unlock_bh_state(bh);
1187 __bforget(bh);
1188 goto drop;
1189 }
1190 }
1191 } else if (jh->b_transaction) {
1192 J_ASSERT_JH(jh, (jh->b_transaction ==
1193 journal->j_committing_transaction));
1194 /* However, if the buffer is still owned by a prior
1195 * (committing) transaction, we can't drop it yet... */
1196 JBUFFER_TRACE(jh, "belongs to older transaction");
1197 /* ... but we CAN drop it from the new transaction if we
1198 * have also modified it since the original commit. */
1199
1200 if (jh->b_next_transaction) {
1201 J_ASSERT(jh->b_next_transaction == transaction);
1202 jh->b_next_transaction = NULL;
Josef Bacik1dfc3222008-04-17 10:38:59 -04001203
1204 /*
1205 * only drop a reference if this transaction modified
1206 * the buffer
1207 */
1208 if (was_modified)
1209 drop_reserve = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001210 }
1211 }
1212
1213not_jbd:
1214 spin_unlock(&journal->j_list_lock);
1215 jbd_unlock_bh_state(bh);
1216 __brelse(bh);
1217drop:
1218 if (drop_reserve) {
1219 /* no need to reserve log space for this block -bzzz */
1220 handle->h_buffer_credits++;
1221 }
1222 return err;
1223}
1224
1225/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001226 * int jbd2_journal_stop() - complete a transaction
Dave Kleikamp470decc2006-10-11 01:20:57 -07001227 * @handle: tranaction to complete.
1228 *
1229 * All done for a particular handle.
1230 *
1231 * There is not much action needed here. We just return any remaining
1232 * buffer credits to the transaction and remove the handle. The only
1233 * complication is that we need to start a commit operation if the
1234 * filesystem is marked for synchronous update.
1235 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001236 * jbd2_journal_stop itself will not usually return an error, but it may
Dave Kleikamp470decc2006-10-11 01:20:57 -07001237 * do so in unusual circumstances. In particular, expect it to
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001238 * return -EIO if a jbd2_journal_abort has been executed since the
Dave Kleikamp470decc2006-10-11 01:20:57 -07001239 * transaction began.
1240 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001241int jbd2_journal_stop(handle_t *handle)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001242{
1243 transaction_t *transaction = handle->h_transaction;
1244 journal_t *journal = transaction->t_journal;
Josef Bacike07f7182008-11-26 01:14:26 -05001245 int err;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001246 pid_t pid;
1247
Dave Kleikamp470decc2006-10-11 01:20:57 -07001248 J_ASSERT(journal_current_handle() == handle);
1249
1250 if (is_handle_aborted(handle))
1251 err = -EIO;
OGAWA Hirofumi3e2a5322006-10-19 23:29:11 -07001252 else {
1253 J_ASSERT(transaction->t_updates > 0);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001254 err = 0;
OGAWA Hirofumi3e2a5322006-10-19 23:29:11 -07001255 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001256
1257 if (--handle->h_ref > 0) {
1258 jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
1259 handle->h_ref);
1260 return err;
1261 }
1262
1263 jbd_debug(4, "Handle %p going down\n", handle);
1264
1265 /*
1266 * Implement synchronous transaction batching. If the handle
1267 * was synchronous, don't force a commit immediately. Let's
Josef Bacike07f7182008-11-26 01:14:26 -05001268 * yield and let another thread piggyback onto this
1269 * transaction. Keep doing that while new threads continue to
1270 * arrive. It doesn't cost much - we're about to run a commit
1271 * and sleep on IO anyway. Speeds up many-threaded, many-dir
1272 * operations by 30x or more...
Dave Kleikamp470decc2006-10-11 01:20:57 -07001273 *
Josef Bacike07f7182008-11-26 01:14:26 -05001274 * We try and optimize the sleep time against what the
1275 * underlying disk can do, instead of having a static sleep
1276 * time. This is useful for the case where our storage is so
1277 * fast that it is more optimal to go ahead and force a flush
1278 * and wait for the transaction to be committed than it is to
1279 * wait for an arbitrary amount of time for new writers to
1280 * join the transaction. We achieve this by measuring how
1281 * long it takes to commit a transaction, and compare it with
1282 * how long this transaction has been running, and if run time
1283 * < commit time then we sleep for the delta and commit. This
1284 * greatly helps super fast disks that would see slowdowns as
1285 * more threads started doing fsyncs.
1286 *
1287 * But don't do this if this process was the most recent one
1288 * to perform a synchronous write. We do this to detect the
1289 * case where a single process is doing a stream of sync
1290 * writes. No point in waiting for joiners in that case.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001291 */
1292 pid = current->pid;
1293 if (handle->h_sync && journal->j_last_sync_writer != pid) {
Josef Bacike07f7182008-11-26 01:14:26 -05001294 u64 commit_time, trans_time;
1295
Dave Kleikamp470decc2006-10-11 01:20:57 -07001296 journal->j_last_sync_writer = pid;
Josef Bacike07f7182008-11-26 01:14:26 -05001297
1298 spin_lock(&journal->j_state_lock);
1299 commit_time = journal->j_average_commit_time;
1300 spin_unlock(&journal->j_state_lock);
1301
1302 trans_time = ktime_to_ns(ktime_sub(ktime_get(),
1303 transaction->t_start_time));
1304
Theodore Ts'o30773842009-01-03 20:27:38 -05001305 commit_time = max_t(u64, commit_time,
1306 1000*journal->j_min_batch_time);
Josef Bacike07f7182008-11-26 01:14:26 -05001307 commit_time = min_t(u64, commit_time,
Theodore Ts'o30773842009-01-03 20:27:38 -05001308 1000*journal->j_max_batch_time);
Josef Bacike07f7182008-11-26 01:14:26 -05001309
1310 if (trans_time < commit_time) {
1311 ktime_t expires = ktime_add_ns(ktime_get(),
1312 commit_time);
1313 set_current_state(TASK_UNINTERRUPTIBLE);
1314 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1315 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001316 }
1317
Theodore Ts'o70585482009-03-25 23:35:46 -04001318 if (handle->h_sync)
1319 transaction->t_synchronous_commit = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001320 current->journal_info = NULL;
1321 spin_lock(&journal->j_state_lock);
1322 spin_lock(&transaction->t_handle_lock);
1323 transaction->t_outstanding_credits -= handle->h_buffer_credits;
1324 transaction->t_updates--;
1325 if (!transaction->t_updates) {
1326 wake_up(&journal->j_wait_updates);
1327 if (journal->j_barrier_count)
1328 wake_up(&journal->j_wait_transaction_locked);
1329 }
1330
1331 /*
1332 * If the handle is marked SYNC, we need to set another commit
1333 * going! We also want to force a commit if the current
1334 * transaction is occupying too much of the log, or if the
1335 * transaction is too old now.
1336 */
1337 if (handle->h_sync ||
1338 transaction->t_outstanding_credits >
1339 journal->j_max_transaction_buffers ||
1340 time_after_eq(jiffies, transaction->t_expires)) {
1341 /* Do this even for aborted journals: an abort still
1342 * completes the commit thread, it just doesn't write
1343 * anything to disk. */
1344 tid_t tid = transaction->t_tid;
1345
1346 spin_unlock(&transaction->t_handle_lock);
1347 jbd_debug(2, "transaction too old, requesting commit for "
1348 "handle %p\n", handle);
1349 /* This is non-blocking */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001350 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001351 spin_unlock(&journal->j_state_lock);
1352
1353 /*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001354 * Special case: JBD2_SYNC synchronous updates require us
Dave Kleikamp470decc2006-10-11 01:20:57 -07001355 * to wait for the commit to complete.
1356 */
1357 if (handle->h_sync && !(current->flags & PF_MEMALLOC))
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001358 err = jbd2_log_wait_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001359 } else {
1360 spin_unlock(&transaction->t_handle_lock);
1361 spin_unlock(&journal->j_state_lock);
1362 }
1363
Ingo Molnar3295f0e2008-08-11 10:30:30 +02001364 lock_map_release(&handle->h_lockdep_map);
Mingming Cao7b751062008-01-28 23:58:27 -05001365
Mingming Caoaf1e76d2007-10-16 18:38:25 -04001366 jbd2_free_handle(handle);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001367 return err;
1368}
1369
Randy Dunlap5648ba52008-04-17 10:38:59 -04001370/**
1371 * int jbd2_journal_force_commit() - force any uncommitted transactions
Dave Kleikamp470decc2006-10-11 01:20:57 -07001372 * @journal: journal to force
1373 *
1374 * For synchronous operations: force any uncommitted transactions
1375 * to disk. May seem kludgy, but it reuses all the handle batching
1376 * code in a very simple manner.
1377 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001378int jbd2_journal_force_commit(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001379{
1380 handle_t *handle;
1381 int ret;
1382
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001383 handle = jbd2_journal_start(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001384 if (IS_ERR(handle)) {
1385 ret = PTR_ERR(handle);
1386 } else {
1387 handle->h_sync = 1;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001388 ret = jbd2_journal_stop(handle);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001389 }
1390 return ret;
1391}
1392
1393/*
1394 *
1395 * List management code snippets: various functions for manipulating the
1396 * transaction buffer lists.
1397 *
1398 */
1399
1400/*
1401 * Append a buffer to a transaction list, given the transaction's list head
1402 * pointer.
1403 *
1404 * j_list_lock is held.
1405 *
1406 * jbd_lock_bh_state(jh2bh(jh)) is held.
1407 */
1408
1409static inline void
1410__blist_add_buffer(struct journal_head **list, struct journal_head *jh)
1411{
1412 if (!*list) {
1413 jh->b_tnext = jh->b_tprev = jh;
1414 *list = jh;
1415 } else {
1416 /* Insert at the tail of the list to preserve order */
1417 struct journal_head *first = *list, *last = first->b_tprev;
1418 jh->b_tprev = last;
1419 jh->b_tnext = first;
1420 last->b_tnext = first->b_tprev = jh;
1421 }
1422}
1423
1424/*
1425 * Remove a buffer from a transaction list, given the transaction's list
1426 * head pointer.
1427 *
1428 * Called with j_list_lock held, and the journal may not be locked.
1429 *
1430 * jbd_lock_bh_state(jh2bh(jh)) is held.
1431 */
1432
1433static inline void
1434__blist_del_buffer(struct journal_head **list, struct journal_head *jh)
1435{
1436 if (*list == jh) {
1437 *list = jh->b_tnext;
1438 if (*list == jh)
1439 *list = NULL;
1440 }
1441 jh->b_tprev->b_tnext = jh->b_tnext;
1442 jh->b_tnext->b_tprev = jh->b_tprev;
1443}
1444
1445/*
1446 * Remove a buffer from the appropriate transaction list.
1447 *
1448 * Note that this function can *change* the value of
Jan Kara87c89c22008-07-11 19:27:31 -04001449 * bh->b_transaction->t_buffers, t_forget, t_iobuf_list, t_shadow_list,
1450 * t_log_list or t_reserved_list. If the caller is holding onto a copy of one
1451 * of these pointers, it could go bad. Generally the caller needs to re-read
1452 * the pointer from the transaction_t.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001453 *
1454 * Called under j_list_lock. The journal may not be locked.
1455 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001456void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001457{
1458 struct journal_head **list = NULL;
1459 transaction_t *transaction;
1460 struct buffer_head *bh = jh2bh(jh);
1461
1462 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1463 transaction = jh->b_transaction;
1464 if (transaction)
1465 assert_spin_locked(&transaction->t_journal->j_list_lock);
1466
1467 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1468 if (jh->b_jlist != BJ_None)
Mingming Cao40191912008-01-28 23:58:27 -05001469 J_ASSERT_JH(jh, transaction != NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001470
1471 switch (jh->b_jlist) {
1472 case BJ_None:
1473 return;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001474 case BJ_Metadata:
1475 transaction->t_nr_buffers--;
1476 J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
1477 list = &transaction->t_buffers;
1478 break;
1479 case BJ_Forget:
1480 list = &transaction->t_forget;
1481 break;
1482 case BJ_IO:
1483 list = &transaction->t_iobuf_list;
1484 break;
1485 case BJ_Shadow:
1486 list = &transaction->t_shadow_list;
1487 break;
1488 case BJ_LogCtl:
1489 list = &transaction->t_log_list;
1490 break;
1491 case BJ_Reserved:
1492 list = &transaction->t_reserved_list;
1493 break;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001494 }
1495
1496 __blist_del_buffer(list, jh);
1497 jh->b_jlist = BJ_None;
1498 if (test_clear_buffer_jbddirty(bh))
1499 mark_buffer_dirty(bh); /* Expose it to the VM */
1500}
1501
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001502void __jbd2_journal_unfile_buffer(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001503{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001504 __jbd2_journal_temp_unlink_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001505 jh->b_transaction = NULL;
1506}
1507
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001508void jbd2_journal_unfile_buffer(journal_t *journal, struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001509{
1510 jbd_lock_bh_state(jh2bh(jh));
1511 spin_lock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001512 __jbd2_journal_unfile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001513 spin_unlock(&journal->j_list_lock);
1514 jbd_unlock_bh_state(jh2bh(jh));
1515}
1516
1517/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001518 * Called from jbd2_journal_try_to_free_buffers().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001519 *
1520 * Called under jbd_lock_bh_state(bh)
1521 */
1522static void
1523__journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh)
1524{
1525 struct journal_head *jh;
1526
1527 jh = bh2jh(bh);
1528
1529 if (buffer_locked(bh) || buffer_dirty(bh))
1530 goto out;
1531
Mingming Cao40191912008-01-28 23:58:27 -05001532 if (jh->b_next_transaction != NULL)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001533 goto out;
1534
1535 spin_lock(&journal->j_list_lock);
Jan Kara87c89c22008-07-11 19:27:31 -04001536 if (jh->b_cp_transaction != NULL && jh->b_transaction == NULL) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001537 /* written-back checkpointed metadata buffer */
1538 if (jh->b_jlist == BJ_None) {
1539 JBUFFER_TRACE(jh, "remove from checkpoint list");
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001540 __jbd2_journal_remove_checkpoint(jh);
1541 jbd2_journal_remove_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001542 __brelse(bh);
1543 }
1544 }
1545 spin_unlock(&journal->j_list_lock);
1546out:
1547 return;
1548}
1549
Mingming Cao530576b2008-07-13 21:06:39 -04001550/*
1551 * jbd2_journal_try_to_free_buffers() could race with
1552 * jbd2_journal_commit_transaction(). The later might still hold the
1553 * reference count to the buffers when inspecting them on
1554 * t_syncdata_list or t_locked_list.
1555 *
1556 * jbd2_journal_try_to_free_buffers() will call this function to
1557 * wait for the current transaction to finish syncing data buffers, before
1558 * try to free that buffer.
1559 *
1560 * Called with journal->j_state_lock hold.
1561 */
1562static void jbd2_journal_wait_for_transaction_sync_data(journal_t *journal)
1563{
1564 transaction_t *transaction;
1565 tid_t tid;
1566
1567 spin_lock(&journal->j_state_lock);
1568 transaction = journal->j_committing_transaction;
1569
1570 if (!transaction) {
1571 spin_unlock(&journal->j_state_lock);
1572 return;
1573 }
1574
1575 tid = transaction->t_tid;
1576 spin_unlock(&journal->j_state_lock);
1577 jbd2_log_wait_commit(journal, tid);
1578}
Dave Kleikamp470decc2006-10-11 01:20:57 -07001579
1580/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001581 * int jbd2_journal_try_to_free_buffers() - try to free page buffers.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001582 * @journal: journal for operation
1583 * @page: to try and free
Mingming Cao530576b2008-07-13 21:06:39 -04001584 * @gfp_mask: we use the mask to detect how hard should we try to release
1585 * buffers. If __GFP_WAIT and __GFP_FS is set, we wait for commit code to
1586 * release the buffers.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001587 *
1588 *
1589 * For all the buffers on this page,
1590 * if they are fully written out ordered data, move them onto BUF_CLEAN
1591 * so try_to_free_buffers() can reap them.
1592 *
1593 * This function returns non-zero if we wish try_to_free_buffers()
1594 * to be called. We do this if the page is releasable by try_to_free_buffers().
1595 * We also do it if the page has locked or dirty buffers and the caller wants
1596 * us to perform sync or async writeout.
1597 *
1598 * This complicates JBD locking somewhat. We aren't protected by the
1599 * BKL here. We wish to remove the buffer from its committing or
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001600 * running transaction's ->t_datalist via __jbd2_journal_unfile_buffer.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001601 *
1602 * This may *change* the value of transaction_t->t_datalist, so anyone
1603 * who looks at t_datalist needs to lock against this function.
1604 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001605 * Even worse, someone may be doing a jbd2_journal_dirty_data on this
1606 * buffer. So we need to lock against that. jbd2_journal_dirty_data()
Dave Kleikamp470decc2006-10-11 01:20:57 -07001607 * will come out of the lock with the buffer dirty, which makes it
1608 * ineligible for release here.
1609 *
1610 * Who else is affected by this? hmm... Really the only contender
1611 * is do_get_write_access() - it could be looking at the buffer while
1612 * journal_try_to_free_buffer() is changing its state. But that
1613 * cannot happen because we never reallocate freed data as metadata
1614 * while the data is part of a transaction. Yes?
Mingming Cao530576b2008-07-13 21:06:39 -04001615 *
1616 * Return 0 on failure, 1 on success
Dave Kleikamp470decc2006-10-11 01:20:57 -07001617 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001618int jbd2_journal_try_to_free_buffers(journal_t *journal,
Mingming Cao530576b2008-07-13 21:06:39 -04001619 struct page *page, gfp_t gfp_mask)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001620{
1621 struct buffer_head *head;
1622 struct buffer_head *bh;
1623 int ret = 0;
1624
1625 J_ASSERT(PageLocked(page));
1626
1627 head = page_buffers(page);
1628 bh = head;
1629 do {
1630 struct journal_head *jh;
1631
1632 /*
1633 * We take our own ref against the journal_head here to avoid
1634 * having to add tons of locking around each instance of
Mingming Cao530576b2008-07-13 21:06:39 -04001635 * jbd2_journal_remove_journal_head() and
1636 * jbd2_journal_put_journal_head().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001637 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001638 jh = jbd2_journal_grab_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001639 if (!jh)
1640 continue;
1641
1642 jbd_lock_bh_state(bh);
1643 __journal_try_to_free_buffer(journal, bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001644 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001645 jbd_unlock_bh_state(bh);
1646 if (buffer_jbd(bh))
1647 goto busy;
1648 } while ((bh = bh->b_this_page) != head);
Mingming Cao530576b2008-07-13 21:06:39 -04001649
Dave Kleikamp470decc2006-10-11 01:20:57 -07001650 ret = try_to_free_buffers(page);
Mingming Cao530576b2008-07-13 21:06:39 -04001651
1652 /*
1653 * There are a number of places where jbd2_journal_try_to_free_buffers()
1654 * could race with jbd2_journal_commit_transaction(), the later still
1655 * holds the reference to the buffers to free while processing them.
1656 * try_to_free_buffers() failed to free those buffers. Some of the
1657 * caller of releasepage() request page buffers to be dropped, otherwise
1658 * treat the fail-to-free as errors (such as generic_file_direct_IO())
1659 *
1660 * So, if the caller of try_to_release_page() wants the synchronous
1661 * behaviour(i.e make sure buffers are dropped upon return),
1662 * let's wait for the current transaction to finish flush of
1663 * dirty data buffers, then try to free those buffers again,
1664 * with the journal locked.
1665 */
1666 if (ret == 0 && (gfp_mask & __GFP_WAIT) && (gfp_mask & __GFP_FS)) {
1667 jbd2_journal_wait_for_transaction_sync_data(journal);
1668 ret = try_to_free_buffers(page);
1669 }
1670
Dave Kleikamp470decc2006-10-11 01:20:57 -07001671busy:
1672 return ret;
1673}
1674
1675/*
1676 * This buffer is no longer needed. If it is on an older transaction's
1677 * checkpoint list we need to record it on this transaction's forget list
1678 * to pin this buffer (and hence its checkpointing transaction) down until
1679 * this transaction commits. If the buffer isn't on a checkpoint list, we
1680 * release it.
1681 * Returns non-zero if JBD no longer has an interest in the buffer.
1682 *
1683 * Called under j_list_lock.
1684 *
1685 * Called under jbd_lock_bh_state(bh).
1686 */
1687static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
1688{
1689 int may_free = 1;
1690 struct buffer_head *bh = jh2bh(jh);
1691
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001692 __jbd2_journal_unfile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001693
1694 if (jh->b_cp_transaction) {
1695 JBUFFER_TRACE(jh, "on running+cp transaction");
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001696 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001697 clear_buffer_jbddirty(bh);
1698 may_free = 0;
1699 } else {
1700 JBUFFER_TRACE(jh, "on running transaction");
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001701 jbd2_journal_remove_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001702 __brelse(bh);
1703 }
1704 return may_free;
1705}
1706
1707/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001708 * jbd2_journal_invalidatepage
Dave Kleikamp470decc2006-10-11 01:20:57 -07001709 *
1710 * This code is tricky. It has a number of cases to deal with.
1711 *
1712 * There are two invariants which this code relies on:
1713 *
1714 * i_size must be updated on disk before we start calling invalidatepage on the
1715 * data.
1716 *
1717 * This is done in ext3 by defining an ext3_setattr method which
1718 * updates i_size before truncate gets going. By maintaining this
1719 * invariant, we can be sure that it is safe to throw away any buffers
1720 * attached to the current transaction: once the transaction commits,
1721 * we know that the data will not be needed.
1722 *
1723 * Note however that we can *not* throw away data belonging to the
1724 * previous, committing transaction!
1725 *
1726 * Any disk blocks which *are* part of the previous, committing
1727 * transaction (and which therefore cannot be discarded immediately) are
1728 * not going to be reused in the new running transaction
1729 *
1730 * The bitmap committed_data images guarantee this: any block which is
1731 * allocated in one transaction and removed in the next will be marked
1732 * as in-use in the committed_data bitmap, so cannot be reused until
1733 * the next transaction to delete the block commits. This means that
1734 * leaving committing buffers dirty is quite safe: the disk blocks
1735 * cannot be reallocated to a different file and so buffer aliasing is
1736 * not possible.
1737 *
1738 *
1739 * The above applies mainly to ordered data mode. In writeback mode we
1740 * don't make guarantees about the order in which data hits disk --- in
1741 * particular we don't guarantee that new dirty data is flushed before
1742 * transaction commit --- so it is always safe just to discard data
1743 * immediately in that mode. --sct
1744 */
1745
1746/*
1747 * The journal_unmap_buffer helper function returns zero if the buffer
1748 * concerned remains pinned as an anonymous buffer belonging to an older
1749 * transaction.
1750 *
1751 * We're outside-transaction here. Either or both of j_running_transaction
1752 * and j_committing_transaction may be NULL.
1753 */
1754static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh)
1755{
1756 transaction_t *transaction;
1757 struct journal_head *jh;
1758 int may_free = 1;
1759 int ret;
1760
1761 BUFFER_TRACE(bh, "entry");
1762
1763 /*
1764 * It is safe to proceed here without the j_list_lock because the
1765 * buffers cannot be stolen by try_to_free_buffers as long as we are
1766 * holding the page lock. --sct
1767 */
1768
1769 if (!buffer_jbd(bh))
1770 goto zap_buffer_unlocked;
1771
Jan Kara87c89c22008-07-11 19:27:31 -04001772 /* OK, we have data buffer in journaled mode */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001773 spin_lock(&journal->j_state_lock);
1774 jbd_lock_bh_state(bh);
1775 spin_lock(&journal->j_list_lock);
1776
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001777 jh = jbd2_journal_grab_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001778 if (!jh)
1779 goto zap_buffer_no_jh;
1780
1781 transaction = jh->b_transaction;
1782 if (transaction == NULL) {
1783 /* First case: not on any transaction. If it
1784 * has no checkpoint link, then we can zap it:
1785 * it's a writeback-mode buffer so we don't care
1786 * if it hits disk safely. */
1787 if (!jh->b_cp_transaction) {
1788 JBUFFER_TRACE(jh, "not on any transaction: zap");
1789 goto zap_buffer;
1790 }
1791
1792 if (!buffer_dirty(bh)) {
1793 /* bdflush has written it. We can drop it now */
1794 goto zap_buffer;
1795 }
1796
1797 /* OK, it must be in the journal but still not
1798 * written fully to disk: it's metadata or
1799 * journaled data... */
1800
1801 if (journal->j_running_transaction) {
1802 /* ... and once the current transaction has
1803 * committed, the buffer won't be needed any
1804 * longer. */
1805 JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
1806 ret = __dispose_buffer(jh,
1807 journal->j_running_transaction);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001808 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001809 spin_unlock(&journal->j_list_lock);
1810 jbd_unlock_bh_state(bh);
1811 spin_unlock(&journal->j_state_lock);
1812 return ret;
1813 } else {
1814 /* There is no currently-running transaction. So the
1815 * orphan record which we wrote for this file must have
1816 * passed into commit. We must attach this buffer to
1817 * the committing transaction, if it exists. */
1818 if (journal->j_committing_transaction) {
1819 JBUFFER_TRACE(jh, "give to committing trans");
1820 ret = __dispose_buffer(jh,
1821 journal->j_committing_transaction);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001822 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001823 spin_unlock(&journal->j_list_lock);
1824 jbd_unlock_bh_state(bh);
1825 spin_unlock(&journal->j_state_lock);
1826 return ret;
1827 } else {
1828 /* The orphan record's transaction has
1829 * committed. We can cleanse this buffer */
1830 clear_buffer_jbddirty(bh);
1831 goto zap_buffer;
1832 }
1833 }
1834 } else if (transaction == journal->j_committing_transaction) {
Eric Sandeen9b579882006-10-28 10:38:28 -07001835 JBUFFER_TRACE(jh, "on committing transaction");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001836 /*
1837 * If it is committing, we simply cannot touch it. We
1838 * can remove it's next_transaction pointer from the
1839 * running transaction if that is set, but nothing
1840 * else. */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001841 set_buffer_freed(bh);
1842 if (jh->b_next_transaction) {
1843 J_ASSERT(jh->b_next_transaction ==
1844 journal->j_running_transaction);
1845 jh->b_next_transaction = NULL;
1846 }
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001847 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001848 spin_unlock(&journal->j_list_lock);
1849 jbd_unlock_bh_state(bh);
1850 spin_unlock(&journal->j_state_lock);
1851 return 0;
1852 } else {
1853 /* Good, the buffer belongs to the running transaction.
1854 * We are writing our own transaction's data, not any
1855 * previous one's, so it is safe to throw it away
1856 * (remember that we expect the filesystem to have set
1857 * i_size already for this truncate so recovery will not
1858 * expose the disk blocks we are discarding here.) */
1859 J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
Eric Sandeen9b579882006-10-28 10:38:28 -07001860 JBUFFER_TRACE(jh, "on running transaction");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001861 may_free = __dispose_buffer(jh, transaction);
1862 }
1863
1864zap_buffer:
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001865 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001866zap_buffer_no_jh:
1867 spin_unlock(&journal->j_list_lock);
1868 jbd_unlock_bh_state(bh);
1869 spin_unlock(&journal->j_state_lock);
1870zap_buffer_unlocked:
1871 clear_buffer_dirty(bh);
1872 J_ASSERT_BH(bh, !buffer_jbddirty(bh));
1873 clear_buffer_mapped(bh);
1874 clear_buffer_req(bh);
1875 clear_buffer_new(bh);
1876 bh->b_bdev = NULL;
1877 return may_free;
1878}
1879
1880/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001881 * void jbd2_journal_invalidatepage()
Dave Kleikamp470decc2006-10-11 01:20:57 -07001882 * @journal: journal to use for flush...
1883 * @page: page to flush
1884 * @offset: length of page to invalidate.
1885 *
1886 * Reap page buffers containing data after offset in page.
1887 *
1888 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001889void jbd2_journal_invalidatepage(journal_t *journal,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001890 struct page *page,
1891 unsigned long offset)
1892{
1893 struct buffer_head *head, *bh, *next;
1894 unsigned int curr_off = 0;
1895 int may_free = 1;
1896
1897 if (!PageLocked(page))
1898 BUG();
1899 if (!page_has_buffers(page))
1900 return;
1901
1902 /* We will potentially be playing with lists other than just the
1903 * data lists (especially for journaled data mode), so be
1904 * cautious in our locking. */
1905
1906 head = bh = page_buffers(page);
1907 do {
1908 unsigned int next_off = curr_off + bh->b_size;
1909 next = bh->b_this_page;
1910
1911 if (offset <= curr_off) {
1912 /* This block is wholly outside the truncation point */
1913 lock_buffer(bh);
1914 may_free &= journal_unmap_buffer(journal, bh);
1915 unlock_buffer(bh);
1916 }
1917 curr_off = next_off;
1918 bh = next;
1919
1920 } while (bh != head);
1921
1922 if (!offset) {
1923 if (may_free && try_to_free_buffers(page))
1924 J_ASSERT(!page_has_buffers(page));
1925 }
1926}
1927
1928/*
1929 * File a buffer on the given transaction list.
1930 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001931void __jbd2_journal_file_buffer(struct journal_head *jh,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001932 transaction_t *transaction, int jlist)
1933{
1934 struct journal_head **list = NULL;
1935 int was_dirty = 0;
1936 struct buffer_head *bh = jh2bh(jh);
1937
1938 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1939 assert_spin_locked(&transaction->t_journal->j_list_lock);
1940
1941 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1942 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
Mingming Cao40191912008-01-28 23:58:27 -05001943 jh->b_transaction == NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001944
1945 if (jh->b_transaction && jh->b_jlist == jlist)
1946 return;
1947
1948 /* The following list of buffer states needs to be consistent
1949 * with __jbd_unexpected_dirty_buffer()'s handling of dirty
1950 * state. */
1951
1952 if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
1953 jlist == BJ_Shadow || jlist == BJ_Forget) {
1954 if (test_clear_buffer_dirty(bh) ||
1955 test_clear_buffer_jbddirty(bh))
1956 was_dirty = 1;
1957 }
1958
1959 if (jh->b_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001960 __jbd2_journal_temp_unlink_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001961 jh->b_transaction = transaction;
1962
1963 switch (jlist) {
1964 case BJ_None:
1965 J_ASSERT_JH(jh, !jh->b_committed_data);
1966 J_ASSERT_JH(jh, !jh->b_frozen_data);
1967 return;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001968 case BJ_Metadata:
1969 transaction->t_nr_buffers++;
1970 list = &transaction->t_buffers;
1971 break;
1972 case BJ_Forget:
1973 list = &transaction->t_forget;
1974 break;
1975 case BJ_IO:
1976 list = &transaction->t_iobuf_list;
1977 break;
1978 case BJ_Shadow:
1979 list = &transaction->t_shadow_list;
1980 break;
1981 case BJ_LogCtl:
1982 list = &transaction->t_log_list;
1983 break;
1984 case BJ_Reserved:
1985 list = &transaction->t_reserved_list;
1986 break;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001987 }
1988
1989 __blist_add_buffer(list, jh);
1990 jh->b_jlist = jlist;
1991
1992 if (was_dirty)
1993 set_buffer_jbddirty(bh);
1994}
1995
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001996void jbd2_journal_file_buffer(struct journal_head *jh,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001997 transaction_t *transaction, int jlist)
1998{
1999 jbd_lock_bh_state(jh2bh(jh));
2000 spin_lock(&transaction->t_journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002001 __jbd2_journal_file_buffer(jh, transaction, jlist);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002002 spin_unlock(&transaction->t_journal->j_list_lock);
2003 jbd_unlock_bh_state(jh2bh(jh));
2004}
2005
2006/*
2007 * Remove a buffer from its current buffer list in preparation for
2008 * dropping it from its current transaction entirely. If the buffer has
2009 * already started to be used by a subsequent transaction, refile the
2010 * buffer on that transaction's metadata list.
2011 *
2012 * Called under journal->j_list_lock
2013 *
2014 * Called under jbd_lock_bh_state(jh2bh(jh))
2015 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002016void __jbd2_journal_refile_buffer(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002017{
2018 int was_dirty;
2019 struct buffer_head *bh = jh2bh(jh);
2020
2021 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
2022 if (jh->b_transaction)
2023 assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
2024
2025 /* If the buffer is now unused, just drop it. */
2026 if (jh->b_next_transaction == NULL) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002027 __jbd2_journal_unfile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002028 return;
2029 }
2030
2031 /*
2032 * It has been modified by a later transaction: add it to the new
2033 * transaction's metadata list.
2034 */
2035
2036 was_dirty = test_clear_buffer_jbddirty(bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002037 __jbd2_journal_temp_unlink_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002038 jh->b_transaction = jh->b_next_transaction;
2039 jh->b_next_transaction = NULL;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002040 __jbd2_journal_file_buffer(jh, jh->b_transaction,
Josef Bacik1dfc3222008-04-17 10:38:59 -04002041 jh->b_modified ? BJ_Metadata : BJ_Reserved);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002042 J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
2043
2044 if (was_dirty)
2045 set_buffer_jbddirty(bh);
2046}
2047
2048/*
2049 * For the unlocked version of this call, also make sure that any
2050 * hanging journal_head is cleaned up if necessary.
2051 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002052 * __jbd2_journal_refile_buffer is usually called as part of a single locked
Dave Kleikamp470decc2006-10-11 01:20:57 -07002053 * operation on a buffer_head, in which the caller is probably going to
2054 * be hooking the journal_head onto other lists. In that case it is up
2055 * to the caller to remove the journal_head if necessary. For the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002056 * unlocked jbd2_journal_refile_buffer call, the caller isn't going to be
Dave Kleikamp470decc2006-10-11 01:20:57 -07002057 * doing anything else to the buffer so we need to do the cleanup
2058 * ourselves to avoid a jh leak.
2059 *
2060 * *** The journal_head may be freed by this call! ***
2061 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002062void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002063{
2064 struct buffer_head *bh = jh2bh(jh);
2065
2066 jbd_lock_bh_state(bh);
2067 spin_lock(&journal->j_list_lock);
2068
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002069 __jbd2_journal_refile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002070 jbd_unlock_bh_state(bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002071 jbd2_journal_remove_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002072
2073 spin_unlock(&journal->j_list_lock);
2074 __brelse(bh);
2075}
Jan Karac851ed52008-07-11 19:27:31 -04002076
2077/*
2078 * File inode in the inode list of the handle's transaction
2079 */
2080int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode)
2081{
2082 transaction_t *transaction = handle->h_transaction;
2083 journal_t *journal = transaction->t_journal;
2084
2085 if (is_handle_aborted(handle))
2086 return -EIO;
2087
2088 jbd_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino,
2089 transaction->t_tid);
2090
2091 /*
2092 * First check whether inode isn't already on the transaction's
2093 * lists without taking the lock. Note that this check is safe
2094 * without the lock as we cannot race with somebody removing inode
2095 * from the transaction. The reason is that we remove inode from the
2096 * transaction only in journal_release_jbd_inode() and when we commit
2097 * the transaction. We are guarded from the first case by holding
2098 * a reference to the inode. We are safe against the second case
2099 * because if jinode->i_transaction == transaction, commit code
2100 * cannot touch the transaction because we hold reference to it,
2101 * and if jinode->i_next_transaction == transaction, commit code
2102 * will only file the inode where we want it.
2103 */
2104 if (jinode->i_transaction == transaction ||
2105 jinode->i_next_transaction == transaction)
2106 return 0;
2107
2108 spin_lock(&journal->j_list_lock);
2109
2110 if (jinode->i_transaction == transaction ||
2111 jinode->i_next_transaction == transaction)
2112 goto done;
2113
2114 /* On some different transaction's list - should be
2115 * the committing one */
2116 if (jinode->i_transaction) {
2117 J_ASSERT(jinode->i_next_transaction == NULL);
2118 J_ASSERT(jinode->i_transaction ==
2119 journal->j_committing_transaction);
2120 jinode->i_next_transaction = transaction;
2121 goto done;
2122 }
2123 /* Not on any transaction list... */
2124 J_ASSERT(!jinode->i_next_transaction);
2125 jinode->i_transaction = transaction;
2126 list_add(&jinode->i_list, &transaction->t_inode_list);
2127done:
2128 spin_unlock(&journal->j_list_lock);
2129
2130 return 0;
2131}
2132
2133/*
Jan Kara7f5aa212009-02-10 11:15:34 -05002134 * File truncate and transaction commit interact with each other in a
2135 * non-trivial way. If a transaction writing data block A is
2136 * committing, we cannot discard the data by truncate until we have
2137 * written them. Otherwise if we crashed after the transaction with
2138 * write has committed but before the transaction with truncate has
2139 * committed, we could see stale data in block A. This function is a
2140 * helper to solve this problem. It starts writeout of the truncated
2141 * part in case it is in the committing transaction.
2142 *
2143 * Filesystem code must call this function when inode is journaled in
2144 * ordered mode before truncation happens and after the inode has been
2145 * placed on orphan list with the new inode size. The second condition
2146 * avoids the race that someone writes new data and we start
2147 * committing the transaction after this function has been called but
2148 * before a transaction for truncate is started (and furthermore it
2149 * allows us to optimize the case where the addition to orphan list
2150 * happens in the same transaction as write --- we don't have to write
2151 * any data in such case).
Jan Karac851ed52008-07-11 19:27:31 -04002152 */
Jan Kara7f5aa212009-02-10 11:15:34 -05002153int jbd2_journal_begin_ordered_truncate(journal_t *journal,
2154 struct jbd2_inode *jinode,
Jan Karac851ed52008-07-11 19:27:31 -04002155 loff_t new_size)
2156{
Jan Kara7f5aa212009-02-10 11:15:34 -05002157 transaction_t *inode_trans, *commit_trans;
Jan Karac851ed52008-07-11 19:27:31 -04002158 int ret = 0;
2159
Jan Kara7f5aa212009-02-10 11:15:34 -05002160 /* This is a quick check to avoid locking if not necessary */
2161 if (!jinode->i_transaction)
Jan Karac851ed52008-07-11 19:27:31 -04002162 goto out;
Jan Kara7f5aa212009-02-10 11:15:34 -05002163 /* Locks are here just to force reading of recent values, it is
2164 * enough that the transaction was not committing before we started
2165 * a transaction adding the inode to orphan list */
Jan Karac851ed52008-07-11 19:27:31 -04002166 spin_lock(&journal->j_state_lock);
2167 commit_trans = journal->j_committing_transaction;
2168 spin_unlock(&journal->j_state_lock);
Jan Kara7f5aa212009-02-10 11:15:34 -05002169 spin_lock(&journal->j_list_lock);
2170 inode_trans = jinode->i_transaction;
2171 spin_unlock(&journal->j_list_lock);
2172 if (inode_trans == commit_trans) {
2173 ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping,
Jan Karac851ed52008-07-11 19:27:31 -04002174 new_size, LLONG_MAX);
2175 if (ret)
2176 jbd2_journal_abort(journal, ret);
2177 }
2178out:
2179 return ret;
2180}