1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <linux/module.h>
26#include <linux/time.h>
27#include <linux/fs.h>
28#include <linux/jbd2.h>
29#include <linux/errno.h>
30#include <linux/slab.h>
31#include <linux/init.h>
32#include <linux/mm.h>
33#include <linux/freezer.h>
34#include <linux/pagemap.h>
35#include <linux/kthread.h>
36#include <linux/poison.h>
37#include <linux/proc_fs.h>
38#include <linux/seq_file.h>
39#include <linux/math64.h>
40#include <linux/hash.h>
41#include <linux/log2.h>
42#include <linux/vmalloc.h>
43#include <linux/backing-dev.h>
44#include <linux/bitops.h>
45#include <linux/ratelimit.h>
46
47#define CREATE_TRACE_POINTS
48#include <trace/events/jbd2.h>
49
50#include <asm/uaccess.h>
51#include <asm/page.h>
52
53#ifdef CONFIG_JBD2_DEBUG
54ushort jbd2_journal_enable_debug __read_mostly;
55EXPORT_SYMBOL(jbd2_journal_enable_debug);
56
57module_param_named(jbd2_debug, jbd2_journal_enable_debug, ushort, 0644);
58MODULE_PARM_DESC(jbd2_debug, "Debugging level for jbd2");
59#endif
60
61EXPORT_SYMBOL(jbd2_journal_extend);
62EXPORT_SYMBOL(jbd2_journal_stop);
63EXPORT_SYMBOL(jbd2_journal_lock_updates);
64EXPORT_SYMBOL(jbd2_journal_unlock_updates);
65EXPORT_SYMBOL(jbd2_journal_get_write_access);
66EXPORT_SYMBOL(jbd2_journal_get_create_access);
67EXPORT_SYMBOL(jbd2_journal_get_undo_access);
68EXPORT_SYMBOL(jbd2_journal_set_triggers);
69EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
70EXPORT_SYMBOL(jbd2_journal_forget);
71#if 0
72EXPORT_SYMBOL(journal_sync_buffer);
73#endif
74EXPORT_SYMBOL(jbd2_journal_flush);
75EXPORT_SYMBOL(jbd2_journal_revoke);
76
77EXPORT_SYMBOL(jbd2_journal_init_dev);
78EXPORT_SYMBOL(jbd2_journal_init_inode);
79EXPORT_SYMBOL(jbd2_journal_check_used_features);
80EXPORT_SYMBOL(jbd2_journal_check_available_features);
81EXPORT_SYMBOL(jbd2_journal_set_features);
82EXPORT_SYMBOL(jbd2_journal_load);
83EXPORT_SYMBOL(jbd2_journal_destroy);
84EXPORT_SYMBOL(jbd2_journal_abort);
85EXPORT_SYMBOL(jbd2_journal_errno);
86EXPORT_SYMBOL(jbd2_journal_ack_err);
87EXPORT_SYMBOL(jbd2_journal_clear_err);
88EXPORT_SYMBOL(jbd2_log_wait_commit);
89EXPORT_SYMBOL(jbd2_log_start_commit);
90EXPORT_SYMBOL(jbd2_journal_start_commit);
91EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
92EXPORT_SYMBOL(jbd2_journal_wipe);
93EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
94EXPORT_SYMBOL(jbd2_journal_invalidatepage);
95EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
96EXPORT_SYMBOL(jbd2_journal_force_commit);
97EXPORT_SYMBOL(jbd2_journal_file_inode);
98EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
99EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
100EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
101EXPORT_SYMBOL(jbd2_inode_cache);
102
103static void __journal_abort_soft (journal_t *journal, int errno);
104static int jbd2_journal_create_slab(size_t slab_size);
105
106#ifdef CONFIG_JBD2_DEBUG
107void __jbd2_debug(int level, const char *file, const char *func,
108 unsigned int line, const char *fmt, ...)
109{
110 struct va_format vaf;
111 va_list args;
112
113 if (level > jbd2_journal_enable_debug)
114 return;
115 va_start(args, fmt);
116 vaf.fmt = fmt;
117 vaf.va = &args;
118 printk(KERN_DEBUG "%s: (%s, %u): %pV\n", file, func, line, &vaf);
119 va_end(args);
120}
121EXPORT_SYMBOL(__jbd2_debug);
122#endif
123
124
125static int jbd2_verify_csum_type(journal_t *j, journal_superblock_t *sb)
126{
127 if (!jbd2_journal_has_csum_v2or3(j))
128 return 1;
129
130 return sb->s_checksum_type == JBD2_CRC32C_CHKSUM;
131}
132
133static __be32 jbd2_superblock_csum(journal_t *j, journal_superblock_t *sb)
134{
135 __u32 csum;
136 __be32 old_csum;
137
138 old_csum = sb->s_checksum;
139 sb->s_checksum = 0;
140 csum = jbd2_chksum(j, ~0, (char *)sb, sizeof(journal_superblock_t));
141 sb->s_checksum = old_csum;
142
143 return cpu_to_be32(csum);
144}
145
146static int jbd2_superblock_csum_verify(journal_t *j, journal_superblock_t *sb)
147{
148 if (!jbd2_journal_has_csum_v2or3(j))
149 return 1;
150
151 return sb->s_checksum == jbd2_superblock_csum(j, sb);
152}
153
154static void jbd2_superblock_csum_set(journal_t *j, journal_superblock_t *sb)
155{
156 if (!jbd2_journal_has_csum_v2or3(j))
157 return;
158
159 sb->s_checksum = jbd2_superblock_csum(j, sb);
160}
161
162
163
164
165
166static void commit_timeout(unsigned long __data)
167{
168 struct task_struct * p = (struct task_struct *) __data;
169
170 wake_up_process(p);
171}
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189static int kjournald2(void *arg)
190{
191 journal_t *journal = arg;
192 transaction_t *transaction;
193
194
195
196
197
198 setup_timer(&journal->j_commit_timer, commit_timeout,
199 (unsigned long)current);
200
201 set_freezable();
202
203
204 journal->j_task = current;
205 wake_up(&journal->j_wait_done_commit);
206
207
208
209
210 write_lock(&journal->j_state_lock);
211
212loop:
213 if (journal->j_flags & JBD2_UNMOUNT)
214 goto end_loop;
215
216 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
217 journal->j_commit_sequence, journal->j_commit_request);
218
219 if (journal->j_commit_sequence != journal->j_commit_request) {
220 jbd_debug(1, "OK, requests differ\n");
221 write_unlock(&journal->j_state_lock);
222 del_timer_sync(&journal->j_commit_timer);
223 jbd2_journal_commit_transaction(journal);
224 write_lock(&journal->j_state_lock);
225 goto loop;
226 }
227
228 wake_up(&journal->j_wait_done_commit);
229 if (freezing(current)) {
230
231
232
233
234
235 jbd_debug(1, "Now suspending kjournald2\n");
236 write_unlock(&journal->j_state_lock);
237 try_to_freeze();
238 write_lock(&journal->j_state_lock);
239 } else {
240
241
242
243
244 DEFINE_WAIT(wait);
245 int should_sleep = 1;
246
247 prepare_to_wait(&journal->j_wait_commit, &wait,
248 TASK_INTERRUPTIBLE);
249 if (journal->j_commit_sequence != journal->j_commit_request)
250 should_sleep = 0;
251 transaction = journal->j_running_transaction;
252 if (transaction && time_after_eq(jiffies,
253 transaction->t_expires))
254 should_sleep = 0;
255 if (journal->j_flags & JBD2_UNMOUNT)
256 should_sleep = 0;
257 if (should_sleep) {
258 write_unlock(&journal->j_state_lock);
259 schedule();
260 write_lock(&journal->j_state_lock);
261 }
262 finish_wait(&journal->j_wait_commit, &wait);
263 }
264
265 jbd_debug(1, "kjournald2 wakes\n");
266
267
268
269
270 transaction = journal->j_running_transaction;
271 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
272 journal->j_commit_request = transaction->t_tid;
273 jbd_debug(1, "woke because of timeout\n");
274 }
275 goto loop;
276
277end_loop:
278 write_unlock(&journal->j_state_lock);
279 del_timer_sync(&journal->j_commit_timer);
280 journal->j_task = NULL;
281 wake_up(&journal->j_wait_done_commit);
282 jbd_debug(1, "Journal thread exiting.\n");
283 return 0;
284}
285
286static int jbd2_journal_start_thread(journal_t *journal)
287{
288 struct task_struct *t;
289
290 t = kthread_run(kjournald2, journal, "jbd2/%s",
291 journal->j_devname);
292 if (IS_ERR(t))
293 return PTR_ERR(t);
294
295 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
296 return 0;
297}
298
299static void journal_kill_thread(journal_t *journal)
300{
301 write_lock(&journal->j_state_lock);
302 journal->j_flags |= JBD2_UNMOUNT;
303
304 while (journal->j_task) {
305 write_unlock(&journal->j_state_lock);
306 wake_up(&journal->j_wait_commit);
307 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
308 write_lock(&journal->j_state_lock);
309 }
310 write_unlock(&journal->j_state_lock);
311}
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
349 struct journal_head *jh_in,
350 struct buffer_head **bh_out,
351 sector_t blocknr)
352{
353 int need_copy_out = 0;
354 int done_copy_out = 0;
355 int do_escape = 0;
356 char *mapped_data;
357 struct buffer_head *new_bh;
358 struct page *new_page;
359 unsigned int new_offset;
360 struct buffer_head *bh_in = jh2bh(jh_in);
361 journal_t *journal = transaction->t_journal;
362
363
364
365
366
367
368
369
370
371
372 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
373
374retry_alloc:
375 new_bh = alloc_buffer_head(GFP_NOFS);
376 if (!new_bh) {
377
378
379
380
381 congestion_wait(BLK_RW_ASYNC, HZ/50);
382 goto retry_alloc;
383 }
384
385
386 atomic_set(&new_bh->b_count, 1);
387
388 jbd_lock_bh_state(bh_in);
389repeat:
390
391
392
393
394 if (jh_in->b_frozen_data) {
395 done_copy_out = 1;
396 new_page = virt_to_page(jh_in->b_frozen_data);
397 new_offset = offset_in_page(jh_in->b_frozen_data);
398 } else {
399 new_page = jh2bh(jh_in)->b_page;
400 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
401 }
402
403 mapped_data = kmap_atomic(new_page);
404
405
406
407
408
409
410 if (!done_copy_out)
411 jbd2_buffer_frozen_trigger(jh_in, mapped_data + new_offset,
412 jh_in->b_triggers);
413
414
415
416
417 if (*((__be32 *)(mapped_data + new_offset)) ==
418 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
419 need_copy_out = 1;
420 do_escape = 1;
421 }
422 kunmap_atomic(mapped_data);
423
424
425
426
427 if (need_copy_out && !done_copy_out) {
428 char *tmp;
429
430 jbd_unlock_bh_state(bh_in);
431 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
432 if (!tmp) {
433 brelse(new_bh);
434 return -ENOMEM;
435 }
436 jbd_lock_bh_state(bh_in);
437 if (jh_in->b_frozen_data) {
438 jbd2_free(tmp, bh_in->b_size);
439 goto repeat;
440 }
441
442 jh_in->b_frozen_data = tmp;
443 mapped_data = kmap_atomic(new_page);
444 memcpy(tmp, mapped_data + new_offset, bh_in->b_size);
445 kunmap_atomic(mapped_data);
446
447 new_page = virt_to_page(tmp);
448 new_offset = offset_in_page(tmp);
449 done_copy_out = 1;
450
451
452
453
454
455
456 jh_in->b_frozen_triggers = jh_in->b_triggers;
457 }
458
459
460
461
462
463 if (do_escape) {
464 mapped_data = kmap_atomic(new_page);
465 *((unsigned int *)(mapped_data + new_offset)) = 0;
466 kunmap_atomic(mapped_data);
467 }
468
469 set_bh_page(new_bh, new_page, new_offset);
470 new_bh->b_size = bh_in->b_size;
471 new_bh->b_bdev = journal->j_dev;
472 new_bh->b_blocknr = blocknr;
473 new_bh->b_private = bh_in;
474 set_buffer_mapped(new_bh);
475 set_buffer_dirty(new_bh);
476
477 *bh_out = new_bh;
478
479
480
481
482
483
484 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
485 spin_lock(&journal->j_list_lock);
486 __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
487 spin_unlock(&journal->j_list_lock);
488 set_buffer_shadow(bh_in);
489 jbd_unlock_bh_state(bh_in);
490
491 return do_escape | (done_copy_out << 1);
492}
493
494
495
496
497
498
499
500
501
502
503int __jbd2_log_start_commit(journal_t *journal, tid_t target)
504{
505
506 if (journal->j_commit_request == target)
507 return 0;
508
509
510
511
512
513
514 if (journal->j_running_transaction &&
515 journal->j_running_transaction->t_tid == target) {
516
517
518
519
520
521 journal->j_commit_request = target;
522 jbd_debug(1, "JBD2: requesting commit %d/%d\n",
523 journal->j_commit_request,
524 journal->j_commit_sequence);
525 journal->j_running_transaction->t_requested = jiffies;
526 wake_up(&journal->j_wait_commit);
527 return 1;
528 } else if (!tid_geq(journal->j_commit_request, target))
529
530
531
532 WARN_ONCE(1, "JBD2: bad log_start_commit: %u %u %u %u\n",
533 journal->j_commit_request,
534 journal->j_commit_sequence,
535 target, journal->j_running_transaction ?
536 journal->j_running_transaction->t_tid : 0);
537 return 0;
538}
539
540int jbd2_log_start_commit(journal_t *journal, tid_t tid)
541{
542 int ret;
543
544 write_lock(&journal->j_state_lock);
545 ret = __jbd2_log_start_commit(journal, tid);
546 write_unlock(&journal->j_state_lock);
547 return ret;
548}
549
550
551
552
553
554
555
556
557static int __jbd2_journal_force_commit(journal_t *journal)
558{
559 transaction_t *transaction = NULL;
560 tid_t tid;
561 int need_to_start = 0, ret = 0;
562
563 read_lock(&journal->j_state_lock);
564 if (journal->j_running_transaction && !current->journal_info) {
565 transaction = journal->j_running_transaction;
566 if (!tid_geq(journal->j_commit_request, transaction->t_tid))
567 need_to_start = 1;
568 } else if (journal->j_committing_transaction)
569 transaction = journal->j_committing_transaction;
570
571 if (!transaction) {
572
573 read_unlock(&journal->j_state_lock);
574 return 0;
575 }
576 tid = transaction->t_tid;
577 read_unlock(&journal->j_state_lock);
578 if (need_to_start)
579 jbd2_log_start_commit(journal, tid);
580 ret = jbd2_log_wait_commit(journal, tid);
581 if (!ret)
582 ret = 1;
583
584 return ret;
585}
586
587
588
589
590
591
592
593
594
595int jbd2_journal_force_commit_nested(journal_t *journal)
596{
597 int ret;
598
599 ret = __jbd2_journal_force_commit(journal);
600 return ret > 0;
601}
602
603
604
605
606
607
608
609
610int jbd2_journal_force_commit(journal_t *journal)
611{
612 int ret;
613
614 J_ASSERT(!current->journal_info);
615 ret = __jbd2_journal_force_commit(journal);
616 if (ret > 0)
617 ret = 0;
618 return ret;
619}
620
621
622
623
624
625
626int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
627{
628 int ret = 0;
629
630 write_lock(&journal->j_state_lock);
631 if (journal->j_running_transaction) {
632 tid_t tid = journal->j_running_transaction->t_tid;
633
634 __jbd2_log_start_commit(journal, tid);
635
636
637 if (ptid)
638 *ptid = tid;
639 ret = 1;
640 } else if (journal->j_committing_transaction) {
641
642
643
644
645 if (ptid)
646 *ptid = journal->j_committing_transaction->t_tid;
647 ret = 1;
648 }
649 write_unlock(&journal->j_state_lock);
650 return ret;
651}
652
653
654
655
656
657
658
659int jbd2_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
660{
661 int ret = 0;
662 transaction_t *commit_trans;
663
664 if (!(journal->j_flags & JBD2_BARRIER))
665 return 0;
666 read_lock(&journal->j_state_lock);
667
668 if (tid_geq(journal->j_commit_sequence, tid))
669 goto out;
670 commit_trans = journal->j_committing_transaction;
671 if (!commit_trans || commit_trans->t_tid != tid) {
672 ret = 1;
673 goto out;
674 }
675
676
677
678
679 if (journal->j_fs_dev != journal->j_dev) {
680 if (!commit_trans->t_need_data_flush ||
681 commit_trans->t_state >= T_COMMIT_DFLUSH)
682 goto out;
683 } else {
684 if (commit_trans->t_state >= T_COMMIT_JFLUSH)
685 goto out;
686 }
687 ret = 1;
688out:
689 read_unlock(&journal->j_state_lock);
690 return ret;
691}
692EXPORT_SYMBOL(jbd2_trans_will_send_data_barrier);
693
694
695
696
697
698int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
699{
700 int err = 0;
701
702 read_lock(&journal->j_state_lock);
703#ifdef CONFIG_JBD2_DEBUG
704 if (!tid_geq(journal->j_commit_request, tid)) {
705 printk(KERN_ERR
706 "%s: error: j_commit_request=%d, tid=%d\n",
707 __func__, journal->j_commit_request, tid);
708 }
709#endif
710 while (tid_gt(tid, journal->j_commit_sequence)) {
711 jbd_debug(1, "JBD2: want %d, j_commit_sequence=%d\n",
712 tid, journal->j_commit_sequence);
713 read_unlock(&journal->j_state_lock);
714 wake_up(&journal->j_wait_commit);
715 wait_event(journal->j_wait_done_commit,
716 !tid_gt(tid, journal->j_commit_sequence));
717 read_lock(&journal->j_state_lock);
718 }
719 read_unlock(&journal->j_state_lock);
720
721 if (unlikely(is_journal_aborted(journal)))
722 err = -EIO;
723 return err;
724}
725
726
727int jbd2_transaction_committed(journal_t *journal, tid_t tid)
728{
729 int ret = 1;
730
731 read_lock(&journal->j_state_lock);
732 if (journal->j_running_transaction &&
733 journal->j_running_transaction->t_tid == tid)
734 ret = 0;
735 if (journal->j_committing_transaction &&
736 journal->j_committing_transaction->t_tid == tid)
737 ret = 0;
738 read_unlock(&journal->j_state_lock);
739 return ret;
740}
741EXPORT_SYMBOL(jbd2_transaction_committed);
742
743
744
745
746
747
748
749
750int jbd2_complete_transaction(journal_t *journal, tid_t tid)
751{
752 int need_to_wait = 1;
753
754 read_lock(&journal->j_state_lock);
755 if (journal->j_running_transaction &&
756 journal->j_running_transaction->t_tid == tid) {
757 if (journal->j_commit_request != tid) {
758
759 read_unlock(&journal->j_state_lock);
760 jbd2_log_start_commit(journal, tid);
761 goto wait_commit;
762 }
763 } else if (!(journal->j_committing_transaction &&
764 journal->j_committing_transaction->t_tid == tid))
765 need_to_wait = 0;
766 read_unlock(&journal->j_state_lock);
767 if (!need_to_wait)
768 return 0;
769wait_commit:
770 return jbd2_log_wait_commit(journal, tid);
771}
772EXPORT_SYMBOL(jbd2_complete_transaction);
773
774
775
776
777
778int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
779{
780 unsigned long blocknr;
781
782 write_lock(&journal->j_state_lock);
783 J_ASSERT(journal->j_free > 1);
784
785 blocknr = journal->j_head;
786 journal->j_head++;
787 journal->j_free--;
788 if (journal->j_head == journal->j_last)
789 journal->j_head = journal->j_first;
790 write_unlock(&journal->j_state_lock);
791 return jbd2_journal_bmap(journal, blocknr, retp);
792}
793
794
795
796
797
798
799
800
801int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
802 unsigned long long *retp)
803{
804 int err = 0;
805 unsigned long long ret;
806
807 if (journal->j_inode) {
808 ret = bmap(journal->j_inode, blocknr);
809 if (ret)
810 *retp = ret;
811 else {
812 printk(KERN_ALERT "%s: journal block not found "
813 "at offset %lu on %s\n",
814 __func__, blocknr, journal->j_devname);
815 err = -EIO;
816 __journal_abort_soft(journal, err);
817 }
818 } else {
819 *retp = blocknr;
820 }
821 return err;
822}
823
824
825
826
827
828
829
830
831
832
833
834struct buffer_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
835{
836 struct buffer_head *bh;
837 unsigned long long blocknr;
838 int err;
839
840 err = jbd2_journal_next_log_block(journal, &blocknr);
841
842 if (err)
843 return NULL;
844
845 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
846 if (!bh)
847 return NULL;
848 lock_buffer(bh);
849 memset(bh->b_data, 0, journal->j_blocksize);
850 set_buffer_uptodate(bh);
851 unlock_buffer(bh);
852 BUFFER_TRACE(bh, "return this buffer");
853 return bh;
854}
855
856
857
858
859
860
861
862
863
864
865
866int jbd2_journal_get_log_tail(journal_t *journal, tid_t *tid,
867 unsigned long *block)
868{
869 transaction_t *transaction;
870 int ret;
871
872 read_lock(&journal->j_state_lock);
873 spin_lock(&journal->j_list_lock);
874 transaction = journal->j_checkpoint_transactions;
875 if (transaction) {
876 *tid = transaction->t_tid;
877 *block = transaction->t_log_start;
878 } else if ((transaction = journal->j_committing_transaction) != NULL) {
879 *tid = transaction->t_tid;
880 *block = transaction->t_log_start;
881 } else if ((transaction = journal->j_running_transaction) != NULL) {
882 *tid = transaction->t_tid;
883 *block = journal->j_head;
884 } else {
885 *tid = journal->j_transaction_sequence;
886 *block = journal->j_head;
887 }
888 ret = tid_gt(*tid, journal->j_tail_sequence);
889 spin_unlock(&journal->j_list_lock);
890 read_unlock(&journal->j_state_lock);
891
892 return ret;
893}
894
895
896
897
898
899
900
901
902
903
904
905int __jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block)
906{
907 unsigned long freed;
908 int ret;
909
910 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
911
912
913
914
915
916
917
918 ret = jbd2_journal_update_sb_log_tail(journal, tid, block, WRITE_FUA);
919 if (ret)
920 goto out;
921
922 write_lock(&journal->j_state_lock);
923 freed = block - journal->j_tail;
924 if (block < journal->j_tail)
925 freed += journal->j_last - journal->j_first;
926
927 trace_jbd2_update_log_tail(journal, tid, block, freed);
928 jbd_debug(1,
929 "Cleaning journal tail from %d to %d (offset %lu), "
930 "freeing %lu\n",
931 journal->j_tail_sequence, tid, block, freed);
932
933 journal->j_free += freed;
934 journal->j_tail_sequence = tid;
935 journal->j_tail = block;
936 write_unlock(&journal->j_state_lock);
937
938out:
939 return ret;
940}
941
942
943
944
945
946
947void jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block)
948{
949 mutex_lock(&journal->j_checkpoint_mutex);
950 if (tid_gt(tid, journal->j_tail_sequence))
951 __jbd2_update_log_tail(journal, tid, block);
952 mutex_unlock(&journal->j_checkpoint_mutex);
953}
954
955struct jbd2_stats_proc_session {
956 journal_t *journal;
957 struct transaction_stats_s *stats;
958 int start;
959 int max;
960};
961
962static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
963{
964 return *pos ? NULL : SEQ_START_TOKEN;
965}
966
967static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
968{
969 return NULL;
970}
971
972static int jbd2_seq_info_show(struct seq_file *seq, void *v)
973{
974 struct jbd2_stats_proc_session *s = seq->private;
975
976 if (v != SEQ_START_TOKEN)
977 return 0;
978 seq_printf(seq, "%lu transactions (%lu requested), "
979 "each up to %u blocks\n",
980 s->stats->ts_tid, s->stats->ts_requested,
981 s->journal->j_max_transaction_buffers);
982 if (s->stats->ts_tid == 0)
983 return 0;
984 seq_printf(seq, "average: \n %ums waiting for transaction\n",
985 jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid));
986 seq_printf(seq, " %ums request delay\n",
987 (s->stats->ts_requested == 0) ? 0 :
988 jiffies_to_msecs(s->stats->run.rs_request_delay /
989 s->stats->ts_requested));
990 seq_printf(seq, " %ums running transaction\n",
991 jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid));
992 seq_printf(seq, " %ums transaction was being locked\n",
993 jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid));
994 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
995 jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid));
996 seq_printf(seq, " %ums logging transaction\n",
997 jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid));
998 seq_printf(seq, " %lluus average transaction commit time\n",
999 div_u64(s->journal->j_average_commit_time, 1000));
1000 seq_printf(seq, " %lu handles per transaction\n",
1001 s->stats->run.rs_handle_count / s->stats->ts_tid);
1002 seq_printf(seq, " %lu blocks per transaction\n",
1003 s->stats->run.rs_blocks / s->stats->ts_tid);
1004 seq_printf(seq, " %lu logged blocks per transaction\n",
1005 s->stats->run.rs_blocks_logged / s->stats->ts_tid);
1006 return 0;
1007}
1008
1009static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
1010{
1011}
1012
1013static const struct seq_operations jbd2_seq_info_ops = {
1014 .start = jbd2_seq_info_start,
1015 .next = jbd2_seq_info_next,
1016 .stop = jbd2_seq_info_stop,
1017 .show = jbd2_seq_info_show,
1018};
1019
1020static int jbd2_seq_info_open(struct inode *inode, struct file *file)
1021{
1022 journal_t *journal = PDE_DATA(inode);
1023 struct jbd2_stats_proc_session *s;
1024 int rc, size;
1025
1026 s = kmalloc(sizeof(*s), GFP_KERNEL);
1027 if (s == NULL)
1028 return -ENOMEM;
1029 size = sizeof(struct transaction_stats_s);
1030 s->stats = kmalloc(size, GFP_KERNEL);
1031 if (s->stats == NULL) {
1032 kfree(s);
1033 return -ENOMEM;
1034 }
1035 spin_lock(&journal->j_history_lock);
1036 memcpy(s->stats, &journal->j_stats, size);
1037 s->journal = journal;
1038 spin_unlock(&journal->j_history_lock);
1039
1040 rc = seq_open(file, &jbd2_seq_info_ops);
1041 if (rc == 0) {
1042 struct seq_file *m = file->private_data;
1043 m->private = s;
1044 } else {
1045 kfree(s->stats);
1046 kfree(s);
1047 }
1048 return rc;
1049
1050}
1051
1052static int jbd2_seq_info_release(struct inode *inode, struct file *file)
1053{
1054 struct seq_file *seq = file->private_data;
1055 struct jbd2_stats_proc_session *s = seq->private;
1056 kfree(s->stats);
1057 kfree(s);
1058 return seq_release(inode, file);
1059}
1060
1061static const struct file_operations jbd2_seq_info_fops = {
1062 .owner = THIS_MODULE,
1063 .open = jbd2_seq_info_open,
1064 .read = seq_read,
1065 .llseek = seq_lseek,
1066 .release = jbd2_seq_info_release,
1067};
1068
1069static struct proc_dir_entry *proc_jbd2_stats;
1070
1071static void jbd2_stats_proc_init(journal_t *journal)
1072{
1073 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
1074 if (journal->j_proc_entry) {
1075 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
1076 &jbd2_seq_info_fops, journal);
1077 }
1078}
1079
1080static void jbd2_stats_proc_exit(journal_t *journal)
1081{
1082 remove_proc_entry("info", journal->j_proc_entry);
1083 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
1084}
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095static journal_t * journal_init_common (void)
1096{
1097 journal_t *journal;
1098 int err;
1099
1100 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
1101 if (!journal)
1102 return NULL;
1103
1104 init_waitqueue_head(&journal->j_wait_transaction_locked);
1105 init_waitqueue_head(&journal->j_wait_done_commit);
1106 init_waitqueue_head(&journal->j_wait_commit);
1107 init_waitqueue_head(&journal->j_wait_updates);
1108 init_waitqueue_head(&journal->j_wait_reserved);
1109 mutex_init(&journal->j_barrier);
1110 mutex_init(&journal->j_checkpoint_mutex);
1111 spin_lock_init(&journal->j_revoke_lock);
1112 spin_lock_init(&journal->j_list_lock);
1113 rwlock_init(&journal->j_state_lock);
1114
1115 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
1116 journal->j_min_batch_time = 0;
1117 journal->j_max_batch_time = 15000;
1118 atomic_set(&journal->j_reserved_credits, 0);
1119
1120
1121 journal->j_flags = JBD2_ABORT;
1122
1123
1124 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
1125 if (err) {
1126 kfree(journal);
1127 return NULL;
1128 }
1129
1130 spin_lock_init(&journal->j_history_lock);
1131
1132 return journal;
1133}
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158journal_t * jbd2_journal_init_dev(struct block_device *bdev,
1159 struct block_device *fs_dev,
1160 unsigned long long start, int len, int blocksize)
1161{
1162 journal_t *journal = journal_init_common();
1163 struct buffer_head *bh;
1164 char *p;
1165 int n;
1166
1167 if (!journal)
1168 return NULL;
1169
1170
1171 journal->j_blocksize = blocksize;
1172 journal->j_dev = bdev;
1173 journal->j_fs_dev = fs_dev;
1174 journal->j_blk_offset = start;
1175 journal->j_maxlen = len;
1176 bdevname(journal->j_dev, journal->j_devname);
1177 p = journal->j_devname;
1178 while ((p = strchr(p, '/')))
1179 *p = '!';
1180 jbd2_stats_proc_init(journal);
1181 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1182 journal->j_wbufsize = n;
1183 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1184 if (!journal->j_wbuf) {
1185 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
1186 __func__);
1187 goto out_err;
1188 }
1189
1190 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
1191 if (!bh) {
1192 printk(KERN_ERR
1193 "%s: Cannot get buffer for journal superblock\n",
1194 __func__);
1195 goto out_err;
1196 }
1197 journal->j_sb_buffer = bh;
1198 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1199
1200 return journal;
1201out_err:
1202 kfree(journal->j_wbuf);
1203 jbd2_stats_proc_exit(journal);
1204 kfree(journal);
1205 return NULL;
1206}
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216journal_t * jbd2_journal_init_inode (struct inode *inode)
1217{
1218 struct buffer_head *bh;
1219 journal_t *journal = journal_init_common();
1220 char *p;
1221 int err;
1222 int n;
1223 unsigned long long blocknr;
1224
1225 if (!journal)
1226 return NULL;
1227
1228 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1229 journal->j_inode = inode;
1230 bdevname(journal->j_dev, journal->j_devname);
1231 p = journal->j_devname;
1232 while ((p = strchr(p, '/')))
1233 *p = '!';
1234 p = journal->j_devname + strlen(journal->j_devname);
1235 sprintf(p, "-%lu", journal->j_inode->i_ino);
1236 jbd_debug(1,
1237 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1238 journal, inode->i_sb->s_id, inode->i_ino,
1239 (long long) inode->i_size,
1240 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1241
1242 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1243 journal->j_blocksize = inode->i_sb->s_blocksize;
1244 jbd2_stats_proc_init(journal);
1245
1246
1247 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1248 journal->j_wbufsize = n;
1249 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1250 if (!journal->j_wbuf) {
1251 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
1252 __func__);
1253 goto out_err;
1254 }
1255
1256 err = jbd2_journal_bmap(journal, 0, &blocknr);
1257
1258 if (err) {
1259 printk(KERN_ERR "%s: Cannot locate journal superblock\n",
1260 __func__);
1261 goto out_err;
1262 }
1263
1264 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
1265 if (!bh) {
1266 printk(KERN_ERR
1267 "%s: Cannot get buffer for journal superblock\n",
1268 __func__);
1269 goto out_err;
1270 }
1271 journal->j_sb_buffer = bh;
1272 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1273
1274 return journal;
1275out_err:
1276 kfree(journal->j_wbuf);
1277 jbd2_stats_proc_exit(journal);
1278 kfree(journal);
1279 return NULL;
1280}
1281
1282
1283
1284
1285
1286
1287static void journal_fail_superblock (journal_t *journal)
1288{
1289 struct buffer_head *bh = journal->j_sb_buffer;
1290 brelse(bh);
1291 journal->j_sb_buffer = NULL;
1292}
1293
1294
1295
1296
1297
1298
1299
1300
1301static int journal_reset(journal_t *journal)
1302{
1303 journal_superblock_t *sb = journal->j_superblock;
1304 unsigned long long first, last;
1305
1306 first = be32_to_cpu(sb->s_first);
1307 last = be32_to_cpu(sb->s_maxlen);
1308 if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
1309 printk(KERN_ERR "JBD2: Journal too short (blocks %llu-%llu).\n",
1310 first, last);
1311 journal_fail_superblock(journal);
1312 return -EINVAL;
1313 }
1314
1315 journal->j_first = first;
1316 journal->j_last = last;
1317
1318 journal->j_head = first;
1319 journal->j_tail = first;
1320 journal->j_free = last - first;
1321
1322 journal->j_tail_sequence = journal->j_transaction_sequence;
1323 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1324 journal->j_commit_request = journal->j_commit_sequence;
1325
1326 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1327
1328
1329
1330
1331
1332
1333
1334 if (sb->s_start == 0) {
1335 jbd_debug(1, "JBD2: Skipping superblock update on recovered sb "
1336 "(start %ld, seq %d, errno %d)\n",
1337 journal->j_tail, journal->j_tail_sequence,
1338 journal->j_errno);
1339 journal->j_flags |= JBD2_FLUSHED;
1340 } else {
1341
1342 mutex_lock(&journal->j_checkpoint_mutex);
1343
1344
1345
1346
1347
1348
1349 jbd2_journal_update_sb_log_tail(journal,
1350 journal->j_tail_sequence,
1351 journal->j_tail,
1352 WRITE_FUA);
1353 mutex_unlock(&journal->j_checkpoint_mutex);
1354 }
1355 return jbd2_journal_start_thread(journal);
1356}
1357
1358static int jbd2_write_superblock(journal_t *journal, int write_op)
1359{
1360 struct buffer_head *bh = journal->j_sb_buffer;
1361 journal_superblock_t *sb = journal->j_superblock;
1362 int ret;
1363
1364
1365 if (!buffer_mapped(bh))
1366 return -EIO;
1367
1368 trace_jbd2_write_superblock(journal, write_op);
1369 if (!(journal->j_flags & JBD2_BARRIER))
1370 write_op &= ~(REQ_FUA | REQ_FLUSH);
1371 lock_buffer(bh);
1372 if (buffer_write_io_error(bh)) {
1373
1374
1375
1376
1377
1378
1379
1380
1381 printk(KERN_ERR "JBD2: previous I/O error detected "
1382 "for journal superblock update for %s.\n",
1383 journal->j_devname);
1384 clear_buffer_write_io_error(bh);
1385 set_buffer_uptodate(bh);
1386 }
1387 jbd2_superblock_csum_set(journal, sb);
1388 get_bh(bh);
1389 bh->b_end_io = end_buffer_write_sync;
1390 ret = submit_bh(write_op, bh);
1391 wait_on_buffer(bh);
1392 if (buffer_write_io_error(bh)) {
1393 clear_buffer_write_io_error(bh);
1394 set_buffer_uptodate(bh);
1395 ret = -EIO;
1396 }
1397 if (ret) {
1398 printk(KERN_ERR "JBD2: Error %d detected when updating "
1399 "journal superblock for %s.\n", ret,
1400 journal->j_devname);
1401 jbd2_journal_abort(journal, ret);
1402 }
1403
1404 return ret;
1405}
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417int jbd2_journal_update_sb_log_tail(journal_t *journal, tid_t tail_tid,
1418 unsigned long tail_block, int write_op)
1419{
1420 journal_superblock_t *sb = journal->j_superblock;
1421 int ret;
1422
1423 if (is_journal_aborted(journal))
1424 return -EIO;
1425
1426 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
1427 jbd_debug(1, "JBD2: updating superblock (start %lu, seq %u)\n",
1428 tail_block, tail_tid);
1429
1430 sb->s_sequence = cpu_to_be32(tail_tid);
1431 sb->s_start = cpu_to_be32(tail_block);
1432
1433 ret = jbd2_write_superblock(journal, write_op);
1434 if (ret)
1435 goto out;
1436
1437
1438 write_lock(&journal->j_state_lock);
1439 WARN_ON(!sb->s_sequence);
1440 journal->j_flags &= ~JBD2_FLUSHED;
1441 write_unlock(&journal->j_state_lock);
1442
1443out:
1444 return ret;
1445}
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455static void jbd2_mark_journal_empty(journal_t *journal, int write_op)
1456{
1457 journal_superblock_t *sb = journal->j_superblock;
1458
1459 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
1460 read_lock(&journal->j_state_lock);
1461
1462 if (sb->s_start == 0) {
1463 read_unlock(&journal->j_state_lock);
1464 return;
1465 }
1466 jbd_debug(1, "JBD2: Marking journal as empty (seq %d)\n",
1467 journal->j_tail_sequence);
1468
1469 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1470 sb->s_start = cpu_to_be32(0);
1471 read_unlock(&journal->j_state_lock);
1472
1473 jbd2_write_superblock(journal, write_op);
1474
1475
1476 write_lock(&journal->j_state_lock);
1477 journal->j_flags |= JBD2_FLUSHED;
1478 write_unlock(&journal->j_state_lock);
1479}
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489void jbd2_journal_update_sb_errno(journal_t *journal)
1490{
1491 journal_superblock_t *sb = journal->j_superblock;
1492
1493 read_lock(&journal->j_state_lock);
1494 jbd_debug(1, "JBD2: updating superblock error (errno %d)\n",
1495 journal->j_errno);
1496 sb->s_errno = cpu_to_be32(journal->j_errno);
1497 read_unlock(&journal->j_state_lock);
1498
1499 jbd2_write_superblock(journal, WRITE_SYNC);
1500}
1501EXPORT_SYMBOL(jbd2_journal_update_sb_errno);
1502
1503
1504
1505
1506
1507static int journal_get_superblock(journal_t *journal)
1508{
1509 struct buffer_head *bh;
1510 journal_superblock_t *sb;
1511 int err = -EIO;
1512
1513 bh = journal->j_sb_buffer;
1514
1515 J_ASSERT(bh != NULL);
1516 if (!buffer_uptodate(bh)) {
1517 ll_rw_block(READ, 1, &bh);
1518 wait_on_buffer(bh);
1519 if (!buffer_uptodate(bh)) {
1520 printk(KERN_ERR
1521 "JBD2: IO error reading journal superblock\n");
1522 goto out;
1523 }
1524 }
1525
1526 if (buffer_verified(bh))
1527 return 0;
1528
1529 sb = journal->j_superblock;
1530
1531 err = -EINVAL;
1532
1533 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
1534 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1535 printk(KERN_WARNING "JBD2: no valid journal superblock found\n");
1536 goto out;
1537 }
1538
1539 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1540 case JBD2_SUPERBLOCK_V1:
1541 journal->j_format_version = 1;
1542 break;
1543 case JBD2_SUPERBLOCK_V2:
1544 journal->j_format_version = 2;
1545 break;
1546 default:
1547 printk(KERN_WARNING "JBD2: unrecognised superblock format ID\n");
1548 goto out;
1549 }
1550
1551 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1552 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1553 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1554 printk(KERN_WARNING "JBD2: journal file too short\n");
1555 goto out;
1556 }
1557
1558 if (be32_to_cpu(sb->s_first) == 0 ||
1559 be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
1560 printk(KERN_WARNING
1561 "JBD2: Invalid start block of journal: %u\n",
1562 be32_to_cpu(sb->s_first));
1563 goto out;
1564 }
1565
1566 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2) &&
1567 JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V3)) {
1568
1569 printk(KERN_ERR "JBD2: Can't enable checksumming v2 and v3 "
1570 "at the same time!\n");
1571 goto out;
1572 }
1573
1574 if (jbd2_journal_has_csum_v2or3(journal) &&
1575 JBD2_HAS_COMPAT_FEATURE(journal, JBD2_FEATURE_COMPAT_CHECKSUM)) {
1576
1577 printk(KERN_ERR "JBD2: Can't enable checksumming v1 and v2/3 "
1578 "at the same time!\n");
1579 goto out;
1580 }
1581
1582 if (!jbd2_verify_csum_type(journal, sb)) {
1583 printk(KERN_ERR "JBD2: Unknown checksum type\n");
1584 goto out;
1585 }
1586
1587
1588 if (jbd2_journal_has_csum_v2or3(journal)) {
1589 journal->j_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
1590 if (IS_ERR(journal->j_chksum_driver)) {
1591 printk(KERN_ERR "JBD2: Cannot load crc32c driver.\n");
1592 err = PTR_ERR(journal->j_chksum_driver);
1593 journal->j_chksum_driver = NULL;
1594 goto out;
1595 }
1596 }
1597
1598
1599 if (!jbd2_superblock_csum_verify(journal, sb)) {
1600 printk(KERN_ERR "JBD2: journal checksum error\n");
1601 goto out;
1602 }
1603
1604
1605 if (jbd2_journal_has_csum_v2or3(journal))
1606 journal->j_csum_seed = jbd2_chksum(journal, ~0, sb->s_uuid,
1607 sizeof(sb->s_uuid));
1608
1609 set_buffer_verified(bh);
1610
1611 return 0;
1612
1613out:
1614 journal_fail_superblock(journal);
1615 return err;
1616}
1617
1618
1619
1620
1621
1622
1623static int load_superblock(journal_t *journal)
1624{
1625 int err;
1626 journal_superblock_t *sb;
1627
1628 err = journal_get_superblock(journal);
1629 if (err)
1630 return err;
1631
1632 sb = journal->j_superblock;
1633
1634 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1635 journal->j_tail = be32_to_cpu(sb->s_start);
1636 journal->j_first = be32_to_cpu(sb->s_first);
1637 journal->j_last = be32_to_cpu(sb->s_maxlen);
1638 journal->j_errno = be32_to_cpu(sb->s_errno);
1639
1640 return 0;
1641}
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652int jbd2_journal_load(journal_t *journal)
1653{
1654 int err;
1655 journal_superblock_t *sb;
1656
1657 err = load_superblock(journal);
1658 if (err)
1659 return err;
1660
1661 sb = journal->j_superblock;
1662
1663
1664
1665 if (journal->j_format_version >= 2) {
1666 if ((sb->s_feature_ro_compat &
1667 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
1668 (sb->s_feature_incompat &
1669 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
1670 printk(KERN_WARNING
1671 "JBD2: Unrecognised features on journal\n");
1672 return -EINVAL;
1673 }
1674 }
1675
1676
1677
1678
1679 err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize));
1680 if (err)
1681 return err;
1682
1683
1684
1685 if (jbd2_journal_recover(journal))
1686 goto recovery_error;
1687
1688 if (journal->j_failed_commit) {
1689 printk(KERN_ERR "JBD2: journal transaction %u on %s "
1690 "is corrupt.\n", journal->j_failed_commit,
1691 journal->j_devname);
1692 return -EIO;
1693 }
1694
1695
1696
1697
1698 journal->j_flags &= ~JBD2_ABORT;
1699
1700
1701
1702
1703 if (journal_reset(journal))
1704 goto recovery_error;
1705
1706 journal->j_flags |= JBD2_LOADED;
1707 return 0;
1708
1709recovery_error:
1710 printk(KERN_WARNING "JBD2: recovery failed\n");
1711 return -EIO;
1712}
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722int jbd2_journal_destroy(journal_t *journal)
1723{
1724 int err = 0;
1725
1726
1727 journal_kill_thread(journal);
1728
1729
1730 if (journal->j_running_transaction)
1731 jbd2_journal_commit_transaction(journal);
1732
1733
1734
1735
1736 spin_lock(&journal->j_list_lock);
1737 while (journal->j_checkpoint_transactions != NULL) {
1738 spin_unlock(&journal->j_list_lock);
1739 mutex_lock(&journal->j_checkpoint_mutex);
1740 err = jbd2_log_do_checkpoint(journal);
1741 mutex_unlock(&journal->j_checkpoint_mutex);
1742
1743
1744
1745
1746 if (err) {
1747 jbd2_journal_destroy_checkpoint(journal);
1748 spin_lock(&journal->j_list_lock);
1749 break;
1750 }
1751 spin_lock(&journal->j_list_lock);
1752 }
1753
1754 J_ASSERT(journal->j_running_transaction == NULL);
1755 J_ASSERT(journal->j_committing_transaction == NULL);
1756 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1757 spin_unlock(&journal->j_list_lock);
1758
1759 if (journal->j_sb_buffer) {
1760 if (!is_journal_aborted(journal)) {
1761 mutex_lock(&journal->j_checkpoint_mutex);
1762
1763 write_lock(&journal->j_state_lock);
1764 journal->j_tail_sequence =
1765 ++journal->j_transaction_sequence;
1766 write_unlock(&journal->j_state_lock);
1767
1768 jbd2_mark_journal_empty(journal, WRITE_FLUSH_FUA);
1769 mutex_unlock(&journal->j_checkpoint_mutex);
1770 } else
1771 err = -EIO;
1772 brelse(journal->j_sb_buffer);
1773 }
1774
1775 if (journal->j_proc_entry)
1776 jbd2_stats_proc_exit(journal);
1777 iput(journal->j_inode);
1778 if (journal->j_revoke)
1779 jbd2_journal_destroy_revoke(journal);
1780 if (journal->j_chksum_driver)
1781 crypto_free_shash(journal->j_chksum_driver);
1782 kfree(journal->j_wbuf);
1783 kfree(journal);
1784
1785 return err;
1786}
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
1801 unsigned long ro, unsigned long incompat)
1802{
1803 journal_superblock_t *sb;
1804
1805 if (!compat && !ro && !incompat)
1806 return 1;
1807
1808 if (journal->j_format_version == 0 &&
1809 journal_get_superblock(journal) != 0)
1810 return 0;
1811 if (journal->j_format_version == 1)
1812 return 0;
1813
1814 sb = journal->j_superblock;
1815
1816 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1817 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1818 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1819 return 1;
1820
1821 return 0;
1822}
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
1836 unsigned long ro, unsigned long incompat)
1837{
1838 if (!compat && !ro && !incompat)
1839 return 1;
1840
1841
1842
1843
1844
1845 if (journal->j_format_version != 2)
1846 return 0;
1847
1848 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1849 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1850 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
1851 return 1;
1852
1853 return 0;
1854}
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
1869 unsigned long ro, unsigned long incompat)
1870{
1871#define INCOMPAT_FEATURE_ON(f) \
1872 ((incompat & (f)) && !(sb->s_feature_incompat & cpu_to_be32(f)))
1873#define COMPAT_FEATURE_ON(f) \
1874 ((compat & (f)) && !(sb->s_feature_compat & cpu_to_be32(f)))
1875 journal_superblock_t *sb;
1876
1877 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
1878 return 1;
1879
1880 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
1881 return 0;
1882
1883
1884 if (incompat & JBD2_FEATURE_INCOMPAT_CSUM_V2) {
1885 incompat &= ~JBD2_FEATURE_INCOMPAT_CSUM_V2;
1886 incompat |= JBD2_FEATURE_INCOMPAT_CSUM_V3;
1887 }
1888
1889
1890 if (incompat & JBD2_FEATURE_INCOMPAT_CSUM_V3 &&
1891 compat & JBD2_FEATURE_COMPAT_CHECKSUM)
1892 compat &= ~JBD2_FEATURE_COMPAT_CHECKSUM;
1893
1894 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1895 compat, ro, incompat);
1896
1897 sb = journal->j_superblock;
1898
1899
1900 if (INCOMPAT_FEATURE_ON(JBD2_FEATURE_INCOMPAT_CSUM_V3)) {
1901 sb->s_checksum_type = JBD2_CRC32C_CHKSUM;
1902 sb->s_feature_compat &=
1903 ~cpu_to_be32(JBD2_FEATURE_COMPAT_CHECKSUM);
1904
1905
1906 if (journal->j_chksum_driver == NULL) {
1907 journal->j_chksum_driver = crypto_alloc_shash("crc32c",
1908 0, 0);
1909 if (IS_ERR(journal->j_chksum_driver)) {
1910 printk(KERN_ERR "JBD2: Cannot load crc32c "
1911 "driver.\n");
1912 journal->j_chksum_driver = NULL;
1913 return 0;
1914 }
1915
1916
1917 journal->j_csum_seed = jbd2_chksum(journal, ~0,
1918 sb->s_uuid,
1919 sizeof(sb->s_uuid));
1920 }
1921 }
1922
1923
1924 if (COMPAT_FEATURE_ON(JBD2_FEATURE_COMPAT_CHECKSUM))
1925 sb->s_feature_incompat &=
1926 ~cpu_to_be32(JBD2_FEATURE_INCOMPAT_CSUM_V2 |
1927 JBD2_FEATURE_INCOMPAT_CSUM_V3);
1928
1929 sb->s_feature_compat |= cpu_to_be32(compat);
1930 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1931 sb->s_feature_incompat |= cpu_to_be32(incompat);
1932
1933 return 1;
1934#undef COMPAT_FEATURE_ON
1935#undef INCOMPAT_FEATURE_ON
1936}
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1950 unsigned long ro, unsigned long incompat)
1951{
1952 journal_superblock_t *sb;
1953
1954 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1955 compat, ro, incompat);
1956
1957 sb = journal->j_superblock;
1958
1959 sb->s_feature_compat &= ~cpu_to_be32(compat);
1960 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1961 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1962}
1963EXPORT_SYMBOL(jbd2_journal_clear_features);
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974int jbd2_journal_flush(journal_t *journal)
1975{
1976 int err = 0;
1977 transaction_t *transaction = NULL;
1978
1979 write_lock(&journal->j_state_lock);
1980
1981
1982 if (journal->j_running_transaction) {
1983 transaction = journal->j_running_transaction;
1984 __jbd2_log_start_commit(journal, transaction->t_tid);
1985 } else if (journal->j_committing_transaction)
1986 transaction = journal->j_committing_transaction;
1987
1988
1989 if (transaction) {
1990 tid_t tid = transaction->t_tid;
1991
1992 write_unlock(&journal->j_state_lock);
1993 jbd2_log_wait_commit(journal, tid);
1994 } else {
1995 write_unlock(&journal->j_state_lock);
1996 }
1997
1998
1999 spin_lock(&journal->j_list_lock);
2000 while (!err && journal->j_checkpoint_transactions != NULL) {
2001 spin_unlock(&journal->j_list_lock);
2002 mutex_lock(&journal->j_checkpoint_mutex);
2003 err = jbd2_log_do_checkpoint(journal);
2004 mutex_unlock(&journal->j_checkpoint_mutex);
2005 spin_lock(&journal->j_list_lock);
2006 }
2007 spin_unlock(&journal->j_list_lock);
2008
2009 if (is_journal_aborted(journal))
2010 return -EIO;
2011
2012 mutex_lock(&journal->j_checkpoint_mutex);
2013 if (!err) {
2014 err = jbd2_cleanup_journal_tail(journal);
2015 if (err < 0) {
2016 mutex_unlock(&journal->j_checkpoint_mutex);
2017 goto out;
2018 }
2019 err = 0;
2020 }
2021
2022
2023
2024
2025
2026
2027 jbd2_mark_journal_empty(journal, WRITE_FUA);
2028 mutex_unlock(&journal->j_checkpoint_mutex);
2029 write_lock(&journal->j_state_lock);
2030 J_ASSERT(!journal->j_running_transaction);
2031 J_ASSERT(!journal->j_committing_transaction);
2032 J_ASSERT(!journal->j_checkpoint_transactions);
2033 J_ASSERT(journal->j_head == journal->j_tail);
2034 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
2035 write_unlock(&journal->j_state_lock);
2036out:
2037 return err;
2038}
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053int jbd2_journal_wipe(journal_t *journal, int write)
2054{
2055 int err = 0;
2056
2057 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
2058
2059 err = load_superblock(journal);
2060 if (err)
2061 return err;
2062
2063 if (!journal->j_tail)
2064 goto no_recovery;
2065
2066 printk(KERN_WARNING "JBD2: %s recovery information on journal\n",
2067 write ? "Clearing" : "Ignoring");
2068
2069 err = jbd2_journal_skip_recovery(journal);
2070 if (write) {
2071
2072 mutex_lock(&journal->j_checkpoint_mutex);
2073 jbd2_mark_journal_empty(journal, WRITE_FUA);
2074 mutex_unlock(&journal->j_checkpoint_mutex);
2075 }
2076
2077 no_recovery:
2078 return err;
2079}
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094void __jbd2_journal_abort_hard(journal_t *journal)
2095{
2096 transaction_t *transaction;
2097
2098 if (journal->j_flags & JBD2_ABORT)
2099 return;
2100
2101 printk(KERN_ERR "Aborting journal on device %s.\n",
2102 journal->j_devname);
2103
2104 write_lock(&journal->j_state_lock);
2105 journal->j_flags |= JBD2_ABORT;
2106 transaction = journal->j_running_transaction;
2107 if (transaction)
2108 __jbd2_log_start_commit(journal, transaction->t_tid);
2109 write_unlock(&journal->j_state_lock);
2110}
2111
2112
2113
2114static void __journal_abort_soft (journal_t *journal, int errno)
2115{
2116 if (journal->j_flags & JBD2_ABORT)
2117 return;
2118
2119 if (!journal->j_errno)
2120 journal->j_errno = errno;
2121
2122 __jbd2_journal_abort_hard(journal);
2123
2124 jbd2_journal_update_sb_errno(journal);
2125 write_lock(&journal->j_state_lock);
2126 journal->j_flags |= JBD2_REC_ERR;
2127 write_unlock(&journal->j_state_lock);
2128}
2129
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
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171void jbd2_journal_abort(journal_t *journal, int errno)
2172{
2173 __journal_abort_soft(journal, errno);
2174}
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187int jbd2_journal_errno(journal_t *journal)
2188{
2189 int err;
2190
2191 read_lock(&journal->j_state_lock);
2192 if (journal->j_flags & JBD2_ABORT)
2193 err = -EROFS;
2194 else
2195 err = journal->j_errno;
2196 read_unlock(&journal->j_state_lock);
2197 return err;
2198}
2199
2200
2201
2202
2203
2204
2205
2206
2207int jbd2_journal_clear_err(journal_t *journal)
2208{
2209 int err = 0;
2210
2211 write_lock(&journal->j_state_lock);
2212 if (journal->j_flags & JBD2_ABORT)
2213 err = -EROFS;
2214 else
2215 journal->j_errno = 0;
2216 write_unlock(&journal->j_state_lock);
2217 return err;
2218}
2219
2220
2221
2222
2223
2224
2225
2226
2227void jbd2_journal_ack_err(journal_t *journal)
2228{
2229 write_lock(&journal->j_state_lock);
2230 if (journal->j_errno)
2231 journal->j_flags |= JBD2_ACK_ERR;
2232 write_unlock(&journal->j_state_lock);
2233}
2234
2235int jbd2_journal_blocks_per_page(struct inode *inode)
2236{
2237 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
2238}
2239
2240
2241
2242
2243size_t journal_tag_bytes(journal_t *journal)
2244{
2245 size_t sz;
2246
2247 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V3))
2248 return sizeof(journal_block_tag3_t);
2249
2250 sz = sizeof(journal_block_tag_t);
2251
2252 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2))
2253 sz += sizeof(__u16);
2254
2255 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
2256 return sz;
2257 else
2258 return sz - sizeof(__u32);
2259}
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276#define JBD2_MAX_SLABS 8
2277static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS];
2278
2279static const char *jbd2_slab_names[JBD2_MAX_SLABS] = {
2280 "jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k",
2281 "jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k"
2282};
2283
2284
2285static void jbd2_journal_destroy_slabs(void)
2286{
2287 int i;
2288
2289 for (i = 0; i < JBD2_MAX_SLABS; i++) {
2290 if (jbd2_slab[i])
2291 kmem_cache_destroy(jbd2_slab[i]);
2292 jbd2_slab[i] = NULL;
2293 }
2294}
2295
2296static int jbd2_journal_create_slab(size_t size)
2297{
2298 static DEFINE_MUTEX(jbd2_slab_create_mutex);
2299 int i = order_base_2(size) - 10;
2300 size_t slab_size;
2301
2302 if (size == PAGE_SIZE)
2303 return 0;
2304
2305 if (i >= JBD2_MAX_SLABS)
2306 return -EINVAL;
2307
2308 if (unlikely(i < 0))
2309 i = 0;
2310 mutex_lock(&jbd2_slab_create_mutex);
2311 if (jbd2_slab[i]) {
2312 mutex_unlock(&jbd2_slab_create_mutex);
2313 return 0;
2314 }
2315
2316 slab_size = 1 << (i+10);
2317 jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size,
2318 slab_size, 0, NULL);
2319 mutex_unlock(&jbd2_slab_create_mutex);
2320 if (!jbd2_slab[i]) {
2321 printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n");
2322 return -ENOMEM;
2323 }
2324 return 0;
2325}
2326
2327static struct kmem_cache *get_slab(size_t size)
2328{
2329 int i = order_base_2(size) - 10;
2330
2331 BUG_ON(i >= JBD2_MAX_SLABS);
2332 if (unlikely(i < 0))
2333 i = 0;
2334 BUG_ON(jbd2_slab[i] == NULL);
2335 return jbd2_slab[i];
2336}
2337
2338void *jbd2_alloc(size_t size, gfp_t flags)
2339{
2340 void *ptr;
2341
2342 BUG_ON(size & (size-1));
2343
2344 flags |= __GFP_REPEAT;
2345 if (size == PAGE_SIZE)
2346 ptr = (void *)__get_free_pages(flags, 0);
2347 else if (size > PAGE_SIZE) {
2348 int order = get_order(size);
2349
2350 if (order < 3)
2351 ptr = (void *)__get_free_pages(flags, order);
2352 else
2353 ptr = vmalloc(size);
2354 } else
2355 ptr = kmem_cache_alloc(get_slab(size), flags);
2356
2357
2358
2359 BUG_ON(((unsigned long) ptr) & (size-1));
2360
2361 return ptr;
2362}
2363
2364void jbd2_free(void *ptr, size_t size)
2365{
2366 if (size == PAGE_SIZE) {
2367 free_pages((unsigned long)ptr, 0);
2368 return;
2369 }
2370 if (size > PAGE_SIZE) {
2371 int order = get_order(size);
2372
2373 if (order < 3)
2374 free_pages((unsigned long)ptr, order);
2375 else
2376 vfree(ptr);
2377 return;
2378 }
2379 kmem_cache_free(get_slab(size), ptr);
2380};
2381
2382
2383
2384
2385static struct kmem_cache *jbd2_journal_head_cache;
2386#ifdef CONFIG_JBD2_DEBUG
2387static atomic_t nr_journal_heads = ATOMIC_INIT(0);
2388#endif
2389
2390static int jbd2_journal_init_journal_head_cache(void)
2391{
2392 int retval;
2393
2394 J_ASSERT(jbd2_journal_head_cache == NULL);
2395 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
2396 sizeof(struct journal_head),
2397 0,
2398 SLAB_TEMPORARY,
2399 NULL);
2400 retval = 0;
2401 if (!jbd2_journal_head_cache) {
2402 retval = -ENOMEM;
2403 printk(KERN_EMERG "JBD2: no memory for journal_head cache\n");
2404 }
2405 return retval;
2406}
2407
2408static void jbd2_journal_destroy_journal_head_cache(void)
2409{
2410 if (jbd2_journal_head_cache) {
2411 kmem_cache_destroy(jbd2_journal_head_cache);
2412 jbd2_journal_head_cache = NULL;
2413 }
2414}
2415
2416
2417
2418
2419static struct journal_head *journal_alloc_journal_head(void)
2420{
2421 struct journal_head *ret;
2422
2423#ifdef CONFIG_JBD2_DEBUG
2424 atomic_inc(&nr_journal_heads);
2425#endif
2426 ret = kmem_cache_zalloc(jbd2_journal_head_cache, GFP_NOFS);
2427 if (!ret) {
2428 jbd_debug(1, "out of memory for journal_head\n");
2429 pr_notice_ratelimited("ENOMEM in %s, retrying.\n", __func__);
2430 while (!ret) {
2431 yield();
2432 ret = kmem_cache_zalloc(jbd2_journal_head_cache, GFP_NOFS);
2433 }
2434 }
2435 return ret;
2436}
2437
2438static void journal_free_journal_head(struct journal_head *jh)
2439{
2440#ifdef CONFIG_JBD2_DEBUG
2441 atomic_dec(&nr_journal_heads);
2442 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
2443#endif
2444 kmem_cache_free(jbd2_journal_head_cache, jh);
2445}
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
2489{
2490 struct journal_head *jh;
2491 struct journal_head *new_jh = NULL;
2492
2493repeat:
2494 if (!buffer_jbd(bh))
2495 new_jh = journal_alloc_journal_head();
2496
2497 jbd_lock_bh_journal_head(bh);
2498 if (buffer_jbd(bh)) {
2499 jh = bh2jh(bh);
2500 } else {
2501 J_ASSERT_BH(bh,
2502 (atomic_read(&bh->b_count) > 0) ||
2503 (bh->b_page && bh->b_page->mapping));
2504
2505 if (!new_jh) {
2506 jbd_unlock_bh_journal_head(bh);
2507 goto repeat;
2508 }
2509
2510 jh = new_jh;
2511 new_jh = NULL;
2512 set_buffer_jbd(bh);
2513 bh->b_private = jh;
2514 jh->b_bh = bh;
2515 get_bh(bh);
2516 BUFFER_TRACE(bh, "added journal_head");
2517 }
2518 jh->b_jcount++;
2519 jbd_unlock_bh_journal_head(bh);
2520 if (new_jh)
2521 journal_free_journal_head(new_jh);
2522 return bh->b_private;
2523}
2524
2525
2526
2527
2528
2529struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
2530{
2531 struct journal_head *jh = NULL;
2532
2533 jbd_lock_bh_journal_head(bh);
2534 if (buffer_jbd(bh)) {
2535 jh = bh2jh(bh);
2536 jh->b_jcount++;
2537 }
2538 jbd_unlock_bh_journal_head(bh);
2539 return jh;
2540}
2541
2542static void __journal_remove_journal_head(struct buffer_head *bh)
2543{
2544 struct journal_head *jh = bh2jh(bh);
2545
2546 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2547 J_ASSERT_JH(jh, jh->b_transaction == NULL);
2548 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
2549 J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
2550 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2551 J_ASSERT_BH(bh, buffer_jbd(bh));
2552 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2553 BUFFER_TRACE(bh, "remove journal_head");
2554 if (jh->b_frozen_data) {
2555 printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
2556 jbd2_free(jh->b_frozen_data, bh->b_size);
2557 }
2558 if (jh->b_committed_data) {
2559 printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
2560 jbd2_free(jh->b_committed_data, bh->b_size);
2561 }
2562 bh->b_private = NULL;
2563 jh->b_bh = NULL;
2564 clear_buffer_jbd(bh);
2565 journal_free_journal_head(jh);
2566}
2567
2568
2569
2570
2571
2572void jbd2_journal_put_journal_head(struct journal_head *jh)
2573{
2574 struct buffer_head *bh = jh2bh(jh);
2575
2576 jbd_lock_bh_journal_head(bh);
2577 J_ASSERT_JH(jh, jh->b_jcount > 0);
2578 --jh->b_jcount;
2579 if (!jh->b_jcount) {
2580 __journal_remove_journal_head(bh);
2581 jbd_unlock_bh_journal_head(bh);
2582 __brelse(bh);
2583 } else
2584 jbd_unlock_bh_journal_head(bh);
2585}
2586
2587
2588
2589
2590void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2591{
2592 jinode->i_transaction = NULL;
2593 jinode->i_next_transaction = NULL;
2594 jinode->i_vfs_inode = inode;
2595 jinode->i_flags = 0;
2596 INIT_LIST_HEAD(&jinode->i_list);
2597}
2598
2599
2600
2601
2602
2603
2604void jbd2_journal_release_jbd_inode(journal_t *journal,
2605 struct jbd2_inode *jinode)
2606{
2607 if (!journal)
2608 return;
2609restart:
2610 spin_lock(&journal->j_list_lock);
2611
2612 if (test_bit(__JI_COMMIT_RUNNING, &jinode->i_flags)) {
2613 wait_queue_head_t *wq;
2614 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2615 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2616 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2617 spin_unlock(&journal->j_list_lock);
2618 schedule();
2619 finish_wait(wq, &wait.wait);
2620 goto restart;
2621 }
2622
2623 if (jinode->i_transaction) {
2624 list_del(&jinode->i_list);
2625 jinode->i_transaction = NULL;
2626 }
2627 spin_unlock(&journal->j_list_lock);
2628}
2629
2630
2631#ifdef CONFIG_PROC_FS
2632
2633#define JBD2_STATS_PROC_NAME "fs/jbd2"
2634
2635static void __init jbd2_create_jbd_stats_proc_entry(void)
2636{
2637 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2638}
2639
2640static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2641{
2642 if (proc_jbd2_stats)
2643 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2644}
2645
2646#else
2647
2648#define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2649#define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2650
2651#endif
2652
2653struct kmem_cache *jbd2_handle_cache, *jbd2_inode_cache;
2654
2655static int __init jbd2_journal_init_handle_cache(void)
2656{
2657 jbd2_handle_cache = KMEM_CACHE(jbd2_journal_handle, SLAB_TEMPORARY);
2658 if (jbd2_handle_cache == NULL) {
2659 printk(KERN_EMERG "JBD2: failed to create handle cache\n");
2660 return -ENOMEM;
2661 }
2662 jbd2_inode_cache = KMEM_CACHE(jbd2_inode, 0);
2663 if (jbd2_inode_cache == NULL) {
2664 printk(KERN_EMERG "JBD2: failed to create inode cache\n");
2665 kmem_cache_destroy(jbd2_handle_cache);
2666 return -ENOMEM;
2667 }
2668 return 0;
2669}
2670
2671static void jbd2_journal_destroy_handle_cache(void)
2672{
2673 if (jbd2_handle_cache)
2674 kmem_cache_destroy(jbd2_handle_cache);
2675 if (jbd2_inode_cache)
2676 kmem_cache_destroy(jbd2_inode_cache);
2677
2678}
2679
2680
2681
2682
2683
2684static int __init journal_init_caches(void)
2685{
2686 int ret;
2687
2688 ret = jbd2_journal_init_revoke_caches();
2689 if (ret == 0)
2690 ret = jbd2_journal_init_journal_head_cache();
2691 if (ret == 0)
2692 ret = jbd2_journal_init_handle_cache();
2693 if (ret == 0)
2694 ret = jbd2_journal_init_transaction_cache();
2695 return ret;
2696}
2697
2698static void jbd2_journal_destroy_caches(void)
2699{
2700 jbd2_journal_destroy_revoke_caches();
2701 jbd2_journal_destroy_journal_head_cache();
2702 jbd2_journal_destroy_handle_cache();
2703 jbd2_journal_destroy_transaction_cache();
2704 jbd2_journal_destroy_slabs();
2705}
2706
2707static int __init journal_init(void)
2708{
2709 int ret;
2710
2711 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2712
2713 ret = journal_init_caches();
2714 if (ret == 0) {
2715 jbd2_create_jbd_stats_proc_entry();
2716 } else {
2717 jbd2_journal_destroy_caches();
2718 }
2719 return ret;
2720}
2721
2722static void __exit journal_exit(void)
2723{
2724#ifdef CONFIG_JBD2_DEBUG
2725 int n = atomic_read(&nr_journal_heads);
2726 if (n)
2727 printk(KERN_ERR "JBD2: leaked %d journal_heads!\n", n);
2728#endif
2729 jbd2_remove_jbd_stats_proc_entry();
2730 jbd2_journal_destroy_caches();
2731}
2732
2733MODULE_LICENSE("GPL");
2734module_init(journal_init);
2735module_exit(journal_exit);
2736
2737