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