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