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, 1, _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 rc = jbd2_check_fs_dev_write_error(journal);
1223 if (rc) {
1224 /*
1225 * If the fs dev has writeback errors, it may have failed
1226 * to async write out metadata buffers in the background.
1227 * In this case, we could read old data from disk and write
1228 * it out again, which may lead to on-disk filesystem
1229 * inconsistency. Aborting journal can avoid it happen.
1230 */
1231 jbd2_journal_abort(journal, rc);
1232 return -EIO;
1233 }
1234
1235 if (jbd2_write_access_granted(handle, bh, false))
1236 return 0;
1237
1238 jh = jbd2_journal_add_journal_head(bh);
1239 /* We do not want to get caught playing with fields which the
1240 * log thread also manipulates. Make sure that the buffer
1241 * completes any outstanding IO before proceeding. */
1242 rc = do_get_write_access(handle, jh, 0);
1243 jbd2_journal_put_journal_head(jh);
1244 return rc;
1245 }
1246
1247
1248 /*
1249 * When the user wants to journal a newly created buffer_head
1250 * (ie. getblk() returned a new buffer and we are going to populate it
1251 * manually rather than reading off disk), then we need to keep the
1252 * buffer_head locked until it has been completely filled with new
1253 * data. In this case, we should be able to make the assertion that
1254 * the bh is not already part of an existing transaction.
1255 *
1256 * The buffer should already be locked by the caller by this point.
1257 * There is no lock ranking violation: it was a newly created,
1258 * unlocked buffer beforehand. */
1259
1260 /**
1261 * jbd2_journal_get_create_access () - notify intent to use newly created bh
1262 * @handle: transaction to new buffer to
1263 * @bh: new buffer.
1264 *
1265 * Call this if you create a new bh.
1266 */
jbd2_journal_get_create_access(handle_t * handle,struct buffer_head * bh)1267 int jbd2_journal_get_create_access(handle_t *handle, struct buffer_head *bh)
1268 {
1269 transaction_t *transaction = handle->h_transaction;
1270 journal_t *journal;
1271 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
1272 int err;
1273
1274 jbd2_debug(5, "journal_head %p\n", jh);
1275 err = -EROFS;
1276 if (is_handle_aborted(handle))
1277 goto out;
1278 journal = transaction->t_journal;
1279 err = 0;
1280
1281 JBUFFER_TRACE(jh, "entry");
1282 /*
1283 * The buffer may already belong to this transaction due to pre-zeroing
1284 * in the filesystem's new_block code. It may also be on the previous,
1285 * committing transaction's lists, but it HAS to be in Forget state in
1286 * that case: the transaction must have deleted the buffer for it to be
1287 * reused here.
1288 * In the case of file system data inconsistency, for example, if the
1289 * block bitmap of a referenced block is not set, it can lead to the
1290 * situation where a block being committed is allocated and used again.
1291 * As a result, the following condition will not be satisfied, so here
1292 * we directly trigger a JBD abort instead of immediately invoking
1293 * bugon.
1294 */
1295 spin_lock(&jh->b_state_lock);
1296 if (!(jh->b_transaction == transaction || jh->b_transaction == NULL ||
1297 (jh->b_transaction == journal->j_committing_transaction &&
1298 jh->b_jlist == BJ_Forget)) || jh->b_next_transaction != NULL) {
1299 err = -EROFS;
1300 spin_unlock(&jh->b_state_lock);
1301 jbd2_journal_abort(journal, err);
1302 goto out;
1303 }
1304
1305 J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
1306
1307 if (jh->b_transaction == NULL) {
1308 /*
1309 * Previous jbd2_journal_forget() could have left the buffer
1310 * with jbddirty bit set because it was being committed. When
1311 * the commit finished, we've filed the buffer for
1312 * checkpointing and marked it dirty. Now we are reallocating
1313 * the buffer so the transaction freeing it must have
1314 * committed and so it's safe to clear the dirty bit.
1315 */
1316 clear_buffer_dirty(jh2bh(jh));
1317 /* first access by this transaction */
1318 jh->b_modified = 0;
1319
1320 JBUFFER_TRACE(jh, "file as BJ_Reserved");
1321 spin_lock(&journal->j_list_lock);
1322 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
1323 spin_unlock(&journal->j_list_lock);
1324 } else if (jh->b_transaction == journal->j_committing_transaction) {
1325 /* first access by this transaction */
1326 jh->b_modified = 0;
1327
1328 JBUFFER_TRACE(jh, "set next transaction");
1329 spin_lock(&journal->j_list_lock);
1330 jh->b_next_transaction = transaction;
1331 spin_unlock(&journal->j_list_lock);
1332 }
1333 spin_unlock(&jh->b_state_lock);
1334
1335 /*
1336 * akpm: I added this. ext3_alloc_branch can pick up new indirect
1337 * blocks which contain freed but then revoked metadata. We need
1338 * to cancel the revoke in case we end up freeing it yet again
1339 * and the reallocating as data - this would cause a second revoke,
1340 * which hits an assertion error.
1341 */
1342 JBUFFER_TRACE(jh, "cancelling revoke");
1343 jbd2_journal_cancel_revoke(handle, jh);
1344 out:
1345 jbd2_journal_put_journal_head(jh);
1346 return err;
1347 }
1348
1349 /**
1350 * jbd2_journal_get_undo_access() - Notify intent to modify metadata with
1351 * non-rewindable consequences
1352 * @handle: transaction
1353 * @bh: buffer to undo
1354 *
1355 * Sometimes there is a need to distinguish between metadata which has
1356 * been committed to disk and that which has not. The ext3fs code uses
1357 * this for freeing and allocating space, we have to make sure that we
1358 * do not reuse freed space until the deallocation has been committed,
1359 * since if we overwrote that space we would make the delete
1360 * un-rewindable in case of a crash.
1361 *
1362 * To deal with that, jbd2_journal_get_undo_access requests write access to a
1363 * buffer for parts of non-rewindable operations such as delete
1364 * operations on the bitmaps. The journaling code must keep a copy of
1365 * the buffer's contents prior to the undo_access call until such time
1366 * as we know that the buffer has definitely been committed to disk.
1367 *
1368 * We never need to know which transaction the committed data is part
1369 * of, buffers touched here are guaranteed to be dirtied later and so
1370 * will be committed to a new transaction in due course, at which point
1371 * we can discard the old committed data pointer.
1372 *
1373 * Returns error number or 0 on success.
1374 */
jbd2_journal_get_undo_access(handle_t * handle,struct buffer_head * bh)1375 int jbd2_journal_get_undo_access(handle_t *handle, struct buffer_head *bh)
1376 {
1377 int err;
1378 struct journal_head *jh;
1379 char *committed_data = NULL;
1380
1381 if (is_handle_aborted(handle))
1382 return -EROFS;
1383
1384 if (jbd2_write_access_granted(handle, bh, true))
1385 return 0;
1386
1387 jh = jbd2_journal_add_journal_head(bh);
1388 JBUFFER_TRACE(jh, "entry");
1389
1390 /*
1391 * Do this first --- it can drop the journal lock, so we want to
1392 * make sure that obtaining the committed_data is done
1393 * atomically wrt. completion of any outstanding commits.
1394 */
1395 err = do_get_write_access(handle, jh, 1);
1396 if (err)
1397 goto out;
1398
1399 repeat:
1400 if (!jh->b_committed_data)
1401 committed_data = jbd2_alloc(jh2bh(jh)->b_size,
1402 GFP_NOFS|__GFP_NOFAIL);
1403
1404 spin_lock(&jh->b_state_lock);
1405 if (!jh->b_committed_data) {
1406 /* Copy out the current buffer contents into the
1407 * preserved, committed copy. */
1408 JBUFFER_TRACE(jh, "generate b_committed data");
1409 if (!committed_data) {
1410 spin_unlock(&jh->b_state_lock);
1411 goto repeat;
1412 }
1413
1414 jh->b_committed_data = committed_data;
1415 committed_data = NULL;
1416 memcpy(jh->b_committed_data, bh->b_data, bh->b_size);
1417 }
1418 spin_unlock(&jh->b_state_lock);
1419 out:
1420 jbd2_journal_put_journal_head(jh);
1421 if (unlikely(committed_data))
1422 jbd2_free(committed_data, bh->b_size);
1423 return err;
1424 }
1425
1426 /**
1427 * jbd2_journal_set_triggers() - Add triggers for commit writeout
1428 * @bh: buffer to trigger on
1429 * @type: struct jbd2_buffer_trigger_type containing the trigger(s).
1430 *
1431 * Set any triggers on this journal_head. This is always safe, because
1432 * triggers for a committing buffer will be saved off, and triggers for
1433 * a running transaction will match the buffer in that transaction.
1434 *
1435 * Call with NULL to clear the triggers.
1436 */
jbd2_journal_set_triggers(struct buffer_head * bh,struct jbd2_buffer_trigger_type * type)1437 void jbd2_journal_set_triggers(struct buffer_head *bh,
1438 struct jbd2_buffer_trigger_type *type)
1439 {
1440 struct journal_head *jh = jbd2_journal_grab_journal_head(bh);
1441
1442 if (WARN_ON_ONCE(!jh))
1443 return;
1444 jh->b_triggers = type;
1445 jbd2_journal_put_journal_head(jh);
1446 }
1447
jbd2_buffer_frozen_trigger(struct journal_head * jh,void * mapped_data,struct jbd2_buffer_trigger_type * triggers)1448 void jbd2_buffer_frozen_trigger(struct journal_head *jh, void *mapped_data,
1449 struct jbd2_buffer_trigger_type *triggers)
1450 {
1451 struct buffer_head *bh = jh2bh(jh);
1452
1453 if (!triggers || !triggers->t_frozen)
1454 return;
1455
1456 triggers->t_frozen(triggers, bh, mapped_data, bh->b_size);
1457 }
1458
jbd2_buffer_abort_trigger(struct journal_head * jh,struct jbd2_buffer_trigger_type * triggers)1459 void jbd2_buffer_abort_trigger(struct journal_head *jh,
1460 struct jbd2_buffer_trigger_type *triggers)
1461 {
1462 if (!triggers || !triggers->t_abort)
1463 return;
1464
1465 triggers->t_abort(triggers, jh2bh(jh));
1466 }
1467
1468 /**
1469 * jbd2_journal_dirty_metadata() - mark a buffer as containing dirty metadata
1470 * @handle: transaction to add buffer to.
1471 * @bh: buffer to mark
1472 *
1473 * mark dirty metadata which needs to be journaled as part of the current
1474 * transaction.
1475 *
1476 * The buffer must have previously had jbd2_journal_get_write_access()
1477 * called so that it has a valid journal_head attached to the buffer
1478 * head.
1479 *
1480 * The buffer is placed on the transaction's metadata list and is marked
1481 * as belonging to the transaction.
1482 *
1483 * Returns error number or 0 on success.
1484 *
1485 * Special care needs to be taken if the buffer already belongs to the
1486 * current committing transaction (in which case we should have frozen
1487 * data present for that commit). In that case, we don't relink the
1488 * buffer: that only gets done when the old transaction finally
1489 * completes its commit.
1490 */
jbd2_journal_dirty_metadata(handle_t * handle,struct buffer_head * bh)1491 int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
1492 {
1493 transaction_t *transaction = handle->h_transaction;
1494 journal_t *journal;
1495 struct journal_head *jh;
1496 int ret = 0;
1497
1498 if (!buffer_jbd(bh))
1499 return -EUCLEAN;
1500
1501 /*
1502 * We don't grab jh reference here since the buffer must be part
1503 * of the running transaction.
1504 */
1505 jh = bh2jh(bh);
1506 jbd2_debug(5, "journal_head %p\n", jh);
1507 JBUFFER_TRACE(jh, "entry");
1508
1509 /*
1510 * This and the following assertions are unreliable since we may see jh
1511 * in inconsistent state unless we grab bh_state lock. But this is
1512 * crucial to catch bugs so let's do a reliable check until the
1513 * lockless handling is fully proven.
1514 */
1515 if (data_race(jh->b_transaction != transaction &&
1516 jh->b_next_transaction != transaction)) {
1517 spin_lock(&jh->b_state_lock);
1518 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
1519 jh->b_next_transaction == transaction);
1520 spin_unlock(&jh->b_state_lock);
1521 }
1522 if (data_race(jh->b_modified == 1)) {
1523 /* If it's in our transaction it must be in BJ_Metadata list. */
1524 if (data_race(jh->b_transaction == transaction &&
1525 jh->b_jlist != BJ_Metadata)) {
1526 spin_lock(&jh->b_state_lock);
1527 if (jh->b_transaction == transaction &&
1528 jh->b_jlist != BJ_Metadata)
1529 pr_err("JBD2: assertion failure: h_type=%u "
1530 "h_line_no=%u block_no=%llu jlist=%u\n",
1531 handle->h_type, handle->h_line_no,
1532 (unsigned long long) bh->b_blocknr,
1533 jh->b_jlist);
1534 J_ASSERT_JH(jh, jh->b_transaction != transaction ||
1535 jh->b_jlist == BJ_Metadata);
1536 spin_unlock(&jh->b_state_lock);
1537 }
1538 goto out;
1539 }
1540
1541 spin_lock(&jh->b_state_lock);
1542
1543 if (is_handle_aborted(handle)) {
1544 /*
1545 * Check journal aborting with @jh->b_state_lock locked,
1546 * since 'jh->b_transaction' could be replaced with
1547 * 'jh->b_next_transaction' during old transaction
1548 * committing if journal aborted, which may fail
1549 * assertion on 'jh->b_frozen_data == NULL'.
1550 */
1551 ret = -EROFS;
1552 goto out_unlock_bh;
1553 }
1554
1555 journal = transaction->t_journal;
1556
1557 if (jh->b_modified == 0) {
1558 /*
1559 * This buffer's got modified and becoming part
1560 * of the transaction. This needs to be done
1561 * once a transaction -bzzz
1562 */
1563 if (WARN_ON_ONCE(jbd2_handle_buffer_credits(handle) <= 0)) {
1564 ret = -ENOSPC;
1565 goto out_unlock_bh;
1566 }
1567 jh->b_modified = 1;
1568 handle->h_total_credits--;
1569 }
1570
1571 /*
1572 * fastpath, to avoid expensive locking. If this buffer is already
1573 * on the running transaction's metadata list there is nothing to do.
1574 * Nobody can take it off again because there is a handle open.
1575 * I _think_ we're OK here with SMP barriers - a mistaken decision will
1576 * result in this test being false, so we go in and take the locks.
1577 */
1578 if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) {
1579 JBUFFER_TRACE(jh, "fastpath");
1580 if (unlikely(jh->b_transaction !=
1581 journal->j_running_transaction)) {
1582 printk(KERN_ERR "JBD2: %s: "
1583 "jh->b_transaction (%llu, %p, %u) != "
1584 "journal->j_running_transaction (%p, %u)\n",
1585 journal->j_devname,
1586 (unsigned long long) bh->b_blocknr,
1587 jh->b_transaction,
1588 jh->b_transaction ? jh->b_transaction->t_tid : 0,
1589 journal->j_running_transaction,
1590 journal->j_running_transaction ?
1591 journal->j_running_transaction->t_tid : 0);
1592 ret = -EINVAL;
1593 }
1594 goto out_unlock_bh;
1595 }
1596
1597 set_buffer_jbddirty(bh);
1598
1599 /*
1600 * Metadata already on the current transaction list doesn't
1601 * need to be filed. Metadata on another transaction's list must
1602 * be committing, and will be refiled once the commit completes:
1603 * leave it alone for now.
1604 */
1605 if (jh->b_transaction != transaction) {
1606 JBUFFER_TRACE(jh, "already on other transaction");
1607 if (unlikely(((jh->b_transaction !=
1608 journal->j_committing_transaction)) ||
1609 (jh->b_next_transaction != transaction))) {
1610 printk(KERN_ERR "jbd2_journal_dirty_metadata: %s: "
1611 "bad jh for block %llu: "
1612 "transaction (%p, %u), "
1613 "jh->b_transaction (%p, %u), "
1614 "jh->b_next_transaction (%p, %u), jlist %u\n",
1615 journal->j_devname,
1616 (unsigned long long) bh->b_blocknr,
1617 transaction, transaction->t_tid,
1618 jh->b_transaction,
1619 jh->b_transaction ?
1620 jh->b_transaction->t_tid : 0,
1621 jh->b_next_transaction,
1622 jh->b_next_transaction ?
1623 jh->b_next_transaction->t_tid : 0,
1624 jh->b_jlist);
1625 WARN_ON(1);
1626 ret = -EINVAL;
1627 }
1628 /* And this case is illegal: we can't reuse another
1629 * transaction's data buffer, ever. */
1630 goto out_unlock_bh;
1631 }
1632
1633 /* That test should have eliminated the following case: */
1634 J_ASSERT_JH(jh, jh->b_frozen_data == NULL);
1635
1636 JBUFFER_TRACE(jh, "file as BJ_Metadata");
1637 spin_lock(&journal->j_list_lock);
1638 __jbd2_journal_file_buffer(jh, transaction, BJ_Metadata);
1639 spin_unlock(&journal->j_list_lock);
1640 out_unlock_bh:
1641 spin_unlock(&jh->b_state_lock);
1642 out:
1643 JBUFFER_TRACE(jh, "exit");
1644 return ret;
1645 }
1646
1647 /**
1648 * jbd2_journal_forget() - bforget() for potentially-journaled buffers.
1649 * @handle: transaction handle
1650 * @bh: bh to 'forget'
1651 *
1652 * We can only do the bforget if there are no commits pending against the
1653 * buffer. If the buffer is dirty in the current running transaction we
1654 * can safely unlink it.
1655 *
1656 * bh may not be a journalled buffer at all - it may be a non-JBD
1657 * buffer which came off the hashtable. Check for this.
1658 *
1659 * Decrements bh->b_count by one.
1660 *
1661 * Allow this call even if the handle has aborted --- it may be part of
1662 * the caller's cleanup after an abort.
1663 */
jbd2_journal_forget(handle_t * handle,struct buffer_head * bh)1664 int jbd2_journal_forget(handle_t *handle, struct buffer_head *bh)
1665 {
1666 transaction_t *transaction = handle->h_transaction;
1667 journal_t *journal;
1668 struct journal_head *jh;
1669 int drop_reserve = 0;
1670 int err = 0;
1671 int was_modified = 0;
1672 int wait_for_writeback = 0;
1673
1674 if (is_handle_aborted(handle))
1675 return -EROFS;
1676 journal = transaction->t_journal;
1677
1678 BUFFER_TRACE(bh, "entry");
1679
1680 jh = jbd2_journal_grab_journal_head(bh);
1681 if (!jh) {
1682 __bforget(bh);
1683 return 0;
1684 }
1685
1686 spin_lock(&jh->b_state_lock);
1687
1688 /* Critical error: attempting to delete a bitmap buffer, maybe?
1689 * Don't do any jbd operations, and return an error. */
1690 if (!J_EXPECT_JH(jh, !jh->b_committed_data,
1691 "inconsistent data on disk")) {
1692 err = -EIO;
1693 goto drop;
1694 }
1695
1696 /* keep track of whether or not this transaction modified us */
1697 was_modified = jh->b_modified;
1698
1699 /*
1700 * The buffer's going from the transaction, we must drop
1701 * all references -bzzz
1702 */
1703 jh->b_modified = 0;
1704
1705 if (jh->b_transaction == transaction) {
1706 J_ASSERT_JH(jh, !jh->b_frozen_data);
1707
1708 /* If we are forgetting a buffer which is already part
1709 * of this transaction, then we can just drop it from
1710 * the transaction immediately. */
1711 clear_buffer_dirty(bh);
1712 clear_buffer_jbddirty(bh);
1713
1714 JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
1715
1716 /*
1717 * we only want to drop a reference if this transaction
1718 * modified the buffer
1719 */
1720 if (was_modified)
1721 drop_reserve = 1;
1722
1723 /*
1724 * We are no longer going to journal this buffer.
1725 * However, the commit of this transaction is still
1726 * important to the buffer: the delete that we are now
1727 * processing might obsolete an old log entry, so by
1728 * committing, we can satisfy the buffer's checkpoint.
1729 *
1730 * So, if we have a checkpoint on the buffer, we should
1731 * now refile the buffer on our BJ_Forget list so that
1732 * we know to remove the checkpoint after we commit.
1733 */
1734
1735 spin_lock(&journal->j_list_lock);
1736 if (jh->b_cp_transaction) {
1737 __jbd2_journal_temp_unlink_buffer(jh);
1738 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
1739 } else {
1740 __jbd2_journal_unfile_buffer(jh);
1741 jbd2_journal_put_journal_head(jh);
1742 }
1743 spin_unlock(&journal->j_list_lock);
1744 } else if (jh->b_transaction) {
1745 J_ASSERT_JH(jh, (jh->b_transaction ==
1746 journal->j_committing_transaction));
1747 /* However, if the buffer is still owned by a prior
1748 * (committing) transaction, we can't drop it yet... */
1749 JBUFFER_TRACE(jh, "belongs to older transaction");
1750 /* ... but we CAN drop it from the new transaction through
1751 * marking the buffer as freed and set j_next_transaction to
1752 * the new transaction, so that not only the commit code
1753 * knows it should clear dirty bits when it is done with the
1754 * buffer, but also the buffer can be checkpointed only
1755 * after the new transaction commits. */
1756
1757 set_buffer_freed(bh);
1758
1759 if (!jh->b_next_transaction) {
1760 spin_lock(&journal->j_list_lock);
1761 jh->b_next_transaction = transaction;
1762 spin_unlock(&journal->j_list_lock);
1763 } else {
1764 J_ASSERT(jh->b_next_transaction == transaction);
1765
1766 /*
1767 * only drop a reference if this transaction modified
1768 * the buffer
1769 */
1770 if (was_modified)
1771 drop_reserve = 1;
1772 }
1773 } else {
1774 /*
1775 * Finally, if the buffer is not belongs to any
1776 * transaction, we can just drop it now if it has no
1777 * checkpoint.
1778 */
1779 spin_lock(&journal->j_list_lock);
1780 if (!jh->b_cp_transaction) {
1781 JBUFFER_TRACE(jh, "belongs to none transaction");
1782 spin_unlock(&journal->j_list_lock);
1783 goto drop;
1784 }
1785
1786 /*
1787 * Otherwise, if the buffer has been written to disk,
1788 * it is safe to remove the checkpoint and drop it.
1789 */
1790 if (jbd2_journal_try_remove_checkpoint(jh) >= 0) {
1791 spin_unlock(&journal->j_list_lock);
1792 goto drop;
1793 }
1794
1795 /*
1796 * The buffer has not yet been written to disk. We should
1797 * either clear the buffer or ensure that the ongoing I/O
1798 * is completed, and attach this buffer to current
1799 * transaction so that the buffer can be checkpointed only
1800 * after the current transaction commits.
1801 */
1802 clear_buffer_dirty(bh);
1803 wait_for_writeback = 1;
1804 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
1805 spin_unlock(&journal->j_list_lock);
1806 }
1807 drop:
1808 __brelse(bh);
1809 spin_unlock(&jh->b_state_lock);
1810 if (wait_for_writeback)
1811 wait_on_buffer(bh);
1812 jbd2_journal_put_journal_head(jh);
1813 if (drop_reserve) {
1814 /* no need to reserve log space for this block -bzzz */
1815 handle->h_total_credits++;
1816 }
1817 return err;
1818 }
1819
1820 /**
1821 * jbd2_journal_stop() - complete a transaction
1822 * @handle: transaction to complete.
1823 *
1824 * All done for a particular handle.
1825 *
1826 * There is not much action needed here. We just return any remaining
1827 * buffer credits to the transaction and remove the handle. The only
1828 * complication is that we need to start a commit operation if the
1829 * filesystem is marked for synchronous update.
1830 *
1831 * jbd2_journal_stop itself will not usually return an error, but it may
1832 * do so in unusual circumstances. In particular, expect it to
1833 * return -EIO if a jbd2_journal_abort has been executed since the
1834 * transaction began.
1835 */
jbd2_journal_stop(handle_t * handle)1836 int jbd2_journal_stop(handle_t *handle)
1837 {
1838 transaction_t *transaction = handle->h_transaction;
1839 journal_t *journal;
1840 int err = 0, wait_for_commit = 0;
1841 tid_t tid;
1842 pid_t pid;
1843
1844 if (--handle->h_ref > 0) {
1845 jbd2_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
1846 handle->h_ref);
1847 if (is_handle_aborted(handle))
1848 return -EIO;
1849 return 0;
1850 }
1851 if (!transaction) {
1852 /*
1853 * Handle is already detached from the transaction so there is
1854 * nothing to do other than free the handle.
1855 */
1856 memalloc_nofs_restore(handle->saved_alloc_context);
1857 goto free_and_exit;
1858 }
1859 journal = transaction->t_journal;
1860 tid = transaction->t_tid;
1861
1862 if (is_handle_aborted(handle))
1863 err = -EIO;
1864
1865 jbd2_debug(4, "Handle %p going down\n", handle);
1866 trace_jbd2_handle_stats(journal->j_fs_dev->bd_dev,
1867 tid, handle->h_type, handle->h_line_no,
1868 jiffies - handle->h_start_jiffies,
1869 handle->h_sync, handle->h_requested_credits,
1870 (handle->h_requested_credits -
1871 handle->h_total_credits));
1872
1873 /*
1874 * Implement synchronous transaction batching. If the handle
1875 * was synchronous, don't force a commit immediately. Let's
1876 * yield and let another thread piggyback onto this
1877 * transaction. Keep doing that while new threads continue to
1878 * arrive. It doesn't cost much - we're about to run a commit
1879 * and sleep on IO anyway. Speeds up many-threaded, many-dir
1880 * operations by 30x or more...
1881 *
1882 * We try and optimize the sleep time against what the
1883 * underlying disk can do, instead of having a static sleep
1884 * time. This is useful for the case where our storage is so
1885 * fast that it is more optimal to go ahead and force a flush
1886 * and wait for the transaction to be committed than it is to
1887 * wait for an arbitrary amount of time for new writers to
1888 * join the transaction. We achieve this by measuring how
1889 * long it takes to commit a transaction, and compare it with
1890 * how long this transaction has been running, and if run time
1891 * < commit time then we sleep for the delta and commit. This
1892 * greatly helps super fast disks that would see slowdowns as
1893 * more threads started doing fsyncs.
1894 *
1895 * But don't do this if this process was the most recent one
1896 * to perform a synchronous write. We do this to detect the
1897 * case where a single process is doing a stream of sync
1898 * writes. No point in waiting for joiners in that case.
1899 *
1900 * Setting max_batch_time to 0 disables this completely.
1901 */
1902 pid = current->pid;
1903 if (handle->h_sync && journal->j_last_sync_writer != pid &&
1904 journal->j_max_batch_time) {
1905 u64 commit_time, trans_time;
1906
1907 journal->j_last_sync_writer = pid;
1908
1909 read_lock(&journal->j_state_lock);
1910 commit_time = journal->j_average_commit_time;
1911 read_unlock(&journal->j_state_lock);
1912
1913 trans_time = ktime_to_ns(ktime_sub(ktime_get(),
1914 transaction->t_start_time));
1915
1916 commit_time = max_t(u64, commit_time,
1917 1000*journal->j_min_batch_time);
1918 commit_time = min_t(u64, commit_time,
1919 1000*journal->j_max_batch_time);
1920
1921 if (trans_time < commit_time) {
1922 ktime_t expires = ktime_add_ns(ktime_get(),
1923 commit_time);
1924 set_current_state(TASK_UNINTERRUPTIBLE);
1925 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1926 }
1927 }
1928
1929 if (handle->h_sync)
1930 transaction->t_synchronous_commit = 1;
1931
1932 /*
1933 * If the handle is marked SYNC, we need to set another commit
1934 * going! We also want to force a commit if the transaction is too
1935 * old now.
1936 */
1937 if (handle->h_sync ||
1938 time_after_eq(jiffies, transaction->t_expires)) {
1939 /* Do this even for aborted journals: an abort still
1940 * completes the commit thread, it just doesn't write
1941 * anything to disk. */
1942
1943 jbd2_debug(2, "transaction too old, requesting commit for "
1944 "handle %p\n", handle);
1945 /* This is non-blocking */
1946 jbd2_log_start_commit(journal, tid);
1947
1948 /*
1949 * Special case: JBD2_SYNC synchronous updates require us
1950 * to wait for the commit to complete.
1951 */
1952 if (handle->h_sync && !(current->flags & PF_MEMALLOC))
1953 wait_for_commit = 1;
1954 }
1955
1956 /*
1957 * Once stop_this_handle() drops t_updates, the transaction could start
1958 * committing on us and eventually disappear. So we must not
1959 * dereference transaction pointer again after calling
1960 * stop_this_handle().
1961 */
1962 stop_this_handle(handle);
1963
1964 if (wait_for_commit)
1965 err = jbd2_log_wait_commit(journal, tid);
1966
1967 free_and_exit:
1968 if (handle->h_rsv_handle)
1969 jbd2_free_handle(handle->h_rsv_handle);
1970 jbd2_free_handle(handle);
1971 return err;
1972 }
1973
1974 /*
1975 *
1976 * List management code snippets: various functions for manipulating the
1977 * transaction buffer lists.
1978 *
1979 */
1980
1981 /*
1982 * Append a buffer to a transaction list, given the transaction's list head
1983 * pointer.
1984 *
1985 * j_list_lock is held.
1986 *
1987 * jh->b_state_lock is held.
1988 */
1989
1990 static inline void
__blist_add_buffer(struct journal_head ** list,struct journal_head * jh)1991 __blist_add_buffer(struct journal_head **list, struct journal_head *jh)
1992 {
1993 if (!*list) {
1994 jh->b_tnext = jh->b_tprev = jh;
1995 *list = jh;
1996 } else {
1997 /* Insert at the tail of the list to preserve order */
1998 struct journal_head *first = *list, *last = first->b_tprev;
1999 jh->b_tprev = last;
2000 jh->b_tnext = first;
2001 last->b_tnext = first->b_tprev = jh;
2002 }
2003 }
2004
2005 /*
2006 * Remove a buffer from a transaction list, given the transaction's list
2007 * head pointer.
2008 *
2009 * Called with j_list_lock held, and the journal may not be locked.
2010 *
2011 * jh->b_state_lock is held.
2012 */
2013
2014 static inline void
__blist_del_buffer(struct journal_head ** list,struct journal_head * jh)2015 __blist_del_buffer(struct journal_head **list, struct journal_head *jh)
2016 {
2017 if (*list == jh) {
2018 *list = jh->b_tnext;
2019 if (*list == jh)
2020 *list = NULL;
2021 }
2022 jh->b_tprev->b_tnext = jh->b_tnext;
2023 jh->b_tnext->b_tprev = jh->b_tprev;
2024 }
2025
2026 /*
2027 * Remove a buffer from the appropriate transaction list.
2028 *
2029 * Note that this function can *change* the value of
2030 * bh->b_transaction->t_buffers, t_forget, t_shadow_list, t_log_list or
2031 * t_reserved_list. If the caller is holding onto a copy of one of these
2032 * pointers, it could go bad. Generally the caller needs to re-read the
2033 * pointer from the transaction_t.
2034 *
2035 * Called under j_list_lock.
2036 */
__jbd2_journal_temp_unlink_buffer(struct journal_head * jh)2037 static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh)
2038 {
2039 struct journal_head **list = NULL;
2040 transaction_t *transaction;
2041 struct buffer_head *bh = jh2bh(jh);
2042
2043 lockdep_assert_held(&jh->b_state_lock);
2044 transaction = jh->b_transaction;
2045 if (transaction)
2046 assert_spin_locked(&transaction->t_journal->j_list_lock);
2047
2048 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
2049 if (jh->b_jlist != BJ_None)
2050 J_ASSERT_JH(jh, transaction != NULL);
2051
2052 switch (jh->b_jlist) {
2053 case BJ_None:
2054 return;
2055 case BJ_Metadata:
2056 transaction->t_nr_buffers--;
2057 J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
2058 list = &transaction->t_buffers;
2059 break;
2060 case BJ_Forget:
2061 list = &transaction->t_forget;
2062 break;
2063 case BJ_Shadow:
2064 list = &transaction->t_shadow_list;
2065 break;
2066 case BJ_Reserved:
2067 list = &transaction->t_reserved_list;
2068 break;
2069 }
2070
2071 __blist_del_buffer(list, jh);
2072 jh->b_jlist = BJ_None;
2073 if (transaction && is_journal_aborted(transaction->t_journal))
2074 clear_buffer_jbddirty(bh);
2075 else if (test_clear_buffer_jbddirty(bh))
2076 mark_buffer_dirty(bh); /* Expose it to the VM */
2077 }
2078
2079 /*
2080 * Remove buffer from all transactions. The caller is responsible for dropping
2081 * the jh reference that belonged to the transaction.
2082 *
2083 * Called with bh_state lock and j_list_lock
2084 */
__jbd2_journal_unfile_buffer(struct journal_head * jh)2085 static void __jbd2_journal_unfile_buffer(struct journal_head *jh)
2086 {
2087 J_ASSERT_JH(jh, jh->b_transaction != NULL);
2088 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
2089
2090 __jbd2_journal_temp_unlink_buffer(jh);
2091 jh->b_transaction = NULL;
2092 }
2093
2094 /**
2095 * jbd2_journal_try_to_free_buffers() - try to free page buffers.
2096 * @journal: journal for operation
2097 * @folio: Folio to detach data from.
2098 *
2099 * For all the buffers on this page,
2100 * if they are fully written out ordered data, move them onto BUF_CLEAN
2101 * so try_to_free_buffers() can reap them.
2102 *
2103 * This function returns non-zero if we wish try_to_free_buffers()
2104 * to be called. We do this if the page is releasable by try_to_free_buffers().
2105 * We also do it if the page has locked or dirty buffers and the caller wants
2106 * us to perform sync or async writeout.
2107 *
2108 * This complicates JBD locking somewhat. We aren't protected by the
2109 * BKL here. We wish to remove the buffer from its committing or
2110 * running transaction's ->t_datalist via __jbd2_journal_unfile_buffer.
2111 *
2112 * This may *change* the value of transaction_t->t_datalist, so anyone
2113 * who looks at t_datalist needs to lock against this function.
2114 *
2115 * Even worse, someone may be doing a jbd2_journal_dirty_data on this
2116 * buffer. So we need to lock against that. jbd2_journal_dirty_data()
2117 * will come out of the lock with the buffer dirty, which makes it
2118 * ineligible for release here.
2119 *
2120 * Who else is affected by this? hmm... Really the only contender
2121 * is do_get_write_access() - it could be looking at the buffer while
2122 * journal_try_to_free_buffer() is changing its state. But that
2123 * cannot happen because we never reallocate freed data as metadata
2124 * while the data is part of a transaction. Yes?
2125 *
2126 * Return false on failure, true on success
2127 */
jbd2_journal_try_to_free_buffers(journal_t * journal,struct folio * folio)2128 bool jbd2_journal_try_to_free_buffers(journal_t *journal, struct folio *folio)
2129 {
2130 struct buffer_head *head;
2131 struct buffer_head *bh;
2132 bool ret = false;
2133
2134 J_ASSERT(folio_test_locked(folio));
2135
2136 head = folio_buffers(folio);
2137 bh = head;
2138 do {
2139 struct journal_head *jh;
2140
2141 /*
2142 * We take our own ref against the journal_head here to avoid
2143 * having to add tons of locking around each instance of
2144 * jbd2_journal_put_journal_head().
2145 */
2146 jh = jbd2_journal_grab_journal_head(bh);
2147 if (!jh)
2148 continue;
2149
2150 spin_lock(&jh->b_state_lock);
2151 if (!jh->b_transaction && !jh->b_next_transaction) {
2152 spin_lock(&journal->j_list_lock);
2153 /* Remove written-back checkpointed metadata buffer */
2154 if (jh->b_cp_transaction != NULL)
2155 jbd2_journal_try_remove_checkpoint(jh);
2156 spin_unlock(&journal->j_list_lock);
2157 }
2158 spin_unlock(&jh->b_state_lock);
2159 jbd2_journal_put_journal_head(jh);
2160 if (buffer_jbd(bh))
2161 goto busy;
2162 } while ((bh = bh->b_this_page) != head);
2163
2164 ret = try_to_free_buffers(folio);
2165 busy:
2166 return ret;
2167 }
2168
2169 /*
2170 * This buffer is no longer needed. If it is on an older transaction's
2171 * checkpoint list we need to record it on this transaction's forget list
2172 * to pin this buffer (and hence its checkpointing transaction) down until
2173 * this transaction commits. If the buffer isn't on a checkpoint list, we
2174 * release it.
2175 * Returns non-zero if JBD no longer has an interest in the buffer.
2176 *
2177 * Called under j_list_lock.
2178 *
2179 * Called under jh->b_state_lock.
2180 */
__dispose_buffer(struct journal_head * jh,transaction_t * transaction)2181 static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
2182 {
2183 int may_free = 1;
2184 struct buffer_head *bh = jh2bh(jh);
2185
2186 if (jh->b_cp_transaction) {
2187 JBUFFER_TRACE(jh, "on running+cp transaction");
2188 __jbd2_journal_temp_unlink_buffer(jh);
2189 /*
2190 * We don't want to write the buffer anymore, clear the
2191 * bit so that we don't confuse checks in
2192 * __jbd2_journal_file_buffer
2193 */
2194 clear_buffer_dirty(bh);
2195 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
2196 may_free = 0;
2197 } else {
2198 JBUFFER_TRACE(jh, "on running transaction");
2199 __jbd2_journal_unfile_buffer(jh);
2200 jbd2_journal_put_journal_head(jh);
2201 }
2202 return may_free;
2203 }
2204
2205 /*
2206 * jbd2_journal_invalidate_folio
2207 *
2208 * This code is tricky. It has a number of cases to deal with.
2209 *
2210 * There are two invariants which this code relies on:
2211 *
2212 * i_size must be updated on disk before we start calling invalidate_folio
2213 * on the data.
2214 *
2215 * This is done in ext3 by defining an ext3_setattr method which
2216 * updates i_size before truncate gets going. By maintaining this
2217 * invariant, we can be sure that it is safe to throw away any buffers
2218 * attached to the current transaction: once the transaction commits,
2219 * we know that the data will not be needed.
2220 *
2221 * Note however that we can *not* throw away data belonging to the
2222 * previous, committing transaction!
2223 *
2224 * Any disk blocks which *are* part of the previous, committing
2225 * transaction (and which therefore cannot be discarded immediately) are
2226 * not going to be reused in the new running transaction
2227 *
2228 * The bitmap committed_data images guarantee this: any block which is
2229 * allocated in one transaction and removed in the next will be marked
2230 * as in-use in the committed_data bitmap, so cannot be reused until
2231 * the next transaction to delete the block commits. This means that
2232 * leaving committing buffers dirty is quite safe: the disk blocks
2233 * cannot be reallocated to a different file and so buffer aliasing is
2234 * not possible.
2235 *
2236 *
2237 * The above applies mainly to ordered data mode. In writeback mode we
2238 * don't make guarantees about the order in which data hits disk --- in
2239 * particular we don't guarantee that new dirty data is flushed before
2240 * transaction commit --- so it is always safe just to discard data
2241 * immediately in that mode. --sct
2242 */
2243
2244 /*
2245 * The journal_unmap_buffer helper function returns zero if the buffer
2246 * concerned remains pinned as an anonymous buffer belonging to an older
2247 * transaction.
2248 *
2249 * We're outside-transaction here. Either or both of j_running_transaction
2250 * and j_committing_transaction may be NULL.
2251 */
journal_unmap_buffer(journal_t * journal,struct buffer_head * bh,int partial_page)2252 static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh,
2253 int partial_page)
2254 {
2255 transaction_t *transaction;
2256 struct journal_head *jh;
2257 int may_free = 1;
2258
2259 BUFFER_TRACE(bh, "entry");
2260
2261 /*
2262 * It is safe to proceed here without the j_list_lock because the
2263 * buffers cannot be stolen by try_to_free_buffers as long as we are
2264 * holding the page lock. --sct
2265 */
2266
2267 jh = jbd2_journal_grab_journal_head(bh);
2268 if (!jh)
2269 goto zap_buffer_unlocked;
2270
2271 /* OK, we have data buffer in journaled mode */
2272 write_lock(&journal->j_state_lock);
2273 spin_lock(&jh->b_state_lock);
2274 spin_lock(&journal->j_list_lock);
2275
2276 /*
2277 * We cannot remove the buffer from checkpoint lists until the
2278 * transaction adding inode to orphan list (let's call it T)
2279 * is committed. Otherwise if the transaction changing the
2280 * buffer would be cleaned from the journal before T is
2281 * committed, a crash will cause that the correct contents of
2282 * the buffer will be lost. On the other hand we have to
2283 * clear the buffer dirty bit at latest at the moment when the
2284 * transaction marking the buffer as freed in the filesystem
2285 * structures is committed because from that moment on the
2286 * block can be reallocated and used by a different page.
2287 * Since the block hasn't been freed yet but the inode has
2288 * already been added to orphan list, it is safe for us to add
2289 * the buffer to BJ_Forget list of the newest transaction.
2290 *
2291 * Also we have to clear buffer_mapped flag of a truncated buffer
2292 * because the buffer_head may be attached to the page straddling
2293 * i_size (can happen only when blocksize < pagesize) and thus the
2294 * buffer_head can be reused when the file is extended again. So we end
2295 * up keeping around invalidated buffers attached to transactions'
2296 * BJ_Forget list just to stop checkpointing code from cleaning up
2297 * the transaction this buffer was modified in.
2298 */
2299 transaction = jh->b_transaction;
2300 if (transaction == NULL) {
2301 /* First case: not on any transaction. If it
2302 * has no checkpoint link, then we can zap it:
2303 * it's a writeback-mode buffer so we don't care
2304 * if it hits disk safely. */
2305 if (!jh->b_cp_transaction) {
2306 JBUFFER_TRACE(jh, "not on any transaction: zap");
2307 goto zap_buffer;
2308 }
2309
2310 if (!buffer_dirty(bh)) {
2311 /* bdflush has written it. We can drop it now */
2312 __jbd2_journal_remove_checkpoint(jh);
2313 goto zap_buffer;
2314 }
2315
2316 /* OK, it must be in the journal but still not
2317 * written fully to disk: it's metadata or
2318 * journaled data... */
2319
2320 if (journal->j_running_transaction) {
2321 /* ... and once the current transaction has
2322 * committed, the buffer won't be needed any
2323 * longer. */
2324 JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
2325 may_free = __dispose_buffer(jh,
2326 journal->j_running_transaction);
2327 goto zap_buffer;
2328 } else {
2329 /* There is no currently-running transaction. So the
2330 * orphan record which we wrote for this file must have
2331 * passed into commit. We must attach this buffer to
2332 * the committing transaction, if it exists. */
2333 if (journal->j_committing_transaction) {
2334 JBUFFER_TRACE(jh, "give to committing trans");
2335 may_free = __dispose_buffer(jh,
2336 journal->j_committing_transaction);
2337 goto zap_buffer;
2338 } else {
2339 /* The orphan record's transaction has
2340 * committed. We can cleanse this buffer */
2341 clear_buffer_jbddirty(bh);
2342 __jbd2_journal_remove_checkpoint(jh);
2343 goto zap_buffer;
2344 }
2345 }
2346 } else if (transaction == journal->j_committing_transaction) {
2347 JBUFFER_TRACE(jh, "on committing transaction");
2348 /*
2349 * The buffer is committing, we simply cannot touch
2350 * it. If the page is straddling i_size we have to wait
2351 * for commit and try again.
2352 */
2353 if (partial_page) {
2354 spin_unlock(&journal->j_list_lock);
2355 spin_unlock(&jh->b_state_lock);
2356 write_unlock(&journal->j_state_lock);
2357 jbd2_journal_put_journal_head(jh);
2358 /* Already zapped buffer? Nothing to do... */
2359 if (!bh->b_bdev)
2360 return 0;
2361 return -EBUSY;
2362 }
2363 /*
2364 * OK, buffer won't be reachable after truncate. We just clear
2365 * b_modified to not confuse transaction credit accounting, and
2366 * set j_next_transaction to the running transaction (if there
2367 * is one) and mark buffer as freed so that commit code knows
2368 * it should clear dirty bits when it is done with the buffer.
2369 */
2370 set_buffer_freed(bh);
2371 if (journal->j_running_transaction && buffer_jbddirty(bh))
2372 jh->b_next_transaction = journal->j_running_transaction;
2373 jh->b_modified = 0;
2374 spin_unlock(&journal->j_list_lock);
2375 spin_unlock(&jh->b_state_lock);
2376 write_unlock(&journal->j_state_lock);
2377 jbd2_journal_put_journal_head(jh);
2378 return 0;
2379 } else {
2380 /* Good, the buffer belongs to the running transaction.
2381 * We are writing our own transaction's data, not any
2382 * previous one's, so it is safe to throw it away
2383 * (remember that we expect the filesystem to have set
2384 * i_size already for this truncate so recovery will not
2385 * expose the disk blocks we are discarding here.) */
2386 J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
2387 JBUFFER_TRACE(jh, "on running transaction");
2388 may_free = __dispose_buffer(jh, transaction);
2389 }
2390
2391 zap_buffer:
2392 /*
2393 * This is tricky. Although the buffer is truncated, it may be reused
2394 * if blocksize < pagesize and it is attached to the page straddling
2395 * EOF. Since the buffer might have been added to BJ_Forget list of the
2396 * running transaction, journal_get_write_access() won't clear
2397 * b_modified and credit accounting gets confused. So clear b_modified
2398 * here.
2399 */
2400 jh->b_modified = 0;
2401 spin_unlock(&journal->j_list_lock);
2402 spin_unlock(&jh->b_state_lock);
2403 write_unlock(&journal->j_state_lock);
2404 jbd2_journal_put_journal_head(jh);
2405 zap_buffer_unlocked:
2406 clear_buffer_dirty(bh);
2407 J_ASSERT_BH(bh, !buffer_jbddirty(bh));
2408 clear_buffer_mapped(bh);
2409 clear_buffer_req(bh);
2410 clear_buffer_new(bh);
2411 clear_buffer_delay(bh);
2412 clear_buffer_unwritten(bh);
2413 bh->b_bdev = NULL;
2414 return may_free;
2415 }
2416
2417 /**
2418 * jbd2_journal_invalidate_folio()
2419 * @journal: journal to use for flush...
2420 * @folio: folio to flush
2421 * @offset: start of the range to invalidate
2422 * @length: length of the range to invalidate
2423 *
2424 * Reap page buffers containing data after in the specified range in page.
2425 * Can return -EBUSY if buffers are part of the committing transaction and
2426 * the page is straddling i_size. Caller then has to wait for current commit
2427 * and try again.
2428 */
jbd2_journal_invalidate_folio(journal_t * journal,struct folio * folio,size_t offset,size_t length)2429 int jbd2_journal_invalidate_folio(journal_t *journal, struct folio *folio,
2430 size_t offset, size_t length)
2431 {
2432 struct buffer_head *head, *bh, *next;
2433 unsigned int stop = offset + length;
2434 unsigned int curr_off = 0;
2435 int partial_page = (offset || length < folio_size(folio));
2436 int may_free = 1;
2437 int ret = 0;
2438
2439 if (!folio_test_locked(folio))
2440 BUG();
2441 head = folio_buffers(folio);
2442 if (!head)
2443 return 0;
2444
2445 BUG_ON(stop > folio_size(folio) || stop < length);
2446
2447 /* We will potentially be playing with lists other than just the
2448 * data lists (especially for journaled data mode), so be
2449 * cautious in our locking. */
2450
2451 bh = head;
2452 do {
2453 unsigned int next_off = curr_off + bh->b_size;
2454 next = bh->b_this_page;
2455
2456 if (next_off > stop)
2457 return 0;
2458
2459 if (offset <= curr_off) {
2460 /* This block is wholly outside the truncation point */
2461 lock_buffer(bh);
2462 ret = journal_unmap_buffer(journal, bh, partial_page);
2463 unlock_buffer(bh);
2464 if (ret < 0)
2465 return ret;
2466 may_free &= ret;
2467 }
2468 curr_off = next_off;
2469 bh = next;
2470
2471 } while (bh != head);
2472
2473 if (!partial_page) {
2474 if (may_free && try_to_free_buffers(folio))
2475 J_ASSERT(!folio_buffers(folio));
2476 }
2477 return 0;
2478 }
2479
2480 /*
2481 * File a buffer on the given transaction list.
2482 */
__jbd2_journal_file_buffer(struct journal_head * jh,transaction_t * transaction,int jlist)2483 void __jbd2_journal_file_buffer(struct journal_head *jh,
2484 transaction_t *transaction, int jlist)
2485 {
2486 struct journal_head **list = NULL;
2487 int was_dirty = 0;
2488 struct buffer_head *bh = jh2bh(jh);
2489
2490 lockdep_assert_held(&jh->b_state_lock);
2491 assert_spin_locked(&transaction->t_journal->j_list_lock);
2492
2493 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
2494 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
2495 jh->b_transaction == NULL);
2496
2497 if (jh->b_transaction && jh->b_jlist == jlist)
2498 return;
2499
2500 if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
2501 jlist == BJ_Shadow || jlist == BJ_Forget) {
2502 /*
2503 * For metadata buffers, we track dirty bit in buffer_jbddirty
2504 * instead of buffer_dirty. We should not see a dirty bit set
2505 * here because we clear it in do_get_write_access but e.g.
2506 * tune2fs can modify the sb and set the dirty bit at any time
2507 * so we try to gracefully handle that.
2508 */
2509 if (buffer_dirty(bh))
2510 warn_dirty_buffer(bh);
2511 if (test_clear_buffer_dirty(bh) ||
2512 test_clear_buffer_jbddirty(bh))
2513 was_dirty = 1;
2514 }
2515
2516 if (jh->b_transaction)
2517 __jbd2_journal_temp_unlink_buffer(jh);
2518 else
2519 jbd2_journal_grab_journal_head(bh);
2520 jh->b_transaction = transaction;
2521
2522 switch (jlist) {
2523 case BJ_None:
2524 J_ASSERT_JH(jh, !jh->b_committed_data);
2525 J_ASSERT_JH(jh, !jh->b_frozen_data);
2526 return;
2527 case BJ_Metadata:
2528 transaction->t_nr_buffers++;
2529 list = &transaction->t_buffers;
2530 break;
2531 case BJ_Forget:
2532 list = &transaction->t_forget;
2533 break;
2534 case BJ_Shadow:
2535 list = &transaction->t_shadow_list;
2536 break;
2537 case BJ_Reserved:
2538 list = &transaction->t_reserved_list;
2539 break;
2540 }
2541
2542 __blist_add_buffer(list, jh);
2543 jh->b_jlist = jlist;
2544
2545 if (was_dirty)
2546 set_buffer_jbddirty(bh);
2547 }
2548
jbd2_journal_file_buffer(struct journal_head * jh,transaction_t * transaction,int jlist)2549 void jbd2_journal_file_buffer(struct journal_head *jh,
2550 transaction_t *transaction, int jlist)
2551 {
2552 spin_lock(&jh->b_state_lock);
2553 spin_lock(&transaction->t_journal->j_list_lock);
2554 __jbd2_journal_file_buffer(jh, transaction, jlist);
2555 spin_unlock(&transaction->t_journal->j_list_lock);
2556 spin_unlock(&jh->b_state_lock);
2557 }
2558
2559 /*
2560 * Remove a buffer from its current buffer list in preparation for
2561 * dropping it from its current transaction entirely. If the buffer has
2562 * already started to be used by a subsequent transaction, refile the
2563 * buffer on that transaction's metadata list.
2564 *
2565 * Called under j_list_lock
2566 * Called under jh->b_state_lock
2567 *
2568 * When this function returns true, there's no next transaction to refile to
2569 * and the caller has to drop jh reference through
2570 * jbd2_journal_put_journal_head().
2571 */
__jbd2_journal_refile_buffer(struct journal_head * jh)2572 bool __jbd2_journal_refile_buffer(struct journal_head *jh)
2573 {
2574 int was_dirty, jlist;
2575 struct buffer_head *bh = jh2bh(jh);
2576
2577 lockdep_assert_held(&jh->b_state_lock);
2578 if (jh->b_transaction)
2579 assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
2580
2581 /* If the buffer is now unused, just drop it. */
2582 if (jh->b_next_transaction == NULL) {
2583 __jbd2_journal_unfile_buffer(jh);
2584 return true;
2585 }
2586
2587 /*
2588 * It has been modified by a later transaction: add it to the new
2589 * transaction's metadata list.
2590 */
2591
2592 was_dirty = test_clear_buffer_jbddirty(bh);
2593 __jbd2_journal_temp_unlink_buffer(jh);
2594
2595 /*
2596 * b_transaction must be set, otherwise the new b_transaction won't
2597 * be holding jh reference
2598 */
2599 J_ASSERT_JH(jh, jh->b_transaction != NULL);
2600
2601 /*
2602 * We set b_transaction here because b_next_transaction will inherit
2603 * our jh reference and thus __jbd2_journal_file_buffer() must not
2604 * take a new one.
2605 */
2606 WRITE_ONCE(jh->b_transaction, jh->b_next_transaction);
2607 WRITE_ONCE(jh->b_next_transaction, NULL);
2608 if (buffer_freed(bh))
2609 jlist = BJ_Forget;
2610 else if (jh->b_modified)
2611 jlist = BJ_Metadata;
2612 else
2613 jlist = BJ_Reserved;
2614 __jbd2_journal_file_buffer(jh, jh->b_transaction, jlist);
2615 J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
2616
2617 if (was_dirty)
2618 set_buffer_jbddirty(bh);
2619 return false;
2620 }
2621
2622 /*
2623 * __jbd2_journal_refile_buffer() with necessary locking added. We take our
2624 * bh reference so that we can safely unlock bh.
2625 *
2626 * The jh and bh may be freed by this call.
2627 */
jbd2_journal_refile_buffer(journal_t * journal,struct journal_head * jh)2628 void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh)
2629 {
2630 bool drop;
2631
2632 spin_lock(&jh->b_state_lock);
2633 spin_lock(&journal->j_list_lock);
2634 drop = __jbd2_journal_refile_buffer(jh);
2635 spin_unlock(&jh->b_state_lock);
2636 spin_unlock(&journal->j_list_lock);
2637 if (drop)
2638 jbd2_journal_put_journal_head(jh);
2639 }
2640
2641 /*
2642 * File inode in the inode list of the handle's transaction
2643 */
jbd2_journal_file_inode(handle_t * handle,struct jbd2_inode * jinode,unsigned long flags,loff_t start_byte,loff_t end_byte)2644 static int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode,
2645 unsigned long flags, loff_t start_byte, loff_t end_byte)
2646 {
2647 transaction_t *transaction = handle->h_transaction;
2648 journal_t *journal;
2649
2650 if (is_handle_aborted(handle))
2651 return -EROFS;
2652 journal = transaction->t_journal;
2653
2654 jbd2_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino,
2655 transaction->t_tid);
2656
2657 spin_lock(&journal->j_list_lock);
2658 jinode->i_flags |= flags;
2659
2660 if (jinode->i_dirty_end) {
2661 jinode->i_dirty_start = min(jinode->i_dirty_start, start_byte);
2662 jinode->i_dirty_end = max(jinode->i_dirty_end, end_byte);
2663 } else {
2664 jinode->i_dirty_start = start_byte;
2665 jinode->i_dirty_end = end_byte;
2666 }
2667
2668 /* Is inode already attached where we need it? */
2669 if (jinode->i_transaction == transaction ||
2670 jinode->i_next_transaction == transaction)
2671 goto done;
2672
2673 /*
2674 * We only ever set this variable to 1 so the test is safe. Since
2675 * t_need_data_flush is likely to be set, we do the test to save some
2676 * cacheline bouncing
2677 */
2678 if (!transaction->t_need_data_flush)
2679 transaction->t_need_data_flush = 1;
2680 /* On some different transaction's list - should be
2681 * the committing one */
2682 if (jinode->i_transaction) {
2683 J_ASSERT(jinode->i_next_transaction == NULL);
2684 J_ASSERT(jinode->i_transaction ==
2685 journal->j_committing_transaction);
2686 jinode->i_next_transaction = transaction;
2687 goto done;
2688 }
2689 /* Not on any transaction list... */
2690 J_ASSERT(!jinode->i_next_transaction);
2691 jinode->i_transaction = transaction;
2692 list_add(&jinode->i_list, &transaction->t_inode_list);
2693 done:
2694 spin_unlock(&journal->j_list_lock);
2695
2696 return 0;
2697 }
2698
jbd2_journal_inode_ranged_write(handle_t * handle,struct jbd2_inode * jinode,loff_t start_byte,loff_t length)2699 int jbd2_journal_inode_ranged_write(handle_t *handle,
2700 struct jbd2_inode *jinode, loff_t start_byte, loff_t length)
2701 {
2702 return jbd2_journal_file_inode(handle, jinode,
2703 JI_WRITE_DATA | JI_WAIT_DATA, start_byte,
2704 start_byte + length - 1);
2705 }
2706
jbd2_journal_inode_ranged_wait(handle_t * handle,struct jbd2_inode * jinode,loff_t start_byte,loff_t length)2707 int jbd2_journal_inode_ranged_wait(handle_t *handle, struct jbd2_inode *jinode,
2708 loff_t start_byte, loff_t length)
2709 {
2710 return jbd2_journal_file_inode(handle, jinode, JI_WAIT_DATA,
2711 start_byte, start_byte + length - 1);
2712 }
2713
2714 /*
2715 * File truncate and transaction commit interact with each other in a
2716 * non-trivial way. If a transaction writing data block A is
2717 * committing, we cannot discard the data by truncate until we have
2718 * written them. Otherwise if we crashed after the transaction with
2719 * write has committed but before the transaction with truncate has
2720 * committed, we could see stale data in block A. This function is a
2721 * helper to solve this problem. It starts writeout of the truncated
2722 * part in case it is in the committing transaction.
2723 *
2724 * Filesystem code must call this function when inode is journaled in
2725 * ordered mode before truncation happens and after the inode has been
2726 * placed on orphan list with the new inode size. The second condition
2727 * avoids the race that someone writes new data and we start
2728 * committing the transaction after this function has been called but
2729 * before a transaction for truncate is started (and furthermore it
2730 * allows us to optimize the case where the addition to orphan list
2731 * happens in the same transaction as write --- we don't have to write
2732 * any data in such case).
2733 */
jbd2_journal_begin_ordered_truncate(journal_t * journal,struct jbd2_inode * jinode,loff_t new_size)2734 int jbd2_journal_begin_ordered_truncate(journal_t *journal,
2735 struct jbd2_inode *jinode,
2736 loff_t new_size)
2737 {
2738 transaction_t *inode_trans, *commit_trans;
2739 int ret = 0;
2740
2741 /* This is a quick check to avoid locking if not necessary */
2742 if (!jinode->i_transaction)
2743 goto out;
2744 /* Locks are here just to force reading of recent values, it is
2745 * enough that the transaction was not committing before we started
2746 * a transaction adding the inode to orphan list */
2747 read_lock(&journal->j_state_lock);
2748 commit_trans = journal->j_committing_transaction;
2749 read_unlock(&journal->j_state_lock);
2750 spin_lock(&journal->j_list_lock);
2751 inode_trans = jinode->i_transaction;
2752 spin_unlock(&journal->j_list_lock);
2753 if (inode_trans == commit_trans) {
2754 ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping,
2755 new_size, LLONG_MAX);
2756 if (ret)
2757 jbd2_journal_abort(journal, ret);
2758 }
2759 out:
2760 return ret;
2761 }
2762