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/jbd2.h>
23#include <linux/errno.h>
24#include <linux/slab.h>
25#include <linux/timer.h>
26#include <linux/mm.h>
27#include <linux/highmem.h>
28#include <linux/hrtimer.h>
29#include <linux/backing-dev.h>
30#include <linux/bug.h>
31#include <linux/module.h>
32
33static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh);
34static void __jbd2_journal_unfile_buffer(struct journal_head *jh);
35
36static struct kmem_cache *transaction_cache;
37int __init jbd2_journal_init_transaction_cache(void)
38{
39 J_ASSERT(!transaction_cache);
40 transaction_cache = kmem_cache_create("jbd2_transaction_s",
41 sizeof(transaction_t),
42 0,
43 SLAB_HWCACHE_ALIGN|SLAB_TEMPORARY,
44 NULL);
45 if (transaction_cache)
46 return 0;
47 return -ENOMEM;
48}
49
50void jbd2_journal_destroy_transaction_cache(void)
51{
52 if (transaction_cache) {
53 kmem_cache_destroy(transaction_cache);
54 transaction_cache = NULL;
55 }
56}
57
58void jbd2_journal_free_transaction(transaction_t *transaction)
59{
60 if (unlikely(ZERO_OR_NULL_PTR(transaction)))
61 return;
62 kmem_cache_free(transaction_cache, transaction);
63}
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80static transaction_t *
81jbd2_get_transaction(journal_t *journal, transaction_t *transaction)
82{
83 transaction->t_journal = journal;
84 transaction->t_state = T_RUNNING;
85 transaction->t_start_time = ktime_get();
86 transaction->t_tid = journal->j_transaction_sequence++;
87 transaction->t_expires = jiffies + journal->j_commit_interval;
88 spin_lock_init(&transaction->t_handle_lock);
89 atomic_set(&transaction->t_updates, 0);
90 atomic_set(&transaction->t_outstanding_credits, 0);
91 atomic_set(&transaction->t_handle_count, 0);
92 INIT_LIST_HEAD(&transaction->t_inode_list);
93 INIT_LIST_HEAD(&transaction->t_private_list);
94
95
96 journal->j_commit_timer.expires = round_jiffies_up(transaction->t_expires);
97 add_timer(&journal->j_commit_timer);
98
99 J_ASSERT(journal->j_running_transaction == NULL);
100 journal->j_running_transaction = transaction;
101 transaction->t_max_wait = 0;
102 transaction->t_start = jiffies;
103
104 return transaction;
105}
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125static inline void update_t_max_wait(transaction_t *transaction,
126 unsigned long ts)
127{
128#ifdef CONFIG_JBD2_DEBUG
129 if (jbd2_journal_enable_debug &&
130 time_after(transaction->t_start, ts)) {
131 ts = jbd2_time_diff(ts, transaction->t_start);
132 spin_lock(&transaction->t_handle_lock);
133 if (ts > transaction->t_max_wait)
134 transaction->t_max_wait = ts;
135 spin_unlock(&transaction->t_handle_lock);
136 }
137#endif
138}
139
140
141
142
143
144
145
146
147static int start_this_handle(journal_t *journal, handle_t *handle,
148 gfp_t gfp_mask)
149{
150 transaction_t *transaction, *new_transaction = NULL;
151 tid_t tid;
152 int needed, need_to_start;
153 int nblocks = handle->h_buffer_credits;
154 unsigned long ts = jiffies;
155
156 if (nblocks > journal->j_max_transaction_buffers) {
157 printk(KERN_ERR "JBD2: %s wants too many credits (%d > %d)\n",
158 current->comm, nblocks,
159 journal->j_max_transaction_buffers);
160 return -ENOSPC;
161 }
162
163alloc_transaction:
164 if (!journal->j_running_transaction) {
165 new_transaction = kmem_cache_zalloc(transaction_cache,
166 gfp_mask);
167 if (!new_transaction) {
168
169
170
171
172
173
174
175 if ((gfp_mask & __GFP_FS) == 0) {
176 congestion_wait(BLK_RW_ASYNC, HZ/50);
177 goto alloc_transaction;
178 }
179 return -ENOMEM;
180 }
181 }
182
183 jbd_debug(3, "New handle %p going live.\n", handle);
184
185
186
187
188
189repeat:
190 read_lock(&journal->j_state_lock);
191 BUG_ON(journal->j_flags & JBD2_UNMOUNT);
192 if (is_journal_aborted(journal) ||
193 (journal->j_errno != 0 && !(journal->j_flags & JBD2_ACK_ERR))) {
194 read_unlock(&journal->j_state_lock);
195 jbd2_journal_free_transaction(new_transaction);
196 return -EROFS;
197 }
198
199
200 if (journal->j_barrier_count) {
201 read_unlock(&journal->j_state_lock);
202 wait_event(journal->j_wait_transaction_locked,
203 journal->j_barrier_count == 0);
204 goto repeat;
205 }
206
207 if (!journal->j_running_transaction) {
208 read_unlock(&journal->j_state_lock);
209 if (!new_transaction)
210 goto alloc_transaction;
211 write_lock(&journal->j_state_lock);
212 if (!journal->j_running_transaction &&
213 !journal->j_barrier_count) {
214 jbd2_get_transaction(journal, new_transaction);
215 new_transaction = NULL;
216 }
217 write_unlock(&journal->j_state_lock);
218 goto repeat;
219 }
220
221 transaction = journal->j_running_transaction;
222
223
224
225
226
227 if (transaction->t_state == T_LOCKED) {
228 DEFINE_WAIT(wait);
229
230 prepare_to_wait(&journal->j_wait_transaction_locked,
231 &wait, TASK_UNINTERRUPTIBLE);
232 read_unlock(&journal->j_state_lock);
233 schedule();
234 finish_wait(&journal->j_wait_transaction_locked, &wait);
235 goto repeat;
236 }
237
238
239
240
241
242
243 needed = atomic_add_return(nblocks,
244 &transaction->t_outstanding_credits);
245
246 if (needed > journal->j_max_transaction_buffers) {
247
248
249
250
251
252 DEFINE_WAIT(wait);
253
254 jbd_debug(2, "Handle %p starting new commit...\n", handle);
255 atomic_sub(nblocks, &transaction->t_outstanding_credits);
256 prepare_to_wait(&journal->j_wait_transaction_locked, &wait,
257 TASK_UNINTERRUPTIBLE);
258 tid = transaction->t_tid;
259 need_to_start = !tid_geq(journal->j_commit_request, tid);
260 read_unlock(&journal->j_state_lock);
261 if (need_to_start)
262 jbd2_log_start_commit(journal, tid);
263 schedule();
264 finish_wait(&journal->j_wait_transaction_locked, &wait);
265 goto repeat;
266 }
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293 if (__jbd2_log_space_left(journal) < jbd_space_needed(journal)) {
294 jbd_debug(2, "Handle %p waiting for checkpoint...\n", handle);
295 atomic_sub(nblocks, &transaction->t_outstanding_credits);
296 read_unlock(&journal->j_state_lock);
297 write_lock(&journal->j_state_lock);
298 if (__jbd2_log_space_left(journal) < jbd_space_needed(journal))
299 __jbd2_log_wait_for_space(journal);
300 write_unlock(&journal->j_state_lock);
301 goto repeat;
302 }
303
304
305
306
307 update_t_max_wait(transaction, ts);
308 handle->h_transaction = transaction;
309 atomic_inc(&transaction->t_updates);
310 atomic_inc(&transaction->t_handle_count);
311 jbd_debug(4, "Handle %p given %d credits (total %d, free %d)\n",
312 handle, nblocks,
313 atomic_read(&transaction->t_outstanding_credits),
314 __jbd2_log_space_left(journal));
315 read_unlock(&journal->j_state_lock);
316
317 lock_map_acquire(&handle->h_lockdep_map);
318 jbd2_journal_free_transaction(new_transaction);
319 return 0;
320}
321
322static struct lock_class_key jbd2_handle_key;
323
324
325static handle_t *new_handle(int nblocks)
326{
327 handle_t *handle = jbd2_alloc_handle(GFP_NOFS);
328 if (!handle)
329 return NULL;
330 memset(handle, 0, sizeof(*handle));
331 handle->h_buffer_credits = nblocks;
332 handle->h_ref = 1;
333
334 lockdep_init_map(&handle->h_lockdep_map, "jbd2_handle",
335 &jbd2_handle_key, 0);
336
337 return handle;
338}
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355handle_t *jbd2__journal_start(journal_t *journal, int nblocks, gfp_t gfp_mask)
356{
357 handle_t *handle = journal_current_handle();
358 int err;
359
360 if (!journal)
361 return ERR_PTR(-EROFS);
362
363 if (handle) {
364 J_ASSERT(handle->h_transaction->t_journal == journal);
365 handle->h_ref++;
366 return handle;
367 }
368
369 handle = new_handle(nblocks);
370 if (!handle)
371 return ERR_PTR(-ENOMEM);
372
373 current->journal_info = handle;
374
375 err = start_this_handle(journal, handle, gfp_mask);
376 if (err < 0) {
377 jbd2_free_handle(handle);
378 current->journal_info = NULL;
379 handle = ERR_PTR(err);
380 }
381 return handle;
382}
383EXPORT_SYMBOL(jbd2__journal_start);
384
385
386handle_t *jbd2_journal_start(journal_t *journal, int nblocks)
387{
388 return jbd2__journal_start(journal, nblocks, GFP_NOFS);
389}
390EXPORT_SYMBOL(jbd2_journal_start);
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413int jbd2_journal_extend(handle_t *handle, int nblocks)
414{
415 transaction_t *transaction = handle->h_transaction;
416 journal_t *journal = transaction->t_journal;
417 int result;
418 int wanted;
419
420 result = -EIO;
421 if (is_handle_aborted(handle))
422 goto out;
423
424 result = 1;
425
426 read_lock(&journal->j_state_lock);
427
428
429 if (handle->h_transaction->t_state != T_RUNNING) {
430 jbd_debug(3, "denied handle %p %d blocks: "
431 "transaction not running\n", handle, nblocks);
432 goto error_out;
433 }
434
435 spin_lock(&transaction->t_handle_lock);
436 wanted = atomic_read(&transaction->t_outstanding_credits) + nblocks;
437
438 if (wanted > journal->j_max_transaction_buffers) {
439 jbd_debug(3, "denied handle %p %d blocks: "
440 "transaction too large\n", handle, nblocks);
441 goto unlock;
442 }
443
444 if (wanted > __jbd2_log_space_left(journal)) {
445 jbd_debug(3, "denied handle %p %d blocks: "
446 "insufficient log space\n", handle, nblocks);
447 goto unlock;
448 }
449
450 handle->h_buffer_credits += nblocks;
451 atomic_add(nblocks, &transaction->t_outstanding_credits);
452 result = 0;
453
454 jbd_debug(3, "extended handle %p by %d\n", handle, nblocks);
455unlock:
456 spin_unlock(&transaction->t_handle_lock);
457error_out:
458 read_unlock(&journal->j_state_lock);
459out:
460 return result;
461}
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478int jbd2__journal_restart(handle_t *handle, int nblocks, gfp_t gfp_mask)
479{
480 transaction_t *transaction = handle->h_transaction;
481 journal_t *journal = transaction->t_journal;
482 tid_t tid;
483 int need_to_start, ret;
484
485
486
487 if (is_handle_aborted(handle))
488 return 0;
489
490
491
492
493
494 J_ASSERT(atomic_read(&transaction->t_updates) > 0);
495 J_ASSERT(journal_current_handle() == handle);
496
497 read_lock(&journal->j_state_lock);
498 spin_lock(&transaction->t_handle_lock);
499 atomic_sub(handle->h_buffer_credits,
500 &transaction->t_outstanding_credits);
501 if (atomic_dec_and_test(&transaction->t_updates))
502 wake_up(&journal->j_wait_updates);
503 spin_unlock(&transaction->t_handle_lock);
504
505 jbd_debug(2, "restarting handle %p\n", handle);
506 tid = transaction->t_tid;
507 need_to_start = !tid_geq(journal->j_commit_request, tid);
508 read_unlock(&journal->j_state_lock);
509 if (need_to_start)
510 jbd2_log_start_commit(journal, tid);
511
512 lock_map_release(&handle->h_lockdep_map);
513 handle->h_buffer_credits = nblocks;
514 ret = start_this_handle(journal, handle, gfp_mask);
515 return ret;
516}
517EXPORT_SYMBOL(jbd2__journal_restart);
518
519
520int jbd2_journal_restart(handle_t *handle, int nblocks)
521{
522 return jbd2__journal_restart(handle, nblocks, GFP_NOFS);
523}
524EXPORT_SYMBOL(jbd2_journal_restart);
525
526
527
528
529
530
531
532
533
534
535
536void jbd2_journal_lock_updates(journal_t *journal)
537{
538 DEFINE_WAIT(wait);
539
540 write_lock(&journal->j_state_lock);
541 ++journal->j_barrier_count;
542
543
544 while (1) {
545 transaction_t *transaction = journal->j_running_transaction;
546
547 if (!transaction)
548 break;
549
550 spin_lock(&transaction->t_handle_lock);
551 prepare_to_wait(&journal->j_wait_updates, &wait,
552 TASK_UNINTERRUPTIBLE);
553 if (!atomic_read(&transaction->t_updates)) {
554 spin_unlock(&transaction->t_handle_lock);
555 finish_wait(&journal->j_wait_updates, &wait);
556 break;
557 }
558 spin_unlock(&transaction->t_handle_lock);
559 write_unlock(&journal->j_state_lock);
560 schedule();
561 finish_wait(&journal->j_wait_updates, &wait);
562 write_lock(&journal->j_state_lock);
563 }
564 write_unlock(&journal->j_state_lock);
565
566
567
568
569
570
571
572 mutex_lock(&journal->j_barrier);
573}
574
575
576
577
578
579
580
581
582
583void jbd2_journal_unlock_updates (journal_t *journal)
584{
585 J_ASSERT(journal->j_barrier_count != 0);
586
587 mutex_unlock(&journal->j_barrier);
588 write_lock(&journal->j_state_lock);
589 --journal->j_barrier_count;
590 write_unlock(&journal->j_state_lock);
591 wake_up(&journal->j_wait_transaction_locked);
592}
593
594static void warn_dirty_buffer(struct buffer_head *bh)
595{
596 char b[BDEVNAME_SIZE];
597
598 printk(KERN_WARNING
599 "JBD2: Spotted dirty metadata buffer (dev = %s, blocknr = %llu). "
600 "There's a risk of filesystem corruption in case of system "
601 "crash.\n",
602 bdevname(bh->b_bdev, b), (unsigned long long)bh->b_blocknr);
603}
604
605
606
607
608
609
610
611
612
613
614
615static int
616do_get_write_access(handle_t *handle, struct journal_head *jh,
617 int force_copy)
618{
619 struct buffer_head *bh;
620 transaction_t *transaction;
621 journal_t *journal;
622 int error;
623 char *frozen_buffer = NULL;
624 int need_copy = 0;
625
626 if (is_handle_aborted(handle))
627 return -EROFS;
628
629 transaction = handle->h_transaction;
630 journal = transaction->t_journal;
631
632 jbd_debug(5, "journal_head %p, force_copy %d\n", jh, force_copy);
633
634 JBUFFER_TRACE(jh, "entry");
635repeat:
636 bh = jh2bh(jh);
637
638
639
640 lock_buffer(bh);
641 jbd_lock_bh_state(bh);
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656 if (buffer_dirty(bh)) {
657
658
659
660
661 if (jh->b_transaction) {
662 J_ASSERT_JH(jh,
663 jh->b_transaction == transaction ||
664 jh->b_transaction ==
665 journal->j_committing_transaction);
666 if (jh->b_next_transaction)
667 J_ASSERT_JH(jh, jh->b_next_transaction ==
668 transaction);
669 warn_dirty_buffer(bh);
670 }
671
672
673
674
675
676 JBUFFER_TRACE(jh, "Journalling dirty buffer");
677 clear_buffer_dirty(bh);
678 set_buffer_jbddirty(bh);
679 }
680
681 unlock_buffer(bh);
682
683 error = -EROFS;
684 if (is_handle_aborted(handle)) {
685 jbd_unlock_bh_state(bh);
686 goto out;
687 }
688 error = 0;
689
690
691
692
693
694 if (jh->b_transaction == transaction ||
695 jh->b_next_transaction == transaction)
696 goto done;
697
698
699
700
701
702 jh->b_modified = 0;
703
704
705
706
707
708 if (jh->b_frozen_data) {
709 JBUFFER_TRACE(jh, "has frozen data");
710 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
711 jh->b_next_transaction = transaction;
712 goto done;
713 }
714
715
716
717 if (jh->b_transaction && jh->b_transaction != transaction) {
718 JBUFFER_TRACE(jh, "owned by older transaction");
719 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
720 J_ASSERT_JH(jh, jh->b_transaction ==
721 journal->j_committing_transaction);
722
723
724
725
726
727
728
729
730
731
732 if (jh->b_jlist == BJ_Shadow) {
733 DEFINE_WAIT_BIT(wait, &bh->b_state, BH_Unshadow);
734 wait_queue_head_t *wqh;
735
736 wqh = bit_waitqueue(&bh->b_state, BH_Unshadow);
737
738 JBUFFER_TRACE(jh, "on shadow: sleep");
739 jbd_unlock_bh_state(bh);
740
741 for ( ; ; ) {
742 prepare_to_wait(wqh, &wait.wait,
743 TASK_UNINTERRUPTIBLE);
744 if (jh->b_jlist != BJ_Shadow)
745 break;
746 schedule();
747 }
748 finish_wait(wqh, &wait.wait);
749 goto repeat;
750 }
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766 if (jh->b_jlist != BJ_Forget || force_copy) {
767 JBUFFER_TRACE(jh, "generate frozen data");
768 if (!frozen_buffer) {
769 JBUFFER_TRACE(jh, "allocate memory for buffer");
770 jbd_unlock_bh_state(bh);
771 frozen_buffer =
772 jbd2_alloc(jh2bh(jh)->b_size,
773 GFP_NOFS);
774 if (!frozen_buffer) {
775 printk(KERN_EMERG
776 "%s: OOM for frozen_buffer\n",
777 __func__);
778 JBUFFER_TRACE(jh, "oom!");
779 error = -ENOMEM;
780 jbd_lock_bh_state(bh);
781 goto done;
782 }
783 goto repeat;
784 }
785 jh->b_frozen_data = frozen_buffer;
786 frozen_buffer = NULL;
787 need_copy = 1;
788 }
789 jh->b_next_transaction = transaction;
790 }
791
792
793
794
795
796
797
798 if (!jh->b_transaction) {
799 JBUFFER_TRACE(jh, "no transaction");
800 J_ASSERT_JH(jh, !jh->b_next_transaction);
801 JBUFFER_TRACE(jh, "file as BJ_Reserved");
802 spin_lock(&journal->j_list_lock);
803 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
804 spin_unlock(&journal->j_list_lock);
805 }
806
807done:
808 if (need_copy) {
809 struct page *page;
810 int offset;
811 char *source;
812
813 J_EXPECT_JH(jh, buffer_uptodate(jh2bh(jh)),
814 "Possible IO failure.\n");
815 page = jh2bh(jh)->b_page;
816 offset = offset_in_page(jh2bh(jh)->b_data);
817 source = kmap_atomic(page);
818
819 jbd2_buffer_frozen_trigger(jh, source + offset,
820 jh->b_triggers);
821 memcpy(jh->b_frozen_data, source+offset, jh2bh(jh)->b_size);
822 kunmap_atomic(source);
823
824
825
826
827
828 jh->b_frozen_triggers = jh->b_triggers;
829 }
830 jbd_unlock_bh_state(bh);
831
832
833
834
835
836 jbd2_journal_cancel_revoke(handle, jh);
837
838out:
839 if (unlikely(frozen_buffer))
840 jbd2_free(frozen_buffer, bh->b_size);
841
842 JBUFFER_TRACE(jh, "exit");
843 return error;
844}
845
846
847
848
849
850
851
852
853
854
855
856
857int jbd2_journal_get_write_access(handle_t *handle, struct buffer_head *bh)
858{
859 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
860 int rc;
861
862
863
864
865 rc = do_get_write_access(handle, jh, 0);
866 jbd2_journal_put_journal_head(jh);
867 return rc;
868}
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890int jbd2_journal_get_create_access(handle_t *handle, struct buffer_head *bh)
891{
892 transaction_t *transaction = handle->h_transaction;
893 journal_t *journal = transaction->t_journal;
894 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
895 int err;
896
897 jbd_debug(5, "journal_head %p\n", jh);
898 err = -EROFS;
899 if (is_handle_aborted(handle))
900 goto out;
901 err = 0;
902
903 JBUFFER_TRACE(jh, "entry");
904
905
906
907
908
909
910
911 jbd_lock_bh_state(bh);
912 spin_lock(&journal->j_list_lock);
913 J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
914 jh->b_transaction == NULL ||
915 (jh->b_transaction == journal->j_committing_transaction &&
916 jh->b_jlist == BJ_Forget)));
917
918 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
919 J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
920
921 if (jh->b_transaction == NULL) {
922
923
924
925
926
927
928
929
930 clear_buffer_dirty(jh2bh(jh));
931
932 jh->b_modified = 0;
933
934 JBUFFER_TRACE(jh, "file as BJ_Reserved");
935 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
936 } else if (jh->b_transaction == journal->j_committing_transaction) {
937
938 jh->b_modified = 0;
939
940 JBUFFER_TRACE(jh, "set next transaction");
941 jh->b_next_transaction = transaction;
942 }
943 spin_unlock(&journal->j_list_lock);
944 jbd_unlock_bh_state(bh);
945
946
947
948
949
950
951
952
953 JBUFFER_TRACE(jh, "cancelling revoke");
954 jbd2_journal_cancel_revoke(handle, jh);
955out:
956 jbd2_journal_put_journal_head(jh);
957 return err;
958}
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986int jbd2_journal_get_undo_access(handle_t *handle, struct buffer_head *bh)
987{
988 int err;
989 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
990 char *committed_data = NULL;
991
992 JBUFFER_TRACE(jh, "entry");
993
994
995
996
997
998
999 err = do_get_write_access(handle, jh, 1);
1000 if (err)
1001 goto out;
1002
1003repeat:
1004 if (!jh->b_committed_data) {
1005 committed_data = jbd2_alloc(jh2bh(jh)->b_size, GFP_NOFS);
1006 if (!committed_data) {
1007 printk(KERN_EMERG "%s: No memory for committed data\n",
1008 __func__);
1009 err = -ENOMEM;
1010 goto out;
1011 }
1012 }
1013
1014 jbd_lock_bh_state(bh);
1015 if (!jh->b_committed_data) {
1016
1017
1018 JBUFFER_TRACE(jh, "generate b_committed data");
1019 if (!committed_data) {
1020 jbd_unlock_bh_state(bh);
1021 goto repeat;
1022 }
1023
1024 jh->b_committed_data = committed_data;
1025 committed_data = NULL;
1026 memcpy(jh->b_committed_data, bh->b_data, bh->b_size);
1027 }
1028 jbd_unlock_bh_state(bh);
1029out:
1030 jbd2_journal_put_journal_head(jh);
1031 if (unlikely(committed_data))
1032 jbd2_free(committed_data, bh->b_size);
1033 return err;
1034}
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047void jbd2_journal_set_triggers(struct buffer_head *bh,
1048 struct jbd2_buffer_trigger_type *type)
1049{
1050 struct journal_head *jh = bh2jh(bh);
1051
1052 jh->b_triggers = type;
1053}
1054
1055void jbd2_buffer_frozen_trigger(struct journal_head *jh, void *mapped_data,
1056 struct jbd2_buffer_trigger_type *triggers)
1057{
1058 struct buffer_head *bh = jh2bh(jh);
1059
1060 if (!triggers || !triggers->t_frozen)
1061 return;
1062
1063 triggers->t_frozen(triggers, bh, mapped_data, bh->b_size);
1064}
1065
1066void jbd2_buffer_abort_trigger(struct journal_head *jh,
1067 struct jbd2_buffer_trigger_type *triggers)
1068{
1069 if (!triggers || !triggers->t_abort)
1070 return;
1071
1072 triggers->t_abort(triggers, jh2bh(jh));
1073}
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
1101{
1102 transaction_t *transaction = handle->h_transaction;
1103 journal_t *journal = transaction->t_journal;
1104 struct journal_head *jh = bh2jh(bh);
1105 int ret = 0;
1106
1107 jbd_debug(5, "journal_head %p\n", jh);
1108 JBUFFER_TRACE(jh, "entry");
1109 if (is_handle_aborted(handle))
1110 goto out;
1111 if (!buffer_jbd(bh)) {
1112 ret = -EUCLEAN;
1113 goto out;
1114 }
1115
1116 jbd_lock_bh_state(bh);
1117
1118 if (jh->b_modified == 0) {
1119
1120
1121
1122
1123
1124 jh->b_modified = 1;
1125 J_ASSERT_JH(jh, handle->h_buffer_credits > 0);
1126 handle->h_buffer_credits--;
1127 }
1128
1129
1130
1131
1132
1133
1134
1135
1136 if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) {
1137 JBUFFER_TRACE(jh, "fastpath");
1138 if (unlikely(jh->b_transaction !=
1139 journal->j_running_transaction)) {
1140 printk(KERN_EMERG "JBD: %s: "
1141 "jh->b_transaction (%llu, %p, %u) != "
1142 "journal->j_running_transaction (%p, %u)",
1143 journal->j_devname,
1144 (unsigned long long) bh->b_blocknr,
1145 jh->b_transaction,
1146 jh->b_transaction ? jh->b_transaction->t_tid : 0,
1147 journal->j_running_transaction,
1148 journal->j_running_transaction ?
1149 journal->j_running_transaction->t_tid : 0);
1150 ret = -EINVAL;
1151 }
1152 goto out_unlock_bh;
1153 }
1154
1155 set_buffer_jbddirty(bh);
1156
1157
1158
1159
1160
1161
1162
1163 if (jh->b_transaction != transaction) {
1164 JBUFFER_TRACE(jh, "already on other transaction");
1165 if (unlikely(jh->b_transaction !=
1166 journal->j_committing_transaction)) {
1167 printk(KERN_EMERG "JBD: %s: "
1168 "jh->b_transaction (%llu, %p, %u) != "
1169 "journal->j_committing_transaction (%p, %u)",
1170 journal->j_devname,
1171 (unsigned long long) bh->b_blocknr,
1172 jh->b_transaction,
1173 jh->b_transaction ? jh->b_transaction->t_tid : 0,
1174 journal->j_committing_transaction,
1175 journal->j_committing_transaction ?
1176 journal->j_committing_transaction->t_tid : 0);
1177 ret = -EINVAL;
1178 }
1179 if (unlikely(jh->b_next_transaction != transaction)) {
1180 printk(KERN_EMERG "JBD: %s: "
1181 "jh->b_next_transaction (%llu, %p, %u) != "
1182 "transaction (%p, %u)",
1183 journal->j_devname,
1184 (unsigned long long) bh->b_blocknr,
1185 jh->b_next_transaction,
1186 jh->b_next_transaction ?
1187 jh->b_next_transaction->t_tid : 0,
1188 transaction, transaction->t_tid);
1189 ret = -EINVAL;
1190 }
1191
1192
1193 goto out_unlock_bh;
1194 }
1195
1196
1197 J_ASSERT_JH(jh, jh->b_frozen_data == NULL);
1198
1199 JBUFFER_TRACE(jh, "file as BJ_Metadata");
1200 spin_lock(&journal->j_list_lock);
1201 __jbd2_journal_file_buffer(jh, handle->h_transaction, BJ_Metadata);
1202 spin_unlock(&journal->j_list_lock);
1203out_unlock_bh:
1204 jbd_unlock_bh_state(bh);
1205out:
1206 JBUFFER_TRACE(jh, "exit");
1207 WARN_ON(ret);
1208 return ret;
1209}
1210
1211
1212
1213
1214
1215
1216void
1217jbd2_journal_release_buffer(handle_t *handle, struct buffer_head *bh)
1218{
1219 BUFFER_TRACE(bh, "entry");
1220}
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239int jbd2_journal_forget (handle_t *handle, struct buffer_head *bh)
1240{
1241 transaction_t *transaction = handle->h_transaction;
1242 journal_t *journal = transaction->t_journal;
1243 struct journal_head *jh;
1244 int drop_reserve = 0;
1245 int err = 0;
1246 int was_modified = 0;
1247
1248 BUFFER_TRACE(bh, "entry");
1249
1250 jbd_lock_bh_state(bh);
1251 spin_lock(&journal->j_list_lock);
1252
1253 if (!buffer_jbd(bh))
1254 goto not_jbd;
1255 jh = bh2jh(bh);
1256
1257
1258
1259 if (!J_EXPECT_JH(jh, !jh->b_committed_data,
1260 "inconsistent data on disk")) {
1261 err = -EIO;
1262 goto not_jbd;
1263 }
1264
1265
1266 was_modified = jh->b_modified;
1267
1268
1269
1270
1271
1272 jh->b_modified = 0;
1273
1274 if (jh->b_transaction == handle->h_transaction) {
1275 J_ASSERT_JH(jh, !jh->b_frozen_data);
1276
1277
1278
1279
1280 clear_buffer_dirty(bh);
1281 clear_buffer_jbddirty(bh);
1282
1283 JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
1284
1285
1286
1287
1288
1289 if (was_modified)
1290 drop_reserve = 1;
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304 if (jh->b_cp_transaction) {
1305 __jbd2_journal_temp_unlink_buffer(jh);
1306 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
1307 } else {
1308 __jbd2_journal_unfile_buffer(jh);
1309 if (!buffer_jbd(bh)) {
1310 spin_unlock(&journal->j_list_lock);
1311 jbd_unlock_bh_state(bh);
1312 __bforget(bh);
1313 goto drop;
1314 }
1315 }
1316 } else if (jh->b_transaction) {
1317 J_ASSERT_JH(jh, (jh->b_transaction ==
1318 journal->j_committing_transaction));
1319
1320
1321 JBUFFER_TRACE(jh, "belongs to older transaction");
1322
1323
1324
1325 if (jh->b_next_transaction) {
1326 J_ASSERT(jh->b_next_transaction == transaction);
1327 jh->b_next_transaction = NULL;
1328
1329
1330
1331
1332
1333 if (was_modified)
1334 drop_reserve = 1;
1335 }
1336 }
1337
1338not_jbd:
1339 spin_unlock(&journal->j_list_lock);
1340 jbd_unlock_bh_state(bh);
1341 __brelse(bh);
1342drop:
1343 if (drop_reserve) {
1344
1345 handle->h_buffer_credits++;
1346 }
1347 return err;
1348}
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366int jbd2_journal_stop(handle_t *handle)
1367{
1368 transaction_t *transaction = handle->h_transaction;
1369 journal_t *journal = transaction->t_journal;
1370 int err, wait_for_commit = 0;
1371 tid_t tid;
1372 pid_t pid;
1373
1374 J_ASSERT(journal_current_handle() == handle);
1375
1376 if (is_handle_aborted(handle))
1377 err = -EIO;
1378 else {
1379 J_ASSERT(atomic_read(&transaction->t_updates) > 0);
1380 err = 0;
1381 }
1382
1383 if (--handle->h_ref > 0) {
1384 jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
1385 handle->h_ref);
1386 return err;
1387 }
1388
1389 jbd_debug(4, "Handle %p going down\n", handle);
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418 pid = current->pid;
1419 if (handle->h_sync && journal->j_last_sync_writer != pid) {
1420 u64 commit_time, trans_time;
1421
1422 journal->j_last_sync_writer = pid;
1423
1424 read_lock(&journal->j_state_lock);
1425 commit_time = journal->j_average_commit_time;
1426 read_unlock(&journal->j_state_lock);
1427
1428 trans_time = ktime_to_ns(ktime_sub(ktime_get(),
1429 transaction->t_start_time));
1430
1431 commit_time = max_t(u64, commit_time,
1432 1000*journal->j_min_batch_time);
1433 commit_time = min_t(u64, commit_time,
1434 1000*journal->j_max_batch_time);
1435
1436 if (trans_time < commit_time) {
1437 ktime_t expires = ktime_add_ns(ktime_get(),
1438 commit_time);
1439 set_current_state(TASK_UNINTERRUPTIBLE);
1440 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1441 }
1442 }
1443
1444 if (handle->h_sync)
1445 transaction->t_synchronous_commit = 1;
1446 current->journal_info = NULL;
1447 atomic_sub(handle->h_buffer_credits,
1448 &transaction->t_outstanding_credits);
1449
1450
1451
1452
1453
1454
1455
1456 if (handle->h_sync ||
1457 (atomic_read(&transaction->t_outstanding_credits) >
1458 journal->j_max_transaction_buffers) ||
1459 time_after_eq(jiffies, transaction->t_expires)) {
1460
1461
1462
1463
1464 jbd_debug(2, "transaction too old, requesting commit for "
1465 "handle %p\n", handle);
1466
1467 jbd2_log_start_commit(journal, transaction->t_tid);
1468
1469
1470
1471
1472
1473 if (handle->h_sync && !(current->flags & PF_MEMALLOC))
1474 wait_for_commit = 1;
1475 }
1476
1477
1478
1479
1480
1481
1482
1483 tid = transaction->t_tid;
1484 if (atomic_dec_and_test(&transaction->t_updates)) {
1485 wake_up(&journal->j_wait_updates);
1486 if (journal->j_barrier_count)
1487 wake_up(&journal->j_wait_transaction_locked);
1488 }
1489
1490 if (wait_for_commit)
1491 err = jbd2_log_wait_commit(journal, tid);
1492
1493 lock_map_release(&handle->h_lockdep_map);
1494
1495 jbd2_free_handle(handle);
1496 return err;
1497}
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507int jbd2_journal_force_commit(journal_t *journal)
1508{
1509 handle_t *handle;
1510 int ret;
1511
1512 handle = jbd2_journal_start(journal, 1);
1513 if (IS_ERR(handle)) {
1514 ret = PTR_ERR(handle);
1515 } else {
1516 handle->h_sync = 1;
1517 ret = jbd2_journal_stop(handle);
1518 }
1519 return ret;
1520}
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538static inline void
1539__blist_add_buffer(struct journal_head **list, struct journal_head *jh)
1540{
1541 if (!*list) {
1542 jh->b_tnext = jh->b_tprev = jh;
1543 *list = jh;
1544 } else {
1545
1546 struct journal_head *first = *list, *last = first->b_tprev;
1547 jh->b_tprev = last;
1548 jh->b_tnext = first;
1549 last->b_tnext = first->b_tprev = jh;
1550 }
1551}
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562static inline void
1563__blist_del_buffer(struct journal_head **list, struct journal_head *jh)
1564{
1565 if (*list == jh) {
1566 *list = jh->b_tnext;
1567 if (*list == jh)
1568 *list = NULL;
1569 }
1570 jh->b_tprev->b_tnext = jh->b_tnext;
1571 jh->b_tnext->b_tprev = jh->b_tprev;
1572}
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh)
1586{
1587 struct journal_head **list = NULL;
1588 transaction_t *transaction;
1589 struct buffer_head *bh = jh2bh(jh);
1590
1591 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1592 transaction = jh->b_transaction;
1593 if (transaction)
1594 assert_spin_locked(&transaction->t_journal->j_list_lock);
1595
1596 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1597 if (jh->b_jlist != BJ_None)
1598 J_ASSERT_JH(jh, transaction != NULL);
1599
1600 switch (jh->b_jlist) {
1601 case BJ_None:
1602 return;
1603 case BJ_Metadata:
1604 transaction->t_nr_buffers--;
1605 J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
1606 list = &transaction->t_buffers;
1607 break;
1608 case BJ_Forget:
1609 list = &transaction->t_forget;
1610 break;
1611 case BJ_IO:
1612 list = &transaction->t_iobuf_list;
1613 break;
1614 case BJ_Shadow:
1615 list = &transaction->t_shadow_list;
1616 break;
1617 case BJ_LogCtl:
1618 list = &transaction->t_log_list;
1619 break;
1620 case BJ_Reserved:
1621 list = &transaction->t_reserved_list;
1622 break;
1623 }
1624
1625 __blist_del_buffer(list, jh);
1626 jh->b_jlist = BJ_None;
1627 if (test_clear_buffer_jbddirty(bh))
1628 mark_buffer_dirty(bh);
1629}
1630
1631
1632
1633
1634
1635
1636
1637
1638static void __jbd2_journal_unfile_buffer(struct journal_head *jh)
1639{
1640 __jbd2_journal_temp_unlink_buffer(jh);
1641 jh->b_transaction = NULL;
1642 jbd2_journal_put_journal_head(jh);
1643}
1644
1645void jbd2_journal_unfile_buffer(journal_t *journal, struct journal_head *jh)
1646{
1647 struct buffer_head *bh = jh2bh(jh);
1648
1649
1650 get_bh(bh);
1651 jbd_lock_bh_state(bh);
1652 spin_lock(&journal->j_list_lock);
1653 __jbd2_journal_unfile_buffer(jh);
1654 spin_unlock(&journal->j_list_lock);
1655 jbd_unlock_bh_state(bh);
1656 __brelse(bh);
1657}
1658
1659
1660
1661
1662
1663
1664static void
1665__journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh)
1666{
1667 struct journal_head *jh;
1668
1669 jh = bh2jh(bh);
1670
1671 if (buffer_locked(bh) || buffer_dirty(bh))
1672 goto out;
1673
1674 if (jh->b_next_transaction != NULL)
1675 goto out;
1676
1677 spin_lock(&journal->j_list_lock);
1678 if (jh->b_cp_transaction != NULL && jh->b_transaction == NULL) {
1679
1680 JBUFFER_TRACE(jh, "remove from checkpoint list");
1681 __jbd2_journal_remove_checkpoint(jh);
1682 }
1683 spin_unlock(&journal->j_list_lock);
1684out:
1685 return;
1686}
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726int jbd2_journal_try_to_free_buffers(journal_t *journal,
1727 struct page *page, gfp_t gfp_mask)
1728{
1729 struct buffer_head *head;
1730 struct buffer_head *bh;
1731 int ret = 0;
1732
1733 J_ASSERT(PageLocked(page));
1734
1735 head = page_buffers(page);
1736 bh = head;
1737 do {
1738 struct journal_head *jh;
1739
1740
1741
1742
1743
1744
1745 jh = jbd2_journal_grab_journal_head(bh);
1746 if (!jh)
1747 continue;
1748
1749 jbd_lock_bh_state(bh);
1750 __journal_try_to_free_buffer(journal, bh);
1751 jbd2_journal_put_journal_head(jh);
1752 jbd_unlock_bh_state(bh);
1753 if (buffer_jbd(bh))
1754 goto busy;
1755 } while ((bh = bh->b_this_page) != head);
1756
1757 ret = try_to_free_buffers(page);
1758
1759busy:
1760 return ret;
1761}
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
1776{
1777 int may_free = 1;
1778 struct buffer_head *bh = jh2bh(jh);
1779
1780 if (jh->b_cp_transaction) {
1781 JBUFFER_TRACE(jh, "on running+cp transaction");
1782 __jbd2_journal_temp_unlink_buffer(jh);
1783
1784
1785
1786
1787
1788 clear_buffer_dirty(bh);
1789 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
1790 may_free = 0;
1791 } else {
1792 JBUFFER_TRACE(jh, "on running transaction");
1793 __jbd2_journal_unfile_buffer(jh);
1794 }
1795 return may_free;
1796}
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh,
1846 int partial_page)
1847{
1848 transaction_t *transaction;
1849 struct journal_head *jh;
1850 int may_free = 1;
1851
1852 BUFFER_TRACE(bh, "entry");
1853
1854retry:
1855
1856
1857
1858
1859
1860
1861 if (!buffer_jbd(bh))
1862 goto zap_buffer_unlocked;
1863
1864
1865 write_lock(&journal->j_state_lock);
1866 jbd_lock_bh_state(bh);
1867 spin_lock(&journal->j_list_lock);
1868
1869 jh = jbd2_journal_grab_journal_head(bh);
1870 if (!jh)
1871 goto zap_buffer_no_jh;
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896 transaction = jh->b_transaction;
1897 if (transaction == NULL) {
1898
1899
1900
1901
1902 if (!jh->b_cp_transaction) {
1903 JBUFFER_TRACE(jh, "not on any transaction: zap");
1904 goto zap_buffer;
1905 }
1906
1907 if (!buffer_dirty(bh)) {
1908
1909 goto zap_buffer;
1910 }
1911
1912
1913
1914
1915
1916 if (journal->j_running_transaction) {
1917
1918
1919
1920 JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
1921 may_free = __dispose_buffer(jh,
1922 journal->j_running_transaction);
1923 goto zap_buffer;
1924 } else {
1925
1926
1927
1928
1929 if (journal->j_committing_transaction) {
1930 JBUFFER_TRACE(jh, "give to committing trans");
1931 may_free = __dispose_buffer(jh,
1932 journal->j_committing_transaction);
1933 goto zap_buffer;
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
1944
1945
1946
1947
1948 if (partial_page) {
1949 tid_t tid = journal->j_committing_transaction->t_tid;
1950
1951 jbd2_journal_put_journal_head(jh);
1952 spin_unlock(&journal->j_list_lock);
1953 jbd_unlock_bh_state(bh);
1954 write_unlock(&journal->j_state_lock);
1955 jbd2_log_wait_commit(journal, tid);
1956 goto retry;
1957 }
1958
1959
1960
1961
1962
1963
1964 set_buffer_freed(bh);
1965 if (journal->j_running_transaction && buffer_jbddirty(bh))
1966 jh->b_next_transaction = journal->j_running_transaction;
1967 jbd2_journal_put_journal_head(jh);
1968 spin_unlock(&journal->j_list_lock);
1969 jbd_unlock_bh_state(bh);
1970 write_unlock(&journal->j_state_lock);
1971 return 0;
1972 } else {
1973
1974
1975
1976
1977
1978
1979 J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
1980 JBUFFER_TRACE(jh, "on running transaction");
1981 may_free = __dispose_buffer(jh, transaction);
1982 }
1983
1984zap_buffer:
1985
1986
1987
1988
1989
1990
1991
1992
1993 jh->b_modified = 0;
1994 jbd2_journal_put_journal_head(jh);
1995zap_buffer_no_jh:
1996 spin_unlock(&journal->j_list_lock);
1997 jbd_unlock_bh_state(bh);
1998 write_unlock(&journal->j_state_lock);
1999zap_buffer_unlocked:
2000 clear_buffer_dirty(bh);
2001 J_ASSERT_BH(bh, !buffer_jbddirty(bh));
2002 clear_buffer_mapped(bh);
2003 clear_buffer_req(bh);
2004 clear_buffer_new(bh);
2005 clear_buffer_delay(bh);
2006 clear_buffer_unwritten(bh);
2007 bh->b_bdev = NULL;
2008 return may_free;
2009}
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020void jbd2_journal_invalidatepage(journal_t *journal,
2021 struct page *page,
2022 unsigned long offset)
2023{
2024 struct buffer_head *head, *bh, *next;
2025 unsigned int curr_off = 0;
2026 int may_free = 1;
2027
2028 if (!PageLocked(page))
2029 BUG();
2030 if (!page_has_buffers(page))
2031 return;
2032
2033
2034
2035
2036
2037 head = bh = page_buffers(page);
2038 do {
2039 unsigned int next_off = curr_off + bh->b_size;
2040 next = bh->b_this_page;
2041
2042 if (offset <= curr_off) {
2043
2044 lock_buffer(bh);
2045 may_free &= journal_unmap_buffer(journal, bh,
2046 offset > 0);
2047 unlock_buffer(bh);
2048 }
2049 curr_off = next_off;
2050 bh = next;
2051
2052 } while (bh != head);
2053
2054 if (!offset) {
2055 if (may_free && try_to_free_buffers(page))
2056 J_ASSERT(!page_has_buffers(page));
2057 }
2058}
2059
2060
2061
2062
2063void __jbd2_journal_file_buffer(struct journal_head *jh,
2064 transaction_t *transaction, int jlist)
2065{
2066 struct journal_head **list = NULL;
2067 int was_dirty = 0;
2068 struct buffer_head *bh = jh2bh(jh);
2069
2070 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
2071 assert_spin_locked(&transaction->t_journal->j_list_lock);
2072
2073 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
2074 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
2075 jh->b_transaction == NULL);
2076
2077 if (jh->b_transaction && jh->b_jlist == jlist)
2078 return;
2079
2080 if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
2081 jlist == BJ_Shadow || jlist == BJ_Forget) {
2082
2083
2084
2085
2086
2087
2088
2089 if (buffer_dirty(bh))
2090 warn_dirty_buffer(bh);
2091 if (test_clear_buffer_dirty(bh) ||
2092 test_clear_buffer_jbddirty(bh))
2093 was_dirty = 1;
2094 }
2095
2096 if (jh->b_transaction)
2097 __jbd2_journal_temp_unlink_buffer(jh);
2098 else
2099 jbd2_journal_grab_journal_head(bh);
2100 jh->b_transaction = transaction;
2101
2102 switch (jlist) {
2103 case BJ_None:
2104 J_ASSERT_JH(jh, !jh->b_committed_data);
2105 J_ASSERT_JH(jh, !jh->b_frozen_data);
2106 return;
2107 case BJ_Metadata:
2108 transaction->t_nr_buffers++;
2109 list = &transaction->t_buffers;
2110 break;
2111 case BJ_Forget:
2112 list = &transaction->t_forget;
2113 break;
2114 case BJ_IO:
2115 list = &transaction->t_iobuf_list;
2116 break;
2117 case BJ_Shadow:
2118 list = &transaction->t_shadow_list;
2119 break;
2120 case BJ_LogCtl:
2121 list = &transaction->t_log_list;
2122 break;
2123 case BJ_Reserved:
2124 list = &transaction->t_reserved_list;
2125 break;
2126 }
2127
2128 __blist_add_buffer(list, jh);
2129 jh->b_jlist = jlist;
2130
2131 if (was_dirty)
2132 set_buffer_jbddirty(bh);
2133}
2134
2135void jbd2_journal_file_buffer(struct journal_head *jh,
2136 transaction_t *transaction, int jlist)
2137{
2138 jbd_lock_bh_state(jh2bh(jh));
2139 spin_lock(&transaction->t_journal->j_list_lock);
2140 __jbd2_journal_file_buffer(jh, transaction, jlist);
2141 spin_unlock(&transaction->t_journal->j_list_lock);
2142 jbd_unlock_bh_state(jh2bh(jh));
2143}
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156void __jbd2_journal_refile_buffer(struct journal_head *jh)
2157{
2158 int was_dirty, jlist;
2159 struct buffer_head *bh = jh2bh(jh);
2160
2161 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
2162 if (jh->b_transaction)
2163 assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
2164
2165
2166 if (jh->b_next_transaction == NULL) {
2167 __jbd2_journal_unfile_buffer(jh);
2168 return;
2169 }
2170
2171
2172
2173
2174
2175
2176 was_dirty = test_clear_buffer_jbddirty(bh);
2177 __jbd2_journal_temp_unlink_buffer(jh);
2178
2179
2180
2181
2182
2183 jh->b_transaction = jh->b_next_transaction;
2184 jh->b_next_transaction = NULL;
2185 if (buffer_freed(bh))
2186 jlist = BJ_Forget;
2187 else if (jh->b_modified)
2188 jlist = BJ_Metadata;
2189 else
2190 jlist = BJ_Reserved;
2191 __jbd2_journal_file_buffer(jh, jh->b_transaction, jlist);
2192 J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
2193
2194 if (was_dirty)
2195 set_buffer_jbddirty(bh);
2196}
2197
2198
2199
2200
2201
2202
2203
2204void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh)
2205{
2206 struct buffer_head *bh = jh2bh(jh);
2207
2208
2209 get_bh(bh);
2210 jbd_lock_bh_state(bh);
2211 spin_lock(&journal->j_list_lock);
2212 __jbd2_journal_refile_buffer(jh);
2213 jbd_unlock_bh_state(bh);
2214 spin_unlock(&journal->j_list_lock);
2215 __brelse(bh);
2216}
2217
2218
2219
2220
2221int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode)
2222{
2223 transaction_t *transaction = handle->h_transaction;
2224 journal_t *journal = transaction->t_journal;
2225
2226 if (is_handle_aborted(handle))
2227 return -EIO;
2228
2229 jbd_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino,
2230 transaction->t_tid);
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245 if (jinode->i_transaction == transaction ||
2246 jinode->i_next_transaction == transaction)
2247 return 0;
2248
2249 spin_lock(&journal->j_list_lock);
2250
2251 if (jinode->i_transaction == transaction ||
2252 jinode->i_next_transaction == transaction)
2253 goto done;
2254
2255
2256
2257
2258
2259
2260 if (!transaction->t_need_data_flush)
2261 transaction->t_need_data_flush = 1;
2262
2263
2264 if (jinode->i_transaction) {
2265 J_ASSERT(jinode->i_next_transaction == NULL);
2266 J_ASSERT(jinode->i_transaction ==
2267 journal->j_committing_transaction);
2268 jinode->i_next_transaction = transaction;
2269 goto done;
2270 }
2271
2272 J_ASSERT(!jinode->i_next_transaction);
2273 jinode->i_transaction = transaction;
2274 list_add(&jinode->i_list, &transaction->t_inode_list);
2275done:
2276 spin_unlock(&journal->j_list_lock);
2277
2278 return 0;
2279}
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301int jbd2_journal_begin_ordered_truncate(journal_t *journal,
2302 struct jbd2_inode *jinode,
2303 loff_t new_size)
2304{
2305 transaction_t *inode_trans, *commit_trans;
2306 int ret = 0;
2307
2308
2309 if (!jinode->i_transaction)
2310 goto out;
2311
2312
2313
2314 read_lock(&journal->j_state_lock);
2315 commit_trans = journal->j_committing_transaction;
2316 read_unlock(&journal->j_state_lock);
2317 spin_lock(&journal->j_list_lock);
2318 inode_trans = jinode->i_transaction;
2319 spin_unlock(&journal->j_list_lock);
2320 if (inode_trans == commit_trans) {
2321 ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping,
2322 new_size, LLONG_MAX);
2323 if (ret)
2324 jbd2_journal_abort(journal, ret);
2325 }
2326out:
2327 return ret;
2328}
2329