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; 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 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 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 */ 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 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; 110470decc6SDave Kleikamp spin_lock_init(&transaction->t_handle_lock); 111a51dca9cSTheodore Ts'o atomic_set(&transaction->t_updates, 0); 1128f7d89f3SJan Kara atomic_set(&transaction->t_outstanding_credits, 1139f356e5aSJan Kara jbd2_descriptor_blocks_per_trans(journal) + 1148f7d89f3SJan Kara atomic_read(&journal->j_reserved_credits)); 115fdc3ef88SJan Kara atomic_set(&transaction->t_outstanding_revokes, 0); 1168dd42046STheodore Ts'o atomic_set(&transaction->t_handle_count, 0); 117c851ed54SJan Kara INIT_LIST_HEAD(&transaction->t_inode_list); 1183e624fc7STheodore Ts'o INIT_LIST_HEAD(&transaction->t_private_list); 119470decc6SDave Kleikamp 120470decc6SDave Kleikamp /* Set up the commit timer for the new transaction. */ 121b1f485f2SAndreas Dilger journal->j_commit_timer.expires = round_jiffies_up(transaction->t_expires); 122470decc6SDave Kleikamp add_timer(&journal->j_commit_timer); 123470decc6SDave Kleikamp 124470decc6SDave Kleikamp J_ASSERT(journal->j_running_transaction == NULL); 125470decc6SDave Kleikamp journal->j_running_transaction = transaction; 1268e85fb3fSJohann Lombardi transaction->t_max_wait = 0; 1278e85fb3fSJohann Lombardi transaction->t_start = jiffies; 1289fff24aaSTheodore Ts'o transaction->t_requested = 0; 129470decc6SDave Kleikamp } 130470decc6SDave Kleikamp 131470decc6SDave Kleikamp /* 132470decc6SDave Kleikamp * Handle management. 133470decc6SDave Kleikamp * 134470decc6SDave Kleikamp * A handle_t is an object which represents a single atomic update to a 135470decc6SDave Kleikamp * filesystem, and which tracks all of the modifications which form part 136470decc6SDave Kleikamp * of that one update. 137470decc6SDave Kleikamp */ 138470decc6SDave Kleikamp 139470decc6SDave Kleikamp /* 14028e35e42STao Ma * Update transaction's maximum wait time, if debugging is enabled. 1416d0bf005STheodore Ts'o * 1426d0bf005STheodore Ts'o * In order for t_max_wait to be reliable, it must be protected by a 1436d0bf005STheodore Ts'o * lock. But doing so will mean that start_this_handle() can not be 1446d0bf005STheodore Ts'o * run in parallel on SMP systems, which limits our scalability. So 1456d0bf005STheodore Ts'o * unless debugging is enabled, we no longer update t_max_wait, which 1466d0bf005STheodore Ts'o * means that maximum wait time reported by the jbd2_run_stats 1476d0bf005STheodore Ts'o * tracepoint will always be zero. 1486d0bf005STheodore Ts'o */ 14928e35e42STao Ma static inline void update_t_max_wait(transaction_t *transaction, 15028e35e42STao Ma unsigned long ts) 1516d0bf005STheodore Ts'o { 1526d0bf005STheodore Ts'o #ifdef CONFIG_JBD2_DEBUG 1536d0bf005STheodore Ts'o if (jbd2_journal_enable_debug && 1546d0bf005STheodore Ts'o time_after(transaction->t_start, ts)) { 1556d0bf005STheodore Ts'o ts = jbd2_time_diff(ts, transaction->t_start); 1566d0bf005STheodore Ts'o spin_lock(&transaction->t_handle_lock); 1576d0bf005STheodore Ts'o if (ts > transaction->t_max_wait) 1586d0bf005STheodore Ts'o transaction->t_max_wait = ts; 1596d0bf005STheodore Ts'o spin_unlock(&transaction->t_handle_lock); 1606d0bf005STheodore Ts'o } 1616d0bf005STheodore Ts'o #endif 1626d0bf005STheodore Ts'o } 1636d0bf005STheodore Ts'o 1646d0bf005STheodore Ts'o /* 16596f1e097SJan Kara * Wait until running transaction passes to T_FLUSH state and new transaction 16696f1e097SJan Kara * can thus be started. Also starts the commit if needed. The function expects 16796f1e097SJan Kara * running transaction to exist and releases j_state_lock. 1688f7d89f3SJan Kara */ 1698f7d89f3SJan Kara static void wait_transaction_locked(journal_t *journal) 1708f7d89f3SJan Kara __releases(journal->j_state_lock) 1718f7d89f3SJan Kara { 1728f7d89f3SJan Kara DEFINE_WAIT(wait); 1738f7d89f3SJan Kara int need_to_start; 1748f7d89f3SJan Kara tid_t tid = journal->j_running_transaction->t_tid; 1758f7d89f3SJan Kara 1768f7d89f3SJan Kara prepare_to_wait(&journal->j_wait_transaction_locked, &wait, 1778f7d89f3SJan Kara TASK_UNINTERRUPTIBLE); 1788f7d89f3SJan Kara need_to_start = !tid_geq(journal->j_commit_request, tid); 1798f7d89f3SJan Kara read_unlock(&journal->j_state_lock); 1808f7d89f3SJan Kara if (need_to_start) 1818f7d89f3SJan Kara jbd2_log_start_commit(journal, tid); 182e03a9976SJan Kara jbd2_might_wait_for_commit(journal); 1838f7d89f3SJan Kara schedule(); 1848f7d89f3SJan Kara finish_wait(&journal->j_wait_transaction_locked, &wait); 1858f7d89f3SJan Kara } 1868f7d89f3SJan Kara 18796f1e097SJan Kara /* 18896f1e097SJan Kara * Wait until running transaction transitions from T_SWITCH to T_FLUSH 18996f1e097SJan Kara * state and new transaction can thus be started. The function releases 19096f1e097SJan Kara * j_state_lock. 19196f1e097SJan Kara */ 19296f1e097SJan Kara static void wait_transaction_switching(journal_t *journal) 19396f1e097SJan Kara __releases(journal->j_state_lock) 19496f1e097SJan Kara { 19596f1e097SJan Kara DEFINE_WAIT(wait); 19696f1e097SJan Kara 19796f1e097SJan Kara if (WARN_ON(!journal->j_running_transaction || 19896f1e097SJan Kara journal->j_running_transaction->t_state != T_SWITCH)) 19996f1e097SJan Kara return; 20096f1e097SJan Kara prepare_to_wait(&journal->j_wait_transaction_locked, &wait, 20196f1e097SJan Kara TASK_UNINTERRUPTIBLE); 20296f1e097SJan Kara read_unlock(&journal->j_state_lock); 20396f1e097SJan Kara /* 20496f1e097SJan Kara * We don't call jbd2_might_wait_for_commit() here as there's no 20596f1e097SJan Kara * waiting for outstanding handles happening anymore in T_SWITCH state 20696f1e097SJan Kara * and handling of reserved handles actually relies on that for 20796f1e097SJan Kara * correctness. 20896f1e097SJan Kara */ 20996f1e097SJan Kara schedule(); 21096f1e097SJan Kara finish_wait(&journal->j_wait_transaction_locked, &wait); 21196f1e097SJan Kara } 21296f1e097SJan Kara 2138f7d89f3SJan Kara static void sub_reserved_credits(journal_t *journal, int blocks) 2148f7d89f3SJan Kara { 2158f7d89f3SJan Kara atomic_sub(blocks, &journal->j_reserved_credits); 2168f7d89f3SJan Kara wake_up(&journal->j_wait_reserved); 2178f7d89f3SJan Kara } 2188f7d89f3SJan Kara 2198f7d89f3SJan Kara /* 2208f7d89f3SJan Kara * Wait until we can add credits for handle to the running transaction. Called 2218f7d89f3SJan Kara * with j_state_lock held for reading. Returns 0 if handle joined the running 2228f7d89f3SJan Kara * transaction. Returns 1 if we had to wait, j_state_lock is dropped, and 2238f7d89f3SJan Kara * caller must retry. 2248f7d89f3SJan Kara */ 2258f7d89f3SJan Kara static int add_transaction_credits(journal_t *journal, int blocks, 2268f7d89f3SJan Kara int rsv_blocks) 2278f7d89f3SJan Kara { 2288f7d89f3SJan Kara transaction_t *t = journal->j_running_transaction; 2298f7d89f3SJan Kara int needed; 2308f7d89f3SJan Kara int total = blocks + rsv_blocks; 2318f7d89f3SJan Kara 2328f7d89f3SJan Kara /* 2338f7d89f3SJan Kara * If the current transaction is locked down for commit, wait 2348f7d89f3SJan Kara * for the lock to be released. 2358f7d89f3SJan Kara */ 23696f1e097SJan Kara if (t->t_state != T_RUNNING) { 23796f1e097SJan Kara WARN_ON_ONCE(t->t_state >= T_FLUSH); 2388f7d89f3SJan Kara wait_transaction_locked(journal); 2398f7d89f3SJan Kara return 1; 2408f7d89f3SJan Kara } 2418f7d89f3SJan Kara 2428f7d89f3SJan Kara /* 2438f7d89f3SJan Kara * If there is not enough space left in the log to write all 2448f7d89f3SJan Kara * potential buffers requested by this operation, we need to 2458f7d89f3SJan Kara * stall pending a log checkpoint to free some more log space. 2468f7d89f3SJan Kara */ 2478f7d89f3SJan Kara needed = atomic_add_return(total, &t->t_outstanding_credits); 2488f7d89f3SJan Kara if (needed > journal->j_max_transaction_buffers) { 2498f7d89f3SJan Kara /* 2508f7d89f3SJan Kara * If the current transaction is already too large, 2518f7d89f3SJan Kara * then start to commit it: we can then go back and 2528f7d89f3SJan Kara * attach this handle to a new transaction. 2538f7d89f3SJan Kara */ 2548f7d89f3SJan Kara atomic_sub(total, &t->t_outstanding_credits); 2556d3ec14dSLukas Czerner 2566d3ec14dSLukas Czerner /* 2576d3ec14dSLukas Czerner * Is the number of reserved credits in the current transaction too 2586d3ec14dSLukas Czerner * big to fit this handle? Wait until reserved credits are freed. 2596d3ec14dSLukas Czerner */ 2606d3ec14dSLukas Czerner if (atomic_read(&journal->j_reserved_credits) + total > 2616d3ec14dSLukas Czerner journal->j_max_transaction_buffers) { 2626d3ec14dSLukas Czerner read_unlock(&journal->j_state_lock); 263e03a9976SJan Kara jbd2_might_wait_for_commit(journal); 2646d3ec14dSLukas Czerner wait_event(journal->j_wait_reserved, 2656d3ec14dSLukas Czerner atomic_read(&journal->j_reserved_credits) + total <= 2666d3ec14dSLukas Czerner journal->j_max_transaction_buffers); 2676d3ec14dSLukas Czerner return 1; 2686d3ec14dSLukas Czerner } 2696d3ec14dSLukas Czerner 2708f7d89f3SJan Kara wait_transaction_locked(journal); 2718f7d89f3SJan Kara return 1; 2728f7d89f3SJan Kara } 2738f7d89f3SJan Kara 2748f7d89f3SJan Kara /* 2758f7d89f3SJan Kara * The commit code assumes that it can get enough log space 2768f7d89f3SJan Kara * without forcing a checkpoint. This is *critical* for 2778f7d89f3SJan Kara * correctness: a checkpoint of a buffer which is also 2788f7d89f3SJan Kara * associated with a committing transaction creates a deadlock, 2798f7d89f3SJan Kara * so commit simply cannot force through checkpoints. 2808f7d89f3SJan Kara * 2818f7d89f3SJan Kara * We must therefore ensure the necessary space in the journal 2828f7d89f3SJan Kara * *before* starting to dirty potentially checkpointed buffers 2838f7d89f3SJan Kara * in the new transaction. 2848f7d89f3SJan Kara */ 28577444ac4SJan Kara if (jbd2_log_space_left(journal) < journal->j_max_transaction_buffers) { 2868f7d89f3SJan Kara atomic_sub(total, &t->t_outstanding_credits); 2878f7d89f3SJan Kara read_unlock(&journal->j_state_lock); 288e03a9976SJan Kara jbd2_might_wait_for_commit(journal); 2898f7d89f3SJan Kara write_lock(&journal->j_state_lock); 29077444ac4SJan Kara if (jbd2_log_space_left(journal) < 29177444ac4SJan Kara journal->j_max_transaction_buffers) 2928f7d89f3SJan Kara __jbd2_log_wait_for_space(journal); 2938f7d89f3SJan Kara write_unlock(&journal->j_state_lock); 2948f7d89f3SJan Kara return 1; 2958f7d89f3SJan Kara } 2968f7d89f3SJan Kara 2978f7d89f3SJan Kara /* No reservation? We are done... */ 2988f7d89f3SJan Kara if (!rsv_blocks) 2998f7d89f3SJan Kara return 0; 3008f7d89f3SJan Kara 3018f7d89f3SJan Kara needed = atomic_add_return(rsv_blocks, &journal->j_reserved_credits); 3028f7d89f3SJan Kara /* We allow at most half of a transaction to be reserved */ 3038f7d89f3SJan Kara if (needed > journal->j_max_transaction_buffers / 2) { 3048f7d89f3SJan Kara sub_reserved_credits(journal, rsv_blocks); 3058f7d89f3SJan Kara atomic_sub(total, &t->t_outstanding_credits); 3068f7d89f3SJan Kara read_unlock(&journal->j_state_lock); 307e03a9976SJan Kara jbd2_might_wait_for_commit(journal); 3088f7d89f3SJan Kara wait_event(journal->j_wait_reserved, 3098f7d89f3SJan Kara atomic_read(&journal->j_reserved_credits) + rsv_blocks 3108f7d89f3SJan Kara <= journal->j_max_transaction_buffers / 2); 3118f7d89f3SJan Kara return 1; 3128f7d89f3SJan Kara } 3138f7d89f3SJan Kara return 0; 3148f7d89f3SJan Kara } 3158f7d89f3SJan Kara 3168f7d89f3SJan Kara /* 317470decc6SDave Kleikamp * start_this_handle: Given a handle, deal with any locking or stalling 318470decc6SDave Kleikamp * needed to make sure that there is enough journal space for the handle 319470decc6SDave Kleikamp * to begin. Attach the handle to a transaction and set up the 320470decc6SDave Kleikamp * transaction's buffer credits. 321470decc6SDave Kleikamp */ 322470decc6SDave Kleikamp 32347def826STheodore Ts'o static int start_this_handle(journal_t *journal, handle_t *handle, 324d2159fb7SDan Carpenter gfp_t gfp_mask) 325470decc6SDave Kleikamp { 326e4471831STheodore Ts'o transaction_t *transaction, *new_transaction = NULL; 327933f1c1eSJan Kara int blocks = handle->h_total_credits; 3288f7d89f3SJan Kara int rsv_blocks = 0; 32928e35e42STao Ma unsigned long ts = jiffies; 330470decc6SDave Kleikamp 3318f7d89f3SJan Kara if (handle->h_rsv_handle) 332933f1c1eSJan Kara rsv_blocks = handle->h_rsv_handle->h_total_credits; 3338f7d89f3SJan Kara 3346d3ec14dSLukas Czerner /* 3356d3ec14dSLukas Czerner * Limit the number of reserved credits to 1/2 of maximum transaction 3366d3ec14dSLukas Czerner * size and limit the number of total credits to not exceed maximum 3376d3ec14dSLukas Czerner * transaction size per operation. 3386d3ec14dSLukas Czerner */ 3396d3ec14dSLukas Czerner if ((rsv_blocks > journal->j_max_transaction_buffers / 2) || 3406d3ec14dSLukas Czerner (rsv_blocks + blocks > journal->j_max_transaction_buffers)) { 3416d3ec14dSLukas Czerner printk(KERN_ERR "JBD2: %s wants too many credits " 3426d3ec14dSLukas Czerner "credits:%d rsv_credits:%d max:%d\n", 3436d3ec14dSLukas Czerner current->comm, blocks, rsv_blocks, 3446d3ec14dSLukas Czerner journal->j_max_transaction_buffers); 3456d3ec14dSLukas Czerner WARN_ON(1); 3466d3ec14dSLukas Czerner return -ENOSPC; 3476d3ec14dSLukas Czerner } 3486d3ec14dSLukas Czerner 349470decc6SDave Kleikamp alloc_transaction: 350470decc6SDave Kleikamp if (!journal->j_running_transaction) { 3516ccaf3e2SMichal Hocko /* 3526ccaf3e2SMichal Hocko * If __GFP_FS is not present, then we may be being called from 3536ccaf3e2SMichal Hocko * inside the fs writeback layer, so we MUST NOT fail. 3546ccaf3e2SMichal Hocko */ 3556ccaf3e2SMichal Hocko if ((gfp_mask & __GFP_FS) == 0) 3566ccaf3e2SMichal Hocko gfp_mask |= __GFP_NOFAIL; 357b2f4edb3SWanlong Gao new_transaction = kmem_cache_zalloc(transaction_cache, 358b2f4edb3SWanlong Gao gfp_mask); 3596ccaf3e2SMichal Hocko if (!new_transaction) 36047def826STheodore Ts'o return -ENOMEM; 361470decc6SDave Kleikamp } 362470decc6SDave Kleikamp 363470decc6SDave Kleikamp jbd_debug(3, "New handle %p going live.\n", handle); 364470decc6SDave Kleikamp 365470decc6SDave Kleikamp /* 366470decc6SDave Kleikamp * We need to hold j_state_lock until t_updates has been incremented, 367470decc6SDave Kleikamp * for proper journal barrier handling 368470decc6SDave Kleikamp */ 369a931da6aSTheodore Ts'o repeat: 370a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 3715c2178e7STheodore Ts'o BUG_ON(journal->j_flags & JBD2_UNMOUNT); 372470decc6SDave Kleikamp if (is_journal_aborted(journal) || 373f7f4bccbSMingming Cao (journal->j_errno != 0 && !(journal->j_flags & JBD2_ACK_ERR))) { 374a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 3750c2022ecSYongqiang Yang jbd2_journal_free_transaction(new_transaction); 37647def826STheodore Ts'o return -EROFS; 377470decc6SDave Kleikamp } 378470decc6SDave Kleikamp 3798f7d89f3SJan Kara /* 3808f7d89f3SJan Kara * Wait on the journal's transaction barrier if necessary. Specifically 3818f7d89f3SJan Kara * we allow reserved handles to proceed because otherwise commit could 3828f7d89f3SJan Kara * deadlock on page writeback not being able to complete. 3838f7d89f3SJan Kara */ 3848f7d89f3SJan Kara if (!handle->h_reserved && journal->j_barrier_count) { 385a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 386470decc6SDave Kleikamp wait_event(journal->j_wait_transaction_locked, 387470decc6SDave Kleikamp journal->j_barrier_count == 0); 388470decc6SDave Kleikamp goto repeat; 389470decc6SDave Kleikamp } 390470decc6SDave Kleikamp 391470decc6SDave Kleikamp if (!journal->j_running_transaction) { 392a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 393a931da6aSTheodore Ts'o if (!new_transaction) 394470decc6SDave Kleikamp goto alloc_transaction; 395a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 396d7961c7fSJan Kara if (!journal->j_running_transaction && 3978f7d89f3SJan Kara (handle->h_reserved || !journal->j_barrier_count)) { 398f7f4bccbSMingming Cao jbd2_get_transaction(journal, new_transaction); 399470decc6SDave Kleikamp new_transaction = NULL; 400470decc6SDave Kleikamp } 401a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 402a931da6aSTheodore Ts'o goto repeat; 403a931da6aSTheodore Ts'o } 404470decc6SDave Kleikamp 405470decc6SDave Kleikamp transaction = journal->j_running_transaction; 406470decc6SDave Kleikamp 4078f7d89f3SJan Kara if (!handle->h_reserved) { 4088f7d89f3SJan Kara /* We may have dropped j_state_lock - restart in that case */ 4098f7d89f3SJan Kara if (add_transaction_credits(journal, blocks, rsv_blocks)) 410470decc6SDave Kleikamp goto repeat; 4118f7d89f3SJan Kara } else { 412470decc6SDave Kleikamp /* 4138f7d89f3SJan Kara * We have handle reserved so we are allowed to join T_LOCKED 4148f7d89f3SJan Kara * transaction and we don't have to check for transaction size 41596f1e097SJan Kara * and journal space. But we still have to wait while running 41696f1e097SJan Kara * transaction is being switched to a committing one as it 41796f1e097SJan Kara * won't wait for any handles anymore. 418470decc6SDave Kleikamp */ 41996f1e097SJan Kara if (transaction->t_state == T_SWITCH) { 42096f1e097SJan Kara wait_transaction_switching(journal); 42196f1e097SJan Kara goto repeat; 42296f1e097SJan Kara } 4238f7d89f3SJan Kara sub_reserved_credits(journal, blocks); 4248f7d89f3SJan Kara handle->h_reserved = 0; 425470decc6SDave Kleikamp } 426470decc6SDave Kleikamp 427470decc6SDave Kleikamp /* OK, account for the buffers that this operation expects to 4288dd42046STheodore Ts'o * use and add the handle to the running transaction. 4298dd42046STheodore Ts'o */ 43028e35e42STao Ma update_t_max_wait(transaction, ts); 431470decc6SDave Kleikamp handle->h_transaction = transaction; 4328f7d89f3SJan Kara handle->h_requested_credits = blocks; 433fdc3ef88SJan Kara handle->h_revoke_credits_requested = handle->h_revoke_credits; 434343d9c28STheodore Ts'o handle->h_start_jiffies = jiffies; 435a51dca9cSTheodore Ts'o atomic_inc(&transaction->t_updates); 4368dd42046STheodore Ts'o atomic_inc(&transaction->t_handle_count); 4378f7d89f3SJan Kara jbd_debug(4, "Handle %p given %d credits (total %d, free %lu)\n", 4388f7d89f3SJan Kara handle, blocks, 439a51dca9cSTheodore Ts'o atomic_read(&transaction->t_outstanding_credits), 44076c39904SJan Kara jbd2_log_space_left(journal)); 441a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 44241a5b913STheodore Ts'o current->journal_info = handle; 4439599b0e5SJan Kara 444ab714affSJan Kara rwsem_acquire_read(&journal->j_trans_commit_map, 0, 0, _THIS_IP_); 4450c2022ecSYongqiang Yang jbd2_journal_free_transaction(new_transaction); 44681378da6SMichal Hocko /* 44781378da6SMichal Hocko * Ensure that no allocations done while the transaction is open are 44881378da6SMichal Hocko * going to recurse back to the fs layer. 44981378da6SMichal Hocko */ 45081378da6SMichal Hocko handle->saved_alloc_context = memalloc_nofs_save(); 45147def826STheodore Ts'o return 0; 452470decc6SDave Kleikamp } 453470decc6SDave Kleikamp 454470decc6SDave Kleikamp /* Allocate a new handle. This should probably be in a slab... */ 455470decc6SDave Kleikamp static handle_t *new_handle(int nblocks) 456470decc6SDave Kleikamp { 457af1e76d6SMingming Cao handle_t *handle = jbd2_alloc_handle(GFP_NOFS); 458470decc6SDave Kleikamp if (!handle) 459470decc6SDave Kleikamp return NULL; 460933f1c1eSJan Kara handle->h_total_credits = nblocks; 461470decc6SDave Kleikamp handle->h_ref = 1; 462470decc6SDave Kleikamp 463470decc6SDave Kleikamp return handle; 464470decc6SDave Kleikamp } 465470decc6SDave Kleikamp 4668f7d89f3SJan Kara handle_t *jbd2__journal_start(journal_t *journal, int nblocks, int rsv_blocks, 467fdc3ef88SJan Kara int revoke_records, gfp_t gfp_mask, 468fdc3ef88SJan Kara unsigned int type, unsigned int line_no) 469470decc6SDave Kleikamp { 470470decc6SDave Kleikamp handle_t *handle = journal_current_handle(); 471470decc6SDave Kleikamp int err; 472470decc6SDave Kleikamp 473470decc6SDave Kleikamp if (!journal) 474470decc6SDave Kleikamp return ERR_PTR(-EROFS); 475470decc6SDave Kleikamp 476470decc6SDave Kleikamp if (handle) { 477470decc6SDave Kleikamp J_ASSERT(handle->h_transaction->t_journal == journal); 478470decc6SDave Kleikamp handle->h_ref++; 479470decc6SDave Kleikamp return handle; 480470decc6SDave Kleikamp } 481470decc6SDave Kleikamp 482fdc3ef88SJan Kara nblocks += DIV_ROUND_UP(revoke_records, 483fdc3ef88SJan Kara journal->j_revoke_records_per_block); 484470decc6SDave Kleikamp handle = new_handle(nblocks); 485470decc6SDave Kleikamp if (!handle) 486470decc6SDave Kleikamp return ERR_PTR(-ENOMEM); 4878f7d89f3SJan Kara if (rsv_blocks) { 4888f7d89f3SJan Kara handle_t *rsv_handle; 4898f7d89f3SJan Kara 4908f7d89f3SJan Kara rsv_handle = new_handle(rsv_blocks); 4918f7d89f3SJan Kara if (!rsv_handle) { 4928f7d89f3SJan Kara jbd2_free_handle(handle); 4938f7d89f3SJan Kara return ERR_PTR(-ENOMEM); 4948f7d89f3SJan Kara } 4958f7d89f3SJan Kara rsv_handle->h_reserved = 1; 4968f7d89f3SJan Kara rsv_handle->h_journal = journal; 4978f7d89f3SJan Kara handle->h_rsv_handle = rsv_handle; 4988f7d89f3SJan Kara } 499fdc3ef88SJan Kara handle->h_revoke_credits = revoke_records; 500470decc6SDave Kleikamp 50147def826STheodore Ts'o err = start_this_handle(journal, handle, gfp_mask); 502470decc6SDave Kleikamp if (err < 0) { 5038f7d89f3SJan Kara if (handle->h_rsv_handle) 5048f7d89f3SJan Kara jbd2_free_handle(handle->h_rsv_handle); 505af1e76d6SMingming Cao jbd2_free_handle(handle); 506df05c1b8SDmitry Monakhov return ERR_PTR(err); 507470decc6SDave Kleikamp } 508343d9c28STheodore Ts'o handle->h_type = type; 509343d9c28STheodore Ts'o handle->h_line_no = line_no; 510343d9c28STheodore Ts'o trace_jbd2_handle_start(journal->j_fs_dev->bd_dev, 511343d9c28STheodore Ts'o handle->h_transaction->t_tid, type, 512343d9c28STheodore Ts'o line_no, nblocks); 51381378da6SMichal Hocko 514470decc6SDave Kleikamp return handle; 515470decc6SDave Kleikamp } 51647def826STheodore Ts'o EXPORT_SYMBOL(jbd2__journal_start); 51747def826STheodore Ts'o 51847def826STheodore Ts'o 51991e4775dSMauro Carvalho Chehab /** 52091e4775dSMauro Carvalho Chehab * handle_t *jbd2_journal_start() - Obtain a new handle. 52191e4775dSMauro Carvalho Chehab * @journal: Journal to start transaction on. 52291e4775dSMauro Carvalho Chehab * @nblocks: number of block buffer we might modify 52391e4775dSMauro Carvalho Chehab * 52491e4775dSMauro Carvalho Chehab * We make sure that the transaction can guarantee at least nblocks of 52591e4775dSMauro Carvalho Chehab * modified buffers in the log. We block until the log can guarantee 52691e4775dSMauro Carvalho Chehab * that much space. Additionally, if rsv_blocks > 0, we also create another 52791e4775dSMauro Carvalho Chehab * handle with rsv_blocks reserved blocks in the journal. This handle is 5280c1cba6cSwangyan * stored in h_rsv_handle. It is not attached to any particular transaction 52991e4775dSMauro Carvalho Chehab * and thus doesn't block transaction commit. If the caller uses this reserved 53091e4775dSMauro Carvalho Chehab * handle, it has to set h_rsv_handle to NULL as otherwise jbd2_journal_stop() 53191e4775dSMauro Carvalho Chehab * on the parent handle will dispose the reserved one. Reserved handle has to 53291e4775dSMauro Carvalho Chehab * be converted to a normal handle using jbd2_journal_start_reserved() before 53391e4775dSMauro Carvalho Chehab * it can be used. 53491e4775dSMauro Carvalho Chehab * 53591e4775dSMauro Carvalho Chehab * Return a pointer to a newly allocated handle, or an ERR_PTR() value 53691e4775dSMauro Carvalho Chehab * on failure. 53791e4775dSMauro Carvalho Chehab */ 53847def826STheodore Ts'o handle_t *jbd2_journal_start(journal_t *journal, int nblocks) 53947def826STheodore Ts'o { 540fdc3ef88SJan Kara return jbd2__journal_start(journal, nblocks, 0, 0, GFP_NOFS, 0, 0); 54147def826STheodore Ts'o } 54247def826STheodore Ts'o EXPORT_SYMBOL(jbd2_journal_start); 54347def826STheodore Ts'o 54414ff6286SJan Kara static void __jbd2_journal_unreserve_handle(handle_t *handle, transaction_t *t) 5458f7d89f3SJan Kara { 5468f7d89f3SJan Kara journal_t *journal = handle->h_journal; 5478f7d89f3SJan Kara 5488f7d89f3SJan Kara WARN_ON(!handle->h_reserved); 549933f1c1eSJan Kara sub_reserved_credits(journal, handle->h_total_credits); 55014ff6286SJan Kara if (t) 55114ff6286SJan Kara atomic_sub(handle->h_total_credits, &t->t_outstanding_credits); 552ec8b6f60SJan Kara } 553ec8b6f60SJan Kara 554ec8b6f60SJan Kara void jbd2_journal_free_reserved(handle_t *handle) 555ec8b6f60SJan Kara { 55614ff6286SJan Kara journal_t *journal = handle->h_journal; 55714ff6286SJan Kara 55814ff6286SJan Kara /* Get j_state_lock to pin running transaction if it exists */ 55914ff6286SJan Kara read_lock(&journal->j_state_lock); 56014ff6286SJan Kara __jbd2_journal_unreserve_handle(handle, journal->j_running_transaction); 56114ff6286SJan Kara read_unlock(&journal->j_state_lock); 5628f7d89f3SJan Kara jbd2_free_handle(handle); 5638f7d89f3SJan Kara } 5648f7d89f3SJan Kara EXPORT_SYMBOL(jbd2_journal_free_reserved); 5658f7d89f3SJan Kara 5668f7d89f3SJan Kara /** 567f69120ceSTobin C. Harding * int jbd2_journal_start_reserved() - start reserved handle 5688f7d89f3SJan Kara * @handle: handle to start 569f69120ceSTobin C. Harding * @type: for handle statistics 570f69120ceSTobin C. Harding * @line_no: for handle statistics 5718f7d89f3SJan Kara * 5728f7d89f3SJan Kara * Start handle that has been previously reserved with jbd2_journal_reserve(). 5738f7d89f3SJan Kara * This attaches @handle to the running transaction (or creates one if there's 5748f7d89f3SJan Kara * not transaction running). Unlike jbd2_journal_start() this function cannot 5758f7d89f3SJan Kara * block on journal commit, checkpointing, or similar stuff. It can block on 5768f7d89f3SJan Kara * memory allocation or frozen journal though. 5778f7d89f3SJan Kara * 5788f7d89f3SJan Kara * Return 0 on success, non-zero on error - handle is freed in that case. 5798f7d89f3SJan Kara */ 5808f7d89f3SJan Kara int jbd2_journal_start_reserved(handle_t *handle, unsigned int type, 5818f7d89f3SJan Kara unsigned int line_no) 5828f7d89f3SJan Kara { 5838f7d89f3SJan Kara journal_t *journal = handle->h_journal; 5848f7d89f3SJan Kara int ret = -EIO; 5858f7d89f3SJan Kara 5868f7d89f3SJan Kara if (WARN_ON(!handle->h_reserved)) { 5878f7d89f3SJan Kara /* Someone passed in normal handle? Just stop it. */ 5888f7d89f3SJan Kara jbd2_journal_stop(handle); 5898f7d89f3SJan Kara return ret; 5908f7d89f3SJan Kara } 5918f7d89f3SJan Kara /* 5928f7d89f3SJan Kara * Usefulness of mixing of reserved and unreserved handles is 5938f7d89f3SJan Kara * questionable. So far nobody seems to need it so just error out. 5948f7d89f3SJan Kara */ 5958f7d89f3SJan Kara if (WARN_ON(current->journal_info)) { 5968f7d89f3SJan Kara jbd2_journal_free_reserved(handle); 5978f7d89f3SJan Kara return ret; 5988f7d89f3SJan Kara } 5998f7d89f3SJan Kara 6008f7d89f3SJan Kara handle->h_journal = NULL; 6018f7d89f3SJan Kara /* 6028f7d89f3SJan Kara * GFP_NOFS is here because callers are likely from writeback or 6038f7d89f3SJan Kara * similarly constrained call sites 6048f7d89f3SJan Kara */ 6058f7d89f3SJan Kara ret = start_this_handle(journal, handle, GFP_NOFS); 60692e3b405SDan Carpenter if (ret < 0) { 607b2569260STheodore Ts'o handle->h_journal = journal; 6088f7d89f3SJan Kara jbd2_journal_free_reserved(handle); 60992e3b405SDan Carpenter return ret; 61092e3b405SDan Carpenter } 6118f7d89f3SJan Kara handle->h_type = type; 6128f7d89f3SJan Kara handle->h_line_no = line_no; 6134c273352SXiaoguang Wang trace_jbd2_handle_start(journal->j_fs_dev->bd_dev, 6144c273352SXiaoguang Wang handle->h_transaction->t_tid, type, 615933f1c1eSJan Kara line_no, handle->h_total_credits); 61692e3b405SDan Carpenter return 0; 6178f7d89f3SJan Kara } 6188f7d89f3SJan Kara EXPORT_SYMBOL(jbd2_journal_start_reserved); 619470decc6SDave Kleikamp 620470decc6SDave Kleikamp /** 621f7f4bccbSMingming Cao * int jbd2_journal_extend() - extend buffer credits. 622470decc6SDave Kleikamp * @handle: handle to 'extend' 623470decc6SDave Kleikamp * @nblocks: nr blocks to try to extend by. 624fdc3ef88SJan Kara * @revoke_records: number of revoke records to try to extend by. 625470decc6SDave Kleikamp * 626470decc6SDave Kleikamp * Some transactions, such as large extends and truncates, can be done 627470decc6SDave Kleikamp * atomically all at once or in several stages. The operation requests 628bd7ced98SMasanari Iida * a credit for a number of buffer modifications in advance, but can 629470decc6SDave Kleikamp * extend its credit if it needs more. 630470decc6SDave Kleikamp * 631f7f4bccbSMingming Cao * jbd2_journal_extend tries to give the running handle more buffer credits. 632470decc6SDave Kleikamp * It does not guarantee that allocation - this is a best-effort only. 633470decc6SDave Kleikamp * The calling process MUST be able to deal cleanly with a failure to 634470decc6SDave Kleikamp * extend here. 635470decc6SDave Kleikamp * 636470decc6SDave Kleikamp * Return 0 on success, non-zero on failure. 637470decc6SDave Kleikamp * 638470decc6SDave Kleikamp * return code < 0 implies an error 639470decc6SDave Kleikamp * return code > 0 implies normal transaction-full status. 640470decc6SDave Kleikamp */ 641fdc3ef88SJan Kara int jbd2_journal_extend(handle_t *handle, int nblocks, int revoke_records) 642470decc6SDave Kleikamp { 643470decc6SDave Kleikamp transaction_t *transaction = handle->h_transaction; 64441a5b913STheodore Ts'o journal_t *journal; 645470decc6SDave Kleikamp int result; 646470decc6SDave Kleikamp int wanted; 647470decc6SDave Kleikamp 648470decc6SDave Kleikamp if (is_handle_aborted(handle)) 64941a5b913STheodore Ts'o return -EROFS; 65041a5b913STheodore Ts'o journal = transaction->t_journal; 651470decc6SDave Kleikamp 652470decc6SDave Kleikamp result = 1; 653470decc6SDave Kleikamp 654a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 655470decc6SDave Kleikamp 656470decc6SDave Kleikamp /* Don't extend a locked-down transaction! */ 65741a5b913STheodore Ts'o if (transaction->t_state != T_RUNNING) { 658470decc6SDave Kleikamp jbd_debug(3, "denied handle %p %d blocks: " 659470decc6SDave Kleikamp "transaction not running\n", handle, nblocks); 660470decc6SDave Kleikamp goto error_out; 661470decc6SDave Kleikamp } 662470decc6SDave Kleikamp 663fdc3ef88SJan Kara nblocks += DIV_ROUND_UP( 664fdc3ef88SJan Kara handle->h_revoke_credits_requested + revoke_records, 665fdc3ef88SJan Kara journal->j_revoke_records_per_block) - 666fdc3ef88SJan Kara DIV_ROUND_UP( 667fdc3ef88SJan Kara handle->h_revoke_credits_requested, 668fdc3ef88SJan Kara journal->j_revoke_records_per_block); 669470decc6SDave Kleikamp spin_lock(&transaction->t_handle_lock); 670fe1e8db5SJan Kara wanted = atomic_add_return(nblocks, 671fe1e8db5SJan Kara &transaction->t_outstanding_credits); 672470decc6SDave Kleikamp 673470decc6SDave Kleikamp if (wanted > journal->j_max_transaction_buffers) { 674470decc6SDave Kleikamp jbd_debug(3, "denied handle %p %d blocks: " 675470decc6SDave Kleikamp "transaction too large\n", handle, nblocks); 676fe1e8db5SJan Kara atomic_sub(nblocks, &transaction->t_outstanding_credits); 677470decc6SDave Kleikamp goto unlock; 678470decc6SDave Kleikamp } 679470decc6SDave Kleikamp 680343d9c28STheodore Ts'o trace_jbd2_handle_extend(journal->j_fs_dev->bd_dev, 68141a5b913STheodore Ts'o transaction->t_tid, 682343d9c28STheodore Ts'o handle->h_type, handle->h_line_no, 683933f1c1eSJan Kara handle->h_total_credits, 684343d9c28STheodore Ts'o nblocks); 685343d9c28STheodore Ts'o 686933f1c1eSJan Kara handle->h_total_credits += nblocks; 687343d9c28STheodore Ts'o handle->h_requested_credits += nblocks; 688fdc3ef88SJan Kara handle->h_revoke_credits += revoke_records; 689fdc3ef88SJan Kara handle->h_revoke_credits_requested += revoke_records; 690470decc6SDave Kleikamp result = 0; 691470decc6SDave Kleikamp 692470decc6SDave Kleikamp jbd_debug(3, "extended handle %p by %d\n", handle, nblocks); 693470decc6SDave Kleikamp unlock: 694470decc6SDave Kleikamp spin_unlock(&transaction->t_handle_lock); 695470decc6SDave Kleikamp error_out: 696a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 697470decc6SDave Kleikamp return result; 698470decc6SDave Kleikamp } 699470decc6SDave Kleikamp 700ec8b6f60SJan Kara static void stop_this_handle(handle_t *handle) 701ec8b6f60SJan Kara { 702ec8b6f60SJan Kara transaction_t *transaction = handle->h_transaction; 703ec8b6f60SJan Kara journal_t *journal = transaction->t_journal; 704fdc3ef88SJan Kara int revokes; 705ec8b6f60SJan Kara 706ec8b6f60SJan Kara J_ASSERT(journal_current_handle() == handle); 707ec8b6f60SJan Kara J_ASSERT(atomic_read(&transaction->t_updates) > 0); 708ec8b6f60SJan Kara current->journal_info = NULL; 709fdc3ef88SJan Kara /* 710fdc3ef88SJan Kara * Subtract necessary revoke descriptor blocks from handle credits. We 711fdc3ef88SJan Kara * take care to account only for revoke descriptor blocks the 712fdc3ef88SJan Kara * transaction will really need as large sequences of transactions with 713fdc3ef88SJan Kara * small numbers of revokes are relatively common. 714fdc3ef88SJan Kara */ 715fdc3ef88SJan Kara revokes = handle->h_revoke_credits_requested - handle->h_revoke_credits; 716fdc3ef88SJan Kara if (revokes) { 717fdc3ef88SJan Kara int t_revokes, revoke_descriptors; 718fdc3ef88SJan Kara int rr_per_blk = journal->j_revoke_records_per_block; 719fdc3ef88SJan Kara 720fdc3ef88SJan Kara WARN_ON_ONCE(DIV_ROUND_UP(revokes, rr_per_blk) 721933f1c1eSJan Kara > handle->h_total_credits); 722fdc3ef88SJan Kara t_revokes = atomic_add_return(revokes, 723fdc3ef88SJan Kara &transaction->t_outstanding_revokes); 724fdc3ef88SJan Kara revoke_descriptors = 725fdc3ef88SJan Kara DIV_ROUND_UP(t_revokes, rr_per_blk) - 726fdc3ef88SJan Kara DIV_ROUND_UP(t_revokes - revokes, rr_per_blk); 727933f1c1eSJan Kara handle->h_total_credits -= revoke_descriptors; 728fdc3ef88SJan Kara } 729933f1c1eSJan Kara atomic_sub(handle->h_total_credits, 730ec8b6f60SJan Kara &transaction->t_outstanding_credits); 731ec8b6f60SJan Kara if (handle->h_rsv_handle) 73214ff6286SJan Kara __jbd2_journal_unreserve_handle(handle->h_rsv_handle, 73314ff6286SJan Kara transaction); 734ec8b6f60SJan Kara if (atomic_dec_and_test(&transaction->t_updates)) 735ec8b6f60SJan Kara wake_up(&journal->j_wait_updates); 736ec8b6f60SJan Kara 73750b8b3f8SLinus Torvalds rwsem_release(&journal->j_trans_commit_map, _THIS_IP_); 738ec8b6f60SJan Kara /* 739ec8b6f60SJan Kara * Scope of the GFP_NOFS context is over here and so we can restore the 740ec8b6f60SJan Kara * original alloc context. 741ec8b6f60SJan Kara */ 742ec8b6f60SJan Kara memalloc_nofs_restore(handle->saved_alloc_context); 743ec8b6f60SJan Kara } 744470decc6SDave Kleikamp 745470decc6SDave Kleikamp /** 746f7f4bccbSMingming Cao * int jbd2_journal_restart() - restart a handle . 747470decc6SDave Kleikamp * @handle: handle to restart 748470decc6SDave Kleikamp * @nblocks: nr credits requested 749fdc3ef88SJan Kara * @revoke_records: number of revoke record credits requested 750f69120ceSTobin C. Harding * @gfp_mask: memory allocation flags (for start_this_handle) 751470decc6SDave Kleikamp * 752470decc6SDave Kleikamp * Restart a handle for a multi-transaction filesystem 753470decc6SDave Kleikamp * operation. 754470decc6SDave Kleikamp * 755f7f4bccbSMingming Cao * If the jbd2_journal_extend() call above fails to grant new buffer credits 756f7f4bccbSMingming Cao * to a running handle, a call to jbd2_journal_restart will commit the 757470decc6SDave Kleikamp * handle's transaction so far and reattach the handle to a new 758bd7ced98SMasanari Iida * transaction capable of guaranteeing the requested number of 7598f7d89f3SJan Kara * credits. We preserve reserved handle if there's any attached to the 7608f7d89f3SJan Kara * passed in handle. 761470decc6SDave Kleikamp */ 762fdc3ef88SJan Kara int jbd2__journal_restart(handle_t *handle, int nblocks, int revoke_records, 763fdc3ef88SJan Kara gfp_t gfp_mask) 764470decc6SDave Kleikamp { 765470decc6SDave Kleikamp transaction_t *transaction = handle->h_transaction; 76641a5b913STheodore Ts'o journal_t *journal; 767e4471831STheodore Ts'o tid_t tid; 768ec8b6f60SJan Kara int need_to_start; 7690094f981SJan Kara int ret; 770470decc6SDave Kleikamp 771470decc6SDave Kleikamp /* If we've had an abort of any type, don't even think about 772470decc6SDave Kleikamp * actually doing the restart! */ 773470decc6SDave Kleikamp if (is_handle_aborted(handle)) 774470decc6SDave Kleikamp return 0; 77541a5b913STheodore Ts'o journal = transaction->t_journal; 776ec8b6f60SJan Kara tid = transaction->t_tid; 777470decc6SDave Kleikamp 778470decc6SDave Kleikamp /* 779470decc6SDave Kleikamp * First unlink the handle from its current transaction, and start the 780470decc6SDave Kleikamp * commit on that. 781470decc6SDave Kleikamp */ 782470decc6SDave Kleikamp jbd_debug(2, "restarting handle %p\n", handle); 783ec8b6f60SJan Kara stop_this_handle(handle); 784ec8b6f60SJan Kara handle->h_transaction = NULL; 785ec8b6f60SJan Kara 786ec8b6f60SJan Kara /* 787ec8b6f60SJan Kara * TODO: If we use READ_ONCE / WRITE_ONCE for j_commit_request we can 788ec8b6f60SJan Kara * get rid of pointless j_state_lock traffic like this. 789ec8b6f60SJan Kara */ 790ec8b6f60SJan Kara read_lock(&journal->j_state_lock); 791e4471831STheodore Ts'o need_to_start = !tid_geq(journal->j_commit_request, tid); 792a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 793e4471831STheodore Ts'o if (need_to_start) 794e4471831STheodore Ts'o jbd2_log_start_commit(journal, tid); 795933f1c1eSJan Kara handle->h_total_credits = nblocks + 796fdc3ef88SJan Kara DIV_ROUND_UP(revoke_records, 797fdc3ef88SJan Kara journal->j_revoke_records_per_block); 798fdc3ef88SJan Kara handle->h_revoke_credits = revoke_records; 79947def826STheodore Ts'o ret = start_this_handle(journal, handle, gfp_mask); 8000094f981SJan Kara trace_jbd2_handle_restart(journal->j_fs_dev->bd_dev, 8010094f981SJan Kara ret ? 0 : handle->h_transaction->t_tid, 8020094f981SJan Kara handle->h_type, handle->h_line_no, 8030094f981SJan Kara handle->h_total_credits); 804470decc6SDave Kleikamp return ret; 805470decc6SDave Kleikamp } 80647def826STheodore Ts'o EXPORT_SYMBOL(jbd2__journal_restart); 807470decc6SDave Kleikamp 808470decc6SDave Kleikamp 80947def826STheodore Ts'o int jbd2_journal_restart(handle_t *handle, int nblocks) 81047def826STheodore Ts'o { 811fdc3ef88SJan Kara return jbd2__journal_restart(handle, nblocks, 0, GFP_NOFS); 81247def826STheodore Ts'o } 81347def826STheodore Ts'o EXPORT_SYMBOL(jbd2_journal_restart); 81447def826STheodore Ts'o 815470decc6SDave Kleikamp /** 816f7f4bccbSMingming Cao * void jbd2_journal_lock_updates () - establish a transaction barrier. 817470decc6SDave Kleikamp * @journal: Journal to establish a barrier on. 818470decc6SDave Kleikamp * 819470decc6SDave Kleikamp * This locks out any further updates from being started, and blocks 820470decc6SDave Kleikamp * until all existing updates have completed, returning only once the 821470decc6SDave Kleikamp * journal is in a quiescent state with no updates running. 822470decc6SDave Kleikamp * 823470decc6SDave Kleikamp * The journal lock should not be held on entry. 824470decc6SDave Kleikamp */ 825f7f4bccbSMingming Cao void jbd2_journal_lock_updates(journal_t *journal) 826470decc6SDave Kleikamp { 827470decc6SDave Kleikamp DEFINE_WAIT(wait); 828470decc6SDave Kleikamp 8291eaa566dSJan Kara jbd2_might_wait_for_commit(journal); 8301eaa566dSJan Kara 831a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 832470decc6SDave Kleikamp ++journal->j_barrier_count; 833470decc6SDave Kleikamp 8348f7d89f3SJan Kara /* Wait until there are no reserved handles */ 8358f7d89f3SJan Kara if (atomic_read(&journal->j_reserved_credits)) { 8368f7d89f3SJan Kara write_unlock(&journal->j_state_lock); 8378f7d89f3SJan Kara wait_event(journal->j_wait_reserved, 8388f7d89f3SJan Kara atomic_read(&journal->j_reserved_credits) == 0); 8398f7d89f3SJan Kara write_lock(&journal->j_state_lock); 8408f7d89f3SJan Kara } 8418f7d89f3SJan Kara 842470decc6SDave Kleikamp /* Wait until there are no running updates */ 843470decc6SDave Kleikamp while (1) { 844470decc6SDave Kleikamp transaction_t *transaction = journal->j_running_transaction; 845470decc6SDave Kleikamp 846470decc6SDave Kleikamp if (!transaction) 847470decc6SDave Kleikamp break; 848470decc6SDave Kleikamp 849470decc6SDave Kleikamp spin_lock(&transaction->t_handle_lock); 850470decc6SDave Kleikamp prepare_to_wait(&journal->j_wait_updates, &wait, 851470decc6SDave Kleikamp TASK_UNINTERRUPTIBLE); 8529837d8e9SJan Kara if (!atomic_read(&transaction->t_updates)) { 8539837d8e9SJan Kara spin_unlock(&transaction->t_handle_lock); 8549837d8e9SJan Kara finish_wait(&journal->j_wait_updates, &wait); 8559837d8e9SJan Kara break; 8569837d8e9SJan Kara } 857470decc6SDave Kleikamp spin_unlock(&transaction->t_handle_lock); 858a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 859470decc6SDave Kleikamp schedule(); 860470decc6SDave Kleikamp finish_wait(&journal->j_wait_updates, &wait); 861a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 862470decc6SDave Kleikamp } 863a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 864470decc6SDave Kleikamp 865470decc6SDave Kleikamp /* 866470decc6SDave Kleikamp * We have now established a barrier against other normal updates, but 867f7f4bccbSMingming Cao * we also need to barrier against other jbd2_journal_lock_updates() calls 868470decc6SDave Kleikamp * to make sure that we serialise special journal-locked operations 869470decc6SDave Kleikamp * too. 870470decc6SDave Kleikamp */ 871470decc6SDave Kleikamp mutex_lock(&journal->j_barrier); 872470decc6SDave Kleikamp } 873470decc6SDave Kleikamp 874470decc6SDave Kleikamp /** 875f7f4bccbSMingming Cao * void jbd2_journal_unlock_updates (journal_t* journal) - release barrier 876470decc6SDave Kleikamp * @journal: Journal to release the barrier on. 877470decc6SDave Kleikamp * 878f7f4bccbSMingming Cao * Release a transaction barrier obtained with jbd2_journal_lock_updates(). 879470decc6SDave Kleikamp * 880470decc6SDave Kleikamp * Should be called without the journal lock held. 881470decc6SDave Kleikamp */ 882f7f4bccbSMingming Cao void jbd2_journal_unlock_updates (journal_t *journal) 883470decc6SDave Kleikamp { 884470decc6SDave Kleikamp J_ASSERT(journal->j_barrier_count != 0); 885470decc6SDave Kleikamp 886470decc6SDave Kleikamp mutex_unlock(&journal->j_barrier); 887a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 888470decc6SDave Kleikamp --journal->j_barrier_count; 889a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 890470decc6SDave Kleikamp wake_up(&journal->j_wait_transaction_locked); 891470decc6SDave Kleikamp } 892470decc6SDave Kleikamp 893f91d1d04SJan Kara static void warn_dirty_buffer(struct buffer_head *bh) 894470decc6SDave Kleikamp { 895f91d1d04SJan Kara printk(KERN_WARNING 896a1c6f057SDmitry Monakhov "JBD2: Spotted dirty metadata buffer (dev = %pg, blocknr = %llu). " 897f91d1d04SJan Kara "There's a risk of filesystem corruption in case of system " 898f91d1d04SJan Kara "crash.\n", 899a1c6f057SDmitry Monakhov bh->b_bdev, (unsigned long long)bh->b_blocknr); 900470decc6SDave Kleikamp } 901470decc6SDave Kleikamp 902ee57aba1SJan Kara /* Call t_frozen trigger and copy buffer data into jh->b_frozen_data. */ 903ee57aba1SJan Kara static void jbd2_freeze_jh_data(struct journal_head *jh) 904ee57aba1SJan Kara { 905ee57aba1SJan Kara struct page *page; 906ee57aba1SJan Kara int offset; 907ee57aba1SJan Kara char *source; 908ee57aba1SJan Kara struct buffer_head *bh = jh2bh(jh); 909ee57aba1SJan Kara 910ee57aba1SJan Kara J_EXPECT_JH(jh, buffer_uptodate(bh), "Possible IO failure.\n"); 911ee57aba1SJan Kara page = bh->b_page; 912ee57aba1SJan Kara offset = offset_in_page(bh->b_data); 913ee57aba1SJan Kara source = kmap_atomic(page); 914ee57aba1SJan Kara /* Fire data frozen trigger just before we copy the data */ 915ee57aba1SJan Kara jbd2_buffer_frozen_trigger(jh, source + offset, jh->b_triggers); 916ee57aba1SJan Kara memcpy(jh->b_frozen_data, source + offset, bh->b_size); 917ee57aba1SJan Kara kunmap_atomic(source); 918ee57aba1SJan Kara 919ee57aba1SJan Kara /* 920ee57aba1SJan Kara * Now that the frozen data is saved off, we need to store any matching 921ee57aba1SJan Kara * triggers. 922ee57aba1SJan Kara */ 923ee57aba1SJan Kara jh->b_frozen_triggers = jh->b_triggers; 924ee57aba1SJan Kara } 925ee57aba1SJan Kara 926470decc6SDave Kleikamp /* 927470decc6SDave Kleikamp * If the buffer is already part of the current transaction, then there 928470decc6SDave Kleikamp * is nothing we need to do. If it is already part of a prior 929470decc6SDave Kleikamp * transaction which we are still committing to disk, then we need to 930470decc6SDave Kleikamp * make sure that we do not overwrite the old copy: we do copy-out to 931470decc6SDave Kleikamp * preserve the copy going to disk. We also account the buffer against 932470decc6SDave Kleikamp * the handle's metadata buffer credits (unless the buffer is already 933470decc6SDave Kleikamp * part of the transaction, that is). 934470decc6SDave Kleikamp * 935470decc6SDave Kleikamp */ 936470decc6SDave Kleikamp static int 937470decc6SDave Kleikamp do_get_write_access(handle_t *handle, struct journal_head *jh, 938470decc6SDave Kleikamp int force_copy) 939470decc6SDave Kleikamp { 940470decc6SDave Kleikamp struct buffer_head *bh; 94141a5b913STheodore Ts'o transaction_t *transaction = handle->h_transaction; 942470decc6SDave Kleikamp journal_t *journal; 943470decc6SDave Kleikamp int error; 944470decc6SDave Kleikamp char *frozen_buffer = NULL; 945f783f091STheodore Ts'o unsigned long start_lock, time_lock; 946470decc6SDave Kleikamp 947470decc6SDave Kleikamp journal = transaction->t_journal; 948470decc6SDave Kleikamp 949cfef2c6aSTheodore Ts'o jbd_debug(5, "journal_head %p, force_copy %d\n", jh, force_copy); 950470decc6SDave Kleikamp 951470decc6SDave Kleikamp JBUFFER_TRACE(jh, "entry"); 952470decc6SDave Kleikamp repeat: 953470decc6SDave Kleikamp bh = jh2bh(jh); 954470decc6SDave Kleikamp 955470decc6SDave Kleikamp /* @@@ Need to check for errors here at some point. */ 956470decc6SDave Kleikamp 957f783f091STheodore Ts'o start_lock = jiffies; 958470decc6SDave Kleikamp lock_buffer(bh); 95946417064SThomas Gleixner spin_lock(&jh->b_state_lock); 960470decc6SDave Kleikamp 961f783f091STheodore Ts'o /* If it takes too long to lock the buffer, trace it */ 962f783f091STheodore Ts'o time_lock = jbd2_time_diff(start_lock, jiffies); 963f783f091STheodore Ts'o if (time_lock > HZ/10) 964f783f091STheodore Ts'o trace_jbd2_lock_buffer_stall(bh->b_bdev->bd_dev, 965f783f091STheodore Ts'o jiffies_to_msecs(time_lock)); 966f783f091STheodore Ts'o 967470decc6SDave Kleikamp /* We now hold the buffer lock so it is safe to query the buffer 968470decc6SDave Kleikamp * state. Is the buffer dirty? 969470decc6SDave Kleikamp * 970470decc6SDave Kleikamp * If so, there are two possibilities. The buffer may be 971470decc6SDave Kleikamp * non-journaled, and undergoing a quite legitimate writeback. 972470decc6SDave Kleikamp * Otherwise, it is journaled, and we don't expect dirty buffers 973470decc6SDave Kleikamp * in that state (the buffers should be marked JBD_Dirty 974470decc6SDave Kleikamp * instead.) So either the IO is being done under our own 975470decc6SDave Kleikamp * control and this is a bug, or it's a third party IO such as 976470decc6SDave Kleikamp * dump(8) (which may leave the buffer scheduled for read --- 977470decc6SDave Kleikamp * ie. locked but not dirty) or tune2fs (which may actually have 978470decc6SDave Kleikamp * the buffer dirtied, ugh.) */ 979470decc6SDave Kleikamp 980470decc6SDave Kleikamp if (buffer_dirty(bh)) { 981470decc6SDave Kleikamp /* 982470decc6SDave Kleikamp * First question: is this buffer already part of the current 983470decc6SDave Kleikamp * transaction or the existing committing transaction? 984470decc6SDave Kleikamp */ 985470decc6SDave Kleikamp if (jh->b_transaction) { 986470decc6SDave Kleikamp J_ASSERT_JH(jh, 987470decc6SDave Kleikamp jh->b_transaction == transaction || 988470decc6SDave Kleikamp jh->b_transaction == 989470decc6SDave Kleikamp journal->j_committing_transaction); 990470decc6SDave Kleikamp if (jh->b_next_transaction) 991470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_next_transaction == 992470decc6SDave Kleikamp transaction); 993f91d1d04SJan Kara warn_dirty_buffer(bh); 994470decc6SDave Kleikamp } 995470decc6SDave Kleikamp /* 996470decc6SDave Kleikamp * In any case we need to clean the dirty flag and we must 997470decc6SDave Kleikamp * do it under the buffer lock to be sure we don't race 998470decc6SDave Kleikamp * with running write-out. 999470decc6SDave Kleikamp */ 1000f91d1d04SJan Kara JBUFFER_TRACE(jh, "Journalling dirty buffer"); 1001f91d1d04SJan Kara clear_buffer_dirty(bh); 1002f91d1d04SJan Kara set_buffer_jbddirty(bh); 1003470decc6SDave Kleikamp } 1004470decc6SDave Kleikamp 1005470decc6SDave Kleikamp unlock_buffer(bh); 1006470decc6SDave Kleikamp 1007470decc6SDave Kleikamp error = -EROFS; 1008470decc6SDave Kleikamp if (is_handle_aborted(handle)) { 100946417064SThomas Gleixner spin_unlock(&jh->b_state_lock); 1010470decc6SDave Kleikamp goto out; 1011470decc6SDave Kleikamp } 1012470decc6SDave Kleikamp error = 0; 1013470decc6SDave Kleikamp 1014470decc6SDave Kleikamp /* 1015470decc6SDave Kleikamp * The buffer is already part of this transaction if b_transaction or 1016470decc6SDave Kleikamp * b_next_transaction points to it 1017470decc6SDave Kleikamp */ 1018470decc6SDave Kleikamp if (jh->b_transaction == transaction || 1019470decc6SDave Kleikamp jh->b_next_transaction == transaction) 1020470decc6SDave Kleikamp goto done; 1021470decc6SDave Kleikamp 1022470decc6SDave Kleikamp /* 10239fc7c63aSJosef Bacik * this is the first time this transaction is touching this buffer, 10249fc7c63aSJosef Bacik * reset the modified flag 10259fc7c63aSJosef Bacik */ 10269fc7c63aSJosef Bacik jh->b_modified = 0; 10279fc7c63aSJosef Bacik 10289fc7c63aSJosef Bacik /* 10298b00f400SJan Kara * If the buffer is not journaled right now, we need to make sure it 10308b00f400SJan Kara * doesn't get written to disk before the caller actually commits the 10318b00f400SJan Kara * new data 10328b00f400SJan Kara */ 10338b00f400SJan Kara if (!jh->b_transaction) { 10348b00f400SJan Kara JBUFFER_TRACE(jh, "no transaction"); 10358b00f400SJan Kara J_ASSERT_JH(jh, !jh->b_next_transaction); 10368b00f400SJan Kara JBUFFER_TRACE(jh, "file as BJ_Reserved"); 1037de92c8caSJan Kara /* 1038de92c8caSJan Kara * Make sure all stores to jh (b_modified, b_frozen_data) are 1039de92c8caSJan Kara * visible before attaching it to the running transaction. 1040de92c8caSJan Kara * Paired with barrier in jbd2_write_access_granted() 1041de92c8caSJan Kara */ 1042de92c8caSJan Kara smp_wmb(); 10438b00f400SJan Kara spin_lock(&journal->j_list_lock); 10448b00f400SJan Kara __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved); 10458b00f400SJan Kara spin_unlock(&journal->j_list_lock); 10468b00f400SJan Kara goto done; 10478b00f400SJan Kara } 10488b00f400SJan Kara /* 1049470decc6SDave Kleikamp * If there is already a copy-out version of this buffer, then we don't 1050470decc6SDave Kleikamp * need to make another one 1051470decc6SDave Kleikamp */ 1052470decc6SDave Kleikamp if (jh->b_frozen_data) { 1053470decc6SDave Kleikamp JBUFFER_TRACE(jh, "has frozen data"); 1054470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_next_transaction == NULL); 1055de92c8caSJan Kara goto attach_next; 1056470decc6SDave Kleikamp } 1057470decc6SDave Kleikamp 1058470decc6SDave Kleikamp JBUFFER_TRACE(jh, "owned by older transaction"); 1059470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_next_transaction == NULL); 10608b00f400SJan Kara J_ASSERT_JH(jh, jh->b_transaction == journal->j_committing_transaction); 1061470decc6SDave Kleikamp 10628b00f400SJan Kara /* 10638b00f400SJan Kara * There is one case we have to be very careful about. If the 10648b00f400SJan Kara * committing transaction is currently writing this buffer out to disk 10658b00f400SJan Kara * and has NOT made a copy-out, then we cannot modify the buffer 10668b00f400SJan Kara * contents at all right now. The essence of copy-out is that it is 10678b00f400SJan Kara * the extra copy, not the primary copy, which gets journaled. If the 10688b00f400SJan Kara * primary copy is already going to disk then we cannot do copy-out 10698b00f400SJan Kara * here. 10708b00f400SJan Kara */ 1071b34090e5SJan Kara if (buffer_shadow(bh)) { 1072470decc6SDave Kleikamp JBUFFER_TRACE(jh, "on shadow: sleep"); 107346417064SThomas Gleixner spin_unlock(&jh->b_state_lock); 10748b00f400SJan Kara wait_on_bit_io(&bh->b_state, BH_Shadow, TASK_UNINTERRUPTIBLE); 1075470decc6SDave Kleikamp goto repeat; 1076470decc6SDave Kleikamp } 1077470decc6SDave Kleikamp 1078b34090e5SJan Kara /* 10798b00f400SJan Kara * Only do the copy if the currently-owning transaction still needs it. 10808b00f400SJan Kara * If buffer isn't on BJ_Metadata list, the committing transaction is 10818b00f400SJan Kara * past that stage (here we use the fact that BH_Shadow is set under 10828b00f400SJan Kara * bh_state lock together with refiling to BJ_Shadow list and at this 10838b00f400SJan Kara * point we know the buffer doesn't have BH_Shadow set). 1084470decc6SDave Kleikamp * 10858b00f400SJan Kara * Subtle point, though: if this is a get_undo_access, then we will be 10868b00f400SJan Kara * relying on the frozen_data to contain the new value of the 10878b00f400SJan Kara * committed_data record after the transaction, so we HAVE to force the 10888b00f400SJan Kara * frozen_data copy in that case. 1089b34090e5SJan Kara */ 1090b34090e5SJan Kara if (jh->b_jlist == BJ_Metadata || force_copy) { 1091470decc6SDave Kleikamp JBUFFER_TRACE(jh, "generate frozen data"); 1092470decc6SDave Kleikamp if (!frozen_buffer) { 1093470decc6SDave Kleikamp JBUFFER_TRACE(jh, "allocate memory for buffer"); 109446417064SThomas Gleixner spin_unlock(&jh->b_state_lock); 1095490c1b44SMichal Hocko frozen_buffer = jbd2_alloc(jh2bh(jh)->b_size, 1096490c1b44SMichal Hocko GFP_NOFS | __GFP_NOFAIL); 1097470decc6SDave Kleikamp goto repeat; 1098470decc6SDave Kleikamp } 1099470decc6SDave Kleikamp jh->b_frozen_data = frozen_buffer; 1100470decc6SDave Kleikamp frozen_buffer = NULL; 1101ee57aba1SJan Kara jbd2_freeze_jh_data(jh); 1102470decc6SDave Kleikamp } 1103de92c8caSJan Kara attach_next: 1104de92c8caSJan Kara /* 1105de92c8caSJan Kara * Make sure all stores to jh (b_modified, b_frozen_data) are visible 1106de92c8caSJan Kara * before attaching it to the running transaction. Paired with barrier 1107de92c8caSJan Kara * in jbd2_write_access_granted() 1108de92c8caSJan Kara */ 1109de92c8caSJan Kara smp_wmb(); 1110470decc6SDave Kleikamp jh->b_next_transaction = transaction; 1111470decc6SDave Kleikamp 1112470decc6SDave Kleikamp done: 111346417064SThomas Gleixner spin_unlock(&jh->b_state_lock); 1114470decc6SDave Kleikamp 1115470decc6SDave Kleikamp /* 1116470decc6SDave Kleikamp * If we are about to journal a buffer, then any revoke pending on it is 1117470decc6SDave Kleikamp * no longer valid 1118470decc6SDave Kleikamp */ 1119f7f4bccbSMingming Cao jbd2_journal_cancel_revoke(handle, jh); 1120470decc6SDave Kleikamp 1121470decc6SDave Kleikamp out: 1122470decc6SDave Kleikamp if (unlikely(frozen_buffer)) /* It's usually NULL */ 1123af1e76d6SMingming Cao jbd2_free(frozen_buffer, bh->b_size); 1124470decc6SDave Kleikamp 1125470decc6SDave Kleikamp JBUFFER_TRACE(jh, "exit"); 1126470decc6SDave Kleikamp return error; 1127470decc6SDave Kleikamp } 1128470decc6SDave Kleikamp 1129de92c8caSJan Kara /* Fast check whether buffer is already attached to the required transaction */ 1130087ffd4eSJunxiao Bi static bool jbd2_write_access_granted(handle_t *handle, struct buffer_head *bh, 1131087ffd4eSJunxiao Bi bool undo) 1132de92c8caSJan Kara { 1133de92c8caSJan Kara struct journal_head *jh; 1134de92c8caSJan Kara bool ret = false; 1135de92c8caSJan Kara 1136de92c8caSJan Kara /* Dirty buffers require special handling... */ 1137de92c8caSJan Kara if (buffer_dirty(bh)) 1138de92c8caSJan Kara return false; 1139de92c8caSJan Kara 1140de92c8caSJan Kara /* 1141de92c8caSJan Kara * RCU protects us from dereferencing freed pages. So the checks we do 1142de92c8caSJan Kara * are guaranteed not to oops. However the jh slab object can get freed 1143de92c8caSJan Kara * & reallocated while we work with it. So we have to be careful. When 1144de92c8caSJan Kara * we see jh attached to the running transaction, we know it must stay 1145de92c8caSJan Kara * so until the transaction is committed. Thus jh won't be freed and 1146de92c8caSJan Kara * will be attached to the same bh while we run. However it can 1147de92c8caSJan Kara * happen jh gets freed, reallocated, and attached to the transaction 1148de92c8caSJan Kara * just after we get pointer to it from bh. So we have to be careful 1149de92c8caSJan Kara * and recheck jh still belongs to our bh before we return success. 1150de92c8caSJan Kara */ 1151de92c8caSJan Kara rcu_read_lock(); 1152de92c8caSJan Kara if (!buffer_jbd(bh)) 1153de92c8caSJan Kara goto out; 1154de92c8caSJan Kara /* This should be bh2jh() but that doesn't work with inline functions */ 1155de92c8caSJan Kara jh = READ_ONCE(bh->b_private); 1156de92c8caSJan Kara if (!jh) 1157de92c8caSJan Kara goto out; 1158087ffd4eSJunxiao Bi /* For undo access buffer must have data copied */ 1159087ffd4eSJunxiao Bi if (undo && !jh->b_committed_data) 1160087ffd4eSJunxiao Bi goto out; 11616c5d9112SQian Cai if (READ_ONCE(jh->b_transaction) != handle->h_transaction && 11626c5d9112SQian Cai READ_ONCE(jh->b_next_transaction) != handle->h_transaction) 1163de92c8caSJan Kara goto out; 1164de92c8caSJan Kara /* 1165de92c8caSJan Kara * There are two reasons for the barrier here: 1166de92c8caSJan Kara * 1) Make sure to fetch b_bh after we did previous checks so that we 1167de92c8caSJan Kara * detect when jh went through free, realloc, attach to transaction 1168de92c8caSJan Kara * while we were checking. Paired with implicit barrier in that path. 1169de92c8caSJan Kara * 2) So that access to bh done after jbd2_write_access_granted() 1170de92c8caSJan Kara * doesn't get reordered and see inconsistent state of concurrent 1171de92c8caSJan Kara * do_get_write_access(). 1172de92c8caSJan Kara */ 1173de92c8caSJan Kara smp_mb(); 1174de92c8caSJan Kara if (unlikely(jh->b_bh != bh)) 1175de92c8caSJan Kara goto out; 1176de92c8caSJan Kara ret = true; 1177de92c8caSJan Kara out: 1178de92c8caSJan Kara rcu_read_unlock(); 1179de92c8caSJan Kara return ret; 1180de92c8caSJan Kara } 1181de92c8caSJan Kara 1182470decc6SDave Kleikamp /** 1183f7f4bccbSMingming Cao * int jbd2_journal_get_write_access() - notify intent to modify a buffer for metadata (not data) update. 1184470decc6SDave Kleikamp * @handle: transaction to add buffer modifications to 1185470decc6SDave Kleikamp * @bh: bh to be used for metadata writes 1186470decc6SDave Kleikamp * 1187df1b560aSMauro Carvalho Chehab * Returns: error code or 0 on success. 1188470decc6SDave Kleikamp * 1189470decc6SDave Kleikamp * In full data journalling mode the buffer may be of type BJ_AsyncData, 1190df1b560aSMauro Carvalho Chehab * because we're ``write()ing`` a buffer which is also part of a shared mapping. 1191470decc6SDave Kleikamp */ 1192470decc6SDave Kleikamp 1193f7f4bccbSMingming Cao int jbd2_journal_get_write_access(handle_t *handle, struct buffer_head *bh) 1194470decc6SDave Kleikamp { 1195de92c8caSJan Kara struct journal_head *jh; 1196470decc6SDave Kleikamp int rc; 1197470decc6SDave Kleikamp 11988eedabfdSwangyan if (is_handle_aborted(handle)) 11998eedabfdSwangyan return -EROFS; 12008eedabfdSwangyan 1201087ffd4eSJunxiao Bi if (jbd2_write_access_granted(handle, bh, false)) 1202de92c8caSJan Kara return 0; 1203de92c8caSJan Kara 1204de92c8caSJan Kara jh = jbd2_journal_add_journal_head(bh); 1205470decc6SDave Kleikamp /* We do not want to get caught playing with fields which the 1206470decc6SDave Kleikamp * log thread also manipulates. Make sure that the buffer 1207470decc6SDave Kleikamp * completes any outstanding IO before proceeding. */ 1208470decc6SDave Kleikamp rc = do_get_write_access(handle, jh, 0); 1209f7f4bccbSMingming Cao jbd2_journal_put_journal_head(jh); 1210470decc6SDave Kleikamp return rc; 1211470decc6SDave Kleikamp } 1212470decc6SDave Kleikamp 1213470decc6SDave Kleikamp 1214470decc6SDave Kleikamp /* 1215470decc6SDave Kleikamp * When the user wants to journal a newly created buffer_head 1216470decc6SDave Kleikamp * (ie. getblk() returned a new buffer and we are going to populate it 1217470decc6SDave Kleikamp * manually rather than reading off disk), then we need to keep the 1218470decc6SDave Kleikamp * buffer_head locked until it has been completely filled with new 1219470decc6SDave Kleikamp * data. In this case, we should be able to make the assertion that 1220470decc6SDave Kleikamp * the bh is not already part of an existing transaction. 1221470decc6SDave Kleikamp * 1222470decc6SDave Kleikamp * The buffer should already be locked by the caller by this point. 1223470decc6SDave Kleikamp * There is no lock ranking violation: it was a newly created, 1224470decc6SDave Kleikamp * unlocked buffer beforehand. */ 1225470decc6SDave Kleikamp 1226470decc6SDave Kleikamp /** 1227f7f4bccbSMingming Cao * int jbd2_journal_get_create_access () - notify intent to use newly created bh 1228470decc6SDave Kleikamp * @handle: transaction to new buffer to 1229470decc6SDave Kleikamp * @bh: new buffer. 1230470decc6SDave Kleikamp * 1231470decc6SDave Kleikamp * Call this if you create a new bh. 1232470decc6SDave Kleikamp */ 1233f7f4bccbSMingming Cao int jbd2_journal_get_create_access(handle_t *handle, struct buffer_head *bh) 1234470decc6SDave Kleikamp { 1235470decc6SDave Kleikamp transaction_t *transaction = handle->h_transaction; 123641a5b913STheodore Ts'o journal_t *journal; 1237f7f4bccbSMingming Cao struct journal_head *jh = jbd2_journal_add_journal_head(bh); 1238470decc6SDave Kleikamp int err; 1239470decc6SDave Kleikamp 1240470decc6SDave Kleikamp jbd_debug(5, "journal_head %p\n", jh); 1241470decc6SDave Kleikamp err = -EROFS; 1242470decc6SDave Kleikamp if (is_handle_aborted(handle)) 1243470decc6SDave Kleikamp goto out; 124441a5b913STheodore Ts'o journal = transaction->t_journal; 1245470decc6SDave Kleikamp err = 0; 1246470decc6SDave Kleikamp 1247470decc6SDave Kleikamp JBUFFER_TRACE(jh, "entry"); 1248470decc6SDave Kleikamp /* 1249470decc6SDave Kleikamp * The buffer may already belong to this transaction due to pre-zeroing 1250470decc6SDave Kleikamp * in the filesystem's new_block code. It may also be on the previous, 1251470decc6SDave Kleikamp * committing transaction's lists, but it HAS to be in Forget state in 1252470decc6SDave Kleikamp * that case: the transaction must have deleted the buffer for it to be 1253470decc6SDave Kleikamp * reused here. 1254470decc6SDave Kleikamp */ 125546417064SThomas Gleixner spin_lock(&jh->b_state_lock); 1256470decc6SDave Kleikamp J_ASSERT_JH(jh, (jh->b_transaction == transaction || 1257470decc6SDave Kleikamp jh->b_transaction == NULL || 1258470decc6SDave Kleikamp (jh->b_transaction == journal->j_committing_transaction && 1259470decc6SDave Kleikamp jh->b_jlist == BJ_Forget))); 1260470decc6SDave Kleikamp 1261470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_next_transaction == NULL); 1262470decc6SDave Kleikamp J_ASSERT_JH(jh, buffer_locked(jh2bh(jh))); 1263470decc6SDave Kleikamp 1264470decc6SDave Kleikamp if (jh->b_transaction == NULL) { 1265f91d1d04SJan Kara /* 1266f91d1d04SJan Kara * Previous jbd2_journal_forget() could have left the buffer 1267f91d1d04SJan Kara * with jbddirty bit set because it was being committed. When 1268f91d1d04SJan Kara * the commit finished, we've filed the buffer for 1269f91d1d04SJan Kara * checkpointing and marked it dirty. Now we are reallocating 1270f91d1d04SJan Kara * the buffer so the transaction freeing it must have 1271f91d1d04SJan Kara * committed and so it's safe to clear the dirty bit. 1272f91d1d04SJan Kara */ 1273f91d1d04SJan Kara clear_buffer_dirty(jh2bh(jh)); 12749fc7c63aSJosef Bacik /* first access by this transaction */ 12759fc7c63aSJosef Bacik jh->b_modified = 0; 12769fc7c63aSJosef Bacik 1277470decc6SDave Kleikamp JBUFFER_TRACE(jh, "file as BJ_Reserved"); 12786e4862a5STheodore Ts'o spin_lock(&journal->j_list_lock); 1279f7f4bccbSMingming Cao __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved); 1280559cce69STaesoo Kim spin_unlock(&journal->j_list_lock); 1281470decc6SDave Kleikamp } else if (jh->b_transaction == journal->j_committing_transaction) { 12829fc7c63aSJosef Bacik /* first access by this transaction */ 12839fc7c63aSJosef Bacik jh->b_modified = 0; 12849fc7c63aSJosef Bacik 1285470decc6SDave Kleikamp JBUFFER_TRACE(jh, "set next transaction"); 12866e4862a5STheodore Ts'o spin_lock(&journal->j_list_lock); 1287470decc6SDave Kleikamp jh->b_next_transaction = transaction; 1288470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 1289559cce69STaesoo Kim } 129046417064SThomas Gleixner spin_unlock(&jh->b_state_lock); 1291470decc6SDave Kleikamp 1292470decc6SDave Kleikamp /* 1293470decc6SDave Kleikamp * akpm: I added this. ext3_alloc_branch can pick up new indirect 1294470decc6SDave Kleikamp * blocks which contain freed but then revoked metadata. We need 1295470decc6SDave Kleikamp * to cancel the revoke in case we end up freeing it yet again 1296470decc6SDave Kleikamp * and the reallocating as data - this would cause a second revoke, 1297470decc6SDave Kleikamp * which hits an assertion error. 1298470decc6SDave Kleikamp */ 1299470decc6SDave Kleikamp JBUFFER_TRACE(jh, "cancelling revoke"); 1300f7f4bccbSMingming Cao jbd2_journal_cancel_revoke(handle, jh); 1301470decc6SDave Kleikamp out: 13023991b400SDing Dinghua jbd2_journal_put_journal_head(jh); 1303470decc6SDave Kleikamp return err; 1304470decc6SDave Kleikamp } 1305470decc6SDave Kleikamp 1306470decc6SDave Kleikamp /** 1307f7f4bccbSMingming Cao * int jbd2_journal_get_undo_access() - Notify intent to modify metadata with 1308470decc6SDave Kleikamp * non-rewindable consequences 1309470decc6SDave Kleikamp * @handle: transaction 1310470decc6SDave Kleikamp * @bh: buffer to undo 1311470decc6SDave Kleikamp * 1312470decc6SDave Kleikamp * Sometimes there is a need to distinguish between metadata which has 1313470decc6SDave Kleikamp * been committed to disk and that which has not. The ext3fs code uses 1314470decc6SDave Kleikamp * this for freeing and allocating space, we have to make sure that we 1315470decc6SDave Kleikamp * do not reuse freed space until the deallocation has been committed, 1316470decc6SDave Kleikamp * since if we overwrote that space we would make the delete 1317470decc6SDave Kleikamp * un-rewindable in case of a crash. 1318470decc6SDave Kleikamp * 1319f7f4bccbSMingming Cao * To deal with that, jbd2_journal_get_undo_access requests write access to a 1320470decc6SDave Kleikamp * buffer for parts of non-rewindable operations such as delete 1321470decc6SDave Kleikamp * operations on the bitmaps. The journaling code must keep a copy of 1322470decc6SDave Kleikamp * the buffer's contents prior to the undo_access call until such time 1323470decc6SDave Kleikamp * as we know that the buffer has definitely been committed to disk. 1324470decc6SDave Kleikamp * 1325470decc6SDave Kleikamp * We never need to know which transaction the committed data is part 1326470decc6SDave Kleikamp * of, buffers touched here are guaranteed to be dirtied later and so 1327470decc6SDave Kleikamp * will be committed to a new transaction in due course, at which point 1328470decc6SDave Kleikamp * we can discard the old committed data pointer. 1329470decc6SDave Kleikamp * 1330470decc6SDave Kleikamp * Returns error number or 0 on success. 1331470decc6SDave Kleikamp */ 1332f7f4bccbSMingming Cao int jbd2_journal_get_undo_access(handle_t *handle, struct buffer_head *bh) 1333470decc6SDave Kleikamp { 1334470decc6SDave Kleikamp int err; 1335de92c8caSJan Kara struct journal_head *jh; 1336470decc6SDave Kleikamp char *committed_data = NULL; 1337470decc6SDave Kleikamp 13388eedabfdSwangyan if (is_handle_aborted(handle)) 13398eedabfdSwangyan return -EROFS; 13408eedabfdSwangyan 1341087ffd4eSJunxiao Bi if (jbd2_write_access_granted(handle, bh, true)) 1342de92c8caSJan Kara return 0; 1343470decc6SDave Kleikamp 1344de92c8caSJan Kara jh = jbd2_journal_add_journal_head(bh); 134501215d3eSzhangyi (F) JBUFFER_TRACE(jh, "entry"); 134601215d3eSzhangyi (F) 1347470decc6SDave Kleikamp /* 1348470decc6SDave Kleikamp * Do this first --- it can drop the journal lock, so we want to 1349470decc6SDave Kleikamp * make sure that obtaining the committed_data is done 1350470decc6SDave Kleikamp * atomically wrt. completion of any outstanding commits. 1351470decc6SDave Kleikamp */ 1352470decc6SDave Kleikamp err = do_get_write_access(handle, jh, 1); 1353470decc6SDave Kleikamp if (err) 1354470decc6SDave Kleikamp goto out; 1355470decc6SDave Kleikamp 1356470decc6SDave Kleikamp repeat: 1357490c1b44SMichal Hocko if (!jh->b_committed_data) 1358490c1b44SMichal Hocko committed_data = jbd2_alloc(jh2bh(jh)->b_size, 1359490c1b44SMichal Hocko GFP_NOFS|__GFP_NOFAIL); 1360470decc6SDave Kleikamp 136146417064SThomas Gleixner spin_lock(&jh->b_state_lock); 1362470decc6SDave Kleikamp if (!jh->b_committed_data) { 1363470decc6SDave Kleikamp /* Copy out the current buffer contents into the 1364470decc6SDave Kleikamp * preserved, committed copy. */ 1365470decc6SDave Kleikamp JBUFFER_TRACE(jh, "generate b_committed data"); 1366470decc6SDave Kleikamp if (!committed_data) { 136746417064SThomas Gleixner spin_unlock(&jh->b_state_lock); 1368470decc6SDave Kleikamp goto repeat; 1369470decc6SDave Kleikamp } 1370470decc6SDave Kleikamp 1371470decc6SDave Kleikamp jh->b_committed_data = committed_data; 1372470decc6SDave Kleikamp committed_data = NULL; 1373470decc6SDave Kleikamp memcpy(jh->b_committed_data, bh->b_data, bh->b_size); 1374470decc6SDave Kleikamp } 137546417064SThomas Gleixner spin_unlock(&jh->b_state_lock); 1376470decc6SDave Kleikamp out: 1377f7f4bccbSMingming Cao jbd2_journal_put_journal_head(jh); 1378470decc6SDave Kleikamp if (unlikely(committed_data)) 1379af1e76d6SMingming Cao jbd2_free(committed_data, bh->b_size); 1380470decc6SDave Kleikamp return err; 1381470decc6SDave Kleikamp } 1382470decc6SDave Kleikamp 1383470decc6SDave Kleikamp /** 1384e06c8227SJoel Becker * void jbd2_journal_set_triggers() - Add triggers for commit writeout 1385e06c8227SJoel Becker * @bh: buffer to trigger on 1386e06c8227SJoel Becker * @type: struct jbd2_buffer_trigger_type containing the trigger(s). 1387e06c8227SJoel Becker * 1388e06c8227SJoel Becker * Set any triggers on this journal_head. This is always safe, because 1389e06c8227SJoel Becker * triggers for a committing buffer will be saved off, and triggers for 1390e06c8227SJoel Becker * a running transaction will match the buffer in that transaction. 1391e06c8227SJoel Becker * 1392e06c8227SJoel Becker * Call with NULL to clear the triggers. 1393e06c8227SJoel Becker */ 1394e06c8227SJoel Becker void jbd2_journal_set_triggers(struct buffer_head *bh, 1395e06c8227SJoel Becker struct jbd2_buffer_trigger_type *type) 1396e06c8227SJoel Becker { 1397ad56edadSJan Kara struct journal_head *jh = jbd2_journal_grab_journal_head(bh); 1398e06c8227SJoel Becker 1399ad56edadSJan Kara if (WARN_ON(!jh)) 1400ad56edadSJan Kara return; 1401e06c8227SJoel Becker jh->b_triggers = type; 1402ad56edadSJan Kara jbd2_journal_put_journal_head(jh); 1403e06c8227SJoel Becker } 1404e06c8227SJoel Becker 140513ceef09SJan Kara void jbd2_buffer_frozen_trigger(struct journal_head *jh, void *mapped_data, 1406e06c8227SJoel Becker struct jbd2_buffer_trigger_type *triggers) 1407e06c8227SJoel Becker { 1408e06c8227SJoel Becker struct buffer_head *bh = jh2bh(jh); 1409e06c8227SJoel Becker 141013ceef09SJan Kara if (!triggers || !triggers->t_frozen) 1411e06c8227SJoel Becker return; 1412e06c8227SJoel Becker 141313ceef09SJan Kara triggers->t_frozen(triggers, bh, mapped_data, bh->b_size); 1414e06c8227SJoel Becker } 1415e06c8227SJoel Becker 1416e06c8227SJoel Becker void jbd2_buffer_abort_trigger(struct journal_head *jh, 1417e06c8227SJoel Becker struct jbd2_buffer_trigger_type *triggers) 1418e06c8227SJoel Becker { 1419e06c8227SJoel Becker if (!triggers || !triggers->t_abort) 1420e06c8227SJoel Becker return; 1421e06c8227SJoel Becker 1422e06c8227SJoel Becker triggers->t_abort(triggers, jh2bh(jh)); 1423e06c8227SJoel Becker } 1424e06c8227SJoel Becker 1425e06c8227SJoel Becker /** 1426f7f4bccbSMingming Cao * int jbd2_journal_dirty_metadata() - mark a buffer as containing dirty metadata 1427470decc6SDave Kleikamp * @handle: transaction to add buffer to. 1428470decc6SDave Kleikamp * @bh: buffer to mark 1429470decc6SDave Kleikamp * 1430470decc6SDave Kleikamp * mark dirty metadata which needs to be journaled as part of the current 1431470decc6SDave Kleikamp * transaction. 1432470decc6SDave Kleikamp * 14339ea7a0dfSTheodore Ts'o * The buffer must have previously had jbd2_journal_get_write_access() 14349ea7a0dfSTheodore Ts'o * called so that it has a valid journal_head attached to the buffer 14359ea7a0dfSTheodore Ts'o * head. 14369ea7a0dfSTheodore Ts'o * 1437470decc6SDave Kleikamp * The buffer is placed on the transaction's metadata list and is marked 1438470decc6SDave Kleikamp * as belonging to the transaction. 1439470decc6SDave Kleikamp * 1440470decc6SDave Kleikamp * Returns error number or 0 on success. 1441470decc6SDave Kleikamp * 1442470decc6SDave Kleikamp * Special care needs to be taken if the buffer already belongs to the 1443470decc6SDave Kleikamp * current committing transaction (in which case we should have frozen 1444470decc6SDave Kleikamp * data present for that commit). In that case, we don't relink the 1445470decc6SDave Kleikamp * buffer: that only gets done when the old transaction finally 1446470decc6SDave Kleikamp * completes its commit. 1447470decc6SDave Kleikamp */ 1448f7f4bccbSMingming Cao int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh) 1449470decc6SDave Kleikamp { 1450470decc6SDave Kleikamp transaction_t *transaction = handle->h_transaction; 145141a5b913STheodore Ts'o journal_t *journal; 1452ad56edadSJan Kara struct journal_head *jh; 14539ea7a0dfSTheodore Ts'o int ret = 0; 1454470decc6SDave Kleikamp 1455470decc6SDave Kleikamp if (is_handle_aborted(handle)) 145641a5b913STheodore Ts'o return -EROFS; 145701215d3eSzhangyi (F) if (!buffer_jbd(bh)) 145801215d3eSzhangyi (F) return -EUCLEAN; 145901215d3eSzhangyi (F) 14606e06ae88SJan Kara /* 14616e06ae88SJan Kara * We don't grab jh reference here since the buffer must be part 14626e06ae88SJan Kara * of the running transaction. 14636e06ae88SJan Kara */ 14646e06ae88SJan Kara jh = bh2jh(bh); 146501215d3eSzhangyi (F) jbd_debug(5, "journal_head %p\n", jh); 146601215d3eSzhangyi (F) JBUFFER_TRACE(jh, "entry"); 146701215d3eSzhangyi (F) 14686e06ae88SJan Kara /* 14696e06ae88SJan Kara * This and the following assertions are unreliable since we may see jh 14706e06ae88SJan Kara * in inconsistent state unless we grab bh_state lock. But this is 14716e06ae88SJan Kara * crucial to catch bugs so let's do a reliable check until the 14726e06ae88SJan Kara * lockless handling is fully proven. 14736e06ae88SJan Kara */ 14746e06ae88SJan Kara if (jh->b_transaction != transaction && 14756e06ae88SJan Kara jh->b_next_transaction != transaction) { 147646417064SThomas Gleixner spin_lock(&jh->b_state_lock); 14776e06ae88SJan Kara J_ASSERT_JH(jh, jh->b_transaction == transaction || 14786e06ae88SJan Kara jh->b_next_transaction == transaction); 147946417064SThomas Gleixner spin_unlock(&jh->b_state_lock); 14806e06ae88SJan Kara } 14816e06ae88SJan Kara if (jh->b_modified == 1) { 14826e06ae88SJan Kara /* If it's in our transaction it must be in BJ_Metadata list. */ 14836e06ae88SJan Kara if (jh->b_transaction == transaction && 14846e06ae88SJan Kara jh->b_jlist != BJ_Metadata) { 148546417064SThomas Gleixner spin_lock(&jh->b_state_lock); 1486e09463f2STheodore Ts'o if (jh->b_transaction == transaction && 1487e09463f2STheodore Ts'o jh->b_jlist != BJ_Metadata) 1488e09463f2STheodore Ts'o pr_err("JBD2: assertion failure: h_type=%u " 1489e09463f2STheodore Ts'o "h_line_no=%u block_no=%llu jlist=%u\n", 1490e09463f2STheodore Ts'o handle->h_type, handle->h_line_no, 1491e09463f2STheodore Ts'o (unsigned long long) bh->b_blocknr, 1492e09463f2STheodore Ts'o jh->b_jlist); 14936e06ae88SJan Kara J_ASSERT_JH(jh, jh->b_transaction != transaction || 14946e06ae88SJan Kara jh->b_jlist == BJ_Metadata); 149546417064SThomas Gleixner spin_unlock(&jh->b_state_lock); 14966e06ae88SJan Kara } 14976e06ae88SJan Kara goto out; 14986e06ae88SJan Kara } 14996e06ae88SJan Kara 15006e06ae88SJan Kara journal = transaction->t_journal; 150146417064SThomas Gleixner spin_lock(&jh->b_state_lock); 1502470decc6SDave Kleikamp 1503470decc6SDave Kleikamp if (jh->b_modified == 0) { 1504470decc6SDave Kleikamp /* 1505470decc6SDave Kleikamp * This buffer's got modified and becoming part 1506470decc6SDave Kleikamp * of the transaction. This needs to be done 1507470decc6SDave Kleikamp * once a transaction -bzzz 1508470decc6SDave Kleikamp */ 1509d090707eSJan Kara if (WARN_ON_ONCE(jbd2_handle_buffer_credits(handle) <= 0)) { 1510f6c07cadSTheodore Ts'o ret = -ENOSPC; 1511f6c07cadSTheodore Ts'o goto out_unlock_bh; 1512f6c07cadSTheodore Ts'o } 1513e09463f2STheodore Ts'o jh->b_modified = 1; 1514933f1c1eSJan Kara handle->h_total_credits--; 1515470decc6SDave Kleikamp } 1516470decc6SDave Kleikamp 1517470decc6SDave Kleikamp /* 1518470decc6SDave Kleikamp * fastpath, to avoid expensive locking. If this buffer is already 1519470decc6SDave Kleikamp * on the running transaction's metadata list there is nothing to do. 1520470decc6SDave Kleikamp * Nobody can take it off again because there is a handle open. 1521470decc6SDave Kleikamp * I _think_ we're OK here with SMP barriers - a mistaken decision will 1522470decc6SDave Kleikamp * result in this test being false, so we go in and take the locks. 1523470decc6SDave Kleikamp */ 1524470decc6SDave Kleikamp if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) { 1525470decc6SDave Kleikamp JBUFFER_TRACE(jh, "fastpath"); 15269ea7a0dfSTheodore Ts'o if (unlikely(jh->b_transaction != 15279ea7a0dfSTheodore Ts'o journal->j_running_transaction)) { 1528a67c848aSDmitry Monakhov printk(KERN_ERR "JBD2: %s: " 15299ea7a0dfSTheodore Ts'o "jh->b_transaction (%llu, %p, %u) != " 153066a4cb18STheodore Ts'o "journal->j_running_transaction (%p, %u)\n", 15319ea7a0dfSTheodore Ts'o journal->j_devname, 15329ea7a0dfSTheodore Ts'o (unsigned long long) bh->b_blocknr, 15339ea7a0dfSTheodore Ts'o jh->b_transaction, 15349ea7a0dfSTheodore Ts'o jh->b_transaction ? jh->b_transaction->t_tid : 0, 15359ea7a0dfSTheodore Ts'o journal->j_running_transaction, 15369ea7a0dfSTheodore Ts'o journal->j_running_transaction ? 15379ea7a0dfSTheodore Ts'o journal->j_running_transaction->t_tid : 0); 15389ea7a0dfSTheodore Ts'o ret = -EINVAL; 15399ea7a0dfSTheodore Ts'o } 1540470decc6SDave Kleikamp goto out_unlock_bh; 1541470decc6SDave Kleikamp } 1542470decc6SDave Kleikamp 1543470decc6SDave Kleikamp set_buffer_jbddirty(bh); 1544470decc6SDave Kleikamp 1545470decc6SDave Kleikamp /* 1546470decc6SDave Kleikamp * Metadata already on the current transaction list doesn't 1547470decc6SDave Kleikamp * need to be filed. Metadata on another transaction's list must 1548470decc6SDave Kleikamp * be committing, and will be refiled once the commit completes: 1549470decc6SDave Kleikamp * leave it alone for now. 1550470decc6SDave Kleikamp */ 1551470decc6SDave Kleikamp if (jh->b_transaction != transaction) { 1552470decc6SDave Kleikamp JBUFFER_TRACE(jh, "already on other transaction"); 155366a4cb18STheodore Ts'o if (unlikely(((jh->b_transaction != 155466a4cb18STheodore Ts'o journal->j_committing_transaction)) || 155566a4cb18STheodore Ts'o (jh->b_next_transaction != transaction))) { 155666a4cb18STheodore Ts'o printk(KERN_ERR "jbd2_journal_dirty_metadata: %s: " 155766a4cb18STheodore Ts'o "bad jh for block %llu: " 155866a4cb18STheodore Ts'o "transaction (%p, %u), " 155966a4cb18STheodore Ts'o "jh->b_transaction (%p, %u), " 156066a4cb18STheodore Ts'o "jh->b_next_transaction (%p, %u), jlist %u\n", 15619ea7a0dfSTheodore Ts'o journal->j_devname, 15629ea7a0dfSTheodore Ts'o (unsigned long long) bh->b_blocknr, 156366a4cb18STheodore Ts'o transaction, transaction->t_tid, 15649ea7a0dfSTheodore Ts'o jh->b_transaction, 156566a4cb18STheodore Ts'o jh->b_transaction ? 156666a4cb18STheodore Ts'o jh->b_transaction->t_tid : 0, 15679ea7a0dfSTheodore Ts'o jh->b_next_transaction, 15689ea7a0dfSTheodore Ts'o jh->b_next_transaction ? 15699ea7a0dfSTheodore Ts'o jh->b_next_transaction->t_tid : 0, 157066a4cb18STheodore Ts'o jh->b_jlist); 157166a4cb18STheodore Ts'o WARN_ON(1); 15729ea7a0dfSTheodore Ts'o ret = -EINVAL; 15739ea7a0dfSTheodore Ts'o } 1574470decc6SDave Kleikamp /* And this case is illegal: we can't reuse another 1575470decc6SDave Kleikamp * transaction's data buffer, ever. */ 1576470decc6SDave Kleikamp goto out_unlock_bh; 1577470decc6SDave Kleikamp } 1578470decc6SDave Kleikamp 1579470decc6SDave Kleikamp /* That test should have eliminated the following case: */ 15804019191bSMingming Cao J_ASSERT_JH(jh, jh->b_frozen_data == NULL); 1581470decc6SDave Kleikamp 1582470decc6SDave Kleikamp JBUFFER_TRACE(jh, "file as BJ_Metadata"); 1583470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 158441a5b913STheodore Ts'o __jbd2_journal_file_buffer(jh, transaction, BJ_Metadata); 1585470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 1586470decc6SDave Kleikamp out_unlock_bh: 158746417064SThomas Gleixner spin_unlock(&jh->b_state_lock); 1588470decc6SDave Kleikamp out: 1589470decc6SDave Kleikamp JBUFFER_TRACE(jh, "exit"); 15909ea7a0dfSTheodore Ts'o return ret; 1591470decc6SDave Kleikamp } 1592470decc6SDave Kleikamp 1593470decc6SDave Kleikamp /** 1594f7f4bccbSMingming Cao * void jbd2_journal_forget() - bforget() for potentially-journaled buffers. 1595470decc6SDave Kleikamp * @handle: transaction handle 1596470decc6SDave Kleikamp * @bh: bh to 'forget' 1597470decc6SDave Kleikamp * 1598470decc6SDave Kleikamp * We can only do the bforget if there are no commits pending against the 1599470decc6SDave Kleikamp * buffer. If the buffer is dirty in the current running transaction we 1600470decc6SDave Kleikamp * can safely unlink it. 1601470decc6SDave Kleikamp * 1602470decc6SDave Kleikamp * bh may not be a journalled buffer at all - it may be a non-JBD 1603470decc6SDave Kleikamp * buffer which came off the hashtable. Check for this. 1604470decc6SDave Kleikamp * 1605470decc6SDave Kleikamp * Decrements bh->b_count by one. 1606470decc6SDave Kleikamp * 1607470decc6SDave Kleikamp * Allow this call even if the handle has aborted --- it may be part of 1608470decc6SDave Kleikamp * the caller's cleanup after an abort. 1609470decc6SDave Kleikamp */ 1610f7f4bccbSMingming Cao int jbd2_journal_forget(handle_t *handle, struct buffer_head *bh) 1611470decc6SDave Kleikamp { 1612470decc6SDave Kleikamp transaction_t *transaction = handle->h_transaction; 161341a5b913STheodore Ts'o journal_t *journal; 1614470decc6SDave Kleikamp struct journal_head *jh; 1615470decc6SDave Kleikamp int drop_reserve = 0; 1616470decc6SDave Kleikamp int err = 0; 16171dfc3220SJosef Bacik int was_modified = 0; 1618470decc6SDave Kleikamp 161941a5b913STheodore Ts'o if (is_handle_aborted(handle)) 162041a5b913STheodore Ts'o return -EROFS; 162141a5b913STheodore Ts'o journal = transaction->t_journal; 162241a5b913STheodore Ts'o 1623470decc6SDave Kleikamp BUFFER_TRACE(bh, "entry"); 1624470decc6SDave Kleikamp 162546417064SThomas Gleixner jh = jbd2_journal_grab_journal_head(bh); 162646417064SThomas Gleixner if (!jh) { 162746417064SThomas Gleixner __bforget(bh); 162846417064SThomas Gleixner return 0; 162946417064SThomas Gleixner } 1630470decc6SDave Kleikamp 163146417064SThomas Gleixner spin_lock(&jh->b_state_lock); 1632470decc6SDave Kleikamp 1633470decc6SDave Kleikamp /* Critical error: attempting to delete a bitmap buffer, maybe? 1634470decc6SDave Kleikamp * Don't do any jbd operations, and return an error. */ 1635470decc6SDave Kleikamp if (!J_EXPECT_JH(jh, !jh->b_committed_data, 1636470decc6SDave Kleikamp "inconsistent data on disk")) { 1637470decc6SDave Kleikamp err = -EIO; 16382e710ff0SJan Kara goto drop; 1639470decc6SDave Kleikamp } 1640470decc6SDave Kleikamp 164148fc7f7eSAdam Buchbinder /* keep track of whether or not this transaction modified us */ 16421dfc3220SJosef Bacik was_modified = jh->b_modified; 16431dfc3220SJosef Bacik 1644470decc6SDave Kleikamp /* 1645470decc6SDave Kleikamp * The buffer's going from the transaction, we must drop 1646470decc6SDave Kleikamp * all references -bzzz 1647470decc6SDave Kleikamp */ 1648470decc6SDave Kleikamp jh->b_modified = 0; 1649470decc6SDave Kleikamp 165041a5b913STheodore Ts'o if (jh->b_transaction == transaction) { 1651470decc6SDave Kleikamp J_ASSERT_JH(jh, !jh->b_frozen_data); 1652470decc6SDave Kleikamp 1653470decc6SDave Kleikamp /* If we are forgetting a buffer which is already part 1654470decc6SDave Kleikamp * of this transaction, then we can just drop it from 1655470decc6SDave Kleikamp * the transaction immediately. */ 1656470decc6SDave Kleikamp clear_buffer_dirty(bh); 1657470decc6SDave Kleikamp clear_buffer_jbddirty(bh); 1658470decc6SDave Kleikamp 1659470decc6SDave Kleikamp JBUFFER_TRACE(jh, "belongs to current transaction: unfile"); 1660470decc6SDave Kleikamp 16611dfc3220SJosef Bacik /* 16621dfc3220SJosef Bacik * we only want to drop a reference if this transaction 16631dfc3220SJosef Bacik * modified the buffer 16641dfc3220SJosef Bacik */ 16651dfc3220SJosef Bacik if (was_modified) 1666470decc6SDave Kleikamp drop_reserve = 1; 1667470decc6SDave Kleikamp 1668470decc6SDave Kleikamp /* 1669470decc6SDave Kleikamp * We are no longer going to journal this buffer. 1670470decc6SDave Kleikamp * However, the commit of this transaction is still 1671470decc6SDave Kleikamp * important to the buffer: the delete that we are now 1672470decc6SDave Kleikamp * processing might obsolete an old log entry, so by 1673470decc6SDave Kleikamp * committing, we can satisfy the buffer's checkpoint. 1674470decc6SDave Kleikamp * 1675470decc6SDave Kleikamp * So, if we have a checkpoint on the buffer, we should 1676470decc6SDave Kleikamp * now refile the buffer on our BJ_Forget list so that 1677470decc6SDave Kleikamp * we know to remove the checkpoint after we commit. 1678470decc6SDave Kleikamp */ 1679470decc6SDave Kleikamp 16800bfea811STheodore Ts'o spin_lock(&journal->j_list_lock); 1681470decc6SDave Kleikamp if (jh->b_cp_transaction) { 1682f7f4bccbSMingming Cao __jbd2_journal_temp_unlink_buffer(jh); 1683f7f4bccbSMingming Cao __jbd2_journal_file_buffer(jh, transaction, BJ_Forget); 1684470decc6SDave Kleikamp } else { 1685f7f4bccbSMingming Cao __jbd2_journal_unfile_buffer(jh); 168693108ebbSJan Kara jbd2_journal_put_journal_head(jh); 1687470decc6SDave Kleikamp } 16880bfea811STheodore Ts'o spin_unlock(&journal->j_list_lock); 1689470decc6SDave Kleikamp } else if (jh->b_transaction) { 1690470decc6SDave Kleikamp J_ASSERT_JH(jh, (jh->b_transaction == 1691470decc6SDave Kleikamp journal->j_committing_transaction)); 1692470decc6SDave Kleikamp /* However, if the buffer is still owned by a prior 1693470decc6SDave Kleikamp * (committing) transaction, we can't drop it yet... */ 1694470decc6SDave Kleikamp JBUFFER_TRACE(jh, "belongs to older transaction"); 1695904cdbd4Szhangyi (F) /* ... but we CAN drop it from the new transaction through 1696904cdbd4Szhangyi (F) * marking the buffer as freed and set j_next_transaction to 1697904cdbd4Szhangyi (F) * the new transaction, so that not only the commit code 1698904cdbd4Szhangyi (F) * knows it should clear dirty bits when it is done with the 1699904cdbd4Szhangyi (F) * buffer, but also the buffer can be checkpointed only 1700904cdbd4Szhangyi (F) * after the new transaction commits. */ 1701470decc6SDave Kleikamp 1702904cdbd4Szhangyi (F) set_buffer_freed(bh); 1703904cdbd4Szhangyi (F) 1704904cdbd4Szhangyi (F) if (!jh->b_next_transaction) { 17050bfea811STheodore Ts'o spin_lock(&journal->j_list_lock); 1706904cdbd4Szhangyi (F) jh->b_next_transaction = transaction; 17070bfea811STheodore Ts'o spin_unlock(&journal->j_list_lock); 1708904cdbd4Szhangyi (F) } else { 1709904cdbd4Szhangyi (F) J_ASSERT(jh->b_next_transaction == transaction); 17101dfc3220SJosef Bacik 17111dfc3220SJosef Bacik /* 17121dfc3220SJosef Bacik * only drop a reference if this transaction modified 17131dfc3220SJosef Bacik * the buffer 17141dfc3220SJosef Bacik */ 17151dfc3220SJosef Bacik if (was_modified) 1716470decc6SDave Kleikamp drop_reserve = 1; 1717470decc6SDave Kleikamp } 171859759926Szhangyi (F) } else { 171959759926Szhangyi (F) /* 172059759926Szhangyi (F) * Finally, if the buffer is not belongs to any 172159759926Szhangyi (F) * transaction, we can just drop it now if it has no 172259759926Szhangyi (F) * checkpoint. 172359759926Szhangyi (F) */ 172459759926Szhangyi (F) spin_lock(&journal->j_list_lock); 172559759926Szhangyi (F) if (!jh->b_cp_transaction) { 172659759926Szhangyi (F) JBUFFER_TRACE(jh, "belongs to none transaction"); 172759759926Szhangyi (F) spin_unlock(&journal->j_list_lock); 17282e710ff0SJan Kara goto drop; 1729470decc6SDave Kleikamp } 1730470decc6SDave Kleikamp 173159759926Szhangyi (F) /* 173259759926Szhangyi (F) * Otherwise, if the buffer has been written to disk, 173359759926Szhangyi (F) * it is safe to remove the checkpoint and drop it. 173459759926Szhangyi (F) */ 173559759926Szhangyi (F) if (!buffer_dirty(bh)) { 173659759926Szhangyi (F) __jbd2_journal_remove_checkpoint(jh); 173759759926Szhangyi (F) spin_unlock(&journal->j_list_lock); 17382e710ff0SJan Kara goto drop; 173959759926Szhangyi (F) } 174059759926Szhangyi (F) 174159759926Szhangyi (F) /* 174259759926Szhangyi (F) * The buffer is still not written to disk, we should 174359759926Szhangyi (F) * attach this buffer to current transaction so that the 174459759926Szhangyi (F) * buffer can be checkpointed only after the current 174559759926Szhangyi (F) * transaction commits. 174659759926Szhangyi (F) */ 174759759926Szhangyi (F) clear_buffer_dirty(bh); 174859759926Szhangyi (F) __jbd2_journal_file_buffer(jh, transaction, BJ_Forget); 174959759926Szhangyi (F) spin_unlock(&journal->j_list_lock); 175059759926Szhangyi (F) } 1751470decc6SDave Kleikamp drop: 1752470decc6SDave Kleikamp __brelse(bh); 175346417064SThomas Gleixner spin_unlock(&jh->b_state_lock); 175446417064SThomas Gleixner jbd2_journal_put_journal_head(jh); 1755470decc6SDave Kleikamp if (drop_reserve) { 1756470decc6SDave Kleikamp /* no need to reserve log space for this block -bzzz */ 1757933f1c1eSJan Kara handle->h_total_credits++; 1758470decc6SDave Kleikamp } 1759470decc6SDave Kleikamp return err; 1760470decc6SDave Kleikamp } 1761470decc6SDave Kleikamp 1762470decc6SDave Kleikamp /** 1763f7f4bccbSMingming Cao * int jbd2_journal_stop() - complete a transaction 1764bd7ced98SMasanari Iida * @handle: transaction to complete. 1765470decc6SDave Kleikamp * 1766470decc6SDave Kleikamp * All done for a particular handle. 1767470decc6SDave Kleikamp * 1768470decc6SDave Kleikamp * There is not much action needed here. We just return any remaining 1769470decc6SDave Kleikamp * buffer credits to the transaction and remove the handle. The only 1770470decc6SDave Kleikamp * complication is that we need to start a commit operation if the 1771470decc6SDave Kleikamp * filesystem is marked for synchronous update. 1772470decc6SDave Kleikamp * 1773f7f4bccbSMingming Cao * jbd2_journal_stop itself will not usually return an error, but it may 1774470decc6SDave Kleikamp * do so in unusual circumstances. In particular, expect it to 1775f7f4bccbSMingming Cao * return -EIO if a jbd2_journal_abort has been executed since the 1776470decc6SDave Kleikamp * transaction began. 1777470decc6SDave Kleikamp */ 1778f7f4bccbSMingming Cao int jbd2_journal_stop(handle_t *handle) 1779470decc6SDave Kleikamp { 1780470decc6SDave Kleikamp transaction_t *transaction = handle->h_transaction; 178141a5b913STheodore Ts'o journal_t *journal; 178241a5b913STheodore Ts'o int err = 0, wait_for_commit = 0; 1783a51dca9cSTheodore Ts'o tid_t tid; 1784470decc6SDave Kleikamp pid_t pid; 1785470decc6SDave Kleikamp 17869d506594SLukas Czerner if (--handle->h_ref > 0) { 17879d506594SLukas Czerner jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1, 17889d506594SLukas Czerner handle->h_ref); 1789dfaf5ffdSJan Kara if (is_handle_aborted(handle)) 1790dfaf5ffdSJan Kara return -EIO; 1791dfaf5ffdSJan Kara return 0; 1792dfaf5ffdSJan Kara } 1793dfaf5ffdSJan Kara if (!transaction) { 1794dfaf5ffdSJan Kara /* 1795dfaf5ffdSJan Kara * Handle is already detached from the transaction so there is 1796dfaf5ffdSJan Kara * nothing to do other than free the handle. 1797dfaf5ffdSJan Kara */ 1798ec8b6f60SJan Kara memalloc_nofs_restore(handle->saved_alloc_context); 179941a5b913STheodore Ts'o goto free_and_exit; 18009d506594SLukas Czerner } 180141a5b913STheodore Ts'o journal = transaction->t_journal; 1802dfaf5ffdSJan Kara tid = transaction->t_tid; 1803470decc6SDave Kleikamp 1804470decc6SDave Kleikamp if (is_handle_aborted(handle)) 1805470decc6SDave Kleikamp err = -EIO; 1806470decc6SDave Kleikamp 1807470decc6SDave Kleikamp jbd_debug(4, "Handle %p going down\n", handle); 1808343d9c28STheodore Ts'o trace_jbd2_handle_stats(journal->j_fs_dev->bd_dev, 1809dfaf5ffdSJan Kara tid, handle->h_type, handle->h_line_no, 1810343d9c28STheodore Ts'o jiffies - handle->h_start_jiffies, 1811343d9c28STheodore Ts'o handle->h_sync, handle->h_requested_credits, 1812343d9c28STheodore Ts'o (handle->h_requested_credits - 1813933f1c1eSJan Kara handle->h_total_credits)); 1814470decc6SDave Kleikamp 1815470decc6SDave Kleikamp /* 1816470decc6SDave Kleikamp * Implement synchronous transaction batching. If the handle 1817470decc6SDave Kleikamp * was synchronous, don't force a commit immediately. Let's 1818e07f7183SJosef Bacik * yield and let another thread piggyback onto this 1819e07f7183SJosef Bacik * transaction. Keep doing that while new threads continue to 1820e07f7183SJosef Bacik * arrive. It doesn't cost much - we're about to run a commit 1821e07f7183SJosef Bacik * and sleep on IO anyway. Speeds up many-threaded, many-dir 1822e07f7183SJosef Bacik * operations by 30x or more... 1823470decc6SDave Kleikamp * 1824e07f7183SJosef Bacik * We try and optimize the sleep time against what the 1825e07f7183SJosef Bacik * underlying disk can do, instead of having a static sleep 1826e07f7183SJosef Bacik * time. This is useful for the case where our storage is so 1827e07f7183SJosef Bacik * fast that it is more optimal to go ahead and force a flush 1828e07f7183SJosef Bacik * and wait for the transaction to be committed than it is to 1829e07f7183SJosef Bacik * wait for an arbitrary amount of time for new writers to 1830e07f7183SJosef Bacik * join the transaction. We achieve this by measuring how 1831e07f7183SJosef Bacik * long it takes to commit a transaction, and compare it with 1832e07f7183SJosef Bacik * how long this transaction has been running, and if run time 1833e07f7183SJosef Bacik * < commit time then we sleep for the delta and commit. This 1834e07f7183SJosef Bacik * greatly helps super fast disks that would see slowdowns as 1835e07f7183SJosef Bacik * more threads started doing fsyncs. 1836e07f7183SJosef Bacik * 1837e07f7183SJosef Bacik * But don't do this if this process was the most recent one 1838e07f7183SJosef Bacik * to perform a synchronous write. We do this to detect the 1839e07f7183SJosef Bacik * case where a single process is doing a stream of sync 1840e07f7183SJosef Bacik * writes. No point in waiting for joiners in that case. 18415dd21424SEric Sandeen * 18425dd21424SEric Sandeen * Setting max_batch_time to 0 disables this completely. 1843470decc6SDave Kleikamp */ 1844470decc6SDave Kleikamp pid = current->pid; 18455dd21424SEric Sandeen if (handle->h_sync && journal->j_last_sync_writer != pid && 18465dd21424SEric Sandeen journal->j_max_batch_time) { 1847e07f7183SJosef Bacik u64 commit_time, trans_time; 1848e07f7183SJosef Bacik 1849470decc6SDave Kleikamp journal->j_last_sync_writer = pid; 1850e07f7183SJosef Bacik 1851a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 1852e07f7183SJosef Bacik commit_time = journal->j_average_commit_time; 1853a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 1854e07f7183SJosef Bacik 1855e07f7183SJosef Bacik trans_time = ktime_to_ns(ktime_sub(ktime_get(), 1856e07f7183SJosef Bacik transaction->t_start_time)); 1857e07f7183SJosef Bacik 185830773840STheodore Ts'o commit_time = max_t(u64, commit_time, 185930773840STheodore Ts'o 1000*journal->j_min_batch_time); 1860e07f7183SJosef Bacik commit_time = min_t(u64, commit_time, 186130773840STheodore Ts'o 1000*journal->j_max_batch_time); 1862e07f7183SJosef Bacik 1863e07f7183SJosef Bacik if (trans_time < commit_time) { 1864e07f7183SJosef Bacik ktime_t expires = ktime_add_ns(ktime_get(), 1865e07f7183SJosef Bacik commit_time); 1866e07f7183SJosef Bacik set_current_state(TASK_UNINTERRUPTIBLE); 1867e07f7183SJosef Bacik schedule_hrtimeout(&expires, HRTIMER_MODE_ABS); 1868e07f7183SJosef Bacik } 1869470decc6SDave Kleikamp } 1870470decc6SDave Kleikamp 18717058548cSTheodore Ts'o if (handle->h_sync) 18727058548cSTheodore Ts'o transaction->t_synchronous_commit = 1; 1873470decc6SDave Kleikamp 1874470decc6SDave Kleikamp /* 1875470decc6SDave Kleikamp * If the handle is marked SYNC, we need to set another commit 1876150549edSJan Kara * going! We also want to force a commit if the transaction is too 1877150549edSJan Kara * old now. 1878470decc6SDave Kleikamp */ 1879470decc6SDave Kleikamp if (handle->h_sync || 1880470decc6SDave Kleikamp time_after_eq(jiffies, transaction->t_expires)) { 1881470decc6SDave Kleikamp /* Do this even for aborted journals: an abort still 1882470decc6SDave Kleikamp * completes the commit thread, it just doesn't write 1883470decc6SDave Kleikamp * anything to disk. */ 1884470decc6SDave Kleikamp 1885470decc6SDave Kleikamp jbd_debug(2, "transaction too old, requesting commit for " 1886470decc6SDave Kleikamp "handle %p\n", handle); 1887470decc6SDave Kleikamp /* This is non-blocking */ 1888dfaf5ffdSJan Kara jbd2_log_start_commit(journal, tid); 1889470decc6SDave Kleikamp 1890470decc6SDave Kleikamp /* 1891f7f4bccbSMingming Cao * Special case: JBD2_SYNC synchronous updates require us 1892470decc6SDave Kleikamp * to wait for the commit to complete. 1893470decc6SDave Kleikamp */ 1894470decc6SDave Kleikamp if (handle->h_sync && !(current->flags & PF_MEMALLOC)) 1895a51dca9cSTheodore Ts'o wait_for_commit = 1; 1896470decc6SDave Kleikamp } 1897470decc6SDave Kleikamp 1898a51dca9cSTheodore Ts'o /* 1899ec8b6f60SJan Kara * Once stop_this_handle() drops t_updates, the transaction could start 1900ec8b6f60SJan Kara * committing on us and eventually disappear. So we must not 1901ec8b6f60SJan Kara * dereference transaction pointer again after calling 1902ec8b6f60SJan Kara * stop_this_handle(). 1903a51dca9cSTheodore Ts'o */ 1904ec8b6f60SJan Kara stop_this_handle(handle); 19057a4b188fSJan Kara 1906a51dca9cSTheodore Ts'o if (wait_for_commit) 1907a51dca9cSTheodore Ts'o err = jbd2_log_wait_commit(journal, tid); 1908a51dca9cSTheodore Ts'o 190941a5b913STheodore Ts'o free_and_exit: 1910ec8b6f60SJan Kara if (handle->h_rsv_handle) 1911ec8b6f60SJan Kara jbd2_free_handle(handle->h_rsv_handle); 1912af1e76d6SMingming Cao jbd2_free_handle(handle); 1913470decc6SDave Kleikamp return err; 1914470decc6SDave Kleikamp } 1915470decc6SDave Kleikamp 1916470decc6SDave Kleikamp /* 1917470decc6SDave Kleikamp * 1918470decc6SDave Kleikamp * List management code snippets: various functions for manipulating the 1919470decc6SDave Kleikamp * transaction buffer lists. 1920470decc6SDave Kleikamp * 1921470decc6SDave Kleikamp */ 1922470decc6SDave Kleikamp 1923470decc6SDave Kleikamp /* 1924470decc6SDave Kleikamp * Append a buffer to a transaction list, given the transaction's list head 1925470decc6SDave Kleikamp * pointer. 1926470decc6SDave Kleikamp * 1927470decc6SDave Kleikamp * j_list_lock is held. 1928470decc6SDave Kleikamp * 192946417064SThomas Gleixner * jh->b_state_lock is held. 1930470decc6SDave Kleikamp */ 1931470decc6SDave Kleikamp 1932470decc6SDave Kleikamp static inline void 1933470decc6SDave Kleikamp __blist_add_buffer(struct journal_head **list, struct journal_head *jh) 1934470decc6SDave Kleikamp { 1935470decc6SDave Kleikamp if (!*list) { 1936470decc6SDave Kleikamp jh->b_tnext = jh->b_tprev = jh; 1937470decc6SDave Kleikamp *list = jh; 1938470decc6SDave Kleikamp } else { 1939470decc6SDave Kleikamp /* Insert at the tail of the list to preserve order */ 1940470decc6SDave Kleikamp struct journal_head *first = *list, *last = first->b_tprev; 1941470decc6SDave Kleikamp jh->b_tprev = last; 1942470decc6SDave Kleikamp jh->b_tnext = first; 1943470decc6SDave Kleikamp last->b_tnext = first->b_tprev = jh; 1944470decc6SDave Kleikamp } 1945470decc6SDave Kleikamp } 1946470decc6SDave Kleikamp 1947470decc6SDave Kleikamp /* 1948470decc6SDave Kleikamp * Remove a buffer from a transaction list, given the transaction's list 1949470decc6SDave Kleikamp * head pointer. 1950470decc6SDave Kleikamp * 1951470decc6SDave Kleikamp * Called with j_list_lock held, and the journal may not be locked. 1952470decc6SDave Kleikamp * 195346417064SThomas Gleixner * jh->b_state_lock is held. 1954470decc6SDave Kleikamp */ 1955470decc6SDave Kleikamp 1956470decc6SDave Kleikamp static inline void 1957470decc6SDave Kleikamp __blist_del_buffer(struct journal_head **list, struct journal_head *jh) 1958470decc6SDave Kleikamp { 1959470decc6SDave Kleikamp if (*list == jh) { 1960470decc6SDave Kleikamp *list = jh->b_tnext; 1961470decc6SDave Kleikamp if (*list == jh) 1962470decc6SDave Kleikamp *list = NULL; 1963470decc6SDave Kleikamp } 1964470decc6SDave Kleikamp jh->b_tprev->b_tnext = jh->b_tnext; 1965470decc6SDave Kleikamp jh->b_tnext->b_tprev = jh->b_tprev; 1966470decc6SDave Kleikamp } 1967470decc6SDave Kleikamp 1968470decc6SDave Kleikamp /* 1969470decc6SDave Kleikamp * Remove a buffer from the appropriate transaction list. 1970470decc6SDave Kleikamp * 1971470decc6SDave Kleikamp * Note that this function can *change* the value of 1972f5113effSJan Kara * bh->b_transaction->t_buffers, t_forget, t_shadow_list, t_log_list or 1973f5113effSJan Kara * t_reserved_list. If the caller is holding onto a copy of one of these 1974f5113effSJan Kara * pointers, it could go bad. Generally the caller needs to re-read the 1975f5113effSJan Kara * pointer from the transaction_t. 1976470decc6SDave Kleikamp * 19775bebccf9SJan Kara * Called under j_list_lock. 1978470decc6SDave Kleikamp */ 19795bebccf9SJan Kara static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh) 1980470decc6SDave Kleikamp { 1981470decc6SDave Kleikamp struct journal_head **list = NULL; 1982470decc6SDave Kleikamp transaction_t *transaction; 1983470decc6SDave Kleikamp struct buffer_head *bh = jh2bh(jh); 1984470decc6SDave Kleikamp 198546417064SThomas Gleixner lockdep_assert_held(&jh->b_state_lock); 1986470decc6SDave Kleikamp transaction = jh->b_transaction; 1987470decc6SDave Kleikamp if (transaction) 1988470decc6SDave Kleikamp assert_spin_locked(&transaction->t_journal->j_list_lock); 1989470decc6SDave Kleikamp 1990470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_jlist < BJ_Types); 1991470decc6SDave Kleikamp if (jh->b_jlist != BJ_None) 19924019191bSMingming Cao J_ASSERT_JH(jh, transaction != NULL); 1993470decc6SDave Kleikamp 1994470decc6SDave Kleikamp switch (jh->b_jlist) { 1995470decc6SDave Kleikamp case BJ_None: 1996470decc6SDave Kleikamp return; 1997470decc6SDave Kleikamp case BJ_Metadata: 1998470decc6SDave Kleikamp transaction->t_nr_buffers--; 1999470decc6SDave Kleikamp J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0); 2000470decc6SDave Kleikamp list = &transaction->t_buffers; 2001470decc6SDave Kleikamp break; 2002470decc6SDave Kleikamp case BJ_Forget: 2003470decc6SDave Kleikamp list = &transaction->t_forget; 2004470decc6SDave Kleikamp break; 2005470decc6SDave Kleikamp case BJ_Shadow: 2006470decc6SDave Kleikamp list = &transaction->t_shadow_list; 2007470decc6SDave Kleikamp break; 2008470decc6SDave Kleikamp case BJ_Reserved: 2009470decc6SDave Kleikamp list = &transaction->t_reserved_list; 2010470decc6SDave Kleikamp break; 2011470decc6SDave Kleikamp } 2012470decc6SDave Kleikamp 2013470decc6SDave Kleikamp __blist_del_buffer(list, jh); 2014470decc6SDave Kleikamp jh->b_jlist = BJ_None; 2015e112666bSTheodore Ts'o if (transaction && is_journal_aborted(transaction->t_journal)) 2016e112666bSTheodore Ts'o clear_buffer_jbddirty(bh); 2017e112666bSTheodore Ts'o else if (test_clear_buffer_jbddirty(bh)) 2018470decc6SDave Kleikamp mark_buffer_dirty(bh); /* Expose it to the VM */ 2019470decc6SDave Kleikamp } 2020470decc6SDave Kleikamp 2021de1b7941SJan Kara /* 202293108ebbSJan Kara * Remove buffer from all transactions. The caller is responsible for dropping 202393108ebbSJan Kara * the jh reference that belonged to the transaction. 2024de1b7941SJan Kara * 2025de1b7941SJan Kara * Called with bh_state lock and j_list_lock 2026de1b7941SJan Kara */ 2027de1b7941SJan Kara static void __jbd2_journal_unfile_buffer(struct journal_head *jh) 2028470decc6SDave Kleikamp { 2029f7f4bccbSMingming Cao __jbd2_journal_temp_unlink_buffer(jh); 2030470decc6SDave Kleikamp jh->b_transaction = NULL; 2031470decc6SDave Kleikamp } 2032470decc6SDave Kleikamp 2033f7f4bccbSMingming Cao void jbd2_journal_unfile_buffer(journal_t *journal, struct journal_head *jh) 2034470decc6SDave Kleikamp { 2035de1b7941SJan Kara struct buffer_head *bh = jh2bh(jh); 2036de1b7941SJan Kara 2037de1b7941SJan Kara /* Get reference so that buffer cannot be freed before we unlock it */ 2038de1b7941SJan Kara get_bh(bh); 203946417064SThomas Gleixner spin_lock(&jh->b_state_lock); 2040470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 2041f7f4bccbSMingming Cao __jbd2_journal_unfile_buffer(jh); 2042470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 204346417064SThomas Gleixner spin_unlock(&jh->b_state_lock); 204493108ebbSJan Kara jbd2_journal_put_journal_head(jh); 2045de1b7941SJan Kara __brelse(bh); 2046470decc6SDave Kleikamp } 2047470decc6SDave Kleikamp 2048470decc6SDave Kleikamp /* 2049f7f4bccbSMingming Cao * Called from jbd2_journal_try_to_free_buffers(). 2050470decc6SDave Kleikamp * 205146417064SThomas Gleixner * Called under jh->b_state_lock 2052470decc6SDave Kleikamp */ 2053470decc6SDave Kleikamp static void 2054470decc6SDave Kleikamp __journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh) 2055470decc6SDave Kleikamp { 2056470decc6SDave Kleikamp struct journal_head *jh; 2057470decc6SDave Kleikamp 2058470decc6SDave Kleikamp jh = bh2jh(bh); 2059470decc6SDave Kleikamp 2060470decc6SDave Kleikamp if (buffer_locked(bh) || buffer_dirty(bh)) 2061470decc6SDave Kleikamp goto out; 2062470decc6SDave Kleikamp 2063d2eb0b99STheodore Ts'o if (jh->b_next_transaction != NULL || jh->b_transaction != NULL) 2064470decc6SDave Kleikamp goto out; 2065470decc6SDave Kleikamp 2066470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 2067d2eb0b99STheodore Ts'o if (jh->b_cp_transaction != NULL) { 2068470decc6SDave Kleikamp /* written-back checkpointed metadata buffer */ 2069470decc6SDave Kleikamp JBUFFER_TRACE(jh, "remove from checkpoint list"); 2070f7f4bccbSMingming Cao __jbd2_journal_remove_checkpoint(jh); 2071470decc6SDave Kleikamp } 2072470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 2073470decc6SDave Kleikamp out: 2074470decc6SDave Kleikamp return; 2075470decc6SDave Kleikamp } 2076470decc6SDave Kleikamp 2077470decc6SDave Kleikamp /** 2078f7f4bccbSMingming Cao * int jbd2_journal_try_to_free_buffers() - try to free page buffers. 2079470decc6SDave Kleikamp * @journal: journal for operation 2080470decc6SDave Kleikamp * @page: to try and free 2081530576bbSMingming Cao * @gfp_mask: we use the mask to detect how hard should we try to release 2082d0164adcSMel Gorman * buffers. If __GFP_DIRECT_RECLAIM and __GFP_FS is set, we wait for commit 2083d0164adcSMel Gorman * code to release the buffers. 2084470decc6SDave Kleikamp * 2085470decc6SDave Kleikamp * 2086470decc6SDave Kleikamp * For all the buffers on this page, 2087470decc6SDave Kleikamp * if they are fully written out ordered data, move them onto BUF_CLEAN 2088470decc6SDave Kleikamp * so try_to_free_buffers() can reap them. 2089470decc6SDave Kleikamp * 2090470decc6SDave Kleikamp * This function returns non-zero if we wish try_to_free_buffers() 2091470decc6SDave Kleikamp * to be called. We do this if the page is releasable by try_to_free_buffers(). 2092470decc6SDave Kleikamp * We also do it if the page has locked or dirty buffers and the caller wants 2093470decc6SDave Kleikamp * us to perform sync or async writeout. 2094470decc6SDave Kleikamp * 2095470decc6SDave Kleikamp * This complicates JBD locking somewhat. We aren't protected by the 2096470decc6SDave Kleikamp * BKL here. We wish to remove the buffer from its committing or 2097f7f4bccbSMingming Cao * running transaction's ->t_datalist via __jbd2_journal_unfile_buffer. 2098470decc6SDave Kleikamp * 2099470decc6SDave Kleikamp * This may *change* the value of transaction_t->t_datalist, so anyone 2100470decc6SDave Kleikamp * who looks at t_datalist needs to lock against this function. 2101470decc6SDave Kleikamp * 2102f7f4bccbSMingming Cao * Even worse, someone may be doing a jbd2_journal_dirty_data on this 2103f7f4bccbSMingming Cao * buffer. So we need to lock against that. jbd2_journal_dirty_data() 2104470decc6SDave Kleikamp * will come out of the lock with the buffer dirty, which makes it 2105470decc6SDave Kleikamp * ineligible for release here. 2106470decc6SDave Kleikamp * 2107470decc6SDave Kleikamp * Who else is affected by this? hmm... Really the only contender 2108470decc6SDave Kleikamp * is do_get_write_access() - it could be looking at the buffer while 2109470decc6SDave Kleikamp * journal_try_to_free_buffer() is changing its state. But that 2110470decc6SDave Kleikamp * cannot happen because we never reallocate freed data as metadata 2111470decc6SDave Kleikamp * while the data is part of a transaction. Yes? 2112530576bbSMingming Cao * 2113530576bbSMingming Cao * Return 0 on failure, 1 on success 2114470decc6SDave Kleikamp */ 2115f7f4bccbSMingming Cao int jbd2_journal_try_to_free_buffers(journal_t *journal, 2116530576bbSMingming Cao struct page *page, gfp_t gfp_mask) 2117470decc6SDave Kleikamp { 2118470decc6SDave Kleikamp struct buffer_head *head; 2119470decc6SDave Kleikamp struct buffer_head *bh; 2120470decc6SDave Kleikamp int ret = 0; 2121470decc6SDave Kleikamp 2122470decc6SDave Kleikamp J_ASSERT(PageLocked(page)); 2123470decc6SDave Kleikamp 2124470decc6SDave Kleikamp head = page_buffers(page); 2125470decc6SDave Kleikamp bh = head; 2126470decc6SDave Kleikamp do { 2127470decc6SDave Kleikamp struct journal_head *jh; 2128470decc6SDave Kleikamp 2129470decc6SDave Kleikamp /* 2130470decc6SDave Kleikamp * We take our own ref against the journal_head here to avoid 2131470decc6SDave Kleikamp * having to add tons of locking around each instance of 2132530576bbSMingming Cao * jbd2_journal_put_journal_head(). 2133470decc6SDave Kleikamp */ 2134f7f4bccbSMingming Cao jh = jbd2_journal_grab_journal_head(bh); 2135470decc6SDave Kleikamp if (!jh) 2136470decc6SDave Kleikamp continue; 2137470decc6SDave Kleikamp 213846417064SThomas Gleixner spin_lock(&jh->b_state_lock); 2139470decc6SDave Kleikamp __journal_try_to_free_buffer(journal, bh); 214046417064SThomas Gleixner spin_unlock(&jh->b_state_lock); 2141f7f4bccbSMingming Cao jbd2_journal_put_journal_head(jh); 2142470decc6SDave Kleikamp if (buffer_jbd(bh)) 2143470decc6SDave Kleikamp goto busy; 2144470decc6SDave Kleikamp } while ((bh = bh->b_this_page) != head); 2145530576bbSMingming Cao 2146470decc6SDave Kleikamp ret = try_to_free_buffers(page); 2147530576bbSMingming Cao 2148470decc6SDave Kleikamp busy: 2149470decc6SDave Kleikamp return ret; 2150470decc6SDave Kleikamp } 2151470decc6SDave Kleikamp 2152470decc6SDave Kleikamp /* 2153470decc6SDave Kleikamp * This buffer is no longer needed. If it is on an older transaction's 2154470decc6SDave Kleikamp * checkpoint list we need to record it on this transaction's forget list 2155470decc6SDave Kleikamp * to pin this buffer (and hence its checkpointing transaction) down until 2156470decc6SDave Kleikamp * this transaction commits. If the buffer isn't on a checkpoint list, we 2157470decc6SDave Kleikamp * release it. 2158470decc6SDave Kleikamp * Returns non-zero if JBD no longer has an interest in the buffer. 2159470decc6SDave Kleikamp * 2160470decc6SDave Kleikamp * Called under j_list_lock. 2161470decc6SDave Kleikamp * 216246417064SThomas Gleixner * Called under jh->b_state_lock. 2163470decc6SDave Kleikamp */ 2164470decc6SDave Kleikamp static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction) 2165470decc6SDave Kleikamp { 2166470decc6SDave Kleikamp int may_free = 1; 2167470decc6SDave Kleikamp struct buffer_head *bh = jh2bh(jh); 2168470decc6SDave Kleikamp 2169470decc6SDave Kleikamp if (jh->b_cp_transaction) { 2170470decc6SDave Kleikamp JBUFFER_TRACE(jh, "on running+cp transaction"); 2171de1b7941SJan Kara __jbd2_journal_temp_unlink_buffer(jh); 2172f91d1d04SJan Kara /* 2173f91d1d04SJan Kara * We don't want to write the buffer anymore, clear the 2174f91d1d04SJan Kara * bit so that we don't confuse checks in 2175f91d1d04SJan Kara * __journal_file_buffer 2176f91d1d04SJan Kara */ 2177f91d1d04SJan Kara clear_buffer_dirty(bh); 2178f7f4bccbSMingming Cao __jbd2_journal_file_buffer(jh, transaction, BJ_Forget); 2179470decc6SDave Kleikamp may_free = 0; 2180470decc6SDave Kleikamp } else { 2181470decc6SDave Kleikamp JBUFFER_TRACE(jh, "on running transaction"); 2182de1b7941SJan Kara __jbd2_journal_unfile_buffer(jh); 218393108ebbSJan Kara jbd2_journal_put_journal_head(jh); 2184470decc6SDave Kleikamp } 2185470decc6SDave Kleikamp return may_free; 2186470decc6SDave Kleikamp } 2187470decc6SDave Kleikamp 2188470decc6SDave Kleikamp /* 2189f7f4bccbSMingming Cao * jbd2_journal_invalidatepage 2190470decc6SDave Kleikamp * 2191470decc6SDave Kleikamp * This code is tricky. It has a number of cases to deal with. 2192470decc6SDave Kleikamp * 2193470decc6SDave Kleikamp * There are two invariants which this code relies on: 2194470decc6SDave Kleikamp * 2195470decc6SDave Kleikamp * i_size must be updated on disk before we start calling invalidatepage on the 2196470decc6SDave Kleikamp * data. 2197470decc6SDave Kleikamp * 2198470decc6SDave Kleikamp * This is done in ext3 by defining an ext3_setattr method which 2199470decc6SDave Kleikamp * updates i_size before truncate gets going. By maintaining this 2200470decc6SDave Kleikamp * invariant, we can be sure that it is safe to throw away any buffers 2201470decc6SDave Kleikamp * attached to the current transaction: once the transaction commits, 2202470decc6SDave Kleikamp * we know that the data will not be needed. 2203470decc6SDave Kleikamp * 2204470decc6SDave Kleikamp * Note however that we can *not* throw away data belonging to the 2205470decc6SDave Kleikamp * previous, committing transaction! 2206470decc6SDave Kleikamp * 2207470decc6SDave Kleikamp * Any disk blocks which *are* part of the previous, committing 2208470decc6SDave Kleikamp * transaction (and which therefore cannot be discarded immediately) are 2209470decc6SDave Kleikamp * not going to be reused in the new running transaction 2210470decc6SDave Kleikamp * 2211470decc6SDave Kleikamp * The bitmap committed_data images guarantee this: any block which is 2212470decc6SDave Kleikamp * allocated in one transaction and removed in the next will be marked 2213470decc6SDave Kleikamp * as in-use in the committed_data bitmap, so cannot be reused until 2214470decc6SDave Kleikamp * the next transaction to delete the block commits. This means that 2215470decc6SDave Kleikamp * leaving committing buffers dirty is quite safe: the disk blocks 2216470decc6SDave Kleikamp * cannot be reallocated to a different file and so buffer aliasing is 2217470decc6SDave Kleikamp * not possible. 2218470decc6SDave Kleikamp * 2219470decc6SDave Kleikamp * 2220470decc6SDave Kleikamp * The above applies mainly to ordered data mode. In writeback mode we 2221470decc6SDave Kleikamp * don't make guarantees about the order in which data hits disk --- in 2222470decc6SDave Kleikamp * particular we don't guarantee that new dirty data is flushed before 2223470decc6SDave Kleikamp * transaction commit --- so it is always safe just to discard data 2224470decc6SDave Kleikamp * immediately in that mode. --sct 2225470decc6SDave Kleikamp */ 2226470decc6SDave Kleikamp 2227470decc6SDave Kleikamp /* 2228470decc6SDave Kleikamp * The journal_unmap_buffer helper function returns zero if the buffer 2229470decc6SDave Kleikamp * concerned remains pinned as an anonymous buffer belonging to an older 2230470decc6SDave Kleikamp * transaction. 2231470decc6SDave Kleikamp * 2232470decc6SDave Kleikamp * We're outside-transaction here. Either or both of j_running_transaction 2233470decc6SDave Kleikamp * and j_committing_transaction may be NULL. 2234470decc6SDave Kleikamp */ 2235b794e7a6SJan Kara static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh, 2236b794e7a6SJan Kara int partial_page) 2237470decc6SDave Kleikamp { 2238470decc6SDave Kleikamp transaction_t *transaction; 2239470decc6SDave Kleikamp struct journal_head *jh; 2240470decc6SDave Kleikamp int may_free = 1; 2241470decc6SDave Kleikamp 2242470decc6SDave Kleikamp BUFFER_TRACE(bh, "entry"); 2243470decc6SDave Kleikamp 2244470decc6SDave Kleikamp /* 2245470decc6SDave Kleikamp * It is safe to proceed here without the j_list_lock because the 2246470decc6SDave Kleikamp * buffers cannot be stolen by try_to_free_buffers as long as we are 2247470decc6SDave Kleikamp * holding the page lock. --sct 2248470decc6SDave Kleikamp */ 2249470decc6SDave Kleikamp 2250d84560f7SThomas Gleixner jh = jbd2_journal_grab_journal_head(bh); 2251d84560f7SThomas Gleixner if (!jh) 2252470decc6SDave Kleikamp goto zap_buffer_unlocked; 2253470decc6SDave Kleikamp 225487c89c23SJan Kara /* OK, we have data buffer in journaled mode */ 2255a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 225646417064SThomas Gleixner spin_lock(&jh->b_state_lock); 2257470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 2258470decc6SDave Kleikamp 2259ba869023Sdingdinghua /* 2260ba869023Sdingdinghua * We cannot remove the buffer from checkpoint lists until the 2261ba869023Sdingdinghua * transaction adding inode to orphan list (let's call it T) 2262ba869023Sdingdinghua * is committed. Otherwise if the transaction changing the 2263ba869023Sdingdinghua * buffer would be cleaned from the journal before T is 2264ba869023Sdingdinghua * committed, a crash will cause that the correct contents of 2265ba869023Sdingdinghua * the buffer will be lost. On the other hand we have to 2266ba869023Sdingdinghua * clear the buffer dirty bit at latest at the moment when the 2267ba869023Sdingdinghua * transaction marking the buffer as freed in the filesystem 2268ba869023Sdingdinghua * structures is committed because from that moment on the 2269b794e7a6SJan Kara * block can be reallocated and used by a different page. 2270ba869023Sdingdinghua * Since the block hasn't been freed yet but the inode has 2271ba869023Sdingdinghua * already been added to orphan list, it is safe for us to add 2272ba869023Sdingdinghua * the buffer to BJ_Forget list of the newest transaction. 2273b794e7a6SJan Kara * 2274b794e7a6SJan Kara * Also we have to clear buffer_mapped flag of a truncated buffer 2275b794e7a6SJan Kara * because the buffer_head may be attached to the page straddling 2276b794e7a6SJan Kara * i_size (can happen only when blocksize < pagesize) and thus the 2277b794e7a6SJan Kara * buffer_head can be reused when the file is extended again. So we end 2278b794e7a6SJan Kara * up keeping around invalidated buffers attached to transactions' 2279b794e7a6SJan Kara * BJ_Forget list just to stop checkpointing code from cleaning up 2280b794e7a6SJan Kara * the transaction this buffer was modified in. 2281ba869023Sdingdinghua */ 2282470decc6SDave Kleikamp transaction = jh->b_transaction; 2283470decc6SDave Kleikamp if (transaction == NULL) { 2284470decc6SDave Kleikamp /* First case: not on any transaction. If it 2285470decc6SDave Kleikamp * has no checkpoint link, then we can zap it: 2286470decc6SDave Kleikamp * it's a writeback-mode buffer so we don't care 2287470decc6SDave Kleikamp * if it hits disk safely. */ 2288470decc6SDave Kleikamp if (!jh->b_cp_transaction) { 2289470decc6SDave Kleikamp JBUFFER_TRACE(jh, "not on any transaction: zap"); 2290470decc6SDave Kleikamp goto zap_buffer; 2291470decc6SDave Kleikamp } 2292470decc6SDave Kleikamp 2293470decc6SDave Kleikamp if (!buffer_dirty(bh)) { 2294470decc6SDave Kleikamp /* bdflush has written it. We can drop it now */ 2295bc23f0c8SJan Kara __jbd2_journal_remove_checkpoint(jh); 2296470decc6SDave Kleikamp goto zap_buffer; 2297470decc6SDave Kleikamp } 2298470decc6SDave Kleikamp 2299470decc6SDave Kleikamp /* OK, it must be in the journal but still not 2300470decc6SDave Kleikamp * written fully to disk: it's metadata or 2301470decc6SDave Kleikamp * journaled data... */ 2302470decc6SDave Kleikamp 2303470decc6SDave Kleikamp if (journal->j_running_transaction) { 2304470decc6SDave Kleikamp /* ... and once the current transaction has 2305470decc6SDave Kleikamp * committed, the buffer won't be needed any 2306470decc6SDave Kleikamp * longer. */ 2307470decc6SDave Kleikamp JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget"); 2308b794e7a6SJan Kara may_free = __dispose_buffer(jh, 2309470decc6SDave Kleikamp journal->j_running_transaction); 2310b794e7a6SJan Kara goto zap_buffer; 2311470decc6SDave Kleikamp } else { 2312470decc6SDave Kleikamp /* There is no currently-running transaction. So the 2313470decc6SDave Kleikamp * orphan record which we wrote for this file must have 2314470decc6SDave Kleikamp * passed into commit. We must attach this buffer to 2315470decc6SDave Kleikamp * the committing transaction, if it exists. */ 2316470decc6SDave Kleikamp if (journal->j_committing_transaction) { 2317470decc6SDave Kleikamp JBUFFER_TRACE(jh, "give to committing trans"); 2318b794e7a6SJan Kara may_free = __dispose_buffer(jh, 2319470decc6SDave Kleikamp journal->j_committing_transaction); 2320b794e7a6SJan Kara goto zap_buffer; 2321470decc6SDave Kleikamp } else { 2322470decc6SDave Kleikamp /* The orphan record's transaction has 2323470decc6SDave Kleikamp * committed. We can cleanse this buffer */ 2324470decc6SDave Kleikamp clear_buffer_jbddirty(bh); 2325bc23f0c8SJan Kara __jbd2_journal_remove_checkpoint(jh); 2326470decc6SDave Kleikamp goto zap_buffer; 2327470decc6SDave Kleikamp } 2328470decc6SDave Kleikamp } 2329470decc6SDave Kleikamp } else if (transaction == journal->j_committing_transaction) { 23309b57988dSEric Sandeen JBUFFER_TRACE(jh, "on committing transaction"); 2331470decc6SDave Kleikamp /* 2332ba869023Sdingdinghua * The buffer is committing, we simply cannot touch 2333b794e7a6SJan Kara * it. If the page is straddling i_size we have to wait 2334b794e7a6SJan Kara * for commit and try again. 2335b794e7a6SJan Kara */ 2336b794e7a6SJan Kara if (partial_page) { 2337b794e7a6SJan Kara spin_unlock(&journal->j_list_lock); 233846417064SThomas Gleixner spin_unlock(&jh->b_state_lock); 2339b794e7a6SJan Kara write_unlock(&journal->j_state_lock); 234046417064SThomas Gleixner jbd2_journal_put_journal_head(jh); 234153e87268SJan Kara return -EBUSY; 2342b794e7a6SJan Kara } 2343b794e7a6SJan Kara /* 23446a66a7deSzhangyi (F) * OK, buffer won't be reachable after truncate. We just clear 23456a66a7deSzhangyi (F) * b_modified to not confuse transaction credit accounting, and 23466a66a7deSzhangyi (F) * set j_next_transaction to the running transaction (if there 23476a66a7deSzhangyi (F) * is one) and mark buffer as freed so that commit code knows 23486a66a7deSzhangyi (F) * it should clear dirty bits when it is done with the buffer. 2349ba869023Sdingdinghua */ 2350470decc6SDave Kleikamp set_buffer_freed(bh); 2351ba869023Sdingdinghua if (journal->j_running_transaction && buffer_jbddirty(bh)) 2352ba869023Sdingdinghua jh->b_next_transaction = journal->j_running_transaction; 23536a66a7deSzhangyi (F) jh->b_modified = 0; 2354470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 235546417064SThomas Gleixner spin_unlock(&jh->b_state_lock); 2356a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 235746417064SThomas Gleixner jbd2_journal_put_journal_head(jh); 2358470decc6SDave Kleikamp return 0; 2359470decc6SDave Kleikamp } else { 2360470decc6SDave Kleikamp /* Good, the buffer belongs to the running transaction. 2361470decc6SDave Kleikamp * We are writing our own transaction's data, not any 2362470decc6SDave Kleikamp * previous one's, so it is safe to throw it away 2363470decc6SDave Kleikamp * (remember that we expect the filesystem to have set 2364470decc6SDave Kleikamp * i_size already for this truncate so recovery will not 2365470decc6SDave Kleikamp * expose the disk blocks we are discarding here.) */ 2366470decc6SDave Kleikamp J_ASSERT_JH(jh, transaction == journal->j_running_transaction); 23679b57988dSEric Sandeen JBUFFER_TRACE(jh, "on running transaction"); 2368470decc6SDave Kleikamp may_free = __dispose_buffer(jh, transaction); 2369470decc6SDave Kleikamp } 2370470decc6SDave Kleikamp 2371470decc6SDave Kleikamp zap_buffer: 2372b794e7a6SJan Kara /* 2373b794e7a6SJan Kara * This is tricky. Although the buffer is truncated, it may be reused 2374b794e7a6SJan Kara * if blocksize < pagesize and it is attached to the page straddling 2375b794e7a6SJan Kara * EOF. Since the buffer might have been added to BJ_Forget list of the 2376b794e7a6SJan Kara * running transaction, journal_get_write_access() won't clear 2377b794e7a6SJan Kara * b_modified and credit accounting gets confused. So clear b_modified 2378b794e7a6SJan Kara * here. 2379b794e7a6SJan Kara */ 2380b794e7a6SJan Kara jh->b_modified = 0; 2381470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 238246417064SThomas Gleixner spin_unlock(&jh->b_state_lock); 2383a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 238446417064SThomas Gleixner jbd2_journal_put_journal_head(jh); 2385470decc6SDave Kleikamp zap_buffer_unlocked: 2386470decc6SDave Kleikamp clear_buffer_dirty(bh); 2387470decc6SDave Kleikamp J_ASSERT_BH(bh, !buffer_jbddirty(bh)); 2388470decc6SDave Kleikamp clear_buffer_mapped(bh); 2389470decc6SDave Kleikamp clear_buffer_req(bh); 2390470decc6SDave Kleikamp clear_buffer_new(bh); 239115291164SEric Sandeen clear_buffer_delay(bh); 239215291164SEric Sandeen clear_buffer_unwritten(bh); 2393470decc6SDave Kleikamp bh->b_bdev = NULL; 2394470decc6SDave Kleikamp return may_free; 2395470decc6SDave Kleikamp } 2396470decc6SDave Kleikamp 2397470decc6SDave Kleikamp /** 2398f7f4bccbSMingming Cao * void jbd2_journal_invalidatepage() 2399470decc6SDave Kleikamp * @journal: journal to use for flush... 2400470decc6SDave Kleikamp * @page: page to flush 2401259709b0SLukas Czerner * @offset: start of the range to invalidate 2402259709b0SLukas Czerner * @length: length of the range to invalidate 2403470decc6SDave Kleikamp * 2404259709b0SLukas Czerner * Reap page buffers containing data after in the specified range in page. 2405259709b0SLukas Czerner * Can return -EBUSY if buffers are part of the committing transaction and 2406259709b0SLukas Czerner * the page is straddling i_size. Caller then has to wait for current commit 2407259709b0SLukas Czerner * and try again. 2408470decc6SDave Kleikamp */ 240953e87268SJan Kara int jbd2_journal_invalidatepage(journal_t *journal, 2410470decc6SDave Kleikamp struct page *page, 2411259709b0SLukas Czerner unsigned int offset, 2412259709b0SLukas Czerner unsigned int length) 2413470decc6SDave Kleikamp { 2414470decc6SDave Kleikamp struct buffer_head *head, *bh, *next; 2415259709b0SLukas Czerner unsigned int stop = offset + length; 2416470decc6SDave Kleikamp unsigned int curr_off = 0; 241709cbfeafSKirill A. Shutemov int partial_page = (offset || length < PAGE_SIZE); 2418470decc6SDave Kleikamp int may_free = 1; 241953e87268SJan Kara int ret = 0; 2420470decc6SDave Kleikamp 2421470decc6SDave Kleikamp if (!PageLocked(page)) 2422470decc6SDave Kleikamp BUG(); 2423470decc6SDave Kleikamp if (!page_has_buffers(page)) 242453e87268SJan Kara return 0; 2425470decc6SDave Kleikamp 242609cbfeafSKirill A. Shutemov BUG_ON(stop > PAGE_SIZE || stop < length); 2427259709b0SLukas Czerner 2428470decc6SDave Kleikamp /* We will potentially be playing with lists other than just the 2429470decc6SDave Kleikamp * data lists (especially for journaled data mode), so be 2430470decc6SDave Kleikamp * cautious in our locking. */ 2431470decc6SDave Kleikamp 2432470decc6SDave Kleikamp head = bh = page_buffers(page); 2433470decc6SDave Kleikamp do { 2434470decc6SDave Kleikamp unsigned int next_off = curr_off + bh->b_size; 2435470decc6SDave Kleikamp next = bh->b_this_page; 2436470decc6SDave Kleikamp 2437259709b0SLukas Czerner if (next_off > stop) 2438259709b0SLukas Czerner return 0; 2439259709b0SLukas Czerner 2440470decc6SDave Kleikamp if (offset <= curr_off) { 2441470decc6SDave Kleikamp /* This block is wholly outside the truncation point */ 2442470decc6SDave Kleikamp lock_buffer(bh); 2443259709b0SLukas Czerner ret = journal_unmap_buffer(journal, bh, partial_page); 2444470decc6SDave Kleikamp unlock_buffer(bh); 244553e87268SJan Kara if (ret < 0) 244653e87268SJan Kara return ret; 244753e87268SJan Kara may_free &= ret; 2448470decc6SDave Kleikamp } 2449470decc6SDave Kleikamp curr_off = next_off; 2450470decc6SDave Kleikamp bh = next; 2451470decc6SDave Kleikamp 2452470decc6SDave Kleikamp } while (bh != head); 2453470decc6SDave Kleikamp 2454259709b0SLukas Czerner if (!partial_page) { 2455470decc6SDave Kleikamp if (may_free && try_to_free_buffers(page)) 2456470decc6SDave Kleikamp J_ASSERT(!page_has_buffers(page)); 2457470decc6SDave Kleikamp } 245853e87268SJan Kara return 0; 2459470decc6SDave Kleikamp } 2460470decc6SDave Kleikamp 2461470decc6SDave Kleikamp /* 2462470decc6SDave Kleikamp * File a buffer on the given transaction list. 2463470decc6SDave Kleikamp */ 2464f7f4bccbSMingming Cao void __jbd2_journal_file_buffer(struct journal_head *jh, 2465470decc6SDave Kleikamp transaction_t *transaction, int jlist) 2466470decc6SDave Kleikamp { 2467470decc6SDave Kleikamp struct journal_head **list = NULL; 2468470decc6SDave Kleikamp int was_dirty = 0; 2469470decc6SDave Kleikamp struct buffer_head *bh = jh2bh(jh); 2470470decc6SDave Kleikamp 247146417064SThomas Gleixner lockdep_assert_held(&jh->b_state_lock); 2472470decc6SDave Kleikamp assert_spin_locked(&transaction->t_journal->j_list_lock); 2473470decc6SDave Kleikamp 2474470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_jlist < BJ_Types); 2475470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_transaction == transaction || 24764019191bSMingming Cao jh->b_transaction == NULL); 2477470decc6SDave Kleikamp 2478470decc6SDave Kleikamp if (jh->b_transaction && jh->b_jlist == jlist) 2479470decc6SDave Kleikamp return; 2480470decc6SDave Kleikamp 2481470decc6SDave Kleikamp if (jlist == BJ_Metadata || jlist == BJ_Reserved || 2482470decc6SDave Kleikamp jlist == BJ_Shadow || jlist == BJ_Forget) { 2483f91d1d04SJan Kara /* 2484f91d1d04SJan Kara * For metadata buffers, we track dirty bit in buffer_jbddirty 2485f91d1d04SJan Kara * instead of buffer_dirty. We should not see a dirty bit set 2486f91d1d04SJan Kara * here because we clear it in do_get_write_access but e.g. 2487f91d1d04SJan Kara * tune2fs can modify the sb and set the dirty bit at any time 2488f91d1d04SJan Kara * so we try to gracefully handle that. 2489f91d1d04SJan Kara */ 2490f91d1d04SJan Kara if (buffer_dirty(bh)) 2491f91d1d04SJan Kara warn_dirty_buffer(bh); 2492470decc6SDave Kleikamp if (test_clear_buffer_dirty(bh) || 2493470decc6SDave Kleikamp test_clear_buffer_jbddirty(bh)) 2494470decc6SDave Kleikamp was_dirty = 1; 2495470decc6SDave Kleikamp } 2496470decc6SDave Kleikamp 2497470decc6SDave Kleikamp if (jh->b_transaction) 2498f7f4bccbSMingming Cao __jbd2_journal_temp_unlink_buffer(jh); 2499de1b7941SJan Kara else 2500de1b7941SJan Kara jbd2_journal_grab_journal_head(bh); 2501470decc6SDave Kleikamp jh->b_transaction = transaction; 2502470decc6SDave Kleikamp 2503470decc6SDave Kleikamp switch (jlist) { 2504470decc6SDave Kleikamp case BJ_None: 2505470decc6SDave Kleikamp J_ASSERT_JH(jh, !jh->b_committed_data); 2506470decc6SDave Kleikamp J_ASSERT_JH(jh, !jh->b_frozen_data); 2507470decc6SDave Kleikamp return; 2508470decc6SDave Kleikamp case BJ_Metadata: 2509470decc6SDave Kleikamp transaction->t_nr_buffers++; 2510470decc6SDave Kleikamp list = &transaction->t_buffers; 2511470decc6SDave Kleikamp break; 2512470decc6SDave Kleikamp case BJ_Forget: 2513470decc6SDave Kleikamp list = &transaction->t_forget; 2514470decc6SDave Kleikamp break; 2515470decc6SDave Kleikamp case BJ_Shadow: 2516470decc6SDave Kleikamp list = &transaction->t_shadow_list; 2517470decc6SDave Kleikamp break; 2518470decc6SDave Kleikamp case BJ_Reserved: 2519470decc6SDave Kleikamp list = &transaction->t_reserved_list; 2520470decc6SDave Kleikamp break; 2521470decc6SDave Kleikamp } 2522470decc6SDave Kleikamp 2523470decc6SDave Kleikamp __blist_add_buffer(list, jh); 2524470decc6SDave Kleikamp jh->b_jlist = jlist; 2525470decc6SDave Kleikamp 2526470decc6SDave Kleikamp if (was_dirty) 2527470decc6SDave Kleikamp set_buffer_jbddirty(bh); 2528470decc6SDave Kleikamp } 2529470decc6SDave Kleikamp 2530f7f4bccbSMingming Cao void jbd2_journal_file_buffer(struct journal_head *jh, 2531470decc6SDave Kleikamp transaction_t *transaction, int jlist) 2532470decc6SDave Kleikamp { 253346417064SThomas Gleixner spin_lock(&jh->b_state_lock); 2534470decc6SDave Kleikamp spin_lock(&transaction->t_journal->j_list_lock); 2535f7f4bccbSMingming Cao __jbd2_journal_file_buffer(jh, transaction, jlist); 2536470decc6SDave Kleikamp spin_unlock(&transaction->t_journal->j_list_lock); 253746417064SThomas Gleixner spin_unlock(&jh->b_state_lock); 2538470decc6SDave Kleikamp } 2539470decc6SDave Kleikamp 2540470decc6SDave Kleikamp /* 2541470decc6SDave Kleikamp * Remove a buffer from its current buffer list in preparation for 2542470decc6SDave Kleikamp * dropping it from its current transaction entirely. If the buffer has 2543470decc6SDave Kleikamp * already started to be used by a subsequent transaction, refile the 2544470decc6SDave Kleikamp * buffer on that transaction's metadata list. 2545470decc6SDave Kleikamp * 2546de1b7941SJan Kara * Called under j_list_lock 254746417064SThomas Gleixner * Called under jh->b_state_lock 2548de1b7941SJan Kara * 254993108ebbSJan Kara * When this function returns true, there's no next transaction to refile to 255093108ebbSJan Kara * and the caller has to drop jh reference through 255193108ebbSJan Kara * jbd2_journal_put_journal_head(). 2552470decc6SDave Kleikamp */ 255393108ebbSJan Kara bool __jbd2_journal_refile_buffer(struct journal_head *jh) 2554470decc6SDave Kleikamp { 2555ba869023Sdingdinghua int was_dirty, jlist; 2556470decc6SDave Kleikamp struct buffer_head *bh = jh2bh(jh); 2557470decc6SDave Kleikamp 255846417064SThomas Gleixner lockdep_assert_held(&jh->b_state_lock); 2559470decc6SDave Kleikamp if (jh->b_transaction) 2560470decc6SDave Kleikamp assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock); 2561470decc6SDave Kleikamp 2562470decc6SDave Kleikamp /* If the buffer is now unused, just drop it. */ 2563470decc6SDave Kleikamp if (jh->b_next_transaction == NULL) { 2564f7f4bccbSMingming Cao __jbd2_journal_unfile_buffer(jh); 256593108ebbSJan Kara return true; 2566470decc6SDave Kleikamp } 2567470decc6SDave Kleikamp 2568470decc6SDave Kleikamp /* 2569470decc6SDave Kleikamp * It has been modified by a later transaction: add it to the new 2570470decc6SDave Kleikamp * transaction's metadata list. 2571470decc6SDave Kleikamp */ 2572470decc6SDave Kleikamp 2573470decc6SDave Kleikamp was_dirty = test_clear_buffer_jbddirty(bh); 2574f7f4bccbSMingming Cao __jbd2_journal_temp_unlink_buffer(jh); 2575de1b7941SJan Kara /* 2576de1b7941SJan Kara * We set b_transaction here because b_next_transaction will inherit 2577de1b7941SJan Kara * our jh reference and thus __jbd2_journal_file_buffer() must not 2578de1b7941SJan Kara * take a new one. 2579de1b7941SJan Kara */ 25806c5d9112SQian Cai WRITE_ONCE(jh->b_transaction, jh->b_next_transaction); 25816c5d9112SQian Cai WRITE_ONCE(jh->b_next_transaction, NULL); 2582ba869023Sdingdinghua if (buffer_freed(bh)) 2583ba869023Sdingdinghua jlist = BJ_Forget; 2584ba869023Sdingdinghua else if (jh->b_modified) 2585ba869023Sdingdinghua jlist = BJ_Metadata; 2586ba869023Sdingdinghua else 2587ba869023Sdingdinghua jlist = BJ_Reserved; 2588ba869023Sdingdinghua __jbd2_journal_file_buffer(jh, jh->b_transaction, jlist); 2589470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING); 2590470decc6SDave Kleikamp 2591470decc6SDave Kleikamp if (was_dirty) 2592470decc6SDave Kleikamp set_buffer_jbddirty(bh); 259393108ebbSJan Kara return false; 2594470decc6SDave Kleikamp } 2595470decc6SDave Kleikamp 2596470decc6SDave Kleikamp /* 2597de1b7941SJan Kara * __jbd2_journal_refile_buffer() with necessary locking added. We take our 2598de1b7941SJan Kara * bh reference so that we can safely unlock bh. 2599470decc6SDave Kleikamp * 2600de1b7941SJan Kara * The jh and bh may be freed by this call. 2601470decc6SDave Kleikamp */ 2602f7f4bccbSMingming Cao void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh) 2603470decc6SDave Kleikamp { 260493108ebbSJan Kara bool drop; 2605470decc6SDave Kleikamp 260646417064SThomas Gleixner spin_lock(&jh->b_state_lock); 2607470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 260893108ebbSJan Kara drop = __jbd2_journal_refile_buffer(jh); 260946417064SThomas Gleixner spin_unlock(&jh->b_state_lock); 2610470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 261193108ebbSJan Kara if (drop) 261293108ebbSJan Kara jbd2_journal_put_journal_head(jh); 2613470decc6SDave Kleikamp } 2614c851ed54SJan Kara 2615c851ed54SJan Kara /* 2616c851ed54SJan Kara * File inode in the inode list of the handle's transaction 2617c851ed54SJan Kara */ 261841617e1aSJan Kara static int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode, 26196ba0e7dcSRoss Zwisler unsigned long flags, loff_t start_byte, loff_t end_byte) 2620c851ed54SJan Kara { 2621c851ed54SJan Kara transaction_t *transaction = handle->h_transaction; 262241a5b913STheodore Ts'o journal_t *journal; 2623c851ed54SJan Kara 2624c851ed54SJan Kara if (is_handle_aborted(handle)) 262541a5b913STheodore Ts'o return -EROFS; 262641a5b913STheodore Ts'o journal = transaction->t_journal; 2627c851ed54SJan Kara 2628c851ed54SJan Kara jbd_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino, 2629c851ed54SJan Kara transaction->t_tid); 2630c851ed54SJan Kara 2631c851ed54SJan Kara spin_lock(&journal->j_list_lock); 263241617e1aSJan Kara jinode->i_flags |= flags; 26336ba0e7dcSRoss Zwisler 26346ba0e7dcSRoss Zwisler if (jinode->i_dirty_end) { 26356ba0e7dcSRoss Zwisler jinode->i_dirty_start = min(jinode->i_dirty_start, start_byte); 26366ba0e7dcSRoss Zwisler jinode->i_dirty_end = max(jinode->i_dirty_end, end_byte); 26376ba0e7dcSRoss Zwisler } else { 26386ba0e7dcSRoss Zwisler jinode->i_dirty_start = start_byte; 26396ba0e7dcSRoss Zwisler jinode->i_dirty_end = end_byte; 26406ba0e7dcSRoss Zwisler } 26416ba0e7dcSRoss Zwisler 264241617e1aSJan Kara /* Is inode already attached where we need it? */ 2643c851ed54SJan Kara if (jinode->i_transaction == transaction || 2644c851ed54SJan Kara jinode->i_next_transaction == transaction) 2645c851ed54SJan Kara goto done; 2646c851ed54SJan Kara 264781be12c8SJan Kara /* 264881be12c8SJan Kara * We only ever set this variable to 1 so the test is safe. Since 264981be12c8SJan Kara * t_need_data_flush is likely to be set, we do the test to save some 265081be12c8SJan Kara * cacheline bouncing 265181be12c8SJan Kara */ 265281be12c8SJan Kara if (!transaction->t_need_data_flush) 265381be12c8SJan Kara transaction->t_need_data_flush = 1; 2654c851ed54SJan Kara /* On some different transaction's list - should be 2655c851ed54SJan Kara * the committing one */ 2656c851ed54SJan Kara if (jinode->i_transaction) { 2657c851ed54SJan Kara J_ASSERT(jinode->i_next_transaction == NULL); 2658c851ed54SJan Kara J_ASSERT(jinode->i_transaction == 2659c851ed54SJan Kara journal->j_committing_transaction); 2660c851ed54SJan Kara jinode->i_next_transaction = transaction; 2661c851ed54SJan Kara goto done; 2662c851ed54SJan Kara } 2663c851ed54SJan Kara /* Not on any transaction list... */ 2664c851ed54SJan Kara J_ASSERT(!jinode->i_next_transaction); 2665c851ed54SJan Kara jinode->i_transaction = transaction; 2666c851ed54SJan Kara list_add(&jinode->i_list, &transaction->t_inode_list); 2667c851ed54SJan Kara done: 2668c851ed54SJan Kara spin_unlock(&journal->j_list_lock); 2669c851ed54SJan Kara 2670c851ed54SJan Kara return 0; 2671c851ed54SJan Kara } 2672c851ed54SJan Kara 26736ba0e7dcSRoss Zwisler int jbd2_journal_inode_ranged_write(handle_t *handle, 26746ba0e7dcSRoss Zwisler struct jbd2_inode *jinode, loff_t start_byte, loff_t length) 26756ba0e7dcSRoss Zwisler { 26766ba0e7dcSRoss Zwisler return jbd2_journal_file_inode(handle, jinode, 26776ba0e7dcSRoss Zwisler JI_WRITE_DATA | JI_WAIT_DATA, start_byte, 26786ba0e7dcSRoss Zwisler start_byte + length - 1); 26796ba0e7dcSRoss Zwisler } 26806ba0e7dcSRoss Zwisler 26816ba0e7dcSRoss Zwisler int jbd2_journal_inode_ranged_wait(handle_t *handle, struct jbd2_inode *jinode, 26826ba0e7dcSRoss Zwisler loff_t start_byte, loff_t length) 26836ba0e7dcSRoss Zwisler { 26846ba0e7dcSRoss Zwisler return jbd2_journal_file_inode(handle, jinode, JI_WAIT_DATA, 26856ba0e7dcSRoss Zwisler start_byte, start_byte + length - 1); 268641617e1aSJan Kara } 268741617e1aSJan Kara 2688c851ed54SJan Kara /* 26897f5aa215SJan Kara * File truncate and transaction commit interact with each other in a 26907f5aa215SJan Kara * non-trivial way. If a transaction writing data block A is 26917f5aa215SJan Kara * committing, we cannot discard the data by truncate until we have 26927f5aa215SJan Kara * written them. Otherwise if we crashed after the transaction with 26937f5aa215SJan Kara * write has committed but before the transaction with truncate has 26947f5aa215SJan Kara * committed, we could see stale data in block A. This function is a 26957f5aa215SJan Kara * helper to solve this problem. It starts writeout of the truncated 26967f5aa215SJan Kara * part in case it is in the committing transaction. 26977f5aa215SJan Kara * 26987f5aa215SJan Kara * Filesystem code must call this function when inode is journaled in 26997f5aa215SJan Kara * ordered mode before truncation happens and after the inode has been 27007f5aa215SJan Kara * placed on orphan list with the new inode size. The second condition 27017f5aa215SJan Kara * avoids the race that someone writes new data and we start 27027f5aa215SJan Kara * committing the transaction after this function has been called but 27037f5aa215SJan Kara * before a transaction for truncate is started (and furthermore it 27047f5aa215SJan Kara * allows us to optimize the case where the addition to orphan list 27057f5aa215SJan Kara * happens in the same transaction as write --- we don't have to write 27067f5aa215SJan Kara * any data in such case). 2707c851ed54SJan Kara */ 27087f5aa215SJan Kara int jbd2_journal_begin_ordered_truncate(journal_t *journal, 27097f5aa215SJan Kara struct jbd2_inode *jinode, 2710c851ed54SJan Kara loff_t new_size) 2711c851ed54SJan Kara { 27127f5aa215SJan Kara transaction_t *inode_trans, *commit_trans; 2713c851ed54SJan Kara int ret = 0; 2714c851ed54SJan Kara 27157f5aa215SJan Kara /* This is a quick check to avoid locking if not necessary */ 27167f5aa215SJan Kara if (!jinode->i_transaction) 2717c851ed54SJan Kara goto out; 27187f5aa215SJan Kara /* Locks are here just to force reading of recent values, it is 27197f5aa215SJan Kara * enough that the transaction was not committing before we started 27207f5aa215SJan Kara * a transaction adding the inode to orphan list */ 2721a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 2722c851ed54SJan Kara commit_trans = journal->j_committing_transaction; 2723a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 27247f5aa215SJan Kara spin_lock(&journal->j_list_lock); 27257f5aa215SJan Kara inode_trans = jinode->i_transaction; 27267f5aa215SJan Kara spin_unlock(&journal->j_list_lock); 27277f5aa215SJan Kara if (inode_trans == commit_trans) { 27287f5aa215SJan Kara ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping, 2729c851ed54SJan Kara new_size, LLONG_MAX); 2730c851ed54SJan Kara if (ret) 2731c851ed54SJan Kara jbd2_journal_abort(journal, ret); 2732c851ed54SJan Kara } 2733c851ed54SJan Kara out: 2734c851ed54SJan Kara return ret; 2735c851ed54SJan Kara } 2736