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