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