blob: 2d7109414cdd6b7a4d21bdb2e738ff20581523a4 [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);
Jan Karade1b7942011-06-13 15:38:22 -040033static void __jbd2_journal_unfile_buffer(struct journal_head *jh);
Adrian Bunk7ddae862006-12-06 20:38:27 -080034
Dave Kleikamp470decc2006-10-11 01:20:57 -070035/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -070036 * jbd2_get_transaction: obtain a new transaction_t object.
Dave Kleikamp470decc2006-10-11 01:20:57 -070037 *
38 * Simply allocate and initialise a new transaction. Create it in
39 * RUNNING state and add it to the current journal (which should not
40 * have an existing running transaction: we only make a new transaction
41 * once we have started to commit the old one).
42 *
43 * Preconditions:
44 * The journal MUST be locked. We don't perform atomic mallocs on the
45 * new transaction and we can't block without protecting against other
46 * processes trying to touch the journal while it is in transition.
47 *
Dave Kleikamp470decc2006-10-11 01:20:57 -070048 */
49
50static transaction_t *
Mingming Caof7f4bcc2006-10-11 01:20:59 -070051jbd2_get_transaction(journal_t *journal, transaction_t *transaction)
Dave Kleikamp470decc2006-10-11 01:20:57 -070052{
53 transaction->t_journal = journal;
54 transaction->t_state = T_RUNNING;
Josef Bacike07f7182008-11-26 01:14:26 -050055 transaction->t_start_time = ktime_get();
Dave Kleikamp470decc2006-10-11 01:20:57 -070056 transaction->t_tid = journal->j_transaction_sequence++;
57 transaction->t_expires = jiffies + journal->j_commit_interval;
58 spin_lock_init(&transaction->t_handle_lock);
Theodore Ts'oa51dca92010-08-02 08:43:25 -040059 atomic_set(&transaction->t_updates, 0);
60 atomic_set(&transaction->t_outstanding_credits, 0);
Theodore Ts'o8dd42042010-08-03 21:38:29 -040061 atomic_set(&transaction->t_handle_count, 0);
Jan Karac851ed52008-07-11 19:27:31 -040062 INIT_LIST_HEAD(&transaction->t_inode_list);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -040063 INIT_LIST_HEAD(&transaction->t_private_list);
Dave Kleikamp470decc2006-10-11 01:20:57 -070064
65 /* Set up the commit timer for the new transaction. */
Andreas Dilgerb1f485f2009-08-10 22:51:53 -040066 journal->j_commit_timer.expires = round_jiffies_up(transaction->t_expires);
Dave Kleikamp470decc2006-10-11 01:20:57 -070067 add_timer(&journal->j_commit_timer);
68
69 J_ASSERT(journal->j_running_transaction == NULL);
70 journal->j_running_transaction = transaction;
Johann Lombardi8e85fb32008-01-28 23:58:27 -050071 transaction->t_max_wait = 0;
72 transaction->t_start = jiffies;
Dave Kleikamp470decc2006-10-11 01:20:57 -070073
74 return transaction;
75}
76
77/*
78 * Handle management.
79 *
80 * A handle_t is an object which represents a single atomic update to a
81 * filesystem, and which tracks all of the modifications which form part
82 * of that one update.
83 */
84
85/*
Tao Ma28e35e42011-05-22 21:45:26 -040086 * Update transaction's maximum wait time, if debugging is enabled.
Theodore Ts'o6d0bf002010-08-09 17:28:38 -040087 *
88 * In order for t_max_wait to be reliable, it must be protected by a
89 * lock. But doing so will mean that start_this_handle() can not be
90 * run in parallel on SMP systems, which limits our scalability. So
91 * unless debugging is enabled, we no longer update t_max_wait, which
92 * means that maximum wait time reported by the jbd2_run_stats
93 * tracepoint will always be zero.
94 */
Tao Ma28e35e42011-05-22 21:45:26 -040095static inline void update_t_max_wait(transaction_t *transaction,
96 unsigned long ts)
Theodore Ts'o6d0bf002010-08-09 17:28:38 -040097{
98#ifdef CONFIG_JBD2_DEBUG
Theodore Ts'o6d0bf002010-08-09 17:28:38 -040099 if (jbd2_journal_enable_debug &&
100 time_after(transaction->t_start, ts)) {
101 ts = jbd2_time_diff(ts, transaction->t_start);
102 spin_lock(&transaction->t_handle_lock);
103 if (ts > transaction->t_max_wait)
104 transaction->t_max_wait = ts;
105 spin_unlock(&transaction->t_handle_lock);
106 }
107#endif
108}
109
110/*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700111 * start_this_handle: Given a handle, deal with any locking or stalling
112 * needed to make sure that there is enough journal space for the handle
113 * to begin. Attach the handle to a transaction and set up the
114 * transaction's buffer credits.
115 */
116
Theodore Ts'o47def822010-07-27 11:56:05 -0400117static int start_this_handle(journal_t *journal, handle_t *handle,
118 int gfp_mask)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700119{
Theodore Ts'oe4471832011-02-12 08:18:24 -0500120 transaction_t *transaction, *new_transaction = NULL;
121 tid_t tid;
122 int needed, need_to_start;
123 int nblocks = handle->h_buffer_credits;
Tao Ma28e35e42011-05-22 21:45:26 -0400124 unsigned long ts = jiffies;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700125
126 if (nblocks > journal->j_max_transaction_buffers) {
127 printk(KERN_ERR "JBD: %s wants too many credits (%d > %d)\n",
128 current->comm, nblocks,
129 journal->j_max_transaction_buffers);
Theodore Ts'o47def822010-07-27 11:56:05 -0400130 return -ENOSPC;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700131 }
132
133alloc_transaction:
134 if (!journal->j_running_transaction) {
Theodore Ts'o47def822010-07-27 11:56:05 -0400135 new_transaction = kzalloc(sizeof(*new_transaction), gfp_mask);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700136 if (!new_transaction) {
Theodore Ts'o47def822010-07-27 11:56:05 -0400137 /*
138 * If __GFP_FS is not present, then we may be
139 * being called from inside the fs writeback
140 * layer, so we MUST NOT fail. Since
141 * __GFP_NOFAIL is going away, we will arrange
142 * to retry the allocation ourselves.
143 */
144 if ((gfp_mask & __GFP_FS) == 0) {
145 congestion_wait(BLK_RW_ASYNC, HZ/50);
146 goto alloc_transaction;
147 }
148 return -ENOMEM;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700149 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700150 }
151
152 jbd_debug(3, "New handle %p going live.\n", handle);
153
Dave Kleikamp470decc2006-10-11 01:20:57 -0700154 /*
155 * We need to hold j_state_lock until t_updates has been incremented,
156 * for proper journal barrier handling
157 */
Theodore Ts'oa931da62010-08-03 21:35:12 -0400158repeat:
159 read_lock(&journal->j_state_lock);
Theodore Ts'o5c2178e2010-10-27 21:30:04 -0400160 BUG_ON(journal->j_flags & JBD2_UNMOUNT);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700161 if (is_journal_aborted(journal) ||
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700162 (journal->j_errno != 0 && !(journal->j_flags & JBD2_ACK_ERR))) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400163 read_unlock(&journal->j_state_lock);
Theodore Ts'o47def822010-07-27 11:56:05 -0400164 kfree(new_transaction);
165 return -EROFS;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700166 }
167
168 /* Wait on the journal's transaction barrier if necessary */
169 if (journal->j_barrier_count) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400170 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700171 wait_event(journal->j_wait_transaction_locked,
172 journal->j_barrier_count == 0);
173 goto repeat;
174 }
175
176 if (!journal->j_running_transaction) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400177 read_unlock(&journal->j_state_lock);
178 if (!new_transaction)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700179 goto alloc_transaction;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400180 write_lock(&journal->j_state_lock);
181 if (!journal->j_running_transaction) {
182 jbd2_get_transaction(journal, new_transaction);
183 new_transaction = NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700184 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400185 write_unlock(&journal->j_state_lock);
186 goto repeat;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700187 }
188
189 transaction = journal->j_running_transaction;
190
191 /*
192 * If the current transaction is locked down for commit, wait for the
193 * lock to be released.
194 */
195 if (transaction->t_state == T_LOCKED) {
196 DEFINE_WAIT(wait);
197
198 prepare_to_wait(&journal->j_wait_transaction_locked,
199 &wait, TASK_UNINTERRUPTIBLE);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400200 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700201 schedule();
202 finish_wait(&journal->j_wait_transaction_locked, &wait);
203 goto repeat;
204 }
205
206 /*
207 * If there is not enough space left in the log to write all potential
208 * buffers requested by this operation, we need to stall pending a log
209 * checkpoint to free some more log space.
210 */
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400211 needed = atomic_add_return(nblocks,
212 &transaction->t_outstanding_credits);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700213
214 if (needed > journal->j_max_transaction_buffers) {
215 /*
216 * If the current transaction is already too large, then start
217 * to commit it: we can then go back and attach this handle to
218 * a new transaction.
219 */
220 DEFINE_WAIT(wait);
221
222 jbd_debug(2, "Handle %p starting new commit...\n", handle);
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400223 atomic_sub(nblocks, &transaction->t_outstanding_credits);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700224 prepare_to_wait(&journal->j_wait_transaction_locked, &wait,
225 TASK_UNINTERRUPTIBLE);
Theodore Ts'oe4471832011-02-12 08:18:24 -0500226 tid = transaction->t_tid;
227 need_to_start = !tid_geq(journal->j_commit_request, tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400228 read_unlock(&journal->j_state_lock);
Theodore Ts'oe4471832011-02-12 08:18:24 -0500229 if (need_to_start)
230 jbd2_log_start_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700231 schedule();
232 finish_wait(&journal->j_wait_transaction_locked, &wait);
233 goto repeat;
234 }
235
236 /*
237 * The commit code assumes that it can get enough log space
238 * without forcing a checkpoint. This is *critical* for
239 * correctness: a checkpoint of a buffer which is also
240 * associated with a committing transaction creates a deadlock,
241 * so commit simply cannot force through checkpoints.
242 *
243 * We must therefore ensure the necessary space in the journal
244 * *before* starting to dirty potentially checkpointed buffers
245 * in the new transaction.
246 *
247 * The worst part is, any transaction currently committing can
248 * reduce the free space arbitrarily. Be careful to account for
249 * those buffers when checkpointing.
250 */
251
252 /*
253 * @@@ AKPM: This seems rather over-defensive. We're giving commit
254 * a _lot_ of headroom: 1/4 of the journal plus the size of
255 * the committing transaction. Really, we only need to give it
256 * committing_transaction->t_outstanding_credits plus "enough" for
257 * the log control blocks.
Uwe Kleine-Königa34f0b32010-12-10 14:55:42 +0100258 * Also, this test is inconsistent with the matching one in
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700259 * jbd2_journal_extend().
Dave Kleikamp470decc2006-10-11 01:20:57 -0700260 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700261 if (__jbd2_log_space_left(journal) < jbd_space_needed(journal)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700262 jbd_debug(2, "Handle %p waiting for checkpoint...\n", handle);
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400263 atomic_sub(nblocks, &transaction->t_outstanding_credits);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400264 read_unlock(&journal->j_state_lock);
265 write_lock(&journal->j_state_lock);
266 if (__jbd2_log_space_left(journal) < jbd_space_needed(journal))
267 __jbd2_log_wait_for_space(journal);
268 write_unlock(&journal->j_state_lock);
269 goto repeat;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700270 }
271
272 /* OK, account for the buffers that this operation expects to
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400273 * use and add the handle to the running transaction.
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400274 */
Tao Ma28e35e42011-05-22 21:45:26 -0400275 update_t_max_wait(transaction, ts);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700276 handle->h_transaction = transaction;
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400277 atomic_inc(&transaction->t_updates);
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400278 atomic_inc(&transaction->t_handle_count);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700279 jbd_debug(4, "Handle %p given %d credits (total %d, free %d)\n",
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400280 handle, nblocks,
281 atomic_read(&transaction->t_outstanding_credits),
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700282 __jbd2_log_space_left(journal));
Theodore Ts'oa931da62010-08-03 21:35:12 -0400283 read_unlock(&journal->j_state_lock);
Jan Kara9599b0e2009-08-17 21:23:17 -0400284
285 lock_map_acquire(&handle->h_lockdep_map);
Theodore Ts'o47def822010-07-27 11:56:05 -0400286 kfree(new_transaction);
287 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700288}
289
Mingming Cao7b751062008-01-28 23:58:27 -0500290static struct lock_class_key jbd2_handle_key;
291
Dave Kleikamp470decc2006-10-11 01:20:57 -0700292/* Allocate a new handle. This should probably be in a slab... */
293static handle_t *new_handle(int nblocks)
294{
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400295 handle_t *handle = jbd2_alloc_handle(GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700296 if (!handle)
297 return NULL;
298 memset(handle, 0, sizeof(*handle));
299 handle->h_buffer_credits = nblocks;
300 handle->h_ref = 1;
301
Mingming Cao7b751062008-01-28 23:58:27 -0500302 lockdep_init_map(&handle->h_lockdep_map, "jbd2_handle",
303 &jbd2_handle_key, 0);
304
Dave Kleikamp470decc2006-10-11 01:20:57 -0700305 return handle;
306}
307
308/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700309 * handle_t *jbd2_journal_start() - Obtain a new handle.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700310 * @journal: Journal to start transaction on.
311 * @nblocks: number of block buffer we might modify
312 *
313 * We make sure that the transaction can guarantee at least nblocks of
314 * modified buffers in the log. We block until the log can guarantee
315 * that much space.
316 *
317 * This function is visible to journal users (like ext3fs), so is not
318 * called with the journal already locked.
319 *
Eryu Guanc8675162011-05-24 17:09:58 -0400320 * Return a pointer to a newly allocated handle, or an ERR_PTR() value
321 * on failure.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700322 */
Theodore Ts'o47def822010-07-27 11:56:05 -0400323handle_t *jbd2__journal_start(journal_t *journal, int nblocks, int gfp_mask)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700324{
325 handle_t *handle = journal_current_handle();
326 int err;
327
328 if (!journal)
329 return ERR_PTR(-EROFS);
330
331 if (handle) {
332 J_ASSERT(handle->h_transaction->t_journal == journal);
333 handle->h_ref++;
334 return handle;
335 }
336
337 handle = new_handle(nblocks);
338 if (!handle)
339 return ERR_PTR(-ENOMEM);
340
341 current->journal_info = handle;
342
Theodore Ts'o47def822010-07-27 11:56:05 -0400343 err = start_this_handle(journal, handle, gfp_mask);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700344 if (err < 0) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400345 jbd2_free_handle(handle);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700346 current->journal_info = NULL;
347 handle = ERR_PTR(err);
348 }
349 return handle;
350}
Theodore Ts'o47def822010-07-27 11:56:05 -0400351EXPORT_SYMBOL(jbd2__journal_start);
352
353
354handle_t *jbd2_journal_start(journal_t *journal, int nblocks)
355{
356 return jbd2__journal_start(journal, nblocks, GFP_NOFS);
357}
358EXPORT_SYMBOL(jbd2_journal_start);
359
Dave Kleikamp470decc2006-10-11 01:20:57 -0700360
361/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700362 * int jbd2_journal_extend() - extend buffer credits.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700363 * @handle: handle to 'extend'
364 * @nblocks: nr blocks to try to extend by.
365 *
366 * Some transactions, such as large extends and truncates, can be done
367 * atomically all at once or in several stages. The operation requests
368 * a credit for a number of buffer modications in advance, but can
369 * extend its credit if it needs more.
370 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700371 * jbd2_journal_extend tries to give the running handle more buffer credits.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700372 * It does not guarantee that allocation - this is a best-effort only.
373 * The calling process MUST be able to deal cleanly with a failure to
374 * extend here.
375 *
376 * Return 0 on success, non-zero on failure.
377 *
378 * return code < 0 implies an error
379 * return code > 0 implies normal transaction-full status.
380 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700381int jbd2_journal_extend(handle_t *handle, int nblocks)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700382{
383 transaction_t *transaction = handle->h_transaction;
384 journal_t *journal = transaction->t_journal;
385 int result;
386 int wanted;
387
388 result = -EIO;
389 if (is_handle_aborted(handle))
390 goto out;
391
392 result = 1;
393
Theodore Ts'oa931da62010-08-03 21:35:12 -0400394 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700395
396 /* Don't extend a locked-down transaction! */
397 if (handle->h_transaction->t_state != T_RUNNING) {
398 jbd_debug(3, "denied handle %p %d blocks: "
399 "transaction not running\n", handle, nblocks);
400 goto error_out;
401 }
402
403 spin_lock(&transaction->t_handle_lock);
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400404 wanted = atomic_read(&transaction->t_outstanding_credits) + nblocks;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700405
406 if (wanted > journal->j_max_transaction_buffers) {
407 jbd_debug(3, "denied handle %p %d blocks: "
408 "transaction too large\n", handle, nblocks);
409 goto unlock;
410 }
411
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700412 if (wanted > __jbd2_log_space_left(journal)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700413 jbd_debug(3, "denied handle %p %d blocks: "
414 "insufficient log space\n", handle, nblocks);
415 goto unlock;
416 }
417
418 handle->h_buffer_credits += nblocks;
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400419 atomic_add(nblocks, &transaction->t_outstanding_credits);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700420 result = 0;
421
422 jbd_debug(3, "extended handle %p by %d\n", handle, nblocks);
423unlock:
424 spin_unlock(&transaction->t_handle_lock);
425error_out:
Theodore Ts'oa931da62010-08-03 21:35:12 -0400426 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700427out:
428 return result;
429}
430
431
432/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700433 * int jbd2_journal_restart() - restart a handle .
Dave Kleikamp470decc2006-10-11 01:20:57 -0700434 * @handle: handle to restart
435 * @nblocks: nr credits requested
436 *
437 * Restart a handle for a multi-transaction filesystem
438 * operation.
439 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700440 * If the jbd2_journal_extend() call above fails to grant new buffer credits
441 * to a running handle, a call to jbd2_journal_restart will commit the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700442 * handle's transaction so far and reattach the handle to a new
443 * transaction capabable of guaranteeing the requested number of
444 * credits.
445 */
Theodore Ts'o47def822010-07-27 11:56:05 -0400446int jbd2__journal_restart(handle_t *handle, int nblocks, int gfp_mask)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700447{
448 transaction_t *transaction = handle->h_transaction;
449 journal_t *journal = transaction->t_journal;
Theodore Ts'oe4471832011-02-12 08:18:24 -0500450 tid_t tid;
451 int need_to_start, ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700452
453 /* If we've had an abort of any type, don't even think about
454 * actually doing the restart! */
455 if (is_handle_aborted(handle))
456 return 0;
457
458 /*
459 * First unlink the handle from its current transaction, and start the
460 * commit on that.
461 */
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400462 J_ASSERT(atomic_read(&transaction->t_updates) > 0);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700463 J_ASSERT(journal_current_handle() == handle);
464
Theodore Ts'oa931da62010-08-03 21:35:12 -0400465 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700466 spin_lock(&transaction->t_handle_lock);
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400467 atomic_sub(handle->h_buffer_credits,
468 &transaction->t_outstanding_credits);
469 if (atomic_dec_and_test(&transaction->t_updates))
Dave Kleikamp470decc2006-10-11 01:20:57 -0700470 wake_up(&journal->j_wait_updates);
471 spin_unlock(&transaction->t_handle_lock);
472
473 jbd_debug(2, "restarting handle %p\n", handle);
Theodore Ts'oe4471832011-02-12 08:18:24 -0500474 tid = transaction->t_tid;
475 need_to_start = !tid_geq(journal->j_commit_request, tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400476 read_unlock(&journal->j_state_lock);
Theodore Ts'oe4471832011-02-12 08:18:24 -0500477 if (need_to_start)
478 jbd2_log_start_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700479
Jan Kara9599b0e2009-08-17 21:23:17 -0400480 lock_map_release(&handle->h_lockdep_map);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700481 handle->h_buffer_credits = nblocks;
Theodore Ts'o47def822010-07-27 11:56:05 -0400482 ret = start_this_handle(journal, handle, gfp_mask);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700483 return ret;
484}
Theodore Ts'o47def822010-07-27 11:56:05 -0400485EXPORT_SYMBOL(jbd2__journal_restart);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700486
487
Theodore Ts'o47def822010-07-27 11:56:05 -0400488int jbd2_journal_restart(handle_t *handle, int nblocks)
489{
490 return jbd2__journal_restart(handle, nblocks, GFP_NOFS);
491}
492EXPORT_SYMBOL(jbd2_journal_restart);
493
Dave Kleikamp470decc2006-10-11 01:20:57 -0700494/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700495 * void jbd2_journal_lock_updates () - establish a transaction barrier.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700496 * @journal: Journal to establish a barrier on.
497 *
498 * This locks out any further updates from being started, and blocks
499 * until all existing updates have completed, returning only once the
500 * journal is in a quiescent state with no updates running.
501 *
502 * The journal lock should not be held on entry.
503 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700504void jbd2_journal_lock_updates(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700505{
506 DEFINE_WAIT(wait);
507
Theodore Ts'oa931da62010-08-03 21:35:12 -0400508 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700509 ++journal->j_barrier_count;
510
511 /* Wait until there are no running updates */
512 while (1) {
513 transaction_t *transaction = journal->j_running_transaction;
514
515 if (!transaction)
516 break;
517
518 spin_lock(&transaction->t_handle_lock);
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400519 if (!atomic_read(&transaction->t_updates)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700520 spin_unlock(&transaction->t_handle_lock);
521 break;
522 }
523 prepare_to_wait(&journal->j_wait_updates, &wait,
524 TASK_UNINTERRUPTIBLE);
525 spin_unlock(&transaction->t_handle_lock);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400526 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700527 schedule();
528 finish_wait(&journal->j_wait_updates, &wait);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400529 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700530 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400531 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700532
533 /*
534 * We have now established a barrier against other normal updates, but
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700535 * we also need to barrier against other jbd2_journal_lock_updates() calls
Dave Kleikamp470decc2006-10-11 01:20:57 -0700536 * to make sure that we serialise special journal-locked operations
537 * too.
538 */
539 mutex_lock(&journal->j_barrier);
540}
541
542/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700543 * void jbd2_journal_unlock_updates (journal_t* journal) - release barrier
Dave Kleikamp470decc2006-10-11 01:20:57 -0700544 * @journal: Journal to release the barrier on.
545 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700546 * Release a transaction barrier obtained with jbd2_journal_lock_updates().
Dave Kleikamp470decc2006-10-11 01:20:57 -0700547 *
548 * Should be called without the journal lock held.
549 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700550void jbd2_journal_unlock_updates (journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700551{
552 J_ASSERT(journal->j_barrier_count != 0);
553
554 mutex_unlock(&journal->j_barrier);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400555 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700556 --journal->j_barrier_count;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400557 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700558 wake_up(&journal->j_wait_transaction_locked);
559}
560
Jan Karaf91d1d02009-07-13 16:16:20 -0400561static void warn_dirty_buffer(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700562{
Jan Karaf91d1d02009-07-13 16:16:20 -0400563 char b[BDEVNAME_SIZE];
Dave Kleikamp470decc2006-10-11 01:20:57 -0700564
Jan Karaf91d1d02009-07-13 16:16:20 -0400565 printk(KERN_WARNING
566 "JBD: Spotted dirty metadata buffer (dev = %s, blocknr = %llu). "
567 "There's a risk of filesystem corruption in case of system "
568 "crash.\n",
569 bdevname(bh->b_bdev, b), (unsigned long long)bh->b_blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700570}
571
572/*
573 * If the buffer is already part of the current transaction, then there
574 * is nothing we need to do. If it is already part of a prior
575 * transaction which we are still committing to disk, then we need to
576 * make sure that we do not overwrite the old copy: we do copy-out to
577 * preserve the copy going to disk. We also account the buffer against
578 * the handle's metadata buffer credits (unless the buffer is already
579 * part of the transaction, that is).
580 *
581 */
582static int
583do_get_write_access(handle_t *handle, struct journal_head *jh,
584 int force_copy)
585{
586 struct buffer_head *bh;
587 transaction_t *transaction;
588 journal_t *journal;
589 int error;
590 char *frozen_buffer = NULL;
591 int need_copy = 0;
592
593 if (is_handle_aborted(handle))
594 return -EROFS;
595
596 transaction = handle->h_transaction;
597 journal = transaction->t_journal;
598
Theodore Ts'ocfef2c62010-12-18 13:07:34 -0500599 jbd_debug(5, "journal_head %p, force_copy %d\n", jh, force_copy);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700600
601 JBUFFER_TRACE(jh, "entry");
602repeat:
603 bh = jh2bh(jh);
604
605 /* @@@ Need to check for errors here at some point. */
606
607 lock_buffer(bh);
608 jbd_lock_bh_state(bh);
609
610 /* We now hold the buffer lock so it is safe to query the buffer
611 * state. Is the buffer dirty?
612 *
613 * If so, there are two possibilities. The buffer may be
614 * non-journaled, and undergoing a quite legitimate writeback.
615 * Otherwise, it is journaled, and we don't expect dirty buffers
616 * in that state (the buffers should be marked JBD_Dirty
617 * instead.) So either the IO is being done under our own
618 * control and this is a bug, or it's a third party IO such as
619 * dump(8) (which may leave the buffer scheduled for read ---
620 * ie. locked but not dirty) or tune2fs (which may actually have
621 * the buffer dirtied, ugh.) */
622
623 if (buffer_dirty(bh)) {
624 /*
625 * First question: is this buffer already part of the current
626 * transaction or the existing committing transaction?
627 */
628 if (jh->b_transaction) {
629 J_ASSERT_JH(jh,
630 jh->b_transaction == transaction ||
631 jh->b_transaction ==
632 journal->j_committing_transaction);
633 if (jh->b_next_transaction)
634 J_ASSERT_JH(jh, jh->b_next_transaction ==
635 transaction);
Jan Karaf91d1d02009-07-13 16:16:20 -0400636 warn_dirty_buffer(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700637 }
638 /*
639 * In any case we need to clean the dirty flag and we must
640 * do it under the buffer lock to be sure we don't race
641 * with running write-out.
642 */
Jan Karaf91d1d02009-07-13 16:16:20 -0400643 JBUFFER_TRACE(jh, "Journalling dirty buffer");
644 clear_buffer_dirty(bh);
645 set_buffer_jbddirty(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700646 }
647
648 unlock_buffer(bh);
649
650 error = -EROFS;
651 if (is_handle_aborted(handle)) {
652 jbd_unlock_bh_state(bh);
653 goto out;
654 }
655 error = 0;
656
657 /*
658 * The buffer is already part of this transaction if b_transaction or
659 * b_next_transaction points to it
660 */
661 if (jh->b_transaction == transaction ||
662 jh->b_next_transaction == transaction)
663 goto done;
664
665 /*
Josef Bacik9fc7c632008-04-17 10:38:59 -0400666 * this is the first time this transaction is touching this buffer,
667 * reset the modified flag
668 */
669 jh->b_modified = 0;
670
671 /*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700672 * If there is already a copy-out version of this buffer, then we don't
673 * need to make another one
674 */
675 if (jh->b_frozen_data) {
676 JBUFFER_TRACE(jh, "has frozen data");
677 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
678 jh->b_next_transaction = transaction;
679 goto done;
680 }
681
682 /* Is there data here we need to preserve? */
683
684 if (jh->b_transaction && jh->b_transaction != transaction) {
685 JBUFFER_TRACE(jh, "owned by older transaction");
686 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
687 J_ASSERT_JH(jh, jh->b_transaction ==
688 journal->j_committing_transaction);
689
690 /* There is one case we have to be very careful about.
691 * If the committing transaction is currently writing
692 * this buffer out to disk and has NOT made a copy-out,
693 * then we cannot modify the buffer contents at all
694 * right now. The essence of copy-out is that it is the
695 * extra copy, not the primary copy, which gets
696 * journaled. If the primary copy is already going to
697 * disk then we cannot do copy-out here. */
698
699 if (jh->b_jlist == BJ_Shadow) {
700 DEFINE_WAIT_BIT(wait, &bh->b_state, BH_Unshadow);
701 wait_queue_head_t *wqh;
702
703 wqh = bit_waitqueue(&bh->b_state, BH_Unshadow);
704
705 JBUFFER_TRACE(jh, "on shadow: sleep");
706 jbd_unlock_bh_state(bh);
707 /* commit wakes up all shadow buffers after IO */
708 for ( ; ; ) {
709 prepare_to_wait(wqh, &wait.wait,
710 TASK_UNINTERRUPTIBLE);
711 if (jh->b_jlist != BJ_Shadow)
712 break;
713 schedule();
714 }
715 finish_wait(wqh, &wait.wait);
716 goto repeat;
717 }
718
719 /* Only do the copy if the currently-owning transaction
720 * still needs it. If it is on the Forget list, the
721 * committing transaction is past that stage. The
722 * buffer had better remain locked during the kmalloc,
723 * but that should be true --- we hold the journal lock
724 * still and the buffer is already on the BUF_JOURNAL
725 * list so won't be flushed.
726 *
727 * Subtle point, though: if this is a get_undo_access,
728 * then we will be relying on the frozen_data to contain
729 * the new value of the committed_data record after the
730 * transaction, so we HAVE to force the frozen_data copy
731 * in that case. */
732
733 if (jh->b_jlist != BJ_Forget || force_copy) {
734 JBUFFER_TRACE(jh, "generate frozen data");
735 if (!frozen_buffer) {
736 JBUFFER_TRACE(jh, "allocate memory for buffer");
737 jbd_unlock_bh_state(bh);
738 frozen_buffer =
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400739 jbd2_alloc(jh2bh(jh)->b_size,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700740 GFP_NOFS);
741 if (!frozen_buffer) {
742 printk(KERN_EMERG
743 "%s: OOM for frozen_buffer\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400744 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700745 JBUFFER_TRACE(jh, "oom!");
746 error = -ENOMEM;
747 jbd_lock_bh_state(bh);
748 goto done;
749 }
750 goto repeat;
751 }
752 jh->b_frozen_data = frozen_buffer;
753 frozen_buffer = NULL;
754 need_copy = 1;
755 }
756 jh->b_next_transaction = transaction;
757 }
758
759
760 /*
761 * Finally, if the buffer is not journaled right now, we need to make
762 * sure it doesn't get written to disk before the caller actually
763 * commits the new data
764 */
765 if (!jh->b_transaction) {
766 JBUFFER_TRACE(jh, "no transaction");
767 J_ASSERT_JH(jh, !jh->b_next_transaction);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700768 JBUFFER_TRACE(jh, "file as BJ_Reserved");
769 spin_lock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700770 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700771 spin_unlock(&journal->j_list_lock);
772 }
773
774done:
775 if (need_copy) {
776 struct page *page;
777 int offset;
778 char *source;
779
780 J_EXPECT_JH(jh, buffer_uptodate(jh2bh(jh)),
781 "Possible IO failure.\n");
782 page = jh2bh(jh)->b_page;
Theodore Ts'oa1dd5332010-12-18 13:13:40 -0500783 offset = offset_in_page(jh2bh(jh)->b_data);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700784 source = kmap_atomic(page, KM_USER0);
Jan Kara13ceef02010-07-14 07:56:33 +0200785 /* Fire data frozen trigger just before we copy the data */
786 jbd2_buffer_frozen_trigger(jh, source + offset,
787 jh->b_triggers);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700788 memcpy(jh->b_frozen_data, source+offset, jh2bh(jh)->b_size);
789 kunmap_atomic(source, KM_USER0);
Joel Beckere06c8222008-09-11 15:35:47 -0700790
791 /*
792 * Now that the frozen data is saved off, we need to store
793 * any matching triggers.
794 */
795 jh->b_frozen_triggers = jh->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700796 }
797 jbd_unlock_bh_state(bh);
798
799 /*
800 * If we are about to journal a buffer, then any revoke pending on it is
801 * no longer valid
802 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700803 jbd2_journal_cancel_revoke(handle, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700804
805out:
806 if (unlikely(frozen_buffer)) /* It's usually NULL */
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400807 jbd2_free(frozen_buffer, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700808
809 JBUFFER_TRACE(jh, "exit");
810 return error;
811}
812
813/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700814 * int jbd2_journal_get_write_access() - notify intent to modify a buffer for metadata (not data) update.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700815 * @handle: transaction to add buffer modifications to
816 * @bh: bh to be used for metadata writes
Dave Kleikamp470decc2006-10-11 01:20:57 -0700817 *
818 * Returns an error code or 0 on success.
819 *
820 * In full data journalling mode the buffer may be of type BJ_AsyncData,
821 * because we're write()ing a buffer which is also part of a shared mapping.
822 */
823
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700824int jbd2_journal_get_write_access(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700825{
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700826 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700827 int rc;
828
829 /* We do not want to get caught playing with fields which the
830 * log thread also manipulates. Make sure that the buffer
831 * completes any outstanding IO before proceeding. */
832 rc = do_get_write_access(handle, jh, 0);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700833 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700834 return rc;
835}
836
837
838/*
839 * When the user wants to journal a newly created buffer_head
840 * (ie. getblk() returned a new buffer and we are going to populate it
841 * manually rather than reading off disk), then we need to keep the
842 * buffer_head locked until it has been completely filled with new
843 * data. In this case, we should be able to make the assertion that
844 * the bh is not already part of an existing transaction.
845 *
846 * The buffer should already be locked by the caller by this point.
847 * There is no lock ranking violation: it was a newly created,
848 * unlocked buffer beforehand. */
849
850/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700851 * int jbd2_journal_get_create_access () - notify intent to use newly created bh
Dave Kleikamp470decc2006-10-11 01:20:57 -0700852 * @handle: transaction to new buffer to
853 * @bh: new buffer.
854 *
855 * Call this if you create a new bh.
856 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700857int jbd2_journal_get_create_access(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700858{
859 transaction_t *transaction = handle->h_transaction;
860 journal_t *journal = transaction->t_journal;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700861 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700862 int err;
863
864 jbd_debug(5, "journal_head %p\n", jh);
865 err = -EROFS;
866 if (is_handle_aborted(handle))
867 goto out;
868 err = 0;
869
870 JBUFFER_TRACE(jh, "entry");
871 /*
872 * The buffer may already belong to this transaction due to pre-zeroing
873 * in the filesystem's new_block code. It may also be on the previous,
874 * committing transaction's lists, but it HAS to be in Forget state in
875 * that case: the transaction must have deleted the buffer for it to be
876 * reused here.
877 */
878 jbd_lock_bh_state(bh);
879 spin_lock(&journal->j_list_lock);
880 J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
881 jh->b_transaction == NULL ||
882 (jh->b_transaction == journal->j_committing_transaction &&
883 jh->b_jlist == BJ_Forget)));
884
885 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
886 J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
887
888 if (jh->b_transaction == NULL) {
Jan Karaf91d1d02009-07-13 16:16:20 -0400889 /*
890 * Previous jbd2_journal_forget() could have left the buffer
891 * with jbddirty bit set because it was being committed. When
892 * the commit finished, we've filed the buffer for
893 * checkpointing and marked it dirty. Now we are reallocating
894 * the buffer so the transaction freeing it must have
895 * committed and so it's safe to clear the dirty bit.
896 */
897 clear_buffer_dirty(jh2bh(jh));
Josef Bacik9fc7c632008-04-17 10:38:59 -0400898 /* first access by this transaction */
899 jh->b_modified = 0;
900
Dave Kleikamp470decc2006-10-11 01:20:57 -0700901 JBUFFER_TRACE(jh, "file as BJ_Reserved");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700902 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700903 } else if (jh->b_transaction == journal->j_committing_transaction) {
Josef Bacik9fc7c632008-04-17 10:38:59 -0400904 /* first access by this transaction */
905 jh->b_modified = 0;
906
Dave Kleikamp470decc2006-10-11 01:20:57 -0700907 JBUFFER_TRACE(jh, "set next transaction");
908 jh->b_next_transaction = transaction;
909 }
910 spin_unlock(&journal->j_list_lock);
911 jbd_unlock_bh_state(bh);
912
913 /*
914 * akpm: I added this. ext3_alloc_branch can pick up new indirect
915 * blocks which contain freed but then revoked metadata. We need
916 * to cancel the revoke in case we end up freeing it yet again
917 * and the reallocating as data - this would cause a second revoke,
918 * which hits an assertion error.
919 */
920 JBUFFER_TRACE(jh, "cancelling revoke");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700921 jbd2_journal_cancel_revoke(handle, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700922out:
Ding Dinghua3991b402011-05-25 17:43:48 -0400923 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700924 return err;
925}
926
927/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700928 * int jbd2_journal_get_undo_access() - Notify intent to modify metadata with
Dave Kleikamp470decc2006-10-11 01:20:57 -0700929 * non-rewindable consequences
930 * @handle: transaction
931 * @bh: buffer to undo
Dave Kleikamp470decc2006-10-11 01:20:57 -0700932 *
933 * Sometimes there is a need to distinguish between metadata which has
934 * been committed to disk and that which has not. The ext3fs code uses
935 * this for freeing and allocating space, we have to make sure that we
936 * do not reuse freed space until the deallocation has been committed,
937 * since if we overwrote that space we would make the delete
938 * un-rewindable in case of a crash.
939 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700940 * To deal with that, jbd2_journal_get_undo_access requests write access to a
Dave Kleikamp470decc2006-10-11 01:20:57 -0700941 * buffer for parts of non-rewindable operations such as delete
942 * operations on the bitmaps. The journaling code must keep a copy of
943 * the buffer's contents prior to the undo_access call until such time
944 * as we know that the buffer has definitely been committed to disk.
945 *
946 * We never need to know which transaction the committed data is part
947 * of, buffers touched here are guaranteed to be dirtied later and so
948 * will be committed to a new transaction in due course, at which point
949 * we can discard the old committed data pointer.
950 *
951 * Returns error number or 0 on success.
952 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700953int jbd2_journal_get_undo_access(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700954{
955 int err;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700956 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700957 char *committed_data = NULL;
958
959 JBUFFER_TRACE(jh, "entry");
960
961 /*
962 * Do this first --- it can drop the journal lock, so we want to
963 * make sure that obtaining the committed_data is done
964 * atomically wrt. completion of any outstanding commits.
965 */
966 err = do_get_write_access(handle, jh, 1);
967 if (err)
968 goto out;
969
970repeat:
971 if (!jh->b_committed_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400972 committed_data = jbd2_alloc(jh2bh(jh)->b_size, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700973 if (!committed_data) {
974 printk(KERN_EMERG "%s: No memory for committed data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400975 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700976 err = -ENOMEM;
977 goto out;
978 }
979 }
980
981 jbd_lock_bh_state(bh);
982 if (!jh->b_committed_data) {
983 /* Copy out the current buffer contents into the
984 * preserved, committed copy. */
985 JBUFFER_TRACE(jh, "generate b_committed data");
986 if (!committed_data) {
987 jbd_unlock_bh_state(bh);
988 goto repeat;
989 }
990
991 jh->b_committed_data = committed_data;
992 committed_data = NULL;
993 memcpy(jh->b_committed_data, bh->b_data, bh->b_size);
994 }
995 jbd_unlock_bh_state(bh);
996out:
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700997 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700998 if (unlikely(committed_data))
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400999 jbd2_free(committed_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001000 return err;
1001}
1002
1003/**
Joel Beckere06c8222008-09-11 15:35:47 -07001004 * void jbd2_journal_set_triggers() - Add triggers for commit writeout
1005 * @bh: buffer to trigger on
1006 * @type: struct jbd2_buffer_trigger_type containing the trigger(s).
1007 *
1008 * Set any triggers on this journal_head. This is always safe, because
1009 * triggers for a committing buffer will be saved off, and triggers for
1010 * a running transaction will match the buffer in that transaction.
1011 *
1012 * Call with NULL to clear the triggers.
1013 */
1014void jbd2_journal_set_triggers(struct buffer_head *bh,
1015 struct jbd2_buffer_trigger_type *type)
1016{
1017 struct journal_head *jh = bh2jh(bh);
1018
1019 jh->b_triggers = type;
1020}
1021
Jan Kara13ceef02010-07-14 07:56:33 +02001022void jbd2_buffer_frozen_trigger(struct journal_head *jh, void *mapped_data,
Joel Beckere06c8222008-09-11 15:35:47 -07001023 struct jbd2_buffer_trigger_type *triggers)
1024{
1025 struct buffer_head *bh = jh2bh(jh);
1026
Jan Kara13ceef02010-07-14 07:56:33 +02001027 if (!triggers || !triggers->t_frozen)
Joel Beckere06c8222008-09-11 15:35:47 -07001028 return;
1029
Jan Kara13ceef02010-07-14 07:56:33 +02001030 triggers->t_frozen(triggers, bh, mapped_data, bh->b_size);
Joel Beckere06c8222008-09-11 15:35:47 -07001031}
1032
1033void jbd2_buffer_abort_trigger(struct journal_head *jh,
1034 struct jbd2_buffer_trigger_type *triggers)
1035{
1036 if (!triggers || !triggers->t_abort)
1037 return;
1038
1039 triggers->t_abort(triggers, jh2bh(jh));
1040}
1041
1042
1043
1044/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001045 * int jbd2_journal_dirty_metadata() - mark a buffer as containing dirty metadata
Dave Kleikamp470decc2006-10-11 01:20:57 -07001046 * @handle: transaction to add buffer to.
1047 * @bh: buffer to mark
1048 *
1049 * mark dirty metadata which needs to be journaled as part of the current
1050 * transaction.
1051 *
1052 * The buffer is placed on the transaction's metadata list and is marked
1053 * as belonging to the transaction.
1054 *
1055 * Returns error number or 0 on success.
1056 *
1057 * Special care needs to be taken if the buffer already belongs to the
1058 * current committing transaction (in which case we should have frozen
1059 * data present for that commit). In that case, we don't relink the
1060 * buffer: that only gets done when the old transaction finally
1061 * completes its commit.
1062 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001063int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001064{
1065 transaction_t *transaction = handle->h_transaction;
1066 journal_t *journal = transaction->t_journal;
1067 struct journal_head *jh = bh2jh(bh);
1068
1069 jbd_debug(5, "journal_head %p\n", jh);
1070 JBUFFER_TRACE(jh, "entry");
1071 if (is_handle_aborted(handle))
1072 goto out;
1073
1074 jbd_lock_bh_state(bh);
1075
1076 if (jh->b_modified == 0) {
1077 /*
1078 * This buffer's got modified and becoming part
1079 * of the transaction. This needs to be done
1080 * once a transaction -bzzz
1081 */
1082 jh->b_modified = 1;
1083 J_ASSERT_JH(jh, handle->h_buffer_credits > 0);
1084 handle->h_buffer_credits--;
1085 }
1086
1087 /*
1088 * fastpath, to avoid expensive locking. If this buffer is already
1089 * on the running transaction's metadata list there is nothing to do.
1090 * Nobody can take it off again because there is a handle open.
1091 * I _think_ we're OK here with SMP barriers - a mistaken decision will
1092 * result in this test being false, so we go in and take the locks.
1093 */
1094 if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) {
1095 JBUFFER_TRACE(jh, "fastpath");
1096 J_ASSERT_JH(jh, jh->b_transaction ==
1097 journal->j_running_transaction);
1098 goto out_unlock_bh;
1099 }
1100
1101 set_buffer_jbddirty(bh);
1102
1103 /*
1104 * Metadata already on the current transaction list doesn't
1105 * need to be filed. Metadata on another transaction's list must
1106 * be committing, and will be refiled once the commit completes:
1107 * leave it alone for now.
1108 */
1109 if (jh->b_transaction != transaction) {
1110 JBUFFER_TRACE(jh, "already on other transaction");
1111 J_ASSERT_JH(jh, jh->b_transaction ==
1112 journal->j_committing_transaction);
1113 J_ASSERT_JH(jh, jh->b_next_transaction == transaction);
1114 /* And this case is illegal: we can't reuse another
1115 * transaction's data buffer, ever. */
1116 goto out_unlock_bh;
1117 }
1118
1119 /* That test should have eliminated the following case: */
Mingming Cao40191912008-01-28 23:58:27 -05001120 J_ASSERT_JH(jh, jh->b_frozen_data == NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001121
1122 JBUFFER_TRACE(jh, "file as BJ_Metadata");
1123 spin_lock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001124 __jbd2_journal_file_buffer(jh, handle->h_transaction, BJ_Metadata);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001125 spin_unlock(&journal->j_list_lock);
1126out_unlock_bh:
1127 jbd_unlock_bh_state(bh);
1128out:
1129 JBUFFER_TRACE(jh, "exit");
1130 return 0;
1131}
1132
1133/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001134 * jbd2_journal_release_buffer: undo a get_write_access without any buffer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001135 * updates, if the update decided in the end that it didn't need access.
1136 *
1137 */
1138void
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001139jbd2_journal_release_buffer(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001140{
1141 BUFFER_TRACE(bh, "entry");
1142}
1143
1144/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001145 * void jbd2_journal_forget() - bforget() for potentially-journaled buffers.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001146 * @handle: transaction handle
1147 * @bh: bh to 'forget'
1148 *
1149 * We can only do the bforget if there are no commits pending against the
1150 * buffer. If the buffer is dirty in the current running transaction we
1151 * can safely unlink it.
1152 *
1153 * bh may not be a journalled buffer at all - it may be a non-JBD
1154 * buffer which came off the hashtable. Check for this.
1155 *
1156 * Decrements bh->b_count by one.
1157 *
1158 * Allow this call even if the handle has aborted --- it may be part of
1159 * the caller's cleanup after an abort.
1160 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001161int jbd2_journal_forget (handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001162{
1163 transaction_t *transaction = handle->h_transaction;
1164 journal_t *journal = transaction->t_journal;
1165 struct journal_head *jh;
1166 int drop_reserve = 0;
1167 int err = 0;
Josef Bacik1dfc3222008-04-17 10:38:59 -04001168 int was_modified = 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001169
1170 BUFFER_TRACE(bh, "entry");
1171
1172 jbd_lock_bh_state(bh);
1173 spin_lock(&journal->j_list_lock);
1174
1175 if (!buffer_jbd(bh))
1176 goto not_jbd;
1177 jh = bh2jh(bh);
1178
1179 /* Critical error: attempting to delete a bitmap buffer, maybe?
1180 * Don't do any jbd operations, and return an error. */
1181 if (!J_EXPECT_JH(jh, !jh->b_committed_data,
1182 "inconsistent data on disk")) {
1183 err = -EIO;
1184 goto not_jbd;
1185 }
1186
Josef Bacik1dfc3222008-04-17 10:38:59 -04001187 /* keep track of wether or not this transaction modified us */
1188 was_modified = jh->b_modified;
1189
Dave Kleikamp470decc2006-10-11 01:20:57 -07001190 /*
1191 * The buffer's going from the transaction, we must drop
1192 * all references -bzzz
1193 */
1194 jh->b_modified = 0;
1195
1196 if (jh->b_transaction == handle->h_transaction) {
1197 J_ASSERT_JH(jh, !jh->b_frozen_data);
1198
1199 /* If we are forgetting a buffer which is already part
1200 * of this transaction, then we can just drop it from
1201 * the transaction immediately. */
1202 clear_buffer_dirty(bh);
1203 clear_buffer_jbddirty(bh);
1204
1205 JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
1206
Josef Bacik1dfc3222008-04-17 10:38:59 -04001207 /*
1208 * we only want to drop a reference if this transaction
1209 * modified the buffer
1210 */
1211 if (was_modified)
1212 drop_reserve = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001213
1214 /*
1215 * We are no longer going to journal this buffer.
1216 * However, the commit of this transaction is still
1217 * important to the buffer: the delete that we are now
1218 * processing might obsolete an old log entry, so by
1219 * committing, we can satisfy the buffer's checkpoint.
1220 *
1221 * So, if we have a checkpoint on the buffer, we should
1222 * now refile the buffer on our BJ_Forget list so that
1223 * we know to remove the checkpoint after we commit.
1224 */
1225
1226 if (jh->b_cp_transaction) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001227 __jbd2_journal_temp_unlink_buffer(jh);
1228 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001229 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001230 __jbd2_journal_unfile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001231 if (!buffer_jbd(bh)) {
1232 spin_unlock(&journal->j_list_lock);
1233 jbd_unlock_bh_state(bh);
1234 __bforget(bh);
1235 goto drop;
1236 }
1237 }
1238 } else if (jh->b_transaction) {
1239 J_ASSERT_JH(jh, (jh->b_transaction ==
1240 journal->j_committing_transaction));
1241 /* However, if the buffer is still owned by a prior
1242 * (committing) transaction, we can't drop it yet... */
1243 JBUFFER_TRACE(jh, "belongs to older transaction");
1244 /* ... but we CAN drop it from the new transaction if we
1245 * have also modified it since the original commit. */
1246
1247 if (jh->b_next_transaction) {
1248 J_ASSERT(jh->b_next_transaction == transaction);
1249 jh->b_next_transaction = NULL;
Josef Bacik1dfc3222008-04-17 10:38:59 -04001250
1251 /*
1252 * only drop a reference if this transaction modified
1253 * the buffer
1254 */
1255 if (was_modified)
1256 drop_reserve = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001257 }
1258 }
1259
1260not_jbd:
1261 spin_unlock(&journal->j_list_lock);
1262 jbd_unlock_bh_state(bh);
1263 __brelse(bh);
1264drop:
1265 if (drop_reserve) {
1266 /* no need to reserve log space for this block -bzzz */
1267 handle->h_buffer_credits++;
1268 }
1269 return err;
1270}
1271
1272/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001273 * int jbd2_journal_stop() - complete a transaction
Dave Kleikamp470decc2006-10-11 01:20:57 -07001274 * @handle: tranaction to complete.
1275 *
1276 * All done for a particular handle.
1277 *
1278 * There is not much action needed here. We just return any remaining
1279 * buffer credits to the transaction and remove the handle. The only
1280 * complication is that we need to start a commit operation if the
1281 * filesystem is marked for synchronous update.
1282 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001283 * jbd2_journal_stop itself will not usually return an error, but it may
Dave Kleikamp470decc2006-10-11 01:20:57 -07001284 * do so in unusual circumstances. In particular, expect it to
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001285 * return -EIO if a jbd2_journal_abort has been executed since the
Dave Kleikamp470decc2006-10-11 01:20:57 -07001286 * transaction began.
1287 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001288int jbd2_journal_stop(handle_t *handle)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001289{
1290 transaction_t *transaction = handle->h_transaction;
1291 journal_t *journal = transaction->t_journal;
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001292 int err, wait_for_commit = 0;
1293 tid_t tid;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001294 pid_t pid;
1295
Dave Kleikamp470decc2006-10-11 01:20:57 -07001296 J_ASSERT(journal_current_handle() == handle);
1297
1298 if (is_handle_aborted(handle))
1299 err = -EIO;
OGAWA Hirofumi3e2a5322006-10-19 23:29:11 -07001300 else {
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001301 J_ASSERT(atomic_read(&transaction->t_updates) > 0);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001302 err = 0;
OGAWA Hirofumi3e2a5322006-10-19 23:29:11 -07001303 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001304
1305 if (--handle->h_ref > 0) {
1306 jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
1307 handle->h_ref);
1308 return err;
1309 }
1310
1311 jbd_debug(4, "Handle %p going down\n", handle);
1312
1313 /*
1314 * Implement synchronous transaction batching. If the handle
1315 * was synchronous, don't force a commit immediately. Let's
Josef Bacike07f7182008-11-26 01:14:26 -05001316 * yield and let another thread piggyback onto this
1317 * transaction. Keep doing that while new threads continue to
1318 * arrive. It doesn't cost much - we're about to run a commit
1319 * and sleep on IO anyway. Speeds up many-threaded, many-dir
1320 * operations by 30x or more...
Dave Kleikamp470decc2006-10-11 01:20:57 -07001321 *
Josef Bacike07f7182008-11-26 01:14:26 -05001322 * We try and optimize the sleep time against what the
1323 * underlying disk can do, instead of having a static sleep
1324 * time. This is useful for the case where our storage is so
1325 * fast that it is more optimal to go ahead and force a flush
1326 * and wait for the transaction to be committed than it is to
1327 * wait for an arbitrary amount of time for new writers to
1328 * join the transaction. We achieve this by measuring how
1329 * long it takes to commit a transaction, and compare it with
1330 * how long this transaction has been running, and if run time
1331 * < commit time then we sleep for the delta and commit. This
1332 * greatly helps super fast disks that would see slowdowns as
1333 * more threads started doing fsyncs.
1334 *
1335 * But don't do this if this process was the most recent one
1336 * to perform a synchronous write. We do this to detect the
1337 * case where a single process is doing a stream of sync
1338 * writes. No point in waiting for joiners in that case.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001339 */
1340 pid = current->pid;
1341 if (handle->h_sync && journal->j_last_sync_writer != pid) {
Josef Bacike07f7182008-11-26 01:14:26 -05001342 u64 commit_time, trans_time;
1343
Dave Kleikamp470decc2006-10-11 01:20:57 -07001344 journal->j_last_sync_writer = pid;
Josef Bacike07f7182008-11-26 01:14:26 -05001345
Theodore Ts'oa931da62010-08-03 21:35:12 -04001346 read_lock(&journal->j_state_lock);
Josef Bacike07f7182008-11-26 01:14:26 -05001347 commit_time = journal->j_average_commit_time;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001348 read_unlock(&journal->j_state_lock);
Josef Bacike07f7182008-11-26 01:14:26 -05001349
1350 trans_time = ktime_to_ns(ktime_sub(ktime_get(),
1351 transaction->t_start_time));
1352
Theodore Ts'o30773842009-01-03 20:27:38 -05001353 commit_time = max_t(u64, commit_time,
1354 1000*journal->j_min_batch_time);
Josef Bacike07f7182008-11-26 01:14:26 -05001355 commit_time = min_t(u64, commit_time,
Theodore Ts'o30773842009-01-03 20:27:38 -05001356 1000*journal->j_max_batch_time);
Josef Bacike07f7182008-11-26 01:14:26 -05001357
1358 if (trans_time < commit_time) {
1359 ktime_t expires = ktime_add_ns(ktime_get(),
1360 commit_time);
1361 set_current_state(TASK_UNINTERRUPTIBLE);
1362 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1363 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001364 }
1365
Theodore Ts'o70585482009-03-25 23:35:46 -04001366 if (handle->h_sync)
1367 transaction->t_synchronous_commit = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001368 current->journal_info = NULL;
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001369 atomic_sub(handle->h_buffer_credits,
1370 &transaction->t_outstanding_credits);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001371
1372 /*
1373 * If the handle is marked SYNC, we need to set another commit
1374 * going! We also want to force a commit if the current
1375 * transaction is occupying too much of the log, or if the
1376 * transaction is too old now.
1377 */
1378 if (handle->h_sync ||
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001379 (atomic_read(&transaction->t_outstanding_credits) >
1380 journal->j_max_transaction_buffers) ||
1381 time_after_eq(jiffies, transaction->t_expires)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001382 /* Do this even for aborted journals: an abort still
1383 * completes the commit thread, it just doesn't write
1384 * anything to disk. */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001385
Dave Kleikamp470decc2006-10-11 01:20:57 -07001386 jbd_debug(2, "transaction too old, requesting commit for "
1387 "handle %p\n", handle);
1388 /* This is non-blocking */
Theodore Ts'oc35a56a2010-05-16 05:00:00 -04001389 jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001390
1391 /*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001392 * Special case: JBD2_SYNC synchronous updates require us
Dave Kleikamp470decc2006-10-11 01:20:57 -07001393 * to wait for the commit to complete.
1394 */
1395 if (handle->h_sync && !(current->flags & PF_MEMALLOC))
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001396 wait_for_commit = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001397 }
1398
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001399 /*
1400 * Once we drop t_updates, if it goes to zero the transaction
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001401 * could start committing on us and eventually disappear. So
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001402 * once we do this, we must not dereference transaction
1403 * pointer again.
1404 */
1405 tid = transaction->t_tid;
1406 if (atomic_dec_and_test(&transaction->t_updates)) {
1407 wake_up(&journal->j_wait_updates);
1408 if (journal->j_barrier_count)
1409 wake_up(&journal->j_wait_transaction_locked);
1410 }
1411
1412 if (wait_for_commit)
1413 err = jbd2_log_wait_commit(journal, tid);
1414
Ingo Molnar3295f0e2008-08-11 10:30:30 +02001415 lock_map_release(&handle->h_lockdep_map);
Mingming Cao7b751062008-01-28 23:58:27 -05001416
Mingming Caoaf1e76d2007-10-16 18:38:25 -04001417 jbd2_free_handle(handle);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001418 return err;
1419}
1420
Randy Dunlap5648ba52008-04-17 10:38:59 -04001421/**
1422 * int jbd2_journal_force_commit() - force any uncommitted transactions
Dave Kleikamp470decc2006-10-11 01:20:57 -07001423 * @journal: journal to force
1424 *
1425 * For synchronous operations: force any uncommitted transactions
1426 * to disk. May seem kludgy, but it reuses all the handle batching
1427 * code in a very simple manner.
1428 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001429int jbd2_journal_force_commit(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001430{
1431 handle_t *handle;
1432 int ret;
1433
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001434 handle = jbd2_journal_start(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001435 if (IS_ERR(handle)) {
1436 ret = PTR_ERR(handle);
1437 } else {
1438 handle->h_sync = 1;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001439 ret = jbd2_journal_stop(handle);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001440 }
1441 return ret;
1442}
1443
1444/*
1445 *
1446 * List management code snippets: various functions for manipulating the
1447 * transaction buffer lists.
1448 *
1449 */
1450
1451/*
1452 * Append a buffer to a transaction list, given the transaction's list head
1453 * pointer.
1454 *
1455 * j_list_lock is held.
1456 *
1457 * jbd_lock_bh_state(jh2bh(jh)) is held.
1458 */
1459
1460static inline void
1461__blist_add_buffer(struct journal_head **list, struct journal_head *jh)
1462{
1463 if (!*list) {
1464 jh->b_tnext = jh->b_tprev = jh;
1465 *list = jh;
1466 } else {
1467 /* Insert at the tail of the list to preserve order */
1468 struct journal_head *first = *list, *last = first->b_tprev;
1469 jh->b_tprev = last;
1470 jh->b_tnext = first;
1471 last->b_tnext = first->b_tprev = jh;
1472 }
1473}
1474
1475/*
1476 * Remove a buffer from a transaction list, given the transaction's list
1477 * head pointer.
1478 *
1479 * Called with j_list_lock held, and the journal may not be locked.
1480 *
1481 * jbd_lock_bh_state(jh2bh(jh)) is held.
1482 */
1483
1484static inline void
1485__blist_del_buffer(struct journal_head **list, struct journal_head *jh)
1486{
1487 if (*list == jh) {
1488 *list = jh->b_tnext;
1489 if (*list == jh)
1490 *list = NULL;
1491 }
1492 jh->b_tprev->b_tnext = jh->b_tnext;
1493 jh->b_tnext->b_tprev = jh->b_tprev;
1494}
1495
1496/*
1497 * Remove a buffer from the appropriate transaction list.
1498 *
1499 * Note that this function can *change* the value of
Jan Kara87c89c22008-07-11 19:27:31 -04001500 * bh->b_transaction->t_buffers, t_forget, t_iobuf_list, t_shadow_list,
1501 * t_log_list or t_reserved_list. If the caller is holding onto a copy of one
1502 * of these pointers, it could go bad. Generally the caller needs to re-read
1503 * the pointer from the transaction_t.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001504 *
1505 * Called under j_list_lock. The journal may not be locked.
1506 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001507void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001508{
1509 struct journal_head **list = NULL;
1510 transaction_t *transaction;
1511 struct buffer_head *bh = jh2bh(jh);
1512
1513 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1514 transaction = jh->b_transaction;
1515 if (transaction)
1516 assert_spin_locked(&transaction->t_journal->j_list_lock);
1517
1518 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1519 if (jh->b_jlist != BJ_None)
Mingming Cao40191912008-01-28 23:58:27 -05001520 J_ASSERT_JH(jh, transaction != NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001521
1522 switch (jh->b_jlist) {
1523 case BJ_None:
1524 return;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001525 case BJ_Metadata:
1526 transaction->t_nr_buffers--;
1527 J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
1528 list = &transaction->t_buffers;
1529 break;
1530 case BJ_Forget:
1531 list = &transaction->t_forget;
1532 break;
1533 case BJ_IO:
1534 list = &transaction->t_iobuf_list;
1535 break;
1536 case BJ_Shadow:
1537 list = &transaction->t_shadow_list;
1538 break;
1539 case BJ_LogCtl:
1540 list = &transaction->t_log_list;
1541 break;
1542 case BJ_Reserved:
1543 list = &transaction->t_reserved_list;
1544 break;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001545 }
1546
1547 __blist_del_buffer(list, jh);
1548 jh->b_jlist = BJ_None;
1549 if (test_clear_buffer_jbddirty(bh))
1550 mark_buffer_dirty(bh); /* Expose it to the VM */
1551}
1552
Jan Karade1b7942011-06-13 15:38:22 -04001553/*
1554 * Remove buffer from all transactions.
1555 *
1556 * Called with bh_state lock and j_list_lock
1557 *
1558 * jh and bh may be already freed when this function returns.
1559 */
1560static void __jbd2_journal_unfile_buffer(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001561{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001562 __jbd2_journal_temp_unlink_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001563 jh->b_transaction = NULL;
Jan Karade1b7942011-06-13 15:38:22 -04001564 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001565}
1566
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001567void jbd2_journal_unfile_buffer(journal_t *journal, struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001568{
Jan Karade1b7942011-06-13 15:38:22 -04001569 struct buffer_head *bh = jh2bh(jh);
1570
1571 /* Get reference so that buffer cannot be freed before we unlock it */
1572 get_bh(bh);
1573 jbd_lock_bh_state(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001574 spin_lock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001575 __jbd2_journal_unfile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001576 spin_unlock(&journal->j_list_lock);
Jan Karade1b7942011-06-13 15:38:22 -04001577 jbd_unlock_bh_state(bh);
1578 __brelse(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001579}
1580
1581/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001582 * Called from jbd2_journal_try_to_free_buffers().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001583 *
1584 * Called under jbd_lock_bh_state(bh)
1585 */
1586static void
1587__journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh)
1588{
1589 struct journal_head *jh;
1590
1591 jh = bh2jh(bh);
1592
1593 if (buffer_locked(bh) || buffer_dirty(bh))
1594 goto out;
1595
Mingming Cao40191912008-01-28 23:58:27 -05001596 if (jh->b_next_transaction != NULL)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001597 goto out;
1598
1599 spin_lock(&journal->j_list_lock);
Jan Kara87c89c22008-07-11 19:27:31 -04001600 if (jh->b_cp_transaction != NULL && jh->b_transaction == NULL) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001601 /* written-back checkpointed metadata buffer */
1602 if (jh->b_jlist == BJ_None) {
1603 JBUFFER_TRACE(jh, "remove from checkpoint list");
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001604 __jbd2_journal_remove_checkpoint(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001605 }
1606 }
1607 spin_unlock(&journal->j_list_lock);
1608out:
1609 return;
1610}
1611
Dave Kleikamp470decc2006-10-11 01:20:57 -07001612/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001613 * int jbd2_journal_try_to_free_buffers() - try to free page buffers.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001614 * @journal: journal for operation
1615 * @page: to try and free
Mingming Cao530576b2008-07-13 21:06:39 -04001616 * @gfp_mask: we use the mask to detect how hard should we try to release
1617 * buffers. If __GFP_WAIT and __GFP_FS is set, we wait for commit code to
1618 * release the buffers.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001619 *
1620 *
1621 * For all the buffers on this page,
1622 * if they are fully written out ordered data, move them onto BUF_CLEAN
1623 * so try_to_free_buffers() can reap them.
1624 *
1625 * This function returns non-zero if we wish try_to_free_buffers()
1626 * to be called. We do this if the page is releasable by try_to_free_buffers().
1627 * We also do it if the page has locked or dirty buffers and the caller wants
1628 * us to perform sync or async writeout.
1629 *
1630 * This complicates JBD locking somewhat. We aren't protected by the
1631 * BKL here. We wish to remove the buffer from its committing or
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001632 * running transaction's ->t_datalist via __jbd2_journal_unfile_buffer.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001633 *
1634 * This may *change* the value of transaction_t->t_datalist, so anyone
1635 * who looks at t_datalist needs to lock against this function.
1636 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001637 * Even worse, someone may be doing a jbd2_journal_dirty_data on this
1638 * buffer. So we need to lock against that. jbd2_journal_dirty_data()
Dave Kleikamp470decc2006-10-11 01:20:57 -07001639 * will come out of the lock with the buffer dirty, which makes it
1640 * ineligible for release here.
1641 *
1642 * Who else is affected by this? hmm... Really the only contender
1643 * is do_get_write_access() - it could be looking at the buffer while
1644 * journal_try_to_free_buffer() is changing its state. But that
1645 * cannot happen because we never reallocate freed data as metadata
1646 * while the data is part of a transaction. Yes?
Mingming Cao530576b2008-07-13 21:06:39 -04001647 *
1648 * Return 0 on failure, 1 on success
Dave Kleikamp470decc2006-10-11 01:20:57 -07001649 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001650int jbd2_journal_try_to_free_buffers(journal_t *journal,
Mingming Cao530576b2008-07-13 21:06:39 -04001651 struct page *page, gfp_t gfp_mask)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001652{
1653 struct buffer_head *head;
1654 struct buffer_head *bh;
1655 int ret = 0;
1656
1657 J_ASSERT(PageLocked(page));
1658
1659 head = page_buffers(page);
1660 bh = head;
1661 do {
1662 struct journal_head *jh;
1663
1664 /*
1665 * We take our own ref against the journal_head here to avoid
1666 * having to add tons of locking around each instance of
Mingming Cao530576b2008-07-13 21:06:39 -04001667 * jbd2_journal_put_journal_head().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001668 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001669 jh = jbd2_journal_grab_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001670 if (!jh)
1671 continue;
1672
1673 jbd_lock_bh_state(bh);
1674 __journal_try_to_free_buffer(journal, bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001675 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001676 jbd_unlock_bh_state(bh);
1677 if (buffer_jbd(bh))
1678 goto busy;
1679 } while ((bh = bh->b_this_page) != head);
Mingming Cao530576b2008-07-13 21:06:39 -04001680
Dave Kleikamp470decc2006-10-11 01:20:57 -07001681 ret = try_to_free_buffers(page);
Mingming Cao530576b2008-07-13 21:06:39 -04001682
Dave Kleikamp470decc2006-10-11 01:20:57 -07001683busy:
1684 return ret;
1685}
1686
1687/*
1688 * This buffer is no longer needed. If it is on an older transaction's
1689 * checkpoint list we need to record it on this transaction's forget list
1690 * to pin this buffer (and hence its checkpointing transaction) down until
1691 * this transaction commits. If the buffer isn't on a checkpoint list, we
1692 * release it.
1693 * Returns non-zero if JBD no longer has an interest in the buffer.
1694 *
1695 * Called under j_list_lock.
1696 *
1697 * Called under jbd_lock_bh_state(bh).
1698 */
1699static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
1700{
1701 int may_free = 1;
1702 struct buffer_head *bh = jh2bh(jh);
1703
Dave Kleikamp470decc2006-10-11 01:20:57 -07001704 if (jh->b_cp_transaction) {
1705 JBUFFER_TRACE(jh, "on running+cp transaction");
Jan Karade1b7942011-06-13 15:38:22 -04001706 __jbd2_journal_temp_unlink_buffer(jh);
Jan Karaf91d1d02009-07-13 16:16:20 -04001707 /*
1708 * We don't want to write the buffer anymore, clear the
1709 * bit so that we don't confuse checks in
1710 * __journal_file_buffer
1711 */
1712 clear_buffer_dirty(bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001713 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001714 may_free = 0;
1715 } else {
1716 JBUFFER_TRACE(jh, "on running transaction");
Jan Karade1b7942011-06-13 15:38:22 -04001717 __jbd2_journal_unfile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001718 }
1719 return may_free;
1720}
1721
1722/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001723 * jbd2_journal_invalidatepage
Dave Kleikamp470decc2006-10-11 01:20:57 -07001724 *
1725 * This code is tricky. It has a number of cases to deal with.
1726 *
1727 * There are two invariants which this code relies on:
1728 *
1729 * i_size must be updated on disk before we start calling invalidatepage on the
1730 * data.
1731 *
1732 * This is done in ext3 by defining an ext3_setattr method which
1733 * updates i_size before truncate gets going. By maintaining this
1734 * invariant, we can be sure that it is safe to throw away any buffers
1735 * attached to the current transaction: once the transaction commits,
1736 * we know that the data will not be needed.
1737 *
1738 * Note however that we can *not* throw away data belonging to the
1739 * previous, committing transaction!
1740 *
1741 * Any disk blocks which *are* part of the previous, committing
1742 * transaction (and which therefore cannot be discarded immediately) are
1743 * not going to be reused in the new running transaction
1744 *
1745 * The bitmap committed_data images guarantee this: any block which is
1746 * allocated in one transaction and removed in the next will be marked
1747 * as in-use in the committed_data bitmap, so cannot be reused until
1748 * the next transaction to delete the block commits. This means that
1749 * leaving committing buffers dirty is quite safe: the disk blocks
1750 * cannot be reallocated to a different file and so buffer aliasing is
1751 * not possible.
1752 *
1753 *
1754 * The above applies mainly to ordered data mode. In writeback mode we
1755 * don't make guarantees about the order in which data hits disk --- in
1756 * particular we don't guarantee that new dirty data is flushed before
1757 * transaction commit --- so it is always safe just to discard data
1758 * immediately in that mode. --sct
1759 */
1760
1761/*
1762 * The journal_unmap_buffer helper function returns zero if the buffer
1763 * concerned remains pinned as an anonymous buffer belonging to an older
1764 * transaction.
1765 *
1766 * We're outside-transaction here. Either or both of j_running_transaction
1767 * and j_committing_transaction may be NULL.
1768 */
1769static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh)
1770{
1771 transaction_t *transaction;
1772 struct journal_head *jh;
1773 int may_free = 1;
1774 int ret;
1775
1776 BUFFER_TRACE(bh, "entry");
1777
1778 /*
1779 * It is safe to proceed here without the j_list_lock because the
1780 * buffers cannot be stolen by try_to_free_buffers as long as we are
1781 * holding the page lock. --sct
1782 */
1783
1784 if (!buffer_jbd(bh))
1785 goto zap_buffer_unlocked;
1786
Jan Kara87c89c22008-07-11 19:27:31 -04001787 /* OK, we have data buffer in journaled mode */
Theodore Ts'oa931da62010-08-03 21:35:12 -04001788 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001789 jbd_lock_bh_state(bh);
1790 spin_lock(&journal->j_list_lock);
1791
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001792 jh = jbd2_journal_grab_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001793 if (!jh)
1794 goto zap_buffer_no_jh;
1795
dingdinghuaba869022010-02-15 16:35:42 -05001796 /*
1797 * We cannot remove the buffer from checkpoint lists until the
1798 * transaction adding inode to orphan list (let's call it T)
1799 * is committed. Otherwise if the transaction changing the
1800 * buffer would be cleaned from the journal before T is
1801 * committed, a crash will cause that the correct contents of
1802 * the buffer will be lost. On the other hand we have to
1803 * clear the buffer dirty bit at latest at the moment when the
1804 * transaction marking the buffer as freed in the filesystem
1805 * structures is committed because from that moment on the
1806 * buffer can be reallocated and used by a different page.
1807 * Since the block hasn't been freed yet but the inode has
1808 * already been added to orphan list, it is safe for us to add
1809 * the buffer to BJ_Forget list of the newest transaction.
1810 */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001811 transaction = jh->b_transaction;
1812 if (transaction == NULL) {
1813 /* First case: not on any transaction. If it
1814 * has no checkpoint link, then we can zap it:
1815 * it's a writeback-mode buffer so we don't care
1816 * if it hits disk safely. */
1817 if (!jh->b_cp_transaction) {
1818 JBUFFER_TRACE(jh, "not on any transaction: zap");
1819 goto zap_buffer;
1820 }
1821
1822 if (!buffer_dirty(bh)) {
1823 /* bdflush has written it. We can drop it now */
1824 goto zap_buffer;
1825 }
1826
1827 /* OK, it must be in the journal but still not
1828 * written fully to disk: it's metadata or
1829 * journaled data... */
1830
1831 if (journal->j_running_transaction) {
1832 /* ... and once the current transaction has
1833 * committed, the buffer won't be needed any
1834 * longer. */
1835 JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
1836 ret = __dispose_buffer(jh,
1837 journal->j_running_transaction);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001838 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001839 spin_unlock(&journal->j_list_lock);
1840 jbd_unlock_bh_state(bh);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001841 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001842 return ret;
1843 } else {
1844 /* There is no currently-running transaction. So the
1845 * orphan record which we wrote for this file must have
1846 * passed into commit. We must attach this buffer to
1847 * the committing transaction, if it exists. */
1848 if (journal->j_committing_transaction) {
1849 JBUFFER_TRACE(jh, "give to committing trans");
1850 ret = __dispose_buffer(jh,
1851 journal->j_committing_transaction);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001852 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001853 spin_unlock(&journal->j_list_lock);
1854 jbd_unlock_bh_state(bh);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001855 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001856 return ret;
1857 } else {
1858 /* The orphan record's transaction has
1859 * committed. We can cleanse this buffer */
1860 clear_buffer_jbddirty(bh);
1861 goto zap_buffer;
1862 }
1863 }
1864 } else if (transaction == journal->j_committing_transaction) {
Eric Sandeen9b579882006-10-28 10:38:28 -07001865 JBUFFER_TRACE(jh, "on committing transaction");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001866 /*
dingdinghuaba869022010-02-15 16:35:42 -05001867 * The buffer is committing, we simply cannot touch
1868 * it. So we just set j_next_transaction to the
1869 * running transaction (if there is one) and mark
1870 * buffer as freed so that commit code knows it should
1871 * clear dirty bits when it is done with the buffer.
1872 */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001873 set_buffer_freed(bh);
dingdinghuaba869022010-02-15 16:35:42 -05001874 if (journal->j_running_transaction && buffer_jbddirty(bh))
1875 jh->b_next_transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001876 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001877 spin_unlock(&journal->j_list_lock);
1878 jbd_unlock_bh_state(bh);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001879 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001880 return 0;
1881 } else {
1882 /* Good, the buffer belongs to the running transaction.
1883 * We are writing our own transaction's data, not any
1884 * previous one's, so it is safe to throw it away
1885 * (remember that we expect the filesystem to have set
1886 * i_size already for this truncate so recovery will not
1887 * expose the disk blocks we are discarding here.) */
1888 J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
Eric Sandeen9b579882006-10-28 10:38:28 -07001889 JBUFFER_TRACE(jh, "on running transaction");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001890 may_free = __dispose_buffer(jh, transaction);
1891 }
1892
1893zap_buffer:
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001894 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001895zap_buffer_no_jh:
1896 spin_unlock(&journal->j_list_lock);
1897 jbd_unlock_bh_state(bh);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001898 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001899zap_buffer_unlocked:
1900 clear_buffer_dirty(bh);
1901 J_ASSERT_BH(bh, !buffer_jbddirty(bh));
1902 clear_buffer_mapped(bh);
1903 clear_buffer_req(bh);
1904 clear_buffer_new(bh);
1905 bh->b_bdev = NULL;
1906 return may_free;
1907}
1908
1909/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001910 * void jbd2_journal_invalidatepage()
Dave Kleikamp470decc2006-10-11 01:20:57 -07001911 * @journal: journal to use for flush...
1912 * @page: page to flush
1913 * @offset: length of page to invalidate.
1914 *
1915 * Reap page buffers containing data after offset in page.
1916 *
1917 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001918void jbd2_journal_invalidatepage(journal_t *journal,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001919 struct page *page,
1920 unsigned long offset)
1921{
1922 struct buffer_head *head, *bh, *next;
1923 unsigned int curr_off = 0;
1924 int may_free = 1;
1925
1926 if (!PageLocked(page))
1927 BUG();
1928 if (!page_has_buffers(page))
1929 return;
1930
1931 /* We will potentially be playing with lists other than just the
1932 * data lists (especially for journaled data mode), so be
1933 * cautious in our locking. */
1934
1935 head = bh = page_buffers(page);
1936 do {
1937 unsigned int next_off = curr_off + bh->b_size;
1938 next = bh->b_this_page;
1939
1940 if (offset <= curr_off) {
1941 /* This block is wholly outside the truncation point */
1942 lock_buffer(bh);
1943 may_free &= journal_unmap_buffer(journal, bh);
1944 unlock_buffer(bh);
1945 }
1946 curr_off = next_off;
1947 bh = next;
1948
1949 } while (bh != head);
1950
1951 if (!offset) {
1952 if (may_free && try_to_free_buffers(page))
1953 J_ASSERT(!page_has_buffers(page));
1954 }
1955}
1956
1957/*
1958 * File a buffer on the given transaction list.
1959 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001960void __jbd2_journal_file_buffer(struct journal_head *jh,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001961 transaction_t *transaction, int jlist)
1962{
1963 struct journal_head **list = NULL;
1964 int was_dirty = 0;
1965 struct buffer_head *bh = jh2bh(jh);
1966
1967 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1968 assert_spin_locked(&transaction->t_journal->j_list_lock);
1969
1970 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1971 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
Mingming Cao40191912008-01-28 23:58:27 -05001972 jh->b_transaction == NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001973
1974 if (jh->b_transaction && jh->b_jlist == jlist)
1975 return;
1976
Dave Kleikamp470decc2006-10-11 01:20:57 -07001977 if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
1978 jlist == BJ_Shadow || jlist == BJ_Forget) {
Jan Karaf91d1d02009-07-13 16:16:20 -04001979 /*
1980 * For metadata buffers, we track dirty bit in buffer_jbddirty
1981 * instead of buffer_dirty. We should not see a dirty bit set
1982 * here because we clear it in do_get_write_access but e.g.
1983 * tune2fs can modify the sb and set the dirty bit at any time
1984 * so we try to gracefully handle that.
1985 */
1986 if (buffer_dirty(bh))
1987 warn_dirty_buffer(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001988 if (test_clear_buffer_dirty(bh) ||
1989 test_clear_buffer_jbddirty(bh))
1990 was_dirty = 1;
1991 }
1992
1993 if (jh->b_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001994 __jbd2_journal_temp_unlink_buffer(jh);
Jan Karade1b7942011-06-13 15:38:22 -04001995 else
1996 jbd2_journal_grab_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001997 jh->b_transaction = transaction;
1998
1999 switch (jlist) {
2000 case BJ_None:
2001 J_ASSERT_JH(jh, !jh->b_committed_data);
2002 J_ASSERT_JH(jh, !jh->b_frozen_data);
2003 return;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002004 case BJ_Metadata:
2005 transaction->t_nr_buffers++;
2006 list = &transaction->t_buffers;
2007 break;
2008 case BJ_Forget:
2009 list = &transaction->t_forget;
2010 break;
2011 case BJ_IO:
2012 list = &transaction->t_iobuf_list;
2013 break;
2014 case BJ_Shadow:
2015 list = &transaction->t_shadow_list;
2016 break;
2017 case BJ_LogCtl:
2018 list = &transaction->t_log_list;
2019 break;
2020 case BJ_Reserved:
2021 list = &transaction->t_reserved_list;
2022 break;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002023 }
2024
2025 __blist_add_buffer(list, jh);
2026 jh->b_jlist = jlist;
2027
2028 if (was_dirty)
2029 set_buffer_jbddirty(bh);
2030}
2031
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002032void jbd2_journal_file_buffer(struct journal_head *jh,
Dave Kleikamp470decc2006-10-11 01:20:57 -07002033 transaction_t *transaction, int jlist)
2034{
2035 jbd_lock_bh_state(jh2bh(jh));
2036 spin_lock(&transaction->t_journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002037 __jbd2_journal_file_buffer(jh, transaction, jlist);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002038 spin_unlock(&transaction->t_journal->j_list_lock);
2039 jbd_unlock_bh_state(jh2bh(jh));
2040}
2041
2042/*
2043 * Remove a buffer from its current buffer list in preparation for
2044 * dropping it from its current transaction entirely. If the buffer has
2045 * already started to be used by a subsequent transaction, refile the
2046 * buffer on that transaction's metadata list.
2047 *
Jan Karade1b7942011-06-13 15:38:22 -04002048 * Called under j_list_lock
Dave Kleikamp470decc2006-10-11 01:20:57 -07002049 * Called under jbd_lock_bh_state(jh2bh(jh))
Jan Karade1b7942011-06-13 15:38:22 -04002050 *
2051 * jh and bh may be already free when this function returns
Dave Kleikamp470decc2006-10-11 01:20:57 -07002052 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002053void __jbd2_journal_refile_buffer(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002054{
dingdinghuaba869022010-02-15 16:35:42 -05002055 int was_dirty, jlist;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002056 struct buffer_head *bh = jh2bh(jh);
2057
2058 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
2059 if (jh->b_transaction)
2060 assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
2061
2062 /* If the buffer is now unused, just drop it. */
2063 if (jh->b_next_transaction == NULL) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002064 __jbd2_journal_unfile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002065 return;
2066 }
2067
2068 /*
2069 * It has been modified by a later transaction: add it to the new
2070 * transaction's metadata list.
2071 */
2072
2073 was_dirty = test_clear_buffer_jbddirty(bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002074 __jbd2_journal_temp_unlink_buffer(jh);
Jan Karade1b7942011-06-13 15:38:22 -04002075 /*
2076 * We set b_transaction here because b_next_transaction will inherit
2077 * our jh reference and thus __jbd2_journal_file_buffer() must not
2078 * take a new one.
2079 */
Dave Kleikamp470decc2006-10-11 01:20:57 -07002080 jh->b_transaction = jh->b_next_transaction;
2081 jh->b_next_transaction = NULL;
dingdinghuaba869022010-02-15 16:35:42 -05002082 if (buffer_freed(bh))
2083 jlist = BJ_Forget;
2084 else if (jh->b_modified)
2085 jlist = BJ_Metadata;
2086 else
2087 jlist = BJ_Reserved;
2088 __jbd2_journal_file_buffer(jh, jh->b_transaction, jlist);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002089 J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
2090
2091 if (was_dirty)
2092 set_buffer_jbddirty(bh);
2093}
2094
2095/*
Jan Karade1b7942011-06-13 15:38:22 -04002096 * __jbd2_journal_refile_buffer() with necessary locking added. We take our
2097 * bh reference so that we can safely unlock bh.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002098 *
Jan Karade1b7942011-06-13 15:38:22 -04002099 * The jh and bh may be freed by this call.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002100 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002101void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002102{
2103 struct buffer_head *bh = jh2bh(jh);
2104
Jan Karade1b7942011-06-13 15:38:22 -04002105 /* Get reference so that buffer cannot be freed before we unlock it */
2106 get_bh(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002107 jbd_lock_bh_state(bh);
2108 spin_lock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002109 __jbd2_journal_refile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002110 jbd_unlock_bh_state(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002111 spin_unlock(&journal->j_list_lock);
2112 __brelse(bh);
2113}
Jan Karac851ed52008-07-11 19:27:31 -04002114
2115/*
2116 * File inode in the inode list of the handle's transaction
2117 */
2118int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode)
2119{
2120 transaction_t *transaction = handle->h_transaction;
2121 journal_t *journal = transaction->t_journal;
2122
2123 if (is_handle_aborted(handle))
2124 return -EIO;
2125
2126 jbd_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino,
2127 transaction->t_tid);
2128
2129 /*
2130 * First check whether inode isn't already on the transaction's
2131 * lists without taking the lock. Note that this check is safe
2132 * without the lock as we cannot race with somebody removing inode
2133 * from the transaction. The reason is that we remove inode from the
2134 * transaction only in journal_release_jbd_inode() and when we commit
2135 * the transaction. We are guarded from the first case by holding
2136 * a reference to the inode. We are safe against the second case
2137 * because if jinode->i_transaction == transaction, commit code
2138 * cannot touch the transaction because we hold reference to it,
2139 * and if jinode->i_next_transaction == transaction, commit code
2140 * will only file the inode where we want it.
2141 */
2142 if (jinode->i_transaction == transaction ||
2143 jinode->i_next_transaction == transaction)
2144 return 0;
2145
2146 spin_lock(&journal->j_list_lock);
2147
2148 if (jinode->i_transaction == transaction ||
2149 jinode->i_next_transaction == transaction)
2150 goto done;
2151
Jan Kara81be12c2011-05-24 11:52:40 -04002152 /*
2153 * We only ever set this variable to 1 so the test is safe. Since
2154 * t_need_data_flush is likely to be set, we do the test to save some
2155 * cacheline bouncing
2156 */
2157 if (!transaction->t_need_data_flush)
2158 transaction->t_need_data_flush = 1;
Jan Karac851ed52008-07-11 19:27:31 -04002159 /* On some different transaction's list - should be
2160 * the committing one */
2161 if (jinode->i_transaction) {
2162 J_ASSERT(jinode->i_next_transaction == NULL);
2163 J_ASSERT(jinode->i_transaction ==
2164 journal->j_committing_transaction);
2165 jinode->i_next_transaction = transaction;
2166 goto done;
2167 }
2168 /* Not on any transaction list... */
2169 J_ASSERT(!jinode->i_next_transaction);
2170 jinode->i_transaction = transaction;
2171 list_add(&jinode->i_list, &transaction->t_inode_list);
2172done:
2173 spin_unlock(&journal->j_list_lock);
2174
2175 return 0;
2176}
2177
2178/*
Jan Kara7f5aa212009-02-10 11:15:34 -05002179 * File truncate and transaction commit interact with each other in a
2180 * non-trivial way. If a transaction writing data block A is
2181 * committing, we cannot discard the data by truncate until we have
2182 * written them. Otherwise if we crashed after the transaction with
2183 * write has committed but before the transaction with truncate has
2184 * committed, we could see stale data in block A. This function is a
2185 * helper to solve this problem. It starts writeout of the truncated
2186 * part in case it is in the committing transaction.
2187 *
2188 * Filesystem code must call this function when inode is journaled in
2189 * ordered mode before truncation happens and after the inode has been
2190 * placed on orphan list with the new inode size. The second condition
2191 * avoids the race that someone writes new data and we start
2192 * committing the transaction after this function has been called but
2193 * before a transaction for truncate is started (and furthermore it
2194 * allows us to optimize the case where the addition to orphan list
2195 * happens in the same transaction as write --- we don't have to write
2196 * any data in such case).
Jan Karac851ed52008-07-11 19:27:31 -04002197 */
Jan Kara7f5aa212009-02-10 11:15:34 -05002198int jbd2_journal_begin_ordered_truncate(journal_t *journal,
2199 struct jbd2_inode *jinode,
Jan Karac851ed52008-07-11 19:27:31 -04002200 loff_t new_size)
2201{
Jan Kara7f5aa212009-02-10 11:15:34 -05002202 transaction_t *inode_trans, *commit_trans;
Jan Karac851ed52008-07-11 19:27:31 -04002203 int ret = 0;
2204
Jan Kara7f5aa212009-02-10 11:15:34 -05002205 /* This is a quick check to avoid locking if not necessary */
2206 if (!jinode->i_transaction)
Jan Karac851ed52008-07-11 19:27:31 -04002207 goto out;
Jan Kara7f5aa212009-02-10 11:15:34 -05002208 /* Locks are here just to force reading of recent values, it is
2209 * enough that the transaction was not committing before we started
2210 * a transaction adding the inode to orphan list */
Theodore Ts'oa931da62010-08-03 21:35:12 -04002211 read_lock(&journal->j_state_lock);
Jan Karac851ed52008-07-11 19:27:31 -04002212 commit_trans = journal->j_committing_transaction;
Theodore Ts'oa931da62010-08-03 21:35:12 -04002213 read_unlock(&journal->j_state_lock);
Jan Kara7f5aa212009-02-10 11:15:34 -05002214 spin_lock(&journal->j_list_lock);
2215 inode_trans = jinode->i_transaction;
2216 spin_unlock(&journal->j_list_lock);
2217 if (inode_trans == commit_trans) {
2218 ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping,
Jan Karac851ed52008-07-11 19:27:31 -04002219 new_size, LLONG_MAX);
2220 if (ret)
2221 jbd2_journal_abort(journal, ret);
2222 }
2223out:
2224 return ret;
2225}