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