~ubuntu-branches/debian/wheezy/linux-2.6/wheezy

« back to all changes in this revision

Viewing changes to fs/jbd2/transaction.c

  • Committer: Bazaar Package Importer
  • Author(s): Ben Hutchings, Ben Hutchings, Aurelien Jarno, Martin Michlmayr
  • Date: 2011-04-06 13:53:30 UTC
  • mfrom: (43.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20110406135330-wjufxhd0tvn3zx4z
Tags: 2.6.38-3
[ Ben Hutchings ]
* [ppc64] Add to linux-tools package architectures (Closes: #620124)
* [amd64] Save cr4 to mmu_cr4_features at boot time (Closes: #620284)
* appletalk: Fix bugs introduced when removing use of BKL
* ALSA: Fix yet another race in disconnection
* cciss: Fix lost command issue
* ath9k: Fix kernel panic in AR2427
* ses: Avoid kernel panic when lun 0 is not mapped
* PCI/ACPI: Report ASPM support to BIOS if not disabled from command line

[ Aurelien Jarno ]
* rtlwifi: fix build when PCI is not enabled.

[ Martin Michlmayr ]
* rtlwifi: Eliminate udelay calls with too large values (Closes: #620204)

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
#include <linux/mm.h>
27
27
#include <linux/highmem.h>
28
28
#include <linux/hrtimer.h>
 
29
#include <linux/backing-dev.h>
 
30
#include <linux/module.h>
29
31
 
30
32
static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh);
31
33
 
53
55
        transaction->t_tid = journal->j_transaction_sequence++;
54
56
        transaction->t_expires = jiffies + journal->j_commit_interval;
55
57
        spin_lock_init(&transaction->t_handle_lock);
 
58
        atomic_set(&transaction->t_updates, 0);
 
59
        atomic_set(&transaction->t_outstanding_credits, 0);
 
60
        atomic_set(&transaction->t_handle_count, 0);
56
61
        INIT_LIST_HEAD(&transaction->t_inode_list);
57
62
        INIT_LIST_HEAD(&transaction->t_private_list);
58
63
 
77
82
 */
78
83
 
79
84
/*
 
85
 * Update transiaction's maximum wait time, if debugging is enabled.
 
86
 *
 
87
 * In order for t_max_wait to be reliable, it must be protected by a
 
88
 * lock.  But doing so will mean that start_this_handle() can not be
 
89
 * run in parallel on SMP systems, which limits our scalability.  So
 
90
 * unless debugging is enabled, we no longer update t_max_wait, which
 
91
 * means that maximum wait time reported by the jbd2_run_stats
 
92
 * tracepoint will always be zero.
 
93
 */
 
94
static inline void update_t_max_wait(transaction_t *transaction)
 
95
{
 
96
#ifdef CONFIG_JBD2_DEBUG
 
97
        unsigned long ts = jiffies;
 
98
 
 
99
        if (jbd2_journal_enable_debug &&
 
100
            time_after(transaction->t_start, ts)) {
 
101
                ts = jbd2_time_diff(ts, transaction->t_start);
 
102
                spin_lock(&transaction->t_handle_lock);
 
103
                if (ts > transaction->t_max_wait)
 
104
                        transaction->t_max_wait = ts;
 
105
                spin_unlock(&transaction->t_handle_lock);
 
106
        }
 
107
#endif
 
108
}
 
109
 
 
110
/*
80
111
 * start_this_handle: Given a handle, deal with any locking or stalling
81
112
 * needed to make sure that there is enough journal space for the handle
82
113
 * to begin.  Attach the handle to a transaction and set up the
83
114
 * transaction's buffer credits.
84
115
 */
85
116
 
86
 
static int start_this_handle(journal_t *journal, handle_t *handle)
 
117
static int start_this_handle(journal_t *journal, handle_t *handle,
 
118
                             int gfp_mask)
87
119
{
88
 
        transaction_t *transaction;
89
 
        int needed;
90
 
        int nblocks = handle->h_buffer_credits;
91
 
        transaction_t *new_transaction = NULL;
92
 
        int ret = 0;
93
 
        unsigned long ts = jiffies;
 
120
        transaction_t   *transaction, *new_transaction = NULL;
 
121
        tid_t           tid;
 
122
        int             needed, need_to_start;
 
123
        int             nblocks = handle->h_buffer_credits;
94
124
 
95
125
        if (nblocks > journal->j_max_transaction_buffers) {
96
126
                printk(KERN_ERR "JBD: %s wants too many credits (%d > %d)\n",
97
127
                       current->comm, nblocks,
98
128
                       journal->j_max_transaction_buffers);
99
 
                ret = -ENOSPC;
100
 
                goto out;
 
129
                return -ENOSPC;
101
130
        }
102
131
 
103
132
alloc_transaction:
104
133
        if (!journal->j_running_transaction) {
105
 
                new_transaction = kzalloc(sizeof(*new_transaction),
106
 
                                                GFP_NOFS|__GFP_NOFAIL);
 
134
                new_transaction = kzalloc(sizeof(*new_transaction), gfp_mask);
107
135
                if (!new_transaction) {
108
 
                        ret = -ENOMEM;
109
 
                        goto out;
 
136
                        /*
 
137
                         * If __GFP_FS is not present, then we may be
 
138
                         * being called from inside the fs writeback
 
139
                         * layer, so we MUST NOT fail.  Since
 
140
                         * __GFP_NOFAIL is going away, we will arrange
 
141
                         * to retry the allocation ourselves.
 
142
                         */
 
143
                        if ((gfp_mask & __GFP_FS) == 0) {
 
144
                                congestion_wait(BLK_RW_ASYNC, HZ/50);
 
145
                                goto alloc_transaction;
 
146
                        }
 
147
                        return -ENOMEM;
110
148
                }
111
149
        }
112
150
 
113
151
        jbd_debug(3, "New handle %p going live.\n", handle);
114
152
 
115
 
repeat:
116
 
 
117
153
        /*
118
154
         * We need to hold j_state_lock until t_updates has been incremented,
119
155
         * for proper journal barrier handling
120
156
         */
121
 
        spin_lock(&journal->j_state_lock);
122
 
repeat_locked:
 
157
repeat:
 
158
        read_lock(&journal->j_state_lock);
 
159
        BUG_ON(journal->j_flags & JBD2_UNMOUNT);
123
160
        if (is_journal_aborted(journal) ||
124
161
            (journal->j_errno != 0 && !(journal->j_flags & JBD2_ACK_ERR))) {
125
 
                spin_unlock(&journal->j_state_lock);
126
 
                ret = -EROFS;
127
 
                goto out;
 
162
                read_unlock(&journal->j_state_lock);
 
163
                kfree(new_transaction);
 
164
                return -EROFS;
128
165
        }
129
166
 
130
167
        /* Wait on the journal's transaction barrier if necessary */
131
168
        if (journal->j_barrier_count) {
132
 
                spin_unlock(&journal->j_state_lock);
 
169
                read_unlock(&journal->j_state_lock);
133
170
                wait_event(journal->j_wait_transaction_locked,
134
171
                                journal->j_barrier_count == 0);
135
172
                goto repeat;
136
173
        }
137
174
 
138
175
        if (!journal->j_running_transaction) {
139
 
                if (!new_transaction) {
140
 
                        spin_unlock(&journal->j_state_lock);
 
176
                read_unlock(&journal->j_state_lock);
 
177
                if (!new_transaction)
141
178
                        goto alloc_transaction;
 
179
                write_lock(&journal->j_state_lock);
 
180
                if (!journal->j_running_transaction) {
 
181
                        jbd2_get_transaction(journal, new_transaction);
 
182
                        new_transaction = NULL;
142
183
                }
143
 
                jbd2_get_transaction(journal, new_transaction);
144
 
                new_transaction = NULL;
 
184
                write_unlock(&journal->j_state_lock);
 
185
                goto repeat;
145
186
        }
146
187
 
147
188
        transaction = journal->j_running_transaction;
155
196
 
156
197
                prepare_to_wait(&journal->j_wait_transaction_locked,
157
198
                                        &wait, TASK_UNINTERRUPTIBLE);
158
 
                spin_unlock(&journal->j_state_lock);
 
199
                read_unlock(&journal->j_state_lock);
159
200
                schedule();
160
201
                finish_wait(&journal->j_wait_transaction_locked, &wait);
161
202
                goto repeat;
166
207
         * buffers requested by this operation, we need to stall pending a log
167
208
         * checkpoint to free some more log space.
168
209
         */
169
 
        spin_lock(&transaction->t_handle_lock);
170
 
        needed = transaction->t_outstanding_credits + nblocks;
 
210
        needed = atomic_add_return(nblocks,
 
211
                                   &transaction->t_outstanding_credits);
171
212
 
172
213
        if (needed > journal->j_max_transaction_buffers) {
173
214
                /*
178
219
                DEFINE_WAIT(wait);
179
220
 
180
221
                jbd_debug(2, "Handle %p starting new commit...\n", handle);
181
 
                spin_unlock(&transaction->t_handle_lock);
 
222
                atomic_sub(nblocks, &transaction->t_outstanding_credits);
182
223
                prepare_to_wait(&journal->j_wait_transaction_locked, &wait,
183
224
                                TASK_UNINTERRUPTIBLE);
184
 
                __jbd2_log_start_commit(journal, transaction->t_tid);
185
 
                spin_unlock(&journal->j_state_lock);
 
225
                tid = transaction->t_tid;
 
226
                need_to_start = !tid_geq(journal->j_commit_request, tid);
 
227
                read_unlock(&journal->j_state_lock);
 
228
                if (need_to_start)
 
229
                        jbd2_log_start_commit(journal, tid);
186
230
                schedule();
187
231
                finish_wait(&journal->j_wait_transaction_locked, &wait);
188
232
                goto repeat;
210
254
         * the committing transaction.  Really, we only need to give it
211
255
         * committing_transaction->t_outstanding_credits plus "enough" for
212
256
         * the log control blocks.
213
 
         * Also, this test is inconsitent with the matching one in
 
257
         * Also, this test is inconsistent with the matching one in
214
258
         * jbd2_journal_extend().
215
259
         */
216
260
        if (__jbd2_log_space_left(journal) < jbd_space_needed(journal)) {
217
261
                jbd_debug(2, "Handle %p waiting for checkpoint...\n", handle);
218
 
                spin_unlock(&transaction->t_handle_lock);
219
 
                __jbd2_log_wait_for_space(journal);
220
 
                goto repeat_locked;
 
262
                atomic_sub(nblocks, &transaction->t_outstanding_credits);
 
263
                read_unlock(&journal->j_state_lock);
 
264
                write_lock(&journal->j_state_lock);
 
265
                if (__jbd2_log_space_left(journal) < jbd_space_needed(journal))
 
266
                        __jbd2_log_wait_for_space(journal);
 
267
                write_unlock(&journal->j_state_lock);
 
268
                goto repeat;
221
269
        }
222
270
 
223
271
        /* OK, account for the buffers that this operation expects to
224
 
         * use and add the handle to the running transaction. */
225
 
 
226
 
        if (time_after(transaction->t_start, ts)) {
227
 
                ts = jbd2_time_diff(ts, transaction->t_start);
228
 
                if (ts > transaction->t_max_wait)
229
 
                        transaction->t_max_wait = ts;
230
 
        }
231
 
 
 
272
         * use and add the handle to the running transaction. 
 
273
         */
 
274
        update_t_max_wait(transaction);
232
275
        handle->h_transaction = transaction;
233
 
        transaction->t_outstanding_credits += nblocks;
234
 
        transaction->t_updates++;
235
 
        transaction->t_handle_count++;
 
276
        atomic_inc(&transaction->t_updates);
 
277
        atomic_inc(&transaction->t_handle_count);
236
278
        jbd_debug(4, "Handle %p given %d credits (total %d, free %d)\n",
237
 
                  handle, nblocks, transaction->t_outstanding_credits,
 
279
                  handle, nblocks,
 
280
                  atomic_read(&transaction->t_outstanding_credits),
238
281
                  __jbd2_log_space_left(journal));
239
 
        spin_unlock(&transaction->t_handle_lock);
240
 
        spin_unlock(&journal->j_state_lock);
 
282
        read_unlock(&journal->j_state_lock);
241
283
 
242
284
        lock_map_acquire(&handle->h_lockdep_map);
243
 
out:
244
 
        if (unlikely(new_transaction))          /* It's usually NULL */
245
 
                kfree(new_transaction);
246
 
        return ret;
 
285
        kfree(new_transaction);
 
286
        return 0;
247
287
}
248
288
 
249
289
static struct lock_class_key jbd2_handle_key;
278
318
 *
279
319
 * Return a pointer to a newly allocated handle, or NULL on failure
280
320
 */
281
 
handle_t *jbd2_journal_start(journal_t *journal, int nblocks)
 
321
handle_t *jbd2__journal_start(journal_t *journal, int nblocks, int gfp_mask)
282
322
{
283
323
        handle_t *handle = journal_current_handle();
284
324
        int err;
298
338
 
299
339
        current->journal_info = handle;
300
340
 
301
 
        err = start_this_handle(journal, handle);
 
341
        err = start_this_handle(journal, handle, gfp_mask);
302
342
        if (err < 0) {
303
343
                jbd2_free_handle(handle);
304
344
                current->journal_info = NULL;
305
345
                handle = ERR_PTR(err);
306
 
                goto out;
307
346
        }
308
 
out:
309
347
        return handle;
310
348
}
 
349
EXPORT_SYMBOL(jbd2__journal_start);
 
350
 
 
351
 
 
352
handle_t *jbd2_journal_start(journal_t *journal, int nblocks)
 
353
{
 
354
        return jbd2__journal_start(journal, nblocks, GFP_NOFS);
 
355
}
 
356
EXPORT_SYMBOL(jbd2_journal_start);
 
357
 
311
358
 
312
359
/**
313
360
 * int jbd2_journal_extend() - extend buffer credits.
342
389
 
343
390
        result = 1;
344
391
 
345
 
        spin_lock(&journal->j_state_lock);
 
392
        read_lock(&journal->j_state_lock);
346
393
 
347
394
        /* Don't extend a locked-down transaction! */
348
395
        if (handle->h_transaction->t_state != T_RUNNING) {
352
399
        }
353
400
 
354
401
        spin_lock(&transaction->t_handle_lock);
355
 
        wanted = transaction->t_outstanding_credits + nblocks;
 
402
        wanted = atomic_read(&transaction->t_outstanding_credits) + nblocks;
356
403
 
357
404
        if (wanted > journal->j_max_transaction_buffers) {
358
405
                jbd_debug(3, "denied handle %p %d blocks: "
367
414
        }
368
415
 
369
416
        handle->h_buffer_credits += nblocks;
370
 
        transaction->t_outstanding_credits += nblocks;
 
417
        atomic_add(nblocks, &transaction->t_outstanding_credits);
371
418
        result = 0;
372
419
 
373
420
        jbd_debug(3, "extended handle %p by %d\n", handle, nblocks);
374
421
unlock:
375
422
        spin_unlock(&transaction->t_handle_lock);
376
423
error_out:
377
 
        spin_unlock(&journal->j_state_lock);
 
424
        read_unlock(&journal->j_state_lock);
378
425
out:
379
426
        return result;
380
427
}
394
441
 * transaction capabable of guaranteeing the requested number of
395
442
 * credits.
396
443
 */
397
 
 
398
 
int jbd2_journal_restart(handle_t *handle, int nblocks)
 
444
int jbd2__journal_restart(handle_t *handle, int nblocks, int gfp_mask)
399
445
{
400
446
        transaction_t *transaction = handle->h_transaction;
401
447
        journal_t *journal = transaction->t_journal;
402
 
        int ret;
 
448
        tid_t           tid;
 
449
        int             need_to_start, ret;
403
450
 
404
451
        /* If we've had an abort of any type, don't even think about
405
452
         * actually doing the restart! */
410
457
         * First unlink the handle from its current transaction, and start the
411
458
         * commit on that.
412
459
         */
413
 
        J_ASSERT(transaction->t_updates > 0);
 
460
        J_ASSERT(atomic_read(&transaction->t_updates) > 0);
414
461
        J_ASSERT(journal_current_handle() == handle);
415
462
 
416
 
        spin_lock(&journal->j_state_lock);
 
463
        read_lock(&journal->j_state_lock);
417
464
        spin_lock(&transaction->t_handle_lock);
418
 
        transaction->t_outstanding_credits -= handle->h_buffer_credits;
419
 
        transaction->t_updates--;
420
 
 
421
 
        if (!transaction->t_updates)
 
465
        atomic_sub(handle->h_buffer_credits,
 
466
                   &transaction->t_outstanding_credits);
 
467
        if (atomic_dec_and_test(&transaction->t_updates))
422
468
                wake_up(&journal->j_wait_updates);
423
469
        spin_unlock(&transaction->t_handle_lock);
424
470
 
425
471
        jbd_debug(2, "restarting handle %p\n", handle);
426
 
        __jbd2_log_start_commit(journal, transaction->t_tid);
427
 
        spin_unlock(&journal->j_state_lock);
 
472
        tid = transaction->t_tid;
 
473
        need_to_start = !tid_geq(journal->j_commit_request, tid);
 
474
        read_unlock(&journal->j_state_lock);
 
475
        if (need_to_start)
 
476
                jbd2_log_start_commit(journal, tid);
428
477
 
429
478
        lock_map_release(&handle->h_lockdep_map);
430
479
        handle->h_buffer_credits = nblocks;
431
 
        ret = start_this_handle(journal, handle);
 
480
        ret = start_this_handle(journal, handle, gfp_mask);
432
481
        return ret;
433
482
}
434
 
 
 
483
EXPORT_SYMBOL(jbd2__journal_restart);
 
484
 
 
485
 
 
486
int jbd2_journal_restart(handle_t *handle, int nblocks)
 
487
{
 
488
        return jbd2__journal_restart(handle, nblocks, GFP_NOFS);
 
489
}
 
490
EXPORT_SYMBOL(jbd2_journal_restart);
435
491
 
436
492
/**
437
493
 * void jbd2_journal_lock_updates () - establish a transaction barrier.
447
503
{
448
504
        DEFINE_WAIT(wait);
449
505
 
450
 
        spin_lock(&journal->j_state_lock);
 
506
        write_lock(&journal->j_state_lock);
451
507
        ++journal->j_barrier_count;
452
508
 
453
509
        /* Wait until there are no running updates */
458
514
                        break;
459
515
 
460
516
                spin_lock(&transaction->t_handle_lock);
461
 
                if (!transaction->t_updates) {
 
517
                if (!atomic_read(&transaction->t_updates)) {
462
518
                        spin_unlock(&transaction->t_handle_lock);
463
519
                        break;
464
520
                }
465
521
                prepare_to_wait(&journal->j_wait_updates, &wait,
466
522
                                TASK_UNINTERRUPTIBLE);
467
523
                spin_unlock(&transaction->t_handle_lock);
468
 
                spin_unlock(&journal->j_state_lock);
 
524
                write_unlock(&journal->j_state_lock);
469
525
                schedule();
470
526
                finish_wait(&journal->j_wait_updates, &wait);
471
 
                spin_lock(&journal->j_state_lock);
 
527
                write_lock(&journal->j_state_lock);
472
528
        }
473
 
        spin_unlock(&journal->j_state_lock);
 
529
        write_unlock(&journal->j_state_lock);
474
530
 
475
531
        /*
476
532
         * We have now established a barrier against other normal updates, but
494
550
        J_ASSERT(journal->j_barrier_count != 0);
495
551
 
496
552
        mutex_unlock(&journal->j_barrier);
497
 
        spin_lock(&journal->j_state_lock);
 
553
        write_lock(&journal->j_state_lock);
498
554
        --journal->j_barrier_count;
499
 
        spin_unlock(&journal->j_state_lock);
 
555
        write_unlock(&journal->j_state_lock);
500
556
        wake_up(&journal->j_wait_transaction_locked);
501
557
}
502
558
 
538
594
        transaction = handle->h_transaction;
539
595
        journal = transaction->t_journal;
540
596
 
541
 
        jbd_debug(5, "buffer_head %p, force_copy %d\n", jh, force_copy);
 
597
        jbd_debug(5, "journal_head %p, force_copy %d\n", jh, force_copy);
542
598
 
543
599
        JBUFFER_TRACE(jh, "entry");
544
600
repeat:
723
779
                J_EXPECT_JH(jh, buffer_uptodate(jh2bh(jh)),
724
780
                            "Possible IO failure.\n");
725
781
                page = jh2bh(jh)->b_page;
726
 
                offset = ((unsigned long) jh2bh(jh)->b_data) & ~PAGE_MASK;
 
782
                offset = offset_in_page(jh2bh(jh)->b_data);
727
783
                source = kmap_atomic(page, KM_USER0);
 
784
                /* Fire data frozen trigger just before we copy the data */
 
785
                jbd2_buffer_frozen_trigger(jh, source + offset,
 
786
                                           jh->b_triggers);
728
787
                memcpy(jh->b_frozen_data, source+offset, jh2bh(jh)->b_size);
729
788
                kunmap_atomic(source, KM_USER0);
730
789
 
963
1022
        jh->b_triggers = type;
964
1023
}
965
1024
 
966
 
void jbd2_buffer_commit_trigger(struct journal_head *jh, void *mapped_data,
 
1025
void jbd2_buffer_frozen_trigger(struct journal_head *jh, void *mapped_data,
967
1026
                                struct jbd2_buffer_trigger_type *triggers)
968
1027
{
969
1028
        struct buffer_head *bh = jh2bh(jh);
970
1029
 
971
 
        if (!triggers || !triggers->t_commit)
 
1030
        if (!triggers || !triggers->t_frozen)
972
1031
                return;
973
1032
 
974
 
        triggers->t_commit(triggers, bh, mapped_data, bh->b_size);
 
1033
        triggers->t_frozen(triggers, bh, mapped_data, bh->b_size);
975
1034
}
976
1035
 
977
1036
void jbd2_buffer_abort_trigger(struct journal_head *jh,
1235
1294
{
1236
1295
        transaction_t *transaction = handle->h_transaction;
1237
1296
        journal_t *journal = transaction->t_journal;
1238
 
        int err;
 
1297
        int err, wait_for_commit = 0;
 
1298
        tid_t tid;
1239
1299
        pid_t pid;
1240
1300
 
1241
1301
        J_ASSERT(journal_current_handle() == handle);
1243
1303
        if (is_handle_aborted(handle))
1244
1304
                err = -EIO;
1245
1305
        else {
1246
 
                J_ASSERT(transaction->t_updates > 0);
 
1306
                J_ASSERT(atomic_read(&transaction->t_updates) > 0);
1247
1307
                err = 0;
1248
1308
        }
1249
1309
 
1288
1348
 
1289
1349
                journal->j_last_sync_writer = pid;
1290
1350
 
1291
 
                spin_lock(&journal->j_state_lock);
 
1351
                read_lock(&journal->j_state_lock);
1292
1352
                commit_time = journal->j_average_commit_time;
1293
 
                spin_unlock(&journal->j_state_lock);
 
1353
                read_unlock(&journal->j_state_lock);
1294
1354
 
1295
1355
                trans_time = ktime_to_ns(ktime_sub(ktime_get(),
1296
1356
                                                   transaction->t_start_time));
1311
1371
        if (handle->h_sync)
1312
1372
                transaction->t_synchronous_commit = 1;
1313
1373
        current->journal_info = NULL;
1314
 
        spin_lock(&journal->j_state_lock);
1315
 
        spin_lock(&transaction->t_handle_lock);
1316
 
        transaction->t_outstanding_credits -= handle->h_buffer_credits;
1317
 
        transaction->t_updates--;
1318
 
        if (!transaction->t_updates) {
1319
 
                wake_up(&journal->j_wait_updates);
1320
 
                if (journal->j_barrier_count)
1321
 
                        wake_up(&journal->j_wait_transaction_locked);
1322
 
        }
 
1374
        atomic_sub(handle->h_buffer_credits,
 
1375
                   &transaction->t_outstanding_credits);
1323
1376
 
1324
1377
        /*
1325
1378
         * If the handle is marked SYNC, we need to set another commit
1328
1381
         * transaction is too old now.
1329
1382
         */
1330
1383
        if (handle->h_sync ||
1331
 
                        transaction->t_outstanding_credits >
1332
 
                                journal->j_max_transaction_buffers ||
1333
 
                        time_after_eq(jiffies, transaction->t_expires)) {
 
1384
            (atomic_read(&transaction->t_outstanding_credits) >
 
1385
             journal->j_max_transaction_buffers) ||
 
1386
            time_after_eq(jiffies, transaction->t_expires)) {
1334
1387
                /* Do this even for aborted journals: an abort still
1335
1388
                 * completes the commit thread, it just doesn't write
1336
1389
                 * anything to disk. */
1337
 
                tid_t tid = transaction->t_tid;
1338
1390
 
1339
 
                spin_unlock(&transaction->t_handle_lock);
1340
1391
                jbd_debug(2, "transaction too old, requesting commit for "
1341
1392
                                        "handle %p\n", handle);
1342
1393
                /* This is non-blocking */
1343
 
                __jbd2_log_start_commit(journal, transaction->t_tid);
1344
 
                spin_unlock(&journal->j_state_lock);
 
1394
                jbd2_log_start_commit(journal, transaction->t_tid);
1345
1395
 
1346
1396
                /*
1347
1397
                 * Special case: JBD2_SYNC synchronous updates require us
1348
1398
                 * to wait for the commit to complete.
1349
1399
                 */
1350
1400
                if (handle->h_sync && !(current->flags & PF_MEMALLOC))
1351
 
                        err = jbd2_log_wait_commit(journal, tid);
1352
 
        } else {
1353
 
                spin_unlock(&transaction->t_handle_lock);
1354
 
                spin_unlock(&journal->j_state_lock);
1355
 
        }
 
1401
                        wait_for_commit = 1;
 
1402
        }
 
1403
 
 
1404
        /*
 
1405
         * Once we drop t_updates, if it goes to zero the transaction
 
1406
         * could start commiting on us and eventually disappear.  So
 
1407
         * once we do this, we must not dereference transaction
 
1408
         * pointer again.
 
1409
         */
 
1410
        tid = transaction->t_tid;
 
1411
        if (atomic_dec_and_test(&transaction->t_updates)) {
 
1412
                wake_up(&journal->j_wait_updates);
 
1413
                if (journal->j_barrier_count)
 
1414
                        wake_up(&journal->j_wait_transaction_locked);
 
1415
        }
 
1416
 
 
1417
        if (wait_for_commit)
 
1418
                err = jbd2_log_wait_commit(journal, tid);
1356
1419
 
1357
1420
        lock_map_release(&handle->h_lockdep_map);
1358
1421
 
1719
1782
                goto zap_buffer_unlocked;
1720
1783
 
1721
1784
        /* OK, we have data buffer in journaled mode */
1722
 
        spin_lock(&journal->j_state_lock);
 
1785
        write_lock(&journal->j_state_lock);
1723
1786
        jbd_lock_bh_state(bh);
1724
1787
        spin_lock(&journal->j_list_lock);
1725
1788
 
1727
1790
        if (!jh)
1728
1791
                goto zap_buffer_no_jh;
1729
1792
 
 
1793
        /*
 
1794
         * We cannot remove the buffer from checkpoint lists until the
 
1795
         * transaction adding inode to orphan list (let's call it T)
 
1796
         * is committed.  Otherwise if the transaction changing the
 
1797
         * buffer would be cleaned from the journal before T is
 
1798
         * committed, a crash will cause that the correct contents of
 
1799
         * the buffer will be lost.  On the other hand we have to
 
1800
         * clear the buffer dirty bit at latest at the moment when the
 
1801
         * transaction marking the buffer as freed in the filesystem
 
1802
         * structures is committed because from that moment on the
 
1803
         * buffer can be reallocated and used by a different page.
 
1804
         * Since the block hasn't been freed yet but the inode has
 
1805
         * already been added to orphan list, it is safe for us to add
 
1806
         * the buffer to BJ_Forget list of the newest transaction.
 
1807
         */
1730
1808
        transaction = jh->b_transaction;
1731
1809
        if (transaction == NULL) {
1732
1810
                /* First case: not on any transaction.  If it
1757
1835
                        jbd2_journal_put_journal_head(jh);
1758
1836
                        spin_unlock(&journal->j_list_lock);
1759
1837
                        jbd_unlock_bh_state(bh);
1760
 
                        spin_unlock(&journal->j_state_lock);
 
1838
                        write_unlock(&journal->j_state_lock);
1761
1839
                        return ret;
1762
1840
                } else {
1763
1841
                        /* There is no currently-running transaction. So the
1771
1849
                                jbd2_journal_put_journal_head(jh);
1772
1850
                                spin_unlock(&journal->j_list_lock);
1773
1851
                                jbd_unlock_bh_state(bh);
1774
 
                                spin_unlock(&journal->j_state_lock);
 
1852
                                write_unlock(&journal->j_state_lock);
1775
1853
                                return ret;
1776
1854
                        } else {
1777
1855
                                /* The orphan record's transaction has
1783
1861
        } else if (transaction == journal->j_committing_transaction) {
1784
1862
                JBUFFER_TRACE(jh, "on committing transaction");
1785
1863
                /*
1786
 
                 * If it is committing, we simply cannot touch it.  We
1787
 
                 * can remove it's next_transaction pointer from the
1788
 
                 * running transaction if that is set, but nothing
1789
 
                 * else. */
 
1864
                 * The buffer is committing, we simply cannot touch
 
1865
                 * it. So we just set j_next_transaction to the
 
1866
                 * running transaction (if there is one) and mark
 
1867
                 * buffer as freed so that commit code knows it should
 
1868
                 * clear dirty bits when it is done with the buffer.
 
1869
                 */
1790
1870
                set_buffer_freed(bh);
1791
 
                if (jh->b_next_transaction) {
1792
 
                        J_ASSERT(jh->b_next_transaction ==
1793
 
                                        journal->j_running_transaction);
1794
 
                        jh->b_next_transaction = NULL;
1795
 
                }
 
1871
                if (journal->j_running_transaction && buffer_jbddirty(bh))
 
1872
                        jh->b_next_transaction = journal->j_running_transaction;
1796
1873
                jbd2_journal_put_journal_head(jh);
1797
1874
                spin_unlock(&journal->j_list_lock);
1798
1875
                jbd_unlock_bh_state(bh);
1799
 
                spin_unlock(&journal->j_state_lock);
 
1876
                write_unlock(&journal->j_state_lock);
1800
1877
                return 0;
1801
1878
        } else {
1802
1879
                /* Good, the buffer belongs to the running transaction.
1815
1892
zap_buffer_no_jh:
1816
1893
        spin_unlock(&journal->j_list_lock);
1817
1894
        jbd_unlock_bh_state(bh);
1818
 
        spin_unlock(&journal->j_state_lock);
 
1895
        write_unlock(&journal->j_state_lock);
1819
1896
zap_buffer_unlocked:
1820
1897
        clear_buffer_dirty(bh);
1821
1898
        J_ASSERT_BH(bh, !buffer_jbddirty(bh));
1969
2046
 */
1970
2047
void __jbd2_journal_refile_buffer(struct journal_head *jh)
1971
2048
{
1972
 
        int was_dirty;
 
2049
        int was_dirty, jlist;
1973
2050
        struct buffer_head *bh = jh2bh(jh);
1974
2051
 
1975
2052
        J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1991
2068
        __jbd2_journal_temp_unlink_buffer(jh);
1992
2069
        jh->b_transaction = jh->b_next_transaction;
1993
2070
        jh->b_next_transaction = NULL;
1994
 
        __jbd2_journal_file_buffer(jh, jh->b_transaction,
1995
 
                                jh->b_modified ? BJ_Metadata : BJ_Reserved);
 
2071
        if (buffer_freed(bh))
 
2072
                jlist = BJ_Forget;
 
2073
        else if (jh->b_modified)
 
2074
                jlist = BJ_Metadata;
 
2075
        else
 
2076
                jlist = BJ_Reserved;
 
2077
        __jbd2_journal_file_buffer(jh, jh->b_transaction, jlist);
1996
2078
        J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
1997
2079
 
1998
2080
        if (was_dirty)
2117
2199
        /* Locks are here just to force reading of recent values, it is
2118
2200
         * enough that the transaction was not committing before we started
2119
2201
         * a transaction adding the inode to orphan list */
2120
 
        spin_lock(&journal->j_state_lock);
 
2202
        read_lock(&journal->j_state_lock);
2121
2203
        commit_trans = journal->j_committing_transaction;
2122
 
        spin_unlock(&journal->j_state_lock);
 
2204
        read_unlock(&journal->j_state_lock);
2123
2205
        spin_lock(&journal->j_list_lock);
2124
2206
        inode_trans = jinode->i_transaction;
2125
2207
        spin_unlock(&journal->j_list_lock);