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); 69470decc6SDave Kleikamp #if 0 70470decc6SDave Kleikamp EXPORT_SYMBOL(journal_sync_buffer); 71470decc6SDave Kleikamp #endif 72f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_flush); 73f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_revoke); 74470decc6SDave Kleikamp 75f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_init_dev); 76f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_init_inode); 77f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_check_used_features); 78f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_check_available_features); 79f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_set_features); 80f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_load); 81f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_destroy); 82f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_abort); 83f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_errno); 84f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_ack_err); 85f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_clear_err); 86f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_log_wait_commit); 873b799d15STheodore Ts'o EXPORT_SYMBOL(jbd2_log_start_commit); 88f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_start_commit); 89f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_force_commit_nested); 90f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_wipe); 91f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_blocks_per_page); 92f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_invalidatepage); 93f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers); 94f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_force_commit); 9541617e1aSJan Kara EXPORT_SYMBOL(jbd2_journal_inode_add_write); 9641617e1aSJan Kara EXPORT_SYMBOL(jbd2_journal_inode_add_wait); 97c851ed54SJan Kara EXPORT_SYMBOL(jbd2_journal_init_jbd_inode); 98c851ed54SJan Kara EXPORT_SYMBOL(jbd2_journal_release_jbd_inode); 99c851ed54SJan Kara EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate); 1008aefcd55STheodore Ts'o EXPORT_SYMBOL(jbd2_inode_cache); 101470decc6SDave Kleikamp 102470decc6SDave Kleikamp static void __journal_abort_soft (journal_t *journal, int errno); 103d2eecb03STheodore Ts'o static int jbd2_journal_create_slab(size_t slab_size); 104470decc6SDave Kleikamp 105169f1a2aSPaul Gortmaker #ifdef CONFIG_JBD2_DEBUG 106169f1a2aSPaul Gortmaker void __jbd2_debug(int level, const char *file, const char *func, 107169f1a2aSPaul Gortmaker unsigned int line, const char *fmt, ...) 108169f1a2aSPaul Gortmaker { 109169f1a2aSPaul Gortmaker struct va_format vaf; 110169f1a2aSPaul Gortmaker va_list args; 111169f1a2aSPaul Gortmaker 112169f1a2aSPaul Gortmaker if (level > jbd2_journal_enable_debug) 113169f1a2aSPaul Gortmaker return; 114169f1a2aSPaul Gortmaker va_start(args, fmt); 115169f1a2aSPaul Gortmaker vaf.fmt = fmt; 116169f1a2aSPaul Gortmaker vaf.va = &args; 117*9196f571SWang Shilong printk(KERN_DEBUG "%s: (%s, %u): %pV", file, func, line, &vaf); 118169f1a2aSPaul Gortmaker va_end(args); 119169f1a2aSPaul Gortmaker } 120169f1a2aSPaul Gortmaker EXPORT_SYMBOL(__jbd2_debug); 121169f1a2aSPaul Gortmaker #endif 122169f1a2aSPaul Gortmaker 12325ed6e8aSDarrick J. Wong /* Checksumming functions */ 1247747e6d0SRashika Kheria static int jbd2_verify_csum_type(journal_t *j, journal_superblock_t *sb) 12525ed6e8aSDarrick J. Wong { 1268595798cSDarrick J. Wong if (!jbd2_journal_has_csum_v2or3_feature(j)) 12725ed6e8aSDarrick J. Wong return 1; 12825ed6e8aSDarrick J. Wong 12925ed6e8aSDarrick J. Wong return sb->s_checksum_type == JBD2_CRC32C_CHKSUM; 13025ed6e8aSDarrick J. Wong } 13125ed6e8aSDarrick J. Wong 13218a6ea1eSDarrick J. Wong static __be32 jbd2_superblock_csum(journal_t *j, journal_superblock_t *sb) 1334fd5ea43SDarrick J. Wong { 13418a6ea1eSDarrick J. Wong __u32 csum; 13518a6ea1eSDarrick J. Wong __be32 old_csum; 1364fd5ea43SDarrick J. Wong 1374fd5ea43SDarrick J. Wong old_csum = sb->s_checksum; 1384fd5ea43SDarrick J. Wong sb->s_checksum = 0; 1394fd5ea43SDarrick J. Wong csum = jbd2_chksum(j, ~0, (char *)sb, sizeof(journal_superblock_t)); 1404fd5ea43SDarrick J. Wong sb->s_checksum = old_csum; 1414fd5ea43SDarrick J. Wong 1424fd5ea43SDarrick J. Wong return cpu_to_be32(csum); 1434fd5ea43SDarrick J. Wong } 1444fd5ea43SDarrick J. Wong 1457747e6d0SRashika Kheria static int jbd2_superblock_csum_verify(journal_t *j, journal_superblock_t *sb) 1464fd5ea43SDarrick J. Wong { 147db9ee220SDarrick J. Wong if (!jbd2_journal_has_csum_v2or3(j)) 1484fd5ea43SDarrick J. Wong return 1; 1494fd5ea43SDarrick J. Wong 1504fd5ea43SDarrick J. Wong return sb->s_checksum == jbd2_superblock_csum(j, sb); 1514fd5ea43SDarrick J. Wong } 1524fd5ea43SDarrick J. Wong 1537747e6d0SRashika Kheria static void jbd2_superblock_csum_set(journal_t *j, journal_superblock_t *sb) 1544fd5ea43SDarrick J. Wong { 155db9ee220SDarrick J. Wong if (!jbd2_journal_has_csum_v2or3(j)) 1564fd5ea43SDarrick J. Wong return; 1574fd5ea43SDarrick J. Wong 1584fd5ea43SDarrick J. Wong sb->s_checksum = jbd2_superblock_csum(j, sb); 1594fd5ea43SDarrick J. Wong } 1604fd5ea43SDarrick J. Wong 161470decc6SDave Kleikamp /* 162470decc6SDave Kleikamp * Helper function used to manage commit timeouts 163470decc6SDave Kleikamp */ 164470decc6SDave Kleikamp 165e3c95788SKees Cook static void commit_timeout(struct timer_list *t) 166470decc6SDave Kleikamp { 167e3c95788SKees Cook journal_t *journal = from_timer(journal, t, j_commit_timer); 168470decc6SDave Kleikamp 169e3c95788SKees Cook wake_up_process(journal->j_task); 170470decc6SDave Kleikamp } 171470decc6SDave Kleikamp 172470decc6SDave Kleikamp /* 173f7f4bccbSMingming Cao * kjournald2: The main thread function used to manage a logging device 174470decc6SDave Kleikamp * journal. 175470decc6SDave Kleikamp * 176470decc6SDave Kleikamp * This kernel thread is responsible for two things: 177470decc6SDave Kleikamp * 178470decc6SDave Kleikamp * 1) COMMIT: Every so often we need to commit the current state of the 179470decc6SDave Kleikamp * filesystem to disk. The journal thread is responsible for writing 180470decc6SDave Kleikamp * all of the metadata buffers to disk. 181470decc6SDave Kleikamp * 182470decc6SDave Kleikamp * 2) CHECKPOINT: We cannot reuse a used section of the log file until all 183470decc6SDave Kleikamp * of the data in that part of the log has been rewritten elsewhere on 184470decc6SDave Kleikamp * the disk. Flushing these old buffers to reclaim space in the log is 185470decc6SDave Kleikamp * known as checkpointing, and this thread is responsible for that job. 186470decc6SDave Kleikamp */ 187470decc6SDave Kleikamp 188f7f4bccbSMingming Cao static int kjournald2(void *arg) 189470decc6SDave Kleikamp { 190470decc6SDave Kleikamp journal_t *journal = arg; 191470decc6SDave Kleikamp transaction_t *transaction; 192470decc6SDave Kleikamp 193470decc6SDave Kleikamp /* 194470decc6SDave Kleikamp * Set up an interval timer which can be used to trigger a commit wakeup 195470decc6SDave Kleikamp * after the commit interval expires 196470decc6SDave Kleikamp */ 197e3c95788SKees Cook timer_setup(&journal->j_commit_timer, commit_timeout, 0); 198470decc6SDave Kleikamp 19935c80422SNigel Cunningham set_freezable(); 20035c80422SNigel Cunningham 201470decc6SDave Kleikamp /* Record that the journal thread is running */ 202470decc6SDave Kleikamp journal->j_task = current; 203470decc6SDave Kleikamp wake_up(&journal->j_wait_done_commit); 204470decc6SDave Kleikamp 205470decc6SDave Kleikamp /* 206eb52da3fSMichal Hocko * Make sure that no allocations from this kernel thread will ever 207eb52da3fSMichal Hocko * recurse to the fs layer because we are responsible for the 208eb52da3fSMichal Hocko * transaction commit and any fs involvement might get stuck waiting for 209eb52da3fSMichal Hocko * the trasn. commit. 210eb52da3fSMichal Hocko */ 211eb52da3fSMichal Hocko memalloc_nofs_save(); 212eb52da3fSMichal Hocko 213eb52da3fSMichal Hocko /* 214470decc6SDave Kleikamp * And now, wait forever for commit wakeup events. 215470decc6SDave Kleikamp */ 216a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 217470decc6SDave Kleikamp 218470decc6SDave Kleikamp loop: 219f7f4bccbSMingming Cao if (journal->j_flags & JBD2_UNMOUNT) 220470decc6SDave Kleikamp goto end_loop; 221470decc6SDave Kleikamp 222470decc6SDave Kleikamp jbd_debug(1, "commit_sequence=%d, commit_request=%d\n", 223470decc6SDave Kleikamp journal->j_commit_sequence, journal->j_commit_request); 224470decc6SDave Kleikamp 225470decc6SDave Kleikamp if (journal->j_commit_sequence != journal->j_commit_request) { 226470decc6SDave Kleikamp jbd_debug(1, "OK, requests differ\n"); 227a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 228470decc6SDave Kleikamp del_timer_sync(&journal->j_commit_timer); 229f7f4bccbSMingming Cao jbd2_journal_commit_transaction(journal); 230a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 231470decc6SDave Kleikamp goto loop; 232470decc6SDave Kleikamp } 233470decc6SDave Kleikamp 234470decc6SDave Kleikamp wake_up(&journal->j_wait_done_commit); 235470decc6SDave Kleikamp if (freezing(current)) { 236470decc6SDave Kleikamp /* 237470decc6SDave Kleikamp * The simpler the better. Flushing journal isn't a 238470decc6SDave Kleikamp * good idea, because that depends on threads that may 239470decc6SDave Kleikamp * be already stopped. 240470decc6SDave Kleikamp */ 241f7f4bccbSMingming Cao jbd_debug(1, "Now suspending kjournald2\n"); 242a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 243a0acae0eSTejun Heo try_to_freeze(); 244a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 245470decc6SDave Kleikamp } else { 246470decc6SDave Kleikamp /* 247470decc6SDave Kleikamp * We assume on resume that commits are already there, 248470decc6SDave Kleikamp * so we don't sleep 249470decc6SDave Kleikamp */ 250470decc6SDave Kleikamp DEFINE_WAIT(wait); 251470decc6SDave Kleikamp int should_sleep = 1; 252470decc6SDave Kleikamp 253470decc6SDave Kleikamp prepare_to_wait(&journal->j_wait_commit, &wait, 254470decc6SDave Kleikamp TASK_INTERRUPTIBLE); 255470decc6SDave Kleikamp if (journal->j_commit_sequence != journal->j_commit_request) 256470decc6SDave Kleikamp should_sleep = 0; 257470decc6SDave Kleikamp transaction = journal->j_running_transaction; 258470decc6SDave Kleikamp if (transaction && time_after_eq(jiffies, 259470decc6SDave Kleikamp transaction->t_expires)) 260470decc6SDave Kleikamp should_sleep = 0; 261f7f4bccbSMingming Cao if (journal->j_flags & JBD2_UNMOUNT) 262470decc6SDave Kleikamp should_sleep = 0; 263470decc6SDave Kleikamp if (should_sleep) { 264a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 265470decc6SDave Kleikamp schedule(); 266a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 267470decc6SDave Kleikamp } 268470decc6SDave Kleikamp finish_wait(&journal->j_wait_commit, &wait); 269470decc6SDave Kleikamp } 270470decc6SDave Kleikamp 271f7f4bccbSMingming Cao jbd_debug(1, "kjournald2 wakes\n"); 272470decc6SDave Kleikamp 273470decc6SDave Kleikamp /* 274470decc6SDave Kleikamp * Were we woken up by a commit wakeup event? 275470decc6SDave Kleikamp */ 276470decc6SDave Kleikamp transaction = journal->j_running_transaction; 277470decc6SDave Kleikamp if (transaction && time_after_eq(jiffies, transaction->t_expires)) { 278470decc6SDave Kleikamp journal->j_commit_request = transaction->t_tid; 279470decc6SDave Kleikamp jbd_debug(1, "woke because of timeout\n"); 280470decc6SDave Kleikamp } 281470decc6SDave Kleikamp goto loop; 282470decc6SDave Kleikamp 283470decc6SDave Kleikamp end_loop: 284470decc6SDave Kleikamp del_timer_sync(&journal->j_commit_timer); 285470decc6SDave Kleikamp journal->j_task = NULL; 286470decc6SDave Kleikamp wake_up(&journal->j_wait_done_commit); 287470decc6SDave Kleikamp jbd_debug(1, "Journal thread exiting.\n"); 288dbfcef6bSSahitya Tummala write_unlock(&journal->j_state_lock); 289470decc6SDave Kleikamp return 0; 290470decc6SDave Kleikamp } 291470decc6SDave Kleikamp 29297f06784SPavel Emelianov static int jbd2_journal_start_thread(journal_t *journal) 293470decc6SDave Kleikamp { 29497f06784SPavel Emelianov struct task_struct *t; 29597f06784SPavel Emelianov 29690576c0bSTheodore Ts'o t = kthread_run(kjournald2, journal, "jbd2/%s", 29790576c0bSTheodore Ts'o journal->j_devname); 29897f06784SPavel Emelianov if (IS_ERR(t)) 29997f06784SPavel Emelianov return PTR_ERR(t); 30097f06784SPavel Emelianov 3011076d17aSAl Viro wait_event(journal->j_wait_done_commit, journal->j_task != NULL); 30297f06784SPavel Emelianov return 0; 303470decc6SDave Kleikamp } 304470decc6SDave Kleikamp 305470decc6SDave Kleikamp static void journal_kill_thread(journal_t *journal) 306470decc6SDave Kleikamp { 307a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 308f7f4bccbSMingming Cao journal->j_flags |= JBD2_UNMOUNT; 309470decc6SDave Kleikamp 310470decc6SDave Kleikamp while (journal->j_task) { 311a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 3123469a32aSTheodore Ts'o wake_up(&journal->j_wait_commit); 3131076d17aSAl Viro wait_event(journal->j_wait_done_commit, journal->j_task == NULL); 314a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 315470decc6SDave Kleikamp } 316a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 317470decc6SDave Kleikamp } 318470decc6SDave Kleikamp 319470decc6SDave Kleikamp /* 320f7f4bccbSMingming Cao * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal. 321470decc6SDave Kleikamp * 322470decc6SDave Kleikamp * Writes a metadata buffer to a given disk block. The actual IO is not 323470decc6SDave Kleikamp * performed but a new buffer_head is constructed which labels the data 324470decc6SDave Kleikamp * to be written with the correct destination disk block. 325470decc6SDave Kleikamp * 326470decc6SDave Kleikamp * Any magic-number escaping which needs to be done will cause a 327470decc6SDave Kleikamp * copy-out here. If the buffer happens to start with the 328f7f4bccbSMingming Cao * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the 329470decc6SDave Kleikamp * magic number is only written to the log for descripter blocks. In 330470decc6SDave Kleikamp * this case, we copy the data and replace the first word with 0, and we 331470decc6SDave Kleikamp * return a result code which indicates that this buffer needs to be 332470decc6SDave Kleikamp * marked as an escaped buffer in the corresponding log descriptor 333470decc6SDave Kleikamp * block. The missing word can then be restored when the block is read 334470decc6SDave Kleikamp * during recovery. 335470decc6SDave Kleikamp * 336470decc6SDave Kleikamp * If the source buffer has already been modified by a new transaction 337470decc6SDave Kleikamp * since we took the last commit snapshot, we use the frozen copy of 338470decc6SDave Kleikamp * that data for IO. If we end up using the existing buffer_head's data 339f5113effSJan Kara * for the write, then we have to make sure nobody modifies it while the 340f5113effSJan Kara * IO is in progress. do_get_write_access() handles this. 341470decc6SDave Kleikamp * 342f5113effSJan Kara * The function returns a pointer to the buffer_head to be used for IO. 343470decc6SDave Kleikamp * 344470decc6SDave Kleikamp * 345470decc6SDave Kleikamp * Return value: 346470decc6SDave Kleikamp * <0: Error 347470decc6SDave Kleikamp * >=0: Finished OK 348470decc6SDave Kleikamp * 349470decc6SDave Kleikamp * On success: 350470decc6SDave Kleikamp * Bit 0 set == escape performed on the data 351470decc6SDave Kleikamp * Bit 1 set == buffer copy-out performed (kfree the data after IO) 352470decc6SDave Kleikamp */ 353470decc6SDave Kleikamp 354f7f4bccbSMingming Cao int jbd2_journal_write_metadata_buffer(transaction_t *transaction, 355470decc6SDave Kleikamp struct journal_head *jh_in, 356f5113effSJan Kara struct buffer_head **bh_out, 357f5113effSJan Kara sector_t blocknr) 358470decc6SDave Kleikamp { 359470decc6SDave Kleikamp int need_copy_out = 0; 360470decc6SDave Kleikamp int done_copy_out = 0; 361470decc6SDave Kleikamp int do_escape = 0; 362470decc6SDave Kleikamp char *mapped_data; 363470decc6SDave Kleikamp struct buffer_head *new_bh; 364470decc6SDave Kleikamp struct page *new_page; 365470decc6SDave Kleikamp unsigned int new_offset; 366470decc6SDave Kleikamp struct buffer_head *bh_in = jh2bh(jh_in); 36796577c43Sdingdinghua journal_t *journal = transaction->t_journal; 368470decc6SDave Kleikamp 369470decc6SDave Kleikamp /* 370470decc6SDave Kleikamp * The buffer really shouldn't be locked: only the current committing 371470decc6SDave Kleikamp * transaction is allowed to write it, so nobody else is allowed 372470decc6SDave Kleikamp * to do any IO. 373470decc6SDave Kleikamp * 374470decc6SDave Kleikamp * akpm: except if we're journalling data, and write() output is 375470decc6SDave Kleikamp * also part of a shared mapping, and another thread has 376470decc6SDave Kleikamp * decided to launch a writepage() against this buffer. 377470decc6SDave Kleikamp */ 378470decc6SDave Kleikamp J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in)); 379470decc6SDave Kleikamp 3806ccaf3e2SMichal Hocko new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL); 38147def826STheodore Ts'o 38296577c43Sdingdinghua /* keep subsequent assertions sane */ 38396577c43Sdingdinghua atomic_set(&new_bh->b_count, 1); 384470decc6SDave Kleikamp 385f5113effSJan Kara jbd_lock_bh_state(bh_in); 386f5113effSJan Kara repeat: 387470decc6SDave Kleikamp /* 388470decc6SDave Kleikamp * If a new transaction has already done a buffer copy-out, then 389470decc6SDave Kleikamp * we use that version of the data for the commit. 390470decc6SDave Kleikamp */ 391470decc6SDave Kleikamp if (jh_in->b_frozen_data) { 392470decc6SDave Kleikamp done_copy_out = 1; 393470decc6SDave Kleikamp new_page = virt_to_page(jh_in->b_frozen_data); 394470decc6SDave Kleikamp new_offset = offset_in_page(jh_in->b_frozen_data); 395470decc6SDave Kleikamp } else { 396470decc6SDave Kleikamp new_page = jh2bh(jh_in)->b_page; 397470decc6SDave Kleikamp new_offset = offset_in_page(jh2bh(jh_in)->b_data); 398470decc6SDave Kleikamp } 399470decc6SDave Kleikamp 400303a8f2aSCong Wang mapped_data = kmap_atomic(new_page); 401470decc6SDave Kleikamp /* 40213ceef09SJan Kara * Fire data frozen trigger if data already wasn't frozen. Do this 40313ceef09SJan Kara * before checking for escaping, as the trigger may modify the magic 40413ceef09SJan Kara * offset. If a copy-out happens afterwards, it will have the correct 40513ceef09SJan Kara * data in the buffer. 406e06c8227SJoel Becker */ 40713ceef09SJan Kara if (!done_copy_out) 40813ceef09SJan Kara jbd2_buffer_frozen_trigger(jh_in, mapped_data + new_offset, 40913ceef09SJan Kara jh_in->b_triggers); 410e06c8227SJoel Becker 411e06c8227SJoel Becker /* 412470decc6SDave Kleikamp * Check for escaping 413470decc6SDave Kleikamp */ 414470decc6SDave Kleikamp if (*((__be32 *)(mapped_data + new_offset)) == 415f7f4bccbSMingming Cao cpu_to_be32(JBD2_MAGIC_NUMBER)) { 416470decc6SDave Kleikamp need_copy_out = 1; 417470decc6SDave Kleikamp do_escape = 1; 418470decc6SDave Kleikamp } 419303a8f2aSCong Wang kunmap_atomic(mapped_data); 420470decc6SDave Kleikamp 421470decc6SDave Kleikamp /* 422470decc6SDave Kleikamp * Do we need to do a data copy? 423470decc6SDave Kleikamp */ 424470decc6SDave Kleikamp if (need_copy_out && !done_copy_out) { 425470decc6SDave Kleikamp char *tmp; 426470decc6SDave Kleikamp 427470decc6SDave Kleikamp jbd_unlock_bh_state(bh_in); 428af1e76d6SMingming Cao tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS); 429e6ec116bSTheodore Ts'o if (!tmp) { 430f5113effSJan Kara brelse(new_bh); 431e6ec116bSTheodore Ts'o return -ENOMEM; 432e6ec116bSTheodore Ts'o } 433470decc6SDave Kleikamp jbd_lock_bh_state(bh_in); 434470decc6SDave Kleikamp if (jh_in->b_frozen_data) { 435af1e76d6SMingming Cao jbd2_free(tmp, bh_in->b_size); 436470decc6SDave Kleikamp goto repeat; 437470decc6SDave Kleikamp } 438470decc6SDave Kleikamp 439470decc6SDave Kleikamp jh_in->b_frozen_data = tmp; 440303a8f2aSCong Wang mapped_data = kmap_atomic(new_page); 441f5113effSJan Kara memcpy(tmp, mapped_data + new_offset, bh_in->b_size); 442303a8f2aSCong Wang kunmap_atomic(mapped_data); 443470decc6SDave Kleikamp 444470decc6SDave Kleikamp new_page = virt_to_page(tmp); 445470decc6SDave Kleikamp new_offset = offset_in_page(tmp); 446470decc6SDave Kleikamp done_copy_out = 1; 447e06c8227SJoel Becker 448e06c8227SJoel Becker /* 449e06c8227SJoel Becker * This isn't strictly necessary, as we're using frozen 450e06c8227SJoel Becker * data for the escaping, but it keeps consistency with 451e06c8227SJoel Becker * b_frozen_data usage. 452e06c8227SJoel Becker */ 453e06c8227SJoel Becker jh_in->b_frozen_triggers = jh_in->b_triggers; 454470decc6SDave Kleikamp } 455470decc6SDave Kleikamp 456470decc6SDave Kleikamp /* 457470decc6SDave Kleikamp * Did we need to do an escaping? Now we've done all the 458470decc6SDave Kleikamp * copying, we can finally do so. 459470decc6SDave Kleikamp */ 460470decc6SDave Kleikamp if (do_escape) { 461303a8f2aSCong Wang mapped_data = kmap_atomic(new_page); 462470decc6SDave Kleikamp *((unsigned int *)(mapped_data + new_offset)) = 0; 463303a8f2aSCong Wang kunmap_atomic(mapped_data); 464470decc6SDave Kleikamp } 465470decc6SDave Kleikamp 466470decc6SDave Kleikamp set_bh_page(new_bh, new_page, new_offset); 467f5113effSJan Kara new_bh->b_size = bh_in->b_size; 468f5113effSJan Kara new_bh->b_bdev = journal->j_dev; 469470decc6SDave Kleikamp new_bh->b_blocknr = blocknr; 470b34090e5SJan Kara new_bh->b_private = bh_in; 471470decc6SDave Kleikamp set_buffer_mapped(new_bh); 472470decc6SDave Kleikamp set_buffer_dirty(new_bh); 473470decc6SDave Kleikamp 474f5113effSJan Kara *bh_out = new_bh; 475470decc6SDave Kleikamp 476470decc6SDave Kleikamp /* 477470decc6SDave Kleikamp * The to-be-written buffer needs to get moved to the io queue, 478470decc6SDave Kleikamp * and the original buffer whose contents we are shadowing or 479470decc6SDave Kleikamp * copying is moved to the transaction's shadow queue. 480470decc6SDave Kleikamp */ 481470decc6SDave Kleikamp JBUFFER_TRACE(jh_in, "file as BJ_Shadow"); 48296577c43Sdingdinghua spin_lock(&journal->j_list_lock); 48396577c43Sdingdinghua __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow); 48496577c43Sdingdinghua spin_unlock(&journal->j_list_lock); 485b34090e5SJan Kara set_buffer_shadow(bh_in); 48696577c43Sdingdinghua jbd_unlock_bh_state(bh_in); 48796577c43Sdingdinghua 488470decc6SDave Kleikamp return do_escape | (done_copy_out << 1); 489470decc6SDave Kleikamp } 490470decc6SDave Kleikamp 491470decc6SDave Kleikamp /* 492470decc6SDave Kleikamp * Allocation code for the journal file. Manage the space left in the 493470decc6SDave Kleikamp * journal, so that we can begin checkpointing when appropriate. 494470decc6SDave Kleikamp */ 495470decc6SDave Kleikamp 496470decc6SDave Kleikamp /* 497e4471831STheodore Ts'o * Called with j_state_lock locked for writing. 498e4471831STheodore Ts'o * Returns true if a transaction commit was started. 499470decc6SDave Kleikamp */ 500f7f4bccbSMingming Cao int __jbd2_log_start_commit(journal_t *journal, tid_t target) 501470decc6SDave Kleikamp { 502e7b04ac0SEric Sandeen /* Return if the txn has already requested to be committed */ 503e7b04ac0SEric Sandeen if (journal->j_commit_request == target) 504e7b04ac0SEric Sandeen return 0; 505e7b04ac0SEric Sandeen 506470decc6SDave Kleikamp /* 507deeeaf13STheodore Ts'o * The only transaction we can possibly wait upon is the 508deeeaf13STheodore Ts'o * currently running transaction (if it exists). Otherwise, 509deeeaf13STheodore Ts'o * the target tid must be an old one. 510470decc6SDave Kleikamp */ 511deeeaf13STheodore Ts'o if (journal->j_running_transaction && 512deeeaf13STheodore Ts'o journal->j_running_transaction->t_tid == target) { 513470decc6SDave Kleikamp /* 514bcf3d0bcSAndrea Gelmini * We want a new commit: OK, mark the request and wakeup the 515470decc6SDave Kleikamp * commit thread. We do _not_ do the commit ourselves. 516470decc6SDave Kleikamp */ 517470decc6SDave Kleikamp 518470decc6SDave Kleikamp journal->j_commit_request = target; 519f2a44523SEryu Guan jbd_debug(1, "JBD2: requesting commit %d/%d\n", 520470decc6SDave Kleikamp journal->j_commit_request, 521470decc6SDave Kleikamp journal->j_commit_sequence); 5229fff24aaSTheodore Ts'o journal->j_running_transaction->t_requested = jiffies; 523470decc6SDave Kleikamp wake_up(&journal->j_wait_commit); 524470decc6SDave Kleikamp return 1; 525deeeaf13STheodore Ts'o } else if (!tid_geq(journal->j_commit_request, target)) 526deeeaf13STheodore Ts'o /* This should never happen, but if it does, preserve 527deeeaf13STheodore Ts'o the evidence before kjournald goes into a loop and 528deeeaf13STheodore Ts'o increments j_commit_sequence beyond all recognition. */ 529f2a44523SEryu Guan WARN_ONCE(1, "JBD2: bad log_start_commit: %u %u %u %u\n", 5301be2add6STheodore Ts'o journal->j_commit_request, 5311be2add6STheodore Ts'o journal->j_commit_sequence, 532deeeaf13STheodore Ts'o target, journal->j_running_transaction ? 533deeeaf13STheodore Ts'o journal->j_running_transaction->t_tid : 0); 534470decc6SDave Kleikamp return 0; 535470decc6SDave Kleikamp } 536470decc6SDave Kleikamp 537f7f4bccbSMingming Cao int jbd2_log_start_commit(journal_t *journal, tid_t tid) 538470decc6SDave Kleikamp { 539470decc6SDave Kleikamp int ret; 540470decc6SDave Kleikamp 541a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 542f7f4bccbSMingming Cao ret = __jbd2_log_start_commit(journal, tid); 543a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 544470decc6SDave Kleikamp return ret; 545470decc6SDave Kleikamp } 546470decc6SDave Kleikamp 547470decc6SDave Kleikamp /* 5489ff86446SDmitry Monakhov * Force and wait any uncommitted transactions. We can only force the running 5499ff86446SDmitry Monakhov * transaction if we don't have an active handle, otherwise, we will deadlock. 5509ff86446SDmitry Monakhov * Returns: <0 in case of error, 5519ff86446SDmitry Monakhov * 0 if nothing to commit, 5529ff86446SDmitry Monakhov * 1 if transaction was successfully committed. 553470decc6SDave Kleikamp */ 5549ff86446SDmitry Monakhov static int __jbd2_journal_force_commit(journal_t *journal) 555470decc6SDave Kleikamp { 556470decc6SDave Kleikamp transaction_t *transaction = NULL; 557470decc6SDave Kleikamp tid_t tid; 5589ff86446SDmitry Monakhov int need_to_start = 0, ret = 0; 559470decc6SDave Kleikamp 560a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 561470decc6SDave Kleikamp if (journal->j_running_transaction && !current->journal_info) { 562470decc6SDave Kleikamp transaction = journal->j_running_transaction; 563e4471831STheodore Ts'o if (!tid_geq(journal->j_commit_request, transaction->t_tid)) 564e4471831STheodore Ts'o need_to_start = 1; 565470decc6SDave Kleikamp } else if (journal->j_committing_transaction) 566470decc6SDave Kleikamp transaction = journal->j_committing_transaction; 567470decc6SDave Kleikamp 568470decc6SDave Kleikamp if (!transaction) { 5699ff86446SDmitry Monakhov /* Nothing to commit */ 570a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 5719ff86446SDmitry Monakhov return 0; 572470decc6SDave Kleikamp } 573470decc6SDave Kleikamp tid = transaction->t_tid; 574a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 575e4471831STheodore Ts'o if (need_to_start) 576e4471831STheodore Ts'o jbd2_log_start_commit(journal, tid); 5779ff86446SDmitry Monakhov ret = jbd2_log_wait_commit(journal, tid); 5789ff86446SDmitry Monakhov if (!ret) 5799ff86446SDmitry Monakhov ret = 1; 5809ff86446SDmitry Monakhov 5819ff86446SDmitry Monakhov return ret; 5829ff86446SDmitry Monakhov } 5839ff86446SDmitry Monakhov 5849ff86446SDmitry Monakhov /** 5859ff86446SDmitry Monakhov * Force and wait upon a commit if the calling process is not within 5869ff86446SDmitry Monakhov * transaction. This is used for forcing out undo-protected data which contains 5879ff86446SDmitry Monakhov * bitmaps, when the fs is running out of space. 5889ff86446SDmitry Monakhov * 5899ff86446SDmitry Monakhov * @journal: journal to force 5909ff86446SDmitry Monakhov * Returns true if progress was made. 5919ff86446SDmitry Monakhov */ 5929ff86446SDmitry Monakhov int jbd2_journal_force_commit_nested(journal_t *journal) 5939ff86446SDmitry Monakhov { 5949ff86446SDmitry Monakhov int ret; 5959ff86446SDmitry Monakhov 5969ff86446SDmitry Monakhov ret = __jbd2_journal_force_commit(journal); 5979ff86446SDmitry Monakhov return ret > 0; 5989ff86446SDmitry Monakhov } 5999ff86446SDmitry Monakhov 6009ff86446SDmitry Monakhov /** 6019ff86446SDmitry Monakhov * int journal_force_commit() - force any uncommitted transactions 6029ff86446SDmitry Monakhov * @journal: journal to force 6039ff86446SDmitry Monakhov * 6049ff86446SDmitry Monakhov * Caller want unconditional commit. We can only force the running transaction 6059ff86446SDmitry Monakhov * if we don't have an active handle, otherwise, we will deadlock. 6069ff86446SDmitry Monakhov */ 6079ff86446SDmitry Monakhov int jbd2_journal_force_commit(journal_t *journal) 6089ff86446SDmitry Monakhov { 6099ff86446SDmitry Monakhov int ret; 6109ff86446SDmitry Monakhov 6119ff86446SDmitry Monakhov J_ASSERT(!current->journal_info); 6129ff86446SDmitry Monakhov ret = __jbd2_journal_force_commit(journal); 6139ff86446SDmitry Monakhov if (ret > 0) 6149ff86446SDmitry Monakhov ret = 0; 6159ff86446SDmitry Monakhov return ret; 616470decc6SDave Kleikamp } 617470decc6SDave Kleikamp 618470decc6SDave Kleikamp /* 619470decc6SDave Kleikamp * Start a commit of the current running transaction (if any). Returns true 620c88ccea3SJan Kara * if a transaction is going to be committed (or is currently already 621c88ccea3SJan Kara * committing), and fills its tid in at *ptid 622470decc6SDave Kleikamp */ 623f7f4bccbSMingming Cao int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid) 624470decc6SDave Kleikamp { 625470decc6SDave Kleikamp int ret = 0; 626470decc6SDave Kleikamp 627a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 628470decc6SDave Kleikamp if (journal->j_running_transaction) { 629470decc6SDave Kleikamp tid_t tid = journal->j_running_transaction->t_tid; 630470decc6SDave Kleikamp 631c88ccea3SJan Kara __jbd2_log_start_commit(journal, tid); 632c88ccea3SJan Kara /* There's a running transaction and we've just made sure 633c88ccea3SJan Kara * it's commit has been scheduled. */ 634c88ccea3SJan Kara if (ptid) 635470decc6SDave Kleikamp *ptid = tid; 636c88ccea3SJan Kara ret = 1; 637c88ccea3SJan Kara } else if (journal->j_committing_transaction) { 638470decc6SDave Kleikamp /* 63912810ad7SArtem Bityutskiy * If commit has been started, then we have to wait for 64012810ad7SArtem Bityutskiy * completion of that transaction. 641470decc6SDave Kleikamp */ 642c88ccea3SJan Kara if (ptid) 643470decc6SDave Kleikamp *ptid = journal->j_committing_transaction->t_tid; 644470decc6SDave Kleikamp ret = 1; 645470decc6SDave Kleikamp } 646a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 647470decc6SDave Kleikamp return ret; 648470decc6SDave Kleikamp } 649470decc6SDave Kleikamp 650470decc6SDave Kleikamp /* 651bbd2be36SJan Kara * Return 1 if a given transaction has not yet sent barrier request 652bbd2be36SJan Kara * connected with a transaction commit. If 0 is returned, transaction 653bbd2be36SJan Kara * may or may not have sent the barrier. Used to avoid sending barrier 654bbd2be36SJan Kara * twice in common cases. 655bbd2be36SJan Kara */ 656bbd2be36SJan Kara int jbd2_trans_will_send_data_barrier(journal_t *journal, tid_t tid) 657bbd2be36SJan Kara { 658bbd2be36SJan Kara int ret = 0; 659bbd2be36SJan Kara transaction_t *commit_trans; 660bbd2be36SJan Kara 661bbd2be36SJan Kara if (!(journal->j_flags & JBD2_BARRIER)) 662bbd2be36SJan Kara return 0; 663bbd2be36SJan Kara read_lock(&journal->j_state_lock); 664bbd2be36SJan Kara /* Transaction already committed? */ 665bbd2be36SJan Kara if (tid_geq(journal->j_commit_sequence, tid)) 666bbd2be36SJan Kara goto out; 667bbd2be36SJan Kara commit_trans = journal->j_committing_transaction; 668bbd2be36SJan Kara if (!commit_trans || commit_trans->t_tid != tid) { 669bbd2be36SJan Kara ret = 1; 670bbd2be36SJan Kara goto out; 671bbd2be36SJan Kara } 672bbd2be36SJan Kara /* 673bbd2be36SJan Kara * Transaction is being committed and we already proceeded to 674bbd2be36SJan Kara * submitting a flush to fs partition? 675bbd2be36SJan Kara */ 676bbd2be36SJan Kara if (journal->j_fs_dev != journal->j_dev) { 677bbd2be36SJan Kara if (!commit_trans->t_need_data_flush || 678bbd2be36SJan Kara commit_trans->t_state >= T_COMMIT_DFLUSH) 679bbd2be36SJan Kara goto out; 680bbd2be36SJan Kara } else { 681bbd2be36SJan Kara if (commit_trans->t_state >= T_COMMIT_JFLUSH) 682bbd2be36SJan Kara goto out; 683bbd2be36SJan Kara } 684bbd2be36SJan Kara ret = 1; 685bbd2be36SJan Kara out: 686bbd2be36SJan Kara read_unlock(&journal->j_state_lock); 687bbd2be36SJan Kara return ret; 688bbd2be36SJan Kara } 689bbd2be36SJan Kara EXPORT_SYMBOL(jbd2_trans_will_send_data_barrier); 690bbd2be36SJan Kara 691bbd2be36SJan Kara /* 692470decc6SDave Kleikamp * Wait for a specified commit to complete. 693470decc6SDave Kleikamp * The caller may not hold the journal lock. 694470decc6SDave Kleikamp */ 695f7f4bccbSMingming Cao int jbd2_log_wait_commit(journal_t *journal, tid_t tid) 696470decc6SDave Kleikamp { 697470decc6SDave Kleikamp int err = 0; 698470decc6SDave Kleikamp 699c52c47e4SJan Kara read_lock(&journal->j_state_lock); 700c52c47e4SJan Kara #ifdef CONFIG_PROVE_LOCKING 701c52c47e4SJan Kara /* 702c52c47e4SJan Kara * Some callers make sure transaction is already committing and in that 703c52c47e4SJan Kara * case we cannot block on open handles anymore. So don't warn in that 704c52c47e4SJan Kara * case. 705c52c47e4SJan Kara */ 706c52c47e4SJan Kara if (tid_gt(tid, journal->j_commit_sequence) && 707c52c47e4SJan Kara (!journal->j_committing_transaction || 708c52c47e4SJan Kara journal->j_committing_transaction->t_tid != tid)) { 709c52c47e4SJan Kara read_unlock(&journal->j_state_lock); 7101eaa566dSJan Kara jbd2_might_wait_for_commit(journal); 711a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 712c52c47e4SJan Kara } 713c52c47e4SJan Kara #endif 714e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 715470decc6SDave Kleikamp if (!tid_geq(journal->j_commit_request, tid)) { 71675685071SJan Kara printk(KERN_ERR 717470decc6SDave Kleikamp "%s: error: j_commit_request=%d, tid=%d\n", 718329d291fSHarvey Harrison __func__, journal->j_commit_request, tid); 719470decc6SDave Kleikamp } 720470decc6SDave Kleikamp #endif 721470decc6SDave Kleikamp while (tid_gt(tid, journal->j_commit_sequence)) { 722f2a44523SEryu Guan jbd_debug(1, "JBD2: want %d, j_commit_sequence=%d\n", 723470decc6SDave Kleikamp tid, journal->j_commit_sequence); 724a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 7253469a32aSTheodore Ts'o wake_up(&journal->j_wait_commit); 726470decc6SDave Kleikamp wait_event(journal->j_wait_done_commit, 727470decc6SDave Kleikamp !tid_gt(tid, journal->j_commit_sequence)); 728a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 729470decc6SDave Kleikamp } 730a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 731470decc6SDave Kleikamp 73275685071SJan Kara if (unlikely(is_journal_aborted(journal))) 733470decc6SDave Kleikamp err = -EIO; 734470decc6SDave Kleikamp return err; 735470decc6SDave Kleikamp } 736470decc6SDave Kleikamp 737b8a6176cSJan Kara /* Return 1 when transaction with given tid has already committed. */ 738b8a6176cSJan Kara int jbd2_transaction_committed(journal_t *journal, tid_t tid) 739b8a6176cSJan Kara { 740b8a6176cSJan Kara int ret = 1; 741b8a6176cSJan Kara 742b8a6176cSJan Kara read_lock(&journal->j_state_lock); 743b8a6176cSJan Kara if (journal->j_running_transaction && 744b8a6176cSJan Kara journal->j_running_transaction->t_tid == tid) 745b8a6176cSJan Kara ret = 0; 746b8a6176cSJan Kara if (journal->j_committing_transaction && 747b8a6176cSJan Kara journal->j_committing_transaction->t_tid == tid) 748b8a6176cSJan Kara ret = 0; 749b8a6176cSJan Kara read_unlock(&journal->j_state_lock); 750b8a6176cSJan Kara return ret; 751b8a6176cSJan Kara } 752b8a6176cSJan Kara EXPORT_SYMBOL(jbd2_transaction_committed); 753b8a6176cSJan Kara 754470decc6SDave Kleikamp /* 755d76a3a77STheodore Ts'o * When this function returns the transaction corresponding to tid 756d76a3a77STheodore Ts'o * will be completed. If the transaction has currently running, start 757d76a3a77STheodore Ts'o * committing that transaction before waiting for it to complete. If 758d76a3a77STheodore Ts'o * the transaction id is stale, it is by definition already completed, 759d76a3a77STheodore Ts'o * so just return SUCCESS. 760d76a3a77STheodore Ts'o */ 761d76a3a77STheodore Ts'o int jbd2_complete_transaction(journal_t *journal, tid_t tid) 762d76a3a77STheodore Ts'o { 763d76a3a77STheodore Ts'o int need_to_wait = 1; 764d76a3a77STheodore Ts'o 765d76a3a77STheodore Ts'o read_lock(&journal->j_state_lock); 766d76a3a77STheodore Ts'o if (journal->j_running_transaction && 767d76a3a77STheodore Ts'o journal->j_running_transaction->t_tid == tid) { 768d76a3a77STheodore Ts'o if (journal->j_commit_request != tid) { 769d76a3a77STheodore Ts'o /* transaction not yet started, so request it */ 770d76a3a77STheodore Ts'o read_unlock(&journal->j_state_lock); 771d76a3a77STheodore Ts'o jbd2_log_start_commit(journal, tid); 772d76a3a77STheodore Ts'o goto wait_commit; 773d76a3a77STheodore Ts'o } 774d76a3a77STheodore Ts'o } else if (!(journal->j_committing_transaction && 775d76a3a77STheodore Ts'o journal->j_committing_transaction->t_tid == tid)) 776d76a3a77STheodore Ts'o need_to_wait = 0; 777d76a3a77STheodore Ts'o read_unlock(&journal->j_state_lock); 778d76a3a77STheodore Ts'o if (!need_to_wait) 779d76a3a77STheodore Ts'o return 0; 780d76a3a77STheodore Ts'o wait_commit: 781d76a3a77STheodore Ts'o return jbd2_log_wait_commit(journal, tid); 782d76a3a77STheodore Ts'o } 783d76a3a77STheodore Ts'o EXPORT_SYMBOL(jbd2_complete_transaction); 784d76a3a77STheodore Ts'o 785d76a3a77STheodore Ts'o /* 786470decc6SDave Kleikamp * Log buffer allocation routines: 787470decc6SDave Kleikamp */ 788470decc6SDave Kleikamp 78918eba7aaSMingming Cao int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp) 790470decc6SDave Kleikamp { 791470decc6SDave Kleikamp unsigned long blocknr; 792470decc6SDave Kleikamp 793a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 794470decc6SDave Kleikamp J_ASSERT(journal->j_free > 1); 795470decc6SDave Kleikamp 796470decc6SDave Kleikamp blocknr = journal->j_head; 797470decc6SDave Kleikamp journal->j_head++; 798470decc6SDave Kleikamp journal->j_free--; 799470decc6SDave Kleikamp if (journal->j_head == journal->j_last) 800470decc6SDave Kleikamp journal->j_head = journal->j_first; 801a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 802f7f4bccbSMingming Cao return jbd2_journal_bmap(journal, blocknr, retp); 803470decc6SDave Kleikamp } 804470decc6SDave Kleikamp 805470decc6SDave Kleikamp /* 806470decc6SDave Kleikamp * Conversion of logical to physical block numbers for the journal 807470decc6SDave Kleikamp * 808470decc6SDave Kleikamp * On external journals the journal blocks are identity-mapped, so 809470decc6SDave Kleikamp * this is a no-op. If needed, we can use j_blk_offset - everything is 810470decc6SDave Kleikamp * ready. 811470decc6SDave Kleikamp */ 812f7f4bccbSMingming Cao int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr, 81318eba7aaSMingming Cao unsigned long long *retp) 814470decc6SDave Kleikamp { 815470decc6SDave Kleikamp int err = 0; 81618eba7aaSMingming Cao unsigned long long ret; 817470decc6SDave Kleikamp 818470decc6SDave Kleikamp if (journal->j_inode) { 819470decc6SDave Kleikamp ret = bmap(journal->j_inode, blocknr); 820470decc6SDave Kleikamp if (ret) 821470decc6SDave Kleikamp *retp = ret; 822470decc6SDave Kleikamp else { 823470decc6SDave Kleikamp printk(KERN_ALERT "%s: journal block not found " 824470decc6SDave Kleikamp "at offset %lu on %s\n", 82505496769STheodore Ts'o __func__, blocknr, journal->j_devname); 826470decc6SDave Kleikamp err = -EIO; 827470decc6SDave Kleikamp __journal_abort_soft(journal, err); 828470decc6SDave Kleikamp } 829470decc6SDave Kleikamp } else { 830470decc6SDave Kleikamp *retp = blocknr; /* +journal->j_blk_offset */ 831470decc6SDave Kleikamp } 832470decc6SDave Kleikamp return err; 833470decc6SDave Kleikamp } 834470decc6SDave Kleikamp 835470decc6SDave Kleikamp /* 836470decc6SDave Kleikamp * We play buffer_head aliasing tricks to write data/metadata blocks to 837470decc6SDave Kleikamp * the journal without copying their contents, but for journal 838470decc6SDave Kleikamp * descriptor blocks we do need to generate bona fide buffers. 839470decc6SDave Kleikamp * 840f7f4bccbSMingming Cao * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying 841470decc6SDave Kleikamp * the buffer's contents they really should run flush_dcache_page(bh->b_page). 842470decc6SDave Kleikamp * But we don't bother doing that, so there will be coherency problems with 843470decc6SDave Kleikamp * mmaps of blockdevs which hold live JBD-controlled filesystems. 844470decc6SDave Kleikamp */ 84532ab6715SJan Kara struct buffer_head * 84632ab6715SJan Kara jbd2_journal_get_descriptor_buffer(transaction_t *transaction, int type) 847470decc6SDave Kleikamp { 84832ab6715SJan Kara journal_t *journal = transaction->t_journal; 849470decc6SDave Kleikamp struct buffer_head *bh; 85018eba7aaSMingming Cao unsigned long long blocknr; 85132ab6715SJan Kara journal_header_t *header; 852470decc6SDave Kleikamp int err; 853470decc6SDave Kleikamp 854f7f4bccbSMingming Cao err = jbd2_journal_next_log_block(journal, &blocknr); 855470decc6SDave Kleikamp 856470decc6SDave Kleikamp if (err) 857470decc6SDave Kleikamp return NULL; 858470decc6SDave Kleikamp 859470decc6SDave Kleikamp bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize); 8604b905671SJan Kara if (!bh) 8614b905671SJan Kara return NULL; 862470decc6SDave Kleikamp lock_buffer(bh); 863470decc6SDave Kleikamp memset(bh->b_data, 0, journal->j_blocksize); 86432ab6715SJan Kara header = (journal_header_t *)bh->b_data; 86532ab6715SJan Kara header->h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER); 86632ab6715SJan Kara header->h_blocktype = cpu_to_be32(type); 86732ab6715SJan Kara header->h_sequence = cpu_to_be32(transaction->t_tid); 868470decc6SDave Kleikamp set_buffer_uptodate(bh); 869470decc6SDave Kleikamp unlock_buffer(bh); 870470decc6SDave Kleikamp BUFFER_TRACE(bh, "return this buffer"); 871e5a120aeSJan Kara return bh; 872470decc6SDave Kleikamp } 873470decc6SDave Kleikamp 8741101cd4dSJan Kara void jbd2_descriptor_block_csum_set(journal_t *j, struct buffer_head *bh) 8751101cd4dSJan Kara { 8761101cd4dSJan Kara struct jbd2_journal_block_tail *tail; 8771101cd4dSJan Kara __u32 csum; 8781101cd4dSJan Kara 8791101cd4dSJan Kara if (!jbd2_journal_has_csum_v2or3(j)) 8801101cd4dSJan Kara return; 8811101cd4dSJan Kara 8821101cd4dSJan Kara tail = (struct jbd2_journal_block_tail *)(bh->b_data + j->j_blocksize - 8831101cd4dSJan Kara sizeof(struct jbd2_journal_block_tail)); 8841101cd4dSJan Kara tail->t_checksum = 0; 8851101cd4dSJan Kara csum = jbd2_chksum(j, j->j_csum_seed, bh->b_data, j->j_blocksize); 8861101cd4dSJan Kara tail->t_checksum = cpu_to_be32(csum); 8871101cd4dSJan Kara } 8881101cd4dSJan Kara 88979feb521SJan Kara /* 89079feb521SJan Kara * Return tid of the oldest transaction in the journal and block in the journal 89179feb521SJan Kara * where the transaction starts. 89279feb521SJan Kara * 89379feb521SJan Kara * If the journal is now empty, return which will be the next transaction ID 89479feb521SJan Kara * we will write and where will that transaction start. 89579feb521SJan Kara * 89679feb521SJan Kara * The return value is 0 if journal tail cannot be pushed any further, 1 if 89779feb521SJan Kara * it can. 89879feb521SJan Kara */ 89979feb521SJan Kara int jbd2_journal_get_log_tail(journal_t *journal, tid_t *tid, 90079feb521SJan Kara unsigned long *block) 90179feb521SJan Kara { 90279feb521SJan Kara transaction_t *transaction; 90379feb521SJan Kara int ret; 90479feb521SJan Kara 90579feb521SJan Kara read_lock(&journal->j_state_lock); 90679feb521SJan Kara spin_lock(&journal->j_list_lock); 90779feb521SJan Kara transaction = journal->j_checkpoint_transactions; 90879feb521SJan Kara if (transaction) { 90979feb521SJan Kara *tid = transaction->t_tid; 91079feb521SJan Kara *block = transaction->t_log_start; 91179feb521SJan Kara } else if ((transaction = journal->j_committing_transaction) != NULL) { 91279feb521SJan Kara *tid = transaction->t_tid; 91379feb521SJan Kara *block = transaction->t_log_start; 91479feb521SJan Kara } else if ((transaction = journal->j_running_transaction) != NULL) { 91579feb521SJan Kara *tid = transaction->t_tid; 91679feb521SJan Kara *block = journal->j_head; 91779feb521SJan Kara } else { 91879feb521SJan Kara *tid = journal->j_transaction_sequence; 91979feb521SJan Kara *block = journal->j_head; 92079feb521SJan Kara } 92179feb521SJan Kara ret = tid_gt(*tid, journal->j_tail_sequence); 92279feb521SJan Kara spin_unlock(&journal->j_list_lock); 92379feb521SJan Kara read_unlock(&journal->j_state_lock); 92479feb521SJan Kara 92579feb521SJan Kara return ret; 92679feb521SJan Kara } 92779feb521SJan Kara 92879feb521SJan Kara /* 92979feb521SJan Kara * Update information in journal structure and in on disk journal superblock 93079feb521SJan Kara * about log tail. This function does not check whether information passed in 93179feb521SJan Kara * really pushes log tail further. It's responsibility of the caller to make 93279feb521SJan Kara * sure provided log tail information is valid (e.g. by holding 93379feb521SJan Kara * j_checkpoint_mutex all the time between computing log tail and calling this 93479feb521SJan Kara * function as is the case with jbd2_cleanup_journal_tail()). 93579feb521SJan Kara * 93679feb521SJan Kara * Requires j_checkpoint_mutex 93779feb521SJan Kara */ 9386f6a6fdaSJoseph Qi int __jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block) 93979feb521SJan Kara { 94079feb521SJan Kara unsigned long freed; 9416f6a6fdaSJoseph Qi int ret; 94279feb521SJan Kara 94379feb521SJan Kara BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex)); 94479feb521SJan Kara 94579feb521SJan Kara /* 94679feb521SJan Kara * We cannot afford for write to remain in drive's caches since as 94779feb521SJan Kara * soon as we update j_tail, next transaction can start reusing journal 94879feb521SJan Kara * space and if we lose sb update during power failure we'd replay 94979feb521SJan Kara * old transaction with possibly newly overwritten data. 95079feb521SJan Kara */ 95117f423b5SJan Kara ret = jbd2_journal_update_sb_log_tail(journal, tid, block, 95217f423b5SJan Kara REQ_SYNC | REQ_FUA); 9536f6a6fdaSJoseph Qi if (ret) 9546f6a6fdaSJoseph Qi goto out; 9556f6a6fdaSJoseph Qi 95679feb521SJan Kara write_lock(&journal->j_state_lock); 95779feb521SJan Kara freed = block - journal->j_tail; 95879feb521SJan Kara if (block < journal->j_tail) 95979feb521SJan Kara freed += journal->j_last - journal->j_first; 96079feb521SJan Kara 96179feb521SJan Kara trace_jbd2_update_log_tail(journal, tid, block, freed); 96279feb521SJan Kara jbd_debug(1, 96379feb521SJan Kara "Cleaning journal tail from %d to %d (offset %lu), " 96479feb521SJan Kara "freeing %lu\n", 96579feb521SJan Kara journal->j_tail_sequence, tid, block, freed); 96679feb521SJan Kara 96779feb521SJan Kara journal->j_free += freed; 96879feb521SJan Kara journal->j_tail_sequence = tid; 96979feb521SJan Kara journal->j_tail = block; 97079feb521SJan Kara write_unlock(&journal->j_state_lock); 9716f6a6fdaSJoseph Qi 9726f6a6fdaSJoseph Qi out: 9736f6a6fdaSJoseph Qi return ret; 97479feb521SJan Kara } 97579feb521SJan Kara 9763339578fSJan Kara /* 97785e0c4e8STheodore Ts'o * This is a variation of __jbd2_update_log_tail which checks for validity of 9783339578fSJan Kara * provided log tail and locks j_checkpoint_mutex. So it is safe against races 9793339578fSJan Kara * with other threads updating log tail. 9803339578fSJan Kara */ 9813339578fSJan Kara void jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block) 9823339578fSJan Kara { 9836fa7aa50STejun Heo mutex_lock_io(&journal->j_checkpoint_mutex); 9843339578fSJan Kara if (tid_gt(tid, journal->j_tail_sequence)) 9853339578fSJan Kara __jbd2_update_log_tail(journal, tid, block); 9863339578fSJan Kara mutex_unlock(&journal->j_checkpoint_mutex); 9873339578fSJan Kara } 9883339578fSJan Kara 9898e85fb3fSJohann Lombardi struct jbd2_stats_proc_session { 9908e85fb3fSJohann Lombardi journal_t *journal; 9918e85fb3fSJohann Lombardi struct transaction_stats_s *stats; 9928e85fb3fSJohann Lombardi int start; 9938e85fb3fSJohann Lombardi int max; 9948e85fb3fSJohann Lombardi }; 9958e85fb3fSJohann Lombardi 9968e85fb3fSJohann Lombardi static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos) 9978e85fb3fSJohann Lombardi { 9988e85fb3fSJohann Lombardi return *pos ? NULL : SEQ_START_TOKEN; 9998e85fb3fSJohann Lombardi } 10008e85fb3fSJohann Lombardi 10018e85fb3fSJohann Lombardi static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos) 10028e85fb3fSJohann Lombardi { 10038e85fb3fSJohann Lombardi return NULL; 10048e85fb3fSJohann Lombardi } 10058e85fb3fSJohann Lombardi 10068e85fb3fSJohann Lombardi static int jbd2_seq_info_show(struct seq_file *seq, void *v) 10078e85fb3fSJohann Lombardi { 10088e85fb3fSJohann Lombardi struct jbd2_stats_proc_session *s = seq->private; 10098e85fb3fSJohann Lombardi 10108e85fb3fSJohann Lombardi if (v != SEQ_START_TOKEN) 10118e85fb3fSJohann Lombardi return 0; 10129fff24aaSTheodore Ts'o seq_printf(seq, "%lu transactions (%lu requested), " 10139fff24aaSTheodore Ts'o "each up to %u blocks\n", 10149fff24aaSTheodore Ts'o s->stats->ts_tid, s->stats->ts_requested, 10158e85fb3fSJohann Lombardi s->journal->j_max_transaction_buffers); 10168e85fb3fSJohann Lombardi if (s->stats->ts_tid == 0) 10178e85fb3fSJohann Lombardi return 0; 10188e85fb3fSJohann Lombardi seq_printf(seq, "average: \n %ums waiting for transaction\n", 1019bf699327STheodore Ts'o jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid)); 10209fff24aaSTheodore Ts'o seq_printf(seq, " %ums request delay\n", 10219fff24aaSTheodore Ts'o (s->stats->ts_requested == 0) ? 0 : 10229fff24aaSTheodore Ts'o jiffies_to_msecs(s->stats->run.rs_request_delay / 10239fff24aaSTheodore Ts'o s->stats->ts_requested)); 10248e85fb3fSJohann Lombardi seq_printf(seq, " %ums running transaction\n", 1025bf699327STheodore Ts'o jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid)); 10268e85fb3fSJohann Lombardi seq_printf(seq, " %ums transaction was being locked\n", 1027bf699327STheodore Ts'o jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid)); 10288e85fb3fSJohann Lombardi seq_printf(seq, " %ums flushing data (in ordered mode)\n", 1029bf699327STheodore Ts'o jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid)); 10308e85fb3fSJohann Lombardi seq_printf(seq, " %ums logging transaction\n", 1031bf699327STheodore Ts'o jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid)); 1032c225aa57SSimon Holm Thøgersen seq_printf(seq, " %lluus average transaction commit time\n", 1033c225aa57SSimon Holm Thøgersen div_u64(s->journal->j_average_commit_time, 1000)); 10348e85fb3fSJohann Lombardi seq_printf(seq, " %lu handles per transaction\n", 1035bf699327STheodore Ts'o s->stats->run.rs_handle_count / s->stats->ts_tid); 10368e85fb3fSJohann Lombardi seq_printf(seq, " %lu blocks per transaction\n", 1037bf699327STheodore Ts'o s->stats->run.rs_blocks / s->stats->ts_tid); 10388e85fb3fSJohann Lombardi seq_printf(seq, " %lu logged blocks per transaction\n", 1039bf699327STheodore Ts'o s->stats->run.rs_blocks_logged / s->stats->ts_tid); 10408e85fb3fSJohann Lombardi return 0; 10418e85fb3fSJohann Lombardi } 10428e85fb3fSJohann Lombardi 10438e85fb3fSJohann Lombardi static void jbd2_seq_info_stop(struct seq_file *seq, void *v) 10448e85fb3fSJohann Lombardi { 10458e85fb3fSJohann Lombardi } 10468e85fb3fSJohann Lombardi 104788e9d34cSJames Morris static const struct seq_operations jbd2_seq_info_ops = { 10488e85fb3fSJohann Lombardi .start = jbd2_seq_info_start, 10498e85fb3fSJohann Lombardi .next = jbd2_seq_info_next, 10508e85fb3fSJohann Lombardi .stop = jbd2_seq_info_stop, 10518e85fb3fSJohann Lombardi .show = jbd2_seq_info_show, 10528e85fb3fSJohann Lombardi }; 10538e85fb3fSJohann Lombardi 10548e85fb3fSJohann Lombardi static int jbd2_seq_info_open(struct inode *inode, struct file *file) 10558e85fb3fSJohann Lombardi { 1056d9dda78bSAl Viro journal_t *journal = PDE_DATA(inode); 10578e85fb3fSJohann Lombardi struct jbd2_stats_proc_session *s; 10588e85fb3fSJohann Lombardi int rc, size; 10598e85fb3fSJohann Lombardi 10608e85fb3fSJohann Lombardi s = kmalloc(sizeof(*s), GFP_KERNEL); 10618e85fb3fSJohann Lombardi if (s == NULL) 10628e85fb3fSJohann Lombardi return -ENOMEM; 10638e85fb3fSJohann Lombardi size = sizeof(struct transaction_stats_s); 10648e85fb3fSJohann Lombardi s->stats = kmalloc(size, GFP_KERNEL); 10658e85fb3fSJohann Lombardi if (s->stats == NULL) { 10668e85fb3fSJohann Lombardi kfree(s); 10678e85fb3fSJohann Lombardi return -ENOMEM; 10688e85fb3fSJohann Lombardi } 10698e85fb3fSJohann Lombardi spin_lock(&journal->j_history_lock); 10708e85fb3fSJohann Lombardi memcpy(s->stats, &journal->j_stats, size); 10718e85fb3fSJohann Lombardi s->journal = journal; 10728e85fb3fSJohann Lombardi spin_unlock(&journal->j_history_lock); 10738e85fb3fSJohann Lombardi 10748e85fb3fSJohann Lombardi rc = seq_open(file, &jbd2_seq_info_ops); 10758e85fb3fSJohann Lombardi if (rc == 0) { 10768e85fb3fSJohann Lombardi struct seq_file *m = file->private_data; 10778e85fb3fSJohann Lombardi m->private = s; 10788e85fb3fSJohann Lombardi } else { 10798e85fb3fSJohann Lombardi kfree(s->stats); 10808e85fb3fSJohann Lombardi kfree(s); 10818e85fb3fSJohann Lombardi } 10828e85fb3fSJohann Lombardi return rc; 10838e85fb3fSJohann Lombardi 10848e85fb3fSJohann Lombardi } 10858e85fb3fSJohann Lombardi 10868e85fb3fSJohann Lombardi static int jbd2_seq_info_release(struct inode *inode, struct file *file) 10878e85fb3fSJohann Lombardi { 10888e85fb3fSJohann Lombardi struct seq_file *seq = file->private_data; 10898e85fb3fSJohann Lombardi struct jbd2_stats_proc_session *s = seq->private; 10908e85fb3fSJohann Lombardi kfree(s->stats); 10918e85fb3fSJohann Lombardi kfree(s); 10928e85fb3fSJohann Lombardi return seq_release(inode, file); 10938e85fb3fSJohann Lombardi } 10948e85fb3fSJohann Lombardi 1095828c0950SAlexey Dobriyan static const struct file_operations jbd2_seq_info_fops = { 10968e85fb3fSJohann Lombardi .owner = THIS_MODULE, 10978e85fb3fSJohann Lombardi .open = jbd2_seq_info_open, 10988e85fb3fSJohann Lombardi .read = seq_read, 10998e85fb3fSJohann Lombardi .llseek = seq_lseek, 11008e85fb3fSJohann Lombardi .release = jbd2_seq_info_release, 11018e85fb3fSJohann Lombardi }; 11028e85fb3fSJohann Lombardi 11038e85fb3fSJohann Lombardi static struct proc_dir_entry *proc_jbd2_stats; 11048e85fb3fSJohann Lombardi 11058e85fb3fSJohann Lombardi static void jbd2_stats_proc_init(journal_t *journal) 11068e85fb3fSJohann Lombardi { 110705496769STheodore Ts'o journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats); 11088e85fb3fSJohann Lombardi if (journal->j_proc_entry) { 110979da3664SDenis V. Lunev proc_create_data("info", S_IRUGO, journal->j_proc_entry, 111079da3664SDenis V. Lunev &jbd2_seq_info_fops, journal); 11118e85fb3fSJohann Lombardi } 11128e85fb3fSJohann Lombardi } 11138e85fb3fSJohann Lombardi 11148e85fb3fSJohann Lombardi static void jbd2_stats_proc_exit(journal_t *journal) 11158e85fb3fSJohann Lombardi { 11168e85fb3fSJohann Lombardi remove_proc_entry("info", journal->j_proc_entry); 111705496769STheodore Ts'o remove_proc_entry(journal->j_devname, proc_jbd2_stats); 11188e85fb3fSJohann Lombardi } 11198e85fb3fSJohann Lombardi 1120470decc6SDave Kleikamp /* 1121470decc6SDave Kleikamp * Management for journal control blocks: functions to create and 1122470decc6SDave Kleikamp * destroy journal_t structures, and to initialise and read existing 1123470decc6SDave Kleikamp * journal blocks from disk. */ 1124470decc6SDave Kleikamp 1125470decc6SDave Kleikamp /* First: create and setup a journal_t object in memory. We initialise 1126470decc6SDave Kleikamp * very few fields yet: that has to wait until we have created the 1127470decc6SDave Kleikamp * journal structures from from scratch, or loaded them from disk. */ 1128470decc6SDave Kleikamp 1129f0c9fd54SGeliang Tang static journal_t *journal_init_common(struct block_device *bdev, 1130f0c9fd54SGeliang Tang struct block_device *fs_dev, 1131f0c9fd54SGeliang Tang unsigned long long start, int len, int blocksize) 1132470decc6SDave Kleikamp { 1133ab714affSJan Kara static struct lock_class_key jbd2_trans_commit_key; 1134470decc6SDave Kleikamp journal_t *journal; 1135470decc6SDave Kleikamp int err; 1136f0c9fd54SGeliang Tang struct buffer_head *bh; 1137f0c9fd54SGeliang Tang int n; 1138470decc6SDave Kleikamp 11393ebfdf88SAndrew Morton journal = kzalloc(sizeof(*journal), GFP_KERNEL); 1140470decc6SDave Kleikamp if (!journal) 1141b7271b0aSTheodore Ts'o return NULL; 1142470decc6SDave Kleikamp 1143470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_transaction_locked); 1144470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_done_commit); 1145470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_commit); 1146470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_updates); 11478f7d89f3SJan Kara init_waitqueue_head(&journal->j_wait_reserved); 1148470decc6SDave Kleikamp mutex_init(&journal->j_barrier); 1149470decc6SDave Kleikamp mutex_init(&journal->j_checkpoint_mutex); 1150470decc6SDave Kleikamp spin_lock_init(&journal->j_revoke_lock); 1151470decc6SDave Kleikamp spin_lock_init(&journal->j_list_lock); 1152a931da6aSTheodore Ts'o rwlock_init(&journal->j_state_lock); 1153470decc6SDave Kleikamp 1154cd02ff0bSMingming Cao journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE); 115530773840STheodore Ts'o journal->j_min_batch_time = 0; 115630773840STheodore Ts'o journal->j_max_batch_time = 15000; /* 15ms */ 11578f7d89f3SJan Kara atomic_set(&journal->j_reserved_credits, 0); 1158470decc6SDave Kleikamp 1159470decc6SDave Kleikamp /* The journal is marked for error until we succeed with recovery! */ 1160f7f4bccbSMingming Cao journal->j_flags = JBD2_ABORT; 1161470decc6SDave Kleikamp 1162470decc6SDave Kleikamp /* Set up a default-sized revoke table for the new mount. */ 1163f7f4bccbSMingming Cao err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH); 1164cd9cb405SEric Biggers if (err) 1165cd9cb405SEric Biggers goto err_cleanup; 11668e85fb3fSJohann Lombardi 1167bf699327STheodore Ts'o spin_lock_init(&journal->j_history_lock); 11688e85fb3fSJohann Lombardi 1169ab714affSJan Kara lockdep_init_map(&journal->j_trans_commit_map, "jbd2_handle", 1170ab714affSJan Kara &jbd2_trans_commit_key, 0); 1171ab714affSJan Kara 1172f0c9fd54SGeliang Tang /* journal descriptor can store up to n blocks -bzzz */ 1173f0c9fd54SGeliang Tang journal->j_blocksize = blocksize; 1174f0c9fd54SGeliang Tang journal->j_dev = bdev; 1175f0c9fd54SGeliang Tang journal->j_fs_dev = fs_dev; 1176f0c9fd54SGeliang Tang journal->j_blk_offset = start; 1177f0c9fd54SGeliang Tang journal->j_maxlen = len; 1178f0c9fd54SGeliang Tang n = journal->j_blocksize / sizeof(journal_block_tag_t); 1179f0c9fd54SGeliang Tang journal->j_wbufsize = n; 1180f0c9fd54SGeliang Tang journal->j_wbuf = kmalloc_array(n, sizeof(struct buffer_head *), 1181f0c9fd54SGeliang Tang GFP_KERNEL); 1182cd9cb405SEric Biggers if (!journal->j_wbuf) 1183cd9cb405SEric Biggers goto err_cleanup; 1184f0c9fd54SGeliang Tang 1185f0c9fd54SGeliang Tang bh = getblk_unmovable(journal->j_dev, start, journal->j_blocksize); 1186f0c9fd54SGeliang Tang if (!bh) { 1187f0c9fd54SGeliang Tang pr_err("%s: Cannot get buffer for journal superblock\n", 1188f0c9fd54SGeliang Tang __func__); 1189cd9cb405SEric Biggers goto err_cleanup; 1190f0c9fd54SGeliang Tang } 1191f0c9fd54SGeliang Tang journal->j_sb_buffer = bh; 1192f0c9fd54SGeliang Tang journal->j_superblock = (journal_superblock_t *)bh->b_data; 1193f0c9fd54SGeliang Tang 1194470decc6SDave Kleikamp return journal; 1195cd9cb405SEric Biggers 1196cd9cb405SEric Biggers err_cleanup: 1197cd9cb405SEric Biggers kfree(journal->j_wbuf); 1198cd9cb405SEric Biggers jbd2_journal_destroy_revoke(journal); 1199cd9cb405SEric Biggers kfree(journal); 1200cd9cb405SEric Biggers return NULL; 1201470decc6SDave Kleikamp } 1202470decc6SDave Kleikamp 1203f7f4bccbSMingming Cao /* jbd2_journal_init_dev and jbd2_journal_init_inode: 1204470decc6SDave Kleikamp * 1205470decc6SDave Kleikamp * Create a journal structure assigned some fixed set of disk blocks to 1206470decc6SDave Kleikamp * the journal. We don't actually touch those disk blocks yet, but we 1207470decc6SDave Kleikamp * need to set up all of the mapping information to tell the journaling 1208470decc6SDave Kleikamp * system where the journal blocks are. 1209470decc6SDave Kleikamp * 1210470decc6SDave Kleikamp */ 1211470decc6SDave Kleikamp 1212470decc6SDave Kleikamp /** 12135648ba5bSRandy Dunlap * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure 1214470decc6SDave Kleikamp * @bdev: Block device on which to create the journal 1215470decc6SDave Kleikamp * @fs_dev: Device which hold journalled filesystem for this journal. 1216470decc6SDave Kleikamp * @start: Block nr Start of journal. 1217470decc6SDave Kleikamp * @len: Length of the journal in blocks. 1218470decc6SDave Kleikamp * @blocksize: blocksize of journalling device 12195648ba5bSRandy Dunlap * 12205648ba5bSRandy Dunlap * Returns: a newly created journal_t * 1221470decc6SDave Kleikamp * 1222f7f4bccbSMingming Cao * jbd2_journal_init_dev creates a journal which maps a fixed contiguous 1223470decc6SDave Kleikamp * range of blocks on an arbitrary block device. 1224470decc6SDave Kleikamp * 1225470decc6SDave Kleikamp */ 1226f7f4bccbSMingming Cao journal_t *jbd2_journal_init_dev(struct block_device *bdev, 1227470decc6SDave Kleikamp struct block_device *fs_dev, 122818eba7aaSMingming Cao unsigned long long start, int len, int blocksize) 1229470decc6SDave Kleikamp { 1230f0c9fd54SGeliang Tang journal_t *journal; 1231470decc6SDave Kleikamp 1232f0c9fd54SGeliang Tang journal = journal_init_common(bdev, fs_dev, start, len, blocksize); 1233470decc6SDave Kleikamp if (!journal) 1234470decc6SDave Kleikamp return NULL; 1235470decc6SDave Kleikamp 12360587aa3dSyangsheng bdevname(journal->j_dev, journal->j_devname); 123781ae394bSRasmus Villemoes strreplace(journal->j_devname, '/', '!'); 12384b905671SJan Kara jbd2_stats_proc_init(journal); 12394b905671SJan Kara 1240470decc6SDave Kleikamp return journal; 1241470decc6SDave Kleikamp } 1242470decc6SDave Kleikamp 1243470decc6SDave Kleikamp /** 1244f7f4bccbSMingming Cao * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode. 1245470decc6SDave Kleikamp * @inode: An inode to create the journal in 1246470decc6SDave Kleikamp * 1247f7f4bccbSMingming Cao * jbd2_journal_init_inode creates a journal which maps an on-disk inode as 1248470decc6SDave Kleikamp * the journal. The inode must exist already, must support bmap() and 1249470decc6SDave Kleikamp * must have all data blocks preallocated. 1250470decc6SDave Kleikamp */ 1251f7f4bccbSMingming Cao journal_t *jbd2_journal_init_inode(struct inode *inode) 1252470decc6SDave Kleikamp { 1253f0c9fd54SGeliang Tang journal_t *journal; 125405496769STheodore Ts'o char *p; 125518eba7aaSMingming Cao unsigned long long blocknr; 1256470decc6SDave Kleikamp 1257f0c9fd54SGeliang Tang blocknr = bmap(inode, 0); 1258f0c9fd54SGeliang Tang if (!blocknr) { 1259f0c9fd54SGeliang Tang pr_err("%s: Cannot locate journal superblock\n", 1260f0c9fd54SGeliang Tang __func__); 1261f0c9fd54SGeliang Tang return NULL; 1262f0c9fd54SGeliang Tang } 1263f0c9fd54SGeliang Tang 1264f0c9fd54SGeliang Tang jbd_debug(1, "JBD2: inode %s/%ld, size %lld, bits %d, blksize %ld\n", 1265f0c9fd54SGeliang Tang inode->i_sb->s_id, inode->i_ino, (long long) inode->i_size, 1266f0c9fd54SGeliang Tang inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize); 1267f0c9fd54SGeliang Tang 1268f0c9fd54SGeliang Tang journal = journal_init_common(inode->i_sb->s_bdev, inode->i_sb->s_bdev, 1269f0c9fd54SGeliang Tang blocknr, inode->i_size >> inode->i_sb->s_blocksize_bits, 1270f0c9fd54SGeliang Tang inode->i_sb->s_blocksize); 1271470decc6SDave Kleikamp if (!journal) 1272470decc6SDave Kleikamp return NULL; 1273470decc6SDave Kleikamp 1274470decc6SDave Kleikamp journal->j_inode = inode; 127505496769STheodore Ts'o bdevname(journal->j_dev, journal->j_devname); 127681ae394bSRasmus Villemoes p = strreplace(journal->j_devname, '/', '!'); 127790576c0bSTheodore Ts'o sprintf(p, "-%lu", journal->j_inode->i_ino); 12788e85fb3fSJohann Lombardi jbd2_stats_proc_init(journal); 1279470decc6SDave Kleikamp 1280470decc6SDave Kleikamp return journal; 1281470decc6SDave Kleikamp } 1282470decc6SDave Kleikamp 1283470decc6SDave Kleikamp /* 1284470decc6SDave Kleikamp * If the journal init or create aborts, we need to mark the journal 1285470decc6SDave Kleikamp * superblock as being NULL to prevent the journal destroy from writing 1286470decc6SDave Kleikamp * back a bogus superblock. 1287470decc6SDave Kleikamp */ 1288470decc6SDave Kleikamp static void journal_fail_superblock (journal_t *journal) 1289470decc6SDave Kleikamp { 1290470decc6SDave Kleikamp struct buffer_head *bh = journal->j_sb_buffer; 1291470decc6SDave Kleikamp brelse(bh); 1292470decc6SDave Kleikamp journal->j_sb_buffer = NULL; 1293470decc6SDave Kleikamp } 1294470decc6SDave Kleikamp 1295470decc6SDave Kleikamp /* 1296470decc6SDave Kleikamp * Given a journal_t structure, initialise the various fields for 1297470decc6SDave Kleikamp * startup of a new journaling session. We use this both when creating 1298470decc6SDave Kleikamp * a journal, and after recovering an old journal to reset it for 1299470decc6SDave Kleikamp * subsequent use. 1300470decc6SDave Kleikamp */ 1301470decc6SDave Kleikamp 1302470decc6SDave Kleikamp static int journal_reset(journal_t *journal) 1303470decc6SDave Kleikamp { 1304470decc6SDave Kleikamp journal_superblock_t *sb = journal->j_superblock; 130518eba7aaSMingming Cao unsigned long long first, last; 1306470decc6SDave Kleikamp 1307470decc6SDave Kleikamp first = be32_to_cpu(sb->s_first); 1308470decc6SDave Kleikamp last = be32_to_cpu(sb->s_maxlen); 1309f6f50e28SJan Kara if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) { 1310f2a44523SEryu Guan printk(KERN_ERR "JBD2: Journal too short (blocks %llu-%llu).\n", 1311f6f50e28SJan Kara first, last); 1312f6f50e28SJan Kara journal_fail_superblock(journal); 1313f6f50e28SJan Kara return -EINVAL; 1314f6f50e28SJan Kara } 1315470decc6SDave Kleikamp 1316470decc6SDave Kleikamp journal->j_first = first; 1317470decc6SDave Kleikamp journal->j_last = last; 1318470decc6SDave Kleikamp 1319470decc6SDave Kleikamp journal->j_head = first; 1320470decc6SDave Kleikamp journal->j_tail = first; 1321470decc6SDave Kleikamp journal->j_free = last - first; 1322470decc6SDave Kleikamp 1323470decc6SDave Kleikamp journal->j_tail_sequence = journal->j_transaction_sequence; 1324470decc6SDave Kleikamp journal->j_commit_sequence = journal->j_transaction_sequence - 1; 1325470decc6SDave Kleikamp journal->j_commit_request = journal->j_commit_sequence; 1326470decc6SDave Kleikamp 1327470decc6SDave Kleikamp journal->j_max_transaction_buffers = journal->j_maxlen / 4; 1328470decc6SDave Kleikamp 1329470decc6SDave Kleikamp /* 1330470decc6SDave Kleikamp * As a special case, if the on-disk copy is already marked as needing 133124bcc89cSJan Kara * no recovery (s_start == 0), then we can safely defer the superblock 133224bcc89cSJan Kara * update until the next commit by setting JBD2_FLUSHED. This avoids 1333470decc6SDave Kleikamp * attempting a write to a potential-readonly device. 1334470decc6SDave Kleikamp */ 133524bcc89cSJan Kara if (sb->s_start == 0) { 1336f2a44523SEryu Guan jbd_debug(1, "JBD2: Skipping superblock update on recovered sb " 1337470decc6SDave Kleikamp "(start %ld, seq %d, errno %d)\n", 1338470decc6SDave Kleikamp journal->j_tail, journal->j_tail_sequence, 1339470decc6SDave Kleikamp journal->j_errno); 134024bcc89cSJan Kara journal->j_flags |= JBD2_FLUSHED; 134124bcc89cSJan Kara } else { 1342a78bb11dSJan Kara /* Lock here to make assertions happy... */ 13436fa7aa50STejun Heo mutex_lock_io(&journal->j_checkpoint_mutex); 134479feb521SJan Kara /* 134570fd7614SChristoph Hellwig * Update log tail information. We use REQ_FUA since new 134679feb521SJan Kara * transaction will start reusing journal space and so we 134779feb521SJan Kara * must make sure information about current log tail is on 134879feb521SJan Kara * disk before that. 134979feb521SJan Kara */ 135079feb521SJan Kara jbd2_journal_update_sb_log_tail(journal, 135179feb521SJan Kara journal->j_tail_sequence, 135279feb521SJan Kara journal->j_tail, 135317f423b5SJan Kara REQ_SYNC | REQ_FUA); 1354a78bb11dSJan Kara mutex_unlock(&journal->j_checkpoint_mutex); 135524bcc89cSJan Kara } 135624bcc89cSJan Kara return jbd2_journal_start_thread(journal); 1357470decc6SDave Kleikamp } 1358470decc6SDave Kleikamp 13592a222ca9SMike Christie static int jbd2_write_superblock(journal_t *journal, int write_flags) 136024bcc89cSJan Kara { 136124bcc89cSJan Kara struct buffer_head *bh = journal->j_sb_buffer; 1362fe52d17cSTheodore Ts'o journal_superblock_t *sb = journal->j_superblock; 136379feb521SJan Kara int ret; 136424bcc89cSJan Kara 13652a222ca9SMike Christie trace_jbd2_write_superblock(journal, write_flags); 136679feb521SJan Kara if (!(journal->j_flags & JBD2_BARRIER)) 136728a8f0d3SMike Christie write_flags &= ~(REQ_FUA | REQ_PREFLUSH); 136879feb521SJan Kara lock_buffer(bh); 1369914258bfSTheodore Ts'o if (buffer_write_io_error(bh)) { 1370914258bfSTheodore Ts'o /* 1371914258bfSTheodore Ts'o * Oh, dear. A previous attempt to write the journal 1372914258bfSTheodore Ts'o * superblock failed. This could happen because the 1373914258bfSTheodore Ts'o * USB device was yanked out. Or it could happen to 1374914258bfSTheodore Ts'o * be a transient write error and maybe the block will 1375914258bfSTheodore Ts'o * be remapped. Nothing we can do but to retry the 1376914258bfSTheodore Ts'o * write and hope for the best. 1377914258bfSTheodore Ts'o */ 1378914258bfSTheodore Ts'o printk(KERN_ERR "JBD2: previous I/O error detected " 1379914258bfSTheodore Ts'o "for journal superblock update for %s.\n", 1380914258bfSTheodore Ts'o journal->j_devname); 1381914258bfSTheodore Ts'o clear_buffer_write_io_error(bh); 1382914258bfSTheodore Ts'o set_buffer_uptodate(bh); 1383914258bfSTheodore Ts'o } 1384fe52d17cSTheodore Ts'o jbd2_superblock_csum_set(journal, sb); 138579feb521SJan Kara get_bh(bh); 138679feb521SJan Kara bh->b_end_io = end_buffer_write_sync; 13872a222ca9SMike Christie ret = submit_bh(REQ_OP_WRITE, write_flags, bh); 138879feb521SJan Kara wait_on_buffer(bh); 1389914258bfSTheodore Ts'o if (buffer_write_io_error(bh)) { 1390914258bfSTheodore Ts'o clear_buffer_write_io_error(bh); 1391914258bfSTheodore Ts'o set_buffer_uptodate(bh); 139279feb521SJan Kara ret = -EIO; 139379feb521SJan Kara } 139479feb521SJan Kara if (ret) { 139579feb521SJan Kara printk(KERN_ERR "JBD2: Error %d detected when updating " 139679feb521SJan Kara "journal superblock for %s.\n", ret, 139779feb521SJan Kara journal->j_devname); 13986f6a6fdaSJoseph Qi jbd2_journal_abort(journal, ret); 1399914258bfSTheodore Ts'o } 14006f6a6fdaSJoseph Qi 14016f6a6fdaSJoseph Qi return ret; 140224bcc89cSJan Kara } 1403470decc6SDave Kleikamp 140424bcc89cSJan Kara /** 140524bcc89cSJan Kara * jbd2_journal_update_sb_log_tail() - Update log tail in journal sb on disk. 140624bcc89cSJan Kara * @journal: The journal to update. 140779feb521SJan Kara * @tail_tid: TID of the new transaction at the tail of the log 140879feb521SJan Kara * @tail_block: The first block of the transaction at the tail of the log 140979feb521SJan Kara * @write_op: With which operation should we write the journal sb 141024bcc89cSJan Kara * 141124bcc89cSJan Kara * Update a journal's superblock information about log tail and write it to 141224bcc89cSJan Kara * disk, waiting for the IO to complete. 141324bcc89cSJan Kara */ 14146f6a6fdaSJoseph Qi int jbd2_journal_update_sb_log_tail(journal_t *journal, tid_t tail_tid, 141579feb521SJan Kara unsigned long tail_block, int write_op) 141624bcc89cSJan Kara { 141724bcc89cSJan Kara journal_superblock_t *sb = journal->j_superblock; 14186f6a6fdaSJoseph Qi int ret; 14192201c590SSeiji Aguchi 142085e0c4e8STheodore Ts'o if (is_journal_aborted(journal)) 142185e0c4e8STheodore Ts'o return -EIO; 142285e0c4e8STheodore Ts'o 1423a78bb11dSJan Kara BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex)); 142479feb521SJan Kara jbd_debug(1, "JBD2: updating superblock (start %lu, seq %u)\n", 142579feb521SJan Kara tail_block, tail_tid); 1426470decc6SDave Kleikamp 142779feb521SJan Kara sb->s_sequence = cpu_to_be32(tail_tid); 142879feb521SJan Kara sb->s_start = cpu_to_be32(tail_block); 142924bcc89cSJan Kara 14306f6a6fdaSJoseph Qi ret = jbd2_write_superblock(journal, write_op); 14316f6a6fdaSJoseph Qi if (ret) 14326f6a6fdaSJoseph Qi goto out; 143324bcc89cSJan Kara 143424bcc89cSJan Kara /* Log is no longer empty */ 1435a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 143624bcc89cSJan Kara WARN_ON(!sb->s_sequence); 1437f7f4bccbSMingming Cao journal->j_flags &= ~JBD2_FLUSHED; 143824bcc89cSJan Kara write_unlock(&journal->j_state_lock); 14396f6a6fdaSJoseph Qi 14406f6a6fdaSJoseph Qi out: 14416f6a6fdaSJoseph Qi return ret; 144224bcc89cSJan Kara } 144324bcc89cSJan Kara 144424bcc89cSJan Kara /** 144524bcc89cSJan Kara * jbd2_mark_journal_empty() - Mark on disk journal as empty. 144624bcc89cSJan Kara * @journal: The journal to update. 1447c0a2ad9bSOGAWA Hirofumi * @write_op: With which operation should we write the journal sb 144824bcc89cSJan Kara * 144924bcc89cSJan Kara * Update a journal's dynamic superblock fields to show that journal is empty. 145024bcc89cSJan Kara * Write updated superblock to disk waiting for IO to complete. 145124bcc89cSJan Kara */ 1452c0a2ad9bSOGAWA Hirofumi static void jbd2_mark_journal_empty(journal_t *journal, int write_op) 145324bcc89cSJan Kara { 145424bcc89cSJan Kara journal_superblock_t *sb = journal->j_superblock; 145524bcc89cSJan Kara 1456a78bb11dSJan Kara BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex)); 1457470decc6SDave Kleikamp read_lock(&journal->j_state_lock); 1458eeecef0aSEric Sandeen /* Is it already empty? */ 1459eeecef0aSEric Sandeen if (sb->s_start == 0) { 1460eeecef0aSEric Sandeen read_unlock(&journal->j_state_lock); 1461eeecef0aSEric Sandeen return; 1462eeecef0aSEric Sandeen } 146324bcc89cSJan Kara jbd_debug(1, "JBD2: Marking journal as empty (seq %d)\n", 146424bcc89cSJan Kara journal->j_tail_sequence); 1465470decc6SDave Kleikamp 1466470decc6SDave Kleikamp sb->s_sequence = cpu_to_be32(journal->j_tail_sequence); 146724bcc89cSJan Kara sb->s_start = cpu_to_be32(0); 146824bcc89cSJan Kara read_unlock(&journal->j_state_lock); 146924bcc89cSJan Kara 1470c0a2ad9bSOGAWA Hirofumi jbd2_write_superblock(journal, write_op); 147124bcc89cSJan Kara 147224bcc89cSJan Kara /* Log is no longer empty */ 147324bcc89cSJan Kara write_lock(&journal->j_state_lock); 1474f7f4bccbSMingming Cao journal->j_flags |= JBD2_FLUSHED; 1475a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 1476470decc6SDave Kleikamp } 1477470decc6SDave Kleikamp 147824bcc89cSJan Kara 147924bcc89cSJan Kara /** 148024bcc89cSJan Kara * jbd2_journal_update_sb_errno() - Update error in the journal. 148124bcc89cSJan Kara * @journal: The journal to update. 148224bcc89cSJan Kara * 148324bcc89cSJan Kara * Update a journal's errno. Write updated superblock to disk waiting for IO 148424bcc89cSJan Kara * to complete. 148524bcc89cSJan Kara */ 1486d796c52eSTheodore Ts'o void jbd2_journal_update_sb_errno(journal_t *journal) 148724bcc89cSJan Kara { 148824bcc89cSJan Kara journal_superblock_t *sb = journal->j_superblock; 1489fb7c0244STheodore Ts'o int errcode; 149024bcc89cSJan Kara 149124bcc89cSJan Kara read_lock(&journal->j_state_lock); 1492fb7c0244STheodore Ts'o errcode = journal->j_errno; 1493470decc6SDave Kleikamp read_unlock(&journal->j_state_lock); 1494fb7c0244STheodore Ts'o if (errcode == -ESHUTDOWN) 1495fb7c0244STheodore Ts'o errcode = 0; 1496fb7c0244STheodore Ts'o jbd_debug(1, "JBD2: updating superblock error (errno %d)\n", errcode); 1497fb7c0244STheodore Ts'o sb->s_errno = cpu_to_be32(errcode); 1498470decc6SDave Kleikamp 149917f423b5SJan Kara jbd2_write_superblock(journal, REQ_SYNC | REQ_FUA); 1500470decc6SDave Kleikamp } 1501d796c52eSTheodore Ts'o EXPORT_SYMBOL(jbd2_journal_update_sb_errno); 1502470decc6SDave Kleikamp 1503470decc6SDave Kleikamp /* 1504470decc6SDave Kleikamp * Read the superblock for a given journal, performing initial 1505470decc6SDave Kleikamp * validation of the format. 1506470decc6SDave Kleikamp */ 1507470decc6SDave Kleikamp static int journal_get_superblock(journal_t *journal) 1508470decc6SDave Kleikamp { 1509470decc6SDave Kleikamp struct buffer_head *bh; 1510470decc6SDave Kleikamp journal_superblock_t *sb; 1511470decc6SDave Kleikamp int err = -EIO; 1512470decc6SDave Kleikamp 1513470decc6SDave Kleikamp bh = journal->j_sb_buffer; 1514470decc6SDave Kleikamp 1515470decc6SDave Kleikamp J_ASSERT(bh != NULL); 1516470decc6SDave Kleikamp if (!buffer_uptodate(bh)) { 1517dfec8a14SMike Christie ll_rw_block(REQ_OP_READ, 0, 1, &bh); 1518470decc6SDave Kleikamp wait_on_buffer(bh); 1519470decc6SDave Kleikamp if (!buffer_uptodate(bh)) { 1520470decc6SDave Kleikamp printk(KERN_ERR 1521f2a44523SEryu Guan "JBD2: IO error reading journal superblock\n"); 1522470decc6SDave Kleikamp goto out; 1523470decc6SDave Kleikamp } 1524470decc6SDave Kleikamp } 1525470decc6SDave Kleikamp 152625ed6e8aSDarrick J. Wong if (buffer_verified(bh)) 152725ed6e8aSDarrick J. Wong return 0; 152825ed6e8aSDarrick J. Wong 1529470decc6SDave Kleikamp sb = journal->j_superblock; 1530470decc6SDave Kleikamp 1531470decc6SDave Kleikamp err = -EINVAL; 1532470decc6SDave Kleikamp 1533f7f4bccbSMingming Cao if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) || 1534470decc6SDave Kleikamp sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) { 1535f2a44523SEryu Guan printk(KERN_WARNING "JBD2: no valid journal superblock found\n"); 1536470decc6SDave Kleikamp goto out; 1537470decc6SDave Kleikamp } 1538470decc6SDave Kleikamp 1539470decc6SDave Kleikamp switch(be32_to_cpu(sb->s_header.h_blocktype)) { 1540f7f4bccbSMingming Cao case JBD2_SUPERBLOCK_V1: 1541470decc6SDave Kleikamp journal->j_format_version = 1; 1542470decc6SDave Kleikamp break; 1543f7f4bccbSMingming Cao case JBD2_SUPERBLOCK_V2: 1544470decc6SDave Kleikamp journal->j_format_version = 2; 1545470decc6SDave Kleikamp break; 1546470decc6SDave Kleikamp default: 1547f2a44523SEryu Guan printk(KERN_WARNING "JBD2: unrecognised superblock format ID\n"); 1548470decc6SDave Kleikamp goto out; 1549470decc6SDave Kleikamp } 1550470decc6SDave Kleikamp 1551470decc6SDave Kleikamp if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen) 1552470decc6SDave Kleikamp journal->j_maxlen = be32_to_cpu(sb->s_maxlen); 1553470decc6SDave Kleikamp else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) { 1554f2a44523SEryu Guan printk(KERN_WARNING "JBD2: journal file too short\n"); 1555470decc6SDave Kleikamp goto out; 1556470decc6SDave Kleikamp } 1557470decc6SDave Kleikamp 15588762202dSEryu Guan if (be32_to_cpu(sb->s_first) == 0 || 15598762202dSEryu Guan be32_to_cpu(sb->s_first) >= journal->j_maxlen) { 15608762202dSEryu Guan printk(KERN_WARNING 15618762202dSEryu Guan "JBD2: Invalid start block of journal: %u\n", 15628762202dSEryu Guan be32_to_cpu(sb->s_first)); 15638762202dSEryu Guan goto out; 15648762202dSEryu Guan } 15658762202dSEryu Guan 156656316a0dSDarrick J. Wong if (jbd2_has_feature_csum2(journal) && 156756316a0dSDarrick J. Wong jbd2_has_feature_csum3(journal)) { 1568db9ee220SDarrick J. Wong /* Can't have checksum v2 and v3 at the same time! */ 1569db9ee220SDarrick J. Wong printk(KERN_ERR "JBD2: Can't enable checksumming v2 and v3 " 1570db9ee220SDarrick J. Wong "at the same time!\n"); 1571db9ee220SDarrick J. Wong goto out; 1572db9ee220SDarrick J. Wong } 1573db9ee220SDarrick J. Wong 15748595798cSDarrick J. Wong if (jbd2_journal_has_csum_v2or3_feature(journal) && 157556316a0dSDarrick J. Wong jbd2_has_feature_checksum(journal)) { 1576feb8c6d3SDarrick J. Wong /* Can't have checksum v1 and v2 on at the same time! */ 1577feb8c6d3SDarrick J. Wong printk(KERN_ERR "JBD2: Can't enable checksumming v1 and v2/3 " 1578feb8c6d3SDarrick J. Wong "at the same time!\n"); 1579feb8c6d3SDarrick J. Wong goto out; 1580feb8c6d3SDarrick J. Wong } 1581feb8c6d3SDarrick J. Wong 158225ed6e8aSDarrick J. Wong if (!jbd2_verify_csum_type(journal, sb)) { 1583a67c848aSDmitry Monakhov printk(KERN_ERR "JBD2: Unknown checksum type\n"); 158425ed6e8aSDarrick J. Wong goto out; 158525ed6e8aSDarrick J. Wong } 158625ed6e8aSDarrick J. Wong 158701b5adceSDarrick J. Wong /* Load the checksum driver */ 15888595798cSDarrick J. Wong if (jbd2_journal_has_csum_v2or3_feature(journal)) { 158901b5adceSDarrick J. Wong journal->j_chksum_driver = crypto_alloc_shash("crc32c", 0, 0); 159001b5adceSDarrick J. Wong if (IS_ERR(journal->j_chksum_driver)) { 1591a67c848aSDmitry Monakhov printk(KERN_ERR "JBD2: Cannot load crc32c driver.\n"); 159201b5adceSDarrick J. Wong err = PTR_ERR(journal->j_chksum_driver); 159301b5adceSDarrick J. Wong journal->j_chksum_driver = NULL; 159401b5adceSDarrick J. Wong goto out; 159501b5adceSDarrick J. Wong } 159601b5adceSDarrick J. Wong } 159701b5adceSDarrick J. Wong 15984fd5ea43SDarrick J. Wong /* Check superblock checksum */ 15994fd5ea43SDarrick J. Wong if (!jbd2_superblock_csum_verify(journal, sb)) { 1600a67c848aSDmitry Monakhov printk(KERN_ERR "JBD2: journal checksum error\n"); 16016a797d27SDarrick J. Wong err = -EFSBADCRC; 16024fd5ea43SDarrick J. Wong goto out; 16034fd5ea43SDarrick J. Wong } 16044fd5ea43SDarrick J. Wong 16054fd5ea43SDarrick J. Wong /* Precompute checksum seed for all metadata */ 1606db9ee220SDarrick J. Wong if (jbd2_journal_has_csum_v2or3(journal)) 16074fd5ea43SDarrick J. Wong journal->j_csum_seed = jbd2_chksum(journal, ~0, sb->s_uuid, 16084fd5ea43SDarrick J. Wong sizeof(sb->s_uuid)); 16094fd5ea43SDarrick J. Wong 161025ed6e8aSDarrick J. Wong set_buffer_verified(bh); 161125ed6e8aSDarrick J. Wong 1612470decc6SDave Kleikamp return 0; 1613470decc6SDave Kleikamp 1614470decc6SDave Kleikamp out: 1615470decc6SDave Kleikamp journal_fail_superblock(journal); 1616470decc6SDave Kleikamp return err; 1617470decc6SDave Kleikamp } 1618470decc6SDave Kleikamp 1619470decc6SDave Kleikamp /* 1620470decc6SDave Kleikamp * Load the on-disk journal superblock and read the key fields into the 1621470decc6SDave Kleikamp * journal_t. 1622470decc6SDave Kleikamp */ 1623470decc6SDave Kleikamp 1624470decc6SDave Kleikamp static int load_superblock(journal_t *journal) 1625470decc6SDave Kleikamp { 1626470decc6SDave Kleikamp int err; 1627470decc6SDave Kleikamp journal_superblock_t *sb; 1628470decc6SDave Kleikamp 1629470decc6SDave Kleikamp err = journal_get_superblock(journal); 1630470decc6SDave Kleikamp if (err) 1631470decc6SDave Kleikamp return err; 1632470decc6SDave Kleikamp 1633470decc6SDave Kleikamp sb = journal->j_superblock; 1634470decc6SDave Kleikamp 1635470decc6SDave Kleikamp journal->j_tail_sequence = be32_to_cpu(sb->s_sequence); 1636470decc6SDave Kleikamp journal->j_tail = be32_to_cpu(sb->s_start); 1637470decc6SDave Kleikamp journal->j_first = be32_to_cpu(sb->s_first); 1638470decc6SDave Kleikamp journal->j_last = be32_to_cpu(sb->s_maxlen); 1639470decc6SDave Kleikamp journal->j_errno = be32_to_cpu(sb->s_errno); 1640470decc6SDave Kleikamp 1641470decc6SDave Kleikamp return 0; 1642470decc6SDave Kleikamp } 1643470decc6SDave Kleikamp 1644470decc6SDave Kleikamp 1645470decc6SDave Kleikamp /** 1646f7f4bccbSMingming Cao * int jbd2_journal_load() - Read journal from disk. 1647470decc6SDave Kleikamp * @journal: Journal to act on. 1648470decc6SDave Kleikamp * 1649470decc6SDave Kleikamp * Given a journal_t structure which tells us which disk blocks contain 1650470decc6SDave Kleikamp * a journal, read the journal from disk to initialise the in-memory 1651470decc6SDave Kleikamp * structures. 1652470decc6SDave Kleikamp */ 1653f7f4bccbSMingming Cao int jbd2_journal_load(journal_t *journal) 1654470decc6SDave Kleikamp { 1655470decc6SDave Kleikamp int err; 1656470decc6SDave Kleikamp journal_superblock_t *sb; 1657470decc6SDave Kleikamp 1658470decc6SDave Kleikamp err = load_superblock(journal); 1659470decc6SDave Kleikamp if (err) 1660470decc6SDave Kleikamp return err; 1661470decc6SDave Kleikamp 1662470decc6SDave Kleikamp sb = journal->j_superblock; 1663470decc6SDave Kleikamp /* If this is a V2 superblock, then we have to check the 1664470decc6SDave Kleikamp * features flags on it. */ 1665470decc6SDave Kleikamp 1666470decc6SDave Kleikamp if (journal->j_format_version >= 2) { 1667470decc6SDave Kleikamp if ((sb->s_feature_ro_compat & 1668f7f4bccbSMingming Cao ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) || 1669470decc6SDave Kleikamp (sb->s_feature_incompat & 1670f7f4bccbSMingming Cao ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) { 1671470decc6SDave Kleikamp printk(KERN_WARNING 1672f2a44523SEryu Guan "JBD2: Unrecognised features on journal\n"); 1673470decc6SDave Kleikamp return -EINVAL; 1674470decc6SDave Kleikamp } 1675470decc6SDave Kleikamp } 1676470decc6SDave Kleikamp 1677d2eecb03STheodore Ts'o /* 1678d2eecb03STheodore Ts'o * Create a slab for this blocksize 1679d2eecb03STheodore Ts'o */ 1680d2eecb03STheodore Ts'o err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize)); 1681d2eecb03STheodore Ts'o if (err) 1682d2eecb03STheodore Ts'o return err; 1683d2eecb03STheodore Ts'o 1684470decc6SDave Kleikamp /* Let the recovery code check whether it needs to recover any 1685470decc6SDave Kleikamp * data from the journal. */ 1686f7f4bccbSMingming Cao if (jbd2_journal_recover(journal)) 1687470decc6SDave Kleikamp goto recovery_error; 1688470decc6SDave Kleikamp 1689e6a47428STheodore Ts'o if (journal->j_failed_commit) { 1690e6a47428STheodore Ts'o printk(KERN_ERR "JBD2: journal transaction %u on %s " 1691e6a47428STheodore Ts'o "is corrupt.\n", journal->j_failed_commit, 1692e6a47428STheodore Ts'o journal->j_devname); 16936a797d27SDarrick J. Wong return -EFSCORRUPTED; 1694e6a47428STheodore Ts'o } 1695e6a47428STheodore Ts'o 1696470decc6SDave Kleikamp /* OK, we've finished with the dynamic journal bits: 1697470decc6SDave Kleikamp * reinitialise the dynamic contents of the superblock in memory 1698470decc6SDave Kleikamp * and reset them on disk. */ 1699470decc6SDave Kleikamp if (journal_reset(journal)) 1700470decc6SDave Kleikamp goto recovery_error; 1701470decc6SDave Kleikamp 1702f7f4bccbSMingming Cao journal->j_flags &= ~JBD2_ABORT; 1703f7f4bccbSMingming Cao journal->j_flags |= JBD2_LOADED; 1704470decc6SDave Kleikamp return 0; 1705470decc6SDave Kleikamp 1706470decc6SDave Kleikamp recovery_error: 1707f2a44523SEryu Guan printk(KERN_WARNING "JBD2: recovery failed\n"); 1708470decc6SDave Kleikamp return -EIO; 1709470decc6SDave Kleikamp } 1710470decc6SDave Kleikamp 1711470decc6SDave Kleikamp /** 1712f7f4bccbSMingming Cao * void jbd2_journal_destroy() - Release a journal_t structure. 1713470decc6SDave Kleikamp * @journal: Journal to act on. 1714470decc6SDave Kleikamp * 1715470decc6SDave Kleikamp * Release a journal_t structure once it is no longer in use by the 1716470decc6SDave Kleikamp * journaled object. 171744519fafSHidehiro Kawai * Return <0 if we couldn't clean up the journal. 1718470decc6SDave Kleikamp */ 171944519fafSHidehiro Kawai int jbd2_journal_destroy(journal_t *journal) 1720470decc6SDave Kleikamp { 172144519fafSHidehiro Kawai int err = 0; 172244519fafSHidehiro Kawai 1723470decc6SDave Kleikamp /* Wait for the commit thread to wake up and die. */ 1724470decc6SDave Kleikamp journal_kill_thread(journal); 1725470decc6SDave Kleikamp 1726470decc6SDave Kleikamp /* Force a final log commit */ 1727470decc6SDave Kleikamp if (journal->j_running_transaction) 1728f7f4bccbSMingming Cao jbd2_journal_commit_transaction(journal); 1729470decc6SDave Kleikamp 1730470decc6SDave Kleikamp /* Force any old transactions to disk */ 1731470decc6SDave Kleikamp 1732470decc6SDave Kleikamp /* Totally anal locking here... */ 1733470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 1734470decc6SDave Kleikamp while (journal->j_checkpoint_transactions != NULL) { 1735470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 17366fa7aa50STejun Heo mutex_lock_io(&journal->j_checkpoint_mutex); 1737841df7dfSJan Kara err = jbd2_log_do_checkpoint(journal); 17381a0d3786STheodore Ts'o mutex_unlock(&journal->j_checkpoint_mutex); 1739841df7dfSJan Kara /* 1740841df7dfSJan Kara * If checkpointing failed, just free the buffers to avoid 1741841df7dfSJan Kara * looping forever 1742841df7dfSJan Kara */ 1743841df7dfSJan Kara if (err) { 1744841df7dfSJan Kara jbd2_journal_destroy_checkpoint(journal); 1745841df7dfSJan Kara spin_lock(&journal->j_list_lock); 1746841df7dfSJan Kara break; 1747841df7dfSJan Kara } 1748470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 1749470decc6SDave Kleikamp } 1750470decc6SDave Kleikamp 1751470decc6SDave Kleikamp J_ASSERT(journal->j_running_transaction == NULL); 1752470decc6SDave Kleikamp J_ASSERT(journal->j_committing_transaction == NULL); 1753470decc6SDave Kleikamp J_ASSERT(journal->j_checkpoint_transactions == NULL); 1754470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 1755470decc6SDave Kleikamp 175644519fafSHidehiro Kawai if (journal->j_sb_buffer) { 175744519fafSHidehiro Kawai if (!is_journal_aborted(journal)) { 17586fa7aa50STejun Heo mutex_lock_io(&journal->j_checkpoint_mutex); 1759c0a2ad9bSOGAWA Hirofumi 1760c0a2ad9bSOGAWA Hirofumi write_lock(&journal->j_state_lock); 1761c0a2ad9bSOGAWA Hirofumi journal->j_tail_sequence = 1762c0a2ad9bSOGAWA Hirofumi ++journal->j_transaction_sequence; 1763c0a2ad9bSOGAWA Hirofumi write_unlock(&journal->j_state_lock); 1764c0a2ad9bSOGAWA Hirofumi 176570fd7614SChristoph Hellwig jbd2_mark_journal_empty(journal, 176617f423b5SJan Kara REQ_SYNC | REQ_PREFLUSH | REQ_FUA); 1767a78bb11dSJan Kara mutex_unlock(&journal->j_checkpoint_mutex); 1768a78bb11dSJan Kara } else 176944519fafSHidehiro Kawai err = -EIO; 1770470decc6SDave Kleikamp brelse(journal->j_sb_buffer); 1771470decc6SDave Kleikamp } 1772470decc6SDave Kleikamp 17738e85fb3fSJohann Lombardi if (journal->j_proc_entry) 17748e85fb3fSJohann Lombardi jbd2_stats_proc_exit(journal); 1775470decc6SDave Kleikamp iput(journal->j_inode); 1776470decc6SDave Kleikamp if (journal->j_revoke) 1777f7f4bccbSMingming Cao jbd2_journal_destroy_revoke(journal); 177801b5adceSDarrick J. Wong if (journal->j_chksum_driver) 177901b5adceSDarrick J. Wong crypto_free_shash(journal->j_chksum_driver); 1780470decc6SDave Kleikamp kfree(journal->j_wbuf); 1781470decc6SDave Kleikamp kfree(journal); 178244519fafSHidehiro Kawai 178344519fafSHidehiro Kawai return err; 1784470decc6SDave Kleikamp } 1785470decc6SDave Kleikamp 1786470decc6SDave Kleikamp 1787470decc6SDave Kleikamp /** 1788f7f4bccbSMingming Cao *int jbd2_journal_check_used_features () - Check if features specified are used. 1789470decc6SDave Kleikamp * @journal: Journal to check. 1790470decc6SDave Kleikamp * @compat: bitmask of compatible features 1791470decc6SDave Kleikamp * @ro: bitmask of features that force read-only mount 1792470decc6SDave Kleikamp * @incompat: bitmask of incompatible features 1793470decc6SDave Kleikamp * 1794470decc6SDave Kleikamp * Check whether the journal uses all of a given set of 1795470decc6SDave Kleikamp * features. Return true (non-zero) if it does. 1796470decc6SDave Kleikamp **/ 1797470decc6SDave Kleikamp 1798f7f4bccbSMingming Cao int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat, 1799470decc6SDave Kleikamp unsigned long ro, unsigned long incompat) 1800470decc6SDave Kleikamp { 1801470decc6SDave Kleikamp journal_superblock_t *sb; 1802470decc6SDave Kleikamp 1803470decc6SDave Kleikamp if (!compat && !ro && !incompat) 1804470decc6SDave Kleikamp return 1; 18051113e1b5SPatrick J. LoPresti /* Load journal superblock if it is not loaded yet. */ 18061113e1b5SPatrick J. LoPresti if (journal->j_format_version == 0 && 18071113e1b5SPatrick J. LoPresti journal_get_superblock(journal) != 0) 18081113e1b5SPatrick J. LoPresti return 0; 1809470decc6SDave Kleikamp if (journal->j_format_version == 1) 1810470decc6SDave Kleikamp return 0; 1811470decc6SDave Kleikamp 1812470decc6SDave Kleikamp sb = journal->j_superblock; 1813470decc6SDave Kleikamp 1814470decc6SDave Kleikamp if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) && 1815470decc6SDave Kleikamp ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) && 1816470decc6SDave Kleikamp ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat)) 1817470decc6SDave Kleikamp return 1; 1818470decc6SDave Kleikamp 1819470decc6SDave Kleikamp return 0; 1820470decc6SDave Kleikamp } 1821470decc6SDave Kleikamp 1822470decc6SDave Kleikamp /** 1823f7f4bccbSMingming Cao * int jbd2_journal_check_available_features() - Check feature set in journalling layer 1824470decc6SDave Kleikamp * @journal: Journal to check. 1825470decc6SDave Kleikamp * @compat: bitmask of compatible features 1826470decc6SDave Kleikamp * @ro: bitmask of features that force read-only mount 1827470decc6SDave Kleikamp * @incompat: bitmask of incompatible features 1828470decc6SDave Kleikamp * 1829470decc6SDave Kleikamp * Check whether the journaling code supports the use of 1830470decc6SDave Kleikamp * all of a given set of features on this journal. Return true 1831470decc6SDave Kleikamp * (non-zero) if it can. */ 1832470decc6SDave Kleikamp 1833f7f4bccbSMingming Cao int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat, 1834470decc6SDave Kleikamp unsigned long ro, unsigned long incompat) 1835470decc6SDave Kleikamp { 1836470decc6SDave Kleikamp if (!compat && !ro && !incompat) 1837470decc6SDave Kleikamp return 1; 1838470decc6SDave Kleikamp 1839470decc6SDave Kleikamp /* We can support any known requested features iff the 1840470decc6SDave Kleikamp * superblock is in version 2. Otherwise we fail to support any 1841470decc6SDave Kleikamp * extended sb features. */ 1842470decc6SDave Kleikamp 1843470decc6SDave Kleikamp if (journal->j_format_version != 2) 1844470decc6SDave Kleikamp return 0; 1845470decc6SDave Kleikamp 1846f7f4bccbSMingming Cao if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat && 1847f7f4bccbSMingming Cao (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro && 1848f7f4bccbSMingming Cao (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat) 1849470decc6SDave Kleikamp return 1; 1850470decc6SDave Kleikamp 1851470decc6SDave Kleikamp return 0; 1852470decc6SDave Kleikamp } 1853470decc6SDave Kleikamp 1854470decc6SDave Kleikamp /** 1855f7f4bccbSMingming Cao * int jbd2_journal_set_features () - Mark a given journal feature in the superblock 1856470decc6SDave Kleikamp * @journal: Journal to act on. 1857470decc6SDave Kleikamp * @compat: bitmask of compatible features 1858470decc6SDave Kleikamp * @ro: bitmask of features that force read-only mount 1859470decc6SDave Kleikamp * @incompat: bitmask of incompatible features 1860470decc6SDave Kleikamp * 1861470decc6SDave Kleikamp * Mark a given journal feature as present on the 1862470decc6SDave Kleikamp * superblock. Returns true if the requested features could be set. 1863470decc6SDave Kleikamp * 1864470decc6SDave Kleikamp */ 1865470decc6SDave Kleikamp 1866f7f4bccbSMingming Cao int jbd2_journal_set_features (journal_t *journal, unsigned long compat, 1867470decc6SDave Kleikamp unsigned long ro, unsigned long incompat) 1868470decc6SDave Kleikamp { 186925ed6e8aSDarrick J. Wong #define INCOMPAT_FEATURE_ON(f) \ 187025ed6e8aSDarrick J. Wong ((incompat & (f)) && !(sb->s_feature_incompat & cpu_to_be32(f))) 187125ed6e8aSDarrick J. Wong #define COMPAT_FEATURE_ON(f) \ 187225ed6e8aSDarrick J. Wong ((compat & (f)) && !(sb->s_feature_compat & cpu_to_be32(f))) 1873470decc6SDave Kleikamp journal_superblock_t *sb; 1874470decc6SDave Kleikamp 1875f7f4bccbSMingming Cao if (jbd2_journal_check_used_features(journal, compat, ro, incompat)) 1876470decc6SDave Kleikamp return 1; 1877470decc6SDave Kleikamp 1878f7f4bccbSMingming Cao if (!jbd2_journal_check_available_features(journal, compat, ro, incompat)) 1879470decc6SDave Kleikamp return 0; 1880470decc6SDave Kleikamp 1881db9ee220SDarrick J. Wong /* If enabling v2 checksums, turn on v3 instead */ 1882db9ee220SDarrick J. Wong if (incompat & JBD2_FEATURE_INCOMPAT_CSUM_V2) { 1883db9ee220SDarrick J. Wong incompat &= ~JBD2_FEATURE_INCOMPAT_CSUM_V2; 1884db9ee220SDarrick J. Wong incompat |= JBD2_FEATURE_INCOMPAT_CSUM_V3; 1885db9ee220SDarrick J. Wong } 1886db9ee220SDarrick J. Wong 1887db9ee220SDarrick J. Wong /* Asking for checksumming v3 and v1? Only give them v3. */ 1888db9ee220SDarrick J. Wong if (incompat & JBD2_FEATURE_INCOMPAT_CSUM_V3 && 188925ed6e8aSDarrick J. Wong compat & JBD2_FEATURE_COMPAT_CHECKSUM) 189025ed6e8aSDarrick J. Wong compat &= ~JBD2_FEATURE_COMPAT_CHECKSUM; 189125ed6e8aSDarrick J. Wong 1892470decc6SDave Kleikamp jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n", 1893470decc6SDave Kleikamp compat, ro, incompat); 1894470decc6SDave Kleikamp 1895470decc6SDave Kleikamp sb = journal->j_superblock; 1896470decc6SDave Kleikamp 1897db9ee220SDarrick J. Wong /* If enabling v3 checksums, update superblock */ 1898db9ee220SDarrick J. Wong if (INCOMPAT_FEATURE_ON(JBD2_FEATURE_INCOMPAT_CSUM_V3)) { 189925ed6e8aSDarrick J. Wong sb->s_checksum_type = JBD2_CRC32C_CHKSUM; 190025ed6e8aSDarrick J. Wong sb->s_feature_compat &= 190125ed6e8aSDarrick J. Wong ~cpu_to_be32(JBD2_FEATURE_COMPAT_CHECKSUM); 190201b5adceSDarrick J. Wong 190301b5adceSDarrick J. Wong /* Load the checksum driver */ 190401b5adceSDarrick J. Wong if (journal->j_chksum_driver == NULL) { 190501b5adceSDarrick J. Wong journal->j_chksum_driver = crypto_alloc_shash("crc32c", 190601b5adceSDarrick J. Wong 0, 0); 190701b5adceSDarrick J. Wong if (IS_ERR(journal->j_chksum_driver)) { 1908a67c848aSDmitry Monakhov printk(KERN_ERR "JBD2: Cannot load crc32c " 190901b5adceSDarrick J. Wong "driver.\n"); 191001b5adceSDarrick J. Wong journal->j_chksum_driver = NULL; 191101b5adceSDarrick J. Wong return 0; 191201b5adceSDarrick J. Wong } 19134fd5ea43SDarrick J. Wong 19144fd5ea43SDarrick J. Wong /* Precompute checksum seed for all metadata */ 19154fd5ea43SDarrick J. Wong journal->j_csum_seed = jbd2_chksum(journal, ~0, 19164fd5ea43SDarrick J. Wong sb->s_uuid, 19174fd5ea43SDarrick J. Wong sizeof(sb->s_uuid)); 191825ed6e8aSDarrick J. Wong } 191932f38691SDarrick J. Wong } 192025ed6e8aSDarrick J. Wong 192125ed6e8aSDarrick J. Wong /* If enabling v1 checksums, downgrade superblock */ 192225ed6e8aSDarrick J. Wong if (COMPAT_FEATURE_ON(JBD2_FEATURE_COMPAT_CHECKSUM)) 192325ed6e8aSDarrick J. Wong sb->s_feature_incompat &= 1924db9ee220SDarrick J. Wong ~cpu_to_be32(JBD2_FEATURE_INCOMPAT_CSUM_V2 | 1925db9ee220SDarrick J. Wong JBD2_FEATURE_INCOMPAT_CSUM_V3); 192625ed6e8aSDarrick J. Wong 1927470decc6SDave Kleikamp sb->s_feature_compat |= cpu_to_be32(compat); 1928470decc6SDave Kleikamp sb->s_feature_ro_compat |= cpu_to_be32(ro); 1929470decc6SDave Kleikamp sb->s_feature_incompat |= cpu_to_be32(incompat); 1930470decc6SDave Kleikamp 1931470decc6SDave Kleikamp return 1; 193225ed6e8aSDarrick J. Wong #undef COMPAT_FEATURE_ON 193325ed6e8aSDarrick J. Wong #undef INCOMPAT_FEATURE_ON 1934470decc6SDave Kleikamp } 1935470decc6SDave Kleikamp 1936818d276cSGirish Shilamkar /* 1937818d276cSGirish Shilamkar * jbd2_journal_clear_features () - Clear a given journal feature in the 1938818d276cSGirish Shilamkar * superblock 1939818d276cSGirish Shilamkar * @journal: Journal to act on. 1940818d276cSGirish Shilamkar * @compat: bitmask of compatible features 1941818d276cSGirish Shilamkar * @ro: bitmask of features that force read-only mount 1942818d276cSGirish Shilamkar * @incompat: bitmask of incompatible features 1943818d276cSGirish Shilamkar * 1944818d276cSGirish Shilamkar * Clear a given journal feature as present on the 1945818d276cSGirish Shilamkar * superblock. 1946818d276cSGirish Shilamkar */ 1947818d276cSGirish Shilamkar void jbd2_journal_clear_features(journal_t *journal, unsigned long compat, 1948818d276cSGirish Shilamkar unsigned long ro, unsigned long incompat) 1949818d276cSGirish Shilamkar { 1950818d276cSGirish Shilamkar journal_superblock_t *sb; 1951818d276cSGirish Shilamkar 1952818d276cSGirish Shilamkar jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n", 1953818d276cSGirish Shilamkar compat, ro, incompat); 1954818d276cSGirish Shilamkar 1955818d276cSGirish Shilamkar sb = journal->j_superblock; 1956818d276cSGirish Shilamkar 1957818d276cSGirish Shilamkar sb->s_feature_compat &= ~cpu_to_be32(compat); 1958818d276cSGirish Shilamkar sb->s_feature_ro_compat &= ~cpu_to_be32(ro); 1959818d276cSGirish Shilamkar sb->s_feature_incompat &= ~cpu_to_be32(incompat); 1960818d276cSGirish Shilamkar } 1961818d276cSGirish Shilamkar EXPORT_SYMBOL(jbd2_journal_clear_features); 1962470decc6SDave Kleikamp 1963470decc6SDave Kleikamp /** 1964f7f4bccbSMingming Cao * int jbd2_journal_flush () - Flush journal 1965470decc6SDave Kleikamp * @journal: Journal to act on. 1966470decc6SDave Kleikamp * 1967470decc6SDave Kleikamp * Flush all data for a given journal to disk and empty the journal. 1968470decc6SDave Kleikamp * Filesystems can use this when remounting readonly to ensure that 1969470decc6SDave Kleikamp * recovery does not need to happen on remount. 1970470decc6SDave Kleikamp */ 1971470decc6SDave Kleikamp 1972f7f4bccbSMingming Cao int jbd2_journal_flush(journal_t *journal) 1973470decc6SDave Kleikamp { 1974470decc6SDave Kleikamp int err = 0; 1975470decc6SDave Kleikamp transaction_t *transaction = NULL; 1976470decc6SDave Kleikamp 1977a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 1978470decc6SDave Kleikamp 1979470decc6SDave Kleikamp /* Force everything buffered to the log... */ 1980470decc6SDave Kleikamp if (journal->j_running_transaction) { 1981470decc6SDave Kleikamp transaction = journal->j_running_transaction; 1982f7f4bccbSMingming Cao __jbd2_log_start_commit(journal, transaction->t_tid); 1983470decc6SDave Kleikamp } else if (journal->j_committing_transaction) 1984470decc6SDave Kleikamp transaction = journal->j_committing_transaction; 1985470decc6SDave Kleikamp 1986470decc6SDave Kleikamp /* Wait for the log commit to complete... */ 1987470decc6SDave Kleikamp if (transaction) { 1988470decc6SDave Kleikamp tid_t tid = transaction->t_tid; 1989470decc6SDave Kleikamp 1990a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 1991f7f4bccbSMingming Cao jbd2_log_wait_commit(journal, tid); 1992470decc6SDave Kleikamp } else { 1993a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 1994470decc6SDave Kleikamp } 1995470decc6SDave Kleikamp 1996470decc6SDave Kleikamp /* ...and flush everything in the log out to disk. */ 1997470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 1998470decc6SDave Kleikamp while (!err && journal->j_checkpoint_transactions != NULL) { 1999470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 20006fa7aa50STejun Heo mutex_lock_io(&journal->j_checkpoint_mutex); 2001f7f4bccbSMingming Cao err = jbd2_log_do_checkpoint(journal); 200244519fafSHidehiro Kawai mutex_unlock(&journal->j_checkpoint_mutex); 2003470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 2004470decc6SDave Kleikamp } 2005470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 200644519fafSHidehiro Kawai 200744519fafSHidehiro Kawai if (is_journal_aborted(journal)) 200844519fafSHidehiro Kawai return -EIO; 200944519fafSHidehiro Kawai 20106fa7aa50STejun Heo mutex_lock_io(&journal->j_checkpoint_mutex); 20116f6a6fdaSJoseph Qi if (!err) { 20126f6a6fdaSJoseph Qi err = jbd2_cleanup_journal_tail(journal); 20136f6a6fdaSJoseph Qi if (err < 0) { 20146f6a6fdaSJoseph Qi mutex_unlock(&journal->j_checkpoint_mutex); 20156f6a6fdaSJoseph Qi goto out; 20166f6a6fdaSJoseph Qi } 20176f6a6fdaSJoseph Qi err = 0; 20186f6a6fdaSJoseph Qi } 2019470decc6SDave Kleikamp 2020470decc6SDave Kleikamp /* Finally, mark the journal as really needing no recovery. 2021470decc6SDave Kleikamp * This sets s_start==0 in the underlying superblock, which is 2022470decc6SDave Kleikamp * the magic code for a fully-recovered superblock. Any future 2023470decc6SDave Kleikamp * commits of data to the journal will restore the current 2024470decc6SDave Kleikamp * s_start value. */ 202517f423b5SJan Kara jbd2_mark_journal_empty(journal, REQ_SYNC | REQ_FUA); 2026a78bb11dSJan Kara mutex_unlock(&journal->j_checkpoint_mutex); 2027a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 2028470decc6SDave Kleikamp J_ASSERT(!journal->j_running_transaction); 2029470decc6SDave Kleikamp J_ASSERT(!journal->j_committing_transaction); 2030470decc6SDave Kleikamp J_ASSERT(!journal->j_checkpoint_transactions); 2031470decc6SDave Kleikamp J_ASSERT(journal->j_head == journal->j_tail); 2032470decc6SDave Kleikamp J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence); 2033a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 20346f6a6fdaSJoseph Qi out: 20356f6a6fdaSJoseph Qi return err; 2036470decc6SDave Kleikamp } 2037470decc6SDave Kleikamp 2038470decc6SDave Kleikamp /** 2039f7f4bccbSMingming Cao * int jbd2_journal_wipe() - Wipe journal contents 2040470decc6SDave Kleikamp * @journal: Journal to act on. 2041470decc6SDave Kleikamp * @write: flag (see below) 2042470decc6SDave Kleikamp * 2043470decc6SDave Kleikamp * Wipe out all of the contents of a journal, safely. This will produce 2044470decc6SDave Kleikamp * a warning if the journal contains any valid recovery information. 2045f7f4bccbSMingming Cao * Must be called between journal_init_*() and jbd2_journal_load(). 2046470decc6SDave Kleikamp * 2047470decc6SDave Kleikamp * If 'write' is non-zero, then we wipe out the journal on disk; otherwise 2048470decc6SDave Kleikamp * we merely suppress recovery. 2049470decc6SDave Kleikamp */ 2050470decc6SDave Kleikamp 2051f7f4bccbSMingming Cao int jbd2_journal_wipe(journal_t *journal, int write) 2052470decc6SDave Kleikamp { 2053470decc6SDave Kleikamp int err = 0; 2054470decc6SDave Kleikamp 2055f7f4bccbSMingming Cao J_ASSERT (!(journal->j_flags & JBD2_LOADED)); 2056470decc6SDave Kleikamp 2057470decc6SDave Kleikamp err = load_superblock(journal); 2058470decc6SDave Kleikamp if (err) 2059470decc6SDave Kleikamp return err; 2060470decc6SDave Kleikamp 2061470decc6SDave Kleikamp if (!journal->j_tail) 2062470decc6SDave Kleikamp goto no_recovery; 2063470decc6SDave Kleikamp 2064f2a44523SEryu Guan printk(KERN_WARNING "JBD2: %s recovery information on journal\n", 2065470decc6SDave Kleikamp write ? "Clearing" : "Ignoring"); 2066470decc6SDave Kleikamp 2067f7f4bccbSMingming Cao err = jbd2_journal_skip_recovery(journal); 2068a78bb11dSJan Kara if (write) { 2069a78bb11dSJan Kara /* Lock to make assertions happy... */ 2070a78bb11dSJan Kara mutex_lock(&journal->j_checkpoint_mutex); 207117f423b5SJan Kara jbd2_mark_journal_empty(journal, REQ_SYNC | REQ_FUA); 2072a78bb11dSJan Kara mutex_unlock(&journal->j_checkpoint_mutex); 2073a78bb11dSJan Kara } 2074470decc6SDave Kleikamp 2075470decc6SDave Kleikamp no_recovery: 2076470decc6SDave Kleikamp return err; 2077470decc6SDave Kleikamp } 2078470decc6SDave Kleikamp 2079470decc6SDave Kleikamp /* 2080470decc6SDave Kleikamp * Journal abort has very specific semantics, which we describe 2081470decc6SDave Kleikamp * for journal abort. 2082470decc6SDave Kleikamp * 2083bfcd3555SAlberto Bertogli * Two internal functions, which provide abort to the jbd layer 2084470decc6SDave Kleikamp * itself are here. 2085470decc6SDave Kleikamp */ 2086470decc6SDave Kleikamp 2087470decc6SDave Kleikamp /* 2088470decc6SDave Kleikamp * Quick version for internal journal use (doesn't lock the journal). 2089470decc6SDave Kleikamp * Aborts hard --- we mark the abort as occurred, but do _nothing_ else, 2090470decc6SDave Kleikamp * and don't attempt to make any other journal updates. 2091470decc6SDave Kleikamp */ 2092f7f4bccbSMingming Cao void __jbd2_journal_abort_hard(journal_t *journal) 2093470decc6SDave Kleikamp { 2094470decc6SDave Kleikamp transaction_t *transaction; 2095470decc6SDave Kleikamp 2096f7f4bccbSMingming Cao if (journal->j_flags & JBD2_ABORT) 2097470decc6SDave Kleikamp return; 2098470decc6SDave Kleikamp 2099470decc6SDave Kleikamp printk(KERN_ERR "Aborting journal on device %s.\n", 210005496769STheodore Ts'o journal->j_devname); 2101470decc6SDave Kleikamp 2102a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 2103f7f4bccbSMingming Cao journal->j_flags |= JBD2_ABORT; 2104470decc6SDave Kleikamp transaction = journal->j_running_transaction; 2105470decc6SDave Kleikamp if (transaction) 2106f7f4bccbSMingming Cao __jbd2_log_start_commit(journal, transaction->t_tid); 2107a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 2108470decc6SDave Kleikamp } 2109470decc6SDave Kleikamp 2110470decc6SDave Kleikamp /* Soft abort: record the abort error status in the journal superblock, 2111470decc6SDave Kleikamp * but don't do any other IO. */ 2112470decc6SDave Kleikamp static void __journal_abort_soft (journal_t *journal, int errno) 2113470decc6SDave Kleikamp { 2114fb7c0244STheodore Ts'o int old_errno; 2115470decc6SDave Kleikamp 2116fb7c0244STheodore Ts'o write_lock(&journal->j_state_lock); 2117fb7c0244STheodore Ts'o old_errno = journal->j_errno; 2118fb7c0244STheodore Ts'o if (!journal->j_errno || errno == -ESHUTDOWN) 2119470decc6SDave Kleikamp journal->j_errno = errno; 2120470decc6SDave Kleikamp 2121fb7c0244STheodore Ts'o if (journal->j_flags & JBD2_ABORT) { 2122fb7c0244STheodore Ts'o write_unlock(&journal->j_state_lock); 2123fb7c0244STheodore Ts'o if (!old_errno && old_errno != -ESHUTDOWN && 2124fb7c0244STheodore Ts'o errno == -ESHUTDOWN) 2125fb7c0244STheodore Ts'o jbd2_journal_update_sb_errno(journal); 2126fb7c0244STheodore Ts'o return; 2127fb7c0244STheodore Ts'o } 2128fb7c0244STheodore Ts'o write_unlock(&journal->j_state_lock); 2129fb7c0244STheodore Ts'o 2130f7f4bccbSMingming Cao __jbd2_journal_abort_hard(journal); 2131470decc6SDave Kleikamp 21324327ba52SDaeho Jeong if (errno) { 213324bcc89cSJan Kara jbd2_journal_update_sb_errno(journal); 21344327ba52SDaeho Jeong write_lock(&journal->j_state_lock); 21354327ba52SDaeho Jeong journal->j_flags |= JBD2_REC_ERR; 21364327ba52SDaeho Jeong write_unlock(&journal->j_state_lock); 21374327ba52SDaeho Jeong } 2138470decc6SDave Kleikamp } 2139470decc6SDave Kleikamp 2140470decc6SDave Kleikamp /** 2141f7f4bccbSMingming Cao * void jbd2_journal_abort () - Shutdown the journal immediately. 2142470decc6SDave Kleikamp * @journal: the journal to shutdown. 2143470decc6SDave Kleikamp * @errno: an error number to record in the journal indicating 2144470decc6SDave Kleikamp * the reason for the shutdown. 2145470decc6SDave Kleikamp * 2146470decc6SDave Kleikamp * Perform a complete, immediate shutdown of the ENTIRE 2147470decc6SDave Kleikamp * journal (not of a single transaction). This operation cannot be 2148470decc6SDave Kleikamp * undone without closing and reopening the journal. 2149470decc6SDave Kleikamp * 2150f7f4bccbSMingming Cao * The jbd2_journal_abort function is intended to support higher level error 2151470decc6SDave Kleikamp * recovery mechanisms such as the ext2/ext3 remount-readonly error 2152470decc6SDave Kleikamp * mode. 2153470decc6SDave Kleikamp * 2154470decc6SDave Kleikamp * Journal abort has very specific semantics. Any existing dirty, 2155470decc6SDave Kleikamp * unjournaled buffers in the main filesystem will still be written to 2156470decc6SDave Kleikamp * disk by bdflush, but the journaling mechanism will be suspended 2157470decc6SDave Kleikamp * immediately and no further transaction commits will be honoured. 2158470decc6SDave Kleikamp * 2159470decc6SDave Kleikamp * Any dirty, journaled buffers will be written back to disk without 2160470decc6SDave Kleikamp * hitting the journal. Atomicity cannot be guaranteed on an aborted 2161470decc6SDave Kleikamp * filesystem, but we _do_ attempt to leave as much data as possible 2162470decc6SDave Kleikamp * behind for fsck to use for cleanup. 2163470decc6SDave Kleikamp * 2164470decc6SDave Kleikamp * Any attempt to get a new transaction handle on a journal which is in 2165470decc6SDave Kleikamp * ABORT state will just result in an -EROFS error return. A 2166f7f4bccbSMingming Cao * jbd2_journal_stop on an existing handle will return -EIO if we have 2167470decc6SDave Kleikamp * entered abort state during the update. 2168470decc6SDave Kleikamp * 2169470decc6SDave Kleikamp * Recursive transactions are not disturbed by journal abort until the 2170f7f4bccbSMingming Cao * final jbd2_journal_stop, which will receive the -EIO error. 2171470decc6SDave Kleikamp * 2172f7f4bccbSMingming Cao * Finally, the jbd2_journal_abort call allows the caller to supply an errno 2173470decc6SDave Kleikamp * which will be recorded (if possible) in the journal superblock. This 2174470decc6SDave Kleikamp * allows a client to record failure conditions in the middle of a 2175470decc6SDave Kleikamp * transaction without having to complete the transaction to record the 2176470decc6SDave Kleikamp * failure to disk. ext3_error, for example, now uses this 2177470decc6SDave Kleikamp * functionality. 2178470decc6SDave Kleikamp * 2179470decc6SDave Kleikamp * Errors which originate from within the journaling layer will NOT 2180470decc6SDave Kleikamp * supply an errno; a null errno implies that absolutely no further 2181470decc6SDave Kleikamp * writes are done to the journal (unless there are any already in 2182470decc6SDave Kleikamp * progress). 2183470decc6SDave Kleikamp * 2184470decc6SDave Kleikamp */ 2185470decc6SDave Kleikamp 2186f7f4bccbSMingming Cao void jbd2_journal_abort(journal_t *journal, int errno) 2187470decc6SDave Kleikamp { 2188470decc6SDave Kleikamp __journal_abort_soft(journal, errno); 2189470decc6SDave Kleikamp } 2190470decc6SDave Kleikamp 2191470decc6SDave Kleikamp /** 2192f7f4bccbSMingming Cao * int jbd2_journal_errno () - returns the journal's error state. 2193470decc6SDave Kleikamp * @journal: journal to examine. 2194470decc6SDave Kleikamp * 2195bfcd3555SAlberto Bertogli * This is the errno number set with jbd2_journal_abort(), the last 2196470decc6SDave Kleikamp * time the journal was mounted - if the journal was stopped 2197470decc6SDave Kleikamp * without calling abort this will be 0. 2198470decc6SDave Kleikamp * 2199470decc6SDave Kleikamp * If the journal has been aborted on this mount time -EROFS will 2200470decc6SDave Kleikamp * be returned. 2201470decc6SDave Kleikamp */ 2202f7f4bccbSMingming Cao int jbd2_journal_errno(journal_t *journal) 2203470decc6SDave Kleikamp { 2204470decc6SDave Kleikamp int err; 2205470decc6SDave Kleikamp 2206a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 2207f7f4bccbSMingming Cao if (journal->j_flags & JBD2_ABORT) 2208470decc6SDave Kleikamp err = -EROFS; 2209470decc6SDave Kleikamp else 2210470decc6SDave Kleikamp err = journal->j_errno; 2211a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 2212470decc6SDave Kleikamp return err; 2213470decc6SDave Kleikamp } 2214470decc6SDave Kleikamp 2215470decc6SDave Kleikamp /** 2216f7f4bccbSMingming Cao * int jbd2_journal_clear_err () - clears the journal's error state 2217470decc6SDave Kleikamp * @journal: journal to act on. 2218470decc6SDave Kleikamp * 2219bfcd3555SAlberto Bertogli * An error must be cleared or acked to take a FS out of readonly 2220470decc6SDave Kleikamp * mode. 2221470decc6SDave Kleikamp */ 2222f7f4bccbSMingming Cao int jbd2_journal_clear_err(journal_t *journal) 2223470decc6SDave Kleikamp { 2224470decc6SDave Kleikamp int err = 0; 2225470decc6SDave Kleikamp 2226a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 2227f7f4bccbSMingming Cao if (journal->j_flags & JBD2_ABORT) 2228470decc6SDave Kleikamp err = -EROFS; 2229470decc6SDave Kleikamp else 2230470decc6SDave Kleikamp journal->j_errno = 0; 2231a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 2232470decc6SDave Kleikamp return err; 2233470decc6SDave Kleikamp } 2234470decc6SDave Kleikamp 2235470decc6SDave Kleikamp /** 2236f7f4bccbSMingming Cao * void jbd2_journal_ack_err() - Ack journal err. 2237470decc6SDave Kleikamp * @journal: journal to act on. 2238470decc6SDave Kleikamp * 2239bfcd3555SAlberto Bertogli * An error must be cleared or acked to take a FS out of readonly 2240470decc6SDave Kleikamp * mode. 2241470decc6SDave Kleikamp */ 2242f7f4bccbSMingming Cao void jbd2_journal_ack_err(journal_t *journal) 2243470decc6SDave Kleikamp { 2244a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 2245470decc6SDave Kleikamp if (journal->j_errno) 2246f7f4bccbSMingming Cao journal->j_flags |= JBD2_ACK_ERR; 2247a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 2248470decc6SDave Kleikamp } 2249470decc6SDave Kleikamp 2250f7f4bccbSMingming Cao int jbd2_journal_blocks_per_page(struct inode *inode) 2251470decc6SDave Kleikamp { 225209cbfeafSKirill A. Shutemov return 1 << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits); 2253470decc6SDave Kleikamp } 2254470decc6SDave Kleikamp 2255470decc6SDave Kleikamp /* 2256b517bea1SZach Brown * helper functions to deal with 32 or 64bit block numbers. 2257b517bea1SZach Brown */ 2258b517bea1SZach Brown size_t journal_tag_bytes(journal_t *journal) 2259b517bea1SZach Brown { 2260db9ee220SDarrick J. Wong size_t sz; 2261db9ee220SDarrick J. Wong 226256316a0dSDarrick J. Wong if (jbd2_has_feature_csum3(journal)) 2263db9ee220SDarrick J. Wong return sizeof(journal_block_tag3_t); 2264db9ee220SDarrick J. Wong 2265db9ee220SDarrick J. Wong sz = sizeof(journal_block_tag_t); 2266c3900875SDarrick J. Wong 226756316a0dSDarrick J. Wong if (jbd2_has_feature_csum2(journal)) 2268db9ee220SDarrick J. Wong sz += sizeof(__u16); 2269c3900875SDarrick J. Wong 227056316a0dSDarrick J. Wong if (jbd2_has_feature_64bit(journal)) 2271db9ee220SDarrick J. Wong return sz; 2272b517bea1SZach Brown else 2273db9ee220SDarrick J. Wong return sz - sizeof(__u32); 2274b517bea1SZach Brown } 2275b517bea1SZach Brown 2276b517bea1SZach Brown /* 2277d2eecb03STheodore Ts'o * JBD memory management 2278d2eecb03STheodore Ts'o * 2279d2eecb03STheodore Ts'o * These functions are used to allocate block-sized chunks of memory 2280d2eecb03STheodore Ts'o * used for making copies of buffer_head data. Very often it will be 2281d2eecb03STheodore Ts'o * page-sized chunks of data, but sometimes it will be in 2282d2eecb03STheodore Ts'o * sub-page-size chunks. (For example, 16k pages on Power systems 2283d2eecb03STheodore Ts'o * with a 4k block file system.) For blocks smaller than a page, we 2284d2eecb03STheodore Ts'o * use a SLAB allocator. There are slab caches for each block size, 2285d2eecb03STheodore Ts'o * which are allocated at mount time, if necessary, and we only free 2286d2eecb03STheodore Ts'o * (all of) the slab caches when/if the jbd2 module is unloaded. For 2287d2eecb03STheodore Ts'o * this reason we don't need to a mutex to protect access to 2288d2eecb03STheodore Ts'o * jbd2_slab[] allocating or releasing memory; only in 2289d2eecb03STheodore Ts'o * jbd2_journal_create_slab(). 2290d2eecb03STheodore Ts'o */ 2291d2eecb03STheodore Ts'o #define JBD2_MAX_SLABS 8 2292d2eecb03STheodore Ts'o static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS]; 2293d2eecb03STheodore Ts'o 2294d2eecb03STheodore Ts'o static const char *jbd2_slab_names[JBD2_MAX_SLABS] = { 2295d2eecb03STheodore Ts'o "jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k", 2296d2eecb03STheodore Ts'o "jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k" 2297d2eecb03STheodore Ts'o }; 2298d2eecb03STheodore Ts'o 2299d2eecb03STheodore Ts'o 2300d2eecb03STheodore Ts'o static void jbd2_journal_destroy_slabs(void) 2301d2eecb03STheodore Ts'o { 2302d2eecb03STheodore Ts'o int i; 2303d2eecb03STheodore Ts'o 2304d2eecb03STheodore Ts'o for (i = 0; i < JBD2_MAX_SLABS; i++) { 2305d2eecb03STheodore Ts'o if (jbd2_slab[i]) 2306d2eecb03STheodore Ts'o kmem_cache_destroy(jbd2_slab[i]); 2307d2eecb03STheodore Ts'o jbd2_slab[i] = NULL; 2308d2eecb03STheodore Ts'o } 2309d2eecb03STheodore Ts'o } 2310d2eecb03STheodore Ts'o 2311d2eecb03STheodore Ts'o static int jbd2_journal_create_slab(size_t size) 2312d2eecb03STheodore Ts'o { 231351dfacdeSThomas Gleixner static DEFINE_MUTEX(jbd2_slab_create_mutex); 2314d2eecb03STheodore Ts'o int i = order_base_2(size) - 10; 2315d2eecb03STheodore Ts'o size_t slab_size; 2316d2eecb03STheodore Ts'o 2317d2eecb03STheodore Ts'o if (size == PAGE_SIZE) 2318d2eecb03STheodore Ts'o return 0; 2319d2eecb03STheodore Ts'o 2320d2eecb03STheodore Ts'o if (i >= JBD2_MAX_SLABS) 2321d2eecb03STheodore Ts'o return -EINVAL; 2322d2eecb03STheodore Ts'o 2323d2eecb03STheodore Ts'o if (unlikely(i < 0)) 2324d2eecb03STheodore Ts'o i = 0; 232551dfacdeSThomas Gleixner mutex_lock(&jbd2_slab_create_mutex); 2326d2eecb03STheodore Ts'o if (jbd2_slab[i]) { 232751dfacdeSThomas Gleixner mutex_unlock(&jbd2_slab_create_mutex); 2328d2eecb03STheodore Ts'o return 0; /* Already created */ 2329d2eecb03STheodore Ts'o } 2330d2eecb03STheodore Ts'o 2331d2eecb03STheodore Ts'o slab_size = 1 << (i+10); 2332d2eecb03STheodore Ts'o jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size, 2333d2eecb03STheodore Ts'o slab_size, 0, NULL); 233451dfacdeSThomas Gleixner mutex_unlock(&jbd2_slab_create_mutex); 2335d2eecb03STheodore Ts'o if (!jbd2_slab[i]) { 2336d2eecb03STheodore Ts'o printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n"); 2337d2eecb03STheodore Ts'o return -ENOMEM; 2338d2eecb03STheodore Ts'o } 2339d2eecb03STheodore Ts'o return 0; 2340d2eecb03STheodore Ts'o } 2341d2eecb03STheodore Ts'o 2342d2eecb03STheodore Ts'o static struct kmem_cache *get_slab(size_t size) 2343d2eecb03STheodore Ts'o { 2344d2eecb03STheodore Ts'o int i = order_base_2(size) - 10; 2345d2eecb03STheodore Ts'o 2346d2eecb03STheodore Ts'o BUG_ON(i >= JBD2_MAX_SLABS); 2347d2eecb03STheodore Ts'o if (unlikely(i < 0)) 2348d2eecb03STheodore Ts'o i = 0; 23498ac97b74SBill Pemberton BUG_ON(jbd2_slab[i] == NULL); 2350d2eecb03STheodore Ts'o return jbd2_slab[i]; 2351d2eecb03STheodore Ts'o } 2352d2eecb03STheodore Ts'o 2353d2eecb03STheodore Ts'o void *jbd2_alloc(size_t size, gfp_t flags) 2354d2eecb03STheodore Ts'o { 2355d2eecb03STheodore Ts'o void *ptr; 2356d2eecb03STheodore Ts'o 2357d2eecb03STheodore Ts'o BUG_ON(size & (size-1)); /* Must be a power of 2 */ 2358d2eecb03STheodore Ts'o 2359f2db1971SMichal Hocko if (size < PAGE_SIZE) 2360d2eecb03STheodore Ts'o ptr = kmem_cache_alloc(get_slab(size), flags); 2361f2db1971SMichal Hocko else 2362f2db1971SMichal Hocko ptr = (void *)__get_free_pages(flags, get_order(size)); 2363d2eecb03STheodore Ts'o 2364d2eecb03STheodore Ts'o /* Check alignment; SLUB has gotten this wrong in the past, 2365d2eecb03STheodore Ts'o * and this can lead to user data corruption! */ 2366d2eecb03STheodore Ts'o BUG_ON(((unsigned long) ptr) & (size-1)); 2367d2eecb03STheodore Ts'o 2368d2eecb03STheodore Ts'o return ptr; 2369d2eecb03STheodore Ts'o } 2370d2eecb03STheodore Ts'o 2371d2eecb03STheodore Ts'o void jbd2_free(void *ptr, size_t size) 2372d2eecb03STheodore Ts'o { 2373f2db1971SMichal Hocko if (size < PAGE_SIZE) 2374d2eecb03STheodore Ts'o kmem_cache_free(get_slab(size), ptr); 2375f2db1971SMichal Hocko else 2376f2db1971SMichal Hocko free_pages((unsigned long)ptr, get_order(size)); 2377d2eecb03STheodore Ts'o }; 2378d2eecb03STheodore Ts'o 2379d2eecb03STheodore Ts'o /* 2380470decc6SDave Kleikamp * Journal_head storage management 2381470decc6SDave Kleikamp */ 2382e18b890bSChristoph Lameter static struct kmem_cache *jbd2_journal_head_cache; 2383e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 2384470decc6SDave Kleikamp static atomic_t nr_journal_heads = ATOMIC_INIT(0); 2385470decc6SDave Kleikamp #endif 2386470decc6SDave Kleikamp 23874185a2acSYongqiang Yang static int jbd2_journal_init_journal_head_cache(void) 2388470decc6SDave Kleikamp { 2389470decc6SDave Kleikamp int retval; 2390470decc6SDave Kleikamp 23911076d17aSAl Viro J_ASSERT(jbd2_journal_head_cache == NULL); 2392a920e941SJohann Lombardi jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head", 2393470decc6SDave Kleikamp sizeof(struct journal_head), 2394470decc6SDave Kleikamp 0, /* offset */ 23955f0d5a3aSPaul E. McKenney SLAB_TEMPORARY | SLAB_TYPESAFE_BY_RCU, 239620c2df83SPaul Mundt NULL); /* ctor */ 2397470decc6SDave Kleikamp retval = 0; 23981076d17aSAl Viro if (!jbd2_journal_head_cache) { 2399470decc6SDave Kleikamp retval = -ENOMEM; 2400f2a44523SEryu Guan printk(KERN_EMERG "JBD2: no memory for journal_head cache\n"); 2401470decc6SDave Kleikamp } 2402470decc6SDave Kleikamp return retval; 2403470decc6SDave Kleikamp } 2404470decc6SDave Kleikamp 24054185a2acSYongqiang Yang static void jbd2_journal_destroy_journal_head_cache(void) 2406470decc6SDave Kleikamp { 24078a9362ebSDuane Griffin if (jbd2_journal_head_cache) { 2408f7f4bccbSMingming Cao kmem_cache_destroy(jbd2_journal_head_cache); 2409f7f4bccbSMingming Cao jbd2_journal_head_cache = NULL; 2410470decc6SDave Kleikamp } 24118a9362ebSDuane Griffin } 2412470decc6SDave Kleikamp 2413470decc6SDave Kleikamp /* 2414470decc6SDave Kleikamp * journal_head splicing and dicing 2415470decc6SDave Kleikamp */ 2416470decc6SDave Kleikamp static struct journal_head *journal_alloc_journal_head(void) 2417470decc6SDave Kleikamp { 2418470decc6SDave Kleikamp struct journal_head *ret; 2419470decc6SDave Kleikamp 2420e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 2421470decc6SDave Kleikamp atomic_inc(&nr_journal_heads); 2422470decc6SDave Kleikamp #endif 24235d9cf9c6SZheng Liu ret = kmem_cache_zalloc(jbd2_journal_head_cache, GFP_NOFS); 24241076d17aSAl Viro if (!ret) { 2425470decc6SDave Kleikamp jbd_debug(1, "out of memory for journal_head\n"); 2426670be5a7STheodore Ts'o pr_notice_ratelimited("ENOMEM in %s, retrying.\n", __func__); 24277b506b10SMichal Hocko ret = kmem_cache_zalloc(jbd2_journal_head_cache, 24287b506b10SMichal Hocko GFP_NOFS | __GFP_NOFAIL); 2429470decc6SDave Kleikamp } 2430470decc6SDave Kleikamp return ret; 2431470decc6SDave Kleikamp } 2432470decc6SDave Kleikamp 2433470decc6SDave Kleikamp static void journal_free_journal_head(struct journal_head *jh) 2434470decc6SDave Kleikamp { 2435e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 2436470decc6SDave Kleikamp atomic_dec(&nr_journal_heads); 2437cd02ff0bSMingming Cao memset(jh, JBD2_POISON_FREE, sizeof(*jh)); 2438470decc6SDave Kleikamp #endif 2439f7f4bccbSMingming Cao kmem_cache_free(jbd2_journal_head_cache, jh); 2440470decc6SDave Kleikamp } 2441470decc6SDave Kleikamp 2442470decc6SDave Kleikamp /* 2443470decc6SDave Kleikamp * A journal_head is attached to a buffer_head whenever JBD has an 2444470decc6SDave Kleikamp * interest in the buffer. 2445470decc6SDave Kleikamp * 2446470decc6SDave Kleikamp * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit 2447470decc6SDave Kleikamp * is set. This bit is tested in core kernel code where we need to take 2448470decc6SDave Kleikamp * JBD-specific actions. Testing the zeroness of ->b_private is not reliable 2449470decc6SDave Kleikamp * there. 2450470decc6SDave Kleikamp * 2451470decc6SDave Kleikamp * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one. 2452470decc6SDave Kleikamp * 2453470decc6SDave Kleikamp * When a buffer has its BH_JBD bit set it is immune from being released by 2454470decc6SDave Kleikamp * core kernel code, mainly via ->b_count. 2455470decc6SDave Kleikamp * 2456de1b7941SJan Kara * A journal_head is detached from its buffer_head when the journal_head's 2457de1b7941SJan Kara * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint 2458de1b7941SJan Kara * transaction (b_cp_transaction) hold their references to b_jcount. 2459470decc6SDave Kleikamp * 2460470decc6SDave Kleikamp * Various places in the kernel want to attach a journal_head to a buffer_head 2461470decc6SDave Kleikamp * _before_ attaching the journal_head to a transaction. To protect the 2462f7f4bccbSMingming Cao * journal_head in this situation, jbd2_journal_add_journal_head elevates the 2463470decc6SDave Kleikamp * journal_head's b_jcount refcount by one. The caller must call 2464f7f4bccbSMingming Cao * jbd2_journal_put_journal_head() to undo this. 2465470decc6SDave Kleikamp * 2466470decc6SDave Kleikamp * So the typical usage would be: 2467470decc6SDave Kleikamp * 2468470decc6SDave Kleikamp * (Attach a journal_head if needed. Increments b_jcount) 2469f7f4bccbSMingming Cao * struct journal_head *jh = jbd2_journal_add_journal_head(bh); 2470470decc6SDave Kleikamp * ... 2471de1b7941SJan Kara * (Get another reference for transaction) 2472de1b7941SJan Kara * jbd2_journal_grab_journal_head(bh); 2473470decc6SDave Kleikamp * jh->b_transaction = xxx; 2474de1b7941SJan Kara * (Put original reference) 2475f7f4bccbSMingming Cao * jbd2_journal_put_journal_head(jh); 2476470decc6SDave Kleikamp */ 2477470decc6SDave Kleikamp 2478470decc6SDave Kleikamp /* 2479470decc6SDave Kleikamp * Give a buffer_head a journal_head. 2480470decc6SDave Kleikamp * 2481470decc6SDave Kleikamp * May sleep. 2482470decc6SDave Kleikamp */ 2483f7f4bccbSMingming Cao struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh) 2484470decc6SDave Kleikamp { 2485470decc6SDave Kleikamp struct journal_head *jh; 2486470decc6SDave Kleikamp struct journal_head *new_jh = NULL; 2487470decc6SDave Kleikamp 2488470decc6SDave Kleikamp repeat: 24895d9cf9c6SZheng Liu if (!buffer_jbd(bh)) 2490470decc6SDave Kleikamp new_jh = journal_alloc_journal_head(); 2491470decc6SDave Kleikamp 2492470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 2493470decc6SDave Kleikamp if (buffer_jbd(bh)) { 2494470decc6SDave Kleikamp jh = bh2jh(bh); 2495470decc6SDave Kleikamp } else { 2496470decc6SDave Kleikamp J_ASSERT_BH(bh, 2497470decc6SDave Kleikamp (atomic_read(&bh->b_count) > 0) || 2498470decc6SDave Kleikamp (bh->b_page && bh->b_page->mapping)); 2499470decc6SDave Kleikamp 2500470decc6SDave Kleikamp if (!new_jh) { 2501470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 2502470decc6SDave Kleikamp goto repeat; 2503470decc6SDave Kleikamp } 2504470decc6SDave Kleikamp 2505470decc6SDave Kleikamp jh = new_jh; 2506470decc6SDave Kleikamp new_jh = NULL; /* We consumed it */ 2507470decc6SDave Kleikamp set_buffer_jbd(bh); 2508470decc6SDave Kleikamp bh->b_private = jh; 2509470decc6SDave Kleikamp jh->b_bh = bh; 2510470decc6SDave Kleikamp get_bh(bh); 2511470decc6SDave Kleikamp BUFFER_TRACE(bh, "added journal_head"); 2512470decc6SDave Kleikamp } 2513470decc6SDave Kleikamp jh->b_jcount++; 2514470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 2515470decc6SDave Kleikamp if (new_jh) 2516470decc6SDave Kleikamp journal_free_journal_head(new_jh); 2517470decc6SDave Kleikamp return bh->b_private; 2518470decc6SDave Kleikamp } 2519470decc6SDave Kleikamp 2520470decc6SDave Kleikamp /* 2521470decc6SDave Kleikamp * Grab a ref against this buffer_head's journal_head. If it ended up not 2522470decc6SDave Kleikamp * having a journal_head, return NULL 2523470decc6SDave Kleikamp */ 2524f7f4bccbSMingming Cao struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh) 2525470decc6SDave Kleikamp { 2526470decc6SDave Kleikamp struct journal_head *jh = NULL; 2527470decc6SDave Kleikamp 2528470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 2529470decc6SDave Kleikamp if (buffer_jbd(bh)) { 2530470decc6SDave Kleikamp jh = bh2jh(bh); 2531470decc6SDave Kleikamp jh->b_jcount++; 2532470decc6SDave Kleikamp } 2533470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 2534470decc6SDave Kleikamp return jh; 2535470decc6SDave Kleikamp } 2536470decc6SDave Kleikamp 2537470decc6SDave Kleikamp static void __journal_remove_journal_head(struct buffer_head *bh) 2538470decc6SDave Kleikamp { 2539470decc6SDave Kleikamp struct journal_head *jh = bh2jh(bh); 2540470decc6SDave Kleikamp 2541470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_jcount >= 0); 2542de1b7941SJan Kara J_ASSERT_JH(jh, jh->b_transaction == NULL); 2543de1b7941SJan Kara J_ASSERT_JH(jh, jh->b_next_transaction == NULL); 2544de1b7941SJan Kara J_ASSERT_JH(jh, jh->b_cp_transaction == NULL); 2545470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_jlist == BJ_None); 2546470decc6SDave Kleikamp J_ASSERT_BH(bh, buffer_jbd(bh)); 2547470decc6SDave Kleikamp J_ASSERT_BH(bh, jh2bh(jh) == bh); 2548470decc6SDave Kleikamp BUFFER_TRACE(bh, "remove journal_head"); 2549470decc6SDave Kleikamp if (jh->b_frozen_data) { 2550de1b7941SJan Kara printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__); 2551af1e76d6SMingming Cao jbd2_free(jh->b_frozen_data, bh->b_size); 2552470decc6SDave Kleikamp } 2553470decc6SDave Kleikamp if (jh->b_committed_data) { 2554de1b7941SJan Kara printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__); 2555af1e76d6SMingming Cao jbd2_free(jh->b_committed_data, bh->b_size); 2556470decc6SDave Kleikamp } 2557470decc6SDave Kleikamp bh->b_private = NULL; 2558470decc6SDave Kleikamp jh->b_bh = NULL; /* debug, really */ 2559470decc6SDave Kleikamp clear_buffer_jbd(bh); 2560470decc6SDave Kleikamp journal_free_journal_head(jh); 2561470decc6SDave Kleikamp } 2562470decc6SDave Kleikamp 2563470decc6SDave Kleikamp /* 2564de1b7941SJan Kara * Drop a reference on the passed journal_head. If it fell to zero then 2565470decc6SDave Kleikamp * release the journal_head from the buffer_head. 2566470decc6SDave Kleikamp */ 2567f7f4bccbSMingming Cao void jbd2_journal_put_journal_head(struct journal_head *jh) 2568470decc6SDave Kleikamp { 2569470decc6SDave Kleikamp struct buffer_head *bh = jh2bh(jh); 2570470decc6SDave Kleikamp 2571470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 2572470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_jcount > 0); 2573470decc6SDave Kleikamp --jh->b_jcount; 2574de1b7941SJan Kara if (!jh->b_jcount) { 2575470decc6SDave Kleikamp __journal_remove_journal_head(bh); 2576de1b7941SJan Kara jbd_unlock_bh_journal_head(bh); 2577470decc6SDave Kleikamp __brelse(bh); 2578de1b7941SJan Kara } else 2579470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 2580470decc6SDave Kleikamp } 2581470decc6SDave Kleikamp 2582470decc6SDave Kleikamp /* 2583c851ed54SJan Kara * Initialize jbd inode head 2584c851ed54SJan Kara */ 2585c851ed54SJan Kara void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode) 2586c851ed54SJan Kara { 2587c851ed54SJan Kara jinode->i_transaction = NULL; 2588c851ed54SJan Kara jinode->i_next_transaction = NULL; 2589c851ed54SJan Kara jinode->i_vfs_inode = inode; 2590c851ed54SJan Kara jinode->i_flags = 0; 2591c851ed54SJan Kara INIT_LIST_HEAD(&jinode->i_list); 2592c851ed54SJan Kara } 2593c851ed54SJan Kara 2594c851ed54SJan Kara /* 2595c851ed54SJan Kara * Function to be called before we start removing inode from memory (i.e., 2596c851ed54SJan Kara * clear_inode() is a fine place to be called from). It removes inode from 2597c851ed54SJan Kara * transaction's lists. 2598c851ed54SJan Kara */ 2599c851ed54SJan Kara void jbd2_journal_release_jbd_inode(journal_t *journal, 2600c851ed54SJan Kara struct jbd2_inode *jinode) 2601c851ed54SJan Kara { 2602c851ed54SJan Kara if (!journal) 2603c851ed54SJan Kara return; 2604c851ed54SJan Kara restart: 2605c851ed54SJan Kara spin_lock(&journal->j_list_lock); 2606c851ed54SJan Kara /* Is commit writing out inode - we have to wait */ 2607cb0d9d47SJan Kara if (jinode->i_flags & JI_COMMIT_RUNNING) { 2608c851ed54SJan Kara wait_queue_head_t *wq; 2609c851ed54SJan Kara DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING); 2610c851ed54SJan Kara wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING); 261121417136SIngo Molnar prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE); 2612c851ed54SJan Kara spin_unlock(&journal->j_list_lock); 2613c851ed54SJan Kara schedule(); 261421417136SIngo Molnar finish_wait(wq, &wait.wq_entry); 2615c851ed54SJan Kara goto restart; 2616c851ed54SJan Kara } 2617c851ed54SJan Kara 2618c851ed54SJan Kara if (jinode->i_transaction) { 2619c851ed54SJan Kara list_del(&jinode->i_list); 2620c851ed54SJan Kara jinode->i_transaction = NULL; 2621c851ed54SJan Kara } 2622c851ed54SJan Kara spin_unlock(&journal->j_list_lock); 2623c851ed54SJan Kara } 2624c851ed54SJan Kara 2625470decc6SDave Kleikamp 26268e85fb3fSJohann Lombardi #ifdef CONFIG_PROC_FS 26278e85fb3fSJohann Lombardi 26288e85fb3fSJohann Lombardi #define JBD2_STATS_PROC_NAME "fs/jbd2" 26298e85fb3fSJohann Lombardi 26308e85fb3fSJohann Lombardi static void __init jbd2_create_jbd_stats_proc_entry(void) 26318e85fb3fSJohann Lombardi { 26328e85fb3fSJohann Lombardi proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL); 26338e85fb3fSJohann Lombardi } 26348e85fb3fSJohann Lombardi 26358e85fb3fSJohann Lombardi static void __exit jbd2_remove_jbd_stats_proc_entry(void) 26368e85fb3fSJohann Lombardi { 26378e85fb3fSJohann Lombardi if (proc_jbd2_stats) 26388e85fb3fSJohann Lombardi remove_proc_entry(JBD2_STATS_PROC_NAME, NULL); 26398e85fb3fSJohann Lombardi } 26408e85fb3fSJohann Lombardi 26418e85fb3fSJohann Lombardi #else 26428e85fb3fSJohann Lombardi 26438e85fb3fSJohann Lombardi #define jbd2_create_jbd_stats_proc_entry() do {} while (0) 26448e85fb3fSJohann Lombardi #define jbd2_remove_jbd_stats_proc_entry() do {} while (0) 26458e85fb3fSJohann Lombardi 26468e85fb3fSJohann Lombardi #endif 26478e85fb3fSJohann Lombardi 26488aefcd55STheodore Ts'o struct kmem_cache *jbd2_handle_cache, *jbd2_inode_cache; 2649470decc6SDave Kleikamp 26504185a2acSYongqiang Yang static int __init jbd2_journal_init_handle_cache(void) 2651470decc6SDave Kleikamp { 26528aefcd55STheodore Ts'o jbd2_handle_cache = KMEM_CACHE(jbd2_journal_handle, SLAB_TEMPORARY); 2653f7f4bccbSMingming Cao if (jbd2_handle_cache == NULL) { 26548aefcd55STheodore Ts'o printk(KERN_EMERG "JBD2: failed to create handle cache\n"); 26558aefcd55STheodore Ts'o return -ENOMEM; 26568aefcd55STheodore Ts'o } 26578aefcd55STheodore Ts'o jbd2_inode_cache = KMEM_CACHE(jbd2_inode, 0); 26588aefcd55STheodore Ts'o if (jbd2_inode_cache == NULL) { 26598aefcd55STheodore Ts'o printk(KERN_EMERG "JBD2: failed to create inode cache\n"); 26608aefcd55STheodore Ts'o kmem_cache_destroy(jbd2_handle_cache); 2661470decc6SDave Kleikamp return -ENOMEM; 2662470decc6SDave Kleikamp } 2663470decc6SDave Kleikamp return 0; 2664470decc6SDave Kleikamp } 2665470decc6SDave Kleikamp 2666f7f4bccbSMingming Cao static void jbd2_journal_destroy_handle_cache(void) 2667470decc6SDave Kleikamp { 2668f7f4bccbSMingming Cao if (jbd2_handle_cache) 2669f7f4bccbSMingming Cao kmem_cache_destroy(jbd2_handle_cache); 26708aefcd55STheodore Ts'o if (jbd2_inode_cache) 26718aefcd55STheodore Ts'o kmem_cache_destroy(jbd2_inode_cache); 26728aefcd55STheodore Ts'o 2673470decc6SDave Kleikamp } 2674470decc6SDave Kleikamp 2675470decc6SDave Kleikamp /* 2676470decc6SDave Kleikamp * Module startup and shutdown 2677470decc6SDave Kleikamp */ 2678470decc6SDave Kleikamp 2679470decc6SDave Kleikamp static int __init journal_init_caches(void) 2680470decc6SDave Kleikamp { 2681470decc6SDave Kleikamp int ret; 2682470decc6SDave Kleikamp 2683f7f4bccbSMingming Cao ret = jbd2_journal_init_revoke_caches(); 2684470decc6SDave Kleikamp if (ret == 0) 26854185a2acSYongqiang Yang ret = jbd2_journal_init_journal_head_cache(); 2686470decc6SDave Kleikamp if (ret == 0) 26874185a2acSYongqiang Yang ret = jbd2_journal_init_handle_cache(); 26880c2022ecSYongqiang Yang if (ret == 0) 26890c2022ecSYongqiang Yang ret = jbd2_journal_init_transaction_cache(); 2690470decc6SDave Kleikamp return ret; 2691470decc6SDave Kleikamp } 2692470decc6SDave Kleikamp 2693f7f4bccbSMingming Cao static void jbd2_journal_destroy_caches(void) 2694470decc6SDave Kleikamp { 2695f7f4bccbSMingming Cao jbd2_journal_destroy_revoke_caches(); 26964185a2acSYongqiang Yang jbd2_journal_destroy_journal_head_cache(); 2697f7f4bccbSMingming Cao jbd2_journal_destroy_handle_cache(); 26980c2022ecSYongqiang Yang jbd2_journal_destroy_transaction_cache(); 2699d2eecb03STheodore Ts'o jbd2_journal_destroy_slabs(); 2700470decc6SDave Kleikamp } 2701470decc6SDave Kleikamp 2702470decc6SDave Kleikamp static int __init journal_init(void) 2703470decc6SDave Kleikamp { 2704470decc6SDave Kleikamp int ret; 2705470decc6SDave Kleikamp 2706470decc6SDave Kleikamp BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024); 2707470decc6SDave Kleikamp 2708470decc6SDave Kleikamp ret = journal_init_caches(); 2709620de4e1SDuane Griffin if (ret == 0) { 27108e85fb3fSJohann Lombardi jbd2_create_jbd_stats_proc_entry(); 2711620de4e1SDuane Griffin } else { 2712620de4e1SDuane Griffin jbd2_journal_destroy_caches(); 2713620de4e1SDuane Griffin } 2714470decc6SDave Kleikamp return ret; 2715470decc6SDave Kleikamp } 2716470decc6SDave Kleikamp 2717470decc6SDave Kleikamp static void __exit journal_exit(void) 2718470decc6SDave Kleikamp { 2719e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 2720470decc6SDave Kleikamp int n = atomic_read(&nr_journal_heads); 2721470decc6SDave Kleikamp if (n) 272275685071SJan Kara printk(KERN_ERR "JBD2: leaked %d journal_heads!\n", n); 2723470decc6SDave Kleikamp #endif 27248e85fb3fSJohann Lombardi jbd2_remove_jbd_stats_proc_entry(); 2725f7f4bccbSMingming Cao jbd2_journal_destroy_caches(); 2726470decc6SDave Kleikamp } 2727470decc6SDave Kleikamp 2728470decc6SDave Kleikamp MODULE_LICENSE("GPL"); 2729470decc6SDave Kleikamp module_init(journal_init); 2730470decc6SDave Kleikamp module_exit(journal_exit); 2731470decc6SDave Kleikamp 2732