blob: c0065040c5bebe13f877f7c48768663978d45439 [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);
Jan Kara8f7d89f2013-06-04 12:35:11 -040092 atomic_set(&transaction->t_outstanding_credits,
93 atomic_read(&journal->j_reserved_credits));
Theodore Ts'o8dd42042010-08-03 21:38:29 -040094 atomic_set(&transaction->t_handle_count, 0);
Jan Karac851ed52008-07-11 19:27:31 -040095 INIT_LIST_HEAD(&transaction->t_inode_list);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -040096 INIT_LIST_HEAD(&transaction->t_private_list);
Dave Kleikamp470decc2006-10-11 01:20:57 -070097
98 /* Set up the commit timer for the new transaction. */
Andreas Dilgerb1f485f2009-08-10 22:51:53 -040099 journal->j_commit_timer.expires = round_jiffies_up(transaction->t_expires);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700100 add_timer(&journal->j_commit_timer);
101
102 J_ASSERT(journal->j_running_transaction == NULL);
103 journal->j_running_transaction = transaction;
Johann Lombardi8e85fb32008-01-28 23:58:27 -0500104 transaction->t_max_wait = 0;
105 transaction->t_start = jiffies;
Theodore Ts'o9fff24a2013-02-06 22:30:23 -0500106 transaction->t_requested = 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700107
108 return transaction;
109}
110
111/*
112 * Handle management.
113 *
114 * A handle_t is an object which represents a single atomic update to a
115 * filesystem, and which tracks all of the modifications which form part
116 * of that one update.
117 */
118
119/*
Tao Ma28e35e42011-05-22 21:45:26 -0400120 * Update transaction's maximum wait time, if debugging is enabled.
Theodore Ts'o6d0bf002010-08-09 17:28:38 -0400121 *
122 * In order for t_max_wait to be reliable, it must be protected by a
123 * lock. But doing so will mean that start_this_handle() can not be
124 * run in parallel on SMP systems, which limits our scalability. So
125 * unless debugging is enabled, we no longer update t_max_wait, which
126 * means that maximum wait time reported by the jbd2_run_stats
127 * tracepoint will always be zero.
128 */
Tao Ma28e35e42011-05-22 21:45:26 -0400129static inline void update_t_max_wait(transaction_t *transaction,
130 unsigned long ts)
Theodore Ts'o6d0bf002010-08-09 17:28:38 -0400131{
132#ifdef CONFIG_JBD2_DEBUG
Theodore Ts'o6d0bf002010-08-09 17:28:38 -0400133 if (jbd2_journal_enable_debug &&
134 time_after(transaction->t_start, ts)) {
135 ts = jbd2_time_diff(ts, transaction->t_start);
136 spin_lock(&transaction->t_handle_lock);
137 if (ts > transaction->t_max_wait)
138 transaction->t_max_wait = ts;
139 spin_unlock(&transaction->t_handle_lock);
140 }
141#endif
142}
143
144/*
Jan Kara8f7d89f2013-06-04 12:35:11 -0400145 * Wait until running transaction passes T_LOCKED state. Also starts the commit
146 * if needed. The function expects running transaction to exist and releases
147 * j_state_lock.
148 */
149static void wait_transaction_locked(journal_t *journal)
150 __releases(journal->j_state_lock)
151{
152 DEFINE_WAIT(wait);
153 int need_to_start;
154 tid_t tid = journal->j_running_transaction->t_tid;
155
156 prepare_to_wait(&journal->j_wait_transaction_locked, &wait,
157 TASK_UNINTERRUPTIBLE);
158 need_to_start = !tid_geq(journal->j_commit_request, tid);
159 read_unlock(&journal->j_state_lock);
160 if (need_to_start)
161 jbd2_log_start_commit(journal, tid);
162 schedule();
163 finish_wait(&journal->j_wait_transaction_locked, &wait);
164}
165
166static void sub_reserved_credits(journal_t *journal, int blocks)
167{
168 atomic_sub(blocks, &journal->j_reserved_credits);
169 wake_up(&journal->j_wait_reserved);
170}
171
172/*
173 * Wait until we can add credits for handle to the running transaction. Called
174 * with j_state_lock held for reading. Returns 0 if handle joined the running
175 * transaction. Returns 1 if we had to wait, j_state_lock is dropped, and
176 * caller must retry.
177 */
178static int add_transaction_credits(journal_t *journal, int blocks,
179 int rsv_blocks)
180{
181 transaction_t *t = journal->j_running_transaction;
182 int needed;
183 int total = blocks + rsv_blocks;
184
185 /*
186 * If the current transaction is locked down for commit, wait
187 * for the lock to be released.
188 */
189 if (t->t_state == T_LOCKED) {
190 wait_transaction_locked(journal);
191 return 1;
192 }
193
194 /*
195 * If there is not enough space left in the log to write all
196 * potential buffers requested by this operation, we need to
197 * stall pending a log checkpoint to free some more log space.
198 */
199 needed = atomic_add_return(total, &t->t_outstanding_credits);
200 if (needed > journal->j_max_transaction_buffers) {
201 /*
202 * If the current transaction is already too large,
203 * then start to commit it: we can then go back and
204 * attach this handle to a new transaction.
205 */
206 atomic_sub(total, &t->t_outstanding_credits);
Lukas Czerner6d3ec142015-08-04 11:21:52 -0400207
208 /*
209 * Is the number of reserved credits in the current transaction too
210 * big to fit this handle? Wait until reserved credits are freed.
211 */
212 if (atomic_read(&journal->j_reserved_credits) + total >
213 journal->j_max_transaction_buffers) {
214 read_unlock(&journal->j_state_lock);
215 wait_event(journal->j_wait_reserved,
216 atomic_read(&journal->j_reserved_credits) + total <=
217 journal->j_max_transaction_buffers);
218 return 1;
219 }
220
Jan Kara8f7d89f2013-06-04 12:35:11 -0400221 wait_transaction_locked(journal);
222 return 1;
223 }
224
225 /*
226 * The commit code assumes that it can get enough log space
227 * without forcing a checkpoint. This is *critical* for
228 * correctness: a checkpoint of a buffer which is also
229 * associated with a committing transaction creates a deadlock,
230 * so commit simply cannot force through checkpoints.
231 *
232 * We must therefore ensure the necessary space in the journal
233 * *before* starting to dirty potentially checkpointed buffers
234 * in the new transaction.
235 */
236 if (jbd2_log_space_left(journal) < jbd2_space_needed(journal)) {
237 atomic_sub(total, &t->t_outstanding_credits);
238 read_unlock(&journal->j_state_lock);
239 write_lock(&journal->j_state_lock);
240 if (jbd2_log_space_left(journal) < jbd2_space_needed(journal))
241 __jbd2_log_wait_for_space(journal);
242 write_unlock(&journal->j_state_lock);
243 return 1;
244 }
245
246 /* No reservation? We are done... */
247 if (!rsv_blocks)
248 return 0;
249
250 needed = atomic_add_return(rsv_blocks, &journal->j_reserved_credits);
251 /* We allow at most half of a transaction to be reserved */
252 if (needed > journal->j_max_transaction_buffers / 2) {
253 sub_reserved_credits(journal, rsv_blocks);
254 atomic_sub(total, &t->t_outstanding_credits);
255 read_unlock(&journal->j_state_lock);
256 wait_event(journal->j_wait_reserved,
257 atomic_read(&journal->j_reserved_credits) + rsv_blocks
258 <= journal->j_max_transaction_buffers / 2);
259 return 1;
260 }
261 return 0;
262}
263
264/*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700265 * start_this_handle: Given a handle, deal with any locking or stalling
266 * needed to make sure that there is enough journal space for the handle
267 * to begin. Attach the handle to a transaction and set up the
268 * transaction's buffer credits.
269 */
270
Theodore Ts'o47def822010-07-27 11:56:05 -0400271static int start_this_handle(journal_t *journal, handle_t *handle,
Dan Carpenterd2159fb2011-09-04 10:20:14 -0400272 gfp_t gfp_mask)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700273{
Theodore Ts'oe4471832011-02-12 08:18:24 -0500274 transaction_t *transaction, *new_transaction = NULL;
Jan Kara8f7d89f2013-06-04 12:35:11 -0400275 int blocks = handle->h_buffer_credits;
276 int rsv_blocks = 0;
Tao Ma28e35e42011-05-22 21:45:26 -0400277 unsigned long ts = jiffies;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700278
Jan Kara8f7d89f2013-06-04 12:35:11 -0400279 if (handle->h_rsv_handle)
280 rsv_blocks = handle->h_rsv_handle->h_buffer_credits;
281
Lukas Czerner6d3ec142015-08-04 11:21:52 -0400282 /*
283 * Limit the number of reserved credits to 1/2 of maximum transaction
284 * size and limit the number of total credits to not exceed maximum
285 * transaction size per operation.
286 */
287 if ((rsv_blocks > journal->j_max_transaction_buffers / 2) ||
288 (rsv_blocks + blocks > journal->j_max_transaction_buffers)) {
289 printk(KERN_ERR "JBD2: %s wants too many credits "
290 "credits:%d rsv_credits:%d max:%d\n",
291 current->comm, blocks, rsv_blocks,
292 journal->j_max_transaction_buffers);
293 WARN_ON(1);
294 return -ENOSPC;
295 }
296
Dave Kleikamp470decc2006-10-11 01:20:57 -0700297alloc_transaction:
298 if (!journal->j_running_transaction) {
Michal Hocko6ccaf3e2015-06-08 10:53:10 -0400299 /*
300 * If __GFP_FS is not present, then we may be being called from
301 * inside the fs writeback layer, so we MUST NOT fail.
302 */
303 if ((gfp_mask & __GFP_FS) == 0)
304 gfp_mask |= __GFP_NOFAIL;
Wanlong Gaob2f4edb2012-06-01 00:10:32 -0400305 new_transaction = kmem_cache_zalloc(transaction_cache,
306 gfp_mask);
Michal Hocko6ccaf3e2015-06-08 10:53:10 -0400307 if (!new_transaction)
Theodore Ts'o47def822010-07-27 11:56:05 -0400308 return -ENOMEM;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700309 }
310
311 jbd_debug(3, "New handle %p going live.\n", handle);
312
Dave Kleikamp470decc2006-10-11 01:20:57 -0700313 /*
314 * We need to hold j_state_lock until t_updates has been incremented,
315 * for proper journal barrier handling
316 */
Theodore Ts'oa931da62010-08-03 21:35:12 -0400317repeat:
318 read_lock(&journal->j_state_lock);
Theodore Ts'o5c2178e2010-10-27 21:30:04 -0400319 BUG_ON(journal->j_flags & JBD2_UNMOUNT);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700320 if (is_journal_aborted(journal) ||
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700321 (journal->j_errno != 0 && !(journal->j_flags & JBD2_ACK_ERR))) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400322 read_unlock(&journal->j_state_lock);
Yongqiang Yang0c2022e2012-02-20 17:53:02 -0500323 jbd2_journal_free_transaction(new_transaction);
Theodore Ts'o47def822010-07-27 11:56:05 -0400324 return -EROFS;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700325 }
326
Jan Kara8f7d89f2013-06-04 12:35:11 -0400327 /*
328 * Wait on the journal's transaction barrier if necessary. Specifically
329 * we allow reserved handles to proceed because otherwise commit could
330 * deadlock on page writeback not being able to complete.
331 */
332 if (!handle->h_reserved && journal->j_barrier_count) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400333 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700334 wait_event(journal->j_wait_transaction_locked,
335 journal->j_barrier_count == 0);
336 goto repeat;
337 }
338
339 if (!journal->j_running_transaction) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400340 read_unlock(&journal->j_state_lock);
341 if (!new_transaction)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700342 goto alloc_transaction;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400343 write_lock(&journal->j_state_lock);
Jan Karad7961c72012-12-21 00:15:51 -0500344 if (!journal->j_running_transaction &&
Jan Kara8f7d89f2013-06-04 12:35:11 -0400345 (handle->h_reserved || !journal->j_barrier_count)) {
Theodore Ts'oa931da62010-08-03 21:35:12 -0400346 jbd2_get_transaction(journal, new_transaction);
347 new_transaction = NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700348 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400349 write_unlock(&journal->j_state_lock);
350 goto repeat;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700351 }
352
353 transaction = journal->j_running_transaction;
354
Jan Kara8f7d89f2013-06-04 12:35:11 -0400355 if (!handle->h_reserved) {
356 /* We may have dropped j_state_lock - restart in that case */
357 if (add_transaction_credits(journal, blocks, rsv_blocks))
358 goto repeat;
359 } else {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700360 /*
Jan Kara8f7d89f2013-06-04 12:35:11 -0400361 * We have handle reserved so we are allowed to join T_LOCKED
362 * transaction and we don't have to check for transaction size
363 * and journal space.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700364 */
Jan Kara8f7d89f2013-06-04 12:35:11 -0400365 sub_reserved_credits(journal, blocks);
366 handle->h_reserved = 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700367 }
368
369 /* OK, account for the buffers that this operation expects to
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400370 * use and add the handle to the running transaction.
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400371 */
Tao Ma28e35e42011-05-22 21:45:26 -0400372 update_t_max_wait(transaction, ts);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700373 handle->h_transaction = transaction;
Jan Kara8f7d89f2013-06-04 12:35:11 -0400374 handle->h_requested_credits = blocks;
Theodore Ts'o343d9c22013-02-08 13:00:22 -0500375 handle->h_start_jiffies = jiffies;
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400376 atomic_inc(&transaction->t_updates);
Theodore Ts'o8dd42042010-08-03 21:38:29 -0400377 atomic_inc(&transaction->t_handle_count);
Jan Kara8f7d89f2013-06-04 12:35:11 -0400378 jbd_debug(4, "Handle %p given %d credits (total %d, free %lu)\n",
379 handle, blocks,
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400380 atomic_read(&transaction->t_outstanding_credits),
Jan Kara76c39902013-06-04 12:12:57 -0400381 jbd2_log_space_left(journal));
Theodore Ts'oa931da62010-08-03 21:35:12 -0400382 read_unlock(&journal->j_state_lock);
Theodore Ts'o41a5b912013-07-01 08:12:41 -0400383 current->journal_info = handle;
Jan Kara9599b0e2009-08-17 21:23:17 -0400384
Jan Karaab714af2016-06-30 11:39:38 -0400385 rwsem_acquire_read(&journal->j_trans_commit_map, 0, 0, _THIS_IP_);
Yongqiang Yang0c2022e2012-02-20 17:53:02 -0500386 jbd2_journal_free_transaction(new_transaction);
Theodore Ts'o47def822010-07-27 11:56:05 -0400387 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700388}
389
390/* Allocate a new handle. This should probably be in a slab... */
391static handle_t *new_handle(int nblocks)
392{
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400393 handle_t *handle = jbd2_alloc_handle(GFP_NOFS);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700394 if (!handle)
395 return NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700396 handle->h_buffer_credits = nblocks;
397 handle->h_ref = 1;
398
399 return handle;
400}
401
402/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700403 * handle_t *jbd2_journal_start() - Obtain a new handle.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700404 * @journal: Journal to start transaction on.
405 * @nblocks: number of block buffer we might modify
406 *
407 * We make sure that the transaction can guarantee at least nblocks of
408 * modified buffers in the log. We block until the log can guarantee
Jan Kara8f7d89f2013-06-04 12:35:11 -0400409 * that much space. Additionally, if rsv_blocks > 0, we also create another
410 * handle with rsv_blocks reserved blocks in the journal. This handle is
411 * is stored in h_rsv_handle. It is not attached to any particular transaction
412 * and thus doesn't block transaction commit. If the caller uses this reserved
413 * handle, it has to set h_rsv_handle to NULL as otherwise jbd2_journal_stop()
414 * on the parent handle will dispose the reserved one. Reserved handle has to
415 * be converted to a normal handle using jbd2_journal_start_reserved() before
416 * it can be used.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700417 *
Eryu Guanc8675162011-05-24 17:09:58 -0400418 * Return a pointer to a newly allocated handle, or an ERR_PTR() value
419 * on failure.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700420 */
Jan Kara8f7d89f2013-06-04 12:35:11 -0400421handle_t *jbd2__journal_start(journal_t *journal, int nblocks, int rsv_blocks,
422 gfp_t gfp_mask, unsigned int type,
423 unsigned int line_no)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700424{
425 handle_t *handle = journal_current_handle();
426 int err;
427
428 if (!journal)
429 return ERR_PTR(-EROFS);
430
431 if (handle) {
432 J_ASSERT(handle->h_transaction->t_journal == journal);
433 handle->h_ref++;
434 return handle;
435 }
436
437 handle = new_handle(nblocks);
438 if (!handle)
439 return ERR_PTR(-ENOMEM);
Jan Kara8f7d89f2013-06-04 12:35:11 -0400440 if (rsv_blocks) {
441 handle_t *rsv_handle;
442
443 rsv_handle = new_handle(rsv_blocks);
444 if (!rsv_handle) {
445 jbd2_free_handle(handle);
446 return ERR_PTR(-ENOMEM);
447 }
448 rsv_handle->h_reserved = 1;
449 rsv_handle->h_journal = journal;
450 handle->h_rsv_handle = rsv_handle;
451 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700452
Theodore Ts'o47def822010-07-27 11:56:05 -0400453 err = start_this_handle(journal, handle, gfp_mask);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700454 if (err < 0) {
Jan Kara8f7d89f2013-06-04 12:35:11 -0400455 if (handle->h_rsv_handle)
456 jbd2_free_handle(handle->h_rsv_handle);
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400457 jbd2_free_handle(handle);
Dmitry Monakhovdf05c1b82013-03-02 17:08:46 -0500458 return ERR_PTR(err);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700459 }
Theodore Ts'o343d9c22013-02-08 13:00:22 -0500460 handle->h_type = type;
461 handle->h_line_no = line_no;
462 trace_jbd2_handle_start(journal->j_fs_dev->bd_dev,
463 handle->h_transaction->t_tid, type,
464 line_no, nblocks);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700465 return handle;
466}
Theodore Ts'o47def822010-07-27 11:56:05 -0400467EXPORT_SYMBOL(jbd2__journal_start);
468
469
470handle_t *jbd2_journal_start(journal_t *journal, int nblocks)
471{
Jan Kara8f7d89f2013-06-04 12:35:11 -0400472 return jbd2__journal_start(journal, nblocks, 0, GFP_NOFS, 0, 0);
Theodore Ts'o47def822010-07-27 11:56:05 -0400473}
474EXPORT_SYMBOL(jbd2_journal_start);
475
Jan Kara8f7d89f2013-06-04 12:35:11 -0400476void jbd2_journal_free_reserved(handle_t *handle)
477{
478 journal_t *journal = handle->h_journal;
479
480 WARN_ON(!handle->h_reserved);
481 sub_reserved_credits(journal, handle->h_buffer_credits);
482 jbd2_free_handle(handle);
483}
484EXPORT_SYMBOL(jbd2_journal_free_reserved);
485
486/**
487 * int jbd2_journal_start_reserved(handle_t *handle) - start reserved handle
488 * @handle: handle to start
489 *
490 * Start handle that has been previously reserved with jbd2_journal_reserve().
491 * This attaches @handle to the running transaction (or creates one if there's
492 * not transaction running). Unlike jbd2_journal_start() this function cannot
493 * block on journal commit, checkpointing, or similar stuff. It can block on
494 * memory allocation or frozen journal though.
495 *
496 * Return 0 on success, non-zero on error - handle is freed in that case.
497 */
498int jbd2_journal_start_reserved(handle_t *handle, unsigned int type,
499 unsigned int line_no)
500{
501 journal_t *journal = handle->h_journal;
502 int ret = -EIO;
503
504 if (WARN_ON(!handle->h_reserved)) {
505 /* Someone passed in normal handle? Just stop it. */
506 jbd2_journal_stop(handle);
507 return ret;
508 }
509 /*
510 * Usefulness of mixing of reserved and unreserved handles is
511 * questionable. So far nobody seems to need it so just error out.
512 */
513 if (WARN_ON(current->journal_info)) {
514 jbd2_journal_free_reserved(handle);
515 return ret;
516 }
517
518 handle->h_journal = NULL;
Jan Kara8f7d89f2013-06-04 12:35:11 -0400519 /*
520 * GFP_NOFS is here because callers are likely from writeback or
521 * similarly constrained call sites
522 */
523 ret = start_this_handle(journal, handle, GFP_NOFS);
Dan Carpenter92e3b402014-02-17 20:33:01 -0500524 if (ret < 0) {
Jan Kara8f7d89f2013-06-04 12:35:11 -0400525 jbd2_journal_free_reserved(handle);
Dan Carpenter92e3b402014-02-17 20:33:01 -0500526 return ret;
527 }
Jan Kara8f7d89f2013-06-04 12:35:11 -0400528 handle->h_type = type;
529 handle->h_line_no = line_no;
Dan Carpenter92e3b402014-02-17 20:33:01 -0500530 return 0;
Jan Kara8f7d89f2013-06-04 12:35:11 -0400531}
532EXPORT_SYMBOL(jbd2_journal_start_reserved);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700533
534/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700535 * int jbd2_journal_extend() - extend buffer credits.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700536 * @handle: handle to 'extend'
537 * @nblocks: nr blocks to try to extend by.
538 *
539 * Some transactions, such as large extends and truncates, can be done
540 * atomically all at once or in several stages. The operation requests
Masanari Iidabd7ced92016-02-02 22:31:06 +0900541 * a credit for a number of buffer modifications in advance, but can
Dave Kleikamp470decc2006-10-11 01:20:57 -0700542 * extend its credit if it needs more.
543 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700544 * jbd2_journal_extend tries to give the running handle more buffer credits.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700545 * It does not guarantee that allocation - this is a best-effort only.
546 * The calling process MUST be able to deal cleanly with a failure to
547 * extend here.
548 *
549 * Return 0 on success, non-zero on failure.
550 *
551 * return code < 0 implies an error
552 * return code > 0 implies normal transaction-full status.
553 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700554int jbd2_journal_extend(handle_t *handle, int nblocks)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700555{
556 transaction_t *transaction = handle->h_transaction;
Theodore Ts'o41a5b912013-07-01 08:12:41 -0400557 journal_t *journal;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700558 int result;
559 int wanted;
560
Dave Kleikamp470decc2006-10-11 01:20:57 -0700561 if (is_handle_aborted(handle))
Theodore Ts'o41a5b912013-07-01 08:12:41 -0400562 return -EROFS;
563 journal = transaction->t_journal;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700564
565 result = 1;
566
Theodore Ts'oa931da62010-08-03 21:35:12 -0400567 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700568
569 /* Don't extend a locked-down transaction! */
Theodore Ts'o41a5b912013-07-01 08:12:41 -0400570 if (transaction->t_state != T_RUNNING) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700571 jbd_debug(3, "denied handle %p %d blocks: "
572 "transaction not running\n", handle, nblocks);
573 goto error_out;
574 }
575
576 spin_lock(&transaction->t_handle_lock);
Jan Karafe1e8db2013-06-04 12:22:15 -0400577 wanted = atomic_add_return(nblocks,
578 &transaction->t_outstanding_credits);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700579
580 if (wanted > journal->j_max_transaction_buffers) {
581 jbd_debug(3, "denied handle %p %d blocks: "
582 "transaction too large\n", handle, nblocks);
Jan Karafe1e8db2013-06-04 12:22:15 -0400583 atomic_sub(nblocks, &transaction->t_outstanding_credits);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700584 goto unlock;
585 }
586
Jan Kara76c39902013-06-04 12:12:57 -0400587 if (wanted + (wanted >> JBD2_CONTROL_BLOCKS_SHIFT) >
588 jbd2_log_space_left(journal)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -0700589 jbd_debug(3, "denied handle %p %d blocks: "
590 "insufficient log space\n", handle, nblocks);
Jan Karafe1e8db2013-06-04 12:22:15 -0400591 atomic_sub(nblocks, &transaction->t_outstanding_credits);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700592 goto unlock;
593 }
594
Theodore Ts'o343d9c22013-02-08 13:00:22 -0500595 trace_jbd2_handle_extend(journal->j_fs_dev->bd_dev,
Theodore Ts'o41a5b912013-07-01 08:12:41 -0400596 transaction->t_tid,
Theodore Ts'o343d9c22013-02-08 13:00:22 -0500597 handle->h_type, handle->h_line_no,
598 handle->h_buffer_credits,
599 nblocks);
600
Dave Kleikamp470decc2006-10-11 01:20:57 -0700601 handle->h_buffer_credits += nblocks;
Theodore Ts'o343d9c22013-02-08 13:00:22 -0500602 handle->h_requested_credits += nblocks;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700603 result = 0;
604
605 jbd_debug(3, "extended handle %p by %d\n", handle, nblocks);
606unlock:
607 spin_unlock(&transaction->t_handle_lock);
608error_out:
Theodore Ts'oa931da62010-08-03 21:35:12 -0400609 read_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700610 return result;
611}
612
613
614/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700615 * int jbd2_journal_restart() - restart a handle .
Dave Kleikamp470decc2006-10-11 01:20:57 -0700616 * @handle: handle to restart
617 * @nblocks: nr credits requested
618 *
619 * Restart a handle for a multi-transaction filesystem
620 * operation.
621 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700622 * If the jbd2_journal_extend() call above fails to grant new buffer credits
623 * to a running handle, a call to jbd2_journal_restart will commit the
Dave Kleikamp470decc2006-10-11 01:20:57 -0700624 * handle's transaction so far and reattach the handle to a new
Masanari Iidabd7ced92016-02-02 22:31:06 +0900625 * transaction capable of guaranteeing the requested number of
Jan Kara8f7d89f2013-06-04 12:35:11 -0400626 * credits. We preserve reserved handle if there's any attached to the
627 * passed in handle.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700628 */
Dan Carpenterd2159fb2011-09-04 10:20:14 -0400629int jbd2__journal_restart(handle_t *handle, int nblocks, gfp_t gfp_mask)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700630{
631 transaction_t *transaction = handle->h_transaction;
Theodore Ts'o41a5b912013-07-01 08:12:41 -0400632 journal_t *journal;
Theodore Ts'oe4471832011-02-12 08:18:24 -0500633 tid_t tid;
634 int need_to_start, ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700635
636 /* If we've had an abort of any type, don't even think about
637 * actually doing the restart! */
638 if (is_handle_aborted(handle))
639 return 0;
Theodore Ts'o41a5b912013-07-01 08:12:41 -0400640 journal = transaction->t_journal;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700641
642 /*
643 * First unlink the handle from its current transaction, and start the
644 * commit on that.
645 */
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400646 J_ASSERT(atomic_read(&transaction->t_updates) > 0);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700647 J_ASSERT(journal_current_handle() == handle);
648
Theodore Ts'oa931da62010-08-03 21:35:12 -0400649 read_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700650 spin_lock(&transaction->t_handle_lock);
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400651 atomic_sub(handle->h_buffer_credits,
652 &transaction->t_outstanding_credits);
Jan Kara8f7d89f2013-06-04 12:35:11 -0400653 if (handle->h_rsv_handle) {
654 sub_reserved_credits(journal,
655 handle->h_rsv_handle->h_buffer_credits);
656 }
Theodore Ts'oa51dca92010-08-02 08:43:25 -0400657 if (atomic_dec_and_test(&transaction->t_updates))
Dave Kleikamp470decc2006-10-11 01:20:57 -0700658 wake_up(&journal->j_wait_updates);
Theodore Ts'o39c04152013-07-01 08:12:40 -0400659 tid = transaction->t_tid;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700660 spin_unlock(&transaction->t_handle_lock);
Theodore Ts'o41a5b912013-07-01 08:12:41 -0400661 handle->h_transaction = NULL;
662 current->journal_info = NULL;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700663
664 jbd_debug(2, "restarting handle %p\n", handle);
Theodore Ts'oe4471832011-02-12 08:18:24 -0500665 need_to_start = !tid_geq(journal->j_commit_request, tid);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400666 read_unlock(&journal->j_state_lock);
Theodore Ts'oe4471832011-02-12 08:18:24 -0500667 if (need_to_start)
668 jbd2_log_start_commit(journal, tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700669
Jan Karaab714af2016-06-30 11:39:38 -0400670 rwsem_release(&journal->j_trans_commit_map, 1, _THIS_IP_);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700671 handle->h_buffer_credits = nblocks;
Theodore Ts'o47def822010-07-27 11:56:05 -0400672 ret = start_this_handle(journal, handle, gfp_mask);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700673 return ret;
674}
Theodore Ts'o47def822010-07-27 11:56:05 -0400675EXPORT_SYMBOL(jbd2__journal_restart);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700676
677
Theodore Ts'o47def822010-07-27 11:56:05 -0400678int jbd2_journal_restart(handle_t *handle, int nblocks)
679{
680 return jbd2__journal_restart(handle, nblocks, GFP_NOFS);
681}
682EXPORT_SYMBOL(jbd2_journal_restart);
683
Dave Kleikamp470decc2006-10-11 01:20:57 -0700684/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700685 * void jbd2_journal_lock_updates () - establish a transaction barrier.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700686 * @journal: Journal to establish a barrier on.
687 *
688 * This locks out any further updates from being started, and blocks
689 * until all existing updates have completed, returning only once the
690 * journal is in a quiescent state with no updates running.
691 *
692 * The journal lock should not be held on entry.
693 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700694void jbd2_journal_lock_updates(journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700695{
696 DEFINE_WAIT(wait);
697
Theodore Ts'oa931da62010-08-03 21:35:12 -0400698 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700699 ++journal->j_barrier_count;
700
Jan Kara8f7d89f2013-06-04 12:35:11 -0400701 /* Wait until there are no reserved handles */
702 if (atomic_read(&journal->j_reserved_credits)) {
703 write_unlock(&journal->j_state_lock);
704 wait_event(journal->j_wait_reserved,
705 atomic_read(&journal->j_reserved_credits) == 0);
706 write_lock(&journal->j_state_lock);
707 }
708
Dave Kleikamp470decc2006-10-11 01:20:57 -0700709 /* Wait until there are no running updates */
710 while (1) {
711 transaction_t *transaction = journal->j_running_transaction;
712
713 if (!transaction)
714 break;
715
716 spin_lock(&transaction->t_handle_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700717 prepare_to_wait(&journal->j_wait_updates, &wait,
718 TASK_UNINTERRUPTIBLE);
Jan Kara9837d8e2012-01-04 22:03:11 -0500719 if (!atomic_read(&transaction->t_updates)) {
720 spin_unlock(&transaction->t_handle_lock);
721 finish_wait(&journal->j_wait_updates, &wait);
722 break;
723 }
Dave Kleikamp470decc2006-10-11 01:20:57 -0700724 spin_unlock(&transaction->t_handle_lock);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400725 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700726 schedule();
727 finish_wait(&journal->j_wait_updates, &wait);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400728 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700729 }
Theodore Ts'oa931da62010-08-03 21:35:12 -0400730 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700731
732 /*
733 * We have now established a barrier against other normal updates, but
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700734 * we also need to barrier against other jbd2_journal_lock_updates() calls
Dave Kleikamp470decc2006-10-11 01:20:57 -0700735 * to make sure that we serialise special journal-locked operations
736 * too.
737 */
738 mutex_lock(&journal->j_barrier);
739}
740
741/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700742 * void jbd2_journal_unlock_updates (journal_t* journal) - release barrier
Dave Kleikamp470decc2006-10-11 01:20:57 -0700743 * @journal: Journal to release the barrier on.
744 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700745 * Release a transaction barrier obtained with jbd2_journal_lock_updates().
Dave Kleikamp470decc2006-10-11 01:20:57 -0700746 *
747 * Should be called without the journal lock held.
748 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700749void jbd2_journal_unlock_updates (journal_t *journal)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700750{
751 J_ASSERT(journal->j_barrier_count != 0);
752
753 mutex_unlock(&journal->j_barrier);
Theodore Ts'oa931da62010-08-03 21:35:12 -0400754 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700755 --journal->j_barrier_count;
Theodore Ts'oa931da62010-08-03 21:35:12 -0400756 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700757 wake_up(&journal->j_wait_transaction_locked);
758}
759
Jan Karaf91d1d02009-07-13 16:16:20 -0400760static void warn_dirty_buffer(struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -0700761{
Jan Karaf91d1d02009-07-13 16:16:20 -0400762 printk(KERN_WARNING
Dmitry Monakhova1c6f0572015-04-13 16:31:37 +0400763 "JBD2: Spotted dirty metadata buffer (dev = %pg, blocknr = %llu). "
Jan Karaf91d1d02009-07-13 16:16:20 -0400764 "There's a risk of filesystem corruption in case of system "
765 "crash.\n",
Dmitry Monakhova1c6f0572015-04-13 16:31:37 +0400766 bh->b_bdev, (unsigned long long)bh->b_blocknr);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700767}
768
Jan Karaee57aba2015-06-08 12:39:07 -0400769/* Call t_frozen trigger and copy buffer data into jh->b_frozen_data. */
770static void jbd2_freeze_jh_data(struct journal_head *jh)
771{
772 struct page *page;
773 int offset;
774 char *source;
775 struct buffer_head *bh = jh2bh(jh);
776
777 J_EXPECT_JH(jh, buffer_uptodate(bh), "Possible IO failure.\n");
778 page = bh->b_page;
779 offset = offset_in_page(bh->b_data);
780 source = kmap_atomic(page);
781 /* Fire data frozen trigger just before we copy the data */
782 jbd2_buffer_frozen_trigger(jh, source + offset, jh->b_triggers);
783 memcpy(jh->b_frozen_data, source + offset, bh->b_size);
784 kunmap_atomic(source);
785
786 /*
787 * Now that the frozen data is saved off, we need to store any matching
788 * triggers.
789 */
790 jh->b_frozen_triggers = jh->b_triggers;
791}
792
Dave Kleikamp470decc2006-10-11 01:20:57 -0700793/*
794 * If the buffer is already part of the current transaction, then there
795 * is nothing we need to do. If it is already part of a prior
796 * transaction which we are still committing to disk, then we need to
797 * make sure that we do not overwrite the old copy: we do copy-out to
798 * preserve the copy going to disk. We also account the buffer against
799 * the handle's metadata buffer credits (unless the buffer is already
800 * part of the transaction, that is).
801 *
802 */
803static int
804do_get_write_access(handle_t *handle, struct journal_head *jh,
805 int force_copy)
806{
807 struct buffer_head *bh;
Theodore Ts'o41a5b912013-07-01 08:12:41 -0400808 transaction_t *transaction = handle->h_transaction;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700809 journal_t *journal;
810 int error;
811 char *frozen_buffer = NULL;
Theodore Ts'of783f092013-04-21 16:47:54 -0400812 unsigned long start_lock, time_lock;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700813
814 if (is_handle_aborted(handle))
815 return -EROFS;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700816 journal = transaction->t_journal;
817
Theodore Ts'ocfef2c62010-12-18 13:07:34 -0500818 jbd_debug(5, "journal_head %p, force_copy %d\n", jh, force_copy);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700819
820 JBUFFER_TRACE(jh, "entry");
821repeat:
822 bh = jh2bh(jh);
823
824 /* @@@ Need to check for errors here at some point. */
825
Theodore Ts'of783f092013-04-21 16:47:54 -0400826 start_lock = jiffies;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700827 lock_buffer(bh);
828 jbd_lock_bh_state(bh);
829
Theodore Ts'of783f092013-04-21 16:47:54 -0400830 /* If it takes too long to lock the buffer, trace it */
831 time_lock = jbd2_time_diff(start_lock, jiffies);
832 if (time_lock > HZ/10)
833 trace_jbd2_lock_buffer_stall(bh->b_bdev->bd_dev,
834 jiffies_to_msecs(time_lock));
835
Dave Kleikamp470decc2006-10-11 01:20:57 -0700836 /* We now hold the buffer lock so it is safe to query the buffer
837 * state. Is the buffer dirty?
838 *
839 * If so, there are two possibilities. The buffer may be
840 * non-journaled, and undergoing a quite legitimate writeback.
841 * Otherwise, it is journaled, and we don't expect dirty buffers
842 * in that state (the buffers should be marked JBD_Dirty
843 * instead.) So either the IO is being done under our own
844 * control and this is a bug, or it's a third party IO such as
845 * dump(8) (which may leave the buffer scheduled for read ---
846 * ie. locked but not dirty) or tune2fs (which may actually have
847 * the buffer dirtied, ugh.) */
848
849 if (buffer_dirty(bh)) {
850 /*
851 * First question: is this buffer already part of the current
852 * transaction or the existing committing transaction?
853 */
854 if (jh->b_transaction) {
855 J_ASSERT_JH(jh,
856 jh->b_transaction == transaction ||
857 jh->b_transaction ==
858 journal->j_committing_transaction);
859 if (jh->b_next_transaction)
860 J_ASSERT_JH(jh, jh->b_next_transaction ==
861 transaction);
Jan Karaf91d1d02009-07-13 16:16:20 -0400862 warn_dirty_buffer(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700863 }
864 /*
865 * In any case we need to clean the dirty flag and we must
866 * do it under the buffer lock to be sure we don't race
867 * with running write-out.
868 */
Jan Karaf91d1d02009-07-13 16:16:20 -0400869 JBUFFER_TRACE(jh, "Journalling dirty buffer");
870 clear_buffer_dirty(bh);
871 set_buffer_jbddirty(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700872 }
873
874 unlock_buffer(bh);
875
876 error = -EROFS;
877 if (is_handle_aborted(handle)) {
878 jbd_unlock_bh_state(bh);
879 goto out;
880 }
881 error = 0;
882
883 /*
884 * The buffer is already part of this transaction if b_transaction or
885 * b_next_transaction points to it
886 */
887 if (jh->b_transaction == transaction ||
888 jh->b_next_transaction == transaction)
889 goto done;
890
891 /*
Josef Bacik9fc7c632008-04-17 10:38:59 -0400892 * this is the first time this transaction is touching this buffer,
893 * reset the modified flag
894 */
895 jh->b_modified = 0;
896
897 /*
Jan Kara8b00f402015-06-08 12:44:21 -0400898 * If the buffer is not journaled right now, we need to make sure it
899 * doesn't get written to disk before the caller actually commits the
900 * new data
901 */
902 if (!jh->b_transaction) {
903 JBUFFER_TRACE(jh, "no transaction");
904 J_ASSERT_JH(jh, !jh->b_next_transaction);
905 JBUFFER_TRACE(jh, "file as BJ_Reserved");
Jan Karade92c8c2015-06-08 12:46:37 -0400906 /*
907 * Make sure all stores to jh (b_modified, b_frozen_data) are
908 * visible before attaching it to the running transaction.
909 * Paired with barrier in jbd2_write_access_granted()
910 */
911 smp_wmb();
Jan Kara8b00f402015-06-08 12:44:21 -0400912 spin_lock(&journal->j_list_lock);
913 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
914 spin_unlock(&journal->j_list_lock);
915 goto done;
916 }
917 /*
Dave Kleikamp470decc2006-10-11 01:20:57 -0700918 * If there is already a copy-out version of this buffer, then we don't
919 * need to make another one
920 */
921 if (jh->b_frozen_data) {
922 JBUFFER_TRACE(jh, "has frozen data");
923 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
Jan Karade92c8c2015-06-08 12:46:37 -0400924 goto attach_next;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700925 }
926
Jan Kara8b00f402015-06-08 12:44:21 -0400927 JBUFFER_TRACE(jh, "owned by older transaction");
928 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
929 J_ASSERT_JH(jh, jh->b_transaction == journal->j_committing_transaction);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700930
931 /*
Jan Kara8b00f402015-06-08 12:44:21 -0400932 * There is one case we have to be very careful about. If the
933 * committing transaction is currently writing this buffer out to disk
934 * and has NOT made a copy-out, then we cannot modify the buffer
935 * contents at all right now. The essence of copy-out is that it is
936 * the extra copy, not the primary copy, which gets journaled. If the
937 * primary copy is already going to disk then we cannot do copy-out
938 * here.
Dave Kleikamp470decc2006-10-11 01:20:57 -0700939 */
Jan Kara8b00f402015-06-08 12:44:21 -0400940 if (buffer_shadow(bh)) {
941 JBUFFER_TRACE(jh, "on shadow: sleep");
942 jbd_unlock_bh_state(bh);
943 wait_on_bit_io(&bh->b_state, BH_Shadow, TASK_UNINTERRUPTIBLE);
944 goto repeat;
Dave Kleikamp470decc2006-10-11 01:20:57 -0700945 }
946
Jan Kara8b00f402015-06-08 12:44:21 -0400947 /*
948 * Only do the copy if the currently-owning transaction still needs it.
949 * If buffer isn't on BJ_Metadata list, the committing transaction is
950 * past that stage (here we use the fact that BH_Shadow is set under
951 * bh_state lock together with refiling to BJ_Shadow list and at this
952 * point we know the buffer doesn't have BH_Shadow set).
953 *
954 * Subtle point, though: if this is a get_undo_access, then we will be
955 * relying on the frozen_data to contain the new value of the
956 * committed_data record after the transaction, so we HAVE to force the
957 * frozen_data copy in that case.
958 */
959 if (jh->b_jlist == BJ_Metadata || force_copy) {
960 JBUFFER_TRACE(jh, "generate frozen data");
961 if (!frozen_buffer) {
962 JBUFFER_TRACE(jh, "allocate memory for buffer");
963 jbd_unlock_bh_state(bh);
Michal Hocko490c1b42016-03-13 17:38:20 -0400964 frozen_buffer = jbd2_alloc(jh2bh(jh)->b_size,
965 GFP_NOFS | __GFP_NOFAIL);
Jan Kara8b00f402015-06-08 12:44:21 -0400966 goto repeat;
967 }
968 jh->b_frozen_data = frozen_buffer;
969 frozen_buffer = NULL;
970 jbd2_freeze_jh_data(jh);
971 }
Jan Karade92c8c2015-06-08 12:46:37 -0400972attach_next:
973 /*
974 * Make sure all stores to jh (b_modified, b_frozen_data) are visible
975 * before attaching it to the running transaction. Paired with barrier
976 * in jbd2_write_access_granted()
977 */
978 smp_wmb();
Jan Kara8b00f402015-06-08 12:44:21 -0400979 jh->b_next_transaction = transaction;
980
Dave Kleikamp470decc2006-10-11 01:20:57 -0700981done:
Dave Kleikamp470decc2006-10-11 01:20:57 -0700982 jbd_unlock_bh_state(bh);
983
984 /*
985 * If we are about to journal a buffer, then any revoke pending on it is
986 * no longer valid
987 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -0700988 jbd2_journal_cancel_revoke(handle, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700989
990out:
991 if (unlikely(frozen_buffer)) /* It's usually NULL */
Mingming Caoaf1e76d2007-10-16 18:38:25 -0400992 jbd2_free(frozen_buffer, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -0700993
994 JBUFFER_TRACE(jh, "exit");
995 return error;
996}
997
Jan Karade92c8c2015-06-08 12:46:37 -0400998/* Fast check whether buffer is already attached to the required transaction */
Junxiao Bi087ffd42015-12-04 12:29:28 -0500999static bool jbd2_write_access_granted(handle_t *handle, struct buffer_head *bh,
1000 bool undo)
Jan Karade92c8c2015-06-08 12:46:37 -04001001{
1002 struct journal_head *jh;
1003 bool ret = false;
1004
1005 /* Dirty buffers require special handling... */
1006 if (buffer_dirty(bh))
1007 return false;
1008
1009 /*
1010 * RCU protects us from dereferencing freed pages. So the checks we do
1011 * are guaranteed not to oops. However the jh slab object can get freed
1012 * & reallocated while we work with it. So we have to be careful. When
1013 * we see jh attached to the running transaction, we know it must stay
1014 * so until the transaction is committed. Thus jh won't be freed and
1015 * will be attached to the same bh while we run. However it can
1016 * happen jh gets freed, reallocated, and attached to the transaction
1017 * just after we get pointer to it from bh. So we have to be careful
1018 * and recheck jh still belongs to our bh before we return success.
1019 */
1020 rcu_read_lock();
1021 if (!buffer_jbd(bh))
1022 goto out;
1023 /* This should be bh2jh() but that doesn't work with inline functions */
1024 jh = READ_ONCE(bh->b_private);
1025 if (!jh)
1026 goto out;
Junxiao Bi087ffd42015-12-04 12:29:28 -05001027 /* For undo access buffer must have data copied */
1028 if (undo && !jh->b_committed_data)
1029 goto out;
Jan Karade92c8c2015-06-08 12:46:37 -04001030 if (jh->b_transaction != handle->h_transaction &&
1031 jh->b_next_transaction != handle->h_transaction)
1032 goto out;
1033 /*
1034 * There are two reasons for the barrier here:
1035 * 1) Make sure to fetch b_bh after we did previous checks so that we
1036 * detect when jh went through free, realloc, attach to transaction
1037 * while we were checking. Paired with implicit barrier in that path.
1038 * 2) So that access to bh done after jbd2_write_access_granted()
1039 * doesn't get reordered and see inconsistent state of concurrent
1040 * do_get_write_access().
1041 */
1042 smp_mb();
1043 if (unlikely(jh->b_bh != bh))
1044 goto out;
1045 ret = true;
1046out:
1047 rcu_read_unlock();
1048 return ret;
1049}
1050
Dave Kleikamp470decc2006-10-11 01:20:57 -07001051/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001052 * int jbd2_journal_get_write_access() - notify intent to modify a buffer for metadata (not data) update.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001053 * @handle: transaction to add buffer modifications to
1054 * @bh: bh to be used for metadata writes
Dave Kleikamp470decc2006-10-11 01:20:57 -07001055 *
1056 * Returns an error code or 0 on success.
1057 *
1058 * In full data journalling mode the buffer may be of type BJ_AsyncData,
1059 * because we're write()ing a buffer which is also part of a shared mapping.
1060 */
1061
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001062int jbd2_journal_get_write_access(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001063{
Jan Karade92c8c2015-06-08 12:46:37 -04001064 struct journal_head *jh;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001065 int rc;
1066
Junxiao Bi087ffd42015-12-04 12:29:28 -05001067 if (jbd2_write_access_granted(handle, bh, false))
Jan Karade92c8c2015-06-08 12:46:37 -04001068 return 0;
1069
1070 jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001071 /* We do not want to get caught playing with fields which the
1072 * log thread also manipulates. Make sure that the buffer
1073 * completes any outstanding IO before proceeding. */
1074 rc = do_get_write_access(handle, jh, 0);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001075 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001076 return rc;
1077}
1078
1079
1080/*
1081 * When the user wants to journal a newly created buffer_head
1082 * (ie. getblk() returned a new buffer and we are going to populate it
1083 * manually rather than reading off disk), then we need to keep the
1084 * buffer_head locked until it has been completely filled with new
1085 * data. In this case, we should be able to make the assertion that
1086 * the bh is not already part of an existing transaction.
1087 *
1088 * The buffer should already be locked by the caller by this point.
1089 * There is no lock ranking violation: it was a newly created,
1090 * unlocked buffer beforehand. */
1091
1092/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001093 * int jbd2_journal_get_create_access () - notify intent to use newly created bh
Dave Kleikamp470decc2006-10-11 01:20:57 -07001094 * @handle: transaction to new buffer to
1095 * @bh: new buffer.
1096 *
1097 * Call this if you create a new bh.
1098 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001099int jbd2_journal_get_create_access(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001100{
1101 transaction_t *transaction = handle->h_transaction;
Theodore Ts'o41a5b912013-07-01 08:12:41 -04001102 journal_t *journal;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001103 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001104 int err;
1105
1106 jbd_debug(5, "journal_head %p\n", jh);
1107 err = -EROFS;
1108 if (is_handle_aborted(handle))
1109 goto out;
Theodore Ts'o41a5b912013-07-01 08:12:41 -04001110 journal = transaction->t_journal;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001111 err = 0;
1112
1113 JBUFFER_TRACE(jh, "entry");
1114 /*
1115 * The buffer may already belong to this transaction due to pre-zeroing
1116 * in the filesystem's new_block code. It may also be on the previous,
1117 * committing transaction's lists, but it HAS to be in Forget state in
1118 * that case: the transaction must have deleted the buffer for it to be
1119 * reused here.
1120 */
1121 jbd_lock_bh_state(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001122 J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
1123 jh->b_transaction == NULL ||
1124 (jh->b_transaction == journal->j_committing_transaction &&
1125 jh->b_jlist == BJ_Forget)));
1126
1127 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
1128 J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
1129
1130 if (jh->b_transaction == NULL) {
Jan Karaf91d1d02009-07-13 16:16:20 -04001131 /*
1132 * Previous jbd2_journal_forget() could have left the buffer
1133 * with jbddirty bit set because it was being committed. When
1134 * the commit finished, we've filed the buffer for
1135 * checkpointing and marked it dirty. Now we are reallocating
1136 * the buffer so the transaction freeing it must have
1137 * committed and so it's safe to clear the dirty bit.
1138 */
1139 clear_buffer_dirty(jh2bh(jh));
Josef Bacik9fc7c632008-04-17 10:38:59 -04001140 /* first access by this transaction */
1141 jh->b_modified = 0;
1142
Dave Kleikamp470decc2006-10-11 01:20:57 -07001143 JBUFFER_TRACE(jh, "file as BJ_Reserved");
Theodore Ts'o6e4862a2014-03-09 00:46:23 -05001144 spin_lock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001145 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001146 } else if (jh->b_transaction == journal->j_committing_transaction) {
Josef Bacik9fc7c632008-04-17 10:38:59 -04001147 /* first access by this transaction */
1148 jh->b_modified = 0;
1149
Dave Kleikamp470decc2006-10-11 01:20:57 -07001150 JBUFFER_TRACE(jh, "set next transaction");
Theodore Ts'o6e4862a2014-03-09 00:46:23 -05001151 spin_lock(&journal->j_list_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001152 jh->b_next_transaction = transaction;
1153 }
1154 spin_unlock(&journal->j_list_lock);
1155 jbd_unlock_bh_state(bh);
1156
1157 /*
1158 * akpm: I added this. ext3_alloc_branch can pick up new indirect
1159 * blocks which contain freed but then revoked metadata. We need
1160 * to cancel the revoke in case we end up freeing it yet again
1161 * and the reallocating as data - this would cause a second revoke,
1162 * which hits an assertion error.
1163 */
1164 JBUFFER_TRACE(jh, "cancelling revoke");
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001165 jbd2_journal_cancel_revoke(handle, jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001166out:
Ding Dinghua3991b402011-05-25 17:43:48 -04001167 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001168 return err;
1169}
1170
1171/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001172 * int jbd2_journal_get_undo_access() - Notify intent to modify metadata with
Dave Kleikamp470decc2006-10-11 01:20:57 -07001173 * non-rewindable consequences
1174 * @handle: transaction
1175 * @bh: buffer to undo
Dave Kleikamp470decc2006-10-11 01:20:57 -07001176 *
1177 * Sometimes there is a need to distinguish between metadata which has
1178 * been committed to disk and that which has not. The ext3fs code uses
1179 * this for freeing and allocating space, we have to make sure that we
1180 * do not reuse freed space until the deallocation has been committed,
1181 * since if we overwrote that space we would make the delete
1182 * un-rewindable in case of a crash.
1183 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001184 * To deal with that, jbd2_journal_get_undo_access requests write access to a
Dave Kleikamp470decc2006-10-11 01:20:57 -07001185 * buffer for parts of non-rewindable operations such as delete
1186 * operations on the bitmaps. The journaling code must keep a copy of
1187 * the buffer's contents prior to the undo_access call until such time
1188 * as we know that the buffer has definitely been committed to disk.
1189 *
1190 * We never need to know which transaction the committed data is part
1191 * of, buffers touched here are guaranteed to be dirtied later and so
1192 * will be committed to a new transaction in due course, at which point
1193 * we can discard the old committed data pointer.
1194 *
1195 * Returns error number or 0 on success.
1196 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001197int jbd2_journal_get_undo_access(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001198{
1199 int err;
Jan Karade92c8c2015-06-08 12:46:37 -04001200 struct journal_head *jh;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001201 char *committed_data = NULL;
1202
1203 JBUFFER_TRACE(jh, "entry");
Junxiao Bi087ffd42015-12-04 12:29:28 -05001204 if (jbd2_write_access_granted(handle, bh, true))
Jan Karade92c8c2015-06-08 12:46:37 -04001205 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001206
Jan Karade92c8c2015-06-08 12:46:37 -04001207 jh = jbd2_journal_add_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001208 /*
1209 * Do this first --- it can drop the journal lock, so we want to
1210 * make sure that obtaining the committed_data is done
1211 * atomically wrt. completion of any outstanding commits.
1212 */
1213 err = do_get_write_access(handle, jh, 1);
1214 if (err)
1215 goto out;
1216
1217repeat:
Michal Hocko490c1b42016-03-13 17:38:20 -04001218 if (!jh->b_committed_data)
1219 committed_data = jbd2_alloc(jh2bh(jh)->b_size,
1220 GFP_NOFS|__GFP_NOFAIL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001221
1222 jbd_lock_bh_state(bh);
1223 if (!jh->b_committed_data) {
1224 /* Copy out the current buffer contents into the
1225 * preserved, committed copy. */
1226 JBUFFER_TRACE(jh, "generate b_committed data");
1227 if (!committed_data) {
1228 jbd_unlock_bh_state(bh);
1229 goto repeat;
1230 }
1231
1232 jh->b_committed_data = committed_data;
1233 committed_data = NULL;
1234 memcpy(jh->b_committed_data, bh->b_data, bh->b_size);
1235 }
1236 jbd_unlock_bh_state(bh);
1237out:
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001238 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001239 if (unlikely(committed_data))
Mingming Caoaf1e76d2007-10-16 18:38:25 -04001240 jbd2_free(committed_data, bh->b_size);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001241 return err;
1242}
1243
1244/**
Joel Beckere06c8222008-09-11 15:35:47 -07001245 * void jbd2_journal_set_triggers() - Add triggers for commit writeout
1246 * @bh: buffer to trigger on
1247 * @type: struct jbd2_buffer_trigger_type containing the trigger(s).
1248 *
1249 * Set any triggers on this journal_head. This is always safe, because
1250 * triggers for a committing buffer will be saved off, and triggers for
1251 * a running transaction will match the buffer in that transaction.
1252 *
1253 * Call with NULL to clear the triggers.
1254 */
1255void jbd2_journal_set_triggers(struct buffer_head *bh,
1256 struct jbd2_buffer_trigger_type *type)
1257{
Jan Karaad56eda2013-03-11 13:24:56 -04001258 struct journal_head *jh = jbd2_journal_grab_journal_head(bh);
Joel Beckere06c8222008-09-11 15:35:47 -07001259
Jan Karaad56eda2013-03-11 13:24:56 -04001260 if (WARN_ON(!jh))
1261 return;
Joel Beckere06c8222008-09-11 15:35:47 -07001262 jh->b_triggers = type;
Jan Karaad56eda2013-03-11 13:24:56 -04001263 jbd2_journal_put_journal_head(jh);
Joel Beckere06c8222008-09-11 15:35:47 -07001264}
1265
Jan Kara13ceef02010-07-14 07:56:33 +02001266void jbd2_buffer_frozen_trigger(struct journal_head *jh, void *mapped_data,
Joel Beckere06c8222008-09-11 15:35:47 -07001267 struct jbd2_buffer_trigger_type *triggers)
1268{
1269 struct buffer_head *bh = jh2bh(jh);
1270
Jan Kara13ceef02010-07-14 07:56:33 +02001271 if (!triggers || !triggers->t_frozen)
Joel Beckere06c8222008-09-11 15:35:47 -07001272 return;
1273
Jan Kara13ceef02010-07-14 07:56:33 +02001274 triggers->t_frozen(triggers, bh, mapped_data, bh->b_size);
Joel Beckere06c8222008-09-11 15:35:47 -07001275}
1276
1277void jbd2_buffer_abort_trigger(struct journal_head *jh,
1278 struct jbd2_buffer_trigger_type *triggers)
1279{
1280 if (!triggers || !triggers->t_abort)
1281 return;
1282
1283 triggers->t_abort(triggers, jh2bh(jh));
1284}
1285
Joel Beckere06c8222008-09-11 15:35:47 -07001286/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001287 * int jbd2_journal_dirty_metadata() - mark a buffer as containing dirty metadata
Dave Kleikamp470decc2006-10-11 01:20:57 -07001288 * @handle: transaction to add buffer to.
1289 * @bh: buffer to mark
1290 *
1291 * mark dirty metadata which needs to be journaled as part of the current
1292 * transaction.
1293 *
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -04001294 * The buffer must have previously had jbd2_journal_get_write_access()
1295 * called so that it has a valid journal_head attached to the buffer
1296 * head.
1297 *
Dave Kleikamp470decc2006-10-11 01:20:57 -07001298 * The buffer is placed on the transaction's metadata list and is marked
1299 * as belonging to the transaction.
1300 *
1301 * Returns error number or 0 on success.
1302 *
1303 * Special care needs to be taken if the buffer already belongs to the
1304 * current committing transaction (in which case we should have frozen
1305 * data present for that commit). In that case, we don't relink the
1306 * buffer: that only gets done when the old transaction finally
1307 * completes its commit.
1308 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001309int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001310{
1311 transaction_t *transaction = handle->h_transaction;
Theodore Ts'o41a5b912013-07-01 08:12:41 -04001312 journal_t *journal;
Jan Karaad56eda2013-03-11 13:24:56 -04001313 struct journal_head *jh;
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -04001314 int ret = 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001315
Dave Kleikamp470decc2006-10-11 01:20:57 -07001316 if (is_handle_aborted(handle))
Theodore Ts'o41a5b912013-07-01 08:12:41 -04001317 return -EROFS;
Jan Kara6e06ae82015-07-12 18:11:30 -04001318 if (!buffer_jbd(bh)) {
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -04001319 ret = -EUCLEAN;
1320 goto out;
1321 }
Jan Kara6e06ae82015-07-12 18:11:30 -04001322 /*
1323 * We don't grab jh reference here since the buffer must be part
1324 * of the running transaction.
1325 */
1326 jh = bh2jh(bh);
1327 /*
1328 * This and the following assertions are unreliable since we may see jh
1329 * in inconsistent state unless we grab bh_state lock. But this is
1330 * crucial to catch bugs so let's do a reliable check until the
1331 * lockless handling is fully proven.
1332 */
1333 if (jh->b_transaction != transaction &&
1334 jh->b_next_transaction != transaction) {
1335 jbd_lock_bh_state(bh);
1336 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
1337 jh->b_next_transaction == transaction);
1338 jbd_unlock_bh_state(bh);
1339 }
1340 if (jh->b_modified == 1) {
1341 /* If it's in our transaction it must be in BJ_Metadata list. */
1342 if (jh->b_transaction == transaction &&
1343 jh->b_jlist != BJ_Metadata) {
1344 jbd_lock_bh_state(bh);
1345 J_ASSERT_JH(jh, jh->b_transaction != transaction ||
1346 jh->b_jlist == BJ_Metadata);
1347 jbd_unlock_bh_state(bh);
1348 }
1349 goto out;
1350 }
1351
1352 journal = transaction->t_journal;
Jan Karaad56eda2013-03-11 13:24:56 -04001353 jbd_debug(5, "journal_head %p\n", jh);
1354 JBUFFER_TRACE(jh, "entry");
Dave Kleikamp470decc2006-10-11 01:20:57 -07001355
1356 jbd_lock_bh_state(bh);
1357
1358 if (jh->b_modified == 0) {
1359 /*
1360 * This buffer's got modified and becoming part
1361 * of the transaction. This needs to be done
1362 * once a transaction -bzzz
1363 */
1364 jh->b_modified = 1;
Theodore Ts'of6c07ca2013-12-08 21:12:59 -05001365 if (handle->h_buffer_credits <= 0) {
1366 ret = -ENOSPC;
1367 goto out_unlock_bh;
1368 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001369 handle->h_buffer_credits--;
1370 }
1371
1372 /*
1373 * fastpath, to avoid expensive locking. If this buffer is already
1374 * on the running transaction's metadata list there is nothing to do.
1375 * Nobody can take it off again because there is a handle open.
1376 * I _think_ we're OK here with SMP barriers - a mistaken decision will
1377 * result in this test being false, so we go in and take the locks.
1378 */
1379 if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) {
1380 JBUFFER_TRACE(jh, "fastpath");
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -04001381 if (unlikely(jh->b_transaction !=
1382 journal->j_running_transaction)) {
Dmitry Monakhova67c8482013-12-08 21:14:59 -05001383 printk(KERN_ERR "JBD2: %s: "
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -04001384 "jh->b_transaction (%llu, %p, %u) != "
Theodore Ts'o66a4cb12014-03-12 16:38:03 -04001385 "journal->j_running_transaction (%p, %u)\n",
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -04001386 journal->j_devname,
1387 (unsigned long long) bh->b_blocknr,
1388 jh->b_transaction,
1389 jh->b_transaction ? jh->b_transaction->t_tid : 0,
1390 journal->j_running_transaction,
1391 journal->j_running_transaction ?
1392 journal->j_running_transaction->t_tid : 0);
1393 ret = -EINVAL;
1394 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001395 goto out_unlock_bh;
1396 }
1397
1398 set_buffer_jbddirty(bh);
1399
1400 /*
1401 * Metadata already on the current transaction list doesn't
1402 * need to be filed. Metadata on another transaction's list must
1403 * be committing, and will be refiled once the commit completes:
1404 * leave it alone for now.
1405 */
1406 if (jh->b_transaction != transaction) {
1407 JBUFFER_TRACE(jh, "already on other transaction");
Theodore Ts'o66a4cb12014-03-12 16:38:03 -04001408 if (unlikely(((jh->b_transaction !=
1409 journal->j_committing_transaction)) ||
1410 (jh->b_next_transaction != transaction))) {
1411 printk(KERN_ERR "jbd2_journal_dirty_metadata: %s: "
1412 "bad jh for block %llu: "
1413 "transaction (%p, %u), "
1414 "jh->b_transaction (%p, %u), "
1415 "jh->b_next_transaction (%p, %u), jlist %u\n",
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -04001416 journal->j_devname,
1417 (unsigned long long) bh->b_blocknr,
Theodore Ts'o66a4cb12014-03-12 16:38:03 -04001418 transaction, transaction->t_tid,
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -04001419 jh->b_transaction,
Theodore Ts'o66a4cb12014-03-12 16:38:03 -04001420 jh->b_transaction ?
1421 jh->b_transaction->t_tid : 0,
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -04001422 jh->b_next_transaction,
1423 jh->b_next_transaction ?
1424 jh->b_next_transaction->t_tid : 0,
Theodore Ts'o66a4cb12014-03-12 16:38:03 -04001425 jh->b_jlist);
1426 WARN_ON(1);
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -04001427 ret = -EINVAL;
1428 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001429 /* And this case is illegal: we can't reuse another
1430 * transaction's data buffer, ever. */
1431 goto out_unlock_bh;
1432 }
1433
1434 /* That test should have eliminated the following case: */
Mingming Cao40191912008-01-28 23:58:27 -05001435 J_ASSERT_JH(jh, jh->b_frozen_data == NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001436
1437 JBUFFER_TRACE(jh, "file as BJ_Metadata");
1438 spin_lock(&journal->j_list_lock);
Theodore Ts'o41a5b912013-07-01 08:12:41 -04001439 __jbd2_journal_file_buffer(jh, transaction, BJ_Metadata);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001440 spin_unlock(&journal->j_list_lock);
1441out_unlock_bh:
1442 jbd_unlock_bh_state(bh);
1443out:
1444 JBUFFER_TRACE(jh, "exit");
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -04001445 return ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001446}
1447
Dave Kleikamp470decc2006-10-11 01:20:57 -07001448/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001449 * void jbd2_journal_forget() - bforget() for potentially-journaled buffers.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001450 * @handle: transaction handle
1451 * @bh: bh to 'forget'
1452 *
1453 * We can only do the bforget if there are no commits pending against the
1454 * buffer. If the buffer is dirty in the current running transaction we
1455 * can safely unlink it.
1456 *
1457 * bh may not be a journalled buffer at all - it may be a non-JBD
1458 * buffer which came off the hashtable. Check for this.
1459 *
1460 * Decrements bh->b_count by one.
1461 *
1462 * Allow this call even if the handle has aborted --- it may be part of
1463 * the caller's cleanup after an abort.
1464 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001465int jbd2_journal_forget (handle_t *handle, struct buffer_head *bh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001466{
1467 transaction_t *transaction = handle->h_transaction;
Theodore Ts'o41a5b912013-07-01 08:12:41 -04001468 journal_t *journal;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001469 struct journal_head *jh;
1470 int drop_reserve = 0;
1471 int err = 0;
Josef Bacik1dfc3222008-04-17 10:38:59 -04001472 int was_modified = 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001473
Theodore Ts'o41a5b912013-07-01 08:12:41 -04001474 if (is_handle_aborted(handle))
1475 return -EROFS;
1476 journal = transaction->t_journal;
1477
Dave Kleikamp470decc2006-10-11 01:20:57 -07001478 BUFFER_TRACE(bh, "entry");
1479
1480 jbd_lock_bh_state(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001481
1482 if (!buffer_jbd(bh))
1483 goto not_jbd;
1484 jh = bh2jh(bh);
1485
1486 /* Critical error: attempting to delete a bitmap buffer, maybe?
1487 * Don't do any jbd operations, and return an error. */
1488 if (!J_EXPECT_JH(jh, !jh->b_committed_data,
1489 "inconsistent data on disk")) {
1490 err = -EIO;
1491 goto not_jbd;
1492 }
1493
Adam Buchbinder48fc7f72012-09-19 21:48:00 -04001494 /* keep track of whether or not this transaction modified us */
Josef Bacik1dfc3222008-04-17 10:38:59 -04001495 was_modified = jh->b_modified;
1496
Dave Kleikamp470decc2006-10-11 01:20:57 -07001497 /*
1498 * The buffer's going from the transaction, we must drop
1499 * all references -bzzz
1500 */
1501 jh->b_modified = 0;
1502
Theodore Ts'o41a5b912013-07-01 08:12:41 -04001503 if (jh->b_transaction == transaction) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001504 J_ASSERT_JH(jh, !jh->b_frozen_data);
1505
1506 /* If we are forgetting a buffer which is already part
1507 * of this transaction, then we can just drop it from
1508 * the transaction immediately. */
1509 clear_buffer_dirty(bh);
1510 clear_buffer_jbddirty(bh);
1511
1512 JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
1513
Josef Bacik1dfc3222008-04-17 10:38:59 -04001514 /*
1515 * we only want to drop a reference if this transaction
1516 * modified the buffer
1517 */
1518 if (was_modified)
1519 drop_reserve = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001520
1521 /*
1522 * We are no longer going to journal this buffer.
1523 * However, the commit of this transaction is still
1524 * important to the buffer: the delete that we are now
1525 * processing might obsolete an old log entry, so by
1526 * committing, we can satisfy the buffer's checkpoint.
1527 *
1528 * So, if we have a checkpoint on the buffer, we should
1529 * now refile the buffer on our BJ_Forget list so that
1530 * we know to remove the checkpoint after we commit.
1531 */
1532
Theodore Ts'o0bfea812014-03-09 00:56:58 -05001533 spin_lock(&journal->j_list_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001534 if (jh->b_cp_transaction) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001535 __jbd2_journal_temp_unlink_buffer(jh);
1536 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001537 } else {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001538 __jbd2_journal_unfile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001539 if (!buffer_jbd(bh)) {
1540 spin_unlock(&journal->j_list_lock);
1541 jbd_unlock_bh_state(bh);
1542 __bforget(bh);
1543 goto drop;
1544 }
1545 }
Theodore Ts'o0bfea812014-03-09 00:56:58 -05001546 spin_unlock(&journal->j_list_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001547 } else if (jh->b_transaction) {
1548 J_ASSERT_JH(jh, (jh->b_transaction ==
1549 journal->j_committing_transaction));
1550 /* However, if the buffer is still owned by a prior
1551 * (committing) transaction, we can't drop it yet... */
1552 JBUFFER_TRACE(jh, "belongs to older transaction");
1553 /* ... but we CAN drop it from the new transaction if we
1554 * have also modified it since the original commit. */
1555
1556 if (jh->b_next_transaction) {
1557 J_ASSERT(jh->b_next_transaction == transaction);
Theodore Ts'o0bfea812014-03-09 00:56:58 -05001558 spin_lock(&journal->j_list_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001559 jh->b_next_transaction = NULL;
Theodore Ts'o0bfea812014-03-09 00:56:58 -05001560 spin_unlock(&journal->j_list_lock);
Josef Bacik1dfc3222008-04-17 10:38:59 -04001561
1562 /*
1563 * only drop a reference if this transaction modified
1564 * the buffer
1565 */
1566 if (was_modified)
1567 drop_reserve = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001568 }
1569 }
1570
1571not_jbd:
Dave Kleikamp470decc2006-10-11 01:20:57 -07001572 jbd_unlock_bh_state(bh);
1573 __brelse(bh);
1574drop:
1575 if (drop_reserve) {
1576 /* no need to reserve log space for this block -bzzz */
1577 handle->h_buffer_credits++;
1578 }
1579 return err;
1580}
1581
1582/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001583 * int jbd2_journal_stop() - complete a transaction
Masanari Iidabd7ced92016-02-02 22:31:06 +09001584 * @handle: transaction to complete.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001585 *
1586 * All done for a particular handle.
1587 *
1588 * There is not much action needed here. We just return any remaining
1589 * buffer credits to the transaction and remove the handle. The only
1590 * complication is that we need to start a commit operation if the
1591 * filesystem is marked for synchronous update.
1592 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001593 * jbd2_journal_stop itself will not usually return an error, but it may
Dave Kleikamp470decc2006-10-11 01:20:57 -07001594 * do so in unusual circumstances. In particular, expect it to
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001595 * return -EIO if a jbd2_journal_abort has been executed since the
Dave Kleikamp470decc2006-10-11 01:20:57 -07001596 * transaction began.
1597 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001598int jbd2_journal_stop(handle_t *handle)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001599{
1600 transaction_t *transaction = handle->h_transaction;
Theodore Ts'o41a5b912013-07-01 08:12:41 -04001601 journal_t *journal;
1602 int err = 0, wait_for_commit = 0;
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001603 tid_t tid;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001604 pid_t pid;
1605
Lukas Czerner9d506592015-05-14 18:55:18 -04001606 if (!transaction) {
1607 /*
1608 * Handle is already detached from the transaction so
1609 * there is nothing to do other than decrease a refcount,
1610 * or free the handle if refcount drops to zero
1611 */
1612 if (--handle->h_ref > 0) {
1613 jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
1614 handle->h_ref);
1615 return err;
1616 } else {
1617 if (handle->h_rsv_handle)
1618 jbd2_free_handle(handle->h_rsv_handle);
1619 goto free_and_exit;
1620 }
1621 }
Theodore Ts'o41a5b912013-07-01 08:12:41 -04001622 journal = transaction->t_journal;
1623
Dave Kleikamp470decc2006-10-11 01:20:57 -07001624 J_ASSERT(journal_current_handle() == handle);
1625
1626 if (is_handle_aborted(handle))
1627 err = -EIO;
Theodore Ts'o41a5b912013-07-01 08:12:41 -04001628 else
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001629 J_ASSERT(atomic_read(&transaction->t_updates) > 0);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001630
1631 if (--handle->h_ref > 0) {
1632 jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
1633 handle->h_ref);
1634 return err;
1635 }
1636
1637 jbd_debug(4, "Handle %p going down\n", handle);
Theodore Ts'o343d9c22013-02-08 13:00:22 -05001638 trace_jbd2_handle_stats(journal->j_fs_dev->bd_dev,
Theodore Ts'o41a5b912013-07-01 08:12:41 -04001639 transaction->t_tid,
Theodore Ts'o343d9c22013-02-08 13:00:22 -05001640 handle->h_type, handle->h_line_no,
1641 jiffies - handle->h_start_jiffies,
1642 handle->h_sync, handle->h_requested_credits,
1643 (handle->h_requested_credits -
1644 handle->h_buffer_credits));
Dave Kleikamp470decc2006-10-11 01:20:57 -07001645
1646 /*
1647 * Implement synchronous transaction batching. If the handle
1648 * was synchronous, don't force a commit immediately. Let's
Josef Bacike07f7182008-11-26 01:14:26 -05001649 * yield and let another thread piggyback onto this
1650 * transaction. Keep doing that while new threads continue to
1651 * arrive. It doesn't cost much - we're about to run a commit
1652 * and sleep on IO anyway. Speeds up many-threaded, many-dir
1653 * operations by 30x or more...
Dave Kleikamp470decc2006-10-11 01:20:57 -07001654 *
Josef Bacike07f7182008-11-26 01:14:26 -05001655 * We try and optimize the sleep time against what the
1656 * underlying disk can do, instead of having a static sleep
1657 * time. This is useful for the case where our storage is so
1658 * fast that it is more optimal to go ahead and force a flush
1659 * and wait for the transaction to be committed than it is to
1660 * wait for an arbitrary amount of time for new writers to
1661 * join the transaction. We achieve this by measuring how
1662 * long it takes to commit a transaction, and compare it with
1663 * how long this transaction has been running, and if run time
1664 * < commit time then we sleep for the delta and commit. This
1665 * greatly helps super fast disks that would see slowdowns as
1666 * more threads started doing fsyncs.
1667 *
1668 * But don't do this if this process was the most recent one
1669 * to perform a synchronous write. We do this to detect the
1670 * case where a single process is doing a stream of sync
1671 * writes. No point in waiting for joiners in that case.
Eric Sandeen5dd21422014-07-05 19:18:22 -04001672 *
1673 * Setting max_batch_time to 0 disables this completely.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001674 */
1675 pid = current->pid;
Eric Sandeen5dd21422014-07-05 19:18:22 -04001676 if (handle->h_sync && journal->j_last_sync_writer != pid &&
1677 journal->j_max_batch_time) {
Josef Bacike07f7182008-11-26 01:14:26 -05001678 u64 commit_time, trans_time;
1679
Dave Kleikamp470decc2006-10-11 01:20:57 -07001680 journal->j_last_sync_writer = pid;
Josef Bacike07f7182008-11-26 01:14:26 -05001681
Theodore Ts'oa931da62010-08-03 21:35:12 -04001682 read_lock(&journal->j_state_lock);
Josef Bacike07f7182008-11-26 01:14:26 -05001683 commit_time = journal->j_average_commit_time;
Theodore Ts'oa931da62010-08-03 21:35:12 -04001684 read_unlock(&journal->j_state_lock);
Josef Bacike07f7182008-11-26 01:14:26 -05001685
1686 trans_time = ktime_to_ns(ktime_sub(ktime_get(),
1687 transaction->t_start_time));
1688
Theodore Ts'o30773842009-01-03 20:27:38 -05001689 commit_time = max_t(u64, commit_time,
1690 1000*journal->j_min_batch_time);
Josef Bacike07f7182008-11-26 01:14:26 -05001691 commit_time = min_t(u64, commit_time,
Theodore Ts'o30773842009-01-03 20:27:38 -05001692 1000*journal->j_max_batch_time);
Josef Bacike07f7182008-11-26 01:14:26 -05001693
1694 if (trans_time < commit_time) {
1695 ktime_t expires = ktime_add_ns(ktime_get(),
1696 commit_time);
1697 set_current_state(TASK_UNINTERRUPTIBLE);
1698 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1699 }
Dave Kleikamp470decc2006-10-11 01:20:57 -07001700 }
1701
Theodore Ts'o70585482009-03-25 23:35:46 -04001702 if (handle->h_sync)
1703 transaction->t_synchronous_commit = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001704 current->journal_info = NULL;
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001705 atomic_sub(handle->h_buffer_credits,
1706 &transaction->t_outstanding_credits);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001707
1708 /*
1709 * If the handle is marked SYNC, we need to set another commit
1710 * going! We also want to force a commit if the current
1711 * transaction is occupying too much of the log, or if the
1712 * transaction is too old now.
1713 */
1714 if (handle->h_sync ||
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001715 (atomic_read(&transaction->t_outstanding_credits) >
1716 journal->j_max_transaction_buffers) ||
1717 time_after_eq(jiffies, transaction->t_expires)) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001718 /* Do this even for aborted journals: an abort still
1719 * completes the commit thread, it just doesn't write
1720 * anything to disk. */
Dave Kleikamp470decc2006-10-11 01:20:57 -07001721
Dave Kleikamp470decc2006-10-11 01:20:57 -07001722 jbd_debug(2, "transaction too old, requesting commit for "
1723 "handle %p\n", handle);
1724 /* This is non-blocking */
Theodore Ts'oc35a56a2010-05-16 05:00:00 -04001725 jbd2_log_start_commit(journal, transaction->t_tid);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001726
1727 /*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001728 * Special case: JBD2_SYNC synchronous updates require us
Dave Kleikamp470decc2006-10-11 01:20:57 -07001729 * to wait for the commit to complete.
1730 */
1731 if (handle->h_sync && !(current->flags & PF_MEMALLOC))
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001732 wait_for_commit = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001733 }
1734
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001735 /*
1736 * Once we drop t_updates, if it goes to zero the transaction
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001737 * could start committing on us and eventually disappear. So
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001738 * once we do this, we must not dereference transaction
1739 * pointer again.
1740 */
1741 tid = transaction->t_tid;
1742 if (atomic_dec_and_test(&transaction->t_updates)) {
1743 wake_up(&journal->j_wait_updates);
1744 if (journal->j_barrier_count)
1745 wake_up(&journal->j_wait_transaction_locked);
1746 }
1747
Jan Karaab714af2016-06-30 11:39:38 -04001748 rwsem_release(&journal->j_trans_commit_map, 1, _THIS_IP_);
Jan Kara7a4b1882016-06-30 11:30:21 -04001749
Theodore Ts'oa51dca92010-08-02 08:43:25 -04001750 if (wait_for_commit)
1751 err = jbd2_log_wait_commit(journal, tid);
1752
Jan Kara8f7d89f2013-06-04 12:35:11 -04001753 if (handle->h_rsv_handle)
1754 jbd2_journal_free_reserved(handle->h_rsv_handle);
Theodore Ts'o41a5b912013-07-01 08:12:41 -04001755free_and_exit:
Mingming Caoaf1e76d2007-10-16 18:38:25 -04001756 jbd2_free_handle(handle);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001757 return err;
1758}
1759
Dave Kleikamp470decc2006-10-11 01:20:57 -07001760/*
1761 *
1762 * List management code snippets: various functions for manipulating the
1763 * transaction buffer lists.
1764 *
1765 */
1766
1767/*
1768 * Append a buffer to a transaction list, given the transaction's list head
1769 * pointer.
1770 *
1771 * j_list_lock is held.
1772 *
1773 * jbd_lock_bh_state(jh2bh(jh)) is held.
1774 */
1775
1776static inline void
1777__blist_add_buffer(struct journal_head **list, struct journal_head *jh)
1778{
1779 if (!*list) {
1780 jh->b_tnext = jh->b_tprev = jh;
1781 *list = jh;
1782 } else {
1783 /* Insert at the tail of the list to preserve order */
1784 struct journal_head *first = *list, *last = first->b_tprev;
1785 jh->b_tprev = last;
1786 jh->b_tnext = first;
1787 last->b_tnext = first->b_tprev = jh;
1788 }
1789}
1790
1791/*
1792 * Remove a buffer from a transaction list, given the transaction's list
1793 * head pointer.
1794 *
1795 * Called with j_list_lock held, and the journal may not be locked.
1796 *
1797 * jbd_lock_bh_state(jh2bh(jh)) is held.
1798 */
1799
1800static inline void
1801__blist_del_buffer(struct journal_head **list, struct journal_head *jh)
1802{
1803 if (*list == jh) {
1804 *list = jh->b_tnext;
1805 if (*list == jh)
1806 *list = NULL;
1807 }
1808 jh->b_tprev->b_tnext = jh->b_tnext;
1809 jh->b_tnext->b_tprev = jh->b_tprev;
1810}
1811
1812/*
1813 * Remove a buffer from the appropriate transaction list.
1814 *
1815 * Note that this function can *change* the value of
Jan Karaf5113ef2013-06-04 12:01:45 -04001816 * bh->b_transaction->t_buffers, t_forget, t_shadow_list, t_log_list or
1817 * t_reserved_list. If the caller is holding onto a copy of one of these
1818 * pointers, it could go bad. Generally the caller needs to re-read the
1819 * pointer from the transaction_t.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001820 *
Jan Kara5bebccf2012-03-13 22:25:06 -04001821 * Called under j_list_lock.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001822 */
Jan Kara5bebccf2012-03-13 22:25:06 -04001823static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001824{
1825 struct journal_head **list = NULL;
1826 transaction_t *transaction;
1827 struct buffer_head *bh = jh2bh(jh);
1828
1829 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1830 transaction = jh->b_transaction;
1831 if (transaction)
1832 assert_spin_locked(&transaction->t_journal->j_list_lock);
1833
1834 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1835 if (jh->b_jlist != BJ_None)
Mingming Cao40191912008-01-28 23:58:27 -05001836 J_ASSERT_JH(jh, transaction != NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001837
1838 switch (jh->b_jlist) {
1839 case BJ_None:
1840 return;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001841 case BJ_Metadata:
1842 transaction->t_nr_buffers--;
1843 J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
1844 list = &transaction->t_buffers;
1845 break;
1846 case BJ_Forget:
1847 list = &transaction->t_forget;
1848 break;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001849 case BJ_Shadow:
1850 list = &transaction->t_shadow_list;
1851 break;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001852 case BJ_Reserved:
1853 list = &transaction->t_reserved_list;
1854 break;
Dave Kleikamp470decc2006-10-11 01:20:57 -07001855 }
1856
1857 __blist_del_buffer(list, jh);
1858 jh->b_jlist = BJ_None;
1859 if (test_clear_buffer_jbddirty(bh))
1860 mark_buffer_dirty(bh); /* Expose it to the VM */
1861}
1862
Jan Karade1b7942011-06-13 15:38:22 -04001863/*
1864 * Remove buffer from all transactions.
1865 *
1866 * Called with bh_state lock and j_list_lock
1867 *
1868 * jh and bh may be already freed when this function returns.
1869 */
1870static void __jbd2_journal_unfile_buffer(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001871{
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001872 __jbd2_journal_temp_unlink_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001873 jh->b_transaction = NULL;
Jan Karade1b7942011-06-13 15:38:22 -04001874 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001875}
1876
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001877void jbd2_journal_unfile_buffer(journal_t *journal, struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001878{
Jan Karade1b7942011-06-13 15:38:22 -04001879 struct buffer_head *bh = jh2bh(jh);
1880
1881 /* Get reference so that buffer cannot be freed before we unlock it */
1882 get_bh(bh);
1883 jbd_lock_bh_state(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001884 spin_lock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001885 __jbd2_journal_unfile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001886 spin_unlock(&journal->j_list_lock);
Jan Karade1b7942011-06-13 15:38:22 -04001887 jbd_unlock_bh_state(bh);
1888 __brelse(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001889}
1890
1891/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001892 * Called from jbd2_journal_try_to_free_buffers().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001893 *
1894 * Called under jbd_lock_bh_state(bh)
1895 */
1896static void
1897__journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh)
1898{
1899 struct journal_head *jh;
1900
1901 jh = bh2jh(bh);
1902
1903 if (buffer_locked(bh) || buffer_dirty(bh))
1904 goto out;
1905
Theodore Ts'od2eb0b92014-03-09 00:07:19 -05001906 if (jh->b_next_transaction != NULL || jh->b_transaction != NULL)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001907 goto out;
1908
1909 spin_lock(&journal->j_list_lock);
Theodore Ts'od2eb0b92014-03-09 00:07:19 -05001910 if (jh->b_cp_transaction != NULL) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07001911 /* written-back checkpointed metadata buffer */
Jan Karac254c9e2012-03-13 22:27:44 -04001912 JBUFFER_TRACE(jh, "remove from checkpoint list");
1913 __jbd2_journal_remove_checkpoint(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001914 }
1915 spin_unlock(&journal->j_list_lock);
1916out:
1917 return;
1918}
1919
Dave Kleikamp470decc2006-10-11 01:20:57 -07001920/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001921 * int jbd2_journal_try_to_free_buffers() - try to free page buffers.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001922 * @journal: journal for operation
1923 * @page: to try and free
Mingming Cao530576b2008-07-13 21:06:39 -04001924 * @gfp_mask: we use the mask to detect how hard should we try to release
Mel Gormand0164ad2015-11-06 16:28:21 -08001925 * buffers. If __GFP_DIRECT_RECLAIM and __GFP_FS is set, we wait for commit
1926 * code to release the buffers.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001927 *
1928 *
1929 * For all the buffers on this page,
1930 * if they are fully written out ordered data, move them onto BUF_CLEAN
1931 * so try_to_free_buffers() can reap them.
1932 *
1933 * This function returns non-zero if we wish try_to_free_buffers()
1934 * to be called. We do this if the page is releasable by try_to_free_buffers().
1935 * We also do it if the page has locked or dirty buffers and the caller wants
1936 * us to perform sync or async writeout.
1937 *
1938 * This complicates JBD locking somewhat. We aren't protected by the
1939 * BKL here. We wish to remove the buffer from its committing or
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001940 * running transaction's ->t_datalist via __jbd2_journal_unfile_buffer.
Dave Kleikamp470decc2006-10-11 01:20:57 -07001941 *
1942 * This may *change* the value of transaction_t->t_datalist, so anyone
1943 * who looks at t_datalist needs to lock against this function.
1944 *
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001945 * Even worse, someone may be doing a jbd2_journal_dirty_data on this
1946 * buffer. So we need to lock against that. jbd2_journal_dirty_data()
Dave Kleikamp470decc2006-10-11 01:20:57 -07001947 * will come out of the lock with the buffer dirty, which makes it
1948 * ineligible for release here.
1949 *
1950 * Who else is affected by this? hmm... Really the only contender
1951 * is do_get_write_access() - it could be looking at the buffer while
1952 * journal_try_to_free_buffer() is changing its state. But that
1953 * cannot happen because we never reallocate freed data as metadata
1954 * while the data is part of a transaction. Yes?
Mingming Cao530576b2008-07-13 21:06:39 -04001955 *
1956 * Return 0 on failure, 1 on success
Dave Kleikamp470decc2006-10-11 01:20:57 -07001957 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001958int jbd2_journal_try_to_free_buffers(journal_t *journal,
Mingming Cao530576b2008-07-13 21:06:39 -04001959 struct page *page, gfp_t gfp_mask)
Dave Kleikamp470decc2006-10-11 01:20:57 -07001960{
1961 struct buffer_head *head;
1962 struct buffer_head *bh;
1963 int ret = 0;
1964
1965 J_ASSERT(PageLocked(page));
1966
1967 head = page_buffers(page);
1968 bh = head;
1969 do {
1970 struct journal_head *jh;
1971
1972 /*
1973 * We take our own ref against the journal_head here to avoid
1974 * having to add tons of locking around each instance of
Mingming Cao530576b2008-07-13 21:06:39 -04001975 * jbd2_journal_put_journal_head().
Dave Kleikamp470decc2006-10-11 01:20:57 -07001976 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001977 jh = jbd2_journal_grab_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001978 if (!jh)
1979 continue;
1980
1981 jbd_lock_bh_state(bh);
1982 __journal_try_to_free_buffer(journal, bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07001983 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07001984 jbd_unlock_bh_state(bh);
1985 if (buffer_jbd(bh))
1986 goto busy;
1987 } while ((bh = bh->b_this_page) != head);
Mingming Cao530576b2008-07-13 21:06:39 -04001988
Dave Kleikamp470decc2006-10-11 01:20:57 -07001989 ret = try_to_free_buffers(page);
Mingming Cao530576b2008-07-13 21:06:39 -04001990
Dave Kleikamp470decc2006-10-11 01:20:57 -07001991busy:
1992 return ret;
1993}
1994
1995/*
1996 * This buffer is no longer needed. If it is on an older transaction's
1997 * checkpoint list we need to record it on this transaction's forget list
1998 * to pin this buffer (and hence its checkpointing transaction) down until
1999 * this transaction commits. If the buffer isn't on a checkpoint list, we
2000 * release it.
2001 * Returns non-zero if JBD no longer has an interest in the buffer.
2002 *
2003 * Called under j_list_lock.
2004 *
2005 * Called under jbd_lock_bh_state(bh).
2006 */
2007static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
2008{
2009 int may_free = 1;
2010 struct buffer_head *bh = jh2bh(jh);
2011
Dave Kleikamp470decc2006-10-11 01:20:57 -07002012 if (jh->b_cp_transaction) {
2013 JBUFFER_TRACE(jh, "on running+cp transaction");
Jan Karade1b7942011-06-13 15:38:22 -04002014 __jbd2_journal_temp_unlink_buffer(jh);
Jan Karaf91d1d02009-07-13 16:16:20 -04002015 /*
2016 * We don't want to write the buffer anymore, clear the
2017 * bit so that we don't confuse checks in
2018 * __journal_file_buffer
2019 */
2020 clear_buffer_dirty(bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002021 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002022 may_free = 0;
2023 } else {
2024 JBUFFER_TRACE(jh, "on running transaction");
Jan Karade1b7942011-06-13 15:38:22 -04002025 __jbd2_journal_unfile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002026 }
2027 return may_free;
2028}
2029
2030/*
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002031 * jbd2_journal_invalidatepage
Dave Kleikamp470decc2006-10-11 01:20:57 -07002032 *
2033 * This code is tricky. It has a number of cases to deal with.
2034 *
2035 * There are two invariants which this code relies on:
2036 *
2037 * i_size must be updated on disk before we start calling invalidatepage on the
2038 * data.
2039 *
2040 * This is done in ext3 by defining an ext3_setattr method which
2041 * updates i_size before truncate gets going. By maintaining this
2042 * invariant, we can be sure that it is safe to throw away any buffers
2043 * attached to the current transaction: once the transaction commits,
2044 * we know that the data will not be needed.
2045 *
2046 * Note however that we can *not* throw away data belonging to the
2047 * previous, committing transaction!
2048 *
2049 * Any disk blocks which *are* part of the previous, committing
2050 * transaction (and which therefore cannot be discarded immediately) are
2051 * not going to be reused in the new running transaction
2052 *
2053 * The bitmap committed_data images guarantee this: any block which is
2054 * allocated in one transaction and removed in the next will be marked
2055 * as in-use in the committed_data bitmap, so cannot be reused until
2056 * the next transaction to delete the block commits. This means that
2057 * leaving committing buffers dirty is quite safe: the disk blocks
2058 * cannot be reallocated to a different file and so buffer aliasing is
2059 * not possible.
2060 *
2061 *
2062 * The above applies mainly to ordered data mode. In writeback mode we
2063 * don't make guarantees about the order in which data hits disk --- in
2064 * particular we don't guarantee that new dirty data is flushed before
2065 * transaction commit --- so it is always safe just to discard data
2066 * immediately in that mode. --sct
2067 */
2068
2069/*
2070 * The journal_unmap_buffer helper function returns zero if the buffer
2071 * concerned remains pinned as an anonymous buffer belonging to an older
2072 * transaction.
2073 *
2074 * We're outside-transaction here. Either or both of j_running_transaction
2075 * and j_committing_transaction may be NULL.
2076 */
Jan Karab794e7a2012-09-26 23:11:13 -04002077static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh,
2078 int partial_page)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002079{
2080 transaction_t *transaction;
2081 struct journal_head *jh;
2082 int may_free = 1;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002083
2084 BUFFER_TRACE(bh, "entry");
2085
2086 /*
2087 * It is safe to proceed here without the j_list_lock because the
2088 * buffers cannot be stolen by try_to_free_buffers as long as we are
2089 * holding the page lock. --sct
2090 */
2091
2092 if (!buffer_jbd(bh))
2093 goto zap_buffer_unlocked;
2094
Jan Kara87c89c22008-07-11 19:27:31 -04002095 /* OK, we have data buffer in journaled mode */
Theodore Ts'oa931da62010-08-03 21:35:12 -04002096 write_lock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002097 jbd_lock_bh_state(bh);
2098 spin_lock(&journal->j_list_lock);
2099
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002100 jh = jbd2_journal_grab_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002101 if (!jh)
2102 goto zap_buffer_no_jh;
2103
dingdinghuaba869022010-02-15 16:35:42 -05002104 /*
2105 * We cannot remove the buffer from checkpoint lists until the
2106 * transaction adding inode to orphan list (let's call it T)
2107 * is committed. Otherwise if the transaction changing the
2108 * buffer would be cleaned from the journal before T is
2109 * committed, a crash will cause that the correct contents of
2110 * the buffer will be lost. On the other hand we have to
2111 * clear the buffer dirty bit at latest at the moment when the
2112 * transaction marking the buffer as freed in the filesystem
2113 * structures is committed because from that moment on the
Jan Karab794e7a2012-09-26 23:11:13 -04002114 * block can be reallocated and used by a different page.
dingdinghuaba869022010-02-15 16:35:42 -05002115 * Since the block hasn't been freed yet but the inode has
2116 * already been added to orphan list, it is safe for us to add
2117 * the buffer to BJ_Forget list of the newest transaction.
Jan Karab794e7a2012-09-26 23:11:13 -04002118 *
2119 * Also we have to clear buffer_mapped flag of a truncated buffer
2120 * because the buffer_head may be attached to the page straddling
2121 * i_size (can happen only when blocksize < pagesize) and thus the
2122 * buffer_head can be reused when the file is extended again. So we end
2123 * up keeping around invalidated buffers attached to transactions'
2124 * BJ_Forget list just to stop checkpointing code from cleaning up
2125 * the transaction this buffer was modified in.
dingdinghuaba869022010-02-15 16:35:42 -05002126 */
Dave Kleikamp470decc2006-10-11 01:20:57 -07002127 transaction = jh->b_transaction;
2128 if (transaction == NULL) {
2129 /* First case: not on any transaction. If it
2130 * has no checkpoint link, then we can zap it:
2131 * it's a writeback-mode buffer so we don't care
2132 * if it hits disk safely. */
2133 if (!jh->b_cp_transaction) {
2134 JBUFFER_TRACE(jh, "not on any transaction: zap");
2135 goto zap_buffer;
2136 }
2137
2138 if (!buffer_dirty(bh)) {
2139 /* bdflush has written it. We can drop it now */
Jan Karabc23f0c2015-11-24 15:34:35 -05002140 __jbd2_journal_remove_checkpoint(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002141 goto zap_buffer;
2142 }
2143
2144 /* OK, it must be in the journal but still not
2145 * written fully to disk: it's metadata or
2146 * journaled data... */
2147
2148 if (journal->j_running_transaction) {
2149 /* ... and once the current transaction has
2150 * committed, the buffer won't be needed any
2151 * longer. */
2152 JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
Jan Karab794e7a2012-09-26 23:11:13 -04002153 may_free = __dispose_buffer(jh,
Dave Kleikamp470decc2006-10-11 01:20:57 -07002154 journal->j_running_transaction);
Jan Karab794e7a2012-09-26 23:11:13 -04002155 goto zap_buffer;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002156 } else {
2157 /* There is no currently-running transaction. So the
2158 * orphan record which we wrote for this file must have
2159 * passed into commit. We must attach this buffer to
2160 * the committing transaction, if it exists. */
2161 if (journal->j_committing_transaction) {
2162 JBUFFER_TRACE(jh, "give to committing trans");
Jan Karab794e7a2012-09-26 23:11:13 -04002163 may_free = __dispose_buffer(jh,
Dave Kleikamp470decc2006-10-11 01:20:57 -07002164 journal->j_committing_transaction);
Jan Karab794e7a2012-09-26 23:11:13 -04002165 goto zap_buffer;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002166 } else {
2167 /* The orphan record's transaction has
2168 * committed. We can cleanse this buffer */
2169 clear_buffer_jbddirty(bh);
Jan Karabc23f0c2015-11-24 15:34:35 -05002170 __jbd2_journal_remove_checkpoint(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002171 goto zap_buffer;
2172 }
2173 }
2174 } else if (transaction == journal->j_committing_transaction) {
Eric Sandeen9b579882006-10-28 10:38:28 -07002175 JBUFFER_TRACE(jh, "on committing transaction");
Dave Kleikamp470decc2006-10-11 01:20:57 -07002176 /*
dingdinghuaba869022010-02-15 16:35:42 -05002177 * The buffer is committing, we simply cannot touch
Jan Karab794e7a2012-09-26 23:11:13 -04002178 * it. If the page is straddling i_size we have to wait
2179 * for commit and try again.
2180 */
2181 if (partial_page) {
Jan Karab794e7a2012-09-26 23:11:13 -04002182 jbd2_journal_put_journal_head(jh);
2183 spin_unlock(&journal->j_list_lock);
2184 jbd_unlock_bh_state(bh);
2185 write_unlock(&journal->j_state_lock);
Jan Kara53e87262012-12-25 13:29:52 -05002186 return -EBUSY;
Jan Karab794e7a2012-09-26 23:11:13 -04002187 }
2188 /*
2189 * OK, buffer won't be reachable after truncate. We just set
2190 * j_next_transaction to the running transaction (if there is
2191 * one) and mark buffer as freed so that commit code knows it
2192 * should clear dirty bits when it is done with the buffer.
dingdinghuaba869022010-02-15 16:35:42 -05002193 */
Dave Kleikamp470decc2006-10-11 01:20:57 -07002194 set_buffer_freed(bh);
dingdinghuaba869022010-02-15 16:35:42 -05002195 if (journal->j_running_transaction && buffer_jbddirty(bh))
2196 jh->b_next_transaction = journal->j_running_transaction;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002197 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002198 spin_unlock(&journal->j_list_lock);
2199 jbd_unlock_bh_state(bh);
Theodore Ts'oa931da62010-08-03 21:35:12 -04002200 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002201 return 0;
2202 } else {
2203 /* Good, the buffer belongs to the running transaction.
2204 * We are writing our own transaction's data, not any
2205 * previous one's, so it is safe to throw it away
2206 * (remember that we expect the filesystem to have set
2207 * i_size already for this truncate so recovery will not
2208 * expose the disk blocks we are discarding here.) */
2209 J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
Eric Sandeen9b579882006-10-28 10:38:28 -07002210 JBUFFER_TRACE(jh, "on running transaction");
Dave Kleikamp470decc2006-10-11 01:20:57 -07002211 may_free = __dispose_buffer(jh, transaction);
2212 }
2213
2214zap_buffer:
Jan Karab794e7a2012-09-26 23:11:13 -04002215 /*
2216 * This is tricky. Although the buffer is truncated, it may be reused
2217 * if blocksize < pagesize and it is attached to the page straddling
2218 * EOF. Since the buffer might have been added to BJ_Forget list of the
2219 * running transaction, journal_get_write_access() won't clear
2220 * b_modified and credit accounting gets confused. So clear b_modified
2221 * here.
2222 */
2223 jh->b_modified = 0;
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002224 jbd2_journal_put_journal_head(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002225zap_buffer_no_jh:
2226 spin_unlock(&journal->j_list_lock);
2227 jbd_unlock_bh_state(bh);
Theodore Ts'oa931da62010-08-03 21:35:12 -04002228 write_unlock(&journal->j_state_lock);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002229zap_buffer_unlocked:
2230 clear_buffer_dirty(bh);
2231 J_ASSERT_BH(bh, !buffer_jbddirty(bh));
2232 clear_buffer_mapped(bh);
2233 clear_buffer_req(bh);
2234 clear_buffer_new(bh);
Eric Sandeen15291162012-02-20 17:53:01 -05002235 clear_buffer_delay(bh);
2236 clear_buffer_unwritten(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002237 bh->b_bdev = NULL;
2238 return may_free;
2239}
2240
2241/**
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002242 * void jbd2_journal_invalidatepage()
Dave Kleikamp470decc2006-10-11 01:20:57 -07002243 * @journal: journal to use for flush...
2244 * @page: page to flush
Lukas Czerner259709b2013-05-21 23:20:03 -04002245 * @offset: start of the range to invalidate
2246 * @length: length of the range to invalidate
Dave Kleikamp470decc2006-10-11 01:20:57 -07002247 *
Lukas Czerner259709b2013-05-21 23:20:03 -04002248 * Reap page buffers containing data after in the specified range in page.
2249 * Can return -EBUSY if buffers are part of the committing transaction and
2250 * the page is straddling i_size. Caller then has to wait for current commit
2251 * and try again.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002252 */
Jan Kara53e87262012-12-25 13:29:52 -05002253int jbd2_journal_invalidatepage(journal_t *journal,
2254 struct page *page,
Lukas Czerner259709b2013-05-21 23:20:03 -04002255 unsigned int offset,
2256 unsigned int length)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002257{
2258 struct buffer_head *head, *bh, *next;
Lukas Czerner259709b2013-05-21 23:20:03 -04002259 unsigned int stop = offset + length;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002260 unsigned int curr_off = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002261 int partial_page = (offset || length < PAGE_SIZE);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002262 int may_free = 1;
Jan Kara53e87262012-12-25 13:29:52 -05002263 int ret = 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002264
2265 if (!PageLocked(page))
2266 BUG();
2267 if (!page_has_buffers(page))
Jan Kara53e87262012-12-25 13:29:52 -05002268 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002269
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002270 BUG_ON(stop > PAGE_SIZE || stop < length);
Lukas Czerner259709b2013-05-21 23:20:03 -04002271
Dave Kleikamp470decc2006-10-11 01:20:57 -07002272 /* We will potentially be playing with lists other than just the
2273 * data lists (especially for journaled data mode), so be
2274 * cautious in our locking. */
2275
2276 head = bh = page_buffers(page);
2277 do {
2278 unsigned int next_off = curr_off + bh->b_size;
2279 next = bh->b_this_page;
2280
Lukas Czerner259709b2013-05-21 23:20:03 -04002281 if (next_off > stop)
2282 return 0;
2283
Dave Kleikamp470decc2006-10-11 01:20:57 -07002284 if (offset <= curr_off) {
2285 /* This block is wholly outside the truncation point */
2286 lock_buffer(bh);
Lukas Czerner259709b2013-05-21 23:20:03 -04002287 ret = journal_unmap_buffer(journal, bh, partial_page);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002288 unlock_buffer(bh);
Jan Kara53e87262012-12-25 13:29:52 -05002289 if (ret < 0)
2290 return ret;
2291 may_free &= ret;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002292 }
2293 curr_off = next_off;
2294 bh = next;
2295
2296 } while (bh != head);
2297
Lukas Czerner259709b2013-05-21 23:20:03 -04002298 if (!partial_page) {
Dave Kleikamp470decc2006-10-11 01:20:57 -07002299 if (may_free && try_to_free_buffers(page))
2300 J_ASSERT(!page_has_buffers(page));
2301 }
Jan Kara53e87262012-12-25 13:29:52 -05002302 return 0;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002303}
2304
2305/*
2306 * File a buffer on the given transaction list.
2307 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002308void __jbd2_journal_file_buffer(struct journal_head *jh,
Dave Kleikamp470decc2006-10-11 01:20:57 -07002309 transaction_t *transaction, int jlist)
2310{
2311 struct journal_head **list = NULL;
2312 int was_dirty = 0;
2313 struct buffer_head *bh = jh2bh(jh);
2314
2315 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
2316 assert_spin_locked(&transaction->t_journal->j_list_lock);
2317
2318 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
2319 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
Mingming Cao40191912008-01-28 23:58:27 -05002320 jh->b_transaction == NULL);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002321
2322 if (jh->b_transaction && jh->b_jlist == jlist)
2323 return;
2324
Dave Kleikamp470decc2006-10-11 01:20:57 -07002325 if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
2326 jlist == BJ_Shadow || jlist == BJ_Forget) {
Jan Karaf91d1d02009-07-13 16:16:20 -04002327 /*
2328 * For metadata buffers, we track dirty bit in buffer_jbddirty
2329 * instead of buffer_dirty. We should not see a dirty bit set
2330 * here because we clear it in do_get_write_access but e.g.
2331 * tune2fs can modify the sb and set the dirty bit at any time
2332 * so we try to gracefully handle that.
2333 */
2334 if (buffer_dirty(bh))
2335 warn_dirty_buffer(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002336 if (test_clear_buffer_dirty(bh) ||
2337 test_clear_buffer_jbddirty(bh))
2338 was_dirty = 1;
2339 }
2340
2341 if (jh->b_transaction)
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002342 __jbd2_journal_temp_unlink_buffer(jh);
Jan Karade1b7942011-06-13 15:38:22 -04002343 else
2344 jbd2_journal_grab_journal_head(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002345 jh->b_transaction = transaction;
2346
2347 switch (jlist) {
2348 case BJ_None:
2349 J_ASSERT_JH(jh, !jh->b_committed_data);
2350 J_ASSERT_JH(jh, !jh->b_frozen_data);
2351 return;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002352 case BJ_Metadata:
2353 transaction->t_nr_buffers++;
2354 list = &transaction->t_buffers;
2355 break;
2356 case BJ_Forget:
2357 list = &transaction->t_forget;
2358 break;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002359 case BJ_Shadow:
2360 list = &transaction->t_shadow_list;
2361 break;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002362 case BJ_Reserved:
2363 list = &transaction->t_reserved_list;
2364 break;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002365 }
2366
2367 __blist_add_buffer(list, jh);
2368 jh->b_jlist = jlist;
2369
2370 if (was_dirty)
2371 set_buffer_jbddirty(bh);
2372}
2373
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002374void jbd2_journal_file_buffer(struct journal_head *jh,
Dave Kleikamp470decc2006-10-11 01:20:57 -07002375 transaction_t *transaction, int jlist)
2376{
2377 jbd_lock_bh_state(jh2bh(jh));
2378 spin_lock(&transaction->t_journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002379 __jbd2_journal_file_buffer(jh, transaction, jlist);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002380 spin_unlock(&transaction->t_journal->j_list_lock);
2381 jbd_unlock_bh_state(jh2bh(jh));
2382}
2383
2384/*
2385 * Remove a buffer from its current buffer list in preparation for
2386 * dropping it from its current transaction entirely. If the buffer has
2387 * already started to be used by a subsequent transaction, refile the
2388 * buffer on that transaction's metadata list.
2389 *
Jan Karade1b7942011-06-13 15:38:22 -04002390 * Called under j_list_lock
Dave Kleikamp470decc2006-10-11 01:20:57 -07002391 * Called under jbd_lock_bh_state(jh2bh(jh))
Jan Karade1b7942011-06-13 15:38:22 -04002392 *
2393 * jh and bh may be already free when this function returns
Dave Kleikamp470decc2006-10-11 01:20:57 -07002394 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002395void __jbd2_journal_refile_buffer(struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002396{
dingdinghuaba869022010-02-15 16:35:42 -05002397 int was_dirty, jlist;
Dave Kleikamp470decc2006-10-11 01:20:57 -07002398 struct buffer_head *bh = jh2bh(jh);
2399
2400 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
2401 if (jh->b_transaction)
2402 assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
2403
2404 /* If the buffer is now unused, just drop it. */
2405 if (jh->b_next_transaction == NULL) {
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002406 __jbd2_journal_unfile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002407 return;
2408 }
2409
2410 /*
2411 * It has been modified by a later transaction: add it to the new
2412 * transaction's metadata list.
2413 */
2414
2415 was_dirty = test_clear_buffer_jbddirty(bh);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002416 __jbd2_journal_temp_unlink_buffer(jh);
Jan Karade1b7942011-06-13 15:38:22 -04002417 /*
2418 * We set b_transaction here because b_next_transaction will inherit
2419 * our jh reference and thus __jbd2_journal_file_buffer() must not
2420 * take a new one.
2421 */
Dave Kleikamp470decc2006-10-11 01:20:57 -07002422 jh->b_transaction = jh->b_next_transaction;
2423 jh->b_next_transaction = NULL;
dingdinghuaba869022010-02-15 16:35:42 -05002424 if (buffer_freed(bh))
2425 jlist = BJ_Forget;
2426 else if (jh->b_modified)
2427 jlist = BJ_Metadata;
2428 else
2429 jlist = BJ_Reserved;
2430 __jbd2_journal_file_buffer(jh, jh->b_transaction, jlist);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002431 J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
2432
2433 if (was_dirty)
2434 set_buffer_jbddirty(bh);
2435}
2436
2437/*
Jan Karade1b7942011-06-13 15:38:22 -04002438 * __jbd2_journal_refile_buffer() with necessary locking added. We take our
2439 * bh reference so that we can safely unlock bh.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002440 *
Jan Karade1b7942011-06-13 15:38:22 -04002441 * The jh and bh may be freed by this call.
Dave Kleikamp470decc2006-10-11 01:20:57 -07002442 */
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002443void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh)
Dave Kleikamp470decc2006-10-11 01:20:57 -07002444{
2445 struct buffer_head *bh = jh2bh(jh);
2446
Jan Karade1b7942011-06-13 15:38:22 -04002447 /* Get reference so that buffer cannot be freed before we unlock it */
2448 get_bh(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002449 jbd_lock_bh_state(bh);
2450 spin_lock(&journal->j_list_lock);
Mingming Caof7f4bcc2006-10-11 01:20:59 -07002451 __jbd2_journal_refile_buffer(jh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002452 jbd_unlock_bh_state(bh);
Dave Kleikamp470decc2006-10-11 01:20:57 -07002453 spin_unlock(&journal->j_list_lock);
2454 __brelse(bh);
2455}
Jan Karac851ed52008-07-11 19:27:31 -04002456
2457/*
2458 * File inode in the inode list of the handle's transaction
2459 */
Jan Kara41617e12016-04-24 00:56:07 -04002460static int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode,
2461 unsigned long flags)
Jan Karac851ed52008-07-11 19:27:31 -04002462{
2463 transaction_t *transaction = handle->h_transaction;
Theodore Ts'o41a5b912013-07-01 08:12:41 -04002464 journal_t *journal;
Jan Karac851ed52008-07-11 19:27:31 -04002465
2466 if (is_handle_aborted(handle))
Theodore Ts'o41a5b912013-07-01 08:12:41 -04002467 return -EROFS;
2468 journal = transaction->t_journal;
Jan Karac851ed52008-07-11 19:27:31 -04002469
2470 jbd_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino,
2471 transaction->t_tid);
2472
2473 /*
2474 * First check whether inode isn't already on the transaction's
2475 * lists without taking the lock. Note that this check is safe
2476 * without the lock as we cannot race with somebody removing inode
2477 * from the transaction. The reason is that we remove inode from the
2478 * transaction only in journal_release_jbd_inode() and when we commit
2479 * the transaction. We are guarded from the first case by holding
2480 * a reference to the inode. We are safe against the second case
2481 * because if jinode->i_transaction == transaction, commit code
2482 * cannot touch the transaction because we hold reference to it,
2483 * and if jinode->i_next_transaction == transaction, commit code
2484 * will only file the inode where we want it.
2485 */
Jan Kara41617e12016-04-24 00:56:07 -04002486 if ((jinode->i_transaction == transaction ||
2487 jinode->i_next_transaction == transaction) &&
2488 (jinode->i_flags & flags) == flags)
Jan Karac851ed52008-07-11 19:27:31 -04002489 return 0;
2490
2491 spin_lock(&journal->j_list_lock);
Jan Kara41617e12016-04-24 00:56:07 -04002492 jinode->i_flags |= flags;
2493 /* Is inode already attached where we need it? */
Jan Karac851ed52008-07-11 19:27:31 -04002494 if (jinode->i_transaction == transaction ||
2495 jinode->i_next_transaction == transaction)
2496 goto done;
2497
Jan Kara81be12c2011-05-24 11:52:40 -04002498 /*
2499 * We only ever set this variable to 1 so the test is safe. Since
2500 * t_need_data_flush is likely to be set, we do the test to save some
2501 * cacheline bouncing
2502 */
2503 if (!transaction->t_need_data_flush)
2504 transaction->t_need_data_flush = 1;
Jan Karac851ed52008-07-11 19:27:31 -04002505 /* On some different transaction's list - should be
2506 * the committing one */
2507 if (jinode->i_transaction) {
2508 J_ASSERT(jinode->i_next_transaction == NULL);
2509 J_ASSERT(jinode->i_transaction ==
2510 journal->j_committing_transaction);
2511 jinode->i_next_transaction = transaction;
2512 goto done;
2513 }
2514 /* Not on any transaction list... */
2515 J_ASSERT(!jinode->i_next_transaction);
2516 jinode->i_transaction = transaction;
2517 list_add(&jinode->i_list, &transaction->t_inode_list);
2518done:
2519 spin_unlock(&journal->j_list_lock);
2520
2521 return 0;
2522}
2523
Jan Kara41617e12016-04-24 00:56:07 -04002524int jbd2_journal_inode_add_write(handle_t *handle, struct jbd2_inode *jinode)
2525{
2526 return jbd2_journal_file_inode(handle, jinode,
2527 JI_WRITE_DATA | JI_WAIT_DATA);
2528}
2529
2530int jbd2_journal_inode_add_wait(handle_t *handle, struct jbd2_inode *jinode)
2531{
2532 return jbd2_journal_file_inode(handle, jinode, JI_WAIT_DATA);
2533}
2534
Jan Karac851ed52008-07-11 19:27:31 -04002535/*
Jan Kara7f5aa212009-02-10 11:15:34 -05002536 * File truncate and transaction commit interact with each other in a
2537 * non-trivial way. If a transaction writing data block A is
2538 * committing, we cannot discard the data by truncate until we have
2539 * written them. Otherwise if we crashed after the transaction with
2540 * write has committed but before the transaction with truncate has
2541 * committed, we could see stale data in block A. This function is a
2542 * helper to solve this problem. It starts writeout of the truncated
2543 * part in case it is in the committing transaction.
2544 *
2545 * Filesystem code must call this function when inode is journaled in
2546 * ordered mode before truncation happens and after the inode has been
2547 * placed on orphan list with the new inode size. The second condition
2548 * avoids the race that someone writes new data and we start
2549 * committing the transaction after this function has been called but
2550 * before a transaction for truncate is started (and furthermore it
2551 * allows us to optimize the case where the addition to orphan list
2552 * happens in the same transaction as write --- we don't have to write
2553 * any data in such case).
Jan Karac851ed52008-07-11 19:27:31 -04002554 */
Jan Kara7f5aa212009-02-10 11:15:34 -05002555int jbd2_journal_begin_ordered_truncate(journal_t *journal,
2556 struct jbd2_inode *jinode,
Jan Karac851ed52008-07-11 19:27:31 -04002557 loff_t new_size)
2558{
Jan Kara7f5aa212009-02-10 11:15:34 -05002559 transaction_t *inode_trans, *commit_trans;
Jan Karac851ed52008-07-11 19:27:31 -04002560 int ret = 0;
2561
Jan Kara7f5aa212009-02-10 11:15:34 -05002562 /* This is a quick check to avoid locking if not necessary */
2563 if (!jinode->i_transaction)
Jan Karac851ed52008-07-11 19:27:31 -04002564 goto out;
Jan Kara7f5aa212009-02-10 11:15:34 -05002565 /* Locks are here just to force reading of recent values, it is
2566 * enough that the transaction was not committing before we started
2567 * a transaction adding the inode to orphan list */
Theodore Ts'oa931da62010-08-03 21:35:12 -04002568 read_lock(&journal->j_state_lock);
Jan Karac851ed52008-07-11 19:27:31 -04002569 commit_trans = journal->j_committing_transaction;
Theodore Ts'oa931da62010-08-03 21:35:12 -04002570 read_unlock(&journal->j_state_lock);
Jan Kara7f5aa212009-02-10 11:15:34 -05002571 spin_lock(&journal->j_list_lock);
2572 inode_trans = jinode->i_transaction;
2573 spin_unlock(&journal->j_list_lock);
2574 if (inode_trans == commit_trans) {
2575 ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping,
Jan Karac851ed52008-07-11 19:27:31 -04002576 new_size, LLONG_MAX);
2577 if (ret)
2578 jbd2_journal_abort(journal, ret);
2579 }
2580out:
2581 return ret;
2582}