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