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