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