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