1470decc6SDave Kleikamp /* 2f7f4bccbSMingming Cao * linux/fs/jbd2/journal.c 3470decc6SDave Kleikamp * 4470decc6SDave Kleikamp * Written by Stephen C. Tweedie <sct@redhat.com>, 1998 5470decc6SDave Kleikamp * 6470decc6SDave Kleikamp * Copyright 1998 Red Hat corp --- All Rights Reserved 7470decc6SDave Kleikamp * 8470decc6SDave Kleikamp * This file is part of the Linux kernel and is made available under 9470decc6SDave Kleikamp * the terms of the GNU General Public License, version 2, or at your 10470decc6SDave Kleikamp * option, any later version, incorporated herein by reference. 11470decc6SDave Kleikamp * 12470decc6SDave Kleikamp * Generic filesystem journal-writing code; part of the ext2fs 13470decc6SDave Kleikamp * journaling system. 14470decc6SDave Kleikamp * 15470decc6SDave Kleikamp * This file manages journals: areas of disk reserved for logging 16470decc6SDave Kleikamp * transactional updates. This includes the kernel journaling thread 17470decc6SDave Kleikamp * which is responsible for scheduling updates to the log. 18470decc6SDave Kleikamp * 19470decc6SDave Kleikamp * We do not actually manage the physical storage of the journal in this 20470decc6SDave Kleikamp * file: that is left to a per-journal policy function, which allows us 21470decc6SDave Kleikamp * to store the journal within a filesystem-specified area for ext2 22470decc6SDave Kleikamp * journaling (ext2 can use a reserved inode for storing the log). 23470decc6SDave Kleikamp */ 24470decc6SDave Kleikamp 25470decc6SDave Kleikamp #include <linux/module.h> 26470decc6SDave Kleikamp #include <linux/time.h> 27470decc6SDave Kleikamp #include <linux/fs.h> 28f7f4bccbSMingming Cao #include <linux/jbd2.h> 29470decc6SDave Kleikamp #include <linux/errno.h> 30470decc6SDave Kleikamp #include <linux/slab.h> 31470decc6SDave Kleikamp #include <linux/init.h> 32470decc6SDave Kleikamp #include <linux/mm.h> 337dfb7103SNigel Cunningham #include <linux/freezer.h> 34470decc6SDave Kleikamp #include <linux/pagemap.h> 35470decc6SDave Kleikamp #include <linux/kthread.h> 36470decc6SDave Kleikamp #include <linux/poison.h> 37470decc6SDave Kleikamp #include <linux/proc_fs.h> 38470decc6SDave Kleikamp 39470decc6SDave Kleikamp #include <asm/uaccess.h> 40470decc6SDave Kleikamp #include <asm/page.h> 41470decc6SDave Kleikamp 42f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_start); 43f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_restart); 44f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_extend); 45f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_stop); 46f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_lock_updates); 47f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_unlock_updates); 48f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_get_write_access); 49f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_get_create_access); 50f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_get_undo_access); 51f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_dirty_data); 52f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_dirty_metadata); 53f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_release_buffer); 54f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_forget); 55470decc6SDave Kleikamp #if 0 56470decc6SDave Kleikamp EXPORT_SYMBOL(journal_sync_buffer); 57470decc6SDave Kleikamp #endif 58f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_flush); 59f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_revoke); 60470decc6SDave Kleikamp 61f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_init_dev); 62f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_init_inode); 63f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_update_format); 64f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_check_used_features); 65f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_check_available_features); 66f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_set_features); 67f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_create); 68f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_load); 69f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_destroy); 70f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_update_superblock); 71f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_abort); 72f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_errno); 73f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_ack_err); 74f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_clear_err); 75f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_log_wait_commit); 76f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_start_commit); 77f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_force_commit_nested); 78f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_wipe); 79f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_blocks_per_page); 80f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_invalidatepage); 81f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers); 82f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_force_commit); 83470decc6SDave Kleikamp 84470decc6SDave Kleikamp static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *); 85470decc6SDave Kleikamp static void __journal_abort_soft (journal_t *journal, int errno); 86f7f4bccbSMingming Cao static int jbd2_journal_create_jbd_slab(size_t slab_size); 87470decc6SDave Kleikamp 88470decc6SDave Kleikamp /* 89470decc6SDave Kleikamp * Helper function used to manage commit timeouts 90470decc6SDave Kleikamp */ 91470decc6SDave Kleikamp 92470decc6SDave Kleikamp static void commit_timeout(unsigned long __data) 93470decc6SDave Kleikamp { 94470decc6SDave Kleikamp struct task_struct * p = (struct task_struct *) __data; 95470decc6SDave Kleikamp 96470decc6SDave Kleikamp wake_up_process(p); 97470decc6SDave Kleikamp } 98470decc6SDave Kleikamp 99470decc6SDave Kleikamp /* 100f7f4bccbSMingming Cao * kjournald2: The main thread function used to manage a logging device 101470decc6SDave Kleikamp * journal. 102470decc6SDave Kleikamp * 103470decc6SDave Kleikamp * This kernel thread is responsible for two things: 104470decc6SDave Kleikamp * 105470decc6SDave Kleikamp * 1) COMMIT: Every so often we need to commit the current state of the 106470decc6SDave Kleikamp * filesystem to disk. The journal thread is responsible for writing 107470decc6SDave Kleikamp * all of the metadata buffers to disk. 108470decc6SDave Kleikamp * 109470decc6SDave Kleikamp * 2) CHECKPOINT: We cannot reuse a used section of the log file until all 110470decc6SDave Kleikamp * of the data in that part of the log has been rewritten elsewhere on 111470decc6SDave Kleikamp * the disk. Flushing these old buffers to reclaim space in the log is 112470decc6SDave Kleikamp * known as checkpointing, and this thread is responsible for that job. 113470decc6SDave Kleikamp */ 114470decc6SDave Kleikamp 115f7f4bccbSMingming Cao static int kjournald2(void *arg) 116470decc6SDave Kleikamp { 117470decc6SDave Kleikamp journal_t *journal = arg; 118470decc6SDave Kleikamp transaction_t *transaction; 119470decc6SDave Kleikamp 120470decc6SDave Kleikamp /* 121470decc6SDave Kleikamp * Set up an interval timer which can be used to trigger a commit wakeup 122470decc6SDave Kleikamp * after the commit interval expires 123470decc6SDave Kleikamp */ 124470decc6SDave Kleikamp setup_timer(&journal->j_commit_timer, commit_timeout, 125470decc6SDave Kleikamp (unsigned long)current); 126470decc6SDave Kleikamp 127470decc6SDave Kleikamp /* Record that the journal thread is running */ 128470decc6SDave Kleikamp journal->j_task = current; 129470decc6SDave Kleikamp wake_up(&journal->j_wait_done_commit); 130470decc6SDave Kleikamp 131f7f4bccbSMingming Cao printk(KERN_INFO "kjournald2 starting. Commit interval %ld seconds\n", 132470decc6SDave Kleikamp journal->j_commit_interval / HZ); 133470decc6SDave Kleikamp 134470decc6SDave Kleikamp /* 135470decc6SDave Kleikamp * And now, wait forever for commit wakeup events. 136470decc6SDave Kleikamp */ 137470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 138470decc6SDave Kleikamp 139470decc6SDave Kleikamp loop: 140f7f4bccbSMingming Cao if (journal->j_flags & JBD2_UNMOUNT) 141470decc6SDave Kleikamp goto end_loop; 142470decc6SDave Kleikamp 143470decc6SDave Kleikamp jbd_debug(1, "commit_sequence=%d, commit_request=%d\n", 144470decc6SDave Kleikamp journal->j_commit_sequence, journal->j_commit_request); 145470decc6SDave Kleikamp 146470decc6SDave Kleikamp if (journal->j_commit_sequence != journal->j_commit_request) { 147470decc6SDave Kleikamp jbd_debug(1, "OK, requests differ\n"); 148470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 149470decc6SDave Kleikamp del_timer_sync(&journal->j_commit_timer); 150f7f4bccbSMingming Cao jbd2_journal_commit_transaction(journal); 151470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 152470decc6SDave Kleikamp goto loop; 153470decc6SDave Kleikamp } 154470decc6SDave Kleikamp 155470decc6SDave Kleikamp wake_up(&journal->j_wait_done_commit); 156470decc6SDave Kleikamp if (freezing(current)) { 157470decc6SDave Kleikamp /* 158470decc6SDave Kleikamp * The simpler the better. Flushing journal isn't a 159470decc6SDave Kleikamp * good idea, because that depends on threads that may 160470decc6SDave Kleikamp * be already stopped. 161470decc6SDave Kleikamp */ 162f7f4bccbSMingming Cao jbd_debug(1, "Now suspending kjournald2\n"); 163470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 164470decc6SDave Kleikamp refrigerator(); 165470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 166470decc6SDave Kleikamp } else { 167470decc6SDave Kleikamp /* 168470decc6SDave Kleikamp * We assume on resume that commits are already there, 169470decc6SDave Kleikamp * so we don't sleep 170470decc6SDave Kleikamp */ 171470decc6SDave Kleikamp DEFINE_WAIT(wait); 172470decc6SDave Kleikamp int should_sleep = 1; 173470decc6SDave Kleikamp 174470decc6SDave Kleikamp prepare_to_wait(&journal->j_wait_commit, &wait, 175470decc6SDave Kleikamp TASK_INTERRUPTIBLE); 176470decc6SDave Kleikamp if (journal->j_commit_sequence != journal->j_commit_request) 177470decc6SDave Kleikamp should_sleep = 0; 178470decc6SDave Kleikamp transaction = journal->j_running_transaction; 179470decc6SDave Kleikamp if (transaction && time_after_eq(jiffies, 180470decc6SDave Kleikamp transaction->t_expires)) 181470decc6SDave Kleikamp should_sleep = 0; 182f7f4bccbSMingming Cao if (journal->j_flags & JBD2_UNMOUNT) 183470decc6SDave Kleikamp should_sleep = 0; 184470decc6SDave Kleikamp if (should_sleep) { 185470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 186470decc6SDave Kleikamp schedule(); 187470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 188470decc6SDave Kleikamp } 189470decc6SDave Kleikamp finish_wait(&journal->j_wait_commit, &wait); 190470decc6SDave Kleikamp } 191470decc6SDave Kleikamp 192f7f4bccbSMingming Cao jbd_debug(1, "kjournald2 wakes\n"); 193470decc6SDave Kleikamp 194470decc6SDave Kleikamp /* 195470decc6SDave Kleikamp * Were we woken up by a commit wakeup event? 196470decc6SDave Kleikamp */ 197470decc6SDave Kleikamp transaction = journal->j_running_transaction; 198470decc6SDave Kleikamp if (transaction && time_after_eq(jiffies, transaction->t_expires)) { 199470decc6SDave Kleikamp journal->j_commit_request = transaction->t_tid; 200470decc6SDave Kleikamp jbd_debug(1, "woke because of timeout\n"); 201470decc6SDave Kleikamp } 202470decc6SDave Kleikamp goto loop; 203470decc6SDave Kleikamp 204470decc6SDave Kleikamp end_loop: 205470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 206470decc6SDave Kleikamp del_timer_sync(&journal->j_commit_timer); 207470decc6SDave Kleikamp journal->j_task = NULL; 208470decc6SDave Kleikamp wake_up(&journal->j_wait_done_commit); 209470decc6SDave Kleikamp jbd_debug(1, "Journal thread exiting.\n"); 210470decc6SDave Kleikamp return 0; 211470decc6SDave Kleikamp } 212470decc6SDave Kleikamp 21397f06784SPavel Emelianov static int jbd2_journal_start_thread(journal_t *journal) 214470decc6SDave Kleikamp { 21597f06784SPavel Emelianov struct task_struct *t; 21697f06784SPavel Emelianov 21797f06784SPavel Emelianov t = kthread_run(kjournald2, journal, "kjournald2"); 21897f06784SPavel Emelianov if (IS_ERR(t)) 21997f06784SPavel Emelianov return PTR_ERR(t); 22097f06784SPavel Emelianov 221470decc6SDave Kleikamp wait_event(journal->j_wait_done_commit, journal->j_task != 0); 22297f06784SPavel Emelianov return 0; 223470decc6SDave Kleikamp } 224470decc6SDave Kleikamp 225470decc6SDave Kleikamp static void journal_kill_thread(journal_t *journal) 226470decc6SDave Kleikamp { 227470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 228f7f4bccbSMingming Cao journal->j_flags |= JBD2_UNMOUNT; 229470decc6SDave Kleikamp 230470decc6SDave Kleikamp while (journal->j_task) { 231470decc6SDave Kleikamp wake_up(&journal->j_wait_commit); 232470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 233470decc6SDave Kleikamp wait_event(journal->j_wait_done_commit, journal->j_task == 0); 234470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 235470decc6SDave Kleikamp } 236470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 237470decc6SDave Kleikamp } 238470decc6SDave Kleikamp 239470decc6SDave Kleikamp /* 240f7f4bccbSMingming Cao * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal. 241470decc6SDave Kleikamp * 242470decc6SDave Kleikamp * Writes a metadata buffer to a given disk block. The actual IO is not 243470decc6SDave Kleikamp * performed but a new buffer_head is constructed which labels the data 244470decc6SDave Kleikamp * to be written with the correct destination disk block. 245470decc6SDave Kleikamp * 246470decc6SDave Kleikamp * Any magic-number escaping which needs to be done will cause a 247470decc6SDave Kleikamp * copy-out here. If the buffer happens to start with the 248f7f4bccbSMingming Cao * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the 249470decc6SDave Kleikamp * magic number is only written to the log for descripter blocks. In 250470decc6SDave Kleikamp * this case, we copy the data and replace the first word with 0, and we 251470decc6SDave Kleikamp * return a result code which indicates that this buffer needs to be 252470decc6SDave Kleikamp * marked as an escaped buffer in the corresponding log descriptor 253470decc6SDave Kleikamp * block. The missing word can then be restored when the block is read 254470decc6SDave Kleikamp * during recovery. 255470decc6SDave Kleikamp * 256470decc6SDave Kleikamp * If the source buffer has already been modified by a new transaction 257470decc6SDave Kleikamp * since we took the last commit snapshot, we use the frozen copy of 258470decc6SDave Kleikamp * that data for IO. If we end up using the existing buffer_head's data 259470decc6SDave Kleikamp * for the write, then we *have* to lock the buffer to prevent anyone 260470decc6SDave Kleikamp * else from using and possibly modifying it while the IO is in 261470decc6SDave Kleikamp * progress. 262470decc6SDave Kleikamp * 263470decc6SDave Kleikamp * The function returns a pointer to the buffer_heads to be used for IO. 264470decc6SDave Kleikamp * 265470decc6SDave Kleikamp * We assume that the journal has already been locked in this function. 266470decc6SDave Kleikamp * 267470decc6SDave Kleikamp * Return value: 268470decc6SDave Kleikamp * <0: Error 269470decc6SDave Kleikamp * >=0: Finished OK 270470decc6SDave Kleikamp * 271470decc6SDave Kleikamp * On success: 272470decc6SDave Kleikamp * Bit 0 set == escape performed on the data 273470decc6SDave Kleikamp * Bit 1 set == buffer copy-out performed (kfree the data after IO) 274470decc6SDave Kleikamp */ 275470decc6SDave Kleikamp 276f7f4bccbSMingming Cao int jbd2_journal_write_metadata_buffer(transaction_t *transaction, 277470decc6SDave Kleikamp struct journal_head *jh_in, 278470decc6SDave Kleikamp struct journal_head **jh_out, 27918eba7aaSMingming Cao unsigned long long blocknr) 280470decc6SDave Kleikamp { 281470decc6SDave Kleikamp int need_copy_out = 0; 282470decc6SDave Kleikamp int done_copy_out = 0; 283470decc6SDave Kleikamp int do_escape = 0; 284470decc6SDave Kleikamp char *mapped_data; 285470decc6SDave Kleikamp struct buffer_head *new_bh; 286470decc6SDave Kleikamp struct journal_head *new_jh; 287470decc6SDave Kleikamp struct page *new_page; 288470decc6SDave Kleikamp unsigned int new_offset; 289470decc6SDave Kleikamp struct buffer_head *bh_in = jh2bh(jh_in); 290470decc6SDave Kleikamp 291470decc6SDave Kleikamp /* 292470decc6SDave Kleikamp * The buffer really shouldn't be locked: only the current committing 293470decc6SDave Kleikamp * transaction is allowed to write it, so nobody else is allowed 294470decc6SDave Kleikamp * to do any IO. 295470decc6SDave Kleikamp * 296470decc6SDave Kleikamp * akpm: except if we're journalling data, and write() output is 297470decc6SDave Kleikamp * also part of a shared mapping, and another thread has 298470decc6SDave Kleikamp * decided to launch a writepage() against this buffer. 299470decc6SDave Kleikamp */ 300470decc6SDave Kleikamp J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in)); 301470decc6SDave Kleikamp 302470decc6SDave Kleikamp new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL); 303470decc6SDave Kleikamp 304470decc6SDave Kleikamp /* 305470decc6SDave Kleikamp * If a new transaction has already done a buffer copy-out, then 306470decc6SDave Kleikamp * we use that version of the data for the commit. 307470decc6SDave Kleikamp */ 308470decc6SDave Kleikamp jbd_lock_bh_state(bh_in); 309470decc6SDave Kleikamp repeat: 310470decc6SDave Kleikamp if (jh_in->b_frozen_data) { 311470decc6SDave Kleikamp done_copy_out = 1; 312470decc6SDave Kleikamp new_page = virt_to_page(jh_in->b_frozen_data); 313470decc6SDave Kleikamp new_offset = offset_in_page(jh_in->b_frozen_data); 314470decc6SDave Kleikamp } else { 315470decc6SDave Kleikamp new_page = jh2bh(jh_in)->b_page; 316470decc6SDave Kleikamp new_offset = offset_in_page(jh2bh(jh_in)->b_data); 317470decc6SDave Kleikamp } 318470decc6SDave Kleikamp 319470decc6SDave Kleikamp mapped_data = kmap_atomic(new_page, KM_USER0); 320470decc6SDave Kleikamp /* 321470decc6SDave Kleikamp * Check for escaping 322470decc6SDave Kleikamp */ 323470decc6SDave Kleikamp if (*((__be32 *)(mapped_data + new_offset)) == 324f7f4bccbSMingming Cao cpu_to_be32(JBD2_MAGIC_NUMBER)) { 325470decc6SDave Kleikamp need_copy_out = 1; 326470decc6SDave Kleikamp do_escape = 1; 327470decc6SDave Kleikamp } 328470decc6SDave Kleikamp kunmap_atomic(mapped_data, KM_USER0); 329470decc6SDave Kleikamp 330470decc6SDave Kleikamp /* 331470decc6SDave Kleikamp * Do we need to do a data copy? 332470decc6SDave Kleikamp */ 333470decc6SDave Kleikamp if (need_copy_out && !done_copy_out) { 334470decc6SDave Kleikamp char *tmp; 335470decc6SDave Kleikamp 336470decc6SDave Kleikamp jbd_unlock_bh_state(bh_in); 337f7f4bccbSMingming Cao tmp = jbd2_slab_alloc(bh_in->b_size, GFP_NOFS); 338470decc6SDave Kleikamp jbd_lock_bh_state(bh_in); 339470decc6SDave Kleikamp if (jh_in->b_frozen_data) { 340f7f4bccbSMingming Cao jbd2_slab_free(tmp, bh_in->b_size); 341470decc6SDave Kleikamp goto repeat; 342470decc6SDave Kleikamp } 343470decc6SDave Kleikamp 344470decc6SDave Kleikamp jh_in->b_frozen_data = tmp; 345470decc6SDave Kleikamp mapped_data = kmap_atomic(new_page, KM_USER0); 346470decc6SDave Kleikamp memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size); 347470decc6SDave Kleikamp kunmap_atomic(mapped_data, KM_USER0); 348470decc6SDave Kleikamp 349470decc6SDave Kleikamp new_page = virt_to_page(tmp); 350470decc6SDave Kleikamp new_offset = offset_in_page(tmp); 351470decc6SDave Kleikamp done_copy_out = 1; 352470decc6SDave Kleikamp } 353470decc6SDave Kleikamp 354470decc6SDave Kleikamp /* 355470decc6SDave Kleikamp * Did we need to do an escaping? Now we've done all the 356470decc6SDave Kleikamp * copying, we can finally do so. 357470decc6SDave Kleikamp */ 358470decc6SDave Kleikamp if (do_escape) { 359470decc6SDave Kleikamp mapped_data = kmap_atomic(new_page, KM_USER0); 360470decc6SDave Kleikamp *((unsigned int *)(mapped_data + new_offset)) = 0; 361470decc6SDave Kleikamp kunmap_atomic(mapped_data, KM_USER0); 362470decc6SDave Kleikamp } 363470decc6SDave Kleikamp 364470decc6SDave Kleikamp /* keep subsequent assertions sane */ 365470decc6SDave Kleikamp new_bh->b_state = 0; 366470decc6SDave Kleikamp init_buffer(new_bh, NULL, NULL); 367470decc6SDave Kleikamp atomic_set(&new_bh->b_count, 1); 368470decc6SDave Kleikamp jbd_unlock_bh_state(bh_in); 369470decc6SDave Kleikamp 370f7f4bccbSMingming Cao new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */ 371470decc6SDave Kleikamp 372470decc6SDave Kleikamp set_bh_page(new_bh, new_page, new_offset); 373470decc6SDave Kleikamp new_jh->b_transaction = NULL; 374470decc6SDave Kleikamp new_bh->b_size = jh2bh(jh_in)->b_size; 375470decc6SDave Kleikamp new_bh->b_bdev = transaction->t_journal->j_dev; 376470decc6SDave Kleikamp new_bh->b_blocknr = blocknr; 377470decc6SDave Kleikamp set_buffer_mapped(new_bh); 378470decc6SDave Kleikamp set_buffer_dirty(new_bh); 379470decc6SDave Kleikamp 380470decc6SDave Kleikamp *jh_out = new_jh; 381470decc6SDave Kleikamp 382470decc6SDave Kleikamp /* 383470decc6SDave Kleikamp * The to-be-written buffer needs to get moved to the io queue, 384470decc6SDave Kleikamp * and the original buffer whose contents we are shadowing or 385470decc6SDave Kleikamp * copying is moved to the transaction's shadow queue. 386470decc6SDave Kleikamp */ 387470decc6SDave Kleikamp JBUFFER_TRACE(jh_in, "file as BJ_Shadow"); 388f7f4bccbSMingming Cao jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow); 389470decc6SDave Kleikamp JBUFFER_TRACE(new_jh, "file as BJ_IO"); 390f7f4bccbSMingming Cao jbd2_journal_file_buffer(new_jh, transaction, BJ_IO); 391470decc6SDave Kleikamp 392470decc6SDave Kleikamp return do_escape | (done_copy_out << 1); 393470decc6SDave Kleikamp } 394470decc6SDave Kleikamp 395470decc6SDave Kleikamp /* 396470decc6SDave Kleikamp * Allocation code for the journal file. Manage the space left in the 397470decc6SDave Kleikamp * journal, so that we can begin checkpointing when appropriate. 398470decc6SDave Kleikamp */ 399470decc6SDave Kleikamp 400470decc6SDave Kleikamp /* 401f7f4bccbSMingming Cao * __jbd2_log_space_left: Return the number of free blocks left in the journal. 402470decc6SDave Kleikamp * 403470decc6SDave Kleikamp * Called with the journal already locked. 404470decc6SDave Kleikamp * 405470decc6SDave Kleikamp * Called under j_state_lock 406470decc6SDave Kleikamp */ 407470decc6SDave Kleikamp 408f7f4bccbSMingming Cao int __jbd2_log_space_left(journal_t *journal) 409470decc6SDave Kleikamp { 410470decc6SDave Kleikamp int left = journal->j_free; 411470decc6SDave Kleikamp 412470decc6SDave Kleikamp assert_spin_locked(&journal->j_state_lock); 413470decc6SDave Kleikamp 414470decc6SDave Kleikamp /* 415470decc6SDave Kleikamp * Be pessimistic here about the number of those free blocks which 416470decc6SDave Kleikamp * might be required for log descriptor control blocks. 417470decc6SDave Kleikamp */ 418470decc6SDave Kleikamp 419470decc6SDave Kleikamp #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */ 420470decc6SDave Kleikamp 421470decc6SDave Kleikamp left -= MIN_LOG_RESERVED_BLOCKS; 422470decc6SDave Kleikamp 423470decc6SDave Kleikamp if (left <= 0) 424470decc6SDave Kleikamp return 0; 425470decc6SDave Kleikamp left -= (left >> 3); 426470decc6SDave Kleikamp return left; 427470decc6SDave Kleikamp } 428470decc6SDave Kleikamp 429470decc6SDave Kleikamp /* 430470decc6SDave Kleikamp * Called under j_state_lock. Returns true if a transaction was started. 431470decc6SDave Kleikamp */ 432f7f4bccbSMingming Cao int __jbd2_log_start_commit(journal_t *journal, tid_t target) 433470decc6SDave Kleikamp { 434470decc6SDave Kleikamp /* 435470decc6SDave Kleikamp * Are we already doing a recent enough commit? 436470decc6SDave Kleikamp */ 437470decc6SDave Kleikamp if (!tid_geq(journal->j_commit_request, target)) { 438470decc6SDave Kleikamp /* 439470decc6SDave Kleikamp * We want a new commit: OK, mark the request and wakup the 440470decc6SDave Kleikamp * commit thread. We do _not_ do the commit ourselves. 441470decc6SDave Kleikamp */ 442470decc6SDave Kleikamp 443470decc6SDave Kleikamp journal->j_commit_request = target; 444470decc6SDave Kleikamp jbd_debug(1, "JBD: requesting commit %d/%d\n", 445470decc6SDave Kleikamp journal->j_commit_request, 446470decc6SDave Kleikamp journal->j_commit_sequence); 447470decc6SDave Kleikamp wake_up(&journal->j_wait_commit); 448470decc6SDave Kleikamp return 1; 449470decc6SDave Kleikamp } 450470decc6SDave Kleikamp return 0; 451470decc6SDave Kleikamp } 452470decc6SDave Kleikamp 453f7f4bccbSMingming Cao int jbd2_log_start_commit(journal_t *journal, tid_t tid) 454470decc6SDave Kleikamp { 455470decc6SDave Kleikamp int ret; 456470decc6SDave Kleikamp 457470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 458f7f4bccbSMingming Cao ret = __jbd2_log_start_commit(journal, tid); 459470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 460470decc6SDave Kleikamp return ret; 461470decc6SDave Kleikamp } 462470decc6SDave Kleikamp 463470decc6SDave Kleikamp /* 464470decc6SDave Kleikamp * Force and wait upon a commit if the calling process is not within 465470decc6SDave Kleikamp * transaction. This is used for forcing out undo-protected data which contains 466470decc6SDave Kleikamp * bitmaps, when the fs is running out of space. 467470decc6SDave Kleikamp * 468470decc6SDave Kleikamp * We can only force the running transaction if we don't have an active handle; 469470decc6SDave Kleikamp * otherwise, we will deadlock. 470470decc6SDave Kleikamp * 471470decc6SDave Kleikamp * Returns true if a transaction was started. 472470decc6SDave Kleikamp */ 473f7f4bccbSMingming Cao int jbd2_journal_force_commit_nested(journal_t *journal) 474470decc6SDave Kleikamp { 475470decc6SDave Kleikamp transaction_t *transaction = NULL; 476470decc6SDave Kleikamp tid_t tid; 477470decc6SDave Kleikamp 478470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 479470decc6SDave Kleikamp if (journal->j_running_transaction && !current->journal_info) { 480470decc6SDave Kleikamp transaction = journal->j_running_transaction; 481f7f4bccbSMingming Cao __jbd2_log_start_commit(journal, transaction->t_tid); 482470decc6SDave Kleikamp } else if (journal->j_committing_transaction) 483470decc6SDave Kleikamp transaction = journal->j_committing_transaction; 484470decc6SDave Kleikamp 485470decc6SDave Kleikamp if (!transaction) { 486470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 487470decc6SDave Kleikamp return 0; /* Nothing to retry */ 488470decc6SDave Kleikamp } 489470decc6SDave Kleikamp 490470decc6SDave Kleikamp tid = transaction->t_tid; 491470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 492f7f4bccbSMingming Cao jbd2_log_wait_commit(journal, tid); 493470decc6SDave Kleikamp return 1; 494470decc6SDave Kleikamp } 495470decc6SDave Kleikamp 496470decc6SDave Kleikamp /* 497470decc6SDave Kleikamp * Start a commit of the current running transaction (if any). Returns true 498470decc6SDave Kleikamp * if a transaction was started, and fills its tid in at *ptid 499470decc6SDave Kleikamp */ 500f7f4bccbSMingming Cao int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid) 501470decc6SDave Kleikamp { 502470decc6SDave Kleikamp int ret = 0; 503470decc6SDave Kleikamp 504470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 505470decc6SDave Kleikamp if (journal->j_running_transaction) { 506470decc6SDave Kleikamp tid_t tid = journal->j_running_transaction->t_tid; 507470decc6SDave Kleikamp 508f7f4bccbSMingming Cao ret = __jbd2_log_start_commit(journal, tid); 509470decc6SDave Kleikamp if (ret && ptid) 510470decc6SDave Kleikamp *ptid = tid; 511470decc6SDave Kleikamp } else if (journal->j_committing_transaction && ptid) { 512470decc6SDave Kleikamp /* 513470decc6SDave Kleikamp * If ext3_write_super() recently started a commit, then we 514470decc6SDave Kleikamp * have to wait for completion of that transaction 515470decc6SDave Kleikamp */ 516470decc6SDave Kleikamp *ptid = journal->j_committing_transaction->t_tid; 517470decc6SDave Kleikamp ret = 1; 518470decc6SDave Kleikamp } 519470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 520470decc6SDave Kleikamp return ret; 521470decc6SDave Kleikamp } 522470decc6SDave Kleikamp 523470decc6SDave Kleikamp /* 524470decc6SDave Kleikamp * Wait for a specified commit to complete. 525470decc6SDave Kleikamp * The caller may not hold the journal lock. 526470decc6SDave Kleikamp */ 527f7f4bccbSMingming Cao int jbd2_log_wait_commit(journal_t *journal, tid_t tid) 528470decc6SDave Kleikamp { 529470decc6SDave Kleikamp int err = 0; 530470decc6SDave Kleikamp 531*e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 532470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 533470decc6SDave Kleikamp if (!tid_geq(journal->j_commit_request, tid)) { 534470decc6SDave Kleikamp printk(KERN_EMERG 535470decc6SDave Kleikamp "%s: error: j_commit_request=%d, tid=%d\n", 536470decc6SDave Kleikamp __FUNCTION__, journal->j_commit_request, tid); 537470decc6SDave Kleikamp } 538470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 539470decc6SDave Kleikamp #endif 540470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 541470decc6SDave Kleikamp while (tid_gt(tid, journal->j_commit_sequence)) { 542470decc6SDave Kleikamp jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n", 543470decc6SDave Kleikamp tid, journal->j_commit_sequence); 544470decc6SDave Kleikamp wake_up(&journal->j_wait_commit); 545470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 546470decc6SDave Kleikamp wait_event(journal->j_wait_done_commit, 547470decc6SDave Kleikamp !tid_gt(tid, journal->j_commit_sequence)); 548470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 549470decc6SDave Kleikamp } 550470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 551470decc6SDave Kleikamp 552470decc6SDave Kleikamp if (unlikely(is_journal_aborted(journal))) { 553470decc6SDave Kleikamp printk(KERN_EMERG "journal commit I/O error\n"); 554470decc6SDave Kleikamp err = -EIO; 555470decc6SDave Kleikamp } 556470decc6SDave Kleikamp return err; 557470decc6SDave Kleikamp } 558470decc6SDave Kleikamp 559470decc6SDave Kleikamp /* 560470decc6SDave Kleikamp * Log buffer allocation routines: 561470decc6SDave Kleikamp */ 562470decc6SDave Kleikamp 56318eba7aaSMingming Cao int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp) 564470decc6SDave Kleikamp { 565470decc6SDave Kleikamp unsigned long blocknr; 566470decc6SDave Kleikamp 567470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 568470decc6SDave Kleikamp J_ASSERT(journal->j_free > 1); 569470decc6SDave Kleikamp 570470decc6SDave Kleikamp blocknr = journal->j_head; 571470decc6SDave Kleikamp journal->j_head++; 572470decc6SDave Kleikamp journal->j_free--; 573470decc6SDave Kleikamp if (journal->j_head == journal->j_last) 574470decc6SDave Kleikamp journal->j_head = journal->j_first; 575470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 576f7f4bccbSMingming Cao return jbd2_journal_bmap(journal, blocknr, retp); 577470decc6SDave Kleikamp } 578470decc6SDave Kleikamp 579470decc6SDave Kleikamp /* 580470decc6SDave Kleikamp * Conversion of logical to physical block numbers for the journal 581470decc6SDave Kleikamp * 582470decc6SDave Kleikamp * On external journals the journal blocks are identity-mapped, so 583470decc6SDave Kleikamp * this is a no-op. If needed, we can use j_blk_offset - everything is 584470decc6SDave Kleikamp * ready. 585470decc6SDave Kleikamp */ 586f7f4bccbSMingming Cao int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr, 58718eba7aaSMingming Cao unsigned long long *retp) 588470decc6SDave Kleikamp { 589470decc6SDave Kleikamp int err = 0; 59018eba7aaSMingming Cao unsigned long long ret; 591470decc6SDave Kleikamp 592470decc6SDave Kleikamp if (journal->j_inode) { 593470decc6SDave Kleikamp ret = bmap(journal->j_inode, blocknr); 594470decc6SDave Kleikamp if (ret) 595470decc6SDave Kleikamp *retp = ret; 596470decc6SDave Kleikamp else { 597470decc6SDave Kleikamp char b[BDEVNAME_SIZE]; 598470decc6SDave Kleikamp 599470decc6SDave Kleikamp printk(KERN_ALERT "%s: journal block not found " 600470decc6SDave Kleikamp "at offset %lu on %s\n", 601470decc6SDave Kleikamp __FUNCTION__, 602470decc6SDave Kleikamp blocknr, 603470decc6SDave Kleikamp bdevname(journal->j_dev, b)); 604470decc6SDave Kleikamp err = -EIO; 605470decc6SDave Kleikamp __journal_abort_soft(journal, err); 606470decc6SDave Kleikamp } 607470decc6SDave Kleikamp } else { 608470decc6SDave Kleikamp *retp = blocknr; /* +journal->j_blk_offset */ 609470decc6SDave Kleikamp } 610470decc6SDave Kleikamp return err; 611470decc6SDave Kleikamp } 612470decc6SDave Kleikamp 613470decc6SDave Kleikamp /* 614470decc6SDave Kleikamp * We play buffer_head aliasing tricks to write data/metadata blocks to 615470decc6SDave Kleikamp * the journal without copying their contents, but for journal 616470decc6SDave Kleikamp * descriptor blocks we do need to generate bona fide buffers. 617470decc6SDave Kleikamp * 618f7f4bccbSMingming Cao * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying 619470decc6SDave Kleikamp * the buffer's contents they really should run flush_dcache_page(bh->b_page). 620470decc6SDave Kleikamp * But we don't bother doing that, so there will be coherency problems with 621470decc6SDave Kleikamp * mmaps of blockdevs which hold live JBD-controlled filesystems. 622470decc6SDave Kleikamp */ 623f7f4bccbSMingming Cao struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal) 624470decc6SDave Kleikamp { 625470decc6SDave Kleikamp struct buffer_head *bh; 62618eba7aaSMingming Cao unsigned long long blocknr; 627470decc6SDave Kleikamp int err; 628470decc6SDave Kleikamp 629f7f4bccbSMingming Cao err = jbd2_journal_next_log_block(journal, &blocknr); 630470decc6SDave Kleikamp 631470decc6SDave Kleikamp if (err) 632470decc6SDave Kleikamp return NULL; 633470decc6SDave Kleikamp 634470decc6SDave Kleikamp bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize); 635470decc6SDave Kleikamp lock_buffer(bh); 636470decc6SDave Kleikamp memset(bh->b_data, 0, journal->j_blocksize); 637470decc6SDave Kleikamp set_buffer_uptodate(bh); 638470decc6SDave Kleikamp unlock_buffer(bh); 639470decc6SDave Kleikamp BUFFER_TRACE(bh, "return this buffer"); 640f7f4bccbSMingming Cao return jbd2_journal_add_journal_head(bh); 641470decc6SDave Kleikamp } 642470decc6SDave Kleikamp 643470decc6SDave Kleikamp /* 644470decc6SDave Kleikamp * Management for journal control blocks: functions to create and 645470decc6SDave Kleikamp * destroy journal_t structures, and to initialise and read existing 646470decc6SDave Kleikamp * journal blocks from disk. */ 647470decc6SDave Kleikamp 648470decc6SDave Kleikamp /* First: create and setup a journal_t object in memory. We initialise 649470decc6SDave Kleikamp * very few fields yet: that has to wait until we have created the 650470decc6SDave Kleikamp * journal structures from from scratch, or loaded them from disk. */ 651470decc6SDave Kleikamp 652470decc6SDave Kleikamp static journal_t * journal_init_common (void) 653470decc6SDave Kleikamp { 654470decc6SDave Kleikamp journal_t *journal; 655470decc6SDave Kleikamp int err; 656470decc6SDave Kleikamp 657470decc6SDave Kleikamp journal = jbd_kmalloc(sizeof(*journal), GFP_KERNEL); 658470decc6SDave Kleikamp if (!journal) 659470decc6SDave Kleikamp goto fail; 660470decc6SDave Kleikamp memset(journal, 0, sizeof(*journal)); 661470decc6SDave Kleikamp 662470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_transaction_locked); 663470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_logspace); 664470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_done_commit); 665470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_checkpoint); 666470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_commit); 667470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_updates); 668470decc6SDave Kleikamp mutex_init(&journal->j_barrier); 669470decc6SDave Kleikamp mutex_init(&journal->j_checkpoint_mutex); 670470decc6SDave Kleikamp spin_lock_init(&journal->j_revoke_lock); 671470decc6SDave Kleikamp spin_lock_init(&journal->j_list_lock); 672470decc6SDave Kleikamp spin_lock_init(&journal->j_state_lock); 673470decc6SDave Kleikamp 674470decc6SDave Kleikamp journal->j_commit_interval = (HZ * JBD_DEFAULT_MAX_COMMIT_AGE); 675470decc6SDave Kleikamp 676470decc6SDave Kleikamp /* The journal is marked for error until we succeed with recovery! */ 677f7f4bccbSMingming Cao journal->j_flags = JBD2_ABORT; 678470decc6SDave Kleikamp 679470decc6SDave Kleikamp /* Set up a default-sized revoke table for the new mount. */ 680f7f4bccbSMingming Cao err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH); 681470decc6SDave Kleikamp if (err) { 682470decc6SDave Kleikamp kfree(journal); 683470decc6SDave Kleikamp goto fail; 684470decc6SDave Kleikamp } 685470decc6SDave Kleikamp return journal; 686470decc6SDave Kleikamp fail: 687470decc6SDave Kleikamp return NULL; 688470decc6SDave Kleikamp } 689470decc6SDave Kleikamp 690f7f4bccbSMingming Cao /* jbd2_journal_init_dev and jbd2_journal_init_inode: 691470decc6SDave Kleikamp * 692470decc6SDave Kleikamp * Create a journal structure assigned some fixed set of disk blocks to 693470decc6SDave Kleikamp * the journal. We don't actually touch those disk blocks yet, but we 694470decc6SDave Kleikamp * need to set up all of the mapping information to tell the journaling 695470decc6SDave Kleikamp * system where the journal blocks are. 696470decc6SDave Kleikamp * 697470decc6SDave Kleikamp */ 698470decc6SDave Kleikamp 699470decc6SDave Kleikamp /** 700f7f4bccbSMingming Cao * journal_t * jbd2_journal_init_dev() - creates an initialises a journal structure 701470decc6SDave Kleikamp * @bdev: Block device on which to create the journal 702470decc6SDave Kleikamp * @fs_dev: Device which hold journalled filesystem for this journal. 703470decc6SDave Kleikamp * @start: Block nr Start of journal. 704470decc6SDave Kleikamp * @len: Length of the journal in blocks. 705470decc6SDave Kleikamp * @blocksize: blocksize of journalling device 706470decc6SDave Kleikamp * @returns: a newly created journal_t * 707470decc6SDave Kleikamp * 708f7f4bccbSMingming Cao * jbd2_journal_init_dev creates a journal which maps a fixed contiguous 709470decc6SDave Kleikamp * range of blocks on an arbitrary block device. 710470decc6SDave Kleikamp * 711470decc6SDave Kleikamp */ 712f7f4bccbSMingming Cao journal_t * jbd2_journal_init_dev(struct block_device *bdev, 713470decc6SDave Kleikamp struct block_device *fs_dev, 71418eba7aaSMingming Cao unsigned long long start, int len, int blocksize) 715470decc6SDave Kleikamp { 716470decc6SDave Kleikamp journal_t *journal = journal_init_common(); 717470decc6SDave Kleikamp struct buffer_head *bh; 718470decc6SDave Kleikamp int n; 719470decc6SDave Kleikamp 720470decc6SDave Kleikamp if (!journal) 721470decc6SDave Kleikamp return NULL; 722470decc6SDave Kleikamp 723470decc6SDave Kleikamp /* journal descriptor can store up to n blocks -bzzz */ 724470decc6SDave Kleikamp journal->j_blocksize = blocksize; 725470decc6SDave Kleikamp n = journal->j_blocksize / sizeof(journal_block_tag_t); 726470decc6SDave Kleikamp journal->j_wbufsize = n; 727470decc6SDave Kleikamp journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL); 728470decc6SDave Kleikamp if (!journal->j_wbuf) { 729470decc6SDave Kleikamp printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n", 730470decc6SDave Kleikamp __FUNCTION__); 731470decc6SDave Kleikamp kfree(journal); 732470decc6SDave Kleikamp journal = NULL; 7335eb30790SDave Kleikamp goto out; 734470decc6SDave Kleikamp } 735470decc6SDave Kleikamp journal->j_dev = bdev; 736470decc6SDave Kleikamp journal->j_fs_dev = fs_dev; 737470decc6SDave Kleikamp journal->j_blk_offset = start; 738470decc6SDave Kleikamp journal->j_maxlen = len; 739470decc6SDave Kleikamp 740470decc6SDave Kleikamp bh = __getblk(journal->j_dev, start, journal->j_blocksize); 741470decc6SDave Kleikamp J_ASSERT(bh != NULL); 742470decc6SDave Kleikamp journal->j_sb_buffer = bh; 743470decc6SDave Kleikamp journal->j_superblock = (journal_superblock_t *)bh->b_data; 7445eb30790SDave Kleikamp out: 745470decc6SDave Kleikamp return journal; 746470decc6SDave Kleikamp } 747470decc6SDave Kleikamp 748470decc6SDave Kleikamp /** 749f7f4bccbSMingming Cao * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode. 750470decc6SDave Kleikamp * @inode: An inode to create the journal in 751470decc6SDave Kleikamp * 752f7f4bccbSMingming Cao * jbd2_journal_init_inode creates a journal which maps an on-disk inode as 753470decc6SDave Kleikamp * the journal. The inode must exist already, must support bmap() and 754470decc6SDave Kleikamp * must have all data blocks preallocated. 755470decc6SDave Kleikamp */ 756f7f4bccbSMingming Cao journal_t * jbd2_journal_init_inode (struct inode *inode) 757470decc6SDave Kleikamp { 758470decc6SDave Kleikamp struct buffer_head *bh; 759470decc6SDave Kleikamp journal_t *journal = journal_init_common(); 760470decc6SDave Kleikamp int err; 761470decc6SDave Kleikamp int n; 76218eba7aaSMingming Cao unsigned long long blocknr; 763470decc6SDave Kleikamp 764470decc6SDave Kleikamp if (!journal) 765470decc6SDave Kleikamp return NULL; 766470decc6SDave Kleikamp 767470decc6SDave Kleikamp journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev; 768470decc6SDave Kleikamp journal->j_inode = inode; 769470decc6SDave Kleikamp jbd_debug(1, 770470decc6SDave Kleikamp "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n", 771470decc6SDave Kleikamp journal, inode->i_sb->s_id, inode->i_ino, 772470decc6SDave Kleikamp (long long) inode->i_size, 773470decc6SDave Kleikamp inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize); 774470decc6SDave Kleikamp 775470decc6SDave Kleikamp journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits; 776470decc6SDave Kleikamp journal->j_blocksize = inode->i_sb->s_blocksize; 777470decc6SDave Kleikamp 778470decc6SDave Kleikamp /* journal descriptor can store up to n blocks -bzzz */ 779470decc6SDave Kleikamp n = journal->j_blocksize / sizeof(journal_block_tag_t); 780470decc6SDave Kleikamp journal->j_wbufsize = n; 781470decc6SDave Kleikamp journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL); 782470decc6SDave Kleikamp if (!journal->j_wbuf) { 783470decc6SDave Kleikamp printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n", 784470decc6SDave Kleikamp __FUNCTION__); 785470decc6SDave Kleikamp kfree(journal); 786470decc6SDave Kleikamp return NULL; 787470decc6SDave Kleikamp } 788470decc6SDave Kleikamp 789f7f4bccbSMingming Cao err = jbd2_journal_bmap(journal, 0, &blocknr); 790470decc6SDave Kleikamp /* If that failed, give up */ 791470decc6SDave Kleikamp if (err) { 792470decc6SDave Kleikamp printk(KERN_ERR "%s: Cannnot locate journal superblock\n", 793470decc6SDave Kleikamp __FUNCTION__); 794470decc6SDave Kleikamp kfree(journal); 795470decc6SDave Kleikamp return NULL; 796470decc6SDave Kleikamp } 797470decc6SDave Kleikamp 798470decc6SDave Kleikamp bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize); 799470decc6SDave Kleikamp J_ASSERT(bh != NULL); 800470decc6SDave Kleikamp journal->j_sb_buffer = bh; 801470decc6SDave Kleikamp journal->j_superblock = (journal_superblock_t *)bh->b_data; 802470decc6SDave Kleikamp 803470decc6SDave Kleikamp return journal; 804470decc6SDave Kleikamp } 805470decc6SDave Kleikamp 806470decc6SDave Kleikamp /* 807470decc6SDave Kleikamp * If the journal init or create aborts, we need to mark the journal 808470decc6SDave Kleikamp * superblock as being NULL to prevent the journal destroy from writing 809470decc6SDave Kleikamp * back a bogus superblock. 810470decc6SDave Kleikamp */ 811470decc6SDave Kleikamp static void journal_fail_superblock (journal_t *journal) 812470decc6SDave Kleikamp { 813470decc6SDave Kleikamp struct buffer_head *bh = journal->j_sb_buffer; 814470decc6SDave Kleikamp brelse(bh); 815470decc6SDave Kleikamp journal->j_sb_buffer = NULL; 816470decc6SDave Kleikamp } 817470decc6SDave Kleikamp 818470decc6SDave Kleikamp /* 819470decc6SDave Kleikamp * Given a journal_t structure, initialise the various fields for 820470decc6SDave Kleikamp * startup of a new journaling session. We use this both when creating 821470decc6SDave Kleikamp * a journal, and after recovering an old journal to reset it for 822470decc6SDave Kleikamp * subsequent use. 823470decc6SDave Kleikamp */ 824470decc6SDave Kleikamp 825470decc6SDave Kleikamp static int journal_reset(journal_t *journal) 826470decc6SDave Kleikamp { 827470decc6SDave Kleikamp journal_superblock_t *sb = journal->j_superblock; 82818eba7aaSMingming Cao unsigned long long first, last; 829470decc6SDave Kleikamp 830470decc6SDave Kleikamp first = be32_to_cpu(sb->s_first); 831470decc6SDave Kleikamp last = be32_to_cpu(sb->s_maxlen); 832470decc6SDave Kleikamp 833470decc6SDave Kleikamp journal->j_first = first; 834470decc6SDave Kleikamp journal->j_last = last; 835470decc6SDave Kleikamp 836470decc6SDave Kleikamp journal->j_head = first; 837470decc6SDave Kleikamp journal->j_tail = first; 838470decc6SDave Kleikamp journal->j_free = last - first; 839470decc6SDave Kleikamp 840470decc6SDave Kleikamp journal->j_tail_sequence = journal->j_transaction_sequence; 841470decc6SDave Kleikamp journal->j_commit_sequence = journal->j_transaction_sequence - 1; 842470decc6SDave Kleikamp journal->j_commit_request = journal->j_commit_sequence; 843470decc6SDave Kleikamp 844470decc6SDave Kleikamp journal->j_max_transaction_buffers = journal->j_maxlen / 4; 845470decc6SDave Kleikamp 846470decc6SDave Kleikamp /* Add the dynamic fields and write it to disk. */ 847f7f4bccbSMingming Cao jbd2_journal_update_superblock(journal, 1); 84897f06784SPavel Emelianov return jbd2_journal_start_thread(journal); 849470decc6SDave Kleikamp } 850470decc6SDave Kleikamp 851470decc6SDave Kleikamp /** 852f7f4bccbSMingming Cao * int jbd2_journal_create() - Initialise the new journal file 853470decc6SDave Kleikamp * @journal: Journal to create. This structure must have been initialised 854470decc6SDave Kleikamp * 855470decc6SDave Kleikamp * Given a journal_t structure which tells us which disk blocks we can 856470decc6SDave Kleikamp * use, create a new journal superblock and initialise all of the 857470decc6SDave Kleikamp * journal fields from scratch. 858470decc6SDave Kleikamp **/ 859f7f4bccbSMingming Cao int jbd2_journal_create(journal_t *journal) 860470decc6SDave Kleikamp { 86118eba7aaSMingming Cao unsigned long long blocknr; 862470decc6SDave Kleikamp struct buffer_head *bh; 863470decc6SDave Kleikamp journal_superblock_t *sb; 864470decc6SDave Kleikamp int i, err; 865470decc6SDave Kleikamp 866f7f4bccbSMingming Cao if (journal->j_maxlen < JBD2_MIN_JOURNAL_BLOCKS) { 867470decc6SDave Kleikamp printk (KERN_ERR "Journal length (%d blocks) too short.\n", 868470decc6SDave Kleikamp journal->j_maxlen); 869470decc6SDave Kleikamp journal_fail_superblock(journal); 870470decc6SDave Kleikamp return -EINVAL; 871470decc6SDave Kleikamp } 872470decc6SDave Kleikamp 873470decc6SDave Kleikamp if (journal->j_inode == NULL) { 874470decc6SDave Kleikamp /* 875470decc6SDave Kleikamp * We don't know what block to start at! 876470decc6SDave Kleikamp */ 877470decc6SDave Kleikamp printk(KERN_EMERG 878470decc6SDave Kleikamp "%s: creation of journal on external device!\n", 879470decc6SDave Kleikamp __FUNCTION__); 880470decc6SDave Kleikamp BUG(); 881470decc6SDave Kleikamp } 882470decc6SDave Kleikamp 883470decc6SDave Kleikamp /* Zero out the entire journal on disk. We cannot afford to 884f7f4bccbSMingming Cao have any blocks on disk beginning with JBD2_MAGIC_NUMBER. */ 885470decc6SDave Kleikamp jbd_debug(1, "JBD: Zeroing out journal blocks...\n"); 886470decc6SDave Kleikamp for (i = 0; i < journal->j_maxlen; i++) { 887f7f4bccbSMingming Cao err = jbd2_journal_bmap(journal, i, &blocknr); 888470decc6SDave Kleikamp if (err) 889470decc6SDave Kleikamp return err; 890470decc6SDave Kleikamp bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize); 891470decc6SDave Kleikamp lock_buffer(bh); 892470decc6SDave Kleikamp memset (bh->b_data, 0, journal->j_blocksize); 893470decc6SDave Kleikamp BUFFER_TRACE(bh, "marking dirty"); 894470decc6SDave Kleikamp mark_buffer_dirty(bh); 895470decc6SDave Kleikamp BUFFER_TRACE(bh, "marking uptodate"); 896470decc6SDave Kleikamp set_buffer_uptodate(bh); 897470decc6SDave Kleikamp unlock_buffer(bh); 898470decc6SDave Kleikamp __brelse(bh); 899470decc6SDave Kleikamp } 900470decc6SDave Kleikamp 901470decc6SDave Kleikamp sync_blockdev(journal->j_dev); 902470decc6SDave Kleikamp jbd_debug(1, "JBD: journal cleared.\n"); 903470decc6SDave Kleikamp 904470decc6SDave Kleikamp /* OK, fill in the initial static fields in the new superblock */ 905470decc6SDave Kleikamp sb = journal->j_superblock; 906470decc6SDave Kleikamp 907f7f4bccbSMingming Cao sb->s_header.h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER); 908f7f4bccbSMingming Cao sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2); 909470decc6SDave Kleikamp 910470decc6SDave Kleikamp sb->s_blocksize = cpu_to_be32(journal->j_blocksize); 911470decc6SDave Kleikamp sb->s_maxlen = cpu_to_be32(journal->j_maxlen); 912470decc6SDave Kleikamp sb->s_first = cpu_to_be32(1); 913470decc6SDave Kleikamp 914470decc6SDave Kleikamp journal->j_transaction_sequence = 1; 915470decc6SDave Kleikamp 916f7f4bccbSMingming Cao journal->j_flags &= ~JBD2_ABORT; 917470decc6SDave Kleikamp journal->j_format_version = 2; 918470decc6SDave Kleikamp 919470decc6SDave Kleikamp return journal_reset(journal); 920470decc6SDave Kleikamp } 921470decc6SDave Kleikamp 922470decc6SDave Kleikamp /** 923f7f4bccbSMingming Cao * void jbd2_journal_update_superblock() - Update journal sb on disk. 924470decc6SDave Kleikamp * @journal: The journal to update. 925470decc6SDave Kleikamp * @wait: Set to '0' if you don't want to wait for IO completion. 926470decc6SDave Kleikamp * 927470decc6SDave Kleikamp * Update a journal's dynamic superblock fields and write it to disk, 928470decc6SDave Kleikamp * optionally waiting for the IO to complete. 929470decc6SDave Kleikamp */ 930f7f4bccbSMingming Cao void jbd2_journal_update_superblock(journal_t *journal, int wait) 931470decc6SDave Kleikamp { 932470decc6SDave Kleikamp journal_superblock_t *sb = journal->j_superblock; 933470decc6SDave Kleikamp struct buffer_head *bh = journal->j_sb_buffer; 934470decc6SDave Kleikamp 935470decc6SDave Kleikamp /* 936470decc6SDave Kleikamp * As a special case, if the on-disk copy is already marked as needing 937470decc6SDave Kleikamp * no recovery (s_start == 0) and there are no outstanding transactions 938470decc6SDave Kleikamp * in the filesystem, then we can safely defer the superblock update 939f7f4bccbSMingming Cao * until the next commit by setting JBD2_FLUSHED. This avoids 940470decc6SDave Kleikamp * attempting a write to a potential-readonly device. 941470decc6SDave Kleikamp */ 942470decc6SDave Kleikamp if (sb->s_start == 0 && journal->j_tail_sequence == 943470decc6SDave Kleikamp journal->j_transaction_sequence) { 944470decc6SDave Kleikamp jbd_debug(1,"JBD: Skipping superblock update on recovered sb " 945470decc6SDave Kleikamp "(start %ld, seq %d, errno %d)\n", 946470decc6SDave Kleikamp journal->j_tail, journal->j_tail_sequence, 947470decc6SDave Kleikamp journal->j_errno); 948470decc6SDave Kleikamp goto out; 949470decc6SDave Kleikamp } 950470decc6SDave Kleikamp 951470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 952470decc6SDave Kleikamp jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n", 953470decc6SDave Kleikamp journal->j_tail, journal->j_tail_sequence, journal->j_errno); 954470decc6SDave Kleikamp 955470decc6SDave Kleikamp sb->s_sequence = cpu_to_be32(journal->j_tail_sequence); 956470decc6SDave Kleikamp sb->s_start = cpu_to_be32(journal->j_tail); 957470decc6SDave Kleikamp sb->s_errno = cpu_to_be32(journal->j_errno); 958470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 959470decc6SDave Kleikamp 960470decc6SDave Kleikamp BUFFER_TRACE(bh, "marking dirty"); 961470decc6SDave Kleikamp mark_buffer_dirty(bh); 962470decc6SDave Kleikamp if (wait) 963470decc6SDave Kleikamp sync_dirty_buffer(bh); 964470decc6SDave Kleikamp else 965470decc6SDave Kleikamp ll_rw_block(SWRITE, 1, &bh); 966470decc6SDave Kleikamp 967470decc6SDave Kleikamp out: 968470decc6SDave Kleikamp /* If we have just flushed the log (by marking s_start==0), then 969470decc6SDave Kleikamp * any future commit will have to be careful to update the 970470decc6SDave Kleikamp * superblock again to re-record the true start of the log. */ 971470decc6SDave Kleikamp 972470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 973470decc6SDave Kleikamp if (sb->s_start) 974f7f4bccbSMingming Cao journal->j_flags &= ~JBD2_FLUSHED; 975470decc6SDave Kleikamp else 976f7f4bccbSMingming Cao journal->j_flags |= JBD2_FLUSHED; 977470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 978470decc6SDave Kleikamp } 979470decc6SDave Kleikamp 980470decc6SDave Kleikamp /* 981470decc6SDave Kleikamp * Read the superblock for a given journal, performing initial 982470decc6SDave Kleikamp * validation of the format. 983470decc6SDave Kleikamp */ 984470decc6SDave Kleikamp 985470decc6SDave Kleikamp static int journal_get_superblock(journal_t *journal) 986470decc6SDave Kleikamp { 987470decc6SDave Kleikamp struct buffer_head *bh; 988470decc6SDave Kleikamp journal_superblock_t *sb; 989470decc6SDave Kleikamp int err = -EIO; 990470decc6SDave Kleikamp 991470decc6SDave Kleikamp bh = journal->j_sb_buffer; 992470decc6SDave Kleikamp 993470decc6SDave Kleikamp J_ASSERT(bh != NULL); 994470decc6SDave Kleikamp if (!buffer_uptodate(bh)) { 995470decc6SDave Kleikamp ll_rw_block(READ, 1, &bh); 996470decc6SDave Kleikamp wait_on_buffer(bh); 997470decc6SDave Kleikamp if (!buffer_uptodate(bh)) { 998470decc6SDave Kleikamp printk (KERN_ERR 999470decc6SDave Kleikamp "JBD: IO error reading journal superblock\n"); 1000470decc6SDave Kleikamp goto out; 1001470decc6SDave Kleikamp } 1002470decc6SDave Kleikamp } 1003470decc6SDave Kleikamp 1004470decc6SDave Kleikamp sb = journal->j_superblock; 1005470decc6SDave Kleikamp 1006470decc6SDave Kleikamp err = -EINVAL; 1007470decc6SDave Kleikamp 1008f7f4bccbSMingming Cao if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) || 1009470decc6SDave Kleikamp sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) { 1010470decc6SDave Kleikamp printk(KERN_WARNING "JBD: no valid journal superblock found\n"); 1011470decc6SDave Kleikamp goto out; 1012470decc6SDave Kleikamp } 1013470decc6SDave Kleikamp 1014470decc6SDave Kleikamp switch(be32_to_cpu(sb->s_header.h_blocktype)) { 1015f7f4bccbSMingming Cao case JBD2_SUPERBLOCK_V1: 1016470decc6SDave Kleikamp journal->j_format_version = 1; 1017470decc6SDave Kleikamp break; 1018f7f4bccbSMingming Cao case JBD2_SUPERBLOCK_V2: 1019470decc6SDave Kleikamp journal->j_format_version = 2; 1020470decc6SDave Kleikamp break; 1021470decc6SDave Kleikamp default: 1022470decc6SDave Kleikamp printk(KERN_WARNING "JBD: unrecognised superblock format ID\n"); 1023470decc6SDave Kleikamp goto out; 1024470decc6SDave Kleikamp } 1025470decc6SDave Kleikamp 1026470decc6SDave Kleikamp if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen) 1027470decc6SDave Kleikamp journal->j_maxlen = be32_to_cpu(sb->s_maxlen); 1028470decc6SDave Kleikamp else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) { 1029470decc6SDave Kleikamp printk (KERN_WARNING "JBD: journal file too short\n"); 1030470decc6SDave Kleikamp goto out; 1031470decc6SDave Kleikamp } 1032470decc6SDave Kleikamp 1033470decc6SDave Kleikamp return 0; 1034470decc6SDave Kleikamp 1035470decc6SDave Kleikamp out: 1036470decc6SDave Kleikamp journal_fail_superblock(journal); 1037470decc6SDave Kleikamp return err; 1038470decc6SDave Kleikamp } 1039470decc6SDave Kleikamp 1040470decc6SDave Kleikamp /* 1041470decc6SDave Kleikamp * Load the on-disk journal superblock and read the key fields into the 1042470decc6SDave Kleikamp * journal_t. 1043470decc6SDave Kleikamp */ 1044470decc6SDave Kleikamp 1045470decc6SDave Kleikamp static int load_superblock(journal_t *journal) 1046470decc6SDave Kleikamp { 1047470decc6SDave Kleikamp int err; 1048470decc6SDave Kleikamp journal_superblock_t *sb; 1049470decc6SDave Kleikamp 1050470decc6SDave Kleikamp err = journal_get_superblock(journal); 1051470decc6SDave Kleikamp if (err) 1052470decc6SDave Kleikamp return err; 1053470decc6SDave Kleikamp 1054470decc6SDave Kleikamp sb = journal->j_superblock; 1055470decc6SDave Kleikamp 1056470decc6SDave Kleikamp journal->j_tail_sequence = be32_to_cpu(sb->s_sequence); 1057470decc6SDave Kleikamp journal->j_tail = be32_to_cpu(sb->s_start); 1058470decc6SDave Kleikamp journal->j_first = be32_to_cpu(sb->s_first); 1059470decc6SDave Kleikamp journal->j_last = be32_to_cpu(sb->s_maxlen); 1060470decc6SDave Kleikamp journal->j_errno = be32_to_cpu(sb->s_errno); 1061470decc6SDave Kleikamp 1062470decc6SDave Kleikamp return 0; 1063470decc6SDave Kleikamp } 1064470decc6SDave Kleikamp 1065470decc6SDave Kleikamp 1066470decc6SDave Kleikamp /** 1067f7f4bccbSMingming Cao * int jbd2_journal_load() - Read journal from disk. 1068470decc6SDave Kleikamp * @journal: Journal to act on. 1069470decc6SDave Kleikamp * 1070470decc6SDave Kleikamp * Given a journal_t structure which tells us which disk blocks contain 1071470decc6SDave Kleikamp * a journal, read the journal from disk to initialise the in-memory 1072470decc6SDave Kleikamp * structures. 1073470decc6SDave Kleikamp */ 1074f7f4bccbSMingming Cao int jbd2_journal_load(journal_t *journal) 1075470decc6SDave Kleikamp { 1076470decc6SDave Kleikamp int err; 1077470decc6SDave Kleikamp journal_superblock_t *sb; 1078470decc6SDave Kleikamp 1079470decc6SDave Kleikamp err = load_superblock(journal); 1080470decc6SDave Kleikamp if (err) 1081470decc6SDave Kleikamp return err; 1082470decc6SDave Kleikamp 1083470decc6SDave Kleikamp sb = journal->j_superblock; 1084470decc6SDave Kleikamp /* If this is a V2 superblock, then we have to check the 1085470decc6SDave Kleikamp * features flags on it. */ 1086470decc6SDave Kleikamp 1087470decc6SDave Kleikamp if (journal->j_format_version >= 2) { 1088470decc6SDave Kleikamp if ((sb->s_feature_ro_compat & 1089f7f4bccbSMingming Cao ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) || 1090470decc6SDave Kleikamp (sb->s_feature_incompat & 1091f7f4bccbSMingming Cao ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) { 1092470decc6SDave Kleikamp printk (KERN_WARNING 1093470decc6SDave Kleikamp "JBD: Unrecognised features on journal\n"); 1094470decc6SDave Kleikamp return -EINVAL; 1095470decc6SDave Kleikamp } 1096470decc6SDave Kleikamp } 1097470decc6SDave Kleikamp 1098470decc6SDave Kleikamp /* 1099470decc6SDave Kleikamp * Create a slab for this blocksize 1100470decc6SDave Kleikamp */ 1101f7f4bccbSMingming Cao err = jbd2_journal_create_jbd_slab(be32_to_cpu(sb->s_blocksize)); 1102470decc6SDave Kleikamp if (err) 1103470decc6SDave Kleikamp return err; 1104470decc6SDave Kleikamp 1105470decc6SDave Kleikamp /* Let the recovery code check whether it needs to recover any 1106470decc6SDave Kleikamp * data from the journal. */ 1107f7f4bccbSMingming Cao if (jbd2_journal_recover(journal)) 1108470decc6SDave Kleikamp goto recovery_error; 1109470decc6SDave Kleikamp 1110470decc6SDave Kleikamp /* OK, we've finished with the dynamic journal bits: 1111470decc6SDave Kleikamp * reinitialise the dynamic contents of the superblock in memory 1112470decc6SDave Kleikamp * and reset them on disk. */ 1113470decc6SDave Kleikamp if (journal_reset(journal)) 1114470decc6SDave Kleikamp goto recovery_error; 1115470decc6SDave Kleikamp 1116f7f4bccbSMingming Cao journal->j_flags &= ~JBD2_ABORT; 1117f7f4bccbSMingming Cao journal->j_flags |= JBD2_LOADED; 1118470decc6SDave Kleikamp return 0; 1119470decc6SDave Kleikamp 1120470decc6SDave Kleikamp recovery_error: 1121470decc6SDave Kleikamp printk (KERN_WARNING "JBD: recovery failed\n"); 1122470decc6SDave Kleikamp return -EIO; 1123470decc6SDave Kleikamp } 1124470decc6SDave Kleikamp 1125470decc6SDave Kleikamp /** 1126f7f4bccbSMingming Cao * void jbd2_journal_destroy() - Release a journal_t structure. 1127470decc6SDave Kleikamp * @journal: Journal to act on. 1128470decc6SDave Kleikamp * 1129470decc6SDave Kleikamp * Release a journal_t structure once it is no longer in use by the 1130470decc6SDave Kleikamp * journaled object. 1131470decc6SDave Kleikamp */ 1132f7f4bccbSMingming Cao void jbd2_journal_destroy(journal_t *journal) 1133470decc6SDave Kleikamp { 1134470decc6SDave Kleikamp /* Wait for the commit thread to wake up and die. */ 1135470decc6SDave Kleikamp journal_kill_thread(journal); 1136470decc6SDave Kleikamp 1137470decc6SDave Kleikamp /* Force a final log commit */ 1138470decc6SDave Kleikamp if (journal->j_running_transaction) 1139f7f4bccbSMingming Cao jbd2_journal_commit_transaction(journal); 1140470decc6SDave Kleikamp 1141470decc6SDave Kleikamp /* Force any old transactions to disk */ 1142470decc6SDave Kleikamp 1143470decc6SDave Kleikamp /* Totally anal locking here... */ 1144470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 1145470decc6SDave Kleikamp while (journal->j_checkpoint_transactions != NULL) { 1146470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 1147f7f4bccbSMingming Cao jbd2_log_do_checkpoint(journal); 1148470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 1149470decc6SDave Kleikamp } 1150470decc6SDave Kleikamp 1151470decc6SDave Kleikamp J_ASSERT(journal->j_running_transaction == NULL); 1152470decc6SDave Kleikamp J_ASSERT(journal->j_committing_transaction == NULL); 1153470decc6SDave Kleikamp J_ASSERT(journal->j_checkpoint_transactions == NULL); 1154470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 1155470decc6SDave Kleikamp 1156470decc6SDave Kleikamp /* We can now mark the journal as empty. */ 1157470decc6SDave Kleikamp journal->j_tail = 0; 1158470decc6SDave Kleikamp journal->j_tail_sequence = ++journal->j_transaction_sequence; 1159470decc6SDave Kleikamp if (journal->j_sb_buffer) { 1160f7f4bccbSMingming Cao jbd2_journal_update_superblock(journal, 1); 1161470decc6SDave Kleikamp brelse(journal->j_sb_buffer); 1162470decc6SDave Kleikamp } 1163470decc6SDave Kleikamp 1164470decc6SDave Kleikamp if (journal->j_inode) 1165470decc6SDave Kleikamp iput(journal->j_inode); 1166470decc6SDave Kleikamp if (journal->j_revoke) 1167f7f4bccbSMingming Cao jbd2_journal_destroy_revoke(journal); 1168470decc6SDave Kleikamp kfree(journal->j_wbuf); 1169470decc6SDave Kleikamp kfree(journal); 1170470decc6SDave Kleikamp } 1171470decc6SDave Kleikamp 1172470decc6SDave Kleikamp 1173470decc6SDave Kleikamp /** 1174f7f4bccbSMingming Cao *int jbd2_journal_check_used_features () - Check if features specified are used. 1175470decc6SDave Kleikamp * @journal: Journal to check. 1176470decc6SDave Kleikamp * @compat: bitmask of compatible features 1177470decc6SDave Kleikamp * @ro: bitmask of features that force read-only mount 1178470decc6SDave Kleikamp * @incompat: bitmask of incompatible features 1179470decc6SDave Kleikamp * 1180470decc6SDave Kleikamp * Check whether the journal uses all of a given set of 1181470decc6SDave Kleikamp * features. Return true (non-zero) if it does. 1182470decc6SDave Kleikamp **/ 1183470decc6SDave Kleikamp 1184f7f4bccbSMingming Cao int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat, 1185470decc6SDave Kleikamp unsigned long ro, unsigned long incompat) 1186470decc6SDave Kleikamp { 1187470decc6SDave Kleikamp journal_superblock_t *sb; 1188470decc6SDave Kleikamp 1189470decc6SDave Kleikamp if (!compat && !ro && !incompat) 1190470decc6SDave Kleikamp return 1; 1191470decc6SDave Kleikamp if (journal->j_format_version == 1) 1192470decc6SDave Kleikamp return 0; 1193470decc6SDave Kleikamp 1194470decc6SDave Kleikamp sb = journal->j_superblock; 1195470decc6SDave Kleikamp 1196470decc6SDave Kleikamp if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) && 1197470decc6SDave Kleikamp ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) && 1198470decc6SDave Kleikamp ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat)) 1199470decc6SDave Kleikamp return 1; 1200470decc6SDave Kleikamp 1201470decc6SDave Kleikamp return 0; 1202470decc6SDave Kleikamp } 1203470decc6SDave Kleikamp 1204470decc6SDave Kleikamp /** 1205f7f4bccbSMingming Cao * int jbd2_journal_check_available_features() - Check feature set in journalling layer 1206470decc6SDave Kleikamp * @journal: Journal to check. 1207470decc6SDave Kleikamp * @compat: bitmask of compatible features 1208470decc6SDave Kleikamp * @ro: bitmask of features that force read-only mount 1209470decc6SDave Kleikamp * @incompat: bitmask of incompatible features 1210470decc6SDave Kleikamp * 1211470decc6SDave Kleikamp * Check whether the journaling code supports the use of 1212470decc6SDave Kleikamp * all of a given set of features on this journal. Return true 1213470decc6SDave Kleikamp * (non-zero) if it can. */ 1214470decc6SDave Kleikamp 1215f7f4bccbSMingming Cao int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat, 1216470decc6SDave Kleikamp unsigned long ro, unsigned long incompat) 1217470decc6SDave Kleikamp { 1218470decc6SDave Kleikamp journal_superblock_t *sb; 1219470decc6SDave Kleikamp 1220470decc6SDave Kleikamp if (!compat && !ro && !incompat) 1221470decc6SDave Kleikamp return 1; 1222470decc6SDave Kleikamp 1223470decc6SDave Kleikamp sb = journal->j_superblock; 1224470decc6SDave Kleikamp 1225470decc6SDave Kleikamp /* We can support any known requested features iff the 1226470decc6SDave Kleikamp * superblock is in version 2. Otherwise we fail to support any 1227470decc6SDave Kleikamp * extended sb features. */ 1228470decc6SDave Kleikamp 1229470decc6SDave Kleikamp if (journal->j_format_version != 2) 1230470decc6SDave Kleikamp return 0; 1231470decc6SDave Kleikamp 1232f7f4bccbSMingming Cao if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat && 1233f7f4bccbSMingming Cao (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro && 1234f7f4bccbSMingming Cao (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat) 1235470decc6SDave Kleikamp return 1; 1236470decc6SDave Kleikamp 1237470decc6SDave Kleikamp return 0; 1238470decc6SDave Kleikamp } 1239470decc6SDave Kleikamp 1240470decc6SDave Kleikamp /** 1241f7f4bccbSMingming Cao * int jbd2_journal_set_features () - Mark a given journal feature in the superblock 1242470decc6SDave Kleikamp * @journal: Journal to act on. 1243470decc6SDave Kleikamp * @compat: bitmask of compatible features 1244470decc6SDave Kleikamp * @ro: bitmask of features that force read-only mount 1245470decc6SDave Kleikamp * @incompat: bitmask of incompatible features 1246470decc6SDave Kleikamp * 1247470decc6SDave Kleikamp * Mark a given journal feature as present on the 1248470decc6SDave Kleikamp * superblock. Returns true if the requested features could be set. 1249470decc6SDave Kleikamp * 1250470decc6SDave Kleikamp */ 1251470decc6SDave Kleikamp 1252f7f4bccbSMingming Cao int jbd2_journal_set_features (journal_t *journal, unsigned long compat, 1253470decc6SDave Kleikamp unsigned long ro, unsigned long incompat) 1254470decc6SDave Kleikamp { 1255470decc6SDave Kleikamp journal_superblock_t *sb; 1256470decc6SDave Kleikamp 1257f7f4bccbSMingming Cao if (jbd2_journal_check_used_features(journal, compat, ro, incompat)) 1258470decc6SDave Kleikamp return 1; 1259470decc6SDave Kleikamp 1260f7f4bccbSMingming Cao if (!jbd2_journal_check_available_features(journal, compat, ro, incompat)) 1261470decc6SDave Kleikamp return 0; 1262470decc6SDave Kleikamp 1263470decc6SDave Kleikamp jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n", 1264470decc6SDave Kleikamp compat, ro, incompat); 1265470decc6SDave Kleikamp 1266470decc6SDave Kleikamp sb = journal->j_superblock; 1267470decc6SDave Kleikamp 1268470decc6SDave Kleikamp sb->s_feature_compat |= cpu_to_be32(compat); 1269470decc6SDave Kleikamp sb->s_feature_ro_compat |= cpu_to_be32(ro); 1270470decc6SDave Kleikamp sb->s_feature_incompat |= cpu_to_be32(incompat); 1271470decc6SDave Kleikamp 1272470decc6SDave Kleikamp return 1; 1273470decc6SDave Kleikamp } 1274470decc6SDave Kleikamp 1275470decc6SDave Kleikamp 1276470decc6SDave Kleikamp /** 1277f7f4bccbSMingming Cao * int jbd2_journal_update_format () - Update on-disk journal structure. 1278470decc6SDave Kleikamp * @journal: Journal to act on. 1279470decc6SDave Kleikamp * 1280470decc6SDave Kleikamp * Given an initialised but unloaded journal struct, poke about in the 1281470decc6SDave Kleikamp * on-disk structure to update it to the most recent supported version. 1282470decc6SDave Kleikamp */ 1283f7f4bccbSMingming Cao int jbd2_journal_update_format (journal_t *journal) 1284470decc6SDave Kleikamp { 1285470decc6SDave Kleikamp journal_superblock_t *sb; 1286470decc6SDave Kleikamp int err; 1287470decc6SDave Kleikamp 1288470decc6SDave Kleikamp err = journal_get_superblock(journal); 1289470decc6SDave Kleikamp if (err) 1290470decc6SDave Kleikamp return err; 1291470decc6SDave Kleikamp 1292470decc6SDave Kleikamp sb = journal->j_superblock; 1293470decc6SDave Kleikamp 1294470decc6SDave Kleikamp switch (be32_to_cpu(sb->s_header.h_blocktype)) { 1295f7f4bccbSMingming Cao case JBD2_SUPERBLOCK_V2: 1296470decc6SDave Kleikamp return 0; 1297f7f4bccbSMingming Cao case JBD2_SUPERBLOCK_V1: 1298470decc6SDave Kleikamp return journal_convert_superblock_v1(journal, sb); 1299470decc6SDave Kleikamp default: 1300470decc6SDave Kleikamp break; 1301470decc6SDave Kleikamp } 1302470decc6SDave Kleikamp return -EINVAL; 1303470decc6SDave Kleikamp } 1304470decc6SDave Kleikamp 1305470decc6SDave Kleikamp static int journal_convert_superblock_v1(journal_t *journal, 1306470decc6SDave Kleikamp journal_superblock_t *sb) 1307470decc6SDave Kleikamp { 1308470decc6SDave Kleikamp int offset, blocksize; 1309470decc6SDave Kleikamp struct buffer_head *bh; 1310470decc6SDave Kleikamp 1311470decc6SDave Kleikamp printk(KERN_WARNING 1312470decc6SDave Kleikamp "JBD: Converting superblock from version 1 to 2.\n"); 1313470decc6SDave Kleikamp 1314470decc6SDave Kleikamp /* Pre-initialise new fields to zero */ 1315470decc6SDave Kleikamp offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb); 1316470decc6SDave Kleikamp blocksize = be32_to_cpu(sb->s_blocksize); 1317470decc6SDave Kleikamp memset(&sb->s_feature_compat, 0, blocksize-offset); 1318470decc6SDave Kleikamp 1319470decc6SDave Kleikamp sb->s_nr_users = cpu_to_be32(1); 1320f7f4bccbSMingming Cao sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2); 1321470decc6SDave Kleikamp journal->j_format_version = 2; 1322470decc6SDave Kleikamp 1323470decc6SDave Kleikamp bh = journal->j_sb_buffer; 1324470decc6SDave Kleikamp BUFFER_TRACE(bh, "marking dirty"); 1325470decc6SDave Kleikamp mark_buffer_dirty(bh); 1326470decc6SDave Kleikamp sync_dirty_buffer(bh); 1327470decc6SDave Kleikamp return 0; 1328470decc6SDave Kleikamp } 1329470decc6SDave Kleikamp 1330470decc6SDave Kleikamp 1331470decc6SDave Kleikamp /** 1332f7f4bccbSMingming Cao * int jbd2_journal_flush () - Flush journal 1333470decc6SDave Kleikamp * @journal: Journal to act on. 1334470decc6SDave Kleikamp * 1335470decc6SDave Kleikamp * Flush all data for a given journal to disk and empty the journal. 1336470decc6SDave Kleikamp * Filesystems can use this when remounting readonly to ensure that 1337470decc6SDave Kleikamp * recovery does not need to happen on remount. 1338470decc6SDave Kleikamp */ 1339470decc6SDave Kleikamp 1340f7f4bccbSMingming Cao int jbd2_journal_flush(journal_t *journal) 1341470decc6SDave Kleikamp { 1342470decc6SDave Kleikamp int err = 0; 1343470decc6SDave Kleikamp transaction_t *transaction = NULL; 1344470decc6SDave Kleikamp unsigned long old_tail; 1345470decc6SDave Kleikamp 1346470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 1347470decc6SDave Kleikamp 1348470decc6SDave Kleikamp /* Force everything buffered to the log... */ 1349470decc6SDave Kleikamp if (journal->j_running_transaction) { 1350470decc6SDave Kleikamp transaction = journal->j_running_transaction; 1351f7f4bccbSMingming Cao __jbd2_log_start_commit(journal, transaction->t_tid); 1352470decc6SDave Kleikamp } else if (journal->j_committing_transaction) 1353470decc6SDave Kleikamp transaction = journal->j_committing_transaction; 1354470decc6SDave Kleikamp 1355470decc6SDave Kleikamp /* Wait for the log commit to complete... */ 1356470decc6SDave Kleikamp if (transaction) { 1357470decc6SDave Kleikamp tid_t tid = transaction->t_tid; 1358470decc6SDave Kleikamp 1359470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1360f7f4bccbSMingming Cao jbd2_log_wait_commit(journal, tid); 1361470decc6SDave Kleikamp } else { 1362470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1363470decc6SDave Kleikamp } 1364470decc6SDave Kleikamp 1365470decc6SDave Kleikamp /* ...and flush everything in the log out to disk. */ 1366470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 1367470decc6SDave Kleikamp while (!err && journal->j_checkpoint_transactions != NULL) { 1368470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 1369f7f4bccbSMingming Cao err = jbd2_log_do_checkpoint(journal); 1370470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 1371470decc6SDave Kleikamp } 1372470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 1373f7f4bccbSMingming Cao jbd2_cleanup_journal_tail(journal); 1374470decc6SDave Kleikamp 1375470decc6SDave Kleikamp /* Finally, mark the journal as really needing no recovery. 1376470decc6SDave Kleikamp * This sets s_start==0 in the underlying superblock, which is 1377470decc6SDave Kleikamp * the magic code for a fully-recovered superblock. Any future 1378470decc6SDave Kleikamp * commits of data to the journal will restore the current 1379470decc6SDave Kleikamp * s_start value. */ 1380470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 1381470decc6SDave Kleikamp old_tail = journal->j_tail; 1382470decc6SDave Kleikamp journal->j_tail = 0; 1383470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1384f7f4bccbSMingming Cao jbd2_journal_update_superblock(journal, 1); 1385470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 1386470decc6SDave Kleikamp journal->j_tail = old_tail; 1387470decc6SDave Kleikamp 1388470decc6SDave Kleikamp J_ASSERT(!journal->j_running_transaction); 1389470decc6SDave Kleikamp J_ASSERT(!journal->j_committing_transaction); 1390470decc6SDave Kleikamp J_ASSERT(!journal->j_checkpoint_transactions); 1391470decc6SDave Kleikamp J_ASSERT(journal->j_head == journal->j_tail); 1392470decc6SDave Kleikamp J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence); 1393470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1394470decc6SDave Kleikamp return err; 1395470decc6SDave Kleikamp } 1396470decc6SDave Kleikamp 1397470decc6SDave Kleikamp /** 1398f7f4bccbSMingming Cao * int jbd2_journal_wipe() - Wipe journal contents 1399470decc6SDave Kleikamp * @journal: Journal to act on. 1400470decc6SDave Kleikamp * @write: flag (see below) 1401470decc6SDave Kleikamp * 1402470decc6SDave Kleikamp * Wipe out all of the contents of a journal, safely. This will produce 1403470decc6SDave Kleikamp * a warning if the journal contains any valid recovery information. 1404f7f4bccbSMingming Cao * Must be called between journal_init_*() and jbd2_journal_load(). 1405470decc6SDave Kleikamp * 1406470decc6SDave Kleikamp * If 'write' is non-zero, then we wipe out the journal on disk; otherwise 1407470decc6SDave Kleikamp * we merely suppress recovery. 1408470decc6SDave Kleikamp */ 1409470decc6SDave Kleikamp 1410f7f4bccbSMingming Cao int jbd2_journal_wipe(journal_t *journal, int write) 1411470decc6SDave Kleikamp { 1412470decc6SDave Kleikamp journal_superblock_t *sb; 1413470decc6SDave Kleikamp int err = 0; 1414470decc6SDave Kleikamp 1415f7f4bccbSMingming Cao J_ASSERT (!(journal->j_flags & JBD2_LOADED)); 1416470decc6SDave Kleikamp 1417470decc6SDave Kleikamp err = load_superblock(journal); 1418470decc6SDave Kleikamp if (err) 1419470decc6SDave Kleikamp return err; 1420470decc6SDave Kleikamp 1421470decc6SDave Kleikamp sb = journal->j_superblock; 1422470decc6SDave Kleikamp 1423470decc6SDave Kleikamp if (!journal->j_tail) 1424470decc6SDave Kleikamp goto no_recovery; 1425470decc6SDave Kleikamp 1426470decc6SDave Kleikamp printk (KERN_WARNING "JBD: %s recovery information on journal\n", 1427470decc6SDave Kleikamp write ? "Clearing" : "Ignoring"); 1428470decc6SDave Kleikamp 1429f7f4bccbSMingming Cao err = jbd2_journal_skip_recovery(journal); 1430470decc6SDave Kleikamp if (write) 1431f7f4bccbSMingming Cao jbd2_journal_update_superblock(journal, 1); 1432470decc6SDave Kleikamp 1433470decc6SDave Kleikamp no_recovery: 1434470decc6SDave Kleikamp return err; 1435470decc6SDave Kleikamp } 1436470decc6SDave Kleikamp 1437470decc6SDave Kleikamp /* 1438470decc6SDave Kleikamp * journal_dev_name: format a character string to describe on what 1439470decc6SDave Kleikamp * device this journal is present. 1440470decc6SDave Kleikamp */ 1441470decc6SDave Kleikamp 1442470decc6SDave Kleikamp static const char *journal_dev_name(journal_t *journal, char *buffer) 1443470decc6SDave Kleikamp { 1444470decc6SDave Kleikamp struct block_device *bdev; 1445470decc6SDave Kleikamp 1446470decc6SDave Kleikamp if (journal->j_inode) 1447470decc6SDave Kleikamp bdev = journal->j_inode->i_sb->s_bdev; 1448470decc6SDave Kleikamp else 1449470decc6SDave Kleikamp bdev = journal->j_dev; 1450470decc6SDave Kleikamp 1451470decc6SDave Kleikamp return bdevname(bdev, buffer); 1452470decc6SDave Kleikamp } 1453470decc6SDave Kleikamp 1454470decc6SDave Kleikamp /* 1455470decc6SDave Kleikamp * Journal abort has very specific semantics, which we describe 1456470decc6SDave Kleikamp * for journal abort. 1457470decc6SDave Kleikamp * 1458470decc6SDave Kleikamp * Two internal function, which provide abort to te jbd layer 1459470decc6SDave Kleikamp * itself are here. 1460470decc6SDave Kleikamp */ 1461470decc6SDave Kleikamp 1462470decc6SDave Kleikamp /* 1463470decc6SDave Kleikamp * Quick version for internal journal use (doesn't lock the journal). 1464470decc6SDave Kleikamp * Aborts hard --- we mark the abort as occurred, but do _nothing_ else, 1465470decc6SDave Kleikamp * and don't attempt to make any other journal updates. 1466470decc6SDave Kleikamp */ 1467f7f4bccbSMingming Cao void __jbd2_journal_abort_hard(journal_t *journal) 1468470decc6SDave Kleikamp { 1469470decc6SDave Kleikamp transaction_t *transaction; 1470470decc6SDave Kleikamp char b[BDEVNAME_SIZE]; 1471470decc6SDave Kleikamp 1472f7f4bccbSMingming Cao if (journal->j_flags & JBD2_ABORT) 1473470decc6SDave Kleikamp return; 1474470decc6SDave Kleikamp 1475470decc6SDave Kleikamp printk(KERN_ERR "Aborting journal on device %s.\n", 1476470decc6SDave Kleikamp journal_dev_name(journal, b)); 1477470decc6SDave Kleikamp 1478470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 1479f7f4bccbSMingming Cao journal->j_flags |= JBD2_ABORT; 1480470decc6SDave Kleikamp transaction = journal->j_running_transaction; 1481470decc6SDave Kleikamp if (transaction) 1482f7f4bccbSMingming Cao __jbd2_log_start_commit(journal, transaction->t_tid); 1483470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1484470decc6SDave Kleikamp } 1485470decc6SDave Kleikamp 1486470decc6SDave Kleikamp /* Soft abort: record the abort error status in the journal superblock, 1487470decc6SDave Kleikamp * but don't do any other IO. */ 1488470decc6SDave Kleikamp static void __journal_abort_soft (journal_t *journal, int errno) 1489470decc6SDave Kleikamp { 1490f7f4bccbSMingming Cao if (journal->j_flags & JBD2_ABORT) 1491470decc6SDave Kleikamp return; 1492470decc6SDave Kleikamp 1493470decc6SDave Kleikamp if (!journal->j_errno) 1494470decc6SDave Kleikamp journal->j_errno = errno; 1495470decc6SDave Kleikamp 1496f7f4bccbSMingming Cao __jbd2_journal_abort_hard(journal); 1497470decc6SDave Kleikamp 1498470decc6SDave Kleikamp if (errno) 1499f7f4bccbSMingming Cao jbd2_journal_update_superblock(journal, 1); 1500470decc6SDave Kleikamp } 1501470decc6SDave Kleikamp 1502470decc6SDave Kleikamp /** 1503f7f4bccbSMingming Cao * void jbd2_journal_abort () - Shutdown the journal immediately. 1504470decc6SDave Kleikamp * @journal: the journal to shutdown. 1505470decc6SDave Kleikamp * @errno: an error number to record in the journal indicating 1506470decc6SDave Kleikamp * the reason for the shutdown. 1507470decc6SDave Kleikamp * 1508470decc6SDave Kleikamp * Perform a complete, immediate shutdown of the ENTIRE 1509470decc6SDave Kleikamp * journal (not of a single transaction). This operation cannot be 1510470decc6SDave Kleikamp * undone without closing and reopening the journal. 1511470decc6SDave Kleikamp * 1512f7f4bccbSMingming Cao * The jbd2_journal_abort function is intended to support higher level error 1513470decc6SDave Kleikamp * recovery mechanisms such as the ext2/ext3 remount-readonly error 1514470decc6SDave Kleikamp * mode. 1515470decc6SDave Kleikamp * 1516470decc6SDave Kleikamp * Journal abort has very specific semantics. Any existing dirty, 1517470decc6SDave Kleikamp * unjournaled buffers in the main filesystem will still be written to 1518470decc6SDave Kleikamp * disk by bdflush, but the journaling mechanism will be suspended 1519470decc6SDave Kleikamp * immediately and no further transaction commits will be honoured. 1520470decc6SDave Kleikamp * 1521470decc6SDave Kleikamp * Any dirty, journaled buffers will be written back to disk without 1522470decc6SDave Kleikamp * hitting the journal. Atomicity cannot be guaranteed on an aborted 1523470decc6SDave Kleikamp * filesystem, but we _do_ attempt to leave as much data as possible 1524470decc6SDave Kleikamp * behind for fsck to use for cleanup. 1525470decc6SDave Kleikamp * 1526470decc6SDave Kleikamp * Any attempt to get a new transaction handle on a journal which is in 1527470decc6SDave Kleikamp * ABORT state will just result in an -EROFS error return. A 1528f7f4bccbSMingming Cao * jbd2_journal_stop on an existing handle will return -EIO if we have 1529470decc6SDave Kleikamp * entered abort state during the update. 1530470decc6SDave Kleikamp * 1531470decc6SDave Kleikamp * Recursive transactions are not disturbed by journal abort until the 1532f7f4bccbSMingming Cao * final jbd2_journal_stop, which will receive the -EIO error. 1533470decc6SDave Kleikamp * 1534f7f4bccbSMingming Cao * Finally, the jbd2_journal_abort call allows the caller to supply an errno 1535470decc6SDave Kleikamp * which will be recorded (if possible) in the journal superblock. This 1536470decc6SDave Kleikamp * allows a client to record failure conditions in the middle of a 1537470decc6SDave Kleikamp * transaction without having to complete the transaction to record the 1538470decc6SDave Kleikamp * failure to disk. ext3_error, for example, now uses this 1539470decc6SDave Kleikamp * functionality. 1540470decc6SDave Kleikamp * 1541470decc6SDave Kleikamp * Errors which originate from within the journaling layer will NOT 1542470decc6SDave Kleikamp * supply an errno; a null errno implies that absolutely no further 1543470decc6SDave Kleikamp * writes are done to the journal (unless there are any already in 1544470decc6SDave Kleikamp * progress). 1545470decc6SDave Kleikamp * 1546470decc6SDave Kleikamp */ 1547470decc6SDave Kleikamp 1548f7f4bccbSMingming Cao void jbd2_journal_abort(journal_t *journal, int errno) 1549470decc6SDave Kleikamp { 1550470decc6SDave Kleikamp __journal_abort_soft(journal, errno); 1551470decc6SDave Kleikamp } 1552470decc6SDave Kleikamp 1553470decc6SDave Kleikamp /** 1554f7f4bccbSMingming Cao * int jbd2_journal_errno () - returns the journal's error state. 1555470decc6SDave Kleikamp * @journal: journal to examine. 1556470decc6SDave Kleikamp * 1557f7f4bccbSMingming Cao * This is the errno numbet set with jbd2_journal_abort(), the last 1558470decc6SDave Kleikamp * time the journal was mounted - if the journal was stopped 1559470decc6SDave Kleikamp * without calling abort this will be 0. 1560470decc6SDave Kleikamp * 1561470decc6SDave Kleikamp * If the journal has been aborted on this mount time -EROFS will 1562470decc6SDave Kleikamp * be returned. 1563470decc6SDave Kleikamp */ 1564f7f4bccbSMingming Cao int jbd2_journal_errno(journal_t *journal) 1565470decc6SDave Kleikamp { 1566470decc6SDave Kleikamp int err; 1567470decc6SDave Kleikamp 1568470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 1569f7f4bccbSMingming Cao if (journal->j_flags & JBD2_ABORT) 1570470decc6SDave Kleikamp err = -EROFS; 1571470decc6SDave Kleikamp else 1572470decc6SDave Kleikamp err = journal->j_errno; 1573470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1574470decc6SDave Kleikamp return err; 1575470decc6SDave Kleikamp } 1576470decc6SDave Kleikamp 1577470decc6SDave Kleikamp /** 1578f7f4bccbSMingming Cao * int jbd2_journal_clear_err () - clears the journal's error state 1579470decc6SDave Kleikamp * @journal: journal to act on. 1580470decc6SDave Kleikamp * 1581470decc6SDave Kleikamp * An error must be cleared or Acked to take a FS out of readonly 1582470decc6SDave Kleikamp * mode. 1583470decc6SDave Kleikamp */ 1584f7f4bccbSMingming Cao int jbd2_journal_clear_err(journal_t *journal) 1585470decc6SDave Kleikamp { 1586470decc6SDave Kleikamp int err = 0; 1587470decc6SDave Kleikamp 1588470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 1589f7f4bccbSMingming Cao if (journal->j_flags & JBD2_ABORT) 1590470decc6SDave Kleikamp err = -EROFS; 1591470decc6SDave Kleikamp else 1592470decc6SDave Kleikamp journal->j_errno = 0; 1593470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1594470decc6SDave Kleikamp return err; 1595470decc6SDave Kleikamp } 1596470decc6SDave Kleikamp 1597470decc6SDave Kleikamp /** 1598f7f4bccbSMingming Cao * void jbd2_journal_ack_err() - Ack journal err. 1599470decc6SDave Kleikamp * @journal: journal to act on. 1600470decc6SDave Kleikamp * 1601470decc6SDave Kleikamp * An error must be cleared or Acked to take a FS out of readonly 1602470decc6SDave Kleikamp * mode. 1603470decc6SDave Kleikamp */ 1604f7f4bccbSMingming Cao void jbd2_journal_ack_err(journal_t *journal) 1605470decc6SDave Kleikamp { 1606470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 1607470decc6SDave Kleikamp if (journal->j_errno) 1608f7f4bccbSMingming Cao journal->j_flags |= JBD2_ACK_ERR; 1609470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1610470decc6SDave Kleikamp } 1611470decc6SDave Kleikamp 1612f7f4bccbSMingming Cao int jbd2_journal_blocks_per_page(struct inode *inode) 1613470decc6SDave Kleikamp { 1614470decc6SDave Kleikamp return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); 1615470decc6SDave Kleikamp } 1616470decc6SDave Kleikamp 1617470decc6SDave Kleikamp /* 1618b517bea1SZach Brown * helper functions to deal with 32 or 64bit block numbers. 1619b517bea1SZach Brown */ 1620b517bea1SZach Brown size_t journal_tag_bytes(journal_t *journal) 1621b517bea1SZach Brown { 1622b517bea1SZach Brown if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT)) 1623b517bea1SZach Brown return JBD_TAG_SIZE64; 1624b517bea1SZach Brown else 1625b517bea1SZach Brown return JBD_TAG_SIZE32; 1626b517bea1SZach Brown } 1627b517bea1SZach Brown 1628b517bea1SZach Brown /* 1629470decc6SDave Kleikamp * Simple support for retrying memory allocations. Introduced to help to 1630470decc6SDave Kleikamp * debug different VM deadlock avoidance strategies. 1631470decc6SDave Kleikamp */ 1632f7f4bccbSMingming Cao void * __jbd2_kmalloc (const char *where, size_t size, gfp_t flags, int retry) 1633470decc6SDave Kleikamp { 1634470decc6SDave Kleikamp return kmalloc(size, flags | (retry ? __GFP_NOFAIL : 0)); 1635470decc6SDave Kleikamp } 1636470decc6SDave Kleikamp 1637470decc6SDave Kleikamp /* 1638470decc6SDave Kleikamp * jbd slab management: create 1k, 2k, 4k, 8k slabs as needed 1639470decc6SDave Kleikamp * and allocate frozen and commit buffers from these slabs. 1640470decc6SDave Kleikamp * 1641470decc6SDave Kleikamp * Reason for doing this is to avoid, SLAB_DEBUG - since it could 1642470decc6SDave Kleikamp * cause bh to cross page boundary. 1643470decc6SDave Kleikamp */ 1644470decc6SDave Kleikamp 1645470decc6SDave Kleikamp #define JBD_MAX_SLABS 5 1646470decc6SDave Kleikamp #define JBD_SLAB_INDEX(size) (size >> 11) 1647470decc6SDave Kleikamp 1648e18b890bSChristoph Lameter static struct kmem_cache *jbd_slab[JBD_MAX_SLABS]; 1649470decc6SDave Kleikamp static const char *jbd_slab_names[JBD_MAX_SLABS] = { 1650a920e941SJohann Lombardi "jbd2_1k", "jbd2_2k", "jbd2_4k", NULL, "jbd2_8k" 1651470decc6SDave Kleikamp }; 1652470decc6SDave Kleikamp 1653f7f4bccbSMingming Cao static void jbd2_journal_destroy_jbd_slabs(void) 1654470decc6SDave Kleikamp { 1655470decc6SDave Kleikamp int i; 1656470decc6SDave Kleikamp 1657470decc6SDave Kleikamp for (i = 0; i < JBD_MAX_SLABS; i++) { 1658470decc6SDave Kleikamp if (jbd_slab[i]) 1659470decc6SDave Kleikamp kmem_cache_destroy(jbd_slab[i]); 1660470decc6SDave Kleikamp jbd_slab[i] = NULL; 1661470decc6SDave Kleikamp } 1662470decc6SDave Kleikamp } 1663470decc6SDave Kleikamp 1664f7f4bccbSMingming Cao static int jbd2_journal_create_jbd_slab(size_t slab_size) 1665470decc6SDave Kleikamp { 1666470decc6SDave Kleikamp int i = JBD_SLAB_INDEX(slab_size); 1667470decc6SDave Kleikamp 1668470decc6SDave Kleikamp BUG_ON(i >= JBD_MAX_SLABS); 1669470decc6SDave Kleikamp 1670470decc6SDave Kleikamp /* 1671470decc6SDave Kleikamp * Check if we already have a slab created for this size 1672470decc6SDave Kleikamp */ 1673470decc6SDave Kleikamp if (jbd_slab[i]) 1674470decc6SDave Kleikamp return 0; 1675470decc6SDave Kleikamp 1676470decc6SDave Kleikamp /* 1677470decc6SDave Kleikamp * Create a slab and force alignment to be same as slabsize - 1678470decc6SDave Kleikamp * this will make sure that allocations won't cross the page 1679470decc6SDave Kleikamp * boundary. 1680470decc6SDave Kleikamp */ 1681470decc6SDave Kleikamp jbd_slab[i] = kmem_cache_create(jbd_slab_names[i], 1682470decc6SDave Kleikamp slab_size, slab_size, 0, NULL, NULL); 1683470decc6SDave Kleikamp if (!jbd_slab[i]) { 1684470decc6SDave Kleikamp printk(KERN_EMERG "JBD: no memory for jbd_slab cache\n"); 1685470decc6SDave Kleikamp return -ENOMEM; 1686470decc6SDave Kleikamp } 1687470decc6SDave Kleikamp return 0; 1688470decc6SDave Kleikamp } 1689470decc6SDave Kleikamp 1690f7f4bccbSMingming Cao void * jbd2_slab_alloc(size_t size, gfp_t flags) 1691470decc6SDave Kleikamp { 1692470decc6SDave Kleikamp int idx; 1693470decc6SDave Kleikamp 1694470decc6SDave Kleikamp idx = JBD_SLAB_INDEX(size); 1695470decc6SDave Kleikamp BUG_ON(jbd_slab[idx] == NULL); 1696470decc6SDave Kleikamp return kmem_cache_alloc(jbd_slab[idx], flags | __GFP_NOFAIL); 1697470decc6SDave Kleikamp } 1698470decc6SDave Kleikamp 1699f7f4bccbSMingming Cao void jbd2_slab_free(void *ptr, size_t size) 1700470decc6SDave Kleikamp { 1701470decc6SDave Kleikamp int idx; 1702470decc6SDave Kleikamp 1703470decc6SDave Kleikamp idx = JBD_SLAB_INDEX(size); 1704470decc6SDave Kleikamp BUG_ON(jbd_slab[idx] == NULL); 1705470decc6SDave Kleikamp kmem_cache_free(jbd_slab[idx], ptr); 1706470decc6SDave Kleikamp } 1707470decc6SDave Kleikamp 1708470decc6SDave Kleikamp /* 1709470decc6SDave Kleikamp * Journal_head storage management 1710470decc6SDave Kleikamp */ 1711e18b890bSChristoph Lameter static struct kmem_cache *jbd2_journal_head_cache; 1712*e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 1713470decc6SDave Kleikamp static atomic_t nr_journal_heads = ATOMIC_INIT(0); 1714470decc6SDave Kleikamp #endif 1715470decc6SDave Kleikamp 1716f7f4bccbSMingming Cao static int journal_init_jbd2_journal_head_cache(void) 1717470decc6SDave Kleikamp { 1718470decc6SDave Kleikamp int retval; 1719470decc6SDave Kleikamp 1720f7f4bccbSMingming Cao J_ASSERT(jbd2_journal_head_cache == 0); 1721a920e941SJohann Lombardi jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head", 1722470decc6SDave Kleikamp sizeof(struct journal_head), 1723470decc6SDave Kleikamp 0, /* offset */ 1724470decc6SDave Kleikamp 0, /* flags */ 1725470decc6SDave Kleikamp NULL, /* ctor */ 1726470decc6SDave Kleikamp NULL); /* dtor */ 1727470decc6SDave Kleikamp retval = 0; 1728f7f4bccbSMingming Cao if (jbd2_journal_head_cache == 0) { 1729470decc6SDave Kleikamp retval = -ENOMEM; 1730470decc6SDave Kleikamp printk(KERN_EMERG "JBD: no memory for journal_head cache\n"); 1731470decc6SDave Kleikamp } 1732470decc6SDave Kleikamp return retval; 1733470decc6SDave Kleikamp } 1734470decc6SDave Kleikamp 1735f7f4bccbSMingming Cao static void jbd2_journal_destroy_jbd2_journal_head_cache(void) 1736470decc6SDave Kleikamp { 1737f7f4bccbSMingming Cao J_ASSERT(jbd2_journal_head_cache != NULL); 1738f7f4bccbSMingming Cao kmem_cache_destroy(jbd2_journal_head_cache); 1739f7f4bccbSMingming Cao jbd2_journal_head_cache = NULL; 1740470decc6SDave Kleikamp } 1741470decc6SDave Kleikamp 1742470decc6SDave Kleikamp /* 1743470decc6SDave Kleikamp * journal_head splicing and dicing 1744470decc6SDave Kleikamp */ 1745470decc6SDave Kleikamp static struct journal_head *journal_alloc_journal_head(void) 1746470decc6SDave Kleikamp { 1747470decc6SDave Kleikamp struct journal_head *ret; 1748470decc6SDave Kleikamp static unsigned long last_warning; 1749470decc6SDave Kleikamp 1750*e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 1751470decc6SDave Kleikamp atomic_inc(&nr_journal_heads); 1752470decc6SDave Kleikamp #endif 1753f7f4bccbSMingming Cao ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS); 1754470decc6SDave Kleikamp if (ret == 0) { 1755470decc6SDave Kleikamp jbd_debug(1, "out of memory for journal_head\n"); 1756470decc6SDave Kleikamp if (time_after(jiffies, last_warning + 5*HZ)) { 1757470decc6SDave Kleikamp printk(KERN_NOTICE "ENOMEM in %s, retrying.\n", 1758470decc6SDave Kleikamp __FUNCTION__); 1759470decc6SDave Kleikamp last_warning = jiffies; 1760470decc6SDave Kleikamp } 1761470decc6SDave Kleikamp while (ret == 0) { 1762470decc6SDave Kleikamp yield(); 1763f7f4bccbSMingming Cao ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS); 1764470decc6SDave Kleikamp } 1765470decc6SDave Kleikamp } 1766470decc6SDave Kleikamp return ret; 1767470decc6SDave Kleikamp } 1768470decc6SDave Kleikamp 1769470decc6SDave Kleikamp static void journal_free_journal_head(struct journal_head *jh) 1770470decc6SDave Kleikamp { 1771*e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 1772470decc6SDave Kleikamp atomic_dec(&nr_journal_heads); 1773470decc6SDave Kleikamp memset(jh, JBD_POISON_FREE, sizeof(*jh)); 1774470decc6SDave Kleikamp #endif 1775f7f4bccbSMingming Cao kmem_cache_free(jbd2_journal_head_cache, jh); 1776470decc6SDave Kleikamp } 1777470decc6SDave Kleikamp 1778470decc6SDave Kleikamp /* 1779470decc6SDave Kleikamp * A journal_head is attached to a buffer_head whenever JBD has an 1780470decc6SDave Kleikamp * interest in the buffer. 1781470decc6SDave Kleikamp * 1782470decc6SDave Kleikamp * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit 1783470decc6SDave Kleikamp * is set. This bit is tested in core kernel code where we need to take 1784470decc6SDave Kleikamp * JBD-specific actions. Testing the zeroness of ->b_private is not reliable 1785470decc6SDave Kleikamp * there. 1786470decc6SDave Kleikamp * 1787470decc6SDave Kleikamp * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one. 1788470decc6SDave Kleikamp * 1789470decc6SDave Kleikamp * When a buffer has its BH_JBD bit set it is immune from being released by 1790470decc6SDave Kleikamp * core kernel code, mainly via ->b_count. 1791470decc6SDave Kleikamp * 1792470decc6SDave Kleikamp * A journal_head may be detached from its buffer_head when the journal_head's 1793470decc6SDave Kleikamp * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL. 1794f7f4bccbSMingming Cao * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the 1795470decc6SDave Kleikamp * journal_head can be dropped if needed. 1796470decc6SDave Kleikamp * 1797470decc6SDave Kleikamp * Various places in the kernel want to attach a journal_head to a buffer_head 1798470decc6SDave Kleikamp * _before_ attaching the journal_head to a transaction. To protect the 1799f7f4bccbSMingming Cao * journal_head in this situation, jbd2_journal_add_journal_head elevates the 1800470decc6SDave Kleikamp * journal_head's b_jcount refcount by one. The caller must call 1801f7f4bccbSMingming Cao * jbd2_journal_put_journal_head() to undo this. 1802470decc6SDave Kleikamp * 1803470decc6SDave Kleikamp * So the typical usage would be: 1804470decc6SDave Kleikamp * 1805470decc6SDave Kleikamp * (Attach a journal_head if needed. Increments b_jcount) 1806f7f4bccbSMingming Cao * struct journal_head *jh = jbd2_journal_add_journal_head(bh); 1807470decc6SDave Kleikamp * ... 1808470decc6SDave Kleikamp * jh->b_transaction = xxx; 1809f7f4bccbSMingming Cao * jbd2_journal_put_journal_head(jh); 1810470decc6SDave Kleikamp * 1811470decc6SDave Kleikamp * Now, the journal_head's b_jcount is zero, but it is safe from being released 1812470decc6SDave Kleikamp * because it has a non-zero b_transaction. 1813470decc6SDave Kleikamp */ 1814470decc6SDave Kleikamp 1815470decc6SDave Kleikamp /* 1816470decc6SDave Kleikamp * Give a buffer_head a journal_head. 1817470decc6SDave Kleikamp * 1818470decc6SDave Kleikamp * Doesn't need the journal lock. 1819470decc6SDave Kleikamp * May sleep. 1820470decc6SDave Kleikamp */ 1821f7f4bccbSMingming Cao struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh) 1822470decc6SDave Kleikamp { 1823470decc6SDave Kleikamp struct journal_head *jh; 1824470decc6SDave Kleikamp struct journal_head *new_jh = NULL; 1825470decc6SDave Kleikamp 1826470decc6SDave Kleikamp repeat: 1827470decc6SDave Kleikamp if (!buffer_jbd(bh)) { 1828470decc6SDave Kleikamp new_jh = journal_alloc_journal_head(); 1829470decc6SDave Kleikamp memset(new_jh, 0, sizeof(*new_jh)); 1830470decc6SDave Kleikamp } 1831470decc6SDave Kleikamp 1832470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 1833470decc6SDave Kleikamp if (buffer_jbd(bh)) { 1834470decc6SDave Kleikamp jh = bh2jh(bh); 1835470decc6SDave Kleikamp } else { 1836470decc6SDave Kleikamp J_ASSERT_BH(bh, 1837470decc6SDave Kleikamp (atomic_read(&bh->b_count) > 0) || 1838470decc6SDave Kleikamp (bh->b_page && bh->b_page->mapping)); 1839470decc6SDave Kleikamp 1840470decc6SDave Kleikamp if (!new_jh) { 1841470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 1842470decc6SDave Kleikamp goto repeat; 1843470decc6SDave Kleikamp } 1844470decc6SDave Kleikamp 1845470decc6SDave Kleikamp jh = new_jh; 1846470decc6SDave Kleikamp new_jh = NULL; /* We consumed it */ 1847470decc6SDave Kleikamp set_buffer_jbd(bh); 1848470decc6SDave Kleikamp bh->b_private = jh; 1849470decc6SDave Kleikamp jh->b_bh = bh; 1850470decc6SDave Kleikamp get_bh(bh); 1851470decc6SDave Kleikamp BUFFER_TRACE(bh, "added journal_head"); 1852470decc6SDave Kleikamp } 1853470decc6SDave Kleikamp jh->b_jcount++; 1854470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 1855470decc6SDave Kleikamp if (new_jh) 1856470decc6SDave Kleikamp journal_free_journal_head(new_jh); 1857470decc6SDave Kleikamp return bh->b_private; 1858470decc6SDave Kleikamp } 1859470decc6SDave Kleikamp 1860470decc6SDave Kleikamp /* 1861470decc6SDave Kleikamp * Grab a ref against this buffer_head's journal_head. If it ended up not 1862470decc6SDave Kleikamp * having a journal_head, return NULL 1863470decc6SDave Kleikamp */ 1864f7f4bccbSMingming Cao struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh) 1865470decc6SDave Kleikamp { 1866470decc6SDave Kleikamp struct journal_head *jh = NULL; 1867470decc6SDave Kleikamp 1868470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 1869470decc6SDave Kleikamp if (buffer_jbd(bh)) { 1870470decc6SDave Kleikamp jh = bh2jh(bh); 1871470decc6SDave Kleikamp jh->b_jcount++; 1872470decc6SDave Kleikamp } 1873470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 1874470decc6SDave Kleikamp return jh; 1875470decc6SDave Kleikamp } 1876470decc6SDave Kleikamp 1877470decc6SDave Kleikamp static void __journal_remove_journal_head(struct buffer_head *bh) 1878470decc6SDave Kleikamp { 1879470decc6SDave Kleikamp struct journal_head *jh = bh2jh(bh); 1880470decc6SDave Kleikamp 1881470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_jcount >= 0); 1882470decc6SDave Kleikamp 1883470decc6SDave Kleikamp get_bh(bh); 1884470decc6SDave Kleikamp if (jh->b_jcount == 0) { 1885470decc6SDave Kleikamp if (jh->b_transaction == NULL && 1886470decc6SDave Kleikamp jh->b_next_transaction == NULL && 1887470decc6SDave Kleikamp jh->b_cp_transaction == NULL) { 1888470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_jlist == BJ_None); 1889470decc6SDave Kleikamp J_ASSERT_BH(bh, buffer_jbd(bh)); 1890470decc6SDave Kleikamp J_ASSERT_BH(bh, jh2bh(jh) == bh); 1891470decc6SDave Kleikamp BUFFER_TRACE(bh, "remove journal_head"); 1892470decc6SDave Kleikamp if (jh->b_frozen_data) { 1893470decc6SDave Kleikamp printk(KERN_WARNING "%s: freeing " 1894470decc6SDave Kleikamp "b_frozen_data\n", 1895470decc6SDave Kleikamp __FUNCTION__); 1896f7f4bccbSMingming Cao jbd2_slab_free(jh->b_frozen_data, bh->b_size); 1897470decc6SDave Kleikamp } 1898470decc6SDave Kleikamp if (jh->b_committed_data) { 1899470decc6SDave Kleikamp printk(KERN_WARNING "%s: freeing " 1900470decc6SDave Kleikamp "b_committed_data\n", 1901470decc6SDave Kleikamp __FUNCTION__); 1902f7f4bccbSMingming Cao jbd2_slab_free(jh->b_committed_data, bh->b_size); 1903470decc6SDave Kleikamp } 1904470decc6SDave Kleikamp bh->b_private = NULL; 1905470decc6SDave Kleikamp jh->b_bh = NULL; /* debug, really */ 1906470decc6SDave Kleikamp clear_buffer_jbd(bh); 1907470decc6SDave Kleikamp __brelse(bh); 1908470decc6SDave Kleikamp journal_free_journal_head(jh); 1909470decc6SDave Kleikamp } else { 1910470decc6SDave Kleikamp BUFFER_TRACE(bh, "journal_head was locked"); 1911470decc6SDave Kleikamp } 1912470decc6SDave Kleikamp } 1913470decc6SDave Kleikamp } 1914470decc6SDave Kleikamp 1915470decc6SDave Kleikamp /* 1916f7f4bccbSMingming Cao * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction 1917470decc6SDave Kleikamp * and has a zero b_jcount then remove and release its journal_head. If we did 1918470decc6SDave Kleikamp * see that the buffer is not used by any transaction we also "logically" 1919470decc6SDave Kleikamp * decrement ->b_count. 1920470decc6SDave Kleikamp * 1921470decc6SDave Kleikamp * We in fact take an additional increment on ->b_count as a convenience, 1922470decc6SDave Kleikamp * because the caller usually wants to do additional things with the bh 1923470decc6SDave Kleikamp * after calling here. 1924f7f4bccbSMingming Cao * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some 1925470decc6SDave Kleikamp * time. Once the caller has run __brelse(), the buffer is eligible for 1926470decc6SDave Kleikamp * reaping by try_to_free_buffers(). 1927470decc6SDave Kleikamp */ 1928f7f4bccbSMingming Cao void jbd2_journal_remove_journal_head(struct buffer_head *bh) 1929470decc6SDave Kleikamp { 1930470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 1931470decc6SDave Kleikamp __journal_remove_journal_head(bh); 1932470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 1933470decc6SDave Kleikamp } 1934470decc6SDave Kleikamp 1935470decc6SDave Kleikamp /* 1936470decc6SDave Kleikamp * Drop a reference on the passed journal_head. If it fell to zero then try to 1937470decc6SDave Kleikamp * release the journal_head from the buffer_head. 1938470decc6SDave Kleikamp */ 1939f7f4bccbSMingming Cao void jbd2_journal_put_journal_head(struct journal_head *jh) 1940470decc6SDave Kleikamp { 1941470decc6SDave Kleikamp struct buffer_head *bh = jh2bh(jh); 1942470decc6SDave Kleikamp 1943470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 1944470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_jcount > 0); 1945470decc6SDave Kleikamp --jh->b_jcount; 1946470decc6SDave Kleikamp if (!jh->b_jcount && !jh->b_transaction) { 1947470decc6SDave Kleikamp __journal_remove_journal_head(bh); 1948470decc6SDave Kleikamp __brelse(bh); 1949470decc6SDave Kleikamp } 1950470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 1951470decc6SDave Kleikamp } 1952470decc6SDave Kleikamp 1953470decc6SDave Kleikamp /* 1954470decc6SDave Kleikamp * /proc tunables 1955470decc6SDave Kleikamp */ 1956*e23291b9SJose R. Santos #if defined(CONFIG_JBD2_DEBUG) 1957f7f4bccbSMingming Cao int jbd2_journal_enable_debug; 1958f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_enable_debug); 1959470decc6SDave Kleikamp #endif 1960470decc6SDave Kleikamp 1961*e23291b9SJose R. Santos #if defined(CONFIG_JBD2_DEBUG) && defined(CONFIG_PROC_FS) 1962470decc6SDave Kleikamp 1963470decc6SDave Kleikamp static struct proc_dir_entry *proc_jbd_debug; 1964470decc6SDave Kleikamp 1965470decc6SDave Kleikamp static int read_jbd_debug(char *page, char **start, off_t off, 1966470decc6SDave Kleikamp int count, int *eof, void *data) 1967470decc6SDave Kleikamp { 1968470decc6SDave Kleikamp int ret; 1969470decc6SDave Kleikamp 1970f7f4bccbSMingming Cao ret = sprintf(page + off, "%d\n", jbd2_journal_enable_debug); 1971470decc6SDave Kleikamp *eof = 1; 1972470decc6SDave Kleikamp return ret; 1973470decc6SDave Kleikamp } 1974470decc6SDave Kleikamp 1975470decc6SDave Kleikamp static int write_jbd_debug(struct file *file, const char __user *buffer, 1976470decc6SDave Kleikamp unsigned long count, void *data) 1977470decc6SDave Kleikamp { 1978470decc6SDave Kleikamp char buf[32]; 1979470decc6SDave Kleikamp 1980470decc6SDave Kleikamp if (count > ARRAY_SIZE(buf) - 1) 1981470decc6SDave Kleikamp count = ARRAY_SIZE(buf) - 1; 1982470decc6SDave Kleikamp if (copy_from_user(buf, buffer, count)) 1983470decc6SDave Kleikamp return -EFAULT; 1984470decc6SDave Kleikamp buf[ARRAY_SIZE(buf) - 1] = '\0'; 1985f7f4bccbSMingming Cao jbd2_journal_enable_debug = simple_strtoul(buf, NULL, 10); 1986470decc6SDave Kleikamp return count; 1987470decc6SDave Kleikamp } 1988470decc6SDave Kleikamp 1989f7f4bccbSMingming Cao #define JBD_PROC_NAME "sys/fs/jbd2-debug" 1990470decc6SDave Kleikamp 1991470decc6SDave Kleikamp static void __init create_jbd_proc_entry(void) 1992470decc6SDave Kleikamp { 1993470decc6SDave Kleikamp proc_jbd_debug = create_proc_entry(JBD_PROC_NAME, 0644, NULL); 1994470decc6SDave Kleikamp if (proc_jbd_debug) { 1995470decc6SDave Kleikamp /* Why is this so hard? */ 1996470decc6SDave Kleikamp proc_jbd_debug->read_proc = read_jbd_debug; 1997470decc6SDave Kleikamp proc_jbd_debug->write_proc = write_jbd_debug; 1998470decc6SDave Kleikamp } 1999470decc6SDave Kleikamp } 2000470decc6SDave Kleikamp 2001f7f4bccbSMingming Cao static void __exit jbd2_remove_jbd_proc_entry(void) 2002470decc6SDave Kleikamp { 2003470decc6SDave Kleikamp if (proc_jbd_debug) 2004470decc6SDave Kleikamp remove_proc_entry(JBD_PROC_NAME, NULL); 2005470decc6SDave Kleikamp } 2006470decc6SDave Kleikamp 2007470decc6SDave Kleikamp #else 2008470decc6SDave Kleikamp 2009470decc6SDave Kleikamp #define create_jbd_proc_entry() do {} while (0) 2010f7f4bccbSMingming Cao #define jbd2_remove_jbd_proc_entry() do {} while (0) 2011470decc6SDave Kleikamp 2012470decc6SDave Kleikamp #endif 2013470decc6SDave Kleikamp 2014e18b890bSChristoph Lameter struct kmem_cache *jbd2_handle_cache; 2015470decc6SDave Kleikamp 2016470decc6SDave Kleikamp static int __init journal_init_handle_cache(void) 2017470decc6SDave Kleikamp { 2018a920e941SJohann Lombardi jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle", 2019470decc6SDave Kleikamp sizeof(handle_t), 2020470decc6SDave Kleikamp 0, /* offset */ 2021470decc6SDave Kleikamp 0, /* flags */ 2022470decc6SDave Kleikamp NULL, /* ctor */ 2023470decc6SDave Kleikamp NULL); /* dtor */ 2024f7f4bccbSMingming Cao if (jbd2_handle_cache == NULL) { 2025470decc6SDave Kleikamp printk(KERN_EMERG "JBD: failed to create handle cache\n"); 2026470decc6SDave Kleikamp return -ENOMEM; 2027470decc6SDave Kleikamp } 2028470decc6SDave Kleikamp return 0; 2029470decc6SDave Kleikamp } 2030470decc6SDave Kleikamp 2031f7f4bccbSMingming Cao static void jbd2_journal_destroy_handle_cache(void) 2032470decc6SDave Kleikamp { 2033f7f4bccbSMingming Cao if (jbd2_handle_cache) 2034f7f4bccbSMingming Cao kmem_cache_destroy(jbd2_handle_cache); 2035470decc6SDave Kleikamp } 2036470decc6SDave Kleikamp 2037470decc6SDave Kleikamp /* 2038470decc6SDave Kleikamp * Module startup and shutdown 2039470decc6SDave Kleikamp */ 2040470decc6SDave Kleikamp 2041470decc6SDave Kleikamp static int __init journal_init_caches(void) 2042470decc6SDave Kleikamp { 2043470decc6SDave Kleikamp int ret; 2044470decc6SDave Kleikamp 2045f7f4bccbSMingming Cao ret = jbd2_journal_init_revoke_caches(); 2046470decc6SDave Kleikamp if (ret == 0) 2047f7f4bccbSMingming Cao ret = journal_init_jbd2_journal_head_cache(); 2048470decc6SDave Kleikamp if (ret == 0) 2049470decc6SDave Kleikamp ret = journal_init_handle_cache(); 2050470decc6SDave Kleikamp return ret; 2051470decc6SDave Kleikamp } 2052470decc6SDave Kleikamp 2053f7f4bccbSMingming Cao static void jbd2_journal_destroy_caches(void) 2054470decc6SDave Kleikamp { 2055f7f4bccbSMingming Cao jbd2_journal_destroy_revoke_caches(); 2056f7f4bccbSMingming Cao jbd2_journal_destroy_jbd2_journal_head_cache(); 2057f7f4bccbSMingming Cao jbd2_journal_destroy_handle_cache(); 2058f7f4bccbSMingming Cao jbd2_journal_destroy_jbd_slabs(); 2059470decc6SDave Kleikamp } 2060470decc6SDave Kleikamp 2061470decc6SDave Kleikamp static int __init journal_init(void) 2062470decc6SDave Kleikamp { 2063470decc6SDave Kleikamp int ret; 2064470decc6SDave Kleikamp 2065470decc6SDave Kleikamp BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024); 2066470decc6SDave Kleikamp 2067470decc6SDave Kleikamp ret = journal_init_caches(); 2068470decc6SDave Kleikamp if (ret != 0) 2069f7f4bccbSMingming Cao jbd2_journal_destroy_caches(); 2070470decc6SDave Kleikamp create_jbd_proc_entry(); 2071470decc6SDave Kleikamp return ret; 2072470decc6SDave Kleikamp } 2073470decc6SDave Kleikamp 2074470decc6SDave Kleikamp static void __exit journal_exit(void) 2075470decc6SDave Kleikamp { 2076*e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 2077470decc6SDave Kleikamp int n = atomic_read(&nr_journal_heads); 2078470decc6SDave Kleikamp if (n) 2079470decc6SDave Kleikamp printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n); 2080470decc6SDave Kleikamp #endif 2081f7f4bccbSMingming Cao jbd2_remove_jbd_proc_entry(); 2082f7f4bccbSMingming Cao jbd2_journal_destroy_caches(); 2083470decc6SDave Kleikamp } 2084470decc6SDave Kleikamp 2085470decc6SDave Kleikamp MODULE_LICENSE("GPL"); 2086470decc6SDave Kleikamp module_init(journal_init); 2087470decc6SDave Kleikamp module_exit(journal_exit); 2088470decc6SDave Kleikamp 2089