blob: 735609e2d63697437d9624836e3c7fc2d348641c [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>
Randy Dunlap44705752011-10-27 04:05:13 -040030#include <linux/bug.h>
Theodore Ts'o47def822010-07-27 11:56:05 -040031#include <linux/module.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070032
Adrian Bunk7ddae862006-12-06 20:38:27 -080033static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh);
Jan Karade1b7942011-06-13 15:38:22 -040034static void __jbd2_journal_unfile_buffer(struct journal_head *jh);
Adrian Bunk7ddae862006-12-06 20:38:27 -080035
Yongqiang Yang0c2022e2012-02-20 17:53:02 -050036static struct kmem_cache *transaction_cache;
37int __init jbd2_journal_init_transaction_cache(void)
38{
39 J_ASSERT(!transaction_cache);
40 transaction_cache = kmem_cache_create("jbd2_transaction_s",
41 sizeof(transaction_t),
42 0,
43 SLAB_HWCACHE_ALIGN|SLAB_TEMPORARY,
44 NULL);
45 if (transaction_cache)
46 return 0;
47 return -ENOMEM;
48}
49
50void jbd2_journal_destroy_transaction_cache(void)
51{
52 if (transaction_cache) {
53 kmem_cache_destroy(transaction_cache);
54 transaction_cache = NULL;
55 }
56}
57
58void jbd2_journal_free_transaction(transaction_t *transaction)
59{
60 if (unlikely(ZERO_OR_NULL_PTR(transaction)))
61 return;
62 kmem_cache_free(transaction_cache, transaction);
63}
64
Dave Kleikamp470decc2006-10-11 01:20:57 -070065/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -070066 * jbd2_get_transaction: obtain a new transaction_t object.
Dave Kleikamp470decc2006-10-11 01:20:57 -070067 *
68 * Simply allocate and initialise a new transaction. Create it in
69 * RUNNING state and add it to the current journal (which should not
70 * have an existing running transaction: we only make a new transaction
71 * once we have started to commit the old one).
72 *
73 * Preconditions:
74 * The journal MUST be locked. We don't perform atomic mallocs on the
75 * new transaction and we can't block without protecting against other
76 * processes trying to touch the journal while it is in transition.
77 *
Dave Kleikamp470decc2006-10-11 01:20:57 -070078 */
79
80static transaction_t *
Mingming Caof7f4bcc2006-10-11 01:20:59 -070081jbd2_get_transaction(journal_t *journal, transaction_t *transaction)
Dave Kleikamp470decc2006-10-11 01:20:57 -070082{
83 transaction->t_journal = journal;
84 transaction->t_state = T_RUNNING;
Josef Bacike07f7182008-11-26 01:14:26 -050085 transaction->t_start_time = ktime_get();
Dave Kleikamp470decc2006-10-11 01:20:57 -070086 transaction->t_tid = journal->j_transaction_sequence++;
87 transaction->t_expires = jiffies + journal->j_commit_interval;
88 spin_lock_init(&transaction->t_handle_lock);
Theodore Ts'oa51dca92010-08-02 08:43:25 -040089 atomic_set(&transaction->t_updates, 0);
90 atomic_set(&transaction->t_outstanding_credits, 0);
Theodore Ts'o8dd42042010-08-03 21:38:29 -040091 atomic_set(&transaction->t_handle_count, 0);
Jan Karac851ed52008-07-11 19:27:31 -040092 INIT_LIST_HEAD(&transaction->t_inode_list);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -040093 INIT_LIST_HEAD(&transaction->t_private_list);
Dave Kleikamp470decc2006-10-11 01:20:57 -070094
95 /* Set up the commit timer for the new transaction. */
Andreas Dilgerb1f485f2009-08-10 22:51:53 -040096 journal->j_commit_timer.expires = round_jiffies_up(transaction->t_expires);
Dave Kleikamp470decc2006-10-11 01:20:57 -070097 add_timer(&journal->j_commit_timer);
98
99 J_ASSERT(journal->j_running_transaction == NULL);
100 journal->j_running_transaction = transaction;
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500101 transaction->t_max_wait = 0;
102 transaction->t_start = jiffies;
Theodore Ts'o9fff24a2013-02-06 22:30:23 -0500103 transaction->t_requested = 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700104
105 return transaction;
106}
107
108/*
109 * Handle management.
110 *
111 * A handle_t is an object which represents a single atomic update to a
112 * filesystem, and which tracks all of the modifications which form part
113 * of that one update.
114 */
115
116/*
Tao Ma28e35e42011-05-22 21:45:26 -0400117 * Update transaction's maximum wait time, if debugging is enabled.
Theodore Ts'o6d0bf002010-08-09 17:28:38 -0400118 *
119 * In order for t_max_wait to be reliable, it must be protected by a
120 * lock. But doing so will mean that start_this_handle() can not be
121 * run in parallel on SMP systems, which limits our scalability. So
122 * unless debugging is enabled, we no longer update t_max_wait, which
123 * means that maximum wait time reported by the jbd2_run_stats
124 * tracepoint will always be zero.
125 */
Tao Ma28e35e42011-05-22 21:45:26 -0400126static inline void update_t_max_wait(transaction_t *transaction,
127 unsigned long ts)
Theodore Ts'o6d0bf002010-08-09 17:28:38 -0400128{
129#ifdef CONFIG_JBD2_DEBUG
Theodore Ts'o6d0bf002010-08-09 17:28:38 -0400130 if (jbd2_journal_enable_debug &&
131 time_after(transaction->t_start, ts)) {
132 ts = jbd2_time_diff(ts, transaction->t_start);
133 spin_lock(&transaction->t_handle_lock);
134 if (ts > transaction->t_max_wait)
135 transaction->t_max_wait = ts;
136 spin_unlock(&transaction->t_handle_lock);
137 }
138#endif
139}
140
141/*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700142 * start_this_handle: Given a handle, deal with any locking or stalling
143 * needed to make sure that there is enough journal space for the handle
144 * to begin. Attach the handle to a transaction and set up the
145 * transaction's buffer credits.
146 */
147
Theodore Ts'o47def822010-07-27 11:56:05 -0400148static int start_this_handle(journal_t *journal, handle_t *handle,
Dan Carpenterd2159fb2011-09-04 10:20:14 -0400149 gfp_t gfp_mask)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700150{
Theodore Ts'oe4471832011-02-12 08:18:24 -0500151 transaction_t *transaction, *new_transaction = NULL;
152 tid_t tid;
153 int needed, need_to_start;
154 int nblocks = handle->h_buffer_credits;
Tao Ma28e35e42011-05-22 21:45:26 -0400155 unsigned long ts = jiffies;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700156
157 if (nblocks > journal->j_max_transaction_buffers) {
Eryu Guanf2a44522011-11-01 19:09:18 -0400158 printk(KERN_ERR "JBD2: %s wants too many credits (%d > %d)\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -0700159 current->comm, nblocks,
160 journal->j_max_transaction_buffers);
Theodore Ts'o47def822010-07-27 11:56:05 -0400161 return -ENOSPC;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700162 }
163
164alloc_transaction:
165 if (!journal->j_running_transaction) {
Wanlong Gaob2f4edb2012-06-01 00:10:32 -0400166 new_transaction = kmem_cache_zalloc(transaction_cache,
167 gfp_mask);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700168 if (!new_transaction) {
Theodore Ts'o47def822010-07-27 11:56:05 -0400169 /*
170 * If __GFP_FS is not present, then we may be
171 * being called from inside the fs writeback
172 * layer, so we MUST NOT fail. Since
173 * __GFP_NOFAIL is going away, we will arrange
174 * to retry the allocation ourselves.
175 */
176 if ((gfp_mask & __GFP_FS) == 0) {
177 congestion_wait(BLK_RW_ASYNC, HZ/50);
178 goto alloc_transaction;
179 }
180 return -ENOMEM;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700181 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700182 }
183
184 jbd_debug(3, "New handle %p going live.\n", handle);
185
Dave Kleikamp470decc2006-10-11 01:20:57 -0700186 /*
187 * We need to hold j_state_lock until t_updates has been incremented,
188 * for proper journal barrier handling
189 */
Theodore Ts'oa931da62010-08-03 21:35:12 -0400190repeat:
191 read_lock(&journal->j_state_lock);
Theodore Ts'o5c2178e2010-10-27 21:30:04 -0400192 BUG_ON(journal->j_flags & JBD2_UNMOUNT);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700193 if (is_journal_aborted(journal) ||
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700194 (journal->j_errno != 0 && !(journal->j_flags & JBD2_ACK_ERR))) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400195 read_unlock(&journal->j_state_lock);
Yongqiang Yang0c2022e2012-02-20 17:53:02 -0500196 jbd2_journal_free_transaction(new_transaction);
Theodore Ts'o47def822010-07-27 11:56:05 -0400197 return -EROFS;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700198 }
199
200 /* Wait on the journal's transaction barrier if necessary */
201 if (journal->j_barrier_count) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400202 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700203 wait_event(journal->j_wait_transaction_locked,
204 journal->j_barrier_count == 0);
205 goto repeat;
206 }
207
208 if (!journal->j_running_transaction) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400209 read_unlock(&journal->j_state_lock);
210 if (!new_transaction)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700211 goto alloc_transaction;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400212 write_lock(&journal->j_state_lock);
Jan Karad7961c72012-12-21 00:15:51 -0500213 if (!journal->j_running_transaction &&
214 !journal->j_barrier_count) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400215 jbd2_get_transaction(journal, new_transaction);
216 new_transaction = NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700217 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400218 write_unlock(&journal->j_state_lock);
219 goto repeat;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700220 }
221
222 transaction = journal->j_running_transaction;
223
224 /*
225 * If the current transaction is locked down for commit, wait for the
226 * lock to be released.
227 */
228 if (transaction->t_state == T_LOCKED) {
229 DEFINE_WAIT(wait);
230
231 prepare_to_wait(&journal->j_wait_transaction_locked,
232 &wait, TASK_UNINTERRUPTIBLE);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400233 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700234 schedule();
235 finish_wait(&journal->j_wait_transaction_locked, &wait);
236 goto repeat;
237 }
238
239 /*
240 * If there is not enough space left in the log to write all potential
241 * buffers requested by this operation, we need to stall pending a log
242 * checkpoint to free some more log space.
243 */
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400244 needed = atomic_add_return(nblocks,
245 &transaction->t_outstanding_credits);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700246
247 if (needed > journal->j_max_transaction_buffers) {
248 /*
249 * If the current transaction is already too large, then start
250 * to commit it: we can then go back and attach this handle to
251 * a new transaction.
252 */
253 DEFINE_WAIT(wait);
254
255 jbd_debug(2, "Handle %p starting new commit...\n", handle);
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400256 atomic_sub(nblocks, &transaction->t_outstanding_credits);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700257 prepare_to_wait(&journal->j_wait_transaction_locked, &wait,
258 TASK_UNINTERRUPTIBLE);
Theodore Ts'oe4471832011-02-12 08:18:24 -0500259 tid = transaction->t_tid;
260 need_to_start = !tid_geq(journal->j_commit_request, tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400261 read_unlock(&journal->j_state_lock);
Theodore Ts'oe4471832011-02-12 08:18:24 -0500262 if (need_to_start)
263 jbd2_log_start_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700264 schedule();
265 finish_wait(&journal->j_wait_transaction_locked, &wait);
266 goto repeat;
267 }
268
269 /*
270 * The commit code assumes that it can get enough log space
271 * without forcing a checkpoint. This is *critical* for
272 * correctness: a checkpoint of a buffer which is also
273 * associated with a committing transaction creates a deadlock,
274 * so commit simply cannot force through checkpoints.
275 *
276 * We must therefore ensure the necessary space in the journal
277 * *before* starting to dirty potentially checkpointed buffers
278 * in the new transaction.
279 *
280 * The worst part is, any transaction currently committing can
281 * reduce the free space arbitrarily. Be careful to account for
282 * those buffers when checkpointing.
283 */
284
285 /*
286 * @@@ AKPM: This seems rather over-defensive. We're giving commit
287 * a _lot_ of headroom: 1/4 of the journal plus the size of
288 * the committing transaction. Really, we only need to give it
289 * committing_transaction->t_outstanding_credits plus "enough" for
290 * the log control blocks.
Uwe Kleine-Königa34f0b32010-12-10 14:55:42 +0100291 * Also, this test is inconsistent with the matching one in
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700292 * jbd2_journal_extend().
Dave Kleikamp470decc2006-10-11 01:20:57 -0700293 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700294 if (__jbd2_log_space_left(journal) < jbd_space_needed(journal)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700295 jbd_debug(2, "Handle %p waiting for checkpoint...\n", handle);
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400296 atomic_sub(nblocks, &transaction->t_outstanding_credits);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400297 read_unlock(&journal->j_state_lock);
298 write_lock(&journal->j_state_lock);
299 if (__jbd2_log_space_left(journal) < jbd_space_needed(journal))
300 __jbd2_log_wait_for_space(journal);
301 write_unlock(&journal->j_state_lock);
302 goto repeat;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700303 }
304
305 /* OK, account for the buffers that this operation expects to
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400306 * use and add the handle to the running transaction.
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400307 */
Tao Ma28e35e42011-05-22 21:45:26 -0400308 update_t_max_wait(transaction, ts);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700309 handle->h_transaction = transaction;
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400310 atomic_inc(&transaction->t_updates);
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400311 atomic_inc(&transaction->t_handle_count);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700312 jbd_debug(4, "Handle %p given %d credits (total %d, free %d)\n",
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400313 handle, nblocks,
314 atomic_read(&transaction->t_outstanding_credits),
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700315 __jbd2_log_space_left(journal));
Theodore Ts'oa931da62010-08-03 21:35:12 -0400316 read_unlock(&journal->j_state_lock);
Jan Kara9599b0e2009-08-17 21:23:17 -0400317
318 lock_map_acquire(&handle->h_lockdep_map);
Yongqiang Yang0c2022e2012-02-20 17:53:02 -0500319 jbd2_journal_free_transaction(new_transaction);
Theodore Ts'o47def822010-07-27 11:56:05 -0400320 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700321}
322
Mingming Cao7b751062008-01-28 23:58:27 -0500323static struct lock_class_key jbd2_handle_key;
324
Dave Kleikamp470decc2006-10-11 01:20:57 -0700325/* Allocate a new handle. This should probably be in a slab... */
326static handle_t *new_handle(int nblocks)
327{
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400328 handle_t *handle = jbd2_alloc_handle(GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700329 if (!handle)
330 return NULL;
331 memset(handle, 0, sizeof(*handle));
332 handle->h_buffer_credits = nblocks;
333 handle->h_ref = 1;
334
Mingming Cao7b751062008-01-28 23:58:27 -0500335 lockdep_init_map(&handle->h_lockdep_map, "jbd2_handle",
336 &jbd2_handle_key, 0);
337
Dave Kleikamp470decc2006-10-11 01:20:57 -0700338 return handle;
339}
340
341/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700342 * handle_t *jbd2_journal_start() - Obtain a new handle.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700343 * @journal: Journal to start transaction on.
344 * @nblocks: number of block buffer we might modify
345 *
346 * We make sure that the transaction can guarantee at least nblocks of
347 * modified buffers in the log. We block until the log can guarantee
348 * that much space.
349 *
350 * This function is visible to journal users (like ext3fs), so is not
351 * called with the journal already locked.
352 *
Eryu Guanc8675162011-05-24 17:09:58 -0400353 * Return a pointer to a newly allocated handle, or an ERR_PTR() value
354 * on failure.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700355 */
Dan Carpenterd2159fb2011-09-04 10:20:14 -0400356handle_t *jbd2__journal_start(journal_t *journal, int nblocks, gfp_t gfp_mask)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700357{
358 handle_t *handle = journal_current_handle();
359 int err;
360
361 if (!journal)
362 return ERR_PTR(-EROFS);
363
364 if (handle) {
365 J_ASSERT(handle->h_transaction->t_journal == journal);
366 handle->h_ref++;
367 return handle;
368 }
369
370 handle = new_handle(nblocks);
371 if (!handle)
372 return ERR_PTR(-ENOMEM);
373
374 current->journal_info = handle;
375
Theodore Ts'o47def822010-07-27 11:56:05 -0400376 err = start_this_handle(journal, handle, gfp_mask);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700377 if (err < 0) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400378 jbd2_free_handle(handle);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700379 current->journal_info = NULL;
380 handle = ERR_PTR(err);
381 }
382 return handle;
383}
Theodore Ts'o47def822010-07-27 11:56:05 -0400384EXPORT_SYMBOL(jbd2__journal_start);
385
386
387handle_t *jbd2_journal_start(journal_t *journal, int nblocks)
388{
389 return jbd2__journal_start(journal, nblocks, GFP_NOFS);
390}
391EXPORT_SYMBOL(jbd2_journal_start);
392
Dave Kleikamp470decc2006-10-11 01:20:57 -0700393
394/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700395 * int jbd2_journal_extend() - extend buffer credits.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700396 * @handle: handle to 'extend'
397 * @nblocks: nr blocks to try to extend by.
398 *
399 * Some transactions, such as large extends and truncates, can be done
400 * atomically all at once or in several stages. The operation requests
401 * a credit for a number of buffer modications in advance, but can
402 * extend its credit if it needs more.
403 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700404 * jbd2_journal_extend tries to give the running handle more buffer credits.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700405 * It does not guarantee that allocation - this is a best-effort only.
406 * The calling process MUST be able to deal cleanly with a failure to
407 * extend here.
408 *
409 * Return 0 on success, non-zero on failure.
410 *
411 * return code < 0 implies an error
412 * return code > 0 implies normal transaction-full status.
413 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700414int jbd2_journal_extend(handle_t *handle, int nblocks)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700415{
416 transaction_t *transaction = handle->h_transaction;
417 journal_t *journal = transaction->t_journal;
418 int result;
419 int wanted;
420
421 result = -EIO;
422 if (is_handle_aborted(handle))
423 goto out;
424
425 result = 1;
426
Theodore Ts'oa931da62010-08-03 21:35:12 -0400427 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700428
429 /* Don't extend a locked-down transaction! */
430 if (handle->h_transaction->t_state != T_RUNNING) {
431 jbd_debug(3, "denied handle %p %d blocks: "
432 "transaction not running\n", handle, nblocks);
433 goto error_out;
434 }
435
436 spin_lock(&transaction->t_handle_lock);
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400437 wanted = atomic_read(&transaction->t_outstanding_credits) + nblocks;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700438
439 if (wanted > journal->j_max_transaction_buffers) {
440 jbd_debug(3, "denied handle %p %d blocks: "
441 "transaction too large\n", handle, nblocks);
442 goto unlock;
443 }
444
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700445 if (wanted > __jbd2_log_space_left(journal)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700446 jbd_debug(3, "denied handle %p %d blocks: "
447 "insufficient log space\n", handle, nblocks);
448 goto unlock;
449 }
450
451 handle->h_buffer_credits += nblocks;
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400452 atomic_add(nblocks, &transaction->t_outstanding_credits);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700453 result = 0;
454
455 jbd_debug(3, "extended handle %p by %d\n", handle, nblocks);
456unlock:
457 spin_unlock(&transaction->t_handle_lock);
458error_out:
Theodore Ts'oa931da62010-08-03 21:35:12 -0400459 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700460out:
461 return result;
462}
463
464
465/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700466 * int jbd2_journal_restart() - restart a handle .
Dave Kleikamp470decc2006-10-11 01:20:57 -0700467 * @handle: handle to restart
468 * @nblocks: nr credits requested
469 *
470 * Restart a handle for a multi-transaction filesystem
471 * operation.
472 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700473 * If the jbd2_journal_extend() call above fails to grant new buffer credits
474 * to a running handle, a call to jbd2_journal_restart will commit the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700475 * handle's transaction so far and reattach the handle to a new
476 * transaction capabable of guaranteeing the requested number of
477 * credits.
478 */
Dan Carpenterd2159fb2011-09-04 10:20:14 -0400479int jbd2__journal_restart(handle_t *handle, int nblocks, gfp_t gfp_mask)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700480{
481 transaction_t *transaction = handle->h_transaction;
482 journal_t *journal = transaction->t_journal;
Theodore Ts'oe4471832011-02-12 08:18:24 -0500483 tid_t tid;
484 int need_to_start, ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700485
486 /* If we've had an abort of any type, don't even think about
487 * actually doing the restart! */
488 if (is_handle_aborted(handle))
489 return 0;
490
491 /*
492 * First unlink the handle from its current transaction, and start the
493 * commit on that.
494 */
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400495 J_ASSERT(atomic_read(&transaction->t_updates) > 0);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700496 J_ASSERT(journal_current_handle() == handle);
497
Theodore Ts'oa931da62010-08-03 21:35:12 -0400498 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700499 spin_lock(&transaction->t_handle_lock);
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400500 atomic_sub(handle->h_buffer_credits,
501 &transaction->t_outstanding_credits);
502 if (atomic_dec_and_test(&transaction->t_updates))
Dave Kleikamp470decc2006-10-11 01:20:57 -0700503 wake_up(&journal->j_wait_updates);
504 spin_unlock(&transaction->t_handle_lock);
505
506 jbd_debug(2, "restarting handle %p\n", handle);
Theodore Ts'oe4471832011-02-12 08:18:24 -0500507 tid = transaction->t_tid;
508 need_to_start = !tid_geq(journal->j_commit_request, tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400509 read_unlock(&journal->j_state_lock);
Theodore Ts'oe4471832011-02-12 08:18:24 -0500510 if (need_to_start)
511 jbd2_log_start_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700512
Jan Kara9599b0e2009-08-17 21:23:17 -0400513 lock_map_release(&handle->h_lockdep_map);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700514 handle->h_buffer_credits = nblocks;
Theodore Ts'o47def822010-07-27 11:56:05 -0400515 ret = start_this_handle(journal, handle, gfp_mask);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700516 return ret;
517}
Theodore Ts'o47def822010-07-27 11:56:05 -0400518EXPORT_SYMBOL(jbd2__journal_restart);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700519
520
Theodore Ts'o47def822010-07-27 11:56:05 -0400521int jbd2_journal_restart(handle_t *handle, int nblocks)
522{
523 return jbd2__journal_restart(handle, nblocks, GFP_NOFS);
524}
525EXPORT_SYMBOL(jbd2_journal_restart);
526
Dave Kleikamp470decc2006-10-11 01:20:57 -0700527/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700528 * void jbd2_journal_lock_updates () - establish a transaction barrier.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700529 * @journal: Journal to establish a barrier on.
530 *
531 * This locks out any further updates from being started, and blocks
532 * until all existing updates have completed, returning only once the
533 * journal is in a quiescent state with no updates running.
534 *
535 * The journal lock should not be held on entry.
536 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700537void jbd2_journal_lock_updates(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700538{
539 DEFINE_WAIT(wait);
540
Theodore Ts'oa931da62010-08-03 21:35:12 -0400541 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700542 ++journal->j_barrier_count;
543
544 /* Wait until there are no running updates */
545 while (1) {
546 transaction_t *transaction = journal->j_running_transaction;
547
548 if (!transaction)
549 break;
550
551 spin_lock(&transaction->t_handle_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700552 prepare_to_wait(&journal->j_wait_updates, &wait,
553 TASK_UNINTERRUPTIBLE);
Jan Kara9837d8e2012-01-04 22:03:11 -0500554 if (!atomic_read(&transaction->t_updates)) {
555 spin_unlock(&transaction->t_handle_lock);
556 finish_wait(&journal->j_wait_updates, &wait);
557 break;
558 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700559 spin_unlock(&transaction->t_handle_lock);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400560 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700561 schedule();
562 finish_wait(&journal->j_wait_updates, &wait);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400563 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700564 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400565 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700566
567 /*
568 * We have now established a barrier against other normal updates, but
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700569 * we also need to barrier against other jbd2_journal_lock_updates() calls
Dave Kleikamp470decc2006-10-11 01:20:57 -0700570 * to make sure that we serialise special journal-locked operations
571 * too.
572 */
573 mutex_lock(&journal->j_barrier);
574}
575
576/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700577 * void jbd2_journal_unlock_updates (journal_t* journal) - release barrier
Dave Kleikamp470decc2006-10-11 01:20:57 -0700578 * @journal: Journal to release the barrier on.
579 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700580 * Release a transaction barrier obtained with jbd2_journal_lock_updates().
Dave Kleikamp470decc2006-10-11 01:20:57 -0700581 *
582 * Should be called without the journal lock held.
583 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700584void jbd2_journal_unlock_updates (journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700585{
586 J_ASSERT(journal->j_barrier_count != 0);
587
588 mutex_unlock(&journal->j_barrier);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400589 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700590 --journal->j_barrier_count;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400591 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700592 wake_up(&journal->j_wait_transaction_locked);
593}
594
Jan Karaf91d1d02009-07-13 16:16:20 -0400595static void warn_dirty_buffer(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700596{
Jan Karaf91d1d02009-07-13 16:16:20 -0400597 char b[BDEVNAME_SIZE];
Dave Kleikamp470decc2006-10-11 01:20:57 -0700598
Jan Karaf91d1d02009-07-13 16:16:20 -0400599 printk(KERN_WARNING
Eryu Guanf2a44522011-11-01 19:09:18 -0400600 "JBD2: Spotted dirty metadata buffer (dev = %s, blocknr = %llu). "
Jan Karaf91d1d02009-07-13 16:16:20 -0400601 "There's a risk of filesystem corruption in case of system "
602 "crash.\n",
603 bdevname(bh->b_bdev, b), (unsigned long long)bh->b_blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700604}
605
606/*
607 * If the buffer is already part of the current transaction, then there
608 * is nothing we need to do. If it is already part of a prior
609 * transaction which we are still committing to disk, then we need to
610 * make sure that we do not overwrite the old copy: we do copy-out to
611 * preserve the copy going to disk. We also account the buffer against
612 * the handle's metadata buffer credits (unless the buffer is already
613 * part of the transaction, that is).
614 *
615 */
616static int
617do_get_write_access(handle_t *handle, struct journal_head *jh,
618 int force_copy)
619{
620 struct buffer_head *bh;
621 transaction_t *transaction;
622 journal_t *journal;
623 int error;
624 char *frozen_buffer = NULL;
625 int need_copy = 0;
626
627 if (is_handle_aborted(handle))
628 return -EROFS;
629
630 transaction = handle->h_transaction;
631 journal = transaction->t_journal;
632
Theodore Ts'ocfef2c62010-12-18 13:07:34 -0500633 jbd_debug(5, "journal_head %p, force_copy %d\n", jh, force_copy);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700634
635 JBUFFER_TRACE(jh, "entry");
636repeat:
637 bh = jh2bh(jh);
638
639 /* @@@ Need to check for errors here at some point. */
640
641 lock_buffer(bh);
642 jbd_lock_bh_state(bh);
643
644 /* We now hold the buffer lock so it is safe to query the buffer
645 * state. Is the buffer dirty?
646 *
647 * If so, there are two possibilities. The buffer may be
648 * non-journaled, and undergoing a quite legitimate writeback.
649 * Otherwise, it is journaled, and we don't expect dirty buffers
650 * in that state (the buffers should be marked JBD_Dirty
651 * instead.) So either the IO is being done under our own
652 * control and this is a bug, or it's a third party IO such as
653 * dump(8) (which may leave the buffer scheduled for read ---
654 * ie. locked but not dirty) or tune2fs (which may actually have
655 * the buffer dirtied, ugh.) */
656
657 if (buffer_dirty(bh)) {
658 /*
659 * First question: is this buffer already part of the current
660 * transaction or the existing committing transaction?
661 */
662 if (jh->b_transaction) {
663 J_ASSERT_JH(jh,
664 jh->b_transaction == transaction ||
665 jh->b_transaction ==
666 journal->j_committing_transaction);
667 if (jh->b_next_transaction)
668 J_ASSERT_JH(jh, jh->b_next_transaction ==
669 transaction);
Jan Karaf91d1d02009-07-13 16:16:20 -0400670 warn_dirty_buffer(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700671 }
672 /*
673 * In any case we need to clean the dirty flag and we must
674 * do it under the buffer lock to be sure we don't race
675 * with running write-out.
676 */
Jan Karaf91d1d02009-07-13 16:16:20 -0400677 JBUFFER_TRACE(jh, "Journalling dirty buffer");
678 clear_buffer_dirty(bh);
679 set_buffer_jbddirty(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700680 }
681
682 unlock_buffer(bh);
683
684 error = -EROFS;
685 if (is_handle_aborted(handle)) {
686 jbd_unlock_bh_state(bh);
687 goto out;
688 }
689 error = 0;
690
691 /*
692 * The buffer is already part of this transaction if b_transaction or
693 * b_next_transaction points to it
694 */
695 if (jh->b_transaction == transaction ||
696 jh->b_next_transaction == transaction)
697 goto done;
698
699 /*
Josef Bacik9fc7c632008-04-17 10:38:59 -0400700 * this is the first time this transaction is touching this buffer,
701 * reset the modified flag
702 */
703 jh->b_modified = 0;
704
705 /*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700706 * If there is already a copy-out version of this buffer, then we don't
707 * need to make another one
708 */
709 if (jh->b_frozen_data) {
710 JBUFFER_TRACE(jh, "has frozen data");
711 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
712 jh->b_next_transaction = transaction;
713 goto done;
714 }
715
716 /* Is there data here we need to preserve? */
717
718 if (jh->b_transaction && jh->b_transaction != transaction) {
719 JBUFFER_TRACE(jh, "owned by older transaction");
720 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
721 J_ASSERT_JH(jh, jh->b_transaction ==
722 journal->j_committing_transaction);
723
724 /* There is one case we have to be very careful about.
725 * If the committing transaction is currently writing
726 * this buffer out to disk and has NOT made a copy-out,
727 * then we cannot modify the buffer contents at all
728 * right now. The essence of copy-out is that it is the
729 * extra copy, not the primary copy, which gets
730 * journaled. If the primary copy is already going to
731 * disk then we cannot do copy-out here. */
732
733 if (jh->b_jlist == BJ_Shadow) {
734 DEFINE_WAIT_BIT(wait, &bh->b_state, BH_Unshadow);
735 wait_queue_head_t *wqh;
736
737 wqh = bit_waitqueue(&bh->b_state, BH_Unshadow);
738
739 JBUFFER_TRACE(jh, "on shadow: sleep");
740 jbd_unlock_bh_state(bh);
741 /* commit wakes up all shadow buffers after IO */
742 for ( ; ; ) {
743 prepare_to_wait(wqh, &wait.wait,
744 TASK_UNINTERRUPTIBLE);
745 if (jh->b_jlist != BJ_Shadow)
746 break;
747 schedule();
748 }
749 finish_wait(wqh, &wait.wait);
750 goto repeat;
751 }
752
753 /* Only do the copy if the currently-owning transaction
754 * still needs it. If it is on the Forget list, the
755 * committing transaction is past that stage. The
756 * buffer had better remain locked during the kmalloc,
757 * but that should be true --- we hold the journal lock
758 * still and the buffer is already on the BUF_JOURNAL
759 * list so won't be flushed.
760 *
761 * Subtle point, though: if this is a get_undo_access,
762 * then we will be relying on the frozen_data to contain
763 * the new value of the committed_data record after the
764 * transaction, so we HAVE to force the frozen_data copy
765 * in that case. */
766
767 if (jh->b_jlist != BJ_Forget || force_copy) {
768 JBUFFER_TRACE(jh, "generate frozen data");
769 if (!frozen_buffer) {
770 JBUFFER_TRACE(jh, "allocate memory for buffer");
771 jbd_unlock_bh_state(bh);
772 frozen_buffer =
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400773 jbd2_alloc(jh2bh(jh)->b_size,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700774 GFP_NOFS);
775 if (!frozen_buffer) {
776 printk(KERN_EMERG
777 "%s: OOM for frozen_buffer\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400778 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700779 JBUFFER_TRACE(jh, "oom!");
780 error = -ENOMEM;
781 jbd_lock_bh_state(bh);
782 goto done;
783 }
784 goto repeat;
785 }
786 jh->b_frozen_data = frozen_buffer;
787 frozen_buffer = NULL;
788 need_copy = 1;
789 }
790 jh->b_next_transaction = transaction;
791 }
792
793
794 /*
795 * Finally, if the buffer is not journaled right now, we need to make
796 * sure it doesn't get written to disk before the caller actually
797 * commits the new data
798 */
799 if (!jh->b_transaction) {
800 JBUFFER_TRACE(jh, "no transaction");
801 J_ASSERT_JH(jh, !jh->b_next_transaction);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700802 JBUFFER_TRACE(jh, "file as BJ_Reserved");
803 spin_lock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700804 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700805 spin_unlock(&journal->j_list_lock);
806 }
807
808done:
809 if (need_copy) {
810 struct page *page;
811 int offset;
812 char *source;
813
814 J_EXPECT_JH(jh, buffer_uptodate(jh2bh(jh)),
815 "Possible IO failure.\n");
816 page = jh2bh(jh)->b_page;
Theodore Ts'oa1dd5332010-12-18 13:13:40 -0500817 offset = offset_in_page(jh2bh(jh)->b_data);
Cong Wang303a8f22011-11-25 23:14:31 +0800818 source = kmap_atomic(page);
Jan Kara13ceef02010-07-14 07:56:33 +0200819 /* Fire data frozen trigger just before we copy the data */
820 jbd2_buffer_frozen_trigger(jh, source + offset,
821 jh->b_triggers);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700822 memcpy(jh->b_frozen_data, source+offset, jh2bh(jh)->b_size);
Cong Wang303a8f22011-11-25 23:14:31 +0800823 kunmap_atomic(source);
Joel Beckere06c8222008-09-11 15:35:47 -0700824
825 /*
826 * Now that the frozen data is saved off, we need to store
827 * any matching triggers.
828 */
829 jh->b_frozen_triggers = jh->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700830 }
831 jbd_unlock_bh_state(bh);
832
833 /*
834 * If we are about to journal a buffer, then any revoke pending on it is
835 * no longer valid
836 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700837 jbd2_journal_cancel_revoke(handle, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700838
839out:
840 if (unlikely(frozen_buffer)) /* It's usually NULL */
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400841 jbd2_free(frozen_buffer, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700842
843 JBUFFER_TRACE(jh, "exit");
844 return error;
845}
846
847/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700848 * int jbd2_journal_get_write_access() - notify intent to modify a buffer for metadata (not data) update.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700849 * @handle: transaction to add buffer modifications to
850 * @bh: bh to be used for metadata writes
Dave Kleikamp470decc2006-10-11 01:20:57 -0700851 *
852 * Returns an error code or 0 on success.
853 *
854 * In full data journalling mode the buffer may be of type BJ_AsyncData,
855 * because we're write()ing a buffer which is also part of a shared mapping.
856 */
857
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700858int jbd2_journal_get_write_access(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700859{
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700860 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700861 int rc;
862
863 /* We do not want to get caught playing with fields which the
864 * log thread also manipulates. Make sure that the buffer
865 * completes any outstanding IO before proceeding. */
866 rc = do_get_write_access(handle, jh, 0);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700867 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700868 return rc;
869}
870
871
872/*
873 * When the user wants to journal a newly created buffer_head
874 * (ie. getblk() returned a new buffer and we are going to populate it
875 * manually rather than reading off disk), then we need to keep the
876 * buffer_head locked until it has been completely filled with new
877 * data. In this case, we should be able to make the assertion that
878 * the bh is not already part of an existing transaction.
879 *
880 * The buffer should already be locked by the caller by this point.
881 * There is no lock ranking violation: it was a newly created,
882 * unlocked buffer beforehand. */
883
884/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700885 * int jbd2_journal_get_create_access () - notify intent to use newly created bh
Dave Kleikamp470decc2006-10-11 01:20:57 -0700886 * @handle: transaction to new buffer to
887 * @bh: new buffer.
888 *
889 * Call this if you create a new bh.
890 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700891int jbd2_journal_get_create_access(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700892{
893 transaction_t *transaction = handle->h_transaction;
894 journal_t *journal = transaction->t_journal;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700895 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700896 int err;
897
898 jbd_debug(5, "journal_head %p\n", jh);
899 err = -EROFS;
900 if (is_handle_aborted(handle))
901 goto out;
902 err = 0;
903
904 JBUFFER_TRACE(jh, "entry");
905 /*
906 * The buffer may already belong to this transaction due to pre-zeroing
907 * in the filesystem's new_block code. It may also be on the previous,
908 * committing transaction's lists, but it HAS to be in Forget state in
909 * that case: the transaction must have deleted the buffer for it to be
910 * reused here.
911 */
912 jbd_lock_bh_state(bh);
913 spin_lock(&journal->j_list_lock);
914 J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
915 jh->b_transaction == NULL ||
916 (jh->b_transaction == journal->j_committing_transaction &&
917 jh->b_jlist == BJ_Forget)));
918
919 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
920 J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
921
922 if (jh->b_transaction == NULL) {
Jan Karaf91d1d02009-07-13 16:16:20 -0400923 /*
924 * Previous jbd2_journal_forget() could have left the buffer
925 * with jbddirty bit set because it was being committed. When
926 * the commit finished, we've filed the buffer for
927 * checkpointing and marked it dirty. Now we are reallocating
928 * the buffer so the transaction freeing it must have
929 * committed and so it's safe to clear the dirty bit.
930 */
931 clear_buffer_dirty(jh2bh(jh));
Josef Bacik9fc7c632008-04-17 10:38:59 -0400932 /* first access by this transaction */
933 jh->b_modified = 0;
934
Dave Kleikamp470decc2006-10-11 01:20:57 -0700935 JBUFFER_TRACE(jh, "file as BJ_Reserved");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700936 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700937 } else if (jh->b_transaction == journal->j_committing_transaction) {
Josef Bacik9fc7c632008-04-17 10:38:59 -0400938 /* first access by this transaction */
939 jh->b_modified = 0;
940
Dave Kleikamp470decc2006-10-11 01:20:57 -0700941 JBUFFER_TRACE(jh, "set next transaction");
942 jh->b_next_transaction = transaction;
943 }
944 spin_unlock(&journal->j_list_lock);
945 jbd_unlock_bh_state(bh);
946
947 /*
948 * akpm: I added this. ext3_alloc_branch can pick up new indirect
949 * blocks which contain freed but then revoked metadata. We need
950 * to cancel the revoke in case we end up freeing it yet again
951 * and the reallocating as data - this would cause a second revoke,
952 * which hits an assertion error.
953 */
954 JBUFFER_TRACE(jh, "cancelling revoke");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700955 jbd2_journal_cancel_revoke(handle, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700956out:
Ding Dinghua3991b402011-05-25 17:43:48 -0400957 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700958 return err;
959}
960
961/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700962 * int jbd2_journal_get_undo_access() - Notify intent to modify metadata with
Dave Kleikamp470decc2006-10-11 01:20:57 -0700963 * non-rewindable consequences
964 * @handle: transaction
965 * @bh: buffer to undo
Dave Kleikamp470decc2006-10-11 01:20:57 -0700966 *
967 * Sometimes there is a need to distinguish between metadata which has
968 * been committed to disk and that which has not. The ext3fs code uses
969 * this for freeing and allocating space, we have to make sure that we
970 * do not reuse freed space until the deallocation has been committed,
971 * since if we overwrote that space we would make the delete
972 * un-rewindable in case of a crash.
973 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700974 * To deal with that, jbd2_journal_get_undo_access requests write access to a
Dave Kleikamp470decc2006-10-11 01:20:57 -0700975 * buffer for parts of non-rewindable operations such as delete
976 * operations on the bitmaps. The journaling code must keep a copy of
977 * the buffer's contents prior to the undo_access call until such time
978 * as we know that the buffer has definitely been committed to disk.
979 *
980 * We never need to know which transaction the committed data is part
981 * of, buffers touched here are guaranteed to be dirtied later and so
982 * will be committed to a new transaction in due course, at which point
983 * we can discard the old committed data pointer.
984 *
985 * Returns error number or 0 on success.
986 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700987int jbd2_journal_get_undo_access(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700988{
989 int err;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700990 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700991 char *committed_data = NULL;
992
993 JBUFFER_TRACE(jh, "entry");
994
995 /*
996 * Do this first --- it can drop the journal lock, so we want to
997 * make sure that obtaining the committed_data is done
998 * atomically wrt. completion of any outstanding commits.
999 */
1000 err = do_get_write_access(handle, jh, 1);
1001 if (err)
1002 goto out;
1003
1004repeat:
1005 if (!jh->b_committed_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -04001006 committed_data = jbd2_alloc(jh2bh(jh)->b_size, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001007 if (!committed_data) {
1008 printk(KERN_EMERG "%s: No memory for committed data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001009 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001010 err = -ENOMEM;
1011 goto out;
1012 }
1013 }
1014
1015 jbd_lock_bh_state(bh);
1016 if (!jh->b_committed_data) {
1017 /* Copy out the current buffer contents into the
1018 * preserved, committed copy. */
1019 JBUFFER_TRACE(jh, "generate b_committed data");
1020 if (!committed_data) {
1021 jbd_unlock_bh_state(bh);
1022 goto repeat;
1023 }
1024
1025 jh->b_committed_data = committed_data;
1026 committed_data = NULL;
1027 memcpy(jh->b_committed_data, bh->b_data, bh->b_size);
1028 }
1029 jbd_unlock_bh_state(bh);
1030out:
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001031 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001032 if (unlikely(committed_data))
Mingming Caoaf1e76d2007-10-16 18:38:25 -04001033 jbd2_free(committed_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001034 return err;
1035}
1036
1037/**
Joel Beckere06c8222008-09-11 15:35:47 -07001038 * void jbd2_journal_set_triggers() - Add triggers for commit writeout
1039 * @bh: buffer to trigger on
1040 * @type: struct jbd2_buffer_trigger_type containing the trigger(s).
1041 *
1042 * Set any triggers on this journal_head. This is always safe, because
1043 * triggers for a committing buffer will be saved off, and triggers for
1044 * a running transaction will match the buffer in that transaction.
1045 *
1046 * Call with NULL to clear the triggers.
1047 */
1048void jbd2_journal_set_triggers(struct buffer_head *bh,
1049 struct jbd2_buffer_trigger_type *type)
1050{
1051 struct journal_head *jh = bh2jh(bh);
1052
1053 jh->b_triggers = type;
1054}
1055
Jan Kara13ceef02010-07-14 07:56:33 +02001056void jbd2_buffer_frozen_trigger(struct journal_head *jh, void *mapped_data,
Joel Beckere06c8222008-09-11 15:35:47 -07001057 struct jbd2_buffer_trigger_type *triggers)
1058{
1059 struct buffer_head *bh = jh2bh(jh);
1060
Jan Kara13ceef02010-07-14 07:56:33 +02001061 if (!triggers || !triggers->t_frozen)
Joel Beckere06c8222008-09-11 15:35:47 -07001062 return;
1063
Jan Kara13ceef02010-07-14 07:56:33 +02001064 triggers->t_frozen(triggers, bh, mapped_data, bh->b_size);
Joel Beckere06c8222008-09-11 15:35:47 -07001065}
1066
1067void jbd2_buffer_abort_trigger(struct journal_head *jh,
1068 struct jbd2_buffer_trigger_type *triggers)
1069{
1070 if (!triggers || !triggers->t_abort)
1071 return;
1072
1073 triggers->t_abort(triggers, jh2bh(jh));
1074}
1075
1076
1077
1078/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001079 * int jbd2_journal_dirty_metadata() - mark a buffer as containing dirty metadata
Dave Kleikamp470decc2006-10-11 01:20:57 -07001080 * @handle: transaction to add buffer to.
1081 * @bh: buffer to mark
1082 *
1083 * mark dirty metadata which needs to be journaled as part of the current
1084 * transaction.
1085 *
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -04001086 * The buffer must have previously had jbd2_journal_get_write_access()
1087 * called so that it has a valid journal_head attached to the buffer
1088 * head.
1089 *
Dave Kleikamp470decc2006-10-11 01:20:57 -07001090 * The buffer is placed on the transaction's metadata list and is marked
1091 * as belonging to the transaction.
1092 *
1093 * Returns error number or 0 on success.
1094 *
1095 * Special care needs to be taken if the buffer already belongs to the
1096 * current committing transaction (in which case we should have frozen
1097 * data present for that commit). In that case, we don't relink the
1098 * buffer: that only gets done when the old transaction finally
1099 * completes its commit.
1100 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001101int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001102{
1103 transaction_t *transaction = handle->h_transaction;
1104 journal_t *journal = transaction->t_journal;
1105 struct journal_head *jh = bh2jh(bh);
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -04001106 int ret = 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001107
1108 jbd_debug(5, "journal_head %p\n", jh);
1109 JBUFFER_TRACE(jh, "entry");
1110 if (is_handle_aborted(handle))
1111 goto out;
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -04001112 if (!buffer_jbd(bh)) {
1113 ret = -EUCLEAN;
1114 goto out;
1115 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001116
1117 jbd_lock_bh_state(bh);
1118
1119 if (jh->b_modified == 0) {
1120 /*
1121 * This buffer's got modified and becoming part
1122 * of the transaction. This needs to be done
1123 * once a transaction -bzzz
1124 */
1125 jh->b_modified = 1;
1126 J_ASSERT_JH(jh, handle->h_buffer_credits > 0);
1127 handle->h_buffer_credits--;
1128 }
1129
1130 /*
1131 * fastpath, to avoid expensive locking. If this buffer is already
1132 * on the running transaction's metadata list there is nothing to do.
1133 * Nobody can take it off again because there is a handle open.
1134 * I _think_ we're OK here with SMP barriers - a mistaken decision will
1135 * result in this test being false, so we go in and take the locks.
1136 */
1137 if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) {
1138 JBUFFER_TRACE(jh, "fastpath");
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -04001139 if (unlikely(jh->b_transaction !=
1140 journal->j_running_transaction)) {
1141 printk(KERN_EMERG "JBD: %s: "
1142 "jh->b_transaction (%llu, %p, %u) != "
1143 "journal->j_running_transaction (%p, %u)",
1144 journal->j_devname,
1145 (unsigned long long) bh->b_blocknr,
1146 jh->b_transaction,
1147 jh->b_transaction ? jh->b_transaction->t_tid : 0,
1148 journal->j_running_transaction,
1149 journal->j_running_transaction ?
1150 journal->j_running_transaction->t_tid : 0);
1151 ret = -EINVAL;
1152 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001153 goto out_unlock_bh;
1154 }
1155
1156 set_buffer_jbddirty(bh);
1157
1158 /*
1159 * Metadata already on the current transaction list doesn't
1160 * need to be filed. Metadata on another transaction's list must
1161 * be committing, and will be refiled once the commit completes:
1162 * leave it alone for now.
1163 */
1164 if (jh->b_transaction != transaction) {
1165 JBUFFER_TRACE(jh, "already on other transaction");
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -04001166 if (unlikely(jh->b_transaction !=
1167 journal->j_committing_transaction)) {
1168 printk(KERN_EMERG "JBD: %s: "
1169 "jh->b_transaction (%llu, %p, %u) != "
1170 "journal->j_committing_transaction (%p, %u)",
1171 journal->j_devname,
1172 (unsigned long long) bh->b_blocknr,
1173 jh->b_transaction,
1174 jh->b_transaction ? jh->b_transaction->t_tid : 0,
1175 journal->j_committing_transaction,
1176 journal->j_committing_transaction ?
1177 journal->j_committing_transaction->t_tid : 0);
1178 ret = -EINVAL;
1179 }
1180 if (unlikely(jh->b_next_transaction != transaction)) {
1181 printk(KERN_EMERG "JBD: %s: "
1182 "jh->b_next_transaction (%llu, %p, %u) != "
1183 "transaction (%p, %u)",
1184 journal->j_devname,
1185 (unsigned long long) bh->b_blocknr,
1186 jh->b_next_transaction,
1187 jh->b_next_transaction ?
1188 jh->b_next_transaction->t_tid : 0,
1189 transaction, transaction->t_tid);
1190 ret = -EINVAL;
1191 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001192 /* And this case is illegal: we can't reuse another
1193 * transaction's data buffer, ever. */
1194 goto out_unlock_bh;
1195 }
1196
1197 /* That test should have eliminated the following case: */
Mingming Cao40191912008-01-28 23:58:27 -05001198 J_ASSERT_JH(jh, jh->b_frozen_data == NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001199
1200 JBUFFER_TRACE(jh, "file as BJ_Metadata");
1201 spin_lock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001202 __jbd2_journal_file_buffer(jh, handle->h_transaction, BJ_Metadata);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001203 spin_unlock(&journal->j_list_lock);
1204out_unlock_bh:
1205 jbd_unlock_bh_state(bh);
1206out:
1207 JBUFFER_TRACE(jh, "exit");
Randy Dunlap44705752011-10-27 04:05:13 -04001208 WARN_ON(ret); /* All errors are bugs, so dump the stack */
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -04001209 return ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001210}
1211
Dave Kleikamp470decc2006-10-11 01:20:57 -07001212/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001213 * void jbd2_journal_forget() - bforget() for potentially-journaled buffers.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001214 * @handle: transaction handle
1215 * @bh: bh to 'forget'
1216 *
1217 * We can only do the bforget if there are no commits pending against the
1218 * buffer. If the buffer is dirty in the current running transaction we
1219 * can safely unlink it.
1220 *
1221 * bh may not be a journalled buffer at all - it may be a non-JBD
1222 * buffer which came off the hashtable. Check for this.
1223 *
1224 * Decrements bh->b_count by one.
1225 *
1226 * Allow this call even if the handle has aborted --- it may be part of
1227 * the caller's cleanup after an abort.
1228 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001229int jbd2_journal_forget (handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001230{
1231 transaction_t *transaction = handle->h_transaction;
1232 journal_t *journal = transaction->t_journal;
1233 struct journal_head *jh;
1234 int drop_reserve = 0;
1235 int err = 0;
Josef Bacik1dfc3222008-04-17 10:38:59 -04001236 int was_modified = 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001237
1238 BUFFER_TRACE(bh, "entry");
1239
1240 jbd_lock_bh_state(bh);
1241 spin_lock(&journal->j_list_lock);
1242
1243 if (!buffer_jbd(bh))
1244 goto not_jbd;
1245 jh = bh2jh(bh);
1246
1247 /* Critical error: attempting to delete a bitmap buffer, maybe?
1248 * Don't do any jbd operations, and return an error. */
1249 if (!J_EXPECT_JH(jh, !jh->b_committed_data,
1250 "inconsistent data on disk")) {
1251 err = -EIO;
1252 goto not_jbd;
1253 }
1254
Adam Buchbinder48fc7f72012-09-19 21:48:00 -04001255 /* keep track of whether or not this transaction modified us */
Josef Bacik1dfc3222008-04-17 10:38:59 -04001256 was_modified = jh->b_modified;
1257
Dave Kleikamp470decc2006-10-11 01:20:57 -07001258 /*
1259 * The buffer's going from the transaction, we must drop
1260 * all references -bzzz
1261 */
1262 jh->b_modified = 0;
1263
1264 if (jh->b_transaction == handle->h_transaction) {
1265 J_ASSERT_JH(jh, !jh->b_frozen_data);
1266
1267 /* If we are forgetting a buffer which is already part
1268 * of this transaction, then we can just drop it from
1269 * the transaction immediately. */
1270 clear_buffer_dirty(bh);
1271 clear_buffer_jbddirty(bh);
1272
1273 JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
1274
Josef Bacik1dfc3222008-04-17 10:38:59 -04001275 /*
1276 * we only want to drop a reference if this transaction
1277 * modified the buffer
1278 */
1279 if (was_modified)
1280 drop_reserve = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001281
1282 /*
1283 * We are no longer going to journal this buffer.
1284 * However, the commit of this transaction is still
1285 * important to the buffer: the delete that we are now
1286 * processing might obsolete an old log entry, so by
1287 * committing, we can satisfy the buffer's checkpoint.
1288 *
1289 * So, if we have a checkpoint on the buffer, we should
1290 * now refile the buffer on our BJ_Forget list so that
1291 * we know to remove the checkpoint after we commit.
1292 */
1293
1294 if (jh->b_cp_transaction) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001295 __jbd2_journal_temp_unlink_buffer(jh);
1296 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001297 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001298 __jbd2_journal_unfile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001299 if (!buffer_jbd(bh)) {
1300 spin_unlock(&journal->j_list_lock);
1301 jbd_unlock_bh_state(bh);
1302 __bforget(bh);
1303 goto drop;
1304 }
1305 }
1306 } else if (jh->b_transaction) {
1307 J_ASSERT_JH(jh, (jh->b_transaction ==
1308 journal->j_committing_transaction));
1309 /* However, if the buffer is still owned by a prior
1310 * (committing) transaction, we can't drop it yet... */
1311 JBUFFER_TRACE(jh, "belongs to older transaction");
1312 /* ... but we CAN drop it from the new transaction if we
1313 * have also modified it since the original commit. */
1314
1315 if (jh->b_next_transaction) {
1316 J_ASSERT(jh->b_next_transaction == transaction);
1317 jh->b_next_transaction = NULL;
Josef Bacik1dfc3222008-04-17 10:38:59 -04001318
1319 /*
1320 * only drop a reference if this transaction modified
1321 * the buffer
1322 */
1323 if (was_modified)
1324 drop_reserve = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001325 }
1326 }
1327
1328not_jbd:
1329 spin_unlock(&journal->j_list_lock);
1330 jbd_unlock_bh_state(bh);
1331 __brelse(bh);
1332drop:
1333 if (drop_reserve) {
1334 /* no need to reserve log space for this block -bzzz */
1335 handle->h_buffer_credits++;
1336 }
1337 return err;
1338}
1339
1340/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001341 * int jbd2_journal_stop() - complete a transaction
Dave Kleikamp470decc2006-10-11 01:20:57 -07001342 * @handle: tranaction to complete.
1343 *
1344 * All done for a particular handle.
1345 *
1346 * There is not much action needed here. We just return any remaining
1347 * buffer credits to the transaction and remove the handle. The only
1348 * complication is that we need to start a commit operation if the
1349 * filesystem is marked for synchronous update.
1350 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001351 * jbd2_journal_stop itself will not usually return an error, but it may
Dave Kleikamp470decc2006-10-11 01:20:57 -07001352 * do so in unusual circumstances. In particular, expect it to
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001353 * return -EIO if a jbd2_journal_abort has been executed since the
Dave Kleikamp470decc2006-10-11 01:20:57 -07001354 * transaction began.
1355 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001356int jbd2_journal_stop(handle_t *handle)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001357{
1358 transaction_t *transaction = handle->h_transaction;
1359 journal_t *journal = transaction->t_journal;
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001360 int err, wait_for_commit = 0;
1361 tid_t tid;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001362 pid_t pid;
1363
Dave Kleikamp470decc2006-10-11 01:20:57 -07001364 J_ASSERT(journal_current_handle() == handle);
1365
1366 if (is_handle_aborted(handle))
1367 err = -EIO;
OGAWA Hirofumi3e2a5322006-10-19 23:29:11 -07001368 else {
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001369 J_ASSERT(atomic_read(&transaction->t_updates) > 0);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001370 err = 0;
OGAWA Hirofumi3e2a5322006-10-19 23:29:11 -07001371 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001372
1373 if (--handle->h_ref > 0) {
1374 jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
1375 handle->h_ref);
1376 return err;
1377 }
1378
1379 jbd_debug(4, "Handle %p going down\n", handle);
1380
1381 /*
1382 * Implement synchronous transaction batching. If the handle
1383 * was synchronous, don't force a commit immediately. Let's
Josef Bacike07f7182008-11-26 01:14:26 -05001384 * yield and let another thread piggyback onto this
1385 * transaction. Keep doing that while new threads continue to
1386 * arrive. It doesn't cost much - we're about to run a commit
1387 * and sleep on IO anyway. Speeds up many-threaded, many-dir
1388 * operations by 30x or more...
Dave Kleikamp470decc2006-10-11 01:20:57 -07001389 *
Josef Bacike07f7182008-11-26 01:14:26 -05001390 * We try and optimize the sleep time against what the
1391 * underlying disk can do, instead of having a static sleep
1392 * time. This is useful for the case where our storage is so
1393 * fast that it is more optimal to go ahead and force a flush
1394 * and wait for the transaction to be committed than it is to
1395 * wait for an arbitrary amount of time for new writers to
1396 * join the transaction. We achieve this by measuring how
1397 * long it takes to commit a transaction, and compare it with
1398 * how long this transaction has been running, and if run time
1399 * < commit time then we sleep for the delta and commit. This
1400 * greatly helps super fast disks that would see slowdowns as
1401 * more threads started doing fsyncs.
1402 *
1403 * But don't do this if this process was the most recent one
1404 * to perform a synchronous write. We do this to detect the
1405 * case where a single process is doing a stream of sync
1406 * writes. No point in waiting for joiners in that case.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001407 */
1408 pid = current->pid;
1409 if (handle->h_sync && journal->j_last_sync_writer != pid) {
Josef Bacike07f7182008-11-26 01:14:26 -05001410 u64 commit_time, trans_time;
1411
Dave Kleikamp470decc2006-10-11 01:20:57 -07001412 journal->j_last_sync_writer = pid;
Josef Bacike07f7182008-11-26 01:14:26 -05001413
Theodore Ts'oa931da62010-08-03 21:35:12 -04001414 read_lock(&journal->j_state_lock);
Josef Bacike07f7182008-11-26 01:14:26 -05001415 commit_time = journal->j_average_commit_time;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001416 read_unlock(&journal->j_state_lock);
Josef Bacike07f7182008-11-26 01:14:26 -05001417
1418 trans_time = ktime_to_ns(ktime_sub(ktime_get(),
1419 transaction->t_start_time));
1420
Theodore Ts'o30773842009-01-03 20:27:38 -05001421 commit_time = max_t(u64, commit_time,
1422 1000*journal->j_min_batch_time);
Josef Bacike07f7182008-11-26 01:14:26 -05001423 commit_time = min_t(u64, commit_time,
Theodore Ts'o30773842009-01-03 20:27:38 -05001424 1000*journal->j_max_batch_time);
Josef Bacike07f7182008-11-26 01:14:26 -05001425
1426 if (trans_time < commit_time) {
1427 ktime_t expires = ktime_add_ns(ktime_get(),
1428 commit_time);
1429 set_current_state(TASK_UNINTERRUPTIBLE);
1430 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1431 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001432 }
1433
Theodore Ts'o70585482009-03-25 23:35:46 -04001434 if (handle->h_sync)
1435 transaction->t_synchronous_commit = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001436 current->journal_info = NULL;
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001437 atomic_sub(handle->h_buffer_credits,
1438 &transaction->t_outstanding_credits);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001439
1440 /*
1441 * If the handle is marked SYNC, we need to set another commit
1442 * going! We also want to force a commit if the current
1443 * transaction is occupying too much of the log, or if the
1444 * transaction is too old now.
1445 */
1446 if (handle->h_sync ||
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001447 (atomic_read(&transaction->t_outstanding_credits) >
1448 journal->j_max_transaction_buffers) ||
1449 time_after_eq(jiffies, transaction->t_expires)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001450 /* Do this even for aborted journals: an abort still
1451 * completes the commit thread, it just doesn't write
1452 * anything to disk. */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001453
Dave Kleikamp470decc2006-10-11 01:20:57 -07001454 jbd_debug(2, "transaction too old, requesting commit for "
1455 "handle %p\n", handle);
1456 /* This is non-blocking */
Theodore Ts'oc35a56a2010-05-16 05:00:00 -04001457 jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001458
1459 /*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001460 * Special case: JBD2_SYNC synchronous updates require us
Dave Kleikamp470decc2006-10-11 01:20:57 -07001461 * to wait for the commit to complete.
1462 */
1463 if (handle->h_sync && !(current->flags & PF_MEMALLOC))
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001464 wait_for_commit = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001465 }
1466
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001467 /*
1468 * Once we drop t_updates, if it goes to zero the transaction
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001469 * could start committing on us and eventually disappear. So
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001470 * once we do this, we must not dereference transaction
1471 * pointer again.
1472 */
1473 tid = transaction->t_tid;
1474 if (atomic_dec_and_test(&transaction->t_updates)) {
1475 wake_up(&journal->j_wait_updates);
1476 if (journal->j_barrier_count)
1477 wake_up(&journal->j_wait_transaction_locked);
1478 }
1479
1480 if (wait_for_commit)
1481 err = jbd2_log_wait_commit(journal, tid);
1482
Ingo Molnar3295f0e2008-08-11 10:30:30 +02001483 lock_map_release(&handle->h_lockdep_map);
Mingming Cao7b751062008-01-28 23:58:27 -05001484
Mingming Caoaf1e76d2007-10-16 18:38:25 -04001485 jbd2_free_handle(handle);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001486 return err;
1487}
1488
Randy Dunlap5648ba52008-04-17 10:38:59 -04001489/**
1490 * int jbd2_journal_force_commit() - force any uncommitted transactions
Dave Kleikamp470decc2006-10-11 01:20:57 -07001491 * @journal: journal to force
1492 *
1493 * For synchronous operations: force any uncommitted transactions
1494 * to disk. May seem kludgy, but it reuses all the handle batching
1495 * code in a very simple manner.
1496 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001497int jbd2_journal_force_commit(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001498{
1499 handle_t *handle;
1500 int ret;
1501
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001502 handle = jbd2_journal_start(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001503 if (IS_ERR(handle)) {
1504 ret = PTR_ERR(handle);
1505 } else {
1506 handle->h_sync = 1;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001507 ret = jbd2_journal_stop(handle);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001508 }
1509 return ret;
1510}
1511
1512/*
1513 *
1514 * List management code snippets: various functions for manipulating the
1515 * transaction buffer lists.
1516 *
1517 */
1518
1519/*
1520 * Append a buffer to a transaction list, given the transaction's list head
1521 * pointer.
1522 *
1523 * j_list_lock is held.
1524 *
1525 * jbd_lock_bh_state(jh2bh(jh)) is held.
1526 */
1527
1528static inline void
1529__blist_add_buffer(struct journal_head **list, struct journal_head *jh)
1530{
1531 if (!*list) {
1532 jh->b_tnext = jh->b_tprev = jh;
1533 *list = jh;
1534 } else {
1535 /* Insert at the tail of the list to preserve order */
1536 struct journal_head *first = *list, *last = first->b_tprev;
1537 jh->b_tprev = last;
1538 jh->b_tnext = first;
1539 last->b_tnext = first->b_tprev = jh;
1540 }
1541}
1542
1543/*
1544 * Remove a buffer from a transaction list, given the transaction's list
1545 * head pointer.
1546 *
1547 * Called with j_list_lock held, and the journal may not be locked.
1548 *
1549 * jbd_lock_bh_state(jh2bh(jh)) is held.
1550 */
1551
1552static inline void
1553__blist_del_buffer(struct journal_head **list, struct journal_head *jh)
1554{
1555 if (*list == jh) {
1556 *list = jh->b_tnext;
1557 if (*list == jh)
1558 *list = NULL;
1559 }
1560 jh->b_tprev->b_tnext = jh->b_tnext;
1561 jh->b_tnext->b_tprev = jh->b_tprev;
1562}
1563
1564/*
1565 * Remove a buffer from the appropriate transaction list.
1566 *
1567 * Note that this function can *change* the value of
Jan Kara87c89c22008-07-11 19:27:31 -04001568 * bh->b_transaction->t_buffers, t_forget, t_iobuf_list, t_shadow_list,
1569 * t_log_list or t_reserved_list. If the caller is holding onto a copy of one
1570 * of these pointers, it could go bad. Generally the caller needs to re-read
1571 * the pointer from the transaction_t.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001572 *
Jan Kara5bebccf2012-03-13 22:25:06 -04001573 * Called under j_list_lock.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001574 */
Jan Kara5bebccf2012-03-13 22:25:06 -04001575static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001576{
1577 struct journal_head **list = NULL;
1578 transaction_t *transaction;
1579 struct buffer_head *bh = jh2bh(jh);
1580
1581 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1582 transaction = jh->b_transaction;
1583 if (transaction)
1584 assert_spin_locked(&transaction->t_journal->j_list_lock);
1585
1586 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1587 if (jh->b_jlist != BJ_None)
Mingming Cao40191912008-01-28 23:58:27 -05001588 J_ASSERT_JH(jh, transaction != NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001589
1590 switch (jh->b_jlist) {
1591 case BJ_None:
1592 return;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001593 case BJ_Metadata:
1594 transaction->t_nr_buffers--;
1595 J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
1596 list = &transaction->t_buffers;
1597 break;
1598 case BJ_Forget:
1599 list = &transaction->t_forget;
1600 break;
1601 case BJ_IO:
1602 list = &transaction->t_iobuf_list;
1603 break;
1604 case BJ_Shadow:
1605 list = &transaction->t_shadow_list;
1606 break;
1607 case BJ_LogCtl:
1608 list = &transaction->t_log_list;
1609 break;
1610 case BJ_Reserved:
1611 list = &transaction->t_reserved_list;
1612 break;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001613 }
1614
1615 __blist_del_buffer(list, jh);
1616 jh->b_jlist = BJ_None;
1617 if (test_clear_buffer_jbddirty(bh))
1618 mark_buffer_dirty(bh); /* Expose it to the VM */
1619}
1620
Jan Karade1b7942011-06-13 15:38:22 -04001621/*
1622 * Remove buffer from all transactions.
1623 *
1624 * Called with bh_state lock and j_list_lock
1625 *
1626 * jh and bh may be already freed when this function returns.
1627 */
1628static void __jbd2_journal_unfile_buffer(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001629{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001630 __jbd2_journal_temp_unlink_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001631 jh->b_transaction = NULL;
Jan Karade1b7942011-06-13 15:38:22 -04001632 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001633}
1634
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001635void jbd2_journal_unfile_buffer(journal_t *journal, struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001636{
Jan Karade1b7942011-06-13 15:38:22 -04001637 struct buffer_head *bh = jh2bh(jh);
1638
1639 /* Get reference so that buffer cannot be freed before we unlock it */
1640 get_bh(bh);
1641 jbd_lock_bh_state(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001642 spin_lock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001643 __jbd2_journal_unfile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001644 spin_unlock(&journal->j_list_lock);
Jan Karade1b7942011-06-13 15:38:22 -04001645 jbd_unlock_bh_state(bh);
1646 __brelse(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001647}
1648
1649/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001650 * Called from jbd2_journal_try_to_free_buffers().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001651 *
1652 * Called under jbd_lock_bh_state(bh)
1653 */
1654static void
1655__journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh)
1656{
1657 struct journal_head *jh;
1658
1659 jh = bh2jh(bh);
1660
1661 if (buffer_locked(bh) || buffer_dirty(bh))
1662 goto out;
1663
Mingming Cao40191912008-01-28 23:58:27 -05001664 if (jh->b_next_transaction != NULL)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001665 goto out;
1666
1667 spin_lock(&journal->j_list_lock);
Jan Kara87c89c22008-07-11 19:27:31 -04001668 if (jh->b_cp_transaction != NULL && jh->b_transaction == NULL) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001669 /* written-back checkpointed metadata buffer */
Jan Karac254c9e2012-03-13 22:27:44 -04001670 JBUFFER_TRACE(jh, "remove from checkpoint list");
1671 __jbd2_journal_remove_checkpoint(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001672 }
1673 spin_unlock(&journal->j_list_lock);
1674out:
1675 return;
1676}
1677
Dave Kleikamp470decc2006-10-11 01:20:57 -07001678/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001679 * int jbd2_journal_try_to_free_buffers() - try to free page buffers.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001680 * @journal: journal for operation
1681 * @page: to try and free
Mingming Cao530576b2008-07-13 21:06:39 -04001682 * @gfp_mask: we use the mask to detect how hard should we try to release
1683 * buffers. If __GFP_WAIT and __GFP_FS is set, we wait for commit code to
1684 * release the buffers.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001685 *
1686 *
1687 * For all the buffers on this page,
1688 * if they are fully written out ordered data, move them onto BUF_CLEAN
1689 * so try_to_free_buffers() can reap them.
1690 *
1691 * This function returns non-zero if we wish try_to_free_buffers()
1692 * to be called. We do this if the page is releasable by try_to_free_buffers().
1693 * We also do it if the page has locked or dirty buffers and the caller wants
1694 * us to perform sync or async writeout.
1695 *
1696 * This complicates JBD locking somewhat. We aren't protected by the
1697 * BKL here. We wish to remove the buffer from its committing or
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001698 * running transaction's ->t_datalist via __jbd2_journal_unfile_buffer.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001699 *
1700 * This may *change* the value of transaction_t->t_datalist, so anyone
1701 * who looks at t_datalist needs to lock against this function.
1702 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001703 * Even worse, someone may be doing a jbd2_journal_dirty_data on this
1704 * buffer. So we need to lock against that. jbd2_journal_dirty_data()
Dave Kleikamp470decc2006-10-11 01:20:57 -07001705 * will come out of the lock with the buffer dirty, which makes it
1706 * ineligible for release here.
1707 *
1708 * Who else is affected by this? hmm... Really the only contender
1709 * is do_get_write_access() - it could be looking at the buffer while
1710 * journal_try_to_free_buffer() is changing its state. But that
1711 * cannot happen because we never reallocate freed data as metadata
1712 * while the data is part of a transaction. Yes?
Mingming Cao530576b2008-07-13 21:06:39 -04001713 *
1714 * Return 0 on failure, 1 on success
Dave Kleikamp470decc2006-10-11 01:20:57 -07001715 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001716int jbd2_journal_try_to_free_buffers(journal_t *journal,
Mingming Cao530576b2008-07-13 21:06:39 -04001717 struct page *page, gfp_t gfp_mask)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001718{
1719 struct buffer_head *head;
1720 struct buffer_head *bh;
1721 int ret = 0;
1722
1723 J_ASSERT(PageLocked(page));
1724
1725 head = page_buffers(page);
1726 bh = head;
1727 do {
1728 struct journal_head *jh;
1729
1730 /*
1731 * We take our own ref against the journal_head here to avoid
1732 * having to add tons of locking around each instance of
Mingming Cao530576b2008-07-13 21:06:39 -04001733 * jbd2_journal_put_journal_head().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001734 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001735 jh = jbd2_journal_grab_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001736 if (!jh)
1737 continue;
1738
1739 jbd_lock_bh_state(bh);
1740 __journal_try_to_free_buffer(journal, bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001741 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001742 jbd_unlock_bh_state(bh);
1743 if (buffer_jbd(bh))
1744 goto busy;
1745 } while ((bh = bh->b_this_page) != head);
Mingming Cao530576b2008-07-13 21:06:39 -04001746
Dave Kleikamp470decc2006-10-11 01:20:57 -07001747 ret = try_to_free_buffers(page);
Mingming Cao530576b2008-07-13 21:06:39 -04001748
Dave Kleikamp470decc2006-10-11 01:20:57 -07001749busy:
1750 return ret;
1751}
1752
1753/*
1754 * This buffer is no longer needed. If it is on an older transaction's
1755 * checkpoint list we need to record it on this transaction's forget list
1756 * to pin this buffer (and hence its checkpointing transaction) down until
1757 * this transaction commits. If the buffer isn't on a checkpoint list, we
1758 * release it.
1759 * Returns non-zero if JBD no longer has an interest in the buffer.
1760 *
1761 * Called under j_list_lock.
1762 *
1763 * Called under jbd_lock_bh_state(bh).
1764 */
1765static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
1766{
1767 int may_free = 1;
1768 struct buffer_head *bh = jh2bh(jh);
1769
Dave Kleikamp470decc2006-10-11 01:20:57 -07001770 if (jh->b_cp_transaction) {
1771 JBUFFER_TRACE(jh, "on running+cp transaction");
Jan Karade1b7942011-06-13 15:38:22 -04001772 __jbd2_journal_temp_unlink_buffer(jh);
Jan Karaf91d1d02009-07-13 16:16:20 -04001773 /*
1774 * We don't want to write the buffer anymore, clear the
1775 * bit so that we don't confuse checks in
1776 * __journal_file_buffer
1777 */
1778 clear_buffer_dirty(bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001779 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001780 may_free = 0;
1781 } else {
1782 JBUFFER_TRACE(jh, "on running transaction");
Jan Karade1b7942011-06-13 15:38:22 -04001783 __jbd2_journal_unfile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001784 }
1785 return may_free;
1786}
1787
1788/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001789 * jbd2_journal_invalidatepage
Dave Kleikamp470decc2006-10-11 01:20:57 -07001790 *
1791 * This code is tricky. It has a number of cases to deal with.
1792 *
1793 * There are two invariants which this code relies on:
1794 *
1795 * i_size must be updated on disk before we start calling invalidatepage on the
1796 * data.
1797 *
1798 * This is done in ext3 by defining an ext3_setattr method which
1799 * updates i_size before truncate gets going. By maintaining this
1800 * invariant, we can be sure that it is safe to throw away any buffers
1801 * attached to the current transaction: once the transaction commits,
1802 * we know that the data will not be needed.
1803 *
1804 * Note however that we can *not* throw away data belonging to the
1805 * previous, committing transaction!
1806 *
1807 * Any disk blocks which *are* part of the previous, committing
1808 * transaction (and which therefore cannot be discarded immediately) are
1809 * not going to be reused in the new running transaction
1810 *
1811 * The bitmap committed_data images guarantee this: any block which is
1812 * allocated in one transaction and removed in the next will be marked
1813 * as in-use in the committed_data bitmap, so cannot be reused until
1814 * the next transaction to delete the block commits. This means that
1815 * leaving committing buffers dirty is quite safe: the disk blocks
1816 * cannot be reallocated to a different file and so buffer aliasing is
1817 * not possible.
1818 *
1819 *
1820 * The above applies mainly to ordered data mode. In writeback mode we
1821 * don't make guarantees about the order in which data hits disk --- in
1822 * particular we don't guarantee that new dirty data is flushed before
1823 * transaction commit --- so it is always safe just to discard data
1824 * immediately in that mode. --sct
1825 */
1826
1827/*
1828 * The journal_unmap_buffer helper function returns zero if the buffer
1829 * concerned remains pinned as an anonymous buffer belonging to an older
1830 * transaction.
1831 *
1832 * We're outside-transaction here. Either or both of j_running_transaction
1833 * and j_committing_transaction may be NULL.
1834 */
Jan Karab794e7a2012-09-26 23:11:13 -04001835static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh,
1836 int partial_page)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001837{
1838 transaction_t *transaction;
1839 struct journal_head *jh;
1840 int may_free = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001841
1842 BUFFER_TRACE(bh, "entry");
1843
1844 /*
1845 * It is safe to proceed here without the j_list_lock because the
1846 * buffers cannot be stolen by try_to_free_buffers as long as we are
1847 * holding the page lock. --sct
1848 */
1849
1850 if (!buffer_jbd(bh))
1851 goto zap_buffer_unlocked;
1852
Jan Kara87c89c22008-07-11 19:27:31 -04001853 /* OK, we have data buffer in journaled mode */
Theodore Ts'oa931da62010-08-03 21:35:12 -04001854 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001855 jbd_lock_bh_state(bh);
1856 spin_lock(&journal->j_list_lock);
1857
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001858 jh = jbd2_journal_grab_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001859 if (!jh)
1860 goto zap_buffer_no_jh;
1861
dingdinghuaba869022010-02-15 16:35:42 -05001862 /*
1863 * We cannot remove the buffer from checkpoint lists until the
1864 * transaction adding inode to orphan list (let's call it T)
1865 * is committed. Otherwise if the transaction changing the
1866 * buffer would be cleaned from the journal before T is
1867 * committed, a crash will cause that the correct contents of
1868 * the buffer will be lost. On the other hand we have to
1869 * clear the buffer dirty bit at latest at the moment when the
1870 * transaction marking the buffer as freed in the filesystem
1871 * structures is committed because from that moment on the
Jan Karab794e7a2012-09-26 23:11:13 -04001872 * block can be reallocated and used by a different page.
dingdinghuaba869022010-02-15 16:35:42 -05001873 * Since the block hasn't been freed yet but the inode has
1874 * already been added to orphan list, it is safe for us to add
1875 * the buffer to BJ_Forget list of the newest transaction.
Jan Karab794e7a2012-09-26 23:11:13 -04001876 *
1877 * Also we have to clear buffer_mapped flag of a truncated buffer
1878 * because the buffer_head may be attached to the page straddling
1879 * i_size (can happen only when blocksize < pagesize) and thus the
1880 * buffer_head can be reused when the file is extended again. So we end
1881 * up keeping around invalidated buffers attached to transactions'
1882 * BJ_Forget list just to stop checkpointing code from cleaning up
1883 * the transaction this buffer was modified in.
dingdinghuaba869022010-02-15 16:35:42 -05001884 */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001885 transaction = jh->b_transaction;
1886 if (transaction == NULL) {
1887 /* First case: not on any transaction. If it
1888 * has no checkpoint link, then we can zap it:
1889 * it's a writeback-mode buffer so we don't care
1890 * if it hits disk safely. */
1891 if (!jh->b_cp_transaction) {
1892 JBUFFER_TRACE(jh, "not on any transaction: zap");
1893 goto zap_buffer;
1894 }
1895
1896 if (!buffer_dirty(bh)) {
1897 /* bdflush has written it. We can drop it now */
1898 goto zap_buffer;
1899 }
1900
1901 /* OK, it must be in the journal but still not
1902 * written fully to disk: it's metadata or
1903 * journaled data... */
1904
1905 if (journal->j_running_transaction) {
1906 /* ... and once the current transaction has
1907 * committed, the buffer won't be needed any
1908 * longer. */
1909 JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
Jan Karab794e7a2012-09-26 23:11:13 -04001910 may_free = __dispose_buffer(jh,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001911 journal->j_running_transaction);
Jan Karab794e7a2012-09-26 23:11:13 -04001912 goto zap_buffer;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001913 } else {
1914 /* There is no currently-running transaction. So the
1915 * orphan record which we wrote for this file must have
1916 * passed into commit. We must attach this buffer to
1917 * the committing transaction, if it exists. */
1918 if (journal->j_committing_transaction) {
1919 JBUFFER_TRACE(jh, "give to committing trans");
Jan Karab794e7a2012-09-26 23:11:13 -04001920 may_free = __dispose_buffer(jh,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001921 journal->j_committing_transaction);
Jan Karab794e7a2012-09-26 23:11:13 -04001922 goto zap_buffer;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001923 } else {
1924 /* The orphan record's transaction has
1925 * committed. We can cleanse this buffer */
1926 clear_buffer_jbddirty(bh);
1927 goto zap_buffer;
1928 }
1929 }
1930 } else if (transaction == journal->j_committing_transaction) {
Eric Sandeen9b579882006-10-28 10:38:28 -07001931 JBUFFER_TRACE(jh, "on committing transaction");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001932 /*
dingdinghuaba869022010-02-15 16:35:42 -05001933 * The buffer is committing, we simply cannot touch
Jan Karab794e7a2012-09-26 23:11:13 -04001934 * it. If the page is straddling i_size we have to wait
1935 * for commit and try again.
1936 */
1937 if (partial_page) {
Jan Karab794e7a2012-09-26 23:11:13 -04001938 jbd2_journal_put_journal_head(jh);
1939 spin_unlock(&journal->j_list_lock);
1940 jbd_unlock_bh_state(bh);
1941 write_unlock(&journal->j_state_lock);
Jan Kara53e87262012-12-25 13:29:52 -05001942 return -EBUSY;
Jan Karab794e7a2012-09-26 23:11:13 -04001943 }
1944 /*
1945 * OK, buffer won't be reachable after truncate. We just set
1946 * j_next_transaction to the running transaction (if there is
1947 * one) and mark buffer as freed so that commit code knows it
1948 * should clear dirty bits when it is done with the buffer.
dingdinghuaba869022010-02-15 16:35:42 -05001949 */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001950 set_buffer_freed(bh);
dingdinghuaba869022010-02-15 16:35:42 -05001951 if (journal->j_running_transaction && buffer_jbddirty(bh))
1952 jh->b_next_transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001953 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001954 spin_unlock(&journal->j_list_lock);
1955 jbd_unlock_bh_state(bh);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001956 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001957 return 0;
1958 } else {
1959 /* Good, the buffer belongs to the running transaction.
1960 * We are writing our own transaction's data, not any
1961 * previous one's, so it is safe to throw it away
1962 * (remember that we expect the filesystem to have set
1963 * i_size already for this truncate so recovery will not
1964 * expose the disk blocks we are discarding here.) */
1965 J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
Eric Sandeen9b579882006-10-28 10:38:28 -07001966 JBUFFER_TRACE(jh, "on running transaction");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001967 may_free = __dispose_buffer(jh, transaction);
1968 }
1969
1970zap_buffer:
Jan Karab794e7a2012-09-26 23:11:13 -04001971 /*
1972 * This is tricky. Although the buffer is truncated, it may be reused
1973 * if blocksize < pagesize and it is attached to the page straddling
1974 * EOF. Since the buffer might have been added to BJ_Forget list of the
1975 * running transaction, journal_get_write_access() won't clear
1976 * b_modified and credit accounting gets confused. So clear b_modified
1977 * here.
1978 */
1979 jh->b_modified = 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001980 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001981zap_buffer_no_jh:
1982 spin_unlock(&journal->j_list_lock);
1983 jbd_unlock_bh_state(bh);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001984 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001985zap_buffer_unlocked:
1986 clear_buffer_dirty(bh);
1987 J_ASSERT_BH(bh, !buffer_jbddirty(bh));
1988 clear_buffer_mapped(bh);
1989 clear_buffer_req(bh);
1990 clear_buffer_new(bh);
Eric Sandeen15291162012-02-20 17:53:01 -05001991 clear_buffer_delay(bh);
1992 clear_buffer_unwritten(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001993 bh->b_bdev = NULL;
1994 return may_free;
1995}
1996
1997/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001998 * void jbd2_journal_invalidatepage()
Dave Kleikamp470decc2006-10-11 01:20:57 -07001999 * @journal: journal to use for flush...
2000 * @page: page to flush
2001 * @offset: length of page to invalidate.
2002 *
Jan Kara53e87262012-12-25 13:29:52 -05002003 * Reap page buffers containing data after offset in page. Can return -EBUSY
2004 * if buffers are part of the committing transaction and the page is straddling
2005 * i_size. Caller then has to wait for current commit and try again.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002006 */
Jan Kara53e87262012-12-25 13:29:52 -05002007int jbd2_journal_invalidatepage(journal_t *journal,
2008 struct page *page,
2009 unsigned long offset)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002010{
2011 struct buffer_head *head, *bh, *next;
2012 unsigned int curr_off = 0;
2013 int may_free = 1;
Jan Kara53e87262012-12-25 13:29:52 -05002014 int ret = 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002015
2016 if (!PageLocked(page))
2017 BUG();
2018 if (!page_has_buffers(page))
Jan Kara53e87262012-12-25 13:29:52 -05002019 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002020
2021 /* We will potentially be playing with lists other than just the
2022 * data lists (especially for journaled data mode), so be
2023 * cautious in our locking. */
2024
2025 head = bh = page_buffers(page);
2026 do {
2027 unsigned int next_off = curr_off + bh->b_size;
2028 next = bh->b_this_page;
2029
2030 if (offset <= curr_off) {
2031 /* This block is wholly outside the truncation point */
2032 lock_buffer(bh);
Jan Kara53e87262012-12-25 13:29:52 -05002033 ret = journal_unmap_buffer(journal, bh, offset > 0);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002034 unlock_buffer(bh);
Jan Kara53e87262012-12-25 13:29:52 -05002035 if (ret < 0)
2036 return ret;
2037 may_free &= ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002038 }
2039 curr_off = next_off;
2040 bh = next;
2041
2042 } while (bh != head);
2043
2044 if (!offset) {
2045 if (may_free && try_to_free_buffers(page))
2046 J_ASSERT(!page_has_buffers(page));
2047 }
Jan Kara53e87262012-12-25 13:29:52 -05002048 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002049}
2050
2051/*
2052 * File a buffer on the given transaction list.
2053 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002054void __jbd2_journal_file_buffer(struct journal_head *jh,
Dave Kleikamp470decc2006-10-11 01:20:57 -07002055 transaction_t *transaction, int jlist)
2056{
2057 struct journal_head **list = NULL;
2058 int was_dirty = 0;
2059 struct buffer_head *bh = jh2bh(jh);
2060
2061 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
2062 assert_spin_locked(&transaction->t_journal->j_list_lock);
2063
2064 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
2065 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
Mingming Cao40191912008-01-28 23:58:27 -05002066 jh->b_transaction == NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002067
2068 if (jh->b_transaction && jh->b_jlist == jlist)
2069 return;
2070
Dave Kleikamp470decc2006-10-11 01:20:57 -07002071 if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
2072 jlist == BJ_Shadow || jlist == BJ_Forget) {
Jan Karaf91d1d02009-07-13 16:16:20 -04002073 /*
2074 * For metadata buffers, we track dirty bit in buffer_jbddirty
2075 * instead of buffer_dirty. We should not see a dirty bit set
2076 * here because we clear it in do_get_write_access but e.g.
2077 * tune2fs can modify the sb and set the dirty bit at any time
2078 * so we try to gracefully handle that.
2079 */
2080 if (buffer_dirty(bh))
2081 warn_dirty_buffer(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002082 if (test_clear_buffer_dirty(bh) ||
2083 test_clear_buffer_jbddirty(bh))
2084 was_dirty = 1;
2085 }
2086
2087 if (jh->b_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002088 __jbd2_journal_temp_unlink_buffer(jh);
Jan Karade1b7942011-06-13 15:38:22 -04002089 else
2090 jbd2_journal_grab_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002091 jh->b_transaction = transaction;
2092
2093 switch (jlist) {
2094 case BJ_None:
2095 J_ASSERT_JH(jh, !jh->b_committed_data);
2096 J_ASSERT_JH(jh, !jh->b_frozen_data);
2097 return;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002098 case BJ_Metadata:
2099 transaction->t_nr_buffers++;
2100 list = &transaction->t_buffers;
2101 break;
2102 case BJ_Forget:
2103 list = &transaction->t_forget;
2104 break;
2105 case BJ_IO:
2106 list = &transaction->t_iobuf_list;
2107 break;
2108 case BJ_Shadow:
2109 list = &transaction->t_shadow_list;
2110 break;
2111 case BJ_LogCtl:
2112 list = &transaction->t_log_list;
2113 break;
2114 case BJ_Reserved:
2115 list = &transaction->t_reserved_list;
2116 break;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002117 }
2118
2119 __blist_add_buffer(list, jh);
2120 jh->b_jlist = jlist;
2121
2122 if (was_dirty)
2123 set_buffer_jbddirty(bh);
2124}
2125
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002126void jbd2_journal_file_buffer(struct journal_head *jh,
Dave Kleikamp470decc2006-10-11 01:20:57 -07002127 transaction_t *transaction, int jlist)
2128{
2129 jbd_lock_bh_state(jh2bh(jh));
2130 spin_lock(&transaction->t_journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002131 __jbd2_journal_file_buffer(jh, transaction, jlist);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002132 spin_unlock(&transaction->t_journal->j_list_lock);
2133 jbd_unlock_bh_state(jh2bh(jh));
2134}
2135
2136/*
2137 * Remove a buffer from its current buffer list in preparation for
2138 * dropping it from its current transaction entirely. If the buffer has
2139 * already started to be used by a subsequent transaction, refile the
2140 * buffer on that transaction's metadata list.
2141 *
Jan Karade1b7942011-06-13 15:38:22 -04002142 * Called under j_list_lock
Dave Kleikamp470decc2006-10-11 01:20:57 -07002143 * Called under jbd_lock_bh_state(jh2bh(jh))
Jan Karade1b7942011-06-13 15:38:22 -04002144 *
2145 * jh and bh may be already free when this function returns
Dave Kleikamp470decc2006-10-11 01:20:57 -07002146 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002147void __jbd2_journal_refile_buffer(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002148{
dingdinghuaba869022010-02-15 16:35:42 -05002149 int was_dirty, jlist;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002150 struct buffer_head *bh = jh2bh(jh);
2151
2152 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
2153 if (jh->b_transaction)
2154 assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
2155
2156 /* If the buffer is now unused, just drop it. */
2157 if (jh->b_next_transaction == NULL) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002158 __jbd2_journal_unfile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002159 return;
2160 }
2161
2162 /*
2163 * It has been modified by a later transaction: add it to the new
2164 * transaction's metadata list.
2165 */
2166
2167 was_dirty = test_clear_buffer_jbddirty(bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002168 __jbd2_journal_temp_unlink_buffer(jh);
Jan Karade1b7942011-06-13 15:38:22 -04002169 /*
2170 * We set b_transaction here because b_next_transaction will inherit
2171 * our jh reference and thus __jbd2_journal_file_buffer() must not
2172 * take a new one.
2173 */
Dave Kleikamp470decc2006-10-11 01:20:57 -07002174 jh->b_transaction = jh->b_next_transaction;
2175 jh->b_next_transaction = NULL;
dingdinghuaba869022010-02-15 16:35:42 -05002176 if (buffer_freed(bh))
2177 jlist = BJ_Forget;
2178 else if (jh->b_modified)
2179 jlist = BJ_Metadata;
2180 else
2181 jlist = BJ_Reserved;
2182 __jbd2_journal_file_buffer(jh, jh->b_transaction, jlist);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002183 J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
2184
2185 if (was_dirty)
2186 set_buffer_jbddirty(bh);
2187}
2188
2189/*
Jan Karade1b7942011-06-13 15:38:22 -04002190 * __jbd2_journal_refile_buffer() with necessary locking added. We take our
2191 * bh reference so that we can safely unlock bh.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002192 *
Jan Karade1b7942011-06-13 15:38:22 -04002193 * The jh and bh may be freed by this call.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002194 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002195void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002196{
2197 struct buffer_head *bh = jh2bh(jh);
2198
Jan Karade1b7942011-06-13 15:38:22 -04002199 /* Get reference so that buffer cannot be freed before we unlock it */
2200 get_bh(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002201 jbd_lock_bh_state(bh);
2202 spin_lock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002203 __jbd2_journal_refile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002204 jbd_unlock_bh_state(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002205 spin_unlock(&journal->j_list_lock);
2206 __brelse(bh);
2207}
Jan Karac851ed52008-07-11 19:27:31 -04002208
2209/*
2210 * File inode in the inode list of the handle's transaction
2211 */
2212int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode)
2213{
2214 transaction_t *transaction = handle->h_transaction;
2215 journal_t *journal = transaction->t_journal;
2216
2217 if (is_handle_aborted(handle))
2218 return -EIO;
2219
2220 jbd_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino,
2221 transaction->t_tid);
2222
2223 /*
2224 * First check whether inode isn't already on the transaction's
2225 * lists without taking the lock. Note that this check is safe
2226 * without the lock as we cannot race with somebody removing inode
2227 * from the transaction. The reason is that we remove inode from the
2228 * transaction only in journal_release_jbd_inode() and when we commit
2229 * the transaction. We are guarded from the first case by holding
2230 * a reference to the inode. We are safe against the second case
2231 * because if jinode->i_transaction == transaction, commit code
2232 * cannot touch the transaction because we hold reference to it,
2233 * and if jinode->i_next_transaction == transaction, commit code
2234 * will only file the inode where we want it.
2235 */
2236 if (jinode->i_transaction == transaction ||
2237 jinode->i_next_transaction == transaction)
2238 return 0;
2239
2240 spin_lock(&journal->j_list_lock);
2241
2242 if (jinode->i_transaction == transaction ||
2243 jinode->i_next_transaction == transaction)
2244 goto done;
2245
Jan Kara81be12c2011-05-24 11:52:40 -04002246 /*
2247 * We only ever set this variable to 1 so the test is safe. Since
2248 * t_need_data_flush is likely to be set, we do the test to save some
2249 * cacheline bouncing
2250 */
2251 if (!transaction->t_need_data_flush)
2252 transaction->t_need_data_flush = 1;
Jan Karac851ed52008-07-11 19:27:31 -04002253 /* On some different transaction's list - should be
2254 * the committing one */
2255 if (jinode->i_transaction) {
2256 J_ASSERT(jinode->i_next_transaction == NULL);
2257 J_ASSERT(jinode->i_transaction ==
2258 journal->j_committing_transaction);
2259 jinode->i_next_transaction = transaction;
2260 goto done;
2261 }
2262 /* Not on any transaction list... */
2263 J_ASSERT(!jinode->i_next_transaction);
2264 jinode->i_transaction = transaction;
2265 list_add(&jinode->i_list, &transaction->t_inode_list);
2266done:
2267 spin_unlock(&journal->j_list_lock);
2268
2269 return 0;
2270}
2271
2272/*
Jan Kara7f5aa212009-02-10 11:15:34 -05002273 * File truncate and transaction commit interact with each other in a
2274 * non-trivial way. If a transaction writing data block A is
2275 * committing, we cannot discard the data by truncate until we have
2276 * written them. Otherwise if we crashed after the transaction with
2277 * write has committed but before the transaction with truncate has
2278 * committed, we could see stale data in block A. This function is a
2279 * helper to solve this problem. It starts writeout of the truncated
2280 * part in case it is in the committing transaction.
2281 *
2282 * Filesystem code must call this function when inode is journaled in
2283 * ordered mode before truncation happens and after the inode has been
2284 * placed on orphan list with the new inode size. The second condition
2285 * avoids the race that someone writes new data and we start
2286 * committing the transaction after this function has been called but
2287 * before a transaction for truncate is started (and furthermore it
2288 * allows us to optimize the case where the addition to orphan list
2289 * happens in the same transaction as write --- we don't have to write
2290 * any data in such case).
Jan Karac851ed52008-07-11 19:27:31 -04002291 */
Jan Kara7f5aa212009-02-10 11:15:34 -05002292int jbd2_journal_begin_ordered_truncate(journal_t *journal,
2293 struct jbd2_inode *jinode,
Jan Karac851ed52008-07-11 19:27:31 -04002294 loff_t new_size)
2295{
Jan Kara7f5aa212009-02-10 11:15:34 -05002296 transaction_t *inode_trans, *commit_trans;
Jan Karac851ed52008-07-11 19:27:31 -04002297 int ret = 0;
2298
Jan Kara7f5aa212009-02-10 11:15:34 -05002299 /* This is a quick check to avoid locking if not necessary */
2300 if (!jinode->i_transaction)
Jan Karac851ed52008-07-11 19:27:31 -04002301 goto out;
Jan Kara7f5aa212009-02-10 11:15:34 -05002302 /* Locks are here just to force reading of recent values, it is
2303 * enough that the transaction was not committing before we started
2304 * a transaction adding the inode to orphan list */
Theodore Ts'oa931da62010-08-03 21:35:12 -04002305 read_lock(&journal->j_state_lock);
Jan Karac851ed52008-07-11 19:27:31 -04002306 commit_trans = journal->j_committing_transaction;
Theodore Ts'oa931da62010-08-03 21:35:12 -04002307 read_unlock(&journal->j_state_lock);
Jan Kara7f5aa212009-02-10 11:15:34 -05002308 spin_lock(&journal->j_list_lock);
2309 inode_trans = jinode->i_transaction;
2310 spin_unlock(&journal->j_list_lock);
2311 if (inode_trans == commit_trans) {
2312 ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping,
Jan Karac851ed52008-07-11 19:27:31 -04002313 new_size, LLONG_MAX);
2314 if (ret)
2315 jbd2_journal_abort(journal, ret);
2316 }
2317out:
2318 return ret;
2319}