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