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