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