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> 45879c5e6bSTheodore Ts'o 46879c5e6bSTheodore Ts'o #define CREATE_TRACE_POINTS 47879c5e6bSTheodore Ts'o #include <trace/events/jbd2.h> 48470decc6SDave Kleikamp 49470decc6SDave Kleikamp #include <asm/uaccess.h> 50470decc6SDave Kleikamp #include <asm/page.h> 51470decc6SDave Kleikamp 52f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_extend); 53f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_stop); 54f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_lock_updates); 55f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_unlock_updates); 56f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_get_write_access); 57f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_get_create_access); 58f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_get_undo_access); 59e06c8227SJoel Becker EXPORT_SYMBOL(jbd2_journal_set_triggers); 60f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_dirty_metadata); 61f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_release_buffer); 62f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_forget); 63470decc6SDave Kleikamp #if 0 64470decc6SDave Kleikamp EXPORT_SYMBOL(journal_sync_buffer); 65470decc6SDave Kleikamp #endif 66f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_flush); 67f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_revoke); 68470decc6SDave Kleikamp 69f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_init_dev); 70f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_init_inode); 71f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_update_format); 72f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_check_used_features); 73f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_check_available_features); 74f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_set_features); 75f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_load); 76f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_destroy); 77f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_abort); 78f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_errno); 79f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_ack_err); 80f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_clear_err); 81f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_log_wait_commit); 823b799d15STheodore Ts'o EXPORT_SYMBOL(jbd2_log_start_commit); 83f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_start_commit); 84f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_force_commit_nested); 85f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_wipe); 86f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_blocks_per_page); 87f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_invalidatepage); 88f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers); 89f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_force_commit); 90c851ed54SJan Kara EXPORT_SYMBOL(jbd2_journal_file_inode); 91c851ed54SJan Kara EXPORT_SYMBOL(jbd2_journal_init_jbd_inode); 92c851ed54SJan Kara EXPORT_SYMBOL(jbd2_journal_release_jbd_inode); 93c851ed54SJan Kara EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate); 94470decc6SDave Kleikamp 95470decc6SDave Kleikamp static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *); 96470decc6SDave Kleikamp static void __journal_abort_soft (journal_t *journal, int errno); 97d2eecb03STheodore Ts'o static int jbd2_journal_create_slab(size_t slab_size); 98470decc6SDave Kleikamp 99470decc6SDave Kleikamp /* 100470decc6SDave Kleikamp * Helper function used to manage commit timeouts 101470decc6SDave Kleikamp */ 102470decc6SDave Kleikamp 103470decc6SDave Kleikamp static void commit_timeout(unsigned long __data) 104470decc6SDave Kleikamp { 105470decc6SDave Kleikamp struct task_struct * p = (struct task_struct *) __data; 106470decc6SDave Kleikamp 107470decc6SDave Kleikamp wake_up_process(p); 108470decc6SDave Kleikamp } 109470decc6SDave Kleikamp 110470decc6SDave Kleikamp /* 111f7f4bccbSMingming Cao * kjournald2: The main thread function used to manage a logging device 112470decc6SDave Kleikamp * journal. 113470decc6SDave Kleikamp * 114470decc6SDave Kleikamp * This kernel thread is responsible for two things: 115470decc6SDave Kleikamp * 116470decc6SDave Kleikamp * 1) COMMIT: Every so often we need to commit the current state of the 117470decc6SDave Kleikamp * filesystem to disk. The journal thread is responsible for writing 118470decc6SDave Kleikamp * all of the metadata buffers to disk. 119470decc6SDave Kleikamp * 120470decc6SDave Kleikamp * 2) CHECKPOINT: We cannot reuse a used section of the log file until all 121470decc6SDave Kleikamp * of the data in that part of the log has been rewritten elsewhere on 122470decc6SDave Kleikamp * the disk. Flushing these old buffers to reclaim space in the log is 123470decc6SDave Kleikamp * known as checkpointing, and this thread is responsible for that job. 124470decc6SDave Kleikamp */ 125470decc6SDave Kleikamp 126f7f4bccbSMingming Cao static int kjournald2(void *arg) 127470decc6SDave Kleikamp { 128470decc6SDave Kleikamp journal_t *journal = arg; 129470decc6SDave Kleikamp transaction_t *transaction; 130470decc6SDave Kleikamp 131470decc6SDave Kleikamp /* 132470decc6SDave Kleikamp * Set up an interval timer which can be used to trigger a commit wakeup 133470decc6SDave Kleikamp * after the commit interval expires 134470decc6SDave Kleikamp */ 135470decc6SDave Kleikamp setup_timer(&journal->j_commit_timer, commit_timeout, 136470decc6SDave Kleikamp (unsigned long)current); 137470decc6SDave Kleikamp 138470decc6SDave Kleikamp /* Record that the journal thread is running */ 139470decc6SDave Kleikamp journal->j_task = current; 140470decc6SDave Kleikamp wake_up(&journal->j_wait_done_commit); 141470decc6SDave Kleikamp 142470decc6SDave Kleikamp /* 143470decc6SDave Kleikamp * And now, wait forever for commit wakeup events. 144470decc6SDave Kleikamp */ 145*a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 146470decc6SDave Kleikamp 147470decc6SDave Kleikamp loop: 148f7f4bccbSMingming Cao if (journal->j_flags & JBD2_UNMOUNT) 149470decc6SDave Kleikamp goto end_loop; 150470decc6SDave Kleikamp 151470decc6SDave Kleikamp jbd_debug(1, "commit_sequence=%d, commit_request=%d\n", 152470decc6SDave Kleikamp journal->j_commit_sequence, journal->j_commit_request); 153470decc6SDave Kleikamp 154470decc6SDave Kleikamp if (journal->j_commit_sequence != journal->j_commit_request) { 155470decc6SDave Kleikamp jbd_debug(1, "OK, requests differ\n"); 156*a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 157470decc6SDave Kleikamp del_timer_sync(&journal->j_commit_timer); 158f7f4bccbSMingming Cao jbd2_journal_commit_transaction(journal); 159*a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 160470decc6SDave Kleikamp goto loop; 161470decc6SDave Kleikamp } 162470decc6SDave Kleikamp 163470decc6SDave Kleikamp wake_up(&journal->j_wait_done_commit); 164470decc6SDave Kleikamp if (freezing(current)) { 165470decc6SDave Kleikamp /* 166470decc6SDave Kleikamp * The simpler the better. Flushing journal isn't a 167470decc6SDave Kleikamp * good idea, because that depends on threads that may 168470decc6SDave Kleikamp * be already stopped. 169470decc6SDave Kleikamp */ 170f7f4bccbSMingming Cao jbd_debug(1, "Now suspending kjournald2\n"); 171*a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 172470decc6SDave Kleikamp refrigerator(); 173*a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 174470decc6SDave Kleikamp } else { 175470decc6SDave Kleikamp /* 176470decc6SDave Kleikamp * We assume on resume that commits are already there, 177470decc6SDave Kleikamp * so we don't sleep 178470decc6SDave Kleikamp */ 179470decc6SDave Kleikamp DEFINE_WAIT(wait); 180470decc6SDave Kleikamp int should_sleep = 1; 181470decc6SDave Kleikamp 182470decc6SDave Kleikamp prepare_to_wait(&journal->j_wait_commit, &wait, 183470decc6SDave Kleikamp TASK_INTERRUPTIBLE); 184470decc6SDave Kleikamp if (journal->j_commit_sequence != journal->j_commit_request) 185470decc6SDave Kleikamp should_sleep = 0; 186470decc6SDave Kleikamp transaction = journal->j_running_transaction; 187470decc6SDave Kleikamp if (transaction && time_after_eq(jiffies, 188470decc6SDave Kleikamp transaction->t_expires)) 189470decc6SDave Kleikamp should_sleep = 0; 190f7f4bccbSMingming Cao if (journal->j_flags & JBD2_UNMOUNT) 191470decc6SDave Kleikamp should_sleep = 0; 192470decc6SDave Kleikamp if (should_sleep) { 193*a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 194470decc6SDave Kleikamp schedule(); 195*a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 196470decc6SDave Kleikamp } 197470decc6SDave Kleikamp finish_wait(&journal->j_wait_commit, &wait); 198470decc6SDave Kleikamp } 199470decc6SDave Kleikamp 200f7f4bccbSMingming Cao jbd_debug(1, "kjournald2 wakes\n"); 201470decc6SDave Kleikamp 202470decc6SDave Kleikamp /* 203470decc6SDave Kleikamp * Were we woken up by a commit wakeup event? 204470decc6SDave Kleikamp */ 205470decc6SDave Kleikamp transaction = journal->j_running_transaction; 206470decc6SDave Kleikamp if (transaction && time_after_eq(jiffies, transaction->t_expires)) { 207470decc6SDave Kleikamp journal->j_commit_request = transaction->t_tid; 208470decc6SDave Kleikamp jbd_debug(1, "woke because of timeout\n"); 209470decc6SDave Kleikamp } 210470decc6SDave Kleikamp goto loop; 211470decc6SDave Kleikamp 212470decc6SDave Kleikamp end_loop: 213*a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 214470decc6SDave Kleikamp del_timer_sync(&journal->j_commit_timer); 215470decc6SDave Kleikamp journal->j_task = NULL; 216470decc6SDave Kleikamp wake_up(&journal->j_wait_done_commit); 217470decc6SDave Kleikamp jbd_debug(1, "Journal thread exiting.\n"); 218470decc6SDave Kleikamp return 0; 219470decc6SDave Kleikamp } 220470decc6SDave Kleikamp 22197f06784SPavel Emelianov static int jbd2_journal_start_thread(journal_t *journal) 222470decc6SDave Kleikamp { 22397f06784SPavel Emelianov struct task_struct *t; 22497f06784SPavel Emelianov 22590576c0bSTheodore Ts'o t = kthread_run(kjournald2, journal, "jbd2/%s", 22690576c0bSTheodore Ts'o journal->j_devname); 22797f06784SPavel Emelianov if (IS_ERR(t)) 22897f06784SPavel Emelianov return PTR_ERR(t); 22997f06784SPavel Emelianov 2301076d17aSAl Viro wait_event(journal->j_wait_done_commit, journal->j_task != NULL); 23197f06784SPavel Emelianov return 0; 232470decc6SDave Kleikamp } 233470decc6SDave Kleikamp 234470decc6SDave Kleikamp static void journal_kill_thread(journal_t *journal) 235470decc6SDave Kleikamp { 236*a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 237f7f4bccbSMingming Cao journal->j_flags |= JBD2_UNMOUNT; 238470decc6SDave Kleikamp 239470decc6SDave Kleikamp while (journal->j_task) { 240470decc6SDave Kleikamp wake_up(&journal->j_wait_commit); 241*a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 2421076d17aSAl Viro wait_event(journal->j_wait_done_commit, journal->j_task == NULL); 243*a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 244470decc6SDave Kleikamp } 245*a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 246470decc6SDave Kleikamp } 247470decc6SDave Kleikamp 248470decc6SDave Kleikamp /* 249f7f4bccbSMingming Cao * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal. 250470decc6SDave Kleikamp * 251470decc6SDave Kleikamp * Writes a metadata buffer to a given disk block. The actual IO is not 252470decc6SDave Kleikamp * performed but a new buffer_head is constructed which labels the data 253470decc6SDave Kleikamp * to be written with the correct destination disk block. 254470decc6SDave Kleikamp * 255470decc6SDave Kleikamp * Any magic-number escaping which needs to be done will cause a 256470decc6SDave Kleikamp * copy-out here. If the buffer happens to start with the 257f7f4bccbSMingming Cao * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the 258470decc6SDave Kleikamp * magic number is only written to the log for descripter blocks. In 259470decc6SDave Kleikamp * this case, we copy the data and replace the first word with 0, and we 260470decc6SDave Kleikamp * return a result code which indicates that this buffer needs to be 261470decc6SDave Kleikamp * marked as an escaped buffer in the corresponding log descriptor 262470decc6SDave Kleikamp * block. The missing word can then be restored when the block is read 263470decc6SDave Kleikamp * during recovery. 264470decc6SDave Kleikamp * 265470decc6SDave Kleikamp * If the source buffer has already been modified by a new transaction 266470decc6SDave Kleikamp * since we took the last commit snapshot, we use the frozen copy of 267470decc6SDave Kleikamp * that data for IO. If we end up using the existing buffer_head's data 268470decc6SDave Kleikamp * for the write, then we *have* to lock the buffer to prevent anyone 269470decc6SDave Kleikamp * else from using and possibly modifying it while the IO is in 270470decc6SDave Kleikamp * progress. 271470decc6SDave Kleikamp * 272470decc6SDave Kleikamp * The function returns a pointer to the buffer_heads to be used for IO. 273470decc6SDave Kleikamp * 274470decc6SDave Kleikamp * We assume that the journal has already been locked in this function. 275470decc6SDave Kleikamp * 276470decc6SDave Kleikamp * Return value: 277470decc6SDave Kleikamp * <0: Error 278470decc6SDave Kleikamp * >=0: Finished OK 279470decc6SDave Kleikamp * 280470decc6SDave Kleikamp * On success: 281470decc6SDave Kleikamp * Bit 0 set == escape performed on the data 282470decc6SDave Kleikamp * Bit 1 set == buffer copy-out performed (kfree the data after IO) 283470decc6SDave Kleikamp */ 284470decc6SDave Kleikamp 285f7f4bccbSMingming Cao int jbd2_journal_write_metadata_buffer(transaction_t *transaction, 286470decc6SDave Kleikamp struct journal_head *jh_in, 287470decc6SDave Kleikamp struct journal_head **jh_out, 28818eba7aaSMingming Cao unsigned long long blocknr) 289470decc6SDave Kleikamp { 290470decc6SDave Kleikamp int need_copy_out = 0; 291470decc6SDave Kleikamp int done_copy_out = 0; 292470decc6SDave Kleikamp int do_escape = 0; 293470decc6SDave Kleikamp char *mapped_data; 294470decc6SDave Kleikamp struct buffer_head *new_bh; 295470decc6SDave Kleikamp struct journal_head *new_jh; 296470decc6SDave Kleikamp struct page *new_page; 297470decc6SDave Kleikamp unsigned int new_offset; 298470decc6SDave Kleikamp struct buffer_head *bh_in = jh2bh(jh_in); 299e06c8227SJoel Becker struct jbd2_buffer_trigger_type *triggers; 30096577c43Sdingdinghua journal_t *journal = transaction->t_journal; 301470decc6SDave Kleikamp 302470decc6SDave Kleikamp /* 303470decc6SDave Kleikamp * The buffer really shouldn't be locked: only the current committing 304470decc6SDave Kleikamp * transaction is allowed to write it, so nobody else is allowed 305470decc6SDave Kleikamp * to do any IO. 306470decc6SDave Kleikamp * 307470decc6SDave Kleikamp * akpm: except if we're journalling data, and write() output is 308470decc6SDave Kleikamp * also part of a shared mapping, and another thread has 309470decc6SDave Kleikamp * decided to launch a writepage() against this buffer. 310470decc6SDave Kleikamp */ 311470decc6SDave Kleikamp J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in)); 312470decc6SDave Kleikamp 31347def826STheodore Ts'o retry_alloc: 31447def826STheodore Ts'o new_bh = alloc_buffer_head(GFP_NOFS); 31547def826STheodore Ts'o if (!new_bh) { 31647def826STheodore Ts'o /* 31747def826STheodore Ts'o * Failure is not an option, but __GFP_NOFAIL is going 31847def826STheodore Ts'o * away; so we retry ourselves here. 31947def826STheodore Ts'o */ 32047def826STheodore Ts'o congestion_wait(BLK_RW_ASYNC, HZ/50); 32147def826STheodore Ts'o goto retry_alloc; 32247def826STheodore Ts'o } 32347def826STheodore Ts'o 32496577c43Sdingdinghua /* keep subsequent assertions sane */ 32596577c43Sdingdinghua new_bh->b_state = 0; 32696577c43Sdingdinghua init_buffer(new_bh, NULL, NULL); 32796577c43Sdingdinghua atomic_set(&new_bh->b_count, 1); 32896577c43Sdingdinghua new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */ 329470decc6SDave Kleikamp 330470decc6SDave Kleikamp /* 331470decc6SDave Kleikamp * If a new transaction has already done a buffer copy-out, then 332470decc6SDave Kleikamp * we use that version of the data for the commit. 333470decc6SDave Kleikamp */ 334470decc6SDave Kleikamp jbd_lock_bh_state(bh_in); 335470decc6SDave Kleikamp repeat: 336470decc6SDave Kleikamp if (jh_in->b_frozen_data) { 337470decc6SDave Kleikamp done_copy_out = 1; 338470decc6SDave Kleikamp new_page = virt_to_page(jh_in->b_frozen_data); 339470decc6SDave Kleikamp new_offset = offset_in_page(jh_in->b_frozen_data); 340e06c8227SJoel Becker triggers = jh_in->b_frozen_triggers; 341470decc6SDave Kleikamp } else { 342470decc6SDave Kleikamp new_page = jh2bh(jh_in)->b_page; 343470decc6SDave Kleikamp new_offset = offset_in_page(jh2bh(jh_in)->b_data); 344e06c8227SJoel Becker triggers = jh_in->b_triggers; 345470decc6SDave Kleikamp } 346470decc6SDave Kleikamp 347470decc6SDave Kleikamp mapped_data = kmap_atomic(new_page, KM_USER0); 348470decc6SDave Kleikamp /* 349e06c8227SJoel Becker * Fire any commit trigger. Do this before checking for escaping, 350e06c8227SJoel Becker * as the trigger may modify the magic offset. If a copy-out 351e06c8227SJoel Becker * happens afterwards, it will have the correct data in the buffer. 352e06c8227SJoel Becker */ 353e06c8227SJoel Becker jbd2_buffer_commit_trigger(jh_in, mapped_data + new_offset, 354e06c8227SJoel Becker triggers); 355e06c8227SJoel Becker 356e06c8227SJoel Becker /* 357470decc6SDave Kleikamp * Check for escaping 358470decc6SDave Kleikamp */ 359470decc6SDave Kleikamp if (*((__be32 *)(mapped_data + new_offset)) == 360f7f4bccbSMingming Cao cpu_to_be32(JBD2_MAGIC_NUMBER)) { 361470decc6SDave Kleikamp need_copy_out = 1; 362470decc6SDave Kleikamp do_escape = 1; 363470decc6SDave Kleikamp } 364470decc6SDave Kleikamp kunmap_atomic(mapped_data, KM_USER0); 365470decc6SDave Kleikamp 366470decc6SDave Kleikamp /* 367470decc6SDave Kleikamp * Do we need to do a data copy? 368470decc6SDave Kleikamp */ 369470decc6SDave Kleikamp if (need_copy_out && !done_copy_out) { 370470decc6SDave Kleikamp char *tmp; 371470decc6SDave Kleikamp 372470decc6SDave Kleikamp jbd_unlock_bh_state(bh_in); 373af1e76d6SMingming Cao tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS); 374e6ec116bSTheodore Ts'o if (!tmp) { 375e6ec116bSTheodore Ts'o jbd2_journal_put_journal_head(new_jh); 376e6ec116bSTheodore Ts'o return -ENOMEM; 377e6ec116bSTheodore Ts'o } 378470decc6SDave Kleikamp jbd_lock_bh_state(bh_in); 379470decc6SDave Kleikamp if (jh_in->b_frozen_data) { 380af1e76d6SMingming Cao jbd2_free(tmp, bh_in->b_size); 381470decc6SDave Kleikamp goto repeat; 382470decc6SDave Kleikamp } 383470decc6SDave Kleikamp 384470decc6SDave Kleikamp jh_in->b_frozen_data = tmp; 385470decc6SDave Kleikamp mapped_data = kmap_atomic(new_page, KM_USER0); 386470decc6SDave Kleikamp memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size); 387470decc6SDave Kleikamp kunmap_atomic(mapped_data, KM_USER0); 388470decc6SDave Kleikamp 389470decc6SDave Kleikamp new_page = virt_to_page(tmp); 390470decc6SDave Kleikamp new_offset = offset_in_page(tmp); 391470decc6SDave Kleikamp done_copy_out = 1; 392e06c8227SJoel Becker 393e06c8227SJoel Becker /* 394e06c8227SJoel Becker * This isn't strictly necessary, as we're using frozen 395e06c8227SJoel Becker * data for the escaping, but it keeps consistency with 396e06c8227SJoel Becker * b_frozen_data usage. 397e06c8227SJoel Becker */ 398e06c8227SJoel Becker jh_in->b_frozen_triggers = jh_in->b_triggers; 399470decc6SDave Kleikamp } 400470decc6SDave Kleikamp 401470decc6SDave Kleikamp /* 402470decc6SDave Kleikamp * Did we need to do an escaping? Now we've done all the 403470decc6SDave Kleikamp * copying, we can finally do so. 404470decc6SDave Kleikamp */ 405470decc6SDave Kleikamp if (do_escape) { 406470decc6SDave Kleikamp mapped_data = kmap_atomic(new_page, KM_USER0); 407470decc6SDave Kleikamp *((unsigned int *)(mapped_data + new_offset)) = 0; 408470decc6SDave Kleikamp kunmap_atomic(mapped_data, KM_USER0); 409470decc6SDave Kleikamp } 410470decc6SDave Kleikamp 411470decc6SDave Kleikamp set_bh_page(new_bh, new_page, new_offset); 412470decc6SDave Kleikamp new_jh->b_transaction = NULL; 413470decc6SDave Kleikamp new_bh->b_size = jh2bh(jh_in)->b_size; 414470decc6SDave Kleikamp new_bh->b_bdev = transaction->t_journal->j_dev; 415470decc6SDave Kleikamp new_bh->b_blocknr = blocknr; 416470decc6SDave Kleikamp set_buffer_mapped(new_bh); 417470decc6SDave Kleikamp set_buffer_dirty(new_bh); 418470decc6SDave Kleikamp 419470decc6SDave Kleikamp *jh_out = new_jh; 420470decc6SDave Kleikamp 421470decc6SDave Kleikamp /* 422470decc6SDave Kleikamp * The to-be-written buffer needs to get moved to the io queue, 423470decc6SDave Kleikamp * and the original buffer whose contents we are shadowing or 424470decc6SDave Kleikamp * copying is moved to the transaction's shadow queue. 425470decc6SDave Kleikamp */ 426470decc6SDave Kleikamp JBUFFER_TRACE(jh_in, "file as BJ_Shadow"); 42796577c43Sdingdinghua spin_lock(&journal->j_list_lock); 42896577c43Sdingdinghua __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow); 42996577c43Sdingdinghua spin_unlock(&journal->j_list_lock); 43096577c43Sdingdinghua jbd_unlock_bh_state(bh_in); 43196577c43Sdingdinghua 432470decc6SDave Kleikamp JBUFFER_TRACE(new_jh, "file as BJ_IO"); 433f7f4bccbSMingming Cao jbd2_journal_file_buffer(new_jh, transaction, BJ_IO); 434470decc6SDave Kleikamp 435470decc6SDave Kleikamp return do_escape | (done_copy_out << 1); 436470decc6SDave Kleikamp } 437470decc6SDave Kleikamp 438470decc6SDave Kleikamp /* 439470decc6SDave Kleikamp * Allocation code for the journal file. Manage the space left in the 440470decc6SDave Kleikamp * journal, so that we can begin checkpointing when appropriate. 441470decc6SDave Kleikamp */ 442470decc6SDave Kleikamp 443470decc6SDave Kleikamp /* 444f7f4bccbSMingming Cao * __jbd2_log_space_left: Return the number of free blocks left in the journal. 445470decc6SDave Kleikamp * 446470decc6SDave Kleikamp * Called with the journal already locked. 447470decc6SDave Kleikamp * 448470decc6SDave Kleikamp * Called under j_state_lock 449470decc6SDave Kleikamp */ 450470decc6SDave Kleikamp 451f7f4bccbSMingming Cao int __jbd2_log_space_left(journal_t *journal) 452470decc6SDave Kleikamp { 453470decc6SDave Kleikamp int left = journal->j_free; 454470decc6SDave Kleikamp 455*a931da6aSTheodore Ts'o /* assert_spin_locked(&journal->j_state_lock); */ 456470decc6SDave Kleikamp 457470decc6SDave Kleikamp /* 458470decc6SDave Kleikamp * Be pessimistic here about the number of those free blocks which 459470decc6SDave Kleikamp * might be required for log descriptor control blocks. 460470decc6SDave Kleikamp */ 461470decc6SDave Kleikamp 462470decc6SDave Kleikamp #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */ 463470decc6SDave Kleikamp 464470decc6SDave Kleikamp left -= MIN_LOG_RESERVED_BLOCKS; 465470decc6SDave Kleikamp 466470decc6SDave Kleikamp if (left <= 0) 467470decc6SDave Kleikamp return 0; 468470decc6SDave Kleikamp left -= (left >> 3); 469470decc6SDave Kleikamp return left; 470470decc6SDave Kleikamp } 471470decc6SDave Kleikamp 472470decc6SDave Kleikamp /* 473c88ccea3SJan Kara * Called under j_state_lock. Returns true if a transaction commit was started. 474470decc6SDave Kleikamp */ 475f7f4bccbSMingming Cao int __jbd2_log_start_commit(journal_t *journal, tid_t target) 476470decc6SDave Kleikamp { 477470decc6SDave Kleikamp /* 478470decc6SDave Kleikamp * Are we already doing a recent enough commit? 479470decc6SDave Kleikamp */ 480470decc6SDave Kleikamp if (!tid_geq(journal->j_commit_request, target)) { 481470decc6SDave Kleikamp /* 482470decc6SDave Kleikamp * We want a new commit: OK, mark the request and wakup the 483470decc6SDave Kleikamp * commit thread. We do _not_ do the commit ourselves. 484470decc6SDave Kleikamp */ 485470decc6SDave Kleikamp 486470decc6SDave Kleikamp journal->j_commit_request = target; 487470decc6SDave Kleikamp jbd_debug(1, "JBD: requesting commit %d/%d\n", 488470decc6SDave Kleikamp journal->j_commit_request, 489470decc6SDave Kleikamp journal->j_commit_sequence); 490470decc6SDave Kleikamp wake_up(&journal->j_wait_commit); 491470decc6SDave Kleikamp return 1; 492470decc6SDave Kleikamp } 493470decc6SDave Kleikamp return 0; 494470decc6SDave Kleikamp } 495470decc6SDave Kleikamp 496f7f4bccbSMingming Cao int jbd2_log_start_commit(journal_t *journal, tid_t tid) 497470decc6SDave Kleikamp { 498470decc6SDave Kleikamp int ret; 499470decc6SDave Kleikamp 500*a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 501f7f4bccbSMingming Cao ret = __jbd2_log_start_commit(journal, tid); 502*a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 503470decc6SDave Kleikamp return ret; 504470decc6SDave Kleikamp } 505470decc6SDave Kleikamp 506470decc6SDave Kleikamp /* 507470decc6SDave Kleikamp * Force and wait upon a commit if the calling process is not within 508470decc6SDave Kleikamp * transaction. This is used for forcing out undo-protected data which contains 509470decc6SDave Kleikamp * bitmaps, when the fs is running out of space. 510470decc6SDave Kleikamp * 511470decc6SDave Kleikamp * We can only force the running transaction if we don't have an active handle; 512470decc6SDave Kleikamp * otherwise, we will deadlock. 513470decc6SDave Kleikamp * 514470decc6SDave Kleikamp * Returns true if a transaction was started. 515470decc6SDave Kleikamp */ 516f7f4bccbSMingming Cao int jbd2_journal_force_commit_nested(journal_t *journal) 517470decc6SDave Kleikamp { 518470decc6SDave Kleikamp transaction_t *transaction = NULL; 519470decc6SDave Kleikamp tid_t tid; 520470decc6SDave Kleikamp 521*a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 522470decc6SDave Kleikamp if (journal->j_running_transaction && !current->journal_info) { 523470decc6SDave Kleikamp transaction = journal->j_running_transaction; 524f7f4bccbSMingming Cao __jbd2_log_start_commit(journal, transaction->t_tid); 525470decc6SDave Kleikamp } else if (journal->j_committing_transaction) 526470decc6SDave Kleikamp transaction = journal->j_committing_transaction; 527470decc6SDave Kleikamp 528470decc6SDave Kleikamp if (!transaction) { 529*a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 530470decc6SDave Kleikamp return 0; /* Nothing to retry */ 531470decc6SDave Kleikamp } 532470decc6SDave Kleikamp 533470decc6SDave Kleikamp tid = transaction->t_tid; 534*a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 535f7f4bccbSMingming Cao jbd2_log_wait_commit(journal, tid); 536470decc6SDave Kleikamp return 1; 537470decc6SDave Kleikamp } 538470decc6SDave Kleikamp 539470decc6SDave Kleikamp /* 540470decc6SDave Kleikamp * Start a commit of the current running transaction (if any). Returns true 541c88ccea3SJan Kara * if a transaction is going to be committed (or is currently already 542c88ccea3SJan Kara * committing), and fills its tid in at *ptid 543470decc6SDave Kleikamp */ 544f7f4bccbSMingming Cao int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid) 545470decc6SDave Kleikamp { 546470decc6SDave Kleikamp int ret = 0; 547470decc6SDave Kleikamp 548*a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 549470decc6SDave Kleikamp if (journal->j_running_transaction) { 550470decc6SDave Kleikamp tid_t tid = journal->j_running_transaction->t_tid; 551470decc6SDave Kleikamp 552c88ccea3SJan Kara __jbd2_log_start_commit(journal, tid); 553c88ccea3SJan Kara /* There's a running transaction and we've just made sure 554c88ccea3SJan Kara * it's commit has been scheduled. */ 555c88ccea3SJan Kara if (ptid) 556470decc6SDave Kleikamp *ptid = tid; 557c88ccea3SJan Kara ret = 1; 558c88ccea3SJan Kara } else if (journal->j_committing_transaction) { 559470decc6SDave Kleikamp /* 560470decc6SDave Kleikamp * If ext3_write_super() recently started a commit, then we 561470decc6SDave Kleikamp * have to wait for completion of that transaction 562470decc6SDave Kleikamp */ 563c88ccea3SJan Kara if (ptid) 564470decc6SDave Kleikamp *ptid = journal->j_committing_transaction->t_tid; 565470decc6SDave Kleikamp ret = 1; 566470decc6SDave Kleikamp } 567*a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 568470decc6SDave Kleikamp return ret; 569470decc6SDave Kleikamp } 570470decc6SDave Kleikamp 571470decc6SDave Kleikamp /* 572470decc6SDave Kleikamp * Wait for a specified commit to complete. 573470decc6SDave Kleikamp * The caller may not hold the journal lock. 574470decc6SDave Kleikamp */ 575f7f4bccbSMingming Cao int jbd2_log_wait_commit(journal_t *journal, tid_t tid) 576470decc6SDave Kleikamp { 577470decc6SDave Kleikamp int err = 0; 578470decc6SDave Kleikamp 579*a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 580e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 581470decc6SDave Kleikamp if (!tid_geq(journal->j_commit_request, tid)) { 582470decc6SDave Kleikamp printk(KERN_EMERG 583470decc6SDave Kleikamp "%s: error: j_commit_request=%d, tid=%d\n", 584329d291fSHarvey Harrison __func__, journal->j_commit_request, tid); 585470decc6SDave Kleikamp } 586470decc6SDave Kleikamp #endif 587470decc6SDave Kleikamp while (tid_gt(tid, journal->j_commit_sequence)) { 588470decc6SDave Kleikamp jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n", 589470decc6SDave Kleikamp tid, journal->j_commit_sequence); 590470decc6SDave Kleikamp wake_up(&journal->j_wait_commit); 591*a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 592470decc6SDave Kleikamp wait_event(journal->j_wait_done_commit, 593470decc6SDave Kleikamp !tid_gt(tid, journal->j_commit_sequence)); 594*a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 595470decc6SDave Kleikamp } 596*a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 597470decc6SDave Kleikamp 598470decc6SDave Kleikamp if (unlikely(is_journal_aborted(journal))) { 599470decc6SDave Kleikamp printk(KERN_EMERG "journal commit I/O error\n"); 600470decc6SDave Kleikamp err = -EIO; 601470decc6SDave Kleikamp } 602470decc6SDave Kleikamp return err; 603470decc6SDave Kleikamp } 604470decc6SDave Kleikamp 605470decc6SDave Kleikamp /* 606470decc6SDave Kleikamp * Log buffer allocation routines: 607470decc6SDave Kleikamp */ 608470decc6SDave Kleikamp 60918eba7aaSMingming Cao int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp) 610470decc6SDave Kleikamp { 611470decc6SDave Kleikamp unsigned long blocknr; 612470decc6SDave Kleikamp 613*a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 614470decc6SDave Kleikamp J_ASSERT(journal->j_free > 1); 615470decc6SDave Kleikamp 616470decc6SDave Kleikamp blocknr = journal->j_head; 617470decc6SDave Kleikamp journal->j_head++; 618470decc6SDave Kleikamp journal->j_free--; 619470decc6SDave Kleikamp if (journal->j_head == journal->j_last) 620470decc6SDave Kleikamp journal->j_head = journal->j_first; 621*a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 622f7f4bccbSMingming Cao return jbd2_journal_bmap(journal, blocknr, retp); 623470decc6SDave Kleikamp } 624470decc6SDave Kleikamp 625470decc6SDave Kleikamp /* 626470decc6SDave Kleikamp * Conversion of logical to physical block numbers for the journal 627470decc6SDave Kleikamp * 628470decc6SDave Kleikamp * On external journals the journal blocks are identity-mapped, so 629470decc6SDave Kleikamp * this is a no-op. If needed, we can use j_blk_offset - everything is 630470decc6SDave Kleikamp * ready. 631470decc6SDave Kleikamp */ 632f7f4bccbSMingming Cao int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr, 63318eba7aaSMingming Cao unsigned long long *retp) 634470decc6SDave Kleikamp { 635470decc6SDave Kleikamp int err = 0; 63618eba7aaSMingming Cao unsigned long long ret; 637470decc6SDave Kleikamp 638470decc6SDave Kleikamp if (journal->j_inode) { 639470decc6SDave Kleikamp ret = bmap(journal->j_inode, blocknr); 640470decc6SDave Kleikamp if (ret) 641470decc6SDave Kleikamp *retp = ret; 642470decc6SDave Kleikamp else { 643470decc6SDave Kleikamp printk(KERN_ALERT "%s: journal block not found " 644470decc6SDave Kleikamp "at offset %lu on %s\n", 64505496769STheodore Ts'o __func__, blocknr, journal->j_devname); 646470decc6SDave Kleikamp err = -EIO; 647470decc6SDave Kleikamp __journal_abort_soft(journal, err); 648470decc6SDave Kleikamp } 649470decc6SDave Kleikamp } else { 650470decc6SDave Kleikamp *retp = blocknr; /* +journal->j_blk_offset */ 651470decc6SDave Kleikamp } 652470decc6SDave Kleikamp return err; 653470decc6SDave Kleikamp } 654470decc6SDave Kleikamp 655470decc6SDave Kleikamp /* 656470decc6SDave Kleikamp * We play buffer_head aliasing tricks to write data/metadata blocks to 657470decc6SDave Kleikamp * the journal without copying their contents, but for journal 658470decc6SDave Kleikamp * descriptor blocks we do need to generate bona fide buffers. 659470decc6SDave Kleikamp * 660f7f4bccbSMingming Cao * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying 661470decc6SDave Kleikamp * the buffer's contents they really should run flush_dcache_page(bh->b_page). 662470decc6SDave Kleikamp * But we don't bother doing that, so there will be coherency problems with 663470decc6SDave Kleikamp * mmaps of blockdevs which hold live JBD-controlled filesystems. 664470decc6SDave Kleikamp */ 665f7f4bccbSMingming Cao struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal) 666470decc6SDave Kleikamp { 667470decc6SDave Kleikamp struct buffer_head *bh; 66818eba7aaSMingming Cao unsigned long long blocknr; 669470decc6SDave Kleikamp int err; 670470decc6SDave Kleikamp 671f7f4bccbSMingming Cao err = jbd2_journal_next_log_block(journal, &blocknr); 672470decc6SDave Kleikamp 673470decc6SDave Kleikamp if (err) 674470decc6SDave Kleikamp return NULL; 675470decc6SDave Kleikamp 676470decc6SDave Kleikamp bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize); 6774b905671SJan Kara if (!bh) 6784b905671SJan Kara return NULL; 679470decc6SDave Kleikamp lock_buffer(bh); 680470decc6SDave Kleikamp memset(bh->b_data, 0, journal->j_blocksize); 681470decc6SDave Kleikamp set_buffer_uptodate(bh); 682470decc6SDave Kleikamp unlock_buffer(bh); 683470decc6SDave Kleikamp BUFFER_TRACE(bh, "return this buffer"); 684f7f4bccbSMingming Cao return jbd2_journal_add_journal_head(bh); 685470decc6SDave Kleikamp } 686470decc6SDave Kleikamp 6878e85fb3fSJohann Lombardi struct jbd2_stats_proc_session { 6888e85fb3fSJohann Lombardi journal_t *journal; 6898e85fb3fSJohann Lombardi struct transaction_stats_s *stats; 6908e85fb3fSJohann Lombardi int start; 6918e85fb3fSJohann Lombardi int max; 6928e85fb3fSJohann Lombardi }; 6938e85fb3fSJohann Lombardi 6948e85fb3fSJohann Lombardi static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos) 6958e85fb3fSJohann Lombardi { 6968e85fb3fSJohann Lombardi return *pos ? NULL : SEQ_START_TOKEN; 6978e85fb3fSJohann Lombardi } 6988e85fb3fSJohann Lombardi 6998e85fb3fSJohann Lombardi static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos) 7008e85fb3fSJohann Lombardi { 7018e85fb3fSJohann Lombardi return NULL; 7028e85fb3fSJohann Lombardi } 7038e85fb3fSJohann Lombardi 7048e85fb3fSJohann Lombardi static int jbd2_seq_info_show(struct seq_file *seq, void *v) 7058e85fb3fSJohann Lombardi { 7068e85fb3fSJohann Lombardi struct jbd2_stats_proc_session *s = seq->private; 7078e85fb3fSJohann Lombardi 7088e85fb3fSJohann Lombardi if (v != SEQ_START_TOKEN) 7098e85fb3fSJohann Lombardi return 0; 7108e85fb3fSJohann Lombardi seq_printf(seq, "%lu transaction, each up to %u blocks\n", 7118e85fb3fSJohann Lombardi s->stats->ts_tid, 7128e85fb3fSJohann Lombardi s->journal->j_max_transaction_buffers); 7138e85fb3fSJohann Lombardi if (s->stats->ts_tid == 0) 7148e85fb3fSJohann Lombardi return 0; 7158e85fb3fSJohann Lombardi seq_printf(seq, "average: \n %ums waiting for transaction\n", 716bf699327STheodore Ts'o jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid)); 7178e85fb3fSJohann Lombardi seq_printf(seq, " %ums running transaction\n", 718bf699327STheodore Ts'o jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid)); 7198e85fb3fSJohann Lombardi seq_printf(seq, " %ums transaction was being locked\n", 720bf699327STheodore Ts'o jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid)); 7218e85fb3fSJohann Lombardi seq_printf(seq, " %ums flushing data (in ordered mode)\n", 722bf699327STheodore Ts'o jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid)); 7238e85fb3fSJohann Lombardi seq_printf(seq, " %ums logging transaction\n", 724bf699327STheodore Ts'o jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid)); 725c225aa57SSimon Holm Thøgersen seq_printf(seq, " %lluus average transaction commit time\n", 726c225aa57SSimon Holm Thøgersen div_u64(s->journal->j_average_commit_time, 1000)); 7278e85fb3fSJohann Lombardi seq_printf(seq, " %lu handles per transaction\n", 728bf699327STheodore Ts'o s->stats->run.rs_handle_count / s->stats->ts_tid); 7298e85fb3fSJohann Lombardi seq_printf(seq, " %lu blocks per transaction\n", 730bf699327STheodore Ts'o s->stats->run.rs_blocks / s->stats->ts_tid); 7318e85fb3fSJohann Lombardi seq_printf(seq, " %lu logged blocks per transaction\n", 732bf699327STheodore Ts'o s->stats->run.rs_blocks_logged / s->stats->ts_tid); 7338e85fb3fSJohann Lombardi return 0; 7348e85fb3fSJohann Lombardi } 7358e85fb3fSJohann Lombardi 7368e85fb3fSJohann Lombardi static void jbd2_seq_info_stop(struct seq_file *seq, void *v) 7378e85fb3fSJohann Lombardi { 7388e85fb3fSJohann Lombardi } 7398e85fb3fSJohann Lombardi 74088e9d34cSJames Morris static const struct seq_operations jbd2_seq_info_ops = { 7418e85fb3fSJohann Lombardi .start = jbd2_seq_info_start, 7428e85fb3fSJohann Lombardi .next = jbd2_seq_info_next, 7438e85fb3fSJohann Lombardi .stop = jbd2_seq_info_stop, 7448e85fb3fSJohann Lombardi .show = jbd2_seq_info_show, 7458e85fb3fSJohann Lombardi }; 7468e85fb3fSJohann Lombardi 7478e85fb3fSJohann Lombardi static int jbd2_seq_info_open(struct inode *inode, struct file *file) 7488e85fb3fSJohann Lombardi { 7498e85fb3fSJohann Lombardi journal_t *journal = PDE(inode)->data; 7508e85fb3fSJohann Lombardi struct jbd2_stats_proc_session *s; 7518e85fb3fSJohann Lombardi int rc, size; 7528e85fb3fSJohann Lombardi 7538e85fb3fSJohann Lombardi s = kmalloc(sizeof(*s), GFP_KERNEL); 7548e85fb3fSJohann Lombardi if (s == NULL) 7558e85fb3fSJohann Lombardi return -ENOMEM; 7568e85fb3fSJohann Lombardi size = sizeof(struct transaction_stats_s); 7578e85fb3fSJohann Lombardi s->stats = kmalloc(size, GFP_KERNEL); 7588e85fb3fSJohann Lombardi if (s->stats == NULL) { 7598e85fb3fSJohann Lombardi kfree(s); 7608e85fb3fSJohann Lombardi return -ENOMEM; 7618e85fb3fSJohann Lombardi } 7628e85fb3fSJohann Lombardi spin_lock(&journal->j_history_lock); 7638e85fb3fSJohann Lombardi memcpy(s->stats, &journal->j_stats, size); 7648e85fb3fSJohann Lombardi s->journal = journal; 7658e85fb3fSJohann Lombardi spin_unlock(&journal->j_history_lock); 7668e85fb3fSJohann Lombardi 7678e85fb3fSJohann Lombardi rc = seq_open(file, &jbd2_seq_info_ops); 7688e85fb3fSJohann Lombardi if (rc == 0) { 7698e85fb3fSJohann Lombardi struct seq_file *m = file->private_data; 7708e85fb3fSJohann Lombardi m->private = s; 7718e85fb3fSJohann Lombardi } else { 7728e85fb3fSJohann Lombardi kfree(s->stats); 7738e85fb3fSJohann Lombardi kfree(s); 7748e85fb3fSJohann Lombardi } 7758e85fb3fSJohann Lombardi return rc; 7768e85fb3fSJohann Lombardi 7778e85fb3fSJohann Lombardi } 7788e85fb3fSJohann Lombardi 7798e85fb3fSJohann Lombardi static int jbd2_seq_info_release(struct inode *inode, struct file *file) 7808e85fb3fSJohann Lombardi { 7818e85fb3fSJohann Lombardi struct seq_file *seq = file->private_data; 7828e85fb3fSJohann Lombardi struct jbd2_stats_proc_session *s = seq->private; 7838e85fb3fSJohann Lombardi kfree(s->stats); 7848e85fb3fSJohann Lombardi kfree(s); 7858e85fb3fSJohann Lombardi return seq_release(inode, file); 7868e85fb3fSJohann Lombardi } 7878e85fb3fSJohann Lombardi 788828c0950SAlexey Dobriyan static const struct file_operations jbd2_seq_info_fops = { 7898e85fb3fSJohann Lombardi .owner = THIS_MODULE, 7908e85fb3fSJohann Lombardi .open = jbd2_seq_info_open, 7918e85fb3fSJohann Lombardi .read = seq_read, 7928e85fb3fSJohann Lombardi .llseek = seq_lseek, 7938e85fb3fSJohann Lombardi .release = jbd2_seq_info_release, 7948e85fb3fSJohann Lombardi }; 7958e85fb3fSJohann Lombardi 7968e85fb3fSJohann Lombardi static struct proc_dir_entry *proc_jbd2_stats; 7978e85fb3fSJohann Lombardi 7988e85fb3fSJohann Lombardi static void jbd2_stats_proc_init(journal_t *journal) 7998e85fb3fSJohann Lombardi { 80005496769STheodore Ts'o journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats); 8018e85fb3fSJohann Lombardi if (journal->j_proc_entry) { 80279da3664SDenis V. Lunev proc_create_data("info", S_IRUGO, journal->j_proc_entry, 80379da3664SDenis V. Lunev &jbd2_seq_info_fops, journal); 8048e85fb3fSJohann Lombardi } 8058e85fb3fSJohann Lombardi } 8068e85fb3fSJohann Lombardi 8078e85fb3fSJohann Lombardi static void jbd2_stats_proc_exit(journal_t *journal) 8088e85fb3fSJohann Lombardi { 8098e85fb3fSJohann Lombardi remove_proc_entry("info", journal->j_proc_entry); 81005496769STheodore Ts'o remove_proc_entry(journal->j_devname, proc_jbd2_stats); 8118e85fb3fSJohann Lombardi } 8128e85fb3fSJohann Lombardi 813470decc6SDave Kleikamp /* 814470decc6SDave Kleikamp * Management for journal control blocks: functions to create and 815470decc6SDave Kleikamp * destroy journal_t structures, and to initialise and read existing 816470decc6SDave Kleikamp * journal blocks from disk. */ 817470decc6SDave Kleikamp 818470decc6SDave Kleikamp /* First: create and setup a journal_t object in memory. We initialise 819470decc6SDave Kleikamp * very few fields yet: that has to wait until we have created the 820470decc6SDave Kleikamp * journal structures from from scratch, or loaded them from disk. */ 821470decc6SDave Kleikamp 822470decc6SDave Kleikamp static journal_t * journal_init_common (void) 823470decc6SDave Kleikamp { 824470decc6SDave Kleikamp journal_t *journal; 825470decc6SDave Kleikamp int err; 826470decc6SDave Kleikamp 8273ebfdf88SAndrew Morton journal = kzalloc(sizeof(*journal), GFP_KERNEL); 828470decc6SDave Kleikamp if (!journal) 829470decc6SDave Kleikamp goto fail; 830470decc6SDave Kleikamp 831470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_transaction_locked); 832470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_logspace); 833470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_done_commit); 834470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_checkpoint); 835470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_commit); 836470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_updates); 837470decc6SDave Kleikamp mutex_init(&journal->j_barrier); 838470decc6SDave Kleikamp mutex_init(&journal->j_checkpoint_mutex); 839470decc6SDave Kleikamp spin_lock_init(&journal->j_revoke_lock); 840470decc6SDave Kleikamp spin_lock_init(&journal->j_list_lock); 841*a931da6aSTheodore Ts'o rwlock_init(&journal->j_state_lock); 842470decc6SDave Kleikamp 843cd02ff0bSMingming Cao journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE); 84430773840STheodore Ts'o journal->j_min_batch_time = 0; 84530773840STheodore Ts'o journal->j_max_batch_time = 15000; /* 15ms */ 846470decc6SDave Kleikamp 847470decc6SDave Kleikamp /* The journal is marked for error until we succeed with recovery! */ 848f7f4bccbSMingming Cao journal->j_flags = JBD2_ABORT; 849470decc6SDave Kleikamp 850470decc6SDave Kleikamp /* Set up a default-sized revoke table for the new mount. */ 851f7f4bccbSMingming Cao err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH); 852470decc6SDave Kleikamp if (err) { 853470decc6SDave Kleikamp kfree(journal); 854470decc6SDave Kleikamp goto fail; 855470decc6SDave Kleikamp } 8568e85fb3fSJohann Lombardi 857bf699327STheodore Ts'o spin_lock_init(&journal->j_history_lock); 8588e85fb3fSJohann Lombardi 859470decc6SDave Kleikamp return journal; 860470decc6SDave Kleikamp fail: 861470decc6SDave Kleikamp return NULL; 862470decc6SDave Kleikamp } 863470decc6SDave Kleikamp 864f7f4bccbSMingming Cao /* jbd2_journal_init_dev and jbd2_journal_init_inode: 865470decc6SDave Kleikamp * 866470decc6SDave Kleikamp * Create a journal structure assigned some fixed set of disk blocks to 867470decc6SDave Kleikamp * the journal. We don't actually touch those disk blocks yet, but we 868470decc6SDave Kleikamp * need to set up all of the mapping information to tell the journaling 869470decc6SDave Kleikamp * system where the journal blocks are. 870470decc6SDave Kleikamp * 871470decc6SDave Kleikamp */ 872470decc6SDave Kleikamp 873470decc6SDave Kleikamp /** 8745648ba5bSRandy Dunlap * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure 875470decc6SDave Kleikamp * @bdev: Block device on which to create the journal 876470decc6SDave Kleikamp * @fs_dev: Device which hold journalled filesystem for this journal. 877470decc6SDave Kleikamp * @start: Block nr Start of journal. 878470decc6SDave Kleikamp * @len: Length of the journal in blocks. 879470decc6SDave Kleikamp * @blocksize: blocksize of journalling device 8805648ba5bSRandy Dunlap * 8815648ba5bSRandy Dunlap * Returns: a newly created journal_t * 882470decc6SDave Kleikamp * 883f7f4bccbSMingming Cao * jbd2_journal_init_dev creates a journal which maps a fixed contiguous 884470decc6SDave Kleikamp * range of blocks on an arbitrary block device. 885470decc6SDave Kleikamp * 886470decc6SDave Kleikamp */ 887f7f4bccbSMingming Cao journal_t * jbd2_journal_init_dev(struct block_device *bdev, 888470decc6SDave Kleikamp struct block_device *fs_dev, 88918eba7aaSMingming Cao unsigned long long start, int len, int blocksize) 890470decc6SDave Kleikamp { 891470decc6SDave Kleikamp journal_t *journal = journal_init_common(); 892470decc6SDave Kleikamp struct buffer_head *bh; 89305496769STheodore Ts'o char *p; 894470decc6SDave Kleikamp int n; 895470decc6SDave Kleikamp 896470decc6SDave Kleikamp if (!journal) 897470decc6SDave Kleikamp return NULL; 898470decc6SDave Kleikamp 899470decc6SDave Kleikamp /* journal descriptor can store up to n blocks -bzzz */ 900470decc6SDave Kleikamp journal->j_blocksize = blocksize; 9014b905671SJan Kara jbd2_stats_proc_init(journal); 902470decc6SDave Kleikamp n = journal->j_blocksize / sizeof(journal_block_tag_t); 903470decc6SDave Kleikamp journal->j_wbufsize = n; 904470decc6SDave Kleikamp journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL); 905470decc6SDave Kleikamp if (!journal->j_wbuf) { 906470decc6SDave Kleikamp printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n", 907329d291fSHarvey Harrison __func__); 9084b905671SJan Kara goto out_err; 909470decc6SDave Kleikamp } 910470decc6SDave Kleikamp journal->j_dev = bdev; 911470decc6SDave Kleikamp journal->j_fs_dev = fs_dev; 912470decc6SDave Kleikamp journal->j_blk_offset = start; 913470decc6SDave Kleikamp journal->j_maxlen = len; 91405496769STheodore Ts'o bdevname(journal->j_dev, journal->j_devname); 91505496769STheodore Ts'o p = journal->j_devname; 91605496769STheodore Ts'o while ((p = strchr(p, '/'))) 91705496769STheodore Ts'o *p = '!'; 918470decc6SDave Kleikamp 919470decc6SDave Kleikamp bh = __getblk(journal->j_dev, start, journal->j_blocksize); 9204b905671SJan Kara if (!bh) { 9214b905671SJan Kara printk(KERN_ERR 9224b905671SJan Kara "%s: Cannot get buffer for journal superblock\n", 9234b905671SJan Kara __func__); 9244b905671SJan Kara goto out_err; 9254b905671SJan Kara } 926470decc6SDave Kleikamp journal->j_sb_buffer = bh; 927470decc6SDave Kleikamp journal->j_superblock = (journal_superblock_t *)bh->b_data; 9284b905671SJan Kara 929470decc6SDave Kleikamp return journal; 9304b905671SJan Kara out_err: 9317b02bec0STao Ma kfree(journal->j_wbuf); 9324b905671SJan Kara jbd2_stats_proc_exit(journal); 9334b905671SJan Kara kfree(journal); 9344b905671SJan Kara return NULL; 935470decc6SDave Kleikamp } 936470decc6SDave Kleikamp 937470decc6SDave Kleikamp /** 938f7f4bccbSMingming Cao * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode. 939470decc6SDave Kleikamp * @inode: An inode to create the journal in 940470decc6SDave Kleikamp * 941f7f4bccbSMingming Cao * jbd2_journal_init_inode creates a journal which maps an on-disk inode as 942470decc6SDave Kleikamp * the journal. The inode must exist already, must support bmap() and 943470decc6SDave Kleikamp * must have all data blocks preallocated. 944470decc6SDave Kleikamp */ 945f7f4bccbSMingming Cao journal_t * jbd2_journal_init_inode (struct inode *inode) 946470decc6SDave Kleikamp { 947470decc6SDave Kleikamp struct buffer_head *bh; 948470decc6SDave Kleikamp journal_t *journal = journal_init_common(); 94905496769STheodore Ts'o char *p; 950470decc6SDave Kleikamp int err; 951470decc6SDave Kleikamp int n; 95218eba7aaSMingming Cao unsigned long long blocknr; 953470decc6SDave Kleikamp 954470decc6SDave Kleikamp if (!journal) 955470decc6SDave Kleikamp return NULL; 956470decc6SDave Kleikamp 957470decc6SDave Kleikamp journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev; 958470decc6SDave Kleikamp journal->j_inode = inode; 95905496769STheodore Ts'o bdevname(journal->j_dev, journal->j_devname); 96005496769STheodore Ts'o p = journal->j_devname; 96105496769STheodore Ts'o while ((p = strchr(p, '/'))) 96205496769STheodore Ts'o *p = '!'; 96305496769STheodore Ts'o p = journal->j_devname + strlen(journal->j_devname); 96490576c0bSTheodore Ts'o sprintf(p, "-%lu", journal->j_inode->i_ino); 965470decc6SDave Kleikamp jbd_debug(1, 966470decc6SDave Kleikamp "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n", 967470decc6SDave Kleikamp journal, inode->i_sb->s_id, inode->i_ino, 968470decc6SDave Kleikamp (long long) inode->i_size, 969470decc6SDave Kleikamp inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize); 970470decc6SDave Kleikamp 971470decc6SDave Kleikamp journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits; 972470decc6SDave Kleikamp journal->j_blocksize = inode->i_sb->s_blocksize; 9738e85fb3fSJohann Lombardi jbd2_stats_proc_init(journal); 974470decc6SDave Kleikamp 975470decc6SDave Kleikamp /* journal descriptor can store up to n blocks -bzzz */ 976470decc6SDave Kleikamp n = journal->j_blocksize / sizeof(journal_block_tag_t); 977470decc6SDave Kleikamp journal->j_wbufsize = n; 978470decc6SDave Kleikamp journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL); 979470decc6SDave Kleikamp if (!journal->j_wbuf) { 980470decc6SDave Kleikamp printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n", 981329d291fSHarvey Harrison __func__); 9824b905671SJan Kara goto out_err; 983470decc6SDave Kleikamp } 984470decc6SDave Kleikamp 985f7f4bccbSMingming Cao err = jbd2_journal_bmap(journal, 0, &blocknr); 986470decc6SDave Kleikamp /* If that failed, give up */ 987470decc6SDave Kleikamp if (err) { 988470decc6SDave Kleikamp printk(KERN_ERR "%s: Cannnot locate journal superblock\n", 989329d291fSHarvey Harrison __func__); 9904b905671SJan Kara goto out_err; 991470decc6SDave Kleikamp } 992470decc6SDave Kleikamp 993470decc6SDave Kleikamp bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize); 9944b905671SJan Kara if (!bh) { 9954b905671SJan Kara printk(KERN_ERR 9964b905671SJan Kara "%s: Cannot get buffer for journal superblock\n", 9974b905671SJan Kara __func__); 9984b905671SJan Kara goto out_err; 9994b905671SJan Kara } 1000470decc6SDave Kleikamp journal->j_sb_buffer = bh; 1001470decc6SDave Kleikamp journal->j_superblock = (journal_superblock_t *)bh->b_data; 1002470decc6SDave Kleikamp 1003470decc6SDave Kleikamp return journal; 10044b905671SJan Kara out_err: 10057b02bec0STao Ma kfree(journal->j_wbuf); 10064b905671SJan Kara jbd2_stats_proc_exit(journal); 10074b905671SJan Kara kfree(journal); 10084b905671SJan Kara return NULL; 1009470decc6SDave Kleikamp } 1010470decc6SDave Kleikamp 1011470decc6SDave Kleikamp /* 1012470decc6SDave Kleikamp * If the journal init or create aborts, we need to mark the journal 1013470decc6SDave Kleikamp * superblock as being NULL to prevent the journal destroy from writing 1014470decc6SDave Kleikamp * back a bogus superblock. 1015470decc6SDave Kleikamp */ 1016470decc6SDave Kleikamp static void journal_fail_superblock (journal_t *journal) 1017470decc6SDave Kleikamp { 1018470decc6SDave Kleikamp struct buffer_head *bh = journal->j_sb_buffer; 1019470decc6SDave Kleikamp brelse(bh); 1020470decc6SDave Kleikamp journal->j_sb_buffer = NULL; 1021470decc6SDave Kleikamp } 1022470decc6SDave Kleikamp 1023470decc6SDave Kleikamp /* 1024470decc6SDave Kleikamp * Given a journal_t structure, initialise the various fields for 1025470decc6SDave Kleikamp * startup of a new journaling session. We use this both when creating 1026470decc6SDave Kleikamp * a journal, and after recovering an old journal to reset it for 1027470decc6SDave Kleikamp * subsequent use. 1028470decc6SDave Kleikamp */ 1029470decc6SDave Kleikamp 1030470decc6SDave Kleikamp static int journal_reset(journal_t *journal) 1031470decc6SDave Kleikamp { 1032470decc6SDave Kleikamp journal_superblock_t *sb = journal->j_superblock; 103318eba7aaSMingming Cao unsigned long long first, last; 1034470decc6SDave Kleikamp 1035470decc6SDave Kleikamp first = be32_to_cpu(sb->s_first); 1036470decc6SDave Kleikamp last = be32_to_cpu(sb->s_maxlen); 1037f6f50e28SJan Kara if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) { 1038f6f50e28SJan Kara printk(KERN_ERR "JBD: Journal too short (blocks %llu-%llu).\n", 1039f6f50e28SJan Kara first, last); 1040f6f50e28SJan Kara journal_fail_superblock(journal); 1041f6f50e28SJan Kara return -EINVAL; 1042f6f50e28SJan Kara } 1043470decc6SDave Kleikamp 1044470decc6SDave Kleikamp journal->j_first = first; 1045470decc6SDave Kleikamp journal->j_last = last; 1046470decc6SDave Kleikamp 1047470decc6SDave Kleikamp journal->j_head = first; 1048470decc6SDave Kleikamp journal->j_tail = first; 1049470decc6SDave Kleikamp journal->j_free = last - first; 1050470decc6SDave Kleikamp 1051470decc6SDave Kleikamp journal->j_tail_sequence = journal->j_transaction_sequence; 1052470decc6SDave Kleikamp journal->j_commit_sequence = journal->j_transaction_sequence - 1; 1053470decc6SDave Kleikamp journal->j_commit_request = journal->j_commit_sequence; 1054470decc6SDave Kleikamp 1055470decc6SDave Kleikamp journal->j_max_transaction_buffers = journal->j_maxlen / 4; 1056470decc6SDave Kleikamp 1057470decc6SDave Kleikamp /* Add the dynamic fields and write it to disk. */ 1058f7f4bccbSMingming Cao jbd2_journal_update_superblock(journal, 1); 105997f06784SPavel Emelianov return jbd2_journal_start_thread(journal); 1060470decc6SDave Kleikamp } 1061470decc6SDave Kleikamp 1062470decc6SDave Kleikamp /** 1063f7f4bccbSMingming Cao * void jbd2_journal_update_superblock() - Update journal sb on disk. 1064470decc6SDave Kleikamp * @journal: The journal to update. 1065470decc6SDave Kleikamp * @wait: Set to '0' if you don't want to wait for IO completion. 1066470decc6SDave Kleikamp * 1067470decc6SDave Kleikamp * Update a journal's dynamic superblock fields and write it to disk, 1068470decc6SDave Kleikamp * optionally waiting for the IO to complete. 1069470decc6SDave Kleikamp */ 1070f7f4bccbSMingming Cao void jbd2_journal_update_superblock(journal_t *journal, int wait) 1071470decc6SDave Kleikamp { 1072470decc6SDave Kleikamp journal_superblock_t *sb = journal->j_superblock; 1073470decc6SDave Kleikamp struct buffer_head *bh = journal->j_sb_buffer; 1074470decc6SDave Kleikamp 1075470decc6SDave Kleikamp /* 1076470decc6SDave Kleikamp * As a special case, if the on-disk copy is already marked as needing 1077470decc6SDave Kleikamp * no recovery (s_start == 0) and there are no outstanding transactions 1078470decc6SDave Kleikamp * in the filesystem, then we can safely defer the superblock update 1079f7f4bccbSMingming Cao * until the next commit by setting JBD2_FLUSHED. This avoids 1080470decc6SDave Kleikamp * attempting a write to a potential-readonly device. 1081470decc6SDave Kleikamp */ 1082470decc6SDave Kleikamp if (sb->s_start == 0 && journal->j_tail_sequence == 1083470decc6SDave Kleikamp journal->j_transaction_sequence) { 1084470decc6SDave Kleikamp jbd_debug(1,"JBD: Skipping superblock update on recovered sb " 1085470decc6SDave Kleikamp "(start %ld, seq %d, errno %d)\n", 1086470decc6SDave Kleikamp journal->j_tail, journal->j_tail_sequence, 1087470decc6SDave Kleikamp journal->j_errno); 1088470decc6SDave Kleikamp goto out; 1089470decc6SDave Kleikamp } 1090470decc6SDave Kleikamp 1091914258bfSTheodore Ts'o if (buffer_write_io_error(bh)) { 1092914258bfSTheodore Ts'o /* 1093914258bfSTheodore Ts'o * Oh, dear. A previous attempt to write the journal 1094914258bfSTheodore Ts'o * superblock failed. This could happen because the 1095914258bfSTheodore Ts'o * USB device was yanked out. Or it could happen to 1096914258bfSTheodore Ts'o * be a transient write error and maybe the block will 1097914258bfSTheodore Ts'o * be remapped. Nothing we can do but to retry the 1098914258bfSTheodore Ts'o * write and hope for the best. 1099914258bfSTheodore Ts'o */ 1100914258bfSTheodore Ts'o printk(KERN_ERR "JBD2: previous I/O error detected " 1101914258bfSTheodore Ts'o "for journal superblock update for %s.\n", 1102914258bfSTheodore Ts'o journal->j_devname); 1103914258bfSTheodore Ts'o clear_buffer_write_io_error(bh); 1104914258bfSTheodore Ts'o set_buffer_uptodate(bh); 1105914258bfSTheodore Ts'o } 1106914258bfSTheodore Ts'o 1107*a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 1108470decc6SDave Kleikamp jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n", 1109470decc6SDave Kleikamp journal->j_tail, journal->j_tail_sequence, journal->j_errno); 1110470decc6SDave Kleikamp 1111470decc6SDave Kleikamp sb->s_sequence = cpu_to_be32(journal->j_tail_sequence); 1112470decc6SDave Kleikamp sb->s_start = cpu_to_be32(journal->j_tail); 1113470decc6SDave Kleikamp sb->s_errno = cpu_to_be32(journal->j_errno); 1114*a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 1115470decc6SDave Kleikamp 1116470decc6SDave Kleikamp BUFFER_TRACE(bh, "marking dirty"); 1117470decc6SDave Kleikamp mark_buffer_dirty(bh); 1118914258bfSTheodore Ts'o if (wait) { 1119470decc6SDave Kleikamp sync_dirty_buffer(bh); 1120914258bfSTheodore Ts'o if (buffer_write_io_error(bh)) { 1121914258bfSTheodore Ts'o printk(KERN_ERR "JBD2: I/O error detected " 1122914258bfSTheodore Ts'o "when updating journal superblock for %s.\n", 1123914258bfSTheodore Ts'o journal->j_devname); 1124914258bfSTheodore Ts'o clear_buffer_write_io_error(bh); 1125914258bfSTheodore Ts'o set_buffer_uptodate(bh); 1126914258bfSTheodore Ts'o } 1127914258bfSTheodore Ts'o } else 1128470decc6SDave Kleikamp ll_rw_block(SWRITE, 1, &bh); 1129470decc6SDave Kleikamp 1130470decc6SDave Kleikamp out: 1131470decc6SDave Kleikamp /* If we have just flushed the log (by marking s_start==0), then 1132470decc6SDave Kleikamp * any future commit will have to be careful to update the 1133470decc6SDave Kleikamp * superblock again to re-record the true start of the log. */ 1134470decc6SDave Kleikamp 1135*a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 1136470decc6SDave Kleikamp if (sb->s_start) 1137f7f4bccbSMingming Cao journal->j_flags &= ~JBD2_FLUSHED; 1138470decc6SDave Kleikamp else 1139f7f4bccbSMingming Cao journal->j_flags |= JBD2_FLUSHED; 1140*a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 1141470decc6SDave Kleikamp } 1142470decc6SDave Kleikamp 1143470decc6SDave Kleikamp /* 1144470decc6SDave Kleikamp * Read the superblock for a given journal, performing initial 1145470decc6SDave Kleikamp * validation of the format. 1146470decc6SDave Kleikamp */ 1147470decc6SDave Kleikamp 1148470decc6SDave Kleikamp static int journal_get_superblock(journal_t *journal) 1149470decc6SDave Kleikamp { 1150470decc6SDave Kleikamp struct buffer_head *bh; 1151470decc6SDave Kleikamp journal_superblock_t *sb; 1152470decc6SDave Kleikamp int err = -EIO; 1153470decc6SDave Kleikamp 1154470decc6SDave Kleikamp bh = journal->j_sb_buffer; 1155470decc6SDave Kleikamp 1156470decc6SDave Kleikamp J_ASSERT(bh != NULL); 1157470decc6SDave Kleikamp if (!buffer_uptodate(bh)) { 1158470decc6SDave Kleikamp ll_rw_block(READ, 1, &bh); 1159470decc6SDave Kleikamp wait_on_buffer(bh); 1160470decc6SDave Kleikamp if (!buffer_uptodate(bh)) { 1161470decc6SDave Kleikamp printk (KERN_ERR 1162470decc6SDave Kleikamp "JBD: IO error reading journal superblock\n"); 1163470decc6SDave Kleikamp goto out; 1164470decc6SDave Kleikamp } 1165470decc6SDave Kleikamp } 1166470decc6SDave Kleikamp 1167470decc6SDave Kleikamp sb = journal->j_superblock; 1168470decc6SDave Kleikamp 1169470decc6SDave Kleikamp err = -EINVAL; 1170470decc6SDave Kleikamp 1171f7f4bccbSMingming Cao if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) || 1172470decc6SDave Kleikamp sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) { 1173470decc6SDave Kleikamp printk(KERN_WARNING "JBD: no valid journal superblock found\n"); 1174470decc6SDave Kleikamp goto out; 1175470decc6SDave Kleikamp } 1176470decc6SDave Kleikamp 1177470decc6SDave Kleikamp switch(be32_to_cpu(sb->s_header.h_blocktype)) { 1178f7f4bccbSMingming Cao case JBD2_SUPERBLOCK_V1: 1179470decc6SDave Kleikamp journal->j_format_version = 1; 1180470decc6SDave Kleikamp break; 1181f7f4bccbSMingming Cao case JBD2_SUPERBLOCK_V2: 1182470decc6SDave Kleikamp journal->j_format_version = 2; 1183470decc6SDave Kleikamp break; 1184470decc6SDave Kleikamp default: 1185470decc6SDave Kleikamp printk(KERN_WARNING "JBD: unrecognised superblock format ID\n"); 1186470decc6SDave Kleikamp goto out; 1187470decc6SDave Kleikamp } 1188470decc6SDave Kleikamp 1189470decc6SDave Kleikamp if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen) 1190470decc6SDave Kleikamp journal->j_maxlen = be32_to_cpu(sb->s_maxlen); 1191470decc6SDave Kleikamp else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) { 1192470decc6SDave Kleikamp printk (KERN_WARNING "JBD: journal file too short\n"); 1193470decc6SDave Kleikamp goto out; 1194470decc6SDave Kleikamp } 1195470decc6SDave Kleikamp 1196470decc6SDave Kleikamp return 0; 1197470decc6SDave Kleikamp 1198470decc6SDave Kleikamp out: 1199470decc6SDave Kleikamp journal_fail_superblock(journal); 1200470decc6SDave Kleikamp return err; 1201470decc6SDave Kleikamp } 1202470decc6SDave Kleikamp 1203470decc6SDave Kleikamp /* 1204470decc6SDave Kleikamp * Load the on-disk journal superblock and read the key fields into the 1205470decc6SDave Kleikamp * journal_t. 1206470decc6SDave Kleikamp */ 1207470decc6SDave Kleikamp 1208470decc6SDave Kleikamp static int load_superblock(journal_t *journal) 1209470decc6SDave Kleikamp { 1210470decc6SDave Kleikamp int err; 1211470decc6SDave Kleikamp journal_superblock_t *sb; 1212470decc6SDave Kleikamp 1213470decc6SDave Kleikamp err = journal_get_superblock(journal); 1214470decc6SDave Kleikamp if (err) 1215470decc6SDave Kleikamp return err; 1216470decc6SDave Kleikamp 1217470decc6SDave Kleikamp sb = journal->j_superblock; 1218470decc6SDave Kleikamp 1219470decc6SDave Kleikamp journal->j_tail_sequence = be32_to_cpu(sb->s_sequence); 1220470decc6SDave Kleikamp journal->j_tail = be32_to_cpu(sb->s_start); 1221470decc6SDave Kleikamp journal->j_first = be32_to_cpu(sb->s_first); 1222470decc6SDave Kleikamp journal->j_last = be32_to_cpu(sb->s_maxlen); 1223470decc6SDave Kleikamp journal->j_errno = be32_to_cpu(sb->s_errno); 1224470decc6SDave Kleikamp 1225470decc6SDave Kleikamp return 0; 1226470decc6SDave Kleikamp } 1227470decc6SDave Kleikamp 1228470decc6SDave Kleikamp 1229470decc6SDave Kleikamp /** 1230f7f4bccbSMingming Cao * int jbd2_journal_load() - Read journal from disk. 1231470decc6SDave Kleikamp * @journal: Journal to act on. 1232470decc6SDave Kleikamp * 1233470decc6SDave Kleikamp * Given a journal_t structure which tells us which disk blocks contain 1234470decc6SDave Kleikamp * a journal, read the journal from disk to initialise the in-memory 1235470decc6SDave Kleikamp * structures. 1236470decc6SDave Kleikamp */ 1237f7f4bccbSMingming Cao int jbd2_journal_load(journal_t *journal) 1238470decc6SDave Kleikamp { 1239470decc6SDave Kleikamp int err; 1240470decc6SDave Kleikamp journal_superblock_t *sb; 1241470decc6SDave Kleikamp 1242470decc6SDave Kleikamp err = load_superblock(journal); 1243470decc6SDave Kleikamp if (err) 1244470decc6SDave Kleikamp return err; 1245470decc6SDave Kleikamp 1246470decc6SDave Kleikamp sb = journal->j_superblock; 1247470decc6SDave Kleikamp /* If this is a V2 superblock, then we have to check the 1248470decc6SDave Kleikamp * features flags on it. */ 1249470decc6SDave Kleikamp 1250470decc6SDave Kleikamp if (journal->j_format_version >= 2) { 1251470decc6SDave Kleikamp if ((sb->s_feature_ro_compat & 1252f7f4bccbSMingming Cao ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) || 1253470decc6SDave Kleikamp (sb->s_feature_incompat & 1254f7f4bccbSMingming Cao ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) { 1255470decc6SDave Kleikamp printk (KERN_WARNING 1256470decc6SDave Kleikamp "JBD: Unrecognised features on journal\n"); 1257470decc6SDave Kleikamp return -EINVAL; 1258470decc6SDave Kleikamp } 1259470decc6SDave Kleikamp } 1260470decc6SDave Kleikamp 1261d2eecb03STheodore Ts'o /* 1262d2eecb03STheodore Ts'o * Create a slab for this blocksize 1263d2eecb03STheodore Ts'o */ 1264d2eecb03STheodore Ts'o err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize)); 1265d2eecb03STheodore Ts'o if (err) 1266d2eecb03STheodore Ts'o return err; 1267d2eecb03STheodore Ts'o 1268470decc6SDave Kleikamp /* Let the recovery code check whether it needs to recover any 1269470decc6SDave Kleikamp * data from the journal. */ 1270f7f4bccbSMingming Cao if (jbd2_journal_recover(journal)) 1271470decc6SDave Kleikamp goto recovery_error; 1272470decc6SDave Kleikamp 1273e6a47428STheodore Ts'o if (journal->j_failed_commit) { 1274e6a47428STheodore Ts'o printk(KERN_ERR "JBD2: journal transaction %u on %s " 1275e6a47428STheodore Ts'o "is corrupt.\n", journal->j_failed_commit, 1276e6a47428STheodore Ts'o journal->j_devname); 1277e6a47428STheodore Ts'o return -EIO; 1278e6a47428STheodore Ts'o } 1279e6a47428STheodore Ts'o 1280470decc6SDave Kleikamp /* OK, we've finished with the dynamic journal bits: 1281470decc6SDave Kleikamp * reinitialise the dynamic contents of the superblock in memory 1282470decc6SDave Kleikamp * and reset them on disk. */ 1283470decc6SDave Kleikamp if (journal_reset(journal)) 1284470decc6SDave Kleikamp goto recovery_error; 1285470decc6SDave Kleikamp 1286f7f4bccbSMingming Cao journal->j_flags &= ~JBD2_ABORT; 1287f7f4bccbSMingming Cao journal->j_flags |= JBD2_LOADED; 1288470decc6SDave Kleikamp return 0; 1289470decc6SDave Kleikamp 1290470decc6SDave Kleikamp recovery_error: 1291470decc6SDave Kleikamp printk (KERN_WARNING "JBD: recovery failed\n"); 1292470decc6SDave Kleikamp return -EIO; 1293470decc6SDave Kleikamp } 1294470decc6SDave Kleikamp 1295470decc6SDave Kleikamp /** 1296f7f4bccbSMingming Cao * void jbd2_journal_destroy() - Release a journal_t structure. 1297470decc6SDave Kleikamp * @journal: Journal to act on. 1298470decc6SDave Kleikamp * 1299470decc6SDave Kleikamp * Release a journal_t structure once it is no longer in use by the 1300470decc6SDave Kleikamp * journaled object. 130144519fafSHidehiro Kawai * Return <0 if we couldn't clean up the journal. 1302470decc6SDave Kleikamp */ 130344519fafSHidehiro Kawai int jbd2_journal_destroy(journal_t *journal) 1304470decc6SDave Kleikamp { 130544519fafSHidehiro Kawai int err = 0; 130644519fafSHidehiro Kawai 1307470decc6SDave Kleikamp /* Wait for the commit thread to wake up and die. */ 1308470decc6SDave Kleikamp journal_kill_thread(journal); 1309470decc6SDave Kleikamp 1310470decc6SDave Kleikamp /* Force a final log commit */ 1311470decc6SDave Kleikamp if (journal->j_running_transaction) 1312f7f4bccbSMingming Cao jbd2_journal_commit_transaction(journal); 1313470decc6SDave Kleikamp 1314470decc6SDave Kleikamp /* Force any old transactions to disk */ 1315470decc6SDave Kleikamp 1316470decc6SDave Kleikamp /* Totally anal locking here... */ 1317470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 1318470decc6SDave Kleikamp while (journal->j_checkpoint_transactions != NULL) { 1319470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 13201a0d3786STheodore Ts'o mutex_lock(&journal->j_checkpoint_mutex); 1321f7f4bccbSMingming Cao jbd2_log_do_checkpoint(journal); 13221a0d3786STheodore Ts'o mutex_unlock(&journal->j_checkpoint_mutex); 1323470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 1324470decc6SDave Kleikamp } 1325470decc6SDave Kleikamp 1326470decc6SDave Kleikamp J_ASSERT(journal->j_running_transaction == NULL); 1327470decc6SDave Kleikamp J_ASSERT(journal->j_committing_transaction == NULL); 1328470decc6SDave Kleikamp J_ASSERT(journal->j_checkpoint_transactions == NULL); 1329470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 1330470decc6SDave Kleikamp 133144519fafSHidehiro Kawai if (journal->j_sb_buffer) { 133244519fafSHidehiro Kawai if (!is_journal_aborted(journal)) { 1333470decc6SDave Kleikamp /* We can now mark the journal as empty. */ 1334470decc6SDave Kleikamp journal->j_tail = 0; 133544519fafSHidehiro Kawai journal->j_tail_sequence = 133644519fafSHidehiro Kawai ++journal->j_transaction_sequence; 1337f7f4bccbSMingming Cao jbd2_journal_update_superblock(journal, 1); 133844519fafSHidehiro Kawai } else { 133944519fafSHidehiro Kawai err = -EIO; 134044519fafSHidehiro Kawai } 1341470decc6SDave Kleikamp brelse(journal->j_sb_buffer); 1342470decc6SDave Kleikamp } 1343470decc6SDave Kleikamp 13448e85fb3fSJohann Lombardi if (journal->j_proc_entry) 13458e85fb3fSJohann Lombardi jbd2_stats_proc_exit(journal); 1346470decc6SDave Kleikamp if (journal->j_inode) 1347470decc6SDave Kleikamp iput(journal->j_inode); 1348470decc6SDave Kleikamp if (journal->j_revoke) 1349f7f4bccbSMingming Cao jbd2_journal_destroy_revoke(journal); 1350470decc6SDave Kleikamp kfree(journal->j_wbuf); 1351470decc6SDave Kleikamp kfree(journal); 135244519fafSHidehiro Kawai 135344519fafSHidehiro Kawai return err; 1354470decc6SDave Kleikamp } 1355470decc6SDave Kleikamp 1356470decc6SDave Kleikamp 1357470decc6SDave Kleikamp /** 1358f7f4bccbSMingming Cao *int jbd2_journal_check_used_features () - Check if features specified are used. 1359470decc6SDave Kleikamp * @journal: Journal to check. 1360470decc6SDave Kleikamp * @compat: bitmask of compatible features 1361470decc6SDave Kleikamp * @ro: bitmask of features that force read-only mount 1362470decc6SDave Kleikamp * @incompat: bitmask of incompatible features 1363470decc6SDave Kleikamp * 1364470decc6SDave Kleikamp * Check whether the journal uses all of a given set of 1365470decc6SDave Kleikamp * features. Return true (non-zero) if it does. 1366470decc6SDave Kleikamp **/ 1367470decc6SDave Kleikamp 1368f7f4bccbSMingming Cao int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat, 1369470decc6SDave Kleikamp unsigned long ro, unsigned long incompat) 1370470decc6SDave Kleikamp { 1371470decc6SDave Kleikamp journal_superblock_t *sb; 1372470decc6SDave Kleikamp 1373470decc6SDave Kleikamp if (!compat && !ro && !incompat) 1374470decc6SDave Kleikamp return 1; 1375470decc6SDave Kleikamp if (journal->j_format_version == 1) 1376470decc6SDave Kleikamp return 0; 1377470decc6SDave Kleikamp 1378470decc6SDave Kleikamp sb = journal->j_superblock; 1379470decc6SDave Kleikamp 1380470decc6SDave Kleikamp if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) && 1381470decc6SDave Kleikamp ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) && 1382470decc6SDave Kleikamp ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat)) 1383470decc6SDave Kleikamp return 1; 1384470decc6SDave Kleikamp 1385470decc6SDave Kleikamp return 0; 1386470decc6SDave Kleikamp } 1387470decc6SDave Kleikamp 1388470decc6SDave Kleikamp /** 1389f7f4bccbSMingming Cao * int jbd2_journal_check_available_features() - Check feature set in journalling layer 1390470decc6SDave Kleikamp * @journal: Journal to check. 1391470decc6SDave Kleikamp * @compat: bitmask of compatible features 1392470decc6SDave Kleikamp * @ro: bitmask of features that force read-only mount 1393470decc6SDave Kleikamp * @incompat: bitmask of incompatible features 1394470decc6SDave Kleikamp * 1395470decc6SDave Kleikamp * Check whether the journaling code supports the use of 1396470decc6SDave Kleikamp * all of a given set of features on this journal. Return true 1397470decc6SDave Kleikamp * (non-zero) if it can. */ 1398470decc6SDave Kleikamp 1399f7f4bccbSMingming Cao int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat, 1400470decc6SDave Kleikamp unsigned long ro, unsigned long incompat) 1401470decc6SDave Kleikamp { 1402470decc6SDave Kleikamp if (!compat && !ro && !incompat) 1403470decc6SDave Kleikamp return 1; 1404470decc6SDave Kleikamp 1405470decc6SDave Kleikamp /* We can support any known requested features iff the 1406470decc6SDave Kleikamp * superblock is in version 2. Otherwise we fail to support any 1407470decc6SDave Kleikamp * extended sb features. */ 1408470decc6SDave Kleikamp 1409470decc6SDave Kleikamp if (journal->j_format_version != 2) 1410470decc6SDave Kleikamp return 0; 1411470decc6SDave Kleikamp 1412f7f4bccbSMingming Cao if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat && 1413f7f4bccbSMingming Cao (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro && 1414f7f4bccbSMingming Cao (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat) 1415470decc6SDave Kleikamp return 1; 1416470decc6SDave Kleikamp 1417470decc6SDave Kleikamp return 0; 1418470decc6SDave Kleikamp } 1419470decc6SDave Kleikamp 1420470decc6SDave Kleikamp /** 1421f7f4bccbSMingming Cao * int jbd2_journal_set_features () - Mark a given journal feature in the superblock 1422470decc6SDave Kleikamp * @journal: Journal to act on. 1423470decc6SDave Kleikamp * @compat: bitmask of compatible features 1424470decc6SDave Kleikamp * @ro: bitmask of features that force read-only mount 1425470decc6SDave Kleikamp * @incompat: bitmask of incompatible features 1426470decc6SDave Kleikamp * 1427470decc6SDave Kleikamp * Mark a given journal feature as present on the 1428470decc6SDave Kleikamp * superblock. Returns true if the requested features could be set. 1429470decc6SDave Kleikamp * 1430470decc6SDave Kleikamp */ 1431470decc6SDave Kleikamp 1432f7f4bccbSMingming Cao int jbd2_journal_set_features (journal_t *journal, unsigned long compat, 1433470decc6SDave Kleikamp unsigned long ro, unsigned long incompat) 1434470decc6SDave Kleikamp { 1435470decc6SDave Kleikamp journal_superblock_t *sb; 1436470decc6SDave Kleikamp 1437f7f4bccbSMingming Cao if (jbd2_journal_check_used_features(journal, compat, ro, incompat)) 1438470decc6SDave Kleikamp return 1; 1439470decc6SDave Kleikamp 1440f7f4bccbSMingming Cao if (!jbd2_journal_check_available_features(journal, compat, ro, incompat)) 1441470decc6SDave Kleikamp return 0; 1442470decc6SDave Kleikamp 1443470decc6SDave Kleikamp jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n", 1444470decc6SDave Kleikamp compat, ro, incompat); 1445470decc6SDave Kleikamp 1446470decc6SDave Kleikamp sb = journal->j_superblock; 1447470decc6SDave Kleikamp 1448470decc6SDave Kleikamp sb->s_feature_compat |= cpu_to_be32(compat); 1449470decc6SDave Kleikamp sb->s_feature_ro_compat |= cpu_to_be32(ro); 1450470decc6SDave Kleikamp sb->s_feature_incompat |= cpu_to_be32(incompat); 1451470decc6SDave Kleikamp 1452470decc6SDave Kleikamp return 1; 1453470decc6SDave Kleikamp } 1454470decc6SDave Kleikamp 1455818d276cSGirish Shilamkar /* 1456818d276cSGirish Shilamkar * jbd2_journal_clear_features () - Clear a given journal feature in the 1457818d276cSGirish Shilamkar * superblock 1458818d276cSGirish Shilamkar * @journal: Journal to act on. 1459818d276cSGirish Shilamkar * @compat: bitmask of compatible features 1460818d276cSGirish Shilamkar * @ro: bitmask of features that force read-only mount 1461818d276cSGirish Shilamkar * @incompat: bitmask of incompatible features 1462818d276cSGirish Shilamkar * 1463818d276cSGirish Shilamkar * Clear a given journal feature as present on the 1464818d276cSGirish Shilamkar * superblock. 1465818d276cSGirish Shilamkar */ 1466818d276cSGirish Shilamkar void jbd2_journal_clear_features(journal_t *journal, unsigned long compat, 1467818d276cSGirish Shilamkar unsigned long ro, unsigned long incompat) 1468818d276cSGirish Shilamkar { 1469818d276cSGirish Shilamkar journal_superblock_t *sb; 1470818d276cSGirish Shilamkar 1471818d276cSGirish Shilamkar jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n", 1472818d276cSGirish Shilamkar compat, ro, incompat); 1473818d276cSGirish Shilamkar 1474818d276cSGirish Shilamkar sb = journal->j_superblock; 1475818d276cSGirish Shilamkar 1476818d276cSGirish Shilamkar sb->s_feature_compat &= ~cpu_to_be32(compat); 1477818d276cSGirish Shilamkar sb->s_feature_ro_compat &= ~cpu_to_be32(ro); 1478818d276cSGirish Shilamkar sb->s_feature_incompat &= ~cpu_to_be32(incompat); 1479818d276cSGirish Shilamkar } 1480818d276cSGirish Shilamkar EXPORT_SYMBOL(jbd2_journal_clear_features); 1481470decc6SDave Kleikamp 1482470decc6SDave Kleikamp /** 1483f7f4bccbSMingming Cao * int jbd2_journal_update_format () - Update on-disk journal structure. 1484470decc6SDave Kleikamp * @journal: Journal to act on. 1485470decc6SDave Kleikamp * 1486470decc6SDave Kleikamp * Given an initialised but unloaded journal struct, poke about in the 1487470decc6SDave Kleikamp * on-disk structure to update it to the most recent supported version. 1488470decc6SDave Kleikamp */ 1489f7f4bccbSMingming Cao int jbd2_journal_update_format (journal_t *journal) 1490470decc6SDave Kleikamp { 1491470decc6SDave Kleikamp journal_superblock_t *sb; 1492470decc6SDave Kleikamp int err; 1493470decc6SDave Kleikamp 1494470decc6SDave Kleikamp err = journal_get_superblock(journal); 1495470decc6SDave Kleikamp if (err) 1496470decc6SDave Kleikamp return err; 1497470decc6SDave Kleikamp 1498470decc6SDave Kleikamp sb = journal->j_superblock; 1499470decc6SDave Kleikamp 1500470decc6SDave Kleikamp switch (be32_to_cpu(sb->s_header.h_blocktype)) { 1501f7f4bccbSMingming Cao case JBD2_SUPERBLOCK_V2: 1502470decc6SDave Kleikamp return 0; 1503f7f4bccbSMingming Cao case JBD2_SUPERBLOCK_V1: 1504470decc6SDave Kleikamp return journal_convert_superblock_v1(journal, sb); 1505470decc6SDave Kleikamp default: 1506470decc6SDave Kleikamp break; 1507470decc6SDave Kleikamp } 1508470decc6SDave Kleikamp return -EINVAL; 1509470decc6SDave Kleikamp } 1510470decc6SDave Kleikamp 1511470decc6SDave Kleikamp static int journal_convert_superblock_v1(journal_t *journal, 1512470decc6SDave Kleikamp journal_superblock_t *sb) 1513470decc6SDave Kleikamp { 1514470decc6SDave Kleikamp int offset, blocksize; 1515470decc6SDave Kleikamp struct buffer_head *bh; 1516470decc6SDave Kleikamp 1517470decc6SDave Kleikamp printk(KERN_WARNING 1518470decc6SDave Kleikamp "JBD: Converting superblock from version 1 to 2.\n"); 1519470decc6SDave Kleikamp 1520470decc6SDave Kleikamp /* Pre-initialise new fields to zero */ 1521470decc6SDave Kleikamp offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb); 1522470decc6SDave Kleikamp blocksize = be32_to_cpu(sb->s_blocksize); 1523470decc6SDave Kleikamp memset(&sb->s_feature_compat, 0, blocksize-offset); 1524470decc6SDave Kleikamp 1525470decc6SDave Kleikamp sb->s_nr_users = cpu_to_be32(1); 1526f7f4bccbSMingming Cao sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2); 1527470decc6SDave Kleikamp journal->j_format_version = 2; 1528470decc6SDave Kleikamp 1529470decc6SDave Kleikamp bh = journal->j_sb_buffer; 1530470decc6SDave Kleikamp BUFFER_TRACE(bh, "marking dirty"); 1531470decc6SDave Kleikamp mark_buffer_dirty(bh); 1532470decc6SDave Kleikamp sync_dirty_buffer(bh); 1533470decc6SDave Kleikamp return 0; 1534470decc6SDave Kleikamp } 1535470decc6SDave Kleikamp 1536470decc6SDave Kleikamp 1537470decc6SDave Kleikamp /** 1538f7f4bccbSMingming Cao * int jbd2_journal_flush () - Flush journal 1539470decc6SDave Kleikamp * @journal: Journal to act on. 1540470decc6SDave Kleikamp * 1541470decc6SDave Kleikamp * Flush all data for a given journal to disk and empty the journal. 1542470decc6SDave Kleikamp * Filesystems can use this when remounting readonly to ensure that 1543470decc6SDave Kleikamp * recovery does not need to happen on remount. 1544470decc6SDave Kleikamp */ 1545470decc6SDave Kleikamp 1546f7f4bccbSMingming Cao int jbd2_journal_flush(journal_t *journal) 1547470decc6SDave Kleikamp { 1548470decc6SDave Kleikamp int err = 0; 1549470decc6SDave Kleikamp transaction_t *transaction = NULL; 1550470decc6SDave Kleikamp unsigned long old_tail; 1551470decc6SDave Kleikamp 1552*a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 1553470decc6SDave Kleikamp 1554470decc6SDave Kleikamp /* Force everything buffered to the log... */ 1555470decc6SDave Kleikamp if (journal->j_running_transaction) { 1556470decc6SDave Kleikamp transaction = journal->j_running_transaction; 1557f7f4bccbSMingming Cao __jbd2_log_start_commit(journal, transaction->t_tid); 1558470decc6SDave Kleikamp } else if (journal->j_committing_transaction) 1559470decc6SDave Kleikamp transaction = journal->j_committing_transaction; 1560470decc6SDave Kleikamp 1561470decc6SDave Kleikamp /* Wait for the log commit to complete... */ 1562470decc6SDave Kleikamp if (transaction) { 1563470decc6SDave Kleikamp tid_t tid = transaction->t_tid; 1564470decc6SDave Kleikamp 1565*a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 1566f7f4bccbSMingming Cao jbd2_log_wait_commit(journal, tid); 1567470decc6SDave Kleikamp } else { 1568*a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 1569470decc6SDave Kleikamp } 1570470decc6SDave Kleikamp 1571470decc6SDave Kleikamp /* ...and flush everything in the log out to disk. */ 1572470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 1573470decc6SDave Kleikamp while (!err && journal->j_checkpoint_transactions != NULL) { 1574470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 157544519fafSHidehiro Kawai mutex_lock(&journal->j_checkpoint_mutex); 1576f7f4bccbSMingming Cao err = jbd2_log_do_checkpoint(journal); 157744519fafSHidehiro Kawai mutex_unlock(&journal->j_checkpoint_mutex); 1578470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 1579470decc6SDave Kleikamp } 1580470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 158144519fafSHidehiro Kawai 158244519fafSHidehiro Kawai if (is_journal_aborted(journal)) 158344519fafSHidehiro Kawai return -EIO; 158444519fafSHidehiro Kawai 1585f7f4bccbSMingming Cao jbd2_cleanup_journal_tail(journal); 1586470decc6SDave Kleikamp 1587470decc6SDave Kleikamp /* Finally, mark the journal as really needing no recovery. 1588470decc6SDave Kleikamp * This sets s_start==0 in the underlying superblock, which is 1589470decc6SDave Kleikamp * the magic code for a fully-recovered superblock. Any future 1590470decc6SDave Kleikamp * commits of data to the journal will restore the current 1591470decc6SDave Kleikamp * s_start value. */ 1592*a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 1593470decc6SDave Kleikamp old_tail = journal->j_tail; 1594470decc6SDave Kleikamp journal->j_tail = 0; 1595*a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 1596f7f4bccbSMingming Cao jbd2_journal_update_superblock(journal, 1); 1597*a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 1598470decc6SDave Kleikamp journal->j_tail = old_tail; 1599470decc6SDave Kleikamp 1600470decc6SDave Kleikamp J_ASSERT(!journal->j_running_transaction); 1601470decc6SDave Kleikamp J_ASSERT(!journal->j_committing_transaction); 1602470decc6SDave Kleikamp J_ASSERT(!journal->j_checkpoint_transactions); 1603470decc6SDave Kleikamp J_ASSERT(journal->j_head == journal->j_tail); 1604470decc6SDave Kleikamp J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence); 1605*a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 160644519fafSHidehiro Kawai return 0; 1607470decc6SDave Kleikamp } 1608470decc6SDave Kleikamp 1609470decc6SDave Kleikamp /** 1610f7f4bccbSMingming Cao * int jbd2_journal_wipe() - Wipe journal contents 1611470decc6SDave Kleikamp * @journal: Journal to act on. 1612470decc6SDave Kleikamp * @write: flag (see below) 1613470decc6SDave Kleikamp * 1614470decc6SDave Kleikamp * Wipe out all of the contents of a journal, safely. This will produce 1615470decc6SDave Kleikamp * a warning if the journal contains any valid recovery information. 1616f7f4bccbSMingming Cao * Must be called between journal_init_*() and jbd2_journal_load(). 1617470decc6SDave Kleikamp * 1618470decc6SDave Kleikamp * If 'write' is non-zero, then we wipe out the journal on disk; otherwise 1619470decc6SDave Kleikamp * we merely suppress recovery. 1620470decc6SDave Kleikamp */ 1621470decc6SDave Kleikamp 1622f7f4bccbSMingming Cao int jbd2_journal_wipe(journal_t *journal, int write) 1623470decc6SDave Kleikamp { 1624470decc6SDave Kleikamp int err = 0; 1625470decc6SDave Kleikamp 1626f7f4bccbSMingming Cao J_ASSERT (!(journal->j_flags & JBD2_LOADED)); 1627470decc6SDave Kleikamp 1628470decc6SDave Kleikamp err = load_superblock(journal); 1629470decc6SDave Kleikamp if (err) 1630470decc6SDave Kleikamp return err; 1631470decc6SDave Kleikamp 1632470decc6SDave Kleikamp if (!journal->j_tail) 1633470decc6SDave Kleikamp goto no_recovery; 1634470decc6SDave Kleikamp 1635470decc6SDave Kleikamp printk (KERN_WARNING "JBD: %s recovery information on journal\n", 1636470decc6SDave Kleikamp write ? "Clearing" : "Ignoring"); 1637470decc6SDave Kleikamp 1638f7f4bccbSMingming Cao err = jbd2_journal_skip_recovery(journal); 1639470decc6SDave Kleikamp if (write) 1640f7f4bccbSMingming Cao jbd2_journal_update_superblock(journal, 1); 1641470decc6SDave Kleikamp 1642470decc6SDave Kleikamp no_recovery: 1643470decc6SDave Kleikamp return err; 1644470decc6SDave Kleikamp } 1645470decc6SDave Kleikamp 1646470decc6SDave Kleikamp /* 1647470decc6SDave Kleikamp * Journal abort has very specific semantics, which we describe 1648470decc6SDave Kleikamp * for journal abort. 1649470decc6SDave Kleikamp * 1650bfcd3555SAlberto Bertogli * Two internal functions, which provide abort to the jbd layer 1651470decc6SDave Kleikamp * itself are here. 1652470decc6SDave Kleikamp */ 1653470decc6SDave Kleikamp 1654470decc6SDave Kleikamp /* 1655470decc6SDave Kleikamp * Quick version for internal journal use (doesn't lock the journal). 1656470decc6SDave Kleikamp * Aborts hard --- we mark the abort as occurred, but do _nothing_ else, 1657470decc6SDave Kleikamp * and don't attempt to make any other journal updates. 1658470decc6SDave Kleikamp */ 1659f7f4bccbSMingming Cao void __jbd2_journal_abort_hard(journal_t *journal) 1660470decc6SDave Kleikamp { 1661470decc6SDave Kleikamp transaction_t *transaction; 1662470decc6SDave Kleikamp 1663f7f4bccbSMingming Cao if (journal->j_flags & JBD2_ABORT) 1664470decc6SDave Kleikamp return; 1665470decc6SDave Kleikamp 1666470decc6SDave Kleikamp printk(KERN_ERR "Aborting journal on device %s.\n", 166705496769STheodore Ts'o journal->j_devname); 1668470decc6SDave Kleikamp 1669*a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 1670f7f4bccbSMingming Cao journal->j_flags |= JBD2_ABORT; 1671470decc6SDave Kleikamp transaction = journal->j_running_transaction; 1672470decc6SDave Kleikamp if (transaction) 1673f7f4bccbSMingming Cao __jbd2_log_start_commit(journal, transaction->t_tid); 1674*a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 1675470decc6SDave Kleikamp } 1676470decc6SDave Kleikamp 1677470decc6SDave Kleikamp /* Soft abort: record the abort error status in the journal superblock, 1678470decc6SDave Kleikamp * but don't do any other IO. */ 1679470decc6SDave Kleikamp static void __journal_abort_soft (journal_t *journal, int errno) 1680470decc6SDave Kleikamp { 1681f7f4bccbSMingming Cao if (journal->j_flags & JBD2_ABORT) 1682470decc6SDave Kleikamp return; 1683470decc6SDave Kleikamp 1684470decc6SDave Kleikamp if (!journal->j_errno) 1685470decc6SDave Kleikamp journal->j_errno = errno; 1686470decc6SDave Kleikamp 1687f7f4bccbSMingming Cao __jbd2_journal_abort_hard(journal); 1688470decc6SDave Kleikamp 1689470decc6SDave Kleikamp if (errno) 1690f7f4bccbSMingming Cao jbd2_journal_update_superblock(journal, 1); 1691470decc6SDave Kleikamp } 1692470decc6SDave Kleikamp 1693470decc6SDave Kleikamp /** 1694f7f4bccbSMingming Cao * void jbd2_journal_abort () - Shutdown the journal immediately. 1695470decc6SDave Kleikamp * @journal: the journal to shutdown. 1696470decc6SDave Kleikamp * @errno: an error number to record in the journal indicating 1697470decc6SDave Kleikamp * the reason for the shutdown. 1698470decc6SDave Kleikamp * 1699470decc6SDave Kleikamp * Perform a complete, immediate shutdown of the ENTIRE 1700470decc6SDave Kleikamp * journal (not of a single transaction). This operation cannot be 1701470decc6SDave Kleikamp * undone without closing and reopening the journal. 1702470decc6SDave Kleikamp * 1703f7f4bccbSMingming Cao * The jbd2_journal_abort function is intended to support higher level error 1704470decc6SDave Kleikamp * recovery mechanisms such as the ext2/ext3 remount-readonly error 1705470decc6SDave Kleikamp * mode. 1706470decc6SDave Kleikamp * 1707470decc6SDave Kleikamp * Journal abort has very specific semantics. Any existing dirty, 1708470decc6SDave Kleikamp * unjournaled buffers in the main filesystem will still be written to 1709470decc6SDave Kleikamp * disk by bdflush, but the journaling mechanism will be suspended 1710470decc6SDave Kleikamp * immediately and no further transaction commits will be honoured. 1711470decc6SDave Kleikamp * 1712470decc6SDave Kleikamp * Any dirty, journaled buffers will be written back to disk without 1713470decc6SDave Kleikamp * hitting the journal. Atomicity cannot be guaranteed on an aborted 1714470decc6SDave Kleikamp * filesystem, but we _do_ attempt to leave as much data as possible 1715470decc6SDave Kleikamp * behind for fsck to use for cleanup. 1716470decc6SDave Kleikamp * 1717470decc6SDave Kleikamp * Any attempt to get a new transaction handle on a journal which is in 1718470decc6SDave Kleikamp * ABORT state will just result in an -EROFS error return. A 1719f7f4bccbSMingming Cao * jbd2_journal_stop on an existing handle will return -EIO if we have 1720470decc6SDave Kleikamp * entered abort state during the update. 1721470decc6SDave Kleikamp * 1722470decc6SDave Kleikamp * Recursive transactions are not disturbed by journal abort until the 1723f7f4bccbSMingming Cao * final jbd2_journal_stop, which will receive the -EIO error. 1724470decc6SDave Kleikamp * 1725f7f4bccbSMingming Cao * Finally, the jbd2_journal_abort call allows the caller to supply an errno 1726470decc6SDave Kleikamp * which will be recorded (if possible) in the journal superblock. This 1727470decc6SDave Kleikamp * allows a client to record failure conditions in the middle of a 1728470decc6SDave Kleikamp * transaction without having to complete the transaction to record the 1729470decc6SDave Kleikamp * failure to disk. ext3_error, for example, now uses this 1730470decc6SDave Kleikamp * functionality. 1731470decc6SDave Kleikamp * 1732470decc6SDave Kleikamp * Errors which originate from within the journaling layer will NOT 1733470decc6SDave Kleikamp * supply an errno; a null errno implies that absolutely no further 1734470decc6SDave Kleikamp * writes are done to the journal (unless there are any already in 1735470decc6SDave Kleikamp * progress). 1736470decc6SDave Kleikamp * 1737470decc6SDave Kleikamp */ 1738470decc6SDave Kleikamp 1739f7f4bccbSMingming Cao void jbd2_journal_abort(journal_t *journal, int errno) 1740470decc6SDave Kleikamp { 1741470decc6SDave Kleikamp __journal_abort_soft(journal, errno); 1742470decc6SDave Kleikamp } 1743470decc6SDave Kleikamp 1744470decc6SDave Kleikamp /** 1745f7f4bccbSMingming Cao * int jbd2_journal_errno () - returns the journal's error state. 1746470decc6SDave Kleikamp * @journal: journal to examine. 1747470decc6SDave Kleikamp * 1748bfcd3555SAlberto Bertogli * This is the errno number set with jbd2_journal_abort(), the last 1749470decc6SDave Kleikamp * time the journal was mounted - if the journal was stopped 1750470decc6SDave Kleikamp * without calling abort this will be 0. 1751470decc6SDave Kleikamp * 1752470decc6SDave Kleikamp * If the journal has been aborted on this mount time -EROFS will 1753470decc6SDave Kleikamp * be returned. 1754470decc6SDave Kleikamp */ 1755f7f4bccbSMingming Cao int jbd2_journal_errno(journal_t *journal) 1756470decc6SDave Kleikamp { 1757470decc6SDave Kleikamp int err; 1758470decc6SDave Kleikamp 1759*a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 1760f7f4bccbSMingming Cao if (journal->j_flags & JBD2_ABORT) 1761470decc6SDave Kleikamp err = -EROFS; 1762470decc6SDave Kleikamp else 1763470decc6SDave Kleikamp err = journal->j_errno; 1764*a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 1765470decc6SDave Kleikamp return err; 1766470decc6SDave Kleikamp } 1767470decc6SDave Kleikamp 1768470decc6SDave Kleikamp /** 1769f7f4bccbSMingming Cao * int jbd2_journal_clear_err () - clears the journal's error state 1770470decc6SDave Kleikamp * @journal: journal to act on. 1771470decc6SDave Kleikamp * 1772bfcd3555SAlberto Bertogli * An error must be cleared or acked to take a FS out of readonly 1773470decc6SDave Kleikamp * mode. 1774470decc6SDave Kleikamp */ 1775f7f4bccbSMingming Cao int jbd2_journal_clear_err(journal_t *journal) 1776470decc6SDave Kleikamp { 1777470decc6SDave Kleikamp int err = 0; 1778470decc6SDave Kleikamp 1779*a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 1780f7f4bccbSMingming Cao if (journal->j_flags & JBD2_ABORT) 1781470decc6SDave Kleikamp err = -EROFS; 1782470decc6SDave Kleikamp else 1783470decc6SDave Kleikamp journal->j_errno = 0; 1784*a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 1785470decc6SDave Kleikamp return err; 1786470decc6SDave Kleikamp } 1787470decc6SDave Kleikamp 1788470decc6SDave Kleikamp /** 1789f7f4bccbSMingming Cao * void jbd2_journal_ack_err() - Ack journal err. 1790470decc6SDave Kleikamp * @journal: journal to act on. 1791470decc6SDave Kleikamp * 1792bfcd3555SAlberto Bertogli * An error must be cleared or acked to take a FS out of readonly 1793470decc6SDave Kleikamp * mode. 1794470decc6SDave Kleikamp */ 1795f7f4bccbSMingming Cao void jbd2_journal_ack_err(journal_t *journal) 1796470decc6SDave Kleikamp { 1797*a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 1798470decc6SDave Kleikamp if (journal->j_errno) 1799f7f4bccbSMingming Cao journal->j_flags |= JBD2_ACK_ERR; 1800*a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 1801470decc6SDave Kleikamp } 1802470decc6SDave Kleikamp 1803f7f4bccbSMingming Cao int jbd2_journal_blocks_per_page(struct inode *inode) 1804470decc6SDave Kleikamp { 1805470decc6SDave Kleikamp return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); 1806470decc6SDave Kleikamp } 1807470decc6SDave Kleikamp 1808470decc6SDave Kleikamp /* 1809b517bea1SZach Brown * helper functions to deal with 32 or 64bit block numbers. 1810b517bea1SZach Brown */ 1811b517bea1SZach Brown size_t journal_tag_bytes(journal_t *journal) 1812b517bea1SZach Brown { 1813b517bea1SZach Brown if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT)) 1814cd02ff0bSMingming Cao return JBD2_TAG_SIZE64; 1815b517bea1SZach Brown else 1816cd02ff0bSMingming Cao return JBD2_TAG_SIZE32; 1817b517bea1SZach Brown } 1818b517bea1SZach Brown 1819b517bea1SZach Brown /* 1820d2eecb03STheodore Ts'o * JBD memory management 1821d2eecb03STheodore Ts'o * 1822d2eecb03STheodore Ts'o * These functions are used to allocate block-sized chunks of memory 1823d2eecb03STheodore Ts'o * used for making copies of buffer_head data. Very often it will be 1824d2eecb03STheodore Ts'o * page-sized chunks of data, but sometimes it will be in 1825d2eecb03STheodore Ts'o * sub-page-size chunks. (For example, 16k pages on Power systems 1826d2eecb03STheodore Ts'o * with a 4k block file system.) For blocks smaller than a page, we 1827d2eecb03STheodore Ts'o * use a SLAB allocator. There are slab caches for each block size, 1828d2eecb03STheodore Ts'o * which are allocated at mount time, if necessary, and we only free 1829d2eecb03STheodore Ts'o * (all of) the slab caches when/if the jbd2 module is unloaded. For 1830d2eecb03STheodore Ts'o * this reason we don't need to a mutex to protect access to 1831d2eecb03STheodore Ts'o * jbd2_slab[] allocating or releasing memory; only in 1832d2eecb03STheodore Ts'o * jbd2_journal_create_slab(). 1833d2eecb03STheodore Ts'o */ 1834d2eecb03STheodore Ts'o #define JBD2_MAX_SLABS 8 1835d2eecb03STheodore Ts'o static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS]; 1836d2eecb03STheodore Ts'o static DECLARE_MUTEX(jbd2_slab_create_sem); 1837d2eecb03STheodore Ts'o 1838d2eecb03STheodore Ts'o static const char *jbd2_slab_names[JBD2_MAX_SLABS] = { 1839d2eecb03STheodore Ts'o "jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k", 1840d2eecb03STheodore Ts'o "jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k" 1841d2eecb03STheodore Ts'o }; 1842d2eecb03STheodore Ts'o 1843d2eecb03STheodore Ts'o 1844d2eecb03STheodore Ts'o static void jbd2_journal_destroy_slabs(void) 1845d2eecb03STheodore Ts'o { 1846d2eecb03STheodore Ts'o int i; 1847d2eecb03STheodore Ts'o 1848d2eecb03STheodore Ts'o for (i = 0; i < JBD2_MAX_SLABS; i++) { 1849d2eecb03STheodore Ts'o if (jbd2_slab[i]) 1850d2eecb03STheodore Ts'o kmem_cache_destroy(jbd2_slab[i]); 1851d2eecb03STheodore Ts'o jbd2_slab[i] = NULL; 1852d2eecb03STheodore Ts'o } 1853d2eecb03STheodore Ts'o } 1854d2eecb03STheodore Ts'o 1855d2eecb03STheodore Ts'o static int jbd2_journal_create_slab(size_t size) 1856d2eecb03STheodore Ts'o { 1857d2eecb03STheodore Ts'o int i = order_base_2(size) - 10; 1858d2eecb03STheodore Ts'o size_t slab_size; 1859d2eecb03STheodore Ts'o 1860d2eecb03STheodore Ts'o if (size == PAGE_SIZE) 1861d2eecb03STheodore Ts'o return 0; 1862d2eecb03STheodore Ts'o 1863d2eecb03STheodore Ts'o if (i >= JBD2_MAX_SLABS) 1864d2eecb03STheodore Ts'o return -EINVAL; 1865d2eecb03STheodore Ts'o 1866d2eecb03STheodore Ts'o if (unlikely(i < 0)) 1867d2eecb03STheodore Ts'o i = 0; 1868d2eecb03STheodore Ts'o down(&jbd2_slab_create_sem); 1869d2eecb03STheodore Ts'o if (jbd2_slab[i]) { 1870d2eecb03STheodore Ts'o up(&jbd2_slab_create_sem); 1871d2eecb03STheodore Ts'o return 0; /* Already created */ 1872d2eecb03STheodore Ts'o } 1873d2eecb03STheodore Ts'o 1874d2eecb03STheodore Ts'o slab_size = 1 << (i+10); 1875d2eecb03STheodore Ts'o jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size, 1876d2eecb03STheodore Ts'o slab_size, 0, NULL); 1877d2eecb03STheodore Ts'o up(&jbd2_slab_create_sem); 1878d2eecb03STheodore Ts'o if (!jbd2_slab[i]) { 1879d2eecb03STheodore Ts'o printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n"); 1880d2eecb03STheodore Ts'o return -ENOMEM; 1881d2eecb03STheodore Ts'o } 1882d2eecb03STheodore Ts'o return 0; 1883d2eecb03STheodore Ts'o } 1884d2eecb03STheodore Ts'o 1885d2eecb03STheodore Ts'o static struct kmem_cache *get_slab(size_t size) 1886d2eecb03STheodore Ts'o { 1887d2eecb03STheodore Ts'o int i = order_base_2(size) - 10; 1888d2eecb03STheodore Ts'o 1889d2eecb03STheodore Ts'o BUG_ON(i >= JBD2_MAX_SLABS); 1890d2eecb03STheodore Ts'o if (unlikely(i < 0)) 1891d2eecb03STheodore Ts'o i = 0; 18928ac97b74SBill Pemberton BUG_ON(jbd2_slab[i] == NULL); 1893d2eecb03STheodore Ts'o return jbd2_slab[i]; 1894d2eecb03STheodore Ts'o } 1895d2eecb03STheodore Ts'o 1896d2eecb03STheodore Ts'o void *jbd2_alloc(size_t size, gfp_t flags) 1897d2eecb03STheodore Ts'o { 1898d2eecb03STheodore Ts'o void *ptr; 1899d2eecb03STheodore Ts'o 1900d2eecb03STheodore Ts'o BUG_ON(size & (size-1)); /* Must be a power of 2 */ 1901d2eecb03STheodore Ts'o 1902d2eecb03STheodore Ts'o flags |= __GFP_REPEAT; 1903d2eecb03STheodore Ts'o if (size == PAGE_SIZE) 1904d2eecb03STheodore Ts'o ptr = (void *)__get_free_pages(flags, 0); 1905d2eecb03STheodore Ts'o else if (size > PAGE_SIZE) { 1906d2eecb03STheodore Ts'o int order = get_order(size); 1907d2eecb03STheodore Ts'o 1908d2eecb03STheodore Ts'o if (order < 3) 1909d2eecb03STheodore Ts'o ptr = (void *)__get_free_pages(flags, order); 1910d2eecb03STheodore Ts'o else 1911d2eecb03STheodore Ts'o ptr = vmalloc(size); 1912d2eecb03STheodore Ts'o } else 1913d2eecb03STheodore Ts'o ptr = kmem_cache_alloc(get_slab(size), flags); 1914d2eecb03STheodore Ts'o 1915d2eecb03STheodore Ts'o /* Check alignment; SLUB has gotten this wrong in the past, 1916d2eecb03STheodore Ts'o * and this can lead to user data corruption! */ 1917d2eecb03STheodore Ts'o BUG_ON(((unsigned long) ptr) & (size-1)); 1918d2eecb03STheodore Ts'o 1919d2eecb03STheodore Ts'o return ptr; 1920d2eecb03STheodore Ts'o } 1921d2eecb03STheodore Ts'o 1922d2eecb03STheodore Ts'o void jbd2_free(void *ptr, size_t size) 1923d2eecb03STheodore Ts'o { 1924d2eecb03STheodore Ts'o if (size == PAGE_SIZE) { 1925d2eecb03STheodore Ts'o free_pages((unsigned long)ptr, 0); 1926d2eecb03STheodore Ts'o return; 1927d2eecb03STheodore Ts'o } 1928d2eecb03STheodore Ts'o if (size > PAGE_SIZE) { 1929d2eecb03STheodore Ts'o int order = get_order(size); 1930d2eecb03STheodore Ts'o 1931d2eecb03STheodore Ts'o if (order < 3) 1932d2eecb03STheodore Ts'o free_pages((unsigned long)ptr, order); 1933d2eecb03STheodore Ts'o else 1934d2eecb03STheodore Ts'o vfree(ptr); 1935d2eecb03STheodore Ts'o return; 1936d2eecb03STheodore Ts'o } 1937d2eecb03STheodore Ts'o kmem_cache_free(get_slab(size), ptr); 1938d2eecb03STheodore Ts'o }; 1939d2eecb03STheodore Ts'o 1940d2eecb03STheodore Ts'o /* 1941470decc6SDave Kleikamp * Journal_head storage management 1942470decc6SDave Kleikamp */ 1943e18b890bSChristoph Lameter static struct kmem_cache *jbd2_journal_head_cache; 1944e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 1945470decc6SDave Kleikamp static atomic_t nr_journal_heads = ATOMIC_INIT(0); 1946470decc6SDave Kleikamp #endif 1947470decc6SDave Kleikamp 1948f7f4bccbSMingming Cao static int journal_init_jbd2_journal_head_cache(void) 1949470decc6SDave Kleikamp { 1950470decc6SDave Kleikamp int retval; 1951470decc6SDave Kleikamp 19521076d17aSAl Viro J_ASSERT(jbd2_journal_head_cache == NULL); 1953a920e941SJohann Lombardi jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head", 1954470decc6SDave Kleikamp sizeof(struct journal_head), 1955470decc6SDave Kleikamp 0, /* offset */ 195677160957SMingming Cao SLAB_TEMPORARY, /* flags */ 195720c2df83SPaul Mundt NULL); /* ctor */ 1958470decc6SDave Kleikamp retval = 0; 19591076d17aSAl Viro if (!jbd2_journal_head_cache) { 1960470decc6SDave Kleikamp retval = -ENOMEM; 1961470decc6SDave Kleikamp printk(KERN_EMERG "JBD: no memory for journal_head cache\n"); 1962470decc6SDave Kleikamp } 1963470decc6SDave Kleikamp return retval; 1964470decc6SDave Kleikamp } 1965470decc6SDave Kleikamp 1966f7f4bccbSMingming Cao static void jbd2_journal_destroy_jbd2_journal_head_cache(void) 1967470decc6SDave Kleikamp { 19688a9362ebSDuane Griffin if (jbd2_journal_head_cache) { 1969f7f4bccbSMingming Cao kmem_cache_destroy(jbd2_journal_head_cache); 1970f7f4bccbSMingming Cao jbd2_journal_head_cache = NULL; 1971470decc6SDave Kleikamp } 19728a9362ebSDuane Griffin } 1973470decc6SDave Kleikamp 1974470decc6SDave Kleikamp /* 1975470decc6SDave Kleikamp * journal_head splicing and dicing 1976470decc6SDave Kleikamp */ 1977470decc6SDave Kleikamp static struct journal_head *journal_alloc_journal_head(void) 1978470decc6SDave Kleikamp { 1979470decc6SDave Kleikamp struct journal_head *ret; 1980470decc6SDave Kleikamp static unsigned long last_warning; 1981470decc6SDave Kleikamp 1982e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 1983470decc6SDave Kleikamp atomic_inc(&nr_journal_heads); 1984470decc6SDave Kleikamp #endif 1985f7f4bccbSMingming Cao ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS); 19861076d17aSAl Viro if (!ret) { 1987470decc6SDave Kleikamp jbd_debug(1, "out of memory for journal_head\n"); 1988470decc6SDave Kleikamp if (time_after(jiffies, last_warning + 5*HZ)) { 1989470decc6SDave Kleikamp printk(KERN_NOTICE "ENOMEM in %s, retrying.\n", 1990329d291fSHarvey Harrison __func__); 1991470decc6SDave Kleikamp last_warning = jiffies; 1992470decc6SDave Kleikamp } 19931076d17aSAl Viro while (!ret) { 1994470decc6SDave Kleikamp yield(); 1995f7f4bccbSMingming Cao ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS); 1996470decc6SDave Kleikamp } 1997470decc6SDave Kleikamp } 1998470decc6SDave Kleikamp return ret; 1999470decc6SDave Kleikamp } 2000470decc6SDave Kleikamp 2001470decc6SDave Kleikamp static void journal_free_journal_head(struct journal_head *jh) 2002470decc6SDave Kleikamp { 2003e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 2004470decc6SDave Kleikamp atomic_dec(&nr_journal_heads); 2005cd02ff0bSMingming Cao memset(jh, JBD2_POISON_FREE, sizeof(*jh)); 2006470decc6SDave Kleikamp #endif 2007f7f4bccbSMingming Cao kmem_cache_free(jbd2_journal_head_cache, jh); 2008470decc6SDave Kleikamp } 2009470decc6SDave Kleikamp 2010470decc6SDave Kleikamp /* 2011470decc6SDave Kleikamp * A journal_head is attached to a buffer_head whenever JBD has an 2012470decc6SDave Kleikamp * interest in the buffer. 2013470decc6SDave Kleikamp * 2014470decc6SDave Kleikamp * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit 2015470decc6SDave Kleikamp * is set. This bit is tested in core kernel code where we need to take 2016470decc6SDave Kleikamp * JBD-specific actions. Testing the zeroness of ->b_private is not reliable 2017470decc6SDave Kleikamp * there. 2018470decc6SDave Kleikamp * 2019470decc6SDave Kleikamp * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one. 2020470decc6SDave Kleikamp * 2021470decc6SDave Kleikamp * When a buffer has its BH_JBD bit set it is immune from being released by 2022470decc6SDave Kleikamp * core kernel code, mainly via ->b_count. 2023470decc6SDave Kleikamp * 2024470decc6SDave Kleikamp * A journal_head may be detached from its buffer_head when the journal_head's 2025470decc6SDave Kleikamp * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL. 2026f7f4bccbSMingming Cao * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the 2027470decc6SDave Kleikamp * journal_head can be dropped if needed. 2028470decc6SDave Kleikamp * 2029470decc6SDave Kleikamp * Various places in the kernel want to attach a journal_head to a buffer_head 2030470decc6SDave Kleikamp * _before_ attaching the journal_head to a transaction. To protect the 2031f7f4bccbSMingming Cao * journal_head in this situation, jbd2_journal_add_journal_head elevates the 2032470decc6SDave Kleikamp * journal_head's b_jcount refcount by one. The caller must call 2033f7f4bccbSMingming Cao * jbd2_journal_put_journal_head() to undo this. 2034470decc6SDave Kleikamp * 2035470decc6SDave Kleikamp * So the typical usage would be: 2036470decc6SDave Kleikamp * 2037470decc6SDave Kleikamp * (Attach a journal_head if needed. Increments b_jcount) 2038f7f4bccbSMingming Cao * struct journal_head *jh = jbd2_journal_add_journal_head(bh); 2039470decc6SDave Kleikamp * ... 2040470decc6SDave Kleikamp * jh->b_transaction = xxx; 2041f7f4bccbSMingming Cao * jbd2_journal_put_journal_head(jh); 2042470decc6SDave Kleikamp * 2043470decc6SDave Kleikamp * Now, the journal_head's b_jcount is zero, but it is safe from being released 2044470decc6SDave Kleikamp * because it has a non-zero b_transaction. 2045470decc6SDave Kleikamp */ 2046470decc6SDave Kleikamp 2047470decc6SDave Kleikamp /* 2048470decc6SDave Kleikamp * Give a buffer_head a journal_head. 2049470decc6SDave Kleikamp * 2050470decc6SDave Kleikamp * Doesn't need the journal lock. 2051470decc6SDave Kleikamp * May sleep. 2052470decc6SDave Kleikamp */ 2053f7f4bccbSMingming Cao struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh) 2054470decc6SDave Kleikamp { 2055470decc6SDave Kleikamp struct journal_head *jh; 2056470decc6SDave Kleikamp struct journal_head *new_jh = NULL; 2057470decc6SDave Kleikamp 2058470decc6SDave Kleikamp repeat: 2059470decc6SDave Kleikamp if (!buffer_jbd(bh)) { 2060470decc6SDave Kleikamp new_jh = journal_alloc_journal_head(); 2061470decc6SDave Kleikamp memset(new_jh, 0, sizeof(*new_jh)); 2062470decc6SDave Kleikamp } 2063470decc6SDave Kleikamp 2064470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 2065470decc6SDave Kleikamp if (buffer_jbd(bh)) { 2066470decc6SDave Kleikamp jh = bh2jh(bh); 2067470decc6SDave Kleikamp } else { 2068470decc6SDave Kleikamp J_ASSERT_BH(bh, 2069470decc6SDave Kleikamp (atomic_read(&bh->b_count) > 0) || 2070470decc6SDave Kleikamp (bh->b_page && bh->b_page->mapping)); 2071470decc6SDave Kleikamp 2072470decc6SDave Kleikamp if (!new_jh) { 2073470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 2074470decc6SDave Kleikamp goto repeat; 2075470decc6SDave Kleikamp } 2076470decc6SDave Kleikamp 2077470decc6SDave Kleikamp jh = new_jh; 2078470decc6SDave Kleikamp new_jh = NULL; /* We consumed it */ 2079470decc6SDave Kleikamp set_buffer_jbd(bh); 2080470decc6SDave Kleikamp bh->b_private = jh; 2081470decc6SDave Kleikamp jh->b_bh = bh; 2082470decc6SDave Kleikamp get_bh(bh); 2083470decc6SDave Kleikamp BUFFER_TRACE(bh, "added journal_head"); 2084470decc6SDave Kleikamp } 2085470decc6SDave Kleikamp jh->b_jcount++; 2086470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 2087470decc6SDave Kleikamp if (new_jh) 2088470decc6SDave Kleikamp journal_free_journal_head(new_jh); 2089470decc6SDave Kleikamp return bh->b_private; 2090470decc6SDave Kleikamp } 2091470decc6SDave Kleikamp 2092470decc6SDave Kleikamp /* 2093470decc6SDave Kleikamp * Grab a ref against this buffer_head's journal_head. If it ended up not 2094470decc6SDave Kleikamp * having a journal_head, return NULL 2095470decc6SDave Kleikamp */ 2096f7f4bccbSMingming Cao struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh) 2097470decc6SDave Kleikamp { 2098470decc6SDave Kleikamp struct journal_head *jh = NULL; 2099470decc6SDave Kleikamp 2100470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 2101470decc6SDave Kleikamp if (buffer_jbd(bh)) { 2102470decc6SDave Kleikamp jh = bh2jh(bh); 2103470decc6SDave Kleikamp jh->b_jcount++; 2104470decc6SDave Kleikamp } 2105470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 2106470decc6SDave Kleikamp return jh; 2107470decc6SDave Kleikamp } 2108470decc6SDave Kleikamp 2109470decc6SDave Kleikamp static void __journal_remove_journal_head(struct buffer_head *bh) 2110470decc6SDave Kleikamp { 2111470decc6SDave Kleikamp struct journal_head *jh = bh2jh(bh); 2112470decc6SDave Kleikamp 2113470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_jcount >= 0); 2114470decc6SDave Kleikamp 2115470decc6SDave Kleikamp get_bh(bh); 2116470decc6SDave Kleikamp if (jh->b_jcount == 0) { 2117470decc6SDave Kleikamp if (jh->b_transaction == NULL && 2118470decc6SDave Kleikamp jh->b_next_transaction == NULL && 2119470decc6SDave Kleikamp jh->b_cp_transaction == NULL) { 2120470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_jlist == BJ_None); 2121470decc6SDave Kleikamp J_ASSERT_BH(bh, buffer_jbd(bh)); 2122470decc6SDave Kleikamp J_ASSERT_BH(bh, jh2bh(jh) == bh); 2123470decc6SDave Kleikamp BUFFER_TRACE(bh, "remove journal_head"); 2124470decc6SDave Kleikamp if (jh->b_frozen_data) { 2125470decc6SDave Kleikamp printk(KERN_WARNING "%s: freeing " 2126470decc6SDave Kleikamp "b_frozen_data\n", 2127329d291fSHarvey Harrison __func__); 2128af1e76d6SMingming Cao jbd2_free(jh->b_frozen_data, bh->b_size); 2129470decc6SDave Kleikamp } 2130470decc6SDave Kleikamp if (jh->b_committed_data) { 2131470decc6SDave Kleikamp printk(KERN_WARNING "%s: freeing " 2132470decc6SDave Kleikamp "b_committed_data\n", 2133329d291fSHarvey Harrison __func__); 2134af1e76d6SMingming Cao jbd2_free(jh->b_committed_data, bh->b_size); 2135470decc6SDave Kleikamp } 2136470decc6SDave Kleikamp bh->b_private = NULL; 2137470decc6SDave Kleikamp jh->b_bh = NULL; /* debug, really */ 2138470decc6SDave Kleikamp clear_buffer_jbd(bh); 2139470decc6SDave Kleikamp __brelse(bh); 2140470decc6SDave Kleikamp journal_free_journal_head(jh); 2141470decc6SDave Kleikamp } else { 2142470decc6SDave Kleikamp BUFFER_TRACE(bh, "journal_head was locked"); 2143470decc6SDave Kleikamp } 2144470decc6SDave Kleikamp } 2145470decc6SDave Kleikamp } 2146470decc6SDave Kleikamp 2147470decc6SDave Kleikamp /* 2148f7f4bccbSMingming Cao * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction 2149470decc6SDave Kleikamp * and has a zero b_jcount then remove and release its journal_head. If we did 2150470decc6SDave Kleikamp * see that the buffer is not used by any transaction we also "logically" 2151470decc6SDave Kleikamp * decrement ->b_count. 2152470decc6SDave Kleikamp * 2153470decc6SDave Kleikamp * We in fact take an additional increment on ->b_count as a convenience, 2154470decc6SDave Kleikamp * because the caller usually wants to do additional things with the bh 2155470decc6SDave Kleikamp * after calling here. 2156f7f4bccbSMingming Cao * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some 2157470decc6SDave Kleikamp * time. Once the caller has run __brelse(), the buffer is eligible for 2158470decc6SDave Kleikamp * reaping by try_to_free_buffers(). 2159470decc6SDave Kleikamp */ 2160f7f4bccbSMingming Cao void jbd2_journal_remove_journal_head(struct buffer_head *bh) 2161470decc6SDave Kleikamp { 2162470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 2163470decc6SDave Kleikamp __journal_remove_journal_head(bh); 2164470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 2165470decc6SDave Kleikamp } 2166470decc6SDave Kleikamp 2167470decc6SDave Kleikamp /* 2168470decc6SDave Kleikamp * Drop a reference on the passed journal_head. If it fell to zero then try to 2169470decc6SDave Kleikamp * release the journal_head from the buffer_head. 2170470decc6SDave Kleikamp */ 2171f7f4bccbSMingming Cao void jbd2_journal_put_journal_head(struct journal_head *jh) 2172470decc6SDave Kleikamp { 2173470decc6SDave Kleikamp struct buffer_head *bh = jh2bh(jh); 2174470decc6SDave Kleikamp 2175470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 2176470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_jcount > 0); 2177470decc6SDave Kleikamp --jh->b_jcount; 2178470decc6SDave Kleikamp if (!jh->b_jcount && !jh->b_transaction) { 2179470decc6SDave Kleikamp __journal_remove_journal_head(bh); 2180470decc6SDave Kleikamp __brelse(bh); 2181470decc6SDave Kleikamp } 2182470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 2183470decc6SDave Kleikamp } 2184470decc6SDave Kleikamp 2185470decc6SDave Kleikamp /* 2186c851ed54SJan Kara * Initialize jbd inode head 2187c851ed54SJan Kara */ 2188c851ed54SJan Kara void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode) 2189c851ed54SJan Kara { 2190c851ed54SJan Kara jinode->i_transaction = NULL; 2191c851ed54SJan Kara jinode->i_next_transaction = NULL; 2192c851ed54SJan Kara jinode->i_vfs_inode = inode; 2193c851ed54SJan Kara jinode->i_flags = 0; 2194c851ed54SJan Kara INIT_LIST_HEAD(&jinode->i_list); 2195c851ed54SJan Kara } 2196c851ed54SJan Kara 2197c851ed54SJan Kara /* 2198c851ed54SJan Kara * Function to be called before we start removing inode from memory (i.e., 2199c851ed54SJan Kara * clear_inode() is a fine place to be called from). It removes inode from 2200c851ed54SJan Kara * transaction's lists. 2201c851ed54SJan Kara */ 2202c851ed54SJan Kara void jbd2_journal_release_jbd_inode(journal_t *journal, 2203c851ed54SJan Kara struct jbd2_inode *jinode) 2204c851ed54SJan Kara { 2205c851ed54SJan Kara if (!journal) 2206c851ed54SJan Kara return; 2207c851ed54SJan Kara restart: 2208c851ed54SJan Kara spin_lock(&journal->j_list_lock); 2209c851ed54SJan Kara /* Is commit writing out inode - we have to wait */ 2210c851ed54SJan Kara if (jinode->i_flags & JI_COMMIT_RUNNING) { 2211c851ed54SJan Kara wait_queue_head_t *wq; 2212c851ed54SJan Kara DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING); 2213c851ed54SJan Kara wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING); 2214c851ed54SJan Kara prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE); 2215c851ed54SJan Kara spin_unlock(&journal->j_list_lock); 2216c851ed54SJan Kara schedule(); 2217c851ed54SJan Kara finish_wait(wq, &wait.wait); 2218c851ed54SJan Kara goto restart; 2219c851ed54SJan Kara } 2220c851ed54SJan Kara 2221c851ed54SJan Kara if (jinode->i_transaction) { 2222c851ed54SJan Kara list_del(&jinode->i_list); 2223c851ed54SJan Kara jinode->i_transaction = NULL; 2224c851ed54SJan Kara } 2225c851ed54SJan Kara spin_unlock(&journal->j_list_lock); 2226c851ed54SJan Kara } 2227c851ed54SJan Kara 2228c851ed54SJan Kara /* 22290f49d5d0SJose R. Santos * debugfs tunables 2230470decc6SDave Kleikamp */ 22316f38c74fSJose R. Santos #ifdef CONFIG_JBD2_DEBUG 22326f38c74fSJose R. Santos u8 jbd2_journal_enable_debug __read_mostly; 2233f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_enable_debug); 2234470decc6SDave Kleikamp 22350f49d5d0SJose R. Santos #define JBD2_DEBUG_NAME "jbd2-debug" 2236470decc6SDave Kleikamp 22376f38c74fSJose R. Santos static struct dentry *jbd2_debugfs_dir; 22386f38c74fSJose R. Santos static struct dentry *jbd2_debug; 22390f49d5d0SJose R. Santos 22400f49d5d0SJose R. Santos static void __init jbd2_create_debugfs_entry(void) 2241470decc6SDave Kleikamp { 22420f49d5d0SJose R. Santos jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL); 22430f49d5d0SJose R. Santos if (jbd2_debugfs_dir) 2244765f8361SYin Kangkai jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME, 2245765f8361SYin Kangkai S_IRUGO | S_IWUSR, 22460f49d5d0SJose R. Santos jbd2_debugfs_dir, 22470f49d5d0SJose R. Santos &jbd2_journal_enable_debug); 2248470decc6SDave Kleikamp } 2249470decc6SDave Kleikamp 22500f49d5d0SJose R. Santos static void __exit jbd2_remove_debugfs_entry(void) 2251470decc6SDave Kleikamp { 22520f49d5d0SJose R. Santos debugfs_remove(jbd2_debug); 22530f49d5d0SJose R. Santos debugfs_remove(jbd2_debugfs_dir); 2254470decc6SDave Kleikamp } 2255470decc6SDave Kleikamp 2256470decc6SDave Kleikamp #else 2257470decc6SDave Kleikamp 22580f49d5d0SJose R. Santos static void __init jbd2_create_debugfs_entry(void) 22590f49d5d0SJose R. Santos { 22600f49d5d0SJose R. Santos } 22610f49d5d0SJose R. Santos 22620f49d5d0SJose R. Santos static void __exit jbd2_remove_debugfs_entry(void) 22630f49d5d0SJose R. Santos { 22640f49d5d0SJose R. Santos } 2265470decc6SDave Kleikamp 2266470decc6SDave Kleikamp #endif 2267470decc6SDave Kleikamp 22688e85fb3fSJohann Lombardi #ifdef CONFIG_PROC_FS 22698e85fb3fSJohann Lombardi 22708e85fb3fSJohann Lombardi #define JBD2_STATS_PROC_NAME "fs/jbd2" 22718e85fb3fSJohann Lombardi 22728e85fb3fSJohann Lombardi static void __init jbd2_create_jbd_stats_proc_entry(void) 22738e85fb3fSJohann Lombardi { 22748e85fb3fSJohann Lombardi proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL); 22758e85fb3fSJohann Lombardi } 22768e85fb3fSJohann Lombardi 22778e85fb3fSJohann Lombardi static void __exit jbd2_remove_jbd_stats_proc_entry(void) 22788e85fb3fSJohann Lombardi { 22798e85fb3fSJohann Lombardi if (proc_jbd2_stats) 22808e85fb3fSJohann Lombardi remove_proc_entry(JBD2_STATS_PROC_NAME, NULL); 22818e85fb3fSJohann Lombardi } 22828e85fb3fSJohann Lombardi 22838e85fb3fSJohann Lombardi #else 22848e85fb3fSJohann Lombardi 22858e85fb3fSJohann Lombardi #define jbd2_create_jbd_stats_proc_entry() do {} while (0) 22868e85fb3fSJohann Lombardi #define jbd2_remove_jbd_stats_proc_entry() do {} while (0) 22878e85fb3fSJohann Lombardi 22888e85fb3fSJohann Lombardi #endif 22898e85fb3fSJohann Lombardi 2290e18b890bSChristoph Lameter struct kmem_cache *jbd2_handle_cache; 2291470decc6SDave Kleikamp 2292470decc6SDave Kleikamp static int __init journal_init_handle_cache(void) 2293470decc6SDave Kleikamp { 2294a920e941SJohann Lombardi jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle", 2295470decc6SDave Kleikamp sizeof(handle_t), 2296470decc6SDave Kleikamp 0, /* offset */ 229777160957SMingming Cao SLAB_TEMPORARY, /* flags */ 229820c2df83SPaul Mundt NULL); /* ctor */ 2299f7f4bccbSMingming Cao if (jbd2_handle_cache == NULL) { 2300470decc6SDave Kleikamp printk(KERN_EMERG "JBD: failed to create handle cache\n"); 2301470decc6SDave Kleikamp return -ENOMEM; 2302470decc6SDave Kleikamp } 2303470decc6SDave Kleikamp return 0; 2304470decc6SDave Kleikamp } 2305470decc6SDave Kleikamp 2306f7f4bccbSMingming Cao static void jbd2_journal_destroy_handle_cache(void) 2307470decc6SDave Kleikamp { 2308f7f4bccbSMingming Cao if (jbd2_handle_cache) 2309f7f4bccbSMingming Cao kmem_cache_destroy(jbd2_handle_cache); 2310470decc6SDave Kleikamp } 2311470decc6SDave Kleikamp 2312470decc6SDave Kleikamp /* 2313470decc6SDave Kleikamp * Module startup and shutdown 2314470decc6SDave Kleikamp */ 2315470decc6SDave Kleikamp 2316470decc6SDave Kleikamp static int __init journal_init_caches(void) 2317470decc6SDave Kleikamp { 2318470decc6SDave Kleikamp int ret; 2319470decc6SDave Kleikamp 2320f7f4bccbSMingming Cao ret = jbd2_journal_init_revoke_caches(); 2321470decc6SDave Kleikamp if (ret == 0) 2322f7f4bccbSMingming Cao ret = journal_init_jbd2_journal_head_cache(); 2323470decc6SDave Kleikamp if (ret == 0) 2324470decc6SDave Kleikamp ret = journal_init_handle_cache(); 2325470decc6SDave Kleikamp return ret; 2326470decc6SDave Kleikamp } 2327470decc6SDave Kleikamp 2328f7f4bccbSMingming Cao static void jbd2_journal_destroy_caches(void) 2329470decc6SDave Kleikamp { 2330f7f4bccbSMingming Cao jbd2_journal_destroy_revoke_caches(); 2331f7f4bccbSMingming Cao jbd2_journal_destroy_jbd2_journal_head_cache(); 2332f7f4bccbSMingming Cao jbd2_journal_destroy_handle_cache(); 2333d2eecb03STheodore Ts'o jbd2_journal_destroy_slabs(); 2334470decc6SDave Kleikamp } 2335470decc6SDave Kleikamp 2336470decc6SDave Kleikamp static int __init journal_init(void) 2337470decc6SDave Kleikamp { 2338470decc6SDave Kleikamp int ret; 2339470decc6SDave Kleikamp 2340470decc6SDave Kleikamp BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024); 2341470decc6SDave Kleikamp 2342470decc6SDave Kleikamp ret = journal_init_caches(); 2343620de4e1SDuane Griffin if (ret == 0) { 23440f49d5d0SJose R. Santos jbd2_create_debugfs_entry(); 23458e85fb3fSJohann Lombardi jbd2_create_jbd_stats_proc_entry(); 2346620de4e1SDuane Griffin } else { 2347620de4e1SDuane Griffin jbd2_journal_destroy_caches(); 2348620de4e1SDuane Griffin } 2349470decc6SDave Kleikamp return ret; 2350470decc6SDave Kleikamp } 2351470decc6SDave Kleikamp 2352470decc6SDave Kleikamp static void __exit journal_exit(void) 2353470decc6SDave Kleikamp { 2354e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 2355470decc6SDave Kleikamp int n = atomic_read(&nr_journal_heads); 2356470decc6SDave Kleikamp if (n) 2357470decc6SDave Kleikamp printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n); 2358470decc6SDave Kleikamp #endif 23590f49d5d0SJose R. Santos jbd2_remove_debugfs_entry(); 23608e85fb3fSJohann Lombardi jbd2_remove_jbd_stats_proc_entry(); 2361f7f4bccbSMingming Cao jbd2_journal_destroy_caches(); 2362470decc6SDave Kleikamp } 2363470decc6SDave Kleikamp 2364879c5e6bSTheodore Ts'o /* 2365879c5e6bSTheodore Ts'o * jbd2_dev_to_name is a utility function used by the jbd2 and ext4 2366879c5e6bSTheodore Ts'o * tracing infrastructure to map a dev_t to a device name. 2367879c5e6bSTheodore Ts'o * 2368879c5e6bSTheodore Ts'o * The caller should use rcu_read_lock() in order to make sure the 2369879c5e6bSTheodore Ts'o * device name stays valid until its done with it. We use 2370879c5e6bSTheodore Ts'o * rcu_read_lock() as well to make sure we're safe in case the caller 2371879c5e6bSTheodore Ts'o * gets sloppy, and because rcu_read_lock() is cheap and can be safely 2372879c5e6bSTheodore Ts'o * nested. 2373879c5e6bSTheodore Ts'o */ 2374879c5e6bSTheodore Ts'o struct devname_cache { 2375879c5e6bSTheodore Ts'o struct rcu_head rcu; 2376879c5e6bSTheodore Ts'o dev_t device; 2377879c5e6bSTheodore Ts'o char devname[BDEVNAME_SIZE]; 2378879c5e6bSTheodore Ts'o }; 2379879c5e6bSTheodore Ts'o #define CACHE_SIZE_BITS 6 2380879c5e6bSTheodore Ts'o static struct devname_cache *devcache[1 << CACHE_SIZE_BITS]; 2381879c5e6bSTheodore Ts'o static DEFINE_SPINLOCK(devname_cache_lock); 2382879c5e6bSTheodore Ts'o 2383879c5e6bSTheodore Ts'o static void free_devcache(struct rcu_head *rcu) 2384879c5e6bSTheodore Ts'o { 2385879c5e6bSTheodore Ts'o kfree(rcu); 2386879c5e6bSTheodore Ts'o } 2387879c5e6bSTheodore Ts'o 2388879c5e6bSTheodore Ts'o const char *jbd2_dev_to_name(dev_t device) 2389879c5e6bSTheodore Ts'o { 2390879c5e6bSTheodore Ts'o int i = hash_32(device, CACHE_SIZE_BITS); 2391879c5e6bSTheodore Ts'o char *ret; 2392879c5e6bSTheodore Ts'o struct block_device *bd; 2393b5744805STheodore Ts'o static struct devname_cache *new_dev; 2394879c5e6bSTheodore Ts'o 2395879c5e6bSTheodore Ts'o rcu_read_lock(); 2396879c5e6bSTheodore Ts'o if (devcache[i] && devcache[i]->device == device) { 2397879c5e6bSTheodore Ts'o ret = devcache[i]->devname; 2398879c5e6bSTheodore Ts'o rcu_read_unlock(); 2399879c5e6bSTheodore Ts'o return ret; 2400879c5e6bSTheodore Ts'o } 2401879c5e6bSTheodore Ts'o rcu_read_unlock(); 2402879c5e6bSTheodore Ts'o 2403b5744805STheodore Ts'o new_dev = kmalloc(sizeof(struct devname_cache), GFP_KERNEL); 2404b5744805STheodore Ts'o if (!new_dev) 2405b5744805STheodore Ts'o return "NODEV-ALLOCFAILURE"; /* Something non-NULL */ 2406879c5e6bSTheodore Ts'o spin_lock(&devname_cache_lock); 2407879c5e6bSTheodore Ts'o if (devcache[i]) { 2408879c5e6bSTheodore Ts'o if (devcache[i]->device == device) { 2409b5744805STheodore Ts'o kfree(new_dev); 2410879c5e6bSTheodore Ts'o ret = devcache[i]->devname; 2411879c5e6bSTheodore Ts'o spin_unlock(&devname_cache_lock); 2412879c5e6bSTheodore Ts'o return ret; 2413879c5e6bSTheodore Ts'o } 2414879c5e6bSTheodore Ts'o call_rcu(&devcache[i]->rcu, free_devcache); 2415879c5e6bSTheodore Ts'o } 2416b5744805STheodore Ts'o devcache[i] = new_dev; 2417879c5e6bSTheodore Ts'o devcache[i]->device = device; 2418879c5e6bSTheodore Ts'o bd = bdget(device); 2419879c5e6bSTheodore Ts'o if (bd) { 2420879c5e6bSTheodore Ts'o bdevname(bd, devcache[i]->devname); 2421879c5e6bSTheodore Ts'o bdput(bd); 2422879c5e6bSTheodore Ts'o } else 2423879c5e6bSTheodore Ts'o __bdevname(device, devcache[i]->devname); 2424879c5e6bSTheodore Ts'o ret = devcache[i]->devname; 2425879c5e6bSTheodore Ts'o spin_unlock(&devname_cache_lock); 2426879c5e6bSTheodore Ts'o return ret; 2427879c5e6bSTheodore Ts'o } 2428879c5e6bSTheodore Ts'o EXPORT_SYMBOL(jbd2_dev_to_name); 2429879c5e6bSTheodore Ts'o 2430470decc6SDave Kleikamp MODULE_LICENSE("GPL"); 2431470decc6SDave Kleikamp module_init(journal_init); 2432470decc6SDave Kleikamp module_exit(journal_exit); 2433470decc6SDave Kleikamp 2434