1470decc6SDave Kleikamp /* 2f7f4bccbSMingming Cao * linux/fs/jbd2/journal.c 3470decc6SDave Kleikamp * 4470decc6SDave Kleikamp * Written by Stephen C. Tweedie <sct@redhat.com>, 1998 5470decc6SDave Kleikamp * 6470decc6SDave Kleikamp * Copyright 1998 Red Hat corp --- All Rights Reserved 7470decc6SDave Kleikamp * 8470decc6SDave Kleikamp * This file is part of the Linux kernel and is made available under 9470decc6SDave Kleikamp * the terms of the GNU General Public License, version 2, or at your 10470decc6SDave Kleikamp * option, any later version, incorporated herein by reference. 11470decc6SDave Kleikamp * 12470decc6SDave Kleikamp * Generic filesystem journal-writing code; part of the ext2fs 13470decc6SDave Kleikamp * journaling system. 14470decc6SDave Kleikamp * 15470decc6SDave Kleikamp * This file manages journals: areas of disk reserved for logging 16470decc6SDave Kleikamp * transactional updates. This includes the kernel journaling thread 17470decc6SDave Kleikamp * which is responsible for scheduling updates to the log. 18470decc6SDave Kleikamp * 19470decc6SDave Kleikamp * We do not actually manage the physical storage of the journal in this 20470decc6SDave Kleikamp * file: that is left to a per-journal policy function, which allows us 21470decc6SDave Kleikamp * to store the journal within a filesystem-specified area for ext2 22470decc6SDave Kleikamp * journaling (ext2 can use a reserved inode for storing the log). 23470decc6SDave Kleikamp */ 24470decc6SDave Kleikamp 25470decc6SDave Kleikamp #include <linux/module.h> 26470decc6SDave Kleikamp #include <linux/time.h> 27470decc6SDave Kleikamp #include <linux/fs.h> 28f7f4bccbSMingming Cao #include <linux/jbd2.h> 29470decc6SDave Kleikamp #include <linux/errno.h> 30470decc6SDave Kleikamp #include <linux/slab.h> 31470decc6SDave Kleikamp #include <linux/init.h> 32470decc6SDave Kleikamp #include <linux/mm.h> 337dfb7103SNigel Cunningham #include <linux/freezer.h> 34470decc6SDave Kleikamp #include <linux/pagemap.h> 35470decc6SDave Kleikamp #include <linux/kthread.h> 36470decc6SDave Kleikamp #include <linux/poison.h> 37470decc6SDave Kleikamp #include <linux/proc_fs.h> 380f49d5d0SJose R. Santos #include <linux/debugfs.h> 398e85fb3fSJohann Lombardi #include <linux/seq_file.h> 40c225aa57SSimon Holm Thøgersen #include <linux/math64.h> 41879c5e6bSTheodore Ts'o #include <linux/hash.h> 42d2eecb03STheodore Ts'o #include <linux/log2.h> 43d2eecb03STheodore Ts'o #include <linux/vmalloc.h> 4447def826STheodore Ts'o #include <linux/backing-dev.h> 4539e3ac25SBrian King #include <linux/bitops.h> 46670be5a7STheodore Ts'o #include <linux/ratelimit.h> 47879c5e6bSTheodore Ts'o 48879c5e6bSTheodore Ts'o #define CREATE_TRACE_POINTS 49879c5e6bSTheodore Ts'o #include <trace/events/jbd2.h> 50470decc6SDave Kleikamp 51470decc6SDave Kleikamp #include <asm/uaccess.h> 52470decc6SDave Kleikamp #include <asm/page.h> 53470decc6SDave Kleikamp 54f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_extend); 55f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_stop); 56f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_lock_updates); 57f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_unlock_updates); 58f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_get_write_access); 59f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_get_create_access); 60f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_get_undo_access); 61e06c8227SJoel Becker EXPORT_SYMBOL(jbd2_journal_set_triggers); 62f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_dirty_metadata); 63f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_forget); 64470decc6SDave Kleikamp #if 0 65470decc6SDave Kleikamp EXPORT_SYMBOL(journal_sync_buffer); 66470decc6SDave Kleikamp #endif 67f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_flush); 68f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_revoke); 69470decc6SDave Kleikamp 70f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_init_dev); 71f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_init_inode); 72f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_check_used_features); 73f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_check_available_features); 74f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_set_features); 75f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_load); 76f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_destroy); 77f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_abort); 78f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_errno); 79f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_ack_err); 80f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_clear_err); 81f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_log_wait_commit); 823b799d15STheodore Ts'o EXPORT_SYMBOL(jbd2_log_start_commit); 83f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_start_commit); 84f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_force_commit_nested); 85f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_wipe); 86f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_blocks_per_page); 87f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_invalidatepage); 88f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers); 89f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_force_commit); 90c851ed54SJan Kara EXPORT_SYMBOL(jbd2_journal_file_inode); 91c851ed54SJan Kara EXPORT_SYMBOL(jbd2_journal_init_jbd_inode); 92c851ed54SJan Kara EXPORT_SYMBOL(jbd2_journal_release_jbd_inode); 93c851ed54SJan Kara EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate); 948aefcd55STheodore Ts'o EXPORT_SYMBOL(jbd2_inode_cache); 95470decc6SDave Kleikamp 96470decc6SDave Kleikamp static void __journal_abort_soft (journal_t *journal, int errno); 97d2eecb03STheodore Ts'o static int jbd2_journal_create_slab(size_t slab_size); 98470decc6SDave Kleikamp 9925ed6e8aSDarrick J. Wong /* Checksumming functions */ 10025ed6e8aSDarrick J. Wong int jbd2_verify_csum_type(journal_t *j, journal_superblock_t *sb) 10125ed6e8aSDarrick J. Wong { 10225ed6e8aSDarrick J. Wong if (!JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_CSUM_V2)) 10325ed6e8aSDarrick J. Wong return 1; 10425ed6e8aSDarrick J. Wong 10525ed6e8aSDarrick J. Wong return sb->s_checksum_type == JBD2_CRC32C_CHKSUM; 10625ed6e8aSDarrick J. Wong } 10725ed6e8aSDarrick J. Wong 1084fd5ea43SDarrick J. Wong static __u32 jbd2_superblock_csum(journal_t *j, journal_superblock_t *sb) 1094fd5ea43SDarrick J. Wong { 1104fd5ea43SDarrick J. Wong __u32 csum, old_csum; 1114fd5ea43SDarrick J. Wong 1124fd5ea43SDarrick J. Wong old_csum = sb->s_checksum; 1134fd5ea43SDarrick J. Wong sb->s_checksum = 0; 1144fd5ea43SDarrick J. Wong csum = jbd2_chksum(j, ~0, (char *)sb, sizeof(journal_superblock_t)); 1154fd5ea43SDarrick J. Wong sb->s_checksum = old_csum; 1164fd5ea43SDarrick J. Wong 1174fd5ea43SDarrick J. Wong return cpu_to_be32(csum); 1184fd5ea43SDarrick J. Wong } 1194fd5ea43SDarrick J. Wong 1204fd5ea43SDarrick J. Wong int jbd2_superblock_csum_verify(journal_t *j, journal_superblock_t *sb) 1214fd5ea43SDarrick J. Wong { 1224fd5ea43SDarrick J. Wong if (!JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_CSUM_V2)) 1234fd5ea43SDarrick J. Wong return 1; 1244fd5ea43SDarrick J. Wong 1254fd5ea43SDarrick J. Wong return sb->s_checksum == jbd2_superblock_csum(j, sb); 1264fd5ea43SDarrick J. Wong } 1274fd5ea43SDarrick J. Wong 1284fd5ea43SDarrick J. Wong void jbd2_superblock_csum_set(journal_t *j, journal_superblock_t *sb) 1294fd5ea43SDarrick J. Wong { 1304fd5ea43SDarrick J. Wong if (!JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_CSUM_V2)) 1314fd5ea43SDarrick J. Wong return; 1324fd5ea43SDarrick J. Wong 1334fd5ea43SDarrick J. Wong sb->s_checksum = jbd2_superblock_csum(j, sb); 1344fd5ea43SDarrick J. Wong } 1354fd5ea43SDarrick J. Wong 136470decc6SDave Kleikamp /* 137470decc6SDave Kleikamp * Helper function used to manage commit timeouts 138470decc6SDave Kleikamp */ 139470decc6SDave Kleikamp 140470decc6SDave Kleikamp static void commit_timeout(unsigned long __data) 141470decc6SDave Kleikamp { 142470decc6SDave Kleikamp struct task_struct * p = (struct task_struct *) __data; 143470decc6SDave Kleikamp 144470decc6SDave Kleikamp wake_up_process(p); 145470decc6SDave Kleikamp } 146470decc6SDave Kleikamp 147470decc6SDave Kleikamp /* 148f7f4bccbSMingming Cao * kjournald2: The main thread function used to manage a logging device 149470decc6SDave Kleikamp * journal. 150470decc6SDave Kleikamp * 151470decc6SDave Kleikamp * This kernel thread is responsible for two things: 152470decc6SDave Kleikamp * 153470decc6SDave Kleikamp * 1) COMMIT: Every so often we need to commit the current state of the 154470decc6SDave Kleikamp * filesystem to disk. The journal thread is responsible for writing 155470decc6SDave Kleikamp * all of the metadata buffers to disk. 156470decc6SDave Kleikamp * 157470decc6SDave Kleikamp * 2) CHECKPOINT: We cannot reuse a used section of the log file until all 158470decc6SDave Kleikamp * of the data in that part of the log has been rewritten elsewhere on 159470decc6SDave Kleikamp * the disk. Flushing these old buffers to reclaim space in the log is 160470decc6SDave Kleikamp * known as checkpointing, and this thread is responsible for that job. 161470decc6SDave Kleikamp */ 162470decc6SDave Kleikamp 163f7f4bccbSMingming Cao static int kjournald2(void *arg) 164470decc6SDave Kleikamp { 165470decc6SDave Kleikamp journal_t *journal = arg; 166470decc6SDave Kleikamp transaction_t *transaction; 167470decc6SDave Kleikamp 168470decc6SDave Kleikamp /* 169470decc6SDave Kleikamp * Set up an interval timer which can be used to trigger a commit wakeup 170470decc6SDave Kleikamp * after the commit interval expires 171470decc6SDave Kleikamp */ 172470decc6SDave Kleikamp setup_timer(&journal->j_commit_timer, commit_timeout, 173470decc6SDave Kleikamp (unsigned long)current); 174470decc6SDave Kleikamp 17535c80422SNigel Cunningham set_freezable(); 17635c80422SNigel Cunningham 177470decc6SDave Kleikamp /* Record that the journal thread is running */ 178470decc6SDave Kleikamp journal->j_task = current; 179470decc6SDave Kleikamp wake_up(&journal->j_wait_done_commit); 180470decc6SDave Kleikamp 181470decc6SDave Kleikamp /* 182470decc6SDave Kleikamp * And now, wait forever for commit wakeup events. 183470decc6SDave Kleikamp */ 184a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 185470decc6SDave Kleikamp 186470decc6SDave Kleikamp loop: 187f7f4bccbSMingming Cao if (journal->j_flags & JBD2_UNMOUNT) 188470decc6SDave Kleikamp goto end_loop; 189470decc6SDave Kleikamp 190470decc6SDave Kleikamp jbd_debug(1, "commit_sequence=%d, commit_request=%d\n", 191470decc6SDave Kleikamp journal->j_commit_sequence, journal->j_commit_request); 192470decc6SDave Kleikamp 193470decc6SDave Kleikamp if (journal->j_commit_sequence != journal->j_commit_request) { 194470decc6SDave Kleikamp jbd_debug(1, "OK, requests differ\n"); 195a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 196470decc6SDave Kleikamp del_timer_sync(&journal->j_commit_timer); 197f7f4bccbSMingming Cao jbd2_journal_commit_transaction(journal); 198a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 199470decc6SDave Kleikamp goto loop; 200470decc6SDave Kleikamp } 201470decc6SDave Kleikamp 202470decc6SDave Kleikamp wake_up(&journal->j_wait_done_commit); 203470decc6SDave Kleikamp if (freezing(current)) { 204470decc6SDave Kleikamp /* 205470decc6SDave Kleikamp * The simpler the better. Flushing journal isn't a 206470decc6SDave Kleikamp * good idea, because that depends on threads that may 207470decc6SDave Kleikamp * be already stopped. 208470decc6SDave Kleikamp */ 209f7f4bccbSMingming Cao jbd_debug(1, "Now suspending kjournald2\n"); 210a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 211a0acae0eSTejun Heo try_to_freeze(); 212a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 213470decc6SDave Kleikamp } else { 214470decc6SDave Kleikamp /* 215470decc6SDave Kleikamp * We assume on resume that commits are already there, 216470decc6SDave Kleikamp * so we don't sleep 217470decc6SDave Kleikamp */ 218470decc6SDave Kleikamp DEFINE_WAIT(wait); 219470decc6SDave Kleikamp int should_sleep = 1; 220470decc6SDave Kleikamp 221470decc6SDave Kleikamp prepare_to_wait(&journal->j_wait_commit, &wait, 222470decc6SDave Kleikamp TASK_INTERRUPTIBLE); 223470decc6SDave Kleikamp if (journal->j_commit_sequence != journal->j_commit_request) 224470decc6SDave Kleikamp should_sleep = 0; 225470decc6SDave Kleikamp transaction = journal->j_running_transaction; 226470decc6SDave Kleikamp if (transaction && time_after_eq(jiffies, 227470decc6SDave Kleikamp transaction->t_expires)) 228470decc6SDave Kleikamp should_sleep = 0; 229f7f4bccbSMingming Cao if (journal->j_flags & JBD2_UNMOUNT) 230470decc6SDave Kleikamp should_sleep = 0; 231470decc6SDave Kleikamp if (should_sleep) { 232a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 233470decc6SDave Kleikamp schedule(); 234a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 235470decc6SDave Kleikamp } 236470decc6SDave Kleikamp finish_wait(&journal->j_wait_commit, &wait); 237470decc6SDave Kleikamp } 238470decc6SDave Kleikamp 239f7f4bccbSMingming Cao jbd_debug(1, "kjournald2 wakes\n"); 240470decc6SDave Kleikamp 241470decc6SDave Kleikamp /* 242470decc6SDave Kleikamp * Were we woken up by a commit wakeup event? 243470decc6SDave Kleikamp */ 244470decc6SDave Kleikamp transaction = journal->j_running_transaction; 245470decc6SDave Kleikamp if (transaction && time_after_eq(jiffies, transaction->t_expires)) { 246470decc6SDave Kleikamp journal->j_commit_request = transaction->t_tid; 247470decc6SDave Kleikamp jbd_debug(1, "woke because of timeout\n"); 248470decc6SDave Kleikamp } 249470decc6SDave Kleikamp goto loop; 250470decc6SDave Kleikamp 251470decc6SDave Kleikamp end_loop: 252a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 253470decc6SDave Kleikamp del_timer_sync(&journal->j_commit_timer); 254470decc6SDave Kleikamp journal->j_task = NULL; 255470decc6SDave Kleikamp wake_up(&journal->j_wait_done_commit); 256470decc6SDave Kleikamp jbd_debug(1, "Journal thread exiting.\n"); 257470decc6SDave Kleikamp return 0; 258470decc6SDave Kleikamp } 259470decc6SDave Kleikamp 26097f06784SPavel Emelianov static int jbd2_journal_start_thread(journal_t *journal) 261470decc6SDave Kleikamp { 26297f06784SPavel Emelianov struct task_struct *t; 26397f06784SPavel Emelianov 26490576c0bSTheodore Ts'o t = kthread_run(kjournald2, journal, "jbd2/%s", 26590576c0bSTheodore Ts'o journal->j_devname); 26697f06784SPavel Emelianov if (IS_ERR(t)) 26797f06784SPavel Emelianov return PTR_ERR(t); 26897f06784SPavel Emelianov 2691076d17aSAl Viro wait_event(journal->j_wait_done_commit, journal->j_task != NULL); 27097f06784SPavel Emelianov return 0; 271470decc6SDave Kleikamp } 272470decc6SDave Kleikamp 273470decc6SDave Kleikamp static void journal_kill_thread(journal_t *journal) 274470decc6SDave Kleikamp { 275a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 276f7f4bccbSMingming Cao journal->j_flags |= JBD2_UNMOUNT; 277470decc6SDave Kleikamp 278470decc6SDave Kleikamp while (journal->j_task) { 279470decc6SDave Kleikamp wake_up(&journal->j_wait_commit); 280a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 2811076d17aSAl Viro wait_event(journal->j_wait_done_commit, journal->j_task == NULL); 282a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 283470decc6SDave Kleikamp } 284a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 285470decc6SDave Kleikamp } 286470decc6SDave Kleikamp 287470decc6SDave Kleikamp /* 288f7f4bccbSMingming Cao * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal. 289470decc6SDave Kleikamp * 290470decc6SDave Kleikamp * Writes a metadata buffer to a given disk block. The actual IO is not 291470decc6SDave Kleikamp * performed but a new buffer_head is constructed which labels the data 292470decc6SDave Kleikamp * to be written with the correct destination disk block. 293470decc6SDave Kleikamp * 294470decc6SDave Kleikamp * Any magic-number escaping which needs to be done will cause a 295470decc6SDave Kleikamp * copy-out here. If the buffer happens to start with the 296f7f4bccbSMingming Cao * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the 297470decc6SDave Kleikamp * magic number is only written to the log for descripter blocks. In 298470decc6SDave Kleikamp * this case, we copy the data and replace the first word with 0, and we 299470decc6SDave Kleikamp * return a result code which indicates that this buffer needs to be 300470decc6SDave Kleikamp * marked as an escaped buffer in the corresponding log descriptor 301470decc6SDave Kleikamp * block. The missing word can then be restored when the block is read 302470decc6SDave Kleikamp * during recovery. 303470decc6SDave Kleikamp * 304470decc6SDave Kleikamp * If the source buffer has already been modified by a new transaction 305470decc6SDave Kleikamp * since we took the last commit snapshot, we use the frozen copy of 306470decc6SDave Kleikamp * that data for IO. If we end up using the existing buffer_head's data 307470decc6SDave Kleikamp * for the write, then we *have* to lock the buffer to prevent anyone 308470decc6SDave Kleikamp * else from using and possibly modifying it while the IO is in 309470decc6SDave Kleikamp * progress. 310470decc6SDave Kleikamp * 311470decc6SDave Kleikamp * The function returns a pointer to the buffer_heads to be used for IO. 312470decc6SDave Kleikamp * 313470decc6SDave Kleikamp * We assume that the journal has already been locked in this function. 314470decc6SDave Kleikamp * 315470decc6SDave Kleikamp * Return value: 316470decc6SDave Kleikamp * <0: Error 317470decc6SDave Kleikamp * >=0: Finished OK 318470decc6SDave Kleikamp * 319470decc6SDave Kleikamp * On success: 320470decc6SDave Kleikamp * Bit 0 set == escape performed on the data 321470decc6SDave Kleikamp * Bit 1 set == buffer copy-out performed (kfree the data after IO) 322470decc6SDave Kleikamp */ 323470decc6SDave Kleikamp 324f7f4bccbSMingming Cao int jbd2_journal_write_metadata_buffer(transaction_t *transaction, 325470decc6SDave Kleikamp struct journal_head *jh_in, 326470decc6SDave Kleikamp struct journal_head **jh_out, 32718eba7aaSMingming Cao unsigned long long blocknr) 328470decc6SDave Kleikamp { 329470decc6SDave Kleikamp int need_copy_out = 0; 330470decc6SDave Kleikamp int done_copy_out = 0; 331470decc6SDave Kleikamp int do_escape = 0; 332470decc6SDave Kleikamp char *mapped_data; 333470decc6SDave Kleikamp struct buffer_head *new_bh; 334470decc6SDave Kleikamp struct journal_head *new_jh; 335470decc6SDave Kleikamp struct page *new_page; 336470decc6SDave Kleikamp unsigned int new_offset; 337470decc6SDave Kleikamp struct buffer_head *bh_in = jh2bh(jh_in); 33896577c43Sdingdinghua journal_t *journal = transaction->t_journal; 339470decc6SDave Kleikamp 340470decc6SDave Kleikamp /* 341470decc6SDave Kleikamp * The buffer really shouldn't be locked: only the current committing 342470decc6SDave Kleikamp * transaction is allowed to write it, so nobody else is allowed 343470decc6SDave Kleikamp * to do any IO. 344470decc6SDave Kleikamp * 345470decc6SDave Kleikamp * akpm: except if we're journalling data, and write() output is 346470decc6SDave Kleikamp * also part of a shared mapping, and another thread has 347470decc6SDave Kleikamp * decided to launch a writepage() against this buffer. 348470decc6SDave Kleikamp */ 349470decc6SDave Kleikamp J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in)); 350470decc6SDave Kleikamp 35147def826STheodore Ts'o retry_alloc: 35247def826STheodore Ts'o new_bh = alloc_buffer_head(GFP_NOFS); 35347def826STheodore Ts'o if (!new_bh) { 35447def826STheodore Ts'o /* 35547def826STheodore Ts'o * Failure is not an option, but __GFP_NOFAIL is going 35647def826STheodore Ts'o * away; so we retry ourselves here. 35747def826STheodore Ts'o */ 35847def826STheodore Ts'o congestion_wait(BLK_RW_ASYNC, HZ/50); 35947def826STheodore Ts'o goto retry_alloc; 36047def826STheodore Ts'o } 36147def826STheodore Ts'o 36296577c43Sdingdinghua /* keep subsequent assertions sane */ 36396577c43Sdingdinghua new_bh->b_state = 0; 36496577c43Sdingdinghua init_buffer(new_bh, NULL, NULL); 36596577c43Sdingdinghua atomic_set(&new_bh->b_count, 1); 36696577c43Sdingdinghua new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */ 367470decc6SDave Kleikamp 368470decc6SDave Kleikamp /* 369470decc6SDave Kleikamp * If a new transaction has already done a buffer copy-out, then 370470decc6SDave Kleikamp * we use that version of the data for the commit. 371470decc6SDave Kleikamp */ 372470decc6SDave Kleikamp jbd_lock_bh_state(bh_in); 373470decc6SDave Kleikamp repeat: 374470decc6SDave Kleikamp if (jh_in->b_frozen_data) { 375470decc6SDave Kleikamp done_copy_out = 1; 376470decc6SDave Kleikamp new_page = virt_to_page(jh_in->b_frozen_data); 377470decc6SDave Kleikamp new_offset = offset_in_page(jh_in->b_frozen_data); 378470decc6SDave Kleikamp } else { 379470decc6SDave Kleikamp new_page = jh2bh(jh_in)->b_page; 380470decc6SDave Kleikamp new_offset = offset_in_page(jh2bh(jh_in)->b_data); 381470decc6SDave Kleikamp } 382470decc6SDave Kleikamp 383303a8f2aSCong Wang mapped_data = kmap_atomic(new_page); 384470decc6SDave Kleikamp /* 38513ceef09SJan Kara * Fire data frozen trigger if data already wasn't frozen. Do this 38613ceef09SJan Kara * before checking for escaping, as the trigger may modify the magic 38713ceef09SJan Kara * offset. If a copy-out happens afterwards, it will have the correct 38813ceef09SJan Kara * data in the buffer. 389e06c8227SJoel Becker */ 39013ceef09SJan Kara if (!done_copy_out) 39113ceef09SJan Kara jbd2_buffer_frozen_trigger(jh_in, mapped_data + new_offset, 39213ceef09SJan Kara jh_in->b_triggers); 393e06c8227SJoel Becker 394e06c8227SJoel Becker /* 395470decc6SDave Kleikamp * Check for escaping 396470decc6SDave Kleikamp */ 397470decc6SDave Kleikamp if (*((__be32 *)(mapped_data + new_offset)) == 398f7f4bccbSMingming Cao cpu_to_be32(JBD2_MAGIC_NUMBER)) { 399470decc6SDave Kleikamp need_copy_out = 1; 400470decc6SDave Kleikamp do_escape = 1; 401470decc6SDave Kleikamp } 402303a8f2aSCong Wang kunmap_atomic(mapped_data); 403470decc6SDave Kleikamp 404470decc6SDave Kleikamp /* 405470decc6SDave Kleikamp * Do we need to do a data copy? 406470decc6SDave Kleikamp */ 407470decc6SDave Kleikamp if (need_copy_out && !done_copy_out) { 408470decc6SDave Kleikamp char *tmp; 409470decc6SDave Kleikamp 410470decc6SDave Kleikamp jbd_unlock_bh_state(bh_in); 411af1e76d6SMingming Cao tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS); 412e6ec116bSTheodore Ts'o if (!tmp) { 413e6ec116bSTheodore Ts'o jbd2_journal_put_journal_head(new_jh); 414e6ec116bSTheodore Ts'o return -ENOMEM; 415e6ec116bSTheodore Ts'o } 416470decc6SDave Kleikamp jbd_lock_bh_state(bh_in); 417470decc6SDave Kleikamp if (jh_in->b_frozen_data) { 418af1e76d6SMingming Cao jbd2_free(tmp, bh_in->b_size); 419470decc6SDave Kleikamp goto repeat; 420470decc6SDave Kleikamp } 421470decc6SDave Kleikamp 422470decc6SDave Kleikamp jh_in->b_frozen_data = tmp; 423303a8f2aSCong Wang mapped_data = kmap_atomic(new_page); 424470decc6SDave Kleikamp memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size); 425303a8f2aSCong Wang kunmap_atomic(mapped_data); 426470decc6SDave Kleikamp 427470decc6SDave Kleikamp new_page = virt_to_page(tmp); 428470decc6SDave Kleikamp new_offset = offset_in_page(tmp); 429470decc6SDave Kleikamp done_copy_out = 1; 430e06c8227SJoel Becker 431e06c8227SJoel Becker /* 432e06c8227SJoel Becker * This isn't strictly necessary, as we're using frozen 433e06c8227SJoel Becker * data for the escaping, but it keeps consistency with 434e06c8227SJoel Becker * b_frozen_data usage. 435e06c8227SJoel Becker */ 436e06c8227SJoel Becker jh_in->b_frozen_triggers = jh_in->b_triggers; 437470decc6SDave Kleikamp } 438470decc6SDave Kleikamp 439470decc6SDave Kleikamp /* 440470decc6SDave Kleikamp * Did we need to do an escaping? Now we've done all the 441470decc6SDave Kleikamp * copying, we can finally do so. 442470decc6SDave Kleikamp */ 443470decc6SDave Kleikamp if (do_escape) { 444303a8f2aSCong Wang mapped_data = kmap_atomic(new_page); 445470decc6SDave Kleikamp *((unsigned int *)(mapped_data + new_offset)) = 0; 446303a8f2aSCong Wang kunmap_atomic(mapped_data); 447470decc6SDave Kleikamp } 448470decc6SDave Kleikamp 449470decc6SDave Kleikamp set_bh_page(new_bh, new_page, new_offset); 450470decc6SDave Kleikamp new_jh->b_transaction = NULL; 451470decc6SDave Kleikamp new_bh->b_size = jh2bh(jh_in)->b_size; 452470decc6SDave Kleikamp new_bh->b_bdev = transaction->t_journal->j_dev; 453470decc6SDave Kleikamp new_bh->b_blocknr = blocknr; 454470decc6SDave Kleikamp set_buffer_mapped(new_bh); 455470decc6SDave Kleikamp set_buffer_dirty(new_bh); 456470decc6SDave Kleikamp 457470decc6SDave Kleikamp *jh_out = new_jh; 458470decc6SDave Kleikamp 459470decc6SDave Kleikamp /* 460470decc6SDave Kleikamp * The to-be-written buffer needs to get moved to the io queue, 461470decc6SDave Kleikamp * and the original buffer whose contents we are shadowing or 462470decc6SDave Kleikamp * copying is moved to the transaction's shadow queue. 463470decc6SDave Kleikamp */ 464470decc6SDave Kleikamp JBUFFER_TRACE(jh_in, "file as BJ_Shadow"); 46596577c43Sdingdinghua spin_lock(&journal->j_list_lock); 46696577c43Sdingdinghua __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow); 46796577c43Sdingdinghua spin_unlock(&journal->j_list_lock); 46896577c43Sdingdinghua jbd_unlock_bh_state(bh_in); 46996577c43Sdingdinghua 470470decc6SDave Kleikamp JBUFFER_TRACE(new_jh, "file as BJ_IO"); 471f7f4bccbSMingming Cao jbd2_journal_file_buffer(new_jh, transaction, BJ_IO); 472470decc6SDave Kleikamp 473470decc6SDave Kleikamp return do_escape | (done_copy_out << 1); 474470decc6SDave Kleikamp } 475470decc6SDave Kleikamp 476470decc6SDave Kleikamp /* 477470decc6SDave Kleikamp * Allocation code for the journal file. Manage the space left in the 478470decc6SDave Kleikamp * journal, so that we can begin checkpointing when appropriate. 479470decc6SDave Kleikamp */ 480470decc6SDave Kleikamp 481470decc6SDave Kleikamp /* 482f7f4bccbSMingming Cao * __jbd2_log_space_left: Return the number of free blocks left in the journal. 483470decc6SDave Kleikamp * 484470decc6SDave Kleikamp * Called with the journal already locked. 485470decc6SDave Kleikamp * 486470decc6SDave Kleikamp * Called under j_state_lock 487470decc6SDave Kleikamp */ 488470decc6SDave Kleikamp 489f7f4bccbSMingming Cao int __jbd2_log_space_left(journal_t *journal) 490470decc6SDave Kleikamp { 491470decc6SDave Kleikamp int left = journal->j_free; 492470decc6SDave Kleikamp 493a931da6aSTheodore Ts'o /* assert_spin_locked(&journal->j_state_lock); */ 494470decc6SDave Kleikamp 495470decc6SDave Kleikamp /* 496470decc6SDave Kleikamp * Be pessimistic here about the number of those free blocks which 497470decc6SDave Kleikamp * might be required for log descriptor control blocks. 498470decc6SDave Kleikamp */ 499470decc6SDave Kleikamp 500470decc6SDave Kleikamp #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */ 501470decc6SDave Kleikamp 502470decc6SDave Kleikamp left -= MIN_LOG_RESERVED_BLOCKS; 503470decc6SDave Kleikamp 504470decc6SDave Kleikamp if (left <= 0) 505470decc6SDave Kleikamp return 0; 506470decc6SDave Kleikamp left -= (left >> 3); 507470decc6SDave Kleikamp return left; 508470decc6SDave Kleikamp } 509470decc6SDave Kleikamp 510470decc6SDave Kleikamp /* 511e4471831STheodore Ts'o * Called with j_state_lock locked for writing. 512e4471831STheodore Ts'o * Returns true if a transaction commit was started. 513470decc6SDave Kleikamp */ 514f7f4bccbSMingming Cao int __jbd2_log_start_commit(journal_t *journal, tid_t target) 515470decc6SDave Kleikamp { 516*e7b04ac0SEric Sandeen /* Return if the txn has already requested to be committed */ 517*e7b04ac0SEric Sandeen if (journal->j_commit_request == target) 518*e7b04ac0SEric Sandeen return 0; 519*e7b04ac0SEric Sandeen 520470decc6SDave Kleikamp /* 521deeeaf13STheodore Ts'o * The only transaction we can possibly wait upon is the 522deeeaf13STheodore Ts'o * currently running transaction (if it exists). Otherwise, 523deeeaf13STheodore Ts'o * the target tid must be an old one. 524470decc6SDave Kleikamp */ 525deeeaf13STheodore Ts'o if (journal->j_running_transaction && 526deeeaf13STheodore Ts'o journal->j_running_transaction->t_tid == target) { 527470decc6SDave Kleikamp /* 528bcf3d0bcSAndrea Gelmini * We want a new commit: OK, mark the request and wakeup the 529470decc6SDave Kleikamp * commit thread. We do _not_ do the commit ourselves. 530470decc6SDave Kleikamp */ 531470decc6SDave Kleikamp 532470decc6SDave Kleikamp journal->j_commit_request = target; 533f2a44523SEryu Guan jbd_debug(1, "JBD2: requesting commit %d/%d\n", 534470decc6SDave Kleikamp journal->j_commit_request, 535470decc6SDave Kleikamp journal->j_commit_sequence); 536470decc6SDave Kleikamp wake_up(&journal->j_wait_commit); 537470decc6SDave Kleikamp return 1; 538deeeaf13STheodore Ts'o } else if (!tid_geq(journal->j_commit_request, target)) 539deeeaf13STheodore Ts'o /* This should never happen, but if it does, preserve 540deeeaf13STheodore Ts'o the evidence before kjournald goes into a loop and 541deeeaf13STheodore Ts'o increments j_commit_sequence beyond all recognition. */ 542f2a44523SEryu Guan WARN_ONCE(1, "JBD2: bad log_start_commit: %u %u %u %u\n", 5431be2add6STheodore Ts'o journal->j_commit_request, 5441be2add6STheodore Ts'o journal->j_commit_sequence, 545deeeaf13STheodore Ts'o target, journal->j_running_transaction ? 546deeeaf13STheodore Ts'o journal->j_running_transaction->t_tid : 0); 547470decc6SDave Kleikamp return 0; 548470decc6SDave Kleikamp } 549470decc6SDave Kleikamp 550f7f4bccbSMingming Cao int jbd2_log_start_commit(journal_t *journal, tid_t tid) 551470decc6SDave Kleikamp { 552470decc6SDave Kleikamp int ret; 553470decc6SDave Kleikamp 554a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 555f7f4bccbSMingming Cao ret = __jbd2_log_start_commit(journal, tid); 556a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 557470decc6SDave Kleikamp return ret; 558470decc6SDave Kleikamp } 559470decc6SDave Kleikamp 560470decc6SDave Kleikamp /* 561470decc6SDave Kleikamp * Force and wait upon a commit if the calling process is not within 562470decc6SDave Kleikamp * transaction. This is used for forcing out undo-protected data which contains 563470decc6SDave Kleikamp * bitmaps, when the fs is running out of space. 564470decc6SDave Kleikamp * 565470decc6SDave Kleikamp * We can only force the running transaction if we don't have an active handle; 566470decc6SDave Kleikamp * otherwise, we will deadlock. 567470decc6SDave Kleikamp * 568470decc6SDave Kleikamp * Returns true if a transaction was started. 569470decc6SDave Kleikamp */ 570f7f4bccbSMingming Cao int jbd2_journal_force_commit_nested(journal_t *journal) 571470decc6SDave Kleikamp { 572470decc6SDave Kleikamp transaction_t *transaction = NULL; 573470decc6SDave Kleikamp tid_t tid; 574e4471831STheodore Ts'o int need_to_start = 0; 575470decc6SDave Kleikamp 576a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 577470decc6SDave Kleikamp if (journal->j_running_transaction && !current->journal_info) { 578470decc6SDave Kleikamp transaction = journal->j_running_transaction; 579e4471831STheodore Ts'o if (!tid_geq(journal->j_commit_request, transaction->t_tid)) 580e4471831STheodore Ts'o need_to_start = 1; 581470decc6SDave Kleikamp } else if (journal->j_committing_transaction) 582470decc6SDave Kleikamp transaction = journal->j_committing_transaction; 583470decc6SDave Kleikamp 584470decc6SDave Kleikamp if (!transaction) { 585a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 586470decc6SDave Kleikamp return 0; /* Nothing to retry */ 587470decc6SDave Kleikamp } 588470decc6SDave Kleikamp 589470decc6SDave Kleikamp tid = transaction->t_tid; 590a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 591e4471831STheodore Ts'o if (need_to_start) 592e4471831STheodore Ts'o jbd2_log_start_commit(journal, tid); 593f7f4bccbSMingming Cao jbd2_log_wait_commit(journal, tid); 594470decc6SDave Kleikamp return 1; 595470decc6SDave Kleikamp } 596470decc6SDave Kleikamp 597470decc6SDave Kleikamp /* 598470decc6SDave Kleikamp * Start a commit of the current running transaction (if any). Returns true 599c88ccea3SJan Kara * if a transaction is going to be committed (or is currently already 600c88ccea3SJan Kara * committing), and fills its tid in at *ptid 601470decc6SDave Kleikamp */ 602f7f4bccbSMingming Cao int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid) 603470decc6SDave Kleikamp { 604470decc6SDave Kleikamp int ret = 0; 605470decc6SDave Kleikamp 606a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 607470decc6SDave Kleikamp if (journal->j_running_transaction) { 608470decc6SDave Kleikamp tid_t tid = journal->j_running_transaction->t_tid; 609470decc6SDave Kleikamp 610c88ccea3SJan Kara __jbd2_log_start_commit(journal, tid); 611c88ccea3SJan Kara /* There's a running transaction and we've just made sure 612c88ccea3SJan Kara * it's commit has been scheduled. */ 613c88ccea3SJan Kara if (ptid) 614470decc6SDave Kleikamp *ptid = tid; 615c88ccea3SJan Kara ret = 1; 616c88ccea3SJan Kara } else if (journal->j_committing_transaction) { 617470decc6SDave Kleikamp /* 61812810ad7SArtem Bityutskiy * If commit has been started, then we have to wait for 61912810ad7SArtem Bityutskiy * completion of that transaction. 620470decc6SDave Kleikamp */ 621c88ccea3SJan Kara if (ptid) 622470decc6SDave Kleikamp *ptid = journal->j_committing_transaction->t_tid; 623470decc6SDave Kleikamp ret = 1; 624470decc6SDave Kleikamp } 625a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 626470decc6SDave Kleikamp return ret; 627470decc6SDave Kleikamp } 628470decc6SDave Kleikamp 629470decc6SDave Kleikamp /* 630bbd2be36SJan Kara * Return 1 if a given transaction has not yet sent barrier request 631bbd2be36SJan Kara * connected with a transaction commit. If 0 is returned, transaction 632bbd2be36SJan Kara * may or may not have sent the barrier. Used to avoid sending barrier 633bbd2be36SJan Kara * twice in common cases. 634bbd2be36SJan Kara */ 635bbd2be36SJan Kara int jbd2_trans_will_send_data_barrier(journal_t *journal, tid_t tid) 636bbd2be36SJan Kara { 637bbd2be36SJan Kara int ret = 0; 638bbd2be36SJan Kara transaction_t *commit_trans; 639bbd2be36SJan Kara 640bbd2be36SJan Kara if (!(journal->j_flags & JBD2_BARRIER)) 641bbd2be36SJan Kara return 0; 642bbd2be36SJan Kara read_lock(&journal->j_state_lock); 643bbd2be36SJan Kara /* Transaction already committed? */ 644bbd2be36SJan Kara if (tid_geq(journal->j_commit_sequence, tid)) 645bbd2be36SJan Kara goto out; 646bbd2be36SJan Kara commit_trans = journal->j_committing_transaction; 647bbd2be36SJan Kara if (!commit_trans || commit_trans->t_tid != tid) { 648bbd2be36SJan Kara ret = 1; 649bbd2be36SJan Kara goto out; 650bbd2be36SJan Kara } 651bbd2be36SJan Kara /* 652bbd2be36SJan Kara * Transaction is being committed and we already proceeded to 653bbd2be36SJan Kara * submitting a flush to fs partition? 654bbd2be36SJan Kara */ 655bbd2be36SJan Kara if (journal->j_fs_dev != journal->j_dev) { 656bbd2be36SJan Kara if (!commit_trans->t_need_data_flush || 657bbd2be36SJan Kara commit_trans->t_state >= T_COMMIT_DFLUSH) 658bbd2be36SJan Kara goto out; 659bbd2be36SJan Kara } else { 660bbd2be36SJan Kara if (commit_trans->t_state >= T_COMMIT_JFLUSH) 661bbd2be36SJan Kara goto out; 662bbd2be36SJan Kara } 663bbd2be36SJan Kara ret = 1; 664bbd2be36SJan Kara out: 665bbd2be36SJan Kara read_unlock(&journal->j_state_lock); 666bbd2be36SJan Kara return ret; 667bbd2be36SJan Kara } 668bbd2be36SJan Kara EXPORT_SYMBOL(jbd2_trans_will_send_data_barrier); 669bbd2be36SJan Kara 670bbd2be36SJan Kara /* 671470decc6SDave Kleikamp * Wait for a specified commit to complete. 672470decc6SDave Kleikamp * The caller may not hold the journal lock. 673470decc6SDave Kleikamp */ 674f7f4bccbSMingming Cao int jbd2_log_wait_commit(journal_t *journal, tid_t tid) 675470decc6SDave Kleikamp { 676470decc6SDave Kleikamp int err = 0; 677470decc6SDave Kleikamp 678a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 679e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 680470decc6SDave Kleikamp if (!tid_geq(journal->j_commit_request, tid)) { 681470decc6SDave Kleikamp printk(KERN_EMERG 682470decc6SDave Kleikamp "%s: error: j_commit_request=%d, tid=%d\n", 683329d291fSHarvey Harrison __func__, journal->j_commit_request, tid); 684470decc6SDave Kleikamp } 685470decc6SDave Kleikamp #endif 686470decc6SDave Kleikamp while (tid_gt(tid, journal->j_commit_sequence)) { 687f2a44523SEryu Guan jbd_debug(1, "JBD2: want %d, j_commit_sequence=%d\n", 688470decc6SDave Kleikamp tid, journal->j_commit_sequence); 689470decc6SDave Kleikamp wake_up(&journal->j_wait_commit); 690a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 691470decc6SDave Kleikamp wait_event(journal->j_wait_done_commit, 692470decc6SDave Kleikamp !tid_gt(tid, journal->j_commit_sequence)); 693a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 694470decc6SDave Kleikamp } 695a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 696470decc6SDave Kleikamp 697470decc6SDave Kleikamp if (unlikely(is_journal_aborted(journal))) { 698470decc6SDave Kleikamp printk(KERN_EMERG "journal commit I/O error\n"); 699470decc6SDave Kleikamp err = -EIO; 700470decc6SDave Kleikamp } 701470decc6SDave Kleikamp return err; 702470decc6SDave Kleikamp } 703470decc6SDave Kleikamp 704470decc6SDave Kleikamp /* 705470decc6SDave Kleikamp * Log buffer allocation routines: 706470decc6SDave Kleikamp */ 707470decc6SDave Kleikamp 70818eba7aaSMingming Cao int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp) 709470decc6SDave Kleikamp { 710470decc6SDave Kleikamp unsigned long blocknr; 711470decc6SDave Kleikamp 712a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 713470decc6SDave Kleikamp J_ASSERT(journal->j_free > 1); 714470decc6SDave Kleikamp 715470decc6SDave Kleikamp blocknr = journal->j_head; 716470decc6SDave Kleikamp journal->j_head++; 717470decc6SDave Kleikamp journal->j_free--; 718470decc6SDave Kleikamp if (journal->j_head == journal->j_last) 719470decc6SDave Kleikamp journal->j_head = journal->j_first; 720a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 721f7f4bccbSMingming Cao return jbd2_journal_bmap(journal, blocknr, retp); 722470decc6SDave Kleikamp } 723470decc6SDave Kleikamp 724470decc6SDave Kleikamp /* 725470decc6SDave Kleikamp * Conversion of logical to physical block numbers for the journal 726470decc6SDave Kleikamp * 727470decc6SDave Kleikamp * On external journals the journal blocks are identity-mapped, so 728470decc6SDave Kleikamp * this is a no-op. If needed, we can use j_blk_offset - everything is 729470decc6SDave Kleikamp * ready. 730470decc6SDave Kleikamp */ 731f7f4bccbSMingming Cao int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr, 73218eba7aaSMingming Cao unsigned long long *retp) 733470decc6SDave Kleikamp { 734470decc6SDave Kleikamp int err = 0; 73518eba7aaSMingming Cao unsigned long long ret; 736470decc6SDave Kleikamp 737470decc6SDave Kleikamp if (journal->j_inode) { 738470decc6SDave Kleikamp ret = bmap(journal->j_inode, blocknr); 739470decc6SDave Kleikamp if (ret) 740470decc6SDave Kleikamp *retp = ret; 741470decc6SDave Kleikamp else { 742470decc6SDave Kleikamp printk(KERN_ALERT "%s: journal block not found " 743470decc6SDave Kleikamp "at offset %lu on %s\n", 74405496769STheodore Ts'o __func__, blocknr, journal->j_devname); 745470decc6SDave Kleikamp err = -EIO; 746470decc6SDave Kleikamp __journal_abort_soft(journal, err); 747470decc6SDave Kleikamp } 748470decc6SDave Kleikamp } else { 749470decc6SDave Kleikamp *retp = blocknr; /* +journal->j_blk_offset */ 750470decc6SDave Kleikamp } 751470decc6SDave Kleikamp return err; 752470decc6SDave Kleikamp } 753470decc6SDave Kleikamp 754470decc6SDave Kleikamp /* 755470decc6SDave Kleikamp * We play buffer_head aliasing tricks to write data/metadata blocks to 756470decc6SDave Kleikamp * the journal without copying their contents, but for journal 757470decc6SDave Kleikamp * descriptor blocks we do need to generate bona fide buffers. 758470decc6SDave Kleikamp * 759f7f4bccbSMingming Cao * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying 760470decc6SDave Kleikamp * the buffer's contents they really should run flush_dcache_page(bh->b_page). 761470decc6SDave Kleikamp * But we don't bother doing that, so there will be coherency problems with 762470decc6SDave Kleikamp * mmaps of blockdevs which hold live JBD-controlled filesystems. 763470decc6SDave Kleikamp */ 764f7f4bccbSMingming Cao struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal) 765470decc6SDave Kleikamp { 766470decc6SDave Kleikamp struct buffer_head *bh; 76718eba7aaSMingming Cao unsigned long long blocknr; 768470decc6SDave Kleikamp int err; 769470decc6SDave Kleikamp 770f7f4bccbSMingming Cao err = jbd2_journal_next_log_block(journal, &blocknr); 771470decc6SDave Kleikamp 772470decc6SDave Kleikamp if (err) 773470decc6SDave Kleikamp return NULL; 774470decc6SDave Kleikamp 775470decc6SDave Kleikamp bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize); 7764b905671SJan Kara if (!bh) 7774b905671SJan Kara return NULL; 778470decc6SDave Kleikamp lock_buffer(bh); 779470decc6SDave Kleikamp memset(bh->b_data, 0, journal->j_blocksize); 780470decc6SDave Kleikamp set_buffer_uptodate(bh); 781470decc6SDave Kleikamp unlock_buffer(bh); 782470decc6SDave Kleikamp BUFFER_TRACE(bh, "return this buffer"); 783f7f4bccbSMingming Cao return jbd2_journal_add_journal_head(bh); 784470decc6SDave Kleikamp } 785470decc6SDave Kleikamp 78679feb521SJan Kara /* 78779feb521SJan Kara * Return tid of the oldest transaction in the journal and block in the journal 78879feb521SJan Kara * where the transaction starts. 78979feb521SJan Kara * 79079feb521SJan Kara * If the journal is now empty, return which will be the next transaction ID 79179feb521SJan Kara * we will write and where will that transaction start. 79279feb521SJan Kara * 79379feb521SJan Kara * The return value is 0 if journal tail cannot be pushed any further, 1 if 79479feb521SJan Kara * it can. 79579feb521SJan Kara */ 79679feb521SJan Kara int jbd2_journal_get_log_tail(journal_t *journal, tid_t *tid, 79779feb521SJan Kara unsigned long *block) 79879feb521SJan Kara { 79979feb521SJan Kara transaction_t *transaction; 80079feb521SJan Kara int ret; 80179feb521SJan Kara 80279feb521SJan Kara read_lock(&journal->j_state_lock); 80379feb521SJan Kara spin_lock(&journal->j_list_lock); 80479feb521SJan Kara transaction = journal->j_checkpoint_transactions; 80579feb521SJan Kara if (transaction) { 80679feb521SJan Kara *tid = transaction->t_tid; 80779feb521SJan Kara *block = transaction->t_log_start; 80879feb521SJan Kara } else if ((transaction = journal->j_committing_transaction) != NULL) { 80979feb521SJan Kara *tid = transaction->t_tid; 81079feb521SJan Kara *block = transaction->t_log_start; 81179feb521SJan Kara } else if ((transaction = journal->j_running_transaction) != NULL) { 81279feb521SJan Kara *tid = transaction->t_tid; 81379feb521SJan Kara *block = journal->j_head; 81479feb521SJan Kara } else { 81579feb521SJan Kara *tid = journal->j_transaction_sequence; 81679feb521SJan Kara *block = journal->j_head; 81779feb521SJan Kara } 81879feb521SJan Kara ret = tid_gt(*tid, journal->j_tail_sequence); 81979feb521SJan Kara spin_unlock(&journal->j_list_lock); 82079feb521SJan Kara read_unlock(&journal->j_state_lock); 82179feb521SJan Kara 82279feb521SJan Kara return ret; 82379feb521SJan Kara } 82479feb521SJan Kara 82579feb521SJan Kara /* 82679feb521SJan Kara * Update information in journal structure and in on disk journal superblock 82779feb521SJan Kara * about log tail. This function does not check whether information passed in 82879feb521SJan Kara * really pushes log tail further. It's responsibility of the caller to make 82979feb521SJan Kara * sure provided log tail information is valid (e.g. by holding 83079feb521SJan Kara * j_checkpoint_mutex all the time between computing log tail and calling this 83179feb521SJan Kara * function as is the case with jbd2_cleanup_journal_tail()). 83279feb521SJan Kara * 83379feb521SJan Kara * Requires j_checkpoint_mutex 83479feb521SJan Kara */ 83579feb521SJan Kara void __jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block) 83679feb521SJan Kara { 83779feb521SJan Kara unsigned long freed; 83879feb521SJan Kara 83979feb521SJan Kara BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex)); 84079feb521SJan Kara 84179feb521SJan Kara /* 84279feb521SJan Kara * We cannot afford for write to remain in drive's caches since as 84379feb521SJan Kara * soon as we update j_tail, next transaction can start reusing journal 84479feb521SJan Kara * space and if we lose sb update during power failure we'd replay 84579feb521SJan Kara * old transaction with possibly newly overwritten data. 84679feb521SJan Kara */ 84779feb521SJan Kara jbd2_journal_update_sb_log_tail(journal, tid, block, WRITE_FUA); 84879feb521SJan Kara write_lock(&journal->j_state_lock); 84979feb521SJan Kara freed = block - journal->j_tail; 85079feb521SJan Kara if (block < journal->j_tail) 85179feb521SJan Kara freed += journal->j_last - journal->j_first; 85279feb521SJan Kara 85379feb521SJan Kara trace_jbd2_update_log_tail(journal, tid, block, freed); 85479feb521SJan Kara jbd_debug(1, 85579feb521SJan Kara "Cleaning journal tail from %d to %d (offset %lu), " 85679feb521SJan Kara "freeing %lu\n", 85779feb521SJan Kara journal->j_tail_sequence, tid, block, freed); 85879feb521SJan Kara 85979feb521SJan Kara journal->j_free += freed; 86079feb521SJan Kara journal->j_tail_sequence = tid; 86179feb521SJan Kara journal->j_tail = block; 86279feb521SJan Kara write_unlock(&journal->j_state_lock); 86379feb521SJan Kara } 86479feb521SJan Kara 8653339578fSJan Kara /* 8663339578fSJan Kara * This is a variaon of __jbd2_update_log_tail which checks for validity of 8673339578fSJan Kara * provided log tail and locks j_checkpoint_mutex. So it is safe against races 8683339578fSJan Kara * with other threads updating log tail. 8693339578fSJan Kara */ 8703339578fSJan Kara void jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block) 8713339578fSJan Kara { 8723339578fSJan Kara mutex_lock(&journal->j_checkpoint_mutex); 8733339578fSJan Kara if (tid_gt(tid, journal->j_tail_sequence)) 8743339578fSJan Kara __jbd2_update_log_tail(journal, tid, block); 8753339578fSJan Kara mutex_unlock(&journal->j_checkpoint_mutex); 8763339578fSJan Kara } 8773339578fSJan Kara 8788e85fb3fSJohann Lombardi struct jbd2_stats_proc_session { 8798e85fb3fSJohann Lombardi journal_t *journal; 8808e85fb3fSJohann Lombardi struct transaction_stats_s *stats; 8818e85fb3fSJohann Lombardi int start; 8828e85fb3fSJohann Lombardi int max; 8838e85fb3fSJohann Lombardi }; 8848e85fb3fSJohann Lombardi 8858e85fb3fSJohann Lombardi static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos) 8868e85fb3fSJohann Lombardi { 8878e85fb3fSJohann Lombardi return *pos ? NULL : SEQ_START_TOKEN; 8888e85fb3fSJohann Lombardi } 8898e85fb3fSJohann Lombardi 8908e85fb3fSJohann Lombardi static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos) 8918e85fb3fSJohann Lombardi { 8928e85fb3fSJohann Lombardi return NULL; 8938e85fb3fSJohann Lombardi } 8948e85fb3fSJohann Lombardi 8958e85fb3fSJohann Lombardi static int jbd2_seq_info_show(struct seq_file *seq, void *v) 8968e85fb3fSJohann Lombardi { 8978e85fb3fSJohann Lombardi struct jbd2_stats_proc_session *s = seq->private; 8988e85fb3fSJohann Lombardi 8998e85fb3fSJohann Lombardi if (v != SEQ_START_TOKEN) 9008e85fb3fSJohann Lombardi return 0; 9018e85fb3fSJohann Lombardi seq_printf(seq, "%lu transaction, each up to %u blocks\n", 9028e85fb3fSJohann Lombardi s->stats->ts_tid, 9038e85fb3fSJohann Lombardi s->journal->j_max_transaction_buffers); 9048e85fb3fSJohann Lombardi if (s->stats->ts_tid == 0) 9058e85fb3fSJohann Lombardi return 0; 9068e85fb3fSJohann Lombardi seq_printf(seq, "average: \n %ums waiting for transaction\n", 907bf699327STheodore Ts'o jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid)); 9088e85fb3fSJohann Lombardi seq_printf(seq, " %ums running transaction\n", 909bf699327STheodore Ts'o jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid)); 9108e85fb3fSJohann Lombardi seq_printf(seq, " %ums transaction was being locked\n", 911bf699327STheodore Ts'o jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid)); 9128e85fb3fSJohann Lombardi seq_printf(seq, " %ums flushing data (in ordered mode)\n", 913bf699327STheodore Ts'o jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid)); 9148e85fb3fSJohann Lombardi seq_printf(seq, " %ums logging transaction\n", 915bf699327STheodore Ts'o jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid)); 916c225aa57SSimon Holm Thøgersen seq_printf(seq, " %lluus average transaction commit time\n", 917c225aa57SSimon Holm Thøgersen div_u64(s->journal->j_average_commit_time, 1000)); 9188e85fb3fSJohann Lombardi seq_printf(seq, " %lu handles per transaction\n", 919bf699327STheodore Ts'o s->stats->run.rs_handle_count / s->stats->ts_tid); 9208e85fb3fSJohann Lombardi seq_printf(seq, " %lu blocks per transaction\n", 921bf699327STheodore Ts'o s->stats->run.rs_blocks / s->stats->ts_tid); 9228e85fb3fSJohann Lombardi seq_printf(seq, " %lu logged blocks per transaction\n", 923bf699327STheodore Ts'o s->stats->run.rs_blocks_logged / s->stats->ts_tid); 9248e85fb3fSJohann Lombardi return 0; 9258e85fb3fSJohann Lombardi } 9268e85fb3fSJohann Lombardi 9278e85fb3fSJohann Lombardi static void jbd2_seq_info_stop(struct seq_file *seq, void *v) 9288e85fb3fSJohann Lombardi { 9298e85fb3fSJohann Lombardi } 9308e85fb3fSJohann Lombardi 93188e9d34cSJames Morris static const struct seq_operations jbd2_seq_info_ops = { 9328e85fb3fSJohann Lombardi .start = jbd2_seq_info_start, 9338e85fb3fSJohann Lombardi .next = jbd2_seq_info_next, 9348e85fb3fSJohann Lombardi .stop = jbd2_seq_info_stop, 9358e85fb3fSJohann Lombardi .show = jbd2_seq_info_show, 9368e85fb3fSJohann Lombardi }; 9378e85fb3fSJohann Lombardi 9388e85fb3fSJohann Lombardi static int jbd2_seq_info_open(struct inode *inode, struct file *file) 9398e85fb3fSJohann Lombardi { 9408e85fb3fSJohann Lombardi journal_t *journal = PDE(inode)->data; 9418e85fb3fSJohann Lombardi struct jbd2_stats_proc_session *s; 9428e85fb3fSJohann Lombardi int rc, size; 9438e85fb3fSJohann Lombardi 9448e85fb3fSJohann Lombardi s = kmalloc(sizeof(*s), GFP_KERNEL); 9458e85fb3fSJohann Lombardi if (s == NULL) 9468e85fb3fSJohann Lombardi return -ENOMEM; 9478e85fb3fSJohann Lombardi size = sizeof(struct transaction_stats_s); 9488e85fb3fSJohann Lombardi s->stats = kmalloc(size, GFP_KERNEL); 9498e85fb3fSJohann Lombardi if (s->stats == NULL) { 9508e85fb3fSJohann Lombardi kfree(s); 9518e85fb3fSJohann Lombardi return -ENOMEM; 9528e85fb3fSJohann Lombardi } 9538e85fb3fSJohann Lombardi spin_lock(&journal->j_history_lock); 9548e85fb3fSJohann Lombardi memcpy(s->stats, &journal->j_stats, size); 9558e85fb3fSJohann Lombardi s->journal = journal; 9568e85fb3fSJohann Lombardi spin_unlock(&journal->j_history_lock); 9578e85fb3fSJohann Lombardi 9588e85fb3fSJohann Lombardi rc = seq_open(file, &jbd2_seq_info_ops); 9598e85fb3fSJohann Lombardi if (rc == 0) { 9608e85fb3fSJohann Lombardi struct seq_file *m = file->private_data; 9618e85fb3fSJohann Lombardi m->private = s; 9628e85fb3fSJohann Lombardi } else { 9638e85fb3fSJohann Lombardi kfree(s->stats); 9648e85fb3fSJohann Lombardi kfree(s); 9658e85fb3fSJohann Lombardi } 9668e85fb3fSJohann Lombardi return rc; 9678e85fb3fSJohann Lombardi 9688e85fb3fSJohann Lombardi } 9698e85fb3fSJohann Lombardi 9708e85fb3fSJohann Lombardi static int jbd2_seq_info_release(struct inode *inode, struct file *file) 9718e85fb3fSJohann Lombardi { 9728e85fb3fSJohann Lombardi struct seq_file *seq = file->private_data; 9738e85fb3fSJohann Lombardi struct jbd2_stats_proc_session *s = seq->private; 9748e85fb3fSJohann Lombardi kfree(s->stats); 9758e85fb3fSJohann Lombardi kfree(s); 9768e85fb3fSJohann Lombardi return seq_release(inode, file); 9778e85fb3fSJohann Lombardi } 9788e85fb3fSJohann Lombardi 979828c0950SAlexey Dobriyan static const struct file_operations jbd2_seq_info_fops = { 9808e85fb3fSJohann Lombardi .owner = THIS_MODULE, 9818e85fb3fSJohann Lombardi .open = jbd2_seq_info_open, 9828e85fb3fSJohann Lombardi .read = seq_read, 9838e85fb3fSJohann Lombardi .llseek = seq_lseek, 9848e85fb3fSJohann Lombardi .release = jbd2_seq_info_release, 9858e85fb3fSJohann Lombardi }; 9868e85fb3fSJohann Lombardi 9878e85fb3fSJohann Lombardi static struct proc_dir_entry *proc_jbd2_stats; 9888e85fb3fSJohann Lombardi 9898e85fb3fSJohann Lombardi static void jbd2_stats_proc_init(journal_t *journal) 9908e85fb3fSJohann Lombardi { 99105496769STheodore Ts'o journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats); 9928e85fb3fSJohann Lombardi if (journal->j_proc_entry) { 99379da3664SDenis V. Lunev proc_create_data("info", S_IRUGO, journal->j_proc_entry, 99479da3664SDenis V. Lunev &jbd2_seq_info_fops, journal); 9958e85fb3fSJohann Lombardi } 9968e85fb3fSJohann Lombardi } 9978e85fb3fSJohann Lombardi 9988e85fb3fSJohann Lombardi static void jbd2_stats_proc_exit(journal_t *journal) 9998e85fb3fSJohann Lombardi { 10008e85fb3fSJohann Lombardi remove_proc_entry("info", journal->j_proc_entry); 100105496769STheodore Ts'o remove_proc_entry(journal->j_devname, proc_jbd2_stats); 10028e85fb3fSJohann Lombardi } 10038e85fb3fSJohann Lombardi 1004470decc6SDave Kleikamp /* 1005470decc6SDave Kleikamp * Management for journal control blocks: functions to create and 1006470decc6SDave Kleikamp * destroy journal_t structures, and to initialise and read existing 1007470decc6SDave Kleikamp * journal blocks from disk. */ 1008470decc6SDave Kleikamp 1009470decc6SDave Kleikamp /* First: create and setup a journal_t object in memory. We initialise 1010470decc6SDave Kleikamp * very few fields yet: that has to wait until we have created the 1011470decc6SDave Kleikamp * journal structures from from scratch, or loaded them from disk. */ 1012470decc6SDave Kleikamp 1013470decc6SDave Kleikamp static journal_t * journal_init_common (void) 1014470decc6SDave Kleikamp { 1015470decc6SDave Kleikamp journal_t *journal; 1016470decc6SDave Kleikamp int err; 1017470decc6SDave Kleikamp 10183ebfdf88SAndrew Morton journal = kzalloc(sizeof(*journal), GFP_KERNEL); 1019470decc6SDave Kleikamp if (!journal) 1020b7271b0aSTheodore Ts'o return NULL; 1021470decc6SDave Kleikamp 1022470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_transaction_locked); 1023470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_logspace); 1024470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_done_commit); 1025470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_checkpoint); 1026470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_commit); 1027470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_updates); 1028470decc6SDave Kleikamp mutex_init(&journal->j_barrier); 1029470decc6SDave Kleikamp mutex_init(&journal->j_checkpoint_mutex); 1030470decc6SDave Kleikamp spin_lock_init(&journal->j_revoke_lock); 1031470decc6SDave Kleikamp spin_lock_init(&journal->j_list_lock); 1032a931da6aSTheodore Ts'o rwlock_init(&journal->j_state_lock); 1033470decc6SDave Kleikamp 1034cd02ff0bSMingming Cao journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE); 103530773840STheodore Ts'o journal->j_min_batch_time = 0; 103630773840STheodore Ts'o journal->j_max_batch_time = 15000; /* 15ms */ 1037470decc6SDave Kleikamp 1038470decc6SDave Kleikamp /* The journal is marked for error until we succeed with recovery! */ 1039f7f4bccbSMingming Cao journal->j_flags = JBD2_ABORT; 1040470decc6SDave Kleikamp 1041470decc6SDave Kleikamp /* Set up a default-sized revoke table for the new mount. */ 1042f7f4bccbSMingming Cao err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH); 1043470decc6SDave Kleikamp if (err) { 1044470decc6SDave Kleikamp kfree(journal); 1045b7271b0aSTheodore Ts'o return NULL; 1046470decc6SDave Kleikamp } 10478e85fb3fSJohann Lombardi 1048bf699327STheodore Ts'o spin_lock_init(&journal->j_history_lock); 10498e85fb3fSJohann Lombardi 1050470decc6SDave Kleikamp return journal; 1051470decc6SDave Kleikamp } 1052470decc6SDave Kleikamp 1053f7f4bccbSMingming Cao /* jbd2_journal_init_dev and jbd2_journal_init_inode: 1054470decc6SDave Kleikamp * 1055470decc6SDave Kleikamp * Create a journal structure assigned some fixed set of disk blocks to 1056470decc6SDave Kleikamp * the journal. We don't actually touch those disk blocks yet, but we 1057470decc6SDave Kleikamp * need to set up all of the mapping information to tell the journaling 1058470decc6SDave Kleikamp * system where the journal blocks are. 1059470decc6SDave Kleikamp * 1060470decc6SDave Kleikamp */ 1061470decc6SDave Kleikamp 1062470decc6SDave Kleikamp /** 10635648ba5bSRandy Dunlap * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure 1064470decc6SDave Kleikamp * @bdev: Block device on which to create the journal 1065470decc6SDave Kleikamp * @fs_dev: Device which hold journalled filesystem for this journal. 1066470decc6SDave Kleikamp * @start: Block nr Start of journal. 1067470decc6SDave Kleikamp * @len: Length of the journal in blocks. 1068470decc6SDave Kleikamp * @blocksize: blocksize of journalling device 10695648ba5bSRandy Dunlap * 10705648ba5bSRandy Dunlap * Returns: a newly created journal_t * 1071470decc6SDave Kleikamp * 1072f7f4bccbSMingming Cao * jbd2_journal_init_dev creates a journal which maps a fixed contiguous 1073470decc6SDave Kleikamp * range of blocks on an arbitrary block device. 1074470decc6SDave Kleikamp * 1075470decc6SDave Kleikamp */ 1076f7f4bccbSMingming Cao journal_t * jbd2_journal_init_dev(struct block_device *bdev, 1077470decc6SDave Kleikamp struct block_device *fs_dev, 107818eba7aaSMingming Cao unsigned long long start, int len, int blocksize) 1079470decc6SDave Kleikamp { 1080470decc6SDave Kleikamp journal_t *journal = journal_init_common(); 1081470decc6SDave Kleikamp struct buffer_head *bh; 108205496769STheodore Ts'o char *p; 1083470decc6SDave Kleikamp int n; 1084470decc6SDave Kleikamp 1085470decc6SDave Kleikamp if (!journal) 1086470decc6SDave Kleikamp return NULL; 1087470decc6SDave Kleikamp 1088470decc6SDave Kleikamp /* journal descriptor can store up to n blocks -bzzz */ 1089470decc6SDave Kleikamp journal->j_blocksize = blocksize; 10900587aa3dSyangsheng journal->j_dev = bdev; 10910587aa3dSyangsheng journal->j_fs_dev = fs_dev; 10920587aa3dSyangsheng journal->j_blk_offset = start; 10930587aa3dSyangsheng journal->j_maxlen = len; 10940587aa3dSyangsheng bdevname(journal->j_dev, journal->j_devname); 10950587aa3dSyangsheng p = journal->j_devname; 10960587aa3dSyangsheng while ((p = strchr(p, '/'))) 10970587aa3dSyangsheng *p = '!'; 10984b905671SJan Kara jbd2_stats_proc_init(journal); 1099470decc6SDave Kleikamp n = journal->j_blocksize / sizeof(journal_block_tag_t); 1100470decc6SDave Kleikamp journal->j_wbufsize = n; 1101470decc6SDave Kleikamp journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL); 1102470decc6SDave Kleikamp if (!journal->j_wbuf) { 110325985edcSLucas De Marchi printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n", 1104329d291fSHarvey Harrison __func__); 11054b905671SJan Kara goto out_err; 1106470decc6SDave Kleikamp } 1107470decc6SDave Kleikamp 1108470decc6SDave Kleikamp bh = __getblk(journal->j_dev, start, journal->j_blocksize); 11094b905671SJan Kara if (!bh) { 11104b905671SJan Kara printk(KERN_ERR 11114b905671SJan Kara "%s: Cannot get buffer for journal superblock\n", 11124b905671SJan Kara __func__); 11134b905671SJan Kara goto out_err; 11144b905671SJan Kara } 1115470decc6SDave Kleikamp journal->j_sb_buffer = bh; 1116470decc6SDave Kleikamp journal->j_superblock = (journal_superblock_t *)bh->b_data; 11174b905671SJan Kara 1118470decc6SDave Kleikamp return journal; 11194b905671SJan Kara out_err: 11207b02bec0STao Ma kfree(journal->j_wbuf); 11214b905671SJan Kara jbd2_stats_proc_exit(journal); 11224b905671SJan Kara kfree(journal); 11234b905671SJan Kara return NULL; 1124470decc6SDave Kleikamp } 1125470decc6SDave Kleikamp 1126470decc6SDave Kleikamp /** 1127f7f4bccbSMingming Cao * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode. 1128470decc6SDave Kleikamp * @inode: An inode to create the journal in 1129470decc6SDave Kleikamp * 1130f7f4bccbSMingming Cao * jbd2_journal_init_inode creates a journal which maps an on-disk inode as 1131470decc6SDave Kleikamp * the journal. The inode must exist already, must support bmap() and 1132470decc6SDave Kleikamp * must have all data blocks preallocated. 1133470decc6SDave Kleikamp */ 1134f7f4bccbSMingming Cao journal_t * jbd2_journal_init_inode (struct inode *inode) 1135470decc6SDave Kleikamp { 1136470decc6SDave Kleikamp struct buffer_head *bh; 1137470decc6SDave Kleikamp journal_t *journal = journal_init_common(); 113805496769STheodore Ts'o char *p; 1139470decc6SDave Kleikamp int err; 1140470decc6SDave Kleikamp int n; 114118eba7aaSMingming Cao unsigned long long blocknr; 1142470decc6SDave Kleikamp 1143470decc6SDave Kleikamp if (!journal) 1144470decc6SDave Kleikamp return NULL; 1145470decc6SDave Kleikamp 1146470decc6SDave Kleikamp journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev; 1147470decc6SDave Kleikamp journal->j_inode = inode; 114805496769STheodore Ts'o bdevname(journal->j_dev, journal->j_devname); 114905496769STheodore Ts'o p = journal->j_devname; 115005496769STheodore Ts'o while ((p = strchr(p, '/'))) 115105496769STheodore Ts'o *p = '!'; 115205496769STheodore Ts'o p = journal->j_devname + strlen(journal->j_devname); 115390576c0bSTheodore Ts'o sprintf(p, "-%lu", journal->j_inode->i_ino); 1154470decc6SDave Kleikamp jbd_debug(1, 1155470decc6SDave Kleikamp "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n", 1156470decc6SDave Kleikamp journal, inode->i_sb->s_id, inode->i_ino, 1157470decc6SDave Kleikamp (long long) inode->i_size, 1158470decc6SDave Kleikamp inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize); 1159470decc6SDave Kleikamp 1160470decc6SDave Kleikamp journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits; 1161470decc6SDave Kleikamp journal->j_blocksize = inode->i_sb->s_blocksize; 11628e85fb3fSJohann Lombardi jbd2_stats_proc_init(journal); 1163470decc6SDave Kleikamp 1164470decc6SDave Kleikamp /* journal descriptor can store up to n blocks -bzzz */ 1165470decc6SDave Kleikamp n = journal->j_blocksize / sizeof(journal_block_tag_t); 1166470decc6SDave Kleikamp journal->j_wbufsize = n; 1167470decc6SDave Kleikamp journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL); 1168470decc6SDave Kleikamp if (!journal->j_wbuf) { 116925985edcSLucas De Marchi printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n", 1170329d291fSHarvey Harrison __func__); 11714b905671SJan Kara goto out_err; 1172470decc6SDave Kleikamp } 1173470decc6SDave Kleikamp 1174f7f4bccbSMingming Cao err = jbd2_journal_bmap(journal, 0, &blocknr); 1175470decc6SDave Kleikamp /* If that failed, give up */ 1176470decc6SDave Kleikamp if (err) { 11773c26bdb4SJustin P. Mattock printk(KERN_ERR "%s: Cannot locate journal superblock\n", 1178329d291fSHarvey Harrison __func__); 11794b905671SJan Kara goto out_err; 1180470decc6SDave Kleikamp } 1181470decc6SDave Kleikamp 1182470decc6SDave Kleikamp bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize); 11834b905671SJan Kara if (!bh) { 11844b905671SJan Kara printk(KERN_ERR 11854b905671SJan Kara "%s: Cannot get buffer for journal superblock\n", 11864b905671SJan Kara __func__); 11874b905671SJan Kara goto out_err; 11884b905671SJan Kara } 1189470decc6SDave Kleikamp journal->j_sb_buffer = bh; 1190470decc6SDave Kleikamp journal->j_superblock = (journal_superblock_t *)bh->b_data; 1191470decc6SDave Kleikamp 1192470decc6SDave Kleikamp return journal; 11934b905671SJan Kara out_err: 11947b02bec0STao Ma kfree(journal->j_wbuf); 11954b905671SJan Kara jbd2_stats_proc_exit(journal); 11964b905671SJan Kara kfree(journal); 11974b905671SJan Kara return NULL; 1198470decc6SDave Kleikamp } 1199470decc6SDave Kleikamp 1200470decc6SDave Kleikamp /* 1201470decc6SDave Kleikamp * If the journal init or create aborts, we need to mark the journal 1202470decc6SDave Kleikamp * superblock as being NULL to prevent the journal destroy from writing 1203470decc6SDave Kleikamp * back a bogus superblock. 1204470decc6SDave Kleikamp */ 1205470decc6SDave Kleikamp static void journal_fail_superblock (journal_t *journal) 1206470decc6SDave Kleikamp { 1207470decc6SDave Kleikamp struct buffer_head *bh = journal->j_sb_buffer; 1208470decc6SDave Kleikamp brelse(bh); 1209470decc6SDave Kleikamp journal->j_sb_buffer = NULL; 1210470decc6SDave Kleikamp } 1211470decc6SDave Kleikamp 1212470decc6SDave Kleikamp /* 1213470decc6SDave Kleikamp * Given a journal_t structure, initialise the various fields for 1214470decc6SDave Kleikamp * startup of a new journaling session. We use this both when creating 1215470decc6SDave Kleikamp * a journal, and after recovering an old journal to reset it for 1216470decc6SDave Kleikamp * subsequent use. 1217470decc6SDave Kleikamp */ 1218470decc6SDave Kleikamp 1219470decc6SDave Kleikamp static int journal_reset(journal_t *journal) 1220470decc6SDave Kleikamp { 1221470decc6SDave Kleikamp journal_superblock_t *sb = journal->j_superblock; 122218eba7aaSMingming Cao unsigned long long first, last; 1223470decc6SDave Kleikamp 1224470decc6SDave Kleikamp first = be32_to_cpu(sb->s_first); 1225470decc6SDave Kleikamp last = be32_to_cpu(sb->s_maxlen); 1226f6f50e28SJan Kara if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) { 1227f2a44523SEryu Guan printk(KERN_ERR "JBD2: Journal too short (blocks %llu-%llu).\n", 1228f6f50e28SJan Kara first, last); 1229f6f50e28SJan Kara journal_fail_superblock(journal); 1230f6f50e28SJan Kara return -EINVAL; 1231f6f50e28SJan Kara } 1232470decc6SDave Kleikamp 1233470decc6SDave Kleikamp journal->j_first = first; 1234470decc6SDave Kleikamp journal->j_last = last; 1235470decc6SDave Kleikamp 1236470decc6SDave Kleikamp journal->j_head = first; 1237470decc6SDave Kleikamp journal->j_tail = first; 1238470decc6SDave Kleikamp journal->j_free = last - first; 1239470decc6SDave Kleikamp 1240470decc6SDave Kleikamp journal->j_tail_sequence = journal->j_transaction_sequence; 1241470decc6SDave Kleikamp journal->j_commit_sequence = journal->j_transaction_sequence - 1; 1242470decc6SDave Kleikamp journal->j_commit_request = journal->j_commit_sequence; 1243470decc6SDave Kleikamp 1244470decc6SDave Kleikamp journal->j_max_transaction_buffers = journal->j_maxlen / 4; 1245470decc6SDave Kleikamp 1246470decc6SDave Kleikamp /* 1247470decc6SDave Kleikamp * As a special case, if the on-disk copy is already marked as needing 124824bcc89cSJan Kara * no recovery (s_start == 0), then we can safely defer the superblock 124924bcc89cSJan Kara * update until the next commit by setting JBD2_FLUSHED. This avoids 1250470decc6SDave Kleikamp * attempting a write to a potential-readonly device. 1251470decc6SDave Kleikamp */ 125224bcc89cSJan Kara if (sb->s_start == 0) { 1253f2a44523SEryu Guan jbd_debug(1, "JBD2: Skipping superblock update on recovered sb " 1254470decc6SDave Kleikamp "(start %ld, seq %d, errno %d)\n", 1255470decc6SDave Kleikamp journal->j_tail, journal->j_tail_sequence, 1256470decc6SDave Kleikamp journal->j_errno); 125724bcc89cSJan Kara journal->j_flags |= JBD2_FLUSHED; 125824bcc89cSJan Kara } else { 1259a78bb11dSJan Kara /* Lock here to make assertions happy... */ 1260a78bb11dSJan Kara mutex_lock(&journal->j_checkpoint_mutex); 126179feb521SJan Kara /* 126279feb521SJan Kara * Update log tail information. We use WRITE_FUA since new 126379feb521SJan Kara * transaction will start reusing journal space and so we 126479feb521SJan Kara * must make sure information about current log tail is on 126579feb521SJan Kara * disk before that. 126679feb521SJan Kara */ 126779feb521SJan Kara jbd2_journal_update_sb_log_tail(journal, 126879feb521SJan Kara journal->j_tail_sequence, 126979feb521SJan Kara journal->j_tail, 127079feb521SJan Kara WRITE_FUA); 1271a78bb11dSJan Kara mutex_unlock(&journal->j_checkpoint_mutex); 127224bcc89cSJan Kara } 127324bcc89cSJan Kara return jbd2_journal_start_thread(journal); 1274470decc6SDave Kleikamp } 1275470decc6SDave Kleikamp 127679feb521SJan Kara static void jbd2_write_superblock(journal_t *journal, int write_op) 127724bcc89cSJan Kara { 127824bcc89cSJan Kara struct buffer_head *bh = journal->j_sb_buffer; 127979feb521SJan Kara int ret; 128024bcc89cSJan Kara 128179feb521SJan Kara trace_jbd2_write_superblock(journal, write_op); 128279feb521SJan Kara if (!(journal->j_flags & JBD2_BARRIER)) 128379feb521SJan Kara write_op &= ~(REQ_FUA | REQ_FLUSH); 128479feb521SJan Kara lock_buffer(bh); 1285914258bfSTheodore Ts'o if (buffer_write_io_error(bh)) { 1286914258bfSTheodore Ts'o /* 1287914258bfSTheodore Ts'o * Oh, dear. A previous attempt to write the journal 1288914258bfSTheodore Ts'o * superblock failed. This could happen because the 1289914258bfSTheodore Ts'o * USB device was yanked out. Or it could happen to 1290914258bfSTheodore Ts'o * be a transient write error and maybe the block will 1291914258bfSTheodore Ts'o * be remapped. Nothing we can do but to retry the 1292914258bfSTheodore Ts'o * write and hope for the best. 1293914258bfSTheodore Ts'o */ 1294914258bfSTheodore Ts'o printk(KERN_ERR "JBD2: previous I/O error detected " 1295914258bfSTheodore Ts'o "for journal superblock update for %s.\n", 1296914258bfSTheodore Ts'o journal->j_devname); 1297914258bfSTheodore Ts'o clear_buffer_write_io_error(bh); 1298914258bfSTheodore Ts'o set_buffer_uptodate(bh); 1299914258bfSTheodore Ts'o } 130079feb521SJan Kara get_bh(bh); 130179feb521SJan Kara bh->b_end_io = end_buffer_write_sync; 130279feb521SJan Kara ret = submit_bh(write_op, bh); 130379feb521SJan Kara wait_on_buffer(bh); 1304914258bfSTheodore Ts'o if (buffer_write_io_error(bh)) { 1305914258bfSTheodore Ts'o clear_buffer_write_io_error(bh); 1306914258bfSTheodore Ts'o set_buffer_uptodate(bh); 130779feb521SJan Kara ret = -EIO; 130879feb521SJan Kara } 130979feb521SJan Kara if (ret) { 131079feb521SJan Kara printk(KERN_ERR "JBD2: Error %d detected when updating " 131179feb521SJan Kara "journal superblock for %s.\n", ret, 131279feb521SJan Kara journal->j_devname); 1313914258bfSTheodore Ts'o } 131424bcc89cSJan Kara } 1315470decc6SDave Kleikamp 131624bcc89cSJan Kara /** 131724bcc89cSJan Kara * jbd2_journal_update_sb_log_tail() - Update log tail in journal sb on disk. 131824bcc89cSJan Kara * @journal: The journal to update. 131979feb521SJan Kara * @tail_tid: TID of the new transaction at the tail of the log 132079feb521SJan Kara * @tail_block: The first block of the transaction at the tail of the log 132179feb521SJan Kara * @write_op: With which operation should we write the journal sb 132224bcc89cSJan Kara * 132324bcc89cSJan Kara * Update a journal's superblock information about log tail and write it to 132424bcc89cSJan Kara * disk, waiting for the IO to complete. 132524bcc89cSJan Kara */ 132679feb521SJan Kara void jbd2_journal_update_sb_log_tail(journal_t *journal, tid_t tail_tid, 132779feb521SJan Kara unsigned long tail_block, int write_op) 132824bcc89cSJan Kara { 132924bcc89cSJan Kara journal_superblock_t *sb = journal->j_superblock; 13302201c590SSeiji Aguchi 1331a78bb11dSJan Kara BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex)); 133279feb521SJan Kara jbd_debug(1, "JBD2: updating superblock (start %lu, seq %u)\n", 133379feb521SJan Kara tail_block, tail_tid); 1334470decc6SDave Kleikamp 133579feb521SJan Kara sb->s_sequence = cpu_to_be32(tail_tid); 133679feb521SJan Kara sb->s_start = cpu_to_be32(tail_block); 133724bcc89cSJan Kara 133879feb521SJan Kara jbd2_write_superblock(journal, write_op); 133924bcc89cSJan Kara 134024bcc89cSJan Kara /* Log is no longer empty */ 1341a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 134224bcc89cSJan Kara WARN_ON(!sb->s_sequence); 1343f7f4bccbSMingming Cao journal->j_flags &= ~JBD2_FLUSHED; 134424bcc89cSJan Kara write_unlock(&journal->j_state_lock); 134524bcc89cSJan Kara } 134624bcc89cSJan Kara 134724bcc89cSJan Kara /** 134824bcc89cSJan Kara * jbd2_mark_journal_empty() - Mark on disk journal as empty. 134924bcc89cSJan Kara * @journal: The journal to update. 135024bcc89cSJan Kara * 135124bcc89cSJan Kara * Update a journal's dynamic superblock fields to show that journal is empty. 135224bcc89cSJan Kara * Write updated superblock to disk waiting for IO to complete. 135324bcc89cSJan Kara */ 135424bcc89cSJan Kara static void jbd2_mark_journal_empty(journal_t *journal) 135524bcc89cSJan Kara { 135624bcc89cSJan Kara journal_superblock_t *sb = journal->j_superblock; 135724bcc89cSJan Kara 1358a78bb11dSJan Kara BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex)); 1359470decc6SDave Kleikamp read_lock(&journal->j_state_lock); 1360eeecef0aSEric Sandeen /* Is it already empty? */ 1361eeecef0aSEric Sandeen if (sb->s_start == 0) { 1362eeecef0aSEric Sandeen read_unlock(&journal->j_state_lock); 1363eeecef0aSEric Sandeen return; 1364eeecef0aSEric Sandeen } 136524bcc89cSJan Kara jbd_debug(1, "JBD2: Marking journal as empty (seq %d)\n", 136624bcc89cSJan Kara journal->j_tail_sequence); 1367470decc6SDave Kleikamp 1368470decc6SDave Kleikamp sb->s_sequence = cpu_to_be32(journal->j_tail_sequence); 136924bcc89cSJan Kara sb->s_start = cpu_to_be32(0); 137024bcc89cSJan Kara read_unlock(&journal->j_state_lock); 137124bcc89cSJan Kara 137279feb521SJan Kara jbd2_write_superblock(journal, WRITE_FUA); 137324bcc89cSJan Kara 137424bcc89cSJan Kara /* Log is no longer empty */ 137524bcc89cSJan Kara write_lock(&journal->j_state_lock); 1376f7f4bccbSMingming Cao journal->j_flags |= JBD2_FLUSHED; 1377a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 1378470decc6SDave Kleikamp } 1379470decc6SDave Kleikamp 138024bcc89cSJan Kara 138124bcc89cSJan Kara /** 138224bcc89cSJan Kara * jbd2_journal_update_sb_errno() - Update error in the journal. 138324bcc89cSJan Kara * @journal: The journal to update. 138424bcc89cSJan Kara * 138524bcc89cSJan Kara * Update a journal's errno. Write updated superblock to disk waiting for IO 138624bcc89cSJan Kara * to complete. 138724bcc89cSJan Kara */ 1388d796c52eSTheodore Ts'o void jbd2_journal_update_sb_errno(journal_t *journal) 138924bcc89cSJan Kara { 139024bcc89cSJan Kara journal_superblock_t *sb = journal->j_superblock; 139124bcc89cSJan Kara 139224bcc89cSJan Kara read_lock(&journal->j_state_lock); 139324bcc89cSJan Kara jbd_debug(1, "JBD2: updating superblock error (errno %d)\n", 139424bcc89cSJan Kara journal->j_errno); 1395470decc6SDave Kleikamp sb->s_errno = cpu_to_be32(journal->j_errno); 13964fd5ea43SDarrick J. Wong jbd2_superblock_csum_set(journal, sb); 1397470decc6SDave Kleikamp read_unlock(&journal->j_state_lock); 1398470decc6SDave Kleikamp 139979feb521SJan Kara jbd2_write_superblock(journal, WRITE_SYNC); 1400470decc6SDave Kleikamp } 1401d796c52eSTheodore Ts'o EXPORT_SYMBOL(jbd2_journal_update_sb_errno); 1402470decc6SDave Kleikamp 1403470decc6SDave Kleikamp /* 1404470decc6SDave Kleikamp * Read the superblock for a given journal, performing initial 1405470decc6SDave Kleikamp * validation of the format. 1406470decc6SDave Kleikamp */ 1407470decc6SDave Kleikamp static int journal_get_superblock(journal_t *journal) 1408470decc6SDave Kleikamp { 1409470decc6SDave Kleikamp struct buffer_head *bh; 1410470decc6SDave Kleikamp journal_superblock_t *sb; 1411470decc6SDave Kleikamp int err = -EIO; 1412470decc6SDave Kleikamp 1413470decc6SDave Kleikamp bh = journal->j_sb_buffer; 1414470decc6SDave Kleikamp 1415470decc6SDave Kleikamp J_ASSERT(bh != NULL); 1416470decc6SDave Kleikamp if (!buffer_uptodate(bh)) { 1417470decc6SDave Kleikamp ll_rw_block(READ, 1, &bh); 1418470decc6SDave Kleikamp wait_on_buffer(bh); 1419470decc6SDave Kleikamp if (!buffer_uptodate(bh)) { 1420470decc6SDave Kleikamp printk(KERN_ERR 1421f2a44523SEryu Guan "JBD2: IO error reading journal superblock\n"); 1422470decc6SDave Kleikamp goto out; 1423470decc6SDave Kleikamp } 1424470decc6SDave Kleikamp } 1425470decc6SDave Kleikamp 142625ed6e8aSDarrick J. Wong if (buffer_verified(bh)) 142725ed6e8aSDarrick J. Wong return 0; 142825ed6e8aSDarrick J. Wong 1429470decc6SDave Kleikamp sb = journal->j_superblock; 1430470decc6SDave Kleikamp 1431470decc6SDave Kleikamp err = -EINVAL; 1432470decc6SDave Kleikamp 1433f7f4bccbSMingming Cao if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) || 1434470decc6SDave Kleikamp sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) { 1435f2a44523SEryu Guan printk(KERN_WARNING "JBD2: no valid journal superblock found\n"); 1436470decc6SDave Kleikamp goto out; 1437470decc6SDave Kleikamp } 1438470decc6SDave Kleikamp 1439470decc6SDave Kleikamp switch(be32_to_cpu(sb->s_header.h_blocktype)) { 1440f7f4bccbSMingming Cao case JBD2_SUPERBLOCK_V1: 1441470decc6SDave Kleikamp journal->j_format_version = 1; 1442470decc6SDave Kleikamp break; 1443f7f4bccbSMingming Cao case JBD2_SUPERBLOCK_V2: 1444470decc6SDave Kleikamp journal->j_format_version = 2; 1445470decc6SDave Kleikamp break; 1446470decc6SDave Kleikamp default: 1447f2a44523SEryu Guan printk(KERN_WARNING "JBD2: unrecognised superblock format ID\n"); 1448470decc6SDave Kleikamp goto out; 1449470decc6SDave Kleikamp } 1450470decc6SDave Kleikamp 1451470decc6SDave Kleikamp if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen) 1452470decc6SDave Kleikamp journal->j_maxlen = be32_to_cpu(sb->s_maxlen); 1453470decc6SDave Kleikamp else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) { 1454f2a44523SEryu Guan printk(KERN_WARNING "JBD2: journal file too short\n"); 1455470decc6SDave Kleikamp goto out; 1456470decc6SDave Kleikamp } 1457470decc6SDave Kleikamp 14588762202dSEryu Guan if (be32_to_cpu(sb->s_first) == 0 || 14598762202dSEryu Guan be32_to_cpu(sb->s_first) >= journal->j_maxlen) { 14608762202dSEryu Guan printk(KERN_WARNING 14618762202dSEryu Guan "JBD2: Invalid start block of journal: %u\n", 14628762202dSEryu Guan be32_to_cpu(sb->s_first)); 14638762202dSEryu Guan goto out; 14648762202dSEryu Guan } 14658762202dSEryu Guan 146625ed6e8aSDarrick J. Wong if (JBD2_HAS_COMPAT_FEATURE(journal, JBD2_FEATURE_COMPAT_CHECKSUM) && 146725ed6e8aSDarrick J. Wong JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2)) { 146825ed6e8aSDarrick J. Wong /* Can't have checksum v1 and v2 on at the same time! */ 146925ed6e8aSDarrick J. Wong printk(KERN_ERR "JBD: Can't enable checksumming v1 and v2 " 147025ed6e8aSDarrick J. Wong "at the same time!\n"); 147125ed6e8aSDarrick J. Wong goto out; 147225ed6e8aSDarrick J. Wong } 147325ed6e8aSDarrick J. Wong 147425ed6e8aSDarrick J. Wong if (!jbd2_verify_csum_type(journal, sb)) { 147525ed6e8aSDarrick J. Wong printk(KERN_ERR "JBD: Unknown checksum type\n"); 147625ed6e8aSDarrick J. Wong goto out; 147725ed6e8aSDarrick J. Wong } 147825ed6e8aSDarrick J. Wong 147901b5adceSDarrick J. Wong /* Load the checksum driver */ 148001b5adceSDarrick J. Wong if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2)) { 148101b5adceSDarrick J. Wong journal->j_chksum_driver = crypto_alloc_shash("crc32c", 0, 0); 148201b5adceSDarrick J. Wong if (IS_ERR(journal->j_chksum_driver)) { 148301b5adceSDarrick J. Wong printk(KERN_ERR "JBD: Cannot load crc32c driver.\n"); 148401b5adceSDarrick J. Wong err = PTR_ERR(journal->j_chksum_driver); 148501b5adceSDarrick J. Wong journal->j_chksum_driver = NULL; 148601b5adceSDarrick J. Wong goto out; 148701b5adceSDarrick J. Wong } 148801b5adceSDarrick J. Wong } 148901b5adceSDarrick J. Wong 14904fd5ea43SDarrick J. Wong /* Check superblock checksum */ 14914fd5ea43SDarrick J. Wong if (!jbd2_superblock_csum_verify(journal, sb)) { 14924fd5ea43SDarrick J. Wong printk(KERN_ERR "JBD: journal checksum error\n"); 14934fd5ea43SDarrick J. Wong goto out; 14944fd5ea43SDarrick J. Wong } 14954fd5ea43SDarrick J. Wong 14964fd5ea43SDarrick J. Wong /* Precompute checksum seed for all metadata */ 14974fd5ea43SDarrick J. Wong if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2)) 14984fd5ea43SDarrick J. Wong journal->j_csum_seed = jbd2_chksum(journal, ~0, sb->s_uuid, 14994fd5ea43SDarrick J. Wong sizeof(sb->s_uuid)); 15004fd5ea43SDarrick J. Wong 150125ed6e8aSDarrick J. Wong set_buffer_verified(bh); 150225ed6e8aSDarrick J. Wong 1503470decc6SDave Kleikamp return 0; 1504470decc6SDave Kleikamp 1505470decc6SDave Kleikamp out: 1506470decc6SDave Kleikamp journal_fail_superblock(journal); 1507470decc6SDave Kleikamp return err; 1508470decc6SDave Kleikamp } 1509470decc6SDave Kleikamp 1510470decc6SDave Kleikamp /* 1511470decc6SDave Kleikamp * Load the on-disk journal superblock and read the key fields into the 1512470decc6SDave Kleikamp * journal_t. 1513470decc6SDave Kleikamp */ 1514470decc6SDave Kleikamp 1515470decc6SDave Kleikamp static int load_superblock(journal_t *journal) 1516470decc6SDave Kleikamp { 1517470decc6SDave Kleikamp int err; 1518470decc6SDave Kleikamp journal_superblock_t *sb; 1519470decc6SDave Kleikamp 1520470decc6SDave Kleikamp err = journal_get_superblock(journal); 1521470decc6SDave Kleikamp if (err) 1522470decc6SDave Kleikamp return err; 1523470decc6SDave Kleikamp 1524470decc6SDave Kleikamp sb = journal->j_superblock; 1525470decc6SDave Kleikamp 1526470decc6SDave Kleikamp journal->j_tail_sequence = be32_to_cpu(sb->s_sequence); 1527470decc6SDave Kleikamp journal->j_tail = be32_to_cpu(sb->s_start); 1528470decc6SDave Kleikamp journal->j_first = be32_to_cpu(sb->s_first); 1529470decc6SDave Kleikamp journal->j_last = be32_to_cpu(sb->s_maxlen); 1530470decc6SDave Kleikamp journal->j_errno = be32_to_cpu(sb->s_errno); 1531470decc6SDave Kleikamp 1532470decc6SDave Kleikamp return 0; 1533470decc6SDave Kleikamp } 1534470decc6SDave Kleikamp 1535470decc6SDave Kleikamp 1536470decc6SDave Kleikamp /** 1537f7f4bccbSMingming Cao * int jbd2_journal_load() - Read journal from disk. 1538470decc6SDave Kleikamp * @journal: Journal to act on. 1539470decc6SDave Kleikamp * 1540470decc6SDave Kleikamp * Given a journal_t structure which tells us which disk blocks contain 1541470decc6SDave Kleikamp * a journal, read the journal from disk to initialise the in-memory 1542470decc6SDave Kleikamp * structures. 1543470decc6SDave Kleikamp */ 1544f7f4bccbSMingming Cao int jbd2_journal_load(journal_t *journal) 1545470decc6SDave Kleikamp { 1546470decc6SDave Kleikamp int err; 1547470decc6SDave Kleikamp journal_superblock_t *sb; 1548470decc6SDave Kleikamp 1549470decc6SDave Kleikamp err = load_superblock(journal); 1550470decc6SDave Kleikamp if (err) 1551470decc6SDave Kleikamp return err; 1552470decc6SDave Kleikamp 1553470decc6SDave Kleikamp sb = journal->j_superblock; 1554470decc6SDave Kleikamp /* If this is a V2 superblock, then we have to check the 1555470decc6SDave Kleikamp * features flags on it. */ 1556470decc6SDave Kleikamp 1557470decc6SDave Kleikamp if (journal->j_format_version >= 2) { 1558470decc6SDave Kleikamp if ((sb->s_feature_ro_compat & 1559f7f4bccbSMingming Cao ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) || 1560470decc6SDave Kleikamp (sb->s_feature_incompat & 1561f7f4bccbSMingming Cao ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) { 1562470decc6SDave Kleikamp printk(KERN_WARNING 1563f2a44523SEryu Guan "JBD2: Unrecognised features on journal\n"); 1564470decc6SDave Kleikamp return -EINVAL; 1565470decc6SDave Kleikamp } 1566470decc6SDave Kleikamp } 1567470decc6SDave Kleikamp 1568d2eecb03STheodore Ts'o /* 1569d2eecb03STheodore Ts'o * Create a slab for this blocksize 1570d2eecb03STheodore Ts'o */ 1571d2eecb03STheodore Ts'o err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize)); 1572d2eecb03STheodore Ts'o if (err) 1573d2eecb03STheodore Ts'o return err; 1574d2eecb03STheodore Ts'o 1575470decc6SDave Kleikamp /* Let the recovery code check whether it needs to recover any 1576470decc6SDave Kleikamp * data from the journal. */ 1577f7f4bccbSMingming Cao if (jbd2_journal_recover(journal)) 1578470decc6SDave Kleikamp goto recovery_error; 1579470decc6SDave Kleikamp 1580e6a47428STheodore Ts'o if (journal->j_failed_commit) { 1581e6a47428STheodore Ts'o printk(KERN_ERR "JBD2: journal transaction %u on %s " 1582e6a47428STheodore Ts'o "is corrupt.\n", journal->j_failed_commit, 1583e6a47428STheodore Ts'o journal->j_devname); 1584e6a47428STheodore Ts'o return -EIO; 1585e6a47428STheodore Ts'o } 1586e6a47428STheodore Ts'o 1587470decc6SDave Kleikamp /* OK, we've finished with the dynamic journal bits: 1588470decc6SDave Kleikamp * reinitialise the dynamic contents of the superblock in memory 1589470decc6SDave Kleikamp * and reset them on disk. */ 1590470decc6SDave Kleikamp if (journal_reset(journal)) 1591470decc6SDave Kleikamp goto recovery_error; 1592470decc6SDave Kleikamp 1593f7f4bccbSMingming Cao journal->j_flags &= ~JBD2_ABORT; 1594f7f4bccbSMingming Cao journal->j_flags |= JBD2_LOADED; 1595470decc6SDave Kleikamp return 0; 1596470decc6SDave Kleikamp 1597470decc6SDave Kleikamp recovery_error: 1598f2a44523SEryu Guan printk(KERN_WARNING "JBD2: recovery failed\n"); 1599470decc6SDave Kleikamp return -EIO; 1600470decc6SDave Kleikamp } 1601470decc6SDave Kleikamp 1602470decc6SDave Kleikamp /** 1603f7f4bccbSMingming Cao * void jbd2_journal_destroy() - Release a journal_t structure. 1604470decc6SDave Kleikamp * @journal: Journal to act on. 1605470decc6SDave Kleikamp * 1606470decc6SDave Kleikamp * Release a journal_t structure once it is no longer in use by the 1607470decc6SDave Kleikamp * journaled object. 160844519fafSHidehiro Kawai * Return <0 if we couldn't clean up the journal. 1609470decc6SDave Kleikamp */ 161044519fafSHidehiro Kawai int jbd2_journal_destroy(journal_t *journal) 1611470decc6SDave Kleikamp { 161244519fafSHidehiro Kawai int err = 0; 161344519fafSHidehiro Kawai 1614470decc6SDave Kleikamp /* Wait for the commit thread to wake up and die. */ 1615470decc6SDave Kleikamp journal_kill_thread(journal); 1616470decc6SDave Kleikamp 1617470decc6SDave Kleikamp /* Force a final log commit */ 1618470decc6SDave Kleikamp if (journal->j_running_transaction) 1619f7f4bccbSMingming Cao jbd2_journal_commit_transaction(journal); 1620470decc6SDave Kleikamp 1621470decc6SDave Kleikamp /* Force any old transactions to disk */ 1622470decc6SDave Kleikamp 1623470decc6SDave Kleikamp /* Totally anal locking here... */ 1624470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 1625470decc6SDave Kleikamp while (journal->j_checkpoint_transactions != NULL) { 1626470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 16271a0d3786STheodore Ts'o mutex_lock(&journal->j_checkpoint_mutex); 1628f7f4bccbSMingming Cao jbd2_log_do_checkpoint(journal); 16291a0d3786STheodore Ts'o mutex_unlock(&journal->j_checkpoint_mutex); 1630470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 1631470decc6SDave Kleikamp } 1632470decc6SDave Kleikamp 1633470decc6SDave Kleikamp J_ASSERT(journal->j_running_transaction == NULL); 1634470decc6SDave Kleikamp J_ASSERT(journal->j_committing_transaction == NULL); 1635470decc6SDave Kleikamp J_ASSERT(journal->j_checkpoint_transactions == NULL); 1636470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 1637470decc6SDave Kleikamp 163844519fafSHidehiro Kawai if (journal->j_sb_buffer) { 163944519fafSHidehiro Kawai if (!is_journal_aborted(journal)) { 1640a78bb11dSJan Kara mutex_lock(&journal->j_checkpoint_mutex); 164124bcc89cSJan Kara jbd2_mark_journal_empty(journal); 1642a78bb11dSJan Kara mutex_unlock(&journal->j_checkpoint_mutex); 1643a78bb11dSJan Kara } else 164444519fafSHidehiro Kawai err = -EIO; 1645470decc6SDave Kleikamp brelse(journal->j_sb_buffer); 1646470decc6SDave Kleikamp } 1647470decc6SDave Kleikamp 16488e85fb3fSJohann Lombardi if (journal->j_proc_entry) 16498e85fb3fSJohann Lombardi jbd2_stats_proc_exit(journal); 1650470decc6SDave Kleikamp if (journal->j_inode) 1651470decc6SDave Kleikamp iput(journal->j_inode); 1652470decc6SDave Kleikamp if (journal->j_revoke) 1653f7f4bccbSMingming Cao jbd2_journal_destroy_revoke(journal); 165401b5adceSDarrick J. Wong if (journal->j_chksum_driver) 165501b5adceSDarrick J. Wong crypto_free_shash(journal->j_chksum_driver); 1656470decc6SDave Kleikamp kfree(journal->j_wbuf); 1657470decc6SDave Kleikamp kfree(journal); 165844519fafSHidehiro Kawai 165944519fafSHidehiro Kawai return err; 1660470decc6SDave Kleikamp } 1661470decc6SDave Kleikamp 1662470decc6SDave Kleikamp 1663470decc6SDave Kleikamp /** 1664f7f4bccbSMingming Cao *int jbd2_journal_check_used_features () - Check if features specified are used. 1665470decc6SDave Kleikamp * @journal: Journal to check. 1666470decc6SDave Kleikamp * @compat: bitmask of compatible features 1667470decc6SDave Kleikamp * @ro: bitmask of features that force read-only mount 1668470decc6SDave Kleikamp * @incompat: bitmask of incompatible features 1669470decc6SDave Kleikamp * 1670470decc6SDave Kleikamp * Check whether the journal uses all of a given set of 1671470decc6SDave Kleikamp * features. Return true (non-zero) if it does. 1672470decc6SDave Kleikamp **/ 1673470decc6SDave Kleikamp 1674f7f4bccbSMingming Cao int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat, 1675470decc6SDave Kleikamp unsigned long ro, unsigned long incompat) 1676470decc6SDave Kleikamp { 1677470decc6SDave Kleikamp journal_superblock_t *sb; 1678470decc6SDave Kleikamp 1679470decc6SDave Kleikamp if (!compat && !ro && !incompat) 1680470decc6SDave Kleikamp return 1; 16811113e1b5SPatrick J. LoPresti /* Load journal superblock if it is not loaded yet. */ 16821113e1b5SPatrick J. LoPresti if (journal->j_format_version == 0 && 16831113e1b5SPatrick J. LoPresti journal_get_superblock(journal) != 0) 16841113e1b5SPatrick J. LoPresti return 0; 1685470decc6SDave Kleikamp if (journal->j_format_version == 1) 1686470decc6SDave Kleikamp return 0; 1687470decc6SDave Kleikamp 1688470decc6SDave Kleikamp sb = journal->j_superblock; 1689470decc6SDave Kleikamp 1690470decc6SDave Kleikamp if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) && 1691470decc6SDave Kleikamp ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) && 1692470decc6SDave Kleikamp ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat)) 1693470decc6SDave Kleikamp return 1; 1694470decc6SDave Kleikamp 1695470decc6SDave Kleikamp return 0; 1696470decc6SDave Kleikamp } 1697470decc6SDave Kleikamp 1698470decc6SDave Kleikamp /** 1699f7f4bccbSMingming Cao * int jbd2_journal_check_available_features() - Check feature set in journalling layer 1700470decc6SDave Kleikamp * @journal: Journal to check. 1701470decc6SDave Kleikamp * @compat: bitmask of compatible features 1702470decc6SDave Kleikamp * @ro: bitmask of features that force read-only mount 1703470decc6SDave Kleikamp * @incompat: bitmask of incompatible features 1704470decc6SDave Kleikamp * 1705470decc6SDave Kleikamp * Check whether the journaling code supports the use of 1706470decc6SDave Kleikamp * all of a given set of features on this journal. Return true 1707470decc6SDave Kleikamp * (non-zero) if it can. */ 1708470decc6SDave Kleikamp 1709f7f4bccbSMingming Cao int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat, 1710470decc6SDave Kleikamp unsigned long ro, unsigned long incompat) 1711470decc6SDave Kleikamp { 1712470decc6SDave Kleikamp if (!compat && !ro && !incompat) 1713470decc6SDave Kleikamp return 1; 1714470decc6SDave Kleikamp 1715470decc6SDave Kleikamp /* We can support any known requested features iff the 1716470decc6SDave Kleikamp * superblock is in version 2. Otherwise we fail to support any 1717470decc6SDave Kleikamp * extended sb features. */ 1718470decc6SDave Kleikamp 1719470decc6SDave Kleikamp if (journal->j_format_version != 2) 1720470decc6SDave Kleikamp return 0; 1721470decc6SDave Kleikamp 1722f7f4bccbSMingming Cao if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat && 1723f7f4bccbSMingming Cao (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro && 1724f7f4bccbSMingming Cao (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat) 1725470decc6SDave Kleikamp return 1; 1726470decc6SDave Kleikamp 1727470decc6SDave Kleikamp return 0; 1728470decc6SDave Kleikamp } 1729470decc6SDave Kleikamp 1730470decc6SDave Kleikamp /** 1731f7f4bccbSMingming Cao * int jbd2_journal_set_features () - Mark a given journal feature in the superblock 1732470decc6SDave Kleikamp * @journal: Journal to act on. 1733470decc6SDave Kleikamp * @compat: bitmask of compatible features 1734470decc6SDave Kleikamp * @ro: bitmask of features that force read-only mount 1735470decc6SDave Kleikamp * @incompat: bitmask of incompatible features 1736470decc6SDave Kleikamp * 1737470decc6SDave Kleikamp * Mark a given journal feature as present on the 1738470decc6SDave Kleikamp * superblock. Returns true if the requested features could be set. 1739470decc6SDave Kleikamp * 1740470decc6SDave Kleikamp */ 1741470decc6SDave Kleikamp 1742f7f4bccbSMingming Cao int jbd2_journal_set_features (journal_t *journal, unsigned long compat, 1743470decc6SDave Kleikamp unsigned long ro, unsigned long incompat) 1744470decc6SDave Kleikamp { 174525ed6e8aSDarrick J. Wong #define INCOMPAT_FEATURE_ON(f) \ 174625ed6e8aSDarrick J. Wong ((incompat & (f)) && !(sb->s_feature_incompat & cpu_to_be32(f))) 174725ed6e8aSDarrick J. Wong #define COMPAT_FEATURE_ON(f) \ 174825ed6e8aSDarrick J. Wong ((compat & (f)) && !(sb->s_feature_compat & cpu_to_be32(f))) 1749470decc6SDave Kleikamp journal_superblock_t *sb; 1750470decc6SDave Kleikamp 1751f7f4bccbSMingming Cao if (jbd2_journal_check_used_features(journal, compat, ro, incompat)) 1752470decc6SDave Kleikamp return 1; 1753470decc6SDave Kleikamp 1754f7f4bccbSMingming Cao if (!jbd2_journal_check_available_features(journal, compat, ro, incompat)) 1755470decc6SDave Kleikamp return 0; 1756470decc6SDave Kleikamp 175725ed6e8aSDarrick J. Wong /* Asking for checksumming v2 and v1? Only give them v2. */ 175825ed6e8aSDarrick J. Wong if (incompat & JBD2_FEATURE_INCOMPAT_CSUM_V2 && 175925ed6e8aSDarrick J. Wong compat & JBD2_FEATURE_COMPAT_CHECKSUM) 176025ed6e8aSDarrick J. Wong compat &= ~JBD2_FEATURE_COMPAT_CHECKSUM; 176125ed6e8aSDarrick J. Wong 1762470decc6SDave Kleikamp jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n", 1763470decc6SDave Kleikamp compat, ro, incompat); 1764470decc6SDave Kleikamp 1765470decc6SDave Kleikamp sb = journal->j_superblock; 1766470decc6SDave Kleikamp 176725ed6e8aSDarrick J. Wong /* If enabling v2 checksums, update superblock */ 176825ed6e8aSDarrick J. Wong if (INCOMPAT_FEATURE_ON(JBD2_FEATURE_INCOMPAT_CSUM_V2)) { 176925ed6e8aSDarrick J. Wong sb->s_checksum_type = JBD2_CRC32C_CHKSUM; 177025ed6e8aSDarrick J. Wong sb->s_feature_compat &= 177125ed6e8aSDarrick J. Wong ~cpu_to_be32(JBD2_FEATURE_COMPAT_CHECKSUM); 177201b5adceSDarrick J. Wong 177301b5adceSDarrick J. Wong /* Load the checksum driver */ 177401b5adceSDarrick J. Wong if (journal->j_chksum_driver == NULL) { 177501b5adceSDarrick J. Wong journal->j_chksum_driver = crypto_alloc_shash("crc32c", 177601b5adceSDarrick J. Wong 0, 0); 177701b5adceSDarrick J. Wong if (IS_ERR(journal->j_chksum_driver)) { 177801b5adceSDarrick J. Wong printk(KERN_ERR "JBD: Cannot load crc32c " 177901b5adceSDarrick J. Wong "driver.\n"); 178001b5adceSDarrick J. Wong journal->j_chksum_driver = NULL; 178101b5adceSDarrick J. Wong return 0; 178201b5adceSDarrick J. Wong } 178301b5adceSDarrick J. Wong } 17844fd5ea43SDarrick J. Wong 17854fd5ea43SDarrick J. Wong /* Precompute checksum seed for all metadata */ 17864fd5ea43SDarrick J. Wong if (JBD2_HAS_INCOMPAT_FEATURE(journal, 17874fd5ea43SDarrick J. Wong JBD2_FEATURE_INCOMPAT_CSUM_V2)) 17884fd5ea43SDarrick J. Wong journal->j_csum_seed = jbd2_chksum(journal, ~0, 17894fd5ea43SDarrick J. Wong sb->s_uuid, 17904fd5ea43SDarrick J. Wong sizeof(sb->s_uuid)); 179125ed6e8aSDarrick J. Wong } 179225ed6e8aSDarrick J. Wong 179325ed6e8aSDarrick J. Wong /* If enabling v1 checksums, downgrade superblock */ 179425ed6e8aSDarrick J. Wong if (COMPAT_FEATURE_ON(JBD2_FEATURE_COMPAT_CHECKSUM)) 179525ed6e8aSDarrick J. Wong sb->s_feature_incompat &= 179625ed6e8aSDarrick J. Wong ~cpu_to_be32(JBD2_FEATURE_INCOMPAT_CSUM_V2); 179725ed6e8aSDarrick J. Wong 1798470decc6SDave Kleikamp sb->s_feature_compat |= cpu_to_be32(compat); 1799470decc6SDave Kleikamp sb->s_feature_ro_compat |= cpu_to_be32(ro); 1800470decc6SDave Kleikamp sb->s_feature_incompat |= cpu_to_be32(incompat); 1801470decc6SDave Kleikamp 1802470decc6SDave Kleikamp return 1; 180325ed6e8aSDarrick J. Wong #undef COMPAT_FEATURE_ON 180425ed6e8aSDarrick J. Wong #undef INCOMPAT_FEATURE_ON 1805470decc6SDave Kleikamp } 1806470decc6SDave Kleikamp 1807818d276cSGirish Shilamkar /* 1808818d276cSGirish Shilamkar * jbd2_journal_clear_features () - Clear a given journal feature in the 1809818d276cSGirish Shilamkar * superblock 1810818d276cSGirish Shilamkar * @journal: Journal to act on. 1811818d276cSGirish Shilamkar * @compat: bitmask of compatible features 1812818d276cSGirish Shilamkar * @ro: bitmask of features that force read-only mount 1813818d276cSGirish Shilamkar * @incompat: bitmask of incompatible features 1814818d276cSGirish Shilamkar * 1815818d276cSGirish Shilamkar * Clear a given journal feature as present on the 1816818d276cSGirish Shilamkar * superblock. 1817818d276cSGirish Shilamkar */ 1818818d276cSGirish Shilamkar void jbd2_journal_clear_features(journal_t *journal, unsigned long compat, 1819818d276cSGirish Shilamkar unsigned long ro, unsigned long incompat) 1820818d276cSGirish Shilamkar { 1821818d276cSGirish Shilamkar journal_superblock_t *sb; 1822818d276cSGirish Shilamkar 1823818d276cSGirish Shilamkar jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n", 1824818d276cSGirish Shilamkar compat, ro, incompat); 1825818d276cSGirish Shilamkar 1826818d276cSGirish Shilamkar sb = journal->j_superblock; 1827818d276cSGirish Shilamkar 1828818d276cSGirish Shilamkar sb->s_feature_compat &= ~cpu_to_be32(compat); 1829818d276cSGirish Shilamkar sb->s_feature_ro_compat &= ~cpu_to_be32(ro); 1830818d276cSGirish Shilamkar sb->s_feature_incompat &= ~cpu_to_be32(incompat); 1831818d276cSGirish Shilamkar } 1832818d276cSGirish Shilamkar EXPORT_SYMBOL(jbd2_journal_clear_features); 1833470decc6SDave Kleikamp 1834470decc6SDave Kleikamp /** 1835f7f4bccbSMingming Cao * int jbd2_journal_flush () - Flush journal 1836470decc6SDave Kleikamp * @journal: Journal to act on. 1837470decc6SDave Kleikamp * 1838470decc6SDave Kleikamp * Flush all data for a given journal to disk and empty the journal. 1839470decc6SDave Kleikamp * Filesystems can use this when remounting readonly to ensure that 1840470decc6SDave Kleikamp * recovery does not need to happen on remount. 1841470decc6SDave Kleikamp */ 1842470decc6SDave Kleikamp 1843f7f4bccbSMingming Cao int jbd2_journal_flush(journal_t *journal) 1844470decc6SDave Kleikamp { 1845470decc6SDave Kleikamp int err = 0; 1846470decc6SDave Kleikamp transaction_t *transaction = NULL; 1847470decc6SDave Kleikamp 1848a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 1849470decc6SDave Kleikamp 1850470decc6SDave Kleikamp /* Force everything buffered to the log... */ 1851470decc6SDave Kleikamp if (journal->j_running_transaction) { 1852470decc6SDave Kleikamp transaction = journal->j_running_transaction; 1853f7f4bccbSMingming Cao __jbd2_log_start_commit(journal, transaction->t_tid); 1854470decc6SDave Kleikamp } else if (journal->j_committing_transaction) 1855470decc6SDave Kleikamp transaction = journal->j_committing_transaction; 1856470decc6SDave Kleikamp 1857470decc6SDave Kleikamp /* Wait for the log commit to complete... */ 1858470decc6SDave Kleikamp if (transaction) { 1859470decc6SDave Kleikamp tid_t tid = transaction->t_tid; 1860470decc6SDave Kleikamp 1861a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 1862f7f4bccbSMingming Cao jbd2_log_wait_commit(journal, tid); 1863470decc6SDave Kleikamp } else { 1864a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 1865470decc6SDave Kleikamp } 1866470decc6SDave Kleikamp 1867470decc6SDave Kleikamp /* ...and flush everything in the log out to disk. */ 1868470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 1869470decc6SDave Kleikamp while (!err && journal->j_checkpoint_transactions != NULL) { 1870470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 187144519fafSHidehiro Kawai mutex_lock(&journal->j_checkpoint_mutex); 1872f7f4bccbSMingming Cao err = jbd2_log_do_checkpoint(journal); 187344519fafSHidehiro Kawai mutex_unlock(&journal->j_checkpoint_mutex); 1874470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 1875470decc6SDave Kleikamp } 1876470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 187744519fafSHidehiro Kawai 187844519fafSHidehiro Kawai if (is_journal_aborted(journal)) 187944519fafSHidehiro Kawai return -EIO; 188044519fafSHidehiro Kawai 1881a78bb11dSJan Kara mutex_lock(&journal->j_checkpoint_mutex); 1882f7f4bccbSMingming Cao jbd2_cleanup_journal_tail(journal); 1883470decc6SDave Kleikamp 1884470decc6SDave Kleikamp /* Finally, mark the journal as really needing no recovery. 1885470decc6SDave Kleikamp * This sets s_start==0 in the underlying superblock, which is 1886470decc6SDave Kleikamp * the magic code for a fully-recovered superblock. Any future 1887470decc6SDave Kleikamp * commits of data to the journal will restore the current 1888470decc6SDave Kleikamp * s_start value. */ 188924bcc89cSJan Kara jbd2_mark_journal_empty(journal); 1890a78bb11dSJan Kara mutex_unlock(&journal->j_checkpoint_mutex); 1891a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 1892470decc6SDave Kleikamp J_ASSERT(!journal->j_running_transaction); 1893470decc6SDave Kleikamp J_ASSERT(!journal->j_committing_transaction); 1894470decc6SDave Kleikamp J_ASSERT(!journal->j_checkpoint_transactions); 1895470decc6SDave Kleikamp J_ASSERT(journal->j_head == journal->j_tail); 1896470decc6SDave Kleikamp J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence); 1897a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 189844519fafSHidehiro Kawai return 0; 1899470decc6SDave Kleikamp } 1900470decc6SDave Kleikamp 1901470decc6SDave Kleikamp /** 1902f7f4bccbSMingming Cao * int jbd2_journal_wipe() - Wipe journal contents 1903470decc6SDave Kleikamp * @journal: Journal to act on. 1904470decc6SDave Kleikamp * @write: flag (see below) 1905470decc6SDave Kleikamp * 1906470decc6SDave Kleikamp * Wipe out all of the contents of a journal, safely. This will produce 1907470decc6SDave Kleikamp * a warning if the journal contains any valid recovery information. 1908f7f4bccbSMingming Cao * Must be called between journal_init_*() and jbd2_journal_load(). 1909470decc6SDave Kleikamp * 1910470decc6SDave Kleikamp * If 'write' is non-zero, then we wipe out the journal on disk; otherwise 1911470decc6SDave Kleikamp * we merely suppress recovery. 1912470decc6SDave Kleikamp */ 1913470decc6SDave Kleikamp 1914f7f4bccbSMingming Cao int jbd2_journal_wipe(journal_t *journal, int write) 1915470decc6SDave Kleikamp { 1916470decc6SDave Kleikamp int err = 0; 1917470decc6SDave Kleikamp 1918f7f4bccbSMingming Cao J_ASSERT (!(journal->j_flags & JBD2_LOADED)); 1919470decc6SDave Kleikamp 1920470decc6SDave Kleikamp err = load_superblock(journal); 1921470decc6SDave Kleikamp if (err) 1922470decc6SDave Kleikamp return err; 1923470decc6SDave Kleikamp 1924470decc6SDave Kleikamp if (!journal->j_tail) 1925470decc6SDave Kleikamp goto no_recovery; 1926470decc6SDave Kleikamp 1927f2a44523SEryu Guan printk(KERN_WARNING "JBD2: %s recovery information on journal\n", 1928470decc6SDave Kleikamp write ? "Clearing" : "Ignoring"); 1929470decc6SDave Kleikamp 1930f7f4bccbSMingming Cao err = jbd2_journal_skip_recovery(journal); 1931a78bb11dSJan Kara if (write) { 1932a78bb11dSJan Kara /* Lock to make assertions happy... */ 1933a78bb11dSJan Kara mutex_lock(&journal->j_checkpoint_mutex); 193424bcc89cSJan Kara jbd2_mark_journal_empty(journal); 1935a78bb11dSJan Kara mutex_unlock(&journal->j_checkpoint_mutex); 1936a78bb11dSJan Kara } 1937470decc6SDave Kleikamp 1938470decc6SDave Kleikamp no_recovery: 1939470decc6SDave Kleikamp return err; 1940470decc6SDave Kleikamp } 1941470decc6SDave Kleikamp 1942470decc6SDave Kleikamp /* 1943470decc6SDave Kleikamp * Journal abort has very specific semantics, which we describe 1944470decc6SDave Kleikamp * for journal abort. 1945470decc6SDave Kleikamp * 1946bfcd3555SAlberto Bertogli * Two internal functions, which provide abort to the jbd layer 1947470decc6SDave Kleikamp * itself are here. 1948470decc6SDave Kleikamp */ 1949470decc6SDave Kleikamp 1950470decc6SDave Kleikamp /* 1951470decc6SDave Kleikamp * Quick version for internal journal use (doesn't lock the journal). 1952470decc6SDave Kleikamp * Aborts hard --- we mark the abort as occurred, but do _nothing_ else, 1953470decc6SDave Kleikamp * and don't attempt to make any other journal updates. 1954470decc6SDave Kleikamp */ 1955f7f4bccbSMingming Cao void __jbd2_journal_abort_hard(journal_t *journal) 1956470decc6SDave Kleikamp { 1957470decc6SDave Kleikamp transaction_t *transaction; 1958470decc6SDave Kleikamp 1959f7f4bccbSMingming Cao if (journal->j_flags & JBD2_ABORT) 1960470decc6SDave Kleikamp return; 1961470decc6SDave Kleikamp 1962470decc6SDave Kleikamp printk(KERN_ERR "Aborting journal on device %s.\n", 196305496769STheodore Ts'o journal->j_devname); 1964470decc6SDave Kleikamp 1965a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 1966f7f4bccbSMingming Cao journal->j_flags |= JBD2_ABORT; 1967470decc6SDave Kleikamp transaction = journal->j_running_transaction; 1968470decc6SDave Kleikamp if (transaction) 1969f7f4bccbSMingming Cao __jbd2_log_start_commit(journal, transaction->t_tid); 1970a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 1971470decc6SDave Kleikamp } 1972470decc6SDave Kleikamp 1973470decc6SDave Kleikamp /* Soft abort: record the abort error status in the journal superblock, 1974470decc6SDave Kleikamp * but don't do any other IO. */ 1975470decc6SDave Kleikamp static void __journal_abort_soft (journal_t *journal, int errno) 1976470decc6SDave Kleikamp { 1977f7f4bccbSMingming Cao if (journal->j_flags & JBD2_ABORT) 1978470decc6SDave Kleikamp return; 1979470decc6SDave Kleikamp 1980470decc6SDave Kleikamp if (!journal->j_errno) 1981470decc6SDave Kleikamp journal->j_errno = errno; 1982470decc6SDave Kleikamp 1983f7f4bccbSMingming Cao __jbd2_journal_abort_hard(journal); 1984470decc6SDave Kleikamp 1985470decc6SDave Kleikamp if (errno) 198624bcc89cSJan Kara jbd2_journal_update_sb_errno(journal); 1987470decc6SDave Kleikamp } 1988470decc6SDave Kleikamp 1989470decc6SDave Kleikamp /** 1990f7f4bccbSMingming Cao * void jbd2_journal_abort () - Shutdown the journal immediately. 1991470decc6SDave Kleikamp * @journal: the journal to shutdown. 1992470decc6SDave Kleikamp * @errno: an error number to record in the journal indicating 1993470decc6SDave Kleikamp * the reason for the shutdown. 1994470decc6SDave Kleikamp * 1995470decc6SDave Kleikamp * Perform a complete, immediate shutdown of the ENTIRE 1996470decc6SDave Kleikamp * journal (not of a single transaction). This operation cannot be 1997470decc6SDave Kleikamp * undone without closing and reopening the journal. 1998470decc6SDave Kleikamp * 1999f7f4bccbSMingming Cao * The jbd2_journal_abort function is intended to support higher level error 2000470decc6SDave Kleikamp * recovery mechanisms such as the ext2/ext3 remount-readonly error 2001470decc6SDave Kleikamp * mode. 2002470decc6SDave Kleikamp * 2003470decc6SDave Kleikamp * Journal abort has very specific semantics. Any existing dirty, 2004470decc6SDave Kleikamp * unjournaled buffers in the main filesystem will still be written to 2005470decc6SDave Kleikamp * disk by bdflush, but the journaling mechanism will be suspended 2006470decc6SDave Kleikamp * immediately and no further transaction commits will be honoured. 2007470decc6SDave Kleikamp * 2008470decc6SDave Kleikamp * Any dirty, journaled buffers will be written back to disk without 2009470decc6SDave Kleikamp * hitting the journal. Atomicity cannot be guaranteed on an aborted 2010470decc6SDave Kleikamp * filesystem, but we _do_ attempt to leave as much data as possible 2011470decc6SDave Kleikamp * behind for fsck to use for cleanup. 2012470decc6SDave Kleikamp * 2013470decc6SDave Kleikamp * Any attempt to get a new transaction handle on a journal which is in 2014470decc6SDave Kleikamp * ABORT state will just result in an -EROFS error return. A 2015f7f4bccbSMingming Cao * jbd2_journal_stop on an existing handle will return -EIO if we have 2016470decc6SDave Kleikamp * entered abort state during the update. 2017470decc6SDave Kleikamp * 2018470decc6SDave Kleikamp * Recursive transactions are not disturbed by journal abort until the 2019f7f4bccbSMingming Cao * final jbd2_journal_stop, which will receive the -EIO error. 2020470decc6SDave Kleikamp * 2021f7f4bccbSMingming Cao * Finally, the jbd2_journal_abort call allows the caller to supply an errno 2022470decc6SDave Kleikamp * which will be recorded (if possible) in the journal superblock. This 2023470decc6SDave Kleikamp * allows a client to record failure conditions in the middle of a 2024470decc6SDave Kleikamp * transaction without having to complete the transaction to record the 2025470decc6SDave Kleikamp * failure to disk. ext3_error, for example, now uses this 2026470decc6SDave Kleikamp * functionality. 2027470decc6SDave Kleikamp * 2028470decc6SDave Kleikamp * Errors which originate from within the journaling layer will NOT 2029470decc6SDave Kleikamp * supply an errno; a null errno implies that absolutely no further 2030470decc6SDave Kleikamp * writes are done to the journal (unless there are any already in 2031470decc6SDave Kleikamp * progress). 2032470decc6SDave Kleikamp * 2033470decc6SDave Kleikamp */ 2034470decc6SDave Kleikamp 2035f7f4bccbSMingming Cao void jbd2_journal_abort(journal_t *journal, int errno) 2036470decc6SDave Kleikamp { 2037470decc6SDave Kleikamp __journal_abort_soft(journal, errno); 2038470decc6SDave Kleikamp } 2039470decc6SDave Kleikamp 2040470decc6SDave Kleikamp /** 2041f7f4bccbSMingming Cao * int jbd2_journal_errno () - returns the journal's error state. 2042470decc6SDave Kleikamp * @journal: journal to examine. 2043470decc6SDave Kleikamp * 2044bfcd3555SAlberto Bertogli * This is the errno number set with jbd2_journal_abort(), the last 2045470decc6SDave Kleikamp * time the journal was mounted - if the journal was stopped 2046470decc6SDave Kleikamp * without calling abort this will be 0. 2047470decc6SDave Kleikamp * 2048470decc6SDave Kleikamp * If the journal has been aborted on this mount time -EROFS will 2049470decc6SDave Kleikamp * be returned. 2050470decc6SDave Kleikamp */ 2051f7f4bccbSMingming Cao int jbd2_journal_errno(journal_t *journal) 2052470decc6SDave Kleikamp { 2053470decc6SDave Kleikamp int err; 2054470decc6SDave Kleikamp 2055a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 2056f7f4bccbSMingming Cao if (journal->j_flags & JBD2_ABORT) 2057470decc6SDave Kleikamp err = -EROFS; 2058470decc6SDave Kleikamp else 2059470decc6SDave Kleikamp err = journal->j_errno; 2060a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 2061470decc6SDave Kleikamp return err; 2062470decc6SDave Kleikamp } 2063470decc6SDave Kleikamp 2064470decc6SDave Kleikamp /** 2065f7f4bccbSMingming Cao * int jbd2_journal_clear_err () - clears the journal's error state 2066470decc6SDave Kleikamp * @journal: journal to act on. 2067470decc6SDave Kleikamp * 2068bfcd3555SAlberto Bertogli * An error must be cleared or acked to take a FS out of readonly 2069470decc6SDave Kleikamp * mode. 2070470decc6SDave Kleikamp */ 2071f7f4bccbSMingming Cao int jbd2_journal_clear_err(journal_t *journal) 2072470decc6SDave Kleikamp { 2073470decc6SDave Kleikamp int err = 0; 2074470decc6SDave Kleikamp 2075a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 2076f7f4bccbSMingming Cao if (journal->j_flags & JBD2_ABORT) 2077470decc6SDave Kleikamp err = -EROFS; 2078470decc6SDave Kleikamp else 2079470decc6SDave Kleikamp journal->j_errno = 0; 2080a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 2081470decc6SDave Kleikamp return err; 2082470decc6SDave Kleikamp } 2083470decc6SDave Kleikamp 2084470decc6SDave Kleikamp /** 2085f7f4bccbSMingming Cao * void jbd2_journal_ack_err() - Ack journal err. 2086470decc6SDave Kleikamp * @journal: journal to act on. 2087470decc6SDave Kleikamp * 2088bfcd3555SAlberto Bertogli * An error must be cleared or acked to take a FS out of readonly 2089470decc6SDave Kleikamp * mode. 2090470decc6SDave Kleikamp */ 2091f7f4bccbSMingming Cao void jbd2_journal_ack_err(journal_t *journal) 2092470decc6SDave Kleikamp { 2093a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 2094470decc6SDave Kleikamp if (journal->j_errno) 2095f7f4bccbSMingming Cao journal->j_flags |= JBD2_ACK_ERR; 2096a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 2097470decc6SDave Kleikamp } 2098470decc6SDave Kleikamp 2099f7f4bccbSMingming Cao int jbd2_journal_blocks_per_page(struct inode *inode) 2100470decc6SDave Kleikamp { 2101470decc6SDave Kleikamp return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); 2102470decc6SDave Kleikamp } 2103470decc6SDave Kleikamp 2104470decc6SDave Kleikamp /* 2105b517bea1SZach Brown * helper functions to deal with 32 or 64bit block numbers. 2106b517bea1SZach Brown */ 2107b517bea1SZach Brown size_t journal_tag_bytes(journal_t *journal) 2108b517bea1SZach Brown { 2109c3900875SDarrick J. Wong journal_block_tag_t tag; 2110c3900875SDarrick J. Wong size_t x = 0; 2111c3900875SDarrick J. Wong 2112c3900875SDarrick J. Wong if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2)) 2113c3900875SDarrick J. Wong x += sizeof(tag.t_checksum); 2114c3900875SDarrick J. Wong 2115b517bea1SZach Brown if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT)) 2116c3900875SDarrick J. Wong return x + JBD2_TAG_SIZE64; 2117b517bea1SZach Brown else 2118c3900875SDarrick J. Wong return x + JBD2_TAG_SIZE32; 2119b517bea1SZach Brown } 2120b517bea1SZach Brown 2121b517bea1SZach Brown /* 2122d2eecb03STheodore Ts'o * JBD memory management 2123d2eecb03STheodore Ts'o * 2124d2eecb03STheodore Ts'o * These functions are used to allocate block-sized chunks of memory 2125d2eecb03STheodore Ts'o * used for making copies of buffer_head data. Very often it will be 2126d2eecb03STheodore Ts'o * page-sized chunks of data, but sometimes it will be in 2127d2eecb03STheodore Ts'o * sub-page-size chunks. (For example, 16k pages on Power systems 2128d2eecb03STheodore Ts'o * with a 4k block file system.) For blocks smaller than a page, we 2129d2eecb03STheodore Ts'o * use a SLAB allocator. There are slab caches for each block size, 2130d2eecb03STheodore Ts'o * which are allocated at mount time, if necessary, and we only free 2131d2eecb03STheodore Ts'o * (all of) the slab caches when/if the jbd2 module is unloaded. For 2132d2eecb03STheodore Ts'o * this reason we don't need to a mutex to protect access to 2133d2eecb03STheodore Ts'o * jbd2_slab[] allocating or releasing memory; only in 2134d2eecb03STheodore Ts'o * jbd2_journal_create_slab(). 2135d2eecb03STheodore Ts'o */ 2136d2eecb03STheodore Ts'o #define JBD2_MAX_SLABS 8 2137d2eecb03STheodore Ts'o static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS]; 2138d2eecb03STheodore Ts'o 2139d2eecb03STheodore Ts'o static const char *jbd2_slab_names[JBD2_MAX_SLABS] = { 2140d2eecb03STheodore Ts'o "jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k", 2141d2eecb03STheodore Ts'o "jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k" 2142d2eecb03STheodore Ts'o }; 2143d2eecb03STheodore Ts'o 2144d2eecb03STheodore Ts'o 2145d2eecb03STheodore Ts'o static void jbd2_journal_destroy_slabs(void) 2146d2eecb03STheodore Ts'o { 2147d2eecb03STheodore Ts'o int i; 2148d2eecb03STheodore Ts'o 2149d2eecb03STheodore Ts'o for (i = 0; i < JBD2_MAX_SLABS; i++) { 2150d2eecb03STheodore Ts'o if (jbd2_slab[i]) 2151d2eecb03STheodore Ts'o kmem_cache_destroy(jbd2_slab[i]); 2152d2eecb03STheodore Ts'o jbd2_slab[i] = NULL; 2153d2eecb03STheodore Ts'o } 2154d2eecb03STheodore Ts'o } 2155d2eecb03STheodore Ts'o 2156d2eecb03STheodore Ts'o static int jbd2_journal_create_slab(size_t size) 2157d2eecb03STheodore Ts'o { 215851dfacdeSThomas Gleixner static DEFINE_MUTEX(jbd2_slab_create_mutex); 2159d2eecb03STheodore Ts'o int i = order_base_2(size) - 10; 2160d2eecb03STheodore Ts'o size_t slab_size; 2161d2eecb03STheodore Ts'o 2162d2eecb03STheodore Ts'o if (size == PAGE_SIZE) 2163d2eecb03STheodore Ts'o return 0; 2164d2eecb03STheodore Ts'o 2165d2eecb03STheodore Ts'o if (i >= JBD2_MAX_SLABS) 2166d2eecb03STheodore Ts'o return -EINVAL; 2167d2eecb03STheodore Ts'o 2168d2eecb03STheodore Ts'o if (unlikely(i < 0)) 2169d2eecb03STheodore Ts'o i = 0; 217051dfacdeSThomas Gleixner mutex_lock(&jbd2_slab_create_mutex); 2171d2eecb03STheodore Ts'o if (jbd2_slab[i]) { 217251dfacdeSThomas Gleixner mutex_unlock(&jbd2_slab_create_mutex); 2173d2eecb03STheodore Ts'o return 0; /* Already created */ 2174d2eecb03STheodore Ts'o } 2175d2eecb03STheodore Ts'o 2176d2eecb03STheodore Ts'o slab_size = 1 << (i+10); 2177d2eecb03STheodore Ts'o jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size, 2178d2eecb03STheodore Ts'o slab_size, 0, NULL); 217951dfacdeSThomas Gleixner mutex_unlock(&jbd2_slab_create_mutex); 2180d2eecb03STheodore Ts'o if (!jbd2_slab[i]) { 2181d2eecb03STheodore Ts'o printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n"); 2182d2eecb03STheodore Ts'o return -ENOMEM; 2183d2eecb03STheodore Ts'o } 2184d2eecb03STheodore Ts'o return 0; 2185d2eecb03STheodore Ts'o } 2186d2eecb03STheodore Ts'o 2187d2eecb03STheodore Ts'o static struct kmem_cache *get_slab(size_t size) 2188d2eecb03STheodore Ts'o { 2189d2eecb03STheodore Ts'o int i = order_base_2(size) - 10; 2190d2eecb03STheodore Ts'o 2191d2eecb03STheodore Ts'o BUG_ON(i >= JBD2_MAX_SLABS); 2192d2eecb03STheodore Ts'o if (unlikely(i < 0)) 2193d2eecb03STheodore Ts'o i = 0; 21948ac97b74SBill Pemberton BUG_ON(jbd2_slab[i] == NULL); 2195d2eecb03STheodore Ts'o return jbd2_slab[i]; 2196d2eecb03STheodore Ts'o } 2197d2eecb03STheodore Ts'o 2198d2eecb03STheodore Ts'o void *jbd2_alloc(size_t size, gfp_t flags) 2199d2eecb03STheodore Ts'o { 2200d2eecb03STheodore Ts'o void *ptr; 2201d2eecb03STheodore Ts'o 2202d2eecb03STheodore Ts'o BUG_ON(size & (size-1)); /* Must be a power of 2 */ 2203d2eecb03STheodore Ts'o 2204d2eecb03STheodore Ts'o flags |= __GFP_REPEAT; 2205d2eecb03STheodore Ts'o if (size == PAGE_SIZE) 2206d2eecb03STheodore Ts'o ptr = (void *)__get_free_pages(flags, 0); 2207d2eecb03STheodore Ts'o else if (size > PAGE_SIZE) { 2208d2eecb03STheodore Ts'o int order = get_order(size); 2209d2eecb03STheodore Ts'o 2210d2eecb03STheodore Ts'o if (order < 3) 2211d2eecb03STheodore Ts'o ptr = (void *)__get_free_pages(flags, order); 2212d2eecb03STheodore Ts'o else 2213d2eecb03STheodore Ts'o ptr = vmalloc(size); 2214d2eecb03STheodore Ts'o } else 2215d2eecb03STheodore Ts'o ptr = kmem_cache_alloc(get_slab(size), flags); 2216d2eecb03STheodore Ts'o 2217d2eecb03STheodore Ts'o /* Check alignment; SLUB has gotten this wrong in the past, 2218d2eecb03STheodore Ts'o * and this can lead to user data corruption! */ 2219d2eecb03STheodore Ts'o BUG_ON(((unsigned long) ptr) & (size-1)); 2220d2eecb03STheodore Ts'o 2221d2eecb03STheodore Ts'o return ptr; 2222d2eecb03STheodore Ts'o } 2223d2eecb03STheodore Ts'o 2224d2eecb03STheodore Ts'o void jbd2_free(void *ptr, size_t size) 2225d2eecb03STheodore Ts'o { 2226d2eecb03STheodore Ts'o if (size == PAGE_SIZE) { 2227d2eecb03STheodore Ts'o free_pages((unsigned long)ptr, 0); 2228d2eecb03STheodore Ts'o return; 2229d2eecb03STheodore Ts'o } 2230d2eecb03STheodore Ts'o if (size > PAGE_SIZE) { 2231d2eecb03STheodore Ts'o int order = get_order(size); 2232d2eecb03STheodore Ts'o 2233d2eecb03STheodore Ts'o if (order < 3) 2234d2eecb03STheodore Ts'o free_pages((unsigned long)ptr, order); 2235d2eecb03STheodore Ts'o else 2236d2eecb03STheodore Ts'o vfree(ptr); 2237d2eecb03STheodore Ts'o return; 2238d2eecb03STheodore Ts'o } 2239d2eecb03STheodore Ts'o kmem_cache_free(get_slab(size), ptr); 2240d2eecb03STheodore Ts'o }; 2241d2eecb03STheodore Ts'o 2242d2eecb03STheodore Ts'o /* 2243470decc6SDave Kleikamp * Journal_head storage management 2244470decc6SDave Kleikamp */ 2245e18b890bSChristoph Lameter static struct kmem_cache *jbd2_journal_head_cache; 2246e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 2247470decc6SDave Kleikamp static atomic_t nr_journal_heads = ATOMIC_INIT(0); 2248470decc6SDave Kleikamp #endif 2249470decc6SDave Kleikamp 22504185a2acSYongqiang Yang static int jbd2_journal_init_journal_head_cache(void) 2251470decc6SDave Kleikamp { 2252470decc6SDave Kleikamp int retval; 2253470decc6SDave Kleikamp 22541076d17aSAl Viro J_ASSERT(jbd2_journal_head_cache == NULL); 2255a920e941SJohann Lombardi jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head", 2256470decc6SDave Kleikamp sizeof(struct journal_head), 2257470decc6SDave Kleikamp 0, /* offset */ 225877160957SMingming Cao SLAB_TEMPORARY, /* flags */ 225920c2df83SPaul Mundt NULL); /* ctor */ 2260470decc6SDave Kleikamp retval = 0; 22611076d17aSAl Viro if (!jbd2_journal_head_cache) { 2262470decc6SDave Kleikamp retval = -ENOMEM; 2263f2a44523SEryu Guan printk(KERN_EMERG "JBD2: no memory for journal_head cache\n"); 2264470decc6SDave Kleikamp } 2265470decc6SDave Kleikamp return retval; 2266470decc6SDave Kleikamp } 2267470decc6SDave Kleikamp 22684185a2acSYongqiang Yang static void jbd2_journal_destroy_journal_head_cache(void) 2269470decc6SDave Kleikamp { 22708a9362ebSDuane Griffin if (jbd2_journal_head_cache) { 2271f7f4bccbSMingming Cao kmem_cache_destroy(jbd2_journal_head_cache); 2272f7f4bccbSMingming Cao jbd2_journal_head_cache = NULL; 2273470decc6SDave Kleikamp } 22748a9362ebSDuane Griffin } 2275470decc6SDave Kleikamp 2276470decc6SDave Kleikamp /* 2277470decc6SDave Kleikamp * journal_head splicing and dicing 2278470decc6SDave Kleikamp */ 2279470decc6SDave Kleikamp static struct journal_head *journal_alloc_journal_head(void) 2280470decc6SDave Kleikamp { 2281470decc6SDave Kleikamp struct journal_head *ret; 2282470decc6SDave Kleikamp 2283e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 2284470decc6SDave Kleikamp atomic_inc(&nr_journal_heads); 2285470decc6SDave Kleikamp #endif 2286f7f4bccbSMingming Cao ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS); 22871076d17aSAl Viro if (!ret) { 2288470decc6SDave Kleikamp jbd_debug(1, "out of memory for journal_head\n"); 2289670be5a7STheodore Ts'o pr_notice_ratelimited("ENOMEM in %s, retrying.\n", __func__); 22901076d17aSAl Viro while (!ret) { 2291470decc6SDave Kleikamp yield(); 2292f7f4bccbSMingming Cao ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS); 2293470decc6SDave Kleikamp } 2294470decc6SDave Kleikamp } 2295470decc6SDave Kleikamp return ret; 2296470decc6SDave Kleikamp } 2297470decc6SDave Kleikamp 2298470decc6SDave Kleikamp static void journal_free_journal_head(struct journal_head *jh) 2299470decc6SDave Kleikamp { 2300e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 2301470decc6SDave Kleikamp atomic_dec(&nr_journal_heads); 2302cd02ff0bSMingming Cao memset(jh, JBD2_POISON_FREE, sizeof(*jh)); 2303470decc6SDave Kleikamp #endif 2304f7f4bccbSMingming Cao kmem_cache_free(jbd2_journal_head_cache, jh); 2305470decc6SDave Kleikamp } 2306470decc6SDave Kleikamp 2307470decc6SDave Kleikamp /* 2308470decc6SDave Kleikamp * A journal_head is attached to a buffer_head whenever JBD has an 2309470decc6SDave Kleikamp * interest in the buffer. 2310470decc6SDave Kleikamp * 2311470decc6SDave Kleikamp * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit 2312470decc6SDave Kleikamp * is set. This bit is tested in core kernel code where we need to take 2313470decc6SDave Kleikamp * JBD-specific actions. Testing the zeroness of ->b_private is not reliable 2314470decc6SDave Kleikamp * there. 2315470decc6SDave Kleikamp * 2316470decc6SDave Kleikamp * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one. 2317470decc6SDave Kleikamp * 2318470decc6SDave Kleikamp * When a buffer has its BH_JBD bit set it is immune from being released by 2319470decc6SDave Kleikamp * core kernel code, mainly via ->b_count. 2320470decc6SDave Kleikamp * 2321de1b7941SJan Kara * A journal_head is detached from its buffer_head when the journal_head's 2322de1b7941SJan Kara * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint 2323de1b7941SJan Kara * transaction (b_cp_transaction) hold their references to b_jcount. 2324470decc6SDave Kleikamp * 2325470decc6SDave Kleikamp * Various places in the kernel want to attach a journal_head to a buffer_head 2326470decc6SDave Kleikamp * _before_ attaching the journal_head to a transaction. To protect the 2327f7f4bccbSMingming Cao * journal_head in this situation, jbd2_journal_add_journal_head elevates the 2328470decc6SDave Kleikamp * journal_head's b_jcount refcount by one. The caller must call 2329f7f4bccbSMingming Cao * jbd2_journal_put_journal_head() to undo this. 2330470decc6SDave Kleikamp * 2331470decc6SDave Kleikamp * So the typical usage would be: 2332470decc6SDave Kleikamp * 2333470decc6SDave Kleikamp * (Attach a journal_head if needed. Increments b_jcount) 2334f7f4bccbSMingming Cao * struct journal_head *jh = jbd2_journal_add_journal_head(bh); 2335470decc6SDave Kleikamp * ... 2336de1b7941SJan Kara * (Get another reference for transaction) 2337de1b7941SJan Kara * jbd2_journal_grab_journal_head(bh); 2338470decc6SDave Kleikamp * jh->b_transaction = xxx; 2339de1b7941SJan Kara * (Put original reference) 2340f7f4bccbSMingming Cao * jbd2_journal_put_journal_head(jh); 2341470decc6SDave Kleikamp */ 2342470decc6SDave Kleikamp 2343470decc6SDave Kleikamp /* 2344470decc6SDave Kleikamp * Give a buffer_head a journal_head. 2345470decc6SDave Kleikamp * 2346470decc6SDave Kleikamp * May sleep. 2347470decc6SDave Kleikamp */ 2348f7f4bccbSMingming Cao struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh) 2349470decc6SDave Kleikamp { 2350470decc6SDave Kleikamp struct journal_head *jh; 2351470decc6SDave Kleikamp struct journal_head *new_jh = NULL; 2352470decc6SDave Kleikamp 2353470decc6SDave Kleikamp repeat: 2354470decc6SDave Kleikamp if (!buffer_jbd(bh)) { 2355470decc6SDave Kleikamp new_jh = journal_alloc_journal_head(); 2356470decc6SDave Kleikamp memset(new_jh, 0, sizeof(*new_jh)); 2357470decc6SDave Kleikamp } 2358470decc6SDave Kleikamp 2359470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 2360470decc6SDave Kleikamp if (buffer_jbd(bh)) { 2361470decc6SDave Kleikamp jh = bh2jh(bh); 2362470decc6SDave Kleikamp } else { 2363470decc6SDave Kleikamp J_ASSERT_BH(bh, 2364470decc6SDave Kleikamp (atomic_read(&bh->b_count) > 0) || 2365470decc6SDave Kleikamp (bh->b_page && bh->b_page->mapping)); 2366470decc6SDave Kleikamp 2367470decc6SDave Kleikamp if (!new_jh) { 2368470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 2369470decc6SDave Kleikamp goto repeat; 2370470decc6SDave Kleikamp } 2371470decc6SDave Kleikamp 2372470decc6SDave Kleikamp jh = new_jh; 2373470decc6SDave Kleikamp new_jh = NULL; /* We consumed it */ 2374470decc6SDave Kleikamp set_buffer_jbd(bh); 2375470decc6SDave Kleikamp bh->b_private = jh; 2376470decc6SDave Kleikamp jh->b_bh = bh; 2377470decc6SDave Kleikamp get_bh(bh); 2378470decc6SDave Kleikamp BUFFER_TRACE(bh, "added journal_head"); 2379470decc6SDave Kleikamp } 2380470decc6SDave Kleikamp jh->b_jcount++; 2381470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 2382470decc6SDave Kleikamp if (new_jh) 2383470decc6SDave Kleikamp journal_free_journal_head(new_jh); 2384470decc6SDave Kleikamp return bh->b_private; 2385470decc6SDave Kleikamp } 2386470decc6SDave Kleikamp 2387470decc6SDave Kleikamp /* 2388470decc6SDave Kleikamp * Grab a ref against this buffer_head's journal_head. If it ended up not 2389470decc6SDave Kleikamp * having a journal_head, return NULL 2390470decc6SDave Kleikamp */ 2391f7f4bccbSMingming Cao struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh) 2392470decc6SDave Kleikamp { 2393470decc6SDave Kleikamp struct journal_head *jh = NULL; 2394470decc6SDave Kleikamp 2395470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 2396470decc6SDave Kleikamp if (buffer_jbd(bh)) { 2397470decc6SDave Kleikamp jh = bh2jh(bh); 2398470decc6SDave Kleikamp jh->b_jcount++; 2399470decc6SDave Kleikamp } 2400470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 2401470decc6SDave Kleikamp return jh; 2402470decc6SDave Kleikamp } 2403470decc6SDave Kleikamp 2404470decc6SDave Kleikamp static void __journal_remove_journal_head(struct buffer_head *bh) 2405470decc6SDave Kleikamp { 2406470decc6SDave Kleikamp struct journal_head *jh = bh2jh(bh); 2407470decc6SDave Kleikamp 2408470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_jcount >= 0); 2409de1b7941SJan Kara J_ASSERT_JH(jh, jh->b_transaction == NULL); 2410de1b7941SJan Kara J_ASSERT_JH(jh, jh->b_next_transaction == NULL); 2411de1b7941SJan Kara J_ASSERT_JH(jh, jh->b_cp_transaction == NULL); 2412470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_jlist == BJ_None); 2413470decc6SDave Kleikamp J_ASSERT_BH(bh, buffer_jbd(bh)); 2414470decc6SDave Kleikamp J_ASSERT_BH(bh, jh2bh(jh) == bh); 2415470decc6SDave Kleikamp BUFFER_TRACE(bh, "remove journal_head"); 2416470decc6SDave Kleikamp if (jh->b_frozen_data) { 2417de1b7941SJan Kara printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__); 2418af1e76d6SMingming Cao jbd2_free(jh->b_frozen_data, bh->b_size); 2419470decc6SDave Kleikamp } 2420470decc6SDave Kleikamp if (jh->b_committed_data) { 2421de1b7941SJan Kara printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__); 2422af1e76d6SMingming Cao jbd2_free(jh->b_committed_data, bh->b_size); 2423470decc6SDave Kleikamp } 2424470decc6SDave Kleikamp bh->b_private = NULL; 2425470decc6SDave Kleikamp jh->b_bh = NULL; /* debug, really */ 2426470decc6SDave Kleikamp clear_buffer_jbd(bh); 2427470decc6SDave Kleikamp journal_free_journal_head(jh); 2428470decc6SDave Kleikamp } 2429470decc6SDave Kleikamp 2430470decc6SDave Kleikamp /* 2431de1b7941SJan Kara * Drop a reference on the passed journal_head. If it fell to zero then 2432470decc6SDave Kleikamp * release the journal_head from the buffer_head. 2433470decc6SDave Kleikamp */ 2434f7f4bccbSMingming Cao void jbd2_journal_put_journal_head(struct journal_head *jh) 2435470decc6SDave Kleikamp { 2436470decc6SDave Kleikamp struct buffer_head *bh = jh2bh(jh); 2437470decc6SDave Kleikamp 2438470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 2439470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_jcount > 0); 2440470decc6SDave Kleikamp --jh->b_jcount; 2441de1b7941SJan Kara if (!jh->b_jcount) { 2442470decc6SDave Kleikamp __journal_remove_journal_head(bh); 2443de1b7941SJan Kara jbd_unlock_bh_journal_head(bh); 2444470decc6SDave Kleikamp __brelse(bh); 2445de1b7941SJan Kara } else 2446470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 2447470decc6SDave Kleikamp } 2448470decc6SDave Kleikamp 2449470decc6SDave Kleikamp /* 2450c851ed54SJan Kara * Initialize jbd inode head 2451c851ed54SJan Kara */ 2452c851ed54SJan Kara void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode) 2453c851ed54SJan Kara { 2454c851ed54SJan Kara jinode->i_transaction = NULL; 2455c851ed54SJan Kara jinode->i_next_transaction = NULL; 2456c851ed54SJan Kara jinode->i_vfs_inode = inode; 2457c851ed54SJan Kara jinode->i_flags = 0; 2458c851ed54SJan Kara INIT_LIST_HEAD(&jinode->i_list); 2459c851ed54SJan Kara } 2460c851ed54SJan Kara 2461c851ed54SJan Kara /* 2462c851ed54SJan Kara * Function to be called before we start removing inode from memory (i.e., 2463c851ed54SJan Kara * clear_inode() is a fine place to be called from). It removes inode from 2464c851ed54SJan Kara * transaction's lists. 2465c851ed54SJan Kara */ 2466c851ed54SJan Kara void jbd2_journal_release_jbd_inode(journal_t *journal, 2467c851ed54SJan Kara struct jbd2_inode *jinode) 2468c851ed54SJan Kara { 2469c851ed54SJan Kara if (!journal) 2470c851ed54SJan Kara return; 2471c851ed54SJan Kara restart: 2472c851ed54SJan Kara spin_lock(&journal->j_list_lock); 2473c851ed54SJan Kara /* Is commit writing out inode - we have to wait */ 247439e3ac25SBrian King if (test_bit(__JI_COMMIT_RUNNING, &jinode->i_flags)) { 2475c851ed54SJan Kara wait_queue_head_t *wq; 2476c851ed54SJan Kara DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING); 2477c851ed54SJan Kara wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING); 2478c851ed54SJan Kara prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE); 2479c851ed54SJan Kara spin_unlock(&journal->j_list_lock); 2480c851ed54SJan Kara schedule(); 2481c851ed54SJan Kara finish_wait(wq, &wait.wait); 2482c851ed54SJan Kara goto restart; 2483c851ed54SJan Kara } 2484c851ed54SJan Kara 2485c851ed54SJan Kara if (jinode->i_transaction) { 2486c851ed54SJan Kara list_del(&jinode->i_list); 2487c851ed54SJan Kara jinode->i_transaction = NULL; 2488c851ed54SJan Kara } 2489c851ed54SJan Kara spin_unlock(&journal->j_list_lock); 2490c851ed54SJan Kara } 2491c851ed54SJan Kara 2492c851ed54SJan Kara /* 24930f49d5d0SJose R. Santos * debugfs tunables 2494470decc6SDave Kleikamp */ 24956f38c74fSJose R. Santos #ifdef CONFIG_JBD2_DEBUG 24966f38c74fSJose R. Santos u8 jbd2_journal_enable_debug __read_mostly; 2497f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_enable_debug); 2498470decc6SDave Kleikamp 24990f49d5d0SJose R. Santos #define JBD2_DEBUG_NAME "jbd2-debug" 2500470decc6SDave Kleikamp 25016f38c74fSJose R. Santos static struct dentry *jbd2_debugfs_dir; 25026f38c74fSJose R. Santos static struct dentry *jbd2_debug; 25030f49d5d0SJose R. Santos 25040f49d5d0SJose R. Santos static void __init jbd2_create_debugfs_entry(void) 2505470decc6SDave Kleikamp { 25060f49d5d0SJose R. Santos jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL); 25070f49d5d0SJose R. Santos if (jbd2_debugfs_dir) 2508765f8361SYin Kangkai jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME, 2509765f8361SYin Kangkai S_IRUGO | S_IWUSR, 25100f49d5d0SJose R. Santos jbd2_debugfs_dir, 25110f49d5d0SJose R. Santos &jbd2_journal_enable_debug); 2512470decc6SDave Kleikamp } 2513470decc6SDave Kleikamp 25140f49d5d0SJose R. Santos static void __exit jbd2_remove_debugfs_entry(void) 2515470decc6SDave Kleikamp { 25160f49d5d0SJose R. Santos debugfs_remove(jbd2_debug); 25170f49d5d0SJose R. Santos debugfs_remove(jbd2_debugfs_dir); 2518470decc6SDave Kleikamp } 2519470decc6SDave Kleikamp 2520470decc6SDave Kleikamp #else 2521470decc6SDave Kleikamp 25220f49d5d0SJose R. Santos static void __init jbd2_create_debugfs_entry(void) 25230f49d5d0SJose R. Santos { 25240f49d5d0SJose R. Santos } 25250f49d5d0SJose R. Santos 25260f49d5d0SJose R. Santos static void __exit jbd2_remove_debugfs_entry(void) 25270f49d5d0SJose R. Santos { 25280f49d5d0SJose R. Santos } 2529470decc6SDave Kleikamp 2530470decc6SDave Kleikamp #endif 2531470decc6SDave Kleikamp 25328e85fb3fSJohann Lombardi #ifdef CONFIG_PROC_FS 25338e85fb3fSJohann Lombardi 25348e85fb3fSJohann Lombardi #define JBD2_STATS_PROC_NAME "fs/jbd2" 25358e85fb3fSJohann Lombardi 25368e85fb3fSJohann Lombardi static void __init jbd2_create_jbd_stats_proc_entry(void) 25378e85fb3fSJohann Lombardi { 25388e85fb3fSJohann Lombardi proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL); 25398e85fb3fSJohann Lombardi } 25408e85fb3fSJohann Lombardi 25418e85fb3fSJohann Lombardi static void __exit jbd2_remove_jbd_stats_proc_entry(void) 25428e85fb3fSJohann Lombardi { 25438e85fb3fSJohann Lombardi if (proc_jbd2_stats) 25448e85fb3fSJohann Lombardi remove_proc_entry(JBD2_STATS_PROC_NAME, NULL); 25458e85fb3fSJohann Lombardi } 25468e85fb3fSJohann Lombardi 25478e85fb3fSJohann Lombardi #else 25488e85fb3fSJohann Lombardi 25498e85fb3fSJohann Lombardi #define jbd2_create_jbd_stats_proc_entry() do {} while (0) 25508e85fb3fSJohann Lombardi #define jbd2_remove_jbd_stats_proc_entry() do {} while (0) 25518e85fb3fSJohann Lombardi 25528e85fb3fSJohann Lombardi #endif 25538e85fb3fSJohann Lombardi 25548aefcd55STheodore Ts'o struct kmem_cache *jbd2_handle_cache, *jbd2_inode_cache; 2555470decc6SDave Kleikamp 25564185a2acSYongqiang Yang static int __init jbd2_journal_init_handle_cache(void) 2557470decc6SDave Kleikamp { 25588aefcd55STheodore Ts'o jbd2_handle_cache = KMEM_CACHE(jbd2_journal_handle, SLAB_TEMPORARY); 2559f7f4bccbSMingming Cao if (jbd2_handle_cache == NULL) { 25608aefcd55STheodore Ts'o printk(KERN_EMERG "JBD2: failed to create handle cache\n"); 25618aefcd55STheodore Ts'o return -ENOMEM; 25628aefcd55STheodore Ts'o } 25638aefcd55STheodore Ts'o jbd2_inode_cache = KMEM_CACHE(jbd2_inode, 0); 25648aefcd55STheodore Ts'o if (jbd2_inode_cache == NULL) { 25658aefcd55STheodore Ts'o printk(KERN_EMERG "JBD2: failed to create inode cache\n"); 25668aefcd55STheodore Ts'o kmem_cache_destroy(jbd2_handle_cache); 2567470decc6SDave Kleikamp return -ENOMEM; 2568470decc6SDave Kleikamp } 2569470decc6SDave Kleikamp return 0; 2570470decc6SDave Kleikamp } 2571470decc6SDave Kleikamp 2572f7f4bccbSMingming Cao static void jbd2_journal_destroy_handle_cache(void) 2573470decc6SDave Kleikamp { 2574f7f4bccbSMingming Cao if (jbd2_handle_cache) 2575f7f4bccbSMingming Cao kmem_cache_destroy(jbd2_handle_cache); 25768aefcd55STheodore Ts'o if (jbd2_inode_cache) 25778aefcd55STheodore Ts'o kmem_cache_destroy(jbd2_inode_cache); 25788aefcd55STheodore Ts'o 2579470decc6SDave Kleikamp } 2580470decc6SDave Kleikamp 2581470decc6SDave Kleikamp /* 2582470decc6SDave Kleikamp * Module startup and shutdown 2583470decc6SDave Kleikamp */ 2584470decc6SDave Kleikamp 2585470decc6SDave Kleikamp static int __init journal_init_caches(void) 2586470decc6SDave Kleikamp { 2587470decc6SDave Kleikamp int ret; 2588470decc6SDave Kleikamp 2589f7f4bccbSMingming Cao ret = jbd2_journal_init_revoke_caches(); 2590470decc6SDave Kleikamp if (ret == 0) 25914185a2acSYongqiang Yang ret = jbd2_journal_init_journal_head_cache(); 2592470decc6SDave Kleikamp if (ret == 0) 25934185a2acSYongqiang Yang ret = jbd2_journal_init_handle_cache(); 25940c2022ecSYongqiang Yang if (ret == 0) 25950c2022ecSYongqiang Yang ret = jbd2_journal_init_transaction_cache(); 2596470decc6SDave Kleikamp return ret; 2597470decc6SDave Kleikamp } 2598470decc6SDave Kleikamp 2599f7f4bccbSMingming Cao static void jbd2_journal_destroy_caches(void) 2600470decc6SDave Kleikamp { 2601f7f4bccbSMingming Cao jbd2_journal_destroy_revoke_caches(); 26024185a2acSYongqiang Yang jbd2_journal_destroy_journal_head_cache(); 2603f7f4bccbSMingming Cao jbd2_journal_destroy_handle_cache(); 26040c2022ecSYongqiang Yang jbd2_journal_destroy_transaction_cache(); 2605d2eecb03STheodore Ts'o jbd2_journal_destroy_slabs(); 2606470decc6SDave Kleikamp } 2607470decc6SDave Kleikamp 2608470decc6SDave Kleikamp static int __init journal_init(void) 2609470decc6SDave Kleikamp { 2610470decc6SDave Kleikamp int ret; 2611470decc6SDave Kleikamp 2612470decc6SDave Kleikamp BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024); 2613470decc6SDave Kleikamp 2614470decc6SDave Kleikamp ret = journal_init_caches(); 2615620de4e1SDuane Griffin if (ret == 0) { 26160f49d5d0SJose R. Santos jbd2_create_debugfs_entry(); 26178e85fb3fSJohann Lombardi jbd2_create_jbd_stats_proc_entry(); 2618620de4e1SDuane Griffin } else { 2619620de4e1SDuane Griffin jbd2_journal_destroy_caches(); 2620620de4e1SDuane Griffin } 2621470decc6SDave Kleikamp return ret; 2622470decc6SDave Kleikamp } 2623470decc6SDave Kleikamp 2624470decc6SDave Kleikamp static void __exit journal_exit(void) 2625470decc6SDave Kleikamp { 2626e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 2627470decc6SDave Kleikamp int n = atomic_read(&nr_journal_heads); 2628470decc6SDave Kleikamp if (n) 2629f2a44523SEryu Guan printk(KERN_EMERG "JBD2: leaked %d journal_heads!\n", n); 2630470decc6SDave Kleikamp #endif 26310f49d5d0SJose R. Santos jbd2_remove_debugfs_entry(); 26328e85fb3fSJohann Lombardi jbd2_remove_jbd_stats_proc_entry(); 2633f7f4bccbSMingming Cao jbd2_journal_destroy_caches(); 2634470decc6SDave Kleikamp } 2635470decc6SDave Kleikamp 2636470decc6SDave Kleikamp MODULE_LICENSE("GPL"); 2637470decc6SDave Kleikamp module_init(journal_init); 2638470decc6SDave Kleikamp module_exit(journal_exit); 2639470decc6SDave Kleikamp 2640