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