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