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