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