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 jbd2_get_transaction(journal, new_transaction);
214 new_transaction = NULL;
215 }
216 write_unlock(&journal->j_state_lock);
217 goto repeat;
218 }
219
220 transaction = journal->j_running_transaction;
221
222
223
224
225
226 if (transaction->t_state == T_LOCKED) {
227 DEFINE_WAIT(wait);
228
229 prepare_to_wait(&journal->j_wait_transaction_locked,
230 &wait, TASK_UNINTERRUPTIBLE);
231 read_unlock(&journal->j_state_lock);
232 schedule();
233 finish_wait(&journal->j_wait_transaction_locked, &wait);
234 goto repeat;
235 }
236
237
238
239
240
241
242 needed = atomic_add_return(nblocks,
243 &transaction->t_outstanding_credits);
244
245 if (needed > journal->j_max_transaction_buffers) {
246
247
248
249
250
251 DEFINE_WAIT(wait);
252
253 jbd_debug(2, "Handle %p starting new commit...\n", handle);
254 atomic_sub(nblocks, &transaction->t_outstanding_credits);
255 prepare_to_wait(&journal->j_wait_transaction_locked, &wait,
256 TASK_UNINTERRUPTIBLE);
257 tid = transaction->t_tid;
258 need_to_start = !tid_geq(journal->j_commit_request, tid);
259 read_unlock(&journal->j_state_lock);
260 if (need_to_start)
261 jbd2_log_start_commit(journal, tid);
262 schedule();
263 finish_wait(&journal->j_wait_transaction_locked, &wait);
264 goto repeat;
265 }
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 if (__jbd2_log_space_left(journal) < jbd_space_needed(journal)) {
293 jbd_debug(2, "Handle %p waiting for checkpoint...\n", handle);
294 atomic_sub(nblocks, &transaction->t_outstanding_credits);
295 read_unlock(&journal->j_state_lock);
296 write_lock(&journal->j_state_lock);
297 if (__jbd2_log_space_left(journal) < jbd_space_needed(journal))
298 __jbd2_log_wait_for_space(journal);
299 write_unlock(&journal->j_state_lock);
300 goto repeat;
301 }
302
303
304
305
306 update_t_max_wait(transaction, ts);
307 handle->h_transaction = transaction;
308 atomic_inc(&transaction->t_updates);
309 atomic_inc(&transaction->t_handle_count);
310 jbd_debug(4, "Handle %p given %d credits (total %d, free %d)\n",
311 handle, nblocks,
312 atomic_read(&transaction->t_outstanding_credits),
313 __jbd2_log_space_left(journal));
314 read_unlock(&journal->j_state_lock);
315
316 lock_map_acquire(&handle->h_lockdep_map);
317 jbd2_journal_free_transaction(new_transaction);
318 return 0;
319}
320
321static struct lock_class_key jbd2_handle_key;
322
323
324static handle_t *new_handle(int nblocks)
325{
326 handle_t *handle = jbd2_alloc_handle(GFP_NOFS);
327 if (!handle)
328 return NULL;
329 memset(handle, 0, sizeof(*handle));
330 handle->h_buffer_credits = nblocks;
331 handle->h_ref = 1;
332
333 lockdep_init_map(&handle->h_lockdep_map, "jbd2_handle",
334 &jbd2_handle_key, 0);
335
336 return handle;
337}
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354handle_t *jbd2__journal_start(journal_t *journal, int nblocks, gfp_t gfp_mask)
355{
356 handle_t *handle = journal_current_handle();
357 int err;
358
359 if (!journal)
360 return ERR_PTR(-EROFS);
361
362 if (handle) {
363 J_ASSERT(handle->h_transaction->t_journal == journal);
364 handle->h_ref++;
365 return handle;
366 }
367
368 handle = new_handle(nblocks);
369 if (!handle)
370 return ERR_PTR(-ENOMEM);
371
372 current->journal_info = handle;
373
374 err = start_this_handle(journal, handle, gfp_mask);
375 if (err < 0) {
376 jbd2_free_handle(handle);
377 current->journal_info = NULL;
378 handle = ERR_PTR(err);
379 }
380 return handle;
381}
382EXPORT_SYMBOL(jbd2__journal_start);
383
384
385handle_t *jbd2_journal_start(journal_t *journal, int nblocks)
386{
387 return jbd2__journal_start(journal, nblocks, GFP_NOFS);
388}
389EXPORT_SYMBOL(jbd2_journal_start);
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412int jbd2_journal_extend(handle_t *handle, int nblocks)
413{
414 transaction_t *transaction = handle->h_transaction;
415 journal_t *journal = transaction->t_journal;
416 int result;
417 int wanted;
418
419 result = -EIO;
420 if (is_handle_aborted(handle))
421 goto out;
422
423 result = 1;
424
425 read_lock(&journal->j_state_lock);
426
427
428 if (handle->h_transaction->t_state != T_RUNNING) {
429 jbd_debug(3, "denied handle %p %d blocks: "
430 "transaction not running\n", handle, nblocks);
431 goto error_out;
432 }
433
434 spin_lock(&transaction->t_handle_lock);
435 wanted = atomic_read(&transaction->t_outstanding_credits) + nblocks;
436
437 if (wanted > journal->j_max_transaction_buffers) {
438 jbd_debug(3, "denied handle %p %d blocks: "
439 "transaction too large\n", handle, nblocks);
440 goto unlock;
441 }
442
443 if (wanted > __jbd2_log_space_left(journal)) {
444 jbd_debug(3, "denied handle %p %d blocks: "
445 "insufficient log space\n", handle, nblocks);
446 goto unlock;
447 }
448
449 handle->h_buffer_credits += nblocks;
450 atomic_add(nblocks, &transaction->t_outstanding_credits);
451 result = 0;
452
453 jbd_debug(3, "extended handle %p by %d\n", handle, nblocks);
454unlock:
455 spin_unlock(&transaction->t_handle_lock);
456error_out:
457 read_unlock(&journal->j_state_lock);
458out:
459 return result;
460}
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477int jbd2__journal_restart(handle_t *handle, int nblocks, gfp_t gfp_mask)
478{
479 transaction_t *transaction = handle->h_transaction;
480 journal_t *journal = transaction->t_journal;
481 tid_t tid;
482 int need_to_start, ret;
483
484
485
486 if (is_handle_aborted(handle))
487 return 0;
488
489
490
491
492
493 J_ASSERT(atomic_read(&transaction->t_updates) > 0);
494 J_ASSERT(journal_current_handle() == handle);
495
496 read_lock(&journal->j_state_lock);
497 spin_lock(&transaction->t_handle_lock);
498 atomic_sub(handle->h_buffer_credits,
499 &transaction->t_outstanding_credits);
500 if (atomic_dec_and_test(&transaction->t_updates))
501 wake_up(&journal->j_wait_updates);
502 spin_unlock(&transaction->t_handle_lock);
503
504 jbd_debug(2, "restarting handle %p\n", handle);
505 tid = transaction->t_tid;
506 need_to_start = !tid_geq(journal->j_commit_request, tid);
507 read_unlock(&journal->j_state_lock);
508 if (need_to_start)
509 jbd2_log_start_commit(journal, tid);
510
511 lock_map_release(&handle->h_lockdep_map);
512 handle->h_buffer_credits = nblocks;
513 ret = start_this_handle(journal, handle, gfp_mask);
514 return ret;
515}
516EXPORT_SYMBOL(jbd2__journal_restart);
517
518
519int jbd2_journal_restart(handle_t *handle, int nblocks)
520{
521 return jbd2__journal_restart(handle, nblocks, GFP_NOFS);
522}
523EXPORT_SYMBOL(jbd2_journal_restart);
524
525
526
527
528
529
530
531
532
533
534
535void jbd2_journal_lock_updates(journal_t *journal)
536{
537 DEFINE_WAIT(wait);
538
539 write_lock(&journal->j_state_lock);
540 ++journal->j_barrier_count;
541
542
543 while (1) {
544 transaction_t *transaction = journal->j_running_transaction;
545
546 if (!transaction)
547 break;
548
549 spin_lock(&transaction->t_handle_lock);
550 prepare_to_wait(&journal->j_wait_updates, &wait,
551 TASK_UNINTERRUPTIBLE);
552 if (!atomic_read(&transaction->t_updates)) {
553 spin_unlock(&transaction->t_handle_lock);
554 finish_wait(&journal->j_wait_updates, &wait);
555 break;
556 }
557 spin_unlock(&transaction->t_handle_lock);
558 write_unlock(&journal->j_state_lock);
559 schedule();
560 finish_wait(&journal->j_wait_updates, &wait);
561 write_lock(&journal->j_state_lock);
562 }
563 write_unlock(&journal->j_state_lock);
564
565
566
567
568
569
570
571 mutex_lock(&journal->j_barrier);
572}
573
574
575
576
577
578
579
580
581
582void jbd2_journal_unlock_updates (journal_t *journal)
583{
584 J_ASSERT(journal->j_barrier_count != 0);
585
586 mutex_unlock(&journal->j_barrier);
587 write_lock(&journal->j_state_lock);
588 --journal->j_barrier_count;
589 write_unlock(&journal->j_state_lock);
590 wake_up(&journal->j_wait_transaction_locked);
591}
592
593static void warn_dirty_buffer(struct buffer_head *bh)
594{
595 char b[BDEVNAME_SIZE];
596
597 printk(KERN_WARNING
598 "JBD2: Spotted dirty metadata buffer (dev = %s, blocknr = %llu). "
599 "There's a risk of filesystem corruption in case of system "
600 "crash.\n",
601 bdevname(bh->b_bdev, b), (unsigned long long)bh->b_blocknr);
602}
603
604
605
606
607
608
609
610
611
612
613
614static int
615do_get_write_access(handle_t *handle, struct journal_head *jh,
616 int force_copy)
617{
618 struct buffer_head *bh;
619 transaction_t *transaction;
620 journal_t *journal;
621 int error;
622 char *frozen_buffer = NULL;
623 int need_copy = 0;
624
625 if (is_handle_aborted(handle))
626 return -EROFS;
627
628 transaction = handle->h_transaction;
629 journal = transaction->t_journal;
630
631 jbd_debug(5, "journal_head %p, force_copy %d\n", jh, force_copy);
632
633 JBUFFER_TRACE(jh, "entry");
634repeat:
635 bh = jh2bh(jh);
636
637
638
639 lock_buffer(bh);
640 jbd_lock_bh_state(bh);
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655 if (buffer_dirty(bh)) {
656
657
658
659
660 if (jh->b_transaction) {
661 J_ASSERT_JH(jh,
662 jh->b_transaction == transaction ||
663 jh->b_transaction ==
664 journal->j_committing_transaction);
665 if (jh->b_next_transaction)
666 J_ASSERT_JH(jh, jh->b_next_transaction ==
667 transaction);
668 warn_dirty_buffer(bh);
669 }
670
671
672
673
674
675 JBUFFER_TRACE(jh, "Journalling dirty buffer");
676 clear_buffer_dirty(bh);
677 set_buffer_jbddirty(bh);
678 }
679
680 unlock_buffer(bh);
681
682 error = -EROFS;
683 if (is_handle_aborted(handle)) {
684 jbd_unlock_bh_state(bh);
685 goto out;
686 }
687 error = 0;
688
689
690
691
692
693 if (jh->b_transaction == transaction ||
694 jh->b_next_transaction == transaction)
695 goto done;
696
697
698
699
700
701 jh->b_modified = 0;
702
703
704
705
706
707 if (jh->b_frozen_data) {
708 JBUFFER_TRACE(jh, "has frozen data");
709 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
710 jh->b_next_transaction = transaction;
711 goto done;
712 }
713
714
715
716 if (jh->b_transaction && jh->b_transaction != transaction) {
717 JBUFFER_TRACE(jh, "owned by older transaction");
718 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
719 J_ASSERT_JH(jh, jh->b_transaction ==
720 journal->j_committing_transaction);
721
722
723
724
725
726
727
728
729
730
731 if (jh->b_jlist == BJ_Shadow) {
732 DEFINE_WAIT_BIT(wait, &bh->b_state, BH_Unshadow);
733 wait_queue_head_t *wqh;
734
735 wqh = bit_waitqueue(&bh->b_state, BH_Unshadow);
736
737 JBUFFER_TRACE(jh, "on shadow: sleep");
738 jbd_unlock_bh_state(bh);
739
740 for ( ; ; ) {
741 prepare_to_wait(wqh, &wait.wait,
742 TASK_UNINTERRUPTIBLE);
743 if (jh->b_jlist != BJ_Shadow)
744 break;
745 schedule();
746 }
747 finish_wait(wqh, &wait.wait);
748 goto repeat;
749 }
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765 if (jh->b_jlist != BJ_Forget || force_copy) {
766 JBUFFER_TRACE(jh, "generate frozen data");
767 if (!frozen_buffer) {
768 JBUFFER_TRACE(jh, "allocate memory for buffer");
769 jbd_unlock_bh_state(bh);
770 frozen_buffer =
771 jbd2_alloc(jh2bh(jh)->b_size,
772 GFP_NOFS);
773 if (!frozen_buffer) {
774 printk(KERN_EMERG
775 "%s: OOM for frozen_buffer\n",
776 __func__);
777 JBUFFER_TRACE(jh, "oom!");
778 error = -ENOMEM;
779 jbd_lock_bh_state(bh);
780 goto done;
781 }
782 goto repeat;
783 }
784 jh->b_frozen_data = frozen_buffer;
785 frozen_buffer = NULL;
786 need_copy = 1;
787 }
788 jh->b_next_transaction = transaction;
789 }
790
791
792
793
794
795
796
797 if (!jh->b_transaction) {
798 JBUFFER_TRACE(jh, "no transaction");
799 J_ASSERT_JH(jh, !jh->b_next_transaction);
800 JBUFFER_TRACE(jh, "file as BJ_Reserved");
801 spin_lock(&journal->j_list_lock);
802 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
803 spin_unlock(&journal->j_list_lock);
804 }
805
806done:
807 if (need_copy) {
808 struct page *page;
809 int offset;
810 char *source;
811
812 J_EXPECT_JH(jh, buffer_uptodate(jh2bh(jh)),
813 "Possible IO failure.\n");
814 page = jh2bh(jh)->b_page;
815 offset = offset_in_page(jh2bh(jh)->b_data);
816 source = kmap_atomic(page);
817
818 jbd2_buffer_frozen_trigger(jh, source + offset,
819 jh->b_triggers);
820 memcpy(jh->b_frozen_data, source+offset, jh2bh(jh)->b_size);
821 kunmap_atomic(source);
822
823
824
825
826
827 jh->b_frozen_triggers = jh->b_triggers;
828 }
829 jbd_unlock_bh_state(bh);
830
831
832
833
834
835 jbd2_journal_cancel_revoke(handle, jh);
836
837out:
838 if (unlikely(frozen_buffer))
839 jbd2_free(frozen_buffer, bh->b_size);
840
841 JBUFFER_TRACE(jh, "exit");
842 return error;
843}
844
845
846
847
848
849
850
851
852
853
854
855
856int jbd2_journal_get_write_access(handle_t *handle, struct buffer_head *bh)
857{
858 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
859 int rc;
860
861
862
863
864 rc = do_get_write_access(handle, jh, 0);
865 jbd2_journal_put_journal_head(jh);
866 return rc;
867}
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889int jbd2_journal_get_create_access(handle_t *handle, struct buffer_head *bh)
890{
891 transaction_t *transaction = handle->h_transaction;
892 journal_t *journal = transaction->t_journal;
893 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
894 int err;
895
896 jbd_debug(5, "journal_head %p\n", jh);
897 err = -EROFS;
898 if (is_handle_aborted(handle))
899 goto out;
900 err = 0;
901
902 JBUFFER_TRACE(jh, "entry");
903
904
905
906
907
908
909
910 jbd_lock_bh_state(bh);
911 spin_lock(&journal->j_list_lock);
912 J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
913 jh->b_transaction == NULL ||
914 (jh->b_transaction == journal->j_committing_transaction &&
915 jh->b_jlist == BJ_Forget)));
916
917 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
918 J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
919
920 if (jh->b_transaction == NULL) {
921
922
923
924
925
926
927
928
929 clear_buffer_dirty(jh2bh(jh));
930
931 jh->b_modified = 0;
932
933 JBUFFER_TRACE(jh, "file as BJ_Reserved");
934 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
935 } else if (jh->b_transaction == journal->j_committing_transaction) {
936
937 jh->b_modified = 0;
938
939 JBUFFER_TRACE(jh, "set next transaction");
940 jh->b_next_transaction = transaction;
941 }
942 spin_unlock(&journal->j_list_lock);
943 jbd_unlock_bh_state(bh);
944
945
946
947
948
949
950
951
952 JBUFFER_TRACE(jh, "cancelling revoke");
953 jbd2_journal_cancel_revoke(handle, jh);
954out:
955 jbd2_journal_put_journal_head(jh);
956 return err;
957}
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
985int jbd2_journal_get_undo_access(handle_t *handle, struct buffer_head *bh)
986{
987 int err;
988 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
989 char *committed_data = NULL;
990
991 JBUFFER_TRACE(jh, "entry");
992
993
994
995
996
997
998 err = do_get_write_access(handle, jh, 1);
999 if (err)
1000 goto out;
1001
1002repeat:
1003 if (!jh->b_committed_data) {
1004 committed_data = jbd2_alloc(jh2bh(jh)->b_size, GFP_NOFS);
1005 if (!committed_data) {
1006 printk(KERN_EMERG "%s: No memory for committed data\n",
1007 __func__);
1008 err = -ENOMEM;
1009 goto out;
1010 }
1011 }
1012
1013 jbd_lock_bh_state(bh);
1014 if (!jh->b_committed_data) {
1015
1016
1017 JBUFFER_TRACE(jh, "generate b_committed data");
1018 if (!committed_data) {
1019 jbd_unlock_bh_state(bh);
1020 goto repeat;
1021 }
1022
1023 jh->b_committed_data = committed_data;
1024 committed_data = NULL;
1025 memcpy(jh->b_committed_data, bh->b_data, bh->b_size);
1026 }
1027 jbd_unlock_bh_state(bh);
1028out:
1029 jbd2_journal_put_journal_head(jh);
1030 if (unlikely(committed_data))
1031 jbd2_free(committed_data, bh->b_size);
1032 return err;
1033}
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046void jbd2_journal_set_triggers(struct buffer_head *bh,
1047 struct jbd2_buffer_trigger_type *type)
1048{
1049 struct journal_head *jh = bh2jh(bh);
1050
1051 jh->b_triggers = type;
1052}
1053
1054void jbd2_buffer_frozen_trigger(struct journal_head *jh, void *mapped_data,
1055 struct jbd2_buffer_trigger_type *triggers)
1056{
1057 struct buffer_head *bh = jh2bh(jh);
1058
1059 if (!triggers || !triggers->t_frozen)
1060 return;
1061
1062 triggers->t_frozen(triggers, bh, mapped_data, bh->b_size);
1063}
1064
1065void jbd2_buffer_abort_trigger(struct journal_head *jh,
1066 struct jbd2_buffer_trigger_type *triggers)
1067{
1068 if (!triggers || !triggers->t_abort)
1069 return;
1070
1071 triggers->t_abort(triggers, jh2bh(jh));
1072}
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
1099int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
1100{
1101 transaction_t *transaction = handle->h_transaction;
1102 journal_t *journal = transaction->t_journal;
1103 struct journal_head *jh = bh2jh(bh);
1104 int ret = 0;
1105
1106 jbd_debug(5, "journal_head %p\n", jh);
1107 JBUFFER_TRACE(jh, "entry");
1108 if (is_handle_aborted(handle))
1109 goto out;
1110 if (!buffer_jbd(bh)) {
1111 ret = -EUCLEAN;
1112 goto out;
1113 }
1114
1115 jbd_lock_bh_state(bh);
1116
1117 if (jh->b_modified == 0) {
1118
1119
1120
1121
1122
1123 jh->b_modified = 1;
1124 J_ASSERT_JH(jh, handle->h_buffer_credits > 0);
1125 handle->h_buffer_credits--;
1126 }
1127
1128
1129
1130
1131
1132
1133
1134
1135 if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) {
1136 JBUFFER_TRACE(jh, "fastpath");
1137 if (unlikely(jh->b_transaction !=
1138 journal->j_running_transaction)) {
1139 printk(KERN_EMERG "JBD: %s: "
1140 "jh->b_transaction (%llu, %p, %u) != "
1141 "journal->j_running_transaction (%p, %u)",
1142 journal->j_devname,
1143 (unsigned long long) bh->b_blocknr,
1144 jh->b_transaction,
1145 jh->b_transaction ? jh->b_transaction->t_tid : 0,
1146 journal->j_running_transaction,
1147 journal->j_running_transaction ?
1148 journal->j_running_transaction->t_tid : 0);
1149 ret = -EINVAL;
1150 }
1151 goto out_unlock_bh;
1152 }
1153
1154 set_buffer_jbddirty(bh);
1155
1156
1157
1158
1159
1160
1161
1162 if (jh->b_transaction != transaction) {
1163 JBUFFER_TRACE(jh, "already on other transaction");
1164 if (unlikely(jh->b_transaction !=
1165 journal->j_committing_transaction)) {
1166 printk(KERN_EMERG "JBD: %s: "
1167 "jh->b_transaction (%llu, %p, %u) != "
1168 "journal->j_committing_transaction (%p, %u)",
1169 journal->j_devname,
1170 (unsigned long long) bh->b_blocknr,
1171 jh->b_transaction,
1172 jh->b_transaction ? jh->b_transaction->t_tid : 0,
1173 journal->j_committing_transaction,
1174 journal->j_committing_transaction ?
1175 journal->j_committing_transaction->t_tid : 0);
1176 ret = -EINVAL;
1177 }
1178 if (unlikely(jh->b_next_transaction != transaction)) {
1179 printk(KERN_EMERG "JBD: %s: "
1180 "jh->b_next_transaction (%llu, %p, %u) != "
1181 "transaction (%p, %u)",
1182 journal->j_devname,
1183 (unsigned long long) bh->b_blocknr,
1184 jh->b_next_transaction,
1185 jh->b_next_transaction ?
1186 jh->b_next_transaction->t_tid : 0,
1187 transaction, transaction->t_tid);
1188 ret = -EINVAL;
1189 }
1190
1191
1192 goto out_unlock_bh;
1193 }
1194
1195
1196 J_ASSERT_JH(jh, jh->b_frozen_data == NULL);
1197
1198 JBUFFER_TRACE(jh, "file as BJ_Metadata");
1199 spin_lock(&journal->j_list_lock);
1200 __jbd2_journal_file_buffer(jh, handle->h_transaction, BJ_Metadata);
1201 spin_unlock(&journal->j_list_lock);
1202out_unlock_bh:
1203 jbd_unlock_bh_state(bh);
1204out:
1205 JBUFFER_TRACE(jh, "exit");
1206 WARN_ON(ret);
1207 return ret;
1208}
1209
1210
1211
1212
1213
1214
1215void
1216jbd2_journal_release_buffer(handle_t *handle, struct buffer_head *bh)
1217{
1218 BUFFER_TRACE(bh, "entry");
1219}
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238int jbd2_journal_forget (handle_t *handle, struct buffer_head *bh)
1239{
1240 transaction_t *transaction = handle->h_transaction;
1241 journal_t *journal = transaction->t_journal;
1242 struct journal_head *jh;
1243 int drop_reserve = 0;
1244 int err = 0;
1245 int was_modified = 0;
1246
1247 BUFFER_TRACE(bh, "entry");
1248
1249 jbd_lock_bh_state(bh);
1250 spin_lock(&journal->j_list_lock);
1251
1252 if (!buffer_jbd(bh))
1253 goto not_jbd;
1254 jh = bh2jh(bh);
1255
1256
1257
1258 if (!J_EXPECT_JH(jh, !jh->b_committed_data,
1259 "inconsistent data on disk")) {
1260 err = -EIO;
1261 goto not_jbd;
1262 }
1263
1264
1265 was_modified = jh->b_modified;
1266
1267
1268
1269
1270
1271 jh->b_modified = 0;
1272
1273 if (jh->b_transaction == handle->h_transaction) {
1274 J_ASSERT_JH(jh, !jh->b_frozen_data);
1275
1276
1277
1278
1279 clear_buffer_dirty(bh);
1280 clear_buffer_jbddirty(bh);
1281
1282 JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
1283
1284
1285
1286
1287
1288 if (was_modified)
1289 drop_reserve = 1;
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303 if (jh->b_cp_transaction) {
1304 __jbd2_journal_temp_unlink_buffer(jh);
1305 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
1306 } else {
1307 __jbd2_journal_unfile_buffer(jh);
1308 if (!buffer_jbd(bh)) {
1309 spin_unlock(&journal->j_list_lock);
1310 jbd_unlock_bh_state(bh);
1311 __bforget(bh);
1312 goto drop;
1313 }
1314 }
1315 } else if (jh->b_transaction) {
1316 J_ASSERT_JH(jh, (jh->b_transaction ==
1317 journal->j_committing_transaction));
1318
1319
1320 JBUFFER_TRACE(jh, "belongs to older transaction");
1321
1322
1323
1324 if (jh->b_next_transaction) {
1325 J_ASSERT(jh->b_next_transaction == transaction);
1326 jh->b_next_transaction = NULL;
1327
1328
1329
1330
1331
1332 if (was_modified)
1333 drop_reserve = 1;
1334 }
1335 }
1336
1337not_jbd:
1338 spin_unlock(&journal->j_list_lock);
1339 jbd_unlock_bh_state(bh);
1340 __brelse(bh);
1341drop:
1342 if (drop_reserve) {
1343
1344 handle->h_buffer_credits++;
1345 }
1346 return err;
1347}
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365int jbd2_journal_stop(handle_t *handle)
1366{
1367 transaction_t *transaction = handle->h_transaction;
1368 journal_t *journal = transaction->t_journal;
1369 int err, wait_for_commit = 0;
1370 tid_t tid;
1371 pid_t pid;
1372
1373 J_ASSERT(journal_current_handle() == handle);
1374
1375 if (is_handle_aborted(handle))
1376 err = -EIO;
1377 else {
1378 J_ASSERT(atomic_read(&transaction->t_updates) > 0);
1379 err = 0;
1380 }
1381
1382 if (--handle->h_ref > 0) {
1383 jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
1384 handle->h_ref);
1385 return err;
1386 }
1387
1388 jbd_debug(4, "Handle %p going down\n", handle);
1389
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 pid = current->pid;
1418 if (handle->h_sync && journal->j_last_sync_writer != pid) {
1419 u64 commit_time, trans_time;
1420
1421 journal->j_last_sync_writer = pid;
1422
1423 read_lock(&journal->j_state_lock);
1424 commit_time = journal->j_average_commit_time;
1425 read_unlock(&journal->j_state_lock);
1426
1427 trans_time = ktime_to_ns(ktime_sub(ktime_get(),
1428 transaction->t_start_time));
1429
1430 commit_time = max_t(u64, commit_time,
1431 1000*journal->j_min_batch_time);
1432 commit_time = min_t(u64, commit_time,
1433 1000*journal->j_max_batch_time);
1434
1435 if (trans_time < commit_time) {
1436 ktime_t expires = ktime_add_ns(ktime_get(),
1437 commit_time);
1438 set_current_state(TASK_UNINTERRUPTIBLE);
1439 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1440 }
1441 }
1442
1443 if (handle->h_sync)
1444 transaction->t_synchronous_commit = 1;
1445 current->journal_info = NULL;
1446 atomic_sub(handle->h_buffer_credits,
1447 &transaction->t_outstanding_credits);
1448
1449
1450
1451
1452
1453
1454
1455 if (handle->h_sync ||
1456 (atomic_read(&transaction->t_outstanding_credits) >
1457 journal->j_max_transaction_buffers) ||
1458 time_after_eq(jiffies, transaction->t_expires)) {
1459
1460
1461
1462
1463 jbd_debug(2, "transaction too old, requesting commit for "
1464 "handle %p\n", handle);
1465
1466 jbd2_log_start_commit(journal, transaction->t_tid);
1467
1468
1469
1470
1471
1472 if (handle->h_sync && !(current->flags & PF_MEMALLOC))
1473 wait_for_commit = 1;
1474 }
1475
1476
1477
1478
1479
1480
1481
1482 tid = transaction->t_tid;
1483 if (atomic_dec_and_test(&transaction->t_updates)) {
1484 wake_up(&journal->j_wait_updates);
1485 if (journal->j_barrier_count)
1486 wake_up(&journal->j_wait_transaction_locked);
1487 }
1488
1489 if (wait_for_commit)
1490 err = jbd2_log_wait_commit(journal, tid);
1491
1492 lock_map_release(&handle->h_lockdep_map);
1493
1494 jbd2_free_handle(handle);
1495 return err;
1496}
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506int jbd2_journal_force_commit(journal_t *journal)
1507{
1508 handle_t *handle;
1509 int ret;
1510
1511 handle = jbd2_journal_start(journal, 1);
1512 if (IS_ERR(handle)) {
1513 ret = PTR_ERR(handle);
1514 } else {
1515 handle->h_sync = 1;
1516 ret = jbd2_journal_stop(handle);
1517 }
1518 return ret;
1519}
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537static inline void
1538__blist_add_buffer(struct journal_head **list, struct journal_head *jh)
1539{
1540 if (!*list) {
1541 jh->b_tnext = jh->b_tprev = jh;
1542 *list = jh;
1543 } else {
1544
1545 struct journal_head *first = *list, *last = first->b_tprev;
1546 jh->b_tprev = last;
1547 jh->b_tnext = first;
1548 last->b_tnext = first->b_tprev = jh;
1549 }
1550}
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561static inline void
1562__blist_del_buffer(struct journal_head **list, struct journal_head *jh)
1563{
1564 if (*list == jh) {
1565 *list = jh->b_tnext;
1566 if (*list == jh)
1567 *list = NULL;
1568 }
1569 jh->b_tprev->b_tnext = jh->b_tnext;
1570 jh->b_tnext->b_tprev = jh->b_tprev;
1571}
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh)
1585{
1586 struct journal_head **list = NULL;
1587 transaction_t *transaction;
1588 struct buffer_head *bh = jh2bh(jh);
1589
1590 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1591 transaction = jh->b_transaction;
1592 if (transaction)
1593 assert_spin_locked(&transaction->t_journal->j_list_lock);
1594
1595 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1596 if (jh->b_jlist != BJ_None)
1597 J_ASSERT_JH(jh, transaction != NULL);
1598
1599 switch (jh->b_jlist) {
1600 case BJ_None:
1601 return;
1602 case BJ_Metadata:
1603 transaction->t_nr_buffers--;
1604 J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
1605 list = &transaction->t_buffers;
1606 break;
1607 case BJ_Forget:
1608 list = &transaction->t_forget;
1609 break;
1610 case BJ_IO:
1611 list = &transaction->t_iobuf_list;
1612 break;
1613 case BJ_Shadow:
1614 list = &transaction->t_shadow_list;
1615 break;
1616 case BJ_LogCtl:
1617 list = &transaction->t_log_list;
1618 break;
1619 case BJ_Reserved:
1620 list = &transaction->t_reserved_list;
1621 break;
1622 }
1623
1624 __blist_del_buffer(list, jh);
1625 jh->b_jlist = BJ_None;
1626 if (test_clear_buffer_jbddirty(bh))
1627 mark_buffer_dirty(bh);
1628}
1629
1630
1631
1632
1633
1634
1635
1636
1637static void __jbd2_journal_unfile_buffer(struct journal_head *jh)
1638{
1639 __jbd2_journal_temp_unlink_buffer(jh);
1640 jh->b_transaction = NULL;
1641 jbd2_journal_put_journal_head(jh);
1642}
1643
1644void jbd2_journal_unfile_buffer(journal_t *journal, struct journal_head *jh)
1645{
1646 struct buffer_head *bh = jh2bh(jh);
1647
1648
1649 get_bh(bh);
1650 jbd_lock_bh_state(bh);
1651 spin_lock(&journal->j_list_lock);
1652 __jbd2_journal_unfile_buffer(jh);
1653 spin_unlock(&journal->j_list_lock);
1654 jbd_unlock_bh_state(bh);
1655 __brelse(bh);
1656}
1657
1658
1659
1660
1661
1662
1663static void
1664__journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh)
1665{
1666 struct journal_head *jh;
1667
1668 jh = bh2jh(bh);
1669
1670 if (buffer_locked(bh) || buffer_dirty(bh))
1671 goto out;
1672
1673 if (jh->b_next_transaction != NULL)
1674 goto out;
1675
1676 spin_lock(&journal->j_list_lock);
1677 if (jh->b_cp_transaction != NULL && jh->b_transaction == NULL) {
1678
1679 JBUFFER_TRACE(jh, "remove from checkpoint list");
1680 __jbd2_journal_remove_checkpoint(jh);
1681 }
1682 spin_unlock(&journal->j_list_lock);
1683out:
1684 return;
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
1720
1721
1722
1723
1724
1725int jbd2_journal_try_to_free_buffers(journal_t *journal,
1726 struct page *page, gfp_t gfp_mask)
1727{
1728 struct buffer_head *head;
1729 struct buffer_head *bh;
1730 int ret = 0;
1731
1732 J_ASSERT(PageLocked(page));
1733
1734 head = page_buffers(page);
1735 bh = head;
1736 do {
1737 struct journal_head *jh;
1738
1739
1740
1741
1742
1743
1744 jh = jbd2_journal_grab_journal_head(bh);
1745 if (!jh)
1746 continue;
1747
1748 jbd_lock_bh_state(bh);
1749 __journal_try_to_free_buffer(journal, bh);
1750 jbd2_journal_put_journal_head(jh);
1751 jbd_unlock_bh_state(bh);
1752 if (buffer_jbd(bh))
1753 goto busy;
1754 } while ((bh = bh->b_this_page) != head);
1755
1756 ret = try_to_free_buffers(page);
1757
1758busy:
1759 return ret;
1760}
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
1775{
1776 int may_free = 1;
1777 struct buffer_head *bh = jh2bh(jh);
1778
1779 if (jh->b_cp_transaction) {
1780 JBUFFER_TRACE(jh, "on running+cp transaction");
1781 __jbd2_journal_temp_unlink_buffer(jh);
1782
1783
1784
1785
1786
1787 clear_buffer_dirty(bh);
1788 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
1789 may_free = 0;
1790 } else {
1791 JBUFFER_TRACE(jh, "on running transaction");
1792 __jbd2_journal_unfile_buffer(jh);
1793 }
1794 return may_free;
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
1841
1842
1843
1844static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh)
1845{
1846 transaction_t *transaction;
1847 struct journal_head *jh;
1848 int may_free = 1;
1849 int ret;
1850
1851 BUFFER_TRACE(bh, "entry");
1852
1853
1854
1855
1856
1857
1858
1859 if (!buffer_jbd(bh))
1860 goto zap_buffer_unlocked;
1861
1862
1863 write_lock(&journal->j_state_lock);
1864 jbd_lock_bh_state(bh);
1865 spin_lock(&journal->j_list_lock);
1866
1867 jh = jbd2_journal_grab_journal_head(bh);
1868 if (!jh)
1869 goto zap_buffer_no_jh;
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886 transaction = jh->b_transaction;
1887 if (transaction == NULL) {
1888
1889
1890
1891
1892 if (!jh->b_cp_transaction) {
1893 JBUFFER_TRACE(jh, "not on any transaction: zap");
1894 goto zap_buffer;
1895 }
1896
1897 if (!buffer_dirty(bh)) {
1898
1899 goto zap_buffer;
1900 }
1901
1902
1903
1904
1905
1906 if (journal->j_running_transaction) {
1907
1908
1909
1910 JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
1911 ret = __dispose_buffer(jh,
1912 journal->j_running_transaction);
1913 jbd2_journal_put_journal_head(jh);
1914 spin_unlock(&journal->j_list_lock);
1915 jbd_unlock_bh_state(bh);
1916 write_unlock(&journal->j_state_lock);
1917 return ret;
1918 } else {
1919
1920
1921
1922
1923 if (journal->j_committing_transaction) {
1924 JBUFFER_TRACE(jh, "give to committing trans");
1925 ret = __dispose_buffer(jh,
1926 journal->j_committing_transaction);
1927 jbd2_journal_put_journal_head(jh);
1928 spin_unlock(&journal->j_list_lock);
1929 jbd_unlock_bh_state(bh);
1930 write_unlock(&journal->j_state_lock);
1931 return ret;
1932 } else {
1933
1934
1935 clear_buffer_jbddirty(bh);
1936 goto zap_buffer;
1937 }
1938 }
1939 } else if (transaction == journal->j_committing_transaction) {
1940 JBUFFER_TRACE(jh, "on committing transaction");
1941
1942
1943
1944
1945
1946
1947
1948 set_buffer_freed(bh);
1949 if (journal->j_running_transaction && buffer_jbddirty(bh))
1950 jh->b_next_transaction = journal->j_running_transaction;
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 return 0;
1956 } else {
1957
1958
1959
1960
1961
1962
1963 J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
1964 JBUFFER_TRACE(jh, "on running transaction");
1965 may_free = __dispose_buffer(jh, transaction);
1966 }
1967
1968zap_buffer:
1969 jbd2_journal_put_journal_head(jh);
1970zap_buffer_no_jh:
1971 spin_unlock(&journal->j_list_lock);
1972 jbd_unlock_bh_state(bh);
1973 write_unlock(&journal->j_state_lock);
1974zap_buffer_unlocked:
1975 clear_buffer_dirty(bh);
1976 J_ASSERT_BH(bh, !buffer_jbddirty(bh));
1977 clear_buffer_mapped(bh);
1978 clear_buffer_req(bh);
1979 clear_buffer_new(bh);
1980 clear_buffer_delay(bh);
1981 clear_buffer_unwritten(bh);
1982 bh->b_bdev = NULL;
1983 return may_free;
1984}
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995void jbd2_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 __jbd2_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 __jbd2_journal_temp_unlink_buffer(jh);
2072 else
2073 jbd2_journal_grab_journal_head(bh);
2074 jh->b_transaction = transaction;
2075
2076 switch (jlist) {
2077 case BJ_None:
2078 J_ASSERT_JH(jh, !jh->b_committed_data);
2079 J_ASSERT_JH(jh, !jh->b_frozen_data);
2080 return;
2081 case BJ_Metadata:
2082 transaction->t_nr_buffers++;
2083 list = &transaction->t_buffers;
2084 break;
2085 case BJ_Forget:
2086 list = &transaction->t_forget;
2087 break;
2088 case BJ_IO:
2089 list = &transaction->t_iobuf_list;
2090 break;
2091 case BJ_Shadow:
2092 list = &transaction->t_shadow_list;
2093 break;
2094 case BJ_LogCtl:
2095 list = &transaction->t_log_list;
2096 break;
2097 case BJ_Reserved:
2098 list = &transaction->t_reserved_list;
2099 break;
2100 }
2101
2102 __blist_add_buffer(list, jh);
2103 jh->b_jlist = jlist;
2104
2105 if (was_dirty)
2106 set_buffer_jbddirty(bh);
2107}
2108
2109void jbd2_journal_file_buffer(struct journal_head *jh,
2110 transaction_t *transaction, int jlist)
2111{
2112 jbd_lock_bh_state(jh2bh(jh));
2113 spin_lock(&transaction->t_journal->j_list_lock);
2114 __jbd2_journal_file_buffer(jh, transaction, jlist);
2115 spin_unlock(&transaction->t_journal->j_list_lock);
2116 jbd_unlock_bh_state(jh2bh(jh));
2117}
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130void __jbd2_journal_refile_buffer(struct journal_head *jh)
2131{
2132 int was_dirty, jlist;
2133 struct buffer_head *bh = jh2bh(jh);
2134
2135 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
2136 if (jh->b_transaction)
2137 assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
2138
2139
2140 if (jh->b_next_transaction == NULL) {
2141 __jbd2_journal_unfile_buffer(jh);
2142 return;
2143 }
2144
2145
2146
2147
2148
2149
2150 was_dirty = test_clear_buffer_jbddirty(bh);
2151 __jbd2_journal_temp_unlink_buffer(jh);
2152
2153
2154
2155
2156
2157 jh->b_transaction = jh->b_next_transaction;
2158 jh->b_next_transaction = NULL;
2159 if (buffer_freed(bh))
2160 jlist = BJ_Forget;
2161 else if (jh->b_modified)
2162 jlist = BJ_Metadata;
2163 else
2164 jlist = BJ_Reserved;
2165 __jbd2_journal_file_buffer(jh, jh->b_transaction, jlist);
2166 J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
2167
2168 if (was_dirty)
2169 set_buffer_jbddirty(bh);
2170}
2171
2172
2173
2174
2175
2176
2177
2178void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh)
2179{
2180 struct buffer_head *bh = jh2bh(jh);
2181
2182
2183 get_bh(bh);
2184 jbd_lock_bh_state(bh);
2185 spin_lock(&journal->j_list_lock);
2186 __jbd2_journal_refile_buffer(jh);
2187 jbd_unlock_bh_state(bh);
2188 spin_unlock(&journal->j_list_lock);
2189 __brelse(bh);
2190}
2191
2192
2193
2194
2195int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode)
2196{
2197 transaction_t *transaction = handle->h_transaction;
2198 journal_t *journal = transaction->t_journal;
2199
2200 if (is_handle_aborted(handle))
2201 return -EIO;
2202
2203 jbd_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino,
2204 transaction->t_tid);
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219 if (jinode->i_transaction == transaction ||
2220 jinode->i_next_transaction == transaction)
2221 return 0;
2222
2223 spin_lock(&journal->j_list_lock);
2224
2225 if (jinode->i_transaction == transaction ||
2226 jinode->i_next_transaction == transaction)
2227 goto done;
2228
2229
2230
2231
2232
2233
2234 if (!transaction->t_need_data_flush)
2235 transaction->t_need_data_flush = 1;
2236
2237
2238 if (jinode->i_transaction) {
2239 J_ASSERT(jinode->i_next_transaction == NULL);
2240 J_ASSERT(jinode->i_transaction ==
2241 journal->j_committing_transaction);
2242 jinode->i_next_transaction = transaction;
2243 goto done;
2244 }
2245
2246 J_ASSERT(!jinode->i_next_transaction);
2247 jinode->i_transaction = transaction;
2248 list_add(&jinode->i_list, &transaction->t_inode_list);
2249done:
2250 spin_unlock(&journal->j_list_lock);
2251
2252 return 0;
2253}
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275int jbd2_journal_begin_ordered_truncate(journal_t *journal,
2276 struct jbd2_inode *jinode,
2277 loff_t new_size)
2278{
2279 transaction_t *inode_trans, *commit_trans;
2280 int ret = 0;
2281
2282
2283 if (!jinode->i_transaction)
2284 goto out;
2285
2286
2287
2288 read_lock(&journal->j_state_lock);
2289 commit_trans = journal->j_committing_transaction;
2290 read_unlock(&journal->j_state_lock);
2291 spin_lock(&journal->j_list_lock);
2292 inode_trans = jinode->i_transaction;
2293 spin_unlock(&journal->j_list_lock);
2294 if (inode_trans == commit_trans) {
2295 ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping,
2296 new_size, LLONG_MAX);
2297 if (ret)
2298 jbd2_journal_abort(journal, ret);
2299 }
2300out:
2301 return ret;
2302}
2303