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