blob: 6bf0a242613ec54fb3186931031262e48329f2c6 [file] [log] [blame]
Dave Kleikamp470decc2006-10-11 01:20:57 -07001/*
Uwe Kleine-König58862692007-05-09 07:51:49 +02002 * linux/fs/jbd2/transaction.c
Dave Kleikamp470decc2006-10-11 01:20:57 -07003 *
4 * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
5 *
6 * Copyright 1998 Red Hat corp --- All Rights Reserved
7 *
8 * This file is part of the Linux kernel and is made available under
9 * the terms of the GNU General Public License, version 2, or at your
10 * option, any later version, incorporated herein by reference.
11 *
12 * Generic filesystem transaction handling code; part of the ext2fs
13 * journaling system.
14 *
15 * This file manages transactions (compound commits managed by the
16 * journaling code) and handles (individual atomic operations by the
17 * filesystem).
18 */
19
20#include <linux/time.h>
21#include <linux/fs.h>
Mingming Caof7f4bcc2006-10-11 01:20:59 -070022#include <linux/jbd2.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070023#include <linux/errno.h>
24#include <linux/slab.h>
25#include <linux/timer.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070026#include <linux/mm.h>
27#include <linux/highmem.h>
Josef Bacike07f7182008-11-26 01:14:26 -050028#include <linux/hrtimer.h>
Theodore Ts'o47def822010-07-27 11:56:05 -040029#include <linux/backing-dev.h>
30#include <linux/module.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070031
Adrian Bunk7ddae862006-12-06 20:38:27 -080032static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh);
33
Dave Kleikamp470decc2006-10-11 01:20:57 -070034/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -070035 * jbd2_get_transaction: obtain a new transaction_t object.
Dave Kleikamp470decc2006-10-11 01:20:57 -070036 *
37 * Simply allocate and initialise a new transaction. Create it in
38 * RUNNING state and add it to the current journal (which should not
39 * have an existing running transaction: we only make a new transaction
40 * once we have started to commit the old one).
41 *
42 * Preconditions:
43 * The journal MUST be locked. We don't perform atomic mallocs on the
44 * new transaction and we can't block without protecting against other
45 * processes trying to touch the journal while it is in transition.
46 *
Dave Kleikamp470decc2006-10-11 01:20:57 -070047 */
48
49static transaction_t *
Mingming Caof7f4bcc2006-10-11 01:20:59 -070050jbd2_get_transaction(journal_t *journal, transaction_t *transaction)
Dave Kleikamp470decc2006-10-11 01:20:57 -070051{
52 transaction->t_journal = journal;
53 transaction->t_state = T_RUNNING;
Josef Bacike07f7182008-11-26 01:14:26 -050054 transaction->t_start_time = ktime_get();
Dave Kleikamp470decc2006-10-11 01:20:57 -070055 transaction->t_tid = journal->j_transaction_sequence++;
56 transaction->t_expires = jiffies + journal->j_commit_interval;
57 spin_lock_init(&transaction->t_handle_lock);
Theodore Ts'oa51dca92010-08-02 08:43:25 -040058 atomic_set(&transaction->t_updates, 0);
59 atomic_set(&transaction->t_outstanding_credits, 0);
Theodore Ts'o8dd42042010-08-03 21:38:29 -040060 atomic_set(&transaction->t_handle_count, 0);
Jan Karac851ed52008-07-11 19:27:31 -040061 INIT_LIST_HEAD(&transaction->t_inode_list);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -040062 INIT_LIST_HEAD(&transaction->t_private_list);
Dave Kleikamp470decc2006-10-11 01:20:57 -070063
64 /* Set up the commit timer for the new transaction. */
Andreas Dilgerb1f485f2009-08-10 22:51:53 -040065 journal->j_commit_timer.expires = round_jiffies_up(transaction->t_expires);
Dave Kleikamp470decc2006-10-11 01:20:57 -070066 add_timer(&journal->j_commit_timer);
67
68 J_ASSERT(journal->j_running_transaction == NULL);
69 journal->j_running_transaction = transaction;
Johann Lombardi8e85fb32008-01-28 23:58:27 -050070 transaction->t_max_wait = 0;
71 transaction->t_start = jiffies;
Dave Kleikamp470decc2006-10-11 01:20:57 -070072
73 return transaction;
74}
75
76/*
77 * Handle management.
78 *
79 * A handle_t is an object which represents a single atomic update to a
80 * filesystem, and which tracks all of the modifications which form part
81 * of that one update.
82 */
83
84/*
Theodore Ts'o6d0bf002010-08-09 17:28:38 -040085 * Update transiaction's maximum wait time, if debugging is enabled.
86 *
87 * In order for t_max_wait to be reliable, it must be protected by a
88 * lock. But doing so will mean that start_this_handle() can not be
89 * run in parallel on SMP systems, which limits our scalability. So
90 * unless debugging is enabled, we no longer update t_max_wait, which
91 * means that maximum wait time reported by the jbd2_run_stats
92 * tracepoint will always be zero.
93 */
94static inline void update_t_max_wait(transaction_t *transaction)
95{
96#ifdef CONFIG_JBD2_DEBUG
97 unsigned long ts = jiffies;
98
99 if (jbd2_journal_enable_debug &&
100 time_after(transaction->t_start, ts)) {
101 ts = jbd2_time_diff(ts, transaction->t_start);
102 spin_lock(&transaction->t_handle_lock);
103 if (ts > transaction->t_max_wait)
104 transaction->t_max_wait = ts;
105 spin_unlock(&transaction->t_handle_lock);
106 }
107#endif
108}
109
110/*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700111 * start_this_handle: Given a handle, deal with any locking or stalling
112 * needed to make sure that there is enough journal space for the handle
113 * to begin. Attach the handle to a transaction and set up the
114 * transaction's buffer credits.
115 */
116
Theodore Ts'o47def822010-07-27 11:56:05 -0400117static int start_this_handle(journal_t *journal, handle_t *handle,
118 int gfp_mask)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700119{
120 transaction_t *transaction;
121 int needed;
122 int nblocks = handle->h_buffer_credits;
123 transaction_t *new_transaction = NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700124
125 if (nblocks > journal->j_max_transaction_buffers) {
126 printk(KERN_ERR "JBD: %s wants too many credits (%d > %d)\n",
127 current->comm, nblocks,
128 journal->j_max_transaction_buffers);
Theodore Ts'o47def822010-07-27 11:56:05 -0400129 return -ENOSPC;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700130 }
131
132alloc_transaction:
133 if (!journal->j_running_transaction) {
Theodore Ts'o47def822010-07-27 11:56:05 -0400134 new_transaction = kzalloc(sizeof(*new_transaction), gfp_mask);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700135 if (!new_transaction) {
Theodore Ts'o47def822010-07-27 11:56:05 -0400136 /*
137 * If __GFP_FS is not present, then we may be
138 * being called from inside the fs writeback
139 * layer, so we MUST NOT fail. Since
140 * __GFP_NOFAIL is going away, we will arrange
141 * to retry the allocation ourselves.
142 */
143 if ((gfp_mask & __GFP_FS) == 0) {
144 congestion_wait(BLK_RW_ASYNC, HZ/50);
145 goto alloc_transaction;
146 }
147 return -ENOMEM;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700148 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700149 }
150
151 jbd_debug(3, "New handle %p going live.\n", handle);
152
Dave Kleikamp470decc2006-10-11 01:20:57 -0700153 /*
154 * We need to hold j_state_lock until t_updates has been incremented,
155 * for proper journal barrier handling
156 */
Theodore Ts'oa931da62010-08-03 21:35:12 -0400157repeat:
158 read_lock(&journal->j_state_lock);
Theodore Ts'o5c2178e2010-10-27 21:30:04 -0400159 BUG_ON(journal->j_flags & JBD2_UNMOUNT);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700160 if (is_journal_aborted(journal) ||
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700161 (journal->j_errno != 0 && !(journal->j_flags & JBD2_ACK_ERR))) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400162 read_unlock(&journal->j_state_lock);
Theodore Ts'o47def822010-07-27 11:56:05 -0400163 kfree(new_transaction);
164 return -EROFS;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700165 }
166
167 /* Wait on the journal's transaction barrier if necessary */
168 if (journal->j_barrier_count) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400169 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700170 wait_event(journal->j_wait_transaction_locked,
171 journal->j_barrier_count == 0);
172 goto repeat;
173 }
174
175 if (!journal->j_running_transaction) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400176 read_unlock(&journal->j_state_lock);
177 if (!new_transaction)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700178 goto alloc_transaction;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400179 write_lock(&journal->j_state_lock);
180 if (!journal->j_running_transaction) {
181 jbd2_get_transaction(journal, new_transaction);
182 new_transaction = NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700183 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400184 write_unlock(&journal->j_state_lock);
185 goto repeat;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700186 }
187
188 transaction = journal->j_running_transaction;
189
190 /*
191 * If the current transaction is locked down for commit, wait for the
192 * lock to be released.
193 */
194 if (transaction->t_state == T_LOCKED) {
195 DEFINE_WAIT(wait);
196
197 prepare_to_wait(&journal->j_wait_transaction_locked,
198 &wait, TASK_UNINTERRUPTIBLE);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400199 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700200 schedule();
201 finish_wait(&journal->j_wait_transaction_locked, &wait);
202 goto repeat;
203 }
204
205 /*
206 * If there is not enough space left in the log to write all potential
207 * buffers requested by this operation, we need to stall pending a log
208 * checkpoint to free some more log space.
209 */
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400210 needed = atomic_add_return(nblocks,
211 &transaction->t_outstanding_credits);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700212
213 if (needed > journal->j_max_transaction_buffers) {
214 /*
215 * If the current transaction is already too large, then start
216 * to commit it: we can then go back and attach this handle to
217 * a new transaction.
218 */
219 DEFINE_WAIT(wait);
220
221 jbd_debug(2, "Handle %p starting new commit...\n", handle);
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400222 atomic_sub(nblocks, &transaction->t_outstanding_credits);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700223 prepare_to_wait(&journal->j_wait_transaction_locked, &wait,
224 TASK_UNINTERRUPTIBLE);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700225 __jbd2_log_start_commit(journal, transaction->t_tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400226 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700227 schedule();
228 finish_wait(&journal->j_wait_transaction_locked, &wait);
229 goto repeat;
230 }
231
232 /*
233 * The commit code assumes that it can get enough log space
234 * without forcing a checkpoint. This is *critical* for
235 * correctness: a checkpoint of a buffer which is also
236 * associated with a committing transaction creates a deadlock,
237 * so commit simply cannot force through checkpoints.
238 *
239 * We must therefore ensure the necessary space in the journal
240 * *before* starting to dirty potentially checkpointed buffers
241 * in the new transaction.
242 *
243 * The worst part is, any transaction currently committing can
244 * reduce the free space arbitrarily. Be careful to account for
245 * those buffers when checkpointing.
246 */
247
248 /*
249 * @@@ AKPM: This seems rather over-defensive. We're giving commit
250 * a _lot_ of headroom: 1/4 of the journal plus the size of
251 * the committing transaction. Really, we only need to give it
252 * committing_transaction->t_outstanding_credits plus "enough" for
253 * the log control blocks.
254 * Also, this test is inconsitent with the matching one in
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700255 * jbd2_journal_extend().
Dave Kleikamp470decc2006-10-11 01:20:57 -0700256 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700257 if (__jbd2_log_space_left(journal) < jbd_space_needed(journal)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700258 jbd_debug(2, "Handle %p waiting for checkpoint...\n", handle);
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400259 atomic_sub(nblocks, &transaction->t_outstanding_credits);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400260 read_unlock(&journal->j_state_lock);
261 write_lock(&journal->j_state_lock);
262 if (__jbd2_log_space_left(journal) < jbd_space_needed(journal))
263 __jbd2_log_wait_for_space(journal);
264 write_unlock(&journal->j_state_lock);
265 goto repeat;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700266 }
267
268 /* OK, account for the buffers that this operation expects to
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400269 * use and add the handle to the running transaction.
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400270 */
Theodore Ts'o6d0bf002010-08-09 17:28:38 -0400271 update_t_max_wait(transaction);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700272 handle->h_transaction = transaction;
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400273 atomic_inc(&transaction->t_updates);
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400274 atomic_inc(&transaction->t_handle_count);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700275 jbd_debug(4, "Handle %p given %d credits (total %d, free %d)\n",
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400276 handle, nblocks,
277 atomic_read(&transaction->t_outstanding_credits),
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700278 __jbd2_log_space_left(journal));
Theodore Ts'oa931da62010-08-03 21:35:12 -0400279 read_unlock(&journal->j_state_lock);
Jan Kara9599b0e2009-08-17 21:23:17 -0400280
281 lock_map_acquire(&handle->h_lockdep_map);
Theodore Ts'o47def822010-07-27 11:56:05 -0400282 kfree(new_transaction);
283 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700284}
285
Mingming Cao7b751062008-01-28 23:58:27 -0500286static struct lock_class_key jbd2_handle_key;
287
Dave Kleikamp470decc2006-10-11 01:20:57 -0700288/* Allocate a new handle. This should probably be in a slab... */
289static handle_t *new_handle(int nblocks)
290{
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400291 handle_t *handle = jbd2_alloc_handle(GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700292 if (!handle)
293 return NULL;
294 memset(handle, 0, sizeof(*handle));
295 handle->h_buffer_credits = nblocks;
296 handle->h_ref = 1;
297
Mingming Cao7b751062008-01-28 23:58:27 -0500298 lockdep_init_map(&handle->h_lockdep_map, "jbd2_handle",
299 &jbd2_handle_key, 0);
300
Dave Kleikamp470decc2006-10-11 01:20:57 -0700301 return handle;
302}
303
304/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700305 * handle_t *jbd2_journal_start() - Obtain a new handle.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700306 * @journal: Journal to start transaction on.
307 * @nblocks: number of block buffer we might modify
308 *
309 * We make sure that the transaction can guarantee at least nblocks of
310 * modified buffers in the log. We block until the log can guarantee
311 * that much space.
312 *
313 * This function is visible to journal users (like ext3fs), so is not
314 * called with the journal already locked.
315 *
316 * Return a pointer to a newly allocated handle, or NULL on failure
317 */
Theodore Ts'o47def822010-07-27 11:56:05 -0400318handle_t *jbd2__journal_start(journal_t *journal, int nblocks, int gfp_mask)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700319{
320 handle_t *handle = journal_current_handle();
321 int err;
322
323 if (!journal)
324 return ERR_PTR(-EROFS);
325
326 if (handle) {
327 J_ASSERT(handle->h_transaction->t_journal == journal);
328 handle->h_ref++;
329 return handle;
330 }
331
332 handle = new_handle(nblocks);
333 if (!handle)
334 return ERR_PTR(-ENOMEM);
335
336 current->journal_info = handle;
337
Theodore Ts'o47def822010-07-27 11:56:05 -0400338 err = start_this_handle(journal, handle, gfp_mask);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700339 if (err < 0) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400340 jbd2_free_handle(handle);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700341 current->journal_info = NULL;
342 handle = ERR_PTR(err);
Mingming Cao7b751062008-01-28 23:58:27 -0500343 goto out;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700344 }
Mingming Cao7b751062008-01-28 23:58:27 -0500345out:
Dave Kleikamp470decc2006-10-11 01:20:57 -0700346 return handle;
347}
Theodore Ts'o47def822010-07-27 11:56:05 -0400348EXPORT_SYMBOL(jbd2__journal_start);
349
350
351handle_t *jbd2_journal_start(journal_t *journal, int nblocks)
352{
353 return jbd2__journal_start(journal, nblocks, GFP_NOFS);
354}
355EXPORT_SYMBOL(jbd2_journal_start);
356
Dave Kleikamp470decc2006-10-11 01:20:57 -0700357
358/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700359 * int jbd2_journal_extend() - extend buffer credits.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700360 * @handle: handle to 'extend'
361 * @nblocks: nr blocks to try to extend by.
362 *
363 * Some transactions, such as large extends and truncates, can be done
364 * atomically all at once or in several stages. The operation requests
365 * a credit for a number of buffer modications in advance, but can
366 * extend its credit if it needs more.
367 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700368 * jbd2_journal_extend tries to give the running handle more buffer credits.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700369 * It does not guarantee that allocation - this is a best-effort only.
370 * The calling process MUST be able to deal cleanly with a failure to
371 * extend here.
372 *
373 * Return 0 on success, non-zero on failure.
374 *
375 * return code < 0 implies an error
376 * return code > 0 implies normal transaction-full status.
377 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700378int jbd2_journal_extend(handle_t *handle, int nblocks)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700379{
380 transaction_t *transaction = handle->h_transaction;
381 journal_t *journal = transaction->t_journal;
382 int result;
383 int wanted;
384
385 result = -EIO;
386 if (is_handle_aborted(handle))
387 goto out;
388
389 result = 1;
390
Theodore Ts'oa931da62010-08-03 21:35:12 -0400391 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700392
393 /* Don't extend a locked-down transaction! */
394 if (handle->h_transaction->t_state != T_RUNNING) {
395 jbd_debug(3, "denied handle %p %d blocks: "
396 "transaction not running\n", handle, nblocks);
397 goto error_out;
398 }
399
400 spin_lock(&transaction->t_handle_lock);
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400401 wanted = atomic_read(&transaction->t_outstanding_credits) + nblocks;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700402
403 if (wanted > journal->j_max_transaction_buffers) {
404 jbd_debug(3, "denied handle %p %d blocks: "
405 "transaction too large\n", handle, nblocks);
406 goto unlock;
407 }
408
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700409 if (wanted > __jbd2_log_space_left(journal)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700410 jbd_debug(3, "denied handle %p %d blocks: "
411 "insufficient log space\n", handle, nblocks);
412 goto unlock;
413 }
414
415 handle->h_buffer_credits += nblocks;
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400416 atomic_add(nblocks, &transaction->t_outstanding_credits);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700417 result = 0;
418
419 jbd_debug(3, "extended handle %p by %d\n", handle, nblocks);
420unlock:
421 spin_unlock(&transaction->t_handle_lock);
422error_out:
Theodore Ts'oa931da62010-08-03 21:35:12 -0400423 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700424out:
425 return result;
426}
427
428
429/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700430 * int jbd2_journal_restart() - restart a handle .
Dave Kleikamp470decc2006-10-11 01:20:57 -0700431 * @handle: handle to restart
432 * @nblocks: nr credits requested
433 *
434 * Restart a handle for a multi-transaction filesystem
435 * operation.
436 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700437 * If the jbd2_journal_extend() call above fails to grant new buffer credits
438 * to a running handle, a call to jbd2_journal_restart will commit the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700439 * handle's transaction so far and reattach the handle to a new
440 * transaction capabable of guaranteeing the requested number of
441 * credits.
442 */
Theodore Ts'o47def822010-07-27 11:56:05 -0400443int jbd2__journal_restart(handle_t *handle, int nblocks, int gfp_mask)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700444{
445 transaction_t *transaction = handle->h_transaction;
446 journal_t *journal = transaction->t_journal;
447 int ret;
448
449 /* If we've had an abort of any type, don't even think about
450 * actually doing the restart! */
451 if (is_handle_aborted(handle))
452 return 0;
453
454 /*
455 * First unlink the handle from its current transaction, and start the
456 * commit on that.
457 */
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400458 J_ASSERT(atomic_read(&transaction->t_updates) > 0);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700459 J_ASSERT(journal_current_handle() == handle);
460
Theodore Ts'oa931da62010-08-03 21:35:12 -0400461 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700462 spin_lock(&transaction->t_handle_lock);
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400463 atomic_sub(handle->h_buffer_credits,
464 &transaction->t_outstanding_credits);
465 if (atomic_dec_and_test(&transaction->t_updates))
Dave Kleikamp470decc2006-10-11 01:20:57 -0700466 wake_up(&journal->j_wait_updates);
467 spin_unlock(&transaction->t_handle_lock);
468
469 jbd_debug(2, "restarting handle %p\n", handle);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700470 __jbd2_log_start_commit(journal, transaction->t_tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400471 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700472
Jan Kara9599b0e2009-08-17 21:23:17 -0400473 lock_map_release(&handle->h_lockdep_map);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700474 handle->h_buffer_credits = nblocks;
Theodore Ts'o47def822010-07-27 11:56:05 -0400475 ret = start_this_handle(journal, handle, gfp_mask);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700476 return ret;
477}
Theodore Ts'o47def822010-07-27 11:56:05 -0400478EXPORT_SYMBOL(jbd2__journal_restart);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700479
480
Theodore Ts'o47def822010-07-27 11:56:05 -0400481int jbd2_journal_restart(handle_t *handle, int nblocks)
482{
483 return jbd2__journal_restart(handle, nblocks, GFP_NOFS);
484}
485EXPORT_SYMBOL(jbd2_journal_restart);
486
Dave Kleikamp470decc2006-10-11 01:20:57 -0700487/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700488 * void jbd2_journal_lock_updates () - establish a transaction barrier.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700489 * @journal: Journal to establish a barrier on.
490 *
491 * This locks out any further updates from being started, and blocks
492 * until all existing updates have completed, returning only once the
493 * journal is in a quiescent state with no updates running.
494 *
495 * The journal lock should not be held on entry.
496 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700497void jbd2_journal_lock_updates(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700498{
499 DEFINE_WAIT(wait);
500
Theodore Ts'oa931da62010-08-03 21:35:12 -0400501 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700502 ++journal->j_barrier_count;
503
504 /* Wait until there are no running updates */
505 while (1) {
506 transaction_t *transaction = journal->j_running_transaction;
507
508 if (!transaction)
509 break;
510
511 spin_lock(&transaction->t_handle_lock);
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400512 if (!atomic_read(&transaction->t_updates)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700513 spin_unlock(&transaction->t_handle_lock);
514 break;
515 }
516 prepare_to_wait(&journal->j_wait_updates, &wait,
517 TASK_UNINTERRUPTIBLE);
518 spin_unlock(&transaction->t_handle_lock);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400519 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700520 schedule();
521 finish_wait(&journal->j_wait_updates, &wait);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400522 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700523 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400524 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700525
526 /*
527 * We have now established a barrier against other normal updates, but
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700528 * we also need to barrier against other jbd2_journal_lock_updates() calls
Dave Kleikamp470decc2006-10-11 01:20:57 -0700529 * to make sure that we serialise special journal-locked operations
530 * too.
531 */
532 mutex_lock(&journal->j_barrier);
533}
534
535/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700536 * void jbd2_journal_unlock_updates (journal_t* journal) - release barrier
Dave Kleikamp470decc2006-10-11 01:20:57 -0700537 * @journal: Journal to release the barrier on.
538 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700539 * Release a transaction barrier obtained with jbd2_journal_lock_updates().
Dave Kleikamp470decc2006-10-11 01:20:57 -0700540 *
541 * Should be called without the journal lock held.
542 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700543void jbd2_journal_unlock_updates (journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700544{
545 J_ASSERT(journal->j_barrier_count != 0);
546
547 mutex_unlock(&journal->j_barrier);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400548 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700549 --journal->j_barrier_count;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400550 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700551 wake_up(&journal->j_wait_transaction_locked);
552}
553
Jan Karaf91d1d02009-07-13 16:16:20 -0400554static void warn_dirty_buffer(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700555{
Jan Karaf91d1d02009-07-13 16:16:20 -0400556 char b[BDEVNAME_SIZE];
Dave Kleikamp470decc2006-10-11 01:20:57 -0700557
Jan Karaf91d1d02009-07-13 16:16:20 -0400558 printk(KERN_WARNING
559 "JBD: Spotted dirty metadata buffer (dev = %s, blocknr = %llu). "
560 "There's a risk of filesystem corruption in case of system "
561 "crash.\n",
562 bdevname(bh->b_bdev, b), (unsigned long long)bh->b_blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700563}
564
565/*
566 * If the buffer is already part of the current transaction, then there
567 * is nothing we need to do. If it is already part of a prior
568 * transaction which we are still committing to disk, then we need to
569 * make sure that we do not overwrite the old copy: we do copy-out to
570 * preserve the copy going to disk. We also account the buffer against
571 * the handle's metadata buffer credits (unless the buffer is already
572 * part of the transaction, that is).
573 *
574 */
575static int
576do_get_write_access(handle_t *handle, struct journal_head *jh,
577 int force_copy)
578{
579 struct buffer_head *bh;
580 transaction_t *transaction;
581 journal_t *journal;
582 int error;
583 char *frozen_buffer = NULL;
584 int need_copy = 0;
585
586 if (is_handle_aborted(handle))
587 return -EROFS;
588
589 transaction = handle->h_transaction;
590 journal = transaction->t_journal;
591
592 jbd_debug(5, "buffer_head %p, force_copy %d\n", jh, force_copy);
593
594 JBUFFER_TRACE(jh, "entry");
595repeat:
596 bh = jh2bh(jh);
597
598 /* @@@ Need to check for errors here at some point. */
599
600 lock_buffer(bh);
601 jbd_lock_bh_state(bh);
602
603 /* We now hold the buffer lock so it is safe to query the buffer
604 * state. Is the buffer dirty?
605 *
606 * If so, there are two possibilities. The buffer may be
607 * non-journaled, and undergoing a quite legitimate writeback.
608 * Otherwise, it is journaled, and we don't expect dirty buffers
609 * in that state (the buffers should be marked JBD_Dirty
610 * instead.) So either the IO is being done under our own
611 * control and this is a bug, or it's a third party IO such as
612 * dump(8) (which may leave the buffer scheduled for read ---
613 * ie. locked but not dirty) or tune2fs (which may actually have
614 * the buffer dirtied, ugh.) */
615
616 if (buffer_dirty(bh)) {
617 /*
618 * First question: is this buffer already part of the current
619 * transaction or the existing committing transaction?
620 */
621 if (jh->b_transaction) {
622 J_ASSERT_JH(jh,
623 jh->b_transaction == transaction ||
624 jh->b_transaction ==
625 journal->j_committing_transaction);
626 if (jh->b_next_transaction)
627 J_ASSERT_JH(jh, jh->b_next_transaction ==
628 transaction);
Jan Karaf91d1d02009-07-13 16:16:20 -0400629 warn_dirty_buffer(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700630 }
631 /*
632 * In any case we need to clean the dirty flag and we must
633 * do it under the buffer lock to be sure we don't race
634 * with running write-out.
635 */
Jan Karaf91d1d02009-07-13 16:16:20 -0400636 JBUFFER_TRACE(jh, "Journalling dirty buffer");
637 clear_buffer_dirty(bh);
638 set_buffer_jbddirty(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700639 }
640
641 unlock_buffer(bh);
642
643 error = -EROFS;
644 if (is_handle_aborted(handle)) {
645 jbd_unlock_bh_state(bh);
646 goto out;
647 }
648 error = 0;
649
650 /*
651 * The buffer is already part of this transaction if b_transaction or
652 * b_next_transaction points to it
653 */
654 if (jh->b_transaction == transaction ||
655 jh->b_next_transaction == transaction)
656 goto done;
657
658 /*
Josef Bacik9fc7c632008-04-17 10:38:59 -0400659 * this is the first time this transaction is touching this buffer,
660 * reset the modified flag
661 */
662 jh->b_modified = 0;
663
664 /*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700665 * If there is already a copy-out version of this buffer, then we don't
666 * need to make another one
667 */
668 if (jh->b_frozen_data) {
669 JBUFFER_TRACE(jh, "has frozen data");
670 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
671 jh->b_next_transaction = transaction;
672 goto done;
673 }
674
675 /* Is there data here we need to preserve? */
676
677 if (jh->b_transaction && jh->b_transaction != transaction) {
678 JBUFFER_TRACE(jh, "owned by older transaction");
679 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
680 J_ASSERT_JH(jh, jh->b_transaction ==
681 journal->j_committing_transaction);
682
683 /* There is one case we have to be very careful about.
684 * If the committing transaction is currently writing
685 * this buffer out to disk and has NOT made a copy-out,
686 * then we cannot modify the buffer contents at all
687 * right now. The essence of copy-out is that it is the
688 * extra copy, not the primary copy, which gets
689 * journaled. If the primary copy is already going to
690 * disk then we cannot do copy-out here. */
691
692 if (jh->b_jlist == BJ_Shadow) {
693 DEFINE_WAIT_BIT(wait, &bh->b_state, BH_Unshadow);
694 wait_queue_head_t *wqh;
695
696 wqh = bit_waitqueue(&bh->b_state, BH_Unshadow);
697
698 JBUFFER_TRACE(jh, "on shadow: sleep");
699 jbd_unlock_bh_state(bh);
700 /* commit wakes up all shadow buffers after IO */
701 for ( ; ; ) {
702 prepare_to_wait(wqh, &wait.wait,
703 TASK_UNINTERRUPTIBLE);
704 if (jh->b_jlist != BJ_Shadow)
705 break;
706 schedule();
707 }
708 finish_wait(wqh, &wait.wait);
709 goto repeat;
710 }
711
712 /* Only do the copy if the currently-owning transaction
713 * still needs it. If it is on the Forget list, the
714 * committing transaction is past that stage. The
715 * buffer had better remain locked during the kmalloc,
716 * but that should be true --- we hold the journal lock
717 * still and the buffer is already on the BUF_JOURNAL
718 * list so won't be flushed.
719 *
720 * Subtle point, though: if this is a get_undo_access,
721 * then we will be relying on the frozen_data to contain
722 * the new value of the committed_data record after the
723 * transaction, so we HAVE to force the frozen_data copy
724 * in that case. */
725
726 if (jh->b_jlist != BJ_Forget || force_copy) {
727 JBUFFER_TRACE(jh, "generate frozen data");
728 if (!frozen_buffer) {
729 JBUFFER_TRACE(jh, "allocate memory for buffer");
730 jbd_unlock_bh_state(bh);
731 frozen_buffer =
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400732 jbd2_alloc(jh2bh(jh)->b_size,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700733 GFP_NOFS);
734 if (!frozen_buffer) {
735 printk(KERN_EMERG
736 "%s: OOM for frozen_buffer\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400737 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700738 JBUFFER_TRACE(jh, "oom!");
739 error = -ENOMEM;
740 jbd_lock_bh_state(bh);
741 goto done;
742 }
743 goto repeat;
744 }
745 jh->b_frozen_data = frozen_buffer;
746 frozen_buffer = NULL;
747 need_copy = 1;
748 }
749 jh->b_next_transaction = transaction;
750 }
751
752
753 /*
754 * Finally, if the buffer is not journaled right now, we need to make
755 * sure it doesn't get written to disk before the caller actually
756 * commits the new data
757 */
758 if (!jh->b_transaction) {
759 JBUFFER_TRACE(jh, "no transaction");
760 J_ASSERT_JH(jh, !jh->b_next_transaction);
761 jh->b_transaction = transaction;
762 JBUFFER_TRACE(jh, "file as BJ_Reserved");
763 spin_lock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700764 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700765 spin_unlock(&journal->j_list_lock);
766 }
767
768done:
769 if (need_copy) {
770 struct page *page;
771 int offset;
772 char *source;
773
774 J_EXPECT_JH(jh, buffer_uptodate(jh2bh(jh)),
775 "Possible IO failure.\n");
776 page = jh2bh(jh)->b_page;
777 offset = ((unsigned long) jh2bh(jh)->b_data) & ~PAGE_MASK;
778 source = kmap_atomic(page, KM_USER0);
Jan Kara13ceef02010-07-14 07:56:33 +0200779 /* Fire data frozen trigger just before we copy the data */
780 jbd2_buffer_frozen_trigger(jh, source + offset,
781 jh->b_triggers);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700782 memcpy(jh->b_frozen_data, source+offset, jh2bh(jh)->b_size);
783 kunmap_atomic(source, KM_USER0);
Joel Beckere06c8222008-09-11 15:35:47 -0700784
785 /*
786 * Now that the frozen data is saved off, we need to store
787 * any matching triggers.
788 */
789 jh->b_frozen_triggers = jh->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700790 }
791 jbd_unlock_bh_state(bh);
792
793 /*
794 * If we are about to journal a buffer, then any revoke pending on it is
795 * no longer valid
796 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700797 jbd2_journal_cancel_revoke(handle, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700798
799out:
800 if (unlikely(frozen_buffer)) /* It's usually NULL */
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400801 jbd2_free(frozen_buffer, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700802
803 JBUFFER_TRACE(jh, "exit");
804 return error;
805}
806
807/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700808 * int jbd2_journal_get_write_access() - notify intent to modify a buffer for metadata (not data) update.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700809 * @handle: transaction to add buffer modifications to
810 * @bh: bh to be used for metadata writes
811 * @credits: variable that will receive credits for the buffer
812 *
813 * Returns an error code or 0 on success.
814 *
815 * In full data journalling mode the buffer may be of type BJ_AsyncData,
816 * because we're write()ing a buffer which is also part of a shared mapping.
817 */
818
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700819int jbd2_journal_get_write_access(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700820{
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700821 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700822 int rc;
823
824 /* We do not want to get caught playing with fields which the
825 * log thread also manipulates. Make sure that the buffer
826 * completes any outstanding IO before proceeding. */
827 rc = do_get_write_access(handle, jh, 0);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700828 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700829 return rc;
830}
831
832
833/*
834 * When the user wants to journal a newly created buffer_head
835 * (ie. getblk() returned a new buffer and we are going to populate it
836 * manually rather than reading off disk), then we need to keep the
837 * buffer_head locked until it has been completely filled with new
838 * data. In this case, we should be able to make the assertion that
839 * the bh is not already part of an existing transaction.
840 *
841 * The buffer should already be locked by the caller by this point.
842 * There is no lock ranking violation: it was a newly created,
843 * unlocked buffer beforehand. */
844
845/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700846 * int jbd2_journal_get_create_access () - notify intent to use newly created bh
Dave Kleikamp470decc2006-10-11 01:20:57 -0700847 * @handle: transaction to new buffer to
848 * @bh: new buffer.
849 *
850 * Call this if you create a new bh.
851 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700852int jbd2_journal_get_create_access(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700853{
854 transaction_t *transaction = handle->h_transaction;
855 journal_t *journal = transaction->t_journal;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700856 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700857 int err;
858
859 jbd_debug(5, "journal_head %p\n", jh);
860 err = -EROFS;
861 if (is_handle_aborted(handle))
862 goto out;
863 err = 0;
864
865 JBUFFER_TRACE(jh, "entry");
866 /*
867 * The buffer may already belong to this transaction due to pre-zeroing
868 * in the filesystem's new_block code. It may also be on the previous,
869 * committing transaction's lists, but it HAS to be in Forget state in
870 * that case: the transaction must have deleted the buffer for it to be
871 * reused here.
872 */
873 jbd_lock_bh_state(bh);
874 spin_lock(&journal->j_list_lock);
875 J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
876 jh->b_transaction == NULL ||
877 (jh->b_transaction == journal->j_committing_transaction &&
878 jh->b_jlist == BJ_Forget)));
879
880 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
881 J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
882
883 if (jh->b_transaction == NULL) {
Jan Karaf91d1d02009-07-13 16:16:20 -0400884 /*
885 * Previous jbd2_journal_forget() could have left the buffer
886 * with jbddirty bit set because it was being committed. When
887 * the commit finished, we've filed the buffer for
888 * checkpointing and marked it dirty. Now we are reallocating
889 * the buffer so the transaction freeing it must have
890 * committed and so it's safe to clear the dirty bit.
891 */
892 clear_buffer_dirty(jh2bh(jh));
Dave Kleikamp470decc2006-10-11 01:20:57 -0700893 jh->b_transaction = transaction;
Josef Bacik9fc7c632008-04-17 10:38:59 -0400894
895 /* first access by this transaction */
896 jh->b_modified = 0;
897
Dave Kleikamp470decc2006-10-11 01:20:57 -0700898 JBUFFER_TRACE(jh, "file as BJ_Reserved");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700899 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700900 } else if (jh->b_transaction == journal->j_committing_transaction) {
Josef Bacik9fc7c632008-04-17 10:38:59 -0400901 /* first access by this transaction */
902 jh->b_modified = 0;
903
Dave Kleikamp470decc2006-10-11 01:20:57 -0700904 JBUFFER_TRACE(jh, "set next transaction");
905 jh->b_next_transaction = transaction;
906 }
907 spin_unlock(&journal->j_list_lock);
908 jbd_unlock_bh_state(bh);
909
910 /*
911 * akpm: I added this. ext3_alloc_branch can pick up new indirect
912 * blocks which contain freed but then revoked metadata. We need
913 * to cancel the revoke in case we end up freeing it yet again
914 * and the reallocating as data - this would cause a second revoke,
915 * which hits an assertion error.
916 */
917 JBUFFER_TRACE(jh, "cancelling revoke");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700918 jbd2_journal_cancel_revoke(handle, jh);
919 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700920out:
921 return err;
922}
923
924/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700925 * int jbd2_journal_get_undo_access() - Notify intent to modify metadata with
Dave Kleikamp470decc2006-10-11 01:20:57 -0700926 * non-rewindable consequences
927 * @handle: transaction
928 * @bh: buffer to undo
929 * @credits: store the number of taken credits here (if not NULL)
930 *
931 * Sometimes there is a need to distinguish between metadata which has
932 * been committed to disk and that which has not. The ext3fs code uses
933 * this for freeing and allocating space, we have to make sure that we
934 * do not reuse freed space until the deallocation has been committed,
935 * since if we overwrote that space we would make the delete
936 * un-rewindable in case of a crash.
937 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700938 * To deal with that, jbd2_journal_get_undo_access requests write access to a
Dave Kleikamp470decc2006-10-11 01:20:57 -0700939 * buffer for parts of non-rewindable operations such as delete
940 * operations on the bitmaps. The journaling code must keep a copy of
941 * the buffer's contents prior to the undo_access call until such time
942 * as we know that the buffer has definitely been committed to disk.
943 *
944 * We never need to know which transaction the committed data is part
945 * of, buffers touched here are guaranteed to be dirtied later and so
946 * will be committed to a new transaction in due course, at which point
947 * we can discard the old committed data pointer.
948 *
949 * Returns error number or 0 on success.
950 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700951int jbd2_journal_get_undo_access(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700952{
953 int err;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700954 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700955 char *committed_data = NULL;
956
957 JBUFFER_TRACE(jh, "entry");
958
959 /*
960 * Do this first --- it can drop the journal lock, so we want to
961 * make sure that obtaining the committed_data is done
962 * atomically wrt. completion of any outstanding commits.
963 */
964 err = do_get_write_access(handle, jh, 1);
965 if (err)
966 goto out;
967
968repeat:
969 if (!jh->b_committed_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400970 committed_data = jbd2_alloc(jh2bh(jh)->b_size, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700971 if (!committed_data) {
972 printk(KERN_EMERG "%s: No memory for committed data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400973 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700974 err = -ENOMEM;
975 goto out;
976 }
977 }
978
979 jbd_lock_bh_state(bh);
980 if (!jh->b_committed_data) {
981 /* Copy out the current buffer contents into the
982 * preserved, committed copy. */
983 JBUFFER_TRACE(jh, "generate b_committed data");
984 if (!committed_data) {
985 jbd_unlock_bh_state(bh);
986 goto repeat;
987 }
988
989 jh->b_committed_data = committed_data;
990 committed_data = NULL;
991 memcpy(jh->b_committed_data, bh->b_data, bh->b_size);
992 }
993 jbd_unlock_bh_state(bh);
994out:
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700995 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700996 if (unlikely(committed_data))
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400997 jbd2_free(committed_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700998 return err;
999}
1000
1001/**
Joel Beckere06c8222008-09-11 15:35:47 -07001002 * void jbd2_journal_set_triggers() - Add triggers for commit writeout
1003 * @bh: buffer to trigger on
1004 * @type: struct jbd2_buffer_trigger_type containing the trigger(s).
1005 *
1006 * Set any triggers on this journal_head. This is always safe, because
1007 * triggers for a committing buffer will be saved off, and triggers for
1008 * a running transaction will match the buffer in that transaction.
1009 *
1010 * Call with NULL to clear the triggers.
1011 */
1012void jbd2_journal_set_triggers(struct buffer_head *bh,
1013 struct jbd2_buffer_trigger_type *type)
1014{
1015 struct journal_head *jh = bh2jh(bh);
1016
1017 jh->b_triggers = type;
1018}
1019
Jan Kara13ceef02010-07-14 07:56:33 +02001020void jbd2_buffer_frozen_trigger(struct journal_head *jh, void *mapped_data,
Joel Beckere06c8222008-09-11 15:35:47 -07001021 struct jbd2_buffer_trigger_type *triggers)
1022{
1023 struct buffer_head *bh = jh2bh(jh);
1024
Jan Kara13ceef02010-07-14 07:56:33 +02001025 if (!triggers || !triggers->t_frozen)
Joel Beckere06c8222008-09-11 15:35:47 -07001026 return;
1027
Jan Kara13ceef02010-07-14 07:56:33 +02001028 triggers->t_frozen(triggers, bh, mapped_data, bh->b_size);
Joel Beckere06c8222008-09-11 15:35:47 -07001029}
1030
1031void jbd2_buffer_abort_trigger(struct journal_head *jh,
1032 struct jbd2_buffer_trigger_type *triggers)
1033{
1034 if (!triggers || !triggers->t_abort)
1035 return;
1036
1037 triggers->t_abort(triggers, jh2bh(jh));
1038}
1039
1040
1041
1042/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001043 * int jbd2_journal_dirty_metadata() - mark a buffer as containing dirty metadata
Dave Kleikamp470decc2006-10-11 01:20:57 -07001044 * @handle: transaction to add buffer to.
1045 * @bh: buffer to mark
1046 *
1047 * mark dirty metadata which needs to be journaled as part of the current
1048 * transaction.
1049 *
1050 * The buffer is placed on the transaction's metadata list and is marked
1051 * as belonging to the transaction.
1052 *
1053 * Returns error number or 0 on success.
1054 *
1055 * Special care needs to be taken if the buffer already belongs to the
1056 * current committing transaction (in which case we should have frozen
1057 * data present for that commit). In that case, we don't relink the
1058 * buffer: that only gets done when the old transaction finally
1059 * completes its commit.
1060 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001061int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001062{
1063 transaction_t *transaction = handle->h_transaction;
1064 journal_t *journal = transaction->t_journal;
1065 struct journal_head *jh = bh2jh(bh);
1066
1067 jbd_debug(5, "journal_head %p\n", jh);
1068 JBUFFER_TRACE(jh, "entry");
1069 if (is_handle_aborted(handle))
1070 goto out;
1071
1072 jbd_lock_bh_state(bh);
1073
1074 if (jh->b_modified == 0) {
1075 /*
1076 * This buffer's got modified and becoming part
1077 * of the transaction. This needs to be done
1078 * once a transaction -bzzz
1079 */
1080 jh->b_modified = 1;
1081 J_ASSERT_JH(jh, handle->h_buffer_credits > 0);
1082 handle->h_buffer_credits--;
1083 }
1084
1085 /*
1086 * fastpath, to avoid expensive locking. If this buffer is already
1087 * on the running transaction's metadata list there is nothing to do.
1088 * Nobody can take it off again because there is a handle open.
1089 * I _think_ we're OK here with SMP barriers - a mistaken decision will
1090 * result in this test being false, so we go in and take the locks.
1091 */
1092 if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) {
1093 JBUFFER_TRACE(jh, "fastpath");
1094 J_ASSERT_JH(jh, jh->b_transaction ==
1095 journal->j_running_transaction);
1096 goto out_unlock_bh;
1097 }
1098
1099 set_buffer_jbddirty(bh);
1100
1101 /*
1102 * Metadata already on the current transaction list doesn't
1103 * need to be filed. Metadata on another transaction's list must
1104 * be committing, and will be refiled once the commit completes:
1105 * leave it alone for now.
1106 */
1107 if (jh->b_transaction != transaction) {
1108 JBUFFER_TRACE(jh, "already on other transaction");
1109 J_ASSERT_JH(jh, jh->b_transaction ==
1110 journal->j_committing_transaction);
1111 J_ASSERT_JH(jh, jh->b_next_transaction == transaction);
1112 /* And this case is illegal: we can't reuse another
1113 * transaction's data buffer, ever. */
1114 goto out_unlock_bh;
1115 }
1116
1117 /* That test should have eliminated the following case: */
Mingming Cao40191912008-01-28 23:58:27 -05001118 J_ASSERT_JH(jh, jh->b_frozen_data == NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001119
1120 JBUFFER_TRACE(jh, "file as BJ_Metadata");
1121 spin_lock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001122 __jbd2_journal_file_buffer(jh, handle->h_transaction, BJ_Metadata);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001123 spin_unlock(&journal->j_list_lock);
1124out_unlock_bh:
1125 jbd_unlock_bh_state(bh);
1126out:
1127 JBUFFER_TRACE(jh, "exit");
1128 return 0;
1129}
1130
1131/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001132 * jbd2_journal_release_buffer: undo a get_write_access without any buffer
Dave Kleikamp470decc2006-10-11 01:20:57 -07001133 * updates, if the update decided in the end that it didn't need access.
1134 *
1135 */
1136void
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001137jbd2_journal_release_buffer(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001138{
1139 BUFFER_TRACE(bh, "entry");
1140}
1141
1142/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001143 * void jbd2_journal_forget() - bforget() for potentially-journaled buffers.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001144 * @handle: transaction handle
1145 * @bh: bh to 'forget'
1146 *
1147 * We can only do the bforget if there are no commits pending against the
1148 * buffer. If the buffer is dirty in the current running transaction we
1149 * can safely unlink it.
1150 *
1151 * bh may not be a journalled buffer at all - it may be a non-JBD
1152 * buffer which came off the hashtable. Check for this.
1153 *
1154 * Decrements bh->b_count by one.
1155 *
1156 * Allow this call even if the handle has aborted --- it may be part of
1157 * the caller's cleanup after an abort.
1158 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001159int jbd2_journal_forget (handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001160{
1161 transaction_t *transaction = handle->h_transaction;
1162 journal_t *journal = transaction->t_journal;
1163 struct journal_head *jh;
1164 int drop_reserve = 0;
1165 int err = 0;
Josef Bacik1dfc3222008-04-17 10:38:59 -04001166 int was_modified = 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001167
1168 BUFFER_TRACE(bh, "entry");
1169
1170 jbd_lock_bh_state(bh);
1171 spin_lock(&journal->j_list_lock);
1172
1173 if (!buffer_jbd(bh))
1174 goto not_jbd;
1175 jh = bh2jh(bh);
1176
1177 /* Critical error: attempting to delete a bitmap buffer, maybe?
1178 * Don't do any jbd operations, and return an error. */
1179 if (!J_EXPECT_JH(jh, !jh->b_committed_data,
1180 "inconsistent data on disk")) {
1181 err = -EIO;
1182 goto not_jbd;
1183 }
1184
Josef Bacik1dfc3222008-04-17 10:38:59 -04001185 /* keep track of wether or not this transaction modified us */
1186 was_modified = jh->b_modified;
1187
Dave Kleikamp470decc2006-10-11 01:20:57 -07001188 /*
1189 * The buffer's going from the transaction, we must drop
1190 * all references -bzzz
1191 */
1192 jh->b_modified = 0;
1193
1194 if (jh->b_transaction == handle->h_transaction) {
1195 J_ASSERT_JH(jh, !jh->b_frozen_data);
1196
1197 /* If we are forgetting a buffer which is already part
1198 * of this transaction, then we can just drop it from
1199 * the transaction immediately. */
1200 clear_buffer_dirty(bh);
1201 clear_buffer_jbddirty(bh);
1202
1203 JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
1204
Josef Bacik1dfc3222008-04-17 10:38:59 -04001205 /*
1206 * we only want to drop a reference if this transaction
1207 * modified the buffer
1208 */
1209 if (was_modified)
1210 drop_reserve = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001211
1212 /*
1213 * We are no longer going to journal this buffer.
1214 * However, the commit of this transaction is still
1215 * important to the buffer: the delete that we are now
1216 * processing might obsolete an old log entry, so by
1217 * committing, we can satisfy the buffer's checkpoint.
1218 *
1219 * So, if we have a checkpoint on the buffer, we should
1220 * now refile the buffer on our BJ_Forget list so that
1221 * we know to remove the checkpoint after we commit.
1222 */
1223
1224 if (jh->b_cp_transaction) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001225 __jbd2_journal_temp_unlink_buffer(jh);
1226 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001227 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001228 __jbd2_journal_unfile_buffer(jh);
1229 jbd2_journal_remove_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001230 __brelse(bh);
1231 if (!buffer_jbd(bh)) {
1232 spin_unlock(&journal->j_list_lock);
1233 jbd_unlock_bh_state(bh);
1234 __bforget(bh);
1235 goto drop;
1236 }
1237 }
1238 } else if (jh->b_transaction) {
1239 J_ASSERT_JH(jh, (jh->b_transaction ==
1240 journal->j_committing_transaction));
1241 /* However, if the buffer is still owned by a prior
1242 * (committing) transaction, we can't drop it yet... */
1243 JBUFFER_TRACE(jh, "belongs to older transaction");
1244 /* ... but we CAN drop it from the new transaction if we
1245 * have also modified it since the original commit. */
1246
1247 if (jh->b_next_transaction) {
1248 J_ASSERT(jh->b_next_transaction == transaction);
1249 jh->b_next_transaction = NULL;
Josef Bacik1dfc3222008-04-17 10:38:59 -04001250
1251 /*
1252 * only drop a reference if this transaction modified
1253 * the buffer
1254 */
1255 if (was_modified)
1256 drop_reserve = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001257 }
1258 }
1259
1260not_jbd:
1261 spin_unlock(&journal->j_list_lock);
1262 jbd_unlock_bh_state(bh);
1263 __brelse(bh);
1264drop:
1265 if (drop_reserve) {
1266 /* no need to reserve log space for this block -bzzz */
1267 handle->h_buffer_credits++;
1268 }
1269 return err;
1270}
1271
1272/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001273 * int jbd2_journal_stop() - complete a transaction
Dave Kleikamp470decc2006-10-11 01:20:57 -07001274 * @handle: tranaction to complete.
1275 *
1276 * All done for a particular handle.
1277 *
1278 * There is not much action needed here. We just return any remaining
1279 * buffer credits to the transaction and remove the handle. The only
1280 * complication is that we need to start a commit operation if the
1281 * filesystem is marked for synchronous update.
1282 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001283 * jbd2_journal_stop itself will not usually return an error, but it may
Dave Kleikamp470decc2006-10-11 01:20:57 -07001284 * do so in unusual circumstances. In particular, expect it to
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001285 * return -EIO if a jbd2_journal_abort has been executed since the
Dave Kleikamp470decc2006-10-11 01:20:57 -07001286 * transaction began.
1287 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001288int jbd2_journal_stop(handle_t *handle)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001289{
1290 transaction_t *transaction = handle->h_transaction;
1291 journal_t *journal = transaction->t_journal;
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001292 int err, wait_for_commit = 0;
1293 tid_t tid;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001294 pid_t pid;
1295
Dave Kleikamp470decc2006-10-11 01:20:57 -07001296 J_ASSERT(journal_current_handle() == handle);
1297
1298 if (is_handle_aborted(handle))
1299 err = -EIO;
OGAWA Hirofumi3e2a5322006-10-19 23:29:11 -07001300 else {
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001301 J_ASSERT(atomic_read(&transaction->t_updates) > 0);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001302 err = 0;
OGAWA Hirofumi3e2a5322006-10-19 23:29:11 -07001303 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001304
1305 if (--handle->h_ref > 0) {
1306 jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
1307 handle->h_ref);
1308 return err;
1309 }
1310
1311 jbd_debug(4, "Handle %p going down\n", handle);
1312
1313 /*
1314 * Implement synchronous transaction batching. If the handle
1315 * was synchronous, don't force a commit immediately. Let's
Josef Bacike07f7182008-11-26 01:14:26 -05001316 * yield and let another thread piggyback onto this
1317 * transaction. Keep doing that while new threads continue to
1318 * arrive. It doesn't cost much - we're about to run a commit
1319 * and sleep on IO anyway. Speeds up many-threaded, many-dir
1320 * operations by 30x or more...
Dave Kleikamp470decc2006-10-11 01:20:57 -07001321 *
Josef Bacike07f7182008-11-26 01:14:26 -05001322 * We try and optimize the sleep time against what the
1323 * underlying disk can do, instead of having a static sleep
1324 * time. This is useful for the case where our storage is so
1325 * fast that it is more optimal to go ahead and force a flush
1326 * and wait for the transaction to be committed than it is to
1327 * wait for an arbitrary amount of time for new writers to
1328 * join the transaction. We achieve this by measuring how
1329 * long it takes to commit a transaction, and compare it with
1330 * how long this transaction has been running, and if run time
1331 * < commit time then we sleep for the delta and commit. This
1332 * greatly helps super fast disks that would see slowdowns as
1333 * more threads started doing fsyncs.
1334 *
1335 * But don't do this if this process was the most recent one
1336 * to perform a synchronous write. We do this to detect the
1337 * case where a single process is doing a stream of sync
1338 * writes. No point in waiting for joiners in that case.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001339 */
1340 pid = current->pid;
1341 if (handle->h_sync && journal->j_last_sync_writer != pid) {
Josef Bacike07f7182008-11-26 01:14:26 -05001342 u64 commit_time, trans_time;
1343
Dave Kleikamp470decc2006-10-11 01:20:57 -07001344 journal->j_last_sync_writer = pid;
Josef Bacike07f7182008-11-26 01:14:26 -05001345
Theodore Ts'oa931da62010-08-03 21:35:12 -04001346 read_lock(&journal->j_state_lock);
Josef Bacike07f7182008-11-26 01:14:26 -05001347 commit_time = journal->j_average_commit_time;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001348 read_unlock(&journal->j_state_lock);
Josef Bacike07f7182008-11-26 01:14:26 -05001349
1350 trans_time = ktime_to_ns(ktime_sub(ktime_get(),
1351 transaction->t_start_time));
1352
Theodore Ts'o30773842009-01-03 20:27:38 -05001353 commit_time = max_t(u64, commit_time,
1354 1000*journal->j_min_batch_time);
Josef Bacike07f7182008-11-26 01:14:26 -05001355 commit_time = min_t(u64, commit_time,
Theodore Ts'o30773842009-01-03 20:27:38 -05001356 1000*journal->j_max_batch_time);
Josef Bacike07f7182008-11-26 01:14:26 -05001357
1358 if (trans_time < commit_time) {
1359 ktime_t expires = ktime_add_ns(ktime_get(),
1360 commit_time);
1361 set_current_state(TASK_UNINTERRUPTIBLE);
1362 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1363 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001364 }
1365
Theodore Ts'o70585482009-03-25 23:35:46 -04001366 if (handle->h_sync)
1367 transaction->t_synchronous_commit = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001368 current->journal_info = NULL;
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001369 atomic_sub(handle->h_buffer_credits,
1370 &transaction->t_outstanding_credits);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001371
1372 /*
1373 * If the handle is marked SYNC, we need to set another commit
1374 * going! We also want to force a commit if the current
1375 * transaction is occupying too much of the log, or if the
1376 * transaction is too old now.
1377 */
1378 if (handle->h_sync ||
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001379 (atomic_read(&transaction->t_outstanding_credits) >
1380 journal->j_max_transaction_buffers) ||
1381 time_after_eq(jiffies, transaction->t_expires)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001382 /* Do this even for aborted journals: an abort still
1383 * completes the commit thread, it just doesn't write
1384 * anything to disk. */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001385
Dave Kleikamp470decc2006-10-11 01:20:57 -07001386 jbd_debug(2, "transaction too old, requesting commit for "
1387 "handle %p\n", handle);
1388 /* This is non-blocking */
Theodore Ts'oc35a56a2010-05-16 05:00:00 -04001389 jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001390
1391 /*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001392 * Special case: JBD2_SYNC synchronous updates require us
Dave Kleikamp470decc2006-10-11 01:20:57 -07001393 * to wait for the commit to complete.
1394 */
1395 if (handle->h_sync && !(current->flags & PF_MEMALLOC))
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001396 wait_for_commit = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001397 }
1398
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001399 /*
1400 * Once we drop t_updates, if it goes to zero the transaction
1401 * could start commiting on us and eventually disappear. So
1402 * once we do this, we must not dereference transaction
1403 * pointer again.
1404 */
1405 tid = transaction->t_tid;
1406 if (atomic_dec_and_test(&transaction->t_updates)) {
1407 wake_up(&journal->j_wait_updates);
1408 if (journal->j_barrier_count)
1409 wake_up(&journal->j_wait_transaction_locked);
1410 }
1411
1412 if (wait_for_commit)
1413 err = jbd2_log_wait_commit(journal, tid);
1414
Ingo Molnar3295f0e2008-08-11 10:30:30 +02001415 lock_map_release(&handle->h_lockdep_map);
Mingming Cao7b751062008-01-28 23:58:27 -05001416
Mingming Caoaf1e76d2007-10-16 18:38:25 -04001417 jbd2_free_handle(handle);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001418 return err;
1419}
1420
Randy Dunlap5648ba52008-04-17 10:38:59 -04001421/**
1422 * int jbd2_journal_force_commit() - force any uncommitted transactions
Dave Kleikamp470decc2006-10-11 01:20:57 -07001423 * @journal: journal to force
1424 *
1425 * For synchronous operations: force any uncommitted transactions
1426 * to disk. May seem kludgy, but it reuses all the handle batching
1427 * code in a very simple manner.
1428 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001429int jbd2_journal_force_commit(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001430{
1431 handle_t *handle;
1432 int ret;
1433
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001434 handle = jbd2_journal_start(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001435 if (IS_ERR(handle)) {
1436 ret = PTR_ERR(handle);
1437 } else {
1438 handle->h_sync = 1;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001439 ret = jbd2_journal_stop(handle);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001440 }
1441 return ret;
1442}
1443
1444/*
1445 *
1446 * List management code snippets: various functions for manipulating the
1447 * transaction buffer lists.
1448 *
1449 */
1450
1451/*
1452 * Append a buffer to a transaction list, given the transaction's list head
1453 * pointer.
1454 *
1455 * j_list_lock is held.
1456 *
1457 * jbd_lock_bh_state(jh2bh(jh)) is held.
1458 */
1459
1460static inline void
1461__blist_add_buffer(struct journal_head **list, struct journal_head *jh)
1462{
1463 if (!*list) {
1464 jh->b_tnext = jh->b_tprev = jh;
1465 *list = jh;
1466 } else {
1467 /* Insert at the tail of the list to preserve order */
1468 struct journal_head *first = *list, *last = first->b_tprev;
1469 jh->b_tprev = last;
1470 jh->b_tnext = first;
1471 last->b_tnext = first->b_tprev = jh;
1472 }
1473}
1474
1475/*
1476 * Remove a buffer from a transaction list, given the transaction's list
1477 * head pointer.
1478 *
1479 * Called with j_list_lock held, and the journal may not be locked.
1480 *
1481 * jbd_lock_bh_state(jh2bh(jh)) is held.
1482 */
1483
1484static inline void
1485__blist_del_buffer(struct journal_head **list, struct journal_head *jh)
1486{
1487 if (*list == jh) {
1488 *list = jh->b_tnext;
1489 if (*list == jh)
1490 *list = NULL;
1491 }
1492 jh->b_tprev->b_tnext = jh->b_tnext;
1493 jh->b_tnext->b_tprev = jh->b_tprev;
1494}
1495
1496/*
1497 * Remove a buffer from the appropriate transaction list.
1498 *
1499 * Note that this function can *change* the value of
Jan Kara87c89c22008-07-11 19:27:31 -04001500 * bh->b_transaction->t_buffers, t_forget, t_iobuf_list, t_shadow_list,
1501 * t_log_list or t_reserved_list. If the caller is holding onto a copy of one
1502 * of these pointers, it could go bad. Generally the caller needs to re-read
1503 * the pointer from the transaction_t.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001504 *
1505 * Called under j_list_lock. The journal may not be locked.
1506 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001507void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001508{
1509 struct journal_head **list = NULL;
1510 transaction_t *transaction;
1511 struct buffer_head *bh = jh2bh(jh);
1512
1513 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1514 transaction = jh->b_transaction;
1515 if (transaction)
1516 assert_spin_locked(&transaction->t_journal->j_list_lock);
1517
1518 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1519 if (jh->b_jlist != BJ_None)
Mingming Cao40191912008-01-28 23:58:27 -05001520 J_ASSERT_JH(jh, transaction != NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001521
1522 switch (jh->b_jlist) {
1523 case BJ_None:
1524 return;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001525 case BJ_Metadata:
1526 transaction->t_nr_buffers--;
1527 J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
1528 list = &transaction->t_buffers;
1529 break;
1530 case BJ_Forget:
1531 list = &transaction->t_forget;
1532 break;
1533 case BJ_IO:
1534 list = &transaction->t_iobuf_list;
1535 break;
1536 case BJ_Shadow:
1537 list = &transaction->t_shadow_list;
1538 break;
1539 case BJ_LogCtl:
1540 list = &transaction->t_log_list;
1541 break;
1542 case BJ_Reserved:
1543 list = &transaction->t_reserved_list;
1544 break;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001545 }
1546
1547 __blist_del_buffer(list, jh);
1548 jh->b_jlist = BJ_None;
1549 if (test_clear_buffer_jbddirty(bh))
1550 mark_buffer_dirty(bh); /* Expose it to the VM */
1551}
1552
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001553void __jbd2_journal_unfile_buffer(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001554{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001555 __jbd2_journal_temp_unlink_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001556 jh->b_transaction = NULL;
1557}
1558
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001559void jbd2_journal_unfile_buffer(journal_t *journal, struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001560{
1561 jbd_lock_bh_state(jh2bh(jh));
1562 spin_lock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001563 __jbd2_journal_unfile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001564 spin_unlock(&journal->j_list_lock);
1565 jbd_unlock_bh_state(jh2bh(jh));
1566}
1567
1568/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001569 * Called from jbd2_journal_try_to_free_buffers().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001570 *
1571 * Called under jbd_lock_bh_state(bh)
1572 */
1573static void
1574__journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh)
1575{
1576 struct journal_head *jh;
1577
1578 jh = bh2jh(bh);
1579
1580 if (buffer_locked(bh) || buffer_dirty(bh))
1581 goto out;
1582
Mingming Cao40191912008-01-28 23:58:27 -05001583 if (jh->b_next_transaction != NULL)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001584 goto out;
1585
1586 spin_lock(&journal->j_list_lock);
Jan Kara87c89c22008-07-11 19:27:31 -04001587 if (jh->b_cp_transaction != NULL && jh->b_transaction == NULL) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001588 /* written-back checkpointed metadata buffer */
1589 if (jh->b_jlist == BJ_None) {
1590 JBUFFER_TRACE(jh, "remove from checkpoint list");
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001591 __jbd2_journal_remove_checkpoint(jh);
1592 jbd2_journal_remove_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001593 __brelse(bh);
1594 }
1595 }
1596 spin_unlock(&journal->j_list_lock);
1597out:
1598 return;
1599}
1600
Dave Kleikamp470decc2006-10-11 01:20:57 -07001601/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001602 * int jbd2_journal_try_to_free_buffers() - try to free page buffers.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001603 * @journal: journal for operation
1604 * @page: to try and free
Mingming Cao530576b2008-07-13 21:06:39 -04001605 * @gfp_mask: we use the mask to detect how hard should we try to release
1606 * buffers. If __GFP_WAIT and __GFP_FS is set, we wait for commit code to
1607 * release the buffers.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001608 *
1609 *
1610 * For all the buffers on this page,
1611 * if they are fully written out ordered data, move them onto BUF_CLEAN
1612 * so try_to_free_buffers() can reap them.
1613 *
1614 * This function returns non-zero if we wish try_to_free_buffers()
1615 * to be called. We do this if the page is releasable by try_to_free_buffers().
1616 * We also do it if the page has locked or dirty buffers and the caller wants
1617 * us to perform sync or async writeout.
1618 *
1619 * This complicates JBD locking somewhat. We aren't protected by the
1620 * BKL here. We wish to remove the buffer from its committing or
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001621 * running transaction's ->t_datalist via __jbd2_journal_unfile_buffer.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001622 *
1623 * This may *change* the value of transaction_t->t_datalist, so anyone
1624 * who looks at t_datalist needs to lock against this function.
1625 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001626 * Even worse, someone may be doing a jbd2_journal_dirty_data on this
1627 * buffer. So we need to lock against that. jbd2_journal_dirty_data()
Dave Kleikamp470decc2006-10-11 01:20:57 -07001628 * will come out of the lock with the buffer dirty, which makes it
1629 * ineligible for release here.
1630 *
1631 * Who else is affected by this? hmm... Really the only contender
1632 * is do_get_write_access() - it could be looking at the buffer while
1633 * journal_try_to_free_buffer() is changing its state. But that
1634 * cannot happen because we never reallocate freed data as metadata
1635 * while the data is part of a transaction. Yes?
Mingming Cao530576b2008-07-13 21:06:39 -04001636 *
1637 * Return 0 on failure, 1 on success
Dave Kleikamp470decc2006-10-11 01:20:57 -07001638 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001639int jbd2_journal_try_to_free_buffers(journal_t *journal,
Mingming Cao530576b2008-07-13 21:06:39 -04001640 struct page *page, gfp_t gfp_mask)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001641{
1642 struct buffer_head *head;
1643 struct buffer_head *bh;
1644 int ret = 0;
1645
1646 J_ASSERT(PageLocked(page));
1647
1648 head = page_buffers(page);
1649 bh = head;
1650 do {
1651 struct journal_head *jh;
1652
1653 /*
1654 * We take our own ref against the journal_head here to avoid
1655 * having to add tons of locking around each instance of
Mingming Cao530576b2008-07-13 21:06:39 -04001656 * jbd2_journal_remove_journal_head() and
1657 * jbd2_journal_put_journal_head().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001658 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001659 jh = jbd2_journal_grab_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001660 if (!jh)
1661 continue;
1662
1663 jbd_lock_bh_state(bh);
1664 __journal_try_to_free_buffer(journal, bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001665 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001666 jbd_unlock_bh_state(bh);
1667 if (buffer_jbd(bh))
1668 goto busy;
1669 } while ((bh = bh->b_this_page) != head);
Mingming Cao530576b2008-07-13 21:06:39 -04001670
Dave Kleikamp470decc2006-10-11 01:20:57 -07001671 ret = try_to_free_buffers(page);
Mingming Cao530576b2008-07-13 21:06:39 -04001672
Dave Kleikamp470decc2006-10-11 01:20:57 -07001673busy:
1674 return ret;
1675}
1676
1677/*
1678 * This buffer is no longer needed. If it is on an older transaction's
1679 * checkpoint list we need to record it on this transaction's forget list
1680 * to pin this buffer (and hence its checkpointing transaction) down until
1681 * this transaction commits. If the buffer isn't on a checkpoint list, we
1682 * release it.
1683 * Returns non-zero if JBD no longer has an interest in the buffer.
1684 *
1685 * Called under j_list_lock.
1686 *
1687 * Called under jbd_lock_bh_state(bh).
1688 */
1689static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
1690{
1691 int may_free = 1;
1692 struct buffer_head *bh = jh2bh(jh);
1693
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001694 __jbd2_journal_unfile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001695
1696 if (jh->b_cp_transaction) {
1697 JBUFFER_TRACE(jh, "on running+cp transaction");
Jan Karaf91d1d02009-07-13 16:16:20 -04001698 /*
1699 * We don't want to write the buffer anymore, clear the
1700 * bit so that we don't confuse checks in
1701 * __journal_file_buffer
1702 */
1703 clear_buffer_dirty(bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001704 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001705 may_free = 0;
1706 } else {
1707 JBUFFER_TRACE(jh, "on running transaction");
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001708 jbd2_journal_remove_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001709 __brelse(bh);
1710 }
1711 return may_free;
1712}
1713
1714/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001715 * jbd2_journal_invalidatepage
Dave Kleikamp470decc2006-10-11 01:20:57 -07001716 *
1717 * This code is tricky. It has a number of cases to deal with.
1718 *
1719 * There are two invariants which this code relies on:
1720 *
1721 * i_size must be updated on disk before we start calling invalidatepage on the
1722 * data.
1723 *
1724 * This is done in ext3 by defining an ext3_setattr method which
1725 * updates i_size before truncate gets going. By maintaining this
1726 * invariant, we can be sure that it is safe to throw away any buffers
1727 * attached to the current transaction: once the transaction commits,
1728 * we know that the data will not be needed.
1729 *
1730 * Note however that we can *not* throw away data belonging to the
1731 * previous, committing transaction!
1732 *
1733 * Any disk blocks which *are* part of the previous, committing
1734 * transaction (and which therefore cannot be discarded immediately) are
1735 * not going to be reused in the new running transaction
1736 *
1737 * The bitmap committed_data images guarantee this: any block which is
1738 * allocated in one transaction and removed in the next will be marked
1739 * as in-use in the committed_data bitmap, so cannot be reused until
1740 * the next transaction to delete the block commits. This means that
1741 * leaving committing buffers dirty is quite safe: the disk blocks
1742 * cannot be reallocated to a different file and so buffer aliasing is
1743 * not possible.
1744 *
1745 *
1746 * The above applies mainly to ordered data mode. In writeback mode we
1747 * don't make guarantees about the order in which data hits disk --- in
1748 * particular we don't guarantee that new dirty data is flushed before
1749 * transaction commit --- so it is always safe just to discard data
1750 * immediately in that mode. --sct
1751 */
1752
1753/*
1754 * The journal_unmap_buffer helper function returns zero if the buffer
1755 * concerned remains pinned as an anonymous buffer belonging to an older
1756 * transaction.
1757 *
1758 * We're outside-transaction here. Either or both of j_running_transaction
1759 * and j_committing_transaction may be NULL.
1760 */
1761static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh)
1762{
1763 transaction_t *transaction;
1764 struct journal_head *jh;
1765 int may_free = 1;
1766 int ret;
1767
1768 BUFFER_TRACE(bh, "entry");
1769
1770 /*
1771 * It is safe to proceed here without the j_list_lock because the
1772 * buffers cannot be stolen by try_to_free_buffers as long as we are
1773 * holding the page lock. --sct
1774 */
1775
1776 if (!buffer_jbd(bh))
1777 goto zap_buffer_unlocked;
1778
Jan Kara87c89c22008-07-11 19:27:31 -04001779 /* OK, we have data buffer in journaled mode */
Theodore Ts'oa931da62010-08-03 21:35:12 -04001780 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001781 jbd_lock_bh_state(bh);
1782 spin_lock(&journal->j_list_lock);
1783
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001784 jh = jbd2_journal_grab_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001785 if (!jh)
1786 goto zap_buffer_no_jh;
1787
dingdinghuaba869022010-02-15 16:35:42 -05001788 /*
1789 * We cannot remove the buffer from checkpoint lists until the
1790 * transaction adding inode to orphan list (let's call it T)
1791 * is committed. Otherwise if the transaction changing the
1792 * buffer would be cleaned from the journal before T is
1793 * committed, a crash will cause that the correct contents of
1794 * the buffer will be lost. On the other hand we have to
1795 * clear the buffer dirty bit at latest at the moment when the
1796 * transaction marking the buffer as freed in the filesystem
1797 * structures is committed because from that moment on the
1798 * buffer can be reallocated and used by a different page.
1799 * Since the block hasn't been freed yet but the inode has
1800 * already been added to orphan list, it is safe for us to add
1801 * the buffer to BJ_Forget list of the newest transaction.
1802 */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001803 transaction = jh->b_transaction;
1804 if (transaction == NULL) {
1805 /* First case: not on any transaction. If it
1806 * has no checkpoint link, then we can zap it:
1807 * it's a writeback-mode buffer so we don't care
1808 * if it hits disk safely. */
1809 if (!jh->b_cp_transaction) {
1810 JBUFFER_TRACE(jh, "not on any transaction: zap");
1811 goto zap_buffer;
1812 }
1813
1814 if (!buffer_dirty(bh)) {
1815 /* bdflush has written it. We can drop it now */
1816 goto zap_buffer;
1817 }
1818
1819 /* OK, it must be in the journal but still not
1820 * written fully to disk: it's metadata or
1821 * journaled data... */
1822
1823 if (journal->j_running_transaction) {
1824 /* ... and once the current transaction has
1825 * committed, the buffer won't be needed any
1826 * longer. */
1827 JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
1828 ret = __dispose_buffer(jh,
1829 journal->j_running_transaction);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001830 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001831 spin_unlock(&journal->j_list_lock);
1832 jbd_unlock_bh_state(bh);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001833 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001834 return ret;
1835 } else {
1836 /* There is no currently-running transaction. So the
1837 * orphan record which we wrote for this file must have
1838 * passed into commit. We must attach this buffer to
1839 * the committing transaction, if it exists. */
1840 if (journal->j_committing_transaction) {
1841 JBUFFER_TRACE(jh, "give to committing trans");
1842 ret = __dispose_buffer(jh,
1843 journal->j_committing_transaction);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001844 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001845 spin_unlock(&journal->j_list_lock);
1846 jbd_unlock_bh_state(bh);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001847 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001848 return ret;
1849 } else {
1850 /* The orphan record's transaction has
1851 * committed. We can cleanse this buffer */
1852 clear_buffer_jbddirty(bh);
1853 goto zap_buffer;
1854 }
1855 }
1856 } else if (transaction == journal->j_committing_transaction) {
Eric Sandeen9b579882006-10-28 10:38:28 -07001857 JBUFFER_TRACE(jh, "on committing transaction");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001858 /*
dingdinghuaba869022010-02-15 16:35:42 -05001859 * The buffer is committing, we simply cannot touch
1860 * it. So we just set j_next_transaction to the
1861 * running transaction (if there is one) and mark
1862 * buffer as freed so that commit code knows it should
1863 * clear dirty bits when it is done with the buffer.
1864 */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001865 set_buffer_freed(bh);
dingdinghuaba869022010-02-15 16:35:42 -05001866 if (journal->j_running_transaction && buffer_jbddirty(bh))
1867 jh->b_next_transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001868 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001869 spin_unlock(&journal->j_list_lock);
1870 jbd_unlock_bh_state(bh);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001871 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001872 return 0;
1873 } else {
1874 /* Good, the buffer belongs to the running transaction.
1875 * We are writing our own transaction's data, not any
1876 * previous one's, so it is safe to throw it away
1877 * (remember that we expect the filesystem to have set
1878 * i_size already for this truncate so recovery will not
1879 * expose the disk blocks we are discarding here.) */
1880 J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
Eric Sandeen9b579882006-10-28 10:38:28 -07001881 JBUFFER_TRACE(jh, "on running transaction");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001882 may_free = __dispose_buffer(jh, transaction);
1883 }
1884
1885zap_buffer:
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001886 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001887zap_buffer_no_jh:
1888 spin_unlock(&journal->j_list_lock);
1889 jbd_unlock_bh_state(bh);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001890 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001891zap_buffer_unlocked:
1892 clear_buffer_dirty(bh);
1893 J_ASSERT_BH(bh, !buffer_jbddirty(bh));
1894 clear_buffer_mapped(bh);
1895 clear_buffer_req(bh);
1896 clear_buffer_new(bh);
1897 bh->b_bdev = NULL;
1898 return may_free;
1899}
1900
1901/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001902 * void jbd2_journal_invalidatepage()
Dave Kleikamp470decc2006-10-11 01:20:57 -07001903 * @journal: journal to use for flush...
1904 * @page: page to flush
1905 * @offset: length of page to invalidate.
1906 *
1907 * Reap page buffers containing data after offset in page.
1908 *
1909 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001910void jbd2_journal_invalidatepage(journal_t *journal,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001911 struct page *page,
1912 unsigned long offset)
1913{
1914 struct buffer_head *head, *bh, *next;
1915 unsigned int curr_off = 0;
1916 int may_free = 1;
1917
1918 if (!PageLocked(page))
1919 BUG();
1920 if (!page_has_buffers(page))
1921 return;
1922
1923 /* We will potentially be playing with lists other than just the
1924 * data lists (especially for journaled data mode), so be
1925 * cautious in our locking. */
1926
1927 head = bh = page_buffers(page);
1928 do {
1929 unsigned int next_off = curr_off + bh->b_size;
1930 next = bh->b_this_page;
1931
1932 if (offset <= curr_off) {
1933 /* This block is wholly outside the truncation point */
1934 lock_buffer(bh);
1935 may_free &= journal_unmap_buffer(journal, bh);
1936 unlock_buffer(bh);
1937 }
1938 curr_off = next_off;
1939 bh = next;
1940
1941 } while (bh != head);
1942
1943 if (!offset) {
1944 if (may_free && try_to_free_buffers(page))
1945 J_ASSERT(!page_has_buffers(page));
1946 }
1947}
1948
1949/*
1950 * File a buffer on the given transaction list.
1951 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001952void __jbd2_journal_file_buffer(struct journal_head *jh,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001953 transaction_t *transaction, int jlist)
1954{
1955 struct journal_head **list = NULL;
1956 int was_dirty = 0;
1957 struct buffer_head *bh = jh2bh(jh);
1958
1959 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1960 assert_spin_locked(&transaction->t_journal->j_list_lock);
1961
1962 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1963 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
Mingming Cao40191912008-01-28 23:58:27 -05001964 jh->b_transaction == NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001965
1966 if (jh->b_transaction && jh->b_jlist == jlist)
1967 return;
1968
Dave Kleikamp470decc2006-10-11 01:20:57 -07001969 if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
1970 jlist == BJ_Shadow || jlist == BJ_Forget) {
Jan Karaf91d1d02009-07-13 16:16:20 -04001971 /*
1972 * For metadata buffers, we track dirty bit in buffer_jbddirty
1973 * instead of buffer_dirty. We should not see a dirty bit set
1974 * here because we clear it in do_get_write_access but e.g.
1975 * tune2fs can modify the sb and set the dirty bit at any time
1976 * so we try to gracefully handle that.
1977 */
1978 if (buffer_dirty(bh))
1979 warn_dirty_buffer(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001980 if (test_clear_buffer_dirty(bh) ||
1981 test_clear_buffer_jbddirty(bh))
1982 was_dirty = 1;
1983 }
1984
1985 if (jh->b_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001986 __jbd2_journal_temp_unlink_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001987 jh->b_transaction = transaction;
1988
1989 switch (jlist) {
1990 case BJ_None:
1991 J_ASSERT_JH(jh, !jh->b_committed_data);
1992 J_ASSERT_JH(jh, !jh->b_frozen_data);
1993 return;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001994 case BJ_Metadata:
1995 transaction->t_nr_buffers++;
1996 list = &transaction->t_buffers;
1997 break;
1998 case BJ_Forget:
1999 list = &transaction->t_forget;
2000 break;
2001 case BJ_IO:
2002 list = &transaction->t_iobuf_list;
2003 break;
2004 case BJ_Shadow:
2005 list = &transaction->t_shadow_list;
2006 break;
2007 case BJ_LogCtl:
2008 list = &transaction->t_log_list;
2009 break;
2010 case BJ_Reserved:
2011 list = &transaction->t_reserved_list;
2012 break;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002013 }
2014
2015 __blist_add_buffer(list, jh);
2016 jh->b_jlist = jlist;
2017
2018 if (was_dirty)
2019 set_buffer_jbddirty(bh);
2020}
2021
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002022void jbd2_journal_file_buffer(struct journal_head *jh,
Dave Kleikamp470decc2006-10-11 01:20:57 -07002023 transaction_t *transaction, int jlist)
2024{
2025 jbd_lock_bh_state(jh2bh(jh));
2026 spin_lock(&transaction->t_journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002027 __jbd2_journal_file_buffer(jh, transaction, jlist);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002028 spin_unlock(&transaction->t_journal->j_list_lock);
2029 jbd_unlock_bh_state(jh2bh(jh));
2030}
2031
2032/*
2033 * Remove a buffer from its current buffer list in preparation for
2034 * dropping it from its current transaction entirely. If the buffer has
2035 * already started to be used by a subsequent transaction, refile the
2036 * buffer on that transaction's metadata list.
2037 *
2038 * Called under journal->j_list_lock
2039 *
2040 * Called under jbd_lock_bh_state(jh2bh(jh))
2041 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002042void __jbd2_journal_refile_buffer(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002043{
dingdinghuaba869022010-02-15 16:35:42 -05002044 int was_dirty, jlist;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002045 struct buffer_head *bh = jh2bh(jh);
2046
2047 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
2048 if (jh->b_transaction)
2049 assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
2050
2051 /* If the buffer is now unused, just drop it. */
2052 if (jh->b_next_transaction == NULL) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002053 __jbd2_journal_unfile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002054 return;
2055 }
2056
2057 /*
2058 * It has been modified by a later transaction: add it to the new
2059 * transaction's metadata list.
2060 */
2061
2062 was_dirty = test_clear_buffer_jbddirty(bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002063 __jbd2_journal_temp_unlink_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002064 jh->b_transaction = jh->b_next_transaction;
2065 jh->b_next_transaction = NULL;
dingdinghuaba869022010-02-15 16:35:42 -05002066 if (buffer_freed(bh))
2067 jlist = BJ_Forget;
2068 else if (jh->b_modified)
2069 jlist = BJ_Metadata;
2070 else
2071 jlist = BJ_Reserved;
2072 __jbd2_journal_file_buffer(jh, jh->b_transaction, jlist);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002073 J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
2074
2075 if (was_dirty)
2076 set_buffer_jbddirty(bh);
2077}
2078
2079/*
2080 * For the unlocked version of this call, also make sure that any
2081 * hanging journal_head is cleaned up if necessary.
2082 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002083 * __jbd2_journal_refile_buffer is usually called as part of a single locked
Dave Kleikamp470decc2006-10-11 01:20:57 -07002084 * operation on a buffer_head, in which the caller is probably going to
2085 * be hooking the journal_head onto other lists. In that case it is up
2086 * to the caller to remove the journal_head if necessary. For the
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002087 * unlocked jbd2_journal_refile_buffer call, the caller isn't going to be
Dave Kleikamp470decc2006-10-11 01:20:57 -07002088 * doing anything else to the buffer so we need to do the cleanup
2089 * ourselves to avoid a jh leak.
2090 *
2091 * *** The journal_head may be freed by this call! ***
2092 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002093void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002094{
2095 struct buffer_head *bh = jh2bh(jh);
2096
2097 jbd_lock_bh_state(bh);
2098 spin_lock(&journal->j_list_lock);
2099
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002100 __jbd2_journal_refile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002101 jbd_unlock_bh_state(bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002102 jbd2_journal_remove_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002103
2104 spin_unlock(&journal->j_list_lock);
2105 __brelse(bh);
2106}
Jan Karac851ed52008-07-11 19:27:31 -04002107
2108/*
2109 * File inode in the inode list of the handle's transaction
2110 */
2111int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode)
2112{
2113 transaction_t *transaction = handle->h_transaction;
2114 journal_t *journal = transaction->t_journal;
2115
2116 if (is_handle_aborted(handle))
2117 return -EIO;
2118
2119 jbd_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino,
2120 transaction->t_tid);
2121
2122 /*
2123 * First check whether inode isn't already on the transaction's
2124 * lists without taking the lock. Note that this check is safe
2125 * without the lock as we cannot race with somebody removing inode
2126 * from the transaction. The reason is that we remove inode from the
2127 * transaction only in journal_release_jbd_inode() and when we commit
2128 * the transaction. We are guarded from the first case by holding
2129 * a reference to the inode. We are safe against the second case
2130 * because if jinode->i_transaction == transaction, commit code
2131 * cannot touch the transaction because we hold reference to it,
2132 * and if jinode->i_next_transaction == transaction, commit code
2133 * will only file the inode where we want it.
2134 */
2135 if (jinode->i_transaction == transaction ||
2136 jinode->i_next_transaction == transaction)
2137 return 0;
2138
2139 spin_lock(&journal->j_list_lock);
2140
2141 if (jinode->i_transaction == transaction ||
2142 jinode->i_next_transaction == transaction)
2143 goto done;
2144
2145 /* On some different transaction's list - should be
2146 * the committing one */
2147 if (jinode->i_transaction) {
2148 J_ASSERT(jinode->i_next_transaction == NULL);
2149 J_ASSERT(jinode->i_transaction ==
2150 journal->j_committing_transaction);
2151 jinode->i_next_transaction = transaction;
2152 goto done;
2153 }
2154 /* Not on any transaction list... */
2155 J_ASSERT(!jinode->i_next_transaction);
2156 jinode->i_transaction = transaction;
2157 list_add(&jinode->i_list, &transaction->t_inode_list);
2158done:
2159 spin_unlock(&journal->j_list_lock);
2160
2161 return 0;
2162}
2163
2164/*
Jan Kara7f5aa212009-02-10 11:15:34 -05002165 * File truncate and transaction commit interact with each other in a
2166 * non-trivial way. If a transaction writing data block A is
2167 * committing, we cannot discard the data by truncate until we have
2168 * written them. Otherwise if we crashed after the transaction with
2169 * write has committed but before the transaction with truncate has
2170 * committed, we could see stale data in block A. This function is a
2171 * helper to solve this problem. It starts writeout of the truncated
2172 * part in case it is in the committing transaction.
2173 *
2174 * Filesystem code must call this function when inode is journaled in
2175 * ordered mode before truncation happens and after the inode has been
2176 * placed on orphan list with the new inode size. The second condition
2177 * avoids the race that someone writes new data and we start
2178 * committing the transaction after this function has been called but
2179 * before a transaction for truncate is started (and furthermore it
2180 * allows us to optimize the case where the addition to orphan list
2181 * happens in the same transaction as write --- we don't have to write
2182 * any data in such case).
Jan Karac851ed52008-07-11 19:27:31 -04002183 */
Jan Kara7f5aa212009-02-10 11:15:34 -05002184int jbd2_journal_begin_ordered_truncate(journal_t *journal,
2185 struct jbd2_inode *jinode,
Jan Karac851ed52008-07-11 19:27:31 -04002186 loff_t new_size)
2187{
Jan Kara7f5aa212009-02-10 11:15:34 -05002188 transaction_t *inode_trans, *commit_trans;
Jan Karac851ed52008-07-11 19:27:31 -04002189 int ret = 0;
2190
Jan Kara7f5aa212009-02-10 11:15:34 -05002191 /* This is a quick check to avoid locking if not necessary */
2192 if (!jinode->i_transaction)
Jan Karac851ed52008-07-11 19:27:31 -04002193 goto out;
Jan Kara7f5aa212009-02-10 11:15:34 -05002194 /* Locks are here just to force reading of recent values, it is
2195 * enough that the transaction was not committing before we started
2196 * a transaction adding the inode to orphan list */
Theodore Ts'oa931da62010-08-03 21:35:12 -04002197 read_lock(&journal->j_state_lock);
Jan Karac851ed52008-07-11 19:27:31 -04002198 commit_trans = journal->j_committing_transaction;
Theodore Ts'oa931da62010-08-03 21:35:12 -04002199 read_unlock(&journal->j_state_lock);
Jan Kara7f5aa212009-02-10 11:15:34 -05002200 spin_lock(&journal->j_list_lock);
2201 inode_trans = jinode->i_transaction;
2202 spin_unlock(&journal->j_list_lock);
2203 if (inode_trans == commit_trans) {
2204 ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping,
Jan Karac851ed52008-07-11 19:27:31 -04002205 new_size, LLONG_MAX);
2206 if (ret)
2207 jbd2_journal_abort(journal, ret);
2208 }
2209out:
2210 return ret;
2211}