jbd2: Add commit time into the commit block
[pandora-kernel.git] / fs / jbd2 / commit.c
1 /*
2  * linux/fs/jbd2/commit.c
3  *
4  * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
5  *
6  * Copyright 1998 Red Hat corp --- All Rights Reserved
7  *
8  * This file is part of the Linux kernel and is made available under
9  * the terms of the GNU General Public License, version 2, or at your
10  * option, any later version, incorporated herein by reference.
11  *
12  * Journal commit routines for the generic filesystem journaling code;
13  * part of the ext2fs journaling system.
14  */
15
16 #include <linux/time.h>
17 #include <linux/fs.h>
18 #include <linux/jbd2.h>
19 #include <linux/errno.h>
20 #include <linux/slab.h>
21 #include <linux/mm.h>
22 #include <linux/pagemap.h>
23 #include <linux/jiffies.h>
24 #include <linux/crc32.h>
25
26 /*
27  * Default IO end handler for temporary BJ_IO buffer_heads.
28  */
29 static void journal_end_buffer_io_sync(struct buffer_head *bh, int uptodate)
30 {
31         BUFFER_TRACE(bh, "");
32         if (uptodate)
33                 set_buffer_uptodate(bh);
34         else
35                 clear_buffer_uptodate(bh);
36         unlock_buffer(bh);
37 }
38
39 /*
40  * When an ext3-ordered file is truncated, it is possible that many pages are
41  * not sucessfully freed, because they are attached to a committing transaction.
42  * After the transaction commits, these pages are left on the LRU, with no
43  * ->mapping, and with attached buffers.  These pages are trivially reclaimable
44  * by the VM, but their apparent absence upsets the VM accounting, and it makes
45  * the numbers in /proc/meminfo look odd.
46  *
47  * So here, we have a buffer which has just come off the forget list.  Look to
48  * see if we can strip all buffers from the backing page.
49  *
50  * Called under lock_journal(), and possibly under journal_datalist_lock.  The
51  * caller provided us with a ref against the buffer, and we drop that here.
52  */
53 static void release_buffer_page(struct buffer_head *bh)
54 {
55         struct page *page;
56
57         if (buffer_dirty(bh))
58                 goto nope;
59         if (atomic_read(&bh->b_count) != 1)
60                 goto nope;
61         page = bh->b_page;
62         if (!page)
63                 goto nope;
64         if (page->mapping)
65                 goto nope;
66
67         /* OK, it's a truncated page */
68         if (TestSetPageLocked(page))
69                 goto nope;
70
71         page_cache_get(page);
72         __brelse(bh);
73         try_to_free_buffers(page);
74         unlock_page(page);
75         page_cache_release(page);
76         return;
77
78 nope:
79         __brelse(bh);
80 }
81
82 /*
83  * Try to acquire jbd_lock_bh_state() against the buffer, when j_list_lock is
84  * held.  For ranking reasons we must trylock.  If we lose, schedule away and
85  * return 0.  j_list_lock is dropped in this case.
86  */
87 static int inverted_lock(journal_t *journal, struct buffer_head *bh)
88 {
89         if (!jbd_trylock_bh_state(bh)) {
90                 spin_unlock(&journal->j_list_lock);
91                 schedule();
92                 return 0;
93         }
94         return 1;
95 }
96
97 /*
98  * Done it all: now submit the commit record.  We should have
99  * cleaned up our previous buffers by now, so if we are in abort
100  * mode we can now just skip the rest of the journal write
101  * entirely.
102  *
103  * Returns 1 if the journal needs to be aborted or 0 on success
104  */
105 static int journal_submit_commit_record(journal_t *journal,
106                                         transaction_t *commit_transaction,
107                                         struct buffer_head **cbh,
108                                         __u32 crc32_sum)
109 {
110         struct journal_head *descriptor;
111         struct commit_header *tmp;
112         struct buffer_head *bh;
113         int ret;
114         int barrier_done = 0;
115         struct timespec now = current_kernel_time();
116
117         if (is_journal_aborted(journal))
118                 return 0;
119
120         descriptor = jbd2_journal_get_descriptor_buffer(journal);
121         if (!descriptor)
122                 return 1;
123
124         bh = jh2bh(descriptor);
125
126         tmp = (struct commit_header *)bh->b_data;
127         tmp->h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER);
128         tmp->h_blocktype = cpu_to_be32(JBD2_COMMIT_BLOCK);
129         tmp->h_sequence = cpu_to_be32(commit_transaction->t_tid);
130         tmp->h_commit_sec = cpu_to_be64(now.tv_sec);
131         tmp->h_commit_nsec = cpu_to_be32(now.tv_nsec);
132
133         if (JBD2_HAS_COMPAT_FEATURE(journal,
134                                     JBD2_FEATURE_COMPAT_CHECKSUM)) {
135                 tmp->h_chksum_type      = JBD2_CRC32_CHKSUM;
136                 tmp->h_chksum_size      = JBD2_CRC32_CHKSUM_SIZE;
137                 tmp->h_chksum[0]        = cpu_to_be32(crc32_sum);
138         }
139
140         JBUFFER_TRACE(descriptor, "submit commit block");
141         lock_buffer(bh);
142         get_bh(bh);
143         set_buffer_dirty(bh);
144         set_buffer_uptodate(bh);
145         bh->b_end_io = journal_end_buffer_io_sync;
146
147         if (journal->j_flags & JBD2_BARRIER &&
148                 !JBD2_HAS_INCOMPAT_FEATURE(journal,
149                                          JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT)) {
150                 set_buffer_ordered(bh);
151                 barrier_done = 1;
152         }
153         ret = submit_bh(WRITE, bh);
154         if (barrier_done)
155                 clear_buffer_ordered(bh);
156
157         /* is it possible for another commit to fail at roughly
158          * the same time as this one?  If so, we don't want to
159          * trust the barrier flag in the super, but instead want
160          * to remember if we sent a barrier request
161          */
162         if (ret == -EOPNOTSUPP && barrier_done) {
163                 char b[BDEVNAME_SIZE];
164
165                 printk(KERN_WARNING
166                         "JBD: barrier-based sync failed on %s - "
167                         "disabling barriers\n",
168                         bdevname(journal->j_dev, b));
169                 spin_lock(&journal->j_state_lock);
170                 journal->j_flags &= ~JBD2_BARRIER;
171                 spin_unlock(&journal->j_state_lock);
172
173                 /* And try again, without the barrier */
174                 lock_buffer(bh);
175                 set_buffer_uptodate(bh);
176                 set_buffer_dirty(bh);
177                 ret = submit_bh(WRITE, bh);
178         }
179         *cbh = bh;
180         return ret;
181 }
182
183 /*
184  * This function along with journal_submit_commit_record
185  * allows to write the commit record asynchronously.
186  */
187 static int journal_wait_on_commit_record(struct buffer_head *bh)
188 {
189         int ret = 0;
190
191         clear_buffer_dirty(bh);
192         wait_on_buffer(bh);
193
194         if (unlikely(!buffer_uptodate(bh)))
195                 ret = -EIO;
196         put_bh(bh);            /* One for getblk() */
197         jbd2_journal_put_journal_head(bh2jh(bh));
198
199         return ret;
200 }
201
202 /*
203  * Wait for all submitted IO to complete.
204  */
205 static int journal_wait_on_locked_list(journal_t *journal,
206                                        transaction_t *commit_transaction)
207 {
208         int ret = 0;
209         struct journal_head *jh;
210
211         while (commit_transaction->t_locked_list) {
212                 struct buffer_head *bh;
213
214                 jh = commit_transaction->t_locked_list->b_tprev;
215                 bh = jh2bh(jh);
216                 get_bh(bh);
217                 if (buffer_locked(bh)) {
218                         spin_unlock(&journal->j_list_lock);
219                         wait_on_buffer(bh);
220                         if (unlikely(!buffer_uptodate(bh)))
221                                 ret = -EIO;
222                         spin_lock(&journal->j_list_lock);
223                 }
224                 if (!inverted_lock(journal, bh)) {
225                         put_bh(bh);
226                         spin_lock(&journal->j_list_lock);
227                         continue;
228                 }
229                 if (buffer_jbd(bh) && jh->b_jlist == BJ_Locked) {
230                         __jbd2_journal_unfile_buffer(jh);
231                         jbd_unlock_bh_state(bh);
232                         jbd2_journal_remove_journal_head(bh);
233                         put_bh(bh);
234                 } else {
235                         jbd_unlock_bh_state(bh);
236                 }
237                 put_bh(bh);
238                 cond_resched_lock(&journal->j_list_lock);
239         }
240         return ret;
241   }
242
243 static void journal_do_submit_data(struct buffer_head **wbuf, int bufs)
244 {
245         int i;
246
247         for (i = 0; i < bufs; i++) {
248                 wbuf[i]->b_end_io = end_buffer_write_sync;
249                 /* We use-up our safety reference in submit_bh() */
250                 submit_bh(WRITE, wbuf[i]);
251         }
252 }
253
254 /*
255  *  Submit all the data buffers to disk
256  */
257 static void journal_submit_data_buffers(journal_t *journal,
258                                 transaction_t *commit_transaction)
259 {
260         struct journal_head *jh;
261         struct buffer_head *bh;
262         int locked;
263         int bufs = 0;
264         struct buffer_head **wbuf = journal->j_wbuf;
265
266         /*
267          * Whenever we unlock the journal and sleep, things can get added
268          * onto ->t_sync_datalist, so we have to keep looping back to
269          * write_out_data until we *know* that the list is empty.
270          *
271          * Cleanup any flushed data buffers from the data list.  Even in
272          * abort mode, we want to flush this out as soon as possible.
273          */
274 write_out_data:
275         cond_resched();
276         spin_lock(&journal->j_list_lock);
277
278         while (commit_transaction->t_sync_datalist) {
279                 jh = commit_transaction->t_sync_datalist;
280                 bh = jh2bh(jh);
281                 locked = 0;
282
283                 /* Get reference just to make sure buffer does not disappear
284                  * when we are forced to drop various locks */
285                 get_bh(bh);
286                 /* If the buffer is dirty, we need to submit IO and hence
287                  * we need the buffer lock. We try to lock the buffer without
288                  * blocking. If we fail, we need to drop j_list_lock and do
289                  * blocking lock_buffer().
290                  */
291                 if (buffer_dirty(bh)) {
292                         if (test_set_buffer_locked(bh)) {
293                                 BUFFER_TRACE(bh, "needs blocking lock");
294                                 spin_unlock(&journal->j_list_lock);
295                                 /* Write out all data to prevent deadlocks */
296                                 journal_do_submit_data(wbuf, bufs);
297                                 bufs = 0;
298                                 lock_buffer(bh);
299                                 spin_lock(&journal->j_list_lock);
300                         }
301                         locked = 1;
302                 }
303                 /* We have to get bh_state lock. Again out of order, sigh. */
304                 if (!inverted_lock(journal, bh)) {
305                         jbd_lock_bh_state(bh);
306                         spin_lock(&journal->j_list_lock);
307                 }
308                 /* Someone already cleaned up the buffer? */
309                 if (!buffer_jbd(bh)
310                         || jh->b_transaction != commit_transaction
311                         || jh->b_jlist != BJ_SyncData) {
312                         jbd_unlock_bh_state(bh);
313                         if (locked)
314                                 unlock_buffer(bh);
315                         BUFFER_TRACE(bh, "already cleaned up");
316                         put_bh(bh);
317                         continue;
318                 }
319                 if (locked && test_clear_buffer_dirty(bh)) {
320                         BUFFER_TRACE(bh, "needs writeout, adding to array");
321                         wbuf[bufs++] = bh;
322                         __jbd2_journal_file_buffer(jh, commit_transaction,
323                                                 BJ_Locked);
324                         jbd_unlock_bh_state(bh);
325                         if (bufs == journal->j_wbufsize) {
326                                 spin_unlock(&journal->j_list_lock);
327                                 journal_do_submit_data(wbuf, bufs);
328                                 bufs = 0;
329                                 goto write_out_data;
330                         }
331                 } else if (!locked && buffer_locked(bh)) {
332                         __jbd2_journal_file_buffer(jh, commit_transaction,
333                                                 BJ_Locked);
334                         jbd_unlock_bh_state(bh);
335                         put_bh(bh);
336                 } else {
337                         BUFFER_TRACE(bh, "writeout complete: unfile");
338                         __jbd2_journal_unfile_buffer(jh);
339                         jbd_unlock_bh_state(bh);
340                         if (locked)
341                                 unlock_buffer(bh);
342                         jbd2_journal_remove_journal_head(bh);
343                         /* Once for our safety reference, once for
344                          * jbd2_journal_remove_journal_head() */
345                         put_bh(bh);
346                         put_bh(bh);
347                 }
348
349                 if (need_resched() || spin_needbreak(&journal->j_list_lock)) {
350                         spin_unlock(&journal->j_list_lock);
351                         goto write_out_data;
352                 }
353         }
354         spin_unlock(&journal->j_list_lock);
355         journal_do_submit_data(wbuf, bufs);
356 }
357
358 static __u32 jbd2_checksum_data(__u32 crc32_sum, struct buffer_head *bh)
359 {
360         struct page *page = bh->b_page;
361         char *addr;
362         __u32 checksum;
363
364         addr = kmap_atomic(page, KM_USER0);
365         checksum = crc32_be(crc32_sum,
366                 (void *)(addr + offset_in_page(bh->b_data)), bh->b_size);
367         kunmap_atomic(addr, KM_USER0);
368
369         return checksum;
370 }
371
372 static void write_tag_block(int tag_bytes, journal_block_tag_t *tag,
373                                    unsigned long long block)
374 {
375         tag->t_blocknr = cpu_to_be32(block & (u32)~0);
376         if (tag_bytes > JBD2_TAG_SIZE32)
377                 tag->t_blocknr_high = cpu_to_be32((block >> 31) >> 1);
378 }
379
380 /*
381  * jbd2_journal_commit_transaction
382  *
383  * The primary function for committing a transaction to the log.  This
384  * function is called by the journal thread to begin a complete commit.
385  */
386 void jbd2_journal_commit_transaction(journal_t *journal)
387 {
388         struct transaction_stats_s stats;
389         transaction_t *commit_transaction;
390         struct journal_head *jh, *new_jh, *descriptor;
391         struct buffer_head **wbuf = journal->j_wbuf;
392         int bufs;
393         int flags;
394         int err;
395         unsigned long long blocknr;
396         char *tagp = NULL;
397         journal_header_t *header;
398         journal_block_tag_t *tag = NULL;
399         int space_left = 0;
400         int first_tag = 0;
401         int tag_flag;
402         int i;
403         int tag_bytes = journal_tag_bytes(journal);
404         struct buffer_head *cbh = NULL; /* For transactional checksums */
405         __u32 crc32_sum = ~0;
406
407         /*
408          * First job: lock down the current transaction and wait for
409          * all outstanding updates to complete.
410          */
411
412 #ifdef COMMIT_STATS
413         spin_lock(&journal->j_list_lock);
414         summarise_journal_usage(journal);
415         spin_unlock(&journal->j_list_lock);
416 #endif
417
418         /* Do we need to erase the effects of a prior jbd2_journal_flush? */
419         if (journal->j_flags & JBD2_FLUSHED) {
420                 jbd_debug(3, "super block updated\n");
421                 jbd2_journal_update_superblock(journal, 1);
422         } else {
423                 jbd_debug(3, "superblock not updated\n");
424         }
425
426         J_ASSERT(journal->j_running_transaction != NULL);
427         J_ASSERT(journal->j_committing_transaction == NULL);
428
429         commit_transaction = journal->j_running_transaction;
430         J_ASSERT(commit_transaction->t_state == T_RUNNING);
431
432         jbd_debug(1, "JBD: starting commit of transaction %d\n",
433                         commit_transaction->t_tid);
434
435         spin_lock(&journal->j_state_lock);
436         commit_transaction->t_state = T_LOCKED;
437
438         stats.u.run.rs_wait = commit_transaction->t_max_wait;
439         stats.u.run.rs_locked = jiffies;
440         stats.u.run.rs_running = jbd2_time_diff(commit_transaction->t_start,
441                                                 stats.u.run.rs_locked);
442
443         spin_lock(&commit_transaction->t_handle_lock);
444         while (commit_transaction->t_updates) {
445                 DEFINE_WAIT(wait);
446
447                 prepare_to_wait(&journal->j_wait_updates, &wait,
448                                         TASK_UNINTERRUPTIBLE);
449                 if (commit_transaction->t_updates) {
450                         spin_unlock(&commit_transaction->t_handle_lock);
451                         spin_unlock(&journal->j_state_lock);
452                         schedule();
453                         spin_lock(&journal->j_state_lock);
454                         spin_lock(&commit_transaction->t_handle_lock);
455                 }
456                 finish_wait(&journal->j_wait_updates, &wait);
457         }
458         spin_unlock(&commit_transaction->t_handle_lock);
459
460         J_ASSERT (commit_transaction->t_outstanding_credits <=
461                         journal->j_max_transaction_buffers);
462
463         /*
464          * First thing we are allowed to do is to discard any remaining
465          * BJ_Reserved buffers.  Note, it is _not_ permissible to assume
466          * that there are no such buffers: if a large filesystem
467          * operation like a truncate needs to split itself over multiple
468          * transactions, then it may try to do a jbd2_journal_restart() while
469          * there are still BJ_Reserved buffers outstanding.  These must
470          * be released cleanly from the current transaction.
471          *
472          * In this case, the filesystem must still reserve write access
473          * again before modifying the buffer in the new transaction, but
474          * we do not require it to remember exactly which old buffers it
475          * has reserved.  This is consistent with the existing behaviour
476          * that multiple jbd2_journal_get_write_access() calls to the same
477          * buffer are perfectly permissable.
478          */
479         while (commit_transaction->t_reserved_list) {
480                 jh = commit_transaction->t_reserved_list;
481                 JBUFFER_TRACE(jh, "reserved, unused: refile");
482                 /*
483                  * A jbd2_journal_get_undo_access()+jbd2_journal_release_buffer() may
484                  * leave undo-committed data.
485                  */
486                 if (jh->b_committed_data) {
487                         struct buffer_head *bh = jh2bh(jh);
488
489                         jbd_lock_bh_state(bh);
490                         jbd2_free(jh->b_committed_data, bh->b_size);
491                         jh->b_committed_data = NULL;
492                         jbd_unlock_bh_state(bh);
493                 }
494                 jbd2_journal_refile_buffer(journal, jh);
495         }
496
497         /*
498          * Now try to drop any written-back buffers from the journal's
499          * checkpoint lists.  We do this *before* commit because it potentially
500          * frees some memory
501          */
502         spin_lock(&journal->j_list_lock);
503         __jbd2_journal_clean_checkpoint_list(journal);
504         spin_unlock(&journal->j_list_lock);
505
506         jbd_debug (3, "JBD: commit phase 1\n");
507
508         /*
509          * Switch to a new revoke table.
510          */
511         jbd2_journal_switch_revoke_table(journal);
512
513         stats.u.run.rs_flushing = jiffies;
514         stats.u.run.rs_locked = jbd2_time_diff(stats.u.run.rs_locked,
515                                                stats.u.run.rs_flushing);
516
517         commit_transaction->t_state = T_FLUSH;
518         journal->j_committing_transaction = commit_transaction;
519         journal->j_running_transaction = NULL;
520         commit_transaction->t_log_start = journal->j_head;
521         wake_up(&journal->j_wait_transaction_locked);
522         spin_unlock(&journal->j_state_lock);
523
524         jbd_debug (3, "JBD: commit phase 2\n");
525
526         /*
527          * Now start flushing things to disk, in the order they appear
528          * on the transaction lists.  Data blocks go first.
529          */
530         err = 0;
531         journal_submit_data_buffers(journal, commit_transaction);
532
533         /*
534          * Wait for all previously submitted IO to complete if commit
535          * record is to be written synchronously.
536          */
537         spin_lock(&journal->j_list_lock);
538         if (!JBD2_HAS_INCOMPAT_FEATURE(journal,
539                 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT))
540                 err = journal_wait_on_locked_list(journal,
541                                                 commit_transaction);
542
543         spin_unlock(&journal->j_list_lock);
544
545         if (err)
546                 jbd2_journal_abort(journal, err);
547
548         jbd2_journal_write_revoke_records(journal, commit_transaction);
549
550         jbd_debug(3, "JBD: commit phase 2\n");
551
552         /*
553          * If we found any dirty or locked buffers, then we should have
554          * looped back up to the write_out_data label.  If there weren't
555          * any then journal_clean_data_list should have wiped the list
556          * clean by now, so check that it is in fact empty.
557          */
558         J_ASSERT (commit_transaction->t_sync_datalist == NULL);
559
560         jbd_debug (3, "JBD: commit phase 3\n");
561
562         /*
563          * Way to go: we have now written out all of the data for a
564          * transaction!  Now comes the tricky part: we need to write out
565          * metadata.  Loop over the transaction's entire buffer list:
566          */
567         spin_lock(&journal->j_state_lock);
568         commit_transaction->t_state = T_COMMIT;
569         spin_unlock(&journal->j_state_lock);
570
571         stats.u.run.rs_logging = jiffies;
572         stats.u.run.rs_flushing = jbd2_time_diff(stats.u.run.rs_flushing,
573                                                  stats.u.run.rs_logging);
574         stats.u.run.rs_blocks = commit_transaction->t_outstanding_credits;
575         stats.u.run.rs_blocks_logged = 0;
576
577         J_ASSERT(commit_transaction->t_nr_buffers <=
578                  commit_transaction->t_outstanding_credits);
579
580         descriptor = NULL;
581         bufs = 0;
582         while (commit_transaction->t_buffers) {
583
584                 /* Find the next buffer to be journaled... */
585
586                 jh = commit_transaction->t_buffers;
587
588                 /* If we're in abort mode, we just un-journal the buffer and
589                    release it for background writing. */
590
591                 if (is_journal_aborted(journal)) {
592                         JBUFFER_TRACE(jh, "journal is aborting: refile");
593                         jbd2_journal_refile_buffer(journal, jh);
594                         /* If that was the last one, we need to clean up
595                          * any descriptor buffers which may have been
596                          * already allocated, even if we are now
597                          * aborting. */
598                         if (!commit_transaction->t_buffers)
599                                 goto start_journal_io;
600                         continue;
601                 }
602
603                 /* Make sure we have a descriptor block in which to
604                    record the metadata buffer. */
605
606                 if (!descriptor) {
607                         struct buffer_head *bh;
608
609                         J_ASSERT (bufs == 0);
610
611                         jbd_debug(4, "JBD: get descriptor\n");
612
613                         descriptor = jbd2_journal_get_descriptor_buffer(journal);
614                         if (!descriptor) {
615                                 jbd2_journal_abort(journal, -EIO);
616                                 continue;
617                         }
618
619                         bh = jh2bh(descriptor);
620                         jbd_debug(4, "JBD: got buffer %llu (%p)\n",
621                                 (unsigned long long)bh->b_blocknr, bh->b_data);
622                         header = (journal_header_t *)&bh->b_data[0];
623                         header->h_magic     = cpu_to_be32(JBD2_MAGIC_NUMBER);
624                         header->h_blocktype = cpu_to_be32(JBD2_DESCRIPTOR_BLOCK);
625                         header->h_sequence  = cpu_to_be32(commit_transaction->t_tid);
626
627                         tagp = &bh->b_data[sizeof(journal_header_t)];
628                         space_left = bh->b_size - sizeof(journal_header_t);
629                         first_tag = 1;
630                         set_buffer_jwrite(bh);
631                         set_buffer_dirty(bh);
632                         wbuf[bufs++] = bh;
633
634                         /* Record it so that we can wait for IO
635                            completion later */
636                         BUFFER_TRACE(bh, "ph3: file as descriptor");
637                         jbd2_journal_file_buffer(descriptor, commit_transaction,
638                                         BJ_LogCtl);
639                 }
640
641                 /* Where is the buffer to be written? */
642
643                 err = jbd2_journal_next_log_block(journal, &blocknr);
644                 /* If the block mapping failed, just abandon the buffer
645                    and repeat this loop: we'll fall into the
646                    refile-on-abort condition above. */
647                 if (err) {
648                         jbd2_journal_abort(journal, err);
649                         continue;
650                 }
651
652                 /*
653                  * start_this_handle() uses t_outstanding_credits to determine
654                  * the free space in the log, but this counter is changed
655                  * by jbd2_journal_next_log_block() also.
656                  */
657                 commit_transaction->t_outstanding_credits--;
658
659                 /* Bump b_count to prevent truncate from stumbling over
660                    the shadowed buffer!  @@@ This can go if we ever get
661                    rid of the BJ_IO/BJ_Shadow pairing of buffers. */
662                 atomic_inc(&jh2bh(jh)->b_count);
663
664                 /* Make a temporary IO buffer with which to write it out
665                    (this will requeue both the metadata buffer and the
666                    temporary IO buffer). new_bh goes on BJ_IO*/
667
668                 set_bit(BH_JWrite, &jh2bh(jh)->b_state);
669                 /*
670                  * akpm: jbd2_journal_write_metadata_buffer() sets
671                  * new_bh->b_transaction to commit_transaction.
672                  * We need to clean this up before we release new_bh
673                  * (which is of type BJ_IO)
674                  */
675                 JBUFFER_TRACE(jh, "ph3: write metadata");
676                 flags = jbd2_journal_write_metadata_buffer(commit_transaction,
677                                                       jh, &new_jh, blocknr);
678                 set_bit(BH_JWrite, &jh2bh(new_jh)->b_state);
679                 wbuf[bufs++] = jh2bh(new_jh);
680
681                 /* Record the new block's tag in the current descriptor
682                    buffer */
683
684                 tag_flag = 0;
685                 if (flags & 1)
686                         tag_flag |= JBD2_FLAG_ESCAPE;
687                 if (!first_tag)
688                         tag_flag |= JBD2_FLAG_SAME_UUID;
689
690                 tag = (journal_block_tag_t *) tagp;
691                 write_tag_block(tag_bytes, tag, jh2bh(jh)->b_blocknr);
692                 tag->t_flags = cpu_to_be32(tag_flag);
693                 tagp += tag_bytes;
694                 space_left -= tag_bytes;
695
696                 if (first_tag) {
697                         memcpy (tagp, journal->j_uuid, 16);
698                         tagp += 16;
699                         space_left -= 16;
700                         first_tag = 0;
701                 }
702
703                 /* If there's no more to do, or if the descriptor is full,
704                    let the IO rip! */
705
706                 if (bufs == journal->j_wbufsize ||
707                     commit_transaction->t_buffers == NULL ||
708                     space_left < tag_bytes + 16) {
709
710                         jbd_debug(4, "JBD: Submit %d IOs\n", bufs);
711
712                         /* Write an end-of-descriptor marker before
713                            submitting the IOs.  "tag" still points to
714                            the last tag we set up. */
715
716                         tag->t_flags |= cpu_to_be32(JBD2_FLAG_LAST_TAG);
717
718 start_journal_io:
719                         for (i = 0; i < bufs; i++) {
720                                 struct buffer_head *bh = wbuf[i];
721                                 /*
722                                  * Compute checksum.
723                                  */
724                                 if (JBD2_HAS_COMPAT_FEATURE(journal,
725                                         JBD2_FEATURE_COMPAT_CHECKSUM)) {
726                                         crc32_sum =
727                                             jbd2_checksum_data(crc32_sum, bh);
728                                 }
729
730                                 lock_buffer(bh);
731                                 clear_buffer_dirty(bh);
732                                 set_buffer_uptodate(bh);
733                                 bh->b_end_io = journal_end_buffer_io_sync;
734                                 submit_bh(WRITE, bh);
735                         }
736                         cond_resched();
737                         stats.u.run.rs_blocks_logged += bufs;
738
739                         /* Force a new descriptor to be generated next
740                            time round the loop. */
741                         descriptor = NULL;
742                         bufs = 0;
743                 }
744         }
745
746         /* Done it all: now write the commit record asynchronously. */
747
748         if (JBD2_HAS_INCOMPAT_FEATURE(journal,
749                 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT)) {
750                 err = journal_submit_commit_record(journal, commit_transaction,
751                                                  &cbh, crc32_sum);
752                 if (err)
753                         __jbd2_journal_abort_hard(journal);
754
755                 spin_lock(&journal->j_list_lock);
756                 err = journal_wait_on_locked_list(journal,
757                                                 commit_transaction);
758                 spin_unlock(&journal->j_list_lock);
759                 if (err)
760                         __jbd2_journal_abort_hard(journal);
761         }
762
763         /* Lo and behold: we have just managed to send a transaction to
764            the log.  Before we can commit it, wait for the IO so far to
765            complete.  Control buffers being written are on the
766            transaction's t_log_list queue, and metadata buffers are on
767            the t_iobuf_list queue.
768
769            Wait for the buffers in reverse order.  That way we are
770            less likely to be woken up until all IOs have completed, and
771            so we incur less scheduling load.
772         */
773
774         jbd_debug(3, "JBD: commit phase 4\n");
775
776         /*
777          * akpm: these are BJ_IO, and j_list_lock is not needed.
778          * See __journal_try_to_free_buffer.
779          */
780 wait_for_iobuf:
781         while (commit_transaction->t_iobuf_list != NULL) {
782                 struct buffer_head *bh;
783
784                 jh = commit_transaction->t_iobuf_list->b_tprev;
785                 bh = jh2bh(jh);
786                 if (buffer_locked(bh)) {
787                         wait_on_buffer(bh);
788                         goto wait_for_iobuf;
789                 }
790                 if (cond_resched())
791                         goto wait_for_iobuf;
792
793                 if (unlikely(!buffer_uptodate(bh)))
794                         err = -EIO;
795
796                 clear_buffer_jwrite(bh);
797
798                 JBUFFER_TRACE(jh, "ph4: unfile after journal write");
799                 jbd2_journal_unfile_buffer(journal, jh);
800
801                 /*
802                  * ->t_iobuf_list should contain only dummy buffer_heads
803                  * which were created by jbd2_journal_write_metadata_buffer().
804                  */
805                 BUFFER_TRACE(bh, "dumping temporary bh");
806                 jbd2_journal_put_journal_head(jh);
807                 __brelse(bh);
808                 J_ASSERT_BH(bh, atomic_read(&bh->b_count) == 0);
809                 free_buffer_head(bh);
810
811                 /* We also have to unlock and free the corresponding
812                    shadowed buffer */
813                 jh = commit_transaction->t_shadow_list->b_tprev;
814                 bh = jh2bh(jh);
815                 clear_bit(BH_JWrite, &bh->b_state);
816                 J_ASSERT_BH(bh, buffer_jbddirty(bh));
817
818                 /* The metadata is now released for reuse, but we need
819                    to remember it against this transaction so that when
820                    we finally commit, we can do any checkpointing
821                    required. */
822                 JBUFFER_TRACE(jh, "file as BJ_Forget");
823                 jbd2_journal_file_buffer(jh, commit_transaction, BJ_Forget);
824                 /* Wake up any transactions which were waiting for this
825                    IO to complete */
826                 wake_up_bit(&bh->b_state, BH_Unshadow);
827                 JBUFFER_TRACE(jh, "brelse shadowed buffer");
828                 __brelse(bh);
829         }
830
831         J_ASSERT (commit_transaction->t_shadow_list == NULL);
832
833         jbd_debug(3, "JBD: commit phase 5\n");
834
835         /* Here we wait for the revoke record and descriptor record buffers */
836  wait_for_ctlbuf:
837         while (commit_transaction->t_log_list != NULL) {
838                 struct buffer_head *bh;
839
840                 jh = commit_transaction->t_log_list->b_tprev;
841                 bh = jh2bh(jh);
842                 if (buffer_locked(bh)) {
843                         wait_on_buffer(bh);
844                         goto wait_for_ctlbuf;
845                 }
846                 if (cond_resched())
847                         goto wait_for_ctlbuf;
848
849                 if (unlikely(!buffer_uptodate(bh)))
850                         err = -EIO;
851
852                 BUFFER_TRACE(bh, "ph5: control buffer writeout done: unfile");
853                 clear_buffer_jwrite(bh);
854                 jbd2_journal_unfile_buffer(journal, jh);
855                 jbd2_journal_put_journal_head(jh);
856                 __brelse(bh);           /* One for getblk */
857                 /* AKPM: bforget here */
858         }
859
860         jbd_debug(3, "JBD: commit phase 6\n");
861
862         if (!JBD2_HAS_INCOMPAT_FEATURE(journal,
863                 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT)) {
864                 err = journal_submit_commit_record(journal, commit_transaction,
865                                                 &cbh, crc32_sum);
866                 if (err)
867                         __jbd2_journal_abort_hard(journal);
868         }
869         if (!err && !is_journal_aborted(journal))
870                 err = journal_wait_on_commit_record(cbh);
871
872         if (err)
873                 jbd2_journal_abort(journal, err);
874
875         /* End of a transaction!  Finally, we can do checkpoint
876            processing: any buffers committed as a result of this
877            transaction can be removed from any checkpoint list it was on
878            before. */
879
880         jbd_debug(3, "JBD: commit phase 7\n");
881
882         J_ASSERT(commit_transaction->t_sync_datalist == NULL);
883         J_ASSERT(commit_transaction->t_buffers == NULL);
884         J_ASSERT(commit_transaction->t_checkpoint_list == NULL);
885         J_ASSERT(commit_transaction->t_iobuf_list == NULL);
886         J_ASSERT(commit_transaction->t_shadow_list == NULL);
887         J_ASSERT(commit_transaction->t_log_list == NULL);
888
889 restart_loop:
890         /*
891          * As there are other places (journal_unmap_buffer()) adding buffers
892          * to this list we have to be careful and hold the j_list_lock.
893          */
894         spin_lock(&journal->j_list_lock);
895         while (commit_transaction->t_forget) {
896                 transaction_t *cp_transaction;
897                 struct buffer_head *bh;
898
899                 jh = commit_transaction->t_forget;
900                 spin_unlock(&journal->j_list_lock);
901                 bh = jh2bh(jh);
902                 jbd_lock_bh_state(bh);
903                 J_ASSERT_JH(jh, jh->b_transaction == commit_transaction ||
904                         jh->b_transaction == journal->j_running_transaction);
905
906                 /*
907                  * If there is undo-protected committed data against
908                  * this buffer, then we can remove it now.  If it is a
909                  * buffer needing such protection, the old frozen_data
910                  * field now points to a committed version of the
911                  * buffer, so rotate that field to the new committed
912                  * data.
913                  *
914                  * Otherwise, we can just throw away the frozen data now.
915                  */
916                 if (jh->b_committed_data) {
917                         jbd2_free(jh->b_committed_data, bh->b_size);
918                         jh->b_committed_data = NULL;
919                         if (jh->b_frozen_data) {
920                                 jh->b_committed_data = jh->b_frozen_data;
921                                 jh->b_frozen_data = NULL;
922                         }
923                 } else if (jh->b_frozen_data) {
924                         jbd2_free(jh->b_frozen_data, bh->b_size);
925                         jh->b_frozen_data = NULL;
926                 }
927
928                 spin_lock(&journal->j_list_lock);
929                 cp_transaction = jh->b_cp_transaction;
930                 if (cp_transaction) {
931                         JBUFFER_TRACE(jh, "remove from old cp transaction");
932                         cp_transaction->t_chp_stats.cs_dropped++;
933                         __jbd2_journal_remove_checkpoint(jh);
934                 }
935
936                 /* Only re-checkpoint the buffer_head if it is marked
937                  * dirty.  If the buffer was added to the BJ_Forget list
938                  * by jbd2_journal_forget, it may no longer be dirty and
939                  * there's no point in keeping a checkpoint record for
940                  * it. */
941
942                 /* A buffer which has been freed while still being
943                  * journaled by a previous transaction may end up still
944                  * being dirty here, but we want to avoid writing back
945                  * that buffer in the future now that the last use has
946                  * been committed.  That's not only a performance gain,
947                  * it also stops aliasing problems if the buffer is left
948                  * behind for writeback and gets reallocated for another
949                  * use in a different page. */
950                 if (buffer_freed(bh)) {
951                         clear_buffer_freed(bh);
952                         clear_buffer_jbddirty(bh);
953                 }
954
955                 if (buffer_jbddirty(bh)) {
956                         JBUFFER_TRACE(jh, "add to new checkpointing trans");
957                         __jbd2_journal_insert_checkpoint(jh, commit_transaction);
958                         JBUFFER_TRACE(jh, "refile for checkpoint writeback");
959                         __jbd2_journal_refile_buffer(jh);
960                         jbd_unlock_bh_state(bh);
961                 } else {
962                         J_ASSERT_BH(bh, !buffer_dirty(bh));
963                         /* The buffer on BJ_Forget list and not jbddirty means
964                          * it has been freed by this transaction and hence it
965                          * could not have been reallocated until this
966                          * transaction has committed. *BUT* it could be
967                          * reallocated once we have written all the data to
968                          * disk and before we process the buffer on BJ_Forget
969                          * list. */
970                         JBUFFER_TRACE(jh, "refile or unfile freed buffer");
971                         __jbd2_journal_refile_buffer(jh);
972                         if (!jh->b_transaction) {
973                                 jbd_unlock_bh_state(bh);
974                                  /* needs a brelse */
975                                 jbd2_journal_remove_journal_head(bh);
976                                 release_buffer_page(bh);
977                         } else
978                                 jbd_unlock_bh_state(bh);
979                 }
980                 cond_resched_lock(&journal->j_list_lock);
981         }
982         spin_unlock(&journal->j_list_lock);
983         /*
984          * This is a bit sleazy.  We use j_list_lock to protect transition
985          * of a transaction into T_FINISHED state and calling
986          * __jbd2_journal_drop_transaction(). Otherwise we could race with
987          * other checkpointing code processing the transaction...
988          */
989         spin_lock(&journal->j_state_lock);
990         spin_lock(&journal->j_list_lock);
991         /*
992          * Now recheck if some buffers did not get attached to the transaction
993          * while the lock was dropped...
994          */
995         if (commit_transaction->t_forget) {
996                 spin_unlock(&journal->j_list_lock);
997                 spin_unlock(&journal->j_state_lock);
998                 goto restart_loop;
999         }
1000
1001         /* Done with this transaction! */
1002
1003         jbd_debug(3, "JBD: commit phase 8\n");
1004
1005         J_ASSERT(commit_transaction->t_state == T_COMMIT);
1006
1007         commit_transaction->t_start = jiffies;
1008         stats.u.run.rs_logging = jbd2_time_diff(stats.u.run.rs_logging,
1009                                                 commit_transaction->t_start);
1010
1011         /*
1012          * File the transaction for history
1013          */
1014         stats.ts_type = JBD2_STATS_RUN;
1015         stats.ts_tid = commit_transaction->t_tid;
1016         stats.u.run.rs_handle_count = commit_transaction->t_handle_count;
1017         spin_lock(&journal->j_history_lock);
1018         memcpy(journal->j_history + journal->j_history_cur, &stats,
1019                         sizeof(stats));
1020         if (++journal->j_history_cur == journal->j_history_max)
1021                 journal->j_history_cur = 0;
1022
1023         /*
1024          * Calculate overall stats
1025          */
1026         journal->j_stats.ts_tid++;
1027         journal->j_stats.u.run.rs_wait += stats.u.run.rs_wait;
1028         journal->j_stats.u.run.rs_running += stats.u.run.rs_running;
1029         journal->j_stats.u.run.rs_locked += stats.u.run.rs_locked;
1030         journal->j_stats.u.run.rs_flushing += stats.u.run.rs_flushing;
1031         journal->j_stats.u.run.rs_logging += stats.u.run.rs_logging;
1032         journal->j_stats.u.run.rs_handle_count += stats.u.run.rs_handle_count;
1033         journal->j_stats.u.run.rs_blocks += stats.u.run.rs_blocks;
1034         journal->j_stats.u.run.rs_blocks_logged += stats.u.run.rs_blocks_logged;
1035         spin_unlock(&journal->j_history_lock);
1036
1037         commit_transaction->t_state = T_FINISHED;
1038         J_ASSERT(commit_transaction == journal->j_committing_transaction);
1039         journal->j_commit_sequence = commit_transaction->t_tid;
1040         journal->j_committing_transaction = NULL;
1041         spin_unlock(&journal->j_state_lock);
1042
1043         if (commit_transaction->t_checkpoint_list == NULL &&
1044             commit_transaction->t_checkpoint_io_list == NULL) {
1045                 __jbd2_journal_drop_transaction(journal, commit_transaction);
1046         } else {
1047                 if (journal->j_checkpoint_transactions == NULL) {
1048                         journal->j_checkpoint_transactions = commit_transaction;
1049                         commit_transaction->t_cpnext = commit_transaction;
1050                         commit_transaction->t_cpprev = commit_transaction;
1051                 } else {
1052                         commit_transaction->t_cpnext =
1053                                 journal->j_checkpoint_transactions;
1054                         commit_transaction->t_cpprev =
1055                                 commit_transaction->t_cpnext->t_cpprev;
1056                         commit_transaction->t_cpnext->t_cpprev =
1057                                 commit_transaction;
1058                         commit_transaction->t_cpprev->t_cpnext =
1059                                 commit_transaction;
1060                 }
1061         }
1062         spin_unlock(&journal->j_list_lock);
1063
1064         jbd_debug(1, "JBD: commit %d complete, head %d\n",
1065                   journal->j_commit_sequence, journal->j_tail_sequence);
1066
1067         wake_up(&journal->j_wait_done_commit);
1068 }