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