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_metadata); 54f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_release_buffer); 55f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_forget); 56470decc6SDave Kleikamp #if 0 57470decc6SDave Kleikamp EXPORT_SYMBOL(journal_sync_buffer); 58470decc6SDave Kleikamp #endif 59f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_flush); 60f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_revoke); 61470decc6SDave Kleikamp 62f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_init_dev); 63f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_init_inode); 64f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_update_format); 65f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_check_used_features); 66f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_check_available_features); 67f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_set_features); 68f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_create); 69f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_load); 70f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_destroy); 71f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_abort); 72f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_errno); 73f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_ack_err); 74f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_clear_err); 75f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_log_wait_commit); 76f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_start_commit); 77f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_force_commit_nested); 78f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_wipe); 79f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_blocks_per_page); 80f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_invalidatepage); 81f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers); 82f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_force_commit); 83c851ed54SJan Kara EXPORT_SYMBOL(jbd2_journal_file_inode); 84c851ed54SJan Kara EXPORT_SYMBOL(jbd2_journal_init_jbd_inode); 85c851ed54SJan Kara EXPORT_SYMBOL(jbd2_journal_release_jbd_inode); 86c851ed54SJan Kara EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate); 87470decc6SDave Kleikamp 88470decc6SDave Kleikamp static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *); 89470decc6SDave Kleikamp static void __journal_abort_soft (journal_t *journal, int errno); 90470decc6SDave Kleikamp 91470decc6SDave Kleikamp /* 92470decc6SDave Kleikamp * Helper function used to manage commit timeouts 93470decc6SDave Kleikamp */ 94470decc6SDave Kleikamp 95470decc6SDave Kleikamp static void commit_timeout(unsigned long __data) 96470decc6SDave Kleikamp { 97470decc6SDave Kleikamp struct task_struct * p = (struct task_struct *) __data; 98470decc6SDave Kleikamp 99470decc6SDave Kleikamp wake_up_process(p); 100470decc6SDave Kleikamp } 101470decc6SDave Kleikamp 102470decc6SDave Kleikamp /* 103f7f4bccbSMingming Cao * kjournald2: The main thread function used to manage a logging device 104470decc6SDave Kleikamp * journal. 105470decc6SDave Kleikamp * 106470decc6SDave Kleikamp * This kernel thread is responsible for two things: 107470decc6SDave Kleikamp * 108470decc6SDave Kleikamp * 1) COMMIT: Every so often we need to commit the current state of the 109470decc6SDave Kleikamp * filesystem to disk. The journal thread is responsible for writing 110470decc6SDave Kleikamp * all of the metadata buffers to disk. 111470decc6SDave Kleikamp * 112470decc6SDave Kleikamp * 2) CHECKPOINT: We cannot reuse a used section of the log file until all 113470decc6SDave Kleikamp * of the data in that part of the log has been rewritten elsewhere on 114470decc6SDave Kleikamp * the disk. Flushing these old buffers to reclaim space in the log is 115470decc6SDave Kleikamp * known as checkpointing, and this thread is responsible for that job. 116470decc6SDave Kleikamp */ 117470decc6SDave Kleikamp 118f7f4bccbSMingming Cao static int kjournald2(void *arg) 119470decc6SDave Kleikamp { 120470decc6SDave Kleikamp journal_t *journal = arg; 121470decc6SDave Kleikamp transaction_t *transaction; 122470decc6SDave Kleikamp 123470decc6SDave Kleikamp /* 124470decc6SDave Kleikamp * Set up an interval timer which can be used to trigger a commit wakeup 125470decc6SDave Kleikamp * after the commit interval expires 126470decc6SDave Kleikamp */ 127470decc6SDave Kleikamp setup_timer(&journal->j_commit_timer, commit_timeout, 128470decc6SDave Kleikamp (unsigned long)current); 129470decc6SDave Kleikamp 130470decc6SDave Kleikamp /* Record that the journal thread is running */ 131470decc6SDave Kleikamp journal->j_task = current; 132470decc6SDave Kleikamp wake_up(&journal->j_wait_done_commit); 133470decc6SDave Kleikamp 134f7f4bccbSMingming Cao printk(KERN_INFO "kjournald2 starting. Commit interval %ld seconds\n", 135470decc6SDave Kleikamp journal->j_commit_interval / HZ); 136470decc6SDave Kleikamp 137470decc6SDave Kleikamp /* 138470decc6SDave Kleikamp * And now, wait forever for commit wakeup events. 139470decc6SDave Kleikamp */ 140470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 141470decc6SDave Kleikamp 142470decc6SDave Kleikamp loop: 143f7f4bccbSMingming Cao if (journal->j_flags & JBD2_UNMOUNT) 144470decc6SDave Kleikamp goto end_loop; 145470decc6SDave Kleikamp 146470decc6SDave Kleikamp jbd_debug(1, "commit_sequence=%d, commit_request=%d\n", 147470decc6SDave Kleikamp journal->j_commit_sequence, journal->j_commit_request); 148470decc6SDave Kleikamp 149470decc6SDave Kleikamp if (journal->j_commit_sequence != journal->j_commit_request) { 150470decc6SDave Kleikamp jbd_debug(1, "OK, requests differ\n"); 151470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 152470decc6SDave Kleikamp del_timer_sync(&journal->j_commit_timer); 153f7f4bccbSMingming Cao jbd2_journal_commit_transaction(journal); 154470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 155470decc6SDave Kleikamp goto loop; 156470decc6SDave Kleikamp } 157470decc6SDave Kleikamp 158470decc6SDave Kleikamp wake_up(&journal->j_wait_done_commit); 159470decc6SDave Kleikamp if (freezing(current)) { 160470decc6SDave Kleikamp /* 161470decc6SDave Kleikamp * The simpler the better. Flushing journal isn't a 162470decc6SDave Kleikamp * good idea, because that depends on threads that may 163470decc6SDave Kleikamp * be already stopped. 164470decc6SDave Kleikamp */ 165f7f4bccbSMingming Cao jbd_debug(1, "Now suspending kjournald2\n"); 166470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 167470decc6SDave Kleikamp refrigerator(); 168470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 169470decc6SDave Kleikamp } else { 170470decc6SDave Kleikamp /* 171470decc6SDave Kleikamp * We assume on resume that commits are already there, 172470decc6SDave Kleikamp * so we don't sleep 173470decc6SDave Kleikamp */ 174470decc6SDave Kleikamp DEFINE_WAIT(wait); 175470decc6SDave Kleikamp int should_sleep = 1; 176470decc6SDave Kleikamp 177470decc6SDave Kleikamp prepare_to_wait(&journal->j_wait_commit, &wait, 178470decc6SDave Kleikamp TASK_INTERRUPTIBLE); 179470decc6SDave Kleikamp if (journal->j_commit_sequence != journal->j_commit_request) 180470decc6SDave Kleikamp should_sleep = 0; 181470decc6SDave Kleikamp transaction = journal->j_running_transaction; 182470decc6SDave Kleikamp if (transaction && time_after_eq(jiffies, 183470decc6SDave Kleikamp transaction->t_expires)) 184470decc6SDave Kleikamp should_sleep = 0; 185f7f4bccbSMingming Cao if (journal->j_flags & JBD2_UNMOUNT) 186470decc6SDave Kleikamp should_sleep = 0; 187470decc6SDave Kleikamp if (should_sleep) { 188470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 189470decc6SDave Kleikamp schedule(); 190470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 191470decc6SDave Kleikamp } 192470decc6SDave Kleikamp finish_wait(&journal->j_wait_commit, &wait); 193470decc6SDave Kleikamp } 194470decc6SDave Kleikamp 195f7f4bccbSMingming Cao jbd_debug(1, "kjournald2 wakes\n"); 196470decc6SDave Kleikamp 197470decc6SDave Kleikamp /* 198470decc6SDave Kleikamp * Were we woken up by a commit wakeup event? 199470decc6SDave Kleikamp */ 200470decc6SDave Kleikamp transaction = journal->j_running_transaction; 201470decc6SDave Kleikamp if (transaction && time_after_eq(jiffies, transaction->t_expires)) { 202470decc6SDave Kleikamp journal->j_commit_request = transaction->t_tid; 203470decc6SDave Kleikamp jbd_debug(1, "woke because of timeout\n"); 204470decc6SDave Kleikamp } 205470decc6SDave Kleikamp goto loop; 206470decc6SDave Kleikamp 207470decc6SDave Kleikamp end_loop: 208470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 209470decc6SDave Kleikamp del_timer_sync(&journal->j_commit_timer); 210470decc6SDave Kleikamp journal->j_task = NULL; 211470decc6SDave Kleikamp wake_up(&journal->j_wait_done_commit); 212470decc6SDave Kleikamp jbd_debug(1, "Journal thread exiting.\n"); 213470decc6SDave Kleikamp return 0; 214470decc6SDave Kleikamp } 215470decc6SDave Kleikamp 21697f06784SPavel Emelianov static int jbd2_journal_start_thread(journal_t *journal) 217470decc6SDave Kleikamp { 21897f06784SPavel Emelianov struct task_struct *t; 21997f06784SPavel Emelianov 22097f06784SPavel Emelianov t = kthread_run(kjournald2, journal, "kjournald2"); 22197f06784SPavel Emelianov if (IS_ERR(t)) 22297f06784SPavel Emelianov return PTR_ERR(t); 22397f06784SPavel Emelianov 2241076d17aSAl Viro wait_event(journal->j_wait_done_commit, journal->j_task != NULL); 22597f06784SPavel Emelianov return 0; 226470decc6SDave Kleikamp } 227470decc6SDave Kleikamp 228470decc6SDave Kleikamp static void journal_kill_thread(journal_t *journal) 229470decc6SDave Kleikamp { 230470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 231f7f4bccbSMingming Cao journal->j_flags |= JBD2_UNMOUNT; 232470decc6SDave Kleikamp 233470decc6SDave Kleikamp while (journal->j_task) { 234470decc6SDave Kleikamp wake_up(&journal->j_wait_commit); 235470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 2361076d17aSAl Viro wait_event(journal->j_wait_done_commit, journal->j_task == NULL); 237470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 238470decc6SDave Kleikamp } 239470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 240470decc6SDave Kleikamp } 241470decc6SDave Kleikamp 242470decc6SDave Kleikamp /* 243f7f4bccbSMingming Cao * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal. 244470decc6SDave Kleikamp * 245470decc6SDave Kleikamp * Writes a metadata buffer to a given disk block. The actual IO is not 246470decc6SDave Kleikamp * performed but a new buffer_head is constructed which labels the data 247470decc6SDave Kleikamp * to be written with the correct destination disk block. 248470decc6SDave Kleikamp * 249470decc6SDave Kleikamp * Any magic-number escaping which needs to be done will cause a 250470decc6SDave Kleikamp * copy-out here. If the buffer happens to start with the 251f7f4bccbSMingming Cao * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the 252470decc6SDave Kleikamp * magic number is only written to the log for descripter blocks. In 253470decc6SDave Kleikamp * this case, we copy the data and replace the first word with 0, and we 254470decc6SDave Kleikamp * return a result code which indicates that this buffer needs to be 255470decc6SDave Kleikamp * marked as an escaped buffer in the corresponding log descriptor 256470decc6SDave Kleikamp * block. The missing word can then be restored when the block is read 257470decc6SDave Kleikamp * during recovery. 258470decc6SDave Kleikamp * 259470decc6SDave Kleikamp * If the source buffer has already been modified by a new transaction 260470decc6SDave Kleikamp * since we took the last commit snapshot, we use the frozen copy of 261470decc6SDave Kleikamp * that data for IO. If we end up using the existing buffer_head's data 262470decc6SDave Kleikamp * for the write, then we *have* to lock the buffer to prevent anyone 263470decc6SDave Kleikamp * else from using and possibly modifying it while the IO is in 264470decc6SDave Kleikamp * progress. 265470decc6SDave Kleikamp * 266470decc6SDave Kleikamp * The function returns a pointer to the buffer_heads to be used for IO. 267470decc6SDave Kleikamp * 268470decc6SDave Kleikamp * We assume that the journal has already been locked in this function. 269470decc6SDave Kleikamp * 270470decc6SDave Kleikamp * Return value: 271470decc6SDave Kleikamp * <0: Error 272470decc6SDave Kleikamp * >=0: Finished OK 273470decc6SDave Kleikamp * 274470decc6SDave Kleikamp * On success: 275470decc6SDave Kleikamp * Bit 0 set == escape performed on the data 276470decc6SDave Kleikamp * Bit 1 set == buffer copy-out performed (kfree the data after IO) 277470decc6SDave Kleikamp */ 278470decc6SDave Kleikamp 279f7f4bccbSMingming Cao int jbd2_journal_write_metadata_buffer(transaction_t *transaction, 280470decc6SDave Kleikamp struct journal_head *jh_in, 281470decc6SDave Kleikamp struct journal_head **jh_out, 28218eba7aaSMingming Cao unsigned long long blocknr) 283470decc6SDave Kleikamp { 284470decc6SDave Kleikamp int need_copy_out = 0; 285470decc6SDave Kleikamp int done_copy_out = 0; 286470decc6SDave Kleikamp int do_escape = 0; 287470decc6SDave Kleikamp char *mapped_data; 288470decc6SDave Kleikamp struct buffer_head *new_bh; 289470decc6SDave Kleikamp struct journal_head *new_jh; 290470decc6SDave Kleikamp struct page *new_page; 291470decc6SDave Kleikamp unsigned int new_offset; 292470decc6SDave Kleikamp struct buffer_head *bh_in = jh2bh(jh_in); 293470decc6SDave Kleikamp 294470decc6SDave Kleikamp /* 295470decc6SDave Kleikamp * The buffer really shouldn't be locked: only the current committing 296470decc6SDave Kleikamp * transaction is allowed to write it, so nobody else is allowed 297470decc6SDave Kleikamp * to do any IO. 298470decc6SDave Kleikamp * 299470decc6SDave Kleikamp * akpm: except if we're journalling data, and write() output is 300470decc6SDave Kleikamp * also part of a shared mapping, and another thread has 301470decc6SDave Kleikamp * decided to launch a writepage() against this buffer. 302470decc6SDave Kleikamp */ 303470decc6SDave Kleikamp J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in)); 304470decc6SDave Kleikamp 305470decc6SDave Kleikamp new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL); 306470decc6SDave Kleikamp 307470decc6SDave Kleikamp /* 308470decc6SDave Kleikamp * If a new transaction has already done a buffer copy-out, then 309470decc6SDave Kleikamp * we use that version of the data for the commit. 310470decc6SDave Kleikamp */ 311470decc6SDave Kleikamp jbd_lock_bh_state(bh_in); 312470decc6SDave Kleikamp repeat: 313470decc6SDave Kleikamp if (jh_in->b_frozen_data) { 314470decc6SDave Kleikamp done_copy_out = 1; 315470decc6SDave Kleikamp new_page = virt_to_page(jh_in->b_frozen_data); 316470decc6SDave Kleikamp new_offset = offset_in_page(jh_in->b_frozen_data); 317470decc6SDave Kleikamp } else { 318470decc6SDave Kleikamp new_page = jh2bh(jh_in)->b_page; 319470decc6SDave Kleikamp new_offset = offset_in_page(jh2bh(jh_in)->b_data); 320470decc6SDave Kleikamp } 321470decc6SDave Kleikamp 322470decc6SDave Kleikamp mapped_data = kmap_atomic(new_page, KM_USER0); 323470decc6SDave Kleikamp /* 324470decc6SDave Kleikamp * Check for escaping 325470decc6SDave Kleikamp */ 326470decc6SDave Kleikamp if (*((__be32 *)(mapped_data + new_offset)) == 327f7f4bccbSMingming Cao cpu_to_be32(JBD2_MAGIC_NUMBER)) { 328470decc6SDave Kleikamp need_copy_out = 1; 329470decc6SDave Kleikamp do_escape = 1; 330470decc6SDave Kleikamp } 331470decc6SDave Kleikamp kunmap_atomic(mapped_data, KM_USER0); 332470decc6SDave Kleikamp 333470decc6SDave Kleikamp /* 334470decc6SDave Kleikamp * Do we need to do a data copy? 335470decc6SDave Kleikamp */ 336470decc6SDave Kleikamp if (need_copy_out && !done_copy_out) { 337470decc6SDave Kleikamp char *tmp; 338470decc6SDave Kleikamp 339470decc6SDave Kleikamp jbd_unlock_bh_state(bh_in); 340af1e76d6SMingming Cao tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS); 341470decc6SDave Kleikamp jbd_lock_bh_state(bh_in); 342470decc6SDave Kleikamp if (jh_in->b_frozen_data) { 343af1e76d6SMingming Cao jbd2_free(tmp, bh_in->b_size); 344470decc6SDave Kleikamp goto repeat; 345470decc6SDave Kleikamp } 346470decc6SDave Kleikamp 347470decc6SDave Kleikamp jh_in->b_frozen_data = tmp; 348470decc6SDave Kleikamp mapped_data = kmap_atomic(new_page, KM_USER0); 349470decc6SDave Kleikamp memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size); 350470decc6SDave Kleikamp kunmap_atomic(mapped_data, KM_USER0); 351470decc6SDave Kleikamp 352470decc6SDave Kleikamp new_page = virt_to_page(tmp); 353470decc6SDave Kleikamp new_offset = offset_in_page(tmp); 354470decc6SDave Kleikamp done_copy_out = 1; 355470decc6SDave Kleikamp } 356470decc6SDave Kleikamp 357470decc6SDave Kleikamp /* 358470decc6SDave Kleikamp * Did we need to do an escaping? Now we've done all the 359470decc6SDave Kleikamp * copying, we can finally do so. 360470decc6SDave Kleikamp */ 361470decc6SDave Kleikamp if (do_escape) { 362470decc6SDave Kleikamp mapped_data = kmap_atomic(new_page, KM_USER0); 363470decc6SDave Kleikamp *((unsigned int *)(mapped_data + new_offset)) = 0; 364470decc6SDave Kleikamp kunmap_atomic(mapped_data, KM_USER0); 365470decc6SDave Kleikamp } 366470decc6SDave Kleikamp 367470decc6SDave Kleikamp /* keep subsequent assertions sane */ 368470decc6SDave Kleikamp new_bh->b_state = 0; 369470decc6SDave Kleikamp init_buffer(new_bh, NULL, NULL); 370470decc6SDave Kleikamp atomic_set(&new_bh->b_count, 1); 371470decc6SDave Kleikamp jbd_unlock_bh_state(bh_in); 372470decc6SDave Kleikamp 373f7f4bccbSMingming Cao new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */ 374470decc6SDave Kleikamp 375470decc6SDave Kleikamp set_bh_page(new_bh, new_page, new_offset); 376470decc6SDave Kleikamp new_jh->b_transaction = NULL; 377470decc6SDave Kleikamp new_bh->b_size = jh2bh(jh_in)->b_size; 378470decc6SDave Kleikamp new_bh->b_bdev = transaction->t_journal->j_dev; 379470decc6SDave Kleikamp new_bh->b_blocknr = blocknr; 380470decc6SDave Kleikamp set_buffer_mapped(new_bh); 381470decc6SDave Kleikamp set_buffer_dirty(new_bh); 382470decc6SDave Kleikamp 383470decc6SDave Kleikamp *jh_out = new_jh; 384470decc6SDave Kleikamp 385470decc6SDave Kleikamp /* 386470decc6SDave Kleikamp * The to-be-written buffer needs to get moved to the io queue, 387470decc6SDave Kleikamp * and the original buffer whose contents we are shadowing or 388470decc6SDave Kleikamp * copying is moved to the transaction's shadow queue. 389470decc6SDave Kleikamp */ 390470decc6SDave Kleikamp JBUFFER_TRACE(jh_in, "file as BJ_Shadow"); 391f7f4bccbSMingming Cao jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow); 392470decc6SDave Kleikamp JBUFFER_TRACE(new_jh, "file as BJ_IO"); 393f7f4bccbSMingming Cao jbd2_journal_file_buffer(new_jh, transaction, BJ_IO); 394470decc6SDave Kleikamp 395470decc6SDave Kleikamp return do_escape | (done_copy_out << 1); 396470decc6SDave Kleikamp } 397470decc6SDave Kleikamp 398470decc6SDave Kleikamp /* 399470decc6SDave Kleikamp * Allocation code for the journal file. Manage the space left in the 400470decc6SDave Kleikamp * journal, so that we can begin checkpointing when appropriate. 401470decc6SDave Kleikamp */ 402470decc6SDave Kleikamp 403470decc6SDave Kleikamp /* 404f7f4bccbSMingming Cao * __jbd2_log_space_left: Return the number of free blocks left in the journal. 405470decc6SDave Kleikamp * 406470decc6SDave Kleikamp * Called with the journal already locked. 407470decc6SDave Kleikamp * 408470decc6SDave Kleikamp * Called under j_state_lock 409470decc6SDave Kleikamp */ 410470decc6SDave Kleikamp 411f7f4bccbSMingming Cao int __jbd2_log_space_left(journal_t *journal) 412470decc6SDave Kleikamp { 413470decc6SDave Kleikamp int left = journal->j_free; 414470decc6SDave Kleikamp 415470decc6SDave Kleikamp assert_spin_locked(&journal->j_state_lock); 416470decc6SDave Kleikamp 417470decc6SDave Kleikamp /* 418470decc6SDave Kleikamp * Be pessimistic here about the number of those free blocks which 419470decc6SDave Kleikamp * might be required for log descriptor control blocks. 420470decc6SDave Kleikamp */ 421470decc6SDave Kleikamp 422470decc6SDave Kleikamp #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */ 423470decc6SDave Kleikamp 424470decc6SDave Kleikamp left -= MIN_LOG_RESERVED_BLOCKS; 425470decc6SDave Kleikamp 426470decc6SDave Kleikamp if (left <= 0) 427470decc6SDave Kleikamp return 0; 428470decc6SDave Kleikamp left -= (left >> 3); 429470decc6SDave Kleikamp return left; 430470decc6SDave Kleikamp } 431470decc6SDave Kleikamp 432470decc6SDave Kleikamp /* 433470decc6SDave Kleikamp * Called under j_state_lock. Returns true if a transaction was started. 434470decc6SDave Kleikamp */ 435f7f4bccbSMingming Cao int __jbd2_log_start_commit(journal_t *journal, tid_t target) 436470decc6SDave Kleikamp { 437470decc6SDave Kleikamp /* 438470decc6SDave Kleikamp * Are we already doing a recent enough commit? 439470decc6SDave Kleikamp */ 440470decc6SDave Kleikamp if (!tid_geq(journal->j_commit_request, target)) { 441470decc6SDave Kleikamp /* 442470decc6SDave Kleikamp * We want a new commit: OK, mark the request and wakup the 443470decc6SDave Kleikamp * commit thread. We do _not_ do the commit ourselves. 444470decc6SDave Kleikamp */ 445470decc6SDave Kleikamp 446470decc6SDave Kleikamp journal->j_commit_request = target; 447470decc6SDave Kleikamp jbd_debug(1, "JBD: requesting commit %d/%d\n", 448470decc6SDave Kleikamp journal->j_commit_request, 449470decc6SDave Kleikamp journal->j_commit_sequence); 450470decc6SDave Kleikamp wake_up(&journal->j_wait_commit); 451470decc6SDave Kleikamp return 1; 452470decc6SDave Kleikamp } 453470decc6SDave Kleikamp return 0; 454470decc6SDave Kleikamp } 455470decc6SDave Kleikamp 456f7f4bccbSMingming Cao int jbd2_log_start_commit(journal_t *journal, tid_t tid) 457470decc6SDave Kleikamp { 458470decc6SDave Kleikamp int ret; 459470decc6SDave Kleikamp 460470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 461f7f4bccbSMingming Cao ret = __jbd2_log_start_commit(journal, tid); 462470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 463470decc6SDave Kleikamp return ret; 464470decc6SDave Kleikamp } 465470decc6SDave Kleikamp 466470decc6SDave Kleikamp /* 467470decc6SDave Kleikamp * Force and wait upon a commit if the calling process is not within 468470decc6SDave Kleikamp * transaction. This is used for forcing out undo-protected data which contains 469470decc6SDave Kleikamp * bitmaps, when the fs is running out of space. 470470decc6SDave Kleikamp * 471470decc6SDave Kleikamp * We can only force the running transaction if we don't have an active handle; 472470decc6SDave Kleikamp * otherwise, we will deadlock. 473470decc6SDave Kleikamp * 474470decc6SDave Kleikamp * Returns true if a transaction was started. 475470decc6SDave Kleikamp */ 476f7f4bccbSMingming Cao int jbd2_journal_force_commit_nested(journal_t *journal) 477470decc6SDave Kleikamp { 478470decc6SDave Kleikamp transaction_t *transaction = NULL; 479470decc6SDave Kleikamp tid_t tid; 480470decc6SDave Kleikamp 481470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 482470decc6SDave Kleikamp if (journal->j_running_transaction && !current->journal_info) { 483470decc6SDave Kleikamp transaction = journal->j_running_transaction; 484f7f4bccbSMingming Cao __jbd2_log_start_commit(journal, transaction->t_tid); 485470decc6SDave Kleikamp } else if (journal->j_committing_transaction) 486470decc6SDave Kleikamp transaction = journal->j_committing_transaction; 487470decc6SDave Kleikamp 488470decc6SDave Kleikamp if (!transaction) { 489470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 490470decc6SDave Kleikamp return 0; /* Nothing to retry */ 491470decc6SDave Kleikamp } 492470decc6SDave Kleikamp 493470decc6SDave Kleikamp tid = transaction->t_tid; 494470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 495f7f4bccbSMingming Cao jbd2_log_wait_commit(journal, tid); 496470decc6SDave Kleikamp return 1; 497470decc6SDave Kleikamp } 498470decc6SDave Kleikamp 499470decc6SDave Kleikamp /* 500470decc6SDave Kleikamp * Start a commit of the current running transaction (if any). Returns true 501470decc6SDave Kleikamp * if a transaction was started, and fills its tid in at *ptid 502470decc6SDave Kleikamp */ 503f7f4bccbSMingming Cao int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid) 504470decc6SDave Kleikamp { 505470decc6SDave Kleikamp int ret = 0; 506470decc6SDave Kleikamp 507470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 508470decc6SDave Kleikamp if (journal->j_running_transaction) { 509470decc6SDave Kleikamp tid_t tid = journal->j_running_transaction->t_tid; 510470decc6SDave Kleikamp 511f7f4bccbSMingming Cao ret = __jbd2_log_start_commit(journal, tid); 512470decc6SDave Kleikamp if (ret && ptid) 513470decc6SDave Kleikamp *ptid = tid; 514470decc6SDave Kleikamp } else if (journal->j_committing_transaction && ptid) { 515470decc6SDave Kleikamp /* 516470decc6SDave Kleikamp * If ext3_write_super() recently started a commit, then we 517470decc6SDave Kleikamp * have to wait for completion of that transaction 518470decc6SDave Kleikamp */ 519470decc6SDave Kleikamp *ptid = journal->j_committing_transaction->t_tid; 520470decc6SDave Kleikamp ret = 1; 521470decc6SDave Kleikamp } 522470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 523470decc6SDave Kleikamp return ret; 524470decc6SDave Kleikamp } 525470decc6SDave Kleikamp 526470decc6SDave Kleikamp /* 527470decc6SDave Kleikamp * Wait for a specified commit to complete. 528470decc6SDave Kleikamp * The caller may not hold the journal lock. 529470decc6SDave Kleikamp */ 530f7f4bccbSMingming Cao int jbd2_log_wait_commit(journal_t *journal, tid_t tid) 531470decc6SDave Kleikamp { 532470decc6SDave Kleikamp int err = 0; 533470decc6SDave Kleikamp 534e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 535470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 536470decc6SDave Kleikamp if (!tid_geq(journal->j_commit_request, tid)) { 537470decc6SDave Kleikamp printk(KERN_EMERG 538470decc6SDave Kleikamp "%s: error: j_commit_request=%d, tid=%d\n", 539329d291fSHarvey Harrison __func__, journal->j_commit_request, tid); 540470decc6SDave Kleikamp } 541470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 542470decc6SDave Kleikamp #endif 543470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 544470decc6SDave Kleikamp while (tid_gt(tid, journal->j_commit_sequence)) { 545470decc6SDave Kleikamp jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n", 546470decc6SDave Kleikamp tid, journal->j_commit_sequence); 547470decc6SDave Kleikamp wake_up(&journal->j_wait_commit); 548470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 549470decc6SDave Kleikamp wait_event(journal->j_wait_done_commit, 550470decc6SDave Kleikamp !tid_gt(tid, journal->j_commit_sequence)); 551470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 552470decc6SDave Kleikamp } 553470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 554470decc6SDave Kleikamp 555470decc6SDave Kleikamp if (unlikely(is_journal_aborted(journal))) { 556470decc6SDave Kleikamp printk(KERN_EMERG "journal commit I/O error\n"); 557470decc6SDave Kleikamp err = -EIO; 558470decc6SDave Kleikamp } 559470decc6SDave Kleikamp return err; 560470decc6SDave Kleikamp } 561470decc6SDave Kleikamp 562470decc6SDave Kleikamp /* 563470decc6SDave Kleikamp * Log buffer allocation routines: 564470decc6SDave Kleikamp */ 565470decc6SDave Kleikamp 56618eba7aaSMingming Cao int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp) 567470decc6SDave Kleikamp { 568470decc6SDave Kleikamp unsigned long blocknr; 569470decc6SDave Kleikamp 570470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 571470decc6SDave Kleikamp J_ASSERT(journal->j_free > 1); 572470decc6SDave Kleikamp 573470decc6SDave Kleikamp blocknr = journal->j_head; 574470decc6SDave Kleikamp journal->j_head++; 575470decc6SDave Kleikamp journal->j_free--; 576470decc6SDave Kleikamp if (journal->j_head == journal->j_last) 577470decc6SDave Kleikamp journal->j_head = journal->j_first; 578470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 579f7f4bccbSMingming Cao return jbd2_journal_bmap(journal, blocknr, retp); 580470decc6SDave Kleikamp } 581470decc6SDave Kleikamp 582470decc6SDave Kleikamp /* 583470decc6SDave Kleikamp * Conversion of logical to physical block numbers for the journal 584470decc6SDave Kleikamp * 585470decc6SDave Kleikamp * On external journals the journal blocks are identity-mapped, so 586470decc6SDave Kleikamp * this is a no-op. If needed, we can use j_blk_offset - everything is 587470decc6SDave Kleikamp * ready. 588470decc6SDave Kleikamp */ 589f7f4bccbSMingming Cao int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr, 59018eba7aaSMingming Cao unsigned long long *retp) 591470decc6SDave Kleikamp { 592470decc6SDave Kleikamp int err = 0; 59318eba7aaSMingming Cao unsigned long long ret; 594470decc6SDave Kleikamp 595470decc6SDave Kleikamp if (journal->j_inode) { 596470decc6SDave Kleikamp ret = bmap(journal->j_inode, blocknr); 597470decc6SDave Kleikamp if (ret) 598470decc6SDave Kleikamp *retp = ret; 599470decc6SDave Kleikamp else { 600470decc6SDave Kleikamp printk(KERN_ALERT "%s: journal block not found " 601470decc6SDave Kleikamp "at offset %lu on %s\n", 60205496769STheodore Ts'o __func__, blocknr, journal->j_devname); 603470decc6SDave Kleikamp err = -EIO; 604470decc6SDave Kleikamp __journal_abort_soft(journal, err); 605470decc6SDave Kleikamp } 606470decc6SDave Kleikamp } else { 607470decc6SDave Kleikamp *retp = blocknr; /* +journal->j_blk_offset */ 608470decc6SDave Kleikamp } 609470decc6SDave Kleikamp return err; 610470decc6SDave Kleikamp } 611470decc6SDave Kleikamp 612470decc6SDave Kleikamp /* 613470decc6SDave Kleikamp * We play buffer_head aliasing tricks to write data/metadata blocks to 614470decc6SDave Kleikamp * the journal without copying their contents, but for journal 615470decc6SDave Kleikamp * descriptor blocks we do need to generate bona fide buffers. 616470decc6SDave Kleikamp * 617f7f4bccbSMingming Cao * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying 618470decc6SDave Kleikamp * the buffer's contents they really should run flush_dcache_page(bh->b_page). 619470decc6SDave Kleikamp * But we don't bother doing that, so there will be coherency problems with 620470decc6SDave Kleikamp * mmaps of blockdevs which hold live JBD-controlled filesystems. 621470decc6SDave Kleikamp */ 622f7f4bccbSMingming Cao struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal) 623470decc6SDave Kleikamp { 624470decc6SDave Kleikamp struct buffer_head *bh; 62518eba7aaSMingming Cao unsigned long long blocknr; 626470decc6SDave Kleikamp int err; 627470decc6SDave Kleikamp 628f7f4bccbSMingming Cao err = jbd2_journal_next_log_block(journal, &blocknr); 629470decc6SDave Kleikamp 630470decc6SDave Kleikamp if (err) 631470decc6SDave Kleikamp return NULL; 632470decc6SDave Kleikamp 633470decc6SDave Kleikamp bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize); 634470decc6SDave Kleikamp lock_buffer(bh); 635470decc6SDave Kleikamp memset(bh->b_data, 0, journal->j_blocksize); 636470decc6SDave Kleikamp set_buffer_uptodate(bh); 637470decc6SDave Kleikamp unlock_buffer(bh); 638470decc6SDave Kleikamp BUFFER_TRACE(bh, "return this buffer"); 639f7f4bccbSMingming Cao return jbd2_journal_add_journal_head(bh); 640470decc6SDave Kleikamp } 641470decc6SDave Kleikamp 6428e85fb3fSJohann Lombardi struct jbd2_stats_proc_session { 6438e85fb3fSJohann Lombardi journal_t *journal; 6448e85fb3fSJohann Lombardi struct transaction_stats_s *stats; 6458e85fb3fSJohann Lombardi int start; 6468e85fb3fSJohann Lombardi int max; 6478e85fb3fSJohann Lombardi }; 6488e85fb3fSJohann Lombardi 6498e85fb3fSJohann Lombardi static void *jbd2_history_skip_empty(struct jbd2_stats_proc_session *s, 6508e85fb3fSJohann Lombardi struct transaction_stats_s *ts, 6518e85fb3fSJohann Lombardi int first) 6528e85fb3fSJohann Lombardi { 6538e85fb3fSJohann Lombardi if (ts == s->stats + s->max) 6548e85fb3fSJohann Lombardi ts = s->stats; 6558e85fb3fSJohann Lombardi if (!first && ts == s->stats + s->start) 6568e85fb3fSJohann Lombardi return NULL; 6578e85fb3fSJohann Lombardi while (ts->ts_type == 0) { 6588e85fb3fSJohann Lombardi ts++; 6598e85fb3fSJohann Lombardi if (ts == s->stats + s->max) 6608e85fb3fSJohann Lombardi ts = s->stats; 6618e85fb3fSJohann Lombardi if (ts == s->stats + s->start) 6628e85fb3fSJohann Lombardi return NULL; 6638e85fb3fSJohann Lombardi } 6648e85fb3fSJohann Lombardi return ts; 6658e85fb3fSJohann Lombardi 6668e85fb3fSJohann Lombardi } 6678e85fb3fSJohann Lombardi 6688e85fb3fSJohann Lombardi static void *jbd2_seq_history_start(struct seq_file *seq, loff_t *pos) 6698e85fb3fSJohann Lombardi { 6708e85fb3fSJohann Lombardi struct jbd2_stats_proc_session *s = seq->private; 6718e85fb3fSJohann Lombardi struct transaction_stats_s *ts; 6728e85fb3fSJohann Lombardi int l = *pos; 6738e85fb3fSJohann Lombardi 6748e85fb3fSJohann Lombardi if (l == 0) 6758e85fb3fSJohann Lombardi return SEQ_START_TOKEN; 6768e85fb3fSJohann Lombardi ts = jbd2_history_skip_empty(s, s->stats + s->start, 1); 6778e85fb3fSJohann Lombardi if (!ts) 6788e85fb3fSJohann Lombardi return NULL; 6798e85fb3fSJohann Lombardi l--; 6808e85fb3fSJohann Lombardi while (l) { 6818e85fb3fSJohann Lombardi ts = jbd2_history_skip_empty(s, ++ts, 0); 6828e85fb3fSJohann Lombardi if (!ts) 6838e85fb3fSJohann Lombardi break; 6848e85fb3fSJohann Lombardi l--; 6858e85fb3fSJohann Lombardi } 6868e85fb3fSJohann Lombardi return ts; 6878e85fb3fSJohann Lombardi } 6888e85fb3fSJohann Lombardi 6898e85fb3fSJohann Lombardi static void *jbd2_seq_history_next(struct seq_file *seq, void *v, loff_t *pos) 6908e85fb3fSJohann Lombardi { 6918e85fb3fSJohann Lombardi struct jbd2_stats_proc_session *s = seq->private; 6928e85fb3fSJohann Lombardi struct transaction_stats_s *ts = v; 6938e85fb3fSJohann Lombardi 6948e85fb3fSJohann Lombardi ++*pos; 6958e85fb3fSJohann Lombardi if (v == SEQ_START_TOKEN) 6968e85fb3fSJohann Lombardi return jbd2_history_skip_empty(s, s->stats + s->start, 1); 6978e85fb3fSJohann Lombardi else 6988e85fb3fSJohann Lombardi return jbd2_history_skip_empty(s, ++ts, 0); 6998e85fb3fSJohann Lombardi } 7008e85fb3fSJohann Lombardi 7018e85fb3fSJohann Lombardi static int jbd2_seq_history_show(struct seq_file *seq, void *v) 7028e85fb3fSJohann Lombardi { 7038e85fb3fSJohann Lombardi struct transaction_stats_s *ts = v; 7048e85fb3fSJohann Lombardi if (v == SEQ_START_TOKEN) { 7058e85fb3fSJohann Lombardi seq_printf(seq, "%-4s %-5s %-5s %-5s %-5s %-5s %-5s %-6s %-5s " 7068e85fb3fSJohann Lombardi "%-5s %-5s %-5s %-5s %-5s\n", "R/C", "tid", 7078e85fb3fSJohann Lombardi "wait", "run", "lock", "flush", "log", "hndls", 7088e85fb3fSJohann Lombardi "block", "inlog", "ctime", "write", "drop", 7098e85fb3fSJohann Lombardi "close"); 7108e85fb3fSJohann Lombardi return 0; 7118e85fb3fSJohann Lombardi } 7128e85fb3fSJohann Lombardi if (ts->ts_type == JBD2_STATS_RUN) 7138e85fb3fSJohann Lombardi seq_printf(seq, "%-4s %-5lu %-5u %-5u %-5u %-5u %-5u " 7148e85fb3fSJohann Lombardi "%-6lu %-5lu %-5lu\n", "R", ts->ts_tid, 7158e85fb3fSJohann Lombardi jiffies_to_msecs(ts->u.run.rs_wait), 7168e85fb3fSJohann Lombardi jiffies_to_msecs(ts->u.run.rs_running), 7178e85fb3fSJohann Lombardi jiffies_to_msecs(ts->u.run.rs_locked), 7188e85fb3fSJohann Lombardi jiffies_to_msecs(ts->u.run.rs_flushing), 7198e85fb3fSJohann Lombardi jiffies_to_msecs(ts->u.run.rs_logging), 7208e85fb3fSJohann Lombardi ts->u.run.rs_handle_count, 7218e85fb3fSJohann Lombardi ts->u.run.rs_blocks, 7228e85fb3fSJohann Lombardi ts->u.run.rs_blocks_logged); 7238e85fb3fSJohann Lombardi else if (ts->ts_type == JBD2_STATS_CHECKPOINT) 7248e85fb3fSJohann Lombardi seq_printf(seq, "%-4s %-5lu %48s %-5u %-5lu %-5lu %-5lu\n", 7258e85fb3fSJohann Lombardi "C", ts->ts_tid, " ", 7268e85fb3fSJohann Lombardi jiffies_to_msecs(ts->u.chp.cs_chp_time), 7278e85fb3fSJohann Lombardi ts->u.chp.cs_written, ts->u.chp.cs_dropped, 7288e85fb3fSJohann Lombardi ts->u.chp.cs_forced_to_close); 7298e85fb3fSJohann Lombardi else 7308e85fb3fSJohann Lombardi J_ASSERT(0); 7318e85fb3fSJohann Lombardi return 0; 7328e85fb3fSJohann Lombardi } 7338e85fb3fSJohann Lombardi 7348e85fb3fSJohann Lombardi static void jbd2_seq_history_stop(struct seq_file *seq, void *v) 7358e85fb3fSJohann Lombardi { 7368e85fb3fSJohann Lombardi } 7378e85fb3fSJohann Lombardi 7388e85fb3fSJohann Lombardi static struct seq_operations jbd2_seq_history_ops = { 7398e85fb3fSJohann Lombardi .start = jbd2_seq_history_start, 7408e85fb3fSJohann Lombardi .next = jbd2_seq_history_next, 7418e85fb3fSJohann Lombardi .stop = jbd2_seq_history_stop, 7428e85fb3fSJohann Lombardi .show = jbd2_seq_history_show, 7438e85fb3fSJohann Lombardi }; 7448e85fb3fSJohann Lombardi 7458e85fb3fSJohann Lombardi static int jbd2_seq_history_open(struct inode *inode, struct file *file) 7468e85fb3fSJohann Lombardi { 7478e85fb3fSJohann Lombardi journal_t *journal = PDE(inode)->data; 7488e85fb3fSJohann Lombardi struct jbd2_stats_proc_session *s; 7498e85fb3fSJohann Lombardi int rc, size; 7508e85fb3fSJohann Lombardi 7518e85fb3fSJohann Lombardi s = kmalloc(sizeof(*s), GFP_KERNEL); 7528e85fb3fSJohann Lombardi if (s == NULL) 7538e85fb3fSJohann Lombardi return -ENOMEM; 7548e85fb3fSJohann Lombardi size = sizeof(struct transaction_stats_s) * journal->j_history_max; 7558e85fb3fSJohann Lombardi s->stats = kmalloc(size, GFP_KERNEL); 7568e85fb3fSJohann Lombardi if (s->stats == NULL) { 7578e85fb3fSJohann Lombardi kfree(s); 7588e85fb3fSJohann Lombardi return -ENOMEM; 7598e85fb3fSJohann Lombardi } 7608e85fb3fSJohann Lombardi spin_lock(&journal->j_history_lock); 7618e85fb3fSJohann Lombardi memcpy(s->stats, journal->j_history, size); 7628e85fb3fSJohann Lombardi s->max = journal->j_history_max; 7638e85fb3fSJohann Lombardi s->start = journal->j_history_cur % s->max; 7648e85fb3fSJohann Lombardi spin_unlock(&journal->j_history_lock); 7658e85fb3fSJohann Lombardi 7668e85fb3fSJohann Lombardi rc = seq_open(file, &jbd2_seq_history_ops); 7678e85fb3fSJohann Lombardi if (rc == 0) { 7688e85fb3fSJohann Lombardi struct seq_file *m = file->private_data; 7698e85fb3fSJohann Lombardi m->private = s; 7708e85fb3fSJohann Lombardi } else { 7718e85fb3fSJohann Lombardi kfree(s->stats); 7728e85fb3fSJohann Lombardi kfree(s); 7738e85fb3fSJohann Lombardi } 7748e85fb3fSJohann Lombardi return rc; 7758e85fb3fSJohann Lombardi 7768e85fb3fSJohann Lombardi } 7778e85fb3fSJohann Lombardi 7788e85fb3fSJohann Lombardi static int jbd2_seq_history_release(struct inode *inode, struct file *file) 7798e85fb3fSJohann Lombardi { 7808e85fb3fSJohann Lombardi struct seq_file *seq = file->private_data; 7818e85fb3fSJohann Lombardi struct jbd2_stats_proc_session *s = seq->private; 7828e85fb3fSJohann Lombardi 7838e85fb3fSJohann Lombardi kfree(s->stats); 7848e85fb3fSJohann Lombardi kfree(s); 7858e85fb3fSJohann Lombardi return seq_release(inode, file); 7868e85fb3fSJohann Lombardi } 7878e85fb3fSJohann Lombardi 7888e85fb3fSJohann Lombardi static struct file_operations jbd2_seq_history_fops = { 7898e85fb3fSJohann Lombardi .owner = THIS_MODULE, 7908e85fb3fSJohann Lombardi .open = jbd2_seq_history_open, 7918e85fb3fSJohann Lombardi .read = seq_read, 7928e85fb3fSJohann Lombardi .llseek = seq_lseek, 7938e85fb3fSJohann Lombardi .release = jbd2_seq_history_release, 7948e85fb3fSJohann Lombardi }; 7958e85fb3fSJohann Lombardi 7968e85fb3fSJohann Lombardi static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos) 7978e85fb3fSJohann Lombardi { 7988e85fb3fSJohann Lombardi return *pos ? NULL : SEQ_START_TOKEN; 7998e85fb3fSJohann Lombardi } 8008e85fb3fSJohann Lombardi 8018e85fb3fSJohann Lombardi static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos) 8028e85fb3fSJohann Lombardi { 8038e85fb3fSJohann Lombardi return NULL; 8048e85fb3fSJohann Lombardi } 8058e85fb3fSJohann Lombardi 8068e85fb3fSJohann Lombardi static int jbd2_seq_info_show(struct seq_file *seq, void *v) 8078e85fb3fSJohann Lombardi { 8088e85fb3fSJohann Lombardi struct jbd2_stats_proc_session *s = seq->private; 8098e85fb3fSJohann Lombardi 8108e85fb3fSJohann Lombardi if (v != SEQ_START_TOKEN) 8118e85fb3fSJohann Lombardi return 0; 8128e85fb3fSJohann Lombardi seq_printf(seq, "%lu transaction, each upto %u blocks\n", 8138e85fb3fSJohann Lombardi s->stats->ts_tid, 8148e85fb3fSJohann Lombardi s->journal->j_max_transaction_buffers); 8158e85fb3fSJohann Lombardi if (s->stats->ts_tid == 0) 8168e85fb3fSJohann Lombardi return 0; 8178e85fb3fSJohann Lombardi seq_printf(seq, "average: \n %ums waiting for transaction\n", 8188e85fb3fSJohann Lombardi jiffies_to_msecs(s->stats->u.run.rs_wait / s->stats->ts_tid)); 8198e85fb3fSJohann Lombardi seq_printf(seq, " %ums running transaction\n", 8208e85fb3fSJohann Lombardi jiffies_to_msecs(s->stats->u.run.rs_running / s->stats->ts_tid)); 8218e85fb3fSJohann Lombardi seq_printf(seq, " %ums transaction was being locked\n", 8228e85fb3fSJohann Lombardi jiffies_to_msecs(s->stats->u.run.rs_locked / s->stats->ts_tid)); 8238e85fb3fSJohann Lombardi seq_printf(seq, " %ums flushing data (in ordered mode)\n", 8248e85fb3fSJohann Lombardi jiffies_to_msecs(s->stats->u.run.rs_flushing / s->stats->ts_tid)); 8258e85fb3fSJohann Lombardi seq_printf(seq, " %ums logging transaction\n", 8268e85fb3fSJohann Lombardi jiffies_to_msecs(s->stats->u.run.rs_logging / s->stats->ts_tid)); 8278e85fb3fSJohann Lombardi seq_printf(seq, " %lu handles per transaction\n", 8288e85fb3fSJohann Lombardi s->stats->u.run.rs_handle_count / s->stats->ts_tid); 8298e85fb3fSJohann Lombardi seq_printf(seq, " %lu blocks per transaction\n", 8308e85fb3fSJohann Lombardi s->stats->u.run.rs_blocks / s->stats->ts_tid); 8318e85fb3fSJohann Lombardi seq_printf(seq, " %lu logged blocks per transaction\n", 8328e85fb3fSJohann Lombardi s->stats->u.run.rs_blocks_logged / s->stats->ts_tid); 8338e85fb3fSJohann Lombardi return 0; 8348e85fb3fSJohann Lombardi } 8358e85fb3fSJohann Lombardi 8368e85fb3fSJohann Lombardi static void jbd2_seq_info_stop(struct seq_file *seq, void *v) 8378e85fb3fSJohann Lombardi { 8388e85fb3fSJohann Lombardi } 8398e85fb3fSJohann Lombardi 8408e85fb3fSJohann Lombardi static struct seq_operations jbd2_seq_info_ops = { 8418e85fb3fSJohann Lombardi .start = jbd2_seq_info_start, 8428e85fb3fSJohann Lombardi .next = jbd2_seq_info_next, 8438e85fb3fSJohann Lombardi .stop = jbd2_seq_info_stop, 8448e85fb3fSJohann Lombardi .show = jbd2_seq_info_show, 8458e85fb3fSJohann Lombardi }; 8468e85fb3fSJohann Lombardi 8478e85fb3fSJohann Lombardi static int jbd2_seq_info_open(struct inode *inode, struct file *file) 8488e85fb3fSJohann Lombardi { 8498e85fb3fSJohann Lombardi journal_t *journal = PDE(inode)->data; 8508e85fb3fSJohann Lombardi struct jbd2_stats_proc_session *s; 8518e85fb3fSJohann Lombardi int rc, size; 8528e85fb3fSJohann Lombardi 8538e85fb3fSJohann Lombardi s = kmalloc(sizeof(*s), GFP_KERNEL); 8548e85fb3fSJohann Lombardi if (s == NULL) 8558e85fb3fSJohann Lombardi return -ENOMEM; 8568e85fb3fSJohann Lombardi size = sizeof(struct transaction_stats_s); 8578e85fb3fSJohann Lombardi s->stats = kmalloc(size, GFP_KERNEL); 8588e85fb3fSJohann Lombardi if (s->stats == NULL) { 8598e85fb3fSJohann Lombardi kfree(s); 8608e85fb3fSJohann Lombardi return -ENOMEM; 8618e85fb3fSJohann Lombardi } 8628e85fb3fSJohann Lombardi spin_lock(&journal->j_history_lock); 8638e85fb3fSJohann Lombardi memcpy(s->stats, &journal->j_stats, size); 8648e85fb3fSJohann Lombardi s->journal = journal; 8658e85fb3fSJohann Lombardi spin_unlock(&journal->j_history_lock); 8668e85fb3fSJohann Lombardi 8678e85fb3fSJohann Lombardi rc = seq_open(file, &jbd2_seq_info_ops); 8688e85fb3fSJohann Lombardi if (rc == 0) { 8698e85fb3fSJohann Lombardi struct seq_file *m = file->private_data; 8708e85fb3fSJohann Lombardi m->private = s; 8718e85fb3fSJohann Lombardi } else { 8728e85fb3fSJohann Lombardi kfree(s->stats); 8738e85fb3fSJohann Lombardi kfree(s); 8748e85fb3fSJohann Lombardi } 8758e85fb3fSJohann Lombardi return rc; 8768e85fb3fSJohann Lombardi 8778e85fb3fSJohann Lombardi } 8788e85fb3fSJohann Lombardi 8798e85fb3fSJohann Lombardi static int jbd2_seq_info_release(struct inode *inode, struct file *file) 8808e85fb3fSJohann Lombardi { 8818e85fb3fSJohann Lombardi struct seq_file *seq = file->private_data; 8828e85fb3fSJohann Lombardi struct jbd2_stats_proc_session *s = seq->private; 8838e85fb3fSJohann Lombardi kfree(s->stats); 8848e85fb3fSJohann Lombardi kfree(s); 8858e85fb3fSJohann Lombardi return seq_release(inode, file); 8868e85fb3fSJohann Lombardi } 8878e85fb3fSJohann Lombardi 8888e85fb3fSJohann Lombardi static struct file_operations jbd2_seq_info_fops = { 8898e85fb3fSJohann Lombardi .owner = THIS_MODULE, 8908e85fb3fSJohann Lombardi .open = jbd2_seq_info_open, 8918e85fb3fSJohann Lombardi .read = seq_read, 8928e85fb3fSJohann Lombardi .llseek = seq_lseek, 8938e85fb3fSJohann Lombardi .release = jbd2_seq_info_release, 8948e85fb3fSJohann Lombardi }; 8958e85fb3fSJohann Lombardi 8968e85fb3fSJohann Lombardi static struct proc_dir_entry *proc_jbd2_stats; 8978e85fb3fSJohann Lombardi 8988e85fb3fSJohann Lombardi static void jbd2_stats_proc_init(journal_t *journal) 8998e85fb3fSJohann Lombardi { 90005496769STheodore Ts'o journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats); 9018e85fb3fSJohann Lombardi if (journal->j_proc_entry) { 90279da3664SDenis V. Lunev proc_create_data("history", S_IRUGO, journal->j_proc_entry, 90379da3664SDenis V. Lunev &jbd2_seq_history_fops, journal); 90479da3664SDenis V. Lunev proc_create_data("info", S_IRUGO, journal->j_proc_entry, 90579da3664SDenis V. Lunev &jbd2_seq_info_fops, journal); 9068e85fb3fSJohann Lombardi } 9078e85fb3fSJohann Lombardi } 9088e85fb3fSJohann Lombardi 9098e85fb3fSJohann Lombardi static void jbd2_stats_proc_exit(journal_t *journal) 9108e85fb3fSJohann Lombardi { 9118e85fb3fSJohann Lombardi remove_proc_entry("info", journal->j_proc_entry); 9128e85fb3fSJohann Lombardi remove_proc_entry("history", journal->j_proc_entry); 91305496769STheodore Ts'o remove_proc_entry(journal->j_devname, proc_jbd2_stats); 9148e85fb3fSJohann Lombardi } 9158e85fb3fSJohann Lombardi 9168e85fb3fSJohann Lombardi static void journal_init_stats(journal_t *journal) 9178e85fb3fSJohann Lombardi { 9188e85fb3fSJohann Lombardi int size; 9198e85fb3fSJohann Lombardi 9208e85fb3fSJohann Lombardi if (!proc_jbd2_stats) 9218e85fb3fSJohann Lombardi return; 9228e85fb3fSJohann Lombardi 9238e85fb3fSJohann Lombardi journal->j_history_max = 100; 9248e85fb3fSJohann Lombardi size = sizeof(struct transaction_stats_s) * journal->j_history_max; 9258e85fb3fSJohann Lombardi journal->j_history = kzalloc(size, GFP_KERNEL); 9268e85fb3fSJohann Lombardi if (!journal->j_history) { 9278e85fb3fSJohann Lombardi journal->j_history_max = 0; 9288e85fb3fSJohann Lombardi return; 9298e85fb3fSJohann Lombardi } 9308e85fb3fSJohann Lombardi spin_lock_init(&journal->j_history_lock); 9318e85fb3fSJohann Lombardi } 9328e85fb3fSJohann Lombardi 933470decc6SDave Kleikamp /* 934470decc6SDave Kleikamp * Management for journal control blocks: functions to create and 935470decc6SDave Kleikamp * destroy journal_t structures, and to initialise and read existing 936470decc6SDave Kleikamp * journal blocks from disk. */ 937470decc6SDave Kleikamp 938470decc6SDave Kleikamp /* First: create and setup a journal_t object in memory. We initialise 939470decc6SDave Kleikamp * very few fields yet: that has to wait until we have created the 940470decc6SDave Kleikamp * journal structures from from scratch, or loaded them from disk. */ 941470decc6SDave Kleikamp 942470decc6SDave Kleikamp static journal_t * journal_init_common (void) 943470decc6SDave Kleikamp { 944470decc6SDave Kleikamp journal_t *journal; 945470decc6SDave Kleikamp int err; 946470decc6SDave Kleikamp 947d802ffa8SMingming Cao journal = kzalloc(sizeof(*journal), GFP_KERNEL|__GFP_NOFAIL); 948470decc6SDave Kleikamp if (!journal) 949470decc6SDave Kleikamp goto fail; 950470decc6SDave Kleikamp 951470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_transaction_locked); 952470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_logspace); 953470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_done_commit); 954470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_checkpoint); 955470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_commit); 956470decc6SDave Kleikamp init_waitqueue_head(&journal->j_wait_updates); 957470decc6SDave Kleikamp mutex_init(&journal->j_barrier); 958470decc6SDave Kleikamp mutex_init(&journal->j_checkpoint_mutex); 959470decc6SDave Kleikamp spin_lock_init(&journal->j_revoke_lock); 960470decc6SDave Kleikamp spin_lock_init(&journal->j_list_lock); 961470decc6SDave Kleikamp spin_lock_init(&journal->j_state_lock); 962470decc6SDave Kleikamp 963cd02ff0bSMingming Cao journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE); 964470decc6SDave Kleikamp 965470decc6SDave Kleikamp /* The journal is marked for error until we succeed with recovery! */ 966f7f4bccbSMingming Cao journal->j_flags = JBD2_ABORT; 967470decc6SDave Kleikamp 968470decc6SDave Kleikamp /* Set up a default-sized revoke table for the new mount. */ 969f7f4bccbSMingming Cao err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH); 970470decc6SDave Kleikamp if (err) { 971470decc6SDave Kleikamp kfree(journal); 972470decc6SDave Kleikamp goto fail; 973470decc6SDave Kleikamp } 9748e85fb3fSJohann Lombardi 9758e85fb3fSJohann Lombardi journal_init_stats(journal); 9768e85fb3fSJohann Lombardi 977470decc6SDave Kleikamp return journal; 978470decc6SDave Kleikamp fail: 979470decc6SDave Kleikamp return NULL; 980470decc6SDave Kleikamp } 981470decc6SDave Kleikamp 982f7f4bccbSMingming Cao /* jbd2_journal_init_dev and jbd2_journal_init_inode: 983470decc6SDave Kleikamp * 984470decc6SDave Kleikamp * Create a journal structure assigned some fixed set of disk blocks to 985470decc6SDave Kleikamp * the journal. We don't actually touch those disk blocks yet, but we 986470decc6SDave Kleikamp * need to set up all of the mapping information to tell the journaling 987470decc6SDave Kleikamp * system where the journal blocks are. 988470decc6SDave Kleikamp * 989470decc6SDave Kleikamp */ 990470decc6SDave Kleikamp 991470decc6SDave Kleikamp /** 9925648ba5bSRandy Dunlap * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure 993470decc6SDave Kleikamp * @bdev: Block device on which to create the journal 994470decc6SDave Kleikamp * @fs_dev: Device which hold journalled filesystem for this journal. 995470decc6SDave Kleikamp * @start: Block nr Start of journal. 996470decc6SDave Kleikamp * @len: Length of the journal in blocks. 997470decc6SDave Kleikamp * @blocksize: blocksize of journalling device 9985648ba5bSRandy Dunlap * 9995648ba5bSRandy Dunlap * Returns: a newly created journal_t * 1000470decc6SDave Kleikamp * 1001f7f4bccbSMingming Cao * jbd2_journal_init_dev creates a journal which maps a fixed contiguous 1002470decc6SDave Kleikamp * range of blocks on an arbitrary block device. 1003470decc6SDave Kleikamp * 1004470decc6SDave Kleikamp */ 1005f7f4bccbSMingming Cao journal_t * jbd2_journal_init_dev(struct block_device *bdev, 1006470decc6SDave Kleikamp struct block_device *fs_dev, 100718eba7aaSMingming Cao unsigned long long start, int len, int blocksize) 1008470decc6SDave Kleikamp { 1009470decc6SDave Kleikamp journal_t *journal = journal_init_common(); 1010470decc6SDave Kleikamp struct buffer_head *bh; 101105496769STheodore Ts'o char *p; 1012470decc6SDave Kleikamp int n; 1013470decc6SDave Kleikamp 1014470decc6SDave Kleikamp if (!journal) 1015470decc6SDave Kleikamp return NULL; 1016470decc6SDave Kleikamp 1017470decc6SDave Kleikamp /* journal descriptor can store up to n blocks -bzzz */ 1018470decc6SDave Kleikamp journal->j_blocksize = blocksize; 1019470decc6SDave Kleikamp n = journal->j_blocksize / sizeof(journal_block_tag_t); 1020470decc6SDave Kleikamp journal->j_wbufsize = n; 1021470decc6SDave Kleikamp journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL); 1022470decc6SDave Kleikamp if (!journal->j_wbuf) { 1023470decc6SDave Kleikamp printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n", 1024329d291fSHarvey Harrison __func__); 1025470decc6SDave Kleikamp kfree(journal); 1026470decc6SDave Kleikamp journal = NULL; 10275eb30790SDave Kleikamp goto out; 1028470decc6SDave Kleikamp } 1029470decc6SDave Kleikamp journal->j_dev = bdev; 1030470decc6SDave Kleikamp journal->j_fs_dev = fs_dev; 1031470decc6SDave Kleikamp journal->j_blk_offset = start; 1032470decc6SDave Kleikamp journal->j_maxlen = len; 103305496769STheodore Ts'o bdevname(journal->j_dev, journal->j_devname); 103405496769STheodore Ts'o p = journal->j_devname; 103505496769STheodore Ts'o while ((p = strchr(p, '/'))) 103605496769STheodore Ts'o *p = '!'; 10378e85fb3fSJohann Lombardi jbd2_stats_proc_init(journal); 1038470decc6SDave Kleikamp 1039470decc6SDave Kleikamp bh = __getblk(journal->j_dev, start, journal->j_blocksize); 1040470decc6SDave Kleikamp J_ASSERT(bh != NULL); 1041470decc6SDave Kleikamp journal->j_sb_buffer = bh; 1042470decc6SDave Kleikamp journal->j_superblock = (journal_superblock_t *)bh->b_data; 10435eb30790SDave Kleikamp out: 1044470decc6SDave Kleikamp return journal; 1045470decc6SDave Kleikamp } 1046470decc6SDave Kleikamp 1047470decc6SDave Kleikamp /** 1048f7f4bccbSMingming Cao * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode. 1049470decc6SDave Kleikamp * @inode: An inode to create the journal in 1050470decc6SDave Kleikamp * 1051f7f4bccbSMingming Cao * jbd2_journal_init_inode creates a journal which maps an on-disk inode as 1052470decc6SDave Kleikamp * the journal. The inode must exist already, must support bmap() and 1053470decc6SDave Kleikamp * must have all data blocks preallocated. 1054470decc6SDave Kleikamp */ 1055f7f4bccbSMingming Cao journal_t * jbd2_journal_init_inode (struct inode *inode) 1056470decc6SDave Kleikamp { 1057470decc6SDave Kleikamp struct buffer_head *bh; 1058470decc6SDave Kleikamp journal_t *journal = journal_init_common(); 105905496769STheodore Ts'o char *p; 1060470decc6SDave Kleikamp int err; 1061470decc6SDave Kleikamp int n; 106218eba7aaSMingming Cao unsigned long long blocknr; 1063470decc6SDave Kleikamp 1064470decc6SDave Kleikamp if (!journal) 1065470decc6SDave Kleikamp return NULL; 1066470decc6SDave Kleikamp 1067470decc6SDave Kleikamp journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev; 1068470decc6SDave Kleikamp journal->j_inode = inode; 106905496769STheodore Ts'o bdevname(journal->j_dev, journal->j_devname); 107005496769STheodore Ts'o p = journal->j_devname; 107105496769STheodore Ts'o while ((p = strchr(p, '/'))) 107205496769STheodore Ts'o *p = '!'; 107305496769STheodore Ts'o p = journal->j_devname + strlen(journal->j_devname); 107405496769STheodore Ts'o sprintf(p, ":%lu", journal->j_inode->i_ino); 1075470decc6SDave Kleikamp jbd_debug(1, 1076470decc6SDave Kleikamp "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n", 1077470decc6SDave Kleikamp journal, inode->i_sb->s_id, inode->i_ino, 1078470decc6SDave Kleikamp (long long) inode->i_size, 1079470decc6SDave Kleikamp inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize); 1080470decc6SDave Kleikamp 1081470decc6SDave Kleikamp journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits; 1082470decc6SDave Kleikamp journal->j_blocksize = inode->i_sb->s_blocksize; 10838e85fb3fSJohann Lombardi jbd2_stats_proc_init(journal); 1084470decc6SDave Kleikamp 1085470decc6SDave Kleikamp /* journal descriptor can store up to n blocks -bzzz */ 1086470decc6SDave Kleikamp n = journal->j_blocksize / sizeof(journal_block_tag_t); 1087470decc6SDave Kleikamp journal->j_wbufsize = n; 1088470decc6SDave Kleikamp journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL); 1089470decc6SDave Kleikamp if (!journal->j_wbuf) { 1090470decc6SDave Kleikamp printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n", 1091329d291fSHarvey Harrison __func__); 1092470decc6SDave Kleikamp kfree(journal); 1093470decc6SDave Kleikamp return NULL; 1094470decc6SDave Kleikamp } 1095470decc6SDave Kleikamp 1096f7f4bccbSMingming Cao err = jbd2_journal_bmap(journal, 0, &blocknr); 1097470decc6SDave Kleikamp /* If that failed, give up */ 1098470decc6SDave Kleikamp if (err) { 1099470decc6SDave Kleikamp printk(KERN_ERR "%s: Cannnot locate journal superblock\n", 1100329d291fSHarvey Harrison __func__); 1101470decc6SDave Kleikamp kfree(journal); 1102470decc6SDave Kleikamp return NULL; 1103470decc6SDave Kleikamp } 1104470decc6SDave Kleikamp 1105470decc6SDave Kleikamp bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize); 1106470decc6SDave Kleikamp J_ASSERT(bh != NULL); 1107470decc6SDave Kleikamp journal->j_sb_buffer = bh; 1108470decc6SDave Kleikamp journal->j_superblock = (journal_superblock_t *)bh->b_data; 1109470decc6SDave Kleikamp 1110470decc6SDave Kleikamp return journal; 1111470decc6SDave Kleikamp } 1112470decc6SDave Kleikamp 1113470decc6SDave Kleikamp /* 1114470decc6SDave Kleikamp * If the journal init or create aborts, we need to mark the journal 1115470decc6SDave Kleikamp * superblock as being NULL to prevent the journal destroy from writing 1116470decc6SDave Kleikamp * back a bogus superblock. 1117470decc6SDave Kleikamp */ 1118470decc6SDave Kleikamp static void journal_fail_superblock (journal_t *journal) 1119470decc6SDave Kleikamp { 1120470decc6SDave Kleikamp struct buffer_head *bh = journal->j_sb_buffer; 1121470decc6SDave Kleikamp brelse(bh); 1122470decc6SDave Kleikamp journal->j_sb_buffer = NULL; 1123470decc6SDave Kleikamp } 1124470decc6SDave Kleikamp 1125470decc6SDave Kleikamp /* 1126470decc6SDave Kleikamp * Given a journal_t structure, initialise the various fields for 1127470decc6SDave Kleikamp * startup of a new journaling session. We use this both when creating 1128470decc6SDave Kleikamp * a journal, and after recovering an old journal to reset it for 1129470decc6SDave Kleikamp * subsequent use. 1130470decc6SDave Kleikamp */ 1131470decc6SDave Kleikamp 1132470decc6SDave Kleikamp static int journal_reset(journal_t *journal) 1133470decc6SDave Kleikamp { 1134470decc6SDave Kleikamp journal_superblock_t *sb = journal->j_superblock; 113518eba7aaSMingming Cao unsigned long long first, last; 1136470decc6SDave Kleikamp 1137470decc6SDave Kleikamp first = be32_to_cpu(sb->s_first); 1138470decc6SDave Kleikamp last = be32_to_cpu(sb->s_maxlen); 1139470decc6SDave Kleikamp 1140470decc6SDave Kleikamp journal->j_first = first; 1141470decc6SDave Kleikamp journal->j_last = last; 1142470decc6SDave Kleikamp 1143470decc6SDave Kleikamp journal->j_head = first; 1144470decc6SDave Kleikamp journal->j_tail = first; 1145470decc6SDave Kleikamp journal->j_free = last - first; 1146470decc6SDave Kleikamp 1147470decc6SDave Kleikamp journal->j_tail_sequence = journal->j_transaction_sequence; 1148470decc6SDave Kleikamp journal->j_commit_sequence = journal->j_transaction_sequence - 1; 1149470decc6SDave Kleikamp journal->j_commit_request = journal->j_commit_sequence; 1150470decc6SDave Kleikamp 1151470decc6SDave Kleikamp journal->j_max_transaction_buffers = journal->j_maxlen / 4; 1152470decc6SDave Kleikamp 1153470decc6SDave Kleikamp /* Add the dynamic fields and write it to disk. */ 1154f7f4bccbSMingming Cao jbd2_journal_update_superblock(journal, 1); 115597f06784SPavel Emelianov return jbd2_journal_start_thread(journal); 1156470decc6SDave Kleikamp } 1157470decc6SDave Kleikamp 1158470decc6SDave Kleikamp /** 1159f7f4bccbSMingming Cao * int jbd2_journal_create() - Initialise the new journal file 1160470decc6SDave Kleikamp * @journal: Journal to create. This structure must have been initialised 1161470decc6SDave Kleikamp * 1162470decc6SDave Kleikamp * Given a journal_t structure which tells us which disk blocks we can 1163470decc6SDave Kleikamp * use, create a new journal superblock and initialise all of the 1164470decc6SDave Kleikamp * journal fields from scratch. 1165470decc6SDave Kleikamp **/ 1166f7f4bccbSMingming Cao int jbd2_journal_create(journal_t *journal) 1167470decc6SDave Kleikamp { 116818eba7aaSMingming Cao unsigned long long blocknr; 1169470decc6SDave Kleikamp struct buffer_head *bh; 1170470decc6SDave Kleikamp journal_superblock_t *sb; 1171470decc6SDave Kleikamp int i, err; 1172470decc6SDave Kleikamp 1173f7f4bccbSMingming Cao if (journal->j_maxlen < JBD2_MIN_JOURNAL_BLOCKS) { 1174470decc6SDave Kleikamp printk (KERN_ERR "Journal length (%d blocks) too short.\n", 1175470decc6SDave Kleikamp journal->j_maxlen); 1176470decc6SDave Kleikamp journal_fail_superblock(journal); 1177470decc6SDave Kleikamp return -EINVAL; 1178470decc6SDave Kleikamp } 1179470decc6SDave Kleikamp 1180470decc6SDave Kleikamp if (journal->j_inode == NULL) { 1181470decc6SDave Kleikamp /* 1182470decc6SDave Kleikamp * We don't know what block to start at! 1183470decc6SDave Kleikamp */ 1184470decc6SDave Kleikamp printk(KERN_EMERG 1185470decc6SDave Kleikamp "%s: creation of journal on external device!\n", 1186329d291fSHarvey Harrison __func__); 1187470decc6SDave Kleikamp BUG(); 1188470decc6SDave Kleikamp } 1189470decc6SDave Kleikamp 1190470decc6SDave Kleikamp /* Zero out the entire journal on disk. We cannot afford to 1191f7f4bccbSMingming Cao have any blocks on disk beginning with JBD2_MAGIC_NUMBER. */ 1192470decc6SDave Kleikamp jbd_debug(1, "JBD: Zeroing out journal blocks...\n"); 1193470decc6SDave Kleikamp for (i = 0; i < journal->j_maxlen; i++) { 1194f7f4bccbSMingming Cao err = jbd2_journal_bmap(journal, i, &blocknr); 1195470decc6SDave Kleikamp if (err) 1196470decc6SDave Kleikamp return err; 1197470decc6SDave Kleikamp bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize); 1198470decc6SDave Kleikamp lock_buffer(bh); 1199470decc6SDave Kleikamp memset (bh->b_data, 0, journal->j_blocksize); 1200470decc6SDave Kleikamp BUFFER_TRACE(bh, "marking dirty"); 1201470decc6SDave Kleikamp mark_buffer_dirty(bh); 1202470decc6SDave Kleikamp BUFFER_TRACE(bh, "marking uptodate"); 1203470decc6SDave Kleikamp set_buffer_uptodate(bh); 1204470decc6SDave Kleikamp unlock_buffer(bh); 1205470decc6SDave Kleikamp __brelse(bh); 1206470decc6SDave Kleikamp } 1207470decc6SDave Kleikamp 1208470decc6SDave Kleikamp sync_blockdev(journal->j_dev); 1209470decc6SDave Kleikamp jbd_debug(1, "JBD: journal cleared.\n"); 1210470decc6SDave Kleikamp 1211470decc6SDave Kleikamp /* OK, fill in the initial static fields in the new superblock */ 1212470decc6SDave Kleikamp sb = journal->j_superblock; 1213470decc6SDave Kleikamp 1214f7f4bccbSMingming Cao sb->s_header.h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER); 1215f7f4bccbSMingming Cao sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2); 1216470decc6SDave Kleikamp 1217470decc6SDave Kleikamp sb->s_blocksize = cpu_to_be32(journal->j_blocksize); 1218470decc6SDave Kleikamp sb->s_maxlen = cpu_to_be32(journal->j_maxlen); 1219470decc6SDave Kleikamp sb->s_first = cpu_to_be32(1); 1220470decc6SDave Kleikamp 1221470decc6SDave Kleikamp journal->j_transaction_sequence = 1; 1222470decc6SDave Kleikamp 1223f7f4bccbSMingming Cao journal->j_flags &= ~JBD2_ABORT; 1224470decc6SDave Kleikamp journal->j_format_version = 2; 1225470decc6SDave Kleikamp 1226470decc6SDave Kleikamp return journal_reset(journal); 1227470decc6SDave Kleikamp } 1228470decc6SDave Kleikamp 1229470decc6SDave Kleikamp /** 1230f7f4bccbSMingming Cao * void jbd2_journal_update_superblock() - Update journal sb on disk. 1231470decc6SDave Kleikamp * @journal: The journal to update. 1232470decc6SDave Kleikamp * @wait: Set to '0' if you don't want to wait for IO completion. 1233470decc6SDave Kleikamp * 1234470decc6SDave Kleikamp * Update a journal's dynamic superblock fields and write it to disk, 1235470decc6SDave Kleikamp * optionally waiting for the IO to complete. 1236470decc6SDave Kleikamp */ 1237f7f4bccbSMingming Cao void jbd2_journal_update_superblock(journal_t *journal, int wait) 1238470decc6SDave Kleikamp { 1239470decc6SDave Kleikamp journal_superblock_t *sb = journal->j_superblock; 1240470decc6SDave Kleikamp struct buffer_head *bh = journal->j_sb_buffer; 1241470decc6SDave Kleikamp 1242470decc6SDave Kleikamp /* 1243470decc6SDave Kleikamp * As a special case, if the on-disk copy is already marked as needing 1244470decc6SDave Kleikamp * no recovery (s_start == 0) and there are no outstanding transactions 1245470decc6SDave Kleikamp * in the filesystem, then we can safely defer the superblock update 1246f7f4bccbSMingming Cao * until the next commit by setting JBD2_FLUSHED. This avoids 1247470decc6SDave Kleikamp * attempting a write to a potential-readonly device. 1248470decc6SDave Kleikamp */ 1249470decc6SDave Kleikamp if (sb->s_start == 0 && journal->j_tail_sequence == 1250470decc6SDave Kleikamp journal->j_transaction_sequence) { 1251470decc6SDave Kleikamp jbd_debug(1,"JBD: Skipping superblock update on recovered sb " 1252470decc6SDave Kleikamp "(start %ld, seq %d, errno %d)\n", 1253470decc6SDave Kleikamp journal->j_tail, journal->j_tail_sequence, 1254470decc6SDave Kleikamp journal->j_errno); 1255470decc6SDave Kleikamp goto out; 1256470decc6SDave Kleikamp } 1257470decc6SDave Kleikamp 1258*914258bfSTheodore Ts'o if (buffer_write_io_error(bh)) { 1259*914258bfSTheodore Ts'o /* 1260*914258bfSTheodore Ts'o * Oh, dear. A previous attempt to write the journal 1261*914258bfSTheodore Ts'o * superblock failed. This could happen because the 1262*914258bfSTheodore Ts'o * USB device was yanked out. Or it could happen to 1263*914258bfSTheodore Ts'o * be a transient write error and maybe the block will 1264*914258bfSTheodore Ts'o * be remapped. Nothing we can do but to retry the 1265*914258bfSTheodore Ts'o * write and hope for the best. 1266*914258bfSTheodore Ts'o */ 1267*914258bfSTheodore Ts'o printk(KERN_ERR "JBD2: previous I/O error detected " 1268*914258bfSTheodore Ts'o "for journal superblock update for %s.\n", 1269*914258bfSTheodore Ts'o journal->j_devname); 1270*914258bfSTheodore Ts'o clear_buffer_write_io_error(bh); 1271*914258bfSTheodore Ts'o set_buffer_uptodate(bh); 1272*914258bfSTheodore Ts'o } 1273*914258bfSTheodore Ts'o 1274470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 1275470decc6SDave Kleikamp jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n", 1276470decc6SDave Kleikamp journal->j_tail, journal->j_tail_sequence, journal->j_errno); 1277470decc6SDave Kleikamp 1278470decc6SDave Kleikamp sb->s_sequence = cpu_to_be32(journal->j_tail_sequence); 1279470decc6SDave Kleikamp sb->s_start = cpu_to_be32(journal->j_tail); 1280470decc6SDave Kleikamp sb->s_errno = cpu_to_be32(journal->j_errno); 1281470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1282470decc6SDave Kleikamp 1283470decc6SDave Kleikamp BUFFER_TRACE(bh, "marking dirty"); 1284470decc6SDave Kleikamp mark_buffer_dirty(bh); 1285*914258bfSTheodore Ts'o if (wait) { 1286470decc6SDave Kleikamp sync_dirty_buffer(bh); 1287*914258bfSTheodore Ts'o if (buffer_write_io_error(bh)) { 1288*914258bfSTheodore Ts'o printk(KERN_ERR "JBD2: I/O error detected " 1289*914258bfSTheodore Ts'o "when updating journal superblock for %s.\n", 1290*914258bfSTheodore Ts'o journal->j_devname); 1291*914258bfSTheodore Ts'o clear_buffer_write_io_error(bh); 1292*914258bfSTheodore Ts'o set_buffer_uptodate(bh); 1293*914258bfSTheodore Ts'o } 1294*914258bfSTheodore Ts'o } else 1295470decc6SDave Kleikamp ll_rw_block(SWRITE, 1, &bh); 1296470decc6SDave Kleikamp 1297470decc6SDave Kleikamp out: 1298470decc6SDave Kleikamp /* If we have just flushed the log (by marking s_start==0), then 1299470decc6SDave Kleikamp * any future commit will have to be careful to update the 1300470decc6SDave Kleikamp * superblock again to re-record the true start of the log. */ 1301470decc6SDave Kleikamp 1302470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 1303470decc6SDave Kleikamp if (sb->s_start) 1304f7f4bccbSMingming Cao journal->j_flags &= ~JBD2_FLUSHED; 1305470decc6SDave Kleikamp else 1306f7f4bccbSMingming Cao journal->j_flags |= JBD2_FLUSHED; 1307470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1308470decc6SDave Kleikamp } 1309470decc6SDave Kleikamp 1310470decc6SDave Kleikamp /* 1311470decc6SDave Kleikamp * Read the superblock for a given journal, performing initial 1312470decc6SDave Kleikamp * validation of the format. 1313470decc6SDave Kleikamp */ 1314470decc6SDave Kleikamp 1315470decc6SDave Kleikamp static int journal_get_superblock(journal_t *journal) 1316470decc6SDave Kleikamp { 1317470decc6SDave Kleikamp struct buffer_head *bh; 1318470decc6SDave Kleikamp journal_superblock_t *sb; 1319470decc6SDave Kleikamp int err = -EIO; 1320470decc6SDave Kleikamp 1321470decc6SDave Kleikamp bh = journal->j_sb_buffer; 1322470decc6SDave Kleikamp 1323470decc6SDave Kleikamp J_ASSERT(bh != NULL); 1324470decc6SDave Kleikamp if (!buffer_uptodate(bh)) { 1325470decc6SDave Kleikamp ll_rw_block(READ, 1, &bh); 1326470decc6SDave Kleikamp wait_on_buffer(bh); 1327470decc6SDave Kleikamp if (!buffer_uptodate(bh)) { 1328470decc6SDave Kleikamp printk (KERN_ERR 1329470decc6SDave Kleikamp "JBD: IO error reading journal superblock\n"); 1330470decc6SDave Kleikamp goto out; 1331470decc6SDave Kleikamp } 1332470decc6SDave Kleikamp } 1333470decc6SDave Kleikamp 1334470decc6SDave Kleikamp sb = journal->j_superblock; 1335470decc6SDave Kleikamp 1336470decc6SDave Kleikamp err = -EINVAL; 1337470decc6SDave Kleikamp 1338f7f4bccbSMingming Cao if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) || 1339470decc6SDave Kleikamp sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) { 1340470decc6SDave Kleikamp printk(KERN_WARNING "JBD: no valid journal superblock found\n"); 1341470decc6SDave Kleikamp goto out; 1342470decc6SDave Kleikamp } 1343470decc6SDave Kleikamp 1344470decc6SDave Kleikamp switch(be32_to_cpu(sb->s_header.h_blocktype)) { 1345f7f4bccbSMingming Cao case JBD2_SUPERBLOCK_V1: 1346470decc6SDave Kleikamp journal->j_format_version = 1; 1347470decc6SDave Kleikamp break; 1348f7f4bccbSMingming Cao case JBD2_SUPERBLOCK_V2: 1349470decc6SDave Kleikamp journal->j_format_version = 2; 1350470decc6SDave Kleikamp break; 1351470decc6SDave Kleikamp default: 1352470decc6SDave Kleikamp printk(KERN_WARNING "JBD: unrecognised superblock format ID\n"); 1353470decc6SDave Kleikamp goto out; 1354470decc6SDave Kleikamp } 1355470decc6SDave Kleikamp 1356470decc6SDave Kleikamp if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen) 1357470decc6SDave Kleikamp journal->j_maxlen = be32_to_cpu(sb->s_maxlen); 1358470decc6SDave Kleikamp else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) { 1359470decc6SDave Kleikamp printk (KERN_WARNING "JBD: journal file too short\n"); 1360470decc6SDave Kleikamp goto out; 1361470decc6SDave Kleikamp } 1362470decc6SDave Kleikamp 1363470decc6SDave Kleikamp return 0; 1364470decc6SDave Kleikamp 1365470decc6SDave Kleikamp out: 1366470decc6SDave Kleikamp journal_fail_superblock(journal); 1367470decc6SDave Kleikamp return err; 1368470decc6SDave Kleikamp } 1369470decc6SDave Kleikamp 1370470decc6SDave Kleikamp /* 1371470decc6SDave Kleikamp * Load the on-disk journal superblock and read the key fields into the 1372470decc6SDave Kleikamp * journal_t. 1373470decc6SDave Kleikamp */ 1374470decc6SDave Kleikamp 1375470decc6SDave Kleikamp static int load_superblock(journal_t *journal) 1376470decc6SDave Kleikamp { 1377470decc6SDave Kleikamp int err; 1378470decc6SDave Kleikamp journal_superblock_t *sb; 1379470decc6SDave Kleikamp 1380470decc6SDave Kleikamp err = journal_get_superblock(journal); 1381470decc6SDave Kleikamp if (err) 1382470decc6SDave Kleikamp return err; 1383470decc6SDave Kleikamp 1384470decc6SDave Kleikamp sb = journal->j_superblock; 1385470decc6SDave Kleikamp 1386470decc6SDave Kleikamp journal->j_tail_sequence = be32_to_cpu(sb->s_sequence); 1387470decc6SDave Kleikamp journal->j_tail = be32_to_cpu(sb->s_start); 1388470decc6SDave Kleikamp journal->j_first = be32_to_cpu(sb->s_first); 1389470decc6SDave Kleikamp journal->j_last = be32_to_cpu(sb->s_maxlen); 1390470decc6SDave Kleikamp journal->j_errno = be32_to_cpu(sb->s_errno); 1391470decc6SDave Kleikamp 1392470decc6SDave Kleikamp return 0; 1393470decc6SDave Kleikamp } 1394470decc6SDave Kleikamp 1395470decc6SDave Kleikamp 1396470decc6SDave Kleikamp /** 1397f7f4bccbSMingming Cao * int jbd2_journal_load() - Read journal from disk. 1398470decc6SDave Kleikamp * @journal: Journal to act on. 1399470decc6SDave Kleikamp * 1400470decc6SDave Kleikamp * Given a journal_t structure which tells us which disk blocks contain 1401470decc6SDave Kleikamp * a journal, read the journal from disk to initialise the in-memory 1402470decc6SDave Kleikamp * structures. 1403470decc6SDave Kleikamp */ 1404f7f4bccbSMingming Cao int jbd2_journal_load(journal_t *journal) 1405470decc6SDave Kleikamp { 1406470decc6SDave Kleikamp int err; 1407470decc6SDave Kleikamp journal_superblock_t *sb; 1408470decc6SDave Kleikamp 1409470decc6SDave Kleikamp err = load_superblock(journal); 1410470decc6SDave Kleikamp if (err) 1411470decc6SDave Kleikamp return err; 1412470decc6SDave Kleikamp 1413470decc6SDave Kleikamp sb = journal->j_superblock; 1414470decc6SDave Kleikamp /* If this is a V2 superblock, then we have to check the 1415470decc6SDave Kleikamp * features flags on it. */ 1416470decc6SDave Kleikamp 1417470decc6SDave Kleikamp if (journal->j_format_version >= 2) { 1418470decc6SDave Kleikamp if ((sb->s_feature_ro_compat & 1419f7f4bccbSMingming Cao ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) || 1420470decc6SDave Kleikamp (sb->s_feature_incompat & 1421f7f4bccbSMingming Cao ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) { 1422470decc6SDave Kleikamp printk (KERN_WARNING 1423470decc6SDave Kleikamp "JBD: Unrecognised features on journal\n"); 1424470decc6SDave Kleikamp return -EINVAL; 1425470decc6SDave Kleikamp } 1426470decc6SDave Kleikamp } 1427470decc6SDave Kleikamp 1428470decc6SDave Kleikamp /* Let the recovery code check whether it needs to recover any 1429470decc6SDave Kleikamp * data from the journal. */ 1430f7f4bccbSMingming Cao if (jbd2_journal_recover(journal)) 1431470decc6SDave Kleikamp goto recovery_error; 1432470decc6SDave Kleikamp 1433470decc6SDave Kleikamp /* OK, we've finished with the dynamic journal bits: 1434470decc6SDave Kleikamp * reinitialise the dynamic contents of the superblock in memory 1435470decc6SDave Kleikamp * and reset them on disk. */ 1436470decc6SDave Kleikamp if (journal_reset(journal)) 1437470decc6SDave Kleikamp goto recovery_error; 1438470decc6SDave Kleikamp 1439f7f4bccbSMingming Cao journal->j_flags &= ~JBD2_ABORT; 1440f7f4bccbSMingming Cao journal->j_flags |= JBD2_LOADED; 1441470decc6SDave Kleikamp return 0; 1442470decc6SDave Kleikamp 1443470decc6SDave Kleikamp recovery_error: 1444470decc6SDave Kleikamp printk (KERN_WARNING "JBD: recovery failed\n"); 1445470decc6SDave Kleikamp return -EIO; 1446470decc6SDave Kleikamp } 1447470decc6SDave Kleikamp 1448470decc6SDave Kleikamp /** 1449f7f4bccbSMingming Cao * void jbd2_journal_destroy() - Release a journal_t structure. 1450470decc6SDave Kleikamp * @journal: Journal to act on. 1451470decc6SDave Kleikamp * 1452470decc6SDave Kleikamp * Release a journal_t structure once it is no longer in use by the 1453470decc6SDave Kleikamp * journaled object. 1454470decc6SDave Kleikamp */ 1455f7f4bccbSMingming Cao void jbd2_journal_destroy(journal_t *journal) 1456470decc6SDave Kleikamp { 1457470decc6SDave Kleikamp /* Wait for the commit thread to wake up and die. */ 1458470decc6SDave Kleikamp journal_kill_thread(journal); 1459470decc6SDave Kleikamp 1460470decc6SDave Kleikamp /* Force a final log commit */ 1461470decc6SDave Kleikamp if (journal->j_running_transaction) 1462f7f4bccbSMingming Cao jbd2_journal_commit_transaction(journal); 1463470decc6SDave Kleikamp 1464470decc6SDave Kleikamp /* Force any old transactions to disk */ 1465470decc6SDave Kleikamp 1466470decc6SDave Kleikamp /* Totally anal locking here... */ 1467470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 1468470decc6SDave Kleikamp while (journal->j_checkpoint_transactions != NULL) { 1469470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 1470f7f4bccbSMingming Cao jbd2_log_do_checkpoint(journal); 1471470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 1472470decc6SDave Kleikamp } 1473470decc6SDave Kleikamp 1474470decc6SDave Kleikamp J_ASSERT(journal->j_running_transaction == NULL); 1475470decc6SDave Kleikamp J_ASSERT(journal->j_committing_transaction == NULL); 1476470decc6SDave Kleikamp J_ASSERT(journal->j_checkpoint_transactions == NULL); 1477470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 1478470decc6SDave Kleikamp 1479470decc6SDave Kleikamp /* We can now mark the journal as empty. */ 1480470decc6SDave Kleikamp journal->j_tail = 0; 1481470decc6SDave Kleikamp journal->j_tail_sequence = ++journal->j_transaction_sequence; 1482470decc6SDave Kleikamp if (journal->j_sb_buffer) { 1483f7f4bccbSMingming Cao jbd2_journal_update_superblock(journal, 1); 1484470decc6SDave Kleikamp brelse(journal->j_sb_buffer); 1485470decc6SDave Kleikamp } 1486470decc6SDave Kleikamp 14878e85fb3fSJohann Lombardi if (journal->j_proc_entry) 14888e85fb3fSJohann Lombardi jbd2_stats_proc_exit(journal); 1489470decc6SDave Kleikamp if (journal->j_inode) 1490470decc6SDave Kleikamp iput(journal->j_inode); 1491470decc6SDave Kleikamp if (journal->j_revoke) 1492f7f4bccbSMingming Cao jbd2_journal_destroy_revoke(journal); 1493470decc6SDave Kleikamp kfree(journal->j_wbuf); 1494470decc6SDave Kleikamp kfree(journal); 1495470decc6SDave Kleikamp } 1496470decc6SDave Kleikamp 1497470decc6SDave Kleikamp 1498470decc6SDave Kleikamp /** 1499f7f4bccbSMingming Cao *int jbd2_journal_check_used_features () - Check if features specified are used. 1500470decc6SDave Kleikamp * @journal: Journal to check. 1501470decc6SDave Kleikamp * @compat: bitmask of compatible features 1502470decc6SDave Kleikamp * @ro: bitmask of features that force read-only mount 1503470decc6SDave Kleikamp * @incompat: bitmask of incompatible features 1504470decc6SDave Kleikamp * 1505470decc6SDave Kleikamp * Check whether the journal uses all of a given set of 1506470decc6SDave Kleikamp * features. Return true (non-zero) if it does. 1507470decc6SDave Kleikamp **/ 1508470decc6SDave Kleikamp 1509f7f4bccbSMingming Cao int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat, 1510470decc6SDave Kleikamp unsigned long ro, unsigned long incompat) 1511470decc6SDave Kleikamp { 1512470decc6SDave Kleikamp journal_superblock_t *sb; 1513470decc6SDave Kleikamp 1514470decc6SDave Kleikamp if (!compat && !ro && !incompat) 1515470decc6SDave Kleikamp return 1; 1516470decc6SDave Kleikamp if (journal->j_format_version == 1) 1517470decc6SDave Kleikamp return 0; 1518470decc6SDave Kleikamp 1519470decc6SDave Kleikamp sb = journal->j_superblock; 1520470decc6SDave Kleikamp 1521470decc6SDave Kleikamp if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) && 1522470decc6SDave Kleikamp ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) && 1523470decc6SDave Kleikamp ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat)) 1524470decc6SDave Kleikamp return 1; 1525470decc6SDave Kleikamp 1526470decc6SDave Kleikamp return 0; 1527470decc6SDave Kleikamp } 1528470decc6SDave Kleikamp 1529470decc6SDave Kleikamp /** 1530f7f4bccbSMingming Cao * int jbd2_journal_check_available_features() - Check feature set in journalling layer 1531470decc6SDave Kleikamp * @journal: Journal to check. 1532470decc6SDave Kleikamp * @compat: bitmask of compatible features 1533470decc6SDave Kleikamp * @ro: bitmask of features that force read-only mount 1534470decc6SDave Kleikamp * @incompat: bitmask of incompatible features 1535470decc6SDave Kleikamp * 1536470decc6SDave Kleikamp * Check whether the journaling code supports the use of 1537470decc6SDave Kleikamp * all of a given set of features on this journal. Return true 1538470decc6SDave Kleikamp * (non-zero) if it can. */ 1539470decc6SDave Kleikamp 1540f7f4bccbSMingming Cao int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat, 1541470decc6SDave Kleikamp unsigned long ro, unsigned long incompat) 1542470decc6SDave Kleikamp { 1543470decc6SDave Kleikamp journal_superblock_t *sb; 1544470decc6SDave Kleikamp 1545470decc6SDave Kleikamp if (!compat && !ro && !incompat) 1546470decc6SDave Kleikamp return 1; 1547470decc6SDave Kleikamp 1548470decc6SDave Kleikamp sb = journal->j_superblock; 1549470decc6SDave Kleikamp 1550470decc6SDave Kleikamp /* We can support any known requested features iff the 1551470decc6SDave Kleikamp * superblock is in version 2. Otherwise we fail to support any 1552470decc6SDave Kleikamp * extended sb features. */ 1553470decc6SDave Kleikamp 1554470decc6SDave Kleikamp if (journal->j_format_version != 2) 1555470decc6SDave Kleikamp return 0; 1556470decc6SDave Kleikamp 1557f7f4bccbSMingming Cao if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat && 1558f7f4bccbSMingming Cao (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro && 1559f7f4bccbSMingming Cao (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat) 1560470decc6SDave Kleikamp return 1; 1561470decc6SDave Kleikamp 1562470decc6SDave Kleikamp return 0; 1563470decc6SDave Kleikamp } 1564470decc6SDave Kleikamp 1565470decc6SDave Kleikamp /** 1566f7f4bccbSMingming Cao * int jbd2_journal_set_features () - Mark a given journal feature in the superblock 1567470decc6SDave Kleikamp * @journal: Journal to act on. 1568470decc6SDave Kleikamp * @compat: bitmask of compatible features 1569470decc6SDave Kleikamp * @ro: bitmask of features that force read-only mount 1570470decc6SDave Kleikamp * @incompat: bitmask of incompatible features 1571470decc6SDave Kleikamp * 1572470decc6SDave Kleikamp * Mark a given journal feature as present on the 1573470decc6SDave Kleikamp * superblock. Returns true if the requested features could be set. 1574470decc6SDave Kleikamp * 1575470decc6SDave Kleikamp */ 1576470decc6SDave Kleikamp 1577f7f4bccbSMingming Cao int jbd2_journal_set_features (journal_t *journal, unsigned long compat, 1578470decc6SDave Kleikamp unsigned long ro, unsigned long incompat) 1579470decc6SDave Kleikamp { 1580470decc6SDave Kleikamp journal_superblock_t *sb; 1581470decc6SDave Kleikamp 1582f7f4bccbSMingming Cao if (jbd2_journal_check_used_features(journal, compat, ro, incompat)) 1583470decc6SDave Kleikamp return 1; 1584470decc6SDave Kleikamp 1585f7f4bccbSMingming Cao if (!jbd2_journal_check_available_features(journal, compat, ro, incompat)) 1586470decc6SDave Kleikamp return 0; 1587470decc6SDave Kleikamp 1588470decc6SDave Kleikamp jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n", 1589470decc6SDave Kleikamp compat, ro, incompat); 1590470decc6SDave Kleikamp 1591470decc6SDave Kleikamp sb = journal->j_superblock; 1592470decc6SDave Kleikamp 1593470decc6SDave Kleikamp sb->s_feature_compat |= cpu_to_be32(compat); 1594470decc6SDave Kleikamp sb->s_feature_ro_compat |= cpu_to_be32(ro); 1595470decc6SDave Kleikamp sb->s_feature_incompat |= cpu_to_be32(incompat); 1596470decc6SDave Kleikamp 1597470decc6SDave Kleikamp return 1; 1598470decc6SDave Kleikamp } 1599470decc6SDave Kleikamp 1600818d276cSGirish Shilamkar /* 1601818d276cSGirish Shilamkar * jbd2_journal_clear_features () - Clear a given journal feature in the 1602818d276cSGirish Shilamkar * superblock 1603818d276cSGirish Shilamkar * @journal: Journal to act on. 1604818d276cSGirish Shilamkar * @compat: bitmask of compatible features 1605818d276cSGirish Shilamkar * @ro: bitmask of features that force read-only mount 1606818d276cSGirish Shilamkar * @incompat: bitmask of incompatible features 1607818d276cSGirish Shilamkar * 1608818d276cSGirish Shilamkar * Clear a given journal feature as present on the 1609818d276cSGirish Shilamkar * superblock. 1610818d276cSGirish Shilamkar */ 1611818d276cSGirish Shilamkar void jbd2_journal_clear_features(journal_t *journal, unsigned long compat, 1612818d276cSGirish Shilamkar unsigned long ro, unsigned long incompat) 1613818d276cSGirish Shilamkar { 1614818d276cSGirish Shilamkar journal_superblock_t *sb; 1615818d276cSGirish Shilamkar 1616818d276cSGirish Shilamkar jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n", 1617818d276cSGirish Shilamkar compat, ro, incompat); 1618818d276cSGirish Shilamkar 1619818d276cSGirish Shilamkar sb = journal->j_superblock; 1620818d276cSGirish Shilamkar 1621818d276cSGirish Shilamkar sb->s_feature_compat &= ~cpu_to_be32(compat); 1622818d276cSGirish Shilamkar sb->s_feature_ro_compat &= ~cpu_to_be32(ro); 1623818d276cSGirish Shilamkar sb->s_feature_incompat &= ~cpu_to_be32(incompat); 1624818d276cSGirish Shilamkar } 1625818d276cSGirish Shilamkar EXPORT_SYMBOL(jbd2_journal_clear_features); 1626470decc6SDave Kleikamp 1627470decc6SDave Kleikamp /** 1628f7f4bccbSMingming Cao * int jbd2_journal_update_format () - Update on-disk journal structure. 1629470decc6SDave Kleikamp * @journal: Journal to act on. 1630470decc6SDave Kleikamp * 1631470decc6SDave Kleikamp * Given an initialised but unloaded journal struct, poke about in the 1632470decc6SDave Kleikamp * on-disk structure to update it to the most recent supported version. 1633470decc6SDave Kleikamp */ 1634f7f4bccbSMingming Cao int jbd2_journal_update_format (journal_t *journal) 1635470decc6SDave Kleikamp { 1636470decc6SDave Kleikamp journal_superblock_t *sb; 1637470decc6SDave Kleikamp int err; 1638470decc6SDave Kleikamp 1639470decc6SDave Kleikamp err = journal_get_superblock(journal); 1640470decc6SDave Kleikamp if (err) 1641470decc6SDave Kleikamp return err; 1642470decc6SDave Kleikamp 1643470decc6SDave Kleikamp sb = journal->j_superblock; 1644470decc6SDave Kleikamp 1645470decc6SDave Kleikamp switch (be32_to_cpu(sb->s_header.h_blocktype)) { 1646f7f4bccbSMingming Cao case JBD2_SUPERBLOCK_V2: 1647470decc6SDave Kleikamp return 0; 1648f7f4bccbSMingming Cao case JBD2_SUPERBLOCK_V1: 1649470decc6SDave Kleikamp return journal_convert_superblock_v1(journal, sb); 1650470decc6SDave Kleikamp default: 1651470decc6SDave Kleikamp break; 1652470decc6SDave Kleikamp } 1653470decc6SDave Kleikamp return -EINVAL; 1654470decc6SDave Kleikamp } 1655470decc6SDave Kleikamp 1656470decc6SDave Kleikamp static int journal_convert_superblock_v1(journal_t *journal, 1657470decc6SDave Kleikamp journal_superblock_t *sb) 1658470decc6SDave Kleikamp { 1659470decc6SDave Kleikamp int offset, blocksize; 1660470decc6SDave Kleikamp struct buffer_head *bh; 1661470decc6SDave Kleikamp 1662470decc6SDave Kleikamp printk(KERN_WARNING 1663470decc6SDave Kleikamp "JBD: Converting superblock from version 1 to 2.\n"); 1664470decc6SDave Kleikamp 1665470decc6SDave Kleikamp /* Pre-initialise new fields to zero */ 1666470decc6SDave Kleikamp offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb); 1667470decc6SDave Kleikamp blocksize = be32_to_cpu(sb->s_blocksize); 1668470decc6SDave Kleikamp memset(&sb->s_feature_compat, 0, blocksize-offset); 1669470decc6SDave Kleikamp 1670470decc6SDave Kleikamp sb->s_nr_users = cpu_to_be32(1); 1671f7f4bccbSMingming Cao sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2); 1672470decc6SDave Kleikamp journal->j_format_version = 2; 1673470decc6SDave Kleikamp 1674470decc6SDave Kleikamp bh = journal->j_sb_buffer; 1675470decc6SDave Kleikamp BUFFER_TRACE(bh, "marking dirty"); 1676470decc6SDave Kleikamp mark_buffer_dirty(bh); 1677470decc6SDave Kleikamp sync_dirty_buffer(bh); 1678470decc6SDave Kleikamp return 0; 1679470decc6SDave Kleikamp } 1680470decc6SDave Kleikamp 1681470decc6SDave Kleikamp 1682470decc6SDave Kleikamp /** 1683f7f4bccbSMingming Cao * int jbd2_journal_flush () - Flush journal 1684470decc6SDave Kleikamp * @journal: Journal to act on. 1685470decc6SDave Kleikamp * 1686470decc6SDave Kleikamp * Flush all data for a given journal to disk and empty the journal. 1687470decc6SDave Kleikamp * Filesystems can use this when remounting readonly to ensure that 1688470decc6SDave Kleikamp * recovery does not need to happen on remount. 1689470decc6SDave Kleikamp */ 1690470decc6SDave Kleikamp 1691f7f4bccbSMingming Cao int jbd2_journal_flush(journal_t *journal) 1692470decc6SDave Kleikamp { 1693470decc6SDave Kleikamp int err = 0; 1694470decc6SDave Kleikamp transaction_t *transaction = NULL; 1695470decc6SDave Kleikamp unsigned long old_tail; 1696470decc6SDave Kleikamp 1697470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 1698470decc6SDave Kleikamp 1699470decc6SDave Kleikamp /* Force everything buffered to the log... */ 1700470decc6SDave Kleikamp if (journal->j_running_transaction) { 1701470decc6SDave Kleikamp transaction = journal->j_running_transaction; 1702f7f4bccbSMingming Cao __jbd2_log_start_commit(journal, transaction->t_tid); 1703470decc6SDave Kleikamp } else if (journal->j_committing_transaction) 1704470decc6SDave Kleikamp transaction = journal->j_committing_transaction; 1705470decc6SDave Kleikamp 1706470decc6SDave Kleikamp /* Wait for the log commit to complete... */ 1707470decc6SDave Kleikamp if (transaction) { 1708470decc6SDave Kleikamp tid_t tid = transaction->t_tid; 1709470decc6SDave Kleikamp 1710470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1711f7f4bccbSMingming Cao jbd2_log_wait_commit(journal, tid); 1712470decc6SDave Kleikamp } else { 1713470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1714470decc6SDave Kleikamp } 1715470decc6SDave Kleikamp 1716470decc6SDave Kleikamp /* ...and flush everything in the log out to disk. */ 1717470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 1718470decc6SDave Kleikamp while (!err && journal->j_checkpoint_transactions != NULL) { 1719470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 1720f7f4bccbSMingming Cao err = jbd2_log_do_checkpoint(journal); 1721470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 1722470decc6SDave Kleikamp } 1723470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 1724f7f4bccbSMingming Cao jbd2_cleanup_journal_tail(journal); 1725470decc6SDave Kleikamp 1726470decc6SDave Kleikamp /* Finally, mark the journal as really needing no recovery. 1727470decc6SDave Kleikamp * This sets s_start==0 in the underlying superblock, which is 1728470decc6SDave Kleikamp * the magic code for a fully-recovered superblock. Any future 1729470decc6SDave Kleikamp * commits of data to the journal will restore the current 1730470decc6SDave Kleikamp * s_start value. */ 1731470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 1732470decc6SDave Kleikamp old_tail = journal->j_tail; 1733470decc6SDave Kleikamp journal->j_tail = 0; 1734470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1735f7f4bccbSMingming Cao jbd2_journal_update_superblock(journal, 1); 1736470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 1737470decc6SDave Kleikamp journal->j_tail = old_tail; 1738470decc6SDave Kleikamp 1739470decc6SDave Kleikamp J_ASSERT(!journal->j_running_transaction); 1740470decc6SDave Kleikamp J_ASSERT(!journal->j_committing_transaction); 1741470decc6SDave Kleikamp J_ASSERT(!journal->j_checkpoint_transactions); 1742470decc6SDave Kleikamp J_ASSERT(journal->j_head == journal->j_tail); 1743470decc6SDave Kleikamp J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence); 1744470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1745470decc6SDave Kleikamp return err; 1746470decc6SDave Kleikamp } 1747470decc6SDave Kleikamp 1748470decc6SDave Kleikamp /** 1749f7f4bccbSMingming Cao * int jbd2_journal_wipe() - Wipe journal contents 1750470decc6SDave Kleikamp * @journal: Journal to act on. 1751470decc6SDave Kleikamp * @write: flag (see below) 1752470decc6SDave Kleikamp * 1753470decc6SDave Kleikamp * Wipe out all of the contents of a journal, safely. This will produce 1754470decc6SDave Kleikamp * a warning if the journal contains any valid recovery information. 1755f7f4bccbSMingming Cao * Must be called between journal_init_*() and jbd2_journal_load(). 1756470decc6SDave Kleikamp * 1757470decc6SDave Kleikamp * If 'write' is non-zero, then we wipe out the journal on disk; otherwise 1758470decc6SDave Kleikamp * we merely suppress recovery. 1759470decc6SDave Kleikamp */ 1760470decc6SDave Kleikamp 1761f7f4bccbSMingming Cao int jbd2_journal_wipe(journal_t *journal, int write) 1762470decc6SDave Kleikamp { 1763470decc6SDave Kleikamp journal_superblock_t *sb; 1764470decc6SDave Kleikamp int err = 0; 1765470decc6SDave Kleikamp 1766f7f4bccbSMingming Cao J_ASSERT (!(journal->j_flags & JBD2_LOADED)); 1767470decc6SDave Kleikamp 1768470decc6SDave Kleikamp err = load_superblock(journal); 1769470decc6SDave Kleikamp if (err) 1770470decc6SDave Kleikamp return err; 1771470decc6SDave Kleikamp 1772470decc6SDave Kleikamp sb = journal->j_superblock; 1773470decc6SDave Kleikamp 1774470decc6SDave Kleikamp if (!journal->j_tail) 1775470decc6SDave Kleikamp goto no_recovery; 1776470decc6SDave Kleikamp 1777470decc6SDave Kleikamp printk (KERN_WARNING "JBD: %s recovery information on journal\n", 1778470decc6SDave Kleikamp write ? "Clearing" : "Ignoring"); 1779470decc6SDave Kleikamp 1780f7f4bccbSMingming Cao err = jbd2_journal_skip_recovery(journal); 1781470decc6SDave Kleikamp if (write) 1782f7f4bccbSMingming Cao jbd2_journal_update_superblock(journal, 1); 1783470decc6SDave Kleikamp 1784470decc6SDave Kleikamp no_recovery: 1785470decc6SDave Kleikamp return err; 1786470decc6SDave Kleikamp } 1787470decc6SDave Kleikamp 1788470decc6SDave Kleikamp /* 1789470decc6SDave Kleikamp * Journal abort has very specific semantics, which we describe 1790470decc6SDave Kleikamp * for journal abort. 1791470decc6SDave Kleikamp * 1792470decc6SDave Kleikamp * Two internal function, which provide abort to te jbd layer 1793470decc6SDave Kleikamp * itself are here. 1794470decc6SDave Kleikamp */ 1795470decc6SDave Kleikamp 1796470decc6SDave Kleikamp /* 1797470decc6SDave Kleikamp * Quick version for internal journal use (doesn't lock the journal). 1798470decc6SDave Kleikamp * Aborts hard --- we mark the abort as occurred, but do _nothing_ else, 1799470decc6SDave Kleikamp * and don't attempt to make any other journal updates. 1800470decc6SDave Kleikamp */ 1801f7f4bccbSMingming Cao void __jbd2_journal_abort_hard(journal_t *journal) 1802470decc6SDave Kleikamp { 1803470decc6SDave Kleikamp transaction_t *transaction; 1804470decc6SDave Kleikamp 1805f7f4bccbSMingming Cao if (journal->j_flags & JBD2_ABORT) 1806470decc6SDave Kleikamp return; 1807470decc6SDave Kleikamp 1808470decc6SDave Kleikamp printk(KERN_ERR "Aborting journal on device %s.\n", 180905496769STheodore Ts'o journal->j_devname); 1810470decc6SDave Kleikamp 1811470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 1812f7f4bccbSMingming Cao journal->j_flags |= JBD2_ABORT; 1813470decc6SDave Kleikamp transaction = journal->j_running_transaction; 1814470decc6SDave Kleikamp if (transaction) 1815f7f4bccbSMingming Cao __jbd2_log_start_commit(journal, transaction->t_tid); 1816470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1817470decc6SDave Kleikamp } 1818470decc6SDave Kleikamp 1819470decc6SDave Kleikamp /* Soft abort: record the abort error status in the journal superblock, 1820470decc6SDave Kleikamp * but don't do any other IO. */ 1821470decc6SDave Kleikamp static void __journal_abort_soft (journal_t *journal, int errno) 1822470decc6SDave Kleikamp { 1823f7f4bccbSMingming Cao if (journal->j_flags & JBD2_ABORT) 1824470decc6SDave Kleikamp return; 1825470decc6SDave Kleikamp 1826470decc6SDave Kleikamp if (!journal->j_errno) 1827470decc6SDave Kleikamp journal->j_errno = errno; 1828470decc6SDave Kleikamp 1829f7f4bccbSMingming Cao __jbd2_journal_abort_hard(journal); 1830470decc6SDave Kleikamp 1831470decc6SDave Kleikamp if (errno) 1832f7f4bccbSMingming Cao jbd2_journal_update_superblock(journal, 1); 1833470decc6SDave Kleikamp } 1834470decc6SDave Kleikamp 1835470decc6SDave Kleikamp /** 1836f7f4bccbSMingming Cao * void jbd2_journal_abort () - Shutdown the journal immediately. 1837470decc6SDave Kleikamp * @journal: the journal to shutdown. 1838470decc6SDave Kleikamp * @errno: an error number to record in the journal indicating 1839470decc6SDave Kleikamp * the reason for the shutdown. 1840470decc6SDave Kleikamp * 1841470decc6SDave Kleikamp * Perform a complete, immediate shutdown of the ENTIRE 1842470decc6SDave Kleikamp * journal (not of a single transaction). This operation cannot be 1843470decc6SDave Kleikamp * undone without closing and reopening the journal. 1844470decc6SDave Kleikamp * 1845f7f4bccbSMingming Cao * The jbd2_journal_abort function is intended to support higher level error 1846470decc6SDave Kleikamp * recovery mechanisms such as the ext2/ext3 remount-readonly error 1847470decc6SDave Kleikamp * mode. 1848470decc6SDave Kleikamp * 1849470decc6SDave Kleikamp * Journal abort has very specific semantics. Any existing dirty, 1850470decc6SDave Kleikamp * unjournaled buffers in the main filesystem will still be written to 1851470decc6SDave Kleikamp * disk by bdflush, but the journaling mechanism will be suspended 1852470decc6SDave Kleikamp * immediately and no further transaction commits will be honoured. 1853470decc6SDave Kleikamp * 1854470decc6SDave Kleikamp * Any dirty, journaled buffers will be written back to disk without 1855470decc6SDave Kleikamp * hitting the journal. Atomicity cannot be guaranteed on an aborted 1856470decc6SDave Kleikamp * filesystem, but we _do_ attempt to leave as much data as possible 1857470decc6SDave Kleikamp * behind for fsck to use for cleanup. 1858470decc6SDave Kleikamp * 1859470decc6SDave Kleikamp * Any attempt to get a new transaction handle on a journal which is in 1860470decc6SDave Kleikamp * ABORT state will just result in an -EROFS error return. A 1861f7f4bccbSMingming Cao * jbd2_journal_stop on an existing handle will return -EIO if we have 1862470decc6SDave Kleikamp * entered abort state during the update. 1863470decc6SDave Kleikamp * 1864470decc6SDave Kleikamp * Recursive transactions are not disturbed by journal abort until the 1865f7f4bccbSMingming Cao * final jbd2_journal_stop, which will receive the -EIO error. 1866470decc6SDave Kleikamp * 1867f7f4bccbSMingming Cao * Finally, the jbd2_journal_abort call allows the caller to supply an errno 1868470decc6SDave Kleikamp * which will be recorded (if possible) in the journal superblock. This 1869470decc6SDave Kleikamp * allows a client to record failure conditions in the middle of a 1870470decc6SDave Kleikamp * transaction without having to complete the transaction to record the 1871470decc6SDave Kleikamp * failure to disk. ext3_error, for example, now uses this 1872470decc6SDave Kleikamp * functionality. 1873470decc6SDave Kleikamp * 1874470decc6SDave Kleikamp * Errors which originate from within the journaling layer will NOT 1875470decc6SDave Kleikamp * supply an errno; a null errno implies that absolutely no further 1876470decc6SDave Kleikamp * writes are done to the journal (unless there are any already in 1877470decc6SDave Kleikamp * progress). 1878470decc6SDave Kleikamp * 1879470decc6SDave Kleikamp */ 1880470decc6SDave Kleikamp 1881f7f4bccbSMingming Cao void jbd2_journal_abort(journal_t *journal, int errno) 1882470decc6SDave Kleikamp { 1883470decc6SDave Kleikamp __journal_abort_soft(journal, errno); 1884470decc6SDave Kleikamp } 1885470decc6SDave Kleikamp 1886470decc6SDave Kleikamp /** 1887f7f4bccbSMingming Cao * int jbd2_journal_errno () - returns the journal's error state. 1888470decc6SDave Kleikamp * @journal: journal to examine. 1889470decc6SDave Kleikamp * 1890f7f4bccbSMingming Cao * This is the errno numbet set with jbd2_journal_abort(), the last 1891470decc6SDave Kleikamp * time the journal was mounted - if the journal was stopped 1892470decc6SDave Kleikamp * without calling abort this will be 0. 1893470decc6SDave Kleikamp * 1894470decc6SDave Kleikamp * If the journal has been aborted on this mount time -EROFS will 1895470decc6SDave Kleikamp * be returned. 1896470decc6SDave Kleikamp */ 1897f7f4bccbSMingming Cao int jbd2_journal_errno(journal_t *journal) 1898470decc6SDave Kleikamp { 1899470decc6SDave Kleikamp int err; 1900470decc6SDave Kleikamp 1901470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 1902f7f4bccbSMingming Cao if (journal->j_flags & JBD2_ABORT) 1903470decc6SDave Kleikamp err = -EROFS; 1904470decc6SDave Kleikamp else 1905470decc6SDave Kleikamp err = journal->j_errno; 1906470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1907470decc6SDave Kleikamp return err; 1908470decc6SDave Kleikamp } 1909470decc6SDave Kleikamp 1910470decc6SDave Kleikamp /** 1911f7f4bccbSMingming Cao * int jbd2_journal_clear_err () - clears the journal's error state 1912470decc6SDave Kleikamp * @journal: journal to act on. 1913470decc6SDave Kleikamp * 1914470decc6SDave Kleikamp * An error must be cleared or Acked to take a FS out of readonly 1915470decc6SDave Kleikamp * mode. 1916470decc6SDave Kleikamp */ 1917f7f4bccbSMingming Cao int jbd2_journal_clear_err(journal_t *journal) 1918470decc6SDave Kleikamp { 1919470decc6SDave Kleikamp int err = 0; 1920470decc6SDave Kleikamp 1921470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 1922f7f4bccbSMingming Cao if (journal->j_flags & JBD2_ABORT) 1923470decc6SDave Kleikamp err = -EROFS; 1924470decc6SDave Kleikamp else 1925470decc6SDave Kleikamp journal->j_errno = 0; 1926470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1927470decc6SDave Kleikamp return err; 1928470decc6SDave Kleikamp } 1929470decc6SDave Kleikamp 1930470decc6SDave Kleikamp /** 1931f7f4bccbSMingming Cao * void jbd2_journal_ack_err() - Ack journal err. 1932470decc6SDave Kleikamp * @journal: journal to act on. 1933470decc6SDave Kleikamp * 1934470decc6SDave Kleikamp * An error must be cleared or Acked to take a FS out of readonly 1935470decc6SDave Kleikamp * mode. 1936470decc6SDave Kleikamp */ 1937f7f4bccbSMingming Cao void jbd2_journal_ack_err(journal_t *journal) 1938470decc6SDave Kleikamp { 1939470decc6SDave Kleikamp spin_lock(&journal->j_state_lock); 1940470decc6SDave Kleikamp if (journal->j_errno) 1941f7f4bccbSMingming Cao journal->j_flags |= JBD2_ACK_ERR; 1942470decc6SDave Kleikamp spin_unlock(&journal->j_state_lock); 1943470decc6SDave Kleikamp } 1944470decc6SDave Kleikamp 1945f7f4bccbSMingming Cao int jbd2_journal_blocks_per_page(struct inode *inode) 1946470decc6SDave Kleikamp { 1947470decc6SDave Kleikamp return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); 1948470decc6SDave Kleikamp } 1949470decc6SDave Kleikamp 1950470decc6SDave Kleikamp /* 1951b517bea1SZach Brown * helper functions to deal with 32 or 64bit block numbers. 1952b517bea1SZach Brown */ 1953b517bea1SZach Brown size_t journal_tag_bytes(journal_t *journal) 1954b517bea1SZach Brown { 1955b517bea1SZach Brown if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT)) 1956cd02ff0bSMingming Cao return JBD2_TAG_SIZE64; 1957b517bea1SZach Brown else 1958cd02ff0bSMingming Cao return JBD2_TAG_SIZE32; 1959b517bea1SZach Brown } 1960b517bea1SZach Brown 1961b517bea1SZach Brown /* 1962470decc6SDave Kleikamp * Journal_head storage management 1963470decc6SDave Kleikamp */ 1964e18b890bSChristoph Lameter static struct kmem_cache *jbd2_journal_head_cache; 1965e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 1966470decc6SDave Kleikamp static atomic_t nr_journal_heads = ATOMIC_INIT(0); 1967470decc6SDave Kleikamp #endif 1968470decc6SDave Kleikamp 1969f7f4bccbSMingming Cao static int journal_init_jbd2_journal_head_cache(void) 1970470decc6SDave Kleikamp { 1971470decc6SDave Kleikamp int retval; 1972470decc6SDave Kleikamp 19731076d17aSAl Viro J_ASSERT(jbd2_journal_head_cache == NULL); 1974a920e941SJohann Lombardi jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head", 1975470decc6SDave Kleikamp sizeof(struct journal_head), 1976470decc6SDave Kleikamp 0, /* offset */ 197777160957SMingming Cao SLAB_TEMPORARY, /* flags */ 197820c2df83SPaul Mundt NULL); /* ctor */ 1979470decc6SDave Kleikamp retval = 0; 19801076d17aSAl Viro if (!jbd2_journal_head_cache) { 1981470decc6SDave Kleikamp retval = -ENOMEM; 1982470decc6SDave Kleikamp printk(KERN_EMERG "JBD: no memory for journal_head cache\n"); 1983470decc6SDave Kleikamp } 1984470decc6SDave Kleikamp return retval; 1985470decc6SDave Kleikamp } 1986470decc6SDave Kleikamp 1987f7f4bccbSMingming Cao static void jbd2_journal_destroy_jbd2_journal_head_cache(void) 1988470decc6SDave Kleikamp { 19898a9362ebSDuane Griffin if (jbd2_journal_head_cache) { 1990f7f4bccbSMingming Cao kmem_cache_destroy(jbd2_journal_head_cache); 1991f7f4bccbSMingming Cao jbd2_journal_head_cache = NULL; 1992470decc6SDave Kleikamp } 19938a9362ebSDuane Griffin } 1994470decc6SDave Kleikamp 1995470decc6SDave Kleikamp /* 1996470decc6SDave Kleikamp * journal_head splicing and dicing 1997470decc6SDave Kleikamp */ 1998470decc6SDave Kleikamp static struct journal_head *journal_alloc_journal_head(void) 1999470decc6SDave Kleikamp { 2000470decc6SDave Kleikamp struct journal_head *ret; 2001470decc6SDave Kleikamp static unsigned long last_warning; 2002470decc6SDave Kleikamp 2003e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 2004470decc6SDave Kleikamp atomic_inc(&nr_journal_heads); 2005470decc6SDave Kleikamp #endif 2006f7f4bccbSMingming Cao ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS); 20071076d17aSAl Viro if (!ret) { 2008470decc6SDave Kleikamp jbd_debug(1, "out of memory for journal_head\n"); 2009470decc6SDave Kleikamp if (time_after(jiffies, last_warning + 5*HZ)) { 2010470decc6SDave Kleikamp printk(KERN_NOTICE "ENOMEM in %s, retrying.\n", 2011329d291fSHarvey Harrison __func__); 2012470decc6SDave Kleikamp last_warning = jiffies; 2013470decc6SDave Kleikamp } 20141076d17aSAl Viro while (!ret) { 2015470decc6SDave Kleikamp yield(); 2016f7f4bccbSMingming Cao ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS); 2017470decc6SDave Kleikamp } 2018470decc6SDave Kleikamp } 2019470decc6SDave Kleikamp return ret; 2020470decc6SDave Kleikamp } 2021470decc6SDave Kleikamp 2022470decc6SDave Kleikamp static void journal_free_journal_head(struct journal_head *jh) 2023470decc6SDave Kleikamp { 2024e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 2025470decc6SDave Kleikamp atomic_dec(&nr_journal_heads); 2026cd02ff0bSMingming Cao memset(jh, JBD2_POISON_FREE, sizeof(*jh)); 2027470decc6SDave Kleikamp #endif 2028f7f4bccbSMingming Cao kmem_cache_free(jbd2_journal_head_cache, jh); 2029470decc6SDave Kleikamp } 2030470decc6SDave Kleikamp 2031470decc6SDave Kleikamp /* 2032470decc6SDave Kleikamp * A journal_head is attached to a buffer_head whenever JBD has an 2033470decc6SDave Kleikamp * interest in the buffer. 2034470decc6SDave Kleikamp * 2035470decc6SDave Kleikamp * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit 2036470decc6SDave Kleikamp * is set. This bit is tested in core kernel code where we need to take 2037470decc6SDave Kleikamp * JBD-specific actions. Testing the zeroness of ->b_private is not reliable 2038470decc6SDave Kleikamp * there. 2039470decc6SDave Kleikamp * 2040470decc6SDave Kleikamp * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one. 2041470decc6SDave Kleikamp * 2042470decc6SDave Kleikamp * When a buffer has its BH_JBD bit set it is immune from being released by 2043470decc6SDave Kleikamp * core kernel code, mainly via ->b_count. 2044470decc6SDave Kleikamp * 2045470decc6SDave Kleikamp * A journal_head may be detached from its buffer_head when the journal_head's 2046470decc6SDave Kleikamp * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL. 2047f7f4bccbSMingming Cao * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the 2048470decc6SDave Kleikamp * journal_head can be dropped if needed. 2049470decc6SDave Kleikamp * 2050470decc6SDave Kleikamp * Various places in the kernel want to attach a journal_head to a buffer_head 2051470decc6SDave Kleikamp * _before_ attaching the journal_head to a transaction. To protect the 2052f7f4bccbSMingming Cao * journal_head in this situation, jbd2_journal_add_journal_head elevates the 2053470decc6SDave Kleikamp * journal_head's b_jcount refcount by one. The caller must call 2054f7f4bccbSMingming Cao * jbd2_journal_put_journal_head() to undo this. 2055470decc6SDave Kleikamp * 2056470decc6SDave Kleikamp * So the typical usage would be: 2057470decc6SDave Kleikamp * 2058470decc6SDave Kleikamp * (Attach a journal_head if needed. Increments b_jcount) 2059f7f4bccbSMingming Cao * struct journal_head *jh = jbd2_journal_add_journal_head(bh); 2060470decc6SDave Kleikamp * ... 2061470decc6SDave Kleikamp * jh->b_transaction = xxx; 2062f7f4bccbSMingming Cao * jbd2_journal_put_journal_head(jh); 2063470decc6SDave Kleikamp * 2064470decc6SDave Kleikamp * Now, the journal_head's b_jcount is zero, but it is safe from being released 2065470decc6SDave Kleikamp * because it has a non-zero b_transaction. 2066470decc6SDave Kleikamp */ 2067470decc6SDave Kleikamp 2068470decc6SDave Kleikamp /* 2069470decc6SDave Kleikamp * Give a buffer_head a journal_head. 2070470decc6SDave Kleikamp * 2071470decc6SDave Kleikamp * Doesn't need the journal lock. 2072470decc6SDave Kleikamp * May sleep. 2073470decc6SDave Kleikamp */ 2074f7f4bccbSMingming Cao struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh) 2075470decc6SDave Kleikamp { 2076470decc6SDave Kleikamp struct journal_head *jh; 2077470decc6SDave Kleikamp struct journal_head *new_jh = NULL; 2078470decc6SDave Kleikamp 2079470decc6SDave Kleikamp repeat: 2080470decc6SDave Kleikamp if (!buffer_jbd(bh)) { 2081470decc6SDave Kleikamp new_jh = journal_alloc_journal_head(); 2082470decc6SDave Kleikamp memset(new_jh, 0, sizeof(*new_jh)); 2083470decc6SDave Kleikamp } 2084470decc6SDave Kleikamp 2085470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 2086470decc6SDave Kleikamp if (buffer_jbd(bh)) { 2087470decc6SDave Kleikamp jh = bh2jh(bh); 2088470decc6SDave Kleikamp } else { 2089470decc6SDave Kleikamp J_ASSERT_BH(bh, 2090470decc6SDave Kleikamp (atomic_read(&bh->b_count) > 0) || 2091470decc6SDave Kleikamp (bh->b_page && bh->b_page->mapping)); 2092470decc6SDave Kleikamp 2093470decc6SDave Kleikamp if (!new_jh) { 2094470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 2095470decc6SDave Kleikamp goto repeat; 2096470decc6SDave Kleikamp } 2097470decc6SDave Kleikamp 2098470decc6SDave Kleikamp jh = new_jh; 2099470decc6SDave Kleikamp new_jh = NULL; /* We consumed it */ 2100470decc6SDave Kleikamp set_buffer_jbd(bh); 2101470decc6SDave Kleikamp bh->b_private = jh; 2102470decc6SDave Kleikamp jh->b_bh = bh; 2103470decc6SDave Kleikamp get_bh(bh); 2104470decc6SDave Kleikamp BUFFER_TRACE(bh, "added journal_head"); 2105470decc6SDave Kleikamp } 2106470decc6SDave Kleikamp jh->b_jcount++; 2107470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 2108470decc6SDave Kleikamp if (new_jh) 2109470decc6SDave Kleikamp journal_free_journal_head(new_jh); 2110470decc6SDave Kleikamp return bh->b_private; 2111470decc6SDave Kleikamp } 2112470decc6SDave Kleikamp 2113470decc6SDave Kleikamp /* 2114470decc6SDave Kleikamp * Grab a ref against this buffer_head's journal_head. If it ended up not 2115470decc6SDave Kleikamp * having a journal_head, return NULL 2116470decc6SDave Kleikamp */ 2117f7f4bccbSMingming Cao struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh) 2118470decc6SDave Kleikamp { 2119470decc6SDave Kleikamp struct journal_head *jh = NULL; 2120470decc6SDave Kleikamp 2121470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 2122470decc6SDave Kleikamp if (buffer_jbd(bh)) { 2123470decc6SDave Kleikamp jh = bh2jh(bh); 2124470decc6SDave Kleikamp jh->b_jcount++; 2125470decc6SDave Kleikamp } 2126470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 2127470decc6SDave Kleikamp return jh; 2128470decc6SDave Kleikamp } 2129470decc6SDave Kleikamp 2130470decc6SDave Kleikamp static void __journal_remove_journal_head(struct buffer_head *bh) 2131470decc6SDave Kleikamp { 2132470decc6SDave Kleikamp struct journal_head *jh = bh2jh(bh); 2133470decc6SDave Kleikamp 2134470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_jcount >= 0); 2135470decc6SDave Kleikamp 2136470decc6SDave Kleikamp get_bh(bh); 2137470decc6SDave Kleikamp if (jh->b_jcount == 0) { 2138470decc6SDave Kleikamp if (jh->b_transaction == NULL && 2139470decc6SDave Kleikamp jh->b_next_transaction == NULL && 2140470decc6SDave Kleikamp jh->b_cp_transaction == NULL) { 2141470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_jlist == BJ_None); 2142470decc6SDave Kleikamp J_ASSERT_BH(bh, buffer_jbd(bh)); 2143470decc6SDave Kleikamp J_ASSERT_BH(bh, jh2bh(jh) == bh); 2144470decc6SDave Kleikamp BUFFER_TRACE(bh, "remove journal_head"); 2145470decc6SDave Kleikamp if (jh->b_frozen_data) { 2146470decc6SDave Kleikamp printk(KERN_WARNING "%s: freeing " 2147470decc6SDave Kleikamp "b_frozen_data\n", 2148329d291fSHarvey Harrison __func__); 2149af1e76d6SMingming Cao jbd2_free(jh->b_frozen_data, bh->b_size); 2150470decc6SDave Kleikamp } 2151470decc6SDave Kleikamp if (jh->b_committed_data) { 2152470decc6SDave Kleikamp printk(KERN_WARNING "%s: freeing " 2153470decc6SDave Kleikamp "b_committed_data\n", 2154329d291fSHarvey Harrison __func__); 2155af1e76d6SMingming Cao jbd2_free(jh->b_committed_data, bh->b_size); 2156470decc6SDave Kleikamp } 2157470decc6SDave Kleikamp bh->b_private = NULL; 2158470decc6SDave Kleikamp jh->b_bh = NULL; /* debug, really */ 2159470decc6SDave Kleikamp clear_buffer_jbd(bh); 2160470decc6SDave Kleikamp __brelse(bh); 2161470decc6SDave Kleikamp journal_free_journal_head(jh); 2162470decc6SDave Kleikamp } else { 2163470decc6SDave Kleikamp BUFFER_TRACE(bh, "journal_head was locked"); 2164470decc6SDave Kleikamp } 2165470decc6SDave Kleikamp } 2166470decc6SDave Kleikamp } 2167470decc6SDave Kleikamp 2168470decc6SDave Kleikamp /* 2169f7f4bccbSMingming Cao * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction 2170470decc6SDave Kleikamp * and has a zero b_jcount then remove and release its journal_head. If we did 2171470decc6SDave Kleikamp * see that the buffer is not used by any transaction we also "logically" 2172470decc6SDave Kleikamp * decrement ->b_count. 2173470decc6SDave Kleikamp * 2174470decc6SDave Kleikamp * We in fact take an additional increment on ->b_count as a convenience, 2175470decc6SDave Kleikamp * because the caller usually wants to do additional things with the bh 2176470decc6SDave Kleikamp * after calling here. 2177f7f4bccbSMingming Cao * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some 2178470decc6SDave Kleikamp * time. Once the caller has run __brelse(), the buffer is eligible for 2179470decc6SDave Kleikamp * reaping by try_to_free_buffers(). 2180470decc6SDave Kleikamp */ 2181f7f4bccbSMingming Cao void jbd2_journal_remove_journal_head(struct buffer_head *bh) 2182470decc6SDave Kleikamp { 2183470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 2184470decc6SDave Kleikamp __journal_remove_journal_head(bh); 2185470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 2186470decc6SDave Kleikamp } 2187470decc6SDave Kleikamp 2188470decc6SDave Kleikamp /* 2189470decc6SDave Kleikamp * Drop a reference on the passed journal_head. If it fell to zero then try to 2190470decc6SDave Kleikamp * release the journal_head from the buffer_head. 2191470decc6SDave Kleikamp */ 2192f7f4bccbSMingming Cao void jbd2_journal_put_journal_head(struct journal_head *jh) 2193470decc6SDave Kleikamp { 2194470decc6SDave Kleikamp struct buffer_head *bh = jh2bh(jh); 2195470decc6SDave Kleikamp 2196470decc6SDave Kleikamp jbd_lock_bh_journal_head(bh); 2197470decc6SDave Kleikamp J_ASSERT_JH(jh, jh->b_jcount > 0); 2198470decc6SDave Kleikamp --jh->b_jcount; 2199470decc6SDave Kleikamp if (!jh->b_jcount && !jh->b_transaction) { 2200470decc6SDave Kleikamp __journal_remove_journal_head(bh); 2201470decc6SDave Kleikamp __brelse(bh); 2202470decc6SDave Kleikamp } 2203470decc6SDave Kleikamp jbd_unlock_bh_journal_head(bh); 2204470decc6SDave Kleikamp } 2205470decc6SDave Kleikamp 2206470decc6SDave Kleikamp /* 2207c851ed54SJan Kara * Initialize jbd inode head 2208c851ed54SJan Kara */ 2209c851ed54SJan Kara void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode) 2210c851ed54SJan Kara { 2211c851ed54SJan Kara jinode->i_transaction = NULL; 2212c851ed54SJan Kara jinode->i_next_transaction = NULL; 2213c851ed54SJan Kara jinode->i_vfs_inode = inode; 2214c851ed54SJan Kara jinode->i_flags = 0; 2215c851ed54SJan Kara INIT_LIST_HEAD(&jinode->i_list); 2216c851ed54SJan Kara } 2217c851ed54SJan Kara 2218c851ed54SJan Kara /* 2219c851ed54SJan Kara * Function to be called before we start removing inode from memory (i.e., 2220c851ed54SJan Kara * clear_inode() is a fine place to be called from). It removes inode from 2221c851ed54SJan Kara * transaction's lists. 2222c851ed54SJan Kara */ 2223c851ed54SJan Kara void jbd2_journal_release_jbd_inode(journal_t *journal, 2224c851ed54SJan Kara struct jbd2_inode *jinode) 2225c851ed54SJan Kara { 2226c851ed54SJan Kara int writeout = 0; 2227c851ed54SJan Kara 2228c851ed54SJan Kara if (!journal) 2229c851ed54SJan Kara return; 2230c851ed54SJan Kara restart: 2231c851ed54SJan Kara spin_lock(&journal->j_list_lock); 2232c851ed54SJan Kara /* Is commit writing out inode - we have to wait */ 2233c851ed54SJan Kara if (jinode->i_flags & JI_COMMIT_RUNNING) { 2234c851ed54SJan Kara wait_queue_head_t *wq; 2235c851ed54SJan Kara DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING); 2236c851ed54SJan Kara wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING); 2237c851ed54SJan Kara prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE); 2238c851ed54SJan Kara spin_unlock(&journal->j_list_lock); 2239c851ed54SJan Kara schedule(); 2240c851ed54SJan Kara finish_wait(wq, &wait.wait); 2241c851ed54SJan Kara goto restart; 2242c851ed54SJan Kara } 2243c851ed54SJan Kara 2244c851ed54SJan Kara /* Do we need to wait for data writeback? */ 2245c851ed54SJan Kara if (journal->j_committing_transaction == jinode->i_transaction) 2246c851ed54SJan Kara writeout = 1; 2247c851ed54SJan Kara if (jinode->i_transaction) { 2248c851ed54SJan Kara list_del(&jinode->i_list); 2249c851ed54SJan Kara jinode->i_transaction = NULL; 2250c851ed54SJan Kara } 2251c851ed54SJan Kara spin_unlock(&journal->j_list_lock); 2252c851ed54SJan Kara } 2253c851ed54SJan Kara 2254c851ed54SJan Kara /* 22550f49d5d0SJose R. Santos * debugfs tunables 2256470decc6SDave Kleikamp */ 22576f38c74fSJose R. Santos #ifdef CONFIG_JBD2_DEBUG 22586f38c74fSJose R. Santos u8 jbd2_journal_enable_debug __read_mostly; 2259f7f4bccbSMingming Cao EXPORT_SYMBOL(jbd2_journal_enable_debug); 2260470decc6SDave Kleikamp 22610f49d5d0SJose R. Santos #define JBD2_DEBUG_NAME "jbd2-debug" 2262470decc6SDave Kleikamp 22636f38c74fSJose R. Santos static struct dentry *jbd2_debugfs_dir; 22646f38c74fSJose R. Santos static struct dentry *jbd2_debug; 22650f49d5d0SJose R. Santos 22660f49d5d0SJose R. Santos static void __init jbd2_create_debugfs_entry(void) 2267470decc6SDave Kleikamp { 22680f49d5d0SJose R. Santos jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL); 22690f49d5d0SJose R. Santos if (jbd2_debugfs_dir) 22700f49d5d0SJose R. Santos jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME, S_IRUGO, 22710f49d5d0SJose R. Santos jbd2_debugfs_dir, 22720f49d5d0SJose R. Santos &jbd2_journal_enable_debug); 2273470decc6SDave Kleikamp } 2274470decc6SDave Kleikamp 22750f49d5d0SJose R. Santos static void __exit jbd2_remove_debugfs_entry(void) 2276470decc6SDave Kleikamp { 22770f49d5d0SJose R. Santos debugfs_remove(jbd2_debug); 22780f49d5d0SJose R. Santos debugfs_remove(jbd2_debugfs_dir); 2279470decc6SDave Kleikamp } 2280470decc6SDave Kleikamp 2281470decc6SDave Kleikamp #else 2282470decc6SDave Kleikamp 22830f49d5d0SJose R. Santos static void __init jbd2_create_debugfs_entry(void) 22840f49d5d0SJose R. Santos { 22850f49d5d0SJose R. Santos } 22860f49d5d0SJose R. Santos 22870f49d5d0SJose R. Santos static void __exit jbd2_remove_debugfs_entry(void) 22880f49d5d0SJose R. Santos { 22890f49d5d0SJose R. Santos } 2290470decc6SDave Kleikamp 2291470decc6SDave Kleikamp #endif 2292470decc6SDave Kleikamp 22938e85fb3fSJohann Lombardi #ifdef CONFIG_PROC_FS 22948e85fb3fSJohann Lombardi 22958e85fb3fSJohann Lombardi #define JBD2_STATS_PROC_NAME "fs/jbd2" 22968e85fb3fSJohann Lombardi 22978e85fb3fSJohann Lombardi static void __init jbd2_create_jbd_stats_proc_entry(void) 22988e85fb3fSJohann Lombardi { 22998e85fb3fSJohann Lombardi proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL); 23008e85fb3fSJohann Lombardi } 23018e85fb3fSJohann Lombardi 23028e85fb3fSJohann Lombardi static void __exit jbd2_remove_jbd_stats_proc_entry(void) 23038e85fb3fSJohann Lombardi { 23048e85fb3fSJohann Lombardi if (proc_jbd2_stats) 23058e85fb3fSJohann Lombardi remove_proc_entry(JBD2_STATS_PROC_NAME, NULL); 23068e85fb3fSJohann Lombardi } 23078e85fb3fSJohann Lombardi 23088e85fb3fSJohann Lombardi #else 23098e85fb3fSJohann Lombardi 23108e85fb3fSJohann Lombardi #define jbd2_create_jbd_stats_proc_entry() do {} while (0) 23118e85fb3fSJohann Lombardi #define jbd2_remove_jbd_stats_proc_entry() do {} while (0) 23128e85fb3fSJohann Lombardi 23138e85fb3fSJohann Lombardi #endif 23148e85fb3fSJohann Lombardi 2315e18b890bSChristoph Lameter struct kmem_cache *jbd2_handle_cache; 2316470decc6SDave Kleikamp 2317470decc6SDave Kleikamp static int __init journal_init_handle_cache(void) 2318470decc6SDave Kleikamp { 2319a920e941SJohann Lombardi jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle", 2320470decc6SDave Kleikamp sizeof(handle_t), 2321470decc6SDave Kleikamp 0, /* offset */ 232277160957SMingming Cao SLAB_TEMPORARY, /* flags */ 232320c2df83SPaul Mundt NULL); /* ctor */ 2324f7f4bccbSMingming Cao if (jbd2_handle_cache == NULL) { 2325470decc6SDave Kleikamp printk(KERN_EMERG "JBD: failed to create handle cache\n"); 2326470decc6SDave Kleikamp return -ENOMEM; 2327470decc6SDave Kleikamp } 2328470decc6SDave Kleikamp return 0; 2329470decc6SDave Kleikamp } 2330470decc6SDave Kleikamp 2331f7f4bccbSMingming Cao static void jbd2_journal_destroy_handle_cache(void) 2332470decc6SDave Kleikamp { 2333f7f4bccbSMingming Cao if (jbd2_handle_cache) 2334f7f4bccbSMingming Cao kmem_cache_destroy(jbd2_handle_cache); 2335470decc6SDave Kleikamp } 2336470decc6SDave Kleikamp 2337470decc6SDave Kleikamp /* 2338470decc6SDave Kleikamp * Module startup and shutdown 2339470decc6SDave Kleikamp */ 2340470decc6SDave Kleikamp 2341470decc6SDave Kleikamp static int __init journal_init_caches(void) 2342470decc6SDave Kleikamp { 2343470decc6SDave Kleikamp int ret; 2344470decc6SDave Kleikamp 2345f7f4bccbSMingming Cao ret = jbd2_journal_init_revoke_caches(); 2346470decc6SDave Kleikamp if (ret == 0) 2347f7f4bccbSMingming Cao ret = journal_init_jbd2_journal_head_cache(); 2348470decc6SDave Kleikamp if (ret == 0) 2349470decc6SDave Kleikamp ret = journal_init_handle_cache(); 2350470decc6SDave Kleikamp return ret; 2351470decc6SDave Kleikamp } 2352470decc6SDave Kleikamp 2353f7f4bccbSMingming Cao static void jbd2_journal_destroy_caches(void) 2354470decc6SDave Kleikamp { 2355f7f4bccbSMingming Cao jbd2_journal_destroy_revoke_caches(); 2356f7f4bccbSMingming Cao jbd2_journal_destroy_jbd2_journal_head_cache(); 2357f7f4bccbSMingming Cao jbd2_journal_destroy_handle_cache(); 2358470decc6SDave Kleikamp } 2359470decc6SDave Kleikamp 2360470decc6SDave Kleikamp static int __init journal_init(void) 2361470decc6SDave Kleikamp { 2362470decc6SDave Kleikamp int ret; 2363470decc6SDave Kleikamp 2364470decc6SDave Kleikamp BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024); 2365470decc6SDave Kleikamp 2366470decc6SDave Kleikamp ret = journal_init_caches(); 2367620de4e1SDuane Griffin if (ret == 0) { 23680f49d5d0SJose R. Santos jbd2_create_debugfs_entry(); 23698e85fb3fSJohann Lombardi jbd2_create_jbd_stats_proc_entry(); 2370620de4e1SDuane Griffin } else { 2371620de4e1SDuane Griffin jbd2_journal_destroy_caches(); 2372620de4e1SDuane Griffin } 2373470decc6SDave Kleikamp return ret; 2374470decc6SDave Kleikamp } 2375470decc6SDave Kleikamp 2376470decc6SDave Kleikamp static void __exit journal_exit(void) 2377470decc6SDave Kleikamp { 2378e23291b9SJose R. Santos #ifdef CONFIG_JBD2_DEBUG 2379470decc6SDave Kleikamp int n = atomic_read(&nr_journal_heads); 2380470decc6SDave Kleikamp if (n) 2381470decc6SDave Kleikamp printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n); 2382470decc6SDave Kleikamp #endif 23830f49d5d0SJose R. Santos jbd2_remove_debugfs_entry(); 23848e85fb3fSJohann Lombardi jbd2_remove_jbd_stats_proc_entry(); 2385f7f4bccbSMingming Cao jbd2_journal_destroy_caches(); 2386470decc6SDave Kleikamp } 2387470decc6SDave Kleikamp 2388470decc6SDave Kleikamp MODULE_LICENSE("GPL"); 2389470decc6SDave Kleikamp module_init(journal_init); 2390470decc6SDave Kleikamp module_exit(journal_exit); 2391470decc6SDave Kleikamp 2392