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