1470decc6SDave Kleikamp /* 2f7f4bccbSMingming Cao * linux/fs/jbd2/journal.c 3470decc6SDave Kleikamp * 4470decc6SDave Kleikamp * Written by Stephen C. Tweedie <sct@redhat.com>, 1998 5470decc6SDave Kleikamp * 6470decc6SDave Kleikamp * Copyright 1998 Red Hat corp --- All Rights Reserved 7470decc6SDave Kleikamp * 8470decc6SDave Kleikamp * This file is part of the Linux kernel and is made available under 9470decc6SDave Kleikamp * the terms of the GNU General Public License, version 2, or at your 10470decc6SDave Kleikamp * option, any later version, incorporated herein by reference. 11470decc6SDave Kleikamp * 12470decc6SDave Kleikamp * Generic filesystem journal-writing code; part of the ext2fs 13470decc6SDave Kleikamp * journaling system. 14470decc6SDave Kleikamp * 15470decc6SDave Kleikamp * This file manages journals: areas of disk reserved for logging 16470decc6SDave Kleikamp * transactional updates. This includes the kernel journaling thread 17470decc6SDave Kleikamp * which is responsible for scheduling updates to the log. 18470decc6SDave Kleikamp * 19470decc6SDave Kleikamp * We do not actually manage the physical storage of the journal in this 20470decc6SDave Kleikamp * file: that is left to a per-journal policy function, which allows us 21470decc6SDave Kleikamp * to store the journal within a filesystem-specified area for ext2 22470decc6SDave Kleikamp * journaling (ext2 can use a reserved inode for storing the log). 23470decc6SDave Kleikamp */ 24470decc6SDave Kleikamp 25470decc6SDave Kleikamp #include <linux/module.h> 26470decc6SDave Kleikamp #include <linux/time.h> 27470decc6SDave Kleikamp #include <linux/fs.h> 28f7f4bccbSMingming Cao #include <linux/jbd2.h> 29470decc6SDave Kleikamp #include <linux/errno.h> 30470decc6SDave Kleikamp #include <linux/slab.h> 31470decc6SDave Kleikamp #include <linux/init.h> 32470decc6SDave Kleikamp #include <linux/mm.h> 337dfb7103SNigel Cunningham #include <linux/freezer.h> 34470decc6SDave Kleikamp #include <linux/pagemap.h> 35470decc6SDave Kleikamp #include <linux/kthread.h> 36470decc6SDave Kleikamp #include <linux/poison.h> 37470decc6SDave Kleikamp #include <linux/proc_fs.h> 380f49d5d0SJose R. Santos #include <linux/debugfs.h> 398e85fb3fSJohann Lombardi #include <linux/seq_file.h> 40470decc6SDave Kleikamp 41470decc6SDave Kleikamp #include <asm/uaccess.h> 42470decc6SDave Kleikamp #include <asm/page.h> 43470decc6SDave Kleikamp 44f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_start); 45f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_restart); 46f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_extend); 47f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_stop); 48f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_lock_updates); 49f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_unlock_updates); 50f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_get_write_access); 51f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_get_create_access); 52f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_get_undo_access); 53f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_dirty_data); 54f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_dirty_metadata); 55f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_release_buffer); 56f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_forget); 57470decc6SDave Kleikamp #if 0 58470decc6SDave Kleikamp EXPORT_SYMBOL(journal_sync_buffer); 59470decc6SDave Kleikamp #endif 60f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_flush); 61f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_revoke); 62470decc6SDave Kleikamp 63f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_init_dev); 64f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_init_inode); 65f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_update_format); 66f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_check_used_features); 67f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_check_available_features); 68f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_set_features); 69f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_create); 70f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_load); 71f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_destroy); 72f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_update_superblock); 73f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_abort); 74f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_errno); 75f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_ack_err); 76f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_clear_err); 77f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_log_wait_commit); 78f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_start_commit); 79f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_force_commit_nested); 80f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_wipe); 81f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_blocks_per_page); 82f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_invalidatepage); 83f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers); 84f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_force_commit); 85470decc6SDave Kleikamp 86470decc6SDave Kleikamp static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *); 87470decc6SDave Kleikamp static void __journal_abort_soft (journal_t *journal, int errno); 88470decc6SDave Kleikamp 89470decc6SDave Kleikamp /* 90470decc6SDave Kleikamp * Helper function used to manage commit timeouts 91470decc6SDave Kleikamp */ 92470decc6SDave Kleikamp 93470decc6SDave Kleikamp static void commit_timeout(unsigned long __data) 94470decc6SDave Kleikamp { 95470decc6SDave Kleikamp struct task_struct * p = (struct task_struct *) __data; 96470decc6SDave Kleikamp 97470decc6SDave Kleikamp wake_up_process(p); 98470decc6SDave Kleikamp } 99470decc6SDave Kleikamp 100470decc6SDave Kleikamp /* 101f7f4bccbSMingming Cao * kjournald2: The main thread function used to manage a logging device 102470decc6SDave Kleikamp * journal. 103470decc6SDave Kleikamp * 104470decc6SDave Kleikamp * This kernel thread is responsible for two things: 105470decc6SDave Kleikamp * 106470decc6SDave Kleikamp * 1) COMMIT: Every so often we need to commit the current state of the 107470decc6SDave Kleikamp * filesystem to disk. The journal thread is responsible for writing 108470decc6SDave Kleikamp * all of the metadata buffers to disk. 109470decc6SDave Kleikamp * 110470decc6SDave Kleikamp * 2) CHECKPOINT: We cannot reuse a used section of the log file until all 111470decc6SDave Kleikamp * of the data in that part of the log has been rewritten elsewhere on 112470decc6SDave Kleikamp * the disk. Flushing these old buffers to reclaim space in the log is 113470decc6SDave Kleikamp * known as checkpointing, and this thread is responsible for that job. 114470decc6SDave Kleikamp */ 115470decc6SDave Kleikamp 116f7f4bccbSMingming Cao static int kjournald2(void *arg) 117470decc6SDave Kleikamp { 118470decc6SDave Kleikamp journal_t *journal = arg; 119470decc6SDave Kleikamp transaction_t *transaction; 120470decc6SDave Kleikamp 121470decc6SDave Kleikamp /* 122470decc6SDave Kleikamp * Set up an interval timer which can be used to trigger a commit wakeup 123470decc6SDave Kleikamp * after the commit interval expires 124470decc6SDave Kleikamp */ 125470decc6SDave Kleikamp setup_timer(&journal->j_commit_timer, commit_timeout, 126470decc6SDave Kleikamp (unsigned long)current); 127470decc6SDave Kleikamp 128470decc6SDave Kleikamp /* Record that the journal thread is running */ 129470decc6SDave Kleikamp journal->j_task = current; 130470decc6SDave Kleikamp wake_up(&journal->j_wait_done_commit); 131470decc6SDave Kleikamp 132f7f4bccbSMingming Cao printk(KERN_INFO "kjournald2 starting. Commit interval %ld seconds\n", 133470decc6SDave Kleikamp journal->j_commit_interval / HZ); 134470decc6SDave Kleikamp 135470decc6SDave Kleikamp /* 136470decc6SDave Kleikamp * And now, wait forever for commit wakeup events. 137470decc6SDave Kleikamp */ 138470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 139470decc6SDave Kleikamp 140470decc6SDave Kleikamp loop: 141f7f4bccbSMingming Cao if (journal->j_flags & JBD2_UNMOUNT) 142470decc6SDave Kleikamp goto end_loop; 143470decc6SDave Kleikamp 144470decc6SDave Kleikamp jbd_debug(1, "commit_sequence=%d, commit_request=%d\n", 145470decc6SDave Kleikamp journal->j_commit_sequence, journal->j_commit_request); 146470decc6SDave Kleikamp 147470decc6SDave Kleikamp if (journal->j_commit_sequence != journal->j_commit_request) { 148470decc6SDave Kleikamp jbd_debug(1, "OK, requests differ\n"); 149470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 150470decc6SDave Kleikamp del_timer_sync(&journal->j_commit_timer); 151f7f4bccbSMingming Cao jbd2_journal_commit_transaction(journal); 152470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 153470decc6SDave Kleikamp goto loop; 154470decc6SDave Kleikamp } 155470decc6SDave Kleikamp 156470decc6SDave Kleikamp wake_up(&journal->j_wait_done_commit); 157470decc6SDave Kleikamp if (freezing(current)) { 158470decc6SDave Kleikamp /* 159470decc6SDave Kleikamp * The simpler the better. Flushing journal isn't a 160470decc6SDave Kleikamp * good idea, because that depends on threads that may 161470decc6SDave Kleikamp * be already stopped. 162470decc6SDave Kleikamp */ 163f7f4bccbSMingming Cao jbd_debug(1, "Now suspending kjournald2\n"); 164470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 165470decc6SDave Kleikamp refrigerator(); 166470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 167470decc6SDave Kleikamp } else { 168470decc6SDave Kleikamp /* 169470decc6SDave Kleikamp * We assume on resume that commits are already there, 170470decc6SDave Kleikamp * so we don't sleep 171470decc6SDave Kleikamp */ 172470decc6SDave Kleikamp DEFINE_WAIT(wait); 173470decc6SDave Kleikamp int should_sleep = 1; 174470decc6SDave Kleikamp 175470decc6SDave Kleikamp prepare_to_wait(&journal->j_wait_commit, &wait, 176470decc6SDave Kleikamp TASK_INTERRUPTIBLE); 177470decc6SDave Kleikamp if (journal->j_commit_sequence != journal->j_commit_request) 178470decc6SDave Kleikamp should_sleep = 0; 179470decc6SDave Kleikamp transaction = journal->j_running_transaction; 180470decc6SDave Kleikamp if (transaction && time_after_eq(jiffies, 181470decc6SDave Kleikamp transaction->t_expires)) 182470decc6SDave Kleikamp should_sleep = 0; 183f7f4bccbSMingming Cao if (journal->j_flags & JBD2_UNMOUNT) 184470decc6SDave Kleikamp should_sleep = 0; 185470decc6SDave Kleikamp if (should_sleep) { 186470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 187470decc6SDave Kleikamp schedule(); 188470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 189470decc6SDave Kleikamp } 190470decc6SDave Kleikamp finish_wait(&journal->j_wait_commit, &wait); 191470decc6SDave Kleikamp } 192470decc6SDave Kleikamp 193f7f4bccbSMingming Cao jbd_debug(1, "kjournald2 wakes\n"); 194470decc6SDave Kleikamp 195470decc6SDave Kleikamp /* 196470decc6SDave Kleikamp * Were we woken up by a commit wakeup event? 197470decc6SDave Kleikamp */ 198470decc6SDave Kleikamp transaction = journal->j_running_transaction; 199470decc6SDave Kleikamp if (transaction && time_after_eq(jiffies, transaction->t_expires)) { 200470decc6SDave Kleikamp journal->j_commit_request = transaction->t_tid; 201470decc6SDave Kleikamp jbd_debug(1, "woke because of timeout\n"); 202470decc6SDave Kleikamp } 203470decc6SDave Kleikamp goto loop; 204470decc6SDave Kleikamp 205470decc6SDave Kleikamp end_loop: 206470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 207470decc6SDave Kleikamp del_timer_sync(&journal->j_commit_timer); 208470decc6SDave Kleikamp journal->j_task = NULL; 209470decc6SDave Kleikamp wake_up(&journal->j_wait_done_commit); 210470decc6SDave Kleikamp jbd_debug(1, "Journal thread exiting.\n"); 211470decc6SDave Kleikamp return 0; 212470decc6SDave Kleikamp } 213470decc6SDave Kleikamp 21497f06784SPavel Emelianov static int jbd2_journal_start_thread(journal_t *journal) 215470decc6SDave Kleikamp { 21697f06784SPavel Emelianov struct task_struct *t; 21797f06784SPavel Emelianov 21897f06784SPavel Emelianov t = kthread_run(kjournald2, journal, "kjournald2"); 21997f06784SPavel Emelianov if (IS_ERR(t)) 22097f06784SPavel Emelianov return PTR_ERR(t); 22197f06784SPavel Emelianov 2221076d17aSAl Viro wait_event(journal->j_wait_done_commit, journal->j_task != NULL); 22397f06784SPavel Emelianov return 0; 224470decc6SDave Kleikamp } 225470decc6SDave Kleikamp 226470decc6SDave Kleikamp static void journal_kill_thread(journal_t *journal) 227470decc6SDave Kleikamp { 228470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 229f7f4bccbSMingming Cao journal->j_flags |= JBD2_UNMOUNT; 230470decc6SDave Kleikamp 231470decc6SDave Kleikamp while (journal->j_task) { 232470decc6SDave Kleikamp wake_up(&journal->j_wait_commit); 233470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 2341076d17aSAl Viro wait_event(journal->j_wait_done_commit, journal->j_task == NULL); 235470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 236470decc6SDave Kleikamp } 237470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 238470decc6SDave Kleikamp } 239470decc6SDave Kleikamp 240470decc6SDave Kleikamp /* 241f7f4bccbSMingming Cao * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal. 242470decc6SDave Kleikamp * 243470decc6SDave Kleikamp * Writes a metadata buffer to a given disk block. The actual IO is not 244470decc6SDave Kleikamp * performed but a new buffer_head is constructed which labels the data 245470decc6SDave Kleikamp * to be written with the correct destination disk block. 246470decc6SDave Kleikamp * 247470decc6SDave Kleikamp * Any magic-number escaping which needs to be done will cause a 248470decc6SDave Kleikamp * copy-out here. If the buffer happens to start with the 249f7f4bccbSMingming Cao * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the 250470decc6SDave Kleikamp * magic number is only written to the log for descripter blocks. In 251470decc6SDave Kleikamp * this case, we copy the data and replace the first word with 0, and we 252470decc6SDave Kleikamp * return a result code which indicates that this buffer needs to be 253470decc6SDave Kleikamp * marked as an escaped buffer in the corresponding log descriptor 254470decc6SDave Kleikamp * block. The missing word can then be restored when the block is read 255470decc6SDave Kleikamp * during recovery. 256470decc6SDave Kleikamp * 257470decc6SDave Kleikamp * If the source buffer has already been modified by a new transaction 258470decc6SDave Kleikamp * since we took the last commit snapshot, we use the frozen copy of 259470decc6SDave Kleikamp * that data for IO. If we end up using the existing buffer_head's data 260470decc6SDave Kleikamp * for the write, then we *have* to lock the buffer to prevent anyone 261470decc6SDave Kleikamp * else from using and possibly modifying it while the IO is in 262470decc6SDave Kleikamp * progress. 263470decc6SDave Kleikamp * 264470decc6SDave Kleikamp * The function returns a pointer to the buffer_heads to be used for IO. 265470decc6SDave Kleikamp * 266470decc6SDave Kleikamp * We assume that the journal has already been locked in this function. 267470decc6SDave Kleikamp * 268470decc6SDave Kleikamp * Return value: 269470decc6SDave Kleikamp * <0: Error 270470decc6SDave Kleikamp * >=0: Finished OK 271470decc6SDave Kleikamp * 272470decc6SDave Kleikamp * On success: 273470decc6SDave Kleikamp * Bit 0 set == escape performed on the data 274470decc6SDave Kleikamp * Bit 1 set == buffer copy-out performed (kfree the data after IO) 275470decc6SDave Kleikamp */ 276470decc6SDave Kleikamp 277f7f4bccbSMingming Cao int jbd2_journal_write_metadata_buffer(transaction_t *transaction, 278470decc6SDave Kleikamp struct journal_head *jh_in, 279470decc6SDave Kleikamp struct journal_head **jh_out, 28018eba7aaSMingming Cao unsigned long long blocknr) 281470decc6SDave Kleikamp { 282470decc6SDave Kleikamp int need_copy_out = 0; 283470decc6SDave Kleikamp int done_copy_out = 0; 284470decc6SDave Kleikamp int do_escape = 0; 285470decc6SDave Kleikamp char *mapped_data; 286470decc6SDave Kleikamp struct buffer_head *new_bh; 287470decc6SDave Kleikamp struct journal_head *new_jh; 288470decc6SDave Kleikamp struct page *new_page; 289470decc6SDave Kleikamp unsigned int new_offset; 290470decc6SDave Kleikamp struct buffer_head *bh_in = jh2bh(jh_in); 291470decc6SDave Kleikamp 292470decc6SDave Kleikamp /* 293470decc6SDave Kleikamp * The buffer really shouldn't be locked: only the current committing 294470decc6SDave Kleikamp * transaction is allowed to write it, so nobody else is allowed 295470decc6SDave Kleikamp * to do any IO. 296470decc6SDave Kleikamp * 297470decc6SDave Kleikamp * akpm: except if we're journalling data, and write() output is 298470decc6SDave Kleikamp * also part of a shared mapping, and another thread has 299470decc6SDave Kleikamp * decided to launch a writepage() against this buffer. 300470decc6SDave Kleikamp */ 301470decc6SDave Kleikamp J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in)); 302470decc6SDave Kleikamp 303470decc6SDave Kleikamp new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL); 304470decc6SDave Kleikamp 305470decc6SDave Kleikamp /* 306470decc6SDave Kleikamp * If a new transaction has already done a buffer copy-out, then 307470decc6SDave Kleikamp * we use that version of the data for the commit. 308470decc6SDave Kleikamp */ 309470decc6SDave Kleikamp jbd_lock_bh_state(bh_in); 310470decc6SDave Kleikamp repeat: 311470decc6SDave Kleikamp if (jh_in->b_frozen_data) { 312470decc6SDave Kleikamp done_copy_out = 1; 313470decc6SDave Kleikamp new_page = virt_to_page(jh_in->b_frozen_data); 314470decc6SDave Kleikamp new_offset = offset_in_page(jh_in->b_frozen_data); 315470decc6SDave Kleikamp } else { 316470decc6SDave Kleikamp new_page = jh2bh(jh_in)->b_page; 317470decc6SDave Kleikamp new_offset = offset_in_page(jh2bh(jh_in)->b_data); 318470decc6SDave Kleikamp } 319470decc6SDave Kleikamp 320470decc6SDave Kleikamp mapped_data = kmap_atomic(new_page, KM_USER0); 321470decc6SDave Kleikamp /* 322470decc6SDave Kleikamp * Check for escaping 323470decc6SDave Kleikamp */ 324470decc6SDave Kleikamp if (*((__be32 *)(mapped_data + new_offset)) == 325f7f4bccbSMingming Cao cpu_to_be32(JBD2_MAGIC_NUMBER)) { 326470decc6SDave Kleikamp need_copy_out = 1; 327470decc6SDave Kleikamp do_escape = 1; 328470decc6SDave Kleikamp } 329470decc6SDave Kleikamp kunmap_atomic(mapped_data, KM_USER0); 330470decc6SDave Kleikamp 331470decc6SDave Kleikamp /* 332470decc6SDave Kleikamp * Do we need to do a data copy? 333470decc6SDave Kleikamp */ 334470decc6SDave Kleikamp if (need_copy_out && !done_copy_out) { 335470decc6SDave Kleikamp char *tmp; 336470decc6SDave Kleikamp 337470decc6SDave Kleikamp jbd_unlock_bh_state(bh_in); 338af1e76d6SMingming Cao tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS); 339470decc6SDave Kleikamp jbd_lock_bh_state(bh_in); 340470decc6SDave Kleikamp if (jh_in->b_frozen_data) { 341af1e76d6SMingming Cao jbd2_free(tmp, bh_in->b_size); 342470decc6SDave Kleikamp goto repeat; 343470decc6SDave Kleikamp } 344470decc6SDave Kleikamp 345470decc6SDave Kleikamp jh_in->b_frozen_data = tmp; 346470decc6SDave Kleikamp mapped_data = kmap_atomic(new_page, KM_USER0); 347470decc6SDave Kleikamp memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size); 348470decc6SDave Kleikamp kunmap_atomic(mapped_data, KM_USER0); 349470decc6SDave Kleikamp 350470decc6SDave Kleikamp new_page = virt_to_page(tmp); 351470decc6SDave Kleikamp new_offset = offset_in_page(tmp); 352470decc6SDave Kleikamp done_copy_out = 1; 353470decc6SDave Kleikamp } 354470decc6SDave Kleikamp 355470decc6SDave Kleikamp /* 356470decc6SDave Kleikamp * Did we need to do an escaping? Now we've done all the 357470decc6SDave Kleikamp * copying, we can finally do so. 358470decc6SDave Kleikamp */ 359470decc6SDave Kleikamp if (do_escape) { 360470decc6SDave Kleikamp mapped_data = kmap_atomic(new_page, KM_USER0); 361470decc6SDave Kleikamp *((unsigned int *)(mapped_data + new_offset)) = 0; 362470decc6SDave Kleikamp kunmap_atomic(mapped_data, KM_USER0); 363470decc6SDave Kleikamp } 364470decc6SDave Kleikamp 365470decc6SDave Kleikamp /* keep subsequent assertions sane */ 366470decc6SDave Kleikamp new_bh->b_state = 0; 367470decc6SDave Kleikamp init_buffer(new_bh, NULL, NULL); 368470decc6SDave Kleikamp atomic_set(&new_bh->b_count, 1); 369470decc6SDave Kleikamp jbd_unlock_bh_state(bh_in); 370470decc6SDave Kleikamp 371f7f4bccbSMingming Cao new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */ 372470decc6SDave Kleikamp 373470decc6SDave Kleikamp set_bh_page(new_bh, new_page, new_offset); 374470decc6SDave Kleikamp new_jh->b_transaction = NULL; 375470decc6SDave Kleikamp new_bh->b_size = jh2bh(jh_in)->b_size; 376470decc6SDave Kleikamp new_bh->b_bdev = transaction->t_journal->j_dev; 377470decc6SDave Kleikamp new_bh->b_blocknr = blocknr; 378470decc6SDave Kleikamp set_buffer_mapped(new_bh); 379470decc6SDave Kleikamp set_buffer_dirty(new_bh); 380470decc6SDave Kleikamp 381470decc6SDave Kleikamp *jh_out = new_jh; 382470decc6SDave Kleikamp 383470decc6SDave Kleikamp /* 384470decc6SDave Kleikamp * The to-be-written buffer needs to get moved to the io queue, 385470decc6SDave Kleikamp * and the original buffer whose contents we are shadowing or 386470decc6SDave Kleikamp * copying is moved to the transaction's shadow queue. 387470decc6SDave Kleikamp */ 388470decc6SDave Kleikamp JBUFFER_TRACE(jh_in, "file as BJ_Shadow"); 389f7f4bccbSMingming Cao jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow); 390470decc6SDave Kleikamp JBUFFER_TRACE(new_jh, "file as BJ_IO"); 391f7f4bccbSMingming Cao jbd2_journal_file_buffer(new_jh, transaction, BJ_IO); 392470decc6SDave Kleikamp 393470decc6SDave Kleikamp return do_escape | (done_copy_out << 1); 394470decc6SDave Kleikamp } 395470decc6SDave Kleikamp 396470decc6SDave Kleikamp /* 397470decc6SDave Kleikamp * Allocation code for the journal file. Manage the space left in the 398470decc6SDave Kleikamp * journal, so that we can begin checkpointing when appropriate. 399470decc6SDave Kleikamp */ 400470decc6SDave Kleikamp 401470decc6SDave Kleikamp /* 402f7f4bccbSMingming Cao * __jbd2_log_space_left: Return the number of free blocks left in the journal. 403470decc6SDave Kleikamp * 404470decc6SDave Kleikamp * Called with the journal already locked. 405470decc6SDave Kleikamp * 406470decc6SDave Kleikamp * Called under j_state_lock 407470decc6SDave Kleikamp */ 408470decc6SDave Kleikamp 409f7f4bccbSMingming Cao int __jbd2_log_space_left(journal_t *journal) 410470decc6SDave Kleikamp { 411470decc6SDave Kleikamp int left = journal->j_free; 412470decc6SDave Kleikamp 413470decc6SDave Kleikamp assert_spin_locked(&journal->j_state_lock); 414470decc6SDave Kleikamp 415470decc6SDave Kleikamp /* 416470decc6SDave Kleikamp * Be pessimistic here about the number of those free blocks which 417470decc6SDave Kleikamp * might be required for log descriptor control blocks. 418470decc6SDave Kleikamp */ 419470decc6SDave Kleikamp 420470decc6SDave Kleikamp #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */ 421470decc6SDave Kleikamp 422470decc6SDave Kleikamp left -= MIN_LOG_RESERVED_BLOCKS; 423470decc6SDave Kleikamp 424470decc6SDave Kleikamp if (left <= 0) 425470decc6SDave Kleikamp return 0; 426470decc6SDave Kleikamp left -= (left >> 3); 427470decc6SDave Kleikamp return left; 428470decc6SDave Kleikamp } 429470decc6SDave Kleikamp 430470decc6SDave Kleikamp /* 431470decc6SDave Kleikamp * Called under j_state_lock. Returns true if a transaction was started. 432470decc6SDave Kleikamp */ 433f7f4bccbSMingming Cao int __jbd2_log_start_commit(journal_t *journal, tid_t target) 434470decc6SDave Kleikamp { 435470decc6SDave Kleikamp /* 436470decc6SDave Kleikamp * Are we already doing a recent enough commit? 437470decc6SDave Kleikamp */ 438470decc6SDave Kleikamp if (!tid_geq(journal->j_commit_request, target)) { 439470decc6SDave Kleikamp /* 440470decc6SDave Kleikamp * We want a new commit: OK, mark the request and wakup the 441470decc6SDave Kleikamp * commit thread. We do _not_ do the commit ourselves. 442470decc6SDave Kleikamp */ 443470decc6SDave Kleikamp 444470decc6SDave Kleikamp journal->j_commit_request = target; 445470decc6SDave Kleikamp jbd_debug(1, "JBD: requesting commit %d/%d\n", 446470decc6SDave Kleikamp journal->j_commit_request, 447470decc6SDave Kleikamp journal->j_commit_sequence); 448470decc6SDave Kleikamp wake_up(&journal->j_wait_commit); 449470decc6SDave Kleikamp return 1; 450470decc6SDave Kleikamp } 451470decc6SDave Kleikamp return 0; 452470decc6SDave Kleikamp } 453470decc6SDave Kleikamp 454f7f4bccbSMingming Cao int jbd2_log_start_commit(journal_t *journal, tid_t tid) 455470decc6SDave Kleikamp { 456470decc6SDave Kleikamp int ret; 457470decc6SDave Kleikamp 458470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 459f7f4bccbSMingming Cao ret = __jbd2_log_start_commit(journal, tid); 460470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 461470decc6SDave Kleikamp return ret; 462470decc6SDave Kleikamp } 463470decc6SDave Kleikamp 464470decc6SDave Kleikamp /* 465470decc6SDave Kleikamp * Force and wait upon a commit if the calling process is not within 466470decc6SDave Kleikamp * transaction. This is used for forcing out undo-protected data which contains 467470decc6SDave Kleikamp * bitmaps, when the fs is running out of space. 468470decc6SDave Kleikamp * 469470decc6SDave Kleikamp * We can only force the running transaction if we don't have an active handle; 470470decc6SDave Kleikamp * otherwise, we will deadlock. 471470decc6SDave Kleikamp * 472470decc6SDave Kleikamp * Returns true if a transaction was started. 473470decc6SDave Kleikamp */ 474f7f4bccbSMingming Cao int jbd2_journal_force_commit_nested(journal_t *journal) 475470decc6SDave Kleikamp { 476470decc6SDave Kleikamp transaction_t *transaction = NULL; 477470decc6SDave Kleikamp tid_t tid; 478470decc6SDave Kleikamp 479470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 480470decc6SDave Kleikamp if (journal->j_running_transaction && !current->journal_info) { 481470decc6SDave Kleikamp transaction = journal->j_running_transaction; 482f7f4bccbSMingming Cao __jbd2_log_start_commit(journal, transaction->t_tid); 483470decc6SDave Kleikamp } else if (journal->j_committing_transaction) 484470decc6SDave Kleikamp transaction = journal->j_committing_transaction; 485470decc6SDave Kleikamp 486470decc6SDave Kleikamp if (!transaction) { 487470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 488470decc6SDave Kleikamp return 0; /* Nothing to retry */ 489470decc6SDave Kleikamp } 490470decc6SDave Kleikamp 491470decc6SDave Kleikamp tid = transaction->t_tid; 492470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 493f7f4bccbSMingming Cao jbd2_log_wait_commit(journal, tid); 494470decc6SDave Kleikamp return 1; 495470decc6SDave Kleikamp } 496470decc6SDave Kleikamp 497470decc6SDave Kleikamp /* 498470decc6SDave Kleikamp * Start a commit of the current running transaction (if any). Returns true 499470decc6SDave Kleikamp * if a transaction was started, and fills its tid in at *ptid 500470decc6SDave Kleikamp */ 501f7f4bccbSMingming Cao int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid) 502470decc6SDave Kleikamp { 503470decc6SDave Kleikamp int ret = 0; 504470decc6SDave Kleikamp 505470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 506470decc6SDave Kleikamp if (journal->j_running_transaction) { 507470decc6SDave Kleikamp tid_t tid = journal->j_running_transaction->t_tid; 508470decc6SDave Kleikamp 509f7f4bccbSMingming Cao ret = __jbd2_log_start_commit(journal, tid); 510470decc6SDave Kleikamp if (ret && ptid) 511470decc6SDave Kleikamp *ptid = tid; 512470decc6SDave Kleikamp } else if (journal->j_committing_transaction && ptid) { 513470decc6SDave Kleikamp /* 514470decc6SDave Kleikamp * If ext3_write_super() recently started a commit, then we 515470decc6SDave Kleikamp * have to wait for completion of that transaction 516470decc6SDave Kleikamp */ 517470decc6SDave Kleikamp *ptid = journal->j_committing_transaction->t_tid; 518470decc6SDave Kleikamp ret = 1; 519470decc6SDave Kleikamp } 520470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 521470decc6SDave Kleikamp return ret; 522470decc6SDave Kleikamp } 523470decc6SDave Kleikamp 524470decc6SDave Kleikamp /* 525470decc6SDave Kleikamp * Wait for a specified commit to complete. 526470decc6SDave Kleikamp * The caller may not hold the journal lock. 527470decc6SDave Kleikamp */ 528f7f4bccbSMingming Cao int jbd2_log_wait_commit(journal_t *journal, tid_t tid) 529470decc6SDave Kleikamp { 530470decc6SDave Kleikamp int err = 0; 531470decc6SDave Kleikamp 532e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 533470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 534470decc6SDave Kleikamp if (!tid_geq(journal->j_commit_request, tid)) { 535470decc6SDave Kleikamp printk(KERN_EMERG 536470decc6SDave Kleikamp "%s: error: j_commit_request=%d, tid=%d\n", 537470decc6SDave Kleikamp __FUNCTION__, journal->j_commit_request, tid); 538470decc6SDave Kleikamp } 539470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 540470decc6SDave Kleikamp #endif 541470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 542470decc6SDave Kleikamp while (tid_gt(tid, journal->j_commit_sequence)) { 543470decc6SDave Kleikamp jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n", 544470decc6SDave Kleikamp tid, journal->j_commit_sequence); 545470decc6SDave Kleikamp wake_up(&journal->j_wait_commit); 546470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 547470decc6SDave Kleikamp wait_event(journal->j_wait_done_commit, 548470decc6SDave Kleikamp !tid_gt(tid, journal->j_commit_sequence)); 549470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 550470decc6SDave Kleikamp } 551470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 552470decc6SDave Kleikamp 553470decc6SDave Kleikamp if (unlikely(is_journal_aborted(journal))) { 554470decc6SDave Kleikamp printk(KERN_EMERG "journal commit I/O error\n"); 555470decc6SDave Kleikamp err = -EIO; 556470decc6SDave Kleikamp } 557470decc6SDave Kleikamp return err; 558470decc6SDave Kleikamp } 559470decc6SDave Kleikamp 560470decc6SDave Kleikamp /* 561470decc6SDave Kleikamp * Log buffer allocation routines: 562470decc6SDave Kleikamp */ 563470decc6SDave Kleikamp 56418eba7aaSMingming Cao int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp) 565470decc6SDave Kleikamp { 566470decc6SDave Kleikamp unsigned long blocknr; 567470decc6SDave Kleikamp 568470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 569470decc6SDave Kleikamp J_ASSERT(journal->j_free > 1); 570470decc6SDave Kleikamp 571470decc6SDave Kleikamp blocknr = journal->j_head; 572470decc6SDave Kleikamp journal->j_head++; 573470decc6SDave Kleikamp journal->j_free--; 574470decc6SDave Kleikamp if (journal->j_head == journal->j_last) 575470decc6SDave Kleikamp journal->j_head = journal->j_first; 576470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 577f7f4bccbSMingming Cao return jbd2_journal_bmap(journal, blocknr, retp); 578470decc6SDave Kleikamp } 579470decc6SDave Kleikamp 580470decc6SDave Kleikamp /* 581470decc6SDave Kleikamp * Conversion of logical to physical block numbers for the journal 582470decc6SDave Kleikamp * 583470decc6SDave Kleikamp * On external journals the journal blocks are identity-mapped, so 584470decc6SDave Kleikamp * this is a no-op. If needed, we can use j_blk_offset - everything is 585470decc6SDave Kleikamp * ready. 586470decc6SDave Kleikamp */ 587f7f4bccbSMingming Cao int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr, 58818eba7aaSMingming Cao unsigned long long *retp) 589470decc6SDave Kleikamp { 590470decc6SDave Kleikamp int err = 0; 59118eba7aaSMingming Cao unsigned long long ret; 592470decc6SDave Kleikamp 593470decc6SDave Kleikamp if (journal->j_inode) { 594470decc6SDave Kleikamp ret = bmap(journal->j_inode, blocknr); 595470decc6SDave Kleikamp if (ret) 596470decc6SDave Kleikamp *retp = ret; 597470decc6SDave Kleikamp else { 598470decc6SDave Kleikamp char b[BDEVNAME_SIZE]; 599470decc6SDave Kleikamp 600470decc6SDave Kleikamp printk(KERN_ALERT "%s: journal block not found " 601470decc6SDave Kleikamp "at offset %lu on %s\n", 602470decc6SDave Kleikamp __FUNCTION__, 603470decc6SDave Kleikamp blocknr, 604470decc6SDave Kleikamp bdevname(journal->j_dev, b)); 605470decc6SDave Kleikamp err = -EIO; 606470decc6SDave Kleikamp __journal_abort_soft(journal, err); 607470decc6SDave Kleikamp } 608470decc6SDave Kleikamp } else { 609470decc6SDave Kleikamp *retp = blocknr; /* +journal->j_blk_offset */ 610470decc6SDave Kleikamp } 611470decc6SDave Kleikamp return err; 612470decc6SDave Kleikamp } 613470decc6SDave Kleikamp 614470decc6SDave Kleikamp /* 615470decc6SDave Kleikamp * We play buffer_head aliasing tricks to write data/metadata blocks to 616470decc6SDave Kleikamp * the journal without copying their contents, but for journal 617470decc6SDave Kleikamp * descriptor blocks we do need to generate bona fide buffers. 618470decc6SDave Kleikamp * 619f7f4bccbSMingming Cao * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying 620470decc6SDave Kleikamp * the buffer's contents they really should run flush_dcache_page(bh->b_page). 621470decc6SDave Kleikamp * But we don't bother doing that, so there will be coherency problems with 622470decc6SDave Kleikamp * mmaps of blockdevs which hold live JBD-controlled filesystems. 623470decc6SDave Kleikamp */ 624f7f4bccbSMingming Cao struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal) 625470decc6SDave Kleikamp { 626470decc6SDave Kleikamp struct buffer_head *bh; 62718eba7aaSMingming Cao unsigned long long blocknr; 628470decc6SDave Kleikamp int err; 629470decc6SDave Kleikamp 630f7f4bccbSMingming Cao err = jbd2_journal_next_log_block(journal, &blocknr); 631470decc6SDave Kleikamp 632470decc6SDave Kleikamp if (err) 633470decc6SDave Kleikamp return NULL; 634470decc6SDave Kleikamp 635470decc6SDave Kleikamp bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize); 636470decc6SDave Kleikamp lock_buffer(bh); 637470decc6SDave Kleikamp memset(bh->b_data, 0, journal->j_blocksize); 638470decc6SDave Kleikamp set_buffer_uptodate(bh); 639470decc6SDave Kleikamp unlock_buffer(bh); 640470decc6SDave Kleikamp BUFFER_TRACE(bh, "return this buffer"); 641f7f4bccbSMingming Cao return jbd2_journal_add_journal_head(bh); 642470decc6SDave Kleikamp } 643470decc6SDave Kleikamp 6448e85fb3fSJohann Lombardi struct jbd2_stats_proc_session { 6458e85fb3fSJohann Lombardi journal_t *journal; 6468e85fb3fSJohann Lombardi struct transaction_stats_s *stats; 6478e85fb3fSJohann Lombardi int start; 6488e85fb3fSJohann Lombardi int max; 6498e85fb3fSJohann Lombardi }; 6508e85fb3fSJohann Lombardi 6518e85fb3fSJohann Lombardi static void *jbd2_history_skip_empty(struct jbd2_stats_proc_session *s, 6528e85fb3fSJohann Lombardi struct transaction_stats_s *ts, 6538e85fb3fSJohann Lombardi int first) 6548e85fb3fSJohann Lombardi { 6558e85fb3fSJohann Lombardi if (ts == s->stats + s->max) 6568e85fb3fSJohann Lombardi ts = s->stats; 6578e85fb3fSJohann Lombardi if (!first && ts == s->stats + s->start) 6588e85fb3fSJohann Lombardi return NULL; 6598e85fb3fSJohann Lombardi while (ts->ts_type == 0) { 6608e85fb3fSJohann Lombardi ts++; 6618e85fb3fSJohann Lombardi if (ts == s->stats + s->max) 6628e85fb3fSJohann Lombardi ts = s->stats; 6638e85fb3fSJohann Lombardi if (ts == s->stats + s->start) 6648e85fb3fSJohann Lombardi return NULL; 6658e85fb3fSJohann Lombardi } 6668e85fb3fSJohann Lombardi return ts; 6678e85fb3fSJohann Lombardi 6688e85fb3fSJohann Lombardi } 6698e85fb3fSJohann Lombardi 6708e85fb3fSJohann Lombardi static void *jbd2_seq_history_start(struct seq_file *seq, loff_t *pos) 6718e85fb3fSJohann Lombardi { 6728e85fb3fSJohann Lombardi struct jbd2_stats_proc_session *s = seq->private; 6738e85fb3fSJohann Lombardi struct transaction_stats_s *ts; 6748e85fb3fSJohann Lombardi int l = *pos; 6758e85fb3fSJohann Lombardi 6768e85fb3fSJohann Lombardi if (l == 0) 6778e85fb3fSJohann Lombardi return SEQ_START_TOKEN; 6788e85fb3fSJohann Lombardi ts = jbd2_history_skip_empty(s, s->stats + s->start, 1); 6798e85fb3fSJohann Lombardi if (!ts) 6808e85fb3fSJohann Lombardi return NULL; 6818e85fb3fSJohann Lombardi l--; 6828e85fb3fSJohann Lombardi while (l) { 6838e85fb3fSJohann Lombardi ts = jbd2_history_skip_empty(s, ++ts, 0); 6848e85fb3fSJohann Lombardi if (!ts) 6858e85fb3fSJohann Lombardi break; 6868e85fb3fSJohann Lombardi l--; 6878e85fb3fSJohann Lombardi } 6888e85fb3fSJohann Lombardi return ts; 6898e85fb3fSJohann Lombardi } 6908e85fb3fSJohann Lombardi 6918e85fb3fSJohann Lombardi static void *jbd2_seq_history_next(struct seq_file *seq, void *v, loff_t *pos) 6928e85fb3fSJohann Lombardi { 6938e85fb3fSJohann Lombardi struct jbd2_stats_proc_session *s = seq->private; 6948e85fb3fSJohann Lombardi struct transaction_stats_s *ts = v; 6958e85fb3fSJohann Lombardi 6968e85fb3fSJohann Lombardi ++*pos; 6978e85fb3fSJohann Lombardi if (v == SEQ_START_TOKEN) 6988e85fb3fSJohann Lombardi return jbd2_history_skip_empty(s, s->stats + s->start, 1); 6998e85fb3fSJohann Lombardi else 7008e85fb3fSJohann Lombardi return jbd2_history_skip_empty(s, ++ts, 0); 7018e85fb3fSJohann Lombardi } 7028e85fb3fSJohann Lombardi 7038e85fb3fSJohann Lombardi static int jbd2_seq_history_show(struct seq_file *seq, void *v) 7048e85fb3fSJohann Lombardi { 7058e85fb3fSJohann Lombardi struct transaction_stats_s *ts = v; 7068e85fb3fSJohann Lombardi if (v == SEQ_START_TOKEN) { 7078e85fb3fSJohann Lombardi seq_printf(seq, "%-4s %-5s %-5s %-5s %-5s %-5s %-5s %-6s %-5s " 7088e85fb3fSJohann Lombardi "%-5s %-5s %-5s %-5s %-5s\n", "R/C", "tid", 7098e85fb3fSJohann Lombardi "wait", "run", "lock", "flush", "log", "hndls", 7108e85fb3fSJohann Lombardi "block", "inlog", "ctime", "write", "drop", 7118e85fb3fSJohann Lombardi "close"); 7128e85fb3fSJohann Lombardi return 0; 7138e85fb3fSJohann Lombardi } 7148e85fb3fSJohann Lombardi if (ts->ts_type == JBD2_STATS_RUN) 7158e85fb3fSJohann Lombardi seq_printf(seq, "%-4s %-5lu %-5u %-5u %-5u %-5u %-5u " 7168e85fb3fSJohann Lombardi "%-6lu %-5lu %-5lu\n", "R", ts->ts_tid, 7178e85fb3fSJohann Lombardi jiffies_to_msecs(ts->u.run.rs_wait), 7188e85fb3fSJohann Lombardi jiffies_to_msecs(ts->u.run.rs_running), 7198e85fb3fSJohann Lombardi jiffies_to_msecs(ts->u.run.rs_locked), 7208e85fb3fSJohann Lombardi jiffies_to_msecs(ts->u.run.rs_flushing), 7218e85fb3fSJohann Lombardi jiffies_to_msecs(ts->u.run.rs_logging), 7228e85fb3fSJohann Lombardi ts->u.run.rs_handle_count, 7238e85fb3fSJohann Lombardi ts->u.run.rs_blocks, 7248e85fb3fSJohann Lombardi ts->u.run.rs_blocks_logged); 7258e85fb3fSJohann Lombardi else if (ts->ts_type == JBD2_STATS_CHECKPOINT) 7268e85fb3fSJohann Lombardi seq_printf(seq, "%-4s %-5lu %48s %-5u %-5lu %-5lu %-5lu\n", 7278e85fb3fSJohann Lombardi "C", ts->ts_tid, " ", 7288e85fb3fSJohann Lombardi jiffies_to_msecs(ts->u.chp.cs_chp_time), 7298e85fb3fSJohann Lombardi ts->u.chp.cs_written, ts->u.chp.cs_dropped, 7308e85fb3fSJohann Lombardi ts->u.chp.cs_forced_to_close); 7318e85fb3fSJohann Lombardi else 7328e85fb3fSJohann Lombardi J_ASSERT(0); 7338e85fb3fSJohann Lombardi return 0; 7348e85fb3fSJohann Lombardi } 7358e85fb3fSJohann Lombardi 7368e85fb3fSJohann Lombardi static void jbd2_seq_history_stop(struct seq_file *seq, void *v) 7378e85fb3fSJohann Lombardi { 7388e85fb3fSJohann Lombardi } 7398e85fb3fSJohann Lombardi 7408e85fb3fSJohann Lombardi static struct seq_operations jbd2_seq_history_ops = { 7418e85fb3fSJohann Lombardi .start = jbd2_seq_history_start, 7428e85fb3fSJohann Lombardi .next = jbd2_seq_history_next, 7438e85fb3fSJohann Lombardi .stop = jbd2_seq_history_stop, 7448e85fb3fSJohann Lombardi .show = jbd2_seq_history_show, 7458e85fb3fSJohann Lombardi }; 7468e85fb3fSJohann Lombardi 7478e85fb3fSJohann Lombardi static int jbd2_seq_history_open(struct inode *inode, struct file *file) 7488e85fb3fSJohann Lombardi { 7498e85fb3fSJohann Lombardi journal_t *journal = PDE(inode)->data; 7508e85fb3fSJohann Lombardi struct jbd2_stats_proc_session *s; 7518e85fb3fSJohann Lombardi int rc, size; 7528e85fb3fSJohann Lombardi 7538e85fb3fSJohann Lombardi s = kmalloc(sizeof(*s), GFP_KERNEL); 7548e85fb3fSJohann Lombardi if (s == NULL) 7558e85fb3fSJohann Lombardi return -ENOMEM; 7568e85fb3fSJohann Lombardi size = sizeof(struct transaction_stats_s) * journal->j_history_max; 7578e85fb3fSJohann Lombardi s->stats = kmalloc(size, GFP_KERNEL); 7588e85fb3fSJohann Lombardi if (s->stats == NULL) { 7598e85fb3fSJohann Lombardi kfree(s); 7608e85fb3fSJohann Lombardi return -ENOMEM; 7618e85fb3fSJohann Lombardi } 7628e85fb3fSJohann Lombardi spin_lock(&journal->j_history_lock); 7638e85fb3fSJohann Lombardi memcpy(s->stats, journal->j_history, size); 7648e85fb3fSJohann Lombardi s->max = journal->j_history_max; 7658e85fb3fSJohann Lombardi s->start = journal->j_history_cur % s->max; 7668e85fb3fSJohann Lombardi spin_unlock(&journal->j_history_lock); 7678e85fb3fSJohann Lombardi 7688e85fb3fSJohann Lombardi rc = seq_open(file, &jbd2_seq_history_ops); 7698e85fb3fSJohann Lombardi if (rc == 0) { 7708e85fb3fSJohann Lombardi struct seq_file *m = file->private_data; 7718e85fb3fSJohann Lombardi m->private = s; 7728e85fb3fSJohann Lombardi } else { 7738e85fb3fSJohann Lombardi kfree(s->stats); 7748e85fb3fSJohann Lombardi kfree(s); 7758e85fb3fSJohann Lombardi } 7768e85fb3fSJohann Lombardi return rc; 7778e85fb3fSJohann Lombardi 7788e85fb3fSJohann Lombardi } 7798e85fb3fSJohann Lombardi 7808e85fb3fSJohann Lombardi static int jbd2_seq_history_release(struct inode *inode, struct file *file) 7818e85fb3fSJohann Lombardi { 7828e85fb3fSJohann Lombardi struct seq_file *seq = file->private_data; 7838e85fb3fSJohann Lombardi struct jbd2_stats_proc_session *s = seq->private; 7848e85fb3fSJohann Lombardi 7858e85fb3fSJohann Lombardi kfree(s->stats); 7868e85fb3fSJohann Lombardi kfree(s); 7878e85fb3fSJohann Lombardi return seq_release(inode, file); 7888e85fb3fSJohann Lombardi } 7898e85fb3fSJohann Lombardi 7908e85fb3fSJohann Lombardi static struct file_operations jbd2_seq_history_fops = { 7918e85fb3fSJohann Lombardi .owner = THIS_MODULE, 7928e85fb3fSJohann Lombardi .open = jbd2_seq_history_open, 7938e85fb3fSJohann Lombardi .read = seq_read, 7948e85fb3fSJohann Lombardi .llseek = seq_lseek, 7958e85fb3fSJohann Lombardi .release = jbd2_seq_history_release, 7968e85fb3fSJohann Lombardi }; 7978e85fb3fSJohann Lombardi 7988e85fb3fSJohann Lombardi static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos) 7998e85fb3fSJohann Lombardi { 8008e85fb3fSJohann Lombardi return *pos ? NULL : SEQ_START_TOKEN; 8018e85fb3fSJohann Lombardi } 8028e85fb3fSJohann Lombardi 8038e85fb3fSJohann Lombardi static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos) 8048e85fb3fSJohann Lombardi { 8058e85fb3fSJohann Lombardi return NULL; 8068e85fb3fSJohann Lombardi } 8078e85fb3fSJohann Lombardi 8088e85fb3fSJohann Lombardi static int jbd2_seq_info_show(struct seq_file *seq, void *v) 8098e85fb3fSJohann Lombardi { 8108e85fb3fSJohann Lombardi struct jbd2_stats_proc_session *s = seq->private; 8118e85fb3fSJohann Lombardi 8128e85fb3fSJohann Lombardi if (v != SEQ_START_TOKEN) 8138e85fb3fSJohann Lombardi return 0; 8148e85fb3fSJohann Lombardi seq_printf(seq, "%lu transaction, each upto %u blocks\n", 8158e85fb3fSJohann Lombardi s->stats->ts_tid, 8168e85fb3fSJohann Lombardi s->journal->j_max_transaction_buffers); 8178e85fb3fSJohann Lombardi if (s->stats->ts_tid == 0) 8188e85fb3fSJohann Lombardi return 0; 8198e85fb3fSJohann Lombardi seq_printf(seq, "average: \n %ums waiting for transaction\n", 8208e85fb3fSJohann Lombardi jiffies_to_msecs(s->stats->u.run.rs_wait / s->stats->ts_tid)); 8218e85fb3fSJohann Lombardi seq_printf(seq, " %ums running transaction\n", 8228e85fb3fSJohann Lombardi jiffies_to_msecs(s->stats->u.run.rs_running / s->stats->ts_tid)); 8238e85fb3fSJohann Lombardi seq_printf(seq, " %ums transaction was being locked\n", 8248e85fb3fSJohann Lombardi jiffies_to_msecs(s->stats->u.run.rs_locked / s->stats->ts_tid)); 8258e85fb3fSJohann Lombardi seq_printf(seq, " %ums flushing data (in ordered mode)\n", 8268e85fb3fSJohann Lombardi jiffies_to_msecs(s->stats->u.run.rs_flushing / s->stats->ts_tid)); 8278e85fb3fSJohann Lombardi seq_printf(seq, " %ums logging transaction\n", 8288e85fb3fSJohann Lombardi jiffies_to_msecs(s->stats->u.run.rs_logging / s->stats->ts_tid)); 8298e85fb3fSJohann Lombardi seq_printf(seq, " %lu handles per transaction\n", 8308e85fb3fSJohann Lombardi s->stats->u.run.rs_handle_count / s->stats->ts_tid); 8318e85fb3fSJohann Lombardi seq_printf(seq, " %lu blocks per transaction\n", 8328e85fb3fSJohann Lombardi s->stats->u.run.rs_blocks / s->stats->ts_tid); 8338e85fb3fSJohann Lombardi seq_printf(seq, " %lu logged blocks per transaction\n", 8348e85fb3fSJohann Lombardi s->stats->u.run.rs_blocks_logged / s->stats->ts_tid); 8358e85fb3fSJohann Lombardi return 0; 8368e85fb3fSJohann Lombardi } 8378e85fb3fSJohann Lombardi 8388e85fb3fSJohann Lombardi static void jbd2_seq_info_stop(struct seq_file *seq, void *v) 8398e85fb3fSJohann Lombardi { 8408e85fb3fSJohann Lombardi } 8418e85fb3fSJohann Lombardi 8428e85fb3fSJohann Lombardi static struct seq_operations jbd2_seq_info_ops = { 8438e85fb3fSJohann Lombardi .start = jbd2_seq_info_start, 8448e85fb3fSJohann Lombardi .next = jbd2_seq_info_next, 8458e85fb3fSJohann Lombardi .stop = jbd2_seq_info_stop, 8468e85fb3fSJohann Lombardi .show = jbd2_seq_info_show, 8478e85fb3fSJohann Lombardi }; 8488e85fb3fSJohann Lombardi 8498e85fb3fSJohann Lombardi static int jbd2_seq_info_open(struct inode *inode, struct file *file) 8508e85fb3fSJohann Lombardi { 8518e85fb3fSJohann Lombardi journal_t *journal = PDE(inode)->data; 8528e85fb3fSJohann Lombardi struct jbd2_stats_proc_session *s; 8538e85fb3fSJohann Lombardi int rc, size; 8548e85fb3fSJohann Lombardi 8558e85fb3fSJohann Lombardi s = kmalloc(sizeof(*s), GFP_KERNEL); 8568e85fb3fSJohann Lombardi if (s == NULL) 8578e85fb3fSJohann Lombardi return -ENOMEM; 8588e85fb3fSJohann Lombardi size = sizeof(struct transaction_stats_s); 8598e85fb3fSJohann Lombardi s->stats = kmalloc(size, GFP_KERNEL); 8608e85fb3fSJohann Lombardi if (s->stats == NULL) { 8618e85fb3fSJohann Lombardi kfree(s); 8628e85fb3fSJohann Lombardi return -ENOMEM; 8638e85fb3fSJohann Lombardi } 8648e85fb3fSJohann Lombardi spin_lock(&journal->j_history_lock); 8658e85fb3fSJohann Lombardi memcpy(s->stats, &journal->j_stats, size); 8668e85fb3fSJohann Lombardi s->journal = journal; 8678e85fb3fSJohann Lombardi spin_unlock(&journal->j_history_lock); 8688e85fb3fSJohann Lombardi 8698e85fb3fSJohann Lombardi rc = seq_open(file, &jbd2_seq_info_ops); 8708e85fb3fSJohann Lombardi if (rc == 0) { 8718e85fb3fSJohann Lombardi struct seq_file *m = file->private_data; 8728e85fb3fSJohann Lombardi m->private = s; 8738e85fb3fSJohann Lombardi } else { 8748e85fb3fSJohann Lombardi kfree(s->stats); 8758e85fb3fSJohann Lombardi kfree(s); 8768e85fb3fSJohann Lombardi } 8778e85fb3fSJohann Lombardi return rc; 8788e85fb3fSJohann Lombardi 8798e85fb3fSJohann Lombardi } 8808e85fb3fSJohann Lombardi 8818e85fb3fSJohann Lombardi static int jbd2_seq_info_release(struct inode *inode, struct file *file) 8828e85fb3fSJohann Lombardi { 8838e85fb3fSJohann Lombardi struct seq_file *seq = file->private_data; 8848e85fb3fSJohann Lombardi struct jbd2_stats_proc_session *s = seq->private; 8858e85fb3fSJohann Lombardi kfree(s->stats); 8868e85fb3fSJohann Lombardi kfree(s); 8878e85fb3fSJohann Lombardi return seq_release(inode, file); 8888e85fb3fSJohann Lombardi } 8898e85fb3fSJohann Lombardi 8908e85fb3fSJohann Lombardi static struct file_operations jbd2_seq_info_fops = { 8918e85fb3fSJohann Lombardi .owner = THIS_MODULE, 8928e85fb3fSJohann Lombardi .open = jbd2_seq_info_open, 8938e85fb3fSJohann Lombardi .read = seq_read, 8948e85fb3fSJohann Lombardi .llseek = seq_lseek, 8958e85fb3fSJohann Lombardi .release = jbd2_seq_info_release, 8968e85fb3fSJohann Lombardi }; 8978e85fb3fSJohann Lombardi 8988e85fb3fSJohann Lombardi static struct proc_dir_entry *proc_jbd2_stats; 8998e85fb3fSJohann Lombardi 9008e85fb3fSJohann Lombardi static void jbd2_stats_proc_init(journal_t *journal) 9018e85fb3fSJohann Lombardi { 9028e85fb3fSJohann Lombardi char name[BDEVNAME_SIZE]; 9038e85fb3fSJohann Lombardi 9048e85fb3fSJohann Lombardi snprintf(name, sizeof(name) - 1, "%s", bdevname(journal->j_dev, name)); 9058e85fb3fSJohann Lombardi journal->j_proc_entry = proc_mkdir(name, proc_jbd2_stats); 9068e85fb3fSJohann Lombardi if (journal->j_proc_entry) { 907*79da3664SDenis V. Lunev proc_create_data("history", S_IRUGO, journal->j_proc_entry, 908*79da3664SDenis V. Lunev &jbd2_seq_history_fops, journal); 909*79da3664SDenis V. Lunev proc_create_data("info", S_IRUGO, journal->j_proc_entry, 910*79da3664SDenis V. Lunev &jbd2_seq_info_fops, journal); 9118e85fb3fSJohann Lombardi } 9128e85fb3fSJohann Lombardi } 9138e85fb3fSJohann Lombardi 9148e85fb3fSJohann Lombardi static void jbd2_stats_proc_exit(journal_t *journal) 9158e85fb3fSJohann Lombardi { 9168e85fb3fSJohann Lombardi char name[BDEVNAME_SIZE]; 9178e85fb3fSJohann Lombardi 9188e85fb3fSJohann Lombardi snprintf(name, sizeof(name) - 1, "%s", bdevname(journal->j_dev, name)); 9198e85fb3fSJohann Lombardi remove_proc_entry("info", journal->j_proc_entry); 9208e85fb3fSJohann Lombardi remove_proc_entry("history", journal->j_proc_entry); 9218e85fb3fSJohann Lombardi remove_proc_entry(name, proc_jbd2_stats); 9228e85fb3fSJohann Lombardi } 9238e85fb3fSJohann Lombardi 9248e85fb3fSJohann Lombardi static void journal_init_stats(journal_t *journal) 9258e85fb3fSJohann Lombardi { 9268e85fb3fSJohann Lombardi int size; 9278e85fb3fSJohann Lombardi 9288e85fb3fSJohann Lombardi if (!proc_jbd2_stats) 9298e85fb3fSJohann Lombardi return; 9308e85fb3fSJohann Lombardi 9318e85fb3fSJohann Lombardi journal->j_history_max = 100; 9328e85fb3fSJohann Lombardi size = sizeof(struct transaction_stats_s) * journal->j_history_max; 9338e85fb3fSJohann Lombardi journal->j_history = kzalloc(size, GFP_KERNEL); 9348e85fb3fSJohann Lombardi if (!journal->j_history) { 9358e85fb3fSJohann Lombardi journal->j_history_max = 0; 9368e85fb3fSJohann Lombardi return; 9378e85fb3fSJohann Lombardi } 9388e85fb3fSJohann Lombardi spin_lock_init(&journal->j_history_lock); 9398e85fb3fSJohann Lombardi } 9408e85fb3fSJohann Lombardi 941470decc6SDave Kleikamp /* 942470decc6SDave Kleikamp * Management for journal control blocks: functions to create and 943470decc6SDave Kleikamp * destroy journal_t structures, and to initialise and read existing 944470decc6SDave Kleikamp * journal blocks from disk. */ 945470decc6SDave Kleikamp 946470decc6SDave Kleikamp /* First: create and setup a journal_t object in memory. We initialise 947470decc6SDave Kleikamp * very few fields yet: that has to wait until we have created the 948470decc6SDave Kleikamp * journal structures from from scratch, or loaded them from disk. */ 949470decc6SDave Kleikamp 950470decc6SDave Kleikamp static journal_t * journal_init_common (void) 951470decc6SDave Kleikamp { 952470decc6SDave Kleikamp journal_t *journal; 953470decc6SDave Kleikamp int err; 954470decc6SDave Kleikamp 955d802ffa8SMingming Cao journal = kzalloc(sizeof(*journal), GFP_KERNEL|__GFP_NOFAIL); 956470decc6SDave Kleikamp if (!journal) 957470decc6SDave Kleikamp goto fail; 958470decc6SDave Kleikamp 959470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_transaction_locked); 960470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_logspace); 961470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_done_commit); 962470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_checkpoint); 963470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_commit); 964470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_updates); 965470decc6SDave Kleikamp mutex_init(&journal->j_barrier); 966470decc6SDave Kleikamp mutex_init(&journal->j_checkpoint_mutex); 967470decc6SDave Kleikamp spin_lock_init(&journal->j_revoke_lock); 968470decc6SDave Kleikamp spin_lock_init(&journal->j_list_lock); 969470decc6SDave Kleikamp spin_lock_init(&journal->j_state_lock); 970470decc6SDave Kleikamp 971cd02ff0bSMingming Cao journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE); 972470decc6SDave Kleikamp 973470decc6SDave Kleikamp /* The journal is marked for error until we succeed with recovery! */ 974f7f4bccbSMingming Cao journal->j_flags = JBD2_ABORT; 975470decc6SDave Kleikamp 976470decc6SDave Kleikamp /* Set up a default-sized revoke table for the new mount. */ 977f7f4bccbSMingming Cao err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH); 978470decc6SDave Kleikamp if (err) { 979470decc6SDave Kleikamp kfree(journal); 980470decc6SDave Kleikamp goto fail; 981470decc6SDave Kleikamp } 9828e85fb3fSJohann Lombardi 9838e85fb3fSJohann Lombardi journal_init_stats(journal); 9848e85fb3fSJohann Lombardi 985470decc6SDave Kleikamp return journal; 986470decc6SDave Kleikamp fail: 987470decc6SDave Kleikamp return NULL; 988470decc6SDave Kleikamp } 989470decc6SDave Kleikamp 990f7f4bccbSMingming Cao /* jbd2_journal_init_dev and jbd2_journal_init_inode: 991470decc6SDave Kleikamp * 992470decc6SDave Kleikamp * Create a journal structure assigned some fixed set of disk blocks to 993470decc6SDave Kleikamp * the journal. We don't actually touch those disk blocks yet, but we 994470decc6SDave Kleikamp * need to set up all of the mapping information to tell the journaling 995470decc6SDave Kleikamp * system where the journal blocks are. 996470decc6SDave Kleikamp * 997470decc6SDave Kleikamp */ 998470decc6SDave Kleikamp 999470decc6SDave Kleikamp /** 1000f7f4bccbSMingming Cao * journal_t * jbd2_journal_init_dev() - creates an initialises a journal structure 1001470decc6SDave Kleikamp * @bdev: Block device on which to create the journal 1002470decc6SDave Kleikamp * @fs_dev: Device which hold journalled filesystem for this journal. 1003470decc6SDave Kleikamp * @start: Block nr Start of journal. 1004470decc6SDave Kleikamp * @len: Length of the journal in blocks. 1005470decc6SDave Kleikamp * @blocksize: blocksize of journalling device 1006470decc6SDave Kleikamp * @returns: a newly created journal_t * 1007470decc6SDave Kleikamp * 1008f7f4bccbSMingming Cao * jbd2_journal_init_dev creates a journal which maps a fixed contiguous 1009470decc6SDave Kleikamp * range of blocks on an arbitrary block device. 1010470decc6SDave Kleikamp * 1011470decc6SDave Kleikamp */ 1012f7f4bccbSMingming Cao journal_t * jbd2_journal_init_dev(struct block_device *bdev, 1013470decc6SDave Kleikamp struct block_device *fs_dev, 101418eba7aaSMingming Cao unsigned long long start, int len, int blocksize) 1015470decc6SDave Kleikamp { 1016470decc6SDave Kleikamp journal_t *journal = journal_init_common(); 1017470decc6SDave Kleikamp struct buffer_head *bh; 1018470decc6SDave Kleikamp int n; 1019470decc6SDave Kleikamp 1020470decc6SDave Kleikamp if (!journal) 1021470decc6SDave Kleikamp return NULL; 1022470decc6SDave Kleikamp 1023470decc6SDave Kleikamp /* journal descriptor can store up to n blocks -bzzz */ 1024470decc6SDave Kleikamp journal->j_blocksize = blocksize; 1025470decc6SDave Kleikamp n = journal->j_blocksize / sizeof(journal_block_tag_t); 1026470decc6SDave Kleikamp journal->j_wbufsize = n; 1027470decc6SDave Kleikamp journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL); 1028470decc6SDave Kleikamp if (!journal->j_wbuf) { 1029470decc6SDave Kleikamp printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n", 1030470decc6SDave Kleikamp __FUNCTION__); 1031470decc6SDave Kleikamp kfree(journal); 1032470decc6SDave Kleikamp journal = NULL; 10335eb30790SDave Kleikamp goto out; 1034470decc6SDave Kleikamp } 1035470decc6SDave Kleikamp journal->j_dev = bdev; 1036470decc6SDave Kleikamp journal->j_fs_dev = fs_dev; 1037470decc6SDave Kleikamp journal->j_blk_offset = start; 1038470decc6SDave Kleikamp journal->j_maxlen = len; 10398e85fb3fSJohann Lombardi jbd2_stats_proc_init(journal); 1040470decc6SDave Kleikamp 1041470decc6SDave Kleikamp bh = __getblk(journal->j_dev, start, journal->j_blocksize); 1042470decc6SDave Kleikamp J_ASSERT(bh != NULL); 1043470decc6SDave Kleikamp journal->j_sb_buffer = bh; 1044470decc6SDave Kleikamp journal->j_superblock = (journal_superblock_t *)bh->b_data; 10455eb30790SDave Kleikamp out: 1046470decc6SDave Kleikamp return journal; 1047470decc6SDave Kleikamp } 1048470decc6SDave Kleikamp 1049470decc6SDave Kleikamp /** 1050f7f4bccbSMingming Cao * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode. 1051470decc6SDave Kleikamp * @inode: An inode to create the journal in 1052470decc6SDave Kleikamp * 1053f7f4bccbSMingming Cao * jbd2_journal_init_inode creates a journal which maps an on-disk inode as 1054470decc6SDave Kleikamp * the journal. The inode must exist already, must support bmap() and 1055470decc6SDave Kleikamp * must have all data blocks preallocated. 1056470decc6SDave Kleikamp */ 1057f7f4bccbSMingming Cao journal_t * jbd2_journal_init_inode (struct inode *inode) 1058470decc6SDave Kleikamp { 1059470decc6SDave Kleikamp struct buffer_head *bh; 1060470decc6SDave Kleikamp journal_t *journal = journal_init_common(); 1061470decc6SDave Kleikamp int err; 1062470decc6SDave Kleikamp int n; 106318eba7aaSMingming Cao unsigned long long blocknr; 1064470decc6SDave Kleikamp 1065470decc6SDave Kleikamp if (!journal) 1066470decc6SDave Kleikamp return NULL; 1067470decc6SDave Kleikamp 1068470decc6SDave Kleikamp journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev; 1069470decc6SDave Kleikamp journal->j_inode = inode; 1070470decc6SDave Kleikamp jbd_debug(1, 1071470decc6SDave Kleikamp "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n", 1072470decc6SDave Kleikamp journal, inode->i_sb->s_id, inode->i_ino, 1073470decc6SDave Kleikamp (long long) inode->i_size, 1074470decc6SDave Kleikamp inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize); 1075470decc6SDave Kleikamp 1076470decc6SDave Kleikamp journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits; 1077470decc6SDave Kleikamp journal->j_blocksize = inode->i_sb->s_blocksize; 10788e85fb3fSJohann Lombardi jbd2_stats_proc_init(journal); 1079470decc6SDave Kleikamp 1080470decc6SDave Kleikamp /* journal descriptor can store up to n blocks -bzzz */ 1081470decc6SDave Kleikamp n = journal->j_blocksize / sizeof(journal_block_tag_t); 1082470decc6SDave Kleikamp journal->j_wbufsize = n; 1083470decc6SDave Kleikamp journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL); 1084470decc6SDave Kleikamp if (!journal->j_wbuf) { 1085470decc6SDave Kleikamp printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n", 1086470decc6SDave Kleikamp __FUNCTION__); 1087470decc6SDave Kleikamp kfree(journal); 1088470decc6SDave Kleikamp return NULL; 1089470decc6SDave Kleikamp } 1090470decc6SDave Kleikamp 1091f7f4bccbSMingming Cao err = jbd2_journal_bmap(journal, 0, &blocknr); 1092470decc6SDave Kleikamp /* If that failed, give up */ 1093470decc6SDave Kleikamp if (err) { 1094470decc6SDave Kleikamp printk(KERN_ERR "%s: Cannnot locate journal superblock\n", 1095470decc6SDave Kleikamp __FUNCTION__); 1096470decc6SDave Kleikamp kfree(journal); 1097470decc6SDave Kleikamp return NULL; 1098470decc6SDave Kleikamp } 1099470decc6SDave Kleikamp 1100470decc6SDave Kleikamp bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize); 1101470decc6SDave Kleikamp J_ASSERT(bh != NULL); 1102470decc6SDave Kleikamp journal->j_sb_buffer = bh; 1103470decc6SDave Kleikamp journal->j_superblock = (journal_superblock_t *)bh->b_data; 1104470decc6SDave Kleikamp 1105470decc6SDave Kleikamp return journal; 1106470decc6SDave Kleikamp } 1107470decc6SDave Kleikamp 1108470decc6SDave Kleikamp /* 1109470decc6SDave Kleikamp * If the journal init or create aborts, we need to mark the journal 1110470decc6SDave Kleikamp * superblock as being NULL to prevent the journal destroy from writing 1111470decc6SDave Kleikamp * back a bogus superblock. 1112470decc6SDave Kleikamp */ 1113470decc6SDave Kleikamp static void journal_fail_superblock (journal_t *journal) 1114470decc6SDave Kleikamp { 1115470decc6SDave Kleikamp struct buffer_head *bh = journal->j_sb_buffer; 1116470decc6SDave Kleikamp brelse(bh); 1117470decc6SDave Kleikamp journal->j_sb_buffer = NULL; 1118470decc6SDave Kleikamp } 1119470decc6SDave Kleikamp 1120470decc6SDave Kleikamp /* 1121470decc6SDave Kleikamp * Given a journal_t structure, initialise the various fields for 1122470decc6SDave Kleikamp * startup of a new journaling session. We use this both when creating 1123470decc6SDave Kleikamp * a journal, and after recovering an old journal to reset it for 1124470decc6SDave Kleikamp * subsequent use. 1125470decc6SDave Kleikamp */ 1126470decc6SDave Kleikamp 1127470decc6SDave Kleikamp static int journal_reset(journal_t *journal) 1128470decc6SDave Kleikamp { 1129470decc6SDave Kleikamp journal_superblock_t *sb = journal->j_superblock; 113018eba7aaSMingming Cao unsigned long long first, last; 1131470decc6SDave Kleikamp 1132470decc6SDave Kleikamp first = be32_to_cpu(sb->s_first); 1133470decc6SDave Kleikamp last = be32_to_cpu(sb->s_maxlen); 1134470decc6SDave Kleikamp 1135470decc6SDave Kleikamp journal->j_first = first; 1136470decc6SDave Kleikamp journal->j_last = last; 1137470decc6SDave Kleikamp 1138470decc6SDave Kleikamp journal->j_head = first; 1139470decc6SDave Kleikamp journal->j_tail = first; 1140470decc6SDave Kleikamp journal->j_free = last - first; 1141470decc6SDave Kleikamp 1142470decc6SDave Kleikamp journal->j_tail_sequence = journal->j_transaction_sequence; 1143470decc6SDave Kleikamp journal->j_commit_sequence = journal->j_transaction_sequence - 1; 1144470decc6SDave Kleikamp journal->j_commit_request = journal->j_commit_sequence; 1145470decc6SDave Kleikamp 1146470decc6SDave Kleikamp journal->j_max_transaction_buffers = journal->j_maxlen / 4; 1147470decc6SDave Kleikamp 1148470decc6SDave Kleikamp /* Add the dynamic fields and write it to disk. */ 1149f7f4bccbSMingming Cao jbd2_journal_update_superblock(journal, 1); 115097f06784SPavel Emelianov return jbd2_journal_start_thread(journal); 1151470decc6SDave Kleikamp } 1152470decc6SDave Kleikamp 1153470decc6SDave Kleikamp /** 1154f7f4bccbSMingming Cao * int jbd2_journal_create() - Initialise the new journal file 1155470decc6SDave Kleikamp * @journal: Journal to create. This structure must have been initialised 1156470decc6SDave Kleikamp * 1157470decc6SDave Kleikamp * Given a journal_t structure which tells us which disk blocks we can 1158470decc6SDave Kleikamp * use, create a new journal superblock and initialise all of the 1159470decc6SDave Kleikamp * journal fields from scratch. 1160470decc6SDave Kleikamp **/ 1161f7f4bccbSMingming Cao int jbd2_journal_create(journal_t *journal) 1162470decc6SDave Kleikamp { 116318eba7aaSMingming Cao unsigned long long blocknr; 1164470decc6SDave Kleikamp struct buffer_head *bh; 1165470decc6SDave Kleikamp journal_superblock_t *sb; 1166470decc6SDave Kleikamp int i, err; 1167470decc6SDave Kleikamp 1168f7f4bccbSMingming Cao if (journal->j_maxlen < JBD2_MIN_JOURNAL_BLOCKS) { 1169470decc6SDave Kleikamp printk (KERN_ERR "Journal length (%d blocks) too short.\n", 1170470decc6SDave Kleikamp journal->j_maxlen); 1171470decc6SDave Kleikamp journal_fail_superblock(journal); 1172470decc6SDave Kleikamp return -EINVAL; 1173470decc6SDave Kleikamp } 1174470decc6SDave Kleikamp 1175470decc6SDave Kleikamp if (journal->j_inode == NULL) { 1176470decc6SDave Kleikamp /* 1177470decc6SDave Kleikamp * We don't know what block to start at! 1178470decc6SDave Kleikamp */ 1179470decc6SDave Kleikamp printk(KERN_EMERG 1180470decc6SDave Kleikamp "%s: creation of journal on external device!\n", 1181470decc6SDave Kleikamp __FUNCTION__); 1182470decc6SDave Kleikamp BUG(); 1183470decc6SDave Kleikamp } 1184470decc6SDave Kleikamp 1185470decc6SDave Kleikamp /* Zero out the entire journal on disk. We cannot afford to 1186f7f4bccbSMingming Cao have any blocks on disk beginning with JBD2_MAGIC_NUMBER. */ 1187470decc6SDave Kleikamp jbd_debug(1, "JBD: Zeroing out journal blocks...\n"); 1188470decc6SDave Kleikamp for (i = 0; i < journal->j_maxlen; i++) { 1189f7f4bccbSMingming Cao err = jbd2_journal_bmap(journal, i, &blocknr); 1190470decc6SDave Kleikamp if (err) 1191470decc6SDave Kleikamp return err; 1192470decc6SDave Kleikamp bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize); 1193470decc6SDave Kleikamp lock_buffer(bh); 1194470decc6SDave Kleikamp memset (bh->b_data, 0, journal->j_blocksize); 1195470decc6SDave Kleikamp BUFFER_TRACE(bh, "marking dirty"); 1196470decc6SDave Kleikamp mark_buffer_dirty(bh); 1197470decc6SDave Kleikamp BUFFER_TRACE(bh, "marking uptodate"); 1198470decc6SDave Kleikamp set_buffer_uptodate(bh); 1199470decc6SDave Kleikamp unlock_buffer(bh); 1200470decc6SDave Kleikamp __brelse(bh); 1201470decc6SDave Kleikamp } 1202470decc6SDave Kleikamp 1203470decc6SDave Kleikamp sync_blockdev(journal->j_dev); 1204470decc6SDave Kleikamp jbd_debug(1, "JBD: journal cleared.\n"); 1205470decc6SDave Kleikamp 1206470decc6SDave Kleikamp /* OK, fill in the initial static fields in the new superblock */ 1207470decc6SDave Kleikamp sb = journal->j_superblock; 1208470decc6SDave Kleikamp 1209f7f4bccbSMingming Cao sb->s_header.h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER); 1210f7f4bccbSMingming Cao sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2); 1211470decc6SDave Kleikamp 1212470decc6SDave Kleikamp sb->s_blocksize = cpu_to_be32(journal->j_blocksize); 1213470decc6SDave Kleikamp sb->s_maxlen = cpu_to_be32(journal->j_maxlen); 1214470decc6SDave Kleikamp sb->s_first = cpu_to_be32(1); 1215470decc6SDave Kleikamp 1216470decc6SDave Kleikamp journal->j_transaction_sequence = 1; 1217470decc6SDave Kleikamp 1218f7f4bccbSMingming Cao journal->j_flags &= ~JBD2_ABORT; 1219470decc6SDave Kleikamp journal->j_format_version = 2; 1220470decc6SDave Kleikamp 1221470decc6SDave Kleikamp return journal_reset(journal); 1222470decc6SDave Kleikamp } 1223470decc6SDave Kleikamp 1224470decc6SDave Kleikamp /** 1225f7f4bccbSMingming Cao * void jbd2_journal_update_superblock() - Update journal sb on disk. 1226470decc6SDave Kleikamp * @journal: The journal to update. 1227470decc6SDave Kleikamp * @wait: Set to '0' if you don't want to wait for IO completion. 1228470decc6SDave Kleikamp * 1229470decc6SDave Kleikamp * Update a journal's dynamic superblock fields and write it to disk, 1230470decc6SDave Kleikamp * optionally waiting for the IO to complete. 1231470decc6SDave Kleikamp */ 1232f7f4bccbSMingming Cao void jbd2_journal_update_superblock(journal_t *journal, int wait) 1233470decc6SDave Kleikamp { 1234470decc6SDave Kleikamp journal_superblock_t *sb = journal->j_superblock; 1235470decc6SDave Kleikamp struct buffer_head *bh = journal->j_sb_buffer; 1236470decc6SDave Kleikamp 1237470decc6SDave Kleikamp /* 1238470decc6SDave Kleikamp * As a special case, if the on-disk copy is already marked as needing 1239470decc6SDave Kleikamp * no recovery (s_start == 0) and there are no outstanding transactions 1240470decc6SDave Kleikamp * in the filesystem, then we can safely defer the superblock update 1241f7f4bccbSMingming Cao * until the next commit by setting JBD2_FLUSHED. This avoids 1242470decc6SDave Kleikamp * attempting a write to a potential-readonly device. 1243470decc6SDave Kleikamp */ 1244470decc6SDave Kleikamp if (sb->s_start == 0 && journal->j_tail_sequence == 1245470decc6SDave Kleikamp journal->j_transaction_sequence) { 1246470decc6SDave Kleikamp jbd_debug(1,"JBD: Skipping superblock update on recovered sb " 1247470decc6SDave Kleikamp "(start %ld, seq %d, errno %d)\n", 1248470decc6SDave Kleikamp journal->j_tail, journal->j_tail_sequence, 1249470decc6SDave Kleikamp journal->j_errno); 1250470decc6SDave Kleikamp goto out; 1251470decc6SDave Kleikamp } 1252470decc6SDave Kleikamp 1253470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 1254470decc6SDave Kleikamp jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n", 1255470decc6SDave Kleikamp journal->j_tail, journal->j_tail_sequence, journal->j_errno); 1256470decc6SDave Kleikamp 1257470decc6SDave Kleikamp sb->s_sequence = cpu_to_be32(journal->j_tail_sequence); 1258470decc6SDave Kleikamp sb->s_start = cpu_to_be32(journal->j_tail); 1259470decc6SDave Kleikamp sb->s_errno = cpu_to_be32(journal->j_errno); 1260470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1261470decc6SDave Kleikamp 1262470decc6SDave Kleikamp BUFFER_TRACE(bh, "marking dirty"); 1263470decc6SDave Kleikamp mark_buffer_dirty(bh); 1264470decc6SDave Kleikamp if (wait) 1265470decc6SDave Kleikamp sync_dirty_buffer(bh); 1266470decc6SDave Kleikamp else 1267470decc6SDave Kleikamp ll_rw_block(SWRITE, 1, &bh); 1268470decc6SDave Kleikamp 1269470decc6SDave Kleikamp out: 1270470decc6SDave Kleikamp /* If we have just flushed the log (by marking s_start==0), then 1271470decc6SDave Kleikamp * any future commit will have to be careful to update the 1272470decc6SDave Kleikamp * superblock again to re-record the true start of the log. */ 1273470decc6SDave Kleikamp 1274470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 1275470decc6SDave Kleikamp if (sb->s_start) 1276f7f4bccbSMingming Cao journal->j_flags &= ~JBD2_FLUSHED; 1277470decc6SDave Kleikamp else 1278f7f4bccbSMingming Cao journal->j_flags |= JBD2_FLUSHED; 1279470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1280470decc6SDave Kleikamp } 1281470decc6SDave Kleikamp 1282470decc6SDave Kleikamp /* 1283470decc6SDave Kleikamp * Read the superblock for a given journal, performing initial 1284470decc6SDave Kleikamp * validation of the format. 1285470decc6SDave Kleikamp */ 1286470decc6SDave Kleikamp 1287470decc6SDave Kleikamp static int journal_get_superblock(journal_t *journal) 1288470decc6SDave Kleikamp { 1289470decc6SDave Kleikamp struct buffer_head *bh; 1290470decc6SDave Kleikamp journal_superblock_t *sb; 1291470decc6SDave Kleikamp int err = -EIO; 1292470decc6SDave Kleikamp 1293470decc6SDave Kleikamp bh = journal->j_sb_buffer; 1294470decc6SDave Kleikamp 1295470decc6SDave Kleikamp J_ASSERT(bh != NULL); 1296470decc6SDave Kleikamp if (!buffer_uptodate(bh)) { 1297470decc6SDave Kleikamp ll_rw_block(READ, 1, &bh); 1298470decc6SDave Kleikamp wait_on_buffer(bh); 1299470decc6SDave Kleikamp if (!buffer_uptodate(bh)) { 1300470decc6SDave Kleikamp printk (KERN_ERR 1301470decc6SDave Kleikamp "JBD: IO error reading journal superblock\n"); 1302470decc6SDave Kleikamp goto out; 1303470decc6SDave Kleikamp } 1304470decc6SDave Kleikamp } 1305470decc6SDave Kleikamp 1306470decc6SDave Kleikamp sb = journal->j_superblock; 1307470decc6SDave Kleikamp 1308470decc6SDave Kleikamp err = -EINVAL; 1309470decc6SDave Kleikamp 1310f7f4bccbSMingming Cao if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) || 1311470decc6SDave Kleikamp sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) { 1312470decc6SDave Kleikamp printk(KERN_WARNING "JBD: no valid journal superblock found\n"); 1313470decc6SDave Kleikamp goto out; 1314470decc6SDave Kleikamp } 1315470decc6SDave Kleikamp 1316470decc6SDave Kleikamp switch(be32_to_cpu(sb->s_header.h_blocktype)) { 1317f7f4bccbSMingming Cao case JBD2_SUPERBLOCK_V1: 1318470decc6SDave Kleikamp journal->j_format_version = 1; 1319470decc6SDave Kleikamp break; 1320f7f4bccbSMingming Cao case JBD2_SUPERBLOCK_V2: 1321470decc6SDave Kleikamp journal->j_format_version = 2; 1322470decc6SDave Kleikamp break; 1323470decc6SDave Kleikamp default: 1324470decc6SDave Kleikamp printk(KERN_WARNING "JBD: unrecognised superblock format ID\n"); 1325470decc6SDave Kleikamp goto out; 1326470decc6SDave Kleikamp } 1327470decc6SDave Kleikamp 1328470decc6SDave Kleikamp if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen) 1329470decc6SDave Kleikamp journal->j_maxlen = be32_to_cpu(sb->s_maxlen); 1330470decc6SDave Kleikamp else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) { 1331470decc6SDave Kleikamp printk (KERN_WARNING "JBD: journal file too short\n"); 1332470decc6SDave Kleikamp goto out; 1333470decc6SDave Kleikamp } 1334470decc6SDave Kleikamp 1335470decc6SDave Kleikamp return 0; 1336470decc6SDave Kleikamp 1337470decc6SDave Kleikamp out: 1338470decc6SDave Kleikamp journal_fail_superblock(journal); 1339470decc6SDave Kleikamp return err; 1340470decc6SDave Kleikamp } 1341470decc6SDave Kleikamp 1342470decc6SDave Kleikamp /* 1343470decc6SDave Kleikamp * Load the on-disk journal superblock and read the key fields into the 1344470decc6SDave Kleikamp * journal_t. 1345470decc6SDave Kleikamp */ 1346470decc6SDave Kleikamp 1347470decc6SDave Kleikamp static int load_superblock(journal_t *journal) 1348470decc6SDave Kleikamp { 1349470decc6SDave Kleikamp int err; 1350470decc6SDave Kleikamp journal_superblock_t *sb; 1351470decc6SDave Kleikamp 1352470decc6SDave Kleikamp err = journal_get_superblock(journal); 1353470decc6SDave Kleikamp if (err) 1354470decc6SDave Kleikamp return err; 1355470decc6SDave Kleikamp 1356470decc6SDave Kleikamp sb = journal->j_superblock; 1357470decc6SDave Kleikamp 1358470decc6SDave Kleikamp journal->j_tail_sequence = be32_to_cpu(sb->s_sequence); 1359470decc6SDave Kleikamp journal->j_tail = be32_to_cpu(sb->s_start); 1360470decc6SDave Kleikamp journal->j_first = be32_to_cpu(sb->s_first); 1361470decc6SDave Kleikamp journal->j_last = be32_to_cpu(sb->s_maxlen); 1362470decc6SDave Kleikamp journal->j_errno = be32_to_cpu(sb->s_errno); 1363470decc6SDave Kleikamp 1364470decc6SDave Kleikamp return 0; 1365470decc6SDave Kleikamp } 1366470decc6SDave Kleikamp 1367470decc6SDave Kleikamp 1368470decc6SDave Kleikamp /** 1369f7f4bccbSMingming Cao * int jbd2_journal_load() - Read journal from disk. 1370470decc6SDave Kleikamp * @journal: Journal to act on. 1371470decc6SDave Kleikamp * 1372470decc6SDave Kleikamp * Given a journal_t structure which tells us which disk blocks contain 1373470decc6SDave Kleikamp * a journal, read the journal from disk to initialise the in-memory 1374470decc6SDave Kleikamp * structures. 1375470decc6SDave Kleikamp */ 1376f7f4bccbSMingming Cao int jbd2_journal_load(journal_t *journal) 1377470decc6SDave Kleikamp { 1378470decc6SDave Kleikamp int err; 1379470decc6SDave Kleikamp journal_superblock_t *sb; 1380470decc6SDave Kleikamp 1381470decc6SDave Kleikamp err = load_superblock(journal); 1382470decc6SDave Kleikamp if (err) 1383470decc6SDave Kleikamp return err; 1384470decc6SDave Kleikamp 1385470decc6SDave Kleikamp sb = journal->j_superblock; 1386470decc6SDave Kleikamp /* If this is a V2 superblock, then we have to check the 1387470decc6SDave Kleikamp * features flags on it. */ 1388470decc6SDave Kleikamp 1389470decc6SDave Kleikamp if (journal->j_format_version >= 2) { 1390470decc6SDave Kleikamp if ((sb->s_feature_ro_compat & 1391f7f4bccbSMingming Cao ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) || 1392470decc6SDave Kleikamp (sb->s_feature_incompat & 1393f7f4bccbSMingming Cao ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) { 1394470decc6SDave Kleikamp printk (KERN_WARNING 1395470decc6SDave Kleikamp "JBD: Unrecognised features on journal\n"); 1396470decc6SDave Kleikamp return -EINVAL; 1397470decc6SDave Kleikamp } 1398470decc6SDave Kleikamp } 1399470decc6SDave Kleikamp 1400470decc6SDave Kleikamp /* Let the recovery code check whether it needs to recover any 1401470decc6SDave Kleikamp * data from the journal. */ 1402f7f4bccbSMingming Cao if (jbd2_journal_recover(journal)) 1403470decc6SDave Kleikamp goto recovery_error; 1404470decc6SDave Kleikamp 1405470decc6SDave Kleikamp /* OK, we've finished with the dynamic journal bits: 1406470decc6SDave Kleikamp * reinitialise the dynamic contents of the superblock in memory 1407470decc6SDave Kleikamp * and reset them on disk. */ 1408470decc6SDave Kleikamp if (journal_reset(journal)) 1409470decc6SDave Kleikamp goto recovery_error; 1410470decc6SDave Kleikamp 1411f7f4bccbSMingming Cao journal->j_flags &= ~JBD2_ABORT; 1412f7f4bccbSMingming Cao journal->j_flags |= JBD2_LOADED; 1413470decc6SDave Kleikamp return 0; 1414470decc6SDave Kleikamp 1415470decc6SDave Kleikamp recovery_error: 1416470decc6SDave Kleikamp printk (KERN_WARNING "JBD: recovery failed\n"); 1417470decc6SDave Kleikamp return -EIO; 1418470decc6SDave Kleikamp } 1419470decc6SDave Kleikamp 1420470decc6SDave Kleikamp /** 1421f7f4bccbSMingming Cao * void jbd2_journal_destroy() - Release a journal_t structure. 1422470decc6SDave Kleikamp * @journal: Journal to act on. 1423470decc6SDave Kleikamp * 1424470decc6SDave Kleikamp * Release a journal_t structure once it is no longer in use by the 1425470decc6SDave Kleikamp * journaled object. 1426470decc6SDave Kleikamp */ 1427f7f4bccbSMingming Cao void jbd2_journal_destroy(journal_t *journal) 1428470decc6SDave Kleikamp { 1429470decc6SDave Kleikamp /* Wait for the commit thread to wake up and die. */ 1430470decc6SDave Kleikamp journal_kill_thread(journal); 1431470decc6SDave Kleikamp 1432470decc6SDave Kleikamp /* Force a final log commit */ 1433470decc6SDave Kleikamp if (journal->j_running_transaction) 1434f7f4bccbSMingming Cao jbd2_journal_commit_transaction(journal); 1435470decc6SDave Kleikamp 1436470decc6SDave Kleikamp /* Force any old transactions to disk */ 1437470decc6SDave Kleikamp 1438470decc6SDave Kleikamp /* Totally anal locking here... */ 1439470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 1440470decc6SDave Kleikamp while (journal->j_checkpoint_transactions != NULL) { 1441470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 1442f7f4bccbSMingming Cao jbd2_log_do_checkpoint(journal); 1443470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 1444470decc6SDave Kleikamp } 1445470decc6SDave Kleikamp 1446470decc6SDave Kleikamp J_ASSERT(journal->j_running_transaction == NULL); 1447470decc6SDave Kleikamp J_ASSERT(journal->j_committing_transaction == NULL); 1448470decc6SDave Kleikamp J_ASSERT(journal->j_checkpoint_transactions == NULL); 1449470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 1450470decc6SDave Kleikamp 1451470decc6SDave Kleikamp /* We can now mark the journal as empty. */ 1452470decc6SDave Kleikamp journal->j_tail = 0; 1453470decc6SDave Kleikamp journal->j_tail_sequence = ++journal->j_transaction_sequence; 1454470decc6SDave Kleikamp if (journal->j_sb_buffer) { 1455f7f4bccbSMingming Cao jbd2_journal_update_superblock(journal, 1); 1456470decc6SDave Kleikamp brelse(journal->j_sb_buffer); 1457470decc6SDave Kleikamp } 1458470decc6SDave Kleikamp 14598e85fb3fSJohann Lombardi if (journal->j_proc_entry) 14608e85fb3fSJohann Lombardi jbd2_stats_proc_exit(journal); 1461470decc6SDave Kleikamp if (journal->j_inode) 1462470decc6SDave Kleikamp iput(journal->j_inode); 1463470decc6SDave Kleikamp if (journal->j_revoke) 1464f7f4bccbSMingming Cao jbd2_journal_destroy_revoke(journal); 1465470decc6SDave Kleikamp kfree(journal->j_wbuf); 1466470decc6SDave Kleikamp kfree(journal); 1467470decc6SDave Kleikamp } 1468470decc6SDave Kleikamp 1469470decc6SDave Kleikamp 1470470decc6SDave Kleikamp /** 1471f7f4bccbSMingming Cao *int jbd2_journal_check_used_features () - Check if features specified are used. 1472470decc6SDave Kleikamp * @journal: Journal to check. 1473470decc6SDave Kleikamp * @compat: bitmask of compatible features 1474470decc6SDave Kleikamp * @ro: bitmask of features that force read-only mount 1475470decc6SDave Kleikamp * @incompat: bitmask of incompatible features 1476470decc6SDave Kleikamp * 1477470decc6SDave Kleikamp * Check whether the journal uses all of a given set of 1478470decc6SDave Kleikamp * features. Return true (non-zero) if it does. 1479470decc6SDave Kleikamp **/ 1480470decc6SDave Kleikamp 1481f7f4bccbSMingming Cao int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat, 1482470decc6SDave Kleikamp unsigned long ro, unsigned long incompat) 1483470decc6SDave Kleikamp { 1484470decc6SDave Kleikamp journal_superblock_t *sb; 1485470decc6SDave Kleikamp 1486470decc6SDave Kleikamp if (!compat && !ro && !incompat) 1487470decc6SDave Kleikamp return 1; 1488470decc6SDave Kleikamp if (journal->j_format_version == 1) 1489470decc6SDave Kleikamp return 0; 1490470decc6SDave Kleikamp 1491470decc6SDave Kleikamp sb = journal->j_superblock; 1492470decc6SDave Kleikamp 1493470decc6SDave Kleikamp if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) && 1494470decc6SDave Kleikamp ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) && 1495470decc6SDave Kleikamp ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat)) 1496470decc6SDave Kleikamp return 1; 1497470decc6SDave Kleikamp 1498470decc6SDave Kleikamp return 0; 1499470decc6SDave Kleikamp } 1500470decc6SDave Kleikamp 1501470decc6SDave Kleikamp /** 1502f7f4bccbSMingming Cao * int jbd2_journal_check_available_features() - Check feature set in journalling layer 1503470decc6SDave Kleikamp * @journal: Journal to check. 1504470decc6SDave Kleikamp * @compat: bitmask of compatible features 1505470decc6SDave Kleikamp * @ro: bitmask of features that force read-only mount 1506470decc6SDave Kleikamp * @incompat: bitmask of incompatible features 1507470decc6SDave Kleikamp * 1508470decc6SDave Kleikamp * Check whether the journaling code supports the use of 1509470decc6SDave Kleikamp * all of a given set of features on this journal. Return true 1510470decc6SDave Kleikamp * (non-zero) if it can. */ 1511470decc6SDave Kleikamp 1512f7f4bccbSMingming Cao int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat, 1513470decc6SDave Kleikamp unsigned long ro, unsigned long incompat) 1514470decc6SDave Kleikamp { 1515470decc6SDave Kleikamp journal_superblock_t *sb; 1516470decc6SDave Kleikamp 1517470decc6SDave Kleikamp if (!compat && !ro && !incompat) 1518470decc6SDave Kleikamp return 1; 1519470decc6SDave Kleikamp 1520470decc6SDave Kleikamp sb = journal->j_superblock; 1521470decc6SDave Kleikamp 1522470decc6SDave Kleikamp /* We can support any known requested features iff the 1523470decc6SDave Kleikamp * superblock is in version 2. Otherwise we fail to support any 1524470decc6SDave Kleikamp * extended sb features. */ 1525470decc6SDave Kleikamp 1526470decc6SDave Kleikamp if (journal->j_format_version != 2) 1527470decc6SDave Kleikamp return 0; 1528470decc6SDave Kleikamp 1529f7f4bccbSMingming Cao if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat && 1530f7f4bccbSMingming Cao (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro && 1531f7f4bccbSMingming Cao (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat) 1532470decc6SDave Kleikamp return 1; 1533470decc6SDave Kleikamp 1534470decc6SDave Kleikamp return 0; 1535470decc6SDave Kleikamp } 1536470decc6SDave Kleikamp 1537470decc6SDave Kleikamp /** 1538f7f4bccbSMingming Cao * int jbd2_journal_set_features () - Mark a given journal feature in the superblock 1539470decc6SDave Kleikamp * @journal: Journal to act on. 1540470decc6SDave Kleikamp * @compat: bitmask of compatible features 1541470decc6SDave Kleikamp * @ro: bitmask of features that force read-only mount 1542470decc6SDave Kleikamp * @incompat: bitmask of incompatible features 1543470decc6SDave Kleikamp * 1544470decc6SDave Kleikamp * Mark a given journal feature as present on the 1545470decc6SDave Kleikamp * superblock. Returns true if the requested features could be set. 1546470decc6SDave Kleikamp * 1547470decc6SDave Kleikamp */ 1548470decc6SDave Kleikamp 1549f7f4bccbSMingming Cao int jbd2_journal_set_features (journal_t *journal, unsigned long compat, 1550470decc6SDave Kleikamp unsigned long ro, unsigned long incompat) 1551470decc6SDave Kleikamp { 1552470decc6SDave Kleikamp journal_superblock_t *sb; 1553470decc6SDave Kleikamp 1554f7f4bccbSMingming Cao if (jbd2_journal_check_used_features(journal, compat, ro, incompat)) 1555470decc6SDave Kleikamp return 1; 1556470decc6SDave Kleikamp 1557f7f4bccbSMingming Cao if (!jbd2_journal_check_available_features(journal, compat, ro, incompat)) 1558470decc6SDave Kleikamp return 0; 1559470decc6SDave Kleikamp 1560470decc6SDave Kleikamp jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n", 1561470decc6SDave Kleikamp compat, ro, incompat); 1562470decc6SDave Kleikamp 1563470decc6SDave Kleikamp sb = journal->j_superblock; 1564470decc6SDave Kleikamp 1565470decc6SDave Kleikamp sb->s_feature_compat |= cpu_to_be32(compat); 1566470decc6SDave Kleikamp sb->s_feature_ro_compat |= cpu_to_be32(ro); 1567470decc6SDave Kleikamp sb->s_feature_incompat |= cpu_to_be32(incompat); 1568470decc6SDave Kleikamp 1569470decc6SDave Kleikamp return 1; 1570470decc6SDave Kleikamp } 1571470decc6SDave Kleikamp 1572818d276cSGirish Shilamkar /* 1573818d276cSGirish Shilamkar * jbd2_journal_clear_features () - Clear a given journal feature in the 1574818d276cSGirish Shilamkar * superblock 1575818d276cSGirish Shilamkar * @journal: Journal to act on. 1576818d276cSGirish Shilamkar * @compat: bitmask of compatible features 1577818d276cSGirish Shilamkar * @ro: bitmask of features that force read-only mount 1578818d276cSGirish Shilamkar * @incompat: bitmask of incompatible features 1579818d276cSGirish Shilamkar * 1580818d276cSGirish Shilamkar * Clear a given journal feature as present on the 1581818d276cSGirish Shilamkar * superblock. 1582818d276cSGirish Shilamkar */ 1583818d276cSGirish Shilamkar void jbd2_journal_clear_features(journal_t *journal, unsigned long compat, 1584818d276cSGirish Shilamkar unsigned long ro, unsigned long incompat) 1585818d276cSGirish Shilamkar { 1586818d276cSGirish Shilamkar journal_superblock_t *sb; 1587818d276cSGirish Shilamkar 1588818d276cSGirish Shilamkar jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n", 1589818d276cSGirish Shilamkar compat, ro, incompat); 1590818d276cSGirish Shilamkar 1591818d276cSGirish Shilamkar sb = journal->j_superblock; 1592818d276cSGirish Shilamkar 1593818d276cSGirish Shilamkar sb->s_feature_compat &= ~cpu_to_be32(compat); 1594818d276cSGirish Shilamkar sb->s_feature_ro_compat &= ~cpu_to_be32(ro); 1595818d276cSGirish Shilamkar sb->s_feature_incompat &= ~cpu_to_be32(incompat); 1596818d276cSGirish Shilamkar } 1597818d276cSGirish Shilamkar EXPORT_SYMBOL(jbd2_journal_clear_features); 1598470decc6SDave Kleikamp 1599470decc6SDave Kleikamp /** 1600f7f4bccbSMingming Cao * int jbd2_journal_update_format () - Update on-disk journal structure. 1601470decc6SDave Kleikamp * @journal: Journal to act on. 1602470decc6SDave Kleikamp * 1603470decc6SDave Kleikamp * Given an initialised but unloaded journal struct, poke about in the 1604470decc6SDave Kleikamp * on-disk structure to update it to the most recent supported version. 1605470decc6SDave Kleikamp */ 1606f7f4bccbSMingming Cao int jbd2_journal_update_format (journal_t *journal) 1607470decc6SDave Kleikamp { 1608470decc6SDave Kleikamp journal_superblock_t *sb; 1609470decc6SDave Kleikamp int err; 1610470decc6SDave Kleikamp 1611470decc6SDave Kleikamp err = journal_get_superblock(journal); 1612470decc6SDave Kleikamp if (err) 1613470decc6SDave Kleikamp return err; 1614470decc6SDave Kleikamp 1615470decc6SDave Kleikamp sb = journal->j_superblock; 1616470decc6SDave Kleikamp 1617470decc6SDave Kleikamp switch (be32_to_cpu(sb->s_header.h_blocktype)) { 1618f7f4bccbSMingming Cao case JBD2_SUPERBLOCK_V2: 1619470decc6SDave Kleikamp return 0; 1620f7f4bccbSMingming Cao case JBD2_SUPERBLOCK_V1: 1621470decc6SDave Kleikamp return journal_convert_superblock_v1(journal, sb); 1622470decc6SDave Kleikamp default: 1623470decc6SDave Kleikamp break; 1624470decc6SDave Kleikamp } 1625470decc6SDave Kleikamp return -EINVAL; 1626470decc6SDave Kleikamp } 1627470decc6SDave Kleikamp 1628470decc6SDave Kleikamp static int journal_convert_superblock_v1(journal_t *journal, 1629470decc6SDave Kleikamp journal_superblock_t *sb) 1630470decc6SDave Kleikamp { 1631470decc6SDave Kleikamp int offset, blocksize; 1632470decc6SDave Kleikamp struct buffer_head *bh; 1633470decc6SDave Kleikamp 1634470decc6SDave Kleikamp printk(KERN_WARNING 1635470decc6SDave Kleikamp "JBD: Converting superblock from version 1 to 2.\n"); 1636470decc6SDave Kleikamp 1637470decc6SDave Kleikamp /* Pre-initialise new fields to zero */ 1638470decc6SDave Kleikamp offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb); 1639470decc6SDave Kleikamp blocksize = be32_to_cpu(sb->s_blocksize); 1640470decc6SDave Kleikamp memset(&sb->s_feature_compat, 0, blocksize-offset); 1641470decc6SDave Kleikamp 1642470decc6SDave Kleikamp sb->s_nr_users = cpu_to_be32(1); 1643f7f4bccbSMingming Cao sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2); 1644470decc6SDave Kleikamp journal->j_format_version = 2; 1645470decc6SDave Kleikamp 1646470decc6SDave Kleikamp bh = journal->j_sb_buffer; 1647470decc6SDave Kleikamp BUFFER_TRACE(bh, "marking dirty"); 1648470decc6SDave Kleikamp mark_buffer_dirty(bh); 1649470decc6SDave Kleikamp sync_dirty_buffer(bh); 1650470decc6SDave Kleikamp return 0; 1651470decc6SDave Kleikamp } 1652470decc6SDave Kleikamp 1653470decc6SDave Kleikamp 1654470decc6SDave Kleikamp /** 1655f7f4bccbSMingming Cao * int jbd2_journal_flush () - Flush journal 1656470decc6SDave Kleikamp * @journal: Journal to act on. 1657470decc6SDave Kleikamp * 1658470decc6SDave Kleikamp * Flush all data for a given journal to disk and empty the journal. 1659470decc6SDave Kleikamp * Filesystems can use this when remounting readonly to ensure that 1660470decc6SDave Kleikamp * recovery does not need to happen on remount. 1661470decc6SDave Kleikamp */ 1662470decc6SDave Kleikamp 1663f7f4bccbSMingming Cao int jbd2_journal_flush(journal_t *journal) 1664470decc6SDave Kleikamp { 1665470decc6SDave Kleikamp int err = 0; 1666470decc6SDave Kleikamp transaction_t *transaction = NULL; 1667470decc6SDave Kleikamp unsigned long old_tail; 1668470decc6SDave Kleikamp 1669470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 1670470decc6SDave Kleikamp 1671470decc6SDave Kleikamp /* Force everything buffered to the log... */ 1672470decc6SDave Kleikamp if (journal->j_running_transaction) { 1673470decc6SDave Kleikamp transaction = journal->j_running_transaction; 1674f7f4bccbSMingming Cao __jbd2_log_start_commit(journal, transaction->t_tid); 1675470decc6SDave Kleikamp } else if (journal->j_committing_transaction) 1676470decc6SDave Kleikamp transaction = journal->j_committing_transaction; 1677470decc6SDave Kleikamp 1678470decc6SDave Kleikamp /* Wait for the log commit to complete... */ 1679470decc6SDave Kleikamp if (transaction) { 1680470decc6SDave Kleikamp tid_t tid = transaction->t_tid; 1681470decc6SDave Kleikamp 1682470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1683f7f4bccbSMingming Cao jbd2_log_wait_commit(journal, tid); 1684470decc6SDave Kleikamp } else { 1685470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1686470decc6SDave Kleikamp } 1687470decc6SDave Kleikamp 1688470decc6SDave Kleikamp /* ...and flush everything in the log out to disk. */ 1689470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 1690470decc6SDave Kleikamp while (!err && journal->j_checkpoint_transactions != NULL) { 1691470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 1692f7f4bccbSMingming Cao err = jbd2_log_do_checkpoint(journal); 1693470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 1694470decc6SDave Kleikamp } 1695470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 1696f7f4bccbSMingming Cao jbd2_cleanup_journal_tail(journal); 1697470decc6SDave Kleikamp 1698470decc6SDave Kleikamp /* Finally, mark the journal as really needing no recovery. 1699470decc6SDave Kleikamp * This sets s_start==0 in the underlying superblock, which is 1700470decc6SDave Kleikamp * the magic code for a fully-recovered superblock. Any future 1701470decc6SDave Kleikamp * commits of data to the journal will restore the current 1702470decc6SDave Kleikamp * s_start value. */ 1703470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 1704470decc6SDave Kleikamp old_tail = journal->j_tail; 1705470decc6SDave Kleikamp journal->j_tail = 0; 1706470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1707f7f4bccbSMingming Cao jbd2_journal_update_superblock(journal, 1); 1708470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 1709470decc6SDave Kleikamp journal->j_tail = old_tail; 1710470decc6SDave Kleikamp 1711470decc6SDave Kleikamp J_ASSERT(!journal->j_running_transaction); 1712470decc6SDave Kleikamp J_ASSERT(!journal->j_committing_transaction); 1713470decc6SDave Kleikamp J_ASSERT(!journal->j_checkpoint_transactions); 1714470decc6SDave Kleikamp J_ASSERT(journal->j_head == journal->j_tail); 1715470decc6SDave Kleikamp J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence); 1716470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1717470decc6SDave Kleikamp return err; 1718470decc6SDave Kleikamp } 1719470decc6SDave Kleikamp 1720470decc6SDave Kleikamp /** 1721f7f4bccbSMingming Cao * int jbd2_journal_wipe() - Wipe journal contents 1722470decc6SDave Kleikamp * @journal: Journal to act on. 1723470decc6SDave Kleikamp * @write: flag (see below) 1724470decc6SDave Kleikamp * 1725470decc6SDave Kleikamp * Wipe out all of the contents of a journal, safely. This will produce 1726470decc6SDave Kleikamp * a warning if the journal contains any valid recovery information. 1727f7f4bccbSMingming Cao * Must be called between journal_init_*() and jbd2_journal_load(). 1728470decc6SDave Kleikamp * 1729470decc6SDave Kleikamp * If 'write' is non-zero, then we wipe out the journal on disk; otherwise 1730470decc6SDave Kleikamp * we merely suppress recovery. 1731470decc6SDave Kleikamp */ 1732470decc6SDave Kleikamp 1733f7f4bccbSMingming Cao int jbd2_journal_wipe(journal_t *journal, int write) 1734470decc6SDave Kleikamp { 1735470decc6SDave Kleikamp journal_superblock_t *sb; 1736470decc6SDave Kleikamp int err = 0; 1737470decc6SDave Kleikamp 1738f7f4bccbSMingming Cao J_ASSERT (!(journal->j_flags & JBD2_LOADED)); 1739470decc6SDave Kleikamp 1740470decc6SDave Kleikamp err = load_superblock(journal); 1741470decc6SDave Kleikamp if (err) 1742470decc6SDave Kleikamp return err; 1743470decc6SDave Kleikamp 1744470decc6SDave Kleikamp sb = journal->j_superblock; 1745470decc6SDave Kleikamp 1746470decc6SDave Kleikamp if (!journal->j_tail) 1747470decc6SDave Kleikamp goto no_recovery; 1748470decc6SDave Kleikamp 1749470decc6SDave Kleikamp printk (KERN_WARNING "JBD: %s recovery information on journal\n", 1750470decc6SDave Kleikamp write ? "Clearing" : "Ignoring"); 1751470decc6SDave Kleikamp 1752f7f4bccbSMingming Cao err = jbd2_journal_skip_recovery(journal); 1753470decc6SDave Kleikamp if (write) 1754f7f4bccbSMingming Cao jbd2_journal_update_superblock(journal, 1); 1755470decc6SDave Kleikamp 1756470decc6SDave Kleikamp no_recovery: 1757470decc6SDave Kleikamp return err; 1758470decc6SDave Kleikamp } 1759470decc6SDave Kleikamp 1760470decc6SDave Kleikamp /* 1761470decc6SDave Kleikamp * journal_dev_name: format a character string to describe on what 1762470decc6SDave Kleikamp * device this journal is present. 1763470decc6SDave Kleikamp */ 1764470decc6SDave Kleikamp 1765470decc6SDave Kleikamp static const char *journal_dev_name(journal_t *journal, char *buffer) 1766470decc6SDave Kleikamp { 1767470decc6SDave Kleikamp struct block_device *bdev; 1768470decc6SDave Kleikamp 1769470decc6SDave Kleikamp if (journal->j_inode) 1770470decc6SDave Kleikamp bdev = journal->j_inode->i_sb->s_bdev; 1771470decc6SDave Kleikamp else 1772470decc6SDave Kleikamp bdev = journal->j_dev; 1773470decc6SDave Kleikamp 1774470decc6SDave Kleikamp return bdevname(bdev, buffer); 1775470decc6SDave Kleikamp } 1776470decc6SDave Kleikamp 1777470decc6SDave Kleikamp /* 1778470decc6SDave Kleikamp * Journal abort has very specific semantics, which we describe 1779470decc6SDave Kleikamp * for journal abort. 1780470decc6SDave Kleikamp * 1781470decc6SDave Kleikamp * Two internal function, which provide abort to te jbd layer 1782470decc6SDave Kleikamp * itself are here. 1783470decc6SDave Kleikamp */ 1784470decc6SDave Kleikamp 1785470decc6SDave Kleikamp /* 1786470decc6SDave Kleikamp * Quick version for internal journal use (doesn't lock the journal). 1787470decc6SDave Kleikamp * Aborts hard --- we mark the abort as occurred, but do _nothing_ else, 1788470decc6SDave Kleikamp * and don't attempt to make any other journal updates. 1789470decc6SDave Kleikamp */ 1790f7f4bccbSMingming Cao void __jbd2_journal_abort_hard(journal_t *journal) 1791470decc6SDave Kleikamp { 1792470decc6SDave Kleikamp transaction_t *transaction; 1793470decc6SDave Kleikamp char b[BDEVNAME_SIZE]; 1794470decc6SDave Kleikamp 1795f7f4bccbSMingming Cao if (journal->j_flags & JBD2_ABORT) 1796470decc6SDave Kleikamp return; 1797470decc6SDave Kleikamp 1798470decc6SDave Kleikamp printk(KERN_ERR "Aborting journal on device %s.\n", 1799470decc6SDave Kleikamp journal_dev_name(journal, b)); 1800470decc6SDave Kleikamp 1801470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 1802f7f4bccbSMingming Cao journal->j_flags |= JBD2_ABORT; 1803470decc6SDave Kleikamp transaction = journal->j_running_transaction; 1804470decc6SDave Kleikamp if (transaction) 1805f7f4bccbSMingming Cao __jbd2_log_start_commit(journal, transaction->t_tid); 1806470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1807470decc6SDave Kleikamp } 1808470decc6SDave Kleikamp 1809470decc6SDave Kleikamp /* Soft abort: record the abort error status in the journal superblock, 1810470decc6SDave Kleikamp * but don't do any other IO. */ 1811470decc6SDave Kleikamp static void __journal_abort_soft (journal_t *journal, int errno) 1812470decc6SDave Kleikamp { 1813f7f4bccbSMingming Cao if (journal->j_flags & JBD2_ABORT) 1814470decc6SDave Kleikamp return; 1815470decc6SDave Kleikamp 1816470decc6SDave Kleikamp if (!journal->j_errno) 1817470decc6SDave Kleikamp journal->j_errno = errno; 1818470decc6SDave Kleikamp 1819f7f4bccbSMingming Cao __jbd2_journal_abort_hard(journal); 1820470decc6SDave Kleikamp 1821470decc6SDave Kleikamp if (errno) 1822f7f4bccbSMingming Cao jbd2_journal_update_superblock(journal, 1); 1823470decc6SDave Kleikamp } 1824470decc6SDave Kleikamp 1825470decc6SDave Kleikamp /** 1826f7f4bccbSMingming Cao * void jbd2_journal_abort () - Shutdown the journal immediately. 1827470decc6SDave Kleikamp * @journal: the journal to shutdown. 1828470decc6SDave Kleikamp * @errno: an error number to record in the journal indicating 1829470decc6SDave Kleikamp * the reason for the shutdown. 1830470decc6SDave Kleikamp * 1831470decc6SDave Kleikamp * Perform a complete, immediate shutdown of the ENTIRE 1832470decc6SDave Kleikamp * journal (not of a single transaction). This operation cannot be 1833470decc6SDave Kleikamp * undone without closing and reopening the journal. 1834470decc6SDave Kleikamp * 1835f7f4bccbSMingming Cao * The jbd2_journal_abort function is intended to support higher level error 1836470decc6SDave Kleikamp * recovery mechanisms such as the ext2/ext3 remount-readonly error 1837470decc6SDave Kleikamp * mode. 1838470decc6SDave Kleikamp * 1839470decc6SDave Kleikamp * Journal abort has very specific semantics. Any existing dirty, 1840470decc6SDave Kleikamp * unjournaled buffers in the main filesystem will still be written to 1841470decc6SDave Kleikamp * disk by bdflush, but the journaling mechanism will be suspended 1842470decc6SDave Kleikamp * immediately and no further transaction commits will be honoured. 1843470decc6SDave Kleikamp * 1844470decc6SDave Kleikamp * Any dirty, journaled buffers will be written back to disk without 1845470decc6SDave Kleikamp * hitting the journal. Atomicity cannot be guaranteed on an aborted 1846470decc6SDave Kleikamp * filesystem, but we _do_ attempt to leave as much data as possible 1847470decc6SDave Kleikamp * behind for fsck to use for cleanup. 1848470decc6SDave Kleikamp * 1849470decc6SDave Kleikamp * Any attempt to get a new transaction handle on a journal which is in 1850470decc6SDave Kleikamp * ABORT state will just result in an -EROFS error return. A 1851f7f4bccbSMingming Cao * jbd2_journal_stop on an existing handle will return -EIO if we have 1852470decc6SDave Kleikamp * entered abort state during the update. 1853470decc6SDave Kleikamp * 1854470decc6SDave Kleikamp * Recursive transactions are not disturbed by journal abort until the 1855f7f4bccbSMingming Cao * final jbd2_journal_stop, which will receive the -EIO error. 1856470decc6SDave Kleikamp * 1857f7f4bccbSMingming Cao * Finally, the jbd2_journal_abort call allows the caller to supply an errno 1858470decc6SDave Kleikamp * which will be recorded (if possible) in the journal superblock. This 1859470decc6SDave Kleikamp * allows a client to record failure conditions in the middle of a 1860470decc6SDave Kleikamp * transaction without having to complete the transaction to record the 1861470decc6SDave Kleikamp * failure to disk. ext3_error, for example, now uses this 1862470decc6SDave Kleikamp * functionality. 1863470decc6SDave Kleikamp * 1864470decc6SDave Kleikamp * Errors which originate from within the journaling layer will NOT 1865470decc6SDave Kleikamp * supply an errno; a null errno implies that absolutely no further 1866470decc6SDave Kleikamp * writes are done to the journal (unless there are any already in 1867470decc6SDave Kleikamp * progress). 1868470decc6SDave Kleikamp * 1869470decc6SDave Kleikamp */ 1870470decc6SDave Kleikamp 1871f7f4bccbSMingming Cao void jbd2_journal_abort(journal_t *journal, int errno) 1872470decc6SDave Kleikamp { 1873470decc6SDave Kleikamp __journal_abort_soft(journal, errno); 1874470decc6SDave Kleikamp } 1875470decc6SDave Kleikamp 1876470decc6SDave Kleikamp /** 1877f7f4bccbSMingming Cao * int jbd2_journal_errno () - returns the journal's error state. 1878470decc6SDave Kleikamp * @journal: journal to examine. 1879470decc6SDave Kleikamp * 1880f7f4bccbSMingming Cao * This is the errno numbet set with jbd2_journal_abort(), the last 1881470decc6SDave Kleikamp * time the journal was mounted - if the journal was stopped 1882470decc6SDave Kleikamp * without calling abort this will be 0. 1883470decc6SDave Kleikamp * 1884470decc6SDave Kleikamp * If the journal has been aborted on this mount time -EROFS will 1885470decc6SDave Kleikamp * be returned. 1886470decc6SDave Kleikamp */ 1887f7f4bccbSMingming Cao int jbd2_journal_errno(journal_t *journal) 1888470decc6SDave Kleikamp { 1889470decc6SDave Kleikamp int err; 1890470decc6SDave Kleikamp 1891470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 1892f7f4bccbSMingming Cao if (journal->j_flags & JBD2_ABORT) 1893470decc6SDave Kleikamp err = -EROFS; 1894470decc6SDave Kleikamp else 1895470decc6SDave Kleikamp err = journal->j_errno; 1896470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1897470decc6SDave Kleikamp return err; 1898470decc6SDave Kleikamp } 1899470decc6SDave Kleikamp 1900470decc6SDave Kleikamp /** 1901f7f4bccbSMingming Cao * int jbd2_journal_clear_err () - clears the journal's error state 1902470decc6SDave Kleikamp * @journal: journal to act on. 1903470decc6SDave Kleikamp * 1904470decc6SDave Kleikamp * An error must be cleared or Acked to take a FS out of readonly 1905470decc6SDave Kleikamp * mode. 1906470decc6SDave Kleikamp */ 1907f7f4bccbSMingming Cao int jbd2_journal_clear_err(journal_t *journal) 1908470decc6SDave Kleikamp { 1909470decc6SDave Kleikamp int err = 0; 1910470decc6SDave Kleikamp 1911470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 1912f7f4bccbSMingming Cao if (journal->j_flags & JBD2_ABORT) 1913470decc6SDave Kleikamp err = -EROFS; 1914470decc6SDave Kleikamp else 1915470decc6SDave Kleikamp journal->j_errno = 0; 1916470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1917470decc6SDave Kleikamp return err; 1918470decc6SDave Kleikamp } 1919470decc6SDave Kleikamp 1920470decc6SDave Kleikamp /** 1921f7f4bccbSMingming Cao * void jbd2_journal_ack_err() - Ack journal err. 1922470decc6SDave Kleikamp * @journal: journal to act on. 1923470decc6SDave Kleikamp * 1924470decc6SDave Kleikamp * An error must be cleared or Acked to take a FS out of readonly 1925470decc6SDave Kleikamp * mode. 1926470decc6SDave Kleikamp */ 1927f7f4bccbSMingming Cao void jbd2_journal_ack_err(journal_t *journal) 1928470decc6SDave Kleikamp { 1929470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 1930470decc6SDave Kleikamp if (journal->j_errno) 1931f7f4bccbSMingming Cao journal->j_flags |= JBD2_ACK_ERR; 1932470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1933470decc6SDave Kleikamp } 1934470decc6SDave Kleikamp 1935f7f4bccbSMingming Cao int jbd2_journal_blocks_per_page(struct inode *inode) 1936470decc6SDave Kleikamp { 1937470decc6SDave Kleikamp return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); 1938470decc6SDave Kleikamp } 1939470decc6SDave Kleikamp 1940470decc6SDave Kleikamp /* 1941b517bea1SZach Brown * helper functions to deal with 32 or 64bit block numbers. 1942b517bea1SZach Brown */ 1943b517bea1SZach Brown size_t journal_tag_bytes(journal_t *journal) 1944b517bea1SZach Brown { 1945b517bea1SZach Brown if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT)) 1946cd02ff0bSMingming Cao return JBD2_TAG_SIZE64; 1947b517bea1SZach Brown else 1948cd02ff0bSMingming Cao return JBD2_TAG_SIZE32; 1949b517bea1SZach Brown } 1950b517bea1SZach Brown 1951b517bea1SZach Brown /* 1952470decc6SDave Kleikamp * Journal_head storage management 1953470decc6SDave Kleikamp */ 1954e18b890bSChristoph Lameter static struct kmem_cache *jbd2_journal_head_cache; 1955e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 1956470decc6SDave Kleikamp static atomic_t nr_journal_heads = ATOMIC_INIT(0); 1957470decc6SDave Kleikamp #endif 1958470decc6SDave Kleikamp 1959f7f4bccbSMingming Cao static int journal_init_jbd2_journal_head_cache(void) 1960470decc6SDave Kleikamp { 1961470decc6SDave Kleikamp int retval; 1962470decc6SDave Kleikamp 19631076d17aSAl Viro J_ASSERT(jbd2_journal_head_cache == NULL); 1964a920e941SJohann Lombardi jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head", 1965470decc6SDave Kleikamp sizeof(struct journal_head), 1966470decc6SDave Kleikamp 0, /* offset */ 196777160957SMingming Cao SLAB_TEMPORARY, /* flags */ 196820c2df83SPaul Mundt NULL); /* ctor */ 1969470decc6SDave Kleikamp retval = 0; 19701076d17aSAl Viro if (!jbd2_journal_head_cache) { 1971470decc6SDave Kleikamp retval = -ENOMEM; 1972470decc6SDave Kleikamp printk(KERN_EMERG "JBD: no memory for journal_head cache\n"); 1973470decc6SDave Kleikamp } 1974470decc6SDave Kleikamp return retval; 1975470decc6SDave Kleikamp } 1976470decc6SDave Kleikamp 1977f7f4bccbSMingming Cao static void jbd2_journal_destroy_jbd2_journal_head_cache(void) 1978470decc6SDave Kleikamp { 19798a9362ebSDuane Griffin if (jbd2_journal_head_cache) { 1980f7f4bccbSMingming Cao kmem_cache_destroy(jbd2_journal_head_cache); 1981f7f4bccbSMingming Cao jbd2_journal_head_cache = NULL; 1982470decc6SDave Kleikamp } 19838a9362ebSDuane Griffin } 1984470decc6SDave Kleikamp 1985470decc6SDave Kleikamp /* 1986470decc6SDave Kleikamp * journal_head splicing and dicing 1987470decc6SDave Kleikamp */ 1988470decc6SDave Kleikamp static struct journal_head *journal_alloc_journal_head(void) 1989470decc6SDave Kleikamp { 1990470decc6SDave Kleikamp struct journal_head *ret; 1991470decc6SDave Kleikamp static unsigned long last_warning; 1992470decc6SDave Kleikamp 1993e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 1994470decc6SDave Kleikamp atomic_inc(&nr_journal_heads); 1995470decc6SDave Kleikamp #endif 1996f7f4bccbSMingming Cao ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS); 19971076d17aSAl Viro if (!ret) { 1998470decc6SDave Kleikamp jbd_debug(1, "out of memory for journal_head\n"); 1999470decc6SDave Kleikamp if (time_after(jiffies, last_warning + 5*HZ)) { 2000470decc6SDave Kleikamp printk(KERN_NOTICE "ENOMEM in %s, retrying.\n", 2001470decc6SDave Kleikamp __FUNCTION__); 2002470decc6SDave Kleikamp last_warning = jiffies; 2003470decc6SDave Kleikamp } 20041076d17aSAl Viro while (!ret) { 2005470decc6SDave Kleikamp yield(); 2006f7f4bccbSMingming Cao ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS); 2007470decc6SDave Kleikamp } 2008470decc6SDave Kleikamp } 2009470decc6SDave Kleikamp return ret; 2010470decc6SDave Kleikamp } 2011470decc6SDave Kleikamp 2012470decc6SDave Kleikamp static void journal_free_journal_head(struct journal_head *jh) 2013470decc6SDave Kleikamp { 2014e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 2015470decc6SDave Kleikamp atomic_dec(&nr_journal_heads); 2016cd02ff0bSMingming Cao memset(jh, JBD2_POISON_FREE, sizeof(*jh)); 2017470decc6SDave Kleikamp #endif 2018f7f4bccbSMingming Cao kmem_cache_free(jbd2_journal_head_cache, jh); 2019470decc6SDave Kleikamp } 2020470decc6SDave Kleikamp 2021470decc6SDave Kleikamp /* 2022470decc6SDave Kleikamp * A journal_head is attached to a buffer_head whenever JBD has an 2023470decc6SDave Kleikamp * interest in the buffer. 2024470decc6SDave Kleikamp * 2025470decc6SDave Kleikamp * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit 2026470decc6SDave Kleikamp * is set. This bit is tested in core kernel code where we need to take 2027470decc6SDave Kleikamp * JBD-specific actions. Testing the zeroness of ->b_private is not reliable 2028470decc6SDave Kleikamp * there. 2029470decc6SDave Kleikamp * 2030470decc6SDave Kleikamp * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one. 2031470decc6SDave Kleikamp * 2032470decc6SDave Kleikamp * When a buffer has its BH_JBD bit set it is immune from being released by 2033470decc6SDave Kleikamp * core kernel code, mainly via ->b_count. 2034470decc6SDave Kleikamp * 2035470decc6SDave Kleikamp * A journal_head may be detached from its buffer_head when the journal_head's 2036470decc6SDave Kleikamp * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL. 2037f7f4bccbSMingming Cao * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the 2038470decc6SDave Kleikamp * journal_head can be dropped if needed. 2039470decc6SDave Kleikamp * 2040470decc6SDave Kleikamp * Various places in the kernel want to attach a journal_head to a buffer_head 2041470decc6SDave Kleikamp * _before_ attaching the journal_head to a transaction. To protect the 2042f7f4bccbSMingming Cao * journal_head in this situation, jbd2_journal_add_journal_head elevates the 2043470decc6SDave Kleikamp * journal_head's b_jcount refcount by one. The caller must call 2044f7f4bccbSMingming Cao * jbd2_journal_put_journal_head() to undo this. 2045470decc6SDave Kleikamp * 2046470decc6SDave Kleikamp * So the typical usage would be: 2047470decc6SDave Kleikamp * 2048470decc6SDave Kleikamp * (Attach a journal_head if needed. Increments b_jcount) 2049f7f4bccbSMingming Cao * struct journal_head *jh = jbd2_journal_add_journal_head(bh); 2050470decc6SDave Kleikamp * ... 2051470decc6SDave Kleikamp * jh->b_transaction = xxx; 2052f7f4bccbSMingming Cao * jbd2_journal_put_journal_head(jh); 2053470decc6SDave Kleikamp * 2054470decc6SDave Kleikamp * Now, the journal_head's b_jcount is zero, but it is safe from being released 2055470decc6SDave Kleikamp * because it has a non-zero b_transaction. 2056470decc6SDave Kleikamp */ 2057470decc6SDave Kleikamp 2058470decc6SDave Kleikamp /* 2059470decc6SDave Kleikamp * Give a buffer_head a journal_head. 2060470decc6SDave Kleikamp * 2061470decc6SDave Kleikamp * Doesn't need the journal lock. 2062470decc6SDave Kleikamp * May sleep. 2063470decc6SDave Kleikamp */ 2064f7f4bccbSMingming Cao struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh) 2065470decc6SDave Kleikamp { 2066470decc6SDave Kleikamp struct journal_head *jh; 2067470decc6SDave Kleikamp struct journal_head *new_jh = NULL; 2068470decc6SDave Kleikamp 2069470decc6SDave Kleikamp repeat: 2070470decc6SDave Kleikamp if (!buffer_jbd(bh)) { 2071470decc6SDave Kleikamp new_jh = journal_alloc_journal_head(); 2072470decc6SDave Kleikamp memset(new_jh, 0, sizeof(*new_jh)); 2073470decc6SDave Kleikamp } 2074470decc6SDave Kleikamp 2075470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 2076470decc6SDave Kleikamp if (buffer_jbd(bh)) { 2077470decc6SDave Kleikamp jh = bh2jh(bh); 2078470decc6SDave Kleikamp } else { 2079470decc6SDave Kleikamp J_ASSERT_BH(bh, 2080470decc6SDave Kleikamp (atomic_read(&bh->b_count) > 0) || 2081470decc6SDave Kleikamp (bh->b_page && bh->b_page->mapping)); 2082470decc6SDave Kleikamp 2083470decc6SDave Kleikamp if (!new_jh) { 2084470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 2085470decc6SDave Kleikamp goto repeat; 2086470decc6SDave Kleikamp } 2087470decc6SDave Kleikamp 2088470decc6SDave Kleikamp jh = new_jh; 2089470decc6SDave Kleikamp new_jh = NULL; /* We consumed it */ 2090470decc6SDave Kleikamp set_buffer_jbd(bh); 2091470decc6SDave Kleikamp bh->b_private = jh; 2092470decc6SDave Kleikamp jh->b_bh = bh; 2093470decc6SDave Kleikamp get_bh(bh); 2094470decc6SDave Kleikamp BUFFER_TRACE(bh, "added journal_head"); 2095470decc6SDave Kleikamp } 2096470decc6SDave Kleikamp jh->b_jcount++; 2097470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 2098470decc6SDave Kleikamp if (new_jh) 2099470decc6SDave Kleikamp journal_free_journal_head(new_jh); 2100470decc6SDave Kleikamp return bh->b_private; 2101470decc6SDave Kleikamp } 2102470decc6SDave Kleikamp 2103470decc6SDave Kleikamp /* 2104470decc6SDave Kleikamp * Grab a ref against this buffer_head's journal_head. If it ended up not 2105470decc6SDave Kleikamp * having a journal_head, return NULL 2106470decc6SDave Kleikamp */ 2107f7f4bccbSMingming Cao struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh) 2108470decc6SDave Kleikamp { 2109470decc6SDave Kleikamp struct journal_head *jh = NULL; 2110470decc6SDave Kleikamp 2111470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 2112470decc6SDave Kleikamp if (buffer_jbd(bh)) { 2113470decc6SDave Kleikamp jh = bh2jh(bh); 2114470decc6SDave Kleikamp jh->b_jcount++; 2115470decc6SDave Kleikamp } 2116470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 2117470decc6SDave Kleikamp return jh; 2118470decc6SDave Kleikamp } 2119470decc6SDave Kleikamp 2120470decc6SDave Kleikamp static void __journal_remove_journal_head(struct buffer_head *bh) 2121470decc6SDave Kleikamp { 2122470decc6SDave Kleikamp struct journal_head *jh = bh2jh(bh); 2123470decc6SDave Kleikamp 2124470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_jcount >= 0); 2125470decc6SDave Kleikamp 2126470decc6SDave Kleikamp get_bh(bh); 2127470decc6SDave Kleikamp if (jh->b_jcount == 0) { 2128470decc6SDave Kleikamp if (jh->b_transaction == NULL && 2129470decc6SDave Kleikamp jh->b_next_transaction == NULL && 2130470decc6SDave Kleikamp jh->b_cp_transaction == NULL) { 2131470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_jlist == BJ_None); 2132470decc6SDave Kleikamp J_ASSERT_BH(bh, buffer_jbd(bh)); 2133470decc6SDave Kleikamp J_ASSERT_BH(bh, jh2bh(jh) == bh); 2134470decc6SDave Kleikamp BUFFER_TRACE(bh, "remove journal_head"); 2135470decc6SDave Kleikamp if (jh->b_frozen_data) { 2136470decc6SDave Kleikamp printk(KERN_WARNING "%s: freeing " 2137470decc6SDave Kleikamp "b_frozen_data\n", 2138470decc6SDave Kleikamp __FUNCTION__); 2139af1e76d6SMingming Cao jbd2_free(jh->b_frozen_data, bh->b_size); 2140470decc6SDave Kleikamp } 2141470decc6SDave Kleikamp if (jh->b_committed_data) { 2142470decc6SDave Kleikamp printk(KERN_WARNING "%s: freeing " 2143470decc6SDave Kleikamp "b_committed_data\n", 2144470decc6SDave Kleikamp __FUNCTION__); 2145af1e76d6SMingming Cao jbd2_free(jh->b_committed_data, bh->b_size); 2146470decc6SDave Kleikamp } 2147470decc6SDave Kleikamp bh->b_private = NULL; 2148470decc6SDave Kleikamp jh->b_bh = NULL; /* debug, really */ 2149470decc6SDave Kleikamp clear_buffer_jbd(bh); 2150470decc6SDave Kleikamp __brelse(bh); 2151470decc6SDave Kleikamp journal_free_journal_head(jh); 2152470decc6SDave Kleikamp } else { 2153470decc6SDave Kleikamp BUFFER_TRACE(bh, "journal_head was locked"); 2154470decc6SDave Kleikamp } 2155470decc6SDave Kleikamp } 2156470decc6SDave Kleikamp } 2157470decc6SDave Kleikamp 2158470decc6SDave Kleikamp /* 2159f7f4bccbSMingming Cao * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction 2160470decc6SDave Kleikamp * and has a zero b_jcount then remove and release its journal_head. If we did 2161470decc6SDave Kleikamp * see that the buffer is not used by any transaction we also "logically" 2162470decc6SDave Kleikamp * decrement ->b_count. 2163470decc6SDave Kleikamp * 2164470decc6SDave Kleikamp * We in fact take an additional increment on ->b_count as a convenience, 2165470decc6SDave Kleikamp * because the caller usually wants to do additional things with the bh 2166470decc6SDave Kleikamp * after calling here. 2167f7f4bccbSMingming Cao * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some 2168470decc6SDave Kleikamp * time. Once the caller has run __brelse(), the buffer is eligible for 2169470decc6SDave Kleikamp * reaping by try_to_free_buffers(). 2170470decc6SDave Kleikamp */ 2171f7f4bccbSMingming Cao void jbd2_journal_remove_journal_head(struct buffer_head *bh) 2172470decc6SDave Kleikamp { 2173470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 2174470decc6SDave Kleikamp __journal_remove_journal_head(bh); 2175470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 2176470decc6SDave Kleikamp } 2177470decc6SDave Kleikamp 2178470decc6SDave Kleikamp /* 2179470decc6SDave Kleikamp * Drop a reference on the passed journal_head. If it fell to zero then try to 2180470decc6SDave Kleikamp * release the journal_head from the buffer_head. 2181470decc6SDave Kleikamp */ 2182f7f4bccbSMingming Cao void jbd2_journal_put_journal_head(struct journal_head *jh) 2183470decc6SDave Kleikamp { 2184470decc6SDave Kleikamp struct buffer_head *bh = jh2bh(jh); 2185470decc6SDave Kleikamp 2186470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 2187470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_jcount > 0); 2188470decc6SDave Kleikamp --jh->b_jcount; 2189470decc6SDave Kleikamp if (!jh->b_jcount && !jh->b_transaction) { 2190470decc6SDave Kleikamp __journal_remove_journal_head(bh); 2191470decc6SDave Kleikamp __brelse(bh); 2192470decc6SDave Kleikamp } 2193470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 2194470decc6SDave Kleikamp } 2195470decc6SDave Kleikamp 2196470decc6SDave Kleikamp /* 21970f49d5d0SJose R. Santos * debugfs tunables 2198470decc6SDave Kleikamp */ 21996f38c74fSJose R. Santos #ifdef CONFIG_JBD2_DEBUG 22006f38c74fSJose R. Santos u8 jbd2_journal_enable_debug __read_mostly; 2201f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_enable_debug); 2202470decc6SDave Kleikamp 22030f49d5d0SJose R. Santos #define JBD2_DEBUG_NAME "jbd2-debug" 2204470decc6SDave Kleikamp 22056f38c74fSJose R. Santos static struct dentry *jbd2_debugfs_dir; 22066f38c74fSJose R. Santos static struct dentry *jbd2_debug; 22070f49d5d0SJose R. Santos 22080f49d5d0SJose R. Santos static void __init jbd2_create_debugfs_entry(void) 2209470decc6SDave Kleikamp { 22100f49d5d0SJose R. Santos jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL); 22110f49d5d0SJose R. Santos if (jbd2_debugfs_dir) 22120f49d5d0SJose R. Santos jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME, S_IRUGO, 22130f49d5d0SJose R. Santos jbd2_debugfs_dir, 22140f49d5d0SJose R. Santos &jbd2_journal_enable_debug); 2215470decc6SDave Kleikamp } 2216470decc6SDave Kleikamp 22170f49d5d0SJose R. Santos static void __exit jbd2_remove_debugfs_entry(void) 2218470decc6SDave Kleikamp { 22190f49d5d0SJose R. Santos debugfs_remove(jbd2_debug); 22200f49d5d0SJose R. Santos debugfs_remove(jbd2_debugfs_dir); 2221470decc6SDave Kleikamp } 2222470decc6SDave Kleikamp 2223470decc6SDave Kleikamp #else 2224470decc6SDave Kleikamp 22250f49d5d0SJose R. Santos static void __init jbd2_create_debugfs_entry(void) 22260f49d5d0SJose R. Santos { 22270f49d5d0SJose R. Santos } 22280f49d5d0SJose R. Santos 22290f49d5d0SJose R. Santos static void __exit jbd2_remove_debugfs_entry(void) 22300f49d5d0SJose R. Santos { 22310f49d5d0SJose R. Santos } 2232470decc6SDave Kleikamp 2233470decc6SDave Kleikamp #endif 2234470decc6SDave Kleikamp 22358e85fb3fSJohann Lombardi #ifdef CONFIG_PROC_FS 22368e85fb3fSJohann Lombardi 22378e85fb3fSJohann Lombardi #define JBD2_STATS_PROC_NAME "fs/jbd2" 22388e85fb3fSJohann Lombardi 22398e85fb3fSJohann Lombardi static void __init jbd2_create_jbd_stats_proc_entry(void) 22408e85fb3fSJohann Lombardi { 22418e85fb3fSJohann Lombardi proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL); 22428e85fb3fSJohann Lombardi } 22438e85fb3fSJohann Lombardi 22448e85fb3fSJohann Lombardi static void __exit jbd2_remove_jbd_stats_proc_entry(void) 22458e85fb3fSJohann Lombardi { 22468e85fb3fSJohann Lombardi if (proc_jbd2_stats) 22478e85fb3fSJohann Lombardi remove_proc_entry(JBD2_STATS_PROC_NAME, NULL); 22488e85fb3fSJohann Lombardi } 22498e85fb3fSJohann Lombardi 22508e85fb3fSJohann Lombardi #else 22518e85fb3fSJohann Lombardi 22528e85fb3fSJohann Lombardi #define jbd2_create_jbd_stats_proc_entry() do {} while (0) 22538e85fb3fSJohann Lombardi #define jbd2_remove_jbd_stats_proc_entry() do {} while (0) 22548e85fb3fSJohann Lombardi 22558e85fb3fSJohann Lombardi #endif 22568e85fb3fSJohann Lombardi 2257e18b890bSChristoph Lameter struct kmem_cache *jbd2_handle_cache; 2258470decc6SDave Kleikamp 2259470decc6SDave Kleikamp static int __init journal_init_handle_cache(void) 2260470decc6SDave Kleikamp { 2261a920e941SJohann Lombardi jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle", 2262470decc6SDave Kleikamp sizeof(handle_t), 2263470decc6SDave Kleikamp 0, /* offset */ 226477160957SMingming Cao SLAB_TEMPORARY, /* flags */ 226520c2df83SPaul Mundt NULL); /* ctor */ 2266f7f4bccbSMingming Cao if (jbd2_handle_cache == NULL) { 2267470decc6SDave Kleikamp printk(KERN_EMERG "JBD: failed to create handle cache\n"); 2268470decc6SDave Kleikamp return -ENOMEM; 2269470decc6SDave Kleikamp } 2270470decc6SDave Kleikamp return 0; 2271470decc6SDave Kleikamp } 2272470decc6SDave Kleikamp 2273f7f4bccbSMingming Cao static void jbd2_journal_destroy_handle_cache(void) 2274470decc6SDave Kleikamp { 2275f7f4bccbSMingming Cao if (jbd2_handle_cache) 2276f7f4bccbSMingming Cao kmem_cache_destroy(jbd2_handle_cache); 2277470decc6SDave Kleikamp } 2278470decc6SDave Kleikamp 2279470decc6SDave Kleikamp /* 2280470decc6SDave Kleikamp * Module startup and shutdown 2281470decc6SDave Kleikamp */ 2282470decc6SDave Kleikamp 2283470decc6SDave Kleikamp static int __init journal_init_caches(void) 2284470decc6SDave Kleikamp { 2285470decc6SDave Kleikamp int ret; 2286470decc6SDave Kleikamp 2287f7f4bccbSMingming Cao ret = jbd2_journal_init_revoke_caches(); 2288470decc6SDave Kleikamp if (ret == 0) 2289f7f4bccbSMingming Cao ret = journal_init_jbd2_journal_head_cache(); 2290470decc6SDave Kleikamp if (ret == 0) 2291470decc6SDave Kleikamp ret = journal_init_handle_cache(); 2292470decc6SDave Kleikamp return ret; 2293470decc6SDave Kleikamp } 2294470decc6SDave Kleikamp 2295f7f4bccbSMingming Cao static void jbd2_journal_destroy_caches(void) 2296470decc6SDave Kleikamp { 2297f7f4bccbSMingming Cao jbd2_journal_destroy_revoke_caches(); 2298f7f4bccbSMingming Cao jbd2_journal_destroy_jbd2_journal_head_cache(); 2299f7f4bccbSMingming Cao jbd2_journal_destroy_handle_cache(); 2300470decc6SDave Kleikamp } 2301470decc6SDave Kleikamp 2302470decc6SDave Kleikamp static int __init journal_init(void) 2303470decc6SDave Kleikamp { 2304470decc6SDave Kleikamp int ret; 2305470decc6SDave Kleikamp 2306470decc6SDave Kleikamp BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024); 2307470decc6SDave Kleikamp 2308470decc6SDave Kleikamp ret = journal_init_caches(); 2309470decc6SDave Kleikamp if (ret != 0) 2310f7f4bccbSMingming Cao jbd2_journal_destroy_caches(); 23110f49d5d0SJose R. Santos jbd2_create_debugfs_entry(); 23128e85fb3fSJohann Lombardi jbd2_create_jbd_stats_proc_entry(); 2313470decc6SDave Kleikamp return ret; 2314470decc6SDave Kleikamp } 2315470decc6SDave Kleikamp 2316470decc6SDave Kleikamp static void __exit journal_exit(void) 2317470decc6SDave Kleikamp { 2318e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 2319470decc6SDave Kleikamp int n = atomic_read(&nr_journal_heads); 2320470decc6SDave Kleikamp if (n) 2321470decc6SDave Kleikamp printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n); 2322470decc6SDave Kleikamp #endif 23230f49d5d0SJose R. Santos jbd2_remove_debugfs_entry(); 23248e85fb3fSJohann Lombardi jbd2_remove_jbd_stats_proc_entry(); 2325f7f4bccbSMingming Cao jbd2_journal_destroy_caches(); 2326470decc6SDave Kleikamp } 2327470decc6SDave Kleikamp 2328470decc6SDave Kleikamp MODULE_LICENSE("GPL"); 2329470decc6SDave Kleikamp module_init(journal_init); 2330470decc6SDave Kleikamp module_exit(journal_exit); 2331470decc6SDave Kleikamp 2332