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_abort);
72EXPORT_SYMBOL(journal_errno);
73EXPORT_SYMBOL(journal_ack_err);
74EXPORT_SYMBOL(journal_clear_err);
75EXPORT_SYMBOL(log_wait_commit);
76EXPORT_SYMBOL(journal_start_commit);
77EXPORT_SYMBOL(journal_force_commit_nested);
78EXPORT_SYMBOL(journal_wipe);
79EXPORT_SYMBOL(journal_blocks_per_page);
80EXPORT_SYMBOL(journal_invalidatepage);
81EXPORT_SYMBOL(journal_try_to_free_buffers);
82EXPORT_SYMBOL(journal_force_commit);
83
84static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
85static void __journal_abort_soft (journal_t *journal, int errno);
86
87
88
89
90
91static void commit_timeout(unsigned long __data)
92{
93 struct task_struct * p = (struct task_struct *) __data;
94
95 wake_up_process(p);
96}
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114static int kjournald(void *arg)
115{
116 journal_t *journal = arg;
117 transaction_t *transaction;
118
119
120
121
122
123 setup_timer(&journal->j_commit_timer, commit_timeout,
124 (unsigned long)current);
125
126
127 journal->j_task = current;
128 wake_up(&journal->j_wait_done_commit);
129
130 printk(KERN_INFO "kjournald starting. Commit interval %ld seconds\n",
131 journal->j_commit_interval / HZ);
132
133
134
135
136 spin_lock(&journal->j_state_lock);
137
138loop:
139 if (journal->j_flags & JFS_UNMOUNT)
140 goto end_loop;
141
142 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
143 journal->j_commit_sequence, journal->j_commit_request);
144
145 if (journal->j_commit_sequence != journal->j_commit_request) {
146 jbd_debug(1, "OK, requests differ\n");
147 spin_unlock(&journal->j_state_lock);
148 del_timer_sync(&journal->j_commit_timer);
149 journal_commit_transaction(journal);
150 spin_lock(&journal->j_state_lock);
151 goto loop;
152 }
153
154 wake_up(&journal->j_wait_done_commit);
155 if (freezing(current)) {
156
157
158
159
160
161 jbd_debug(1, "Now suspending kjournald\n");
162 spin_unlock(&journal->j_state_lock);
163 refrigerator();
164 spin_lock(&journal->j_state_lock);
165 } else {
166
167
168
169
170 DEFINE_WAIT(wait);
171 int should_sleep = 1;
172
173 prepare_to_wait(&journal->j_wait_commit, &wait,
174 TASK_INTERRUPTIBLE);
175 if (journal->j_commit_sequence != journal->j_commit_request)
176 should_sleep = 0;
177 transaction = journal->j_running_transaction;
178 if (transaction && time_after_eq(jiffies,
179 transaction->t_expires))
180 should_sleep = 0;
181 if (journal->j_flags & JFS_UNMOUNT)
182 should_sleep = 0;
183 if (should_sleep) {
184 spin_unlock(&journal->j_state_lock);
185 schedule();
186 spin_lock(&journal->j_state_lock);
187 }
188 finish_wait(&journal->j_wait_commit, &wait);
189 }
190
191 jbd_debug(1, "kjournald wakes\n");
192
193
194
195
196 transaction = journal->j_running_transaction;
197 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
198 journal->j_commit_request = transaction->t_tid;
199 jbd_debug(1, "woke because of timeout\n");
200 }
201 goto loop;
202
203end_loop:
204 spin_unlock(&journal->j_state_lock);
205 del_timer_sync(&journal->j_commit_timer);
206 journal->j_task = NULL;
207 wake_up(&journal->j_wait_done_commit);
208 jbd_debug(1, "Journal thread exiting.\n");
209 return 0;
210}
211
212static int journal_start_thread(journal_t *journal)
213{
214 struct task_struct *t;
215
216 t = kthread_run(kjournald, journal, "kjournald");
217 if (IS_ERR(t))
218 return PTR_ERR(t);
219
220 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
221 return 0;
222}
223
224static void journal_kill_thread(journal_t *journal)
225{
226 spin_lock(&journal->j_state_lock);
227 journal->j_flags |= JFS_UNMOUNT;
228
229 while (journal->j_task) {
230 wake_up(&journal->j_wait_commit);
231 spin_unlock(&journal->j_state_lock);
232 wait_event(journal->j_wait_done_commit,
233 journal->j_task == NULL);
234 spin_lock(&journal->j_state_lock);
235 }
236 spin_unlock(&journal->j_state_lock);
237}
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
276int journal_write_metadata_buffer(transaction_t *transaction,
277 struct journal_head *jh_in,
278 struct journal_head **jh_out,
279 unsigned long blocknr)
280{
281 int need_copy_out = 0;
282 int done_copy_out = 0;
283 int do_escape = 0;
284 char *mapped_data;
285 struct buffer_head *new_bh;
286 struct journal_head *new_jh;
287 struct page *new_page;
288 unsigned int new_offset;
289 struct buffer_head *bh_in = jh2bh(jh_in);
290
291
292
293
294
295
296
297
298
299
300 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
301
302 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
303
304
305
306
307
308 jbd_lock_bh_state(bh_in);
309repeat:
310 if (jh_in->b_frozen_data) {
311 done_copy_out = 1;
312 new_page = virt_to_page(jh_in->b_frozen_data);
313 new_offset = offset_in_page(jh_in->b_frozen_data);
314 } else {
315 new_page = jh2bh(jh_in)->b_page;
316 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
317 }
318
319 mapped_data = kmap_atomic(new_page, KM_USER0);
320
321
322
323 if (*((__be32 *)(mapped_data + new_offset)) ==
324 cpu_to_be32(JFS_MAGIC_NUMBER)) {
325 need_copy_out = 1;
326 do_escape = 1;
327 }
328 kunmap_atomic(mapped_data, KM_USER0);
329
330
331
332
333 if (need_copy_out && !done_copy_out) {
334 char *tmp;
335
336 jbd_unlock_bh_state(bh_in);
337 tmp = jbd_alloc(bh_in->b_size, GFP_NOFS);
338 jbd_lock_bh_state(bh_in);
339 if (jh_in->b_frozen_data) {
340 jbd_free(tmp, bh_in->b_size);
341 goto repeat;
342 }
343
344 jh_in->b_frozen_data = tmp;
345 mapped_data = kmap_atomic(new_page, KM_USER0);
346 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
347 kunmap_atomic(mapped_data, KM_USER0);
348
349 new_page = virt_to_page(tmp);
350 new_offset = offset_in_page(tmp);
351 done_copy_out = 1;
352 }
353
354
355
356
357
358 if (do_escape) {
359 mapped_data = kmap_atomic(new_page, KM_USER0);
360 *((unsigned int *)(mapped_data + new_offset)) = 0;
361 kunmap_atomic(mapped_data, KM_USER0);
362 }
363
364
365 new_bh->b_state = 0;
366 init_buffer(new_bh, NULL, NULL);
367 atomic_set(&new_bh->b_count, 1);
368 jbd_unlock_bh_state(bh_in);
369
370 new_jh = journal_add_journal_head(new_bh);
371
372 set_bh_page(new_bh, new_page, new_offset);
373 new_jh->b_transaction = NULL;
374 new_bh->b_size = jh2bh(jh_in)->b_size;
375 new_bh->b_bdev = transaction->t_journal->j_dev;
376 new_bh->b_blocknr = blocknr;
377 set_buffer_mapped(new_bh);
378 set_buffer_dirty(new_bh);
379
380 *jh_out = new_jh;
381
382
383
384
385
386
387 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
388 journal_file_buffer(jh_in, transaction, BJ_Shadow);
389 JBUFFER_TRACE(new_jh, "file as BJ_IO");
390 journal_file_buffer(new_jh, transaction, BJ_IO);
391
392 return do_escape | (done_copy_out << 1);
393}
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408int __log_space_left(journal_t *journal)
409{
410 int left = journal->j_free;
411
412 assert_spin_locked(&journal->j_state_lock);
413
414
415
416
417
418
419#define MIN_LOG_RESERVED_BLOCKS 32
420
421 left -= MIN_LOG_RESERVED_BLOCKS;
422
423 if (left <= 0)
424 return 0;
425 left -= (left >> 3);
426 return left;
427}
428
429
430
431
432int __log_start_commit(journal_t *journal, tid_t target)
433{
434
435
436
437 if (!tid_geq(journal->j_commit_request, target)) {
438
439
440
441
442
443 journal->j_commit_request = target;
444 jbd_debug(1, "JBD: requesting commit %d/%d\n",
445 journal->j_commit_request,
446 journal->j_commit_sequence);
447 wake_up(&journal->j_wait_commit);
448 return 1;
449 }
450 return 0;
451}
452
453int log_start_commit(journal_t *journal, tid_t tid)
454{
455 int ret;
456
457 spin_lock(&journal->j_state_lock);
458 ret = __log_start_commit(journal, tid);
459 spin_unlock(&journal->j_state_lock);
460 return ret;
461}
462
463
464
465
466
467
468
469
470
471
472
473int journal_force_commit_nested(journal_t *journal)
474{
475 transaction_t *transaction = NULL;
476 tid_t tid;
477
478 spin_lock(&journal->j_state_lock);
479 if (journal->j_running_transaction && !current->journal_info) {
480 transaction = journal->j_running_transaction;
481 __log_start_commit(journal, transaction->t_tid);
482 } else if (journal->j_committing_transaction)
483 transaction = journal->j_committing_transaction;
484
485 if (!transaction) {
486 spin_unlock(&journal->j_state_lock);
487 return 0;
488 }
489
490 tid = transaction->t_tid;
491 spin_unlock(&journal->j_state_lock);
492 log_wait_commit(journal, tid);
493 return 1;
494}
495
496
497
498
499
500int journal_start_commit(journal_t *journal, tid_t *ptid)
501{
502 int ret = 0;
503
504 spin_lock(&journal->j_state_lock);
505 if (journal->j_running_transaction) {
506 tid_t tid = journal->j_running_transaction->t_tid;
507
508 ret = __log_start_commit(journal, tid);
509 if (ret && ptid)
510 *ptid = tid;
511 } else if (journal->j_committing_transaction && ptid) {
512
513
514
515
516 *ptid = journal->j_committing_transaction->t_tid;
517 ret = 1;
518 }
519 spin_unlock(&journal->j_state_lock);
520 return ret;
521}
522
523
524
525
526
527int log_wait_commit(journal_t *journal, tid_t tid)
528{
529 int err = 0;
530
531#ifdef CONFIG_JBD_DEBUG
532 spin_lock(&journal->j_state_lock);
533 if (!tid_geq(journal->j_commit_request, tid)) {
534 printk(KERN_EMERG
535 "%s: error: j_commit_request=%d, tid=%d\n",
536 __func__, journal->j_commit_request, tid);
537 }
538 spin_unlock(&journal->j_state_lock);
539#endif
540 spin_lock(&journal->j_state_lock);
541 while (tid_gt(tid, journal->j_commit_sequence)) {
542 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
543 tid, journal->j_commit_sequence);
544 wake_up(&journal->j_wait_commit);
545 spin_unlock(&journal->j_state_lock);
546 wait_event(journal->j_wait_done_commit,
547 !tid_gt(tid, journal->j_commit_sequence));
548 spin_lock(&journal->j_state_lock);
549 }
550 spin_unlock(&journal->j_state_lock);
551
552 if (unlikely(is_journal_aborted(journal))) {
553 printk(KERN_EMERG "journal commit I/O error\n");
554 err = -EIO;
555 }
556 return err;
557}
558
559
560
561
562
563int journal_next_log_block(journal_t *journal, unsigned long *retp)
564{
565 unsigned long blocknr;
566
567 spin_lock(&journal->j_state_lock);
568 J_ASSERT(journal->j_free > 1);
569
570 blocknr = journal->j_head;
571 journal->j_head++;
572 journal->j_free--;
573 if (journal->j_head == journal->j_last)
574 journal->j_head = journal->j_first;
575 spin_unlock(&journal->j_state_lock);
576 return journal_bmap(journal, blocknr, retp);
577}
578
579
580
581
582
583
584
585
586int journal_bmap(journal_t *journal, unsigned long blocknr,
587 unsigned long *retp)
588{
589 int err = 0;
590 unsigned long ret;
591
592 if (journal->j_inode) {
593 ret = bmap(journal->j_inode, blocknr);
594 if (ret)
595 *retp = ret;
596 else {
597 char b[BDEVNAME_SIZE];
598
599 printk(KERN_ALERT "%s: journal block not found "
600 "at offset %lu on %s\n",
601 __func__,
602 blocknr,
603 bdevname(journal->j_dev, b));
604 err = -EIO;
605 __journal_abort_soft(journal, err);
606 }
607 } else {
608 *retp = blocknr;
609 }
610 return err;
611}
612
613
614
615
616
617
618
619
620
621
622
623struct journal_head *journal_get_descriptor_buffer(journal_t *journal)
624{
625 struct buffer_head *bh;
626 unsigned long blocknr;
627 int err;
628
629 err = journal_next_log_block(journal, &blocknr);
630
631 if (err)
632 return NULL;
633
634 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
635 lock_buffer(bh);
636 memset(bh->b_data, 0, journal->j_blocksize);
637 set_buffer_uptodate(bh);
638 unlock_buffer(bh);
639 BUFFER_TRACE(bh, "return this buffer");
640 return journal_add_journal_head(bh);
641}
642
643
644
645
646
647
648
649
650
651
652static journal_t * journal_init_common (void)
653{
654 journal_t *journal;
655 int err;
656
657 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
658 if (!journal)
659 goto fail;
660
661 init_waitqueue_head(&journal->j_wait_transaction_locked);
662 init_waitqueue_head(&journal->j_wait_logspace);
663 init_waitqueue_head(&journal->j_wait_done_commit);
664 init_waitqueue_head(&journal->j_wait_checkpoint);
665 init_waitqueue_head(&journal->j_wait_commit);
666 init_waitqueue_head(&journal->j_wait_updates);
667 mutex_init(&journal->j_barrier);
668 mutex_init(&journal->j_checkpoint_mutex);
669 spin_lock_init(&journal->j_revoke_lock);
670 spin_lock_init(&journal->j_list_lock);
671 spin_lock_init(&journal->j_state_lock);
672
673 journal->j_commit_interval = (HZ * JBD_DEFAULT_MAX_COMMIT_AGE);
674
675
676 journal->j_flags = JFS_ABORT;
677
678
679 err = journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
680 if (err) {
681 kfree(journal);
682 goto fail;
683 }
684 return journal;
685fail:
686 return NULL;
687}
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 __func__);
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 __func__);
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 __func__);
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 __func__);
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 if (be32_to_cpu(sb->s_first) == 0 ||
1034 be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
1035 printk(KERN_WARNING
1036 "JBD: Invalid start block of journal: %u\n",
1037 be32_to_cpu(sb->s_first));
1038 goto out;
1039 }
1040
1041 return 0;
1042
1043out:
1044 journal_fail_superblock(journal);
1045 return err;
1046}
1047
1048
1049
1050
1051
1052
1053static int load_superblock(journal_t *journal)
1054{
1055 int err;
1056 journal_superblock_t *sb;
1057
1058 err = journal_get_superblock(journal);
1059 if (err)
1060 return err;
1061
1062 sb = journal->j_superblock;
1063
1064 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1065 journal->j_tail = be32_to_cpu(sb->s_start);
1066 journal->j_first = be32_to_cpu(sb->s_first);
1067 journal->j_last = be32_to_cpu(sb->s_maxlen);
1068 journal->j_errno = be32_to_cpu(sb->s_errno);
1069
1070 return 0;
1071}
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082int journal_load(journal_t *journal)
1083{
1084 int err;
1085 journal_superblock_t *sb;
1086
1087 err = load_superblock(journal);
1088 if (err)
1089 return err;
1090
1091 sb = journal->j_superblock;
1092
1093
1094
1095 if (journal->j_format_version >= 2) {
1096 if ((sb->s_feature_ro_compat &
1097 ~cpu_to_be32(JFS_KNOWN_ROCOMPAT_FEATURES)) ||
1098 (sb->s_feature_incompat &
1099 ~cpu_to_be32(JFS_KNOWN_INCOMPAT_FEATURES))) {
1100 printk (KERN_WARNING
1101 "JBD: Unrecognised features on journal\n");
1102 return -EINVAL;
1103 }
1104 }
1105
1106
1107
1108 if (journal_recover(journal))
1109 goto recovery_error;
1110
1111
1112
1113
1114 if (journal_reset(journal))
1115 goto recovery_error;
1116
1117 journal->j_flags &= ~JFS_ABORT;
1118 journal->j_flags |= JFS_LOADED;
1119 return 0;
1120
1121recovery_error:
1122 printk (KERN_WARNING "JBD: recovery failed\n");
1123 return -EIO;
1124}
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134int journal_destroy(journal_t *journal)
1135{
1136 int err = 0;
1137
1138
1139 journal_kill_thread(journal);
1140
1141
1142 if (journal->j_running_transaction)
1143 journal_commit_transaction(journal);
1144
1145
1146
1147
1148 spin_lock(&journal->j_list_lock);
1149 while (journal->j_checkpoint_transactions != NULL) {
1150 spin_unlock(&journal->j_list_lock);
1151 log_do_checkpoint(journal);
1152 spin_lock(&journal->j_list_lock);
1153 }
1154
1155 J_ASSERT(journal->j_running_transaction == NULL);
1156 J_ASSERT(journal->j_committing_transaction == NULL);
1157 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1158 spin_unlock(&journal->j_list_lock);
1159
1160 if (journal->j_sb_buffer) {
1161 if (!is_journal_aborted(journal)) {
1162
1163 journal->j_tail = 0;
1164 journal->j_tail_sequence =
1165 ++journal->j_transaction_sequence;
1166 journal_update_superblock(journal, 1);
1167 } else {
1168 err = -EIO;
1169 }
1170 brelse(journal->j_sb_buffer);
1171 }
1172
1173 if (journal->j_inode)
1174 iput(journal->j_inode);
1175 if (journal->j_revoke)
1176 journal_destroy_revoke(journal);
1177 kfree(journal->j_wbuf);
1178 kfree(journal);
1179
1180 return err;
1181}
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195int journal_check_used_features (journal_t *journal, unsigned long compat,
1196 unsigned long ro, unsigned long incompat)
1197{
1198 journal_superblock_t *sb;
1199
1200 if (!compat && !ro && !incompat)
1201 return 1;
1202 if (journal->j_format_version == 1)
1203 return 0;
1204
1205 sb = journal->j_superblock;
1206
1207 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1208 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1209 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1210 return 1;
1211
1212 return 0;
1213}
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226int journal_check_available_features (journal_t *journal, unsigned long compat,
1227 unsigned long ro, unsigned long incompat)
1228{
1229 journal_superblock_t *sb;
1230
1231 if (!compat && !ro && !incompat)
1232 return 1;
1233
1234 sb = journal->j_superblock;
1235
1236
1237
1238
1239
1240 if (journal->j_format_version != 2)
1241 return 0;
1242
1243 if ((compat & JFS_KNOWN_COMPAT_FEATURES) == compat &&
1244 (ro & JFS_KNOWN_ROCOMPAT_FEATURES) == ro &&
1245 (incompat & JFS_KNOWN_INCOMPAT_FEATURES) == incompat)
1246 return 1;
1247
1248 return 0;
1249}
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263int journal_set_features (journal_t *journal, unsigned long compat,
1264 unsigned long ro, unsigned long incompat)
1265{
1266 journal_superblock_t *sb;
1267
1268 if (journal_check_used_features(journal, compat, ro, incompat))
1269 return 1;
1270
1271 if (!journal_check_available_features(journal, compat, ro, incompat))
1272 return 0;
1273
1274 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1275 compat, ro, incompat);
1276
1277 sb = journal->j_superblock;
1278
1279 sb->s_feature_compat |= cpu_to_be32(compat);
1280 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1281 sb->s_feature_incompat |= cpu_to_be32(incompat);
1282
1283 return 1;
1284}
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294int journal_update_format (journal_t *journal)
1295{
1296 journal_superblock_t *sb;
1297 int err;
1298
1299 err = journal_get_superblock(journal);
1300 if (err)
1301 return err;
1302
1303 sb = journal->j_superblock;
1304
1305 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
1306 case JFS_SUPERBLOCK_V2:
1307 return 0;
1308 case JFS_SUPERBLOCK_V1:
1309 return journal_convert_superblock_v1(journal, sb);
1310 default:
1311 break;
1312 }
1313 return -EINVAL;
1314}
1315
1316static int journal_convert_superblock_v1(journal_t *journal,
1317 journal_superblock_t *sb)
1318{
1319 int offset, blocksize;
1320 struct buffer_head *bh;
1321
1322 printk(KERN_WARNING
1323 "JBD: Converting superblock from version 1 to 2.\n");
1324
1325
1326 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1327 blocksize = be32_to_cpu(sb->s_blocksize);
1328 memset(&sb->s_feature_compat, 0, blocksize-offset);
1329
1330 sb->s_nr_users = cpu_to_be32(1);
1331 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
1332 journal->j_format_version = 2;
1333
1334 bh = journal->j_sb_buffer;
1335 BUFFER_TRACE(bh, "marking dirty");
1336 mark_buffer_dirty(bh);
1337 sync_dirty_buffer(bh);
1338 return 0;
1339}
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351int journal_flush(journal_t *journal)
1352{
1353 int err = 0;
1354 transaction_t *transaction = NULL;
1355 unsigned long old_tail;
1356
1357 spin_lock(&journal->j_state_lock);
1358
1359
1360 if (journal->j_running_transaction) {
1361 transaction = journal->j_running_transaction;
1362 __log_start_commit(journal, transaction->t_tid);
1363 } else if (journal->j_committing_transaction)
1364 transaction = journal->j_committing_transaction;
1365
1366
1367 if (transaction) {
1368 tid_t tid = transaction->t_tid;
1369
1370 spin_unlock(&journal->j_state_lock);
1371 log_wait_commit(journal, tid);
1372 } else {
1373 spin_unlock(&journal->j_state_lock);
1374 }
1375
1376
1377 spin_lock(&journal->j_list_lock);
1378 while (!err && journal->j_checkpoint_transactions != NULL) {
1379 spin_unlock(&journal->j_list_lock);
1380 mutex_lock(&journal->j_checkpoint_mutex);
1381 err = log_do_checkpoint(journal);
1382 mutex_unlock(&journal->j_checkpoint_mutex);
1383 spin_lock(&journal->j_list_lock);
1384 }
1385 spin_unlock(&journal->j_list_lock);
1386
1387 if (is_journal_aborted(journal))
1388 return -EIO;
1389
1390 cleanup_journal_tail(journal);
1391
1392
1393
1394
1395
1396
1397 spin_lock(&journal->j_state_lock);
1398 old_tail = journal->j_tail;
1399 journal->j_tail = 0;
1400 spin_unlock(&journal->j_state_lock);
1401 journal_update_superblock(journal, 1);
1402 spin_lock(&journal->j_state_lock);
1403 journal->j_tail = old_tail;
1404
1405 J_ASSERT(!journal->j_running_transaction);
1406 J_ASSERT(!journal->j_committing_transaction);
1407 J_ASSERT(!journal->j_checkpoint_transactions);
1408 J_ASSERT(journal->j_head == journal->j_tail);
1409 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1410 spin_unlock(&journal->j_state_lock);
1411 return 0;
1412}
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427int journal_wipe(journal_t *journal, int write)
1428{
1429 journal_superblock_t *sb;
1430 int err = 0;
1431
1432 J_ASSERT (!(journal->j_flags & JFS_LOADED));
1433
1434 err = load_superblock(journal);
1435 if (err)
1436 return err;
1437
1438 sb = journal->j_superblock;
1439
1440 if (!journal->j_tail)
1441 goto no_recovery;
1442
1443 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1444 write ? "Clearing" : "Ignoring");
1445
1446 err = journal_skip_recovery(journal);
1447 if (write)
1448 journal_update_superblock(journal, 1);
1449
1450 no_recovery:
1451 return err;
1452}
1453
1454
1455
1456
1457
1458
1459static const char *journal_dev_name(journal_t *journal, char *buffer)
1460{
1461 struct block_device *bdev;
1462
1463 if (journal->j_inode)
1464 bdev = journal->j_inode->i_sb->s_bdev;
1465 else
1466 bdev = journal->j_dev;
1467
1468 return bdevname(bdev, buffer);
1469}
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484static void __journal_abort_hard(journal_t *journal)
1485{
1486 transaction_t *transaction;
1487 char b[BDEVNAME_SIZE];
1488
1489 if (journal->j_flags & JFS_ABORT)
1490 return;
1491
1492 printk(KERN_ERR "Aborting journal on device %s.\n",
1493 journal_dev_name(journal, b));
1494
1495 spin_lock(&journal->j_state_lock);
1496 journal->j_flags |= JFS_ABORT;
1497 transaction = journal->j_running_transaction;
1498 if (transaction)
1499 __log_start_commit(journal, transaction->t_tid);
1500 spin_unlock(&journal->j_state_lock);
1501}
1502
1503
1504
1505static void __journal_abort_soft (journal_t *journal, int errno)
1506{
1507 if (journal->j_flags & JFS_ABORT)
1508 return;
1509
1510 if (!journal->j_errno)
1511 journal->j_errno = errno;
1512
1513 __journal_abort_hard(journal);
1514
1515 if (errno)
1516 journal_update_superblock(journal, 1);
1517}
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565void journal_abort(journal_t *journal, int errno)
1566{
1567 __journal_abort_soft(journal, errno);
1568}
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581int journal_errno(journal_t *journal)
1582{
1583 int err;
1584
1585 spin_lock(&journal->j_state_lock);
1586 if (journal->j_flags & JFS_ABORT)
1587 err = -EROFS;
1588 else
1589 err = journal->j_errno;
1590 spin_unlock(&journal->j_state_lock);
1591 return err;
1592}
1593
1594
1595
1596
1597
1598
1599
1600
1601int journal_clear_err(journal_t *journal)
1602{
1603 int err = 0;
1604
1605 spin_lock(&journal->j_state_lock);
1606 if (journal->j_flags & JFS_ABORT)
1607 err = -EROFS;
1608 else
1609 journal->j_errno = 0;
1610 spin_unlock(&journal->j_state_lock);
1611 return err;
1612}
1613
1614
1615
1616
1617
1618
1619
1620
1621void journal_ack_err(journal_t *journal)
1622{
1623 spin_lock(&journal->j_state_lock);
1624 if (journal->j_errno)
1625 journal->j_flags |= JFS_ACK_ERR;
1626 spin_unlock(&journal->j_state_lock);
1627}
1628
1629int journal_blocks_per_page(struct inode *inode)
1630{
1631 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1632}
1633
1634
1635
1636
1637static struct kmem_cache *journal_head_cache;
1638#ifdef CONFIG_JBD_DEBUG
1639static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1640#endif
1641
1642static int journal_init_journal_head_cache(void)
1643{
1644 int retval;
1645
1646 J_ASSERT(journal_head_cache == NULL);
1647 journal_head_cache = kmem_cache_create("journal_head",
1648 sizeof(struct journal_head),
1649 0,
1650 SLAB_TEMPORARY,
1651 NULL);
1652 retval = 0;
1653 if (!journal_head_cache) {
1654 retval = -ENOMEM;
1655 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1656 }
1657 return retval;
1658}
1659
1660static void journal_destroy_journal_head_cache(void)
1661{
1662 if (journal_head_cache) {
1663 kmem_cache_destroy(journal_head_cache);
1664 journal_head_cache = NULL;
1665 }
1666}
1667
1668
1669
1670
1671static struct journal_head *journal_alloc_journal_head(void)
1672{
1673 struct journal_head *ret;
1674 static unsigned long last_warning;
1675
1676#ifdef CONFIG_JBD_DEBUG
1677 atomic_inc(&nr_journal_heads);
1678#endif
1679 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
1680 if (ret == NULL) {
1681 jbd_debug(1, "out of memory for journal_head\n");
1682 if (time_after(jiffies, last_warning + 5*HZ)) {
1683 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
1684 __func__);
1685 last_warning = jiffies;
1686 }
1687 while (ret == NULL) {
1688 yield();
1689 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
1690 }
1691 }
1692 return ret;
1693}
1694
1695static void journal_free_journal_head(struct journal_head *jh)
1696{
1697#ifdef CONFIG_JBD_DEBUG
1698 atomic_dec(&nr_journal_heads);
1699 memset(jh, JBD_POISON_FREE, sizeof(*jh));
1700#endif
1701 kmem_cache_free(journal_head_cache, jh);
1702}
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747struct journal_head *journal_add_journal_head(struct buffer_head *bh)
1748{
1749 struct journal_head *jh;
1750 struct journal_head *new_jh = NULL;
1751
1752repeat:
1753 if (!buffer_jbd(bh)) {
1754 new_jh = journal_alloc_journal_head();
1755 memset(new_jh, 0, sizeof(*new_jh));
1756 }
1757
1758 jbd_lock_bh_journal_head(bh);
1759 if (buffer_jbd(bh)) {
1760 jh = bh2jh(bh);
1761 } else {
1762 J_ASSERT_BH(bh,
1763 (atomic_read(&bh->b_count) > 0) ||
1764 (bh->b_page && bh->b_page->mapping));
1765
1766 if (!new_jh) {
1767 jbd_unlock_bh_journal_head(bh);
1768 goto repeat;
1769 }
1770
1771 jh = new_jh;
1772 new_jh = NULL;
1773 set_buffer_jbd(bh);
1774 bh->b_private = jh;
1775 jh->b_bh = bh;
1776 get_bh(bh);
1777 BUFFER_TRACE(bh, "added journal_head");
1778 }
1779 jh->b_jcount++;
1780 jbd_unlock_bh_journal_head(bh);
1781 if (new_jh)
1782 journal_free_journal_head(new_jh);
1783 return bh->b_private;
1784}
1785
1786
1787
1788
1789
1790struct journal_head *journal_grab_journal_head(struct buffer_head *bh)
1791{
1792 struct journal_head *jh = NULL;
1793
1794 jbd_lock_bh_journal_head(bh);
1795 if (buffer_jbd(bh)) {
1796 jh = bh2jh(bh);
1797 jh->b_jcount++;
1798 }
1799 jbd_unlock_bh_journal_head(bh);
1800 return jh;
1801}
1802
1803static void __journal_remove_journal_head(struct buffer_head *bh)
1804{
1805 struct journal_head *jh = bh2jh(bh);
1806
1807 J_ASSERT_JH(jh, jh->b_jcount >= 0);
1808
1809 get_bh(bh);
1810 if (jh->b_jcount == 0) {
1811 if (jh->b_transaction == NULL &&
1812 jh->b_next_transaction == NULL &&
1813 jh->b_cp_transaction == NULL) {
1814 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
1815 J_ASSERT_BH(bh, buffer_jbd(bh));
1816 J_ASSERT_BH(bh, jh2bh(jh) == bh);
1817 BUFFER_TRACE(bh, "remove journal_head");
1818 if (jh->b_frozen_data) {
1819 printk(KERN_WARNING "%s: freeing "
1820 "b_frozen_data\n",
1821 __func__);
1822 jbd_free(jh->b_frozen_data, bh->b_size);
1823 }
1824 if (jh->b_committed_data) {
1825 printk(KERN_WARNING "%s: freeing "
1826 "b_committed_data\n",
1827 __func__);
1828 jbd_free(jh->b_committed_data, bh->b_size);
1829 }
1830 bh->b_private = NULL;
1831 jh->b_bh = NULL;
1832 clear_buffer_jbd(bh);
1833 __brelse(bh);
1834 journal_free_journal_head(jh);
1835 } else {
1836 BUFFER_TRACE(bh, "journal_head was locked");
1837 }
1838 }
1839}
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854void journal_remove_journal_head(struct buffer_head *bh)
1855{
1856 jbd_lock_bh_journal_head(bh);
1857 __journal_remove_journal_head(bh);
1858 jbd_unlock_bh_journal_head(bh);
1859}
1860
1861
1862
1863
1864
1865void journal_put_journal_head(struct journal_head *jh)
1866{
1867 struct buffer_head *bh = jh2bh(jh);
1868
1869 jbd_lock_bh_journal_head(bh);
1870 J_ASSERT_JH(jh, jh->b_jcount > 0);
1871 --jh->b_jcount;
1872 if (!jh->b_jcount && !jh->b_transaction) {
1873 __journal_remove_journal_head(bh);
1874 __brelse(bh);
1875 }
1876 jbd_unlock_bh_journal_head(bh);
1877}
1878
1879
1880
1881
1882#ifdef CONFIG_JBD_DEBUG
1883
1884u8 journal_enable_debug __read_mostly;
1885EXPORT_SYMBOL(journal_enable_debug);
1886
1887static struct dentry *jbd_debugfs_dir;
1888static struct dentry *jbd_debug;
1889
1890static void __init jbd_create_debugfs_entry(void)
1891{
1892 jbd_debugfs_dir = debugfs_create_dir("jbd", NULL);
1893 if (jbd_debugfs_dir)
1894 jbd_debug = debugfs_create_u8("jbd-debug", S_IRUGO,
1895 jbd_debugfs_dir,
1896 &journal_enable_debug);
1897}
1898
1899static void __exit jbd_remove_debugfs_entry(void)
1900{
1901 debugfs_remove(jbd_debug);
1902 debugfs_remove(jbd_debugfs_dir);
1903}
1904
1905#else
1906
1907static inline void jbd_create_debugfs_entry(void)
1908{
1909}
1910
1911static inline void jbd_remove_debugfs_entry(void)
1912{
1913}
1914
1915#endif
1916
1917struct kmem_cache *jbd_handle_cache;
1918
1919static int __init journal_init_handle_cache(void)
1920{
1921 jbd_handle_cache = kmem_cache_create("journal_handle",
1922 sizeof(handle_t),
1923 0,
1924 SLAB_TEMPORARY,
1925 NULL);
1926 if (jbd_handle_cache == NULL) {
1927 printk(KERN_EMERG "JBD: failed to create handle cache\n");
1928 return -ENOMEM;
1929 }
1930 return 0;
1931}
1932
1933static void journal_destroy_handle_cache(void)
1934{
1935 if (jbd_handle_cache)
1936 kmem_cache_destroy(jbd_handle_cache);
1937}
1938
1939
1940
1941
1942
1943static int __init journal_init_caches(void)
1944{
1945 int ret;
1946
1947 ret = journal_init_revoke_caches();
1948 if (ret == 0)
1949 ret = journal_init_journal_head_cache();
1950 if (ret == 0)
1951 ret = journal_init_handle_cache();
1952 return ret;
1953}
1954
1955static void journal_destroy_caches(void)
1956{
1957 journal_destroy_revoke_caches();
1958 journal_destroy_journal_head_cache();
1959 journal_destroy_handle_cache();
1960}
1961
1962static int __init journal_init(void)
1963{
1964 int ret;
1965
1966 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
1967
1968 ret = journal_init_caches();
1969 if (ret != 0)
1970 journal_destroy_caches();
1971 jbd_create_debugfs_entry();
1972 return ret;
1973}
1974
1975static void __exit journal_exit(void)
1976{
1977#ifdef CONFIG_JBD_DEBUG
1978 int n = atomic_read(&nr_journal_heads);
1979 if (n)
1980 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
1981#endif
1982 jbd_remove_debugfs_entry();
1983 journal_destroy_caches();
1984}
1985
1986MODULE_LICENSE("GPL");
1987module_init(journal_init);
1988module_exit(journal_exit);
1989
1990