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