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