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