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/jbd.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
40#include <asm/uaccess.h>
41#include <asm/page.h>
42
43EXPORT_SYMBOL(journal_start);
44EXPORT_SYMBOL(journal_restart);
45EXPORT_SYMBOL(journal_extend);
46EXPORT_SYMBOL(journal_stop);
47EXPORT_SYMBOL(journal_lock_updates);
48EXPORT_SYMBOL(journal_unlock_updates);
49EXPORT_SYMBOL(journal_get_write_access);
50EXPORT_SYMBOL(journal_get_create_access);
51EXPORT_SYMBOL(journal_get_undo_access);
52EXPORT_SYMBOL(journal_dirty_data);
53EXPORT_SYMBOL(journal_dirty_metadata);
54EXPORT_SYMBOL(journal_release_buffer);
55EXPORT_SYMBOL(journal_forget);
56#if 0
57EXPORT_SYMBOL(journal_sync_buffer);
58#endif
59EXPORT_SYMBOL(journal_flush);
60EXPORT_SYMBOL(journal_revoke);
61
62EXPORT_SYMBOL(journal_init_dev);
63EXPORT_SYMBOL(journal_init_inode);
64EXPORT_SYMBOL(journal_update_format);
65EXPORT_SYMBOL(journal_check_used_features);
66EXPORT_SYMBOL(journal_check_available_features);
67EXPORT_SYMBOL(journal_set_features);
68EXPORT_SYMBOL(journal_create);
69EXPORT_SYMBOL(journal_load);
70EXPORT_SYMBOL(journal_destroy);
71EXPORT_SYMBOL(journal_update_superblock);
72EXPORT_SYMBOL(journal_abort);
73EXPORT_SYMBOL(journal_errno);
74EXPORT_SYMBOL(journal_ack_err);
75EXPORT_SYMBOL(journal_clear_err);
76EXPORT_SYMBOL(log_wait_commit);
77EXPORT_SYMBOL(journal_start_commit);
78EXPORT_SYMBOL(journal_force_commit_nested);
79EXPORT_SYMBOL(journal_wipe);
80EXPORT_SYMBOL(journal_blocks_per_page);
81EXPORT_SYMBOL(journal_invalidatepage);
82EXPORT_SYMBOL(journal_try_to_free_buffers);
83EXPORT_SYMBOL(journal_force_commit);
84
85static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
86static void __journal_abort_soft (journal_t *journal, int errno);
87
88
89
90
91
92static void commit_timeout(unsigned long __data)
93{
94 struct task_struct * p = (struct task_struct *) __data;
95
96 wake_up_process(p);
97}
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115static int kjournald(void *arg)
116{
117 journal_t *journal = arg;
118 transaction_t *transaction;
119
120
121
122
123
124 setup_timer(&journal->j_commit_timer, commit_timeout,
125 (unsigned long)current);
126
127
128 journal->j_task = current;
129 wake_up(&journal->j_wait_done_commit);
130
131 printk(KERN_INFO "kjournald starting. Commit interval %ld seconds\n",
132 journal->j_commit_interval / HZ);
133
134
135
136
137 spin_lock(&journal->j_state_lock);
138
139loop:
140 if (journal->j_flags & JFS_UNMOUNT)
141 goto end_loop;
142
143 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
144 journal->j_commit_sequence, journal->j_commit_request);
145
146 if (journal->j_commit_sequence != journal->j_commit_request) {
147 jbd_debug(1, "OK, requests differ\n");
148 spin_unlock(&journal->j_state_lock);
149 del_timer_sync(&journal->j_commit_timer);
150 journal_commit_transaction(journal);
151 spin_lock(&journal->j_state_lock);
152 goto loop;
153 }
154
155 wake_up(&journal->j_wait_done_commit);
156 if (freezing(current)) {
157
158
159
160
161
162 jbd_debug(1, "Now suspending kjournald\n");
163 spin_unlock(&journal->j_state_lock);
164 refrigerator();
165 spin_lock(&journal->j_state_lock);
166 } else {
167
168
169
170
171 DEFINE_WAIT(wait);
172 int should_sleep = 1;
173
174 prepare_to_wait(&journal->j_wait_commit, &wait,
175 TASK_INTERRUPTIBLE);
176 if (journal->j_commit_sequence != journal->j_commit_request)
177 should_sleep = 0;
178 transaction = journal->j_running_transaction;
179 if (transaction && time_after_eq(jiffies,
180 transaction->t_expires))
181 should_sleep = 0;
182 if (journal->j_flags & JFS_UNMOUNT)
183 should_sleep = 0;
184 if (should_sleep) {
185 spin_unlock(&journal->j_state_lock);
186 schedule();
187 spin_lock(&journal->j_state_lock);
188 }
189 finish_wait(&journal->j_wait_commit, &wait);
190 }
191
192 jbd_debug(1, "kjournald wakes\n");
193
194
195
196
197 transaction = journal->j_running_transaction;
198 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
199 journal->j_commit_request = transaction->t_tid;
200 jbd_debug(1, "woke because of timeout\n");
201 }
202 goto loop;
203
204end_loop:
205 spin_unlock(&journal->j_state_lock);
206 del_timer_sync(&journal->j_commit_timer);
207 journal->j_task = NULL;
208 wake_up(&journal->j_wait_done_commit);
209 jbd_debug(1, "Journal thread exiting.\n");
210 return 0;
211}
212
213static int journal_start_thread(journal_t *journal)
214{
215 struct task_struct *t;
216
217 t = kthread_run(kjournald, journal, "kjournald");
218 if (IS_ERR(t))
219 return PTR_ERR(t);
220
221 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
222 return 0;
223}
224
225static void journal_kill_thread(journal_t *journal)
226{
227 spin_lock(&journal->j_state_lock);
228 journal->j_flags |= JFS_UNMOUNT;
229
230 while (journal->j_task) {
231 wake_up(&journal->j_wait_commit);
232 spin_unlock(&journal->j_state_lock);
233 wait_event(journal->j_wait_done_commit,
234 journal->j_task == NULL);
235 spin_lock(&journal->j_state_lock);
236 }
237 spin_unlock(&journal->j_state_lock);
238}
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277int journal_write_metadata_buffer(transaction_t *transaction,
278 struct journal_head *jh_in,
279 struct journal_head **jh_out,
280 unsigned long blocknr)
281{
282 int need_copy_out = 0;
283 int done_copy_out = 0;
284 int do_escape = 0;
285 char *mapped_data;
286 struct buffer_head *new_bh;
287 struct journal_head *new_jh;
288 struct page *new_page;
289 unsigned int new_offset;
290 struct buffer_head *bh_in = jh2bh(jh_in);
291
292
293
294
295
296
297
298
299
300
301 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
302
303 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
304
305
306
307
308
309 jbd_lock_bh_state(bh_in);
310repeat:
311 if (jh_in->b_frozen_data) {
312 done_copy_out = 1;
313 new_page = virt_to_page(jh_in->b_frozen_data);
314 new_offset = offset_in_page(jh_in->b_frozen_data);
315 } else {
316 new_page = jh2bh(jh_in)->b_page;
317 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
318 }
319
320 mapped_data = kmap_atomic(new_page, KM_USER0);
321
322
323
324 if (*((__be32 *)(mapped_data + new_offset)) ==
325 cpu_to_be32(JFS_MAGIC_NUMBER)) {
326 need_copy_out = 1;
327 do_escape = 1;
328 }
329 kunmap_atomic(mapped_data, KM_USER0);
330
331
332
333
334 if (need_copy_out && !done_copy_out) {
335 char *tmp;
336
337 jbd_unlock_bh_state(bh_in);
338 tmp = jbd_alloc(bh_in->b_size, GFP_NOFS);
339 jbd_lock_bh_state(bh_in);
340 if (jh_in->b_frozen_data) {
341 jbd_free(tmp, bh_in->b_size);
342 goto repeat;
343 }
344
345 jh_in->b_frozen_data = tmp;
346 mapped_data = kmap_atomic(new_page, KM_USER0);
347 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
348 kunmap_atomic(mapped_data, KM_USER0);
349
350 new_page = virt_to_page(tmp);
351 new_offset = offset_in_page(tmp);
352 done_copy_out = 1;
353 }
354
355
356
357
358
359 if (do_escape) {
360 mapped_data = kmap_atomic(new_page, KM_USER0);
361 *((unsigned int *)(mapped_data + new_offset)) = 0;
362 kunmap_atomic(mapped_data, KM_USER0);
363 }
364
365
366 new_bh->b_state = 0;
367 init_buffer(new_bh, NULL, NULL);
368 atomic_set(&new_bh->b_count, 1);
369 jbd_unlock_bh_state(bh_in);
370
371 new_jh = journal_add_journal_head(new_bh);
372
373 set_bh_page(new_bh, new_page, new_offset);
374 new_jh->b_transaction = NULL;
375 new_bh->b_size = jh2bh(jh_in)->b_size;
376 new_bh->b_bdev = transaction->t_journal->j_dev;
377 new_bh->b_blocknr = blocknr;
378 set_buffer_mapped(new_bh);
379 set_buffer_dirty(new_bh);
380
381 *jh_out = new_jh;
382
383
384
385
386
387
388 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
389 journal_file_buffer(jh_in, transaction, BJ_Shadow);
390 JBUFFER_TRACE(new_jh, "file as BJ_IO");
391 journal_file_buffer(new_jh, transaction, BJ_IO);
392
393 return do_escape | (done_copy_out << 1);
394}
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409int __log_space_left(journal_t *journal)
410{
411 int left = journal->j_free;
412
413 assert_spin_locked(&journal->j_state_lock);
414
415
416
417
418
419
420#define MIN_LOG_RESERVED_BLOCKS 32
421
422 left -= MIN_LOG_RESERVED_BLOCKS;
423
424 if (left <= 0)
425 return 0;
426 left -= (left >> 3);
427 return left;
428}
429
430
431
432
433int __log_start_commit(journal_t *journal, tid_t target)
434{
435
436
437
438 if (!tid_geq(journal->j_commit_request, target)) {
439
440
441
442
443
444 journal->j_commit_request = target;
445 jbd_debug(1, "JBD: requesting commit %d/%d\n",
446 journal->j_commit_request,
447 journal->j_commit_sequence);
448 wake_up(&journal->j_wait_commit);
449 return 1;
450 }
451 return 0;
452}
453
454int log_start_commit(journal_t *journal, tid_t tid)
455{
456 int ret;
457
458 spin_lock(&journal->j_state_lock);
459 ret = __log_start_commit(journal, tid);
460 spin_unlock(&journal->j_state_lock);
461 return ret;
462}
463
464
465
466
467
468
469
470
471
472
473
474int journal_force_commit_nested(journal_t *journal)
475{
476 transaction_t *transaction = NULL;
477 tid_t tid;
478
479 spin_lock(&journal->j_state_lock);
480 if (journal->j_running_transaction && !current->journal_info) {
481 transaction = journal->j_running_transaction;
482 __log_start_commit(journal, transaction->t_tid);
483 } else if (journal->j_committing_transaction)
484 transaction = journal->j_committing_transaction;
485
486 if (!transaction) {
487 spin_unlock(&journal->j_state_lock);
488 return 0;
489 }
490
491 tid = transaction->t_tid;
492 spin_unlock(&journal->j_state_lock);
493 log_wait_commit(journal, tid);
494 return 1;
495}
496
497
498
499
500
501int journal_start_commit(journal_t *journal, tid_t *ptid)
502{
503 int ret = 0;
504
505 spin_lock(&journal->j_state_lock);
506 if (journal->j_running_transaction) {
507 tid_t tid = journal->j_running_transaction->t_tid;
508
509 ret = __log_start_commit(journal, tid);
510 if (ret && ptid)
511 *ptid = tid;
512 } else if (journal->j_committing_transaction && ptid) {
513
514
515
516
517 *ptid = journal->j_committing_transaction->t_tid;
518 ret = 1;
519 }
520 spin_unlock(&journal->j_state_lock);
521 return ret;
522}
523
524
525
526
527
528int log_wait_commit(journal_t *journal, tid_t tid)
529{
530 int err = 0;
531
532#ifdef CONFIG_JBD_DEBUG
533 spin_lock(&journal->j_state_lock);
534 if (!tid_geq(journal->j_commit_request, tid)) {
535 printk(KERN_EMERG
536 "%s: error: j_commit_request=%d, tid=%d\n",
537 __FUNCTION__, journal->j_commit_request, tid);
538 }
539 spin_unlock(&journal->j_state_lock);
540#endif
541 spin_lock(&journal->j_state_lock);
542 while (tid_gt(tid, journal->j_commit_sequence)) {
543 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
544 tid, journal->j_commit_sequence);
545 wake_up(&journal->j_wait_commit);
546 spin_unlock(&journal->j_state_lock);
547 wait_event(journal->j_wait_done_commit,
548 !tid_gt(tid, journal->j_commit_sequence));
549 spin_lock(&journal->j_state_lock);
550 }
551 spin_unlock(&journal->j_state_lock);
552
553 if (unlikely(is_journal_aborted(journal))) {
554 printk(KERN_EMERG "journal commit I/O error\n");
555 err = -EIO;
556 }
557 return err;
558}
559
560
561
562
563
564int journal_next_log_block(journal_t *journal, unsigned long *retp)
565{
566 unsigned long blocknr;
567
568 spin_lock(&journal->j_state_lock);
569 J_ASSERT(journal->j_free > 1);
570
571 blocknr = journal->j_head;
572 journal->j_head++;
573 journal->j_free--;
574 if (journal->j_head == journal->j_last)
575 journal->j_head = journal->j_first;
576 spin_unlock(&journal->j_state_lock);
577 return journal_bmap(journal, blocknr, retp);
578}
579
580
581
582
583
584
585
586
587int journal_bmap(journal_t *journal, unsigned long blocknr,
588 unsigned long *retp)
589{
590 int err = 0;
591 unsigned long ret;
592
593 if (journal->j_inode) {
594 ret = bmap(journal->j_inode, blocknr);
595 if (ret)
596 *retp = ret;
597 else {
598 char b[BDEVNAME_SIZE];
599
600 printk(KERN_ALERT "%s: journal block not found "
601 "at offset %lu on %s\n",
602 __FUNCTION__,
603 blocknr,
604 bdevname(journal->j_dev, b));
605 err = -EIO;
606 __journal_abort_soft(journal, err);
607 }
608 } else {
609 *retp = blocknr;
610 }
611 return err;
612}
613
614
615
616
617
618
619
620
621
622
623
624struct journal_head *journal_get_descriptor_buffer(journal_t *journal)
625{
626 struct buffer_head *bh;
627 unsigned long blocknr;
628 int err;
629
630 err = journal_next_log_block(journal, &blocknr);
631
632 if (err)
633 return NULL;
634
635 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
636 lock_buffer(bh);
637 memset(bh->b_data, 0, journal->j_blocksize);
638 set_buffer_uptodate(bh);
639 unlock_buffer(bh);
640 BUFFER_TRACE(bh, "return this buffer");
641 return journal_add_journal_head(bh);
642}
643
644
645
646
647
648
649
650
651
652
653static journal_t * journal_init_common (void)
654{
655 journal_t *journal;
656 int err;
657
658 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
659 if (!journal)
660 goto fail;
661
662 init_waitqueue_head(&journal->j_wait_transaction_locked);
663 init_waitqueue_head(&journal->j_wait_logspace);
664 init_waitqueue_head(&journal->j_wait_done_commit);
665 init_waitqueue_head(&journal->j_wait_checkpoint);
666 init_waitqueue_head(&journal->j_wait_commit);
667 init_waitqueue_head(&journal->j_wait_updates);
668 mutex_init(&journal->j_barrier);
669 mutex_init(&journal->j_checkpoint_mutex);
670 spin_lock_init(&journal->j_revoke_lock);
671 spin_lock_init(&journal->j_list_lock);
672 spin_lock_init(&journal->j_state_lock);
673
674 journal->j_commit_interval = (HZ * JBD_DEFAULT_MAX_COMMIT_AGE);
675
676
677 journal->j_flags = JFS_ABORT;
678
679
680 err = journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
681 if (err) {
682 kfree(journal);
683 goto fail;
684 }
685 return journal;
686fail:
687 return NULL;
688}
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712journal_t * journal_init_dev(struct block_device *bdev,
713 struct block_device *fs_dev,
714 int start, int len, int blocksize)
715{
716 journal_t *journal = journal_init_common();
717 struct buffer_head *bh;
718 int n;
719
720 if (!journal)
721 return NULL;
722
723
724 journal->j_blocksize = blocksize;
725 n = journal->j_blocksize / sizeof(journal_block_tag_t);
726 journal->j_wbufsize = n;
727 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
728 if (!journal->j_wbuf) {
729 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
730 __FUNCTION__);
731 kfree(journal);
732 journal = NULL;
733 goto out;
734 }
735 journal->j_dev = bdev;
736 journal->j_fs_dev = fs_dev;
737 journal->j_blk_offset = start;
738 journal->j_maxlen = len;
739
740 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
741 J_ASSERT(bh != NULL);
742 journal->j_sb_buffer = bh;
743 journal->j_superblock = (journal_superblock_t *)bh->b_data;
744out:
745 return journal;
746}
747
748
749
750
751
752
753
754
755
756journal_t * journal_init_inode (struct inode *inode)
757{
758 struct buffer_head *bh;
759 journal_t *journal = journal_init_common();
760 int err;
761 int n;
762 unsigned long blocknr;
763
764 if (!journal)
765 return NULL;
766
767 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
768 journal->j_inode = inode;
769 jbd_debug(1,
770 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
771 journal, inode->i_sb->s_id, inode->i_ino,
772 (long long) inode->i_size,
773 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
774
775 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
776 journal->j_blocksize = inode->i_sb->s_blocksize;
777
778
779 n = journal->j_blocksize / sizeof(journal_block_tag_t);
780 journal->j_wbufsize = n;
781 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
782 if (!journal->j_wbuf) {
783 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
784 __FUNCTION__);
785 kfree(journal);
786 return NULL;
787 }
788
789 err = journal_bmap(journal, 0, &blocknr);
790
791 if (err) {
792 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
793 __FUNCTION__);
794 kfree(journal);
795 return NULL;
796 }
797
798 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
799 J_ASSERT(bh != NULL);
800 journal->j_sb_buffer = bh;
801 journal->j_superblock = (journal_superblock_t *)bh->b_data;
802
803 return journal;
804}
805
806
807
808
809
810
811static void journal_fail_superblock (journal_t *journal)
812{
813 struct buffer_head *bh = journal->j_sb_buffer;
814 brelse(bh);
815 journal->j_sb_buffer = NULL;
816}
817
818
819
820
821
822
823
824
825static int journal_reset(journal_t *journal)
826{
827 journal_superblock_t *sb = journal->j_superblock;
828 unsigned long first, last;
829
830 first = be32_to_cpu(sb->s_first);
831 last = be32_to_cpu(sb->s_maxlen);
832
833 journal->j_first = first;
834 journal->j_last = last;
835
836 journal->j_head = first;
837 journal->j_tail = first;
838 journal->j_free = last - first;
839
840 journal->j_tail_sequence = journal->j_transaction_sequence;
841 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
842 journal->j_commit_request = journal->j_commit_sequence;
843
844 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
845
846
847 journal_update_superblock(journal, 1);
848 return journal_start_thread(journal);
849}
850
851
852
853
854
855
856
857
858
859int journal_create(journal_t *journal)
860{
861 unsigned long blocknr;
862 struct buffer_head *bh;
863 journal_superblock_t *sb;
864 int i, err;
865
866 if (journal->j_maxlen < JFS_MIN_JOURNAL_BLOCKS) {
867 printk (KERN_ERR "Journal length (%d blocks) too short.\n",
868 journal->j_maxlen);
869 journal_fail_superblock(journal);
870 return -EINVAL;
871 }
872
873 if (journal->j_inode == NULL) {
874
875
876
877 printk(KERN_EMERG
878 "%s: creation of journal on external device!\n",
879 __FUNCTION__);
880 BUG();
881 }
882
883
884
885 jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
886 for (i = 0; i < journal->j_maxlen; i++) {
887 err = journal_bmap(journal, i, &blocknr);
888 if (err)
889 return err;
890 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
891 lock_buffer(bh);
892 memset (bh->b_data, 0, journal->j_blocksize);
893 BUFFER_TRACE(bh, "marking dirty");
894 mark_buffer_dirty(bh);
895 BUFFER_TRACE(bh, "marking uptodate");
896 set_buffer_uptodate(bh);
897 unlock_buffer(bh);
898 __brelse(bh);
899 }
900
901 sync_blockdev(journal->j_dev);
902 jbd_debug(1, "JBD: journal cleared.\n");
903
904
905 sb = journal->j_superblock;
906
907 sb->s_header.h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
908 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
909
910 sb->s_blocksize = cpu_to_be32(journal->j_blocksize);
911 sb->s_maxlen = cpu_to_be32(journal->j_maxlen);
912 sb->s_first = cpu_to_be32(1);
913
914 journal->j_transaction_sequence = 1;
915
916 journal->j_flags &= ~JFS_ABORT;
917 journal->j_format_version = 2;
918
919 return journal_reset(journal);
920}
921
922
923
924
925
926
927
928
929
930void journal_update_superblock(journal_t *journal, int wait)
931{
932 journal_superblock_t *sb = journal->j_superblock;
933 struct buffer_head *bh = journal->j_sb_buffer;
934
935
936
937
938
939
940
941
942 if (sb->s_start == 0 && journal->j_tail_sequence ==
943 journal->j_transaction_sequence) {
944 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
945 "(start %ld, seq %d, errno %d)\n",
946 journal->j_tail, journal->j_tail_sequence,
947 journal->j_errno);
948 goto out;
949 }
950
951 spin_lock(&journal->j_state_lock);
952 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
953 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
954
955 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
956 sb->s_start = cpu_to_be32(journal->j_tail);
957 sb->s_errno = cpu_to_be32(journal->j_errno);
958 spin_unlock(&journal->j_state_lock);
959
960 BUFFER_TRACE(bh, "marking dirty");
961 mark_buffer_dirty(bh);
962 if (wait)
963 sync_dirty_buffer(bh);
964 else
965 ll_rw_block(SWRITE, 1, &bh);
966
967out:
968
969
970
971
972 spin_lock(&journal->j_state_lock);
973 if (sb->s_start)
974 journal->j_flags &= ~JFS_FLUSHED;
975 else
976 journal->j_flags |= JFS_FLUSHED;
977 spin_unlock(&journal->j_state_lock);
978}
979
980
981
982
983
984
985static int journal_get_superblock(journal_t *journal)
986{
987 struct buffer_head *bh;
988 journal_superblock_t *sb;
989 int err = -EIO;
990
991 bh = journal->j_sb_buffer;
992
993 J_ASSERT(bh != NULL);
994 if (!buffer_uptodate(bh)) {
995 ll_rw_block(READ, 1, &bh);
996 wait_on_buffer(bh);
997 if (!buffer_uptodate(bh)) {
998 printk (KERN_ERR
999 "JBD: IO error reading journal superblock\n");
1000 goto out;
1001 }
1002 }
1003
1004 sb = journal->j_superblock;
1005
1006 err = -EINVAL;
1007
1008 if (sb->s_header.h_magic != cpu_to_be32(JFS_MAGIC_NUMBER) ||
1009 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1010 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1011 goto out;
1012 }
1013
1014 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1015 case JFS_SUPERBLOCK_V1:
1016 journal->j_format_version = 1;
1017 break;
1018 case JFS_SUPERBLOCK_V2:
1019 journal->j_format_version = 2;
1020 break;
1021 default:
1022 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1023 goto out;
1024 }
1025
1026 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1027 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1028 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1029 printk (KERN_WARNING "JBD: journal file too short\n");
1030 goto out;
1031 }
1032
1033 return 0;
1034
1035out:
1036 journal_fail_superblock(journal);
1037 return err;
1038}
1039
1040
1041
1042
1043
1044
1045static int load_superblock(journal_t *journal)
1046{
1047 int err;
1048 journal_superblock_t *sb;
1049
1050 err = journal_get_superblock(journal);
1051 if (err)
1052 return err;
1053
1054 sb = journal->j_superblock;
1055
1056 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1057 journal->j_tail = be32_to_cpu(sb->s_start);
1058 journal->j_first = be32_to_cpu(sb->s_first);
1059 journal->j_last = be32_to_cpu(sb->s_maxlen);
1060 journal->j_errno = be32_to_cpu(sb->s_errno);
1061
1062 return 0;
1063}
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074int journal_load(journal_t *journal)
1075{
1076 int err;
1077 journal_superblock_t *sb;
1078
1079 err = load_superblock(journal);
1080 if (err)
1081 return err;
1082
1083 sb = journal->j_superblock;
1084
1085
1086
1087 if (journal->j_format_version >= 2) {
1088 if ((sb->s_feature_ro_compat &
1089 ~cpu_to_be32(JFS_KNOWN_ROCOMPAT_FEATURES)) ||
1090 (sb->s_feature_incompat &
1091 ~cpu_to_be32(JFS_KNOWN_INCOMPAT_FEATURES))) {
1092 printk (KERN_WARNING
1093 "JBD: Unrecognised features on journal\n");
1094 return -EINVAL;
1095 }
1096 }
1097
1098
1099
1100 if (journal_recover(journal))
1101 goto recovery_error;
1102
1103
1104
1105
1106 if (journal_reset(journal))
1107 goto recovery_error;
1108
1109 journal->j_flags &= ~JFS_ABORT;
1110 journal->j_flags |= JFS_LOADED;
1111 return 0;
1112
1113recovery_error:
1114 printk (KERN_WARNING "JBD: recovery failed\n");
1115 return -EIO;
1116}
1117
1118
1119
1120
1121
1122
1123
1124
1125void journal_destroy(journal_t *journal)
1126{
1127
1128 journal_kill_thread(journal);
1129
1130
1131 if (journal->j_running_transaction)
1132 journal_commit_transaction(journal);
1133
1134
1135
1136
1137 spin_lock(&journal->j_list_lock);
1138 while (journal->j_checkpoint_transactions != NULL) {
1139 spin_unlock(&journal->j_list_lock);
1140 log_do_checkpoint(journal);
1141 spin_lock(&journal->j_list_lock);
1142 }
1143
1144 J_ASSERT(journal->j_running_transaction == NULL);
1145 J_ASSERT(journal->j_committing_transaction == NULL);
1146 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1147 spin_unlock(&journal->j_list_lock);
1148
1149
1150 journal->j_tail = 0;
1151 journal->j_tail_sequence = ++journal->j_transaction_sequence;
1152 if (journal->j_sb_buffer) {
1153 journal_update_superblock(journal, 1);
1154 brelse(journal->j_sb_buffer);
1155 }
1156
1157 if (journal->j_inode)
1158 iput(journal->j_inode);
1159 if (journal->j_revoke)
1160 journal_destroy_revoke(journal);
1161 kfree(journal->j_wbuf);
1162 kfree(journal);
1163}
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177int journal_check_used_features (journal_t *journal, unsigned long compat,
1178 unsigned long ro, unsigned long incompat)
1179{
1180 journal_superblock_t *sb;
1181
1182 if (!compat && !ro && !incompat)
1183 return 1;
1184 if (journal->j_format_version == 1)
1185 return 0;
1186
1187 sb = journal->j_superblock;
1188
1189 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1190 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1191 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1192 return 1;
1193
1194 return 0;
1195}
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208int journal_check_available_features (journal_t *journal, unsigned long compat,
1209 unsigned long ro, unsigned long incompat)
1210{
1211 journal_superblock_t *sb;
1212
1213 if (!compat && !ro && !incompat)
1214 return 1;
1215
1216 sb = journal->j_superblock;
1217
1218
1219
1220
1221
1222 if (journal->j_format_version != 2)
1223 return 0;
1224
1225 if ((compat & JFS_KNOWN_COMPAT_FEATURES) == compat &&
1226 (ro & JFS_KNOWN_ROCOMPAT_FEATURES) == ro &&
1227 (incompat & JFS_KNOWN_INCOMPAT_FEATURES) == incompat)
1228 return 1;
1229
1230 return 0;
1231}
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245int journal_set_features (journal_t *journal, unsigned long compat,
1246 unsigned long ro, unsigned long incompat)
1247{
1248 journal_superblock_t *sb;
1249
1250 if (journal_check_used_features(journal, compat, ro, incompat))
1251 return 1;
1252
1253 if (!journal_check_available_features(journal, compat, ro, incompat))
1254 return 0;
1255
1256 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1257 compat, ro, incompat);
1258
1259 sb = journal->j_superblock;
1260
1261 sb->s_feature_compat |= cpu_to_be32(compat);
1262 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1263 sb->s_feature_incompat |= cpu_to_be32(incompat);
1264
1265 return 1;
1266}
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276int journal_update_format (journal_t *journal)
1277{
1278 journal_superblock_t *sb;
1279 int err;
1280
1281 err = journal_get_superblock(journal);
1282 if (err)
1283 return err;
1284
1285 sb = journal->j_superblock;
1286
1287 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
1288 case JFS_SUPERBLOCK_V2:
1289 return 0;
1290 case JFS_SUPERBLOCK_V1:
1291 return journal_convert_superblock_v1(journal, sb);
1292 default:
1293 break;
1294 }
1295 return -EINVAL;
1296}
1297
1298static int journal_convert_superblock_v1(journal_t *journal,
1299 journal_superblock_t *sb)
1300{
1301 int offset, blocksize;
1302 struct buffer_head *bh;
1303
1304 printk(KERN_WARNING
1305 "JBD: Converting superblock from version 1 to 2.\n");
1306
1307
1308 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1309 blocksize = be32_to_cpu(sb->s_blocksize);
1310 memset(&sb->s_feature_compat, 0, blocksize-offset);
1311
1312 sb->s_nr_users = cpu_to_be32(1);
1313 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
1314 journal->j_format_version = 2;
1315
1316 bh = journal->j_sb_buffer;
1317 BUFFER_TRACE(bh, "marking dirty");
1318 mark_buffer_dirty(bh);
1319 sync_dirty_buffer(bh);
1320 return 0;
1321}
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333int journal_flush(journal_t *journal)
1334{
1335 int err = 0;
1336 transaction_t *transaction = NULL;
1337 unsigned long old_tail;
1338
1339 spin_lock(&journal->j_state_lock);
1340
1341
1342 if (journal->j_running_transaction) {
1343 transaction = journal->j_running_transaction;
1344 __log_start_commit(journal, transaction->t_tid);
1345 } else if (journal->j_committing_transaction)
1346 transaction = journal->j_committing_transaction;
1347
1348
1349 if (transaction) {
1350 tid_t tid = transaction->t_tid;
1351
1352 spin_unlock(&journal->j_state_lock);
1353 log_wait_commit(journal, tid);
1354 } else {
1355 spin_unlock(&journal->j_state_lock);
1356 }
1357
1358
1359 spin_lock(&journal->j_list_lock);
1360 while (!err && journal->j_checkpoint_transactions != NULL) {
1361 spin_unlock(&journal->j_list_lock);
1362 err = log_do_checkpoint(journal);
1363 spin_lock(&journal->j_list_lock);
1364 }
1365 spin_unlock(&journal->j_list_lock);
1366 cleanup_journal_tail(journal);
1367
1368
1369
1370
1371
1372
1373 spin_lock(&journal->j_state_lock);
1374 old_tail = journal->j_tail;
1375 journal->j_tail = 0;
1376 spin_unlock(&journal->j_state_lock);
1377 journal_update_superblock(journal, 1);
1378 spin_lock(&journal->j_state_lock);
1379 journal->j_tail = old_tail;
1380
1381 J_ASSERT(!journal->j_running_transaction);
1382 J_ASSERT(!journal->j_committing_transaction);
1383 J_ASSERT(!journal->j_checkpoint_transactions);
1384 J_ASSERT(journal->j_head == journal->j_tail);
1385 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1386 spin_unlock(&journal->j_state_lock);
1387 return err;
1388}
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403int journal_wipe(journal_t *journal, int write)
1404{
1405 journal_superblock_t *sb;
1406 int err = 0;
1407
1408 J_ASSERT (!(journal->j_flags & JFS_LOADED));
1409
1410 err = load_superblock(journal);
1411 if (err)
1412 return err;
1413
1414 sb = journal->j_superblock;
1415
1416 if (!journal->j_tail)
1417 goto no_recovery;
1418
1419 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1420 write ? "Clearing" : "Ignoring");
1421
1422 err = journal_skip_recovery(journal);
1423 if (write)
1424 journal_update_superblock(journal, 1);
1425
1426 no_recovery:
1427 return err;
1428}
1429
1430
1431
1432
1433
1434
1435static const char *journal_dev_name(journal_t *journal, char *buffer)
1436{
1437 struct block_device *bdev;
1438
1439 if (journal->j_inode)
1440 bdev = journal->j_inode->i_sb->s_bdev;
1441 else
1442 bdev = journal->j_dev;
1443
1444 return bdevname(bdev, buffer);
1445}
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460void __journal_abort_hard(journal_t *journal)
1461{
1462 transaction_t *transaction;
1463 char b[BDEVNAME_SIZE];
1464
1465 if (journal->j_flags & JFS_ABORT)
1466 return;
1467
1468 printk(KERN_ERR "Aborting journal on device %s.\n",
1469 journal_dev_name(journal, b));
1470
1471 spin_lock(&journal->j_state_lock);
1472 journal->j_flags |= JFS_ABORT;
1473 transaction = journal->j_running_transaction;
1474 if (transaction)
1475 __log_start_commit(journal, transaction->t_tid);
1476 spin_unlock(&journal->j_state_lock);
1477}
1478
1479
1480
1481static void __journal_abort_soft (journal_t *journal, int errno)
1482{
1483 if (journal->j_flags & JFS_ABORT)
1484 return;
1485
1486 if (!journal->j_errno)
1487 journal->j_errno = errno;
1488
1489 __journal_abort_hard(journal);
1490
1491 if (errno)
1492 journal_update_superblock(journal, 1);
1493}
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541void journal_abort(journal_t *journal, int errno)
1542{
1543 __journal_abort_soft(journal, errno);
1544}
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557int journal_errno(journal_t *journal)
1558{
1559 int err;
1560
1561 spin_lock(&journal->j_state_lock);
1562 if (journal->j_flags & JFS_ABORT)
1563 err = -EROFS;
1564 else
1565 err = journal->j_errno;
1566 spin_unlock(&journal->j_state_lock);
1567 return err;
1568}
1569
1570
1571
1572
1573
1574
1575
1576
1577int journal_clear_err(journal_t *journal)
1578{
1579 int err = 0;
1580
1581 spin_lock(&journal->j_state_lock);
1582 if (journal->j_flags & JFS_ABORT)
1583 err = -EROFS;
1584 else
1585 journal->j_errno = 0;
1586 spin_unlock(&journal->j_state_lock);
1587 return err;
1588}
1589
1590
1591
1592
1593
1594
1595
1596
1597void journal_ack_err(journal_t *journal)
1598{
1599 spin_lock(&journal->j_state_lock);
1600 if (journal->j_errno)
1601 journal->j_flags |= JFS_ACK_ERR;
1602 spin_unlock(&journal->j_state_lock);
1603}
1604
1605int journal_blocks_per_page(struct inode *inode)
1606{
1607 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1608}
1609
1610
1611
1612
1613static struct kmem_cache *journal_head_cache;
1614#ifdef CONFIG_JBD_DEBUG
1615static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1616#endif
1617
1618static int journal_init_journal_head_cache(void)
1619{
1620 int retval;
1621
1622 J_ASSERT(journal_head_cache == 0);
1623 journal_head_cache = kmem_cache_create("journal_head",
1624 sizeof(struct journal_head),
1625 0,
1626 SLAB_TEMPORARY,
1627 NULL);
1628 retval = 0;
1629 if (journal_head_cache == 0) {
1630 retval = -ENOMEM;
1631 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1632 }
1633 return retval;
1634}
1635
1636static void journal_destroy_journal_head_cache(void)
1637{
1638 J_ASSERT(journal_head_cache != NULL);
1639 kmem_cache_destroy(journal_head_cache);
1640 journal_head_cache = NULL;
1641}
1642
1643
1644
1645
1646static struct journal_head *journal_alloc_journal_head(void)
1647{
1648 struct journal_head *ret;
1649 static unsigned long last_warning;
1650
1651#ifdef CONFIG_JBD_DEBUG
1652 atomic_inc(&nr_journal_heads);
1653#endif
1654 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
1655 if (ret == NULL) {
1656 jbd_debug(1, "out of memory for journal_head\n");
1657 if (time_after(jiffies, last_warning + 5*HZ)) {
1658 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
1659 __FUNCTION__);
1660 last_warning = jiffies;
1661 }
1662 while (ret == NULL) {
1663 yield();
1664 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
1665 }
1666 }
1667 return ret;
1668}
1669
1670static void journal_free_journal_head(struct journal_head *jh)
1671{
1672#ifdef CONFIG_JBD_DEBUG
1673 atomic_dec(&nr_journal_heads);
1674 memset(jh, JBD_POISON_FREE, sizeof(*jh));
1675#endif
1676 kmem_cache_free(journal_head_cache, jh);
1677}
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722struct journal_head *journal_add_journal_head(struct buffer_head *bh)
1723{
1724 struct journal_head *jh;
1725 struct journal_head *new_jh = NULL;
1726
1727repeat:
1728 if (!buffer_jbd(bh)) {
1729 new_jh = journal_alloc_journal_head();
1730 memset(new_jh, 0, sizeof(*new_jh));
1731 }
1732
1733 jbd_lock_bh_journal_head(bh);
1734 if (buffer_jbd(bh)) {
1735 jh = bh2jh(bh);
1736 } else {
1737 J_ASSERT_BH(bh,
1738 (atomic_read(&bh->b_count) > 0) ||
1739 (bh->b_page && bh->b_page->mapping));
1740
1741 if (!new_jh) {
1742 jbd_unlock_bh_journal_head(bh);
1743 goto repeat;
1744 }
1745
1746 jh = new_jh;
1747 new_jh = NULL;
1748 set_buffer_jbd(bh);
1749 bh->b_private = jh;
1750 jh->b_bh = bh;
1751 get_bh(bh);
1752 BUFFER_TRACE(bh, "added journal_head");
1753 }
1754 jh->b_jcount++;
1755 jbd_unlock_bh_journal_head(bh);
1756 if (new_jh)
1757 journal_free_journal_head(new_jh);
1758 return bh->b_private;
1759}
1760
1761
1762
1763
1764
1765struct journal_head *journal_grab_journal_head(struct buffer_head *bh)
1766{
1767 struct journal_head *jh = NULL;
1768
1769 jbd_lock_bh_journal_head(bh);
1770 if (buffer_jbd(bh)) {
1771 jh = bh2jh(bh);
1772 jh->b_jcount++;
1773 }
1774 jbd_unlock_bh_journal_head(bh);
1775 return jh;
1776}
1777
1778static void __journal_remove_journal_head(struct buffer_head *bh)
1779{
1780 struct journal_head *jh = bh2jh(bh);
1781
1782 J_ASSERT_JH(jh, jh->b_jcount >= 0);
1783
1784 get_bh(bh);
1785 if (jh->b_jcount == 0) {
1786 if (jh->b_transaction == NULL &&
1787 jh->b_next_transaction == NULL &&
1788 jh->b_cp_transaction == NULL) {
1789 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
1790 J_ASSERT_BH(bh, buffer_jbd(bh));
1791 J_ASSERT_BH(bh, jh2bh(jh) == bh);
1792 BUFFER_TRACE(bh, "remove journal_head");
1793 if (jh->b_frozen_data) {
1794 printk(KERN_WARNING "%s: freeing "
1795 "b_frozen_data\n",
1796 __FUNCTION__);
1797 jbd_free(jh->b_frozen_data, bh->b_size);
1798 }
1799 if (jh->b_committed_data) {
1800 printk(KERN_WARNING "%s: freeing "
1801 "b_committed_data\n",
1802 __FUNCTION__);
1803 jbd_free(jh->b_committed_data, bh->b_size);
1804 }
1805 bh->b_private = NULL;
1806 jh->b_bh = NULL;
1807 clear_buffer_jbd(bh);
1808 __brelse(bh);
1809 journal_free_journal_head(jh);
1810 } else {
1811 BUFFER_TRACE(bh, "journal_head was locked");
1812 }
1813 }
1814}
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829void journal_remove_journal_head(struct buffer_head *bh)
1830{
1831 jbd_lock_bh_journal_head(bh);
1832 __journal_remove_journal_head(bh);
1833 jbd_unlock_bh_journal_head(bh);
1834}
1835
1836
1837
1838
1839
1840void journal_put_journal_head(struct journal_head *jh)
1841{
1842 struct buffer_head *bh = jh2bh(jh);
1843
1844 jbd_lock_bh_journal_head(bh);
1845 J_ASSERT_JH(jh, jh->b_jcount > 0);
1846 --jh->b_jcount;
1847 if (!jh->b_jcount && !jh->b_transaction) {
1848 __journal_remove_journal_head(bh);
1849 __brelse(bh);
1850 }
1851 jbd_unlock_bh_journal_head(bh);
1852}
1853
1854
1855
1856
1857#ifdef CONFIG_JBD_DEBUG
1858
1859u8 journal_enable_debug __read_mostly;
1860EXPORT_SYMBOL(journal_enable_debug);
1861
1862static struct dentry *jbd_debugfs_dir;
1863static struct dentry *jbd_debug;
1864
1865static void __init jbd_create_debugfs_entry(void)
1866{
1867 jbd_debugfs_dir = debugfs_create_dir("jbd", NULL);
1868 if (jbd_debugfs_dir)
1869 jbd_debug = debugfs_create_u8("jbd-debug", S_IRUGO,
1870 jbd_debugfs_dir,
1871 &journal_enable_debug);
1872}
1873
1874static void __exit jbd_remove_debugfs_entry(void)
1875{
1876 debugfs_remove(jbd_debug);
1877 debugfs_remove(jbd_debugfs_dir);
1878}
1879
1880#else
1881
1882static inline void jbd_create_debugfs_entry(void)
1883{
1884}
1885
1886static inline void jbd_remove_debugfs_entry(void)
1887{
1888}
1889
1890#endif
1891
1892struct kmem_cache *jbd_handle_cache;
1893
1894static int __init journal_init_handle_cache(void)
1895{
1896 jbd_handle_cache = kmem_cache_create("journal_handle",
1897 sizeof(handle_t),
1898 0,
1899 SLAB_TEMPORARY,
1900 NULL);
1901 if (jbd_handle_cache == NULL) {
1902 printk(KERN_EMERG "JBD: failed to create handle cache\n");
1903 return -ENOMEM;
1904 }
1905 return 0;
1906}
1907
1908static void journal_destroy_handle_cache(void)
1909{
1910 if (jbd_handle_cache)
1911 kmem_cache_destroy(jbd_handle_cache);
1912}
1913
1914
1915
1916
1917
1918static int __init journal_init_caches(void)
1919{
1920 int ret;
1921
1922 ret = journal_init_revoke_caches();
1923 if (ret == 0)
1924 ret = journal_init_journal_head_cache();
1925 if (ret == 0)
1926 ret = journal_init_handle_cache();
1927 return ret;
1928}
1929
1930static void journal_destroy_caches(void)
1931{
1932 journal_destroy_revoke_caches();
1933 journal_destroy_journal_head_cache();
1934 journal_destroy_handle_cache();
1935}
1936
1937static int __init journal_init(void)
1938{
1939 int ret;
1940
1941 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
1942
1943 ret = journal_init_caches();
1944 if (ret != 0)
1945 journal_destroy_caches();
1946 jbd_create_debugfs_entry();
1947 return ret;
1948}
1949
1950static void __exit journal_exit(void)
1951{
1952#ifdef CONFIG_JBD_DEBUG
1953 int n = atomic_read(&nr_journal_heads);
1954 if (n)
1955 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
1956#endif
1957 jbd_remove_debugfs_entry();
1958 journal_destroy_caches();
1959}
1960
1961MODULE_LICENSE("GPL");
1962module_init(journal_init);
1963module_exit(journal_exit);
1964
1965