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