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