1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
33static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh);
34static void __jbd2_journal_unfile_buffer(struct journal_head *jh);
35
36static struct kmem_cache *transaction_cache;
37int __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
52void jbd2_journal_destroy_transaction_cache(void)
53{
54 kmem_cache_destroy(transaction_cache);
55 transaction_cache = NULL;
56}
57
58void 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
67
68static int jbd2_descriptor_blocks_per_trans(journal_t *journal)
69{
70 int tag_space = journal->j_blocksize - sizeof(journal_header_t);
71 int tags_per_block;
72
73
74 tag_space -= 16;
75 if (jbd2_journal_has_csum_v2or3(journal))
76 tag_space -= sizeof(struct jbd2_journal_block_tail);
77
78 tags_per_block = (tag_space - 16) / journal_tag_bytes(journal);
79
80
81
82
83 return 1 + DIV_ROUND_UP(journal->j_max_transaction_buffers,
84 tags_per_block);
85}
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102static void jbd2_get_transaction(journal_t *journal,
103 transaction_t *transaction)
104{
105 transaction->t_journal = journal;
106 transaction->t_state = T_RUNNING;
107 transaction->t_start_time = ktime_get();
108 transaction->t_tid = journal->j_transaction_sequence++;
109 transaction->t_expires = jiffies + journal->j_commit_interval;
110 spin_lock_init(&transaction->t_handle_lock);
111 atomic_set(&transaction->t_updates, 0);
112 atomic_set(&transaction->t_outstanding_credits,
113 jbd2_descriptor_blocks_per_trans(journal) +
114 atomic_read(&journal->j_reserved_credits));
115 atomic_set(&transaction->t_outstanding_revokes, 0);
116 atomic_set(&transaction->t_handle_count, 0);
117 INIT_LIST_HEAD(&transaction->t_inode_list);
118 INIT_LIST_HEAD(&transaction->t_private_list);
119
120
121 journal->j_commit_timer.expires = round_jiffies_up(transaction->t_expires);
122 add_timer(&journal->j_commit_timer);
123
124 J_ASSERT(journal->j_running_transaction == NULL);
125 journal->j_running_transaction = transaction;
126 transaction->t_max_wait = 0;
127 transaction->t_start = jiffies;
128 transaction->t_requested = 0;
129}
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149static inline void update_t_max_wait(transaction_t *transaction,
150 unsigned long ts)
151{
152#ifdef CONFIG_JBD2_DEBUG
153 if (jbd2_journal_enable_debug &&
154 time_after(transaction->t_start, ts)) {
155 ts = jbd2_time_diff(ts, transaction->t_start);
156 spin_lock(&transaction->t_handle_lock);
157 if (ts > transaction->t_max_wait)
158 transaction->t_max_wait = ts;
159 spin_unlock(&transaction->t_handle_lock);
160 }
161#endif
162}
163
164
165
166
167
168
169static void wait_transaction_locked(journal_t *journal)
170 __releases(journal->j_state_lock)
171{
172 DEFINE_WAIT(wait);
173 int need_to_start;
174 tid_t tid = journal->j_running_transaction->t_tid;
175
176 prepare_to_wait(&journal->j_wait_transaction_locked, &wait,
177 TASK_UNINTERRUPTIBLE);
178 need_to_start = !tid_geq(journal->j_commit_request, tid);
179 read_unlock(&journal->j_state_lock);
180 if (need_to_start)
181 jbd2_log_start_commit(journal, tid);
182 jbd2_might_wait_for_commit(journal);
183 schedule();
184 finish_wait(&journal->j_wait_transaction_locked, &wait);
185}
186
187
188
189
190
191
192static void wait_transaction_switching(journal_t *journal)
193 __releases(journal->j_state_lock)
194{
195 DEFINE_WAIT(wait);
196
197 if (WARN_ON(!journal->j_running_transaction ||
198 journal->j_running_transaction->t_state != T_SWITCH)) {
199 read_unlock(&journal->j_state_lock);
200 return;
201 }
202 prepare_to_wait(&journal->j_wait_transaction_locked, &wait,
203 TASK_UNINTERRUPTIBLE);
204 read_unlock(&journal->j_state_lock);
205
206
207
208
209
210
211 schedule();
212 finish_wait(&journal->j_wait_transaction_locked, &wait);
213}
214
215static void sub_reserved_credits(journal_t *journal, int blocks)
216{
217 atomic_sub(blocks, &journal->j_reserved_credits);
218 wake_up(&journal->j_wait_reserved);
219}
220
221
222
223
224
225
226
227static int add_transaction_credits(journal_t *journal, int blocks,
228 int rsv_blocks)
229{
230 transaction_t *t = journal->j_running_transaction;
231 int needed;
232 int total = blocks + rsv_blocks;
233
234
235
236
237
238 if (t->t_state != T_RUNNING) {
239 WARN_ON_ONCE(t->t_state >= T_FLUSH);
240 wait_transaction_locked(journal);
241 return 1;
242 }
243
244
245
246
247
248
249 needed = atomic_add_return(total, &t->t_outstanding_credits);
250 if (needed > journal->j_max_transaction_buffers) {
251
252
253
254
255
256 atomic_sub(total, &t->t_outstanding_credits);
257
258
259
260
261
262 if (atomic_read(&journal->j_reserved_credits) + total >
263 journal->j_max_transaction_buffers) {
264 read_unlock(&journal->j_state_lock);
265 jbd2_might_wait_for_commit(journal);
266 wait_event(journal->j_wait_reserved,
267 atomic_read(&journal->j_reserved_credits) + total <=
268 journal->j_max_transaction_buffers);
269 return 1;
270 }
271
272 wait_transaction_locked(journal);
273 return 1;
274 }
275
276
277
278
279
280
281
282
283
284
285
286
287 if (jbd2_log_space_left(journal) < journal->j_max_transaction_buffers) {
288 atomic_sub(total, &t->t_outstanding_credits);
289 read_unlock(&journal->j_state_lock);
290 jbd2_might_wait_for_commit(journal);
291 write_lock(&journal->j_state_lock);
292 if (jbd2_log_space_left(journal) <
293 journal->j_max_transaction_buffers)
294 __jbd2_log_wait_for_space(journal);
295 write_unlock(&journal->j_state_lock);
296 return 1;
297 }
298
299
300 if (!rsv_blocks)
301 return 0;
302
303 needed = atomic_add_return(rsv_blocks, &journal->j_reserved_credits);
304
305 if (needed > journal->j_max_transaction_buffers / 2) {
306 sub_reserved_credits(journal, rsv_blocks);
307 atomic_sub(total, &t->t_outstanding_credits);
308 read_unlock(&journal->j_state_lock);
309 jbd2_might_wait_for_commit(journal);
310 wait_event(journal->j_wait_reserved,
311 atomic_read(&journal->j_reserved_credits) + rsv_blocks
312 <= journal->j_max_transaction_buffers / 2);
313 return 1;
314 }
315 return 0;
316}
317
318
319
320
321
322
323
324
325static int start_this_handle(journal_t *journal, handle_t *handle,
326 gfp_t gfp_mask)
327{
328 transaction_t *transaction, *new_transaction = NULL;
329 int blocks = handle->h_total_credits;
330 int rsv_blocks = 0;
331 unsigned long ts = jiffies;
332
333 if (handle->h_rsv_handle)
334 rsv_blocks = handle->h_rsv_handle->h_total_credits;
335
336
337
338
339
340
341 if ((rsv_blocks > journal->j_max_transaction_buffers / 2) ||
342 (rsv_blocks + blocks > journal->j_max_transaction_buffers)) {
343 printk(KERN_ERR "JBD2: %s wants too many credits "
344 "credits:%d rsv_credits:%d max:%d\n",
345 current->comm, blocks, rsv_blocks,
346 journal->j_max_transaction_buffers);
347 WARN_ON(1);
348 return -ENOSPC;
349 }
350
351alloc_transaction:
352 if (!journal->j_running_transaction) {
353
354
355
356
357 if ((gfp_mask & __GFP_FS) == 0)
358 gfp_mask |= __GFP_NOFAIL;
359 new_transaction = kmem_cache_zalloc(transaction_cache,
360 gfp_mask);
361 if (!new_transaction)
362 return -ENOMEM;
363 }
364
365 jbd_debug(3, "New handle %p going live.\n", handle);
366
367
368
369
370
371repeat:
372 read_lock(&journal->j_state_lock);
373 BUG_ON(journal->j_flags & JBD2_UNMOUNT);
374 if (is_journal_aborted(journal) ||
375 (journal->j_errno != 0 && !(journal->j_flags & JBD2_ACK_ERR))) {
376 read_unlock(&journal->j_state_lock);
377 jbd2_journal_free_transaction(new_transaction);
378 return -EROFS;
379 }
380
381
382
383
384
385
386 if (!handle->h_reserved && journal->j_barrier_count) {
387 read_unlock(&journal->j_state_lock);
388 wait_event(journal->j_wait_transaction_locked,
389 journal->j_barrier_count == 0);
390 goto repeat;
391 }
392
393 if (!journal->j_running_transaction) {
394 read_unlock(&journal->j_state_lock);
395 if (!new_transaction)
396 goto alloc_transaction;
397 write_lock(&journal->j_state_lock);
398 if (!journal->j_running_transaction &&
399 (handle->h_reserved || !journal->j_barrier_count)) {
400 jbd2_get_transaction(journal, new_transaction);
401 new_transaction = NULL;
402 }
403 write_unlock(&journal->j_state_lock);
404 goto repeat;
405 }
406
407 transaction = journal->j_running_transaction;
408
409 if (!handle->h_reserved) {
410
411 if (add_transaction_credits(journal, blocks, rsv_blocks))
412 goto repeat;
413 } else {
414
415
416
417
418
419
420
421 if (transaction->t_state == T_SWITCH) {
422 wait_transaction_switching(journal);
423 goto repeat;
424 }
425 sub_reserved_credits(journal, blocks);
426 handle->h_reserved = 0;
427 }
428
429
430
431
432 update_t_max_wait(transaction, ts);
433 handle->h_transaction = transaction;
434 handle->h_requested_credits = blocks;
435 handle->h_revoke_credits_requested = handle->h_revoke_credits;
436 handle->h_start_jiffies = jiffies;
437 atomic_inc(&transaction->t_updates);
438 atomic_inc(&transaction->t_handle_count);
439 jbd_debug(4, "Handle %p given %d credits (total %d, free %lu)\n",
440 handle, blocks,
441 atomic_read(&transaction->t_outstanding_credits),
442 jbd2_log_space_left(journal));
443 read_unlock(&journal->j_state_lock);
444 current->journal_info = handle;
445
446 rwsem_acquire_read(&journal->j_trans_commit_map, 0, 0, _THIS_IP_);
447 jbd2_journal_free_transaction(new_transaction);
448
449
450
451
452 handle->saved_alloc_context = memalloc_nofs_save();
453 return 0;
454}
455
456
457static handle_t *new_handle(int nblocks)
458{
459 handle_t *handle = jbd2_alloc_handle(GFP_NOFS);
460 if (!handle)
461 return NULL;
462 handle->h_total_credits = nblocks;
463 handle->h_ref = 1;
464
465 return handle;
466}
467
468handle_t *jbd2__journal_start(journal_t *journal, int nblocks, int rsv_blocks,
469 int revoke_records, gfp_t gfp_mask,
470 unsigned int type, unsigned int line_no)
471{
472 handle_t *handle = journal_current_handle();
473 int err;
474
475 if (!journal)
476 return ERR_PTR(-EROFS);
477
478 if (handle) {
479 J_ASSERT(handle->h_transaction->t_journal == journal);
480 handle->h_ref++;
481 return handle;
482 }
483
484 nblocks += DIV_ROUND_UP(revoke_records,
485 journal->j_revoke_records_per_block);
486 handle = new_handle(nblocks);
487 if (!handle)
488 return ERR_PTR(-ENOMEM);
489 if (rsv_blocks) {
490 handle_t *rsv_handle;
491
492 rsv_handle = new_handle(rsv_blocks);
493 if (!rsv_handle) {
494 jbd2_free_handle(handle);
495 return ERR_PTR(-ENOMEM);
496 }
497 rsv_handle->h_reserved = 1;
498 rsv_handle->h_journal = journal;
499 handle->h_rsv_handle = rsv_handle;
500 }
501 handle->h_revoke_credits = revoke_records;
502
503 err = start_this_handle(journal, handle, gfp_mask);
504 if (err < 0) {
505 if (handle->h_rsv_handle)
506 jbd2_free_handle(handle->h_rsv_handle);
507 jbd2_free_handle(handle);
508 return ERR_PTR(err);
509 }
510 handle->h_type = type;
511 handle->h_line_no = line_no;
512 trace_jbd2_handle_start(journal->j_fs_dev->bd_dev,
513 handle->h_transaction->t_tid, type,
514 line_no, nblocks);
515
516 return handle;
517}
518EXPORT_SYMBOL(jbd2__journal_start);
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540handle_t *jbd2_journal_start(journal_t *journal, int nblocks)
541{
542 return jbd2__journal_start(journal, nblocks, 0, 0, GFP_NOFS, 0, 0);
543}
544EXPORT_SYMBOL(jbd2_journal_start);
545
546static void __jbd2_journal_unreserve_handle(handle_t *handle, transaction_t *t)
547{
548 journal_t *journal = handle->h_journal;
549
550 WARN_ON(!handle->h_reserved);
551 sub_reserved_credits(journal, handle->h_total_credits);
552 if (t)
553 atomic_sub(handle->h_total_credits, &t->t_outstanding_credits);
554}
555
556void jbd2_journal_free_reserved(handle_t *handle)
557{
558 journal_t *journal = handle->h_journal;
559
560
561 read_lock(&journal->j_state_lock);
562 __jbd2_journal_unreserve_handle(handle, journal->j_running_transaction);
563 read_unlock(&journal->j_state_lock);
564 jbd2_free_handle(handle);
565}
566EXPORT_SYMBOL(jbd2_journal_free_reserved);
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582int jbd2_journal_start_reserved(handle_t *handle, unsigned int type,
583 unsigned int line_no)
584{
585 journal_t *journal = handle->h_journal;
586 int ret = -EIO;
587
588 if (WARN_ON(!handle->h_reserved)) {
589
590 jbd2_journal_stop(handle);
591 return ret;
592 }
593
594
595
596
597 if (WARN_ON(current->journal_info)) {
598 jbd2_journal_free_reserved(handle);
599 return ret;
600 }
601
602 handle->h_journal = NULL;
603
604
605
606
607 ret = start_this_handle(journal, handle, GFP_NOFS);
608 if (ret < 0) {
609 handle->h_journal = journal;
610 jbd2_journal_free_reserved(handle);
611 return ret;
612 }
613 handle->h_type = type;
614 handle->h_line_no = line_no;
615 trace_jbd2_handle_start(journal->j_fs_dev->bd_dev,
616 handle->h_transaction->t_tid, type,
617 line_no, handle->h_total_credits);
618 return 0;
619}
620EXPORT_SYMBOL(jbd2_journal_start_reserved);
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643int jbd2_journal_extend(handle_t *handle, int nblocks, int revoke_records)
644{
645 transaction_t *transaction = handle->h_transaction;
646 journal_t *journal;
647 int result;
648 int wanted;
649
650 if (is_handle_aborted(handle))
651 return -EROFS;
652 journal = transaction->t_journal;
653
654 result = 1;
655
656 read_lock(&journal->j_state_lock);
657
658
659 if (transaction->t_state != T_RUNNING) {
660 jbd_debug(3, "denied handle %p %d blocks: "
661 "transaction not running\n", handle, nblocks);
662 goto error_out;
663 }
664
665 nblocks += DIV_ROUND_UP(
666 handle->h_revoke_credits_requested + revoke_records,
667 journal->j_revoke_records_per_block) -
668 DIV_ROUND_UP(
669 handle->h_revoke_credits_requested,
670 journal->j_revoke_records_per_block);
671 spin_lock(&transaction->t_handle_lock);
672 wanted = atomic_add_return(nblocks,
673 &transaction->t_outstanding_credits);
674
675 if (wanted > journal->j_max_transaction_buffers) {
676 jbd_debug(3, "denied handle %p %d blocks: "
677 "transaction too large\n", handle, nblocks);
678 atomic_sub(nblocks, &transaction->t_outstanding_credits);
679 goto unlock;
680 }
681
682 trace_jbd2_handle_extend(journal->j_fs_dev->bd_dev,
683 transaction->t_tid,
684 handle->h_type, handle->h_line_no,
685 handle->h_total_credits,
686 nblocks);
687
688 handle->h_total_credits += nblocks;
689 handle->h_requested_credits += nblocks;
690 handle->h_revoke_credits += revoke_records;
691 handle->h_revoke_credits_requested += revoke_records;
692 result = 0;
693
694 jbd_debug(3, "extended handle %p by %d\n", handle, nblocks);
695unlock:
696 spin_unlock(&transaction->t_handle_lock);
697error_out:
698 read_unlock(&journal->j_state_lock);
699 return result;
700}
701
702static void stop_this_handle(handle_t *handle)
703{
704 transaction_t *transaction = handle->h_transaction;
705 journal_t *journal = transaction->t_journal;
706 int revokes;
707
708 J_ASSERT(journal_current_handle() == handle);
709 J_ASSERT(atomic_read(&transaction->t_updates) > 0);
710 current->journal_info = NULL;
711
712
713
714
715
716
717 revokes = handle->h_revoke_credits_requested - handle->h_revoke_credits;
718 if (revokes) {
719 int t_revokes, revoke_descriptors;
720 int rr_per_blk = journal->j_revoke_records_per_block;
721
722 WARN_ON_ONCE(DIV_ROUND_UP(revokes, rr_per_blk)
723 > handle->h_total_credits);
724 t_revokes = atomic_add_return(revokes,
725 &transaction->t_outstanding_revokes);
726 revoke_descriptors =
727 DIV_ROUND_UP(t_revokes, rr_per_blk) -
728 DIV_ROUND_UP(t_revokes - revokes, rr_per_blk);
729 handle->h_total_credits -= revoke_descriptors;
730 }
731 atomic_sub(handle->h_total_credits,
732 &transaction->t_outstanding_credits);
733 if (handle->h_rsv_handle)
734 __jbd2_journal_unreserve_handle(handle->h_rsv_handle,
735 transaction);
736 if (atomic_dec_and_test(&transaction->t_updates))
737 wake_up(&journal->j_wait_updates);
738
739 rwsem_release(&journal->j_trans_commit_map, _THIS_IP_);
740
741
742
743
744 memalloc_nofs_restore(handle->saved_alloc_context);
745}
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764int jbd2__journal_restart(handle_t *handle, int nblocks, int revoke_records,
765 gfp_t gfp_mask)
766{
767 transaction_t *transaction = handle->h_transaction;
768 journal_t *journal;
769 tid_t tid;
770 int need_to_start;
771 int ret;
772
773
774
775 if (is_handle_aborted(handle))
776 return 0;
777 journal = transaction->t_journal;
778 tid = transaction->t_tid;
779
780
781
782
783
784 jbd_debug(2, "restarting handle %p\n", handle);
785 stop_this_handle(handle);
786 handle->h_transaction = NULL;
787
788
789
790
791
792 read_lock(&journal->j_state_lock);
793 need_to_start = !tid_geq(journal->j_commit_request, tid);
794 read_unlock(&journal->j_state_lock);
795 if (need_to_start)
796 jbd2_log_start_commit(journal, tid);
797 handle->h_total_credits = nblocks +
798 DIV_ROUND_UP(revoke_records,
799 journal->j_revoke_records_per_block);
800 handle->h_revoke_credits = revoke_records;
801 ret = start_this_handle(journal, handle, gfp_mask);
802 trace_jbd2_handle_restart(journal->j_fs_dev->bd_dev,
803 ret ? 0 : handle->h_transaction->t_tid,
804 handle->h_type, handle->h_line_no,
805 handle->h_total_credits);
806 return ret;
807}
808EXPORT_SYMBOL(jbd2__journal_restart);
809
810
811int jbd2_journal_restart(handle_t *handle, int nblocks)
812{
813 return jbd2__journal_restart(handle, nblocks, 0, GFP_NOFS);
814}
815EXPORT_SYMBOL(jbd2_journal_restart);
816
817
818
819
820
821
822
823
824
825
826
827void jbd2_journal_lock_updates(journal_t *journal)
828{
829 DEFINE_WAIT(wait);
830
831 jbd2_might_wait_for_commit(journal);
832
833 write_lock(&journal->j_state_lock);
834 ++journal->j_barrier_count;
835
836
837 if (atomic_read(&journal->j_reserved_credits)) {
838 write_unlock(&journal->j_state_lock);
839 wait_event(journal->j_wait_reserved,
840 atomic_read(&journal->j_reserved_credits) == 0);
841 write_lock(&journal->j_state_lock);
842 }
843
844
845 while (1) {
846 transaction_t *transaction = journal->j_running_transaction;
847
848 if (!transaction)
849 break;
850
851 spin_lock(&transaction->t_handle_lock);
852 prepare_to_wait(&journal->j_wait_updates, &wait,
853 TASK_UNINTERRUPTIBLE);
854 if (!atomic_read(&transaction->t_updates)) {
855 spin_unlock(&transaction->t_handle_lock);
856 finish_wait(&journal->j_wait_updates, &wait);
857 break;
858 }
859 spin_unlock(&transaction->t_handle_lock);
860 write_unlock(&journal->j_state_lock);
861 schedule();
862 finish_wait(&journal->j_wait_updates, &wait);
863 write_lock(&journal->j_state_lock);
864 }
865 write_unlock(&journal->j_state_lock);
866
867
868
869
870
871
872
873 mutex_lock(&journal->j_barrier);
874}
875
876
877
878
879
880
881
882
883
884void jbd2_journal_unlock_updates (journal_t *journal)
885{
886 J_ASSERT(journal->j_barrier_count != 0);
887
888 mutex_unlock(&journal->j_barrier);
889 write_lock(&journal->j_state_lock);
890 --journal->j_barrier_count;
891 write_unlock(&journal->j_state_lock);
892 wake_up(&journal->j_wait_transaction_locked);
893}
894
895static void warn_dirty_buffer(struct buffer_head *bh)
896{
897 printk(KERN_WARNING
898 "JBD2: Spotted dirty metadata buffer (dev = %pg, blocknr = %llu). "
899 "There's a risk of filesystem corruption in case of system "
900 "crash.\n",
901 bh->b_bdev, (unsigned long long)bh->b_blocknr);
902}
903
904
905static void jbd2_freeze_jh_data(struct journal_head *jh)
906{
907 struct page *page;
908 int offset;
909 char *source;
910 struct buffer_head *bh = jh2bh(jh);
911
912 J_EXPECT_JH(jh, buffer_uptodate(bh), "Possible IO failure.\n");
913 page = bh->b_page;
914 offset = offset_in_page(bh->b_data);
915 source = kmap_atomic(page);
916
917 jbd2_buffer_frozen_trigger(jh, source + offset, jh->b_triggers);
918 memcpy(jh->b_frozen_data, source + offset, bh->b_size);
919 kunmap_atomic(source);
920
921
922
923
924
925 jh->b_frozen_triggers = jh->b_triggers;
926}
927
928
929
930
931
932
933
934
935
936
937
938static int
939do_get_write_access(handle_t *handle, struct journal_head *jh,
940 int force_copy)
941{
942 struct buffer_head *bh;
943 transaction_t *transaction = handle->h_transaction;
944 journal_t *journal;
945 int error;
946 char *frozen_buffer = NULL;
947 unsigned long start_lock, time_lock;
948
949 journal = transaction->t_journal;
950
951 jbd_debug(5, "journal_head %p, force_copy %d\n", jh, force_copy);
952
953 JBUFFER_TRACE(jh, "entry");
954repeat:
955 bh = jh2bh(jh);
956
957
958
959 start_lock = jiffies;
960 lock_buffer(bh);
961 spin_lock(&jh->b_state_lock);
962
963
964 time_lock = jbd2_time_diff(start_lock, jiffies);
965 if (time_lock > HZ/10)
966 trace_jbd2_lock_buffer_stall(bh->b_bdev->bd_dev,
967 jiffies_to_msecs(time_lock));
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982 if (buffer_dirty(bh)) {
983
984
985
986
987 if (jh->b_transaction) {
988 J_ASSERT_JH(jh,
989 jh->b_transaction == transaction ||
990 jh->b_transaction ==
991 journal->j_committing_transaction);
992 if (jh->b_next_transaction)
993 J_ASSERT_JH(jh, jh->b_next_transaction ==
994 transaction);
995 warn_dirty_buffer(bh);
996 }
997
998
999
1000
1001
1002 JBUFFER_TRACE(jh, "Journalling dirty buffer");
1003 clear_buffer_dirty(bh);
1004 set_buffer_jbddirty(bh);
1005 }
1006
1007 unlock_buffer(bh);
1008
1009 error = -EROFS;
1010 if (is_handle_aborted(handle)) {
1011 spin_unlock(&jh->b_state_lock);
1012 goto out;
1013 }
1014 error = 0;
1015
1016
1017
1018
1019
1020 if (jh->b_transaction == transaction ||
1021 jh->b_next_transaction == transaction)
1022 goto done;
1023
1024
1025
1026
1027
1028 jh->b_modified = 0;
1029
1030
1031
1032
1033
1034
1035 if (!jh->b_transaction) {
1036 JBUFFER_TRACE(jh, "no transaction");
1037 J_ASSERT_JH(jh, !jh->b_next_transaction);
1038 JBUFFER_TRACE(jh, "file as BJ_Reserved");
1039
1040
1041
1042
1043
1044 smp_wmb();
1045 spin_lock(&journal->j_list_lock);
1046 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
1047 spin_unlock(&journal->j_list_lock);
1048 goto done;
1049 }
1050
1051
1052
1053
1054 if (jh->b_frozen_data) {
1055 JBUFFER_TRACE(jh, "has frozen data");
1056 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
1057 goto attach_next;
1058 }
1059
1060 JBUFFER_TRACE(jh, "owned by older transaction");
1061 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
1062 J_ASSERT_JH(jh, jh->b_transaction == journal->j_committing_transaction);
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073 if (buffer_shadow(bh)) {
1074 JBUFFER_TRACE(jh, "on shadow: sleep");
1075 spin_unlock(&jh->b_state_lock);
1076 wait_on_bit_io(&bh->b_state, BH_Shadow, TASK_UNINTERRUPTIBLE);
1077 goto repeat;
1078 }
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092 if (jh->b_jlist == BJ_Metadata || force_copy) {
1093 JBUFFER_TRACE(jh, "generate frozen data");
1094 if (!frozen_buffer) {
1095 JBUFFER_TRACE(jh, "allocate memory for buffer");
1096 spin_unlock(&jh->b_state_lock);
1097 frozen_buffer = jbd2_alloc(jh2bh(jh)->b_size,
1098 GFP_NOFS | __GFP_NOFAIL);
1099 goto repeat;
1100 }
1101 jh->b_frozen_data = frozen_buffer;
1102 frozen_buffer = NULL;
1103 jbd2_freeze_jh_data(jh);
1104 }
1105attach_next:
1106
1107
1108
1109
1110
1111 smp_wmb();
1112 jh->b_next_transaction = transaction;
1113
1114done:
1115 spin_unlock(&jh->b_state_lock);
1116
1117
1118
1119
1120
1121 jbd2_journal_cancel_revoke(handle, jh);
1122
1123out:
1124 if (unlikely(frozen_buffer))
1125 jbd2_free(frozen_buffer, bh->b_size);
1126
1127 JBUFFER_TRACE(jh, "exit");
1128 return error;
1129}
1130
1131
1132static bool jbd2_write_access_granted(handle_t *handle, struct buffer_head *bh,
1133 bool undo)
1134{
1135 struct journal_head *jh;
1136 bool ret = false;
1137
1138
1139 if (buffer_dirty(bh))
1140 return false;
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153 rcu_read_lock();
1154 if (!buffer_jbd(bh))
1155 goto out;
1156
1157 jh = READ_ONCE(bh->b_private);
1158 if (!jh)
1159 goto out;
1160
1161 if (undo && !jh->b_committed_data)
1162 goto out;
1163 if (READ_ONCE(jh->b_transaction) != handle->h_transaction &&
1164 READ_ONCE(jh->b_next_transaction) != handle->h_transaction)
1165 goto out;
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175 smp_mb();
1176 if (unlikely(jh->b_bh != bh))
1177 goto out;
1178 ret = true;
1179out:
1180 rcu_read_unlock();
1181 return ret;
1182}
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196int jbd2_journal_get_write_access(handle_t *handle, struct buffer_head *bh)
1197{
1198 struct journal_head *jh;
1199 int rc;
1200
1201 if (is_handle_aborted(handle))
1202 return -EROFS;
1203
1204 if (jbd2_write_access_granted(handle, bh, false))
1205 return 0;
1206
1207 jh = jbd2_journal_add_journal_head(bh);
1208
1209
1210
1211 rc = do_get_write_access(handle, jh, 0);
1212 jbd2_journal_put_journal_head(jh);
1213 return rc;
1214}
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236int jbd2_journal_get_create_access(handle_t *handle, struct buffer_head *bh)
1237{
1238 transaction_t *transaction = handle->h_transaction;
1239 journal_t *journal;
1240 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
1241 int err;
1242
1243 jbd_debug(5, "journal_head %p\n", jh);
1244 err = -EROFS;
1245 if (is_handle_aborted(handle))
1246 goto out;
1247 journal = transaction->t_journal;
1248 err = 0;
1249
1250 JBUFFER_TRACE(jh, "entry");
1251
1252
1253
1254
1255
1256
1257
1258 spin_lock(&jh->b_state_lock);
1259 J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
1260 jh->b_transaction == NULL ||
1261 (jh->b_transaction == journal->j_committing_transaction &&
1262 jh->b_jlist == BJ_Forget)));
1263
1264 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
1265 J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
1266
1267 if (jh->b_transaction == NULL) {
1268
1269
1270
1271
1272
1273
1274
1275
1276 clear_buffer_dirty(jh2bh(jh));
1277
1278 jh->b_modified = 0;
1279
1280 JBUFFER_TRACE(jh, "file as BJ_Reserved");
1281 spin_lock(&journal->j_list_lock);
1282 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
1283 spin_unlock(&journal->j_list_lock);
1284 } else if (jh->b_transaction == journal->j_committing_transaction) {
1285
1286 jh->b_modified = 0;
1287
1288 JBUFFER_TRACE(jh, "set next transaction");
1289 spin_lock(&journal->j_list_lock);
1290 jh->b_next_transaction = transaction;
1291 spin_unlock(&journal->j_list_lock);
1292 }
1293 spin_unlock(&jh->b_state_lock);
1294
1295
1296
1297
1298
1299
1300
1301
1302 JBUFFER_TRACE(jh, "cancelling revoke");
1303 jbd2_journal_cancel_revoke(handle, jh);
1304out:
1305 jbd2_journal_put_journal_head(jh);
1306 return err;
1307}
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335int jbd2_journal_get_undo_access(handle_t *handle, struct buffer_head *bh)
1336{
1337 int err;
1338 struct journal_head *jh;
1339 char *committed_data = NULL;
1340
1341 if (is_handle_aborted(handle))
1342 return -EROFS;
1343
1344 if (jbd2_write_access_granted(handle, bh, true))
1345 return 0;
1346
1347 jh = jbd2_journal_add_journal_head(bh);
1348 JBUFFER_TRACE(jh, "entry");
1349
1350
1351
1352
1353
1354
1355 err = do_get_write_access(handle, jh, 1);
1356 if (err)
1357 goto out;
1358
1359repeat:
1360 if (!jh->b_committed_data)
1361 committed_data = jbd2_alloc(jh2bh(jh)->b_size,
1362 GFP_NOFS|__GFP_NOFAIL);
1363
1364 spin_lock(&jh->b_state_lock);
1365 if (!jh->b_committed_data) {
1366
1367
1368 JBUFFER_TRACE(jh, "generate b_committed data");
1369 if (!committed_data) {
1370 spin_unlock(&jh->b_state_lock);
1371 goto repeat;
1372 }
1373
1374 jh->b_committed_data = committed_data;
1375 committed_data = NULL;
1376 memcpy(jh->b_committed_data, bh->b_data, bh->b_size);
1377 }
1378 spin_unlock(&jh->b_state_lock);
1379out:
1380 jbd2_journal_put_journal_head(jh);
1381 if (unlikely(committed_data))
1382 jbd2_free(committed_data, bh->b_size);
1383 return err;
1384}
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397void jbd2_journal_set_triggers(struct buffer_head *bh,
1398 struct jbd2_buffer_trigger_type *type)
1399{
1400 struct journal_head *jh = jbd2_journal_grab_journal_head(bh);
1401
1402 if (WARN_ON(!jh))
1403 return;
1404 jh->b_triggers = type;
1405 jbd2_journal_put_journal_head(jh);
1406}
1407
1408void jbd2_buffer_frozen_trigger(struct journal_head *jh, void *mapped_data,
1409 struct jbd2_buffer_trigger_type *triggers)
1410{
1411 struct buffer_head *bh = jh2bh(jh);
1412
1413 if (!triggers || !triggers->t_frozen)
1414 return;
1415
1416 triggers->t_frozen(triggers, bh, mapped_data, bh->b_size);
1417}
1418
1419void jbd2_buffer_abort_trigger(struct journal_head *jh,
1420 struct jbd2_buffer_trigger_type *triggers)
1421{
1422 if (!triggers || !triggers->t_abort)
1423 return;
1424
1425 triggers->t_abort(triggers, jh2bh(jh));
1426}
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
1452{
1453 transaction_t *transaction = handle->h_transaction;
1454 journal_t *journal;
1455 struct journal_head *jh;
1456 int ret = 0;
1457
1458 if (is_handle_aborted(handle))
1459 return -EROFS;
1460 if (!buffer_jbd(bh))
1461 return -EUCLEAN;
1462
1463
1464
1465
1466
1467 jh = bh2jh(bh);
1468 jbd_debug(5, "journal_head %p\n", jh);
1469 JBUFFER_TRACE(jh, "entry");
1470
1471
1472
1473
1474
1475
1476
1477 if (jh->b_transaction != transaction &&
1478 jh->b_next_transaction != transaction) {
1479 spin_lock(&jh->b_state_lock);
1480 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
1481 jh->b_next_transaction == transaction);
1482 spin_unlock(&jh->b_state_lock);
1483 }
1484 if (jh->b_modified == 1) {
1485
1486 if (jh->b_transaction == transaction &&
1487 jh->b_jlist != BJ_Metadata) {
1488 spin_lock(&jh->b_state_lock);
1489 if (jh->b_transaction == transaction &&
1490 jh->b_jlist != BJ_Metadata)
1491 pr_err("JBD2: assertion failure: h_type=%u "
1492 "h_line_no=%u block_no=%llu jlist=%u\n",
1493 handle->h_type, handle->h_line_no,
1494 (unsigned long long) bh->b_blocknr,
1495 jh->b_jlist);
1496 J_ASSERT_JH(jh, jh->b_transaction != transaction ||
1497 jh->b_jlist == BJ_Metadata);
1498 spin_unlock(&jh->b_state_lock);
1499 }
1500 goto out;
1501 }
1502
1503 journal = transaction->t_journal;
1504 spin_lock(&jh->b_state_lock);
1505
1506 if (jh->b_modified == 0) {
1507
1508
1509
1510
1511
1512 if (WARN_ON_ONCE(jbd2_handle_buffer_credits(handle) <= 0)) {
1513 ret = -ENOSPC;
1514 goto out_unlock_bh;
1515 }
1516 jh->b_modified = 1;
1517 handle->h_total_credits--;
1518 }
1519
1520
1521
1522
1523
1524
1525
1526
1527 if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) {
1528 JBUFFER_TRACE(jh, "fastpath");
1529 if (unlikely(jh->b_transaction !=
1530 journal->j_running_transaction)) {
1531 printk(KERN_ERR "JBD2: %s: "
1532 "jh->b_transaction (%llu, %p, %u) != "
1533 "journal->j_running_transaction (%p, %u)\n",
1534 journal->j_devname,
1535 (unsigned long long) bh->b_blocknr,
1536 jh->b_transaction,
1537 jh->b_transaction ? jh->b_transaction->t_tid : 0,
1538 journal->j_running_transaction,
1539 journal->j_running_transaction ?
1540 journal->j_running_transaction->t_tid : 0);
1541 ret = -EINVAL;
1542 }
1543 goto out_unlock_bh;
1544 }
1545
1546 set_buffer_jbddirty(bh);
1547
1548
1549
1550
1551
1552
1553
1554 if (jh->b_transaction != transaction) {
1555 JBUFFER_TRACE(jh, "already on other transaction");
1556 if (unlikely(((jh->b_transaction !=
1557 journal->j_committing_transaction)) ||
1558 (jh->b_next_transaction != transaction))) {
1559 printk(KERN_ERR "jbd2_journal_dirty_metadata: %s: "
1560 "bad jh for block %llu: "
1561 "transaction (%p, %u), "
1562 "jh->b_transaction (%p, %u), "
1563 "jh->b_next_transaction (%p, %u), jlist %u\n",
1564 journal->j_devname,
1565 (unsigned long long) bh->b_blocknr,
1566 transaction, transaction->t_tid,
1567 jh->b_transaction,
1568 jh->b_transaction ?
1569 jh->b_transaction->t_tid : 0,
1570 jh->b_next_transaction,
1571 jh->b_next_transaction ?
1572 jh->b_next_transaction->t_tid : 0,
1573 jh->b_jlist);
1574 WARN_ON(1);
1575 ret = -EINVAL;
1576 }
1577
1578
1579 goto out_unlock_bh;
1580 }
1581
1582
1583 J_ASSERT_JH(jh, jh->b_frozen_data == NULL);
1584
1585 JBUFFER_TRACE(jh, "file as BJ_Metadata");
1586 spin_lock(&journal->j_list_lock);
1587 __jbd2_journal_file_buffer(jh, transaction, BJ_Metadata);
1588 spin_unlock(&journal->j_list_lock);
1589out_unlock_bh:
1590 spin_unlock(&jh->b_state_lock);
1591out:
1592 JBUFFER_TRACE(jh, "exit");
1593 return ret;
1594}
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613int jbd2_journal_forget(handle_t *handle, struct buffer_head *bh)
1614{
1615 transaction_t *transaction = handle->h_transaction;
1616 journal_t *journal;
1617 struct journal_head *jh;
1618 int drop_reserve = 0;
1619 int err = 0;
1620 int was_modified = 0;
1621
1622 if (is_handle_aborted(handle))
1623 return -EROFS;
1624 journal = transaction->t_journal;
1625
1626 BUFFER_TRACE(bh, "entry");
1627
1628 jh = jbd2_journal_grab_journal_head(bh);
1629 if (!jh) {
1630 __bforget(bh);
1631 return 0;
1632 }
1633
1634 spin_lock(&jh->b_state_lock);
1635
1636
1637
1638 if (!J_EXPECT_JH(jh, !jh->b_committed_data,
1639 "inconsistent data on disk")) {
1640 err = -EIO;
1641 goto drop;
1642 }
1643
1644
1645 was_modified = jh->b_modified;
1646
1647
1648
1649
1650
1651 jh->b_modified = 0;
1652
1653 if (jh->b_transaction == transaction) {
1654 J_ASSERT_JH(jh, !jh->b_frozen_data);
1655
1656
1657
1658
1659 clear_buffer_dirty(bh);
1660 clear_buffer_jbddirty(bh);
1661
1662 JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
1663
1664
1665
1666
1667
1668 if (was_modified)
1669 drop_reserve = 1;
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683 spin_lock(&journal->j_list_lock);
1684 if (jh->b_cp_transaction) {
1685 __jbd2_journal_temp_unlink_buffer(jh);
1686 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
1687 } else {
1688 __jbd2_journal_unfile_buffer(jh);
1689 jbd2_journal_put_journal_head(jh);
1690 }
1691 spin_unlock(&journal->j_list_lock);
1692 } else if (jh->b_transaction) {
1693 J_ASSERT_JH(jh, (jh->b_transaction ==
1694 journal->j_committing_transaction));
1695
1696
1697 JBUFFER_TRACE(jh, "belongs to older transaction");
1698
1699
1700
1701
1702
1703
1704
1705 set_buffer_freed(bh);
1706
1707 if (!jh->b_next_transaction) {
1708 spin_lock(&journal->j_list_lock);
1709 jh->b_next_transaction = transaction;
1710 spin_unlock(&journal->j_list_lock);
1711 } else {
1712 J_ASSERT(jh->b_next_transaction == transaction);
1713
1714
1715
1716
1717
1718 if (was_modified)
1719 drop_reserve = 1;
1720 }
1721 } else {
1722
1723
1724
1725
1726
1727 spin_lock(&journal->j_list_lock);
1728 if (!jh->b_cp_transaction) {
1729 JBUFFER_TRACE(jh, "belongs to none transaction");
1730 spin_unlock(&journal->j_list_lock);
1731 goto drop;
1732 }
1733
1734
1735
1736
1737
1738 if (!buffer_dirty(bh)) {
1739 __jbd2_journal_remove_checkpoint(jh);
1740 spin_unlock(&journal->j_list_lock);
1741 goto drop;
1742 }
1743
1744
1745
1746
1747
1748
1749
1750 clear_buffer_dirty(bh);
1751 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
1752 spin_unlock(&journal->j_list_lock);
1753 }
1754drop:
1755 __brelse(bh);
1756 spin_unlock(&jh->b_state_lock);
1757 jbd2_journal_put_journal_head(jh);
1758 if (drop_reserve) {
1759
1760 handle->h_total_credits++;
1761 }
1762 return err;
1763}
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781int jbd2_journal_stop(handle_t *handle)
1782{
1783 transaction_t *transaction = handle->h_transaction;
1784 journal_t *journal;
1785 int err = 0, wait_for_commit = 0;
1786 tid_t tid;
1787 pid_t pid;
1788
1789 if (--handle->h_ref > 0) {
1790 jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
1791 handle->h_ref);
1792 if (is_handle_aborted(handle))
1793 return -EIO;
1794 return 0;
1795 }
1796 if (!transaction) {
1797
1798
1799
1800
1801 memalloc_nofs_restore(handle->saved_alloc_context);
1802 goto free_and_exit;
1803 }
1804 journal = transaction->t_journal;
1805 tid = transaction->t_tid;
1806
1807 if (is_handle_aborted(handle))
1808 err = -EIO;
1809
1810 jbd_debug(4, "Handle %p going down\n", handle);
1811 trace_jbd2_handle_stats(journal->j_fs_dev->bd_dev,
1812 tid, handle->h_type, handle->h_line_no,
1813 jiffies - handle->h_start_jiffies,
1814 handle->h_sync, handle->h_requested_credits,
1815 (handle->h_requested_credits -
1816 handle->h_total_credits));
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847 pid = current->pid;
1848 if (handle->h_sync && journal->j_last_sync_writer != pid &&
1849 journal->j_max_batch_time) {
1850 u64 commit_time, trans_time;
1851
1852 journal->j_last_sync_writer = pid;
1853
1854 read_lock(&journal->j_state_lock);
1855 commit_time = journal->j_average_commit_time;
1856 read_unlock(&journal->j_state_lock);
1857
1858 trans_time = ktime_to_ns(ktime_sub(ktime_get(),
1859 transaction->t_start_time));
1860
1861 commit_time = max_t(u64, commit_time,
1862 1000*journal->j_min_batch_time);
1863 commit_time = min_t(u64, commit_time,
1864 1000*journal->j_max_batch_time);
1865
1866 if (trans_time < commit_time) {
1867 ktime_t expires = ktime_add_ns(ktime_get(),
1868 commit_time);
1869 set_current_state(TASK_UNINTERRUPTIBLE);
1870 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1871 }
1872 }
1873
1874 if (handle->h_sync)
1875 transaction->t_synchronous_commit = 1;
1876
1877
1878
1879
1880
1881
1882 if (handle->h_sync ||
1883 time_after_eq(jiffies, transaction->t_expires)) {
1884
1885
1886
1887
1888 jbd_debug(2, "transaction too old, requesting commit for "
1889 "handle %p\n", handle);
1890
1891 jbd2_log_start_commit(journal, tid);
1892
1893
1894
1895
1896
1897 if (handle->h_sync && !(current->flags & PF_MEMALLOC))
1898 wait_for_commit = 1;
1899 }
1900
1901
1902
1903
1904
1905
1906
1907 stop_this_handle(handle);
1908
1909 if (wait_for_commit)
1910 err = jbd2_log_wait_commit(journal, tid);
1911
1912free_and_exit:
1913 if (handle->h_rsv_handle)
1914 jbd2_free_handle(handle->h_rsv_handle);
1915 jbd2_free_handle(handle);
1916 return err;
1917}
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935static inline void
1936__blist_add_buffer(struct journal_head **list, struct journal_head *jh)
1937{
1938 if (!*list) {
1939 jh->b_tnext = jh->b_tprev = jh;
1940 *list = jh;
1941 } else {
1942
1943 struct journal_head *first = *list, *last = first->b_tprev;
1944 jh->b_tprev = last;
1945 jh->b_tnext = first;
1946 last->b_tnext = first->b_tprev = jh;
1947 }
1948}
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959static inline void
1960__blist_del_buffer(struct journal_head **list, struct journal_head *jh)
1961{
1962 if (*list == jh) {
1963 *list = jh->b_tnext;
1964 if (*list == jh)
1965 *list = NULL;
1966 }
1967 jh->b_tprev->b_tnext = jh->b_tnext;
1968 jh->b_tnext->b_tprev = jh->b_tprev;
1969}
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh)
1983{
1984 struct journal_head **list = NULL;
1985 transaction_t *transaction;
1986 struct buffer_head *bh = jh2bh(jh);
1987
1988 lockdep_assert_held(&jh->b_state_lock);
1989 transaction = jh->b_transaction;
1990 if (transaction)
1991 assert_spin_locked(&transaction->t_journal->j_list_lock);
1992
1993 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1994 if (jh->b_jlist != BJ_None)
1995 J_ASSERT_JH(jh, transaction != NULL);
1996
1997 switch (jh->b_jlist) {
1998 case BJ_None:
1999 return;
2000 case BJ_Metadata:
2001 transaction->t_nr_buffers--;
2002 J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
2003 list = &transaction->t_buffers;
2004 break;
2005 case BJ_Forget:
2006 list = &transaction->t_forget;
2007 break;
2008 case BJ_Shadow:
2009 list = &transaction->t_shadow_list;
2010 break;
2011 case BJ_Reserved:
2012 list = &transaction->t_reserved_list;
2013 break;
2014 }
2015
2016 __blist_del_buffer(list, jh);
2017 jh->b_jlist = BJ_None;
2018 if (transaction && is_journal_aborted(transaction->t_journal))
2019 clear_buffer_jbddirty(bh);
2020 else if (test_clear_buffer_jbddirty(bh))
2021 mark_buffer_dirty(bh);
2022}
2023
2024
2025
2026
2027
2028
2029
2030static void __jbd2_journal_unfile_buffer(struct journal_head *jh)
2031{
2032 J_ASSERT_JH(jh, jh->b_transaction != NULL);
2033 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
2034
2035 __jbd2_journal_temp_unlink_buffer(jh);
2036 jh->b_transaction = NULL;
2037}
2038
2039void jbd2_journal_unfile_buffer(journal_t *journal, struct journal_head *jh)
2040{
2041 struct buffer_head *bh = jh2bh(jh);
2042
2043
2044 get_bh(bh);
2045 spin_lock(&jh->b_state_lock);
2046 spin_lock(&journal->j_list_lock);
2047 __jbd2_journal_unfile_buffer(jh);
2048 spin_unlock(&journal->j_list_lock);
2049 spin_unlock(&jh->b_state_lock);
2050 jbd2_journal_put_journal_head(jh);
2051 __brelse(bh);
2052}
2053
2054
2055
2056
2057
2058
2059static void
2060__journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh)
2061{
2062 struct journal_head *jh;
2063
2064 jh = bh2jh(bh);
2065
2066 if (buffer_locked(bh) || buffer_dirty(bh))
2067 goto out;
2068
2069 if (jh->b_next_transaction != NULL || jh->b_transaction != NULL)
2070 goto out;
2071
2072 spin_lock(&journal->j_list_lock);
2073 if (jh->b_cp_transaction != NULL) {
2074
2075 JBUFFER_TRACE(jh, "remove from checkpoint list");
2076 __jbd2_journal_remove_checkpoint(jh);
2077 }
2078 spin_unlock(&journal->j_list_lock);
2079out:
2080 return;
2081}
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117int jbd2_journal_try_to_free_buffers(journal_t *journal, struct page *page)
2118{
2119 struct buffer_head *head;
2120 struct buffer_head *bh;
2121 bool has_write_io_error = false;
2122 int ret = 0;
2123
2124 J_ASSERT(PageLocked(page));
2125
2126 head = page_buffers(page);
2127 bh = head;
2128 do {
2129 struct journal_head *jh;
2130
2131
2132
2133
2134
2135
2136 jh = jbd2_journal_grab_journal_head(bh);
2137 if (!jh)
2138 continue;
2139
2140 spin_lock(&jh->b_state_lock);
2141 __journal_try_to_free_buffer(journal, bh);
2142 spin_unlock(&jh->b_state_lock);
2143 jbd2_journal_put_journal_head(jh);
2144 if (buffer_jbd(bh))
2145 goto busy;
2146
2147
2148
2149
2150
2151
2152
2153 if (buffer_write_io_error(bh)) {
2154 pr_err("JBD2: Error while async write back metadata bh %llu.",
2155 (unsigned long long)bh->b_blocknr);
2156 has_write_io_error = true;
2157 }
2158 } while ((bh = bh->b_this_page) != head);
2159
2160 ret = try_to_free_buffers(page);
2161
2162busy:
2163 if (has_write_io_error)
2164 jbd2_journal_abort(journal, -EIO);
2165
2166 return ret;
2167}
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
2182{
2183 int may_free = 1;
2184 struct buffer_head *bh = jh2bh(jh);
2185
2186 if (jh->b_cp_transaction) {
2187 JBUFFER_TRACE(jh, "on running+cp transaction");
2188 __jbd2_journal_temp_unlink_buffer(jh);
2189
2190
2191
2192
2193
2194 clear_buffer_dirty(bh);
2195 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
2196 may_free = 0;
2197 } else {
2198 JBUFFER_TRACE(jh, "on running transaction");
2199 __jbd2_journal_unfile_buffer(jh);
2200 jbd2_journal_put_journal_head(jh);
2201 }
2202 return may_free;
2203}
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh,
2253 int partial_page)
2254{
2255 transaction_t *transaction;
2256 struct journal_head *jh;
2257 int may_free = 1;
2258
2259 BUFFER_TRACE(bh, "entry");
2260
2261
2262
2263
2264
2265
2266
2267 jh = jbd2_journal_grab_journal_head(bh);
2268 if (!jh)
2269 goto zap_buffer_unlocked;
2270
2271
2272 write_lock(&journal->j_state_lock);
2273 spin_lock(&jh->b_state_lock);
2274 spin_lock(&journal->j_list_lock);
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299 transaction = jh->b_transaction;
2300 if (transaction == NULL) {
2301
2302
2303
2304
2305 if (!jh->b_cp_transaction) {
2306 JBUFFER_TRACE(jh, "not on any transaction: zap");
2307 goto zap_buffer;
2308 }
2309
2310 if (!buffer_dirty(bh)) {
2311
2312 __jbd2_journal_remove_checkpoint(jh);
2313 goto zap_buffer;
2314 }
2315
2316
2317
2318
2319
2320 if (journal->j_running_transaction) {
2321
2322
2323
2324 JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
2325 may_free = __dispose_buffer(jh,
2326 journal->j_running_transaction);
2327 goto zap_buffer;
2328 } else {
2329
2330
2331
2332
2333 if (journal->j_committing_transaction) {
2334 JBUFFER_TRACE(jh, "give to committing trans");
2335 may_free = __dispose_buffer(jh,
2336 journal->j_committing_transaction);
2337 goto zap_buffer;
2338 } else {
2339
2340
2341 clear_buffer_jbddirty(bh);
2342 __jbd2_journal_remove_checkpoint(jh);
2343 goto zap_buffer;
2344 }
2345 }
2346 } else if (transaction == journal->j_committing_transaction) {
2347 JBUFFER_TRACE(jh, "on committing transaction");
2348
2349
2350
2351
2352
2353 if (partial_page) {
2354 spin_unlock(&journal->j_list_lock);
2355 spin_unlock(&jh->b_state_lock);
2356 write_unlock(&journal->j_state_lock);
2357 jbd2_journal_put_journal_head(jh);
2358 return -EBUSY;
2359 }
2360
2361
2362
2363
2364
2365
2366
2367 set_buffer_freed(bh);
2368 if (journal->j_running_transaction && buffer_jbddirty(bh))
2369 jh->b_next_transaction = journal->j_running_transaction;
2370 jh->b_modified = 0;
2371 spin_unlock(&journal->j_list_lock);
2372 spin_unlock(&jh->b_state_lock);
2373 write_unlock(&journal->j_state_lock);
2374 jbd2_journal_put_journal_head(jh);
2375 return 0;
2376 } else {
2377
2378
2379
2380
2381
2382
2383 J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
2384 JBUFFER_TRACE(jh, "on running transaction");
2385 may_free = __dispose_buffer(jh, transaction);
2386 }
2387
2388zap_buffer:
2389
2390
2391
2392
2393
2394
2395
2396
2397 jh->b_modified = 0;
2398 spin_unlock(&journal->j_list_lock);
2399 spin_unlock(&jh->b_state_lock);
2400 write_unlock(&journal->j_state_lock);
2401 jbd2_journal_put_journal_head(jh);
2402zap_buffer_unlocked:
2403 clear_buffer_dirty(bh);
2404 J_ASSERT_BH(bh, !buffer_jbddirty(bh));
2405 clear_buffer_mapped(bh);
2406 clear_buffer_req(bh);
2407 clear_buffer_new(bh);
2408 clear_buffer_delay(bh);
2409 clear_buffer_unwritten(bh);
2410 bh->b_bdev = NULL;
2411 return may_free;
2412}
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426int jbd2_journal_invalidatepage(journal_t *journal,
2427 struct page *page,
2428 unsigned int offset,
2429 unsigned int length)
2430{
2431 struct buffer_head *head, *bh, *next;
2432 unsigned int stop = offset + length;
2433 unsigned int curr_off = 0;
2434 int partial_page = (offset || length < PAGE_SIZE);
2435 int may_free = 1;
2436 int ret = 0;
2437
2438 if (!PageLocked(page))
2439 BUG();
2440 if (!page_has_buffers(page))
2441 return 0;
2442
2443 BUG_ON(stop > PAGE_SIZE || stop < length);
2444
2445
2446
2447
2448
2449 head = bh = page_buffers(page);
2450 do {
2451 unsigned int next_off = curr_off + bh->b_size;
2452 next = bh->b_this_page;
2453
2454 if (next_off > stop)
2455 return 0;
2456
2457 if (offset <= curr_off) {
2458
2459 lock_buffer(bh);
2460 ret = journal_unmap_buffer(journal, bh, partial_page);
2461 unlock_buffer(bh);
2462 if (ret < 0)
2463 return ret;
2464 may_free &= ret;
2465 }
2466 curr_off = next_off;
2467 bh = next;
2468
2469 } while (bh != head);
2470
2471 if (!partial_page) {
2472 if (may_free && try_to_free_buffers(page))
2473 J_ASSERT(!page_has_buffers(page));
2474 }
2475 return 0;
2476}
2477
2478
2479
2480
2481void __jbd2_journal_file_buffer(struct journal_head *jh,
2482 transaction_t *transaction, int jlist)
2483{
2484 struct journal_head **list = NULL;
2485 int was_dirty = 0;
2486 struct buffer_head *bh = jh2bh(jh);
2487
2488 lockdep_assert_held(&jh->b_state_lock);
2489 assert_spin_locked(&transaction->t_journal->j_list_lock);
2490
2491 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
2492 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
2493 jh->b_transaction == NULL);
2494
2495 if (jh->b_transaction && jh->b_jlist == jlist)
2496 return;
2497
2498 if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
2499 jlist == BJ_Shadow || jlist == BJ_Forget) {
2500
2501
2502
2503
2504
2505
2506
2507 if (buffer_dirty(bh))
2508 warn_dirty_buffer(bh);
2509 if (test_clear_buffer_dirty(bh) ||
2510 test_clear_buffer_jbddirty(bh))
2511 was_dirty = 1;
2512 }
2513
2514 if (jh->b_transaction)
2515 __jbd2_journal_temp_unlink_buffer(jh);
2516 else
2517 jbd2_journal_grab_journal_head(bh);
2518 jh->b_transaction = transaction;
2519
2520 switch (jlist) {
2521 case BJ_None:
2522 J_ASSERT_JH(jh, !jh->b_committed_data);
2523 J_ASSERT_JH(jh, !jh->b_frozen_data);
2524 return;
2525 case BJ_Metadata:
2526 transaction->t_nr_buffers++;
2527 list = &transaction->t_buffers;
2528 break;
2529 case BJ_Forget:
2530 list = &transaction->t_forget;
2531 break;
2532 case BJ_Shadow:
2533 list = &transaction->t_shadow_list;
2534 break;
2535 case BJ_Reserved:
2536 list = &transaction->t_reserved_list;
2537 break;
2538 }
2539
2540 __blist_add_buffer(list, jh);
2541 jh->b_jlist = jlist;
2542
2543 if (was_dirty)
2544 set_buffer_jbddirty(bh);
2545}
2546
2547void jbd2_journal_file_buffer(struct journal_head *jh,
2548 transaction_t *transaction, int jlist)
2549{
2550 spin_lock(&jh->b_state_lock);
2551 spin_lock(&transaction->t_journal->j_list_lock);
2552 __jbd2_journal_file_buffer(jh, transaction, jlist);
2553 spin_unlock(&transaction->t_journal->j_list_lock);
2554 spin_unlock(&jh->b_state_lock);
2555}
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570bool __jbd2_journal_refile_buffer(struct journal_head *jh)
2571{
2572 int was_dirty, jlist;
2573 struct buffer_head *bh = jh2bh(jh);
2574
2575 lockdep_assert_held(&jh->b_state_lock);
2576 if (jh->b_transaction)
2577 assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
2578
2579
2580 if (jh->b_next_transaction == NULL) {
2581 __jbd2_journal_unfile_buffer(jh);
2582 return true;
2583 }
2584
2585
2586
2587
2588
2589
2590 was_dirty = test_clear_buffer_jbddirty(bh);
2591 __jbd2_journal_temp_unlink_buffer(jh);
2592
2593
2594
2595
2596
2597 J_ASSERT_JH(jh, jh->b_transaction != NULL);
2598
2599
2600
2601
2602
2603
2604 WRITE_ONCE(jh->b_transaction, jh->b_next_transaction);
2605 WRITE_ONCE(jh->b_next_transaction, NULL);
2606 if (buffer_freed(bh))
2607 jlist = BJ_Forget;
2608 else if (jh->b_modified)
2609 jlist = BJ_Metadata;
2610 else
2611 jlist = BJ_Reserved;
2612 __jbd2_journal_file_buffer(jh, jh->b_transaction, jlist);
2613 J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
2614
2615 if (was_dirty)
2616 set_buffer_jbddirty(bh);
2617 return false;
2618}
2619
2620
2621
2622
2623
2624
2625
2626void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh)
2627{
2628 bool drop;
2629
2630 spin_lock(&jh->b_state_lock);
2631 spin_lock(&journal->j_list_lock);
2632 drop = __jbd2_journal_refile_buffer(jh);
2633 spin_unlock(&jh->b_state_lock);
2634 spin_unlock(&journal->j_list_lock);
2635 if (drop)
2636 jbd2_journal_put_journal_head(jh);
2637}
2638
2639
2640
2641
2642static int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode,
2643 unsigned long flags, loff_t start_byte, loff_t end_byte)
2644{
2645 transaction_t *transaction = handle->h_transaction;
2646 journal_t *journal;
2647
2648 if (is_handle_aborted(handle))
2649 return -EROFS;
2650 journal = transaction->t_journal;
2651
2652 jbd_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino,
2653 transaction->t_tid);
2654
2655 spin_lock(&journal->j_list_lock);
2656 jinode->i_flags |= flags;
2657
2658 if (jinode->i_dirty_end) {
2659 jinode->i_dirty_start = min(jinode->i_dirty_start, start_byte);
2660 jinode->i_dirty_end = max(jinode->i_dirty_end, end_byte);
2661 } else {
2662 jinode->i_dirty_start = start_byte;
2663 jinode->i_dirty_end = end_byte;
2664 }
2665
2666
2667 if (jinode->i_transaction == transaction ||
2668 jinode->i_next_transaction == transaction)
2669 goto done;
2670
2671
2672
2673
2674
2675
2676 if (!transaction->t_need_data_flush)
2677 transaction->t_need_data_flush = 1;
2678
2679
2680 if (jinode->i_transaction) {
2681 J_ASSERT(jinode->i_next_transaction == NULL);
2682 J_ASSERT(jinode->i_transaction ==
2683 journal->j_committing_transaction);
2684 jinode->i_next_transaction = transaction;
2685 goto done;
2686 }
2687
2688 J_ASSERT(!jinode->i_next_transaction);
2689 jinode->i_transaction = transaction;
2690 list_add(&jinode->i_list, &transaction->t_inode_list);
2691done:
2692 spin_unlock(&journal->j_list_lock);
2693
2694 return 0;
2695}
2696
2697int jbd2_journal_inode_ranged_write(handle_t *handle,
2698 struct jbd2_inode *jinode, loff_t start_byte, loff_t length)
2699{
2700 return jbd2_journal_file_inode(handle, jinode,
2701 JI_WRITE_DATA | JI_WAIT_DATA, start_byte,
2702 start_byte + length - 1);
2703}
2704
2705int jbd2_journal_inode_ranged_wait(handle_t *handle, struct jbd2_inode *jinode,
2706 loff_t start_byte, loff_t length)
2707{
2708 return jbd2_journal_file_inode(handle, jinode, JI_WAIT_DATA,
2709 start_byte, start_byte + length - 1);
2710}
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732int jbd2_journal_begin_ordered_truncate(journal_t *journal,
2733 struct jbd2_inode *jinode,
2734 loff_t new_size)
2735{
2736 transaction_t *inode_trans, *commit_trans;
2737 int ret = 0;
2738
2739
2740 if (!jinode->i_transaction)
2741 goto out;
2742
2743
2744
2745 read_lock(&journal->j_state_lock);
2746 commit_trans = journal->j_committing_transaction;
2747 read_unlock(&journal->j_state_lock);
2748 spin_lock(&journal->j_list_lock);
2749 inode_trans = jinode->i_transaction;
2750 spin_unlock(&journal->j_list_lock);
2751 if (inode_trans == commit_trans) {
2752 ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping,
2753 new_size, LLONG_MAX);
2754 if (ret)
2755 jbd2_journal_abort(journal, ret);
2756 }
2757out:
2758 return ret;
2759}
2760