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