1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * linux/fs/jbd2/transaction.c
4 *
5 * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
6 *
7 * Copyright 1998 Red Hat corp --- All Rights Reserved
8 *
9 * Generic filesystem transaction handling code; part of the ext2fs
10 * journaling system.
11 *
12 * This file manages transactions (compound commits managed by the
13 * journaling code) and handles (individual atomic operations by the
14 * filesystem).
15 */
16
17 #include <linux/time.h>
18 #include <linux/fs.h>
19 #include <linux/jbd2.h>
20 #include <linux/errno.h>
21 #include <linux/slab.h>
22 #include <linux/timer.h>
23 #include <linux/mm.h>
24 #include <linux/highmem.h>
25 #include <linux/hrtimer.h>
26 #include <linux/backing-dev.h>
27 #include <linux/bug.h>
28 #include <linux/module.h>
29 #include <linux/sched/mm.h>
30
31 #include <trace/events/jbd2.h>
32
33 static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh);
34 static void __jbd2_journal_unfile_buffer(struct journal_head *jh);
35
36 static struct kmem_cache *transaction_cache;
jbd2_journal_init_transaction_cache(void)37 int __init jbd2_journal_init_transaction_cache(void)
38 {
39 J_ASSERT(!transaction_cache);
40 transaction_cache = kmem_cache_create("jbd2_transaction_s",
41 sizeof(transaction_t),
42 0,
43 SLAB_HWCACHE_ALIGN|SLAB_TEMPORARY,
44 NULL);
45 if (!transaction_cache) {
46 pr_emerg("JBD2: failed to create transaction cache\n");
47 return -ENOMEM;
48 }
49 return 0;
50 }
51
jbd2_journal_destroy_transaction_cache(void)52 void jbd2_journal_destroy_transaction_cache(void)
53 {
54 kmem_cache_destroy(transaction_cache);
55 transaction_cache = NULL;
56 }
57
jbd2_journal_free_transaction(transaction_t * transaction)58 void jbd2_journal_free_transaction(transaction_t *transaction)
59 {
60 if (unlikely(ZERO_OR_NULL_PTR(transaction)))
61 return;
62 kmem_cache_free(transaction_cache, transaction);
63 }
64
65 /*
66 * jbd2_get_transaction: obtain a new transaction_t object.
67 *
68 * Simply initialise a new transaction. Initialize it in
69 * RUNNING state and add it to the current journal (which should not
70 * have an existing running transaction: we only make a new transaction
71 * once we have started to commit the old one).
72 *
73 * Preconditions:
74 * The journal MUST be locked. We don't perform atomic mallocs on the
75 * new transaction and we can't block without protecting against other
76 * processes trying to touch the journal while it is in transition.
77 *
78 */
79
jbd2_get_transaction(journal_t * journal,transaction_t * transaction)80 static void jbd2_get_transaction(journal_t *journal,
81 transaction_t *transaction)
82 {
83 transaction->t_journal = journal;
84 transaction->t_state = T_RUNNING;
85 transaction->t_start_time = ktime_get();
86 transaction->t_tid = journal->j_transaction_sequence++;
87 transaction->t_expires = jiffies + journal->j_commit_interval;
88 atomic_set(&transaction->t_updates, 0);
89 atomic_set(&transaction->t_outstanding_credits,
90 journal->j_transaction_overhead_buffers +
91 atomic_read(&journal->j_reserved_credits));
92 atomic_set(&transaction->t_outstanding_revokes, 0);
93 atomic_set(&transaction->t_handle_count, 0);
94 INIT_LIST_HEAD(&transaction->t_inode_list);
95
96 /* Set up the commit timer for the new transaction. */
97 journal->j_commit_timer.expires = round_jiffies_up(transaction->t_expires);
98 add_timer(&journal->j_commit_timer);
99
100 J_ASSERT(journal->j_running_transaction == NULL);
101 journal->j_running_transaction = transaction;
102 transaction->t_max_wait = 0;
103 transaction->t_start = jiffies;
104 transaction->t_requested = 0;
105 }
106
107 /*
108 * Handle management.
109 *
110 * A handle_t is an object which represents a single atomic update to a
111 * filesystem, and which tracks all of the modifications which form part
112 * of that one update.
113 */
114
115 /*
116 * t_max_wait is carefully updated here with use of atomic compare exchange.
117 * Note that there could be multiplre threads trying to do this simultaneously
118 * hence using cmpxchg to avoid any use of locks in this case.
119 */
update_t_max_wait(transaction_t * transaction,unsigned long ts)120 static inline void update_t_max_wait(transaction_t *transaction,
121 unsigned long ts)
122 {
123 unsigned long oldts, newts;
124
125 if (time_after(transaction->t_start, ts)) {
126 newts = jbd2_time_diff(ts, transaction->t_start);
127 oldts = READ_ONCE(transaction->t_max_wait);
128 while (oldts < newts)
129 oldts = cmpxchg(&transaction->t_max_wait, oldts, newts);
130 }
131 }
132
133 /*
134 * Wait until running transaction passes to T_FLUSH state and new transaction
135 * can thus be started. Also starts the commit if needed. The function expects
136 * running transaction to exist and releases j_state_lock.
137 */
wait_transaction_locked(journal_t * journal)138 static void wait_transaction_locked(journal_t *journal)
139 __releases(journal->j_state_lock)
140 {
141 DEFINE_WAIT(wait);
142 int need_to_start;
143 tid_t tid = journal->j_running_transaction->t_tid;
144
145 prepare_to_wait_exclusive(&journal->j_wait_transaction_locked, &wait,
146 TASK_UNINTERRUPTIBLE);
147 need_to_start = !tid_geq(journal->j_commit_request, tid);
148 read_unlock(&journal->j_state_lock);
149 if (need_to_start)
150 jbd2_log_start_commit(journal, tid);
151 jbd2_might_wait_for_commit(journal);
152 schedule();
153 finish_wait(&journal->j_wait_transaction_locked, &wait);
154 }
155
156 /*
157 * Wait until running transaction transitions from T_SWITCH to T_FLUSH
158 * state and new transaction can thus be started. The function releases
159 * j_state_lock.
160 */
wait_transaction_switching(journal_t * journal)161 static void wait_transaction_switching(journal_t *journal)
162 __releases(journal->j_state_lock)
163 {
164 DEFINE_WAIT(wait);
165
166 if (WARN_ON(!journal->j_running_transaction ||
167 journal->j_running_transaction->t_state != T_SWITCH)) {
168 read_unlock(&journal->j_state_lock);
169 return;
170 }
171 prepare_to_wait_exclusive(&journal->j_wait_transaction_locked, &wait,
172 TASK_UNINTERRUPTIBLE);
173 read_unlock(&journal->j_state_lock);
174 /*
175 * We don't call jbd2_might_wait_for_commit() here as there's no
176 * waiting for outstanding handles happening anymore in T_SWITCH state
177 * and handling of reserved handles actually relies on that for
178 * correctness.
179 */
180 schedule();
181 finish_wait(&journal->j_wait_transaction_locked, &wait);
182 }
183
sub_reserved_credits(journal_t * journal,int blocks)184 static void sub_reserved_credits(journal_t *journal, int blocks)
185 {
186 atomic_sub(blocks, &journal->j_reserved_credits);
187 wake_up(&journal->j_wait_reserved);
188 }
189
190 /* Maximum number of blocks for user transaction payload */
jbd2_max_user_trans_buffers(journal_t * journal)191 static int jbd2_max_user_trans_buffers(journal_t *journal)
192 {
193 return journal->j_max_transaction_buffers -
194 journal->j_transaction_overhead_buffers;
195 }
196
197 /*
198 * Wait until we can add credits for handle to the running transaction. Called
199 * with j_state_lock held for reading. Returns 0 if handle joined the running
200 * transaction. Returns 1 if we had to wait, j_state_lock is dropped, and
201 * caller must retry.
202 *
203 * Note: because j_state_lock may be dropped depending on the return
204 * value, we need to fake out sparse so ti doesn't complain about a
205 * locking imbalance. Callers of add_transaction_credits will need to
206 * make a similar accomodation.
207 */
add_transaction_credits(journal_t * journal,int blocks,int rsv_blocks)208 static int add_transaction_credits(journal_t *journal, int blocks,
209 int rsv_blocks)
210 __must_hold(&journal->j_state_lock)
211 {
212 transaction_t *t = journal->j_running_transaction;
213 int needed;
214 int total = blocks + rsv_blocks;
215
216 /*
217 * If the current transaction is locked down for commit, wait
218 * for the lock to be released.
219 */
220 if (t->t_state != T_RUNNING) {
221 WARN_ON_ONCE(t->t_state >= T_FLUSH);
222 wait_transaction_locked(journal);
223 __acquire(&journal->j_state_lock); /* fake out sparse */
224 return 1;
225 }
226
227 /*
228 * If there is not enough space left in the log to write all
229 * potential buffers requested by this operation, we need to
230 * stall pending a log checkpoint to free some more log space.
231 */
232 needed = atomic_add_return(total, &t->t_outstanding_credits);
233 if (needed > journal->j_max_transaction_buffers) {
234 /*
235 * If the current transaction is already too large,
236 * then start to commit it: we can then go back and
237 * attach this handle to a new transaction.
238 */
239 atomic_sub(total, &t->t_outstanding_credits);
240
241 /*
242 * Is the number of reserved credits in the current transaction too
243 * big to fit this handle? Wait until reserved credits are freed.
244 */
245 if (atomic_read(&journal->j_reserved_credits) + total >
246 jbd2_max_user_trans_buffers(journal)) {
247 read_unlock(&journal->j_state_lock);
248 jbd2_might_wait_for_commit(journal);
249 wait_event(journal->j_wait_reserved,
250 atomic_read(&journal->j_reserved_credits) + total <=
251 jbd2_max_user_trans_buffers(journal));
252 __acquire(&journal->j_state_lock); /* fake out sparse */
253 return 1;
254 }
255
256 wait_transaction_locked(journal);
257 __acquire(&journal->j_state_lock); /* fake out sparse */
258 return 1;
259 }
260
261 /*
262 * The commit code assumes that it can get enough log space
263 * without forcing a checkpoint. This is *critical* for
264 * correctness: a checkpoint of a buffer which is also
265 * associated with a committing transaction creates a deadlock,
266 * so commit simply cannot force through checkpoints.
267 *
268 * We must therefore ensure the necessary space in the journal
269 * *before* starting to dirty potentially checkpointed buffers
270 * in the new transaction.
271 */
272 if (jbd2_log_space_left(journal) < journal->j_max_transaction_buffers) {
273 atomic_sub(total, &t->t_outstanding_credits);
274 read_unlock(&journal->j_state_lock);
275 jbd2_might_wait_for_commit(journal);
276 write_lock(&journal->j_state_lock);
277 if (jbd2_log_space_left(journal) <
278 journal->j_max_transaction_buffers)
279 __jbd2_log_wait_for_space(journal);
280 write_unlock(&journal->j_state_lock);
281 __acquire(&journal->j_state_lock); /* fake out sparse */
282 return 1;
283 }
284
285 /* No reservation? We are done... */
286 if (!rsv_blocks)
287 return 0;
288
289 needed = atomic_add_return(rsv_blocks, &journal->j_reserved_credits);
290 /* We allow at most half of a transaction to be reserved */
291 if (needed > jbd2_max_user_trans_buffers(journal) / 2) {
292 sub_reserved_credits(journal, rsv_blocks);
293 atomic_sub(total, &t->t_outstanding_credits);
294 read_unlock(&journal->j_state_lock);
295 jbd2_might_wait_for_commit(journal);
296 wait_event(journal->j_wait_reserved,
297 atomic_read(&journal->j_reserved_credits) + rsv_blocks
298 <= jbd2_max_user_trans_buffers(journal) / 2);
299 __acquire(&journal->j_state_lock); /* fake out sparse */
300 return 1;
301 }
302 return 0;
303 }
304
305 /*
306 * start_this_handle: Given a handle, deal with any locking or stalling
307 * needed to make sure that there is enough journal space for the handle
308 * to begin. Attach the handle to a transaction and set up the
309 * transaction's buffer credits.
310 */
311
start_this_handle(journal_t * journal,handle_t * handle,gfp_t gfp_mask)312 static int start_this_handle(journal_t *journal, handle_t *handle,
313 gfp_t gfp_mask)
314 {
315 transaction_t *transaction, *new_transaction = NULL;
316 int blocks = handle->h_total_credits;
317 int rsv_blocks = 0;
318 unsigned long ts = jiffies;
319
320 if (handle->h_rsv_handle)
321 rsv_blocks = handle->h_rsv_handle->h_total_credits;
322
323 /*
324 * Limit the number of reserved credits to 1/2 of maximum transaction
325 * size and limit the number of total credits to not exceed maximum
326 * transaction size per operation.
327 */
328 if (rsv_blocks > jbd2_max_user_trans_buffers(journal) / 2 ||
329 rsv_blocks + blocks > jbd2_max_user_trans_buffers(journal)) {
330 printk(KERN_ERR "JBD2: %s wants too many credits "
331 "credits:%d rsv_credits:%d max:%d\n",
332 current->comm, blocks, rsv_blocks,
333 jbd2_max_user_trans_buffers(journal));
334 WARN_ON(1);
335 return -ENOSPC;
336 }
337
338 alloc_transaction:
339 /*
340 * This check is racy but it is just an optimization of allocating new
341 * transaction early if there are high chances we'll need it. If we
342 * guess wrong, we'll retry or free unused transaction.
343 */
344 if (!data_race(journal->j_running_transaction)) {
345 /*
346 * If __GFP_FS is not present, then we may be being called from
347 * inside the fs writeback layer, so we MUST NOT fail.
348 */
349 if ((gfp_mask & __GFP_FS) == 0)
350 gfp_mask |= __GFP_NOFAIL;
351 new_transaction = kmem_cache_zalloc(transaction_cache,
352 gfp_mask);
353 if (!new_transaction)
354 return -ENOMEM;
355 }
356
357 jbd2_debug(3, "New handle %p going live.\n", handle);
358
359 /*
360 * We need to hold j_state_lock until t_updates has been incremented,
361 * for proper journal barrier handling
362 */
363 repeat:
364 read_lock(&journal->j_state_lock);
365 BUG_ON(journal->j_flags & JBD2_UNMOUNT);
366 if (is_journal_aborted(journal) ||
367 (journal->j_errno != 0 && !(journal->j_flags & JBD2_ACK_ERR))) {
368 read_unlock(&journal->j_state_lock);
369 jbd2_journal_free_transaction(new_transaction);
370 return -EROFS;
371 }
372
373 /*
374 * Wait on the journal's transaction barrier if necessary. Specifically
375 * we allow reserved handles to proceed because otherwise commit could
376 * deadlock on page writeback not being able to complete.
377 */
378 if (!handle->h_reserved && journal->j_barrier_count) {
379 read_unlock(&journal->j_state_lock);
380 wait_event(journal->j_wait_transaction_locked,
381 journal->j_barrier_count == 0);
382 goto repeat;
383 }
384
385 if (!journal->j_running_transaction) {
386 read_unlock(&journal->j_state_lock);
387 if (!new_transaction)
388 goto alloc_transaction;
389 write_lock(&journal->j_state_lock);
390 if (!journal->j_running_transaction &&
391 (handle->h_reserved || !journal->j_barrier_count)) {
392 jbd2_get_transaction(journal, new_transaction);
393 new_transaction = NULL;
394 }
395 write_unlock(&journal->j_state_lock);
396 goto repeat;
397 }
398
399 transaction = journal->j_running_transaction;
400
401 if (!handle->h_reserved) {
402 /* We may have dropped j_state_lock - restart in that case */
403 if (add_transaction_credits(journal, blocks, rsv_blocks)) {
404 /*
405 * add_transaction_credits releases
406 * j_state_lock on a non-zero return
407 */
408 __release(&journal->j_state_lock);
409 goto repeat;
410 }
411 } else {
412 /*
413 * We have handle reserved so we are allowed to join T_LOCKED
414 * transaction and we don't have to check for transaction size
415 * and journal space. But we still have to wait while running
416 * transaction is being switched to a committing one as it
417 * won't wait for any handles anymore.
418 */
419 if (transaction->t_state == T_SWITCH) {
420 wait_transaction_switching(journal);
421 goto repeat;
422 }
423 sub_reserved_credits(journal, blocks);
424 handle->h_reserved = 0;
425 }
426
427 /* OK, account for the buffers that this operation expects to
428 * use and add the handle to the running transaction.
429 */
430 update_t_max_wait(transaction, ts);
431 handle->h_transaction = transaction;
432 handle->h_requested_credits = blocks;
433 handle->h_revoke_credits_requested = handle->h_revoke_credits;
434 handle->h_start_jiffies = jiffies;
435 atomic_inc(&transaction->t_updates);
436 atomic_inc(&transaction->t_handle_count);
437 jbd2_debug(4, "Handle %p given %d credits (total %d, free %lu)\n",
438 handle, blocks,
439 atomic_read(&transaction->t_outstanding_credits),
440 jbd2_log_space_left(journal));
441 read_unlock(&journal->j_state_lock);
442 current->journal_info = handle;
443
444 rwsem_acquire_read(&journal->j_trans_commit_map, 0, 0, _THIS_IP_);
445 jbd2_journal_free_transaction(new_transaction);
446 /*
447 * Ensure that no allocations done while the transaction is open are
448 * going to recurse back to the fs layer.
449 */
450 handle->saved_alloc_context = memalloc_nofs_save();
451 return 0;
452 }
453
454 /* Allocate a new handle. This should probably be in a slab... */
new_handle(int nblocks)455 static handle_t *new_handle(int nblocks)
456 {
457 handle_t *handle = jbd2_alloc_handle(GFP_NOFS);
458 if (!handle)
459 return NULL;
460 handle->h_total_credits = nblocks;
461 handle->h_ref = 1;
462
463 return handle;
464 }
465
jbd2__journal_start(journal_t * journal,int nblocks,int rsv_blocks,int revoke_records,gfp_t gfp_mask,unsigned int type,unsigned int line_no)466 handle_t *jbd2__journal_start(journal_t *journal, int nblocks, int rsv_blocks,
467 int revoke_records, gfp_t gfp_mask,
468 unsigned int type, unsigned int line_no)
469 {
470 handle_t *handle = journal_current_handle();
471 int err;
472
473 if (!journal)
474 return ERR_PTR(-EROFS);
475
476 if (handle) {
477 J_ASSERT(handle->h_transaction->t_journal == journal);
478 handle->h_ref++;
479 return handle;
480 }
481
482 nblocks += DIV_ROUND_UP(revoke_records,
483 journal->j_revoke_records_per_block);
484 handle = new_handle(nblocks);
485 if (!handle)
486 return ERR_PTR(-ENOMEM);
487 if (rsv_blocks) {
488 handle_t *rsv_handle;
489
490 rsv_handle = new_handle(rsv_blocks);
491 if (!rsv_handle) {
492 jbd2_free_handle(handle);
493 return ERR_PTR(-ENOMEM);
494 }
495 rsv_handle->h_reserved = 1;
496 rsv_handle->h_journal = journal;
497 handle->h_rsv_handle = rsv_handle;
498 }
499 handle->h_revoke_credits = revoke_records;
500
501 err = start_this_handle(journal, handle, gfp_mask);
502 if (err < 0) {
503 if (handle->h_rsv_handle)
504 jbd2_free_handle(handle->h_rsv_handle);
505 jbd2_free_handle(handle);
506 return ERR_PTR(err);
507 }
508 handle->h_type = type;
509 handle->h_line_no = line_no;
510 trace_jbd2_handle_start(journal->j_fs_dev->bd_dev,
511 handle->h_transaction->t_tid, type,
512 line_no, nblocks);
513
514 return handle;
515 }
516 EXPORT_SYMBOL(jbd2__journal_start);
517
518
519 /**
520 * jbd2_journal_start() - Obtain a new handle.
521 * @journal: Journal to start transaction on.
522 * @nblocks: number of block buffer we might modify
523 *
524 * We make sure that the transaction can guarantee at least nblocks of
525 * modified buffers in the log. We block until the log can guarantee
526 * that much space. Additionally, if rsv_blocks > 0, we also create another
527 * handle with rsv_blocks reserved blocks in the journal. This handle is
528 * stored in h_rsv_handle. It is not attached to any particular transaction
529 * and thus doesn't block transaction commit. If the caller uses this reserved
530 * handle, it has to set h_rsv_handle to NULL as otherwise jbd2_journal_stop()
531 * on the parent handle will dispose the reserved one. Reserved handle has to
532 * be converted to a normal handle using jbd2_journal_start_reserved() before
533 * it can be used.
534 *
535 * Return a pointer to a newly allocated handle, or an ERR_PTR() value
536 * on failure.
537 */
jbd2_journal_start(journal_t * journal,int nblocks)538 handle_t *jbd2_journal_start(journal_t *journal, int nblocks)
539 {
540 return jbd2__journal_start(journal, nblocks, 0, 0, GFP_NOFS, 0, 0);
541 }
542 EXPORT_SYMBOL(jbd2_journal_start);
543
__jbd2_journal_unreserve_handle(handle_t * handle,transaction_t * t)544 static void __jbd2_journal_unreserve_handle(handle_t *handle, transaction_t *t)
545 {
546 journal_t *journal = handle->h_journal;
547
548 WARN_ON(!handle->h_reserved);
549 sub_reserved_credits(journal, handle->h_total_credits);
550 if (t)
551 atomic_sub(handle->h_total_credits, &t->t_outstanding_credits);
552 }
553
jbd2_journal_free_reserved(handle_t * handle)554 void jbd2_journal_free_reserved(handle_t *handle)
555 {
556 journal_t *journal = handle->h_journal;
557
558 /* Get j_state_lock to pin running transaction if it exists */
559 read_lock(&journal->j_state_lock);
560 __jbd2_journal_unreserve_handle(handle, journal->j_running_transaction);
561 read_unlock(&journal->j_state_lock);
562 jbd2_free_handle(handle);
563 }
564 EXPORT_SYMBOL(jbd2_journal_free_reserved);
565
566 /**
567 * jbd2_journal_start_reserved() - start reserved handle
568 * @handle: handle to start
569 * @type: for handle statistics
570 * @line_no: for handle statistics
571 *
572 * Start handle that has been previously reserved with jbd2_journal_reserve().
573 * This attaches @handle to the running transaction (or creates one if there's
574 * not transaction running). Unlike jbd2_journal_start() this function cannot
575 * block on journal commit, checkpointing, or similar stuff. It can block on
576 * memory allocation or frozen journal though.
577 *
578 * Return 0 on success, non-zero on error - handle is freed in that case.
579 */
jbd2_journal_start_reserved(handle_t * handle,unsigned int type,unsigned int line_no)580 int jbd2_journal_start_reserved(handle_t *handle, unsigned int type,
581 unsigned int line_no)
582 {
583 journal_t *journal = handle->h_journal;
584 int ret = -EIO;
585
586 if (WARN_ON(!handle->h_reserved)) {
587 /* Someone passed in normal handle? Just stop it. */
588 jbd2_journal_stop(handle);
589 return ret;
590 }
591 /*
592 * Usefulness of mixing of reserved and unreserved handles is
593 * questionable. So far nobody seems to need it so just error out.
594 */
595 if (WARN_ON(current->journal_info)) {
596 jbd2_journal_free_reserved(handle);
597 return ret;
598 }
599
600 handle->h_journal = NULL;
601 /*
602 * GFP_NOFS is here because callers are likely from writeback or
603 * similarly constrained call sites
604 */
605 ret = start_this_handle(journal, handle, GFP_NOFS);
606 if (ret < 0) {
607 handle->h_journal = journal;
608 jbd2_journal_free_reserved(handle);
609 return ret;
610 }
611 handle->h_type = type;
612 handle->h_line_no = line_no;
613 trace_jbd2_handle_start(journal->j_fs_dev->bd_dev,
614 handle->h_transaction->t_tid, type,
615 line_no, handle->h_total_credits);
616 return 0;
617 }
618 EXPORT_SYMBOL(jbd2_journal_start_reserved);
619
620 /**
621 * jbd2_journal_extend() - extend buffer credits.
622 * @handle: handle to 'extend'
623 * @nblocks: nr blocks to try to extend by.
624 * @revoke_records: number of revoke records to try to extend by.
625 *
626 * Some transactions, such as large extends and truncates, can be done
627 * atomically all at once or in several stages. The operation requests
628 * a credit for a number of buffer modifications in advance, but can
629 * extend its credit if it needs more.
630 *
631 * jbd2_journal_extend tries to give the running handle more buffer credits.
632 * It does not guarantee that allocation - this is a best-effort only.
633 * The calling process MUST be able to deal cleanly with a failure to
634 * extend here.
635 *
636 * Return 0 on success, non-zero on failure.
637 *
638 * return code < 0 implies an error
639 * return code > 0 implies normal transaction-full status.
640 */
jbd2_journal_extend(handle_t * handle,int nblocks,int revoke_records)641 int jbd2_journal_extend(handle_t *handle, int nblocks, int revoke_records)
642 {
643 transaction_t *transaction = handle->h_transaction;
644 journal_t *journal;
645 int result;
646 int wanted;
647
648 if (is_handle_aborted(handle))
649 return -EROFS;
650 journal = transaction->t_journal;
651
652 result = 1;
653
654 read_lock(&journal->j_state_lock);
655
656 /* Don't extend a locked-down transaction! */
657 if (transaction->t_state != T_RUNNING) {
658 jbd2_debug(3, "denied handle %p %d blocks: "
659 "transaction not running\n", handle, nblocks);
660 goto error_out;
661 }
662
663 nblocks += DIV_ROUND_UP(
664 handle->h_revoke_credits_requested + revoke_records,
665 journal->j_revoke_records_per_block) -
666 DIV_ROUND_UP(
667 handle->h_revoke_credits_requested,
668 journal->j_revoke_records_per_block);
669 wanted = atomic_add_return(nblocks,
670 &transaction->t_outstanding_credits);
671
672 if (wanted > journal->j_max_transaction_buffers) {
673 jbd2_debug(3, "denied handle %p %d blocks: "
674 "transaction too large\n", handle, nblocks);
675 atomic_sub(nblocks, &transaction->t_outstanding_credits);
676 goto error_out;
677 }
678
679 trace_jbd2_handle_extend(journal->j_fs_dev->bd_dev,
680 transaction->t_tid,
681 handle->h_type, handle->h_line_no,
682 handle->h_total_credits,
683 nblocks);
684
685 handle->h_total_credits += nblocks;
686 handle->h_requested_credits += nblocks;
687 handle->h_revoke_credits += revoke_records;
688 handle->h_revoke_credits_requested += revoke_records;
689 result = 0;
690
691 jbd2_debug(3, "extended handle %p by %d\n", handle, nblocks);
692 error_out:
693 read_unlock(&journal->j_state_lock);
694 return result;
695 }
696
stop_this_handle(handle_t * handle)697 static void stop_this_handle(handle_t *handle)
698 {
699 transaction_t *transaction = handle->h_transaction;
700 journal_t *journal = transaction->t_journal;
701 int revokes;
702
703 J_ASSERT(journal_current_handle() == handle);
704 J_ASSERT(atomic_read(&transaction->t_updates) > 0);
705 current->journal_info = NULL;
706 /*
707 * Subtract necessary revoke descriptor blocks from handle credits. We
708 * take care to account only for revoke descriptor blocks the
709 * transaction will really need as large sequences of transactions with
710 * small numbers of revokes are relatively common.
711 */
712 revokes = handle->h_revoke_credits_requested - handle->h_revoke_credits;
713 if (revokes) {
714 int t_revokes, revoke_descriptors;
715 int rr_per_blk = journal->j_revoke_records_per_block;
716
717 WARN_ON_ONCE(DIV_ROUND_UP(revokes, rr_per_blk)
718 > handle->h_total_credits);
719 t_revokes = atomic_add_return(revokes,
720 &transaction->t_outstanding_revokes);
721 revoke_descriptors =
722 DIV_ROUND_UP(t_revokes, rr_per_blk) -
723 DIV_ROUND_UP(t_revokes - revokes, rr_per_blk);
724 handle->h_total_credits -= revoke_descriptors;
725 }
726 atomic_sub(handle->h_total_credits,
727 &transaction->t_outstanding_credits);
728 if (handle->h_rsv_handle)
729 __jbd2_journal_unreserve_handle(handle->h_rsv_handle,
730 transaction);
731 if (atomic_dec_and_test(&transaction->t_updates))
732 wake_up(&journal->j_wait_updates);
733
734 rwsem_release(&journal->j_trans_commit_map, _THIS_IP_);
735 /*
736 * Scope of the GFP_NOFS context is over here and so we can restore the
737 * original alloc context.
738 */
739 memalloc_nofs_restore(handle->saved_alloc_context);
740 }
741
742 /**
743 * jbd2__journal_restart() - restart a handle .
744 * @handle: handle to restart
745 * @nblocks: nr credits requested
746 * @revoke_records: number of revoke record credits requested
747 * @gfp_mask: memory allocation flags (for start_this_handle)
748 *
749 * Restart a handle for a multi-transaction filesystem
750 * operation.
751 *
752 * If the jbd2_journal_extend() call above fails to grant new buffer credits
753 * to a running handle, a call to jbd2_journal_restart will commit the
754 * handle's transaction so far and reattach the handle to a new
755 * transaction capable of guaranteeing the requested number of
756 * credits. We preserve reserved handle if there's any attached to the
757 * passed in handle.
758 */
jbd2__journal_restart(handle_t * handle,int nblocks,int revoke_records,gfp_t gfp_mask)759 int jbd2__journal_restart(handle_t *handle, int nblocks, int revoke_records,
760 gfp_t gfp_mask)
761 {
762 transaction_t *transaction = handle->h_transaction;
763 journal_t *journal;
764 tid_t tid;
765 int need_to_start;
766 int ret;
767
768 /* If we've had an abort of any type, don't even think about
769 * actually doing the restart! */
770 if (is_handle_aborted(handle))
771 return 0;
772 journal = transaction->t_journal;
773 tid = transaction->t_tid;
774
775 /*
776 * First unlink the handle from its current transaction, and start the
777 * commit on that.
778 */
779 jbd2_debug(2, "restarting handle %p\n", handle);
780 stop_this_handle(handle);
781 handle->h_transaction = NULL;
782
783 /*
784 * TODO: If we use READ_ONCE / WRITE_ONCE for j_commit_request we can
785 * get rid of pointless j_state_lock traffic like this.
786 */
787 read_lock(&journal->j_state_lock);
788 need_to_start = !tid_geq(journal->j_commit_request, tid);
789 read_unlock(&journal->j_state_lock);
790 if (need_to_start)
791 jbd2_log_start_commit(journal, tid);
792 handle->h_total_credits = nblocks +
793 DIV_ROUND_UP(revoke_records,
794 journal->j_revoke_records_per_block);
795 handle->h_revoke_credits = revoke_records;
796 ret = start_this_handle(journal, handle, gfp_mask);
797 trace_jbd2_handle_restart(journal->j_fs_dev->bd_dev,
798 ret ? 0 : handle->h_transaction->t_tid,
799 handle->h_type, handle->h_line_no,
800 handle->h_total_credits);
801 return ret;
802 }
803 EXPORT_SYMBOL(jbd2__journal_restart);
804
805
jbd2_journal_restart(handle_t * handle,int nblocks)806 int jbd2_journal_restart(handle_t *handle, int nblocks)
807 {
808 return jbd2__journal_restart(handle, nblocks, 0, GFP_NOFS);
809 }
810 EXPORT_SYMBOL(jbd2_journal_restart);
811
812 /*
813 * Waits for any outstanding t_updates to finish.
814 * This is called with write j_state_lock held.
815 */
jbd2_journal_wait_updates(journal_t * journal)816 void jbd2_journal_wait_updates(journal_t *journal)
817 {
818 DEFINE_WAIT(wait);
819
820 while (1) {
821 /*
822 * Note that the running transaction can get freed under us if
823 * this transaction is getting committed in
824 * jbd2_journal_commit_transaction() ->
825 * jbd2_journal_free_transaction(). This can only happen when we
826 * release j_state_lock -> schedule() -> acquire j_state_lock.
827 * Hence we should everytime retrieve new j_running_transaction
828 * value (after j_state_lock release acquire cycle), else it may
829 * lead to use-after-free of old freed transaction.
830 */
831 transaction_t *transaction = journal->j_running_transaction;
832
833 if (!transaction)
834 break;
835
836 prepare_to_wait(&journal->j_wait_updates, &wait,
837 TASK_UNINTERRUPTIBLE);
838 if (!atomic_read(&transaction->t_updates)) {
839 finish_wait(&journal->j_wait_updates, &wait);
840 break;
841 }
842 write_unlock(&journal->j_state_lock);
843 schedule();
844 finish_wait(&journal->j_wait_updates, &wait);
845 write_lock(&journal->j_state_lock);
846 }
847 }
848
849 /**
850 * jbd2_journal_lock_updates () - establish a transaction barrier.
851 * @journal: Journal to establish a barrier on.
852 *
853 * This locks out any further updates from being started, and blocks
854 * until all existing updates have completed, returning only once the
855 * journal is in a quiescent state with no updates running.
856 *
857 * The journal lock should not be held on entry.
858 */
jbd2_journal_lock_updates(journal_t * journal)859 void jbd2_journal_lock_updates(journal_t *journal)
860 {
861 jbd2_might_wait_for_commit(journal);
862
863 write_lock(&journal->j_state_lock);
864 ++journal->j_barrier_count;
865
866 /* Wait until there are no reserved handles */
867 if (atomic_read(&journal->j_reserved_credits)) {
868 write_unlock(&journal->j_state_lock);
869 wait_event(journal->j_wait_reserved,
870 atomic_read(&journal->j_reserved_credits) == 0);
871 write_lock(&journal->j_state_lock);
872 }
873
874 /* Wait until there are no running t_updates */
875 jbd2_journal_wait_updates(journal);
876
877 write_unlock(&journal->j_state_lock);
878
879 /*
880 * We have now established a barrier against other normal updates, but
881 * we also need to barrier against other jbd2_journal_lock_updates() calls
882 * to make sure that we serialise special journal-locked operations
883 * too.
884 */
885 mutex_lock(&journal->j_barrier);
886 }
887
888 /**
889 * jbd2_journal_unlock_updates () - release barrier
890 * @journal: Journal to release the barrier on.
891 *
892 * Release a transaction barrier obtained with jbd2_journal_lock_updates().
893 *
894 * Should be called without the journal lock held.
895 */
jbd2_journal_unlock_updates(journal_t * journal)896 void jbd2_journal_unlock_updates (journal_t *journal)
897 {
898 J_ASSERT(journal->j_barrier_count != 0);
899
900 mutex_unlock(&journal->j_barrier);
901 write_lock(&journal->j_state_lock);
902 --journal->j_barrier_count;
903 write_unlock(&journal->j_state_lock);
904 wake_up_all(&journal->j_wait_transaction_locked);
905 }
906
warn_dirty_buffer(struct buffer_head * bh)907 static void warn_dirty_buffer(struct buffer_head *bh)
908 {
909 printk(KERN_WARNING
910 "JBD2: Spotted dirty metadata buffer (dev = %pg, blocknr = %llu). "
911 "There's a risk of filesystem corruption in case of system "
912 "crash.\n",
913 bh->b_bdev, (unsigned long long)bh->b_blocknr);
914 }
915
916 /* Call t_frozen trigger and copy buffer data into jh->b_frozen_data. */
jbd2_freeze_jh_data(struct journal_head * jh)917 static void jbd2_freeze_jh_data(struct journal_head *jh)
918 {
919 char *source;
920 struct buffer_head *bh = jh2bh(jh);
921
922 J_EXPECT_JH(jh, buffer_uptodate(bh), "Possible IO failure.\n");
923 source = kmap_local_folio(bh->b_folio, bh_offset(bh));
924 /* Fire data frozen trigger just before we copy the data */
925 jbd2_buffer_frozen_trigger(jh, source, jh->b_triggers);
926 memcpy(jh->b_frozen_data, source, bh->b_size);
927 kunmap_local(source);
928
929 /*
930 * Now that the frozen data is saved off, we need to store any matching
931 * triggers.
932 */
933 jh->b_frozen_triggers = jh->b_triggers;
934 }
935
936 /*
937 * If the buffer is already part of the current transaction, then there
938 * is nothing we need to do. If it is already part of a prior
939 * transaction which we are still committing to disk, then we need to
940 * make sure that we do not overwrite the old copy: we do copy-out to
941 * preserve the copy going to disk. We also account the buffer against
942 * the handle's metadata buffer credits (unless the buffer is already
943 * part of the transaction, that is).
944 *
945 */
946 static int
do_get_write_access(handle_t * handle,struct journal_head * jh,int force_copy)947 do_get_write_access(handle_t *handle, struct journal_head *jh,
948 int force_copy)
949 {
950 struct buffer_head *bh;
951 transaction_t *transaction = handle->h_transaction;
952 journal_t *journal;
953 int error;
954 char *frozen_buffer = NULL;
955 unsigned long start_lock, time_lock;
956
957 journal = transaction->t_journal;
958
959 jbd2_debug(5, "journal_head %p, force_copy %d\n", jh, force_copy);
960
961 JBUFFER_TRACE(jh, "entry");
962 repeat:
963 bh = jh2bh(jh);
964
965 /* @@@ Need to check for errors here at some point. */
966
967 start_lock = jiffies;
968 lock_buffer(bh);
969 spin_lock(&jh->b_state_lock);
970
971 /* If it takes too long to lock the buffer, trace it */
972 time_lock = jbd2_time_diff(start_lock, jiffies);
973 if (time_lock > HZ/10)
974 trace_jbd2_lock_buffer_stall(bh->b_bdev->bd_dev,
975 jiffies_to_msecs(time_lock));
976
977 /* We now hold the buffer lock so it is safe to query the buffer
978 * state. Is the buffer dirty?
979 *
980 * If so, there are two possibilities. The buffer may be
981 * non-journaled, and undergoing a quite legitimate writeback.
982 * Otherwise, it is journaled, and we don't expect dirty buffers
983 * in that state (the buffers should be marked JBD_Dirty
984 * instead.) So either the IO is being done under our own
985 * control and this is a bug, or it's a third party IO such as
986 * dump(8) (which may leave the buffer scheduled for read ---
987 * ie. locked but not dirty) or tune2fs (which may actually have
988 * the buffer dirtied, ugh.) */
989
990 if (buffer_dirty(bh) && jh->b_transaction) {
991 warn_dirty_buffer(bh);
992 /*
993 * We need to clean the dirty flag and we must do it under the
994 * buffer lock to be sure we don't race with running write-out.
995 */
996 JBUFFER_TRACE(jh, "Journalling dirty buffer");
997 clear_buffer_dirty(bh);
998 /*
999 * The buffer is going to be added to BJ_Reserved list now and
1000 * nothing guarantees jbd2_journal_dirty_metadata() will be
1001 * ever called for it. So we need to set jbddirty bit here to
1002 * make sure the buffer is dirtied and written out when the
1003 * journaling machinery is done with it.
1004 */
1005 set_buffer_jbddirty(bh);
1006 }
1007
1008 error = -EROFS;
1009 if (is_handle_aborted(handle)) {
1010 spin_unlock(&jh->b_state_lock);
1011 unlock_buffer(bh);
1012 goto out;
1013 }
1014 error = 0;
1015
1016 /*
1017 * The buffer is already part of this transaction if b_transaction or
1018 * b_next_transaction points to it
1019 */
1020 if (jh->b_transaction == transaction ||
1021 jh->b_next_transaction == transaction) {
1022 unlock_buffer(bh);
1023 goto done;
1024 }
1025
1026 /*
1027 * this is the first time this transaction is touching this buffer,
1028 * reset the modified flag
1029 */
1030 jh->b_modified = 0;
1031
1032 /*
1033 * If the buffer is not journaled right now, we need to make sure it
1034 * doesn't get written to disk before the caller actually commits the
1035 * new data
1036 */
1037 if (!jh->b_transaction) {
1038 JBUFFER_TRACE(jh, "no transaction");
1039 J_ASSERT_JH(jh, !jh->b_next_transaction);
1040 JBUFFER_TRACE(jh, "file as BJ_Reserved");
1041 /*
1042 * Make sure all stores to jh (b_modified, b_frozen_data) are
1043 * visible before attaching it to the running transaction.
1044 * Paired with barrier in jbd2_write_access_granted()
1045 */
1046 smp_wmb();
1047 spin_lock(&journal->j_list_lock);
1048 if (test_clear_buffer_dirty(bh)) {
1049 /*
1050 * Execute buffer dirty clearing and jh->b_transaction
1051 * assignment under journal->j_list_lock locked to
1052 * prevent bh being removed from checkpoint list if
1053 * the buffer is in an intermediate state (not dirty
1054 * and jh->b_transaction is NULL).
1055 */
1056 JBUFFER_TRACE(jh, "Journalling dirty buffer");
1057 set_buffer_jbddirty(bh);
1058 }
1059 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
1060 spin_unlock(&journal->j_list_lock);
1061 unlock_buffer(bh);
1062 goto done;
1063 }
1064 unlock_buffer(bh);
1065
1066 /*
1067 * If there is already a copy-out version of this buffer, then we don't
1068 * need to make another one
1069 */
1070 if (jh->b_frozen_data) {
1071 JBUFFER_TRACE(jh, "has frozen data");
1072 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
1073 goto attach_next;
1074 }
1075
1076 JBUFFER_TRACE(jh, "owned by older transaction");
1077 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
1078 J_ASSERT_JH(jh, jh->b_transaction == journal->j_committing_transaction);
1079
1080 /*
1081 * There is one case we have to be very careful about. If the
1082 * committing transaction is currently writing this buffer out to disk
1083 * and has NOT made a copy-out, then we cannot modify the buffer
1084 * contents at all right now. The essence of copy-out is that it is
1085 * the extra copy, not the primary copy, which gets journaled. If the
1086 * primary copy is already going to disk then we cannot do copy-out
1087 * here.
1088 */
1089 if (buffer_shadow(bh)) {
1090 JBUFFER_TRACE(jh, "on shadow: sleep");
1091 spin_unlock(&jh->b_state_lock);
1092 wait_on_bit_io(&bh->b_state, BH_Shadow, TASK_UNINTERRUPTIBLE);
1093 goto repeat;
1094 }
1095
1096 /*
1097 * Only do the copy if the currently-owning transaction still needs it.
1098 * If buffer isn't on BJ_Metadata list, the committing transaction is
1099 * past that stage (here we use the fact that BH_Shadow is set under
1100 * bh_state lock together with refiling to BJ_Shadow list and at this
1101 * point we know the buffer doesn't have BH_Shadow set).
1102 *
1103 * Subtle point, though: if this is a get_undo_access, then we will be
1104 * relying on the frozen_data to contain the new value of the
1105 * committed_data record after the transaction, so we HAVE to force the
1106 * frozen_data copy in that case.
1107 */
1108 if (jh->b_jlist == BJ_Metadata || force_copy) {
1109 JBUFFER_TRACE(jh, "generate frozen data");
1110 if (!frozen_buffer) {
1111 JBUFFER_TRACE(jh, "allocate memory for buffer");
1112 spin_unlock(&jh->b_state_lock);
1113 frozen_buffer = jbd2_alloc(jh2bh(jh)->b_size,
1114 GFP_NOFS | __GFP_NOFAIL);
1115 goto repeat;
1116 }
1117 jh->b_frozen_data = frozen_buffer;
1118 frozen_buffer = NULL;
1119 jbd2_freeze_jh_data(jh);
1120 }
1121 attach_next:
1122 /*
1123 * Make sure all stores to jh (b_modified, b_frozen_data) are visible
1124 * before attaching it to the running transaction. Paired with barrier
1125 * in jbd2_write_access_granted()
1126 */
1127 smp_wmb();
1128 jh->b_next_transaction = transaction;
1129
1130 done:
1131 spin_unlock(&jh->b_state_lock);
1132
1133 /*
1134 * If we are about to journal a buffer, then any revoke pending on it is
1135 * no longer valid
1136 */
1137 jbd2_journal_cancel_revoke(handle, jh);
1138
1139 out:
1140 if (unlikely(frozen_buffer)) /* It's usually NULL */
1141 jbd2_free(frozen_buffer, bh->b_size);
1142
1143 JBUFFER_TRACE(jh, "exit");
1144 return error;
1145 }
1146
1147 /* Fast check whether buffer is already attached to the required transaction */
jbd2_write_access_granted(handle_t * handle,struct buffer_head * bh,bool undo)1148 static bool jbd2_write_access_granted(handle_t *handle, struct buffer_head *bh,
1149 bool undo)
1150 {
1151 struct journal_head *jh;
1152 bool ret = false;
1153
1154 /* Dirty buffers require special handling... */
1155 if (buffer_dirty(bh))
1156 return false;
1157
1158 /*
1159 * RCU protects us from dereferencing freed pages. So the checks we do
1160 * are guaranteed not to oops. However the jh slab object can get freed
1161 * & reallocated while we work with it. So we have to be careful. When
1162 * we see jh attached to the running transaction, we know it must stay
1163 * so until the transaction is committed. Thus jh won't be freed and
1164 * will be attached to the same bh while we run. However it can
1165 * happen jh gets freed, reallocated, and attached to the transaction
1166 * just after we get pointer to it from bh. So we have to be careful
1167 * and recheck jh still belongs to our bh before we return success.
1168 */
1169 rcu_read_lock();
1170 if (!buffer_jbd(bh))
1171 goto out;
1172 /* This should be bh2jh() but that doesn't work with inline functions */
1173 jh = READ_ONCE(bh->b_private);
1174 if (!jh)
1175 goto out;
1176 /* For undo access buffer must have data copied */
1177 if (undo && !jh->b_committed_data)
1178 goto out;
1179 if (READ_ONCE(jh->b_transaction) != handle->h_transaction &&
1180 READ_ONCE(jh->b_next_transaction) != handle->h_transaction)
1181 goto out;
1182 /*
1183 * There are two reasons for the barrier here:
1184 * 1) Make sure to fetch b_bh after we did previous checks so that we
1185 * detect when jh went through free, realloc, attach to transaction
1186 * while we were checking. Paired with implicit barrier in that path.
1187 * 2) So that access to bh done after jbd2_write_access_granted()
1188 * doesn't get reordered and see inconsistent state of concurrent
1189 * do_get_write_access().
1190 */
1191 smp_mb();
1192 if (unlikely(jh->b_bh != bh))
1193 goto out;
1194 ret = true;
1195 out:
1196 rcu_read_unlock();
1197 return ret;
1198 }
1199
1200 /**
1201 * jbd2_journal_get_write_access() - notify intent to modify a buffer
1202 * for metadata (not data) update.
1203 * @handle: transaction to add buffer modifications to
1204 * @bh: bh to be used for metadata writes
1205 *
1206 * Returns: error code or 0 on success.
1207 *
1208 * In full data journalling mode the buffer may be of type BJ_AsyncData,
1209 * because we're ``write()ing`` a buffer which is also part of a shared mapping.
1210 */
1211
jbd2_journal_get_write_access(handle_t * handle,struct buffer_head * bh)1212 int jbd2_journal_get_write_access(handle_t *handle, struct buffer_head *bh)
1213 {
1214 struct journal_head *jh;
1215 journal_t *journal;
1216 int rc;
1217
1218 if (is_handle_aborted(handle))
1219 return -EROFS;
1220
1221 journal = handle->h_transaction->t_journal;
1222 if (jbd2_check_fs_dev_write_error(journal)) {
1223 /*
1224 * If the fs dev has writeback errors, it may have failed
1225 * to async write out metadata buffers in the background.
1226 * In this case, we could read old data from disk and write
1227 * it out again, which may lead to on-disk filesystem
1228 * inconsistency. Aborting journal can avoid it happen.
1229 */
1230 jbd2_journal_abort(journal, -EIO);
1231 return -EIO;
1232 }
1233
1234 if (jbd2_write_access_granted(handle, bh, false))
1235 return 0;
1236
1237 jh = jbd2_journal_add_journal_head(bh);
1238 /* We do not want to get caught playing with fields which the
1239 * log thread also manipulates. Make sure that the buffer
1240 * completes any outstanding IO before proceeding. */
1241 rc = do_get_write_access(handle, jh, 0);
1242 jbd2_journal_put_journal_head(jh);
1243 return rc;
1244 }
1245
1246
1247 /*
1248 * When the user wants to journal a newly created buffer_head
1249 * (ie. getblk() returned a new buffer and we are going to populate it
1250 * manually rather than reading off disk), then we need to keep the
1251 * buffer_head locked until it has been completely filled with new
1252 * data. In this case, we should be able to make the assertion that
1253 * the bh is not already part of an existing transaction.
1254 *
1255 * The buffer should already be locked by the caller by this point.
1256 * There is no lock ranking violation: it was a newly created,
1257 * unlocked buffer beforehand. */
1258
1259 /**
1260 * jbd2_journal_get_create_access () - notify intent to use newly created bh
1261 * @handle: transaction to new buffer to
1262 * @bh: new buffer.
1263 *
1264 * Call this if you create a new bh.
1265 */
jbd2_journal_get_create_access(handle_t * handle,struct buffer_head * bh)1266 int jbd2_journal_get_create_access(handle_t *handle, struct buffer_head *bh)
1267 {
1268 transaction_t *transaction = handle->h_transaction;
1269 journal_t *journal;
1270 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
1271 int err;
1272
1273 jbd2_debug(5, "journal_head %p\n", jh);
1274 err = -EROFS;
1275 if (is_handle_aborted(handle))
1276 goto out;
1277 journal = transaction->t_journal;
1278 err = 0;
1279
1280 JBUFFER_TRACE(jh, "entry");
1281 /*
1282 * The buffer may already belong to this transaction due to pre-zeroing
1283 * in the filesystem's new_block code. It may also be on the previous,
1284 * committing transaction's lists, but it HAS to be in Forget state in
1285 * that case: the transaction must have deleted the buffer for it to be
1286 * reused here.
1287 */
1288 spin_lock(&jh->b_state_lock);
1289 J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
1290 jh->b_transaction == NULL ||
1291 (jh->b_transaction == journal->j_committing_transaction &&
1292 jh->b_jlist == BJ_Forget)));
1293
1294 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
1295 J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
1296
1297 if (jh->b_transaction == NULL) {
1298 /*
1299 * Previous jbd2_journal_forget() could have left the buffer
1300 * with jbddirty bit set because it was being committed. When
1301 * the commit finished, we've filed the buffer for
1302 * checkpointing and marked it dirty. Now we are reallocating
1303 * the buffer so the transaction freeing it must have
1304 * committed and so it's safe to clear the dirty bit.
1305 */
1306 clear_buffer_dirty(jh2bh(jh));
1307 /* first access by this transaction */
1308 jh->b_modified = 0;
1309
1310 JBUFFER_TRACE(jh, "file as BJ_Reserved");
1311 spin_lock(&journal->j_list_lock);
1312 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
1313 spin_unlock(&journal->j_list_lock);
1314 } else if (jh->b_transaction == journal->j_committing_transaction) {
1315 /* first access by this transaction */
1316 jh->b_modified = 0;
1317
1318 JBUFFER_TRACE(jh, "set next transaction");
1319 spin_lock(&journal->j_list_lock);
1320 jh->b_next_transaction = transaction;
1321 spin_unlock(&journal->j_list_lock);
1322 }
1323 spin_unlock(&jh->b_state_lock);
1324
1325 /*
1326 * akpm: I added this. ext3_alloc_branch can pick up new indirect
1327 * blocks which contain freed but then revoked metadata. We need
1328 * to cancel the revoke in case we end up freeing it yet again
1329 * and the reallocating as data - this would cause a second revoke,
1330 * which hits an assertion error.
1331 */
1332 JBUFFER_TRACE(jh, "cancelling revoke");
1333 jbd2_journal_cancel_revoke(handle, jh);
1334 out:
1335 jbd2_journal_put_journal_head(jh);
1336 return err;
1337 }
1338
1339 /**
1340 * jbd2_journal_get_undo_access() - Notify intent to modify metadata with
1341 * non-rewindable consequences
1342 * @handle: transaction
1343 * @bh: buffer to undo
1344 *
1345 * Sometimes there is a need to distinguish between metadata which has
1346 * been committed to disk and that which has not. The ext3fs code uses
1347 * this for freeing and allocating space, we have to make sure that we
1348 * do not reuse freed space until the deallocation has been committed,
1349 * since if we overwrote that space we would make the delete
1350 * un-rewindable in case of a crash.
1351 *
1352 * To deal with that, jbd2_journal_get_undo_access requests write access to a
1353 * buffer for parts of non-rewindable operations such as delete
1354 * operations on the bitmaps. The journaling code must keep a copy of
1355 * the buffer's contents prior to the undo_access call until such time
1356 * as we know that the buffer has definitely been committed to disk.
1357 *
1358 * We never need to know which transaction the committed data is part
1359 * of, buffers touched here are guaranteed to be dirtied later and so
1360 * will be committed to a new transaction in due course, at which point
1361 * we can discard the old committed data pointer.
1362 *
1363 * Returns error number or 0 on success.
1364 */
jbd2_journal_get_undo_access(handle_t * handle,struct buffer_head * bh)1365 int jbd2_journal_get_undo_access(handle_t *handle, struct buffer_head *bh)
1366 {
1367 int err;
1368 struct journal_head *jh;
1369 char *committed_data = NULL;
1370
1371 if (is_handle_aborted(handle))
1372 return -EROFS;
1373
1374 if (jbd2_write_access_granted(handle, bh, true))
1375 return 0;
1376
1377 jh = jbd2_journal_add_journal_head(bh);
1378 JBUFFER_TRACE(jh, "entry");
1379
1380 /*
1381 * Do this first --- it can drop the journal lock, so we want to
1382 * make sure that obtaining the committed_data is done
1383 * atomically wrt. completion of any outstanding commits.
1384 */
1385 err = do_get_write_access(handle, jh, 1);
1386 if (err)
1387 goto out;
1388
1389 repeat:
1390 if (!jh->b_committed_data)
1391 committed_data = jbd2_alloc(jh2bh(jh)->b_size,
1392 GFP_NOFS|__GFP_NOFAIL);
1393
1394 spin_lock(&jh->b_state_lock);
1395 if (!jh->b_committed_data) {
1396 /* Copy out the current buffer contents into the
1397 * preserved, committed copy. */
1398 JBUFFER_TRACE(jh, "generate b_committed data");
1399 if (!committed_data) {
1400 spin_unlock(&jh->b_state_lock);
1401 goto repeat;
1402 }
1403
1404 jh->b_committed_data = committed_data;
1405 committed_data = NULL;
1406 memcpy(jh->b_committed_data, bh->b_data, bh->b_size);
1407 }
1408 spin_unlock(&jh->b_state_lock);
1409 out:
1410 jbd2_journal_put_journal_head(jh);
1411 if (unlikely(committed_data))
1412 jbd2_free(committed_data, bh->b_size);
1413 return err;
1414 }
1415
1416 /**
1417 * jbd2_journal_set_triggers() - Add triggers for commit writeout
1418 * @bh: buffer to trigger on
1419 * @type: struct jbd2_buffer_trigger_type containing the trigger(s).
1420 *
1421 * Set any triggers on this journal_head. This is always safe, because
1422 * triggers for a committing buffer will be saved off, and triggers for
1423 * a running transaction will match the buffer in that transaction.
1424 *
1425 * Call with NULL to clear the triggers.
1426 */
jbd2_journal_set_triggers(struct buffer_head * bh,struct jbd2_buffer_trigger_type * type)1427 void jbd2_journal_set_triggers(struct buffer_head *bh,
1428 struct jbd2_buffer_trigger_type *type)
1429 {
1430 struct journal_head *jh = jbd2_journal_grab_journal_head(bh);
1431
1432 if (WARN_ON_ONCE(!jh))
1433 return;
1434 jh->b_triggers = type;
1435 jbd2_journal_put_journal_head(jh);
1436 }
1437
jbd2_buffer_frozen_trigger(struct journal_head * jh,void * mapped_data,struct jbd2_buffer_trigger_type * triggers)1438 void jbd2_buffer_frozen_trigger(struct journal_head *jh, void *mapped_data,
1439 struct jbd2_buffer_trigger_type *triggers)
1440 {
1441 struct buffer_head *bh = jh2bh(jh);
1442
1443 if (!triggers || !triggers->t_frozen)
1444 return;
1445
1446 triggers->t_frozen(triggers, bh, mapped_data, bh->b_size);
1447 }
1448
jbd2_buffer_abort_trigger(struct journal_head * jh,struct jbd2_buffer_trigger_type * triggers)1449 void jbd2_buffer_abort_trigger(struct journal_head *jh,
1450 struct jbd2_buffer_trigger_type *triggers)
1451 {
1452 if (!triggers || !triggers->t_abort)
1453 return;
1454
1455 triggers->t_abort(triggers, jh2bh(jh));
1456 }
1457
1458 /**
1459 * jbd2_journal_dirty_metadata() - mark a buffer as containing dirty metadata
1460 * @handle: transaction to add buffer to.
1461 * @bh: buffer to mark
1462 *
1463 * mark dirty metadata which needs to be journaled as part of the current
1464 * transaction.
1465 *
1466 * The buffer must have previously had jbd2_journal_get_write_access()
1467 * called so that it has a valid journal_head attached to the buffer
1468 * head.
1469 *
1470 * The buffer is placed on the transaction's metadata list and is marked
1471 * as belonging to the transaction.
1472 *
1473 * Returns error number or 0 on success.
1474 *
1475 * Special care needs to be taken if the buffer already belongs to the
1476 * current committing transaction (in which case we should have frozen
1477 * data present for that commit). In that case, we don't relink the
1478 * buffer: that only gets done when the old transaction finally
1479 * completes its commit.
1480 */
jbd2_journal_dirty_metadata(handle_t * handle,struct buffer_head * bh)1481 int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
1482 {
1483 transaction_t *transaction = handle->h_transaction;
1484 journal_t *journal;
1485 struct journal_head *jh;
1486 int ret = 0;
1487
1488 if (!buffer_jbd(bh))
1489 return -EUCLEAN;
1490
1491 /*
1492 * We don't grab jh reference here since the buffer must be part
1493 * of the running transaction.
1494 */
1495 jh = bh2jh(bh);
1496 jbd2_debug(5, "journal_head %p\n", jh);
1497 JBUFFER_TRACE(jh, "entry");
1498
1499 /*
1500 * This and the following assertions are unreliable since we may see jh
1501 * in inconsistent state unless we grab bh_state lock. But this is
1502 * crucial to catch bugs so let's do a reliable check until the
1503 * lockless handling is fully proven.
1504 */
1505 if (data_race(jh->b_transaction != transaction &&
1506 jh->b_next_transaction != transaction)) {
1507 spin_lock(&jh->b_state_lock);
1508 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
1509 jh->b_next_transaction == transaction);
1510 spin_unlock(&jh->b_state_lock);
1511 }
1512 if (jh->b_modified == 1) {
1513 /* If it's in our transaction it must be in BJ_Metadata list. */
1514 if (data_race(jh->b_transaction == transaction &&
1515 jh->b_jlist != BJ_Metadata)) {
1516 spin_lock(&jh->b_state_lock);
1517 if (jh->b_transaction == transaction &&
1518 jh->b_jlist != BJ_Metadata)
1519 pr_err("JBD2: assertion failure: h_type=%u "
1520 "h_line_no=%u block_no=%llu jlist=%u\n",
1521 handle->h_type, handle->h_line_no,
1522 (unsigned long long) bh->b_blocknr,
1523 jh->b_jlist);
1524 J_ASSERT_JH(jh, jh->b_transaction != transaction ||
1525 jh->b_jlist == BJ_Metadata);
1526 spin_unlock(&jh->b_state_lock);
1527 }
1528 goto out;
1529 }
1530
1531 journal = transaction->t_journal;
1532 spin_lock(&jh->b_state_lock);
1533
1534 if (is_handle_aborted(handle)) {
1535 /*
1536 * Check journal aborting with @jh->b_state_lock locked,
1537 * since 'jh->b_transaction' could be replaced with
1538 * 'jh->b_next_transaction' during old transaction
1539 * committing if journal aborted, which may fail
1540 * assertion on 'jh->b_frozen_data == NULL'.
1541 */
1542 ret = -EROFS;
1543 goto out_unlock_bh;
1544 }
1545
1546 if (jh->b_modified == 0) {
1547 /*
1548 * This buffer's got modified and becoming part
1549 * of the transaction. This needs to be done
1550 * once a transaction -bzzz
1551 */
1552 if (WARN_ON_ONCE(jbd2_handle_buffer_credits(handle) <= 0)) {
1553 ret = -ENOSPC;
1554 goto out_unlock_bh;
1555 }
1556 jh->b_modified = 1;
1557 handle->h_total_credits--;
1558 }
1559
1560 /*
1561 * fastpath, to avoid expensive locking. If this buffer is already
1562 * on the running transaction's metadata list there is nothing to do.
1563 * Nobody can take it off again because there is a handle open.
1564 * I _think_ we're OK here with SMP barriers - a mistaken decision will
1565 * result in this test being false, so we go in and take the locks.
1566 */
1567 if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) {
1568 JBUFFER_TRACE(jh, "fastpath");
1569 if (unlikely(jh->b_transaction !=
1570 journal->j_running_transaction)) {
1571 printk(KERN_ERR "JBD2: %s: "
1572 "jh->b_transaction (%llu, %p, %u) != "
1573 "journal->j_running_transaction (%p, %u)\n",
1574 journal->j_devname,
1575 (unsigned long long) bh->b_blocknr,
1576 jh->b_transaction,
1577 jh->b_transaction ? jh->b_transaction->t_tid : 0,
1578 journal->j_running_transaction,
1579 journal->j_running_transaction ?
1580 journal->j_running_transaction->t_tid : 0);
1581 ret = -EINVAL;
1582 }
1583 goto out_unlock_bh;
1584 }
1585
1586 set_buffer_jbddirty(bh);
1587
1588 /*
1589 * Metadata already on the current transaction list doesn't
1590 * need to be filed. Metadata on another transaction's list must
1591 * be committing, and will be refiled once the commit completes:
1592 * leave it alone for now.
1593 */
1594 if (jh->b_transaction != transaction) {
1595 JBUFFER_TRACE(jh, "already on other transaction");
1596 if (unlikely(((jh->b_transaction !=
1597 journal->j_committing_transaction)) ||
1598 (jh->b_next_transaction != transaction))) {
1599 printk(KERN_ERR "jbd2_journal_dirty_metadata: %s: "
1600 "bad jh for block %llu: "
1601 "transaction (%p, %u), "
1602 "jh->b_transaction (%p, %u), "
1603 "jh->b_next_transaction (%p, %u), jlist %u\n",
1604 journal->j_devname,
1605 (unsigned long long) bh->b_blocknr,
1606 transaction, transaction->t_tid,
1607 jh->b_transaction,
1608 jh->b_transaction ?
1609 jh->b_transaction->t_tid : 0,
1610 jh->b_next_transaction,
1611 jh->b_next_transaction ?
1612 jh->b_next_transaction->t_tid : 0,
1613 jh->b_jlist);
1614 WARN_ON(1);
1615 ret = -EINVAL;
1616 }
1617 /* And this case is illegal: we can't reuse another
1618 * transaction's data buffer, ever. */
1619 goto out_unlock_bh;
1620 }
1621
1622 /* That test should have eliminated the following case: */
1623 J_ASSERT_JH(jh, jh->b_frozen_data == NULL);
1624
1625 JBUFFER_TRACE(jh, "file as BJ_Metadata");
1626 spin_lock(&journal->j_list_lock);
1627 __jbd2_journal_file_buffer(jh, transaction, BJ_Metadata);
1628 spin_unlock(&journal->j_list_lock);
1629 out_unlock_bh:
1630 spin_unlock(&jh->b_state_lock);
1631 out:
1632 JBUFFER_TRACE(jh, "exit");
1633 return ret;
1634 }
1635
1636 /**
1637 * jbd2_journal_forget() - bforget() for potentially-journaled buffers.
1638 * @handle: transaction handle
1639 * @bh: bh to 'forget'
1640 *
1641 * We can only do the bforget if there are no commits pending against the
1642 * buffer. If the buffer is dirty in the current running transaction we
1643 * can safely unlink it.
1644 *
1645 * bh may not be a journalled buffer at all - it may be a non-JBD
1646 * buffer which came off the hashtable. Check for this.
1647 *
1648 * Decrements bh->b_count by one.
1649 *
1650 * Allow this call even if the handle has aborted --- it may be part of
1651 * the caller's cleanup after an abort.
1652 */
jbd2_journal_forget(handle_t * handle,struct buffer_head * bh)1653 int jbd2_journal_forget(handle_t *handle, struct buffer_head *bh)
1654 {
1655 transaction_t *transaction = handle->h_transaction;
1656 journal_t *journal;
1657 struct journal_head *jh;
1658 int drop_reserve = 0;
1659 int err = 0;
1660 int was_modified = 0;
1661
1662 if (is_handle_aborted(handle))
1663 return -EROFS;
1664 journal = transaction->t_journal;
1665
1666 BUFFER_TRACE(bh, "entry");
1667
1668 jh = jbd2_journal_grab_journal_head(bh);
1669 if (!jh) {
1670 __bforget(bh);
1671 return 0;
1672 }
1673
1674 spin_lock(&jh->b_state_lock);
1675
1676 /* Critical error: attempting to delete a bitmap buffer, maybe?
1677 * Don't do any jbd operations, and return an error. */
1678 if (!J_EXPECT_JH(jh, !jh->b_committed_data,
1679 "inconsistent data on disk")) {
1680 err = -EIO;
1681 goto drop;
1682 }
1683
1684 /* keep track of whether or not this transaction modified us */
1685 was_modified = jh->b_modified;
1686
1687 /*
1688 * The buffer's going from the transaction, we must drop
1689 * all references -bzzz
1690 */
1691 jh->b_modified = 0;
1692
1693 if (jh->b_transaction == transaction) {
1694 J_ASSERT_JH(jh, !jh->b_frozen_data);
1695
1696 /* If we are forgetting a buffer which is already part
1697 * of this transaction, then we can just drop it from
1698 * the transaction immediately. */
1699 clear_buffer_dirty(bh);
1700 clear_buffer_jbddirty(bh);
1701
1702 JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
1703
1704 /*
1705 * we only want to drop a reference if this transaction
1706 * modified the buffer
1707 */
1708 if (was_modified)
1709 drop_reserve = 1;
1710
1711 /*
1712 * We are no longer going to journal this buffer.
1713 * However, the commit of this transaction is still
1714 * important to the buffer: the delete that we are now
1715 * processing might obsolete an old log entry, so by
1716 * committing, we can satisfy the buffer's checkpoint.
1717 *
1718 * So, if we have a checkpoint on the buffer, we should
1719 * now refile the buffer on our BJ_Forget list so that
1720 * we know to remove the checkpoint after we commit.
1721 */
1722
1723 spin_lock(&journal->j_list_lock);
1724 if (jh->b_cp_transaction) {
1725 __jbd2_journal_temp_unlink_buffer(jh);
1726 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
1727 } else {
1728 __jbd2_journal_unfile_buffer(jh);
1729 jbd2_journal_put_journal_head(jh);
1730 }
1731 spin_unlock(&journal->j_list_lock);
1732 } else if (jh->b_transaction) {
1733 J_ASSERT_JH(jh, (jh->b_transaction ==
1734 journal->j_committing_transaction));
1735 /* However, if the buffer is still owned by a prior
1736 * (committing) transaction, we can't drop it yet... */
1737 JBUFFER_TRACE(jh, "belongs to older transaction");
1738 /* ... but we CAN drop it from the new transaction through
1739 * marking the buffer as freed and set j_next_transaction to
1740 * the new transaction, so that not only the commit code
1741 * knows it should clear dirty bits when it is done with the
1742 * buffer, but also the buffer can be checkpointed only
1743 * after the new transaction commits. */
1744
1745 set_buffer_freed(bh);
1746
1747 if (!jh->b_next_transaction) {
1748 spin_lock(&journal->j_list_lock);
1749 jh->b_next_transaction = transaction;
1750 spin_unlock(&journal->j_list_lock);
1751 } else {
1752 J_ASSERT(jh->b_next_transaction == transaction);
1753
1754 /*
1755 * only drop a reference if this transaction modified
1756 * the buffer
1757 */
1758 if (was_modified)
1759 drop_reserve = 1;
1760 }
1761 } else {
1762 /*
1763 * Finally, if the buffer is not belongs to any
1764 * transaction, we can just drop it now if it has no
1765 * checkpoint.
1766 */
1767 spin_lock(&journal->j_list_lock);
1768 if (!jh->b_cp_transaction) {
1769 JBUFFER_TRACE(jh, "belongs to none transaction");
1770 spin_unlock(&journal->j_list_lock);
1771 goto drop;
1772 }
1773
1774 /*
1775 * Otherwise, if the buffer has been written to disk,
1776 * it is safe to remove the checkpoint and drop it.
1777 */
1778 if (jbd2_journal_try_remove_checkpoint(jh) >= 0) {
1779 spin_unlock(&journal->j_list_lock);
1780 goto drop;
1781 }
1782
1783 /*
1784 * The buffer is still not written to disk, we should
1785 * attach this buffer to current transaction so that the
1786 * buffer can be checkpointed only after the current
1787 * transaction commits.
1788 */
1789 clear_buffer_dirty(bh);
1790 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
1791 spin_unlock(&journal->j_list_lock);
1792 }
1793 drop:
1794 __brelse(bh);
1795 spin_unlock(&jh->b_state_lock);
1796 jbd2_journal_put_journal_head(jh);
1797 if (drop_reserve) {
1798 /* no need to reserve log space for this block -bzzz */
1799 handle->h_total_credits++;
1800 }
1801 return err;
1802 }
1803
1804 /**
1805 * jbd2_journal_stop() - complete a transaction
1806 * @handle: transaction to complete.
1807 *
1808 * All done for a particular handle.
1809 *
1810 * There is not much action needed here. We just return any remaining
1811 * buffer credits to the transaction and remove the handle. The only
1812 * complication is that we need to start a commit operation if the
1813 * filesystem is marked for synchronous update.
1814 *
1815 * jbd2_journal_stop itself will not usually return an error, but it may
1816 * do so in unusual circumstances. In particular, expect it to
1817 * return -EIO if a jbd2_journal_abort has been executed since the
1818 * transaction began.
1819 */
jbd2_journal_stop(handle_t * handle)1820 int jbd2_journal_stop(handle_t *handle)
1821 {
1822 transaction_t *transaction = handle->h_transaction;
1823 journal_t *journal;
1824 int err = 0, wait_for_commit = 0;
1825 tid_t tid;
1826 pid_t pid;
1827
1828 if (--handle->h_ref > 0) {
1829 jbd2_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
1830 handle->h_ref);
1831 if (is_handle_aborted(handle))
1832 return -EIO;
1833 return 0;
1834 }
1835 if (!transaction) {
1836 /*
1837 * Handle is already detached from the transaction so there is
1838 * nothing to do other than free the handle.
1839 */
1840 memalloc_nofs_restore(handle->saved_alloc_context);
1841 goto free_and_exit;
1842 }
1843 journal = transaction->t_journal;
1844 tid = transaction->t_tid;
1845
1846 if (is_handle_aborted(handle))
1847 err = -EIO;
1848
1849 jbd2_debug(4, "Handle %p going down\n", handle);
1850 trace_jbd2_handle_stats(journal->j_fs_dev->bd_dev,
1851 tid, handle->h_type, handle->h_line_no,
1852 jiffies - handle->h_start_jiffies,
1853 handle->h_sync, handle->h_requested_credits,
1854 (handle->h_requested_credits -
1855 handle->h_total_credits));
1856
1857 /*
1858 * Implement synchronous transaction batching. If the handle
1859 * was synchronous, don't force a commit immediately. Let's
1860 * yield and let another thread piggyback onto this
1861 * transaction. Keep doing that while new threads continue to
1862 * arrive. It doesn't cost much - we're about to run a commit
1863 * and sleep on IO anyway. Speeds up many-threaded, many-dir
1864 * operations by 30x or more...
1865 *
1866 * We try and optimize the sleep time against what the
1867 * underlying disk can do, instead of having a static sleep
1868 * time. This is useful for the case where our storage is so
1869 * fast that it is more optimal to go ahead and force a flush
1870 * and wait for the transaction to be committed than it is to
1871 * wait for an arbitrary amount of time for new writers to
1872 * join the transaction. We achieve this by measuring how
1873 * long it takes to commit a transaction, and compare it with
1874 * how long this transaction has been running, and if run time
1875 * < commit time then we sleep for the delta and commit. This
1876 * greatly helps super fast disks that would see slowdowns as
1877 * more threads started doing fsyncs.
1878 *
1879 * But don't do this if this process was the most recent one
1880 * to perform a synchronous write. We do this to detect the
1881 * case where a single process is doing a stream of sync
1882 * writes. No point in waiting for joiners in that case.
1883 *
1884 * Setting max_batch_time to 0 disables this completely.
1885 */
1886 pid = current->pid;
1887 if (handle->h_sync && journal->j_last_sync_writer != pid &&
1888 journal->j_max_batch_time) {
1889 u64 commit_time, trans_time;
1890
1891 journal->j_last_sync_writer = pid;
1892
1893 read_lock(&journal->j_state_lock);
1894 commit_time = journal->j_average_commit_time;
1895 read_unlock(&journal->j_state_lock);
1896
1897 trans_time = ktime_to_ns(ktime_sub(ktime_get(),
1898 transaction->t_start_time));
1899
1900 commit_time = max_t(u64, commit_time,
1901 1000*journal->j_min_batch_time);
1902 commit_time = min_t(u64, commit_time,
1903 1000*journal->j_max_batch_time);
1904
1905 if (trans_time < commit_time) {
1906 ktime_t expires = ktime_add_ns(ktime_get(),
1907 commit_time);
1908 set_current_state(TASK_UNINTERRUPTIBLE);
1909 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1910 }
1911 }
1912
1913 if (handle->h_sync)
1914 transaction->t_synchronous_commit = 1;
1915
1916 /*
1917 * If the handle is marked SYNC, we need to set another commit
1918 * going! We also want to force a commit if the transaction is too
1919 * old now.
1920 */
1921 if (handle->h_sync ||
1922 time_after_eq(jiffies, transaction->t_expires)) {
1923 /* Do this even for aborted journals: an abort still
1924 * completes the commit thread, it just doesn't write
1925 * anything to disk. */
1926
1927 jbd2_debug(2, "transaction too old, requesting commit for "
1928 "handle %p\n", handle);
1929 /* This is non-blocking */
1930 jbd2_log_start_commit(journal, tid);
1931
1932 /*
1933 * Special case: JBD2_SYNC synchronous updates require us
1934 * to wait for the commit to complete.
1935 */
1936 if (handle->h_sync && !(current->flags & PF_MEMALLOC))
1937 wait_for_commit = 1;
1938 }
1939
1940 /*
1941 * Once stop_this_handle() drops t_updates, the transaction could start
1942 * committing on us and eventually disappear. So we must not
1943 * dereference transaction pointer again after calling
1944 * stop_this_handle().
1945 */
1946 stop_this_handle(handle);
1947
1948 if (wait_for_commit)
1949 err = jbd2_log_wait_commit(journal, tid);
1950
1951 free_and_exit:
1952 if (handle->h_rsv_handle)
1953 jbd2_free_handle(handle->h_rsv_handle);
1954 jbd2_free_handle(handle);
1955 return err;
1956 }
1957
1958 /*
1959 *
1960 * List management code snippets: various functions for manipulating the
1961 * transaction buffer lists.
1962 *
1963 */
1964
1965 /*
1966 * Append a buffer to a transaction list, given the transaction's list head
1967 * pointer.
1968 *
1969 * j_list_lock is held.
1970 *
1971 * jh->b_state_lock is held.
1972 */
1973
1974 static inline void
__blist_add_buffer(struct journal_head ** list,struct journal_head * jh)1975 __blist_add_buffer(struct journal_head **list, struct journal_head *jh)
1976 {
1977 if (!*list) {
1978 jh->b_tnext = jh->b_tprev = jh;
1979 *list = jh;
1980 } else {
1981 /* Insert at the tail of the list to preserve order */
1982 struct journal_head *first = *list, *last = first->b_tprev;
1983 jh->b_tprev = last;
1984 jh->b_tnext = first;
1985 last->b_tnext = first->b_tprev = jh;
1986 }
1987 }
1988
1989 /*
1990 * Remove a buffer from a transaction list, given the transaction's list
1991 * head pointer.
1992 *
1993 * Called with j_list_lock held, and the journal may not be locked.
1994 *
1995 * jh->b_state_lock is held.
1996 */
1997
1998 static inline void
__blist_del_buffer(struct journal_head ** list,struct journal_head * jh)1999 __blist_del_buffer(struct journal_head **list, struct journal_head *jh)
2000 {
2001 if (*list == jh) {
2002 *list = jh->b_tnext;
2003 if (*list == jh)
2004 *list = NULL;
2005 }
2006 jh->b_tprev->b_tnext = jh->b_tnext;
2007 jh->b_tnext->b_tprev = jh->b_tprev;
2008 }
2009
2010 /*
2011 * Remove a buffer from the appropriate transaction list.
2012 *
2013 * Note that this function can *change* the value of
2014 * bh->b_transaction->t_buffers, t_forget, t_shadow_list, t_log_list or
2015 * t_reserved_list. If the caller is holding onto a copy of one of these
2016 * pointers, it could go bad. Generally the caller needs to re-read the
2017 * pointer from the transaction_t.
2018 *
2019 * Called under j_list_lock.
2020 */
__jbd2_journal_temp_unlink_buffer(struct journal_head * jh)2021 static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh)
2022 {
2023 struct journal_head **list = NULL;
2024 transaction_t *transaction;
2025 struct buffer_head *bh = jh2bh(jh);
2026
2027 lockdep_assert_held(&jh->b_state_lock);
2028 transaction = jh->b_transaction;
2029 if (transaction)
2030 assert_spin_locked(&transaction->t_journal->j_list_lock);
2031
2032 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
2033 if (jh->b_jlist != BJ_None)
2034 J_ASSERT_JH(jh, transaction != NULL);
2035
2036 switch (jh->b_jlist) {
2037 case BJ_None:
2038 return;
2039 case BJ_Metadata:
2040 transaction->t_nr_buffers--;
2041 J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
2042 list = &transaction->t_buffers;
2043 break;
2044 case BJ_Forget:
2045 list = &transaction->t_forget;
2046 break;
2047 case BJ_Shadow:
2048 list = &transaction->t_shadow_list;
2049 break;
2050 case BJ_Reserved:
2051 list = &transaction->t_reserved_list;
2052 break;
2053 }
2054
2055 __blist_del_buffer(list, jh);
2056 jh->b_jlist = BJ_None;
2057 if (transaction && is_journal_aborted(transaction->t_journal))
2058 clear_buffer_jbddirty(bh);
2059 else if (test_clear_buffer_jbddirty(bh))
2060 mark_buffer_dirty(bh); /* Expose it to the VM */
2061 }
2062
2063 /*
2064 * Remove buffer from all transactions. The caller is responsible for dropping
2065 * the jh reference that belonged to the transaction.
2066 *
2067 * Called with bh_state lock and j_list_lock
2068 */
__jbd2_journal_unfile_buffer(struct journal_head * jh)2069 static void __jbd2_journal_unfile_buffer(struct journal_head *jh)
2070 {
2071 J_ASSERT_JH(jh, jh->b_transaction != NULL);
2072 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
2073
2074 __jbd2_journal_temp_unlink_buffer(jh);
2075 jh->b_transaction = NULL;
2076 }
2077
2078 /**
2079 * jbd2_journal_try_to_free_buffers() - try to free page buffers.
2080 * @journal: journal for operation
2081 * @folio: Folio to detach data from.
2082 *
2083 * For all the buffers on this page,
2084 * if they are fully written out ordered data, move them onto BUF_CLEAN
2085 * so try_to_free_buffers() can reap them.
2086 *
2087 * This function returns non-zero if we wish try_to_free_buffers()
2088 * to be called. We do this if the page is releasable by try_to_free_buffers().
2089 * We also do it if the page has locked or dirty buffers and the caller wants
2090 * us to perform sync or async writeout.
2091 *
2092 * This complicates JBD locking somewhat. We aren't protected by the
2093 * BKL here. We wish to remove the buffer from its committing or
2094 * running transaction's ->t_datalist via __jbd2_journal_unfile_buffer.
2095 *
2096 * This may *change* the value of transaction_t->t_datalist, so anyone
2097 * who looks at t_datalist needs to lock against this function.
2098 *
2099 * Even worse, someone may be doing a jbd2_journal_dirty_data on this
2100 * buffer. So we need to lock against that. jbd2_journal_dirty_data()
2101 * will come out of the lock with the buffer dirty, which makes it
2102 * ineligible for release here.
2103 *
2104 * Who else is affected by this? hmm... Really the only contender
2105 * is do_get_write_access() - it could be looking at the buffer while
2106 * journal_try_to_free_buffer() is changing its state. But that
2107 * cannot happen because we never reallocate freed data as metadata
2108 * while the data is part of a transaction. Yes?
2109 *
2110 * Return false on failure, true on success
2111 */
jbd2_journal_try_to_free_buffers(journal_t * journal,struct folio * folio)2112 bool jbd2_journal_try_to_free_buffers(journal_t *journal, struct folio *folio)
2113 {
2114 struct buffer_head *head;
2115 struct buffer_head *bh;
2116 bool ret = false;
2117
2118 J_ASSERT(folio_test_locked(folio));
2119
2120 head = folio_buffers(folio);
2121 bh = head;
2122 do {
2123 struct journal_head *jh;
2124
2125 /*
2126 * We take our own ref against the journal_head here to avoid
2127 * having to add tons of locking around each instance of
2128 * jbd2_journal_put_journal_head().
2129 */
2130 jh = jbd2_journal_grab_journal_head(bh);
2131 if (!jh)
2132 continue;
2133
2134 spin_lock(&jh->b_state_lock);
2135 if (!jh->b_transaction && !jh->b_next_transaction) {
2136 spin_lock(&journal->j_list_lock);
2137 /* Remove written-back checkpointed metadata buffer */
2138 if (jh->b_cp_transaction != NULL)
2139 jbd2_journal_try_remove_checkpoint(jh);
2140 spin_unlock(&journal->j_list_lock);
2141 }
2142 spin_unlock(&jh->b_state_lock);
2143 jbd2_journal_put_journal_head(jh);
2144 if (buffer_jbd(bh))
2145 goto busy;
2146 } while ((bh = bh->b_this_page) != head);
2147
2148 ret = try_to_free_buffers(folio);
2149 busy:
2150 return ret;
2151 }
2152
2153 /*
2154 * This buffer is no longer needed. If it is on an older transaction's
2155 * checkpoint list we need to record it on this transaction's forget list
2156 * to pin this buffer (and hence its checkpointing transaction) down until
2157 * this transaction commits. If the buffer isn't on a checkpoint list, we
2158 * release it.
2159 * Returns non-zero if JBD no longer has an interest in the buffer.
2160 *
2161 * Called under j_list_lock.
2162 *
2163 * Called under jh->b_state_lock.
2164 */
__dispose_buffer(struct journal_head * jh,transaction_t * transaction)2165 static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
2166 {
2167 int may_free = 1;
2168 struct buffer_head *bh = jh2bh(jh);
2169
2170 if (jh->b_cp_transaction) {
2171 JBUFFER_TRACE(jh, "on running+cp transaction");
2172 __jbd2_journal_temp_unlink_buffer(jh);
2173 /*
2174 * We don't want to write the buffer anymore, clear the
2175 * bit so that we don't confuse checks in
2176 * __jbd2_journal_file_buffer
2177 */
2178 clear_buffer_dirty(bh);
2179 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
2180 may_free = 0;
2181 } else {
2182 JBUFFER_TRACE(jh, "on running transaction");
2183 __jbd2_journal_unfile_buffer(jh);
2184 jbd2_journal_put_journal_head(jh);
2185 }
2186 return may_free;
2187 }
2188
2189 /*
2190 * jbd2_journal_invalidate_folio
2191 *
2192 * This code is tricky. It has a number of cases to deal with.
2193 *
2194 * There are two invariants which this code relies on:
2195 *
2196 * i_size must be updated on disk before we start calling invalidate_folio
2197 * on the data.
2198 *
2199 * This is done in ext3 by defining an ext3_setattr method which
2200 * updates i_size before truncate gets going. By maintaining this
2201 * invariant, we can be sure that it is safe to throw away any buffers
2202 * attached to the current transaction: once the transaction commits,
2203 * we know that the data will not be needed.
2204 *
2205 * Note however that we can *not* throw away data belonging to the
2206 * previous, committing transaction!
2207 *
2208 * Any disk blocks which *are* part of the previous, committing
2209 * transaction (and which therefore cannot be discarded immediately) are
2210 * not going to be reused in the new running transaction
2211 *
2212 * The bitmap committed_data images guarantee this: any block which is
2213 * allocated in one transaction and removed in the next will be marked
2214 * as in-use in the committed_data bitmap, so cannot be reused until
2215 * the next transaction to delete the block commits. This means that
2216 * leaving committing buffers dirty is quite safe: the disk blocks
2217 * cannot be reallocated to a different file and so buffer aliasing is
2218 * not possible.
2219 *
2220 *
2221 * The above applies mainly to ordered data mode. In writeback mode we
2222 * don't make guarantees about the order in which data hits disk --- in
2223 * particular we don't guarantee that new dirty data is flushed before
2224 * transaction commit --- so it is always safe just to discard data
2225 * immediately in that mode. --sct
2226 */
2227
2228 /*
2229 * The journal_unmap_buffer helper function returns zero if the buffer
2230 * concerned remains pinned as an anonymous buffer belonging to an older
2231 * transaction.
2232 *
2233 * We're outside-transaction here. Either or both of j_running_transaction
2234 * and j_committing_transaction may be NULL.
2235 */
journal_unmap_buffer(journal_t * journal,struct buffer_head * bh,int partial_page)2236 static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh,
2237 int partial_page)
2238 {
2239 transaction_t *transaction;
2240 struct journal_head *jh;
2241 int may_free = 1;
2242
2243 BUFFER_TRACE(bh, "entry");
2244
2245 /*
2246 * It is safe to proceed here without the j_list_lock because the
2247 * buffers cannot be stolen by try_to_free_buffers as long as we are
2248 * holding the page lock. --sct
2249 */
2250
2251 jh = jbd2_journal_grab_journal_head(bh);
2252 if (!jh)
2253 goto zap_buffer_unlocked;
2254
2255 /* OK, we have data buffer in journaled mode */
2256 write_lock(&journal->j_state_lock);
2257 spin_lock(&jh->b_state_lock);
2258 spin_lock(&journal->j_list_lock);
2259
2260 /*
2261 * We cannot remove the buffer from checkpoint lists until the
2262 * transaction adding inode to orphan list (let's call it T)
2263 * is committed. Otherwise if the transaction changing the
2264 * buffer would be cleaned from the journal before T is
2265 * committed, a crash will cause that the correct contents of
2266 * the buffer will be lost. On the other hand we have to
2267 * clear the buffer dirty bit at latest at the moment when the
2268 * transaction marking the buffer as freed in the filesystem
2269 * structures is committed because from that moment on the
2270 * block can be reallocated and used by a different page.
2271 * Since the block hasn't been freed yet but the inode has
2272 * already been added to orphan list, it is safe for us to add
2273 * the buffer to BJ_Forget list of the newest transaction.
2274 *
2275 * Also we have to clear buffer_mapped flag of a truncated buffer
2276 * because the buffer_head may be attached to the page straddling
2277 * i_size (can happen only when blocksize < pagesize) and thus the
2278 * buffer_head can be reused when the file is extended again. So we end
2279 * up keeping around invalidated buffers attached to transactions'
2280 * BJ_Forget list just to stop checkpointing code from cleaning up
2281 * the transaction this buffer was modified in.
2282 */
2283 transaction = jh->b_transaction;
2284 if (transaction == NULL) {
2285 /* First case: not on any transaction. If it
2286 * has no checkpoint link, then we can zap it:
2287 * it's a writeback-mode buffer so we don't care
2288 * if it hits disk safely. */
2289 if (!jh->b_cp_transaction) {
2290 JBUFFER_TRACE(jh, "not on any transaction: zap");
2291 goto zap_buffer;
2292 }
2293
2294 if (!buffer_dirty(bh)) {
2295 /* bdflush has written it. We can drop it now */
2296 __jbd2_journal_remove_checkpoint(jh);
2297 goto zap_buffer;
2298 }
2299
2300 /* OK, it must be in the journal but still not
2301 * written fully to disk: it's metadata or
2302 * journaled data... */
2303
2304 if (journal->j_running_transaction) {
2305 /* ... and once the current transaction has
2306 * committed, the buffer won't be needed any
2307 * longer. */
2308 JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
2309 may_free = __dispose_buffer(jh,
2310 journal->j_running_transaction);
2311 goto zap_buffer;
2312 } else {
2313 /* There is no currently-running transaction. So the
2314 * orphan record which we wrote for this file must have
2315 * passed into commit. We must attach this buffer to
2316 * the committing transaction, if it exists. */
2317 if (journal->j_committing_transaction) {
2318 JBUFFER_TRACE(jh, "give to committing trans");
2319 may_free = __dispose_buffer(jh,
2320 journal->j_committing_transaction);
2321 goto zap_buffer;
2322 } else {
2323 /* The orphan record's transaction has
2324 * committed. We can cleanse this buffer */
2325 clear_buffer_jbddirty(bh);
2326 __jbd2_journal_remove_checkpoint(jh);
2327 goto zap_buffer;
2328 }
2329 }
2330 } else if (transaction == journal->j_committing_transaction) {
2331 JBUFFER_TRACE(jh, "on committing transaction");
2332 /*
2333 * The buffer is committing, we simply cannot touch
2334 * it. If the page is straddling i_size we have to wait
2335 * for commit and try again.
2336 */
2337 if (partial_page) {
2338 spin_unlock(&journal->j_list_lock);
2339 spin_unlock(&jh->b_state_lock);
2340 write_unlock(&journal->j_state_lock);
2341 jbd2_journal_put_journal_head(jh);
2342 /* Already zapped buffer? Nothing to do... */
2343 if (!bh->b_bdev)
2344 return 0;
2345 return -EBUSY;
2346 }
2347 /*
2348 * OK, buffer won't be reachable after truncate. We just clear
2349 * b_modified to not confuse transaction credit accounting, and
2350 * set j_next_transaction to the running transaction (if there
2351 * is one) and mark buffer as freed so that commit code knows
2352 * it should clear dirty bits when it is done with the buffer.
2353 */
2354 set_buffer_freed(bh);
2355 if (journal->j_running_transaction && buffer_jbddirty(bh))
2356 jh->b_next_transaction = journal->j_running_transaction;
2357 jh->b_modified = 0;
2358 spin_unlock(&journal->j_list_lock);
2359 spin_unlock(&jh->b_state_lock);
2360 write_unlock(&journal->j_state_lock);
2361 jbd2_journal_put_journal_head(jh);
2362 return 0;
2363 } else {
2364 /* Good, the buffer belongs to the running transaction.
2365 * We are writing our own transaction's data, not any
2366 * previous one's, so it is safe to throw it away
2367 * (remember that we expect the filesystem to have set
2368 * i_size already for this truncate so recovery will not
2369 * expose the disk blocks we are discarding here.) */
2370 J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
2371 JBUFFER_TRACE(jh, "on running transaction");
2372 may_free = __dispose_buffer(jh, transaction);
2373 }
2374
2375 zap_buffer:
2376 /*
2377 * This is tricky. Although the buffer is truncated, it may be reused
2378 * if blocksize < pagesize and it is attached to the page straddling
2379 * EOF. Since the buffer might have been added to BJ_Forget list of the
2380 * running transaction, journal_get_write_access() won't clear
2381 * b_modified and credit accounting gets confused. So clear b_modified
2382 * here.
2383 */
2384 jh->b_modified = 0;
2385 spin_unlock(&journal->j_list_lock);
2386 spin_unlock(&jh->b_state_lock);
2387 write_unlock(&journal->j_state_lock);
2388 jbd2_journal_put_journal_head(jh);
2389 zap_buffer_unlocked:
2390 clear_buffer_dirty(bh);
2391 J_ASSERT_BH(bh, !buffer_jbddirty(bh));
2392 clear_buffer_mapped(bh);
2393 clear_buffer_req(bh);
2394 clear_buffer_new(bh);
2395 clear_buffer_delay(bh);
2396 clear_buffer_unwritten(bh);
2397 bh->b_bdev = NULL;
2398 return may_free;
2399 }
2400
2401 /**
2402 * jbd2_journal_invalidate_folio()
2403 * @journal: journal to use for flush...
2404 * @folio: folio to flush
2405 * @offset: start of the range to invalidate
2406 * @length: length of the range to invalidate
2407 *
2408 * Reap page buffers containing data after in the specified range in page.
2409 * Can return -EBUSY if buffers are part of the committing transaction and
2410 * the page is straddling i_size. Caller then has to wait for current commit
2411 * and try again.
2412 */
jbd2_journal_invalidate_folio(journal_t * journal,struct folio * folio,size_t offset,size_t length)2413 int jbd2_journal_invalidate_folio(journal_t *journal, struct folio *folio,
2414 size_t offset, size_t length)
2415 {
2416 struct buffer_head *head, *bh, *next;
2417 unsigned int stop = offset + length;
2418 unsigned int curr_off = 0;
2419 int partial_page = (offset || length < folio_size(folio));
2420 int may_free = 1;
2421 int ret = 0;
2422
2423 if (!folio_test_locked(folio))
2424 BUG();
2425 head = folio_buffers(folio);
2426 if (!head)
2427 return 0;
2428
2429 BUG_ON(stop > folio_size(folio) || stop < length);
2430
2431 /* We will potentially be playing with lists other than just the
2432 * data lists (especially for journaled data mode), so be
2433 * cautious in our locking. */
2434
2435 bh = head;
2436 do {
2437 unsigned int next_off = curr_off + bh->b_size;
2438 next = bh->b_this_page;
2439
2440 if (next_off > stop)
2441 return 0;
2442
2443 if (offset <= curr_off) {
2444 /* This block is wholly outside the truncation point */
2445 lock_buffer(bh);
2446 ret = journal_unmap_buffer(journal, bh, partial_page);
2447 unlock_buffer(bh);
2448 if (ret < 0)
2449 return ret;
2450 may_free &= ret;
2451 }
2452 curr_off = next_off;
2453 bh = next;
2454
2455 } while (bh != head);
2456
2457 if (!partial_page) {
2458 if (may_free && try_to_free_buffers(folio))
2459 J_ASSERT(!folio_buffers(folio));
2460 }
2461 return 0;
2462 }
2463
2464 /*
2465 * File a buffer on the given transaction list.
2466 */
__jbd2_journal_file_buffer(struct journal_head * jh,transaction_t * transaction,int jlist)2467 void __jbd2_journal_file_buffer(struct journal_head *jh,
2468 transaction_t *transaction, int jlist)
2469 {
2470 struct journal_head **list = NULL;
2471 int was_dirty = 0;
2472 struct buffer_head *bh = jh2bh(jh);
2473
2474 lockdep_assert_held(&jh->b_state_lock);
2475 assert_spin_locked(&transaction->t_journal->j_list_lock);
2476
2477 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
2478 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
2479 jh->b_transaction == NULL);
2480
2481 if (jh->b_transaction && jh->b_jlist == jlist)
2482 return;
2483
2484 if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
2485 jlist == BJ_Shadow || jlist == BJ_Forget) {
2486 /*
2487 * For metadata buffers, we track dirty bit in buffer_jbddirty
2488 * instead of buffer_dirty. We should not see a dirty bit set
2489 * here because we clear it in do_get_write_access but e.g.
2490 * tune2fs can modify the sb and set the dirty bit at any time
2491 * so we try to gracefully handle that.
2492 */
2493 if (buffer_dirty(bh))
2494 warn_dirty_buffer(bh);
2495 if (test_clear_buffer_dirty(bh) ||
2496 test_clear_buffer_jbddirty(bh))
2497 was_dirty = 1;
2498 }
2499
2500 if (jh->b_transaction)
2501 __jbd2_journal_temp_unlink_buffer(jh);
2502 else
2503 jbd2_journal_grab_journal_head(bh);
2504 jh->b_transaction = transaction;
2505
2506 switch (jlist) {
2507 case BJ_None:
2508 J_ASSERT_JH(jh, !jh->b_committed_data);
2509 J_ASSERT_JH(jh, !jh->b_frozen_data);
2510 return;
2511 case BJ_Metadata:
2512 transaction->t_nr_buffers++;
2513 list = &transaction->t_buffers;
2514 break;
2515 case BJ_Forget:
2516 list = &transaction->t_forget;
2517 break;
2518 case BJ_Shadow:
2519 list = &transaction->t_shadow_list;
2520 break;
2521 case BJ_Reserved:
2522 list = &transaction->t_reserved_list;
2523 break;
2524 }
2525
2526 __blist_add_buffer(list, jh);
2527 jh->b_jlist = jlist;
2528
2529 if (was_dirty)
2530 set_buffer_jbddirty(bh);
2531 }
2532
jbd2_journal_file_buffer(struct journal_head * jh,transaction_t * transaction,int jlist)2533 void jbd2_journal_file_buffer(struct journal_head *jh,
2534 transaction_t *transaction, int jlist)
2535 {
2536 spin_lock(&jh->b_state_lock);
2537 spin_lock(&transaction->t_journal->j_list_lock);
2538 __jbd2_journal_file_buffer(jh, transaction, jlist);
2539 spin_unlock(&transaction->t_journal->j_list_lock);
2540 spin_unlock(&jh->b_state_lock);
2541 }
2542
2543 /*
2544 * Remove a buffer from its current buffer list in preparation for
2545 * dropping it from its current transaction entirely. If the buffer has
2546 * already started to be used by a subsequent transaction, refile the
2547 * buffer on that transaction's metadata list.
2548 *
2549 * Called under j_list_lock
2550 * Called under jh->b_state_lock
2551 *
2552 * When this function returns true, there's no next transaction to refile to
2553 * and the caller has to drop jh reference through
2554 * jbd2_journal_put_journal_head().
2555 */
__jbd2_journal_refile_buffer(struct journal_head * jh)2556 bool __jbd2_journal_refile_buffer(struct journal_head *jh)
2557 {
2558 int was_dirty, jlist;
2559 struct buffer_head *bh = jh2bh(jh);
2560
2561 lockdep_assert_held(&jh->b_state_lock);
2562 if (jh->b_transaction)
2563 assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
2564
2565 /* If the buffer is now unused, just drop it. */
2566 if (jh->b_next_transaction == NULL) {
2567 __jbd2_journal_unfile_buffer(jh);
2568 return true;
2569 }
2570
2571 /*
2572 * It has been modified by a later transaction: add it to the new
2573 * transaction's metadata list.
2574 */
2575
2576 was_dirty = test_clear_buffer_jbddirty(bh);
2577 __jbd2_journal_temp_unlink_buffer(jh);
2578
2579 /*
2580 * b_transaction must be set, otherwise the new b_transaction won't
2581 * be holding jh reference
2582 */
2583 J_ASSERT_JH(jh, jh->b_transaction != NULL);
2584
2585 /*
2586 * We set b_transaction here because b_next_transaction will inherit
2587 * our jh reference and thus __jbd2_journal_file_buffer() must not
2588 * take a new one.
2589 */
2590 WRITE_ONCE(jh->b_transaction, jh->b_next_transaction);
2591 WRITE_ONCE(jh->b_next_transaction, NULL);
2592 if (buffer_freed(bh))
2593 jlist = BJ_Forget;
2594 else if (jh->b_modified)
2595 jlist = BJ_Metadata;
2596 else
2597 jlist = BJ_Reserved;
2598 __jbd2_journal_file_buffer(jh, jh->b_transaction, jlist);
2599 J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
2600
2601 if (was_dirty)
2602 set_buffer_jbddirty(bh);
2603 return false;
2604 }
2605
2606 /*
2607 * __jbd2_journal_refile_buffer() with necessary locking added. We take our
2608 * bh reference so that we can safely unlock bh.
2609 *
2610 * The jh and bh may be freed by this call.
2611 */
jbd2_journal_refile_buffer(journal_t * journal,struct journal_head * jh)2612 void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh)
2613 {
2614 bool drop;
2615
2616 spin_lock(&jh->b_state_lock);
2617 spin_lock(&journal->j_list_lock);
2618 drop = __jbd2_journal_refile_buffer(jh);
2619 spin_unlock(&jh->b_state_lock);
2620 spin_unlock(&journal->j_list_lock);
2621 if (drop)
2622 jbd2_journal_put_journal_head(jh);
2623 }
2624
2625 /*
2626 * File inode in the inode list of the handle's transaction
2627 */
jbd2_journal_file_inode(handle_t * handle,struct jbd2_inode * jinode,unsigned long flags,loff_t start_byte,loff_t end_byte)2628 static int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode,
2629 unsigned long flags, loff_t start_byte, loff_t end_byte)
2630 {
2631 transaction_t *transaction = handle->h_transaction;
2632 journal_t *journal;
2633
2634 if (is_handle_aborted(handle))
2635 return -EROFS;
2636 journal = transaction->t_journal;
2637
2638 jbd2_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino,
2639 transaction->t_tid);
2640
2641 spin_lock(&journal->j_list_lock);
2642 jinode->i_flags |= flags;
2643
2644 if (jinode->i_dirty_end) {
2645 jinode->i_dirty_start = min(jinode->i_dirty_start, start_byte);
2646 jinode->i_dirty_end = max(jinode->i_dirty_end, end_byte);
2647 } else {
2648 jinode->i_dirty_start = start_byte;
2649 jinode->i_dirty_end = end_byte;
2650 }
2651
2652 /* Is inode already attached where we need it? */
2653 if (jinode->i_transaction == transaction ||
2654 jinode->i_next_transaction == transaction)
2655 goto done;
2656
2657 /*
2658 * We only ever set this variable to 1 so the test is safe. Since
2659 * t_need_data_flush is likely to be set, we do the test to save some
2660 * cacheline bouncing
2661 */
2662 if (!transaction->t_need_data_flush)
2663 transaction->t_need_data_flush = 1;
2664 /* On some different transaction's list - should be
2665 * the committing one */
2666 if (jinode->i_transaction) {
2667 J_ASSERT(jinode->i_next_transaction == NULL);
2668 J_ASSERT(jinode->i_transaction ==
2669 journal->j_committing_transaction);
2670 jinode->i_next_transaction = transaction;
2671 goto done;
2672 }
2673 /* Not on any transaction list... */
2674 J_ASSERT(!jinode->i_next_transaction);
2675 jinode->i_transaction = transaction;
2676 list_add(&jinode->i_list, &transaction->t_inode_list);
2677 done:
2678 spin_unlock(&journal->j_list_lock);
2679
2680 return 0;
2681 }
2682
jbd2_journal_inode_ranged_write(handle_t * handle,struct jbd2_inode * jinode,loff_t start_byte,loff_t length)2683 int jbd2_journal_inode_ranged_write(handle_t *handle,
2684 struct jbd2_inode *jinode, loff_t start_byte, loff_t length)
2685 {
2686 return jbd2_journal_file_inode(handle, jinode,
2687 JI_WRITE_DATA | JI_WAIT_DATA, start_byte,
2688 start_byte + length - 1);
2689 }
2690
jbd2_journal_inode_ranged_wait(handle_t * handle,struct jbd2_inode * jinode,loff_t start_byte,loff_t length)2691 int jbd2_journal_inode_ranged_wait(handle_t *handle, struct jbd2_inode *jinode,
2692 loff_t start_byte, loff_t length)
2693 {
2694 return jbd2_journal_file_inode(handle, jinode, JI_WAIT_DATA,
2695 start_byte, start_byte + length - 1);
2696 }
2697
2698 /*
2699 * File truncate and transaction commit interact with each other in a
2700 * non-trivial way. If a transaction writing data block A is
2701 * committing, we cannot discard the data by truncate until we have
2702 * written them. Otherwise if we crashed after the transaction with
2703 * write has committed but before the transaction with truncate has
2704 * committed, we could see stale data in block A. This function is a
2705 * helper to solve this problem. It starts writeout of the truncated
2706 * part in case it is in the committing transaction.
2707 *
2708 * Filesystem code must call this function when inode is journaled in
2709 * ordered mode before truncation happens and after the inode has been
2710 * placed on orphan list with the new inode size. The second condition
2711 * avoids the race that someone writes new data and we start
2712 * committing the transaction after this function has been called but
2713 * before a transaction for truncate is started (and furthermore it
2714 * allows us to optimize the case where the addition to orphan list
2715 * happens in the same transaction as write --- we don't have to write
2716 * any data in such case).
2717 */
jbd2_journal_begin_ordered_truncate(journal_t * journal,struct jbd2_inode * jinode,loff_t new_size)2718 int jbd2_journal_begin_ordered_truncate(journal_t *journal,
2719 struct jbd2_inode *jinode,
2720 loff_t new_size)
2721 {
2722 transaction_t *inode_trans, *commit_trans;
2723 int ret = 0;
2724
2725 /* This is a quick check to avoid locking if not necessary */
2726 if (!jinode->i_transaction)
2727 goto out;
2728 /* Locks are here just to force reading of recent values, it is
2729 * enough that the transaction was not committing before we started
2730 * a transaction adding the inode to orphan list */
2731 read_lock(&journal->j_state_lock);
2732 commit_trans = journal->j_committing_transaction;
2733 read_unlock(&journal->j_state_lock);
2734 spin_lock(&journal->j_list_lock);
2735 inode_trans = jinode->i_transaction;
2736 spin_unlock(&journal->j_list_lock);
2737 if (inode_trans == commit_trans) {
2738 ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping,
2739 new_size, LLONG_MAX);
2740 if (ret)
2741 jbd2_journal_abort(journal, ret);
2742 }
2743 out:
2744 return ret;
2745 }
2746