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