1 // SPDX-License-Identifier: GPL-2.0+
3 * linux/fs/jbd2/transaction.c
5 * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
7 * Copyright 1998 Red Hat corp --- All Rights Reserved
9 * Generic filesystem transaction handling code; part of the ext2fs
12 * This file manages transactions (compound commits managed by the
13 * journaling code) and handles (individual atomic operations by the
17 #include <linux/time.h>
19 #include <linux/jbd2.h>
20 #include <linux/errno.h>
21 #include <linux/slab.h>
22 #include <linux/timer.h>
24 #include <linux/highmem.h>
25 #include <linux/hrtimer.h>
26 #include <linux/backing-dev.h>
27 #include <linux/bug.h>
28 #include <linux/module.h>
29 #include <linux/sched/mm.h>
31 #include <trace/events/jbd2.h>
33 static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh);
34 static void __jbd2_journal_unfile_buffer(struct journal_head *jh);
36 static struct kmem_cache *transaction_cache;
37 int __init jbd2_journal_init_transaction_cache(void)
39 J_ASSERT(!transaction_cache);
40 transaction_cache = kmem_cache_create("jbd2_transaction_s",
41 sizeof(transaction_t),
43 SLAB_HWCACHE_ALIGN|SLAB_TEMPORARY,
45 if (!transaction_cache) {
46 pr_emerg("JBD2: failed to create transaction cache\n");
52 void jbd2_journal_destroy_transaction_cache(void)
54 kmem_cache_destroy(transaction_cache);
55 transaction_cache = NULL;
58 void jbd2_journal_free_transaction(transaction_t *transaction)
60 if (unlikely(ZERO_OR_NULL_PTR(transaction)))
62 kmem_cache_free(transaction_cache, transaction);
66 * jbd2_get_transaction: obtain a new transaction_t object.
68 * Simply initialise a new transaction. Initialize it in
69 * RUNNING state and add it to the current journal (which should not
70 * have an existing running transaction: we only make a new transaction
71 * once we have started to commit the old one).
74 * The journal MUST be locked. We don't perform atomic mallocs on the
75 * new transaction and we can't block without protecting against other
76 * processes trying to touch the journal while it is in transition.
80 static void jbd2_get_transaction(journal_t *journal,
81 transaction_t *transaction)
83 transaction->t_journal = journal;
84 transaction->t_state = T_RUNNING;
85 transaction->t_start_time = ktime_get();
86 transaction->t_tid = journal->j_transaction_sequence++;
87 transaction->t_expires = jiffies + journal->j_commit_interval;
88 spin_lock_init(&transaction->t_handle_lock);
89 atomic_set(&transaction->t_updates, 0);
90 atomic_set(&transaction->t_outstanding_credits,
91 atomic_read(&journal->j_reserved_credits));
92 atomic_set(&transaction->t_handle_count, 0);
93 INIT_LIST_HEAD(&transaction->t_inode_list);
94 INIT_LIST_HEAD(&transaction->t_private_list);
96 /* Set up the commit timer for the new transaction. */
97 journal->j_commit_timer.expires = round_jiffies_up(transaction->t_expires);
98 add_timer(&journal->j_commit_timer);
100 J_ASSERT(journal->j_running_transaction == NULL);
101 journal->j_running_transaction = transaction;
102 transaction->t_max_wait = 0;
103 transaction->t_start = jiffies;
104 transaction->t_requested = 0;
110 * A handle_t is an object which represents a single atomic update to a
111 * filesystem, and which tracks all of the modifications which form part
112 * of that one update.
116 * Update transaction's maximum wait time, if debugging is enabled.
118 * In order for t_max_wait to be reliable, it must be protected by a
119 * lock. But doing so will mean that start_this_handle() can not be
120 * run in parallel on SMP systems, which limits our scalability. So
121 * unless debugging is enabled, we no longer update t_max_wait, which
122 * means that maximum wait time reported by the jbd2_run_stats
123 * tracepoint will always be zero.
125 static inline void update_t_max_wait(transaction_t *transaction,
128 #ifdef CONFIG_JBD2_DEBUG
129 if (jbd2_journal_enable_debug &&
130 time_after(transaction->t_start, ts)) {
131 ts = jbd2_time_diff(ts, transaction->t_start);
132 spin_lock(&transaction->t_handle_lock);
133 if (ts > transaction->t_max_wait)
134 transaction->t_max_wait = ts;
135 spin_unlock(&transaction->t_handle_lock);
141 * Wait until running transaction passes to T_FLUSH state and new transaction
142 * can thus be started. Also starts the commit if needed. The function expects
143 * running transaction to exist and releases j_state_lock.
145 static void wait_transaction_locked(journal_t *journal)
146 __releases(journal->j_state_lock)
150 tid_t tid = journal->j_running_transaction->t_tid;
152 prepare_to_wait_exclusive(&journal->j_wait_transaction_locked, &wait,
153 TASK_UNINTERRUPTIBLE);
154 need_to_start = !tid_geq(journal->j_commit_request, tid);
155 read_unlock(&journal->j_state_lock);
157 jbd2_log_start_commit(journal, tid);
158 jbd2_might_wait_for_commit(journal);
160 finish_wait(&journal->j_wait_transaction_locked, &wait);
164 * Wait until running transaction transitions from T_SWITCH to T_FLUSH
165 * state and new transaction can thus be started. The function releases
168 static void wait_transaction_switching(journal_t *journal)
169 __releases(journal->j_state_lock)
173 if (WARN_ON(!journal->j_running_transaction ||
174 journal->j_running_transaction->t_state != T_SWITCH)) {
175 read_unlock(&journal->j_state_lock);
178 prepare_to_wait_exclusive(&journal->j_wait_transaction_locked, &wait,
179 TASK_UNINTERRUPTIBLE);
180 read_unlock(&journal->j_state_lock);
182 * We don't call jbd2_might_wait_for_commit() here as there's no
183 * waiting for outstanding handles happening anymore in T_SWITCH state
184 * and handling of reserved handles actually relies on that for
188 finish_wait(&journal->j_wait_transaction_locked, &wait);
191 static void sub_reserved_credits(journal_t *journal, int blocks)
193 atomic_sub(blocks, &journal->j_reserved_credits);
194 wake_up(&journal->j_wait_reserved);
198 * Wait until we can add credits for handle to the running transaction. Called
199 * with j_state_lock held for reading. Returns 0 if handle joined the running
200 * transaction. Returns 1 if we had to wait, j_state_lock is dropped, and
203 static int add_transaction_credits(journal_t *journal, int blocks,
206 transaction_t *t = journal->j_running_transaction;
208 int total = blocks + rsv_blocks;
211 * If the current transaction is locked down for commit, wait
212 * for the lock to be released.
214 if (t->t_state != T_RUNNING) {
215 WARN_ON_ONCE(t->t_state >= T_FLUSH);
216 wait_transaction_locked(journal);
221 * If there is not enough space left in the log to write all
222 * potential buffers requested by this operation, we need to
223 * stall pending a log checkpoint to free some more log space.
225 needed = atomic_add_return(total, &t->t_outstanding_credits);
226 if (needed > journal->j_max_transaction_buffers) {
228 * If the current transaction is already too large,
229 * then start to commit it: we can then go back and
230 * attach this handle to a new transaction.
232 atomic_sub(total, &t->t_outstanding_credits);
235 * Is the number of reserved credits in the current transaction too
236 * big to fit this handle? Wait until reserved credits are freed.
238 if (atomic_read(&journal->j_reserved_credits) + total >
239 journal->j_max_transaction_buffers) {
240 read_unlock(&journal->j_state_lock);
241 jbd2_might_wait_for_commit(journal);
242 wait_event(journal->j_wait_reserved,
243 atomic_read(&journal->j_reserved_credits) + total <=
244 journal->j_max_transaction_buffers);
248 wait_transaction_locked(journal);
253 * The commit code assumes that it can get enough log space
254 * without forcing a checkpoint. This is *critical* for
255 * correctness: a checkpoint of a buffer which is also
256 * associated with a committing transaction creates a deadlock,
257 * so commit simply cannot force through checkpoints.
259 * We must therefore ensure the necessary space in the journal
260 * *before* starting to dirty potentially checkpointed buffers
261 * in the new transaction.
263 if (jbd2_log_space_left(journal) < jbd2_space_needed(journal)) {
264 atomic_sub(total, &t->t_outstanding_credits);
265 read_unlock(&journal->j_state_lock);
266 jbd2_might_wait_for_commit(journal);
267 write_lock(&journal->j_state_lock);
268 if (jbd2_log_space_left(journal) < jbd2_space_needed(journal))
269 __jbd2_log_wait_for_space(journal);
270 write_unlock(&journal->j_state_lock);
274 /* No reservation? We are done... */
278 needed = atomic_add_return(rsv_blocks, &journal->j_reserved_credits);
279 /* We allow at most half of a transaction to be reserved */
280 if (needed > journal->j_max_transaction_buffers / 2) {
281 sub_reserved_credits(journal, rsv_blocks);
282 atomic_sub(total, &t->t_outstanding_credits);
283 read_unlock(&journal->j_state_lock);
284 jbd2_might_wait_for_commit(journal);
285 wait_event(journal->j_wait_reserved,
286 atomic_read(&journal->j_reserved_credits) + rsv_blocks
287 <= journal->j_max_transaction_buffers / 2);
294 * start_this_handle: Given a handle, deal with any locking or stalling
295 * needed to make sure that there is enough journal space for the handle
296 * to begin. Attach the handle to a transaction and set up the
297 * transaction's buffer credits.
300 static int start_this_handle(journal_t *journal, handle_t *handle,
303 transaction_t *transaction, *new_transaction = NULL;
304 int blocks = handle->h_buffer_credits;
306 unsigned long ts = jiffies;
308 if (handle->h_rsv_handle)
309 rsv_blocks = handle->h_rsv_handle->h_buffer_credits;
312 * Limit the number of reserved credits to 1/2 of maximum transaction
313 * size and limit the number of total credits to not exceed maximum
314 * transaction size per operation.
316 if ((rsv_blocks > journal->j_max_transaction_buffers / 2) ||
317 (rsv_blocks + blocks > journal->j_max_transaction_buffers)) {
318 printk(KERN_ERR "JBD2: %s wants too many credits "
319 "credits:%d rsv_credits:%d max:%d\n",
320 current->comm, blocks, rsv_blocks,
321 journal->j_max_transaction_buffers);
327 if (!journal->j_running_transaction) {
329 * If __GFP_FS is not present, then we may be being called from
330 * inside the fs writeback layer, so we MUST NOT fail.
332 if ((gfp_mask & __GFP_FS) == 0)
333 gfp_mask |= __GFP_NOFAIL;
334 new_transaction = kmem_cache_zalloc(transaction_cache,
336 if (!new_transaction)
340 jbd_debug(3, "New handle %p going live.\n", handle);
343 * We need to hold j_state_lock until t_updates has been incremented,
344 * for proper journal barrier handling
347 read_lock(&journal->j_state_lock);
348 BUG_ON(journal->j_flags & JBD2_UNMOUNT);
349 if (is_journal_aborted(journal) ||
350 (journal->j_errno != 0 && !(journal->j_flags & JBD2_ACK_ERR))) {
351 read_unlock(&journal->j_state_lock);
352 jbd2_journal_free_transaction(new_transaction);
357 * Wait on the journal's transaction barrier if necessary. Specifically
358 * we allow reserved handles to proceed because otherwise commit could
359 * deadlock on page writeback not being able to complete.
361 if (!handle->h_reserved && journal->j_barrier_count) {
362 read_unlock(&journal->j_state_lock);
363 wait_event(journal->j_wait_transaction_locked,
364 journal->j_barrier_count == 0);
368 if (!journal->j_running_transaction) {
369 read_unlock(&journal->j_state_lock);
370 if (!new_transaction)
371 goto alloc_transaction;
372 write_lock(&journal->j_state_lock);
373 if (!journal->j_running_transaction &&
374 (handle->h_reserved || !journal->j_barrier_count)) {
375 jbd2_get_transaction(journal, new_transaction);
376 new_transaction = NULL;
378 write_unlock(&journal->j_state_lock);
382 transaction = journal->j_running_transaction;
384 if (!handle->h_reserved) {
385 /* We may have dropped j_state_lock - restart in that case */
386 if (add_transaction_credits(journal, blocks, rsv_blocks))
390 * We have handle reserved so we are allowed to join T_LOCKED
391 * transaction and we don't have to check for transaction size
392 * and journal space. But we still have to wait while running
393 * transaction is being switched to a committing one as it
394 * won't wait for any handles anymore.
396 if (transaction->t_state == T_SWITCH) {
397 wait_transaction_switching(journal);
400 sub_reserved_credits(journal, blocks);
401 handle->h_reserved = 0;
404 /* OK, account for the buffers that this operation expects to
405 * use and add the handle to the running transaction.
407 update_t_max_wait(transaction, ts);
408 handle->h_transaction = transaction;
409 handle->h_requested_credits = blocks;
410 handle->h_start_jiffies = jiffies;
411 atomic_inc(&transaction->t_updates);
412 atomic_inc(&transaction->t_handle_count);
413 jbd_debug(4, "Handle %p given %d credits (total %d, free %lu)\n",
415 atomic_read(&transaction->t_outstanding_credits),
416 jbd2_log_space_left(journal));
417 read_unlock(&journal->j_state_lock);
418 current->journal_info = handle;
420 rwsem_acquire_read(&journal->j_trans_commit_map, 0, 0, _THIS_IP_);
421 jbd2_journal_free_transaction(new_transaction);
423 * Ensure that no allocations done while the transaction is open are
424 * going to recurse back to the fs layer.
426 handle->saved_alloc_context = memalloc_nofs_save();
430 /* Allocate a new handle. This should probably be in a slab... */
431 static handle_t *new_handle(int nblocks)
433 handle_t *handle = jbd2_alloc_handle(GFP_NOFS);
436 handle->h_buffer_credits = nblocks;
442 handle_t *jbd2__journal_start(journal_t *journal, int nblocks, int rsv_blocks,
443 gfp_t gfp_mask, unsigned int type,
444 unsigned int line_no)
446 handle_t *handle = journal_current_handle();
450 return ERR_PTR(-EROFS);
453 J_ASSERT(handle->h_transaction->t_journal == journal);
458 handle = new_handle(nblocks);
460 return ERR_PTR(-ENOMEM);
462 handle_t *rsv_handle;
464 rsv_handle = new_handle(rsv_blocks);
466 jbd2_free_handle(handle);
467 return ERR_PTR(-ENOMEM);
469 rsv_handle->h_reserved = 1;
470 rsv_handle->h_journal = journal;
471 handle->h_rsv_handle = rsv_handle;
474 err = start_this_handle(journal, handle, gfp_mask);
476 if (handle->h_rsv_handle)
477 jbd2_free_handle(handle->h_rsv_handle);
478 jbd2_free_handle(handle);
481 handle->h_type = type;
482 handle->h_line_no = line_no;
483 trace_jbd2_handle_start(journal->j_fs_dev->bd_dev,
484 handle->h_transaction->t_tid, type,
489 EXPORT_SYMBOL(jbd2__journal_start);
493 * jbd2_journal_start() - Obtain a new handle.
494 * @journal: Journal to start transaction on.
495 * @nblocks: number of block buffer we might modify
497 * We make sure that the transaction can guarantee at least nblocks of
498 * modified buffers in the log. We block until the log can guarantee
499 * that much space. Additionally, if rsv_blocks > 0, we also create another
500 * handle with rsv_blocks reserved blocks in the journal. This handle is
501 * is stored in h_rsv_handle. It is not attached to any particular transaction
502 * and thus doesn't block transaction commit. If the caller uses this reserved
503 * handle, it has to set h_rsv_handle to NULL as otherwise jbd2_journal_stop()
504 * on the parent handle will dispose the reserved one. Reserved handle has to
505 * be converted to a normal handle using jbd2_journal_start_reserved() before
508 * Return a pointer to a newly allocated handle, or an ERR_PTR() value
511 handle_t *jbd2_journal_start(journal_t *journal, int nblocks)
513 return jbd2__journal_start(journal, nblocks, 0, GFP_NOFS, 0, 0);
515 EXPORT_SYMBOL(jbd2_journal_start);
517 void jbd2_journal_free_reserved(handle_t *handle)
519 journal_t *journal = handle->h_journal;
521 WARN_ON(!handle->h_reserved);
522 sub_reserved_credits(journal, handle->h_buffer_credits);
523 jbd2_free_handle(handle);
525 EXPORT_SYMBOL(jbd2_journal_free_reserved);
528 * jbd2_journal_start_reserved() - start reserved handle
529 * @handle: handle to start
530 * @type: for handle statistics
531 * @line_no: for handle statistics
533 * Start handle that has been previously reserved with jbd2_journal_reserve().
534 * This attaches @handle to the running transaction (or creates one if there's
535 * not transaction running). Unlike jbd2_journal_start() this function cannot
536 * block on journal commit, checkpointing, or similar stuff. It can block on
537 * memory allocation or frozen journal though.
539 * Return 0 on success, non-zero on error - handle is freed in that case.
541 int jbd2_journal_start_reserved(handle_t *handle, unsigned int type,
542 unsigned int line_no)
544 journal_t *journal = handle->h_journal;
547 if (WARN_ON(!handle->h_reserved)) {
548 /* Someone passed in normal handle? Just stop it. */
549 jbd2_journal_stop(handle);
553 * Usefulness of mixing of reserved and unreserved handles is
554 * questionable. So far nobody seems to need it so just error out.
556 if (WARN_ON(current->journal_info)) {
557 jbd2_journal_free_reserved(handle);
561 handle->h_journal = NULL;
563 * GFP_NOFS is here because callers are likely from writeback or
564 * similarly constrained call sites
566 ret = start_this_handle(journal, handle, GFP_NOFS);
568 handle->h_journal = journal;
569 jbd2_journal_free_reserved(handle);
572 handle->h_type = type;
573 handle->h_line_no = line_no;
574 trace_jbd2_handle_start(journal->j_fs_dev->bd_dev,
575 handle->h_transaction->t_tid, type,
576 line_no, handle->h_buffer_credits);
579 EXPORT_SYMBOL(jbd2_journal_start_reserved);
582 * jbd2_journal_extend() - extend buffer credits.
583 * @handle: handle to 'extend'
584 * @nblocks: nr blocks to try to extend by.
586 * Some transactions, such as large extends and truncates, can be done
587 * atomically all at once or in several stages. The operation requests
588 * a credit for a number of buffer modifications in advance, but can
589 * extend its credit if it needs more.
591 * jbd2_journal_extend tries to give the running handle more buffer credits.
592 * It does not guarantee that allocation - this is a best-effort only.
593 * The calling process MUST be able to deal cleanly with a failure to
596 * Return 0 on success, non-zero on failure.
598 * return code < 0 implies an error
599 * return code > 0 implies normal transaction-full status.
601 int jbd2_journal_extend(handle_t *handle, int nblocks)
603 transaction_t *transaction = handle->h_transaction;
608 if (is_handle_aborted(handle))
610 journal = transaction->t_journal;
614 read_lock(&journal->j_state_lock);
616 /* Don't extend a locked-down transaction! */
617 if (transaction->t_state != T_RUNNING) {
618 jbd_debug(3, "denied handle %p %d blocks: "
619 "transaction not running\n", handle, nblocks);
623 spin_lock(&transaction->t_handle_lock);
624 wanted = atomic_add_return(nblocks,
625 &transaction->t_outstanding_credits);
627 if (wanted > journal->j_max_transaction_buffers) {
628 jbd_debug(3, "denied handle %p %d blocks: "
629 "transaction too large\n", handle, nblocks);
630 atomic_sub(nblocks, &transaction->t_outstanding_credits);
634 if (wanted + (wanted >> JBD2_CONTROL_BLOCKS_SHIFT) >
635 jbd2_log_space_left(journal)) {
636 jbd_debug(3, "denied handle %p %d blocks: "
637 "insufficient log space\n", handle, nblocks);
638 atomic_sub(nblocks, &transaction->t_outstanding_credits);
642 trace_jbd2_handle_extend(journal->j_fs_dev->bd_dev,
644 handle->h_type, handle->h_line_no,
645 handle->h_buffer_credits,
648 handle->h_buffer_credits += nblocks;
649 handle->h_requested_credits += nblocks;
652 jbd_debug(3, "extended handle %p by %d\n", handle, nblocks);
654 spin_unlock(&transaction->t_handle_lock);
656 read_unlock(&journal->j_state_lock);
662 * jbd2__journal_restart() - restart a handle .
663 * @handle: handle to restart
664 * @nblocks: nr credits requested
665 * @gfp_mask: memory allocation flags (for start_this_handle)
667 * Restart a handle for a multi-transaction filesystem
670 * If the jbd2_journal_extend() call above fails to grant new buffer credits
671 * to a running handle, a call to jbd2_journal_restart will commit the
672 * handle's transaction so far and reattach the handle to a new
673 * transaction capable of guaranteeing the requested number of
674 * credits. We preserve reserved handle if there's any attached to the
677 int jbd2__journal_restart(handle_t *handle, int nblocks, gfp_t gfp_mask)
679 transaction_t *transaction = handle->h_transaction;
682 int need_to_start, ret;
684 /* If we've had an abort of any type, don't even think about
685 * actually doing the restart! */
686 if (is_handle_aborted(handle))
688 journal = transaction->t_journal;
691 * First unlink the handle from its current transaction, and start the
694 J_ASSERT(atomic_read(&transaction->t_updates) > 0);
695 J_ASSERT(journal_current_handle() == handle);
697 read_lock(&journal->j_state_lock);
698 spin_lock(&transaction->t_handle_lock);
699 atomic_sub(handle->h_buffer_credits,
700 &transaction->t_outstanding_credits);
701 if (handle->h_rsv_handle) {
702 sub_reserved_credits(journal,
703 handle->h_rsv_handle->h_buffer_credits);
705 if (atomic_dec_and_test(&transaction->t_updates))
706 wake_up(&journal->j_wait_updates);
707 tid = transaction->t_tid;
708 spin_unlock(&transaction->t_handle_lock);
709 handle->h_transaction = NULL;
710 current->journal_info = NULL;
712 jbd_debug(2, "restarting handle %p\n", handle);
713 need_to_start = !tid_geq(journal->j_commit_request, tid);
714 read_unlock(&journal->j_state_lock);
716 jbd2_log_start_commit(journal, tid);
718 rwsem_release(&journal->j_trans_commit_map, 1, _THIS_IP_);
719 handle->h_buffer_credits = nblocks;
721 * Restore the original nofs context because the journal restart
722 * is basically the same thing as journal stop and start.
723 * start_this_handle will start a new nofs context.
725 memalloc_nofs_restore(handle->saved_alloc_context);
726 ret = start_this_handle(journal, handle, gfp_mask);
729 EXPORT_SYMBOL(jbd2__journal_restart);
732 int jbd2_journal_restart(handle_t *handle, int nblocks)
734 return jbd2__journal_restart(handle, nblocks, GFP_NOFS);
736 EXPORT_SYMBOL(jbd2_journal_restart);
739 * jbd2_journal_lock_updates () - establish a transaction barrier.
740 * @journal: Journal to establish a barrier on.
742 * This locks out any further updates from being started, and blocks
743 * until all existing updates have completed, returning only once the
744 * journal is in a quiescent state with no updates running.
746 * The journal lock should not be held on entry.
748 void jbd2_journal_lock_updates(journal_t *journal)
752 jbd2_might_wait_for_commit(journal);
754 write_lock(&journal->j_state_lock);
755 ++journal->j_barrier_count;
757 /* Wait until there are no reserved handles */
758 if (atomic_read(&journal->j_reserved_credits)) {
759 write_unlock(&journal->j_state_lock);
760 wait_event(journal->j_wait_reserved,
761 atomic_read(&journal->j_reserved_credits) == 0);
762 write_lock(&journal->j_state_lock);
765 /* Wait until there are no running updates */
767 transaction_t *transaction = journal->j_running_transaction;
772 spin_lock(&transaction->t_handle_lock);
773 prepare_to_wait(&journal->j_wait_updates, &wait,
774 TASK_UNINTERRUPTIBLE);
775 if (!atomic_read(&transaction->t_updates)) {
776 spin_unlock(&transaction->t_handle_lock);
777 finish_wait(&journal->j_wait_updates, &wait);
780 spin_unlock(&transaction->t_handle_lock);
781 write_unlock(&journal->j_state_lock);
783 finish_wait(&journal->j_wait_updates, &wait);
784 write_lock(&journal->j_state_lock);
786 write_unlock(&journal->j_state_lock);
789 * We have now established a barrier against other normal updates, but
790 * we also need to barrier against other jbd2_journal_lock_updates() calls
791 * to make sure that we serialise special journal-locked operations
794 mutex_lock(&journal->j_barrier);
798 * jbd2_journal_unlock_updates () - release barrier
799 * @journal: Journal to release the barrier on.
801 * Release a transaction barrier obtained with jbd2_journal_lock_updates().
803 * Should be called without the journal lock held.
805 void jbd2_journal_unlock_updates (journal_t *journal)
807 J_ASSERT(journal->j_barrier_count != 0);
809 mutex_unlock(&journal->j_barrier);
810 write_lock(&journal->j_state_lock);
811 --journal->j_barrier_count;
812 write_unlock(&journal->j_state_lock);
813 wake_up_all(&journal->j_wait_transaction_locked);
816 static void warn_dirty_buffer(struct buffer_head *bh)
819 "JBD2: Spotted dirty metadata buffer (dev = %pg, blocknr = %llu). "
820 "There's a risk of filesystem corruption in case of system "
822 bh->b_bdev, (unsigned long long)bh->b_blocknr);
825 /* Call t_frozen trigger and copy buffer data into jh->b_frozen_data. */
826 static void jbd2_freeze_jh_data(struct journal_head *jh)
831 struct buffer_head *bh = jh2bh(jh);
833 J_EXPECT_JH(jh, buffer_uptodate(bh), "Possible IO failure.\n");
835 offset = offset_in_page(bh->b_data);
836 source = kmap_atomic(page);
837 /* Fire data frozen trigger just before we copy the data */
838 jbd2_buffer_frozen_trigger(jh, source + offset, jh->b_triggers);
839 memcpy(jh->b_frozen_data, source + offset, bh->b_size);
840 kunmap_atomic(source);
843 * Now that the frozen data is saved off, we need to store any matching
846 jh->b_frozen_triggers = jh->b_triggers;
850 * If the buffer is already part of the current transaction, then there
851 * is nothing we need to do. If it is already part of a prior
852 * transaction which we are still committing to disk, then we need to
853 * make sure that we do not overwrite the old copy: we do copy-out to
854 * preserve the copy going to disk. We also account the buffer against
855 * the handle's metadata buffer credits (unless the buffer is already
856 * part of the transaction, that is).
860 do_get_write_access(handle_t *handle, struct journal_head *jh,
863 struct buffer_head *bh;
864 transaction_t *transaction = handle->h_transaction;
867 char *frozen_buffer = NULL;
868 unsigned long start_lock, time_lock;
870 journal = transaction->t_journal;
872 jbd_debug(5, "journal_head %p, force_copy %d\n", jh, force_copy);
874 JBUFFER_TRACE(jh, "entry");
878 /* @@@ Need to check for errors here at some point. */
880 start_lock = jiffies;
882 jbd_lock_bh_state(bh);
884 /* If it takes too long to lock the buffer, trace it */
885 time_lock = jbd2_time_diff(start_lock, jiffies);
886 if (time_lock > HZ/10)
887 trace_jbd2_lock_buffer_stall(bh->b_bdev->bd_dev,
888 jiffies_to_msecs(time_lock));
890 /* We now hold the buffer lock so it is safe to query the buffer
891 * state. Is the buffer dirty?
893 * If so, there are two possibilities. The buffer may be
894 * non-journaled, and undergoing a quite legitimate writeback.
895 * Otherwise, it is journaled, and we don't expect dirty buffers
896 * in that state (the buffers should be marked JBD_Dirty
897 * instead.) So either the IO is being done under our own
898 * control and this is a bug, or it's a third party IO such as
899 * dump(8) (which may leave the buffer scheduled for read ---
900 * ie. locked but not dirty) or tune2fs (which may actually have
901 * the buffer dirtied, ugh.) */
903 if (buffer_dirty(bh)) {
905 * First question: is this buffer already part of the current
906 * transaction or the existing committing transaction?
908 if (jh->b_transaction) {
910 jh->b_transaction == transaction ||
912 journal->j_committing_transaction);
913 if (jh->b_next_transaction)
914 J_ASSERT_JH(jh, jh->b_next_transaction ==
916 warn_dirty_buffer(bh);
919 * In any case we need to clean the dirty flag and we must
920 * do it under the buffer lock to be sure we don't race
921 * with running write-out.
923 JBUFFER_TRACE(jh, "Journalling dirty buffer");
924 clear_buffer_dirty(bh);
925 set_buffer_jbddirty(bh);
931 if (is_handle_aborted(handle)) {
932 jbd_unlock_bh_state(bh);
938 * The buffer is already part of this transaction if b_transaction or
939 * b_next_transaction points to it
941 if (jh->b_transaction == transaction ||
942 jh->b_next_transaction == transaction)
946 * this is the first time this transaction is touching this buffer,
947 * reset the modified flag
952 * If the buffer is not journaled right now, we need to make sure it
953 * doesn't get written to disk before the caller actually commits the
956 if (!jh->b_transaction) {
957 JBUFFER_TRACE(jh, "no transaction");
958 J_ASSERT_JH(jh, !jh->b_next_transaction);
959 JBUFFER_TRACE(jh, "file as BJ_Reserved");
961 * Make sure all stores to jh (b_modified, b_frozen_data) are
962 * visible before attaching it to the running transaction.
963 * Paired with barrier in jbd2_write_access_granted()
966 spin_lock(&journal->j_list_lock);
967 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
968 spin_unlock(&journal->j_list_lock);
972 * If there is already a copy-out version of this buffer, then we don't
973 * need to make another one
975 if (jh->b_frozen_data) {
976 JBUFFER_TRACE(jh, "has frozen data");
977 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
981 JBUFFER_TRACE(jh, "owned by older transaction");
982 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
983 J_ASSERT_JH(jh, jh->b_transaction == journal->j_committing_transaction);
986 * There is one case we have to be very careful about. If the
987 * committing transaction is currently writing this buffer out to disk
988 * and has NOT made a copy-out, then we cannot modify the buffer
989 * contents at all right now. The essence of copy-out is that it is
990 * the extra copy, not the primary copy, which gets journaled. If the
991 * primary copy is already going to disk then we cannot do copy-out
994 if (buffer_shadow(bh)) {
995 JBUFFER_TRACE(jh, "on shadow: sleep");
996 jbd_unlock_bh_state(bh);
997 wait_on_bit_io(&bh->b_state, BH_Shadow, TASK_UNINTERRUPTIBLE);
1002 * Only do the copy if the currently-owning transaction still needs it.
1003 * If buffer isn't on BJ_Metadata list, the committing transaction is
1004 * past that stage (here we use the fact that BH_Shadow is set under
1005 * bh_state lock together with refiling to BJ_Shadow list and at this
1006 * point we know the buffer doesn't have BH_Shadow set).
1008 * Subtle point, though: if this is a get_undo_access, then we will be
1009 * relying on the frozen_data to contain the new value of the
1010 * committed_data record after the transaction, so we HAVE to force the
1011 * frozen_data copy in that case.
1013 if (jh->b_jlist == BJ_Metadata || force_copy) {
1014 JBUFFER_TRACE(jh, "generate frozen data");
1015 if (!frozen_buffer) {
1016 JBUFFER_TRACE(jh, "allocate memory for buffer");
1017 jbd_unlock_bh_state(bh);
1018 frozen_buffer = jbd2_alloc(jh2bh(jh)->b_size,
1019 GFP_NOFS | __GFP_NOFAIL);
1022 jh->b_frozen_data = frozen_buffer;
1023 frozen_buffer = NULL;
1024 jbd2_freeze_jh_data(jh);
1028 * Make sure all stores to jh (b_modified, b_frozen_data) are visible
1029 * before attaching it to the running transaction. Paired with barrier
1030 * in jbd2_write_access_granted()
1033 jh->b_next_transaction = transaction;
1036 jbd_unlock_bh_state(bh);
1039 * If we are about to journal a buffer, then any revoke pending on it is
1042 jbd2_journal_cancel_revoke(handle, jh);
1045 if (unlikely(frozen_buffer)) /* It's usually NULL */
1046 jbd2_free(frozen_buffer, bh->b_size);
1048 JBUFFER_TRACE(jh, "exit");
1052 /* Fast check whether buffer is already attached to the required transaction */
1053 static bool jbd2_write_access_granted(handle_t *handle, struct buffer_head *bh,
1056 struct journal_head *jh;
1059 /* Dirty buffers require special handling... */
1060 if (buffer_dirty(bh))
1064 * RCU protects us from dereferencing freed pages. So the checks we do
1065 * are guaranteed not to oops. However the jh slab object can get freed
1066 * & reallocated while we work with it. So we have to be careful. When
1067 * we see jh attached to the running transaction, we know it must stay
1068 * so until the transaction is committed. Thus jh won't be freed and
1069 * will be attached to the same bh while we run. However it can
1070 * happen jh gets freed, reallocated, and attached to the transaction
1071 * just after we get pointer to it from bh. So we have to be careful
1072 * and recheck jh still belongs to our bh before we return success.
1075 if (!buffer_jbd(bh))
1077 /* This should be bh2jh() but that doesn't work with inline functions */
1078 jh = READ_ONCE(bh->b_private);
1081 /* For undo access buffer must have data copied */
1082 if (undo && !jh->b_committed_data)
1084 if (READ_ONCE(jh->b_transaction) != handle->h_transaction &&
1085 READ_ONCE(jh->b_next_transaction) != handle->h_transaction)
1088 * There are two reasons for the barrier here:
1089 * 1) Make sure to fetch b_bh after we did previous checks so that we
1090 * detect when jh went through free, realloc, attach to transaction
1091 * while we were checking. Paired with implicit barrier in that path.
1092 * 2) So that access to bh done after jbd2_write_access_granted()
1093 * doesn't get reordered and see inconsistent state of concurrent
1094 * do_get_write_access().
1097 if (unlikely(jh->b_bh != bh))
1106 * jbd2_journal_get_write_access() - notify intent to modify a buffer
1107 * for metadata (not data) update.
1108 * @handle: transaction to add buffer modifications to
1109 * @bh: bh to be used for metadata writes
1111 * Returns: error code or 0 on success.
1113 * In full data journalling mode the buffer may be of type BJ_AsyncData,
1114 * because we're ``write()ing`` a buffer which is also part of a shared mapping.
1117 int jbd2_journal_get_write_access(handle_t *handle, struct buffer_head *bh)
1119 struct journal_head *jh;
1122 if (is_handle_aborted(handle))
1125 if (jbd2_write_access_granted(handle, bh, false))
1128 jh = jbd2_journal_add_journal_head(bh);
1129 /* We do not want to get caught playing with fields which the
1130 * log thread also manipulates. Make sure that the buffer
1131 * completes any outstanding IO before proceeding. */
1132 rc = do_get_write_access(handle, jh, 0);
1133 jbd2_journal_put_journal_head(jh);
1139 * When the user wants to journal a newly created buffer_head
1140 * (ie. getblk() returned a new buffer and we are going to populate it
1141 * manually rather than reading off disk), then we need to keep the
1142 * buffer_head locked until it has been completely filled with new
1143 * data. In this case, we should be able to make the assertion that
1144 * the bh is not already part of an existing transaction.
1146 * The buffer should already be locked by the caller by this point.
1147 * There is no lock ranking violation: it was a newly created,
1148 * unlocked buffer beforehand. */
1151 * jbd2_journal_get_create_access () - notify intent to use newly created bh
1152 * @handle: transaction to new buffer to
1155 * Call this if you create a new bh.
1157 int jbd2_journal_get_create_access(handle_t *handle, struct buffer_head *bh)
1159 transaction_t *transaction = handle->h_transaction;
1161 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
1164 jbd_debug(5, "journal_head %p\n", jh);
1166 if (is_handle_aborted(handle))
1168 journal = transaction->t_journal;
1171 JBUFFER_TRACE(jh, "entry");
1173 * The buffer may already belong to this transaction due to pre-zeroing
1174 * in the filesystem's new_block code. It may also be on the previous,
1175 * committing transaction's lists, but it HAS to be in Forget state in
1176 * that case: the transaction must have deleted the buffer for it to be
1179 jbd_lock_bh_state(bh);
1180 J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
1181 jh->b_transaction == NULL ||
1182 (jh->b_transaction == journal->j_committing_transaction &&
1183 jh->b_jlist == BJ_Forget)));
1185 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
1186 J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
1188 if (jh->b_transaction == NULL) {
1190 * Previous jbd2_journal_forget() could have left the buffer
1191 * with jbddirty bit set because it was being committed. When
1192 * the commit finished, we've filed the buffer for
1193 * checkpointing and marked it dirty. Now we are reallocating
1194 * the buffer so the transaction freeing it must have
1195 * committed and so it's safe to clear the dirty bit.
1197 clear_buffer_dirty(jh2bh(jh));
1198 /* first access by this transaction */
1201 JBUFFER_TRACE(jh, "file as BJ_Reserved");
1202 spin_lock(&journal->j_list_lock);
1203 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
1204 spin_unlock(&journal->j_list_lock);
1205 } else if (jh->b_transaction == journal->j_committing_transaction) {
1206 /* first access by this transaction */
1209 JBUFFER_TRACE(jh, "set next transaction");
1210 spin_lock(&journal->j_list_lock);
1211 jh->b_next_transaction = transaction;
1212 spin_unlock(&journal->j_list_lock);
1214 jbd_unlock_bh_state(bh);
1217 * akpm: I added this. ext3_alloc_branch can pick up new indirect
1218 * blocks which contain freed but then revoked metadata. We need
1219 * to cancel the revoke in case we end up freeing it yet again
1220 * and the reallocating as data - this would cause a second revoke,
1221 * which hits an assertion error.
1223 JBUFFER_TRACE(jh, "cancelling revoke");
1224 jbd2_journal_cancel_revoke(handle, jh);
1226 jbd2_journal_put_journal_head(jh);
1231 * jbd2_journal_get_undo_access() - Notify intent to modify metadata with
1232 * non-rewindable consequences
1233 * @handle: transaction
1234 * @bh: buffer to undo
1236 * Sometimes there is a need to distinguish between metadata which has
1237 * been committed to disk and that which has not. The ext3fs code uses
1238 * this for freeing and allocating space, we have to make sure that we
1239 * do not reuse freed space until the deallocation has been committed,
1240 * since if we overwrote that space we would make the delete
1241 * un-rewindable in case of a crash.
1243 * To deal with that, jbd2_journal_get_undo_access requests write access to a
1244 * buffer for parts of non-rewindable operations such as delete
1245 * operations on the bitmaps. The journaling code must keep a copy of
1246 * the buffer's contents prior to the undo_access call until such time
1247 * as we know that the buffer has definitely been committed to disk.
1249 * We never need to know which transaction the committed data is part
1250 * of, buffers touched here are guaranteed to be dirtied later and so
1251 * will be committed to a new transaction in due course, at which point
1252 * we can discard the old committed data pointer.
1254 * Returns error number or 0 on success.
1256 int jbd2_journal_get_undo_access(handle_t *handle, struct buffer_head *bh)
1259 struct journal_head *jh;
1260 char *committed_data = NULL;
1262 if (is_handle_aborted(handle))
1265 if (jbd2_write_access_granted(handle, bh, true))
1268 jh = jbd2_journal_add_journal_head(bh);
1269 JBUFFER_TRACE(jh, "entry");
1272 * Do this first --- it can drop the journal lock, so we want to
1273 * make sure that obtaining the committed_data is done
1274 * atomically wrt. completion of any outstanding commits.
1276 err = do_get_write_access(handle, jh, 1);
1281 if (!jh->b_committed_data)
1282 committed_data = jbd2_alloc(jh2bh(jh)->b_size,
1283 GFP_NOFS|__GFP_NOFAIL);
1285 jbd_lock_bh_state(bh);
1286 if (!jh->b_committed_data) {
1287 /* Copy out the current buffer contents into the
1288 * preserved, committed copy. */
1289 JBUFFER_TRACE(jh, "generate b_committed data");
1290 if (!committed_data) {
1291 jbd_unlock_bh_state(bh);
1295 jh->b_committed_data = committed_data;
1296 committed_data = NULL;
1297 memcpy(jh->b_committed_data, bh->b_data, bh->b_size);
1299 jbd_unlock_bh_state(bh);
1301 jbd2_journal_put_journal_head(jh);
1302 if (unlikely(committed_data))
1303 jbd2_free(committed_data, bh->b_size);
1308 * jbd2_journal_set_triggers() - Add triggers for commit writeout
1309 * @bh: buffer to trigger on
1310 * @type: struct jbd2_buffer_trigger_type containing the trigger(s).
1312 * Set any triggers on this journal_head. This is always safe, because
1313 * triggers for a committing buffer will be saved off, and triggers for
1314 * a running transaction will match the buffer in that transaction.
1316 * Call with NULL to clear the triggers.
1318 void jbd2_journal_set_triggers(struct buffer_head *bh,
1319 struct jbd2_buffer_trigger_type *type)
1321 struct journal_head *jh = jbd2_journal_grab_journal_head(bh);
1325 jh->b_triggers = type;
1326 jbd2_journal_put_journal_head(jh);
1329 void jbd2_buffer_frozen_trigger(struct journal_head *jh, void *mapped_data,
1330 struct jbd2_buffer_trigger_type *triggers)
1332 struct buffer_head *bh = jh2bh(jh);
1334 if (!triggers || !triggers->t_frozen)
1337 triggers->t_frozen(triggers, bh, mapped_data, bh->b_size);
1340 void jbd2_buffer_abort_trigger(struct journal_head *jh,
1341 struct jbd2_buffer_trigger_type *triggers)
1343 if (!triggers || !triggers->t_abort)
1346 triggers->t_abort(triggers, jh2bh(jh));
1350 * jbd2_journal_dirty_metadata() - mark a buffer as containing dirty metadata
1351 * @handle: transaction to add buffer to.
1352 * @bh: buffer to mark
1354 * mark dirty metadata which needs to be journaled as part of the current
1357 * The buffer must have previously had jbd2_journal_get_write_access()
1358 * called so that it has a valid journal_head attached to the buffer
1361 * The buffer is placed on the transaction's metadata list and is marked
1362 * as belonging to the transaction.
1364 * Returns error number or 0 on success.
1366 * Special care needs to be taken if the buffer already belongs to the
1367 * current committing transaction (in which case we should have frozen
1368 * data present for that commit). In that case, we don't relink the
1369 * buffer: that only gets done when the old transaction finally
1370 * completes its commit.
1372 int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
1374 transaction_t *transaction = handle->h_transaction;
1376 struct journal_head *jh;
1379 if (!buffer_jbd(bh))
1383 * We don't grab jh reference here since the buffer must be part
1384 * of the running transaction.
1387 jbd_debug(5, "journal_head %p\n", jh);
1388 JBUFFER_TRACE(jh, "entry");
1391 * This and the following assertions are unreliable since we may see jh
1392 * in inconsistent state unless we grab bh_state lock. But this is
1393 * crucial to catch bugs so let's do a reliable check until the
1394 * lockless handling is fully proven.
1396 if (jh->b_transaction != transaction &&
1397 jh->b_next_transaction != transaction) {
1398 jbd_lock_bh_state(bh);
1399 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
1400 jh->b_next_transaction == transaction);
1401 jbd_unlock_bh_state(bh);
1403 if (jh->b_modified == 1) {
1404 /* If it's in our transaction it must be in BJ_Metadata list. */
1405 if (jh->b_transaction == transaction &&
1406 jh->b_jlist != BJ_Metadata) {
1407 jbd_lock_bh_state(bh);
1408 if (jh->b_transaction == transaction &&
1409 jh->b_jlist != BJ_Metadata)
1410 pr_err("JBD2: assertion failure: h_type=%u "
1411 "h_line_no=%u block_no=%llu jlist=%u\n",
1412 handle->h_type, handle->h_line_no,
1413 (unsigned long long) bh->b_blocknr,
1415 J_ASSERT_JH(jh, jh->b_transaction != transaction ||
1416 jh->b_jlist == BJ_Metadata);
1417 jbd_unlock_bh_state(bh);
1422 journal = transaction->t_journal;
1423 jbd_lock_bh_state(bh);
1425 if (is_handle_aborted(handle)) {
1427 * Check journal aborting with @jh->b_state_lock locked,
1428 * since 'jh->b_transaction' could be replaced with
1429 * 'jh->b_next_transaction' during old transaction
1430 * committing if journal aborted, which may fail
1431 * assertion on 'jh->b_frozen_data == NULL'.
1437 if (jh->b_modified == 0) {
1439 * This buffer's got modified and becoming part
1440 * of the transaction. This needs to be done
1441 * once a transaction -bzzz
1443 if (handle->h_buffer_credits <= 0) {
1448 handle->h_buffer_credits--;
1452 * fastpath, to avoid expensive locking. If this buffer is already
1453 * on the running transaction's metadata list there is nothing to do.
1454 * Nobody can take it off again because there is a handle open.
1455 * I _think_ we're OK here with SMP barriers - a mistaken decision will
1456 * result in this test being false, so we go in and take the locks.
1458 if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) {
1459 JBUFFER_TRACE(jh, "fastpath");
1460 if (unlikely(jh->b_transaction !=
1461 journal->j_running_transaction)) {
1462 printk(KERN_ERR "JBD2: %s: "
1463 "jh->b_transaction (%llu, %p, %u) != "
1464 "journal->j_running_transaction (%p, %u)\n",
1466 (unsigned long long) bh->b_blocknr,
1468 jh->b_transaction ? jh->b_transaction->t_tid : 0,
1469 journal->j_running_transaction,
1470 journal->j_running_transaction ?
1471 journal->j_running_transaction->t_tid : 0);
1477 set_buffer_jbddirty(bh);
1480 * Metadata already on the current transaction list doesn't
1481 * need to be filed. Metadata on another transaction's list must
1482 * be committing, and will be refiled once the commit completes:
1483 * leave it alone for now.
1485 if (jh->b_transaction != transaction) {
1486 JBUFFER_TRACE(jh, "already on other transaction");
1487 if (unlikely(((jh->b_transaction !=
1488 journal->j_committing_transaction)) ||
1489 (jh->b_next_transaction != transaction))) {
1490 printk(KERN_ERR "jbd2_journal_dirty_metadata: %s: "
1491 "bad jh for block %llu: "
1492 "transaction (%p, %u), "
1493 "jh->b_transaction (%p, %u), "
1494 "jh->b_next_transaction (%p, %u), jlist %u\n",
1496 (unsigned long long) bh->b_blocknr,
1497 transaction, transaction->t_tid,
1500 jh->b_transaction->t_tid : 0,
1501 jh->b_next_transaction,
1502 jh->b_next_transaction ?
1503 jh->b_next_transaction->t_tid : 0,
1508 /* And this case is illegal: we can't reuse another
1509 * transaction's data buffer, ever. */
1513 /* That test should have eliminated the following case: */
1514 J_ASSERT_JH(jh, jh->b_frozen_data == NULL);
1516 JBUFFER_TRACE(jh, "file as BJ_Metadata");
1517 spin_lock(&journal->j_list_lock);
1518 __jbd2_journal_file_buffer(jh, transaction, BJ_Metadata);
1519 spin_unlock(&journal->j_list_lock);
1521 jbd_unlock_bh_state(bh);
1523 JBUFFER_TRACE(jh, "exit");
1528 * jbd2_journal_forget() - bforget() for potentially-journaled buffers.
1529 * @handle: transaction handle
1530 * @bh: bh to 'forget'
1532 * We can only do the bforget if there are no commits pending against the
1533 * buffer. If the buffer is dirty in the current running transaction we
1534 * can safely unlink it.
1536 * bh may not be a journalled buffer at all - it may be a non-JBD
1537 * buffer which came off the hashtable. Check for this.
1539 * Decrements bh->b_count by one.
1541 * Allow this call even if the handle has aborted --- it may be part of
1542 * the caller's cleanup after an abort.
1544 int jbd2_journal_forget (handle_t *handle, struct buffer_head *bh)
1546 transaction_t *transaction = handle->h_transaction;
1548 struct journal_head *jh;
1549 int drop_reserve = 0;
1551 int was_modified = 0;
1553 if (is_handle_aborted(handle))
1555 journal = transaction->t_journal;
1557 BUFFER_TRACE(bh, "entry");
1559 jbd_lock_bh_state(bh);
1561 if (!buffer_jbd(bh))
1565 /* Critical error: attempting to delete a bitmap buffer, maybe?
1566 * Don't do any jbd operations, and return an error. */
1567 if (!J_EXPECT_JH(jh, !jh->b_committed_data,
1568 "inconsistent data on disk")) {
1573 /* keep track of whether or not this transaction modified us */
1574 was_modified = jh->b_modified;
1577 * The buffer's going from the transaction, we must drop
1578 * all references -bzzz
1582 if (jh->b_transaction == transaction) {
1583 J_ASSERT_JH(jh, !jh->b_frozen_data);
1585 /* If we are forgetting a buffer which is already part
1586 * of this transaction, then we can just drop it from
1587 * the transaction immediately. */
1588 clear_buffer_dirty(bh);
1589 clear_buffer_jbddirty(bh);
1591 JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
1594 * we only want to drop a reference if this transaction
1595 * modified the buffer
1601 * We are no longer going to journal this buffer.
1602 * However, the commit of this transaction is still
1603 * important to the buffer: the delete that we are now
1604 * processing might obsolete an old log entry, so by
1605 * committing, we can satisfy the buffer's checkpoint.
1607 * So, if we have a checkpoint on the buffer, we should
1608 * now refile the buffer on our BJ_Forget list so that
1609 * we know to remove the checkpoint after we commit.
1612 spin_lock(&journal->j_list_lock);
1613 if (jh->b_cp_transaction) {
1614 __jbd2_journal_temp_unlink_buffer(jh);
1615 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
1617 __jbd2_journal_unfile_buffer(jh);
1618 if (!buffer_jbd(bh)) {
1619 spin_unlock(&journal->j_list_lock);
1623 spin_unlock(&journal->j_list_lock);
1624 } else if (jh->b_transaction) {
1625 J_ASSERT_JH(jh, (jh->b_transaction ==
1626 journal->j_committing_transaction));
1627 /* However, if the buffer is still owned by a prior
1628 * (committing) transaction, we can't drop it yet... */
1629 JBUFFER_TRACE(jh, "belongs to older transaction");
1630 /* ... but we CAN drop it from the new transaction through
1631 * marking the buffer as freed and set j_next_transaction to
1632 * the new transaction, so that not only the commit code
1633 * knows it should clear dirty bits when it is done with the
1634 * buffer, but also the buffer can be checkpointed only
1635 * after the new transaction commits. */
1637 set_buffer_freed(bh);
1639 if (!jh->b_next_transaction) {
1640 spin_lock(&journal->j_list_lock);
1641 jh->b_next_transaction = transaction;
1642 spin_unlock(&journal->j_list_lock);
1644 J_ASSERT(jh->b_next_transaction == transaction);
1647 * only drop a reference if this transaction modified
1655 * Finally, if the buffer is not belongs to any
1656 * transaction, we can just drop it now if it has no
1659 spin_lock(&journal->j_list_lock);
1660 if (!jh->b_cp_transaction) {
1661 JBUFFER_TRACE(jh, "belongs to none transaction");
1662 spin_unlock(&journal->j_list_lock);
1667 * Otherwise, if the buffer has been written to disk,
1668 * it is safe to remove the checkpoint and drop it.
1670 if (!buffer_dirty(bh)) {
1671 __jbd2_journal_remove_checkpoint(jh);
1672 spin_unlock(&journal->j_list_lock);
1677 * The buffer is still not written to disk, we should
1678 * attach this buffer to current transaction so that the
1679 * buffer can be checkpointed only after the current
1680 * transaction commits.
1682 clear_buffer_dirty(bh);
1683 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
1684 spin_unlock(&journal->j_list_lock);
1687 jbd_unlock_bh_state(bh);
1691 /* no need to reserve log space for this block -bzzz */
1692 handle->h_buffer_credits++;
1697 jbd_unlock_bh_state(bh);
1703 * jbd2_journal_stop() - complete a transaction
1704 * @handle: transaction to complete.
1706 * All done for a particular handle.
1708 * There is not much action needed here. We just return any remaining
1709 * buffer credits to the transaction and remove the handle. The only
1710 * complication is that we need to start a commit operation if the
1711 * filesystem is marked for synchronous update.
1713 * jbd2_journal_stop itself will not usually return an error, but it may
1714 * do so in unusual circumstances. In particular, expect it to
1715 * return -EIO if a jbd2_journal_abort has been executed since the
1716 * transaction began.
1718 int jbd2_journal_stop(handle_t *handle)
1720 transaction_t *transaction = handle->h_transaction;
1722 int err = 0, wait_for_commit = 0;
1728 * Handle is already detached from the transaction so
1729 * there is nothing to do other than decrease a refcount,
1730 * or free the handle if refcount drops to zero
1732 if (--handle->h_ref > 0) {
1733 jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
1737 if (handle->h_rsv_handle)
1738 jbd2_free_handle(handle->h_rsv_handle);
1742 journal = transaction->t_journal;
1744 J_ASSERT(journal_current_handle() == handle);
1746 if (is_handle_aborted(handle))
1749 J_ASSERT(atomic_read(&transaction->t_updates) > 0);
1751 if (--handle->h_ref > 0) {
1752 jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
1757 jbd_debug(4, "Handle %p going down\n", handle);
1758 trace_jbd2_handle_stats(journal->j_fs_dev->bd_dev,
1760 handle->h_type, handle->h_line_no,
1761 jiffies - handle->h_start_jiffies,
1762 handle->h_sync, handle->h_requested_credits,
1763 (handle->h_requested_credits -
1764 handle->h_buffer_credits));
1767 * Implement synchronous transaction batching. If the handle
1768 * was synchronous, don't force a commit immediately. Let's
1769 * yield and let another thread piggyback onto this
1770 * transaction. Keep doing that while new threads continue to
1771 * arrive. It doesn't cost much - we're about to run a commit
1772 * and sleep on IO anyway. Speeds up many-threaded, many-dir
1773 * operations by 30x or more...
1775 * We try and optimize the sleep time against what the
1776 * underlying disk can do, instead of having a static sleep
1777 * time. This is useful for the case where our storage is so
1778 * fast that it is more optimal to go ahead and force a flush
1779 * and wait for the transaction to be committed than it is to
1780 * wait for an arbitrary amount of time for new writers to
1781 * join the transaction. We achieve this by measuring how
1782 * long it takes to commit a transaction, and compare it with
1783 * how long this transaction has been running, and if run time
1784 * < commit time then we sleep for the delta and commit. This
1785 * greatly helps super fast disks that would see slowdowns as
1786 * more threads started doing fsyncs.
1788 * But don't do this if this process was the most recent one
1789 * to perform a synchronous write. We do this to detect the
1790 * case where a single process is doing a stream of sync
1791 * writes. No point in waiting for joiners in that case.
1793 * Setting max_batch_time to 0 disables this completely.
1796 if (handle->h_sync && journal->j_last_sync_writer != pid &&
1797 journal->j_max_batch_time) {
1798 u64 commit_time, trans_time;
1800 journal->j_last_sync_writer = pid;
1802 read_lock(&journal->j_state_lock);
1803 commit_time = journal->j_average_commit_time;
1804 read_unlock(&journal->j_state_lock);
1806 trans_time = ktime_to_ns(ktime_sub(ktime_get(),
1807 transaction->t_start_time));
1809 commit_time = max_t(u64, commit_time,
1810 1000*journal->j_min_batch_time);
1811 commit_time = min_t(u64, commit_time,
1812 1000*journal->j_max_batch_time);
1814 if (trans_time < commit_time) {
1815 ktime_t expires = ktime_add_ns(ktime_get(),
1817 set_current_state(TASK_UNINTERRUPTIBLE);
1818 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1823 transaction->t_synchronous_commit = 1;
1824 current->journal_info = NULL;
1825 atomic_sub(handle->h_buffer_credits,
1826 &transaction->t_outstanding_credits);
1829 * If the handle is marked SYNC, we need to set another commit
1830 * going! We also want to force a commit if the current
1831 * transaction is occupying too much of the log, or if the
1832 * transaction is too old now.
1834 if (handle->h_sync ||
1835 (atomic_read(&transaction->t_outstanding_credits) >
1836 journal->j_max_transaction_buffers) ||
1837 time_after_eq(jiffies, transaction->t_expires)) {
1838 /* Do this even for aborted journals: an abort still
1839 * completes the commit thread, it just doesn't write
1840 * anything to disk. */
1842 jbd_debug(2, "transaction too old, requesting commit for "
1843 "handle %p\n", handle);
1844 /* This is non-blocking */
1845 jbd2_log_start_commit(journal, transaction->t_tid);
1848 * Special case: JBD2_SYNC synchronous updates require us
1849 * to wait for the commit to complete.
1851 if (handle->h_sync && !(current->flags & PF_MEMALLOC))
1852 wait_for_commit = 1;
1856 * Once we drop t_updates, if it goes to zero the transaction
1857 * could start committing on us and eventually disappear. So
1858 * once we do this, we must not dereference transaction
1861 tid = transaction->t_tid;
1862 if (atomic_dec_and_test(&transaction->t_updates)) {
1863 wake_up(&journal->j_wait_updates);
1864 if (journal->j_barrier_count)
1865 wake_up(&journal->j_wait_transaction_locked);
1868 rwsem_release(&journal->j_trans_commit_map, 1, _THIS_IP_);
1870 if (wait_for_commit)
1871 err = jbd2_log_wait_commit(journal, tid);
1873 if (handle->h_rsv_handle)
1874 jbd2_journal_free_reserved(handle->h_rsv_handle);
1877 * Scope of the GFP_NOFS context is over here and so we can restore the
1878 * original alloc context.
1880 memalloc_nofs_restore(handle->saved_alloc_context);
1881 jbd2_free_handle(handle);
1887 * List management code snippets: various functions for manipulating the
1888 * transaction buffer lists.
1893 * Append a buffer to a transaction list, given the transaction's list head
1896 * j_list_lock is held.
1898 * jbd_lock_bh_state(jh2bh(jh)) is held.
1902 __blist_add_buffer(struct journal_head **list, struct journal_head *jh)
1905 jh->b_tnext = jh->b_tprev = jh;
1908 /* Insert at the tail of the list to preserve order */
1909 struct journal_head *first = *list, *last = first->b_tprev;
1911 jh->b_tnext = first;
1912 last->b_tnext = first->b_tprev = jh;
1917 * Remove a buffer from a transaction list, given the transaction's list
1920 * Called with j_list_lock held, and the journal may not be locked.
1922 * jbd_lock_bh_state(jh2bh(jh)) is held.
1926 __blist_del_buffer(struct journal_head **list, struct journal_head *jh)
1929 *list = jh->b_tnext;
1933 jh->b_tprev->b_tnext = jh->b_tnext;
1934 jh->b_tnext->b_tprev = jh->b_tprev;
1938 * Remove a buffer from the appropriate transaction list.
1940 * Note that this function can *change* the value of
1941 * bh->b_transaction->t_buffers, t_forget, t_shadow_list, t_log_list or
1942 * t_reserved_list. If the caller is holding onto a copy of one of these
1943 * pointers, it could go bad. Generally the caller needs to re-read the
1944 * pointer from the transaction_t.
1946 * Called under j_list_lock.
1948 static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh)
1950 struct journal_head **list = NULL;
1951 transaction_t *transaction;
1952 struct buffer_head *bh = jh2bh(jh);
1954 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1955 transaction = jh->b_transaction;
1957 assert_spin_locked(&transaction->t_journal->j_list_lock);
1959 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1960 if (jh->b_jlist != BJ_None)
1961 J_ASSERT_JH(jh, transaction != NULL);
1963 switch (jh->b_jlist) {
1967 transaction->t_nr_buffers--;
1968 J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
1969 list = &transaction->t_buffers;
1972 list = &transaction->t_forget;
1975 list = &transaction->t_shadow_list;
1978 list = &transaction->t_reserved_list;
1982 __blist_del_buffer(list, jh);
1983 jh->b_jlist = BJ_None;
1984 if (transaction && is_journal_aborted(transaction->t_journal))
1985 clear_buffer_jbddirty(bh);
1986 else if (test_clear_buffer_jbddirty(bh))
1987 mark_buffer_dirty(bh); /* Expose it to the VM */
1991 * Remove buffer from all transactions.
1993 * Called with bh_state lock and j_list_lock
1995 * jh and bh may be already freed when this function returns.
1997 static void __jbd2_journal_unfile_buffer(struct journal_head *jh)
1999 J_ASSERT_JH(jh, jh->b_transaction != NULL);
2000 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
2002 __jbd2_journal_temp_unlink_buffer(jh);
2003 jh->b_transaction = NULL;
2004 jbd2_journal_put_journal_head(jh);
2007 void jbd2_journal_unfile_buffer(journal_t *journal, struct journal_head *jh)
2009 struct buffer_head *bh = jh2bh(jh);
2011 /* Get reference so that buffer cannot be freed before we unlock it */
2013 jbd_lock_bh_state(bh);
2014 spin_lock(&journal->j_list_lock);
2015 __jbd2_journal_unfile_buffer(jh);
2016 spin_unlock(&journal->j_list_lock);
2017 jbd_unlock_bh_state(bh);
2022 * Called from jbd2_journal_try_to_free_buffers().
2024 * Called under jbd_lock_bh_state(bh)
2027 __journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh)
2029 struct journal_head *jh;
2033 if (buffer_locked(bh) || buffer_dirty(bh))
2036 if (jh->b_next_transaction != NULL || jh->b_transaction != NULL)
2039 spin_lock(&journal->j_list_lock);
2040 if (jh->b_cp_transaction != NULL) {
2041 /* written-back checkpointed metadata buffer */
2042 JBUFFER_TRACE(jh, "remove from checkpoint list");
2043 __jbd2_journal_remove_checkpoint(jh);
2045 spin_unlock(&journal->j_list_lock);
2051 * jbd2_journal_try_to_free_buffers() - try to free page buffers.
2052 * @journal: journal for operation
2053 * @page: to try and free
2054 * @gfp_mask: we use the mask to detect how hard should we try to release
2055 * buffers. If __GFP_DIRECT_RECLAIM and __GFP_FS is set, we wait for commit
2056 * code to release the buffers.
2059 * For all the buffers on this page,
2060 * if they are fully written out ordered data, move them onto BUF_CLEAN
2061 * so try_to_free_buffers() can reap them.
2063 * This function returns non-zero if we wish try_to_free_buffers()
2064 * to be called. We do this if the page is releasable by try_to_free_buffers().
2065 * We also do it if the page has locked or dirty buffers and the caller wants
2066 * us to perform sync or async writeout.
2068 * This complicates JBD locking somewhat. We aren't protected by the
2069 * BKL here. We wish to remove the buffer from its committing or
2070 * running transaction's ->t_datalist via __jbd2_journal_unfile_buffer.
2072 * This may *change* the value of transaction_t->t_datalist, so anyone
2073 * who looks at t_datalist needs to lock against this function.
2075 * Even worse, someone may be doing a jbd2_journal_dirty_data on this
2076 * buffer. So we need to lock against that. jbd2_journal_dirty_data()
2077 * will come out of the lock with the buffer dirty, which makes it
2078 * ineligible for release here.
2080 * Who else is affected by this? hmm... Really the only contender
2081 * is do_get_write_access() - it could be looking at the buffer while
2082 * journal_try_to_free_buffer() is changing its state. But that
2083 * cannot happen because we never reallocate freed data as metadata
2084 * while the data is part of a transaction. Yes?
2086 * Return 0 on failure, 1 on success
2088 int jbd2_journal_try_to_free_buffers(journal_t *journal,
2089 struct page *page, gfp_t gfp_mask)
2091 struct buffer_head *head;
2092 struct buffer_head *bh;
2093 bool has_write_io_error = false;
2096 J_ASSERT(PageLocked(page));
2098 head = page_buffers(page);
2101 struct journal_head *jh;
2104 * We take our own ref against the journal_head here to avoid
2105 * having to add tons of locking around each instance of
2106 * jbd2_journal_put_journal_head().
2108 jh = jbd2_journal_grab_journal_head(bh);
2112 jbd_lock_bh_state(bh);
2113 __journal_try_to_free_buffer(journal, bh);
2114 jbd2_journal_put_journal_head(jh);
2115 jbd_unlock_bh_state(bh);
2120 * If we free a metadata buffer which has been failed to
2121 * write out, the jbd2 checkpoint procedure will not detect
2122 * this failure and may lead to filesystem inconsistency
2123 * after cleanup journal tail.
2125 if (buffer_write_io_error(bh)) {
2126 pr_err("JBD2: Error while async write back metadata bh %llu.",
2127 (unsigned long long)bh->b_blocknr);
2128 has_write_io_error = true;
2130 } while ((bh = bh->b_this_page) != head);
2132 ret = try_to_free_buffers(page);
2135 if (has_write_io_error)
2136 jbd2_journal_abort(journal, -EIO);
2142 * This buffer is no longer needed. If it is on an older transaction's
2143 * checkpoint list we need to record it on this transaction's forget list
2144 * to pin this buffer (and hence its checkpointing transaction) down until
2145 * this transaction commits. If the buffer isn't on a checkpoint list, we
2147 * Returns non-zero if JBD no longer has an interest in the buffer.
2149 * Called under j_list_lock.
2151 * Called under jbd_lock_bh_state(bh).
2153 static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
2156 struct buffer_head *bh = jh2bh(jh);
2158 if (jh->b_cp_transaction) {
2159 JBUFFER_TRACE(jh, "on running+cp transaction");
2160 __jbd2_journal_temp_unlink_buffer(jh);
2162 * We don't want to write the buffer anymore, clear the
2163 * bit so that we don't confuse checks in
2164 * __journal_file_buffer
2166 clear_buffer_dirty(bh);
2167 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
2170 JBUFFER_TRACE(jh, "on running transaction");
2171 __jbd2_journal_unfile_buffer(jh);
2177 * jbd2_journal_invalidatepage
2179 * This code is tricky. It has a number of cases to deal with.
2181 * There are two invariants which this code relies on:
2183 * i_size must be updated on disk before we start calling invalidatepage on the
2186 * This is done in ext3 by defining an ext3_setattr method which
2187 * updates i_size before truncate gets going. By maintaining this
2188 * invariant, we can be sure that it is safe to throw away any buffers
2189 * attached to the current transaction: once the transaction commits,
2190 * we know that the data will not be needed.
2192 * Note however that we can *not* throw away data belonging to the
2193 * previous, committing transaction!
2195 * Any disk blocks which *are* part of the previous, committing
2196 * transaction (and which therefore cannot be discarded immediately) are
2197 * not going to be reused in the new running transaction
2199 * The bitmap committed_data images guarantee this: any block which is
2200 * allocated in one transaction and removed in the next will be marked
2201 * as in-use in the committed_data bitmap, so cannot be reused until
2202 * the next transaction to delete the block commits. This means that
2203 * leaving committing buffers dirty is quite safe: the disk blocks
2204 * cannot be reallocated to a different file and so buffer aliasing is
2208 * The above applies mainly to ordered data mode. In writeback mode we
2209 * don't make guarantees about the order in which data hits disk --- in
2210 * particular we don't guarantee that new dirty data is flushed before
2211 * transaction commit --- so it is always safe just to discard data
2212 * immediately in that mode. --sct
2216 * The journal_unmap_buffer helper function returns zero if the buffer
2217 * concerned remains pinned as an anonymous buffer belonging to an older
2220 * We're outside-transaction here. Either or both of j_running_transaction
2221 * and j_committing_transaction may be NULL.
2223 static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh,
2226 transaction_t *transaction;
2227 struct journal_head *jh;
2230 BUFFER_TRACE(bh, "entry");
2233 * It is safe to proceed here without the j_list_lock because the
2234 * buffers cannot be stolen by try_to_free_buffers as long as we are
2235 * holding the page lock. --sct
2238 if (!buffer_jbd(bh))
2239 goto zap_buffer_unlocked;
2241 /* OK, we have data buffer in journaled mode */
2242 write_lock(&journal->j_state_lock);
2243 jbd_lock_bh_state(bh);
2244 spin_lock(&journal->j_list_lock);
2246 jh = jbd2_journal_grab_journal_head(bh);
2248 goto zap_buffer_no_jh;
2251 * We cannot remove the buffer from checkpoint lists until the
2252 * transaction adding inode to orphan list (let's call it T)
2253 * is committed. Otherwise if the transaction changing the
2254 * buffer would be cleaned from the journal before T is
2255 * committed, a crash will cause that the correct contents of
2256 * the buffer will be lost. On the other hand we have to
2257 * clear the buffer dirty bit at latest at the moment when the
2258 * transaction marking the buffer as freed in the filesystem
2259 * structures is committed because from that moment on the
2260 * block can be reallocated and used by a different page.
2261 * Since the block hasn't been freed yet but the inode has
2262 * already been added to orphan list, it is safe for us to add
2263 * the buffer to BJ_Forget list of the newest transaction.
2265 * Also we have to clear buffer_mapped flag of a truncated buffer
2266 * because the buffer_head may be attached to the page straddling
2267 * i_size (can happen only when blocksize < pagesize) and thus the
2268 * buffer_head can be reused when the file is extended again. So we end
2269 * up keeping around invalidated buffers attached to transactions'
2270 * BJ_Forget list just to stop checkpointing code from cleaning up
2271 * the transaction this buffer was modified in.
2273 transaction = jh->b_transaction;
2274 if (transaction == NULL) {
2275 /* First case: not on any transaction. If it
2276 * has no checkpoint link, then we can zap it:
2277 * it's a writeback-mode buffer so we don't care
2278 * if it hits disk safely. */
2279 if (!jh->b_cp_transaction) {
2280 JBUFFER_TRACE(jh, "not on any transaction: zap");
2284 if (!buffer_dirty(bh)) {
2285 /* bdflush has written it. We can drop it now */
2286 __jbd2_journal_remove_checkpoint(jh);
2290 /* OK, it must be in the journal but still not
2291 * written fully to disk: it's metadata or
2292 * journaled data... */
2294 if (journal->j_running_transaction) {
2295 /* ... and once the current transaction has
2296 * committed, the buffer won't be needed any
2298 JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
2299 may_free = __dispose_buffer(jh,
2300 journal->j_running_transaction);
2303 /* There is no currently-running transaction. So the
2304 * orphan record which we wrote for this file must have
2305 * passed into commit. We must attach this buffer to
2306 * the committing transaction, if it exists. */
2307 if (journal->j_committing_transaction) {
2308 JBUFFER_TRACE(jh, "give to committing trans");
2309 may_free = __dispose_buffer(jh,
2310 journal->j_committing_transaction);
2313 /* The orphan record's transaction has
2314 * committed. We can cleanse this buffer */
2315 clear_buffer_jbddirty(bh);
2316 __jbd2_journal_remove_checkpoint(jh);
2320 } else if (transaction == journal->j_committing_transaction) {
2321 JBUFFER_TRACE(jh, "on committing transaction");
2323 * The buffer is committing, we simply cannot touch
2324 * it. If the page is straddling i_size we have to wait
2325 * for commit and try again.
2328 jbd2_journal_put_journal_head(jh);
2329 spin_unlock(&journal->j_list_lock);
2330 jbd_unlock_bh_state(bh);
2331 write_unlock(&journal->j_state_lock);
2335 * OK, buffer won't be reachable after truncate. We just clear
2336 * b_modified to not confuse transaction credit accounting, and
2337 * set j_next_transaction to the running transaction (if there
2338 * is one) and mark buffer as freed so that commit code knows
2339 * it should clear dirty bits when it is done with the buffer.
2341 set_buffer_freed(bh);
2342 if (journal->j_running_transaction && buffer_jbddirty(bh))
2343 jh->b_next_transaction = journal->j_running_transaction;
2345 jbd2_journal_put_journal_head(jh);
2346 spin_unlock(&journal->j_list_lock);
2347 jbd_unlock_bh_state(bh);
2348 write_unlock(&journal->j_state_lock);
2351 /* Good, the buffer belongs to the running transaction.
2352 * We are writing our own transaction's data, not any
2353 * previous one's, so it is safe to throw it away
2354 * (remember that we expect the filesystem to have set
2355 * i_size already for this truncate so recovery will not
2356 * expose the disk blocks we are discarding here.) */
2357 J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
2358 JBUFFER_TRACE(jh, "on running transaction");
2359 may_free = __dispose_buffer(jh, transaction);
2364 * This is tricky. Although the buffer is truncated, it may be reused
2365 * if blocksize < pagesize and it is attached to the page straddling
2366 * EOF. Since the buffer might have been added to BJ_Forget list of the
2367 * running transaction, journal_get_write_access() won't clear
2368 * b_modified and credit accounting gets confused. So clear b_modified
2372 jbd2_journal_put_journal_head(jh);
2374 spin_unlock(&journal->j_list_lock);
2375 jbd_unlock_bh_state(bh);
2376 write_unlock(&journal->j_state_lock);
2377 zap_buffer_unlocked:
2378 clear_buffer_dirty(bh);
2379 J_ASSERT_BH(bh, !buffer_jbddirty(bh));
2380 clear_buffer_mapped(bh);
2381 clear_buffer_req(bh);
2382 clear_buffer_new(bh);
2383 clear_buffer_delay(bh);
2384 clear_buffer_unwritten(bh);
2390 * jbd2_journal_invalidatepage()
2391 * @journal: journal to use for flush...
2392 * @page: page to flush
2393 * @offset: start of the range to invalidate
2394 * @length: length of the range to invalidate
2396 * Reap page buffers containing data after in the specified range in page.
2397 * Can return -EBUSY if buffers are part of the committing transaction and
2398 * the page is straddling i_size. Caller then has to wait for current commit
2401 int jbd2_journal_invalidatepage(journal_t *journal,
2403 unsigned int offset,
2404 unsigned int length)
2406 struct buffer_head *head, *bh, *next;
2407 unsigned int stop = offset + length;
2408 unsigned int curr_off = 0;
2409 int partial_page = (offset || length < PAGE_SIZE);
2413 if (!PageLocked(page))
2415 if (!page_has_buffers(page))
2418 BUG_ON(stop > PAGE_SIZE || stop < length);
2420 /* We will potentially be playing with lists other than just the
2421 * data lists (especially for journaled data mode), so be
2422 * cautious in our locking. */
2424 head = bh = page_buffers(page);
2426 unsigned int next_off = curr_off + bh->b_size;
2427 next = bh->b_this_page;
2429 if (next_off > stop)
2432 if (offset <= curr_off) {
2433 /* This block is wholly outside the truncation point */
2435 ret = journal_unmap_buffer(journal, bh, partial_page);
2441 curr_off = next_off;
2444 } while (bh != head);
2446 if (!partial_page) {
2447 if (may_free && try_to_free_buffers(page))
2448 J_ASSERT(!page_has_buffers(page));
2454 * File a buffer on the given transaction list.
2456 void __jbd2_journal_file_buffer(struct journal_head *jh,
2457 transaction_t *transaction, int jlist)
2459 struct journal_head **list = NULL;
2461 struct buffer_head *bh = jh2bh(jh);
2463 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
2464 assert_spin_locked(&transaction->t_journal->j_list_lock);
2466 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
2467 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
2468 jh->b_transaction == NULL);
2470 if (jh->b_transaction && jh->b_jlist == jlist)
2473 if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
2474 jlist == BJ_Shadow || jlist == BJ_Forget) {
2476 * For metadata buffers, we track dirty bit in buffer_jbddirty
2477 * instead of buffer_dirty. We should not see a dirty bit set
2478 * here because we clear it in do_get_write_access but e.g.
2479 * tune2fs can modify the sb and set the dirty bit at any time
2480 * so we try to gracefully handle that.
2482 if (buffer_dirty(bh))
2483 warn_dirty_buffer(bh);
2484 if (test_clear_buffer_dirty(bh) ||
2485 test_clear_buffer_jbddirty(bh))
2489 if (jh->b_transaction)
2490 __jbd2_journal_temp_unlink_buffer(jh);
2492 jbd2_journal_grab_journal_head(bh);
2493 jh->b_transaction = transaction;
2497 J_ASSERT_JH(jh, !jh->b_committed_data);
2498 J_ASSERT_JH(jh, !jh->b_frozen_data);
2501 transaction->t_nr_buffers++;
2502 list = &transaction->t_buffers;
2505 list = &transaction->t_forget;
2508 list = &transaction->t_shadow_list;
2511 list = &transaction->t_reserved_list;
2515 __blist_add_buffer(list, jh);
2516 jh->b_jlist = jlist;
2519 set_buffer_jbddirty(bh);
2522 void jbd2_journal_file_buffer(struct journal_head *jh,
2523 transaction_t *transaction, int jlist)
2525 jbd_lock_bh_state(jh2bh(jh));
2526 spin_lock(&transaction->t_journal->j_list_lock);
2527 __jbd2_journal_file_buffer(jh, transaction, jlist);
2528 spin_unlock(&transaction->t_journal->j_list_lock);
2529 jbd_unlock_bh_state(jh2bh(jh));
2533 * Remove a buffer from its current buffer list in preparation for
2534 * dropping it from its current transaction entirely. If the buffer has
2535 * already started to be used by a subsequent transaction, refile the
2536 * buffer on that transaction's metadata list.
2538 * Called under j_list_lock
2539 * Called under jbd_lock_bh_state(jh2bh(jh))
2541 * jh and bh may be already free when this function returns
2543 void __jbd2_journal_refile_buffer(struct journal_head *jh)
2545 int was_dirty, jlist;
2546 struct buffer_head *bh = jh2bh(jh);
2548 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
2549 if (jh->b_transaction)
2550 assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
2552 /* If the buffer is now unused, just drop it. */
2553 if (jh->b_next_transaction == NULL) {
2554 __jbd2_journal_unfile_buffer(jh);
2559 * It has been modified by a later transaction: add it to the new
2560 * transaction's metadata list.
2563 was_dirty = test_clear_buffer_jbddirty(bh);
2564 __jbd2_journal_temp_unlink_buffer(jh);
2567 * b_transaction must be set, otherwise the new b_transaction won't
2568 * be holding jh reference
2570 J_ASSERT_JH(jh, jh->b_transaction != NULL);
2573 * We set b_transaction here because b_next_transaction will inherit
2574 * our jh reference and thus __jbd2_journal_file_buffer() must not
2577 WRITE_ONCE(jh->b_transaction, jh->b_next_transaction);
2578 WRITE_ONCE(jh->b_next_transaction, NULL);
2579 if (buffer_freed(bh))
2581 else if (jh->b_modified)
2582 jlist = BJ_Metadata;
2584 jlist = BJ_Reserved;
2585 __jbd2_journal_file_buffer(jh, jh->b_transaction, jlist);
2586 J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
2589 set_buffer_jbddirty(bh);
2593 * __jbd2_journal_refile_buffer() with necessary locking added. We take our
2594 * bh reference so that we can safely unlock bh.
2596 * The jh and bh may be freed by this call.
2598 void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh)
2600 struct buffer_head *bh = jh2bh(jh);
2602 /* Get reference so that buffer cannot be freed before we unlock it */
2604 jbd_lock_bh_state(bh);
2605 spin_lock(&journal->j_list_lock);
2606 __jbd2_journal_refile_buffer(jh);
2607 jbd_unlock_bh_state(bh);
2608 spin_unlock(&journal->j_list_lock);
2613 * File inode in the inode list of the handle's transaction
2615 static int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode,
2616 unsigned long flags, loff_t start_byte, loff_t end_byte)
2618 transaction_t *transaction = handle->h_transaction;
2621 if (is_handle_aborted(handle))
2623 journal = transaction->t_journal;
2625 jbd_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino,
2626 transaction->t_tid);
2628 spin_lock(&journal->j_list_lock);
2629 jinode->i_flags |= flags;
2631 if (jinode->i_dirty_end) {
2632 jinode->i_dirty_start = min(jinode->i_dirty_start, start_byte);
2633 jinode->i_dirty_end = max(jinode->i_dirty_end, end_byte);
2635 jinode->i_dirty_start = start_byte;
2636 jinode->i_dirty_end = end_byte;
2639 /* Is inode already attached where we need it? */
2640 if (jinode->i_transaction == transaction ||
2641 jinode->i_next_transaction == transaction)
2645 * We only ever set this variable to 1 so the test is safe. Since
2646 * t_need_data_flush is likely to be set, we do the test to save some
2647 * cacheline bouncing
2649 if (!transaction->t_need_data_flush)
2650 transaction->t_need_data_flush = 1;
2651 /* On some different transaction's list - should be
2652 * the committing one */
2653 if (jinode->i_transaction) {
2654 J_ASSERT(jinode->i_next_transaction == NULL);
2655 J_ASSERT(jinode->i_transaction ==
2656 journal->j_committing_transaction);
2657 jinode->i_next_transaction = transaction;
2660 /* Not on any transaction list... */
2661 J_ASSERT(!jinode->i_next_transaction);
2662 jinode->i_transaction = transaction;
2663 list_add(&jinode->i_list, &transaction->t_inode_list);
2665 spin_unlock(&journal->j_list_lock);
2670 int jbd2_journal_inode_ranged_write(handle_t *handle,
2671 struct jbd2_inode *jinode, loff_t start_byte, loff_t length)
2673 return jbd2_journal_file_inode(handle, jinode,
2674 JI_WRITE_DATA | JI_WAIT_DATA, start_byte,
2675 start_byte + length - 1);
2678 int jbd2_journal_inode_ranged_wait(handle_t *handle, struct jbd2_inode *jinode,
2679 loff_t start_byte, loff_t length)
2681 return jbd2_journal_file_inode(handle, jinode, JI_WAIT_DATA,
2682 start_byte, start_byte + length - 1);
2686 * File truncate and transaction commit interact with each other in a
2687 * non-trivial way. If a transaction writing data block A is
2688 * committing, we cannot discard the data by truncate until we have
2689 * written them. Otherwise if we crashed after the transaction with
2690 * write has committed but before the transaction with truncate has
2691 * committed, we could see stale data in block A. This function is a
2692 * helper to solve this problem. It starts writeout of the truncated
2693 * part in case it is in the committing transaction.
2695 * Filesystem code must call this function when inode is journaled in
2696 * ordered mode before truncation happens and after the inode has been
2697 * placed on orphan list with the new inode size. The second condition
2698 * avoids the race that someone writes new data and we start
2699 * committing the transaction after this function has been called but
2700 * before a transaction for truncate is started (and furthermore it
2701 * allows us to optimize the case where the addition to orphan list
2702 * happens in the same transaction as write --- we don't have to write
2703 * any data in such case).
2705 int jbd2_journal_begin_ordered_truncate(journal_t *journal,
2706 struct jbd2_inode *jinode,
2709 transaction_t *inode_trans, *commit_trans;
2712 /* This is a quick check to avoid locking if not necessary */
2713 if (!jinode->i_transaction)
2715 /* Locks are here just to force reading of recent values, it is
2716 * enough that the transaction was not committing before we started
2717 * a transaction adding the inode to orphan list */
2718 read_lock(&journal->j_state_lock);
2719 commit_trans = journal->j_committing_transaction;
2720 read_unlock(&journal->j_state_lock);
2721 spin_lock(&journal->j_list_lock);
2722 inode_trans = jinode->i_transaction;
2723 spin_unlock(&journal->j_list_lock);
2724 if (inode_trans == commit_trans) {
2725 ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping,
2726 new_size, LLONG_MAX);
2728 jbd2_journal_abort(journal, ret);