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/smp_lock.h> 32470decc6SDave Kleikamp #include <linux/init.h> 33470decc6SDave Kleikamp #include <linux/mm.h> 34470decc6SDave Kleikamp #include <linux/suspend.h> 35470decc6SDave Kleikamp #include <linux/pagemap.h> 36470decc6SDave Kleikamp #include <linux/kthread.h> 37470decc6SDave Kleikamp #include <linux/poison.h> 38470decc6SDave Kleikamp #include <linux/proc_fs.h> 39470decc6SDave Kleikamp 40470decc6SDave Kleikamp #include <asm/uaccess.h> 41470decc6SDave Kleikamp #include <asm/page.h> 42470decc6SDave Kleikamp 43f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_start); 44f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_restart); 45f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_extend); 46f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_stop); 47f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_lock_updates); 48f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_unlock_updates); 49f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_get_write_access); 50f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_get_create_access); 51f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_get_undo_access); 52f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_dirty_data); 53f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_dirty_metadata); 54f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_release_buffer); 55f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_forget); 56470decc6SDave Kleikamp #if 0 57470decc6SDave Kleikamp EXPORT_SYMBOL(journal_sync_buffer); 58470decc6SDave Kleikamp #endif 59f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_flush); 60f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_revoke); 61470decc6SDave Kleikamp 62f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_init_dev); 63f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_init_inode); 64f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_update_format); 65f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_check_used_features); 66f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_check_available_features); 67f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_set_features); 68f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_create); 69f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_load); 70f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_destroy); 71f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_update_superblock); 72f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_abort); 73f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_errno); 74f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_ack_err); 75f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_clear_err); 76f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_log_wait_commit); 77f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_start_commit); 78f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_force_commit_nested); 79f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_wipe); 80f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_blocks_per_page); 81f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_invalidatepage); 82f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers); 83f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_force_commit); 84470decc6SDave Kleikamp 85470decc6SDave Kleikamp static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *); 86470decc6SDave Kleikamp static void __journal_abort_soft (journal_t *journal, int errno); 87f7f4bccbSMingming Cao static int jbd2_journal_create_jbd_slab(size_t slab_size); 88470decc6SDave Kleikamp 89470decc6SDave Kleikamp /* 90470decc6SDave Kleikamp * Helper function used to manage commit timeouts 91470decc6SDave Kleikamp */ 92470decc6SDave Kleikamp 93470decc6SDave Kleikamp static void commit_timeout(unsigned long __data) 94470decc6SDave Kleikamp { 95470decc6SDave Kleikamp struct task_struct * p = (struct task_struct *) __data; 96470decc6SDave Kleikamp 97470decc6SDave Kleikamp wake_up_process(p); 98470decc6SDave Kleikamp } 99470decc6SDave Kleikamp 100470decc6SDave Kleikamp /* 101f7f4bccbSMingming Cao * kjournald2: The main thread function used to manage a logging device 102470decc6SDave Kleikamp * journal. 103470decc6SDave Kleikamp * 104470decc6SDave Kleikamp * This kernel thread is responsible for two things: 105470decc6SDave Kleikamp * 106470decc6SDave Kleikamp * 1) COMMIT: Every so often we need to commit the current state of the 107470decc6SDave Kleikamp * filesystem to disk. The journal thread is responsible for writing 108470decc6SDave Kleikamp * all of the metadata buffers to disk. 109470decc6SDave Kleikamp * 110470decc6SDave Kleikamp * 2) CHECKPOINT: We cannot reuse a used section of the log file until all 111470decc6SDave Kleikamp * of the data in that part of the log has been rewritten elsewhere on 112470decc6SDave Kleikamp * the disk. Flushing these old buffers to reclaim space in the log is 113470decc6SDave Kleikamp * known as checkpointing, and this thread is responsible for that job. 114470decc6SDave Kleikamp */ 115470decc6SDave Kleikamp 116f7f4bccbSMingming Cao static int kjournald2(void *arg) 117470decc6SDave Kleikamp { 118470decc6SDave Kleikamp journal_t *journal = arg; 119470decc6SDave Kleikamp transaction_t *transaction; 120470decc6SDave Kleikamp 121470decc6SDave Kleikamp /* 122470decc6SDave Kleikamp * Set up an interval timer which can be used to trigger a commit wakeup 123470decc6SDave Kleikamp * after the commit interval expires 124470decc6SDave Kleikamp */ 125470decc6SDave Kleikamp setup_timer(&journal->j_commit_timer, commit_timeout, 126470decc6SDave Kleikamp (unsigned long)current); 127470decc6SDave Kleikamp 128470decc6SDave Kleikamp /* Record that the journal thread is running */ 129470decc6SDave Kleikamp journal->j_task = current; 130470decc6SDave Kleikamp wake_up(&journal->j_wait_done_commit); 131470decc6SDave Kleikamp 132f7f4bccbSMingming Cao printk(KERN_INFO "kjournald2 starting. Commit interval %ld seconds\n", 133470decc6SDave Kleikamp journal->j_commit_interval / HZ); 134470decc6SDave Kleikamp 135470decc6SDave Kleikamp /* 136470decc6SDave Kleikamp * And now, wait forever for commit wakeup events. 137470decc6SDave Kleikamp */ 138470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 139470decc6SDave Kleikamp 140470decc6SDave Kleikamp loop: 141f7f4bccbSMingming Cao if (journal->j_flags & JBD2_UNMOUNT) 142470decc6SDave Kleikamp goto end_loop; 143470decc6SDave Kleikamp 144470decc6SDave Kleikamp jbd_debug(1, "commit_sequence=%d, commit_request=%d\n", 145470decc6SDave Kleikamp journal->j_commit_sequence, journal->j_commit_request); 146470decc6SDave Kleikamp 147470decc6SDave Kleikamp if (journal->j_commit_sequence != journal->j_commit_request) { 148470decc6SDave Kleikamp jbd_debug(1, "OK, requests differ\n"); 149470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 150470decc6SDave Kleikamp del_timer_sync(&journal->j_commit_timer); 151f7f4bccbSMingming Cao jbd2_journal_commit_transaction(journal); 152470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 153470decc6SDave Kleikamp goto loop; 154470decc6SDave Kleikamp } 155470decc6SDave Kleikamp 156470decc6SDave Kleikamp wake_up(&journal->j_wait_done_commit); 157470decc6SDave Kleikamp if (freezing(current)) { 158470decc6SDave Kleikamp /* 159470decc6SDave Kleikamp * The simpler the better. Flushing journal isn't a 160470decc6SDave Kleikamp * good idea, because that depends on threads that may 161470decc6SDave Kleikamp * be already stopped. 162470decc6SDave Kleikamp */ 163f7f4bccbSMingming Cao jbd_debug(1, "Now suspending kjournald2\n"); 164470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 165470decc6SDave Kleikamp refrigerator(); 166470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 167470decc6SDave Kleikamp } else { 168470decc6SDave Kleikamp /* 169470decc6SDave Kleikamp * We assume on resume that commits are already there, 170470decc6SDave Kleikamp * so we don't sleep 171470decc6SDave Kleikamp */ 172470decc6SDave Kleikamp DEFINE_WAIT(wait); 173470decc6SDave Kleikamp int should_sleep = 1; 174470decc6SDave Kleikamp 175470decc6SDave Kleikamp prepare_to_wait(&journal->j_wait_commit, &wait, 176470decc6SDave Kleikamp TASK_INTERRUPTIBLE); 177470decc6SDave Kleikamp if (journal->j_commit_sequence != journal->j_commit_request) 178470decc6SDave Kleikamp should_sleep = 0; 179470decc6SDave Kleikamp transaction = journal->j_running_transaction; 180470decc6SDave Kleikamp if (transaction && time_after_eq(jiffies, 181470decc6SDave Kleikamp transaction->t_expires)) 182470decc6SDave Kleikamp should_sleep = 0; 183f7f4bccbSMingming Cao if (journal->j_flags & JBD2_UNMOUNT) 184470decc6SDave Kleikamp should_sleep = 0; 185470decc6SDave Kleikamp if (should_sleep) { 186470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 187470decc6SDave Kleikamp schedule(); 188470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 189470decc6SDave Kleikamp } 190470decc6SDave Kleikamp finish_wait(&journal->j_wait_commit, &wait); 191470decc6SDave Kleikamp } 192470decc6SDave Kleikamp 193f7f4bccbSMingming Cao jbd_debug(1, "kjournald2 wakes\n"); 194470decc6SDave Kleikamp 195470decc6SDave Kleikamp /* 196470decc6SDave Kleikamp * Were we woken up by a commit wakeup event? 197470decc6SDave Kleikamp */ 198470decc6SDave Kleikamp transaction = journal->j_running_transaction; 199470decc6SDave Kleikamp if (transaction && time_after_eq(jiffies, transaction->t_expires)) { 200470decc6SDave Kleikamp journal->j_commit_request = transaction->t_tid; 201470decc6SDave Kleikamp jbd_debug(1, "woke because of timeout\n"); 202470decc6SDave Kleikamp } 203470decc6SDave Kleikamp goto loop; 204470decc6SDave Kleikamp 205470decc6SDave Kleikamp end_loop: 206470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 207470decc6SDave Kleikamp del_timer_sync(&journal->j_commit_timer); 208470decc6SDave Kleikamp journal->j_task = NULL; 209470decc6SDave Kleikamp wake_up(&journal->j_wait_done_commit); 210470decc6SDave Kleikamp jbd_debug(1, "Journal thread exiting.\n"); 211470decc6SDave Kleikamp return 0; 212470decc6SDave Kleikamp } 213470decc6SDave Kleikamp 214f7f4bccbSMingming Cao static void jbd2_journal_start_thread(journal_t *journal) 215470decc6SDave Kleikamp { 216f7f4bccbSMingming Cao kthread_run(kjournald2, journal, "kjournald2"); 217470decc6SDave Kleikamp wait_event(journal->j_wait_done_commit, journal->j_task != 0); 218470decc6SDave Kleikamp } 219470decc6SDave Kleikamp 220470decc6SDave Kleikamp static void journal_kill_thread(journal_t *journal) 221470decc6SDave Kleikamp { 222470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 223f7f4bccbSMingming Cao journal->j_flags |= JBD2_UNMOUNT; 224470decc6SDave Kleikamp 225470decc6SDave Kleikamp while (journal->j_task) { 226470decc6SDave Kleikamp wake_up(&journal->j_wait_commit); 227470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 228470decc6SDave Kleikamp wait_event(journal->j_wait_done_commit, journal->j_task == 0); 229470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 230470decc6SDave Kleikamp } 231470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 232470decc6SDave Kleikamp } 233470decc6SDave Kleikamp 234470decc6SDave Kleikamp /* 235f7f4bccbSMingming Cao * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal. 236470decc6SDave Kleikamp * 237470decc6SDave Kleikamp * Writes a metadata buffer to a given disk block. The actual IO is not 238470decc6SDave Kleikamp * performed but a new buffer_head is constructed which labels the data 239470decc6SDave Kleikamp * to be written with the correct destination disk block. 240470decc6SDave Kleikamp * 241470decc6SDave Kleikamp * Any magic-number escaping which needs to be done will cause a 242470decc6SDave Kleikamp * copy-out here. If the buffer happens to start with the 243f7f4bccbSMingming Cao * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the 244470decc6SDave Kleikamp * magic number is only written to the log for descripter blocks. In 245470decc6SDave Kleikamp * this case, we copy the data and replace the first word with 0, and we 246470decc6SDave Kleikamp * return a result code which indicates that this buffer needs to be 247470decc6SDave Kleikamp * marked as an escaped buffer in the corresponding log descriptor 248470decc6SDave Kleikamp * block. The missing word can then be restored when the block is read 249470decc6SDave Kleikamp * during recovery. 250470decc6SDave Kleikamp * 251470decc6SDave Kleikamp * If the source buffer has already been modified by a new transaction 252470decc6SDave Kleikamp * since we took the last commit snapshot, we use the frozen copy of 253470decc6SDave Kleikamp * that data for IO. If we end up using the existing buffer_head's data 254470decc6SDave Kleikamp * for the write, then we *have* to lock the buffer to prevent anyone 255470decc6SDave Kleikamp * else from using and possibly modifying it while the IO is in 256470decc6SDave Kleikamp * progress. 257470decc6SDave Kleikamp * 258470decc6SDave Kleikamp * The function returns a pointer to the buffer_heads to be used for IO. 259470decc6SDave Kleikamp * 260470decc6SDave Kleikamp * We assume that the journal has already been locked in this function. 261470decc6SDave Kleikamp * 262470decc6SDave Kleikamp * Return value: 263470decc6SDave Kleikamp * <0: Error 264470decc6SDave Kleikamp * >=0: Finished OK 265470decc6SDave Kleikamp * 266470decc6SDave Kleikamp * On success: 267470decc6SDave Kleikamp * Bit 0 set == escape performed on the data 268470decc6SDave Kleikamp * Bit 1 set == buffer copy-out performed (kfree the data after IO) 269470decc6SDave Kleikamp */ 270470decc6SDave Kleikamp 271f7f4bccbSMingming Cao int jbd2_journal_write_metadata_buffer(transaction_t *transaction, 272470decc6SDave Kleikamp struct journal_head *jh_in, 273470decc6SDave Kleikamp struct journal_head **jh_out, 27418eba7aaSMingming Cao unsigned long long blocknr) 275470decc6SDave Kleikamp { 276470decc6SDave Kleikamp int need_copy_out = 0; 277470decc6SDave Kleikamp int done_copy_out = 0; 278470decc6SDave Kleikamp int do_escape = 0; 279470decc6SDave Kleikamp char *mapped_data; 280470decc6SDave Kleikamp struct buffer_head *new_bh; 281470decc6SDave Kleikamp struct journal_head *new_jh; 282470decc6SDave Kleikamp struct page *new_page; 283470decc6SDave Kleikamp unsigned int new_offset; 284470decc6SDave Kleikamp struct buffer_head *bh_in = jh2bh(jh_in); 285470decc6SDave Kleikamp 286470decc6SDave Kleikamp /* 287470decc6SDave Kleikamp * The buffer really shouldn't be locked: only the current committing 288470decc6SDave Kleikamp * transaction is allowed to write it, so nobody else is allowed 289470decc6SDave Kleikamp * to do any IO. 290470decc6SDave Kleikamp * 291470decc6SDave Kleikamp * akpm: except if we're journalling data, and write() output is 292470decc6SDave Kleikamp * also part of a shared mapping, and another thread has 293470decc6SDave Kleikamp * decided to launch a writepage() against this buffer. 294470decc6SDave Kleikamp */ 295470decc6SDave Kleikamp J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in)); 296470decc6SDave Kleikamp 297470decc6SDave Kleikamp new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL); 298470decc6SDave Kleikamp 299470decc6SDave Kleikamp /* 300470decc6SDave Kleikamp * If a new transaction has already done a buffer copy-out, then 301470decc6SDave Kleikamp * we use that version of the data for the commit. 302470decc6SDave Kleikamp */ 303470decc6SDave Kleikamp jbd_lock_bh_state(bh_in); 304470decc6SDave Kleikamp repeat: 305470decc6SDave Kleikamp if (jh_in->b_frozen_data) { 306470decc6SDave Kleikamp done_copy_out = 1; 307470decc6SDave Kleikamp new_page = virt_to_page(jh_in->b_frozen_data); 308470decc6SDave Kleikamp new_offset = offset_in_page(jh_in->b_frozen_data); 309470decc6SDave Kleikamp } else { 310470decc6SDave Kleikamp new_page = jh2bh(jh_in)->b_page; 311470decc6SDave Kleikamp new_offset = offset_in_page(jh2bh(jh_in)->b_data); 312470decc6SDave Kleikamp } 313470decc6SDave Kleikamp 314470decc6SDave Kleikamp mapped_data = kmap_atomic(new_page, KM_USER0); 315470decc6SDave Kleikamp /* 316470decc6SDave Kleikamp * Check for escaping 317470decc6SDave Kleikamp */ 318470decc6SDave Kleikamp if (*((__be32 *)(mapped_data + new_offset)) == 319f7f4bccbSMingming Cao cpu_to_be32(JBD2_MAGIC_NUMBER)) { 320470decc6SDave Kleikamp need_copy_out = 1; 321470decc6SDave Kleikamp do_escape = 1; 322470decc6SDave Kleikamp } 323470decc6SDave Kleikamp kunmap_atomic(mapped_data, KM_USER0); 324470decc6SDave Kleikamp 325470decc6SDave Kleikamp /* 326470decc6SDave Kleikamp * Do we need to do a data copy? 327470decc6SDave Kleikamp */ 328470decc6SDave Kleikamp if (need_copy_out && !done_copy_out) { 329470decc6SDave Kleikamp char *tmp; 330470decc6SDave Kleikamp 331470decc6SDave Kleikamp jbd_unlock_bh_state(bh_in); 332f7f4bccbSMingming Cao tmp = jbd2_slab_alloc(bh_in->b_size, GFP_NOFS); 333470decc6SDave Kleikamp jbd_lock_bh_state(bh_in); 334470decc6SDave Kleikamp if (jh_in->b_frozen_data) { 335f7f4bccbSMingming Cao jbd2_slab_free(tmp, bh_in->b_size); 336470decc6SDave Kleikamp goto repeat; 337470decc6SDave Kleikamp } 338470decc6SDave Kleikamp 339470decc6SDave Kleikamp jh_in->b_frozen_data = tmp; 340470decc6SDave Kleikamp mapped_data = kmap_atomic(new_page, KM_USER0); 341470decc6SDave Kleikamp memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size); 342470decc6SDave Kleikamp kunmap_atomic(mapped_data, KM_USER0); 343470decc6SDave Kleikamp 344470decc6SDave Kleikamp new_page = virt_to_page(tmp); 345470decc6SDave Kleikamp new_offset = offset_in_page(tmp); 346470decc6SDave Kleikamp done_copy_out = 1; 347470decc6SDave Kleikamp } 348470decc6SDave Kleikamp 349470decc6SDave Kleikamp /* 350470decc6SDave Kleikamp * Did we need to do an escaping? Now we've done all the 351470decc6SDave Kleikamp * copying, we can finally do so. 352470decc6SDave Kleikamp */ 353470decc6SDave Kleikamp if (do_escape) { 354470decc6SDave Kleikamp mapped_data = kmap_atomic(new_page, KM_USER0); 355470decc6SDave Kleikamp *((unsigned int *)(mapped_data + new_offset)) = 0; 356470decc6SDave Kleikamp kunmap_atomic(mapped_data, KM_USER0); 357470decc6SDave Kleikamp } 358470decc6SDave Kleikamp 359470decc6SDave Kleikamp /* keep subsequent assertions sane */ 360470decc6SDave Kleikamp new_bh->b_state = 0; 361470decc6SDave Kleikamp init_buffer(new_bh, NULL, NULL); 362470decc6SDave Kleikamp atomic_set(&new_bh->b_count, 1); 363470decc6SDave Kleikamp jbd_unlock_bh_state(bh_in); 364470decc6SDave Kleikamp 365f7f4bccbSMingming Cao new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */ 366470decc6SDave Kleikamp 367470decc6SDave Kleikamp set_bh_page(new_bh, new_page, new_offset); 368470decc6SDave Kleikamp new_jh->b_transaction = NULL; 369470decc6SDave Kleikamp new_bh->b_size = jh2bh(jh_in)->b_size; 370470decc6SDave Kleikamp new_bh->b_bdev = transaction->t_journal->j_dev; 371470decc6SDave Kleikamp new_bh->b_blocknr = blocknr; 372470decc6SDave Kleikamp set_buffer_mapped(new_bh); 373470decc6SDave Kleikamp set_buffer_dirty(new_bh); 374470decc6SDave Kleikamp 375470decc6SDave Kleikamp *jh_out = new_jh; 376470decc6SDave Kleikamp 377470decc6SDave Kleikamp /* 378470decc6SDave Kleikamp * The to-be-written buffer needs to get moved to the io queue, 379470decc6SDave Kleikamp * and the original buffer whose contents we are shadowing or 380470decc6SDave Kleikamp * copying is moved to the transaction's shadow queue. 381470decc6SDave Kleikamp */ 382470decc6SDave Kleikamp JBUFFER_TRACE(jh_in, "file as BJ_Shadow"); 383f7f4bccbSMingming Cao jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow); 384470decc6SDave Kleikamp JBUFFER_TRACE(new_jh, "file as BJ_IO"); 385f7f4bccbSMingming Cao jbd2_journal_file_buffer(new_jh, transaction, BJ_IO); 386470decc6SDave Kleikamp 387470decc6SDave Kleikamp return do_escape | (done_copy_out << 1); 388470decc6SDave Kleikamp } 389470decc6SDave Kleikamp 390470decc6SDave Kleikamp /* 391470decc6SDave Kleikamp * Allocation code for the journal file. Manage the space left in the 392470decc6SDave Kleikamp * journal, so that we can begin checkpointing when appropriate. 393470decc6SDave Kleikamp */ 394470decc6SDave Kleikamp 395470decc6SDave Kleikamp /* 396f7f4bccbSMingming Cao * __jbd2_log_space_left: Return the number of free blocks left in the journal. 397470decc6SDave Kleikamp * 398470decc6SDave Kleikamp * Called with the journal already locked. 399470decc6SDave Kleikamp * 400470decc6SDave Kleikamp * Called under j_state_lock 401470decc6SDave Kleikamp */ 402470decc6SDave Kleikamp 403f7f4bccbSMingming Cao int __jbd2_log_space_left(journal_t *journal) 404470decc6SDave Kleikamp { 405470decc6SDave Kleikamp int left = journal->j_free; 406470decc6SDave Kleikamp 407470decc6SDave Kleikamp assert_spin_locked(&journal->j_state_lock); 408470decc6SDave Kleikamp 409470decc6SDave Kleikamp /* 410470decc6SDave Kleikamp * Be pessimistic here about the number of those free blocks which 411470decc6SDave Kleikamp * might be required for log descriptor control blocks. 412470decc6SDave Kleikamp */ 413470decc6SDave Kleikamp 414470decc6SDave Kleikamp #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */ 415470decc6SDave Kleikamp 416470decc6SDave Kleikamp left -= MIN_LOG_RESERVED_BLOCKS; 417470decc6SDave Kleikamp 418470decc6SDave Kleikamp if (left <= 0) 419470decc6SDave Kleikamp return 0; 420470decc6SDave Kleikamp left -= (left >> 3); 421470decc6SDave Kleikamp return left; 422470decc6SDave Kleikamp } 423470decc6SDave Kleikamp 424470decc6SDave Kleikamp /* 425470decc6SDave Kleikamp * Called under j_state_lock. Returns true if a transaction was started. 426470decc6SDave Kleikamp */ 427f7f4bccbSMingming Cao int __jbd2_log_start_commit(journal_t *journal, tid_t target) 428470decc6SDave Kleikamp { 429470decc6SDave Kleikamp /* 430470decc6SDave Kleikamp * Are we already doing a recent enough commit? 431470decc6SDave Kleikamp */ 432470decc6SDave Kleikamp if (!tid_geq(journal->j_commit_request, target)) { 433470decc6SDave Kleikamp /* 434470decc6SDave Kleikamp * We want a new commit: OK, mark the request and wakup the 435470decc6SDave Kleikamp * commit thread. We do _not_ do the commit ourselves. 436470decc6SDave Kleikamp */ 437470decc6SDave Kleikamp 438470decc6SDave Kleikamp journal->j_commit_request = target; 439470decc6SDave Kleikamp jbd_debug(1, "JBD: requesting commit %d/%d\n", 440470decc6SDave Kleikamp journal->j_commit_request, 441470decc6SDave Kleikamp journal->j_commit_sequence); 442470decc6SDave Kleikamp wake_up(&journal->j_wait_commit); 443470decc6SDave Kleikamp return 1; 444470decc6SDave Kleikamp } 445470decc6SDave Kleikamp return 0; 446470decc6SDave Kleikamp } 447470decc6SDave Kleikamp 448f7f4bccbSMingming Cao int jbd2_log_start_commit(journal_t *journal, tid_t tid) 449470decc6SDave Kleikamp { 450470decc6SDave Kleikamp int ret; 451470decc6SDave Kleikamp 452470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 453f7f4bccbSMingming Cao ret = __jbd2_log_start_commit(journal, tid); 454470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 455470decc6SDave Kleikamp return ret; 456470decc6SDave Kleikamp } 457470decc6SDave Kleikamp 458470decc6SDave Kleikamp /* 459470decc6SDave Kleikamp * Force and wait upon a commit if the calling process is not within 460470decc6SDave Kleikamp * transaction. This is used for forcing out undo-protected data which contains 461470decc6SDave Kleikamp * bitmaps, when the fs is running out of space. 462470decc6SDave Kleikamp * 463470decc6SDave Kleikamp * We can only force the running transaction if we don't have an active handle; 464470decc6SDave Kleikamp * otherwise, we will deadlock. 465470decc6SDave Kleikamp * 466470decc6SDave Kleikamp * Returns true if a transaction was started. 467470decc6SDave Kleikamp */ 468f7f4bccbSMingming Cao int jbd2_journal_force_commit_nested(journal_t *journal) 469470decc6SDave Kleikamp { 470470decc6SDave Kleikamp transaction_t *transaction = NULL; 471470decc6SDave Kleikamp tid_t tid; 472470decc6SDave Kleikamp 473470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 474470decc6SDave Kleikamp if (journal->j_running_transaction && !current->journal_info) { 475470decc6SDave Kleikamp transaction = journal->j_running_transaction; 476f7f4bccbSMingming Cao __jbd2_log_start_commit(journal, transaction->t_tid); 477470decc6SDave Kleikamp } else if (journal->j_committing_transaction) 478470decc6SDave Kleikamp transaction = journal->j_committing_transaction; 479470decc6SDave Kleikamp 480470decc6SDave Kleikamp if (!transaction) { 481470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 482470decc6SDave Kleikamp return 0; /* Nothing to retry */ 483470decc6SDave Kleikamp } 484470decc6SDave Kleikamp 485470decc6SDave Kleikamp tid = transaction->t_tid; 486470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 487f7f4bccbSMingming Cao jbd2_log_wait_commit(journal, tid); 488470decc6SDave Kleikamp return 1; 489470decc6SDave Kleikamp } 490470decc6SDave Kleikamp 491470decc6SDave Kleikamp /* 492470decc6SDave Kleikamp * Start a commit of the current running transaction (if any). Returns true 493470decc6SDave Kleikamp * if a transaction was started, and fills its tid in at *ptid 494470decc6SDave Kleikamp */ 495f7f4bccbSMingming Cao int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid) 496470decc6SDave Kleikamp { 497470decc6SDave Kleikamp int ret = 0; 498470decc6SDave Kleikamp 499470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 500470decc6SDave Kleikamp if (journal->j_running_transaction) { 501470decc6SDave Kleikamp tid_t tid = journal->j_running_transaction->t_tid; 502470decc6SDave Kleikamp 503f7f4bccbSMingming Cao ret = __jbd2_log_start_commit(journal, tid); 504470decc6SDave Kleikamp if (ret && ptid) 505470decc6SDave Kleikamp *ptid = tid; 506470decc6SDave Kleikamp } else if (journal->j_committing_transaction && ptid) { 507470decc6SDave Kleikamp /* 508470decc6SDave Kleikamp * If ext3_write_super() recently started a commit, then we 509470decc6SDave Kleikamp * have to wait for completion of that transaction 510470decc6SDave Kleikamp */ 511470decc6SDave Kleikamp *ptid = journal->j_committing_transaction->t_tid; 512470decc6SDave Kleikamp ret = 1; 513470decc6SDave Kleikamp } 514470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 515470decc6SDave Kleikamp return ret; 516470decc6SDave Kleikamp } 517470decc6SDave Kleikamp 518470decc6SDave Kleikamp /* 519470decc6SDave Kleikamp * Wait for a specified commit to complete. 520470decc6SDave Kleikamp * The caller may not hold the journal lock. 521470decc6SDave Kleikamp */ 522f7f4bccbSMingming Cao int jbd2_log_wait_commit(journal_t *journal, tid_t tid) 523470decc6SDave Kleikamp { 524470decc6SDave Kleikamp int err = 0; 525470decc6SDave Kleikamp 526470decc6SDave Kleikamp #ifdef CONFIG_JBD_DEBUG 527470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 528470decc6SDave Kleikamp if (!tid_geq(journal->j_commit_request, tid)) { 529470decc6SDave Kleikamp printk(KERN_EMERG 530470decc6SDave Kleikamp "%s: error: j_commit_request=%d, tid=%d\n", 531470decc6SDave Kleikamp __FUNCTION__, journal->j_commit_request, tid); 532470decc6SDave Kleikamp } 533470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 534470decc6SDave Kleikamp #endif 535470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 536470decc6SDave Kleikamp while (tid_gt(tid, journal->j_commit_sequence)) { 537470decc6SDave Kleikamp jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n", 538470decc6SDave Kleikamp tid, journal->j_commit_sequence); 539470decc6SDave Kleikamp wake_up(&journal->j_wait_commit); 540470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 541470decc6SDave Kleikamp wait_event(journal->j_wait_done_commit, 542470decc6SDave Kleikamp !tid_gt(tid, journal->j_commit_sequence)); 543470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 544470decc6SDave Kleikamp } 545470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 546470decc6SDave Kleikamp 547470decc6SDave Kleikamp if (unlikely(is_journal_aborted(journal))) { 548470decc6SDave Kleikamp printk(KERN_EMERG "journal commit I/O error\n"); 549470decc6SDave Kleikamp err = -EIO; 550470decc6SDave Kleikamp } 551470decc6SDave Kleikamp return err; 552470decc6SDave Kleikamp } 553470decc6SDave Kleikamp 554470decc6SDave Kleikamp /* 555470decc6SDave Kleikamp * Log buffer allocation routines: 556470decc6SDave Kleikamp */ 557470decc6SDave Kleikamp 55818eba7aaSMingming Cao int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp) 559470decc6SDave Kleikamp { 560470decc6SDave Kleikamp unsigned long blocknr; 561470decc6SDave Kleikamp 562470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 563470decc6SDave Kleikamp J_ASSERT(journal->j_free > 1); 564470decc6SDave Kleikamp 565470decc6SDave Kleikamp blocknr = journal->j_head; 566470decc6SDave Kleikamp journal->j_head++; 567470decc6SDave Kleikamp journal->j_free--; 568470decc6SDave Kleikamp if (journal->j_head == journal->j_last) 569470decc6SDave Kleikamp journal->j_head = journal->j_first; 570470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 571f7f4bccbSMingming Cao return jbd2_journal_bmap(journal, blocknr, retp); 572470decc6SDave Kleikamp } 573470decc6SDave Kleikamp 574470decc6SDave Kleikamp /* 575470decc6SDave Kleikamp * Conversion of logical to physical block numbers for the journal 576470decc6SDave Kleikamp * 577470decc6SDave Kleikamp * On external journals the journal blocks are identity-mapped, so 578470decc6SDave Kleikamp * this is a no-op. If needed, we can use j_blk_offset - everything is 579470decc6SDave Kleikamp * ready. 580470decc6SDave Kleikamp */ 581f7f4bccbSMingming Cao int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr, 58218eba7aaSMingming Cao unsigned long long *retp) 583470decc6SDave Kleikamp { 584470decc6SDave Kleikamp int err = 0; 58518eba7aaSMingming Cao unsigned long long ret; 586470decc6SDave Kleikamp 587470decc6SDave Kleikamp if (journal->j_inode) { 588470decc6SDave Kleikamp ret = bmap(journal->j_inode, blocknr); 589470decc6SDave Kleikamp if (ret) 590470decc6SDave Kleikamp *retp = ret; 591470decc6SDave Kleikamp else { 592470decc6SDave Kleikamp char b[BDEVNAME_SIZE]; 593470decc6SDave Kleikamp 594470decc6SDave Kleikamp printk(KERN_ALERT "%s: journal block not found " 595470decc6SDave Kleikamp "at offset %lu on %s\n", 596470decc6SDave Kleikamp __FUNCTION__, 597470decc6SDave Kleikamp blocknr, 598470decc6SDave Kleikamp bdevname(journal->j_dev, b)); 599470decc6SDave Kleikamp err = -EIO; 600470decc6SDave Kleikamp __journal_abort_soft(journal, err); 601470decc6SDave Kleikamp } 602470decc6SDave Kleikamp } else { 603470decc6SDave Kleikamp *retp = blocknr; /* +journal->j_blk_offset */ 604470decc6SDave Kleikamp } 605470decc6SDave Kleikamp return err; 606470decc6SDave Kleikamp } 607470decc6SDave Kleikamp 608470decc6SDave Kleikamp /* 609470decc6SDave Kleikamp * We play buffer_head aliasing tricks to write data/metadata blocks to 610470decc6SDave Kleikamp * the journal without copying their contents, but for journal 611470decc6SDave Kleikamp * descriptor blocks we do need to generate bona fide buffers. 612470decc6SDave Kleikamp * 613f7f4bccbSMingming Cao * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying 614470decc6SDave Kleikamp * the buffer's contents they really should run flush_dcache_page(bh->b_page). 615470decc6SDave Kleikamp * But we don't bother doing that, so there will be coherency problems with 616470decc6SDave Kleikamp * mmaps of blockdevs which hold live JBD-controlled filesystems. 617470decc6SDave Kleikamp */ 618f7f4bccbSMingming Cao struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal) 619470decc6SDave Kleikamp { 620470decc6SDave Kleikamp struct buffer_head *bh; 62118eba7aaSMingming Cao unsigned long long blocknr; 622470decc6SDave Kleikamp int err; 623470decc6SDave Kleikamp 624f7f4bccbSMingming Cao err = jbd2_journal_next_log_block(journal, &blocknr); 625470decc6SDave Kleikamp 626470decc6SDave Kleikamp if (err) 627470decc6SDave Kleikamp return NULL; 628470decc6SDave Kleikamp 629470decc6SDave Kleikamp bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize); 630470decc6SDave Kleikamp lock_buffer(bh); 631470decc6SDave Kleikamp memset(bh->b_data, 0, journal->j_blocksize); 632470decc6SDave Kleikamp set_buffer_uptodate(bh); 633470decc6SDave Kleikamp unlock_buffer(bh); 634470decc6SDave Kleikamp BUFFER_TRACE(bh, "return this buffer"); 635f7f4bccbSMingming Cao return jbd2_journal_add_journal_head(bh); 636470decc6SDave Kleikamp } 637470decc6SDave Kleikamp 638470decc6SDave Kleikamp /* 639470decc6SDave Kleikamp * Management for journal control blocks: functions to create and 640470decc6SDave Kleikamp * destroy journal_t structures, and to initialise and read existing 641470decc6SDave Kleikamp * journal blocks from disk. */ 642470decc6SDave Kleikamp 643470decc6SDave Kleikamp /* First: create and setup a journal_t object in memory. We initialise 644470decc6SDave Kleikamp * very few fields yet: that has to wait until we have created the 645470decc6SDave Kleikamp * journal structures from from scratch, or loaded them from disk. */ 646470decc6SDave Kleikamp 647470decc6SDave Kleikamp static journal_t * journal_init_common (void) 648470decc6SDave Kleikamp { 649470decc6SDave Kleikamp journal_t *journal; 650470decc6SDave Kleikamp int err; 651470decc6SDave Kleikamp 652470decc6SDave Kleikamp journal = jbd_kmalloc(sizeof(*journal), GFP_KERNEL); 653470decc6SDave Kleikamp if (!journal) 654470decc6SDave Kleikamp goto fail; 655470decc6SDave Kleikamp memset(journal, 0, sizeof(*journal)); 656470decc6SDave Kleikamp 657470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_transaction_locked); 658470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_logspace); 659470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_done_commit); 660470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_checkpoint); 661470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_commit); 662470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_updates); 663470decc6SDave Kleikamp mutex_init(&journal->j_barrier); 664470decc6SDave Kleikamp mutex_init(&journal->j_checkpoint_mutex); 665470decc6SDave Kleikamp spin_lock_init(&journal->j_revoke_lock); 666470decc6SDave Kleikamp spin_lock_init(&journal->j_list_lock); 667470decc6SDave Kleikamp spin_lock_init(&journal->j_state_lock); 668470decc6SDave Kleikamp 669470decc6SDave Kleikamp journal->j_commit_interval = (HZ * JBD_DEFAULT_MAX_COMMIT_AGE); 670470decc6SDave Kleikamp 671470decc6SDave Kleikamp /* The journal is marked for error until we succeed with recovery! */ 672f7f4bccbSMingming Cao journal->j_flags = JBD2_ABORT; 673470decc6SDave Kleikamp 674470decc6SDave Kleikamp /* Set up a default-sized revoke table for the new mount. */ 675f7f4bccbSMingming Cao err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH); 676470decc6SDave Kleikamp if (err) { 677470decc6SDave Kleikamp kfree(journal); 678470decc6SDave Kleikamp goto fail; 679470decc6SDave Kleikamp } 680470decc6SDave Kleikamp return journal; 681470decc6SDave Kleikamp fail: 682470decc6SDave Kleikamp return NULL; 683470decc6SDave Kleikamp } 684470decc6SDave Kleikamp 685f7f4bccbSMingming Cao /* jbd2_journal_init_dev and jbd2_journal_init_inode: 686470decc6SDave Kleikamp * 687470decc6SDave Kleikamp * Create a journal structure assigned some fixed set of disk blocks to 688470decc6SDave Kleikamp * the journal. We don't actually touch those disk blocks yet, but we 689470decc6SDave Kleikamp * need to set up all of the mapping information to tell the journaling 690470decc6SDave Kleikamp * system where the journal blocks are. 691470decc6SDave Kleikamp * 692470decc6SDave Kleikamp */ 693470decc6SDave Kleikamp 694470decc6SDave Kleikamp /** 695f7f4bccbSMingming Cao * journal_t * jbd2_journal_init_dev() - creates an initialises a journal structure 696470decc6SDave Kleikamp * @bdev: Block device on which to create the journal 697470decc6SDave Kleikamp * @fs_dev: Device which hold journalled filesystem for this journal. 698470decc6SDave Kleikamp * @start: Block nr Start of journal. 699470decc6SDave Kleikamp * @len: Length of the journal in blocks. 700470decc6SDave Kleikamp * @blocksize: blocksize of journalling device 701470decc6SDave Kleikamp * @returns: a newly created journal_t * 702470decc6SDave Kleikamp * 703f7f4bccbSMingming Cao * jbd2_journal_init_dev creates a journal which maps a fixed contiguous 704470decc6SDave Kleikamp * range of blocks on an arbitrary block device. 705470decc6SDave Kleikamp * 706470decc6SDave Kleikamp */ 707f7f4bccbSMingming Cao journal_t * jbd2_journal_init_dev(struct block_device *bdev, 708470decc6SDave Kleikamp struct block_device *fs_dev, 70918eba7aaSMingming Cao unsigned long long start, int len, int blocksize) 710470decc6SDave Kleikamp { 711470decc6SDave Kleikamp journal_t *journal = journal_init_common(); 712470decc6SDave Kleikamp struct buffer_head *bh; 713470decc6SDave Kleikamp int n; 714470decc6SDave Kleikamp 715470decc6SDave Kleikamp if (!journal) 716470decc6SDave Kleikamp return NULL; 717470decc6SDave Kleikamp 718470decc6SDave Kleikamp /* journal descriptor can store up to n blocks -bzzz */ 719470decc6SDave Kleikamp journal->j_blocksize = blocksize; 720470decc6SDave Kleikamp n = journal->j_blocksize / sizeof(journal_block_tag_t); 721470decc6SDave Kleikamp journal->j_wbufsize = n; 722470decc6SDave Kleikamp journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL); 723470decc6SDave Kleikamp if (!journal->j_wbuf) { 724470decc6SDave Kleikamp printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n", 725470decc6SDave Kleikamp __FUNCTION__); 726470decc6SDave Kleikamp kfree(journal); 727470decc6SDave Kleikamp journal = NULL; 728*5eb30790SDave Kleikamp goto out; 729470decc6SDave Kleikamp } 730470decc6SDave Kleikamp journal->j_dev = bdev; 731470decc6SDave Kleikamp journal->j_fs_dev = fs_dev; 732470decc6SDave Kleikamp journal->j_blk_offset = start; 733470decc6SDave Kleikamp journal->j_maxlen = len; 734470decc6SDave Kleikamp 735470decc6SDave Kleikamp bh = __getblk(journal->j_dev, start, journal->j_blocksize); 736470decc6SDave Kleikamp J_ASSERT(bh != NULL); 737470decc6SDave Kleikamp journal->j_sb_buffer = bh; 738470decc6SDave Kleikamp journal->j_superblock = (journal_superblock_t *)bh->b_data; 739*5eb30790SDave Kleikamp out: 740470decc6SDave Kleikamp return journal; 741470decc6SDave Kleikamp } 742470decc6SDave Kleikamp 743470decc6SDave Kleikamp /** 744f7f4bccbSMingming Cao * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode. 745470decc6SDave Kleikamp * @inode: An inode to create the journal in 746470decc6SDave Kleikamp * 747f7f4bccbSMingming Cao * jbd2_journal_init_inode creates a journal which maps an on-disk inode as 748470decc6SDave Kleikamp * the journal. The inode must exist already, must support bmap() and 749470decc6SDave Kleikamp * must have all data blocks preallocated. 750470decc6SDave Kleikamp */ 751f7f4bccbSMingming Cao journal_t * jbd2_journal_init_inode (struct inode *inode) 752470decc6SDave Kleikamp { 753470decc6SDave Kleikamp struct buffer_head *bh; 754470decc6SDave Kleikamp journal_t *journal = journal_init_common(); 755470decc6SDave Kleikamp int err; 756470decc6SDave Kleikamp int n; 75718eba7aaSMingming Cao unsigned long long blocknr; 758470decc6SDave Kleikamp 759470decc6SDave Kleikamp if (!journal) 760470decc6SDave Kleikamp return NULL; 761470decc6SDave Kleikamp 762470decc6SDave Kleikamp journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev; 763470decc6SDave Kleikamp journal->j_inode = inode; 764470decc6SDave Kleikamp jbd_debug(1, 765470decc6SDave Kleikamp "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n", 766470decc6SDave Kleikamp journal, inode->i_sb->s_id, inode->i_ino, 767470decc6SDave Kleikamp (long long) inode->i_size, 768470decc6SDave Kleikamp inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize); 769470decc6SDave Kleikamp 770470decc6SDave Kleikamp journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits; 771470decc6SDave Kleikamp journal->j_blocksize = inode->i_sb->s_blocksize; 772470decc6SDave Kleikamp 773470decc6SDave Kleikamp /* journal descriptor can store up to n blocks -bzzz */ 774470decc6SDave Kleikamp n = journal->j_blocksize / sizeof(journal_block_tag_t); 775470decc6SDave Kleikamp journal->j_wbufsize = n; 776470decc6SDave Kleikamp journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL); 777470decc6SDave Kleikamp if (!journal->j_wbuf) { 778470decc6SDave Kleikamp printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n", 779470decc6SDave Kleikamp __FUNCTION__); 780470decc6SDave Kleikamp kfree(journal); 781470decc6SDave Kleikamp return NULL; 782470decc6SDave Kleikamp } 783470decc6SDave Kleikamp 784f7f4bccbSMingming Cao err = jbd2_journal_bmap(journal, 0, &blocknr); 785470decc6SDave Kleikamp /* If that failed, give up */ 786470decc6SDave Kleikamp if (err) { 787470decc6SDave Kleikamp printk(KERN_ERR "%s: Cannnot locate journal superblock\n", 788470decc6SDave Kleikamp __FUNCTION__); 789470decc6SDave Kleikamp kfree(journal); 790470decc6SDave Kleikamp return NULL; 791470decc6SDave Kleikamp } 792470decc6SDave Kleikamp 793470decc6SDave Kleikamp bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize); 794470decc6SDave Kleikamp J_ASSERT(bh != NULL); 795470decc6SDave Kleikamp journal->j_sb_buffer = bh; 796470decc6SDave Kleikamp journal->j_superblock = (journal_superblock_t *)bh->b_data; 797470decc6SDave Kleikamp 798470decc6SDave Kleikamp return journal; 799470decc6SDave Kleikamp } 800470decc6SDave Kleikamp 801470decc6SDave Kleikamp /* 802470decc6SDave Kleikamp * If the journal init or create aborts, we need to mark the journal 803470decc6SDave Kleikamp * superblock as being NULL to prevent the journal destroy from writing 804470decc6SDave Kleikamp * back a bogus superblock. 805470decc6SDave Kleikamp */ 806470decc6SDave Kleikamp static void journal_fail_superblock (journal_t *journal) 807470decc6SDave Kleikamp { 808470decc6SDave Kleikamp struct buffer_head *bh = journal->j_sb_buffer; 809470decc6SDave Kleikamp brelse(bh); 810470decc6SDave Kleikamp journal->j_sb_buffer = NULL; 811470decc6SDave Kleikamp } 812470decc6SDave Kleikamp 813470decc6SDave Kleikamp /* 814470decc6SDave Kleikamp * Given a journal_t structure, initialise the various fields for 815470decc6SDave Kleikamp * startup of a new journaling session. We use this both when creating 816470decc6SDave Kleikamp * a journal, and after recovering an old journal to reset it for 817470decc6SDave Kleikamp * subsequent use. 818470decc6SDave Kleikamp */ 819470decc6SDave Kleikamp 820470decc6SDave Kleikamp static int journal_reset(journal_t *journal) 821470decc6SDave Kleikamp { 822470decc6SDave Kleikamp journal_superblock_t *sb = journal->j_superblock; 82318eba7aaSMingming Cao unsigned long long first, last; 824470decc6SDave Kleikamp 825470decc6SDave Kleikamp first = be32_to_cpu(sb->s_first); 826470decc6SDave Kleikamp last = be32_to_cpu(sb->s_maxlen); 827470decc6SDave Kleikamp 828470decc6SDave Kleikamp journal->j_first = first; 829470decc6SDave Kleikamp journal->j_last = last; 830470decc6SDave Kleikamp 831470decc6SDave Kleikamp journal->j_head = first; 832470decc6SDave Kleikamp journal->j_tail = first; 833470decc6SDave Kleikamp journal->j_free = last - first; 834470decc6SDave Kleikamp 835470decc6SDave Kleikamp journal->j_tail_sequence = journal->j_transaction_sequence; 836470decc6SDave Kleikamp journal->j_commit_sequence = journal->j_transaction_sequence - 1; 837470decc6SDave Kleikamp journal->j_commit_request = journal->j_commit_sequence; 838470decc6SDave Kleikamp 839470decc6SDave Kleikamp journal->j_max_transaction_buffers = journal->j_maxlen / 4; 840470decc6SDave Kleikamp 841470decc6SDave Kleikamp /* Add the dynamic fields and write it to disk. */ 842f7f4bccbSMingming Cao jbd2_journal_update_superblock(journal, 1); 843f7f4bccbSMingming Cao jbd2_journal_start_thread(journal); 844470decc6SDave Kleikamp return 0; 845470decc6SDave Kleikamp } 846470decc6SDave Kleikamp 847470decc6SDave Kleikamp /** 848f7f4bccbSMingming Cao * int jbd2_journal_create() - Initialise the new journal file 849470decc6SDave Kleikamp * @journal: Journal to create. This structure must have been initialised 850470decc6SDave Kleikamp * 851470decc6SDave Kleikamp * Given a journal_t structure which tells us which disk blocks we can 852470decc6SDave Kleikamp * use, create a new journal superblock and initialise all of the 853470decc6SDave Kleikamp * journal fields from scratch. 854470decc6SDave Kleikamp **/ 855f7f4bccbSMingming Cao int jbd2_journal_create(journal_t *journal) 856470decc6SDave Kleikamp { 85718eba7aaSMingming Cao unsigned long long blocknr; 858470decc6SDave Kleikamp struct buffer_head *bh; 859470decc6SDave Kleikamp journal_superblock_t *sb; 860470decc6SDave Kleikamp int i, err; 861470decc6SDave Kleikamp 862f7f4bccbSMingming Cao if (journal->j_maxlen < JBD2_MIN_JOURNAL_BLOCKS) { 863470decc6SDave Kleikamp printk (KERN_ERR "Journal length (%d blocks) too short.\n", 864470decc6SDave Kleikamp journal->j_maxlen); 865470decc6SDave Kleikamp journal_fail_superblock(journal); 866470decc6SDave Kleikamp return -EINVAL; 867470decc6SDave Kleikamp } 868470decc6SDave Kleikamp 869470decc6SDave Kleikamp if (journal->j_inode == NULL) { 870470decc6SDave Kleikamp /* 871470decc6SDave Kleikamp * We don't know what block to start at! 872470decc6SDave Kleikamp */ 873470decc6SDave Kleikamp printk(KERN_EMERG 874470decc6SDave Kleikamp "%s: creation of journal on external device!\n", 875470decc6SDave Kleikamp __FUNCTION__); 876470decc6SDave Kleikamp BUG(); 877470decc6SDave Kleikamp } 878470decc6SDave Kleikamp 879470decc6SDave Kleikamp /* Zero out the entire journal on disk. We cannot afford to 880f7f4bccbSMingming Cao have any blocks on disk beginning with JBD2_MAGIC_NUMBER. */ 881470decc6SDave Kleikamp jbd_debug(1, "JBD: Zeroing out journal blocks...\n"); 882470decc6SDave Kleikamp for (i = 0; i < journal->j_maxlen; i++) { 883f7f4bccbSMingming Cao err = jbd2_journal_bmap(journal, i, &blocknr); 884470decc6SDave Kleikamp if (err) 885470decc6SDave Kleikamp return err; 886470decc6SDave Kleikamp bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize); 887470decc6SDave Kleikamp lock_buffer(bh); 888470decc6SDave Kleikamp memset (bh->b_data, 0, journal->j_blocksize); 889470decc6SDave Kleikamp BUFFER_TRACE(bh, "marking dirty"); 890470decc6SDave Kleikamp mark_buffer_dirty(bh); 891470decc6SDave Kleikamp BUFFER_TRACE(bh, "marking uptodate"); 892470decc6SDave Kleikamp set_buffer_uptodate(bh); 893470decc6SDave Kleikamp unlock_buffer(bh); 894470decc6SDave Kleikamp __brelse(bh); 895470decc6SDave Kleikamp } 896470decc6SDave Kleikamp 897470decc6SDave Kleikamp sync_blockdev(journal->j_dev); 898470decc6SDave Kleikamp jbd_debug(1, "JBD: journal cleared.\n"); 899470decc6SDave Kleikamp 900470decc6SDave Kleikamp /* OK, fill in the initial static fields in the new superblock */ 901470decc6SDave Kleikamp sb = journal->j_superblock; 902470decc6SDave Kleikamp 903f7f4bccbSMingming Cao sb->s_header.h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER); 904f7f4bccbSMingming Cao sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2); 905470decc6SDave Kleikamp 906470decc6SDave Kleikamp sb->s_blocksize = cpu_to_be32(journal->j_blocksize); 907470decc6SDave Kleikamp sb->s_maxlen = cpu_to_be32(journal->j_maxlen); 908470decc6SDave Kleikamp sb->s_first = cpu_to_be32(1); 909470decc6SDave Kleikamp 910470decc6SDave Kleikamp journal->j_transaction_sequence = 1; 911470decc6SDave Kleikamp 912f7f4bccbSMingming Cao journal->j_flags &= ~JBD2_ABORT; 913470decc6SDave Kleikamp journal->j_format_version = 2; 914470decc6SDave Kleikamp 915470decc6SDave Kleikamp return journal_reset(journal); 916470decc6SDave Kleikamp } 917470decc6SDave Kleikamp 918470decc6SDave Kleikamp /** 919f7f4bccbSMingming Cao * void jbd2_journal_update_superblock() - Update journal sb on disk. 920470decc6SDave Kleikamp * @journal: The journal to update. 921470decc6SDave Kleikamp * @wait: Set to '0' if you don't want to wait for IO completion. 922470decc6SDave Kleikamp * 923470decc6SDave Kleikamp * Update a journal's dynamic superblock fields and write it to disk, 924470decc6SDave Kleikamp * optionally waiting for the IO to complete. 925470decc6SDave Kleikamp */ 926f7f4bccbSMingming Cao void jbd2_journal_update_superblock(journal_t *journal, int wait) 927470decc6SDave Kleikamp { 928470decc6SDave Kleikamp journal_superblock_t *sb = journal->j_superblock; 929470decc6SDave Kleikamp struct buffer_head *bh = journal->j_sb_buffer; 930470decc6SDave Kleikamp 931470decc6SDave Kleikamp /* 932470decc6SDave Kleikamp * As a special case, if the on-disk copy is already marked as needing 933470decc6SDave Kleikamp * no recovery (s_start == 0) and there are no outstanding transactions 934470decc6SDave Kleikamp * in the filesystem, then we can safely defer the superblock update 935f7f4bccbSMingming Cao * until the next commit by setting JBD2_FLUSHED. This avoids 936470decc6SDave Kleikamp * attempting a write to a potential-readonly device. 937470decc6SDave Kleikamp */ 938470decc6SDave Kleikamp if (sb->s_start == 0 && journal->j_tail_sequence == 939470decc6SDave Kleikamp journal->j_transaction_sequence) { 940470decc6SDave Kleikamp jbd_debug(1,"JBD: Skipping superblock update on recovered sb " 941470decc6SDave Kleikamp "(start %ld, seq %d, errno %d)\n", 942470decc6SDave Kleikamp journal->j_tail, journal->j_tail_sequence, 943470decc6SDave Kleikamp journal->j_errno); 944470decc6SDave Kleikamp goto out; 945470decc6SDave Kleikamp } 946470decc6SDave Kleikamp 947470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 948470decc6SDave Kleikamp jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n", 949470decc6SDave Kleikamp journal->j_tail, journal->j_tail_sequence, journal->j_errno); 950470decc6SDave Kleikamp 951470decc6SDave Kleikamp sb->s_sequence = cpu_to_be32(journal->j_tail_sequence); 952470decc6SDave Kleikamp sb->s_start = cpu_to_be32(journal->j_tail); 953470decc6SDave Kleikamp sb->s_errno = cpu_to_be32(journal->j_errno); 954470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 955470decc6SDave Kleikamp 956470decc6SDave Kleikamp BUFFER_TRACE(bh, "marking dirty"); 957470decc6SDave Kleikamp mark_buffer_dirty(bh); 958470decc6SDave Kleikamp if (wait) 959470decc6SDave Kleikamp sync_dirty_buffer(bh); 960470decc6SDave Kleikamp else 961470decc6SDave Kleikamp ll_rw_block(SWRITE, 1, &bh); 962470decc6SDave Kleikamp 963470decc6SDave Kleikamp out: 964470decc6SDave Kleikamp /* If we have just flushed the log (by marking s_start==0), then 965470decc6SDave Kleikamp * any future commit will have to be careful to update the 966470decc6SDave Kleikamp * superblock again to re-record the true start of the log. */ 967470decc6SDave Kleikamp 968470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 969470decc6SDave Kleikamp if (sb->s_start) 970f7f4bccbSMingming Cao journal->j_flags &= ~JBD2_FLUSHED; 971470decc6SDave Kleikamp else 972f7f4bccbSMingming Cao journal->j_flags |= JBD2_FLUSHED; 973470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 974470decc6SDave Kleikamp } 975470decc6SDave Kleikamp 976470decc6SDave Kleikamp /* 977470decc6SDave Kleikamp * Read the superblock for a given journal, performing initial 978470decc6SDave Kleikamp * validation of the format. 979470decc6SDave Kleikamp */ 980470decc6SDave Kleikamp 981470decc6SDave Kleikamp static int journal_get_superblock(journal_t *journal) 982470decc6SDave Kleikamp { 983470decc6SDave Kleikamp struct buffer_head *bh; 984470decc6SDave Kleikamp journal_superblock_t *sb; 985470decc6SDave Kleikamp int err = -EIO; 986470decc6SDave Kleikamp 987470decc6SDave Kleikamp bh = journal->j_sb_buffer; 988470decc6SDave Kleikamp 989470decc6SDave Kleikamp J_ASSERT(bh != NULL); 990470decc6SDave Kleikamp if (!buffer_uptodate(bh)) { 991470decc6SDave Kleikamp ll_rw_block(READ, 1, &bh); 992470decc6SDave Kleikamp wait_on_buffer(bh); 993470decc6SDave Kleikamp if (!buffer_uptodate(bh)) { 994470decc6SDave Kleikamp printk (KERN_ERR 995470decc6SDave Kleikamp "JBD: IO error reading journal superblock\n"); 996470decc6SDave Kleikamp goto out; 997470decc6SDave Kleikamp } 998470decc6SDave Kleikamp } 999470decc6SDave Kleikamp 1000470decc6SDave Kleikamp sb = journal->j_superblock; 1001470decc6SDave Kleikamp 1002470decc6SDave Kleikamp err = -EINVAL; 1003470decc6SDave Kleikamp 1004f7f4bccbSMingming Cao if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) || 1005470decc6SDave Kleikamp sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) { 1006470decc6SDave Kleikamp printk(KERN_WARNING "JBD: no valid journal superblock found\n"); 1007470decc6SDave Kleikamp goto out; 1008470decc6SDave Kleikamp } 1009470decc6SDave Kleikamp 1010470decc6SDave Kleikamp switch(be32_to_cpu(sb->s_header.h_blocktype)) { 1011f7f4bccbSMingming Cao case JBD2_SUPERBLOCK_V1: 1012470decc6SDave Kleikamp journal->j_format_version = 1; 1013470decc6SDave Kleikamp break; 1014f7f4bccbSMingming Cao case JBD2_SUPERBLOCK_V2: 1015470decc6SDave Kleikamp journal->j_format_version = 2; 1016470decc6SDave Kleikamp break; 1017470decc6SDave Kleikamp default: 1018470decc6SDave Kleikamp printk(KERN_WARNING "JBD: unrecognised superblock format ID\n"); 1019470decc6SDave Kleikamp goto out; 1020470decc6SDave Kleikamp } 1021470decc6SDave Kleikamp 1022470decc6SDave Kleikamp if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen) 1023470decc6SDave Kleikamp journal->j_maxlen = be32_to_cpu(sb->s_maxlen); 1024470decc6SDave Kleikamp else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) { 1025470decc6SDave Kleikamp printk (KERN_WARNING "JBD: journal file too short\n"); 1026470decc6SDave Kleikamp goto out; 1027470decc6SDave Kleikamp } 1028470decc6SDave Kleikamp 1029470decc6SDave Kleikamp return 0; 1030470decc6SDave Kleikamp 1031470decc6SDave Kleikamp out: 1032470decc6SDave Kleikamp journal_fail_superblock(journal); 1033470decc6SDave Kleikamp return err; 1034470decc6SDave Kleikamp } 1035470decc6SDave Kleikamp 1036470decc6SDave Kleikamp /* 1037470decc6SDave Kleikamp * Load the on-disk journal superblock and read the key fields into the 1038470decc6SDave Kleikamp * journal_t. 1039470decc6SDave Kleikamp */ 1040470decc6SDave Kleikamp 1041470decc6SDave Kleikamp static int load_superblock(journal_t *journal) 1042470decc6SDave Kleikamp { 1043470decc6SDave Kleikamp int err; 1044470decc6SDave Kleikamp journal_superblock_t *sb; 1045470decc6SDave Kleikamp 1046470decc6SDave Kleikamp err = journal_get_superblock(journal); 1047470decc6SDave Kleikamp if (err) 1048470decc6SDave Kleikamp return err; 1049470decc6SDave Kleikamp 1050470decc6SDave Kleikamp sb = journal->j_superblock; 1051470decc6SDave Kleikamp 1052470decc6SDave Kleikamp journal->j_tail_sequence = be32_to_cpu(sb->s_sequence); 1053470decc6SDave Kleikamp journal->j_tail = be32_to_cpu(sb->s_start); 1054470decc6SDave Kleikamp journal->j_first = be32_to_cpu(sb->s_first); 1055470decc6SDave Kleikamp journal->j_last = be32_to_cpu(sb->s_maxlen); 1056470decc6SDave Kleikamp journal->j_errno = be32_to_cpu(sb->s_errno); 1057470decc6SDave Kleikamp 1058470decc6SDave Kleikamp return 0; 1059470decc6SDave Kleikamp } 1060470decc6SDave Kleikamp 1061470decc6SDave Kleikamp 1062470decc6SDave Kleikamp /** 1063f7f4bccbSMingming Cao * int jbd2_journal_load() - Read journal from disk. 1064470decc6SDave Kleikamp * @journal: Journal to act on. 1065470decc6SDave Kleikamp * 1066470decc6SDave Kleikamp * Given a journal_t structure which tells us which disk blocks contain 1067470decc6SDave Kleikamp * a journal, read the journal from disk to initialise the in-memory 1068470decc6SDave Kleikamp * structures. 1069470decc6SDave Kleikamp */ 1070f7f4bccbSMingming Cao int jbd2_journal_load(journal_t *journal) 1071470decc6SDave Kleikamp { 1072470decc6SDave Kleikamp int err; 1073470decc6SDave Kleikamp journal_superblock_t *sb; 1074470decc6SDave Kleikamp 1075470decc6SDave Kleikamp err = load_superblock(journal); 1076470decc6SDave Kleikamp if (err) 1077470decc6SDave Kleikamp return err; 1078470decc6SDave Kleikamp 1079470decc6SDave Kleikamp sb = journal->j_superblock; 1080470decc6SDave Kleikamp /* If this is a V2 superblock, then we have to check the 1081470decc6SDave Kleikamp * features flags on it. */ 1082470decc6SDave Kleikamp 1083470decc6SDave Kleikamp if (journal->j_format_version >= 2) { 1084470decc6SDave Kleikamp if ((sb->s_feature_ro_compat & 1085f7f4bccbSMingming Cao ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) || 1086470decc6SDave Kleikamp (sb->s_feature_incompat & 1087f7f4bccbSMingming Cao ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) { 1088470decc6SDave Kleikamp printk (KERN_WARNING 1089470decc6SDave Kleikamp "JBD: Unrecognised features on journal\n"); 1090470decc6SDave Kleikamp return -EINVAL; 1091470decc6SDave Kleikamp } 1092470decc6SDave Kleikamp } 1093470decc6SDave Kleikamp 1094470decc6SDave Kleikamp /* 1095470decc6SDave Kleikamp * Create a slab for this blocksize 1096470decc6SDave Kleikamp */ 1097f7f4bccbSMingming Cao err = jbd2_journal_create_jbd_slab(be32_to_cpu(sb->s_blocksize)); 1098470decc6SDave Kleikamp if (err) 1099470decc6SDave Kleikamp return err; 1100470decc6SDave Kleikamp 1101470decc6SDave Kleikamp /* Let the recovery code check whether it needs to recover any 1102470decc6SDave Kleikamp * data from the journal. */ 1103f7f4bccbSMingming Cao if (jbd2_journal_recover(journal)) 1104470decc6SDave Kleikamp goto recovery_error; 1105470decc6SDave Kleikamp 1106470decc6SDave Kleikamp /* OK, we've finished with the dynamic journal bits: 1107470decc6SDave Kleikamp * reinitialise the dynamic contents of the superblock in memory 1108470decc6SDave Kleikamp * and reset them on disk. */ 1109470decc6SDave Kleikamp if (journal_reset(journal)) 1110470decc6SDave Kleikamp goto recovery_error; 1111470decc6SDave Kleikamp 1112f7f4bccbSMingming Cao journal->j_flags &= ~JBD2_ABORT; 1113f7f4bccbSMingming Cao journal->j_flags |= JBD2_LOADED; 1114470decc6SDave Kleikamp return 0; 1115470decc6SDave Kleikamp 1116470decc6SDave Kleikamp recovery_error: 1117470decc6SDave Kleikamp printk (KERN_WARNING "JBD: recovery failed\n"); 1118470decc6SDave Kleikamp return -EIO; 1119470decc6SDave Kleikamp } 1120470decc6SDave Kleikamp 1121470decc6SDave Kleikamp /** 1122f7f4bccbSMingming Cao * void jbd2_journal_destroy() - Release a journal_t structure. 1123470decc6SDave Kleikamp * @journal: Journal to act on. 1124470decc6SDave Kleikamp * 1125470decc6SDave Kleikamp * Release a journal_t structure once it is no longer in use by the 1126470decc6SDave Kleikamp * journaled object. 1127470decc6SDave Kleikamp */ 1128f7f4bccbSMingming Cao void jbd2_journal_destroy(journal_t *journal) 1129470decc6SDave Kleikamp { 1130470decc6SDave Kleikamp /* Wait for the commit thread to wake up and die. */ 1131470decc6SDave Kleikamp journal_kill_thread(journal); 1132470decc6SDave Kleikamp 1133470decc6SDave Kleikamp /* Force a final log commit */ 1134470decc6SDave Kleikamp if (journal->j_running_transaction) 1135f7f4bccbSMingming Cao jbd2_journal_commit_transaction(journal); 1136470decc6SDave Kleikamp 1137470decc6SDave Kleikamp /* Force any old transactions to disk */ 1138470decc6SDave Kleikamp 1139470decc6SDave Kleikamp /* Totally anal locking here... */ 1140470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 1141470decc6SDave Kleikamp while (journal->j_checkpoint_transactions != NULL) { 1142470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 1143f7f4bccbSMingming Cao jbd2_log_do_checkpoint(journal); 1144470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 1145470decc6SDave Kleikamp } 1146470decc6SDave Kleikamp 1147470decc6SDave Kleikamp J_ASSERT(journal->j_running_transaction == NULL); 1148470decc6SDave Kleikamp J_ASSERT(journal->j_committing_transaction == NULL); 1149470decc6SDave Kleikamp J_ASSERT(journal->j_checkpoint_transactions == NULL); 1150470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 1151470decc6SDave Kleikamp 1152470decc6SDave Kleikamp /* We can now mark the journal as empty. */ 1153470decc6SDave Kleikamp journal->j_tail = 0; 1154470decc6SDave Kleikamp journal->j_tail_sequence = ++journal->j_transaction_sequence; 1155470decc6SDave Kleikamp if (journal->j_sb_buffer) { 1156f7f4bccbSMingming Cao jbd2_journal_update_superblock(journal, 1); 1157470decc6SDave Kleikamp brelse(journal->j_sb_buffer); 1158470decc6SDave Kleikamp } 1159470decc6SDave Kleikamp 1160470decc6SDave Kleikamp if (journal->j_inode) 1161470decc6SDave Kleikamp iput(journal->j_inode); 1162470decc6SDave Kleikamp if (journal->j_revoke) 1163f7f4bccbSMingming Cao jbd2_journal_destroy_revoke(journal); 1164470decc6SDave Kleikamp kfree(journal->j_wbuf); 1165470decc6SDave Kleikamp kfree(journal); 1166470decc6SDave Kleikamp } 1167470decc6SDave Kleikamp 1168470decc6SDave Kleikamp 1169470decc6SDave Kleikamp /** 1170f7f4bccbSMingming Cao *int jbd2_journal_check_used_features () - Check if features specified are used. 1171470decc6SDave Kleikamp * @journal: Journal to check. 1172470decc6SDave Kleikamp * @compat: bitmask of compatible features 1173470decc6SDave Kleikamp * @ro: bitmask of features that force read-only mount 1174470decc6SDave Kleikamp * @incompat: bitmask of incompatible features 1175470decc6SDave Kleikamp * 1176470decc6SDave Kleikamp * Check whether the journal uses all of a given set of 1177470decc6SDave Kleikamp * features. Return true (non-zero) if it does. 1178470decc6SDave Kleikamp **/ 1179470decc6SDave Kleikamp 1180f7f4bccbSMingming Cao int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat, 1181470decc6SDave Kleikamp unsigned long ro, unsigned long incompat) 1182470decc6SDave Kleikamp { 1183470decc6SDave Kleikamp journal_superblock_t *sb; 1184470decc6SDave Kleikamp 1185470decc6SDave Kleikamp if (!compat && !ro && !incompat) 1186470decc6SDave Kleikamp return 1; 1187470decc6SDave Kleikamp if (journal->j_format_version == 1) 1188470decc6SDave Kleikamp return 0; 1189470decc6SDave Kleikamp 1190470decc6SDave Kleikamp sb = journal->j_superblock; 1191470decc6SDave Kleikamp 1192470decc6SDave Kleikamp if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) && 1193470decc6SDave Kleikamp ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) && 1194470decc6SDave Kleikamp ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat)) 1195470decc6SDave Kleikamp return 1; 1196470decc6SDave Kleikamp 1197470decc6SDave Kleikamp return 0; 1198470decc6SDave Kleikamp } 1199470decc6SDave Kleikamp 1200470decc6SDave Kleikamp /** 1201f7f4bccbSMingming Cao * int jbd2_journal_check_available_features() - Check feature set in journalling layer 1202470decc6SDave Kleikamp * @journal: Journal to check. 1203470decc6SDave Kleikamp * @compat: bitmask of compatible features 1204470decc6SDave Kleikamp * @ro: bitmask of features that force read-only mount 1205470decc6SDave Kleikamp * @incompat: bitmask of incompatible features 1206470decc6SDave Kleikamp * 1207470decc6SDave Kleikamp * Check whether the journaling code supports the use of 1208470decc6SDave Kleikamp * all of a given set of features on this journal. Return true 1209470decc6SDave Kleikamp * (non-zero) if it can. */ 1210470decc6SDave Kleikamp 1211f7f4bccbSMingming Cao int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat, 1212470decc6SDave Kleikamp unsigned long ro, unsigned long incompat) 1213470decc6SDave Kleikamp { 1214470decc6SDave Kleikamp journal_superblock_t *sb; 1215470decc6SDave Kleikamp 1216470decc6SDave Kleikamp if (!compat && !ro && !incompat) 1217470decc6SDave Kleikamp return 1; 1218470decc6SDave Kleikamp 1219470decc6SDave Kleikamp sb = journal->j_superblock; 1220470decc6SDave Kleikamp 1221470decc6SDave Kleikamp /* We can support any known requested features iff the 1222470decc6SDave Kleikamp * superblock is in version 2. Otherwise we fail to support any 1223470decc6SDave Kleikamp * extended sb features. */ 1224470decc6SDave Kleikamp 1225470decc6SDave Kleikamp if (journal->j_format_version != 2) 1226470decc6SDave Kleikamp return 0; 1227470decc6SDave Kleikamp 1228f7f4bccbSMingming Cao if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat && 1229f7f4bccbSMingming Cao (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro && 1230f7f4bccbSMingming Cao (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat) 1231470decc6SDave Kleikamp return 1; 1232470decc6SDave Kleikamp 1233470decc6SDave Kleikamp return 0; 1234470decc6SDave Kleikamp } 1235470decc6SDave Kleikamp 1236470decc6SDave Kleikamp /** 1237f7f4bccbSMingming Cao * int jbd2_journal_set_features () - Mark a given journal feature in the superblock 1238470decc6SDave Kleikamp * @journal: Journal to act on. 1239470decc6SDave Kleikamp * @compat: bitmask of compatible features 1240470decc6SDave Kleikamp * @ro: bitmask of features that force read-only mount 1241470decc6SDave Kleikamp * @incompat: bitmask of incompatible features 1242470decc6SDave Kleikamp * 1243470decc6SDave Kleikamp * Mark a given journal feature as present on the 1244470decc6SDave Kleikamp * superblock. Returns true if the requested features could be set. 1245470decc6SDave Kleikamp * 1246470decc6SDave Kleikamp */ 1247470decc6SDave Kleikamp 1248f7f4bccbSMingming Cao int jbd2_journal_set_features (journal_t *journal, unsigned long compat, 1249470decc6SDave Kleikamp unsigned long ro, unsigned long incompat) 1250470decc6SDave Kleikamp { 1251470decc6SDave Kleikamp journal_superblock_t *sb; 1252470decc6SDave Kleikamp 1253f7f4bccbSMingming Cao if (jbd2_journal_check_used_features(journal, compat, ro, incompat)) 1254470decc6SDave Kleikamp return 1; 1255470decc6SDave Kleikamp 1256f7f4bccbSMingming Cao if (!jbd2_journal_check_available_features(journal, compat, ro, incompat)) 1257470decc6SDave Kleikamp return 0; 1258470decc6SDave Kleikamp 1259470decc6SDave Kleikamp jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n", 1260470decc6SDave Kleikamp compat, ro, incompat); 1261470decc6SDave Kleikamp 1262470decc6SDave Kleikamp sb = journal->j_superblock; 1263470decc6SDave Kleikamp 1264470decc6SDave Kleikamp sb->s_feature_compat |= cpu_to_be32(compat); 1265470decc6SDave Kleikamp sb->s_feature_ro_compat |= cpu_to_be32(ro); 1266470decc6SDave Kleikamp sb->s_feature_incompat |= cpu_to_be32(incompat); 1267470decc6SDave Kleikamp 1268470decc6SDave Kleikamp return 1; 1269470decc6SDave Kleikamp } 1270470decc6SDave Kleikamp 1271470decc6SDave Kleikamp 1272470decc6SDave Kleikamp /** 1273f7f4bccbSMingming Cao * int jbd2_journal_update_format () - Update on-disk journal structure. 1274470decc6SDave Kleikamp * @journal: Journal to act on. 1275470decc6SDave Kleikamp * 1276470decc6SDave Kleikamp * Given an initialised but unloaded journal struct, poke about in the 1277470decc6SDave Kleikamp * on-disk structure to update it to the most recent supported version. 1278470decc6SDave Kleikamp */ 1279f7f4bccbSMingming Cao int jbd2_journal_update_format (journal_t *journal) 1280470decc6SDave Kleikamp { 1281470decc6SDave Kleikamp journal_superblock_t *sb; 1282470decc6SDave Kleikamp int err; 1283470decc6SDave Kleikamp 1284470decc6SDave Kleikamp err = journal_get_superblock(journal); 1285470decc6SDave Kleikamp if (err) 1286470decc6SDave Kleikamp return err; 1287470decc6SDave Kleikamp 1288470decc6SDave Kleikamp sb = journal->j_superblock; 1289470decc6SDave Kleikamp 1290470decc6SDave Kleikamp switch (be32_to_cpu(sb->s_header.h_blocktype)) { 1291f7f4bccbSMingming Cao case JBD2_SUPERBLOCK_V2: 1292470decc6SDave Kleikamp return 0; 1293f7f4bccbSMingming Cao case JBD2_SUPERBLOCK_V1: 1294470decc6SDave Kleikamp return journal_convert_superblock_v1(journal, sb); 1295470decc6SDave Kleikamp default: 1296470decc6SDave Kleikamp break; 1297470decc6SDave Kleikamp } 1298470decc6SDave Kleikamp return -EINVAL; 1299470decc6SDave Kleikamp } 1300470decc6SDave Kleikamp 1301470decc6SDave Kleikamp static int journal_convert_superblock_v1(journal_t *journal, 1302470decc6SDave Kleikamp journal_superblock_t *sb) 1303470decc6SDave Kleikamp { 1304470decc6SDave Kleikamp int offset, blocksize; 1305470decc6SDave Kleikamp struct buffer_head *bh; 1306470decc6SDave Kleikamp 1307470decc6SDave Kleikamp printk(KERN_WARNING 1308470decc6SDave Kleikamp "JBD: Converting superblock from version 1 to 2.\n"); 1309470decc6SDave Kleikamp 1310470decc6SDave Kleikamp /* Pre-initialise new fields to zero */ 1311470decc6SDave Kleikamp offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb); 1312470decc6SDave Kleikamp blocksize = be32_to_cpu(sb->s_blocksize); 1313470decc6SDave Kleikamp memset(&sb->s_feature_compat, 0, blocksize-offset); 1314470decc6SDave Kleikamp 1315470decc6SDave Kleikamp sb->s_nr_users = cpu_to_be32(1); 1316f7f4bccbSMingming Cao sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2); 1317470decc6SDave Kleikamp journal->j_format_version = 2; 1318470decc6SDave Kleikamp 1319470decc6SDave Kleikamp bh = journal->j_sb_buffer; 1320470decc6SDave Kleikamp BUFFER_TRACE(bh, "marking dirty"); 1321470decc6SDave Kleikamp mark_buffer_dirty(bh); 1322470decc6SDave Kleikamp sync_dirty_buffer(bh); 1323470decc6SDave Kleikamp return 0; 1324470decc6SDave Kleikamp } 1325470decc6SDave Kleikamp 1326470decc6SDave Kleikamp 1327470decc6SDave Kleikamp /** 1328f7f4bccbSMingming Cao * int jbd2_journal_flush () - Flush journal 1329470decc6SDave Kleikamp * @journal: Journal to act on. 1330470decc6SDave Kleikamp * 1331470decc6SDave Kleikamp * Flush all data for a given journal to disk and empty the journal. 1332470decc6SDave Kleikamp * Filesystems can use this when remounting readonly to ensure that 1333470decc6SDave Kleikamp * recovery does not need to happen on remount. 1334470decc6SDave Kleikamp */ 1335470decc6SDave Kleikamp 1336f7f4bccbSMingming Cao int jbd2_journal_flush(journal_t *journal) 1337470decc6SDave Kleikamp { 1338470decc6SDave Kleikamp int err = 0; 1339470decc6SDave Kleikamp transaction_t *transaction = NULL; 1340470decc6SDave Kleikamp unsigned long old_tail; 1341470decc6SDave Kleikamp 1342470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 1343470decc6SDave Kleikamp 1344470decc6SDave Kleikamp /* Force everything buffered to the log... */ 1345470decc6SDave Kleikamp if (journal->j_running_transaction) { 1346470decc6SDave Kleikamp transaction = journal->j_running_transaction; 1347f7f4bccbSMingming Cao __jbd2_log_start_commit(journal, transaction->t_tid); 1348470decc6SDave Kleikamp } else if (journal->j_committing_transaction) 1349470decc6SDave Kleikamp transaction = journal->j_committing_transaction; 1350470decc6SDave Kleikamp 1351470decc6SDave Kleikamp /* Wait for the log commit to complete... */ 1352470decc6SDave Kleikamp if (transaction) { 1353470decc6SDave Kleikamp tid_t tid = transaction->t_tid; 1354470decc6SDave Kleikamp 1355470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1356f7f4bccbSMingming Cao jbd2_log_wait_commit(journal, tid); 1357470decc6SDave Kleikamp } else { 1358470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1359470decc6SDave Kleikamp } 1360470decc6SDave Kleikamp 1361470decc6SDave Kleikamp /* ...and flush everything in the log out to disk. */ 1362470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 1363470decc6SDave Kleikamp while (!err && journal->j_checkpoint_transactions != NULL) { 1364470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 1365f7f4bccbSMingming Cao err = jbd2_log_do_checkpoint(journal); 1366470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 1367470decc6SDave Kleikamp } 1368470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 1369f7f4bccbSMingming Cao jbd2_cleanup_journal_tail(journal); 1370470decc6SDave Kleikamp 1371470decc6SDave Kleikamp /* Finally, mark the journal as really needing no recovery. 1372470decc6SDave Kleikamp * This sets s_start==0 in the underlying superblock, which is 1373470decc6SDave Kleikamp * the magic code for a fully-recovered superblock. Any future 1374470decc6SDave Kleikamp * commits of data to the journal will restore the current 1375470decc6SDave Kleikamp * s_start value. */ 1376470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 1377470decc6SDave Kleikamp old_tail = journal->j_tail; 1378470decc6SDave Kleikamp journal->j_tail = 0; 1379470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1380f7f4bccbSMingming Cao jbd2_journal_update_superblock(journal, 1); 1381470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 1382470decc6SDave Kleikamp journal->j_tail = old_tail; 1383470decc6SDave Kleikamp 1384470decc6SDave Kleikamp J_ASSERT(!journal->j_running_transaction); 1385470decc6SDave Kleikamp J_ASSERT(!journal->j_committing_transaction); 1386470decc6SDave Kleikamp J_ASSERT(!journal->j_checkpoint_transactions); 1387470decc6SDave Kleikamp J_ASSERT(journal->j_head == journal->j_tail); 1388470decc6SDave Kleikamp J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence); 1389470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1390470decc6SDave Kleikamp return err; 1391470decc6SDave Kleikamp } 1392470decc6SDave Kleikamp 1393470decc6SDave Kleikamp /** 1394f7f4bccbSMingming Cao * int jbd2_journal_wipe() - Wipe journal contents 1395470decc6SDave Kleikamp * @journal: Journal to act on. 1396470decc6SDave Kleikamp * @write: flag (see below) 1397470decc6SDave Kleikamp * 1398470decc6SDave Kleikamp * Wipe out all of the contents of a journal, safely. This will produce 1399470decc6SDave Kleikamp * a warning if the journal contains any valid recovery information. 1400f7f4bccbSMingming Cao * Must be called between journal_init_*() and jbd2_journal_load(). 1401470decc6SDave Kleikamp * 1402470decc6SDave Kleikamp * If 'write' is non-zero, then we wipe out the journal on disk; otherwise 1403470decc6SDave Kleikamp * we merely suppress recovery. 1404470decc6SDave Kleikamp */ 1405470decc6SDave Kleikamp 1406f7f4bccbSMingming Cao int jbd2_journal_wipe(journal_t *journal, int write) 1407470decc6SDave Kleikamp { 1408470decc6SDave Kleikamp journal_superblock_t *sb; 1409470decc6SDave Kleikamp int err = 0; 1410470decc6SDave Kleikamp 1411f7f4bccbSMingming Cao J_ASSERT (!(journal->j_flags & JBD2_LOADED)); 1412470decc6SDave Kleikamp 1413470decc6SDave Kleikamp err = load_superblock(journal); 1414470decc6SDave Kleikamp if (err) 1415470decc6SDave Kleikamp return err; 1416470decc6SDave Kleikamp 1417470decc6SDave Kleikamp sb = journal->j_superblock; 1418470decc6SDave Kleikamp 1419470decc6SDave Kleikamp if (!journal->j_tail) 1420470decc6SDave Kleikamp goto no_recovery; 1421470decc6SDave Kleikamp 1422470decc6SDave Kleikamp printk (KERN_WARNING "JBD: %s recovery information on journal\n", 1423470decc6SDave Kleikamp write ? "Clearing" : "Ignoring"); 1424470decc6SDave Kleikamp 1425f7f4bccbSMingming Cao err = jbd2_journal_skip_recovery(journal); 1426470decc6SDave Kleikamp if (write) 1427f7f4bccbSMingming Cao jbd2_journal_update_superblock(journal, 1); 1428470decc6SDave Kleikamp 1429470decc6SDave Kleikamp no_recovery: 1430470decc6SDave Kleikamp return err; 1431470decc6SDave Kleikamp } 1432470decc6SDave Kleikamp 1433470decc6SDave Kleikamp /* 1434470decc6SDave Kleikamp * journal_dev_name: format a character string to describe on what 1435470decc6SDave Kleikamp * device this journal is present. 1436470decc6SDave Kleikamp */ 1437470decc6SDave Kleikamp 1438470decc6SDave Kleikamp static const char *journal_dev_name(journal_t *journal, char *buffer) 1439470decc6SDave Kleikamp { 1440470decc6SDave Kleikamp struct block_device *bdev; 1441470decc6SDave Kleikamp 1442470decc6SDave Kleikamp if (journal->j_inode) 1443470decc6SDave Kleikamp bdev = journal->j_inode->i_sb->s_bdev; 1444470decc6SDave Kleikamp else 1445470decc6SDave Kleikamp bdev = journal->j_dev; 1446470decc6SDave Kleikamp 1447470decc6SDave Kleikamp return bdevname(bdev, buffer); 1448470decc6SDave Kleikamp } 1449470decc6SDave Kleikamp 1450470decc6SDave Kleikamp /* 1451470decc6SDave Kleikamp * Journal abort has very specific semantics, which we describe 1452470decc6SDave Kleikamp * for journal abort. 1453470decc6SDave Kleikamp * 1454470decc6SDave Kleikamp * Two internal function, which provide abort to te jbd layer 1455470decc6SDave Kleikamp * itself are here. 1456470decc6SDave Kleikamp */ 1457470decc6SDave Kleikamp 1458470decc6SDave Kleikamp /* 1459470decc6SDave Kleikamp * Quick version for internal journal use (doesn't lock the journal). 1460470decc6SDave Kleikamp * Aborts hard --- we mark the abort as occurred, but do _nothing_ else, 1461470decc6SDave Kleikamp * and don't attempt to make any other journal updates. 1462470decc6SDave Kleikamp */ 1463f7f4bccbSMingming Cao void __jbd2_journal_abort_hard(journal_t *journal) 1464470decc6SDave Kleikamp { 1465470decc6SDave Kleikamp transaction_t *transaction; 1466470decc6SDave Kleikamp char b[BDEVNAME_SIZE]; 1467470decc6SDave Kleikamp 1468f7f4bccbSMingming Cao if (journal->j_flags & JBD2_ABORT) 1469470decc6SDave Kleikamp return; 1470470decc6SDave Kleikamp 1471470decc6SDave Kleikamp printk(KERN_ERR "Aborting journal on device %s.\n", 1472470decc6SDave Kleikamp journal_dev_name(journal, b)); 1473470decc6SDave Kleikamp 1474470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 1475f7f4bccbSMingming Cao journal->j_flags |= JBD2_ABORT; 1476470decc6SDave Kleikamp transaction = journal->j_running_transaction; 1477470decc6SDave Kleikamp if (transaction) 1478f7f4bccbSMingming Cao __jbd2_log_start_commit(journal, transaction->t_tid); 1479470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1480470decc6SDave Kleikamp } 1481470decc6SDave Kleikamp 1482470decc6SDave Kleikamp /* Soft abort: record the abort error status in the journal superblock, 1483470decc6SDave Kleikamp * but don't do any other IO. */ 1484470decc6SDave Kleikamp static void __journal_abort_soft (journal_t *journal, int errno) 1485470decc6SDave Kleikamp { 1486f7f4bccbSMingming Cao if (journal->j_flags & JBD2_ABORT) 1487470decc6SDave Kleikamp return; 1488470decc6SDave Kleikamp 1489470decc6SDave Kleikamp if (!journal->j_errno) 1490470decc6SDave Kleikamp journal->j_errno = errno; 1491470decc6SDave Kleikamp 1492f7f4bccbSMingming Cao __jbd2_journal_abort_hard(journal); 1493470decc6SDave Kleikamp 1494470decc6SDave Kleikamp if (errno) 1495f7f4bccbSMingming Cao jbd2_journal_update_superblock(journal, 1); 1496470decc6SDave Kleikamp } 1497470decc6SDave Kleikamp 1498470decc6SDave Kleikamp /** 1499f7f4bccbSMingming Cao * void jbd2_journal_abort () - Shutdown the journal immediately. 1500470decc6SDave Kleikamp * @journal: the journal to shutdown. 1501470decc6SDave Kleikamp * @errno: an error number to record in the journal indicating 1502470decc6SDave Kleikamp * the reason for the shutdown. 1503470decc6SDave Kleikamp * 1504470decc6SDave Kleikamp * Perform a complete, immediate shutdown of the ENTIRE 1505470decc6SDave Kleikamp * journal (not of a single transaction). This operation cannot be 1506470decc6SDave Kleikamp * undone without closing and reopening the journal. 1507470decc6SDave Kleikamp * 1508f7f4bccbSMingming Cao * The jbd2_journal_abort function is intended to support higher level error 1509470decc6SDave Kleikamp * recovery mechanisms such as the ext2/ext3 remount-readonly error 1510470decc6SDave Kleikamp * mode. 1511470decc6SDave Kleikamp * 1512470decc6SDave Kleikamp * Journal abort has very specific semantics. Any existing dirty, 1513470decc6SDave Kleikamp * unjournaled buffers in the main filesystem will still be written to 1514470decc6SDave Kleikamp * disk by bdflush, but the journaling mechanism will be suspended 1515470decc6SDave Kleikamp * immediately and no further transaction commits will be honoured. 1516470decc6SDave Kleikamp * 1517470decc6SDave Kleikamp * Any dirty, journaled buffers will be written back to disk without 1518470decc6SDave Kleikamp * hitting the journal. Atomicity cannot be guaranteed on an aborted 1519470decc6SDave Kleikamp * filesystem, but we _do_ attempt to leave as much data as possible 1520470decc6SDave Kleikamp * behind for fsck to use for cleanup. 1521470decc6SDave Kleikamp * 1522470decc6SDave Kleikamp * Any attempt to get a new transaction handle on a journal which is in 1523470decc6SDave Kleikamp * ABORT state will just result in an -EROFS error return. A 1524f7f4bccbSMingming Cao * jbd2_journal_stop on an existing handle will return -EIO if we have 1525470decc6SDave Kleikamp * entered abort state during the update. 1526470decc6SDave Kleikamp * 1527470decc6SDave Kleikamp * Recursive transactions are not disturbed by journal abort until the 1528f7f4bccbSMingming Cao * final jbd2_journal_stop, which will receive the -EIO error. 1529470decc6SDave Kleikamp * 1530f7f4bccbSMingming Cao * Finally, the jbd2_journal_abort call allows the caller to supply an errno 1531470decc6SDave Kleikamp * which will be recorded (if possible) in the journal superblock. This 1532470decc6SDave Kleikamp * allows a client to record failure conditions in the middle of a 1533470decc6SDave Kleikamp * transaction without having to complete the transaction to record the 1534470decc6SDave Kleikamp * failure to disk. ext3_error, for example, now uses this 1535470decc6SDave Kleikamp * functionality. 1536470decc6SDave Kleikamp * 1537470decc6SDave Kleikamp * Errors which originate from within the journaling layer will NOT 1538470decc6SDave Kleikamp * supply an errno; a null errno implies that absolutely no further 1539470decc6SDave Kleikamp * writes are done to the journal (unless there are any already in 1540470decc6SDave Kleikamp * progress). 1541470decc6SDave Kleikamp * 1542470decc6SDave Kleikamp */ 1543470decc6SDave Kleikamp 1544f7f4bccbSMingming Cao void jbd2_journal_abort(journal_t *journal, int errno) 1545470decc6SDave Kleikamp { 1546470decc6SDave Kleikamp __journal_abort_soft(journal, errno); 1547470decc6SDave Kleikamp } 1548470decc6SDave Kleikamp 1549470decc6SDave Kleikamp /** 1550f7f4bccbSMingming Cao * int jbd2_journal_errno () - returns the journal's error state. 1551470decc6SDave Kleikamp * @journal: journal to examine. 1552470decc6SDave Kleikamp * 1553f7f4bccbSMingming Cao * This is the errno numbet set with jbd2_journal_abort(), the last 1554470decc6SDave Kleikamp * time the journal was mounted - if the journal was stopped 1555470decc6SDave Kleikamp * without calling abort this will be 0. 1556470decc6SDave Kleikamp * 1557470decc6SDave Kleikamp * If the journal has been aborted on this mount time -EROFS will 1558470decc6SDave Kleikamp * be returned. 1559470decc6SDave Kleikamp */ 1560f7f4bccbSMingming Cao int jbd2_journal_errno(journal_t *journal) 1561470decc6SDave Kleikamp { 1562470decc6SDave Kleikamp int err; 1563470decc6SDave Kleikamp 1564470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 1565f7f4bccbSMingming Cao if (journal->j_flags & JBD2_ABORT) 1566470decc6SDave Kleikamp err = -EROFS; 1567470decc6SDave Kleikamp else 1568470decc6SDave Kleikamp err = journal->j_errno; 1569470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1570470decc6SDave Kleikamp return err; 1571470decc6SDave Kleikamp } 1572470decc6SDave Kleikamp 1573470decc6SDave Kleikamp /** 1574f7f4bccbSMingming Cao * int jbd2_journal_clear_err () - clears the journal's error state 1575470decc6SDave Kleikamp * @journal: journal to act on. 1576470decc6SDave Kleikamp * 1577470decc6SDave Kleikamp * An error must be cleared or Acked to take a FS out of readonly 1578470decc6SDave Kleikamp * mode. 1579470decc6SDave Kleikamp */ 1580f7f4bccbSMingming Cao int jbd2_journal_clear_err(journal_t *journal) 1581470decc6SDave Kleikamp { 1582470decc6SDave Kleikamp int err = 0; 1583470decc6SDave Kleikamp 1584470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 1585f7f4bccbSMingming Cao if (journal->j_flags & JBD2_ABORT) 1586470decc6SDave Kleikamp err = -EROFS; 1587470decc6SDave Kleikamp else 1588470decc6SDave Kleikamp journal->j_errno = 0; 1589470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1590470decc6SDave Kleikamp return err; 1591470decc6SDave Kleikamp } 1592470decc6SDave Kleikamp 1593470decc6SDave Kleikamp /** 1594f7f4bccbSMingming Cao * void jbd2_journal_ack_err() - Ack journal err. 1595470decc6SDave Kleikamp * @journal: journal to act on. 1596470decc6SDave Kleikamp * 1597470decc6SDave Kleikamp * An error must be cleared or Acked to take a FS out of readonly 1598470decc6SDave Kleikamp * mode. 1599470decc6SDave Kleikamp */ 1600f7f4bccbSMingming Cao void jbd2_journal_ack_err(journal_t *journal) 1601470decc6SDave Kleikamp { 1602470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 1603470decc6SDave Kleikamp if (journal->j_errno) 1604f7f4bccbSMingming Cao journal->j_flags |= JBD2_ACK_ERR; 1605470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1606470decc6SDave Kleikamp } 1607470decc6SDave Kleikamp 1608f7f4bccbSMingming Cao int jbd2_journal_blocks_per_page(struct inode *inode) 1609470decc6SDave Kleikamp { 1610470decc6SDave Kleikamp return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); 1611470decc6SDave Kleikamp } 1612470decc6SDave Kleikamp 1613470decc6SDave Kleikamp /* 1614b517bea1SZach Brown * helper functions to deal with 32 or 64bit block numbers. 1615b517bea1SZach Brown */ 1616b517bea1SZach Brown size_t journal_tag_bytes(journal_t *journal) 1617b517bea1SZach Brown { 1618b517bea1SZach Brown if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT)) 1619b517bea1SZach Brown return JBD_TAG_SIZE64; 1620b517bea1SZach Brown else 1621b517bea1SZach Brown return JBD_TAG_SIZE32; 1622b517bea1SZach Brown } 1623b517bea1SZach Brown 1624b517bea1SZach Brown /* 1625470decc6SDave Kleikamp * Simple support for retrying memory allocations. Introduced to help to 1626470decc6SDave Kleikamp * debug different VM deadlock avoidance strategies. 1627470decc6SDave Kleikamp */ 1628f7f4bccbSMingming Cao void * __jbd2_kmalloc (const char *where, size_t size, gfp_t flags, int retry) 1629470decc6SDave Kleikamp { 1630470decc6SDave Kleikamp return kmalloc(size, flags | (retry ? __GFP_NOFAIL : 0)); 1631470decc6SDave Kleikamp } 1632470decc6SDave Kleikamp 1633470decc6SDave Kleikamp /* 1634470decc6SDave Kleikamp * jbd slab management: create 1k, 2k, 4k, 8k slabs as needed 1635470decc6SDave Kleikamp * and allocate frozen and commit buffers from these slabs. 1636470decc6SDave Kleikamp * 1637470decc6SDave Kleikamp * Reason for doing this is to avoid, SLAB_DEBUG - since it could 1638470decc6SDave Kleikamp * cause bh to cross page boundary. 1639470decc6SDave Kleikamp */ 1640470decc6SDave Kleikamp 1641470decc6SDave Kleikamp #define JBD_MAX_SLABS 5 1642470decc6SDave Kleikamp #define JBD_SLAB_INDEX(size) (size >> 11) 1643470decc6SDave Kleikamp 1644470decc6SDave Kleikamp static kmem_cache_t *jbd_slab[JBD_MAX_SLABS]; 1645470decc6SDave Kleikamp static const char *jbd_slab_names[JBD_MAX_SLABS] = { 1646a920e941SJohann Lombardi "jbd2_1k", "jbd2_2k", "jbd2_4k", NULL, "jbd2_8k" 1647470decc6SDave Kleikamp }; 1648470decc6SDave Kleikamp 1649f7f4bccbSMingming Cao static void jbd2_journal_destroy_jbd_slabs(void) 1650470decc6SDave Kleikamp { 1651470decc6SDave Kleikamp int i; 1652470decc6SDave Kleikamp 1653470decc6SDave Kleikamp for (i = 0; i < JBD_MAX_SLABS; i++) { 1654470decc6SDave Kleikamp if (jbd_slab[i]) 1655470decc6SDave Kleikamp kmem_cache_destroy(jbd_slab[i]); 1656470decc6SDave Kleikamp jbd_slab[i] = NULL; 1657470decc6SDave Kleikamp } 1658470decc6SDave Kleikamp } 1659470decc6SDave Kleikamp 1660f7f4bccbSMingming Cao static int jbd2_journal_create_jbd_slab(size_t slab_size) 1661470decc6SDave Kleikamp { 1662470decc6SDave Kleikamp int i = JBD_SLAB_INDEX(slab_size); 1663470decc6SDave Kleikamp 1664470decc6SDave Kleikamp BUG_ON(i >= JBD_MAX_SLABS); 1665470decc6SDave Kleikamp 1666470decc6SDave Kleikamp /* 1667470decc6SDave Kleikamp * Check if we already have a slab created for this size 1668470decc6SDave Kleikamp */ 1669470decc6SDave Kleikamp if (jbd_slab[i]) 1670470decc6SDave Kleikamp return 0; 1671470decc6SDave Kleikamp 1672470decc6SDave Kleikamp /* 1673470decc6SDave Kleikamp * Create a slab and force alignment to be same as slabsize - 1674470decc6SDave Kleikamp * this will make sure that allocations won't cross the page 1675470decc6SDave Kleikamp * boundary. 1676470decc6SDave Kleikamp */ 1677470decc6SDave Kleikamp jbd_slab[i] = kmem_cache_create(jbd_slab_names[i], 1678470decc6SDave Kleikamp slab_size, slab_size, 0, NULL, NULL); 1679470decc6SDave Kleikamp if (!jbd_slab[i]) { 1680470decc6SDave Kleikamp printk(KERN_EMERG "JBD: no memory for jbd_slab cache\n"); 1681470decc6SDave Kleikamp return -ENOMEM; 1682470decc6SDave Kleikamp } 1683470decc6SDave Kleikamp return 0; 1684470decc6SDave Kleikamp } 1685470decc6SDave Kleikamp 1686f7f4bccbSMingming Cao void * jbd2_slab_alloc(size_t size, gfp_t flags) 1687470decc6SDave Kleikamp { 1688470decc6SDave Kleikamp int idx; 1689470decc6SDave Kleikamp 1690470decc6SDave Kleikamp idx = JBD_SLAB_INDEX(size); 1691470decc6SDave Kleikamp BUG_ON(jbd_slab[idx] == NULL); 1692470decc6SDave Kleikamp return kmem_cache_alloc(jbd_slab[idx], flags | __GFP_NOFAIL); 1693470decc6SDave Kleikamp } 1694470decc6SDave Kleikamp 1695f7f4bccbSMingming Cao void jbd2_slab_free(void *ptr, size_t size) 1696470decc6SDave Kleikamp { 1697470decc6SDave Kleikamp int idx; 1698470decc6SDave Kleikamp 1699470decc6SDave Kleikamp idx = JBD_SLAB_INDEX(size); 1700470decc6SDave Kleikamp BUG_ON(jbd_slab[idx] == NULL); 1701470decc6SDave Kleikamp kmem_cache_free(jbd_slab[idx], ptr); 1702470decc6SDave Kleikamp } 1703470decc6SDave Kleikamp 1704470decc6SDave Kleikamp /* 1705470decc6SDave Kleikamp * Journal_head storage management 1706470decc6SDave Kleikamp */ 1707f7f4bccbSMingming Cao static kmem_cache_t *jbd2_journal_head_cache; 1708470decc6SDave Kleikamp #ifdef CONFIG_JBD_DEBUG 1709470decc6SDave Kleikamp static atomic_t nr_journal_heads = ATOMIC_INIT(0); 1710470decc6SDave Kleikamp #endif 1711470decc6SDave Kleikamp 1712f7f4bccbSMingming Cao static int journal_init_jbd2_journal_head_cache(void) 1713470decc6SDave Kleikamp { 1714470decc6SDave Kleikamp int retval; 1715470decc6SDave Kleikamp 1716f7f4bccbSMingming Cao J_ASSERT(jbd2_journal_head_cache == 0); 1717a920e941SJohann Lombardi jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head", 1718470decc6SDave Kleikamp sizeof(struct journal_head), 1719470decc6SDave Kleikamp 0, /* offset */ 1720470decc6SDave Kleikamp 0, /* flags */ 1721470decc6SDave Kleikamp NULL, /* ctor */ 1722470decc6SDave Kleikamp NULL); /* dtor */ 1723470decc6SDave Kleikamp retval = 0; 1724f7f4bccbSMingming Cao if (jbd2_journal_head_cache == 0) { 1725470decc6SDave Kleikamp retval = -ENOMEM; 1726470decc6SDave Kleikamp printk(KERN_EMERG "JBD: no memory for journal_head cache\n"); 1727470decc6SDave Kleikamp } 1728470decc6SDave Kleikamp return retval; 1729470decc6SDave Kleikamp } 1730470decc6SDave Kleikamp 1731f7f4bccbSMingming Cao static void jbd2_journal_destroy_jbd2_journal_head_cache(void) 1732470decc6SDave Kleikamp { 1733f7f4bccbSMingming Cao J_ASSERT(jbd2_journal_head_cache != NULL); 1734f7f4bccbSMingming Cao kmem_cache_destroy(jbd2_journal_head_cache); 1735f7f4bccbSMingming Cao jbd2_journal_head_cache = NULL; 1736470decc6SDave Kleikamp } 1737470decc6SDave Kleikamp 1738470decc6SDave Kleikamp /* 1739470decc6SDave Kleikamp * journal_head splicing and dicing 1740470decc6SDave Kleikamp */ 1741470decc6SDave Kleikamp static struct journal_head *journal_alloc_journal_head(void) 1742470decc6SDave Kleikamp { 1743470decc6SDave Kleikamp struct journal_head *ret; 1744470decc6SDave Kleikamp static unsigned long last_warning; 1745470decc6SDave Kleikamp 1746470decc6SDave Kleikamp #ifdef CONFIG_JBD_DEBUG 1747470decc6SDave Kleikamp atomic_inc(&nr_journal_heads); 1748470decc6SDave Kleikamp #endif 1749f7f4bccbSMingming Cao ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS); 1750470decc6SDave Kleikamp if (ret == 0) { 1751470decc6SDave Kleikamp jbd_debug(1, "out of memory for journal_head\n"); 1752470decc6SDave Kleikamp if (time_after(jiffies, last_warning + 5*HZ)) { 1753470decc6SDave Kleikamp printk(KERN_NOTICE "ENOMEM in %s, retrying.\n", 1754470decc6SDave Kleikamp __FUNCTION__); 1755470decc6SDave Kleikamp last_warning = jiffies; 1756470decc6SDave Kleikamp } 1757470decc6SDave Kleikamp while (ret == 0) { 1758470decc6SDave Kleikamp yield(); 1759f7f4bccbSMingming Cao ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS); 1760470decc6SDave Kleikamp } 1761470decc6SDave Kleikamp } 1762470decc6SDave Kleikamp return ret; 1763470decc6SDave Kleikamp } 1764470decc6SDave Kleikamp 1765470decc6SDave Kleikamp static void journal_free_journal_head(struct journal_head *jh) 1766470decc6SDave Kleikamp { 1767470decc6SDave Kleikamp #ifdef CONFIG_JBD_DEBUG 1768470decc6SDave Kleikamp atomic_dec(&nr_journal_heads); 1769470decc6SDave Kleikamp memset(jh, JBD_POISON_FREE, sizeof(*jh)); 1770470decc6SDave Kleikamp #endif 1771f7f4bccbSMingming Cao kmem_cache_free(jbd2_journal_head_cache, jh); 1772470decc6SDave Kleikamp } 1773470decc6SDave Kleikamp 1774470decc6SDave Kleikamp /* 1775470decc6SDave Kleikamp * A journal_head is attached to a buffer_head whenever JBD has an 1776470decc6SDave Kleikamp * interest in the buffer. 1777470decc6SDave Kleikamp * 1778470decc6SDave Kleikamp * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit 1779470decc6SDave Kleikamp * is set. This bit is tested in core kernel code where we need to take 1780470decc6SDave Kleikamp * JBD-specific actions. Testing the zeroness of ->b_private is not reliable 1781470decc6SDave Kleikamp * there. 1782470decc6SDave Kleikamp * 1783470decc6SDave Kleikamp * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one. 1784470decc6SDave Kleikamp * 1785470decc6SDave Kleikamp * When a buffer has its BH_JBD bit set it is immune from being released by 1786470decc6SDave Kleikamp * core kernel code, mainly via ->b_count. 1787470decc6SDave Kleikamp * 1788470decc6SDave Kleikamp * A journal_head may be detached from its buffer_head when the journal_head's 1789470decc6SDave Kleikamp * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL. 1790f7f4bccbSMingming Cao * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the 1791470decc6SDave Kleikamp * journal_head can be dropped if needed. 1792470decc6SDave Kleikamp * 1793470decc6SDave Kleikamp * Various places in the kernel want to attach a journal_head to a buffer_head 1794470decc6SDave Kleikamp * _before_ attaching the journal_head to a transaction. To protect the 1795f7f4bccbSMingming Cao * journal_head in this situation, jbd2_journal_add_journal_head elevates the 1796470decc6SDave Kleikamp * journal_head's b_jcount refcount by one. The caller must call 1797f7f4bccbSMingming Cao * jbd2_journal_put_journal_head() to undo this. 1798470decc6SDave Kleikamp * 1799470decc6SDave Kleikamp * So the typical usage would be: 1800470decc6SDave Kleikamp * 1801470decc6SDave Kleikamp * (Attach a journal_head if needed. Increments b_jcount) 1802f7f4bccbSMingming Cao * struct journal_head *jh = jbd2_journal_add_journal_head(bh); 1803470decc6SDave Kleikamp * ... 1804470decc6SDave Kleikamp * jh->b_transaction = xxx; 1805f7f4bccbSMingming Cao * jbd2_journal_put_journal_head(jh); 1806470decc6SDave Kleikamp * 1807470decc6SDave Kleikamp * Now, the journal_head's b_jcount is zero, but it is safe from being released 1808470decc6SDave Kleikamp * because it has a non-zero b_transaction. 1809470decc6SDave Kleikamp */ 1810470decc6SDave Kleikamp 1811470decc6SDave Kleikamp /* 1812470decc6SDave Kleikamp * Give a buffer_head a journal_head. 1813470decc6SDave Kleikamp * 1814470decc6SDave Kleikamp * Doesn't need the journal lock. 1815470decc6SDave Kleikamp * May sleep. 1816470decc6SDave Kleikamp */ 1817f7f4bccbSMingming Cao struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh) 1818470decc6SDave Kleikamp { 1819470decc6SDave Kleikamp struct journal_head *jh; 1820470decc6SDave Kleikamp struct journal_head *new_jh = NULL; 1821470decc6SDave Kleikamp 1822470decc6SDave Kleikamp repeat: 1823470decc6SDave Kleikamp if (!buffer_jbd(bh)) { 1824470decc6SDave Kleikamp new_jh = journal_alloc_journal_head(); 1825470decc6SDave Kleikamp memset(new_jh, 0, sizeof(*new_jh)); 1826470decc6SDave Kleikamp } 1827470decc6SDave Kleikamp 1828470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 1829470decc6SDave Kleikamp if (buffer_jbd(bh)) { 1830470decc6SDave Kleikamp jh = bh2jh(bh); 1831470decc6SDave Kleikamp } else { 1832470decc6SDave Kleikamp J_ASSERT_BH(bh, 1833470decc6SDave Kleikamp (atomic_read(&bh->b_count) > 0) || 1834470decc6SDave Kleikamp (bh->b_page && bh->b_page->mapping)); 1835470decc6SDave Kleikamp 1836470decc6SDave Kleikamp if (!new_jh) { 1837470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 1838470decc6SDave Kleikamp goto repeat; 1839470decc6SDave Kleikamp } 1840470decc6SDave Kleikamp 1841470decc6SDave Kleikamp jh = new_jh; 1842470decc6SDave Kleikamp new_jh = NULL; /* We consumed it */ 1843470decc6SDave Kleikamp set_buffer_jbd(bh); 1844470decc6SDave Kleikamp bh->b_private = jh; 1845470decc6SDave Kleikamp jh->b_bh = bh; 1846470decc6SDave Kleikamp get_bh(bh); 1847470decc6SDave Kleikamp BUFFER_TRACE(bh, "added journal_head"); 1848470decc6SDave Kleikamp } 1849470decc6SDave Kleikamp jh->b_jcount++; 1850470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 1851470decc6SDave Kleikamp if (new_jh) 1852470decc6SDave Kleikamp journal_free_journal_head(new_jh); 1853470decc6SDave Kleikamp return bh->b_private; 1854470decc6SDave Kleikamp } 1855470decc6SDave Kleikamp 1856470decc6SDave Kleikamp /* 1857470decc6SDave Kleikamp * Grab a ref against this buffer_head's journal_head. If it ended up not 1858470decc6SDave Kleikamp * having a journal_head, return NULL 1859470decc6SDave Kleikamp */ 1860f7f4bccbSMingming Cao struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh) 1861470decc6SDave Kleikamp { 1862470decc6SDave Kleikamp struct journal_head *jh = NULL; 1863470decc6SDave Kleikamp 1864470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 1865470decc6SDave Kleikamp if (buffer_jbd(bh)) { 1866470decc6SDave Kleikamp jh = bh2jh(bh); 1867470decc6SDave Kleikamp jh->b_jcount++; 1868470decc6SDave Kleikamp } 1869470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 1870470decc6SDave Kleikamp return jh; 1871470decc6SDave Kleikamp } 1872470decc6SDave Kleikamp 1873470decc6SDave Kleikamp static void __journal_remove_journal_head(struct buffer_head *bh) 1874470decc6SDave Kleikamp { 1875470decc6SDave Kleikamp struct journal_head *jh = bh2jh(bh); 1876470decc6SDave Kleikamp 1877470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_jcount >= 0); 1878470decc6SDave Kleikamp 1879470decc6SDave Kleikamp get_bh(bh); 1880470decc6SDave Kleikamp if (jh->b_jcount == 0) { 1881470decc6SDave Kleikamp if (jh->b_transaction == NULL && 1882470decc6SDave Kleikamp jh->b_next_transaction == NULL && 1883470decc6SDave Kleikamp jh->b_cp_transaction == NULL) { 1884470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_jlist == BJ_None); 1885470decc6SDave Kleikamp J_ASSERT_BH(bh, buffer_jbd(bh)); 1886470decc6SDave Kleikamp J_ASSERT_BH(bh, jh2bh(jh) == bh); 1887470decc6SDave Kleikamp BUFFER_TRACE(bh, "remove journal_head"); 1888470decc6SDave Kleikamp if (jh->b_frozen_data) { 1889470decc6SDave Kleikamp printk(KERN_WARNING "%s: freeing " 1890470decc6SDave Kleikamp "b_frozen_data\n", 1891470decc6SDave Kleikamp __FUNCTION__); 1892f7f4bccbSMingming Cao jbd2_slab_free(jh->b_frozen_data, bh->b_size); 1893470decc6SDave Kleikamp } 1894470decc6SDave Kleikamp if (jh->b_committed_data) { 1895470decc6SDave Kleikamp printk(KERN_WARNING "%s: freeing " 1896470decc6SDave Kleikamp "b_committed_data\n", 1897470decc6SDave Kleikamp __FUNCTION__); 1898f7f4bccbSMingming Cao jbd2_slab_free(jh->b_committed_data, bh->b_size); 1899470decc6SDave Kleikamp } 1900470decc6SDave Kleikamp bh->b_private = NULL; 1901470decc6SDave Kleikamp jh->b_bh = NULL; /* debug, really */ 1902470decc6SDave Kleikamp clear_buffer_jbd(bh); 1903470decc6SDave Kleikamp __brelse(bh); 1904470decc6SDave Kleikamp journal_free_journal_head(jh); 1905470decc6SDave Kleikamp } else { 1906470decc6SDave Kleikamp BUFFER_TRACE(bh, "journal_head was locked"); 1907470decc6SDave Kleikamp } 1908470decc6SDave Kleikamp } 1909470decc6SDave Kleikamp } 1910470decc6SDave Kleikamp 1911470decc6SDave Kleikamp /* 1912f7f4bccbSMingming Cao * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction 1913470decc6SDave Kleikamp * and has a zero b_jcount then remove and release its journal_head. If we did 1914470decc6SDave Kleikamp * see that the buffer is not used by any transaction we also "logically" 1915470decc6SDave Kleikamp * decrement ->b_count. 1916470decc6SDave Kleikamp * 1917470decc6SDave Kleikamp * We in fact take an additional increment on ->b_count as a convenience, 1918470decc6SDave Kleikamp * because the caller usually wants to do additional things with the bh 1919470decc6SDave Kleikamp * after calling here. 1920f7f4bccbSMingming Cao * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some 1921470decc6SDave Kleikamp * time. Once the caller has run __brelse(), the buffer is eligible for 1922470decc6SDave Kleikamp * reaping by try_to_free_buffers(). 1923470decc6SDave Kleikamp */ 1924f7f4bccbSMingming Cao void jbd2_journal_remove_journal_head(struct buffer_head *bh) 1925470decc6SDave Kleikamp { 1926470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 1927470decc6SDave Kleikamp __journal_remove_journal_head(bh); 1928470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 1929470decc6SDave Kleikamp } 1930470decc6SDave Kleikamp 1931470decc6SDave Kleikamp /* 1932470decc6SDave Kleikamp * Drop a reference on the passed journal_head. If it fell to zero then try to 1933470decc6SDave Kleikamp * release the journal_head from the buffer_head. 1934470decc6SDave Kleikamp */ 1935f7f4bccbSMingming Cao void jbd2_journal_put_journal_head(struct journal_head *jh) 1936470decc6SDave Kleikamp { 1937470decc6SDave Kleikamp struct buffer_head *bh = jh2bh(jh); 1938470decc6SDave Kleikamp 1939470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 1940470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_jcount > 0); 1941470decc6SDave Kleikamp --jh->b_jcount; 1942470decc6SDave Kleikamp if (!jh->b_jcount && !jh->b_transaction) { 1943470decc6SDave Kleikamp __journal_remove_journal_head(bh); 1944470decc6SDave Kleikamp __brelse(bh); 1945470decc6SDave Kleikamp } 1946470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 1947470decc6SDave Kleikamp } 1948470decc6SDave Kleikamp 1949470decc6SDave Kleikamp /* 1950470decc6SDave Kleikamp * /proc tunables 1951470decc6SDave Kleikamp */ 1952470decc6SDave Kleikamp #if defined(CONFIG_JBD_DEBUG) 1953f7f4bccbSMingming Cao int jbd2_journal_enable_debug; 1954f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_enable_debug); 1955470decc6SDave Kleikamp #endif 1956470decc6SDave Kleikamp 1957470decc6SDave Kleikamp #if defined(CONFIG_JBD_DEBUG) && defined(CONFIG_PROC_FS) 1958470decc6SDave Kleikamp 1959470decc6SDave Kleikamp static struct proc_dir_entry *proc_jbd_debug; 1960470decc6SDave Kleikamp 1961470decc6SDave Kleikamp static int read_jbd_debug(char *page, char **start, off_t off, 1962470decc6SDave Kleikamp int count, int *eof, void *data) 1963470decc6SDave Kleikamp { 1964470decc6SDave Kleikamp int ret; 1965470decc6SDave Kleikamp 1966f7f4bccbSMingming Cao ret = sprintf(page + off, "%d\n", jbd2_journal_enable_debug); 1967470decc6SDave Kleikamp *eof = 1; 1968470decc6SDave Kleikamp return ret; 1969470decc6SDave Kleikamp } 1970470decc6SDave Kleikamp 1971470decc6SDave Kleikamp static int write_jbd_debug(struct file *file, const char __user *buffer, 1972470decc6SDave Kleikamp unsigned long count, void *data) 1973470decc6SDave Kleikamp { 1974470decc6SDave Kleikamp char buf[32]; 1975470decc6SDave Kleikamp 1976470decc6SDave Kleikamp if (count > ARRAY_SIZE(buf) - 1) 1977470decc6SDave Kleikamp count = ARRAY_SIZE(buf) - 1; 1978470decc6SDave Kleikamp if (copy_from_user(buf, buffer, count)) 1979470decc6SDave Kleikamp return -EFAULT; 1980470decc6SDave Kleikamp buf[ARRAY_SIZE(buf) - 1] = '\0'; 1981f7f4bccbSMingming Cao jbd2_journal_enable_debug = simple_strtoul(buf, NULL, 10); 1982470decc6SDave Kleikamp return count; 1983470decc6SDave Kleikamp } 1984470decc6SDave Kleikamp 1985f7f4bccbSMingming Cao #define JBD_PROC_NAME "sys/fs/jbd2-debug" 1986470decc6SDave Kleikamp 1987470decc6SDave Kleikamp static void __init create_jbd_proc_entry(void) 1988470decc6SDave Kleikamp { 1989470decc6SDave Kleikamp proc_jbd_debug = create_proc_entry(JBD_PROC_NAME, 0644, NULL); 1990470decc6SDave Kleikamp if (proc_jbd_debug) { 1991470decc6SDave Kleikamp /* Why is this so hard? */ 1992470decc6SDave Kleikamp proc_jbd_debug->read_proc = read_jbd_debug; 1993470decc6SDave Kleikamp proc_jbd_debug->write_proc = write_jbd_debug; 1994470decc6SDave Kleikamp } 1995470decc6SDave Kleikamp } 1996470decc6SDave Kleikamp 1997f7f4bccbSMingming Cao static void __exit jbd2_remove_jbd_proc_entry(void) 1998470decc6SDave Kleikamp { 1999470decc6SDave Kleikamp if (proc_jbd_debug) 2000470decc6SDave Kleikamp remove_proc_entry(JBD_PROC_NAME, NULL); 2001470decc6SDave Kleikamp } 2002470decc6SDave Kleikamp 2003470decc6SDave Kleikamp #else 2004470decc6SDave Kleikamp 2005470decc6SDave Kleikamp #define create_jbd_proc_entry() do {} while (0) 2006f7f4bccbSMingming Cao #define jbd2_remove_jbd_proc_entry() do {} while (0) 2007470decc6SDave Kleikamp 2008470decc6SDave Kleikamp #endif 2009470decc6SDave Kleikamp 2010f7f4bccbSMingming Cao kmem_cache_t *jbd2_handle_cache; 2011470decc6SDave Kleikamp 2012470decc6SDave Kleikamp static int __init journal_init_handle_cache(void) 2013470decc6SDave Kleikamp { 2014a920e941SJohann Lombardi jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle", 2015470decc6SDave Kleikamp sizeof(handle_t), 2016470decc6SDave Kleikamp 0, /* offset */ 2017470decc6SDave Kleikamp 0, /* flags */ 2018470decc6SDave Kleikamp NULL, /* ctor */ 2019470decc6SDave Kleikamp NULL); /* dtor */ 2020f7f4bccbSMingming Cao if (jbd2_handle_cache == NULL) { 2021470decc6SDave Kleikamp printk(KERN_EMERG "JBD: failed to create handle cache\n"); 2022470decc6SDave Kleikamp return -ENOMEM; 2023470decc6SDave Kleikamp } 2024470decc6SDave Kleikamp return 0; 2025470decc6SDave Kleikamp } 2026470decc6SDave Kleikamp 2027f7f4bccbSMingming Cao static void jbd2_journal_destroy_handle_cache(void) 2028470decc6SDave Kleikamp { 2029f7f4bccbSMingming Cao if (jbd2_handle_cache) 2030f7f4bccbSMingming Cao kmem_cache_destroy(jbd2_handle_cache); 2031470decc6SDave Kleikamp } 2032470decc6SDave Kleikamp 2033470decc6SDave Kleikamp /* 2034470decc6SDave Kleikamp * Module startup and shutdown 2035470decc6SDave Kleikamp */ 2036470decc6SDave Kleikamp 2037470decc6SDave Kleikamp static int __init journal_init_caches(void) 2038470decc6SDave Kleikamp { 2039470decc6SDave Kleikamp int ret; 2040470decc6SDave Kleikamp 2041f7f4bccbSMingming Cao ret = jbd2_journal_init_revoke_caches(); 2042470decc6SDave Kleikamp if (ret == 0) 2043f7f4bccbSMingming Cao ret = journal_init_jbd2_journal_head_cache(); 2044470decc6SDave Kleikamp if (ret == 0) 2045470decc6SDave Kleikamp ret = journal_init_handle_cache(); 2046470decc6SDave Kleikamp return ret; 2047470decc6SDave Kleikamp } 2048470decc6SDave Kleikamp 2049f7f4bccbSMingming Cao static void jbd2_journal_destroy_caches(void) 2050470decc6SDave Kleikamp { 2051f7f4bccbSMingming Cao jbd2_journal_destroy_revoke_caches(); 2052f7f4bccbSMingming Cao jbd2_journal_destroy_jbd2_journal_head_cache(); 2053f7f4bccbSMingming Cao jbd2_journal_destroy_handle_cache(); 2054f7f4bccbSMingming Cao jbd2_journal_destroy_jbd_slabs(); 2055470decc6SDave Kleikamp } 2056470decc6SDave Kleikamp 2057470decc6SDave Kleikamp static int __init journal_init(void) 2058470decc6SDave Kleikamp { 2059470decc6SDave Kleikamp int ret; 2060470decc6SDave Kleikamp 2061470decc6SDave Kleikamp BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024); 2062470decc6SDave Kleikamp 2063470decc6SDave Kleikamp ret = journal_init_caches(); 2064470decc6SDave Kleikamp if (ret != 0) 2065f7f4bccbSMingming Cao jbd2_journal_destroy_caches(); 2066470decc6SDave Kleikamp create_jbd_proc_entry(); 2067470decc6SDave Kleikamp return ret; 2068470decc6SDave Kleikamp } 2069470decc6SDave Kleikamp 2070470decc6SDave Kleikamp static void __exit journal_exit(void) 2071470decc6SDave Kleikamp { 2072470decc6SDave Kleikamp #ifdef CONFIG_JBD_DEBUG 2073470decc6SDave Kleikamp int n = atomic_read(&nr_journal_heads); 2074470decc6SDave Kleikamp if (n) 2075470decc6SDave Kleikamp printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n); 2076470decc6SDave Kleikamp #endif 2077f7f4bccbSMingming Cao jbd2_remove_jbd_proc_entry(); 2078f7f4bccbSMingming Cao jbd2_journal_destroy_caches(); 2079470decc6SDave Kleikamp } 2080470decc6SDave Kleikamp 2081470decc6SDave Kleikamp MODULE_LICENSE("GPL"); 2082470decc6SDave Kleikamp module_init(journal_init); 2083470decc6SDave Kleikamp module_exit(journal_exit); 2084470decc6SDave Kleikamp 2085