1f5166768STheodore Ts'o // SPDX-License-Identifier: GPL-2.0+ 2470decc6SDave Kleikamp /* 3f7f4bccbSMingming Cao * linux/fs/jbd2/journal.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 journal-writing code; part of the ext2fs 10470decc6SDave Kleikamp * journaling system. 11470decc6SDave Kleikamp * 12470decc6SDave Kleikamp * This file manages journals: areas of disk reserved for logging 13470decc6SDave Kleikamp * transactional updates. This includes the kernel journaling thread 14470decc6SDave Kleikamp * which is responsible for scheduling updates to the log. 15470decc6SDave Kleikamp * 16470decc6SDave Kleikamp * We do not actually manage the physical storage of the journal in this 17470decc6SDave Kleikamp * file: that is left to a per-journal policy function, which allows us 18470decc6SDave Kleikamp * to store the journal within a filesystem-specified area for ext2 19470decc6SDave Kleikamp * journaling (ext2 can use a reserved inode for storing the log). 20470decc6SDave Kleikamp */ 21470decc6SDave Kleikamp 22470decc6SDave Kleikamp #include <linux/module.h> 23470decc6SDave Kleikamp #include <linux/time.h> 24470decc6SDave Kleikamp #include <linux/fs.h> 25f7f4bccbSMingming Cao #include <linux/jbd2.h> 26470decc6SDave Kleikamp #include <linux/errno.h> 27470decc6SDave Kleikamp #include <linux/slab.h> 28470decc6SDave Kleikamp #include <linux/init.h> 29470decc6SDave Kleikamp #include <linux/mm.h> 307dfb7103SNigel Cunningham #include <linux/freezer.h> 31470decc6SDave Kleikamp #include <linux/pagemap.h> 32470decc6SDave Kleikamp #include <linux/kthread.h> 33470decc6SDave Kleikamp #include <linux/poison.h> 34470decc6SDave Kleikamp #include <linux/proc_fs.h> 358e85fb3fSJohann Lombardi #include <linux/seq_file.h> 36c225aa57SSimon Holm Thøgersen #include <linux/math64.h> 37879c5e6bSTheodore Ts'o #include <linux/hash.h> 38d2eecb03STheodore Ts'o #include <linux/log2.h> 39d2eecb03STheodore Ts'o #include <linux/vmalloc.h> 4047def826STheodore Ts'o #include <linux/backing-dev.h> 4139e3ac25SBrian King #include <linux/bitops.h> 42670be5a7STheodore Ts'o #include <linux/ratelimit.h> 43eb52da3fSMichal Hocko #include <linux/sched/mm.h> 44879c5e6bSTheodore Ts'o 45879c5e6bSTheodore Ts'o #define CREATE_TRACE_POINTS 46879c5e6bSTheodore Ts'o #include <trace/events/jbd2.h> 47470decc6SDave Kleikamp 487c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 49470decc6SDave Kleikamp #include <asm/page.h> 50470decc6SDave Kleikamp 51b6e96d00STheodore Ts'o #ifdef CONFIG_JBD2_DEBUG 5268af74e9SJan Kara static ushort jbd2_journal_enable_debug __read_mostly; 53b6e96d00STheodore Ts'o 54b6e96d00STheodore Ts'o module_param_named(jbd2_debug, jbd2_journal_enable_debug, ushort, 0644); 55b6e96d00STheodore Ts'o MODULE_PARM_DESC(jbd2_debug, "Debugging level for jbd2"); 56b6e96d00STheodore Ts'o #endif 57b6e96d00STheodore Ts'o 58f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_extend); 59f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_stop); 60f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_lock_updates); 61f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_unlock_updates); 62f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_get_write_access); 63f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_get_create_access); 64f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_get_undo_access); 65e06c8227SJoel Becker EXPORT_SYMBOL(jbd2_journal_set_triggers); 66f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_dirty_metadata); 67f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_forget); 68f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_flush); 69f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_revoke); 70470decc6SDave Kleikamp 71f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_init_dev); 72f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_init_inode); 73f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_check_used_features); 74f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_check_available_features); 75f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_set_features); 76f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_load); 77f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_destroy); 78f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_abort); 79f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_errno); 80f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_ack_err); 81f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_clear_err); 82f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_log_wait_commit); 83f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_start_commit); 84f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_force_commit_nested); 85f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_wipe); 86f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_blocks_per_page); 87ccd16945SMatthew Wilcox (Oracle) EXPORT_SYMBOL(jbd2_journal_invalidate_folio); 88f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers); 89f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_force_commit); 906ba0e7dcSRoss Zwisler EXPORT_SYMBOL(jbd2_journal_inode_ranged_write); 916ba0e7dcSRoss Zwisler EXPORT_SYMBOL(jbd2_journal_inode_ranged_wait); 92aa3c0c61SMauricio Faria de Oliveira EXPORT_SYMBOL(jbd2_journal_finish_inode_data_buffers); 93c851ed54SJan Kara EXPORT_SYMBOL(jbd2_journal_init_jbd_inode); 94c851ed54SJan Kara EXPORT_SYMBOL(jbd2_journal_release_jbd_inode); 95c851ed54SJan Kara EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate); 968aefcd55STheodore Ts'o EXPORT_SYMBOL(jbd2_inode_cache); 97470decc6SDave Kleikamp 98d2eecb03STheodore Ts'o static int jbd2_journal_create_slab(size_t slab_size); 99470decc6SDave Kleikamp 100169f1a2aSPaul Gortmaker #ifdef CONFIG_JBD2_DEBUG 101169f1a2aSPaul Gortmaker void __jbd2_debug(int level, const char *file, const char *func, 102169f1a2aSPaul Gortmaker unsigned int line, const char *fmt, ...) 103169f1a2aSPaul Gortmaker { 104169f1a2aSPaul Gortmaker struct va_format vaf; 105169f1a2aSPaul Gortmaker va_list args; 106169f1a2aSPaul Gortmaker 107169f1a2aSPaul Gortmaker if (level > jbd2_journal_enable_debug) 108169f1a2aSPaul Gortmaker return; 109169f1a2aSPaul Gortmaker va_start(args, fmt); 110169f1a2aSPaul Gortmaker vaf.fmt = fmt; 111169f1a2aSPaul Gortmaker vaf.va = &args; 1129196f571SWang Shilong printk(KERN_DEBUG "%s: (%s, %u): %pV", file, func, line, &vaf); 113169f1a2aSPaul Gortmaker va_end(args); 114169f1a2aSPaul Gortmaker } 115169f1a2aSPaul Gortmaker #endif 116169f1a2aSPaul Gortmaker 11725ed6e8aSDarrick J. Wong /* Checksumming functions */ 1187747e6d0SRashika Kheria static int jbd2_verify_csum_type(journal_t *j, journal_superblock_t *sb) 11925ed6e8aSDarrick J. Wong { 1208595798cSDarrick J. Wong if (!jbd2_journal_has_csum_v2or3_feature(j)) 12125ed6e8aSDarrick J. Wong return 1; 12225ed6e8aSDarrick J. Wong 12325ed6e8aSDarrick J. Wong return sb->s_checksum_type == JBD2_CRC32C_CHKSUM; 12425ed6e8aSDarrick J. Wong } 12525ed6e8aSDarrick J. Wong 12618a6ea1eSDarrick J. Wong static __be32 jbd2_superblock_csum(journal_t *j, journal_superblock_t *sb) 1274fd5ea43SDarrick J. Wong { 12818a6ea1eSDarrick J. Wong __u32 csum; 12918a6ea1eSDarrick J. Wong __be32 old_csum; 1304fd5ea43SDarrick J. Wong 1314fd5ea43SDarrick J. Wong old_csum = sb->s_checksum; 1324fd5ea43SDarrick J. Wong sb->s_checksum = 0; 1334fd5ea43SDarrick J. Wong csum = jbd2_chksum(j, ~0, (char *)sb, sizeof(journal_superblock_t)); 1344fd5ea43SDarrick J. Wong sb->s_checksum = old_csum; 1354fd5ea43SDarrick J. Wong 1364fd5ea43SDarrick J. Wong return cpu_to_be32(csum); 1374fd5ea43SDarrick J. Wong } 1384fd5ea43SDarrick J. Wong 139470decc6SDave Kleikamp /* 140470decc6SDave Kleikamp * Helper function used to manage commit timeouts 141470decc6SDave Kleikamp */ 142470decc6SDave Kleikamp 143e3c95788SKees Cook static void commit_timeout(struct timer_list *t) 144470decc6SDave Kleikamp { 145e3c95788SKees Cook journal_t *journal = from_timer(journal, t, j_commit_timer); 146470decc6SDave Kleikamp 147e3c95788SKees Cook wake_up_process(journal->j_task); 148470decc6SDave Kleikamp } 149470decc6SDave Kleikamp 150470decc6SDave Kleikamp /* 151f7f4bccbSMingming Cao * kjournald2: The main thread function used to manage a logging device 152470decc6SDave Kleikamp * journal. 153470decc6SDave Kleikamp * 154470decc6SDave Kleikamp * This kernel thread is responsible for two things: 155470decc6SDave Kleikamp * 156470decc6SDave Kleikamp * 1) COMMIT: Every so often we need to commit the current state of the 157470decc6SDave Kleikamp * filesystem to disk. The journal thread is responsible for writing 158ff780b91SHarshad Shirwadkar * all of the metadata buffers to disk. If a fast commit is ongoing 159ff780b91SHarshad Shirwadkar * journal thread waits until it's done and then continues from 160ff780b91SHarshad Shirwadkar * there on. 161470decc6SDave Kleikamp * 162470decc6SDave Kleikamp * 2) CHECKPOINT: We cannot reuse a used section of the log file until all 163470decc6SDave Kleikamp * of the data in that part of the log has been rewritten elsewhere on 164470decc6SDave Kleikamp * the disk. Flushing these old buffers to reclaim space in the log is 165470decc6SDave Kleikamp * known as checkpointing, and this thread is responsible for that job. 166470decc6SDave Kleikamp */ 167470decc6SDave Kleikamp 168f7f4bccbSMingming Cao static int kjournald2(void *arg) 169470decc6SDave Kleikamp { 170470decc6SDave Kleikamp journal_t *journal = arg; 171470decc6SDave Kleikamp transaction_t *transaction; 172470decc6SDave Kleikamp 173470decc6SDave Kleikamp /* 174470decc6SDave Kleikamp * Set up an interval timer which can be used to trigger a commit wakeup 175470decc6SDave Kleikamp * after the commit interval expires 176470decc6SDave Kleikamp */ 177e3c95788SKees Cook timer_setup(&journal->j_commit_timer, commit_timeout, 0); 178470decc6SDave Kleikamp 17935c80422SNigel Cunningham set_freezable(); 18035c80422SNigel Cunningham 181470decc6SDave Kleikamp /* Record that the journal thread is running */ 182470decc6SDave Kleikamp journal->j_task = current; 183470decc6SDave Kleikamp wake_up(&journal->j_wait_done_commit); 184470decc6SDave Kleikamp 185470decc6SDave Kleikamp /* 186eb52da3fSMichal Hocko * Make sure that no allocations from this kernel thread will ever 187eb52da3fSMichal Hocko * recurse to the fs layer because we are responsible for the 188eb52da3fSMichal Hocko * transaction commit and any fs involvement might get stuck waiting for 189eb52da3fSMichal Hocko * the trasn. commit. 190eb52da3fSMichal Hocko */ 191eb52da3fSMichal Hocko memalloc_nofs_save(); 192eb52da3fSMichal Hocko 193eb52da3fSMichal Hocko /* 194470decc6SDave Kleikamp * And now, wait forever for commit wakeup events. 195470decc6SDave Kleikamp */ 196a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 197470decc6SDave Kleikamp 198470decc6SDave Kleikamp loop: 199f7f4bccbSMingming Cao if (journal->j_flags & JBD2_UNMOUNT) 200470decc6SDave Kleikamp goto end_loop; 201470decc6SDave Kleikamp 202cb3b3bf2SJan Kara jbd2_debug(1, "commit_sequence=%u, commit_request=%u\n", 203470decc6SDave Kleikamp journal->j_commit_sequence, journal->j_commit_request); 204470decc6SDave Kleikamp 205470decc6SDave Kleikamp if (journal->j_commit_sequence != journal->j_commit_request) { 206cb3b3bf2SJan Kara jbd2_debug(1, "OK, requests differ\n"); 207a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 208470decc6SDave Kleikamp del_timer_sync(&journal->j_commit_timer); 209f7f4bccbSMingming Cao jbd2_journal_commit_transaction(journal); 210a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 211470decc6SDave Kleikamp goto loop; 212470decc6SDave Kleikamp } 213470decc6SDave Kleikamp 214470decc6SDave Kleikamp wake_up(&journal->j_wait_done_commit); 215470decc6SDave Kleikamp if (freezing(current)) { 216470decc6SDave Kleikamp /* 217470decc6SDave Kleikamp * The simpler the better. Flushing journal isn't a 218470decc6SDave Kleikamp * good idea, because that depends on threads that may 219470decc6SDave Kleikamp * be already stopped. 220470decc6SDave Kleikamp */ 221cb3b3bf2SJan Kara jbd2_debug(1, "Now suspending kjournald2\n"); 222a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 223a0acae0eSTejun Heo try_to_freeze(); 224a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 225470decc6SDave Kleikamp } else { 226470decc6SDave Kleikamp /* 227470decc6SDave Kleikamp * We assume on resume that commits are already there, 228470decc6SDave Kleikamp * so we don't sleep 229470decc6SDave Kleikamp */ 230470decc6SDave Kleikamp DEFINE_WAIT(wait); 231470decc6SDave Kleikamp int should_sleep = 1; 232470decc6SDave Kleikamp 233470decc6SDave Kleikamp prepare_to_wait(&journal->j_wait_commit, &wait, 234470decc6SDave Kleikamp TASK_INTERRUPTIBLE); 235470decc6SDave Kleikamp if (journal->j_commit_sequence != journal->j_commit_request) 236470decc6SDave Kleikamp should_sleep = 0; 237470decc6SDave Kleikamp transaction = journal->j_running_transaction; 238470decc6SDave Kleikamp if (transaction && time_after_eq(jiffies, 239470decc6SDave Kleikamp transaction->t_expires)) 240470decc6SDave Kleikamp should_sleep = 0; 241f7f4bccbSMingming Cao if (journal->j_flags & JBD2_UNMOUNT) 242470decc6SDave Kleikamp should_sleep = 0; 243470decc6SDave Kleikamp if (should_sleep) { 244a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 245470decc6SDave Kleikamp schedule(); 246a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 247470decc6SDave Kleikamp } 248470decc6SDave Kleikamp finish_wait(&journal->j_wait_commit, &wait); 249470decc6SDave Kleikamp } 250470decc6SDave Kleikamp 251cb3b3bf2SJan Kara jbd2_debug(1, "kjournald2 wakes\n"); 252470decc6SDave Kleikamp 253470decc6SDave Kleikamp /* 254470decc6SDave Kleikamp * Were we woken up by a commit wakeup event? 255470decc6SDave Kleikamp */ 256470decc6SDave Kleikamp transaction = journal->j_running_transaction; 257470decc6SDave Kleikamp if (transaction && time_after_eq(jiffies, transaction->t_expires)) { 258470decc6SDave Kleikamp journal->j_commit_request = transaction->t_tid; 259cb3b3bf2SJan Kara jbd2_debug(1, "woke because of timeout\n"); 260470decc6SDave Kleikamp } 261470decc6SDave Kleikamp goto loop; 262470decc6SDave Kleikamp 263470decc6SDave Kleikamp end_loop: 264470decc6SDave Kleikamp del_timer_sync(&journal->j_commit_timer); 265470decc6SDave Kleikamp journal->j_task = NULL; 266470decc6SDave Kleikamp wake_up(&journal->j_wait_done_commit); 267cb3b3bf2SJan Kara jbd2_debug(1, "Journal thread exiting.\n"); 268dbfcef6bSSahitya Tummala write_unlock(&journal->j_state_lock); 269470decc6SDave Kleikamp return 0; 270470decc6SDave Kleikamp } 271470decc6SDave Kleikamp 27297f06784SPavel Emelianov static int jbd2_journal_start_thread(journal_t *journal) 273470decc6SDave Kleikamp { 27497f06784SPavel Emelianov struct task_struct *t; 27597f06784SPavel Emelianov 27690576c0bSTheodore Ts'o t = kthread_run(kjournald2, journal, "jbd2/%s", 27790576c0bSTheodore Ts'o journal->j_devname); 27897f06784SPavel Emelianov if (IS_ERR(t)) 27997f06784SPavel Emelianov return PTR_ERR(t); 28097f06784SPavel Emelianov 2811076d17aSAl Viro wait_event(journal->j_wait_done_commit, journal->j_task != NULL); 28297f06784SPavel Emelianov return 0; 283470decc6SDave Kleikamp } 284470decc6SDave Kleikamp 285470decc6SDave Kleikamp static void journal_kill_thread(journal_t *journal) 286470decc6SDave Kleikamp { 287a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 288f7f4bccbSMingming Cao journal->j_flags |= JBD2_UNMOUNT; 289470decc6SDave Kleikamp 290470decc6SDave Kleikamp while (journal->j_task) { 291a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 2923469a32aSTheodore Ts'o wake_up(&journal->j_wait_commit); 2931076d17aSAl Viro wait_event(journal->j_wait_done_commit, journal->j_task == NULL); 294a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 295470decc6SDave Kleikamp } 296a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 297470decc6SDave Kleikamp } 298470decc6SDave Kleikamp 299470decc6SDave Kleikamp /* 300f7f4bccbSMingming Cao * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal. 301470decc6SDave Kleikamp * 302470decc6SDave Kleikamp * Writes a metadata buffer to a given disk block. The actual IO is not 303470decc6SDave Kleikamp * performed but a new buffer_head is constructed which labels the data 304470decc6SDave Kleikamp * to be written with the correct destination disk block. 305470decc6SDave Kleikamp * 306470decc6SDave Kleikamp * Any magic-number escaping which needs to be done will cause a 307470decc6SDave Kleikamp * copy-out here. If the buffer happens to start with the 308f7f4bccbSMingming Cao * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the 309470decc6SDave Kleikamp * magic number is only written to the log for descripter blocks. In 310470decc6SDave Kleikamp * this case, we copy the data and replace the first word with 0, and we 311470decc6SDave Kleikamp * return a result code which indicates that this buffer needs to be 312470decc6SDave Kleikamp * marked as an escaped buffer in the corresponding log descriptor 313470decc6SDave Kleikamp * block. The missing word can then be restored when the block is read 314470decc6SDave Kleikamp * during recovery. 315470decc6SDave Kleikamp * 316470decc6SDave Kleikamp * If the source buffer has already been modified by a new transaction 317470decc6SDave Kleikamp * since we took the last commit snapshot, we use the frozen copy of 318470decc6SDave Kleikamp * that data for IO. If we end up using the existing buffer_head's data 319f5113effSJan Kara * for the write, then we have to make sure nobody modifies it while the 320f5113effSJan Kara * IO is in progress. do_get_write_access() handles this. 321470decc6SDave Kleikamp * 322f5113effSJan Kara * The function returns a pointer to the buffer_head to be used for IO. 323470decc6SDave Kleikamp * 324470decc6SDave Kleikamp * 325470decc6SDave Kleikamp * Return value: 326470decc6SDave Kleikamp * <0: Error 327470decc6SDave Kleikamp * >=0: Finished OK 328470decc6SDave Kleikamp * 329470decc6SDave Kleikamp * On success: 330470decc6SDave Kleikamp * Bit 0 set == escape performed on the data 331470decc6SDave Kleikamp * Bit 1 set == buffer copy-out performed (kfree the data after IO) 332470decc6SDave Kleikamp */ 333470decc6SDave Kleikamp 334f7f4bccbSMingming Cao int jbd2_journal_write_metadata_buffer(transaction_t *transaction, 335470decc6SDave Kleikamp struct journal_head *jh_in, 336f5113effSJan Kara struct buffer_head **bh_out, 337f5113effSJan Kara sector_t blocknr) 338470decc6SDave Kleikamp { 339470decc6SDave Kleikamp int need_copy_out = 0; 340470decc6SDave Kleikamp int done_copy_out = 0; 341470decc6SDave Kleikamp int do_escape = 0; 342470decc6SDave Kleikamp char *mapped_data; 343470decc6SDave Kleikamp struct buffer_head *new_bh; 344470decc6SDave Kleikamp struct page *new_page; 345470decc6SDave Kleikamp unsigned int new_offset; 346470decc6SDave Kleikamp struct buffer_head *bh_in = jh2bh(jh_in); 34796577c43Sdingdinghua journal_t *journal = transaction->t_journal; 348470decc6SDave Kleikamp 349470decc6SDave Kleikamp /* 350470decc6SDave Kleikamp * The buffer really shouldn't be locked: only the current committing 351470decc6SDave Kleikamp * transaction is allowed to write it, so nobody else is allowed 352470decc6SDave Kleikamp * to do any IO. 353470decc6SDave Kleikamp * 354470decc6SDave Kleikamp * akpm: except if we're journalling data, and write() output is 355470decc6SDave Kleikamp * also part of a shared mapping, and another thread has 356470decc6SDave Kleikamp * decided to launch a writepage() against this buffer. 357470decc6SDave Kleikamp */ 358470decc6SDave Kleikamp J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in)); 359470decc6SDave Kleikamp 3606ccaf3e2SMichal Hocko new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL); 36147def826STheodore Ts'o 36296577c43Sdingdinghua /* keep subsequent assertions sane */ 36396577c43Sdingdinghua atomic_set(&new_bh->b_count, 1); 364470decc6SDave Kleikamp 36546417064SThomas Gleixner spin_lock(&jh_in->b_state_lock); 366f5113effSJan Kara repeat: 367470decc6SDave Kleikamp /* 368470decc6SDave Kleikamp * If a new transaction has already done a buffer copy-out, then 369470decc6SDave Kleikamp * we use that version of the data for the commit. 370470decc6SDave Kleikamp */ 371470decc6SDave Kleikamp if (jh_in->b_frozen_data) { 372470decc6SDave Kleikamp done_copy_out = 1; 373470decc6SDave Kleikamp new_page = virt_to_page(jh_in->b_frozen_data); 374470decc6SDave Kleikamp new_offset = offset_in_page(jh_in->b_frozen_data); 375470decc6SDave Kleikamp } else { 376470decc6SDave Kleikamp new_page = jh2bh(jh_in)->b_page; 377470decc6SDave Kleikamp new_offset = offset_in_page(jh2bh(jh_in)->b_data); 378470decc6SDave Kleikamp } 379470decc6SDave Kleikamp 380303a8f2aSCong Wang mapped_data = kmap_atomic(new_page); 381470decc6SDave Kleikamp /* 38213ceef09SJan Kara * Fire data frozen trigger if data already wasn't frozen. Do this 38313ceef09SJan Kara * before checking for escaping, as the trigger may modify the magic 38413ceef09SJan Kara * offset. If a copy-out happens afterwards, it will have the correct 38513ceef09SJan Kara * data in the buffer. 386e06c8227SJoel Becker */ 38713ceef09SJan Kara if (!done_copy_out) 38813ceef09SJan Kara jbd2_buffer_frozen_trigger(jh_in, mapped_data + new_offset, 38913ceef09SJan Kara jh_in->b_triggers); 390e06c8227SJoel Becker 391e06c8227SJoel Becker /* 392470decc6SDave Kleikamp * Check for escaping 393470decc6SDave Kleikamp */ 394470decc6SDave Kleikamp if (*((__be32 *)(mapped_data + new_offset)) == 395f7f4bccbSMingming Cao cpu_to_be32(JBD2_MAGIC_NUMBER)) { 396470decc6SDave Kleikamp need_copy_out = 1; 397470decc6SDave Kleikamp do_escape = 1; 398470decc6SDave Kleikamp } 399303a8f2aSCong Wang kunmap_atomic(mapped_data); 400470decc6SDave Kleikamp 401470decc6SDave Kleikamp /* 402470decc6SDave Kleikamp * Do we need to do a data copy? 403470decc6SDave Kleikamp */ 404470decc6SDave Kleikamp if (need_copy_out && !done_copy_out) { 405470decc6SDave Kleikamp char *tmp; 406470decc6SDave Kleikamp 40746417064SThomas Gleixner spin_unlock(&jh_in->b_state_lock); 408af1e76d6SMingming Cao tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS); 409e6ec116bSTheodore Ts'o if (!tmp) { 410f5113effSJan Kara brelse(new_bh); 411e6ec116bSTheodore Ts'o return -ENOMEM; 412e6ec116bSTheodore Ts'o } 41346417064SThomas Gleixner spin_lock(&jh_in->b_state_lock); 414470decc6SDave Kleikamp if (jh_in->b_frozen_data) { 415af1e76d6SMingming Cao jbd2_free(tmp, bh_in->b_size); 416470decc6SDave Kleikamp goto repeat; 417470decc6SDave Kleikamp } 418470decc6SDave Kleikamp 419470decc6SDave Kleikamp jh_in->b_frozen_data = tmp; 420303a8f2aSCong Wang mapped_data = kmap_atomic(new_page); 421f5113effSJan Kara memcpy(tmp, mapped_data + new_offset, bh_in->b_size); 422303a8f2aSCong Wang kunmap_atomic(mapped_data); 423470decc6SDave Kleikamp 424470decc6SDave Kleikamp new_page = virt_to_page(tmp); 425470decc6SDave Kleikamp new_offset = offset_in_page(tmp); 426470decc6SDave Kleikamp done_copy_out = 1; 427e06c8227SJoel Becker 428e06c8227SJoel Becker /* 429e06c8227SJoel Becker * This isn't strictly necessary, as we're using frozen 430e06c8227SJoel Becker * data for the escaping, but it keeps consistency with 431e06c8227SJoel Becker * b_frozen_data usage. 432e06c8227SJoel Becker */ 433e06c8227SJoel Becker jh_in->b_frozen_triggers = jh_in->b_triggers; 434470decc6SDave Kleikamp } 435470decc6SDave Kleikamp 436470decc6SDave Kleikamp /* 437470decc6SDave Kleikamp * Did we need to do an escaping? Now we've done all the 438470decc6SDave Kleikamp * copying, we can finally do so. 439470decc6SDave Kleikamp */ 440470decc6SDave Kleikamp if (do_escape) { 441303a8f2aSCong Wang mapped_data = kmap_atomic(new_page); 442470decc6SDave Kleikamp *((unsigned int *)(mapped_data + new_offset)) = 0; 443303a8f2aSCong Wang kunmap_atomic(mapped_data); 444470decc6SDave Kleikamp } 445470decc6SDave Kleikamp 446470decc6SDave Kleikamp set_bh_page(new_bh, new_page, new_offset); 447f5113effSJan Kara new_bh->b_size = bh_in->b_size; 448f5113effSJan Kara new_bh->b_bdev = journal->j_dev; 449470decc6SDave Kleikamp new_bh->b_blocknr = blocknr; 450b34090e5SJan Kara new_bh->b_private = bh_in; 451470decc6SDave Kleikamp set_buffer_mapped(new_bh); 452470decc6SDave Kleikamp set_buffer_dirty(new_bh); 453470decc6SDave Kleikamp 454f5113effSJan Kara *bh_out = new_bh; 455470decc6SDave Kleikamp 456470decc6SDave Kleikamp /* 457470decc6SDave Kleikamp * The to-be-written buffer needs to get moved to the io queue, 458470decc6SDave Kleikamp * and the original buffer whose contents we are shadowing or 459470decc6SDave Kleikamp * copying is moved to the transaction's shadow queue. 460470decc6SDave Kleikamp */ 461470decc6SDave Kleikamp JBUFFER_TRACE(jh_in, "file as BJ_Shadow"); 46296577c43Sdingdinghua spin_lock(&journal->j_list_lock); 46396577c43Sdingdinghua __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow); 46496577c43Sdingdinghua spin_unlock(&journal->j_list_lock); 465b34090e5SJan Kara set_buffer_shadow(bh_in); 46646417064SThomas Gleixner spin_unlock(&jh_in->b_state_lock); 46796577c43Sdingdinghua 468470decc6SDave Kleikamp return do_escape | (done_copy_out << 1); 469470decc6SDave Kleikamp } 470470decc6SDave Kleikamp 471470decc6SDave Kleikamp /* 472470decc6SDave Kleikamp * Allocation code for the journal file. Manage the space left in the 473470decc6SDave Kleikamp * journal, so that we can begin checkpointing when appropriate. 474470decc6SDave Kleikamp */ 475470decc6SDave Kleikamp 476470decc6SDave Kleikamp /* 477e4471831STheodore Ts'o * Called with j_state_lock locked for writing. 478e4471831STheodore Ts'o * Returns true if a transaction commit was started. 479470decc6SDave Kleikamp */ 480d1324958SJan Kara static int __jbd2_log_start_commit(journal_t *journal, tid_t target) 481470decc6SDave Kleikamp { 482e7b04ac0SEric Sandeen /* Return if the txn has already requested to be committed */ 483e7b04ac0SEric Sandeen if (journal->j_commit_request == target) 484e7b04ac0SEric Sandeen return 0; 485e7b04ac0SEric Sandeen 486470decc6SDave Kleikamp /* 487deeeaf13STheodore Ts'o * The only transaction we can possibly wait upon is the 488deeeaf13STheodore Ts'o * currently running transaction (if it exists). Otherwise, 489deeeaf13STheodore Ts'o * the target tid must be an old one. 490470decc6SDave Kleikamp */ 491deeeaf13STheodore Ts'o if (journal->j_running_transaction && 492deeeaf13STheodore Ts'o journal->j_running_transaction->t_tid == target) { 493470decc6SDave Kleikamp /* 494bcf3d0bcSAndrea Gelmini * We want a new commit: OK, mark the request and wakeup the 495470decc6SDave Kleikamp * commit thread. We do _not_ do the commit ourselves. 496470decc6SDave Kleikamp */ 497470decc6SDave Kleikamp 498470decc6SDave Kleikamp journal->j_commit_request = target; 499cb3b3bf2SJan Kara jbd2_debug(1, "JBD2: requesting commit %u/%u\n", 500470decc6SDave Kleikamp journal->j_commit_request, 501470decc6SDave Kleikamp journal->j_commit_sequence); 5029fff24aaSTheodore Ts'o journal->j_running_transaction->t_requested = jiffies; 503470decc6SDave Kleikamp wake_up(&journal->j_wait_commit); 504470decc6SDave Kleikamp return 1; 505deeeaf13STheodore Ts'o } else if (!tid_geq(journal->j_commit_request, target)) 506deeeaf13STheodore Ts'o /* This should never happen, but if it does, preserve 507deeeaf13STheodore Ts'o the evidence before kjournald goes into a loop and 508deeeaf13STheodore Ts'o increments j_commit_sequence beyond all recognition. */ 509f2a44523SEryu Guan WARN_ONCE(1, "JBD2: bad log_start_commit: %u %u %u %u\n", 5101be2add6STheodore Ts'o journal->j_commit_request, 5111be2add6STheodore Ts'o journal->j_commit_sequence, 512deeeaf13STheodore Ts'o target, journal->j_running_transaction ? 513deeeaf13STheodore Ts'o journal->j_running_transaction->t_tid : 0); 514470decc6SDave Kleikamp return 0; 515470decc6SDave Kleikamp } 516470decc6SDave Kleikamp 517f7f4bccbSMingming Cao int jbd2_log_start_commit(journal_t *journal, tid_t tid) 518470decc6SDave Kleikamp { 519470decc6SDave Kleikamp int ret; 520470decc6SDave Kleikamp 521a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 522f7f4bccbSMingming Cao ret = __jbd2_log_start_commit(journal, tid); 523a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 524470decc6SDave Kleikamp return ret; 525470decc6SDave Kleikamp } 526470decc6SDave Kleikamp 527470decc6SDave Kleikamp /* 5289ff86446SDmitry Monakhov * Force and wait any uncommitted transactions. We can only force the running 5299ff86446SDmitry Monakhov * transaction if we don't have an active handle, otherwise, we will deadlock. 5309ff86446SDmitry Monakhov * Returns: <0 in case of error, 5319ff86446SDmitry Monakhov * 0 if nothing to commit, 5329ff86446SDmitry Monakhov * 1 if transaction was successfully committed. 533470decc6SDave Kleikamp */ 5349ff86446SDmitry Monakhov static int __jbd2_journal_force_commit(journal_t *journal) 535470decc6SDave Kleikamp { 536470decc6SDave Kleikamp transaction_t *transaction = NULL; 537470decc6SDave Kleikamp tid_t tid; 5389ff86446SDmitry Monakhov int need_to_start = 0, ret = 0; 539470decc6SDave Kleikamp 540a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 541470decc6SDave Kleikamp if (journal->j_running_transaction && !current->journal_info) { 542470decc6SDave Kleikamp transaction = journal->j_running_transaction; 543e4471831STheodore Ts'o if (!tid_geq(journal->j_commit_request, transaction->t_tid)) 544e4471831STheodore Ts'o need_to_start = 1; 545470decc6SDave Kleikamp } else if (journal->j_committing_transaction) 546470decc6SDave Kleikamp transaction = journal->j_committing_transaction; 547470decc6SDave Kleikamp 548470decc6SDave Kleikamp if (!transaction) { 5499ff86446SDmitry Monakhov /* Nothing to commit */ 550a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 5519ff86446SDmitry Monakhov return 0; 552470decc6SDave Kleikamp } 553470decc6SDave Kleikamp tid = transaction->t_tid; 554a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 555e4471831STheodore Ts'o if (need_to_start) 556e4471831STheodore Ts'o jbd2_log_start_commit(journal, tid); 5579ff86446SDmitry Monakhov ret = jbd2_log_wait_commit(journal, tid); 5589ff86446SDmitry Monakhov if (!ret) 5599ff86446SDmitry Monakhov ret = 1; 5609ff86446SDmitry Monakhov 5619ff86446SDmitry Monakhov return ret; 5629ff86446SDmitry Monakhov } 5639ff86446SDmitry Monakhov 5649ff86446SDmitry Monakhov /** 5652bf31d94SMauro Carvalho Chehab * jbd2_journal_force_commit_nested - Force and wait upon a commit if the 5662bf31d94SMauro Carvalho Chehab * calling process is not within transaction. 5679ff86446SDmitry Monakhov * 5689ff86446SDmitry Monakhov * @journal: journal to force 5699ff86446SDmitry Monakhov * Returns true if progress was made. 5702bf31d94SMauro Carvalho Chehab * 5712bf31d94SMauro Carvalho Chehab * This is used for forcing out undo-protected data which contains 5722bf31d94SMauro Carvalho Chehab * bitmaps, when the fs is running out of space. 5739ff86446SDmitry Monakhov */ 5749ff86446SDmitry Monakhov int jbd2_journal_force_commit_nested(journal_t *journal) 5759ff86446SDmitry Monakhov { 5769ff86446SDmitry Monakhov int ret; 5779ff86446SDmitry Monakhov 5789ff86446SDmitry Monakhov ret = __jbd2_journal_force_commit(journal); 5799ff86446SDmitry Monakhov return ret > 0; 5809ff86446SDmitry Monakhov } 5819ff86446SDmitry Monakhov 5829ff86446SDmitry Monakhov /** 5832bf31d94SMauro Carvalho Chehab * jbd2_journal_force_commit() - force any uncommitted transactions 5849ff86446SDmitry Monakhov * @journal: journal to force 5859ff86446SDmitry Monakhov * 5869ff86446SDmitry Monakhov * Caller want unconditional commit. We can only force the running transaction 5879ff86446SDmitry Monakhov * if we don't have an active handle, otherwise, we will deadlock. 5889ff86446SDmitry Monakhov */ 5899ff86446SDmitry Monakhov int jbd2_journal_force_commit(journal_t *journal) 5909ff86446SDmitry Monakhov { 5919ff86446SDmitry Monakhov int ret; 5929ff86446SDmitry Monakhov 5939ff86446SDmitry Monakhov J_ASSERT(!current->journal_info); 5949ff86446SDmitry Monakhov ret = __jbd2_journal_force_commit(journal); 5959ff86446SDmitry Monakhov if (ret > 0) 5969ff86446SDmitry Monakhov ret = 0; 5979ff86446SDmitry Monakhov return ret; 598470decc6SDave Kleikamp } 599470decc6SDave Kleikamp 600470decc6SDave Kleikamp /* 601470decc6SDave Kleikamp * Start a commit of the current running transaction (if any). Returns true 602c88ccea3SJan Kara * if a transaction is going to be committed (or is currently already 603c88ccea3SJan Kara * committing), and fills its tid in at *ptid 604470decc6SDave Kleikamp */ 605f7f4bccbSMingming Cao int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid) 606470decc6SDave Kleikamp { 607470decc6SDave Kleikamp int ret = 0; 608470decc6SDave Kleikamp 609a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 610470decc6SDave Kleikamp if (journal->j_running_transaction) { 611470decc6SDave Kleikamp tid_t tid = journal->j_running_transaction->t_tid; 612470decc6SDave Kleikamp 613c88ccea3SJan Kara __jbd2_log_start_commit(journal, tid); 614c88ccea3SJan Kara /* There's a running transaction and we've just made sure 615c88ccea3SJan Kara * it's commit has been scheduled. */ 616c88ccea3SJan Kara if (ptid) 617470decc6SDave Kleikamp *ptid = tid; 618c88ccea3SJan Kara ret = 1; 619c88ccea3SJan Kara } else if (journal->j_committing_transaction) { 620470decc6SDave Kleikamp /* 62112810ad7SArtem Bityutskiy * If commit has been started, then we have to wait for 62212810ad7SArtem Bityutskiy * completion of that transaction. 623470decc6SDave Kleikamp */ 624c88ccea3SJan Kara if (ptid) 625470decc6SDave Kleikamp *ptid = journal->j_committing_transaction->t_tid; 626470decc6SDave Kleikamp ret = 1; 627470decc6SDave Kleikamp } 628a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 629470decc6SDave Kleikamp return ret; 630470decc6SDave Kleikamp } 631470decc6SDave Kleikamp 632470decc6SDave Kleikamp /* 633bbd2be36SJan Kara * Return 1 if a given transaction has not yet sent barrier request 634bbd2be36SJan Kara * connected with a transaction commit. If 0 is returned, transaction 635bbd2be36SJan Kara * may or may not have sent the barrier. Used to avoid sending barrier 636bbd2be36SJan Kara * twice in common cases. 637bbd2be36SJan Kara */ 638bbd2be36SJan Kara int jbd2_trans_will_send_data_barrier(journal_t *journal, tid_t tid) 639bbd2be36SJan Kara { 640bbd2be36SJan Kara int ret = 0; 641bbd2be36SJan Kara transaction_t *commit_trans; 642bbd2be36SJan Kara 643bbd2be36SJan Kara if (!(journal->j_flags & JBD2_BARRIER)) 644bbd2be36SJan Kara return 0; 645bbd2be36SJan Kara read_lock(&journal->j_state_lock); 646bbd2be36SJan Kara /* Transaction already committed? */ 647bbd2be36SJan Kara if (tid_geq(journal->j_commit_sequence, tid)) 648bbd2be36SJan Kara goto out; 649bbd2be36SJan Kara commit_trans = journal->j_committing_transaction; 650bbd2be36SJan Kara if (!commit_trans || commit_trans->t_tid != tid) { 651bbd2be36SJan Kara ret = 1; 652bbd2be36SJan Kara goto out; 653bbd2be36SJan Kara } 654bbd2be36SJan Kara /* 655bbd2be36SJan Kara * Transaction is being committed and we already proceeded to 656bbd2be36SJan Kara * submitting a flush to fs partition? 657bbd2be36SJan Kara */ 658bbd2be36SJan Kara if (journal->j_fs_dev != journal->j_dev) { 659bbd2be36SJan Kara if (!commit_trans->t_need_data_flush || 660bbd2be36SJan Kara commit_trans->t_state >= T_COMMIT_DFLUSH) 661bbd2be36SJan Kara goto out; 662bbd2be36SJan Kara } else { 663bbd2be36SJan Kara if (commit_trans->t_state >= T_COMMIT_JFLUSH) 664bbd2be36SJan Kara goto out; 665bbd2be36SJan Kara } 666bbd2be36SJan Kara ret = 1; 667bbd2be36SJan Kara out: 668bbd2be36SJan Kara read_unlock(&journal->j_state_lock); 669bbd2be36SJan Kara return ret; 670bbd2be36SJan Kara } 671bbd2be36SJan Kara EXPORT_SYMBOL(jbd2_trans_will_send_data_barrier); 672bbd2be36SJan Kara 673bbd2be36SJan Kara /* 674470decc6SDave Kleikamp * Wait for a specified commit to complete. 675470decc6SDave Kleikamp * The caller may not hold the journal lock. 676470decc6SDave Kleikamp */ 677f7f4bccbSMingming Cao int jbd2_log_wait_commit(journal_t *journal, tid_t tid) 678470decc6SDave Kleikamp { 679470decc6SDave Kleikamp int err = 0; 680470decc6SDave Kleikamp 681c52c47e4SJan Kara read_lock(&journal->j_state_lock); 682c52c47e4SJan Kara #ifdef CONFIG_PROVE_LOCKING 683c52c47e4SJan Kara /* 684c52c47e4SJan Kara * Some callers make sure transaction is already committing and in that 685c52c47e4SJan Kara * case we cannot block on open handles anymore. So don't warn in that 686c52c47e4SJan Kara * case. 687c52c47e4SJan Kara */ 688c52c47e4SJan Kara if (tid_gt(tid, journal->j_commit_sequence) && 689c52c47e4SJan Kara (!journal->j_committing_transaction || 690c52c47e4SJan Kara journal->j_committing_transaction->t_tid != tid)) { 691c52c47e4SJan Kara read_unlock(&journal->j_state_lock); 6921eaa566dSJan Kara jbd2_might_wait_for_commit(journal); 693a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 694c52c47e4SJan Kara } 695c52c47e4SJan Kara #endif 696e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 697470decc6SDave Kleikamp if (!tid_geq(journal->j_commit_request, tid)) { 69875685071SJan Kara printk(KERN_ERR 6997821ce41SGaowei Pu "%s: error: j_commit_request=%u, tid=%u\n", 700329d291fSHarvey Harrison __func__, journal->j_commit_request, tid); 701470decc6SDave Kleikamp } 702470decc6SDave Kleikamp #endif 703470decc6SDave Kleikamp while (tid_gt(tid, journal->j_commit_sequence)) { 704cb3b3bf2SJan Kara jbd2_debug(1, "JBD2: want %u, j_commit_sequence=%u\n", 705470decc6SDave Kleikamp tid, journal->j_commit_sequence); 706a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 7073469a32aSTheodore Ts'o wake_up(&journal->j_wait_commit); 708470decc6SDave Kleikamp wait_event(journal->j_wait_done_commit, 709470decc6SDave Kleikamp !tid_gt(tid, journal->j_commit_sequence)); 710a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 711470decc6SDave Kleikamp } 712a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 713470decc6SDave Kleikamp 71475685071SJan Kara if (unlikely(is_journal_aborted(journal))) 715470decc6SDave Kleikamp err = -EIO; 716470decc6SDave Kleikamp return err; 717470decc6SDave Kleikamp } 718470decc6SDave Kleikamp 719ff780b91SHarshad Shirwadkar /* 720ff780b91SHarshad Shirwadkar * Start a fast commit. If there's an ongoing fast or full commit wait for 721ff780b91SHarshad Shirwadkar * it to complete. Returns 0 if a new fast commit was started. Returns -EALREADY 722ff780b91SHarshad Shirwadkar * if a fast commit is not needed, either because there's an already a commit 723ff780b91SHarshad Shirwadkar * going on or this tid has already been committed. Returns -EINVAL if no jbd2 724ff780b91SHarshad Shirwadkar * commit has yet been performed. 725ff780b91SHarshad Shirwadkar */ 726ff780b91SHarshad Shirwadkar int jbd2_fc_begin_commit(journal_t *journal, tid_t tid) 727ff780b91SHarshad Shirwadkar { 72887a144f0SHarshad Shirwadkar if (unlikely(is_journal_aborted(journal))) 72987a144f0SHarshad Shirwadkar return -EIO; 730ff780b91SHarshad Shirwadkar /* 731ff780b91SHarshad Shirwadkar * Fast commits only allowed if at least one full commit has 732ff780b91SHarshad Shirwadkar * been processed. 733ff780b91SHarshad Shirwadkar */ 734ff780b91SHarshad Shirwadkar if (!journal->j_stats.ts_tid) 735ff780b91SHarshad Shirwadkar return -EINVAL; 736ff780b91SHarshad Shirwadkar 737ff780b91SHarshad Shirwadkar write_lock(&journal->j_state_lock); 738480f89d5SHarshad Shirwadkar if (tid <= journal->j_commit_sequence) { 739480f89d5SHarshad Shirwadkar write_unlock(&journal->j_state_lock); 740480f89d5SHarshad Shirwadkar return -EALREADY; 741480f89d5SHarshad Shirwadkar } 742480f89d5SHarshad Shirwadkar 743ff780b91SHarshad Shirwadkar if (journal->j_flags & JBD2_FULL_COMMIT_ONGOING || 744ff780b91SHarshad Shirwadkar (journal->j_flags & JBD2_FAST_COMMIT_ONGOING)) { 745ff780b91SHarshad Shirwadkar DEFINE_WAIT(wait); 746ff780b91SHarshad Shirwadkar 747ff780b91SHarshad Shirwadkar prepare_to_wait(&journal->j_fc_wait, &wait, 748ff780b91SHarshad Shirwadkar TASK_UNINTERRUPTIBLE); 749ff780b91SHarshad Shirwadkar write_unlock(&journal->j_state_lock); 750ff780b91SHarshad Shirwadkar schedule(); 751ff780b91SHarshad Shirwadkar finish_wait(&journal->j_fc_wait, &wait); 752ff780b91SHarshad Shirwadkar return -EALREADY; 753ff780b91SHarshad Shirwadkar } 754ff780b91SHarshad Shirwadkar journal->j_flags |= JBD2_FAST_COMMIT_ONGOING; 755ff780b91SHarshad Shirwadkar write_unlock(&journal->j_state_lock); 7562729cfdcSHarshad Shirwadkar jbd2_journal_lock_updates(journal); 757ff780b91SHarshad Shirwadkar 758ff780b91SHarshad Shirwadkar return 0; 759ff780b91SHarshad Shirwadkar } 760ff780b91SHarshad Shirwadkar EXPORT_SYMBOL(jbd2_fc_begin_commit); 761ff780b91SHarshad Shirwadkar 762ff780b91SHarshad Shirwadkar /* 763ff780b91SHarshad Shirwadkar * Stop a fast commit. If fallback is set, this function starts commit of 764ff780b91SHarshad Shirwadkar * TID tid before any other fast commit can start. 765ff780b91SHarshad Shirwadkar */ 766ff780b91SHarshad Shirwadkar static int __jbd2_fc_end_commit(journal_t *journal, tid_t tid, bool fallback) 767ff780b91SHarshad Shirwadkar { 7682729cfdcSHarshad Shirwadkar jbd2_journal_unlock_updates(journal); 769ff780b91SHarshad Shirwadkar if (journal->j_fc_cleanup_callback) 770e85c81baSXin Yin journal->j_fc_cleanup_callback(journal, 0, tid); 771ff780b91SHarshad Shirwadkar write_lock(&journal->j_state_lock); 772ff780b91SHarshad Shirwadkar journal->j_flags &= ~JBD2_FAST_COMMIT_ONGOING; 773ff780b91SHarshad Shirwadkar if (fallback) 774ff780b91SHarshad Shirwadkar journal->j_flags |= JBD2_FULL_COMMIT_ONGOING; 775ff780b91SHarshad Shirwadkar write_unlock(&journal->j_state_lock); 776ff780b91SHarshad Shirwadkar wake_up(&journal->j_fc_wait); 777ff780b91SHarshad Shirwadkar if (fallback) 778ff780b91SHarshad Shirwadkar return jbd2_complete_transaction(journal, tid); 779ff780b91SHarshad Shirwadkar return 0; 780ff780b91SHarshad Shirwadkar } 781ff780b91SHarshad Shirwadkar 782ff780b91SHarshad Shirwadkar int jbd2_fc_end_commit(journal_t *journal) 783ff780b91SHarshad Shirwadkar { 7840bce577bSHarshad Shirwadkar return __jbd2_fc_end_commit(journal, 0, false); 785ff780b91SHarshad Shirwadkar } 786ff780b91SHarshad Shirwadkar EXPORT_SYMBOL(jbd2_fc_end_commit); 787ff780b91SHarshad Shirwadkar 7880bce577bSHarshad Shirwadkar int jbd2_fc_end_commit_fallback(journal_t *journal) 789ff780b91SHarshad Shirwadkar { 7900bce577bSHarshad Shirwadkar tid_t tid; 7910bce577bSHarshad Shirwadkar 7920bce577bSHarshad Shirwadkar read_lock(&journal->j_state_lock); 7930bce577bSHarshad Shirwadkar tid = journal->j_running_transaction ? 7940bce577bSHarshad Shirwadkar journal->j_running_transaction->t_tid : 0; 7950bce577bSHarshad Shirwadkar read_unlock(&journal->j_state_lock); 7960bce577bSHarshad Shirwadkar return __jbd2_fc_end_commit(journal, tid, true); 797ff780b91SHarshad Shirwadkar } 798ff780b91SHarshad Shirwadkar EXPORT_SYMBOL(jbd2_fc_end_commit_fallback); 799ff780b91SHarshad Shirwadkar 800b8a6176cSJan Kara /* Return 1 when transaction with given tid has already committed. */ 801b8a6176cSJan Kara int jbd2_transaction_committed(journal_t *journal, tid_t tid) 802b8a6176cSJan Kara { 803b8a6176cSJan Kara int ret = 1; 804b8a6176cSJan Kara 805b8a6176cSJan Kara read_lock(&journal->j_state_lock); 806b8a6176cSJan Kara if (journal->j_running_transaction && 807b8a6176cSJan Kara journal->j_running_transaction->t_tid == tid) 808b8a6176cSJan Kara ret = 0; 809b8a6176cSJan Kara if (journal->j_committing_transaction && 810b8a6176cSJan Kara journal->j_committing_transaction->t_tid == tid) 811b8a6176cSJan Kara ret = 0; 812b8a6176cSJan Kara read_unlock(&journal->j_state_lock); 813b8a6176cSJan Kara return ret; 814b8a6176cSJan Kara } 815b8a6176cSJan Kara EXPORT_SYMBOL(jbd2_transaction_committed); 816b8a6176cSJan Kara 817470decc6SDave Kleikamp /* 818d76a3a77STheodore Ts'o * When this function returns the transaction corresponding to tid 819d76a3a77STheodore Ts'o * will be completed. If the transaction has currently running, start 820d76a3a77STheodore Ts'o * committing that transaction before waiting for it to complete. If 821d76a3a77STheodore Ts'o * the transaction id is stale, it is by definition already completed, 822d76a3a77STheodore Ts'o * so just return SUCCESS. 823d76a3a77STheodore Ts'o */ 824d76a3a77STheodore Ts'o int jbd2_complete_transaction(journal_t *journal, tid_t tid) 825d76a3a77STheodore Ts'o { 826d76a3a77STheodore Ts'o int need_to_wait = 1; 827d76a3a77STheodore Ts'o 828d76a3a77STheodore Ts'o read_lock(&journal->j_state_lock); 829d76a3a77STheodore Ts'o if (journal->j_running_transaction && 830d76a3a77STheodore Ts'o journal->j_running_transaction->t_tid == tid) { 831d76a3a77STheodore Ts'o if (journal->j_commit_request != tid) { 832d76a3a77STheodore Ts'o /* transaction not yet started, so request it */ 833d76a3a77STheodore Ts'o read_unlock(&journal->j_state_lock); 834d76a3a77STheodore Ts'o jbd2_log_start_commit(journal, tid); 835d76a3a77STheodore Ts'o goto wait_commit; 836d76a3a77STheodore Ts'o } 837d76a3a77STheodore Ts'o } else if (!(journal->j_committing_transaction && 838d76a3a77STheodore Ts'o journal->j_committing_transaction->t_tid == tid)) 839d76a3a77STheodore Ts'o need_to_wait = 0; 840d76a3a77STheodore Ts'o read_unlock(&journal->j_state_lock); 841d76a3a77STheodore Ts'o if (!need_to_wait) 842d76a3a77STheodore Ts'o return 0; 843d76a3a77STheodore Ts'o wait_commit: 844d76a3a77STheodore Ts'o return jbd2_log_wait_commit(journal, tid); 845d76a3a77STheodore Ts'o } 846d76a3a77STheodore Ts'o EXPORT_SYMBOL(jbd2_complete_transaction); 847d76a3a77STheodore Ts'o 848d76a3a77STheodore Ts'o /* 849470decc6SDave Kleikamp * Log buffer allocation routines: 850470decc6SDave Kleikamp */ 851470decc6SDave Kleikamp 85218eba7aaSMingming Cao int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp) 853470decc6SDave Kleikamp { 854470decc6SDave Kleikamp unsigned long blocknr; 855470decc6SDave Kleikamp 856a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 857470decc6SDave Kleikamp J_ASSERT(journal->j_free > 1); 858470decc6SDave Kleikamp 859470decc6SDave Kleikamp blocknr = journal->j_head; 860470decc6SDave Kleikamp journal->j_head++; 861470decc6SDave Kleikamp journal->j_free--; 862470decc6SDave Kleikamp if (journal->j_head == journal->j_last) 863470decc6SDave Kleikamp journal->j_head = journal->j_first; 864a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 865f7f4bccbSMingming Cao return jbd2_journal_bmap(journal, blocknr, retp); 866470decc6SDave Kleikamp } 867470decc6SDave Kleikamp 868ff780b91SHarshad Shirwadkar /* Map one fast commit buffer for use by the file system */ 869ff780b91SHarshad Shirwadkar int jbd2_fc_get_buf(journal_t *journal, struct buffer_head **bh_out) 870ff780b91SHarshad Shirwadkar { 871ff780b91SHarshad Shirwadkar unsigned long long pblock; 872ff780b91SHarshad Shirwadkar unsigned long blocknr; 873ff780b91SHarshad Shirwadkar int ret = 0; 874ff780b91SHarshad Shirwadkar struct buffer_head *bh; 875ff780b91SHarshad Shirwadkar int fc_off; 876ff780b91SHarshad Shirwadkar 877ff780b91SHarshad Shirwadkar *bh_out = NULL; 878ff780b91SHarshad Shirwadkar 879ff780b91SHarshad Shirwadkar if (journal->j_fc_off + journal->j_fc_first < journal->j_fc_last) { 880ff780b91SHarshad Shirwadkar fc_off = journal->j_fc_off; 881ff780b91SHarshad Shirwadkar blocknr = journal->j_fc_first + fc_off; 882ff780b91SHarshad Shirwadkar journal->j_fc_off++; 883ff780b91SHarshad Shirwadkar } else { 884ff780b91SHarshad Shirwadkar ret = -EINVAL; 885ff780b91SHarshad Shirwadkar } 886ff780b91SHarshad Shirwadkar 887ff780b91SHarshad Shirwadkar if (ret) 888ff780b91SHarshad Shirwadkar return ret; 889ff780b91SHarshad Shirwadkar 890ff780b91SHarshad Shirwadkar ret = jbd2_journal_bmap(journal, blocknr, &pblock); 891ff780b91SHarshad Shirwadkar if (ret) 892ff780b91SHarshad Shirwadkar return ret; 893ff780b91SHarshad Shirwadkar 894ff780b91SHarshad Shirwadkar bh = __getblk(journal->j_dev, pblock, journal->j_blocksize); 895ff780b91SHarshad Shirwadkar if (!bh) 896ff780b91SHarshad Shirwadkar return -ENOMEM; 897ff780b91SHarshad Shirwadkar 898ff780b91SHarshad Shirwadkar 899ff780b91SHarshad Shirwadkar journal->j_fc_wbuf[fc_off] = bh; 900ff780b91SHarshad Shirwadkar 901ff780b91SHarshad Shirwadkar *bh_out = bh; 902ff780b91SHarshad Shirwadkar 903ff780b91SHarshad Shirwadkar return 0; 904ff780b91SHarshad Shirwadkar } 905ff780b91SHarshad Shirwadkar EXPORT_SYMBOL(jbd2_fc_get_buf); 906ff780b91SHarshad Shirwadkar 907ff780b91SHarshad Shirwadkar /* 908ff780b91SHarshad Shirwadkar * Wait on fast commit buffers that were allocated by jbd2_fc_get_buf 909ff780b91SHarshad Shirwadkar * for completion. 910ff780b91SHarshad Shirwadkar */ 911ff780b91SHarshad Shirwadkar int jbd2_fc_wait_bufs(journal_t *journal, int num_blks) 912ff780b91SHarshad Shirwadkar { 913ff780b91SHarshad Shirwadkar struct buffer_head *bh; 914ff780b91SHarshad Shirwadkar int i, j_fc_off; 915ff780b91SHarshad Shirwadkar 916ff780b91SHarshad Shirwadkar j_fc_off = journal->j_fc_off; 917ff780b91SHarshad Shirwadkar 918ff780b91SHarshad Shirwadkar /* 919ff780b91SHarshad Shirwadkar * Wait in reverse order to minimize chances of us being woken up before 920ff780b91SHarshad Shirwadkar * all IOs have completed 921ff780b91SHarshad Shirwadkar */ 922ff780b91SHarshad Shirwadkar for (i = j_fc_off - 1; i >= j_fc_off - num_blks; i--) { 923ff780b91SHarshad Shirwadkar bh = journal->j_fc_wbuf[i]; 924ff780b91SHarshad Shirwadkar wait_on_buffer(bh); 925e0d5fc7aSYe Bin /* 926e0d5fc7aSYe Bin * Update j_fc_off so jbd2_fc_release_bufs can release remain 927e0d5fc7aSYe Bin * buffer head. 928e0d5fc7aSYe Bin */ 929e0d5fc7aSYe Bin if (unlikely(!buffer_uptodate(bh))) { 930243d1a5dSYe Bin journal->j_fc_off = i + 1; 931ff780b91SHarshad Shirwadkar return -EIO; 932ff780b91SHarshad Shirwadkar } 933243d1a5dSYe Bin put_bh(bh); 934243d1a5dSYe Bin journal->j_fc_wbuf[i] = NULL; 935e0d5fc7aSYe Bin } 936ff780b91SHarshad Shirwadkar 937ff780b91SHarshad Shirwadkar return 0; 938ff780b91SHarshad Shirwadkar } 939ff780b91SHarshad Shirwadkar EXPORT_SYMBOL(jbd2_fc_wait_bufs); 940ff780b91SHarshad Shirwadkar 941ff780b91SHarshad Shirwadkar int jbd2_fc_release_bufs(journal_t *journal) 942ff780b91SHarshad Shirwadkar { 943ff780b91SHarshad Shirwadkar struct buffer_head *bh; 944ff780b91SHarshad Shirwadkar int i, j_fc_off; 945ff780b91SHarshad Shirwadkar 946ff780b91SHarshad Shirwadkar j_fc_off = journal->j_fc_off; 947ff780b91SHarshad Shirwadkar 948ff780b91SHarshad Shirwadkar for (i = j_fc_off - 1; i >= 0; i--) { 949ff780b91SHarshad Shirwadkar bh = journal->j_fc_wbuf[i]; 950ff780b91SHarshad Shirwadkar if (!bh) 951ff780b91SHarshad Shirwadkar break; 952ff780b91SHarshad Shirwadkar put_bh(bh); 953ff780b91SHarshad Shirwadkar journal->j_fc_wbuf[i] = NULL; 954ff780b91SHarshad Shirwadkar } 955ff780b91SHarshad Shirwadkar 956ff780b91SHarshad Shirwadkar return 0; 957ff780b91SHarshad Shirwadkar } 958ff780b91SHarshad Shirwadkar EXPORT_SYMBOL(jbd2_fc_release_bufs); 959ff780b91SHarshad Shirwadkar 960470decc6SDave Kleikamp /* 961470decc6SDave Kleikamp * Conversion of logical to physical block numbers for the journal 962470decc6SDave Kleikamp * 963470decc6SDave Kleikamp * On external journals the journal blocks are identity-mapped, so 964470decc6SDave Kleikamp * this is a no-op. If needed, we can use j_blk_offset - everything is 965470decc6SDave Kleikamp * ready. 966470decc6SDave Kleikamp */ 967f7f4bccbSMingming Cao int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr, 96818eba7aaSMingming Cao unsigned long long *retp) 969470decc6SDave Kleikamp { 970470decc6SDave Kleikamp int err = 0; 97118eba7aaSMingming Cao unsigned long long ret; 97262913ae9STheodore Ts'o sector_t block = blocknr; 973470decc6SDave Kleikamp 97462913ae9STheodore Ts'o if (journal->j_bmap) { 97562913ae9STheodore Ts'o err = journal->j_bmap(journal, &block); 97662913ae9STheodore Ts'o if (err == 0) 97762913ae9STheodore Ts'o *retp = block; 97862913ae9STheodore Ts'o } else if (journal->j_inode) { 97930460e1eSCarlos Maiolino ret = bmap(journal->j_inode, &block); 98030460e1eSCarlos Maiolino 98130460e1eSCarlos Maiolino if (ret || !block) { 982470decc6SDave Kleikamp printk(KERN_ALERT "%s: journal block not found " 983470decc6SDave Kleikamp "at offset %lu on %s\n", 98405496769STheodore Ts'o __func__, blocknr, journal->j_devname); 985470decc6SDave Kleikamp err = -EIO; 9867f6225e4Szhangyi (F) jbd2_journal_abort(journal, err); 98730460e1eSCarlos Maiolino } else { 98830460e1eSCarlos Maiolino *retp = block; 989470decc6SDave Kleikamp } 99030460e1eSCarlos Maiolino 991470decc6SDave Kleikamp } else { 992470decc6SDave Kleikamp *retp = blocknr; /* +journal->j_blk_offset */ 993470decc6SDave Kleikamp } 994470decc6SDave Kleikamp return err; 995470decc6SDave Kleikamp } 996470decc6SDave Kleikamp 997470decc6SDave Kleikamp /* 998470decc6SDave Kleikamp * We play buffer_head aliasing tricks to write data/metadata blocks to 999470decc6SDave Kleikamp * the journal without copying their contents, but for journal 1000470decc6SDave Kleikamp * descriptor blocks we do need to generate bona fide buffers. 1001470decc6SDave Kleikamp * 1002f7f4bccbSMingming Cao * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying 1003470decc6SDave Kleikamp * the buffer's contents they really should run flush_dcache_page(bh->b_page). 1004470decc6SDave Kleikamp * But we don't bother doing that, so there will be coherency problems with 1005470decc6SDave Kleikamp * mmaps of blockdevs which hold live JBD-controlled filesystems. 1006470decc6SDave Kleikamp */ 100732ab6715SJan Kara struct buffer_head * 100832ab6715SJan Kara jbd2_journal_get_descriptor_buffer(transaction_t *transaction, int type) 1009470decc6SDave Kleikamp { 101032ab6715SJan Kara journal_t *journal = transaction->t_journal; 1011470decc6SDave Kleikamp struct buffer_head *bh; 101218eba7aaSMingming Cao unsigned long long blocknr; 101332ab6715SJan Kara journal_header_t *header; 1014470decc6SDave Kleikamp int err; 1015470decc6SDave Kleikamp 1016f7f4bccbSMingming Cao err = jbd2_journal_next_log_block(journal, &blocknr); 1017470decc6SDave Kleikamp 1018470decc6SDave Kleikamp if (err) 1019470decc6SDave Kleikamp return NULL; 1020470decc6SDave Kleikamp 1021470decc6SDave Kleikamp bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize); 10224b905671SJan Kara if (!bh) 10234b905671SJan Kara return NULL; 10249f356e5aSJan Kara atomic_dec(&transaction->t_outstanding_credits); 1025470decc6SDave Kleikamp lock_buffer(bh); 1026470decc6SDave Kleikamp memset(bh->b_data, 0, journal->j_blocksize); 102732ab6715SJan Kara header = (journal_header_t *)bh->b_data; 102832ab6715SJan Kara header->h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER); 102932ab6715SJan Kara header->h_blocktype = cpu_to_be32(type); 103032ab6715SJan Kara header->h_sequence = cpu_to_be32(transaction->t_tid); 1031470decc6SDave Kleikamp set_buffer_uptodate(bh); 1032470decc6SDave Kleikamp unlock_buffer(bh); 1033470decc6SDave Kleikamp BUFFER_TRACE(bh, "return this buffer"); 1034e5a120aeSJan Kara return bh; 1035470decc6SDave Kleikamp } 1036470decc6SDave Kleikamp 10371101cd4dSJan Kara void jbd2_descriptor_block_csum_set(journal_t *j, struct buffer_head *bh) 10381101cd4dSJan Kara { 10391101cd4dSJan Kara struct jbd2_journal_block_tail *tail; 10401101cd4dSJan Kara __u32 csum; 10411101cd4dSJan Kara 10421101cd4dSJan Kara if (!jbd2_journal_has_csum_v2or3(j)) 10431101cd4dSJan Kara return; 10441101cd4dSJan Kara 10451101cd4dSJan Kara tail = (struct jbd2_journal_block_tail *)(bh->b_data + j->j_blocksize - 10461101cd4dSJan Kara sizeof(struct jbd2_journal_block_tail)); 10471101cd4dSJan Kara tail->t_checksum = 0; 10481101cd4dSJan Kara csum = jbd2_chksum(j, j->j_csum_seed, bh->b_data, j->j_blocksize); 10491101cd4dSJan Kara tail->t_checksum = cpu_to_be32(csum); 10501101cd4dSJan Kara } 10511101cd4dSJan Kara 105279feb521SJan Kara /* 105379feb521SJan Kara * Return tid of the oldest transaction in the journal and block in the journal 105479feb521SJan Kara * where the transaction starts. 105579feb521SJan Kara * 105679feb521SJan Kara * If the journal is now empty, return which will be the next transaction ID 105779feb521SJan Kara * we will write and where will that transaction start. 105879feb521SJan Kara * 105979feb521SJan Kara * The return value is 0 if journal tail cannot be pushed any further, 1 if 106079feb521SJan Kara * it can. 106179feb521SJan Kara */ 106279feb521SJan Kara int jbd2_journal_get_log_tail(journal_t *journal, tid_t *tid, 106379feb521SJan Kara unsigned long *block) 106479feb521SJan Kara { 106579feb521SJan Kara transaction_t *transaction; 106679feb521SJan Kara int ret; 106779feb521SJan Kara 106879feb521SJan Kara read_lock(&journal->j_state_lock); 106979feb521SJan Kara spin_lock(&journal->j_list_lock); 107079feb521SJan Kara transaction = journal->j_checkpoint_transactions; 107179feb521SJan Kara if (transaction) { 107279feb521SJan Kara *tid = transaction->t_tid; 107379feb521SJan Kara *block = transaction->t_log_start; 107479feb521SJan Kara } else if ((transaction = journal->j_committing_transaction) != NULL) { 107579feb521SJan Kara *tid = transaction->t_tid; 107679feb521SJan Kara *block = transaction->t_log_start; 107779feb521SJan Kara } else if ((transaction = journal->j_running_transaction) != NULL) { 107879feb521SJan Kara *tid = transaction->t_tid; 107979feb521SJan Kara *block = journal->j_head; 108079feb521SJan Kara } else { 108179feb521SJan Kara *tid = journal->j_transaction_sequence; 108279feb521SJan Kara *block = journal->j_head; 108379feb521SJan Kara } 108479feb521SJan Kara ret = tid_gt(*tid, journal->j_tail_sequence); 108579feb521SJan Kara spin_unlock(&journal->j_list_lock); 108679feb521SJan Kara read_unlock(&journal->j_state_lock); 108779feb521SJan Kara 108879feb521SJan Kara return ret; 108979feb521SJan Kara } 109079feb521SJan Kara 109179feb521SJan Kara /* 109279feb521SJan Kara * Update information in journal structure and in on disk journal superblock 109379feb521SJan Kara * about log tail. This function does not check whether information passed in 109479feb521SJan Kara * really pushes log tail further. It's responsibility of the caller to make 109579feb521SJan Kara * sure provided log tail information is valid (e.g. by holding 109679feb521SJan Kara * j_checkpoint_mutex all the time between computing log tail and calling this 109779feb521SJan Kara * function as is the case with jbd2_cleanup_journal_tail()). 109879feb521SJan Kara * 109979feb521SJan Kara * Requires j_checkpoint_mutex 110079feb521SJan Kara */ 11016f6a6fdaSJoseph Qi int __jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block) 110279feb521SJan Kara { 110379feb521SJan Kara unsigned long freed; 11046f6a6fdaSJoseph Qi int ret; 110579feb521SJan Kara 110679feb521SJan Kara BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex)); 110779feb521SJan Kara 110879feb521SJan Kara /* 110979feb521SJan Kara * We cannot afford for write to remain in drive's caches since as 111079feb521SJan Kara * soon as we update j_tail, next transaction can start reusing journal 111179feb521SJan Kara * space and if we lose sb update during power failure we'd replay 111279feb521SJan Kara * old transaction with possibly newly overwritten data. 111379feb521SJan Kara */ 111417f423b5SJan Kara ret = jbd2_journal_update_sb_log_tail(journal, tid, block, 111517f423b5SJan Kara REQ_SYNC | REQ_FUA); 11166f6a6fdaSJoseph Qi if (ret) 11176f6a6fdaSJoseph Qi goto out; 11186f6a6fdaSJoseph Qi 111979feb521SJan Kara write_lock(&journal->j_state_lock); 112079feb521SJan Kara freed = block - journal->j_tail; 112179feb521SJan Kara if (block < journal->j_tail) 112279feb521SJan Kara freed += journal->j_last - journal->j_first; 112379feb521SJan Kara 112479feb521SJan Kara trace_jbd2_update_log_tail(journal, tid, block, freed); 1125cb3b3bf2SJan Kara jbd2_debug(1, 11267821ce41SGaowei Pu "Cleaning journal tail from %u to %u (offset %lu), " 112779feb521SJan Kara "freeing %lu\n", 112879feb521SJan Kara journal->j_tail_sequence, tid, block, freed); 112979feb521SJan Kara 113079feb521SJan Kara journal->j_free += freed; 113179feb521SJan Kara journal->j_tail_sequence = tid; 113279feb521SJan Kara journal->j_tail = block; 113379feb521SJan Kara write_unlock(&journal->j_state_lock); 11346f6a6fdaSJoseph Qi 11356f6a6fdaSJoseph Qi out: 11366f6a6fdaSJoseph Qi return ret; 113779feb521SJan Kara } 113879feb521SJan Kara 11393339578fSJan Kara /* 114085e0c4e8STheodore Ts'o * This is a variation of __jbd2_update_log_tail which checks for validity of 11413339578fSJan Kara * provided log tail and locks j_checkpoint_mutex. So it is safe against races 11423339578fSJan Kara * with other threads updating log tail. 11433339578fSJan Kara */ 11443339578fSJan Kara void jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block) 11453339578fSJan Kara { 11466fa7aa50STejun Heo mutex_lock_io(&journal->j_checkpoint_mutex); 11473339578fSJan Kara if (tid_gt(tid, journal->j_tail_sequence)) 11483339578fSJan Kara __jbd2_update_log_tail(journal, tid, block); 11493339578fSJan Kara mutex_unlock(&journal->j_checkpoint_mutex); 11503339578fSJan Kara } 11513339578fSJan Kara 11528e85fb3fSJohann Lombardi struct jbd2_stats_proc_session { 11538e85fb3fSJohann Lombardi journal_t *journal; 11548e85fb3fSJohann Lombardi struct transaction_stats_s *stats; 11558e85fb3fSJohann Lombardi int start; 11568e85fb3fSJohann Lombardi int max; 11578e85fb3fSJohann Lombardi }; 11588e85fb3fSJohann Lombardi 11598e85fb3fSJohann Lombardi static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos) 11608e85fb3fSJohann Lombardi { 11618e85fb3fSJohann Lombardi return *pos ? NULL : SEQ_START_TOKEN; 11628e85fb3fSJohann Lombardi } 11638e85fb3fSJohann Lombardi 11648e85fb3fSJohann Lombardi static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos) 11658e85fb3fSJohann Lombardi { 11661a8e9cf4SVasily Averin (*pos)++; 11678e85fb3fSJohann Lombardi return NULL; 11688e85fb3fSJohann Lombardi } 11698e85fb3fSJohann Lombardi 11708e85fb3fSJohann Lombardi static int jbd2_seq_info_show(struct seq_file *seq, void *v) 11718e85fb3fSJohann Lombardi { 11728e85fb3fSJohann Lombardi struct jbd2_stats_proc_session *s = seq->private; 11738e85fb3fSJohann Lombardi 11748e85fb3fSJohann Lombardi if (v != SEQ_START_TOKEN) 11758e85fb3fSJohann Lombardi return 0; 11769fff24aaSTheodore Ts'o seq_printf(seq, "%lu transactions (%lu requested), " 11779fff24aaSTheodore Ts'o "each up to %u blocks\n", 11789fff24aaSTheodore Ts'o s->stats->ts_tid, s->stats->ts_requested, 11798e85fb3fSJohann Lombardi s->journal->j_max_transaction_buffers); 11808e85fb3fSJohann Lombardi if (s->stats->ts_tid == 0) 11818e85fb3fSJohann Lombardi return 0; 11828e85fb3fSJohann Lombardi seq_printf(seq, "average: \n %ums waiting for transaction\n", 1183bf699327STheodore Ts'o jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid)); 11849fff24aaSTheodore Ts'o seq_printf(seq, " %ums request delay\n", 11859fff24aaSTheodore Ts'o (s->stats->ts_requested == 0) ? 0 : 11869fff24aaSTheodore Ts'o jiffies_to_msecs(s->stats->run.rs_request_delay / 11879fff24aaSTheodore Ts'o s->stats->ts_requested)); 11888e85fb3fSJohann Lombardi seq_printf(seq, " %ums running transaction\n", 1189bf699327STheodore Ts'o jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid)); 11908e85fb3fSJohann Lombardi seq_printf(seq, " %ums transaction was being locked\n", 1191bf699327STheodore Ts'o jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid)); 11928e85fb3fSJohann Lombardi seq_printf(seq, " %ums flushing data (in ordered mode)\n", 1193bf699327STheodore Ts'o jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid)); 11948e85fb3fSJohann Lombardi seq_printf(seq, " %ums logging transaction\n", 1195bf699327STheodore Ts'o jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid)); 1196c225aa57SSimon Holm Thøgersen seq_printf(seq, " %lluus average transaction commit time\n", 1197c225aa57SSimon Holm Thøgersen div_u64(s->journal->j_average_commit_time, 1000)); 11988e85fb3fSJohann Lombardi seq_printf(seq, " %lu handles per transaction\n", 1199bf699327STheodore Ts'o s->stats->run.rs_handle_count / s->stats->ts_tid); 12008e85fb3fSJohann Lombardi seq_printf(seq, " %lu blocks per transaction\n", 1201bf699327STheodore Ts'o s->stats->run.rs_blocks / s->stats->ts_tid); 12028e85fb3fSJohann Lombardi seq_printf(seq, " %lu logged blocks per transaction\n", 1203bf699327STheodore Ts'o s->stats->run.rs_blocks_logged / s->stats->ts_tid); 12048e85fb3fSJohann Lombardi return 0; 12058e85fb3fSJohann Lombardi } 12068e85fb3fSJohann Lombardi 12078e85fb3fSJohann Lombardi static void jbd2_seq_info_stop(struct seq_file *seq, void *v) 12088e85fb3fSJohann Lombardi { 12098e85fb3fSJohann Lombardi } 12108e85fb3fSJohann Lombardi 121188e9d34cSJames Morris static const struct seq_operations jbd2_seq_info_ops = { 12128e85fb3fSJohann Lombardi .start = jbd2_seq_info_start, 12138e85fb3fSJohann Lombardi .next = jbd2_seq_info_next, 12148e85fb3fSJohann Lombardi .stop = jbd2_seq_info_stop, 12158e85fb3fSJohann Lombardi .show = jbd2_seq_info_show, 12168e85fb3fSJohann Lombardi }; 12178e85fb3fSJohann Lombardi 12188e85fb3fSJohann Lombardi static int jbd2_seq_info_open(struct inode *inode, struct file *file) 12198e85fb3fSJohann Lombardi { 1220359745d7SMuchun Song journal_t *journal = pde_data(inode); 12218e85fb3fSJohann Lombardi struct jbd2_stats_proc_session *s; 12228e85fb3fSJohann Lombardi int rc, size; 12238e85fb3fSJohann Lombardi 12248e85fb3fSJohann Lombardi s = kmalloc(sizeof(*s), GFP_KERNEL); 12258e85fb3fSJohann Lombardi if (s == NULL) 12268e85fb3fSJohann Lombardi return -ENOMEM; 12278e85fb3fSJohann Lombardi size = sizeof(struct transaction_stats_s); 12288e85fb3fSJohann Lombardi s->stats = kmalloc(size, GFP_KERNEL); 12298e85fb3fSJohann Lombardi if (s->stats == NULL) { 12308e85fb3fSJohann Lombardi kfree(s); 12318e85fb3fSJohann Lombardi return -ENOMEM; 12328e85fb3fSJohann Lombardi } 12338e85fb3fSJohann Lombardi spin_lock(&journal->j_history_lock); 12348e85fb3fSJohann Lombardi memcpy(s->stats, &journal->j_stats, size); 12358e85fb3fSJohann Lombardi s->journal = journal; 12368e85fb3fSJohann Lombardi spin_unlock(&journal->j_history_lock); 12378e85fb3fSJohann Lombardi 12388e85fb3fSJohann Lombardi rc = seq_open(file, &jbd2_seq_info_ops); 12398e85fb3fSJohann Lombardi if (rc == 0) { 12408e85fb3fSJohann Lombardi struct seq_file *m = file->private_data; 12418e85fb3fSJohann Lombardi m->private = s; 12428e85fb3fSJohann Lombardi } else { 12438e85fb3fSJohann Lombardi kfree(s->stats); 12448e85fb3fSJohann Lombardi kfree(s); 12458e85fb3fSJohann Lombardi } 12468e85fb3fSJohann Lombardi return rc; 12478e85fb3fSJohann Lombardi 12488e85fb3fSJohann Lombardi } 12498e85fb3fSJohann Lombardi 12508e85fb3fSJohann Lombardi static int jbd2_seq_info_release(struct inode *inode, struct file *file) 12518e85fb3fSJohann Lombardi { 12528e85fb3fSJohann Lombardi struct seq_file *seq = file->private_data; 12538e85fb3fSJohann Lombardi struct jbd2_stats_proc_session *s = seq->private; 12548e85fb3fSJohann Lombardi kfree(s->stats); 12558e85fb3fSJohann Lombardi kfree(s); 12568e85fb3fSJohann Lombardi return seq_release(inode, file); 12578e85fb3fSJohann Lombardi } 12588e85fb3fSJohann Lombardi 125997a32539SAlexey Dobriyan static const struct proc_ops jbd2_info_proc_ops = { 126097a32539SAlexey Dobriyan .proc_open = jbd2_seq_info_open, 126197a32539SAlexey Dobriyan .proc_read = seq_read, 126297a32539SAlexey Dobriyan .proc_lseek = seq_lseek, 126397a32539SAlexey Dobriyan .proc_release = jbd2_seq_info_release, 12648e85fb3fSJohann Lombardi }; 12658e85fb3fSJohann Lombardi 12668e85fb3fSJohann Lombardi static struct proc_dir_entry *proc_jbd2_stats; 12678e85fb3fSJohann Lombardi 12688e85fb3fSJohann Lombardi static void jbd2_stats_proc_init(journal_t *journal) 12698e85fb3fSJohann Lombardi { 127005496769STheodore Ts'o journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats); 12718e85fb3fSJohann Lombardi if (journal->j_proc_entry) { 127279da3664SDenis V. Lunev proc_create_data("info", S_IRUGO, journal->j_proc_entry, 127397a32539SAlexey Dobriyan &jbd2_info_proc_ops, journal); 12748e85fb3fSJohann Lombardi } 12758e85fb3fSJohann Lombardi } 12768e85fb3fSJohann Lombardi 12778e85fb3fSJohann Lombardi static void jbd2_stats_proc_exit(journal_t *journal) 12788e85fb3fSJohann Lombardi { 12798e85fb3fSJohann Lombardi remove_proc_entry("info", journal->j_proc_entry); 128005496769STheodore Ts'o remove_proc_entry(journal->j_devname, proc_jbd2_stats); 12818e85fb3fSJohann Lombardi } 12828e85fb3fSJohann Lombardi 1283b90bfdf5SJan Kara /* Minimum size of descriptor tag */ 1284b90bfdf5SJan Kara static int jbd2_min_tag_size(void) 1285b90bfdf5SJan Kara { 1286b90bfdf5SJan Kara /* 1287b90bfdf5SJan Kara * Tag with 32-bit block numbers does not use last four bytes of the 1288b90bfdf5SJan Kara * structure 1289b90bfdf5SJan Kara */ 1290b90bfdf5SJan Kara return sizeof(journal_block_tag_t) - 4; 1291b90bfdf5SJan Kara } 1292b90bfdf5SJan Kara 12930705e8d1STheodore Ts'o /** 12940705e8d1STheodore Ts'o * jbd2_journal_shrink_scan() 1295715a67f1SYang Li * @shrink: shrinker to work on 1296715a67f1SYang Li * @sc: reclaim request to process 12970705e8d1STheodore Ts'o * 12980705e8d1STheodore Ts'o * Scan the checkpointed buffer on the checkpoint list and release the 12990705e8d1STheodore Ts'o * journal_head. 13000705e8d1STheodore Ts'o */ 13010705e8d1STheodore Ts'o static unsigned long jbd2_journal_shrink_scan(struct shrinker *shrink, 13020705e8d1STheodore Ts'o struct shrink_control *sc) 13030705e8d1STheodore Ts'o { 13040705e8d1STheodore Ts'o journal_t *journal = container_of(shrink, journal_t, j_shrinker); 13050705e8d1STheodore Ts'o unsigned long nr_to_scan = sc->nr_to_scan; 13060705e8d1STheodore Ts'o unsigned long nr_shrunk; 13070705e8d1STheodore Ts'o unsigned long count; 13080705e8d1STheodore Ts'o 13090705e8d1STheodore Ts'o count = percpu_counter_read_positive(&journal->j_checkpoint_jh_count); 13100705e8d1STheodore Ts'o trace_jbd2_shrink_scan_enter(journal, sc->nr_to_scan, count); 13110705e8d1STheodore Ts'o 13120705e8d1STheodore Ts'o nr_shrunk = jbd2_journal_shrink_checkpoint_list(journal, &nr_to_scan); 13130705e8d1STheodore Ts'o 13140705e8d1STheodore Ts'o count = percpu_counter_read_positive(&journal->j_checkpoint_jh_count); 13150705e8d1STheodore Ts'o trace_jbd2_shrink_scan_exit(journal, nr_to_scan, nr_shrunk, count); 13160705e8d1STheodore Ts'o 13170705e8d1STheodore Ts'o return nr_shrunk; 13180705e8d1STheodore Ts'o } 13190705e8d1STheodore Ts'o 13200705e8d1STheodore Ts'o /** 13210705e8d1STheodore Ts'o * jbd2_journal_shrink_count() 1322715a67f1SYang Li * @shrink: shrinker to work on 1323715a67f1SYang Li * @sc: reclaim request to process 13240705e8d1STheodore Ts'o * 13250705e8d1STheodore Ts'o * Count the number of checkpoint buffers on the checkpoint list. 13260705e8d1STheodore Ts'o */ 13270705e8d1STheodore Ts'o static unsigned long jbd2_journal_shrink_count(struct shrinker *shrink, 13280705e8d1STheodore Ts'o struct shrink_control *sc) 13290705e8d1STheodore Ts'o { 13300705e8d1STheodore Ts'o journal_t *journal = container_of(shrink, journal_t, j_shrinker); 13310705e8d1STheodore Ts'o unsigned long count; 13320705e8d1STheodore Ts'o 13330705e8d1STheodore Ts'o count = percpu_counter_read_positive(&journal->j_checkpoint_jh_count); 13340705e8d1STheodore Ts'o trace_jbd2_shrink_count(journal, sc->nr_to_scan, count); 13350705e8d1STheodore Ts'o 13360705e8d1STheodore Ts'o return count; 13370705e8d1STheodore Ts'o } 13380705e8d1STheodore Ts'o 1339470decc6SDave Kleikamp /* 134029a511e4SZhang Yi * If the journal init or create aborts, we need to mark the journal 134129a511e4SZhang Yi * superblock as being NULL to prevent the journal destroy from writing 134229a511e4SZhang Yi * back a bogus superblock. 134329a511e4SZhang Yi */ 134429a511e4SZhang Yi static void journal_fail_superblock(journal_t *journal) 134529a511e4SZhang Yi { 134629a511e4SZhang Yi struct buffer_head *bh = journal->j_sb_buffer; 134729a511e4SZhang Yi brelse(bh); 134829a511e4SZhang Yi journal->j_sb_buffer = NULL; 134929a511e4SZhang Yi } 135029a511e4SZhang Yi 135129a511e4SZhang Yi /* 135229a511e4SZhang Yi * Read the superblock for a given journal, performing initial 135329a511e4SZhang Yi * validation of the format. 135429a511e4SZhang Yi */ 135529a511e4SZhang Yi static int journal_get_superblock(journal_t *journal) 135629a511e4SZhang Yi { 135729a511e4SZhang Yi struct buffer_head *bh; 135829a511e4SZhang Yi journal_superblock_t *sb; 135929a511e4SZhang Yi int err; 136029a511e4SZhang Yi 136129a511e4SZhang Yi bh = journal->j_sb_buffer; 136229a511e4SZhang Yi 136329a511e4SZhang Yi J_ASSERT(bh != NULL); 136429a511e4SZhang Yi if (buffer_verified(bh)) 136529a511e4SZhang Yi return 0; 136629a511e4SZhang Yi 136729a511e4SZhang Yi err = bh_read(bh, 0); 136829a511e4SZhang Yi if (err < 0) { 136929a511e4SZhang Yi printk(KERN_ERR 137029a511e4SZhang Yi "JBD2: IO error reading journal superblock\n"); 137129a511e4SZhang Yi goto out; 137229a511e4SZhang Yi } 137329a511e4SZhang Yi 137429a511e4SZhang Yi sb = journal->j_superblock; 137529a511e4SZhang Yi 137629a511e4SZhang Yi err = -EINVAL; 137729a511e4SZhang Yi 137829a511e4SZhang Yi if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) || 137929a511e4SZhang Yi sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) { 138029a511e4SZhang Yi printk(KERN_WARNING "JBD2: no valid journal superblock found\n"); 138129a511e4SZhang Yi goto out; 138229a511e4SZhang Yi } 138329a511e4SZhang Yi 138429a511e4SZhang Yi if (be32_to_cpu(sb->s_header.h_blocktype) != JBD2_SUPERBLOCK_V1 && 138529a511e4SZhang Yi be32_to_cpu(sb->s_header.h_blocktype) != JBD2_SUPERBLOCK_V2) { 138629a511e4SZhang Yi printk(KERN_WARNING "JBD2: unrecognised superblock format ID\n"); 138729a511e4SZhang Yi goto out; 138829a511e4SZhang Yi } 138929a511e4SZhang Yi 139029a511e4SZhang Yi if (be32_to_cpu(sb->s_maxlen) > journal->j_total_len) { 139129a511e4SZhang Yi printk(KERN_WARNING "JBD2: journal file too short\n"); 139229a511e4SZhang Yi goto out; 139329a511e4SZhang Yi } 139429a511e4SZhang Yi 139529a511e4SZhang Yi if (be32_to_cpu(sb->s_first) == 0 || 139629a511e4SZhang Yi be32_to_cpu(sb->s_first) >= journal->j_total_len) { 139729a511e4SZhang Yi printk(KERN_WARNING 139829a511e4SZhang Yi "JBD2: Invalid start block of journal: %u\n", 139929a511e4SZhang Yi be32_to_cpu(sb->s_first)); 140029a511e4SZhang Yi goto out; 140129a511e4SZhang Yi } 140229a511e4SZhang Yi 140329a511e4SZhang Yi if (jbd2_has_feature_csum2(journal) && 140429a511e4SZhang Yi jbd2_has_feature_csum3(journal)) { 140529a511e4SZhang Yi /* Can't have checksum v2 and v3 at the same time! */ 140629a511e4SZhang Yi printk(KERN_ERR "JBD2: Can't enable checksumming v2 and v3 " 140729a511e4SZhang Yi "at the same time!\n"); 140829a511e4SZhang Yi goto out; 140929a511e4SZhang Yi } 141029a511e4SZhang Yi 141129a511e4SZhang Yi if (jbd2_journal_has_csum_v2or3_feature(journal) && 141229a511e4SZhang Yi jbd2_has_feature_checksum(journal)) { 141329a511e4SZhang Yi /* Can't have checksum v1 and v2 on at the same time! */ 141429a511e4SZhang Yi printk(KERN_ERR "JBD2: Can't enable checksumming v1 and v2/3 " 141529a511e4SZhang Yi "at the same time!\n"); 141629a511e4SZhang Yi goto out; 141729a511e4SZhang Yi } 141829a511e4SZhang Yi 141929a511e4SZhang Yi if (!jbd2_verify_csum_type(journal, sb)) { 142029a511e4SZhang Yi printk(KERN_ERR "JBD2: Unknown checksum type\n"); 142129a511e4SZhang Yi goto out; 142229a511e4SZhang Yi } 142329a511e4SZhang Yi 142429a511e4SZhang Yi /* Load the checksum driver */ 142529a511e4SZhang Yi if (jbd2_journal_has_csum_v2or3_feature(journal)) { 142629a511e4SZhang Yi journal->j_chksum_driver = crypto_alloc_shash("crc32c", 0, 0); 142729a511e4SZhang Yi if (IS_ERR(journal->j_chksum_driver)) { 142829a511e4SZhang Yi printk(KERN_ERR "JBD2: Cannot load crc32c driver.\n"); 142929a511e4SZhang Yi err = PTR_ERR(journal->j_chksum_driver); 143029a511e4SZhang Yi journal->j_chksum_driver = NULL; 143129a511e4SZhang Yi goto out; 143229a511e4SZhang Yi } 143329a511e4SZhang Yi /* Check superblock checksum */ 143429a511e4SZhang Yi if (sb->s_checksum != jbd2_superblock_csum(journal, sb)) { 143529a511e4SZhang Yi printk(KERN_ERR "JBD2: journal checksum error\n"); 143629a511e4SZhang Yi err = -EFSBADCRC; 143729a511e4SZhang Yi goto out; 143829a511e4SZhang Yi } 143929a511e4SZhang Yi } 144029a511e4SZhang Yi set_buffer_verified(bh); 144129a511e4SZhang Yi return 0; 144229a511e4SZhang Yi 144329a511e4SZhang Yi out: 144429a511e4SZhang Yi journal_fail_superblock(journal); 144529a511e4SZhang Yi return err; 144629a511e4SZhang Yi } 144729a511e4SZhang Yi 144829a511e4SZhang Yi static int journal_revoke_records_per_block(journal_t *journal) 144929a511e4SZhang Yi { 145029a511e4SZhang Yi int record_size; 145129a511e4SZhang Yi int space = journal->j_blocksize - sizeof(jbd2_journal_revoke_header_t); 145229a511e4SZhang Yi 145329a511e4SZhang Yi if (jbd2_has_feature_64bit(journal)) 145429a511e4SZhang Yi record_size = 8; 145529a511e4SZhang Yi else 145629a511e4SZhang Yi record_size = 4; 145729a511e4SZhang Yi 145829a511e4SZhang Yi if (jbd2_journal_has_csum_v2or3(journal)) 145929a511e4SZhang Yi space -= sizeof(struct jbd2_journal_block_tail); 146029a511e4SZhang Yi return space / record_size; 146129a511e4SZhang Yi } 146229a511e4SZhang Yi 146329a511e4SZhang Yi /* 146429a511e4SZhang Yi * Load the on-disk journal superblock and read the key fields into the 146529a511e4SZhang Yi * journal_t. 146629a511e4SZhang Yi */ 146729a511e4SZhang Yi static int load_superblock(journal_t *journal) 146829a511e4SZhang Yi { 146929a511e4SZhang Yi int err; 147029a511e4SZhang Yi journal_superblock_t *sb; 147129a511e4SZhang Yi int num_fc_blocks; 147229a511e4SZhang Yi 147329a511e4SZhang Yi err = journal_get_superblock(journal); 147429a511e4SZhang Yi if (err) 147529a511e4SZhang Yi return err; 147629a511e4SZhang Yi 147729a511e4SZhang Yi sb = journal->j_superblock; 147829a511e4SZhang Yi 147929a511e4SZhang Yi journal->j_tail_sequence = be32_to_cpu(sb->s_sequence); 148029a511e4SZhang Yi journal->j_tail = be32_to_cpu(sb->s_start); 148129a511e4SZhang Yi journal->j_first = be32_to_cpu(sb->s_first); 148229a511e4SZhang Yi journal->j_errno = be32_to_cpu(sb->s_errno); 148329a511e4SZhang Yi journal->j_last = be32_to_cpu(sb->s_maxlen); 148429a511e4SZhang Yi 148529a511e4SZhang Yi if (be32_to_cpu(sb->s_maxlen) < journal->j_total_len) 148629a511e4SZhang Yi journal->j_total_len = be32_to_cpu(sb->s_maxlen); 148729a511e4SZhang Yi /* Precompute checksum seed for all metadata */ 148829a511e4SZhang Yi if (jbd2_journal_has_csum_v2or3(journal)) 148929a511e4SZhang Yi journal->j_csum_seed = jbd2_chksum(journal, ~0, sb->s_uuid, 149029a511e4SZhang Yi sizeof(sb->s_uuid)); 149129a511e4SZhang Yi journal->j_revoke_records_per_block = 149229a511e4SZhang Yi journal_revoke_records_per_block(journal); 149329a511e4SZhang Yi 149429a511e4SZhang Yi if (jbd2_has_feature_fast_commit(journal)) { 149529a511e4SZhang Yi journal->j_fc_last = be32_to_cpu(sb->s_maxlen); 149629a511e4SZhang Yi num_fc_blocks = jbd2_journal_get_num_fc_blks(sb); 149729a511e4SZhang Yi if (journal->j_last - num_fc_blocks >= JBD2_MIN_JOURNAL_BLOCKS) 149829a511e4SZhang Yi journal->j_last = journal->j_fc_last - num_fc_blocks; 149929a511e4SZhang Yi journal->j_fc_first = journal->j_last + 1; 150029a511e4SZhang Yi journal->j_fc_off = 0; 150129a511e4SZhang Yi } 150229a511e4SZhang Yi 150329a511e4SZhang Yi return 0; 150429a511e4SZhang Yi } 150529a511e4SZhang Yi 150629a511e4SZhang Yi 150729a511e4SZhang Yi /* 1508470decc6SDave Kleikamp * Management for journal control blocks: functions to create and 1509470decc6SDave Kleikamp * destroy journal_t structures, and to initialise and read existing 1510470decc6SDave Kleikamp * journal blocks from disk. */ 1511470decc6SDave Kleikamp 1512470decc6SDave Kleikamp /* First: create and setup a journal_t object in memory. We initialise 1513470decc6SDave Kleikamp * very few fields yet: that has to wait until we have created the 1514470decc6SDave Kleikamp * journal structures from from scratch, or loaded them from disk. */ 1515470decc6SDave Kleikamp 1516f0c9fd54SGeliang Tang static journal_t *journal_init_common(struct block_device *bdev, 1517f0c9fd54SGeliang Tang struct block_device *fs_dev, 1518f0c9fd54SGeliang Tang unsigned long long start, int len, int blocksize) 1519470decc6SDave Kleikamp { 1520ab714affSJan Kara static struct lock_class_key jbd2_trans_commit_key; 1521470decc6SDave Kleikamp journal_t *journal; 1522470decc6SDave Kleikamp int err; 1523f0c9fd54SGeliang Tang struct buffer_head *bh; 1524f0c9fd54SGeliang Tang int n; 1525470decc6SDave Kleikamp 15263ebfdf88SAndrew Morton journal = kzalloc(sizeof(*journal), GFP_KERNEL); 1527470decc6SDave Kleikamp if (!journal) 1528b7271b0aSTheodore Ts'o return NULL; 1529470decc6SDave Kleikamp 1530470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_transaction_locked); 1531470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_done_commit); 1532470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_commit); 1533470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_updates); 15348f7d89f3SJan Kara init_waitqueue_head(&journal->j_wait_reserved); 1535ff780b91SHarshad Shirwadkar init_waitqueue_head(&journal->j_fc_wait); 15367b97d868Szhangyi (F) mutex_init(&journal->j_abort_mutex); 1537470decc6SDave Kleikamp mutex_init(&journal->j_barrier); 1538470decc6SDave Kleikamp mutex_init(&journal->j_checkpoint_mutex); 1539470decc6SDave Kleikamp spin_lock_init(&journal->j_revoke_lock); 1540470decc6SDave Kleikamp spin_lock_init(&journal->j_list_lock); 1541a931da6aSTheodore Ts'o rwlock_init(&journal->j_state_lock); 1542470decc6SDave Kleikamp 1543cd02ff0bSMingming Cao journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE); 154430773840STheodore Ts'o journal->j_min_batch_time = 0; 154530773840STheodore Ts'o journal->j_max_batch_time = 15000; /* 15ms */ 15468f7d89f3SJan Kara atomic_set(&journal->j_reserved_credits, 0); 1547470decc6SDave Kleikamp 1548470decc6SDave Kleikamp /* The journal is marked for error until we succeed with recovery! */ 1549f7f4bccbSMingming Cao journal->j_flags = JBD2_ABORT; 1550470decc6SDave Kleikamp 1551470decc6SDave Kleikamp /* Set up a default-sized revoke table for the new mount. */ 1552f7f4bccbSMingming Cao err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH); 1553cd9cb405SEric Biggers if (err) 1554cd9cb405SEric Biggers goto err_cleanup; 15558e85fb3fSJohann Lombardi 1556bf699327STheodore Ts'o spin_lock_init(&journal->j_history_lock); 15578e85fb3fSJohann Lombardi 1558ab714affSJan Kara lockdep_init_map(&journal->j_trans_commit_map, "jbd2_handle", 1559ab714affSJan Kara &jbd2_trans_commit_key, 0); 1560ab714affSJan Kara 1561f0c9fd54SGeliang Tang /* journal descriptor can store up to n blocks -bzzz */ 1562f0c9fd54SGeliang Tang journal->j_blocksize = blocksize; 1563f0c9fd54SGeliang Tang journal->j_dev = bdev; 1564f0c9fd54SGeliang Tang journal->j_fs_dev = fs_dev; 1565f0c9fd54SGeliang Tang journal->j_blk_offset = start; 1566ede7dc7fSHarshad Shirwadkar journal->j_total_len = len; 1567b90bfdf5SJan Kara /* We need enough buffers to write out full descriptor block. */ 1568b90bfdf5SJan Kara n = journal->j_blocksize / jbd2_min_tag_size(); 1569f0c9fd54SGeliang Tang journal->j_wbufsize = n; 1570a1e5e465SHarshad Shirwadkar journal->j_fc_wbuf = NULL; 1571f0c9fd54SGeliang Tang journal->j_wbuf = kmalloc_array(n, sizeof(struct buffer_head *), 1572f0c9fd54SGeliang Tang GFP_KERNEL); 1573cd9cb405SEric Biggers if (!journal->j_wbuf) 1574cd9cb405SEric Biggers goto err_cleanup; 1575f0c9fd54SGeliang Tang 1576f0c9fd54SGeliang Tang bh = getblk_unmovable(journal->j_dev, start, journal->j_blocksize); 1577f0c9fd54SGeliang Tang if (!bh) { 1578f0c9fd54SGeliang Tang pr_err("%s: Cannot get buffer for journal superblock\n", 1579f0c9fd54SGeliang Tang __func__); 1580cd9cb405SEric Biggers goto err_cleanup; 1581f0c9fd54SGeliang Tang } 1582f0c9fd54SGeliang Tang journal->j_sb_buffer = bh; 1583f0c9fd54SGeliang Tang journal->j_superblock = (journal_superblock_t *)bh->b_data; 1584f0c9fd54SGeliang Tang 1585*c3071308SZhang Yi err = load_superblock(journal); 1586*c3071308SZhang Yi if (err) 1587*c3071308SZhang Yi goto err_cleanup; 1588*c3071308SZhang Yi 15890705e8d1STheodore Ts'o journal->j_shrink_transaction = NULL; 15900705e8d1STheodore Ts'o journal->j_shrinker.scan_objects = jbd2_journal_shrink_scan; 15910705e8d1STheodore Ts'o journal->j_shrinker.count_objects = jbd2_journal_shrink_count; 15920705e8d1STheodore Ts'o journal->j_shrinker.seeks = DEFAULT_SEEKS; 15930705e8d1STheodore Ts'o journal->j_shrinker.batch = journal->j_max_transaction_buffers; 15940705e8d1STheodore Ts'o 15950705e8d1STheodore Ts'o if (percpu_counter_init(&journal->j_checkpoint_jh_count, 0, GFP_KERNEL)) 15960705e8d1STheodore Ts'o goto err_cleanup; 15970705e8d1STheodore Ts'o 1598e33c267aSRoman Gushchin if (register_shrinker(&journal->j_shrinker, "jbd2-journal:(%u:%u)", 1599e33c267aSRoman Gushchin MAJOR(bdev->bd_dev), MINOR(bdev->bd_dev))) { 16000705e8d1STheodore Ts'o percpu_counter_destroy(&journal->j_checkpoint_jh_count); 16010705e8d1STheodore Ts'o goto err_cleanup; 16020705e8d1STheodore Ts'o } 1603470decc6SDave Kleikamp return journal; 1604cd9cb405SEric Biggers 1605cd9cb405SEric Biggers err_cleanup: 16060705e8d1STheodore Ts'o brelse(journal->j_sb_buffer); 1607cd9cb405SEric Biggers kfree(journal->j_wbuf); 1608cd9cb405SEric Biggers jbd2_journal_destroy_revoke(journal); 1609cd9cb405SEric Biggers kfree(journal); 1610cd9cb405SEric Biggers return NULL; 1611470decc6SDave Kleikamp } 1612470decc6SDave Kleikamp 1613f7f4bccbSMingming Cao /* jbd2_journal_init_dev and jbd2_journal_init_inode: 1614470decc6SDave Kleikamp * 1615470decc6SDave Kleikamp * Create a journal structure assigned some fixed set of disk blocks to 1616470decc6SDave Kleikamp * the journal. We don't actually touch those disk blocks yet, but we 1617470decc6SDave Kleikamp * need to set up all of the mapping information to tell the journaling 1618470decc6SDave Kleikamp * system where the journal blocks are. 1619470decc6SDave Kleikamp * 1620470decc6SDave Kleikamp */ 1621470decc6SDave Kleikamp 1622470decc6SDave Kleikamp /** 16235648ba5bSRandy Dunlap * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure 1624470decc6SDave Kleikamp * @bdev: Block device on which to create the journal 1625470decc6SDave Kleikamp * @fs_dev: Device which hold journalled filesystem for this journal. 1626470decc6SDave Kleikamp * @start: Block nr Start of journal. 1627470decc6SDave Kleikamp * @len: Length of the journal in blocks. 1628470decc6SDave Kleikamp * @blocksize: blocksize of journalling device 16295648ba5bSRandy Dunlap * 16305648ba5bSRandy Dunlap * Returns: a newly created journal_t * 1631470decc6SDave Kleikamp * 1632f7f4bccbSMingming Cao * jbd2_journal_init_dev creates a journal which maps a fixed contiguous 1633470decc6SDave Kleikamp * range of blocks on an arbitrary block device. 1634470decc6SDave Kleikamp * 1635470decc6SDave Kleikamp */ 1636f7f4bccbSMingming Cao journal_t *jbd2_journal_init_dev(struct block_device *bdev, 1637470decc6SDave Kleikamp struct block_device *fs_dev, 163818eba7aaSMingming Cao unsigned long long start, int len, int blocksize) 1639470decc6SDave Kleikamp { 1640f0c9fd54SGeliang Tang journal_t *journal; 1641470decc6SDave Kleikamp 1642f0c9fd54SGeliang Tang journal = journal_init_common(bdev, fs_dev, start, len, blocksize); 1643470decc6SDave Kleikamp if (!journal) 1644470decc6SDave Kleikamp return NULL; 1645470decc6SDave Kleikamp 1646900d156bSChristoph Hellwig snprintf(journal->j_devname, sizeof(journal->j_devname), 1647900d156bSChristoph Hellwig "%pg", journal->j_dev); 164881ae394bSRasmus Villemoes strreplace(journal->j_devname, '/', '!'); 16494b905671SJan Kara jbd2_stats_proc_init(journal); 16504b905671SJan Kara 1651470decc6SDave Kleikamp return journal; 1652470decc6SDave Kleikamp } 1653470decc6SDave Kleikamp 1654470decc6SDave Kleikamp /** 1655f7f4bccbSMingming Cao * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode. 1656470decc6SDave Kleikamp * @inode: An inode to create the journal in 1657470decc6SDave Kleikamp * 1658f7f4bccbSMingming Cao * jbd2_journal_init_inode creates a journal which maps an on-disk inode as 1659470decc6SDave Kleikamp * the journal. The inode must exist already, must support bmap() and 1660470decc6SDave Kleikamp * must have all data blocks preallocated. 1661470decc6SDave Kleikamp */ 1662f7f4bccbSMingming Cao journal_t *jbd2_journal_init_inode(struct inode *inode) 1663470decc6SDave Kleikamp { 1664f0c9fd54SGeliang Tang journal_t *journal; 166530460e1eSCarlos Maiolino sector_t blocknr; 166630460e1eSCarlos Maiolino int err = 0; 1667470decc6SDave Kleikamp 166830460e1eSCarlos Maiolino blocknr = 0; 166930460e1eSCarlos Maiolino err = bmap(inode, &blocknr); 167030460e1eSCarlos Maiolino 167130460e1eSCarlos Maiolino if (err || !blocknr) { 1672f0c9fd54SGeliang Tang pr_err("%s: Cannot locate journal superblock\n", 1673f0c9fd54SGeliang Tang __func__); 1674f0c9fd54SGeliang Tang return NULL; 1675f0c9fd54SGeliang Tang } 1676f0c9fd54SGeliang Tang 1677cb3b3bf2SJan Kara jbd2_debug(1, "JBD2: inode %s/%ld, size %lld, bits %d, blksize %ld\n", 1678f0c9fd54SGeliang Tang inode->i_sb->s_id, inode->i_ino, (long long) inode->i_size, 1679f0c9fd54SGeliang Tang inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize); 1680f0c9fd54SGeliang Tang 1681f0c9fd54SGeliang Tang journal = journal_init_common(inode->i_sb->s_bdev, inode->i_sb->s_bdev, 1682f0c9fd54SGeliang Tang blocknr, inode->i_size >> inode->i_sb->s_blocksize_bits, 1683f0c9fd54SGeliang Tang inode->i_sb->s_blocksize); 1684470decc6SDave Kleikamp if (!journal) 1685470decc6SDave Kleikamp return NULL; 1686470decc6SDave Kleikamp 1687470decc6SDave Kleikamp journal->j_inode = inode; 1688900d156bSChristoph Hellwig snprintf(journal->j_devname, sizeof(journal->j_devname), 16897afb6d8fSAndy Shevchenko "%pg-%lu", journal->j_dev, journal->j_inode->i_ino); 16907afb6d8fSAndy Shevchenko strreplace(journal->j_devname, '/', '!'); 16918e85fb3fSJohann Lombardi jbd2_stats_proc_init(journal); 1692470decc6SDave Kleikamp 1693470decc6SDave Kleikamp return journal; 1694470decc6SDave Kleikamp } 1695470decc6SDave Kleikamp 1696470decc6SDave Kleikamp /* 1697470decc6SDave Kleikamp * Given a journal_t structure, initialise the various fields for 1698470decc6SDave Kleikamp * startup of a new journaling session. We use this both when creating 1699470decc6SDave Kleikamp * a journal, and after recovering an old journal to reset it for 1700470decc6SDave Kleikamp * subsequent use. 1701470decc6SDave Kleikamp */ 1702470decc6SDave Kleikamp 1703470decc6SDave Kleikamp static int journal_reset(journal_t *journal) 1704470decc6SDave Kleikamp { 1705470decc6SDave Kleikamp journal_superblock_t *sb = journal->j_superblock; 170618eba7aaSMingming Cao unsigned long long first, last; 1707470decc6SDave Kleikamp 1708470decc6SDave Kleikamp first = be32_to_cpu(sb->s_first); 1709470decc6SDave Kleikamp last = be32_to_cpu(sb->s_maxlen); 1710f6f50e28SJan Kara if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) { 1711f2a44523SEryu Guan printk(KERN_ERR "JBD2: Journal too short (blocks %llu-%llu).\n", 1712f6f50e28SJan Kara first, last); 1713f6f50e28SJan Kara journal_fail_superblock(journal); 1714f6f50e28SJan Kara return -EINVAL; 1715f6f50e28SJan Kara } 1716470decc6SDave Kleikamp 1717470decc6SDave Kleikamp journal->j_first = first; 17186866d7b3SHarshad Shirwadkar journal->j_last = last; 17196866d7b3SHarshad Shirwadkar 1720c7fc6055SZhang Yi if (journal->j_head != 0 && journal->j_flags & JBD2_CYCLE_RECORD) { 1721c7fc6055SZhang Yi /* 1722c7fc6055SZhang Yi * Disable the cycled recording mode if the journal head block 1723c7fc6055SZhang Yi * number is not correct. 1724c7fc6055SZhang Yi */ 1725c7fc6055SZhang Yi if (journal->j_head < first || journal->j_head >= last) { 1726c7fc6055SZhang Yi printk(KERN_WARNING "JBD2: Incorrect Journal head block %lu, " 1727c7fc6055SZhang Yi "disable journal_cycle_record\n", 1728c7fc6055SZhang Yi journal->j_head); 17296866d7b3SHarshad Shirwadkar journal->j_head = journal->j_first; 1730c7fc6055SZhang Yi } 1731c7fc6055SZhang Yi } else { 1732c7fc6055SZhang Yi journal->j_head = journal->j_first; 1733c7fc6055SZhang Yi } 1734c7fc6055SZhang Yi journal->j_tail = journal->j_head; 17356866d7b3SHarshad Shirwadkar journal->j_free = journal->j_last - journal->j_first; 1736470decc6SDave Kleikamp 1737470decc6SDave Kleikamp journal->j_tail_sequence = journal->j_transaction_sequence; 1738470decc6SDave Kleikamp journal->j_commit_sequence = journal->j_transaction_sequence - 1; 1739470decc6SDave Kleikamp journal->j_commit_request = journal->j_commit_sequence; 1740470decc6SDave Kleikamp 1741ede7dc7fSHarshad Shirwadkar journal->j_max_transaction_buffers = jbd2_journal_get_max_txn_bufs(journal); 1742470decc6SDave Kleikamp 1743470decc6SDave Kleikamp /* 1744a1e5e465SHarshad Shirwadkar * Now that journal recovery is done, turn fast commits off here. This 1745a1e5e465SHarshad Shirwadkar * way, if fast commit was enabled before the crash but if now FS has 1746a1e5e465SHarshad Shirwadkar * disabled it, we don't enable fast commits. 1747a1e5e465SHarshad Shirwadkar */ 1748a1e5e465SHarshad Shirwadkar jbd2_clear_feature_fast_commit(journal); 1749a1e5e465SHarshad Shirwadkar 1750a1e5e465SHarshad Shirwadkar /* 1751470decc6SDave Kleikamp * As a special case, if the on-disk copy is already marked as needing 175224bcc89cSJan Kara * no recovery (s_start == 0), then we can safely defer the superblock 175324bcc89cSJan Kara * update until the next commit by setting JBD2_FLUSHED. This avoids 1754470decc6SDave Kleikamp * attempting a write to a potential-readonly device. 1755470decc6SDave Kleikamp */ 175624bcc89cSJan Kara if (sb->s_start == 0) { 1757cb3b3bf2SJan Kara jbd2_debug(1, "JBD2: Skipping superblock update on recovered sb " 17587821ce41SGaowei Pu "(start %ld, seq %u, errno %d)\n", 1759470decc6SDave Kleikamp journal->j_tail, journal->j_tail_sequence, 1760470decc6SDave Kleikamp journal->j_errno); 176124bcc89cSJan Kara journal->j_flags |= JBD2_FLUSHED; 176224bcc89cSJan Kara } else { 1763a78bb11dSJan Kara /* Lock here to make assertions happy... */ 17646fa7aa50STejun Heo mutex_lock_io(&journal->j_checkpoint_mutex); 176579feb521SJan Kara /* 176670fd7614SChristoph Hellwig * Update log tail information. We use REQ_FUA since new 176779feb521SJan Kara * transaction will start reusing journal space and so we 176879feb521SJan Kara * must make sure information about current log tail is on 176979feb521SJan Kara * disk before that. 177079feb521SJan Kara */ 177179feb521SJan Kara jbd2_journal_update_sb_log_tail(journal, 177279feb521SJan Kara journal->j_tail_sequence, 177379feb521SJan Kara journal->j_tail, 177417f423b5SJan Kara REQ_SYNC | REQ_FUA); 1775a78bb11dSJan Kara mutex_unlock(&journal->j_checkpoint_mutex); 177624bcc89cSJan Kara } 177724bcc89cSJan Kara return jbd2_journal_start_thread(journal); 1778470decc6SDave Kleikamp } 1779470decc6SDave Kleikamp 1780538bcaa6STheodore Ts'o /* 1781538bcaa6STheodore Ts'o * This function expects that the caller will have locked the journal 1782538bcaa6STheodore Ts'o * buffer head, and will return with it unlocked 1783538bcaa6STheodore Ts'o */ 17846669797bSBart Van Assche static int jbd2_write_superblock(journal_t *journal, blk_opf_t write_flags) 178524bcc89cSJan Kara { 178624bcc89cSJan Kara struct buffer_head *bh = journal->j_sb_buffer; 1787fe52d17cSTheodore Ts'o journal_superblock_t *sb = journal->j_superblock; 1788f3ed5df3SRitesh Harjani (IBM) int ret = 0; 178924bcc89cSJan Kara 1790742b06b5SJiufei Xue /* Buffer got discarded which means block device got invalidated */ 1791ef3f5830Szhangyi (F) if (!buffer_mapped(bh)) { 1792ef3f5830Szhangyi (F) unlock_buffer(bh); 1793742b06b5SJiufei Xue return -EIO; 1794ef3f5830Szhangyi (F) } 1795742b06b5SJiufei Xue 17962a222ca9SMike Christie trace_jbd2_write_superblock(journal, write_flags); 179779feb521SJan Kara if (!(journal->j_flags & JBD2_BARRIER)) 179828a8f0d3SMike Christie write_flags &= ~(REQ_FUA | REQ_PREFLUSH); 1799914258bfSTheodore Ts'o if (buffer_write_io_error(bh)) { 1800914258bfSTheodore Ts'o /* 1801914258bfSTheodore Ts'o * Oh, dear. A previous attempt to write the journal 1802914258bfSTheodore Ts'o * superblock failed. This could happen because the 1803914258bfSTheodore Ts'o * USB device was yanked out. Or it could happen to 1804914258bfSTheodore Ts'o * be a transient write error and maybe the block will 1805914258bfSTheodore Ts'o * be remapped. Nothing we can do but to retry the 1806914258bfSTheodore Ts'o * write and hope for the best. 1807914258bfSTheodore Ts'o */ 1808914258bfSTheodore Ts'o printk(KERN_ERR "JBD2: previous I/O error detected " 1809914258bfSTheodore Ts'o "for journal superblock update for %s.\n", 1810914258bfSTheodore Ts'o journal->j_devname); 1811914258bfSTheodore Ts'o clear_buffer_write_io_error(bh); 1812914258bfSTheodore Ts'o set_buffer_uptodate(bh); 1813914258bfSTheodore Ts'o } 1814a58ca992STheodore Ts'o if (jbd2_journal_has_csum_v2or3(journal)) 1815a58ca992STheodore Ts'o sb->s_checksum = jbd2_superblock_csum(journal, sb); 181679feb521SJan Kara get_bh(bh); 181779feb521SJan Kara bh->b_end_io = end_buffer_write_sync; 1818f3ed5df3SRitesh Harjani (IBM) submit_bh(REQ_OP_WRITE | write_flags, bh); 181979feb521SJan Kara wait_on_buffer(bh); 1820914258bfSTheodore Ts'o if (buffer_write_io_error(bh)) { 1821914258bfSTheodore Ts'o clear_buffer_write_io_error(bh); 1822914258bfSTheodore Ts'o set_buffer_uptodate(bh); 182379feb521SJan Kara ret = -EIO; 182479feb521SJan Kara } 182579feb521SJan Kara if (ret) { 1826f3ed5df3SRitesh Harjani (IBM) printk(KERN_ERR "JBD2: I/O error when updating journal superblock for %s.\n", 182779feb521SJan Kara journal->j_devname); 18287b97d868Szhangyi (F) if (!is_journal_aborted(journal)) 18296f6a6fdaSJoseph Qi jbd2_journal_abort(journal, ret); 1830914258bfSTheodore Ts'o } 18316f6a6fdaSJoseph Qi 18326f6a6fdaSJoseph Qi return ret; 183324bcc89cSJan Kara } 1834470decc6SDave Kleikamp 183524bcc89cSJan Kara /** 183624bcc89cSJan Kara * jbd2_journal_update_sb_log_tail() - Update log tail in journal sb on disk. 183724bcc89cSJan Kara * @journal: The journal to update. 183879feb521SJan Kara * @tail_tid: TID of the new transaction at the tail of the log 183979feb521SJan Kara * @tail_block: The first block of the transaction at the tail of the log 18406669797bSBart Van Assche * @write_flags: Flags for the journal sb write operation 184124bcc89cSJan Kara * 184224bcc89cSJan Kara * Update a journal's superblock information about log tail and write it to 184324bcc89cSJan Kara * disk, waiting for the IO to complete. 184424bcc89cSJan Kara */ 18456f6a6fdaSJoseph Qi int jbd2_journal_update_sb_log_tail(journal_t *journal, tid_t tail_tid, 18466669797bSBart Van Assche unsigned long tail_block, 18476669797bSBart Van Assche blk_opf_t write_flags) 184824bcc89cSJan Kara { 184924bcc89cSJan Kara journal_superblock_t *sb = journal->j_superblock; 18506f6a6fdaSJoseph Qi int ret; 18512201c590SSeiji Aguchi 185285e0c4e8STheodore Ts'o if (is_journal_aborted(journal)) 185385e0c4e8STheodore Ts'o return -EIO; 1854fcf37549SZhang Yi if (test_bit(JBD2_CHECKPOINT_IO_ERROR, &journal->j_atomic_flags)) { 1855fcf37549SZhang Yi jbd2_journal_abort(journal, -EIO); 1856fcf37549SZhang Yi return -EIO; 1857fcf37549SZhang Yi } 185885e0c4e8STheodore Ts'o 1859a78bb11dSJan Kara BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex)); 1860cb3b3bf2SJan Kara jbd2_debug(1, "JBD2: updating superblock (start %lu, seq %u)\n", 186179feb521SJan Kara tail_block, tail_tid); 1862470decc6SDave Kleikamp 1863538bcaa6STheodore Ts'o lock_buffer(journal->j_sb_buffer); 186479feb521SJan Kara sb->s_sequence = cpu_to_be32(tail_tid); 186579feb521SJan Kara sb->s_start = cpu_to_be32(tail_block); 186624bcc89cSJan Kara 18676669797bSBart Van Assche ret = jbd2_write_superblock(journal, write_flags); 18686f6a6fdaSJoseph Qi if (ret) 18696f6a6fdaSJoseph Qi goto out; 187024bcc89cSJan Kara 187124bcc89cSJan Kara /* Log is no longer empty */ 1872a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 187324bcc89cSJan Kara WARN_ON(!sb->s_sequence); 1874f7f4bccbSMingming Cao journal->j_flags &= ~JBD2_FLUSHED; 187524bcc89cSJan Kara write_unlock(&journal->j_state_lock); 18766f6a6fdaSJoseph Qi 18776f6a6fdaSJoseph Qi out: 18786f6a6fdaSJoseph Qi return ret; 187924bcc89cSJan Kara } 188024bcc89cSJan Kara 188124bcc89cSJan Kara /** 188224bcc89cSJan Kara * jbd2_mark_journal_empty() - Mark on disk journal as empty. 188324bcc89cSJan Kara * @journal: The journal to update. 18846669797bSBart Van Assche * @write_flags: Flags for the journal sb write operation 188524bcc89cSJan Kara * 188624bcc89cSJan Kara * Update a journal's dynamic superblock fields to show that journal is empty. 188724bcc89cSJan Kara * Write updated superblock to disk waiting for IO to complete. 188824bcc89cSJan Kara */ 18896669797bSBart Van Assche static void jbd2_mark_journal_empty(journal_t *journal, blk_opf_t write_flags) 189024bcc89cSJan Kara { 189124bcc89cSJan Kara journal_superblock_t *sb = journal->j_superblock; 1892ff780b91SHarshad Shirwadkar bool had_fast_commit = false; 189324bcc89cSJan Kara 1894a78bb11dSJan Kara BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex)); 1895538bcaa6STheodore Ts'o lock_buffer(journal->j_sb_buffer); 1896538bcaa6STheodore Ts'o if (sb->s_start == 0) { /* Is it already empty? */ 1897538bcaa6STheodore Ts'o unlock_buffer(journal->j_sb_buffer); 1898eeecef0aSEric Sandeen return; 1899eeecef0aSEric Sandeen } 1900538bcaa6STheodore Ts'o 1901cb3b3bf2SJan Kara jbd2_debug(1, "JBD2: Marking journal as empty (seq %u)\n", 190224bcc89cSJan Kara journal->j_tail_sequence); 1903470decc6SDave Kleikamp 1904470decc6SDave Kleikamp sb->s_sequence = cpu_to_be32(journal->j_tail_sequence); 190524bcc89cSJan Kara sb->s_start = cpu_to_be32(0); 1906c7fc6055SZhang Yi sb->s_head = cpu_to_be32(journal->j_head); 1907ff780b91SHarshad Shirwadkar if (jbd2_has_feature_fast_commit(journal)) { 1908ff780b91SHarshad Shirwadkar /* 1909ff780b91SHarshad Shirwadkar * When journal is clean, no need to commit fast commit flag and 1910ff780b91SHarshad Shirwadkar * make file system incompatible with older kernels. 1911ff780b91SHarshad Shirwadkar */ 1912ff780b91SHarshad Shirwadkar jbd2_clear_feature_fast_commit(journal); 1913ff780b91SHarshad Shirwadkar had_fast_commit = true; 1914ff780b91SHarshad Shirwadkar } 191524bcc89cSJan Kara 19166669797bSBart Van Assche jbd2_write_superblock(journal, write_flags); 191724bcc89cSJan Kara 1918ff780b91SHarshad Shirwadkar if (had_fast_commit) 1919ff780b91SHarshad Shirwadkar jbd2_set_feature_fast_commit(journal); 1920ff780b91SHarshad Shirwadkar 192124bcc89cSJan Kara /* Log is no longer empty */ 192224bcc89cSJan Kara write_lock(&journal->j_state_lock); 1923f7f4bccbSMingming Cao journal->j_flags |= JBD2_FLUSHED; 1924a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 1925470decc6SDave Kleikamp } 1926470decc6SDave Kleikamp 192701d5d965SLeah Rumancik /** 192801d5d965SLeah Rumancik * __jbd2_journal_erase() - Discard or zeroout journal blocks (excluding superblock) 192901d5d965SLeah Rumancik * @journal: The journal to erase. 193001d5d965SLeah Rumancik * @flags: A discard/zeroout request is sent for each physically contigous 193101d5d965SLeah Rumancik * region of the journal. Either JBD2_JOURNAL_FLUSH_DISCARD or 193201d5d965SLeah Rumancik * JBD2_JOURNAL_FLUSH_ZEROOUT must be set to determine which operation 193301d5d965SLeah Rumancik * to perform. 193401d5d965SLeah Rumancik * 193501d5d965SLeah Rumancik * Note: JBD2_JOURNAL_FLUSH_ZEROOUT attempts to use hardware offload. Zeroes 193601d5d965SLeah Rumancik * will be explicitly written if no hardware offload is available, see 193701d5d965SLeah Rumancik * blkdev_issue_zeroout for more details. 193801d5d965SLeah Rumancik */ 193901d5d965SLeah Rumancik static int __jbd2_journal_erase(journal_t *journal, unsigned int flags) 194001d5d965SLeah Rumancik { 194101d5d965SLeah Rumancik int err = 0; 194201d5d965SLeah Rumancik unsigned long block, log_offset; /* logical */ 194301d5d965SLeah Rumancik unsigned long long phys_block, block_start, block_stop; /* physical */ 194401d5d965SLeah Rumancik loff_t byte_start, byte_stop, byte_count; 194501d5d965SLeah Rumancik 194601d5d965SLeah Rumancik /* flags must be set to either discard or zeroout */ 194701d5d965SLeah Rumancik if ((flags & ~JBD2_JOURNAL_FLUSH_VALID) || !flags || 194801d5d965SLeah Rumancik ((flags & JBD2_JOURNAL_FLUSH_DISCARD) && 194901d5d965SLeah Rumancik (flags & JBD2_JOURNAL_FLUSH_ZEROOUT))) 195001d5d965SLeah Rumancik return -EINVAL; 195101d5d965SLeah Rumancik 195270200574SChristoph Hellwig if ((flags & JBD2_JOURNAL_FLUSH_DISCARD) && 195370200574SChristoph Hellwig !bdev_max_discard_sectors(journal->j_dev)) 195401d5d965SLeah Rumancik return -EOPNOTSUPP; 195501d5d965SLeah Rumancik 195601d5d965SLeah Rumancik /* 195701d5d965SLeah Rumancik * lookup block mapping and issue discard/zeroout for each 195801d5d965SLeah Rumancik * contiguous region 195901d5d965SLeah Rumancik */ 196001d5d965SLeah Rumancik log_offset = be32_to_cpu(journal->j_superblock->s_first); 196101d5d965SLeah Rumancik block_start = ~0ULL; 196201d5d965SLeah Rumancik for (block = log_offset; block < journal->j_total_len; block++) { 196301d5d965SLeah Rumancik err = jbd2_journal_bmap(journal, block, &phys_block); 196401d5d965SLeah Rumancik if (err) { 196501d5d965SLeah Rumancik pr_err("JBD2: bad block at offset %lu", block); 196601d5d965SLeah Rumancik return err; 196701d5d965SLeah Rumancik } 196801d5d965SLeah Rumancik 196901d5d965SLeah Rumancik if (block_start == ~0ULL) { 197001d5d965SLeah Rumancik block_start = phys_block; 197101d5d965SLeah Rumancik block_stop = block_start - 1; 197201d5d965SLeah Rumancik } 197301d5d965SLeah Rumancik 197401d5d965SLeah Rumancik /* 197501d5d965SLeah Rumancik * last block not contiguous with current block, 197601d5d965SLeah Rumancik * process last contiguous region and return to this block on 197701d5d965SLeah Rumancik * next loop 197801d5d965SLeah Rumancik */ 197901d5d965SLeah Rumancik if (phys_block != block_stop + 1) { 198001d5d965SLeah Rumancik block--; 198101d5d965SLeah Rumancik } else { 198201d5d965SLeah Rumancik block_stop++; 198301d5d965SLeah Rumancik /* 198401d5d965SLeah Rumancik * if this isn't the last block of journal, 198501d5d965SLeah Rumancik * no need to process now because next block may also 198601d5d965SLeah Rumancik * be part of this contiguous region 198701d5d965SLeah Rumancik */ 198801d5d965SLeah Rumancik if (block != journal->j_total_len - 1) 198901d5d965SLeah Rumancik continue; 199001d5d965SLeah Rumancik } 199101d5d965SLeah Rumancik 199201d5d965SLeah Rumancik /* 199301d5d965SLeah Rumancik * end of contiguous region or this is last block of journal, 199401d5d965SLeah Rumancik * take care of the region 199501d5d965SLeah Rumancik */ 199601d5d965SLeah Rumancik byte_start = block_start * journal->j_blocksize; 199701d5d965SLeah Rumancik byte_stop = block_stop * journal->j_blocksize; 199801d5d965SLeah Rumancik byte_count = (block_stop - block_start + 1) * 199901d5d965SLeah Rumancik journal->j_blocksize; 200001d5d965SLeah Rumancik 200101d5d965SLeah Rumancik truncate_inode_pages_range(journal->j_dev->bd_inode->i_mapping, 200201d5d965SLeah Rumancik byte_start, byte_stop); 200301d5d965SLeah Rumancik 200401d5d965SLeah Rumancik if (flags & JBD2_JOURNAL_FLUSH_DISCARD) { 200501d5d965SLeah Rumancik err = blkdev_issue_discard(journal->j_dev, 200601d5d965SLeah Rumancik byte_start >> SECTOR_SHIFT, 200701d5d965SLeah Rumancik byte_count >> SECTOR_SHIFT, 200844abff2cSChristoph Hellwig GFP_NOFS); 200901d5d965SLeah Rumancik } else if (flags & JBD2_JOURNAL_FLUSH_ZEROOUT) { 201001d5d965SLeah Rumancik err = blkdev_issue_zeroout(journal->j_dev, 201101d5d965SLeah Rumancik byte_start >> SECTOR_SHIFT, 201201d5d965SLeah Rumancik byte_count >> SECTOR_SHIFT, 201301d5d965SLeah Rumancik GFP_NOFS, 0); 201401d5d965SLeah Rumancik } 201501d5d965SLeah Rumancik 201601d5d965SLeah Rumancik if (unlikely(err != 0)) { 201701d5d965SLeah Rumancik pr_err("JBD2: (error %d) unable to wipe journal at physical blocks %llu - %llu", 201801d5d965SLeah Rumancik err, block_start, block_stop); 201901d5d965SLeah Rumancik return err; 202001d5d965SLeah Rumancik } 202101d5d965SLeah Rumancik 202201d5d965SLeah Rumancik /* reset start and stop after processing a region */ 202301d5d965SLeah Rumancik block_start = ~0ULL; 202401d5d965SLeah Rumancik } 202501d5d965SLeah Rumancik 202601d5d965SLeah Rumancik return blkdev_issue_flush(journal->j_dev); 202701d5d965SLeah Rumancik } 202824bcc89cSJan Kara 202924bcc89cSJan Kara /** 203024bcc89cSJan Kara * jbd2_journal_update_sb_errno() - Update error in the journal. 203124bcc89cSJan Kara * @journal: The journal to update. 203224bcc89cSJan Kara * 203324bcc89cSJan Kara * Update a journal's errno. Write updated superblock to disk waiting for IO 203424bcc89cSJan Kara * to complete. 203524bcc89cSJan Kara */ 2036d796c52eSTheodore Ts'o void jbd2_journal_update_sb_errno(journal_t *journal) 203724bcc89cSJan Kara { 203824bcc89cSJan Kara journal_superblock_t *sb = journal->j_superblock; 2039fb7c0244STheodore Ts'o int errcode; 204024bcc89cSJan Kara 2041538bcaa6STheodore Ts'o lock_buffer(journal->j_sb_buffer); 2042fb7c0244STheodore Ts'o errcode = journal->j_errno; 2043fb7c0244STheodore Ts'o if (errcode == -ESHUTDOWN) 2044fb7c0244STheodore Ts'o errcode = 0; 2045cb3b3bf2SJan Kara jbd2_debug(1, "JBD2: updating superblock error (errno %d)\n", errcode); 2046fb7c0244STheodore Ts'o sb->s_errno = cpu_to_be32(errcode); 2047470decc6SDave Kleikamp 204817f423b5SJan Kara jbd2_write_superblock(journal, REQ_SYNC | REQ_FUA); 2049470decc6SDave Kleikamp } 2050d796c52eSTheodore Ts'o EXPORT_SYMBOL(jbd2_journal_update_sb_errno); 2051470decc6SDave Kleikamp 2052470decc6SDave Kleikamp /** 20532bf31d94SMauro Carvalho Chehab * jbd2_journal_load() - Read journal from disk. 2054470decc6SDave Kleikamp * @journal: Journal to act on. 2055470decc6SDave Kleikamp * 2056470decc6SDave Kleikamp * Given a journal_t structure which tells us which disk blocks contain 2057470decc6SDave Kleikamp * a journal, read the journal from disk to initialise the in-memory 2058470decc6SDave Kleikamp * structures. 2059470decc6SDave Kleikamp */ 2060f7f4bccbSMingming Cao int jbd2_journal_load(journal_t *journal) 2061470decc6SDave Kleikamp { 2062470decc6SDave Kleikamp int err; 2063*c3071308SZhang Yi journal_superblock_t *sb = journal->j_superblock; 2064470decc6SDave Kleikamp 20655cf036d4SZhang Yi /* 20665cf036d4SZhang Yi * If this is a V2 superblock, then we have to check the 20675cf036d4SZhang Yi * features flags on it. 20685cf036d4SZhang Yi */ 20695cf036d4SZhang Yi if (jbd2_format_support_feature(journal)) { 2070470decc6SDave Kleikamp if ((sb->s_feature_ro_compat & 2071f7f4bccbSMingming Cao ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) || 2072470decc6SDave Kleikamp (sb->s_feature_incompat & 2073f7f4bccbSMingming Cao ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) { 2074470decc6SDave Kleikamp printk(KERN_WARNING 2075f2a44523SEryu Guan "JBD2: Unrecognised features on journal\n"); 2076470decc6SDave Kleikamp return -EINVAL; 2077470decc6SDave Kleikamp } 2078470decc6SDave Kleikamp } 2079470decc6SDave Kleikamp 2080d2eecb03STheodore Ts'o /* 2081d2eecb03STheodore Ts'o * Create a slab for this blocksize 2082d2eecb03STheodore Ts'o */ 2083d2eecb03STheodore Ts'o err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize)); 2084d2eecb03STheodore Ts'o if (err) 2085d2eecb03STheodore Ts'o return err; 2086d2eecb03STheodore Ts'o 2087470decc6SDave Kleikamp /* Let the recovery code check whether it needs to recover any 2088470decc6SDave Kleikamp * data from the journal. */ 20891d401650SGuoqing Cai err = jbd2_journal_recover(journal); 20901d401650SGuoqing Cai if (err) { 20911d401650SGuoqing Cai pr_warn("JBD2: journal recovery failed\n"); 20921d401650SGuoqing Cai return err; 20931d401650SGuoqing Cai } 2094470decc6SDave Kleikamp 2095e6a47428STheodore Ts'o if (journal->j_failed_commit) { 2096e6a47428STheodore Ts'o printk(KERN_ERR "JBD2: journal transaction %u on %s " 2097e6a47428STheodore Ts'o "is corrupt.\n", journal->j_failed_commit, 2098e6a47428STheodore Ts'o journal->j_devname); 20996a797d27SDarrick J. Wong return -EFSCORRUPTED; 2100e6a47428STheodore Ts'o } 2101a09decffSKai Li /* 2102a09decffSKai Li * clear JBD2_ABORT flag initialized in journal_init_common 2103a09decffSKai Li * here to update log tail information with the newest seq. 2104a09decffSKai Li */ 2105a09decffSKai Li journal->j_flags &= ~JBD2_ABORT; 2106e6a47428STheodore Ts'o 2107470decc6SDave Kleikamp /* OK, we've finished with the dynamic journal bits: 2108470decc6SDave Kleikamp * reinitialise the dynamic contents of the superblock in memory 2109470decc6SDave Kleikamp * and reset them on disk. */ 21101d401650SGuoqing Cai err = journal_reset(journal); 21111d401650SGuoqing Cai if (err) { 21121d401650SGuoqing Cai pr_warn("JBD2: journal reset failed\n"); 21131d401650SGuoqing Cai return err; 21141d401650SGuoqing Cai } 2115470decc6SDave Kleikamp 2116f7f4bccbSMingming Cao journal->j_flags |= JBD2_LOADED; 2117470decc6SDave Kleikamp return 0; 2118470decc6SDave Kleikamp } 2119470decc6SDave Kleikamp 2120470decc6SDave Kleikamp /** 21212bf31d94SMauro Carvalho Chehab * jbd2_journal_destroy() - Release a journal_t structure. 2122470decc6SDave Kleikamp * @journal: Journal to act on. 2123470decc6SDave Kleikamp * 2124470decc6SDave Kleikamp * Release a journal_t structure once it is no longer in use by the 2125470decc6SDave Kleikamp * journaled object. 212644519fafSHidehiro Kawai * Return <0 if we couldn't clean up the journal. 2127470decc6SDave Kleikamp */ 212844519fafSHidehiro Kawai int jbd2_journal_destroy(journal_t *journal) 2129470decc6SDave Kleikamp { 213044519fafSHidehiro Kawai int err = 0; 213144519fafSHidehiro Kawai 2132470decc6SDave Kleikamp /* Wait for the commit thread to wake up and die. */ 2133470decc6SDave Kleikamp journal_kill_thread(journal); 2134470decc6SDave Kleikamp 2135470decc6SDave Kleikamp /* Force a final log commit */ 2136470decc6SDave Kleikamp if (journal->j_running_transaction) 2137f7f4bccbSMingming Cao jbd2_journal_commit_transaction(journal); 2138470decc6SDave Kleikamp 2139470decc6SDave Kleikamp /* Force any old transactions to disk */ 2140470decc6SDave Kleikamp 2141470decc6SDave Kleikamp /* Totally anal locking here... */ 2142470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 2143470decc6SDave Kleikamp while (journal->j_checkpoint_transactions != NULL) { 2144470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 21456fa7aa50STejun Heo mutex_lock_io(&journal->j_checkpoint_mutex); 2146841df7dfSJan Kara err = jbd2_log_do_checkpoint(journal); 21471a0d3786STheodore Ts'o mutex_unlock(&journal->j_checkpoint_mutex); 2148841df7dfSJan Kara /* 2149841df7dfSJan Kara * If checkpointing failed, just free the buffers to avoid 2150841df7dfSJan Kara * looping forever 2151841df7dfSJan Kara */ 2152841df7dfSJan Kara if (err) { 2153841df7dfSJan Kara jbd2_journal_destroy_checkpoint(journal); 2154841df7dfSJan Kara spin_lock(&journal->j_list_lock); 2155841df7dfSJan Kara break; 2156841df7dfSJan Kara } 2157470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 2158470decc6SDave Kleikamp } 2159470decc6SDave Kleikamp 2160470decc6SDave Kleikamp J_ASSERT(journal->j_running_transaction == NULL); 2161470decc6SDave Kleikamp J_ASSERT(journal->j_committing_transaction == NULL); 2162470decc6SDave Kleikamp J_ASSERT(journal->j_checkpoint_transactions == NULL); 2163470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 2164470decc6SDave Kleikamp 2165fcf37549SZhang Yi /* 2166fcf37549SZhang Yi * OK, all checkpoint transactions have been checked, now check the 2167fcf37549SZhang Yi * write out io error flag and abort the journal if some buffer failed 2168fcf37549SZhang Yi * to write back to the original location, otherwise the filesystem 2169fcf37549SZhang Yi * may become inconsistent. 2170fcf37549SZhang Yi */ 2171fcf37549SZhang Yi if (!is_journal_aborted(journal) && 2172fcf37549SZhang Yi test_bit(JBD2_CHECKPOINT_IO_ERROR, &journal->j_atomic_flags)) 2173fcf37549SZhang Yi jbd2_journal_abort(journal, -EIO); 2174fcf37549SZhang Yi 217544519fafSHidehiro Kawai if (journal->j_sb_buffer) { 217644519fafSHidehiro Kawai if (!is_journal_aborted(journal)) { 21776fa7aa50STejun Heo mutex_lock_io(&journal->j_checkpoint_mutex); 2178c0a2ad9bSOGAWA Hirofumi 2179c0a2ad9bSOGAWA Hirofumi write_lock(&journal->j_state_lock); 2180c0a2ad9bSOGAWA Hirofumi journal->j_tail_sequence = 2181c0a2ad9bSOGAWA Hirofumi ++journal->j_transaction_sequence; 2182c0a2ad9bSOGAWA Hirofumi write_unlock(&journal->j_state_lock); 2183c0a2ad9bSOGAWA Hirofumi 218470fd7614SChristoph Hellwig jbd2_mark_journal_empty(journal, 218517f423b5SJan Kara REQ_SYNC | REQ_PREFLUSH | REQ_FUA); 2186a78bb11dSJan Kara mutex_unlock(&journal->j_checkpoint_mutex); 2187a78bb11dSJan Kara } else 218844519fafSHidehiro Kawai err = -EIO; 2189470decc6SDave Kleikamp brelse(journal->j_sb_buffer); 2190470decc6SDave Kleikamp } 2191470decc6SDave Kleikamp 21920705e8d1STheodore Ts'o if (journal->j_shrinker.flags & SHRINKER_REGISTERED) { 21930705e8d1STheodore Ts'o percpu_counter_destroy(&journal->j_checkpoint_jh_count); 21940705e8d1STheodore Ts'o unregister_shrinker(&journal->j_shrinker); 21950705e8d1STheodore Ts'o } 21968e85fb3fSJohann Lombardi if (journal->j_proc_entry) 21978e85fb3fSJohann Lombardi jbd2_stats_proc_exit(journal); 2198470decc6SDave Kleikamp iput(journal->j_inode); 2199470decc6SDave Kleikamp if (journal->j_revoke) 2200f7f4bccbSMingming Cao jbd2_journal_destroy_revoke(journal); 220101b5adceSDarrick J. Wong if (journal->j_chksum_driver) 220201b5adceSDarrick J. Wong crypto_free_shash(journal->j_chksum_driver); 22036866d7b3SHarshad Shirwadkar kfree(journal->j_fc_wbuf); 2204470decc6SDave Kleikamp kfree(journal->j_wbuf); 2205470decc6SDave Kleikamp kfree(journal); 220644519fafSHidehiro Kawai 220744519fafSHidehiro Kawai return err; 2208470decc6SDave Kleikamp } 2209470decc6SDave Kleikamp 2210470decc6SDave Kleikamp 2211470decc6SDave Kleikamp /** 22122bf31d94SMauro Carvalho Chehab * jbd2_journal_check_used_features() - Check if features specified are used. 2213470decc6SDave Kleikamp * @journal: Journal to check. 2214470decc6SDave Kleikamp * @compat: bitmask of compatible features 2215470decc6SDave Kleikamp * @ro: bitmask of features that force read-only mount 2216470decc6SDave Kleikamp * @incompat: bitmask of incompatible features 2217470decc6SDave Kleikamp * 2218470decc6SDave Kleikamp * Check whether the journal uses all of a given set of 2219470decc6SDave Kleikamp * features. Return true (non-zero) if it does. 2220470decc6SDave Kleikamp **/ 2221470decc6SDave Kleikamp 2222f7f4bccbSMingming Cao int jbd2_journal_check_used_features(journal_t *journal, unsigned long compat, 2223470decc6SDave Kleikamp unsigned long ro, unsigned long incompat) 2224470decc6SDave Kleikamp { 2225470decc6SDave Kleikamp journal_superblock_t *sb; 2226470decc6SDave Kleikamp 2227470decc6SDave Kleikamp if (!compat && !ro && !incompat) 2228470decc6SDave Kleikamp return 1; 22293e5cf02cSZhang Yi if (journal_get_superblock(journal)) 22301113e1b5SPatrick J. LoPresti return 0; 22315cf036d4SZhang Yi if (!jbd2_format_support_feature(journal)) 2232470decc6SDave Kleikamp return 0; 2233470decc6SDave Kleikamp 2234470decc6SDave Kleikamp sb = journal->j_superblock; 2235470decc6SDave Kleikamp 2236470decc6SDave Kleikamp if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) && 2237470decc6SDave Kleikamp ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) && 2238470decc6SDave Kleikamp ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat)) 2239470decc6SDave Kleikamp return 1; 2240470decc6SDave Kleikamp 2241470decc6SDave Kleikamp return 0; 2242470decc6SDave Kleikamp } 2243470decc6SDave Kleikamp 2244470decc6SDave Kleikamp /** 22452bf31d94SMauro Carvalho Chehab * jbd2_journal_check_available_features() - Check feature set in journalling layer 2246470decc6SDave Kleikamp * @journal: Journal to check. 2247470decc6SDave Kleikamp * @compat: bitmask of compatible features 2248470decc6SDave Kleikamp * @ro: bitmask of features that force read-only mount 2249470decc6SDave Kleikamp * @incompat: bitmask of incompatible features 2250470decc6SDave Kleikamp * 2251470decc6SDave Kleikamp * Check whether the journaling code supports the use of 2252470decc6SDave Kleikamp * all of a given set of features on this journal. Return true 2253470decc6SDave Kleikamp * (non-zero) if it can. */ 2254470decc6SDave Kleikamp 2255f7f4bccbSMingming Cao int jbd2_journal_check_available_features(journal_t *journal, unsigned long compat, 2256470decc6SDave Kleikamp unsigned long ro, unsigned long incompat) 2257470decc6SDave Kleikamp { 2258470decc6SDave Kleikamp if (!compat && !ro && !incompat) 2259470decc6SDave Kleikamp return 1; 2260470decc6SDave Kleikamp 22615cf036d4SZhang Yi if (!jbd2_format_support_feature(journal)) 2262470decc6SDave Kleikamp return 0; 2263470decc6SDave Kleikamp 2264f7f4bccbSMingming Cao if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat && 2265f7f4bccbSMingming Cao (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro && 2266f7f4bccbSMingming Cao (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat) 2267470decc6SDave Kleikamp return 1; 2268470decc6SDave Kleikamp 2269470decc6SDave Kleikamp return 0; 2270470decc6SDave Kleikamp } 2271470decc6SDave Kleikamp 2272a1e5e465SHarshad Shirwadkar static int 2273a1e5e465SHarshad Shirwadkar jbd2_journal_initialize_fast_commit(journal_t *journal) 2274a1e5e465SHarshad Shirwadkar { 2275a1e5e465SHarshad Shirwadkar journal_superblock_t *sb = journal->j_superblock; 2276a1e5e465SHarshad Shirwadkar unsigned long long num_fc_blks; 2277a1e5e465SHarshad Shirwadkar 22789bd23c31SHarshad Shirwadkar num_fc_blks = jbd2_journal_get_num_fc_blks(sb); 2279a1e5e465SHarshad Shirwadkar if (journal->j_last - num_fc_blks < JBD2_MIN_JOURNAL_BLOCKS) 2280a1e5e465SHarshad Shirwadkar return -ENOSPC; 2281a1e5e465SHarshad Shirwadkar 2282a1e5e465SHarshad Shirwadkar /* Are we called twice? */ 2283a1e5e465SHarshad Shirwadkar WARN_ON(journal->j_fc_wbuf != NULL); 2284a1e5e465SHarshad Shirwadkar journal->j_fc_wbuf = kmalloc_array(num_fc_blks, 2285a1e5e465SHarshad Shirwadkar sizeof(struct buffer_head *), GFP_KERNEL); 2286a1e5e465SHarshad Shirwadkar if (!journal->j_fc_wbuf) 2287a1e5e465SHarshad Shirwadkar return -ENOMEM; 2288a1e5e465SHarshad Shirwadkar 2289a1e5e465SHarshad Shirwadkar journal->j_fc_wbufsize = num_fc_blks; 2290a1e5e465SHarshad Shirwadkar journal->j_fc_last = journal->j_last; 2291a1e5e465SHarshad Shirwadkar journal->j_last = journal->j_fc_last - num_fc_blks; 2292a1e5e465SHarshad Shirwadkar journal->j_fc_first = journal->j_last + 1; 2293a1e5e465SHarshad Shirwadkar journal->j_fc_off = 0; 2294a1e5e465SHarshad Shirwadkar journal->j_free = journal->j_last - journal->j_first; 2295a1e5e465SHarshad Shirwadkar journal->j_max_transaction_buffers = 2296a1e5e465SHarshad Shirwadkar jbd2_journal_get_max_txn_bufs(journal); 2297a1e5e465SHarshad Shirwadkar 2298a1e5e465SHarshad Shirwadkar return 0; 2299a1e5e465SHarshad Shirwadkar } 2300a1e5e465SHarshad Shirwadkar 2301470decc6SDave Kleikamp /** 23022bf31d94SMauro Carvalho Chehab * jbd2_journal_set_features() - Mark a given journal feature in the superblock 2303470decc6SDave Kleikamp * @journal: Journal to act on. 2304470decc6SDave Kleikamp * @compat: bitmask of compatible features 2305470decc6SDave Kleikamp * @ro: bitmask of features that force read-only mount 2306470decc6SDave Kleikamp * @incompat: bitmask of incompatible features 2307470decc6SDave Kleikamp * 2308470decc6SDave Kleikamp * Mark a given journal feature as present on the 2309470decc6SDave Kleikamp * superblock. Returns true if the requested features could be set. 2310470decc6SDave Kleikamp * 2311470decc6SDave Kleikamp */ 2312470decc6SDave Kleikamp 2313f7f4bccbSMingming Cao int jbd2_journal_set_features(journal_t *journal, unsigned long compat, 2314470decc6SDave Kleikamp unsigned long ro, unsigned long incompat) 2315470decc6SDave Kleikamp { 231625ed6e8aSDarrick J. Wong #define INCOMPAT_FEATURE_ON(f) \ 231725ed6e8aSDarrick J. Wong ((incompat & (f)) && !(sb->s_feature_incompat & cpu_to_be32(f))) 231825ed6e8aSDarrick J. Wong #define COMPAT_FEATURE_ON(f) \ 231925ed6e8aSDarrick J. Wong ((compat & (f)) && !(sb->s_feature_compat & cpu_to_be32(f))) 2320470decc6SDave Kleikamp journal_superblock_t *sb; 2321470decc6SDave Kleikamp 2322f7f4bccbSMingming Cao if (jbd2_journal_check_used_features(journal, compat, ro, incompat)) 2323470decc6SDave Kleikamp return 1; 2324470decc6SDave Kleikamp 2325f7f4bccbSMingming Cao if (!jbd2_journal_check_available_features(journal, compat, ro, incompat)) 2326470decc6SDave Kleikamp return 0; 2327470decc6SDave Kleikamp 2328db9ee220SDarrick J. Wong /* If enabling v2 checksums, turn on v3 instead */ 2329db9ee220SDarrick J. Wong if (incompat & JBD2_FEATURE_INCOMPAT_CSUM_V2) { 2330db9ee220SDarrick J. Wong incompat &= ~JBD2_FEATURE_INCOMPAT_CSUM_V2; 2331db9ee220SDarrick J. Wong incompat |= JBD2_FEATURE_INCOMPAT_CSUM_V3; 2332db9ee220SDarrick J. Wong } 2333db9ee220SDarrick J. Wong 2334db9ee220SDarrick J. Wong /* Asking for checksumming v3 and v1? Only give them v3. */ 2335db9ee220SDarrick J. Wong if (incompat & JBD2_FEATURE_INCOMPAT_CSUM_V3 && 233625ed6e8aSDarrick J. Wong compat & JBD2_FEATURE_COMPAT_CHECKSUM) 233725ed6e8aSDarrick J. Wong compat &= ~JBD2_FEATURE_COMPAT_CHECKSUM; 233825ed6e8aSDarrick J. Wong 2339cb3b3bf2SJan Kara jbd2_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n", 2340470decc6SDave Kleikamp compat, ro, incompat); 2341470decc6SDave Kleikamp 2342470decc6SDave Kleikamp sb = journal->j_superblock; 2343470decc6SDave Kleikamp 2344a1e5e465SHarshad Shirwadkar if (incompat & JBD2_FEATURE_INCOMPAT_FAST_COMMIT) { 2345a1e5e465SHarshad Shirwadkar if (jbd2_journal_initialize_fast_commit(journal)) { 2346a1e5e465SHarshad Shirwadkar pr_err("JBD2: Cannot enable fast commits.\n"); 2347a1e5e465SHarshad Shirwadkar return 0; 2348a1e5e465SHarshad Shirwadkar } 2349a1e5e465SHarshad Shirwadkar } 2350a1e5e465SHarshad Shirwadkar 2351538bcaa6STheodore Ts'o /* Load the checksum driver if necessary */ 2352538bcaa6STheodore Ts'o if ((journal->j_chksum_driver == NULL) && 2353538bcaa6STheodore Ts'o INCOMPAT_FEATURE_ON(JBD2_FEATURE_INCOMPAT_CSUM_V3)) { 2354538bcaa6STheodore Ts'o journal->j_chksum_driver = crypto_alloc_shash("crc32c", 0, 0); 2355538bcaa6STheodore Ts'o if (IS_ERR(journal->j_chksum_driver)) { 2356538bcaa6STheodore Ts'o printk(KERN_ERR "JBD2: Cannot load crc32c driver.\n"); 2357538bcaa6STheodore Ts'o journal->j_chksum_driver = NULL; 2358538bcaa6STheodore Ts'o return 0; 2359538bcaa6STheodore Ts'o } 2360538bcaa6STheodore Ts'o /* Precompute checksum seed for all metadata */ 2361538bcaa6STheodore Ts'o journal->j_csum_seed = jbd2_chksum(journal, ~0, sb->s_uuid, 2362538bcaa6STheodore Ts'o sizeof(sb->s_uuid)); 2363538bcaa6STheodore Ts'o } 2364538bcaa6STheodore Ts'o 2365538bcaa6STheodore Ts'o lock_buffer(journal->j_sb_buffer); 2366538bcaa6STheodore Ts'o 2367db9ee220SDarrick J. Wong /* If enabling v3 checksums, update superblock */ 2368db9ee220SDarrick J. Wong if (INCOMPAT_FEATURE_ON(JBD2_FEATURE_INCOMPAT_CSUM_V3)) { 236925ed6e8aSDarrick J. Wong sb->s_checksum_type = JBD2_CRC32C_CHKSUM; 237025ed6e8aSDarrick J. Wong sb->s_feature_compat &= 237125ed6e8aSDarrick J. Wong ~cpu_to_be32(JBD2_FEATURE_COMPAT_CHECKSUM); 237232f38691SDarrick J. Wong } 237325ed6e8aSDarrick J. Wong 237425ed6e8aSDarrick J. Wong /* If enabling v1 checksums, downgrade superblock */ 237525ed6e8aSDarrick J. Wong if (COMPAT_FEATURE_ON(JBD2_FEATURE_COMPAT_CHECKSUM)) 237625ed6e8aSDarrick J. Wong sb->s_feature_incompat &= 2377db9ee220SDarrick J. Wong ~cpu_to_be32(JBD2_FEATURE_INCOMPAT_CSUM_V2 | 2378db9ee220SDarrick J. Wong JBD2_FEATURE_INCOMPAT_CSUM_V3); 237925ed6e8aSDarrick J. Wong 2380470decc6SDave Kleikamp sb->s_feature_compat |= cpu_to_be32(compat); 2381470decc6SDave Kleikamp sb->s_feature_ro_compat |= cpu_to_be32(ro); 2382470decc6SDave Kleikamp sb->s_feature_incompat |= cpu_to_be32(incompat); 2383538bcaa6STheodore Ts'o unlock_buffer(journal->j_sb_buffer); 2384fdc3ef88SJan Kara journal->j_revoke_records_per_block = 2385fdc3ef88SJan Kara journal_revoke_records_per_block(journal); 2386470decc6SDave Kleikamp 2387470decc6SDave Kleikamp return 1; 238825ed6e8aSDarrick J. Wong #undef COMPAT_FEATURE_ON 238925ed6e8aSDarrick J. Wong #undef INCOMPAT_FEATURE_ON 2390470decc6SDave Kleikamp } 2391470decc6SDave Kleikamp 2392818d276cSGirish Shilamkar /* 2393818d276cSGirish Shilamkar * jbd2_journal_clear_features() - Clear a given journal feature in the 2394818d276cSGirish Shilamkar * superblock 2395818d276cSGirish Shilamkar * @journal: Journal to act on. 2396818d276cSGirish Shilamkar * @compat: bitmask of compatible features 2397818d276cSGirish Shilamkar * @ro: bitmask of features that force read-only mount 2398818d276cSGirish Shilamkar * @incompat: bitmask of incompatible features 2399818d276cSGirish Shilamkar * 2400818d276cSGirish Shilamkar * Clear a given journal feature as present on the 2401818d276cSGirish Shilamkar * superblock. 2402818d276cSGirish Shilamkar */ 2403818d276cSGirish Shilamkar void jbd2_journal_clear_features(journal_t *journal, unsigned long compat, 2404818d276cSGirish Shilamkar unsigned long ro, unsigned long incompat) 2405818d276cSGirish Shilamkar { 2406818d276cSGirish Shilamkar journal_superblock_t *sb; 2407818d276cSGirish Shilamkar 2408cb3b3bf2SJan Kara jbd2_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n", 2409818d276cSGirish Shilamkar compat, ro, incompat); 2410818d276cSGirish Shilamkar 2411818d276cSGirish Shilamkar sb = journal->j_superblock; 2412818d276cSGirish Shilamkar 2413818d276cSGirish Shilamkar sb->s_feature_compat &= ~cpu_to_be32(compat); 2414818d276cSGirish Shilamkar sb->s_feature_ro_compat &= ~cpu_to_be32(ro); 2415818d276cSGirish Shilamkar sb->s_feature_incompat &= ~cpu_to_be32(incompat); 2416fdc3ef88SJan Kara journal->j_revoke_records_per_block = 2417fdc3ef88SJan Kara journal_revoke_records_per_block(journal); 2418818d276cSGirish Shilamkar } 2419818d276cSGirish Shilamkar EXPORT_SYMBOL(jbd2_journal_clear_features); 2420470decc6SDave Kleikamp 2421470decc6SDave Kleikamp /** 24222bf31d94SMauro Carvalho Chehab * jbd2_journal_flush() - Flush journal 2423470decc6SDave Kleikamp * @journal: Journal to act on. 242401d5d965SLeah Rumancik * @flags: optional operation on the journal blocks after the flush (see below) 2425470decc6SDave Kleikamp * 2426470decc6SDave Kleikamp * Flush all data for a given journal to disk and empty the journal. 2427470decc6SDave Kleikamp * Filesystems can use this when remounting readonly to ensure that 242801d5d965SLeah Rumancik * recovery does not need to happen on remount. Optionally, a discard or zeroout 242901d5d965SLeah Rumancik * can be issued on the journal blocks after flushing. 243001d5d965SLeah Rumancik * 243101d5d965SLeah Rumancik * flags: 243201d5d965SLeah Rumancik * JBD2_JOURNAL_FLUSH_DISCARD: issues discards for the journal blocks 243301d5d965SLeah Rumancik * JBD2_JOURNAL_FLUSH_ZEROOUT: issues zeroouts for the journal blocks 2434470decc6SDave Kleikamp */ 243501d5d965SLeah Rumancik int jbd2_journal_flush(journal_t *journal, unsigned int flags) 2436470decc6SDave Kleikamp { 2437470decc6SDave Kleikamp int err = 0; 2438470decc6SDave Kleikamp transaction_t *transaction = NULL; 2439470decc6SDave Kleikamp 2440a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 2441470decc6SDave Kleikamp 2442470decc6SDave Kleikamp /* Force everything buffered to the log... */ 2443470decc6SDave Kleikamp if (journal->j_running_transaction) { 2444470decc6SDave Kleikamp transaction = journal->j_running_transaction; 2445f7f4bccbSMingming Cao __jbd2_log_start_commit(journal, transaction->t_tid); 2446470decc6SDave Kleikamp } else if (journal->j_committing_transaction) 2447470decc6SDave Kleikamp transaction = journal->j_committing_transaction; 2448470decc6SDave Kleikamp 2449470decc6SDave Kleikamp /* Wait for the log commit to complete... */ 2450470decc6SDave Kleikamp if (transaction) { 2451470decc6SDave Kleikamp tid_t tid = transaction->t_tid; 2452470decc6SDave Kleikamp 2453a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 2454f7f4bccbSMingming Cao jbd2_log_wait_commit(journal, tid); 2455470decc6SDave Kleikamp } else { 2456a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 2457470decc6SDave Kleikamp } 2458470decc6SDave Kleikamp 2459470decc6SDave Kleikamp /* ...and flush everything in the log out to disk. */ 2460470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 2461470decc6SDave Kleikamp while (!err && journal->j_checkpoint_transactions != NULL) { 2462470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 24636fa7aa50STejun Heo mutex_lock_io(&journal->j_checkpoint_mutex); 2464f7f4bccbSMingming Cao err = jbd2_log_do_checkpoint(journal); 246544519fafSHidehiro Kawai mutex_unlock(&journal->j_checkpoint_mutex); 2466470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 2467470decc6SDave Kleikamp } 2468470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 246944519fafSHidehiro Kawai 247044519fafSHidehiro Kawai if (is_journal_aborted(journal)) 247144519fafSHidehiro Kawai return -EIO; 247244519fafSHidehiro Kawai 24736fa7aa50STejun Heo mutex_lock_io(&journal->j_checkpoint_mutex); 24746f6a6fdaSJoseph Qi if (!err) { 24756f6a6fdaSJoseph Qi err = jbd2_cleanup_journal_tail(journal); 24766f6a6fdaSJoseph Qi if (err < 0) { 24776f6a6fdaSJoseph Qi mutex_unlock(&journal->j_checkpoint_mutex); 24786f6a6fdaSJoseph Qi goto out; 24796f6a6fdaSJoseph Qi } 24806f6a6fdaSJoseph Qi err = 0; 24816f6a6fdaSJoseph Qi } 2482470decc6SDave Kleikamp 2483470decc6SDave Kleikamp /* Finally, mark the journal as really needing no recovery. 2484470decc6SDave Kleikamp * This sets s_start==0 in the underlying superblock, which is 2485470decc6SDave Kleikamp * the magic code for a fully-recovered superblock. Any future 2486470decc6SDave Kleikamp * commits of data to the journal will restore the current 2487470decc6SDave Kleikamp * s_start value. */ 248817f423b5SJan Kara jbd2_mark_journal_empty(journal, REQ_SYNC | REQ_FUA); 248901d5d965SLeah Rumancik 249001d5d965SLeah Rumancik if (flags) 249101d5d965SLeah Rumancik err = __jbd2_journal_erase(journal, flags); 249201d5d965SLeah Rumancik 2493a78bb11dSJan Kara mutex_unlock(&journal->j_checkpoint_mutex); 2494a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 2495470decc6SDave Kleikamp J_ASSERT(!journal->j_running_transaction); 2496470decc6SDave Kleikamp J_ASSERT(!journal->j_committing_transaction); 2497470decc6SDave Kleikamp J_ASSERT(!journal->j_checkpoint_transactions); 2498470decc6SDave Kleikamp J_ASSERT(journal->j_head == journal->j_tail); 2499470decc6SDave Kleikamp J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence); 2500a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 25016f6a6fdaSJoseph Qi out: 25026f6a6fdaSJoseph Qi return err; 2503470decc6SDave Kleikamp } 2504470decc6SDave Kleikamp 2505470decc6SDave Kleikamp /** 25062bf31d94SMauro Carvalho Chehab * jbd2_journal_wipe() - Wipe journal contents 2507470decc6SDave Kleikamp * @journal: Journal to act on. 2508470decc6SDave Kleikamp * @write: flag (see below) 2509470decc6SDave Kleikamp * 2510470decc6SDave Kleikamp * Wipe out all of the contents of a journal, safely. This will produce 2511470decc6SDave Kleikamp * a warning if the journal contains any valid recovery information. 2512f7f4bccbSMingming Cao * Must be called between journal_init_*() and jbd2_journal_load(). 2513470decc6SDave Kleikamp * 2514470decc6SDave Kleikamp * If 'write' is non-zero, then we wipe out the journal on disk; otherwise 2515470decc6SDave Kleikamp * we merely suppress recovery. 2516470decc6SDave Kleikamp */ 2517470decc6SDave Kleikamp 2518f7f4bccbSMingming Cao int jbd2_journal_wipe(journal_t *journal, int write) 2519470decc6SDave Kleikamp { 2520470decc6SDave Kleikamp int err = 0; 2521470decc6SDave Kleikamp 2522f7f4bccbSMingming Cao J_ASSERT (!(journal->j_flags & JBD2_LOADED)); 2523470decc6SDave Kleikamp 2524470decc6SDave Kleikamp if (!journal->j_tail) 2525470decc6SDave Kleikamp goto no_recovery; 2526470decc6SDave Kleikamp 2527f2a44523SEryu Guan printk(KERN_WARNING "JBD2: %s recovery information on journal\n", 2528470decc6SDave Kleikamp write ? "Clearing" : "Ignoring"); 2529470decc6SDave Kleikamp 2530f7f4bccbSMingming Cao err = jbd2_journal_skip_recovery(journal); 2531a78bb11dSJan Kara if (write) { 2532a78bb11dSJan Kara /* Lock to make assertions happy... */ 253353cf9784SXiaoguang Wang mutex_lock_io(&journal->j_checkpoint_mutex); 253417f423b5SJan Kara jbd2_mark_journal_empty(journal, REQ_SYNC | REQ_FUA); 2535a78bb11dSJan Kara mutex_unlock(&journal->j_checkpoint_mutex); 2536a78bb11dSJan Kara } 2537470decc6SDave Kleikamp 2538470decc6SDave Kleikamp no_recovery: 2539470decc6SDave Kleikamp return err; 2540470decc6SDave Kleikamp } 2541470decc6SDave Kleikamp 2542470decc6SDave Kleikamp /** 25432bf31d94SMauro Carvalho Chehab * jbd2_journal_abort () - Shutdown the journal immediately. 2544470decc6SDave Kleikamp * @journal: the journal to shutdown. 2545470decc6SDave Kleikamp * @errno: an error number to record in the journal indicating 2546470decc6SDave Kleikamp * the reason for the shutdown. 2547470decc6SDave Kleikamp * 2548470decc6SDave Kleikamp * Perform a complete, immediate shutdown of the ENTIRE 2549470decc6SDave Kleikamp * journal (not of a single transaction). This operation cannot be 2550470decc6SDave Kleikamp * undone without closing and reopening the journal. 2551470decc6SDave Kleikamp * 2552f7f4bccbSMingming Cao * The jbd2_journal_abort function is intended to support higher level error 2553470decc6SDave Kleikamp * recovery mechanisms such as the ext2/ext3 remount-readonly error 2554470decc6SDave Kleikamp * mode. 2555470decc6SDave Kleikamp * 2556470decc6SDave Kleikamp * Journal abort has very specific semantics. Any existing dirty, 2557470decc6SDave Kleikamp * unjournaled buffers in the main filesystem will still be written to 2558470decc6SDave Kleikamp * disk by bdflush, but the journaling mechanism will be suspended 2559470decc6SDave Kleikamp * immediately and no further transaction commits will be honoured. 2560470decc6SDave Kleikamp * 2561470decc6SDave Kleikamp * Any dirty, journaled buffers will be written back to disk without 2562470decc6SDave Kleikamp * hitting the journal. Atomicity cannot be guaranteed on an aborted 2563470decc6SDave Kleikamp * filesystem, but we _do_ attempt to leave as much data as possible 2564470decc6SDave Kleikamp * behind for fsck to use for cleanup. 2565470decc6SDave Kleikamp * 2566470decc6SDave Kleikamp * Any attempt to get a new transaction handle on a journal which is in 2567470decc6SDave Kleikamp * ABORT state will just result in an -EROFS error return. A 2568f7f4bccbSMingming Cao * jbd2_journal_stop on an existing handle will return -EIO if we have 2569470decc6SDave Kleikamp * entered abort state during the update. 2570470decc6SDave Kleikamp * 2571470decc6SDave Kleikamp * Recursive transactions are not disturbed by journal abort until the 2572f7f4bccbSMingming Cao * final jbd2_journal_stop, which will receive the -EIO error. 2573470decc6SDave Kleikamp * 2574f7f4bccbSMingming Cao * Finally, the jbd2_journal_abort call allows the caller to supply an errno 2575470decc6SDave Kleikamp * which will be recorded (if possible) in the journal superblock. This 2576470decc6SDave Kleikamp * allows a client to record failure conditions in the middle of a 2577470decc6SDave Kleikamp * transaction without having to complete the transaction to record the 2578470decc6SDave Kleikamp * failure to disk. ext3_error, for example, now uses this 2579470decc6SDave Kleikamp * functionality. 2580470decc6SDave Kleikamp * 2581470decc6SDave Kleikamp */ 2582470decc6SDave Kleikamp 2583f7f4bccbSMingming Cao void jbd2_journal_abort(journal_t *journal, int errno) 2584470decc6SDave Kleikamp { 25857f6225e4Szhangyi (F) transaction_t *transaction; 25867f6225e4Szhangyi (F) 25877f6225e4Szhangyi (F) /* 25887b97d868Szhangyi (F) * Lock the aborting procedure until everything is done, this avoid 25897b97d868Szhangyi (F) * races between filesystem's error handling flow (e.g. ext4_abort()), 25907b97d868Szhangyi (F) * ensure panic after the error info is written into journal's 25917b97d868Szhangyi (F) * superblock. 25927b97d868Szhangyi (F) */ 25937b97d868Szhangyi (F) mutex_lock(&journal->j_abort_mutex); 25947b97d868Szhangyi (F) /* 25957f6225e4Szhangyi (F) * ESHUTDOWN always takes precedence because a file system check 25967f6225e4Szhangyi (F) * caused by any other journal abort error is not required after 25977f6225e4Szhangyi (F) * a shutdown triggered. 25987f6225e4Szhangyi (F) */ 25997f6225e4Szhangyi (F) write_lock(&journal->j_state_lock); 26007f6225e4Szhangyi (F) if (journal->j_flags & JBD2_ABORT) { 26017f6225e4Szhangyi (F) int old_errno = journal->j_errno; 26027f6225e4Szhangyi (F) 26037f6225e4Szhangyi (F) write_unlock(&journal->j_state_lock); 26047f6225e4Szhangyi (F) if (old_errno != -ESHUTDOWN && errno == -ESHUTDOWN) { 26057f6225e4Szhangyi (F) journal->j_errno = errno; 26067f6225e4Szhangyi (F) jbd2_journal_update_sb_errno(journal); 26077f6225e4Szhangyi (F) } 26087b97d868Szhangyi (F) mutex_unlock(&journal->j_abort_mutex); 26097f6225e4Szhangyi (F) return; 26107f6225e4Szhangyi (F) } 26117f6225e4Szhangyi (F) 26127f6225e4Szhangyi (F) /* 26137f6225e4Szhangyi (F) * Mark the abort as occurred and start current running transaction 26147f6225e4Szhangyi (F) * to release all journaled buffer. 26157f6225e4Szhangyi (F) */ 26167f6225e4Szhangyi (F) pr_err("Aborting journal on device %s.\n", journal->j_devname); 26177f6225e4Szhangyi (F) 26187f6225e4Szhangyi (F) journal->j_flags |= JBD2_ABORT; 26197f6225e4Szhangyi (F) journal->j_errno = errno; 26207f6225e4Szhangyi (F) transaction = journal->j_running_transaction; 26217f6225e4Szhangyi (F) if (transaction) 26227f6225e4Szhangyi (F) __jbd2_log_start_commit(journal, transaction->t_tid); 26237f6225e4Szhangyi (F) write_unlock(&journal->j_state_lock); 26247f6225e4Szhangyi (F) 26257f6225e4Szhangyi (F) /* 26267f6225e4Szhangyi (F) * Record errno to the journal super block, so that fsck and jbd2 26277f6225e4Szhangyi (F) * layer could realise that a filesystem check is needed. 26287f6225e4Szhangyi (F) */ 26297f6225e4Szhangyi (F) jbd2_journal_update_sb_errno(journal); 26307b97d868Szhangyi (F) mutex_unlock(&journal->j_abort_mutex); 2631470decc6SDave Kleikamp } 2632470decc6SDave Kleikamp 2633470decc6SDave Kleikamp /** 26342bf31d94SMauro Carvalho Chehab * jbd2_journal_errno() - returns the journal's error state. 2635470decc6SDave Kleikamp * @journal: journal to examine. 2636470decc6SDave Kleikamp * 2637bfcd3555SAlberto Bertogli * This is the errno number set with jbd2_journal_abort(), the last 2638470decc6SDave Kleikamp * time the journal was mounted - if the journal was stopped 2639470decc6SDave Kleikamp * without calling abort this will be 0. 2640470decc6SDave Kleikamp * 2641470decc6SDave Kleikamp * If the journal has been aborted on this mount time -EROFS will 2642470decc6SDave Kleikamp * be returned. 2643470decc6SDave Kleikamp */ 2644f7f4bccbSMingming Cao int jbd2_journal_errno(journal_t *journal) 2645470decc6SDave Kleikamp { 2646470decc6SDave Kleikamp int err; 2647470decc6SDave Kleikamp 2648a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 2649f7f4bccbSMingming Cao if (journal->j_flags & JBD2_ABORT) 2650470decc6SDave Kleikamp err = -EROFS; 2651470decc6SDave Kleikamp else 2652470decc6SDave Kleikamp err = journal->j_errno; 2653a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 2654470decc6SDave Kleikamp return err; 2655470decc6SDave Kleikamp } 2656470decc6SDave Kleikamp 2657470decc6SDave Kleikamp /** 26582bf31d94SMauro Carvalho Chehab * jbd2_journal_clear_err() - clears the journal's error state 2659470decc6SDave Kleikamp * @journal: journal to act on. 2660470decc6SDave Kleikamp * 2661bfcd3555SAlberto Bertogli * An error must be cleared or acked to take a FS out of readonly 2662470decc6SDave Kleikamp * mode. 2663470decc6SDave Kleikamp */ 2664f7f4bccbSMingming Cao int jbd2_journal_clear_err(journal_t *journal) 2665470decc6SDave Kleikamp { 2666470decc6SDave Kleikamp int err = 0; 2667470decc6SDave Kleikamp 2668a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 2669f7f4bccbSMingming Cao if (journal->j_flags & JBD2_ABORT) 2670470decc6SDave Kleikamp err = -EROFS; 2671470decc6SDave Kleikamp else 2672470decc6SDave Kleikamp journal->j_errno = 0; 2673a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 2674470decc6SDave Kleikamp return err; 2675470decc6SDave Kleikamp } 2676470decc6SDave Kleikamp 2677470decc6SDave Kleikamp /** 26782bf31d94SMauro Carvalho Chehab * jbd2_journal_ack_err() - Ack journal err. 2679470decc6SDave Kleikamp * @journal: journal to act on. 2680470decc6SDave Kleikamp * 2681bfcd3555SAlberto Bertogli * An error must be cleared or acked to take a FS out of readonly 2682470decc6SDave Kleikamp * mode. 2683470decc6SDave Kleikamp */ 2684f7f4bccbSMingming Cao void jbd2_journal_ack_err(journal_t *journal) 2685470decc6SDave Kleikamp { 2686a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 2687470decc6SDave Kleikamp if (journal->j_errno) 2688f7f4bccbSMingming Cao journal->j_flags |= JBD2_ACK_ERR; 2689a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 2690470decc6SDave Kleikamp } 2691470decc6SDave Kleikamp 2692f7f4bccbSMingming Cao int jbd2_journal_blocks_per_page(struct inode *inode) 2693470decc6SDave Kleikamp { 269409cbfeafSKirill A. Shutemov return 1 << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits); 2695470decc6SDave Kleikamp } 2696470decc6SDave Kleikamp 2697470decc6SDave Kleikamp /* 2698b517bea1SZach Brown * helper functions to deal with 32 or 64bit block numbers. 2699b517bea1SZach Brown */ 2700b517bea1SZach Brown size_t journal_tag_bytes(journal_t *journal) 2701b517bea1SZach Brown { 2702db9ee220SDarrick J. Wong size_t sz; 2703db9ee220SDarrick J. Wong 270456316a0dSDarrick J. Wong if (jbd2_has_feature_csum3(journal)) 2705db9ee220SDarrick J. Wong return sizeof(journal_block_tag3_t); 2706db9ee220SDarrick J. Wong 2707db9ee220SDarrick J. Wong sz = sizeof(journal_block_tag_t); 2708c3900875SDarrick J. Wong 270956316a0dSDarrick J. Wong if (jbd2_has_feature_csum2(journal)) 2710db9ee220SDarrick J. Wong sz += sizeof(__u16); 2711c3900875SDarrick J. Wong 271256316a0dSDarrick J. Wong if (jbd2_has_feature_64bit(journal)) 2713db9ee220SDarrick J. Wong return sz; 2714b517bea1SZach Brown else 2715db9ee220SDarrick J. Wong return sz - sizeof(__u32); 2716b517bea1SZach Brown } 2717b517bea1SZach Brown 2718b517bea1SZach Brown /* 2719d2eecb03STheodore Ts'o * JBD memory management 2720d2eecb03STheodore Ts'o * 2721d2eecb03STheodore Ts'o * These functions are used to allocate block-sized chunks of memory 2722d2eecb03STheodore Ts'o * used for making copies of buffer_head data. Very often it will be 2723d2eecb03STheodore Ts'o * page-sized chunks of data, but sometimes it will be in 2724d2eecb03STheodore Ts'o * sub-page-size chunks. (For example, 16k pages on Power systems 2725d2eecb03STheodore Ts'o * with a 4k block file system.) For blocks smaller than a page, we 2726d2eecb03STheodore Ts'o * use a SLAB allocator. There are slab caches for each block size, 2727d2eecb03STheodore Ts'o * which are allocated at mount time, if necessary, and we only free 2728d2eecb03STheodore Ts'o * (all of) the slab caches when/if the jbd2 module is unloaded. For 2729d2eecb03STheodore Ts'o * this reason we don't need to a mutex to protect access to 2730d2eecb03STheodore Ts'o * jbd2_slab[] allocating or releasing memory; only in 2731d2eecb03STheodore Ts'o * jbd2_journal_create_slab(). 2732d2eecb03STheodore Ts'o */ 2733d2eecb03STheodore Ts'o #define JBD2_MAX_SLABS 8 2734d2eecb03STheodore Ts'o static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS]; 2735d2eecb03STheodore Ts'o 2736d2eecb03STheodore Ts'o static const char *jbd2_slab_names[JBD2_MAX_SLABS] = { 2737d2eecb03STheodore Ts'o "jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k", 2738d2eecb03STheodore Ts'o "jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k" 2739d2eecb03STheodore Ts'o }; 2740d2eecb03STheodore Ts'o 2741d2eecb03STheodore Ts'o 2742d2eecb03STheodore Ts'o static void jbd2_journal_destroy_slabs(void) 2743d2eecb03STheodore Ts'o { 2744d2eecb03STheodore Ts'o int i; 2745d2eecb03STheodore Ts'o 2746d2eecb03STheodore Ts'o for (i = 0; i < JBD2_MAX_SLABS; i++) { 2747d2eecb03STheodore Ts'o kmem_cache_destroy(jbd2_slab[i]); 2748d2eecb03STheodore Ts'o jbd2_slab[i] = NULL; 2749d2eecb03STheodore Ts'o } 2750d2eecb03STheodore Ts'o } 2751d2eecb03STheodore Ts'o 2752d2eecb03STheodore Ts'o static int jbd2_journal_create_slab(size_t size) 2753d2eecb03STheodore Ts'o { 275451dfacdeSThomas Gleixner static DEFINE_MUTEX(jbd2_slab_create_mutex); 2755d2eecb03STheodore Ts'o int i = order_base_2(size) - 10; 2756d2eecb03STheodore Ts'o size_t slab_size; 2757d2eecb03STheodore Ts'o 2758d2eecb03STheodore Ts'o if (size == PAGE_SIZE) 2759d2eecb03STheodore Ts'o return 0; 2760d2eecb03STheodore Ts'o 2761d2eecb03STheodore Ts'o if (i >= JBD2_MAX_SLABS) 2762d2eecb03STheodore Ts'o return -EINVAL; 2763d2eecb03STheodore Ts'o 2764d2eecb03STheodore Ts'o if (unlikely(i < 0)) 2765d2eecb03STheodore Ts'o i = 0; 276651dfacdeSThomas Gleixner mutex_lock(&jbd2_slab_create_mutex); 2767d2eecb03STheodore Ts'o if (jbd2_slab[i]) { 276851dfacdeSThomas Gleixner mutex_unlock(&jbd2_slab_create_mutex); 2769d2eecb03STheodore Ts'o return 0; /* Already created */ 2770d2eecb03STheodore Ts'o } 2771d2eecb03STheodore Ts'o 2772d2eecb03STheodore Ts'o slab_size = 1 << (i+10); 2773d2eecb03STheodore Ts'o jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size, 2774d2eecb03STheodore Ts'o slab_size, 0, NULL); 277551dfacdeSThomas Gleixner mutex_unlock(&jbd2_slab_create_mutex); 2776d2eecb03STheodore Ts'o if (!jbd2_slab[i]) { 2777d2eecb03STheodore Ts'o printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n"); 2778d2eecb03STheodore Ts'o return -ENOMEM; 2779d2eecb03STheodore Ts'o } 2780d2eecb03STheodore Ts'o return 0; 2781d2eecb03STheodore Ts'o } 2782d2eecb03STheodore Ts'o 2783d2eecb03STheodore Ts'o static struct kmem_cache *get_slab(size_t size) 2784d2eecb03STheodore Ts'o { 2785d2eecb03STheodore Ts'o int i = order_base_2(size) - 10; 2786d2eecb03STheodore Ts'o 2787d2eecb03STheodore Ts'o BUG_ON(i >= JBD2_MAX_SLABS); 2788d2eecb03STheodore Ts'o if (unlikely(i < 0)) 2789d2eecb03STheodore Ts'o i = 0; 27908ac97b74SBill Pemberton BUG_ON(jbd2_slab[i] == NULL); 2791d2eecb03STheodore Ts'o return jbd2_slab[i]; 2792d2eecb03STheodore Ts'o } 2793d2eecb03STheodore Ts'o 2794d2eecb03STheodore Ts'o void *jbd2_alloc(size_t size, gfp_t flags) 2795d2eecb03STheodore Ts'o { 2796d2eecb03STheodore Ts'o void *ptr; 2797d2eecb03STheodore Ts'o 2798d2eecb03STheodore Ts'o BUG_ON(size & (size-1)); /* Must be a power of 2 */ 2799d2eecb03STheodore Ts'o 2800f2db1971SMichal Hocko if (size < PAGE_SIZE) 2801d2eecb03STheodore Ts'o ptr = kmem_cache_alloc(get_slab(size), flags); 2802f2db1971SMichal Hocko else 2803f2db1971SMichal Hocko ptr = (void *)__get_free_pages(flags, get_order(size)); 2804d2eecb03STheodore Ts'o 2805d2eecb03STheodore Ts'o /* Check alignment; SLUB has gotten this wrong in the past, 2806d2eecb03STheodore Ts'o * and this can lead to user data corruption! */ 2807d2eecb03STheodore Ts'o BUG_ON(((unsigned long) ptr) & (size-1)); 2808d2eecb03STheodore Ts'o 2809d2eecb03STheodore Ts'o return ptr; 2810d2eecb03STheodore Ts'o } 2811d2eecb03STheodore Ts'o 2812d2eecb03STheodore Ts'o void jbd2_free(void *ptr, size_t size) 2813d2eecb03STheodore Ts'o { 2814f2db1971SMichal Hocko if (size < PAGE_SIZE) 2815d2eecb03STheodore Ts'o kmem_cache_free(get_slab(size), ptr); 2816f2db1971SMichal Hocko else 2817f2db1971SMichal Hocko free_pages((unsigned long)ptr, get_order(size)); 2818d2eecb03STheodore Ts'o }; 2819d2eecb03STheodore Ts'o 2820d2eecb03STheodore Ts'o /* 2821470decc6SDave Kleikamp * Journal_head storage management 2822470decc6SDave Kleikamp */ 2823e18b890bSChristoph Lameter static struct kmem_cache *jbd2_journal_head_cache; 2824e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 2825470decc6SDave Kleikamp static atomic_t nr_journal_heads = ATOMIC_INIT(0); 2826470decc6SDave Kleikamp #endif 2827470decc6SDave Kleikamp 28280d52154bSChengguang Xu static int __init jbd2_journal_init_journal_head_cache(void) 2829470decc6SDave Kleikamp { 28300d52154bSChengguang Xu J_ASSERT(!jbd2_journal_head_cache); 2831a920e941SJohann Lombardi jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head", 2832470decc6SDave Kleikamp sizeof(struct journal_head), 2833470decc6SDave Kleikamp 0, /* offset */ 28345f0d5a3aSPaul E. McKenney SLAB_TEMPORARY | SLAB_TYPESAFE_BY_RCU, 283520c2df83SPaul Mundt NULL); /* ctor */ 28361076d17aSAl Viro if (!jbd2_journal_head_cache) { 2837f2a44523SEryu Guan printk(KERN_EMERG "JBD2: no memory for journal_head cache\n"); 28380d52154bSChengguang Xu return -ENOMEM; 2839470decc6SDave Kleikamp } 28400d52154bSChengguang Xu return 0; 2841470decc6SDave Kleikamp } 2842470decc6SDave Kleikamp 28434185a2acSYongqiang Yang static void jbd2_journal_destroy_journal_head_cache(void) 2844470decc6SDave Kleikamp { 2845f7f4bccbSMingming Cao kmem_cache_destroy(jbd2_journal_head_cache); 2846f7f4bccbSMingming Cao jbd2_journal_head_cache = NULL; 2847470decc6SDave Kleikamp } 2848470decc6SDave Kleikamp 2849470decc6SDave Kleikamp /* 2850470decc6SDave Kleikamp * journal_head splicing and dicing 2851470decc6SDave Kleikamp */ 2852470decc6SDave Kleikamp static struct journal_head *journal_alloc_journal_head(void) 2853470decc6SDave Kleikamp { 2854470decc6SDave Kleikamp struct journal_head *ret; 2855470decc6SDave Kleikamp 2856e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 2857470decc6SDave Kleikamp atomic_inc(&nr_journal_heads); 2858470decc6SDave Kleikamp #endif 28595d9cf9c6SZheng Liu ret = kmem_cache_zalloc(jbd2_journal_head_cache, GFP_NOFS); 28601076d17aSAl Viro if (!ret) { 2861cb3b3bf2SJan Kara jbd2_debug(1, "out of memory for journal_head\n"); 2862670be5a7STheodore Ts'o pr_notice_ratelimited("ENOMEM in %s, retrying.\n", __func__); 28637b506b10SMichal Hocko ret = kmem_cache_zalloc(jbd2_journal_head_cache, 28647b506b10SMichal Hocko GFP_NOFS | __GFP_NOFAIL); 2865470decc6SDave Kleikamp } 286646417064SThomas Gleixner if (ret) 286746417064SThomas Gleixner spin_lock_init(&ret->b_state_lock); 2868470decc6SDave Kleikamp return ret; 2869470decc6SDave Kleikamp } 2870470decc6SDave Kleikamp 2871470decc6SDave Kleikamp static void journal_free_journal_head(struct journal_head *jh) 2872470decc6SDave Kleikamp { 2873e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 2874470decc6SDave Kleikamp atomic_dec(&nr_journal_heads); 2875cd02ff0bSMingming Cao memset(jh, JBD2_POISON_FREE, sizeof(*jh)); 2876470decc6SDave Kleikamp #endif 2877f7f4bccbSMingming Cao kmem_cache_free(jbd2_journal_head_cache, jh); 2878470decc6SDave Kleikamp } 2879470decc6SDave Kleikamp 2880470decc6SDave Kleikamp /* 2881470decc6SDave Kleikamp * A journal_head is attached to a buffer_head whenever JBD has an 2882470decc6SDave Kleikamp * interest in the buffer. 2883470decc6SDave Kleikamp * 2884470decc6SDave Kleikamp * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit 2885470decc6SDave Kleikamp * is set. This bit is tested in core kernel code where we need to take 2886470decc6SDave Kleikamp * JBD-specific actions. Testing the zeroness of ->b_private is not reliable 2887470decc6SDave Kleikamp * there. 2888470decc6SDave Kleikamp * 2889470decc6SDave Kleikamp * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one. 2890470decc6SDave Kleikamp * 2891470decc6SDave Kleikamp * When a buffer has its BH_JBD bit set it is immune from being released by 2892470decc6SDave Kleikamp * core kernel code, mainly via ->b_count. 2893470decc6SDave Kleikamp * 2894de1b7941SJan Kara * A journal_head is detached from its buffer_head when the journal_head's 2895de1b7941SJan Kara * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint 2896de1b7941SJan Kara * transaction (b_cp_transaction) hold their references to b_jcount. 2897470decc6SDave Kleikamp * 2898470decc6SDave Kleikamp * Various places in the kernel want to attach a journal_head to a buffer_head 2899470decc6SDave Kleikamp * _before_ attaching the journal_head to a transaction. To protect the 2900f7f4bccbSMingming Cao * journal_head in this situation, jbd2_journal_add_journal_head elevates the 2901470decc6SDave Kleikamp * journal_head's b_jcount refcount by one. The caller must call 2902f7f4bccbSMingming Cao * jbd2_journal_put_journal_head() to undo this. 2903470decc6SDave Kleikamp * 2904470decc6SDave Kleikamp * So the typical usage would be: 2905470decc6SDave Kleikamp * 2906470decc6SDave Kleikamp * (Attach a journal_head if needed. Increments b_jcount) 2907f7f4bccbSMingming Cao * struct journal_head *jh = jbd2_journal_add_journal_head(bh); 2908470decc6SDave Kleikamp * ... 2909de1b7941SJan Kara * (Get another reference for transaction) 2910de1b7941SJan Kara * jbd2_journal_grab_journal_head(bh); 2911470decc6SDave Kleikamp * jh->b_transaction = xxx; 2912de1b7941SJan Kara * (Put original reference) 2913f7f4bccbSMingming Cao * jbd2_journal_put_journal_head(jh); 2914470decc6SDave Kleikamp */ 2915470decc6SDave Kleikamp 2916470decc6SDave Kleikamp /* 2917470decc6SDave Kleikamp * Give a buffer_head a journal_head. 2918470decc6SDave Kleikamp * 2919470decc6SDave Kleikamp * May sleep. 2920470decc6SDave Kleikamp */ 2921f7f4bccbSMingming Cao struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh) 2922470decc6SDave Kleikamp { 2923470decc6SDave Kleikamp struct journal_head *jh; 2924470decc6SDave Kleikamp struct journal_head *new_jh = NULL; 2925470decc6SDave Kleikamp 2926470decc6SDave Kleikamp repeat: 29275d9cf9c6SZheng Liu if (!buffer_jbd(bh)) 2928470decc6SDave Kleikamp new_jh = journal_alloc_journal_head(); 2929470decc6SDave Kleikamp 2930470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 2931470decc6SDave Kleikamp if (buffer_jbd(bh)) { 2932470decc6SDave Kleikamp jh = bh2jh(bh); 2933470decc6SDave Kleikamp } else { 2934470decc6SDave Kleikamp J_ASSERT_BH(bh, 2935470decc6SDave Kleikamp (atomic_read(&bh->b_count) > 0) || 29360d22fe2fSMatthew Wilcox (Oracle) (bh->b_folio && bh->b_folio->mapping)); 2937470decc6SDave Kleikamp 2938470decc6SDave Kleikamp if (!new_jh) { 2939470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 2940470decc6SDave Kleikamp goto repeat; 2941470decc6SDave Kleikamp } 2942470decc6SDave Kleikamp 2943470decc6SDave Kleikamp jh = new_jh; 2944470decc6SDave Kleikamp new_jh = NULL; /* We consumed it */ 2945470decc6SDave Kleikamp set_buffer_jbd(bh); 2946470decc6SDave Kleikamp bh->b_private = jh; 2947470decc6SDave Kleikamp jh->b_bh = bh; 2948470decc6SDave Kleikamp get_bh(bh); 2949470decc6SDave Kleikamp BUFFER_TRACE(bh, "added journal_head"); 2950470decc6SDave Kleikamp } 2951470decc6SDave Kleikamp jh->b_jcount++; 2952470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 2953470decc6SDave Kleikamp if (new_jh) 2954470decc6SDave Kleikamp journal_free_journal_head(new_jh); 2955470decc6SDave Kleikamp return bh->b_private; 2956470decc6SDave Kleikamp } 2957470decc6SDave Kleikamp 2958470decc6SDave Kleikamp /* 2959470decc6SDave Kleikamp * Grab a ref against this buffer_head's journal_head. If it ended up not 2960470decc6SDave Kleikamp * having a journal_head, return NULL 2961470decc6SDave Kleikamp */ 2962f7f4bccbSMingming Cao struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh) 2963470decc6SDave Kleikamp { 2964470decc6SDave Kleikamp struct journal_head *jh = NULL; 2965470decc6SDave Kleikamp 2966470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 2967470decc6SDave Kleikamp if (buffer_jbd(bh)) { 2968470decc6SDave Kleikamp jh = bh2jh(bh); 2969470decc6SDave Kleikamp jh->b_jcount++; 2970470decc6SDave Kleikamp } 2971470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 2972470decc6SDave Kleikamp return jh; 2973470decc6SDave Kleikamp } 29744cd1103dSJoseph Qi EXPORT_SYMBOL(jbd2_journal_grab_journal_head); 2975470decc6SDave Kleikamp 2976470decc6SDave Kleikamp static void __journal_remove_journal_head(struct buffer_head *bh) 2977470decc6SDave Kleikamp { 2978470decc6SDave Kleikamp struct journal_head *jh = bh2jh(bh); 2979470decc6SDave Kleikamp 2980de1b7941SJan Kara J_ASSERT_JH(jh, jh->b_transaction == NULL); 2981de1b7941SJan Kara J_ASSERT_JH(jh, jh->b_next_transaction == NULL); 2982de1b7941SJan Kara J_ASSERT_JH(jh, jh->b_cp_transaction == NULL); 2983470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_jlist == BJ_None); 2984470decc6SDave Kleikamp J_ASSERT_BH(bh, buffer_jbd(bh)); 2985470decc6SDave Kleikamp J_ASSERT_BH(bh, jh2bh(jh) == bh); 2986470decc6SDave Kleikamp BUFFER_TRACE(bh, "remove journal_head"); 29877855a57dSThomas Gleixner 29887855a57dSThomas Gleixner /* Unlink before dropping the lock */ 2989470decc6SDave Kleikamp bh->b_private = NULL; 2990470decc6SDave Kleikamp jh->b_bh = NULL; /* debug, really */ 2991470decc6SDave Kleikamp clear_buffer_jbd(bh); 29927855a57dSThomas Gleixner } 29937855a57dSThomas Gleixner 29947855a57dSThomas Gleixner static void journal_release_journal_head(struct journal_head *jh, size_t b_size) 29957855a57dSThomas Gleixner { 29967855a57dSThomas Gleixner if (jh->b_frozen_data) { 29977855a57dSThomas Gleixner printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__); 29987855a57dSThomas Gleixner jbd2_free(jh->b_frozen_data, b_size); 29997855a57dSThomas Gleixner } 30007855a57dSThomas Gleixner if (jh->b_committed_data) { 30017855a57dSThomas Gleixner printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__); 30027855a57dSThomas Gleixner jbd2_free(jh->b_committed_data, b_size); 30037855a57dSThomas Gleixner } 3004470decc6SDave Kleikamp journal_free_journal_head(jh); 3005470decc6SDave Kleikamp } 3006470decc6SDave Kleikamp 3007470decc6SDave Kleikamp /* 3008de1b7941SJan Kara * Drop a reference on the passed journal_head. If it fell to zero then 3009470decc6SDave Kleikamp * release the journal_head from the buffer_head. 3010470decc6SDave Kleikamp */ 3011f7f4bccbSMingming Cao void jbd2_journal_put_journal_head(struct journal_head *jh) 3012470decc6SDave Kleikamp { 3013470decc6SDave Kleikamp struct buffer_head *bh = jh2bh(jh); 3014470decc6SDave Kleikamp 3015470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 3016470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_jcount > 0); 3017470decc6SDave Kleikamp --jh->b_jcount; 3018de1b7941SJan Kara if (!jh->b_jcount) { 3019470decc6SDave Kleikamp __journal_remove_journal_head(bh); 3020de1b7941SJan Kara jbd_unlock_bh_journal_head(bh); 30217855a57dSThomas Gleixner journal_release_journal_head(jh, bh->b_size); 3022470decc6SDave Kleikamp __brelse(bh); 30237855a57dSThomas Gleixner } else { 3024470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 3025470decc6SDave Kleikamp } 30267855a57dSThomas Gleixner } 30274cd1103dSJoseph Qi EXPORT_SYMBOL(jbd2_journal_put_journal_head); 3028470decc6SDave Kleikamp 3029470decc6SDave Kleikamp /* 3030c851ed54SJan Kara * Initialize jbd inode head 3031c851ed54SJan Kara */ 3032c851ed54SJan Kara void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode) 3033c851ed54SJan Kara { 3034c851ed54SJan Kara jinode->i_transaction = NULL; 3035c851ed54SJan Kara jinode->i_next_transaction = NULL; 3036c851ed54SJan Kara jinode->i_vfs_inode = inode; 3037c851ed54SJan Kara jinode->i_flags = 0; 30386ba0e7dcSRoss Zwisler jinode->i_dirty_start = 0; 30396ba0e7dcSRoss Zwisler jinode->i_dirty_end = 0; 3040c851ed54SJan Kara INIT_LIST_HEAD(&jinode->i_list); 3041c851ed54SJan Kara } 3042c851ed54SJan Kara 3043c851ed54SJan Kara /* 3044c851ed54SJan Kara * Function to be called before we start removing inode from memory (i.e., 3045c851ed54SJan Kara * clear_inode() is a fine place to be called from). It removes inode from 3046c851ed54SJan Kara * transaction's lists. 3047c851ed54SJan Kara */ 3048c851ed54SJan Kara void jbd2_journal_release_jbd_inode(journal_t *journal, 3049c851ed54SJan Kara struct jbd2_inode *jinode) 3050c851ed54SJan Kara { 3051c851ed54SJan Kara if (!journal) 3052c851ed54SJan Kara return; 3053c851ed54SJan Kara restart: 3054c851ed54SJan Kara spin_lock(&journal->j_list_lock); 3055c851ed54SJan Kara /* Is commit writing out inode - we have to wait */ 3056cb0d9d47SJan Kara if (jinode->i_flags & JI_COMMIT_RUNNING) { 3057c851ed54SJan Kara wait_queue_head_t *wq; 3058c851ed54SJan Kara DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING); 3059c851ed54SJan Kara wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING); 306021417136SIngo Molnar prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE); 3061c851ed54SJan Kara spin_unlock(&journal->j_list_lock); 3062c851ed54SJan Kara schedule(); 306321417136SIngo Molnar finish_wait(wq, &wait.wq_entry); 3064c851ed54SJan Kara goto restart; 3065c851ed54SJan Kara } 3066c851ed54SJan Kara 3067c851ed54SJan Kara if (jinode->i_transaction) { 3068c851ed54SJan Kara list_del(&jinode->i_list); 3069c851ed54SJan Kara jinode->i_transaction = NULL; 3070c851ed54SJan Kara } 3071c851ed54SJan Kara spin_unlock(&journal->j_list_lock); 3072c851ed54SJan Kara } 3073c851ed54SJan Kara 3074470decc6SDave Kleikamp 30758e85fb3fSJohann Lombardi #ifdef CONFIG_PROC_FS 30768e85fb3fSJohann Lombardi 30778e85fb3fSJohann Lombardi #define JBD2_STATS_PROC_NAME "fs/jbd2" 30788e85fb3fSJohann Lombardi 30798e85fb3fSJohann Lombardi static void __init jbd2_create_jbd_stats_proc_entry(void) 30808e85fb3fSJohann Lombardi { 30818e85fb3fSJohann Lombardi proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL); 30828e85fb3fSJohann Lombardi } 30838e85fb3fSJohann Lombardi 30848e85fb3fSJohann Lombardi static void __exit jbd2_remove_jbd_stats_proc_entry(void) 30858e85fb3fSJohann Lombardi { 30868e85fb3fSJohann Lombardi if (proc_jbd2_stats) 30878e85fb3fSJohann Lombardi remove_proc_entry(JBD2_STATS_PROC_NAME, NULL); 30888e85fb3fSJohann Lombardi } 30898e85fb3fSJohann Lombardi 30908e85fb3fSJohann Lombardi #else 30918e85fb3fSJohann Lombardi 30928e85fb3fSJohann Lombardi #define jbd2_create_jbd_stats_proc_entry() do {} while (0) 30938e85fb3fSJohann Lombardi #define jbd2_remove_jbd_stats_proc_entry() do {} while (0) 30948e85fb3fSJohann Lombardi 30958e85fb3fSJohann Lombardi #endif 30968e85fb3fSJohann Lombardi 30978aefcd55STheodore Ts'o struct kmem_cache *jbd2_handle_cache, *jbd2_inode_cache; 3098470decc6SDave Kleikamp 30990d52154bSChengguang Xu static int __init jbd2_journal_init_inode_cache(void) 3100470decc6SDave Kleikamp { 31010d52154bSChengguang Xu J_ASSERT(!jbd2_inode_cache); 31028aefcd55STheodore Ts'o jbd2_inode_cache = KMEM_CACHE(jbd2_inode, 0); 31030d52154bSChengguang Xu if (!jbd2_inode_cache) { 31040d52154bSChengguang Xu pr_emerg("JBD2: failed to create inode cache\n"); 3105470decc6SDave Kleikamp return -ENOMEM; 3106470decc6SDave Kleikamp } 3107470decc6SDave Kleikamp return 0; 3108470decc6SDave Kleikamp } 3109470decc6SDave Kleikamp 31100d52154bSChengguang Xu static int __init jbd2_journal_init_handle_cache(void) 31110d52154bSChengguang Xu { 31120d52154bSChengguang Xu J_ASSERT(!jbd2_handle_cache); 31130d52154bSChengguang Xu jbd2_handle_cache = KMEM_CACHE(jbd2_journal_handle, SLAB_TEMPORARY); 31140d52154bSChengguang Xu if (!jbd2_handle_cache) { 31150d52154bSChengguang Xu printk(KERN_EMERG "JBD2: failed to create handle cache\n"); 31160d52154bSChengguang Xu return -ENOMEM; 31170d52154bSChengguang Xu } 31180d52154bSChengguang Xu return 0; 31190d52154bSChengguang Xu } 31200d52154bSChengguang Xu 31210d52154bSChengguang Xu static void jbd2_journal_destroy_inode_cache(void) 31220d52154bSChengguang Xu { 31230d52154bSChengguang Xu kmem_cache_destroy(jbd2_inode_cache); 31240d52154bSChengguang Xu jbd2_inode_cache = NULL; 31250d52154bSChengguang Xu } 31260d52154bSChengguang Xu 3127f7f4bccbSMingming Cao static void jbd2_journal_destroy_handle_cache(void) 3128470decc6SDave Kleikamp { 3129f7f4bccbSMingming Cao kmem_cache_destroy(jbd2_handle_cache); 31308bdd5b60SWang Long jbd2_handle_cache = NULL; 3131470decc6SDave Kleikamp } 3132470decc6SDave Kleikamp 3133470decc6SDave Kleikamp /* 3134470decc6SDave Kleikamp * Module startup and shutdown 3135470decc6SDave Kleikamp */ 3136470decc6SDave Kleikamp 3137470decc6SDave Kleikamp static int __init journal_init_caches(void) 3138470decc6SDave Kleikamp { 3139470decc6SDave Kleikamp int ret; 3140470decc6SDave Kleikamp 31410d52154bSChengguang Xu ret = jbd2_journal_init_revoke_record_cache(); 31420d52154bSChengguang Xu if (ret == 0) 31430d52154bSChengguang Xu ret = jbd2_journal_init_revoke_table_cache(); 3144470decc6SDave Kleikamp if (ret == 0) 31454185a2acSYongqiang Yang ret = jbd2_journal_init_journal_head_cache(); 3146470decc6SDave Kleikamp if (ret == 0) 31474185a2acSYongqiang Yang ret = jbd2_journal_init_handle_cache(); 31480c2022ecSYongqiang Yang if (ret == 0) 31490d52154bSChengguang Xu ret = jbd2_journal_init_inode_cache(); 31500d52154bSChengguang Xu if (ret == 0) 31510c2022ecSYongqiang Yang ret = jbd2_journal_init_transaction_cache(); 3152470decc6SDave Kleikamp return ret; 3153470decc6SDave Kleikamp } 3154470decc6SDave Kleikamp 3155f7f4bccbSMingming Cao static void jbd2_journal_destroy_caches(void) 3156470decc6SDave Kleikamp { 31570d52154bSChengguang Xu jbd2_journal_destroy_revoke_record_cache(); 31580d52154bSChengguang Xu jbd2_journal_destroy_revoke_table_cache(); 31594185a2acSYongqiang Yang jbd2_journal_destroy_journal_head_cache(); 3160f7f4bccbSMingming Cao jbd2_journal_destroy_handle_cache(); 31610d52154bSChengguang Xu jbd2_journal_destroy_inode_cache(); 31620c2022ecSYongqiang Yang jbd2_journal_destroy_transaction_cache(); 3163d2eecb03STheodore Ts'o jbd2_journal_destroy_slabs(); 3164470decc6SDave Kleikamp } 3165470decc6SDave Kleikamp 3166470decc6SDave Kleikamp static int __init journal_init(void) 3167470decc6SDave Kleikamp { 3168470decc6SDave Kleikamp int ret; 3169470decc6SDave Kleikamp 3170470decc6SDave Kleikamp BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024); 3171470decc6SDave Kleikamp 3172470decc6SDave Kleikamp ret = journal_init_caches(); 3173620de4e1SDuane Griffin if (ret == 0) { 31748e85fb3fSJohann Lombardi jbd2_create_jbd_stats_proc_entry(); 3175620de4e1SDuane Griffin } else { 3176620de4e1SDuane Griffin jbd2_journal_destroy_caches(); 3177620de4e1SDuane Griffin } 3178470decc6SDave Kleikamp return ret; 3179470decc6SDave Kleikamp } 3180470decc6SDave Kleikamp 3181470decc6SDave Kleikamp static void __exit journal_exit(void) 3182470decc6SDave Kleikamp { 3183e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 3184470decc6SDave Kleikamp int n = atomic_read(&nr_journal_heads); 3185470decc6SDave Kleikamp if (n) 318675685071SJan Kara printk(KERN_ERR "JBD2: leaked %d journal_heads!\n", n); 3187470decc6SDave Kleikamp #endif 31888e85fb3fSJohann Lombardi jbd2_remove_jbd_stats_proc_entry(); 3189f7f4bccbSMingming Cao jbd2_journal_destroy_caches(); 3190470decc6SDave Kleikamp } 3191470decc6SDave Kleikamp 3192470decc6SDave Kleikamp MODULE_LICENSE("GPL"); 3193470decc6SDave Kleikamp module_init(journal_init); 3194470decc6SDave Kleikamp module_exit(journal_exit); 3195470decc6SDave Kleikamp 3196