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