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 52b6e96d00STheodore Ts'o ushort jbd2_journal_enable_debug __read_mostly; 53b6e96d00STheodore Ts'o EXPORT_SYMBOL(jbd2_journal_enable_debug); 54b6e96d00STheodore Ts'o 55b6e96d00STheodore Ts'o module_param_named(jbd2_debug, jbd2_journal_enable_debug, ushort, 0644); 56b6e96d00STheodore Ts'o MODULE_PARM_DESC(jbd2_debug, "Debugging level for jbd2"); 57b6e96d00STheodore Ts'o #endif 58b6e96d00STheodore Ts'o 59f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_extend); 60f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_stop); 61f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_lock_updates); 62f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_unlock_updates); 63f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_get_write_access); 64f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_get_create_access); 65f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_get_undo_access); 66e06c8227SJoel Becker EXPORT_SYMBOL(jbd2_journal_set_triggers); 67f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_dirty_metadata); 68f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_forget); 69f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_flush); 70f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_revoke); 71470decc6SDave Kleikamp 72f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_init_dev); 73f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_init_inode); 74f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_check_used_features); 75f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_check_available_features); 76f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_set_features); 77f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_load); 78f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_destroy); 79f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_abort); 80f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_errno); 81f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_ack_err); 82f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_clear_err); 83f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_log_wait_commit); 843b799d15STheodore Ts'o EXPORT_SYMBOL(jbd2_log_start_commit); 85f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_start_commit); 86f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_force_commit_nested); 87f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_wipe); 88f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_blocks_per_page); 89f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_invalidatepage); 90f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers); 91f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_force_commit); 926ba0e7dcSRoss Zwisler EXPORT_SYMBOL(jbd2_journal_inode_ranged_write); 936ba0e7dcSRoss Zwisler EXPORT_SYMBOL(jbd2_journal_inode_ranged_wait); 94aa3c0c61SMauricio Faria de Oliveira EXPORT_SYMBOL(jbd2_journal_submit_inode_data_buffers); 95aa3c0c61SMauricio Faria de Oliveira EXPORT_SYMBOL(jbd2_journal_finish_inode_data_buffers); 96c851ed54SJan Kara EXPORT_SYMBOL(jbd2_journal_init_jbd_inode); 97c851ed54SJan Kara EXPORT_SYMBOL(jbd2_journal_release_jbd_inode); 98c851ed54SJan Kara EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate); 998aefcd55STheodore Ts'o EXPORT_SYMBOL(jbd2_inode_cache); 100470decc6SDave Kleikamp 101d2eecb03STheodore Ts'o static int jbd2_journal_create_slab(size_t slab_size); 102470decc6SDave Kleikamp 103169f1a2aSPaul Gortmaker #ifdef CONFIG_JBD2_DEBUG 104169f1a2aSPaul Gortmaker void __jbd2_debug(int level, const char *file, const char *func, 105169f1a2aSPaul Gortmaker unsigned int line, const char *fmt, ...) 106169f1a2aSPaul Gortmaker { 107169f1a2aSPaul Gortmaker struct va_format vaf; 108169f1a2aSPaul Gortmaker va_list args; 109169f1a2aSPaul Gortmaker 110169f1a2aSPaul Gortmaker if (level > jbd2_journal_enable_debug) 111169f1a2aSPaul Gortmaker return; 112169f1a2aSPaul Gortmaker va_start(args, fmt); 113169f1a2aSPaul Gortmaker vaf.fmt = fmt; 114169f1a2aSPaul Gortmaker vaf.va = &args; 1159196f571SWang Shilong printk(KERN_DEBUG "%s: (%s, %u): %pV", file, func, line, &vaf); 116169f1a2aSPaul Gortmaker va_end(args); 117169f1a2aSPaul Gortmaker } 118169f1a2aSPaul Gortmaker EXPORT_SYMBOL(__jbd2_debug); 119169f1a2aSPaul Gortmaker #endif 120169f1a2aSPaul Gortmaker 12125ed6e8aSDarrick J. Wong /* Checksumming functions */ 1227747e6d0SRashika Kheria static int jbd2_verify_csum_type(journal_t *j, journal_superblock_t *sb) 12325ed6e8aSDarrick J. Wong { 1248595798cSDarrick J. Wong if (!jbd2_journal_has_csum_v2or3_feature(j)) 12525ed6e8aSDarrick J. Wong return 1; 12625ed6e8aSDarrick J. Wong 12725ed6e8aSDarrick J. Wong return sb->s_checksum_type == JBD2_CRC32C_CHKSUM; 12825ed6e8aSDarrick J. Wong } 12925ed6e8aSDarrick J. Wong 13018a6ea1eSDarrick J. Wong static __be32 jbd2_superblock_csum(journal_t *j, journal_superblock_t *sb) 1314fd5ea43SDarrick J. Wong { 13218a6ea1eSDarrick J. Wong __u32 csum; 13318a6ea1eSDarrick J. Wong __be32 old_csum; 1344fd5ea43SDarrick J. Wong 1354fd5ea43SDarrick J. Wong old_csum = sb->s_checksum; 1364fd5ea43SDarrick J. Wong sb->s_checksum = 0; 1374fd5ea43SDarrick J. Wong csum = jbd2_chksum(j, ~0, (char *)sb, sizeof(journal_superblock_t)); 1384fd5ea43SDarrick J. Wong sb->s_checksum = old_csum; 1394fd5ea43SDarrick J. Wong 1404fd5ea43SDarrick J. Wong return cpu_to_be32(csum); 1414fd5ea43SDarrick J. Wong } 1424fd5ea43SDarrick J. Wong 143470decc6SDave Kleikamp /* 144470decc6SDave Kleikamp * Helper function used to manage commit timeouts 145470decc6SDave Kleikamp */ 146470decc6SDave Kleikamp 147e3c95788SKees Cook static void commit_timeout(struct timer_list *t) 148470decc6SDave Kleikamp { 149e3c95788SKees Cook journal_t *journal = from_timer(journal, t, j_commit_timer); 150470decc6SDave Kleikamp 151e3c95788SKees Cook wake_up_process(journal->j_task); 152470decc6SDave Kleikamp } 153470decc6SDave Kleikamp 154470decc6SDave Kleikamp /* 155f7f4bccbSMingming Cao * kjournald2: The main thread function used to manage a logging device 156470decc6SDave Kleikamp * journal. 157470decc6SDave Kleikamp * 158470decc6SDave Kleikamp * This kernel thread is responsible for two things: 159470decc6SDave Kleikamp * 160470decc6SDave Kleikamp * 1) COMMIT: Every so often we need to commit the current state of the 161470decc6SDave Kleikamp * filesystem to disk. The journal thread is responsible for writing 162ff780b91SHarshad Shirwadkar * all of the metadata buffers to disk. If a fast commit is ongoing 163ff780b91SHarshad Shirwadkar * journal thread waits until it's done and then continues from 164ff780b91SHarshad Shirwadkar * there on. 165470decc6SDave Kleikamp * 166470decc6SDave Kleikamp * 2) CHECKPOINT: We cannot reuse a used section of the log file until all 167470decc6SDave Kleikamp * of the data in that part of the log has been rewritten elsewhere on 168470decc6SDave Kleikamp * the disk. Flushing these old buffers to reclaim space in the log is 169470decc6SDave Kleikamp * known as checkpointing, and this thread is responsible for that job. 170470decc6SDave Kleikamp */ 171470decc6SDave Kleikamp 172f7f4bccbSMingming Cao static int kjournald2(void *arg) 173470decc6SDave Kleikamp { 174470decc6SDave Kleikamp journal_t *journal = arg; 175470decc6SDave Kleikamp transaction_t *transaction; 176470decc6SDave Kleikamp 177470decc6SDave Kleikamp /* 178470decc6SDave Kleikamp * Set up an interval timer which can be used to trigger a commit wakeup 179470decc6SDave Kleikamp * after the commit interval expires 180470decc6SDave Kleikamp */ 181e3c95788SKees Cook timer_setup(&journal->j_commit_timer, commit_timeout, 0); 182470decc6SDave Kleikamp 18335c80422SNigel Cunningham set_freezable(); 18435c80422SNigel Cunningham 185470decc6SDave Kleikamp /* Record that the journal thread is running */ 186470decc6SDave Kleikamp journal->j_task = current; 187470decc6SDave Kleikamp wake_up(&journal->j_wait_done_commit); 188470decc6SDave Kleikamp 189470decc6SDave Kleikamp /* 190eb52da3fSMichal Hocko * Make sure that no allocations from this kernel thread will ever 191eb52da3fSMichal Hocko * recurse to the fs layer because we are responsible for the 192eb52da3fSMichal Hocko * transaction commit and any fs involvement might get stuck waiting for 193eb52da3fSMichal Hocko * the trasn. commit. 194eb52da3fSMichal Hocko */ 195eb52da3fSMichal Hocko memalloc_nofs_save(); 196eb52da3fSMichal Hocko 197eb52da3fSMichal Hocko /* 198470decc6SDave Kleikamp * And now, wait forever for commit wakeup events. 199470decc6SDave Kleikamp */ 200a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 201470decc6SDave Kleikamp 202470decc6SDave Kleikamp loop: 203f7f4bccbSMingming Cao if (journal->j_flags & JBD2_UNMOUNT) 204470decc6SDave Kleikamp goto end_loop; 205470decc6SDave Kleikamp 2067821ce41SGaowei Pu jbd_debug(1, "commit_sequence=%u, commit_request=%u\n", 207470decc6SDave Kleikamp journal->j_commit_sequence, journal->j_commit_request); 208470decc6SDave Kleikamp 209470decc6SDave Kleikamp if (journal->j_commit_sequence != journal->j_commit_request) { 210470decc6SDave Kleikamp jbd_debug(1, "OK, requests differ\n"); 211a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 212470decc6SDave Kleikamp del_timer_sync(&journal->j_commit_timer); 213f7f4bccbSMingming Cao jbd2_journal_commit_transaction(journal); 214a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 215470decc6SDave Kleikamp goto loop; 216470decc6SDave Kleikamp } 217470decc6SDave Kleikamp 218470decc6SDave Kleikamp wake_up(&journal->j_wait_done_commit); 219470decc6SDave Kleikamp if (freezing(current)) { 220470decc6SDave Kleikamp /* 221470decc6SDave Kleikamp * The simpler the better. Flushing journal isn't a 222470decc6SDave Kleikamp * good idea, because that depends on threads that may 223470decc6SDave Kleikamp * be already stopped. 224470decc6SDave Kleikamp */ 225f7f4bccbSMingming Cao jbd_debug(1, "Now suspending kjournald2\n"); 226a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 227a0acae0eSTejun Heo try_to_freeze(); 228a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 229470decc6SDave Kleikamp } else { 230470decc6SDave Kleikamp /* 231470decc6SDave Kleikamp * We assume on resume that commits are already there, 232470decc6SDave Kleikamp * so we don't sleep 233470decc6SDave Kleikamp */ 234470decc6SDave Kleikamp DEFINE_WAIT(wait); 235470decc6SDave Kleikamp int should_sleep = 1; 236470decc6SDave Kleikamp 237470decc6SDave Kleikamp prepare_to_wait(&journal->j_wait_commit, &wait, 238470decc6SDave Kleikamp TASK_INTERRUPTIBLE); 239470decc6SDave Kleikamp if (journal->j_commit_sequence != journal->j_commit_request) 240470decc6SDave Kleikamp should_sleep = 0; 241470decc6SDave Kleikamp transaction = journal->j_running_transaction; 242470decc6SDave Kleikamp if (transaction && time_after_eq(jiffies, 243470decc6SDave Kleikamp transaction->t_expires)) 244470decc6SDave Kleikamp should_sleep = 0; 245f7f4bccbSMingming Cao if (journal->j_flags & JBD2_UNMOUNT) 246470decc6SDave Kleikamp should_sleep = 0; 247470decc6SDave Kleikamp if (should_sleep) { 248a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 249470decc6SDave Kleikamp schedule(); 250a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 251470decc6SDave Kleikamp } 252470decc6SDave Kleikamp finish_wait(&journal->j_wait_commit, &wait); 253470decc6SDave Kleikamp } 254470decc6SDave Kleikamp 255f7f4bccbSMingming Cao jbd_debug(1, "kjournald2 wakes\n"); 256470decc6SDave Kleikamp 257470decc6SDave Kleikamp /* 258470decc6SDave Kleikamp * Were we woken up by a commit wakeup event? 259470decc6SDave Kleikamp */ 260470decc6SDave Kleikamp transaction = journal->j_running_transaction; 261470decc6SDave Kleikamp if (transaction && time_after_eq(jiffies, transaction->t_expires)) { 262470decc6SDave Kleikamp journal->j_commit_request = transaction->t_tid; 263470decc6SDave Kleikamp jbd_debug(1, "woke because of timeout\n"); 264470decc6SDave Kleikamp } 265470decc6SDave Kleikamp goto loop; 266470decc6SDave Kleikamp 267470decc6SDave Kleikamp end_loop: 268470decc6SDave Kleikamp del_timer_sync(&journal->j_commit_timer); 269470decc6SDave Kleikamp journal->j_task = NULL; 270470decc6SDave Kleikamp wake_up(&journal->j_wait_done_commit); 271470decc6SDave Kleikamp jbd_debug(1, "Journal thread exiting.\n"); 272dbfcef6bSSahitya Tummala write_unlock(&journal->j_state_lock); 273470decc6SDave Kleikamp return 0; 274470decc6SDave Kleikamp } 275470decc6SDave Kleikamp 27697f06784SPavel Emelianov static int jbd2_journal_start_thread(journal_t *journal) 277470decc6SDave Kleikamp { 27897f06784SPavel Emelianov struct task_struct *t; 27997f06784SPavel Emelianov 28090576c0bSTheodore Ts'o t = kthread_run(kjournald2, journal, "jbd2/%s", 28190576c0bSTheodore Ts'o journal->j_devname); 28297f06784SPavel Emelianov if (IS_ERR(t)) 28397f06784SPavel Emelianov return PTR_ERR(t); 28497f06784SPavel Emelianov 2851076d17aSAl Viro wait_event(journal->j_wait_done_commit, journal->j_task != NULL); 28697f06784SPavel Emelianov return 0; 287470decc6SDave Kleikamp } 288470decc6SDave Kleikamp 289470decc6SDave Kleikamp static void journal_kill_thread(journal_t *journal) 290470decc6SDave Kleikamp { 291a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 292f7f4bccbSMingming Cao journal->j_flags |= JBD2_UNMOUNT; 293470decc6SDave Kleikamp 294470decc6SDave Kleikamp while (journal->j_task) { 295a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 2963469a32aSTheodore Ts'o wake_up(&journal->j_wait_commit); 2971076d17aSAl Viro wait_event(journal->j_wait_done_commit, journal->j_task == NULL); 298a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 299470decc6SDave Kleikamp } 300a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 301470decc6SDave Kleikamp } 302470decc6SDave Kleikamp 303470decc6SDave Kleikamp /* 304f7f4bccbSMingming Cao * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal. 305470decc6SDave Kleikamp * 306470decc6SDave Kleikamp * Writes a metadata buffer to a given disk block. The actual IO is not 307470decc6SDave Kleikamp * performed but a new buffer_head is constructed which labels the data 308470decc6SDave Kleikamp * to be written with the correct destination disk block. 309470decc6SDave Kleikamp * 310470decc6SDave Kleikamp * Any magic-number escaping which needs to be done will cause a 311470decc6SDave Kleikamp * copy-out here. If the buffer happens to start with the 312f7f4bccbSMingming Cao * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the 313470decc6SDave Kleikamp * magic number is only written to the log for descripter blocks. In 314470decc6SDave Kleikamp * this case, we copy the data and replace the first word with 0, and we 315470decc6SDave Kleikamp * return a result code which indicates that this buffer needs to be 316470decc6SDave Kleikamp * marked as an escaped buffer in the corresponding log descriptor 317470decc6SDave Kleikamp * block. The missing word can then be restored when the block is read 318470decc6SDave Kleikamp * during recovery. 319470decc6SDave Kleikamp * 320470decc6SDave Kleikamp * If the source buffer has already been modified by a new transaction 321470decc6SDave Kleikamp * since we took the last commit snapshot, we use the frozen copy of 322470decc6SDave Kleikamp * that data for IO. If we end up using the existing buffer_head's data 323f5113effSJan Kara * for the write, then we have to make sure nobody modifies it while the 324f5113effSJan Kara * IO is in progress. do_get_write_access() handles this. 325470decc6SDave Kleikamp * 326f5113effSJan Kara * The function returns a pointer to the buffer_head to be used for IO. 327470decc6SDave Kleikamp * 328470decc6SDave Kleikamp * 329470decc6SDave Kleikamp * Return value: 330470decc6SDave Kleikamp * <0: Error 331470decc6SDave Kleikamp * >=0: Finished OK 332470decc6SDave Kleikamp * 333470decc6SDave Kleikamp * On success: 334470decc6SDave Kleikamp * Bit 0 set == escape performed on the data 335470decc6SDave Kleikamp * Bit 1 set == buffer copy-out performed (kfree the data after IO) 336470decc6SDave Kleikamp */ 337470decc6SDave Kleikamp 338f7f4bccbSMingming Cao int jbd2_journal_write_metadata_buffer(transaction_t *transaction, 339470decc6SDave Kleikamp struct journal_head *jh_in, 340f5113effSJan Kara struct buffer_head **bh_out, 341f5113effSJan Kara sector_t blocknr) 342470decc6SDave Kleikamp { 343470decc6SDave Kleikamp int need_copy_out = 0; 344470decc6SDave Kleikamp int done_copy_out = 0; 345470decc6SDave Kleikamp int do_escape = 0; 346470decc6SDave Kleikamp char *mapped_data; 347470decc6SDave Kleikamp struct buffer_head *new_bh; 348470decc6SDave Kleikamp struct page *new_page; 349470decc6SDave Kleikamp unsigned int new_offset; 350470decc6SDave Kleikamp struct buffer_head *bh_in = jh2bh(jh_in); 35196577c43Sdingdinghua journal_t *journal = transaction->t_journal; 352470decc6SDave Kleikamp 353470decc6SDave Kleikamp /* 354470decc6SDave Kleikamp * The buffer really shouldn't be locked: only the current committing 355470decc6SDave Kleikamp * transaction is allowed to write it, so nobody else is allowed 356470decc6SDave Kleikamp * to do any IO. 357470decc6SDave Kleikamp * 358470decc6SDave Kleikamp * akpm: except if we're journalling data, and write() output is 359470decc6SDave Kleikamp * also part of a shared mapping, and another thread has 360470decc6SDave Kleikamp * decided to launch a writepage() against this buffer. 361470decc6SDave Kleikamp */ 362470decc6SDave Kleikamp J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in)); 363470decc6SDave Kleikamp 3646ccaf3e2SMichal Hocko new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL); 36547def826STheodore Ts'o 36696577c43Sdingdinghua /* keep subsequent assertions sane */ 36796577c43Sdingdinghua atomic_set(&new_bh->b_count, 1); 368470decc6SDave Kleikamp 36946417064SThomas Gleixner spin_lock(&jh_in->b_state_lock); 370f5113effSJan Kara repeat: 371470decc6SDave Kleikamp /* 372470decc6SDave Kleikamp * If a new transaction has already done a buffer copy-out, then 373470decc6SDave Kleikamp * we use that version of the data for the commit. 374470decc6SDave Kleikamp */ 375470decc6SDave Kleikamp if (jh_in->b_frozen_data) { 376470decc6SDave Kleikamp done_copy_out = 1; 377470decc6SDave Kleikamp new_page = virt_to_page(jh_in->b_frozen_data); 378470decc6SDave Kleikamp new_offset = offset_in_page(jh_in->b_frozen_data); 379470decc6SDave Kleikamp } else { 380470decc6SDave Kleikamp new_page = jh2bh(jh_in)->b_page; 381470decc6SDave Kleikamp new_offset = offset_in_page(jh2bh(jh_in)->b_data); 382470decc6SDave Kleikamp } 383470decc6SDave Kleikamp 384303a8f2aSCong Wang mapped_data = kmap_atomic(new_page); 385470decc6SDave Kleikamp /* 38613ceef09SJan Kara * Fire data frozen trigger if data already wasn't frozen. Do this 38713ceef09SJan Kara * before checking for escaping, as the trigger may modify the magic 38813ceef09SJan Kara * offset. If a copy-out happens afterwards, it will have the correct 38913ceef09SJan Kara * data in the buffer. 390e06c8227SJoel Becker */ 39113ceef09SJan Kara if (!done_copy_out) 39213ceef09SJan Kara jbd2_buffer_frozen_trigger(jh_in, mapped_data + new_offset, 39313ceef09SJan Kara jh_in->b_triggers); 394e06c8227SJoel Becker 395e06c8227SJoel Becker /* 396470decc6SDave Kleikamp * Check for escaping 397470decc6SDave Kleikamp */ 398470decc6SDave Kleikamp if (*((__be32 *)(mapped_data + new_offset)) == 399f7f4bccbSMingming Cao cpu_to_be32(JBD2_MAGIC_NUMBER)) { 400470decc6SDave Kleikamp need_copy_out = 1; 401470decc6SDave Kleikamp do_escape = 1; 402470decc6SDave Kleikamp } 403303a8f2aSCong Wang kunmap_atomic(mapped_data); 404470decc6SDave Kleikamp 405470decc6SDave Kleikamp /* 406470decc6SDave Kleikamp * Do we need to do a data copy? 407470decc6SDave Kleikamp */ 408470decc6SDave Kleikamp if (need_copy_out && !done_copy_out) { 409470decc6SDave Kleikamp char *tmp; 410470decc6SDave Kleikamp 41146417064SThomas Gleixner spin_unlock(&jh_in->b_state_lock); 412af1e76d6SMingming Cao tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS); 413e6ec116bSTheodore Ts'o if (!tmp) { 414f5113effSJan Kara brelse(new_bh); 415e6ec116bSTheodore Ts'o return -ENOMEM; 416e6ec116bSTheodore Ts'o } 41746417064SThomas Gleixner spin_lock(&jh_in->b_state_lock); 418470decc6SDave Kleikamp if (jh_in->b_frozen_data) { 419af1e76d6SMingming Cao jbd2_free(tmp, bh_in->b_size); 420470decc6SDave Kleikamp goto repeat; 421470decc6SDave Kleikamp } 422470decc6SDave Kleikamp 423470decc6SDave Kleikamp jh_in->b_frozen_data = tmp; 424303a8f2aSCong Wang mapped_data = kmap_atomic(new_page); 425f5113effSJan Kara memcpy(tmp, mapped_data + new_offset, bh_in->b_size); 426303a8f2aSCong Wang kunmap_atomic(mapped_data); 427470decc6SDave Kleikamp 428470decc6SDave Kleikamp new_page = virt_to_page(tmp); 429470decc6SDave Kleikamp new_offset = offset_in_page(tmp); 430470decc6SDave Kleikamp done_copy_out = 1; 431e06c8227SJoel Becker 432e06c8227SJoel Becker /* 433e06c8227SJoel Becker * This isn't strictly necessary, as we're using frozen 434e06c8227SJoel Becker * data for the escaping, but it keeps consistency with 435e06c8227SJoel Becker * b_frozen_data usage. 436e06c8227SJoel Becker */ 437e06c8227SJoel Becker jh_in->b_frozen_triggers = jh_in->b_triggers; 438470decc6SDave Kleikamp } 439470decc6SDave Kleikamp 440470decc6SDave Kleikamp /* 441470decc6SDave Kleikamp * Did we need to do an escaping? Now we've done all the 442470decc6SDave Kleikamp * copying, we can finally do so. 443470decc6SDave Kleikamp */ 444470decc6SDave Kleikamp if (do_escape) { 445303a8f2aSCong Wang mapped_data = kmap_atomic(new_page); 446470decc6SDave Kleikamp *((unsigned int *)(mapped_data + new_offset)) = 0; 447303a8f2aSCong Wang kunmap_atomic(mapped_data); 448470decc6SDave Kleikamp } 449470decc6SDave Kleikamp 450470decc6SDave Kleikamp set_bh_page(new_bh, new_page, new_offset); 451f5113effSJan Kara new_bh->b_size = bh_in->b_size; 452f5113effSJan Kara new_bh->b_bdev = journal->j_dev; 453470decc6SDave Kleikamp new_bh->b_blocknr = blocknr; 454b34090e5SJan Kara new_bh->b_private = bh_in; 455470decc6SDave Kleikamp set_buffer_mapped(new_bh); 456470decc6SDave Kleikamp set_buffer_dirty(new_bh); 457470decc6SDave Kleikamp 458f5113effSJan Kara *bh_out = new_bh; 459470decc6SDave Kleikamp 460470decc6SDave Kleikamp /* 461470decc6SDave Kleikamp * The to-be-written buffer needs to get moved to the io queue, 462470decc6SDave Kleikamp * and the original buffer whose contents we are shadowing or 463470decc6SDave Kleikamp * copying is moved to the transaction's shadow queue. 464470decc6SDave Kleikamp */ 465470decc6SDave Kleikamp JBUFFER_TRACE(jh_in, "file as BJ_Shadow"); 46696577c43Sdingdinghua spin_lock(&journal->j_list_lock); 46796577c43Sdingdinghua __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow); 46896577c43Sdingdinghua spin_unlock(&journal->j_list_lock); 469b34090e5SJan Kara set_buffer_shadow(bh_in); 47046417064SThomas Gleixner spin_unlock(&jh_in->b_state_lock); 47196577c43Sdingdinghua 472470decc6SDave Kleikamp return do_escape | (done_copy_out << 1); 473470decc6SDave Kleikamp } 474470decc6SDave Kleikamp 475470decc6SDave Kleikamp /* 476470decc6SDave Kleikamp * Allocation code for the journal file. Manage the space left in the 477470decc6SDave Kleikamp * journal, so that we can begin checkpointing when appropriate. 478470decc6SDave Kleikamp */ 479470decc6SDave Kleikamp 480470decc6SDave Kleikamp /* 481e4471831STheodore Ts'o * Called with j_state_lock locked for writing. 482e4471831STheodore Ts'o * Returns true if a transaction commit was started. 483470decc6SDave Kleikamp */ 484f7f4bccbSMingming Cao int __jbd2_log_start_commit(journal_t *journal, tid_t target) 485470decc6SDave Kleikamp { 486e7b04ac0SEric Sandeen /* Return if the txn has already requested to be committed */ 487e7b04ac0SEric Sandeen if (journal->j_commit_request == target) 488e7b04ac0SEric Sandeen return 0; 489e7b04ac0SEric Sandeen 490470decc6SDave Kleikamp /* 491deeeaf13STheodore Ts'o * The only transaction we can possibly wait upon is the 492deeeaf13STheodore Ts'o * currently running transaction (if it exists). Otherwise, 493deeeaf13STheodore Ts'o * the target tid must be an old one. 494470decc6SDave Kleikamp */ 495deeeaf13STheodore Ts'o if (journal->j_running_transaction && 496deeeaf13STheodore Ts'o journal->j_running_transaction->t_tid == target) { 497470decc6SDave Kleikamp /* 498bcf3d0bcSAndrea Gelmini * We want a new commit: OK, mark the request and wakeup the 499470decc6SDave Kleikamp * commit thread. We do _not_ do the commit ourselves. 500470decc6SDave Kleikamp */ 501470decc6SDave Kleikamp 502470decc6SDave Kleikamp journal->j_commit_request = target; 5037821ce41SGaowei Pu jbd_debug(1, "JBD2: requesting commit %u/%u\n", 504470decc6SDave Kleikamp journal->j_commit_request, 505470decc6SDave Kleikamp journal->j_commit_sequence); 5069fff24aaSTheodore Ts'o journal->j_running_transaction->t_requested = jiffies; 507470decc6SDave Kleikamp wake_up(&journal->j_wait_commit); 508470decc6SDave Kleikamp return 1; 509deeeaf13STheodore Ts'o } else if (!tid_geq(journal->j_commit_request, target)) 510deeeaf13STheodore Ts'o /* This should never happen, but if it does, preserve 511deeeaf13STheodore Ts'o the evidence before kjournald goes into a loop and 512deeeaf13STheodore Ts'o increments j_commit_sequence beyond all recognition. */ 513f2a44523SEryu Guan WARN_ONCE(1, "JBD2: bad log_start_commit: %u %u %u %u\n", 5141be2add6STheodore Ts'o journal->j_commit_request, 5151be2add6STheodore Ts'o journal->j_commit_sequence, 516deeeaf13STheodore Ts'o target, journal->j_running_transaction ? 517deeeaf13STheodore Ts'o journal->j_running_transaction->t_tid : 0); 518470decc6SDave Kleikamp return 0; 519470decc6SDave Kleikamp } 520470decc6SDave Kleikamp 521f7f4bccbSMingming Cao int jbd2_log_start_commit(journal_t *journal, tid_t tid) 522470decc6SDave Kleikamp { 523470decc6SDave Kleikamp int ret; 524470decc6SDave Kleikamp 525a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 526f7f4bccbSMingming Cao ret = __jbd2_log_start_commit(journal, tid); 527a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 528470decc6SDave Kleikamp return ret; 529470decc6SDave Kleikamp } 530470decc6SDave Kleikamp 531470decc6SDave Kleikamp /* 5329ff86446SDmitry Monakhov * Force and wait any uncommitted transactions. We can only force the running 5339ff86446SDmitry Monakhov * transaction if we don't have an active handle, otherwise, we will deadlock. 5349ff86446SDmitry Monakhov * Returns: <0 in case of error, 5359ff86446SDmitry Monakhov * 0 if nothing to commit, 5369ff86446SDmitry Monakhov * 1 if transaction was successfully committed. 537470decc6SDave Kleikamp */ 5389ff86446SDmitry Monakhov static int __jbd2_journal_force_commit(journal_t *journal) 539470decc6SDave Kleikamp { 540470decc6SDave Kleikamp transaction_t *transaction = NULL; 541470decc6SDave Kleikamp tid_t tid; 5429ff86446SDmitry Monakhov int need_to_start = 0, ret = 0; 543470decc6SDave Kleikamp 544a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 545470decc6SDave Kleikamp if (journal->j_running_transaction && !current->journal_info) { 546470decc6SDave Kleikamp transaction = journal->j_running_transaction; 547e4471831STheodore Ts'o if (!tid_geq(journal->j_commit_request, transaction->t_tid)) 548e4471831STheodore Ts'o need_to_start = 1; 549470decc6SDave Kleikamp } else if (journal->j_committing_transaction) 550470decc6SDave Kleikamp transaction = journal->j_committing_transaction; 551470decc6SDave Kleikamp 552470decc6SDave Kleikamp if (!transaction) { 5539ff86446SDmitry Monakhov /* Nothing to commit */ 554a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 5559ff86446SDmitry Monakhov return 0; 556470decc6SDave Kleikamp } 557470decc6SDave Kleikamp tid = transaction->t_tid; 558a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 559e4471831STheodore Ts'o if (need_to_start) 560e4471831STheodore Ts'o jbd2_log_start_commit(journal, tid); 5619ff86446SDmitry Monakhov ret = jbd2_log_wait_commit(journal, tid); 5629ff86446SDmitry Monakhov if (!ret) 5639ff86446SDmitry Monakhov ret = 1; 5649ff86446SDmitry Monakhov 5659ff86446SDmitry Monakhov return ret; 5669ff86446SDmitry Monakhov } 5679ff86446SDmitry Monakhov 5689ff86446SDmitry Monakhov /** 5692bf31d94SMauro Carvalho Chehab * jbd2_journal_force_commit_nested - Force and wait upon a commit if the 5702bf31d94SMauro Carvalho Chehab * calling process is not within transaction. 5719ff86446SDmitry Monakhov * 5729ff86446SDmitry Monakhov * @journal: journal to force 5739ff86446SDmitry Monakhov * Returns true if progress was made. 5742bf31d94SMauro Carvalho Chehab * 5752bf31d94SMauro Carvalho Chehab * This is used for forcing out undo-protected data which contains 5762bf31d94SMauro Carvalho Chehab * bitmaps, when the fs is running out of space. 5779ff86446SDmitry Monakhov */ 5789ff86446SDmitry Monakhov int jbd2_journal_force_commit_nested(journal_t *journal) 5799ff86446SDmitry Monakhov { 5809ff86446SDmitry Monakhov int ret; 5819ff86446SDmitry Monakhov 5829ff86446SDmitry Monakhov ret = __jbd2_journal_force_commit(journal); 5839ff86446SDmitry Monakhov return ret > 0; 5849ff86446SDmitry Monakhov } 5859ff86446SDmitry Monakhov 5869ff86446SDmitry Monakhov /** 5872bf31d94SMauro Carvalho Chehab * jbd2_journal_force_commit() - force any uncommitted transactions 5889ff86446SDmitry Monakhov * @journal: journal to force 5899ff86446SDmitry Monakhov * 5909ff86446SDmitry Monakhov * Caller want unconditional commit. We can only force the running transaction 5919ff86446SDmitry Monakhov * if we don't have an active handle, otherwise, we will deadlock. 5929ff86446SDmitry Monakhov */ 5939ff86446SDmitry Monakhov int jbd2_journal_force_commit(journal_t *journal) 5949ff86446SDmitry Monakhov { 5959ff86446SDmitry Monakhov int ret; 5969ff86446SDmitry Monakhov 5979ff86446SDmitry Monakhov J_ASSERT(!current->journal_info); 5989ff86446SDmitry Monakhov ret = __jbd2_journal_force_commit(journal); 5999ff86446SDmitry Monakhov if (ret > 0) 6009ff86446SDmitry Monakhov ret = 0; 6019ff86446SDmitry Monakhov return ret; 602470decc6SDave Kleikamp } 603470decc6SDave Kleikamp 604470decc6SDave Kleikamp /* 605470decc6SDave Kleikamp * Start a commit of the current running transaction (if any). Returns true 606c88ccea3SJan Kara * if a transaction is going to be committed (or is currently already 607c88ccea3SJan Kara * committing), and fills its tid in at *ptid 608470decc6SDave Kleikamp */ 609f7f4bccbSMingming Cao int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid) 610470decc6SDave Kleikamp { 611470decc6SDave Kleikamp int ret = 0; 612470decc6SDave Kleikamp 613a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 614470decc6SDave Kleikamp if (journal->j_running_transaction) { 615470decc6SDave Kleikamp tid_t tid = journal->j_running_transaction->t_tid; 616470decc6SDave Kleikamp 617c88ccea3SJan Kara __jbd2_log_start_commit(journal, tid); 618c88ccea3SJan Kara /* There's a running transaction and we've just made sure 619c88ccea3SJan Kara * it's commit has been scheduled. */ 620c88ccea3SJan Kara if (ptid) 621470decc6SDave Kleikamp *ptid = tid; 622c88ccea3SJan Kara ret = 1; 623c88ccea3SJan Kara } else if (journal->j_committing_transaction) { 624470decc6SDave Kleikamp /* 62512810ad7SArtem Bityutskiy * If commit has been started, then we have to wait for 62612810ad7SArtem Bityutskiy * completion of that transaction. 627470decc6SDave Kleikamp */ 628c88ccea3SJan Kara if (ptid) 629470decc6SDave Kleikamp *ptid = journal->j_committing_transaction->t_tid; 630470decc6SDave Kleikamp ret = 1; 631470decc6SDave Kleikamp } 632a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 633470decc6SDave Kleikamp return ret; 634470decc6SDave Kleikamp } 635470decc6SDave Kleikamp 636470decc6SDave Kleikamp /* 637bbd2be36SJan Kara * Return 1 if a given transaction has not yet sent barrier request 638bbd2be36SJan Kara * connected with a transaction commit. If 0 is returned, transaction 639bbd2be36SJan Kara * may or may not have sent the barrier. Used to avoid sending barrier 640bbd2be36SJan Kara * twice in common cases. 641bbd2be36SJan Kara */ 642bbd2be36SJan Kara int jbd2_trans_will_send_data_barrier(journal_t *journal, tid_t tid) 643bbd2be36SJan Kara { 644bbd2be36SJan Kara int ret = 0; 645bbd2be36SJan Kara transaction_t *commit_trans; 646bbd2be36SJan Kara 647bbd2be36SJan Kara if (!(journal->j_flags & JBD2_BARRIER)) 648bbd2be36SJan Kara return 0; 649bbd2be36SJan Kara read_lock(&journal->j_state_lock); 650bbd2be36SJan Kara /* Transaction already committed? */ 651bbd2be36SJan Kara if (tid_geq(journal->j_commit_sequence, tid)) 652bbd2be36SJan Kara goto out; 653bbd2be36SJan Kara commit_trans = journal->j_committing_transaction; 654bbd2be36SJan Kara if (!commit_trans || commit_trans->t_tid != tid) { 655bbd2be36SJan Kara ret = 1; 656bbd2be36SJan Kara goto out; 657bbd2be36SJan Kara } 658bbd2be36SJan Kara /* 659bbd2be36SJan Kara * Transaction is being committed and we already proceeded to 660bbd2be36SJan Kara * submitting a flush to fs partition? 661bbd2be36SJan Kara */ 662bbd2be36SJan Kara if (journal->j_fs_dev != journal->j_dev) { 663bbd2be36SJan Kara if (!commit_trans->t_need_data_flush || 664bbd2be36SJan Kara commit_trans->t_state >= T_COMMIT_DFLUSH) 665bbd2be36SJan Kara goto out; 666bbd2be36SJan Kara } else { 667bbd2be36SJan Kara if (commit_trans->t_state >= T_COMMIT_JFLUSH) 668bbd2be36SJan Kara goto out; 669bbd2be36SJan Kara } 670bbd2be36SJan Kara ret = 1; 671bbd2be36SJan Kara out: 672bbd2be36SJan Kara read_unlock(&journal->j_state_lock); 673bbd2be36SJan Kara return ret; 674bbd2be36SJan Kara } 675bbd2be36SJan Kara EXPORT_SYMBOL(jbd2_trans_will_send_data_barrier); 676bbd2be36SJan Kara 677bbd2be36SJan Kara /* 678470decc6SDave Kleikamp * Wait for a specified commit to complete. 679470decc6SDave Kleikamp * The caller may not hold the journal lock. 680470decc6SDave Kleikamp */ 681f7f4bccbSMingming Cao int jbd2_log_wait_commit(journal_t *journal, tid_t tid) 682470decc6SDave Kleikamp { 683470decc6SDave Kleikamp int err = 0; 684470decc6SDave Kleikamp 685c52c47e4SJan Kara read_lock(&journal->j_state_lock); 686c52c47e4SJan Kara #ifdef CONFIG_PROVE_LOCKING 687c52c47e4SJan Kara /* 688c52c47e4SJan Kara * Some callers make sure transaction is already committing and in that 689c52c47e4SJan Kara * case we cannot block on open handles anymore. So don't warn in that 690c52c47e4SJan Kara * case. 691c52c47e4SJan Kara */ 692c52c47e4SJan Kara if (tid_gt(tid, journal->j_commit_sequence) && 693c52c47e4SJan Kara (!journal->j_committing_transaction || 694c52c47e4SJan Kara journal->j_committing_transaction->t_tid != tid)) { 695c52c47e4SJan Kara read_unlock(&journal->j_state_lock); 6961eaa566dSJan Kara jbd2_might_wait_for_commit(journal); 697a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 698c52c47e4SJan Kara } 699c52c47e4SJan Kara #endif 700e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 701470decc6SDave Kleikamp if (!tid_geq(journal->j_commit_request, tid)) { 70275685071SJan Kara printk(KERN_ERR 7037821ce41SGaowei Pu "%s: error: j_commit_request=%u, tid=%u\n", 704329d291fSHarvey Harrison __func__, journal->j_commit_request, tid); 705470decc6SDave Kleikamp } 706470decc6SDave Kleikamp #endif 707470decc6SDave Kleikamp while (tid_gt(tid, journal->j_commit_sequence)) { 7087821ce41SGaowei Pu jbd_debug(1, "JBD2: want %u, j_commit_sequence=%u\n", 709470decc6SDave Kleikamp tid, journal->j_commit_sequence); 710a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 7113469a32aSTheodore Ts'o wake_up(&journal->j_wait_commit); 712470decc6SDave Kleikamp wait_event(journal->j_wait_done_commit, 713470decc6SDave Kleikamp !tid_gt(tid, journal->j_commit_sequence)); 714a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 715470decc6SDave Kleikamp } 716a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 717470decc6SDave Kleikamp 71875685071SJan Kara if (unlikely(is_journal_aborted(journal))) 719470decc6SDave Kleikamp err = -EIO; 720470decc6SDave Kleikamp return err; 721470decc6SDave Kleikamp } 722470decc6SDave Kleikamp 723ff780b91SHarshad Shirwadkar /* 724ff780b91SHarshad Shirwadkar * Start a fast commit. If there's an ongoing fast or full commit wait for 725ff780b91SHarshad Shirwadkar * it to complete. Returns 0 if a new fast commit was started. Returns -EALREADY 726ff780b91SHarshad Shirwadkar * if a fast commit is not needed, either because there's an already a commit 727ff780b91SHarshad Shirwadkar * going on or this tid has already been committed. Returns -EINVAL if no jbd2 728ff780b91SHarshad Shirwadkar * commit has yet been performed. 729ff780b91SHarshad Shirwadkar */ 730ff780b91SHarshad Shirwadkar int jbd2_fc_begin_commit(journal_t *journal, tid_t tid) 731ff780b91SHarshad Shirwadkar { 73287a144f0SHarshad Shirwadkar if (unlikely(is_journal_aborted(journal))) 73387a144f0SHarshad Shirwadkar return -EIO; 734ff780b91SHarshad Shirwadkar /* 735ff780b91SHarshad Shirwadkar * Fast commits only allowed if at least one full commit has 736ff780b91SHarshad Shirwadkar * been processed. 737ff780b91SHarshad Shirwadkar */ 738ff780b91SHarshad Shirwadkar if (!journal->j_stats.ts_tid) 739ff780b91SHarshad Shirwadkar return -EINVAL; 740ff780b91SHarshad Shirwadkar 741ff780b91SHarshad Shirwadkar write_lock(&journal->j_state_lock); 742480f89d5SHarshad Shirwadkar if (tid <= journal->j_commit_sequence) { 743480f89d5SHarshad Shirwadkar write_unlock(&journal->j_state_lock); 744480f89d5SHarshad Shirwadkar return -EALREADY; 745480f89d5SHarshad Shirwadkar } 746480f89d5SHarshad Shirwadkar 747ff780b91SHarshad Shirwadkar if (journal->j_flags & JBD2_FULL_COMMIT_ONGOING || 748ff780b91SHarshad Shirwadkar (journal->j_flags & JBD2_FAST_COMMIT_ONGOING)) { 749ff780b91SHarshad Shirwadkar DEFINE_WAIT(wait); 750ff780b91SHarshad Shirwadkar 751ff780b91SHarshad Shirwadkar prepare_to_wait(&journal->j_fc_wait, &wait, 752ff780b91SHarshad Shirwadkar TASK_UNINTERRUPTIBLE); 753ff780b91SHarshad Shirwadkar write_unlock(&journal->j_state_lock); 754ff780b91SHarshad Shirwadkar schedule(); 755ff780b91SHarshad Shirwadkar finish_wait(&journal->j_fc_wait, &wait); 756ff780b91SHarshad Shirwadkar return -EALREADY; 757ff780b91SHarshad Shirwadkar } 758ff780b91SHarshad Shirwadkar journal->j_flags |= JBD2_FAST_COMMIT_ONGOING; 759ff780b91SHarshad Shirwadkar write_unlock(&journal->j_state_lock); 760ff780b91SHarshad Shirwadkar 761ff780b91SHarshad Shirwadkar return 0; 762ff780b91SHarshad Shirwadkar } 763ff780b91SHarshad Shirwadkar EXPORT_SYMBOL(jbd2_fc_begin_commit); 764ff780b91SHarshad Shirwadkar 765ff780b91SHarshad Shirwadkar /* 766ff780b91SHarshad Shirwadkar * Stop a fast commit. If fallback is set, this function starts commit of 767ff780b91SHarshad Shirwadkar * TID tid before any other fast commit can start. 768ff780b91SHarshad Shirwadkar */ 769ff780b91SHarshad Shirwadkar static int __jbd2_fc_end_commit(journal_t *journal, tid_t tid, bool fallback) 770ff780b91SHarshad Shirwadkar { 771ff780b91SHarshad Shirwadkar if (journal->j_fc_cleanup_callback) 772ff780b91SHarshad Shirwadkar journal->j_fc_cleanup_callback(journal, 0); 773ff780b91SHarshad Shirwadkar write_lock(&journal->j_state_lock); 774ff780b91SHarshad Shirwadkar journal->j_flags &= ~JBD2_FAST_COMMIT_ONGOING; 775ff780b91SHarshad Shirwadkar if (fallback) 776ff780b91SHarshad Shirwadkar journal->j_flags |= JBD2_FULL_COMMIT_ONGOING; 777ff780b91SHarshad Shirwadkar write_unlock(&journal->j_state_lock); 778ff780b91SHarshad Shirwadkar wake_up(&journal->j_fc_wait); 779ff780b91SHarshad Shirwadkar if (fallback) 780ff780b91SHarshad Shirwadkar return jbd2_complete_transaction(journal, tid); 781ff780b91SHarshad Shirwadkar return 0; 782ff780b91SHarshad Shirwadkar } 783ff780b91SHarshad Shirwadkar 784ff780b91SHarshad Shirwadkar int jbd2_fc_end_commit(journal_t *journal) 785ff780b91SHarshad Shirwadkar { 7860bce577bSHarshad Shirwadkar return __jbd2_fc_end_commit(journal, 0, false); 787ff780b91SHarshad Shirwadkar } 788ff780b91SHarshad Shirwadkar EXPORT_SYMBOL(jbd2_fc_end_commit); 789ff780b91SHarshad Shirwadkar 7900bce577bSHarshad Shirwadkar int jbd2_fc_end_commit_fallback(journal_t *journal) 791ff780b91SHarshad Shirwadkar { 7920bce577bSHarshad Shirwadkar tid_t tid; 7930bce577bSHarshad Shirwadkar 7940bce577bSHarshad Shirwadkar read_lock(&journal->j_state_lock); 7950bce577bSHarshad Shirwadkar tid = journal->j_running_transaction ? 7960bce577bSHarshad Shirwadkar journal->j_running_transaction->t_tid : 0; 7970bce577bSHarshad Shirwadkar read_unlock(&journal->j_state_lock); 7980bce577bSHarshad Shirwadkar return __jbd2_fc_end_commit(journal, tid, true); 799ff780b91SHarshad Shirwadkar } 800ff780b91SHarshad Shirwadkar EXPORT_SYMBOL(jbd2_fc_end_commit_fallback); 801ff780b91SHarshad Shirwadkar 802b8a6176cSJan Kara /* Return 1 when transaction with given tid has already committed. */ 803b8a6176cSJan Kara int jbd2_transaction_committed(journal_t *journal, tid_t tid) 804b8a6176cSJan Kara { 805b8a6176cSJan Kara int ret = 1; 806b8a6176cSJan Kara 807b8a6176cSJan Kara read_lock(&journal->j_state_lock); 808b8a6176cSJan Kara if (journal->j_running_transaction && 809b8a6176cSJan Kara journal->j_running_transaction->t_tid == tid) 810b8a6176cSJan Kara ret = 0; 811b8a6176cSJan Kara if (journal->j_committing_transaction && 812b8a6176cSJan Kara journal->j_committing_transaction->t_tid == tid) 813b8a6176cSJan Kara ret = 0; 814b8a6176cSJan Kara read_unlock(&journal->j_state_lock); 815b8a6176cSJan Kara return ret; 816b8a6176cSJan Kara } 817b8a6176cSJan Kara EXPORT_SYMBOL(jbd2_transaction_committed); 818b8a6176cSJan Kara 819470decc6SDave Kleikamp /* 820d76a3a77STheodore Ts'o * When this function returns the transaction corresponding to tid 821d76a3a77STheodore Ts'o * will be completed. If the transaction has currently running, start 822d76a3a77STheodore Ts'o * committing that transaction before waiting for it to complete. If 823d76a3a77STheodore Ts'o * the transaction id is stale, it is by definition already completed, 824d76a3a77STheodore Ts'o * so just return SUCCESS. 825d76a3a77STheodore Ts'o */ 826d76a3a77STheodore Ts'o int jbd2_complete_transaction(journal_t *journal, tid_t tid) 827d76a3a77STheodore Ts'o { 828d76a3a77STheodore Ts'o int need_to_wait = 1; 829d76a3a77STheodore Ts'o 830d76a3a77STheodore Ts'o read_lock(&journal->j_state_lock); 831d76a3a77STheodore Ts'o if (journal->j_running_transaction && 832d76a3a77STheodore Ts'o journal->j_running_transaction->t_tid == tid) { 833d76a3a77STheodore Ts'o if (journal->j_commit_request != tid) { 834d76a3a77STheodore Ts'o /* transaction not yet started, so request it */ 835d76a3a77STheodore Ts'o read_unlock(&journal->j_state_lock); 836d76a3a77STheodore Ts'o jbd2_log_start_commit(journal, tid); 837d76a3a77STheodore Ts'o goto wait_commit; 838d76a3a77STheodore Ts'o } 839d76a3a77STheodore Ts'o } else if (!(journal->j_committing_transaction && 840d76a3a77STheodore Ts'o journal->j_committing_transaction->t_tid == tid)) 841d76a3a77STheodore Ts'o need_to_wait = 0; 842d76a3a77STheodore Ts'o read_unlock(&journal->j_state_lock); 843d76a3a77STheodore Ts'o if (!need_to_wait) 844d76a3a77STheodore Ts'o return 0; 845d76a3a77STheodore Ts'o wait_commit: 846d76a3a77STheodore Ts'o return jbd2_log_wait_commit(journal, tid); 847d76a3a77STheodore Ts'o } 848d76a3a77STheodore Ts'o EXPORT_SYMBOL(jbd2_complete_transaction); 849d76a3a77STheodore Ts'o 850d76a3a77STheodore Ts'o /* 851470decc6SDave Kleikamp * Log buffer allocation routines: 852470decc6SDave Kleikamp */ 853470decc6SDave Kleikamp 85418eba7aaSMingming Cao int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp) 855470decc6SDave Kleikamp { 856470decc6SDave Kleikamp unsigned long blocknr; 857470decc6SDave Kleikamp 858a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 859470decc6SDave Kleikamp J_ASSERT(journal->j_free > 1); 860470decc6SDave Kleikamp 861470decc6SDave Kleikamp blocknr = journal->j_head; 862470decc6SDave Kleikamp journal->j_head++; 863470decc6SDave Kleikamp journal->j_free--; 864470decc6SDave Kleikamp if (journal->j_head == journal->j_last) 865470decc6SDave Kleikamp journal->j_head = journal->j_first; 866a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 867f7f4bccbSMingming Cao return jbd2_journal_bmap(journal, blocknr, retp); 868470decc6SDave Kleikamp } 869470decc6SDave Kleikamp 870ff780b91SHarshad Shirwadkar /* Map one fast commit buffer for use by the file system */ 871ff780b91SHarshad Shirwadkar int jbd2_fc_get_buf(journal_t *journal, struct buffer_head **bh_out) 872ff780b91SHarshad Shirwadkar { 873ff780b91SHarshad Shirwadkar unsigned long long pblock; 874ff780b91SHarshad Shirwadkar unsigned long blocknr; 875ff780b91SHarshad Shirwadkar int ret = 0; 876ff780b91SHarshad Shirwadkar struct buffer_head *bh; 877ff780b91SHarshad Shirwadkar int fc_off; 878ff780b91SHarshad Shirwadkar 879ff780b91SHarshad Shirwadkar *bh_out = NULL; 880ff780b91SHarshad Shirwadkar 881ff780b91SHarshad Shirwadkar if (journal->j_fc_off + journal->j_fc_first < journal->j_fc_last) { 882ff780b91SHarshad Shirwadkar fc_off = journal->j_fc_off; 883ff780b91SHarshad Shirwadkar blocknr = journal->j_fc_first + fc_off; 884ff780b91SHarshad Shirwadkar journal->j_fc_off++; 885ff780b91SHarshad Shirwadkar } else { 886ff780b91SHarshad Shirwadkar ret = -EINVAL; 887ff780b91SHarshad Shirwadkar } 888ff780b91SHarshad Shirwadkar 889ff780b91SHarshad Shirwadkar if (ret) 890ff780b91SHarshad Shirwadkar return ret; 891ff780b91SHarshad Shirwadkar 892ff780b91SHarshad Shirwadkar ret = jbd2_journal_bmap(journal, blocknr, &pblock); 893ff780b91SHarshad Shirwadkar if (ret) 894ff780b91SHarshad Shirwadkar return ret; 895ff780b91SHarshad Shirwadkar 896ff780b91SHarshad Shirwadkar bh = __getblk(journal->j_dev, pblock, journal->j_blocksize); 897ff780b91SHarshad Shirwadkar if (!bh) 898ff780b91SHarshad Shirwadkar return -ENOMEM; 899ff780b91SHarshad Shirwadkar 900ff780b91SHarshad Shirwadkar 901ff780b91SHarshad Shirwadkar journal->j_fc_wbuf[fc_off] = bh; 902ff780b91SHarshad Shirwadkar 903ff780b91SHarshad Shirwadkar *bh_out = bh; 904ff780b91SHarshad Shirwadkar 905ff780b91SHarshad Shirwadkar return 0; 906ff780b91SHarshad Shirwadkar } 907ff780b91SHarshad Shirwadkar EXPORT_SYMBOL(jbd2_fc_get_buf); 908ff780b91SHarshad Shirwadkar 909ff780b91SHarshad Shirwadkar /* 910ff780b91SHarshad Shirwadkar * Wait on fast commit buffers that were allocated by jbd2_fc_get_buf 911ff780b91SHarshad Shirwadkar * for completion. 912ff780b91SHarshad Shirwadkar */ 913ff780b91SHarshad Shirwadkar int jbd2_fc_wait_bufs(journal_t *journal, int num_blks) 914ff780b91SHarshad Shirwadkar { 915ff780b91SHarshad Shirwadkar struct buffer_head *bh; 916ff780b91SHarshad Shirwadkar int i, j_fc_off; 917ff780b91SHarshad Shirwadkar 918ff780b91SHarshad Shirwadkar j_fc_off = journal->j_fc_off; 919ff780b91SHarshad Shirwadkar 920ff780b91SHarshad Shirwadkar /* 921ff780b91SHarshad Shirwadkar * Wait in reverse order to minimize chances of us being woken up before 922ff780b91SHarshad Shirwadkar * all IOs have completed 923ff780b91SHarshad Shirwadkar */ 924ff780b91SHarshad Shirwadkar for (i = j_fc_off - 1; i >= j_fc_off - num_blks; i--) { 925ff780b91SHarshad Shirwadkar bh = journal->j_fc_wbuf[i]; 926ff780b91SHarshad Shirwadkar wait_on_buffer(bh); 927ff780b91SHarshad Shirwadkar put_bh(bh); 928ff780b91SHarshad Shirwadkar journal->j_fc_wbuf[i] = NULL; 929ff780b91SHarshad Shirwadkar if (unlikely(!buffer_uptodate(bh))) 930ff780b91SHarshad Shirwadkar return -EIO; 931ff780b91SHarshad Shirwadkar } 932ff780b91SHarshad Shirwadkar 933ff780b91SHarshad Shirwadkar return 0; 934ff780b91SHarshad Shirwadkar } 935ff780b91SHarshad Shirwadkar EXPORT_SYMBOL(jbd2_fc_wait_bufs); 936ff780b91SHarshad Shirwadkar 937ff780b91SHarshad Shirwadkar int jbd2_fc_release_bufs(journal_t *journal) 938ff780b91SHarshad Shirwadkar { 939ff780b91SHarshad Shirwadkar struct buffer_head *bh; 940ff780b91SHarshad Shirwadkar int i, j_fc_off; 941ff780b91SHarshad Shirwadkar 942ff780b91SHarshad Shirwadkar j_fc_off = journal->j_fc_off; 943ff780b91SHarshad Shirwadkar 944ff780b91SHarshad Shirwadkar for (i = j_fc_off - 1; i >= 0; i--) { 945ff780b91SHarshad Shirwadkar bh = journal->j_fc_wbuf[i]; 946ff780b91SHarshad Shirwadkar if (!bh) 947ff780b91SHarshad Shirwadkar break; 948ff780b91SHarshad Shirwadkar put_bh(bh); 949ff780b91SHarshad Shirwadkar journal->j_fc_wbuf[i] = NULL; 950ff780b91SHarshad Shirwadkar } 951ff780b91SHarshad Shirwadkar 952ff780b91SHarshad Shirwadkar return 0; 953ff780b91SHarshad Shirwadkar } 954ff780b91SHarshad Shirwadkar EXPORT_SYMBOL(jbd2_fc_release_bufs); 955ff780b91SHarshad Shirwadkar 956470decc6SDave Kleikamp /* 957470decc6SDave Kleikamp * Conversion of logical to physical block numbers for the journal 958470decc6SDave Kleikamp * 959470decc6SDave Kleikamp * On external journals the journal blocks are identity-mapped, so 960470decc6SDave Kleikamp * this is a no-op. If needed, we can use j_blk_offset - everything is 961470decc6SDave Kleikamp * ready. 962470decc6SDave Kleikamp */ 963f7f4bccbSMingming Cao int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr, 96418eba7aaSMingming Cao unsigned long long *retp) 965470decc6SDave Kleikamp { 966470decc6SDave Kleikamp int err = 0; 96718eba7aaSMingming Cao unsigned long long ret; 96830460e1eSCarlos Maiolino sector_t block = 0; 969470decc6SDave Kleikamp 970470decc6SDave Kleikamp if (journal->j_inode) { 97130460e1eSCarlos Maiolino block = blocknr; 97230460e1eSCarlos Maiolino ret = bmap(journal->j_inode, &block); 97330460e1eSCarlos Maiolino 97430460e1eSCarlos Maiolino if (ret || !block) { 975470decc6SDave Kleikamp printk(KERN_ALERT "%s: journal block not found " 976470decc6SDave Kleikamp "at offset %lu on %s\n", 97705496769STheodore Ts'o __func__, blocknr, journal->j_devname); 978470decc6SDave Kleikamp err = -EIO; 9797f6225e4Szhangyi (F) jbd2_journal_abort(journal, err); 98030460e1eSCarlos Maiolino } else { 98130460e1eSCarlos Maiolino *retp = block; 982470decc6SDave Kleikamp } 98330460e1eSCarlos Maiolino 984470decc6SDave Kleikamp } else { 985470decc6SDave Kleikamp *retp = blocknr; /* +journal->j_blk_offset */ 986470decc6SDave Kleikamp } 987470decc6SDave Kleikamp return err; 988470decc6SDave Kleikamp } 989470decc6SDave Kleikamp 990470decc6SDave Kleikamp /* 991470decc6SDave Kleikamp * We play buffer_head aliasing tricks to write data/metadata blocks to 992470decc6SDave Kleikamp * the journal without copying their contents, but for journal 993470decc6SDave Kleikamp * descriptor blocks we do need to generate bona fide buffers. 994470decc6SDave Kleikamp * 995f7f4bccbSMingming Cao * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying 996470decc6SDave Kleikamp * the buffer's contents they really should run flush_dcache_page(bh->b_page). 997470decc6SDave Kleikamp * But we don't bother doing that, so there will be coherency problems with 998470decc6SDave Kleikamp * mmaps of blockdevs which hold live JBD-controlled filesystems. 999470decc6SDave Kleikamp */ 100032ab6715SJan Kara struct buffer_head * 100132ab6715SJan Kara jbd2_journal_get_descriptor_buffer(transaction_t *transaction, int type) 1002470decc6SDave Kleikamp { 100332ab6715SJan Kara journal_t *journal = transaction->t_journal; 1004470decc6SDave Kleikamp struct buffer_head *bh; 100518eba7aaSMingming Cao unsigned long long blocknr; 100632ab6715SJan Kara journal_header_t *header; 1007470decc6SDave Kleikamp int err; 1008470decc6SDave Kleikamp 1009f7f4bccbSMingming Cao err = jbd2_journal_next_log_block(journal, &blocknr); 1010470decc6SDave Kleikamp 1011470decc6SDave Kleikamp if (err) 1012470decc6SDave Kleikamp return NULL; 1013470decc6SDave Kleikamp 1014470decc6SDave Kleikamp bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize); 10154b905671SJan Kara if (!bh) 10164b905671SJan Kara return NULL; 10179f356e5aSJan Kara atomic_dec(&transaction->t_outstanding_credits); 1018470decc6SDave Kleikamp lock_buffer(bh); 1019470decc6SDave Kleikamp memset(bh->b_data, 0, journal->j_blocksize); 102032ab6715SJan Kara header = (journal_header_t *)bh->b_data; 102132ab6715SJan Kara header->h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER); 102232ab6715SJan Kara header->h_blocktype = cpu_to_be32(type); 102332ab6715SJan Kara header->h_sequence = cpu_to_be32(transaction->t_tid); 1024470decc6SDave Kleikamp set_buffer_uptodate(bh); 1025470decc6SDave Kleikamp unlock_buffer(bh); 1026470decc6SDave Kleikamp BUFFER_TRACE(bh, "return this buffer"); 1027e5a120aeSJan Kara return bh; 1028470decc6SDave Kleikamp } 1029470decc6SDave Kleikamp 10301101cd4dSJan Kara void jbd2_descriptor_block_csum_set(journal_t *j, struct buffer_head *bh) 10311101cd4dSJan Kara { 10321101cd4dSJan Kara struct jbd2_journal_block_tail *tail; 10331101cd4dSJan Kara __u32 csum; 10341101cd4dSJan Kara 10351101cd4dSJan Kara if (!jbd2_journal_has_csum_v2or3(j)) 10361101cd4dSJan Kara return; 10371101cd4dSJan Kara 10381101cd4dSJan Kara tail = (struct jbd2_journal_block_tail *)(bh->b_data + j->j_blocksize - 10391101cd4dSJan Kara sizeof(struct jbd2_journal_block_tail)); 10401101cd4dSJan Kara tail->t_checksum = 0; 10411101cd4dSJan Kara csum = jbd2_chksum(j, j->j_csum_seed, bh->b_data, j->j_blocksize); 10421101cd4dSJan Kara tail->t_checksum = cpu_to_be32(csum); 10431101cd4dSJan Kara } 10441101cd4dSJan Kara 104579feb521SJan Kara /* 104679feb521SJan Kara * Return tid of the oldest transaction in the journal and block in the journal 104779feb521SJan Kara * where the transaction starts. 104879feb521SJan Kara * 104979feb521SJan Kara * If the journal is now empty, return which will be the next transaction ID 105079feb521SJan Kara * we will write and where will that transaction start. 105179feb521SJan Kara * 105279feb521SJan Kara * The return value is 0 if journal tail cannot be pushed any further, 1 if 105379feb521SJan Kara * it can. 105479feb521SJan Kara */ 105579feb521SJan Kara int jbd2_journal_get_log_tail(journal_t *journal, tid_t *tid, 105679feb521SJan Kara unsigned long *block) 105779feb521SJan Kara { 105879feb521SJan Kara transaction_t *transaction; 105979feb521SJan Kara int ret; 106079feb521SJan Kara 106179feb521SJan Kara read_lock(&journal->j_state_lock); 106279feb521SJan Kara spin_lock(&journal->j_list_lock); 106379feb521SJan Kara transaction = journal->j_checkpoint_transactions; 106479feb521SJan Kara if (transaction) { 106579feb521SJan Kara *tid = transaction->t_tid; 106679feb521SJan Kara *block = transaction->t_log_start; 106779feb521SJan Kara } else if ((transaction = journal->j_committing_transaction) != NULL) { 106879feb521SJan Kara *tid = transaction->t_tid; 106979feb521SJan Kara *block = transaction->t_log_start; 107079feb521SJan Kara } else if ((transaction = journal->j_running_transaction) != NULL) { 107179feb521SJan Kara *tid = transaction->t_tid; 107279feb521SJan Kara *block = journal->j_head; 107379feb521SJan Kara } else { 107479feb521SJan Kara *tid = journal->j_transaction_sequence; 107579feb521SJan Kara *block = journal->j_head; 107679feb521SJan Kara } 107779feb521SJan Kara ret = tid_gt(*tid, journal->j_tail_sequence); 107879feb521SJan Kara spin_unlock(&journal->j_list_lock); 107979feb521SJan Kara read_unlock(&journal->j_state_lock); 108079feb521SJan Kara 108179feb521SJan Kara return ret; 108279feb521SJan Kara } 108379feb521SJan Kara 108479feb521SJan Kara /* 108579feb521SJan Kara * Update information in journal structure and in on disk journal superblock 108679feb521SJan Kara * about log tail. This function does not check whether information passed in 108779feb521SJan Kara * really pushes log tail further. It's responsibility of the caller to make 108879feb521SJan Kara * sure provided log tail information is valid (e.g. by holding 108979feb521SJan Kara * j_checkpoint_mutex all the time between computing log tail and calling this 109079feb521SJan Kara * function as is the case with jbd2_cleanup_journal_tail()). 109179feb521SJan Kara * 109279feb521SJan Kara * Requires j_checkpoint_mutex 109379feb521SJan Kara */ 10946f6a6fdaSJoseph Qi int __jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block) 109579feb521SJan Kara { 109679feb521SJan Kara unsigned long freed; 10976f6a6fdaSJoseph Qi int ret; 109879feb521SJan Kara 109979feb521SJan Kara BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex)); 110079feb521SJan Kara 110179feb521SJan Kara /* 110279feb521SJan Kara * We cannot afford for write to remain in drive's caches since as 110379feb521SJan Kara * soon as we update j_tail, next transaction can start reusing journal 110479feb521SJan Kara * space and if we lose sb update during power failure we'd replay 110579feb521SJan Kara * old transaction with possibly newly overwritten data. 110679feb521SJan Kara */ 110717f423b5SJan Kara ret = jbd2_journal_update_sb_log_tail(journal, tid, block, 110817f423b5SJan Kara REQ_SYNC | REQ_FUA); 11096f6a6fdaSJoseph Qi if (ret) 11106f6a6fdaSJoseph Qi goto out; 11116f6a6fdaSJoseph Qi 111279feb521SJan Kara write_lock(&journal->j_state_lock); 111379feb521SJan Kara freed = block - journal->j_tail; 111479feb521SJan Kara if (block < journal->j_tail) 111579feb521SJan Kara freed += journal->j_last - journal->j_first; 111679feb521SJan Kara 111779feb521SJan Kara trace_jbd2_update_log_tail(journal, tid, block, freed); 111879feb521SJan Kara jbd_debug(1, 11197821ce41SGaowei Pu "Cleaning journal tail from %u to %u (offset %lu), " 112079feb521SJan Kara "freeing %lu\n", 112179feb521SJan Kara journal->j_tail_sequence, tid, block, freed); 112279feb521SJan Kara 112379feb521SJan Kara journal->j_free += freed; 112479feb521SJan Kara journal->j_tail_sequence = tid; 112579feb521SJan Kara journal->j_tail = block; 112679feb521SJan Kara write_unlock(&journal->j_state_lock); 11276f6a6fdaSJoseph Qi 11286f6a6fdaSJoseph Qi out: 11296f6a6fdaSJoseph Qi return ret; 113079feb521SJan Kara } 113179feb521SJan Kara 11323339578fSJan Kara /* 113385e0c4e8STheodore Ts'o * This is a variation of __jbd2_update_log_tail which checks for validity of 11343339578fSJan Kara * provided log tail and locks j_checkpoint_mutex. So it is safe against races 11353339578fSJan Kara * with other threads updating log tail. 11363339578fSJan Kara */ 11373339578fSJan Kara void jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block) 11383339578fSJan Kara { 11396fa7aa50STejun Heo mutex_lock_io(&journal->j_checkpoint_mutex); 11403339578fSJan Kara if (tid_gt(tid, journal->j_tail_sequence)) 11413339578fSJan Kara __jbd2_update_log_tail(journal, tid, block); 11423339578fSJan Kara mutex_unlock(&journal->j_checkpoint_mutex); 11433339578fSJan Kara } 11443339578fSJan Kara 11458e85fb3fSJohann Lombardi struct jbd2_stats_proc_session { 11468e85fb3fSJohann Lombardi journal_t *journal; 11478e85fb3fSJohann Lombardi struct transaction_stats_s *stats; 11488e85fb3fSJohann Lombardi int start; 11498e85fb3fSJohann Lombardi int max; 11508e85fb3fSJohann Lombardi }; 11518e85fb3fSJohann Lombardi 11528e85fb3fSJohann Lombardi static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos) 11538e85fb3fSJohann Lombardi { 11548e85fb3fSJohann Lombardi return *pos ? NULL : SEQ_START_TOKEN; 11558e85fb3fSJohann Lombardi } 11568e85fb3fSJohann Lombardi 11578e85fb3fSJohann Lombardi static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos) 11588e85fb3fSJohann Lombardi { 11591a8e9cf4SVasily Averin (*pos)++; 11608e85fb3fSJohann Lombardi return NULL; 11618e85fb3fSJohann Lombardi } 11628e85fb3fSJohann Lombardi 11638e85fb3fSJohann Lombardi static int jbd2_seq_info_show(struct seq_file *seq, void *v) 11648e85fb3fSJohann Lombardi { 11658e85fb3fSJohann Lombardi struct jbd2_stats_proc_session *s = seq->private; 11668e85fb3fSJohann Lombardi 11678e85fb3fSJohann Lombardi if (v != SEQ_START_TOKEN) 11688e85fb3fSJohann Lombardi return 0; 11699fff24aaSTheodore Ts'o seq_printf(seq, "%lu transactions (%lu requested), " 11709fff24aaSTheodore Ts'o "each up to %u blocks\n", 11719fff24aaSTheodore Ts'o s->stats->ts_tid, s->stats->ts_requested, 11728e85fb3fSJohann Lombardi s->journal->j_max_transaction_buffers); 11738e85fb3fSJohann Lombardi if (s->stats->ts_tid == 0) 11748e85fb3fSJohann Lombardi return 0; 11758e85fb3fSJohann Lombardi seq_printf(seq, "average: \n %ums waiting for transaction\n", 1176bf699327STheodore Ts'o jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid)); 11779fff24aaSTheodore Ts'o seq_printf(seq, " %ums request delay\n", 11789fff24aaSTheodore Ts'o (s->stats->ts_requested == 0) ? 0 : 11799fff24aaSTheodore Ts'o jiffies_to_msecs(s->stats->run.rs_request_delay / 11809fff24aaSTheodore Ts'o s->stats->ts_requested)); 11818e85fb3fSJohann Lombardi seq_printf(seq, " %ums running transaction\n", 1182bf699327STheodore Ts'o jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid)); 11838e85fb3fSJohann Lombardi seq_printf(seq, " %ums transaction was being locked\n", 1184bf699327STheodore Ts'o jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid)); 11858e85fb3fSJohann Lombardi seq_printf(seq, " %ums flushing data (in ordered mode)\n", 1186bf699327STheodore Ts'o jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid)); 11878e85fb3fSJohann Lombardi seq_printf(seq, " %ums logging transaction\n", 1188bf699327STheodore Ts'o jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid)); 1189c225aa57SSimon Holm Thøgersen seq_printf(seq, " %lluus average transaction commit time\n", 1190c225aa57SSimon Holm Thøgersen div_u64(s->journal->j_average_commit_time, 1000)); 11918e85fb3fSJohann Lombardi seq_printf(seq, " %lu handles per transaction\n", 1192bf699327STheodore Ts'o s->stats->run.rs_handle_count / s->stats->ts_tid); 11938e85fb3fSJohann Lombardi seq_printf(seq, " %lu blocks per transaction\n", 1194bf699327STheodore Ts'o s->stats->run.rs_blocks / s->stats->ts_tid); 11958e85fb3fSJohann Lombardi seq_printf(seq, " %lu logged blocks per transaction\n", 1196bf699327STheodore Ts'o s->stats->run.rs_blocks_logged / s->stats->ts_tid); 11978e85fb3fSJohann Lombardi return 0; 11988e85fb3fSJohann Lombardi } 11998e85fb3fSJohann Lombardi 12008e85fb3fSJohann Lombardi static void jbd2_seq_info_stop(struct seq_file *seq, void *v) 12018e85fb3fSJohann Lombardi { 12028e85fb3fSJohann Lombardi } 12038e85fb3fSJohann Lombardi 120488e9d34cSJames Morris static const struct seq_operations jbd2_seq_info_ops = { 12058e85fb3fSJohann Lombardi .start = jbd2_seq_info_start, 12068e85fb3fSJohann Lombardi .next = jbd2_seq_info_next, 12078e85fb3fSJohann Lombardi .stop = jbd2_seq_info_stop, 12088e85fb3fSJohann Lombardi .show = jbd2_seq_info_show, 12098e85fb3fSJohann Lombardi }; 12108e85fb3fSJohann Lombardi 12118e85fb3fSJohann Lombardi static int jbd2_seq_info_open(struct inode *inode, struct file *file) 12128e85fb3fSJohann Lombardi { 1213d9dda78bSAl Viro journal_t *journal = PDE_DATA(inode); 12148e85fb3fSJohann Lombardi struct jbd2_stats_proc_session *s; 12158e85fb3fSJohann Lombardi int rc, size; 12168e85fb3fSJohann Lombardi 12178e85fb3fSJohann Lombardi s = kmalloc(sizeof(*s), GFP_KERNEL); 12188e85fb3fSJohann Lombardi if (s == NULL) 12198e85fb3fSJohann Lombardi return -ENOMEM; 12208e85fb3fSJohann Lombardi size = sizeof(struct transaction_stats_s); 12218e85fb3fSJohann Lombardi s->stats = kmalloc(size, GFP_KERNEL); 12228e85fb3fSJohann Lombardi if (s->stats == NULL) { 12238e85fb3fSJohann Lombardi kfree(s); 12248e85fb3fSJohann Lombardi return -ENOMEM; 12258e85fb3fSJohann Lombardi } 12268e85fb3fSJohann Lombardi spin_lock(&journal->j_history_lock); 12278e85fb3fSJohann Lombardi memcpy(s->stats, &journal->j_stats, size); 12288e85fb3fSJohann Lombardi s->journal = journal; 12298e85fb3fSJohann Lombardi spin_unlock(&journal->j_history_lock); 12308e85fb3fSJohann Lombardi 12318e85fb3fSJohann Lombardi rc = seq_open(file, &jbd2_seq_info_ops); 12328e85fb3fSJohann Lombardi if (rc == 0) { 12338e85fb3fSJohann Lombardi struct seq_file *m = file->private_data; 12348e85fb3fSJohann Lombardi m->private = s; 12358e85fb3fSJohann Lombardi } else { 12368e85fb3fSJohann Lombardi kfree(s->stats); 12378e85fb3fSJohann Lombardi kfree(s); 12388e85fb3fSJohann Lombardi } 12398e85fb3fSJohann Lombardi return rc; 12408e85fb3fSJohann Lombardi 12418e85fb3fSJohann Lombardi } 12428e85fb3fSJohann Lombardi 12438e85fb3fSJohann Lombardi static int jbd2_seq_info_release(struct inode *inode, struct file *file) 12448e85fb3fSJohann Lombardi { 12458e85fb3fSJohann Lombardi struct seq_file *seq = file->private_data; 12468e85fb3fSJohann Lombardi struct jbd2_stats_proc_session *s = seq->private; 12478e85fb3fSJohann Lombardi kfree(s->stats); 12488e85fb3fSJohann Lombardi kfree(s); 12498e85fb3fSJohann Lombardi return seq_release(inode, file); 12508e85fb3fSJohann Lombardi } 12518e85fb3fSJohann Lombardi 125297a32539SAlexey Dobriyan static const struct proc_ops jbd2_info_proc_ops = { 125397a32539SAlexey Dobriyan .proc_open = jbd2_seq_info_open, 125497a32539SAlexey Dobriyan .proc_read = seq_read, 125597a32539SAlexey Dobriyan .proc_lseek = seq_lseek, 125697a32539SAlexey Dobriyan .proc_release = jbd2_seq_info_release, 12578e85fb3fSJohann Lombardi }; 12588e85fb3fSJohann Lombardi 12598e85fb3fSJohann Lombardi static struct proc_dir_entry *proc_jbd2_stats; 12608e85fb3fSJohann Lombardi 12618e85fb3fSJohann Lombardi static void jbd2_stats_proc_init(journal_t *journal) 12628e85fb3fSJohann Lombardi { 126305496769STheodore Ts'o journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats); 12648e85fb3fSJohann Lombardi if (journal->j_proc_entry) { 126579da3664SDenis V. Lunev proc_create_data("info", S_IRUGO, journal->j_proc_entry, 126697a32539SAlexey Dobriyan &jbd2_info_proc_ops, journal); 12678e85fb3fSJohann Lombardi } 12688e85fb3fSJohann Lombardi } 12698e85fb3fSJohann Lombardi 12708e85fb3fSJohann Lombardi static void jbd2_stats_proc_exit(journal_t *journal) 12718e85fb3fSJohann Lombardi { 12728e85fb3fSJohann Lombardi remove_proc_entry("info", journal->j_proc_entry); 127305496769STheodore Ts'o remove_proc_entry(journal->j_devname, proc_jbd2_stats); 12748e85fb3fSJohann Lombardi } 12758e85fb3fSJohann Lombardi 1276b90bfdf5SJan Kara /* Minimum size of descriptor tag */ 1277b90bfdf5SJan Kara static int jbd2_min_tag_size(void) 1278b90bfdf5SJan Kara { 1279b90bfdf5SJan Kara /* 1280b90bfdf5SJan Kara * Tag with 32-bit block numbers does not use last four bytes of the 1281b90bfdf5SJan Kara * structure 1282b90bfdf5SJan Kara */ 1283b90bfdf5SJan Kara return sizeof(journal_block_tag_t) - 4; 1284b90bfdf5SJan Kara } 1285b90bfdf5SJan Kara 1286470decc6SDave Kleikamp /* 1287470decc6SDave Kleikamp * Management for journal control blocks: functions to create and 1288470decc6SDave Kleikamp * destroy journal_t structures, and to initialise and read existing 1289470decc6SDave Kleikamp * journal blocks from disk. */ 1290470decc6SDave Kleikamp 1291470decc6SDave Kleikamp /* First: create and setup a journal_t object in memory. We initialise 1292470decc6SDave Kleikamp * very few fields yet: that has to wait until we have created the 1293470decc6SDave Kleikamp * journal structures from from scratch, or loaded them from disk. */ 1294470decc6SDave Kleikamp 1295f0c9fd54SGeliang Tang static journal_t *journal_init_common(struct block_device *bdev, 1296f0c9fd54SGeliang Tang struct block_device *fs_dev, 1297f0c9fd54SGeliang Tang unsigned long long start, int len, int blocksize) 1298470decc6SDave Kleikamp { 1299ab714affSJan Kara static struct lock_class_key jbd2_trans_commit_key; 1300470decc6SDave Kleikamp journal_t *journal; 1301470decc6SDave Kleikamp int err; 1302f0c9fd54SGeliang Tang struct buffer_head *bh; 1303f0c9fd54SGeliang Tang int n; 1304470decc6SDave Kleikamp 13053ebfdf88SAndrew Morton journal = kzalloc(sizeof(*journal), GFP_KERNEL); 1306470decc6SDave Kleikamp if (!journal) 1307b7271b0aSTheodore Ts'o return NULL; 1308470decc6SDave Kleikamp 1309470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_transaction_locked); 1310470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_done_commit); 1311470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_commit); 1312470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_updates); 13138f7d89f3SJan Kara init_waitqueue_head(&journal->j_wait_reserved); 1314ff780b91SHarshad Shirwadkar init_waitqueue_head(&journal->j_fc_wait); 13157b97d868Szhangyi (F) mutex_init(&journal->j_abort_mutex); 1316470decc6SDave Kleikamp mutex_init(&journal->j_barrier); 1317470decc6SDave Kleikamp mutex_init(&journal->j_checkpoint_mutex); 1318470decc6SDave Kleikamp spin_lock_init(&journal->j_revoke_lock); 1319470decc6SDave Kleikamp spin_lock_init(&journal->j_list_lock); 1320a931da6aSTheodore Ts'o rwlock_init(&journal->j_state_lock); 1321470decc6SDave Kleikamp 1322cd02ff0bSMingming Cao journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE); 132330773840STheodore Ts'o journal->j_min_batch_time = 0; 132430773840STheodore Ts'o journal->j_max_batch_time = 15000; /* 15ms */ 13258f7d89f3SJan Kara atomic_set(&journal->j_reserved_credits, 0); 1326470decc6SDave Kleikamp 1327470decc6SDave Kleikamp /* The journal is marked for error until we succeed with recovery! */ 1328f7f4bccbSMingming Cao journal->j_flags = JBD2_ABORT; 1329470decc6SDave Kleikamp 1330470decc6SDave Kleikamp /* Set up a default-sized revoke table for the new mount. */ 1331f7f4bccbSMingming Cao err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH); 1332cd9cb405SEric Biggers if (err) 1333cd9cb405SEric Biggers goto err_cleanup; 13348e85fb3fSJohann Lombardi 1335bf699327STheodore Ts'o spin_lock_init(&journal->j_history_lock); 13368e85fb3fSJohann Lombardi 1337ab714affSJan Kara lockdep_init_map(&journal->j_trans_commit_map, "jbd2_handle", 1338ab714affSJan Kara &jbd2_trans_commit_key, 0); 1339ab714affSJan Kara 1340f0c9fd54SGeliang Tang /* journal descriptor can store up to n blocks -bzzz */ 1341f0c9fd54SGeliang Tang journal->j_blocksize = blocksize; 1342f0c9fd54SGeliang Tang journal->j_dev = bdev; 1343f0c9fd54SGeliang Tang journal->j_fs_dev = fs_dev; 1344f0c9fd54SGeliang Tang journal->j_blk_offset = start; 1345ede7dc7fSHarshad Shirwadkar journal->j_total_len = len; 1346b90bfdf5SJan Kara /* We need enough buffers to write out full descriptor block. */ 1347b90bfdf5SJan Kara n = journal->j_blocksize / jbd2_min_tag_size(); 1348f0c9fd54SGeliang Tang journal->j_wbufsize = n; 1349a1e5e465SHarshad Shirwadkar journal->j_fc_wbuf = NULL; 1350f0c9fd54SGeliang Tang journal->j_wbuf = kmalloc_array(n, sizeof(struct buffer_head *), 1351f0c9fd54SGeliang Tang GFP_KERNEL); 1352cd9cb405SEric Biggers if (!journal->j_wbuf) 1353cd9cb405SEric Biggers goto err_cleanup; 1354f0c9fd54SGeliang Tang 1355f0c9fd54SGeliang Tang bh = getblk_unmovable(journal->j_dev, start, journal->j_blocksize); 1356f0c9fd54SGeliang Tang if (!bh) { 1357f0c9fd54SGeliang Tang pr_err("%s: Cannot get buffer for journal superblock\n", 1358f0c9fd54SGeliang Tang __func__); 1359cd9cb405SEric Biggers goto err_cleanup; 1360f0c9fd54SGeliang Tang } 1361f0c9fd54SGeliang Tang journal->j_sb_buffer = bh; 1362f0c9fd54SGeliang Tang journal->j_superblock = (journal_superblock_t *)bh->b_data; 1363f0c9fd54SGeliang Tang 1364470decc6SDave Kleikamp return journal; 1365cd9cb405SEric Biggers 1366cd9cb405SEric Biggers err_cleanup: 1367cd9cb405SEric Biggers kfree(journal->j_wbuf); 1368cd9cb405SEric Biggers jbd2_journal_destroy_revoke(journal); 1369cd9cb405SEric Biggers kfree(journal); 1370cd9cb405SEric Biggers return NULL; 1371470decc6SDave Kleikamp } 1372470decc6SDave Kleikamp 1373f7f4bccbSMingming Cao /* jbd2_journal_init_dev and jbd2_journal_init_inode: 1374470decc6SDave Kleikamp * 1375470decc6SDave Kleikamp * Create a journal structure assigned some fixed set of disk blocks to 1376470decc6SDave Kleikamp * the journal. We don't actually touch those disk blocks yet, but we 1377470decc6SDave Kleikamp * need to set up all of the mapping information to tell the journaling 1378470decc6SDave Kleikamp * system where the journal blocks are. 1379470decc6SDave Kleikamp * 1380470decc6SDave Kleikamp */ 1381470decc6SDave Kleikamp 1382470decc6SDave Kleikamp /** 13835648ba5bSRandy Dunlap * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure 1384470decc6SDave Kleikamp * @bdev: Block device on which to create the journal 1385470decc6SDave Kleikamp * @fs_dev: Device which hold journalled filesystem for this journal. 1386470decc6SDave Kleikamp * @start: Block nr Start of journal. 1387470decc6SDave Kleikamp * @len: Length of the journal in blocks. 1388470decc6SDave Kleikamp * @blocksize: blocksize of journalling device 13895648ba5bSRandy Dunlap * 13905648ba5bSRandy Dunlap * Returns: a newly created journal_t * 1391470decc6SDave Kleikamp * 1392f7f4bccbSMingming Cao * jbd2_journal_init_dev creates a journal which maps a fixed contiguous 1393470decc6SDave Kleikamp * range of blocks on an arbitrary block device. 1394470decc6SDave Kleikamp * 1395470decc6SDave Kleikamp */ 1396f7f4bccbSMingming Cao journal_t *jbd2_journal_init_dev(struct block_device *bdev, 1397470decc6SDave Kleikamp struct block_device *fs_dev, 139818eba7aaSMingming Cao unsigned long long start, int len, int blocksize) 1399470decc6SDave Kleikamp { 1400f0c9fd54SGeliang Tang journal_t *journal; 1401470decc6SDave Kleikamp 1402f0c9fd54SGeliang Tang journal = journal_init_common(bdev, fs_dev, start, len, blocksize); 1403470decc6SDave Kleikamp if (!journal) 1404470decc6SDave Kleikamp return NULL; 1405470decc6SDave Kleikamp 14060587aa3dSyangsheng bdevname(journal->j_dev, journal->j_devname); 140781ae394bSRasmus Villemoes strreplace(journal->j_devname, '/', '!'); 14084b905671SJan Kara jbd2_stats_proc_init(journal); 14094b905671SJan Kara 1410470decc6SDave Kleikamp return journal; 1411470decc6SDave Kleikamp } 1412470decc6SDave Kleikamp 1413470decc6SDave Kleikamp /** 1414f7f4bccbSMingming Cao * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode. 1415470decc6SDave Kleikamp * @inode: An inode to create the journal in 1416470decc6SDave Kleikamp * 1417f7f4bccbSMingming Cao * jbd2_journal_init_inode creates a journal which maps an on-disk inode as 1418470decc6SDave Kleikamp * the journal. The inode must exist already, must support bmap() and 1419470decc6SDave Kleikamp * must have all data blocks preallocated. 1420470decc6SDave Kleikamp */ 1421f7f4bccbSMingming Cao journal_t *jbd2_journal_init_inode(struct inode *inode) 1422470decc6SDave Kleikamp { 1423f0c9fd54SGeliang Tang journal_t *journal; 142430460e1eSCarlos Maiolino sector_t blocknr; 142505496769STheodore Ts'o char *p; 142630460e1eSCarlos Maiolino int err = 0; 1427470decc6SDave Kleikamp 142830460e1eSCarlos Maiolino blocknr = 0; 142930460e1eSCarlos Maiolino err = bmap(inode, &blocknr); 143030460e1eSCarlos Maiolino 143130460e1eSCarlos Maiolino if (err || !blocknr) { 1432f0c9fd54SGeliang Tang pr_err("%s: Cannot locate journal superblock\n", 1433f0c9fd54SGeliang Tang __func__); 1434f0c9fd54SGeliang Tang return NULL; 1435f0c9fd54SGeliang Tang } 1436f0c9fd54SGeliang Tang 1437f0c9fd54SGeliang Tang jbd_debug(1, "JBD2: inode %s/%ld, size %lld, bits %d, blksize %ld\n", 1438f0c9fd54SGeliang Tang inode->i_sb->s_id, inode->i_ino, (long long) inode->i_size, 1439f0c9fd54SGeliang Tang inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize); 1440f0c9fd54SGeliang Tang 1441f0c9fd54SGeliang Tang journal = journal_init_common(inode->i_sb->s_bdev, inode->i_sb->s_bdev, 1442f0c9fd54SGeliang Tang blocknr, inode->i_size >> inode->i_sb->s_blocksize_bits, 1443f0c9fd54SGeliang Tang inode->i_sb->s_blocksize); 1444470decc6SDave Kleikamp if (!journal) 1445470decc6SDave Kleikamp return NULL; 1446470decc6SDave Kleikamp 1447470decc6SDave Kleikamp journal->j_inode = inode; 144805496769STheodore Ts'o bdevname(journal->j_dev, journal->j_devname); 144981ae394bSRasmus Villemoes p = strreplace(journal->j_devname, '/', '!'); 145090576c0bSTheodore Ts'o sprintf(p, "-%lu", journal->j_inode->i_ino); 14518e85fb3fSJohann Lombardi jbd2_stats_proc_init(journal); 1452470decc6SDave Kleikamp 1453470decc6SDave Kleikamp return journal; 1454470decc6SDave Kleikamp } 1455470decc6SDave Kleikamp 1456470decc6SDave Kleikamp /* 1457470decc6SDave Kleikamp * If the journal init or create aborts, we need to mark the journal 1458470decc6SDave Kleikamp * superblock as being NULL to prevent the journal destroy from writing 1459470decc6SDave Kleikamp * back a bogus superblock. 1460470decc6SDave Kleikamp */ 1461470decc6SDave Kleikamp static void journal_fail_superblock(journal_t *journal) 1462470decc6SDave Kleikamp { 1463470decc6SDave Kleikamp struct buffer_head *bh = journal->j_sb_buffer; 1464470decc6SDave Kleikamp brelse(bh); 1465470decc6SDave Kleikamp journal->j_sb_buffer = NULL; 1466470decc6SDave Kleikamp } 1467470decc6SDave Kleikamp 1468470decc6SDave Kleikamp /* 1469470decc6SDave Kleikamp * Given a journal_t structure, initialise the various fields for 1470470decc6SDave Kleikamp * startup of a new journaling session. We use this both when creating 1471470decc6SDave Kleikamp * a journal, and after recovering an old journal to reset it for 1472470decc6SDave Kleikamp * subsequent use. 1473470decc6SDave Kleikamp */ 1474470decc6SDave Kleikamp 1475470decc6SDave Kleikamp static int journal_reset(journal_t *journal) 1476470decc6SDave Kleikamp { 1477470decc6SDave Kleikamp journal_superblock_t *sb = journal->j_superblock; 147818eba7aaSMingming Cao unsigned long long first, last; 1479470decc6SDave Kleikamp 1480470decc6SDave Kleikamp first = be32_to_cpu(sb->s_first); 1481470decc6SDave Kleikamp last = be32_to_cpu(sb->s_maxlen); 1482f6f50e28SJan Kara if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) { 1483f2a44523SEryu Guan printk(KERN_ERR "JBD2: Journal too short (blocks %llu-%llu).\n", 1484f6f50e28SJan Kara first, last); 1485f6f50e28SJan Kara journal_fail_superblock(journal); 1486f6f50e28SJan Kara return -EINVAL; 1487f6f50e28SJan Kara } 1488470decc6SDave Kleikamp 1489470decc6SDave Kleikamp journal->j_first = first; 14906866d7b3SHarshad Shirwadkar journal->j_last = last; 14916866d7b3SHarshad Shirwadkar 14926866d7b3SHarshad Shirwadkar journal->j_head = journal->j_first; 14936866d7b3SHarshad Shirwadkar journal->j_tail = journal->j_first; 14946866d7b3SHarshad Shirwadkar journal->j_free = journal->j_last - journal->j_first; 1495470decc6SDave Kleikamp 1496470decc6SDave Kleikamp journal->j_tail_sequence = journal->j_transaction_sequence; 1497470decc6SDave Kleikamp journal->j_commit_sequence = journal->j_transaction_sequence - 1; 1498470decc6SDave Kleikamp journal->j_commit_request = journal->j_commit_sequence; 1499470decc6SDave Kleikamp 1500ede7dc7fSHarshad Shirwadkar journal->j_max_transaction_buffers = jbd2_journal_get_max_txn_bufs(journal); 1501470decc6SDave Kleikamp 1502470decc6SDave Kleikamp /* 1503a1e5e465SHarshad Shirwadkar * Now that journal recovery is done, turn fast commits off here. This 1504a1e5e465SHarshad Shirwadkar * way, if fast commit was enabled before the crash but if now FS has 1505a1e5e465SHarshad Shirwadkar * disabled it, we don't enable fast commits. 1506a1e5e465SHarshad Shirwadkar */ 1507a1e5e465SHarshad Shirwadkar jbd2_clear_feature_fast_commit(journal); 1508a1e5e465SHarshad Shirwadkar 1509a1e5e465SHarshad Shirwadkar /* 1510470decc6SDave Kleikamp * As a special case, if the on-disk copy is already marked as needing 151124bcc89cSJan Kara * no recovery (s_start == 0), then we can safely defer the superblock 151224bcc89cSJan Kara * update until the next commit by setting JBD2_FLUSHED. This avoids 1513470decc6SDave Kleikamp * attempting a write to a potential-readonly device. 1514470decc6SDave Kleikamp */ 151524bcc89cSJan Kara if (sb->s_start == 0) { 1516f2a44523SEryu Guan jbd_debug(1, "JBD2: Skipping superblock update on recovered sb " 15177821ce41SGaowei Pu "(start %ld, seq %u, errno %d)\n", 1518470decc6SDave Kleikamp journal->j_tail, journal->j_tail_sequence, 1519470decc6SDave Kleikamp journal->j_errno); 152024bcc89cSJan Kara journal->j_flags |= JBD2_FLUSHED; 152124bcc89cSJan Kara } else { 1522a78bb11dSJan Kara /* Lock here to make assertions happy... */ 15236fa7aa50STejun Heo mutex_lock_io(&journal->j_checkpoint_mutex); 152479feb521SJan Kara /* 152570fd7614SChristoph Hellwig * Update log tail information. We use REQ_FUA since new 152679feb521SJan Kara * transaction will start reusing journal space and so we 152779feb521SJan Kara * must make sure information about current log tail is on 152879feb521SJan Kara * disk before that. 152979feb521SJan Kara */ 153079feb521SJan Kara jbd2_journal_update_sb_log_tail(journal, 153179feb521SJan Kara journal->j_tail_sequence, 153279feb521SJan Kara journal->j_tail, 153317f423b5SJan Kara REQ_SYNC | REQ_FUA); 1534a78bb11dSJan Kara mutex_unlock(&journal->j_checkpoint_mutex); 153524bcc89cSJan Kara } 153624bcc89cSJan Kara return jbd2_journal_start_thread(journal); 1537470decc6SDave Kleikamp } 1538470decc6SDave Kleikamp 1539538bcaa6STheodore Ts'o /* 1540538bcaa6STheodore Ts'o * This function expects that the caller will have locked the journal 1541538bcaa6STheodore Ts'o * buffer head, and will return with it unlocked 1542538bcaa6STheodore Ts'o */ 15432a222ca9SMike Christie static int jbd2_write_superblock(journal_t *journal, int write_flags) 154424bcc89cSJan Kara { 154524bcc89cSJan Kara struct buffer_head *bh = journal->j_sb_buffer; 1546fe52d17cSTheodore Ts'o journal_superblock_t *sb = journal->j_superblock; 154779feb521SJan Kara int ret; 154824bcc89cSJan Kara 1549742b06b5SJiufei Xue /* Buffer got discarded which means block device got invalidated */ 1550ef3f5830Szhangyi (F) if (!buffer_mapped(bh)) { 1551ef3f5830Szhangyi (F) unlock_buffer(bh); 1552742b06b5SJiufei Xue return -EIO; 1553ef3f5830Szhangyi (F) } 1554742b06b5SJiufei Xue 15552a222ca9SMike Christie trace_jbd2_write_superblock(journal, write_flags); 155679feb521SJan Kara if (!(journal->j_flags & JBD2_BARRIER)) 155728a8f0d3SMike Christie write_flags &= ~(REQ_FUA | REQ_PREFLUSH); 1558914258bfSTheodore Ts'o if (buffer_write_io_error(bh)) { 1559914258bfSTheodore Ts'o /* 1560914258bfSTheodore Ts'o * Oh, dear. A previous attempt to write the journal 1561914258bfSTheodore Ts'o * superblock failed. This could happen because the 1562914258bfSTheodore Ts'o * USB device was yanked out. Or it could happen to 1563914258bfSTheodore Ts'o * be a transient write error and maybe the block will 1564914258bfSTheodore Ts'o * be remapped. Nothing we can do but to retry the 1565914258bfSTheodore Ts'o * write and hope for the best. 1566914258bfSTheodore Ts'o */ 1567914258bfSTheodore Ts'o printk(KERN_ERR "JBD2: previous I/O error detected " 1568914258bfSTheodore Ts'o "for journal superblock update for %s.\n", 1569914258bfSTheodore Ts'o journal->j_devname); 1570914258bfSTheodore Ts'o clear_buffer_write_io_error(bh); 1571914258bfSTheodore Ts'o set_buffer_uptodate(bh); 1572914258bfSTheodore Ts'o } 1573a58ca992STheodore Ts'o if (jbd2_journal_has_csum_v2or3(journal)) 1574a58ca992STheodore Ts'o sb->s_checksum = jbd2_superblock_csum(journal, sb); 157579feb521SJan Kara get_bh(bh); 157679feb521SJan Kara bh->b_end_io = end_buffer_write_sync; 15772a222ca9SMike Christie ret = submit_bh(REQ_OP_WRITE, write_flags, bh); 157879feb521SJan Kara wait_on_buffer(bh); 1579914258bfSTheodore Ts'o if (buffer_write_io_error(bh)) { 1580914258bfSTheodore Ts'o clear_buffer_write_io_error(bh); 1581914258bfSTheodore Ts'o set_buffer_uptodate(bh); 158279feb521SJan Kara ret = -EIO; 158379feb521SJan Kara } 158479feb521SJan Kara if (ret) { 158579feb521SJan Kara printk(KERN_ERR "JBD2: Error %d detected when updating " 158679feb521SJan Kara "journal superblock for %s.\n", ret, 158779feb521SJan Kara journal->j_devname); 15887b97d868Szhangyi (F) if (!is_journal_aborted(journal)) 15896f6a6fdaSJoseph Qi jbd2_journal_abort(journal, ret); 1590914258bfSTheodore Ts'o } 15916f6a6fdaSJoseph Qi 15926f6a6fdaSJoseph Qi return ret; 159324bcc89cSJan Kara } 1594470decc6SDave Kleikamp 159524bcc89cSJan Kara /** 159624bcc89cSJan Kara * jbd2_journal_update_sb_log_tail() - Update log tail in journal sb on disk. 159724bcc89cSJan Kara * @journal: The journal to update. 159879feb521SJan Kara * @tail_tid: TID of the new transaction at the tail of the log 159979feb521SJan Kara * @tail_block: The first block of the transaction at the tail of the log 160079feb521SJan Kara * @write_op: With which operation should we write the journal sb 160124bcc89cSJan Kara * 160224bcc89cSJan Kara * Update a journal's superblock information about log tail and write it to 160324bcc89cSJan Kara * disk, waiting for the IO to complete. 160424bcc89cSJan Kara */ 16056f6a6fdaSJoseph Qi int jbd2_journal_update_sb_log_tail(journal_t *journal, tid_t tail_tid, 160679feb521SJan Kara unsigned long tail_block, int write_op) 160724bcc89cSJan Kara { 160824bcc89cSJan Kara journal_superblock_t *sb = journal->j_superblock; 16096f6a6fdaSJoseph Qi int ret; 16102201c590SSeiji Aguchi 161185e0c4e8STheodore Ts'o if (is_journal_aborted(journal)) 161285e0c4e8STheodore Ts'o return -EIO; 1613*fcf37549SZhang Yi if (test_bit(JBD2_CHECKPOINT_IO_ERROR, &journal->j_atomic_flags)) { 1614*fcf37549SZhang Yi jbd2_journal_abort(journal, -EIO); 1615*fcf37549SZhang Yi return -EIO; 1616*fcf37549SZhang Yi } 161785e0c4e8STheodore Ts'o 1618a78bb11dSJan Kara BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex)); 161979feb521SJan Kara jbd_debug(1, "JBD2: updating superblock (start %lu, seq %u)\n", 162079feb521SJan Kara tail_block, tail_tid); 1621470decc6SDave Kleikamp 1622538bcaa6STheodore Ts'o lock_buffer(journal->j_sb_buffer); 162379feb521SJan Kara sb->s_sequence = cpu_to_be32(tail_tid); 162479feb521SJan Kara sb->s_start = cpu_to_be32(tail_block); 162524bcc89cSJan Kara 16266f6a6fdaSJoseph Qi ret = jbd2_write_superblock(journal, write_op); 16276f6a6fdaSJoseph Qi if (ret) 16286f6a6fdaSJoseph Qi goto out; 162924bcc89cSJan Kara 163024bcc89cSJan Kara /* Log is no longer empty */ 1631a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 163224bcc89cSJan Kara WARN_ON(!sb->s_sequence); 1633f7f4bccbSMingming Cao journal->j_flags &= ~JBD2_FLUSHED; 163424bcc89cSJan Kara write_unlock(&journal->j_state_lock); 16356f6a6fdaSJoseph Qi 16366f6a6fdaSJoseph Qi out: 16376f6a6fdaSJoseph Qi return ret; 163824bcc89cSJan Kara } 163924bcc89cSJan Kara 164024bcc89cSJan Kara /** 164124bcc89cSJan Kara * jbd2_mark_journal_empty() - Mark on disk journal as empty. 164224bcc89cSJan Kara * @journal: The journal to update. 1643c0a2ad9bSOGAWA Hirofumi * @write_op: With which operation should we write the journal sb 164424bcc89cSJan Kara * 164524bcc89cSJan Kara * Update a journal's dynamic superblock fields to show that journal is empty. 164624bcc89cSJan Kara * Write updated superblock to disk waiting for IO to complete. 164724bcc89cSJan Kara */ 1648c0a2ad9bSOGAWA Hirofumi static void jbd2_mark_journal_empty(journal_t *journal, int write_op) 164924bcc89cSJan Kara { 165024bcc89cSJan Kara journal_superblock_t *sb = journal->j_superblock; 1651ff780b91SHarshad Shirwadkar bool had_fast_commit = false; 165224bcc89cSJan Kara 1653a78bb11dSJan Kara BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex)); 1654538bcaa6STheodore Ts'o lock_buffer(journal->j_sb_buffer); 1655538bcaa6STheodore Ts'o if (sb->s_start == 0) { /* Is it already empty? */ 1656538bcaa6STheodore Ts'o unlock_buffer(journal->j_sb_buffer); 1657eeecef0aSEric Sandeen return; 1658eeecef0aSEric Sandeen } 1659538bcaa6STheodore Ts'o 16607821ce41SGaowei Pu jbd_debug(1, "JBD2: Marking journal as empty (seq %u)\n", 166124bcc89cSJan Kara journal->j_tail_sequence); 1662470decc6SDave Kleikamp 1663470decc6SDave Kleikamp sb->s_sequence = cpu_to_be32(journal->j_tail_sequence); 166424bcc89cSJan Kara sb->s_start = cpu_to_be32(0); 1665ff780b91SHarshad Shirwadkar if (jbd2_has_feature_fast_commit(journal)) { 1666ff780b91SHarshad Shirwadkar /* 1667ff780b91SHarshad Shirwadkar * When journal is clean, no need to commit fast commit flag and 1668ff780b91SHarshad Shirwadkar * make file system incompatible with older kernels. 1669ff780b91SHarshad Shirwadkar */ 1670ff780b91SHarshad Shirwadkar jbd2_clear_feature_fast_commit(journal); 1671ff780b91SHarshad Shirwadkar had_fast_commit = true; 1672ff780b91SHarshad Shirwadkar } 167324bcc89cSJan Kara 1674c0a2ad9bSOGAWA Hirofumi jbd2_write_superblock(journal, write_op); 167524bcc89cSJan Kara 1676ff780b91SHarshad Shirwadkar if (had_fast_commit) 1677ff780b91SHarshad Shirwadkar jbd2_set_feature_fast_commit(journal); 1678ff780b91SHarshad Shirwadkar 167924bcc89cSJan Kara /* Log is no longer empty */ 168024bcc89cSJan Kara write_lock(&journal->j_state_lock); 1681f7f4bccbSMingming Cao journal->j_flags |= JBD2_FLUSHED; 1682a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 1683470decc6SDave Kleikamp } 1684470decc6SDave Kleikamp 168501d5d965SLeah Rumancik /** 168601d5d965SLeah Rumancik * __jbd2_journal_erase() - Discard or zeroout journal blocks (excluding superblock) 168701d5d965SLeah Rumancik * @journal: The journal to erase. 168801d5d965SLeah Rumancik * @flags: A discard/zeroout request is sent for each physically contigous 168901d5d965SLeah Rumancik * region of the journal. Either JBD2_JOURNAL_FLUSH_DISCARD or 169001d5d965SLeah Rumancik * JBD2_JOURNAL_FLUSH_ZEROOUT must be set to determine which operation 169101d5d965SLeah Rumancik * to perform. 169201d5d965SLeah Rumancik * 169301d5d965SLeah Rumancik * Note: JBD2_JOURNAL_FLUSH_ZEROOUT attempts to use hardware offload. Zeroes 169401d5d965SLeah Rumancik * will be explicitly written if no hardware offload is available, see 169501d5d965SLeah Rumancik * blkdev_issue_zeroout for more details. 169601d5d965SLeah Rumancik */ 169701d5d965SLeah Rumancik static int __jbd2_journal_erase(journal_t *journal, unsigned int flags) 169801d5d965SLeah Rumancik { 169901d5d965SLeah Rumancik int err = 0; 170001d5d965SLeah Rumancik unsigned long block, log_offset; /* logical */ 170101d5d965SLeah Rumancik unsigned long long phys_block, block_start, block_stop; /* physical */ 170201d5d965SLeah Rumancik loff_t byte_start, byte_stop, byte_count; 170301d5d965SLeah Rumancik struct request_queue *q = bdev_get_queue(journal->j_dev); 170401d5d965SLeah Rumancik 170501d5d965SLeah Rumancik /* flags must be set to either discard or zeroout */ 170601d5d965SLeah Rumancik if ((flags & ~JBD2_JOURNAL_FLUSH_VALID) || !flags || 170701d5d965SLeah Rumancik ((flags & JBD2_JOURNAL_FLUSH_DISCARD) && 170801d5d965SLeah Rumancik (flags & JBD2_JOURNAL_FLUSH_ZEROOUT))) 170901d5d965SLeah Rumancik return -EINVAL; 171001d5d965SLeah Rumancik 171101d5d965SLeah Rumancik if (!q) 171201d5d965SLeah Rumancik return -ENXIO; 171301d5d965SLeah Rumancik 171401d5d965SLeah Rumancik if ((flags & JBD2_JOURNAL_FLUSH_DISCARD) && !blk_queue_discard(q)) 171501d5d965SLeah Rumancik return -EOPNOTSUPP; 171601d5d965SLeah Rumancik 171701d5d965SLeah Rumancik /* 171801d5d965SLeah Rumancik * lookup block mapping and issue discard/zeroout for each 171901d5d965SLeah Rumancik * contiguous region 172001d5d965SLeah Rumancik */ 172101d5d965SLeah Rumancik log_offset = be32_to_cpu(journal->j_superblock->s_first); 172201d5d965SLeah Rumancik block_start = ~0ULL; 172301d5d965SLeah Rumancik for (block = log_offset; block < journal->j_total_len; block++) { 172401d5d965SLeah Rumancik err = jbd2_journal_bmap(journal, block, &phys_block); 172501d5d965SLeah Rumancik if (err) { 172601d5d965SLeah Rumancik pr_err("JBD2: bad block at offset %lu", block); 172701d5d965SLeah Rumancik return err; 172801d5d965SLeah Rumancik } 172901d5d965SLeah Rumancik 173001d5d965SLeah Rumancik if (block_start == ~0ULL) { 173101d5d965SLeah Rumancik block_start = phys_block; 173201d5d965SLeah Rumancik block_stop = block_start - 1; 173301d5d965SLeah Rumancik } 173401d5d965SLeah Rumancik 173501d5d965SLeah Rumancik /* 173601d5d965SLeah Rumancik * last block not contiguous with current block, 173701d5d965SLeah Rumancik * process last contiguous region and return to this block on 173801d5d965SLeah Rumancik * next loop 173901d5d965SLeah Rumancik */ 174001d5d965SLeah Rumancik if (phys_block != block_stop + 1) { 174101d5d965SLeah Rumancik block--; 174201d5d965SLeah Rumancik } else { 174301d5d965SLeah Rumancik block_stop++; 174401d5d965SLeah Rumancik /* 174501d5d965SLeah Rumancik * if this isn't the last block of journal, 174601d5d965SLeah Rumancik * no need to process now because next block may also 174701d5d965SLeah Rumancik * be part of this contiguous region 174801d5d965SLeah Rumancik */ 174901d5d965SLeah Rumancik if (block != journal->j_total_len - 1) 175001d5d965SLeah Rumancik continue; 175101d5d965SLeah Rumancik } 175201d5d965SLeah Rumancik 175301d5d965SLeah Rumancik /* 175401d5d965SLeah Rumancik * end of contiguous region or this is last block of journal, 175501d5d965SLeah Rumancik * take care of the region 175601d5d965SLeah Rumancik */ 175701d5d965SLeah Rumancik byte_start = block_start * journal->j_blocksize; 175801d5d965SLeah Rumancik byte_stop = block_stop * journal->j_blocksize; 175901d5d965SLeah Rumancik byte_count = (block_stop - block_start + 1) * 176001d5d965SLeah Rumancik journal->j_blocksize; 176101d5d965SLeah Rumancik 176201d5d965SLeah Rumancik truncate_inode_pages_range(journal->j_dev->bd_inode->i_mapping, 176301d5d965SLeah Rumancik byte_start, byte_stop); 176401d5d965SLeah Rumancik 176501d5d965SLeah Rumancik if (flags & JBD2_JOURNAL_FLUSH_DISCARD) { 176601d5d965SLeah Rumancik err = blkdev_issue_discard(journal->j_dev, 176701d5d965SLeah Rumancik byte_start >> SECTOR_SHIFT, 176801d5d965SLeah Rumancik byte_count >> SECTOR_SHIFT, 176901d5d965SLeah Rumancik GFP_NOFS, 0); 177001d5d965SLeah Rumancik } else if (flags & JBD2_JOURNAL_FLUSH_ZEROOUT) { 177101d5d965SLeah Rumancik err = blkdev_issue_zeroout(journal->j_dev, 177201d5d965SLeah Rumancik byte_start >> SECTOR_SHIFT, 177301d5d965SLeah Rumancik byte_count >> SECTOR_SHIFT, 177401d5d965SLeah Rumancik GFP_NOFS, 0); 177501d5d965SLeah Rumancik } 177601d5d965SLeah Rumancik 177701d5d965SLeah Rumancik if (unlikely(err != 0)) { 177801d5d965SLeah Rumancik pr_err("JBD2: (error %d) unable to wipe journal at physical blocks %llu - %llu", 177901d5d965SLeah Rumancik err, block_start, block_stop); 178001d5d965SLeah Rumancik return err; 178101d5d965SLeah Rumancik } 178201d5d965SLeah Rumancik 178301d5d965SLeah Rumancik /* reset start and stop after processing a region */ 178401d5d965SLeah Rumancik block_start = ~0ULL; 178501d5d965SLeah Rumancik } 178601d5d965SLeah Rumancik 178701d5d965SLeah Rumancik return blkdev_issue_flush(journal->j_dev); 178801d5d965SLeah Rumancik } 178924bcc89cSJan Kara 179024bcc89cSJan Kara /** 179124bcc89cSJan Kara * jbd2_journal_update_sb_errno() - Update error in the journal. 179224bcc89cSJan Kara * @journal: The journal to update. 179324bcc89cSJan Kara * 179424bcc89cSJan Kara * Update a journal's errno. Write updated superblock to disk waiting for IO 179524bcc89cSJan Kara * to complete. 179624bcc89cSJan Kara */ 1797d796c52eSTheodore Ts'o void jbd2_journal_update_sb_errno(journal_t *journal) 179824bcc89cSJan Kara { 179924bcc89cSJan Kara journal_superblock_t *sb = journal->j_superblock; 1800fb7c0244STheodore Ts'o int errcode; 180124bcc89cSJan Kara 1802538bcaa6STheodore Ts'o lock_buffer(journal->j_sb_buffer); 1803fb7c0244STheodore Ts'o errcode = journal->j_errno; 1804fb7c0244STheodore Ts'o if (errcode == -ESHUTDOWN) 1805fb7c0244STheodore Ts'o errcode = 0; 1806fb7c0244STheodore Ts'o jbd_debug(1, "JBD2: updating superblock error (errno %d)\n", errcode); 1807fb7c0244STheodore Ts'o sb->s_errno = cpu_to_be32(errcode); 1808470decc6SDave Kleikamp 180917f423b5SJan Kara jbd2_write_superblock(journal, REQ_SYNC | REQ_FUA); 1810470decc6SDave Kleikamp } 1811d796c52eSTheodore Ts'o EXPORT_SYMBOL(jbd2_journal_update_sb_errno); 1812470decc6SDave Kleikamp 1813fdc3ef88SJan Kara static int journal_revoke_records_per_block(journal_t *journal) 1814fdc3ef88SJan Kara { 1815fdc3ef88SJan Kara int record_size; 1816fdc3ef88SJan Kara int space = journal->j_blocksize - sizeof(jbd2_journal_revoke_header_t); 1817fdc3ef88SJan Kara 1818fdc3ef88SJan Kara if (jbd2_has_feature_64bit(journal)) 1819fdc3ef88SJan Kara record_size = 8; 1820fdc3ef88SJan Kara else 1821fdc3ef88SJan Kara record_size = 4; 1822fdc3ef88SJan Kara 1823fdc3ef88SJan Kara if (jbd2_journal_has_csum_v2or3(journal)) 1824fdc3ef88SJan Kara space -= sizeof(struct jbd2_journal_block_tail); 1825fdc3ef88SJan Kara return space / record_size; 1826fdc3ef88SJan Kara } 1827fdc3ef88SJan Kara 1828470decc6SDave Kleikamp /* 1829470decc6SDave Kleikamp * Read the superblock for a given journal, performing initial 1830470decc6SDave Kleikamp * validation of the format. 1831470decc6SDave Kleikamp */ 1832470decc6SDave Kleikamp static int journal_get_superblock(journal_t *journal) 1833470decc6SDave Kleikamp { 1834470decc6SDave Kleikamp struct buffer_head *bh; 1835470decc6SDave Kleikamp journal_superblock_t *sb; 1836470decc6SDave Kleikamp int err = -EIO; 1837470decc6SDave Kleikamp 1838470decc6SDave Kleikamp bh = journal->j_sb_buffer; 1839470decc6SDave Kleikamp 1840470decc6SDave Kleikamp J_ASSERT(bh != NULL); 1841470decc6SDave Kleikamp if (!buffer_uptodate(bh)) { 1842dfec8a14SMike Christie ll_rw_block(REQ_OP_READ, 0, 1, &bh); 1843470decc6SDave Kleikamp wait_on_buffer(bh); 1844470decc6SDave Kleikamp if (!buffer_uptodate(bh)) { 1845470decc6SDave Kleikamp printk(KERN_ERR 1846f2a44523SEryu Guan "JBD2: IO error reading journal superblock\n"); 1847470decc6SDave Kleikamp goto out; 1848470decc6SDave Kleikamp } 1849470decc6SDave Kleikamp } 1850470decc6SDave Kleikamp 185125ed6e8aSDarrick J. Wong if (buffer_verified(bh)) 185225ed6e8aSDarrick J. Wong return 0; 185325ed6e8aSDarrick J. Wong 1854470decc6SDave Kleikamp sb = journal->j_superblock; 1855470decc6SDave Kleikamp 1856470decc6SDave Kleikamp err = -EINVAL; 1857470decc6SDave Kleikamp 1858f7f4bccbSMingming Cao if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) || 1859470decc6SDave Kleikamp sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) { 1860f2a44523SEryu Guan printk(KERN_WARNING "JBD2: no valid journal superblock found\n"); 1861470decc6SDave Kleikamp goto out; 1862470decc6SDave Kleikamp } 1863470decc6SDave Kleikamp 1864470decc6SDave Kleikamp switch(be32_to_cpu(sb->s_header.h_blocktype)) { 1865f7f4bccbSMingming Cao case JBD2_SUPERBLOCK_V1: 1866470decc6SDave Kleikamp journal->j_format_version = 1; 1867470decc6SDave Kleikamp break; 1868f7f4bccbSMingming Cao case JBD2_SUPERBLOCK_V2: 1869470decc6SDave Kleikamp journal->j_format_version = 2; 1870470decc6SDave Kleikamp break; 1871470decc6SDave Kleikamp default: 1872f2a44523SEryu Guan printk(KERN_WARNING "JBD2: unrecognised superblock format ID\n"); 1873470decc6SDave Kleikamp goto out; 1874470decc6SDave Kleikamp } 1875470decc6SDave Kleikamp 1876ede7dc7fSHarshad Shirwadkar if (be32_to_cpu(sb->s_maxlen) < journal->j_total_len) 1877ede7dc7fSHarshad Shirwadkar journal->j_total_len = be32_to_cpu(sb->s_maxlen); 1878ede7dc7fSHarshad Shirwadkar else if (be32_to_cpu(sb->s_maxlen) > journal->j_total_len) { 1879f2a44523SEryu Guan printk(KERN_WARNING "JBD2: journal file too short\n"); 1880470decc6SDave Kleikamp goto out; 1881470decc6SDave Kleikamp } 1882470decc6SDave Kleikamp 18838762202dSEryu Guan if (be32_to_cpu(sb->s_first) == 0 || 1884ede7dc7fSHarshad Shirwadkar be32_to_cpu(sb->s_first) >= journal->j_total_len) { 18858762202dSEryu Guan printk(KERN_WARNING 18868762202dSEryu Guan "JBD2: Invalid start block of journal: %u\n", 18878762202dSEryu Guan be32_to_cpu(sb->s_first)); 18888762202dSEryu Guan goto out; 18898762202dSEryu Guan } 18908762202dSEryu Guan 189156316a0dSDarrick J. Wong if (jbd2_has_feature_csum2(journal) && 189256316a0dSDarrick J. Wong jbd2_has_feature_csum3(journal)) { 1893db9ee220SDarrick J. Wong /* Can't have checksum v2 and v3 at the same time! */ 1894db9ee220SDarrick J. Wong printk(KERN_ERR "JBD2: Can't enable checksumming v2 and v3 " 1895db9ee220SDarrick J. Wong "at the same time!\n"); 1896db9ee220SDarrick J. Wong goto out; 1897db9ee220SDarrick J. Wong } 1898db9ee220SDarrick J. Wong 18998595798cSDarrick J. Wong if (jbd2_journal_has_csum_v2or3_feature(journal) && 190056316a0dSDarrick J. Wong jbd2_has_feature_checksum(journal)) { 1901feb8c6d3SDarrick J. Wong /* Can't have checksum v1 and v2 on at the same time! */ 1902feb8c6d3SDarrick J. Wong printk(KERN_ERR "JBD2: Can't enable checksumming v1 and v2/3 " 1903feb8c6d3SDarrick J. Wong "at the same time!\n"); 1904feb8c6d3SDarrick J. Wong goto out; 1905feb8c6d3SDarrick J. Wong } 1906feb8c6d3SDarrick J. Wong 190725ed6e8aSDarrick J. Wong if (!jbd2_verify_csum_type(journal, sb)) { 1908a67c848aSDmitry Monakhov printk(KERN_ERR "JBD2: Unknown checksum type\n"); 190925ed6e8aSDarrick J. Wong goto out; 191025ed6e8aSDarrick J. Wong } 191125ed6e8aSDarrick J. Wong 191201b5adceSDarrick J. Wong /* Load the checksum driver */ 19138595798cSDarrick J. Wong if (jbd2_journal_has_csum_v2or3_feature(journal)) { 191401b5adceSDarrick J. Wong journal->j_chksum_driver = crypto_alloc_shash("crc32c", 0, 0); 191501b5adceSDarrick J. Wong if (IS_ERR(journal->j_chksum_driver)) { 1916a67c848aSDmitry Monakhov printk(KERN_ERR "JBD2: Cannot load crc32c driver.\n"); 191701b5adceSDarrick J. Wong err = PTR_ERR(journal->j_chksum_driver); 191801b5adceSDarrick J. Wong journal->j_chksum_driver = NULL; 191901b5adceSDarrick J. Wong goto out; 192001b5adceSDarrick J. Wong } 192101b5adceSDarrick J. Wong } 192201b5adceSDarrick J. Wong 1923a58ca992STheodore Ts'o if (jbd2_journal_has_csum_v2or3(journal)) { 19244fd5ea43SDarrick J. Wong /* Check superblock checksum */ 1925a58ca992STheodore Ts'o if (sb->s_checksum != jbd2_superblock_csum(journal, sb)) { 1926a67c848aSDmitry Monakhov printk(KERN_ERR "JBD2: journal checksum error\n"); 19276a797d27SDarrick J. Wong err = -EFSBADCRC; 19284fd5ea43SDarrick J. Wong goto out; 19294fd5ea43SDarrick J. Wong } 19304fd5ea43SDarrick J. Wong 19314fd5ea43SDarrick J. Wong /* Precompute checksum seed for all metadata */ 19324fd5ea43SDarrick J. Wong journal->j_csum_seed = jbd2_chksum(journal, ~0, sb->s_uuid, 19334fd5ea43SDarrick J. Wong sizeof(sb->s_uuid)); 1934a58ca992STheodore Ts'o } 19354fd5ea43SDarrick J. Wong 1936fdc3ef88SJan Kara journal->j_revoke_records_per_block = 1937fdc3ef88SJan Kara journal_revoke_records_per_block(journal); 193825ed6e8aSDarrick J. Wong set_buffer_verified(bh); 193925ed6e8aSDarrick J. Wong 1940470decc6SDave Kleikamp return 0; 1941470decc6SDave Kleikamp 1942470decc6SDave Kleikamp out: 1943470decc6SDave Kleikamp journal_fail_superblock(journal); 1944470decc6SDave Kleikamp return err; 1945470decc6SDave Kleikamp } 1946470decc6SDave Kleikamp 1947470decc6SDave Kleikamp /* 1948470decc6SDave Kleikamp * Load the on-disk journal superblock and read the key fields into the 1949470decc6SDave Kleikamp * journal_t. 1950470decc6SDave Kleikamp */ 1951470decc6SDave Kleikamp 1952470decc6SDave Kleikamp static int load_superblock(journal_t *journal) 1953470decc6SDave Kleikamp { 1954470decc6SDave Kleikamp int err; 1955470decc6SDave Kleikamp journal_superblock_t *sb; 1956a1e5e465SHarshad Shirwadkar int num_fc_blocks; 1957470decc6SDave Kleikamp 1958470decc6SDave Kleikamp err = journal_get_superblock(journal); 1959470decc6SDave Kleikamp if (err) 1960470decc6SDave Kleikamp return err; 1961470decc6SDave Kleikamp 1962470decc6SDave Kleikamp sb = journal->j_superblock; 1963470decc6SDave Kleikamp 1964470decc6SDave Kleikamp journal->j_tail_sequence = be32_to_cpu(sb->s_sequence); 1965470decc6SDave Kleikamp journal->j_tail = be32_to_cpu(sb->s_start); 1966470decc6SDave Kleikamp journal->j_first = be32_to_cpu(sb->s_first); 1967470decc6SDave Kleikamp journal->j_errno = be32_to_cpu(sb->s_errno); 1968a1e5e465SHarshad Shirwadkar journal->j_last = be32_to_cpu(sb->s_maxlen); 1969470decc6SDave Kleikamp 1970a1e5e465SHarshad Shirwadkar if (jbd2_has_feature_fast_commit(journal)) { 19716866d7b3SHarshad Shirwadkar journal->j_fc_last = be32_to_cpu(sb->s_maxlen); 19729bd23c31SHarshad Shirwadkar num_fc_blocks = jbd2_journal_get_num_fc_blks(sb); 1973a1e5e465SHarshad Shirwadkar if (journal->j_last - num_fc_blocks >= JBD2_MIN_JOURNAL_BLOCKS) 1974a1e5e465SHarshad Shirwadkar journal->j_last = journal->j_fc_last - num_fc_blocks; 19756866d7b3SHarshad Shirwadkar journal->j_fc_first = journal->j_last + 1; 19766866d7b3SHarshad Shirwadkar journal->j_fc_off = 0; 19776866d7b3SHarshad Shirwadkar } 19786866d7b3SHarshad Shirwadkar 1979470decc6SDave Kleikamp return 0; 1980470decc6SDave Kleikamp } 1981470decc6SDave Kleikamp 1982470decc6SDave Kleikamp 1983470decc6SDave Kleikamp /** 19842bf31d94SMauro Carvalho Chehab * jbd2_journal_load() - Read journal from disk. 1985470decc6SDave Kleikamp * @journal: Journal to act on. 1986470decc6SDave Kleikamp * 1987470decc6SDave Kleikamp * Given a journal_t structure which tells us which disk blocks contain 1988470decc6SDave Kleikamp * a journal, read the journal from disk to initialise the in-memory 1989470decc6SDave Kleikamp * structures. 1990470decc6SDave Kleikamp */ 1991f7f4bccbSMingming Cao int jbd2_journal_load(journal_t *journal) 1992470decc6SDave Kleikamp { 1993470decc6SDave Kleikamp int err; 1994470decc6SDave Kleikamp journal_superblock_t *sb; 1995470decc6SDave Kleikamp 1996470decc6SDave Kleikamp err = load_superblock(journal); 1997470decc6SDave Kleikamp if (err) 1998470decc6SDave Kleikamp return err; 1999470decc6SDave Kleikamp 2000470decc6SDave Kleikamp sb = journal->j_superblock; 2001470decc6SDave Kleikamp /* If this is a V2 superblock, then we have to check the 2002470decc6SDave Kleikamp * features flags on it. */ 2003470decc6SDave Kleikamp 2004470decc6SDave Kleikamp if (journal->j_format_version >= 2) { 2005470decc6SDave Kleikamp if ((sb->s_feature_ro_compat & 2006f7f4bccbSMingming Cao ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) || 2007470decc6SDave Kleikamp (sb->s_feature_incompat & 2008f7f4bccbSMingming Cao ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) { 2009470decc6SDave Kleikamp printk(KERN_WARNING 2010f2a44523SEryu Guan "JBD2: Unrecognised features on journal\n"); 2011470decc6SDave Kleikamp return -EINVAL; 2012470decc6SDave Kleikamp } 2013470decc6SDave Kleikamp } 2014470decc6SDave Kleikamp 2015d2eecb03STheodore Ts'o /* 2016d2eecb03STheodore Ts'o * Create a slab for this blocksize 2017d2eecb03STheodore Ts'o */ 2018d2eecb03STheodore Ts'o err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize)); 2019d2eecb03STheodore Ts'o if (err) 2020d2eecb03STheodore Ts'o return err; 2021d2eecb03STheodore Ts'o 2022470decc6SDave Kleikamp /* Let the recovery code check whether it needs to recover any 2023470decc6SDave Kleikamp * data from the journal. */ 2024f7f4bccbSMingming Cao if (jbd2_journal_recover(journal)) 2025470decc6SDave Kleikamp goto recovery_error; 2026470decc6SDave Kleikamp 2027e6a47428STheodore Ts'o if (journal->j_failed_commit) { 2028e6a47428STheodore Ts'o printk(KERN_ERR "JBD2: journal transaction %u on %s " 2029e6a47428STheodore Ts'o "is corrupt.\n", journal->j_failed_commit, 2030e6a47428STheodore Ts'o journal->j_devname); 20316a797d27SDarrick J. Wong return -EFSCORRUPTED; 2032e6a47428STheodore Ts'o } 2033a09decffSKai Li /* 2034a09decffSKai Li * clear JBD2_ABORT flag initialized in journal_init_common 2035a09decffSKai Li * here to update log tail information with the newest seq. 2036a09decffSKai Li */ 2037a09decffSKai Li journal->j_flags &= ~JBD2_ABORT; 2038e6a47428STheodore Ts'o 2039470decc6SDave Kleikamp /* OK, we've finished with the dynamic journal bits: 2040470decc6SDave Kleikamp * reinitialise the dynamic contents of the superblock in memory 2041470decc6SDave Kleikamp * and reset them on disk. */ 2042470decc6SDave Kleikamp if (journal_reset(journal)) 2043470decc6SDave Kleikamp goto recovery_error; 2044470decc6SDave Kleikamp 2045f7f4bccbSMingming Cao journal->j_flags |= JBD2_LOADED; 2046470decc6SDave Kleikamp return 0; 2047470decc6SDave Kleikamp 2048470decc6SDave Kleikamp recovery_error: 2049f2a44523SEryu Guan printk(KERN_WARNING "JBD2: recovery failed\n"); 2050470decc6SDave Kleikamp return -EIO; 2051470decc6SDave Kleikamp } 2052470decc6SDave Kleikamp 2053470decc6SDave Kleikamp /** 20542bf31d94SMauro Carvalho Chehab * jbd2_journal_destroy() - Release a journal_t structure. 2055470decc6SDave Kleikamp * @journal: Journal to act on. 2056470decc6SDave Kleikamp * 2057470decc6SDave Kleikamp * Release a journal_t structure once it is no longer in use by the 2058470decc6SDave Kleikamp * journaled object. 205944519fafSHidehiro Kawai * Return <0 if we couldn't clean up the journal. 2060470decc6SDave Kleikamp */ 206144519fafSHidehiro Kawai int jbd2_journal_destroy(journal_t *journal) 2062470decc6SDave Kleikamp { 206344519fafSHidehiro Kawai int err = 0; 206444519fafSHidehiro Kawai 2065470decc6SDave Kleikamp /* Wait for the commit thread to wake up and die. */ 2066470decc6SDave Kleikamp journal_kill_thread(journal); 2067470decc6SDave Kleikamp 2068470decc6SDave Kleikamp /* Force a final log commit */ 2069470decc6SDave Kleikamp if (journal->j_running_transaction) 2070f7f4bccbSMingming Cao jbd2_journal_commit_transaction(journal); 2071470decc6SDave Kleikamp 2072470decc6SDave Kleikamp /* Force any old transactions to disk */ 2073470decc6SDave Kleikamp 2074470decc6SDave Kleikamp /* Totally anal locking here... */ 2075470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 2076470decc6SDave Kleikamp while (journal->j_checkpoint_transactions != NULL) { 2077470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 20786fa7aa50STejun Heo mutex_lock_io(&journal->j_checkpoint_mutex); 2079841df7dfSJan Kara err = jbd2_log_do_checkpoint(journal); 20801a0d3786STheodore Ts'o mutex_unlock(&journal->j_checkpoint_mutex); 2081841df7dfSJan Kara /* 2082841df7dfSJan Kara * If checkpointing failed, just free the buffers to avoid 2083841df7dfSJan Kara * looping forever 2084841df7dfSJan Kara */ 2085841df7dfSJan Kara if (err) { 2086841df7dfSJan Kara jbd2_journal_destroy_checkpoint(journal); 2087841df7dfSJan Kara spin_lock(&journal->j_list_lock); 2088841df7dfSJan Kara break; 2089841df7dfSJan Kara } 2090470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 2091470decc6SDave Kleikamp } 2092470decc6SDave Kleikamp 2093470decc6SDave Kleikamp J_ASSERT(journal->j_running_transaction == NULL); 2094470decc6SDave Kleikamp J_ASSERT(journal->j_committing_transaction == NULL); 2095470decc6SDave Kleikamp J_ASSERT(journal->j_checkpoint_transactions == NULL); 2096470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 2097470decc6SDave Kleikamp 2098*fcf37549SZhang Yi /* 2099*fcf37549SZhang Yi * OK, all checkpoint transactions have been checked, now check the 2100*fcf37549SZhang Yi * write out io error flag and abort the journal if some buffer failed 2101*fcf37549SZhang Yi * to write back to the original location, otherwise the filesystem 2102*fcf37549SZhang Yi * may become inconsistent. 2103*fcf37549SZhang Yi */ 2104*fcf37549SZhang Yi if (!is_journal_aborted(journal) && 2105*fcf37549SZhang Yi test_bit(JBD2_CHECKPOINT_IO_ERROR, &journal->j_atomic_flags)) 2106*fcf37549SZhang Yi jbd2_journal_abort(journal, -EIO); 2107*fcf37549SZhang Yi 210844519fafSHidehiro Kawai if (journal->j_sb_buffer) { 210944519fafSHidehiro Kawai if (!is_journal_aborted(journal)) { 21106fa7aa50STejun Heo mutex_lock_io(&journal->j_checkpoint_mutex); 2111c0a2ad9bSOGAWA Hirofumi 2112c0a2ad9bSOGAWA Hirofumi write_lock(&journal->j_state_lock); 2113c0a2ad9bSOGAWA Hirofumi journal->j_tail_sequence = 2114c0a2ad9bSOGAWA Hirofumi ++journal->j_transaction_sequence; 2115c0a2ad9bSOGAWA Hirofumi write_unlock(&journal->j_state_lock); 2116c0a2ad9bSOGAWA Hirofumi 211770fd7614SChristoph Hellwig jbd2_mark_journal_empty(journal, 211817f423b5SJan Kara REQ_SYNC | REQ_PREFLUSH | REQ_FUA); 2119a78bb11dSJan Kara mutex_unlock(&journal->j_checkpoint_mutex); 2120a78bb11dSJan Kara } else 212144519fafSHidehiro Kawai err = -EIO; 2122470decc6SDave Kleikamp brelse(journal->j_sb_buffer); 2123470decc6SDave Kleikamp } 2124470decc6SDave Kleikamp 21258e85fb3fSJohann Lombardi if (journal->j_proc_entry) 21268e85fb3fSJohann Lombardi jbd2_stats_proc_exit(journal); 2127470decc6SDave Kleikamp iput(journal->j_inode); 2128470decc6SDave Kleikamp if (journal->j_revoke) 2129f7f4bccbSMingming Cao jbd2_journal_destroy_revoke(journal); 213001b5adceSDarrick J. Wong if (journal->j_chksum_driver) 213101b5adceSDarrick J. Wong crypto_free_shash(journal->j_chksum_driver); 21326866d7b3SHarshad Shirwadkar kfree(journal->j_fc_wbuf); 2133470decc6SDave Kleikamp kfree(journal->j_wbuf); 2134470decc6SDave Kleikamp kfree(journal); 213544519fafSHidehiro Kawai 213644519fafSHidehiro Kawai return err; 2137470decc6SDave Kleikamp } 2138470decc6SDave Kleikamp 2139470decc6SDave Kleikamp 2140470decc6SDave Kleikamp /** 21412bf31d94SMauro Carvalho Chehab * jbd2_journal_check_used_features() - Check if features specified are used. 2142470decc6SDave Kleikamp * @journal: Journal to check. 2143470decc6SDave Kleikamp * @compat: bitmask of compatible features 2144470decc6SDave Kleikamp * @ro: bitmask of features that force read-only mount 2145470decc6SDave Kleikamp * @incompat: bitmask of incompatible features 2146470decc6SDave Kleikamp * 2147470decc6SDave Kleikamp * Check whether the journal uses all of a given set of 2148470decc6SDave Kleikamp * features. Return true (non-zero) if it does. 2149470decc6SDave Kleikamp **/ 2150470decc6SDave Kleikamp 2151f7f4bccbSMingming Cao int jbd2_journal_check_used_features(journal_t *journal, unsigned long compat, 2152470decc6SDave Kleikamp unsigned long ro, unsigned long incompat) 2153470decc6SDave Kleikamp { 2154470decc6SDave Kleikamp journal_superblock_t *sb; 2155470decc6SDave Kleikamp 2156470decc6SDave Kleikamp if (!compat && !ro && !incompat) 2157470decc6SDave Kleikamp return 1; 21581113e1b5SPatrick J. LoPresti /* Load journal superblock if it is not loaded yet. */ 21591113e1b5SPatrick J. LoPresti if (journal->j_format_version == 0 && 21601113e1b5SPatrick J. LoPresti journal_get_superblock(journal) != 0) 21611113e1b5SPatrick J. LoPresti return 0; 2162470decc6SDave Kleikamp if (journal->j_format_version == 1) 2163470decc6SDave Kleikamp return 0; 2164470decc6SDave Kleikamp 2165470decc6SDave Kleikamp sb = journal->j_superblock; 2166470decc6SDave Kleikamp 2167470decc6SDave Kleikamp if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) && 2168470decc6SDave Kleikamp ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) && 2169470decc6SDave Kleikamp ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat)) 2170470decc6SDave Kleikamp return 1; 2171470decc6SDave Kleikamp 2172470decc6SDave Kleikamp return 0; 2173470decc6SDave Kleikamp } 2174470decc6SDave Kleikamp 2175470decc6SDave Kleikamp /** 21762bf31d94SMauro Carvalho Chehab * jbd2_journal_check_available_features() - Check feature set in journalling layer 2177470decc6SDave Kleikamp * @journal: Journal to check. 2178470decc6SDave Kleikamp * @compat: bitmask of compatible features 2179470decc6SDave Kleikamp * @ro: bitmask of features that force read-only mount 2180470decc6SDave Kleikamp * @incompat: bitmask of incompatible features 2181470decc6SDave Kleikamp * 2182470decc6SDave Kleikamp * Check whether the journaling code supports the use of 2183470decc6SDave Kleikamp * all of a given set of features on this journal. Return true 2184470decc6SDave Kleikamp * (non-zero) if it can. */ 2185470decc6SDave Kleikamp 2186f7f4bccbSMingming Cao int jbd2_journal_check_available_features(journal_t *journal, unsigned long compat, 2187470decc6SDave Kleikamp unsigned long ro, unsigned long incompat) 2188470decc6SDave Kleikamp { 2189470decc6SDave Kleikamp if (!compat && !ro && !incompat) 2190470decc6SDave Kleikamp return 1; 2191470decc6SDave Kleikamp 2192470decc6SDave Kleikamp /* We can support any known requested features iff the 2193470decc6SDave Kleikamp * superblock is in version 2. Otherwise we fail to support any 2194470decc6SDave Kleikamp * extended sb features. */ 2195470decc6SDave Kleikamp 2196470decc6SDave Kleikamp if (journal->j_format_version != 2) 2197470decc6SDave Kleikamp return 0; 2198470decc6SDave Kleikamp 2199f7f4bccbSMingming Cao if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat && 2200f7f4bccbSMingming Cao (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro && 2201f7f4bccbSMingming Cao (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat) 2202470decc6SDave Kleikamp return 1; 2203470decc6SDave Kleikamp 2204470decc6SDave Kleikamp return 0; 2205470decc6SDave Kleikamp } 2206470decc6SDave Kleikamp 2207a1e5e465SHarshad Shirwadkar static int 2208a1e5e465SHarshad Shirwadkar jbd2_journal_initialize_fast_commit(journal_t *journal) 2209a1e5e465SHarshad Shirwadkar { 2210a1e5e465SHarshad Shirwadkar journal_superblock_t *sb = journal->j_superblock; 2211a1e5e465SHarshad Shirwadkar unsigned long long num_fc_blks; 2212a1e5e465SHarshad Shirwadkar 22139bd23c31SHarshad Shirwadkar num_fc_blks = jbd2_journal_get_num_fc_blks(sb); 2214a1e5e465SHarshad Shirwadkar if (journal->j_last - num_fc_blks < JBD2_MIN_JOURNAL_BLOCKS) 2215a1e5e465SHarshad Shirwadkar return -ENOSPC; 2216a1e5e465SHarshad Shirwadkar 2217a1e5e465SHarshad Shirwadkar /* Are we called twice? */ 2218a1e5e465SHarshad Shirwadkar WARN_ON(journal->j_fc_wbuf != NULL); 2219a1e5e465SHarshad Shirwadkar journal->j_fc_wbuf = kmalloc_array(num_fc_blks, 2220a1e5e465SHarshad Shirwadkar sizeof(struct buffer_head *), GFP_KERNEL); 2221a1e5e465SHarshad Shirwadkar if (!journal->j_fc_wbuf) 2222a1e5e465SHarshad Shirwadkar return -ENOMEM; 2223a1e5e465SHarshad Shirwadkar 2224a1e5e465SHarshad Shirwadkar journal->j_fc_wbufsize = num_fc_blks; 2225a1e5e465SHarshad Shirwadkar journal->j_fc_last = journal->j_last; 2226a1e5e465SHarshad Shirwadkar journal->j_last = journal->j_fc_last - num_fc_blks; 2227a1e5e465SHarshad Shirwadkar journal->j_fc_first = journal->j_last + 1; 2228a1e5e465SHarshad Shirwadkar journal->j_fc_off = 0; 2229a1e5e465SHarshad Shirwadkar journal->j_free = journal->j_last - journal->j_first; 2230a1e5e465SHarshad Shirwadkar journal->j_max_transaction_buffers = 2231a1e5e465SHarshad Shirwadkar jbd2_journal_get_max_txn_bufs(journal); 2232a1e5e465SHarshad Shirwadkar 2233a1e5e465SHarshad Shirwadkar return 0; 2234a1e5e465SHarshad Shirwadkar } 2235a1e5e465SHarshad Shirwadkar 2236470decc6SDave Kleikamp /** 22372bf31d94SMauro Carvalho Chehab * jbd2_journal_set_features() - Mark a given journal feature in the superblock 2238470decc6SDave Kleikamp * @journal: Journal to act on. 2239470decc6SDave Kleikamp * @compat: bitmask of compatible features 2240470decc6SDave Kleikamp * @ro: bitmask of features that force read-only mount 2241470decc6SDave Kleikamp * @incompat: bitmask of incompatible features 2242470decc6SDave Kleikamp * 2243470decc6SDave Kleikamp * Mark a given journal feature as present on the 2244470decc6SDave Kleikamp * superblock. Returns true if the requested features could be set. 2245470decc6SDave Kleikamp * 2246470decc6SDave Kleikamp */ 2247470decc6SDave Kleikamp 2248f7f4bccbSMingming Cao int jbd2_journal_set_features(journal_t *journal, unsigned long compat, 2249470decc6SDave Kleikamp unsigned long ro, unsigned long incompat) 2250470decc6SDave Kleikamp { 225125ed6e8aSDarrick J. Wong #define INCOMPAT_FEATURE_ON(f) \ 225225ed6e8aSDarrick J. Wong ((incompat & (f)) && !(sb->s_feature_incompat & cpu_to_be32(f))) 225325ed6e8aSDarrick J. Wong #define COMPAT_FEATURE_ON(f) \ 225425ed6e8aSDarrick J. Wong ((compat & (f)) && !(sb->s_feature_compat & cpu_to_be32(f))) 2255470decc6SDave Kleikamp journal_superblock_t *sb; 2256470decc6SDave Kleikamp 2257f7f4bccbSMingming Cao if (jbd2_journal_check_used_features(journal, compat, ro, incompat)) 2258470decc6SDave Kleikamp return 1; 2259470decc6SDave Kleikamp 2260f7f4bccbSMingming Cao if (!jbd2_journal_check_available_features(journal, compat, ro, incompat)) 2261470decc6SDave Kleikamp return 0; 2262470decc6SDave Kleikamp 2263db9ee220SDarrick J. Wong /* If enabling v2 checksums, turn on v3 instead */ 2264db9ee220SDarrick J. Wong if (incompat & JBD2_FEATURE_INCOMPAT_CSUM_V2) { 2265db9ee220SDarrick J. Wong incompat &= ~JBD2_FEATURE_INCOMPAT_CSUM_V2; 2266db9ee220SDarrick J. Wong incompat |= JBD2_FEATURE_INCOMPAT_CSUM_V3; 2267db9ee220SDarrick J. Wong } 2268db9ee220SDarrick J. Wong 2269db9ee220SDarrick J. Wong /* Asking for checksumming v3 and v1? Only give them v3. */ 2270db9ee220SDarrick J. Wong if (incompat & JBD2_FEATURE_INCOMPAT_CSUM_V3 && 227125ed6e8aSDarrick J. Wong compat & JBD2_FEATURE_COMPAT_CHECKSUM) 227225ed6e8aSDarrick J. Wong compat &= ~JBD2_FEATURE_COMPAT_CHECKSUM; 227325ed6e8aSDarrick J. Wong 2274470decc6SDave Kleikamp jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n", 2275470decc6SDave Kleikamp compat, ro, incompat); 2276470decc6SDave Kleikamp 2277470decc6SDave Kleikamp sb = journal->j_superblock; 2278470decc6SDave Kleikamp 2279a1e5e465SHarshad Shirwadkar if (incompat & JBD2_FEATURE_INCOMPAT_FAST_COMMIT) { 2280a1e5e465SHarshad Shirwadkar if (jbd2_journal_initialize_fast_commit(journal)) { 2281a1e5e465SHarshad Shirwadkar pr_err("JBD2: Cannot enable fast commits.\n"); 2282a1e5e465SHarshad Shirwadkar return 0; 2283a1e5e465SHarshad Shirwadkar } 2284a1e5e465SHarshad Shirwadkar } 2285a1e5e465SHarshad Shirwadkar 2286538bcaa6STheodore Ts'o /* Load the checksum driver if necessary */ 2287538bcaa6STheodore Ts'o if ((journal->j_chksum_driver == NULL) && 2288538bcaa6STheodore Ts'o INCOMPAT_FEATURE_ON(JBD2_FEATURE_INCOMPAT_CSUM_V3)) { 2289538bcaa6STheodore Ts'o journal->j_chksum_driver = crypto_alloc_shash("crc32c", 0, 0); 2290538bcaa6STheodore Ts'o if (IS_ERR(journal->j_chksum_driver)) { 2291538bcaa6STheodore Ts'o printk(KERN_ERR "JBD2: Cannot load crc32c driver.\n"); 2292538bcaa6STheodore Ts'o journal->j_chksum_driver = NULL; 2293538bcaa6STheodore Ts'o return 0; 2294538bcaa6STheodore Ts'o } 2295538bcaa6STheodore Ts'o /* Precompute checksum seed for all metadata */ 2296538bcaa6STheodore Ts'o journal->j_csum_seed = jbd2_chksum(journal, ~0, sb->s_uuid, 2297538bcaa6STheodore Ts'o sizeof(sb->s_uuid)); 2298538bcaa6STheodore Ts'o } 2299538bcaa6STheodore Ts'o 2300538bcaa6STheodore Ts'o lock_buffer(journal->j_sb_buffer); 2301538bcaa6STheodore Ts'o 2302db9ee220SDarrick J. Wong /* If enabling v3 checksums, update superblock */ 2303db9ee220SDarrick J. Wong if (INCOMPAT_FEATURE_ON(JBD2_FEATURE_INCOMPAT_CSUM_V3)) { 230425ed6e8aSDarrick J. Wong sb->s_checksum_type = JBD2_CRC32C_CHKSUM; 230525ed6e8aSDarrick J. Wong sb->s_feature_compat &= 230625ed6e8aSDarrick J. Wong ~cpu_to_be32(JBD2_FEATURE_COMPAT_CHECKSUM); 230732f38691SDarrick J. Wong } 230825ed6e8aSDarrick J. Wong 230925ed6e8aSDarrick J. Wong /* If enabling v1 checksums, downgrade superblock */ 231025ed6e8aSDarrick J. Wong if (COMPAT_FEATURE_ON(JBD2_FEATURE_COMPAT_CHECKSUM)) 231125ed6e8aSDarrick J. Wong sb->s_feature_incompat &= 2312db9ee220SDarrick J. Wong ~cpu_to_be32(JBD2_FEATURE_INCOMPAT_CSUM_V2 | 2313db9ee220SDarrick J. Wong JBD2_FEATURE_INCOMPAT_CSUM_V3); 231425ed6e8aSDarrick J. Wong 2315470decc6SDave Kleikamp sb->s_feature_compat |= cpu_to_be32(compat); 2316470decc6SDave Kleikamp sb->s_feature_ro_compat |= cpu_to_be32(ro); 2317470decc6SDave Kleikamp sb->s_feature_incompat |= cpu_to_be32(incompat); 2318538bcaa6STheodore Ts'o unlock_buffer(journal->j_sb_buffer); 2319fdc3ef88SJan Kara journal->j_revoke_records_per_block = 2320fdc3ef88SJan Kara journal_revoke_records_per_block(journal); 2321470decc6SDave Kleikamp 2322470decc6SDave Kleikamp return 1; 232325ed6e8aSDarrick J. Wong #undef COMPAT_FEATURE_ON 232425ed6e8aSDarrick J. Wong #undef INCOMPAT_FEATURE_ON 2325470decc6SDave Kleikamp } 2326470decc6SDave Kleikamp 2327818d276cSGirish Shilamkar /* 2328818d276cSGirish Shilamkar * jbd2_journal_clear_features() - Clear a given journal feature in the 2329818d276cSGirish Shilamkar * superblock 2330818d276cSGirish Shilamkar * @journal: Journal to act on. 2331818d276cSGirish Shilamkar * @compat: bitmask of compatible features 2332818d276cSGirish Shilamkar * @ro: bitmask of features that force read-only mount 2333818d276cSGirish Shilamkar * @incompat: bitmask of incompatible features 2334818d276cSGirish Shilamkar * 2335818d276cSGirish Shilamkar * Clear a given journal feature as present on the 2336818d276cSGirish Shilamkar * superblock. 2337818d276cSGirish Shilamkar */ 2338818d276cSGirish Shilamkar void jbd2_journal_clear_features(journal_t *journal, unsigned long compat, 2339818d276cSGirish Shilamkar unsigned long ro, unsigned long incompat) 2340818d276cSGirish Shilamkar { 2341818d276cSGirish Shilamkar journal_superblock_t *sb; 2342818d276cSGirish Shilamkar 2343818d276cSGirish Shilamkar jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n", 2344818d276cSGirish Shilamkar compat, ro, incompat); 2345818d276cSGirish Shilamkar 2346818d276cSGirish Shilamkar sb = journal->j_superblock; 2347818d276cSGirish Shilamkar 2348818d276cSGirish Shilamkar sb->s_feature_compat &= ~cpu_to_be32(compat); 2349818d276cSGirish Shilamkar sb->s_feature_ro_compat &= ~cpu_to_be32(ro); 2350818d276cSGirish Shilamkar sb->s_feature_incompat &= ~cpu_to_be32(incompat); 2351fdc3ef88SJan Kara journal->j_revoke_records_per_block = 2352fdc3ef88SJan Kara journal_revoke_records_per_block(journal); 2353818d276cSGirish Shilamkar } 2354818d276cSGirish Shilamkar EXPORT_SYMBOL(jbd2_journal_clear_features); 2355470decc6SDave Kleikamp 2356470decc6SDave Kleikamp /** 23572bf31d94SMauro Carvalho Chehab * jbd2_journal_flush() - Flush journal 2358470decc6SDave Kleikamp * @journal: Journal to act on. 235901d5d965SLeah Rumancik * @flags: optional operation on the journal blocks after the flush (see below) 2360470decc6SDave Kleikamp * 2361470decc6SDave Kleikamp * Flush all data for a given journal to disk and empty the journal. 2362470decc6SDave Kleikamp * Filesystems can use this when remounting readonly to ensure that 236301d5d965SLeah Rumancik * recovery does not need to happen on remount. Optionally, a discard or zeroout 236401d5d965SLeah Rumancik * can be issued on the journal blocks after flushing. 236501d5d965SLeah Rumancik * 236601d5d965SLeah Rumancik * flags: 236701d5d965SLeah Rumancik * JBD2_JOURNAL_FLUSH_DISCARD: issues discards for the journal blocks 236801d5d965SLeah Rumancik * JBD2_JOURNAL_FLUSH_ZEROOUT: issues zeroouts for the journal blocks 2369470decc6SDave Kleikamp */ 237001d5d965SLeah Rumancik int jbd2_journal_flush(journal_t *journal, unsigned int flags) 2371470decc6SDave Kleikamp { 2372470decc6SDave Kleikamp int err = 0; 2373470decc6SDave Kleikamp transaction_t *transaction = NULL; 2374470decc6SDave Kleikamp 2375a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 2376470decc6SDave Kleikamp 2377470decc6SDave Kleikamp /* Force everything buffered to the log... */ 2378470decc6SDave Kleikamp if (journal->j_running_transaction) { 2379470decc6SDave Kleikamp transaction = journal->j_running_transaction; 2380f7f4bccbSMingming Cao __jbd2_log_start_commit(journal, transaction->t_tid); 2381470decc6SDave Kleikamp } else if (journal->j_committing_transaction) 2382470decc6SDave Kleikamp transaction = journal->j_committing_transaction; 2383470decc6SDave Kleikamp 2384470decc6SDave Kleikamp /* Wait for the log commit to complete... */ 2385470decc6SDave Kleikamp if (transaction) { 2386470decc6SDave Kleikamp tid_t tid = transaction->t_tid; 2387470decc6SDave Kleikamp 2388a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 2389f7f4bccbSMingming Cao jbd2_log_wait_commit(journal, tid); 2390470decc6SDave Kleikamp } else { 2391a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 2392470decc6SDave Kleikamp } 2393470decc6SDave Kleikamp 2394470decc6SDave Kleikamp /* ...and flush everything in the log out to disk. */ 2395470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 2396470decc6SDave Kleikamp while (!err && journal->j_checkpoint_transactions != NULL) { 2397470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 23986fa7aa50STejun Heo mutex_lock_io(&journal->j_checkpoint_mutex); 2399f7f4bccbSMingming Cao err = jbd2_log_do_checkpoint(journal); 240044519fafSHidehiro Kawai mutex_unlock(&journal->j_checkpoint_mutex); 2401470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 2402470decc6SDave Kleikamp } 2403470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 240444519fafSHidehiro Kawai 240544519fafSHidehiro Kawai if (is_journal_aborted(journal)) 240644519fafSHidehiro Kawai return -EIO; 240744519fafSHidehiro Kawai 24086fa7aa50STejun Heo mutex_lock_io(&journal->j_checkpoint_mutex); 24096f6a6fdaSJoseph Qi if (!err) { 24106f6a6fdaSJoseph Qi err = jbd2_cleanup_journal_tail(journal); 24116f6a6fdaSJoseph Qi if (err < 0) { 24126f6a6fdaSJoseph Qi mutex_unlock(&journal->j_checkpoint_mutex); 24136f6a6fdaSJoseph Qi goto out; 24146f6a6fdaSJoseph Qi } 24156f6a6fdaSJoseph Qi err = 0; 24166f6a6fdaSJoseph Qi } 2417470decc6SDave Kleikamp 2418470decc6SDave Kleikamp /* Finally, mark the journal as really needing no recovery. 2419470decc6SDave Kleikamp * This sets s_start==0 in the underlying superblock, which is 2420470decc6SDave Kleikamp * the magic code for a fully-recovered superblock. Any future 2421470decc6SDave Kleikamp * commits of data to the journal will restore the current 2422470decc6SDave Kleikamp * s_start value. */ 242317f423b5SJan Kara jbd2_mark_journal_empty(journal, REQ_SYNC | REQ_FUA); 242401d5d965SLeah Rumancik 242501d5d965SLeah Rumancik if (flags) 242601d5d965SLeah Rumancik err = __jbd2_journal_erase(journal, flags); 242701d5d965SLeah Rumancik 2428a78bb11dSJan Kara mutex_unlock(&journal->j_checkpoint_mutex); 2429a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 2430470decc6SDave Kleikamp J_ASSERT(!journal->j_running_transaction); 2431470decc6SDave Kleikamp J_ASSERT(!journal->j_committing_transaction); 2432470decc6SDave Kleikamp J_ASSERT(!journal->j_checkpoint_transactions); 2433470decc6SDave Kleikamp J_ASSERT(journal->j_head == journal->j_tail); 2434470decc6SDave Kleikamp J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence); 2435a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 24366f6a6fdaSJoseph Qi out: 24376f6a6fdaSJoseph Qi return err; 2438470decc6SDave Kleikamp } 2439470decc6SDave Kleikamp 2440470decc6SDave Kleikamp /** 24412bf31d94SMauro Carvalho Chehab * jbd2_journal_wipe() - Wipe journal contents 2442470decc6SDave Kleikamp * @journal: Journal to act on. 2443470decc6SDave Kleikamp * @write: flag (see below) 2444470decc6SDave Kleikamp * 2445470decc6SDave Kleikamp * Wipe out all of the contents of a journal, safely. This will produce 2446470decc6SDave Kleikamp * a warning if the journal contains any valid recovery information. 2447f7f4bccbSMingming Cao * Must be called between journal_init_*() and jbd2_journal_load(). 2448470decc6SDave Kleikamp * 2449470decc6SDave Kleikamp * If 'write' is non-zero, then we wipe out the journal on disk; otherwise 2450470decc6SDave Kleikamp * we merely suppress recovery. 2451470decc6SDave Kleikamp */ 2452470decc6SDave Kleikamp 2453f7f4bccbSMingming Cao int jbd2_journal_wipe(journal_t *journal, int write) 2454470decc6SDave Kleikamp { 2455470decc6SDave Kleikamp int err = 0; 2456470decc6SDave Kleikamp 2457f7f4bccbSMingming Cao J_ASSERT (!(journal->j_flags & JBD2_LOADED)); 2458470decc6SDave Kleikamp 2459470decc6SDave Kleikamp err = load_superblock(journal); 2460470decc6SDave Kleikamp if (err) 2461470decc6SDave Kleikamp return err; 2462470decc6SDave Kleikamp 2463470decc6SDave Kleikamp if (!journal->j_tail) 2464470decc6SDave Kleikamp goto no_recovery; 2465470decc6SDave Kleikamp 2466f2a44523SEryu Guan printk(KERN_WARNING "JBD2: %s recovery information on journal\n", 2467470decc6SDave Kleikamp write ? "Clearing" : "Ignoring"); 2468470decc6SDave Kleikamp 2469f7f4bccbSMingming Cao err = jbd2_journal_skip_recovery(journal); 2470a78bb11dSJan Kara if (write) { 2471a78bb11dSJan Kara /* Lock to make assertions happy... */ 247253cf9784SXiaoguang Wang mutex_lock_io(&journal->j_checkpoint_mutex); 247317f423b5SJan Kara jbd2_mark_journal_empty(journal, REQ_SYNC | REQ_FUA); 2474a78bb11dSJan Kara mutex_unlock(&journal->j_checkpoint_mutex); 2475a78bb11dSJan Kara } 2476470decc6SDave Kleikamp 2477470decc6SDave Kleikamp no_recovery: 2478470decc6SDave Kleikamp return err; 2479470decc6SDave Kleikamp } 2480470decc6SDave Kleikamp 2481470decc6SDave Kleikamp /** 24822bf31d94SMauro Carvalho Chehab * jbd2_journal_abort () - Shutdown the journal immediately. 2483470decc6SDave Kleikamp * @journal: the journal to shutdown. 2484470decc6SDave Kleikamp * @errno: an error number to record in the journal indicating 2485470decc6SDave Kleikamp * the reason for the shutdown. 2486470decc6SDave Kleikamp * 2487470decc6SDave Kleikamp * Perform a complete, immediate shutdown of the ENTIRE 2488470decc6SDave Kleikamp * journal (not of a single transaction). This operation cannot be 2489470decc6SDave Kleikamp * undone without closing and reopening the journal. 2490470decc6SDave Kleikamp * 2491f7f4bccbSMingming Cao * The jbd2_journal_abort function is intended to support higher level error 2492470decc6SDave Kleikamp * recovery mechanisms such as the ext2/ext3 remount-readonly error 2493470decc6SDave Kleikamp * mode. 2494470decc6SDave Kleikamp * 2495470decc6SDave Kleikamp * Journal abort has very specific semantics. Any existing dirty, 2496470decc6SDave Kleikamp * unjournaled buffers in the main filesystem will still be written to 2497470decc6SDave Kleikamp * disk by bdflush, but the journaling mechanism will be suspended 2498470decc6SDave Kleikamp * immediately and no further transaction commits will be honoured. 2499470decc6SDave Kleikamp * 2500470decc6SDave Kleikamp * Any dirty, journaled buffers will be written back to disk without 2501470decc6SDave Kleikamp * hitting the journal. Atomicity cannot be guaranteed on an aborted 2502470decc6SDave Kleikamp * filesystem, but we _do_ attempt to leave as much data as possible 2503470decc6SDave Kleikamp * behind for fsck to use for cleanup. 2504470decc6SDave Kleikamp * 2505470decc6SDave Kleikamp * Any attempt to get a new transaction handle on a journal which is in 2506470decc6SDave Kleikamp * ABORT state will just result in an -EROFS error return. A 2507f7f4bccbSMingming Cao * jbd2_journal_stop on an existing handle will return -EIO if we have 2508470decc6SDave Kleikamp * entered abort state during the update. 2509470decc6SDave Kleikamp * 2510470decc6SDave Kleikamp * Recursive transactions are not disturbed by journal abort until the 2511f7f4bccbSMingming Cao * final jbd2_journal_stop, which will receive the -EIO error. 2512470decc6SDave Kleikamp * 2513f7f4bccbSMingming Cao * Finally, the jbd2_journal_abort call allows the caller to supply an errno 2514470decc6SDave Kleikamp * which will be recorded (if possible) in the journal superblock. This 2515470decc6SDave Kleikamp * allows a client to record failure conditions in the middle of a 2516470decc6SDave Kleikamp * transaction without having to complete the transaction to record the 2517470decc6SDave Kleikamp * failure to disk. ext3_error, for example, now uses this 2518470decc6SDave Kleikamp * functionality. 2519470decc6SDave Kleikamp * 2520470decc6SDave Kleikamp */ 2521470decc6SDave Kleikamp 2522f7f4bccbSMingming Cao void jbd2_journal_abort(journal_t *journal, int errno) 2523470decc6SDave Kleikamp { 25247f6225e4Szhangyi (F) transaction_t *transaction; 25257f6225e4Szhangyi (F) 25267f6225e4Szhangyi (F) /* 25277b97d868Szhangyi (F) * Lock the aborting procedure until everything is done, this avoid 25287b97d868Szhangyi (F) * races between filesystem's error handling flow (e.g. ext4_abort()), 25297b97d868Szhangyi (F) * ensure panic after the error info is written into journal's 25307b97d868Szhangyi (F) * superblock. 25317b97d868Szhangyi (F) */ 25327b97d868Szhangyi (F) mutex_lock(&journal->j_abort_mutex); 25337b97d868Szhangyi (F) /* 25347f6225e4Szhangyi (F) * ESHUTDOWN always takes precedence because a file system check 25357f6225e4Szhangyi (F) * caused by any other journal abort error is not required after 25367f6225e4Szhangyi (F) * a shutdown triggered. 25377f6225e4Szhangyi (F) */ 25387f6225e4Szhangyi (F) write_lock(&journal->j_state_lock); 25397f6225e4Szhangyi (F) if (journal->j_flags & JBD2_ABORT) { 25407f6225e4Szhangyi (F) int old_errno = journal->j_errno; 25417f6225e4Szhangyi (F) 25427f6225e4Szhangyi (F) write_unlock(&journal->j_state_lock); 25437f6225e4Szhangyi (F) if (old_errno != -ESHUTDOWN && errno == -ESHUTDOWN) { 25447f6225e4Szhangyi (F) journal->j_errno = errno; 25457f6225e4Szhangyi (F) jbd2_journal_update_sb_errno(journal); 25467f6225e4Szhangyi (F) } 25477b97d868Szhangyi (F) mutex_unlock(&journal->j_abort_mutex); 25487f6225e4Szhangyi (F) return; 25497f6225e4Szhangyi (F) } 25507f6225e4Szhangyi (F) 25517f6225e4Szhangyi (F) /* 25527f6225e4Szhangyi (F) * Mark the abort as occurred and start current running transaction 25537f6225e4Szhangyi (F) * to release all journaled buffer. 25547f6225e4Szhangyi (F) */ 25557f6225e4Szhangyi (F) pr_err("Aborting journal on device %s.\n", journal->j_devname); 25567f6225e4Szhangyi (F) 25577f6225e4Szhangyi (F) journal->j_flags |= JBD2_ABORT; 25587f6225e4Szhangyi (F) journal->j_errno = errno; 25597f6225e4Szhangyi (F) transaction = journal->j_running_transaction; 25607f6225e4Szhangyi (F) if (transaction) 25617f6225e4Szhangyi (F) __jbd2_log_start_commit(journal, transaction->t_tid); 25627f6225e4Szhangyi (F) write_unlock(&journal->j_state_lock); 25637f6225e4Szhangyi (F) 25647f6225e4Szhangyi (F) /* 25657f6225e4Szhangyi (F) * Record errno to the journal super block, so that fsck and jbd2 25667f6225e4Szhangyi (F) * layer could realise that a filesystem check is needed. 25677f6225e4Szhangyi (F) */ 25687f6225e4Szhangyi (F) jbd2_journal_update_sb_errno(journal); 25697b97d868Szhangyi (F) mutex_unlock(&journal->j_abort_mutex); 2570470decc6SDave Kleikamp } 2571470decc6SDave Kleikamp 2572470decc6SDave Kleikamp /** 25732bf31d94SMauro Carvalho Chehab * jbd2_journal_errno() - returns the journal's error state. 2574470decc6SDave Kleikamp * @journal: journal to examine. 2575470decc6SDave Kleikamp * 2576bfcd3555SAlberto Bertogli * This is the errno number set with jbd2_journal_abort(), the last 2577470decc6SDave Kleikamp * time the journal was mounted - if the journal was stopped 2578470decc6SDave Kleikamp * without calling abort this will be 0. 2579470decc6SDave Kleikamp * 2580470decc6SDave Kleikamp * If the journal has been aborted on this mount time -EROFS will 2581470decc6SDave Kleikamp * be returned. 2582470decc6SDave Kleikamp */ 2583f7f4bccbSMingming Cao int jbd2_journal_errno(journal_t *journal) 2584470decc6SDave Kleikamp { 2585470decc6SDave Kleikamp int err; 2586470decc6SDave Kleikamp 2587a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 2588f7f4bccbSMingming Cao if (journal->j_flags & JBD2_ABORT) 2589470decc6SDave Kleikamp err = -EROFS; 2590470decc6SDave Kleikamp else 2591470decc6SDave Kleikamp err = journal->j_errno; 2592a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 2593470decc6SDave Kleikamp return err; 2594470decc6SDave Kleikamp } 2595470decc6SDave Kleikamp 2596470decc6SDave Kleikamp /** 25972bf31d94SMauro Carvalho Chehab * jbd2_journal_clear_err() - clears the journal's error state 2598470decc6SDave Kleikamp * @journal: journal to act on. 2599470decc6SDave Kleikamp * 2600bfcd3555SAlberto Bertogli * An error must be cleared or acked to take a FS out of readonly 2601470decc6SDave Kleikamp * mode. 2602470decc6SDave Kleikamp */ 2603f7f4bccbSMingming Cao int jbd2_journal_clear_err(journal_t *journal) 2604470decc6SDave Kleikamp { 2605470decc6SDave Kleikamp int err = 0; 2606470decc6SDave Kleikamp 2607a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 2608f7f4bccbSMingming Cao if (journal->j_flags & JBD2_ABORT) 2609470decc6SDave Kleikamp err = -EROFS; 2610470decc6SDave Kleikamp else 2611470decc6SDave Kleikamp journal->j_errno = 0; 2612a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 2613470decc6SDave Kleikamp return err; 2614470decc6SDave Kleikamp } 2615470decc6SDave Kleikamp 2616470decc6SDave Kleikamp /** 26172bf31d94SMauro Carvalho Chehab * jbd2_journal_ack_err() - Ack journal err. 2618470decc6SDave Kleikamp * @journal: journal to act on. 2619470decc6SDave Kleikamp * 2620bfcd3555SAlberto Bertogli * An error must be cleared or acked to take a FS out of readonly 2621470decc6SDave Kleikamp * mode. 2622470decc6SDave Kleikamp */ 2623f7f4bccbSMingming Cao void jbd2_journal_ack_err(journal_t *journal) 2624470decc6SDave Kleikamp { 2625a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 2626470decc6SDave Kleikamp if (journal->j_errno) 2627f7f4bccbSMingming Cao journal->j_flags |= JBD2_ACK_ERR; 2628a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 2629470decc6SDave Kleikamp } 2630470decc6SDave Kleikamp 2631f7f4bccbSMingming Cao int jbd2_journal_blocks_per_page(struct inode *inode) 2632470decc6SDave Kleikamp { 263309cbfeafSKirill A. Shutemov return 1 << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits); 2634470decc6SDave Kleikamp } 2635470decc6SDave Kleikamp 2636470decc6SDave Kleikamp /* 2637b517bea1SZach Brown * helper functions to deal with 32 or 64bit block numbers. 2638b517bea1SZach Brown */ 2639b517bea1SZach Brown size_t journal_tag_bytes(journal_t *journal) 2640b517bea1SZach Brown { 2641db9ee220SDarrick J. Wong size_t sz; 2642db9ee220SDarrick J. Wong 264356316a0dSDarrick J. Wong if (jbd2_has_feature_csum3(journal)) 2644db9ee220SDarrick J. Wong return sizeof(journal_block_tag3_t); 2645db9ee220SDarrick J. Wong 2646db9ee220SDarrick J. Wong sz = sizeof(journal_block_tag_t); 2647c3900875SDarrick J. Wong 264856316a0dSDarrick J. Wong if (jbd2_has_feature_csum2(journal)) 2649db9ee220SDarrick J. Wong sz += sizeof(__u16); 2650c3900875SDarrick J. Wong 265156316a0dSDarrick J. Wong if (jbd2_has_feature_64bit(journal)) 2652db9ee220SDarrick J. Wong return sz; 2653b517bea1SZach Brown else 2654db9ee220SDarrick J. Wong return sz - sizeof(__u32); 2655b517bea1SZach Brown } 2656b517bea1SZach Brown 2657b517bea1SZach Brown /* 2658d2eecb03STheodore Ts'o * JBD memory management 2659d2eecb03STheodore Ts'o * 2660d2eecb03STheodore Ts'o * These functions are used to allocate block-sized chunks of memory 2661d2eecb03STheodore Ts'o * used for making copies of buffer_head data. Very often it will be 2662d2eecb03STheodore Ts'o * page-sized chunks of data, but sometimes it will be in 2663d2eecb03STheodore Ts'o * sub-page-size chunks. (For example, 16k pages on Power systems 2664d2eecb03STheodore Ts'o * with a 4k block file system.) For blocks smaller than a page, we 2665d2eecb03STheodore Ts'o * use a SLAB allocator. There are slab caches for each block size, 2666d2eecb03STheodore Ts'o * which are allocated at mount time, if necessary, and we only free 2667d2eecb03STheodore Ts'o * (all of) the slab caches when/if the jbd2 module is unloaded. For 2668d2eecb03STheodore Ts'o * this reason we don't need to a mutex to protect access to 2669d2eecb03STheodore Ts'o * jbd2_slab[] allocating or releasing memory; only in 2670d2eecb03STheodore Ts'o * jbd2_journal_create_slab(). 2671d2eecb03STheodore Ts'o */ 2672d2eecb03STheodore Ts'o #define JBD2_MAX_SLABS 8 2673d2eecb03STheodore Ts'o static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS]; 2674d2eecb03STheodore Ts'o 2675d2eecb03STheodore Ts'o static const char *jbd2_slab_names[JBD2_MAX_SLABS] = { 2676d2eecb03STheodore Ts'o "jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k", 2677d2eecb03STheodore Ts'o "jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k" 2678d2eecb03STheodore Ts'o }; 2679d2eecb03STheodore Ts'o 2680d2eecb03STheodore Ts'o 2681d2eecb03STheodore Ts'o static void jbd2_journal_destroy_slabs(void) 2682d2eecb03STheodore Ts'o { 2683d2eecb03STheodore Ts'o int i; 2684d2eecb03STheodore Ts'o 2685d2eecb03STheodore Ts'o for (i = 0; i < JBD2_MAX_SLABS; i++) { 2686d2eecb03STheodore Ts'o kmem_cache_destroy(jbd2_slab[i]); 2687d2eecb03STheodore Ts'o jbd2_slab[i] = NULL; 2688d2eecb03STheodore Ts'o } 2689d2eecb03STheodore Ts'o } 2690d2eecb03STheodore Ts'o 2691d2eecb03STheodore Ts'o static int jbd2_journal_create_slab(size_t size) 2692d2eecb03STheodore Ts'o { 269351dfacdeSThomas Gleixner static DEFINE_MUTEX(jbd2_slab_create_mutex); 2694d2eecb03STheodore Ts'o int i = order_base_2(size) - 10; 2695d2eecb03STheodore Ts'o size_t slab_size; 2696d2eecb03STheodore Ts'o 2697d2eecb03STheodore Ts'o if (size == PAGE_SIZE) 2698d2eecb03STheodore Ts'o return 0; 2699d2eecb03STheodore Ts'o 2700d2eecb03STheodore Ts'o if (i >= JBD2_MAX_SLABS) 2701d2eecb03STheodore Ts'o return -EINVAL; 2702d2eecb03STheodore Ts'o 2703d2eecb03STheodore Ts'o if (unlikely(i < 0)) 2704d2eecb03STheodore Ts'o i = 0; 270551dfacdeSThomas Gleixner mutex_lock(&jbd2_slab_create_mutex); 2706d2eecb03STheodore Ts'o if (jbd2_slab[i]) { 270751dfacdeSThomas Gleixner mutex_unlock(&jbd2_slab_create_mutex); 2708d2eecb03STheodore Ts'o return 0; /* Already created */ 2709d2eecb03STheodore Ts'o } 2710d2eecb03STheodore Ts'o 2711d2eecb03STheodore Ts'o slab_size = 1 << (i+10); 2712d2eecb03STheodore Ts'o jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size, 2713d2eecb03STheodore Ts'o slab_size, 0, NULL); 271451dfacdeSThomas Gleixner mutex_unlock(&jbd2_slab_create_mutex); 2715d2eecb03STheodore Ts'o if (!jbd2_slab[i]) { 2716d2eecb03STheodore Ts'o printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n"); 2717d2eecb03STheodore Ts'o return -ENOMEM; 2718d2eecb03STheodore Ts'o } 2719d2eecb03STheodore Ts'o return 0; 2720d2eecb03STheodore Ts'o } 2721d2eecb03STheodore Ts'o 2722d2eecb03STheodore Ts'o static struct kmem_cache *get_slab(size_t size) 2723d2eecb03STheodore Ts'o { 2724d2eecb03STheodore Ts'o int i = order_base_2(size) - 10; 2725d2eecb03STheodore Ts'o 2726d2eecb03STheodore Ts'o BUG_ON(i >= JBD2_MAX_SLABS); 2727d2eecb03STheodore Ts'o if (unlikely(i < 0)) 2728d2eecb03STheodore Ts'o i = 0; 27298ac97b74SBill Pemberton BUG_ON(jbd2_slab[i] == NULL); 2730d2eecb03STheodore Ts'o return jbd2_slab[i]; 2731d2eecb03STheodore Ts'o } 2732d2eecb03STheodore Ts'o 2733d2eecb03STheodore Ts'o void *jbd2_alloc(size_t size, gfp_t flags) 2734d2eecb03STheodore Ts'o { 2735d2eecb03STheodore Ts'o void *ptr; 2736d2eecb03STheodore Ts'o 2737d2eecb03STheodore Ts'o BUG_ON(size & (size-1)); /* Must be a power of 2 */ 2738d2eecb03STheodore Ts'o 2739f2db1971SMichal Hocko if (size < PAGE_SIZE) 2740d2eecb03STheodore Ts'o ptr = kmem_cache_alloc(get_slab(size), flags); 2741f2db1971SMichal Hocko else 2742f2db1971SMichal Hocko ptr = (void *)__get_free_pages(flags, get_order(size)); 2743d2eecb03STheodore Ts'o 2744d2eecb03STheodore Ts'o /* Check alignment; SLUB has gotten this wrong in the past, 2745d2eecb03STheodore Ts'o * and this can lead to user data corruption! */ 2746d2eecb03STheodore Ts'o BUG_ON(((unsigned long) ptr) & (size-1)); 2747d2eecb03STheodore Ts'o 2748d2eecb03STheodore Ts'o return ptr; 2749d2eecb03STheodore Ts'o } 2750d2eecb03STheodore Ts'o 2751d2eecb03STheodore Ts'o void jbd2_free(void *ptr, size_t size) 2752d2eecb03STheodore Ts'o { 2753f2db1971SMichal Hocko if (size < PAGE_SIZE) 2754d2eecb03STheodore Ts'o kmem_cache_free(get_slab(size), ptr); 2755f2db1971SMichal Hocko else 2756f2db1971SMichal Hocko free_pages((unsigned long)ptr, get_order(size)); 2757d2eecb03STheodore Ts'o }; 2758d2eecb03STheodore Ts'o 2759d2eecb03STheodore Ts'o /* 2760470decc6SDave Kleikamp * Journal_head storage management 2761470decc6SDave Kleikamp */ 2762e18b890bSChristoph Lameter static struct kmem_cache *jbd2_journal_head_cache; 2763e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 2764470decc6SDave Kleikamp static atomic_t nr_journal_heads = ATOMIC_INIT(0); 2765470decc6SDave Kleikamp #endif 2766470decc6SDave Kleikamp 27670d52154bSChengguang Xu static int __init jbd2_journal_init_journal_head_cache(void) 2768470decc6SDave Kleikamp { 27690d52154bSChengguang Xu J_ASSERT(!jbd2_journal_head_cache); 2770a920e941SJohann Lombardi jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head", 2771470decc6SDave Kleikamp sizeof(struct journal_head), 2772470decc6SDave Kleikamp 0, /* offset */ 27735f0d5a3aSPaul E. McKenney SLAB_TEMPORARY | SLAB_TYPESAFE_BY_RCU, 277420c2df83SPaul Mundt NULL); /* ctor */ 27751076d17aSAl Viro if (!jbd2_journal_head_cache) { 2776f2a44523SEryu Guan printk(KERN_EMERG "JBD2: no memory for journal_head cache\n"); 27770d52154bSChengguang Xu return -ENOMEM; 2778470decc6SDave Kleikamp } 27790d52154bSChengguang Xu return 0; 2780470decc6SDave Kleikamp } 2781470decc6SDave Kleikamp 27824185a2acSYongqiang Yang static void jbd2_journal_destroy_journal_head_cache(void) 2783470decc6SDave Kleikamp { 2784f7f4bccbSMingming Cao kmem_cache_destroy(jbd2_journal_head_cache); 2785f7f4bccbSMingming Cao jbd2_journal_head_cache = NULL; 2786470decc6SDave Kleikamp } 2787470decc6SDave Kleikamp 2788470decc6SDave Kleikamp /* 2789470decc6SDave Kleikamp * journal_head splicing and dicing 2790470decc6SDave Kleikamp */ 2791470decc6SDave Kleikamp static struct journal_head *journal_alloc_journal_head(void) 2792470decc6SDave Kleikamp { 2793470decc6SDave Kleikamp struct journal_head *ret; 2794470decc6SDave Kleikamp 2795e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 2796470decc6SDave Kleikamp atomic_inc(&nr_journal_heads); 2797470decc6SDave Kleikamp #endif 27985d9cf9c6SZheng Liu ret = kmem_cache_zalloc(jbd2_journal_head_cache, GFP_NOFS); 27991076d17aSAl Viro if (!ret) { 2800470decc6SDave Kleikamp jbd_debug(1, "out of memory for journal_head\n"); 2801670be5a7STheodore Ts'o pr_notice_ratelimited("ENOMEM in %s, retrying.\n", __func__); 28027b506b10SMichal Hocko ret = kmem_cache_zalloc(jbd2_journal_head_cache, 28037b506b10SMichal Hocko GFP_NOFS | __GFP_NOFAIL); 2804470decc6SDave Kleikamp } 280546417064SThomas Gleixner if (ret) 280646417064SThomas Gleixner spin_lock_init(&ret->b_state_lock); 2807470decc6SDave Kleikamp return ret; 2808470decc6SDave Kleikamp } 2809470decc6SDave Kleikamp 2810470decc6SDave Kleikamp static void journal_free_journal_head(struct journal_head *jh) 2811470decc6SDave Kleikamp { 2812e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 2813470decc6SDave Kleikamp atomic_dec(&nr_journal_heads); 2814cd02ff0bSMingming Cao memset(jh, JBD2_POISON_FREE, sizeof(*jh)); 2815470decc6SDave Kleikamp #endif 2816f7f4bccbSMingming Cao kmem_cache_free(jbd2_journal_head_cache, jh); 2817470decc6SDave Kleikamp } 2818470decc6SDave Kleikamp 2819470decc6SDave Kleikamp /* 2820470decc6SDave Kleikamp * A journal_head is attached to a buffer_head whenever JBD has an 2821470decc6SDave Kleikamp * interest in the buffer. 2822470decc6SDave Kleikamp * 2823470decc6SDave Kleikamp * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit 2824470decc6SDave Kleikamp * is set. This bit is tested in core kernel code where we need to take 2825470decc6SDave Kleikamp * JBD-specific actions. Testing the zeroness of ->b_private is not reliable 2826470decc6SDave Kleikamp * there. 2827470decc6SDave Kleikamp * 2828470decc6SDave Kleikamp * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one. 2829470decc6SDave Kleikamp * 2830470decc6SDave Kleikamp * When a buffer has its BH_JBD bit set it is immune from being released by 2831470decc6SDave Kleikamp * core kernel code, mainly via ->b_count. 2832470decc6SDave Kleikamp * 2833de1b7941SJan Kara * A journal_head is detached from its buffer_head when the journal_head's 2834de1b7941SJan Kara * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint 2835de1b7941SJan Kara * transaction (b_cp_transaction) hold their references to b_jcount. 2836470decc6SDave Kleikamp * 2837470decc6SDave Kleikamp * Various places in the kernel want to attach a journal_head to a buffer_head 2838470decc6SDave Kleikamp * _before_ attaching the journal_head to a transaction. To protect the 2839f7f4bccbSMingming Cao * journal_head in this situation, jbd2_journal_add_journal_head elevates the 2840470decc6SDave Kleikamp * journal_head's b_jcount refcount by one. The caller must call 2841f7f4bccbSMingming Cao * jbd2_journal_put_journal_head() to undo this. 2842470decc6SDave Kleikamp * 2843470decc6SDave Kleikamp * So the typical usage would be: 2844470decc6SDave Kleikamp * 2845470decc6SDave Kleikamp * (Attach a journal_head if needed. Increments b_jcount) 2846f7f4bccbSMingming Cao * struct journal_head *jh = jbd2_journal_add_journal_head(bh); 2847470decc6SDave Kleikamp * ... 2848de1b7941SJan Kara * (Get another reference for transaction) 2849de1b7941SJan Kara * jbd2_journal_grab_journal_head(bh); 2850470decc6SDave Kleikamp * jh->b_transaction = xxx; 2851de1b7941SJan Kara * (Put original reference) 2852f7f4bccbSMingming Cao * jbd2_journal_put_journal_head(jh); 2853470decc6SDave Kleikamp */ 2854470decc6SDave Kleikamp 2855470decc6SDave Kleikamp /* 2856470decc6SDave Kleikamp * Give a buffer_head a journal_head. 2857470decc6SDave Kleikamp * 2858470decc6SDave Kleikamp * May sleep. 2859470decc6SDave Kleikamp */ 2860f7f4bccbSMingming Cao struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh) 2861470decc6SDave Kleikamp { 2862470decc6SDave Kleikamp struct journal_head *jh; 2863470decc6SDave Kleikamp struct journal_head *new_jh = NULL; 2864470decc6SDave Kleikamp 2865470decc6SDave Kleikamp repeat: 28665d9cf9c6SZheng Liu if (!buffer_jbd(bh)) 2867470decc6SDave Kleikamp new_jh = journal_alloc_journal_head(); 2868470decc6SDave Kleikamp 2869470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 2870470decc6SDave Kleikamp if (buffer_jbd(bh)) { 2871470decc6SDave Kleikamp jh = bh2jh(bh); 2872470decc6SDave Kleikamp } else { 2873470decc6SDave Kleikamp J_ASSERT_BH(bh, 2874470decc6SDave Kleikamp (atomic_read(&bh->b_count) > 0) || 2875470decc6SDave Kleikamp (bh->b_page && bh->b_page->mapping)); 2876470decc6SDave Kleikamp 2877470decc6SDave Kleikamp if (!new_jh) { 2878470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 2879470decc6SDave Kleikamp goto repeat; 2880470decc6SDave Kleikamp } 2881470decc6SDave Kleikamp 2882470decc6SDave Kleikamp jh = new_jh; 2883470decc6SDave Kleikamp new_jh = NULL; /* We consumed it */ 2884470decc6SDave Kleikamp set_buffer_jbd(bh); 2885470decc6SDave Kleikamp bh->b_private = jh; 2886470decc6SDave Kleikamp jh->b_bh = bh; 2887470decc6SDave Kleikamp get_bh(bh); 2888470decc6SDave Kleikamp BUFFER_TRACE(bh, "added journal_head"); 2889470decc6SDave Kleikamp } 2890470decc6SDave Kleikamp jh->b_jcount++; 2891470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 2892470decc6SDave Kleikamp if (new_jh) 2893470decc6SDave Kleikamp journal_free_journal_head(new_jh); 2894470decc6SDave Kleikamp return bh->b_private; 2895470decc6SDave Kleikamp } 2896470decc6SDave Kleikamp 2897470decc6SDave Kleikamp /* 2898470decc6SDave Kleikamp * Grab a ref against this buffer_head's journal_head. If it ended up not 2899470decc6SDave Kleikamp * having a journal_head, return NULL 2900470decc6SDave Kleikamp */ 2901f7f4bccbSMingming Cao struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh) 2902470decc6SDave Kleikamp { 2903470decc6SDave Kleikamp struct journal_head *jh = NULL; 2904470decc6SDave Kleikamp 2905470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 2906470decc6SDave Kleikamp if (buffer_jbd(bh)) { 2907470decc6SDave Kleikamp jh = bh2jh(bh); 2908470decc6SDave Kleikamp jh->b_jcount++; 2909470decc6SDave Kleikamp } 2910470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 2911470decc6SDave Kleikamp return jh; 2912470decc6SDave Kleikamp } 2913470decc6SDave Kleikamp 2914470decc6SDave Kleikamp static void __journal_remove_journal_head(struct buffer_head *bh) 2915470decc6SDave Kleikamp { 2916470decc6SDave Kleikamp struct journal_head *jh = bh2jh(bh); 2917470decc6SDave Kleikamp 2918de1b7941SJan Kara J_ASSERT_JH(jh, jh->b_transaction == NULL); 2919de1b7941SJan Kara J_ASSERT_JH(jh, jh->b_next_transaction == NULL); 2920de1b7941SJan Kara J_ASSERT_JH(jh, jh->b_cp_transaction == NULL); 2921470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_jlist == BJ_None); 2922470decc6SDave Kleikamp J_ASSERT_BH(bh, buffer_jbd(bh)); 2923470decc6SDave Kleikamp J_ASSERT_BH(bh, jh2bh(jh) == bh); 2924470decc6SDave Kleikamp BUFFER_TRACE(bh, "remove journal_head"); 29257855a57dSThomas Gleixner 29267855a57dSThomas Gleixner /* Unlink before dropping the lock */ 2927470decc6SDave Kleikamp bh->b_private = NULL; 2928470decc6SDave Kleikamp jh->b_bh = NULL; /* debug, really */ 2929470decc6SDave Kleikamp clear_buffer_jbd(bh); 29307855a57dSThomas Gleixner } 29317855a57dSThomas Gleixner 29327855a57dSThomas Gleixner static void journal_release_journal_head(struct journal_head *jh, size_t b_size) 29337855a57dSThomas Gleixner { 29347855a57dSThomas Gleixner if (jh->b_frozen_data) { 29357855a57dSThomas Gleixner printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__); 29367855a57dSThomas Gleixner jbd2_free(jh->b_frozen_data, b_size); 29377855a57dSThomas Gleixner } 29387855a57dSThomas Gleixner if (jh->b_committed_data) { 29397855a57dSThomas Gleixner printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__); 29407855a57dSThomas Gleixner jbd2_free(jh->b_committed_data, b_size); 29417855a57dSThomas Gleixner } 2942470decc6SDave Kleikamp journal_free_journal_head(jh); 2943470decc6SDave Kleikamp } 2944470decc6SDave Kleikamp 2945470decc6SDave Kleikamp /* 2946de1b7941SJan Kara * Drop a reference on the passed journal_head. If it fell to zero then 2947470decc6SDave Kleikamp * release the journal_head from the buffer_head. 2948470decc6SDave Kleikamp */ 2949f7f4bccbSMingming Cao void jbd2_journal_put_journal_head(struct journal_head *jh) 2950470decc6SDave Kleikamp { 2951470decc6SDave Kleikamp struct buffer_head *bh = jh2bh(jh); 2952470decc6SDave Kleikamp 2953470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 2954470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_jcount > 0); 2955470decc6SDave Kleikamp --jh->b_jcount; 2956de1b7941SJan Kara if (!jh->b_jcount) { 2957470decc6SDave Kleikamp __journal_remove_journal_head(bh); 2958de1b7941SJan Kara jbd_unlock_bh_journal_head(bh); 29597855a57dSThomas Gleixner journal_release_journal_head(jh, bh->b_size); 2960470decc6SDave Kleikamp __brelse(bh); 29617855a57dSThomas Gleixner } else { 2962470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 2963470decc6SDave Kleikamp } 29647855a57dSThomas Gleixner } 2965470decc6SDave Kleikamp 2966470decc6SDave Kleikamp /* 2967c851ed54SJan Kara * Initialize jbd inode head 2968c851ed54SJan Kara */ 2969c851ed54SJan Kara void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode) 2970c851ed54SJan Kara { 2971c851ed54SJan Kara jinode->i_transaction = NULL; 2972c851ed54SJan Kara jinode->i_next_transaction = NULL; 2973c851ed54SJan Kara jinode->i_vfs_inode = inode; 2974c851ed54SJan Kara jinode->i_flags = 0; 29756ba0e7dcSRoss Zwisler jinode->i_dirty_start = 0; 29766ba0e7dcSRoss Zwisler jinode->i_dirty_end = 0; 2977c851ed54SJan Kara INIT_LIST_HEAD(&jinode->i_list); 2978c851ed54SJan Kara } 2979c851ed54SJan Kara 2980c851ed54SJan Kara /* 2981c851ed54SJan Kara * Function to be called before we start removing inode from memory (i.e., 2982c851ed54SJan Kara * clear_inode() is a fine place to be called from). It removes inode from 2983c851ed54SJan Kara * transaction's lists. 2984c851ed54SJan Kara */ 2985c851ed54SJan Kara void jbd2_journal_release_jbd_inode(journal_t *journal, 2986c851ed54SJan Kara struct jbd2_inode *jinode) 2987c851ed54SJan Kara { 2988c851ed54SJan Kara if (!journal) 2989c851ed54SJan Kara return; 2990c851ed54SJan Kara restart: 2991c851ed54SJan Kara spin_lock(&journal->j_list_lock); 2992c851ed54SJan Kara /* Is commit writing out inode - we have to wait */ 2993cb0d9d47SJan Kara if (jinode->i_flags & JI_COMMIT_RUNNING) { 2994c851ed54SJan Kara wait_queue_head_t *wq; 2995c851ed54SJan Kara DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING); 2996c851ed54SJan Kara wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING); 299721417136SIngo Molnar prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE); 2998c851ed54SJan Kara spin_unlock(&journal->j_list_lock); 2999c851ed54SJan Kara schedule(); 300021417136SIngo Molnar finish_wait(wq, &wait.wq_entry); 3001c851ed54SJan Kara goto restart; 3002c851ed54SJan Kara } 3003c851ed54SJan Kara 3004c851ed54SJan Kara if (jinode->i_transaction) { 3005c851ed54SJan Kara list_del(&jinode->i_list); 3006c851ed54SJan Kara jinode->i_transaction = NULL; 3007c851ed54SJan Kara } 3008c851ed54SJan Kara spin_unlock(&journal->j_list_lock); 3009c851ed54SJan Kara } 3010c851ed54SJan Kara 3011470decc6SDave Kleikamp 30128e85fb3fSJohann Lombardi #ifdef CONFIG_PROC_FS 30138e85fb3fSJohann Lombardi 30148e85fb3fSJohann Lombardi #define JBD2_STATS_PROC_NAME "fs/jbd2" 30158e85fb3fSJohann Lombardi 30168e85fb3fSJohann Lombardi static void __init jbd2_create_jbd_stats_proc_entry(void) 30178e85fb3fSJohann Lombardi { 30188e85fb3fSJohann Lombardi proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL); 30198e85fb3fSJohann Lombardi } 30208e85fb3fSJohann Lombardi 30218e85fb3fSJohann Lombardi static void __exit jbd2_remove_jbd_stats_proc_entry(void) 30228e85fb3fSJohann Lombardi { 30238e85fb3fSJohann Lombardi if (proc_jbd2_stats) 30248e85fb3fSJohann Lombardi remove_proc_entry(JBD2_STATS_PROC_NAME, NULL); 30258e85fb3fSJohann Lombardi } 30268e85fb3fSJohann Lombardi 30278e85fb3fSJohann Lombardi #else 30288e85fb3fSJohann Lombardi 30298e85fb3fSJohann Lombardi #define jbd2_create_jbd_stats_proc_entry() do {} while (0) 30308e85fb3fSJohann Lombardi #define jbd2_remove_jbd_stats_proc_entry() do {} while (0) 30318e85fb3fSJohann Lombardi 30328e85fb3fSJohann Lombardi #endif 30338e85fb3fSJohann Lombardi 30348aefcd55STheodore Ts'o struct kmem_cache *jbd2_handle_cache, *jbd2_inode_cache; 3035470decc6SDave Kleikamp 30360d52154bSChengguang Xu static int __init jbd2_journal_init_inode_cache(void) 3037470decc6SDave Kleikamp { 30380d52154bSChengguang Xu J_ASSERT(!jbd2_inode_cache); 30398aefcd55STheodore Ts'o jbd2_inode_cache = KMEM_CACHE(jbd2_inode, 0); 30400d52154bSChengguang Xu if (!jbd2_inode_cache) { 30410d52154bSChengguang Xu pr_emerg("JBD2: failed to create inode cache\n"); 3042470decc6SDave Kleikamp return -ENOMEM; 3043470decc6SDave Kleikamp } 3044470decc6SDave Kleikamp return 0; 3045470decc6SDave Kleikamp } 3046470decc6SDave Kleikamp 30470d52154bSChengguang Xu static int __init jbd2_journal_init_handle_cache(void) 30480d52154bSChengguang Xu { 30490d52154bSChengguang Xu J_ASSERT(!jbd2_handle_cache); 30500d52154bSChengguang Xu jbd2_handle_cache = KMEM_CACHE(jbd2_journal_handle, SLAB_TEMPORARY); 30510d52154bSChengguang Xu if (!jbd2_handle_cache) { 30520d52154bSChengguang Xu printk(KERN_EMERG "JBD2: failed to create handle cache\n"); 30530d52154bSChengguang Xu return -ENOMEM; 30540d52154bSChengguang Xu } 30550d52154bSChengguang Xu return 0; 30560d52154bSChengguang Xu } 30570d52154bSChengguang Xu 30580d52154bSChengguang Xu static void jbd2_journal_destroy_inode_cache(void) 30590d52154bSChengguang Xu { 30600d52154bSChengguang Xu kmem_cache_destroy(jbd2_inode_cache); 30610d52154bSChengguang Xu jbd2_inode_cache = NULL; 30620d52154bSChengguang Xu } 30630d52154bSChengguang Xu 3064f7f4bccbSMingming Cao static void jbd2_journal_destroy_handle_cache(void) 3065470decc6SDave Kleikamp { 3066f7f4bccbSMingming Cao kmem_cache_destroy(jbd2_handle_cache); 30678bdd5b60SWang Long jbd2_handle_cache = NULL; 3068470decc6SDave Kleikamp } 3069470decc6SDave Kleikamp 3070470decc6SDave Kleikamp /* 3071470decc6SDave Kleikamp * Module startup and shutdown 3072470decc6SDave Kleikamp */ 3073470decc6SDave Kleikamp 3074470decc6SDave Kleikamp static int __init journal_init_caches(void) 3075470decc6SDave Kleikamp { 3076470decc6SDave Kleikamp int ret; 3077470decc6SDave Kleikamp 30780d52154bSChengguang Xu ret = jbd2_journal_init_revoke_record_cache(); 30790d52154bSChengguang Xu if (ret == 0) 30800d52154bSChengguang Xu ret = jbd2_journal_init_revoke_table_cache(); 3081470decc6SDave Kleikamp if (ret == 0) 30824185a2acSYongqiang Yang ret = jbd2_journal_init_journal_head_cache(); 3083470decc6SDave Kleikamp if (ret == 0) 30844185a2acSYongqiang Yang ret = jbd2_journal_init_handle_cache(); 30850c2022ecSYongqiang Yang if (ret == 0) 30860d52154bSChengguang Xu ret = jbd2_journal_init_inode_cache(); 30870d52154bSChengguang Xu if (ret == 0) 30880c2022ecSYongqiang Yang ret = jbd2_journal_init_transaction_cache(); 3089470decc6SDave Kleikamp return ret; 3090470decc6SDave Kleikamp } 3091470decc6SDave Kleikamp 3092f7f4bccbSMingming Cao static void jbd2_journal_destroy_caches(void) 3093470decc6SDave Kleikamp { 30940d52154bSChengguang Xu jbd2_journal_destroy_revoke_record_cache(); 30950d52154bSChengguang Xu jbd2_journal_destroy_revoke_table_cache(); 30964185a2acSYongqiang Yang jbd2_journal_destroy_journal_head_cache(); 3097f7f4bccbSMingming Cao jbd2_journal_destroy_handle_cache(); 30980d52154bSChengguang Xu jbd2_journal_destroy_inode_cache(); 30990c2022ecSYongqiang Yang jbd2_journal_destroy_transaction_cache(); 3100d2eecb03STheodore Ts'o jbd2_journal_destroy_slabs(); 3101470decc6SDave Kleikamp } 3102470decc6SDave Kleikamp 3103470decc6SDave Kleikamp static int __init journal_init(void) 3104470decc6SDave Kleikamp { 3105470decc6SDave Kleikamp int ret; 3106470decc6SDave Kleikamp 3107470decc6SDave Kleikamp BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024); 3108470decc6SDave Kleikamp 3109470decc6SDave Kleikamp ret = journal_init_caches(); 3110620de4e1SDuane Griffin if (ret == 0) { 31118e85fb3fSJohann Lombardi jbd2_create_jbd_stats_proc_entry(); 3112620de4e1SDuane Griffin } else { 3113620de4e1SDuane Griffin jbd2_journal_destroy_caches(); 3114620de4e1SDuane Griffin } 3115470decc6SDave Kleikamp return ret; 3116470decc6SDave Kleikamp } 3117470decc6SDave Kleikamp 3118470decc6SDave Kleikamp static void __exit journal_exit(void) 3119470decc6SDave Kleikamp { 3120e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 3121470decc6SDave Kleikamp int n = atomic_read(&nr_journal_heads); 3122470decc6SDave Kleikamp if (n) 312375685071SJan Kara printk(KERN_ERR "JBD2: leaked %d journal_heads!\n", n); 3124470decc6SDave Kleikamp #endif 31258e85fb3fSJohann Lombardi jbd2_remove_jbd_stats_proc_entry(); 3126f7f4bccbSMingming Cao jbd2_journal_destroy_caches(); 3127470decc6SDave Kleikamp } 3128470decc6SDave Kleikamp 3129470decc6SDave Kleikamp MODULE_LICENSE("GPL"); 3130470decc6SDave Kleikamp module_init(journal_init); 3131470decc6SDave Kleikamp module_exit(journal_exit); 3132470decc6SDave Kleikamp 3133