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, KM_USER0);
722 memcpy(jh->b_frozen_data, source+offset, jh2bh(jh)->b_size);
723 kunmap_atomic(source, KM_USER0);
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 if (handle->h_sync)
1437 transaction->t_synchronous_commit = 1;
1438 current->journal_info = NULL;
1439 spin_lock(&journal->j_state_lock);
1440 spin_lock(&transaction->t_handle_lock);
1441 transaction->t_outstanding_credits -= handle->h_buffer_credits;
1442 transaction->t_updates--;
1443 if (!transaction->t_updates) {
1444 wake_up(&journal->j_wait_updates);
1445 if (journal->j_barrier_count)
1446 wake_up(&journal->j_wait_transaction_locked);
1447 }
1448
1449
1450
1451
1452
1453
1454
1455 if (handle->h_sync ||
1456 transaction->t_outstanding_credits >
1457 journal->j_max_transaction_buffers ||
1458 time_after_eq(jiffies, transaction->t_expires)) {
1459
1460
1461
1462 tid_t tid = transaction->t_tid;
1463
1464 spin_unlock(&transaction->t_handle_lock);
1465 jbd_debug(2, "transaction too old, requesting commit for "
1466 "handle %p\n", handle);
1467
1468 __log_start_commit(journal, transaction->t_tid);
1469 spin_unlock(&journal->j_state_lock);
1470
1471
1472
1473
1474
1475 if (handle->h_sync && !(current->flags & PF_MEMALLOC))
1476 err = log_wait_commit(journal, tid);
1477 } else {
1478 spin_unlock(&transaction->t_handle_lock);
1479 spin_unlock(&journal->j_state_lock);
1480 }
1481
1482 lock_map_release(&handle->h_lockdep_map);
1483
1484 jbd_free_handle(handle);
1485 return err;
1486}
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496int journal_force_commit(journal_t *journal)
1497{
1498 handle_t *handle;
1499 int ret;
1500
1501 handle = journal_start(journal, 1);
1502 if (IS_ERR(handle)) {
1503 ret = PTR_ERR(handle);
1504 } else {
1505 handle->h_sync = 1;
1506 ret = journal_stop(handle);
1507 }
1508 return ret;
1509}
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527static inline void
1528__blist_add_buffer(struct journal_head **list, struct journal_head *jh)
1529{
1530 if (!*list) {
1531 jh->b_tnext = jh->b_tprev = jh;
1532 *list = jh;
1533 } else {
1534
1535 struct journal_head *first = *list, *last = first->b_tprev;
1536 jh->b_tprev = last;
1537 jh->b_tnext = first;
1538 last->b_tnext = first->b_tprev = jh;
1539 }
1540}
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551static inline void
1552__blist_del_buffer(struct journal_head **list, struct journal_head *jh)
1553{
1554 if (*list == jh) {
1555 *list = jh->b_tnext;
1556 if (*list == jh)
1557 *list = NULL;
1558 }
1559 jh->b_tprev->b_tnext = jh->b_tnext;
1560 jh->b_tnext->b_tprev = jh->b_tprev;
1561}
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574static void __journal_temp_unlink_buffer(struct journal_head *jh)
1575{
1576 struct journal_head **list = NULL;
1577 transaction_t *transaction;
1578 struct buffer_head *bh = jh2bh(jh);
1579
1580 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1581 transaction = jh->b_transaction;
1582 if (transaction)
1583 assert_spin_locked(&transaction->t_journal->j_list_lock);
1584
1585 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1586 if (jh->b_jlist != BJ_None)
1587 J_ASSERT_JH(jh, transaction != NULL);
1588
1589 switch (jh->b_jlist) {
1590 case BJ_None:
1591 return;
1592 case BJ_SyncData:
1593 list = &transaction->t_sync_datalist;
1594 break;
1595 case BJ_Metadata:
1596 transaction->t_nr_buffers--;
1597 J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
1598 list = &transaction->t_buffers;
1599 break;
1600 case BJ_Forget:
1601 list = &transaction->t_forget;
1602 break;
1603 case BJ_IO:
1604 list = &transaction->t_iobuf_list;
1605 break;
1606 case BJ_Shadow:
1607 list = &transaction->t_shadow_list;
1608 break;
1609 case BJ_LogCtl:
1610 list = &transaction->t_log_list;
1611 break;
1612 case BJ_Reserved:
1613 list = &transaction->t_reserved_list;
1614 break;
1615 case BJ_Locked:
1616 list = &transaction->t_locked_list;
1617 break;
1618 }
1619
1620 __blist_del_buffer(list, jh);
1621 jh->b_jlist = BJ_None;
1622 if (test_clear_buffer_jbddirty(bh))
1623 mark_buffer_dirty(bh);
1624}
1625
1626
1627
1628
1629
1630
1631
1632
1633void __journal_unfile_buffer(struct journal_head *jh)
1634{
1635 __journal_temp_unlink_buffer(jh);
1636 jh->b_transaction = NULL;
1637 journal_put_journal_head(jh);
1638}
1639
1640void journal_unfile_buffer(journal_t *journal, struct journal_head *jh)
1641{
1642 struct buffer_head *bh = jh2bh(jh);
1643
1644
1645 get_bh(bh);
1646 jbd_lock_bh_state(bh);
1647 spin_lock(&journal->j_list_lock);
1648 __journal_unfile_buffer(jh);
1649 spin_unlock(&journal->j_list_lock);
1650 jbd_unlock_bh_state(bh);
1651 __brelse(bh);
1652}
1653
1654
1655
1656
1657
1658
1659static void
1660__journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh)
1661{
1662 struct journal_head *jh;
1663
1664 jh = bh2jh(bh);
1665
1666 if (buffer_locked(bh) || buffer_dirty(bh))
1667 goto out;
1668
1669 if (jh->b_next_transaction != NULL)
1670 goto out;
1671
1672 spin_lock(&journal->j_list_lock);
1673 if (jh->b_transaction != NULL && jh->b_cp_transaction == NULL) {
1674 if (jh->b_jlist == BJ_SyncData || jh->b_jlist == BJ_Locked) {
1675
1676 JBUFFER_TRACE(jh, "release data");
1677 __journal_unfile_buffer(jh);
1678 }
1679 } else if (jh->b_cp_transaction != NULL && jh->b_transaction == NULL) {
1680
1681 if (jh->b_jlist == BJ_None) {
1682 JBUFFER_TRACE(jh, "remove from checkpoint list");
1683 __journal_remove_checkpoint(jh);
1684 }
1685 }
1686 spin_unlock(&journal->j_list_lock);
1687out:
1688 return;
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
1727
1728
1729int journal_try_to_free_buffers(journal_t *journal,
1730 struct page *page, gfp_t gfp_mask)
1731{
1732 struct buffer_head *head;
1733 struct buffer_head *bh;
1734 int ret = 0;
1735
1736 J_ASSERT(PageLocked(page));
1737
1738 head = page_buffers(page);
1739 bh = head;
1740 do {
1741 struct journal_head *jh;
1742
1743
1744
1745
1746
1747
1748 jh = journal_grab_journal_head(bh);
1749 if (!jh)
1750 continue;
1751
1752 jbd_lock_bh_state(bh);
1753 __journal_try_to_free_buffer(journal, bh);
1754 journal_put_journal_head(jh);
1755 jbd_unlock_bh_state(bh);
1756 if (buffer_jbd(bh))
1757 goto busy;
1758 } while ((bh = bh->b_this_page) != head);
1759
1760 ret = try_to_free_buffers(page);
1761
1762busy:
1763 return ret;
1764}
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
1779{
1780 int may_free = 1;
1781 struct buffer_head *bh = jh2bh(jh);
1782
1783 if (jh->b_cp_transaction) {
1784 JBUFFER_TRACE(jh, "on running+cp transaction");
1785 __journal_temp_unlink_buffer(jh);
1786
1787
1788
1789
1790
1791 clear_buffer_dirty(bh);
1792 __journal_file_buffer(jh, transaction, BJ_Forget);
1793 may_free = 0;
1794 } else {
1795 JBUFFER_TRACE(jh, "on running transaction");
1796 __journal_unfile_buffer(jh);
1797 }
1798 return may_free;
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
1846
1847
1848static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh)
1849{
1850 transaction_t *transaction;
1851 struct journal_head *jh;
1852 int may_free = 1;
1853 int ret;
1854
1855 BUFFER_TRACE(bh, "entry");
1856
1857
1858
1859
1860
1861
1862
1863 if (!buffer_jbd(bh))
1864 goto zap_buffer_unlocked;
1865
1866 spin_lock(&journal->j_state_lock);
1867 jbd_lock_bh_state(bh);
1868 spin_lock(&journal->j_list_lock);
1869
1870 jh = journal_grab_journal_head(bh);
1871 if (!jh)
1872 goto zap_buffer_no_jh;
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889 transaction = jh->b_transaction;
1890 if (transaction == NULL) {
1891
1892
1893
1894
1895 if (!jh->b_cp_transaction) {
1896 JBUFFER_TRACE(jh, "not on any transaction: zap");
1897 goto zap_buffer;
1898 }
1899
1900 if (!buffer_dirty(bh)) {
1901
1902 goto zap_buffer;
1903 }
1904
1905
1906
1907
1908
1909 if (journal->j_running_transaction) {
1910
1911
1912
1913 JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
1914 ret = __dispose_buffer(jh,
1915 journal->j_running_transaction);
1916 journal_put_journal_head(jh);
1917 spin_unlock(&journal->j_list_lock);
1918 jbd_unlock_bh_state(bh);
1919 spin_unlock(&journal->j_state_lock);
1920 return ret;
1921 } else {
1922
1923
1924
1925
1926 if (journal->j_committing_transaction) {
1927 JBUFFER_TRACE(jh, "give to committing trans");
1928 ret = __dispose_buffer(jh,
1929 journal->j_committing_transaction);
1930 journal_put_journal_head(jh);
1931 spin_unlock(&journal->j_list_lock);
1932 jbd_unlock_bh_state(bh);
1933 spin_unlock(&journal->j_state_lock);
1934 return ret;
1935 } else {
1936
1937
1938 clear_buffer_jbddirty(bh);
1939 goto zap_buffer;
1940 }
1941 }
1942 } else if (transaction == journal->j_committing_transaction) {
1943 JBUFFER_TRACE(jh, "on committing transaction");
1944 if (jh->b_jlist == BJ_Locked) {
1945
1946
1947
1948
1949
1950 may_free = __dispose_buffer(jh, transaction);
1951 goto zap_buffer;
1952 }
1953
1954
1955
1956
1957
1958
1959
1960 set_buffer_freed(bh);
1961 if (journal->j_running_transaction && buffer_jbddirty(bh))
1962 jh->b_next_transaction = journal->j_running_transaction;
1963 journal_put_journal_head(jh);
1964 spin_unlock(&journal->j_list_lock);
1965 jbd_unlock_bh_state(bh);
1966 spin_unlock(&journal->j_state_lock);
1967 return 0;
1968 } else {
1969
1970
1971
1972
1973
1974
1975 J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
1976 JBUFFER_TRACE(jh, "on running transaction");
1977 may_free = __dispose_buffer(jh, transaction);
1978 }
1979
1980zap_buffer:
1981 journal_put_journal_head(jh);
1982zap_buffer_no_jh:
1983 spin_unlock(&journal->j_list_lock);
1984 jbd_unlock_bh_state(bh);
1985 spin_unlock(&journal->j_state_lock);
1986zap_buffer_unlocked:
1987 clear_buffer_dirty(bh);
1988 J_ASSERT_BH(bh, !buffer_jbddirty(bh));
1989 clear_buffer_mapped(bh);
1990 clear_buffer_req(bh);
1991 clear_buffer_new(bh);
1992 bh->b_bdev = NULL;
1993 return may_free;
1994}
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004void journal_invalidatepage(journal_t *journal,
2005 struct page *page,
2006 unsigned long offset)
2007{
2008 struct buffer_head *head, *bh, *next;
2009 unsigned int curr_off = 0;
2010 int may_free = 1;
2011
2012 if (!PageLocked(page))
2013 BUG();
2014 if (!page_has_buffers(page))
2015 return;
2016
2017
2018
2019
2020
2021 head = bh = page_buffers(page);
2022 do {
2023 unsigned int next_off = curr_off + bh->b_size;
2024 next = bh->b_this_page;
2025
2026 if (offset <= curr_off) {
2027
2028 lock_buffer(bh);
2029 may_free &= journal_unmap_buffer(journal, bh);
2030 unlock_buffer(bh);
2031 }
2032 curr_off = next_off;
2033 bh = next;
2034
2035 } while (bh != head);
2036
2037 if (!offset) {
2038 if (may_free && try_to_free_buffers(page))
2039 J_ASSERT(!page_has_buffers(page));
2040 }
2041}
2042
2043
2044
2045
2046void __journal_file_buffer(struct journal_head *jh,
2047 transaction_t *transaction, int jlist)
2048{
2049 struct journal_head **list = NULL;
2050 int was_dirty = 0;
2051 struct buffer_head *bh = jh2bh(jh);
2052
2053 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
2054 assert_spin_locked(&transaction->t_journal->j_list_lock);
2055
2056 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
2057 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
2058 jh->b_transaction == NULL);
2059
2060 if (jh->b_transaction && jh->b_jlist == jlist)
2061 return;
2062
2063 if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
2064 jlist == BJ_Shadow || jlist == BJ_Forget) {
2065
2066
2067
2068
2069
2070
2071
2072 if (buffer_dirty(bh))
2073 warn_dirty_buffer(bh);
2074 if (test_clear_buffer_dirty(bh) ||
2075 test_clear_buffer_jbddirty(bh))
2076 was_dirty = 1;
2077 }
2078
2079 if (jh->b_transaction)
2080 __journal_temp_unlink_buffer(jh);
2081 else
2082 journal_grab_journal_head(bh);
2083 jh->b_transaction = transaction;
2084
2085 switch (jlist) {
2086 case BJ_None:
2087 J_ASSERT_JH(jh, !jh->b_committed_data);
2088 J_ASSERT_JH(jh, !jh->b_frozen_data);
2089 return;
2090 case BJ_SyncData:
2091 list = &transaction->t_sync_datalist;
2092 break;
2093 case BJ_Metadata:
2094 transaction->t_nr_buffers++;
2095 list = &transaction->t_buffers;
2096 break;
2097 case BJ_Forget:
2098 list = &transaction->t_forget;
2099 break;
2100 case BJ_IO:
2101 list = &transaction->t_iobuf_list;
2102 break;
2103 case BJ_Shadow:
2104 list = &transaction->t_shadow_list;
2105 break;
2106 case BJ_LogCtl:
2107 list = &transaction->t_log_list;
2108 break;
2109 case BJ_Reserved:
2110 list = &transaction->t_reserved_list;
2111 break;
2112 case BJ_Locked:
2113 list = &transaction->t_locked_list;
2114 break;
2115 }
2116
2117 __blist_add_buffer(list, jh);
2118 jh->b_jlist = jlist;
2119
2120 if (was_dirty)
2121 set_buffer_jbddirty(bh);
2122}
2123
2124void journal_file_buffer(struct journal_head *jh,
2125 transaction_t *transaction, int jlist)
2126{
2127 jbd_lock_bh_state(jh2bh(jh));
2128 spin_lock(&transaction->t_journal->j_list_lock);
2129 __journal_file_buffer(jh, transaction, jlist);
2130 spin_unlock(&transaction->t_journal->j_list_lock);
2131 jbd_unlock_bh_state(jh2bh(jh));
2132}
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145void __journal_refile_buffer(struct journal_head *jh)
2146{
2147 int was_dirty, jlist;
2148 struct buffer_head *bh = jh2bh(jh);
2149
2150 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
2151 if (jh->b_transaction)
2152 assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
2153
2154
2155 if (jh->b_next_transaction == NULL) {
2156 __journal_unfile_buffer(jh);
2157 return;
2158 }
2159
2160
2161
2162
2163
2164
2165 was_dirty = test_clear_buffer_jbddirty(bh);
2166 __journal_temp_unlink_buffer(jh);
2167
2168
2169
2170
2171
2172 jh->b_transaction = jh->b_next_transaction;
2173 jh->b_next_transaction = NULL;
2174 if (buffer_freed(bh))
2175 jlist = BJ_Forget;
2176 else if (jh->b_modified)
2177 jlist = BJ_Metadata;
2178 else
2179 jlist = BJ_Reserved;
2180 __journal_file_buffer(jh, jh->b_transaction, jlist);
2181 J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
2182
2183 if (was_dirty)
2184 set_buffer_jbddirty(bh);
2185}
2186
2187
2188
2189
2190
2191
2192
2193void journal_refile_buffer(journal_t *journal, struct journal_head *jh)
2194{
2195 struct buffer_head *bh = jh2bh(jh);
2196
2197
2198 get_bh(bh);
2199 jbd_lock_bh_state(bh);
2200 spin_lock(&journal->j_list_lock);
2201 __journal_refile_buffer(jh);
2202 jbd_unlock_bh_state(bh);
2203 spin_unlock(&journal->j_list_lock);
2204 __brelse(bh);
2205}
2206