Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Uwe Kleine-König | 5886269 | 2007-05-09 07:51:49 +0200 | [diff] [blame] | 2 | * linux/fs/jbd/checkpoint.c |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 3 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * Written by Stephen C. Tweedie <sct@redhat.com>, 1999 |
| 5 | * |
| 6 | * Copyright 1999 Red Hat Software --- 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 | * |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 12 | * Checkpoint routines for the generic filesystem journaling code. |
| 13 | * Part of the ext2fs journaling system. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * |
| 15 | * Checkpointing is the process of ensuring that a section of the log is |
| 16 | * committed fully to disk, so that that portion of the log can be |
| 17 | * reused. |
| 18 | */ |
| 19 | |
| 20 | #include <linux/time.h> |
| 21 | #include <linux/fs.h> |
| 22 | #include <linux/jbd.h> |
| 23 | #include <linux/errno.h> |
| 24 | #include <linux/slab.h> |
Tao Ma | a212d1a | 2011-06-07 11:56:50 +0800 | [diff] [blame] | 25 | #include <linux/blkdev.h> |
Lukas Czerner | 99cb1a3 | 2011-05-23 18:33:02 +0200 | [diff] [blame] | 26 | #include <trace/events/jbd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 28 | /* |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 29 | * Unlink a buffer from a transaction checkpoint list. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | * |
| 31 | * Called with j_list_lock held. |
| 32 | */ |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 33 | static inline void __buffer_unlink_first(struct journal_head *jh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | { |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 35 | transaction_t *transaction = jh->b_cp_transaction; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Mark Fasheh | 7c8903f | 2006-02-14 13:53:03 -0800 | [diff] [blame] | 37 | jh->b_cpnext->b_cpprev = jh->b_cpprev; |
| 38 | jh->b_cpprev->b_cpnext = jh->b_cpnext; |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 39 | if (transaction->t_checkpoint_list == jh) { |
Mark Fasheh | 7c8903f | 2006-02-14 13:53:03 -0800 | [diff] [blame] | 40 | transaction->t_checkpoint_list = jh->b_cpnext; |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 41 | if (transaction->t_checkpoint_list == jh) |
| 42 | transaction->t_checkpoint_list = NULL; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /* |
| 47 | * Unlink a buffer from a transaction checkpoint(io) list. |
| 48 | * |
| 49 | * Called with j_list_lock held. |
| 50 | */ |
| 51 | static inline void __buffer_unlink(struct journal_head *jh) |
| 52 | { |
| 53 | transaction_t *transaction = jh->b_cp_transaction; |
| 54 | |
| 55 | __buffer_unlink_first(jh); |
| 56 | if (transaction->t_checkpoint_io_list == jh) { |
| 57 | transaction->t_checkpoint_io_list = jh->b_cpnext; |
| 58 | if (transaction->t_checkpoint_io_list == jh) |
| 59 | transaction->t_checkpoint_io_list = NULL; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | * Move a buffer from the checkpoint list to the checkpoint io list |
| 65 | * |
| 66 | * Called with j_list_lock held |
| 67 | */ |
| 68 | static inline void __buffer_relink_io(struct journal_head *jh) |
| 69 | { |
| 70 | transaction_t *transaction = jh->b_cp_transaction; |
| 71 | |
| 72 | __buffer_unlink_first(jh); |
| 73 | |
| 74 | if (!transaction->t_checkpoint_io_list) { |
| 75 | jh->b_cpnext = jh->b_cpprev = jh; |
| 76 | } else { |
| 77 | jh->b_cpnext = transaction->t_checkpoint_io_list; |
| 78 | jh->b_cpprev = transaction->t_checkpoint_io_list->b_cpprev; |
| 79 | jh->b_cpprev->b_cpnext = jh; |
| 80 | jh->b_cpnext->b_cpprev = jh; |
| 81 | } |
| 82 | transaction->t_checkpoint_io_list = jh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /* |
| 86 | * Try to release a checkpointed buffer from its transaction. |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 87 | * Returns 1 if we released it and 2 if we also released the |
| 88 | * whole transaction. |
| 89 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | * Requires j_list_lock |
| 91 | * Called under jbd_lock_bh_state(jh2bh(jh)), and drops it |
| 92 | */ |
| 93 | static int __try_to_free_cp_buf(struct journal_head *jh) |
| 94 | { |
| 95 | int ret = 0; |
| 96 | struct buffer_head *bh = jh2bh(jh); |
| 97 | |
Hidehiro Kawai | 4afe978 | 2008-10-22 14:15:00 -0700 | [diff] [blame] | 98 | if (jh->b_jlist == BJ_None && !buffer_locked(bh) && |
Hidehiro Kawai | 9f818b4 | 2008-10-22 14:15:02 -0700 | [diff] [blame] | 99 | !buffer_dirty(bh) && !buffer_write_io_error(bh)) { |
Jan Kara | bb18924 | 2011-06-24 23:11:59 +0200 | [diff] [blame] | 100 | /* |
| 101 | * Get our reference so that bh cannot be freed before |
| 102 | * we unlock it |
| 103 | */ |
| 104 | get_bh(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | JBUFFER_TRACE(jh, "remove from checkpoint list"); |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 106 | ret = __journal_remove_checkpoint(jh) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | jbd_unlock_bh_state(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | BUFFER_TRACE(bh, "release"); |
| 109 | __brelse(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } else { |
| 111 | jbd_unlock_bh_state(bh); |
| 112 | } |
| 113 | return ret; |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | * __log_wait_for_space: wait until there is space in the journal. |
| 118 | * |
| 119 | * Called under j-state_lock *only*. It will be unlocked if we have to wait |
| 120 | * for a checkpoint to free up some space in the log. |
| 121 | */ |
| 122 | void __log_wait_for_space(journal_t *journal) |
| 123 | { |
Theodore Ts'o | e219cca | 2008-11-06 22:37:59 -0500 | [diff] [blame] | 124 | int nblocks, space_left; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | assert_spin_locked(&journal->j_state_lock); |
| 126 | |
| 127 | nblocks = jbd_space_needed(journal); |
| 128 | while (__log_space_left(journal) < nblocks) { |
| 129 | if (journal->j_flags & JFS_ABORT) |
| 130 | return; |
| 131 | spin_unlock(&journal->j_state_lock); |
Arjan van de Ven | 2c68ee7 | 2006-03-23 03:00:35 -0800 | [diff] [blame] | 132 | mutex_lock(&journal->j_checkpoint_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
| 134 | /* |
| 135 | * Test again, another process may have checkpointed while we |
Duane Griffin | be07c4e | 2008-10-22 14:15:03 -0700 | [diff] [blame] | 136 | * were waiting for the checkpoint lock. If there are no |
Theodore Ts'o | e219cca | 2008-11-06 22:37:59 -0500 | [diff] [blame] | 137 | * transactions ready to be checkpointed, try to recover |
| 138 | * journal space by calling cleanup_journal_tail(), and if |
| 139 | * that doesn't work, by waiting for the currently committing |
| 140 | * transaction to complete. If there is absolutely no way |
| 141 | * to make progress, this is either a BUG or corrupted |
| 142 | * filesystem, so abort the journal and leave a stack |
| 143 | * trace for forensic evidence. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | */ |
| 145 | spin_lock(&journal->j_state_lock); |
Duane Griffin | be07c4e | 2008-10-22 14:15:03 -0700 | [diff] [blame] | 146 | spin_lock(&journal->j_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | nblocks = jbd_space_needed(journal); |
Theodore Ts'o | e219cca | 2008-11-06 22:37:59 -0500 | [diff] [blame] | 148 | space_left = __log_space_left(journal); |
| 149 | if (space_left < nblocks) { |
Duane Griffin | be07c4e | 2008-10-22 14:15:03 -0700 | [diff] [blame] | 150 | int chkpt = journal->j_checkpoint_transactions != NULL; |
Theodore Ts'o | e219cca | 2008-11-06 22:37:59 -0500 | [diff] [blame] | 151 | tid_t tid = 0; |
Duane Griffin | be07c4e | 2008-10-22 14:15:03 -0700 | [diff] [blame] | 152 | |
Theodore Ts'o | e219cca | 2008-11-06 22:37:59 -0500 | [diff] [blame] | 153 | if (journal->j_committing_transaction) |
| 154 | tid = journal->j_committing_transaction->t_tid; |
Duane Griffin | be07c4e | 2008-10-22 14:15:03 -0700 | [diff] [blame] | 155 | spin_unlock(&journal->j_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | spin_unlock(&journal->j_state_lock); |
Duane Griffin | be07c4e | 2008-10-22 14:15:03 -0700 | [diff] [blame] | 157 | if (chkpt) { |
| 158 | log_do_checkpoint(journal); |
Theodore Ts'o | e219cca | 2008-11-06 22:37:59 -0500 | [diff] [blame] | 159 | } else if (cleanup_journal_tail(journal) == 0) { |
| 160 | /* We were able to recover space; yay! */ |
| 161 | ; |
| 162 | } else if (tid) { |
| 163 | log_wait_commit(journal, tid); |
Duane Griffin | be07c4e | 2008-10-22 14:15:03 -0700 | [diff] [blame] | 164 | } else { |
Theodore Ts'o | e219cca | 2008-11-06 22:37:59 -0500 | [diff] [blame] | 165 | printk(KERN_ERR "%s: needed %d blocks and " |
| 166 | "only had %d space available\n", |
| 167 | __func__, nblocks, space_left); |
| 168 | printk(KERN_ERR "%s: no way to get more " |
| 169 | "journal space\n", __func__); |
| 170 | WARN_ON(1); |
Duane Griffin | be07c4e | 2008-10-22 14:15:03 -0700 | [diff] [blame] | 171 | journal_abort(journal, 0); |
| 172 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | spin_lock(&journal->j_state_lock); |
Duane Griffin | be07c4e | 2008-10-22 14:15:03 -0700 | [diff] [blame] | 174 | } else { |
| 175 | spin_unlock(&journal->j_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | } |
Arjan van de Ven | 2c68ee7 | 2006-03-23 03:00:35 -0800 | [diff] [blame] | 177 | mutex_unlock(&journal->j_checkpoint_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * We were unable to perform jbd_trylock_bh_state() inside j_list_lock. |
| 183 | * The caller must restart a list walk. Wait for someone else to run |
| 184 | * jbd_unlock_bh_state(). |
| 185 | */ |
| 186 | static void jbd_sync_bh(journal_t *journal, struct buffer_head *bh) |
Josh Triplett | e7ab8d6 | 2006-09-27 01:49:26 -0700 | [diff] [blame] | 187 | __releases(journal->j_list_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | { |
| 189 | get_bh(bh); |
| 190 | spin_unlock(&journal->j_list_lock); |
| 191 | jbd_lock_bh_state(bh); |
| 192 | jbd_unlock_bh_state(bh); |
| 193 | put_bh(bh); |
| 194 | } |
| 195 | |
| 196 | /* |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 197 | * Clean up transaction's list of buffers submitted for io. |
| 198 | * We wait for any pending IO to complete and remove any clean |
| 199 | * buffers. Note that we take the buffers in the opposite ordering |
| 200 | * from the one in which they were submitted for IO. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | * |
Hidehiro Kawai | 4afe978 | 2008-10-22 14:15:00 -0700 | [diff] [blame] | 202 | * Return 0 on success, and return <0 if some buffers have failed |
| 203 | * to be written out. |
| 204 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | * Called with j_list_lock held. |
| 206 | */ |
Hidehiro Kawai | 4afe978 | 2008-10-22 14:15:00 -0700 | [diff] [blame] | 207 | static int __wait_cp_io(journal_t *journal, transaction_t *transaction) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | { |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 209 | struct journal_head *jh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | struct buffer_head *bh; |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 211 | tid_t this_tid; |
| 212 | int released = 0; |
Hidehiro Kawai | 4afe978 | 2008-10-22 14:15:00 -0700 | [diff] [blame] | 213 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 215 | this_tid = transaction->t_tid; |
| 216 | restart: |
| 217 | /* Did somebody clean up the transaction in the meanwhile? */ |
| 218 | if (journal->j_checkpoint_transactions != transaction || |
| 219 | transaction->t_tid != this_tid) |
Hidehiro Kawai | 4afe978 | 2008-10-22 14:15:00 -0700 | [diff] [blame] | 220 | return ret; |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 221 | while (!released && transaction->t_checkpoint_io_list) { |
| 222 | jh = transaction->t_checkpoint_io_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | bh = jh2bh(jh); |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 224 | if (!jbd_trylock_bh_state(bh)) { |
| 225 | jbd_sync_bh(journal, bh); |
| 226 | spin_lock(&journal->j_list_lock); |
| 227 | goto restart; |
| 228 | } |
Jan Kara | bb18924 | 2011-06-24 23:11:59 +0200 | [diff] [blame] | 229 | get_bh(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | if (buffer_locked(bh)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | spin_unlock(&journal->j_list_lock); |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 232 | jbd_unlock_bh_state(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | wait_on_buffer(bh); |
| 234 | /* the journal_head may have gone by now */ |
| 235 | BUFFER_TRACE(bh, "brelse"); |
| 236 | __brelse(bh); |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 237 | spin_lock(&journal->j_list_lock); |
| 238 | goto restart; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } |
Hidehiro Kawai | 9f818b4 | 2008-10-22 14:15:02 -0700 | [diff] [blame] | 240 | if (unlikely(buffer_write_io_error(bh))) |
Hidehiro Kawai | 4afe978 | 2008-10-22 14:15:00 -0700 | [diff] [blame] | 241 | ret = -EIO; |
| 242 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | /* |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 244 | * Now in whatever state the buffer currently is, we know that |
| 245 | * it has been written out and so we can drop it from the list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | */ |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 247 | released = __journal_remove_checkpoint(jh); |
| 248 | jbd_unlock_bh_state(bh); |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 249 | __brelse(bh); |
| 250 | } |
Hidehiro Kawai | 4afe978 | 2008-10-22 14:15:00 -0700 | [diff] [blame] | 251 | |
| 252 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | #define NR_BATCH 64 |
| 256 | |
| 257 | static void |
| 258 | __flush_batch(journal_t *journal, struct buffer_head **bhs, int *batch_count) |
| 259 | { |
| 260 | int i; |
Tao Ma | a212d1a | 2011-06-07 11:56:50 +0800 | [diff] [blame] | 261 | struct blk_plug plug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
Tao Ma | a212d1a | 2011-06-07 11:56:50 +0800 | [diff] [blame] | 263 | blk_start_plug(&plug); |
Christoph Hellwig | 9cb569d | 2010-08-11 17:06:24 +0200 | [diff] [blame] | 264 | for (i = 0; i < *batch_count; i++) |
Tao Ma | a212d1a | 2011-06-07 11:56:50 +0800 | [diff] [blame] | 265 | write_dirty_buffer(bhs[i], WRITE_SYNC); |
| 266 | blk_finish_plug(&plug); |
Christoph Hellwig | 9cb569d | 2010-08-11 17:06:24 +0200 | [diff] [blame] | 267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | for (i = 0; i < *batch_count; i++) { |
| 269 | struct buffer_head *bh = bhs[i]; |
| 270 | clear_buffer_jwrite(bh); |
| 271 | BUFFER_TRACE(bh, "brelse"); |
| 272 | __brelse(bh); |
| 273 | } |
| 274 | *batch_count = 0; |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | * Try to flush one buffer from the checkpoint list to disk. |
| 279 | * |
| 280 | * Return 1 if something happened which requires us to abort the current |
Hidehiro Kawai | 4afe978 | 2008-10-22 14:15:00 -0700 | [diff] [blame] | 281 | * scan of the checkpoint list. Return <0 if the buffer has failed to |
| 282 | * be written out. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | * |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 284 | * Called with j_list_lock held and drops it if 1 is returned |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | * Called under jbd_lock_bh_state(jh2bh(jh)), and drops it |
| 286 | */ |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 287 | static int __process_buffer(journal_t *journal, struct journal_head *jh, |
| 288 | struct buffer_head **bhs, int *batch_count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | { |
| 290 | struct buffer_head *bh = jh2bh(jh); |
| 291 | int ret = 0; |
| 292 | |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 293 | if (buffer_locked(bh)) { |
Namhyung Kim | e4d5e3a | 2010-10-16 17:11:02 +0900 | [diff] [blame] | 294 | get_bh(bh); |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 295 | spin_unlock(&journal->j_list_lock); |
| 296 | jbd_unlock_bh_state(bh); |
| 297 | wait_on_buffer(bh); |
| 298 | /* the journal_head may have gone by now */ |
| 299 | BUFFER_TRACE(bh, "brelse"); |
| 300 | __brelse(bh); |
| 301 | ret = 1; |
| 302 | } else if (jh->b_transaction != NULL) { |
| 303 | transaction_t *t = jh->b_transaction; |
| 304 | tid_t tid = t->t_tid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 306 | spin_unlock(&journal->j_list_lock); |
| 307 | jbd_unlock_bh_state(bh); |
| 308 | log_start_commit(journal, tid); |
| 309 | log_wait_commit(journal, tid); |
| 310 | ret = 1; |
| 311 | } else if (!buffer_dirty(bh)) { |
Hidehiro Kawai | 4afe978 | 2008-10-22 14:15:00 -0700 | [diff] [blame] | 312 | ret = 1; |
Hidehiro Kawai | 9f818b4 | 2008-10-22 14:15:02 -0700 | [diff] [blame] | 313 | if (unlikely(buffer_write_io_error(bh))) |
Hidehiro Kawai | 4afe978 | 2008-10-22 14:15:00 -0700 | [diff] [blame] | 314 | ret = -EIO; |
Jan Kara | bb18924 | 2011-06-24 23:11:59 +0200 | [diff] [blame] | 315 | get_bh(bh); |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 316 | J_ASSERT_JH(jh, !buffer_jbddirty(bh)); |
| 317 | BUFFER_TRACE(bh, "remove from checkpoint"); |
| 318 | __journal_remove_checkpoint(jh); |
| 319 | spin_unlock(&journal->j_list_lock); |
| 320 | jbd_unlock_bh_state(bh); |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 321 | __brelse(bh); |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 322 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | /* |
| 324 | * Important: we are about to write the buffer, and |
| 325 | * possibly block, while still holding the journal lock. |
| 326 | * We cannot afford to let the transaction logic start |
| 327 | * messing around with this buffer before we write it to |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 328 | * disk, as that would break recoverability. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | */ |
| 330 | BUFFER_TRACE(bh, "queue"); |
| 331 | get_bh(bh); |
| 332 | J_ASSERT_BH(bh, !buffer_jwrite(bh)); |
| 333 | set_buffer_jwrite(bh); |
| 334 | bhs[*batch_count] = bh; |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 335 | __buffer_relink_io(jh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | jbd_unlock_bh_state(bh); |
| 337 | (*batch_count)++; |
| 338 | if (*batch_count == NR_BATCH) { |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 339 | spin_unlock(&journal->j_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | __flush_batch(journal, bhs, batch_count); |
| 341 | ret = 1; |
| 342 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | } |
| 344 | return ret; |
| 345 | } |
| 346 | |
| 347 | /* |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 348 | * Perform an actual checkpoint. We take the first transaction on the |
| 349 | * list of transactions to be checkpointed and send all its buffers |
| 350 | * to disk. We submit larger chunks of data at once. |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 351 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | * The journal should be locked before calling this function. |
Hidehiro Kawai | 4afe978 | 2008-10-22 14:15:00 -0700 | [diff] [blame] | 353 | * Called with j_checkpoint_mutex held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | */ |
| 355 | int log_do_checkpoint(journal_t *journal) |
| 356 | { |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 357 | transaction_t *transaction; |
| 358 | tid_t this_tid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | int result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | |
| 361 | jbd_debug(1, "Start checkpoint\n"); |
| 362 | |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 363 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | * First thing: if there are any transactions in the log which |
| 365 | * don't need checkpointing, just eliminate them from the |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 366 | * journal straight away. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | */ |
| 368 | result = cleanup_journal_tail(journal); |
Lukas Czerner | 99cb1a3 | 2011-05-23 18:33:02 +0200 | [diff] [blame] | 369 | trace_jbd_checkpoint(journal, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | jbd_debug(1, "cleanup_journal_tail returned %d\n", result); |
| 371 | if (result <= 0) |
| 372 | return result; |
| 373 | |
| 374 | /* |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 375 | * OK, we need to start writing disk blocks. Take one transaction |
| 376 | * and write it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | */ |
Hidehiro Kawai | 4afe978 | 2008-10-22 14:15:00 -0700 | [diff] [blame] | 378 | result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | spin_lock(&journal->j_list_lock); |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 380 | if (!journal->j_checkpoint_transactions) |
| 381 | goto out; |
| 382 | transaction = journal->j_checkpoint_transactions; |
| 383 | this_tid = transaction->t_tid; |
| 384 | restart: |
| 385 | /* |
| 386 | * If someone cleaned up this transaction while we slept, we're |
| 387 | * done (maybe it's a new transaction, but it fell at the same |
| 388 | * address). |
| 389 | */ |
| 390 | if (journal->j_checkpoint_transactions == transaction && |
| 391 | transaction->t_tid == this_tid) { |
| 392 | int batch_count = 0; |
| 393 | struct buffer_head *bhs[NR_BATCH]; |
| 394 | struct journal_head *jh; |
Hidehiro Kawai | 4afe978 | 2008-10-22 14:15:00 -0700 | [diff] [blame] | 395 | int retry = 0, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 397 | while (!retry && transaction->t_checkpoint_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | struct buffer_head *bh; |
| 399 | |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 400 | jh = transaction->t_checkpoint_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | bh = jh2bh(jh); |
| 402 | if (!jbd_trylock_bh_state(bh)) { |
| 403 | jbd_sync_bh(journal, bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | retry = 1; |
| 405 | break; |
| 406 | } |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 407 | retry = __process_buffer(journal, jh, bhs,&batch_count); |
Hidehiro Kawai | 4afe978 | 2008-10-22 14:15:00 -0700 | [diff] [blame] | 408 | if (retry < 0 && !result) |
| 409 | result = retry; |
Nick Piggin | 95c354f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 410 | if (!retry && (need_resched() || |
| 411 | spin_needbreak(&journal->j_list_lock))) { |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 412 | spin_unlock(&journal->j_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | retry = 1; |
| 414 | break; |
| 415 | } |
Jan Kara | f93ea41 | 2006-01-06 00:19:55 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 418 | if (batch_count) { |
| 419 | if (!retry) { |
| 420 | spin_unlock(&journal->j_list_lock); |
| 421 | retry = 1; |
| 422 | } |
| 423 | __flush_batch(journal, bhs, &batch_count); |
| 424 | } |
| 425 | |
| 426 | if (retry) { |
| 427 | spin_lock(&journal->j_list_lock); |
| 428 | goto restart; |
| 429 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | /* |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 431 | * Now we have cleaned up the first transaction's checkpoint |
| 432 | * list. Let's clean up the second one |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | */ |
Hidehiro Kawai | 4afe978 | 2008-10-22 14:15:00 -0700 | [diff] [blame] | 434 | err = __wait_cp_io(journal, transaction); |
| 435 | if (!result) |
| 436 | result = err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | } |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 438 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | spin_unlock(&journal->j_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | if (result < 0) |
Hidehiro Kawai | 4afe978 | 2008-10-22 14:15:00 -0700 | [diff] [blame] | 441 | journal_abort(journal, result); |
| 442 | else |
| 443 | result = cleanup_journal_tail(journal); |
| 444 | |
| 445 | return (result < 0) ? result : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | /* |
| 449 | * Check the list of checkpoint transactions for the journal to see if |
| 450 | * we have already got rid of any since the last update of the log tail |
| 451 | * in the journal superblock. If so, we can instantly roll the |
| 452 | * superblock forward to remove those transactions from the log. |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 453 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | * Return <0 on error, 0 on success, 1 if there was nothing to clean up. |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 455 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | * Called with the journal lock held. |
| 457 | * |
| 458 | * This is the only part of the journaling code which really needs to be |
| 459 | * aware of transaction aborts. Checkpointing involves writing to the |
| 460 | * main filesystem area rather than to the journal, so it can proceed |
Hidehiro Kawai | 4afe978 | 2008-10-22 14:15:00 -0700 | [diff] [blame] | 461 | * even in abort state, but we must not update the super block if |
| 462 | * checkpointing may have failed. Otherwise, we would lose some metadata |
| 463 | * buffers which should be written-back to the filesystem. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | */ |
| 465 | |
| 466 | int cleanup_journal_tail(journal_t *journal) |
| 467 | { |
| 468 | transaction_t * transaction; |
| 469 | tid_t first_tid; |
Jan Kara | 9c28cbc | 2009-08-03 19:21:00 +0200 | [diff] [blame] | 470 | unsigned int blocknr, freed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | |
Hidehiro Kawai | 4afe978 | 2008-10-22 14:15:00 -0700 | [diff] [blame] | 472 | if (is_journal_aborted(journal)) |
| 473 | return 1; |
| 474 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | /* OK, work out the oldest transaction remaining in the log, and |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 476 | * the log block it starts at. |
| 477 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | * If the log is now empty, we need to work out which is the |
| 479 | * next transaction ID we will write, and where it will |
| 480 | * start. */ |
| 481 | |
| 482 | spin_lock(&journal->j_state_lock); |
| 483 | spin_lock(&journal->j_list_lock); |
| 484 | transaction = journal->j_checkpoint_transactions; |
| 485 | if (transaction) { |
| 486 | first_tid = transaction->t_tid; |
| 487 | blocknr = transaction->t_log_start; |
| 488 | } else if ((transaction = journal->j_committing_transaction) != NULL) { |
| 489 | first_tid = transaction->t_tid; |
| 490 | blocknr = transaction->t_log_start; |
| 491 | } else if ((transaction = journal->j_running_transaction) != NULL) { |
| 492 | first_tid = transaction->t_tid; |
| 493 | blocknr = journal->j_head; |
| 494 | } else { |
| 495 | first_tid = journal->j_transaction_sequence; |
| 496 | blocknr = journal->j_head; |
| 497 | } |
| 498 | spin_unlock(&journal->j_list_lock); |
| 499 | J_ASSERT(blocknr != 0); |
| 500 | |
| 501 | /* If the oldest pinned transaction is at the tail of the log |
| 502 | already then there's not much we can do right now. */ |
| 503 | if (journal->j_tail_sequence == first_tid) { |
| 504 | spin_unlock(&journal->j_state_lock); |
| 505 | return 1; |
| 506 | } |
| 507 | |
| 508 | /* OK, update the superblock to recover the freed space. |
| 509 | * Physical blocks come first: have we wrapped beyond the end of |
| 510 | * the log? */ |
| 511 | freed = blocknr - journal->j_tail; |
| 512 | if (blocknr < journal->j_tail) |
| 513 | freed = freed + journal->j_last - journal->j_first; |
| 514 | |
Lukas Czerner | 99cb1a3 | 2011-05-23 18:33:02 +0200 | [diff] [blame] | 515 | trace_jbd_cleanup_journal_tail(journal, first_tid, blocknr, freed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | jbd_debug(1, |
Jan Kara | 9c28cbc | 2009-08-03 19:21:00 +0200 | [diff] [blame] | 517 | "Cleaning journal tail from %d to %d (offset %u), " |
| 518 | "freeing %u\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | journal->j_tail_sequence, first_tid, blocknr, freed); |
| 520 | |
| 521 | journal->j_free += freed; |
| 522 | journal->j_tail_sequence = first_tid; |
| 523 | journal->j_tail = blocknr; |
| 524 | spin_unlock(&journal->j_state_lock); |
| 525 | if (!(journal->j_flags & JFS_ABORT)) |
| 526 | journal_update_superblock(journal, 1); |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | |
| 531 | /* Checkpoint list management */ |
| 532 | |
| 533 | /* |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 534 | * journal_clean_one_cp_list |
Jan Kara | f93ea41 | 2006-01-06 00:19:55 -0800 | [diff] [blame] | 535 | * |
Jan Kara | bb18924 | 2011-06-24 23:11:59 +0200 | [diff] [blame] | 536 | * Find all the written-back checkpoint buffers in the given list and release |
| 537 | * them. |
Jan Kara | f93ea41 | 2006-01-06 00:19:55 -0800 | [diff] [blame] | 538 | * |
Jan Kara | f93ea41 | 2006-01-06 00:19:55 -0800 | [diff] [blame] | 539 | * Called with j_list_lock held. |
Mark Fasheh | 7c8903f | 2006-02-14 13:53:03 -0800 | [diff] [blame] | 540 | * Returns number of bufers reaped (for debug) |
Jan Kara | f93ea41 | 2006-01-06 00:19:55 -0800 | [diff] [blame] | 541 | */ |
| 542 | |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 543 | static int journal_clean_one_cp_list(struct journal_head *jh, int *released) |
| 544 | { |
| 545 | struct journal_head *last_jh; |
| 546 | struct journal_head *next_jh = jh; |
| 547 | int ret, freed = 0; |
| 548 | |
| 549 | *released = 0; |
| 550 | if (!jh) |
| 551 | return 0; |
| 552 | |
Dave Kleikamp | e9ad562 | 2006-09-27 01:49:35 -0700 | [diff] [blame] | 553 | last_jh = jh->b_cpprev; |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 554 | do { |
| 555 | jh = next_jh; |
| 556 | next_jh = jh->b_cpnext; |
| 557 | /* Use trylock because of the ranking */ |
| 558 | if (jbd_trylock_bh_state(jh2bh(jh))) { |
| 559 | ret = __try_to_free_cp_buf(jh); |
| 560 | if (ret) { |
| 561 | freed++; |
| 562 | if (ret == 2) { |
| 563 | *released = 1; |
| 564 | return freed; |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | /* |
| 569 | * This function only frees up some memory |
| 570 | * if possible so we dont have an obligation |
| 571 | * to finish processing. Bail out if preemption |
| 572 | * requested: |
| 573 | */ |
| 574 | if (need_resched()) |
| 575 | return freed; |
| 576 | } while (jh != last_jh); |
| 577 | |
| 578 | return freed; |
| 579 | } |
| 580 | |
| 581 | /* |
| 582 | * journal_clean_checkpoint_list |
| 583 | * |
| 584 | * Find all the written-back checkpoint buffers in the journal and release them. |
| 585 | * |
| 586 | * Called with the journal locked. |
| 587 | * Called with j_list_lock held. |
| 588 | * Returns number of buffers reaped (for debug) |
| 589 | */ |
| 590 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | int __journal_clean_checkpoint_list(journal_t *journal) |
| 592 | { |
| 593 | transaction_t *transaction, *last_transaction, *next_transaction; |
Mark Fasheh | 7c8903f | 2006-02-14 13:53:03 -0800 | [diff] [blame] | 594 | int ret = 0; |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 595 | int released; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | |
| 597 | transaction = journal->j_checkpoint_transactions; |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 598 | if (!transaction) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | goto out; |
| 600 | |
| 601 | last_transaction = transaction->t_cpprev; |
| 602 | next_transaction = transaction; |
| 603 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | transaction = next_transaction; |
| 605 | next_transaction = transaction->t_cpnext; |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 606 | ret += journal_clean_one_cp_list(transaction-> |
| 607 | t_checkpoint_list, &released); |
| 608 | /* |
| 609 | * This function only frees up some memory if possible so we |
| 610 | * dont have an obligation to finish processing. Bail out if |
| 611 | * preemption requested: |
| 612 | */ |
| 613 | if (need_resched()) |
| 614 | goto out; |
| 615 | if (released) |
| 616 | continue; |
| 617 | /* |
| 618 | * It is essential that we are as careful as in the case of |
| 619 | * t_checkpoint_list with removing the buffer from the list as |
| 620 | * we can possibly see not yet submitted buffers on io_list |
| 621 | */ |
| 622 | ret += journal_clean_one_cp_list(transaction-> |
| 623 | t_checkpoint_io_list, &released); |
| 624 | if (need_resched()) |
| 625 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | } while (transaction != last_transaction); |
| 627 | out: |
| 628 | return ret; |
| 629 | } |
| 630 | |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 631 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | * journal_remove_checkpoint: called after a buffer has been committed |
| 633 | * to disk (either by being write-back flushed to disk, or being |
| 634 | * committed to the log). |
| 635 | * |
| 636 | * We cannot safely clean a transaction out of the log until all of the |
| 637 | * buffer updates committed in that transaction have safely been stored |
| 638 | * elsewhere on disk. To achieve this, all of the buffers in a |
| 639 | * transaction need to be maintained on the transaction's checkpoint |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 640 | * lists until they have been rewritten, at which point this function is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | * called to remove the buffer from the existing transaction's |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 642 | * checkpoint lists. |
| 643 | * |
| 644 | * The function returns 1 if it frees the transaction, 0 otherwise. |
Jan Kara | bb18924 | 2011-06-24 23:11:59 +0200 | [diff] [blame] | 645 | * The function can free jh and bh. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | * This function is called with j_list_lock held. |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 648 | * This function is called with jbd_lock_bh_state(jh2bh(jh)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | */ |
| 650 | |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 651 | int __journal_remove_checkpoint(struct journal_head *jh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | { |
| 653 | transaction_t *transaction; |
| 654 | journal_t *journal; |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 655 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | |
| 657 | JBUFFER_TRACE(jh, "entry"); |
| 658 | |
| 659 | if ((transaction = jh->b_cp_transaction) == NULL) { |
| 660 | JBUFFER_TRACE(jh, "not on transaction"); |
| 661 | goto out; |
| 662 | } |
| 663 | journal = transaction->t_journal; |
| 664 | |
Jan Kara | bb18924 | 2011-06-24 23:11:59 +0200 | [diff] [blame] | 665 | JBUFFER_TRACE(jh, "removing from transaction"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | __buffer_unlink(jh); |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 667 | jh->b_cp_transaction = NULL; |
Jan Kara | bb18924 | 2011-06-24 23:11:59 +0200 | [diff] [blame] | 668 | journal_put_journal_head(jh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 670 | if (transaction->t_checkpoint_list != NULL || |
| 671 | transaction->t_checkpoint_io_list != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | |
| 674 | /* |
| 675 | * There is one special case to worry about: if we have just pulled the |
Jan Kara | d4beaf4 | 2007-12-04 23:45:27 -0800 | [diff] [blame] | 676 | * buffer off a running or committing transaction's checkpoing list, |
| 677 | * then even if the checkpoint list is empty, the transaction obviously |
| 678 | * cannot be dropped! |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | * |
Jan Kara | d4beaf4 | 2007-12-04 23:45:27 -0800 | [diff] [blame] | 680 | * The locking here around t_state is a bit sleazy. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | * See the comment at the end of journal_commit_transaction(). |
| 682 | */ |
Jan Kara | bb18924 | 2011-06-24 23:11:59 +0200 | [diff] [blame] | 683 | if (transaction->t_state != T_FINISHED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | |
| 686 | /* OK, that was the last buffer for the transaction: we can now |
| 687 | safely remove this transaction from the log */ |
| 688 | |
| 689 | __journal_drop_transaction(journal, transaction); |
| 690 | |
| 691 | /* Just in case anybody was waiting for more transactions to be |
| 692 | checkpointed... */ |
| 693 | wake_up(&journal->j_wait_logspace); |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 694 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | out: |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 696 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | /* |
| 700 | * journal_insert_checkpoint: put a committed buffer onto a checkpoint |
| 701 | * list so that we know when it is safe to clean the transaction out of |
| 702 | * the log. |
| 703 | * |
| 704 | * Called with the journal locked. |
| 705 | * Called with j_list_lock held. |
| 706 | */ |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 707 | void __journal_insert_checkpoint(struct journal_head *jh, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | transaction_t *transaction) |
| 709 | { |
| 710 | JBUFFER_TRACE(jh, "entry"); |
| 711 | J_ASSERT_JH(jh, buffer_dirty(jh2bh(jh)) || buffer_jbddirty(jh2bh(jh))); |
| 712 | J_ASSERT_JH(jh, jh->b_cp_transaction == NULL); |
| 713 | |
Jan Kara | bb18924 | 2011-06-24 23:11:59 +0200 | [diff] [blame] | 714 | /* Get reference for checkpointing transaction */ |
| 715 | journal_grab_journal_head(jh2bh(jh)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | jh->b_cp_transaction = transaction; |
| 717 | |
| 718 | if (!transaction->t_checkpoint_list) { |
| 719 | jh->b_cpnext = jh->b_cpprev = jh; |
| 720 | } else { |
| 721 | jh->b_cpnext = transaction->t_checkpoint_list; |
| 722 | jh->b_cpprev = transaction->t_checkpoint_list->b_cpprev; |
| 723 | jh->b_cpprev->b_cpnext = jh; |
| 724 | jh->b_cpnext->b_cpprev = jh; |
| 725 | } |
| 726 | transaction->t_checkpoint_list = jh; |
| 727 | } |
| 728 | |
| 729 | /* |
| 730 | * We've finished with this transaction structure: adios... |
Mingming Cao | ae6ddcc | 2006-09-27 01:49:27 -0700 | [diff] [blame] | 731 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | * The transaction must have no links except for the checkpoint by this |
| 733 | * point. |
| 734 | * |
| 735 | * Called with the journal locked. |
| 736 | * Called with j_list_lock held. |
| 737 | */ |
| 738 | |
| 739 | void __journal_drop_transaction(journal_t *journal, transaction_t *transaction) |
| 740 | { |
| 741 | assert_spin_locked(&journal->j_list_lock); |
| 742 | if (transaction->t_cpnext) { |
| 743 | transaction->t_cpnext->t_cpprev = transaction->t_cpprev; |
| 744 | transaction->t_cpprev->t_cpnext = transaction->t_cpnext; |
| 745 | if (journal->j_checkpoint_transactions == transaction) |
| 746 | journal->j_checkpoint_transactions = |
| 747 | transaction->t_cpnext; |
| 748 | if (journal->j_checkpoint_transactions == transaction) |
| 749 | journal->j_checkpoint_transactions = NULL; |
| 750 | } |
| 751 | |
| 752 | J_ASSERT(transaction->t_state == T_FINISHED); |
| 753 | J_ASSERT(transaction->t_buffers == NULL); |
| 754 | J_ASSERT(transaction->t_sync_datalist == NULL); |
| 755 | J_ASSERT(transaction->t_forget == NULL); |
| 756 | J_ASSERT(transaction->t_iobuf_list == NULL); |
| 757 | J_ASSERT(transaction->t_shadow_list == NULL); |
| 758 | J_ASSERT(transaction->t_log_list == NULL); |
| 759 | J_ASSERT(transaction->t_checkpoint_list == NULL); |
Jan Kara | 78ce89c | 2006-06-23 02:06:05 -0700 | [diff] [blame] | 760 | J_ASSERT(transaction->t_checkpoint_io_list == NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | J_ASSERT(transaction->t_updates == 0); |
| 762 | J_ASSERT(journal->j_committing_transaction != transaction); |
| 763 | J_ASSERT(journal->j_running_transaction != transaction); |
| 764 | |
Lukas Czerner | 99cb1a3 | 2011-05-23 18:33:02 +0200 | [diff] [blame] | 765 | trace_jbd_drop_transaction(journal, transaction); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | jbd_debug(1, "Dropping transaction %d, all done\n", transaction->t_tid); |
| 767 | kfree(transaction); |
| 768 | } |