blob: 9dfdf010f1df77735b560ad6e64cb3665512c739 [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/*
Tao Ma28e35e422011-05-22 21:45:26 -040085 * Update transaction's maximum wait time, if debugging is enabled.
Theodore Ts'o6d0bf002010-08-09 17:28:38 -040086 *
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 */
Tao Ma28e35e422011-05-22 21:45:26 -040094static inline void update_t_max_wait(transaction_t *transaction,
95 unsigned long ts)
Theodore Ts'o6d0bf002010-08-09 17:28:38 -040096{
97#ifdef CONFIG_JBD2_DEBUG
Theodore Ts'o6d0bf002010-08-09 17:28:38 -040098 if (jbd2_journal_enable_debug &&
99 time_after(transaction->t_start, ts)) {
100 ts = jbd2_time_diff(ts, transaction->t_start);
101 spin_lock(&transaction->t_handle_lock);
102 if (ts > transaction->t_max_wait)
103 transaction->t_max_wait = ts;
104 spin_unlock(&transaction->t_handle_lock);
105 }
106#endif
107}
108
109/*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700110 * start_this_handle: Given a handle, deal with any locking or stalling
111 * needed to make sure that there is enough journal space for the handle
112 * to begin. Attach the handle to a transaction and set up the
113 * transaction's buffer credits.
114 */
115
Theodore Ts'o47def822010-07-27 11:56:05 -0400116static int start_this_handle(journal_t *journal, handle_t *handle,
117 int gfp_mask)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700118{
Theodore Ts'oe4471832011-02-12 08:18:24 -0500119 transaction_t *transaction, *new_transaction = NULL;
120 tid_t tid;
121 int needed, need_to_start;
122 int nblocks = handle->h_buffer_credits;
Tao Ma28e35e422011-05-22 21:45:26 -0400123 unsigned long ts = jiffies;
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);
Theodore Ts'oe4471832011-02-12 08:18:24 -0500225 tid = transaction->t_tid;
226 need_to_start = !tid_geq(journal->j_commit_request, tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400227 read_unlock(&journal->j_state_lock);
Theodore Ts'oe4471832011-02-12 08:18:24 -0500228 if (need_to_start)
229 jbd2_log_start_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700230 schedule();
231 finish_wait(&journal->j_wait_transaction_locked, &wait);
232 goto repeat;
233 }
234
235 /*
236 * The commit code assumes that it can get enough log space
237 * without forcing a checkpoint. This is *critical* for
238 * correctness: a checkpoint of a buffer which is also
239 * associated with a committing transaction creates a deadlock,
240 * so commit simply cannot force through checkpoints.
241 *
242 * We must therefore ensure the necessary space in the journal
243 * *before* starting to dirty potentially checkpointed buffers
244 * in the new transaction.
245 *
246 * The worst part is, any transaction currently committing can
247 * reduce the free space arbitrarily. Be careful to account for
248 * those buffers when checkpointing.
249 */
250
251 /*
252 * @@@ AKPM: This seems rather over-defensive. We're giving commit
253 * a _lot_ of headroom: 1/4 of the journal plus the size of
254 * the committing transaction. Really, we only need to give it
255 * committing_transaction->t_outstanding_credits plus "enough" for
256 * the log control blocks.
Uwe Kleine-Königa34f0b32010-12-10 14:55:42 +0100257 * Also, this test is inconsistent with the matching one in
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700258 * jbd2_journal_extend().
Dave Kleikamp470decc2006-10-11 01:20:57 -0700259 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700260 if (__jbd2_log_space_left(journal) < jbd_space_needed(journal)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700261 jbd_debug(2, "Handle %p waiting for checkpoint...\n", handle);
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400262 atomic_sub(nblocks, &transaction->t_outstanding_credits);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400263 read_unlock(&journal->j_state_lock);
264 write_lock(&journal->j_state_lock);
265 if (__jbd2_log_space_left(journal) < jbd_space_needed(journal))
266 __jbd2_log_wait_for_space(journal);
267 write_unlock(&journal->j_state_lock);
268 goto repeat;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700269 }
270
271 /* OK, account for the buffers that this operation expects to
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400272 * use and add the handle to the running transaction.
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400273 */
Tao Ma28e35e422011-05-22 21:45:26 -0400274 update_t_max_wait(transaction, ts);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700275 handle->h_transaction = transaction;
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400276 atomic_inc(&transaction->t_updates);
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400277 atomic_inc(&transaction->t_handle_count);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700278 jbd_debug(4, "Handle %p given %d credits (total %d, free %d)\n",
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400279 handle, nblocks,
280 atomic_read(&transaction->t_outstanding_credits),
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700281 __jbd2_log_space_left(journal));
Theodore Ts'oa931da62010-08-03 21:35:12 -0400282 read_unlock(&journal->j_state_lock);
Jan Kara9599b0e2009-08-17 21:23:17 -0400283
284 lock_map_acquire(&handle->h_lockdep_map);
Theodore Ts'o47def822010-07-27 11:56:05 -0400285 kfree(new_transaction);
286 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700287}
288
Mingming Cao7b751062008-01-28 23:58:27 -0500289static struct lock_class_key jbd2_handle_key;
290
Dave Kleikamp470decc2006-10-11 01:20:57 -0700291/* Allocate a new handle. This should probably be in a slab... */
292static handle_t *new_handle(int nblocks)
293{
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400294 handle_t *handle = jbd2_alloc_handle(GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700295 if (!handle)
296 return NULL;
297 memset(handle, 0, sizeof(*handle));
298 handle->h_buffer_credits = nblocks;
299 handle->h_ref = 1;
300
Mingming Cao7b751062008-01-28 23:58:27 -0500301 lockdep_init_map(&handle->h_lockdep_map, "jbd2_handle",
302 &jbd2_handle_key, 0);
303
Dave Kleikamp470decc2006-10-11 01:20:57 -0700304 return handle;
305}
306
307/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700308 * handle_t *jbd2_journal_start() - Obtain a new handle.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700309 * @journal: Journal to start transaction on.
310 * @nblocks: number of block buffer we might modify
311 *
312 * We make sure that the transaction can guarantee at least nblocks of
313 * modified buffers in the log. We block until the log can guarantee
314 * that much space.
315 *
316 * This function is visible to journal users (like ext3fs), so is not
317 * called with the journal already locked.
318 *
Eryu Guanc8675162011-05-24 17:09:58 -0400319 * Return a pointer to a newly allocated handle, or an ERR_PTR() value
320 * on failure.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700321 */
Theodore Ts'o47def822010-07-27 11:56:05 -0400322handle_t *jbd2__journal_start(journal_t *journal, int nblocks, int gfp_mask)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700323{
324 handle_t *handle = journal_current_handle();
325 int err;
326
327 if (!journal)
328 return ERR_PTR(-EROFS);
329
330 if (handle) {
331 J_ASSERT(handle->h_transaction->t_journal == journal);
332 handle->h_ref++;
333 return handle;
334 }
335
336 handle = new_handle(nblocks);
337 if (!handle)
338 return ERR_PTR(-ENOMEM);
339
340 current->journal_info = handle;
341
Theodore Ts'o47def822010-07-27 11:56:05 -0400342 err = start_this_handle(journal, handle, gfp_mask);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700343 if (err < 0) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400344 jbd2_free_handle(handle);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700345 current->journal_info = NULL;
346 handle = ERR_PTR(err);
347 }
348 return handle;
349}
Theodore Ts'o47def822010-07-27 11:56:05 -0400350EXPORT_SYMBOL(jbd2__journal_start);
351
352
353handle_t *jbd2_journal_start(journal_t *journal, int nblocks)
354{
355 return jbd2__journal_start(journal, nblocks, GFP_NOFS);
356}
357EXPORT_SYMBOL(jbd2_journal_start);
358
Dave Kleikamp470decc2006-10-11 01:20:57 -0700359
360/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700361 * int jbd2_journal_extend() - extend buffer credits.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700362 * @handle: handle to 'extend'
363 * @nblocks: nr blocks to try to extend by.
364 *
365 * Some transactions, such as large extends and truncates, can be done
366 * atomically all at once or in several stages. The operation requests
367 * a credit for a number of buffer modications in advance, but can
368 * extend its credit if it needs more.
369 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700370 * jbd2_journal_extend tries to give the running handle more buffer credits.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700371 * It does not guarantee that allocation - this is a best-effort only.
372 * The calling process MUST be able to deal cleanly with a failure to
373 * extend here.
374 *
375 * Return 0 on success, non-zero on failure.
376 *
377 * return code < 0 implies an error
378 * return code > 0 implies normal transaction-full status.
379 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700380int jbd2_journal_extend(handle_t *handle, int nblocks)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700381{
382 transaction_t *transaction = handle->h_transaction;
383 journal_t *journal = transaction->t_journal;
384 int result;
385 int wanted;
386
387 result = -EIO;
388 if (is_handle_aborted(handle))
389 goto out;
390
391 result = 1;
392
Theodore Ts'oa931da62010-08-03 21:35:12 -0400393 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700394
395 /* Don't extend a locked-down transaction! */
396 if (handle->h_transaction->t_state != T_RUNNING) {
397 jbd_debug(3, "denied handle %p %d blocks: "
398 "transaction not running\n", handle, nblocks);
399 goto error_out;
400 }
401
402 spin_lock(&transaction->t_handle_lock);
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400403 wanted = atomic_read(&transaction->t_outstanding_credits) + nblocks;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700404
405 if (wanted > journal->j_max_transaction_buffers) {
406 jbd_debug(3, "denied handle %p %d blocks: "
407 "transaction too large\n", handle, nblocks);
408 goto unlock;
409 }
410
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700411 if (wanted > __jbd2_log_space_left(journal)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700412 jbd_debug(3, "denied handle %p %d blocks: "
413 "insufficient log space\n", handle, nblocks);
414 goto unlock;
415 }
416
417 handle->h_buffer_credits += nblocks;
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400418 atomic_add(nblocks, &transaction->t_outstanding_credits);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700419 result = 0;
420
421 jbd_debug(3, "extended handle %p by %d\n", handle, nblocks);
422unlock:
423 spin_unlock(&transaction->t_handle_lock);
424error_out:
Theodore Ts'oa931da62010-08-03 21:35:12 -0400425 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700426out:
427 return result;
428}
429
430
431/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700432 * int jbd2_journal_restart() - restart a handle .
Dave Kleikamp470decc2006-10-11 01:20:57 -0700433 * @handle: handle to restart
434 * @nblocks: nr credits requested
435 *
436 * Restart a handle for a multi-transaction filesystem
437 * operation.
438 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700439 * If the jbd2_journal_extend() call above fails to grant new buffer credits
440 * to a running handle, a call to jbd2_journal_restart will commit the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700441 * handle's transaction so far and reattach the handle to a new
442 * transaction capabable of guaranteeing the requested number of
443 * credits.
444 */
Theodore Ts'o47def822010-07-27 11:56:05 -0400445int jbd2__journal_restart(handle_t *handle, int nblocks, int gfp_mask)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700446{
447 transaction_t *transaction = handle->h_transaction;
448 journal_t *journal = transaction->t_journal;
Theodore Ts'oe4471832011-02-12 08:18:24 -0500449 tid_t tid;
450 int need_to_start, ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700451
452 /* If we've had an abort of any type, don't even think about
453 * actually doing the restart! */
454 if (is_handle_aborted(handle))
455 return 0;
456
457 /*
458 * First unlink the handle from its current transaction, and start the
459 * commit on that.
460 */
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400461 J_ASSERT(atomic_read(&transaction->t_updates) > 0);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700462 J_ASSERT(journal_current_handle() == handle);
463
Theodore Ts'oa931da62010-08-03 21:35:12 -0400464 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700465 spin_lock(&transaction->t_handle_lock);
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400466 atomic_sub(handle->h_buffer_credits,
467 &transaction->t_outstanding_credits);
468 if (atomic_dec_and_test(&transaction->t_updates))
Dave Kleikamp470decc2006-10-11 01:20:57 -0700469 wake_up(&journal->j_wait_updates);
470 spin_unlock(&transaction->t_handle_lock);
471
472 jbd_debug(2, "restarting handle %p\n", handle);
Theodore Ts'oe4471832011-02-12 08:18:24 -0500473 tid = transaction->t_tid;
474 need_to_start = !tid_geq(journal->j_commit_request, tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400475 read_unlock(&journal->j_state_lock);
Theodore Ts'oe4471832011-02-12 08:18:24 -0500476 if (need_to_start)
477 jbd2_log_start_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700478
Jan Kara9599b0e2009-08-17 21:23:17 -0400479 lock_map_release(&handle->h_lockdep_map);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700480 handle->h_buffer_credits = nblocks;
Theodore Ts'o47def822010-07-27 11:56:05 -0400481 ret = start_this_handle(journal, handle, gfp_mask);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700482 return ret;
483}
Theodore Ts'o47def822010-07-27 11:56:05 -0400484EXPORT_SYMBOL(jbd2__journal_restart);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700485
486
Theodore Ts'o47def822010-07-27 11:56:05 -0400487int jbd2_journal_restart(handle_t *handle, int nblocks)
488{
489 return jbd2__journal_restart(handle, nblocks, GFP_NOFS);
490}
491EXPORT_SYMBOL(jbd2_journal_restart);
492
Dave Kleikamp470decc2006-10-11 01:20:57 -0700493/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700494 * void jbd2_journal_lock_updates () - establish a transaction barrier.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700495 * @journal: Journal to establish a barrier on.
496 *
497 * This locks out any further updates from being started, and blocks
498 * until all existing updates have completed, returning only once the
499 * journal is in a quiescent state with no updates running.
500 *
501 * The journal lock should not be held on entry.
502 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700503void jbd2_journal_lock_updates(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700504{
505 DEFINE_WAIT(wait);
506
Theodore Ts'oa931da62010-08-03 21:35:12 -0400507 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700508 ++journal->j_barrier_count;
509
510 /* Wait until there are no running updates */
511 while (1) {
512 transaction_t *transaction = journal->j_running_transaction;
513
514 if (!transaction)
515 break;
516
517 spin_lock(&transaction->t_handle_lock);
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400518 if (!atomic_read(&transaction->t_updates)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700519 spin_unlock(&transaction->t_handle_lock);
520 break;
521 }
522 prepare_to_wait(&journal->j_wait_updates, &wait,
523 TASK_UNINTERRUPTIBLE);
524 spin_unlock(&transaction->t_handle_lock);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400525 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700526 schedule();
527 finish_wait(&journal->j_wait_updates, &wait);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400528 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700529 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400530 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700531
532 /*
533 * We have now established a barrier against other normal updates, but
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700534 * we also need to barrier against other jbd2_journal_lock_updates() calls
Dave Kleikamp470decc2006-10-11 01:20:57 -0700535 * to make sure that we serialise special journal-locked operations
536 * too.
537 */
538 mutex_lock(&journal->j_barrier);
539}
540
541/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700542 * void jbd2_journal_unlock_updates (journal_t* journal) - release barrier
Dave Kleikamp470decc2006-10-11 01:20:57 -0700543 * @journal: Journal to release the barrier on.
544 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700545 * Release a transaction barrier obtained with jbd2_journal_lock_updates().
Dave Kleikamp470decc2006-10-11 01:20:57 -0700546 *
547 * Should be called without the journal lock held.
548 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700549void jbd2_journal_unlock_updates (journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700550{
551 J_ASSERT(journal->j_barrier_count != 0);
552
553 mutex_unlock(&journal->j_barrier);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400554 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700555 --journal->j_barrier_count;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400556 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700557 wake_up(&journal->j_wait_transaction_locked);
558}
559
Jan Karaf91d1d02009-07-13 16:16:20 -0400560static void warn_dirty_buffer(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700561{
Jan Karaf91d1d02009-07-13 16:16:20 -0400562 char b[BDEVNAME_SIZE];
Dave Kleikamp470decc2006-10-11 01:20:57 -0700563
Jan Karaf91d1d02009-07-13 16:16:20 -0400564 printk(KERN_WARNING
565 "JBD: Spotted dirty metadata buffer (dev = %s, blocknr = %llu). "
566 "There's a risk of filesystem corruption in case of system "
567 "crash.\n",
568 bdevname(bh->b_bdev, b), (unsigned long long)bh->b_blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700569}
570
571/*
572 * If the buffer is already part of the current transaction, then there
573 * is nothing we need to do. If it is already part of a prior
574 * transaction which we are still committing to disk, then we need to
575 * make sure that we do not overwrite the old copy: we do copy-out to
576 * preserve the copy going to disk. We also account the buffer against
577 * the handle's metadata buffer credits (unless the buffer is already
578 * part of the transaction, that is).
579 *
580 */
581static int
582do_get_write_access(handle_t *handle, struct journal_head *jh,
583 int force_copy)
584{
585 struct buffer_head *bh;
586 transaction_t *transaction;
587 journal_t *journal;
588 int error;
589 char *frozen_buffer = NULL;
590 int need_copy = 0;
591
592 if (is_handle_aborted(handle))
593 return -EROFS;
594
595 transaction = handle->h_transaction;
596 journal = transaction->t_journal;
597
Theodore Ts'ocfef2c62010-12-18 13:07:34 -0500598 jbd_debug(5, "journal_head %p, force_copy %d\n", jh, force_copy);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700599
600 JBUFFER_TRACE(jh, "entry");
601repeat:
602 bh = jh2bh(jh);
603
604 /* @@@ Need to check for errors here at some point. */
605
606 lock_buffer(bh);
607 jbd_lock_bh_state(bh);
608
609 /* We now hold the buffer lock so it is safe to query the buffer
610 * state. Is the buffer dirty?
611 *
612 * If so, there are two possibilities. The buffer may be
613 * non-journaled, and undergoing a quite legitimate writeback.
614 * Otherwise, it is journaled, and we don't expect dirty buffers
615 * in that state (the buffers should be marked JBD_Dirty
616 * instead.) So either the IO is being done under our own
617 * control and this is a bug, or it's a third party IO such as
618 * dump(8) (which may leave the buffer scheduled for read ---
619 * ie. locked but not dirty) or tune2fs (which may actually have
620 * the buffer dirtied, ugh.) */
621
622 if (buffer_dirty(bh)) {
623 /*
624 * First question: is this buffer already part of the current
625 * transaction or the existing committing transaction?
626 */
627 if (jh->b_transaction) {
628 J_ASSERT_JH(jh,
629 jh->b_transaction == transaction ||
630 jh->b_transaction ==
631 journal->j_committing_transaction);
632 if (jh->b_next_transaction)
633 J_ASSERT_JH(jh, jh->b_next_transaction ==
634 transaction);
Jan Karaf91d1d02009-07-13 16:16:20 -0400635 warn_dirty_buffer(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700636 }
637 /*
638 * In any case we need to clean the dirty flag and we must
639 * do it under the buffer lock to be sure we don't race
640 * with running write-out.
641 */
Jan Karaf91d1d02009-07-13 16:16:20 -0400642 JBUFFER_TRACE(jh, "Journalling dirty buffer");
643 clear_buffer_dirty(bh);
644 set_buffer_jbddirty(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700645 }
646
647 unlock_buffer(bh);
648
649 error = -EROFS;
650 if (is_handle_aborted(handle)) {
651 jbd_unlock_bh_state(bh);
652 goto out;
653 }
654 error = 0;
655
656 /*
657 * The buffer is already part of this transaction if b_transaction or
658 * b_next_transaction points to it
659 */
660 if (jh->b_transaction == transaction ||
661 jh->b_next_transaction == transaction)
662 goto done;
663
664 /*
Josef Bacik9fc7c632008-04-17 10:38:59 -0400665 * this is the first time this transaction is touching this buffer,
666 * reset the modified flag
667 */
668 jh->b_modified = 0;
669
670 /*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700671 * If there is already a copy-out version of this buffer, then we don't
672 * need to make another one
673 */
674 if (jh->b_frozen_data) {
675 JBUFFER_TRACE(jh, "has frozen data");
676 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
677 jh->b_next_transaction = transaction;
678 goto done;
679 }
680
681 /* Is there data here we need to preserve? */
682
683 if (jh->b_transaction && jh->b_transaction != transaction) {
684 JBUFFER_TRACE(jh, "owned by older transaction");
685 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
686 J_ASSERT_JH(jh, jh->b_transaction ==
687 journal->j_committing_transaction);
688
689 /* There is one case we have to be very careful about.
690 * If the committing transaction is currently writing
691 * this buffer out to disk and has NOT made a copy-out,
692 * then we cannot modify the buffer contents at all
693 * right now. The essence of copy-out is that it is the
694 * extra copy, not the primary copy, which gets
695 * journaled. If the primary copy is already going to
696 * disk then we cannot do copy-out here. */
697
698 if (jh->b_jlist == BJ_Shadow) {
699 DEFINE_WAIT_BIT(wait, &bh->b_state, BH_Unshadow);
700 wait_queue_head_t *wqh;
701
702 wqh = bit_waitqueue(&bh->b_state, BH_Unshadow);
703
704 JBUFFER_TRACE(jh, "on shadow: sleep");
705 jbd_unlock_bh_state(bh);
706 /* commit wakes up all shadow buffers after IO */
707 for ( ; ; ) {
708 prepare_to_wait(wqh, &wait.wait,
709 TASK_UNINTERRUPTIBLE);
710 if (jh->b_jlist != BJ_Shadow)
711 break;
712 schedule();
713 }
714 finish_wait(wqh, &wait.wait);
715 goto repeat;
716 }
717
718 /* Only do the copy if the currently-owning transaction
719 * still needs it. If it is on the Forget list, the
720 * committing transaction is past that stage. The
721 * buffer had better remain locked during the kmalloc,
722 * but that should be true --- we hold the journal lock
723 * still and the buffer is already on the BUF_JOURNAL
724 * list so won't be flushed.
725 *
726 * Subtle point, though: if this is a get_undo_access,
727 * then we will be relying on the frozen_data to contain
728 * the new value of the committed_data record after the
729 * transaction, so we HAVE to force the frozen_data copy
730 * in that case. */
731
732 if (jh->b_jlist != BJ_Forget || force_copy) {
733 JBUFFER_TRACE(jh, "generate frozen data");
734 if (!frozen_buffer) {
735 JBUFFER_TRACE(jh, "allocate memory for buffer");
736 jbd_unlock_bh_state(bh);
737 frozen_buffer =
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400738 jbd2_alloc(jh2bh(jh)->b_size,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700739 GFP_NOFS);
740 if (!frozen_buffer) {
741 printk(KERN_EMERG
742 "%s: OOM for frozen_buffer\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400743 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700744 JBUFFER_TRACE(jh, "oom!");
745 error = -ENOMEM;
746 jbd_lock_bh_state(bh);
747 goto done;
748 }
749 goto repeat;
750 }
751 jh->b_frozen_data = frozen_buffer;
752 frozen_buffer = NULL;
753 need_copy = 1;
754 }
755 jh->b_next_transaction = transaction;
756 }
757
758
759 /*
760 * Finally, if the buffer is not journaled right now, we need to make
761 * sure it doesn't get written to disk before the caller actually
762 * commits the new data
763 */
764 if (!jh->b_transaction) {
765 JBUFFER_TRACE(jh, "no transaction");
766 J_ASSERT_JH(jh, !jh->b_next_transaction);
767 jh->b_transaction = transaction;
768 JBUFFER_TRACE(jh, "file as BJ_Reserved");
769 spin_lock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700770 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700771 spin_unlock(&journal->j_list_lock);
772 }
773
774done:
775 if (need_copy) {
776 struct page *page;
777 int offset;
778 char *source;
779
780 J_EXPECT_JH(jh, buffer_uptodate(jh2bh(jh)),
781 "Possible IO failure.\n");
782 page = jh2bh(jh)->b_page;
Theodore Ts'oa1dd5332010-12-18 13:13:40 -0500783 offset = offset_in_page(jh2bh(jh)->b_data);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700784 source = kmap_atomic(page, KM_USER0);
Jan Kara13ceef02010-07-14 07:56:33 +0200785 /* Fire data frozen trigger just before we copy the data */
786 jbd2_buffer_frozen_trigger(jh, source + offset,
787 jh->b_triggers);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700788 memcpy(jh->b_frozen_data, source+offset, jh2bh(jh)->b_size);
789 kunmap_atomic(source, KM_USER0);
Joel Beckere06c8222008-09-11 15:35:47 -0700790
791 /*
792 * Now that the frozen data is saved off, we need to store
793 * any matching triggers.
794 */
795 jh->b_frozen_triggers = jh->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700796 }
797 jbd_unlock_bh_state(bh);
798
799 /*
800 * If we are about to journal a buffer, then any revoke pending on it is
801 * no longer valid
802 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700803 jbd2_journal_cancel_revoke(handle, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700804
805out:
806 if (unlikely(frozen_buffer)) /* It's usually NULL */
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400807 jbd2_free(frozen_buffer, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700808
809 JBUFFER_TRACE(jh, "exit");
810 return error;
811}
812
813/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700814 * int jbd2_journal_get_write_access() - notify intent to modify a buffer for metadata (not data) update.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700815 * @handle: transaction to add buffer modifications to
816 * @bh: bh to be used for metadata writes
817 * @credits: variable that will receive credits for the buffer
818 *
819 * Returns an error code or 0 on success.
820 *
821 * In full data journalling mode the buffer may be of type BJ_AsyncData,
822 * because we're write()ing a buffer which is also part of a shared mapping.
823 */
824
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700825int jbd2_journal_get_write_access(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700826{
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700827 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700828 int rc;
829
830 /* We do not want to get caught playing with fields which the
831 * log thread also manipulates. Make sure that the buffer
832 * completes any outstanding IO before proceeding. */
833 rc = do_get_write_access(handle, jh, 0);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700834 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700835 return rc;
836}
837
838
839/*
840 * When the user wants to journal a newly created buffer_head
841 * (ie. getblk() returned a new buffer and we are going to populate it
842 * manually rather than reading off disk), then we need to keep the
843 * buffer_head locked until it has been completely filled with new
844 * data. In this case, we should be able to make the assertion that
845 * the bh is not already part of an existing transaction.
846 *
847 * The buffer should already be locked by the caller by this point.
848 * There is no lock ranking violation: it was a newly created,
849 * unlocked buffer beforehand. */
850
851/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700852 * int jbd2_journal_get_create_access () - notify intent to use newly created bh
Dave Kleikamp470decc2006-10-11 01:20:57 -0700853 * @handle: transaction to new buffer to
854 * @bh: new buffer.
855 *
856 * Call this if you create a new bh.
857 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700858int jbd2_journal_get_create_access(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700859{
860 transaction_t *transaction = handle->h_transaction;
861 journal_t *journal = transaction->t_journal;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700862 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700863 int err;
864
865 jbd_debug(5, "journal_head %p\n", jh);
866 err = -EROFS;
867 if (is_handle_aborted(handle))
868 goto out;
869 err = 0;
870
871 JBUFFER_TRACE(jh, "entry");
872 /*
873 * The buffer may already belong to this transaction due to pre-zeroing
874 * in the filesystem's new_block code. It may also be on the previous,
875 * committing transaction's lists, but it HAS to be in Forget state in
876 * that case: the transaction must have deleted the buffer for it to be
877 * reused here.
878 */
879 jbd_lock_bh_state(bh);
880 spin_lock(&journal->j_list_lock);
881 J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
882 jh->b_transaction == NULL ||
883 (jh->b_transaction == journal->j_committing_transaction &&
884 jh->b_jlist == BJ_Forget)));
885
886 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
887 J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
888
889 if (jh->b_transaction == NULL) {
Jan Karaf91d1d02009-07-13 16:16:20 -0400890 /*
891 * Previous jbd2_journal_forget() could have left the buffer
892 * with jbddirty bit set because it was being committed. When
893 * the commit finished, we've filed the buffer for
894 * checkpointing and marked it dirty. Now we are reallocating
895 * the buffer so the transaction freeing it must have
896 * committed and so it's safe to clear the dirty bit.
897 */
898 clear_buffer_dirty(jh2bh(jh));
Dave Kleikamp470decc2006-10-11 01:20:57 -0700899 jh->b_transaction = transaction;
Josef Bacik9fc7c632008-04-17 10:38:59 -0400900
901 /* first access by this transaction */
902 jh->b_modified = 0;
903
Dave Kleikamp470decc2006-10-11 01:20:57 -0700904 JBUFFER_TRACE(jh, "file as BJ_Reserved");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700905 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700906 } else if (jh->b_transaction == journal->j_committing_transaction) {
Josef Bacik9fc7c632008-04-17 10:38:59 -0400907 /* first access by this transaction */
908 jh->b_modified = 0;
909
Dave Kleikamp470decc2006-10-11 01:20:57 -0700910 JBUFFER_TRACE(jh, "set next transaction");
911 jh->b_next_transaction = transaction;
912 }
913 spin_unlock(&journal->j_list_lock);
914 jbd_unlock_bh_state(bh);
915
916 /*
917 * akpm: I added this. ext3_alloc_branch can pick up new indirect
918 * blocks which contain freed but then revoked metadata. We need
919 * to cancel the revoke in case we end up freeing it yet again
920 * and the reallocating as data - this would cause a second revoke,
921 * which hits an assertion error.
922 */
923 JBUFFER_TRACE(jh, "cancelling revoke");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700924 jbd2_journal_cancel_revoke(handle, jh);
925 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700926out:
927 return err;
928}
929
930/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700931 * int jbd2_journal_get_undo_access() - Notify intent to modify metadata with
Dave Kleikamp470decc2006-10-11 01:20:57 -0700932 * non-rewindable consequences
933 * @handle: transaction
934 * @bh: buffer to undo
935 * @credits: store the number of taken credits here (if not NULL)
936 *
937 * Sometimes there is a need to distinguish between metadata which has
938 * been committed to disk and that which has not. The ext3fs code uses
939 * this for freeing and allocating space, we have to make sure that we
940 * do not reuse freed space until the deallocation has been committed,
941 * since if we overwrote that space we would make the delete
942 * un-rewindable in case of a crash.
943 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700944 * To deal with that, jbd2_journal_get_undo_access requests write access to a
Dave Kleikamp470decc2006-10-11 01:20:57 -0700945 * buffer for parts of non-rewindable operations such as delete
946 * operations on the bitmaps. The journaling code must keep a copy of
947 * the buffer's contents prior to the undo_access call until such time
948 * as we know that the buffer has definitely been committed to disk.
949 *
950 * We never need to know which transaction the committed data is part
951 * of, buffers touched here are guaranteed to be dirtied later and so
952 * will be committed to a new transaction in due course, at which point
953 * we can discard the old committed data pointer.
954 *
955 * Returns error number or 0 on success.
956 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700957int jbd2_journal_get_undo_access(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700958{
959 int err;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700960 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700961 char *committed_data = NULL;
962
963 JBUFFER_TRACE(jh, "entry");
964
965 /*
966 * Do this first --- it can drop the journal lock, so we want to
967 * make sure that obtaining the committed_data is done
968 * atomically wrt. completion of any outstanding commits.
969 */
970 err = do_get_write_access(handle, jh, 1);
971 if (err)
972 goto out;
973
974repeat:
975 if (!jh->b_committed_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400976 committed_data = jbd2_alloc(jh2bh(jh)->b_size, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700977 if (!committed_data) {
978 printk(KERN_EMERG "%s: No memory for committed data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400979 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700980 err = -ENOMEM;
981 goto out;
982 }
983 }
984
985 jbd_lock_bh_state(bh);
986 if (!jh->b_committed_data) {
987 /* Copy out the current buffer contents into the
988 * preserved, committed copy. */
989 JBUFFER_TRACE(jh, "generate b_committed data");
990 if (!committed_data) {
991 jbd_unlock_bh_state(bh);
992 goto repeat;
993 }
994
995 jh->b_committed_data = committed_data;
996 committed_data = NULL;
997 memcpy(jh->b_committed_data, bh->b_data, bh->b_size);
998 }
999 jbd_unlock_bh_state(bh);
1000out:
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001001 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001002 if (unlikely(committed_data))
Mingming Caoaf1e76d2007-10-16 18:38:25 -04001003 jbd2_free(committed_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001004 return err;
1005}
1006
1007/**
Joel Beckere06c8222008-09-11 15:35:47 -07001008 * void jbd2_journal_set_triggers() - Add triggers for commit writeout
1009 * @bh: buffer to trigger on
1010 * @type: struct jbd2_buffer_trigger_type containing the trigger(s).
1011 *
1012 * Set any triggers on this journal_head. This is always safe, because
1013 * triggers for a committing buffer will be saved off, and triggers for
1014 * a running transaction will match the buffer in that transaction.
1015 *
1016 * Call with NULL to clear the triggers.
1017 */
1018void jbd2_journal_set_triggers(struct buffer_head *bh,
1019 struct jbd2_buffer_trigger_type *type)
1020{
1021 struct journal_head *jh = bh2jh(bh);
1022
1023 jh->b_triggers = type;
1024}
1025
Jan Kara13ceef02010-07-14 07:56:33 +02001026void jbd2_buffer_frozen_trigger(struct journal_head *jh, void *mapped_data,
Joel Beckere06c8222008-09-11 15:35:47 -07001027 struct jbd2_buffer_trigger_type *triggers)
1028{
1029 struct buffer_head *bh = jh2bh(jh);
1030
Jan Kara13ceef02010-07-14 07:56:33 +02001031 if (!triggers || !triggers->t_frozen)
Joel Beckere06c8222008-09-11 15:35:47 -07001032 return;
1033
Jan Kara13ceef02010-07-14 07:56:33 +02001034 triggers->t_frozen(triggers, bh, mapped_data, bh->b_size);
Joel Beckere06c8222008-09-11 15:35:47 -07001035}
1036
1037void jbd2_buffer_abort_trigger(struct journal_head *jh,
1038 struct jbd2_buffer_trigger_type *triggers)
1039{
1040 if (!triggers || !triggers->t_abort)
1041 return;
1042
1043 triggers->t_abort(triggers, jh2bh(jh));
1044}
1045
1046
1047
1048/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001049 * int jbd2_journal_dirty_metadata() - mark a buffer as containing dirty metadata
Dave Kleikamp470decc2006-10-11 01:20:57 -07001050 * @handle: transaction to add buffer to.
1051 * @bh: buffer to mark
1052 *
1053 * mark dirty metadata which needs to be journaled as part of the current
1054 * transaction.
1055 *
1056 * The buffer is placed on the transaction's metadata list and is marked
1057 * as belonging to the transaction.
1058 *
1059 * Returns error number or 0 on success.
1060 *
1061 * Special care needs to be taken if the buffer already belongs to the
1062 * current committing transaction (in which case we should have frozen
1063 * data present for that commit). In that case, we don't relink the
1064 * buffer: that only gets done when the old transaction finally
1065 * completes its commit.
1066 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001067int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001068{
1069 transaction_t *transaction = handle->h_transaction;
1070 journal_t *journal = transaction->t_journal;
1071 struct journal_head *jh = bh2jh(bh);
1072
1073 jbd_debug(5, "journal_head %p\n", jh);
1074 JBUFFER_TRACE(jh, "entry");
1075 if (is_handle_aborted(handle))
1076 goto out;
1077
1078 jbd_lock_bh_state(bh);
1079
1080 if (jh->b_modified == 0) {
1081 /*
1082 * This buffer's got modified and becoming part
1083 * of the transaction. This needs to be done
1084 * once a transaction -bzzz
1085 */
1086 jh->b_modified = 1;
1087 J_ASSERT_JH(jh, handle->h_buffer_credits > 0);
1088 handle->h_buffer_credits--;
1089 }
1090
1091 /*
1092 * fastpath, to avoid expensive locking. If this buffer is already
1093 * on the running transaction's metadata list there is nothing to do.
1094 * Nobody can take it off again because there is a handle open.
1095 * I _think_ we're OK here with SMP barriers - a mistaken decision will
1096 * result in this test being false, so we go in and take the locks.
1097 */
1098 if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) {
1099 JBUFFER_TRACE(jh, "fastpath");
1100 J_ASSERT_JH(jh, jh->b_transaction ==
1101 journal->j_running_transaction);
1102 goto out_unlock_bh;
1103 }
1104
1105 set_buffer_jbddirty(bh);
1106
1107 /*
1108 * Metadata already on the current transaction list doesn't
1109 * need to be filed. Metadata on another transaction's list must
1110 * be committing, and will be refiled once the commit completes:
1111 * leave it alone for now.
1112 */
1113 if (jh->b_transaction != transaction) {
1114 JBUFFER_TRACE(jh, "already on other transaction");
1115 J_ASSERT_JH(jh, jh->b_transaction ==
1116 journal->j_committing_transaction);
1117 J_ASSERT_JH(jh, jh->b_next_transaction == transaction);
1118 /* And this case is illegal: we can't reuse another
1119 * transaction's data buffer, ever. */
1120 goto out_unlock_bh;
1121 }
1122
1123 /* That test should have eliminated the following case: */
Mingming Cao40191912008-01-28 23:58:27 -05001124 J_ASSERT_JH(jh, jh->b_frozen_data == NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001125
1126 JBUFFER_TRACE(jh, "file as BJ_Metadata");
1127 spin_lock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001128 __jbd2_journal_file_buffer(jh, handle->h_transaction, BJ_Metadata);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001129 spin_unlock(&journal->j_list_lock);
1130out_unlock_bh:
1131 jbd_unlock_bh_state(bh);
1132out:
1133 JBUFFER_TRACE(jh, "exit");
1134 return 0;
1135}
1136
1137/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001138 * jbd2_journal_release_buffer: undo a get_write_access without any buffer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001139 * updates, if the update decided in the end that it didn't need access.
1140 *
1141 */
1142void
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001143jbd2_journal_release_buffer(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001144{
1145 BUFFER_TRACE(bh, "entry");
1146}
1147
1148/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001149 * void jbd2_journal_forget() - bforget() for potentially-journaled buffers.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001150 * @handle: transaction handle
1151 * @bh: bh to 'forget'
1152 *
1153 * We can only do the bforget if there are no commits pending against the
1154 * buffer. If the buffer is dirty in the current running transaction we
1155 * can safely unlink it.
1156 *
1157 * bh may not be a journalled buffer at all - it may be a non-JBD
1158 * buffer which came off the hashtable. Check for this.
1159 *
1160 * Decrements bh->b_count by one.
1161 *
1162 * Allow this call even if the handle has aborted --- it may be part of
1163 * the caller's cleanup after an abort.
1164 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001165int jbd2_journal_forget (handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001166{
1167 transaction_t *transaction = handle->h_transaction;
1168 journal_t *journal = transaction->t_journal;
1169 struct journal_head *jh;
1170 int drop_reserve = 0;
1171 int err = 0;
Josef Bacik1dfc3222008-04-17 10:38:59 -04001172 int was_modified = 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001173
1174 BUFFER_TRACE(bh, "entry");
1175
1176 jbd_lock_bh_state(bh);
1177 spin_lock(&journal->j_list_lock);
1178
1179 if (!buffer_jbd(bh))
1180 goto not_jbd;
1181 jh = bh2jh(bh);
1182
1183 /* Critical error: attempting to delete a bitmap buffer, maybe?
1184 * Don't do any jbd operations, and return an error. */
1185 if (!J_EXPECT_JH(jh, !jh->b_committed_data,
1186 "inconsistent data on disk")) {
1187 err = -EIO;
1188 goto not_jbd;
1189 }
1190
Josef Bacik1dfc3222008-04-17 10:38:59 -04001191 /* keep track of wether or not this transaction modified us */
1192 was_modified = jh->b_modified;
1193
Dave Kleikamp470decc2006-10-11 01:20:57 -07001194 /*
1195 * The buffer's going from the transaction, we must drop
1196 * all references -bzzz
1197 */
1198 jh->b_modified = 0;
1199
1200 if (jh->b_transaction == handle->h_transaction) {
1201 J_ASSERT_JH(jh, !jh->b_frozen_data);
1202
1203 /* If we are forgetting a buffer which is already part
1204 * of this transaction, then we can just drop it from
1205 * the transaction immediately. */
1206 clear_buffer_dirty(bh);
1207 clear_buffer_jbddirty(bh);
1208
1209 JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
1210
Josef Bacik1dfc3222008-04-17 10:38:59 -04001211 /*
1212 * we only want to drop a reference if this transaction
1213 * modified the buffer
1214 */
1215 if (was_modified)
1216 drop_reserve = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001217
1218 /*
1219 * We are no longer going to journal this buffer.
1220 * However, the commit of this transaction is still
1221 * important to the buffer: the delete that we are now
1222 * processing might obsolete an old log entry, so by
1223 * committing, we can satisfy the buffer's checkpoint.
1224 *
1225 * So, if we have a checkpoint on the buffer, we should
1226 * now refile the buffer on our BJ_Forget list so that
1227 * we know to remove the checkpoint after we commit.
1228 */
1229
1230 if (jh->b_cp_transaction) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001231 __jbd2_journal_temp_unlink_buffer(jh);
1232 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001233 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001234 __jbd2_journal_unfile_buffer(jh);
1235 jbd2_journal_remove_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001236 __brelse(bh);
1237 if (!buffer_jbd(bh)) {
1238 spin_unlock(&journal->j_list_lock);
1239 jbd_unlock_bh_state(bh);
1240 __bforget(bh);
1241 goto drop;
1242 }
1243 }
1244 } else if (jh->b_transaction) {
1245 J_ASSERT_JH(jh, (jh->b_transaction ==
1246 journal->j_committing_transaction));
1247 /* However, if the buffer is still owned by a prior
1248 * (committing) transaction, we can't drop it yet... */
1249 JBUFFER_TRACE(jh, "belongs to older transaction");
1250 /* ... but we CAN drop it from the new transaction if we
1251 * have also modified it since the original commit. */
1252
1253 if (jh->b_next_transaction) {
1254 J_ASSERT(jh->b_next_transaction == transaction);
1255 jh->b_next_transaction = NULL;
Josef Bacik1dfc3222008-04-17 10:38:59 -04001256
1257 /*
1258 * only drop a reference if this transaction modified
1259 * the buffer
1260 */
1261 if (was_modified)
1262 drop_reserve = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001263 }
1264 }
1265
1266not_jbd:
1267 spin_unlock(&journal->j_list_lock);
1268 jbd_unlock_bh_state(bh);
1269 __brelse(bh);
1270drop:
1271 if (drop_reserve) {
1272 /* no need to reserve log space for this block -bzzz */
1273 handle->h_buffer_credits++;
1274 }
1275 return err;
1276}
1277
1278/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001279 * int jbd2_journal_stop() - complete a transaction
Dave Kleikamp470decc2006-10-11 01:20:57 -07001280 * @handle: tranaction to complete.
1281 *
1282 * All done for a particular handle.
1283 *
1284 * There is not much action needed here. We just return any remaining
1285 * buffer credits to the transaction and remove the handle. The only
1286 * complication is that we need to start a commit operation if the
1287 * filesystem is marked for synchronous update.
1288 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001289 * jbd2_journal_stop itself will not usually return an error, but it may
Dave Kleikamp470decc2006-10-11 01:20:57 -07001290 * do so in unusual circumstances. In particular, expect it to
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001291 * return -EIO if a jbd2_journal_abort has been executed since the
Dave Kleikamp470decc2006-10-11 01:20:57 -07001292 * transaction began.
1293 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001294int jbd2_journal_stop(handle_t *handle)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001295{
1296 transaction_t *transaction = handle->h_transaction;
1297 journal_t *journal = transaction->t_journal;
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001298 int err, wait_for_commit = 0;
1299 tid_t tid;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001300 pid_t pid;
1301
Dave Kleikamp470decc2006-10-11 01:20:57 -07001302 J_ASSERT(journal_current_handle() == handle);
1303
1304 if (is_handle_aborted(handle))
1305 err = -EIO;
OGAWA Hirofumi3e2a5322006-10-19 23:29:11 -07001306 else {
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001307 J_ASSERT(atomic_read(&transaction->t_updates) > 0);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001308 err = 0;
OGAWA Hirofumi3e2a5322006-10-19 23:29:11 -07001309 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001310
1311 if (--handle->h_ref > 0) {
1312 jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
1313 handle->h_ref);
1314 return err;
1315 }
1316
1317 jbd_debug(4, "Handle %p going down\n", handle);
1318
1319 /*
1320 * Implement synchronous transaction batching. If the handle
1321 * was synchronous, don't force a commit immediately. Let's
Josef Bacike07f7182008-11-26 01:14:26 -05001322 * yield and let another thread piggyback onto this
1323 * transaction. Keep doing that while new threads continue to
1324 * arrive. It doesn't cost much - we're about to run a commit
1325 * and sleep on IO anyway. Speeds up many-threaded, many-dir
1326 * operations by 30x or more...
Dave Kleikamp470decc2006-10-11 01:20:57 -07001327 *
Josef Bacike07f7182008-11-26 01:14:26 -05001328 * We try and optimize the sleep time against what the
1329 * underlying disk can do, instead of having a static sleep
1330 * time. This is useful for the case where our storage is so
1331 * fast that it is more optimal to go ahead and force a flush
1332 * and wait for the transaction to be committed than it is to
1333 * wait for an arbitrary amount of time for new writers to
1334 * join the transaction. We achieve this by measuring how
1335 * long it takes to commit a transaction, and compare it with
1336 * how long this transaction has been running, and if run time
1337 * < commit time then we sleep for the delta and commit. This
1338 * greatly helps super fast disks that would see slowdowns as
1339 * more threads started doing fsyncs.
1340 *
1341 * But don't do this if this process was the most recent one
1342 * to perform a synchronous write. We do this to detect the
1343 * case where a single process is doing a stream of sync
1344 * writes. No point in waiting for joiners in that case.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001345 */
1346 pid = current->pid;
1347 if (handle->h_sync && journal->j_last_sync_writer != pid) {
Josef Bacike07f7182008-11-26 01:14:26 -05001348 u64 commit_time, trans_time;
1349
Dave Kleikamp470decc2006-10-11 01:20:57 -07001350 journal->j_last_sync_writer = pid;
Josef Bacike07f7182008-11-26 01:14:26 -05001351
Theodore Ts'oa931da62010-08-03 21:35:12 -04001352 read_lock(&journal->j_state_lock);
Josef Bacike07f7182008-11-26 01:14:26 -05001353 commit_time = journal->j_average_commit_time;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001354 read_unlock(&journal->j_state_lock);
Josef Bacike07f7182008-11-26 01:14:26 -05001355
1356 trans_time = ktime_to_ns(ktime_sub(ktime_get(),
1357 transaction->t_start_time));
1358
Theodore Ts'o30773842009-01-03 20:27:38 -05001359 commit_time = max_t(u64, commit_time,
1360 1000*journal->j_min_batch_time);
Josef Bacike07f7182008-11-26 01:14:26 -05001361 commit_time = min_t(u64, commit_time,
Theodore Ts'o30773842009-01-03 20:27:38 -05001362 1000*journal->j_max_batch_time);
Josef Bacike07f7182008-11-26 01:14:26 -05001363
1364 if (trans_time < commit_time) {
1365 ktime_t expires = ktime_add_ns(ktime_get(),
1366 commit_time);
1367 set_current_state(TASK_UNINTERRUPTIBLE);
1368 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1369 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001370 }
1371
Theodore Ts'o70585482009-03-25 23:35:46 -04001372 if (handle->h_sync)
1373 transaction->t_synchronous_commit = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001374 current->journal_info = NULL;
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001375 atomic_sub(handle->h_buffer_credits,
1376 &transaction->t_outstanding_credits);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001377
1378 /*
1379 * If the handle is marked SYNC, we need to set another commit
1380 * going! We also want to force a commit if the current
1381 * transaction is occupying too much of the log, or if the
1382 * transaction is too old now.
1383 */
1384 if (handle->h_sync ||
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001385 (atomic_read(&transaction->t_outstanding_credits) >
1386 journal->j_max_transaction_buffers) ||
1387 time_after_eq(jiffies, transaction->t_expires)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001388 /* Do this even for aborted journals: an abort still
1389 * completes the commit thread, it just doesn't write
1390 * anything to disk. */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001391
Dave Kleikamp470decc2006-10-11 01:20:57 -07001392 jbd_debug(2, "transaction too old, requesting commit for "
1393 "handle %p\n", handle);
1394 /* This is non-blocking */
Theodore Ts'oc35a56a2010-05-16 05:00:00 -04001395 jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001396
1397 /*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001398 * Special case: JBD2_SYNC synchronous updates require us
Dave Kleikamp470decc2006-10-11 01:20:57 -07001399 * to wait for the commit to complete.
1400 */
1401 if (handle->h_sync && !(current->flags & PF_MEMALLOC))
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001402 wait_for_commit = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001403 }
1404
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001405 /*
1406 * Once we drop t_updates, if it goes to zero the transaction
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001407 * could start committing on us and eventually disappear. So
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001408 * once we do this, we must not dereference transaction
1409 * pointer again.
1410 */
1411 tid = transaction->t_tid;
1412 if (atomic_dec_and_test(&transaction->t_updates)) {
1413 wake_up(&journal->j_wait_updates);
1414 if (journal->j_barrier_count)
1415 wake_up(&journal->j_wait_transaction_locked);
1416 }
1417
1418 if (wait_for_commit)
1419 err = jbd2_log_wait_commit(journal, tid);
1420
Ingo Molnar3295f0e2008-08-11 10:30:30 +02001421 lock_map_release(&handle->h_lockdep_map);
Mingming Cao7b751062008-01-28 23:58:27 -05001422
Mingming Caoaf1e76d2007-10-16 18:38:25 -04001423 jbd2_free_handle(handle);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001424 return err;
1425}
1426
Randy Dunlap5648ba52008-04-17 10:38:59 -04001427/**
1428 * int jbd2_journal_force_commit() - force any uncommitted transactions
Dave Kleikamp470decc2006-10-11 01:20:57 -07001429 * @journal: journal to force
1430 *
1431 * For synchronous operations: force any uncommitted transactions
1432 * to disk. May seem kludgy, but it reuses all the handle batching
1433 * code in a very simple manner.
1434 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001435int jbd2_journal_force_commit(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001436{
1437 handle_t *handle;
1438 int ret;
1439
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001440 handle = jbd2_journal_start(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001441 if (IS_ERR(handle)) {
1442 ret = PTR_ERR(handle);
1443 } else {
1444 handle->h_sync = 1;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001445 ret = jbd2_journal_stop(handle);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001446 }
1447 return ret;
1448}
1449
1450/*
1451 *
1452 * List management code snippets: various functions for manipulating the
1453 * transaction buffer lists.
1454 *
1455 */
1456
1457/*
1458 * Append a buffer to a transaction list, given the transaction's list head
1459 * pointer.
1460 *
1461 * j_list_lock is held.
1462 *
1463 * jbd_lock_bh_state(jh2bh(jh)) is held.
1464 */
1465
1466static inline void
1467__blist_add_buffer(struct journal_head **list, struct journal_head *jh)
1468{
1469 if (!*list) {
1470 jh->b_tnext = jh->b_tprev = jh;
1471 *list = jh;
1472 } else {
1473 /* Insert at the tail of the list to preserve order */
1474 struct journal_head *first = *list, *last = first->b_tprev;
1475 jh->b_tprev = last;
1476 jh->b_tnext = first;
1477 last->b_tnext = first->b_tprev = jh;
1478 }
1479}
1480
1481/*
1482 * Remove a buffer from a transaction list, given the transaction's list
1483 * head pointer.
1484 *
1485 * Called with j_list_lock held, and the journal may not be locked.
1486 *
1487 * jbd_lock_bh_state(jh2bh(jh)) is held.
1488 */
1489
1490static inline void
1491__blist_del_buffer(struct journal_head **list, struct journal_head *jh)
1492{
1493 if (*list == jh) {
1494 *list = jh->b_tnext;
1495 if (*list == jh)
1496 *list = NULL;
1497 }
1498 jh->b_tprev->b_tnext = jh->b_tnext;
1499 jh->b_tnext->b_tprev = jh->b_tprev;
1500}
1501
1502/*
1503 * Remove a buffer from the appropriate transaction list.
1504 *
1505 * Note that this function can *change* the value of
Jan Kara87c89c22008-07-11 19:27:31 -04001506 * bh->b_transaction->t_buffers, t_forget, t_iobuf_list, t_shadow_list,
1507 * t_log_list or t_reserved_list. If the caller is holding onto a copy of one
1508 * of these pointers, it could go bad. Generally the caller needs to re-read
1509 * the pointer from the transaction_t.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001510 *
1511 * Called under j_list_lock. The journal may not be locked.
1512 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001513void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001514{
1515 struct journal_head **list = NULL;
1516 transaction_t *transaction;
1517 struct buffer_head *bh = jh2bh(jh);
1518
1519 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1520 transaction = jh->b_transaction;
1521 if (transaction)
1522 assert_spin_locked(&transaction->t_journal->j_list_lock);
1523
1524 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1525 if (jh->b_jlist != BJ_None)
Mingming Cao40191912008-01-28 23:58:27 -05001526 J_ASSERT_JH(jh, transaction != NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001527
1528 switch (jh->b_jlist) {
1529 case BJ_None:
1530 return;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001531 case BJ_Metadata:
1532 transaction->t_nr_buffers--;
1533 J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
1534 list = &transaction->t_buffers;
1535 break;
1536 case BJ_Forget:
1537 list = &transaction->t_forget;
1538 break;
1539 case BJ_IO:
1540 list = &transaction->t_iobuf_list;
1541 break;
1542 case BJ_Shadow:
1543 list = &transaction->t_shadow_list;
1544 break;
1545 case BJ_LogCtl:
1546 list = &transaction->t_log_list;
1547 break;
1548 case BJ_Reserved:
1549 list = &transaction->t_reserved_list;
1550 break;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001551 }
1552
1553 __blist_del_buffer(list, jh);
1554 jh->b_jlist = BJ_None;
1555 if (test_clear_buffer_jbddirty(bh))
1556 mark_buffer_dirty(bh); /* Expose it to the VM */
1557}
1558
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001559void __jbd2_journal_unfile_buffer(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001560{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001561 __jbd2_journal_temp_unlink_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001562 jh->b_transaction = NULL;
1563}
1564
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001565void jbd2_journal_unfile_buffer(journal_t *journal, struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001566{
1567 jbd_lock_bh_state(jh2bh(jh));
1568 spin_lock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001569 __jbd2_journal_unfile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001570 spin_unlock(&journal->j_list_lock);
1571 jbd_unlock_bh_state(jh2bh(jh));
1572}
1573
1574/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001575 * Called from jbd2_journal_try_to_free_buffers().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001576 *
1577 * Called under jbd_lock_bh_state(bh)
1578 */
1579static void
1580__journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh)
1581{
1582 struct journal_head *jh;
1583
1584 jh = bh2jh(bh);
1585
1586 if (buffer_locked(bh) || buffer_dirty(bh))
1587 goto out;
1588
Mingming Cao40191912008-01-28 23:58:27 -05001589 if (jh->b_next_transaction != NULL)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001590 goto out;
1591
1592 spin_lock(&journal->j_list_lock);
Jan Kara87c89c22008-07-11 19:27:31 -04001593 if (jh->b_cp_transaction != NULL && jh->b_transaction == NULL) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001594 /* written-back checkpointed metadata buffer */
1595 if (jh->b_jlist == BJ_None) {
1596 JBUFFER_TRACE(jh, "remove from checkpoint list");
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001597 __jbd2_journal_remove_checkpoint(jh);
1598 jbd2_journal_remove_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001599 __brelse(bh);
1600 }
1601 }
1602 spin_unlock(&journal->j_list_lock);
1603out:
1604 return;
1605}
1606
Dave Kleikamp470decc2006-10-11 01:20:57 -07001607/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001608 * int jbd2_journal_try_to_free_buffers() - try to free page buffers.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001609 * @journal: journal for operation
1610 * @page: to try and free
Mingming Cao530576b2008-07-13 21:06:39 -04001611 * @gfp_mask: we use the mask to detect how hard should we try to release
1612 * buffers. If __GFP_WAIT and __GFP_FS is set, we wait for commit code to
1613 * release the buffers.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001614 *
1615 *
1616 * For all the buffers on this page,
1617 * if they are fully written out ordered data, move them onto BUF_CLEAN
1618 * so try_to_free_buffers() can reap them.
1619 *
1620 * This function returns non-zero if we wish try_to_free_buffers()
1621 * to be called. We do this if the page is releasable by try_to_free_buffers().
1622 * We also do it if the page has locked or dirty buffers and the caller wants
1623 * us to perform sync or async writeout.
1624 *
1625 * This complicates JBD locking somewhat. We aren't protected by the
1626 * BKL here. We wish to remove the buffer from its committing or
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001627 * running transaction's ->t_datalist via __jbd2_journal_unfile_buffer.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001628 *
1629 * This may *change* the value of transaction_t->t_datalist, so anyone
1630 * who looks at t_datalist needs to lock against this function.
1631 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001632 * Even worse, someone may be doing a jbd2_journal_dirty_data on this
1633 * buffer. So we need to lock against that. jbd2_journal_dirty_data()
Dave Kleikamp470decc2006-10-11 01:20:57 -07001634 * will come out of the lock with the buffer dirty, which makes it
1635 * ineligible for release here.
1636 *
1637 * Who else is affected by this? hmm... Really the only contender
1638 * is do_get_write_access() - it could be looking at the buffer while
1639 * journal_try_to_free_buffer() is changing its state. But that
1640 * cannot happen because we never reallocate freed data as metadata
1641 * while the data is part of a transaction. Yes?
Mingming Cao530576b2008-07-13 21:06:39 -04001642 *
1643 * Return 0 on failure, 1 on success
Dave Kleikamp470decc2006-10-11 01:20:57 -07001644 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001645int jbd2_journal_try_to_free_buffers(journal_t *journal,
Mingming Cao530576b2008-07-13 21:06:39 -04001646 struct page *page, gfp_t gfp_mask)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001647{
1648 struct buffer_head *head;
1649 struct buffer_head *bh;
1650 int ret = 0;
1651
1652 J_ASSERT(PageLocked(page));
1653
1654 head = page_buffers(page);
1655 bh = head;
1656 do {
1657 struct journal_head *jh;
1658
1659 /*
1660 * We take our own ref against the journal_head here to avoid
1661 * having to add tons of locking around each instance of
Mingming Cao530576b2008-07-13 21:06:39 -04001662 * jbd2_journal_remove_journal_head() and
1663 * jbd2_journal_put_journal_head().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001664 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001665 jh = jbd2_journal_grab_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001666 if (!jh)
1667 continue;
1668
1669 jbd_lock_bh_state(bh);
1670 __journal_try_to_free_buffer(journal, bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001671 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001672 jbd_unlock_bh_state(bh);
1673 if (buffer_jbd(bh))
1674 goto busy;
1675 } while ((bh = bh->b_this_page) != head);
Mingming Cao530576b2008-07-13 21:06:39 -04001676
Dave Kleikamp470decc2006-10-11 01:20:57 -07001677 ret = try_to_free_buffers(page);
Mingming Cao530576b2008-07-13 21:06:39 -04001678
Dave Kleikamp470decc2006-10-11 01:20:57 -07001679busy:
1680 return ret;
1681}
1682
1683/*
1684 * This buffer is no longer needed. If it is on an older transaction's
1685 * checkpoint list we need to record it on this transaction's forget list
1686 * to pin this buffer (and hence its checkpointing transaction) down until
1687 * this transaction commits. If the buffer isn't on a checkpoint list, we
1688 * release it.
1689 * Returns non-zero if JBD no longer has an interest in the buffer.
1690 *
1691 * Called under j_list_lock.
1692 *
1693 * Called under jbd_lock_bh_state(bh).
1694 */
1695static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
1696{
1697 int may_free = 1;
1698 struct buffer_head *bh = jh2bh(jh);
1699
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001700 __jbd2_journal_unfile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001701
1702 if (jh->b_cp_transaction) {
1703 JBUFFER_TRACE(jh, "on running+cp transaction");
Jan Karaf91d1d02009-07-13 16:16:20 -04001704 /*
1705 * We don't want to write the buffer anymore, clear the
1706 * bit so that we don't confuse checks in
1707 * __journal_file_buffer
1708 */
1709 clear_buffer_dirty(bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001710 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001711 may_free = 0;
1712 } else {
1713 JBUFFER_TRACE(jh, "on running transaction");
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001714 jbd2_journal_remove_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001715 __brelse(bh);
1716 }
1717 return may_free;
1718}
1719
1720/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001721 * jbd2_journal_invalidatepage
Dave Kleikamp470decc2006-10-11 01:20:57 -07001722 *
1723 * This code is tricky. It has a number of cases to deal with.
1724 *
1725 * There are two invariants which this code relies on:
1726 *
1727 * i_size must be updated on disk before we start calling invalidatepage on the
1728 * data.
1729 *
1730 * This is done in ext3 by defining an ext3_setattr method which
1731 * updates i_size before truncate gets going. By maintaining this
1732 * invariant, we can be sure that it is safe to throw away any buffers
1733 * attached to the current transaction: once the transaction commits,
1734 * we know that the data will not be needed.
1735 *
1736 * Note however that we can *not* throw away data belonging to the
1737 * previous, committing transaction!
1738 *
1739 * Any disk blocks which *are* part of the previous, committing
1740 * transaction (and which therefore cannot be discarded immediately) are
1741 * not going to be reused in the new running transaction
1742 *
1743 * The bitmap committed_data images guarantee this: any block which is
1744 * allocated in one transaction and removed in the next will be marked
1745 * as in-use in the committed_data bitmap, so cannot be reused until
1746 * the next transaction to delete the block commits. This means that
1747 * leaving committing buffers dirty is quite safe: the disk blocks
1748 * cannot be reallocated to a different file and so buffer aliasing is
1749 * not possible.
1750 *
1751 *
1752 * The above applies mainly to ordered data mode. In writeback mode we
1753 * don't make guarantees about the order in which data hits disk --- in
1754 * particular we don't guarantee that new dirty data is flushed before
1755 * transaction commit --- so it is always safe just to discard data
1756 * immediately in that mode. --sct
1757 */
1758
1759/*
1760 * The journal_unmap_buffer helper function returns zero if the buffer
1761 * concerned remains pinned as an anonymous buffer belonging to an older
1762 * transaction.
1763 *
1764 * We're outside-transaction here. Either or both of j_running_transaction
1765 * and j_committing_transaction may be NULL.
1766 */
1767static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh)
1768{
1769 transaction_t *transaction;
1770 struct journal_head *jh;
1771 int may_free = 1;
1772 int ret;
1773
1774 BUFFER_TRACE(bh, "entry");
1775
1776 /*
1777 * It is safe to proceed here without the j_list_lock because the
1778 * buffers cannot be stolen by try_to_free_buffers as long as we are
1779 * holding the page lock. --sct
1780 */
1781
1782 if (!buffer_jbd(bh))
1783 goto zap_buffer_unlocked;
1784
Jan Kara87c89c22008-07-11 19:27:31 -04001785 /* OK, we have data buffer in journaled mode */
Theodore Ts'oa931da62010-08-03 21:35:12 -04001786 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001787 jbd_lock_bh_state(bh);
1788 spin_lock(&journal->j_list_lock);
1789
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001790 jh = jbd2_journal_grab_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001791 if (!jh)
1792 goto zap_buffer_no_jh;
1793
dingdinghuaba869022010-02-15 16:35:42 -05001794 /*
1795 * We cannot remove the buffer from checkpoint lists until the
1796 * transaction adding inode to orphan list (let's call it T)
1797 * is committed. Otherwise if the transaction changing the
1798 * buffer would be cleaned from the journal before T is
1799 * committed, a crash will cause that the correct contents of
1800 * the buffer will be lost. On the other hand we have to
1801 * clear the buffer dirty bit at latest at the moment when the
1802 * transaction marking the buffer as freed in the filesystem
1803 * structures is committed because from that moment on the
1804 * buffer can be reallocated and used by a different page.
1805 * Since the block hasn't been freed yet but the inode has
1806 * already been added to orphan list, it is safe for us to add
1807 * the buffer to BJ_Forget list of the newest transaction.
1808 */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001809 transaction = jh->b_transaction;
1810 if (transaction == NULL) {
1811 /* First case: not on any transaction. If it
1812 * has no checkpoint link, then we can zap it:
1813 * it's a writeback-mode buffer so we don't care
1814 * if it hits disk safely. */
1815 if (!jh->b_cp_transaction) {
1816 JBUFFER_TRACE(jh, "not on any transaction: zap");
1817 goto zap_buffer;
1818 }
1819
1820 if (!buffer_dirty(bh)) {
1821 /* bdflush has written it. We can drop it now */
1822 goto zap_buffer;
1823 }
1824
1825 /* OK, it must be in the journal but still not
1826 * written fully to disk: it's metadata or
1827 * journaled data... */
1828
1829 if (journal->j_running_transaction) {
1830 /* ... and once the current transaction has
1831 * committed, the buffer won't be needed any
1832 * longer. */
1833 JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
1834 ret = __dispose_buffer(jh,
1835 journal->j_running_transaction);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001836 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001837 spin_unlock(&journal->j_list_lock);
1838 jbd_unlock_bh_state(bh);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001839 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001840 return ret;
1841 } else {
1842 /* There is no currently-running transaction. So the
1843 * orphan record which we wrote for this file must have
1844 * passed into commit. We must attach this buffer to
1845 * the committing transaction, if it exists. */
1846 if (journal->j_committing_transaction) {
1847 JBUFFER_TRACE(jh, "give to committing trans");
1848 ret = __dispose_buffer(jh,
1849 journal->j_committing_transaction);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001850 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001851 spin_unlock(&journal->j_list_lock);
1852 jbd_unlock_bh_state(bh);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001853 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001854 return ret;
1855 } else {
1856 /* The orphan record's transaction has
1857 * committed. We can cleanse this buffer */
1858 clear_buffer_jbddirty(bh);
1859 goto zap_buffer;
1860 }
1861 }
1862 } else if (transaction == journal->j_committing_transaction) {
Eric Sandeen9b579882006-10-28 10:38:28 -07001863 JBUFFER_TRACE(jh, "on committing transaction");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001864 /*
dingdinghuaba869022010-02-15 16:35:42 -05001865 * The buffer is committing, we simply cannot touch
1866 * it. So we just set j_next_transaction to the
1867 * running transaction (if there is one) and mark
1868 * buffer as freed so that commit code knows it should
1869 * clear dirty bits when it is done with the buffer.
1870 */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001871 set_buffer_freed(bh);
dingdinghuaba869022010-02-15 16:35:42 -05001872 if (journal->j_running_transaction && buffer_jbddirty(bh))
1873 jh->b_next_transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001874 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001875 spin_unlock(&journal->j_list_lock);
1876 jbd_unlock_bh_state(bh);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001877 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001878 return 0;
1879 } else {
1880 /* Good, the buffer belongs to the running transaction.
1881 * We are writing our own transaction's data, not any
1882 * previous one's, so it is safe to throw it away
1883 * (remember that we expect the filesystem to have set
1884 * i_size already for this truncate so recovery will not
1885 * expose the disk blocks we are discarding here.) */
1886 J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
Eric Sandeen9b579882006-10-28 10:38:28 -07001887 JBUFFER_TRACE(jh, "on running transaction");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001888 may_free = __dispose_buffer(jh, transaction);
1889 }
1890
1891zap_buffer:
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001892 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001893zap_buffer_no_jh:
1894 spin_unlock(&journal->j_list_lock);
1895 jbd_unlock_bh_state(bh);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001896 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001897zap_buffer_unlocked:
1898 clear_buffer_dirty(bh);
1899 J_ASSERT_BH(bh, !buffer_jbddirty(bh));
1900 clear_buffer_mapped(bh);
1901 clear_buffer_req(bh);
1902 clear_buffer_new(bh);
1903 bh->b_bdev = NULL;
1904 return may_free;
1905}
1906
1907/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001908 * void jbd2_journal_invalidatepage()
Dave Kleikamp470decc2006-10-11 01:20:57 -07001909 * @journal: journal to use for flush...
1910 * @page: page to flush
1911 * @offset: length of page to invalidate.
1912 *
1913 * Reap page buffers containing data after offset in page.
1914 *
1915 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001916void jbd2_journal_invalidatepage(journal_t *journal,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001917 struct page *page,
1918 unsigned long offset)
1919{
1920 struct buffer_head *head, *bh, *next;
1921 unsigned int curr_off = 0;
1922 int may_free = 1;
1923
1924 if (!PageLocked(page))
1925 BUG();
1926 if (!page_has_buffers(page))
1927 return;
1928
1929 /* We will potentially be playing with lists other than just the
1930 * data lists (especially for journaled data mode), so be
1931 * cautious in our locking. */
1932
1933 head = bh = page_buffers(page);
1934 do {
1935 unsigned int next_off = curr_off + bh->b_size;
1936 next = bh->b_this_page;
1937
1938 if (offset <= curr_off) {
1939 /* This block is wholly outside the truncation point */
1940 lock_buffer(bh);
1941 may_free &= journal_unmap_buffer(journal, bh);
1942 unlock_buffer(bh);
1943 }
1944 curr_off = next_off;
1945 bh = next;
1946
1947 } while (bh != head);
1948
1949 if (!offset) {
1950 if (may_free && try_to_free_buffers(page))
1951 J_ASSERT(!page_has_buffers(page));
1952 }
1953}
1954
1955/*
1956 * File a buffer on the given transaction list.
1957 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001958void __jbd2_journal_file_buffer(struct journal_head *jh,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001959 transaction_t *transaction, int jlist)
1960{
1961 struct journal_head **list = NULL;
1962 int was_dirty = 0;
1963 struct buffer_head *bh = jh2bh(jh);
1964
1965 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1966 assert_spin_locked(&transaction->t_journal->j_list_lock);
1967
1968 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1969 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
Mingming Cao40191912008-01-28 23:58:27 -05001970 jh->b_transaction == NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001971
1972 if (jh->b_transaction && jh->b_jlist == jlist)
1973 return;
1974
Dave Kleikamp470decc2006-10-11 01:20:57 -07001975 if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
1976 jlist == BJ_Shadow || jlist == BJ_Forget) {
Jan Karaf91d1d02009-07-13 16:16:20 -04001977 /*
1978 * For metadata buffers, we track dirty bit in buffer_jbddirty
1979 * instead of buffer_dirty. We should not see a dirty bit set
1980 * here because we clear it in do_get_write_access but e.g.
1981 * tune2fs can modify the sb and set the dirty bit at any time
1982 * so we try to gracefully handle that.
1983 */
1984 if (buffer_dirty(bh))
1985 warn_dirty_buffer(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001986 if (test_clear_buffer_dirty(bh) ||
1987 test_clear_buffer_jbddirty(bh))
1988 was_dirty = 1;
1989 }
1990
1991 if (jh->b_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001992 __jbd2_journal_temp_unlink_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001993 jh->b_transaction = transaction;
1994
1995 switch (jlist) {
1996 case BJ_None:
1997 J_ASSERT_JH(jh, !jh->b_committed_data);
1998 J_ASSERT_JH(jh, !jh->b_frozen_data);
1999 return;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002000 case BJ_Metadata:
2001 transaction->t_nr_buffers++;
2002 list = &transaction->t_buffers;
2003 break;
2004 case BJ_Forget:
2005 list = &transaction->t_forget;
2006 break;
2007 case BJ_IO:
2008 list = &transaction->t_iobuf_list;
2009 break;
2010 case BJ_Shadow:
2011 list = &transaction->t_shadow_list;
2012 break;
2013 case BJ_LogCtl:
2014 list = &transaction->t_log_list;
2015 break;
2016 case BJ_Reserved:
2017 list = &transaction->t_reserved_list;
2018 break;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002019 }
2020
2021 __blist_add_buffer(list, jh);
2022 jh->b_jlist = jlist;
2023
2024 if (was_dirty)
2025 set_buffer_jbddirty(bh);
2026}
2027
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002028void jbd2_journal_file_buffer(struct journal_head *jh,
Dave Kleikamp470decc2006-10-11 01:20:57 -07002029 transaction_t *transaction, int jlist)
2030{
2031 jbd_lock_bh_state(jh2bh(jh));
2032 spin_lock(&transaction->t_journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002033 __jbd2_journal_file_buffer(jh, transaction, jlist);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002034 spin_unlock(&transaction->t_journal->j_list_lock);
2035 jbd_unlock_bh_state(jh2bh(jh));
2036}
2037
2038/*
2039 * Remove a buffer from its current buffer list in preparation for
2040 * dropping it from its current transaction entirely. If the buffer has
2041 * already started to be used by a subsequent transaction, refile the
2042 * buffer on that transaction's metadata list.
2043 *
2044 * Called under journal->j_list_lock
2045 *
2046 * Called under jbd_lock_bh_state(jh2bh(jh))
2047 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002048void __jbd2_journal_refile_buffer(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002049{
dingdinghuaba869022010-02-15 16:35:42 -05002050 int was_dirty, jlist;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002051 struct buffer_head *bh = jh2bh(jh);
2052
2053 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
2054 if (jh->b_transaction)
2055 assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
2056
2057 /* If the buffer is now unused, just drop it. */
2058 if (jh->b_next_transaction == NULL) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002059 __jbd2_journal_unfile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002060 return;
2061 }
2062
2063 /*
2064 * It has been modified by a later transaction: add it to the new
2065 * transaction's metadata list.
2066 */
2067
2068 was_dirty = test_clear_buffer_jbddirty(bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002069 __jbd2_journal_temp_unlink_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002070 jh->b_transaction = jh->b_next_transaction;
2071 jh->b_next_transaction = NULL;
dingdinghuaba869022010-02-15 16:35:42 -05002072 if (buffer_freed(bh))
2073 jlist = BJ_Forget;
2074 else if (jh->b_modified)
2075 jlist = BJ_Metadata;
2076 else
2077 jlist = BJ_Reserved;
2078 __jbd2_journal_file_buffer(jh, jh->b_transaction, jlist);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002079 J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
2080
2081 if (was_dirty)
2082 set_buffer_jbddirty(bh);
2083}
2084
2085/*
2086 * For the unlocked version of this call, also make sure that any
2087 * hanging journal_head is cleaned up if necessary.
2088 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002089 * __jbd2_journal_refile_buffer is usually called as part of a single locked
Dave Kleikamp470decc2006-10-11 01:20:57 -07002090 * operation on a buffer_head, in which the caller is probably going to
2091 * be hooking the journal_head onto other lists. In that case it is up
2092 * to the caller to remove the journal_head if necessary. For the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002093 * unlocked jbd2_journal_refile_buffer call, the caller isn't going to be
Dave Kleikamp470decc2006-10-11 01:20:57 -07002094 * doing anything else to the buffer so we need to do the cleanup
2095 * ourselves to avoid a jh leak.
2096 *
2097 * *** The journal_head may be freed by this call! ***
2098 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002099void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002100{
2101 struct buffer_head *bh = jh2bh(jh);
2102
2103 jbd_lock_bh_state(bh);
2104 spin_lock(&journal->j_list_lock);
2105
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002106 __jbd2_journal_refile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002107 jbd_unlock_bh_state(bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002108 jbd2_journal_remove_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002109
2110 spin_unlock(&journal->j_list_lock);
2111 __brelse(bh);
2112}
Jan Karac851ed52008-07-11 19:27:31 -04002113
2114/*
2115 * File inode in the inode list of the handle's transaction
2116 */
2117int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode)
2118{
2119 transaction_t *transaction = handle->h_transaction;
2120 journal_t *journal = transaction->t_journal;
2121
2122 if (is_handle_aborted(handle))
2123 return -EIO;
2124
2125 jbd_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino,
2126 transaction->t_tid);
2127
2128 /*
2129 * First check whether inode isn't already on the transaction's
2130 * lists without taking the lock. Note that this check is safe
2131 * without the lock as we cannot race with somebody removing inode
2132 * from the transaction. The reason is that we remove inode from the
2133 * transaction only in journal_release_jbd_inode() and when we commit
2134 * the transaction. We are guarded from the first case by holding
2135 * a reference to the inode. We are safe against the second case
2136 * because if jinode->i_transaction == transaction, commit code
2137 * cannot touch the transaction because we hold reference to it,
2138 * and if jinode->i_next_transaction == transaction, commit code
2139 * will only file the inode where we want it.
2140 */
2141 if (jinode->i_transaction == transaction ||
2142 jinode->i_next_transaction == transaction)
2143 return 0;
2144
2145 spin_lock(&journal->j_list_lock);
2146
2147 if (jinode->i_transaction == transaction ||
2148 jinode->i_next_transaction == transaction)
2149 goto done;
2150
Jan Kara81be12c2011-05-24 11:52:40 -04002151 /*
2152 * We only ever set this variable to 1 so the test is safe. Since
2153 * t_need_data_flush is likely to be set, we do the test to save some
2154 * cacheline bouncing
2155 */
2156 if (!transaction->t_need_data_flush)
2157 transaction->t_need_data_flush = 1;
Jan Karac851ed52008-07-11 19:27:31 -04002158 /* On some different transaction's list - should be
2159 * the committing one */
2160 if (jinode->i_transaction) {
2161 J_ASSERT(jinode->i_next_transaction == NULL);
2162 J_ASSERT(jinode->i_transaction ==
2163 journal->j_committing_transaction);
2164 jinode->i_next_transaction = transaction;
2165 goto done;
2166 }
2167 /* Not on any transaction list... */
2168 J_ASSERT(!jinode->i_next_transaction);
2169 jinode->i_transaction = transaction;
2170 list_add(&jinode->i_list, &transaction->t_inode_list);
2171done:
2172 spin_unlock(&journal->j_list_lock);
2173
2174 return 0;
2175}
2176
2177/*
Jan Kara7f5aa212009-02-10 11:15:34 -05002178 * File truncate and transaction commit interact with each other in a
2179 * non-trivial way. If a transaction writing data block A is
2180 * committing, we cannot discard the data by truncate until we have
2181 * written them. Otherwise if we crashed after the transaction with
2182 * write has committed but before the transaction with truncate has
2183 * committed, we could see stale data in block A. This function is a
2184 * helper to solve this problem. It starts writeout of the truncated
2185 * part in case it is in the committing transaction.
2186 *
2187 * Filesystem code must call this function when inode is journaled in
2188 * ordered mode before truncation happens and after the inode has been
2189 * placed on orphan list with the new inode size. The second condition
2190 * avoids the race that someone writes new data and we start
2191 * committing the transaction after this function has been called but
2192 * before a transaction for truncate is started (and furthermore it
2193 * allows us to optimize the case where the addition to orphan list
2194 * happens in the same transaction as write --- we don't have to write
2195 * any data in such case).
Jan Karac851ed52008-07-11 19:27:31 -04002196 */
Jan Kara7f5aa212009-02-10 11:15:34 -05002197int jbd2_journal_begin_ordered_truncate(journal_t *journal,
2198 struct jbd2_inode *jinode,
Jan Karac851ed52008-07-11 19:27:31 -04002199 loff_t new_size)
2200{
Jan Kara7f5aa212009-02-10 11:15:34 -05002201 transaction_t *inode_trans, *commit_trans;
Jan Karac851ed52008-07-11 19:27:31 -04002202 int ret = 0;
2203
Jan Kara7f5aa212009-02-10 11:15:34 -05002204 /* This is a quick check to avoid locking if not necessary */
2205 if (!jinode->i_transaction)
Jan Karac851ed52008-07-11 19:27:31 -04002206 goto out;
Jan Kara7f5aa212009-02-10 11:15:34 -05002207 /* Locks are here just to force reading of recent values, it is
2208 * enough that the transaction was not committing before we started
2209 * a transaction adding the inode to orphan list */
Theodore Ts'oa931da62010-08-03 21:35:12 -04002210 read_lock(&journal->j_state_lock);
Jan Karac851ed52008-07-11 19:27:31 -04002211 commit_trans = journal->j_committing_transaction;
Theodore Ts'oa931da62010-08-03 21:35:12 -04002212 read_unlock(&journal->j_state_lock);
Jan Kara7f5aa212009-02-10 11:15:34 -05002213 spin_lock(&journal->j_list_lock);
2214 inode_trans = jinode->i_transaction;
2215 spin_unlock(&journal->j_list_lock);
2216 if (inode_trans == commit_trans) {
2217 ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping,
Jan Karac851ed52008-07-11 19:27:31 -04002218 new_size, LLONG_MAX);
2219 if (ret)
2220 jbd2_journal_abort(journal, ret);
2221 }
2222out:
2223 return ret;
2224}