blob: faad2bd787c77042d40b6e07fa43da04cda37413 [file] [log] [blame]
Dave Kleikamp470decc2006-10-11 01:20:57 -07001/*
Uwe Kleine-König58862692007-05-09 07:51:49 +02002 * linux/fs/jbd2/transaction.c
Dave Kleikamp470decc2006-10-11 01:20:57 -07003 *
4 * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
5 *
6 * Copyright 1998 Red Hat corp --- All Rights Reserved
7 *
8 * This file is part of the Linux kernel and is made available under
9 * the terms of the GNU General Public License, version 2, or at your
10 * option, any later version, incorporated herein by reference.
11 *
12 * Generic filesystem transaction handling code; part of the ext2fs
13 * journaling system.
14 *
15 * This file manages transactions (compound commits managed by the
16 * journaling code) and handles (individual atomic operations by the
17 * filesystem).
18 */
19
20#include <linux/time.h>
21#include <linux/fs.h>
Mingming Caof7f4bcc2006-10-11 01:20:59 -070022#include <linux/jbd2.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070023#include <linux/errno.h>
24#include <linux/slab.h>
25#include <linux/timer.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070026#include <linux/mm.h>
27#include <linux/highmem.h>
Josef Bacike07f7182008-11-26 01:14:26 -050028#include <linux/hrtimer.h>
Theodore Ts'o47def822010-07-27 11:56:05 -040029#include <linux/backing-dev.h>
30#include <linux/module.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070031
Adrian Bunk7ddae862006-12-06 20:38:27 -080032static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh);
33
Dave Kleikamp470decc2006-10-11 01:20:57 -070034/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -070035 * jbd2_get_transaction: obtain a new transaction_t object.
Dave Kleikamp470decc2006-10-11 01:20:57 -070036 *
37 * Simply allocate and initialise a new transaction. Create it in
38 * RUNNING state and add it to the current journal (which should not
39 * have an existing running transaction: we only make a new transaction
40 * once we have started to commit the old one).
41 *
42 * Preconditions:
43 * The journal MUST be locked. We don't perform atomic mallocs on the
44 * new transaction and we can't block without protecting against other
45 * processes trying to touch the journal while it is in transition.
46 *
Dave Kleikamp470decc2006-10-11 01:20:57 -070047 */
48
49static transaction_t *
Mingming Caof7f4bcc2006-10-11 01:20:59 -070050jbd2_get_transaction(journal_t *journal, transaction_t *transaction)
Dave Kleikamp470decc2006-10-11 01:20:57 -070051{
52 transaction->t_journal = journal;
53 transaction->t_state = T_RUNNING;
Josef Bacike07f7182008-11-26 01:14:26 -050054 transaction->t_start_time = ktime_get();
Dave Kleikamp470decc2006-10-11 01:20:57 -070055 transaction->t_tid = journal->j_transaction_sequence++;
56 transaction->t_expires = jiffies + journal->j_commit_interval;
57 spin_lock_init(&transaction->t_handle_lock);
Theodore Ts'oa51dca92010-08-02 08:43:25 -040058 atomic_set(&transaction->t_updates, 0);
59 atomic_set(&transaction->t_outstanding_credits, 0);
Theodore Ts'o8dd42042010-08-03 21:38:29 -040060 atomic_set(&transaction->t_handle_count, 0);
Jan Karac851ed52008-07-11 19:27:31 -040061 INIT_LIST_HEAD(&transaction->t_inode_list);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -040062 INIT_LIST_HEAD(&transaction->t_private_list);
Dave Kleikamp470decc2006-10-11 01:20:57 -070063
64 /* Set up the commit timer for the new transaction. */
Andreas Dilgerb1f485f2009-08-10 22:51:53 -040065 journal->j_commit_timer.expires = round_jiffies_up(transaction->t_expires);
Dave Kleikamp470decc2006-10-11 01:20:57 -070066 add_timer(&journal->j_commit_timer);
67
68 J_ASSERT(journal->j_running_transaction == NULL);
69 journal->j_running_transaction = transaction;
Johann Lombardi8e85fb32008-01-28 23:58:27 -050070 transaction->t_max_wait = 0;
71 transaction->t_start = jiffies;
Dave Kleikamp470decc2006-10-11 01:20:57 -070072
73 return transaction;
74}
75
76/*
77 * Handle management.
78 *
79 * A handle_t is an object which represents a single atomic update to a
80 * filesystem, and which tracks all of the modifications which form part
81 * of that one update.
82 */
83
84/*
Theodore Ts'o6d0bf002010-08-09 17:28:38 -040085 * Update transiaction's maximum wait time, if debugging is enabled.
86 *
87 * In order for t_max_wait to be reliable, it must be protected by a
88 * lock. But doing so will mean that start_this_handle() can not be
89 * run in parallel on SMP systems, which limits our scalability. So
90 * unless debugging is enabled, we no longer update t_max_wait, which
91 * means that maximum wait time reported by the jbd2_run_stats
92 * tracepoint will always be zero.
93 */
94static inline void update_t_max_wait(transaction_t *transaction)
95{
96#ifdef CONFIG_JBD2_DEBUG
97 unsigned long ts = jiffies;
98
99 if (jbd2_journal_enable_debug &&
100 time_after(transaction->t_start, ts)) {
101 ts = jbd2_time_diff(ts, transaction->t_start);
102 spin_lock(&transaction->t_handle_lock);
103 if (ts > transaction->t_max_wait)
104 transaction->t_max_wait = ts;
105 spin_unlock(&transaction->t_handle_lock);
106 }
107#endif
108}
109
110/*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700111 * start_this_handle: Given a handle, deal with any locking or stalling
112 * needed to make sure that there is enough journal space for the handle
113 * to begin. Attach the handle to a transaction and set up the
114 * transaction's buffer credits.
115 */
116
Theodore Ts'o47def822010-07-27 11:56:05 -0400117static int start_this_handle(journal_t *journal, handle_t *handle,
118 int gfp_mask)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700119{
120 transaction_t *transaction;
121 int needed;
122 int nblocks = handle->h_buffer_credits;
123 transaction_t *new_transaction = NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700124
125 if (nblocks > journal->j_max_transaction_buffers) {
126 printk(KERN_ERR "JBD: %s wants too many credits (%d > %d)\n",
127 current->comm, nblocks,
128 journal->j_max_transaction_buffers);
Theodore Ts'o47def822010-07-27 11:56:05 -0400129 return -ENOSPC;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700130 }
131
132alloc_transaction:
133 if (!journal->j_running_transaction) {
Theodore Ts'o47def822010-07-27 11:56:05 -0400134 new_transaction = kzalloc(sizeof(*new_transaction), gfp_mask);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700135 if (!new_transaction) {
Theodore Ts'o47def822010-07-27 11:56:05 -0400136 /*
137 * If __GFP_FS is not present, then we may be
138 * being called from inside the fs writeback
139 * layer, so we MUST NOT fail. Since
140 * __GFP_NOFAIL is going away, we will arrange
141 * to retry the allocation ourselves.
142 */
143 if ((gfp_mask & __GFP_FS) == 0) {
144 congestion_wait(BLK_RW_ASYNC, HZ/50);
145 goto alloc_transaction;
146 }
147 return -ENOMEM;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700148 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700149 }
150
151 jbd_debug(3, "New handle %p going live.\n", handle);
152
Dave Kleikamp470decc2006-10-11 01:20:57 -0700153 /*
154 * We need to hold j_state_lock until t_updates has been incremented,
155 * for proper journal barrier handling
156 */
Theodore Ts'oa931da62010-08-03 21:35:12 -0400157repeat:
158 read_lock(&journal->j_state_lock);
Theodore Ts'o5c2178e2010-10-27 21:30:04 -0400159 BUG_ON(journal->j_flags & JBD2_UNMOUNT);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700160 if (is_journal_aborted(journal) ||
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700161 (journal->j_errno != 0 && !(journal->j_flags & JBD2_ACK_ERR))) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400162 read_unlock(&journal->j_state_lock);
Theodore Ts'o47def822010-07-27 11:56:05 -0400163 kfree(new_transaction);
164 return -EROFS;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700165 }
166
167 /* Wait on the journal's transaction barrier if necessary */
168 if (journal->j_barrier_count) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400169 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700170 wait_event(journal->j_wait_transaction_locked,
171 journal->j_barrier_count == 0);
172 goto repeat;
173 }
174
175 if (!journal->j_running_transaction) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400176 read_unlock(&journal->j_state_lock);
177 if (!new_transaction)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700178 goto alloc_transaction;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400179 write_lock(&journal->j_state_lock);
180 if (!journal->j_running_transaction) {
181 jbd2_get_transaction(journal, new_transaction);
182 new_transaction = NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700183 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400184 write_unlock(&journal->j_state_lock);
185 goto repeat;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700186 }
187
188 transaction = journal->j_running_transaction;
189
190 /*
191 * If the current transaction is locked down for commit, wait for the
192 * lock to be released.
193 */
194 if (transaction->t_state == T_LOCKED) {
195 DEFINE_WAIT(wait);
196
197 prepare_to_wait(&journal->j_wait_transaction_locked,
198 &wait, TASK_UNINTERRUPTIBLE);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400199 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700200 schedule();
201 finish_wait(&journal->j_wait_transaction_locked, &wait);
202 goto repeat;
203 }
204
205 /*
206 * If there is not enough space left in the log to write all potential
207 * buffers requested by this operation, we need to stall pending a log
208 * checkpoint to free some more log space.
209 */
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400210 needed = atomic_add_return(nblocks,
211 &transaction->t_outstanding_credits);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700212
213 if (needed > journal->j_max_transaction_buffers) {
214 /*
215 * If the current transaction is already too large, then start
216 * to commit it: we can then go back and attach this handle to
217 * a new transaction.
218 */
219 DEFINE_WAIT(wait);
220
221 jbd_debug(2, "Handle %p starting new commit...\n", handle);
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400222 atomic_sub(nblocks, &transaction->t_outstanding_credits);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700223 prepare_to_wait(&journal->j_wait_transaction_locked, &wait,
224 TASK_UNINTERRUPTIBLE);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700225 __jbd2_log_start_commit(journal, transaction->t_tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400226 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700227 schedule();
228 finish_wait(&journal->j_wait_transaction_locked, &wait);
229 goto repeat;
230 }
231
232 /*
233 * The commit code assumes that it can get enough log space
234 * without forcing a checkpoint. This is *critical* for
235 * correctness: a checkpoint of a buffer which is also
236 * associated with a committing transaction creates a deadlock,
237 * so commit simply cannot force through checkpoints.
238 *
239 * We must therefore ensure the necessary space in the journal
240 * *before* starting to dirty potentially checkpointed buffers
241 * in the new transaction.
242 *
243 * The worst part is, any transaction currently committing can
244 * reduce the free space arbitrarily. Be careful to account for
245 * those buffers when checkpointing.
246 */
247
248 /*
249 * @@@ AKPM: This seems rather over-defensive. We're giving commit
250 * a _lot_ of headroom: 1/4 of the journal plus the size of
251 * the committing transaction. Really, we only need to give it
252 * committing_transaction->t_outstanding_credits plus "enough" for
253 * the log control blocks.
Uwe Kleine-Königa34f0b32010-12-10 14:55:42 +0100254 * Also, this test is inconsistent with the matching one in
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700255 * jbd2_journal_extend().
Dave Kleikamp470decc2006-10-11 01:20:57 -0700256 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700257 if (__jbd2_log_space_left(journal) < jbd_space_needed(journal)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700258 jbd_debug(2, "Handle %p waiting for checkpoint...\n", handle);
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400259 atomic_sub(nblocks, &transaction->t_outstanding_credits);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400260 read_unlock(&journal->j_state_lock);
261 write_lock(&journal->j_state_lock);
262 if (__jbd2_log_space_left(journal) < jbd_space_needed(journal))
263 __jbd2_log_wait_for_space(journal);
264 write_unlock(&journal->j_state_lock);
265 goto repeat;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700266 }
267
268 /* OK, account for the buffers that this operation expects to
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400269 * use and add the handle to the running transaction.
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400270 */
Theodore Ts'o6d0bf002010-08-09 17:28:38 -0400271 update_t_max_wait(transaction);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700272 handle->h_transaction = transaction;
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400273 atomic_inc(&transaction->t_updates);
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400274 atomic_inc(&transaction->t_handle_count);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700275 jbd_debug(4, "Handle %p given %d credits (total %d, free %d)\n",
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400276 handle, nblocks,
277 atomic_read(&transaction->t_outstanding_credits),
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700278 __jbd2_log_space_left(journal));
Theodore Ts'oa931da62010-08-03 21:35:12 -0400279 read_unlock(&journal->j_state_lock);
Jan Kara9599b0e2009-08-17 21:23:17 -0400280
281 lock_map_acquire(&handle->h_lockdep_map);
Theodore Ts'o47def822010-07-27 11:56:05 -0400282 kfree(new_transaction);
283 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700284}
285
Mingming Cao7b751062008-01-28 23:58:27 -0500286static struct lock_class_key jbd2_handle_key;
287
Dave Kleikamp470decc2006-10-11 01:20:57 -0700288/* Allocate a new handle. This should probably be in a slab... */
289static handle_t *new_handle(int nblocks)
290{
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400291 handle_t *handle = jbd2_alloc_handle(GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700292 if (!handle)
293 return NULL;
294 memset(handle, 0, sizeof(*handle));
295 handle->h_buffer_credits = nblocks;
296 handle->h_ref = 1;
297
Mingming Cao7b751062008-01-28 23:58:27 -0500298 lockdep_init_map(&handle->h_lockdep_map, "jbd2_handle",
299 &jbd2_handle_key, 0);
300
Dave Kleikamp470decc2006-10-11 01:20:57 -0700301 return handle;
302}
303
304/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700305 * handle_t *jbd2_journal_start() - Obtain a new handle.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700306 * @journal: Journal to start transaction on.
307 * @nblocks: number of block buffer we might modify
308 *
309 * We make sure that the transaction can guarantee at least nblocks of
310 * modified buffers in the log. We block until the log can guarantee
311 * that much space.
312 *
313 * This function is visible to journal users (like ext3fs), so is not
314 * called with the journal already locked.
315 *
316 * Return a pointer to a newly allocated handle, or NULL on failure
317 */
Theodore Ts'o47def822010-07-27 11:56:05 -0400318handle_t *jbd2__journal_start(journal_t *journal, int nblocks, int gfp_mask)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700319{
320 handle_t *handle = journal_current_handle();
321 int err;
322
323 if (!journal)
324 return ERR_PTR(-EROFS);
325
326 if (handle) {
327 J_ASSERT(handle->h_transaction->t_journal == journal);
328 handle->h_ref++;
329 return handle;
330 }
331
332 handle = new_handle(nblocks);
333 if (!handle)
334 return ERR_PTR(-ENOMEM);
335
336 current->journal_info = handle;
337
Theodore Ts'o47def822010-07-27 11:56:05 -0400338 err = start_this_handle(journal, handle, gfp_mask);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700339 if (err < 0) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400340 jbd2_free_handle(handle);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700341 current->journal_info = NULL;
342 handle = ERR_PTR(err);
343 }
344 return handle;
345}
Theodore Ts'o47def822010-07-27 11:56:05 -0400346EXPORT_SYMBOL(jbd2__journal_start);
347
348
349handle_t *jbd2_journal_start(journal_t *journal, int nblocks)
350{
351 return jbd2__journal_start(journal, nblocks, GFP_NOFS);
352}
353EXPORT_SYMBOL(jbd2_journal_start);
354
Dave Kleikamp470decc2006-10-11 01:20:57 -0700355
356/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700357 * int jbd2_journal_extend() - extend buffer credits.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700358 * @handle: handle to 'extend'
359 * @nblocks: nr blocks to try to extend by.
360 *
361 * Some transactions, such as large extends and truncates, can be done
362 * atomically all at once or in several stages. The operation requests
363 * a credit for a number of buffer modications in advance, but can
364 * extend its credit if it needs more.
365 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700366 * jbd2_journal_extend tries to give the running handle more buffer credits.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700367 * It does not guarantee that allocation - this is a best-effort only.
368 * The calling process MUST be able to deal cleanly with a failure to
369 * extend here.
370 *
371 * Return 0 on success, non-zero on failure.
372 *
373 * return code < 0 implies an error
374 * return code > 0 implies normal transaction-full status.
375 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700376int jbd2_journal_extend(handle_t *handle, int nblocks)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700377{
378 transaction_t *transaction = handle->h_transaction;
379 journal_t *journal = transaction->t_journal;
380 int result;
381 int wanted;
382
383 result = -EIO;
384 if (is_handle_aborted(handle))
385 goto out;
386
387 result = 1;
388
Theodore Ts'oa931da62010-08-03 21:35:12 -0400389 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700390
391 /* Don't extend a locked-down transaction! */
392 if (handle->h_transaction->t_state != T_RUNNING) {
393 jbd_debug(3, "denied handle %p %d blocks: "
394 "transaction not running\n", handle, nblocks);
395 goto error_out;
396 }
397
398 spin_lock(&transaction->t_handle_lock);
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400399 wanted = atomic_read(&transaction->t_outstanding_credits) + nblocks;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700400
401 if (wanted > journal->j_max_transaction_buffers) {
402 jbd_debug(3, "denied handle %p %d blocks: "
403 "transaction too large\n", handle, nblocks);
404 goto unlock;
405 }
406
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700407 if (wanted > __jbd2_log_space_left(journal)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700408 jbd_debug(3, "denied handle %p %d blocks: "
409 "insufficient log space\n", handle, nblocks);
410 goto unlock;
411 }
412
413 handle->h_buffer_credits += nblocks;
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400414 atomic_add(nblocks, &transaction->t_outstanding_credits);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700415 result = 0;
416
417 jbd_debug(3, "extended handle %p by %d\n", handle, nblocks);
418unlock:
419 spin_unlock(&transaction->t_handle_lock);
420error_out:
Theodore Ts'oa931da62010-08-03 21:35:12 -0400421 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700422out:
423 return result;
424}
425
426
427/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700428 * int jbd2_journal_restart() - restart a handle .
Dave Kleikamp470decc2006-10-11 01:20:57 -0700429 * @handle: handle to restart
430 * @nblocks: nr credits requested
431 *
432 * Restart a handle for a multi-transaction filesystem
433 * operation.
434 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700435 * If the jbd2_journal_extend() call above fails to grant new buffer credits
436 * to a running handle, a call to jbd2_journal_restart will commit the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700437 * handle's transaction so far and reattach the handle to a new
438 * transaction capabable of guaranteeing the requested number of
439 * credits.
440 */
Theodore Ts'o47def822010-07-27 11:56:05 -0400441int jbd2__journal_restart(handle_t *handle, int nblocks, int gfp_mask)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700442{
443 transaction_t *transaction = handle->h_transaction;
444 journal_t *journal = transaction->t_journal;
445 int ret;
446
447 /* If we've had an abort of any type, don't even think about
448 * actually doing the restart! */
449 if (is_handle_aborted(handle))
450 return 0;
451
452 /*
453 * First unlink the handle from its current transaction, and start the
454 * commit on that.
455 */
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400456 J_ASSERT(atomic_read(&transaction->t_updates) > 0);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700457 J_ASSERT(journal_current_handle() == handle);
458
Theodore Ts'oa931da62010-08-03 21:35:12 -0400459 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700460 spin_lock(&transaction->t_handle_lock);
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400461 atomic_sub(handle->h_buffer_credits,
462 &transaction->t_outstanding_credits);
463 if (atomic_dec_and_test(&transaction->t_updates))
Dave Kleikamp470decc2006-10-11 01:20:57 -0700464 wake_up(&journal->j_wait_updates);
465 spin_unlock(&transaction->t_handle_lock);
466
467 jbd_debug(2, "restarting handle %p\n", handle);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700468 __jbd2_log_start_commit(journal, transaction->t_tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400469 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700470
Jan Kara9599b0e2009-08-17 21:23:17 -0400471 lock_map_release(&handle->h_lockdep_map);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700472 handle->h_buffer_credits = nblocks;
Theodore Ts'o47def822010-07-27 11:56:05 -0400473 ret = start_this_handle(journal, handle, gfp_mask);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700474 return ret;
475}
Theodore Ts'o47def822010-07-27 11:56:05 -0400476EXPORT_SYMBOL(jbd2__journal_restart);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700477
478
Theodore Ts'o47def822010-07-27 11:56:05 -0400479int jbd2_journal_restart(handle_t *handle, int nblocks)
480{
481 return jbd2__journal_restart(handle, nblocks, GFP_NOFS);
482}
483EXPORT_SYMBOL(jbd2_journal_restart);
484
Dave Kleikamp470decc2006-10-11 01:20:57 -0700485/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700486 * void jbd2_journal_lock_updates () - establish a transaction barrier.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700487 * @journal: Journal to establish a barrier on.
488 *
489 * This locks out any further updates from being started, and blocks
490 * until all existing updates have completed, returning only once the
491 * journal is in a quiescent state with no updates running.
492 *
493 * The journal lock should not be held on entry.
494 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700495void jbd2_journal_lock_updates(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700496{
497 DEFINE_WAIT(wait);
498
Theodore Ts'oa931da62010-08-03 21:35:12 -0400499 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700500 ++journal->j_barrier_count;
501
502 /* Wait until there are no running updates */
503 while (1) {
504 transaction_t *transaction = journal->j_running_transaction;
505
506 if (!transaction)
507 break;
508
509 spin_lock(&transaction->t_handle_lock);
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400510 if (!atomic_read(&transaction->t_updates)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700511 spin_unlock(&transaction->t_handle_lock);
512 break;
513 }
514 prepare_to_wait(&journal->j_wait_updates, &wait,
515 TASK_UNINTERRUPTIBLE);
516 spin_unlock(&transaction->t_handle_lock);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400517 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700518 schedule();
519 finish_wait(&journal->j_wait_updates, &wait);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400520 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700521 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400522 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700523
524 /*
525 * We have now established a barrier against other normal updates, but
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700526 * we also need to barrier against other jbd2_journal_lock_updates() calls
Dave Kleikamp470decc2006-10-11 01:20:57 -0700527 * to make sure that we serialise special journal-locked operations
528 * too.
529 */
530 mutex_lock(&journal->j_barrier);
531}
532
533/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700534 * void jbd2_journal_unlock_updates (journal_t* journal) - release barrier
Dave Kleikamp470decc2006-10-11 01:20:57 -0700535 * @journal: Journal to release the barrier on.
536 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700537 * Release a transaction barrier obtained with jbd2_journal_lock_updates().
Dave Kleikamp470decc2006-10-11 01:20:57 -0700538 *
539 * Should be called without the journal lock held.
540 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700541void jbd2_journal_unlock_updates (journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700542{
543 J_ASSERT(journal->j_barrier_count != 0);
544
545 mutex_unlock(&journal->j_barrier);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400546 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700547 --journal->j_barrier_count;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400548 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700549 wake_up(&journal->j_wait_transaction_locked);
550}
551
Jan Karaf91d1d02009-07-13 16:16:20 -0400552static void warn_dirty_buffer(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700553{
Jan Karaf91d1d02009-07-13 16:16:20 -0400554 char b[BDEVNAME_SIZE];
Dave Kleikamp470decc2006-10-11 01:20:57 -0700555
Jan Karaf91d1d02009-07-13 16:16:20 -0400556 printk(KERN_WARNING
557 "JBD: Spotted dirty metadata buffer (dev = %s, blocknr = %llu). "
558 "There's a risk of filesystem corruption in case of system "
559 "crash.\n",
560 bdevname(bh->b_bdev, b), (unsigned long long)bh->b_blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700561}
562
563/*
564 * If the buffer is already part of the current transaction, then there
565 * is nothing we need to do. If it is already part of a prior
566 * transaction which we are still committing to disk, then we need to
567 * make sure that we do not overwrite the old copy: we do copy-out to
568 * preserve the copy going to disk. We also account the buffer against
569 * the handle's metadata buffer credits (unless the buffer is already
570 * part of the transaction, that is).
571 *
572 */
573static int
574do_get_write_access(handle_t *handle, struct journal_head *jh,
575 int force_copy)
576{
577 struct buffer_head *bh;
578 transaction_t *transaction;
579 journal_t *journal;
580 int error;
581 char *frozen_buffer = NULL;
582 int need_copy = 0;
583
584 if (is_handle_aborted(handle))
585 return -EROFS;
586
587 transaction = handle->h_transaction;
588 journal = transaction->t_journal;
589
Theodore Ts'ocfef2c62010-12-18 13:07:34 -0500590 jbd_debug(5, "journal_head %p, force_copy %d\n", jh, force_copy);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700591
592 JBUFFER_TRACE(jh, "entry");
593repeat:
594 bh = jh2bh(jh);
595
596 /* @@@ Need to check for errors here at some point. */
597
598 lock_buffer(bh);
599 jbd_lock_bh_state(bh);
600
601 /* We now hold the buffer lock so it is safe to query the buffer
602 * state. Is the buffer dirty?
603 *
604 * If so, there are two possibilities. The buffer may be
605 * non-journaled, and undergoing a quite legitimate writeback.
606 * Otherwise, it is journaled, and we don't expect dirty buffers
607 * in that state (the buffers should be marked JBD_Dirty
608 * instead.) So either the IO is being done under our own
609 * control and this is a bug, or it's a third party IO such as
610 * dump(8) (which may leave the buffer scheduled for read ---
611 * ie. locked but not dirty) or tune2fs (which may actually have
612 * the buffer dirtied, ugh.) */
613
614 if (buffer_dirty(bh)) {
615 /*
616 * First question: is this buffer already part of the current
617 * transaction or the existing committing transaction?
618 */
619 if (jh->b_transaction) {
620 J_ASSERT_JH(jh,
621 jh->b_transaction == transaction ||
622 jh->b_transaction ==
623 journal->j_committing_transaction);
624 if (jh->b_next_transaction)
625 J_ASSERT_JH(jh, jh->b_next_transaction ==
626 transaction);
Jan Karaf91d1d02009-07-13 16:16:20 -0400627 warn_dirty_buffer(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700628 }
629 /*
630 * In any case we need to clean the dirty flag and we must
631 * do it under the buffer lock to be sure we don't race
632 * with running write-out.
633 */
Jan Karaf91d1d02009-07-13 16:16:20 -0400634 JBUFFER_TRACE(jh, "Journalling dirty buffer");
635 clear_buffer_dirty(bh);
636 set_buffer_jbddirty(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700637 }
638
639 unlock_buffer(bh);
640
641 error = -EROFS;
642 if (is_handle_aborted(handle)) {
643 jbd_unlock_bh_state(bh);
644 goto out;
645 }
646 error = 0;
647
648 /*
649 * The buffer is already part of this transaction if b_transaction or
650 * b_next_transaction points to it
651 */
652 if (jh->b_transaction == transaction ||
653 jh->b_next_transaction == transaction)
654 goto done;
655
656 /*
Josef Bacik9fc7c632008-04-17 10:38:59 -0400657 * this is the first time this transaction is touching this buffer,
658 * reset the modified flag
659 */
660 jh->b_modified = 0;
661
662 /*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700663 * If there is already a copy-out version of this buffer, then we don't
664 * need to make another one
665 */
666 if (jh->b_frozen_data) {
667 JBUFFER_TRACE(jh, "has frozen data");
668 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
669 jh->b_next_transaction = transaction;
670 goto done;
671 }
672
673 /* Is there data here we need to preserve? */
674
675 if (jh->b_transaction && jh->b_transaction != transaction) {
676 JBUFFER_TRACE(jh, "owned by older transaction");
677 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
678 J_ASSERT_JH(jh, jh->b_transaction ==
679 journal->j_committing_transaction);
680
681 /* There is one case we have to be very careful about.
682 * If the committing transaction is currently writing
683 * this buffer out to disk and has NOT made a copy-out,
684 * then we cannot modify the buffer contents at all
685 * right now. The essence of copy-out is that it is the
686 * extra copy, not the primary copy, which gets
687 * journaled. If the primary copy is already going to
688 * disk then we cannot do copy-out here. */
689
690 if (jh->b_jlist == BJ_Shadow) {
691 DEFINE_WAIT_BIT(wait, &bh->b_state, BH_Unshadow);
692 wait_queue_head_t *wqh;
693
694 wqh = bit_waitqueue(&bh->b_state, BH_Unshadow);
695
696 JBUFFER_TRACE(jh, "on shadow: sleep");
697 jbd_unlock_bh_state(bh);
698 /* commit wakes up all shadow buffers after IO */
699 for ( ; ; ) {
700 prepare_to_wait(wqh, &wait.wait,
701 TASK_UNINTERRUPTIBLE);
702 if (jh->b_jlist != BJ_Shadow)
703 break;
704 schedule();
705 }
706 finish_wait(wqh, &wait.wait);
707 goto repeat;
708 }
709
710 /* Only do the copy if the currently-owning transaction
711 * still needs it. If it is on the Forget list, the
712 * committing transaction is past that stage. The
713 * buffer had better remain locked during the kmalloc,
714 * but that should be true --- we hold the journal lock
715 * still and the buffer is already on the BUF_JOURNAL
716 * list so won't be flushed.
717 *
718 * Subtle point, though: if this is a get_undo_access,
719 * then we will be relying on the frozen_data to contain
720 * the new value of the committed_data record after the
721 * transaction, so we HAVE to force the frozen_data copy
722 * in that case. */
723
724 if (jh->b_jlist != BJ_Forget || force_copy) {
725 JBUFFER_TRACE(jh, "generate frozen data");
726 if (!frozen_buffer) {
727 JBUFFER_TRACE(jh, "allocate memory for buffer");
728 jbd_unlock_bh_state(bh);
729 frozen_buffer =
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400730 jbd2_alloc(jh2bh(jh)->b_size,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700731 GFP_NOFS);
732 if (!frozen_buffer) {
733 printk(KERN_EMERG
734 "%s: OOM for frozen_buffer\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400735 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700736 JBUFFER_TRACE(jh, "oom!");
737 error = -ENOMEM;
738 jbd_lock_bh_state(bh);
739 goto done;
740 }
741 goto repeat;
742 }
743 jh->b_frozen_data = frozen_buffer;
744 frozen_buffer = NULL;
745 need_copy = 1;
746 }
747 jh->b_next_transaction = transaction;
748 }
749
750
751 /*
752 * Finally, if the buffer is not journaled right now, we need to make
753 * sure it doesn't get written to disk before the caller actually
754 * commits the new data
755 */
756 if (!jh->b_transaction) {
757 JBUFFER_TRACE(jh, "no transaction");
758 J_ASSERT_JH(jh, !jh->b_next_transaction);
759 jh->b_transaction = transaction;
760 JBUFFER_TRACE(jh, "file as BJ_Reserved");
761 spin_lock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700762 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700763 spin_unlock(&journal->j_list_lock);
764 }
765
766done:
767 if (need_copy) {
768 struct page *page;
769 int offset;
770 char *source;
771
772 J_EXPECT_JH(jh, buffer_uptodate(jh2bh(jh)),
773 "Possible IO failure.\n");
774 page = jh2bh(jh)->b_page;
Theodore Ts'oa1dd5332010-12-18 13:13:40 -0500775 offset = offset_in_page(jh2bh(jh)->b_data);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700776 source = kmap_atomic(page, KM_USER0);
Jan Kara13ceef02010-07-14 07:56:33 +0200777 /* Fire data frozen trigger just before we copy the data */
778 jbd2_buffer_frozen_trigger(jh, source + offset,
779 jh->b_triggers);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700780 memcpy(jh->b_frozen_data, source+offset, jh2bh(jh)->b_size);
781 kunmap_atomic(source, KM_USER0);
Joel Beckere06c8222008-09-11 15:35:47 -0700782
783 /*
784 * Now that the frozen data is saved off, we need to store
785 * any matching triggers.
786 */
787 jh->b_frozen_triggers = jh->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700788 }
789 jbd_unlock_bh_state(bh);
790
791 /*
792 * If we are about to journal a buffer, then any revoke pending on it is
793 * no longer valid
794 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700795 jbd2_journal_cancel_revoke(handle, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700796
797out:
798 if (unlikely(frozen_buffer)) /* It's usually NULL */
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400799 jbd2_free(frozen_buffer, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700800
801 JBUFFER_TRACE(jh, "exit");
802 return error;
803}
804
805/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700806 * int jbd2_journal_get_write_access() - notify intent to modify a buffer for metadata (not data) update.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700807 * @handle: transaction to add buffer modifications to
808 * @bh: bh to be used for metadata writes
809 * @credits: variable that will receive credits for the buffer
810 *
811 * Returns an error code or 0 on success.
812 *
813 * In full data journalling mode the buffer may be of type BJ_AsyncData,
814 * because we're write()ing a buffer which is also part of a shared mapping.
815 */
816
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700817int jbd2_journal_get_write_access(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700818{
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700819 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700820 int rc;
821
822 /* We do not want to get caught playing with fields which the
823 * log thread also manipulates. Make sure that the buffer
824 * completes any outstanding IO before proceeding. */
825 rc = do_get_write_access(handle, jh, 0);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700826 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700827 return rc;
828}
829
830
831/*
832 * When the user wants to journal a newly created buffer_head
833 * (ie. getblk() returned a new buffer and we are going to populate it
834 * manually rather than reading off disk), then we need to keep the
835 * buffer_head locked until it has been completely filled with new
836 * data. In this case, we should be able to make the assertion that
837 * the bh is not already part of an existing transaction.
838 *
839 * The buffer should already be locked by the caller by this point.
840 * There is no lock ranking violation: it was a newly created,
841 * unlocked buffer beforehand. */
842
843/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700844 * int jbd2_journal_get_create_access () - notify intent to use newly created bh
Dave Kleikamp470decc2006-10-11 01:20:57 -0700845 * @handle: transaction to new buffer to
846 * @bh: new buffer.
847 *
848 * Call this if you create a new bh.
849 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700850int jbd2_journal_get_create_access(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700851{
852 transaction_t *transaction = handle->h_transaction;
853 journal_t *journal = transaction->t_journal;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700854 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700855 int err;
856
857 jbd_debug(5, "journal_head %p\n", jh);
858 err = -EROFS;
859 if (is_handle_aborted(handle))
860 goto out;
861 err = 0;
862
863 JBUFFER_TRACE(jh, "entry");
864 /*
865 * The buffer may already belong to this transaction due to pre-zeroing
866 * in the filesystem's new_block code. It may also be on the previous,
867 * committing transaction's lists, but it HAS to be in Forget state in
868 * that case: the transaction must have deleted the buffer for it to be
869 * reused here.
870 */
871 jbd_lock_bh_state(bh);
872 spin_lock(&journal->j_list_lock);
873 J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
874 jh->b_transaction == NULL ||
875 (jh->b_transaction == journal->j_committing_transaction &&
876 jh->b_jlist == BJ_Forget)));
877
878 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
879 J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
880
881 if (jh->b_transaction == NULL) {
Jan Karaf91d1d02009-07-13 16:16:20 -0400882 /*
883 * Previous jbd2_journal_forget() could have left the buffer
884 * with jbddirty bit set because it was being committed. When
885 * the commit finished, we've filed the buffer for
886 * checkpointing and marked it dirty. Now we are reallocating
887 * the buffer so the transaction freeing it must have
888 * committed and so it's safe to clear the dirty bit.
889 */
890 clear_buffer_dirty(jh2bh(jh));
Dave Kleikamp470decc2006-10-11 01:20:57 -0700891 jh->b_transaction = transaction;
Josef Bacik9fc7c632008-04-17 10:38:59 -0400892
893 /* first access by this transaction */
894 jh->b_modified = 0;
895
Dave Kleikamp470decc2006-10-11 01:20:57 -0700896 JBUFFER_TRACE(jh, "file as BJ_Reserved");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700897 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700898 } else if (jh->b_transaction == journal->j_committing_transaction) {
Josef Bacik9fc7c632008-04-17 10:38:59 -0400899 /* first access by this transaction */
900 jh->b_modified = 0;
901
Dave Kleikamp470decc2006-10-11 01:20:57 -0700902 JBUFFER_TRACE(jh, "set next transaction");
903 jh->b_next_transaction = transaction;
904 }
905 spin_unlock(&journal->j_list_lock);
906 jbd_unlock_bh_state(bh);
907
908 /*
909 * akpm: I added this. ext3_alloc_branch can pick up new indirect
910 * blocks which contain freed but then revoked metadata. We need
911 * to cancel the revoke in case we end up freeing it yet again
912 * and the reallocating as data - this would cause a second revoke,
913 * which hits an assertion error.
914 */
915 JBUFFER_TRACE(jh, "cancelling revoke");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700916 jbd2_journal_cancel_revoke(handle, jh);
917 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700918out:
919 return err;
920}
921
922/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700923 * int jbd2_journal_get_undo_access() - Notify intent to modify metadata with
Dave Kleikamp470decc2006-10-11 01:20:57 -0700924 * non-rewindable consequences
925 * @handle: transaction
926 * @bh: buffer to undo
927 * @credits: store the number of taken credits here (if not NULL)
928 *
929 * Sometimes there is a need to distinguish between metadata which has
930 * been committed to disk and that which has not. The ext3fs code uses
931 * this for freeing and allocating space, we have to make sure that we
932 * do not reuse freed space until the deallocation has been committed,
933 * since if we overwrote that space we would make the delete
934 * un-rewindable in case of a crash.
935 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700936 * To deal with that, jbd2_journal_get_undo_access requests write access to a
Dave Kleikamp470decc2006-10-11 01:20:57 -0700937 * buffer for parts of non-rewindable operations such as delete
938 * operations on the bitmaps. The journaling code must keep a copy of
939 * the buffer's contents prior to the undo_access call until such time
940 * as we know that the buffer has definitely been committed to disk.
941 *
942 * We never need to know which transaction the committed data is part
943 * of, buffers touched here are guaranteed to be dirtied later and so
944 * will be committed to a new transaction in due course, at which point
945 * we can discard the old committed data pointer.
946 *
947 * Returns error number or 0 on success.
948 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700949int jbd2_journal_get_undo_access(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700950{
951 int err;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700952 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700953 char *committed_data = NULL;
954
955 JBUFFER_TRACE(jh, "entry");
956
957 /*
958 * Do this first --- it can drop the journal lock, so we want to
959 * make sure that obtaining the committed_data is done
960 * atomically wrt. completion of any outstanding commits.
961 */
962 err = do_get_write_access(handle, jh, 1);
963 if (err)
964 goto out;
965
966repeat:
967 if (!jh->b_committed_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400968 committed_data = jbd2_alloc(jh2bh(jh)->b_size, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700969 if (!committed_data) {
970 printk(KERN_EMERG "%s: No memory for committed data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400971 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700972 err = -ENOMEM;
973 goto out;
974 }
975 }
976
977 jbd_lock_bh_state(bh);
978 if (!jh->b_committed_data) {
979 /* Copy out the current buffer contents into the
980 * preserved, committed copy. */
981 JBUFFER_TRACE(jh, "generate b_committed data");
982 if (!committed_data) {
983 jbd_unlock_bh_state(bh);
984 goto repeat;
985 }
986
987 jh->b_committed_data = committed_data;
988 committed_data = NULL;
989 memcpy(jh->b_committed_data, bh->b_data, bh->b_size);
990 }
991 jbd_unlock_bh_state(bh);
992out:
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700993 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700994 if (unlikely(committed_data))
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400995 jbd2_free(committed_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700996 return err;
997}
998
999/**
Joel Beckere06c8222008-09-11 15:35:47 -07001000 * void jbd2_journal_set_triggers() - Add triggers for commit writeout
1001 * @bh: buffer to trigger on
1002 * @type: struct jbd2_buffer_trigger_type containing the trigger(s).
1003 *
1004 * Set any triggers on this journal_head. This is always safe, because
1005 * triggers for a committing buffer will be saved off, and triggers for
1006 * a running transaction will match the buffer in that transaction.
1007 *
1008 * Call with NULL to clear the triggers.
1009 */
1010void jbd2_journal_set_triggers(struct buffer_head *bh,
1011 struct jbd2_buffer_trigger_type *type)
1012{
1013 struct journal_head *jh = bh2jh(bh);
1014
1015 jh->b_triggers = type;
1016}
1017
Jan Kara13ceef02010-07-14 07:56:33 +02001018void jbd2_buffer_frozen_trigger(struct journal_head *jh, void *mapped_data,
Joel Beckere06c8222008-09-11 15:35:47 -07001019 struct jbd2_buffer_trigger_type *triggers)
1020{
1021 struct buffer_head *bh = jh2bh(jh);
1022
Jan Kara13ceef02010-07-14 07:56:33 +02001023 if (!triggers || !triggers->t_frozen)
Joel Beckere06c8222008-09-11 15:35:47 -07001024 return;
1025
Jan Kara13ceef02010-07-14 07:56:33 +02001026 triggers->t_frozen(triggers, bh, mapped_data, bh->b_size);
Joel Beckere06c8222008-09-11 15:35:47 -07001027}
1028
1029void jbd2_buffer_abort_trigger(struct journal_head *jh,
1030 struct jbd2_buffer_trigger_type *triggers)
1031{
1032 if (!triggers || !triggers->t_abort)
1033 return;
1034
1035 triggers->t_abort(triggers, jh2bh(jh));
1036}
1037
1038
1039
1040/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001041 * int jbd2_journal_dirty_metadata() - mark a buffer as containing dirty metadata
Dave Kleikamp470decc2006-10-11 01:20:57 -07001042 * @handle: transaction to add buffer to.
1043 * @bh: buffer to mark
1044 *
1045 * mark dirty metadata which needs to be journaled as part of the current
1046 * transaction.
1047 *
1048 * The buffer is placed on the transaction's metadata list and is marked
1049 * as belonging to the transaction.
1050 *
1051 * Returns error number or 0 on success.
1052 *
1053 * Special care needs to be taken if the buffer already belongs to the
1054 * current committing transaction (in which case we should have frozen
1055 * data present for that commit). In that case, we don't relink the
1056 * buffer: that only gets done when the old transaction finally
1057 * completes its commit.
1058 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001059int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001060{
1061 transaction_t *transaction = handle->h_transaction;
1062 journal_t *journal = transaction->t_journal;
1063 struct journal_head *jh = bh2jh(bh);
1064
1065 jbd_debug(5, "journal_head %p\n", jh);
1066 JBUFFER_TRACE(jh, "entry");
1067 if (is_handle_aborted(handle))
1068 goto out;
1069
1070 jbd_lock_bh_state(bh);
1071
1072 if (jh->b_modified == 0) {
1073 /*
1074 * This buffer's got modified and becoming part
1075 * of the transaction. This needs to be done
1076 * once a transaction -bzzz
1077 */
1078 jh->b_modified = 1;
1079 J_ASSERT_JH(jh, handle->h_buffer_credits > 0);
1080 handle->h_buffer_credits--;
1081 }
1082
1083 /*
1084 * fastpath, to avoid expensive locking. If this buffer is already
1085 * on the running transaction's metadata list there is nothing to do.
1086 * Nobody can take it off again because there is a handle open.
1087 * I _think_ we're OK here with SMP barriers - a mistaken decision will
1088 * result in this test being false, so we go in and take the locks.
1089 */
1090 if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) {
1091 JBUFFER_TRACE(jh, "fastpath");
1092 J_ASSERT_JH(jh, jh->b_transaction ==
1093 journal->j_running_transaction);
1094 goto out_unlock_bh;
1095 }
1096
1097 set_buffer_jbddirty(bh);
1098
1099 /*
1100 * Metadata already on the current transaction list doesn't
1101 * need to be filed. Metadata on another transaction's list must
1102 * be committing, and will be refiled once the commit completes:
1103 * leave it alone for now.
1104 */
1105 if (jh->b_transaction != transaction) {
1106 JBUFFER_TRACE(jh, "already on other transaction");
1107 J_ASSERT_JH(jh, jh->b_transaction ==
1108 journal->j_committing_transaction);
1109 J_ASSERT_JH(jh, jh->b_next_transaction == transaction);
1110 /* And this case is illegal: we can't reuse another
1111 * transaction's data buffer, ever. */
1112 goto out_unlock_bh;
1113 }
1114
1115 /* That test should have eliminated the following case: */
Mingming Cao40191912008-01-28 23:58:27 -05001116 J_ASSERT_JH(jh, jh->b_frozen_data == NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001117
1118 JBUFFER_TRACE(jh, "file as BJ_Metadata");
1119 spin_lock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001120 __jbd2_journal_file_buffer(jh, handle->h_transaction, BJ_Metadata);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001121 spin_unlock(&journal->j_list_lock);
1122out_unlock_bh:
1123 jbd_unlock_bh_state(bh);
1124out:
1125 JBUFFER_TRACE(jh, "exit");
1126 return 0;
1127}
1128
1129/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001130 * jbd2_journal_release_buffer: undo a get_write_access without any buffer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001131 * updates, if the update decided in the end that it didn't need access.
1132 *
1133 */
1134void
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001135jbd2_journal_release_buffer(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001136{
1137 BUFFER_TRACE(bh, "entry");
1138}
1139
1140/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001141 * void jbd2_journal_forget() - bforget() for potentially-journaled buffers.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001142 * @handle: transaction handle
1143 * @bh: bh to 'forget'
1144 *
1145 * We can only do the bforget if there are no commits pending against the
1146 * buffer. If the buffer is dirty in the current running transaction we
1147 * can safely unlink it.
1148 *
1149 * bh may not be a journalled buffer at all - it may be a non-JBD
1150 * buffer which came off the hashtable. Check for this.
1151 *
1152 * Decrements bh->b_count by one.
1153 *
1154 * Allow this call even if the handle has aborted --- it may be part of
1155 * the caller's cleanup after an abort.
1156 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001157int jbd2_journal_forget (handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001158{
1159 transaction_t *transaction = handle->h_transaction;
1160 journal_t *journal = transaction->t_journal;
1161 struct journal_head *jh;
1162 int drop_reserve = 0;
1163 int err = 0;
Josef Bacik1dfc3222008-04-17 10:38:59 -04001164 int was_modified = 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001165
1166 BUFFER_TRACE(bh, "entry");
1167
1168 jbd_lock_bh_state(bh);
1169 spin_lock(&journal->j_list_lock);
1170
1171 if (!buffer_jbd(bh))
1172 goto not_jbd;
1173 jh = bh2jh(bh);
1174
1175 /* Critical error: attempting to delete a bitmap buffer, maybe?
1176 * Don't do any jbd operations, and return an error. */
1177 if (!J_EXPECT_JH(jh, !jh->b_committed_data,
1178 "inconsistent data on disk")) {
1179 err = -EIO;
1180 goto not_jbd;
1181 }
1182
Josef Bacik1dfc3222008-04-17 10:38:59 -04001183 /* keep track of wether or not this transaction modified us */
1184 was_modified = jh->b_modified;
1185
Dave Kleikamp470decc2006-10-11 01:20:57 -07001186 /*
1187 * The buffer's going from the transaction, we must drop
1188 * all references -bzzz
1189 */
1190 jh->b_modified = 0;
1191
1192 if (jh->b_transaction == handle->h_transaction) {
1193 J_ASSERT_JH(jh, !jh->b_frozen_data);
1194
1195 /* If we are forgetting a buffer which is already part
1196 * of this transaction, then we can just drop it from
1197 * the transaction immediately. */
1198 clear_buffer_dirty(bh);
1199 clear_buffer_jbddirty(bh);
1200
1201 JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
1202
Josef Bacik1dfc3222008-04-17 10:38:59 -04001203 /*
1204 * we only want to drop a reference if this transaction
1205 * modified the buffer
1206 */
1207 if (was_modified)
1208 drop_reserve = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001209
1210 /*
1211 * We are no longer going to journal this buffer.
1212 * However, the commit of this transaction is still
1213 * important to the buffer: the delete that we are now
1214 * processing might obsolete an old log entry, so by
1215 * committing, we can satisfy the buffer's checkpoint.
1216 *
1217 * So, if we have a checkpoint on the buffer, we should
1218 * now refile the buffer on our BJ_Forget list so that
1219 * we know to remove the checkpoint after we commit.
1220 */
1221
1222 if (jh->b_cp_transaction) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001223 __jbd2_journal_temp_unlink_buffer(jh);
1224 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001225 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001226 __jbd2_journal_unfile_buffer(jh);
1227 jbd2_journal_remove_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001228 __brelse(bh);
1229 if (!buffer_jbd(bh)) {
1230 spin_unlock(&journal->j_list_lock);
1231 jbd_unlock_bh_state(bh);
1232 __bforget(bh);
1233 goto drop;
1234 }
1235 }
1236 } else if (jh->b_transaction) {
1237 J_ASSERT_JH(jh, (jh->b_transaction ==
1238 journal->j_committing_transaction));
1239 /* However, if the buffer is still owned by a prior
1240 * (committing) transaction, we can't drop it yet... */
1241 JBUFFER_TRACE(jh, "belongs to older transaction");
1242 /* ... but we CAN drop it from the new transaction if we
1243 * have also modified it since the original commit. */
1244
1245 if (jh->b_next_transaction) {
1246 J_ASSERT(jh->b_next_transaction == transaction);
1247 jh->b_next_transaction = NULL;
Josef Bacik1dfc3222008-04-17 10:38:59 -04001248
1249 /*
1250 * only drop a reference if this transaction modified
1251 * the buffer
1252 */
1253 if (was_modified)
1254 drop_reserve = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001255 }
1256 }
1257
1258not_jbd:
1259 spin_unlock(&journal->j_list_lock);
1260 jbd_unlock_bh_state(bh);
1261 __brelse(bh);
1262drop:
1263 if (drop_reserve) {
1264 /* no need to reserve log space for this block -bzzz */
1265 handle->h_buffer_credits++;
1266 }
1267 return err;
1268}
1269
1270/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001271 * int jbd2_journal_stop() - complete a transaction
Dave Kleikamp470decc2006-10-11 01:20:57 -07001272 * @handle: tranaction to complete.
1273 *
1274 * All done for a particular handle.
1275 *
1276 * There is not much action needed here. We just return any remaining
1277 * buffer credits to the transaction and remove the handle. The only
1278 * complication is that we need to start a commit operation if the
1279 * filesystem is marked for synchronous update.
1280 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001281 * jbd2_journal_stop itself will not usually return an error, but it may
Dave Kleikamp470decc2006-10-11 01:20:57 -07001282 * do so in unusual circumstances. In particular, expect it to
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001283 * return -EIO if a jbd2_journal_abort has been executed since the
Dave Kleikamp470decc2006-10-11 01:20:57 -07001284 * transaction began.
1285 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001286int jbd2_journal_stop(handle_t *handle)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001287{
1288 transaction_t *transaction = handle->h_transaction;
1289 journal_t *journal = transaction->t_journal;
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001290 int err, wait_for_commit = 0;
1291 tid_t tid;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001292 pid_t pid;
1293
Dave Kleikamp470decc2006-10-11 01:20:57 -07001294 J_ASSERT(journal_current_handle() == handle);
1295
1296 if (is_handle_aborted(handle))
1297 err = -EIO;
OGAWA Hirofumi3e2a5322006-10-19 23:29:11 -07001298 else {
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001299 J_ASSERT(atomic_read(&transaction->t_updates) > 0);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001300 err = 0;
OGAWA Hirofumi3e2a5322006-10-19 23:29:11 -07001301 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001302
1303 if (--handle->h_ref > 0) {
1304 jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
1305 handle->h_ref);
1306 return err;
1307 }
1308
1309 jbd_debug(4, "Handle %p going down\n", handle);
1310
1311 /*
1312 * Implement synchronous transaction batching. If the handle
1313 * was synchronous, don't force a commit immediately. Let's
Josef Bacike07f7182008-11-26 01:14:26 -05001314 * yield and let another thread piggyback onto this
1315 * transaction. Keep doing that while new threads continue to
1316 * arrive. It doesn't cost much - we're about to run a commit
1317 * and sleep on IO anyway. Speeds up many-threaded, many-dir
1318 * operations by 30x or more...
Dave Kleikamp470decc2006-10-11 01:20:57 -07001319 *
Josef Bacike07f7182008-11-26 01:14:26 -05001320 * We try and optimize the sleep time against what the
1321 * underlying disk can do, instead of having a static sleep
1322 * time. This is useful for the case where our storage is so
1323 * fast that it is more optimal to go ahead and force a flush
1324 * and wait for the transaction to be committed than it is to
1325 * wait for an arbitrary amount of time for new writers to
1326 * join the transaction. We achieve this by measuring how
1327 * long it takes to commit a transaction, and compare it with
1328 * how long this transaction has been running, and if run time
1329 * < commit time then we sleep for the delta and commit. This
1330 * greatly helps super fast disks that would see slowdowns as
1331 * more threads started doing fsyncs.
1332 *
1333 * But don't do this if this process was the most recent one
1334 * to perform a synchronous write. We do this to detect the
1335 * case where a single process is doing a stream of sync
1336 * writes. No point in waiting for joiners in that case.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001337 */
1338 pid = current->pid;
1339 if (handle->h_sync && journal->j_last_sync_writer != pid) {
Josef Bacike07f7182008-11-26 01:14:26 -05001340 u64 commit_time, trans_time;
1341
Dave Kleikamp470decc2006-10-11 01:20:57 -07001342 journal->j_last_sync_writer = pid;
Josef Bacike07f7182008-11-26 01:14:26 -05001343
Theodore Ts'oa931da62010-08-03 21:35:12 -04001344 read_lock(&journal->j_state_lock);
Josef Bacike07f7182008-11-26 01:14:26 -05001345 commit_time = journal->j_average_commit_time;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001346 read_unlock(&journal->j_state_lock);
Josef Bacike07f7182008-11-26 01:14:26 -05001347
1348 trans_time = ktime_to_ns(ktime_sub(ktime_get(),
1349 transaction->t_start_time));
1350
Theodore Ts'o30773842009-01-03 20:27:38 -05001351 commit_time = max_t(u64, commit_time,
1352 1000*journal->j_min_batch_time);
Josef Bacike07f7182008-11-26 01:14:26 -05001353 commit_time = min_t(u64, commit_time,
Theodore Ts'o30773842009-01-03 20:27:38 -05001354 1000*journal->j_max_batch_time);
Josef Bacike07f7182008-11-26 01:14:26 -05001355
1356 if (trans_time < commit_time) {
1357 ktime_t expires = ktime_add_ns(ktime_get(),
1358 commit_time);
1359 set_current_state(TASK_UNINTERRUPTIBLE);
1360 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1361 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001362 }
1363
Theodore Ts'o70585482009-03-25 23:35:46 -04001364 if (handle->h_sync)
1365 transaction->t_synchronous_commit = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001366 current->journal_info = NULL;
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001367 atomic_sub(handle->h_buffer_credits,
1368 &transaction->t_outstanding_credits);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001369
1370 /*
1371 * If the handle is marked SYNC, we need to set another commit
1372 * going! We also want to force a commit if the current
1373 * transaction is occupying too much of the log, or if the
1374 * transaction is too old now.
1375 */
1376 if (handle->h_sync ||
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001377 (atomic_read(&transaction->t_outstanding_credits) >
1378 journal->j_max_transaction_buffers) ||
1379 time_after_eq(jiffies, transaction->t_expires)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001380 /* Do this even for aborted journals: an abort still
1381 * completes the commit thread, it just doesn't write
1382 * anything to disk. */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001383
Dave Kleikamp470decc2006-10-11 01:20:57 -07001384 jbd_debug(2, "transaction too old, requesting commit for "
1385 "handle %p\n", handle);
1386 /* This is non-blocking */
Theodore Ts'oc35a56a2010-05-16 05:00:00 -04001387 jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001388
1389 /*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001390 * Special case: JBD2_SYNC synchronous updates require us
Dave Kleikamp470decc2006-10-11 01:20:57 -07001391 * to wait for the commit to complete.
1392 */
1393 if (handle->h_sync && !(current->flags & PF_MEMALLOC))
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001394 wait_for_commit = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001395 }
1396
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001397 /*
1398 * Once we drop t_updates, if it goes to zero the transaction
1399 * could start commiting on us and eventually disappear. So
1400 * once we do this, we must not dereference transaction
1401 * pointer again.
1402 */
1403 tid = transaction->t_tid;
1404 if (atomic_dec_and_test(&transaction->t_updates)) {
1405 wake_up(&journal->j_wait_updates);
1406 if (journal->j_barrier_count)
1407 wake_up(&journal->j_wait_transaction_locked);
1408 }
1409
1410 if (wait_for_commit)
1411 err = jbd2_log_wait_commit(journal, tid);
1412
Ingo Molnar3295f0e2008-08-11 10:30:30 +02001413 lock_map_release(&handle->h_lockdep_map);
Mingming Cao7b751062008-01-28 23:58:27 -05001414
Mingming Caoaf1e76d2007-10-16 18:38:25 -04001415 jbd2_free_handle(handle);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001416 return err;
1417}
1418
Randy Dunlap5648ba52008-04-17 10:38:59 -04001419/**
1420 * int jbd2_journal_force_commit() - force any uncommitted transactions
Dave Kleikamp470decc2006-10-11 01:20:57 -07001421 * @journal: journal to force
1422 *
1423 * For synchronous operations: force any uncommitted transactions
1424 * to disk. May seem kludgy, but it reuses all the handle batching
1425 * code in a very simple manner.
1426 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001427int jbd2_journal_force_commit(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001428{
1429 handle_t *handle;
1430 int ret;
1431
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001432 handle = jbd2_journal_start(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001433 if (IS_ERR(handle)) {
1434 ret = PTR_ERR(handle);
1435 } else {
1436 handle->h_sync = 1;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001437 ret = jbd2_journal_stop(handle);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001438 }
1439 return ret;
1440}
1441
1442/*
1443 *
1444 * List management code snippets: various functions for manipulating the
1445 * transaction buffer lists.
1446 *
1447 */
1448
1449/*
1450 * Append a buffer to a transaction list, given the transaction's list head
1451 * pointer.
1452 *
1453 * j_list_lock is held.
1454 *
1455 * jbd_lock_bh_state(jh2bh(jh)) is held.
1456 */
1457
1458static inline void
1459__blist_add_buffer(struct journal_head **list, struct journal_head *jh)
1460{
1461 if (!*list) {
1462 jh->b_tnext = jh->b_tprev = jh;
1463 *list = jh;
1464 } else {
1465 /* Insert at the tail of the list to preserve order */
1466 struct journal_head *first = *list, *last = first->b_tprev;
1467 jh->b_tprev = last;
1468 jh->b_tnext = first;
1469 last->b_tnext = first->b_tprev = jh;
1470 }
1471}
1472
1473/*
1474 * Remove a buffer from a transaction list, given the transaction's list
1475 * head pointer.
1476 *
1477 * Called with j_list_lock held, and the journal may not be locked.
1478 *
1479 * jbd_lock_bh_state(jh2bh(jh)) is held.
1480 */
1481
1482static inline void
1483__blist_del_buffer(struct journal_head **list, struct journal_head *jh)
1484{
1485 if (*list == jh) {
1486 *list = jh->b_tnext;
1487 if (*list == jh)
1488 *list = NULL;
1489 }
1490 jh->b_tprev->b_tnext = jh->b_tnext;
1491 jh->b_tnext->b_tprev = jh->b_tprev;
1492}
1493
1494/*
1495 * Remove a buffer from the appropriate transaction list.
1496 *
1497 * Note that this function can *change* the value of
Jan Kara87c89c22008-07-11 19:27:31 -04001498 * bh->b_transaction->t_buffers, t_forget, t_iobuf_list, t_shadow_list,
1499 * t_log_list or t_reserved_list. If the caller is holding onto a copy of one
1500 * of these pointers, it could go bad. Generally the caller needs to re-read
1501 * the pointer from the transaction_t.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001502 *
1503 * Called under j_list_lock. The journal may not be locked.
1504 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001505void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001506{
1507 struct journal_head **list = NULL;
1508 transaction_t *transaction;
1509 struct buffer_head *bh = jh2bh(jh);
1510
1511 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1512 transaction = jh->b_transaction;
1513 if (transaction)
1514 assert_spin_locked(&transaction->t_journal->j_list_lock);
1515
1516 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1517 if (jh->b_jlist != BJ_None)
Mingming Cao40191912008-01-28 23:58:27 -05001518 J_ASSERT_JH(jh, transaction != NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001519
1520 switch (jh->b_jlist) {
1521 case BJ_None:
1522 return;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001523 case BJ_Metadata:
1524 transaction->t_nr_buffers--;
1525 J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
1526 list = &transaction->t_buffers;
1527 break;
1528 case BJ_Forget:
1529 list = &transaction->t_forget;
1530 break;
1531 case BJ_IO:
1532 list = &transaction->t_iobuf_list;
1533 break;
1534 case BJ_Shadow:
1535 list = &transaction->t_shadow_list;
1536 break;
1537 case BJ_LogCtl:
1538 list = &transaction->t_log_list;
1539 break;
1540 case BJ_Reserved:
1541 list = &transaction->t_reserved_list;
1542 break;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001543 }
1544
1545 __blist_del_buffer(list, jh);
1546 jh->b_jlist = BJ_None;
1547 if (test_clear_buffer_jbddirty(bh))
1548 mark_buffer_dirty(bh); /* Expose it to the VM */
1549}
1550
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001551void __jbd2_journal_unfile_buffer(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001552{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001553 __jbd2_journal_temp_unlink_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001554 jh->b_transaction = NULL;
1555}
1556
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001557void jbd2_journal_unfile_buffer(journal_t *journal, struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001558{
1559 jbd_lock_bh_state(jh2bh(jh));
1560 spin_lock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001561 __jbd2_journal_unfile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001562 spin_unlock(&journal->j_list_lock);
1563 jbd_unlock_bh_state(jh2bh(jh));
1564}
1565
1566/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001567 * Called from jbd2_journal_try_to_free_buffers().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001568 *
1569 * Called under jbd_lock_bh_state(bh)
1570 */
1571static void
1572__journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh)
1573{
1574 struct journal_head *jh;
1575
1576 jh = bh2jh(bh);
1577
1578 if (buffer_locked(bh) || buffer_dirty(bh))
1579 goto out;
1580
Mingming Cao40191912008-01-28 23:58:27 -05001581 if (jh->b_next_transaction != NULL)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001582 goto out;
1583
1584 spin_lock(&journal->j_list_lock);
Jan Kara87c89c22008-07-11 19:27:31 -04001585 if (jh->b_cp_transaction != NULL && jh->b_transaction == NULL) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001586 /* written-back checkpointed metadata buffer */
1587 if (jh->b_jlist == BJ_None) {
1588 JBUFFER_TRACE(jh, "remove from checkpoint list");
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001589 __jbd2_journal_remove_checkpoint(jh);
1590 jbd2_journal_remove_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001591 __brelse(bh);
1592 }
1593 }
1594 spin_unlock(&journal->j_list_lock);
1595out:
1596 return;
1597}
1598
Dave Kleikamp470decc2006-10-11 01:20:57 -07001599/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001600 * int jbd2_journal_try_to_free_buffers() - try to free page buffers.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001601 * @journal: journal for operation
1602 * @page: to try and free
Mingming Cao530576b2008-07-13 21:06:39 -04001603 * @gfp_mask: we use the mask to detect how hard should we try to release
1604 * buffers. If __GFP_WAIT and __GFP_FS is set, we wait for commit code to
1605 * release the buffers.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001606 *
1607 *
1608 * For all the buffers on this page,
1609 * if they are fully written out ordered data, move them onto BUF_CLEAN
1610 * so try_to_free_buffers() can reap them.
1611 *
1612 * This function returns non-zero if we wish try_to_free_buffers()
1613 * to be called. We do this if the page is releasable by try_to_free_buffers().
1614 * We also do it if the page has locked or dirty buffers and the caller wants
1615 * us to perform sync or async writeout.
1616 *
1617 * This complicates JBD locking somewhat. We aren't protected by the
1618 * BKL here. We wish to remove the buffer from its committing or
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001619 * running transaction's ->t_datalist via __jbd2_journal_unfile_buffer.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001620 *
1621 * This may *change* the value of transaction_t->t_datalist, so anyone
1622 * who looks at t_datalist needs to lock against this function.
1623 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001624 * Even worse, someone may be doing a jbd2_journal_dirty_data on this
1625 * buffer. So we need to lock against that. jbd2_journal_dirty_data()
Dave Kleikamp470decc2006-10-11 01:20:57 -07001626 * will come out of the lock with the buffer dirty, which makes it
1627 * ineligible for release here.
1628 *
1629 * Who else is affected by this? hmm... Really the only contender
1630 * is do_get_write_access() - it could be looking at the buffer while
1631 * journal_try_to_free_buffer() is changing its state. But that
1632 * cannot happen because we never reallocate freed data as metadata
1633 * while the data is part of a transaction. Yes?
Mingming Cao530576b2008-07-13 21:06:39 -04001634 *
1635 * Return 0 on failure, 1 on success
Dave Kleikamp470decc2006-10-11 01:20:57 -07001636 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001637int jbd2_journal_try_to_free_buffers(journal_t *journal,
Mingming Cao530576b2008-07-13 21:06:39 -04001638 struct page *page, gfp_t gfp_mask)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001639{
1640 struct buffer_head *head;
1641 struct buffer_head *bh;
1642 int ret = 0;
1643
1644 J_ASSERT(PageLocked(page));
1645
1646 head = page_buffers(page);
1647 bh = head;
1648 do {
1649 struct journal_head *jh;
1650
1651 /*
1652 * We take our own ref against the journal_head here to avoid
1653 * having to add tons of locking around each instance of
Mingming Cao530576b2008-07-13 21:06:39 -04001654 * jbd2_journal_remove_journal_head() and
1655 * jbd2_journal_put_journal_head().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001656 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001657 jh = jbd2_journal_grab_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001658 if (!jh)
1659 continue;
1660
1661 jbd_lock_bh_state(bh);
1662 __journal_try_to_free_buffer(journal, bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001663 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001664 jbd_unlock_bh_state(bh);
1665 if (buffer_jbd(bh))
1666 goto busy;
1667 } while ((bh = bh->b_this_page) != head);
Mingming Cao530576b2008-07-13 21:06:39 -04001668
Dave Kleikamp470decc2006-10-11 01:20:57 -07001669 ret = try_to_free_buffers(page);
Mingming Cao530576b2008-07-13 21:06:39 -04001670
Dave Kleikamp470decc2006-10-11 01:20:57 -07001671busy:
1672 return ret;
1673}
1674
1675/*
1676 * This buffer is no longer needed. If it is on an older transaction's
1677 * checkpoint list we need to record it on this transaction's forget list
1678 * to pin this buffer (and hence its checkpointing transaction) down until
1679 * this transaction commits. If the buffer isn't on a checkpoint list, we
1680 * release it.
1681 * Returns non-zero if JBD no longer has an interest in the buffer.
1682 *
1683 * Called under j_list_lock.
1684 *
1685 * Called under jbd_lock_bh_state(bh).
1686 */
1687static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
1688{
1689 int may_free = 1;
1690 struct buffer_head *bh = jh2bh(jh);
1691
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001692 __jbd2_journal_unfile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001693
1694 if (jh->b_cp_transaction) {
1695 JBUFFER_TRACE(jh, "on running+cp transaction");
Jan Karaf91d1d02009-07-13 16:16:20 -04001696 /*
1697 * We don't want to write the buffer anymore, clear the
1698 * bit so that we don't confuse checks in
1699 * __journal_file_buffer
1700 */
1701 clear_buffer_dirty(bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001702 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001703 may_free = 0;
1704 } else {
1705 JBUFFER_TRACE(jh, "on running transaction");
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001706 jbd2_journal_remove_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001707 __brelse(bh);
1708 }
1709 return may_free;
1710}
1711
1712/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001713 * jbd2_journal_invalidatepage
Dave Kleikamp470decc2006-10-11 01:20:57 -07001714 *
1715 * This code is tricky. It has a number of cases to deal with.
1716 *
1717 * There are two invariants which this code relies on:
1718 *
1719 * i_size must be updated on disk before we start calling invalidatepage on the
1720 * data.
1721 *
1722 * This is done in ext3 by defining an ext3_setattr method which
1723 * updates i_size before truncate gets going. By maintaining this
1724 * invariant, we can be sure that it is safe to throw away any buffers
1725 * attached to the current transaction: once the transaction commits,
1726 * we know that the data will not be needed.
1727 *
1728 * Note however that we can *not* throw away data belonging to the
1729 * previous, committing transaction!
1730 *
1731 * Any disk blocks which *are* part of the previous, committing
1732 * transaction (and which therefore cannot be discarded immediately) are
1733 * not going to be reused in the new running transaction
1734 *
1735 * The bitmap committed_data images guarantee this: any block which is
1736 * allocated in one transaction and removed in the next will be marked
1737 * as in-use in the committed_data bitmap, so cannot be reused until
1738 * the next transaction to delete the block commits. This means that
1739 * leaving committing buffers dirty is quite safe: the disk blocks
1740 * cannot be reallocated to a different file and so buffer aliasing is
1741 * not possible.
1742 *
1743 *
1744 * The above applies mainly to ordered data mode. In writeback mode we
1745 * don't make guarantees about the order in which data hits disk --- in
1746 * particular we don't guarantee that new dirty data is flushed before
1747 * transaction commit --- so it is always safe just to discard data
1748 * immediately in that mode. --sct
1749 */
1750
1751/*
1752 * The journal_unmap_buffer helper function returns zero if the buffer
1753 * concerned remains pinned as an anonymous buffer belonging to an older
1754 * transaction.
1755 *
1756 * We're outside-transaction here. Either or both of j_running_transaction
1757 * and j_committing_transaction may be NULL.
1758 */
1759static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh)
1760{
1761 transaction_t *transaction;
1762 struct journal_head *jh;
1763 int may_free = 1;
1764 int ret;
1765
1766 BUFFER_TRACE(bh, "entry");
1767
1768 /*
1769 * It is safe to proceed here without the j_list_lock because the
1770 * buffers cannot be stolen by try_to_free_buffers as long as we are
1771 * holding the page lock. --sct
1772 */
1773
1774 if (!buffer_jbd(bh))
1775 goto zap_buffer_unlocked;
1776
Jan Kara87c89c22008-07-11 19:27:31 -04001777 /* OK, we have data buffer in journaled mode */
Theodore Ts'oa931da62010-08-03 21:35:12 -04001778 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001779 jbd_lock_bh_state(bh);
1780 spin_lock(&journal->j_list_lock);
1781
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001782 jh = jbd2_journal_grab_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001783 if (!jh)
1784 goto zap_buffer_no_jh;
1785
dingdinghuaba869022010-02-15 16:35:42 -05001786 /*
1787 * We cannot remove the buffer from checkpoint lists until the
1788 * transaction adding inode to orphan list (let's call it T)
1789 * is committed. Otherwise if the transaction changing the
1790 * buffer would be cleaned from the journal before T is
1791 * committed, a crash will cause that the correct contents of
1792 * the buffer will be lost. On the other hand we have to
1793 * clear the buffer dirty bit at latest at the moment when the
1794 * transaction marking the buffer as freed in the filesystem
1795 * structures is committed because from that moment on the
1796 * buffer can be reallocated and used by a different page.
1797 * Since the block hasn't been freed yet but the inode has
1798 * already been added to orphan list, it is safe for us to add
1799 * the buffer to BJ_Forget list of the newest transaction.
1800 */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001801 transaction = jh->b_transaction;
1802 if (transaction == NULL) {
1803 /* First case: not on any transaction. If it
1804 * has no checkpoint link, then we can zap it:
1805 * it's a writeback-mode buffer so we don't care
1806 * if it hits disk safely. */
1807 if (!jh->b_cp_transaction) {
1808 JBUFFER_TRACE(jh, "not on any transaction: zap");
1809 goto zap_buffer;
1810 }
1811
1812 if (!buffer_dirty(bh)) {
1813 /* bdflush has written it. We can drop it now */
1814 goto zap_buffer;
1815 }
1816
1817 /* OK, it must be in the journal but still not
1818 * written fully to disk: it's metadata or
1819 * journaled data... */
1820
1821 if (journal->j_running_transaction) {
1822 /* ... and once the current transaction has
1823 * committed, the buffer won't be needed any
1824 * longer. */
1825 JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
1826 ret = __dispose_buffer(jh,
1827 journal->j_running_transaction);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001828 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001829 spin_unlock(&journal->j_list_lock);
1830 jbd_unlock_bh_state(bh);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001831 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001832 return ret;
1833 } else {
1834 /* There is no currently-running transaction. So the
1835 * orphan record which we wrote for this file must have
1836 * passed into commit. We must attach this buffer to
1837 * the committing transaction, if it exists. */
1838 if (journal->j_committing_transaction) {
1839 JBUFFER_TRACE(jh, "give to committing trans");
1840 ret = __dispose_buffer(jh,
1841 journal->j_committing_transaction);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001842 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001843 spin_unlock(&journal->j_list_lock);
1844 jbd_unlock_bh_state(bh);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001845 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001846 return ret;
1847 } else {
1848 /* The orphan record's transaction has
1849 * committed. We can cleanse this buffer */
1850 clear_buffer_jbddirty(bh);
1851 goto zap_buffer;
1852 }
1853 }
1854 } else if (transaction == journal->j_committing_transaction) {
Eric Sandeen9b579882006-10-28 10:38:28 -07001855 JBUFFER_TRACE(jh, "on committing transaction");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001856 /*
dingdinghuaba869022010-02-15 16:35:42 -05001857 * The buffer is committing, we simply cannot touch
1858 * it. So we just set j_next_transaction to the
1859 * running transaction (if there is one) and mark
1860 * buffer as freed so that commit code knows it should
1861 * clear dirty bits when it is done with the buffer.
1862 */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001863 set_buffer_freed(bh);
dingdinghuaba869022010-02-15 16:35:42 -05001864 if (journal->j_running_transaction && buffer_jbddirty(bh))
1865 jh->b_next_transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001866 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001867 spin_unlock(&journal->j_list_lock);
1868 jbd_unlock_bh_state(bh);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001869 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001870 return 0;
1871 } else {
1872 /* Good, the buffer belongs to the running transaction.
1873 * We are writing our own transaction's data, not any
1874 * previous one's, so it is safe to throw it away
1875 * (remember that we expect the filesystem to have set
1876 * i_size already for this truncate so recovery will not
1877 * expose the disk blocks we are discarding here.) */
1878 J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
Eric Sandeen9b579882006-10-28 10:38:28 -07001879 JBUFFER_TRACE(jh, "on running transaction");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001880 may_free = __dispose_buffer(jh, transaction);
1881 }
1882
1883zap_buffer:
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001884 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001885zap_buffer_no_jh:
1886 spin_unlock(&journal->j_list_lock);
1887 jbd_unlock_bh_state(bh);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001888 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001889zap_buffer_unlocked:
1890 clear_buffer_dirty(bh);
1891 J_ASSERT_BH(bh, !buffer_jbddirty(bh));
1892 clear_buffer_mapped(bh);
1893 clear_buffer_req(bh);
1894 clear_buffer_new(bh);
1895 bh->b_bdev = NULL;
1896 return may_free;
1897}
1898
1899/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001900 * void jbd2_journal_invalidatepage()
Dave Kleikamp470decc2006-10-11 01:20:57 -07001901 * @journal: journal to use for flush...
1902 * @page: page to flush
1903 * @offset: length of page to invalidate.
1904 *
1905 * Reap page buffers containing data after offset in page.
1906 *
1907 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001908void jbd2_journal_invalidatepage(journal_t *journal,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001909 struct page *page,
1910 unsigned long offset)
1911{
1912 struct buffer_head *head, *bh, *next;
1913 unsigned int curr_off = 0;
1914 int may_free = 1;
1915
1916 if (!PageLocked(page))
1917 BUG();
1918 if (!page_has_buffers(page))
1919 return;
1920
1921 /* We will potentially be playing with lists other than just the
1922 * data lists (especially for journaled data mode), so be
1923 * cautious in our locking. */
1924
1925 head = bh = page_buffers(page);
1926 do {
1927 unsigned int next_off = curr_off + bh->b_size;
1928 next = bh->b_this_page;
1929
1930 if (offset <= curr_off) {
1931 /* This block is wholly outside the truncation point */
1932 lock_buffer(bh);
1933 may_free &= journal_unmap_buffer(journal, bh);
1934 unlock_buffer(bh);
1935 }
1936 curr_off = next_off;
1937 bh = next;
1938
1939 } while (bh != head);
1940
1941 if (!offset) {
1942 if (may_free && try_to_free_buffers(page))
1943 J_ASSERT(!page_has_buffers(page));
1944 }
1945}
1946
1947/*
1948 * File a buffer on the given transaction list.
1949 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001950void __jbd2_journal_file_buffer(struct journal_head *jh,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001951 transaction_t *transaction, int jlist)
1952{
1953 struct journal_head **list = NULL;
1954 int was_dirty = 0;
1955 struct buffer_head *bh = jh2bh(jh);
1956
1957 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1958 assert_spin_locked(&transaction->t_journal->j_list_lock);
1959
1960 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1961 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
Mingming Cao40191912008-01-28 23:58:27 -05001962 jh->b_transaction == NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001963
1964 if (jh->b_transaction && jh->b_jlist == jlist)
1965 return;
1966
Dave Kleikamp470decc2006-10-11 01:20:57 -07001967 if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
1968 jlist == BJ_Shadow || jlist == BJ_Forget) {
Jan Karaf91d1d02009-07-13 16:16:20 -04001969 /*
1970 * For metadata buffers, we track dirty bit in buffer_jbddirty
1971 * instead of buffer_dirty. We should not see a dirty bit set
1972 * here because we clear it in do_get_write_access but e.g.
1973 * tune2fs can modify the sb and set the dirty bit at any time
1974 * so we try to gracefully handle that.
1975 */
1976 if (buffer_dirty(bh))
1977 warn_dirty_buffer(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001978 if (test_clear_buffer_dirty(bh) ||
1979 test_clear_buffer_jbddirty(bh))
1980 was_dirty = 1;
1981 }
1982
1983 if (jh->b_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001984 __jbd2_journal_temp_unlink_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001985 jh->b_transaction = transaction;
1986
1987 switch (jlist) {
1988 case BJ_None:
1989 J_ASSERT_JH(jh, !jh->b_committed_data);
1990 J_ASSERT_JH(jh, !jh->b_frozen_data);
1991 return;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001992 case BJ_Metadata:
1993 transaction->t_nr_buffers++;
1994 list = &transaction->t_buffers;
1995 break;
1996 case BJ_Forget:
1997 list = &transaction->t_forget;
1998 break;
1999 case BJ_IO:
2000 list = &transaction->t_iobuf_list;
2001 break;
2002 case BJ_Shadow:
2003 list = &transaction->t_shadow_list;
2004 break;
2005 case BJ_LogCtl:
2006 list = &transaction->t_log_list;
2007 break;
2008 case BJ_Reserved:
2009 list = &transaction->t_reserved_list;
2010 break;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002011 }
2012
2013 __blist_add_buffer(list, jh);
2014 jh->b_jlist = jlist;
2015
2016 if (was_dirty)
2017 set_buffer_jbddirty(bh);
2018}
2019
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002020void jbd2_journal_file_buffer(struct journal_head *jh,
Dave Kleikamp470decc2006-10-11 01:20:57 -07002021 transaction_t *transaction, int jlist)
2022{
2023 jbd_lock_bh_state(jh2bh(jh));
2024 spin_lock(&transaction->t_journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002025 __jbd2_journal_file_buffer(jh, transaction, jlist);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002026 spin_unlock(&transaction->t_journal->j_list_lock);
2027 jbd_unlock_bh_state(jh2bh(jh));
2028}
2029
2030/*
2031 * Remove a buffer from its current buffer list in preparation for
2032 * dropping it from its current transaction entirely. If the buffer has
2033 * already started to be used by a subsequent transaction, refile the
2034 * buffer on that transaction's metadata list.
2035 *
2036 * Called under journal->j_list_lock
2037 *
2038 * Called under jbd_lock_bh_state(jh2bh(jh))
2039 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002040void __jbd2_journal_refile_buffer(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002041{
dingdinghuaba869022010-02-15 16:35:42 -05002042 int was_dirty, jlist;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002043 struct buffer_head *bh = jh2bh(jh);
2044
2045 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
2046 if (jh->b_transaction)
2047 assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
2048
2049 /* If the buffer is now unused, just drop it. */
2050 if (jh->b_next_transaction == NULL) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002051 __jbd2_journal_unfile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002052 return;
2053 }
2054
2055 /*
2056 * It has been modified by a later transaction: add it to the new
2057 * transaction's metadata list.
2058 */
2059
2060 was_dirty = test_clear_buffer_jbddirty(bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002061 __jbd2_journal_temp_unlink_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002062 jh->b_transaction = jh->b_next_transaction;
2063 jh->b_next_transaction = NULL;
dingdinghuaba869022010-02-15 16:35:42 -05002064 if (buffer_freed(bh))
2065 jlist = BJ_Forget;
2066 else if (jh->b_modified)
2067 jlist = BJ_Metadata;
2068 else
2069 jlist = BJ_Reserved;
2070 __jbd2_journal_file_buffer(jh, jh->b_transaction, jlist);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002071 J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
2072
2073 if (was_dirty)
2074 set_buffer_jbddirty(bh);
2075}
2076
2077/*
2078 * For the unlocked version of this call, also make sure that any
2079 * hanging journal_head is cleaned up if necessary.
2080 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002081 * __jbd2_journal_refile_buffer is usually called as part of a single locked
Dave Kleikamp470decc2006-10-11 01:20:57 -07002082 * operation on a buffer_head, in which the caller is probably going to
2083 * be hooking the journal_head onto other lists. In that case it is up
2084 * to the caller to remove the journal_head if necessary. For the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002085 * unlocked jbd2_journal_refile_buffer call, the caller isn't going to be
Dave Kleikamp470decc2006-10-11 01:20:57 -07002086 * doing anything else to the buffer so we need to do the cleanup
2087 * ourselves to avoid a jh leak.
2088 *
2089 * *** The journal_head may be freed by this call! ***
2090 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002091void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002092{
2093 struct buffer_head *bh = jh2bh(jh);
2094
2095 jbd_lock_bh_state(bh);
2096 spin_lock(&journal->j_list_lock);
2097
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002098 __jbd2_journal_refile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002099 jbd_unlock_bh_state(bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002100 jbd2_journal_remove_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002101
2102 spin_unlock(&journal->j_list_lock);
2103 __brelse(bh);
2104}
Jan Karac851ed52008-07-11 19:27:31 -04002105
2106/*
2107 * File inode in the inode list of the handle's transaction
2108 */
2109int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode)
2110{
2111 transaction_t *transaction = handle->h_transaction;
2112 journal_t *journal = transaction->t_journal;
2113
2114 if (is_handle_aborted(handle))
2115 return -EIO;
2116
2117 jbd_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino,
2118 transaction->t_tid);
2119
2120 /*
2121 * First check whether inode isn't already on the transaction's
2122 * lists without taking the lock. Note that this check is safe
2123 * without the lock as we cannot race with somebody removing inode
2124 * from the transaction. The reason is that we remove inode from the
2125 * transaction only in journal_release_jbd_inode() and when we commit
2126 * the transaction. We are guarded from the first case by holding
2127 * a reference to the inode. We are safe against the second case
2128 * because if jinode->i_transaction == transaction, commit code
2129 * cannot touch the transaction because we hold reference to it,
2130 * and if jinode->i_next_transaction == transaction, commit code
2131 * will only file the inode where we want it.
2132 */
2133 if (jinode->i_transaction == transaction ||
2134 jinode->i_next_transaction == transaction)
2135 return 0;
2136
2137 spin_lock(&journal->j_list_lock);
2138
2139 if (jinode->i_transaction == transaction ||
2140 jinode->i_next_transaction == transaction)
2141 goto done;
2142
2143 /* On some different transaction's list - should be
2144 * the committing one */
2145 if (jinode->i_transaction) {
2146 J_ASSERT(jinode->i_next_transaction == NULL);
2147 J_ASSERT(jinode->i_transaction ==
2148 journal->j_committing_transaction);
2149 jinode->i_next_transaction = transaction;
2150 goto done;
2151 }
2152 /* Not on any transaction list... */
2153 J_ASSERT(!jinode->i_next_transaction);
2154 jinode->i_transaction = transaction;
2155 list_add(&jinode->i_list, &transaction->t_inode_list);
2156done:
2157 spin_unlock(&journal->j_list_lock);
2158
2159 return 0;
2160}
2161
2162/*
Jan Kara7f5aa212009-02-10 11:15:34 -05002163 * File truncate and transaction commit interact with each other in a
2164 * non-trivial way. If a transaction writing data block A is
2165 * committing, we cannot discard the data by truncate until we have
2166 * written them. Otherwise if we crashed after the transaction with
2167 * write has committed but before the transaction with truncate has
2168 * committed, we could see stale data in block A. This function is a
2169 * helper to solve this problem. It starts writeout of the truncated
2170 * part in case it is in the committing transaction.
2171 *
2172 * Filesystem code must call this function when inode is journaled in
2173 * ordered mode before truncation happens and after the inode has been
2174 * placed on orphan list with the new inode size. The second condition
2175 * avoids the race that someone writes new data and we start
2176 * committing the transaction after this function has been called but
2177 * before a transaction for truncate is started (and furthermore it
2178 * allows us to optimize the case where the addition to orphan list
2179 * happens in the same transaction as write --- we don't have to write
2180 * any data in such case).
Jan Karac851ed52008-07-11 19:27:31 -04002181 */
Jan Kara7f5aa212009-02-10 11:15:34 -05002182int jbd2_journal_begin_ordered_truncate(journal_t *journal,
2183 struct jbd2_inode *jinode,
Jan Karac851ed52008-07-11 19:27:31 -04002184 loff_t new_size)
2185{
Jan Kara7f5aa212009-02-10 11:15:34 -05002186 transaction_t *inode_trans, *commit_trans;
Jan Karac851ed52008-07-11 19:27:31 -04002187 int ret = 0;
2188
Jan Kara7f5aa212009-02-10 11:15:34 -05002189 /* This is a quick check to avoid locking if not necessary */
2190 if (!jinode->i_transaction)
Jan Karac851ed52008-07-11 19:27:31 -04002191 goto out;
Jan Kara7f5aa212009-02-10 11:15:34 -05002192 /* Locks are here just to force reading of recent values, it is
2193 * enough that the transaction was not committing before we started
2194 * a transaction adding the inode to orphan list */
Theodore Ts'oa931da62010-08-03 21:35:12 -04002195 read_lock(&journal->j_state_lock);
Jan Karac851ed52008-07-11 19:27:31 -04002196 commit_trans = journal->j_committing_transaction;
Theodore Ts'oa931da62010-08-03 21:35:12 -04002197 read_unlock(&journal->j_state_lock);
Jan Kara7f5aa212009-02-10 11:15:34 -05002198 spin_lock(&journal->j_list_lock);
2199 inode_trans = jinode->i_transaction;
2200 spin_unlock(&journal->j_list_lock);
2201 if (inode_trans == commit_trans) {
2202 ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping,
Jan Karac851ed52008-07-11 19:27:31 -04002203 new_size, LLONG_MAX);
2204 if (ret)
2205 jbd2_journal_abort(journal, ret);
2206 }
2207out:
2208 return ret;
2209}