Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1 | /* |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 2 | * linux/fs/jbd2/journal.c |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -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> |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 28 | #include <linux/jbd2.h> |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 29 | #include <linux/errno.h> |
| 30 | #include <linux/slab.h> |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -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> |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 34 | #include <linux/pagemap.h> |
| 35 | #include <linux/kthread.h> |
| 36 | #include <linux/poison.h> |
| 37 | #include <linux/proc_fs.h> |
Jose R. Santos | 0f49d5d | 2007-07-18 08:50:18 -0400 | [diff] [blame] | 38 | #include <linux/debugfs.h> |
Johann Lombardi | 8e85fb3 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 39 | #include <linux/seq_file.h> |
Simon Holm Thøgersen | c225aa5 | 2009-01-11 22:34:01 -0500 | [diff] [blame] | 40 | #include <linux/math64.h> |
Theodore Ts'o | 879c5e6 | 2009-06-17 11:47:48 -0400 | [diff] [blame] | 41 | #include <linux/hash.h> |
Theodore Ts'o | d2eecb0 | 2009-12-07 10:36:20 -0500 | [diff] [blame] | 42 | #include <linux/log2.h> |
| 43 | #include <linux/vmalloc.h> |
Theodore Ts'o | 47def82 | 2010-07-27 11:56:05 -0400 | [diff] [blame] | 44 | #include <linux/backing-dev.h> |
Brian King | 39e3ac2 | 2010-10-27 21:25:12 -0400 | [diff] [blame] | 45 | #include <linux/bitops.h> |
Theodore Ts'o | 670be5a | 2010-12-17 10:44:16 -0500 | [diff] [blame] | 46 | #include <linux/ratelimit.h> |
Theodore Ts'o | 879c5e6 | 2009-06-17 11:47:48 -0400 | [diff] [blame] | 47 | |
| 48 | #define CREATE_TRACE_POINTS |
| 49 | #include <trace/events/jbd2.h> |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 50 | |
| 51 | #include <asm/uaccess.h> |
| 52 | #include <asm/page.h> |
Brian King | 39e3ac2 | 2010-10-27 21:25:12 -0400 | [diff] [blame] | 53 | #include <asm/system.h> |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 54 | |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 55 | EXPORT_SYMBOL(jbd2_journal_extend); |
| 56 | EXPORT_SYMBOL(jbd2_journal_stop); |
| 57 | EXPORT_SYMBOL(jbd2_journal_lock_updates); |
| 58 | EXPORT_SYMBOL(jbd2_journal_unlock_updates); |
| 59 | EXPORT_SYMBOL(jbd2_journal_get_write_access); |
| 60 | EXPORT_SYMBOL(jbd2_journal_get_create_access); |
| 61 | EXPORT_SYMBOL(jbd2_journal_get_undo_access); |
Joel Becker | e06c822 | 2008-09-11 15:35:47 -0700 | [diff] [blame] | 62 | EXPORT_SYMBOL(jbd2_journal_set_triggers); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 63 | EXPORT_SYMBOL(jbd2_journal_dirty_metadata); |
| 64 | EXPORT_SYMBOL(jbd2_journal_release_buffer); |
| 65 | EXPORT_SYMBOL(jbd2_journal_forget); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 66 | #if 0 |
| 67 | EXPORT_SYMBOL(journal_sync_buffer); |
| 68 | #endif |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 69 | EXPORT_SYMBOL(jbd2_journal_flush); |
| 70 | EXPORT_SYMBOL(jbd2_journal_revoke); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 71 | |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 72 | EXPORT_SYMBOL(jbd2_journal_init_dev); |
| 73 | EXPORT_SYMBOL(jbd2_journal_init_inode); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 74 | EXPORT_SYMBOL(jbd2_journal_check_used_features); |
| 75 | EXPORT_SYMBOL(jbd2_journal_check_available_features); |
| 76 | EXPORT_SYMBOL(jbd2_journal_set_features); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 77 | EXPORT_SYMBOL(jbd2_journal_load); |
| 78 | EXPORT_SYMBOL(jbd2_journal_destroy); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 79 | EXPORT_SYMBOL(jbd2_journal_abort); |
| 80 | EXPORT_SYMBOL(jbd2_journal_errno); |
| 81 | EXPORT_SYMBOL(jbd2_journal_ack_err); |
| 82 | EXPORT_SYMBOL(jbd2_journal_clear_err); |
| 83 | EXPORT_SYMBOL(jbd2_log_wait_commit); |
Theodore Ts'o | 3b799d1 | 2009-12-09 20:42:53 -0500 | [diff] [blame] | 84 | EXPORT_SYMBOL(jbd2_log_start_commit); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 85 | EXPORT_SYMBOL(jbd2_journal_start_commit); |
| 86 | EXPORT_SYMBOL(jbd2_journal_force_commit_nested); |
| 87 | EXPORT_SYMBOL(jbd2_journal_wipe); |
| 88 | EXPORT_SYMBOL(jbd2_journal_blocks_per_page); |
| 89 | EXPORT_SYMBOL(jbd2_journal_invalidatepage); |
| 90 | EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers); |
| 91 | EXPORT_SYMBOL(jbd2_journal_force_commit); |
Jan Kara | c851ed5 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 92 | EXPORT_SYMBOL(jbd2_journal_file_inode); |
| 93 | EXPORT_SYMBOL(jbd2_journal_init_jbd_inode); |
| 94 | EXPORT_SYMBOL(jbd2_journal_release_jbd_inode); |
| 95 | EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate); |
Theodore Ts'o | 8aefcd5 | 2011-01-10 12:29:43 -0500 | [diff] [blame] | 96 | EXPORT_SYMBOL(jbd2_inode_cache); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 97 | |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 98 | static void __journal_abort_soft (journal_t *journal, int errno); |
Theodore Ts'o | d2eecb0 | 2009-12-07 10:36:20 -0500 | [diff] [blame] | 99 | static int jbd2_journal_create_slab(size_t slab_size); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 100 | |
| 101 | /* |
| 102 | * Helper function used to manage commit timeouts |
| 103 | */ |
| 104 | |
| 105 | static void commit_timeout(unsigned long __data) |
| 106 | { |
| 107 | struct task_struct * p = (struct task_struct *) __data; |
| 108 | |
| 109 | wake_up_process(p); |
| 110 | } |
| 111 | |
| 112 | /* |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 113 | * kjournald2: The main thread function used to manage a logging device |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 114 | * journal. |
| 115 | * |
| 116 | * This kernel thread is responsible for two things: |
| 117 | * |
| 118 | * 1) COMMIT: Every so often we need to commit the current state of the |
| 119 | * filesystem to disk. The journal thread is responsible for writing |
| 120 | * all of the metadata buffers to disk. |
| 121 | * |
| 122 | * 2) CHECKPOINT: We cannot reuse a used section of the log file until all |
| 123 | * of the data in that part of the log has been rewritten elsewhere on |
| 124 | * the disk. Flushing these old buffers to reclaim space in the log is |
| 125 | * known as checkpointing, and this thread is responsible for that job. |
| 126 | */ |
| 127 | |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 128 | static int kjournald2(void *arg) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 129 | { |
| 130 | journal_t *journal = arg; |
| 131 | transaction_t *transaction; |
| 132 | |
| 133 | /* |
| 134 | * Set up an interval timer which can be used to trigger a commit wakeup |
| 135 | * after the commit interval expires |
| 136 | */ |
| 137 | setup_timer(&journal->j_commit_timer, commit_timeout, |
| 138 | (unsigned long)current); |
| 139 | |
| 140 | /* Record that the journal thread is running */ |
| 141 | journal->j_task = current; |
| 142 | wake_up(&journal->j_wait_done_commit); |
| 143 | |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 144 | /* |
| 145 | * And now, wait forever for commit wakeup events. |
| 146 | */ |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 147 | write_lock(&journal->j_state_lock); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 148 | |
| 149 | loop: |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 150 | if (journal->j_flags & JBD2_UNMOUNT) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 151 | goto end_loop; |
| 152 | |
| 153 | jbd_debug(1, "commit_sequence=%d, commit_request=%d\n", |
| 154 | journal->j_commit_sequence, journal->j_commit_request); |
| 155 | |
| 156 | if (journal->j_commit_sequence != journal->j_commit_request) { |
| 157 | jbd_debug(1, "OK, requests differ\n"); |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 158 | write_unlock(&journal->j_state_lock); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 159 | del_timer_sync(&journal->j_commit_timer); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 160 | jbd2_journal_commit_transaction(journal); |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 161 | write_lock(&journal->j_state_lock); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 162 | goto loop; |
| 163 | } |
| 164 | |
| 165 | wake_up(&journal->j_wait_done_commit); |
| 166 | if (freezing(current)) { |
| 167 | /* |
| 168 | * The simpler the better. Flushing journal isn't a |
| 169 | * good idea, because that depends on threads that may |
| 170 | * be already stopped. |
| 171 | */ |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 172 | jbd_debug(1, "Now suspending kjournald2\n"); |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 173 | write_unlock(&journal->j_state_lock); |
Tejun Heo | a0acae0 | 2011-11-21 12:32:22 -0800 | [diff] [blame] | 174 | try_to_freeze(); |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 175 | write_lock(&journal->j_state_lock); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 176 | } else { |
| 177 | /* |
| 178 | * We assume on resume that commits are already there, |
| 179 | * so we don't sleep |
| 180 | */ |
| 181 | DEFINE_WAIT(wait); |
| 182 | int should_sleep = 1; |
| 183 | |
| 184 | prepare_to_wait(&journal->j_wait_commit, &wait, |
| 185 | TASK_INTERRUPTIBLE); |
| 186 | if (journal->j_commit_sequence != journal->j_commit_request) |
| 187 | should_sleep = 0; |
| 188 | transaction = journal->j_running_transaction; |
| 189 | if (transaction && time_after_eq(jiffies, |
| 190 | transaction->t_expires)) |
| 191 | should_sleep = 0; |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 192 | if (journal->j_flags & JBD2_UNMOUNT) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 193 | should_sleep = 0; |
| 194 | if (should_sleep) { |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 195 | write_unlock(&journal->j_state_lock); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 196 | schedule(); |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 197 | write_lock(&journal->j_state_lock); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 198 | } |
| 199 | finish_wait(&journal->j_wait_commit, &wait); |
| 200 | } |
| 201 | |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 202 | jbd_debug(1, "kjournald2 wakes\n"); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 203 | |
| 204 | /* |
| 205 | * Were we woken up by a commit wakeup event? |
| 206 | */ |
| 207 | transaction = journal->j_running_transaction; |
| 208 | if (transaction && time_after_eq(jiffies, transaction->t_expires)) { |
| 209 | journal->j_commit_request = transaction->t_tid; |
| 210 | jbd_debug(1, "woke because of timeout\n"); |
| 211 | } |
| 212 | goto loop; |
| 213 | |
| 214 | end_loop: |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 215 | write_unlock(&journal->j_state_lock); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 216 | del_timer_sync(&journal->j_commit_timer); |
| 217 | journal->j_task = NULL; |
| 218 | wake_up(&journal->j_wait_done_commit); |
| 219 | jbd_debug(1, "Journal thread exiting.\n"); |
| 220 | return 0; |
| 221 | } |
| 222 | |
Pavel Emelianov | 97f0678 | 2007-05-08 00:30:42 -0700 | [diff] [blame] | 223 | static int jbd2_journal_start_thread(journal_t *journal) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 224 | { |
Pavel Emelianov | 97f0678 | 2007-05-08 00:30:42 -0700 | [diff] [blame] | 225 | struct task_struct *t; |
| 226 | |
Theodore Ts'o | 90576c0 | 2009-09-29 15:51:30 -0400 | [diff] [blame] | 227 | t = kthread_run(kjournald2, journal, "jbd2/%s", |
| 228 | journal->j_devname); |
Pavel Emelianov | 97f0678 | 2007-05-08 00:30:42 -0700 | [diff] [blame] | 229 | if (IS_ERR(t)) |
| 230 | return PTR_ERR(t); |
| 231 | |
Al Viro | 1076d17 | 2008-03-29 03:07:18 +0000 | [diff] [blame] | 232 | wait_event(journal->j_wait_done_commit, journal->j_task != NULL); |
Pavel Emelianov | 97f0678 | 2007-05-08 00:30:42 -0700 | [diff] [blame] | 233 | return 0; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | static void journal_kill_thread(journal_t *journal) |
| 237 | { |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 238 | write_lock(&journal->j_state_lock); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 239 | journal->j_flags |= JBD2_UNMOUNT; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 240 | |
| 241 | while (journal->j_task) { |
| 242 | wake_up(&journal->j_wait_commit); |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 243 | write_unlock(&journal->j_state_lock); |
Al Viro | 1076d17 | 2008-03-29 03:07:18 +0000 | [diff] [blame] | 244 | wait_event(journal->j_wait_done_commit, journal->j_task == NULL); |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 245 | write_lock(&journal->j_state_lock); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 246 | } |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 247 | write_unlock(&journal->j_state_lock); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | /* |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 251 | * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal. |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 252 | * |
| 253 | * Writes a metadata buffer to a given disk block. The actual IO is not |
| 254 | * performed but a new buffer_head is constructed which labels the data |
| 255 | * to be written with the correct destination disk block. |
| 256 | * |
| 257 | * Any magic-number escaping which needs to be done will cause a |
| 258 | * copy-out here. If the buffer happens to start with the |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 259 | * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 260 | * magic number is only written to the log for descripter blocks. In |
| 261 | * this case, we copy the data and replace the first word with 0, and we |
| 262 | * return a result code which indicates that this buffer needs to be |
| 263 | * marked as an escaped buffer in the corresponding log descriptor |
| 264 | * block. The missing word can then be restored when the block is read |
| 265 | * during recovery. |
| 266 | * |
| 267 | * If the source buffer has already been modified by a new transaction |
| 268 | * since we took the last commit snapshot, we use the frozen copy of |
| 269 | * that data for IO. If we end up using the existing buffer_head's data |
| 270 | * for the write, then we *have* to lock the buffer to prevent anyone |
| 271 | * else from using and possibly modifying it while the IO is in |
| 272 | * progress. |
| 273 | * |
| 274 | * The function returns a pointer to the buffer_heads to be used for IO. |
| 275 | * |
| 276 | * We assume that the journal has already been locked in this function. |
| 277 | * |
| 278 | * Return value: |
| 279 | * <0: Error |
| 280 | * >=0: Finished OK |
| 281 | * |
| 282 | * On success: |
| 283 | * Bit 0 set == escape performed on the data |
| 284 | * Bit 1 set == buffer copy-out performed (kfree the data after IO) |
| 285 | */ |
| 286 | |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 287 | int jbd2_journal_write_metadata_buffer(transaction_t *transaction, |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 288 | struct journal_head *jh_in, |
| 289 | struct journal_head **jh_out, |
Mingming Cao | 18eba7a | 2006-10-11 01:21:13 -0700 | [diff] [blame] | 290 | unsigned long long blocknr) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 291 | { |
| 292 | int need_copy_out = 0; |
| 293 | int done_copy_out = 0; |
| 294 | int do_escape = 0; |
| 295 | char *mapped_data; |
| 296 | struct buffer_head *new_bh; |
| 297 | struct journal_head *new_jh; |
| 298 | struct page *new_page; |
| 299 | unsigned int new_offset; |
| 300 | struct buffer_head *bh_in = jh2bh(jh_in); |
dingdinghua | 96577c4 | 2009-07-13 17:55:35 -0400 | [diff] [blame] | 301 | journal_t *journal = transaction->t_journal; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 302 | |
| 303 | /* |
| 304 | * The buffer really shouldn't be locked: only the current committing |
| 305 | * transaction is allowed to write it, so nobody else is allowed |
| 306 | * to do any IO. |
| 307 | * |
| 308 | * akpm: except if we're journalling data, and write() output is |
| 309 | * also part of a shared mapping, and another thread has |
| 310 | * decided to launch a writepage() against this buffer. |
| 311 | */ |
| 312 | J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in)); |
| 313 | |
Theodore Ts'o | 47def82 | 2010-07-27 11:56:05 -0400 | [diff] [blame] | 314 | retry_alloc: |
| 315 | new_bh = alloc_buffer_head(GFP_NOFS); |
| 316 | if (!new_bh) { |
| 317 | /* |
| 318 | * Failure is not an option, but __GFP_NOFAIL is going |
| 319 | * away; so we retry ourselves here. |
| 320 | */ |
| 321 | congestion_wait(BLK_RW_ASYNC, HZ/50); |
| 322 | goto retry_alloc; |
| 323 | } |
| 324 | |
dingdinghua | 96577c4 | 2009-07-13 17:55:35 -0400 | [diff] [blame] | 325 | /* keep subsequent assertions sane */ |
| 326 | new_bh->b_state = 0; |
| 327 | init_buffer(new_bh, NULL, NULL); |
| 328 | atomic_set(&new_bh->b_count, 1); |
| 329 | new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */ |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 330 | |
| 331 | /* |
| 332 | * If a new transaction has already done a buffer copy-out, then |
| 333 | * we use that version of the data for the commit. |
| 334 | */ |
| 335 | jbd_lock_bh_state(bh_in); |
| 336 | repeat: |
| 337 | if (jh_in->b_frozen_data) { |
| 338 | done_copy_out = 1; |
| 339 | new_page = virt_to_page(jh_in->b_frozen_data); |
| 340 | new_offset = offset_in_page(jh_in->b_frozen_data); |
| 341 | } else { |
| 342 | new_page = jh2bh(jh_in)->b_page; |
| 343 | new_offset = offset_in_page(jh2bh(jh_in)->b_data); |
| 344 | } |
| 345 | |
| 346 | mapped_data = kmap_atomic(new_page, KM_USER0); |
| 347 | /* |
Jan Kara | 13ceef0 | 2010-07-14 07:56:33 +0200 | [diff] [blame] | 348 | * Fire data frozen trigger if data already wasn't frozen. Do this |
| 349 | * before checking for escaping, as the trigger may modify the magic |
| 350 | * offset. If a copy-out happens afterwards, it will have the correct |
| 351 | * data in the buffer. |
Joel Becker | e06c822 | 2008-09-11 15:35:47 -0700 | [diff] [blame] | 352 | */ |
Jan Kara | 13ceef0 | 2010-07-14 07:56:33 +0200 | [diff] [blame] | 353 | if (!done_copy_out) |
| 354 | jbd2_buffer_frozen_trigger(jh_in, mapped_data + new_offset, |
| 355 | jh_in->b_triggers); |
Joel Becker | e06c822 | 2008-09-11 15:35:47 -0700 | [diff] [blame] | 356 | |
| 357 | /* |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 358 | * Check for escaping |
| 359 | */ |
| 360 | if (*((__be32 *)(mapped_data + new_offset)) == |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 361 | cpu_to_be32(JBD2_MAGIC_NUMBER)) { |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 362 | need_copy_out = 1; |
| 363 | do_escape = 1; |
| 364 | } |
| 365 | kunmap_atomic(mapped_data, KM_USER0); |
| 366 | |
| 367 | /* |
| 368 | * Do we need to do a data copy? |
| 369 | */ |
| 370 | if (need_copy_out && !done_copy_out) { |
| 371 | char *tmp; |
| 372 | |
| 373 | jbd_unlock_bh_state(bh_in); |
Mingming Cao | af1e76d | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 374 | tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS); |
Theodore Ts'o | e6ec116 | 2009-12-01 09:04:42 -0500 | [diff] [blame] | 375 | if (!tmp) { |
| 376 | jbd2_journal_put_journal_head(new_jh); |
| 377 | return -ENOMEM; |
| 378 | } |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 379 | jbd_lock_bh_state(bh_in); |
| 380 | if (jh_in->b_frozen_data) { |
Mingming Cao | af1e76d | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 381 | jbd2_free(tmp, bh_in->b_size); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 382 | goto repeat; |
| 383 | } |
| 384 | |
| 385 | jh_in->b_frozen_data = tmp; |
| 386 | mapped_data = kmap_atomic(new_page, KM_USER0); |
| 387 | memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size); |
| 388 | kunmap_atomic(mapped_data, KM_USER0); |
| 389 | |
| 390 | new_page = virt_to_page(tmp); |
| 391 | new_offset = offset_in_page(tmp); |
| 392 | done_copy_out = 1; |
Joel Becker | e06c822 | 2008-09-11 15:35:47 -0700 | [diff] [blame] | 393 | |
| 394 | /* |
| 395 | * This isn't strictly necessary, as we're using frozen |
| 396 | * data for the escaping, but it keeps consistency with |
| 397 | * b_frozen_data usage. |
| 398 | */ |
| 399 | jh_in->b_frozen_triggers = jh_in->b_triggers; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | /* |
| 403 | * Did we need to do an escaping? Now we've done all the |
| 404 | * copying, we can finally do so. |
| 405 | */ |
| 406 | if (do_escape) { |
| 407 | mapped_data = kmap_atomic(new_page, KM_USER0); |
| 408 | *((unsigned int *)(mapped_data + new_offset)) = 0; |
| 409 | kunmap_atomic(mapped_data, KM_USER0); |
| 410 | } |
| 411 | |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 412 | set_bh_page(new_bh, new_page, new_offset); |
| 413 | new_jh->b_transaction = NULL; |
| 414 | new_bh->b_size = jh2bh(jh_in)->b_size; |
| 415 | new_bh->b_bdev = transaction->t_journal->j_dev; |
| 416 | new_bh->b_blocknr = blocknr; |
| 417 | set_buffer_mapped(new_bh); |
| 418 | set_buffer_dirty(new_bh); |
| 419 | |
| 420 | *jh_out = new_jh; |
| 421 | |
| 422 | /* |
| 423 | * The to-be-written buffer needs to get moved to the io queue, |
| 424 | * and the original buffer whose contents we are shadowing or |
| 425 | * copying is moved to the transaction's shadow queue. |
| 426 | */ |
| 427 | JBUFFER_TRACE(jh_in, "file as BJ_Shadow"); |
dingdinghua | 96577c4 | 2009-07-13 17:55:35 -0400 | [diff] [blame] | 428 | spin_lock(&journal->j_list_lock); |
| 429 | __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow); |
| 430 | spin_unlock(&journal->j_list_lock); |
| 431 | jbd_unlock_bh_state(bh_in); |
| 432 | |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 433 | JBUFFER_TRACE(new_jh, "file as BJ_IO"); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 434 | jbd2_journal_file_buffer(new_jh, transaction, BJ_IO); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 435 | |
| 436 | return do_escape | (done_copy_out << 1); |
| 437 | } |
| 438 | |
| 439 | /* |
| 440 | * Allocation code for the journal file. Manage the space left in the |
| 441 | * journal, so that we can begin checkpointing when appropriate. |
| 442 | */ |
| 443 | |
| 444 | /* |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 445 | * __jbd2_log_space_left: Return the number of free blocks left in the journal. |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 446 | * |
| 447 | * Called with the journal already locked. |
| 448 | * |
| 449 | * Called under j_state_lock |
| 450 | */ |
| 451 | |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 452 | int __jbd2_log_space_left(journal_t *journal) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 453 | { |
| 454 | int left = journal->j_free; |
| 455 | |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 456 | /* assert_spin_locked(&journal->j_state_lock); */ |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 457 | |
| 458 | /* |
| 459 | * Be pessimistic here about the number of those free blocks which |
| 460 | * might be required for log descriptor control blocks. |
| 461 | */ |
| 462 | |
| 463 | #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */ |
| 464 | |
| 465 | left -= MIN_LOG_RESERVED_BLOCKS; |
| 466 | |
| 467 | if (left <= 0) |
| 468 | return 0; |
| 469 | left -= (left >> 3); |
| 470 | return left; |
| 471 | } |
| 472 | |
| 473 | /* |
Theodore Ts'o | e447183 | 2011-02-12 08:18:24 -0500 | [diff] [blame] | 474 | * Called with j_state_lock locked for writing. |
| 475 | * Returns true if a transaction commit was started. |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 476 | */ |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 477 | int __jbd2_log_start_commit(journal_t *journal, tid_t target) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 478 | { |
| 479 | /* |
Theodore Ts'o | deeeaf1 | 2011-05-01 18:16:26 -0400 | [diff] [blame] | 480 | * The only transaction we can possibly wait upon is the |
| 481 | * currently running transaction (if it exists). Otherwise, |
| 482 | * the target tid must be an old one. |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 483 | */ |
Theodore Ts'o | deeeaf1 | 2011-05-01 18:16:26 -0400 | [diff] [blame] | 484 | if (journal->j_running_transaction && |
| 485 | journal->j_running_transaction->t_tid == target) { |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 486 | /* |
Andrea Gelmini | bcf3d0b | 2010-10-16 15:19:14 +0200 | [diff] [blame] | 487 | * We want a new commit: OK, mark the request and wakeup the |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 488 | * commit thread. We do _not_ do the commit ourselves. |
| 489 | */ |
| 490 | |
| 491 | journal->j_commit_request = target; |
Eryu Guan | f2a4452 | 2011-11-01 19:09:18 -0400 | [diff] [blame] | 492 | jbd_debug(1, "JBD2: requesting commit %d/%d\n", |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 493 | journal->j_commit_request, |
| 494 | journal->j_commit_sequence); |
| 495 | wake_up(&journal->j_wait_commit); |
| 496 | return 1; |
Theodore Ts'o | deeeaf1 | 2011-05-01 18:16:26 -0400 | [diff] [blame] | 497 | } else if (!tid_geq(journal->j_commit_request, target)) |
| 498 | /* This should never happen, but if it does, preserve |
| 499 | the evidence before kjournald goes into a loop and |
| 500 | increments j_commit_sequence beyond all recognition. */ |
Eryu Guan | f2a4452 | 2011-11-01 19:09:18 -0400 | [diff] [blame] | 501 | WARN_ONCE(1, "JBD2: bad log_start_commit: %u %u %u %u\n", |
Theodore Ts'o | 1be2add | 2011-05-08 19:37:54 -0400 | [diff] [blame] | 502 | journal->j_commit_request, |
| 503 | journal->j_commit_sequence, |
| 504 | target, journal->j_running_transaction ? |
| 505 | journal->j_running_transaction->t_tid : 0); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 506 | return 0; |
| 507 | } |
| 508 | |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 509 | int jbd2_log_start_commit(journal_t *journal, tid_t tid) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 510 | { |
| 511 | int ret; |
| 512 | |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 513 | write_lock(&journal->j_state_lock); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 514 | ret = __jbd2_log_start_commit(journal, tid); |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 515 | write_unlock(&journal->j_state_lock); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 516 | return ret; |
| 517 | } |
| 518 | |
| 519 | /* |
| 520 | * Force and wait upon a commit if the calling process is not within |
| 521 | * transaction. This is used for forcing out undo-protected data which contains |
| 522 | * bitmaps, when the fs is running out of space. |
| 523 | * |
| 524 | * We can only force the running transaction if we don't have an active handle; |
| 525 | * otherwise, we will deadlock. |
| 526 | * |
| 527 | * Returns true if a transaction was started. |
| 528 | */ |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 529 | int jbd2_journal_force_commit_nested(journal_t *journal) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 530 | { |
| 531 | transaction_t *transaction = NULL; |
| 532 | tid_t tid; |
Theodore Ts'o | e447183 | 2011-02-12 08:18:24 -0500 | [diff] [blame] | 533 | int need_to_start = 0; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 534 | |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 535 | read_lock(&journal->j_state_lock); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 536 | if (journal->j_running_transaction && !current->journal_info) { |
| 537 | transaction = journal->j_running_transaction; |
Theodore Ts'o | e447183 | 2011-02-12 08:18:24 -0500 | [diff] [blame] | 538 | if (!tid_geq(journal->j_commit_request, transaction->t_tid)) |
| 539 | need_to_start = 1; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 540 | } else if (journal->j_committing_transaction) |
| 541 | transaction = journal->j_committing_transaction; |
| 542 | |
| 543 | if (!transaction) { |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 544 | read_unlock(&journal->j_state_lock); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 545 | return 0; /* Nothing to retry */ |
| 546 | } |
| 547 | |
| 548 | tid = transaction->t_tid; |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 549 | read_unlock(&journal->j_state_lock); |
Theodore Ts'o | e447183 | 2011-02-12 08:18:24 -0500 | [diff] [blame] | 550 | if (need_to_start) |
| 551 | jbd2_log_start_commit(journal, tid); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 552 | jbd2_log_wait_commit(journal, tid); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 553 | return 1; |
| 554 | } |
| 555 | |
| 556 | /* |
| 557 | * Start a commit of the current running transaction (if any). Returns true |
Jan Kara | c88ccea | 2009-02-10 11:27:46 -0500 | [diff] [blame] | 558 | * if a transaction is going to be committed (or is currently already |
| 559 | * committing), and fills its tid in at *ptid |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 560 | */ |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 561 | int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 562 | { |
| 563 | int ret = 0; |
| 564 | |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 565 | write_lock(&journal->j_state_lock); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 566 | if (journal->j_running_transaction) { |
| 567 | tid_t tid = journal->j_running_transaction->t_tid; |
| 568 | |
Jan Kara | c88ccea | 2009-02-10 11:27:46 -0500 | [diff] [blame] | 569 | __jbd2_log_start_commit(journal, tid); |
| 570 | /* There's a running transaction and we've just made sure |
| 571 | * it's commit has been scheduled. */ |
| 572 | if (ptid) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 573 | *ptid = tid; |
Jan Kara | c88ccea | 2009-02-10 11:27:46 -0500 | [diff] [blame] | 574 | ret = 1; |
| 575 | } else if (journal->j_committing_transaction) { |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 576 | /* |
| 577 | * If ext3_write_super() recently started a commit, then we |
| 578 | * have to wait for completion of that transaction |
| 579 | */ |
Jan Kara | c88ccea | 2009-02-10 11:27:46 -0500 | [diff] [blame] | 580 | if (ptid) |
| 581 | *ptid = journal->j_committing_transaction->t_tid; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 582 | ret = 1; |
| 583 | } |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 584 | write_unlock(&journal->j_state_lock); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 585 | return ret; |
| 586 | } |
| 587 | |
| 588 | /* |
Jan Kara | bbd2be3 | 2011-05-24 11:59:18 -0400 | [diff] [blame] | 589 | * Return 1 if a given transaction has not yet sent barrier request |
| 590 | * connected with a transaction commit. If 0 is returned, transaction |
| 591 | * may or may not have sent the barrier. Used to avoid sending barrier |
| 592 | * twice in common cases. |
| 593 | */ |
| 594 | int jbd2_trans_will_send_data_barrier(journal_t *journal, tid_t tid) |
| 595 | { |
| 596 | int ret = 0; |
| 597 | transaction_t *commit_trans; |
| 598 | |
| 599 | if (!(journal->j_flags & JBD2_BARRIER)) |
| 600 | return 0; |
| 601 | read_lock(&journal->j_state_lock); |
| 602 | /* Transaction already committed? */ |
| 603 | if (tid_geq(journal->j_commit_sequence, tid)) |
| 604 | goto out; |
| 605 | commit_trans = journal->j_committing_transaction; |
| 606 | if (!commit_trans || commit_trans->t_tid != tid) { |
| 607 | ret = 1; |
| 608 | goto out; |
| 609 | } |
| 610 | /* |
| 611 | * Transaction is being committed and we already proceeded to |
| 612 | * submitting a flush to fs partition? |
| 613 | */ |
| 614 | if (journal->j_fs_dev != journal->j_dev) { |
| 615 | if (!commit_trans->t_need_data_flush || |
| 616 | commit_trans->t_state >= T_COMMIT_DFLUSH) |
| 617 | goto out; |
| 618 | } else { |
| 619 | if (commit_trans->t_state >= T_COMMIT_JFLUSH) |
| 620 | goto out; |
| 621 | } |
| 622 | ret = 1; |
| 623 | out: |
| 624 | read_unlock(&journal->j_state_lock); |
| 625 | return ret; |
| 626 | } |
| 627 | EXPORT_SYMBOL(jbd2_trans_will_send_data_barrier); |
| 628 | |
| 629 | /* |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 630 | * Wait for a specified commit to complete. |
| 631 | * The caller may not hold the journal lock. |
| 632 | */ |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 633 | int jbd2_log_wait_commit(journal_t *journal, tid_t tid) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 634 | { |
| 635 | int err = 0; |
| 636 | |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 637 | read_lock(&journal->j_state_lock); |
Jose R. Santos | e23291b | 2007-07-18 08:57:06 -0400 | [diff] [blame] | 638 | #ifdef CONFIG_JBD2_DEBUG |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 639 | if (!tid_geq(journal->j_commit_request, tid)) { |
| 640 | printk(KERN_EMERG |
| 641 | "%s: error: j_commit_request=%d, tid=%d\n", |
Harvey Harrison | 329d291 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 642 | __func__, journal->j_commit_request, tid); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 643 | } |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 644 | #endif |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 645 | while (tid_gt(tid, journal->j_commit_sequence)) { |
Eryu Guan | f2a4452 | 2011-11-01 19:09:18 -0400 | [diff] [blame] | 646 | jbd_debug(1, "JBD2: want %d, j_commit_sequence=%d\n", |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 647 | tid, journal->j_commit_sequence); |
| 648 | wake_up(&journal->j_wait_commit); |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 649 | read_unlock(&journal->j_state_lock); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 650 | wait_event(journal->j_wait_done_commit, |
| 651 | !tid_gt(tid, journal->j_commit_sequence)); |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 652 | read_lock(&journal->j_state_lock); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 653 | } |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 654 | read_unlock(&journal->j_state_lock); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 655 | |
| 656 | if (unlikely(is_journal_aborted(journal))) { |
| 657 | printk(KERN_EMERG "journal commit I/O error\n"); |
| 658 | err = -EIO; |
| 659 | } |
| 660 | return err; |
| 661 | } |
| 662 | |
| 663 | /* |
| 664 | * Log buffer allocation routines: |
| 665 | */ |
| 666 | |
Mingming Cao | 18eba7a | 2006-10-11 01:21:13 -0700 | [diff] [blame] | 667 | int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 668 | { |
| 669 | unsigned long blocknr; |
| 670 | |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 671 | write_lock(&journal->j_state_lock); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 672 | J_ASSERT(journal->j_free > 1); |
| 673 | |
| 674 | blocknr = journal->j_head; |
| 675 | journal->j_head++; |
| 676 | journal->j_free--; |
| 677 | if (journal->j_head == journal->j_last) |
| 678 | journal->j_head = journal->j_first; |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 679 | write_unlock(&journal->j_state_lock); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 680 | return jbd2_journal_bmap(journal, blocknr, retp); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | /* |
| 684 | * Conversion of logical to physical block numbers for the journal |
| 685 | * |
| 686 | * On external journals the journal blocks are identity-mapped, so |
| 687 | * this is a no-op. If needed, we can use j_blk_offset - everything is |
| 688 | * ready. |
| 689 | */ |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 690 | int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr, |
Mingming Cao | 18eba7a | 2006-10-11 01:21:13 -0700 | [diff] [blame] | 691 | unsigned long long *retp) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 692 | { |
| 693 | int err = 0; |
Mingming Cao | 18eba7a | 2006-10-11 01:21:13 -0700 | [diff] [blame] | 694 | unsigned long long ret; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 695 | |
| 696 | if (journal->j_inode) { |
| 697 | ret = bmap(journal->j_inode, blocknr); |
| 698 | if (ret) |
| 699 | *retp = ret; |
| 700 | else { |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 701 | printk(KERN_ALERT "%s: journal block not found " |
| 702 | "at offset %lu on %s\n", |
Theodore Ts'o | 0549676 | 2008-09-16 14:36:17 -0400 | [diff] [blame] | 703 | __func__, blocknr, journal->j_devname); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 704 | err = -EIO; |
| 705 | __journal_abort_soft(journal, err); |
| 706 | } |
| 707 | } else { |
| 708 | *retp = blocknr; /* +journal->j_blk_offset */ |
| 709 | } |
| 710 | return err; |
| 711 | } |
| 712 | |
| 713 | /* |
| 714 | * We play buffer_head aliasing tricks to write data/metadata blocks to |
| 715 | * the journal without copying their contents, but for journal |
| 716 | * descriptor blocks we do need to generate bona fide buffers. |
| 717 | * |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 718 | * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 719 | * the buffer's contents they really should run flush_dcache_page(bh->b_page). |
| 720 | * But we don't bother doing that, so there will be coherency problems with |
| 721 | * mmaps of blockdevs which hold live JBD-controlled filesystems. |
| 722 | */ |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 723 | struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 724 | { |
| 725 | struct buffer_head *bh; |
Mingming Cao | 18eba7a | 2006-10-11 01:21:13 -0700 | [diff] [blame] | 726 | unsigned long long blocknr; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 727 | int err; |
| 728 | |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 729 | err = jbd2_journal_next_log_block(journal, &blocknr); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 730 | |
| 731 | if (err) |
| 732 | return NULL; |
| 733 | |
| 734 | bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize); |
Jan Kara | 4b90567 | 2009-01-06 14:53:35 -0500 | [diff] [blame] | 735 | if (!bh) |
| 736 | return NULL; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 737 | lock_buffer(bh); |
| 738 | memset(bh->b_data, 0, journal->j_blocksize); |
| 739 | set_buffer_uptodate(bh); |
| 740 | unlock_buffer(bh); |
| 741 | BUFFER_TRACE(bh, "return this buffer"); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 742 | return jbd2_journal_add_journal_head(bh); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 743 | } |
| 744 | |
Jan Kara | 79feb52 | 2012-03-13 22:22:54 -0400 | [diff] [blame^] | 745 | /* |
| 746 | * Return tid of the oldest transaction in the journal and block in the journal |
| 747 | * where the transaction starts. |
| 748 | * |
| 749 | * If the journal is now empty, return which will be the next transaction ID |
| 750 | * we will write and where will that transaction start. |
| 751 | * |
| 752 | * The return value is 0 if journal tail cannot be pushed any further, 1 if |
| 753 | * it can. |
| 754 | */ |
| 755 | int jbd2_journal_get_log_tail(journal_t *journal, tid_t *tid, |
| 756 | unsigned long *block) |
| 757 | { |
| 758 | transaction_t *transaction; |
| 759 | int ret; |
| 760 | |
| 761 | read_lock(&journal->j_state_lock); |
| 762 | spin_lock(&journal->j_list_lock); |
| 763 | transaction = journal->j_checkpoint_transactions; |
| 764 | if (transaction) { |
| 765 | *tid = transaction->t_tid; |
| 766 | *block = transaction->t_log_start; |
| 767 | } else if ((transaction = journal->j_committing_transaction) != NULL) { |
| 768 | *tid = transaction->t_tid; |
| 769 | *block = transaction->t_log_start; |
| 770 | } else if ((transaction = journal->j_running_transaction) != NULL) { |
| 771 | *tid = transaction->t_tid; |
| 772 | *block = journal->j_head; |
| 773 | } else { |
| 774 | *tid = journal->j_transaction_sequence; |
| 775 | *block = journal->j_head; |
| 776 | } |
| 777 | ret = tid_gt(*tid, journal->j_tail_sequence); |
| 778 | spin_unlock(&journal->j_list_lock); |
| 779 | read_unlock(&journal->j_state_lock); |
| 780 | |
| 781 | return ret; |
| 782 | } |
| 783 | |
| 784 | /* |
| 785 | * Update information in journal structure and in on disk journal superblock |
| 786 | * about log tail. This function does not check whether information passed in |
| 787 | * really pushes log tail further. It's responsibility of the caller to make |
| 788 | * sure provided log tail information is valid (e.g. by holding |
| 789 | * j_checkpoint_mutex all the time between computing log tail and calling this |
| 790 | * function as is the case with jbd2_cleanup_journal_tail()). |
| 791 | * |
| 792 | * Requires j_checkpoint_mutex |
| 793 | */ |
| 794 | void __jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block) |
| 795 | { |
| 796 | unsigned long freed; |
| 797 | |
| 798 | BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex)); |
| 799 | |
| 800 | /* |
| 801 | * We cannot afford for write to remain in drive's caches since as |
| 802 | * soon as we update j_tail, next transaction can start reusing journal |
| 803 | * space and if we lose sb update during power failure we'd replay |
| 804 | * old transaction with possibly newly overwritten data. |
| 805 | */ |
| 806 | jbd2_journal_update_sb_log_tail(journal, tid, block, WRITE_FUA); |
| 807 | write_lock(&journal->j_state_lock); |
| 808 | freed = block - journal->j_tail; |
| 809 | if (block < journal->j_tail) |
| 810 | freed += journal->j_last - journal->j_first; |
| 811 | |
| 812 | trace_jbd2_update_log_tail(journal, tid, block, freed); |
| 813 | jbd_debug(1, |
| 814 | "Cleaning journal tail from %d to %d (offset %lu), " |
| 815 | "freeing %lu\n", |
| 816 | journal->j_tail_sequence, tid, block, freed); |
| 817 | |
| 818 | journal->j_free += freed; |
| 819 | journal->j_tail_sequence = tid; |
| 820 | journal->j_tail = block; |
| 821 | write_unlock(&journal->j_state_lock); |
| 822 | } |
| 823 | |
Johann Lombardi | 8e85fb3 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 824 | struct jbd2_stats_proc_session { |
| 825 | journal_t *journal; |
| 826 | struct transaction_stats_s *stats; |
| 827 | int start; |
| 828 | int max; |
| 829 | }; |
| 830 | |
Johann Lombardi | 8e85fb3 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 831 | static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos) |
| 832 | { |
| 833 | return *pos ? NULL : SEQ_START_TOKEN; |
| 834 | } |
| 835 | |
| 836 | static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos) |
| 837 | { |
| 838 | return NULL; |
| 839 | } |
| 840 | |
| 841 | static int jbd2_seq_info_show(struct seq_file *seq, void *v) |
| 842 | { |
| 843 | struct jbd2_stats_proc_session *s = seq->private; |
| 844 | |
| 845 | if (v != SEQ_START_TOKEN) |
| 846 | return 0; |
Theodore Ts'o | bf69932 | 2009-09-30 00:32:06 -0400 | [diff] [blame] | 847 | seq_printf(seq, "%lu transaction, each up to %u blocks\n", |
Johann Lombardi | 8e85fb3 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 848 | s->stats->ts_tid, |
| 849 | s->journal->j_max_transaction_buffers); |
| 850 | if (s->stats->ts_tid == 0) |
| 851 | return 0; |
| 852 | seq_printf(seq, "average: \n %ums waiting for transaction\n", |
Theodore Ts'o | bf69932 | 2009-09-30 00:32:06 -0400 | [diff] [blame] | 853 | jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid)); |
Johann Lombardi | 8e85fb3 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 854 | seq_printf(seq, " %ums running transaction\n", |
Theodore Ts'o | bf69932 | 2009-09-30 00:32:06 -0400 | [diff] [blame] | 855 | jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid)); |
Johann Lombardi | 8e85fb3 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 856 | seq_printf(seq, " %ums transaction was being locked\n", |
Theodore Ts'o | bf69932 | 2009-09-30 00:32:06 -0400 | [diff] [blame] | 857 | jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid)); |
Johann Lombardi | 8e85fb3 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 858 | seq_printf(seq, " %ums flushing data (in ordered mode)\n", |
Theodore Ts'o | bf69932 | 2009-09-30 00:32:06 -0400 | [diff] [blame] | 859 | jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid)); |
Johann Lombardi | 8e85fb3 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 860 | seq_printf(seq, " %ums logging transaction\n", |
Theodore Ts'o | bf69932 | 2009-09-30 00:32:06 -0400 | [diff] [blame] | 861 | jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid)); |
Simon Holm Thøgersen | c225aa5 | 2009-01-11 22:34:01 -0500 | [diff] [blame] | 862 | seq_printf(seq, " %lluus average transaction commit time\n", |
| 863 | div_u64(s->journal->j_average_commit_time, 1000)); |
Johann Lombardi | 8e85fb3 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 864 | seq_printf(seq, " %lu handles per transaction\n", |
Theodore Ts'o | bf69932 | 2009-09-30 00:32:06 -0400 | [diff] [blame] | 865 | s->stats->run.rs_handle_count / s->stats->ts_tid); |
Johann Lombardi | 8e85fb3 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 866 | seq_printf(seq, " %lu blocks per transaction\n", |
Theodore Ts'o | bf69932 | 2009-09-30 00:32:06 -0400 | [diff] [blame] | 867 | s->stats->run.rs_blocks / s->stats->ts_tid); |
Johann Lombardi | 8e85fb3 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 868 | seq_printf(seq, " %lu logged blocks per transaction\n", |
Theodore Ts'o | bf69932 | 2009-09-30 00:32:06 -0400 | [diff] [blame] | 869 | s->stats->run.rs_blocks_logged / s->stats->ts_tid); |
Johann Lombardi | 8e85fb3 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 870 | return 0; |
| 871 | } |
| 872 | |
| 873 | static void jbd2_seq_info_stop(struct seq_file *seq, void *v) |
| 874 | { |
| 875 | } |
| 876 | |
James Morris | 88e9d34 | 2009-09-22 16:43:43 -0700 | [diff] [blame] | 877 | static const struct seq_operations jbd2_seq_info_ops = { |
Johann Lombardi | 8e85fb3 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 878 | .start = jbd2_seq_info_start, |
| 879 | .next = jbd2_seq_info_next, |
| 880 | .stop = jbd2_seq_info_stop, |
| 881 | .show = jbd2_seq_info_show, |
| 882 | }; |
| 883 | |
| 884 | static int jbd2_seq_info_open(struct inode *inode, struct file *file) |
| 885 | { |
| 886 | journal_t *journal = PDE(inode)->data; |
| 887 | struct jbd2_stats_proc_session *s; |
| 888 | int rc, size; |
| 889 | |
| 890 | s = kmalloc(sizeof(*s), GFP_KERNEL); |
| 891 | if (s == NULL) |
| 892 | return -ENOMEM; |
| 893 | size = sizeof(struct transaction_stats_s); |
| 894 | s->stats = kmalloc(size, GFP_KERNEL); |
| 895 | if (s->stats == NULL) { |
| 896 | kfree(s); |
| 897 | return -ENOMEM; |
| 898 | } |
| 899 | spin_lock(&journal->j_history_lock); |
| 900 | memcpy(s->stats, &journal->j_stats, size); |
| 901 | s->journal = journal; |
| 902 | spin_unlock(&journal->j_history_lock); |
| 903 | |
| 904 | rc = seq_open(file, &jbd2_seq_info_ops); |
| 905 | if (rc == 0) { |
| 906 | struct seq_file *m = file->private_data; |
| 907 | m->private = s; |
| 908 | } else { |
| 909 | kfree(s->stats); |
| 910 | kfree(s); |
| 911 | } |
| 912 | return rc; |
| 913 | |
| 914 | } |
| 915 | |
| 916 | static int jbd2_seq_info_release(struct inode *inode, struct file *file) |
| 917 | { |
| 918 | struct seq_file *seq = file->private_data; |
| 919 | struct jbd2_stats_proc_session *s = seq->private; |
| 920 | kfree(s->stats); |
| 921 | kfree(s); |
| 922 | return seq_release(inode, file); |
| 923 | } |
| 924 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 925 | static const struct file_operations jbd2_seq_info_fops = { |
Johann Lombardi | 8e85fb3 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 926 | .owner = THIS_MODULE, |
| 927 | .open = jbd2_seq_info_open, |
| 928 | .read = seq_read, |
| 929 | .llseek = seq_lseek, |
| 930 | .release = jbd2_seq_info_release, |
| 931 | }; |
| 932 | |
| 933 | static struct proc_dir_entry *proc_jbd2_stats; |
| 934 | |
| 935 | static void jbd2_stats_proc_init(journal_t *journal) |
| 936 | { |
Theodore Ts'o | 0549676 | 2008-09-16 14:36:17 -0400 | [diff] [blame] | 937 | journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats); |
Johann Lombardi | 8e85fb3 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 938 | if (journal->j_proc_entry) { |
Denis V. Lunev | 79da366 | 2008-04-29 01:02:11 -0700 | [diff] [blame] | 939 | proc_create_data("info", S_IRUGO, journal->j_proc_entry, |
| 940 | &jbd2_seq_info_fops, journal); |
Johann Lombardi | 8e85fb3 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 941 | } |
| 942 | } |
| 943 | |
| 944 | static void jbd2_stats_proc_exit(journal_t *journal) |
| 945 | { |
Johann Lombardi | 8e85fb3 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 946 | remove_proc_entry("info", journal->j_proc_entry); |
Theodore Ts'o | 0549676 | 2008-09-16 14:36:17 -0400 | [diff] [blame] | 947 | remove_proc_entry(journal->j_devname, proc_jbd2_stats); |
Johann Lombardi | 8e85fb3 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 948 | } |
| 949 | |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 950 | /* |
| 951 | * Management for journal control blocks: functions to create and |
| 952 | * destroy journal_t structures, and to initialise and read existing |
| 953 | * journal blocks from disk. */ |
| 954 | |
| 955 | /* First: create and setup a journal_t object in memory. We initialise |
| 956 | * very few fields yet: that has to wait until we have created the |
| 957 | * journal structures from from scratch, or loaded them from disk. */ |
| 958 | |
| 959 | static journal_t * journal_init_common (void) |
| 960 | { |
| 961 | journal_t *journal; |
| 962 | int err; |
| 963 | |
Andrew Morton | 3ebfdf8 | 2009-12-23 08:05:15 -0500 | [diff] [blame] | 964 | journal = kzalloc(sizeof(*journal), GFP_KERNEL); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 965 | if (!journal) |
Theodore Ts'o | b7271b0 | 2010-12-18 13:39:38 -0500 | [diff] [blame] | 966 | return NULL; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 967 | |
| 968 | init_waitqueue_head(&journal->j_wait_transaction_locked); |
| 969 | init_waitqueue_head(&journal->j_wait_logspace); |
| 970 | init_waitqueue_head(&journal->j_wait_done_commit); |
| 971 | init_waitqueue_head(&journal->j_wait_checkpoint); |
| 972 | init_waitqueue_head(&journal->j_wait_commit); |
| 973 | init_waitqueue_head(&journal->j_wait_updates); |
| 974 | mutex_init(&journal->j_barrier); |
| 975 | mutex_init(&journal->j_checkpoint_mutex); |
| 976 | spin_lock_init(&journal->j_revoke_lock); |
| 977 | spin_lock_init(&journal->j_list_lock); |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 978 | rwlock_init(&journal->j_state_lock); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 979 | |
Mingming Cao | cd02ff0 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 980 | journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE); |
Theodore Ts'o | 3077384 | 2009-01-03 20:27:38 -0500 | [diff] [blame] | 981 | journal->j_min_batch_time = 0; |
| 982 | journal->j_max_batch_time = 15000; /* 15ms */ |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 983 | |
| 984 | /* The journal is marked for error until we succeed with recovery! */ |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 985 | journal->j_flags = JBD2_ABORT; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 986 | |
| 987 | /* Set up a default-sized revoke table for the new mount. */ |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 988 | err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 989 | if (err) { |
| 990 | kfree(journal); |
Theodore Ts'o | b7271b0 | 2010-12-18 13:39:38 -0500 | [diff] [blame] | 991 | return NULL; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 992 | } |
Johann Lombardi | 8e85fb3 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 993 | |
Theodore Ts'o | bf69932 | 2009-09-30 00:32:06 -0400 | [diff] [blame] | 994 | spin_lock_init(&journal->j_history_lock); |
Johann Lombardi | 8e85fb3 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 995 | |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 996 | return journal; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 997 | } |
| 998 | |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 999 | /* jbd2_journal_init_dev and jbd2_journal_init_inode: |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1000 | * |
| 1001 | * Create a journal structure assigned some fixed set of disk blocks to |
| 1002 | * the journal. We don't actually touch those disk blocks yet, but we |
| 1003 | * need to set up all of the mapping information to tell the journaling |
| 1004 | * system where the journal blocks are. |
| 1005 | * |
| 1006 | */ |
| 1007 | |
| 1008 | /** |
Randy Dunlap | 5648ba5 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1009 | * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1010 | * @bdev: Block device on which to create the journal |
| 1011 | * @fs_dev: Device which hold journalled filesystem for this journal. |
| 1012 | * @start: Block nr Start of journal. |
| 1013 | * @len: Length of the journal in blocks. |
| 1014 | * @blocksize: blocksize of journalling device |
Randy Dunlap | 5648ba5 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1015 | * |
| 1016 | * Returns: a newly created journal_t * |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1017 | * |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1018 | * jbd2_journal_init_dev creates a journal which maps a fixed contiguous |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1019 | * range of blocks on an arbitrary block device. |
| 1020 | * |
| 1021 | */ |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1022 | journal_t * jbd2_journal_init_dev(struct block_device *bdev, |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1023 | struct block_device *fs_dev, |
Mingming Cao | 18eba7a | 2006-10-11 01:21:13 -0700 | [diff] [blame] | 1024 | unsigned long long start, int len, int blocksize) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1025 | { |
| 1026 | journal_t *journal = journal_init_common(); |
| 1027 | struct buffer_head *bh; |
Theodore Ts'o | 0549676 | 2008-09-16 14:36:17 -0400 | [diff] [blame] | 1028 | char *p; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1029 | int n; |
| 1030 | |
| 1031 | if (!journal) |
| 1032 | return NULL; |
| 1033 | |
| 1034 | /* journal descriptor can store up to n blocks -bzzz */ |
| 1035 | journal->j_blocksize = blocksize; |
yangsheng | 0587aa3 | 2010-11-17 21:46:26 -0500 | [diff] [blame] | 1036 | journal->j_dev = bdev; |
| 1037 | journal->j_fs_dev = fs_dev; |
| 1038 | journal->j_blk_offset = start; |
| 1039 | journal->j_maxlen = len; |
| 1040 | bdevname(journal->j_dev, journal->j_devname); |
| 1041 | p = journal->j_devname; |
| 1042 | while ((p = strchr(p, '/'))) |
| 1043 | *p = '!'; |
Jan Kara | 4b90567 | 2009-01-06 14:53:35 -0500 | [diff] [blame] | 1044 | jbd2_stats_proc_init(journal); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1045 | n = journal->j_blocksize / sizeof(journal_block_tag_t); |
| 1046 | journal->j_wbufsize = n; |
| 1047 | journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL); |
| 1048 | if (!journal->j_wbuf) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1049 | printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n", |
Harvey Harrison | 329d291 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1050 | __func__); |
Jan Kara | 4b90567 | 2009-01-06 14:53:35 -0500 | [diff] [blame] | 1051 | goto out_err; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1052 | } |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1053 | |
| 1054 | bh = __getblk(journal->j_dev, start, journal->j_blocksize); |
Jan Kara | 4b90567 | 2009-01-06 14:53:35 -0500 | [diff] [blame] | 1055 | if (!bh) { |
| 1056 | printk(KERN_ERR |
| 1057 | "%s: Cannot get buffer for journal superblock\n", |
| 1058 | __func__); |
| 1059 | goto out_err; |
| 1060 | } |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1061 | journal->j_sb_buffer = bh; |
| 1062 | journal->j_superblock = (journal_superblock_t *)bh->b_data; |
Jan Kara | 4b90567 | 2009-01-06 14:53:35 -0500 | [diff] [blame] | 1063 | |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1064 | return journal; |
Jan Kara | 4b90567 | 2009-01-06 14:53:35 -0500 | [diff] [blame] | 1065 | out_err: |
Tao Ma | 7b02bec | 2009-11-10 17:13:22 +0800 | [diff] [blame] | 1066 | kfree(journal->j_wbuf); |
Jan Kara | 4b90567 | 2009-01-06 14:53:35 -0500 | [diff] [blame] | 1067 | jbd2_stats_proc_exit(journal); |
| 1068 | kfree(journal); |
| 1069 | return NULL; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | /** |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1073 | * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode. |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1074 | * @inode: An inode to create the journal in |
| 1075 | * |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1076 | * jbd2_journal_init_inode creates a journal which maps an on-disk inode as |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1077 | * the journal. The inode must exist already, must support bmap() and |
| 1078 | * must have all data blocks preallocated. |
| 1079 | */ |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1080 | journal_t * jbd2_journal_init_inode (struct inode *inode) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1081 | { |
| 1082 | struct buffer_head *bh; |
| 1083 | journal_t *journal = journal_init_common(); |
Theodore Ts'o | 0549676 | 2008-09-16 14:36:17 -0400 | [diff] [blame] | 1084 | char *p; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1085 | int err; |
| 1086 | int n; |
Mingming Cao | 18eba7a | 2006-10-11 01:21:13 -0700 | [diff] [blame] | 1087 | unsigned long long blocknr; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1088 | |
| 1089 | if (!journal) |
| 1090 | return NULL; |
| 1091 | |
| 1092 | journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev; |
| 1093 | journal->j_inode = inode; |
Theodore Ts'o | 0549676 | 2008-09-16 14:36:17 -0400 | [diff] [blame] | 1094 | bdevname(journal->j_dev, journal->j_devname); |
| 1095 | p = journal->j_devname; |
| 1096 | while ((p = strchr(p, '/'))) |
| 1097 | *p = '!'; |
| 1098 | p = journal->j_devname + strlen(journal->j_devname); |
Theodore Ts'o | 90576c0 | 2009-09-29 15:51:30 -0400 | [diff] [blame] | 1099 | sprintf(p, "-%lu", journal->j_inode->i_ino); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1100 | jbd_debug(1, |
| 1101 | "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n", |
| 1102 | journal, inode->i_sb->s_id, inode->i_ino, |
| 1103 | (long long) inode->i_size, |
| 1104 | inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize); |
| 1105 | |
| 1106 | journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits; |
| 1107 | journal->j_blocksize = inode->i_sb->s_blocksize; |
Johann Lombardi | 8e85fb3 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1108 | jbd2_stats_proc_init(journal); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1109 | |
| 1110 | /* journal descriptor can store up to n blocks -bzzz */ |
| 1111 | n = journal->j_blocksize / sizeof(journal_block_tag_t); |
| 1112 | journal->j_wbufsize = n; |
| 1113 | journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL); |
| 1114 | if (!journal->j_wbuf) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1115 | printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n", |
Harvey Harrison | 329d291 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1116 | __func__); |
Jan Kara | 4b90567 | 2009-01-06 14:53:35 -0500 | [diff] [blame] | 1117 | goto out_err; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1118 | } |
| 1119 | |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1120 | err = jbd2_journal_bmap(journal, 0, &blocknr); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1121 | /* If that failed, give up */ |
| 1122 | if (err) { |
Justin P. Mattock | 3c26bdb | 2011-02-26 20:34:05 -0800 | [diff] [blame] | 1123 | printk(KERN_ERR "%s: Cannot locate journal superblock\n", |
Harvey Harrison | 329d291 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1124 | __func__); |
Jan Kara | 4b90567 | 2009-01-06 14:53:35 -0500 | [diff] [blame] | 1125 | goto out_err; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize); |
Jan Kara | 4b90567 | 2009-01-06 14:53:35 -0500 | [diff] [blame] | 1129 | if (!bh) { |
| 1130 | printk(KERN_ERR |
| 1131 | "%s: Cannot get buffer for journal superblock\n", |
| 1132 | __func__); |
| 1133 | goto out_err; |
| 1134 | } |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1135 | journal->j_sb_buffer = bh; |
| 1136 | journal->j_superblock = (journal_superblock_t *)bh->b_data; |
| 1137 | |
| 1138 | return journal; |
Jan Kara | 4b90567 | 2009-01-06 14:53:35 -0500 | [diff] [blame] | 1139 | out_err: |
Tao Ma | 7b02bec | 2009-11-10 17:13:22 +0800 | [diff] [blame] | 1140 | kfree(journal->j_wbuf); |
Jan Kara | 4b90567 | 2009-01-06 14:53:35 -0500 | [diff] [blame] | 1141 | jbd2_stats_proc_exit(journal); |
| 1142 | kfree(journal); |
| 1143 | return NULL; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | /* |
| 1147 | * If the journal init or create aborts, we need to mark the journal |
| 1148 | * superblock as being NULL to prevent the journal destroy from writing |
| 1149 | * back a bogus superblock. |
| 1150 | */ |
| 1151 | static void journal_fail_superblock (journal_t *journal) |
| 1152 | { |
| 1153 | struct buffer_head *bh = journal->j_sb_buffer; |
| 1154 | brelse(bh); |
| 1155 | journal->j_sb_buffer = NULL; |
| 1156 | } |
| 1157 | |
| 1158 | /* |
| 1159 | * Given a journal_t structure, initialise the various fields for |
| 1160 | * startup of a new journaling session. We use this both when creating |
| 1161 | * a journal, and after recovering an old journal to reset it for |
| 1162 | * subsequent use. |
| 1163 | */ |
| 1164 | |
| 1165 | static int journal_reset(journal_t *journal) |
| 1166 | { |
| 1167 | journal_superblock_t *sb = journal->j_superblock; |
Mingming Cao | 18eba7a | 2006-10-11 01:21:13 -0700 | [diff] [blame] | 1168 | unsigned long long first, last; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1169 | |
| 1170 | first = be32_to_cpu(sb->s_first); |
| 1171 | last = be32_to_cpu(sb->s_maxlen); |
Jan Kara | f6f50e2 | 2009-07-17 10:40:01 -0400 | [diff] [blame] | 1172 | if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) { |
Eryu Guan | f2a4452 | 2011-11-01 19:09:18 -0400 | [diff] [blame] | 1173 | printk(KERN_ERR "JBD2: Journal too short (blocks %llu-%llu).\n", |
Jan Kara | f6f50e2 | 2009-07-17 10:40:01 -0400 | [diff] [blame] | 1174 | first, last); |
| 1175 | journal_fail_superblock(journal); |
| 1176 | return -EINVAL; |
| 1177 | } |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1178 | |
| 1179 | journal->j_first = first; |
| 1180 | journal->j_last = last; |
| 1181 | |
| 1182 | journal->j_head = first; |
| 1183 | journal->j_tail = first; |
| 1184 | journal->j_free = last - first; |
| 1185 | |
| 1186 | journal->j_tail_sequence = journal->j_transaction_sequence; |
| 1187 | journal->j_commit_sequence = journal->j_transaction_sequence - 1; |
| 1188 | journal->j_commit_request = journal->j_commit_sequence; |
| 1189 | |
| 1190 | journal->j_max_transaction_buffers = journal->j_maxlen / 4; |
| 1191 | |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1192 | /* |
| 1193 | * As a special case, if the on-disk copy is already marked as needing |
Jan Kara | 24bcc89 | 2012-03-13 15:41:04 -0400 | [diff] [blame] | 1194 | * no recovery (s_start == 0), then we can safely defer the superblock |
| 1195 | * update until the next commit by setting JBD2_FLUSHED. This avoids |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1196 | * attempting a write to a potential-readonly device. |
| 1197 | */ |
Jan Kara | 24bcc89 | 2012-03-13 15:41:04 -0400 | [diff] [blame] | 1198 | if (sb->s_start == 0) { |
Eryu Guan | f2a4452 | 2011-11-01 19:09:18 -0400 | [diff] [blame] | 1199 | jbd_debug(1, "JBD2: Skipping superblock update on recovered sb " |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1200 | "(start %ld, seq %d, errno %d)\n", |
| 1201 | journal->j_tail, journal->j_tail_sequence, |
| 1202 | journal->j_errno); |
Jan Kara | 24bcc89 | 2012-03-13 15:41:04 -0400 | [diff] [blame] | 1203 | journal->j_flags |= JBD2_FLUSHED; |
| 1204 | } else { |
Jan Kara | a78bb11 | 2012-03-13 15:43:04 -0400 | [diff] [blame] | 1205 | /* Lock here to make assertions happy... */ |
| 1206 | mutex_lock(&journal->j_checkpoint_mutex); |
Jan Kara | 79feb52 | 2012-03-13 22:22:54 -0400 | [diff] [blame^] | 1207 | /* |
| 1208 | * Update log tail information. We use WRITE_FUA since new |
| 1209 | * transaction will start reusing journal space and so we |
| 1210 | * must make sure information about current log tail is on |
| 1211 | * disk before that. |
| 1212 | */ |
| 1213 | jbd2_journal_update_sb_log_tail(journal, |
| 1214 | journal->j_tail_sequence, |
| 1215 | journal->j_tail, |
| 1216 | WRITE_FUA); |
Jan Kara | a78bb11 | 2012-03-13 15:43:04 -0400 | [diff] [blame] | 1217 | mutex_unlock(&journal->j_checkpoint_mutex); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1218 | } |
Jan Kara | 24bcc89 | 2012-03-13 15:41:04 -0400 | [diff] [blame] | 1219 | return jbd2_journal_start_thread(journal); |
| 1220 | } |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1221 | |
Jan Kara | 79feb52 | 2012-03-13 22:22:54 -0400 | [diff] [blame^] | 1222 | static void jbd2_write_superblock(journal_t *journal, int write_op) |
Jan Kara | 24bcc89 | 2012-03-13 15:41:04 -0400 | [diff] [blame] | 1223 | { |
| 1224 | struct buffer_head *bh = journal->j_sb_buffer; |
Jan Kara | 79feb52 | 2012-03-13 22:22:54 -0400 | [diff] [blame^] | 1225 | int ret; |
Jan Kara | 24bcc89 | 2012-03-13 15:41:04 -0400 | [diff] [blame] | 1226 | |
Jan Kara | 79feb52 | 2012-03-13 22:22:54 -0400 | [diff] [blame^] | 1227 | trace_jbd2_write_superblock(journal, write_op); |
| 1228 | if (!(journal->j_flags & JBD2_BARRIER)) |
| 1229 | write_op &= ~(REQ_FUA | REQ_FLUSH); |
| 1230 | lock_buffer(bh); |
Theodore Ts'o | 914258b | 2008-10-06 21:35:40 -0400 | [diff] [blame] | 1231 | if (buffer_write_io_error(bh)) { |
| 1232 | /* |
| 1233 | * Oh, dear. A previous attempt to write the journal |
| 1234 | * superblock failed. This could happen because the |
| 1235 | * USB device was yanked out. Or it could happen to |
| 1236 | * be a transient write error and maybe the block will |
| 1237 | * be remapped. Nothing we can do but to retry the |
| 1238 | * write and hope for the best. |
| 1239 | */ |
| 1240 | printk(KERN_ERR "JBD2: previous I/O error detected " |
| 1241 | "for journal superblock update for %s.\n", |
| 1242 | journal->j_devname); |
| 1243 | clear_buffer_write_io_error(bh); |
| 1244 | set_buffer_uptodate(bh); |
| 1245 | } |
Jan Kara | 79feb52 | 2012-03-13 22:22:54 -0400 | [diff] [blame^] | 1246 | get_bh(bh); |
| 1247 | bh->b_end_io = end_buffer_write_sync; |
| 1248 | ret = submit_bh(write_op, bh); |
| 1249 | wait_on_buffer(bh); |
Jan Kara | 24bcc89 | 2012-03-13 15:41:04 -0400 | [diff] [blame] | 1250 | if (buffer_write_io_error(bh)) { |
Jan Kara | 24bcc89 | 2012-03-13 15:41:04 -0400 | [diff] [blame] | 1251 | clear_buffer_write_io_error(bh); |
| 1252 | set_buffer_uptodate(bh); |
Jan Kara | 79feb52 | 2012-03-13 22:22:54 -0400 | [diff] [blame^] | 1253 | ret = -EIO; |
| 1254 | } |
| 1255 | if (ret) { |
| 1256 | printk(KERN_ERR "JBD2: Error %d detected when updating " |
| 1257 | "journal superblock for %s.\n", ret, |
| 1258 | journal->j_devname); |
Jan Kara | 24bcc89 | 2012-03-13 15:41:04 -0400 | [diff] [blame] | 1259 | } |
| 1260 | } |
| 1261 | |
| 1262 | /** |
| 1263 | * jbd2_journal_update_sb_log_tail() - Update log tail in journal sb on disk. |
| 1264 | * @journal: The journal to update. |
Jan Kara | 79feb52 | 2012-03-13 22:22:54 -0400 | [diff] [blame^] | 1265 | * @tail_tid: TID of the new transaction at the tail of the log |
| 1266 | * @tail_block: The first block of the transaction at the tail of the log |
| 1267 | * @write_op: With which operation should we write the journal sb |
Jan Kara | 24bcc89 | 2012-03-13 15:41:04 -0400 | [diff] [blame] | 1268 | * |
| 1269 | * Update a journal's superblock information about log tail and write it to |
| 1270 | * disk, waiting for the IO to complete. |
| 1271 | */ |
Jan Kara | 79feb52 | 2012-03-13 22:22:54 -0400 | [diff] [blame^] | 1272 | void jbd2_journal_update_sb_log_tail(journal_t *journal, tid_t tail_tid, |
| 1273 | unsigned long tail_block, int write_op) |
Jan Kara | 24bcc89 | 2012-03-13 15:41:04 -0400 | [diff] [blame] | 1274 | { |
| 1275 | journal_superblock_t *sb = journal->j_superblock; |
| 1276 | |
Jan Kara | a78bb11 | 2012-03-13 15:43:04 -0400 | [diff] [blame] | 1277 | BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex)); |
Jan Kara | 79feb52 | 2012-03-13 22:22:54 -0400 | [diff] [blame^] | 1278 | jbd_debug(1, "JBD2: updating superblock (start %lu, seq %u)\n", |
| 1279 | tail_block, tail_tid); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1280 | |
Jan Kara | 79feb52 | 2012-03-13 22:22:54 -0400 | [diff] [blame^] | 1281 | sb->s_sequence = cpu_to_be32(tail_tid); |
| 1282 | sb->s_start = cpu_to_be32(tail_block); |
Jan Kara | 24bcc89 | 2012-03-13 15:41:04 -0400 | [diff] [blame] | 1283 | |
Jan Kara | 79feb52 | 2012-03-13 22:22:54 -0400 | [diff] [blame^] | 1284 | jbd2_write_superblock(journal, write_op); |
Jan Kara | 24bcc89 | 2012-03-13 15:41:04 -0400 | [diff] [blame] | 1285 | |
| 1286 | /* Log is no longer empty */ |
| 1287 | write_lock(&journal->j_state_lock); |
| 1288 | WARN_ON(!sb->s_sequence); |
| 1289 | journal->j_flags &= ~JBD2_FLUSHED; |
| 1290 | write_unlock(&journal->j_state_lock); |
| 1291 | } |
| 1292 | |
| 1293 | /** |
| 1294 | * jbd2_mark_journal_empty() - Mark on disk journal as empty. |
| 1295 | * @journal: The journal to update. |
| 1296 | * |
| 1297 | * Update a journal's dynamic superblock fields to show that journal is empty. |
| 1298 | * Write updated superblock to disk waiting for IO to complete. |
| 1299 | */ |
| 1300 | static void jbd2_mark_journal_empty(journal_t *journal) |
| 1301 | { |
| 1302 | journal_superblock_t *sb = journal->j_superblock; |
| 1303 | |
Jan Kara | a78bb11 | 2012-03-13 15:43:04 -0400 | [diff] [blame] | 1304 | BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex)); |
Jan Kara | 24bcc89 | 2012-03-13 15:41:04 -0400 | [diff] [blame] | 1305 | read_lock(&journal->j_state_lock); |
| 1306 | jbd_debug(1, "JBD2: Marking journal as empty (seq %d)\n", |
| 1307 | journal->j_tail_sequence); |
| 1308 | |
| 1309 | sb->s_sequence = cpu_to_be32(journal->j_tail_sequence); |
| 1310 | sb->s_start = cpu_to_be32(0); |
| 1311 | read_unlock(&journal->j_state_lock); |
| 1312 | |
Jan Kara | 79feb52 | 2012-03-13 22:22:54 -0400 | [diff] [blame^] | 1313 | jbd2_write_superblock(journal, WRITE_FUA); |
Jan Kara | 24bcc89 | 2012-03-13 15:41:04 -0400 | [diff] [blame] | 1314 | |
| 1315 | /* Log is no longer empty */ |
| 1316 | write_lock(&journal->j_state_lock); |
| 1317 | journal->j_flags |= JBD2_FLUSHED; |
| 1318 | write_unlock(&journal->j_state_lock); |
| 1319 | } |
| 1320 | |
| 1321 | |
| 1322 | /** |
| 1323 | * jbd2_journal_update_sb_errno() - Update error in the journal. |
| 1324 | * @journal: The journal to update. |
| 1325 | * |
| 1326 | * Update a journal's errno. Write updated superblock to disk waiting for IO |
| 1327 | * to complete. |
| 1328 | */ |
| 1329 | static void jbd2_journal_update_sb_errno(journal_t *journal) |
| 1330 | { |
| 1331 | journal_superblock_t *sb = journal->j_superblock; |
| 1332 | |
| 1333 | read_lock(&journal->j_state_lock); |
| 1334 | jbd_debug(1, "JBD2: updating superblock error (errno %d)\n", |
| 1335 | journal->j_errno); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1336 | sb->s_errno = cpu_to_be32(journal->j_errno); |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 1337 | read_unlock(&journal->j_state_lock); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1338 | |
Jan Kara | 79feb52 | 2012-03-13 22:22:54 -0400 | [diff] [blame^] | 1339 | jbd2_write_superblock(journal, WRITE_SYNC); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | /* |
| 1343 | * Read the superblock for a given journal, performing initial |
| 1344 | * validation of the format. |
| 1345 | */ |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1346 | static int journal_get_superblock(journal_t *journal) |
| 1347 | { |
| 1348 | struct buffer_head *bh; |
| 1349 | journal_superblock_t *sb; |
| 1350 | int err = -EIO; |
| 1351 | |
| 1352 | bh = journal->j_sb_buffer; |
| 1353 | |
| 1354 | J_ASSERT(bh != NULL); |
| 1355 | if (!buffer_uptodate(bh)) { |
| 1356 | ll_rw_block(READ, 1, &bh); |
| 1357 | wait_on_buffer(bh); |
| 1358 | if (!buffer_uptodate(bh)) { |
Eryu Guan | f2a4452 | 2011-11-01 19:09:18 -0400 | [diff] [blame] | 1359 | printk(KERN_ERR |
| 1360 | "JBD2: IO error reading journal superblock\n"); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1361 | goto out; |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | sb = journal->j_superblock; |
| 1366 | |
| 1367 | err = -EINVAL; |
| 1368 | |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1369 | if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) || |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1370 | sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) { |
Eryu Guan | f2a4452 | 2011-11-01 19:09:18 -0400 | [diff] [blame] | 1371 | printk(KERN_WARNING "JBD2: no valid journal superblock found\n"); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1372 | goto out; |
| 1373 | } |
| 1374 | |
| 1375 | switch(be32_to_cpu(sb->s_header.h_blocktype)) { |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1376 | case JBD2_SUPERBLOCK_V1: |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1377 | journal->j_format_version = 1; |
| 1378 | break; |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1379 | case JBD2_SUPERBLOCK_V2: |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1380 | journal->j_format_version = 2; |
| 1381 | break; |
| 1382 | default: |
Eryu Guan | f2a4452 | 2011-11-01 19:09:18 -0400 | [diff] [blame] | 1383 | printk(KERN_WARNING "JBD2: unrecognised superblock format ID\n"); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1384 | goto out; |
| 1385 | } |
| 1386 | |
| 1387 | if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen) |
| 1388 | journal->j_maxlen = be32_to_cpu(sb->s_maxlen); |
| 1389 | else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) { |
Eryu Guan | f2a4452 | 2011-11-01 19:09:18 -0400 | [diff] [blame] | 1390 | printk(KERN_WARNING "JBD2: journal file too short\n"); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1391 | goto out; |
| 1392 | } |
| 1393 | |
Eryu Guan | 8762202 | 2011-11-01 19:04:59 -0400 | [diff] [blame] | 1394 | if (be32_to_cpu(sb->s_first) == 0 || |
| 1395 | be32_to_cpu(sb->s_first) >= journal->j_maxlen) { |
| 1396 | printk(KERN_WARNING |
| 1397 | "JBD2: Invalid start block of journal: %u\n", |
| 1398 | be32_to_cpu(sb->s_first)); |
| 1399 | goto out; |
| 1400 | } |
| 1401 | |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1402 | return 0; |
| 1403 | |
| 1404 | out: |
| 1405 | journal_fail_superblock(journal); |
| 1406 | return err; |
| 1407 | } |
| 1408 | |
| 1409 | /* |
| 1410 | * Load the on-disk journal superblock and read the key fields into the |
| 1411 | * journal_t. |
| 1412 | */ |
| 1413 | |
| 1414 | static int load_superblock(journal_t *journal) |
| 1415 | { |
| 1416 | int err; |
| 1417 | journal_superblock_t *sb; |
| 1418 | |
| 1419 | err = journal_get_superblock(journal); |
| 1420 | if (err) |
| 1421 | return err; |
| 1422 | |
| 1423 | sb = journal->j_superblock; |
| 1424 | |
| 1425 | journal->j_tail_sequence = be32_to_cpu(sb->s_sequence); |
| 1426 | journal->j_tail = be32_to_cpu(sb->s_start); |
| 1427 | journal->j_first = be32_to_cpu(sb->s_first); |
| 1428 | journal->j_last = be32_to_cpu(sb->s_maxlen); |
| 1429 | journal->j_errno = be32_to_cpu(sb->s_errno); |
| 1430 | |
| 1431 | return 0; |
| 1432 | } |
| 1433 | |
| 1434 | |
| 1435 | /** |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1436 | * int jbd2_journal_load() - Read journal from disk. |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1437 | * @journal: Journal to act on. |
| 1438 | * |
| 1439 | * Given a journal_t structure which tells us which disk blocks contain |
| 1440 | * a journal, read the journal from disk to initialise the in-memory |
| 1441 | * structures. |
| 1442 | */ |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1443 | int jbd2_journal_load(journal_t *journal) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1444 | { |
| 1445 | int err; |
| 1446 | journal_superblock_t *sb; |
| 1447 | |
| 1448 | err = load_superblock(journal); |
| 1449 | if (err) |
| 1450 | return err; |
| 1451 | |
| 1452 | sb = journal->j_superblock; |
| 1453 | /* If this is a V2 superblock, then we have to check the |
| 1454 | * features flags on it. */ |
| 1455 | |
| 1456 | if (journal->j_format_version >= 2) { |
| 1457 | if ((sb->s_feature_ro_compat & |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1458 | ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) || |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1459 | (sb->s_feature_incompat & |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1460 | ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) { |
Eryu Guan | f2a4452 | 2011-11-01 19:09:18 -0400 | [diff] [blame] | 1461 | printk(KERN_WARNING |
| 1462 | "JBD2: Unrecognised features on journal\n"); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1463 | return -EINVAL; |
| 1464 | } |
| 1465 | } |
| 1466 | |
Theodore Ts'o | d2eecb0 | 2009-12-07 10:36:20 -0500 | [diff] [blame] | 1467 | /* |
| 1468 | * Create a slab for this blocksize |
| 1469 | */ |
| 1470 | err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize)); |
| 1471 | if (err) |
| 1472 | return err; |
| 1473 | |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1474 | /* Let the recovery code check whether it needs to recover any |
| 1475 | * data from the journal. */ |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1476 | if (jbd2_journal_recover(journal)) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1477 | goto recovery_error; |
| 1478 | |
Theodore Ts'o | e6a4742 | 2009-11-15 15:31:37 -0500 | [diff] [blame] | 1479 | if (journal->j_failed_commit) { |
| 1480 | printk(KERN_ERR "JBD2: journal transaction %u on %s " |
| 1481 | "is corrupt.\n", journal->j_failed_commit, |
| 1482 | journal->j_devname); |
| 1483 | return -EIO; |
| 1484 | } |
| 1485 | |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1486 | /* OK, we've finished with the dynamic journal bits: |
| 1487 | * reinitialise the dynamic contents of the superblock in memory |
| 1488 | * and reset them on disk. */ |
| 1489 | if (journal_reset(journal)) |
| 1490 | goto recovery_error; |
| 1491 | |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1492 | journal->j_flags &= ~JBD2_ABORT; |
| 1493 | journal->j_flags |= JBD2_LOADED; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1494 | return 0; |
| 1495 | |
| 1496 | recovery_error: |
Eryu Guan | f2a4452 | 2011-11-01 19:09:18 -0400 | [diff] [blame] | 1497 | printk(KERN_WARNING "JBD2: recovery failed\n"); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1498 | return -EIO; |
| 1499 | } |
| 1500 | |
| 1501 | /** |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1502 | * void jbd2_journal_destroy() - Release a journal_t structure. |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1503 | * @journal: Journal to act on. |
| 1504 | * |
| 1505 | * Release a journal_t structure once it is no longer in use by the |
| 1506 | * journaled object. |
Hidehiro Kawai | 44519fa | 2008-10-10 20:29:13 -0400 | [diff] [blame] | 1507 | * Return <0 if we couldn't clean up the journal. |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1508 | */ |
Hidehiro Kawai | 44519fa | 2008-10-10 20:29:13 -0400 | [diff] [blame] | 1509 | int jbd2_journal_destroy(journal_t *journal) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1510 | { |
Hidehiro Kawai | 44519fa | 2008-10-10 20:29:13 -0400 | [diff] [blame] | 1511 | int err = 0; |
| 1512 | |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1513 | /* Wait for the commit thread to wake up and die. */ |
| 1514 | journal_kill_thread(journal); |
| 1515 | |
| 1516 | /* Force a final log commit */ |
| 1517 | if (journal->j_running_transaction) |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1518 | jbd2_journal_commit_transaction(journal); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1519 | |
| 1520 | /* Force any old transactions to disk */ |
| 1521 | |
| 1522 | /* Totally anal locking here... */ |
| 1523 | spin_lock(&journal->j_list_lock); |
| 1524 | while (journal->j_checkpoint_transactions != NULL) { |
| 1525 | spin_unlock(&journal->j_list_lock); |
Theodore Ts'o | 1a0d378 | 2008-11-05 00:09:22 -0500 | [diff] [blame] | 1526 | mutex_lock(&journal->j_checkpoint_mutex); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1527 | jbd2_log_do_checkpoint(journal); |
Theodore Ts'o | 1a0d378 | 2008-11-05 00:09:22 -0500 | [diff] [blame] | 1528 | mutex_unlock(&journal->j_checkpoint_mutex); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1529 | spin_lock(&journal->j_list_lock); |
| 1530 | } |
| 1531 | |
| 1532 | J_ASSERT(journal->j_running_transaction == NULL); |
| 1533 | J_ASSERT(journal->j_committing_transaction == NULL); |
| 1534 | J_ASSERT(journal->j_checkpoint_transactions == NULL); |
| 1535 | spin_unlock(&journal->j_list_lock); |
| 1536 | |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1537 | if (journal->j_sb_buffer) { |
Jan Kara | a78bb11 | 2012-03-13 15:43:04 -0400 | [diff] [blame] | 1538 | if (!is_journal_aborted(journal)) { |
| 1539 | mutex_lock(&journal->j_checkpoint_mutex); |
Jan Kara | 24bcc89 | 2012-03-13 15:41:04 -0400 | [diff] [blame] | 1540 | jbd2_mark_journal_empty(journal); |
Jan Kara | a78bb11 | 2012-03-13 15:43:04 -0400 | [diff] [blame] | 1541 | mutex_unlock(&journal->j_checkpoint_mutex); |
| 1542 | } else |
Hidehiro Kawai | 44519fa | 2008-10-10 20:29:13 -0400 | [diff] [blame] | 1543 | err = -EIO; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1544 | brelse(journal->j_sb_buffer); |
| 1545 | } |
| 1546 | |
Johann Lombardi | 8e85fb3 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1547 | if (journal->j_proc_entry) |
| 1548 | jbd2_stats_proc_exit(journal); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1549 | if (journal->j_inode) |
| 1550 | iput(journal->j_inode); |
| 1551 | if (journal->j_revoke) |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1552 | jbd2_journal_destroy_revoke(journal); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1553 | kfree(journal->j_wbuf); |
| 1554 | kfree(journal); |
Hidehiro Kawai | 44519fa | 2008-10-10 20:29:13 -0400 | [diff] [blame] | 1555 | |
| 1556 | return err; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1557 | } |
| 1558 | |
| 1559 | |
| 1560 | /** |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1561 | *int jbd2_journal_check_used_features () - Check if features specified are used. |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1562 | * @journal: Journal to check. |
| 1563 | * @compat: bitmask of compatible features |
| 1564 | * @ro: bitmask of features that force read-only mount |
| 1565 | * @incompat: bitmask of incompatible features |
| 1566 | * |
| 1567 | * Check whether the journal uses all of a given set of |
| 1568 | * features. Return true (non-zero) if it does. |
| 1569 | **/ |
| 1570 | |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1571 | int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat, |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1572 | unsigned long ro, unsigned long incompat) |
| 1573 | { |
| 1574 | journal_superblock_t *sb; |
| 1575 | |
| 1576 | if (!compat && !ro && !incompat) |
| 1577 | return 1; |
Patrick J. LoPresti | 1113e1b | 2010-07-22 15:04:16 -0700 | [diff] [blame] | 1578 | /* Load journal superblock if it is not loaded yet. */ |
| 1579 | if (journal->j_format_version == 0 && |
| 1580 | journal_get_superblock(journal) != 0) |
| 1581 | return 0; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1582 | if (journal->j_format_version == 1) |
| 1583 | return 0; |
| 1584 | |
| 1585 | sb = journal->j_superblock; |
| 1586 | |
| 1587 | if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) && |
| 1588 | ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) && |
| 1589 | ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat)) |
| 1590 | return 1; |
| 1591 | |
| 1592 | return 0; |
| 1593 | } |
| 1594 | |
| 1595 | /** |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1596 | * int jbd2_journal_check_available_features() - Check feature set in journalling layer |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1597 | * @journal: Journal to check. |
| 1598 | * @compat: bitmask of compatible features |
| 1599 | * @ro: bitmask of features that force read-only mount |
| 1600 | * @incompat: bitmask of incompatible features |
| 1601 | * |
| 1602 | * Check whether the journaling code supports the use of |
| 1603 | * all of a given set of features on this journal. Return true |
| 1604 | * (non-zero) if it can. */ |
| 1605 | |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1606 | int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat, |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1607 | unsigned long ro, unsigned long incompat) |
| 1608 | { |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1609 | if (!compat && !ro && !incompat) |
| 1610 | return 1; |
| 1611 | |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1612 | /* We can support any known requested features iff the |
| 1613 | * superblock is in version 2. Otherwise we fail to support any |
| 1614 | * extended sb features. */ |
| 1615 | |
| 1616 | if (journal->j_format_version != 2) |
| 1617 | return 0; |
| 1618 | |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1619 | if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat && |
| 1620 | (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro && |
| 1621 | (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1622 | return 1; |
| 1623 | |
| 1624 | return 0; |
| 1625 | } |
| 1626 | |
| 1627 | /** |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1628 | * int jbd2_journal_set_features () - Mark a given journal feature in the superblock |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1629 | * @journal: Journal to act on. |
| 1630 | * @compat: bitmask of compatible features |
| 1631 | * @ro: bitmask of features that force read-only mount |
| 1632 | * @incompat: bitmask of incompatible features |
| 1633 | * |
| 1634 | * Mark a given journal feature as present on the |
| 1635 | * superblock. Returns true if the requested features could be set. |
| 1636 | * |
| 1637 | */ |
| 1638 | |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1639 | int jbd2_journal_set_features (journal_t *journal, unsigned long compat, |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1640 | unsigned long ro, unsigned long incompat) |
| 1641 | { |
| 1642 | journal_superblock_t *sb; |
| 1643 | |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1644 | if (jbd2_journal_check_used_features(journal, compat, ro, incompat)) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1645 | return 1; |
| 1646 | |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1647 | if (!jbd2_journal_check_available_features(journal, compat, ro, incompat)) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1648 | return 0; |
| 1649 | |
| 1650 | jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n", |
| 1651 | compat, ro, incompat); |
| 1652 | |
| 1653 | sb = journal->j_superblock; |
| 1654 | |
| 1655 | sb->s_feature_compat |= cpu_to_be32(compat); |
| 1656 | sb->s_feature_ro_compat |= cpu_to_be32(ro); |
| 1657 | sb->s_feature_incompat |= cpu_to_be32(incompat); |
| 1658 | |
| 1659 | return 1; |
| 1660 | } |
| 1661 | |
Girish Shilamkar | 818d276 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1662 | /* |
| 1663 | * jbd2_journal_clear_features () - Clear a given journal feature in the |
| 1664 | * superblock |
| 1665 | * @journal: Journal to act on. |
| 1666 | * @compat: bitmask of compatible features |
| 1667 | * @ro: bitmask of features that force read-only mount |
| 1668 | * @incompat: bitmask of incompatible features |
| 1669 | * |
| 1670 | * Clear a given journal feature as present on the |
| 1671 | * superblock. |
| 1672 | */ |
| 1673 | void jbd2_journal_clear_features(journal_t *journal, unsigned long compat, |
| 1674 | unsigned long ro, unsigned long incompat) |
| 1675 | { |
| 1676 | journal_superblock_t *sb; |
| 1677 | |
| 1678 | jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n", |
| 1679 | compat, ro, incompat); |
| 1680 | |
| 1681 | sb = journal->j_superblock; |
| 1682 | |
| 1683 | sb->s_feature_compat &= ~cpu_to_be32(compat); |
| 1684 | sb->s_feature_ro_compat &= ~cpu_to_be32(ro); |
| 1685 | sb->s_feature_incompat &= ~cpu_to_be32(incompat); |
| 1686 | } |
| 1687 | EXPORT_SYMBOL(jbd2_journal_clear_features); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1688 | |
| 1689 | /** |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1690 | * int jbd2_journal_flush () - Flush journal |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1691 | * @journal: Journal to act on. |
| 1692 | * |
| 1693 | * Flush all data for a given journal to disk and empty the journal. |
| 1694 | * Filesystems can use this when remounting readonly to ensure that |
| 1695 | * recovery does not need to happen on remount. |
| 1696 | */ |
| 1697 | |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1698 | int jbd2_journal_flush(journal_t *journal) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1699 | { |
| 1700 | int err = 0; |
| 1701 | transaction_t *transaction = NULL; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1702 | |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 1703 | write_lock(&journal->j_state_lock); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1704 | |
| 1705 | /* Force everything buffered to the log... */ |
| 1706 | if (journal->j_running_transaction) { |
| 1707 | transaction = journal->j_running_transaction; |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1708 | __jbd2_log_start_commit(journal, transaction->t_tid); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1709 | } else if (journal->j_committing_transaction) |
| 1710 | transaction = journal->j_committing_transaction; |
| 1711 | |
| 1712 | /* Wait for the log commit to complete... */ |
| 1713 | if (transaction) { |
| 1714 | tid_t tid = transaction->t_tid; |
| 1715 | |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 1716 | write_unlock(&journal->j_state_lock); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1717 | jbd2_log_wait_commit(journal, tid); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1718 | } else { |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 1719 | write_unlock(&journal->j_state_lock); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1720 | } |
| 1721 | |
| 1722 | /* ...and flush everything in the log out to disk. */ |
| 1723 | spin_lock(&journal->j_list_lock); |
| 1724 | while (!err && journal->j_checkpoint_transactions != NULL) { |
| 1725 | spin_unlock(&journal->j_list_lock); |
Hidehiro Kawai | 44519fa | 2008-10-10 20:29:13 -0400 | [diff] [blame] | 1726 | mutex_lock(&journal->j_checkpoint_mutex); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1727 | err = jbd2_log_do_checkpoint(journal); |
Hidehiro Kawai | 44519fa | 2008-10-10 20:29:13 -0400 | [diff] [blame] | 1728 | mutex_unlock(&journal->j_checkpoint_mutex); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1729 | spin_lock(&journal->j_list_lock); |
| 1730 | } |
| 1731 | spin_unlock(&journal->j_list_lock); |
Hidehiro Kawai | 44519fa | 2008-10-10 20:29:13 -0400 | [diff] [blame] | 1732 | |
| 1733 | if (is_journal_aborted(journal)) |
| 1734 | return -EIO; |
| 1735 | |
Jan Kara | a78bb11 | 2012-03-13 15:43:04 -0400 | [diff] [blame] | 1736 | mutex_lock(&journal->j_checkpoint_mutex); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1737 | jbd2_cleanup_journal_tail(journal); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1738 | |
| 1739 | /* Finally, mark the journal as really needing no recovery. |
| 1740 | * This sets s_start==0 in the underlying superblock, which is |
| 1741 | * the magic code for a fully-recovered superblock. Any future |
| 1742 | * commits of data to the journal will restore the current |
| 1743 | * s_start value. */ |
Jan Kara | 24bcc89 | 2012-03-13 15:41:04 -0400 | [diff] [blame] | 1744 | jbd2_mark_journal_empty(journal); |
Jan Kara | a78bb11 | 2012-03-13 15:43:04 -0400 | [diff] [blame] | 1745 | mutex_unlock(&journal->j_checkpoint_mutex); |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 1746 | write_lock(&journal->j_state_lock); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1747 | J_ASSERT(!journal->j_running_transaction); |
| 1748 | J_ASSERT(!journal->j_committing_transaction); |
| 1749 | J_ASSERT(!journal->j_checkpoint_transactions); |
| 1750 | J_ASSERT(journal->j_head == journal->j_tail); |
| 1751 | J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence); |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 1752 | write_unlock(&journal->j_state_lock); |
Hidehiro Kawai | 44519fa | 2008-10-10 20:29:13 -0400 | [diff] [blame] | 1753 | return 0; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1754 | } |
| 1755 | |
| 1756 | /** |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1757 | * int jbd2_journal_wipe() - Wipe journal contents |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1758 | * @journal: Journal to act on. |
| 1759 | * @write: flag (see below) |
| 1760 | * |
| 1761 | * Wipe out all of the contents of a journal, safely. This will produce |
| 1762 | * a warning if the journal contains any valid recovery information. |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1763 | * Must be called between journal_init_*() and jbd2_journal_load(). |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1764 | * |
| 1765 | * If 'write' is non-zero, then we wipe out the journal on disk; otherwise |
| 1766 | * we merely suppress recovery. |
| 1767 | */ |
| 1768 | |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1769 | int jbd2_journal_wipe(journal_t *journal, int write) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1770 | { |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1771 | int err = 0; |
| 1772 | |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1773 | J_ASSERT (!(journal->j_flags & JBD2_LOADED)); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1774 | |
| 1775 | err = load_superblock(journal); |
| 1776 | if (err) |
| 1777 | return err; |
| 1778 | |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1779 | if (!journal->j_tail) |
| 1780 | goto no_recovery; |
| 1781 | |
Eryu Guan | f2a4452 | 2011-11-01 19:09:18 -0400 | [diff] [blame] | 1782 | printk(KERN_WARNING "JBD2: %s recovery information on journal\n", |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1783 | write ? "Clearing" : "Ignoring"); |
| 1784 | |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1785 | err = jbd2_journal_skip_recovery(journal); |
Jan Kara | a78bb11 | 2012-03-13 15:43:04 -0400 | [diff] [blame] | 1786 | if (write) { |
| 1787 | /* Lock to make assertions happy... */ |
| 1788 | mutex_lock(&journal->j_checkpoint_mutex); |
Jan Kara | 24bcc89 | 2012-03-13 15:41:04 -0400 | [diff] [blame] | 1789 | jbd2_mark_journal_empty(journal); |
Jan Kara | a78bb11 | 2012-03-13 15:43:04 -0400 | [diff] [blame] | 1790 | mutex_unlock(&journal->j_checkpoint_mutex); |
| 1791 | } |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1792 | |
| 1793 | no_recovery: |
| 1794 | return err; |
| 1795 | } |
| 1796 | |
| 1797 | /* |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1798 | * Journal abort has very specific semantics, which we describe |
| 1799 | * for journal abort. |
| 1800 | * |
Alberto Bertogli | bfcd355 | 2009-06-09 00:06:20 -0400 | [diff] [blame] | 1801 | * Two internal functions, which provide abort to the jbd layer |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1802 | * itself are here. |
| 1803 | */ |
| 1804 | |
| 1805 | /* |
| 1806 | * Quick version for internal journal use (doesn't lock the journal). |
| 1807 | * Aborts hard --- we mark the abort as occurred, but do _nothing_ else, |
| 1808 | * and don't attempt to make any other journal updates. |
| 1809 | */ |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1810 | void __jbd2_journal_abort_hard(journal_t *journal) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1811 | { |
| 1812 | transaction_t *transaction; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1813 | |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1814 | if (journal->j_flags & JBD2_ABORT) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1815 | return; |
| 1816 | |
| 1817 | printk(KERN_ERR "Aborting journal on device %s.\n", |
Theodore Ts'o | 0549676 | 2008-09-16 14:36:17 -0400 | [diff] [blame] | 1818 | journal->j_devname); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1819 | |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 1820 | write_lock(&journal->j_state_lock); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1821 | journal->j_flags |= JBD2_ABORT; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1822 | transaction = journal->j_running_transaction; |
| 1823 | if (transaction) |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1824 | __jbd2_log_start_commit(journal, transaction->t_tid); |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 1825 | write_unlock(&journal->j_state_lock); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1826 | } |
| 1827 | |
| 1828 | /* Soft abort: record the abort error status in the journal superblock, |
| 1829 | * but don't do any other IO. */ |
| 1830 | static void __journal_abort_soft (journal_t *journal, int errno) |
| 1831 | { |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1832 | if (journal->j_flags & JBD2_ABORT) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1833 | return; |
| 1834 | |
| 1835 | if (!journal->j_errno) |
| 1836 | journal->j_errno = errno; |
| 1837 | |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1838 | __jbd2_journal_abort_hard(journal); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1839 | |
| 1840 | if (errno) |
Jan Kara | 24bcc89 | 2012-03-13 15:41:04 -0400 | [diff] [blame] | 1841 | jbd2_journal_update_sb_errno(journal); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1842 | } |
| 1843 | |
| 1844 | /** |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1845 | * void jbd2_journal_abort () - Shutdown the journal immediately. |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1846 | * @journal: the journal to shutdown. |
| 1847 | * @errno: an error number to record in the journal indicating |
| 1848 | * the reason for the shutdown. |
| 1849 | * |
| 1850 | * Perform a complete, immediate shutdown of the ENTIRE |
| 1851 | * journal (not of a single transaction). This operation cannot be |
| 1852 | * undone without closing and reopening the journal. |
| 1853 | * |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1854 | * The jbd2_journal_abort function is intended to support higher level error |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1855 | * recovery mechanisms such as the ext2/ext3 remount-readonly error |
| 1856 | * mode. |
| 1857 | * |
| 1858 | * Journal abort has very specific semantics. Any existing dirty, |
| 1859 | * unjournaled buffers in the main filesystem will still be written to |
| 1860 | * disk by bdflush, but the journaling mechanism will be suspended |
| 1861 | * immediately and no further transaction commits will be honoured. |
| 1862 | * |
| 1863 | * Any dirty, journaled buffers will be written back to disk without |
| 1864 | * hitting the journal. Atomicity cannot be guaranteed on an aborted |
| 1865 | * filesystem, but we _do_ attempt to leave as much data as possible |
| 1866 | * behind for fsck to use for cleanup. |
| 1867 | * |
| 1868 | * Any attempt to get a new transaction handle on a journal which is in |
| 1869 | * ABORT state will just result in an -EROFS error return. A |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1870 | * jbd2_journal_stop on an existing handle will return -EIO if we have |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1871 | * entered abort state during the update. |
| 1872 | * |
| 1873 | * Recursive transactions are not disturbed by journal abort until the |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1874 | * final jbd2_journal_stop, which will receive the -EIO error. |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1875 | * |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1876 | * Finally, the jbd2_journal_abort call allows the caller to supply an errno |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1877 | * which will be recorded (if possible) in the journal superblock. This |
| 1878 | * allows a client to record failure conditions in the middle of a |
| 1879 | * transaction without having to complete the transaction to record the |
| 1880 | * failure to disk. ext3_error, for example, now uses this |
| 1881 | * functionality. |
| 1882 | * |
| 1883 | * Errors which originate from within the journaling layer will NOT |
| 1884 | * supply an errno; a null errno implies that absolutely no further |
| 1885 | * writes are done to the journal (unless there are any already in |
| 1886 | * progress). |
| 1887 | * |
| 1888 | */ |
| 1889 | |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1890 | void jbd2_journal_abort(journal_t *journal, int errno) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1891 | { |
| 1892 | __journal_abort_soft(journal, errno); |
| 1893 | } |
| 1894 | |
| 1895 | /** |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1896 | * int jbd2_journal_errno () - returns the journal's error state. |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1897 | * @journal: journal to examine. |
| 1898 | * |
Alberto Bertogli | bfcd355 | 2009-06-09 00:06:20 -0400 | [diff] [blame] | 1899 | * This is the errno number set with jbd2_journal_abort(), the last |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1900 | * time the journal was mounted - if the journal was stopped |
| 1901 | * without calling abort this will be 0. |
| 1902 | * |
| 1903 | * If the journal has been aborted on this mount time -EROFS will |
| 1904 | * be returned. |
| 1905 | */ |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1906 | int jbd2_journal_errno(journal_t *journal) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1907 | { |
| 1908 | int err; |
| 1909 | |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 1910 | read_lock(&journal->j_state_lock); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1911 | if (journal->j_flags & JBD2_ABORT) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1912 | err = -EROFS; |
| 1913 | else |
| 1914 | err = journal->j_errno; |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 1915 | read_unlock(&journal->j_state_lock); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1916 | return err; |
| 1917 | } |
| 1918 | |
| 1919 | /** |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1920 | * int jbd2_journal_clear_err () - clears the journal's error state |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1921 | * @journal: journal to act on. |
| 1922 | * |
Alberto Bertogli | bfcd355 | 2009-06-09 00:06:20 -0400 | [diff] [blame] | 1923 | * An error must be cleared or acked to take a FS out of readonly |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1924 | * mode. |
| 1925 | */ |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1926 | int jbd2_journal_clear_err(journal_t *journal) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1927 | { |
| 1928 | int err = 0; |
| 1929 | |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 1930 | write_lock(&journal->j_state_lock); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1931 | if (journal->j_flags & JBD2_ABORT) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1932 | err = -EROFS; |
| 1933 | else |
| 1934 | journal->j_errno = 0; |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 1935 | write_unlock(&journal->j_state_lock); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1936 | return err; |
| 1937 | } |
| 1938 | |
| 1939 | /** |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1940 | * void jbd2_journal_ack_err() - Ack journal err. |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1941 | * @journal: journal to act on. |
| 1942 | * |
Alberto Bertogli | bfcd355 | 2009-06-09 00:06:20 -0400 | [diff] [blame] | 1943 | * An error must be cleared or acked to take a FS out of readonly |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1944 | * mode. |
| 1945 | */ |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1946 | void jbd2_journal_ack_err(journal_t *journal) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1947 | { |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 1948 | write_lock(&journal->j_state_lock); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1949 | if (journal->j_errno) |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1950 | journal->j_flags |= JBD2_ACK_ERR; |
Theodore Ts'o | a931da6 | 2010-08-03 21:35:12 -0400 | [diff] [blame] | 1951 | write_unlock(&journal->j_state_lock); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1952 | } |
| 1953 | |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 1954 | int jbd2_journal_blocks_per_page(struct inode *inode) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1955 | { |
| 1956 | return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); |
| 1957 | } |
| 1958 | |
| 1959 | /* |
Zach Brown | b517bea | 2006-10-11 01:21:08 -0700 | [diff] [blame] | 1960 | * helper functions to deal with 32 or 64bit block numbers. |
| 1961 | */ |
| 1962 | size_t journal_tag_bytes(journal_t *journal) |
| 1963 | { |
| 1964 | if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT)) |
Mingming Cao | cd02ff0 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 1965 | return JBD2_TAG_SIZE64; |
Zach Brown | b517bea | 2006-10-11 01:21:08 -0700 | [diff] [blame] | 1966 | else |
Mingming Cao | cd02ff0 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 1967 | return JBD2_TAG_SIZE32; |
Zach Brown | b517bea | 2006-10-11 01:21:08 -0700 | [diff] [blame] | 1968 | } |
| 1969 | |
| 1970 | /* |
Theodore Ts'o | d2eecb0 | 2009-12-07 10:36:20 -0500 | [diff] [blame] | 1971 | * JBD memory management |
| 1972 | * |
| 1973 | * These functions are used to allocate block-sized chunks of memory |
| 1974 | * used for making copies of buffer_head data. Very often it will be |
| 1975 | * page-sized chunks of data, but sometimes it will be in |
| 1976 | * sub-page-size chunks. (For example, 16k pages on Power systems |
| 1977 | * with a 4k block file system.) For blocks smaller than a page, we |
| 1978 | * use a SLAB allocator. There are slab caches for each block size, |
| 1979 | * which are allocated at mount time, if necessary, and we only free |
| 1980 | * (all of) the slab caches when/if the jbd2 module is unloaded. For |
| 1981 | * this reason we don't need to a mutex to protect access to |
| 1982 | * jbd2_slab[] allocating or releasing memory; only in |
| 1983 | * jbd2_journal_create_slab(). |
| 1984 | */ |
| 1985 | #define JBD2_MAX_SLABS 8 |
| 1986 | static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS]; |
Theodore Ts'o | d2eecb0 | 2009-12-07 10:36:20 -0500 | [diff] [blame] | 1987 | |
| 1988 | static const char *jbd2_slab_names[JBD2_MAX_SLABS] = { |
| 1989 | "jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k", |
| 1990 | "jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k" |
| 1991 | }; |
| 1992 | |
| 1993 | |
| 1994 | static void jbd2_journal_destroy_slabs(void) |
| 1995 | { |
| 1996 | int i; |
| 1997 | |
| 1998 | for (i = 0; i < JBD2_MAX_SLABS; i++) { |
| 1999 | if (jbd2_slab[i]) |
| 2000 | kmem_cache_destroy(jbd2_slab[i]); |
| 2001 | jbd2_slab[i] = NULL; |
| 2002 | } |
| 2003 | } |
| 2004 | |
| 2005 | static int jbd2_journal_create_slab(size_t size) |
| 2006 | { |
Thomas Gleixner | 51dfacde | 2010-10-16 22:34:39 +0200 | [diff] [blame] | 2007 | static DEFINE_MUTEX(jbd2_slab_create_mutex); |
Theodore Ts'o | d2eecb0 | 2009-12-07 10:36:20 -0500 | [diff] [blame] | 2008 | int i = order_base_2(size) - 10; |
| 2009 | size_t slab_size; |
| 2010 | |
| 2011 | if (size == PAGE_SIZE) |
| 2012 | return 0; |
| 2013 | |
| 2014 | if (i >= JBD2_MAX_SLABS) |
| 2015 | return -EINVAL; |
| 2016 | |
| 2017 | if (unlikely(i < 0)) |
| 2018 | i = 0; |
Thomas Gleixner | 51dfacde | 2010-10-16 22:34:39 +0200 | [diff] [blame] | 2019 | mutex_lock(&jbd2_slab_create_mutex); |
Theodore Ts'o | d2eecb0 | 2009-12-07 10:36:20 -0500 | [diff] [blame] | 2020 | if (jbd2_slab[i]) { |
Thomas Gleixner | 51dfacde | 2010-10-16 22:34:39 +0200 | [diff] [blame] | 2021 | mutex_unlock(&jbd2_slab_create_mutex); |
Theodore Ts'o | d2eecb0 | 2009-12-07 10:36:20 -0500 | [diff] [blame] | 2022 | return 0; /* Already created */ |
| 2023 | } |
| 2024 | |
| 2025 | slab_size = 1 << (i+10); |
| 2026 | jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size, |
| 2027 | slab_size, 0, NULL); |
Thomas Gleixner | 51dfacde | 2010-10-16 22:34:39 +0200 | [diff] [blame] | 2028 | mutex_unlock(&jbd2_slab_create_mutex); |
Theodore Ts'o | d2eecb0 | 2009-12-07 10:36:20 -0500 | [diff] [blame] | 2029 | if (!jbd2_slab[i]) { |
| 2030 | printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n"); |
| 2031 | return -ENOMEM; |
| 2032 | } |
| 2033 | return 0; |
| 2034 | } |
| 2035 | |
| 2036 | static struct kmem_cache *get_slab(size_t size) |
| 2037 | { |
| 2038 | int i = order_base_2(size) - 10; |
| 2039 | |
| 2040 | BUG_ON(i >= JBD2_MAX_SLABS); |
| 2041 | if (unlikely(i < 0)) |
| 2042 | i = 0; |
Bill Pemberton | 8ac97b7 | 2010-04-30 09:34:31 -0400 | [diff] [blame] | 2043 | BUG_ON(jbd2_slab[i] == NULL); |
Theodore Ts'o | d2eecb0 | 2009-12-07 10:36:20 -0500 | [diff] [blame] | 2044 | return jbd2_slab[i]; |
| 2045 | } |
| 2046 | |
| 2047 | void *jbd2_alloc(size_t size, gfp_t flags) |
| 2048 | { |
| 2049 | void *ptr; |
| 2050 | |
| 2051 | BUG_ON(size & (size-1)); /* Must be a power of 2 */ |
| 2052 | |
| 2053 | flags |= __GFP_REPEAT; |
| 2054 | if (size == PAGE_SIZE) |
| 2055 | ptr = (void *)__get_free_pages(flags, 0); |
| 2056 | else if (size > PAGE_SIZE) { |
| 2057 | int order = get_order(size); |
| 2058 | |
| 2059 | if (order < 3) |
| 2060 | ptr = (void *)__get_free_pages(flags, order); |
| 2061 | else |
| 2062 | ptr = vmalloc(size); |
| 2063 | } else |
| 2064 | ptr = kmem_cache_alloc(get_slab(size), flags); |
| 2065 | |
| 2066 | /* Check alignment; SLUB has gotten this wrong in the past, |
| 2067 | * and this can lead to user data corruption! */ |
| 2068 | BUG_ON(((unsigned long) ptr) & (size-1)); |
| 2069 | |
| 2070 | return ptr; |
| 2071 | } |
| 2072 | |
| 2073 | void jbd2_free(void *ptr, size_t size) |
| 2074 | { |
| 2075 | if (size == PAGE_SIZE) { |
| 2076 | free_pages((unsigned long)ptr, 0); |
| 2077 | return; |
| 2078 | } |
| 2079 | if (size > PAGE_SIZE) { |
| 2080 | int order = get_order(size); |
| 2081 | |
| 2082 | if (order < 3) |
| 2083 | free_pages((unsigned long)ptr, order); |
| 2084 | else |
| 2085 | vfree(ptr); |
| 2086 | return; |
| 2087 | } |
| 2088 | kmem_cache_free(get_slab(size), ptr); |
| 2089 | }; |
| 2090 | |
| 2091 | /* |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2092 | * Journal_head storage management |
| 2093 | */ |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 2094 | static struct kmem_cache *jbd2_journal_head_cache; |
Jose R. Santos | e23291b | 2007-07-18 08:57:06 -0400 | [diff] [blame] | 2095 | #ifdef CONFIG_JBD2_DEBUG |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2096 | static atomic_t nr_journal_heads = ATOMIC_INIT(0); |
| 2097 | #endif |
| 2098 | |
Yongqiang Yang | 4185a2a | 2012-02-20 17:53:03 -0500 | [diff] [blame] | 2099 | static int jbd2_journal_init_journal_head_cache(void) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2100 | { |
| 2101 | int retval; |
| 2102 | |
Al Viro | 1076d17 | 2008-03-29 03:07:18 +0000 | [diff] [blame] | 2103 | J_ASSERT(jbd2_journal_head_cache == NULL); |
Johann Lombardi | a920e94 | 2006-10-11 01:21:00 -0700 | [diff] [blame] | 2104 | jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head", |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2105 | sizeof(struct journal_head), |
| 2106 | 0, /* offset */ |
Mingming Cao | 7716095 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2107 | SLAB_TEMPORARY, /* flags */ |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2108 | NULL); /* ctor */ |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2109 | retval = 0; |
Al Viro | 1076d17 | 2008-03-29 03:07:18 +0000 | [diff] [blame] | 2110 | if (!jbd2_journal_head_cache) { |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2111 | retval = -ENOMEM; |
Eryu Guan | f2a4452 | 2011-11-01 19:09:18 -0400 | [diff] [blame] | 2112 | printk(KERN_EMERG "JBD2: no memory for journal_head cache\n"); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2113 | } |
| 2114 | return retval; |
| 2115 | } |
| 2116 | |
Yongqiang Yang | 4185a2a | 2012-02-20 17:53:03 -0500 | [diff] [blame] | 2117 | static void jbd2_journal_destroy_journal_head_cache(void) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2118 | { |
Duane Griffin | 8a9362e | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2119 | if (jbd2_journal_head_cache) { |
| 2120 | kmem_cache_destroy(jbd2_journal_head_cache); |
| 2121 | jbd2_journal_head_cache = NULL; |
| 2122 | } |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2123 | } |
| 2124 | |
| 2125 | /* |
| 2126 | * journal_head splicing and dicing |
| 2127 | */ |
| 2128 | static struct journal_head *journal_alloc_journal_head(void) |
| 2129 | { |
| 2130 | struct journal_head *ret; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2131 | |
Jose R. Santos | e23291b | 2007-07-18 08:57:06 -0400 | [diff] [blame] | 2132 | #ifdef CONFIG_JBD2_DEBUG |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2133 | atomic_inc(&nr_journal_heads); |
| 2134 | #endif |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 2135 | ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS); |
Al Viro | 1076d17 | 2008-03-29 03:07:18 +0000 | [diff] [blame] | 2136 | if (!ret) { |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2137 | jbd_debug(1, "out of memory for journal_head\n"); |
Theodore Ts'o | 670be5a | 2010-12-17 10:44:16 -0500 | [diff] [blame] | 2138 | pr_notice_ratelimited("ENOMEM in %s, retrying.\n", __func__); |
Al Viro | 1076d17 | 2008-03-29 03:07:18 +0000 | [diff] [blame] | 2139 | while (!ret) { |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2140 | yield(); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 2141 | ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2142 | } |
| 2143 | } |
| 2144 | return ret; |
| 2145 | } |
| 2146 | |
| 2147 | static void journal_free_journal_head(struct journal_head *jh) |
| 2148 | { |
Jose R. Santos | e23291b | 2007-07-18 08:57:06 -0400 | [diff] [blame] | 2149 | #ifdef CONFIG_JBD2_DEBUG |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2150 | atomic_dec(&nr_journal_heads); |
Mingming Cao | cd02ff0 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 2151 | memset(jh, JBD2_POISON_FREE, sizeof(*jh)); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2152 | #endif |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 2153 | kmem_cache_free(jbd2_journal_head_cache, jh); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2154 | } |
| 2155 | |
| 2156 | /* |
| 2157 | * A journal_head is attached to a buffer_head whenever JBD has an |
| 2158 | * interest in the buffer. |
| 2159 | * |
| 2160 | * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit |
| 2161 | * is set. This bit is tested in core kernel code where we need to take |
| 2162 | * JBD-specific actions. Testing the zeroness of ->b_private is not reliable |
| 2163 | * there. |
| 2164 | * |
| 2165 | * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one. |
| 2166 | * |
| 2167 | * When a buffer has its BH_JBD bit set it is immune from being released by |
| 2168 | * core kernel code, mainly via ->b_count. |
| 2169 | * |
Jan Kara | de1b794 | 2011-06-13 15:38:22 -0400 | [diff] [blame] | 2170 | * A journal_head is detached from its buffer_head when the journal_head's |
| 2171 | * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint |
| 2172 | * transaction (b_cp_transaction) hold their references to b_jcount. |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2173 | * |
| 2174 | * Various places in the kernel want to attach a journal_head to a buffer_head |
| 2175 | * _before_ attaching the journal_head to a transaction. To protect the |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 2176 | * journal_head in this situation, jbd2_journal_add_journal_head elevates the |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2177 | * journal_head's b_jcount refcount by one. The caller must call |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 2178 | * jbd2_journal_put_journal_head() to undo this. |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2179 | * |
| 2180 | * So the typical usage would be: |
| 2181 | * |
| 2182 | * (Attach a journal_head if needed. Increments b_jcount) |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 2183 | * struct journal_head *jh = jbd2_journal_add_journal_head(bh); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2184 | * ... |
Jan Kara | de1b794 | 2011-06-13 15:38:22 -0400 | [diff] [blame] | 2185 | * (Get another reference for transaction) |
| 2186 | * jbd2_journal_grab_journal_head(bh); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2187 | * jh->b_transaction = xxx; |
Jan Kara | de1b794 | 2011-06-13 15:38:22 -0400 | [diff] [blame] | 2188 | * (Put original reference) |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 2189 | * jbd2_journal_put_journal_head(jh); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2190 | */ |
| 2191 | |
| 2192 | /* |
| 2193 | * Give a buffer_head a journal_head. |
| 2194 | * |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2195 | * May sleep. |
| 2196 | */ |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 2197 | struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2198 | { |
| 2199 | struct journal_head *jh; |
| 2200 | struct journal_head *new_jh = NULL; |
| 2201 | |
| 2202 | repeat: |
| 2203 | if (!buffer_jbd(bh)) { |
| 2204 | new_jh = journal_alloc_journal_head(); |
| 2205 | memset(new_jh, 0, sizeof(*new_jh)); |
| 2206 | } |
| 2207 | |
| 2208 | jbd_lock_bh_journal_head(bh); |
| 2209 | if (buffer_jbd(bh)) { |
| 2210 | jh = bh2jh(bh); |
| 2211 | } else { |
| 2212 | J_ASSERT_BH(bh, |
| 2213 | (atomic_read(&bh->b_count) > 0) || |
| 2214 | (bh->b_page && bh->b_page->mapping)); |
| 2215 | |
| 2216 | if (!new_jh) { |
| 2217 | jbd_unlock_bh_journal_head(bh); |
| 2218 | goto repeat; |
| 2219 | } |
| 2220 | |
| 2221 | jh = new_jh; |
| 2222 | new_jh = NULL; /* We consumed it */ |
| 2223 | set_buffer_jbd(bh); |
| 2224 | bh->b_private = jh; |
| 2225 | jh->b_bh = bh; |
| 2226 | get_bh(bh); |
| 2227 | BUFFER_TRACE(bh, "added journal_head"); |
| 2228 | } |
| 2229 | jh->b_jcount++; |
| 2230 | jbd_unlock_bh_journal_head(bh); |
| 2231 | if (new_jh) |
| 2232 | journal_free_journal_head(new_jh); |
| 2233 | return bh->b_private; |
| 2234 | } |
| 2235 | |
| 2236 | /* |
| 2237 | * Grab a ref against this buffer_head's journal_head. If it ended up not |
| 2238 | * having a journal_head, return NULL |
| 2239 | */ |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 2240 | struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2241 | { |
| 2242 | struct journal_head *jh = NULL; |
| 2243 | |
| 2244 | jbd_lock_bh_journal_head(bh); |
| 2245 | if (buffer_jbd(bh)) { |
| 2246 | jh = bh2jh(bh); |
| 2247 | jh->b_jcount++; |
| 2248 | } |
| 2249 | jbd_unlock_bh_journal_head(bh); |
| 2250 | return jh; |
| 2251 | } |
| 2252 | |
| 2253 | static void __journal_remove_journal_head(struct buffer_head *bh) |
| 2254 | { |
| 2255 | struct journal_head *jh = bh2jh(bh); |
| 2256 | |
| 2257 | J_ASSERT_JH(jh, jh->b_jcount >= 0); |
Jan Kara | de1b794 | 2011-06-13 15:38:22 -0400 | [diff] [blame] | 2258 | J_ASSERT_JH(jh, jh->b_transaction == NULL); |
| 2259 | J_ASSERT_JH(jh, jh->b_next_transaction == NULL); |
| 2260 | J_ASSERT_JH(jh, jh->b_cp_transaction == NULL); |
| 2261 | J_ASSERT_JH(jh, jh->b_jlist == BJ_None); |
| 2262 | J_ASSERT_BH(bh, buffer_jbd(bh)); |
| 2263 | J_ASSERT_BH(bh, jh2bh(jh) == bh); |
| 2264 | BUFFER_TRACE(bh, "remove journal_head"); |
| 2265 | if (jh->b_frozen_data) { |
| 2266 | printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__); |
| 2267 | jbd2_free(jh->b_frozen_data, bh->b_size); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2268 | } |
Jan Kara | de1b794 | 2011-06-13 15:38:22 -0400 | [diff] [blame] | 2269 | if (jh->b_committed_data) { |
| 2270 | printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__); |
| 2271 | jbd2_free(jh->b_committed_data, bh->b_size); |
| 2272 | } |
| 2273 | bh->b_private = NULL; |
| 2274 | jh->b_bh = NULL; /* debug, really */ |
| 2275 | clear_buffer_jbd(bh); |
| 2276 | journal_free_journal_head(jh); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2277 | } |
| 2278 | |
| 2279 | /* |
Jan Kara | de1b794 | 2011-06-13 15:38:22 -0400 | [diff] [blame] | 2280 | * Drop a reference on the passed journal_head. If it fell to zero then |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2281 | * release the journal_head from the buffer_head. |
| 2282 | */ |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 2283 | void jbd2_journal_put_journal_head(struct journal_head *jh) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2284 | { |
| 2285 | struct buffer_head *bh = jh2bh(jh); |
| 2286 | |
| 2287 | jbd_lock_bh_journal_head(bh); |
| 2288 | J_ASSERT_JH(jh, jh->b_jcount > 0); |
| 2289 | --jh->b_jcount; |
Jan Kara | de1b794 | 2011-06-13 15:38:22 -0400 | [diff] [blame] | 2290 | if (!jh->b_jcount) { |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2291 | __journal_remove_journal_head(bh); |
Jan Kara | de1b794 | 2011-06-13 15:38:22 -0400 | [diff] [blame] | 2292 | jbd_unlock_bh_journal_head(bh); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2293 | __brelse(bh); |
Jan Kara | de1b794 | 2011-06-13 15:38:22 -0400 | [diff] [blame] | 2294 | } else |
| 2295 | jbd_unlock_bh_journal_head(bh); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2296 | } |
| 2297 | |
| 2298 | /* |
Jan Kara | c851ed5 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2299 | * Initialize jbd inode head |
| 2300 | */ |
| 2301 | void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode) |
| 2302 | { |
| 2303 | jinode->i_transaction = NULL; |
| 2304 | jinode->i_next_transaction = NULL; |
| 2305 | jinode->i_vfs_inode = inode; |
| 2306 | jinode->i_flags = 0; |
| 2307 | INIT_LIST_HEAD(&jinode->i_list); |
| 2308 | } |
| 2309 | |
| 2310 | /* |
| 2311 | * Function to be called before we start removing inode from memory (i.e., |
| 2312 | * clear_inode() is a fine place to be called from). It removes inode from |
| 2313 | * transaction's lists. |
| 2314 | */ |
| 2315 | void jbd2_journal_release_jbd_inode(journal_t *journal, |
| 2316 | struct jbd2_inode *jinode) |
| 2317 | { |
Jan Kara | c851ed5 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2318 | if (!journal) |
| 2319 | return; |
| 2320 | restart: |
| 2321 | spin_lock(&journal->j_list_lock); |
| 2322 | /* Is commit writing out inode - we have to wait */ |
Brian King | 39e3ac2 | 2010-10-27 21:25:12 -0400 | [diff] [blame] | 2323 | if (test_bit(__JI_COMMIT_RUNNING, &jinode->i_flags)) { |
Jan Kara | c851ed5 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2324 | wait_queue_head_t *wq; |
| 2325 | DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING); |
| 2326 | wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING); |
| 2327 | prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE); |
| 2328 | spin_unlock(&journal->j_list_lock); |
| 2329 | schedule(); |
| 2330 | finish_wait(wq, &wait.wait); |
| 2331 | goto restart; |
| 2332 | } |
| 2333 | |
Jan Kara | c851ed5 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2334 | if (jinode->i_transaction) { |
| 2335 | list_del(&jinode->i_list); |
| 2336 | jinode->i_transaction = NULL; |
| 2337 | } |
| 2338 | spin_unlock(&journal->j_list_lock); |
| 2339 | } |
| 2340 | |
| 2341 | /* |
Jose R. Santos | 0f49d5d | 2007-07-18 08:50:18 -0400 | [diff] [blame] | 2342 | * debugfs tunables |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2343 | */ |
Jose R. Santos | 6f38c74 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 2344 | #ifdef CONFIG_JBD2_DEBUG |
| 2345 | u8 jbd2_journal_enable_debug __read_mostly; |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 2346 | EXPORT_SYMBOL(jbd2_journal_enable_debug); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2347 | |
Jose R. Santos | 0f49d5d | 2007-07-18 08:50:18 -0400 | [diff] [blame] | 2348 | #define JBD2_DEBUG_NAME "jbd2-debug" |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2349 | |
Jose R. Santos | 6f38c74 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 2350 | static struct dentry *jbd2_debugfs_dir; |
| 2351 | static struct dentry *jbd2_debug; |
Jose R. Santos | 0f49d5d | 2007-07-18 08:50:18 -0400 | [diff] [blame] | 2352 | |
| 2353 | static void __init jbd2_create_debugfs_entry(void) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2354 | { |
Jose R. Santos | 0f49d5d | 2007-07-18 08:50:18 -0400 | [diff] [blame] | 2355 | jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL); |
| 2356 | if (jbd2_debugfs_dir) |
Yin Kangkai | 765f836 | 2009-12-15 14:48:25 -0800 | [diff] [blame] | 2357 | jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME, |
| 2358 | S_IRUGO | S_IWUSR, |
Jose R. Santos | 0f49d5d | 2007-07-18 08:50:18 -0400 | [diff] [blame] | 2359 | jbd2_debugfs_dir, |
| 2360 | &jbd2_journal_enable_debug); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2361 | } |
| 2362 | |
Jose R. Santos | 0f49d5d | 2007-07-18 08:50:18 -0400 | [diff] [blame] | 2363 | static void __exit jbd2_remove_debugfs_entry(void) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2364 | { |
Jose R. Santos | 6f38c74 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 2365 | debugfs_remove(jbd2_debug); |
| 2366 | debugfs_remove(jbd2_debugfs_dir); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2367 | } |
| 2368 | |
| 2369 | #else |
| 2370 | |
Jose R. Santos | 0f49d5d | 2007-07-18 08:50:18 -0400 | [diff] [blame] | 2371 | static void __init jbd2_create_debugfs_entry(void) |
| 2372 | { |
Jose R. Santos | 0f49d5d | 2007-07-18 08:50:18 -0400 | [diff] [blame] | 2373 | } |
| 2374 | |
| 2375 | static void __exit jbd2_remove_debugfs_entry(void) |
| 2376 | { |
Jose R. Santos | 0f49d5d | 2007-07-18 08:50:18 -0400 | [diff] [blame] | 2377 | } |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2378 | |
| 2379 | #endif |
| 2380 | |
Johann Lombardi | 8e85fb3 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2381 | #ifdef CONFIG_PROC_FS |
| 2382 | |
| 2383 | #define JBD2_STATS_PROC_NAME "fs/jbd2" |
| 2384 | |
| 2385 | static void __init jbd2_create_jbd_stats_proc_entry(void) |
| 2386 | { |
| 2387 | proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL); |
| 2388 | } |
| 2389 | |
| 2390 | static void __exit jbd2_remove_jbd_stats_proc_entry(void) |
| 2391 | { |
| 2392 | if (proc_jbd2_stats) |
| 2393 | remove_proc_entry(JBD2_STATS_PROC_NAME, NULL); |
| 2394 | } |
| 2395 | |
| 2396 | #else |
| 2397 | |
| 2398 | #define jbd2_create_jbd_stats_proc_entry() do {} while (0) |
| 2399 | #define jbd2_remove_jbd_stats_proc_entry() do {} while (0) |
| 2400 | |
| 2401 | #endif |
| 2402 | |
Theodore Ts'o | 8aefcd5 | 2011-01-10 12:29:43 -0500 | [diff] [blame] | 2403 | struct kmem_cache *jbd2_handle_cache, *jbd2_inode_cache; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2404 | |
Yongqiang Yang | 4185a2a | 2012-02-20 17:53:03 -0500 | [diff] [blame] | 2405 | static int __init jbd2_journal_init_handle_cache(void) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2406 | { |
Theodore Ts'o | 8aefcd5 | 2011-01-10 12:29:43 -0500 | [diff] [blame] | 2407 | jbd2_handle_cache = KMEM_CACHE(jbd2_journal_handle, SLAB_TEMPORARY); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 2408 | if (jbd2_handle_cache == NULL) { |
Theodore Ts'o | 8aefcd5 | 2011-01-10 12:29:43 -0500 | [diff] [blame] | 2409 | printk(KERN_EMERG "JBD2: failed to create handle cache\n"); |
| 2410 | return -ENOMEM; |
| 2411 | } |
| 2412 | jbd2_inode_cache = KMEM_CACHE(jbd2_inode, 0); |
| 2413 | if (jbd2_inode_cache == NULL) { |
| 2414 | printk(KERN_EMERG "JBD2: failed to create inode cache\n"); |
| 2415 | kmem_cache_destroy(jbd2_handle_cache); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2416 | return -ENOMEM; |
| 2417 | } |
| 2418 | return 0; |
| 2419 | } |
| 2420 | |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 2421 | static void jbd2_journal_destroy_handle_cache(void) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2422 | { |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 2423 | if (jbd2_handle_cache) |
| 2424 | kmem_cache_destroy(jbd2_handle_cache); |
Theodore Ts'o | 8aefcd5 | 2011-01-10 12:29:43 -0500 | [diff] [blame] | 2425 | if (jbd2_inode_cache) |
| 2426 | kmem_cache_destroy(jbd2_inode_cache); |
| 2427 | |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2428 | } |
| 2429 | |
| 2430 | /* |
| 2431 | * Module startup and shutdown |
| 2432 | */ |
| 2433 | |
| 2434 | static int __init journal_init_caches(void) |
| 2435 | { |
| 2436 | int ret; |
| 2437 | |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 2438 | ret = jbd2_journal_init_revoke_caches(); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2439 | if (ret == 0) |
Yongqiang Yang | 4185a2a | 2012-02-20 17:53:03 -0500 | [diff] [blame] | 2440 | ret = jbd2_journal_init_journal_head_cache(); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2441 | if (ret == 0) |
Yongqiang Yang | 4185a2a | 2012-02-20 17:53:03 -0500 | [diff] [blame] | 2442 | ret = jbd2_journal_init_handle_cache(); |
Yongqiang Yang | 0c2022e | 2012-02-20 17:53:02 -0500 | [diff] [blame] | 2443 | if (ret == 0) |
| 2444 | ret = jbd2_journal_init_transaction_cache(); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2445 | return ret; |
| 2446 | } |
| 2447 | |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 2448 | static void jbd2_journal_destroy_caches(void) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2449 | { |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 2450 | jbd2_journal_destroy_revoke_caches(); |
Yongqiang Yang | 4185a2a | 2012-02-20 17:53:03 -0500 | [diff] [blame] | 2451 | jbd2_journal_destroy_journal_head_cache(); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 2452 | jbd2_journal_destroy_handle_cache(); |
Yongqiang Yang | 0c2022e | 2012-02-20 17:53:02 -0500 | [diff] [blame] | 2453 | jbd2_journal_destroy_transaction_cache(); |
Theodore Ts'o | d2eecb0 | 2009-12-07 10:36:20 -0500 | [diff] [blame] | 2454 | jbd2_journal_destroy_slabs(); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2455 | } |
| 2456 | |
| 2457 | static int __init journal_init(void) |
| 2458 | { |
| 2459 | int ret; |
| 2460 | |
| 2461 | BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024); |
| 2462 | |
| 2463 | ret = journal_init_caches(); |
Duane Griffin | 620de4e | 2008-04-29 22:02:47 -0400 | [diff] [blame] | 2464 | if (ret == 0) { |
| 2465 | jbd2_create_debugfs_entry(); |
| 2466 | jbd2_create_jbd_stats_proc_entry(); |
| 2467 | } else { |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 2468 | jbd2_journal_destroy_caches(); |
Duane Griffin | 620de4e | 2008-04-29 22:02:47 -0400 | [diff] [blame] | 2469 | } |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2470 | return ret; |
| 2471 | } |
| 2472 | |
| 2473 | static void __exit journal_exit(void) |
| 2474 | { |
Jose R. Santos | e23291b | 2007-07-18 08:57:06 -0400 | [diff] [blame] | 2475 | #ifdef CONFIG_JBD2_DEBUG |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2476 | int n = atomic_read(&nr_journal_heads); |
| 2477 | if (n) |
Eryu Guan | f2a4452 | 2011-11-01 19:09:18 -0400 | [diff] [blame] | 2478 | printk(KERN_EMERG "JBD2: leaked %d journal_heads!\n", n); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2479 | #endif |
Jose R. Santos | 0f49d5d | 2007-07-18 08:50:18 -0400 | [diff] [blame] | 2480 | jbd2_remove_debugfs_entry(); |
Johann Lombardi | 8e85fb3 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2481 | jbd2_remove_jbd_stats_proc_entry(); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 2482 | jbd2_journal_destroy_caches(); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2483 | } |
| 2484 | |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 2485 | MODULE_LICENSE("GPL"); |
| 2486 | module_init(journal_init); |
| 2487 | module_exit(journal_exit); |
| 2488 | |