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> 5339e3ac25SBrian King #include <asm/system.h> 54470decc6SDave Kleikamp 55f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_extend); 56f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_stop); 57f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_lock_updates); 58f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_unlock_updates); 59f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_get_write_access); 60f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_get_create_access); 61f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_get_undo_access); 62e06c8227SJoel Becker EXPORT_SYMBOL(jbd2_journal_set_triggers); 63f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_dirty_metadata); 64f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_release_buffer); 65f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_forget); 66470decc6SDave Kleikamp #if 0 67470decc6SDave Kleikamp EXPORT_SYMBOL(journal_sync_buffer); 68470decc6SDave Kleikamp #endif 69f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_flush); 70f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_revoke); 71470decc6SDave Kleikamp 72f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_init_dev); 73f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_init_inode); 74f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_update_format); 75f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_check_used_features); 76f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_check_available_features); 77f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_set_features); 78f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_load); 79f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_destroy); 80f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_abort); 81f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_errno); 82f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_ack_err); 83f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_clear_err); 84f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_log_wait_commit); 853b799d15STheodore Ts'o EXPORT_SYMBOL(jbd2_log_start_commit); 86f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_start_commit); 87f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_force_commit_nested); 88f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_wipe); 89f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_blocks_per_page); 90f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_invalidatepage); 91f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers); 92f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_force_commit); 93c851ed54SJan Kara EXPORT_SYMBOL(jbd2_journal_file_inode); 94c851ed54SJan Kara EXPORT_SYMBOL(jbd2_journal_init_jbd_inode); 95c851ed54SJan Kara EXPORT_SYMBOL(jbd2_journal_release_jbd_inode); 96c851ed54SJan Kara EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate); 978aefcd55STheodore Ts'o EXPORT_SYMBOL(jbd2_inode_cache); 98470decc6SDave Kleikamp 99470decc6SDave Kleikamp static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *); 100470decc6SDave Kleikamp static void __journal_abort_soft (journal_t *journal, int errno); 101d2eecb03STheodore Ts'o static int jbd2_journal_create_slab(size_t slab_size); 102470decc6SDave Kleikamp 103470decc6SDave Kleikamp /* 104470decc6SDave Kleikamp * Helper function used to manage commit timeouts 105470decc6SDave Kleikamp */ 106470decc6SDave Kleikamp 107470decc6SDave Kleikamp static void commit_timeout(unsigned long __data) 108470decc6SDave Kleikamp { 109470decc6SDave Kleikamp struct task_struct * p = (struct task_struct *) __data; 110470decc6SDave Kleikamp 111470decc6SDave Kleikamp wake_up_process(p); 112470decc6SDave Kleikamp } 113470decc6SDave Kleikamp 114470decc6SDave Kleikamp /* 115f7f4bccbSMingming Cao * kjournald2: The main thread function used to manage a logging device 116470decc6SDave Kleikamp * journal. 117470decc6SDave Kleikamp * 118470decc6SDave Kleikamp * This kernel thread is responsible for two things: 119470decc6SDave Kleikamp * 120470decc6SDave Kleikamp * 1) COMMIT: Every so often we need to commit the current state of the 121470decc6SDave Kleikamp * filesystem to disk. The journal thread is responsible for writing 122470decc6SDave Kleikamp * all of the metadata buffers to disk. 123470decc6SDave Kleikamp * 124470decc6SDave Kleikamp * 2) CHECKPOINT: We cannot reuse a used section of the log file until all 125470decc6SDave Kleikamp * of the data in that part of the log has been rewritten elsewhere on 126470decc6SDave Kleikamp * the disk. Flushing these old buffers to reclaim space in the log is 127470decc6SDave Kleikamp * known as checkpointing, and this thread is responsible for that job. 128470decc6SDave Kleikamp */ 129470decc6SDave Kleikamp 130f7f4bccbSMingming Cao static int kjournald2(void *arg) 131470decc6SDave Kleikamp { 132470decc6SDave Kleikamp journal_t *journal = arg; 133470decc6SDave Kleikamp transaction_t *transaction; 134470decc6SDave Kleikamp 135470decc6SDave Kleikamp /* 136470decc6SDave Kleikamp * Set up an interval timer which can be used to trigger a commit wakeup 137470decc6SDave Kleikamp * after the commit interval expires 138470decc6SDave Kleikamp */ 139470decc6SDave Kleikamp setup_timer(&journal->j_commit_timer, commit_timeout, 140470decc6SDave Kleikamp (unsigned long)current); 141470decc6SDave Kleikamp 142470decc6SDave Kleikamp /* Record that the journal thread is running */ 143470decc6SDave Kleikamp journal->j_task = current; 144470decc6SDave Kleikamp wake_up(&journal->j_wait_done_commit); 145470decc6SDave Kleikamp 146470decc6SDave Kleikamp /* 147470decc6SDave Kleikamp * And now, wait forever for commit wakeup events. 148470decc6SDave Kleikamp */ 149a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 150470decc6SDave Kleikamp 151470decc6SDave Kleikamp loop: 152f7f4bccbSMingming Cao if (journal->j_flags & JBD2_UNMOUNT) 153470decc6SDave Kleikamp goto end_loop; 154470decc6SDave Kleikamp 155470decc6SDave Kleikamp jbd_debug(1, "commit_sequence=%d, commit_request=%d\n", 156470decc6SDave Kleikamp journal->j_commit_sequence, journal->j_commit_request); 157470decc6SDave Kleikamp 158470decc6SDave Kleikamp if (journal->j_commit_sequence != journal->j_commit_request) { 159470decc6SDave Kleikamp jbd_debug(1, "OK, requests differ\n"); 160a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 161470decc6SDave Kleikamp del_timer_sync(&journal->j_commit_timer); 162f7f4bccbSMingming Cao jbd2_journal_commit_transaction(journal); 163a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 164470decc6SDave Kleikamp goto loop; 165470decc6SDave Kleikamp } 166470decc6SDave Kleikamp 167470decc6SDave Kleikamp wake_up(&journal->j_wait_done_commit); 168470decc6SDave Kleikamp if (freezing(current)) { 169470decc6SDave Kleikamp /* 170470decc6SDave Kleikamp * The simpler the better. Flushing journal isn't a 171470decc6SDave Kleikamp * good idea, because that depends on threads that may 172470decc6SDave Kleikamp * be already stopped. 173470decc6SDave Kleikamp */ 174f7f4bccbSMingming Cao jbd_debug(1, "Now suspending kjournald2\n"); 175a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 176470decc6SDave Kleikamp refrigerator(); 177a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 178470decc6SDave Kleikamp } else { 179470decc6SDave Kleikamp /* 180470decc6SDave Kleikamp * We assume on resume that commits are already there, 181470decc6SDave Kleikamp * so we don't sleep 182470decc6SDave Kleikamp */ 183470decc6SDave Kleikamp DEFINE_WAIT(wait); 184470decc6SDave Kleikamp int should_sleep = 1; 185470decc6SDave Kleikamp 186470decc6SDave Kleikamp prepare_to_wait(&journal->j_wait_commit, &wait, 187470decc6SDave Kleikamp TASK_INTERRUPTIBLE); 188470decc6SDave Kleikamp if (journal->j_commit_sequence != journal->j_commit_request) 189470decc6SDave Kleikamp should_sleep = 0; 190470decc6SDave Kleikamp transaction = journal->j_running_transaction; 191470decc6SDave Kleikamp if (transaction && time_after_eq(jiffies, 192470decc6SDave Kleikamp transaction->t_expires)) 193470decc6SDave Kleikamp should_sleep = 0; 194f7f4bccbSMingming Cao if (journal->j_flags & JBD2_UNMOUNT) 195470decc6SDave Kleikamp should_sleep = 0; 196470decc6SDave Kleikamp if (should_sleep) { 197a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 198470decc6SDave Kleikamp schedule(); 199a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 200470decc6SDave Kleikamp } 201470decc6SDave Kleikamp finish_wait(&journal->j_wait_commit, &wait); 202470decc6SDave Kleikamp } 203470decc6SDave Kleikamp 204f7f4bccbSMingming Cao jbd_debug(1, "kjournald2 wakes\n"); 205470decc6SDave Kleikamp 206470decc6SDave Kleikamp /* 207470decc6SDave Kleikamp * Were we woken up by a commit wakeup event? 208470decc6SDave Kleikamp */ 209470decc6SDave Kleikamp transaction = journal->j_running_transaction; 210470decc6SDave Kleikamp if (transaction && time_after_eq(jiffies, transaction->t_expires)) { 211470decc6SDave Kleikamp journal->j_commit_request = transaction->t_tid; 212470decc6SDave Kleikamp jbd_debug(1, "woke because of timeout\n"); 213470decc6SDave Kleikamp } 214470decc6SDave Kleikamp goto loop; 215470decc6SDave Kleikamp 216470decc6SDave Kleikamp end_loop: 217a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 218470decc6SDave Kleikamp del_timer_sync(&journal->j_commit_timer); 219470decc6SDave Kleikamp journal->j_task = NULL; 220470decc6SDave Kleikamp wake_up(&journal->j_wait_done_commit); 221470decc6SDave Kleikamp jbd_debug(1, "Journal thread exiting.\n"); 222470decc6SDave Kleikamp return 0; 223470decc6SDave Kleikamp } 224470decc6SDave Kleikamp 22597f06784SPavel Emelianov static int jbd2_journal_start_thread(journal_t *journal) 226470decc6SDave Kleikamp { 22797f06784SPavel Emelianov struct task_struct *t; 22897f06784SPavel Emelianov 22990576c0bSTheodore Ts'o t = kthread_run(kjournald2, journal, "jbd2/%s", 23090576c0bSTheodore Ts'o journal->j_devname); 23197f06784SPavel Emelianov if (IS_ERR(t)) 23297f06784SPavel Emelianov return PTR_ERR(t); 23397f06784SPavel Emelianov 2341076d17aSAl Viro wait_event(journal->j_wait_done_commit, journal->j_task != NULL); 23597f06784SPavel Emelianov return 0; 236470decc6SDave Kleikamp } 237470decc6SDave Kleikamp 238470decc6SDave Kleikamp static void journal_kill_thread(journal_t *journal) 239470decc6SDave Kleikamp { 240a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 241f7f4bccbSMingming Cao journal->j_flags |= JBD2_UNMOUNT; 242470decc6SDave Kleikamp 243470decc6SDave Kleikamp while (journal->j_task) { 244470decc6SDave Kleikamp wake_up(&journal->j_wait_commit); 245a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 2461076d17aSAl Viro wait_event(journal->j_wait_done_commit, journal->j_task == NULL); 247a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 248470decc6SDave Kleikamp } 249a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 250470decc6SDave Kleikamp } 251470decc6SDave Kleikamp 252470decc6SDave Kleikamp /* 253f7f4bccbSMingming Cao * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal. 254470decc6SDave Kleikamp * 255470decc6SDave Kleikamp * Writes a metadata buffer to a given disk block. The actual IO is not 256470decc6SDave Kleikamp * performed but a new buffer_head is constructed which labels the data 257470decc6SDave Kleikamp * to be written with the correct destination disk block. 258470decc6SDave Kleikamp * 259470decc6SDave Kleikamp * Any magic-number escaping which needs to be done will cause a 260470decc6SDave Kleikamp * copy-out here. If the buffer happens to start with the 261f7f4bccbSMingming Cao * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the 262470decc6SDave Kleikamp * magic number is only written to the log for descripter blocks. In 263470decc6SDave Kleikamp * this case, we copy the data and replace the first word with 0, and we 264470decc6SDave Kleikamp * return a result code which indicates that this buffer needs to be 265470decc6SDave Kleikamp * marked as an escaped buffer in the corresponding log descriptor 266470decc6SDave Kleikamp * block. The missing word can then be restored when the block is read 267470decc6SDave Kleikamp * during recovery. 268470decc6SDave Kleikamp * 269470decc6SDave Kleikamp * If the source buffer has already been modified by a new transaction 270470decc6SDave Kleikamp * since we took the last commit snapshot, we use the frozen copy of 271470decc6SDave Kleikamp * that data for IO. If we end up using the existing buffer_head's data 272470decc6SDave Kleikamp * for the write, then we *have* to lock the buffer to prevent anyone 273470decc6SDave Kleikamp * else from using and possibly modifying it while the IO is in 274470decc6SDave Kleikamp * progress. 275470decc6SDave Kleikamp * 276470decc6SDave Kleikamp * The function returns a pointer to the buffer_heads to be used for IO. 277470decc6SDave Kleikamp * 278470decc6SDave Kleikamp * We assume that the journal has already been locked in this function. 279470decc6SDave Kleikamp * 280470decc6SDave Kleikamp * Return value: 281470decc6SDave Kleikamp * <0: Error 282470decc6SDave Kleikamp * >=0: Finished OK 283470decc6SDave Kleikamp * 284470decc6SDave Kleikamp * On success: 285470decc6SDave Kleikamp * Bit 0 set == escape performed on the data 286470decc6SDave Kleikamp * Bit 1 set == buffer copy-out performed (kfree the data after IO) 287470decc6SDave Kleikamp */ 288470decc6SDave Kleikamp 289f7f4bccbSMingming Cao int jbd2_journal_write_metadata_buffer(transaction_t *transaction, 290470decc6SDave Kleikamp struct journal_head *jh_in, 291470decc6SDave Kleikamp struct journal_head **jh_out, 29218eba7aaSMingming Cao unsigned long long blocknr) 293470decc6SDave Kleikamp { 294470decc6SDave Kleikamp int need_copy_out = 0; 295470decc6SDave Kleikamp int done_copy_out = 0; 296470decc6SDave Kleikamp int do_escape = 0; 297470decc6SDave Kleikamp char *mapped_data; 298470decc6SDave Kleikamp struct buffer_head *new_bh; 299470decc6SDave Kleikamp struct journal_head *new_jh; 300470decc6SDave Kleikamp struct page *new_page; 301470decc6SDave Kleikamp unsigned int new_offset; 302470decc6SDave Kleikamp struct buffer_head *bh_in = jh2bh(jh_in); 30396577c43Sdingdinghua journal_t *journal = transaction->t_journal; 304470decc6SDave Kleikamp 305470decc6SDave Kleikamp /* 306470decc6SDave Kleikamp * The buffer really shouldn't be locked: only the current committing 307470decc6SDave Kleikamp * transaction is allowed to write it, so nobody else is allowed 308470decc6SDave Kleikamp * to do any IO. 309470decc6SDave Kleikamp * 310470decc6SDave Kleikamp * akpm: except if we're journalling data, and write() output is 311470decc6SDave Kleikamp * also part of a shared mapping, and another thread has 312470decc6SDave Kleikamp * decided to launch a writepage() against this buffer. 313470decc6SDave Kleikamp */ 314470decc6SDave Kleikamp J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in)); 315470decc6SDave Kleikamp 31647def826STheodore Ts'o retry_alloc: 31747def826STheodore Ts'o new_bh = alloc_buffer_head(GFP_NOFS); 31847def826STheodore Ts'o if (!new_bh) { 31947def826STheodore Ts'o /* 32047def826STheodore Ts'o * Failure is not an option, but __GFP_NOFAIL is going 32147def826STheodore Ts'o * away; so we retry ourselves here. 32247def826STheodore Ts'o */ 32347def826STheodore Ts'o congestion_wait(BLK_RW_ASYNC, HZ/50); 32447def826STheodore Ts'o goto retry_alloc; 32547def826STheodore Ts'o } 32647def826STheodore Ts'o 32796577c43Sdingdinghua /* keep subsequent assertions sane */ 32896577c43Sdingdinghua new_bh->b_state = 0; 32996577c43Sdingdinghua init_buffer(new_bh, NULL, NULL); 33096577c43Sdingdinghua atomic_set(&new_bh->b_count, 1); 33196577c43Sdingdinghua new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */ 332470decc6SDave Kleikamp 333470decc6SDave Kleikamp /* 334470decc6SDave Kleikamp * If a new transaction has already done a buffer copy-out, then 335470decc6SDave Kleikamp * we use that version of the data for the commit. 336470decc6SDave Kleikamp */ 337470decc6SDave Kleikamp jbd_lock_bh_state(bh_in); 338470decc6SDave Kleikamp repeat: 339470decc6SDave Kleikamp if (jh_in->b_frozen_data) { 340470decc6SDave Kleikamp done_copy_out = 1; 341470decc6SDave Kleikamp new_page = virt_to_page(jh_in->b_frozen_data); 342470decc6SDave Kleikamp new_offset = offset_in_page(jh_in->b_frozen_data); 343470decc6SDave Kleikamp } else { 344470decc6SDave Kleikamp new_page = jh2bh(jh_in)->b_page; 345470decc6SDave Kleikamp new_offset = offset_in_page(jh2bh(jh_in)->b_data); 346470decc6SDave Kleikamp } 347470decc6SDave Kleikamp 348470decc6SDave Kleikamp mapped_data = kmap_atomic(new_page, KM_USER0); 349470decc6SDave Kleikamp /* 35013ceef09SJan Kara * Fire data frozen trigger if data already wasn't frozen. Do this 35113ceef09SJan Kara * before checking for escaping, as the trigger may modify the magic 35213ceef09SJan Kara * offset. If a copy-out happens afterwards, it will have the correct 35313ceef09SJan Kara * data in the buffer. 354e06c8227SJoel Becker */ 35513ceef09SJan Kara if (!done_copy_out) 35613ceef09SJan Kara jbd2_buffer_frozen_trigger(jh_in, mapped_data + new_offset, 35713ceef09SJan Kara jh_in->b_triggers); 358e06c8227SJoel Becker 359e06c8227SJoel Becker /* 360470decc6SDave Kleikamp * Check for escaping 361470decc6SDave Kleikamp */ 362470decc6SDave Kleikamp if (*((__be32 *)(mapped_data + new_offset)) == 363f7f4bccbSMingming Cao cpu_to_be32(JBD2_MAGIC_NUMBER)) { 364470decc6SDave Kleikamp need_copy_out = 1; 365470decc6SDave Kleikamp do_escape = 1; 366470decc6SDave Kleikamp } 367470decc6SDave Kleikamp kunmap_atomic(mapped_data, KM_USER0); 368470decc6SDave Kleikamp 369470decc6SDave Kleikamp /* 370470decc6SDave Kleikamp * Do we need to do a data copy? 371470decc6SDave Kleikamp */ 372470decc6SDave Kleikamp if (need_copy_out && !done_copy_out) { 373470decc6SDave Kleikamp char *tmp; 374470decc6SDave Kleikamp 375470decc6SDave Kleikamp jbd_unlock_bh_state(bh_in); 376af1e76d6SMingming Cao tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS); 377e6ec116bSTheodore Ts'o if (!tmp) { 378e6ec116bSTheodore Ts'o jbd2_journal_put_journal_head(new_jh); 379e6ec116bSTheodore Ts'o return -ENOMEM; 380e6ec116bSTheodore Ts'o } 381470decc6SDave Kleikamp jbd_lock_bh_state(bh_in); 382470decc6SDave Kleikamp if (jh_in->b_frozen_data) { 383af1e76d6SMingming Cao jbd2_free(tmp, bh_in->b_size); 384470decc6SDave Kleikamp goto repeat; 385470decc6SDave Kleikamp } 386470decc6SDave Kleikamp 387470decc6SDave Kleikamp jh_in->b_frozen_data = tmp; 388470decc6SDave Kleikamp mapped_data = kmap_atomic(new_page, KM_USER0); 389470decc6SDave Kleikamp memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size); 390470decc6SDave Kleikamp kunmap_atomic(mapped_data, KM_USER0); 391470decc6SDave Kleikamp 392470decc6SDave Kleikamp new_page = virt_to_page(tmp); 393470decc6SDave Kleikamp new_offset = offset_in_page(tmp); 394470decc6SDave Kleikamp done_copy_out = 1; 395e06c8227SJoel Becker 396e06c8227SJoel Becker /* 397e06c8227SJoel Becker * This isn't strictly necessary, as we're using frozen 398e06c8227SJoel Becker * data for the escaping, but it keeps consistency with 399e06c8227SJoel Becker * b_frozen_data usage. 400e06c8227SJoel Becker */ 401e06c8227SJoel Becker jh_in->b_frozen_triggers = jh_in->b_triggers; 402470decc6SDave Kleikamp } 403470decc6SDave Kleikamp 404470decc6SDave Kleikamp /* 405470decc6SDave Kleikamp * Did we need to do an escaping? Now we've done all the 406470decc6SDave Kleikamp * copying, we can finally do so. 407470decc6SDave Kleikamp */ 408470decc6SDave Kleikamp if (do_escape) { 409470decc6SDave Kleikamp mapped_data = kmap_atomic(new_page, KM_USER0); 410470decc6SDave Kleikamp *((unsigned int *)(mapped_data + new_offset)) = 0; 411470decc6SDave Kleikamp kunmap_atomic(mapped_data, KM_USER0); 412470decc6SDave Kleikamp } 413470decc6SDave Kleikamp 414470decc6SDave Kleikamp set_bh_page(new_bh, new_page, new_offset); 415470decc6SDave Kleikamp new_jh->b_transaction = NULL; 416470decc6SDave Kleikamp new_bh->b_size = jh2bh(jh_in)->b_size; 417470decc6SDave Kleikamp new_bh->b_bdev = transaction->t_journal->j_dev; 418470decc6SDave Kleikamp new_bh->b_blocknr = blocknr; 419470decc6SDave Kleikamp set_buffer_mapped(new_bh); 420470decc6SDave Kleikamp set_buffer_dirty(new_bh); 421470decc6SDave Kleikamp 422470decc6SDave Kleikamp *jh_out = new_jh; 423470decc6SDave Kleikamp 424470decc6SDave Kleikamp /* 425470decc6SDave Kleikamp * The to-be-written buffer needs to get moved to the io queue, 426470decc6SDave Kleikamp * and the original buffer whose contents we are shadowing or 427470decc6SDave Kleikamp * copying is moved to the transaction's shadow queue. 428470decc6SDave Kleikamp */ 429470decc6SDave Kleikamp JBUFFER_TRACE(jh_in, "file as BJ_Shadow"); 43096577c43Sdingdinghua spin_lock(&journal->j_list_lock); 43196577c43Sdingdinghua __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow); 43296577c43Sdingdinghua spin_unlock(&journal->j_list_lock); 43396577c43Sdingdinghua jbd_unlock_bh_state(bh_in); 43496577c43Sdingdinghua 435470decc6SDave Kleikamp JBUFFER_TRACE(new_jh, "file as BJ_IO"); 436f7f4bccbSMingming Cao jbd2_journal_file_buffer(new_jh, transaction, BJ_IO); 437470decc6SDave Kleikamp 438470decc6SDave Kleikamp return do_escape | (done_copy_out << 1); 439470decc6SDave Kleikamp } 440470decc6SDave Kleikamp 441470decc6SDave Kleikamp /* 442470decc6SDave Kleikamp * Allocation code for the journal file. Manage the space left in the 443470decc6SDave Kleikamp * journal, so that we can begin checkpointing when appropriate. 444470decc6SDave Kleikamp */ 445470decc6SDave Kleikamp 446470decc6SDave Kleikamp /* 447f7f4bccbSMingming Cao * __jbd2_log_space_left: Return the number of free blocks left in the journal. 448470decc6SDave Kleikamp * 449470decc6SDave Kleikamp * Called with the journal already locked. 450470decc6SDave Kleikamp * 451470decc6SDave Kleikamp * Called under j_state_lock 452470decc6SDave Kleikamp */ 453470decc6SDave Kleikamp 454f7f4bccbSMingming Cao int __jbd2_log_space_left(journal_t *journal) 455470decc6SDave Kleikamp { 456470decc6SDave Kleikamp int left = journal->j_free; 457470decc6SDave Kleikamp 458a931da6aSTheodore Ts'o /* assert_spin_locked(&journal->j_state_lock); */ 459470decc6SDave Kleikamp 460470decc6SDave Kleikamp /* 461470decc6SDave Kleikamp * Be pessimistic here about the number of those free blocks which 462470decc6SDave Kleikamp * might be required for log descriptor control blocks. 463470decc6SDave Kleikamp */ 464470decc6SDave Kleikamp 465470decc6SDave Kleikamp #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */ 466470decc6SDave Kleikamp 467470decc6SDave Kleikamp left -= MIN_LOG_RESERVED_BLOCKS; 468470decc6SDave Kleikamp 469470decc6SDave Kleikamp if (left <= 0) 470470decc6SDave Kleikamp return 0; 471470decc6SDave Kleikamp left -= (left >> 3); 472470decc6SDave Kleikamp return left; 473470decc6SDave Kleikamp } 474470decc6SDave Kleikamp 475470decc6SDave Kleikamp /* 476e4471831STheodore Ts'o * Called with j_state_lock locked for writing. 477e4471831STheodore Ts'o * Returns true if a transaction commit was started. 478470decc6SDave Kleikamp */ 479f7f4bccbSMingming Cao int __jbd2_log_start_commit(journal_t *journal, tid_t target) 480470decc6SDave Kleikamp { 481470decc6SDave Kleikamp /* 482deeeaf13STheodore Ts'o * The only transaction we can possibly wait upon is the 483deeeaf13STheodore Ts'o * currently running transaction (if it exists). Otherwise, 484deeeaf13STheodore Ts'o * the target tid must be an old one. 485470decc6SDave Kleikamp */ 486deeeaf13STheodore Ts'o if (journal->j_running_transaction && 487deeeaf13STheodore Ts'o journal->j_running_transaction->t_tid == target) { 488470decc6SDave Kleikamp /* 489bcf3d0bcSAndrea Gelmini * We want a new commit: OK, mark the request and wakeup the 490470decc6SDave Kleikamp * commit thread. We do _not_ do the commit ourselves. 491470decc6SDave Kleikamp */ 492470decc6SDave Kleikamp 493470decc6SDave Kleikamp journal->j_commit_request = target; 494470decc6SDave Kleikamp jbd_debug(1, "JBD: requesting commit %d/%d\n", 495470decc6SDave Kleikamp journal->j_commit_request, 496470decc6SDave Kleikamp journal->j_commit_sequence); 497470decc6SDave Kleikamp wake_up(&journal->j_wait_commit); 498470decc6SDave Kleikamp return 1; 499deeeaf13STheodore Ts'o } else if (!tid_geq(journal->j_commit_request, target)) 500deeeaf13STheodore Ts'o /* This should never happen, but if it does, preserve 501deeeaf13STheodore Ts'o the evidence before kjournald goes into a loop and 502deeeaf13STheodore Ts'o increments j_commit_sequence beyond all recognition. */ 503*1be2add6STheodore Ts'o WARN_ONCE(1, "jbd: bad log_start_commit: %u %u %u %u\n", 504*1be2add6STheodore Ts'o journal->j_commit_request, 505*1be2add6STheodore Ts'o journal->j_commit_sequence, 506deeeaf13STheodore Ts'o target, journal->j_running_transaction ? 507deeeaf13STheodore Ts'o journal->j_running_transaction->t_tid : 0); 508470decc6SDave Kleikamp return 0; 509470decc6SDave Kleikamp } 510470decc6SDave Kleikamp 511f7f4bccbSMingming Cao int jbd2_log_start_commit(journal_t *journal, tid_t tid) 512470decc6SDave Kleikamp { 513470decc6SDave Kleikamp int ret; 514470decc6SDave Kleikamp 515a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 516f7f4bccbSMingming Cao ret = __jbd2_log_start_commit(journal, tid); 517a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 518470decc6SDave Kleikamp return ret; 519470decc6SDave Kleikamp } 520470decc6SDave Kleikamp 521470decc6SDave Kleikamp /* 522470decc6SDave Kleikamp * Force and wait upon a commit if the calling process is not within 523470decc6SDave Kleikamp * transaction. This is used for forcing out undo-protected data which contains 524470decc6SDave Kleikamp * bitmaps, when the fs is running out of space. 525470decc6SDave Kleikamp * 526470decc6SDave Kleikamp * We can only force the running transaction if we don't have an active handle; 527470decc6SDave Kleikamp * otherwise, we will deadlock. 528470decc6SDave Kleikamp * 529470decc6SDave Kleikamp * Returns true if a transaction was started. 530470decc6SDave Kleikamp */ 531f7f4bccbSMingming Cao int jbd2_journal_force_commit_nested(journal_t *journal) 532470decc6SDave Kleikamp { 533470decc6SDave Kleikamp transaction_t *transaction = NULL; 534470decc6SDave Kleikamp tid_t tid; 535e4471831STheodore Ts'o int need_to_start = 0; 536470decc6SDave Kleikamp 537a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 538470decc6SDave Kleikamp if (journal->j_running_transaction && !current->journal_info) { 539470decc6SDave Kleikamp transaction = journal->j_running_transaction; 540e4471831STheodore Ts'o if (!tid_geq(journal->j_commit_request, transaction->t_tid)) 541e4471831STheodore Ts'o need_to_start = 1; 542470decc6SDave Kleikamp } else if (journal->j_committing_transaction) 543470decc6SDave Kleikamp transaction = journal->j_committing_transaction; 544470decc6SDave Kleikamp 545470decc6SDave Kleikamp if (!transaction) { 546a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 547470decc6SDave Kleikamp return 0; /* Nothing to retry */ 548470decc6SDave Kleikamp } 549470decc6SDave Kleikamp 550470decc6SDave Kleikamp tid = transaction->t_tid; 551a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 552e4471831STheodore Ts'o if (need_to_start) 553e4471831STheodore Ts'o jbd2_log_start_commit(journal, tid); 554f7f4bccbSMingming Cao jbd2_log_wait_commit(journal, tid); 555470decc6SDave Kleikamp return 1; 556470decc6SDave Kleikamp } 557470decc6SDave Kleikamp 558470decc6SDave Kleikamp /* 559470decc6SDave Kleikamp * Start a commit of the current running transaction (if any). Returns true 560c88ccea3SJan Kara * if a transaction is going to be committed (or is currently already 561c88ccea3SJan Kara * committing), and fills its tid in at *ptid 562470decc6SDave Kleikamp */ 563f7f4bccbSMingming Cao int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid) 564470decc6SDave Kleikamp { 565470decc6SDave Kleikamp int ret = 0; 566470decc6SDave Kleikamp 567a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 568470decc6SDave Kleikamp if (journal->j_running_transaction) { 569470decc6SDave Kleikamp tid_t tid = journal->j_running_transaction->t_tid; 570470decc6SDave Kleikamp 571c88ccea3SJan Kara __jbd2_log_start_commit(journal, tid); 572c88ccea3SJan Kara /* There's a running transaction and we've just made sure 573c88ccea3SJan Kara * it's commit has been scheduled. */ 574c88ccea3SJan Kara if (ptid) 575470decc6SDave Kleikamp *ptid = tid; 576c88ccea3SJan Kara ret = 1; 577c88ccea3SJan Kara } else if (journal->j_committing_transaction) { 578470decc6SDave Kleikamp /* 579470decc6SDave Kleikamp * If ext3_write_super() recently started a commit, then we 580470decc6SDave Kleikamp * have to wait for completion of that transaction 581470decc6SDave Kleikamp */ 582c88ccea3SJan Kara if (ptid) 583470decc6SDave Kleikamp *ptid = journal->j_committing_transaction->t_tid; 584470decc6SDave Kleikamp ret = 1; 585470decc6SDave Kleikamp } 586a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 587470decc6SDave Kleikamp return ret; 588470decc6SDave Kleikamp } 589470decc6SDave Kleikamp 590470decc6SDave Kleikamp /* 591470decc6SDave Kleikamp * Wait for a specified commit to complete. 592470decc6SDave Kleikamp * The caller may not hold the journal lock. 593470decc6SDave Kleikamp */ 594f7f4bccbSMingming Cao int jbd2_log_wait_commit(journal_t *journal, tid_t tid) 595470decc6SDave Kleikamp { 596470decc6SDave Kleikamp int err = 0; 597470decc6SDave Kleikamp 598a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 599e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 600470decc6SDave Kleikamp if (!tid_geq(journal->j_commit_request, tid)) { 601470decc6SDave Kleikamp printk(KERN_EMERG 602470decc6SDave Kleikamp "%s: error: j_commit_request=%d, tid=%d\n", 603329d291fSHarvey Harrison __func__, journal->j_commit_request, tid); 604470decc6SDave Kleikamp } 605470decc6SDave Kleikamp #endif 606470decc6SDave Kleikamp while (tid_gt(tid, journal->j_commit_sequence)) { 607470decc6SDave Kleikamp jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n", 608470decc6SDave Kleikamp tid, journal->j_commit_sequence); 609470decc6SDave Kleikamp wake_up(&journal->j_wait_commit); 610a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 611470decc6SDave Kleikamp wait_event(journal->j_wait_done_commit, 612470decc6SDave Kleikamp !tid_gt(tid, journal->j_commit_sequence)); 613a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 614470decc6SDave Kleikamp } 615a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 616470decc6SDave Kleikamp 617470decc6SDave Kleikamp if (unlikely(is_journal_aborted(journal))) { 618470decc6SDave Kleikamp printk(KERN_EMERG "journal commit I/O error\n"); 619470decc6SDave Kleikamp err = -EIO; 620470decc6SDave Kleikamp } 621470decc6SDave Kleikamp return err; 622470decc6SDave Kleikamp } 623470decc6SDave Kleikamp 624470decc6SDave Kleikamp /* 625470decc6SDave Kleikamp * Log buffer allocation routines: 626470decc6SDave Kleikamp */ 627470decc6SDave Kleikamp 62818eba7aaSMingming Cao int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp) 629470decc6SDave Kleikamp { 630470decc6SDave Kleikamp unsigned long blocknr; 631470decc6SDave Kleikamp 632a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 633470decc6SDave Kleikamp J_ASSERT(journal->j_free > 1); 634470decc6SDave Kleikamp 635470decc6SDave Kleikamp blocknr = journal->j_head; 636470decc6SDave Kleikamp journal->j_head++; 637470decc6SDave Kleikamp journal->j_free--; 638470decc6SDave Kleikamp if (journal->j_head == journal->j_last) 639470decc6SDave Kleikamp journal->j_head = journal->j_first; 640a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 641f7f4bccbSMingming Cao return jbd2_journal_bmap(journal, blocknr, retp); 642470decc6SDave Kleikamp } 643470decc6SDave Kleikamp 644470decc6SDave Kleikamp /* 645470decc6SDave Kleikamp * Conversion of logical to physical block numbers for the journal 646470decc6SDave Kleikamp * 647470decc6SDave Kleikamp * On external journals the journal blocks are identity-mapped, so 648470decc6SDave Kleikamp * this is a no-op. If needed, we can use j_blk_offset - everything is 649470decc6SDave Kleikamp * ready. 650470decc6SDave Kleikamp */ 651f7f4bccbSMingming Cao int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr, 65218eba7aaSMingming Cao unsigned long long *retp) 653470decc6SDave Kleikamp { 654470decc6SDave Kleikamp int err = 0; 65518eba7aaSMingming Cao unsigned long long ret; 656470decc6SDave Kleikamp 657470decc6SDave Kleikamp if (journal->j_inode) { 658470decc6SDave Kleikamp ret = bmap(journal->j_inode, blocknr); 659470decc6SDave Kleikamp if (ret) 660470decc6SDave Kleikamp *retp = ret; 661470decc6SDave Kleikamp else { 662470decc6SDave Kleikamp printk(KERN_ALERT "%s: journal block not found " 663470decc6SDave Kleikamp "at offset %lu on %s\n", 66405496769STheodore Ts'o __func__, blocknr, journal->j_devname); 665470decc6SDave Kleikamp err = -EIO; 666470decc6SDave Kleikamp __journal_abort_soft(journal, err); 667470decc6SDave Kleikamp } 668470decc6SDave Kleikamp } else { 669470decc6SDave Kleikamp *retp = blocknr; /* +journal->j_blk_offset */ 670470decc6SDave Kleikamp } 671470decc6SDave Kleikamp return err; 672470decc6SDave Kleikamp } 673470decc6SDave Kleikamp 674470decc6SDave Kleikamp /* 675470decc6SDave Kleikamp * We play buffer_head aliasing tricks to write data/metadata blocks to 676470decc6SDave Kleikamp * the journal without copying their contents, but for journal 677470decc6SDave Kleikamp * descriptor blocks we do need to generate bona fide buffers. 678470decc6SDave Kleikamp * 679f7f4bccbSMingming Cao * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying 680470decc6SDave Kleikamp * the buffer's contents they really should run flush_dcache_page(bh->b_page). 681470decc6SDave Kleikamp * But we don't bother doing that, so there will be coherency problems with 682470decc6SDave Kleikamp * mmaps of blockdevs which hold live JBD-controlled filesystems. 683470decc6SDave Kleikamp */ 684f7f4bccbSMingming Cao struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal) 685470decc6SDave Kleikamp { 686470decc6SDave Kleikamp struct buffer_head *bh; 68718eba7aaSMingming Cao unsigned long long blocknr; 688470decc6SDave Kleikamp int err; 689470decc6SDave Kleikamp 690f7f4bccbSMingming Cao err = jbd2_journal_next_log_block(journal, &blocknr); 691470decc6SDave Kleikamp 692470decc6SDave Kleikamp if (err) 693470decc6SDave Kleikamp return NULL; 694470decc6SDave Kleikamp 695470decc6SDave Kleikamp bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize); 6964b905671SJan Kara if (!bh) 6974b905671SJan Kara return NULL; 698470decc6SDave Kleikamp lock_buffer(bh); 699470decc6SDave Kleikamp memset(bh->b_data, 0, journal->j_blocksize); 700470decc6SDave Kleikamp set_buffer_uptodate(bh); 701470decc6SDave Kleikamp unlock_buffer(bh); 702470decc6SDave Kleikamp BUFFER_TRACE(bh, "return this buffer"); 703f7f4bccbSMingming Cao return jbd2_journal_add_journal_head(bh); 704470decc6SDave Kleikamp } 705470decc6SDave Kleikamp 7068e85fb3fSJohann Lombardi struct jbd2_stats_proc_session { 7078e85fb3fSJohann Lombardi journal_t *journal; 7088e85fb3fSJohann Lombardi struct transaction_stats_s *stats; 7098e85fb3fSJohann Lombardi int start; 7108e85fb3fSJohann Lombardi int max; 7118e85fb3fSJohann Lombardi }; 7128e85fb3fSJohann Lombardi 7138e85fb3fSJohann Lombardi static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos) 7148e85fb3fSJohann Lombardi { 7158e85fb3fSJohann Lombardi return *pos ? NULL : SEQ_START_TOKEN; 7168e85fb3fSJohann Lombardi } 7178e85fb3fSJohann Lombardi 7188e85fb3fSJohann Lombardi static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos) 7198e85fb3fSJohann Lombardi { 7208e85fb3fSJohann Lombardi return NULL; 7218e85fb3fSJohann Lombardi } 7228e85fb3fSJohann Lombardi 7238e85fb3fSJohann Lombardi static int jbd2_seq_info_show(struct seq_file *seq, void *v) 7248e85fb3fSJohann Lombardi { 7258e85fb3fSJohann Lombardi struct jbd2_stats_proc_session *s = seq->private; 7268e85fb3fSJohann Lombardi 7278e85fb3fSJohann Lombardi if (v != SEQ_START_TOKEN) 7288e85fb3fSJohann Lombardi return 0; 7298e85fb3fSJohann Lombardi seq_printf(seq, "%lu transaction, each up to %u blocks\n", 7308e85fb3fSJohann Lombardi s->stats->ts_tid, 7318e85fb3fSJohann Lombardi s->journal->j_max_transaction_buffers); 7328e85fb3fSJohann Lombardi if (s->stats->ts_tid == 0) 7338e85fb3fSJohann Lombardi return 0; 7348e85fb3fSJohann Lombardi seq_printf(seq, "average: \n %ums waiting for transaction\n", 735bf699327STheodore Ts'o jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid)); 7368e85fb3fSJohann Lombardi seq_printf(seq, " %ums running transaction\n", 737bf699327STheodore Ts'o jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid)); 7388e85fb3fSJohann Lombardi seq_printf(seq, " %ums transaction was being locked\n", 739bf699327STheodore Ts'o jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid)); 7408e85fb3fSJohann Lombardi seq_printf(seq, " %ums flushing data (in ordered mode)\n", 741bf699327STheodore Ts'o jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid)); 7428e85fb3fSJohann Lombardi seq_printf(seq, " %ums logging transaction\n", 743bf699327STheodore Ts'o jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid)); 744c225aa57SSimon Holm Thøgersen seq_printf(seq, " %lluus average transaction commit time\n", 745c225aa57SSimon Holm Thøgersen div_u64(s->journal->j_average_commit_time, 1000)); 7468e85fb3fSJohann Lombardi seq_printf(seq, " %lu handles per transaction\n", 747bf699327STheodore Ts'o s->stats->run.rs_handle_count / s->stats->ts_tid); 7488e85fb3fSJohann Lombardi seq_printf(seq, " %lu blocks per transaction\n", 749bf699327STheodore Ts'o s->stats->run.rs_blocks / s->stats->ts_tid); 7508e85fb3fSJohann Lombardi seq_printf(seq, " %lu logged blocks per transaction\n", 751bf699327STheodore Ts'o s->stats->run.rs_blocks_logged / s->stats->ts_tid); 7528e85fb3fSJohann Lombardi return 0; 7538e85fb3fSJohann Lombardi } 7548e85fb3fSJohann Lombardi 7558e85fb3fSJohann Lombardi static void jbd2_seq_info_stop(struct seq_file *seq, void *v) 7568e85fb3fSJohann Lombardi { 7578e85fb3fSJohann Lombardi } 7588e85fb3fSJohann Lombardi 75988e9d34cSJames Morris static const struct seq_operations jbd2_seq_info_ops = { 7608e85fb3fSJohann Lombardi .start = jbd2_seq_info_start, 7618e85fb3fSJohann Lombardi .next = jbd2_seq_info_next, 7628e85fb3fSJohann Lombardi .stop = jbd2_seq_info_stop, 7638e85fb3fSJohann Lombardi .show = jbd2_seq_info_show, 7648e85fb3fSJohann Lombardi }; 7658e85fb3fSJohann Lombardi 7668e85fb3fSJohann Lombardi static int jbd2_seq_info_open(struct inode *inode, struct file *file) 7678e85fb3fSJohann Lombardi { 7688e85fb3fSJohann Lombardi journal_t *journal = PDE(inode)->data; 7698e85fb3fSJohann Lombardi struct jbd2_stats_proc_session *s; 7708e85fb3fSJohann Lombardi int rc, size; 7718e85fb3fSJohann Lombardi 7728e85fb3fSJohann Lombardi s = kmalloc(sizeof(*s), GFP_KERNEL); 7738e85fb3fSJohann Lombardi if (s == NULL) 7748e85fb3fSJohann Lombardi return -ENOMEM; 7758e85fb3fSJohann Lombardi size = sizeof(struct transaction_stats_s); 7768e85fb3fSJohann Lombardi s->stats = kmalloc(size, GFP_KERNEL); 7778e85fb3fSJohann Lombardi if (s->stats == NULL) { 7788e85fb3fSJohann Lombardi kfree(s); 7798e85fb3fSJohann Lombardi return -ENOMEM; 7808e85fb3fSJohann Lombardi } 7818e85fb3fSJohann Lombardi spin_lock(&journal->j_history_lock); 7828e85fb3fSJohann Lombardi memcpy(s->stats, &journal->j_stats, size); 7838e85fb3fSJohann Lombardi s->journal = journal; 7848e85fb3fSJohann Lombardi spin_unlock(&journal->j_history_lock); 7858e85fb3fSJohann Lombardi 7868e85fb3fSJohann Lombardi rc = seq_open(file, &jbd2_seq_info_ops); 7878e85fb3fSJohann Lombardi if (rc == 0) { 7888e85fb3fSJohann Lombardi struct seq_file *m = file->private_data; 7898e85fb3fSJohann Lombardi m->private = s; 7908e85fb3fSJohann Lombardi } else { 7918e85fb3fSJohann Lombardi kfree(s->stats); 7928e85fb3fSJohann Lombardi kfree(s); 7938e85fb3fSJohann Lombardi } 7948e85fb3fSJohann Lombardi return rc; 7958e85fb3fSJohann Lombardi 7968e85fb3fSJohann Lombardi } 7978e85fb3fSJohann Lombardi 7988e85fb3fSJohann Lombardi static int jbd2_seq_info_release(struct inode *inode, struct file *file) 7998e85fb3fSJohann Lombardi { 8008e85fb3fSJohann Lombardi struct seq_file *seq = file->private_data; 8018e85fb3fSJohann Lombardi struct jbd2_stats_proc_session *s = seq->private; 8028e85fb3fSJohann Lombardi kfree(s->stats); 8038e85fb3fSJohann Lombardi kfree(s); 8048e85fb3fSJohann Lombardi return seq_release(inode, file); 8058e85fb3fSJohann Lombardi } 8068e85fb3fSJohann Lombardi 807828c0950SAlexey Dobriyan static const struct file_operations jbd2_seq_info_fops = { 8088e85fb3fSJohann Lombardi .owner = THIS_MODULE, 8098e85fb3fSJohann Lombardi .open = jbd2_seq_info_open, 8108e85fb3fSJohann Lombardi .read = seq_read, 8118e85fb3fSJohann Lombardi .llseek = seq_lseek, 8128e85fb3fSJohann Lombardi .release = jbd2_seq_info_release, 8138e85fb3fSJohann Lombardi }; 8148e85fb3fSJohann Lombardi 8158e85fb3fSJohann Lombardi static struct proc_dir_entry *proc_jbd2_stats; 8168e85fb3fSJohann Lombardi 8178e85fb3fSJohann Lombardi static void jbd2_stats_proc_init(journal_t *journal) 8188e85fb3fSJohann Lombardi { 81905496769STheodore Ts'o journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats); 8208e85fb3fSJohann Lombardi if (journal->j_proc_entry) { 82179da3664SDenis V. Lunev proc_create_data("info", S_IRUGO, journal->j_proc_entry, 82279da3664SDenis V. Lunev &jbd2_seq_info_fops, journal); 8238e85fb3fSJohann Lombardi } 8248e85fb3fSJohann Lombardi } 8258e85fb3fSJohann Lombardi 8268e85fb3fSJohann Lombardi static void jbd2_stats_proc_exit(journal_t *journal) 8278e85fb3fSJohann Lombardi { 8288e85fb3fSJohann Lombardi remove_proc_entry("info", journal->j_proc_entry); 82905496769STheodore Ts'o remove_proc_entry(journal->j_devname, proc_jbd2_stats); 8308e85fb3fSJohann Lombardi } 8318e85fb3fSJohann Lombardi 832470decc6SDave Kleikamp /* 833470decc6SDave Kleikamp * Management for journal control blocks: functions to create and 834470decc6SDave Kleikamp * destroy journal_t structures, and to initialise and read existing 835470decc6SDave Kleikamp * journal blocks from disk. */ 836470decc6SDave Kleikamp 837470decc6SDave Kleikamp /* First: create and setup a journal_t object in memory. We initialise 838470decc6SDave Kleikamp * very few fields yet: that has to wait until we have created the 839470decc6SDave Kleikamp * journal structures from from scratch, or loaded them from disk. */ 840470decc6SDave Kleikamp 841470decc6SDave Kleikamp static journal_t * journal_init_common (void) 842470decc6SDave Kleikamp { 843470decc6SDave Kleikamp journal_t *journal; 844470decc6SDave Kleikamp int err; 845470decc6SDave Kleikamp 8463ebfdf88SAndrew Morton journal = kzalloc(sizeof(*journal), GFP_KERNEL); 847470decc6SDave Kleikamp if (!journal) 848b7271b0aSTheodore Ts'o return NULL; 849470decc6SDave Kleikamp 850470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_transaction_locked); 851470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_logspace); 852470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_done_commit); 853470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_checkpoint); 854470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_commit); 855470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_updates); 856470decc6SDave Kleikamp mutex_init(&journal->j_barrier); 857470decc6SDave Kleikamp mutex_init(&journal->j_checkpoint_mutex); 858470decc6SDave Kleikamp spin_lock_init(&journal->j_revoke_lock); 859470decc6SDave Kleikamp spin_lock_init(&journal->j_list_lock); 860a931da6aSTheodore Ts'o rwlock_init(&journal->j_state_lock); 861470decc6SDave Kleikamp 862cd02ff0bSMingming Cao journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE); 86330773840STheodore Ts'o journal->j_min_batch_time = 0; 86430773840STheodore Ts'o journal->j_max_batch_time = 15000; /* 15ms */ 865470decc6SDave Kleikamp 866470decc6SDave Kleikamp /* The journal is marked for error until we succeed with recovery! */ 867f7f4bccbSMingming Cao journal->j_flags = JBD2_ABORT; 868470decc6SDave Kleikamp 869470decc6SDave Kleikamp /* Set up a default-sized revoke table for the new mount. */ 870f7f4bccbSMingming Cao err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH); 871470decc6SDave Kleikamp if (err) { 872470decc6SDave Kleikamp kfree(journal); 873b7271b0aSTheodore Ts'o return NULL; 874470decc6SDave Kleikamp } 8758e85fb3fSJohann Lombardi 876bf699327STheodore Ts'o spin_lock_init(&journal->j_history_lock); 8778e85fb3fSJohann Lombardi 878470decc6SDave Kleikamp return journal; 879470decc6SDave Kleikamp } 880470decc6SDave Kleikamp 881f7f4bccbSMingming Cao /* jbd2_journal_init_dev and jbd2_journal_init_inode: 882470decc6SDave Kleikamp * 883470decc6SDave Kleikamp * Create a journal structure assigned some fixed set of disk blocks to 884470decc6SDave Kleikamp * the journal. We don't actually touch those disk blocks yet, but we 885470decc6SDave Kleikamp * need to set up all of the mapping information to tell the journaling 886470decc6SDave Kleikamp * system where the journal blocks are. 887470decc6SDave Kleikamp * 888470decc6SDave Kleikamp */ 889470decc6SDave Kleikamp 890470decc6SDave Kleikamp /** 8915648ba5bSRandy Dunlap * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure 892470decc6SDave Kleikamp * @bdev: Block device on which to create the journal 893470decc6SDave Kleikamp * @fs_dev: Device which hold journalled filesystem for this journal. 894470decc6SDave Kleikamp * @start: Block nr Start of journal. 895470decc6SDave Kleikamp * @len: Length of the journal in blocks. 896470decc6SDave Kleikamp * @blocksize: blocksize of journalling device 8975648ba5bSRandy Dunlap * 8985648ba5bSRandy Dunlap * Returns: a newly created journal_t * 899470decc6SDave Kleikamp * 900f7f4bccbSMingming Cao * jbd2_journal_init_dev creates a journal which maps a fixed contiguous 901470decc6SDave Kleikamp * range of blocks on an arbitrary block device. 902470decc6SDave Kleikamp * 903470decc6SDave Kleikamp */ 904f7f4bccbSMingming Cao journal_t * jbd2_journal_init_dev(struct block_device *bdev, 905470decc6SDave Kleikamp struct block_device *fs_dev, 90618eba7aaSMingming Cao unsigned long long start, int len, int blocksize) 907470decc6SDave Kleikamp { 908470decc6SDave Kleikamp journal_t *journal = journal_init_common(); 909470decc6SDave Kleikamp struct buffer_head *bh; 91005496769STheodore Ts'o char *p; 911470decc6SDave Kleikamp int n; 912470decc6SDave Kleikamp 913470decc6SDave Kleikamp if (!journal) 914470decc6SDave Kleikamp return NULL; 915470decc6SDave Kleikamp 916470decc6SDave Kleikamp /* journal descriptor can store up to n blocks -bzzz */ 917470decc6SDave Kleikamp journal->j_blocksize = blocksize; 9180587aa3dSyangsheng journal->j_dev = bdev; 9190587aa3dSyangsheng journal->j_fs_dev = fs_dev; 9200587aa3dSyangsheng journal->j_blk_offset = start; 9210587aa3dSyangsheng journal->j_maxlen = len; 9220587aa3dSyangsheng bdevname(journal->j_dev, journal->j_devname); 9230587aa3dSyangsheng p = journal->j_devname; 9240587aa3dSyangsheng while ((p = strchr(p, '/'))) 9250587aa3dSyangsheng *p = '!'; 9264b905671SJan Kara jbd2_stats_proc_init(journal); 927470decc6SDave Kleikamp n = journal->j_blocksize / sizeof(journal_block_tag_t); 928470decc6SDave Kleikamp journal->j_wbufsize = n; 929470decc6SDave Kleikamp journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL); 930470decc6SDave Kleikamp if (!journal->j_wbuf) { 93125985edcSLucas De Marchi printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n", 932329d291fSHarvey Harrison __func__); 9334b905671SJan Kara goto out_err; 934470decc6SDave Kleikamp } 935470decc6SDave Kleikamp 936470decc6SDave Kleikamp bh = __getblk(journal->j_dev, start, journal->j_blocksize); 9374b905671SJan Kara if (!bh) { 9384b905671SJan Kara printk(KERN_ERR 9394b905671SJan Kara "%s: Cannot get buffer for journal superblock\n", 9404b905671SJan Kara __func__); 9414b905671SJan Kara goto out_err; 9424b905671SJan Kara } 943470decc6SDave Kleikamp journal->j_sb_buffer = bh; 944470decc6SDave Kleikamp journal->j_superblock = (journal_superblock_t *)bh->b_data; 9454b905671SJan Kara 946470decc6SDave Kleikamp return journal; 9474b905671SJan Kara out_err: 9487b02bec0STao Ma kfree(journal->j_wbuf); 9494b905671SJan Kara jbd2_stats_proc_exit(journal); 9504b905671SJan Kara kfree(journal); 9514b905671SJan Kara return NULL; 952470decc6SDave Kleikamp } 953470decc6SDave Kleikamp 954470decc6SDave Kleikamp /** 955f7f4bccbSMingming Cao * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode. 956470decc6SDave Kleikamp * @inode: An inode to create the journal in 957470decc6SDave Kleikamp * 958f7f4bccbSMingming Cao * jbd2_journal_init_inode creates a journal which maps an on-disk inode as 959470decc6SDave Kleikamp * the journal. The inode must exist already, must support bmap() and 960470decc6SDave Kleikamp * must have all data blocks preallocated. 961470decc6SDave Kleikamp */ 962f7f4bccbSMingming Cao journal_t * jbd2_journal_init_inode (struct inode *inode) 963470decc6SDave Kleikamp { 964470decc6SDave Kleikamp struct buffer_head *bh; 965470decc6SDave Kleikamp journal_t *journal = journal_init_common(); 96605496769STheodore Ts'o char *p; 967470decc6SDave Kleikamp int err; 968470decc6SDave Kleikamp int n; 96918eba7aaSMingming Cao unsigned long long blocknr; 970470decc6SDave Kleikamp 971470decc6SDave Kleikamp if (!journal) 972470decc6SDave Kleikamp return NULL; 973470decc6SDave Kleikamp 974470decc6SDave Kleikamp journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev; 975470decc6SDave Kleikamp journal->j_inode = inode; 97605496769STheodore Ts'o bdevname(journal->j_dev, journal->j_devname); 97705496769STheodore Ts'o p = journal->j_devname; 97805496769STheodore Ts'o while ((p = strchr(p, '/'))) 97905496769STheodore Ts'o *p = '!'; 98005496769STheodore Ts'o p = journal->j_devname + strlen(journal->j_devname); 98190576c0bSTheodore Ts'o sprintf(p, "-%lu", journal->j_inode->i_ino); 982470decc6SDave Kleikamp jbd_debug(1, 983470decc6SDave Kleikamp "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n", 984470decc6SDave Kleikamp journal, inode->i_sb->s_id, inode->i_ino, 985470decc6SDave Kleikamp (long long) inode->i_size, 986470decc6SDave Kleikamp inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize); 987470decc6SDave Kleikamp 988470decc6SDave Kleikamp journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits; 989470decc6SDave Kleikamp journal->j_blocksize = inode->i_sb->s_blocksize; 9908e85fb3fSJohann Lombardi jbd2_stats_proc_init(journal); 991470decc6SDave Kleikamp 992470decc6SDave Kleikamp /* journal descriptor can store up to n blocks -bzzz */ 993470decc6SDave Kleikamp n = journal->j_blocksize / sizeof(journal_block_tag_t); 994470decc6SDave Kleikamp journal->j_wbufsize = n; 995470decc6SDave Kleikamp journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL); 996470decc6SDave Kleikamp if (!journal->j_wbuf) { 99725985edcSLucas De Marchi printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n", 998329d291fSHarvey Harrison __func__); 9994b905671SJan Kara goto out_err; 1000470decc6SDave Kleikamp } 1001470decc6SDave Kleikamp 1002f7f4bccbSMingming Cao err = jbd2_journal_bmap(journal, 0, &blocknr); 1003470decc6SDave Kleikamp /* If that failed, give up */ 1004470decc6SDave Kleikamp if (err) { 10053c26bdb4SJustin P. Mattock printk(KERN_ERR "%s: Cannot locate journal superblock\n", 1006329d291fSHarvey Harrison __func__); 10074b905671SJan Kara goto out_err; 1008470decc6SDave Kleikamp } 1009470decc6SDave Kleikamp 1010470decc6SDave Kleikamp bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize); 10114b905671SJan Kara if (!bh) { 10124b905671SJan Kara printk(KERN_ERR 10134b905671SJan Kara "%s: Cannot get buffer for journal superblock\n", 10144b905671SJan Kara __func__); 10154b905671SJan Kara goto out_err; 10164b905671SJan Kara } 1017470decc6SDave Kleikamp journal->j_sb_buffer = bh; 1018470decc6SDave Kleikamp journal->j_superblock = (journal_superblock_t *)bh->b_data; 1019470decc6SDave Kleikamp 1020470decc6SDave Kleikamp return journal; 10214b905671SJan Kara out_err: 10227b02bec0STao Ma kfree(journal->j_wbuf); 10234b905671SJan Kara jbd2_stats_proc_exit(journal); 10244b905671SJan Kara kfree(journal); 10254b905671SJan Kara return NULL; 1026470decc6SDave Kleikamp } 1027470decc6SDave Kleikamp 1028470decc6SDave Kleikamp /* 1029470decc6SDave Kleikamp * If the journal init or create aborts, we need to mark the journal 1030470decc6SDave Kleikamp * superblock as being NULL to prevent the journal destroy from writing 1031470decc6SDave Kleikamp * back a bogus superblock. 1032470decc6SDave Kleikamp */ 1033470decc6SDave Kleikamp static void journal_fail_superblock (journal_t *journal) 1034470decc6SDave Kleikamp { 1035470decc6SDave Kleikamp struct buffer_head *bh = journal->j_sb_buffer; 1036470decc6SDave Kleikamp brelse(bh); 1037470decc6SDave Kleikamp journal->j_sb_buffer = NULL; 1038470decc6SDave Kleikamp } 1039470decc6SDave Kleikamp 1040470decc6SDave Kleikamp /* 1041470decc6SDave Kleikamp * Given a journal_t structure, initialise the various fields for 1042470decc6SDave Kleikamp * startup of a new journaling session. We use this both when creating 1043470decc6SDave Kleikamp * a journal, and after recovering an old journal to reset it for 1044470decc6SDave Kleikamp * subsequent use. 1045470decc6SDave Kleikamp */ 1046470decc6SDave Kleikamp 1047470decc6SDave Kleikamp static int journal_reset(journal_t *journal) 1048470decc6SDave Kleikamp { 1049470decc6SDave Kleikamp journal_superblock_t *sb = journal->j_superblock; 105018eba7aaSMingming Cao unsigned long long first, last; 1051470decc6SDave Kleikamp 1052470decc6SDave Kleikamp first = be32_to_cpu(sb->s_first); 1053470decc6SDave Kleikamp last = be32_to_cpu(sb->s_maxlen); 1054f6f50e28SJan Kara if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) { 1055f6f50e28SJan Kara printk(KERN_ERR "JBD: Journal too short (blocks %llu-%llu).\n", 1056f6f50e28SJan Kara first, last); 1057f6f50e28SJan Kara journal_fail_superblock(journal); 1058f6f50e28SJan Kara return -EINVAL; 1059f6f50e28SJan Kara } 1060470decc6SDave Kleikamp 1061470decc6SDave Kleikamp journal->j_first = first; 1062470decc6SDave Kleikamp journal->j_last = last; 1063470decc6SDave Kleikamp 1064470decc6SDave Kleikamp journal->j_head = first; 1065470decc6SDave Kleikamp journal->j_tail = first; 1066470decc6SDave Kleikamp journal->j_free = last - first; 1067470decc6SDave Kleikamp 1068470decc6SDave Kleikamp journal->j_tail_sequence = journal->j_transaction_sequence; 1069470decc6SDave Kleikamp journal->j_commit_sequence = journal->j_transaction_sequence - 1; 1070470decc6SDave Kleikamp journal->j_commit_request = journal->j_commit_sequence; 1071470decc6SDave Kleikamp 1072470decc6SDave Kleikamp journal->j_max_transaction_buffers = journal->j_maxlen / 4; 1073470decc6SDave Kleikamp 1074470decc6SDave Kleikamp /* Add the dynamic fields and write it to disk. */ 1075f7f4bccbSMingming Cao jbd2_journal_update_superblock(journal, 1); 107697f06784SPavel Emelianov return jbd2_journal_start_thread(journal); 1077470decc6SDave Kleikamp } 1078470decc6SDave Kleikamp 1079470decc6SDave Kleikamp /** 1080f7f4bccbSMingming Cao * void jbd2_journal_update_superblock() - Update journal sb on disk. 1081470decc6SDave Kleikamp * @journal: The journal to update. 1082470decc6SDave Kleikamp * @wait: Set to '0' if you don't want to wait for IO completion. 1083470decc6SDave Kleikamp * 1084470decc6SDave Kleikamp * Update a journal's dynamic superblock fields and write it to disk, 1085470decc6SDave Kleikamp * optionally waiting for the IO to complete. 1086470decc6SDave Kleikamp */ 1087f7f4bccbSMingming Cao void jbd2_journal_update_superblock(journal_t *journal, int wait) 1088470decc6SDave Kleikamp { 1089470decc6SDave Kleikamp journal_superblock_t *sb = journal->j_superblock; 1090470decc6SDave Kleikamp struct buffer_head *bh = journal->j_sb_buffer; 1091470decc6SDave Kleikamp 1092470decc6SDave Kleikamp /* 1093470decc6SDave Kleikamp * As a special case, if the on-disk copy is already marked as needing 1094470decc6SDave Kleikamp * no recovery (s_start == 0) and there are no outstanding transactions 1095470decc6SDave Kleikamp * in the filesystem, then we can safely defer the superblock update 1096f7f4bccbSMingming Cao * until the next commit by setting JBD2_FLUSHED. This avoids 1097470decc6SDave Kleikamp * attempting a write to a potential-readonly device. 1098470decc6SDave Kleikamp */ 1099470decc6SDave Kleikamp if (sb->s_start == 0 && journal->j_tail_sequence == 1100470decc6SDave Kleikamp journal->j_transaction_sequence) { 1101470decc6SDave Kleikamp jbd_debug(1,"JBD: Skipping superblock update on recovered sb " 1102470decc6SDave Kleikamp "(start %ld, seq %d, errno %d)\n", 1103470decc6SDave Kleikamp journal->j_tail, journal->j_tail_sequence, 1104470decc6SDave Kleikamp journal->j_errno); 1105470decc6SDave Kleikamp goto out; 1106470decc6SDave Kleikamp } 1107470decc6SDave Kleikamp 1108914258bfSTheodore Ts'o if (buffer_write_io_error(bh)) { 1109914258bfSTheodore Ts'o /* 1110914258bfSTheodore Ts'o * Oh, dear. A previous attempt to write the journal 1111914258bfSTheodore Ts'o * superblock failed. This could happen because the 1112914258bfSTheodore Ts'o * USB device was yanked out. Or it could happen to 1113914258bfSTheodore Ts'o * be a transient write error and maybe the block will 1114914258bfSTheodore Ts'o * be remapped. Nothing we can do but to retry the 1115914258bfSTheodore Ts'o * write and hope for the best. 1116914258bfSTheodore Ts'o */ 1117914258bfSTheodore Ts'o printk(KERN_ERR "JBD2: previous I/O error detected " 1118914258bfSTheodore Ts'o "for journal superblock update for %s.\n", 1119914258bfSTheodore Ts'o journal->j_devname); 1120914258bfSTheodore Ts'o clear_buffer_write_io_error(bh); 1121914258bfSTheodore Ts'o set_buffer_uptodate(bh); 1122914258bfSTheodore Ts'o } 1123914258bfSTheodore Ts'o 1124a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 1125470decc6SDave Kleikamp jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n", 1126470decc6SDave Kleikamp journal->j_tail, journal->j_tail_sequence, journal->j_errno); 1127470decc6SDave Kleikamp 1128470decc6SDave Kleikamp sb->s_sequence = cpu_to_be32(journal->j_tail_sequence); 1129470decc6SDave Kleikamp sb->s_start = cpu_to_be32(journal->j_tail); 1130470decc6SDave Kleikamp sb->s_errno = cpu_to_be32(journal->j_errno); 1131a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 1132470decc6SDave Kleikamp 1133470decc6SDave Kleikamp BUFFER_TRACE(bh, "marking dirty"); 1134470decc6SDave Kleikamp mark_buffer_dirty(bh); 1135914258bfSTheodore Ts'o if (wait) { 1136470decc6SDave Kleikamp sync_dirty_buffer(bh); 1137914258bfSTheodore Ts'o if (buffer_write_io_error(bh)) { 1138914258bfSTheodore Ts'o printk(KERN_ERR "JBD2: I/O error detected " 1139914258bfSTheodore Ts'o "when updating journal superblock for %s.\n", 1140914258bfSTheodore Ts'o journal->j_devname); 1141914258bfSTheodore Ts'o clear_buffer_write_io_error(bh); 1142914258bfSTheodore Ts'o set_buffer_uptodate(bh); 1143914258bfSTheodore Ts'o } 1144914258bfSTheodore Ts'o } else 11459cb569d6SChristoph Hellwig write_dirty_buffer(bh, WRITE); 1146470decc6SDave Kleikamp 1147470decc6SDave Kleikamp out: 1148470decc6SDave Kleikamp /* If we have just flushed the log (by marking s_start==0), then 1149470decc6SDave Kleikamp * any future commit will have to be careful to update the 1150470decc6SDave Kleikamp * superblock again to re-record the true start of the log. */ 1151470decc6SDave Kleikamp 1152a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 1153470decc6SDave Kleikamp if (sb->s_start) 1154f7f4bccbSMingming Cao journal->j_flags &= ~JBD2_FLUSHED; 1155470decc6SDave Kleikamp else 1156f7f4bccbSMingming Cao journal->j_flags |= JBD2_FLUSHED; 1157a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 1158470decc6SDave Kleikamp } 1159470decc6SDave Kleikamp 1160470decc6SDave Kleikamp /* 1161470decc6SDave Kleikamp * Read the superblock for a given journal, performing initial 1162470decc6SDave Kleikamp * validation of the format. 1163470decc6SDave Kleikamp */ 1164470decc6SDave Kleikamp 1165470decc6SDave Kleikamp static int journal_get_superblock(journal_t *journal) 1166470decc6SDave Kleikamp { 1167470decc6SDave Kleikamp struct buffer_head *bh; 1168470decc6SDave Kleikamp journal_superblock_t *sb; 1169470decc6SDave Kleikamp int err = -EIO; 1170470decc6SDave Kleikamp 1171470decc6SDave Kleikamp bh = journal->j_sb_buffer; 1172470decc6SDave Kleikamp 1173470decc6SDave Kleikamp J_ASSERT(bh != NULL); 1174470decc6SDave Kleikamp if (!buffer_uptodate(bh)) { 1175470decc6SDave Kleikamp ll_rw_block(READ, 1, &bh); 1176470decc6SDave Kleikamp wait_on_buffer(bh); 1177470decc6SDave Kleikamp if (!buffer_uptodate(bh)) { 1178470decc6SDave Kleikamp printk (KERN_ERR 1179470decc6SDave Kleikamp "JBD: IO error reading journal superblock\n"); 1180470decc6SDave Kleikamp goto out; 1181470decc6SDave Kleikamp } 1182470decc6SDave Kleikamp } 1183470decc6SDave Kleikamp 1184470decc6SDave Kleikamp sb = journal->j_superblock; 1185470decc6SDave Kleikamp 1186470decc6SDave Kleikamp err = -EINVAL; 1187470decc6SDave Kleikamp 1188f7f4bccbSMingming Cao if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) || 1189470decc6SDave Kleikamp sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) { 1190470decc6SDave Kleikamp printk(KERN_WARNING "JBD: no valid journal superblock found\n"); 1191470decc6SDave Kleikamp goto out; 1192470decc6SDave Kleikamp } 1193470decc6SDave Kleikamp 1194470decc6SDave Kleikamp switch(be32_to_cpu(sb->s_header.h_blocktype)) { 1195f7f4bccbSMingming Cao case JBD2_SUPERBLOCK_V1: 1196470decc6SDave Kleikamp journal->j_format_version = 1; 1197470decc6SDave Kleikamp break; 1198f7f4bccbSMingming Cao case JBD2_SUPERBLOCK_V2: 1199470decc6SDave Kleikamp journal->j_format_version = 2; 1200470decc6SDave Kleikamp break; 1201470decc6SDave Kleikamp default: 1202470decc6SDave Kleikamp printk(KERN_WARNING "JBD: unrecognised superblock format ID\n"); 1203470decc6SDave Kleikamp goto out; 1204470decc6SDave Kleikamp } 1205470decc6SDave Kleikamp 1206470decc6SDave Kleikamp if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen) 1207470decc6SDave Kleikamp journal->j_maxlen = be32_to_cpu(sb->s_maxlen); 1208470decc6SDave Kleikamp else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) { 1209470decc6SDave Kleikamp printk (KERN_WARNING "JBD: journal file too short\n"); 1210470decc6SDave Kleikamp goto out; 1211470decc6SDave Kleikamp } 1212470decc6SDave Kleikamp 1213470decc6SDave Kleikamp return 0; 1214470decc6SDave Kleikamp 1215470decc6SDave Kleikamp out: 1216470decc6SDave Kleikamp journal_fail_superblock(journal); 1217470decc6SDave Kleikamp return err; 1218470decc6SDave Kleikamp } 1219470decc6SDave Kleikamp 1220470decc6SDave Kleikamp /* 1221470decc6SDave Kleikamp * Load the on-disk journal superblock and read the key fields into the 1222470decc6SDave Kleikamp * journal_t. 1223470decc6SDave Kleikamp */ 1224470decc6SDave Kleikamp 1225470decc6SDave Kleikamp static int load_superblock(journal_t *journal) 1226470decc6SDave Kleikamp { 1227470decc6SDave Kleikamp int err; 1228470decc6SDave Kleikamp journal_superblock_t *sb; 1229470decc6SDave Kleikamp 1230470decc6SDave Kleikamp err = journal_get_superblock(journal); 1231470decc6SDave Kleikamp if (err) 1232470decc6SDave Kleikamp return err; 1233470decc6SDave Kleikamp 1234470decc6SDave Kleikamp sb = journal->j_superblock; 1235470decc6SDave Kleikamp 1236470decc6SDave Kleikamp journal->j_tail_sequence = be32_to_cpu(sb->s_sequence); 1237470decc6SDave Kleikamp journal->j_tail = be32_to_cpu(sb->s_start); 1238470decc6SDave Kleikamp journal->j_first = be32_to_cpu(sb->s_first); 1239470decc6SDave Kleikamp journal->j_last = be32_to_cpu(sb->s_maxlen); 1240470decc6SDave Kleikamp journal->j_errno = be32_to_cpu(sb->s_errno); 1241470decc6SDave Kleikamp 1242470decc6SDave Kleikamp return 0; 1243470decc6SDave Kleikamp } 1244470decc6SDave Kleikamp 1245470decc6SDave Kleikamp 1246470decc6SDave Kleikamp /** 1247f7f4bccbSMingming Cao * int jbd2_journal_load() - Read journal from disk. 1248470decc6SDave Kleikamp * @journal: Journal to act on. 1249470decc6SDave Kleikamp * 1250470decc6SDave Kleikamp * Given a journal_t structure which tells us which disk blocks contain 1251470decc6SDave Kleikamp * a journal, read the journal from disk to initialise the in-memory 1252470decc6SDave Kleikamp * structures. 1253470decc6SDave Kleikamp */ 1254f7f4bccbSMingming Cao int jbd2_journal_load(journal_t *journal) 1255470decc6SDave Kleikamp { 1256470decc6SDave Kleikamp int err; 1257470decc6SDave Kleikamp journal_superblock_t *sb; 1258470decc6SDave Kleikamp 1259470decc6SDave Kleikamp err = load_superblock(journal); 1260470decc6SDave Kleikamp if (err) 1261470decc6SDave Kleikamp return err; 1262470decc6SDave Kleikamp 1263470decc6SDave Kleikamp sb = journal->j_superblock; 1264470decc6SDave Kleikamp /* If this is a V2 superblock, then we have to check the 1265470decc6SDave Kleikamp * features flags on it. */ 1266470decc6SDave Kleikamp 1267470decc6SDave Kleikamp if (journal->j_format_version >= 2) { 1268470decc6SDave Kleikamp if ((sb->s_feature_ro_compat & 1269f7f4bccbSMingming Cao ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) || 1270470decc6SDave Kleikamp (sb->s_feature_incompat & 1271f7f4bccbSMingming Cao ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) { 1272470decc6SDave Kleikamp printk (KERN_WARNING 1273470decc6SDave Kleikamp "JBD: Unrecognised features on journal\n"); 1274470decc6SDave Kleikamp return -EINVAL; 1275470decc6SDave Kleikamp } 1276470decc6SDave Kleikamp } 1277470decc6SDave Kleikamp 1278d2eecb03STheodore Ts'o /* 1279d2eecb03STheodore Ts'o * Create a slab for this blocksize 1280d2eecb03STheodore Ts'o */ 1281d2eecb03STheodore Ts'o err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize)); 1282d2eecb03STheodore Ts'o if (err) 1283d2eecb03STheodore Ts'o return err; 1284d2eecb03STheodore Ts'o 1285470decc6SDave Kleikamp /* Let the recovery code check whether it needs to recover any 1286470decc6SDave Kleikamp * data from the journal. */ 1287f7f4bccbSMingming Cao if (jbd2_journal_recover(journal)) 1288470decc6SDave Kleikamp goto recovery_error; 1289470decc6SDave Kleikamp 1290e6a47428STheodore Ts'o if (journal->j_failed_commit) { 1291e6a47428STheodore Ts'o printk(KERN_ERR "JBD2: journal transaction %u on %s " 1292e6a47428STheodore Ts'o "is corrupt.\n", journal->j_failed_commit, 1293e6a47428STheodore Ts'o journal->j_devname); 1294e6a47428STheodore Ts'o return -EIO; 1295e6a47428STheodore Ts'o } 1296e6a47428STheodore Ts'o 1297470decc6SDave Kleikamp /* OK, we've finished with the dynamic journal bits: 1298470decc6SDave Kleikamp * reinitialise the dynamic contents of the superblock in memory 1299470decc6SDave Kleikamp * and reset them on disk. */ 1300470decc6SDave Kleikamp if (journal_reset(journal)) 1301470decc6SDave Kleikamp goto recovery_error; 1302470decc6SDave Kleikamp 1303f7f4bccbSMingming Cao journal->j_flags &= ~JBD2_ABORT; 1304f7f4bccbSMingming Cao journal->j_flags |= JBD2_LOADED; 1305470decc6SDave Kleikamp return 0; 1306470decc6SDave Kleikamp 1307470decc6SDave Kleikamp recovery_error: 1308470decc6SDave Kleikamp printk (KERN_WARNING "JBD: recovery failed\n"); 1309470decc6SDave Kleikamp return -EIO; 1310470decc6SDave Kleikamp } 1311470decc6SDave Kleikamp 1312470decc6SDave Kleikamp /** 1313f7f4bccbSMingming Cao * void jbd2_journal_destroy() - Release a journal_t structure. 1314470decc6SDave Kleikamp * @journal: Journal to act on. 1315470decc6SDave Kleikamp * 1316470decc6SDave Kleikamp * Release a journal_t structure once it is no longer in use by the 1317470decc6SDave Kleikamp * journaled object. 131844519fafSHidehiro Kawai * Return <0 if we couldn't clean up the journal. 1319470decc6SDave Kleikamp */ 132044519fafSHidehiro Kawai int jbd2_journal_destroy(journal_t *journal) 1321470decc6SDave Kleikamp { 132244519fafSHidehiro Kawai int err = 0; 132344519fafSHidehiro Kawai 1324470decc6SDave Kleikamp /* Wait for the commit thread to wake up and die. */ 1325470decc6SDave Kleikamp journal_kill_thread(journal); 1326470decc6SDave Kleikamp 1327470decc6SDave Kleikamp /* Force a final log commit */ 1328470decc6SDave Kleikamp if (journal->j_running_transaction) 1329f7f4bccbSMingming Cao jbd2_journal_commit_transaction(journal); 1330470decc6SDave Kleikamp 1331470decc6SDave Kleikamp /* Force any old transactions to disk */ 1332470decc6SDave Kleikamp 1333470decc6SDave Kleikamp /* Totally anal locking here... */ 1334470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 1335470decc6SDave Kleikamp while (journal->j_checkpoint_transactions != NULL) { 1336470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 13371a0d3786STheodore Ts'o mutex_lock(&journal->j_checkpoint_mutex); 1338f7f4bccbSMingming Cao jbd2_log_do_checkpoint(journal); 13391a0d3786STheodore Ts'o mutex_unlock(&journal->j_checkpoint_mutex); 1340470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 1341470decc6SDave Kleikamp } 1342470decc6SDave Kleikamp 1343470decc6SDave Kleikamp J_ASSERT(journal->j_running_transaction == NULL); 1344470decc6SDave Kleikamp J_ASSERT(journal->j_committing_transaction == NULL); 1345470decc6SDave Kleikamp J_ASSERT(journal->j_checkpoint_transactions == NULL); 1346470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 1347470decc6SDave Kleikamp 134844519fafSHidehiro Kawai if (journal->j_sb_buffer) { 134944519fafSHidehiro Kawai if (!is_journal_aborted(journal)) { 1350470decc6SDave Kleikamp /* We can now mark the journal as empty. */ 1351470decc6SDave Kleikamp journal->j_tail = 0; 135244519fafSHidehiro Kawai journal->j_tail_sequence = 135344519fafSHidehiro Kawai ++journal->j_transaction_sequence; 1354f7f4bccbSMingming Cao jbd2_journal_update_superblock(journal, 1); 135544519fafSHidehiro Kawai } else { 135644519fafSHidehiro Kawai err = -EIO; 135744519fafSHidehiro Kawai } 1358470decc6SDave Kleikamp brelse(journal->j_sb_buffer); 1359470decc6SDave Kleikamp } 1360470decc6SDave Kleikamp 13618e85fb3fSJohann Lombardi if (journal->j_proc_entry) 13628e85fb3fSJohann Lombardi jbd2_stats_proc_exit(journal); 1363470decc6SDave Kleikamp if (journal->j_inode) 1364470decc6SDave Kleikamp iput(journal->j_inode); 1365470decc6SDave Kleikamp if (journal->j_revoke) 1366f7f4bccbSMingming Cao jbd2_journal_destroy_revoke(journal); 1367470decc6SDave Kleikamp kfree(journal->j_wbuf); 1368470decc6SDave Kleikamp kfree(journal); 136944519fafSHidehiro Kawai 137044519fafSHidehiro Kawai return err; 1371470decc6SDave Kleikamp } 1372470decc6SDave Kleikamp 1373470decc6SDave Kleikamp 1374470decc6SDave Kleikamp /** 1375f7f4bccbSMingming Cao *int jbd2_journal_check_used_features () - Check if features specified are used. 1376470decc6SDave Kleikamp * @journal: Journal to check. 1377470decc6SDave Kleikamp * @compat: bitmask of compatible features 1378470decc6SDave Kleikamp * @ro: bitmask of features that force read-only mount 1379470decc6SDave Kleikamp * @incompat: bitmask of incompatible features 1380470decc6SDave Kleikamp * 1381470decc6SDave Kleikamp * Check whether the journal uses all of a given set of 1382470decc6SDave Kleikamp * features. Return true (non-zero) if it does. 1383470decc6SDave Kleikamp **/ 1384470decc6SDave Kleikamp 1385f7f4bccbSMingming Cao int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat, 1386470decc6SDave Kleikamp unsigned long ro, unsigned long incompat) 1387470decc6SDave Kleikamp { 1388470decc6SDave Kleikamp journal_superblock_t *sb; 1389470decc6SDave Kleikamp 1390470decc6SDave Kleikamp if (!compat && !ro && !incompat) 1391470decc6SDave Kleikamp return 1; 13921113e1b5SPatrick J. LoPresti /* Load journal superblock if it is not loaded yet. */ 13931113e1b5SPatrick J. LoPresti if (journal->j_format_version == 0 && 13941113e1b5SPatrick J. LoPresti journal_get_superblock(journal) != 0) 13951113e1b5SPatrick J. LoPresti return 0; 1396470decc6SDave Kleikamp if (journal->j_format_version == 1) 1397470decc6SDave Kleikamp return 0; 1398470decc6SDave Kleikamp 1399470decc6SDave Kleikamp sb = journal->j_superblock; 1400470decc6SDave Kleikamp 1401470decc6SDave Kleikamp if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) && 1402470decc6SDave Kleikamp ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) && 1403470decc6SDave Kleikamp ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat)) 1404470decc6SDave Kleikamp return 1; 1405470decc6SDave Kleikamp 1406470decc6SDave Kleikamp return 0; 1407470decc6SDave Kleikamp } 1408470decc6SDave Kleikamp 1409470decc6SDave Kleikamp /** 1410f7f4bccbSMingming Cao * int jbd2_journal_check_available_features() - Check feature set in journalling layer 1411470decc6SDave Kleikamp * @journal: Journal to check. 1412470decc6SDave Kleikamp * @compat: bitmask of compatible features 1413470decc6SDave Kleikamp * @ro: bitmask of features that force read-only mount 1414470decc6SDave Kleikamp * @incompat: bitmask of incompatible features 1415470decc6SDave Kleikamp * 1416470decc6SDave Kleikamp * Check whether the journaling code supports the use of 1417470decc6SDave Kleikamp * all of a given set of features on this journal. Return true 1418470decc6SDave Kleikamp * (non-zero) if it can. */ 1419470decc6SDave Kleikamp 1420f7f4bccbSMingming Cao int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat, 1421470decc6SDave Kleikamp unsigned long ro, unsigned long incompat) 1422470decc6SDave Kleikamp { 1423470decc6SDave Kleikamp if (!compat && !ro && !incompat) 1424470decc6SDave Kleikamp return 1; 1425470decc6SDave Kleikamp 1426470decc6SDave Kleikamp /* We can support any known requested features iff the 1427470decc6SDave Kleikamp * superblock is in version 2. Otherwise we fail to support any 1428470decc6SDave Kleikamp * extended sb features. */ 1429470decc6SDave Kleikamp 1430470decc6SDave Kleikamp if (journal->j_format_version != 2) 1431470decc6SDave Kleikamp return 0; 1432470decc6SDave Kleikamp 1433f7f4bccbSMingming Cao if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat && 1434f7f4bccbSMingming Cao (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro && 1435f7f4bccbSMingming Cao (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat) 1436470decc6SDave Kleikamp return 1; 1437470decc6SDave Kleikamp 1438470decc6SDave Kleikamp return 0; 1439470decc6SDave Kleikamp } 1440470decc6SDave Kleikamp 1441470decc6SDave Kleikamp /** 1442f7f4bccbSMingming Cao * int jbd2_journal_set_features () - Mark a given journal feature in the superblock 1443470decc6SDave Kleikamp * @journal: Journal to act on. 1444470decc6SDave Kleikamp * @compat: bitmask of compatible features 1445470decc6SDave Kleikamp * @ro: bitmask of features that force read-only mount 1446470decc6SDave Kleikamp * @incompat: bitmask of incompatible features 1447470decc6SDave Kleikamp * 1448470decc6SDave Kleikamp * Mark a given journal feature as present on the 1449470decc6SDave Kleikamp * superblock. Returns true if the requested features could be set. 1450470decc6SDave Kleikamp * 1451470decc6SDave Kleikamp */ 1452470decc6SDave Kleikamp 1453f7f4bccbSMingming Cao int jbd2_journal_set_features (journal_t *journal, unsigned long compat, 1454470decc6SDave Kleikamp unsigned long ro, unsigned long incompat) 1455470decc6SDave Kleikamp { 1456470decc6SDave Kleikamp journal_superblock_t *sb; 1457470decc6SDave Kleikamp 1458f7f4bccbSMingming Cao if (jbd2_journal_check_used_features(journal, compat, ro, incompat)) 1459470decc6SDave Kleikamp return 1; 1460470decc6SDave Kleikamp 1461f7f4bccbSMingming Cao if (!jbd2_journal_check_available_features(journal, compat, ro, incompat)) 1462470decc6SDave Kleikamp return 0; 1463470decc6SDave Kleikamp 1464470decc6SDave Kleikamp jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n", 1465470decc6SDave Kleikamp compat, ro, incompat); 1466470decc6SDave Kleikamp 1467470decc6SDave Kleikamp sb = journal->j_superblock; 1468470decc6SDave Kleikamp 1469470decc6SDave Kleikamp sb->s_feature_compat |= cpu_to_be32(compat); 1470470decc6SDave Kleikamp sb->s_feature_ro_compat |= cpu_to_be32(ro); 1471470decc6SDave Kleikamp sb->s_feature_incompat |= cpu_to_be32(incompat); 1472470decc6SDave Kleikamp 1473470decc6SDave Kleikamp return 1; 1474470decc6SDave Kleikamp } 1475470decc6SDave Kleikamp 1476818d276cSGirish Shilamkar /* 1477818d276cSGirish Shilamkar * jbd2_journal_clear_features () - Clear a given journal feature in the 1478818d276cSGirish Shilamkar * superblock 1479818d276cSGirish Shilamkar * @journal: Journal to act on. 1480818d276cSGirish Shilamkar * @compat: bitmask of compatible features 1481818d276cSGirish Shilamkar * @ro: bitmask of features that force read-only mount 1482818d276cSGirish Shilamkar * @incompat: bitmask of incompatible features 1483818d276cSGirish Shilamkar * 1484818d276cSGirish Shilamkar * Clear a given journal feature as present on the 1485818d276cSGirish Shilamkar * superblock. 1486818d276cSGirish Shilamkar */ 1487818d276cSGirish Shilamkar void jbd2_journal_clear_features(journal_t *journal, unsigned long compat, 1488818d276cSGirish Shilamkar unsigned long ro, unsigned long incompat) 1489818d276cSGirish Shilamkar { 1490818d276cSGirish Shilamkar journal_superblock_t *sb; 1491818d276cSGirish Shilamkar 1492818d276cSGirish Shilamkar jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n", 1493818d276cSGirish Shilamkar compat, ro, incompat); 1494818d276cSGirish Shilamkar 1495818d276cSGirish Shilamkar sb = journal->j_superblock; 1496818d276cSGirish Shilamkar 1497818d276cSGirish Shilamkar sb->s_feature_compat &= ~cpu_to_be32(compat); 1498818d276cSGirish Shilamkar sb->s_feature_ro_compat &= ~cpu_to_be32(ro); 1499818d276cSGirish Shilamkar sb->s_feature_incompat &= ~cpu_to_be32(incompat); 1500818d276cSGirish Shilamkar } 1501818d276cSGirish Shilamkar EXPORT_SYMBOL(jbd2_journal_clear_features); 1502470decc6SDave Kleikamp 1503470decc6SDave Kleikamp /** 1504f7f4bccbSMingming Cao * int jbd2_journal_update_format () - Update on-disk journal structure. 1505470decc6SDave Kleikamp * @journal: Journal to act on. 1506470decc6SDave Kleikamp * 1507470decc6SDave Kleikamp * Given an initialised but unloaded journal struct, poke about in the 1508470decc6SDave Kleikamp * on-disk structure to update it to the most recent supported version. 1509470decc6SDave Kleikamp */ 1510f7f4bccbSMingming Cao int jbd2_journal_update_format (journal_t *journal) 1511470decc6SDave Kleikamp { 1512470decc6SDave Kleikamp journal_superblock_t *sb; 1513470decc6SDave Kleikamp int err; 1514470decc6SDave Kleikamp 1515470decc6SDave Kleikamp err = journal_get_superblock(journal); 1516470decc6SDave Kleikamp if (err) 1517470decc6SDave Kleikamp return err; 1518470decc6SDave Kleikamp 1519470decc6SDave Kleikamp sb = journal->j_superblock; 1520470decc6SDave Kleikamp 1521470decc6SDave Kleikamp switch (be32_to_cpu(sb->s_header.h_blocktype)) { 1522f7f4bccbSMingming Cao case JBD2_SUPERBLOCK_V2: 1523470decc6SDave Kleikamp return 0; 1524f7f4bccbSMingming Cao case JBD2_SUPERBLOCK_V1: 1525470decc6SDave Kleikamp return journal_convert_superblock_v1(journal, sb); 1526470decc6SDave Kleikamp default: 1527470decc6SDave Kleikamp break; 1528470decc6SDave Kleikamp } 1529470decc6SDave Kleikamp return -EINVAL; 1530470decc6SDave Kleikamp } 1531470decc6SDave Kleikamp 1532470decc6SDave Kleikamp static int journal_convert_superblock_v1(journal_t *journal, 1533470decc6SDave Kleikamp journal_superblock_t *sb) 1534470decc6SDave Kleikamp { 1535470decc6SDave Kleikamp int offset, blocksize; 1536470decc6SDave Kleikamp struct buffer_head *bh; 1537470decc6SDave Kleikamp 1538470decc6SDave Kleikamp printk(KERN_WARNING 1539470decc6SDave Kleikamp "JBD: Converting superblock from version 1 to 2.\n"); 1540470decc6SDave Kleikamp 1541470decc6SDave Kleikamp /* Pre-initialise new fields to zero */ 1542470decc6SDave Kleikamp offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb); 1543470decc6SDave Kleikamp blocksize = be32_to_cpu(sb->s_blocksize); 1544470decc6SDave Kleikamp memset(&sb->s_feature_compat, 0, blocksize-offset); 1545470decc6SDave Kleikamp 1546470decc6SDave Kleikamp sb->s_nr_users = cpu_to_be32(1); 1547f7f4bccbSMingming Cao sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2); 1548470decc6SDave Kleikamp journal->j_format_version = 2; 1549470decc6SDave Kleikamp 1550470decc6SDave Kleikamp bh = journal->j_sb_buffer; 1551470decc6SDave Kleikamp BUFFER_TRACE(bh, "marking dirty"); 1552470decc6SDave Kleikamp mark_buffer_dirty(bh); 1553470decc6SDave Kleikamp sync_dirty_buffer(bh); 1554470decc6SDave Kleikamp return 0; 1555470decc6SDave Kleikamp } 1556470decc6SDave Kleikamp 1557470decc6SDave Kleikamp 1558470decc6SDave Kleikamp /** 1559f7f4bccbSMingming Cao * int jbd2_journal_flush () - Flush journal 1560470decc6SDave Kleikamp * @journal: Journal to act on. 1561470decc6SDave Kleikamp * 1562470decc6SDave Kleikamp * Flush all data for a given journal to disk and empty the journal. 1563470decc6SDave Kleikamp * Filesystems can use this when remounting readonly to ensure that 1564470decc6SDave Kleikamp * recovery does not need to happen on remount. 1565470decc6SDave Kleikamp */ 1566470decc6SDave Kleikamp 1567f7f4bccbSMingming Cao int jbd2_journal_flush(journal_t *journal) 1568470decc6SDave Kleikamp { 1569470decc6SDave Kleikamp int err = 0; 1570470decc6SDave Kleikamp transaction_t *transaction = NULL; 1571470decc6SDave Kleikamp unsigned long old_tail; 1572470decc6SDave Kleikamp 1573a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 1574470decc6SDave Kleikamp 1575470decc6SDave Kleikamp /* Force everything buffered to the log... */ 1576470decc6SDave Kleikamp if (journal->j_running_transaction) { 1577470decc6SDave Kleikamp transaction = journal->j_running_transaction; 1578f7f4bccbSMingming Cao __jbd2_log_start_commit(journal, transaction->t_tid); 1579470decc6SDave Kleikamp } else if (journal->j_committing_transaction) 1580470decc6SDave Kleikamp transaction = journal->j_committing_transaction; 1581470decc6SDave Kleikamp 1582470decc6SDave Kleikamp /* Wait for the log commit to complete... */ 1583470decc6SDave Kleikamp if (transaction) { 1584470decc6SDave Kleikamp tid_t tid = transaction->t_tid; 1585470decc6SDave Kleikamp 1586a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 1587f7f4bccbSMingming Cao jbd2_log_wait_commit(journal, tid); 1588470decc6SDave Kleikamp } else { 1589a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 1590470decc6SDave Kleikamp } 1591470decc6SDave Kleikamp 1592470decc6SDave Kleikamp /* ...and flush everything in the log out to disk. */ 1593470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 1594470decc6SDave Kleikamp while (!err && journal->j_checkpoint_transactions != NULL) { 1595470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 159644519fafSHidehiro Kawai mutex_lock(&journal->j_checkpoint_mutex); 1597f7f4bccbSMingming Cao err = jbd2_log_do_checkpoint(journal); 159844519fafSHidehiro Kawai mutex_unlock(&journal->j_checkpoint_mutex); 1599470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 1600470decc6SDave Kleikamp } 1601470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 160244519fafSHidehiro Kawai 160344519fafSHidehiro Kawai if (is_journal_aborted(journal)) 160444519fafSHidehiro Kawai return -EIO; 160544519fafSHidehiro Kawai 1606f7f4bccbSMingming Cao jbd2_cleanup_journal_tail(journal); 1607470decc6SDave Kleikamp 1608470decc6SDave Kleikamp /* Finally, mark the journal as really needing no recovery. 1609470decc6SDave Kleikamp * This sets s_start==0 in the underlying superblock, which is 1610470decc6SDave Kleikamp * the magic code for a fully-recovered superblock. Any future 1611470decc6SDave Kleikamp * commits of data to the journal will restore the current 1612470decc6SDave Kleikamp * s_start value. */ 1613a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 1614470decc6SDave Kleikamp old_tail = journal->j_tail; 1615470decc6SDave Kleikamp journal->j_tail = 0; 1616a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 1617f7f4bccbSMingming Cao jbd2_journal_update_superblock(journal, 1); 1618a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 1619470decc6SDave Kleikamp journal->j_tail = old_tail; 1620470decc6SDave Kleikamp 1621470decc6SDave Kleikamp J_ASSERT(!journal->j_running_transaction); 1622470decc6SDave Kleikamp J_ASSERT(!journal->j_committing_transaction); 1623470decc6SDave Kleikamp J_ASSERT(!journal->j_checkpoint_transactions); 1624470decc6SDave Kleikamp J_ASSERT(journal->j_head == journal->j_tail); 1625470decc6SDave Kleikamp J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence); 1626a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 162744519fafSHidehiro Kawai return 0; 1628470decc6SDave Kleikamp } 1629470decc6SDave Kleikamp 1630470decc6SDave Kleikamp /** 1631f7f4bccbSMingming Cao * int jbd2_journal_wipe() - Wipe journal contents 1632470decc6SDave Kleikamp * @journal: Journal to act on. 1633470decc6SDave Kleikamp * @write: flag (see below) 1634470decc6SDave Kleikamp * 1635470decc6SDave Kleikamp * Wipe out all of the contents of a journal, safely. This will produce 1636470decc6SDave Kleikamp * a warning if the journal contains any valid recovery information. 1637f7f4bccbSMingming Cao * Must be called between journal_init_*() and jbd2_journal_load(). 1638470decc6SDave Kleikamp * 1639470decc6SDave Kleikamp * If 'write' is non-zero, then we wipe out the journal on disk; otherwise 1640470decc6SDave Kleikamp * we merely suppress recovery. 1641470decc6SDave Kleikamp */ 1642470decc6SDave Kleikamp 1643f7f4bccbSMingming Cao int jbd2_journal_wipe(journal_t *journal, int write) 1644470decc6SDave Kleikamp { 1645470decc6SDave Kleikamp int err = 0; 1646470decc6SDave Kleikamp 1647f7f4bccbSMingming Cao J_ASSERT (!(journal->j_flags & JBD2_LOADED)); 1648470decc6SDave Kleikamp 1649470decc6SDave Kleikamp err = load_superblock(journal); 1650470decc6SDave Kleikamp if (err) 1651470decc6SDave Kleikamp return err; 1652470decc6SDave Kleikamp 1653470decc6SDave Kleikamp if (!journal->j_tail) 1654470decc6SDave Kleikamp goto no_recovery; 1655470decc6SDave Kleikamp 1656470decc6SDave Kleikamp printk (KERN_WARNING "JBD: %s recovery information on journal\n", 1657470decc6SDave Kleikamp write ? "Clearing" : "Ignoring"); 1658470decc6SDave Kleikamp 1659f7f4bccbSMingming Cao err = jbd2_journal_skip_recovery(journal); 1660470decc6SDave Kleikamp if (write) 1661f7f4bccbSMingming Cao jbd2_journal_update_superblock(journal, 1); 1662470decc6SDave Kleikamp 1663470decc6SDave Kleikamp no_recovery: 1664470decc6SDave Kleikamp return err; 1665470decc6SDave Kleikamp } 1666470decc6SDave Kleikamp 1667470decc6SDave Kleikamp /* 1668470decc6SDave Kleikamp * Journal abort has very specific semantics, which we describe 1669470decc6SDave Kleikamp * for journal abort. 1670470decc6SDave Kleikamp * 1671bfcd3555SAlberto Bertogli * Two internal functions, which provide abort to the jbd layer 1672470decc6SDave Kleikamp * itself are here. 1673470decc6SDave Kleikamp */ 1674470decc6SDave Kleikamp 1675470decc6SDave Kleikamp /* 1676470decc6SDave Kleikamp * Quick version for internal journal use (doesn't lock the journal). 1677470decc6SDave Kleikamp * Aborts hard --- we mark the abort as occurred, but do _nothing_ else, 1678470decc6SDave Kleikamp * and don't attempt to make any other journal updates. 1679470decc6SDave Kleikamp */ 1680f7f4bccbSMingming Cao void __jbd2_journal_abort_hard(journal_t *journal) 1681470decc6SDave Kleikamp { 1682470decc6SDave Kleikamp transaction_t *transaction; 1683470decc6SDave Kleikamp 1684f7f4bccbSMingming Cao if (journal->j_flags & JBD2_ABORT) 1685470decc6SDave Kleikamp return; 1686470decc6SDave Kleikamp 1687470decc6SDave Kleikamp printk(KERN_ERR "Aborting journal on device %s.\n", 168805496769STheodore Ts'o journal->j_devname); 1689470decc6SDave Kleikamp 1690a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 1691f7f4bccbSMingming Cao journal->j_flags |= JBD2_ABORT; 1692470decc6SDave Kleikamp transaction = journal->j_running_transaction; 1693470decc6SDave Kleikamp if (transaction) 1694f7f4bccbSMingming Cao __jbd2_log_start_commit(journal, transaction->t_tid); 1695a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 1696470decc6SDave Kleikamp } 1697470decc6SDave Kleikamp 1698470decc6SDave Kleikamp /* Soft abort: record the abort error status in the journal superblock, 1699470decc6SDave Kleikamp * but don't do any other IO. */ 1700470decc6SDave Kleikamp static void __journal_abort_soft (journal_t *journal, int errno) 1701470decc6SDave Kleikamp { 1702f7f4bccbSMingming Cao if (journal->j_flags & JBD2_ABORT) 1703470decc6SDave Kleikamp return; 1704470decc6SDave Kleikamp 1705470decc6SDave Kleikamp if (!journal->j_errno) 1706470decc6SDave Kleikamp journal->j_errno = errno; 1707470decc6SDave Kleikamp 1708f7f4bccbSMingming Cao __jbd2_journal_abort_hard(journal); 1709470decc6SDave Kleikamp 1710470decc6SDave Kleikamp if (errno) 1711f7f4bccbSMingming Cao jbd2_journal_update_superblock(journal, 1); 1712470decc6SDave Kleikamp } 1713470decc6SDave Kleikamp 1714470decc6SDave Kleikamp /** 1715f7f4bccbSMingming Cao * void jbd2_journal_abort () - Shutdown the journal immediately. 1716470decc6SDave Kleikamp * @journal: the journal to shutdown. 1717470decc6SDave Kleikamp * @errno: an error number to record in the journal indicating 1718470decc6SDave Kleikamp * the reason for the shutdown. 1719470decc6SDave Kleikamp * 1720470decc6SDave Kleikamp * Perform a complete, immediate shutdown of the ENTIRE 1721470decc6SDave Kleikamp * journal (not of a single transaction). This operation cannot be 1722470decc6SDave Kleikamp * undone without closing and reopening the journal. 1723470decc6SDave Kleikamp * 1724f7f4bccbSMingming Cao * The jbd2_journal_abort function is intended to support higher level error 1725470decc6SDave Kleikamp * recovery mechanisms such as the ext2/ext3 remount-readonly error 1726470decc6SDave Kleikamp * mode. 1727470decc6SDave Kleikamp * 1728470decc6SDave Kleikamp * Journal abort has very specific semantics. Any existing dirty, 1729470decc6SDave Kleikamp * unjournaled buffers in the main filesystem will still be written to 1730470decc6SDave Kleikamp * disk by bdflush, but the journaling mechanism will be suspended 1731470decc6SDave Kleikamp * immediately and no further transaction commits will be honoured. 1732470decc6SDave Kleikamp * 1733470decc6SDave Kleikamp * Any dirty, journaled buffers will be written back to disk without 1734470decc6SDave Kleikamp * hitting the journal. Atomicity cannot be guaranteed on an aborted 1735470decc6SDave Kleikamp * filesystem, but we _do_ attempt to leave as much data as possible 1736470decc6SDave Kleikamp * behind for fsck to use for cleanup. 1737470decc6SDave Kleikamp * 1738470decc6SDave Kleikamp * Any attempt to get a new transaction handle on a journal which is in 1739470decc6SDave Kleikamp * ABORT state will just result in an -EROFS error return. A 1740f7f4bccbSMingming Cao * jbd2_journal_stop on an existing handle will return -EIO if we have 1741470decc6SDave Kleikamp * entered abort state during the update. 1742470decc6SDave Kleikamp * 1743470decc6SDave Kleikamp * Recursive transactions are not disturbed by journal abort until the 1744f7f4bccbSMingming Cao * final jbd2_journal_stop, which will receive the -EIO error. 1745470decc6SDave Kleikamp * 1746f7f4bccbSMingming Cao * Finally, the jbd2_journal_abort call allows the caller to supply an errno 1747470decc6SDave Kleikamp * which will be recorded (if possible) in the journal superblock. This 1748470decc6SDave Kleikamp * allows a client to record failure conditions in the middle of a 1749470decc6SDave Kleikamp * transaction without having to complete the transaction to record the 1750470decc6SDave Kleikamp * failure to disk. ext3_error, for example, now uses this 1751470decc6SDave Kleikamp * functionality. 1752470decc6SDave Kleikamp * 1753470decc6SDave Kleikamp * Errors which originate from within the journaling layer will NOT 1754470decc6SDave Kleikamp * supply an errno; a null errno implies that absolutely no further 1755470decc6SDave Kleikamp * writes are done to the journal (unless there are any already in 1756470decc6SDave Kleikamp * progress). 1757470decc6SDave Kleikamp * 1758470decc6SDave Kleikamp */ 1759470decc6SDave Kleikamp 1760f7f4bccbSMingming Cao void jbd2_journal_abort(journal_t *journal, int errno) 1761470decc6SDave Kleikamp { 1762470decc6SDave Kleikamp __journal_abort_soft(journal, errno); 1763470decc6SDave Kleikamp } 1764470decc6SDave Kleikamp 1765470decc6SDave Kleikamp /** 1766f7f4bccbSMingming Cao * int jbd2_journal_errno () - returns the journal's error state. 1767470decc6SDave Kleikamp * @journal: journal to examine. 1768470decc6SDave Kleikamp * 1769bfcd3555SAlberto Bertogli * This is the errno number set with jbd2_journal_abort(), the last 1770470decc6SDave Kleikamp * time the journal was mounted - if the journal was stopped 1771470decc6SDave Kleikamp * without calling abort this will be 0. 1772470decc6SDave Kleikamp * 1773470decc6SDave Kleikamp * If the journal has been aborted on this mount time -EROFS will 1774470decc6SDave Kleikamp * be returned. 1775470decc6SDave Kleikamp */ 1776f7f4bccbSMingming Cao int jbd2_journal_errno(journal_t *journal) 1777470decc6SDave Kleikamp { 1778470decc6SDave Kleikamp int err; 1779470decc6SDave Kleikamp 1780a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 1781f7f4bccbSMingming Cao if (journal->j_flags & JBD2_ABORT) 1782470decc6SDave Kleikamp err = -EROFS; 1783470decc6SDave Kleikamp else 1784470decc6SDave Kleikamp err = journal->j_errno; 1785a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 1786470decc6SDave Kleikamp return err; 1787470decc6SDave Kleikamp } 1788470decc6SDave Kleikamp 1789470decc6SDave Kleikamp /** 1790f7f4bccbSMingming Cao * int jbd2_journal_clear_err () - clears the journal's error state 1791470decc6SDave Kleikamp * @journal: journal to act on. 1792470decc6SDave Kleikamp * 1793bfcd3555SAlberto Bertogli * An error must be cleared or acked to take a FS out of readonly 1794470decc6SDave Kleikamp * mode. 1795470decc6SDave Kleikamp */ 1796f7f4bccbSMingming Cao int jbd2_journal_clear_err(journal_t *journal) 1797470decc6SDave Kleikamp { 1798470decc6SDave Kleikamp int err = 0; 1799470decc6SDave Kleikamp 1800a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 1801f7f4bccbSMingming Cao if (journal->j_flags & JBD2_ABORT) 1802470decc6SDave Kleikamp err = -EROFS; 1803470decc6SDave Kleikamp else 1804470decc6SDave Kleikamp journal->j_errno = 0; 1805a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 1806470decc6SDave Kleikamp return err; 1807470decc6SDave Kleikamp } 1808470decc6SDave Kleikamp 1809470decc6SDave Kleikamp /** 1810f7f4bccbSMingming Cao * void jbd2_journal_ack_err() - Ack journal err. 1811470decc6SDave Kleikamp * @journal: journal to act on. 1812470decc6SDave Kleikamp * 1813bfcd3555SAlberto Bertogli * An error must be cleared or acked to take a FS out of readonly 1814470decc6SDave Kleikamp * mode. 1815470decc6SDave Kleikamp */ 1816f7f4bccbSMingming Cao void jbd2_journal_ack_err(journal_t *journal) 1817470decc6SDave Kleikamp { 1818a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 1819470decc6SDave Kleikamp if (journal->j_errno) 1820f7f4bccbSMingming Cao journal->j_flags |= JBD2_ACK_ERR; 1821a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 1822470decc6SDave Kleikamp } 1823470decc6SDave Kleikamp 1824f7f4bccbSMingming Cao int jbd2_journal_blocks_per_page(struct inode *inode) 1825470decc6SDave Kleikamp { 1826470decc6SDave Kleikamp return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); 1827470decc6SDave Kleikamp } 1828470decc6SDave Kleikamp 1829470decc6SDave Kleikamp /* 1830b517bea1SZach Brown * helper functions to deal with 32 or 64bit block numbers. 1831b517bea1SZach Brown */ 1832b517bea1SZach Brown size_t journal_tag_bytes(journal_t *journal) 1833b517bea1SZach Brown { 1834b517bea1SZach Brown if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT)) 1835cd02ff0bSMingming Cao return JBD2_TAG_SIZE64; 1836b517bea1SZach Brown else 1837cd02ff0bSMingming Cao return JBD2_TAG_SIZE32; 1838b517bea1SZach Brown } 1839b517bea1SZach Brown 1840b517bea1SZach Brown /* 1841d2eecb03STheodore Ts'o * JBD memory management 1842d2eecb03STheodore Ts'o * 1843d2eecb03STheodore Ts'o * These functions are used to allocate block-sized chunks of memory 1844d2eecb03STheodore Ts'o * used for making copies of buffer_head data. Very often it will be 1845d2eecb03STheodore Ts'o * page-sized chunks of data, but sometimes it will be in 1846d2eecb03STheodore Ts'o * sub-page-size chunks. (For example, 16k pages on Power systems 1847d2eecb03STheodore Ts'o * with a 4k block file system.) For blocks smaller than a page, we 1848d2eecb03STheodore Ts'o * use a SLAB allocator. There are slab caches for each block size, 1849d2eecb03STheodore Ts'o * which are allocated at mount time, if necessary, and we only free 1850d2eecb03STheodore Ts'o * (all of) the slab caches when/if the jbd2 module is unloaded. For 1851d2eecb03STheodore Ts'o * this reason we don't need to a mutex to protect access to 1852d2eecb03STheodore Ts'o * jbd2_slab[] allocating or releasing memory; only in 1853d2eecb03STheodore Ts'o * jbd2_journal_create_slab(). 1854d2eecb03STheodore Ts'o */ 1855d2eecb03STheodore Ts'o #define JBD2_MAX_SLABS 8 1856d2eecb03STheodore Ts'o static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS]; 1857d2eecb03STheodore Ts'o 1858d2eecb03STheodore Ts'o static const char *jbd2_slab_names[JBD2_MAX_SLABS] = { 1859d2eecb03STheodore Ts'o "jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k", 1860d2eecb03STheodore Ts'o "jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k" 1861d2eecb03STheodore Ts'o }; 1862d2eecb03STheodore Ts'o 1863d2eecb03STheodore Ts'o 1864d2eecb03STheodore Ts'o static void jbd2_journal_destroy_slabs(void) 1865d2eecb03STheodore Ts'o { 1866d2eecb03STheodore Ts'o int i; 1867d2eecb03STheodore Ts'o 1868d2eecb03STheodore Ts'o for (i = 0; i < JBD2_MAX_SLABS; i++) { 1869d2eecb03STheodore Ts'o if (jbd2_slab[i]) 1870d2eecb03STheodore Ts'o kmem_cache_destroy(jbd2_slab[i]); 1871d2eecb03STheodore Ts'o jbd2_slab[i] = NULL; 1872d2eecb03STheodore Ts'o } 1873d2eecb03STheodore Ts'o } 1874d2eecb03STheodore Ts'o 1875d2eecb03STheodore Ts'o static int jbd2_journal_create_slab(size_t size) 1876d2eecb03STheodore Ts'o { 187751dfacdeSThomas Gleixner static DEFINE_MUTEX(jbd2_slab_create_mutex); 1878d2eecb03STheodore Ts'o int i = order_base_2(size) - 10; 1879d2eecb03STheodore Ts'o size_t slab_size; 1880d2eecb03STheodore Ts'o 1881d2eecb03STheodore Ts'o if (size == PAGE_SIZE) 1882d2eecb03STheodore Ts'o return 0; 1883d2eecb03STheodore Ts'o 1884d2eecb03STheodore Ts'o if (i >= JBD2_MAX_SLABS) 1885d2eecb03STheodore Ts'o return -EINVAL; 1886d2eecb03STheodore Ts'o 1887d2eecb03STheodore Ts'o if (unlikely(i < 0)) 1888d2eecb03STheodore Ts'o i = 0; 188951dfacdeSThomas Gleixner mutex_lock(&jbd2_slab_create_mutex); 1890d2eecb03STheodore Ts'o if (jbd2_slab[i]) { 189151dfacdeSThomas Gleixner mutex_unlock(&jbd2_slab_create_mutex); 1892d2eecb03STheodore Ts'o return 0; /* Already created */ 1893d2eecb03STheodore Ts'o } 1894d2eecb03STheodore Ts'o 1895d2eecb03STheodore Ts'o slab_size = 1 << (i+10); 1896d2eecb03STheodore Ts'o jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size, 1897d2eecb03STheodore Ts'o slab_size, 0, NULL); 189851dfacdeSThomas Gleixner mutex_unlock(&jbd2_slab_create_mutex); 1899d2eecb03STheodore Ts'o if (!jbd2_slab[i]) { 1900d2eecb03STheodore Ts'o printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n"); 1901d2eecb03STheodore Ts'o return -ENOMEM; 1902d2eecb03STheodore Ts'o } 1903d2eecb03STheodore Ts'o return 0; 1904d2eecb03STheodore Ts'o } 1905d2eecb03STheodore Ts'o 1906d2eecb03STheodore Ts'o static struct kmem_cache *get_slab(size_t size) 1907d2eecb03STheodore Ts'o { 1908d2eecb03STheodore Ts'o int i = order_base_2(size) - 10; 1909d2eecb03STheodore Ts'o 1910d2eecb03STheodore Ts'o BUG_ON(i >= JBD2_MAX_SLABS); 1911d2eecb03STheodore Ts'o if (unlikely(i < 0)) 1912d2eecb03STheodore Ts'o i = 0; 19138ac97b74SBill Pemberton BUG_ON(jbd2_slab[i] == NULL); 1914d2eecb03STheodore Ts'o return jbd2_slab[i]; 1915d2eecb03STheodore Ts'o } 1916d2eecb03STheodore Ts'o 1917d2eecb03STheodore Ts'o void *jbd2_alloc(size_t size, gfp_t flags) 1918d2eecb03STheodore Ts'o { 1919d2eecb03STheodore Ts'o void *ptr; 1920d2eecb03STheodore Ts'o 1921d2eecb03STheodore Ts'o BUG_ON(size & (size-1)); /* Must be a power of 2 */ 1922d2eecb03STheodore Ts'o 1923d2eecb03STheodore Ts'o flags |= __GFP_REPEAT; 1924d2eecb03STheodore Ts'o if (size == PAGE_SIZE) 1925d2eecb03STheodore Ts'o ptr = (void *)__get_free_pages(flags, 0); 1926d2eecb03STheodore Ts'o else if (size > PAGE_SIZE) { 1927d2eecb03STheodore Ts'o int order = get_order(size); 1928d2eecb03STheodore Ts'o 1929d2eecb03STheodore Ts'o if (order < 3) 1930d2eecb03STheodore Ts'o ptr = (void *)__get_free_pages(flags, order); 1931d2eecb03STheodore Ts'o else 1932d2eecb03STheodore Ts'o ptr = vmalloc(size); 1933d2eecb03STheodore Ts'o } else 1934d2eecb03STheodore Ts'o ptr = kmem_cache_alloc(get_slab(size), flags); 1935d2eecb03STheodore Ts'o 1936d2eecb03STheodore Ts'o /* Check alignment; SLUB has gotten this wrong in the past, 1937d2eecb03STheodore Ts'o * and this can lead to user data corruption! */ 1938d2eecb03STheodore Ts'o BUG_ON(((unsigned long) ptr) & (size-1)); 1939d2eecb03STheodore Ts'o 1940d2eecb03STheodore Ts'o return ptr; 1941d2eecb03STheodore Ts'o } 1942d2eecb03STheodore Ts'o 1943d2eecb03STheodore Ts'o void jbd2_free(void *ptr, size_t size) 1944d2eecb03STheodore Ts'o { 1945d2eecb03STheodore Ts'o if (size == PAGE_SIZE) { 1946d2eecb03STheodore Ts'o free_pages((unsigned long)ptr, 0); 1947d2eecb03STheodore Ts'o return; 1948d2eecb03STheodore Ts'o } 1949d2eecb03STheodore Ts'o if (size > PAGE_SIZE) { 1950d2eecb03STheodore Ts'o int order = get_order(size); 1951d2eecb03STheodore Ts'o 1952d2eecb03STheodore Ts'o if (order < 3) 1953d2eecb03STheodore Ts'o free_pages((unsigned long)ptr, order); 1954d2eecb03STheodore Ts'o else 1955d2eecb03STheodore Ts'o vfree(ptr); 1956d2eecb03STheodore Ts'o return; 1957d2eecb03STheodore Ts'o } 1958d2eecb03STheodore Ts'o kmem_cache_free(get_slab(size), ptr); 1959d2eecb03STheodore Ts'o }; 1960d2eecb03STheodore Ts'o 1961d2eecb03STheodore Ts'o /* 1962470decc6SDave Kleikamp * Journal_head storage management 1963470decc6SDave Kleikamp */ 1964e18b890bSChristoph Lameter static struct kmem_cache *jbd2_journal_head_cache; 1965e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 1966470decc6SDave Kleikamp static atomic_t nr_journal_heads = ATOMIC_INIT(0); 1967470decc6SDave Kleikamp #endif 1968470decc6SDave Kleikamp 1969f7f4bccbSMingming Cao static int journal_init_jbd2_journal_head_cache(void) 1970470decc6SDave Kleikamp { 1971470decc6SDave Kleikamp int retval; 1972470decc6SDave Kleikamp 19731076d17aSAl Viro J_ASSERT(jbd2_journal_head_cache == NULL); 1974a920e941SJohann Lombardi jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head", 1975470decc6SDave Kleikamp sizeof(struct journal_head), 1976470decc6SDave Kleikamp 0, /* offset */ 197777160957SMingming Cao SLAB_TEMPORARY, /* flags */ 197820c2df83SPaul Mundt NULL); /* ctor */ 1979470decc6SDave Kleikamp retval = 0; 19801076d17aSAl Viro if (!jbd2_journal_head_cache) { 1981470decc6SDave Kleikamp retval = -ENOMEM; 1982470decc6SDave Kleikamp printk(KERN_EMERG "JBD: no memory for journal_head cache\n"); 1983470decc6SDave Kleikamp } 1984470decc6SDave Kleikamp return retval; 1985470decc6SDave Kleikamp } 1986470decc6SDave Kleikamp 1987f7f4bccbSMingming Cao static void jbd2_journal_destroy_jbd2_journal_head_cache(void) 1988470decc6SDave Kleikamp { 19898a9362ebSDuane Griffin if (jbd2_journal_head_cache) { 1990f7f4bccbSMingming Cao kmem_cache_destroy(jbd2_journal_head_cache); 1991f7f4bccbSMingming Cao jbd2_journal_head_cache = NULL; 1992470decc6SDave Kleikamp } 19938a9362ebSDuane Griffin } 1994470decc6SDave Kleikamp 1995470decc6SDave Kleikamp /* 1996470decc6SDave Kleikamp * journal_head splicing and dicing 1997470decc6SDave Kleikamp */ 1998470decc6SDave Kleikamp static struct journal_head *journal_alloc_journal_head(void) 1999470decc6SDave Kleikamp { 2000470decc6SDave Kleikamp struct journal_head *ret; 2001470decc6SDave Kleikamp 2002e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 2003470decc6SDave Kleikamp atomic_inc(&nr_journal_heads); 2004470decc6SDave Kleikamp #endif 2005f7f4bccbSMingming Cao ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS); 20061076d17aSAl Viro if (!ret) { 2007470decc6SDave Kleikamp jbd_debug(1, "out of memory for journal_head\n"); 2008670be5a7STheodore Ts'o pr_notice_ratelimited("ENOMEM in %s, retrying.\n", __func__); 20091076d17aSAl Viro while (!ret) { 2010470decc6SDave Kleikamp yield(); 2011f7f4bccbSMingming Cao ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS); 2012470decc6SDave Kleikamp } 2013470decc6SDave Kleikamp } 2014470decc6SDave Kleikamp return ret; 2015470decc6SDave Kleikamp } 2016470decc6SDave Kleikamp 2017470decc6SDave Kleikamp static void journal_free_journal_head(struct journal_head *jh) 2018470decc6SDave Kleikamp { 2019e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 2020470decc6SDave Kleikamp atomic_dec(&nr_journal_heads); 2021cd02ff0bSMingming Cao memset(jh, JBD2_POISON_FREE, sizeof(*jh)); 2022470decc6SDave Kleikamp #endif 2023f7f4bccbSMingming Cao kmem_cache_free(jbd2_journal_head_cache, jh); 2024470decc6SDave Kleikamp } 2025470decc6SDave Kleikamp 2026470decc6SDave Kleikamp /* 2027470decc6SDave Kleikamp * A journal_head is attached to a buffer_head whenever JBD has an 2028470decc6SDave Kleikamp * interest in the buffer. 2029470decc6SDave Kleikamp * 2030470decc6SDave Kleikamp * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit 2031470decc6SDave Kleikamp * is set. This bit is tested in core kernel code where we need to take 2032470decc6SDave Kleikamp * JBD-specific actions. Testing the zeroness of ->b_private is not reliable 2033470decc6SDave Kleikamp * there. 2034470decc6SDave Kleikamp * 2035470decc6SDave Kleikamp * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one. 2036470decc6SDave Kleikamp * 2037470decc6SDave Kleikamp * When a buffer has its BH_JBD bit set it is immune from being released by 2038470decc6SDave Kleikamp * core kernel code, mainly via ->b_count. 2039470decc6SDave Kleikamp * 2040470decc6SDave Kleikamp * A journal_head may be detached from its buffer_head when the journal_head's 2041470decc6SDave Kleikamp * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL. 2042f7f4bccbSMingming Cao * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the 2043470decc6SDave Kleikamp * journal_head can be dropped if needed. 2044470decc6SDave Kleikamp * 2045470decc6SDave Kleikamp * Various places in the kernel want to attach a journal_head to a buffer_head 2046470decc6SDave Kleikamp * _before_ attaching the journal_head to a transaction. To protect the 2047f7f4bccbSMingming Cao * journal_head in this situation, jbd2_journal_add_journal_head elevates the 2048470decc6SDave Kleikamp * journal_head's b_jcount refcount by one. The caller must call 2049f7f4bccbSMingming Cao * jbd2_journal_put_journal_head() to undo this. 2050470decc6SDave Kleikamp * 2051470decc6SDave Kleikamp * So the typical usage would be: 2052470decc6SDave Kleikamp * 2053470decc6SDave Kleikamp * (Attach a journal_head if needed. Increments b_jcount) 2054f7f4bccbSMingming Cao * struct journal_head *jh = jbd2_journal_add_journal_head(bh); 2055470decc6SDave Kleikamp * ... 2056470decc6SDave Kleikamp * jh->b_transaction = xxx; 2057f7f4bccbSMingming Cao * jbd2_journal_put_journal_head(jh); 2058470decc6SDave Kleikamp * 2059470decc6SDave Kleikamp * Now, the journal_head's b_jcount is zero, but it is safe from being released 2060470decc6SDave Kleikamp * because it has a non-zero b_transaction. 2061470decc6SDave Kleikamp */ 2062470decc6SDave Kleikamp 2063470decc6SDave Kleikamp /* 2064470decc6SDave Kleikamp * Give a buffer_head a journal_head. 2065470decc6SDave Kleikamp * 2066470decc6SDave Kleikamp * Doesn't need the journal lock. 2067470decc6SDave Kleikamp * May sleep. 2068470decc6SDave Kleikamp */ 2069f7f4bccbSMingming Cao struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh) 2070470decc6SDave Kleikamp { 2071470decc6SDave Kleikamp struct journal_head *jh; 2072470decc6SDave Kleikamp struct journal_head *new_jh = NULL; 2073470decc6SDave Kleikamp 2074470decc6SDave Kleikamp repeat: 2075470decc6SDave Kleikamp if (!buffer_jbd(bh)) { 2076470decc6SDave Kleikamp new_jh = journal_alloc_journal_head(); 2077470decc6SDave Kleikamp memset(new_jh, 0, sizeof(*new_jh)); 2078470decc6SDave Kleikamp } 2079470decc6SDave Kleikamp 2080470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 2081470decc6SDave Kleikamp if (buffer_jbd(bh)) { 2082470decc6SDave Kleikamp jh = bh2jh(bh); 2083470decc6SDave Kleikamp } else { 2084470decc6SDave Kleikamp J_ASSERT_BH(bh, 2085470decc6SDave Kleikamp (atomic_read(&bh->b_count) > 0) || 2086470decc6SDave Kleikamp (bh->b_page && bh->b_page->mapping)); 2087470decc6SDave Kleikamp 2088470decc6SDave Kleikamp if (!new_jh) { 2089470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 2090470decc6SDave Kleikamp goto repeat; 2091470decc6SDave Kleikamp } 2092470decc6SDave Kleikamp 2093470decc6SDave Kleikamp jh = new_jh; 2094470decc6SDave Kleikamp new_jh = NULL; /* We consumed it */ 2095470decc6SDave Kleikamp set_buffer_jbd(bh); 2096470decc6SDave Kleikamp bh->b_private = jh; 2097470decc6SDave Kleikamp jh->b_bh = bh; 2098470decc6SDave Kleikamp get_bh(bh); 2099470decc6SDave Kleikamp BUFFER_TRACE(bh, "added journal_head"); 2100470decc6SDave Kleikamp } 2101470decc6SDave Kleikamp jh->b_jcount++; 2102470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 2103470decc6SDave Kleikamp if (new_jh) 2104470decc6SDave Kleikamp journal_free_journal_head(new_jh); 2105470decc6SDave Kleikamp return bh->b_private; 2106470decc6SDave Kleikamp } 2107470decc6SDave Kleikamp 2108470decc6SDave Kleikamp /* 2109470decc6SDave Kleikamp * Grab a ref against this buffer_head's journal_head. If it ended up not 2110470decc6SDave Kleikamp * having a journal_head, return NULL 2111470decc6SDave Kleikamp */ 2112f7f4bccbSMingming Cao struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh) 2113470decc6SDave Kleikamp { 2114470decc6SDave Kleikamp struct journal_head *jh = NULL; 2115470decc6SDave Kleikamp 2116470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 2117470decc6SDave Kleikamp if (buffer_jbd(bh)) { 2118470decc6SDave Kleikamp jh = bh2jh(bh); 2119470decc6SDave Kleikamp jh->b_jcount++; 2120470decc6SDave Kleikamp } 2121470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 2122470decc6SDave Kleikamp return jh; 2123470decc6SDave Kleikamp } 2124470decc6SDave Kleikamp 2125470decc6SDave Kleikamp static void __journal_remove_journal_head(struct buffer_head *bh) 2126470decc6SDave Kleikamp { 2127470decc6SDave Kleikamp struct journal_head *jh = bh2jh(bh); 2128470decc6SDave Kleikamp 2129470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_jcount >= 0); 2130470decc6SDave Kleikamp 2131470decc6SDave Kleikamp get_bh(bh); 2132470decc6SDave Kleikamp if (jh->b_jcount == 0) { 2133470decc6SDave Kleikamp if (jh->b_transaction == NULL && 2134470decc6SDave Kleikamp jh->b_next_transaction == NULL && 2135470decc6SDave Kleikamp jh->b_cp_transaction == NULL) { 2136470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_jlist == BJ_None); 2137470decc6SDave Kleikamp J_ASSERT_BH(bh, buffer_jbd(bh)); 2138470decc6SDave Kleikamp J_ASSERT_BH(bh, jh2bh(jh) == bh); 2139470decc6SDave Kleikamp BUFFER_TRACE(bh, "remove journal_head"); 2140470decc6SDave Kleikamp if (jh->b_frozen_data) { 2141470decc6SDave Kleikamp printk(KERN_WARNING "%s: freeing " 2142470decc6SDave Kleikamp "b_frozen_data\n", 2143329d291fSHarvey Harrison __func__); 2144af1e76d6SMingming Cao jbd2_free(jh->b_frozen_data, bh->b_size); 2145470decc6SDave Kleikamp } 2146470decc6SDave Kleikamp if (jh->b_committed_data) { 2147470decc6SDave Kleikamp printk(KERN_WARNING "%s: freeing " 2148470decc6SDave Kleikamp "b_committed_data\n", 2149329d291fSHarvey Harrison __func__); 2150af1e76d6SMingming Cao jbd2_free(jh->b_committed_data, bh->b_size); 2151470decc6SDave Kleikamp } 2152470decc6SDave Kleikamp bh->b_private = NULL; 2153470decc6SDave Kleikamp jh->b_bh = NULL; /* debug, really */ 2154470decc6SDave Kleikamp clear_buffer_jbd(bh); 2155470decc6SDave Kleikamp __brelse(bh); 2156470decc6SDave Kleikamp journal_free_journal_head(jh); 2157470decc6SDave Kleikamp } else { 2158470decc6SDave Kleikamp BUFFER_TRACE(bh, "journal_head was locked"); 2159470decc6SDave Kleikamp } 2160470decc6SDave Kleikamp } 2161470decc6SDave Kleikamp } 2162470decc6SDave Kleikamp 2163470decc6SDave Kleikamp /* 2164f7f4bccbSMingming Cao * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction 2165470decc6SDave Kleikamp * and has a zero b_jcount then remove and release its journal_head. If we did 2166470decc6SDave Kleikamp * see that the buffer is not used by any transaction we also "logically" 2167470decc6SDave Kleikamp * decrement ->b_count. 2168470decc6SDave Kleikamp * 2169470decc6SDave Kleikamp * We in fact take an additional increment on ->b_count as a convenience, 2170470decc6SDave Kleikamp * because the caller usually wants to do additional things with the bh 2171470decc6SDave Kleikamp * after calling here. 2172f7f4bccbSMingming Cao * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some 2173470decc6SDave Kleikamp * time. Once the caller has run __brelse(), the buffer is eligible for 2174470decc6SDave Kleikamp * reaping by try_to_free_buffers(). 2175470decc6SDave Kleikamp */ 2176f7f4bccbSMingming Cao void jbd2_journal_remove_journal_head(struct buffer_head *bh) 2177470decc6SDave Kleikamp { 2178470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 2179470decc6SDave Kleikamp __journal_remove_journal_head(bh); 2180470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 2181470decc6SDave Kleikamp } 2182470decc6SDave Kleikamp 2183470decc6SDave Kleikamp /* 2184470decc6SDave Kleikamp * Drop a reference on the passed journal_head. If it fell to zero then try to 2185470decc6SDave Kleikamp * release the journal_head from the buffer_head. 2186470decc6SDave Kleikamp */ 2187f7f4bccbSMingming Cao void jbd2_journal_put_journal_head(struct journal_head *jh) 2188470decc6SDave Kleikamp { 2189470decc6SDave Kleikamp struct buffer_head *bh = jh2bh(jh); 2190470decc6SDave Kleikamp 2191470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 2192470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_jcount > 0); 2193470decc6SDave Kleikamp --jh->b_jcount; 2194470decc6SDave Kleikamp if (!jh->b_jcount && !jh->b_transaction) { 2195470decc6SDave Kleikamp __journal_remove_journal_head(bh); 2196470decc6SDave Kleikamp __brelse(bh); 2197470decc6SDave Kleikamp } 2198470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 2199470decc6SDave Kleikamp } 2200470decc6SDave Kleikamp 2201470decc6SDave Kleikamp /* 2202c851ed54SJan Kara * Initialize jbd inode head 2203c851ed54SJan Kara */ 2204c851ed54SJan Kara void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode) 2205c851ed54SJan Kara { 2206c851ed54SJan Kara jinode->i_transaction = NULL; 2207c851ed54SJan Kara jinode->i_next_transaction = NULL; 2208c851ed54SJan Kara jinode->i_vfs_inode = inode; 2209c851ed54SJan Kara jinode->i_flags = 0; 2210c851ed54SJan Kara INIT_LIST_HEAD(&jinode->i_list); 2211c851ed54SJan Kara } 2212c851ed54SJan Kara 2213c851ed54SJan Kara /* 2214c851ed54SJan Kara * Function to be called before we start removing inode from memory (i.e., 2215c851ed54SJan Kara * clear_inode() is a fine place to be called from). It removes inode from 2216c851ed54SJan Kara * transaction's lists. 2217c851ed54SJan Kara */ 2218c851ed54SJan Kara void jbd2_journal_release_jbd_inode(journal_t *journal, 2219c851ed54SJan Kara struct jbd2_inode *jinode) 2220c851ed54SJan Kara { 2221c851ed54SJan Kara if (!journal) 2222c851ed54SJan Kara return; 2223c851ed54SJan Kara restart: 2224c851ed54SJan Kara spin_lock(&journal->j_list_lock); 2225c851ed54SJan Kara /* Is commit writing out inode - we have to wait */ 222639e3ac25SBrian King if (test_bit(__JI_COMMIT_RUNNING, &jinode->i_flags)) { 2227c851ed54SJan Kara wait_queue_head_t *wq; 2228c851ed54SJan Kara DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING); 2229c851ed54SJan Kara wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING); 2230c851ed54SJan Kara prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE); 2231c851ed54SJan Kara spin_unlock(&journal->j_list_lock); 2232c851ed54SJan Kara schedule(); 2233c851ed54SJan Kara finish_wait(wq, &wait.wait); 2234c851ed54SJan Kara goto restart; 2235c851ed54SJan Kara } 2236c851ed54SJan Kara 2237c851ed54SJan Kara if (jinode->i_transaction) { 2238c851ed54SJan Kara list_del(&jinode->i_list); 2239c851ed54SJan Kara jinode->i_transaction = NULL; 2240c851ed54SJan Kara } 2241c851ed54SJan Kara spin_unlock(&journal->j_list_lock); 2242c851ed54SJan Kara } 2243c851ed54SJan Kara 2244c851ed54SJan Kara /* 22450f49d5d0SJose R. Santos * debugfs tunables 2246470decc6SDave Kleikamp */ 22476f38c74fSJose R. Santos #ifdef CONFIG_JBD2_DEBUG 22486f38c74fSJose R. Santos u8 jbd2_journal_enable_debug __read_mostly; 2249f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_enable_debug); 2250470decc6SDave Kleikamp 22510f49d5d0SJose R. Santos #define JBD2_DEBUG_NAME "jbd2-debug" 2252470decc6SDave Kleikamp 22536f38c74fSJose R. Santos static struct dentry *jbd2_debugfs_dir; 22546f38c74fSJose R. Santos static struct dentry *jbd2_debug; 22550f49d5d0SJose R. Santos 22560f49d5d0SJose R. Santos static void __init jbd2_create_debugfs_entry(void) 2257470decc6SDave Kleikamp { 22580f49d5d0SJose R. Santos jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL); 22590f49d5d0SJose R. Santos if (jbd2_debugfs_dir) 2260765f8361SYin Kangkai jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME, 2261765f8361SYin Kangkai S_IRUGO | S_IWUSR, 22620f49d5d0SJose R. Santos jbd2_debugfs_dir, 22630f49d5d0SJose R. Santos &jbd2_journal_enable_debug); 2264470decc6SDave Kleikamp } 2265470decc6SDave Kleikamp 22660f49d5d0SJose R. Santos static void __exit jbd2_remove_debugfs_entry(void) 2267470decc6SDave Kleikamp { 22680f49d5d0SJose R. Santos debugfs_remove(jbd2_debug); 22690f49d5d0SJose R. Santos debugfs_remove(jbd2_debugfs_dir); 2270470decc6SDave Kleikamp } 2271470decc6SDave Kleikamp 2272470decc6SDave Kleikamp #else 2273470decc6SDave Kleikamp 22740f49d5d0SJose R. Santos static void __init jbd2_create_debugfs_entry(void) 22750f49d5d0SJose R. Santos { 22760f49d5d0SJose R. Santos } 22770f49d5d0SJose R. Santos 22780f49d5d0SJose R. Santos static void __exit jbd2_remove_debugfs_entry(void) 22790f49d5d0SJose R. Santos { 22800f49d5d0SJose R. Santos } 2281470decc6SDave Kleikamp 2282470decc6SDave Kleikamp #endif 2283470decc6SDave Kleikamp 22848e85fb3fSJohann Lombardi #ifdef CONFIG_PROC_FS 22858e85fb3fSJohann Lombardi 22868e85fb3fSJohann Lombardi #define JBD2_STATS_PROC_NAME "fs/jbd2" 22878e85fb3fSJohann Lombardi 22888e85fb3fSJohann Lombardi static void __init jbd2_create_jbd_stats_proc_entry(void) 22898e85fb3fSJohann Lombardi { 22908e85fb3fSJohann Lombardi proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL); 22918e85fb3fSJohann Lombardi } 22928e85fb3fSJohann Lombardi 22938e85fb3fSJohann Lombardi static void __exit jbd2_remove_jbd_stats_proc_entry(void) 22948e85fb3fSJohann Lombardi { 22958e85fb3fSJohann Lombardi if (proc_jbd2_stats) 22968e85fb3fSJohann Lombardi remove_proc_entry(JBD2_STATS_PROC_NAME, NULL); 22978e85fb3fSJohann Lombardi } 22988e85fb3fSJohann Lombardi 22998e85fb3fSJohann Lombardi #else 23008e85fb3fSJohann Lombardi 23018e85fb3fSJohann Lombardi #define jbd2_create_jbd_stats_proc_entry() do {} while (0) 23028e85fb3fSJohann Lombardi #define jbd2_remove_jbd_stats_proc_entry() do {} while (0) 23038e85fb3fSJohann Lombardi 23048e85fb3fSJohann Lombardi #endif 23058e85fb3fSJohann Lombardi 23068aefcd55STheodore Ts'o struct kmem_cache *jbd2_handle_cache, *jbd2_inode_cache; 2307470decc6SDave Kleikamp 2308470decc6SDave Kleikamp static int __init journal_init_handle_cache(void) 2309470decc6SDave Kleikamp { 23108aefcd55STheodore Ts'o jbd2_handle_cache = KMEM_CACHE(jbd2_journal_handle, SLAB_TEMPORARY); 2311f7f4bccbSMingming Cao if (jbd2_handle_cache == NULL) { 23128aefcd55STheodore Ts'o printk(KERN_EMERG "JBD2: failed to create handle cache\n"); 23138aefcd55STheodore Ts'o return -ENOMEM; 23148aefcd55STheodore Ts'o } 23158aefcd55STheodore Ts'o jbd2_inode_cache = KMEM_CACHE(jbd2_inode, 0); 23168aefcd55STheodore Ts'o if (jbd2_inode_cache == NULL) { 23178aefcd55STheodore Ts'o printk(KERN_EMERG "JBD2: failed to create inode cache\n"); 23188aefcd55STheodore Ts'o kmem_cache_destroy(jbd2_handle_cache); 2319470decc6SDave Kleikamp return -ENOMEM; 2320470decc6SDave Kleikamp } 2321470decc6SDave Kleikamp return 0; 2322470decc6SDave Kleikamp } 2323470decc6SDave Kleikamp 2324f7f4bccbSMingming Cao static void jbd2_journal_destroy_handle_cache(void) 2325470decc6SDave Kleikamp { 2326f7f4bccbSMingming Cao if (jbd2_handle_cache) 2327f7f4bccbSMingming Cao kmem_cache_destroy(jbd2_handle_cache); 23288aefcd55STheodore Ts'o if (jbd2_inode_cache) 23298aefcd55STheodore Ts'o kmem_cache_destroy(jbd2_inode_cache); 23308aefcd55STheodore Ts'o 2331470decc6SDave Kleikamp } 2332470decc6SDave Kleikamp 2333470decc6SDave Kleikamp /* 2334470decc6SDave Kleikamp * Module startup and shutdown 2335470decc6SDave Kleikamp */ 2336470decc6SDave Kleikamp 2337470decc6SDave Kleikamp static int __init journal_init_caches(void) 2338470decc6SDave Kleikamp { 2339470decc6SDave Kleikamp int ret; 2340470decc6SDave Kleikamp 2341f7f4bccbSMingming Cao ret = jbd2_journal_init_revoke_caches(); 2342470decc6SDave Kleikamp if (ret == 0) 2343f7f4bccbSMingming Cao ret = journal_init_jbd2_journal_head_cache(); 2344470decc6SDave Kleikamp if (ret == 0) 2345470decc6SDave Kleikamp ret = journal_init_handle_cache(); 2346470decc6SDave Kleikamp return ret; 2347470decc6SDave Kleikamp } 2348470decc6SDave Kleikamp 2349f7f4bccbSMingming Cao static void jbd2_journal_destroy_caches(void) 2350470decc6SDave Kleikamp { 2351f7f4bccbSMingming Cao jbd2_journal_destroy_revoke_caches(); 2352f7f4bccbSMingming Cao jbd2_journal_destroy_jbd2_journal_head_cache(); 2353f7f4bccbSMingming Cao jbd2_journal_destroy_handle_cache(); 2354d2eecb03STheodore Ts'o jbd2_journal_destroy_slabs(); 2355470decc6SDave Kleikamp } 2356470decc6SDave Kleikamp 2357470decc6SDave Kleikamp static int __init journal_init(void) 2358470decc6SDave Kleikamp { 2359470decc6SDave Kleikamp int ret; 2360470decc6SDave Kleikamp 2361470decc6SDave Kleikamp BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024); 2362470decc6SDave Kleikamp 2363470decc6SDave Kleikamp ret = journal_init_caches(); 2364620de4e1SDuane Griffin if (ret == 0) { 23650f49d5d0SJose R. Santos jbd2_create_debugfs_entry(); 23668e85fb3fSJohann Lombardi jbd2_create_jbd_stats_proc_entry(); 2367620de4e1SDuane Griffin } else { 2368620de4e1SDuane Griffin jbd2_journal_destroy_caches(); 2369620de4e1SDuane Griffin } 2370470decc6SDave Kleikamp return ret; 2371470decc6SDave Kleikamp } 2372470decc6SDave Kleikamp 2373470decc6SDave Kleikamp static void __exit journal_exit(void) 2374470decc6SDave Kleikamp { 2375e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 2376470decc6SDave Kleikamp int n = atomic_read(&nr_journal_heads); 2377470decc6SDave Kleikamp if (n) 2378470decc6SDave Kleikamp printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n); 2379470decc6SDave Kleikamp #endif 23800f49d5d0SJose R. Santos jbd2_remove_debugfs_entry(); 23818e85fb3fSJohann Lombardi jbd2_remove_jbd_stats_proc_entry(); 2382f7f4bccbSMingming Cao jbd2_journal_destroy_caches(); 2383470decc6SDave Kleikamp } 2384470decc6SDave Kleikamp 2385879c5e6bSTheodore Ts'o /* 2386879c5e6bSTheodore Ts'o * jbd2_dev_to_name is a utility function used by the jbd2 and ext4 2387879c5e6bSTheodore Ts'o * tracing infrastructure to map a dev_t to a device name. 2388879c5e6bSTheodore Ts'o * 2389879c5e6bSTheodore Ts'o * The caller should use rcu_read_lock() in order to make sure the 2390879c5e6bSTheodore Ts'o * device name stays valid until its done with it. We use 2391879c5e6bSTheodore Ts'o * rcu_read_lock() as well to make sure we're safe in case the caller 2392879c5e6bSTheodore Ts'o * gets sloppy, and because rcu_read_lock() is cheap and can be safely 2393879c5e6bSTheodore Ts'o * nested. 2394879c5e6bSTheodore Ts'o */ 2395879c5e6bSTheodore Ts'o struct devname_cache { 2396879c5e6bSTheodore Ts'o struct rcu_head rcu; 2397879c5e6bSTheodore Ts'o dev_t device; 2398879c5e6bSTheodore Ts'o char devname[BDEVNAME_SIZE]; 2399879c5e6bSTheodore Ts'o }; 2400879c5e6bSTheodore Ts'o #define CACHE_SIZE_BITS 6 2401879c5e6bSTheodore Ts'o static struct devname_cache *devcache[1 << CACHE_SIZE_BITS]; 2402879c5e6bSTheodore Ts'o static DEFINE_SPINLOCK(devname_cache_lock); 2403879c5e6bSTheodore Ts'o 2404879c5e6bSTheodore Ts'o static void free_devcache(struct rcu_head *rcu) 2405879c5e6bSTheodore Ts'o { 2406879c5e6bSTheodore Ts'o kfree(rcu); 2407879c5e6bSTheodore Ts'o } 2408879c5e6bSTheodore Ts'o 2409879c5e6bSTheodore Ts'o const char *jbd2_dev_to_name(dev_t device) 2410879c5e6bSTheodore Ts'o { 2411879c5e6bSTheodore Ts'o int i = hash_32(device, CACHE_SIZE_BITS); 2412879c5e6bSTheodore Ts'o char *ret; 2413879c5e6bSTheodore Ts'o struct block_device *bd; 2414b5744805STheodore Ts'o static struct devname_cache *new_dev; 2415879c5e6bSTheodore Ts'o 2416879c5e6bSTheodore Ts'o rcu_read_lock(); 2417879c5e6bSTheodore Ts'o if (devcache[i] && devcache[i]->device == device) { 2418879c5e6bSTheodore Ts'o ret = devcache[i]->devname; 2419879c5e6bSTheodore Ts'o rcu_read_unlock(); 2420879c5e6bSTheodore Ts'o return ret; 2421879c5e6bSTheodore Ts'o } 2422879c5e6bSTheodore Ts'o rcu_read_unlock(); 2423879c5e6bSTheodore Ts'o 2424b5744805STheodore Ts'o new_dev = kmalloc(sizeof(struct devname_cache), GFP_KERNEL); 2425b5744805STheodore Ts'o if (!new_dev) 2426b5744805STheodore Ts'o return "NODEV-ALLOCFAILURE"; /* Something non-NULL */ 242750f689afSZhu Yanhai bd = bdget(device); 2428879c5e6bSTheodore Ts'o spin_lock(&devname_cache_lock); 2429879c5e6bSTheodore Ts'o if (devcache[i]) { 2430879c5e6bSTheodore Ts'o if (devcache[i]->device == device) { 2431b5744805STheodore Ts'o kfree(new_dev); 243250f689afSZhu Yanhai bdput(bd); 2433879c5e6bSTheodore Ts'o ret = devcache[i]->devname; 2434879c5e6bSTheodore Ts'o spin_unlock(&devname_cache_lock); 2435879c5e6bSTheodore Ts'o return ret; 2436879c5e6bSTheodore Ts'o } 2437879c5e6bSTheodore Ts'o call_rcu(&devcache[i]->rcu, free_devcache); 2438879c5e6bSTheodore Ts'o } 2439b5744805STheodore Ts'o devcache[i] = new_dev; 2440879c5e6bSTheodore Ts'o devcache[i]->device = device; 2441879c5e6bSTheodore Ts'o if (bd) { 2442879c5e6bSTheodore Ts'o bdevname(bd, devcache[i]->devname); 2443879c5e6bSTheodore Ts'o bdput(bd); 2444879c5e6bSTheodore Ts'o } else 2445879c5e6bSTheodore Ts'o __bdevname(device, devcache[i]->devname); 2446879c5e6bSTheodore Ts'o ret = devcache[i]->devname; 2447879c5e6bSTheodore Ts'o spin_unlock(&devname_cache_lock); 2448879c5e6bSTheodore Ts'o return ret; 2449879c5e6bSTheodore Ts'o } 2450879c5e6bSTheodore Ts'o EXPORT_SYMBOL(jbd2_dev_to_name); 2451879c5e6bSTheodore Ts'o 2452470decc6SDave Kleikamp MODULE_LICENSE("GPL"); 2453470decc6SDave Kleikamp module_init(journal_init); 2454470decc6SDave Kleikamp module_exit(journal_exit); 2455470decc6SDave Kleikamp 2456