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