blob: 60361c634b5d15fd6f0bb1f5bb61ba21054c3686 [file] [log] [blame]
Dave Kleikamp470decc2006-10-11 01:20:57 -07001/*
Uwe Kleine-König58862692007-05-09 07:51:49 +02002 * linux/fs/jbd2/transaction.c
Dave Kleikamp470decc2006-10-11 01:20:57 -07003 *
4 * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
5 *
6 * Copyright 1998 Red Hat corp --- All Rights Reserved
7 *
8 * This file is part of the Linux kernel and is made available under
9 * the terms of the GNU General Public License, version 2, or at your
10 * option, any later version, incorporated herein by reference.
11 *
12 * Generic filesystem transaction handling code; part of the ext2fs
13 * journaling system.
14 *
15 * This file manages transactions (compound commits managed by the
16 * journaling code) and handles (individual atomic operations by the
17 * filesystem).
18 */
19
20#include <linux/time.h>
21#include <linux/fs.h>
Mingming Caof7f4bcc2006-10-11 01:20:59 -070022#include <linux/jbd2.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070023#include <linux/errno.h>
24#include <linux/slab.h>
25#include <linux/timer.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070026#include <linux/mm.h>
27#include <linux/highmem.h>
Josef Bacike07f7182008-11-26 01:14:26 -050028#include <linux/hrtimer.h>
Theodore Ts'o47def822010-07-27 11:56:05 -040029#include <linux/backing-dev.h>
Randy Dunlap44705752011-10-27 04:05:13 -040030#include <linux/bug.h>
Theodore Ts'o47def822010-07-27 11:56:05 -040031#include <linux/module.h>
Dave Kleikamp470decc2006-10-11 01:20:57 -070032
Theodore Ts'o343d9c22013-02-08 13:00:22 -050033#include <trace/events/jbd2.h>
34
Adrian Bunk7ddae862006-12-06 20:38:27 -080035static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh);
Jan Karade1b7942011-06-13 15:38:22 -040036static void __jbd2_journal_unfile_buffer(struct journal_head *jh);
Adrian Bunk7ddae862006-12-06 20:38:27 -080037
Yongqiang Yang0c2022e2012-02-20 17:53:02 -050038static struct kmem_cache *transaction_cache;
39int __init jbd2_journal_init_transaction_cache(void)
40{
41 J_ASSERT(!transaction_cache);
42 transaction_cache = kmem_cache_create("jbd2_transaction_s",
43 sizeof(transaction_t),
44 0,
45 SLAB_HWCACHE_ALIGN|SLAB_TEMPORARY,
46 NULL);
47 if (transaction_cache)
48 return 0;
49 return -ENOMEM;
50}
51
52void jbd2_journal_destroy_transaction_cache(void)
53{
54 if (transaction_cache) {
55 kmem_cache_destroy(transaction_cache);
56 transaction_cache = NULL;
57 }
58}
59
60void jbd2_journal_free_transaction(transaction_t *transaction)
61{
62 if (unlikely(ZERO_OR_NULL_PTR(transaction)))
63 return;
64 kmem_cache_free(transaction_cache, transaction);
65}
66
Dave Kleikamp470decc2006-10-11 01:20:57 -070067/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -070068 * jbd2_get_transaction: obtain a new transaction_t object.
Dave Kleikamp470decc2006-10-11 01:20:57 -070069 *
70 * Simply allocate and initialise a new transaction. Create it in
71 * RUNNING state and add it to the current journal (which should not
72 * have an existing running transaction: we only make a new transaction
73 * once we have started to commit the old one).
74 *
75 * Preconditions:
76 * The journal MUST be locked. We don't perform atomic mallocs on the
77 * new transaction and we can't block without protecting against other
78 * processes trying to touch the journal while it is in transition.
79 *
Dave Kleikamp470decc2006-10-11 01:20:57 -070080 */
81
82static transaction_t *
Mingming Caof7f4bcc2006-10-11 01:20:59 -070083jbd2_get_transaction(journal_t *journal, transaction_t *transaction)
Dave Kleikamp470decc2006-10-11 01:20:57 -070084{
85 transaction->t_journal = journal;
86 transaction->t_state = T_RUNNING;
Josef Bacike07f7182008-11-26 01:14:26 -050087 transaction->t_start_time = ktime_get();
Dave Kleikamp470decc2006-10-11 01:20:57 -070088 transaction->t_tid = journal->j_transaction_sequence++;
89 transaction->t_expires = jiffies + journal->j_commit_interval;
90 spin_lock_init(&transaction->t_handle_lock);
Theodore Ts'oa51dca92010-08-02 08:43:25 -040091 atomic_set(&transaction->t_updates, 0);
92 atomic_set(&transaction->t_outstanding_credits, 0);
Theodore Ts'o8dd42042010-08-03 21:38:29 -040093 atomic_set(&transaction->t_handle_count, 0);
Jan Karac851ed52008-07-11 19:27:31 -040094 INIT_LIST_HEAD(&transaction->t_inode_list);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -040095 INIT_LIST_HEAD(&transaction->t_private_list);
Dave Kleikamp470decc2006-10-11 01:20:57 -070096
97 /* Set up the commit timer for the new transaction. */
Andreas Dilgerb1f485f2009-08-10 22:51:53 -040098 journal->j_commit_timer.expires = round_jiffies_up(transaction->t_expires);
Dave Kleikamp470decc2006-10-11 01:20:57 -070099 add_timer(&journal->j_commit_timer);
100
101 J_ASSERT(journal->j_running_transaction == NULL);
102 journal->j_running_transaction = transaction;
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500103 transaction->t_max_wait = 0;
104 transaction->t_start = jiffies;
Theodore Ts'o9fff24a2013-02-06 22:30:23 -0500105 transaction->t_requested = 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700106
107 return transaction;
108}
109
110/*
111 * Handle management.
112 *
113 * A handle_t is an object which represents a single atomic update to a
114 * filesystem, and which tracks all of the modifications which form part
115 * of that one update.
116 */
117
118/*
Tao Ma28e35e42011-05-22 21:45:26 -0400119 * Update transaction's maximum wait time, if debugging is enabled.
Theodore Ts'o6d0bf002010-08-09 17:28:38 -0400120 *
121 * In order for t_max_wait to be reliable, it must be protected by a
122 * lock. But doing so will mean that start_this_handle() can not be
123 * run in parallel on SMP systems, which limits our scalability. So
124 * unless debugging is enabled, we no longer update t_max_wait, which
125 * means that maximum wait time reported by the jbd2_run_stats
126 * tracepoint will always be zero.
127 */
Tao Ma28e35e42011-05-22 21:45:26 -0400128static inline void update_t_max_wait(transaction_t *transaction,
129 unsigned long ts)
Theodore Ts'o6d0bf002010-08-09 17:28:38 -0400130{
131#ifdef CONFIG_JBD2_DEBUG
Theodore Ts'o6d0bf002010-08-09 17:28:38 -0400132 if (jbd2_journal_enable_debug &&
133 time_after(transaction->t_start, ts)) {
134 ts = jbd2_time_diff(ts, transaction->t_start);
135 spin_lock(&transaction->t_handle_lock);
136 if (ts > transaction->t_max_wait)
137 transaction->t_max_wait = ts;
138 spin_unlock(&transaction->t_handle_lock);
139 }
140#endif
141}
142
143/*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700144 * start_this_handle: Given a handle, deal with any locking or stalling
145 * needed to make sure that there is enough journal space for the handle
146 * to begin. Attach the handle to a transaction and set up the
147 * transaction's buffer credits.
148 */
149
Theodore Ts'o47def822010-07-27 11:56:05 -0400150static int start_this_handle(journal_t *journal, handle_t *handle,
Dan Carpenterd2159fb2011-09-04 10:20:14 -0400151 gfp_t gfp_mask)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700152{
Theodore Ts'oe4471832011-02-12 08:18:24 -0500153 transaction_t *transaction, *new_transaction = NULL;
154 tid_t tid;
155 int needed, need_to_start;
156 int nblocks = handle->h_buffer_credits;
Tao Ma28e35e42011-05-22 21:45:26 -0400157 unsigned long ts = jiffies;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700158
159 if (nblocks > journal->j_max_transaction_buffers) {
Eryu Guanf2a44522011-11-01 19:09:18 -0400160 printk(KERN_ERR "JBD2: %s wants too many credits (%d > %d)\n",
Dave Kleikamp470decc2006-10-11 01:20:57 -0700161 current->comm, nblocks,
162 journal->j_max_transaction_buffers);
Theodore Ts'o47def822010-07-27 11:56:05 -0400163 return -ENOSPC;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700164 }
165
166alloc_transaction:
167 if (!journal->j_running_transaction) {
Wanlong Gaob2f4edb2012-06-01 00:10:32 -0400168 new_transaction = kmem_cache_zalloc(transaction_cache,
169 gfp_mask);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700170 if (!new_transaction) {
Theodore Ts'o47def822010-07-27 11:56:05 -0400171 /*
172 * If __GFP_FS is not present, then we may be
173 * being called from inside the fs writeback
174 * layer, so we MUST NOT fail. Since
175 * __GFP_NOFAIL is going away, we will arrange
176 * to retry the allocation ourselves.
177 */
178 if ((gfp_mask & __GFP_FS) == 0) {
179 congestion_wait(BLK_RW_ASYNC, HZ/50);
180 goto alloc_transaction;
181 }
182 return -ENOMEM;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700183 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700184 }
185
186 jbd_debug(3, "New handle %p going live.\n", handle);
187
Dave Kleikamp470decc2006-10-11 01:20:57 -0700188 /*
189 * We need to hold j_state_lock until t_updates has been incremented,
190 * for proper journal barrier handling
191 */
Theodore Ts'oa931da62010-08-03 21:35:12 -0400192repeat:
193 read_lock(&journal->j_state_lock);
Theodore Ts'o5c2178e2010-10-27 21:30:04 -0400194 BUG_ON(journal->j_flags & JBD2_UNMOUNT);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700195 if (is_journal_aborted(journal) ||
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700196 (journal->j_errno != 0 && !(journal->j_flags & JBD2_ACK_ERR))) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400197 read_unlock(&journal->j_state_lock);
Yongqiang Yang0c2022e2012-02-20 17:53:02 -0500198 jbd2_journal_free_transaction(new_transaction);
Theodore Ts'o47def822010-07-27 11:56:05 -0400199 return -EROFS;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700200 }
201
202 /* Wait on the journal's transaction barrier if necessary */
203 if (journal->j_barrier_count) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400204 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700205 wait_event(journal->j_wait_transaction_locked,
206 journal->j_barrier_count == 0);
207 goto repeat;
208 }
209
210 if (!journal->j_running_transaction) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400211 read_unlock(&journal->j_state_lock);
212 if (!new_transaction)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700213 goto alloc_transaction;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400214 write_lock(&journal->j_state_lock);
Jan Karad7961c72012-12-21 00:15:51 -0500215 if (!journal->j_running_transaction &&
216 !journal->j_barrier_count) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400217 jbd2_get_transaction(journal, new_transaction);
218 new_transaction = NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700219 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400220 write_unlock(&journal->j_state_lock);
221 goto repeat;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700222 }
223
224 transaction = journal->j_running_transaction;
225
226 /*
227 * If the current transaction is locked down for commit, wait for the
228 * lock to be released.
229 */
230 if (transaction->t_state == T_LOCKED) {
231 DEFINE_WAIT(wait);
232
233 prepare_to_wait(&journal->j_wait_transaction_locked,
234 &wait, TASK_UNINTERRUPTIBLE);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400235 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700236 schedule();
237 finish_wait(&journal->j_wait_transaction_locked, &wait);
238 goto repeat;
239 }
240
241 /*
242 * If there is not enough space left in the log to write all potential
243 * buffers requested by this operation, we need to stall pending a log
244 * checkpoint to free some more log space.
245 */
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400246 needed = atomic_add_return(nblocks,
247 &transaction->t_outstanding_credits);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700248
249 if (needed > journal->j_max_transaction_buffers) {
250 /*
251 * If the current transaction is already too large, then start
252 * to commit it: we can then go back and attach this handle to
253 * a new transaction.
254 */
255 DEFINE_WAIT(wait);
256
257 jbd_debug(2, "Handle %p starting new commit...\n", handle);
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400258 atomic_sub(nblocks, &transaction->t_outstanding_credits);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700259 prepare_to_wait(&journal->j_wait_transaction_locked, &wait,
260 TASK_UNINTERRUPTIBLE);
Theodore Ts'oe4471832011-02-12 08:18:24 -0500261 tid = transaction->t_tid;
262 need_to_start = !tid_geq(journal->j_commit_request, tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400263 read_unlock(&journal->j_state_lock);
Theodore Ts'oe4471832011-02-12 08:18:24 -0500264 if (need_to_start)
265 jbd2_log_start_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700266 schedule();
267 finish_wait(&journal->j_wait_transaction_locked, &wait);
268 goto repeat;
269 }
270
271 /*
272 * The commit code assumes that it can get enough log space
273 * without forcing a checkpoint. This is *critical* for
274 * correctness: a checkpoint of a buffer which is also
275 * associated with a committing transaction creates a deadlock,
276 * so commit simply cannot force through checkpoints.
277 *
278 * We must therefore ensure the necessary space in the journal
279 * *before* starting to dirty potentially checkpointed buffers
280 * in the new transaction.
281 *
282 * The worst part is, any transaction currently committing can
283 * reduce the free space arbitrarily. Be careful to account for
284 * those buffers when checkpointing.
285 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700286 if (__jbd2_log_space_left(journal) < jbd_space_needed(journal)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700287 jbd_debug(2, "Handle %p waiting for checkpoint...\n", handle);
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400288 atomic_sub(nblocks, &transaction->t_outstanding_credits);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400289 read_unlock(&journal->j_state_lock);
290 write_lock(&journal->j_state_lock);
291 if (__jbd2_log_space_left(journal) < jbd_space_needed(journal))
292 __jbd2_log_wait_for_space(journal);
293 write_unlock(&journal->j_state_lock);
294 goto repeat;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700295 }
296
297 /* OK, account for the buffers that this operation expects to
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400298 * use and add the handle to the running transaction.
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400299 */
Tao Ma28e35e42011-05-22 21:45:26 -0400300 update_t_max_wait(transaction, ts);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700301 handle->h_transaction = transaction;
Theodore Ts'o343d9c22013-02-08 13:00:22 -0500302 handle->h_requested_credits = nblocks;
303 handle->h_start_jiffies = jiffies;
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400304 atomic_inc(&transaction->t_updates);
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400305 atomic_inc(&transaction->t_handle_count);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700306 jbd_debug(4, "Handle %p given %d credits (total %d, free %d)\n",
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400307 handle, nblocks,
308 atomic_read(&transaction->t_outstanding_credits),
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700309 __jbd2_log_space_left(journal));
Theodore Ts'oa931da62010-08-03 21:35:12 -0400310 read_unlock(&journal->j_state_lock);
Jan Kara9599b0e2009-08-17 21:23:17 -0400311
312 lock_map_acquire(&handle->h_lockdep_map);
Yongqiang Yang0c2022e2012-02-20 17:53:02 -0500313 jbd2_journal_free_transaction(new_transaction);
Theodore Ts'o47def822010-07-27 11:56:05 -0400314 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700315}
316
Mingming Cao7b751062008-01-28 23:58:27 -0500317static struct lock_class_key jbd2_handle_key;
318
Dave Kleikamp470decc2006-10-11 01:20:57 -0700319/* Allocate a new handle. This should probably be in a slab... */
320static handle_t *new_handle(int nblocks)
321{
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400322 handle_t *handle = jbd2_alloc_handle(GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700323 if (!handle)
324 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700325 handle->h_buffer_credits = nblocks;
326 handle->h_ref = 1;
327
Mingming Cao7b751062008-01-28 23:58:27 -0500328 lockdep_init_map(&handle->h_lockdep_map, "jbd2_handle",
329 &jbd2_handle_key, 0);
330
Dave Kleikamp470decc2006-10-11 01:20:57 -0700331 return handle;
332}
333
334/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700335 * handle_t *jbd2_journal_start() - Obtain a new handle.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700336 * @journal: Journal to start transaction on.
337 * @nblocks: number of block buffer we might modify
338 *
339 * We make sure that the transaction can guarantee at least nblocks of
340 * modified buffers in the log. We block until the log can guarantee
341 * that much space.
342 *
343 * This function is visible to journal users (like ext3fs), so is not
344 * called with the journal already locked.
345 *
Eryu Guanc8675162011-05-24 17:09:58 -0400346 * Return a pointer to a newly allocated handle, or an ERR_PTR() value
347 * on failure.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700348 */
Theodore Ts'o343d9c22013-02-08 13:00:22 -0500349handle_t *jbd2__journal_start(journal_t *journal, int nblocks, gfp_t gfp_mask,
350 unsigned int type, unsigned int line_no)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700351{
352 handle_t *handle = journal_current_handle();
353 int err;
354
355 if (!journal)
356 return ERR_PTR(-EROFS);
357
358 if (handle) {
359 J_ASSERT(handle->h_transaction->t_journal == journal);
360 handle->h_ref++;
361 return handle;
362 }
363
364 handle = new_handle(nblocks);
365 if (!handle)
366 return ERR_PTR(-ENOMEM);
367
368 current->journal_info = handle;
369
Theodore Ts'o47def822010-07-27 11:56:05 -0400370 err = start_this_handle(journal, handle, gfp_mask);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700371 if (err < 0) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400372 jbd2_free_handle(handle);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700373 current->journal_info = NULL;
Dmitry Monakhovdf05c1b82013-03-02 17:08:46 -0500374 return ERR_PTR(err);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700375 }
Theodore Ts'o343d9c22013-02-08 13:00:22 -0500376 handle->h_type = type;
377 handle->h_line_no = line_no;
378 trace_jbd2_handle_start(journal->j_fs_dev->bd_dev,
379 handle->h_transaction->t_tid, type,
380 line_no, nblocks);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700381 return handle;
382}
Theodore Ts'o47def822010-07-27 11:56:05 -0400383EXPORT_SYMBOL(jbd2__journal_start);
384
385
386handle_t *jbd2_journal_start(journal_t *journal, int nblocks)
387{
Theodore Ts'o343d9c22013-02-08 13:00:22 -0500388 return jbd2__journal_start(journal, nblocks, GFP_NOFS, 0, 0);
Theodore Ts'o47def822010-07-27 11:56:05 -0400389}
390EXPORT_SYMBOL(jbd2_journal_start);
391
Dave Kleikamp470decc2006-10-11 01:20:57 -0700392
393/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700394 * int jbd2_journal_extend() - extend buffer credits.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700395 * @handle: handle to 'extend'
396 * @nblocks: nr blocks to try to extend by.
397 *
398 * Some transactions, such as large extends and truncates, can be done
399 * atomically all at once or in several stages. The operation requests
400 * a credit for a number of buffer modications in advance, but can
401 * extend its credit if it needs more.
402 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700403 * jbd2_journal_extend tries to give the running handle more buffer credits.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700404 * It does not guarantee that allocation - this is a best-effort only.
405 * The calling process MUST be able to deal cleanly with a failure to
406 * extend here.
407 *
408 * Return 0 on success, non-zero on failure.
409 *
410 * return code < 0 implies an error
411 * return code > 0 implies normal transaction-full status.
412 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700413int jbd2_journal_extend(handle_t *handle, int nblocks)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700414{
415 transaction_t *transaction = handle->h_transaction;
416 journal_t *journal = transaction->t_journal;
417 int result;
418 int wanted;
419
420 result = -EIO;
421 if (is_handle_aborted(handle))
422 goto out;
423
424 result = 1;
425
Theodore Ts'oa931da62010-08-03 21:35:12 -0400426 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700427
428 /* Don't extend a locked-down transaction! */
429 if (handle->h_transaction->t_state != T_RUNNING) {
430 jbd_debug(3, "denied handle %p %d blocks: "
431 "transaction not running\n", handle, nblocks);
432 goto error_out;
433 }
434
435 spin_lock(&transaction->t_handle_lock);
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400436 wanted = atomic_read(&transaction->t_outstanding_credits) + nblocks;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700437
438 if (wanted > journal->j_max_transaction_buffers) {
439 jbd_debug(3, "denied handle %p %d blocks: "
440 "transaction too large\n", handle, nblocks);
441 goto unlock;
442 }
443
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700444 if (wanted > __jbd2_log_space_left(journal)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700445 jbd_debug(3, "denied handle %p %d blocks: "
446 "insufficient log space\n", handle, nblocks);
447 goto unlock;
448 }
449
Theodore Ts'o343d9c22013-02-08 13:00:22 -0500450 trace_jbd2_handle_extend(journal->j_fs_dev->bd_dev,
451 handle->h_transaction->t_tid,
452 handle->h_type, handle->h_line_no,
453 handle->h_buffer_credits,
454 nblocks);
455
Dave Kleikamp470decc2006-10-11 01:20:57 -0700456 handle->h_buffer_credits += nblocks;
Theodore Ts'o343d9c22013-02-08 13:00:22 -0500457 handle->h_requested_credits += nblocks;
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400458 atomic_add(nblocks, &transaction->t_outstanding_credits);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700459 result = 0;
460
461 jbd_debug(3, "extended handle %p by %d\n", handle, nblocks);
462unlock:
463 spin_unlock(&transaction->t_handle_lock);
464error_out:
Theodore Ts'oa931da62010-08-03 21:35:12 -0400465 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700466out:
467 return result;
468}
469
470
471/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700472 * int jbd2_journal_restart() - restart a handle .
Dave Kleikamp470decc2006-10-11 01:20:57 -0700473 * @handle: handle to restart
474 * @nblocks: nr credits requested
475 *
476 * Restart a handle for a multi-transaction filesystem
477 * operation.
478 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700479 * If the jbd2_journal_extend() call above fails to grant new buffer credits
480 * to a running handle, a call to jbd2_journal_restart will commit the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700481 * handle's transaction so far and reattach the handle to a new
482 * transaction capabable of guaranteeing the requested number of
483 * credits.
484 */
Dan Carpenterd2159fb2011-09-04 10:20:14 -0400485int jbd2__journal_restart(handle_t *handle, int nblocks, gfp_t gfp_mask)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700486{
487 transaction_t *transaction = handle->h_transaction;
488 journal_t *journal = transaction->t_journal;
Theodore Ts'oe4471832011-02-12 08:18:24 -0500489 tid_t tid;
490 int need_to_start, ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700491
492 /* If we've had an abort of any type, don't even think about
493 * actually doing the restart! */
494 if (is_handle_aborted(handle))
495 return 0;
496
497 /*
498 * First unlink the handle from its current transaction, and start the
499 * commit on that.
500 */
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400501 J_ASSERT(atomic_read(&transaction->t_updates) > 0);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700502 J_ASSERT(journal_current_handle() == handle);
503
Theodore Ts'oa931da62010-08-03 21:35:12 -0400504 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700505 spin_lock(&transaction->t_handle_lock);
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400506 atomic_sub(handle->h_buffer_credits,
507 &transaction->t_outstanding_credits);
508 if (atomic_dec_and_test(&transaction->t_updates))
Dave Kleikamp470decc2006-10-11 01:20:57 -0700509 wake_up(&journal->j_wait_updates);
510 spin_unlock(&transaction->t_handle_lock);
511
512 jbd_debug(2, "restarting handle %p\n", handle);
Theodore Ts'oe4471832011-02-12 08:18:24 -0500513 tid = transaction->t_tid;
514 need_to_start = !tid_geq(journal->j_commit_request, tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400515 read_unlock(&journal->j_state_lock);
Theodore Ts'oe4471832011-02-12 08:18:24 -0500516 if (need_to_start)
517 jbd2_log_start_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700518
Jan Kara9599b0e2009-08-17 21:23:17 -0400519 lock_map_release(&handle->h_lockdep_map);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700520 handle->h_buffer_credits = nblocks;
Theodore Ts'o47def822010-07-27 11:56:05 -0400521 ret = start_this_handle(journal, handle, gfp_mask);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700522 return ret;
523}
Theodore Ts'o47def822010-07-27 11:56:05 -0400524EXPORT_SYMBOL(jbd2__journal_restart);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700525
526
Theodore Ts'o47def822010-07-27 11:56:05 -0400527int jbd2_journal_restart(handle_t *handle, int nblocks)
528{
529 return jbd2__journal_restart(handle, nblocks, GFP_NOFS);
530}
531EXPORT_SYMBOL(jbd2_journal_restart);
532
Dave Kleikamp470decc2006-10-11 01:20:57 -0700533/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700534 * void jbd2_journal_lock_updates () - establish a transaction barrier.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700535 * @journal: Journal to establish a barrier on.
536 *
537 * This locks out any further updates from being started, and blocks
538 * until all existing updates have completed, returning only once the
539 * journal is in a quiescent state with no updates running.
540 *
541 * The journal lock should not be held on entry.
542 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700543void jbd2_journal_lock_updates(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700544{
545 DEFINE_WAIT(wait);
546
Theodore Ts'oa931da62010-08-03 21:35:12 -0400547 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700548 ++journal->j_barrier_count;
549
550 /* Wait until there are no running updates */
551 while (1) {
552 transaction_t *transaction = journal->j_running_transaction;
553
554 if (!transaction)
555 break;
556
557 spin_lock(&transaction->t_handle_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700558 prepare_to_wait(&journal->j_wait_updates, &wait,
559 TASK_UNINTERRUPTIBLE);
Jan Kara9837d8e2012-01-04 22:03:11 -0500560 if (!atomic_read(&transaction->t_updates)) {
561 spin_unlock(&transaction->t_handle_lock);
562 finish_wait(&journal->j_wait_updates, &wait);
563 break;
564 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700565 spin_unlock(&transaction->t_handle_lock);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400566 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700567 schedule();
568 finish_wait(&journal->j_wait_updates, &wait);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400569 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700570 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400571 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700572
573 /*
574 * We have now established a barrier against other normal updates, but
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700575 * we also need to barrier against other jbd2_journal_lock_updates() calls
Dave Kleikamp470decc2006-10-11 01:20:57 -0700576 * to make sure that we serialise special journal-locked operations
577 * too.
578 */
579 mutex_lock(&journal->j_barrier);
580}
581
582/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700583 * void jbd2_journal_unlock_updates (journal_t* journal) - release barrier
Dave Kleikamp470decc2006-10-11 01:20:57 -0700584 * @journal: Journal to release the barrier on.
585 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700586 * Release a transaction barrier obtained with jbd2_journal_lock_updates().
Dave Kleikamp470decc2006-10-11 01:20:57 -0700587 *
588 * Should be called without the journal lock held.
589 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700590void jbd2_journal_unlock_updates (journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700591{
592 J_ASSERT(journal->j_barrier_count != 0);
593
594 mutex_unlock(&journal->j_barrier);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400595 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700596 --journal->j_barrier_count;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400597 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700598 wake_up(&journal->j_wait_transaction_locked);
599}
600
Jan Karaf91d1d02009-07-13 16:16:20 -0400601static void warn_dirty_buffer(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700602{
Jan Karaf91d1d02009-07-13 16:16:20 -0400603 char b[BDEVNAME_SIZE];
Dave Kleikamp470decc2006-10-11 01:20:57 -0700604
Jan Karaf91d1d02009-07-13 16:16:20 -0400605 printk(KERN_WARNING
Eryu Guanf2a44522011-11-01 19:09:18 -0400606 "JBD2: Spotted dirty metadata buffer (dev = %s, blocknr = %llu). "
Jan Karaf91d1d02009-07-13 16:16:20 -0400607 "There's a risk of filesystem corruption in case of system "
608 "crash.\n",
609 bdevname(bh->b_bdev, b), (unsigned long long)bh->b_blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700610}
611
Jan Karab34090e2013-06-04 12:08:56 -0400612static int sleep_on_shadow_bh(void *word)
613{
614 io_schedule();
615 return 0;
616}
617
Dave Kleikamp470decc2006-10-11 01:20:57 -0700618/*
619 * If the buffer is already part of the current transaction, then there
620 * is nothing we need to do. If it is already part of a prior
621 * transaction which we are still committing to disk, then we need to
622 * make sure that we do not overwrite the old copy: we do copy-out to
623 * preserve the copy going to disk. We also account the buffer against
624 * the handle's metadata buffer credits (unless the buffer is already
625 * part of the transaction, that is).
626 *
627 */
628static int
629do_get_write_access(handle_t *handle, struct journal_head *jh,
630 int force_copy)
631{
632 struct buffer_head *bh;
633 transaction_t *transaction;
634 journal_t *journal;
635 int error;
636 char *frozen_buffer = NULL;
637 int need_copy = 0;
Theodore Ts'of783f092013-04-21 16:47:54 -0400638 unsigned long start_lock, time_lock;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700639
640 if (is_handle_aborted(handle))
641 return -EROFS;
642
643 transaction = handle->h_transaction;
644 journal = transaction->t_journal;
645
Theodore Ts'ocfef2c62010-12-18 13:07:34 -0500646 jbd_debug(5, "journal_head %p, force_copy %d\n", jh, force_copy);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700647
648 JBUFFER_TRACE(jh, "entry");
649repeat:
650 bh = jh2bh(jh);
651
652 /* @@@ Need to check for errors here at some point. */
653
Theodore Ts'of783f092013-04-21 16:47:54 -0400654 start_lock = jiffies;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700655 lock_buffer(bh);
656 jbd_lock_bh_state(bh);
657
Theodore Ts'of783f092013-04-21 16:47:54 -0400658 /* If it takes too long to lock the buffer, trace it */
659 time_lock = jbd2_time_diff(start_lock, jiffies);
660 if (time_lock > HZ/10)
661 trace_jbd2_lock_buffer_stall(bh->b_bdev->bd_dev,
662 jiffies_to_msecs(time_lock));
663
Dave Kleikamp470decc2006-10-11 01:20:57 -0700664 /* We now hold the buffer lock so it is safe to query the buffer
665 * state. Is the buffer dirty?
666 *
667 * If so, there are two possibilities. The buffer may be
668 * non-journaled, and undergoing a quite legitimate writeback.
669 * Otherwise, it is journaled, and we don't expect dirty buffers
670 * in that state (the buffers should be marked JBD_Dirty
671 * instead.) So either the IO is being done under our own
672 * control and this is a bug, or it's a third party IO such as
673 * dump(8) (which may leave the buffer scheduled for read ---
674 * ie. locked but not dirty) or tune2fs (which may actually have
675 * the buffer dirtied, ugh.) */
676
677 if (buffer_dirty(bh)) {
678 /*
679 * First question: is this buffer already part of the current
680 * transaction or the existing committing transaction?
681 */
682 if (jh->b_transaction) {
683 J_ASSERT_JH(jh,
684 jh->b_transaction == transaction ||
685 jh->b_transaction ==
686 journal->j_committing_transaction);
687 if (jh->b_next_transaction)
688 J_ASSERT_JH(jh, jh->b_next_transaction ==
689 transaction);
Jan Karaf91d1d02009-07-13 16:16:20 -0400690 warn_dirty_buffer(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700691 }
692 /*
693 * In any case we need to clean the dirty flag and we must
694 * do it under the buffer lock to be sure we don't race
695 * with running write-out.
696 */
Jan Karaf91d1d02009-07-13 16:16:20 -0400697 JBUFFER_TRACE(jh, "Journalling dirty buffer");
698 clear_buffer_dirty(bh);
699 set_buffer_jbddirty(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700700 }
701
702 unlock_buffer(bh);
703
704 error = -EROFS;
705 if (is_handle_aborted(handle)) {
706 jbd_unlock_bh_state(bh);
707 goto out;
708 }
709 error = 0;
710
711 /*
712 * The buffer is already part of this transaction if b_transaction or
713 * b_next_transaction points to it
714 */
715 if (jh->b_transaction == transaction ||
716 jh->b_next_transaction == transaction)
717 goto done;
718
719 /*
Josef Bacik9fc7c632008-04-17 10:38:59 -0400720 * this is the first time this transaction is touching this buffer,
721 * reset the modified flag
722 */
723 jh->b_modified = 0;
724
725 /*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700726 * If there is already a copy-out version of this buffer, then we don't
727 * need to make another one
728 */
729 if (jh->b_frozen_data) {
730 JBUFFER_TRACE(jh, "has frozen data");
731 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
732 jh->b_next_transaction = transaction;
733 goto done;
734 }
735
736 /* Is there data here we need to preserve? */
737
738 if (jh->b_transaction && jh->b_transaction != transaction) {
739 JBUFFER_TRACE(jh, "owned by older transaction");
740 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
741 J_ASSERT_JH(jh, jh->b_transaction ==
742 journal->j_committing_transaction);
743
744 /* There is one case we have to be very careful about.
745 * If the committing transaction is currently writing
746 * this buffer out to disk and has NOT made a copy-out,
747 * then we cannot modify the buffer contents at all
748 * right now. The essence of copy-out is that it is the
749 * extra copy, not the primary copy, which gets
750 * journaled. If the primary copy is already going to
751 * disk then we cannot do copy-out here. */
752
Jan Karab34090e2013-06-04 12:08:56 -0400753 if (buffer_shadow(bh)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700754 JBUFFER_TRACE(jh, "on shadow: sleep");
755 jbd_unlock_bh_state(bh);
Jan Karab34090e2013-06-04 12:08:56 -0400756 wait_on_bit(&bh->b_state, BH_Shadow,
757 sleep_on_shadow_bh, TASK_UNINTERRUPTIBLE);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700758 goto repeat;
759 }
760
Jan Karab34090e2013-06-04 12:08:56 -0400761 /*
762 * Only do the copy if the currently-owning transaction still
763 * needs it. If buffer isn't on BJ_Metadata list, the
764 * committing transaction is past that stage (here we use the
765 * fact that BH_Shadow is set under bh_state lock together with
766 * refiling to BJ_Shadow list and at this point we know the
767 * buffer doesn't have BH_Shadow set).
Dave Kleikamp470decc2006-10-11 01:20:57 -0700768 *
769 * Subtle point, though: if this is a get_undo_access,
770 * then we will be relying on the frozen_data to contain
771 * the new value of the committed_data record after the
772 * transaction, so we HAVE to force the frozen_data copy
Jan Karab34090e2013-06-04 12:08:56 -0400773 * in that case.
774 */
775 if (jh->b_jlist == BJ_Metadata || force_copy) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700776 JBUFFER_TRACE(jh, "generate frozen data");
777 if (!frozen_buffer) {
778 JBUFFER_TRACE(jh, "allocate memory for buffer");
779 jbd_unlock_bh_state(bh);
780 frozen_buffer =
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400781 jbd2_alloc(jh2bh(jh)->b_size,
Dave Kleikamp470decc2006-10-11 01:20:57 -0700782 GFP_NOFS);
783 if (!frozen_buffer) {
784 printk(KERN_EMERG
785 "%s: OOM for frozen_buffer\n",
Harvey Harrison329d2912008-04-17 10:38:59 -0400786 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700787 JBUFFER_TRACE(jh, "oom!");
788 error = -ENOMEM;
789 jbd_lock_bh_state(bh);
790 goto done;
791 }
792 goto repeat;
793 }
794 jh->b_frozen_data = frozen_buffer;
795 frozen_buffer = NULL;
796 need_copy = 1;
797 }
798 jh->b_next_transaction = transaction;
799 }
800
801
802 /*
803 * Finally, if the buffer is not journaled right now, we need to make
804 * sure it doesn't get written to disk before the caller actually
805 * commits the new data
806 */
807 if (!jh->b_transaction) {
808 JBUFFER_TRACE(jh, "no transaction");
809 J_ASSERT_JH(jh, !jh->b_next_transaction);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700810 JBUFFER_TRACE(jh, "file as BJ_Reserved");
811 spin_lock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700812 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700813 spin_unlock(&journal->j_list_lock);
814 }
815
816done:
817 if (need_copy) {
818 struct page *page;
819 int offset;
820 char *source;
821
822 J_EXPECT_JH(jh, buffer_uptodate(jh2bh(jh)),
823 "Possible IO failure.\n");
824 page = jh2bh(jh)->b_page;
Theodore Ts'oa1dd5332010-12-18 13:13:40 -0500825 offset = offset_in_page(jh2bh(jh)->b_data);
Cong Wang303a8f22011-11-25 23:14:31 +0800826 source = kmap_atomic(page);
Jan Kara13ceef02010-07-14 07:56:33 +0200827 /* Fire data frozen trigger just before we copy the data */
828 jbd2_buffer_frozen_trigger(jh, source + offset,
829 jh->b_triggers);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700830 memcpy(jh->b_frozen_data, source+offset, jh2bh(jh)->b_size);
Cong Wang303a8f22011-11-25 23:14:31 +0800831 kunmap_atomic(source);
Joel Beckere06c8222008-09-11 15:35:47 -0700832
833 /*
834 * Now that the frozen data is saved off, we need to store
835 * any matching triggers.
836 */
837 jh->b_frozen_triggers = jh->b_triggers;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700838 }
839 jbd_unlock_bh_state(bh);
840
841 /*
842 * If we are about to journal a buffer, then any revoke pending on it is
843 * no longer valid
844 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700845 jbd2_journal_cancel_revoke(handle, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700846
847out:
848 if (unlikely(frozen_buffer)) /* It's usually NULL */
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400849 jbd2_free(frozen_buffer, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700850
851 JBUFFER_TRACE(jh, "exit");
852 return error;
853}
854
855/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700856 * int jbd2_journal_get_write_access() - notify intent to modify a buffer for metadata (not data) update.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700857 * @handle: transaction to add buffer modifications to
858 * @bh: bh to be used for metadata writes
Dave Kleikamp470decc2006-10-11 01:20:57 -0700859 *
860 * Returns an error code or 0 on success.
861 *
862 * In full data journalling mode the buffer may be of type BJ_AsyncData,
863 * because we're write()ing a buffer which is also part of a shared mapping.
864 */
865
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700866int jbd2_journal_get_write_access(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700867{
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700868 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700869 int rc;
870
871 /* We do not want to get caught playing with fields which the
872 * log thread also manipulates. Make sure that the buffer
873 * completes any outstanding IO before proceeding. */
874 rc = do_get_write_access(handle, jh, 0);
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700875 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700876 return rc;
877}
878
879
880/*
881 * When the user wants to journal a newly created buffer_head
882 * (ie. getblk() returned a new buffer and we are going to populate it
883 * manually rather than reading off disk), then we need to keep the
884 * buffer_head locked until it has been completely filled with new
885 * data. In this case, we should be able to make the assertion that
886 * the bh is not already part of an existing transaction.
887 *
888 * The buffer should already be locked by the caller by this point.
889 * There is no lock ranking violation: it was a newly created,
890 * unlocked buffer beforehand. */
891
892/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700893 * int jbd2_journal_get_create_access () - notify intent to use newly created bh
Dave Kleikamp470decc2006-10-11 01:20:57 -0700894 * @handle: transaction to new buffer to
895 * @bh: new buffer.
896 *
897 * Call this if you create a new bh.
898 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700899int jbd2_journal_get_create_access(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700900{
901 transaction_t *transaction = handle->h_transaction;
902 journal_t *journal = transaction->t_journal;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700903 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700904 int err;
905
906 jbd_debug(5, "journal_head %p\n", jh);
907 err = -EROFS;
908 if (is_handle_aborted(handle))
909 goto out;
910 err = 0;
911
912 JBUFFER_TRACE(jh, "entry");
913 /*
914 * The buffer may already belong to this transaction due to pre-zeroing
915 * in the filesystem's new_block code. It may also be on the previous,
916 * committing transaction's lists, but it HAS to be in Forget state in
917 * that case: the transaction must have deleted the buffer for it to be
918 * reused here.
919 */
920 jbd_lock_bh_state(bh);
921 spin_lock(&journal->j_list_lock);
922 J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
923 jh->b_transaction == NULL ||
924 (jh->b_transaction == journal->j_committing_transaction &&
925 jh->b_jlist == BJ_Forget)));
926
927 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
928 J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
929
930 if (jh->b_transaction == NULL) {
Jan Karaf91d1d02009-07-13 16:16:20 -0400931 /*
932 * Previous jbd2_journal_forget() could have left the buffer
933 * with jbddirty bit set because it was being committed. When
934 * the commit finished, we've filed the buffer for
935 * checkpointing and marked it dirty. Now we are reallocating
936 * the buffer so the transaction freeing it must have
937 * committed and so it's safe to clear the dirty bit.
938 */
939 clear_buffer_dirty(jh2bh(jh));
Josef Bacik9fc7c632008-04-17 10:38:59 -0400940 /* first access by this transaction */
941 jh->b_modified = 0;
942
Dave Kleikamp470decc2006-10-11 01:20:57 -0700943 JBUFFER_TRACE(jh, "file as BJ_Reserved");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700944 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700945 } else if (jh->b_transaction == journal->j_committing_transaction) {
Josef Bacik9fc7c632008-04-17 10:38:59 -0400946 /* first access by this transaction */
947 jh->b_modified = 0;
948
Dave Kleikamp470decc2006-10-11 01:20:57 -0700949 JBUFFER_TRACE(jh, "set next transaction");
950 jh->b_next_transaction = transaction;
951 }
952 spin_unlock(&journal->j_list_lock);
953 jbd_unlock_bh_state(bh);
954
955 /*
956 * akpm: I added this. ext3_alloc_branch can pick up new indirect
957 * blocks which contain freed but then revoked metadata. We need
958 * to cancel the revoke in case we end up freeing it yet again
959 * and the reallocating as data - this would cause a second revoke,
960 * which hits an assertion error.
961 */
962 JBUFFER_TRACE(jh, "cancelling revoke");
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700963 jbd2_journal_cancel_revoke(handle, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700964out:
Ding Dinghua3991b402011-05-25 17:43:48 -0400965 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700966 return err;
967}
968
969/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700970 * int jbd2_journal_get_undo_access() - Notify intent to modify metadata with
Dave Kleikamp470decc2006-10-11 01:20:57 -0700971 * non-rewindable consequences
972 * @handle: transaction
973 * @bh: buffer to undo
Dave Kleikamp470decc2006-10-11 01:20:57 -0700974 *
975 * Sometimes there is a need to distinguish between metadata which has
976 * been committed to disk and that which has not. The ext3fs code uses
977 * this for freeing and allocating space, we have to make sure that we
978 * do not reuse freed space until the deallocation has been committed,
979 * since if we overwrote that space we would make the delete
980 * un-rewindable in case of a crash.
981 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700982 * To deal with that, jbd2_journal_get_undo_access requests write access to a
Dave Kleikamp470decc2006-10-11 01:20:57 -0700983 * buffer for parts of non-rewindable operations such as delete
984 * operations on the bitmaps. The journaling code must keep a copy of
985 * the buffer's contents prior to the undo_access call until such time
986 * as we know that the buffer has definitely been committed to disk.
987 *
988 * We never need to know which transaction the committed data is part
989 * of, buffers touched here are guaranteed to be dirtied later and so
990 * will be committed to a new transaction in due course, at which point
991 * we can discard the old committed data pointer.
992 *
993 * Returns error number or 0 on success.
994 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700995int jbd2_journal_get_undo_access(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700996{
997 int err;
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700998 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700999 char *committed_data = NULL;
1000
1001 JBUFFER_TRACE(jh, "entry");
1002
1003 /*
1004 * Do this first --- it can drop the journal lock, so we want to
1005 * make sure that obtaining the committed_data is done
1006 * atomically wrt. completion of any outstanding commits.
1007 */
1008 err = do_get_write_access(handle, jh, 1);
1009 if (err)
1010 goto out;
1011
1012repeat:
1013 if (!jh->b_committed_data) {
Mingming Caoaf1e76d2007-10-16 18:38:25 -04001014 committed_data = jbd2_alloc(jh2bh(jh)->b_size, GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001015 if (!committed_data) {
1016 printk(KERN_EMERG "%s: No memory for committed data\n",
Harvey Harrison329d2912008-04-17 10:38:59 -04001017 __func__);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001018 err = -ENOMEM;
1019 goto out;
1020 }
1021 }
1022
1023 jbd_lock_bh_state(bh);
1024 if (!jh->b_committed_data) {
1025 /* Copy out the current buffer contents into the
1026 * preserved, committed copy. */
1027 JBUFFER_TRACE(jh, "generate b_committed data");
1028 if (!committed_data) {
1029 jbd_unlock_bh_state(bh);
1030 goto repeat;
1031 }
1032
1033 jh->b_committed_data = committed_data;
1034 committed_data = NULL;
1035 memcpy(jh->b_committed_data, bh->b_data, bh->b_size);
1036 }
1037 jbd_unlock_bh_state(bh);
1038out:
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001039 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001040 if (unlikely(committed_data))
Mingming Caoaf1e76d2007-10-16 18:38:25 -04001041 jbd2_free(committed_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001042 return err;
1043}
1044
1045/**
Joel Beckere06c8222008-09-11 15:35:47 -07001046 * void jbd2_journal_set_triggers() - Add triggers for commit writeout
1047 * @bh: buffer to trigger on
1048 * @type: struct jbd2_buffer_trigger_type containing the trigger(s).
1049 *
1050 * Set any triggers on this journal_head. This is always safe, because
1051 * triggers for a committing buffer will be saved off, and triggers for
1052 * a running transaction will match the buffer in that transaction.
1053 *
1054 * Call with NULL to clear the triggers.
1055 */
1056void jbd2_journal_set_triggers(struct buffer_head *bh,
1057 struct jbd2_buffer_trigger_type *type)
1058{
Jan Karaad56eda2013-03-11 13:24:56 -04001059 struct journal_head *jh = jbd2_journal_grab_journal_head(bh);
Joel Beckere06c8222008-09-11 15:35:47 -07001060
Jan Karaad56eda2013-03-11 13:24:56 -04001061 if (WARN_ON(!jh))
1062 return;
Joel Beckere06c8222008-09-11 15:35:47 -07001063 jh->b_triggers = type;
Jan Karaad56eda2013-03-11 13:24:56 -04001064 jbd2_journal_put_journal_head(jh);
Joel Beckere06c8222008-09-11 15:35:47 -07001065}
1066
Jan Kara13ceef02010-07-14 07:56:33 +02001067void jbd2_buffer_frozen_trigger(struct journal_head *jh, void *mapped_data,
Joel Beckere06c8222008-09-11 15:35:47 -07001068 struct jbd2_buffer_trigger_type *triggers)
1069{
1070 struct buffer_head *bh = jh2bh(jh);
1071
Jan Kara13ceef02010-07-14 07:56:33 +02001072 if (!triggers || !triggers->t_frozen)
Joel Beckere06c8222008-09-11 15:35:47 -07001073 return;
1074
Jan Kara13ceef02010-07-14 07:56:33 +02001075 triggers->t_frozen(triggers, bh, mapped_data, bh->b_size);
Joel Beckere06c8222008-09-11 15:35:47 -07001076}
1077
1078void jbd2_buffer_abort_trigger(struct journal_head *jh,
1079 struct jbd2_buffer_trigger_type *triggers)
1080{
1081 if (!triggers || !triggers->t_abort)
1082 return;
1083
1084 triggers->t_abort(triggers, jh2bh(jh));
1085}
1086
1087
1088
1089/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001090 * int jbd2_journal_dirty_metadata() - mark a buffer as containing dirty metadata
Dave Kleikamp470decc2006-10-11 01:20:57 -07001091 * @handle: transaction to add buffer to.
1092 * @bh: buffer to mark
1093 *
1094 * mark dirty metadata which needs to be journaled as part of the current
1095 * transaction.
1096 *
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -04001097 * The buffer must have previously had jbd2_journal_get_write_access()
1098 * called so that it has a valid journal_head attached to the buffer
1099 * head.
1100 *
Dave Kleikamp470decc2006-10-11 01:20:57 -07001101 * The buffer is placed on the transaction's metadata list and is marked
1102 * as belonging to the transaction.
1103 *
1104 * Returns error number or 0 on success.
1105 *
1106 * Special care needs to be taken if the buffer already belongs to the
1107 * current committing transaction (in which case we should have frozen
1108 * data present for that commit). In that case, we don't relink the
1109 * buffer: that only gets done when the old transaction finally
1110 * completes its commit.
1111 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001112int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001113{
1114 transaction_t *transaction = handle->h_transaction;
1115 journal_t *journal = transaction->t_journal;
Jan Karaad56eda2013-03-11 13:24:56 -04001116 struct journal_head *jh;
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -04001117 int ret = 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001118
Dave Kleikamp470decc2006-10-11 01:20:57 -07001119 if (is_handle_aborted(handle))
1120 goto out;
Jan Karaad56eda2013-03-11 13:24:56 -04001121 jh = jbd2_journal_grab_journal_head(bh);
1122 if (!jh) {
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -04001123 ret = -EUCLEAN;
1124 goto out;
1125 }
Jan Karaad56eda2013-03-11 13:24:56 -04001126 jbd_debug(5, "journal_head %p\n", jh);
1127 JBUFFER_TRACE(jh, "entry");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001128
1129 jbd_lock_bh_state(bh);
1130
1131 if (jh->b_modified == 0) {
1132 /*
1133 * This buffer's got modified and becoming part
1134 * of the transaction. This needs to be done
1135 * once a transaction -bzzz
1136 */
1137 jh->b_modified = 1;
1138 J_ASSERT_JH(jh, handle->h_buffer_credits > 0);
1139 handle->h_buffer_credits--;
1140 }
1141
1142 /*
1143 * fastpath, to avoid expensive locking. If this buffer is already
1144 * on the running transaction's metadata list there is nothing to do.
1145 * Nobody can take it off again because there is a handle open.
1146 * I _think_ we're OK here with SMP barriers - a mistaken decision will
1147 * result in this test being false, so we go in and take the locks.
1148 */
1149 if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) {
1150 JBUFFER_TRACE(jh, "fastpath");
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -04001151 if (unlikely(jh->b_transaction !=
1152 journal->j_running_transaction)) {
1153 printk(KERN_EMERG "JBD: %s: "
1154 "jh->b_transaction (%llu, %p, %u) != "
1155 "journal->j_running_transaction (%p, %u)",
1156 journal->j_devname,
1157 (unsigned long long) bh->b_blocknr,
1158 jh->b_transaction,
1159 jh->b_transaction ? jh->b_transaction->t_tid : 0,
1160 journal->j_running_transaction,
1161 journal->j_running_transaction ?
1162 journal->j_running_transaction->t_tid : 0);
1163 ret = -EINVAL;
1164 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001165 goto out_unlock_bh;
1166 }
1167
1168 set_buffer_jbddirty(bh);
1169
1170 /*
1171 * Metadata already on the current transaction list doesn't
1172 * need to be filed. Metadata on another transaction's list must
1173 * be committing, and will be refiled once the commit completes:
1174 * leave it alone for now.
1175 */
1176 if (jh->b_transaction != transaction) {
1177 JBUFFER_TRACE(jh, "already on other transaction");
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -04001178 if (unlikely(jh->b_transaction !=
1179 journal->j_committing_transaction)) {
1180 printk(KERN_EMERG "JBD: %s: "
1181 "jh->b_transaction (%llu, %p, %u) != "
1182 "journal->j_committing_transaction (%p, %u)",
1183 journal->j_devname,
1184 (unsigned long long) bh->b_blocknr,
1185 jh->b_transaction,
1186 jh->b_transaction ? jh->b_transaction->t_tid : 0,
1187 journal->j_committing_transaction,
1188 journal->j_committing_transaction ?
1189 journal->j_committing_transaction->t_tid : 0);
1190 ret = -EINVAL;
1191 }
1192 if (unlikely(jh->b_next_transaction != transaction)) {
1193 printk(KERN_EMERG "JBD: %s: "
1194 "jh->b_next_transaction (%llu, %p, %u) != "
1195 "transaction (%p, %u)",
1196 journal->j_devname,
1197 (unsigned long long) bh->b_blocknr,
1198 jh->b_next_transaction,
1199 jh->b_next_transaction ?
1200 jh->b_next_transaction->t_tid : 0,
1201 transaction, transaction->t_tid);
1202 ret = -EINVAL;
1203 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001204 /* And this case is illegal: we can't reuse another
1205 * transaction's data buffer, ever. */
1206 goto out_unlock_bh;
1207 }
1208
1209 /* That test should have eliminated the following case: */
Mingming Cao40191912008-01-28 23:58:27 -05001210 J_ASSERT_JH(jh, jh->b_frozen_data == NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001211
1212 JBUFFER_TRACE(jh, "file as BJ_Metadata");
1213 spin_lock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001214 __jbd2_journal_file_buffer(jh, handle->h_transaction, BJ_Metadata);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001215 spin_unlock(&journal->j_list_lock);
1216out_unlock_bh:
1217 jbd_unlock_bh_state(bh);
Jan Karaad56eda2013-03-11 13:24:56 -04001218 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001219out:
1220 JBUFFER_TRACE(jh, "exit");
Randy Dunlap44705752011-10-27 04:05:13 -04001221 WARN_ON(ret); /* All errors are bugs, so dump the stack */
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -04001222 return ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001223}
1224
Dave Kleikamp470decc2006-10-11 01:20:57 -07001225/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001226 * void jbd2_journal_forget() - bforget() for potentially-journaled buffers.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001227 * @handle: transaction handle
1228 * @bh: bh to 'forget'
1229 *
1230 * We can only do the bforget if there are no commits pending against the
1231 * buffer. If the buffer is dirty in the current running transaction we
1232 * can safely unlink it.
1233 *
1234 * bh may not be a journalled buffer at all - it may be a non-JBD
1235 * buffer which came off the hashtable. Check for this.
1236 *
1237 * Decrements bh->b_count by one.
1238 *
1239 * Allow this call even if the handle has aborted --- it may be part of
1240 * the caller's cleanup after an abort.
1241 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001242int jbd2_journal_forget (handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001243{
1244 transaction_t *transaction = handle->h_transaction;
1245 journal_t *journal = transaction->t_journal;
1246 struct journal_head *jh;
1247 int drop_reserve = 0;
1248 int err = 0;
Josef Bacik1dfc3222008-04-17 10:38:59 -04001249 int was_modified = 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001250
1251 BUFFER_TRACE(bh, "entry");
1252
1253 jbd_lock_bh_state(bh);
1254 spin_lock(&journal->j_list_lock);
1255
1256 if (!buffer_jbd(bh))
1257 goto not_jbd;
1258 jh = bh2jh(bh);
1259
1260 /* Critical error: attempting to delete a bitmap buffer, maybe?
1261 * Don't do any jbd operations, and return an error. */
1262 if (!J_EXPECT_JH(jh, !jh->b_committed_data,
1263 "inconsistent data on disk")) {
1264 err = -EIO;
1265 goto not_jbd;
1266 }
1267
Adam Buchbinder48fc7f72012-09-19 21:48:00 -04001268 /* keep track of whether or not this transaction modified us */
Josef Bacik1dfc3222008-04-17 10:38:59 -04001269 was_modified = jh->b_modified;
1270
Dave Kleikamp470decc2006-10-11 01:20:57 -07001271 /*
1272 * The buffer's going from the transaction, we must drop
1273 * all references -bzzz
1274 */
1275 jh->b_modified = 0;
1276
1277 if (jh->b_transaction == handle->h_transaction) {
1278 J_ASSERT_JH(jh, !jh->b_frozen_data);
1279
1280 /* If we are forgetting a buffer which is already part
1281 * of this transaction, then we can just drop it from
1282 * the transaction immediately. */
1283 clear_buffer_dirty(bh);
1284 clear_buffer_jbddirty(bh);
1285
1286 JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
1287
Josef Bacik1dfc3222008-04-17 10:38:59 -04001288 /*
1289 * we only want to drop a reference if this transaction
1290 * modified the buffer
1291 */
1292 if (was_modified)
1293 drop_reserve = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001294
1295 /*
1296 * We are no longer going to journal this buffer.
1297 * However, the commit of this transaction is still
1298 * important to the buffer: the delete that we are now
1299 * processing might obsolete an old log entry, so by
1300 * committing, we can satisfy the buffer's checkpoint.
1301 *
1302 * So, if we have a checkpoint on the buffer, we should
1303 * now refile the buffer on our BJ_Forget list so that
1304 * we know to remove the checkpoint after we commit.
1305 */
1306
1307 if (jh->b_cp_transaction) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001308 __jbd2_journal_temp_unlink_buffer(jh);
1309 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001310 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001311 __jbd2_journal_unfile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001312 if (!buffer_jbd(bh)) {
1313 spin_unlock(&journal->j_list_lock);
1314 jbd_unlock_bh_state(bh);
1315 __bforget(bh);
1316 goto drop;
1317 }
1318 }
1319 } else if (jh->b_transaction) {
1320 J_ASSERT_JH(jh, (jh->b_transaction ==
1321 journal->j_committing_transaction));
1322 /* However, if the buffer is still owned by a prior
1323 * (committing) transaction, we can't drop it yet... */
1324 JBUFFER_TRACE(jh, "belongs to older transaction");
1325 /* ... but we CAN drop it from the new transaction if we
1326 * have also modified it since the original commit. */
1327
1328 if (jh->b_next_transaction) {
1329 J_ASSERT(jh->b_next_transaction == transaction);
1330 jh->b_next_transaction = NULL;
Josef Bacik1dfc3222008-04-17 10:38:59 -04001331
1332 /*
1333 * only drop a reference if this transaction modified
1334 * the buffer
1335 */
1336 if (was_modified)
1337 drop_reserve = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001338 }
1339 }
1340
1341not_jbd:
1342 spin_unlock(&journal->j_list_lock);
1343 jbd_unlock_bh_state(bh);
1344 __brelse(bh);
1345drop:
1346 if (drop_reserve) {
1347 /* no need to reserve log space for this block -bzzz */
1348 handle->h_buffer_credits++;
1349 }
1350 return err;
1351}
1352
1353/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001354 * int jbd2_journal_stop() - complete a transaction
Dave Kleikamp470decc2006-10-11 01:20:57 -07001355 * @handle: tranaction to complete.
1356 *
1357 * All done for a particular handle.
1358 *
1359 * There is not much action needed here. We just return any remaining
1360 * buffer credits to the transaction and remove the handle. The only
1361 * complication is that we need to start a commit operation if the
1362 * filesystem is marked for synchronous update.
1363 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001364 * jbd2_journal_stop itself will not usually return an error, but it may
Dave Kleikamp470decc2006-10-11 01:20:57 -07001365 * do so in unusual circumstances. In particular, expect it to
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001366 * return -EIO if a jbd2_journal_abort has been executed since the
Dave Kleikamp470decc2006-10-11 01:20:57 -07001367 * transaction began.
1368 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001369int jbd2_journal_stop(handle_t *handle)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001370{
1371 transaction_t *transaction = handle->h_transaction;
1372 journal_t *journal = transaction->t_journal;
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001373 int err, wait_for_commit = 0;
1374 tid_t tid;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001375 pid_t pid;
1376
Dave Kleikamp470decc2006-10-11 01:20:57 -07001377 J_ASSERT(journal_current_handle() == handle);
1378
1379 if (is_handle_aborted(handle))
1380 err = -EIO;
OGAWA Hirofumi3e2a5322006-10-19 23:29:11 -07001381 else {
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001382 J_ASSERT(atomic_read(&transaction->t_updates) > 0);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001383 err = 0;
OGAWA Hirofumi3e2a5322006-10-19 23:29:11 -07001384 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001385
1386 if (--handle->h_ref > 0) {
1387 jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
1388 handle->h_ref);
1389 return err;
1390 }
1391
1392 jbd_debug(4, "Handle %p going down\n", handle);
Theodore Ts'o343d9c22013-02-08 13:00:22 -05001393 trace_jbd2_handle_stats(journal->j_fs_dev->bd_dev,
1394 handle->h_transaction->t_tid,
1395 handle->h_type, handle->h_line_no,
1396 jiffies - handle->h_start_jiffies,
1397 handle->h_sync, handle->h_requested_credits,
1398 (handle->h_requested_credits -
1399 handle->h_buffer_credits));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001400
1401 /*
1402 * Implement synchronous transaction batching. If the handle
1403 * was synchronous, don't force a commit immediately. Let's
Josef Bacike07f7182008-11-26 01:14:26 -05001404 * yield and let another thread piggyback onto this
1405 * transaction. Keep doing that while new threads continue to
1406 * arrive. It doesn't cost much - we're about to run a commit
1407 * and sleep on IO anyway. Speeds up many-threaded, many-dir
1408 * operations by 30x or more...
Dave Kleikamp470decc2006-10-11 01:20:57 -07001409 *
Josef Bacike07f7182008-11-26 01:14:26 -05001410 * We try and optimize the sleep time against what the
1411 * underlying disk can do, instead of having a static sleep
1412 * time. This is useful for the case where our storage is so
1413 * fast that it is more optimal to go ahead and force a flush
1414 * and wait for the transaction to be committed than it is to
1415 * wait for an arbitrary amount of time for new writers to
1416 * join the transaction. We achieve this by measuring how
1417 * long it takes to commit a transaction, and compare it with
1418 * how long this transaction has been running, and if run time
1419 * < commit time then we sleep for the delta and commit. This
1420 * greatly helps super fast disks that would see slowdowns as
1421 * more threads started doing fsyncs.
1422 *
1423 * But don't do this if this process was the most recent one
1424 * to perform a synchronous write. We do this to detect the
1425 * case where a single process is doing a stream of sync
1426 * writes. No point in waiting for joiners in that case.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001427 */
1428 pid = current->pid;
1429 if (handle->h_sync && journal->j_last_sync_writer != pid) {
Josef Bacike07f7182008-11-26 01:14:26 -05001430 u64 commit_time, trans_time;
1431
Dave Kleikamp470decc2006-10-11 01:20:57 -07001432 journal->j_last_sync_writer = pid;
Josef Bacike07f7182008-11-26 01:14:26 -05001433
Theodore Ts'oa931da62010-08-03 21:35:12 -04001434 read_lock(&journal->j_state_lock);
Josef Bacike07f7182008-11-26 01:14:26 -05001435 commit_time = journal->j_average_commit_time;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001436 read_unlock(&journal->j_state_lock);
Josef Bacike07f7182008-11-26 01:14:26 -05001437
1438 trans_time = ktime_to_ns(ktime_sub(ktime_get(),
1439 transaction->t_start_time));
1440
Theodore Ts'o30773842009-01-03 20:27:38 -05001441 commit_time = max_t(u64, commit_time,
1442 1000*journal->j_min_batch_time);
Josef Bacike07f7182008-11-26 01:14:26 -05001443 commit_time = min_t(u64, commit_time,
Theodore Ts'o30773842009-01-03 20:27:38 -05001444 1000*journal->j_max_batch_time);
Josef Bacike07f7182008-11-26 01:14:26 -05001445
1446 if (trans_time < commit_time) {
1447 ktime_t expires = ktime_add_ns(ktime_get(),
1448 commit_time);
1449 set_current_state(TASK_UNINTERRUPTIBLE);
1450 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1451 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001452 }
1453
Theodore Ts'o70585482009-03-25 23:35:46 -04001454 if (handle->h_sync)
1455 transaction->t_synchronous_commit = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001456 current->journal_info = NULL;
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001457 atomic_sub(handle->h_buffer_credits,
1458 &transaction->t_outstanding_credits);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001459
1460 /*
1461 * If the handle is marked SYNC, we need to set another commit
1462 * going! We also want to force a commit if the current
1463 * transaction is occupying too much of the log, or if the
1464 * transaction is too old now.
1465 */
1466 if (handle->h_sync ||
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001467 (atomic_read(&transaction->t_outstanding_credits) >
1468 journal->j_max_transaction_buffers) ||
1469 time_after_eq(jiffies, transaction->t_expires)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001470 /* Do this even for aborted journals: an abort still
1471 * completes the commit thread, it just doesn't write
1472 * anything to disk. */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001473
Dave Kleikamp470decc2006-10-11 01:20:57 -07001474 jbd_debug(2, "transaction too old, requesting commit for "
1475 "handle %p\n", handle);
1476 /* This is non-blocking */
Theodore Ts'oc35a56a2010-05-16 05:00:00 -04001477 jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001478
1479 /*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001480 * Special case: JBD2_SYNC synchronous updates require us
Dave Kleikamp470decc2006-10-11 01:20:57 -07001481 * to wait for the commit to complete.
1482 */
1483 if (handle->h_sync && !(current->flags & PF_MEMALLOC))
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001484 wait_for_commit = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001485 }
1486
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001487 /*
1488 * Once we drop t_updates, if it goes to zero the transaction
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001489 * could start committing on us and eventually disappear. So
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001490 * once we do this, we must not dereference transaction
1491 * pointer again.
1492 */
1493 tid = transaction->t_tid;
1494 if (atomic_dec_and_test(&transaction->t_updates)) {
1495 wake_up(&journal->j_wait_updates);
1496 if (journal->j_barrier_count)
1497 wake_up(&journal->j_wait_transaction_locked);
1498 }
1499
1500 if (wait_for_commit)
1501 err = jbd2_log_wait_commit(journal, tid);
1502
Ingo Molnar3295f0e2008-08-11 10:30:30 +02001503 lock_map_release(&handle->h_lockdep_map);
Mingming Cao7b751062008-01-28 23:58:27 -05001504
Mingming Caoaf1e76d2007-10-16 18:38:25 -04001505 jbd2_free_handle(handle);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001506 return err;
1507}
1508
Randy Dunlap5648ba52008-04-17 10:38:59 -04001509/**
1510 * int jbd2_journal_force_commit() - force any uncommitted transactions
Dave Kleikamp470decc2006-10-11 01:20:57 -07001511 * @journal: journal to force
1512 *
1513 * For synchronous operations: force any uncommitted transactions
1514 * to disk. May seem kludgy, but it reuses all the handle batching
1515 * code in a very simple manner.
1516 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001517int jbd2_journal_force_commit(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001518{
1519 handle_t *handle;
1520 int ret;
1521
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001522 handle = jbd2_journal_start(journal, 1);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001523 if (IS_ERR(handle)) {
1524 ret = PTR_ERR(handle);
1525 } else {
1526 handle->h_sync = 1;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001527 ret = jbd2_journal_stop(handle);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001528 }
1529 return ret;
1530}
1531
1532/*
1533 *
1534 * List management code snippets: various functions for manipulating the
1535 * transaction buffer lists.
1536 *
1537 */
1538
1539/*
1540 * Append a buffer to a transaction list, given the transaction's list head
1541 * pointer.
1542 *
1543 * j_list_lock is held.
1544 *
1545 * jbd_lock_bh_state(jh2bh(jh)) is held.
1546 */
1547
1548static inline void
1549__blist_add_buffer(struct journal_head **list, struct journal_head *jh)
1550{
1551 if (!*list) {
1552 jh->b_tnext = jh->b_tprev = jh;
1553 *list = jh;
1554 } else {
1555 /* Insert at the tail of the list to preserve order */
1556 struct journal_head *first = *list, *last = first->b_tprev;
1557 jh->b_tprev = last;
1558 jh->b_tnext = first;
1559 last->b_tnext = first->b_tprev = jh;
1560 }
1561}
1562
1563/*
1564 * Remove a buffer from a transaction list, given the transaction's list
1565 * head pointer.
1566 *
1567 * Called with j_list_lock held, and the journal may not be locked.
1568 *
1569 * jbd_lock_bh_state(jh2bh(jh)) is held.
1570 */
1571
1572static inline void
1573__blist_del_buffer(struct journal_head **list, struct journal_head *jh)
1574{
1575 if (*list == jh) {
1576 *list = jh->b_tnext;
1577 if (*list == jh)
1578 *list = NULL;
1579 }
1580 jh->b_tprev->b_tnext = jh->b_tnext;
1581 jh->b_tnext->b_tprev = jh->b_tprev;
1582}
1583
1584/*
1585 * Remove a buffer from the appropriate transaction list.
1586 *
1587 * Note that this function can *change* the value of
Jan Karaf5113ef2013-06-04 12:01:45 -04001588 * bh->b_transaction->t_buffers, t_forget, t_shadow_list, t_log_list or
1589 * t_reserved_list. If the caller is holding onto a copy of one of these
1590 * pointers, it could go bad. Generally the caller needs to re-read the
1591 * pointer from the transaction_t.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001592 *
Jan Kara5bebccf2012-03-13 22:25:06 -04001593 * Called under j_list_lock.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001594 */
Jan Kara5bebccf2012-03-13 22:25:06 -04001595static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001596{
1597 struct journal_head **list = NULL;
1598 transaction_t *transaction;
1599 struct buffer_head *bh = jh2bh(jh);
1600
1601 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1602 transaction = jh->b_transaction;
1603 if (transaction)
1604 assert_spin_locked(&transaction->t_journal->j_list_lock);
1605
1606 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1607 if (jh->b_jlist != BJ_None)
Mingming Cao40191912008-01-28 23:58:27 -05001608 J_ASSERT_JH(jh, transaction != NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001609
1610 switch (jh->b_jlist) {
1611 case BJ_None:
1612 return;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001613 case BJ_Metadata:
1614 transaction->t_nr_buffers--;
1615 J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
1616 list = &transaction->t_buffers;
1617 break;
1618 case BJ_Forget:
1619 list = &transaction->t_forget;
1620 break;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001621 case BJ_Shadow:
1622 list = &transaction->t_shadow_list;
1623 break;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001624 case BJ_Reserved:
1625 list = &transaction->t_reserved_list;
1626 break;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001627 }
1628
1629 __blist_del_buffer(list, jh);
1630 jh->b_jlist = BJ_None;
1631 if (test_clear_buffer_jbddirty(bh))
1632 mark_buffer_dirty(bh); /* Expose it to the VM */
1633}
1634
Jan Karade1b7942011-06-13 15:38:22 -04001635/*
1636 * Remove buffer from all transactions.
1637 *
1638 * Called with bh_state lock and j_list_lock
1639 *
1640 * jh and bh may be already freed when this function returns.
1641 */
1642static void __jbd2_journal_unfile_buffer(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001643{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001644 __jbd2_journal_temp_unlink_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001645 jh->b_transaction = NULL;
Jan Karade1b7942011-06-13 15:38:22 -04001646 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001647}
1648
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001649void jbd2_journal_unfile_buffer(journal_t *journal, struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001650{
Jan Karade1b7942011-06-13 15:38:22 -04001651 struct buffer_head *bh = jh2bh(jh);
1652
1653 /* Get reference so that buffer cannot be freed before we unlock it */
1654 get_bh(bh);
1655 jbd_lock_bh_state(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001656 spin_lock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001657 __jbd2_journal_unfile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001658 spin_unlock(&journal->j_list_lock);
Jan Karade1b7942011-06-13 15:38:22 -04001659 jbd_unlock_bh_state(bh);
1660 __brelse(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001661}
1662
1663/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001664 * Called from jbd2_journal_try_to_free_buffers().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001665 *
1666 * Called under jbd_lock_bh_state(bh)
1667 */
1668static void
1669__journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh)
1670{
1671 struct journal_head *jh;
1672
1673 jh = bh2jh(bh);
1674
1675 if (buffer_locked(bh) || buffer_dirty(bh))
1676 goto out;
1677
Mingming Cao40191912008-01-28 23:58:27 -05001678 if (jh->b_next_transaction != NULL)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001679 goto out;
1680
1681 spin_lock(&journal->j_list_lock);
Jan Kara87c89c22008-07-11 19:27:31 -04001682 if (jh->b_cp_transaction != NULL && jh->b_transaction == NULL) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001683 /* written-back checkpointed metadata buffer */
Jan Karac254c9e2012-03-13 22:27:44 -04001684 JBUFFER_TRACE(jh, "remove from checkpoint list");
1685 __jbd2_journal_remove_checkpoint(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001686 }
1687 spin_unlock(&journal->j_list_lock);
1688out:
1689 return;
1690}
1691
Dave Kleikamp470decc2006-10-11 01:20:57 -07001692/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001693 * int jbd2_journal_try_to_free_buffers() - try to free page buffers.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001694 * @journal: journal for operation
1695 * @page: to try and free
Mingming Cao530576b2008-07-13 21:06:39 -04001696 * @gfp_mask: we use the mask to detect how hard should we try to release
1697 * buffers. If __GFP_WAIT and __GFP_FS is set, we wait for commit code to
1698 * release the buffers.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001699 *
1700 *
1701 * For all the buffers on this page,
1702 * if they are fully written out ordered data, move them onto BUF_CLEAN
1703 * so try_to_free_buffers() can reap them.
1704 *
1705 * This function returns non-zero if we wish try_to_free_buffers()
1706 * to be called. We do this if the page is releasable by try_to_free_buffers().
1707 * We also do it if the page has locked or dirty buffers and the caller wants
1708 * us to perform sync or async writeout.
1709 *
1710 * This complicates JBD locking somewhat. We aren't protected by the
1711 * BKL here. We wish to remove the buffer from its committing or
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001712 * running transaction's ->t_datalist via __jbd2_journal_unfile_buffer.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001713 *
1714 * This may *change* the value of transaction_t->t_datalist, so anyone
1715 * who looks at t_datalist needs to lock against this function.
1716 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001717 * Even worse, someone may be doing a jbd2_journal_dirty_data on this
1718 * buffer. So we need to lock against that. jbd2_journal_dirty_data()
Dave Kleikamp470decc2006-10-11 01:20:57 -07001719 * will come out of the lock with the buffer dirty, which makes it
1720 * ineligible for release here.
1721 *
1722 * Who else is affected by this? hmm... Really the only contender
1723 * is do_get_write_access() - it could be looking at the buffer while
1724 * journal_try_to_free_buffer() is changing its state. But that
1725 * cannot happen because we never reallocate freed data as metadata
1726 * while the data is part of a transaction. Yes?
Mingming Cao530576b2008-07-13 21:06:39 -04001727 *
1728 * Return 0 on failure, 1 on success
Dave Kleikamp470decc2006-10-11 01:20:57 -07001729 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001730int jbd2_journal_try_to_free_buffers(journal_t *journal,
Mingming Cao530576b2008-07-13 21:06:39 -04001731 struct page *page, gfp_t gfp_mask)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001732{
1733 struct buffer_head *head;
1734 struct buffer_head *bh;
1735 int ret = 0;
1736
1737 J_ASSERT(PageLocked(page));
1738
1739 head = page_buffers(page);
1740 bh = head;
1741 do {
1742 struct journal_head *jh;
1743
1744 /*
1745 * We take our own ref against the journal_head here to avoid
1746 * having to add tons of locking around each instance of
Mingming Cao530576b2008-07-13 21:06:39 -04001747 * jbd2_journal_put_journal_head().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001748 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001749 jh = jbd2_journal_grab_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001750 if (!jh)
1751 continue;
1752
1753 jbd_lock_bh_state(bh);
1754 __journal_try_to_free_buffer(journal, bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001755 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001756 jbd_unlock_bh_state(bh);
1757 if (buffer_jbd(bh))
1758 goto busy;
1759 } while ((bh = bh->b_this_page) != head);
Mingming Cao530576b2008-07-13 21:06:39 -04001760
Dave Kleikamp470decc2006-10-11 01:20:57 -07001761 ret = try_to_free_buffers(page);
Mingming Cao530576b2008-07-13 21:06:39 -04001762
Dave Kleikamp470decc2006-10-11 01:20:57 -07001763busy:
1764 return ret;
1765}
1766
1767/*
1768 * This buffer is no longer needed. If it is on an older transaction's
1769 * checkpoint list we need to record it on this transaction's forget list
1770 * to pin this buffer (and hence its checkpointing transaction) down until
1771 * this transaction commits. If the buffer isn't on a checkpoint list, we
1772 * release it.
1773 * Returns non-zero if JBD no longer has an interest in the buffer.
1774 *
1775 * Called under j_list_lock.
1776 *
1777 * Called under jbd_lock_bh_state(bh).
1778 */
1779static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
1780{
1781 int may_free = 1;
1782 struct buffer_head *bh = jh2bh(jh);
1783
Dave Kleikamp470decc2006-10-11 01:20:57 -07001784 if (jh->b_cp_transaction) {
1785 JBUFFER_TRACE(jh, "on running+cp transaction");
Jan Karade1b7942011-06-13 15:38:22 -04001786 __jbd2_journal_temp_unlink_buffer(jh);
Jan Karaf91d1d02009-07-13 16:16:20 -04001787 /*
1788 * We don't want to write the buffer anymore, clear the
1789 * bit so that we don't confuse checks in
1790 * __journal_file_buffer
1791 */
1792 clear_buffer_dirty(bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001793 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001794 may_free = 0;
1795 } else {
1796 JBUFFER_TRACE(jh, "on running transaction");
Jan Karade1b7942011-06-13 15:38:22 -04001797 __jbd2_journal_unfile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001798 }
1799 return may_free;
1800}
1801
1802/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001803 * jbd2_journal_invalidatepage
Dave Kleikamp470decc2006-10-11 01:20:57 -07001804 *
1805 * This code is tricky. It has a number of cases to deal with.
1806 *
1807 * There are two invariants which this code relies on:
1808 *
1809 * i_size must be updated on disk before we start calling invalidatepage on the
1810 * data.
1811 *
1812 * This is done in ext3 by defining an ext3_setattr method which
1813 * updates i_size before truncate gets going. By maintaining this
1814 * invariant, we can be sure that it is safe to throw away any buffers
1815 * attached to the current transaction: once the transaction commits,
1816 * we know that the data will not be needed.
1817 *
1818 * Note however that we can *not* throw away data belonging to the
1819 * previous, committing transaction!
1820 *
1821 * Any disk blocks which *are* part of the previous, committing
1822 * transaction (and which therefore cannot be discarded immediately) are
1823 * not going to be reused in the new running transaction
1824 *
1825 * The bitmap committed_data images guarantee this: any block which is
1826 * allocated in one transaction and removed in the next will be marked
1827 * as in-use in the committed_data bitmap, so cannot be reused until
1828 * the next transaction to delete the block commits. This means that
1829 * leaving committing buffers dirty is quite safe: the disk blocks
1830 * cannot be reallocated to a different file and so buffer aliasing is
1831 * not possible.
1832 *
1833 *
1834 * The above applies mainly to ordered data mode. In writeback mode we
1835 * don't make guarantees about the order in which data hits disk --- in
1836 * particular we don't guarantee that new dirty data is flushed before
1837 * transaction commit --- so it is always safe just to discard data
1838 * immediately in that mode. --sct
1839 */
1840
1841/*
1842 * The journal_unmap_buffer helper function returns zero if the buffer
1843 * concerned remains pinned as an anonymous buffer belonging to an older
1844 * transaction.
1845 *
1846 * We're outside-transaction here. Either or both of j_running_transaction
1847 * and j_committing_transaction may be NULL.
1848 */
Jan Karab794e7a2012-09-26 23:11:13 -04001849static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh,
1850 int partial_page)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001851{
1852 transaction_t *transaction;
1853 struct journal_head *jh;
1854 int may_free = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001855
1856 BUFFER_TRACE(bh, "entry");
1857
1858 /*
1859 * It is safe to proceed here without the j_list_lock because the
1860 * buffers cannot be stolen by try_to_free_buffers as long as we are
1861 * holding the page lock. --sct
1862 */
1863
1864 if (!buffer_jbd(bh))
1865 goto zap_buffer_unlocked;
1866
Jan Kara87c89c22008-07-11 19:27:31 -04001867 /* OK, we have data buffer in journaled mode */
Theodore Ts'oa931da62010-08-03 21:35:12 -04001868 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001869 jbd_lock_bh_state(bh);
1870 spin_lock(&journal->j_list_lock);
1871
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001872 jh = jbd2_journal_grab_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001873 if (!jh)
1874 goto zap_buffer_no_jh;
1875
dingdinghuaba869022010-02-15 16:35:42 -05001876 /*
1877 * We cannot remove the buffer from checkpoint lists until the
1878 * transaction adding inode to orphan list (let's call it T)
1879 * is committed. Otherwise if the transaction changing the
1880 * buffer would be cleaned from the journal before T is
1881 * committed, a crash will cause that the correct contents of
1882 * the buffer will be lost. On the other hand we have to
1883 * clear the buffer dirty bit at latest at the moment when the
1884 * transaction marking the buffer as freed in the filesystem
1885 * structures is committed because from that moment on the
Jan Karab794e7a2012-09-26 23:11:13 -04001886 * block can be reallocated and used by a different page.
dingdinghuaba869022010-02-15 16:35:42 -05001887 * Since the block hasn't been freed yet but the inode has
1888 * already been added to orphan list, it is safe for us to add
1889 * the buffer to BJ_Forget list of the newest transaction.
Jan Karab794e7a2012-09-26 23:11:13 -04001890 *
1891 * Also we have to clear buffer_mapped flag of a truncated buffer
1892 * because the buffer_head may be attached to the page straddling
1893 * i_size (can happen only when blocksize < pagesize) and thus the
1894 * buffer_head can be reused when the file is extended again. So we end
1895 * up keeping around invalidated buffers attached to transactions'
1896 * BJ_Forget list just to stop checkpointing code from cleaning up
1897 * the transaction this buffer was modified in.
dingdinghuaba869022010-02-15 16:35:42 -05001898 */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001899 transaction = jh->b_transaction;
1900 if (transaction == NULL) {
1901 /* First case: not on any transaction. If it
1902 * has no checkpoint link, then we can zap it:
1903 * it's a writeback-mode buffer so we don't care
1904 * if it hits disk safely. */
1905 if (!jh->b_cp_transaction) {
1906 JBUFFER_TRACE(jh, "not on any transaction: zap");
1907 goto zap_buffer;
1908 }
1909
1910 if (!buffer_dirty(bh)) {
1911 /* bdflush has written it. We can drop it now */
1912 goto zap_buffer;
1913 }
1914
1915 /* OK, it must be in the journal but still not
1916 * written fully to disk: it's metadata or
1917 * journaled data... */
1918
1919 if (journal->j_running_transaction) {
1920 /* ... and once the current transaction has
1921 * committed, the buffer won't be needed any
1922 * longer. */
1923 JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
Jan Karab794e7a2012-09-26 23:11:13 -04001924 may_free = __dispose_buffer(jh,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001925 journal->j_running_transaction);
Jan Karab794e7a2012-09-26 23:11:13 -04001926 goto zap_buffer;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001927 } else {
1928 /* There is no currently-running transaction. So the
1929 * orphan record which we wrote for this file must have
1930 * passed into commit. We must attach this buffer to
1931 * the committing transaction, if it exists. */
1932 if (journal->j_committing_transaction) {
1933 JBUFFER_TRACE(jh, "give to committing trans");
Jan Karab794e7a2012-09-26 23:11:13 -04001934 may_free = __dispose_buffer(jh,
Dave Kleikamp470decc2006-10-11 01:20:57 -07001935 journal->j_committing_transaction);
Jan Karab794e7a2012-09-26 23:11:13 -04001936 goto zap_buffer;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001937 } else {
1938 /* The orphan record's transaction has
1939 * committed. We can cleanse this buffer */
1940 clear_buffer_jbddirty(bh);
1941 goto zap_buffer;
1942 }
1943 }
1944 } else if (transaction == journal->j_committing_transaction) {
Eric Sandeen9b579882006-10-28 10:38:28 -07001945 JBUFFER_TRACE(jh, "on committing transaction");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001946 /*
dingdinghuaba869022010-02-15 16:35:42 -05001947 * The buffer is committing, we simply cannot touch
Jan Karab794e7a2012-09-26 23:11:13 -04001948 * it. If the page is straddling i_size we have to wait
1949 * for commit and try again.
1950 */
1951 if (partial_page) {
Jan Karab794e7a2012-09-26 23:11:13 -04001952 jbd2_journal_put_journal_head(jh);
1953 spin_unlock(&journal->j_list_lock);
1954 jbd_unlock_bh_state(bh);
1955 write_unlock(&journal->j_state_lock);
Jan Kara53e87262012-12-25 13:29:52 -05001956 return -EBUSY;
Jan Karab794e7a2012-09-26 23:11:13 -04001957 }
1958 /*
1959 * OK, buffer won't be reachable after truncate. We just set
1960 * j_next_transaction to the running transaction (if there is
1961 * one) and mark buffer as freed so that commit code knows it
1962 * should clear dirty bits when it is done with the buffer.
dingdinghuaba869022010-02-15 16:35:42 -05001963 */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001964 set_buffer_freed(bh);
dingdinghuaba869022010-02-15 16:35:42 -05001965 if (journal->j_running_transaction && buffer_jbddirty(bh))
1966 jh->b_next_transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001967 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001968 spin_unlock(&journal->j_list_lock);
1969 jbd_unlock_bh_state(bh);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001970 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001971 return 0;
1972 } else {
1973 /* Good, the buffer belongs to the running transaction.
1974 * We are writing our own transaction's data, not any
1975 * previous one's, so it is safe to throw it away
1976 * (remember that we expect the filesystem to have set
1977 * i_size already for this truncate so recovery will not
1978 * expose the disk blocks we are discarding here.) */
1979 J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
Eric Sandeen9b579882006-10-28 10:38:28 -07001980 JBUFFER_TRACE(jh, "on running transaction");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001981 may_free = __dispose_buffer(jh, transaction);
1982 }
1983
1984zap_buffer:
Jan Karab794e7a2012-09-26 23:11:13 -04001985 /*
1986 * This is tricky. Although the buffer is truncated, it may be reused
1987 * if blocksize < pagesize and it is attached to the page straddling
1988 * EOF. Since the buffer might have been added to BJ_Forget list of the
1989 * running transaction, journal_get_write_access() won't clear
1990 * b_modified and credit accounting gets confused. So clear b_modified
1991 * here.
1992 */
1993 jh->b_modified = 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001994 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001995zap_buffer_no_jh:
1996 spin_unlock(&journal->j_list_lock);
1997 jbd_unlock_bh_state(bh);
Theodore Ts'oa931da62010-08-03 21:35:12 -04001998 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001999zap_buffer_unlocked:
2000 clear_buffer_dirty(bh);
2001 J_ASSERT_BH(bh, !buffer_jbddirty(bh));
2002 clear_buffer_mapped(bh);
2003 clear_buffer_req(bh);
2004 clear_buffer_new(bh);
Eric Sandeen15291162012-02-20 17:53:01 -05002005 clear_buffer_delay(bh);
2006 clear_buffer_unwritten(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002007 bh->b_bdev = NULL;
2008 return may_free;
2009}
2010
2011/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002012 * void jbd2_journal_invalidatepage()
Dave Kleikamp470decc2006-10-11 01:20:57 -07002013 * @journal: journal to use for flush...
2014 * @page: page to flush
Lukas Czerner259709b2013-05-21 23:20:03 -04002015 * @offset: start of the range to invalidate
2016 * @length: length of the range to invalidate
Dave Kleikamp470decc2006-10-11 01:20:57 -07002017 *
Lukas Czerner259709b2013-05-21 23:20:03 -04002018 * Reap page buffers containing data after in the specified range in page.
2019 * Can return -EBUSY if buffers are part of the committing transaction and
2020 * the page is straddling i_size. Caller then has to wait for current commit
2021 * and try again.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002022 */
Jan Kara53e87262012-12-25 13:29:52 -05002023int jbd2_journal_invalidatepage(journal_t *journal,
2024 struct page *page,
Lukas Czerner259709b2013-05-21 23:20:03 -04002025 unsigned int offset,
2026 unsigned int length)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002027{
2028 struct buffer_head *head, *bh, *next;
Lukas Czerner259709b2013-05-21 23:20:03 -04002029 unsigned int stop = offset + length;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002030 unsigned int curr_off = 0;
Lukas Czerner259709b2013-05-21 23:20:03 -04002031 int partial_page = (offset || length < PAGE_CACHE_SIZE);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002032 int may_free = 1;
Jan Kara53e87262012-12-25 13:29:52 -05002033 int ret = 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002034
2035 if (!PageLocked(page))
2036 BUG();
2037 if (!page_has_buffers(page))
Jan Kara53e87262012-12-25 13:29:52 -05002038 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002039
Lukas Czerner259709b2013-05-21 23:20:03 -04002040 BUG_ON(stop > PAGE_CACHE_SIZE || stop < length);
2041
Dave Kleikamp470decc2006-10-11 01:20:57 -07002042 /* We will potentially be playing with lists other than just the
2043 * data lists (especially for journaled data mode), so be
2044 * cautious in our locking. */
2045
2046 head = bh = page_buffers(page);
2047 do {
2048 unsigned int next_off = curr_off + bh->b_size;
2049 next = bh->b_this_page;
2050
Lukas Czerner259709b2013-05-21 23:20:03 -04002051 if (next_off > stop)
2052 return 0;
2053
Dave Kleikamp470decc2006-10-11 01:20:57 -07002054 if (offset <= curr_off) {
2055 /* This block is wholly outside the truncation point */
2056 lock_buffer(bh);
Lukas Czerner259709b2013-05-21 23:20:03 -04002057 ret = journal_unmap_buffer(journal, bh, partial_page);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002058 unlock_buffer(bh);
Jan Kara53e87262012-12-25 13:29:52 -05002059 if (ret < 0)
2060 return ret;
2061 may_free &= ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002062 }
2063 curr_off = next_off;
2064 bh = next;
2065
2066 } while (bh != head);
2067
Lukas Czerner259709b2013-05-21 23:20:03 -04002068 if (!partial_page) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002069 if (may_free && try_to_free_buffers(page))
2070 J_ASSERT(!page_has_buffers(page));
2071 }
Jan Kara53e87262012-12-25 13:29:52 -05002072 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002073}
2074
2075/*
2076 * File a buffer on the given transaction list.
2077 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002078void __jbd2_journal_file_buffer(struct journal_head *jh,
Dave Kleikamp470decc2006-10-11 01:20:57 -07002079 transaction_t *transaction, int jlist)
2080{
2081 struct journal_head **list = NULL;
2082 int was_dirty = 0;
2083 struct buffer_head *bh = jh2bh(jh);
2084
2085 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
2086 assert_spin_locked(&transaction->t_journal->j_list_lock);
2087
2088 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
2089 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
Mingming Cao40191912008-01-28 23:58:27 -05002090 jh->b_transaction == NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002091
2092 if (jh->b_transaction && jh->b_jlist == jlist)
2093 return;
2094
Dave Kleikamp470decc2006-10-11 01:20:57 -07002095 if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
2096 jlist == BJ_Shadow || jlist == BJ_Forget) {
Jan Karaf91d1d02009-07-13 16:16:20 -04002097 /*
2098 * For metadata buffers, we track dirty bit in buffer_jbddirty
2099 * instead of buffer_dirty. We should not see a dirty bit set
2100 * here because we clear it in do_get_write_access but e.g.
2101 * tune2fs can modify the sb and set the dirty bit at any time
2102 * so we try to gracefully handle that.
2103 */
2104 if (buffer_dirty(bh))
2105 warn_dirty_buffer(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002106 if (test_clear_buffer_dirty(bh) ||
2107 test_clear_buffer_jbddirty(bh))
2108 was_dirty = 1;
2109 }
2110
2111 if (jh->b_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002112 __jbd2_journal_temp_unlink_buffer(jh);
Jan Karade1b7942011-06-13 15:38:22 -04002113 else
2114 jbd2_journal_grab_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002115 jh->b_transaction = transaction;
2116
2117 switch (jlist) {
2118 case BJ_None:
2119 J_ASSERT_JH(jh, !jh->b_committed_data);
2120 J_ASSERT_JH(jh, !jh->b_frozen_data);
2121 return;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002122 case BJ_Metadata:
2123 transaction->t_nr_buffers++;
2124 list = &transaction->t_buffers;
2125 break;
2126 case BJ_Forget:
2127 list = &transaction->t_forget;
2128 break;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002129 case BJ_Shadow:
2130 list = &transaction->t_shadow_list;
2131 break;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002132 case BJ_Reserved:
2133 list = &transaction->t_reserved_list;
2134 break;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002135 }
2136
2137 __blist_add_buffer(list, jh);
2138 jh->b_jlist = jlist;
2139
2140 if (was_dirty)
2141 set_buffer_jbddirty(bh);
2142}
2143
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002144void jbd2_journal_file_buffer(struct journal_head *jh,
Dave Kleikamp470decc2006-10-11 01:20:57 -07002145 transaction_t *transaction, int jlist)
2146{
2147 jbd_lock_bh_state(jh2bh(jh));
2148 spin_lock(&transaction->t_journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002149 __jbd2_journal_file_buffer(jh, transaction, jlist);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002150 spin_unlock(&transaction->t_journal->j_list_lock);
2151 jbd_unlock_bh_state(jh2bh(jh));
2152}
2153
2154/*
2155 * Remove a buffer from its current buffer list in preparation for
2156 * dropping it from its current transaction entirely. If the buffer has
2157 * already started to be used by a subsequent transaction, refile the
2158 * buffer on that transaction's metadata list.
2159 *
Jan Karade1b7942011-06-13 15:38:22 -04002160 * Called under j_list_lock
Dave Kleikamp470decc2006-10-11 01:20:57 -07002161 * Called under jbd_lock_bh_state(jh2bh(jh))
Jan Karade1b7942011-06-13 15:38:22 -04002162 *
2163 * jh and bh may be already free when this function returns
Dave Kleikamp470decc2006-10-11 01:20:57 -07002164 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002165void __jbd2_journal_refile_buffer(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002166{
dingdinghuaba869022010-02-15 16:35:42 -05002167 int was_dirty, jlist;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002168 struct buffer_head *bh = jh2bh(jh);
2169
2170 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
2171 if (jh->b_transaction)
2172 assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
2173
2174 /* If the buffer is now unused, just drop it. */
2175 if (jh->b_next_transaction == NULL) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002176 __jbd2_journal_unfile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002177 return;
2178 }
2179
2180 /*
2181 * It has been modified by a later transaction: add it to the new
2182 * transaction's metadata list.
2183 */
2184
2185 was_dirty = test_clear_buffer_jbddirty(bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002186 __jbd2_journal_temp_unlink_buffer(jh);
Jan Karade1b7942011-06-13 15:38:22 -04002187 /*
2188 * We set b_transaction here because b_next_transaction will inherit
2189 * our jh reference and thus __jbd2_journal_file_buffer() must not
2190 * take a new one.
2191 */
Dave Kleikamp470decc2006-10-11 01:20:57 -07002192 jh->b_transaction = jh->b_next_transaction;
2193 jh->b_next_transaction = NULL;
dingdinghuaba869022010-02-15 16:35:42 -05002194 if (buffer_freed(bh))
2195 jlist = BJ_Forget;
2196 else if (jh->b_modified)
2197 jlist = BJ_Metadata;
2198 else
2199 jlist = BJ_Reserved;
2200 __jbd2_journal_file_buffer(jh, jh->b_transaction, jlist);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002201 J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
2202
2203 if (was_dirty)
2204 set_buffer_jbddirty(bh);
2205}
2206
2207/*
Jan Karade1b7942011-06-13 15:38:22 -04002208 * __jbd2_journal_refile_buffer() with necessary locking added. We take our
2209 * bh reference so that we can safely unlock bh.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002210 *
Jan Karade1b7942011-06-13 15:38:22 -04002211 * The jh and bh may be freed by this call.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002212 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002213void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002214{
2215 struct buffer_head *bh = jh2bh(jh);
2216
Jan Karade1b7942011-06-13 15:38:22 -04002217 /* Get reference so that buffer cannot be freed before we unlock it */
2218 get_bh(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002219 jbd_lock_bh_state(bh);
2220 spin_lock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002221 __jbd2_journal_refile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002222 jbd_unlock_bh_state(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002223 spin_unlock(&journal->j_list_lock);
2224 __brelse(bh);
2225}
Jan Karac851ed52008-07-11 19:27:31 -04002226
2227/*
2228 * File inode in the inode list of the handle's transaction
2229 */
2230int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode)
2231{
2232 transaction_t *transaction = handle->h_transaction;
2233 journal_t *journal = transaction->t_journal;
2234
2235 if (is_handle_aborted(handle))
2236 return -EIO;
2237
2238 jbd_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino,
2239 transaction->t_tid);
2240
2241 /*
2242 * First check whether inode isn't already on the transaction's
2243 * lists without taking the lock. Note that this check is safe
2244 * without the lock as we cannot race with somebody removing inode
2245 * from the transaction. The reason is that we remove inode from the
2246 * transaction only in journal_release_jbd_inode() and when we commit
2247 * the transaction. We are guarded from the first case by holding
2248 * a reference to the inode. We are safe against the second case
2249 * because if jinode->i_transaction == transaction, commit code
2250 * cannot touch the transaction because we hold reference to it,
2251 * and if jinode->i_next_transaction == transaction, commit code
2252 * will only file the inode where we want it.
2253 */
2254 if (jinode->i_transaction == transaction ||
2255 jinode->i_next_transaction == transaction)
2256 return 0;
2257
2258 spin_lock(&journal->j_list_lock);
2259
2260 if (jinode->i_transaction == transaction ||
2261 jinode->i_next_transaction == transaction)
2262 goto done;
2263
Jan Kara81be12c2011-05-24 11:52:40 -04002264 /*
2265 * We only ever set this variable to 1 so the test is safe. Since
2266 * t_need_data_flush is likely to be set, we do the test to save some
2267 * cacheline bouncing
2268 */
2269 if (!transaction->t_need_data_flush)
2270 transaction->t_need_data_flush = 1;
Jan Karac851ed52008-07-11 19:27:31 -04002271 /* On some different transaction's list - should be
2272 * the committing one */
2273 if (jinode->i_transaction) {
2274 J_ASSERT(jinode->i_next_transaction == NULL);
2275 J_ASSERT(jinode->i_transaction ==
2276 journal->j_committing_transaction);
2277 jinode->i_next_transaction = transaction;
2278 goto done;
2279 }
2280 /* Not on any transaction list... */
2281 J_ASSERT(!jinode->i_next_transaction);
2282 jinode->i_transaction = transaction;
2283 list_add(&jinode->i_list, &transaction->t_inode_list);
2284done:
2285 spin_unlock(&journal->j_list_lock);
2286
2287 return 0;
2288}
2289
2290/*
Jan Kara7f5aa212009-02-10 11:15:34 -05002291 * File truncate and transaction commit interact with each other in a
2292 * non-trivial way. If a transaction writing data block A is
2293 * committing, we cannot discard the data by truncate until we have
2294 * written them. Otherwise if we crashed after the transaction with
2295 * write has committed but before the transaction with truncate has
2296 * committed, we could see stale data in block A. This function is a
2297 * helper to solve this problem. It starts writeout of the truncated
2298 * part in case it is in the committing transaction.
2299 *
2300 * Filesystem code must call this function when inode is journaled in
2301 * ordered mode before truncation happens and after the inode has been
2302 * placed on orphan list with the new inode size. The second condition
2303 * avoids the race that someone writes new data and we start
2304 * committing the transaction after this function has been called but
2305 * before a transaction for truncate is started (and furthermore it
2306 * allows us to optimize the case where the addition to orphan list
2307 * happens in the same transaction as write --- we don't have to write
2308 * any data in such case).
Jan Karac851ed52008-07-11 19:27:31 -04002309 */
Jan Kara7f5aa212009-02-10 11:15:34 -05002310int jbd2_journal_begin_ordered_truncate(journal_t *journal,
2311 struct jbd2_inode *jinode,
Jan Karac851ed52008-07-11 19:27:31 -04002312 loff_t new_size)
2313{
Jan Kara7f5aa212009-02-10 11:15:34 -05002314 transaction_t *inode_trans, *commit_trans;
Jan Karac851ed52008-07-11 19:27:31 -04002315 int ret = 0;
2316
Jan Kara7f5aa212009-02-10 11:15:34 -05002317 /* This is a quick check to avoid locking if not necessary */
2318 if (!jinode->i_transaction)
Jan Karac851ed52008-07-11 19:27:31 -04002319 goto out;
Jan Kara7f5aa212009-02-10 11:15:34 -05002320 /* Locks are here just to force reading of recent values, it is
2321 * enough that the transaction was not committing before we started
2322 * a transaction adding the inode to orphan list */
Theodore Ts'oa931da62010-08-03 21:35:12 -04002323 read_lock(&journal->j_state_lock);
Jan Karac851ed52008-07-11 19:27:31 -04002324 commit_trans = journal->j_committing_transaction;
Theodore Ts'oa931da62010-08-03 21:35:12 -04002325 read_unlock(&journal->j_state_lock);
Jan Kara7f5aa212009-02-10 11:15:34 -05002326 spin_lock(&journal->j_list_lock);
2327 inode_trans = jinode->i_transaction;
2328 spin_unlock(&journal->j_list_lock);
2329 if (inode_trans == commit_trans) {
2330 ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping,
Jan Karac851ed52008-07-11 19:27:31 -04002331 new_size, LLONG_MAX);
2332 if (ret)
2333 jbd2_journal_abort(journal, ret);
2334 }
2335out:
2336 return ret;
2337}