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