1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/time.h>
21#include <linux/fs.h>
22#include <linux/jbd.h>
23#include <linux/errno.h>
24#include <linux/slab.h>
25#include <linux/timer.h>
26#include <linux/mm.h>
27#include <linux/highmem.h>
28#include <linux/hrtimer.h>
29#include <linux/backing-dev.h>
30
31static void __journal_temp_unlink_buffer(struct journal_head *jh);
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49static transaction_t *
50get_transaction(journal_t *journal, transaction_t *transaction)
51{
52 transaction->t_journal = journal;
53 transaction->t_state = T_RUNNING;
54 transaction->t_start_time = ktime_get();
55 transaction->t_tid = journal->j_transaction_sequence++;
56 transaction->t_expires = jiffies + journal->j_commit_interval;
57 spin_lock_init(&transaction->t_handle_lock);
58
59
60 journal->j_commit_timer.expires =
61 round_jiffies_up(transaction->t_expires);
62 add_timer(&journal->j_commit_timer);
63
64 J_ASSERT(journal->j_running_transaction == NULL);
65 journal->j_running_transaction = transaction;
66
67 return transaction;
68}
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85static int start_this_handle(journal_t *journal, handle_t *handle)
86{
87 transaction_t *transaction;
88 int needed;
89 int nblocks = handle->h_buffer_credits;
90 transaction_t *new_transaction = NULL;
91 int ret = 0;
92
93 if (nblocks > journal->j_max_transaction_buffers) {
94 printk(KERN_ERR "JBD: %s wants too many credits (%d > %d)\n",
95 current->comm, nblocks,
96 journal->j_max_transaction_buffers);
97 ret = -ENOSPC;
98 goto out;
99 }
100
101alloc_transaction:
102 if (!journal->j_running_transaction) {
103 new_transaction = kzalloc(sizeof(*new_transaction), GFP_NOFS);
104 if (!new_transaction) {
105 congestion_wait(BLK_RW_ASYNC, HZ/50);
106 goto alloc_transaction;
107 }
108 }
109
110 jbd_debug(3, "New handle %p going live.\n", handle);
111
112repeat:
113
114
115
116
117
118 spin_lock(&journal->j_state_lock);
119repeat_locked:
120 if (is_journal_aborted(journal) ||
121 (journal->j_errno != 0 && !(journal->j_flags & JFS_ACK_ERR))) {
122 spin_unlock(&journal->j_state_lock);
123 ret = -EROFS;
124 goto out;
125 }
126
127
128 if (journal->j_barrier_count) {
129 spin_unlock(&journal->j_state_lock);
130 wait_event(journal->j_wait_transaction_locked,
131 journal->j_barrier_count == 0);
132 goto repeat;
133 }
134
135 if (!journal->j_running_transaction) {
136 if (!new_transaction) {
137 spin_unlock(&journal->j_state_lock);
138 goto alloc_transaction;
139 }
140 get_transaction(journal, new_transaction);
141 new_transaction = NULL;
142 }
143
144 transaction = journal->j_running_transaction;
145
146
147
148
149
150 if (transaction->t_state == T_LOCKED) {
151 DEFINE_WAIT(wait);
152
153 prepare_to_wait(&journal->j_wait_transaction_locked,
154 &wait, TASK_UNINTERRUPTIBLE);
155 spin_unlock(&journal->j_state_lock);
156 schedule();
157 finish_wait(&journal->j_wait_transaction_locked, &wait);
158 goto repeat;
159 }
160
161
162
163
164
165
166 spin_lock(&transaction->t_handle_lock);
167 needed = transaction->t_outstanding_credits + nblocks;
168
169 if (needed > journal->j_max_transaction_buffers) {
170
171
172
173
174
175 DEFINE_WAIT(wait);
176
177 jbd_debug(2, "Handle %p starting new commit...\n", handle);
178 spin_unlock(&transaction->t_handle_lock);
179 prepare_to_wait(&journal->j_wait_transaction_locked, &wait,
180 TASK_UNINTERRUPTIBLE);
181 __log_start_commit(journal, transaction->t_tid);
182 spin_unlock(&journal->j_state_lock);
183 schedule();
184 finish_wait(&journal->j_wait_transaction_locked, &wait);
185 goto repeat;
186 }
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213 if (__log_space_left(journal) < jbd_space_needed(journal)) {
214 jbd_debug(2, "Handle %p waiting for checkpoint...\n", handle);
215 spin_unlock(&transaction->t_handle_lock);
216 __log_wait_for_space(journal);
217 goto repeat_locked;
218 }
219
220
221
222
223 handle->h_transaction = transaction;
224 transaction->t_outstanding_credits += nblocks;
225 transaction->t_updates++;
226 transaction->t_handle_count++;
227 jbd_debug(4, "Handle %p given %d credits (total %d, free %d)\n",
228 handle, nblocks, transaction->t_outstanding_credits,
229 __log_space_left(journal));
230 spin_unlock(&transaction->t_handle_lock);
231 spin_unlock(&journal->j_state_lock);
232
233 lock_map_acquire(&handle->h_lockdep_map);
234out:
235 if (unlikely(new_transaction))
236 kfree(new_transaction);
237 return ret;
238}
239
240static struct lock_class_key jbd_handle_key;
241
242
243static handle_t *new_handle(int nblocks)
244{
245 handle_t *handle = jbd_alloc_handle(GFP_NOFS);
246 if (!handle)
247 return NULL;
248 memset(handle, 0, sizeof(*handle));
249 handle->h_buffer_credits = nblocks;
250 handle->h_ref = 1;
251
252 lockdep_init_map(&handle->h_lockdep_map, "jbd_handle", &jbd_handle_key, 0);
253
254 return handle;
255}
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272handle_t *journal_start(journal_t *journal, int nblocks)
273{
274 handle_t *handle = journal_current_handle();
275 int err;
276
277 if (!journal)
278 return ERR_PTR(-EROFS);
279
280 if (handle) {
281 J_ASSERT(handle->h_transaction->t_journal == journal);
282 handle->h_ref++;
283 return handle;
284 }
285
286 handle = new_handle(nblocks);
287 if (!handle)
288 return ERR_PTR(-ENOMEM);
289
290 current->journal_info = handle;
291
292 err = start_this_handle(journal, handle);
293 if (err < 0) {
294 jbd_free_handle(handle);
295 current->journal_info = NULL;
296 handle = ERR_PTR(err);
297 }
298 return handle;
299}
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321int journal_extend(handle_t *handle, int nblocks)
322{
323 transaction_t *transaction = handle->h_transaction;
324 journal_t *journal = transaction->t_journal;
325 int result;
326 int wanted;
327
328 result = -EIO;
329 if (is_handle_aborted(handle))
330 goto out;
331
332 result = 1;
333
334 spin_lock(&journal->j_state_lock);
335
336
337 if (handle->h_transaction->t_state != T_RUNNING) {
338 jbd_debug(3, "denied handle %p %d blocks: "
339 "transaction not running\n", handle, nblocks);
340 goto error_out;
341 }
342
343 spin_lock(&transaction->t_handle_lock);
344 wanted = transaction->t_outstanding_credits + nblocks;
345
346 if (wanted > journal->j_max_transaction_buffers) {
347 jbd_debug(3, "denied handle %p %d blocks: "
348 "transaction too large\n", handle, nblocks);
349 goto unlock;
350 }
351
352 if (wanted > __log_space_left(journal)) {
353 jbd_debug(3, "denied handle %p %d blocks: "
354 "insufficient log space\n", handle, nblocks);
355 goto unlock;
356 }
357
358 handle->h_buffer_credits += nblocks;
359 transaction->t_outstanding_credits += nblocks;
360 result = 0;
361
362 jbd_debug(3, "extended handle %p by %d\n", handle, nblocks);
363unlock:
364 spin_unlock(&transaction->t_handle_lock);
365error_out:
366 spin_unlock(&journal->j_state_lock);
367out:
368 return result;
369}
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387int journal_restart(handle_t *handle, int nblocks)
388{
389 transaction_t *transaction = handle->h_transaction;
390 journal_t *journal = transaction->t_journal;
391 int ret;
392
393
394
395 if (is_handle_aborted(handle))
396 return 0;
397
398
399
400
401
402 J_ASSERT(transaction->t_updates > 0);
403 J_ASSERT(journal_current_handle() == handle);
404
405 spin_lock(&journal->j_state_lock);
406 spin_lock(&transaction->t_handle_lock);
407 transaction->t_outstanding_credits -= handle->h_buffer_credits;
408 transaction->t_updates--;
409
410 if (!transaction->t_updates)
411 wake_up(&journal->j_wait_updates);
412 spin_unlock(&transaction->t_handle_lock);
413
414 jbd_debug(2, "restarting handle %p\n", handle);
415 __log_start_commit(journal, transaction->t_tid);
416 spin_unlock(&journal->j_state_lock);
417
418 lock_map_release(&handle->h_lockdep_map);
419 handle->h_buffer_credits = nblocks;
420 ret = start_this_handle(journal, handle);
421 return ret;
422}
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439void journal_lock_updates(journal_t *journal)
440{
441 DEFINE_WAIT(wait);
442
443wait:
444
445 wait_event(journal->j_wait_transaction_locked,
446 journal->j_barrier_count == 0);
447
448 spin_lock(&journal->j_state_lock);
449
450
451
452
453 if (journal->j_barrier_count > 0) {
454 spin_unlock(&journal->j_state_lock);
455 goto wait;
456 }
457 ++journal->j_barrier_count;
458
459
460 while (1) {
461 transaction_t *transaction = journal->j_running_transaction;
462
463 if (!transaction)
464 break;
465
466 spin_lock(&transaction->t_handle_lock);
467 if (!transaction->t_updates) {
468 spin_unlock(&transaction->t_handle_lock);
469 break;
470 }
471 prepare_to_wait(&journal->j_wait_updates, &wait,
472 TASK_UNINTERRUPTIBLE);
473 spin_unlock(&transaction->t_handle_lock);
474 spin_unlock(&journal->j_state_lock);
475 schedule();
476 finish_wait(&journal->j_wait_updates, &wait);
477 spin_lock(&journal->j_state_lock);
478 }
479 spin_unlock(&journal->j_state_lock);
480}
481
482
483
484
485
486
487
488void journal_unlock_updates (journal_t *journal)
489{
490 J_ASSERT(journal->j_barrier_count != 0);
491
492 spin_lock(&journal->j_state_lock);
493 --journal->j_barrier_count;
494 spin_unlock(&journal->j_state_lock);
495 wake_up(&journal->j_wait_transaction_locked);
496}
497
498static void warn_dirty_buffer(struct buffer_head *bh)
499{
500 char b[BDEVNAME_SIZE];
501
502 printk(KERN_WARNING
503 "JBD: Spotted dirty metadata buffer (dev = %s, blocknr = %llu). "
504 "There's a risk of filesystem corruption in case of system "
505 "crash.\n",
506 bdevname(bh->b_bdev, b), (unsigned long long)bh->b_blocknr);
507}
508
509
510
511
512
513
514
515
516
517
518
519static int
520do_get_write_access(handle_t *handle, struct journal_head *jh,
521 int force_copy)
522{
523 struct buffer_head *bh;
524 transaction_t *transaction;
525 journal_t *journal;
526 int error;
527 char *frozen_buffer = NULL;
528 int need_copy = 0;
529
530 if (is_handle_aborted(handle))
531 return -EROFS;
532
533 transaction = handle->h_transaction;
534 journal = transaction->t_journal;
535
536 jbd_debug(5, "journal_head %p, force_copy %d\n", jh, force_copy);
537
538 JBUFFER_TRACE(jh, "entry");
539repeat:
540 bh = jh2bh(jh);
541
542
543
544 lock_buffer(bh);
545 jbd_lock_bh_state(bh);
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560 if (buffer_dirty(bh)) {
561
562
563
564
565 if (jh->b_transaction) {
566 J_ASSERT_JH(jh,
567 jh->b_transaction == transaction ||
568 jh->b_transaction ==
569 journal->j_committing_transaction);
570 if (jh->b_next_transaction)
571 J_ASSERT_JH(jh, jh->b_next_transaction ==
572 transaction);
573 warn_dirty_buffer(bh);
574 }
575
576
577
578
579
580 JBUFFER_TRACE(jh, "Journalling dirty buffer");
581 clear_buffer_dirty(bh);
582 set_buffer_jbddirty(bh);
583 }
584
585 unlock_buffer(bh);
586
587 error = -EROFS;
588 if (is_handle_aborted(handle)) {
589 jbd_unlock_bh_state(bh);
590 goto out;
591 }
592 error = 0;
593
594
595
596
597
598 if (jh->b_transaction == transaction ||
599 jh->b_next_transaction == transaction)
600 goto done;
601
602
603
604
605
606 jh->b_modified = 0;
607
608
609
610
611
612 if (jh->b_frozen_data) {
613 JBUFFER_TRACE(jh, "has frozen data");
614 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
615 jh->b_next_transaction = transaction;
616 goto done;
617 }
618
619
620
621 if (jh->b_transaction && jh->b_transaction != transaction) {
622 JBUFFER_TRACE(jh, "owned by older transaction");
623 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
624 J_ASSERT_JH(jh, jh->b_transaction ==
625 journal->j_committing_transaction);
626
627
628
629
630
631
632
633
634
635
636 if (jh->b_jlist == BJ_Shadow) {
637 DEFINE_WAIT_BIT(wait, &bh->b_state, BH_Unshadow);
638 wait_queue_head_t *wqh;
639
640 wqh = bit_waitqueue(&bh->b_state, BH_Unshadow);
641
642 JBUFFER_TRACE(jh, "on shadow: sleep");
643 jbd_unlock_bh_state(bh);
644
645 for ( ; ; ) {
646 prepare_to_wait(wqh, &wait.wait,
647 TASK_UNINTERRUPTIBLE);
648 if (jh->b_jlist != BJ_Shadow)
649 break;
650 schedule();
651 }
652 finish_wait(wqh, &wait.wait);
653 goto repeat;
654 }
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670 if (jh->b_jlist != BJ_Forget || force_copy) {
671 JBUFFER_TRACE(jh, "generate frozen data");
672 if (!frozen_buffer) {
673 JBUFFER_TRACE(jh, "allocate memory for buffer");
674 jbd_unlock_bh_state(bh);
675 frozen_buffer =
676 jbd_alloc(jh2bh(jh)->b_size,
677 GFP_NOFS);
678 if (!frozen_buffer) {
679 printk(KERN_EMERG
680 "%s: OOM for frozen_buffer\n",
681 __func__);
682 JBUFFER_TRACE(jh, "oom!");
683 error = -ENOMEM;
684 jbd_lock_bh_state(bh);
685 goto done;
686 }
687 goto repeat;
688 }
689 jh->b_frozen_data = frozen_buffer;
690 frozen_buffer = NULL;
691 need_copy = 1;
692 }
693 jh->b_next_transaction = transaction;
694 }
695
696
697
698
699
700
701
702 if (!jh->b_transaction) {
703 JBUFFER_TRACE(jh, "no transaction");
704 J_ASSERT_JH(jh, !jh->b_next_transaction);
705 JBUFFER_TRACE(jh, "file as BJ_Reserved");
706 spin_lock(&journal->j_list_lock);
707 __journal_file_buffer(jh, transaction, BJ_Reserved);
708 spin_unlock(&journal->j_list_lock);
709 }
710
711done:
712 if (need_copy) {
713 struct page *page;
714 int offset;
715 char *source;
716
717 J_EXPECT_JH(jh, buffer_uptodate(jh2bh(jh)),
718 "Possible IO failure.\n");
719 page = jh2bh(jh)->b_page;
720 offset = offset_in_page(jh2bh(jh)->b_data);
721 source = kmap_atomic(page);
722 memcpy(jh->b_frozen_data, source+offset, jh2bh(jh)->b_size);
723 kunmap_atomic(source);
724 }
725 jbd_unlock_bh_state(bh);
726
727
728
729
730
731 journal_cancel_revoke(handle, jh);
732
733out:
734 if (unlikely(frozen_buffer))
735 jbd_free(frozen_buffer, bh->b_size);
736
737 JBUFFER_TRACE(jh, "exit");
738 return error;
739}
740
741
742
743
744
745
746
747
748
749
750
751
752int journal_get_write_access(handle_t *handle, struct buffer_head *bh)
753{
754 struct journal_head *jh = journal_add_journal_head(bh);
755 int rc;
756
757
758
759
760 rc = do_get_write_access(handle, jh, 0);
761 journal_put_journal_head(jh);
762 return rc;
763}
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785int journal_get_create_access(handle_t *handle, struct buffer_head *bh)
786{
787 transaction_t *transaction = handle->h_transaction;
788 journal_t *journal = transaction->t_journal;
789 struct journal_head *jh = journal_add_journal_head(bh);
790 int err;
791
792 jbd_debug(5, "journal_head %p\n", jh);
793 err = -EROFS;
794 if (is_handle_aborted(handle))
795 goto out;
796 err = 0;
797
798 JBUFFER_TRACE(jh, "entry");
799
800
801
802
803
804
805
806 jbd_lock_bh_state(bh);
807 spin_lock(&journal->j_list_lock);
808 J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
809 jh->b_transaction == NULL ||
810 (jh->b_transaction == journal->j_committing_transaction &&
811 jh->b_jlist == BJ_Forget)));
812
813 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
814 J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
815
816 if (jh->b_transaction == NULL) {
817
818
819
820
821
822
823
824
825 clear_buffer_dirty(jh2bh(jh));
826
827
828 jh->b_modified = 0;
829
830 JBUFFER_TRACE(jh, "file as BJ_Reserved");
831 __journal_file_buffer(jh, transaction, BJ_Reserved);
832 } else if (jh->b_transaction == journal->j_committing_transaction) {
833
834 jh->b_modified = 0;
835
836 JBUFFER_TRACE(jh, "set next transaction");
837 jh->b_next_transaction = transaction;
838 }
839 spin_unlock(&journal->j_list_lock);
840 jbd_unlock_bh_state(bh);
841
842
843
844
845
846
847
848
849 JBUFFER_TRACE(jh, "cancelling revoke");
850 journal_cancel_revoke(handle, jh);
851out:
852 journal_put_journal_head(jh);
853 return err;
854}
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881int journal_get_undo_access(handle_t *handle, struct buffer_head *bh)
882{
883 int err;
884 struct journal_head *jh = journal_add_journal_head(bh);
885 char *committed_data = NULL;
886
887 JBUFFER_TRACE(jh, "entry");
888
889
890
891
892
893
894 err = do_get_write_access(handle, jh, 1);
895 if (err)
896 goto out;
897
898repeat:
899 if (!jh->b_committed_data) {
900 committed_data = jbd_alloc(jh2bh(jh)->b_size, GFP_NOFS);
901 if (!committed_data) {
902 printk(KERN_EMERG "%s: No memory for committed data\n",
903 __func__);
904 err = -ENOMEM;
905 goto out;
906 }
907 }
908
909 jbd_lock_bh_state(bh);
910 if (!jh->b_committed_data) {
911
912
913 JBUFFER_TRACE(jh, "generate b_committed data");
914 if (!committed_data) {
915 jbd_unlock_bh_state(bh);
916 goto repeat;
917 }
918
919 jh->b_committed_data = committed_data;
920 committed_data = NULL;
921 memcpy(jh->b_committed_data, bh->b_data, bh->b_size);
922 }
923 jbd_unlock_bh_state(bh);
924out:
925 journal_put_journal_head(jh);
926 if (unlikely(committed_data))
927 jbd_free(committed_data, bh->b_size);
928 return err;
929}
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948int journal_dirty_data(handle_t *handle, struct buffer_head *bh)
949{
950 journal_t *journal = handle->h_transaction->t_journal;
951 int need_brelse = 0;
952 struct journal_head *jh;
953 int ret = 0;
954
955 if (is_handle_aborted(handle))
956 return ret;
957
958 jh = journal_add_journal_head(bh);
959 JBUFFER_TRACE(jh, "entry");
960
961
962
963
964
965 jbd_debug(4, "jh: %p, tid:%d\n", jh, handle->h_transaction->t_tid);
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988 jbd_lock_bh_state(bh);
989 spin_lock(&journal->j_list_lock);
990
991
992 if (!buffer_mapped(bh)) {
993 JBUFFER_TRACE(jh, "unmapped buffer, bailing out");
994 goto no_journal;
995 }
996
997 if (jh->b_transaction) {
998 JBUFFER_TRACE(jh, "has transaction");
999 if (jh->b_transaction != handle->h_transaction) {
1000 JBUFFER_TRACE(jh, "belongs to older transaction");
1001 J_ASSERT_JH(jh, jh->b_transaction ==
1002 journal->j_committing_transaction);
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037 if (jh->b_jlist != BJ_None &&
1038 jh->b_jlist != BJ_SyncData &&
1039 jh->b_jlist != BJ_Locked) {
1040 JBUFFER_TRACE(jh, "Not stealing");
1041 goto no_journal;
1042 }
1043
1044
1045
1046
1047
1048
1049
1050 if (buffer_dirty(bh)) {
1051 get_bh(bh);
1052 spin_unlock(&journal->j_list_lock);
1053 jbd_unlock_bh_state(bh);
1054 need_brelse = 1;
1055 sync_dirty_buffer(bh);
1056 jbd_lock_bh_state(bh);
1057 spin_lock(&journal->j_list_lock);
1058
1059 if (!buffer_mapped(bh)) {
1060 JBUFFER_TRACE(jh, "buffer got unmapped");
1061 goto no_journal;
1062 }
1063
1064
1065 }
1066
1067
1068
1069
1070
1071
1072 if (unlikely(!buffer_uptodate(bh))) {
1073 ret = -EIO;
1074 goto no_journal;
1075 }
1076
1077 if (jh->b_transaction != NULL &&
1078 jh->b_transaction != handle->h_transaction) {
1079 JBUFFER_TRACE(jh, "unfile from commit");
1080 __journal_temp_unlink_buffer(jh);
1081
1082
1083
1084
1085 jh->b_transaction = handle->h_transaction;
1086 }
1087
1088
1089 }
1090
1091
1092
1093
1094
1095
1096 if (jh->b_jlist != BJ_SyncData && jh->b_jlist != BJ_Locked) {
1097 JBUFFER_TRACE(jh, "not on correct data list: unfile");
1098 J_ASSERT_JH(jh, jh->b_jlist != BJ_Shadow);
1099 JBUFFER_TRACE(jh, "file as data");
1100 __journal_file_buffer(jh, handle->h_transaction,
1101 BJ_SyncData);
1102 }
1103 } else {
1104 JBUFFER_TRACE(jh, "not on a transaction");
1105 __journal_file_buffer(jh, handle->h_transaction, BJ_SyncData);
1106 }
1107no_journal:
1108 spin_unlock(&journal->j_list_lock);
1109 jbd_unlock_bh_state(bh);
1110 if (need_brelse) {
1111 BUFFER_TRACE(bh, "brelse");
1112 __brelse(bh);
1113 }
1114 JBUFFER_TRACE(jh, "exit");
1115 journal_put_journal_head(jh);
1116 return ret;
1117}
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138int journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
1139{
1140 transaction_t *transaction = handle->h_transaction;
1141 journal_t *journal = transaction->t_journal;
1142 struct journal_head *jh = bh2jh(bh);
1143
1144 jbd_debug(5, "journal_head %p\n", jh);
1145 JBUFFER_TRACE(jh, "entry");
1146 if (is_handle_aborted(handle))
1147 goto out;
1148
1149 jbd_lock_bh_state(bh);
1150
1151 if (jh->b_modified == 0) {
1152
1153
1154
1155
1156
1157 jh->b_modified = 1;
1158 J_ASSERT_JH(jh, handle->h_buffer_credits > 0);
1159 handle->h_buffer_credits--;
1160 }
1161
1162
1163
1164
1165
1166
1167
1168
1169 if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) {
1170 JBUFFER_TRACE(jh, "fastpath");
1171 J_ASSERT_JH(jh, jh->b_transaction ==
1172 journal->j_running_transaction);
1173 goto out_unlock_bh;
1174 }
1175
1176 set_buffer_jbddirty(bh);
1177
1178
1179
1180
1181
1182
1183
1184 if (jh->b_transaction != transaction) {
1185 JBUFFER_TRACE(jh, "already on other transaction");
1186 J_ASSERT_JH(jh, jh->b_transaction ==
1187 journal->j_committing_transaction);
1188 J_ASSERT_JH(jh, jh->b_next_transaction == transaction);
1189
1190
1191 goto out_unlock_bh;
1192 }
1193
1194
1195 J_ASSERT_JH(jh, jh->b_frozen_data == NULL);
1196
1197 JBUFFER_TRACE(jh, "file as BJ_Metadata");
1198 spin_lock(&journal->j_list_lock);
1199 __journal_file_buffer(jh, handle->h_transaction, BJ_Metadata);
1200 spin_unlock(&journal->j_list_lock);
1201out_unlock_bh:
1202 jbd_unlock_bh_state(bh);
1203out:
1204 JBUFFER_TRACE(jh, "exit");
1205 return 0;
1206}
1207
1208
1209
1210
1211
1212
1213void
1214journal_release_buffer(handle_t *handle, struct buffer_head *bh)
1215{
1216 BUFFER_TRACE(bh, "entry");
1217}
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236int journal_forget (handle_t *handle, struct buffer_head *bh)
1237{
1238 transaction_t *transaction = handle->h_transaction;
1239 journal_t *journal = transaction->t_journal;
1240 struct journal_head *jh;
1241 int drop_reserve = 0;
1242 int err = 0;
1243 int was_modified = 0;
1244
1245 BUFFER_TRACE(bh, "entry");
1246
1247 jbd_lock_bh_state(bh);
1248 spin_lock(&journal->j_list_lock);
1249
1250 if (!buffer_jbd(bh))
1251 goto not_jbd;
1252 jh = bh2jh(bh);
1253
1254
1255
1256 if (!J_EXPECT_JH(jh, !jh->b_committed_data,
1257 "inconsistent data on disk")) {
1258 err = -EIO;
1259 goto not_jbd;
1260 }
1261
1262
1263 was_modified = jh->b_modified;
1264
1265
1266
1267
1268
1269 jh->b_modified = 0;
1270
1271 if (jh->b_transaction == handle->h_transaction) {
1272 J_ASSERT_JH(jh, !jh->b_frozen_data);
1273
1274
1275
1276
1277 clear_buffer_dirty(bh);
1278 clear_buffer_jbddirty(bh);
1279
1280 JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
1281
1282
1283
1284
1285
1286 if (was_modified)
1287 drop_reserve = 1;
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301 if (jh->b_cp_transaction) {
1302 __journal_temp_unlink_buffer(jh);
1303 __journal_file_buffer(jh, transaction, BJ_Forget);
1304 } else {
1305 __journal_unfile_buffer(jh);
1306 if (!buffer_jbd(bh)) {
1307 spin_unlock(&journal->j_list_lock);
1308 jbd_unlock_bh_state(bh);
1309 __bforget(bh);
1310 goto drop;
1311 }
1312 }
1313 } else if (jh->b_transaction) {
1314 J_ASSERT_JH(jh, (jh->b_transaction ==
1315 journal->j_committing_transaction));
1316
1317
1318 JBUFFER_TRACE(jh, "belongs to older transaction");
1319
1320
1321
1322 if (jh->b_next_transaction) {
1323 J_ASSERT(jh->b_next_transaction == transaction);
1324 jh->b_next_transaction = NULL;
1325
1326
1327
1328
1329
1330 if (was_modified)
1331 drop_reserve = 1;
1332 }
1333 }
1334
1335not_jbd:
1336 spin_unlock(&journal->j_list_lock);
1337 jbd_unlock_bh_state(bh);
1338 __brelse(bh);
1339drop:
1340 if (drop_reserve) {
1341
1342 handle->h_buffer_credits++;
1343 }
1344 return err;
1345}
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363int journal_stop(handle_t *handle)
1364{
1365 transaction_t *transaction = handle->h_transaction;
1366 journal_t *journal = transaction->t_journal;
1367 int err;
1368 pid_t pid;
1369
1370 J_ASSERT(journal_current_handle() == handle);
1371
1372 if (is_handle_aborted(handle))
1373 err = -EIO;
1374 else {
1375 J_ASSERT(transaction->t_updates > 0);
1376 err = 0;
1377 }
1378
1379 if (--handle->h_ref > 0) {
1380 jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
1381 handle->h_ref);
1382 return err;
1383 }
1384
1385 jbd_debug(4, "Handle %p going down\n", handle);
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412 pid = current->pid;
1413 if (handle->h_sync && journal->j_last_sync_writer != pid) {
1414 u64 commit_time, trans_time;
1415
1416 journal->j_last_sync_writer = pid;
1417
1418 spin_lock(&journal->j_state_lock);
1419 commit_time = journal->j_average_commit_time;
1420 spin_unlock(&journal->j_state_lock);
1421
1422 trans_time = ktime_to_ns(ktime_sub(ktime_get(),
1423 transaction->t_start_time));
1424
1425 commit_time = min_t(u64, commit_time,
1426 1000*jiffies_to_usecs(1));
1427
1428 if (trans_time < commit_time) {
1429 ktime_t expires = ktime_add_ns(ktime_get(),
1430 commit_time);
1431 set_current_state(TASK_UNINTERRUPTIBLE);
1432 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1433 }
1434 }
1435
1436 current->journal_info = NULL;
1437 spin_lock(&journal->j_state_lock);
1438 spin_lock(&transaction->t_handle_lock);
1439 transaction->t_outstanding_credits -= handle->h_buffer_credits;
1440 transaction->t_updates--;
1441 if (!transaction->t_updates) {
1442 wake_up(&journal->j_wait_updates);
1443 if (journal->j_barrier_count)
1444 wake_up(&journal->j_wait_transaction_locked);
1445 }
1446
1447
1448
1449
1450
1451
1452
1453 if (handle->h_sync ||
1454 transaction->t_outstanding_credits >
1455 journal->j_max_transaction_buffers ||
1456 time_after_eq(jiffies, transaction->t_expires)) {
1457
1458
1459
1460 tid_t tid = transaction->t_tid;
1461
1462 spin_unlock(&transaction->t_handle_lock);
1463 jbd_debug(2, "transaction too old, requesting commit for "
1464 "handle %p\n", handle);
1465
1466 __log_start_commit(journal, transaction->t_tid);
1467 spin_unlock(&journal->j_state_lock);
1468
1469
1470
1471
1472
1473 if (handle->h_sync && !(current->flags & PF_MEMALLOC))
1474 err = log_wait_commit(journal, tid);
1475 } else {
1476 spin_unlock(&transaction->t_handle_lock);
1477 spin_unlock(&journal->j_state_lock);
1478 }
1479
1480 lock_map_release(&handle->h_lockdep_map);
1481
1482 jbd_free_handle(handle);
1483 return err;
1484}
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494int journal_force_commit(journal_t *journal)
1495{
1496 handle_t *handle;
1497 int ret;
1498
1499 handle = journal_start(journal, 1);
1500 if (IS_ERR(handle)) {
1501 ret = PTR_ERR(handle);
1502 } else {
1503 handle->h_sync = 1;
1504 ret = journal_stop(handle);
1505 }
1506 return ret;
1507}
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525static inline void
1526__blist_add_buffer(struct journal_head **list, struct journal_head *jh)
1527{
1528 if (!*list) {
1529 jh->b_tnext = jh->b_tprev = jh;
1530 *list = jh;
1531 } else {
1532
1533 struct journal_head *first = *list, *last = first->b_tprev;
1534 jh->b_tprev = last;
1535 jh->b_tnext = first;
1536 last->b_tnext = first->b_tprev = jh;
1537 }
1538}
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549static inline void
1550__blist_del_buffer(struct journal_head **list, struct journal_head *jh)
1551{
1552 if (*list == jh) {
1553 *list = jh->b_tnext;
1554 if (*list == jh)
1555 *list = NULL;
1556 }
1557 jh->b_tprev->b_tnext = jh->b_tnext;
1558 jh->b_tnext->b_tprev = jh->b_tprev;
1559}
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572static void __journal_temp_unlink_buffer(struct journal_head *jh)
1573{
1574 struct journal_head **list = NULL;
1575 transaction_t *transaction;
1576 struct buffer_head *bh = jh2bh(jh);
1577
1578 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1579 transaction = jh->b_transaction;
1580 if (transaction)
1581 assert_spin_locked(&transaction->t_journal->j_list_lock);
1582
1583 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1584 if (jh->b_jlist != BJ_None)
1585 J_ASSERT_JH(jh, transaction != NULL);
1586
1587 switch (jh->b_jlist) {
1588 case BJ_None:
1589 return;
1590 case BJ_SyncData:
1591 list = &transaction->t_sync_datalist;
1592 break;
1593 case BJ_Metadata:
1594 transaction->t_nr_buffers--;
1595 J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
1596 list = &transaction->t_buffers;
1597 break;
1598 case BJ_Forget:
1599 list = &transaction->t_forget;
1600 break;
1601 case BJ_IO:
1602 list = &transaction->t_iobuf_list;
1603 break;
1604 case BJ_Shadow:
1605 list = &transaction->t_shadow_list;
1606 break;
1607 case BJ_LogCtl:
1608 list = &transaction->t_log_list;
1609 break;
1610 case BJ_Reserved:
1611 list = &transaction->t_reserved_list;
1612 break;
1613 case BJ_Locked:
1614 list = &transaction->t_locked_list;
1615 break;
1616 }
1617
1618 __blist_del_buffer(list, jh);
1619 jh->b_jlist = BJ_None;
1620 if (test_clear_buffer_jbddirty(bh))
1621 mark_buffer_dirty(bh);
1622}
1623
1624
1625
1626
1627
1628
1629
1630
1631void __journal_unfile_buffer(struct journal_head *jh)
1632{
1633 __journal_temp_unlink_buffer(jh);
1634 jh->b_transaction = NULL;
1635 journal_put_journal_head(jh);
1636}
1637
1638void journal_unfile_buffer(journal_t *journal, struct journal_head *jh)
1639{
1640 struct buffer_head *bh = jh2bh(jh);
1641
1642
1643 get_bh(bh);
1644 jbd_lock_bh_state(bh);
1645 spin_lock(&journal->j_list_lock);
1646 __journal_unfile_buffer(jh);
1647 spin_unlock(&journal->j_list_lock);
1648 jbd_unlock_bh_state(bh);
1649 __brelse(bh);
1650}
1651
1652
1653
1654
1655
1656
1657static void
1658__journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh)
1659{
1660 struct journal_head *jh;
1661
1662 jh = bh2jh(bh);
1663
1664 if (buffer_locked(bh) || buffer_dirty(bh))
1665 goto out;
1666
1667 if (jh->b_next_transaction != NULL)
1668 goto out;
1669
1670 spin_lock(&journal->j_list_lock);
1671 if (jh->b_transaction != NULL && jh->b_cp_transaction == NULL) {
1672 if (jh->b_jlist == BJ_SyncData || jh->b_jlist == BJ_Locked) {
1673
1674 JBUFFER_TRACE(jh, "release data");
1675 __journal_unfile_buffer(jh);
1676 }
1677 } else if (jh->b_cp_transaction != NULL && jh->b_transaction == NULL) {
1678
1679 if (jh->b_jlist == BJ_None) {
1680 JBUFFER_TRACE(jh, "remove from checkpoint list");
1681 __journal_remove_checkpoint(jh);
1682 }
1683 }
1684 spin_unlock(&journal->j_list_lock);
1685out:
1686 return;
1687}
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727int journal_try_to_free_buffers(journal_t *journal,
1728 struct page *page, gfp_t gfp_mask)
1729{
1730 struct buffer_head *head;
1731 struct buffer_head *bh;
1732 int ret = 0;
1733
1734 J_ASSERT(PageLocked(page));
1735
1736 head = page_buffers(page);
1737 bh = head;
1738 do {
1739 struct journal_head *jh;
1740
1741
1742
1743
1744
1745
1746 jh = journal_grab_journal_head(bh);
1747 if (!jh)
1748 continue;
1749
1750 jbd_lock_bh_state(bh);
1751 __journal_try_to_free_buffer(journal, bh);
1752 journal_put_journal_head(jh);
1753 jbd_unlock_bh_state(bh);
1754 if (buffer_jbd(bh))
1755 goto busy;
1756 } while ((bh = bh->b_this_page) != head);
1757
1758 ret = try_to_free_buffers(page);
1759
1760busy:
1761 return ret;
1762}
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
1777{
1778 int may_free = 1;
1779 struct buffer_head *bh = jh2bh(jh);
1780
1781 if (jh->b_cp_transaction) {
1782 JBUFFER_TRACE(jh, "on running+cp transaction");
1783 __journal_temp_unlink_buffer(jh);
1784
1785
1786
1787
1788
1789 clear_buffer_dirty(bh);
1790 __journal_file_buffer(jh, transaction, BJ_Forget);
1791 may_free = 0;
1792 } else {
1793 JBUFFER_TRACE(jh, "on running transaction");
1794 __journal_unfile_buffer(jh);
1795 }
1796 return may_free;
1797}
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
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
1846static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh)
1847{
1848 transaction_t *transaction;
1849 struct journal_head *jh;
1850 int may_free = 1;
1851 int ret;
1852
1853 BUFFER_TRACE(bh, "entry");
1854
1855
1856
1857
1858
1859
1860
1861 if (!buffer_jbd(bh))
1862 goto zap_buffer_unlocked;
1863
1864 spin_lock(&journal->j_state_lock);
1865 jbd_lock_bh_state(bh);
1866 spin_lock(&journal->j_list_lock);
1867
1868 jh = journal_grab_journal_head(bh);
1869 if (!jh)
1870 goto zap_buffer_no_jh;
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887 transaction = jh->b_transaction;
1888 if (transaction == NULL) {
1889
1890
1891
1892
1893 if (!jh->b_cp_transaction) {
1894 JBUFFER_TRACE(jh, "not on any transaction: zap");
1895 goto zap_buffer;
1896 }
1897
1898 if (!buffer_dirty(bh)) {
1899
1900 goto zap_buffer;
1901 }
1902
1903
1904
1905
1906
1907 if (journal->j_running_transaction) {
1908
1909
1910
1911 JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
1912 ret = __dispose_buffer(jh,
1913 journal->j_running_transaction);
1914 journal_put_journal_head(jh);
1915 spin_unlock(&journal->j_list_lock);
1916 jbd_unlock_bh_state(bh);
1917 spin_unlock(&journal->j_state_lock);
1918 return ret;
1919 } else {
1920
1921
1922
1923
1924 if (journal->j_committing_transaction) {
1925 JBUFFER_TRACE(jh, "give to committing trans");
1926 ret = __dispose_buffer(jh,
1927 journal->j_committing_transaction);
1928 journal_put_journal_head(jh);
1929 spin_unlock(&journal->j_list_lock);
1930 jbd_unlock_bh_state(bh);
1931 spin_unlock(&journal->j_state_lock);
1932 return ret;
1933 } else {
1934
1935
1936 clear_buffer_jbddirty(bh);
1937 goto zap_buffer;
1938 }
1939 }
1940 } else if (transaction == journal->j_committing_transaction) {
1941 JBUFFER_TRACE(jh, "on committing transaction");
1942 if (jh->b_jlist == BJ_Locked) {
1943
1944
1945
1946
1947
1948 may_free = __dispose_buffer(jh, transaction);
1949 goto zap_buffer;
1950 }
1951
1952
1953
1954
1955
1956
1957
1958 set_buffer_freed(bh);
1959 if (journal->j_running_transaction && buffer_jbddirty(bh))
1960 jh->b_next_transaction = journal->j_running_transaction;
1961 journal_put_journal_head(jh);
1962 spin_unlock(&journal->j_list_lock);
1963 jbd_unlock_bh_state(bh);
1964 spin_unlock(&journal->j_state_lock);
1965 return 0;
1966 } else {
1967
1968
1969
1970
1971
1972
1973 J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
1974 JBUFFER_TRACE(jh, "on running transaction");
1975 may_free = __dispose_buffer(jh, transaction);
1976 }
1977
1978zap_buffer:
1979 journal_put_journal_head(jh);
1980zap_buffer_no_jh:
1981 spin_unlock(&journal->j_list_lock);
1982 jbd_unlock_bh_state(bh);
1983 spin_unlock(&journal->j_state_lock);
1984zap_buffer_unlocked:
1985 clear_buffer_dirty(bh);
1986 J_ASSERT_BH(bh, !buffer_jbddirty(bh));
1987 clear_buffer_mapped(bh);
1988 clear_buffer_req(bh);
1989 clear_buffer_new(bh);
1990 bh->b_bdev = NULL;
1991 return may_free;
1992}
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002void journal_invalidatepage(journal_t *journal,
2003 struct page *page,
2004 unsigned long offset)
2005{
2006 struct buffer_head *head, *bh, *next;
2007 unsigned int curr_off = 0;
2008 int may_free = 1;
2009
2010 if (!PageLocked(page))
2011 BUG();
2012 if (!page_has_buffers(page))
2013 return;
2014
2015
2016
2017
2018
2019 head = bh = page_buffers(page);
2020 do {
2021 unsigned int next_off = curr_off + bh->b_size;
2022 next = bh->b_this_page;
2023
2024 if (offset <= curr_off) {
2025
2026 lock_buffer(bh);
2027 may_free &= journal_unmap_buffer(journal, bh);
2028 unlock_buffer(bh);
2029 }
2030 curr_off = next_off;
2031 bh = next;
2032
2033 } while (bh != head);
2034
2035 if (!offset) {
2036 if (may_free && try_to_free_buffers(page))
2037 J_ASSERT(!page_has_buffers(page));
2038 }
2039}
2040
2041
2042
2043
2044void __journal_file_buffer(struct journal_head *jh,
2045 transaction_t *transaction, int jlist)
2046{
2047 struct journal_head **list = NULL;
2048 int was_dirty = 0;
2049 struct buffer_head *bh = jh2bh(jh);
2050
2051 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
2052 assert_spin_locked(&transaction->t_journal->j_list_lock);
2053
2054 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
2055 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
2056 jh->b_transaction == NULL);
2057
2058 if (jh->b_transaction && jh->b_jlist == jlist)
2059 return;
2060
2061 if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
2062 jlist == BJ_Shadow || jlist == BJ_Forget) {
2063
2064
2065
2066
2067
2068
2069
2070 if (buffer_dirty(bh))
2071 warn_dirty_buffer(bh);
2072 if (test_clear_buffer_dirty(bh) ||
2073 test_clear_buffer_jbddirty(bh))
2074 was_dirty = 1;
2075 }
2076
2077 if (jh->b_transaction)
2078 __journal_temp_unlink_buffer(jh);
2079 else
2080 journal_grab_journal_head(bh);
2081 jh->b_transaction = transaction;
2082
2083 switch (jlist) {
2084 case BJ_None:
2085 J_ASSERT_JH(jh, !jh->b_committed_data);
2086 J_ASSERT_JH(jh, !jh->b_frozen_data);
2087 return;
2088 case BJ_SyncData:
2089 list = &transaction->t_sync_datalist;
2090 break;
2091 case BJ_Metadata:
2092 transaction->t_nr_buffers++;
2093 list = &transaction->t_buffers;
2094 break;
2095 case BJ_Forget:
2096 list = &transaction->t_forget;
2097 break;
2098 case BJ_IO:
2099 list = &transaction->t_iobuf_list;
2100 break;
2101 case BJ_Shadow:
2102 list = &transaction->t_shadow_list;
2103 break;
2104 case BJ_LogCtl:
2105 list = &transaction->t_log_list;
2106 break;
2107 case BJ_Reserved:
2108 list = &transaction->t_reserved_list;
2109 break;
2110 case BJ_Locked:
2111 list = &transaction->t_locked_list;
2112 break;
2113 }
2114
2115 __blist_add_buffer(list, jh);
2116 jh->b_jlist = jlist;
2117
2118 if (was_dirty)
2119 set_buffer_jbddirty(bh);
2120}
2121
2122void journal_file_buffer(struct journal_head *jh,
2123 transaction_t *transaction, int jlist)
2124{
2125 jbd_lock_bh_state(jh2bh(jh));
2126 spin_lock(&transaction->t_journal->j_list_lock);
2127 __journal_file_buffer(jh, transaction, jlist);
2128 spin_unlock(&transaction->t_journal->j_list_lock);
2129 jbd_unlock_bh_state(jh2bh(jh));
2130}
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143void __journal_refile_buffer(struct journal_head *jh)
2144{
2145 int was_dirty, jlist;
2146 struct buffer_head *bh = jh2bh(jh);
2147
2148 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
2149 if (jh->b_transaction)
2150 assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
2151
2152
2153 if (jh->b_next_transaction == NULL) {
2154 __journal_unfile_buffer(jh);
2155 return;
2156 }
2157
2158
2159
2160
2161
2162
2163 was_dirty = test_clear_buffer_jbddirty(bh);
2164 __journal_temp_unlink_buffer(jh);
2165
2166
2167
2168
2169
2170 jh->b_transaction = jh->b_next_transaction;
2171 jh->b_next_transaction = NULL;
2172 if (buffer_freed(bh))
2173 jlist = BJ_Forget;
2174 else if (jh->b_modified)
2175 jlist = BJ_Metadata;
2176 else
2177 jlist = BJ_Reserved;
2178 __journal_file_buffer(jh, jh->b_transaction, jlist);
2179 J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
2180
2181 if (was_dirty)
2182 set_buffer_jbddirty(bh);
2183}
2184
2185
2186
2187
2188
2189
2190
2191void journal_refile_buffer(journal_t *journal, struct journal_head *jh)
2192{
2193 struct buffer_head *bh = jh2bh(jh);
2194
2195
2196 get_bh(bh);
2197 jbd_lock_bh_state(bh);
2198 spin_lock(&journal->j_list_lock);
2199 __journal_refile_buffer(jh);
2200 jbd_unlock_bh_state(bh);
2201 spin_unlock(&journal->j_list_lock);
2202 __brelse(bh);
2203}
2204