85
* Update transiaction's maximum wait time, if debugging is enabled.
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.
94
static inline void update_t_max_wait(transaction_t *transaction)
96
#ifdef CONFIG_JBD2_DEBUG
97
unsigned long ts = jiffies;
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);
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.
86
static int start_this_handle(journal_t *journal, handle_t *handle)
117
static int start_this_handle(journal_t *journal, handle_t *handle,
88
transaction_t *transaction;
90
int nblocks = handle->h_buffer_credits;
91
transaction_t *new_transaction = NULL;
93
unsigned long ts = jiffies;
120
transaction_t *transaction, *new_transaction = NULL;
122
int needed, need_to_start;
123
int nblocks = handle->h_buffer_credits;
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);
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) {
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.
143
if ((gfp_mask & __GFP_FS) == 0) {
144
congestion_wait(BLK_RW_ASYNC, HZ/50);
145
goto alloc_transaction;
113
151
jbd_debug(3, "New handle %p going live.\n", handle);
118
154
* We need to hold j_state_lock until t_updates has been incremented,
119
155
* for proper journal barrier handling
121
spin_lock(&journal->j_state_lock);
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);
162
read_unlock(&journal->j_state_lock);
163
kfree(new_transaction);
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);
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;
143
jbd2_get_transaction(journal, new_transaction);
144
new_transaction = NULL;
184
write_unlock(&journal->j_state_lock);
147
188
transaction = journal->j_running_transaction;
178
219
DEFINE_WAIT(wait);
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);
229
jbd2_log_start_commit(journal, tid);
187
231
finish_wait(&journal->j_wait_transaction_locked, &wait);
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().
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);
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);
223
271
/* OK, account for the buffers that this operation expects to
224
* use and add the handle to the running transaction. */
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;
272
* use and add the handle to the running transaction.
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,
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);
242
284
lock_map_acquire(&handle->h_lockdep_map);
244
if (unlikely(new_transaction)) /* It's usually NULL */
245
kfree(new_transaction);
285
kfree(new_transaction);
249
289
static struct lock_class_key jbd2_handle_key;
299
339
current->journal_info = handle;
301
err = start_this_handle(journal, handle);
341
err = start_this_handle(journal, handle, gfp_mask);
303
343
jbd2_free_handle(handle);
304
344
current->journal_info = NULL;
305
345
handle = ERR_PTR(err);
349
EXPORT_SYMBOL(jbd2__journal_start);
352
handle_t *jbd2_journal_start(journal_t *journal, int nblocks)
354
return jbd2__journal_start(journal, nblocks, GFP_NOFS);
356
EXPORT_SYMBOL(jbd2_journal_start);
313
360
* int jbd2_journal_extend() - extend buffer credits.
410
457
* First unlink the handle from its current transaction, and start the
411
458
* commit on that.
413
J_ASSERT(transaction->t_updates > 0);
460
J_ASSERT(atomic_read(&transaction->t_updates) > 0);
414
461
J_ASSERT(journal_current_handle() == handle);
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--;
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);
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);
476
jbd2_log_start_commit(journal, tid);
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);
483
EXPORT_SYMBOL(jbd2__journal_restart);
486
int jbd2_journal_restart(handle_t *handle, int nblocks)
488
return jbd2__journal_restart(handle, nblocks, GFP_NOFS);
490
EXPORT_SYMBOL(jbd2_journal_restart);
437
493
* void jbd2_journal_lock_updates () - establish a transaction barrier.
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);
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);
470
526
finish_wait(&journal->j_wait_updates, &wait);
471
spin_lock(&journal->j_state_lock);
527
write_lock(&journal->j_state_lock);
473
spin_unlock(&journal->j_state_lock);
529
write_unlock(&journal->j_state_lock);
476
532
* We have now established a barrier against other normal updates, but
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,
728
787
memcpy(jh->b_frozen_data, source+offset, jh2bh(jh)->b_size);
729
788
kunmap_atomic(source, KM_USER0);
963
1022
jh->b_triggers = type;
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)
969
1028
struct buffer_head *bh = jh2bh(jh);
971
if (!triggers || !triggers->t_commit)
1030
if (!triggers || !triggers->t_frozen)
974
triggers->t_commit(triggers, bh, mapped_data, bh->b_size);
1033
triggers->t_frozen(triggers, bh, mapped_data, bh->b_size);
977
1036
void jbd2_buffer_abort_trigger(struct journal_head *jh,
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);
1374
atomic_sub(handle->h_buffer_credits,
1375
&transaction->t_outstanding_credits);
1325
1378
* If the handle is marked SYNC, we need to set another commit
1328
1381
* transaction is too old now.
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;
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);
1347
1397
* Special case: JBD2_SYNC synchronous updates require us
1348
1398
* to wait for the commit to complete.
1350
1400
if (handle->h_sync && !(current->flags & PF_MEMALLOC))
1351
err = jbd2_log_wait_commit(journal, tid);
1353
spin_unlock(&transaction->t_handle_lock);
1354
spin_unlock(&journal->j_state_lock);
1401
wait_for_commit = 1;
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
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);
1417
if (wait_for_commit)
1418
err = jbd2_log_wait_commit(journal, tid);
1357
1420
lock_map_release(&handle->h_lockdep_map);
1728
1791
goto zap_buffer_no_jh;
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.
1730
1808
transaction = jh->b_transaction;
1731
1809
if (transaction == NULL) {
1732
1810
/* First case: not on any transaction. If it
1783
1861
} else if (transaction == journal->j_committing_transaction) {
1784
1862
JBUFFER_TRACE(jh, "on committing transaction");
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
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.
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;
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);
1802
1879
/* Good, the buffer belongs to the running transaction.