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