OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [fs/] [jbd/] [commit.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 * linux/fs/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/sched.h>
17
#include <linux/fs.h>
18
#include <linux/jbd.h>
19
#include <linux/errno.h>
20
#include <linux/slab.h>
21
#include <linux/locks.h>
22
#include <linux/mm.h>
23
#include <linux/pagemap.h>
24
#include <linux/smp_lock.h>
25
 
26
extern spinlock_t journal_datalist_lock;
27
 
28
/*
29
 * Default IO end handler for temporary BJ_IO buffer_heads.
30
 */
31
void journal_end_buffer_io_sync(struct buffer_head *bh, int uptodate)
32
{
33
        BUFFER_TRACE(bh, "");
34
        mark_buffer_uptodate(bh, uptodate);
35
        unlock_buffer(bh);
36
}
37
 
38
/*
39
 * When an ext3-ordered file is truncated, it is possible that many pages are
40
 * not sucessfully freed, because they are attached to a committing transaction.
41
 * After the transaction commits, these pages are left on the LRU, with no
42
 * ->mapping, and with attached buffers.  These pages are trivially reclaimable
43
 * by the VM, but their apparent absence upsets the VM accounting, and it makes
44
 * the numbers in /proc/meminfo look odd.
45
 *
46
 * So here, we have a buffer which has just come off the forget list.  Look to
47
 * see if we can strip all buffers from the backing page.
48
 *
49
 * Called under lock_journal(), and possibly under journal_datalist_lock.  The
50
 * caller provided us with a ref against the buffer, and we drop that here.
51
 */
52
static void release_buffer_page(struct buffer_head *bh)
53
{
54
        struct page *page;
55
 
56
        if (buffer_dirty(bh))
57
                goto nope;
58
        if (atomic_read(&bh->b_count) != 1)
59
                goto nope;
60
        page = bh->b_page;
61
        if (!page)
62
                goto nope;
63
        if (page->mapping)
64
                goto nope;
65
 
66
        /* OK, it's a truncated page */
67
        if (TryLockPage(page))
68
                goto nope;
69
 
70
        page_cache_get(page);
71
        __brelse(bh);
72
        try_to_free_buffers(page, GFP_NOIO);
73
        unlock_page(page);
74
        page_cache_release(page);
75
        return;
76
 
77
nope:
78
        __brelse(bh);
79
}
80
 
81
/*
82
 * journal_commit_transaction
83
 *
84
 * The primary function for committing a transaction to the log.  This
85
 * function is called by the journal thread to begin a complete commit.
86
 */
87
void journal_commit_transaction(journal_t *journal)
88
{
89
        transaction_t *commit_transaction;
90
        struct journal_head *jh, *new_jh, *descriptor;
91
        struct journal_head *next_jh, *last_jh;
92
        struct buffer_head *wbuf[64];
93
        int bufs;
94
        int flags;
95
        int err;
96
        unsigned long blocknr;
97
        char *tagp = NULL;
98
        journal_header_t *header;
99
        journal_block_tag_t *tag = NULL;
100
        int space_left = 0;
101
        int first_tag = 0;
102
        int tag_flag;
103
        int i;
104
 
105
        /*
106
         * First job: lock down the current transaction and wait for
107
         * all outstanding updates to complete.
108
         */
109
 
110
        lock_journal(journal); /* Protect journal->j_running_transaction */
111
 
112
#ifdef COMMIT_STATS
113
        spin_lock(&journal_datalist_lock);
114
        summarise_journal_usage(journal);
115
        spin_unlock(&journal_datalist_lock);
116
#endif
117
 
118
        lock_kernel();
119
 
120
        J_ASSERT (journal->j_running_transaction != NULL);
121
        J_ASSERT (journal->j_committing_transaction == NULL);
122
 
123
        commit_transaction = journal->j_running_transaction;
124
        J_ASSERT (commit_transaction->t_state == T_RUNNING);
125
 
126
        jbd_debug (1, "JBD: starting commit of transaction %d\n",
127
                   commit_transaction->t_tid);
128
 
129
        commit_transaction->t_state = T_LOCKED;
130
        while (commit_transaction->t_updates != 0) {
131
                unlock_journal(journal);
132
                sleep_on(&journal->j_wait_updates);
133
                lock_journal(journal);
134
        }
135
 
136
        J_ASSERT (commit_transaction->t_outstanding_credits <=
137
                        journal->j_max_transaction_buffers);
138
 
139
        /* Do we need to erase the effects of a prior journal_flush? */
140
        if (journal->j_flags & JFS_FLUSHED) {
141
                jbd_debug(3, "super block updated\n");
142
                journal_update_superblock(journal, 1);
143
        } else {
144
                jbd_debug(3, "superblock not updated\n");
145
        }
146
 
147
        /*
148
         * First thing we are allowed to do is to discard any remaining
149
         * BJ_Reserved buffers.  Note, it is _not_ permissible to assume
150
         * that there are no such buffers: if a large filesystem
151
         * operation like a truncate needs to split itself over multiple
152
         * transactions, then it may try to do a journal_restart() while
153
         * there are still BJ_Reserved buffers outstanding.  These must
154
         * be released cleanly from the current transaction.
155
         *
156
         * In this case, the filesystem must still reserve write access
157
         * again before modifying the buffer in the new transaction, but
158
         * we do not require it to remember exactly which old buffers it
159
         * has reserved.  This is consistent with the existing behaviour
160
         * that multiple journal_get_write_access() calls to the same
161
         * buffer are perfectly permissable.
162
         */
163
 
164
        while (commit_transaction->t_reserved_list) {
165
                jh = commit_transaction->t_reserved_list;
166
                JBUFFER_TRACE(jh, "reserved, unused: refile");
167
                journal_refile_buffer(jh);
168
        }
169
 
170
        /*
171
         * Now try to drop any written-back buffers from the journal's
172
         * checkpoint lists.  We do this *before* commit because it potentially
173
         * frees some memory
174
         */
175
        spin_lock(&journal_datalist_lock);
176
        __journal_clean_checkpoint_list(journal);
177
        spin_unlock(&journal_datalist_lock);
178
 
179
        /* First part of the commit: force the revoke list out to disk.
180
         * The revoke code generates its own metadata blocks on disk for this.
181
         *
182
         * It is important that we do this while the transaction is
183
         * still locked.  Generating the revoke records should not
184
         * generate any IO stalls, so this should be quick; and doing
185
         * the work while we have the transaction locked means that we
186
         * only ever have to maintain the revoke list for one
187
         * transaction at a time.
188
         */
189
 
190
        jbd_debug (3, "JBD: commit phase 1\n");
191
 
192
        journal_write_revoke_records(journal, commit_transaction);
193
 
194
        /*
195
         * Now that we have built the revoke records, we can start
196
         * reusing the revoke list for a new running transaction.  We
197
         * can now safely start committing the old transaction: time to
198
         * get a new running transaction for incoming filesystem updates
199
         */
200
 
201
        commit_transaction->t_state = T_FLUSH;
202
 
203
        wake_up(&journal->j_wait_transaction_locked);
204
 
205
        journal->j_committing_transaction = commit_transaction;
206
        journal->j_running_transaction = NULL;
207
 
208
        commit_transaction->t_log_start = journal->j_head;
209
 
210
        unlock_kernel();
211
 
212
        jbd_debug (3, "JBD: commit phase 2\n");
213
 
214
        /*
215
         * Now start flushing things to disk, in the order they appear
216
         * on the transaction lists.  Data blocks go first.
217
         */
218
 
219
        /*
220
         * Whenever we unlock the journal and sleep, things can get added
221
         * onto ->t_datalist, so we have to keep looping back to write_out_data
222
         * until we *know* that the list is empty.
223
         */
224
write_out_data:
225
 
226
        /*
227
         * Cleanup any flushed data buffers from the data list.  Even in
228
         * abort mode, we want to flush this out as soon as possible.
229
         *
230
         * We take journal_datalist_lock to protect the lists from
231
         * journal_try_to_free_buffers().
232
         */
233
        spin_lock(&journal_datalist_lock);
234
 
235
write_out_data_locked:
236
        bufs = 0;
237
        next_jh = commit_transaction->t_sync_datalist;
238
        if (next_jh == NULL)
239
                goto sync_datalist_empty;
240
        last_jh = next_jh->b_tprev;
241
 
242
        do {
243
                struct buffer_head *bh;
244
 
245
                jh = next_jh;
246
                next_jh = jh->b_tnext;
247
                bh = jh2bh(jh);
248
                if (!buffer_locked(bh)) {
249
                        if (buffer_dirty(bh)) {
250
                                BUFFER_TRACE(bh, "start journal writeout");
251
                                atomic_inc(&bh->b_count);
252
                                wbuf[bufs++] = bh;
253
                        } else {
254
                                BUFFER_TRACE(bh, "writeout complete: unfile");
255
                                __journal_unfile_buffer(jh);
256
                                jh->b_transaction = NULL;
257
                                __journal_remove_journal_head(bh);
258
                                refile_buffer(bh);
259
                                release_buffer_page(bh);
260
                        }
261
                }
262
                if (bufs == ARRAY_SIZE(wbuf)) {
263
                        /*
264
                         * Major speedup: start here on the next scan
265
                         */
266
                        J_ASSERT(commit_transaction->t_sync_datalist != 0);
267
                        commit_transaction->t_sync_datalist = jh;
268
                        break;
269
                }
270
        } while (jh != last_jh);
271
 
272
        if (bufs || current->need_resched) {
273
                jbd_debug(2, "submit %d writes\n", bufs);
274
                spin_unlock(&journal_datalist_lock);
275
                unlock_journal(journal);
276
                if (bufs)
277
                        ll_rw_block(WRITE, bufs, wbuf);
278
                if (current->need_resched)
279
                        schedule();
280
                journal_brelse_array(wbuf, bufs);
281
                lock_journal(journal);
282
                spin_lock(&journal_datalist_lock);
283
                if (bufs)
284
                        goto write_out_data_locked;
285
        }
286
 
287
        /*
288
         * Wait for all previously submitted IO on the data list to complete.
289
         */
290
        jh = commit_transaction->t_sync_datalist;
291
        if (jh == NULL)
292
                goto sync_datalist_empty;
293
 
294
        do {
295
                struct buffer_head *bh;
296
                jh = jh->b_tprev;       /* Wait on the last written */
297
                bh = jh2bh(jh);
298
                if (buffer_locked(bh)) {
299
                        spin_unlock(&journal_datalist_lock);
300
                        unlock_journal(journal);
301
                        wait_on_buffer(bh);
302
                        /* the journal_head may have been removed now */
303
                        lock_journal(journal);
304
                        goto write_out_data;
305
                } else if (buffer_dirty(bh)) {
306
                        goto write_out_data_locked;
307
                }
308
        } while (jh != commit_transaction->t_sync_datalist);
309
        goto write_out_data_locked;
310
 
311
sync_datalist_empty:
312
        /*
313
         * Wait for all the async writepage data.  As they become unlocked
314
         * in end_buffer_io_async(), the only place where they can be
315
         * reaped is in try_to_free_buffers(), and we're locked against
316
         * that.
317
         */
318
        while ((jh = commit_transaction->t_async_datalist)) {
319
                struct buffer_head *bh = jh2bh(jh);
320
                if (__buffer_state(bh, Freed)) {
321
                        BUFFER_TRACE(bh, "Cleaning freed buffer");
322
                        clear_bit(BH_Freed, &bh->b_state);
323
                        clear_bit(BH_Dirty, &bh->b_state);
324
                }
325
                if (buffer_locked(bh)) {
326
                        spin_unlock(&journal_datalist_lock);
327
                        unlock_journal(journal);
328
                        wait_on_buffer(bh);
329
                        lock_journal(journal);
330
                        spin_lock(&journal_datalist_lock);
331
                        continue;       /* List may have changed */
332
                }
333
                if (jh->b_next_transaction) {
334
                        /*
335
                         * For writepage() buffers in journalled data mode: a
336
                         * later transaction may want the buffer for "metadata"
337
                         */
338
                        __journal_refile_buffer(jh);
339
                } else {
340
                        BUFFER_TRACE(bh, "finished async writeout: unfile");
341
                        __journal_unfile_buffer(jh);
342
                        jh->b_transaction = NULL;
343
                        __journal_remove_journal_head(bh);
344
                        BUFFER_TRACE(bh, "finished async writeout: refile");
345
                        /* It can sometimes be on BUF_LOCKED due to migration
346
                         * from syncdata to asyncdata */
347
                        if (bh->b_list != BUF_CLEAN)
348
                                refile_buffer(bh);
349
                        __brelse(bh);
350
                }
351
        }
352
        spin_unlock(&journal_datalist_lock);
353
 
354
        /*
355
         * If we found any dirty or locked buffers, then we should have
356
         * looped back up to the write_out_data label.  If there weren't
357
         * any then journal_clean_data_list should have wiped the list
358
         * clean by now, so check that it is in fact empty.
359
         */
360
        J_ASSERT (commit_transaction->t_sync_datalist == NULL);
361
        J_ASSERT (commit_transaction->t_async_datalist == NULL);
362
 
363
        jbd_debug (3, "JBD: commit phase 3\n");
364
 
365
        /*
366
         * Way to go: we have now written out all of the data for a
367
         * transaction!  Now comes the tricky part: we need to write out
368
         * metadata.  Loop over the transaction's entire buffer list:
369
         */
370
        commit_transaction->t_state = T_COMMIT;
371
 
372
        descriptor = 0;
373
        bufs = 0;
374
        while (commit_transaction->t_buffers) {
375
 
376
                /* Find the next buffer to be journaled... */
377
 
378
                jh = commit_transaction->t_buffers;
379
 
380
                /* If we're in abort mode, we just un-journal the buffer and
381
                   release it for background writing. */
382
 
383
                if (is_journal_aborted(journal)) {
384
                        JBUFFER_TRACE(jh, "journal is aborting: refile");
385
                        journal_refile_buffer(jh);
386
                        /* If that was the last one, we need to clean up
387
                         * any descriptor buffers which may have been
388
                         * already allocated, even if we are now
389
                         * aborting. */
390
                        if (!commit_transaction->t_buffers)
391
                                goto start_journal_io;
392
                        continue;
393
                }
394
 
395
                /* Make sure we have a descriptor block in which to
396
                   record the metadata buffer. */
397
 
398
                if (!descriptor) {
399
                        struct buffer_head *bh;
400
 
401
                        J_ASSERT (bufs == 0);
402
 
403
                        jbd_debug(4, "JBD: get descriptor\n");
404
 
405
                        descriptor = journal_get_descriptor_buffer(journal);
406
                        if (!descriptor) {
407
                                __journal_abort_hard(journal);
408
                                continue;
409
                        }
410
 
411
                        bh = jh2bh(descriptor);
412
                        jbd_debug(4, "JBD: got buffer %ld (%p)\n",
413
                                bh->b_blocknr, bh->b_data);
414
                        header = (journal_header_t *)&bh->b_data[0];
415
                        header->h_magic     = htonl(JFS_MAGIC_NUMBER);
416
                        header->h_blocktype = htonl(JFS_DESCRIPTOR_BLOCK);
417
                        header->h_sequence  = htonl(commit_transaction->t_tid);
418
 
419
                        tagp = &bh->b_data[sizeof(journal_header_t)];
420
                        space_left = bh->b_size - sizeof(journal_header_t);
421
                        first_tag = 1;
422
                        set_bit(BH_JWrite, &bh->b_state);
423
                        wbuf[bufs++] = bh;
424
 
425
                        /* Record it so that we can wait for IO
426
                           completion later */
427
                        BUFFER_TRACE(bh, "ph3: file as descriptor");
428
                        journal_file_buffer(descriptor, commit_transaction,
429
                                                BJ_LogCtl);
430
                }
431
 
432
                /* Where is the buffer to be written? */
433
 
434
                err = journal_next_log_block(journal, &blocknr);
435
                /* If the block mapping failed, just abandon the buffer
436
                   and repeat this loop: we'll fall into the
437
                   refile-on-abort condition above. */
438
                if (err) {
439
                        __journal_abort_hard(journal);
440
                        continue;
441
                }
442
 
443
                /* Bump b_count to prevent truncate from stumbling over
444
                   the shadowed buffer!  @@@ This can go if we ever get
445
                   rid of the BJ_IO/BJ_Shadow pairing of buffers. */
446
                atomic_inc(&jh2bh(jh)->b_count);
447
 
448
                /* Make a temporary IO buffer with which to write it out
449
                   (this will requeue both the metadata buffer and the
450
                   temporary IO buffer). new_bh goes on BJ_IO*/
451
 
452
                set_bit(BH_JWrite, &jh2bh(jh)->b_state);
453
                /*
454
                 * akpm: journal_write_metadata_buffer() sets
455
                 * new_bh->b_transaction to commit_transaction.
456
                 * We need to clean this up before we release new_bh
457
                 * (which is of type BJ_IO)
458
                 */
459
                JBUFFER_TRACE(jh, "ph3: write metadata");
460
                flags = journal_write_metadata_buffer(commit_transaction,
461
                                                      jh, &new_jh, blocknr);
462
                set_bit(BH_JWrite, &jh2bh(new_jh)->b_state);
463
                set_bit(BH_Lock, &jh2bh(new_jh)->b_state);
464
                wbuf[bufs++] = jh2bh(new_jh);
465
 
466
                /* Record the new block's tag in the current descriptor
467
                   buffer */
468
 
469
                tag_flag = 0;
470
                if (flags & 1)
471
                        tag_flag |= JFS_FLAG_ESCAPE;
472
                if (!first_tag)
473
                        tag_flag |= JFS_FLAG_SAME_UUID;
474
 
475
                tag = (journal_block_tag_t *) tagp;
476
                tag->t_blocknr = htonl(jh2bh(jh)->b_blocknr);
477
                tag->t_flags = htonl(tag_flag);
478
                tagp += sizeof(journal_block_tag_t);
479
                space_left -= sizeof(journal_block_tag_t);
480
 
481
                if (first_tag) {
482
                        memcpy (tagp, journal->j_uuid, 16);
483
                        tagp += 16;
484
                        space_left -= 16;
485
                        first_tag = 0;
486
                }
487
 
488
                /* If there's no more to do, or if the descriptor is full,
489
                   let the IO rip! */
490
 
491
                if (bufs == ARRAY_SIZE(wbuf) ||
492
                    commit_transaction->t_buffers == NULL ||
493
                    space_left < sizeof(journal_block_tag_t) + 16) {
494
 
495
                        jbd_debug(4, "JBD: Submit %d IOs\n", bufs);
496
 
497
                        /* Write an end-of-descriptor marker before
498
                           submitting the IOs.  "tag" still points to
499
                           the last tag we set up. */
500
 
501
                        tag->t_flags |= htonl(JFS_FLAG_LAST_TAG);
502
 
503
start_journal_io:
504
                        unlock_journal(journal);
505
                        for (i=0; i<bufs; i++) {
506
                                struct buffer_head *bh = wbuf[i];
507
                                clear_bit(BH_Dirty, &bh->b_state);
508
                                bh->b_end_io = journal_end_buffer_io_sync;
509
                                submit_bh(WRITE, bh);
510
                        }
511
                        if (current->need_resched)
512
                                schedule();
513
                        lock_journal(journal);
514
 
515
                        /* Force a new descriptor to be generated next
516
                           time round the loop. */
517
                        descriptor = NULL;
518
                        bufs = 0;
519
                }
520
        }
521
 
522
        /* Lo and behold: we have just managed to send a transaction to
523
           the log.  Before we can commit it, wait for the IO so far to
524
           complete.  Control buffers being written are on the
525
           transaction's t_log_list queue, and metadata buffers are on
526
           the t_iobuf_list queue.
527
 
528
           Wait for the buffers in reverse order.  That way we are
529
           less likely to be woken up until all IOs have completed, and
530
           so we incur less scheduling load.
531
        */
532
 
533
        jbd_debug(3, "JBD: commit phase 4\n");
534
 
535
        /* akpm: these are BJ_IO, and journal_datalist_lock is not needed */
536
 wait_for_iobuf:
537
        while (commit_transaction->t_iobuf_list != NULL) {
538
                struct buffer_head *bh;
539
                jh = commit_transaction->t_iobuf_list->b_tprev;
540
                bh = jh2bh(jh);
541
                if (buffer_locked(bh)) {
542
                        unlock_journal(journal);
543
                        wait_on_buffer(bh);
544
                        lock_journal(journal);
545
                        goto wait_for_iobuf;
546
                }
547
 
548
                clear_bit(BH_JWrite, &jh2bh(jh)->b_state);
549
 
550
                JBUFFER_TRACE(jh, "ph4: unfile after journal write");
551
                journal_unfile_buffer(jh);
552
 
553
                /*
554
                 * akpm: don't put back a buffer_head with stale pointers
555
                 * dangling around.
556
                 */
557
                J_ASSERT_JH(jh, jh->b_transaction != NULL);
558
                jh->b_transaction = NULL;
559
 
560
                /*
561
                 * ->t_iobuf_list should contain only dummy buffer_heads
562
                 * which were created by journal_write_metadata_buffer().
563
                 */
564
                bh = jh2bh(jh);
565
                BUFFER_TRACE(bh, "dumping temporary bh");
566
                journal_unlock_journal_head(jh);
567
                __brelse(bh);
568
                J_ASSERT_BH(bh, atomic_read(&bh->b_count) == 0);
569
                put_unused_buffer_head(bh);
570
 
571
                /* We also have to unlock and free the corresponding
572
                   shadowed buffer */
573
                jh = commit_transaction->t_shadow_list->b_tprev;
574
                bh = jh2bh(jh);
575
                clear_bit(BH_JWrite, &bh->b_state);
576
                J_ASSERT_BH(bh, buffer_jdirty(bh));
577
 
578
                /* The metadata is now released for reuse, but we need
579
                   to remember it against this transaction so that when
580
                   we finally commit, we can do any checkpointing
581
                   required. */
582
                JBUFFER_TRACE(jh, "file as BJ_Forget");
583
                journal_file_buffer(jh, commit_transaction, BJ_Forget);
584
                /* Wake up any transactions which were waiting for this
585
                   IO to complete */
586
                wake_up(&bh->b_wait);
587
                JBUFFER_TRACE(jh, "brelse shadowed buffer");
588
                __brelse(bh);
589
        }
590
 
591
        J_ASSERT (commit_transaction->t_shadow_list == NULL);
592
 
593
        jbd_debug(3, "JBD: commit phase 5\n");
594
 
595
        /* Here we wait for the revoke record and descriptor record buffers */
596
 wait_for_ctlbuf:
597
        while (commit_transaction->t_log_list != NULL) {
598
                struct buffer_head *bh;
599
 
600
                jh = commit_transaction->t_log_list->b_tprev;
601
                bh = jh2bh(jh);
602
                if (buffer_locked(bh)) {
603
                        unlock_journal(journal);
604
                        wait_on_buffer(bh);
605
                        lock_journal(journal);
606
                        goto wait_for_ctlbuf;
607
                }
608
 
609
                BUFFER_TRACE(bh, "ph5: control buffer writeout done: unfile");
610
                clear_bit(BH_JWrite, &bh->b_state);
611
                journal_unfile_buffer(jh);
612
                jh->b_transaction = NULL;
613
                journal_unlock_journal_head(jh);
614
                put_bh(bh);                     /* One for getblk */
615
        }
616
 
617
        jbd_debug(3, "JBD: commit phase 6\n");
618
 
619
        if (is_journal_aborted(journal)) {
620
                unlock_journal(journal);
621
                goto skip_commit;
622
        }
623
 
624
        /* Done it all: now write the commit record.  We should have
625
         * cleaned up our previous buffers by now, so if we are in abort
626
         * mode we can now just skip the rest of the journal write
627
         * entirely. */
628
 
629
        descriptor = journal_get_descriptor_buffer(journal);
630
        if (!descriptor) {
631
                __journal_abort_hard(journal);
632
                unlock_journal(journal);
633
                goto skip_commit;
634
        }
635
 
636
        /* AKPM: buglet - add `i' to tmp! */
637
        for (i = 0; i < jh2bh(descriptor)->b_size; i += 512) {
638
                journal_header_t *tmp =
639
                        (journal_header_t*)jh2bh(descriptor)->b_data;
640
                tmp->h_magic = htonl(JFS_MAGIC_NUMBER);
641
                tmp->h_blocktype = htonl(JFS_COMMIT_BLOCK);
642
                tmp->h_sequence = htonl(commit_transaction->t_tid);
643
        }
644
 
645
        unlock_journal(journal);
646
        JBUFFER_TRACE(descriptor, "write commit block");
647
        {
648
                struct buffer_head *bh = jh2bh(descriptor);
649
                clear_bit(BH_Dirty, &bh->b_state);
650
                bh->b_end_io = journal_end_buffer_io_sync;
651
                submit_bh(WRITE, bh);
652
                wait_on_buffer(bh);
653
                put_bh(bh);             /* One for getblk() */
654
                journal_unlock_journal_head(descriptor);
655
        }
656
 
657
        /* End of a transaction!  Finally, we can do checkpoint
658
           processing: any buffers committed as a result of this
659
           transaction can be removed from any checkpoint list it was on
660
           before. */
661
 
662
skip_commit: /* The journal should be unlocked by now. */
663
 
664
        /* Call any callbacks that had been registered for handles in this
665
         * transaction.  It is up to the callback to free any allocated
666
         * memory.
667
         */
668
        if (!list_empty(&commit_transaction->t_jcb)) {
669
                struct list_head *p, *n;
670
                int error = is_journal_aborted(journal);
671
 
672
                list_for_each_safe(p, n, &commit_transaction->t_jcb) {
673
                        struct journal_callback *jcb;
674
 
675
                        jcb = list_entry(p, struct journal_callback, jcb_list);
676
                        list_del(p);
677
                        jcb->jcb_func(jcb, error);
678
                }
679
        }
680
 
681
        lock_journal(journal);
682
 
683
        jbd_debug(3, "JBD: commit phase 7\n");
684
 
685
        J_ASSERT(commit_transaction->t_sync_datalist == NULL);
686
        J_ASSERT(commit_transaction->t_async_datalist == NULL);
687
        J_ASSERT(commit_transaction->t_buffers == NULL);
688
        J_ASSERT(commit_transaction->t_checkpoint_list == NULL);
689
        J_ASSERT(commit_transaction->t_iobuf_list == NULL);
690
        J_ASSERT(commit_transaction->t_shadow_list == NULL);
691
        J_ASSERT(commit_transaction->t_log_list == NULL);
692
 
693
        while (commit_transaction->t_forget) {
694
                transaction_t *cp_transaction;
695
                struct buffer_head *bh;
696
                int was_freed = 0;
697
 
698
                jh = commit_transaction->t_forget;
699
                J_ASSERT_JH(jh, jh->b_transaction == commit_transaction ||
700
                        jh->b_transaction == journal->j_running_transaction);
701
 
702
                /*
703
                 * If there is undo-protected committed data against
704
                 * this buffer, then we can remove it now.  If it is a
705
                 * buffer needing such protection, the old frozen_data
706
                 * field now points to a committed version of the
707
                 * buffer, so rotate that field to the new committed
708
                 * data.
709
                 *
710
                 * Otherwise, we can just throw away the frozen data now.
711
                 */
712
                if (jh->b_committed_data) {
713
                        kfree(jh->b_committed_data);
714
                        jh->b_committed_data = NULL;
715
                        if (jh->b_frozen_data) {
716
                                jh->b_committed_data = jh->b_frozen_data;
717
                                jh->b_frozen_data = NULL;
718
                        }
719
                } else if (jh->b_frozen_data) {
720
                        kfree(jh->b_frozen_data);
721
                        jh->b_frozen_data = NULL;
722
                }
723
 
724
                spin_lock(&journal_datalist_lock);
725
                cp_transaction = jh->b_cp_transaction;
726
                if (cp_transaction) {
727
                        JBUFFER_TRACE(jh, "remove from old cp transaction");
728
                        J_ASSERT_JH(jh, commit_transaction != cp_transaction);
729
                        __journal_remove_checkpoint(jh);
730
                }
731
 
732
                /* Only re-checkpoint the buffer_head if it is marked
733
                 * dirty.  If the buffer was added to the BJ_Forget list
734
                 * by journal_forget, it may no longer be dirty and
735
                 * there's no point in keeping a checkpoint record for
736
                 * it. */
737
                bh = jh2bh(jh);
738
 
739
                /* A buffer which has been freed while still being
740
                 * journaled by a previous transaction may end up still
741
                 * being dirty here, but we want to avoid writing back
742
                 * that buffer in the future now that the last use has
743
                 * been committed.  That's not only a performance gain,
744
                 * it also stops aliasing problems if the buffer is left
745
                 * behind for writeback and gets reallocated for another
746
                 * use in a different page. */
747
                if (__buffer_state(bh, Freed)) {
748
                        was_freed = 1;
749
                        clear_bit(BH_Freed, &bh->b_state);
750
                        clear_bit(BH_JBDDirty, &bh->b_state);
751
                }
752
 
753
                if (buffer_jdirty(bh)) {
754
                        JBUFFER_TRACE(jh, "add to new checkpointing trans");
755
                        __journal_insert_checkpoint(jh, commit_transaction);
756
                        JBUFFER_TRACE(jh, "refile for checkpoint writeback");
757
                        __journal_refile_buffer(jh);
758
                } else {
759
                        J_ASSERT_BH(bh, !buffer_dirty(bh));
760
                        J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
761
                        __journal_unfile_buffer(jh);
762
                        jh->b_transaction = 0;
763
                        __journal_remove_journal_head(bh);
764
                        spin_unlock(&journal_datalist_lock);
765
                        if (was_freed)
766
                                release_buffer_page(bh);
767
                        else
768
                                __brelse(bh);
769
                        continue;
770
                }
771
                spin_unlock(&journal_datalist_lock);
772
        }
773
 
774
        /* Done with this transaction! */
775
 
776
        jbd_debug(3, "JBD: commit phase 8\n");
777
 
778
        J_ASSERT (commit_transaction->t_state == T_COMMIT);
779
        commit_transaction->t_state = T_FINISHED;
780
 
781
        J_ASSERT (commit_transaction == journal->j_committing_transaction);
782
        journal->j_commit_sequence = commit_transaction->t_tid;
783
        journal->j_committing_transaction = NULL;
784
 
785
        spin_lock(&journal_datalist_lock);
786
        if (commit_transaction->t_checkpoint_list == NULL) {
787
                __journal_drop_transaction(journal, commit_transaction);
788
        } else {
789
                if (journal->j_checkpoint_transactions == NULL) {
790
                        journal->j_checkpoint_transactions = commit_transaction;
791
                        commit_transaction->t_cpnext = commit_transaction;
792
                        commit_transaction->t_cpprev = commit_transaction;
793
                } else {
794
                        commit_transaction->t_cpnext =
795
                                journal->j_checkpoint_transactions;
796
                        commit_transaction->t_cpprev =
797
                                commit_transaction->t_cpnext->t_cpprev;
798
                        commit_transaction->t_cpnext->t_cpprev =
799
                                commit_transaction;
800
                        commit_transaction->t_cpprev->t_cpnext =
801
                                commit_transaction;
802
                }
803
        }
804
        spin_unlock(&journal_datalist_lock);
805
 
806
        jbd_debug(1, "JBD: commit %d complete, head %d\n",
807
                  journal->j_commit_sequence, journal->j_tail_sequence);
808
 
809
        unlock_journal(journal);
810
        wake_up(&journal->j_wait_done_commit);
811
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.