1f5166768STheodore Ts'o // SPDX-License-Identifier: GPL-2.0+
2470decc6SDave Kleikamp /*
358862699SUwe Kleine-König * linux/fs/jbd2/transaction.c
4470decc6SDave Kleikamp *
5470decc6SDave Kleikamp * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
6470decc6SDave Kleikamp *
7470decc6SDave Kleikamp * Copyright 1998 Red Hat corp --- All Rights Reserved
8470decc6SDave Kleikamp *
9470decc6SDave Kleikamp * Generic filesystem transaction handling code; part of the ext2fs
10470decc6SDave Kleikamp * journaling system.
11470decc6SDave Kleikamp *
12470decc6SDave Kleikamp * This file manages transactions (compound commits managed by the
13470decc6SDave Kleikamp * journaling code) and handles (individual atomic operations by the
14470decc6SDave Kleikamp * filesystem).
15470decc6SDave Kleikamp */
16470decc6SDave Kleikamp
17470decc6SDave Kleikamp #include <linux/time.h>
18470decc6SDave Kleikamp #include <linux/fs.h>
19f7f4bccbSMingming Cao #include <linux/jbd2.h>
20470decc6SDave Kleikamp #include <linux/errno.h>
21470decc6SDave Kleikamp #include <linux/slab.h>
22470decc6SDave Kleikamp #include <linux/timer.h>
23470decc6SDave Kleikamp #include <linux/mm.h>
24470decc6SDave Kleikamp #include <linux/highmem.h>
25e07f7183SJosef Bacik #include <linux/hrtimer.h>
2647def826STheodore Ts'o #include <linux/backing-dev.h>
2744705754SRandy Dunlap #include <linux/bug.h>
2847def826STheodore Ts'o #include <linux/module.h>
2981378da6SMichal Hocko #include <linux/sched/mm.h>
30470decc6SDave Kleikamp
31343d9c28STheodore Ts'o #include <trace/events/jbd2.h>
32343d9c28STheodore Ts'o
337ddae860SAdrian Bunk static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh);
34de1b7941SJan Kara static void __jbd2_journal_unfile_buffer(struct journal_head *jh);
357ddae860SAdrian Bunk
360c2022ecSYongqiang Yang static struct kmem_cache *transaction_cache;
jbd2_journal_init_transaction_cache(void)370c2022ecSYongqiang Yang int __init jbd2_journal_init_transaction_cache(void)
380c2022ecSYongqiang Yang {
390c2022ecSYongqiang Yang J_ASSERT(!transaction_cache);
400c2022ecSYongqiang Yang transaction_cache = kmem_cache_create("jbd2_transaction_s",
410c2022ecSYongqiang Yang sizeof(transaction_t),
420c2022ecSYongqiang Yang 0,
430c2022ecSYongqiang Yang SLAB_HWCACHE_ALIGN|SLAB_TEMPORARY,
440c2022ecSYongqiang Yang NULL);
450d52154bSChengguang Xu if (!transaction_cache) {
460d52154bSChengguang Xu pr_emerg("JBD2: failed to create transaction cache\n");
470c2022ecSYongqiang Yang return -ENOMEM;
480c2022ecSYongqiang Yang }
490d52154bSChengguang Xu return 0;
500d52154bSChengguang Xu }
510c2022ecSYongqiang Yang
jbd2_journal_destroy_transaction_cache(void)520c2022ecSYongqiang Yang void jbd2_journal_destroy_transaction_cache(void)
530c2022ecSYongqiang Yang {
540c2022ecSYongqiang Yang kmem_cache_destroy(transaction_cache);
550c2022ecSYongqiang Yang transaction_cache = NULL;
560c2022ecSYongqiang Yang }
570c2022ecSYongqiang Yang
jbd2_journal_free_transaction(transaction_t * transaction)580c2022ecSYongqiang Yang void jbd2_journal_free_transaction(transaction_t *transaction)
590c2022ecSYongqiang Yang {
600c2022ecSYongqiang Yang if (unlikely(ZERO_OR_NULL_PTR(transaction)))
610c2022ecSYongqiang Yang return;
620c2022ecSYongqiang Yang kmem_cache_free(transaction_cache, transaction);
630c2022ecSYongqiang Yang }
640c2022ecSYongqiang Yang
65470decc6SDave Kleikamp /*
6619014d69SJan Kara * Base amount of descriptor blocks we reserve for each transaction.
679f356e5aSJan Kara */
jbd2_descriptor_blocks_per_trans(journal_t * journal)689f356e5aSJan Kara static int jbd2_descriptor_blocks_per_trans(journal_t *journal)
699f356e5aSJan Kara {
7019014d69SJan Kara int tag_space = journal->j_blocksize - sizeof(journal_header_t);
7119014d69SJan Kara int tags_per_block;
7219014d69SJan Kara
7319014d69SJan Kara /* Subtract UUID */
7419014d69SJan Kara tag_space -= 16;
7519014d69SJan Kara if (jbd2_journal_has_csum_v2or3(journal))
7619014d69SJan Kara tag_space -= sizeof(struct jbd2_journal_block_tail);
7719014d69SJan Kara /* Commit code leaves a slack space of 16 bytes at the end of block */
7819014d69SJan Kara tags_per_block = (tag_space - 16) / journal_tag_bytes(journal);
7919014d69SJan Kara /*
8019014d69SJan Kara * Revoke descriptors are accounted separately so we need to reserve
8119014d69SJan Kara * space for commit block and normal transaction descriptor blocks.
8219014d69SJan Kara */
8319014d69SJan Kara return 1 + DIV_ROUND_UP(journal->j_max_transaction_buffers,
8419014d69SJan Kara tags_per_block);
859f356e5aSJan Kara }
869f356e5aSJan Kara
879f356e5aSJan Kara /*
88f7f4bccbSMingming Cao * jbd2_get_transaction: obtain a new transaction_t object.
89470decc6SDave Kleikamp *
900df6f469SLiu Song * Simply initialise a new transaction. Initialize it in
91470decc6SDave Kleikamp * RUNNING state and add it to the current journal (which should not
92470decc6SDave Kleikamp * have an existing running transaction: we only make a new transaction
93470decc6SDave Kleikamp * once we have started to commit the old one).
94470decc6SDave Kleikamp *
95470decc6SDave Kleikamp * Preconditions:
96470decc6SDave Kleikamp * The journal MUST be locked. We don't perform atomic mallocs on the
97470decc6SDave Kleikamp * new transaction and we can't block without protecting against other
98470decc6SDave Kleikamp * processes trying to touch the journal while it is in transition.
99470decc6SDave Kleikamp *
100470decc6SDave Kleikamp */
101470decc6SDave Kleikamp
jbd2_get_transaction(journal_t * journal,transaction_t * transaction)1020df6f469SLiu Song static void jbd2_get_transaction(journal_t *journal,
1030df6f469SLiu Song transaction_t *transaction)
104470decc6SDave Kleikamp {
105470decc6SDave Kleikamp transaction->t_journal = journal;
106470decc6SDave Kleikamp transaction->t_state = T_RUNNING;
107e07f7183SJosef Bacik transaction->t_start_time = ktime_get();
108470decc6SDave Kleikamp transaction->t_tid = journal->j_transaction_sequence++;
109470decc6SDave Kleikamp transaction->t_expires = jiffies + journal->j_commit_interval;
110a51dca9cSTheodore Ts'o atomic_set(&transaction->t_updates, 0);
1118f7d89f3SJan Kara atomic_set(&transaction->t_outstanding_credits,
1129f356e5aSJan Kara jbd2_descriptor_blocks_per_trans(journal) +
1138f7d89f3SJan Kara atomic_read(&journal->j_reserved_credits));
114fdc3ef88SJan Kara atomic_set(&transaction->t_outstanding_revokes, 0);
1158dd42046STheodore Ts'o atomic_set(&transaction->t_handle_count, 0);
116c851ed54SJan Kara INIT_LIST_HEAD(&transaction->t_inode_list);
1173e624fc7STheodore Ts'o INIT_LIST_HEAD(&transaction->t_private_list);
118470decc6SDave Kleikamp
119470decc6SDave Kleikamp /* Set up the commit timer for the new transaction. */
120b1f485f2SAndreas Dilger journal->j_commit_timer.expires = round_jiffies_up(transaction->t_expires);
121470decc6SDave Kleikamp add_timer(&journal->j_commit_timer);
122470decc6SDave Kleikamp
123470decc6SDave Kleikamp J_ASSERT(journal->j_running_transaction == NULL);
124470decc6SDave Kleikamp journal->j_running_transaction = transaction;
1258e85fb3fSJohann Lombardi transaction->t_max_wait = 0;
1268e85fb3fSJohann Lombardi transaction->t_start = jiffies;
1279fff24aaSTheodore Ts'o transaction->t_requested = 0;
128470decc6SDave Kleikamp }
129470decc6SDave Kleikamp
130470decc6SDave Kleikamp /*
131470decc6SDave Kleikamp * Handle management.
132470decc6SDave Kleikamp *
133470decc6SDave Kleikamp * A handle_t is an object which represents a single atomic update to a
134470decc6SDave Kleikamp * filesystem, and which tracks all of the modifications which form part
135470decc6SDave Kleikamp * of that one update.
136470decc6SDave Kleikamp */
137470decc6SDave Kleikamp
138470decc6SDave Kleikamp /*
13928e35e42STao Ma * Update transaction's maximum wait time, if debugging is enabled.
1406d0bf005STheodore Ts'o *
141f7f497cbSRitesh Harjani * t_max_wait is carefully updated here with use of atomic compare exchange.
142f7f497cbSRitesh Harjani * Note that there could be multiplre threads trying to do this simultaneously
143f7f497cbSRitesh Harjani * hence using cmpxchg to avoid any use of locks in this case.
1442d442920SRitesh Harjani * With this t_max_wait can be updated w/o enabling jbd2_journal_enable_debug.
1456d0bf005STheodore Ts'o */
update_t_max_wait(transaction_t * transaction,unsigned long ts)14628e35e42STao Ma static inline void update_t_max_wait(transaction_t *transaction,
14728e35e42STao Ma unsigned long ts)
1486d0bf005STheodore Ts'o {
149f7f497cbSRitesh Harjani unsigned long oldts, newts;
1502d442920SRitesh Harjani
1512d442920SRitesh Harjani if (time_after(transaction->t_start, ts)) {
152f7f497cbSRitesh Harjani newts = jbd2_time_diff(ts, transaction->t_start);
153f7f497cbSRitesh Harjani oldts = READ_ONCE(transaction->t_max_wait);
154f7f497cbSRitesh Harjani while (oldts < newts)
155f7f497cbSRitesh Harjani oldts = cmpxchg(&transaction->t_max_wait, oldts, newts);
1566d0bf005STheodore Ts'o }
1576d0bf005STheodore Ts'o }
1586d0bf005STheodore Ts'o
1596d0bf005STheodore Ts'o /*
16096f1e097SJan Kara * Wait until running transaction passes to T_FLUSH state and new transaction
16196f1e097SJan Kara * can thus be started. Also starts the commit if needed. The function expects
16296f1e097SJan Kara * running transaction to exist and releases j_state_lock.
1638f7d89f3SJan Kara */
wait_transaction_locked(journal_t * journal)1648f7d89f3SJan Kara static void wait_transaction_locked(journal_t *journal)
1658f7d89f3SJan Kara __releases(journal->j_state_lock)
1668f7d89f3SJan Kara {
1678f7d89f3SJan Kara DEFINE_WAIT(wait);
1688f7d89f3SJan Kara int need_to_start;
1698f7d89f3SJan Kara tid_t tid = journal->j_running_transaction->t_tid;
1708f7d89f3SJan Kara
17134fc8768SAndrew Perepechko prepare_to_wait_exclusive(&journal->j_wait_transaction_locked, &wait,
1728f7d89f3SJan Kara TASK_UNINTERRUPTIBLE);
1738f7d89f3SJan Kara need_to_start = !tid_geq(journal->j_commit_request, tid);
1748f7d89f3SJan Kara read_unlock(&journal->j_state_lock);
1758f7d89f3SJan Kara if (need_to_start)
1768f7d89f3SJan Kara jbd2_log_start_commit(journal, tid);
177e03a9976SJan Kara jbd2_might_wait_for_commit(journal);
1788f7d89f3SJan Kara schedule();
1798f7d89f3SJan Kara finish_wait(&journal->j_wait_transaction_locked, &wait);
1808f7d89f3SJan Kara }
1818f7d89f3SJan Kara
18296f1e097SJan Kara /*
18396f1e097SJan Kara * Wait until running transaction transitions from T_SWITCH to T_FLUSH
18496f1e097SJan Kara * state and new transaction can thus be started. The function releases
18596f1e097SJan Kara * j_state_lock.
18696f1e097SJan Kara */
wait_transaction_switching(journal_t * journal)18796f1e097SJan Kara static void wait_transaction_switching(journal_t *journal)
18896f1e097SJan Kara __releases(journal->j_state_lock)
18996f1e097SJan Kara {
19096f1e097SJan Kara DEFINE_WAIT(wait);
19196f1e097SJan Kara
19296f1e097SJan Kara if (WARN_ON(!journal->j_running_transaction ||
19305d5233dSTheodore Ts'o journal->j_running_transaction->t_state != T_SWITCH)) {
19405d5233dSTheodore Ts'o read_unlock(&journal->j_state_lock);
19596f1e097SJan Kara return;
19605d5233dSTheodore Ts'o }
19734fc8768SAndrew Perepechko prepare_to_wait_exclusive(&journal->j_wait_transaction_locked, &wait,
19896f1e097SJan Kara TASK_UNINTERRUPTIBLE);
19996f1e097SJan Kara read_unlock(&journal->j_state_lock);
20096f1e097SJan Kara /*
20196f1e097SJan Kara * We don't call jbd2_might_wait_for_commit() here as there's no
20296f1e097SJan Kara * waiting for outstanding handles happening anymore in T_SWITCH state
20396f1e097SJan Kara * and handling of reserved handles actually relies on that for
20496f1e097SJan Kara * correctness.
20596f1e097SJan Kara */
20696f1e097SJan Kara schedule();
20796f1e097SJan Kara finish_wait(&journal->j_wait_transaction_locked, &wait);
20896f1e097SJan Kara }
20996f1e097SJan Kara
sub_reserved_credits(journal_t * journal,int blocks)2108f7d89f3SJan Kara static void sub_reserved_credits(journal_t *journal, int blocks)
2118f7d89f3SJan Kara {
2128f7d89f3SJan Kara atomic_sub(blocks, &journal->j_reserved_credits);
2138f7d89f3SJan Kara wake_up(&journal->j_wait_reserved);
2148f7d89f3SJan Kara }
2158f7d89f3SJan Kara
2168f7d89f3SJan Kara /*
2178f7d89f3SJan Kara * Wait until we can add credits for handle to the running transaction. Called
2188f7d89f3SJan Kara * with j_state_lock held for reading. Returns 0 if handle joined the running
2198f7d89f3SJan Kara * transaction. Returns 1 if we had to wait, j_state_lock is dropped, and
2208f7d89f3SJan Kara * caller must retry.
221b33d9f59STheodore Ts'o *
222b33d9f59STheodore Ts'o * Note: because j_state_lock may be dropped depending on the return
223b33d9f59STheodore Ts'o * value, we need to fake out sparse so ti doesn't complain about a
224b33d9f59STheodore Ts'o * locking imbalance. Callers of add_transaction_credits will need to
225b33d9f59STheodore Ts'o * make a similar accomodation.
2268f7d89f3SJan Kara */
add_transaction_credits(journal_t * journal,int blocks,int rsv_blocks)2278f7d89f3SJan Kara static int add_transaction_credits(journal_t *journal, int blocks,
2288f7d89f3SJan Kara int rsv_blocks)
229b33d9f59STheodore Ts'o __must_hold(&journal->j_state_lock)
2308f7d89f3SJan Kara {
2318f7d89f3SJan Kara transaction_t *t = journal->j_running_transaction;
2328f7d89f3SJan Kara int needed;
2338f7d89f3SJan Kara int total = blocks + rsv_blocks;
2348f7d89f3SJan Kara
2358f7d89f3SJan Kara /*
2368f7d89f3SJan Kara * If the current transaction is locked down for commit, wait
2378f7d89f3SJan Kara * for the lock to be released.
2388f7d89f3SJan Kara */
23996f1e097SJan Kara if (t->t_state != T_RUNNING) {
24096f1e097SJan Kara WARN_ON_ONCE(t->t_state >= T_FLUSH);
2418f7d89f3SJan Kara wait_transaction_locked(journal);
242b33d9f59STheodore Ts'o __acquire(&journal->j_state_lock); /* fake out sparse */
2438f7d89f3SJan Kara return 1;
2448f7d89f3SJan Kara }
2458f7d89f3SJan Kara
2468f7d89f3SJan Kara /*
2478f7d89f3SJan Kara * If there is not enough space left in the log to write all
2488f7d89f3SJan Kara * potential buffers requested by this operation, we need to
2498f7d89f3SJan Kara * stall pending a log checkpoint to free some more log space.
2508f7d89f3SJan Kara */
2518f7d89f3SJan Kara needed = atomic_add_return(total, &t->t_outstanding_credits);
2528f7d89f3SJan Kara if (needed > journal->j_max_transaction_buffers) {
2538f7d89f3SJan Kara /*
2548f7d89f3SJan Kara * If the current transaction is already too large,
2558f7d89f3SJan Kara * then start to commit it: we can then go back and
2568f7d89f3SJan Kara * attach this handle to a new transaction.
2578f7d89f3SJan Kara */
2588f7d89f3SJan Kara atomic_sub(total, &t->t_outstanding_credits);
2596d3ec14dSLukas Czerner
2606d3ec14dSLukas Czerner /*
2616d3ec14dSLukas Czerner * Is the number of reserved credits in the current transaction too
2626d3ec14dSLukas Czerner * big to fit this handle? Wait until reserved credits are freed.
2636d3ec14dSLukas Czerner */
2646d3ec14dSLukas Czerner if (atomic_read(&journal->j_reserved_credits) + total >
2656d3ec14dSLukas Czerner journal->j_max_transaction_buffers) {
2666d3ec14dSLukas Czerner read_unlock(&journal->j_state_lock);
267e03a9976SJan Kara jbd2_might_wait_for_commit(journal);
2686d3ec14dSLukas Czerner wait_event(journal->j_wait_reserved,
2696d3ec14dSLukas Czerner atomic_read(&journal->j_reserved_credits) + total <=
2706d3ec14dSLukas Czerner journal->j_max_transaction_buffers);
271b33d9f59STheodore Ts'o __acquire(&journal->j_state_lock); /* fake out sparse */
2726d3ec14dSLukas Czerner return 1;
2736d3ec14dSLukas Czerner }
2746d3ec14dSLukas Czerner
2758f7d89f3SJan Kara wait_transaction_locked(journal);
276b33d9f59STheodore Ts'o __acquire(&journal->j_state_lock); /* fake out sparse */
2778f7d89f3SJan Kara return 1;
2788f7d89f3SJan Kara }
2798f7d89f3SJan Kara
2808f7d89f3SJan Kara /*
2818f7d89f3SJan Kara * The commit code assumes that it can get enough log space
2828f7d89f3SJan Kara * without forcing a checkpoint. This is *critical* for
2838f7d89f3SJan Kara * correctness: a checkpoint of a buffer which is also
2848f7d89f3SJan Kara * associated with a committing transaction creates a deadlock,
2858f7d89f3SJan Kara * so commit simply cannot force through checkpoints.
2868f7d89f3SJan Kara *
2878f7d89f3SJan Kara * We must therefore ensure the necessary space in the journal
2888f7d89f3SJan Kara * *before* starting to dirty potentially checkpointed buffers
2898f7d89f3SJan Kara * in the new transaction.
2908f7d89f3SJan Kara */
29177444ac4SJan Kara if (jbd2_log_space_left(journal) < journal->j_max_transaction_buffers) {
2928f7d89f3SJan Kara atomic_sub(total, &t->t_outstanding_credits);
2938f7d89f3SJan Kara read_unlock(&journal->j_state_lock);
294e03a9976SJan Kara jbd2_might_wait_for_commit(journal);
2958f7d89f3SJan Kara write_lock(&journal->j_state_lock);
29677444ac4SJan Kara if (jbd2_log_space_left(journal) <
29777444ac4SJan Kara journal->j_max_transaction_buffers)
2988f7d89f3SJan Kara __jbd2_log_wait_for_space(journal);
2998f7d89f3SJan Kara write_unlock(&journal->j_state_lock);
300b33d9f59STheodore Ts'o __acquire(&journal->j_state_lock); /* fake out sparse */
3018f7d89f3SJan Kara return 1;
3028f7d89f3SJan Kara }
3038f7d89f3SJan Kara
3048f7d89f3SJan Kara /* No reservation? We are done... */
3058f7d89f3SJan Kara if (!rsv_blocks)
3068f7d89f3SJan Kara return 0;
3078f7d89f3SJan Kara
3088f7d89f3SJan Kara needed = atomic_add_return(rsv_blocks, &journal->j_reserved_credits);
3098f7d89f3SJan Kara /* We allow at most half of a transaction to be reserved */
3108f7d89f3SJan Kara if (needed > journal->j_max_transaction_buffers / 2) {
3118f7d89f3SJan Kara sub_reserved_credits(journal, rsv_blocks);
3128f7d89f3SJan Kara atomic_sub(total, &t->t_outstanding_credits);
3138f7d89f3SJan Kara read_unlock(&journal->j_state_lock);
314e03a9976SJan Kara jbd2_might_wait_for_commit(journal);
3158f7d89f3SJan Kara wait_event(journal->j_wait_reserved,
3168f7d89f3SJan Kara atomic_read(&journal->j_reserved_credits) + rsv_blocks
3178f7d89f3SJan Kara <= journal->j_max_transaction_buffers / 2);
318b33d9f59STheodore Ts'o __acquire(&journal->j_state_lock); /* fake out sparse */
3198f7d89f3SJan Kara return 1;
3208f7d89f3SJan Kara }
3218f7d89f3SJan Kara return 0;
3228f7d89f3SJan Kara }
3238f7d89f3SJan Kara
3248f7d89f3SJan Kara /*
325470decc6SDave Kleikamp * start_this_handle: Given a handle, deal with any locking or stalling
326470decc6SDave Kleikamp * needed to make sure that there is enough journal space for the handle
327470decc6SDave Kleikamp * to begin. Attach the handle to a transaction and set up the
328470decc6SDave Kleikamp * transaction's buffer credits.
329470decc6SDave Kleikamp */
330470decc6SDave Kleikamp
start_this_handle(journal_t * journal,handle_t * handle,gfp_t gfp_mask)33147def826STheodore Ts'o static int start_this_handle(journal_t *journal, handle_t *handle,
332d2159fb7SDan Carpenter gfp_t gfp_mask)
333470decc6SDave Kleikamp {
334e4471831STheodore Ts'o transaction_t *transaction, *new_transaction = NULL;
335933f1c1eSJan Kara int blocks = handle->h_total_credits;
3368f7d89f3SJan Kara int rsv_blocks = 0;
33728e35e42STao Ma unsigned long ts = jiffies;
338470decc6SDave Kleikamp
3398f7d89f3SJan Kara if (handle->h_rsv_handle)
340933f1c1eSJan Kara rsv_blocks = handle->h_rsv_handle->h_total_credits;
3418f7d89f3SJan Kara
3426d3ec14dSLukas Czerner /*
3436d3ec14dSLukas Czerner * Limit the number of reserved credits to 1/2 of maximum transaction
3446d3ec14dSLukas Czerner * size and limit the number of total credits to not exceed maximum
3456d3ec14dSLukas Czerner * transaction size per operation.
3466d3ec14dSLukas Czerner */
3476d3ec14dSLukas Czerner if ((rsv_blocks > journal->j_max_transaction_buffers / 2) ||
3486d3ec14dSLukas Czerner (rsv_blocks + blocks > journal->j_max_transaction_buffers)) {
3496d3ec14dSLukas Czerner printk(KERN_ERR "JBD2: %s wants too many credits "
3506d3ec14dSLukas Czerner "credits:%d rsv_credits:%d max:%d\n",
3516d3ec14dSLukas Czerner current->comm, blocks, rsv_blocks,
3526d3ec14dSLukas Czerner journal->j_max_transaction_buffers);
3536d3ec14dSLukas Czerner WARN_ON(1);
3546d3ec14dSLukas Czerner return -ENOSPC;
3556d3ec14dSLukas Czerner }
3566d3ec14dSLukas Czerner
357470decc6SDave Kleikamp alloc_transaction:
3583b1833e9SJan Kara /*
3593b1833e9SJan Kara * This check is racy but it is just an optimization of allocating new
3603b1833e9SJan Kara * transaction early if there are high chances we'll need it. If we
3613b1833e9SJan Kara * guess wrong, we'll retry or free unused transaction.
3623b1833e9SJan Kara */
3633b1833e9SJan Kara if (!data_race(journal->j_running_transaction)) {
3646ccaf3e2SMichal Hocko /*
3656ccaf3e2SMichal Hocko * If __GFP_FS is not present, then we may be being called from
3666ccaf3e2SMichal Hocko * inside the fs writeback layer, so we MUST NOT fail.
3676ccaf3e2SMichal Hocko */
3686ccaf3e2SMichal Hocko if ((gfp_mask & __GFP_FS) == 0)
3696ccaf3e2SMichal Hocko gfp_mask |= __GFP_NOFAIL;
370b2f4edb3SWanlong Gao new_transaction = kmem_cache_zalloc(transaction_cache,
371b2f4edb3SWanlong Gao gfp_mask);
3726ccaf3e2SMichal Hocko if (!new_transaction)
37347def826STheodore Ts'o return -ENOMEM;
374470decc6SDave Kleikamp }
375470decc6SDave Kleikamp
376cb3b3bf2SJan Kara jbd2_debug(3, "New handle %p going live.\n", handle);
377470decc6SDave Kleikamp
378470decc6SDave Kleikamp /*
379470decc6SDave Kleikamp * We need to hold j_state_lock until t_updates has been incremented,
380470decc6SDave Kleikamp * for proper journal barrier handling
381470decc6SDave Kleikamp */
382a931da6aSTheodore Ts'o repeat:
383a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock);
3845c2178e7STheodore Ts'o BUG_ON(journal->j_flags & JBD2_UNMOUNT);
385470decc6SDave Kleikamp if (is_journal_aborted(journal) ||
386f7f4bccbSMingming Cao (journal->j_errno != 0 && !(journal->j_flags & JBD2_ACK_ERR))) {
387a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock);
3880c2022ecSYongqiang Yang jbd2_journal_free_transaction(new_transaction);
38947def826STheodore Ts'o return -EROFS;
390470decc6SDave Kleikamp }
391470decc6SDave Kleikamp
3928f7d89f3SJan Kara /*
3938f7d89f3SJan Kara * Wait on the journal's transaction barrier if necessary. Specifically
3948f7d89f3SJan Kara * we allow reserved handles to proceed because otherwise commit could
3958f7d89f3SJan Kara * deadlock on page writeback not being able to complete.
3968f7d89f3SJan Kara */
3978f7d89f3SJan Kara if (!handle->h_reserved && journal->j_barrier_count) {
398a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock);
399470decc6SDave Kleikamp wait_event(journal->j_wait_transaction_locked,
400470decc6SDave Kleikamp journal->j_barrier_count == 0);
401470decc6SDave Kleikamp goto repeat;
402470decc6SDave Kleikamp }
403470decc6SDave Kleikamp
404470decc6SDave Kleikamp if (!journal->j_running_transaction) {
405a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock);
406a931da6aSTheodore Ts'o if (!new_transaction)
407470decc6SDave Kleikamp goto alloc_transaction;
408a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock);
409d7961c7fSJan Kara if (!journal->j_running_transaction &&
4108f7d89f3SJan Kara (handle->h_reserved || !journal->j_barrier_count)) {
411f7f4bccbSMingming Cao jbd2_get_transaction(journal, new_transaction);
412470decc6SDave Kleikamp new_transaction = NULL;
413470decc6SDave Kleikamp }
414a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock);
415a931da6aSTheodore Ts'o goto repeat;
416a931da6aSTheodore Ts'o }
417470decc6SDave Kleikamp
418470decc6SDave Kleikamp transaction = journal->j_running_transaction;
419470decc6SDave Kleikamp
4208f7d89f3SJan Kara if (!handle->h_reserved) {
4218f7d89f3SJan Kara /* We may have dropped j_state_lock - restart in that case */
422b33d9f59STheodore Ts'o if (add_transaction_credits(journal, blocks, rsv_blocks)) {
423b33d9f59STheodore Ts'o /*
424b33d9f59STheodore Ts'o * add_transaction_credits releases
425b33d9f59STheodore Ts'o * j_state_lock on a non-zero return
426b33d9f59STheodore Ts'o */
427b33d9f59STheodore Ts'o __release(&journal->j_state_lock);
428470decc6SDave Kleikamp goto repeat;
429b33d9f59STheodore Ts'o }
4308f7d89f3SJan Kara } else {
431470decc6SDave Kleikamp /*
4328f7d89f3SJan Kara * We have handle reserved so we are allowed to join T_LOCKED
4338f7d89f3SJan Kara * transaction and we don't have to check for transaction size
43496f1e097SJan Kara * and journal space. But we still have to wait while running
43596f1e097SJan Kara * transaction is being switched to a committing one as it
43696f1e097SJan Kara * won't wait for any handles anymore.
437470decc6SDave Kleikamp */
43896f1e097SJan Kara if (transaction->t_state == T_SWITCH) {
43996f1e097SJan Kara wait_transaction_switching(journal);
44096f1e097SJan Kara goto repeat;
44196f1e097SJan Kara }
4428f7d89f3SJan Kara sub_reserved_credits(journal, blocks);
4438f7d89f3SJan Kara handle->h_reserved = 0;
444470decc6SDave Kleikamp }
445470decc6SDave Kleikamp
446470decc6SDave Kleikamp /* OK, account for the buffers that this operation expects to
4478dd42046STheodore Ts'o * use and add the handle to the running transaction.
4488dd42046STheodore Ts'o */
44928e35e42STao Ma update_t_max_wait(transaction, ts);
450470decc6SDave Kleikamp handle->h_transaction = transaction;
4518f7d89f3SJan Kara handle->h_requested_credits = blocks;
452fdc3ef88SJan Kara handle->h_revoke_credits_requested = handle->h_revoke_credits;
453343d9c28STheodore Ts'o handle->h_start_jiffies = jiffies;
454a51dca9cSTheodore Ts'o atomic_inc(&transaction->t_updates);
4558dd42046STheodore Ts'o atomic_inc(&transaction->t_handle_count);
456cb3b3bf2SJan Kara jbd2_debug(4, "Handle %p given %d credits (total %d, free %lu)\n",
4578f7d89f3SJan Kara handle, blocks,
458a51dca9cSTheodore Ts'o atomic_read(&transaction->t_outstanding_credits),
45976c39904SJan Kara jbd2_log_space_left(journal));
460a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock);
46141a5b913STheodore Ts'o current->journal_info = handle;
4629599b0e5SJan Kara
463ab714affSJan Kara rwsem_acquire_read(&journal->j_trans_commit_map, 0, 0, _THIS_IP_);
4640c2022ecSYongqiang Yang jbd2_journal_free_transaction(new_transaction);
46581378da6SMichal Hocko /*
46681378da6SMichal Hocko * Ensure that no allocations done while the transaction is open are
46781378da6SMichal Hocko * going to recurse back to the fs layer.
46881378da6SMichal Hocko */
46981378da6SMichal Hocko handle->saved_alloc_context = memalloc_nofs_save();
47047def826STheodore Ts'o return 0;
471470decc6SDave Kleikamp }
472470decc6SDave Kleikamp
473470decc6SDave Kleikamp /* Allocate a new handle. This should probably be in a slab... */
new_handle(int nblocks)474470decc6SDave Kleikamp static handle_t *new_handle(int nblocks)
475470decc6SDave Kleikamp {
476af1e76d6SMingming Cao handle_t *handle = jbd2_alloc_handle(GFP_NOFS);
477470decc6SDave Kleikamp if (!handle)
478470decc6SDave Kleikamp return NULL;
479933f1c1eSJan Kara handle->h_total_credits = nblocks;
480470decc6SDave Kleikamp handle->h_ref = 1;
481470decc6SDave Kleikamp
482470decc6SDave Kleikamp return handle;
483470decc6SDave Kleikamp }
484470decc6SDave Kleikamp
jbd2__journal_start(journal_t * journal,int nblocks,int rsv_blocks,int revoke_records,gfp_t gfp_mask,unsigned int type,unsigned int line_no)4858f7d89f3SJan Kara handle_t *jbd2__journal_start(journal_t *journal, int nblocks, int rsv_blocks,
486fdc3ef88SJan Kara int revoke_records, gfp_t gfp_mask,
487fdc3ef88SJan Kara unsigned int type, unsigned int line_no)
488470decc6SDave Kleikamp {
489470decc6SDave Kleikamp handle_t *handle = journal_current_handle();
490470decc6SDave Kleikamp int err;
491470decc6SDave Kleikamp
492470decc6SDave Kleikamp if (!journal)
493470decc6SDave Kleikamp return ERR_PTR(-EROFS);
494470decc6SDave Kleikamp
495470decc6SDave Kleikamp if (handle) {
496470decc6SDave Kleikamp J_ASSERT(handle->h_transaction->t_journal == journal);
497470decc6SDave Kleikamp handle->h_ref++;
498470decc6SDave Kleikamp return handle;
499470decc6SDave Kleikamp }
500470decc6SDave Kleikamp
501fdc3ef88SJan Kara nblocks += DIV_ROUND_UP(revoke_records,
502fdc3ef88SJan Kara journal->j_revoke_records_per_block);
503470decc6SDave Kleikamp handle = new_handle(nblocks);
504470decc6SDave Kleikamp if (!handle)
505470decc6SDave Kleikamp return ERR_PTR(-ENOMEM);
5068f7d89f3SJan Kara if (rsv_blocks) {
5078f7d89f3SJan Kara handle_t *rsv_handle;
5088f7d89f3SJan Kara
5098f7d89f3SJan Kara rsv_handle = new_handle(rsv_blocks);
5108f7d89f3SJan Kara if (!rsv_handle) {
5118f7d89f3SJan Kara jbd2_free_handle(handle);
5128f7d89f3SJan Kara return ERR_PTR(-ENOMEM);
5138f7d89f3SJan Kara }
5148f7d89f3SJan Kara rsv_handle->h_reserved = 1;
5158f7d89f3SJan Kara rsv_handle->h_journal = journal;
5168f7d89f3SJan Kara handle->h_rsv_handle = rsv_handle;
5178f7d89f3SJan Kara }
518fdc3ef88SJan Kara handle->h_revoke_credits = revoke_records;
519470decc6SDave Kleikamp
52047def826STheodore Ts'o err = start_this_handle(journal, handle, gfp_mask);
521470decc6SDave Kleikamp if (err < 0) {
5228f7d89f3SJan Kara if (handle->h_rsv_handle)
5238f7d89f3SJan Kara jbd2_free_handle(handle->h_rsv_handle);
524af1e76d6SMingming Cao jbd2_free_handle(handle);
525df05c1b8SDmitry Monakhov return ERR_PTR(err);
526470decc6SDave Kleikamp }
527343d9c28STheodore Ts'o handle->h_type = type;
528343d9c28STheodore Ts'o handle->h_line_no = line_no;
529343d9c28STheodore Ts'o trace_jbd2_handle_start(journal->j_fs_dev->bd_dev,
530343d9c28STheodore Ts'o handle->h_transaction->t_tid, type,
531343d9c28STheodore Ts'o line_no, nblocks);
53281378da6SMichal Hocko
533470decc6SDave Kleikamp return handle;
534470decc6SDave Kleikamp }
53547def826STheodore Ts'o EXPORT_SYMBOL(jbd2__journal_start);
53647def826STheodore Ts'o
53747def826STheodore Ts'o
53891e4775dSMauro Carvalho Chehab /**
5392bf31d94SMauro Carvalho Chehab * jbd2_journal_start() - Obtain a new handle.
54091e4775dSMauro Carvalho Chehab * @journal: Journal to start transaction on.
54191e4775dSMauro Carvalho Chehab * @nblocks: number of block buffer we might modify
54291e4775dSMauro Carvalho Chehab *
54391e4775dSMauro Carvalho Chehab * We make sure that the transaction can guarantee at least nblocks of
54491e4775dSMauro Carvalho Chehab * modified buffers in the log. We block until the log can guarantee
54591e4775dSMauro Carvalho Chehab * that much space. Additionally, if rsv_blocks > 0, we also create another
54691e4775dSMauro Carvalho Chehab * handle with rsv_blocks reserved blocks in the journal. This handle is
5470c1cba6cSwangyan * stored in h_rsv_handle. It is not attached to any particular transaction
54891e4775dSMauro Carvalho Chehab * and thus doesn't block transaction commit. If the caller uses this reserved
54991e4775dSMauro Carvalho Chehab * handle, it has to set h_rsv_handle to NULL as otherwise jbd2_journal_stop()
55091e4775dSMauro Carvalho Chehab * on the parent handle will dispose the reserved one. Reserved handle has to
55191e4775dSMauro Carvalho Chehab * be converted to a normal handle using jbd2_journal_start_reserved() before
55291e4775dSMauro Carvalho Chehab * it can be used.
55391e4775dSMauro Carvalho Chehab *
55491e4775dSMauro Carvalho Chehab * Return a pointer to a newly allocated handle, or an ERR_PTR() value
55591e4775dSMauro Carvalho Chehab * on failure.
55691e4775dSMauro Carvalho Chehab */
jbd2_journal_start(journal_t * journal,int nblocks)55747def826STheodore Ts'o handle_t *jbd2_journal_start(journal_t *journal, int nblocks)
55847def826STheodore Ts'o {
559fdc3ef88SJan Kara return jbd2__journal_start(journal, nblocks, 0, 0, GFP_NOFS, 0, 0);
56047def826STheodore Ts'o }
56147def826STheodore Ts'o EXPORT_SYMBOL(jbd2_journal_start);
56247def826STheodore Ts'o
__jbd2_journal_unreserve_handle(handle_t * handle,transaction_t * t)56314ff6286SJan Kara static void __jbd2_journal_unreserve_handle(handle_t *handle, transaction_t *t)
5648f7d89f3SJan Kara {
5658f7d89f3SJan Kara journal_t *journal = handle->h_journal;
5668f7d89f3SJan Kara
5678f7d89f3SJan Kara WARN_ON(!handle->h_reserved);
568933f1c1eSJan Kara sub_reserved_credits(journal, handle->h_total_credits);
56914ff6286SJan Kara if (t)
57014ff6286SJan Kara atomic_sub(handle->h_total_credits, &t->t_outstanding_credits);
571ec8b6f60SJan Kara }
572ec8b6f60SJan Kara
jbd2_journal_free_reserved(handle_t * handle)573ec8b6f60SJan Kara void jbd2_journal_free_reserved(handle_t *handle)
574ec8b6f60SJan Kara {
57514ff6286SJan Kara journal_t *journal = handle->h_journal;
57614ff6286SJan Kara
57714ff6286SJan Kara /* Get j_state_lock to pin running transaction if it exists */
57814ff6286SJan Kara read_lock(&journal->j_state_lock);
57914ff6286SJan Kara __jbd2_journal_unreserve_handle(handle, journal->j_running_transaction);
58014ff6286SJan Kara read_unlock(&journal->j_state_lock);
5818f7d89f3SJan Kara jbd2_free_handle(handle);
5828f7d89f3SJan Kara }
5838f7d89f3SJan Kara EXPORT_SYMBOL(jbd2_journal_free_reserved);
5848f7d89f3SJan Kara
5858f7d89f3SJan Kara /**
5862bf31d94SMauro Carvalho Chehab * jbd2_journal_start_reserved() - start reserved handle
5878f7d89f3SJan Kara * @handle: handle to start
588f69120ceSTobin C. Harding * @type: for handle statistics
589f69120ceSTobin C. Harding * @line_no: for handle statistics
5908f7d89f3SJan Kara *
5918f7d89f3SJan Kara * Start handle that has been previously reserved with jbd2_journal_reserve().
5928f7d89f3SJan Kara * This attaches @handle to the running transaction (or creates one if there's
5938f7d89f3SJan Kara * not transaction running). Unlike jbd2_journal_start() this function cannot
5948f7d89f3SJan Kara * block on journal commit, checkpointing, or similar stuff. It can block on
5958f7d89f3SJan Kara * memory allocation or frozen journal though.
5968f7d89f3SJan Kara *
5978f7d89f3SJan Kara * Return 0 on success, non-zero on error - handle is freed in that case.
5988f7d89f3SJan Kara */
jbd2_journal_start_reserved(handle_t * handle,unsigned int type,unsigned int line_no)5998f7d89f3SJan Kara int jbd2_journal_start_reserved(handle_t *handle, unsigned int type,
6008f7d89f3SJan Kara unsigned int line_no)
6018f7d89f3SJan Kara {
6028f7d89f3SJan Kara journal_t *journal = handle->h_journal;
6038f7d89f3SJan Kara int ret = -EIO;
6048f7d89f3SJan Kara
6058f7d89f3SJan Kara if (WARN_ON(!handle->h_reserved)) {
6068f7d89f3SJan Kara /* Someone passed in normal handle? Just stop it. */
6078f7d89f3SJan Kara jbd2_journal_stop(handle);
6088f7d89f3SJan Kara return ret;
6098f7d89f3SJan Kara }
6108f7d89f3SJan Kara /*
6118f7d89f3SJan Kara * Usefulness of mixing of reserved and unreserved handles is
6128f7d89f3SJan Kara * questionable. So far nobody seems to need it so just error out.
6138f7d89f3SJan Kara */
6148f7d89f3SJan Kara if (WARN_ON(current->journal_info)) {
6158f7d89f3SJan Kara jbd2_journal_free_reserved(handle);
6168f7d89f3SJan Kara return ret;
6178f7d89f3SJan Kara }
6188f7d89f3SJan Kara
6198f7d89f3SJan Kara handle->h_journal = NULL;
6208f7d89f3SJan Kara /*
6218f7d89f3SJan Kara * GFP_NOFS is here because callers are likely from writeback or
6228f7d89f3SJan Kara * similarly constrained call sites
6238f7d89f3SJan Kara */
6248f7d89f3SJan Kara ret = start_this_handle(journal, handle, GFP_NOFS);
62592e3b405SDan Carpenter if (ret < 0) {
626b2569260STheodore Ts'o handle->h_journal = journal;
6278f7d89f3SJan Kara jbd2_journal_free_reserved(handle);
62892e3b405SDan Carpenter return ret;
62992e3b405SDan Carpenter }
6308f7d89f3SJan Kara handle->h_type = type;
6318f7d89f3SJan Kara handle->h_line_no = line_no;
6324c273352SXiaoguang Wang trace_jbd2_handle_start(journal->j_fs_dev->bd_dev,
6334c273352SXiaoguang Wang handle->h_transaction->t_tid, type,
634933f1c1eSJan Kara line_no, handle->h_total_credits);
63592e3b405SDan Carpenter return 0;
6368f7d89f3SJan Kara }
6378f7d89f3SJan Kara EXPORT_SYMBOL(jbd2_journal_start_reserved);
638470decc6SDave Kleikamp
639470decc6SDave Kleikamp /**
6402bf31d94SMauro Carvalho Chehab * jbd2_journal_extend() - extend buffer credits.
641470decc6SDave Kleikamp * @handle: handle to 'extend'
642470decc6SDave Kleikamp * @nblocks: nr blocks to try to extend by.
643fdc3ef88SJan Kara * @revoke_records: number of revoke records to try to extend by.
644470decc6SDave Kleikamp *
645470decc6SDave Kleikamp * Some transactions, such as large extends and truncates, can be done
646470decc6SDave Kleikamp * atomically all at once or in several stages. The operation requests
647bd7ced98SMasanari Iida * a credit for a number of buffer modifications in advance, but can
648470decc6SDave Kleikamp * extend its credit if it needs more.
649470decc6SDave Kleikamp *
650f7f4bccbSMingming Cao * jbd2_journal_extend tries to give the running handle more buffer credits.
651470decc6SDave Kleikamp * It does not guarantee that allocation - this is a best-effort only.
652470decc6SDave Kleikamp * The calling process MUST be able to deal cleanly with a failure to
653470decc6SDave Kleikamp * extend here.
654470decc6SDave Kleikamp *
655470decc6SDave Kleikamp * Return 0 on success, non-zero on failure.
656470decc6SDave Kleikamp *
657470decc6SDave Kleikamp * return code < 0 implies an error
658470decc6SDave Kleikamp * return code > 0 implies normal transaction-full status.
659470decc6SDave Kleikamp */
jbd2_journal_extend(handle_t * handle,int nblocks,int revoke_records)660fdc3ef88SJan Kara int jbd2_journal_extend(handle_t *handle, int nblocks, int revoke_records)
661470decc6SDave Kleikamp {
662470decc6SDave Kleikamp transaction_t *transaction = handle->h_transaction;
66341a5b913STheodore Ts'o journal_t *journal;
664470decc6SDave Kleikamp int result;
665470decc6SDave Kleikamp int wanted;
666470decc6SDave Kleikamp
667470decc6SDave Kleikamp if (is_handle_aborted(handle))
66841a5b913STheodore Ts'o return -EROFS;
66941a5b913STheodore Ts'o journal = transaction->t_journal;
670470decc6SDave Kleikamp
671470decc6SDave Kleikamp result = 1;
672470decc6SDave Kleikamp
673a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock);
674470decc6SDave Kleikamp
675470decc6SDave Kleikamp /* Don't extend a locked-down transaction! */
67641a5b913STheodore Ts'o if (transaction->t_state != T_RUNNING) {
677cb3b3bf2SJan Kara jbd2_debug(3, "denied handle %p %d blocks: "
678470decc6SDave Kleikamp "transaction not running\n", handle, nblocks);
679470decc6SDave Kleikamp goto error_out;
680470decc6SDave Kleikamp }
681470decc6SDave Kleikamp
682fdc3ef88SJan Kara nblocks += DIV_ROUND_UP(
683fdc3ef88SJan Kara handle->h_revoke_credits_requested + revoke_records,
684fdc3ef88SJan Kara journal->j_revoke_records_per_block) -
685fdc3ef88SJan Kara DIV_ROUND_UP(
686fdc3ef88SJan Kara handle->h_revoke_credits_requested,
687fdc3ef88SJan Kara journal->j_revoke_records_per_block);
688fe1e8db5SJan Kara wanted = atomic_add_return(nblocks,
689fe1e8db5SJan Kara &transaction->t_outstanding_credits);
690470decc6SDave Kleikamp
691470decc6SDave Kleikamp if (wanted > journal->j_max_transaction_buffers) {
692cb3b3bf2SJan Kara jbd2_debug(3, "denied handle %p %d blocks: "
693470decc6SDave Kleikamp "transaction too large\n", handle, nblocks);
694fe1e8db5SJan Kara atomic_sub(nblocks, &transaction->t_outstanding_credits);
695f7f497cbSRitesh Harjani goto error_out;
696470decc6SDave Kleikamp }
697470decc6SDave Kleikamp
698343d9c28STheodore Ts'o trace_jbd2_handle_extend(journal->j_fs_dev->bd_dev,
69941a5b913STheodore Ts'o transaction->t_tid,
700343d9c28STheodore Ts'o handle->h_type, handle->h_line_no,
701933f1c1eSJan Kara handle->h_total_credits,
702343d9c28STheodore Ts'o nblocks);
703343d9c28STheodore Ts'o
704933f1c1eSJan Kara handle->h_total_credits += nblocks;
705343d9c28STheodore Ts'o handle->h_requested_credits += nblocks;
706fdc3ef88SJan Kara handle->h_revoke_credits += revoke_records;
707fdc3ef88SJan Kara handle->h_revoke_credits_requested += revoke_records;
708470decc6SDave Kleikamp result = 0;
709470decc6SDave Kleikamp
710cb3b3bf2SJan Kara jbd2_debug(3, "extended handle %p by %d\n", handle, nblocks);
711470decc6SDave Kleikamp error_out:
712a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock);
713470decc6SDave Kleikamp return result;
714470decc6SDave Kleikamp }
715470decc6SDave Kleikamp
stop_this_handle(handle_t * handle)716ec8b6f60SJan Kara static void stop_this_handle(handle_t *handle)
717ec8b6f60SJan Kara {
718ec8b6f60SJan Kara transaction_t *transaction = handle->h_transaction;
719ec8b6f60SJan Kara journal_t *journal = transaction->t_journal;
720fdc3ef88SJan Kara int revokes;
721ec8b6f60SJan Kara
722ec8b6f60SJan Kara J_ASSERT(journal_current_handle() == handle);
723ec8b6f60SJan Kara J_ASSERT(atomic_read(&transaction->t_updates) > 0);
724ec8b6f60SJan Kara current->journal_info = NULL;
725fdc3ef88SJan Kara /*
726fdc3ef88SJan Kara * Subtract necessary revoke descriptor blocks from handle credits. We
727fdc3ef88SJan Kara * take care to account only for revoke descriptor blocks the
728fdc3ef88SJan Kara * transaction will really need as large sequences of transactions with
729fdc3ef88SJan Kara * small numbers of revokes are relatively common.
730fdc3ef88SJan Kara */
731fdc3ef88SJan Kara revokes = handle->h_revoke_credits_requested - handle->h_revoke_credits;
732fdc3ef88SJan Kara if (revokes) {
733fdc3ef88SJan Kara int t_revokes, revoke_descriptors;
734fdc3ef88SJan Kara int rr_per_blk = journal->j_revoke_records_per_block;
735fdc3ef88SJan Kara
736fdc3ef88SJan Kara WARN_ON_ONCE(DIV_ROUND_UP(revokes, rr_per_blk)
737933f1c1eSJan Kara > handle->h_total_credits);
738fdc3ef88SJan Kara t_revokes = atomic_add_return(revokes,
739fdc3ef88SJan Kara &transaction->t_outstanding_revokes);
740fdc3ef88SJan Kara revoke_descriptors =
741fdc3ef88SJan Kara DIV_ROUND_UP(t_revokes, rr_per_blk) -
742fdc3ef88SJan Kara DIV_ROUND_UP(t_revokes - revokes, rr_per_blk);
743933f1c1eSJan Kara handle->h_total_credits -= revoke_descriptors;
744fdc3ef88SJan Kara }
745933f1c1eSJan Kara atomic_sub(handle->h_total_credits,
746ec8b6f60SJan Kara &transaction->t_outstanding_credits);
747ec8b6f60SJan Kara if (handle->h_rsv_handle)
74814ff6286SJan Kara __jbd2_journal_unreserve_handle(handle->h_rsv_handle,
74914ff6286SJan Kara transaction);
750ec8b6f60SJan Kara if (atomic_dec_and_test(&transaction->t_updates))
751ec8b6f60SJan Kara wake_up(&journal->j_wait_updates);
752ec8b6f60SJan Kara
75350b8b3f8SLinus Torvalds rwsem_release(&journal->j_trans_commit_map, _THIS_IP_);
754ec8b6f60SJan Kara /*
755ec8b6f60SJan Kara * Scope of the GFP_NOFS context is over here and so we can restore the
756ec8b6f60SJan Kara * original alloc context.
757ec8b6f60SJan Kara */
758ec8b6f60SJan Kara memalloc_nofs_restore(handle->saved_alloc_context);
759ec8b6f60SJan Kara }
760470decc6SDave Kleikamp
761470decc6SDave Kleikamp /**
7622bf31d94SMauro Carvalho Chehab * jbd2__journal_restart() - restart a handle .
763470decc6SDave Kleikamp * @handle: handle to restart
764470decc6SDave Kleikamp * @nblocks: nr credits requested
765fdc3ef88SJan Kara * @revoke_records: number of revoke record credits requested
766f69120ceSTobin C. Harding * @gfp_mask: memory allocation flags (for start_this_handle)
767470decc6SDave Kleikamp *
768470decc6SDave Kleikamp * Restart a handle for a multi-transaction filesystem
769470decc6SDave Kleikamp * operation.
770470decc6SDave Kleikamp *
771f7f4bccbSMingming Cao * If the jbd2_journal_extend() call above fails to grant new buffer credits
772f7f4bccbSMingming Cao * to a running handle, a call to jbd2_journal_restart will commit the
773470decc6SDave Kleikamp * handle's transaction so far and reattach the handle to a new
774bd7ced98SMasanari Iida * transaction capable of guaranteeing the requested number of
7758f7d89f3SJan Kara * credits. We preserve reserved handle if there's any attached to the
7768f7d89f3SJan Kara * passed in handle.
777470decc6SDave Kleikamp */
jbd2__journal_restart(handle_t * handle,int nblocks,int revoke_records,gfp_t gfp_mask)778fdc3ef88SJan Kara int jbd2__journal_restart(handle_t *handle, int nblocks, int revoke_records,
779fdc3ef88SJan Kara gfp_t gfp_mask)
780470decc6SDave Kleikamp {
781470decc6SDave Kleikamp transaction_t *transaction = handle->h_transaction;
78241a5b913STheodore Ts'o journal_t *journal;
783e4471831STheodore Ts'o tid_t tid;
784ec8b6f60SJan Kara int need_to_start;
7850094f981SJan Kara int ret;
786470decc6SDave Kleikamp
787470decc6SDave Kleikamp /* If we've had an abort of any type, don't even think about
788470decc6SDave Kleikamp * actually doing the restart! */
789470decc6SDave Kleikamp if (is_handle_aborted(handle))
790470decc6SDave Kleikamp return 0;
79141a5b913STheodore Ts'o journal = transaction->t_journal;
792ec8b6f60SJan Kara tid = transaction->t_tid;
793470decc6SDave Kleikamp
794470decc6SDave Kleikamp /*
795470decc6SDave Kleikamp * First unlink the handle from its current transaction, and start the
796470decc6SDave Kleikamp * commit on that.
797470decc6SDave Kleikamp */
798cb3b3bf2SJan Kara jbd2_debug(2, "restarting handle %p\n", handle);
799ec8b6f60SJan Kara stop_this_handle(handle);
800ec8b6f60SJan Kara handle->h_transaction = NULL;
801ec8b6f60SJan Kara
802ec8b6f60SJan Kara /*
803ec8b6f60SJan Kara * TODO: If we use READ_ONCE / WRITE_ONCE for j_commit_request we can
804ec8b6f60SJan Kara * get rid of pointless j_state_lock traffic like this.
805ec8b6f60SJan Kara */
806ec8b6f60SJan Kara read_lock(&journal->j_state_lock);
807e4471831STheodore Ts'o need_to_start = !tid_geq(journal->j_commit_request, tid);
808a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock);
809e4471831STheodore Ts'o if (need_to_start)
810e4471831STheodore Ts'o jbd2_log_start_commit(journal, tid);
811933f1c1eSJan Kara handle->h_total_credits = nblocks +
812fdc3ef88SJan Kara DIV_ROUND_UP(revoke_records,
813fdc3ef88SJan Kara journal->j_revoke_records_per_block);
814fdc3ef88SJan Kara handle->h_revoke_credits = revoke_records;
81547def826STheodore Ts'o ret = start_this_handle(journal, handle, gfp_mask);
8160094f981SJan Kara trace_jbd2_handle_restart(journal->j_fs_dev->bd_dev,
8170094f981SJan Kara ret ? 0 : handle->h_transaction->t_tid,
8180094f981SJan Kara handle->h_type, handle->h_line_no,
8190094f981SJan Kara handle->h_total_credits);
820470decc6SDave Kleikamp return ret;
821470decc6SDave Kleikamp }
82247def826STheodore Ts'o EXPORT_SYMBOL(jbd2__journal_restart);
823470decc6SDave Kleikamp
824470decc6SDave Kleikamp
jbd2_journal_restart(handle_t * handle,int nblocks)82547def826STheodore Ts'o int jbd2_journal_restart(handle_t *handle, int nblocks)
82647def826STheodore Ts'o {
827fdc3ef88SJan Kara return jbd2__journal_restart(handle, nblocks, 0, GFP_NOFS);
82847def826STheodore Ts'o }
82947def826STheodore Ts'o EXPORT_SYMBOL(jbd2_journal_restart);
83047def826STheodore Ts'o
8314f981868SRitesh Harjani /*
8324f981868SRitesh Harjani * Waits for any outstanding t_updates to finish.
8334f981868SRitesh Harjani * This is called with write j_state_lock held.
8344f981868SRitesh Harjani */
jbd2_journal_wait_updates(journal_t * journal)8354f981868SRitesh Harjani void jbd2_journal_wait_updates(journal_t *journal)
8364f981868SRitesh Harjani {
8374f981868SRitesh Harjani DEFINE_WAIT(wait);
8384f981868SRitesh Harjani
839cc16eecaSRitesh Harjani while (1) {
840cc16eecaSRitesh Harjani /*
841cc16eecaSRitesh Harjani * Note that the running transaction can get freed under us if
842cc16eecaSRitesh Harjani * this transaction is getting committed in
843cc16eecaSRitesh Harjani * jbd2_journal_commit_transaction() ->
844cc16eecaSRitesh Harjani * jbd2_journal_free_transaction(). This can only happen when we
845cc16eecaSRitesh Harjani * release j_state_lock -> schedule() -> acquire j_state_lock.
846cc16eecaSRitesh Harjani * Hence we should everytime retrieve new j_running_transaction
847cc16eecaSRitesh Harjani * value (after j_state_lock release acquire cycle), else it may
848cc16eecaSRitesh Harjani * lead to use-after-free of old freed transaction.
849cc16eecaSRitesh Harjani */
850cc16eecaSRitesh Harjani transaction_t *transaction = journal->j_running_transaction;
851cc16eecaSRitesh Harjani
852cc16eecaSRitesh Harjani if (!transaction)
853cc16eecaSRitesh Harjani break;
854cc16eecaSRitesh Harjani
8554f981868SRitesh Harjani prepare_to_wait(&journal->j_wait_updates, &wait,
8564f981868SRitesh Harjani TASK_UNINTERRUPTIBLE);
857cc16eecaSRitesh Harjani if (!atomic_read(&transaction->t_updates)) {
858cc16eecaSRitesh Harjani finish_wait(&journal->j_wait_updates, &wait);
859cc16eecaSRitesh Harjani break;
860cc16eecaSRitesh Harjani }
8614f981868SRitesh Harjani write_unlock(&journal->j_state_lock);
8624f981868SRitesh Harjani schedule();
8634f981868SRitesh Harjani finish_wait(&journal->j_wait_updates, &wait);
864cc16eecaSRitesh Harjani write_lock(&journal->j_state_lock);
8654f981868SRitesh Harjani }
8664f981868SRitesh Harjani }
8674f981868SRitesh Harjani
868470decc6SDave Kleikamp /**
8692bf31d94SMauro Carvalho Chehab * jbd2_journal_lock_updates () - establish a transaction barrier.
870470decc6SDave Kleikamp * @journal: Journal to establish a barrier on.
871470decc6SDave Kleikamp *
872470decc6SDave Kleikamp * This locks out any further updates from being started, and blocks
873470decc6SDave Kleikamp * until all existing updates have completed, returning only once the
874470decc6SDave Kleikamp * journal is in a quiescent state with no updates running.
875470decc6SDave Kleikamp *
876470decc6SDave Kleikamp * The journal lock should not be held on entry.
877470decc6SDave Kleikamp */
jbd2_journal_lock_updates(journal_t * journal)878f7f4bccbSMingming Cao void jbd2_journal_lock_updates(journal_t *journal)
879470decc6SDave Kleikamp {
8801eaa566dSJan Kara jbd2_might_wait_for_commit(journal);
8811eaa566dSJan Kara
882a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock);
883470decc6SDave Kleikamp ++journal->j_barrier_count;
884470decc6SDave Kleikamp
8858f7d89f3SJan Kara /* Wait until there are no reserved handles */
8868f7d89f3SJan Kara if (atomic_read(&journal->j_reserved_credits)) {
8878f7d89f3SJan Kara write_unlock(&journal->j_state_lock);
8888f7d89f3SJan Kara wait_event(journal->j_wait_reserved,
8898f7d89f3SJan Kara atomic_read(&journal->j_reserved_credits) == 0);
8908f7d89f3SJan Kara write_lock(&journal->j_state_lock);
8918f7d89f3SJan Kara }
8928f7d89f3SJan Kara
8934f981868SRitesh Harjani /* Wait until there are no running t_updates */
8944f981868SRitesh Harjani jbd2_journal_wait_updates(journal);
895470decc6SDave Kleikamp
896a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock);
897470decc6SDave Kleikamp
898470decc6SDave Kleikamp /*
899470decc6SDave Kleikamp * We have now established a barrier against other normal updates, but
900f7f4bccbSMingming Cao * we also need to barrier against other jbd2_journal_lock_updates() calls
901470decc6SDave Kleikamp * to make sure that we serialise special journal-locked operations
902470decc6SDave Kleikamp * too.
903470decc6SDave Kleikamp */
904470decc6SDave Kleikamp mutex_lock(&journal->j_barrier);
905470decc6SDave Kleikamp }
906470decc6SDave Kleikamp
907470decc6SDave Kleikamp /**
9082bf31d94SMauro Carvalho Chehab * jbd2_journal_unlock_updates () - release barrier
909470decc6SDave Kleikamp * @journal: Journal to release the barrier on.
910470decc6SDave Kleikamp *
911f7f4bccbSMingming Cao * Release a transaction barrier obtained with jbd2_journal_lock_updates().
912470decc6SDave Kleikamp *
913470decc6SDave Kleikamp * Should be called without the journal lock held.
914470decc6SDave Kleikamp */
jbd2_journal_unlock_updates(journal_t * journal)915f7f4bccbSMingming Cao void jbd2_journal_unlock_updates (journal_t *journal)
916470decc6SDave Kleikamp {
917470decc6SDave Kleikamp J_ASSERT(journal->j_barrier_count != 0);
918470decc6SDave Kleikamp
919470decc6SDave Kleikamp mutex_unlock(&journal->j_barrier);
920a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock);
921470decc6SDave Kleikamp --journal->j_barrier_count;
922a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock);
92334fc8768SAndrew Perepechko wake_up_all(&journal->j_wait_transaction_locked);
924470decc6SDave Kleikamp }
925470decc6SDave Kleikamp
warn_dirty_buffer(struct buffer_head * bh)926f91d1d04SJan Kara static void warn_dirty_buffer(struct buffer_head *bh)
927470decc6SDave Kleikamp {
928f91d1d04SJan Kara printk(KERN_WARNING
929a1c6f057SDmitry Monakhov "JBD2: Spotted dirty metadata buffer (dev = %pg, blocknr = %llu). "
930f91d1d04SJan Kara "There's a risk of filesystem corruption in case of system "
931f91d1d04SJan Kara "crash.\n",
932a1c6f057SDmitry Monakhov bh->b_bdev, (unsigned long long)bh->b_blocknr);
933470decc6SDave Kleikamp }
934470decc6SDave Kleikamp
935ee57aba1SJan Kara /* Call t_frozen trigger and copy buffer data into jh->b_frozen_data. */
jbd2_freeze_jh_data(struct journal_head * jh)936ee57aba1SJan Kara static void jbd2_freeze_jh_data(struct journal_head *jh)
937ee57aba1SJan Kara {
938ee57aba1SJan Kara char *source;
939ee57aba1SJan Kara struct buffer_head *bh = jh2bh(jh);
940ee57aba1SJan Kara
941ee57aba1SJan Kara J_EXPECT_JH(jh, buffer_uptodate(bh), "Possible IO failure.\n");
942*147d4a09SRitesh Harjani (IBM) source = kmap_local_folio(bh->b_folio, bh_offset(bh));
943ee57aba1SJan Kara /* Fire data frozen trigger just before we copy the data */
944*147d4a09SRitesh Harjani (IBM) jbd2_buffer_frozen_trigger(jh, source, jh->b_triggers);
945*147d4a09SRitesh Harjani (IBM) memcpy(jh->b_frozen_data, source, bh->b_size);
946*147d4a09SRitesh Harjani (IBM) kunmap_local(source);
947ee57aba1SJan Kara
948ee57aba1SJan Kara /*
949ee57aba1SJan Kara * Now that the frozen data is saved off, we need to store any matching
950ee57aba1SJan Kara * triggers.
951ee57aba1SJan Kara */
952ee57aba1SJan Kara jh->b_frozen_triggers = jh->b_triggers;
953ee57aba1SJan Kara }
954ee57aba1SJan Kara
955470decc6SDave Kleikamp /*
956470decc6SDave Kleikamp * If the buffer is already part of the current transaction, then there
957470decc6SDave Kleikamp * is nothing we need to do. If it is already part of a prior
958470decc6SDave Kleikamp * transaction which we are still committing to disk, then we need to
959470decc6SDave Kleikamp * make sure that we do not overwrite the old copy: we do copy-out to
960470decc6SDave Kleikamp * preserve the copy going to disk. We also account the buffer against
961470decc6SDave Kleikamp * the handle's metadata buffer credits (unless the buffer is already
962470decc6SDave Kleikamp * part of the transaction, that is).
963470decc6SDave Kleikamp *
964470decc6SDave Kleikamp */
965470decc6SDave Kleikamp static int
do_get_write_access(handle_t * handle,struct journal_head * jh,int force_copy)966470decc6SDave Kleikamp do_get_write_access(handle_t *handle, struct journal_head *jh,
967470decc6SDave Kleikamp int force_copy)
968470decc6SDave Kleikamp {
969470decc6SDave Kleikamp struct buffer_head *bh;
97041a5b913STheodore Ts'o transaction_t *transaction = handle->h_transaction;
971470decc6SDave Kleikamp journal_t *journal;
972470decc6SDave Kleikamp int error;
973470decc6SDave Kleikamp char *frozen_buffer = NULL;
974f783f091STheodore Ts'o unsigned long start_lock, time_lock;
975470decc6SDave Kleikamp
976470decc6SDave Kleikamp journal = transaction->t_journal;
977470decc6SDave Kleikamp
978cb3b3bf2SJan Kara jbd2_debug(5, "journal_head %p, force_copy %d\n", jh, force_copy);
979470decc6SDave Kleikamp
980470decc6SDave Kleikamp JBUFFER_TRACE(jh, "entry");
981470decc6SDave Kleikamp repeat:
982470decc6SDave Kleikamp bh = jh2bh(jh);
983470decc6SDave Kleikamp
984470decc6SDave Kleikamp /* @@@ Need to check for errors here at some point. */
985470decc6SDave Kleikamp
986f783f091STheodore Ts'o start_lock = jiffies;
987470decc6SDave Kleikamp lock_buffer(bh);
98846417064SThomas Gleixner spin_lock(&jh->b_state_lock);
989470decc6SDave Kleikamp
990f783f091STheodore Ts'o /* If it takes too long to lock the buffer, trace it */
991f783f091STheodore Ts'o time_lock = jbd2_time_diff(start_lock, jiffies);
992f783f091STheodore Ts'o if (time_lock > HZ/10)
993f783f091STheodore Ts'o trace_jbd2_lock_buffer_stall(bh->b_bdev->bd_dev,
994f783f091STheodore Ts'o jiffies_to_msecs(time_lock));
995f783f091STheodore Ts'o
996470decc6SDave Kleikamp /* We now hold the buffer lock so it is safe to query the buffer
997470decc6SDave Kleikamp * state. Is the buffer dirty?
998470decc6SDave Kleikamp *
999470decc6SDave Kleikamp * If so, there are two possibilities. The buffer may be
1000470decc6SDave Kleikamp * non-journaled, and undergoing a quite legitimate writeback.
1001470decc6SDave Kleikamp * Otherwise, it is journaled, and we don't expect dirty buffers
1002470decc6SDave Kleikamp * in that state (the buffers should be marked JBD_Dirty
1003470decc6SDave Kleikamp * instead.) So either the IO is being done under our own
1004470decc6SDave Kleikamp * control and this is a bug, or it's a third party IO such as
1005470decc6SDave Kleikamp * dump(8) (which may leave the buffer scheduled for read ---
1006470decc6SDave Kleikamp * ie. locked but not dirty) or tune2fs (which may actually have
1007470decc6SDave Kleikamp * the buffer dirtied, ugh.) */
1008470decc6SDave Kleikamp
1009e6b9bd72SZhihao Cheng if (buffer_dirty(bh) && jh->b_transaction) {
1010f91d1d04SJan Kara warn_dirty_buffer(bh);
1011470decc6SDave Kleikamp /*
1012e6b9bd72SZhihao Cheng * We need to clean the dirty flag and we must do it under the
1013e6b9bd72SZhihao Cheng * buffer lock to be sure we don't race with running write-out.
1014470decc6SDave Kleikamp */
1015f91d1d04SJan Kara JBUFFER_TRACE(jh, "Journalling dirty buffer");
1016f91d1d04SJan Kara clear_buffer_dirty(bh);
1017e6b9bd72SZhihao Cheng /*
1018e6b9bd72SZhihao Cheng * The buffer is going to be added to BJ_Reserved list now and
1019e6b9bd72SZhihao Cheng * nothing guarantees jbd2_journal_dirty_metadata() will be
1020e6b9bd72SZhihao Cheng * ever called for it. So we need to set jbddirty bit here to
1021e6b9bd72SZhihao Cheng * make sure the buffer is dirtied and written out when the
1022e6b9bd72SZhihao Cheng * journaling machinery is done with it.
1023e6b9bd72SZhihao Cheng */
1024f91d1d04SJan Kara set_buffer_jbddirty(bh);
1025470decc6SDave Kleikamp }
1026470decc6SDave Kleikamp
1027470decc6SDave Kleikamp error = -EROFS;
1028470decc6SDave Kleikamp if (is_handle_aborted(handle)) {
102946417064SThomas Gleixner spin_unlock(&jh->b_state_lock);
1030e6b9bd72SZhihao Cheng unlock_buffer(bh);
1031470decc6SDave Kleikamp goto out;
1032470decc6SDave Kleikamp }
1033470decc6SDave Kleikamp error = 0;
1034470decc6SDave Kleikamp
1035470decc6SDave Kleikamp /*
1036470decc6SDave Kleikamp * The buffer is already part of this transaction if b_transaction or
1037470decc6SDave Kleikamp * b_next_transaction points to it
1038470decc6SDave Kleikamp */
1039470decc6SDave Kleikamp if (jh->b_transaction == transaction ||
1040e6b9bd72SZhihao Cheng jh->b_next_transaction == transaction) {
1041e6b9bd72SZhihao Cheng unlock_buffer(bh);
1042470decc6SDave Kleikamp goto done;
1043e6b9bd72SZhihao Cheng }
1044470decc6SDave Kleikamp
1045470decc6SDave Kleikamp /*
10469fc7c63aSJosef Bacik * this is the first time this transaction is touching this buffer,
10479fc7c63aSJosef Bacik * reset the modified flag
10489fc7c63aSJosef Bacik */
10499fc7c63aSJosef Bacik jh->b_modified = 0;
10509fc7c63aSJosef Bacik
10519fc7c63aSJosef Bacik /*
10528b00f400SJan Kara * If the buffer is not journaled right now, we need to make sure it
10538b00f400SJan Kara * doesn't get written to disk before the caller actually commits the
10548b00f400SJan Kara * new data
10558b00f400SJan Kara */
10568b00f400SJan Kara if (!jh->b_transaction) {
10578b00f400SJan Kara JBUFFER_TRACE(jh, "no transaction");
10588b00f400SJan Kara J_ASSERT_JH(jh, !jh->b_next_transaction);
10598b00f400SJan Kara JBUFFER_TRACE(jh, "file as BJ_Reserved");
1060de92c8caSJan Kara /*
1061de92c8caSJan Kara * Make sure all stores to jh (b_modified, b_frozen_data) are
1062de92c8caSJan Kara * visible before attaching it to the running transaction.
1063de92c8caSJan Kara * Paired with barrier in jbd2_write_access_granted()
1064de92c8caSJan Kara */
1065de92c8caSJan Kara smp_wmb();
10668b00f400SJan Kara spin_lock(&journal->j_list_lock);
1067e6b9bd72SZhihao Cheng if (test_clear_buffer_dirty(bh)) {
1068e6b9bd72SZhihao Cheng /*
1069e6b9bd72SZhihao Cheng * Execute buffer dirty clearing and jh->b_transaction
1070e6b9bd72SZhihao Cheng * assignment under journal->j_list_lock locked to
1071e6b9bd72SZhihao Cheng * prevent bh being removed from checkpoint list if
1072e6b9bd72SZhihao Cheng * the buffer is in an intermediate state (not dirty
1073e6b9bd72SZhihao Cheng * and jh->b_transaction is NULL).
1074e6b9bd72SZhihao Cheng */
1075e6b9bd72SZhihao Cheng JBUFFER_TRACE(jh, "Journalling dirty buffer");
1076e6b9bd72SZhihao Cheng set_buffer_jbddirty(bh);
1077e6b9bd72SZhihao Cheng }
10788b00f400SJan Kara __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
10798b00f400SJan Kara spin_unlock(&journal->j_list_lock);
1080e6b9bd72SZhihao Cheng unlock_buffer(bh);
10818b00f400SJan Kara goto done;
10828b00f400SJan Kara }
1083e6b9bd72SZhihao Cheng unlock_buffer(bh);
1084e6b9bd72SZhihao Cheng
10858b00f400SJan Kara /*
1086470decc6SDave Kleikamp * If there is already a copy-out version of this buffer, then we don't
1087470decc6SDave Kleikamp * need to make another one
1088470decc6SDave Kleikamp */
1089470decc6SDave Kleikamp if (jh->b_frozen_data) {
1090470decc6SDave Kleikamp JBUFFER_TRACE(jh, "has frozen data");
1091470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
1092de92c8caSJan Kara goto attach_next;
1093470decc6SDave Kleikamp }
1094470decc6SDave Kleikamp
1095470decc6SDave Kleikamp JBUFFER_TRACE(jh, "owned by older transaction");
1096470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
10978b00f400SJan Kara J_ASSERT_JH(jh, jh->b_transaction == journal->j_committing_transaction);
1098470decc6SDave Kleikamp
10998b00f400SJan Kara /*
11008b00f400SJan Kara * There is one case we have to be very careful about. If the
11018b00f400SJan Kara * committing transaction is currently writing this buffer out to disk
11028b00f400SJan Kara * and has NOT made a copy-out, then we cannot modify the buffer
11038b00f400SJan Kara * contents at all right now. The essence of copy-out is that it is
11048b00f400SJan Kara * the extra copy, not the primary copy, which gets journaled. If the
11058b00f400SJan Kara * primary copy is already going to disk then we cannot do copy-out
11068b00f400SJan Kara * here.
11078b00f400SJan Kara */
1108b34090e5SJan Kara if (buffer_shadow(bh)) {
1109470decc6SDave Kleikamp JBUFFER_TRACE(jh, "on shadow: sleep");
111046417064SThomas Gleixner spin_unlock(&jh->b_state_lock);
11118b00f400SJan Kara wait_on_bit_io(&bh->b_state, BH_Shadow, TASK_UNINTERRUPTIBLE);
1112470decc6SDave Kleikamp goto repeat;
1113470decc6SDave Kleikamp }
1114470decc6SDave Kleikamp
1115b34090e5SJan Kara /*
11168b00f400SJan Kara * Only do the copy if the currently-owning transaction still needs it.
11178b00f400SJan Kara * If buffer isn't on BJ_Metadata list, the committing transaction is
11188b00f400SJan Kara * past that stage (here we use the fact that BH_Shadow is set under
11198b00f400SJan Kara * bh_state lock together with refiling to BJ_Shadow list and at this
11208b00f400SJan Kara * point we know the buffer doesn't have BH_Shadow set).
1121470decc6SDave Kleikamp *
11228b00f400SJan Kara * Subtle point, though: if this is a get_undo_access, then we will be
11238b00f400SJan Kara * relying on the frozen_data to contain the new value of the
11248b00f400SJan Kara * committed_data record after the transaction, so we HAVE to force the
11258b00f400SJan Kara * frozen_data copy in that case.
1126b34090e5SJan Kara */
1127b34090e5SJan Kara if (jh->b_jlist == BJ_Metadata || force_copy) {
1128470decc6SDave Kleikamp JBUFFER_TRACE(jh, "generate frozen data");
1129470decc6SDave Kleikamp if (!frozen_buffer) {
1130470decc6SDave Kleikamp JBUFFER_TRACE(jh, "allocate memory for buffer");
113146417064SThomas Gleixner spin_unlock(&jh->b_state_lock);
1132490c1b44SMichal Hocko frozen_buffer = jbd2_alloc(jh2bh(jh)->b_size,
1133490c1b44SMichal Hocko GFP_NOFS | __GFP_NOFAIL);
1134470decc6SDave Kleikamp goto repeat;
1135470decc6SDave Kleikamp }
1136470decc6SDave Kleikamp jh->b_frozen_data = frozen_buffer;
1137470decc6SDave Kleikamp frozen_buffer = NULL;
1138ee57aba1SJan Kara jbd2_freeze_jh_data(jh);
1139470decc6SDave Kleikamp }
1140de92c8caSJan Kara attach_next:
1141de92c8caSJan Kara /*
1142de92c8caSJan Kara * Make sure all stores to jh (b_modified, b_frozen_data) are visible
1143de92c8caSJan Kara * before attaching it to the running transaction. Paired with barrier
1144de92c8caSJan Kara * in jbd2_write_access_granted()
1145de92c8caSJan Kara */
1146de92c8caSJan Kara smp_wmb();
1147470decc6SDave Kleikamp jh->b_next_transaction = transaction;
1148470decc6SDave Kleikamp
1149470decc6SDave Kleikamp done:
115046417064SThomas Gleixner spin_unlock(&jh->b_state_lock);
1151470decc6SDave Kleikamp
1152470decc6SDave Kleikamp /*
1153470decc6SDave Kleikamp * If we are about to journal a buffer, then any revoke pending on it is
1154470decc6SDave Kleikamp * no longer valid
1155470decc6SDave Kleikamp */
1156f7f4bccbSMingming Cao jbd2_journal_cancel_revoke(handle, jh);
1157470decc6SDave Kleikamp
1158470decc6SDave Kleikamp out:
1159470decc6SDave Kleikamp if (unlikely(frozen_buffer)) /* It's usually NULL */
1160af1e76d6SMingming Cao jbd2_free(frozen_buffer, bh->b_size);
1161470decc6SDave Kleikamp
1162470decc6SDave Kleikamp JBUFFER_TRACE(jh, "exit");
1163470decc6SDave Kleikamp return error;
1164470decc6SDave Kleikamp }
1165470decc6SDave Kleikamp
1166de92c8caSJan Kara /* Fast check whether buffer is already attached to the required transaction */
jbd2_write_access_granted(handle_t * handle,struct buffer_head * bh,bool undo)1167087ffd4eSJunxiao Bi static bool jbd2_write_access_granted(handle_t *handle, struct buffer_head *bh,
1168087ffd4eSJunxiao Bi bool undo)
1169de92c8caSJan Kara {
1170de92c8caSJan Kara struct journal_head *jh;
1171de92c8caSJan Kara bool ret = false;
1172de92c8caSJan Kara
1173de92c8caSJan Kara /* Dirty buffers require special handling... */
1174de92c8caSJan Kara if (buffer_dirty(bh))
1175de92c8caSJan Kara return false;
1176de92c8caSJan Kara
1177de92c8caSJan Kara /*
1178de92c8caSJan Kara * RCU protects us from dereferencing freed pages. So the checks we do
1179de92c8caSJan Kara * are guaranteed not to oops. However the jh slab object can get freed
1180de92c8caSJan Kara * & reallocated while we work with it. So we have to be careful. When
1181de92c8caSJan Kara * we see jh attached to the running transaction, we know it must stay
1182de92c8caSJan Kara * so until the transaction is committed. Thus jh won't be freed and
1183de92c8caSJan Kara * will be attached to the same bh while we run. However it can
1184de92c8caSJan Kara * happen jh gets freed, reallocated, and attached to the transaction
1185de92c8caSJan Kara * just after we get pointer to it from bh. So we have to be careful
1186de92c8caSJan Kara * and recheck jh still belongs to our bh before we return success.
1187de92c8caSJan Kara */
1188de92c8caSJan Kara rcu_read_lock();
1189de92c8caSJan Kara if (!buffer_jbd(bh))
1190de92c8caSJan Kara goto out;
1191de92c8caSJan Kara /* This should be bh2jh() but that doesn't work with inline functions */
1192de92c8caSJan Kara jh = READ_ONCE(bh->b_private);
1193de92c8caSJan Kara if (!jh)
1194de92c8caSJan Kara goto out;
1195087ffd4eSJunxiao Bi /* For undo access buffer must have data copied */
1196087ffd4eSJunxiao Bi if (undo && !jh->b_committed_data)
1197087ffd4eSJunxiao Bi goto out;
11986c5d9112SQian Cai if (READ_ONCE(jh->b_transaction) != handle->h_transaction &&
11996c5d9112SQian Cai READ_ONCE(jh->b_next_transaction) != handle->h_transaction)
1200de92c8caSJan Kara goto out;
1201de92c8caSJan Kara /*
1202de92c8caSJan Kara * There are two reasons for the barrier here:
1203de92c8caSJan Kara * 1) Make sure to fetch b_bh after we did previous checks so that we
1204de92c8caSJan Kara * detect when jh went through free, realloc, attach to transaction
1205de92c8caSJan Kara * while we were checking. Paired with implicit barrier in that path.
1206de92c8caSJan Kara * 2) So that access to bh done after jbd2_write_access_granted()
1207de92c8caSJan Kara * doesn't get reordered and see inconsistent state of concurrent
1208de92c8caSJan Kara * do_get_write_access().
1209de92c8caSJan Kara */
1210de92c8caSJan Kara smp_mb();
1211de92c8caSJan Kara if (unlikely(jh->b_bh != bh))
1212de92c8caSJan Kara goto out;
1213de92c8caSJan Kara ret = true;
1214de92c8caSJan Kara out:
1215de92c8caSJan Kara rcu_read_unlock();
1216de92c8caSJan Kara return ret;
1217de92c8caSJan Kara }
1218de92c8caSJan Kara
1219470decc6SDave Kleikamp /**
12202bf31d94SMauro Carvalho Chehab * jbd2_journal_get_write_access() - notify intent to modify a buffer
12212bf31d94SMauro Carvalho Chehab * for metadata (not data) update.
1222470decc6SDave Kleikamp * @handle: transaction to add buffer modifications to
1223470decc6SDave Kleikamp * @bh: bh to be used for metadata writes
1224470decc6SDave Kleikamp *
1225df1b560aSMauro Carvalho Chehab * Returns: error code or 0 on success.
1226470decc6SDave Kleikamp *
1227470decc6SDave Kleikamp * In full data journalling mode the buffer may be of type BJ_AsyncData,
1228df1b560aSMauro Carvalho Chehab * because we're ``write()ing`` a buffer which is also part of a shared mapping.
1229470decc6SDave Kleikamp */
1230470decc6SDave Kleikamp
jbd2_journal_get_write_access(handle_t * handle,struct buffer_head * bh)1231f7f4bccbSMingming Cao int jbd2_journal_get_write_access(handle_t *handle, struct buffer_head *bh)
1232470decc6SDave Kleikamp {
1233de92c8caSJan Kara struct journal_head *jh;
1234470decc6SDave Kleikamp int rc;
1235470decc6SDave Kleikamp
12368eedabfdSwangyan if (is_handle_aborted(handle))
12378eedabfdSwangyan return -EROFS;
12388eedabfdSwangyan
1239087ffd4eSJunxiao Bi if (jbd2_write_access_granted(handle, bh, false))
1240de92c8caSJan Kara return 0;
1241de92c8caSJan Kara
1242de92c8caSJan Kara jh = jbd2_journal_add_journal_head(bh);
1243470decc6SDave Kleikamp /* We do not want to get caught playing with fields which the
1244470decc6SDave Kleikamp * log thread also manipulates. Make sure that the buffer
1245470decc6SDave Kleikamp * completes any outstanding IO before proceeding. */
1246470decc6SDave Kleikamp rc = do_get_write_access(handle, jh, 0);
1247f7f4bccbSMingming Cao jbd2_journal_put_journal_head(jh);
1248470decc6SDave Kleikamp return rc;
1249470decc6SDave Kleikamp }
1250470decc6SDave Kleikamp
1251470decc6SDave Kleikamp
1252470decc6SDave Kleikamp /*
1253470decc6SDave Kleikamp * When the user wants to journal a newly created buffer_head
1254470decc6SDave Kleikamp * (ie. getblk() returned a new buffer and we are going to populate it
1255470decc6SDave Kleikamp * manually rather than reading off disk), then we need to keep the
1256470decc6SDave Kleikamp * buffer_head locked until it has been completely filled with new
1257470decc6SDave Kleikamp * data. In this case, we should be able to make the assertion that
1258470decc6SDave Kleikamp * the bh is not already part of an existing transaction.
1259470decc6SDave Kleikamp *
1260470decc6SDave Kleikamp * The buffer should already be locked by the caller by this point.
1261470decc6SDave Kleikamp * There is no lock ranking violation: it was a newly created,
1262470decc6SDave Kleikamp * unlocked buffer beforehand. */
1263470decc6SDave Kleikamp
1264470decc6SDave Kleikamp /**
12652bf31d94SMauro Carvalho Chehab * jbd2_journal_get_create_access () - notify intent to use newly created bh
1266470decc6SDave Kleikamp * @handle: transaction to new buffer to
1267470decc6SDave Kleikamp * @bh: new buffer.
1268470decc6SDave Kleikamp *
1269470decc6SDave Kleikamp * Call this if you create a new bh.
1270470decc6SDave Kleikamp */
jbd2_journal_get_create_access(handle_t * handle,struct buffer_head * bh)1271f7f4bccbSMingming Cao int jbd2_journal_get_create_access(handle_t *handle, struct buffer_head *bh)
1272470decc6SDave Kleikamp {
1273470decc6SDave Kleikamp transaction_t *transaction = handle->h_transaction;
127441a5b913STheodore Ts'o journal_t *journal;
1275f7f4bccbSMingming Cao struct journal_head *jh = jbd2_journal_add_journal_head(bh);
1276470decc6SDave Kleikamp int err;
1277470decc6SDave Kleikamp
1278cb3b3bf2SJan Kara jbd2_debug(5, "journal_head %p\n", jh);
1279470decc6SDave Kleikamp err = -EROFS;
1280470decc6SDave Kleikamp if (is_handle_aborted(handle))
1281470decc6SDave Kleikamp goto out;
128241a5b913STheodore Ts'o journal = transaction->t_journal;
1283470decc6SDave Kleikamp err = 0;
1284470decc6SDave Kleikamp
1285470decc6SDave Kleikamp JBUFFER_TRACE(jh, "entry");
1286470decc6SDave Kleikamp /*
1287470decc6SDave Kleikamp * The buffer may already belong to this transaction due to pre-zeroing
1288470decc6SDave Kleikamp * in the filesystem's new_block code. It may also be on the previous,
1289470decc6SDave Kleikamp * committing transaction's lists, but it HAS to be in Forget state in
1290470decc6SDave Kleikamp * that case: the transaction must have deleted the buffer for it to be
1291470decc6SDave Kleikamp * reused here.
1292470decc6SDave Kleikamp */
129346417064SThomas Gleixner spin_lock(&jh->b_state_lock);
1294470decc6SDave Kleikamp J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
1295470decc6SDave Kleikamp jh->b_transaction == NULL ||
1296470decc6SDave Kleikamp (jh->b_transaction == journal->j_committing_transaction &&
1297470decc6SDave Kleikamp jh->b_jlist == BJ_Forget)));
1298470decc6SDave Kleikamp
1299470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
1300470decc6SDave Kleikamp J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
1301470decc6SDave Kleikamp
1302470decc6SDave Kleikamp if (jh->b_transaction == NULL) {
1303f91d1d04SJan Kara /*
1304f91d1d04SJan Kara * Previous jbd2_journal_forget() could have left the buffer
1305f91d1d04SJan Kara * with jbddirty bit set because it was being committed. When
1306f91d1d04SJan Kara * the commit finished, we've filed the buffer for
1307f91d1d04SJan Kara * checkpointing and marked it dirty. Now we are reallocating
1308f91d1d04SJan Kara * the buffer so the transaction freeing it must have
1309f91d1d04SJan Kara * committed and so it's safe to clear the dirty bit.
1310f91d1d04SJan Kara */
1311f91d1d04SJan Kara clear_buffer_dirty(jh2bh(jh));
13129fc7c63aSJosef Bacik /* first access by this transaction */
13139fc7c63aSJosef Bacik jh->b_modified = 0;
13149fc7c63aSJosef Bacik
1315470decc6SDave Kleikamp JBUFFER_TRACE(jh, "file as BJ_Reserved");
13166e4862a5STheodore Ts'o spin_lock(&journal->j_list_lock);
1317f7f4bccbSMingming Cao __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
1318559cce69STaesoo Kim spin_unlock(&journal->j_list_lock);
1319470decc6SDave Kleikamp } else if (jh->b_transaction == journal->j_committing_transaction) {
13209fc7c63aSJosef Bacik /* first access by this transaction */
13219fc7c63aSJosef Bacik jh->b_modified = 0;
13229fc7c63aSJosef Bacik
1323470decc6SDave Kleikamp JBUFFER_TRACE(jh, "set next transaction");
13246e4862a5STheodore Ts'o spin_lock(&journal->j_list_lock);
1325470decc6SDave Kleikamp jh->b_next_transaction = transaction;
1326470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock);
1327559cce69STaesoo Kim }
132846417064SThomas Gleixner spin_unlock(&jh->b_state_lock);
1329470decc6SDave Kleikamp
1330470decc6SDave Kleikamp /*
1331470decc6SDave Kleikamp * akpm: I added this. ext3_alloc_branch can pick up new indirect
1332470decc6SDave Kleikamp * blocks which contain freed but then revoked metadata. We need
1333470decc6SDave Kleikamp * to cancel the revoke in case we end up freeing it yet again
1334470decc6SDave Kleikamp * and the reallocating as data - this would cause a second revoke,
1335470decc6SDave Kleikamp * which hits an assertion error.
1336470decc6SDave Kleikamp */
1337470decc6SDave Kleikamp JBUFFER_TRACE(jh, "cancelling revoke");
1338f7f4bccbSMingming Cao jbd2_journal_cancel_revoke(handle, jh);
1339470decc6SDave Kleikamp out:
13403991b400SDing Dinghua jbd2_journal_put_journal_head(jh);
1341470decc6SDave Kleikamp return err;
1342470decc6SDave Kleikamp }
1343470decc6SDave Kleikamp
1344470decc6SDave Kleikamp /**
13452bf31d94SMauro Carvalho Chehab * jbd2_journal_get_undo_access() - Notify intent to modify metadata with
1346470decc6SDave Kleikamp * non-rewindable consequences
1347470decc6SDave Kleikamp * @handle: transaction
1348470decc6SDave Kleikamp * @bh: buffer to undo
1349470decc6SDave Kleikamp *
1350470decc6SDave Kleikamp * Sometimes there is a need to distinguish between metadata which has
1351470decc6SDave Kleikamp * been committed to disk and that which has not. The ext3fs code uses
1352470decc6SDave Kleikamp * this for freeing and allocating space, we have to make sure that we
1353470decc6SDave Kleikamp * do not reuse freed space until the deallocation has been committed,
1354470decc6SDave Kleikamp * since if we overwrote that space we would make the delete
1355470decc6SDave Kleikamp * un-rewindable in case of a crash.
1356470decc6SDave Kleikamp *
1357f7f4bccbSMingming Cao * To deal with that, jbd2_journal_get_undo_access requests write access to a
1358470decc6SDave Kleikamp * buffer for parts of non-rewindable operations such as delete
1359470decc6SDave Kleikamp * operations on the bitmaps. The journaling code must keep a copy of
1360470decc6SDave Kleikamp * the buffer's contents prior to the undo_access call until such time
1361470decc6SDave Kleikamp * as we know that the buffer has definitely been committed to disk.
1362470decc6SDave Kleikamp *
1363470decc6SDave Kleikamp * We never need to know which transaction the committed data is part
1364470decc6SDave Kleikamp * of, buffers touched here are guaranteed to be dirtied later and so
1365470decc6SDave Kleikamp * will be committed to a new transaction in due course, at which point
1366470decc6SDave Kleikamp * we can discard the old committed data pointer.
1367470decc6SDave Kleikamp *
1368470decc6SDave Kleikamp * Returns error number or 0 on success.
1369470decc6SDave Kleikamp */
jbd2_journal_get_undo_access(handle_t * handle,struct buffer_head * bh)1370f7f4bccbSMingming Cao int jbd2_journal_get_undo_access(handle_t *handle, struct buffer_head *bh)
1371470decc6SDave Kleikamp {
1372470decc6SDave Kleikamp int err;
1373de92c8caSJan Kara struct journal_head *jh;
1374470decc6SDave Kleikamp char *committed_data = NULL;
1375470decc6SDave Kleikamp
13768eedabfdSwangyan if (is_handle_aborted(handle))
13778eedabfdSwangyan return -EROFS;
13788eedabfdSwangyan
1379087ffd4eSJunxiao Bi if (jbd2_write_access_granted(handle, bh, true))
1380de92c8caSJan Kara return 0;
1381470decc6SDave Kleikamp
1382de92c8caSJan Kara jh = jbd2_journal_add_journal_head(bh);
138301215d3eSzhangyi (F) JBUFFER_TRACE(jh, "entry");
138401215d3eSzhangyi (F)
1385470decc6SDave Kleikamp /*
1386470decc6SDave Kleikamp * Do this first --- it can drop the journal lock, so we want to
1387470decc6SDave Kleikamp * make sure that obtaining the committed_data is done
1388470decc6SDave Kleikamp * atomically wrt. completion of any outstanding commits.
1389470decc6SDave Kleikamp */
1390470decc6SDave Kleikamp err = do_get_write_access(handle, jh, 1);
1391470decc6SDave Kleikamp if (err)
1392470decc6SDave Kleikamp goto out;
1393470decc6SDave Kleikamp
1394470decc6SDave Kleikamp repeat:
1395490c1b44SMichal Hocko if (!jh->b_committed_data)
1396490c1b44SMichal Hocko committed_data = jbd2_alloc(jh2bh(jh)->b_size,
1397490c1b44SMichal Hocko GFP_NOFS|__GFP_NOFAIL);
1398470decc6SDave Kleikamp
139946417064SThomas Gleixner spin_lock(&jh->b_state_lock);
1400470decc6SDave Kleikamp if (!jh->b_committed_data) {
1401470decc6SDave Kleikamp /* Copy out the current buffer contents into the
1402470decc6SDave Kleikamp * preserved, committed copy. */
1403470decc6SDave Kleikamp JBUFFER_TRACE(jh, "generate b_committed data");
1404470decc6SDave Kleikamp if (!committed_data) {
140546417064SThomas Gleixner spin_unlock(&jh->b_state_lock);
1406470decc6SDave Kleikamp goto repeat;
1407470decc6SDave Kleikamp }
1408470decc6SDave Kleikamp
1409470decc6SDave Kleikamp jh->b_committed_data = committed_data;
1410470decc6SDave Kleikamp committed_data = NULL;
1411470decc6SDave Kleikamp memcpy(jh->b_committed_data, bh->b_data, bh->b_size);
1412470decc6SDave Kleikamp }
141346417064SThomas Gleixner spin_unlock(&jh->b_state_lock);
1414470decc6SDave Kleikamp out:
1415f7f4bccbSMingming Cao jbd2_journal_put_journal_head(jh);
1416470decc6SDave Kleikamp if (unlikely(committed_data))
1417af1e76d6SMingming Cao jbd2_free(committed_data, bh->b_size);
1418470decc6SDave Kleikamp return err;
1419470decc6SDave Kleikamp }
1420470decc6SDave Kleikamp
1421470decc6SDave Kleikamp /**
14222bf31d94SMauro Carvalho Chehab * jbd2_journal_set_triggers() - Add triggers for commit writeout
1423e06c8227SJoel Becker * @bh: buffer to trigger on
1424e06c8227SJoel Becker * @type: struct jbd2_buffer_trigger_type containing the trigger(s).
1425e06c8227SJoel Becker *
1426e06c8227SJoel Becker * Set any triggers on this journal_head. This is always safe, because
1427e06c8227SJoel Becker * triggers for a committing buffer will be saved off, and triggers for
1428e06c8227SJoel Becker * a running transaction will match the buffer in that transaction.
1429e06c8227SJoel Becker *
1430e06c8227SJoel Becker * Call with NULL to clear the triggers.
1431e06c8227SJoel Becker */
jbd2_journal_set_triggers(struct buffer_head * bh,struct jbd2_buffer_trigger_type * type)1432e06c8227SJoel Becker void jbd2_journal_set_triggers(struct buffer_head *bh,
1433e06c8227SJoel Becker struct jbd2_buffer_trigger_type *type)
1434e06c8227SJoel Becker {
1435ad56edadSJan Kara struct journal_head *jh = jbd2_journal_grab_journal_head(bh);
1436e06c8227SJoel Becker
1437188c299eSJan Kara if (WARN_ON_ONCE(!jh))
1438ad56edadSJan Kara return;
1439e06c8227SJoel Becker jh->b_triggers = type;
1440ad56edadSJan Kara jbd2_journal_put_journal_head(jh);
1441e06c8227SJoel Becker }
1442e06c8227SJoel Becker
jbd2_buffer_frozen_trigger(struct journal_head * jh,void * mapped_data,struct jbd2_buffer_trigger_type * triggers)144313ceef09SJan Kara void jbd2_buffer_frozen_trigger(struct journal_head *jh, void *mapped_data,
1444e06c8227SJoel Becker struct jbd2_buffer_trigger_type *triggers)
1445e06c8227SJoel Becker {
1446e06c8227SJoel Becker struct buffer_head *bh = jh2bh(jh);
1447e06c8227SJoel Becker
144813ceef09SJan Kara if (!triggers || !triggers->t_frozen)
1449e06c8227SJoel Becker return;
1450e06c8227SJoel Becker
145113ceef09SJan Kara triggers->t_frozen(triggers, bh, mapped_data, bh->b_size);
1452e06c8227SJoel Becker }
1453e06c8227SJoel Becker
jbd2_buffer_abort_trigger(struct journal_head * jh,struct jbd2_buffer_trigger_type * triggers)1454e06c8227SJoel Becker void jbd2_buffer_abort_trigger(struct journal_head *jh,
1455e06c8227SJoel Becker struct jbd2_buffer_trigger_type *triggers)
1456e06c8227SJoel Becker {
1457e06c8227SJoel Becker if (!triggers || !triggers->t_abort)
1458e06c8227SJoel Becker return;
1459e06c8227SJoel Becker
1460e06c8227SJoel Becker triggers->t_abort(triggers, jh2bh(jh));
1461e06c8227SJoel Becker }
1462e06c8227SJoel Becker
1463e06c8227SJoel Becker /**
14642bf31d94SMauro Carvalho Chehab * jbd2_journal_dirty_metadata() - mark a buffer as containing dirty metadata
1465470decc6SDave Kleikamp * @handle: transaction to add buffer to.
1466470decc6SDave Kleikamp * @bh: buffer to mark
1467470decc6SDave Kleikamp *
1468470decc6SDave Kleikamp * mark dirty metadata which needs to be journaled as part of the current
1469470decc6SDave Kleikamp * transaction.
1470470decc6SDave Kleikamp *
14719ea7a0dfSTheodore Ts'o * The buffer must have previously had jbd2_journal_get_write_access()
14729ea7a0dfSTheodore Ts'o * called so that it has a valid journal_head attached to the buffer
14739ea7a0dfSTheodore Ts'o * head.
14749ea7a0dfSTheodore Ts'o *
1475470decc6SDave Kleikamp * The buffer is placed on the transaction's metadata list and is marked
1476470decc6SDave Kleikamp * as belonging to the transaction.
1477470decc6SDave Kleikamp *
1478470decc6SDave Kleikamp * Returns error number or 0 on success.
1479470decc6SDave Kleikamp *
1480470decc6SDave Kleikamp * Special care needs to be taken if the buffer already belongs to the
1481470decc6SDave Kleikamp * current committing transaction (in which case we should have frozen
1482470decc6SDave Kleikamp * data present for that commit). In that case, we don't relink the
1483470decc6SDave Kleikamp * buffer: that only gets done when the old transaction finally
1484470decc6SDave Kleikamp * completes its commit.
1485470decc6SDave Kleikamp */
jbd2_journal_dirty_metadata(handle_t * handle,struct buffer_head * bh)1486f7f4bccbSMingming Cao int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
1487470decc6SDave Kleikamp {
1488470decc6SDave Kleikamp transaction_t *transaction = handle->h_transaction;
148941a5b913STheodore Ts'o journal_t *journal;
1490ad56edadSJan Kara struct journal_head *jh;
14919ea7a0dfSTheodore Ts'o int ret = 0;
1492470decc6SDave Kleikamp
149301215d3eSzhangyi (F) if (!buffer_jbd(bh))
149401215d3eSzhangyi (F) return -EUCLEAN;
149501215d3eSzhangyi (F)
14966e06ae88SJan Kara /*
14976e06ae88SJan Kara * We don't grab jh reference here since the buffer must be part
14986e06ae88SJan Kara * of the running transaction.
14996e06ae88SJan Kara */
15006e06ae88SJan Kara jh = bh2jh(bh);
1501cb3b3bf2SJan Kara jbd2_debug(5, "journal_head %p\n", jh);
150201215d3eSzhangyi (F) JBUFFER_TRACE(jh, "entry");
150301215d3eSzhangyi (F)
15046e06ae88SJan Kara /*
15056e06ae88SJan Kara * This and the following assertions are unreliable since we may see jh
15066e06ae88SJan Kara * in inconsistent state unless we grab bh_state lock. But this is
15076e06ae88SJan Kara * crucial to catch bugs so let's do a reliable check until the
15086e06ae88SJan Kara * lockless handling is fully proven.
15096e06ae88SJan Kara */
151083fe6b18SJan Kara if (data_race(jh->b_transaction != transaction &&
151183fe6b18SJan Kara jh->b_next_transaction != transaction)) {
151246417064SThomas Gleixner spin_lock(&jh->b_state_lock);
15136e06ae88SJan Kara J_ASSERT_JH(jh, jh->b_transaction == transaction ||
15146e06ae88SJan Kara jh->b_next_transaction == transaction);
151546417064SThomas Gleixner spin_unlock(&jh->b_state_lock);
15166e06ae88SJan Kara }
15176e06ae88SJan Kara if (jh->b_modified == 1) {
15186e06ae88SJan Kara /* If it's in our transaction it must be in BJ_Metadata list. */
151983fe6b18SJan Kara if (data_race(jh->b_transaction == transaction &&
152083fe6b18SJan Kara jh->b_jlist != BJ_Metadata)) {
152146417064SThomas Gleixner spin_lock(&jh->b_state_lock);
1522e09463f2STheodore Ts'o if (jh->b_transaction == transaction &&
1523e09463f2STheodore Ts'o jh->b_jlist != BJ_Metadata)
1524e09463f2STheodore Ts'o pr_err("JBD2: assertion failure: h_type=%u "
1525e09463f2STheodore Ts'o "h_line_no=%u block_no=%llu jlist=%u\n",
1526e09463f2STheodore Ts'o handle->h_type, handle->h_line_no,
1527e09463f2STheodore Ts'o (unsigned long long) bh->b_blocknr,
1528e09463f2STheodore Ts'o jh->b_jlist);
15296e06ae88SJan Kara J_ASSERT_JH(jh, jh->b_transaction != transaction ||
15306e06ae88SJan Kara jh->b_jlist == BJ_Metadata);
153146417064SThomas Gleixner spin_unlock(&jh->b_state_lock);
15326e06ae88SJan Kara }
15336e06ae88SJan Kara goto out;
15346e06ae88SJan Kara }
15356e06ae88SJan Kara
15366e06ae88SJan Kara journal = transaction->t_journal;
153746417064SThomas Gleixner spin_lock(&jh->b_state_lock);
1538470decc6SDave Kleikamp
15394a734f08SZhihao Cheng if (is_handle_aborted(handle)) {
15404a734f08SZhihao Cheng /*
15414a734f08SZhihao Cheng * Check journal aborting with @jh->b_state_lock locked,
15424a734f08SZhihao Cheng * since 'jh->b_transaction' could be replaced with
15434a734f08SZhihao Cheng * 'jh->b_next_transaction' during old transaction
15444a734f08SZhihao Cheng * committing if journal aborted, which may fail
15454a734f08SZhihao Cheng * assertion on 'jh->b_frozen_data == NULL'.
15464a734f08SZhihao Cheng */
15474a734f08SZhihao Cheng ret = -EROFS;
15484a734f08SZhihao Cheng goto out_unlock_bh;
15494a734f08SZhihao Cheng }
15504a734f08SZhihao Cheng
1551470decc6SDave Kleikamp if (jh->b_modified == 0) {
1552470decc6SDave Kleikamp /*
1553470decc6SDave Kleikamp * This buffer's got modified and becoming part
1554470decc6SDave Kleikamp * of the transaction. This needs to be done
1555470decc6SDave Kleikamp * once a transaction -bzzz
1556470decc6SDave Kleikamp */
1557d090707eSJan Kara if (WARN_ON_ONCE(jbd2_handle_buffer_credits(handle) <= 0)) {
1558f6c07cadSTheodore Ts'o ret = -ENOSPC;
1559f6c07cadSTheodore Ts'o goto out_unlock_bh;
1560f6c07cadSTheodore Ts'o }
1561e09463f2STheodore Ts'o jh->b_modified = 1;
1562933f1c1eSJan Kara handle->h_total_credits--;
1563470decc6SDave Kleikamp }
1564470decc6SDave Kleikamp
1565470decc6SDave Kleikamp /*
1566470decc6SDave Kleikamp * fastpath, to avoid expensive locking. If this buffer is already
1567470decc6SDave Kleikamp * on the running transaction's metadata list there is nothing to do.
1568470decc6SDave Kleikamp * Nobody can take it off again because there is a handle open.
1569470decc6SDave Kleikamp * I _think_ we're OK here with SMP barriers - a mistaken decision will
1570470decc6SDave Kleikamp * result in this test being false, so we go in and take the locks.
1571470decc6SDave Kleikamp */
1572470decc6SDave Kleikamp if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) {
1573470decc6SDave Kleikamp JBUFFER_TRACE(jh, "fastpath");
15749ea7a0dfSTheodore Ts'o if (unlikely(jh->b_transaction !=
15759ea7a0dfSTheodore Ts'o journal->j_running_transaction)) {
1576a67c848aSDmitry Monakhov printk(KERN_ERR "JBD2: %s: "
15779ea7a0dfSTheodore Ts'o "jh->b_transaction (%llu, %p, %u) != "
157866a4cb18STheodore Ts'o "journal->j_running_transaction (%p, %u)\n",
15799ea7a0dfSTheodore Ts'o journal->j_devname,
15809ea7a0dfSTheodore Ts'o (unsigned long long) bh->b_blocknr,
15819ea7a0dfSTheodore Ts'o jh->b_transaction,
15829ea7a0dfSTheodore Ts'o jh->b_transaction ? jh->b_transaction->t_tid : 0,
15839ea7a0dfSTheodore Ts'o journal->j_running_transaction,
15849ea7a0dfSTheodore Ts'o journal->j_running_transaction ?
15859ea7a0dfSTheodore Ts'o journal->j_running_transaction->t_tid : 0);
15869ea7a0dfSTheodore Ts'o ret = -EINVAL;
15879ea7a0dfSTheodore Ts'o }
1588470decc6SDave Kleikamp goto out_unlock_bh;
1589470decc6SDave Kleikamp }
1590470decc6SDave Kleikamp
1591470decc6SDave Kleikamp set_buffer_jbddirty(bh);
1592470decc6SDave Kleikamp
1593470decc6SDave Kleikamp /*
1594470decc6SDave Kleikamp * Metadata already on the current transaction list doesn't
1595470decc6SDave Kleikamp * need to be filed. Metadata on another transaction's list must
1596470decc6SDave Kleikamp * be committing, and will be refiled once the commit completes:
1597470decc6SDave Kleikamp * leave it alone for now.
1598470decc6SDave Kleikamp */
1599470decc6SDave Kleikamp if (jh->b_transaction != transaction) {
1600470decc6SDave Kleikamp JBUFFER_TRACE(jh, "already on other transaction");
160166a4cb18STheodore Ts'o if (unlikely(((jh->b_transaction !=
160266a4cb18STheodore Ts'o journal->j_committing_transaction)) ||
160366a4cb18STheodore Ts'o (jh->b_next_transaction != transaction))) {
160466a4cb18STheodore Ts'o printk(KERN_ERR "jbd2_journal_dirty_metadata: %s: "
160566a4cb18STheodore Ts'o "bad jh for block %llu: "
160666a4cb18STheodore Ts'o "transaction (%p, %u), "
160766a4cb18STheodore Ts'o "jh->b_transaction (%p, %u), "
160866a4cb18STheodore Ts'o "jh->b_next_transaction (%p, %u), jlist %u\n",
16099ea7a0dfSTheodore Ts'o journal->j_devname,
16109ea7a0dfSTheodore Ts'o (unsigned long long) bh->b_blocknr,
161166a4cb18STheodore Ts'o transaction, transaction->t_tid,
16129ea7a0dfSTheodore Ts'o jh->b_transaction,
161366a4cb18STheodore Ts'o jh->b_transaction ?
161466a4cb18STheodore Ts'o jh->b_transaction->t_tid : 0,
16159ea7a0dfSTheodore Ts'o jh->b_next_transaction,
16169ea7a0dfSTheodore Ts'o jh->b_next_transaction ?
16179ea7a0dfSTheodore Ts'o jh->b_next_transaction->t_tid : 0,
161866a4cb18STheodore Ts'o jh->b_jlist);
161966a4cb18STheodore Ts'o WARN_ON(1);
16209ea7a0dfSTheodore Ts'o ret = -EINVAL;
16219ea7a0dfSTheodore Ts'o }
1622470decc6SDave Kleikamp /* And this case is illegal: we can't reuse another
1623470decc6SDave Kleikamp * transaction's data buffer, ever. */
1624470decc6SDave Kleikamp goto out_unlock_bh;
1625470decc6SDave Kleikamp }
1626470decc6SDave Kleikamp
1627470decc6SDave Kleikamp /* That test should have eliminated the following case: */
16284019191bSMingming Cao J_ASSERT_JH(jh, jh->b_frozen_data == NULL);
1629470decc6SDave Kleikamp
1630470decc6SDave Kleikamp JBUFFER_TRACE(jh, "file as BJ_Metadata");
1631470decc6SDave Kleikamp spin_lock(&journal->j_list_lock);
163241a5b913STheodore Ts'o __jbd2_journal_file_buffer(jh, transaction, BJ_Metadata);
1633470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock);
1634470decc6SDave Kleikamp out_unlock_bh:
163546417064SThomas Gleixner spin_unlock(&jh->b_state_lock);
1636470decc6SDave Kleikamp out:
1637470decc6SDave Kleikamp JBUFFER_TRACE(jh, "exit");
16389ea7a0dfSTheodore Ts'o return ret;
1639470decc6SDave Kleikamp }
1640470decc6SDave Kleikamp
1641470decc6SDave Kleikamp /**
16422bf31d94SMauro Carvalho Chehab * jbd2_journal_forget() - bforget() for potentially-journaled buffers.
1643470decc6SDave Kleikamp * @handle: transaction handle
1644470decc6SDave Kleikamp * @bh: bh to 'forget'
1645470decc6SDave Kleikamp *
1646470decc6SDave Kleikamp * We can only do the bforget if there are no commits pending against the
1647470decc6SDave Kleikamp * buffer. If the buffer is dirty in the current running transaction we
1648470decc6SDave Kleikamp * can safely unlink it.
1649470decc6SDave Kleikamp *
1650470decc6SDave Kleikamp * bh may not be a journalled buffer at all - it may be a non-JBD
1651470decc6SDave Kleikamp * buffer which came off the hashtable. Check for this.
1652470decc6SDave Kleikamp *
1653470decc6SDave Kleikamp * Decrements bh->b_count by one.
1654470decc6SDave Kleikamp *
1655470decc6SDave Kleikamp * Allow this call even if the handle has aborted --- it may be part of
1656470decc6SDave Kleikamp * the caller's cleanup after an abort.
1657470decc6SDave Kleikamp */
jbd2_journal_forget(handle_t * handle,struct buffer_head * bh)1658f7f4bccbSMingming Cao int jbd2_journal_forget(handle_t *handle, struct buffer_head *bh)
1659470decc6SDave Kleikamp {
1660470decc6SDave Kleikamp transaction_t *transaction = handle->h_transaction;
166141a5b913STheodore Ts'o journal_t *journal;
1662470decc6SDave Kleikamp struct journal_head *jh;
1663470decc6SDave Kleikamp int drop_reserve = 0;
1664470decc6SDave Kleikamp int err = 0;
16651dfc3220SJosef Bacik int was_modified = 0;
1666470decc6SDave Kleikamp
166741a5b913STheodore Ts'o if (is_handle_aborted(handle))
166841a5b913STheodore Ts'o return -EROFS;
166941a5b913STheodore Ts'o journal = transaction->t_journal;
167041a5b913STheodore Ts'o
1671470decc6SDave Kleikamp BUFFER_TRACE(bh, "entry");
1672470decc6SDave Kleikamp
167346417064SThomas Gleixner jh = jbd2_journal_grab_journal_head(bh);
167446417064SThomas Gleixner if (!jh) {
167546417064SThomas Gleixner __bforget(bh);
167646417064SThomas Gleixner return 0;
167746417064SThomas Gleixner }
1678470decc6SDave Kleikamp
167946417064SThomas Gleixner spin_lock(&jh->b_state_lock);
1680470decc6SDave Kleikamp
1681470decc6SDave Kleikamp /* Critical error: attempting to delete a bitmap buffer, maybe?
1682470decc6SDave Kleikamp * Don't do any jbd operations, and return an error. */
1683470decc6SDave Kleikamp if (!J_EXPECT_JH(jh, !jh->b_committed_data,
1684470decc6SDave Kleikamp "inconsistent data on disk")) {
1685470decc6SDave Kleikamp err = -EIO;
16862e710ff0SJan Kara goto drop;
1687470decc6SDave Kleikamp }
1688470decc6SDave Kleikamp
168948fc7f7eSAdam Buchbinder /* keep track of whether or not this transaction modified us */
16901dfc3220SJosef Bacik was_modified = jh->b_modified;
16911dfc3220SJosef Bacik
1692470decc6SDave Kleikamp /*
1693470decc6SDave Kleikamp * The buffer's going from the transaction, we must drop
1694470decc6SDave Kleikamp * all references -bzzz
1695470decc6SDave Kleikamp */
1696470decc6SDave Kleikamp jh->b_modified = 0;
1697470decc6SDave Kleikamp
169841a5b913STheodore Ts'o if (jh->b_transaction == transaction) {
1699470decc6SDave Kleikamp J_ASSERT_JH(jh, !jh->b_frozen_data);
1700470decc6SDave Kleikamp
1701470decc6SDave Kleikamp /* If we are forgetting a buffer which is already part
1702470decc6SDave Kleikamp * of this transaction, then we can just drop it from
1703470decc6SDave Kleikamp * the transaction immediately. */
1704470decc6SDave Kleikamp clear_buffer_dirty(bh);
1705470decc6SDave Kleikamp clear_buffer_jbddirty(bh);
1706470decc6SDave Kleikamp
1707470decc6SDave Kleikamp JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
1708470decc6SDave Kleikamp
17091dfc3220SJosef Bacik /*
17101dfc3220SJosef Bacik * we only want to drop a reference if this transaction
17111dfc3220SJosef Bacik * modified the buffer
17121dfc3220SJosef Bacik */
17131dfc3220SJosef Bacik if (was_modified)
1714470decc6SDave Kleikamp drop_reserve = 1;
1715470decc6SDave Kleikamp
1716470decc6SDave Kleikamp /*
1717470decc6SDave Kleikamp * We are no longer going to journal this buffer.
1718470decc6SDave Kleikamp * However, the commit of this transaction is still
1719470decc6SDave Kleikamp * important to the buffer: the delete that we are now
1720470decc6SDave Kleikamp * processing might obsolete an old log entry, so by
1721470decc6SDave Kleikamp * committing, we can satisfy the buffer's checkpoint.
1722470decc6SDave Kleikamp *
1723470decc6SDave Kleikamp * So, if we have a checkpoint on the buffer, we should
1724470decc6SDave Kleikamp * now refile the buffer on our BJ_Forget list so that
1725470decc6SDave Kleikamp * we know to remove the checkpoint after we commit.
1726470decc6SDave Kleikamp */
1727470decc6SDave Kleikamp
17280bfea811STheodore Ts'o spin_lock(&journal->j_list_lock);
1729470decc6SDave Kleikamp if (jh->b_cp_transaction) {
1730f7f4bccbSMingming Cao __jbd2_journal_temp_unlink_buffer(jh);
1731f7f4bccbSMingming Cao __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
1732470decc6SDave Kleikamp } else {
1733f7f4bccbSMingming Cao __jbd2_journal_unfile_buffer(jh);
173493108ebbSJan Kara jbd2_journal_put_journal_head(jh);
1735470decc6SDave Kleikamp }
17360bfea811STheodore Ts'o spin_unlock(&journal->j_list_lock);
1737470decc6SDave Kleikamp } else if (jh->b_transaction) {
1738470decc6SDave Kleikamp J_ASSERT_JH(jh, (jh->b_transaction ==
1739470decc6SDave Kleikamp journal->j_committing_transaction));
1740470decc6SDave Kleikamp /* However, if the buffer is still owned by a prior
1741470decc6SDave Kleikamp * (committing) transaction, we can't drop it yet... */
1742470decc6SDave Kleikamp JBUFFER_TRACE(jh, "belongs to older transaction");
1743904cdbd4Szhangyi (F) /* ... but we CAN drop it from the new transaction through
1744904cdbd4Szhangyi (F) * marking the buffer as freed and set j_next_transaction to
1745904cdbd4Szhangyi (F) * the new transaction, so that not only the commit code
1746904cdbd4Szhangyi (F) * knows it should clear dirty bits when it is done with the
1747904cdbd4Szhangyi (F) * buffer, but also the buffer can be checkpointed only
1748904cdbd4Szhangyi (F) * after the new transaction commits. */
1749470decc6SDave Kleikamp
1750904cdbd4Szhangyi (F) set_buffer_freed(bh);
1751904cdbd4Szhangyi (F)
1752904cdbd4Szhangyi (F) if (!jh->b_next_transaction) {
17530bfea811STheodore Ts'o spin_lock(&journal->j_list_lock);
1754904cdbd4Szhangyi (F) jh->b_next_transaction = transaction;
17550bfea811STheodore Ts'o spin_unlock(&journal->j_list_lock);
1756904cdbd4Szhangyi (F) } else {
1757904cdbd4Szhangyi (F) J_ASSERT(jh->b_next_transaction == transaction);
17581dfc3220SJosef Bacik
17591dfc3220SJosef Bacik /*
17601dfc3220SJosef Bacik * only drop a reference if this transaction modified
17611dfc3220SJosef Bacik * the buffer
17621dfc3220SJosef Bacik */
17631dfc3220SJosef Bacik if (was_modified)
1764470decc6SDave Kleikamp drop_reserve = 1;
1765470decc6SDave Kleikamp }
176659759926Szhangyi (F) } else {
176759759926Szhangyi (F) /*
176859759926Szhangyi (F) * Finally, if the buffer is not belongs to any
176959759926Szhangyi (F) * transaction, we can just drop it now if it has no
177059759926Szhangyi (F) * checkpoint.
177159759926Szhangyi (F) */
177259759926Szhangyi (F) spin_lock(&journal->j_list_lock);
177359759926Szhangyi (F) if (!jh->b_cp_transaction) {
177459759926Szhangyi (F) JBUFFER_TRACE(jh, "belongs to none transaction");
177559759926Szhangyi (F) spin_unlock(&journal->j_list_lock);
17762e710ff0SJan Kara goto drop;
1777470decc6SDave Kleikamp }
1778470decc6SDave Kleikamp
177959759926Szhangyi (F) /*
178059759926Szhangyi (F) * Otherwise, if the buffer has been written to disk,
178159759926Szhangyi (F) * it is safe to remove the checkpoint and drop it.
178259759926Szhangyi (F) */
178346f881b5SZhang Yi if (jbd2_journal_try_remove_checkpoint(jh) >= 0) {
178459759926Szhangyi (F) spin_unlock(&journal->j_list_lock);
17852e710ff0SJan Kara goto drop;
178659759926Szhangyi (F) }
178759759926Szhangyi (F)
178859759926Szhangyi (F) /*
178959759926Szhangyi (F) * The buffer is still not written to disk, we should
179059759926Szhangyi (F) * attach this buffer to current transaction so that the
179159759926Szhangyi (F) * buffer can be checkpointed only after the current
179259759926Szhangyi (F) * transaction commits.
179359759926Szhangyi (F) */
179459759926Szhangyi (F) clear_buffer_dirty(bh);
179559759926Szhangyi (F) __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
179659759926Szhangyi (F) spin_unlock(&journal->j_list_lock);
179759759926Szhangyi (F) }
1798470decc6SDave Kleikamp drop:
1799470decc6SDave Kleikamp __brelse(bh);
180046417064SThomas Gleixner spin_unlock(&jh->b_state_lock);
180146417064SThomas Gleixner jbd2_journal_put_journal_head(jh);
1802470decc6SDave Kleikamp if (drop_reserve) {
1803470decc6SDave Kleikamp /* no need to reserve log space for this block -bzzz */
1804933f1c1eSJan Kara handle->h_total_credits++;
1805470decc6SDave Kleikamp }
1806470decc6SDave Kleikamp return err;
1807470decc6SDave Kleikamp }
1808470decc6SDave Kleikamp
1809470decc6SDave Kleikamp /**
18102bf31d94SMauro Carvalho Chehab * jbd2_journal_stop() - complete a transaction
1811bd7ced98SMasanari Iida * @handle: transaction to complete.
1812470decc6SDave Kleikamp *
1813470decc6SDave Kleikamp * All done for a particular handle.
1814470decc6SDave Kleikamp *
1815470decc6SDave Kleikamp * There is not much action needed here. We just return any remaining
1816470decc6SDave Kleikamp * buffer credits to the transaction and remove the handle. The only
1817470decc6SDave Kleikamp * complication is that we need to start a commit operation if the
1818470decc6SDave Kleikamp * filesystem is marked for synchronous update.
1819470decc6SDave Kleikamp *
1820f7f4bccbSMingming Cao * jbd2_journal_stop itself will not usually return an error, but it may
1821470decc6SDave Kleikamp * do so in unusual circumstances. In particular, expect it to
1822f7f4bccbSMingming Cao * return -EIO if a jbd2_journal_abort has been executed since the
1823470decc6SDave Kleikamp * transaction began.
1824470decc6SDave Kleikamp */
jbd2_journal_stop(handle_t * handle)1825f7f4bccbSMingming Cao int jbd2_journal_stop(handle_t *handle)
1826470decc6SDave Kleikamp {
1827470decc6SDave Kleikamp transaction_t *transaction = handle->h_transaction;
182841a5b913STheodore Ts'o journal_t *journal;
182941a5b913STheodore Ts'o int err = 0, wait_for_commit = 0;
1830a51dca9cSTheodore Ts'o tid_t tid;
1831470decc6SDave Kleikamp pid_t pid;
1832470decc6SDave Kleikamp
18339d506594SLukas Czerner if (--handle->h_ref > 0) {
1834cb3b3bf2SJan Kara jbd2_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
18359d506594SLukas Czerner handle->h_ref);
1836dfaf5ffdSJan Kara if (is_handle_aborted(handle))
1837dfaf5ffdSJan Kara return -EIO;
1838dfaf5ffdSJan Kara return 0;
1839dfaf5ffdSJan Kara }
1840dfaf5ffdSJan Kara if (!transaction) {
1841dfaf5ffdSJan Kara /*
1842dfaf5ffdSJan Kara * Handle is already detached from the transaction so there is
1843dfaf5ffdSJan Kara * nothing to do other than free the handle.
1844dfaf5ffdSJan Kara */
1845ec8b6f60SJan Kara memalloc_nofs_restore(handle->saved_alloc_context);
184641a5b913STheodore Ts'o goto free_and_exit;
18479d506594SLukas Czerner }
184841a5b913STheodore Ts'o journal = transaction->t_journal;
1849dfaf5ffdSJan Kara tid = transaction->t_tid;
1850470decc6SDave Kleikamp
1851470decc6SDave Kleikamp if (is_handle_aborted(handle))
1852470decc6SDave Kleikamp err = -EIO;
1853470decc6SDave Kleikamp
1854cb3b3bf2SJan Kara jbd2_debug(4, "Handle %p going down\n", handle);
1855343d9c28STheodore Ts'o trace_jbd2_handle_stats(journal->j_fs_dev->bd_dev,
1856dfaf5ffdSJan Kara tid, handle->h_type, handle->h_line_no,
1857343d9c28STheodore Ts'o jiffies - handle->h_start_jiffies,
1858343d9c28STheodore Ts'o handle->h_sync, handle->h_requested_credits,
1859343d9c28STheodore Ts'o (handle->h_requested_credits -
1860933f1c1eSJan Kara handle->h_total_credits));
1861470decc6SDave Kleikamp
1862470decc6SDave Kleikamp /*
1863470decc6SDave Kleikamp * Implement synchronous transaction batching. If the handle
1864470decc6SDave Kleikamp * was synchronous, don't force a commit immediately. Let's
1865e07f7183SJosef Bacik * yield and let another thread piggyback onto this
1866e07f7183SJosef Bacik * transaction. Keep doing that while new threads continue to
1867e07f7183SJosef Bacik * arrive. It doesn't cost much - we're about to run a commit
1868e07f7183SJosef Bacik * and sleep on IO anyway. Speeds up many-threaded, many-dir
1869e07f7183SJosef Bacik * operations by 30x or more...
1870470decc6SDave Kleikamp *
1871e07f7183SJosef Bacik * We try and optimize the sleep time against what the
1872e07f7183SJosef Bacik * underlying disk can do, instead of having a static sleep
1873e07f7183SJosef Bacik * time. This is useful for the case where our storage is so
1874e07f7183SJosef Bacik * fast that it is more optimal to go ahead and force a flush
1875e07f7183SJosef Bacik * and wait for the transaction to be committed than it is to
1876e07f7183SJosef Bacik * wait for an arbitrary amount of time for new writers to
1877e07f7183SJosef Bacik * join the transaction. We achieve this by measuring how
1878e07f7183SJosef Bacik * long it takes to commit a transaction, and compare it with
1879e07f7183SJosef Bacik * how long this transaction has been running, and if run time
1880e07f7183SJosef Bacik * < commit time then we sleep for the delta and commit. This
1881e07f7183SJosef Bacik * greatly helps super fast disks that would see slowdowns as
1882e07f7183SJosef Bacik * more threads started doing fsyncs.
1883e07f7183SJosef Bacik *
1884e07f7183SJosef Bacik * But don't do this if this process was the most recent one
1885e07f7183SJosef Bacik * to perform a synchronous write. We do this to detect the
1886e07f7183SJosef Bacik * case where a single process is doing a stream of sync
1887e07f7183SJosef Bacik * writes. No point in waiting for joiners in that case.
18885dd21424SEric Sandeen *
18895dd21424SEric Sandeen * Setting max_batch_time to 0 disables this completely.
1890470decc6SDave Kleikamp */
1891470decc6SDave Kleikamp pid = current->pid;
18925dd21424SEric Sandeen if (handle->h_sync && journal->j_last_sync_writer != pid &&
18935dd21424SEric Sandeen journal->j_max_batch_time) {
1894e07f7183SJosef Bacik u64 commit_time, trans_time;
1895e07f7183SJosef Bacik
1896470decc6SDave Kleikamp journal->j_last_sync_writer = pid;
1897e07f7183SJosef Bacik
1898a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock);
1899e07f7183SJosef Bacik commit_time = journal->j_average_commit_time;
1900a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock);
1901e07f7183SJosef Bacik
1902e07f7183SJosef Bacik trans_time = ktime_to_ns(ktime_sub(ktime_get(),
1903e07f7183SJosef Bacik transaction->t_start_time));
1904e07f7183SJosef Bacik
190530773840STheodore Ts'o commit_time = max_t(u64, commit_time,
190630773840STheodore Ts'o 1000*journal->j_min_batch_time);
1907e07f7183SJosef Bacik commit_time = min_t(u64, commit_time,
190830773840STheodore Ts'o 1000*journal->j_max_batch_time);
1909e07f7183SJosef Bacik
1910e07f7183SJosef Bacik if (trans_time < commit_time) {
1911e07f7183SJosef Bacik ktime_t expires = ktime_add_ns(ktime_get(),
1912e07f7183SJosef Bacik commit_time);
1913e07f7183SJosef Bacik set_current_state(TASK_UNINTERRUPTIBLE);
1914e07f7183SJosef Bacik schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1915e07f7183SJosef Bacik }
1916470decc6SDave Kleikamp }
1917470decc6SDave Kleikamp
19187058548cSTheodore Ts'o if (handle->h_sync)
19197058548cSTheodore Ts'o transaction->t_synchronous_commit = 1;
1920470decc6SDave Kleikamp
1921470decc6SDave Kleikamp /*
1922470decc6SDave Kleikamp * If the handle is marked SYNC, we need to set another commit
1923150549edSJan Kara * going! We also want to force a commit if the transaction is too
1924150549edSJan Kara * old now.
1925470decc6SDave Kleikamp */
1926470decc6SDave Kleikamp if (handle->h_sync ||
1927470decc6SDave Kleikamp time_after_eq(jiffies, transaction->t_expires)) {
1928470decc6SDave Kleikamp /* Do this even for aborted journals: an abort still
1929470decc6SDave Kleikamp * completes the commit thread, it just doesn't write
1930470decc6SDave Kleikamp * anything to disk. */
1931470decc6SDave Kleikamp
1932cb3b3bf2SJan Kara jbd2_debug(2, "transaction too old, requesting commit for "
1933470decc6SDave Kleikamp "handle %p\n", handle);
1934470decc6SDave Kleikamp /* This is non-blocking */
1935dfaf5ffdSJan Kara jbd2_log_start_commit(journal, tid);
1936470decc6SDave Kleikamp
1937470decc6SDave Kleikamp /*
1938f7f4bccbSMingming Cao * Special case: JBD2_SYNC synchronous updates require us
1939470decc6SDave Kleikamp * to wait for the commit to complete.
1940470decc6SDave Kleikamp */
1941470decc6SDave Kleikamp if (handle->h_sync && !(current->flags & PF_MEMALLOC))
1942a51dca9cSTheodore Ts'o wait_for_commit = 1;
1943470decc6SDave Kleikamp }
1944470decc6SDave Kleikamp
1945a51dca9cSTheodore Ts'o /*
1946ec8b6f60SJan Kara * Once stop_this_handle() drops t_updates, the transaction could start
1947ec8b6f60SJan Kara * committing on us and eventually disappear. So we must not
1948ec8b6f60SJan Kara * dereference transaction pointer again after calling
1949ec8b6f60SJan Kara * stop_this_handle().
1950a51dca9cSTheodore Ts'o */
1951ec8b6f60SJan Kara stop_this_handle(handle);
19527a4b188fSJan Kara
1953a51dca9cSTheodore Ts'o if (wait_for_commit)
1954a51dca9cSTheodore Ts'o err = jbd2_log_wait_commit(journal, tid);
1955a51dca9cSTheodore Ts'o
195641a5b913STheodore Ts'o free_and_exit:
1957ec8b6f60SJan Kara if (handle->h_rsv_handle)
1958ec8b6f60SJan Kara jbd2_free_handle(handle->h_rsv_handle);
1959af1e76d6SMingming Cao jbd2_free_handle(handle);
1960470decc6SDave Kleikamp return err;
1961470decc6SDave Kleikamp }
1962470decc6SDave Kleikamp
1963470decc6SDave Kleikamp /*
1964470decc6SDave Kleikamp *
1965470decc6SDave Kleikamp * List management code snippets: various functions for manipulating the
1966470decc6SDave Kleikamp * transaction buffer lists.
1967470decc6SDave Kleikamp *
1968470decc6SDave Kleikamp */
1969470decc6SDave Kleikamp
1970470decc6SDave Kleikamp /*
1971470decc6SDave Kleikamp * Append a buffer to a transaction list, given the transaction's list head
1972470decc6SDave Kleikamp * pointer.
1973470decc6SDave Kleikamp *
1974470decc6SDave Kleikamp * j_list_lock is held.
1975470decc6SDave Kleikamp *
197646417064SThomas Gleixner * jh->b_state_lock is held.
1977470decc6SDave Kleikamp */
1978470decc6SDave Kleikamp
1979470decc6SDave Kleikamp static inline void
__blist_add_buffer(struct journal_head ** list,struct journal_head * jh)1980470decc6SDave Kleikamp __blist_add_buffer(struct journal_head **list, struct journal_head *jh)
1981470decc6SDave Kleikamp {
1982470decc6SDave Kleikamp if (!*list) {
1983470decc6SDave Kleikamp jh->b_tnext = jh->b_tprev = jh;
1984470decc6SDave Kleikamp *list = jh;
1985470decc6SDave Kleikamp } else {
1986470decc6SDave Kleikamp /* Insert at the tail of the list to preserve order */
1987470decc6SDave Kleikamp struct journal_head *first = *list, *last = first->b_tprev;
1988470decc6SDave Kleikamp jh->b_tprev = last;
1989470decc6SDave Kleikamp jh->b_tnext = first;
1990470decc6SDave Kleikamp last->b_tnext = first->b_tprev = jh;
1991470decc6SDave Kleikamp }
1992470decc6SDave Kleikamp }
1993470decc6SDave Kleikamp
1994470decc6SDave Kleikamp /*
1995470decc6SDave Kleikamp * Remove a buffer from a transaction list, given the transaction's list
1996470decc6SDave Kleikamp * head pointer.
1997470decc6SDave Kleikamp *
1998470decc6SDave Kleikamp * Called with j_list_lock held, and the journal may not be locked.
1999470decc6SDave Kleikamp *
200046417064SThomas Gleixner * jh->b_state_lock is held.
2001470decc6SDave Kleikamp */
2002470decc6SDave Kleikamp
2003470decc6SDave Kleikamp static inline void
__blist_del_buffer(struct journal_head ** list,struct journal_head * jh)2004470decc6SDave Kleikamp __blist_del_buffer(struct journal_head **list, struct journal_head *jh)
2005470decc6SDave Kleikamp {
2006470decc6SDave Kleikamp if (*list == jh) {
2007470decc6SDave Kleikamp *list = jh->b_tnext;
2008470decc6SDave Kleikamp if (*list == jh)
2009470decc6SDave Kleikamp *list = NULL;
2010470decc6SDave Kleikamp }
2011470decc6SDave Kleikamp jh->b_tprev->b_tnext = jh->b_tnext;
2012470decc6SDave Kleikamp jh->b_tnext->b_tprev = jh->b_tprev;
2013470decc6SDave Kleikamp }
2014470decc6SDave Kleikamp
2015470decc6SDave Kleikamp /*
2016470decc6SDave Kleikamp * Remove a buffer from the appropriate transaction list.
2017470decc6SDave Kleikamp *
2018470decc6SDave Kleikamp * Note that this function can *change* the value of
2019f5113effSJan Kara * bh->b_transaction->t_buffers, t_forget, t_shadow_list, t_log_list or
2020f5113effSJan Kara * t_reserved_list. If the caller is holding onto a copy of one of these
2021f5113effSJan Kara * pointers, it could go bad. Generally the caller needs to re-read the
2022f5113effSJan Kara * pointer from the transaction_t.
2023470decc6SDave Kleikamp *
20245bebccf9SJan Kara * Called under j_list_lock.
2025470decc6SDave Kleikamp */
__jbd2_journal_temp_unlink_buffer(struct journal_head * jh)20265bebccf9SJan Kara static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh)
2027470decc6SDave Kleikamp {
2028470decc6SDave Kleikamp struct journal_head **list = NULL;
2029470decc6SDave Kleikamp transaction_t *transaction;
2030470decc6SDave Kleikamp struct buffer_head *bh = jh2bh(jh);
2031470decc6SDave Kleikamp
203246417064SThomas Gleixner lockdep_assert_held(&jh->b_state_lock);
2033470decc6SDave Kleikamp transaction = jh->b_transaction;
2034470decc6SDave Kleikamp if (transaction)
2035470decc6SDave Kleikamp assert_spin_locked(&transaction->t_journal->j_list_lock);
2036470decc6SDave Kleikamp
2037470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
2038470decc6SDave Kleikamp if (jh->b_jlist != BJ_None)
20394019191bSMingming Cao J_ASSERT_JH(jh, transaction != NULL);
2040470decc6SDave Kleikamp
2041470decc6SDave Kleikamp switch (jh->b_jlist) {
2042470decc6SDave Kleikamp case BJ_None:
2043470decc6SDave Kleikamp return;
2044470decc6SDave Kleikamp case BJ_Metadata:
2045470decc6SDave Kleikamp transaction->t_nr_buffers--;
2046470decc6SDave Kleikamp J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
2047470decc6SDave Kleikamp list = &transaction->t_buffers;
2048470decc6SDave Kleikamp break;
2049470decc6SDave Kleikamp case BJ_Forget:
2050470decc6SDave Kleikamp list = &transaction->t_forget;
2051470decc6SDave Kleikamp break;
2052470decc6SDave Kleikamp case BJ_Shadow:
2053470decc6SDave Kleikamp list = &transaction->t_shadow_list;
2054470decc6SDave Kleikamp break;
2055470decc6SDave Kleikamp case BJ_Reserved:
2056470decc6SDave Kleikamp list = &transaction->t_reserved_list;
2057470decc6SDave Kleikamp break;
2058470decc6SDave Kleikamp }
2059470decc6SDave Kleikamp
2060470decc6SDave Kleikamp __blist_del_buffer(list, jh);
2061470decc6SDave Kleikamp jh->b_jlist = BJ_None;
2062e112666bSTheodore Ts'o if (transaction && is_journal_aborted(transaction->t_journal))
2063e112666bSTheodore Ts'o clear_buffer_jbddirty(bh);
2064e112666bSTheodore Ts'o else if (test_clear_buffer_jbddirty(bh))
2065470decc6SDave Kleikamp mark_buffer_dirty(bh); /* Expose it to the VM */
2066470decc6SDave Kleikamp }
2067470decc6SDave Kleikamp
2068de1b7941SJan Kara /*
206993108ebbSJan Kara * Remove buffer from all transactions. The caller is responsible for dropping
207093108ebbSJan Kara * the jh reference that belonged to the transaction.
2071de1b7941SJan Kara *
2072de1b7941SJan Kara * Called with bh_state lock and j_list_lock
2073de1b7941SJan Kara */
__jbd2_journal_unfile_buffer(struct journal_head * jh)2074de1b7941SJan Kara static void __jbd2_journal_unfile_buffer(struct journal_head *jh)
2075470decc6SDave Kleikamp {
207624dc9864SLukas Czerner J_ASSERT_JH(jh, jh->b_transaction != NULL);
207724dc9864SLukas Czerner J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
207824dc9864SLukas Czerner
2079f7f4bccbSMingming Cao __jbd2_journal_temp_unlink_buffer(jh);
2080470decc6SDave Kleikamp jh->b_transaction = NULL;
2081470decc6SDave Kleikamp }
2082470decc6SDave Kleikamp
jbd2_journal_unfile_buffer(journal_t * journal,struct journal_head * jh)2083f7f4bccbSMingming Cao void jbd2_journal_unfile_buffer(journal_t *journal, struct journal_head *jh)
2084470decc6SDave Kleikamp {
2085de1b7941SJan Kara struct buffer_head *bh = jh2bh(jh);
2086de1b7941SJan Kara
2087de1b7941SJan Kara /* Get reference so that buffer cannot be freed before we unlock it */
2088de1b7941SJan Kara get_bh(bh);
208946417064SThomas Gleixner spin_lock(&jh->b_state_lock);
2090470decc6SDave Kleikamp spin_lock(&journal->j_list_lock);
2091f7f4bccbSMingming Cao __jbd2_journal_unfile_buffer(jh);
2092470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock);
209346417064SThomas Gleixner spin_unlock(&jh->b_state_lock);
209493108ebbSJan Kara jbd2_journal_put_journal_head(jh);
2095de1b7941SJan Kara __brelse(bh);
2096470decc6SDave Kleikamp }
2097470decc6SDave Kleikamp
2098470decc6SDave Kleikamp /**
20992bf31d94SMauro Carvalho Chehab * jbd2_journal_try_to_free_buffers() - try to free page buffers.
2100470decc6SDave Kleikamp * @journal: journal for operation
21014f5bf127SYang Li * @folio: Folio to detach data from.
2102470decc6SDave Kleikamp *
2103470decc6SDave Kleikamp * For all the buffers on this page,
2104470decc6SDave Kleikamp * if they are fully written out ordered data, move them onto BUF_CLEAN
2105470decc6SDave Kleikamp * so try_to_free_buffers() can reap them.
2106470decc6SDave Kleikamp *
2107470decc6SDave Kleikamp * This function returns non-zero if we wish try_to_free_buffers()
2108470decc6SDave Kleikamp * to be called. We do this if the page is releasable by try_to_free_buffers().
2109470decc6SDave Kleikamp * We also do it if the page has locked or dirty buffers and the caller wants
2110470decc6SDave Kleikamp * us to perform sync or async writeout.
2111470decc6SDave Kleikamp *
2112470decc6SDave Kleikamp * This complicates JBD locking somewhat. We aren't protected by the
2113470decc6SDave Kleikamp * BKL here. We wish to remove the buffer from its committing or
2114f7f4bccbSMingming Cao * running transaction's ->t_datalist via __jbd2_journal_unfile_buffer.
2115470decc6SDave Kleikamp *
2116470decc6SDave Kleikamp * This may *change* the value of transaction_t->t_datalist, so anyone
2117470decc6SDave Kleikamp * who looks at t_datalist needs to lock against this function.
2118470decc6SDave Kleikamp *
2119f7f4bccbSMingming Cao * Even worse, someone may be doing a jbd2_journal_dirty_data on this
2120f7f4bccbSMingming Cao * buffer. So we need to lock against that. jbd2_journal_dirty_data()
2121470decc6SDave Kleikamp * will come out of the lock with the buffer dirty, which makes it
2122470decc6SDave Kleikamp * ineligible for release here.
2123470decc6SDave Kleikamp *
2124470decc6SDave Kleikamp * Who else is affected by this? hmm... Really the only contender
2125470decc6SDave Kleikamp * is do_get_write_access() - it could be looking at the buffer while
2126470decc6SDave Kleikamp * journal_try_to_free_buffer() is changing its state. But that
2127470decc6SDave Kleikamp * cannot happen because we never reallocate freed data as metadata
2128470decc6SDave Kleikamp * while the data is part of a transaction. Yes?
2129530576bbSMingming Cao *
2130c56a6eb0SMatthew Wilcox (Oracle) * Return false on failure, true on success
2131470decc6SDave Kleikamp */
jbd2_journal_try_to_free_buffers(journal_t * journal,struct folio * folio)2132c56a6eb0SMatthew Wilcox (Oracle) bool jbd2_journal_try_to_free_buffers(journal_t *journal, struct folio *folio)
2133470decc6SDave Kleikamp {
2134470decc6SDave Kleikamp struct buffer_head *head;
2135470decc6SDave Kleikamp struct buffer_head *bh;
2136c56a6eb0SMatthew Wilcox (Oracle) bool ret = false;
2137470decc6SDave Kleikamp
2138c56a6eb0SMatthew Wilcox (Oracle) J_ASSERT(folio_test_locked(folio));
2139470decc6SDave Kleikamp
2140c56a6eb0SMatthew Wilcox (Oracle) head = folio_buffers(folio);
2141470decc6SDave Kleikamp bh = head;
2142470decc6SDave Kleikamp do {
2143470decc6SDave Kleikamp struct journal_head *jh;
2144470decc6SDave Kleikamp
2145470decc6SDave Kleikamp /*
2146470decc6SDave Kleikamp * We take our own ref against the journal_head here to avoid
2147470decc6SDave Kleikamp * having to add tons of locking around each instance of
2148530576bbSMingming Cao * jbd2_journal_put_journal_head().
2149470decc6SDave Kleikamp */
2150f7f4bccbSMingming Cao jh = jbd2_journal_grab_journal_head(bh);
2151470decc6SDave Kleikamp if (!jh)
2152470decc6SDave Kleikamp continue;
2153470decc6SDave Kleikamp
215446417064SThomas Gleixner spin_lock(&jh->b_state_lock);
21553c55097cSZhang Yi if (!jh->b_transaction && !jh->b_next_transaction) {
21563c55097cSZhang Yi spin_lock(&journal->j_list_lock);
21573c55097cSZhang Yi /* Remove written-back checkpointed metadata buffer */
21583c55097cSZhang Yi if (jh->b_cp_transaction != NULL)
21593c55097cSZhang Yi jbd2_journal_try_remove_checkpoint(jh);
21603c55097cSZhang Yi spin_unlock(&journal->j_list_lock);
21613c55097cSZhang Yi }
216246417064SThomas Gleixner spin_unlock(&jh->b_state_lock);
2163f7f4bccbSMingming Cao jbd2_journal_put_journal_head(jh);
2164470decc6SDave Kleikamp if (buffer_jbd(bh))
2165470decc6SDave Kleikamp goto busy;
2166470decc6SDave Kleikamp } while ((bh = bh->b_this_page) != head);
2167530576bbSMingming Cao
216868189fefSMatthew Wilcox (Oracle) ret = try_to_free_buffers(folio);
2169470decc6SDave Kleikamp busy:
2170470decc6SDave Kleikamp return ret;
2171470decc6SDave Kleikamp }
2172470decc6SDave Kleikamp
2173470decc6SDave Kleikamp /*
2174470decc6SDave Kleikamp * This buffer is no longer needed. If it is on an older transaction's
2175470decc6SDave Kleikamp * checkpoint list we need to record it on this transaction's forget list
2176470decc6SDave Kleikamp * to pin this buffer (and hence its checkpointing transaction) down until
2177470decc6SDave Kleikamp * this transaction commits. If the buffer isn't on a checkpoint list, we
2178470decc6SDave Kleikamp * release it.
2179470decc6SDave Kleikamp * Returns non-zero if JBD no longer has an interest in the buffer.
2180470decc6SDave Kleikamp *
2181470decc6SDave Kleikamp * Called under j_list_lock.
2182470decc6SDave Kleikamp *
218346417064SThomas Gleixner * Called under jh->b_state_lock.
2184470decc6SDave Kleikamp */
__dispose_buffer(struct journal_head * jh,transaction_t * transaction)2185470decc6SDave Kleikamp static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
2186470decc6SDave Kleikamp {
2187470decc6SDave Kleikamp int may_free = 1;
2188470decc6SDave Kleikamp struct buffer_head *bh = jh2bh(jh);
2189470decc6SDave Kleikamp
2190470decc6SDave Kleikamp if (jh->b_cp_transaction) {
2191470decc6SDave Kleikamp JBUFFER_TRACE(jh, "on running+cp transaction");
2192de1b7941SJan Kara __jbd2_journal_temp_unlink_buffer(jh);
2193f91d1d04SJan Kara /*
2194f91d1d04SJan Kara * We don't want to write the buffer anymore, clear the
2195f91d1d04SJan Kara * bit so that we don't confuse checks in
2196f91d1d04SJan Kara * __journal_file_buffer
2197f91d1d04SJan Kara */
2198f91d1d04SJan Kara clear_buffer_dirty(bh);
2199f7f4bccbSMingming Cao __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
2200470decc6SDave Kleikamp may_free = 0;
2201470decc6SDave Kleikamp } else {
2202470decc6SDave Kleikamp JBUFFER_TRACE(jh, "on running transaction");
2203de1b7941SJan Kara __jbd2_journal_unfile_buffer(jh);
220493108ebbSJan Kara jbd2_journal_put_journal_head(jh);
2205470decc6SDave Kleikamp }
2206470decc6SDave Kleikamp return may_free;
2207470decc6SDave Kleikamp }
2208470decc6SDave Kleikamp
2209470decc6SDave Kleikamp /*
2210ccd16945SMatthew Wilcox (Oracle) * jbd2_journal_invalidate_folio
2211470decc6SDave Kleikamp *
2212470decc6SDave Kleikamp * This code is tricky. It has a number of cases to deal with.
2213470decc6SDave Kleikamp *
2214470decc6SDave Kleikamp * There are two invariants which this code relies on:
2215470decc6SDave Kleikamp *
2216ccd16945SMatthew Wilcox (Oracle) * i_size must be updated on disk before we start calling invalidate_folio
2217ccd16945SMatthew Wilcox (Oracle) * on the data.
2218470decc6SDave Kleikamp *
2219470decc6SDave Kleikamp * This is done in ext3 by defining an ext3_setattr method which
2220470decc6SDave Kleikamp * updates i_size before truncate gets going. By maintaining this
2221470decc6SDave Kleikamp * invariant, we can be sure that it is safe to throw away any buffers
2222470decc6SDave Kleikamp * attached to the current transaction: once the transaction commits,
2223470decc6SDave Kleikamp * we know that the data will not be needed.
2224470decc6SDave Kleikamp *
2225470decc6SDave Kleikamp * Note however that we can *not* throw away data belonging to the
2226470decc6SDave Kleikamp * previous, committing transaction!
2227470decc6SDave Kleikamp *
2228470decc6SDave Kleikamp * Any disk blocks which *are* part of the previous, committing
2229470decc6SDave Kleikamp * transaction (and which therefore cannot be discarded immediately) are
2230470decc6SDave Kleikamp * not going to be reused in the new running transaction
2231470decc6SDave Kleikamp *
2232470decc6SDave Kleikamp * The bitmap committed_data images guarantee this: any block which is
2233470decc6SDave Kleikamp * allocated in one transaction and removed in the next will be marked
2234470decc6SDave Kleikamp * as in-use in the committed_data bitmap, so cannot be reused until
2235470decc6SDave Kleikamp * the next transaction to delete the block commits. This means that
2236470decc6SDave Kleikamp * leaving committing buffers dirty is quite safe: the disk blocks
2237470decc6SDave Kleikamp * cannot be reallocated to a different file and so buffer aliasing is
2238470decc6SDave Kleikamp * not possible.
2239470decc6SDave Kleikamp *
2240470decc6SDave Kleikamp *
2241470decc6SDave Kleikamp * The above applies mainly to ordered data mode. In writeback mode we
2242470decc6SDave Kleikamp * don't make guarantees about the order in which data hits disk --- in
2243470decc6SDave Kleikamp * particular we don't guarantee that new dirty data is flushed before
2244470decc6SDave Kleikamp * transaction commit --- so it is always safe just to discard data
2245470decc6SDave Kleikamp * immediately in that mode. --sct
2246470decc6SDave Kleikamp */
2247470decc6SDave Kleikamp
2248470decc6SDave Kleikamp /*
2249470decc6SDave Kleikamp * The journal_unmap_buffer helper function returns zero if the buffer
2250470decc6SDave Kleikamp * concerned remains pinned as an anonymous buffer belonging to an older
2251470decc6SDave Kleikamp * transaction.
2252470decc6SDave Kleikamp *
2253470decc6SDave Kleikamp * We're outside-transaction here. Either or both of j_running_transaction
2254470decc6SDave Kleikamp * and j_committing_transaction may be NULL.
2255470decc6SDave Kleikamp */
journal_unmap_buffer(journal_t * journal,struct buffer_head * bh,int partial_page)2256b794e7a6SJan Kara static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh,
2257b794e7a6SJan Kara int partial_page)
2258470decc6SDave Kleikamp {
2259470decc6SDave Kleikamp transaction_t *transaction;
2260470decc6SDave Kleikamp struct journal_head *jh;
2261470decc6SDave Kleikamp int may_free = 1;
2262470decc6SDave Kleikamp
2263470decc6SDave Kleikamp BUFFER_TRACE(bh, "entry");
2264470decc6SDave Kleikamp
2265470decc6SDave Kleikamp /*
2266470decc6SDave Kleikamp * It is safe to proceed here without the j_list_lock because the
2267470decc6SDave Kleikamp * buffers cannot be stolen by try_to_free_buffers as long as we are
2268470decc6SDave Kleikamp * holding the page lock. --sct
2269470decc6SDave Kleikamp */
2270470decc6SDave Kleikamp
2271d84560f7SThomas Gleixner jh = jbd2_journal_grab_journal_head(bh);
2272d84560f7SThomas Gleixner if (!jh)
2273470decc6SDave Kleikamp goto zap_buffer_unlocked;
2274470decc6SDave Kleikamp
227587c89c23SJan Kara /* OK, we have data buffer in journaled mode */
2276a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock);
227746417064SThomas Gleixner spin_lock(&jh->b_state_lock);
2278470decc6SDave Kleikamp spin_lock(&journal->j_list_lock);
2279470decc6SDave Kleikamp
2280ba869023Sdingdinghua /*
2281ba869023Sdingdinghua * We cannot remove the buffer from checkpoint lists until the
2282ba869023Sdingdinghua * transaction adding inode to orphan list (let's call it T)
2283ba869023Sdingdinghua * is committed. Otherwise if the transaction changing the
2284ba869023Sdingdinghua * buffer would be cleaned from the journal before T is
2285ba869023Sdingdinghua * committed, a crash will cause that the correct contents of
2286ba869023Sdingdinghua * the buffer will be lost. On the other hand we have to
2287ba869023Sdingdinghua * clear the buffer dirty bit at latest at the moment when the
2288ba869023Sdingdinghua * transaction marking the buffer as freed in the filesystem
2289ba869023Sdingdinghua * structures is committed because from that moment on the
2290b794e7a6SJan Kara * block can be reallocated and used by a different page.
2291ba869023Sdingdinghua * Since the block hasn't been freed yet but the inode has
2292ba869023Sdingdinghua * already been added to orphan list, it is safe for us to add
2293ba869023Sdingdinghua * the buffer to BJ_Forget list of the newest transaction.
2294b794e7a6SJan Kara *
2295b794e7a6SJan Kara * Also we have to clear buffer_mapped flag of a truncated buffer
2296b794e7a6SJan Kara * because the buffer_head may be attached to the page straddling
2297b794e7a6SJan Kara * i_size (can happen only when blocksize < pagesize) and thus the
2298b794e7a6SJan Kara * buffer_head can be reused when the file is extended again. So we end
2299b794e7a6SJan Kara * up keeping around invalidated buffers attached to transactions'
2300b794e7a6SJan Kara * BJ_Forget list just to stop checkpointing code from cleaning up
2301b794e7a6SJan Kara * the transaction this buffer was modified in.
2302ba869023Sdingdinghua */
2303470decc6SDave Kleikamp transaction = jh->b_transaction;
2304470decc6SDave Kleikamp if (transaction == NULL) {
2305470decc6SDave Kleikamp /* First case: not on any transaction. If it
2306470decc6SDave Kleikamp * has no checkpoint link, then we can zap it:
2307470decc6SDave Kleikamp * it's a writeback-mode buffer so we don't care
2308470decc6SDave Kleikamp * if it hits disk safely. */
2309470decc6SDave Kleikamp if (!jh->b_cp_transaction) {
2310470decc6SDave Kleikamp JBUFFER_TRACE(jh, "not on any transaction: zap");
2311470decc6SDave Kleikamp goto zap_buffer;
2312470decc6SDave Kleikamp }
2313470decc6SDave Kleikamp
2314470decc6SDave Kleikamp if (!buffer_dirty(bh)) {
2315470decc6SDave Kleikamp /* bdflush has written it. We can drop it now */
2316bc23f0c8SJan Kara __jbd2_journal_remove_checkpoint(jh);
2317470decc6SDave Kleikamp goto zap_buffer;
2318470decc6SDave Kleikamp }
2319470decc6SDave Kleikamp
2320470decc6SDave Kleikamp /* OK, it must be in the journal but still not
2321470decc6SDave Kleikamp * written fully to disk: it's metadata or
2322470decc6SDave Kleikamp * journaled data... */
2323470decc6SDave Kleikamp
2324470decc6SDave Kleikamp if (journal->j_running_transaction) {
2325470decc6SDave Kleikamp /* ... and once the current transaction has
2326470decc6SDave Kleikamp * committed, the buffer won't be needed any
2327470decc6SDave Kleikamp * longer. */
2328470decc6SDave Kleikamp JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
2329b794e7a6SJan Kara may_free = __dispose_buffer(jh,
2330470decc6SDave Kleikamp journal->j_running_transaction);
2331b794e7a6SJan Kara goto zap_buffer;
2332470decc6SDave Kleikamp } else {
2333470decc6SDave Kleikamp /* There is no currently-running transaction. So the
2334470decc6SDave Kleikamp * orphan record which we wrote for this file must have
2335470decc6SDave Kleikamp * passed into commit. We must attach this buffer to
2336470decc6SDave Kleikamp * the committing transaction, if it exists. */
2337470decc6SDave Kleikamp if (journal->j_committing_transaction) {
2338470decc6SDave Kleikamp JBUFFER_TRACE(jh, "give to committing trans");
2339b794e7a6SJan Kara may_free = __dispose_buffer(jh,
2340470decc6SDave Kleikamp journal->j_committing_transaction);
2341b794e7a6SJan Kara goto zap_buffer;
2342470decc6SDave Kleikamp } else {
2343470decc6SDave Kleikamp /* The orphan record's transaction has
2344470decc6SDave Kleikamp * committed. We can cleanse this buffer */
2345470decc6SDave Kleikamp clear_buffer_jbddirty(bh);
2346bc23f0c8SJan Kara __jbd2_journal_remove_checkpoint(jh);
2347470decc6SDave Kleikamp goto zap_buffer;
2348470decc6SDave Kleikamp }
2349470decc6SDave Kleikamp }
2350470decc6SDave Kleikamp } else if (transaction == journal->j_committing_transaction) {
23519b57988dSEric Sandeen JBUFFER_TRACE(jh, "on committing transaction");
2352470decc6SDave Kleikamp /*
2353ba869023Sdingdinghua * The buffer is committing, we simply cannot touch
2354b794e7a6SJan Kara * it. If the page is straddling i_size we have to wait
2355b794e7a6SJan Kara * for commit and try again.
2356b794e7a6SJan Kara */
2357b794e7a6SJan Kara if (partial_page) {
2358b794e7a6SJan Kara spin_unlock(&journal->j_list_lock);
235946417064SThomas Gleixner spin_unlock(&jh->b_state_lock);
2360b794e7a6SJan Kara write_unlock(&journal->j_state_lock);
236146417064SThomas Gleixner jbd2_journal_put_journal_head(jh);
2362bd159398SJan Kara /* Already zapped buffer? Nothing to do... */
2363bd159398SJan Kara if (!bh->b_bdev)
2364bd159398SJan Kara return 0;
236553e87268SJan Kara return -EBUSY;
2366b794e7a6SJan Kara }
2367b794e7a6SJan Kara /*
23686a66a7deSzhangyi (F) * OK, buffer won't be reachable after truncate. We just clear
23696a66a7deSzhangyi (F) * b_modified to not confuse transaction credit accounting, and
23706a66a7deSzhangyi (F) * set j_next_transaction to the running transaction (if there
23716a66a7deSzhangyi (F) * is one) and mark buffer as freed so that commit code knows
23726a66a7deSzhangyi (F) * it should clear dirty bits when it is done with the buffer.
2373ba869023Sdingdinghua */
2374470decc6SDave Kleikamp set_buffer_freed(bh);
2375ba869023Sdingdinghua if (journal->j_running_transaction && buffer_jbddirty(bh))
2376ba869023Sdingdinghua jh->b_next_transaction = journal->j_running_transaction;
23776a66a7deSzhangyi (F) jh->b_modified = 0;
2378470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock);
237946417064SThomas Gleixner spin_unlock(&jh->b_state_lock);
2380a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock);
238146417064SThomas Gleixner jbd2_journal_put_journal_head(jh);
2382470decc6SDave Kleikamp return 0;
2383470decc6SDave Kleikamp } else {
2384470decc6SDave Kleikamp /* Good, the buffer belongs to the running transaction.
2385470decc6SDave Kleikamp * We are writing our own transaction's data, not any
2386470decc6SDave Kleikamp * previous one's, so it is safe to throw it away
2387470decc6SDave Kleikamp * (remember that we expect the filesystem to have set
2388470decc6SDave Kleikamp * i_size already for this truncate so recovery will not
2389470decc6SDave Kleikamp * expose the disk blocks we are discarding here.) */
2390470decc6SDave Kleikamp J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
23919b57988dSEric Sandeen JBUFFER_TRACE(jh, "on running transaction");
2392470decc6SDave Kleikamp may_free = __dispose_buffer(jh, transaction);
2393470decc6SDave Kleikamp }
2394470decc6SDave Kleikamp
2395470decc6SDave Kleikamp zap_buffer:
2396b794e7a6SJan Kara /*
2397b794e7a6SJan Kara * This is tricky. Although the buffer is truncated, it may be reused
2398b794e7a6SJan Kara * if blocksize < pagesize and it is attached to the page straddling
2399b794e7a6SJan Kara * EOF. Since the buffer might have been added to BJ_Forget list of the
2400b794e7a6SJan Kara * running transaction, journal_get_write_access() won't clear
2401b794e7a6SJan Kara * b_modified and credit accounting gets confused. So clear b_modified
2402b794e7a6SJan Kara * here.
2403b794e7a6SJan Kara */
2404b794e7a6SJan Kara jh->b_modified = 0;
2405470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock);
240646417064SThomas Gleixner spin_unlock(&jh->b_state_lock);
2407a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock);
240846417064SThomas Gleixner jbd2_journal_put_journal_head(jh);
2409470decc6SDave Kleikamp zap_buffer_unlocked:
2410470decc6SDave Kleikamp clear_buffer_dirty(bh);
2411470decc6SDave Kleikamp J_ASSERT_BH(bh, !buffer_jbddirty(bh));
2412470decc6SDave Kleikamp clear_buffer_mapped(bh);
2413470decc6SDave Kleikamp clear_buffer_req(bh);
2414470decc6SDave Kleikamp clear_buffer_new(bh);
241515291164SEric Sandeen clear_buffer_delay(bh);
241615291164SEric Sandeen clear_buffer_unwritten(bh);
2417470decc6SDave Kleikamp bh->b_bdev = NULL;
2418470decc6SDave Kleikamp return may_free;
2419470decc6SDave Kleikamp }
2420470decc6SDave Kleikamp
2421470decc6SDave Kleikamp /**
2422ccd16945SMatthew Wilcox (Oracle) * jbd2_journal_invalidate_folio()
2423470decc6SDave Kleikamp * @journal: journal to use for flush...
2424ccd16945SMatthew Wilcox (Oracle) * @folio: folio to flush
2425259709b0SLukas Czerner * @offset: start of the range to invalidate
2426259709b0SLukas Czerner * @length: length of the range to invalidate
2427470decc6SDave Kleikamp *
2428259709b0SLukas Czerner * Reap page buffers containing data after in the specified range in page.
2429259709b0SLukas Czerner * Can return -EBUSY if buffers are part of the committing transaction and
2430259709b0SLukas Czerner * the page is straddling i_size. Caller then has to wait for current commit
2431259709b0SLukas Czerner * and try again.
2432470decc6SDave Kleikamp */
jbd2_journal_invalidate_folio(journal_t * journal,struct folio * folio,size_t offset,size_t length)2433ccd16945SMatthew Wilcox (Oracle) int jbd2_journal_invalidate_folio(journal_t *journal, struct folio *folio,
2434ccd16945SMatthew Wilcox (Oracle) size_t offset, size_t length)
2435470decc6SDave Kleikamp {
2436470decc6SDave Kleikamp struct buffer_head *head, *bh, *next;
2437259709b0SLukas Czerner unsigned int stop = offset + length;
2438470decc6SDave Kleikamp unsigned int curr_off = 0;
2439ccd16945SMatthew Wilcox (Oracle) int partial_page = (offset || length < folio_size(folio));
2440470decc6SDave Kleikamp int may_free = 1;
244153e87268SJan Kara int ret = 0;
2442470decc6SDave Kleikamp
2443ccd16945SMatthew Wilcox (Oracle) if (!folio_test_locked(folio))
2444470decc6SDave Kleikamp BUG();
2445ccd16945SMatthew Wilcox (Oracle) head = folio_buffers(folio);
2446ccd16945SMatthew Wilcox (Oracle) if (!head)
244753e87268SJan Kara return 0;
2448470decc6SDave Kleikamp
2449ccd16945SMatthew Wilcox (Oracle) BUG_ON(stop > folio_size(folio) || stop < length);
2450259709b0SLukas Czerner
2451470decc6SDave Kleikamp /* We will potentially be playing with lists other than just the
2452470decc6SDave Kleikamp * data lists (especially for journaled data mode), so be
2453470decc6SDave Kleikamp * cautious in our locking. */
2454470decc6SDave Kleikamp
2455ccd16945SMatthew Wilcox (Oracle) bh = head;
2456470decc6SDave Kleikamp do {
2457470decc6SDave Kleikamp unsigned int next_off = curr_off + bh->b_size;
2458470decc6SDave Kleikamp next = bh->b_this_page;
2459470decc6SDave Kleikamp
2460259709b0SLukas Czerner if (next_off > stop)
2461259709b0SLukas Czerner return 0;
2462259709b0SLukas Czerner
2463470decc6SDave Kleikamp if (offset <= curr_off) {
2464470decc6SDave Kleikamp /* This block is wholly outside the truncation point */
2465470decc6SDave Kleikamp lock_buffer(bh);
2466259709b0SLukas Czerner ret = journal_unmap_buffer(journal, bh, partial_page);
2467470decc6SDave Kleikamp unlock_buffer(bh);
246853e87268SJan Kara if (ret < 0)
246953e87268SJan Kara return ret;
247053e87268SJan Kara may_free &= ret;
2471470decc6SDave Kleikamp }
2472470decc6SDave Kleikamp curr_off = next_off;
2473470decc6SDave Kleikamp bh = next;
2474470decc6SDave Kleikamp
2475470decc6SDave Kleikamp } while (bh != head);
2476470decc6SDave Kleikamp
2477259709b0SLukas Czerner if (!partial_page) {
247868189fefSMatthew Wilcox (Oracle) if (may_free && try_to_free_buffers(folio))
2479ccd16945SMatthew Wilcox (Oracle) J_ASSERT(!folio_buffers(folio));
2480470decc6SDave Kleikamp }
248153e87268SJan Kara return 0;
2482470decc6SDave Kleikamp }
2483470decc6SDave Kleikamp
2484470decc6SDave Kleikamp /*
2485470decc6SDave Kleikamp * File a buffer on the given transaction list.
2486470decc6SDave Kleikamp */
__jbd2_journal_file_buffer(struct journal_head * jh,transaction_t * transaction,int jlist)2487f7f4bccbSMingming Cao void __jbd2_journal_file_buffer(struct journal_head *jh,
2488470decc6SDave Kleikamp transaction_t *transaction, int jlist)
2489470decc6SDave Kleikamp {
2490470decc6SDave Kleikamp struct journal_head **list = NULL;
2491470decc6SDave Kleikamp int was_dirty = 0;
2492470decc6SDave Kleikamp struct buffer_head *bh = jh2bh(jh);
2493470decc6SDave Kleikamp
249446417064SThomas Gleixner lockdep_assert_held(&jh->b_state_lock);
2495470decc6SDave Kleikamp assert_spin_locked(&transaction->t_journal->j_list_lock);
2496470decc6SDave Kleikamp
2497470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
2498470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_transaction == transaction ||
24994019191bSMingming Cao jh->b_transaction == NULL);
2500470decc6SDave Kleikamp
2501470decc6SDave Kleikamp if (jh->b_transaction && jh->b_jlist == jlist)
2502470decc6SDave Kleikamp return;
2503470decc6SDave Kleikamp
2504470decc6SDave Kleikamp if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
2505470decc6SDave Kleikamp jlist == BJ_Shadow || jlist == BJ_Forget) {
2506f91d1d04SJan Kara /*
2507f91d1d04SJan Kara * For metadata buffers, we track dirty bit in buffer_jbddirty
2508f91d1d04SJan Kara * instead of buffer_dirty. We should not see a dirty bit set
2509f91d1d04SJan Kara * here because we clear it in do_get_write_access but e.g.
2510f91d1d04SJan Kara * tune2fs can modify the sb and set the dirty bit at any time
2511f91d1d04SJan Kara * so we try to gracefully handle that.
2512f91d1d04SJan Kara */
2513f91d1d04SJan Kara if (buffer_dirty(bh))
2514f91d1d04SJan Kara warn_dirty_buffer(bh);
2515470decc6SDave Kleikamp if (test_clear_buffer_dirty(bh) ||
2516470decc6SDave Kleikamp test_clear_buffer_jbddirty(bh))
2517470decc6SDave Kleikamp was_dirty = 1;
2518470decc6SDave Kleikamp }
2519470decc6SDave Kleikamp
2520470decc6SDave Kleikamp if (jh->b_transaction)
2521f7f4bccbSMingming Cao __jbd2_journal_temp_unlink_buffer(jh);
2522de1b7941SJan Kara else
2523de1b7941SJan Kara jbd2_journal_grab_journal_head(bh);
2524470decc6SDave Kleikamp jh->b_transaction = transaction;
2525470decc6SDave Kleikamp
2526470decc6SDave Kleikamp switch (jlist) {
2527470decc6SDave Kleikamp case BJ_None:
2528470decc6SDave Kleikamp J_ASSERT_JH(jh, !jh->b_committed_data);
2529470decc6SDave Kleikamp J_ASSERT_JH(jh, !jh->b_frozen_data);
2530470decc6SDave Kleikamp return;
2531470decc6SDave Kleikamp case BJ_Metadata:
2532470decc6SDave Kleikamp transaction->t_nr_buffers++;
2533470decc6SDave Kleikamp list = &transaction->t_buffers;
2534470decc6SDave Kleikamp break;
2535470decc6SDave Kleikamp case BJ_Forget:
2536470decc6SDave Kleikamp list = &transaction->t_forget;
2537470decc6SDave Kleikamp break;
2538470decc6SDave Kleikamp case BJ_Shadow:
2539470decc6SDave Kleikamp list = &transaction->t_shadow_list;
2540470decc6SDave Kleikamp break;
2541470decc6SDave Kleikamp case BJ_Reserved:
2542470decc6SDave Kleikamp list = &transaction->t_reserved_list;
2543470decc6SDave Kleikamp break;
2544470decc6SDave Kleikamp }
2545470decc6SDave Kleikamp
2546470decc6SDave Kleikamp __blist_add_buffer(list, jh);
2547470decc6SDave Kleikamp jh->b_jlist = jlist;
2548470decc6SDave Kleikamp
2549470decc6SDave Kleikamp if (was_dirty)
2550470decc6SDave Kleikamp set_buffer_jbddirty(bh);
2551470decc6SDave Kleikamp }
2552470decc6SDave Kleikamp
jbd2_journal_file_buffer(struct journal_head * jh,transaction_t * transaction,int jlist)2553f7f4bccbSMingming Cao void jbd2_journal_file_buffer(struct journal_head *jh,
2554470decc6SDave Kleikamp transaction_t *transaction, int jlist)
2555470decc6SDave Kleikamp {
255646417064SThomas Gleixner spin_lock(&jh->b_state_lock);
2557470decc6SDave Kleikamp spin_lock(&transaction->t_journal->j_list_lock);
2558f7f4bccbSMingming Cao __jbd2_journal_file_buffer(jh, transaction, jlist);
2559470decc6SDave Kleikamp spin_unlock(&transaction->t_journal->j_list_lock);
256046417064SThomas Gleixner spin_unlock(&jh->b_state_lock);
2561470decc6SDave Kleikamp }
2562470decc6SDave Kleikamp
2563470decc6SDave Kleikamp /*
2564470decc6SDave Kleikamp * Remove a buffer from its current buffer list in preparation for
2565470decc6SDave Kleikamp * dropping it from its current transaction entirely. If the buffer has
2566470decc6SDave Kleikamp * already started to be used by a subsequent transaction, refile the
2567470decc6SDave Kleikamp * buffer on that transaction's metadata list.
2568470decc6SDave Kleikamp *
2569de1b7941SJan Kara * Called under j_list_lock
257046417064SThomas Gleixner * Called under jh->b_state_lock
2571de1b7941SJan Kara *
257293108ebbSJan Kara * When this function returns true, there's no next transaction to refile to
257393108ebbSJan Kara * and the caller has to drop jh reference through
257493108ebbSJan Kara * jbd2_journal_put_journal_head().
2575470decc6SDave Kleikamp */
__jbd2_journal_refile_buffer(struct journal_head * jh)257693108ebbSJan Kara bool __jbd2_journal_refile_buffer(struct journal_head *jh)
2577470decc6SDave Kleikamp {
2578ba869023Sdingdinghua int was_dirty, jlist;
2579470decc6SDave Kleikamp struct buffer_head *bh = jh2bh(jh);
2580470decc6SDave Kleikamp
258146417064SThomas Gleixner lockdep_assert_held(&jh->b_state_lock);
2582470decc6SDave Kleikamp if (jh->b_transaction)
2583470decc6SDave Kleikamp assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
2584470decc6SDave Kleikamp
2585470decc6SDave Kleikamp /* If the buffer is now unused, just drop it. */
2586470decc6SDave Kleikamp if (jh->b_next_transaction == NULL) {
2587f7f4bccbSMingming Cao __jbd2_journal_unfile_buffer(jh);
258893108ebbSJan Kara return true;
2589470decc6SDave Kleikamp }
2590470decc6SDave Kleikamp
2591470decc6SDave Kleikamp /*
2592470decc6SDave Kleikamp * It has been modified by a later transaction: add it to the new
2593470decc6SDave Kleikamp * transaction's metadata list.
2594470decc6SDave Kleikamp */
2595470decc6SDave Kleikamp
2596470decc6SDave Kleikamp was_dirty = test_clear_buffer_jbddirty(bh);
2597f7f4bccbSMingming Cao __jbd2_journal_temp_unlink_buffer(jh);
259824dc9864SLukas Czerner
259924dc9864SLukas Czerner /*
260024dc9864SLukas Czerner * b_transaction must be set, otherwise the new b_transaction won't
260124dc9864SLukas Czerner * be holding jh reference
260224dc9864SLukas Czerner */
260324dc9864SLukas Czerner J_ASSERT_JH(jh, jh->b_transaction != NULL);
260424dc9864SLukas Czerner
2605de1b7941SJan Kara /*
2606de1b7941SJan Kara * We set b_transaction here because b_next_transaction will inherit
2607de1b7941SJan Kara * our jh reference and thus __jbd2_journal_file_buffer() must not
2608de1b7941SJan Kara * take a new one.
2609de1b7941SJan Kara */
26106c5d9112SQian Cai WRITE_ONCE(jh->b_transaction, jh->b_next_transaction);
26116c5d9112SQian Cai WRITE_ONCE(jh->b_next_transaction, NULL);
2612ba869023Sdingdinghua if (buffer_freed(bh))
2613ba869023Sdingdinghua jlist = BJ_Forget;
2614ba869023Sdingdinghua else if (jh->b_modified)
2615ba869023Sdingdinghua jlist = BJ_Metadata;
2616ba869023Sdingdinghua else
2617ba869023Sdingdinghua jlist = BJ_Reserved;
2618ba869023Sdingdinghua __jbd2_journal_file_buffer(jh, jh->b_transaction, jlist);
2619470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
2620470decc6SDave Kleikamp
2621470decc6SDave Kleikamp if (was_dirty)
2622470decc6SDave Kleikamp set_buffer_jbddirty(bh);
262393108ebbSJan Kara return false;
2624470decc6SDave Kleikamp }
2625470decc6SDave Kleikamp
2626470decc6SDave Kleikamp /*
2627de1b7941SJan Kara * __jbd2_journal_refile_buffer() with necessary locking added. We take our
2628de1b7941SJan Kara * bh reference so that we can safely unlock bh.
2629470decc6SDave Kleikamp *
2630de1b7941SJan Kara * The jh and bh may be freed by this call.
2631470decc6SDave Kleikamp */
jbd2_journal_refile_buffer(journal_t * journal,struct journal_head * jh)2632f7f4bccbSMingming Cao void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh)
2633470decc6SDave Kleikamp {
263493108ebbSJan Kara bool drop;
2635470decc6SDave Kleikamp
263646417064SThomas Gleixner spin_lock(&jh->b_state_lock);
2637470decc6SDave Kleikamp spin_lock(&journal->j_list_lock);
263893108ebbSJan Kara drop = __jbd2_journal_refile_buffer(jh);
263946417064SThomas Gleixner spin_unlock(&jh->b_state_lock);
2640470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock);
264193108ebbSJan Kara if (drop)
264293108ebbSJan Kara jbd2_journal_put_journal_head(jh);
2643470decc6SDave Kleikamp }
2644c851ed54SJan Kara
2645c851ed54SJan Kara /*
2646c851ed54SJan Kara * File inode in the inode list of the handle's transaction
2647c851ed54SJan Kara */
jbd2_journal_file_inode(handle_t * handle,struct jbd2_inode * jinode,unsigned long flags,loff_t start_byte,loff_t end_byte)264841617e1aSJan Kara static int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode,
26496ba0e7dcSRoss Zwisler unsigned long flags, loff_t start_byte, loff_t end_byte)
2650c851ed54SJan Kara {
2651c851ed54SJan Kara transaction_t *transaction = handle->h_transaction;
265241a5b913STheodore Ts'o journal_t *journal;
2653c851ed54SJan Kara
2654c851ed54SJan Kara if (is_handle_aborted(handle))
265541a5b913STheodore Ts'o return -EROFS;
265641a5b913STheodore Ts'o journal = transaction->t_journal;
2657c851ed54SJan Kara
2658cb3b3bf2SJan Kara jbd2_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino,
2659c851ed54SJan Kara transaction->t_tid);
2660c851ed54SJan Kara
2661c851ed54SJan Kara spin_lock(&journal->j_list_lock);
266241617e1aSJan Kara jinode->i_flags |= flags;
26636ba0e7dcSRoss Zwisler
26646ba0e7dcSRoss Zwisler if (jinode->i_dirty_end) {
26656ba0e7dcSRoss Zwisler jinode->i_dirty_start = min(jinode->i_dirty_start, start_byte);
26666ba0e7dcSRoss Zwisler jinode->i_dirty_end = max(jinode->i_dirty_end, end_byte);
26676ba0e7dcSRoss Zwisler } else {
26686ba0e7dcSRoss Zwisler jinode->i_dirty_start = start_byte;
26696ba0e7dcSRoss Zwisler jinode->i_dirty_end = end_byte;
26706ba0e7dcSRoss Zwisler }
26716ba0e7dcSRoss Zwisler
267241617e1aSJan Kara /* Is inode already attached where we need it? */
2673c851ed54SJan Kara if (jinode->i_transaction == transaction ||
2674c851ed54SJan Kara jinode->i_next_transaction == transaction)
2675c851ed54SJan Kara goto done;
2676c851ed54SJan Kara
267781be12c8SJan Kara /*
267881be12c8SJan Kara * We only ever set this variable to 1 so the test is safe. Since
267981be12c8SJan Kara * t_need_data_flush is likely to be set, we do the test to save some
268081be12c8SJan Kara * cacheline bouncing
268181be12c8SJan Kara */
268281be12c8SJan Kara if (!transaction->t_need_data_flush)
268381be12c8SJan Kara transaction->t_need_data_flush = 1;
2684c851ed54SJan Kara /* On some different transaction's list - should be
2685c851ed54SJan Kara * the committing one */
2686c851ed54SJan Kara if (jinode->i_transaction) {
2687c851ed54SJan Kara J_ASSERT(jinode->i_next_transaction == NULL);
2688c851ed54SJan Kara J_ASSERT(jinode->i_transaction ==
2689c851ed54SJan Kara journal->j_committing_transaction);
2690c851ed54SJan Kara jinode->i_next_transaction = transaction;
2691c851ed54SJan Kara goto done;
2692c851ed54SJan Kara }
2693c851ed54SJan Kara /* Not on any transaction list... */
2694c851ed54SJan Kara J_ASSERT(!jinode->i_next_transaction);
2695c851ed54SJan Kara jinode->i_transaction = transaction;
2696c851ed54SJan Kara list_add(&jinode->i_list, &transaction->t_inode_list);
2697c851ed54SJan Kara done:
2698c851ed54SJan Kara spin_unlock(&journal->j_list_lock);
2699c851ed54SJan Kara
2700c851ed54SJan Kara return 0;
2701c851ed54SJan Kara }
2702c851ed54SJan Kara
jbd2_journal_inode_ranged_write(handle_t * handle,struct jbd2_inode * jinode,loff_t start_byte,loff_t length)27036ba0e7dcSRoss Zwisler int jbd2_journal_inode_ranged_write(handle_t *handle,
27046ba0e7dcSRoss Zwisler struct jbd2_inode *jinode, loff_t start_byte, loff_t length)
27056ba0e7dcSRoss Zwisler {
27066ba0e7dcSRoss Zwisler return jbd2_journal_file_inode(handle, jinode,
27076ba0e7dcSRoss Zwisler JI_WRITE_DATA | JI_WAIT_DATA, start_byte,
27086ba0e7dcSRoss Zwisler start_byte + length - 1);
27096ba0e7dcSRoss Zwisler }
27106ba0e7dcSRoss Zwisler
jbd2_journal_inode_ranged_wait(handle_t * handle,struct jbd2_inode * jinode,loff_t start_byte,loff_t length)27116ba0e7dcSRoss Zwisler int jbd2_journal_inode_ranged_wait(handle_t *handle, struct jbd2_inode *jinode,
27126ba0e7dcSRoss Zwisler loff_t start_byte, loff_t length)
27136ba0e7dcSRoss Zwisler {
27146ba0e7dcSRoss Zwisler return jbd2_journal_file_inode(handle, jinode, JI_WAIT_DATA,
27156ba0e7dcSRoss Zwisler start_byte, start_byte + length - 1);
271641617e1aSJan Kara }
271741617e1aSJan Kara
2718c851ed54SJan Kara /*
27197f5aa215SJan Kara * File truncate and transaction commit interact with each other in a
27207f5aa215SJan Kara * non-trivial way. If a transaction writing data block A is
27217f5aa215SJan Kara * committing, we cannot discard the data by truncate until we have
27227f5aa215SJan Kara * written them. Otherwise if we crashed after the transaction with
27237f5aa215SJan Kara * write has committed but before the transaction with truncate has
27247f5aa215SJan Kara * committed, we could see stale data in block A. This function is a
27257f5aa215SJan Kara * helper to solve this problem. It starts writeout of the truncated
27267f5aa215SJan Kara * part in case it is in the committing transaction.
27277f5aa215SJan Kara *
27287f5aa215SJan Kara * Filesystem code must call this function when inode is journaled in
27297f5aa215SJan Kara * ordered mode before truncation happens and after the inode has been
27307f5aa215SJan Kara * placed on orphan list with the new inode size. The second condition
27317f5aa215SJan Kara * avoids the race that someone writes new data and we start
27327f5aa215SJan Kara * committing the transaction after this function has been called but
27337f5aa215SJan Kara * before a transaction for truncate is started (and furthermore it
27347f5aa215SJan Kara * allows us to optimize the case where the addition to orphan list
27357f5aa215SJan Kara * happens in the same transaction as write --- we don't have to write
27367f5aa215SJan Kara * any data in such case).
2737c851ed54SJan Kara */
jbd2_journal_begin_ordered_truncate(journal_t * journal,struct jbd2_inode * jinode,loff_t new_size)27387f5aa215SJan Kara int jbd2_journal_begin_ordered_truncate(journal_t *journal,
27397f5aa215SJan Kara struct jbd2_inode *jinode,
2740c851ed54SJan Kara loff_t new_size)
2741c851ed54SJan Kara {
27427f5aa215SJan Kara transaction_t *inode_trans, *commit_trans;
2743c851ed54SJan Kara int ret = 0;
2744c851ed54SJan Kara
27457f5aa215SJan Kara /* This is a quick check to avoid locking if not necessary */
27467f5aa215SJan Kara if (!jinode->i_transaction)
2747c851ed54SJan Kara goto out;
27487f5aa215SJan Kara /* Locks are here just to force reading of recent values, it is
27497f5aa215SJan Kara * enough that the transaction was not committing before we started
27507f5aa215SJan Kara * a transaction adding the inode to orphan list */
2751a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock);
2752c851ed54SJan Kara commit_trans = journal->j_committing_transaction;
2753a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock);
27547f5aa215SJan Kara spin_lock(&journal->j_list_lock);
27557f5aa215SJan Kara inode_trans = jinode->i_transaction;
27567f5aa215SJan Kara spin_unlock(&journal->j_list_lock);
27577f5aa215SJan Kara if (inode_trans == commit_trans) {
27587f5aa215SJan Kara ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping,
2759c851ed54SJan Kara new_size, LLONG_MAX);
2760c851ed54SJan Kara if (ret)
2761c851ed54SJan Kara jbd2_journal_abort(journal, ret);
2762c851ed54SJan Kara }
2763c851ed54SJan Kara out:
2764c851ed54SJan Kara return ret;
2765c851ed54SJan Kara }
2766