blob: 9c64c7ec48d4c468d649c9acd537ed78f01c59b0 [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>
Theodore Ts'o47def822010-07-27 11:56:05 -040029#include <linux/backing-dev.h>
30#include <linux/module.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070031
Adrian Bunk7ddae862006-12-06 20:38:27 -080032static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh);
33
Dave Kleikamp470decc2006-10-11 01:20:57 -070034/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -070035 * jbd2_get_transaction: obtain a new transaction_t object.
Dave Kleikamp470decc2006-10-11 01:20:57 -070036 *
37 * Simply allocate and initialise a new transaction. Create it in
38 * RUNNING state and add it to the current journal (which should not
39 * have an existing running transaction: we only make a new transaction
40 * once we have started to commit the old one).
41 *
42 * Preconditions:
43 * The journal MUST be locked. We don't perform atomic mallocs on the
44 * new transaction and we can't block without protecting against other
45 * processes trying to touch the journal while it is in transition.
46 *
Dave Kleikamp470decc2006-10-11 01:20:57 -070047 */
48
49static transaction_t *
Mingming Caof7f4bcc2006-10-11 01:20:59 -070050jbd2_get_transaction(journal_t *journal, transaction_t *transaction)
Dave Kleikamp470decc2006-10-11 01:20:57 -070051{
52 transaction->t_journal = journal;
53 transaction->t_state = T_RUNNING;
Josef Bacike07f7182008-11-26 01:14:26 -050054 transaction->t_start_time = ktime_get();
Dave Kleikamp470decc2006-10-11 01:20:57 -070055 transaction->t_tid = journal->j_transaction_sequence++;
56 transaction->t_expires = jiffies + journal->j_commit_interval;
57 spin_lock_init(&transaction->t_handle_lock);
Theodore Ts'oa51dca92010-08-02 08:43:25 -040058 atomic_set(&transaction->t_updates, 0);
59 atomic_set(&transaction->t_outstanding_credits, 0);
Jan Karac851ed52008-07-11 19:27:31 -040060 INIT_LIST_HEAD(&transaction->t_inode_list);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -040061 INIT_LIST_HEAD(&transaction->t_private_list);
Dave Kleikamp470decc2006-10-11 01:20:57 -070062
63 /* Set up the commit timer for the new transaction. */
Andreas Dilgerb1f485f2009-08-10 22:51:53 -040064 journal->j_commit_timer.expires = round_jiffies_up(transaction->t_expires);
Dave Kleikamp470decc2006-10-11 01:20:57 -070065 add_timer(&journal->j_commit_timer);
66
67 J_ASSERT(journal->j_running_transaction == NULL);
68 journal->j_running_transaction = transaction;
Johann Lombardi8e85fb32008-01-28 23:58:27 -050069 transaction->t_max_wait = 0;
70 transaction->t_start = jiffies;
Dave Kleikamp470decc2006-10-11 01:20:57 -070071
72 return transaction;
73}
74
75/*
76 * Handle management.
77 *
78 * A handle_t is an object which represents a single atomic update to a
79 * filesystem, and which tracks all of the modifications which form part
80 * of that one update.
81 */
82
83/*
84 * start_this_handle: Given a handle, deal with any locking or stalling
85 * needed to make sure that there is enough journal space for the handle
86 * to begin. Attach the handle to a transaction and set up the
87 * transaction's buffer credits.
88 */
89
Theodore Ts'o47def822010-07-27 11:56:05 -040090static int start_this_handle(journal_t *journal, handle_t *handle,
91 int gfp_mask)
Dave Kleikamp470decc2006-10-11 01:20:57 -070092{
93 transaction_t *transaction;
94 int needed;
95 int nblocks = handle->h_buffer_credits;
96 transaction_t *new_transaction = NULL;
Johann Lombardi8e85fb32008-01-28 23:58:27 -050097 unsigned long ts = jiffies;
Dave Kleikamp470decc2006-10-11 01:20:57 -070098
99 if (nblocks > journal->j_max_transaction_buffers) {
100 printk(KERN_ERR "JBD: %s wants too many credits (%d > %d)\n",
101 current->comm, nblocks,
102 journal->j_max_transaction_buffers);
Theodore Ts'o47def822010-07-27 11:56:05 -0400103 return -ENOSPC;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700104 }
105
106alloc_transaction:
107 if (!journal->j_running_transaction) {
Theodore Ts'o47def822010-07-27 11:56:05 -0400108 new_transaction = kzalloc(sizeof(*new_transaction), gfp_mask);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700109 if (!new_transaction) {
Theodore Ts'o47def822010-07-27 11:56:05 -0400110 /*
111 * If __GFP_FS is not present, then we may be
112 * being called from inside the fs writeback
113 * layer, so we MUST NOT fail. Since
114 * __GFP_NOFAIL is going away, we will arrange
115 * to retry the allocation ourselves.
116 */
117 if ((gfp_mask & __GFP_FS) == 0) {
118 congestion_wait(BLK_RW_ASYNC, HZ/50);
119 goto alloc_transaction;
120 }
121 return -ENOMEM;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700122 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700123 }
124
125 jbd_debug(3, "New handle %p going live.\n", handle);
126
127repeat:
128
129 /*
130 * We need to hold j_state_lock until t_updates has been incremented,
131 * for proper journal barrier handling
132 */
133 spin_lock(&journal->j_state_lock);
134repeat_locked:
135 if (is_journal_aborted(journal) ||
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700136 (journal->j_errno != 0 && !(journal->j_flags & JBD2_ACK_ERR))) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700137 spin_unlock(&journal->j_state_lock);
Theodore Ts'o47def822010-07-27 11:56:05 -0400138 kfree(new_transaction);
139 return -EROFS;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700140 }
141
142 /* Wait on the journal's transaction barrier if necessary */
143 if (journal->j_barrier_count) {
144 spin_unlock(&journal->j_state_lock);
145 wait_event(journal->j_wait_transaction_locked,
146 journal->j_barrier_count == 0);
147 goto repeat;
148 }
149
150 if (!journal->j_running_transaction) {
151 if (!new_transaction) {
152 spin_unlock(&journal->j_state_lock);
153 goto alloc_transaction;
154 }
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700155 jbd2_get_transaction(journal, new_transaction);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700156 new_transaction = NULL;
157 }
158
159 transaction = journal->j_running_transaction;
160
161 /*
162 * If the current transaction is locked down for commit, wait for the
163 * lock to be released.
164 */
165 if (transaction->t_state == T_LOCKED) {
166 DEFINE_WAIT(wait);
167
168 prepare_to_wait(&journal->j_wait_transaction_locked,
169 &wait, TASK_UNINTERRUPTIBLE);
170 spin_unlock(&journal->j_state_lock);
171 schedule();
172 finish_wait(&journal->j_wait_transaction_locked, &wait);
173 goto repeat;
174 }
175
176 /*
177 * If there is not enough space left in the log to write all potential
178 * buffers requested by this operation, we need to stall pending a log
179 * checkpoint to free some more log space.
180 */
181 spin_lock(&transaction->t_handle_lock);
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400182 needed = atomic_read(&transaction->t_outstanding_credits) + nblocks;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700183
184 if (needed > journal->j_max_transaction_buffers) {
185 /*
186 * If the current transaction is already too large, then start
187 * to commit it: we can then go back and attach this handle to
188 * a new transaction.
189 */
190 DEFINE_WAIT(wait);
191
192 jbd_debug(2, "Handle %p starting new commit...\n", handle);
193 spin_unlock(&transaction->t_handle_lock);
194 prepare_to_wait(&journal->j_wait_transaction_locked, &wait,
195 TASK_UNINTERRUPTIBLE);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700196 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700197 spin_unlock(&journal->j_state_lock);
198 schedule();
199 finish_wait(&journal->j_wait_transaction_locked, &wait);
200 goto repeat;
201 }
202
203 /*
204 * The commit code assumes that it can get enough log space
205 * without forcing a checkpoint. This is *critical* for
206 * correctness: a checkpoint of a buffer which is also
207 * associated with a committing transaction creates a deadlock,
208 * so commit simply cannot force through checkpoints.
209 *
210 * We must therefore ensure the necessary space in the journal
211 * *before* starting to dirty potentially checkpointed buffers
212 * in the new transaction.
213 *
214 * The worst part is, any transaction currently committing can
215 * reduce the free space arbitrarily. Be careful to account for
216 * those buffers when checkpointing.
217 */
218
219 /*
220 * @@@ AKPM: This seems rather over-defensive. We're giving commit
221 * a _lot_ of headroom: 1/4 of the journal plus the size of
222 * the committing transaction. Really, we only need to give it
223 * committing_transaction->t_outstanding_credits plus "enough" for
224 * the log control blocks.
225 * Also, this test is inconsitent with the matching one in
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700226 * jbd2_journal_extend().
Dave Kleikamp470decc2006-10-11 01:20:57 -0700227 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700228 if (__jbd2_log_space_left(journal) < jbd_space_needed(journal)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700229 jbd_debug(2, "Handle %p waiting for checkpoint...\n", handle);
230 spin_unlock(&transaction->t_handle_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700231 __jbd2_log_wait_for_space(journal);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700232 goto repeat_locked;
233 }
234
235 /* OK, account for the buffers that this operation expects to
236 * use and add the handle to the running transaction. */
237
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500238 if (time_after(transaction->t_start, ts)) {
239 ts = jbd2_time_diff(ts, transaction->t_start);
240 if (ts > transaction->t_max_wait)
241 transaction->t_max_wait = ts;
242 }
243
Dave Kleikamp470decc2006-10-11 01:20:57 -0700244 handle->h_transaction = transaction;
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400245 atomic_add(nblocks, &transaction->t_outstanding_credits);
246 atomic_inc(&transaction->t_updates);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700247 transaction->t_handle_count++;
248 jbd_debug(4, "Handle %p given %d credits (total %d, free %d)\n",
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400249 handle, nblocks,
250 atomic_read(&transaction->t_outstanding_credits),
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700251 __jbd2_log_space_left(journal));
Dave Kleikamp470decc2006-10-11 01:20:57 -0700252 spin_unlock(&transaction->t_handle_lock);
253 spin_unlock(&journal->j_state_lock);
Jan Kara9599b0e2009-08-17 21:23:17 -0400254
255 lock_map_acquire(&handle->h_lockdep_map);
Theodore Ts'o47def822010-07-27 11:56:05 -0400256 kfree(new_transaction);
257 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700258}
259
Mingming Cao7b751062008-01-28 23:58:27 -0500260static struct lock_class_key jbd2_handle_key;
261
Dave Kleikamp470decc2006-10-11 01:20:57 -0700262/* Allocate a new handle. This should probably be in a slab... */
263static handle_t *new_handle(int nblocks)
264{
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400265 handle_t *handle = jbd2_alloc_handle(GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700266 if (!handle)
267 return NULL;
268 memset(handle, 0, sizeof(*handle));
269 handle->h_buffer_credits = nblocks;
270 handle->h_ref = 1;
271
Mingming Cao7b751062008-01-28 23:58:27 -0500272 lockdep_init_map(&handle->h_lockdep_map, "jbd2_handle",
273 &jbd2_handle_key, 0);
274
Dave Kleikamp470decc2006-10-11 01:20:57 -0700275 return handle;
276}
277
278/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700279 * handle_t *jbd2_journal_start() - Obtain a new handle.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700280 * @journal: Journal to start transaction on.
281 * @nblocks: number of block buffer we might modify
282 *
283 * We make sure that the transaction can guarantee at least nblocks of
284 * modified buffers in the log. We block until the log can guarantee
285 * that much space.
286 *
287 * This function is visible to journal users (like ext3fs), so is not
288 * called with the journal already locked.
289 *
290 * Return a pointer to a newly allocated handle, or NULL on failure
291 */
Theodore Ts'o47def822010-07-27 11:56:05 -0400292handle_t *jbd2__journal_start(journal_t *journal, int nblocks, int gfp_mask)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700293{
294 handle_t *handle = journal_current_handle();
295 int err;
296
297 if (!journal)
298 return ERR_PTR(-EROFS);
299
300 if (handle) {
301 J_ASSERT(handle->h_transaction->t_journal == journal);
302 handle->h_ref++;
303 return handle;
304 }
305
306 handle = new_handle(nblocks);
307 if (!handle)
308 return ERR_PTR(-ENOMEM);
309
310 current->journal_info = handle;
311
Theodore Ts'o47def822010-07-27 11:56:05 -0400312 err = start_this_handle(journal, handle, gfp_mask);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700313 if (err < 0) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400314 jbd2_free_handle(handle);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700315 current->journal_info = NULL;
316 handle = ERR_PTR(err);
Mingming Cao7b751062008-01-28 23:58:27 -0500317 goto out;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700318 }
Mingming Cao7b751062008-01-28 23:58:27 -0500319out:
Dave Kleikamp470decc2006-10-11 01:20:57 -0700320 return handle;
321}
Theodore Ts'o47def822010-07-27 11:56:05 -0400322EXPORT_SYMBOL(jbd2__journal_start);
323
324
325handle_t *jbd2_journal_start(journal_t *journal, int nblocks)
326{
327 return jbd2__journal_start(journal, nblocks, GFP_NOFS);
328}
329EXPORT_SYMBOL(jbd2_journal_start);
330
Dave Kleikamp470decc2006-10-11 01:20:57 -0700331
332/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700333 * int jbd2_journal_extend() - extend buffer credits.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700334 * @handle: handle to 'extend'
335 * @nblocks: nr blocks to try to extend by.
336 *
337 * Some transactions, such as large extends and truncates, can be done
338 * atomically all at once or in several stages. The operation requests
339 * a credit for a number of buffer modications in advance, but can
340 * extend its credit if it needs more.
341 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700342 * jbd2_journal_extend tries to give the running handle more buffer credits.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700343 * It does not guarantee that allocation - this is a best-effort only.
344 * The calling process MUST be able to deal cleanly with a failure to
345 * extend here.
346 *
347 * Return 0 on success, non-zero on failure.
348 *
349 * return code < 0 implies an error
350 * return code > 0 implies normal transaction-full status.
351 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700352int jbd2_journal_extend(handle_t *handle, int nblocks)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700353{
354 transaction_t *transaction = handle->h_transaction;
355 journal_t *journal = transaction->t_journal;
356 int result;
357 int wanted;
358
359 result = -EIO;
360 if (is_handle_aborted(handle))
361 goto out;
362
363 result = 1;
364
365 spin_lock(&journal->j_state_lock);
366
367 /* Don't extend a locked-down transaction! */
368 if (handle->h_transaction->t_state != T_RUNNING) {
369 jbd_debug(3, "denied handle %p %d blocks: "
370 "transaction not running\n", handle, nblocks);
371 goto error_out;
372 }
373
374 spin_lock(&transaction->t_handle_lock);
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400375 wanted = atomic_read(&transaction->t_outstanding_credits) + nblocks;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700376
377 if (wanted > journal->j_max_transaction_buffers) {
378 jbd_debug(3, "denied handle %p %d blocks: "
379 "transaction too large\n", handle, nblocks);
380 goto unlock;
381 }
382
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700383 if (wanted > __jbd2_log_space_left(journal)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700384 jbd_debug(3, "denied handle %p %d blocks: "
385 "insufficient log space\n", handle, nblocks);
386 goto unlock;
387 }
388
389 handle->h_buffer_credits += nblocks;
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400390 atomic_add(nblocks, &transaction->t_outstanding_credits);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700391 result = 0;
392
393 jbd_debug(3, "extended handle %p by %d\n", handle, nblocks);
394unlock:
395 spin_unlock(&transaction->t_handle_lock);
396error_out:
397 spin_unlock(&journal->j_state_lock);
398out:
399 return result;
400}
401
402
403/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700404 * int jbd2_journal_restart() - restart a handle .
Dave Kleikamp470decc2006-10-11 01:20:57 -0700405 * @handle: handle to restart
406 * @nblocks: nr credits requested
407 *
408 * Restart a handle for a multi-transaction filesystem
409 * operation.
410 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700411 * If the jbd2_journal_extend() call above fails to grant new buffer credits
412 * to a running handle, a call to jbd2_journal_restart will commit the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700413 * handle's transaction so far and reattach the handle to a new
414 * transaction capabable of guaranteeing the requested number of
415 * credits.
416 */
Theodore Ts'o47def822010-07-27 11:56:05 -0400417int jbd2__journal_restart(handle_t *handle, int nblocks, int gfp_mask)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700418{
419 transaction_t *transaction = handle->h_transaction;
420 journal_t *journal = transaction->t_journal;
421 int ret;
422
423 /* If we've had an abort of any type, don't even think about
424 * actually doing the restart! */
425 if (is_handle_aborted(handle))
426 return 0;
427
428 /*
429 * First unlink the handle from its current transaction, and start the
430 * commit on that.
431 */
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400432 J_ASSERT(atomic_read(&transaction->t_updates) > 0);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700433 J_ASSERT(journal_current_handle() == handle);
434
435 spin_lock(&journal->j_state_lock);
436 spin_lock(&transaction->t_handle_lock);
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400437 atomic_sub(handle->h_buffer_credits,
438 &transaction->t_outstanding_credits);
439 if (atomic_dec_and_test(&transaction->t_updates))
Dave Kleikamp470decc2006-10-11 01:20:57 -0700440 wake_up(&journal->j_wait_updates);
441 spin_unlock(&transaction->t_handle_lock);
442
443 jbd_debug(2, "restarting handle %p\n", handle);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700444 __jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700445 spin_unlock(&journal->j_state_lock);
446
Jan Kara9599b0e2009-08-17 21:23:17 -0400447 lock_map_release(&handle->h_lockdep_map);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700448 handle->h_buffer_credits = nblocks;
Theodore Ts'o47def822010-07-27 11:56:05 -0400449 ret = start_this_handle(journal, handle, gfp_mask);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700450 return ret;
451}
Theodore Ts'o47def822010-07-27 11:56:05 -0400452EXPORT_SYMBOL(jbd2__journal_restart);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700453
454
Theodore Ts'o47def822010-07-27 11:56:05 -0400455int jbd2_journal_restart(handle_t *handle, int nblocks)
456{
457 return jbd2__journal_restart(handle, nblocks, GFP_NOFS);
458}
459EXPORT_SYMBOL(jbd2_journal_restart);
460
Dave Kleikamp470decc2006-10-11 01:20:57 -0700461/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700462 * void jbd2_journal_lock_updates () - establish a transaction barrier.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700463 * @journal: Journal to establish a barrier on.
464 *
465 * This locks out any further updates from being started, and blocks
466 * until all existing updates have completed, returning only once the
467 * journal is in a quiescent state with no updates running.
468 *
469 * The journal lock should not be held on entry.
470 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700471void jbd2_journal_lock_updates(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700472{
473 DEFINE_WAIT(wait);
474
475 spin_lock(&journal->j_state_lock);
476 ++journal->j_barrier_count;
477
478 /* Wait until there are no running updates */
479 while (1) {
480 transaction_t *transaction = journal->j_running_transaction;
481
482 if (!transaction)
483 break;
484
485 spin_lock(&transaction->t_handle_lock);
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400486 if (!atomic_read(&transaction->t_updates)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700487 spin_unlock(&transaction->t_handle_lock);
488 break;
489 }
490 prepare_to_wait(&journal->j_wait_updates, &wait,
491 TASK_UNINTERRUPTIBLE);
492 spin_unlock(&transaction->t_handle_lock);
493 spin_unlock(&journal->j_state_lock);
494 schedule();
495 finish_wait(&journal->j_wait_updates, &wait);
496 spin_lock(&journal->j_state_lock);
497 }
498 spin_unlock(&journal->j_state_lock);
499
500 /*
501 * We have now established a barrier against other normal updates, but
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700502 * we also need to barrier against other jbd2_journal_lock_updates() calls
Dave Kleikamp470decc2006-10-11 01:20:57 -0700503 * to make sure that we serialise special journal-locked operations
504 * too.
505 */
506 mutex_lock(&journal->j_barrier);
507}
508
509/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700510 * void jbd2_journal_unlock_updates (journal_t* journal) - release barrier
Dave Kleikamp470decc2006-10-11 01:20:57 -0700511 * @journal: Journal to release the barrier on.
512 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700513 * Release a transaction barrier obtained with jbd2_journal_lock_updates().
Dave Kleikamp470decc2006-10-11 01:20:57 -0700514 *
515 * Should be called without the journal lock held.
516 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700517void jbd2_journal_unlock_updates (journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700518{
519 J_ASSERT(journal->j_barrier_count != 0);
520
521 mutex_unlock(&journal->j_barrier);
522 spin_lock(&journal->j_state_lock);
523 --journal->j_barrier_count;
524 spin_unlock(&journal->j_state_lock);
525 wake_up(&journal->j_wait_transaction_locked);
526}
527
Jan Karaf91d1d02009-07-13 16:16:20 -0400528static void warn_dirty_buffer(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700529{
Jan Karaf91d1d02009-07-13 16:16:20 -0400530 char b[BDEVNAME_SIZE];
Dave Kleikamp470decc2006-10-11 01:20:57 -0700531
Jan Karaf91d1d02009-07-13 16:16:20 -0400532 printk(KERN_WARNING
533 "JBD: Spotted dirty metadata buffer (dev = %s, blocknr = %llu). "
534 "There's a risk of filesystem corruption in case of system "
535 "crash.\n",
536 bdevname(bh->b_bdev, b), (unsigned long long)bh->b_blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700537}
538
539/*
540 * If the buffer is already part of the current transaction, then there
541 * is nothing we need to do. If it is already part of a prior
542 * transaction which we are still committing to disk, then we need to
543 * make sure that we do not overwrite the old copy: we do copy-out to
544 * preserve the copy going to disk. We also account the buffer against
545 * the handle's metadata buffer credits (unless the buffer is already
546 * part of the transaction, that is).
547 *
548 */
549static int
550do_get_write_access(handle_t *handle, struct journal_head *jh,
551 int force_copy)
552{
553 struct buffer_head *bh;
554 transaction_t *transaction;
555 journal_t *journal;
556 int error;
557 char *frozen_buffer = NULL;
558 int need_copy = 0;
559
560 if (is_handle_aborted(handle))
561 return -EROFS;
562
563 transaction = handle->h_transaction;
564 journal = transaction->t_journal;
565
566 jbd_debug(5, "buffer_head %p, force_copy %d\n", jh, force_copy);
567
568 JBUFFER_TRACE(jh, "entry");
569repeat:
570 bh = jh2bh(jh);
571
572 /* @@@ Need to check for errors here at some point. */
573
574 lock_buffer(bh);
575 jbd_lock_bh_state(bh);
576
577 /* We now hold the buffer lock so it is safe to query the buffer
578 * state. Is the buffer dirty?
579 *
580 * If so, there are two possibilities. The buffer may be
581 * non-journaled, and undergoing a quite legitimate writeback.
582 * Otherwise, it is journaled, and we don't expect dirty buffers
583 * in that state (the buffers should be marked JBD_Dirty
584 * instead.) So either the IO is being done under our own
585 * control and this is a bug, or it's a third party IO such as
586 * dump(8) (which may leave the buffer scheduled for read ---
587 * ie. locked but not dirty) or tune2fs (which may actually have
588 * the buffer dirtied, ugh.) */
589
590 if (buffer_dirty(bh)) {
591 /*
592 * First question: is this buffer already part of the current
593 * transaction or the existing committing transaction?
594 */
595 if (jh->b_transaction) {
596 J_ASSERT_JH(jh,
597 jh->b_transaction == transaction ||
598 jh->b_transaction ==
599 journal->j_committing_transaction);
600 if (jh->b_next_transaction)
601 J_ASSERT_JH(jh, jh->b_next_transaction ==
602 transaction);
Jan Karaf91d1d02009-07-13 16:16:20 -0400603 warn_dirty_buffer(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700604 }
605 /*
606 * In any case we need to clean the dirty flag and we must
607 * do it under the buffer lock to be sure we don't race
608 * with running write-out.
609 */
Jan Karaf91d1d02009-07-13 16:16:20 -0400610 JBUFFER_TRACE(jh, "Journalling dirty buffer");
611 clear_buffer_dirty(bh);
612 set_buffer_jbddirty(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700613 }
614
615 unlock_buffer(bh);
616
617 error = -EROFS;
618 if (is_handle_aborted(handle)) {
619 jbd_unlock_bh_state(bh);
620 goto out;
621 }
622 error = 0;
623
624 /*
625 * The buffer is already part of this transaction if b_transaction or
626 * b_next_transaction points to it
627 */
628 if (jh->b_transaction == transaction ||
629 jh->b_next_transaction == transaction)
630 goto done;
631
632 /*
Josef Bacik9fc7c632008-04-17 10:38:59 -0400633 * this is the first time this transaction is touching this buffer,
634 * reset the modified flag
635 */
636 jh->b_modified = 0;
637
638 /*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700639 * If there is already a copy-out version of this buffer, then we don't
640 * need to make another one
641 */
642 if (jh->b_frozen_data) {
643 JBUFFER_TRACE(jh, "has frozen data");
644 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
645 jh->b_next_transaction = transaction;
646 goto done;
647 }
648
649 /* Is there data here we need to preserve? */
650
651 if (jh->b_transaction && jh->b_transaction != transaction) {
652 JBUFFER_TRACE(jh, "owned by older transaction");
653 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
654 J_ASSERT_JH(jh, jh->b_transaction ==
655 journal->j_committing_transaction);
656
657 /* There is one case we have to be very careful about.
658 * If the committing transaction is currently writing
659 * this buffer out to disk and has NOT made a copy-out,
660 * then we cannot modify the buffer contents at all
661 * right now. The essence of copy-out is that it is the
662 * extra copy, not the primary copy, which gets
663 * journaled. If the primary copy is already going to
664 * disk then we cannot do copy-out here. */
665
666 if (jh->b_jlist == BJ_Shadow) {
667 DEFINE_WAIT_BIT(wait, &bh->b_state, BH_Unshadow);
668 wait_queue_head_t *wqh;
669
670 wqh = bit_waitqueue(&bh->b_state, BH_Unshadow);
671
672 JBUFFER_TRACE(jh, "on shadow: sleep");
673 jbd_unlock_bh_state(bh);
674 /* commit wakes up all shadow buffers after IO */
675 for ( ; ; ) {
676 prepare_to_wait(wqh, &wait.wait,
677 TASK_UNINTERRUPTIBLE);
678 if (jh->b_jlist != BJ_Shadow)
679 break;
680 schedule();
681 }
682 finish_wait(wqh, &wait.wait);
683 goto repeat;
684 }
685
686 /* Only do the copy if the currently-owning transaction
687 * still needs it. If it is on the Forget list, the
688 * committing transaction is past that stage. The
689 * buffer had better remain locked during the kmalloc,
690 * but that should be true --- we hold the journal lock
691 * still and the buffer is already on the BUF_JOURNAL
692 * list so won't be flushed.
693 *
694 * Subtle point, though: if this is a get_undo_access,
695 * then we will be relying on the frozen_data to contain
696 * the new value of the committed_data record after the
697 * transaction, so we HAVE to force the frozen_data copy
698 * in that case. */
699
700 if (jh->b_jlist != BJ_Forget || force_copy) {
701 JBUFFER_TRACE(jh, "generate frozen data");
702 if (!frozen_buffer) {
703 JBUFFER_TRACE(jh, "allocate memory for buffer");
704 jbd_unlock_bh_state(bh);
705 frozen_buffer =
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400706 jbd2_alloc(jh2bh(jh)->b_size,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700707 GFP_NOFS);
708 if (!frozen_buffer) {
709 printk(KERN_EMERG
710 "%s: OOM for frozen_buffer\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400711 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700712 JBUFFER_TRACE(jh, "oom!");
713 error = -ENOMEM;
714 jbd_lock_bh_state(bh);
715 goto done;
716 }
717 goto repeat;
718 }
719 jh->b_frozen_data = frozen_buffer;
720 frozen_buffer = NULL;
721 need_copy = 1;
722 }
723 jh->b_next_transaction = transaction;
724 }
725
726
727 /*
728 * Finally, if the buffer is not journaled right now, we need to make
729 * sure it doesn't get written to disk before the caller actually
730 * commits the new data
731 */
732 if (!jh->b_transaction) {
733 JBUFFER_TRACE(jh, "no transaction");
734 J_ASSERT_JH(jh, !jh->b_next_transaction);
735 jh->b_transaction = transaction;
736 JBUFFER_TRACE(jh, "file as BJ_Reserved");
737 spin_lock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700738 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700739 spin_unlock(&journal->j_list_lock);
740 }
741
742done:
743 if (need_copy) {
744 struct page *page;
745 int offset;
746 char *source;
747
748 J_EXPECT_JH(jh, buffer_uptodate(jh2bh(jh)),
749 "Possible IO failure.\n");
750 page = jh2bh(jh)->b_page;
751 offset = ((unsigned long) jh2bh(jh)->b_data) & ~PAGE_MASK;
752 source = kmap_atomic(page, KM_USER0);
753 memcpy(jh->b_frozen_data, source+offset, jh2bh(jh)->b_size);
754 kunmap_atomic(source, KM_USER0);
Joel Beckere06c8222008-09-11 15:35:47 -0700755
756 /*
757 * Now that the frozen data is saved off, we need to store
758 * any matching triggers.
759 */
760 jh->b_frozen_triggers = jh->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700761 }
762 jbd_unlock_bh_state(bh);
763
764 /*
765 * If we are about to journal a buffer, then any revoke pending on it is
766 * no longer valid
767 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700768 jbd2_journal_cancel_revoke(handle, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700769
770out:
771 if (unlikely(frozen_buffer)) /* It's usually NULL */
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400772 jbd2_free(frozen_buffer, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700773
774 JBUFFER_TRACE(jh, "exit");
775 return error;
776}
777
778/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700779 * int jbd2_journal_get_write_access() - notify intent to modify a buffer for metadata (not data) update.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700780 * @handle: transaction to add buffer modifications to
781 * @bh: bh to be used for metadata writes
782 * @credits: variable that will receive credits for the buffer
783 *
784 * Returns an error code or 0 on success.
785 *
786 * In full data journalling mode the buffer may be of type BJ_AsyncData,
787 * because we're write()ing a buffer which is also part of a shared mapping.
788 */
789
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700790int jbd2_journal_get_write_access(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700791{
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700792 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700793 int rc;
794
795 /* We do not want to get caught playing with fields which the
796 * log thread also manipulates. Make sure that the buffer
797 * completes any outstanding IO before proceeding. */
798 rc = do_get_write_access(handle, jh, 0);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700799 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700800 return rc;
801}
802
803
804/*
805 * When the user wants to journal a newly created buffer_head
806 * (ie. getblk() returned a new buffer and we are going to populate it
807 * manually rather than reading off disk), then we need to keep the
808 * buffer_head locked until it has been completely filled with new
809 * data. In this case, we should be able to make the assertion that
810 * the bh is not already part of an existing transaction.
811 *
812 * The buffer should already be locked by the caller by this point.
813 * There is no lock ranking violation: it was a newly created,
814 * unlocked buffer beforehand. */
815
816/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700817 * int jbd2_journal_get_create_access () - notify intent to use newly created bh
Dave Kleikamp470decc2006-10-11 01:20:57 -0700818 * @handle: transaction to new buffer to
819 * @bh: new buffer.
820 *
821 * Call this if you create a new bh.
822 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700823int jbd2_journal_get_create_access(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700824{
825 transaction_t *transaction = handle->h_transaction;
826 journal_t *journal = transaction->t_journal;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700827 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700828 int err;
829
830 jbd_debug(5, "journal_head %p\n", jh);
831 err = -EROFS;
832 if (is_handle_aborted(handle))
833 goto out;
834 err = 0;
835
836 JBUFFER_TRACE(jh, "entry");
837 /*
838 * The buffer may already belong to this transaction due to pre-zeroing
839 * in the filesystem's new_block code. It may also be on the previous,
840 * committing transaction's lists, but it HAS to be in Forget state in
841 * that case: the transaction must have deleted the buffer for it to be
842 * reused here.
843 */
844 jbd_lock_bh_state(bh);
845 spin_lock(&journal->j_list_lock);
846 J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
847 jh->b_transaction == NULL ||
848 (jh->b_transaction == journal->j_committing_transaction &&
849 jh->b_jlist == BJ_Forget)));
850
851 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
852 J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
853
854 if (jh->b_transaction == NULL) {
Jan Karaf91d1d02009-07-13 16:16:20 -0400855 /*
856 * Previous jbd2_journal_forget() could have left the buffer
857 * with jbddirty bit set because it was being committed. When
858 * the commit finished, we've filed the buffer for
859 * checkpointing and marked it dirty. Now we are reallocating
860 * the buffer so the transaction freeing it must have
861 * committed and so it's safe to clear the dirty bit.
862 */
863 clear_buffer_dirty(jh2bh(jh));
Dave Kleikamp470decc2006-10-11 01:20:57 -0700864 jh->b_transaction = transaction;
Josef Bacik9fc7c632008-04-17 10:38:59 -0400865
866 /* first access by this transaction */
867 jh->b_modified = 0;
868
Dave Kleikamp470decc2006-10-11 01:20:57 -0700869 JBUFFER_TRACE(jh, "file as BJ_Reserved");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700870 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700871 } else if (jh->b_transaction == journal->j_committing_transaction) {
Josef Bacik9fc7c632008-04-17 10:38:59 -0400872 /* first access by this transaction */
873 jh->b_modified = 0;
874
Dave Kleikamp470decc2006-10-11 01:20:57 -0700875 JBUFFER_TRACE(jh, "set next transaction");
876 jh->b_next_transaction = transaction;
877 }
878 spin_unlock(&journal->j_list_lock);
879 jbd_unlock_bh_state(bh);
880
881 /*
882 * akpm: I added this. ext3_alloc_branch can pick up new indirect
883 * blocks which contain freed but then revoked metadata. We need
884 * to cancel the revoke in case we end up freeing it yet again
885 * and the reallocating as data - this would cause a second revoke,
886 * which hits an assertion error.
887 */
888 JBUFFER_TRACE(jh, "cancelling revoke");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700889 jbd2_journal_cancel_revoke(handle, jh);
890 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700891out:
892 return err;
893}
894
895/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700896 * int jbd2_journal_get_undo_access() - Notify intent to modify metadata with
Dave Kleikamp470decc2006-10-11 01:20:57 -0700897 * non-rewindable consequences
898 * @handle: transaction
899 * @bh: buffer to undo
900 * @credits: store the number of taken credits here (if not NULL)
901 *
902 * Sometimes there is a need to distinguish between metadata which has
903 * been committed to disk and that which has not. The ext3fs code uses
904 * this for freeing and allocating space, we have to make sure that we
905 * do not reuse freed space until the deallocation has been committed,
906 * since if we overwrote that space we would make the delete
907 * un-rewindable in case of a crash.
908 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700909 * To deal with that, jbd2_journal_get_undo_access requests write access to a
Dave Kleikamp470decc2006-10-11 01:20:57 -0700910 * buffer for parts of non-rewindable operations such as delete
911 * operations on the bitmaps. The journaling code must keep a copy of
912 * the buffer's contents prior to the undo_access call until such time
913 * as we know that the buffer has definitely been committed to disk.
914 *
915 * We never need to know which transaction the committed data is part
916 * of, buffers touched here are guaranteed to be dirtied later and so
917 * will be committed to a new transaction in due course, at which point
918 * we can discard the old committed data pointer.
919 *
920 * Returns error number or 0 on success.
921 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700922int jbd2_journal_get_undo_access(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700923{
924 int err;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700925 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700926 char *committed_data = NULL;
927
928 JBUFFER_TRACE(jh, "entry");
929
930 /*
931 * Do this first --- it can drop the journal lock, so we want to
932 * make sure that obtaining the committed_data is done
933 * atomically wrt. completion of any outstanding commits.
934 */
935 err = do_get_write_access(handle, jh, 1);
936 if (err)
937 goto out;
938
939repeat:
940 if (!jh->b_committed_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400941 committed_data = jbd2_alloc(jh2bh(jh)->b_size, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700942 if (!committed_data) {
943 printk(KERN_EMERG "%s: No memory for committed data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400944 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700945 err = -ENOMEM;
946 goto out;
947 }
948 }
949
950 jbd_lock_bh_state(bh);
951 if (!jh->b_committed_data) {
952 /* Copy out the current buffer contents into the
953 * preserved, committed copy. */
954 JBUFFER_TRACE(jh, "generate b_committed data");
955 if (!committed_data) {
956 jbd_unlock_bh_state(bh);
957 goto repeat;
958 }
959
960 jh->b_committed_data = committed_data;
961 committed_data = NULL;
962 memcpy(jh->b_committed_data, bh->b_data, bh->b_size);
963 }
964 jbd_unlock_bh_state(bh);
965out:
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700966 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700967 if (unlikely(committed_data))
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400968 jbd2_free(committed_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700969 return err;
970}
971
972/**
Joel Beckere06c8222008-09-11 15:35:47 -0700973 * void jbd2_journal_set_triggers() - Add triggers for commit writeout
974 * @bh: buffer to trigger on
975 * @type: struct jbd2_buffer_trigger_type containing the trigger(s).
976 *
977 * Set any triggers on this journal_head. This is always safe, because
978 * triggers for a committing buffer will be saved off, and triggers for
979 * a running transaction will match the buffer in that transaction.
980 *
981 * Call with NULL to clear the triggers.
982 */
983void jbd2_journal_set_triggers(struct buffer_head *bh,
984 struct jbd2_buffer_trigger_type *type)
985{
986 struct journal_head *jh = bh2jh(bh);
987
988 jh->b_triggers = type;
989}
990
991void jbd2_buffer_commit_trigger(struct journal_head *jh, void *mapped_data,
992 struct jbd2_buffer_trigger_type *triggers)
993{
994 struct buffer_head *bh = jh2bh(jh);
995
996 if (!triggers || !triggers->t_commit)
997 return;
998
999 triggers->t_commit(triggers, bh, mapped_data, bh->b_size);
1000}
1001
1002void jbd2_buffer_abort_trigger(struct journal_head *jh,
1003 struct jbd2_buffer_trigger_type *triggers)
1004{
1005 if (!triggers || !triggers->t_abort)
1006 return;
1007
1008 triggers->t_abort(triggers, jh2bh(jh));
1009}
1010
1011
1012
1013/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001014 * int jbd2_journal_dirty_metadata() - mark a buffer as containing dirty metadata
Dave Kleikamp470decc2006-10-11 01:20:57 -07001015 * @handle: transaction to add buffer to.
1016 * @bh: buffer to mark
1017 *
1018 * mark dirty metadata which needs to be journaled as part of the current
1019 * transaction.
1020 *
1021 * The buffer is placed on the transaction's metadata list and is marked
1022 * as belonging to the transaction.
1023 *
1024 * Returns error number or 0 on success.
1025 *
1026 * Special care needs to be taken if the buffer already belongs to the
1027 * current committing transaction (in which case we should have frozen
1028 * data present for that commit). In that case, we don't relink the
1029 * buffer: that only gets done when the old transaction finally
1030 * completes its commit.
1031 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001032int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001033{
1034 transaction_t *transaction = handle->h_transaction;
1035 journal_t *journal = transaction->t_journal;
1036 struct journal_head *jh = bh2jh(bh);
1037
1038 jbd_debug(5, "journal_head %p\n", jh);
1039 JBUFFER_TRACE(jh, "entry");
1040 if (is_handle_aborted(handle))
1041 goto out;
1042
1043 jbd_lock_bh_state(bh);
1044
1045 if (jh->b_modified == 0) {
1046 /*
1047 * This buffer's got modified and becoming part
1048 * of the transaction. This needs to be done
1049 * once a transaction -bzzz
1050 */
1051 jh->b_modified = 1;
1052 J_ASSERT_JH(jh, handle->h_buffer_credits > 0);
1053 handle->h_buffer_credits--;
1054 }
1055
1056 /*
1057 * fastpath, to avoid expensive locking. If this buffer is already
1058 * on the running transaction's metadata list there is nothing to do.
1059 * Nobody can take it off again because there is a handle open.
1060 * I _think_ we're OK here with SMP barriers - a mistaken decision will
1061 * result in this test being false, so we go in and take the locks.
1062 */
1063 if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) {
1064 JBUFFER_TRACE(jh, "fastpath");
1065 J_ASSERT_JH(jh, jh->b_transaction ==
1066 journal->j_running_transaction);
1067 goto out_unlock_bh;
1068 }
1069
1070 set_buffer_jbddirty(bh);
1071
1072 /*
1073 * Metadata already on the current transaction list doesn't
1074 * need to be filed. Metadata on another transaction's list must
1075 * be committing, and will be refiled once the commit completes:
1076 * leave it alone for now.
1077 */
1078 if (jh->b_transaction != transaction) {
1079 JBUFFER_TRACE(jh, "already on other transaction");
1080 J_ASSERT_JH(jh, jh->b_transaction ==
1081 journal->j_committing_transaction);
1082 J_ASSERT_JH(jh, jh->b_next_transaction == transaction);
1083 /* And this case is illegal: we can't reuse another
1084 * transaction's data buffer, ever. */
1085 goto out_unlock_bh;
1086 }
1087
1088 /* That test should have eliminated the following case: */
Mingming Cao40191912008-01-28 23:58:27 -05001089 J_ASSERT_JH(jh, jh->b_frozen_data == NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001090
1091 JBUFFER_TRACE(jh, "file as BJ_Metadata");
1092 spin_lock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001093 __jbd2_journal_file_buffer(jh, handle->h_transaction, BJ_Metadata);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001094 spin_unlock(&journal->j_list_lock);
1095out_unlock_bh:
1096 jbd_unlock_bh_state(bh);
1097out:
1098 JBUFFER_TRACE(jh, "exit");
1099 return 0;
1100}
1101
1102/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001103 * jbd2_journal_release_buffer: undo a get_write_access without any buffer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001104 * updates, if the update decided in the end that it didn't need access.
1105 *
1106 */
1107void
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001108jbd2_journal_release_buffer(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001109{
1110 BUFFER_TRACE(bh, "entry");
1111}
1112
1113/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001114 * void jbd2_journal_forget() - bforget() for potentially-journaled buffers.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001115 * @handle: transaction handle
1116 * @bh: bh to 'forget'
1117 *
1118 * We can only do the bforget if there are no commits pending against the
1119 * buffer. If the buffer is dirty in the current running transaction we
1120 * can safely unlink it.
1121 *
1122 * bh may not be a journalled buffer at all - it may be a non-JBD
1123 * buffer which came off the hashtable. Check for this.
1124 *
1125 * Decrements bh->b_count by one.
1126 *
1127 * Allow this call even if the handle has aborted --- it may be part of
1128 * the caller's cleanup after an abort.
1129 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001130int jbd2_journal_forget (handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001131{
1132 transaction_t *transaction = handle->h_transaction;
1133 journal_t *journal = transaction->t_journal;
1134 struct journal_head *jh;
1135 int drop_reserve = 0;
1136 int err = 0;
Josef Bacik1dfc3222008-04-17 10:38:59 -04001137 int was_modified = 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001138
1139 BUFFER_TRACE(bh, "entry");
1140
1141 jbd_lock_bh_state(bh);
1142 spin_lock(&journal->j_list_lock);
1143
1144 if (!buffer_jbd(bh))
1145 goto not_jbd;
1146 jh = bh2jh(bh);
1147
1148 /* Critical error: attempting to delete a bitmap buffer, maybe?
1149 * Don't do any jbd operations, and return an error. */
1150 if (!J_EXPECT_JH(jh, !jh->b_committed_data,
1151 "inconsistent data on disk")) {
1152 err = -EIO;
1153 goto not_jbd;
1154 }
1155
Josef Bacik1dfc3222008-04-17 10:38:59 -04001156 /* keep track of wether or not this transaction modified us */
1157 was_modified = jh->b_modified;
1158
Dave Kleikamp470decc2006-10-11 01:20:57 -07001159 /*
1160 * The buffer's going from the transaction, we must drop
1161 * all references -bzzz
1162 */
1163 jh->b_modified = 0;
1164
1165 if (jh->b_transaction == handle->h_transaction) {
1166 J_ASSERT_JH(jh, !jh->b_frozen_data);
1167
1168 /* If we are forgetting a buffer which is already part
1169 * of this transaction, then we can just drop it from
1170 * the transaction immediately. */
1171 clear_buffer_dirty(bh);
1172 clear_buffer_jbddirty(bh);
1173
1174 JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
1175
Josef Bacik1dfc3222008-04-17 10:38:59 -04001176 /*
1177 * we only want to drop a reference if this transaction
1178 * modified the buffer
1179 */
1180 if (was_modified)
1181 drop_reserve = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001182
1183 /*
1184 * We are no longer going to journal this buffer.
1185 * However, the commit of this transaction is still
1186 * important to the buffer: the delete that we are now
1187 * processing might obsolete an old log entry, so by
1188 * committing, we can satisfy the buffer's checkpoint.
1189 *
1190 * So, if we have a checkpoint on the buffer, we should
1191 * now refile the buffer on our BJ_Forget list so that
1192 * we know to remove the checkpoint after we commit.
1193 */
1194
1195 if (jh->b_cp_transaction) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001196 __jbd2_journal_temp_unlink_buffer(jh);
1197 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001198 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001199 __jbd2_journal_unfile_buffer(jh);
1200 jbd2_journal_remove_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001201 __brelse(bh);
1202 if (!buffer_jbd(bh)) {
1203 spin_unlock(&journal->j_list_lock);
1204 jbd_unlock_bh_state(bh);
1205 __bforget(bh);
1206 goto drop;
1207 }
1208 }
1209 } else if (jh->b_transaction) {
1210 J_ASSERT_JH(jh, (jh->b_transaction ==
1211 journal->j_committing_transaction));
1212 /* However, if the buffer is still owned by a prior
1213 * (committing) transaction, we can't drop it yet... */
1214 JBUFFER_TRACE(jh, "belongs to older transaction");
1215 /* ... but we CAN drop it from the new transaction if we
1216 * have also modified it since the original commit. */
1217
1218 if (jh->b_next_transaction) {
1219 J_ASSERT(jh->b_next_transaction == transaction);
1220 jh->b_next_transaction = NULL;
Josef Bacik1dfc3222008-04-17 10:38:59 -04001221
1222 /*
1223 * only drop a reference if this transaction modified
1224 * the buffer
1225 */
1226 if (was_modified)
1227 drop_reserve = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001228 }
1229 }
1230
1231not_jbd:
1232 spin_unlock(&journal->j_list_lock);
1233 jbd_unlock_bh_state(bh);
1234 __brelse(bh);
1235drop:
1236 if (drop_reserve) {
1237 /* no need to reserve log space for this block -bzzz */
1238 handle->h_buffer_credits++;
1239 }
1240 return err;
1241}
1242
1243/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001244 * int jbd2_journal_stop() - complete a transaction
Dave Kleikamp470decc2006-10-11 01:20:57 -07001245 * @handle: tranaction to complete.
1246 *
1247 * All done for a particular handle.
1248 *
1249 * There is not much action needed here. We just return any remaining
1250 * buffer credits to the transaction and remove the handle. The only
1251 * complication is that we need to start a commit operation if the
1252 * filesystem is marked for synchronous update.
1253 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001254 * jbd2_journal_stop itself will not usually return an error, but it may
Dave Kleikamp470decc2006-10-11 01:20:57 -07001255 * do so in unusual circumstances. In particular, expect it to
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001256 * return -EIO if a jbd2_journal_abort has been executed since the
Dave Kleikamp470decc2006-10-11 01:20:57 -07001257 * transaction began.
1258 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001259int jbd2_journal_stop(handle_t *handle)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001260{
1261 transaction_t *transaction = handle->h_transaction;
1262 journal_t *journal = transaction->t_journal;
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001263 int err, wait_for_commit = 0;
1264 tid_t tid;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001265 pid_t pid;
1266
Dave Kleikamp470decc2006-10-11 01:20:57 -07001267 J_ASSERT(journal_current_handle() == handle);
1268
1269 if (is_handle_aborted(handle))
1270 err = -EIO;
OGAWA Hirofumi3e2a5322006-10-19 23:29:11 -07001271 else {
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001272 J_ASSERT(atomic_read(&transaction->t_updates) > 0);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001273 err = 0;
OGAWA Hirofumi3e2a5322006-10-19 23:29:11 -07001274 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001275
1276 if (--handle->h_ref > 0) {
1277 jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
1278 handle->h_ref);
1279 return err;
1280 }
1281
1282 jbd_debug(4, "Handle %p going down\n", handle);
1283
1284 /*
1285 * Implement synchronous transaction batching. If the handle
1286 * was synchronous, don't force a commit immediately. Let's
Josef Bacike07f7182008-11-26 01:14:26 -05001287 * yield and let another thread piggyback onto this
1288 * transaction. Keep doing that while new threads continue to
1289 * arrive. It doesn't cost much - we're about to run a commit
1290 * and sleep on IO anyway. Speeds up many-threaded, many-dir
1291 * operations by 30x or more...
Dave Kleikamp470decc2006-10-11 01:20:57 -07001292 *
Josef Bacike07f7182008-11-26 01:14:26 -05001293 * We try and optimize the sleep time against what the
1294 * underlying disk can do, instead of having a static sleep
1295 * time. This is useful for the case where our storage is so
1296 * fast that it is more optimal to go ahead and force a flush
1297 * and wait for the transaction to be committed than it is to
1298 * wait for an arbitrary amount of time for new writers to
1299 * join the transaction. We achieve this by measuring how
1300 * long it takes to commit a transaction, and compare it with
1301 * how long this transaction has been running, and if run time
1302 * < commit time then we sleep for the delta and commit. This
1303 * greatly helps super fast disks that would see slowdowns as
1304 * more threads started doing fsyncs.
1305 *
1306 * But don't do this if this process was the most recent one
1307 * to perform a synchronous write. We do this to detect the
1308 * case where a single process is doing a stream of sync
1309 * writes. No point in waiting for joiners in that case.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001310 */
1311 pid = current->pid;
1312 if (handle->h_sync && journal->j_last_sync_writer != pid) {
Josef Bacike07f7182008-11-26 01:14:26 -05001313 u64 commit_time, trans_time;
1314
Dave Kleikamp470decc2006-10-11 01:20:57 -07001315 journal->j_last_sync_writer = pid;
Josef Bacike07f7182008-11-26 01:14:26 -05001316
1317 spin_lock(&journal->j_state_lock);
1318 commit_time = journal->j_average_commit_time;
1319 spin_unlock(&journal->j_state_lock);
1320
1321 trans_time = ktime_to_ns(ktime_sub(ktime_get(),
1322 transaction->t_start_time));
1323
Theodore Ts'o30773842009-01-03 20:27:38 -05001324 commit_time = max_t(u64, commit_time,
1325 1000*journal->j_min_batch_time);
Josef Bacike07f7182008-11-26 01:14:26 -05001326 commit_time = min_t(u64, commit_time,
Theodore Ts'o30773842009-01-03 20:27:38 -05001327 1000*journal->j_max_batch_time);
Josef Bacike07f7182008-11-26 01:14:26 -05001328
1329 if (trans_time < commit_time) {
1330 ktime_t expires = ktime_add_ns(ktime_get(),
1331 commit_time);
1332 set_current_state(TASK_UNINTERRUPTIBLE);
1333 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1334 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001335 }
1336
Theodore Ts'o70585482009-03-25 23:35:46 -04001337 if (handle->h_sync)
1338 transaction->t_synchronous_commit = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001339 current->journal_info = NULL;
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001340 atomic_sub(handle->h_buffer_credits,
1341 &transaction->t_outstanding_credits);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001342
1343 /*
1344 * If the handle is marked SYNC, we need to set another commit
1345 * going! We also want to force a commit if the current
1346 * transaction is occupying too much of the log, or if the
1347 * transaction is too old now.
1348 */
1349 if (handle->h_sync ||
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001350 (atomic_read(&transaction->t_outstanding_credits) >
1351 journal->j_max_transaction_buffers) ||
1352 time_after_eq(jiffies, transaction->t_expires)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001353 /* Do this even for aborted journals: an abort still
1354 * completes the commit thread, it just doesn't write
1355 * anything to disk. */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001356
Dave Kleikamp470decc2006-10-11 01:20:57 -07001357 jbd_debug(2, "transaction too old, requesting commit for "
1358 "handle %p\n", handle);
1359 /* This is non-blocking */
Theodore Ts'oc35a56a2010-05-16 05:00:00 -04001360 jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001361
1362 /*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001363 * Special case: JBD2_SYNC synchronous updates require us
Dave Kleikamp470decc2006-10-11 01:20:57 -07001364 * to wait for the commit to complete.
1365 */
1366 if (handle->h_sync && !(current->flags & PF_MEMALLOC))
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001367 wait_for_commit = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001368 }
1369
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001370 /*
1371 * Once we drop t_updates, if it goes to zero the transaction
1372 * could start commiting on us and eventually disappear. So
1373 * once we do this, we must not dereference transaction
1374 * pointer again.
1375 */
1376 tid = transaction->t_tid;
1377 if (atomic_dec_and_test(&transaction->t_updates)) {
1378 wake_up(&journal->j_wait_updates);
1379 if (journal->j_barrier_count)
1380 wake_up(&journal->j_wait_transaction_locked);
1381 }
1382
1383 if (wait_for_commit)
1384 err = jbd2_log_wait_commit(journal, tid);
1385
Ingo Molnar3295f0e2008-08-11 10:30:30 +02001386 lock_map_release(&handle->h_lockdep_map);
Mingming Cao7b751062008-01-28 23:58:27 -05001387
Mingming Caoaf1e76d2007-10-16 18:38:25 -04001388 jbd2_free_handle(handle);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001389 return err;
1390}
1391
Randy Dunlap5648ba52008-04-17 10:38:59 -04001392/**
1393 * int jbd2_journal_force_commit() - force any uncommitted transactions
Dave Kleikamp470decc2006-10-11 01:20:57 -07001394 * @journal: journal to force
1395 *
1396 * For synchronous operations: force any uncommitted transactions
1397 * to disk. May seem kludgy, but it reuses all the handle batching
1398 * code in a very simple manner.
1399 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001400int jbd2_journal_force_commit(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001401{
1402 handle_t *handle;
1403 int ret;
1404
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001405 handle = jbd2_journal_start(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001406 if (IS_ERR(handle)) {
1407 ret = PTR_ERR(handle);
1408 } else {
1409 handle->h_sync = 1;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001410 ret = jbd2_journal_stop(handle);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001411 }
1412 return ret;
1413}
1414
1415/*
1416 *
1417 * List management code snippets: various functions for manipulating the
1418 * transaction buffer lists.
1419 *
1420 */
1421
1422/*
1423 * Append a buffer to a transaction list, given the transaction's list head
1424 * pointer.
1425 *
1426 * j_list_lock is held.
1427 *
1428 * jbd_lock_bh_state(jh2bh(jh)) is held.
1429 */
1430
1431static inline void
1432__blist_add_buffer(struct journal_head **list, struct journal_head *jh)
1433{
1434 if (!*list) {
1435 jh->b_tnext = jh->b_tprev = jh;
1436 *list = jh;
1437 } else {
1438 /* Insert at the tail of the list to preserve order */
1439 struct journal_head *first = *list, *last = first->b_tprev;
1440 jh->b_tprev = last;
1441 jh->b_tnext = first;
1442 last->b_tnext = first->b_tprev = jh;
1443 }
1444}
1445
1446/*
1447 * Remove a buffer from a transaction list, given the transaction's list
1448 * head pointer.
1449 *
1450 * Called with j_list_lock held, and the journal may not be locked.
1451 *
1452 * jbd_lock_bh_state(jh2bh(jh)) is held.
1453 */
1454
1455static inline void
1456__blist_del_buffer(struct journal_head **list, struct journal_head *jh)
1457{
1458 if (*list == jh) {
1459 *list = jh->b_tnext;
1460 if (*list == jh)
1461 *list = NULL;
1462 }
1463 jh->b_tprev->b_tnext = jh->b_tnext;
1464 jh->b_tnext->b_tprev = jh->b_tprev;
1465}
1466
1467/*
1468 * Remove a buffer from the appropriate transaction list.
1469 *
1470 * Note that this function can *change* the value of
Jan Kara87c89c22008-07-11 19:27:31 -04001471 * bh->b_transaction->t_buffers, t_forget, t_iobuf_list, t_shadow_list,
1472 * t_log_list or t_reserved_list. If the caller is holding onto a copy of one
1473 * of these pointers, it could go bad. Generally the caller needs to re-read
1474 * the pointer from the transaction_t.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001475 *
1476 * Called under j_list_lock. The journal may not be locked.
1477 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001478void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001479{
1480 struct journal_head **list = NULL;
1481 transaction_t *transaction;
1482 struct buffer_head *bh = jh2bh(jh);
1483
1484 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1485 transaction = jh->b_transaction;
1486 if (transaction)
1487 assert_spin_locked(&transaction->t_journal->j_list_lock);
1488
1489 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1490 if (jh->b_jlist != BJ_None)
Mingming Cao40191912008-01-28 23:58:27 -05001491 J_ASSERT_JH(jh, transaction != NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001492
1493 switch (jh->b_jlist) {
1494 case BJ_None:
1495 return;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001496 case BJ_Metadata:
1497 transaction->t_nr_buffers--;
1498 J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
1499 list = &transaction->t_buffers;
1500 break;
1501 case BJ_Forget:
1502 list = &transaction->t_forget;
1503 break;
1504 case BJ_IO:
1505 list = &transaction->t_iobuf_list;
1506 break;
1507 case BJ_Shadow:
1508 list = &transaction->t_shadow_list;
1509 break;
1510 case BJ_LogCtl:
1511 list = &transaction->t_log_list;
1512 break;
1513 case BJ_Reserved:
1514 list = &transaction->t_reserved_list;
1515 break;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001516 }
1517
1518 __blist_del_buffer(list, jh);
1519 jh->b_jlist = BJ_None;
1520 if (test_clear_buffer_jbddirty(bh))
1521 mark_buffer_dirty(bh); /* Expose it to the VM */
1522}
1523
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001524void __jbd2_journal_unfile_buffer(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001525{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001526 __jbd2_journal_temp_unlink_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001527 jh->b_transaction = NULL;
1528}
1529
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001530void jbd2_journal_unfile_buffer(journal_t *journal, struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001531{
1532 jbd_lock_bh_state(jh2bh(jh));
1533 spin_lock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001534 __jbd2_journal_unfile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001535 spin_unlock(&journal->j_list_lock);
1536 jbd_unlock_bh_state(jh2bh(jh));
1537}
1538
1539/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001540 * Called from jbd2_journal_try_to_free_buffers().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001541 *
1542 * Called under jbd_lock_bh_state(bh)
1543 */
1544static void
1545__journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh)
1546{
1547 struct journal_head *jh;
1548
1549 jh = bh2jh(bh);
1550
1551 if (buffer_locked(bh) || buffer_dirty(bh))
1552 goto out;
1553
Mingming Cao40191912008-01-28 23:58:27 -05001554 if (jh->b_next_transaction != NULL)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001555 goto out;
1556
1557 spin_lock(&journal->j_list_lock);
Jan Kara87c89c22008-07-11 19:27:31 -04001558 if (jh->b_cp_transaction != NULL && jh->b_transaction == NULL) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001559 /* written-back checkpointed metadata buffer */
1560 if (jh->b_jlist == BJ_None) {
1561 JBUFFER_TRACE(jh, "remove from checkpoint list");
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001562 __jbd2_journal_remove_checkpoint(jh);
1563 jbd2_journal_remove_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001564 __brelse(bh);
1565 }
1566 }
1567 spin_unlock(&journal->j_list_lock);
1568out:
1569 return;
1570}
1571
Dave Kleikamp470decc2006-10-11 01:20:57 -07001572/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001573 * int jbd2_journal_try_to_free_buffers() - try to free page buffers.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001574 * @journal: journal for operation
1575 * @page: to try and free
Mingming Cao530576b2008-07-13 21:06:39 -04001576 * @gfp_mask: we use the mask to detect how hard should we try to release
1577 * buffers. If __GFP_WAIT and __GFP_FS is set, we wait for commit code to
1578 * release the buffers.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001579 *
1580 *
1581 * For all the buffers on this page,
1582 * if they are fully written out ordered data, move them onto BUF_CLEAN
1583 * so try_to_free_buffers() can reap them.
1584 *
1585 * This function returns non-zero if we wish try_to_free_buffers()
1586 * to be called. We do this if the page is releasable by try_to_free_buffers().
1587 * We also do it if the page has locked or dirty buffers and the caller wants
1588 * us to perform sync or async writeout.
1589 *
1590 * This complicates JBD locking somewhat. We aren't protected by the
1591 * BKL here. We wish to remove the buffer from its committing or
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001592 * running transaction's ->t_datalist via __jbd2_journal_unfile_buffer.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001593 *
1594 * This may *change* the value of transaction_t->t_datalist, so anyone
1595 * who looks at t_datalist needs to lock against this function.
1596 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001597 * Even worse, someone may be doing a jbd2_journal_dirty_data on this
1598 * buffer. So we need to lock against that. jbd2_journal_dirty_data()
Dave Kleikamp470decc2006-10-11 01:20:57 -07001599 * will come out of the lock with the buffer dirty, which makes it
1600 * ineligible for release here.
1601 *
1602 * Who else is affected by this? hmm... Really the only contender
1603 * is do_get_write_access() - it could be looking at the buffer while
1604 * journal_try_to_free_buffer() is changing its state. But that
1605 * cannot happen because we never reallocate freed data as metadata
1606 * while the data is part of a transaction. Yes?
Mingming Cao530576b2008-07-13 21:06:39 -04001607 *
1608 * Return 0 on failure, 1 on success
Dave Kleikamp470decc2006-10-11 01:20:57 -07001609 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001610int jbd2_journal_try_to_free_buffers(journal_t *journal,
Mingming Cao530576b2008-07-13 21:06:39 -04001611 struct page *page, gfp_t gfp_mask)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001612{
1613 struct buffer_head *head;
1614 struct buffer_head *bh;
1615 int ret = 0;
1616
1617 J_ASSERT(PageLocked(page));
1618
1619 head = page_buffers(page);
1620 bh = head;
1621 do {
1622 struct journal_head *jh;
1623
1624 /*
1625 * We take our own ref against the journal_head here to avoid
1626 * having to add tons of locking around each instance of
Mingming Cao530576b2008-07-13 21:06:39 -04001627 * jbd2_journal_remove_journal_head() and
1628 * jbd2_journal_put_journal_head().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001629 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001630 jh = jbd2_journal_grab_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001631 if (!jh)
1632 continue;
1633
1634 jbd_lock_bh_state(bh);
1635 __journal_try_to_free_buffer(journal, bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001636 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001637 jbd_unlock_bh_state(bh);
1638 if (buffer_jbd(bh))
1639 goto busy;
1640 } while ((bh = bh->b_this_page) != head);
Mingming Cao530576b2008-07-13 21:06:39 -04001641
Dave Kleikamp470decc2006-10-11 01:20:57 -07001642 ret = try_to_free_buffers(page);
Mingming Cao530576b2008-07-13 21:06:39 -04001643
Dave Kleikamp470decc2006-10-11 01:20:57 -07001644busy:
1645 return ret;
1646}
1647
1648/*
1649 * This buffer is no longer needed. If it is on an older transaction's
1650 * checkpoint list we need to record it on this transaction's forget list
1651 * to pin this buffer (and hence its checkpointing transaction) down until
1652 * this transaction commits. If the buffer isn't on a checkpoint list, we
1653 * release it.
1654 * Returns non-zero if JBD no longer has an interest in the buffer.
1655 *
1656 * Called under j_list_lock.
1657 *
1658 * Called under jbd_lock_bh_state(bh).
1659 */
1660static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
1661{
1662 int may_free = 1;
1663 struct buffer_head *bh = jh2bh(jh);
1664
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001665 __jbd2_journal_unfile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001666
1667 if (jh->b_cp_transaction) {
1668 JBUFFER_TRACE(jh, "on running+cp transaction");
Jan Karaf91d1d02009-07-13 16:16:20 -04001669 /*
1670 * We don't want to write the buffer anymore, clear the
1671 * bit so that we don't confuse checks in
1672 * __journal_file_buffer
1673 */
1674 clear_buffer_dirty(bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001675 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001676 may_free = 0;
1677 } else {
1678 JBUFFER_TRACE(jh, "on running transaction");
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001679 jbd2_journal_remove_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001680 __brelse(bh);
1681 }
1682 return may_free;
1683}
1684
1685/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001686 * jbd2_journal_invalidatepage
Dave Kleikamp470decc2006-10-11 01:20:57 -07001687 *
1688 * This code is tricky. It has a number of cases to deal with.
1689 *
1690 * There are two invariants which this code relies on:
1691 *
1692 * i_size must be updated on disk before we start calling invalidatepage on the
1693 * data.
1694 *
1695 * This is done in ext3 by defining an ext3_setattr method which
1696 * updates i_size before truncate gets going. By maintaining this
1697 * invariant, we can be sure that it is safe to throw away any buffers
1698 * attached to the current transaction: once the transaction commits,
1699 * we know that the data will not be needed.
1700 *
1701 * Note however that we can *not* throw away data belonging to the
1702 * previous, committing transaction!
1703 *
1704 * Any disk blocks which *are* part of the previous, committing
1705 * transaction (and which therefore cannot be discarded immediately) are
1706 * not going to be reused in the new running transaction
1707 *
1708 * The bitmap committed_data images guarantee this: any block which is
1709 * allocated in one transaction and removed in the next will be marked
1710 * as in-use in the committed_data bitmap, so cannot be reused until
1711 * the next transaction to delete the block commits. This means that
1712 * leaving committing buffers dirty is quite safe: the disk blocks
1713 * cannot be reallocated to a different file and so buffer aliasing is
1714 * not possible.
1715 *
1716 *
1717 * The above applies mainly to ordered data mode. In writeback mode we
1718 * don't make guarantees about the order in which data hits disk --- in
1719 * particular we don't guarantee that new dirty data is flushed before
1720 * transaction commit --- so it is always safe just to discard data
1721 * immediately in that mode. --sct
1722 */
1723
1724/*
1725 * The journal_unmap_buffer helper function returns zero if the buffer
1726 * concerned remains pinned as an anonymous buffer belonging to an older
1727 * transaction.
1728 *
1729 * We're outside-transaction here. Either or both of j_running_transaction
1730 * and j_committing_transaction may be NULL.
1731 */
1732static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh)
1733{
1734 transaction_t *transaction;
1735 struct journal_head *jh;
1736 int may_free = 1;
1737 int ret;
1738
1739 BUFFER_TRACE(bh, "entry");
1740
1741 /*
1742 * It is safe to proceed here without the j_list_lock because the
1743 * buffers cannot be stolen by try_to_free_buffers as long as we are
1744 * holding the page lock. --sct
1745 */
1746
1747 if (!buffer_jbd(bh))
1748 goto zap_buffer_unlocked;
1749
Jan Kara87c89c22008-07-11 19:27:31 -04001750 /* OK, we have data buffer in journaled mode */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001751 spin_lock(&journal->j_state_lock);
1752 jbd_lock_bh_state(bh);
1753 spin_lock(&journal->j_list_lock);
1754
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001755 jh = jbd2_journal_grab_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001756 if (!jh)
1757 goto zap_buffer_no_jh;
1758
dingdinghuaba869022010-02-15 16:35:42 -05001759 /*
1760 * We cannot remove the buffer from checkpoint lists until the
1761 * transaction adding inode to orphan list (let's call it T)
1762 * is committed. Otherwise if the transaction changing the
1763 * buffer would be cleaned from the journal before T is
1764 * committed, a crash will cause that the correct contents of
1765 * the buffer will be lost. On the other hand we have to
1766 * clear the buffer dirty bit at latest at the moment when the
1767 * transaction marking the buffer as freed in the filesystem
1768 * structures is committed because from that moment on the
1769 * buffer can be reallocated and used by a different page.
1770 * Since the block hasn't been freed yet but the inode has
1771 * already been added to orphan list, it is safe for us to add
1772 * the buffer to BJ_Forget list of the newest transaction.
1773 */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001774 transaction = jh->b_transaction;
1775 if (transaction == NULL) {
1776 /* First case: not on any transaction. If it
1777 * has no checkpoint link, then we can zap it:
1778 * it's a writeback-mode buffer so we don't care
1779 * if it hits disk safely. */
1780 if (!jh->b_cp_transaction) {
1781 JBUFFER_TRACE(jh, "not on any transaction: zap");
1782 goto zap_buffer;
1783 }
1784
1785 if (!buffer_dirty(bh)) {
1786 /* bdflush has written it. We can drop it now */
1787 goto zap_buffer;
1788 }
1789
1790 /* OK, it must be in the journal but still not
1791 * written fully to disk: it's metadata or
1792 * journaled data... */
1793
1794 if (journal->j_running_transaction) {
1795 /* ... and once the current transaction has
1796 * committed, the buffer won't be needed any
1797 * longer. */
1798 JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
1799 ret = __dispose_buffer(jh,
1800 journal->j_running_transaction);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001801 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001802 spin_unlock(&journal->j_list_lock);
1803 jbd_unlock_bh_state(bh);
1804 spin_unlock(&journal->j_state_lock);
1805 return ret;
1806 } else {
1807 /* There is no currently-running transaction. So the
1808 * orphan record which we wrote for this file must have
1809 * passed into commit. We must attach this buffer to
1810 * the committing transaction, if it exists. */
1811 if (journal->j_committing_transaction) {
1812 JBUFFER_TRACE(jh, "give to committing trans");
1813 ret = __dispose_buffer(jh,
1814 journal->j_committing_transaction);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001815 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001816 spin_unlock(&journal->j_list_lock);
1817 jbd_unlock_bh_state(bh);
1818 spin_unlock(&journal->j_state_lock);
1819 return ret;
1820 } else {
1821 /* The orphan record's transaction has
1822 * committed. We can cleanse this buffer */
1823 clear_buffer_jbddirty(bh);
1824 goto zap_buffer;
1825 }
1826 }
1827 } else if (transaction == journal->j_committing_transaction) {
Eric Sandeen9b579882006-10-28 10:38:28 -07001828 JBUFFER_TRACE(jh, "on committing transaction");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001829 /*
dingdinghuaba869022010-02-15 16:35:42 -05001830 * The buffer is committing, we simply cannot touch
1831 * it. So we just set j_next_transaction to the
1832 * running transaction (if there is one) and mark
1833 * buffer as freed so that commit code knows it should
1834 * clear dirty bits when it is done with the buffer.
1835 */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001836 set_buffer_freed(bh);
dingdinghuaba869022010-02-15 16:35:42 -05001837 if (journal->j_running_transaction && buffer_jbddirty(bh))
1838 jh->b_next_transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001839 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001840 spin_unlock(&journal->j_list_lock);
1841 jbd_unlock_bh_state(bh);
1842 spin_unlock(&journal->j_state_lock);
1843 return 0;
1844 } else {
1845 /* Good, the buffer belongs to the running transaction.
1846 * We are writing our own transaction's data, not any
1847 * previous one's, so it is safe to throw it away
1848 * (remember that we expect the filesystem to have set
1849 * i_size already for this truncate so recovery will not
1850 * expose the disk blocks we are discarding here.) */
1851 J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
Eric Sandeen9b579882006-10-28 10:38:28 -07001852 JBUFFER_TRACE(jh, "on running transaction");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001853 may_free = __dispose_buffer(jh, transaction);
1854 }
1855
1856zap_buffer:
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001857 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001858zap_buffer_no_jh:
1859 spin_unlock(&journal->j_list_lock);
1860 jbd_unlock_bh_state(bh);
1861 spin_unlock(&journal->j_state_lock);
1862zap_buffer_unlocked:
1863 clear_buffer_dirty(bh);
1864 J_ASSERT_BH(bh, !buffer_jbddirty(bh));
1865 clear_buffer_mapped(bh);
1866 clear_buffer_req(bh);
1867 clear_buffer_new(bh);
1868 bh->b_bdev = NULL;
1869 return may_free;
1870}
1871
1872/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001873 * void jbd2_journal_invalidatepage()
Dave Kleikamp470decc2006-10-11 01:20:57 -07001874 * @journal: journal to use for flush...
1875 * @page: page to flush
1876 * @offset: length of page to invalidate.
1877 *
1878 * Reap page buffers containing data after offset in page.
1879 *
1880 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001881void jbd2_journal_invalidatepage(journal_t *journal,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001882 struct page *page,
1883 unsigned long offset)
1884{
1885 struct buffer_head *head, *bh, *next;
1886 unsigned int curr_off = 0;
1887 int may_free = 1;
1888
1889 if (!PageLocked(page))
1890 BUG();
1891 if (!page_has_buffers(page))
1892 return;
1893
1894 /* We will potentially be playing with lists other than just the
1895 * data lists (especially for journaled data mode), so be
1896 * cautious in our locking. */
1897
1898 head = bh = page_buffers(page);
1899 do {
1900 unsigned int next_off = curr_off + bh->b_size;
1901 next = bh->b_this_page;
1902
1903 if (offset <= curr_off) {
1904 /* This block is wholly outside the truncation point */
1905 lock_buffer(bh);
1906 may_free &= journal_unmap_buffer(journal, bh);
1907 unlock_buffer(bh);
1908 }
1909 curr_off = next_off;
1910 bh = next;
1911
1912 } while (bh != head);
1913
1914 if (!offset) {
1915 if (may_free && try_to_free_buffers(page))
1916 J_ASSERT(!page_has_buffers(page));
1917 }
1918}
1919
1920/*
1921 * File a buffer on the given transaction list.
1922 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001923void __jbd2_journal_file_buffer(struct journal_head *jh,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001924 transaction_t *transaction, int jlist)
1925{
1926 struct journal_head **list = NULL;
1927 int was_dirty = 0;
1928 struct buffer_head *bh = jh2bh(jh);
1929
1930 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1931 assert_spin_locked(&transaction->t_journal->j_list_lock);
1932
1933 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1934 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
Mingming Cao40191912008-01-28 23:58:27 -05001935 jh->b_transaction == NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001936
1937 if (jh->b_transaction && jh->b_jlist == jlist)
1938 return;
1939
Dave Kleikamp470decc2006-10-11 01:20:57 -07001940 if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
1941 jlist == BJ_Shadow || jlist == BJ_Forget) {
Jan Karaf91d1d02009-07-13 16:16:20 -04001942 /*
1943 * For metadata buffers, we track dirty bit in buffer_jbddirty
1944 * instead of buffer_dirty. We should not see a dirty bit set
1945 * here because we clear it in do_get_write_access but e.g.
1946 * tune2fs can modify the sb and set the dirty bit at any time
1947 * so we try to gracefully handle that.
1948 */
1949 if (buffer_dirty(bh))
1950 warn_dirty_buffer(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001951 if (test_clear_buffer_dirty(bh) ||
1952 test_clear_buffer_jbddirty(bh))
1953 was_dirty = 1;
1954 }
1955
1956 if (jh->b_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001957 __jbd2_journal_temp_unlink_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001958 jh->b_transaction = transaction;
1959
1960 switch (jlist) {
1961 case BJ_None:
1962 J_ASSERT_JH(jh, !jh->b_committed_data);
1963 J_ASSERT_JH(jh, !jh->b_frozen_data);
1964 return;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001965 case BJ_Metadata:
1966 transaction->t_nr_buffers++;
1967 list = &transaction->t_buffers;
1968 break;
1969 case BJ_Forget:
1970 list = &transaction->t_forget;
1971 break;
1972 case BJ_IO:
1973 list = &transaction->t_iobuf_list;
1974 break;
1975 case BJ_Shadow:
1976 list = &transaction->t_shadow_list;
1977 break;
1978 case BJ_LogCtl:
1979 list = &transaction->t_log_list;
1980 break;
1981 case BJ_Reserved:
1982 list = &transaction->t_reserved_list;
1983 break;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001984 }
1985
1986 __blist_add_buffer(list, jh);
1987 jh->b_jlist = jlist;
1988
1989 if (was_dirty)
1990 set_buffer_jbddirty(bh);
1991}
1992
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001993void jbd2_journal_file_buffer(struct journal_head *jh,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001994 transaction_t *transaction, int jlist)
1995{
1996 jbd_lock_bh_state(jh2bh(jh));
1997 spin_lock(&transaction->t_journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001998 __jbd2_journal_file_buffer(jh, transaction, jlist);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001999 spin_unlock(&transaction->t_journal->j_list_lock);
2000 jbd_unlock_bh_state(jh2bh(jh));
2001}
2002
2003/*
2004 * Remove a buffer from its current buffer list in preparation for
2005 * dropping it from its current transaction entirely. If the buffer has
2006 * already started to be used by a subsequent transaction, refile the
2007 * buffer on that transaction's metadata list.
2008 *
2009 * Called under journal->j_list_lock
2010 *
2011 * Called under jbd_lock_bh_state(jh2bh(jh))
2012 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002013void __jbd2_journal_refile_buffer(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002014{
dingdinghuaba869022010-02-15 16:35:42 -05002015 int was_dirty, jlist;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002016 struct buffer_head *bh = jh2bh(jh);
2017
2018 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
2019 if (jh->b_transaction)
2020 assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
2021
2022 /* If the buffer is now unused, just drop it. */
2023 if (jh->b_next_transaction == NULL) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002024 __jbd2_journal_unfile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002025 return;
2026 }
2027
2028 /*
2029 * It has been modified by a later transaction: add it to the new
2030 * transaction's metadata list.
2031 */
2032
2033 was_dirty = test_clear_buffer_jbddirty(bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002034 __jbd2_journal_temp_unlink_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002035 jh->b_transaction = jh->b_next_transaction;
2036 jh->b_next_transaction = NULL;
dingdinghuaba869022010-02-15 16:35:42 -05002037 if (buffer_freed(bh))
2038 jlist = BJ_Forget;
2039 else if (jh->b_modified)
2040 jlist = BJ_Metadata;
2041 else
2042 jlist = BJ_Reserved;
2043 __jbd2_journal_file_buffer(jh, jh->b_transaction, jlist);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002044 J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
2045
2046 if (was_dirty)
2047 set_buffer_jbddirty(bh);
2048}
2049
2050/*
2051 * For the unlocked version of this call, also make sure that any
2052 * hanging journal_head is cleaned up if necessary.
2053 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002054 * __jbd2_journal_refile_buffer is usually called as part of a single locked
Dave Kleikamp470decc2006-10-11 01:20:57 -07002055 * operation on a buffer_head, in which the caller is probably going to
2056 * be hooking the journal_head onto other lists. In that case it is up
2057 * to the caller to remove the journal_head if necessary. For the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002058 * unlocked jbd2_journal_refile_buffer call, the caller isn't going to be
Dave Kleikamp470decc2006-10-11 01:20:57 -07002059 * doing anything else to the buffer so we need to do the cleanup
2060 * ourselves to avoid a jh leak.
2061 *
2062 * *** The journal_head may be freed by this call! ***
2063 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002064void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002065{
2066 struct buffer_head *bh = jh2bh(jh);
2067
2068 jbd_lock_bh_state(bh);
2069 spin_lock(&journal->j_list_lock);
2070
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002071 __jbd2_journal_refile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002072 jbd_unlock_bh_state(bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002073 jbd2_journal_remove_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002074
2075 spin_unlock(&journal->j_list_lock);
2076 __brelse(bh);
2077}
Jan Karac851ed52008-07-11 19:27:31 -04002078
2079/*
2080 * File inode in the inode list of the handle's transaction
2081 */
2082int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode)
2083{
2084 transaction_t *transaction = handle->h_transaction;
2085 journal_t *journal = transaction->t_journal;
2086
2087 if (is_handle_aborted(handle))
2088 return -EIO;
2089
2090 jbd_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino,
2091 transaction->t_tid);
2092
2093 /*
2094 * First check whether inode isn't already on the transaction's
2095 * lists without taking the lock. Note that this check is safe
2096 * without the lock as we cannot race with somebody removing inode
2097 * from the transaction. The reason is that we remove inode from the
2098 * transaction only in journal_release_jbd_inode() and when we commit
2099 * the transaction. We are guarded from the first case by holding
2100 * a reference to the inode. We are safe against the second case
2101 * because if jinode->i_transaction == transaction, commit code
2102 * cannot touch the transaction because we hold reference to it,
2103 * and if jinode->i_next_transaction == transaction, commit code
2104 * will only file the inode where we want it.
2105 */
2106 if (jinode->i_transaction == transaction ||
2107 jinode->i_next_transaction == transaction)
2108 return 0;
2109
2110 spin_lock(&journal->j_list_lock);
2111
2112 if (jinode->i_transaction == transaction ||
2113 jinode->i_next_transaction == transaction)
2114 goto done;
2115
2116 /* On some different transaction's list - should be
2117 * the committing one */
2118 if (jinode->i_transaction) {
2119 J_ASSERT(jinode->i_next_transaction == NULL);
2120 J_ASSERT(jinode->i_transaction ==
2121 journal->j_committing_transaction);
2122 jinode->i_next_transaction = transaction;
2123 goto done;
2124 }
2125 /* Not on any transaction list... */
2126 J_ASSERT(!jinode->i_next_transaction);
2127 jinode->i_transaction = transaction;
2128 list_add(&jinode->i_list, &transaction->t_inode_list);
2129done:
2130 spin_unlock(&journal->j_list_lock);
2131
2132 return 0;
2133}
2134
2135/*
Jan Kara7f5aa212009-02-10 11:15:34 -05002136 * File truncate and transaction commit interact with each other in a
2137 * non-trivial way. If a transaction writing data block A is
2138 * committing, we cannot discard the data by truncate until we have
2139 * written them. Otherwise if we crashed after the transaction with
2140 * write has committed but before the transaction with truncate has
2141 * committed, we could see stale data in block A. This function is a
2142 * helper to solve this problem. It starts writeout of the truncated
2143 * part in case it is in the committing transaction.
2144 *
2145 * Filesystem code must call this function when inode is journaled in
2146 * ordered mode before truncation happens and after the inode has been
2147 * placed on orphan list with the new inode size. The second condition
2148 * avoids the race that someone writes new data and we start
2149 * committing the transaction after this function has been called but
2150 * before a transaction for truncate is started (and furthermore it
2151 * allows us to optimize the case where the addition to orphan list
2152 * happens in the same transaction as write --- we don't have to write
2153 * any data in such case).
Jan Karac851ed52008-07-11 19:27:31 -04002154 */
Jan Kara7f5aa212009-02-10 11:15:34 -05002155int jbd2_journal_begin_ordered_truncate(journal_t *journal,
2156 struct jbd2_inode *jinode,
Jan Karac851ed52008-07-11 19:27:31 -04002157 loff_t new_size)
2158{
Jan Kara7f5aa212009-02-10 11:15:34 -05002159 transaction_t *inode_trans, *commit_trans;
Jan Karac851ed52008-07-11 19:27:31 -04002160 int ret = 0;
2161
Jan Kara7f5aa212009-02-10 11:15:34 -05002162 /* This is a quick check to avoid locking if not necessary */
2163 if (!jinode->i_transaction)
Jan Karac851ed52008-07-11 19:27:31 -04002164 goto out;
Jan Kara7f5aa212009-02-10 11:15:34 -05002165 /* Locks are here just to force reading of recent values, it is
2166 * enough that the transaction was not committing before we started
2167 * a transaction adding the inode to orphan list */
Jan Karac851ed52008-07-11 19:27:31 -04002168 spin_lock(&journal->j_state_lock);
2169 commit_trans = journal->j_committing_transaction;
2170 spin_unlock(&journal->j_state_lock);
Jan Kara7f5aa212009-02-10 11:15:34 -05002171 spin_lock(&journal->j_list_lock);
2172 inode_trans = jinode->i_transaction;
2173 spin_unlock(&journal->j_list_lock);
2174 if (inode_trans == commit_trans) {
2175 ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping,
Jan Karac851ed52008-07-11 19:27:31 -04002176 new_size, LLONG_MAX);
2177 if (ret)
2178 jbd2_journal_abort(journal, ret);
2179 }
2180out:
2181 return ret;
2182}