| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
| Uwe Zeisberger | f30c226 | 2006-10-03 23:01:26 +0200 | [diff] [blame] | 2 | * linux/fs/jbd/journal.c | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * | 
|  | 4 | * Written by Stephen C. Tweedie <sct@redhat.com>, 1998 | 
|  | 5 | * | 
|  | 6 | * Copyright 1998 Red Hat corp --- All Rights Reserved | 
|  | 7 | * | 
|  | 8 | * This file is part of the Linux kernel and is made available under | 
|  | 9 | * the terms of the GNU General Public License, version 2, or at your | 
|  | 10 | * option, any later version, incorporated herein by reference. | 
|  | 11 | * | 
|  | 12 | * Generic filesystem journal-writing code; part of the ext2fs | 
|  | 13 | * journaling system. | 
|  | 14 | * | 
|  | 15 | * This file manages journals: areas of disk reserved for logging | 
|  | 16 | * transactional updates.  This includes the kernel journaling thread | 
|  | 17 | * which is responsible for scheduling updates to the log. | 
|  | 18 | * | 
|  | 19 | * We do not actually manage the physical storage of the journal in this | 
|  | 20 | * file: that is left to a per-journal policy function, which allows us | 
|  | 21 | * to store the journal within a filesystem-specified area for ext2 | 
|  | 22 | * journaling (ext2 can use a reserved inode for storing the log). | 
|  | 23 | */ | 
|  | 24 |  | 
|  | 25 | #include <linux/module.h> | 
|  | 26 | #include <linux/time.h> | 
|  | 27 | #include <linux/fs.h> | 
|  | 28 | #include <linux/jbd.h> | 
|  | 29 | #include <linux/errno.h> | 
|  | 30 | #include <linux/slab.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/init.h> | 
|  | 32 | #include <linux/mm.h> | 
| Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 33 | #include <linux/freezer.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <linux/pagemap.h> | 
| Andrew Morton | 8d8c851 | 2006-03-25 03:06:53 -0800 | [diff] [blame] | 35 | #include <linux/kthread.h> | 
| Randy Dunlap | c9cf552 | 2006-06-27 02:53:52 -0700 | [diff] [blame] | 36 | #include <linux/poison.h> | 
| Andrew Morton | 8d8c851 | 2006-03-25 03:06:53 -0800 | [diff] [blame] | 37 | #include <linux/proc_fs.h> | 
| Jose R. Santos | c2a9159 | 2007-10-18 23:39:22 -0700 | [diff] [blame] | 38 | #include <linux/debugfs.h> | 
| Namhyung Kim | f81e3d4 | 2010-10-04 19:12:13 +0900 | [diff] [blame] | 39 | #include <linux/ratelimit.h> | 
| Andrew Morton | 8d8c851 | 2006-03-25 03:06:53 -0800 | [diff] [blame] | 40 |  | 
| Lukas Czerner | 99cb1a3 | 2011-05-23 18:33:02 +0200 | [diff] [blame] | 41 | #define CREATE_TRACE_POINTS | 
|  | 42 | #include <trace/events/jbd.h> | 
|  | 43 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <asm/uaccess.h> | 
|  | 45 | #include <asm/page.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 |  | 
|  | 47 | EXPORT_SYMBOL(journal_start); | 
|  | 48 | EXPORT_SYMBOL(journal_restart); | 
|  | 49 | EXPORT_SYMBOL(journal_extend); | 
|  | 50 | EXPORT_SYMBOL(journal_stop); | 
|  | 51 | EXPORT_SYMBOL(journal_lock_updates); | 
|  | 52 | EXPORT_SYMBOL(journal_unlock_updates); | 
|  | 53 | EXPORT_SYMBOL(journal_get_write_access); | 
|  | 54 | EXPORT_SYMBOL(journal_get_create_access); | 
|  | 55 | EXPORT_SYMBOL(journal_get_undo_access); | 
|  | 56 | EXPORT_SYMBOL(journal_dirty_data); | 
|  | 57 | EXPORT_SYMBOL(journal_dirty_metadata); | 
|  | 58 | EXPORT_SYMBOL(journal_release_buffer); | 
|  | 59 | EXPORT_SYMBOL(journal_forget); | 
|  | 60 | #if 0 | 
|  | 61 | EXPORT_SYMBOL(journal_sync_buffer); | 
|  | 62 | #endif | 
|  | 63 | EXPORT_SYMBOL(journal_flush); | 
|  | 64 | EXPORT_SYMBOL(journal_revoke); | 
|  | 65 |  | 
|  | 66 | EXPORT_SYMBOL(journal_init_dev); | 
|  | 67 | EXPORT_SYMBOL(journal_init_inode); | 
|  | 68 | EXPORT_SYMBOL(journal_update_format); | 
|  | 69 | EXPORT_SYMBOL(journal_check_used_features); | 
|  | 70 | EXPORT_SYMBOL(journal_check_available_features); | 
|  | 71 | EXPORT_SYMBOL(journal_set_features); | 
|  | 72 | EXPORT_SYMBOL(journal_create); | 
|  | 73 | EXPORT_SYMBOL(journal_load); | 
|  | 74 | EXPORT_SYMBOL(journal_destroy); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | EXPORT_SYMBOL(journal_abort); | 
|  | 76 | EXPORT_SYMBOL(journal_errno); | 
|  | 77 | EXPORT_SYMBOL(journal_ack_err); | 
|  | 78 | EXPORT_SYMBOL(journal_clear_err); | 
|  | 79 | EXPORT_SYMBOL(log_wait_commit); | 
| Stefan Schmidt | ff5e4b5 | 2009-11-12 09:53:50 +0100 | [diff] [blame] | 80 | EXPORT_SYMBOL(log_start_commit); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | EXPORT_SYMBOL(journal_start_commit); | 
|  | 82 | EXPORT_SYMBOL(journal_force_commit_nested); | 
|  | 83 | EXPORT_SYMBOL(journal_wipe); | 
|  | 84 | EXPORT_SYMBOL(journal_blocks_per_page); | 
|  | 85 | EXPORT_SYMBOL(journal_invalidatepage); | 
|  | 86 | EXPORT_SYMBOL(journal_try_to_free_buffers); | 
|  | 87 | EXPORT_SYMBOL(journal_force_commit); | 
|  | 88 |  | 
|  | 89 | static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *); | 
| Adrian Bunk | 022a4a7 | 2005-09-06 15:16:41 -0700 | [diff] [blame] | 90 | static void __journal_abort_soft (journal_t *journal, int errno); | 
| Darrick J. Wong | dff6825 | 2010-10-04 12:35:05 -0700 | [diff] [blame] | 91 | static const char *journal_dev_name(journal_t *journal, char *buffer); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 |  | 
|  | 93 | /* | 
|  | 94 | * Helper function used to manage commit timeouts | 
|  | 95 | */ | 
|  | 96 |  | 
|  | 97 | static void commit_timeout(unsigned long __data) | 
|  | 98 | { | 
|  | 99 | struct task_struct * p = (struct task_struct *) __data; | 
|  | 100 |  | 
|  | 101 | wake_up_process(p); | 
|  | 102 | } | 
|  | 103 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | /* | 
|  | 105 | * kjournald: The main thread function used to manage a logging device | 
|  | 106 | * journal. | 
|  | 107 | * | 
|  | 108 | * This kernel thread is responsible for two things: | 
|  | 109 | * | 
|  | 110 | * 1) COMMIT:  Every so often we need to commit the current state of the | 
|  | 111 | *    filesystem to disk.  The journal thread is responsible for writing | 
|  | 112 | *    all of the metadata buffers to disk. | 
|  | 113 | * | 
|  | 114 | * 2) CHECKPOINT: We cannot reuse a used section of the log file until all | 
|  | 115 | *    of the data in that part of the log has been rewritten elsewhere on | 
|  | 116 | *    the disk.  Flushing these old buffers to reclaim space in the log is | 
|  | 117 | *    known as checkpointing, and this thread is responsible for that job. | 
|  | 118 | */ | 
|  | 119 |  | 
| Adrian Bunk | 022a4a7 | 2005-09-06 15:16:41 -0700 | [diff] [blame] | 120 | static int kjournald(void *arg) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | { | 
| Andrew Morton | e3df189 | 2006-03-25 03:06:53 -0800 | [diff] [blame] | 122 | journal_t *journal = arg; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | transaction_t *transaction; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 |  | 
| Andrew Morton | e3df189 | 2006-03-25 03:06:53 -0800 | [diff] [blame] | 125 | /* | 
|  | 126 | * Set up an interval timer which can be used to trigger a commit wakeup | 
|  | 127 | * after the commit interval expires | 
|  | 128 | */ | 
|  | 129 | setup_timer(&journal->j_commit_timer, commit_timeout, | 
|  | 130 | (unsigned long)current); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 |  | 
|  | 132 | /* Record that the journal thread is running */ | 
|  | 133 | journal->j_task = current; | 
|  | 134 | wake_up(&journal->j_wait_done_commit); | 
|  | 135 |  | 
|  | 136 | printk(KERN_INFO "kjournald starting.  Commit interval %ld seconds\n", | 
|  | 137 | journal->j_commit_interval / HZ); | 
|  | 138 |  | 
|  | 139 | /* | 
|  | 140 | * And now, wait forever for commit wakeup events. | 
|  | 141 | */ | 
|  | 142 | spin_lock(&journal->j_state_lock); | 
|  | 143 |  | 
|  | 144 | loop: | 
|  | 145 | if (journal->j_flags & JFS_UNMOUNT) | 
|  | 146 | goto end_loop; | 
|  | 147 |  | 
|  | 148 | jbd_debug(1, "commit_sequence=%d, commit_request=%d\n", | 
|  | 149 | journal->j_commit_sequence, journal->j_commit_request); | 
|  | 150 |  | 
|  | 151 | if (journal->j_commit_sequence != journal->j_commit_request) { | 
|  | 152 | jbd_debug(1, "OK, requests differ\n"); | 
|  | 153 | spin_unlock(&journal->j_state_lock); | 
| Andrew Morton | e3df189 | 2006-03-25 03:06:53 -0800 | [diff] [blame] | 154 | del_timer_sync(&journal->j_commit_timer); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | journal_commit_transaction(journal); | 
|  | 156 | spin_lock(&journal->j_state_lock); | 
|  | 157 | goto loop; | 
|  | 158 | } | 
|  | 159 |  | 
|  | 160 | wake_up(&journal->j_wait_done_commit); | 
| Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 161 | if (freezing(current)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | /* | 
|  | 163 | * The simpler the better. Flushing journal isn't a | 
|  | 164 | * good idea, because that depends on threads that may | 
|  | 165 | * be already stopped. | 
|  | 166 | */ | 
|  | 167 | jbd_debug(1, "Now suspending kjournald\n"); | 
|  | 168 | spin_unlock(&journal->j_state_lock); | 
| Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 169 | refrigerator(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | spin_lock(&journal->j_state_lock); | 
|  | 171 | } else { | 
|  | 172 | /* | 
|  | 173 | * We assume on resume that commits are already there, | 
|  | 174 | * so we don't sleep | 
|  | 175 | */ | 
|  | 176 | DEFINE_WAIT(wait); | 
|  | 177 | int should_sleep = 1; | 
|  | 178 |  | 
|  | 179 | prepare_to_wait(&journal->j_wait_commit, &wait, | 
|  | 180 | TASK_INTERRUPTIBLE); | 
|  | 181 | if (journal->j_commit_sequence != journal->j_commit_request) | 
|  | 182 | should_sleep = 0; | 
|  | 183 | transaction = journal->j_running_transaction; | 
|  | 184 | if (transaction && time_after_eq(jiffies, | 
|  | 185 | transaction->t_expires)) | 
|  | 186 | should_sleep = 0; | 
| Mark Fasheh | cbf0d27 | 2005-09-06 15:19:08 -0700 | [diff] [blame] | 187 | if (journal->j_flags & JFS_UNMOUNT) | 
| Dave Kleikamp | e9ad562 | 2006-09-27 01:49:35 -0700 | [diff] [blame] | 188 | should_sleep = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | if (should_sleep) { | 
|  | 190 | spin_unlock(&journal->j_state_lock); | 
|  | 191 | schedule(); | 
|  | 192 | spin_lock(&journal->j_state_lock); | 
|  | 193 | } | 
|  | 194 | finish_wait(&journal->j_wait_commit, &wait); | 
|  | 195 | } | 
|  | 196 |  | 
|  | 197 | jbd_debug(1, "kjournald wakes\n"); | 
|  | 198 |  | 
|  | 199 | /* | 
|  | 200 | * Were we woken up by a commit wakeup event? | 
|  | 201 | */ | 
|  | 202 | transaction = journal->j_running_transaction; | 
|  | 203 | if (transaction && time_after_eq(jiffies, transaction->t_expires)) { | 
|  | 204 | journal->j_commit_request = transaction->t_tid; | 
|  | 205 | jbd_debug(1, "woke because of timeout\n"); | 
|  | 206 | } | 
|  | 207 | goto loop; | 
|  | 208 |  | 
|  | 209 | end_loop: | 
|  | 210 | spin_unlock(&journal->j_state_lock); | 
| Andrew Morton | e3df189 | 2006-03-25 03:06:53 -0800 | [diff] [blame] | 211 | del_timer_sync(&journal->j_commit_timer); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | journal->j_task = NULL; | 
|  | 213 | wake_up(&journal->j_wait_done_commit); | 
|  | 214 | jbd_debug(1, "Journal thread exiting.\n"); | 
|  | 215 | return 0; | 
|  | 216 | } | 
|  | 217 |  | 
| Pavel Emelianov | 97f0678 | 2007-05-08 00:30:42 -0700 | [diff] [blame] | 218 | static int journal_start_thread(journal_t *journal) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | { | 
| Pavel Emelianov | 97f0678 | 2007-05-08 00:30:42 -0700 | [diff] [blame] | 220 | struct task_struct *t; | 
|  | 221 |  | 
|  | 222 | t = kthread_run(kjournald, journal, "kjournald"); | 
|  | 223 | if (IS_ERR(t)) | 
|  | 224 | return PTR_ERR(t); | 
|  | 225 |  | 
| Stephen Hemminger | c80544d | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 226 | wait_event(journal->j_wait_done_commit, journal->j_task != NULL); | 
| Pavel Emelianov | 97f0678 | 2007-05-08 00:30:42 -0700 | [diff] [blame] | 227 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | } | 
|  | 229 |  | 
|  | 230 | static void journal_kill_thread(journal_t *journal) | 
|  | 231 | { | 
|  | 232 | spin_lock(&journal->j_state_lock); | 
|  | 233 | journal->j_flags |= JFS_UNMOUNT; | 
|  | 234 |  | 
|  | 235 | while (journal->j_task) { | 
|  | 236 | wake_up(&journal->j_wait_commit); | 
|  | 237 | spin_unlock(&journal->j_state_lock); | 
| Stephen Hemminger | c80544d | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 238 | wait_event(journal->j_wait_done_commit, | 
|  | 239 | journal->j_task == NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | spin_lock(&journal->j_state_lock); | 
|  | 241 | } | 
|  | 242 | spin_unlock(&journal->j_state_lock); | 
|  | 243 | } | 
|  | 244 |  | 
|  | 245 | /* | 
|  | 246 | * journal_write_metadata_buffer: write a metadata buffer to the journal. | 
|  | 247 | * | 
|  | 248 | * Writes a metadata buffer to a given disk block.  The actual IO is not | 
|  | 249 | * performed but a new buffer_head is constructed which labels the data | 
|  | 250 | * to be written with the correct destination disk block. | 
|  | 251 | * | 
|  | 252 | * Any magic-number escaping which needs to be done will cause a | 
|  | 253 | * copy-out here.  If the buffer happens to start with the | 
|  | 254 | * JFS_MAGIC_NUMBER, then we can't write it to the log directly: the | 
|  | 255 | * magic number is only written to the log for descripter blocks.  In | 
|  | 256 | * this case, we copy the data and replace the first word with 0, and we | 
|  | 257 | * return a result code which indicates that this buffer needs to be | 
|  | 258 | * marked as an escaped buffer in the corresponding log descriptor | 
|  | 259 | * block.  The missing word can then be restored when the block is read | 
|  | 260 | * during recovery. | 
|  | 261 | * | 
|  | 262 | * If the source buffer has already been modified by a new transaction | 
|  | 263 | * since we took the last commit snapshot, we use the frozen copy of | 
|  | 264 | * that data for IO.  If we end up using the existing buffer_head's data | 
|  | 265 | * for the write, then we *have* to lock the buffer to prevent anyone | 
|  | 266 | * else from using and possibly modifying it while the IO is in | 
|  | 267 | * progress. | 
|  | 268 | * | 
|  | 269 | * The function returns a pointer to the buffer_heads to be used for IO. | 
|  | 270 | * | 
|  | 271 | * We assume that the journal has already been locked in this function. | 
|  | 272 | * | 
|  | 273 | * Return value: | 
|  | 274 | *  <0: Error | 
|  | 275 | * >=0: Finished OK | 
|  | 276 | * | 
|  | 277 | * On success: | 
|  | 278 | * Bit 0 set == escape performed on the data | 
|  | 279 | * Bit 1 set == buffer copy-out performed (kfree the data after IO) | 
|  | 280 | */ | 
|  | 281 |  | 
|  | 282 | int journal_write_metadata_buffer(transaction_t *transaction, | 
|  | 283 | struct journal_head  *jh_in, | 
|  | 284 | struct journal_head **jh_out, | 
| Jan Kara | 9c28cbc | 2009-08-03 19:21:00 +0200 | [diff] [blame] | 285 | unsigned int blocknr) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | { | 
|  | 287 | int need_copy_out = 0; | 
|  | 288 | int done_copy_out = 0; | 
|  | 289 | int do_escape = 0; | 
|  | 290 | char *mapped_data; | 
|  | 291 | struct buffer_head *new_bh; | 
|  | 292 | struct journal_head *new_jh; | 
|  | 293 | struct page *new_page; | 
|  | 294 | unsigned int new_offset; | 
|  | 295 | struct buffer_head *bh_in = jh2bh(jh_in); | 
| dingdinghua | f1015c4 | 2009-07-15 21:42:05 +0200 | [diff] [blame] | 296 | journal_t *journal = transaction->t_journal; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 |  | 
|  | 298 | /* | 
|  | 299 | * The buffer really shouldn't be locked: only the current committing | 
|  | 300 | * transaction is allowed to write it, so nobody else is allowed | 
|  | 301 | * to do any IO. | 
|  | 302 | * | 
|  | 303 | * akpm: except if we're journalling data, and write() output is | 
|  | 304 | * also part of a shared mapping, and another thread has | 
|  | 305 | * decided to launch a writepage() against this buffer. | 
|  | 306 | */ | 
|  | 307 | J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in)); | 
|  | 308 |  | 
|  | 309 | new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL); | 
| dingdinghua | f1015c4 | 2009-07-15 21:42:05 +0200 | [diff] [blame] | 310 | /* keep subsequent assertions sane */ | 
|  | 311 | new_bh->b_state = 0; | 
|  | 312 | init_buffer(new_bh, NULL, NULL); | 
|  | 313 | atomic_set(&new_bh->b_count, 1); | 
|  | 314 | new_jh = journal_add_journal_head(new_bh);	/* This sleeps */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 |  | 
|  | 316 | /* | 
|  | 317 | * If a new transaction has already done a buffer copy-out, then | 
|  | 318 | * we use that version of the data for the commit. | 
|  | 319 | */ | 
|  | 320 | jbd_lock_bh_state(bh_in); | 
|  | 321 | repeat: | 
|  | 322 | if (jh_in->b_frozen_data) { | 
|  | 323 | done_copy_out = 1; | 
|  | 324 | new_page = virt_to_page(jh_in->b_frozen_data); | 
|  | 325 | new_offset = offset_in_page(jh_in->b_frozen_data); | 
|  | 326 | } else { | 
|  | 327 | new_page = jh2bh(jh_in)->b_page; | 
|  | 328 | new_offset = offset_in_page(jh2bh(jh_in)->b_data); | 
|  | 329 | } | 
|  | 330 |  | 
|  | 331 | mapped_data = kmap_atomic(new_page, KM_USER0); | 
|  | 332 | /* | 
|  | 333 | * Check for escaping | 
|  | 334 | */ | 
|  | 335 | if (*((__be32 *)(mapped_data + new_offset)) == | 
|  | 336 | cpu_to_be32(JFS_MAGIC_NUMBER)) { | 
|  | 337 | need_copy_out = 1; | 
|  | 338 | do_escape = 1; | 
|  | 339 | } | 
|  | 340 | kunmap_atomic(mapped_data, KM_USER0); | 
|  | 341 |  | 
|  | 342 | /* | 
|  | 343 | * Do we need to do a data copy? | 
|  | 344 | */ | 
|  | 345 | if (need_copy_out && !done_copy_out) { | 
|  | 346 | char *tmp; | 
|  | 347 |  | 
|  | 348 | jbd_unlock_bh_state(bh_in); | 
| Mingming Cao | c089d49 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 349 | tmp = jbd_alloc(bh_in->b_size, GFP_NOFS); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | jbd_lock_bh_state(bh_in); | 
|  | 351 | if (jh_in->b_frozen_data) { | 
| Mingming Cao | c089d49 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 352 | jbd_free(tmp, bh_in->b_size); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | goto repeat; | 
|  | 354 | } | 
|  | 355 |  | 
|  | 356 | jh_in->b_frozen_data = tmp; | 
|  | 357 | mapped_data = kmap_atomic(new_page, KM_USER0); | 
|  | 358 | memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size); | 
|  | 359 | kunmap_atomic(mapped_data, KM_USER0); | 
|  | 360 |  | 
|  | 361 | new_page = virt_to_page(tmp); | 
|  | 362 | new_offset = offset_in_page(tmp); | 
|  | 363 | done_copy_out = 1; | 
|  | 364 | } | 
|  | 365 |  | 
|  | 366 | /* | 
|  | 367 | * Did we need to do an escaping?  Now we've done all the | 
|  | 368 | * copying, we can finally do so. | 
|  | 369 | */ | 
|  | 370 | if (do_escape) { | 
|  | 371 | mapped_data = kmap_atomic(new_page, KM_USER0); | 
|  | 372 | *((unsigned int *)(mapped_data + new_offset)) = 0; | 
|  | 373 | kunmap_atomic(mapped_data, KM_USER0); | 
|  | 374 | } | 
|  | 375 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | set_bh_page(new_bh, new_page, new_offset); | 
|  | 377 | new_jh->b_transaction = NULL; | 
|  | 378 | new_bh->b_size = jh2bh(jh_in)->b_size; | 
|  | 379 | new_bh->b_bdev = transaction->t_journal->j_dev; | 
|  | 380 | new_bh->b_blocknr = blocknr; | 
|  | 381 | set_buffer_mapped(new_bh); | 
|  | 382 | set_buffer_dirty(new_bh); | 
|  | 383 |  | 
|  | 384 | *jh_out = new_jh; | 
|  | 385 |  | 
|  | 386 | /* | 
|  | 387 | * The to-be-written buffer needs to get moved to the io queue, | 
|  | 388 | * and the original buffer whose contents we are shadowing or | 
|  | 389 | * copying is moved to the transaction's shadow queue. | 
|  | 390 | */ | 
|  | 391 | JBUFFER_TRACE(jh_in, "file as BJ_Shadow"); | 
| dingdinghua | f1015c4 | 2009-07-15 21:42:05 +0200 | [diff] [blame] | 392 | spin_lock(&journal->j_list_lock); | 
|  | 393 | __journal_file_buffer(jh_in, transaction, BJ_Shadow); | 
|  | 394 | spin_unlock(&journal->j_list_lock); | 
|  | 395 | jbd_unlock_bh_state(bh_in); | 
|  | 396 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | JBUFFER_TRACE(new_jh, "file as BJ_IO"); | 
|  | 398 | journal_file_buffer(new_jh, transaction, BJ_IO); | 
|  | 399 |  | 
|  | 400 | return do_escape | (done_copy_out << 1); | 
|  | 401 | } | 
|  | 402 |  | 
|  | 403 | /* | 
|  | 404 | * Allocation code for the journal file.  Manage the space left in the | 
|  | 405 | * journal, so that we can begin checkpointing when appropriate. | 
|  | 406 | */ | 
|  | 407 |  | 
|  | 408 | /* | 
|  | 409 | * __log_space_left: Return the number of free blocks left in the journal. | 
|  | 410 | * | 
|  | 411 | * Called with the journal already locked. | 
|  | 412 | * | 
|  | 413 | * Called under j_state_lock | 
|  | 414 | */ | 
|  | 415 |  | 
|  | 416 | int __log_space_left(journal_t *journal) | 
|  | 417 | { | 
|  | 418 | int left = journal->j_free; | 
|  | 419 |  | 
|  | 420 | assert_spin_locked(&journal->j_state_lock); | 
|  | 421 |  | 
|  | 422 | /* | 
|  | 423 | * Be pessimistic here about the number of those free blocks which | 
|  | 424 | * might be required for log descriptor control blocks. | 
|  | 425 | */ | 
|  | 426 |  | 
|  | 427 | #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */ | 
|  | 428 |  | 
|  | 429 | left -= MIN_LOG_RESERVED_BLOCKS; | 
|  | 430 |  | 
|  | 431 | if (left <= 0) | 
|  | 432 | return 0; | 
|  | 433 | left -= (left >> 3); | 
|  | 434 | return left; | 
|  | 435 | } | 
|  | 436 |  | 
|  | 437 | /* | 
| Jan Kara | 8fe4cd0 | 2009-02-11 13:04:25 -0800 | [diff] [blame] | 438 | * Called under j_state_lock.  Returns true if a transaction commit was started. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | */ | 
|  | 440 | int __log_start_commit(journal_t *journal, tid_t target) | 
|  | 441 | { | 
|  | 442 | /* | 
| Ted Ts'o | d9b0193 | 2011-04-30 13:17:11 -0400 | [diff] [blame] | 443 | * The only transaction we can possibly wait upon is the | 
|  | 444 | * currently running transaction (if it exists).  Otherwise, | 
|  | 445 | * the target tid must be an old one. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | */ | 
| Ted Ts'o | d9b0193 | 2011-04-30 13:17:11 -0400 | [diff] [blame] | 447 | if (journal->j_running_transaction && | 
|  | 448 | journal->j_running_transaction->t_tid == target) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | /* | 
| Andrea Gelmini | bcf3d0b | 2010-10-16 15:19:14 +0200 | [diff] [blame] | 450 | * We want a new commit: OK, mark the request and wakeup the | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | * commit thread.  We do _not_ do the commit ourselves. | 
|  | 452 | */ | 
|  | 453 |  | 
|  | 454 | journal->j_commit_request = target; | 
|  | 455 | jbd_debug(1, "JBD: requesting commit %d/%d\n", | 
|  | 456 | journal->j_commit_request, | 
|  | 457 | journal->j_commit_sequence); | 
|  | 458 | wake_up(&journal->j_wait_commit); | 
|  | 459 | return 1; | 
| Ted Ts'o | d9b0193 | 2011-04-30 13:17:11 -0400 | [diff] [blame] | 460 | } else if (!tid_geq(journal->j_commit_request, target)) | 
|  | 461 | /* This should never happen, but if it does, preserve | 
|  | 462 | the evidence before kjournald goes into a loop and | 
|  | 463 | increments j_commit_sequence beyond all recognition. */ | 
|  | 464 | WARN_ONCE(1, "jbd: bad log_start_commit: %u %u %u %u\n", | 
|  | 465 | journal->j_commit_request, journal->j_commit_sequence, | 
|  | 466 | target, journal->j_running_transaction ? | 
|  | 467 | journal->j_running_transaction->t_tid : 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | return 0; | 
|  | 469 | } | 
|  | 470 |  | 
|  | 471 | int log_start_commit(journal_t *journal, tid_t tid) | 
|  | 472 | { | 
|  | 473 | int ret; | 
|  | 474 |  | 
|  | 475 | spin_lock(&journal->j_state_lock); | 
|  | 476 | ret = __log_start_commit(journal, tid); | 
|  | 477 | spin_unlock(&journal->j_state_lock); | 
|  | 478 | return ret; | 
|  | 479 | } | 
|  | 480 |  | 
|  | 481 | /* | 
|  | 482 | * Force and wait upon a commit if the calling process is not within | 
|  | 483 | * transaction.  This is used for forcing out undo-protected data which contains | 
|  | 484 | * bitmaps, when the fs is running out of space. | 
|  | 485 | * | 
|  | 486 | * We can only force the running transaction if we don't have an active handle; | 
|  | 487 | * otherwise, we will deadlock. | 
|  | 488 | * | 
|  | 489 | * Returns true if a transaction was started. | 
|  | 490 | */ | 
|  | 491 | int journal_force_commit_nested(journal_t *journal) | 
|  | 492 | { | 
|  | 493 | transaction_t *transaction = NULL; | 
|  | 494 | tid_t tid; | 
|  | 495 |  | 
|  | 496 | spin_lock(&journal->j_state_lock); | 
|  | 497 | if (journal->j_running_transaction && !current->journal_info) { | 
|  | 498 | transaction = journal->j_running_transaction; | 
|  | 499 | __log_start_commit(journal, transaction->t_tid); | 
|  | 500 | } else if (journal->j_committing_transaction) | 
|  | 501 | transaction = journal->j_committing_transaction; | 
|  | 502 |  | 
|  | 503 | if (!transaction) { | 
|  | 504 | spin_unlock(&journal->j_state_lock); | 
|  | 505 | return 0;	/* Nothing to retry */ | 
|  | 506 | } | 
|  | 507 |  | 
|  | 508 | tid = transaction->t_tid; | 
|  | 509 | spin_unlock(&journal->j_state_lock); | 
|  | 510 | log_wait_commit(journal, tid); | 
|  | 511 | return 1; | 
|  | 512 | } | 
|  | 513 |  | 
|  | 514 | /* | 
|  | 515 | * Start a commit of the current running transaction (if any).  Returns true | 
| Jan Kara | 8fe4cd0 | 2009-02-11 13:04:25 -0800 | [diff] [blame] | 516 | * if a transaction is going to be committed (or is currently already | 
|  | 517 | * committing), and fills its tid in at *ptid | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | */ | 
|  | 519 | int journal_start_commit(journal_t *journal, tid_t *ptid) | 
|  | 520 | { | 
|  | 521 | int ret = 0; | 
|  | 522 |  | 
|  | 523 | spin_lock(&journal->j_state_lock); | 
|  | 524 | if (journal->j_running_transaction) { | 
|  | 525 | tid_t tid = journal->j_running_transaction->t_tid; | 
|  | 526 |  | 
| Jan Kara | 8fe4cd0 | 2009-02-11 13:04:25 -0800 | [diff] [blame] | 527 | __log_start_commit(journal, tid); | 
|  | 528 | /* There's a running transaction and we've just made sure | 
|  | 529 | * it's commit has been scheduled. */ | 
|  | 530 | if (ptid) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | *ptid = tid; | 
| Jan Kara | 8fe4cd0 | 2009-02-11 13:04:25 -0800 | [diff] [blame] | 532 | ret = 1; | 
|  | 533 | } else if (journal->j_committing_transaction) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | /* | 
|  | 535 | * If ext3_write_super() recently started a commit, then we | 
|  | 536 | * have to wait for completion of that transaction | 
|  | 537 | */ | 
| Jan Kara | 8fe4cd0 | 2009-02-11 13:04:25 -0800 | [diff] [blame] | 538 | if (ptid) | 
|  | 539 | *ptid = journal->j_committing_transaction->t_tid; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | ret = 1; | 
|  | 541 | } | 
|  | 542 | spin_unlock(&journal->j_state_lock); | 
|  | 543 | return ret; | 
|  | 544 | } | 
|  | 545 |  | 
|  | 546 | /* | 
|  | 547 | * Wait for a specified commit to complete. | 
|  | 548 | * The caller may not hold the journal lock. | 
|  | 549 | */ | 
|  | 550 | int log_wait_commit(journal_t *journal, tid_t tid) | 
|  | 551 | { | 
|  | 552 | int err = 0; | 
|  | 553 |  | 
|  | 554 | #ifdef CONFIG_JBD_DEBUG | 
|  | 555 | spin_lock(&journal->j_state_lock); | 
|  | 556 | if (!tid_geq(journal->j_commit_request, tid)) { | 
|  | 557 | printk(KERN_EMERG | 
|  | 558 | "%s: error: j_commit_request=%d, tid=%d\n", | 
| Harvey Harrison | 08fc99b | 2008-04-28 02:16:16 -0700 | [diff] [blame] | 559 | __func__, journal->j_commit_request, tid); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | } | 
|  | 561 | spin_unlock(&journal->j_state_lock); | 
|  | 562 | #endif | 
|  | 563 | spin_lock(&journal->j_state_lock); | 
|  | 564 | while (tid_gt(tid, journal->j_commit_sequence)) { | 
|  | 565 | jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n", | 
|  | 566 | tid, journal->j_commit_sequence); | 
|  | 567 | wake_up(&journal->j_wait_commit); | 
|  | 568 | spin_unlock(&journal->j_state_lock); | 
|  | 569 | wait_event(journal->j_wait_done_commit, | 
|  | 570 | !tid_gt(tid, journal->j_commit_sequence)); | 
|  | 571 | spin_lock(&journal->j_state_lock); | 
|  | 572 | } | 
|  | 573 | spin_unlock(&journal->j_state_lock); | 
|  | 574 |  | 
|  | 575 | if (unlikely(is_journal_aborted(journal))) { | 
|  | 576 | printk(KERN_EMERG "journal commit I/O error\n"); | 
|  | 577 | err = -EIO; | 
|  | 578 | } | 
|  | 579 | return err; | 
|  | 580 | } | 
|  | 581 |  | 
|  | 582 | /* | 
| Jan Kara | 03f4d80 | 2010-04-15 22:16:24 +0200 | [diff] [blame] | 583 | * Return 1 if a given transaction has not yet sent barrier request | 
|  | 584 | * connected with a transaction commit. If 0 is returned, transaction | 
|  | 585 | * may or may not have sent the barrier. Used to avoid sending barrier | 
|  | 586 | * twice in common cases. | 
|  | 587 | */ | 
|  | 588 | int journal_trans_will_send_data_barrier(journal_t *journal, tid_t tid) | 
|  | 589 | { | 
|  | 590 | int ret = 0; | 
|  | 591 | transaction_t *commit_trans; | 
|  | 592 |  | 
|  | 593 | if (!(journal->j_flags & JFS_BARRIER)) | 
|  | 594 | return 0; | 
|  | 595 | spin_lock(&journal->j_state_lock); | 
|  | 596 | /* Transaction already committed? */ | 
|  | 597 | if (tid_geq(journal->j_commit_sequence, tid)) | 
|  | 598 | goto out; | 
|  | 599 | /* | 
|  | 600 | * Transaction is being committed and we already proceeded to | 
|  | 601 | * writing commit record? | 
|  | 602 | */ | 
|  | 603 | commit_trans = journal->j_committing_transaction; | 
|  | 604 | if (commit_trans && commit_trans->t_tid == tid && | 
|  | 605 | commit_trans->t_state >= T_COMMIT_RECORD) | 
|  | 606 | goto out; | 
|  | 607 | ret = 1; | 
|  | 608 | out: | 
|  | 609 | spin_unlock(&journal->j_state_lock); | 
|  | 610 | return ret; | 
|  | 611 | } | 
| Jan Kara | 5277970 | 2010-04-15 22:24:26 +0200 | [diff] [blame] | 612 | EXPORT_SYMBOL(journal_trans_will_send_data_barrier); | 
| Jan Kara | 03f4d80 | 2010-04-15 22:16:24 +0200 | [diff] [blame] | 613 |  | 
|  | 614 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | * Log buffer allocation routines: | 
|  | 616 | */ | 
|  | 617 |  | 
| Jan Kara | 9c28cbc | 2009-08-03 19:21:00 +0200 | [diff] [blame] | 618 | int journal_next_log_block(journal_t *journal, unsigned int *retp) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | { | 
| Jan Kara | 9c28cbc | 2009-08-03 19:21:00 +0200 | [diff] [blame] | 620 | unsigned int blocknr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 |  | 
|  | 622 | spin_lock(&journal->j_state_lock); | 
|  | 623 | J_ASSERT(journal->j_free > 1); | 
|  | 624 |  | 
|  | 625 | blocknr = journal->j_head; | 
|  | 626 | journal->j_head++; | 
|  | 627 | journal->j_free--; | 
|  | 628 | if (journal->j_head == journal->j_last) | 
|  | 629 | journal->j_head = journal->j_first; | 
|  | 630 | spin_unlock(&journal->j_state_lock); | 
|  | 631 | return journal_bmap(journal, blocknr, retp); | 
|  | 632 | } | 
|  | 633 |  | 
|  | 634 | /* | 
|  | 635 | * Conversion of logical to physical block numbers for the journal | 
|  | 636 | * | 
|  | 637 | * On external journals the journal blocks are identity-mapped, so | 
|  | 638 | * this is a no-op.  If needed, we can use j_blk_offset - everything is | 
|  | 639 | * ready. | 
|  | 640 | */ | 
| Jan Kara | 9c28cbc | 2009-08-03 19:21:00 +0200 | [diff] [blame] | 641 | int journal_bmap(journal_t *journal, unsigned int blocknr, | 
|  | 642 | unsigned int *retp) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | { | 
|  | 644 | int err = 0; | 
| Jan Kara | 9c28cbc | 2009-08-03 19:21:00 +0200 | [diff] [blame] | 645 | unsigned int ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 |  | 
|  | 647 | if (journal->j_inode) { | 
|  | 648 | ret = bmap(journal->j_inode, blocknr); | 
|  | 649 | if (ret) | 
|  | 650 | *retp = ret; | 
|  | 651 | else { | 
|  | 652 | char b[BDEVNAME_SIZE]; | 
|  | 653 |  | 
|  | 654 | printk(KERN_ALERT "%s: journal block not found " | 
| Jan Kara | 9c28cbc | 2009-08-03 19:21:00 +0200 | [diff] [blame] | 655 | "at offset %u on %s\n", | 
| Harvey Harrison | 08fc99b | 2008-04-28 02:16:16 -0700 | [diff] [blame] | 656 | __func__, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | blocknr, | 
|  | 658 | bdevname(journal->j_dev, b)); | 
|  | 659 | err = -EIO; | 
|  | 660 | __journal_abort_soft(journal, err); | 
|  | 661 | } | 
|  | 662 | } else { | 
|  | 663 | *retp = blocknr; /* +journal->j_blk_offset */ | 
|  | 664 | } | 
|  | 665 | return err; | 
|  | 666 | } | 
|  | 667 |  | 
|  | 668 | /* | 
|  | 669 | * We play buffer_head aliasing tricks to write data/metadata blocks to | 
|  | 670 | * the journal without copying their contents, but for journal | 
|  | 671 | * descriptor blocks we do need to generate bona fide buffers. | 
|  | 672 | * | 
|  | 673 | * After the caller of journal_get_descriptor_buffer() has finished modifying | 
|  | 674 | * the buffer's contents they really should run flush_dcache_page(bh->b_page). | 
|  | 675 | * But we don't bother doing that, so there will be coherency problems with | 
|  | 676 | * mmaps of blockdevs which hold live JBD-controlled filesystems. | 
|  | 677 | */ | 
|  | 678 | struct journal_head *journal_get_descriptor_buffer(journal_t *journal) | 
|  | 679 | { | 
|  | 680 | struct buffer_head *bh; | 
| Jan Kara | 9c28cbc | 2009-08-03 19:21:00 +0200 | [diff] [blame] | 681 | unsigned int blocknr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | int err; | 
|  | 683 |  | 
|  | 684 | err = journal_next_log_block(journal, &blocknr); | 
|  | 685 |  | 
|  | 686 | if (err) | 
|  | 687 | return NULL; | 
|  | 688 |  | 
|  | 689 | bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize); | 
| Jan Kara | ecca9af | 2009-04-02 16:57:13 -0700 | [diff] [blame] | 690 | if (!bh) | 
|  | 691 | return NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | lock_buffer(bh); | 
|  | 693 | memset(bh->b_data, 0, journal->j_blocksize); | 
|  | 694 | set_buffer_uptodate(bh); | 
|  | 695 | unlock_buffer(bh); | 
|  | 696 | BUFFER_TRACE(bh, "return this buffer"); | 
|  | 697 | return journal_add_journal_head(bh); | 
|  | 698 | } | 
|  | 699 |  | 
|  | 700 | /* | 
|  | 701 | * Management for journal control blocks: functions to create and | 
|  | 702 | * destroy journal_t structures, and to initialise and read existing | 
|  | 703 | * journal blocks from disk.  */ | 
|  | 704 |  | 
|  | 705 | /* First: create and setup a journal_t object in memory.  We initialise | 
|  | 706 | * very few fields yet: that has to wait until we have created the | 
|  | 707 | * journal structures from from scratch, or loaded them from disk. */ | 
|  | 708 |  | 
|  | 709 | static journal_t * journal_init_common (void) | 
|  | 710 | { | 
|  | 711 | journal_t *journal; | 
|  | 712 | int err; | 
|  | 713 |  | 
| Mingming Cao | 8c3478a | 2007-10-18 23:39:20 -0700 | [diff] [blame] | 714 | journal = kzalloc(sizeof(*journal), GFP_KERNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | if (!journal) | 
|  | 716 | goto fail; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 |  | 
|  | 718 | init_waitqueue_head(&journal->j_wait_transaction_locked); | 
|  | 719 | init_waitqueue_head(&journal->j_wait_logspace); | 
|  | 720 | init_waitqueue_head(&journal->j_wait_done_commit); | 
|  | 721 | init_waitqueue_head(&journal->j_wait_checkpoint); | 
|  | 722 | init_waitqueue_head(&journal->j_wait_commit); | 
|  | 723 | init_waitqueue_head(&journal->j_wait_updates); | 
| Arjan van de Ven | 2c68ee7 | 2006-03-23 03:00:35 -0800 | [diff] [blame] | 724 | mutex_init(&journal->j_barrier); | 
|  | 725 | mutex_init(&journal->j_checkpoint_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | spin_lock_init(&journal->j_revoke_lock); | 
|  | 727 | spin_lock_init(&journal->j_list_lock); | 
|  | 728 | spin_lock_init(&journal->j_state_lock); | 
|  | 729 |  | 
|  | 730 | journal->j_commit_interval = (HZ * JBD_DEFAULT_MAX_COMMIT_AGE); | 
|  | 731 |  | 
|  | 732 | /* The journal is marked for error until we succeed with recovery! */ | 
|  | 733 | journal->j_flags = JFS_ABORT; | 
|  | 734 |  | 
|  | 735 | /* Set up a default-sized revoke table for the new mount. */ | 
|  | 736 | err = journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH); | 
|  | 737 | if (err) { | 
|  | 738 | kfree(journal); | 
|  | 739 | goto fail; | 
|  | 740 | } | 
|  | 741 | return journal; | 
|  | 742 | fail: | 
|  | 743 | return NULL; | 
|  | 744 | } | 
|  | 745 |  | 
|  | 746 | /* journal_init_dev and journal_init_inode: | 
|  | 747 | * | 
|  | 748 | * Create a journal structure assigned some fixed set of disk blocks to | 
|  | 749 | * the journal.  We don't actually touch those disk blocks yet, but we | 
|  | 750 | * need to set up all of the mapping information to tell the journaling | 
|  | 751 | * system where the journal blocks are. | 
|  | 752 | * | 
|  | 753 | */ | 
|  | 754 |  | 
|  | 755 | /** | 
| Randy Dunlap | 0cf01f6 | 2008-03-19 17:00:44 -0700 | [diff] [blame] | 756 | *  journal_t * journal_init_dev() - creates and initialises a journal structure | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | *  @bdev: Block device on which to create the journal | 
|  | 758 | *  @fs_dev: Device which hold journalled filesystem for this journal. | 
|  | 759 | *  @start: Block nr Start of journal. | 
| Eric Sandeen | 37ed322 | 2006-09-27 01:49:31 -0700 | [diff] [blame] | 760 | *  @len:  Length of the journal in blocks. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | *  @blocksize: blocksize of journalling device | 
| Randy Dunlap | 0cf01f6 | 2008-03-19 17:00:44 -0700 | [diff] [blame] | 762 | * | 
|  | 763 | *  Returns: a newly created journal_t * | 
| Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 764 | * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | *  journal_init_dev creates a journal which maps a fixed contiguous | 
|  | 766 | *  range of blocks on an arbitrary block device. | 
| Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 767 | * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | */ | 
|  | 769 | journal_t * journal_init_dev(struct block_device *bdev, | 
|  | 770 | struct block_device *fs_dev, | 
|  | 771 | int start, int len, int blocksize) | 
|  | 772 | { | 
|  | 773 | journal_t *journal = journal_init_common(); | 
|  | 774 | struct buffer_head *bh; | 
|  | 775 | int n; | 
|  | 776 |  | 
|  | 777 | if (!journal) | 
|  | 778 | return NULL; | 
|  | 779 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | /* journal descriptor can store up to n blocks -bzzz */ | 
| Zoltan Menyhart | d180779 | 2006-09-29 01:58:40 -0700 | [diff] [blame] | 781 | journal->j_blocksize = blocksize; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | n = journal->j_blocksize / sizeof(journal_block_tag_t); | 
|  | 783 | journal->j_wbufsize = n; | 
|  | 784 | journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL); | 
|  | 785 | if (!journal->j_wbuf) { | 
| Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 786 | printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n", | 
| Harvey Harrison | 08fc99b | 2008-04-28 02:16:16 -0700 | [diff] [blame] | 787 | __func__); | 
| Jan Kara | ecca9af | 2009-04-02 16:57:13 -0700 | [diff] [blame] | 788 | goto out_err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | } | 
| Zoltan Menyhart | d180779 | 2006-09-29 01:58:40 -0700 | [diff] [blame] | 790 | journal->j_dev = bdev; | 
|  | 791 | journal->j_fs_dev = fs_dev; | 
|  | 792 | journal->j_blk_offset = start; | 
|  | 793 | journal->j_maxlen = len; | 
|  | 794 |  | 
|  | 795 | bh = __getblk(journal->j_dev, start, journal->j_blocksize); | 
| Jan Kara | ecca9af | 2009-04-02 16:57:13 -0700 | [diff] [blame] | 796 | if (!bh) { | 
|  | 797 | printk(KERN_ERR | 
|  | 798 | "%s: Cannot get buffer for journal superblock\n", | 
|  | 799 | __func__); | 
|  | 800 | goto out_err; | 
|  | 801 | } | 
| Zoltan Menyhart | d180779 | 2006-09-29 01:58:40 -0700 | [diff] [blame] | 802 | journal->j_sb_buffer = bh; | 
|  | 803 | journal->j_superblock = (journal_superblock_t *)bh->b_data; | 
| Jan Kara | ecca9af | 2009-04-02 16:57:13 -0700 | [diff] [blame] | 804 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | return journal; | 
| Jan Kara | ecca9af | 2009-04-02 16:57:13 -0700 | [diff] [blame] | 806 | out_err: | 
| Tao Ma | 7b02bec | 2009-11-10 17:13:22 +0800 | [diff] [blame] | 807 | kfree(journal->j_wbuf); | 
| Jan Kara | ecca9af | 2009-04-02 16:57:13 -0700 | [diff] [blame] | 808 | kfree(journal); | 
|  | 809 | return NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | } | 
| Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 811 |  | 
|  | 812 | /** | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | *  journal_t * journal_init_inode () - creates a journal which maps to a inode. | 
|  | 814 | *  @inode: An inode to create the journal in | 
| Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 815 | * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | * journal_init_inode creates a journal which maps an on-disk inode as | 
|  | 817 | * the journal.  The inode must exist already, must support bmap() and | 
|  | 818 | * must have all data blocks preallocated. | 
|  | 819 | */ | 
|  | 820 | journal_t * journal_init_inode (struct inode *inode) | 
|  | 821 | { | 
|  | 822 | struct buffer_head *bh; | 
|  | 823 | journal_t *journal = journal_init_common(); | 
|  | 824 | int err; | 
|  | 825 | int n; | 
| Jan Kara | 9c28cbc | 2009-08-03 19:21:00 +0200 | [diff] [blame] | 826 | unsigned int blocknr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 |  | 
|  | 828 | if (!journal) | 
|  | 829 | return NULL; | 
|  | 830 |  | 
|  | 831 | journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev; | 
|  | 832 | journal->j_inode = inode; | 
|  | 833 | jbd_debug(1, | 
|  | 834 | "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n", | 
| Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 835 | journal, inode->i_sb->s_id, inode->i_ino, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | (long long) inode->i_size, | 
|  | 837 | inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize); | 
|  | 838 |  | 
|  | 839 | journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits; | 
|  | 840 | journal->j_blocksize = inode->i_sb->s_blocksize; | 
|  | 841 |  | 
|  | 842 | /* journal descriptor can store up to n blocks -bzzz */ | 
|  | 843 | n = journal->j_blocksize / sizeof(journal_block_tag_t); | 
|  | 844 | journal->j_wbufsize = n; | 
|  | 845 | journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL); | 
|  | 846 | if (!journal->j_wbuf) { | 
| Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 847 | printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n", | 
| Harvey Harrison | 08fc99b | 2008-04-28 02:16:16 -0700 | [diff] [blame] | 848 | __func__); | 
| Jan Kara | ecca9af | 2009-04-02 16:57:13 -0700 | [diff] [blame] | 849 | goto out_err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | } | 
|  | 851 |  | 
|  | 852 | err = journal_bmap(journal, 0, &blocknr); | 
|  | 853 | /* If that failed, give up */ | 
|  | 854 | if (err) { | 
| Justin P. Mattock | 3c26bdb | 2011-02-26 20:34:05 -0800 | [diff] [blame] | 855 | printk(KERN_ERR "%s: Cannot locate journal superblock\n", | 
| Harvey Harrison | 08fc99b | 2008-04-28 02:16:16 -0700 | [diff] [blame] | 856 | __func__); | 
| Jan Kara | ecca9af | 2009-04-02 16:57:13 -0700 | [diff] [blame] | 857 | goto out_err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | } | 
|  | 859 |  | 
|  | 860 | bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize); | 
| Jan Kara | ecca9af | 2009-04-02 16:57:13 -0700 | [diff] [blame] | 861 | if (!bh) { | 
|  | 862 | printk(KERN_ERR | 
|  | 863 | "%s: Cannot get buffer for journal superblock\n", | 
|  | 864 | __func__); | 
|  | 865 | goto out_err; | 
|  | 866 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | journal->j_sb_buffer = bh; | 
|  | 868 | journal->j_superblock = (journal_superblock_t *)bh->b_data; | 
|  | 869 |  | 
|  | 870 | return journal; | 
| Jan Kara | ecca9af | 2009-04-02 16:57:13 -0700 | [diff] [blame] | 871 | out_err: | 
| Tao Ma | 7b02bec | 2009-11-10 17:13:22 +0800 | [diff] [blame] | 872 | kfree(journal->j_wbuf); | 
| Jan Kara | ecca9af | 2009-04-02 16:57:13 -0700 | [diff] [blame] | 873 | kfree(journal); | 
|  | 874 | return NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | } | 
|  | 876 |  | 
| Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 877 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | * If the journal init or create aborts, we need to mark the journal | 
|  | 879 | * superblock as being NULL to prevent the journal destroy from writing | 
| Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 880 | * back a bogus superblock. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | */ | 
|  | 882 | static void journal_fail_superblock (journal_t *journal) | 
|  | 883 | { | 
|  | 884 | struct buffer_head *bh = journal->j_sb_buffer; | 
|  | 885 | brelse(bh); | 
|  | 886 | journal->j_sb_buffer = NULL; | 
|  | 887 | } | 
|  | 888 |  | 
|  | 889 | /* | 
|  | 890 | * Given a journal_t structure, initialise the various fields for | 
|  | 891 | * startup of a new journaling session.  We use this both when creating | 
|  | 892 | * a journal, and after recovering an old journal to reset it for | 
|  | 893 | * subsequent use. | 
|  | 894 | */ | 
|  | 895 |  | 
|  | 896 | static int journal_reset(journal_t *journal) | 
|  | 897 | { | 
|  | 898 | journal_superblock_t *sb = journal->j_superblock; | 
| Jan Kara | 9c28cbc | 2009-08-03 19:21:00 +0200 | [diff] [blame] | 899 | unsigned int first, last; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 |  | 
|  | 901 | first = be32_to_cpu(sb->s_first); | 
|  | 902 | last = be32_to_cpu(sb->s_maxlen); | 
| Jan Kara | 7447a66 | 2009-07-15 20:36:08 +0200 | [diff] [blame] | 903 | if (first + JFS_MIN_JOURNAL_BLOCKS > last + 1) { | 
| Jan Kara | 9c28cbc | 2009-08-03 19:21:00 +0200 | [diff] [blame] | 904 | printk(KERN_ERR "JBD: Journal too short (blocks %u-%u).\n", | 
| Jan Kara | 7447a66 | 2009-07-15 20:36:08 +0200 | [diff] [blame] | 905 | first, last); | 
|  | 906 | journal_fail_superblock(journal); | 
|  | 907 | return -EINVAL; | 
|  | 908 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 |  | 
|  | 910 | journal->j_first = first; | 
|  | 911 | journal->j_last = last; | 
|  | 912 |  | 
|  | 913 | journal->j_head = first; | 
|  | 914 | journal->j_tail = first; | 
|  | 915 | journal->j_free = last - first; | 
|  | 916 |  | 
|  | 917 | journal->j_tail_sequence = journal->j_transaction_sequence; | 
|  | 918 | journal->j_commit_sequence = journal->j_transaction_sequence - 1; | 
|  | 919 | journal->j_commit_request = journal->j_commit_sequence; | 
|  | 920 |  | 
|  | 921 | journal->j_max_transaction_buffers = journal->j_maxlen / 4; | 
|  | 922 |  | 
|  | 923 | /* Add the dynamic fields and write it to disk. */ | 
|  | 924 | journal_update_superblock(journal, 1); | 
| Pavel Emelianov | 97f0678 | 2007-05-08 00:30:42 -0700 | [diff] [blame] | 925 | return journal_start_thread(journal); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | } | 
|  | 927 |  | 
| Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 928 | /** | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | * int journal_create() - Initialise the new journal file | 
|  | 930 | * @journal: Journal to create. This structure must have been initialised | 
| Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 931 | * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | * Given a journal_t structure which tells us which disk blocks we can | 
|  | 933 | * use, create a new journal superblock and initialise all of the | 
| Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 934 | * journal fields from scratch. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | **/ | 
|  | 936 | int journal_create(journal_t *journal) | 
|  | 937 | { | 
| Jan Kara | 9c28cbc | 2009-08-03 19:21:00 +0200 | [diff] [blame] | 938 | unsigned int blocknr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | struct buffer_head *bh; | 
|  | 940 | journal_superblock_t *sb; | 
|  | 941 | int i, err; | 
|  | 942 |  | 
|  | 943 | if (journal->j_maxlen < JFS_MIN_JOURNAL_BLOCKS) { | 
|  | 944 | printk (KERN_ERR "Journal length (%d blocks) too short.\n", | 
|  | 945 | journal->j_maxlen); | 
|  | 946 | journal_fail_superblock(journal); | 
|  | 947 | return -EINVAL; | 
|  | 948 | } | 
|  | 949 |  | 
|  | 950 | if (journal->j_inode == NULL) { | 
|  | 951 | /* | 
|  | 952 | * We don't know what block to start at! | 
|  | 953 | */ | 
|  | 954 | printk(KERN_EMERG | 
|  | 955 | "%s: creation of journal on external device!\n", | 
| Harvey Harrison | 08fc99b | 2008-04-28 02:16:16 -0700 | [diff] [blame] | 956 | __func__); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | BUG(); | 
|  | 958 | } | 
|  | 959 |  | 
|  | 960 | /* Zero out the entire journal on disk.  We cannot afford to | 
|  | 961 | have any blocks on disk beginning with JFS_MAGIC_NUMBER. */ | 
|  | 962 | jbd_debug(1, "JBD: Zeroing out journal blocks...\n"); | 
|  | 963 | for (i = 0; i < journal->j_maxlen; i++) { | 
|  | 964 | err = journal_bmap(journal, i, &blocknr); | 
|  | 965 | if (err) | 
|  | 966 | return err; | 
|  | 967 | bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize); | 
| Namhyung Kim | 2a0e338 | 2010-10-13 17:17:18 +0900 | [diff] [blame] | 968 | if (unlikely(!bh)) | 
|  | 969 | return -ENOMEM; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | lock_buffer(bh); | 
|  | 971 | memset (bh->b_data, 0, journal->j_blocksize); | 
|  | 972 | BUFFER_TRACE(bh, "marking dirty"); | 
|  | 973 | mark_buffer_dirty(bh); | 
|  | 974 | BUFFER_TRACE(bh, "marking uptodate"); | 
|  | 975 | set_buffer_uptodate(bh); | 
|  | 976 | unlock_buffer(bh); | 
|  | 977 | __brelse(bh); | 
|  | 978 | } | 
|  | 979 |  | 
|  | 980 | sync_blockdev(journal->j_dev); | 
|  | 981 | jbd_debug(1, "JBD: journal cleared.\n"); | 
|  | 982 |  | 
|  | 983 | /* OK, fill in the initial static fields in the new superblock */ | 
|  | 984 | sb = journal->j_superblock; | 
|  | 985 |  | 
|  | 986 | sb->s_header.h_magic	 = cpu_to_be32(JFS_MAGIC_NUMBER); | 
|  | 987 | sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2); | 
|  | 988 |  | 
|  | 989 | sb->s_blocksize	= cpu_to_be32(journal->j_blocksize); | 
|  | 990 | sb->s_maxlen	= cpu_to_be32(journal->j_maxlen); | 
|  | 991 | sb->s_first	= cpu_to_be32(1); | 
|  | 992 |  | 
|  | 993 | journal->j_transaction_sequence = 1; | 
|  | 994 |  | 
|  | 995 | journal->j_flags &= ~JFS_ABORT; | 
|  | 996 | journal->j_format_version = 2; | 
|  | 997 |  | 
|  | 998 | return journal_reset(journal); | 
|  | 999 | } | 
|  | 1000 |  | 
| Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1001 | /** | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | * void journal_update_superblock() - Update journal sb on disk. | 
|  | 1003 | * @journal: The journal to update. | 
|  | 1004 | * @wait: Set to '0' if you don't want to wait for IO completion. | 
|  | 1005 | * | 
|  | 1006 | * Update a journal's dynamic superblock fields and write it to disk, | 
|  | 1007 | * optionally waiting for the IO to complete. | 
|  | 1008 | */ | 
|  | 1009 | void journal_update_superblock(journal_t *journal, int wait) | 
|  | 1010 | { | 
|  | 1011 | journal_superblock_t *sb = journal->j_superblock; | 
|  | 1012 | struct buffer_head *bh = journal->j_sb_buffer; | 
|  | 1013 |  | 
|  | 1014 | /* | 
|  | 1015 | * As a special case, if the on-disk copy is already marked as needing | 
|  | 1016 | * no recovery (s_start == 0) and there are no outstanding transactions | 
|  | 1017 | * in the filesystem, then we can safely defer the superblock update | 
|  | 1018 | * until the next commit by setting JFS_FLUSHED.  This avoids | 
|  | 1019 | * attempting a write to a potential-readonly device. | 
|  | 1020 | */ | 
|  | 1021 | if (sb->s_start == 0 && journal->j_tail_sequence == | 
|  | 1022 | journal->j_transaction_sequence) { | 
|  | 1023 | jbd_debug(1,"JBD: Skipping superblock update on recovered sb " | 
| Jan Kara | 9c28cbc | 2009-08-03 19:21:00 +0200 | [diff] [blame] | 1024 | "(start %u, seq %d, errno %d)\n", | 
| Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1025 | journal->j_tail, journal->j_tail_sequence, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | journal->j_errno); | 
|  | 1027 | goto out; | 
|  | 1028 | } | 
|  | 1029 |  | 
| Darrick J. Wong | dff6825 | 2010-10-04 12:35:05 -0700 | [diff] [blame] | 1030 | if (buffer_write_io_error(bh)) { | 
|  | 1031 | char b[BDEVNAME_SIZE]; | 
|  | 1032 | /* | 
|  | 1033 | * Oh, dear.  A previous attempt to write the journal | 
|  | 1034 | * superblock failed.  This could happen because the | 
|  | 1035 | * USB device was yanked out.  Or it could happen to | 
|  | 1036 | * be a transient write error and maybe the block will | 
|  | 1037 | * be remapped.  Nothing we can do but to retry the | 
|  | 1038 | * write and hope for the best. | 
|  | 1039 | */ | 
|  | 1040 | printk(KERN_ERR "JBD: previous I/O error detected " | 
|  | 1041 | "for journal superblock update for %s.\n", | 
|  | 1042 | journal_dev_name(journal, b)); | 
|  | 1043 | clear_buffer_write_io_error(bh); | 
|  | 1044 | set_buffer_uptodate(bh); | 
|  | 1045 | } | 
|  | 1046 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | spin_lock(&journal->j_state_lock); | 
| Jan Kara | 9c28cbc | 2009-08-03 19:21:00 +0200 | [diff] [blame] | 1048 | jbd_debug(1,"JBD: updating superblock (start %u, seq %d, errno %d)\n", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | journal->j_tail, journal->j_tail_sequence, journal->j_errno); | 
|  | 1050 |  | 
|  | 1051 | sb->s_sequence = cpu_to_be32(journal->j_tail_sequence); | 
|  | 1052 | sb->s_start    = cpu_to_be32(journal->j_tail); | 
|  | 1053 | sb->s_errno    = cpu_to_be32(journal->j_errno); | 
|  | 1054 | spin_unlock(&journal->j_state_lock); | 
|  | 1055 |  | 
|  | 1056 | BUFFER_TRACE(bh, "marking dirty"); | 
|  | 1057 | mark_buffer_dirty(bh); | 
| Darrick J. Wong | dff6825 | 2010-10-04 12:35:05 -0700 | [diff] [blame] | 1058 | if (wait) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | sync_dirty_buffer(bh); | 
| Darrick J. Wong | dff6825 | 2010-10-04 12:35:05 -0700 | [diff] [blame] | 1060 | if (buffer_write_io_error(bh)) { | 
|  | 1061 | char b[BDEVNAME_SIZE]; | 
|  | 1062 | printk(KERN_ERR "JBD: I/O error detected " | 
|  | 1063 | "when updating journal superblock for %s.\n", | 
|  | 1064 | journal_dev_name(journal, b)); | 
|  | 1065 | clear_buffer_write_io_error(bh); | 
|  | 1066 | set_buffer_uptodate(bh); | 
|  | 1067 | } | 
|  | 1068 | } else | 
| Christoph Hellwig | 9cb569d | 2010-08-11 17:06:24 +0200 | [diff] [blame] | 1069 | write_dirty_buffer(bh, WRITE); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 |  | 
| Lukas Czerner | 99cb1a3 | 2011-05-23 18:33:02 +0200 | [diff] [blame] | 1071 | trace_jbd_update_superblock_end(journal, wait); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | out: | 
|  | 1073 | /* If we have just flushed the log (by marking s_start==0), then | 
|  | 1074 | * any future commit will have to be careful to update the | 
|  | 1075 | * superblock again to re-record the true start of the log. */ | 
|  | 1076 |  | 
|  | 1077 | spin_lock(&journal->j_state_lock); | 
|  | 1078 | if (sb->s_start) | 
|  | 1079 | journal->j_flags &= ~JFS_FLUSHED; | 
|  | 1080 | else | 
|  | 1081 | journal->j_flags |= JFS_FLUSHED; | 
|  | 1082 | spin_unlock(&journal->j_state_lock); | 
|  | 1083 | } | 
|  | 1084 |  | 
|  | 1085 | /* | 
|  | 1086 | * Read the superblock for a given journal, performing initial | 
|  | 1087 | * validation of the format. | 
|  | 1088 | */ | 
|  | 1089 |  | 
|  | 1090 | static int journal_get_superblock(journal_t *journal) | 
|  | 1091 | { | 
|  | 1092 | struct buffer_head *bh; | 
|  | 1093 | journal_superblock_t *sb; | 
|  | 1094 | int err = -EIO; | 
|  | 1095 |  | 
|  | 1096 | bh = journal->j_sb_buffer; | 
|  | 1097 |  | 
|  | 1098 | J_ASSERT(bh != NULL); | 
|  | 1099 | if (!buffer_uptodate(bh)) { | 
|  | 1100 | ll_rw_block(READ, 1, &bh); | 
|  | 1101 | wait_on_buffer(bh); | 
|  | 1102 | if (!buffer_uptodate(bh)) { | 
|  | 1103 | printk (KERN_ERR | 
|  | 1104 | "JBD: IO error reading journal superblock\n"); | 
|  | 1105 | goto out; | 
|  | 1106 | } | 
|  | 1107 | } | 
|  | 1108 |  | 
|  | 1109 | sb = journal->j_superblock; | 
|  | 1110 |  | 
|  | 1111 | err = -EINVAL; | 
|  | 1112 |  | 
|  | 1113 | if (sb->s_header.h_magic != cpu_to_be32(JFS_MAGIC_NUMBER) || | 
|  | 1114 | sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) { | 
|  | 1115 | printk(KERN_WARNING "JBD: no valid journal superblock found\n"); | 
|  | 1116 | goto out; | 
|  | 1117 | } | 
|  | 1118 |  | 
|  | 1119 | switch(be32_to_cpu(sb->s_header.h_blocktype)) { | 
|  | 1120 | case JFS_SUPERBLOCK_V1: | 
|  | 1121 | journal->j_format_version = 1; | 
|  | 1122 | break; | 
|  | 1123 | case JFS_SUPERBLOCK_V2: | 
|  | 1124 | journal->j_format_version = 2; | 
|  | 1125 | break; | 
|  | 1126 | default: | 
|  | 1127 | printk(KERN_WARNING "JBD: unrecognised superblock format ID\n"); | 
|  | 1128 | goto out; | 
|  | 1129 | } | 
|  | 1130 |  | 
|  | 1131 | if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen) | 
|  | 1132 | journal->j_maxlen = be32_to_cpu(sb->s_maxlen); | 
|  | 1133 | else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) { | 
|  | 1134 | printk (KERN_WARNING "JBD: journal file too short\n"); | 
|  | 1135 | goto out; | 
|  | 1136 | } | 
|  | 1137 |  | 
| Eryu Guan | 8762202 | 2011-11-01 19:04:59 -0400 | [diff] [blame] | 1138 | if (be32_to_cpu(sb->s_first) == 0 || | 
|  | 1139 | be32_to_cpu(sb->s_first) >= journal->j_maxlen) { | 
|  | 1140 | printk(KERN_WARNING | 
|  | 1141 | "JBD: Invalid start block of journal: %u\n", | 
|  | 1142 | be32_to_cpu(sb->s_first)); | 
|  | 1143 | goto out; | 
|  | 1144 | } | 
|  | 1145 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | return 0; | 
|  | 1147 |  | 
|  | 1148 | out: | 
|  | 1149 | journal_fail_superblock(journal); | 
|  | 1150 | return err; | 
|  | 1151 | } | 
|  | 1152 |  | 
|  | 1153 | /* | 
|  | 1154 | * Load the on-disk journal superblock and read the key fields into the | 
|  | 1155 | * journal_t. | 
|  | 1156 | */ | 
|  | 1157 |  | 
|  | 1158 | static int load_superblock(journal_t *journal) | 
|  | 1159 | { | 
|  | 1160 | int err; | 
|  | 1161 | journal_superblock_t *sb; | 
|  | 1162 |  | 
|  | 1163 | err = journal_get_superblock(journal); | 
|  | 1164 | if (err) | 
|  | 1165 | return err; | 
|  | 1166 |  | 
|  | 1167 | sb = journal->j_superblock; | 
|  | 1168 |  | 
|  | 1169 | journal->j_tail_sequence = be32_to_cpu(sb->s_sequence); | 
|  | 1170 | journal->j_tail = be32_to_cpu(sb->s_start); | 
|  | 1171 | journal->j_first = be32_to_cpu(sb->s_first); | 
|  | 1172 | journal->j_last = be32_to_cpu(sb->s_maxlen); | 
|  | 1173 | journal->j_errno = be32_to_cpu(sb->s_errno); | 
|  | 1174 |  | 
|  | 1175 | return 0; | 
|  | 1176 | } | 
|  | 1177 |  | 
|  | 1178 |  | 
|  | 1179 | /** | 
|  | 1180 | * int journal_load() - Read journal from disk. | 
|  | 1181 | * @journal: Journal to act on. | 
| Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1182 | * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | * Given a journal_t structure which tells us which disk blocks contain | 
|  | 1184 | * a journal, read the journal from disk to initialise the in-memory | 
|  | 1185 | * structures. | 
|  | 1186 | */ | 
|  | 1187 | int journal_load(journal_t *journal) | 
|  | 1188 | { | 
|  | 1189 | int err; | 
| Badari Pulavarty | ea81739 | 2006-08-27 01:23:52 -0700 | [diff] [blame] | 1190 | journal_superblock_t *sb; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 |  | 
|  | 1192 | err = load_superblock(journal); | 
|  | 1193 | if (err) | 
|  | 1194 | return err; | 
|  | 1195 |  | 
| Badari Pulavarty | ea81739 | 2006-08-27 01:23:52 -0700 | [diff] [blame] | 1196 | sb = journal->j_superblock; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | /* If this is a V2 superblock, then we have to check the | 
|  | 1198 | * features flags on it. */ | 
|  | 1199 |  | 
|  | 1200 | if (journal->j_format_version >= 2) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | if ((sb->s_feature_ro_compat & | 
|  | 1202 | ~cpu_to_be32(JFS_KNOWN_ROCOMPAT_FEATURES)) || | 
|  | 1203 | (sb->s_feature_incompat & | 
|  | 1204 | ~cpu_to_be32(JFS_KNOWN_INCOMPAT_FEATURES))) { | 
|  | 1205 | printk (KERN_WARNING | 
|  | 1206 | "JBD: Unrecognised features on journal\n"); | 
|  | 1207 | return -EINVAL; | 
|  | 1208 | } | 
|  | 1209 | } | 
|  | 1210 |  | 
|  | 1211 | /* Let the recovery code check whether it needs to recover any | 
|  | 1212 | * data from the journal. */ | 
|  | 1213 | if (journal_recover(journal)) | 
|  | 1214 | goto recovery_error; | 
|  | 1215 |  | 
|  | 1216 | /* OK, we've finished with the dynamic journal bits: | 
|  | 1217 | * reinitialise the dynamic contents of the superblock in memory | 
|  | 1218 | * and reset them on disk. */ | 
|  | 1219 | if (journal_reset(journal)) | 
|  | 1220 | goto recovery_error; | 
|  | 1221 |  | 
|  | 1222 | journal->j_flags &= ~JFS_ABORT; | 
|  | 1223 | journal->j_flags |= JFS_LOADED; | 
|  | 1224 | return 0; | 
|  | 1225 |  | 
|  | 1226 | recovery_error: | 
|  | 1227 | printk (KERN_WARNING "JBD: recovery failed\n"); | 
|  | 1228 | return -EIO; | 
|  | 1229 | } | 
|  | 1230 |  | 
|  | 1231 | /** | 
|  | 1232 | * void journal_destroy() - Release a journal_t structure. | 
|  | 1233 | * @journal: Journal to act on. | 
|  | 1234 | * | 
|  | 1235 | * Release a journal_t structure once it is no longer in use by the | 
|  | 1236 | * journaled object. | 
| Hidehiro Kawai | 4afe978 | 2008-10-22 14:15:00 -0700 | [diff] [blame] | 1237 | * Return <0 if we couldn't clean up the journal. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | */ | 
| Hidehiro Kawai | 4afe978 | 2008-10-22 14:15:00 -0700 | [diff] [blame] | 1239 | int journal_destroy(journal_t *journal) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | { | 
| Hidehiro Kawai | 4afe978 | 2008-10-22 14:15:00 -0700 | [diff] [blame] | 1241 | int err = 0; | 
|  | 1242 |  | 
| Jan Kara | 03f4d80 | 2010-04-15 22:16:24 +0200 | [diff] [blame] | 1243 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | /* Wait for the commit thread to wake up and die. */ | 
|  | 1245 | journal_kill_thread(journal); | 
|  | 1246 |  | 
|  | 1247 | /* Force a final log commit */ | 
|  | 1248 | if (journal->j_running_transaction) | 
|  | 1249 | journal_commit_transaction(journal); | 
|  | 1250 |  | 
|  | 1251 | /* Force any old transactions to disk */ | 
|  | 1252 |  | 
|  | 1253 | /* Totally anal locking here... */ | 
|  | 1254 | spin_lock(&journal->j_list_lock); | 
|  | 1255 | while (journal->j_checkpoint_transactions != NULL) { | 
|  | 1256 | spin_unlock(&journal->j_list_lock); | 
|  | 1257 | log_do_checkpoint(journal); | 
|  | 1258 | spin_lock(&journal->j_list_lock); | 
|  | 1259 | } | 
|  | 1260 |  | 
|  | 1261 | J_ASSERT(journal->j_running_transaction == NULL); | 
|  | 1262 | J_ASSERT(journal->j_committing_transaction == NULL); | 
|  | 1263 | J_ASSERT(journal->j_checkpoint_transactions == NULL); | 
|  | 1264 | spin_unlock(&journal->j_list_lock); | 
|  | 1265 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | if (journal->j_sb_buffer) { | 
| Hidehiro Kawai | 4afe978 | 2008-10-22 14:15:00 -0700 | [diff] [blame] | 1267 | if (!is_journal_aborted(journal)) { | 
|  | 1268 | /* We can now mark the journal as empty. */ | 
|  | 1269 | journal->j_tail = 0; | 
|  | 1270 | journal->j_tail_sequence = | 
|  | 1271 | ++journal->j_transaction_sequence; | 
|  | 1272 | journal_update_superblock(journal, 1); | 
|  | 1273 | } else { | 
|  | 1274 | err = -EIO; | 
|  | 1275 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | brelse(journal->j_sb_buffer); | 
|  | 1277 | } | 
|  | 1278 |  | 
|  | 1279 | if (journal->j_inode) | 
|  | 1280 | iput(journal->j_inode); | 
|  | 1281 | if (journal->j_revoke) | 
|  | 1282 | journal_destroy_revoke(journal); | 
|  | 1283 | kfree(journal->j_wbuf); | 
|  | 1284 | kfree(journal); | 
| Hidehiro Kawai | 4afe978 | 2008-10-22 14:15:00 -0700 | [diff] [blame] | 1285 |  | 
|  | 1286 | return err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | } | 
|  | 1288 |  | 
|  | 1289 |  | 
|  | 1290 | /** | 
|  | 1291 | *int journal_check_used_features () - Check if features specified are used. | 
|  | 1292 | * @journal: Journal to check. | 
|  | 1293 | * @compat: bitmask of compatible features | 
|  | 1294 | * @ro: bitmask of features that force read-only mount | 
|  | 1295 | * @incompat: bitmask of incompatible features | 
| Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1296 | * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | * Check whether the journal uses all of a given set of | 
| Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1298 | * features.  Return true (non-zero) if it does. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | **/ | 
|  | 1300 |  | 
|  | 1301 | int journal_check_used_features (journal_t *journal, unsigned long compat, | 
|  | 1302 | unsigned long ro, unsigned long incompat) | 
|  | 1303 | { | 
|  | 1304 | journal_superblock_t *sb; | 
|  | 1305 |  | 
|  | 1306 | if (!compat && !ro && !incompat) | 
|  | 1307 | return 1; | 
|  | 1308 | if (journal->j_format_version == 1) | 
|  | 1309 | return 0; | 
|  | 1310 |  | 
|  | 1311 | sb = journal->j_superblock; | 
|  | 1312 |  | 
|  | 1313 | if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) && | 
|  | 1314 | ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) && | 
|  | 1315 | ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat)) | 
|  | 1316 | return 1; | 
|  | 1317 |  | 
|  | 1318 | return 0; | 
|  | 1319 | } | 
|  | 1320 |  | 
|  | 1321 | /** | 
|  | 1322 | * int journal_check_available_features() - Check feature set in journalling layer | 
|  | 1323 | * @journal: Journal to check. | 
|  | 1324 | * @compat: bitmask of compatible features | 
|  | 1325 | * @ro: bitmask of features that force read-only mount | 
|  | 1326 | * @incompat: bitmask of incompatible features | 
| Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1327 | * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | * Check whether the journaling code supports the use of | 
|  | 1329 | * all of a given set of features on this journal.  Return true | 
|  | 1330 | * (non-zero) if it can. */ | 
|  | 1331 |  | 
|  | 1332 | int journal_check_available_features (journal_t *journal, unsigned long compat, | 
|  | 1333 | unsigned long ro, unsigned long incompat) | 
|  | 1334 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | if (!compat && !ro && !incompat) | 
|  | 1336 | return 1; | 
|  | 1337 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | /* We can support any known requested features iff the | 
|  | 1339 | * superblock is in version 2.  Otherwise we fail to support any | 
|  | 1340 | * extended sb features. */ | 
|  | 1341 |  | 
|  | 1342 | if (journal->j_format_version != 2) | 
|  | 1343 | return 0; | 
|  | 1344 |  | 
|  | 1345 | if ((compat   & JFS_KNOWN_COMPAT_FEATURES) == compat && | 
|  | 1346 | (ro       & JFS_KNOWN_ROCOMPAT_FEATURES) == ro && | 
|  | 1347 | (incompat & JFS_KNOWN_INCOMPAT_FEATURES) == incompat) | 
|  | 1348 | return 1; | 
|  | 1349 |  | 
|  | 1350 | return 0; | 
|  | 1351 | } | 
|  | 1352 |  | 
|  | 1353 | /** | 
|  | 1354 | * int journal_set_features () - Mark a given journal feature in the superblock | 
|  | 1355 | * @journal: Journal to act on. | 
|  | 1356 | * @compat: bitmask of compatible features | 
|  | 1357 | * @ro: bitmask of features that force read-only mount | 
|  | 1358 | * @incompat: bitmask of incompatible features | 
|  | 1359 | * | 
|  | 1360 | * Mark a given journal feature as present on the | 
| Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1361 | * superblock.  Returns true if the requested features could be set. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | * | 
|  | 1363 | */ | 
|  | 1364 |  | 
|  | 1365 | int journal_set_features (journal_t *journal, unsigned long compat, | 
|  | 1366 | unsigned long ro, unsigned long incompat) | 
|  | 1367 | { | 
|  | 1368 | journal_superblock_t *sb; | 
|  | 1369 |  | 
|  | 1370 | if (journal_check_used_features(journal, compat, ro, incompat)) | 
|  | 1371 | return 1; | 
|  | 1372 |  | 
|  | 1373 | if (!journal_check_available_features(journal, compat, ro, incompat)) | 
|  | 1374 | return 0; | 
|  | 1375 |  | 
|  | 1376 | jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n", | 
|  | 1377 | compat, ro, incompat); | 
|  | 1378 |  | 
|  | 1379 | sb = journal->j_superblock; | 
|  | 1380 |  | 
|  | 1381 | sb->s_feature_compat    |= cpu_to_be32(compat); | 
|  | 1382 | sb->s_feature_ro_compat |= cpu_to_be32(ro); | 
|  | 1383 | sb->s_feature_incompat  |= cpu_to_be32(incompat); | 
|  | 1384 |  | 
|  | 1385 | return 1; | 
|  | 1386 | } | 
|  | 1387 |  | 
|  | 1388 |  | 
|  | 1389 | /** | 
|  | 1390 | * int journal_update_format () - Update on-disk journal structure. | 
|  | 1391 | * @journal: Journal to act on. | 
|  | 1392 | * | 
|  | 1393 | * Given an initialised but unloaded journal struct, poke about in the | 
|  | 1394 | * on-disk structure to update it to the most recent supported version. | 
|  | 1395 | */ | 
|  | 1396 | int journal_update_format (journal_t *journal) | 
|  | 1397 | { | 
|  | 1398 | journal_superblock_t *sb; | 
|  | 1399 | int err; | 
|  | 1400 |  | 
|  | 1401 | err = journal_get_superblock(journal); | 
|  | 1402 | if (err) | 
|  | 1403 | return err; | 
|  | 1404 |  | 
|  | 1405 | sb = journal->j_superblock; | 
|  | 1406 |  | 
|  | 1407 | switch (be32_to_cpu(sb->s_header.h_blocktype)) { | 
|  | 1408 | case JFS_SUPERBLOCK_V2: | 
|  | 1409 | return 0; | 
|  | 1410 | case JFS_SUPERBLOCK_V1: | 
|  | 1411 | return journal_convert_superblock_v1(journal, sb); | 
|  | 1412 | default: | 
|  | 1413 | break; | 
|  | 1414 | } | 
|  | 1415 | return -EINVAL; | 
|  | 1416 | } | 
|  | 1417 |  | 
|  | 1418 | static int journal_convert_superblock_v1(journal_t *journal, | 
|  | 1419 | journal_superblock_t *sb) | 
|  | 1420 | { | 
|  | 1421 | int offset, blocksize; | 
|  | 1422 | struct buffer_head *bh; | 
|  | 1423 |  | 
|  | 1424 | printk(KERN_WARNING | 
|  | 1425 | "JBD: Converting superblock from version 1 to 2.\n"); | 
|  | 1426 |  | 
|  | 1427 | /* Pre-initialise new fields to zero */ | 
|  | 1428 | offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb); | 
|  | 1429 | blocksize = be32_to_cpu(sb->s_blocksize); | 
|  | 1430 | memset(&sb->s_feature_compat, 0, blocksize-offset); | 
|  | 1431 |  | 
|  | 1432 | sb->s_nr_users = cpu_to_be32(1); | 
|  | 1433 | sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2); | 
|  | 1434 | journal->j_format_version = 2; | 
|  | 1435 |  | 
|  | 1436 | bh = journal->j_sb_buffer; | 
|  | 1437 | BUFFER_TRACE(bh, "marking dirty"); | 
|  | 1438 | mark_buffer_dirty(bh); | 
|  | 1439 | sync_dirty_buffer(bh); | 
|  | 1440 | return 0; | 
|  | 1441 | } | 
|  | 1442 |  | 
|  | 1443 |  | 
|  | 1444 | /** | 
|  | 1445 | * int journal_flush () - Flush journal | 
|  | 1446 | * @journal: Journal to act on. | 
| Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1447 | * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | * Flush all data for a given journal to disk and empty the journal. | 
|  | 1449 | * Filesystems can use this when remounting readonly to ensure that | 
|  | 1450 | * recovery does not need to happen on remount. | 
|  | 1451 | */ | 
|  | 1452 |  | 
|  | 1453 | int journal_flush(journal_t *journal) | 
|  | 1454 | { | 
|  | 1455 | int err = 0; | 
|  | 1456 | transaction_t *transaction = NULL; | 
| Jan Kara | 9c28cbc | 2009-08-03 19:21:00 +0200 | [diff] [blame] | 1457 | unsigned int old_tail; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 |  | 
|  | 1459 | spin_lock(&journal->j_state_lock); | 
|  | 1460 |  | 
|  | 1461 | /* Force everything buffered to the log... */ | 
|  | 1462 | if (journal->j_running_transaction) { | 
|  | 1463 | transaction = journal->j_running_transaction; | 
|  | 1464 | __log_start_commit(journal, transaction->t_tid); | 
|  | 1465 | } else if (journal->j_committing_transaction) | 
|  | 1466 | transaction = journal->j_committing_transaction; | 
|  | 1467 |  | 
|  | 1468 | /* Wait for the log commit to complete... */ | 
|  | 1469 | if (transaction) { | 
|  | 1470 | tid_t tid = transaction->t_tid; | 
|  | 1471 |  | 
|  | 1472 | spin_unlock(&journal->j_state_lock); | 
|  | 1473 | log_wait_commit(journal, tid); | 
|  | 1474 | } else { | 
|  | 1475 | spin_unlock(&journal->j_state_lock); | 
|  | 1476 | } | 
|  | 1477 |  | 
|  | 1478 | /* ...and flush everything in the log out to disk. */ | 
|  | 1479 | spin_lock(&journal->j_list_lock); | 
|  | 1480 | while (!err && journal->j_checkpoint_transactions != NULL) { | 
|  | 1481 | spin_unlock(&journal->j_list_lock); | 
| Hidehiro Kawai | 4afe978 | 2008-10-22 14:15:00 -0700 | [diff] [blame] | 1482 | mutex_lock(&journal->j_checkpoint_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | err = log_do_checkpoint(journal); | 
| Hidehiro Kawai | 4afe978 | 2008-10-22 14:15:00 -0700 | [diff] [blame] | 1484 | mutex_unlock(&journal->j_checkpoint_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | spin_lock(&journal->j_list_lock); | 
|  | 1486 | } | 
|  | 1487 | spin_unlock(&journal->j_list_lock); | 
| Hidehiro Kawai | 4afe978 | 2008-10-22 14:15:00 -0700 | [diff] [blame] | 1488 |  | 
|  | 1489 | if (is_journal_aborted(journal)) | 
|  | 1490 | return -EIO; | 
|  | 1491 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | cleanup_journal_tail(journal); | 
|  | 1493 |  | 
|  | 1494 | /* Finally, mark the journal as really needing no recovery. | 
|  | 1495 | * This sets s_start==0 in the underlying superblock, which is | 
|  | 1496 | * the magic code for a fully-recovered superblock.  Any future | 
|  | 1497 | * commits of data to the journal will restore the current | 
|  | 1498 | * s_start value. */ | 
|  | 1499 | spin_lock(&journal->j_state_lock); | 
|  | 1500 | old_tail = journal->j_tail; | 
|  | 1501 | journal->j_tail = 0; | 
|  | 1502 | spin_unlock(&journal->j_state_lock); | 
|  | 1503 | journal_update_superblock(journal, 1); | 
|  | 1504 | spin_lock(&journal->j_state_lock); | 
|  | 1505 | journal->j_tail = old_tail; | 
|  | 1506 |  | 
|  | 1507 | J_ASSERT(!journal->j_running_transaction); | 
|  | 1508 | J_ASSERT(!journal->j_committing_transaction); | 
|  | 1509 | J_ASSERT(!journal->j_checkpoint_transactions); | 
|  | 1510 | J_ASSERT(journal->j_head == journal->j_tail); | 
|  | 1511 | J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence); | 
|  | 1512 | spin_unlock(&journal->j_state_lock); | 
| Hidehiro Kawai | 4afe978 | 2008-10-22 14:15:00 -0700 | [diff] [blame] | 1513 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | } | 
|  | 1515 |  | 
|  | 1516 | /** | 
|  | 1517 | * int journal_wipe() - Wipe journal contents | 
|  | 1518 | * @journal: Journal to act on. | 
|  | 1519 | * @write: flag (see below) | 
| Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1520 | * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | * Wipe out all of the contents of a journal, safely.  This will produce | 
|  | 1522 | * a warning if the journal contains any valid recovery information. | 
|  | 1523 | * Must be called between journal_init_*() and journal_load(). | 
|  | 1524 | * | 
|  | 1525 | * If 'write' is non-zero, then we wipe out the journal on disk; otherwise | 
|  | 1526 | * we merely suppress recovery. | 
|  | 1527 | */ | 
|  | 1528 |  | 
|  | 1529 | int journal_wipe(journal_t *journal, int write) | 
|  | 1530 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | int err = 0; | 
|  | 1532 |  | 
|  | 1533 | J_ASSERT (!(journal->j_flags & JFS_LOADED)); | 
|  | 1534 |  | 
|  | 1535 | err = load_superblock(journal); | 
|  | 1536 | if (err) | 
|  | 1537 | return err; | 
|  | 1538 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | if (!journal->j_tail) | 
|  | 1540 | goto no_recovery; | 
|  | 1541 |  | 
|  | 1542 | printk (KERN_WARNING "JBD: %s recovery information on journal\n", | 
|  | 1543 | write ? "Clearing" : "Ignoring"); | 
|  | 1544 |  | 
|  | 1545 | err = journal_skip_recovery(journal); | 
|  | 1546 | if (write) | 
|  | 1547 | journal_update_superblock(journal, 1); | 
|  | 1548 |  | 
|  | 1549 | no_recovery: | 
|  | 1550 | return err; | 
|  | 1551 | } | 
|  | 1552 |  | 
|  | 1553 | /* | 
|  | 1554 | * journal_dev_name: format a character string to describe on what | 
|  | 1555 | * device this journal is present. | 
|  | 1556 | */ | 
|  | 1557 |  | 
| Adrian Bunk | 022a4a7 | 2005-09-06 15:16:41 -0700 | [diff] [blame] | 1558 | static const char *journal_dev_name(journal_t *journal, char *buffer) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | { | 
|  | 1560 | struct block_device *bdev; | 
|  | 1561 |  | 
|  | 1562 | if (journal->j_inode) | 
|  | 1563 | bdev = journal->j_inode->i_sb->s_bdev; | 
|  | 1564 | else | 
|  | 1565 | bdev = journal->j_dev; | 
|  | 1566 |  | 
|  | 1567 | return bdevname(bdev, buffer); | 
|  | 1568 | } | 
|  | 1569 |  | 
|  | 1570 | /* | 
|  | 1571 | * Journal abort has very specific semantics, which we describe | 
| Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1572 | * for journal abort. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | * | 
|  | 1574 | * Two internal function, which provide abort to te jbd layer | 
|  | 1575 | * itself are here. | 
|  | 1576 | */ | 
|  | 1577 |  | 
|  | 1578 | /* | 
|  | 1579 | * Quick version for internal journal use (doesn't lock the journal). | 
|  | 1580 | * Aborts hard --- we mark the abort as occurred, but do _nothing_ else, | 
|  | 1581 | * and don't attempt to make any other journal updates. | 
|  | 1582 | */ | 
| Adrian Bunk | 5330838 | 2008-02-06 01:40:12 -0800 | [diff] [blame] | 1583 | static void __journal_abort_hard(journal_t *journal) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | { | 
|  | 1585 | transaction_t *transaction; | 
|  | 1586 | char b[BDEVNAME_SIZE]; | 
|  | 1587 |  | 
|  | 1588 | if (journal->j_flags & JFS_ABORT) | 
|  | 1589 | return; | 
|  | 1590 |  | 
|  | 1591 | printk(KERN_ERR "Aborting journal on device %s.\n", | 
|  | 1592 | journal_dev_name(journal, b)); | 
|  | 1593 |  | 
|  | 1594 | spin_lock(&journal->j_state_lock); | 
|  | 1595 | journal->j_flags |= JFS_ABORT; | 
|  | 1596 | transaction = journal->j_running_transaction; | 
|  | 1597 | if (transaction) | 
|  | 1598 | __log_start_commit(journal, transaction->t_tid); | 
|  | 1599 | spin_unlock(&journal->j_state_lock); | 
|  | 1600 | } | 
|  | 1601 |  | 
|  | 1602 | /* Soft abort: record the abort error status in the journal superblock, | 
|  | 1603 | * but don't do any other IO. */ | 
| Adrian Bunk | 022a4a7 | 2005-09-06 15:16:41 -0700 | [diff] [blame] | 1604 | static void __journal_abort_soft (journal_t *journal, int errno) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | { | 
|  | 1606 | if (journal->j_flags & JFS_ABORT) | 
|  | 1607 | return; | 
|  | 1608 |  | 
|  | 1609 | if (!journal->j_errno) | 
|  | 1610 | journal->j_errno = errno; | 
|  | 1611 |  | 
|  | 1612 | __journal_abort_hard(journal); | 
|  | 1613 |  | 
|  | 1614 | if (errno) | 
|  | 1615 | journal_update_superblock(journal, 1); | 
|  | 1616 | } | 
|  | 1617 |  | 
|  | 1618 | /** | 
|  | 1619 | * void journal_abort () - Shutdown the journal immediately. | 
|  | 1620 | * @journal: the journal to shutdown. | 
|  | 1621 | * @errno:   an error number to record in the journal indicating | 
|  | 1622 | *           the reason for the shutdown. | 
|  | 1623 | * | 
|  | 1624 | * Perform a complete, immediate shutdown of the ENTIRE | 
|  | 1625 | * journal (not of a single transaction).  This operation cannot be | 
|  | 1626 | * undone without closing and reopening the journal. | 
| Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1627 | * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | * The journal_abort function is intended to support higher level error | 
|  | 1629 | * recovery mechanisms such as the ext2/ext3 remount-readonly error | 
|  | 1630 | * mode. | 
|  | 1631 | * | 
|  | 1632 | * Journal abort has very specific semantics.  Any existing dirty, | 
|  | 1633 | * unjournaled buffers in the main filesystem will still be written to | 
|  | 1634 | * disk by bdflush, but the journaling mechanism will be suspended | 
|  | 1635 | * immediately and no further transaction commits will be honoured. | 
|  | 1636 | * | 
|  | 1637 | * Any dirty, journaled buffers will be written back to disk without | 
|  | 1638 | * hitting the journal.  Atomicity cannot be guaranteed on an aborted | 
|  | 1639 | * filesystem, but we _do_ attempt to leave as much data as possible | 
|  | 1640 | * behind for fsck to use for cleanup. | 
|  | 1641 | * | 
|  | 1642 | * Any attempt to get a new transaction handle on a journal which is in | 
|  | 1643 | * ABORT state will just result in an -EROFS error return.  A | 
|  | 1644 | * journal_stop on an existing handle will return -EIO if we have | 
|  | 1645 | * entered abort state during the update. | 
|  | 1646 | * | 
|  | 1647 | * Recursive transactions are not disturbed by journal abort until the | 
|  | 1648 | * final journal_stop, which will receive the -EIO error. | 
|  | 1649 | * | 
|  | 1650 | * Finally, the journal_abort call allows the caller to supply an errno | 
|  | 1651 | * which will be recorded (if possible) in the journal superblock.  This | 
|  | 1652 | * allows a client to record failure conditions in the middle of a | 
|  | 1653 | * transaction without having to complete the transaction to record the | 
|  | 1654 | * failure to disk.  ext3_error, for example, now uses this | 
|  | 1655 | * functionality. | 
|  | 1656 | * | 
|  | 1657 | * Errors which originate from within the journaling layer will NOT | 
|  | 1658 | * supply an errno; a null errno implies that absolutely no further | 
|  | 1659 | * writes are done to the journal (unless there are any already in | 
|  | 1660 | * progress). | 
| Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1661 | * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1662 | */ | 
|  | 1663 |  | 
|  | 1664 | void journal_abort(journal_t *journal, int errno) | 
|  | 1665 | { | 
|  | 1666 | __journal_abort_soft(journal, errno); | 
|  | 1667 | } | 
|  | 1668 |  | 
| Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1669 | /** | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | * int journal_errno () - returns the journal's error state. | 
|  | 1671 | * @journal: journal to examine. | 
|  | 1672 | * | 
|  | 1673 | * This is the errno numbet set with journal_abort(), the last | 
|  | 1674 | * time the journal was mounted - if the journal was stopped | 
|  | 1675 | * without calling abort this will be 0. | 
|  | 1676 | * | 
|  | 1677 | * If the journal has been aborted on this mount time -EROFS will | 
|  | 1678 | * be returned. | 
|  | 1679 | */ | 
|  | 1680 | int journal_errno(journal_t *journal) | 
|  | 1681 | { | 
|  | 1682 | int err; | 
|  | 1683 |  | 
|  | 1684 | spin_lock(&journal->j_state_lock); | 
|  | 1685 | if (journal->j_flags & JFS_ABORT) | 
|  | 1686 | err = -EROFS; | 
|  | 1687 | else | 
|  | 1688 | err = journal->j_errno; | 
|  | 1689 | spin_unlock(&journal->j_state_lock); | 
|  | 1690 | return err; | 
|  | 1691 | } | 
|  | 1692 |  | 
| Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1693 | /** | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | * int journal_clear_err () - clears the journal's error state | 
|  | 1695 | * @journal: journal to act on. | 
|  | 1696 | * | 
|  | 1697 | * An error must be cleared or Acked to take a FS out of readonly | 
|  | 1698 | * mode. | 
|  | 1699 | */ | 
|  | 1700 | int journal_clear_err(journal_t *journal) | 
|  | 1701 | { | 
|  | 1702 | int err = 0; | 
|  | 1703 |  | 
|  | 1704 | spin_lock(&journal->j_state_lock); | 
|  | 1705 | if (journal->j_flags & JFS_ABORT) | 
|  | 1706 | err = -EROFS; | 
|  | 1707 | else | 
|  | 1708 | journal->j_errno = 0; | 
|  | 1709 | spin_unlock(&journal->j_state_lock); | 
|  | 1710 | return err; | 
|  | 1711 | } | 
|  | 1712 |  | 
| Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 1713 | /** | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | * void journal_ack_err() - Ack journal err. | 
|  | 1715 | * @journal: journal to act on. | 
|  | 1716 | * | 
|  | 1717 | * An error must be cleared or Acked to take a FS out of readonly | 
|  | 1718 | * mode. | 
|  | 1719 | */ | 
|  | 1720 | void journal_ack_err(journal_t *journal) | 
|  | 1721 | { | 
|  | 1722 | spin_lock(&journal->j_state_lock); | 
|  | 1723 | if (journal->j_errno) | 
|  | 1724 | journal->j_flags |= JFS_ACK_ERR; | 
|  | 1725 | spin_unlock(&journal->j_state_lock); | 
|  | 1726 | } | 
|  | 1727 |  | 
|  | 1728 | int journal_blocks_per_page(struct inode *inode) | 
|  | 1729 | { | 
|  | 1730 | return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); | 
|  | 1731 | } | 
|  | 1732 |  | 
|  | 1733 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | * Journal_head storage management | 
|  | 1735 | */ | 
| Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 1736 | static struct kmem_cache *journal_head_cache; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | #ifdef CONFIG_JBD_DEBUG | 
|  | 1738 | static atomic_t nr_journal_heads = ATOMIC_INIT(0); | 
|  | 1739 | #endif | 
|  | 1740 |  | 
|  | 1741 | static int journal_init_journal_head_cache(void) | 
|  | 1742 | { | 
|  | 1743 | int retval; | 
|  | 1744 |  | 
| Al Viro | 1076d17 | 2008-03-29 03:07:18 +0000 | [diff] [blame] | 1745 | J_ASSERT(journal_head_cache == NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | journal_head_cache = kmem_cache_create("journal_head", | 
|  | 1747 | sizeof(struct journal_head), | 
|  | 1748 | 0,		/* offset */ | 
| Mel Gorman | e12ba74 | 2007-10-16 01:25:52 -0700 | [diff] [blame] | 1749 | SLAB_TEMPORARY,	/* flags */ | 
| Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1750 | NULL);		/* ctor */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1751 | retval = 0; | 
| Al Viro | 1076d17 | 2008-03-29 03:07:18 +0000 | [diff] [blame] | 1752 | if (!journal_head_cache) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | retval = -ENOMEM; | 
|  | 1754 | printk(KERN_EMERG "JBD: no memory for journal_head cache\n"); | 
|  | 1755 | } | 
|  | 1756 | return retval; | 
|  | 1757 | } | 
|  | 1758 |  | 
|  | 1759 | static void journal_destroy_journal_head_cache(void) | 
|  | 1760 | { | 
| Duane Griffin | 3850f7a | 2008-07-25 01:46:19 -0700 | [diff] [blame] | 1761 | if (journal_head_cache) { | 
|  | 1762 | kmem_cache_destroy(journal_head_cache); | 
|  | 1763 | journal_head_cache = NULL; | 
|  | 1764 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | } | 
|  | 1766 |  | 
|  | 1767 | /* | 
|  | 1768 | * journal_head splicing and dicing | 
|  | 1769 | */ | 
|  | 1770 | static struct journal_head *journal_alloc_journal_head(void) | 
|  | 1771 | { | 
|  | 1772 | struct journal_head *ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 |  | 
|  | 1774 | #ifdef CONFIG_JBD_DEBUG | 
|  | 1775 | atomic_inc(&nr_journal_heads); | 
|  | 1776 | #endif | 
|  | 1777 | ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS); | 
| Stephen Hemminger | c80544d | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 1778 | if (ret == NULL) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | jbd_debug(1, "out of memory for journal_head\n"); | 
| Namhyung Kim | f81e3d4 | 2010-10-04 19:12:13 +0900 | [diff] [blame] | 1780 | printk_ratelimited(KERN_NOTICE "ENOMEM in %s, retrying.\n", | 
|  | 1781 | __func__); | 
|  | 1782 |  | 
| Stephen Hemminger | c80544d | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 1783 | while (ret == NULL) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1784 | yield(); | 
|  | 1785 | ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS); | 
|  | 1786 | } | 
|  | 1787 | } | 
|  | 1788 | return ret; | 
|  | 1789 | } | 
|  | 1790 |  | 
|  | 1791 | static void journal_free_journal_head(struct journal_head *jh) | 
|  | 1792 | { | 
|  | 1793 | #ifdef CONFIG_JBD_DEBUG | 
|  | 1794 | atomic_dec(&nr_journal_heads); | 
| Randy Dunlap | c9cf552 | 2006-06-27 02:53:52 -0700 | [diff] [blame] | 1795 | memset(jh, JBD_POISON_FREE, sizeof(*jh)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | #endif | 
|  | 1797 | kmem_cache_free(journal_head_cache, jh); | 
|  | 1798 | } | 
|  | 1799 |  | 
|  | 1800 | /* | 
|  | 1801 | * A journal_head is attached to a buffer_head whenever JBD has an | 
|  | 1802 | * interest in the buffer. | 
|  | 1803 | * | 
|  | 1804 | * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit | 
|  | 1805 | * is set.  This bit is tested in core kernel code where we need to take | 
|  | 1806 | * JBD-specific actions.  Testing the zeroness of ->b_private is not reliable | 
|  | 1807 | * there. | 
|  | 1808 | * | 
|  | 1809 | * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one. | 
|  | 1810 | * | 
|  | 1811 | * When a buffer has its BH_JBD bit set it is immune from being released by | 
|  | 1812 | * core kernel code, mainly via ->b_count. | 
|  | 1813 | * | 
| Jan Kara | bb18924 | 2011-06-24 23:11:59 +0200 | [diff] [blame] | 1814 | * A journal_head is detached from its buffer_head when the journal_head's | 
|  | 1815 | * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint | 
|  | 1816 | * transaction (b_cp_transaction) hold their references to b_jcount. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | * | 
|  | 1818 | * Various places in the kernel want to attach a journal_head to a buffer_head | 
|  | 1819 | * _before_ attaching the journal_head to a transaction.  To protect the | 
|  | 1820 | * journal_head in this situation, journal_add_journal_head elevates the | 
|  | 1821 | * journal_head's b_jcount refcount by one.  The caller must call | 
|  | 1822 | * journal_put_journal_head() to undo this. | 
|  | 1823 | * | 
|  | 1824 | * So the typical usage would be: | 
|  | 1825 | * | 
|  | 1826 | *	(Attach a journal_head if needed.  Increments b_jcount) | 
|  | 1827 | *	struct journal_head *jh = journal_add_journal_head(bh); | 
|  | 1828 | *	... | 
| Jan Kara | bb18924 | 2011-06-24 23:11:59 +0200 | [diff] [blame] | 1829 | *      (Get another reference for transaction) | 
|  | 1830 | *      journal_grab_journal_head(bh); | 
|  | 1831 | *      jh->b_transaction = xxx; | 
|  | 1832 | *      (Put original reference) | 
|  | 1833 | *      journal_put_journal_head(jh); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | */ | 
|  | 1835 |  | 
|  | 1836 | /* | 
|  | 1837 | * Give a buffer_head a journal_head. | 
|  | 1838 | * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1839 | * May sleep. | 
|  | 1840 | */ | 
|  | 1841 | struct journal_head *journal_add_journal_head(struct buffer_head *bh) | 
|  | 1842 | { | 
|  | 1843 | struct journal_head *jh; | 
|  | 1844 | struct journal_head *new_jh = NULL; | 
|  | 1845 |  | 
|  | 1846 | repeat: | 
|  | 1847 | if (!buffer_jbd(bh)) { | 
|  | 1848 | new_jh = journal_alloc_journal_head(); | 
|  | 1849 | memset(new_jh, 0, sizeof(*new_jh)); | 
|  | 1850 | } | 
|  | 1851 |  | 
|  | 1852 | jbd_lock_bh_journal_head(bh); | 
|  | 1853 | if (buffer_jbd(bh)) { | 
|  | 1854 | jh = bh2jh(bh); | 
|  | 1855 | } else { | 
|  | 1856 | J_ASSERT_BH(bh, | 
|  | 1857 | (atomic_read(&bh->b_count) > 0) || | 
|  | 1858 | (bh->b_page && bh->b_page->mapping)); | 
|  | 1859 |  | 
|  | 1860 | if (!new_jh) { | 
|  | 1861 | jbd_unlock_bh_journal_head(bh); | 
|  | 1862 | goto repeat; | 
|  | 1863 | } | 
|  | 1864 |  | 
|  | 1865 | jh = new_jh; | 
|  | 1866 | new_jh = NULL;		/* We consumed it */ | 
|  | 1867 | set_buffer_jbd(bh); | 
|  | 1868 | bh->b_private = jh; | 
|  | 1869 | jh->b_bh = bh; | 
|  | 1870 | get_bh(bh); | 
|  | 1871 | BUFFER_TRACE(bh, "added journal_head"); | 
|  | 1872 | } | 
|  | 1873 | jh->b_jcount++; | 
|  | 1874 | jbd_unlock_bh_journal_head(bh); | 
|  | 1875 | if (new_jh) | 
|  | 1876 | journal_free_journal_head(new_jh); | 
|  | 1877 | return bh->b_private; | 
|  | 1878 | } | 
|  | 1879 |  | 
|  | 1880 | /* | 
|  | 1881 | * Grab a ref against this buffer_head's journal_head.  If it ended up not | 
|  | 1882 | * having a journal_head, return NULL | 
|  | 1883 | */ | 
|  | 1884 | struct journal_head *journal_grab_journal_head(struct buffer_head *bh) | 
|  | 1885 | { | 
|  | 1886 | struct journal_head *jh = NULL; | 
|  | 1887 |  | 
|  | 1888 | jbd_lock_bh_journal_head(bh); | 
|  | 1889 | if (buffer_jbd(bh)) { | 
|  | 1890 | jh = bh2jh(bh); | 
|  | 1891 | jh->b_jcount++; | 
|  | 1892 | } | 
|  | 1893 | jbd_unlock_bh_journal_head(bh); | 
|  | 1894 | return jh; | 
|  | 1895 | } | 
|  | 1896 |  | 
|  | 1897 | static void __journal_remove_journal_head(struct buffer_head *bh) | 
|  | 1898 | { | 
|  | 1899 | struct journal_head *jh = bh2jh(bh); | 
|  | 1900 |  | 
|  | 1901 | J_ASSERT_JH(jh, jh->b_jcount >= 0); | 
| Jan Kara | bb18924 | 2011-06-24 23:11:59 +0200 | [diff] [blame] | 1902 | J_ASSERT_JH(jh, jh->b_transaction == NULL); | 
|  | 1903 | J_ASSERT_JH(jh, jh->b_next_transaction == NULL); | 
|  | 1904 | J_ASSERT_JH(jh, jh->b_cp_transaction == NULL); | 
|  | 1905 | J_ASSERT_JH(jh, jh->b_jlist == BJ_None); | 
|  | 1906 | J_ASSERT_BH(bh, buffer_jbd(bh)); | 
|  | 1907 | J_ASSERT_BH(bh, jh2bh(jh) == bh); | 
|  | 1908 | BUFFER_TRACE(bh, "remove journal_head"); | 
|  | 1909 | if (jh->b_frozen_data) { | 
|  | 1910 | printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__); | 
|  | 1911 | jbd_free(jh->b_frozen_data, bh->b_size); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1912 | } | 
| Jan Kara | bb18924 | 2011-06-24 23:11:59 +0200 | [diff] [blame] | 1913 | if (jh->b_committed_data) { | 
|  | 1914 | printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__); | 
|  | 1915 | jbd_free(jh->b_committed_data, bh->b_size); | 
|  | 1916 | } | 
|  | 1917 | bh->b_private = NULL; | 
|  | 1918 | jh->b_bh = NULL;	/* debug, really */ | 
|  | 1919 | clear_buffer_jbd(bh); | 
|  | 1920 | journal_free_journal_head(jh); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | } | 
|  | 1922 |  | 
|  | 1923 | /* | 
| Jan Kara | bb18924 | 2011-06-24 23:11:59 +0200 | [diff] [blame] | 1924 | * Drop a reference on the passed journal_head.  If it fell to zero then | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1925 | * release the journal_head from the buffer_head. | 
|  | 1926 | */ | 
|  | 1927 | void journal_put_journal_head(struct journal_head *jh) | 
|  | 1928 | { | 
|  | 1929 | struct buffer_head *bh = jh2bh(jh); | 
|  | 1930 |  | 
|  | 1931 | jbd_lock_bh_journal_head(bh); | 
|  | 1932 | J_ASSERT_JH(jh, jh->b_jcount > 0); | 
|  | 1933 | --jh->b_jcount; | 
| Jan Kara | bb18924 | 2011-06-24 23:11:59 +0200 | [diff] [blame] | 1934 | if (!jh->b_jcount) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | __journal_remove_journal_head(bh); | 
| Jan Kara | bb18924 | 2011-06-24 23:11:59 +0200 | [diff] [blame] | 1936 | jbd_unlock_bh_journal_head(bh); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1937 | __brelse(bh); | 
| Jan Kara | bb18924 | 2011-06-24 23:11:59 +0200 | [diff] [blame] | 1938 | } else | 
|  | 1939 | jbd_unlock_bh_journal_head(bh); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1940 | } | 
|  | 1941 |  | 
|  | 1942 | /* | 
| Jose R. Santos | c2a9159 | 2007-10-18 23:39:22 -0700 | [diff] [blame] | 1943 | * debugfs tunables | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | */ | 
| Jose R. Santos | c2a9159 | 2007-10-18 23:39:22 -0700 | [diff] [blame] | 1945 | #ifdef CONFIG_JBD_DEBUG | 
|  | 1946 |  | 
|  | 1947 | u8 journal_enable_debug __read_mostly; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1948 | EXPORT_SYMBOL(journal_enable_debug); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 |  | 
| Jose R. Santos | c2a9159 | 2007-10-18 23:39:22 -0700 | [diff] [blame] | 1950 | static struct dentry *jbd_debugfs_dir; | 
|  | 1951 | static struct dentry *jbd_debug; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 |  | 
| Jose R. Santos | c2a9159 | 2007-10-18 23:39:22 -0700 | [diff] [blame] | 1953 | static void __init jbd_create_debugfs_entry(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1954 | { | 
| Jose R. Santos | c2a9159 | 2007-10-18 23:39:22 -0700 | [diff] [blame] | 1955 | jbd_debugfs_dir = debugfs_create_dir("jbd", NULL); | 
|  | 1956 | if (jbd_debugfs_dir) | 
| Yin Kangkai | 765f836 | 2009-12-15 14:48:25 -0800 | [diff] [blame] | 1957 | jbd_debug = debugfs_create_u8("jbd-debug", S_IRUGO | S_IWUSR, | 
| Jose R. Santos | c2a9159 | 2007-10-18 23:39:22 -0700 | [diff] [blame] | 1958 | jbd_debugfs_dir, | 
|  | 1959 | &journal_enable_debug); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | } | 
|  | 1961 |  | 
| Jose R. Santos | c2a9159 | 2007-10-18 23:39:22 -0700 | [diff] [blame] | 1962 | static void __exit jbd_remove_debugfs_entry(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | { | 
| Jose R. Santos | c2a9159 | 2007-10-18 23:39:22 -0700 | [diff] [blame] | 1964 | debugfs_remove(jbd_debug); | 
|  | 1965 | debugfs_remove(jbd_debugfs_dir); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1966 | } | 
|  | 1967 |  | 
|  | 1968 | #else | 
|  | 1969 |  | 
| Jose R. Santos | c2a9159 | 2007-10-18 23:39:22 -0700 | [diff] [blame] | 1970 | static inline void jbd_create_debugfs_entry(void) | 
|  | 1971 | { | 
|  | 1972 | } | 
|  | 1973 |  | 
|  | 1974 | static inline void jbd_remove_debugfs_entry(void) | 
|  | 1975 | { | 
|  | 1976 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 |  | 
|  | 1978 | #endif | 
|  | 1979 |  | 
| Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 1980 | struct kmem_cache *jbd_handle_cache; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 |  | 
|  | 1982 | static int __init journal_init_handle_cache(void) | 
|  | 1983 | { | 
|  | 1984 | jbd_handle_cache = kmem_cache_create("journal_handle", | 
|  | 1985 | sizeof(handle_t), | 
|  | 1986 | 0,		/* offset */ | 
| Mel Gorman | e12ba74 | 2007-10-16 01:25:52 -0700 | [diff] [blame] | 1987 | SLAB_TEMPORARY,	/* flags */ | 
| Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1988 | NULL);		/* ctor */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | if (jbd_handle_cache == NULL) { | 
|  | 1990 | printk(KERN_EMERG "JBD: failed to create handle cache\n"); | 
|  | 1991 | return -ENOMEM; | 
|  | 1992 | } | 
|  | 1993 | return 0; | 
|  | 1994 | } | 
|  | 1995 |  | 
|  | 1996 | static void journal_destroy_handle_cache(void) | 
|  | 1997 | { | 
|  | 1998 | if (jbd_handle_cache) | 
|  | 1999 | kmem_cache_destroy(jbd_handle_cache); | 
|  | 2000 | } | 
|  | 2001 |  | 
|  | 2002 | /* | 
|  | 2003 | * Module startup and shutdown | 
|  | 2004 | */ | 
|  | 2005 |  | 
|  | 2006 | static int __init journal_init_caches(void) | 
|  | 2007 | { | 
|  | 2008 | int ret; | 
|  | 2009 |  | 
|  | 2010 | ret = journal_init_revoke_caches(); | 
|  | 2011 | if (ret == 0) | 
|  | 2012 | ret = journal_init_journal_head_cache(); | 
|  | 2013 | if (ret == 0) | 
|  | 2014 | ret = journal_init_handle_cache(); | 
|  | 2015 | return ret; | 
|  | 2016 | } | 
|  | 2017 |  | 
|  | 2018 | static void journal_destroy_caches(void) | 
|  | 2019 | { | 
|  | 2020 | journal_destroy_revoke_caches(); | 
|  | 2021 | journal_destroy_journal_head_cache(); | 
|  | 2022 | journal_destroy_handle_cache(); | 
|  | 2023 | } | 
|  | 2024 |  | 
|  | 2025 | static int __init journal_init(void) | 
|  | 2026 | { | 
|  | 2027 | int ret; | 
|  | 2028 |  | 
| Alexey Dobriyan | 2aed348 | 2006-09-27 01:49:28 -0700 | [diff] [blame] | 2029 | BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024); | 
| Adrian Bunk | 022a4a7 | 2005-09-06 15:16:41 -0700 | [diff] [blame] | 2030 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2031 | ret = journal_init_caches(); | 
|  | 2032 | if (ret != 0) | 
|  | 2033 | journal_destroy_caches(); | 
| Jose R. Santos | c2a9159 | 2007-10-18 23:39:22 -0700 | [diff] [blame] | 2034 | jbd_create_debugfs_entry(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2035 | return ret; | 
|  | 2036 | } | 
|  | 2037 |  | 
|  | 2038 | static void __exit journal_exit(void) | 
|  | 2039 | { | 
|  | 2040 | #ifdef CONFIG_JBD_DEBUG | 
|  | 2041 | int n = atomic_read(&nr_journal_heads); | 
|  | 2042 | if (n) | 
|  | 2043 | printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n); | 
|  | 2044 | #endif | 
| Jose R. Santos | c2a9159 | 2007-10-18 23:39:22 -0700 | [diff] [blame] | 2045 | jbd_remove_debugfs_entry(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2046 | journal_destroy_caches(); | 
|  | 2047 | } | 
|  | 2048 |  | 
|  | 2049 | MODULE_LICENSE("GPL"); | 
|  | 2050 | module_init(journal_init); | 
|  | 2051 | module_exit(journal_exit); | 
|  | 2052 |  |