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/jbd2.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/seq_file.h>
40#include <linux/math64.h>
41#include <linux/hash.h>
42#include <linux/log2.h>
43#include <linux/vmalloc.h>
44#include <linux/backing-dev.h>
45#include <linux/bitops.h>
46#include <linux/ratelimit.h>
47
48#define CREATE_TRACE_POINTS
49#include <trace/events/jbd2.h>
50
51#include <asm/uaccess.h>
52#include <asm/page.h>
53#include <asm/system.h>
54
55EXPORT_SYMBOL(jbd2_journal_extend);
56EXPORT_SYMBOL(jbd2_journal_stop);
57EXPORT_SYMBOL(jbd2_journal_lock_updates);
58EXPORT_SYMBOL(jbd2_journal_unlock_updates);
59EXPORT_SYMBOL(jbd2_journal_get_write_access);
60EXPORT_SYMBOL(jbd2_journal_get_create_access);
61EXPORT_SYMBOL(jbd2_journal_get_undo_access);
62EXPORT_SYMBOL(jbd2_journal_set_triggers);
63EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
64EXPORT_SYMBOL(jbd2_journal_release_buffer);
65EXPORT_SYMBOL(jbd2_journal_forget);
66#if 0
67EXPORT_SYMBOL(journal_sync_buffer);
68#endif
69EXPORT_SYMBOL(jbd2_journal_flush);
70EXPORT_SYMBOL(jbd2_journal_revoke);
71
72EXPORT_SYMBOL(jbd2_journal_init_dev);
73EXPORT_SYMBOL(jbd2_journal_init_inode);
74EXPORT_SYMBOL(jbd2_journal_update_format);
75EXPORT_SYMBOL(jbd2_journal_check_used_features);
76EXPORT_SYMBOL(jbd2_journal_check_available_features);
77EXPORT_SYMBOL(jbd2_journal_set_features);
78EXPORT_SYMBOL(jbd2_journal_load);
79EXPORT_SYMBOL(jbd2_journal_destroy);
80EXPORT_SYMBOL(jbd2_journal_abort);
81EXPORT_SYMBOL(jbd2_journal_errno);
82EXPORT_SYMBOL(jbd2_journal_ack_err);
83EXPORT_SYMBOL(jbd2_journal_clear_err);
84EXPORT_SYMBOL(jbd2_log_wait_commit);
85EXPORT_SYMBOL(jbd2_log_start_commit);
86EXPORT_SYMBOL(jbd2_journal_start_commit);
87EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
88EXPORT_SYMBOL(jbd2_journal_wipe);
89EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
90EXPORT_SYMBOL(jbd2_journal_invalidatepage);
91EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
92EXPORT_SYMBOL(jbd2_journal_force_commit);
93EXPORT_SYMBOL(jbd2_journal_file_inode);
94EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
95EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
96EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
97EXPORT_SYMBOL(jbd2_inode_cache);
98
99static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
100static void __journal_abort_soft (journal_t *journal, int errno);
101static int jbd2_journal_create_slab(size_t slab_size);
102
103
104
105
106
107static void commit_timeout(unsigned long __data)
108{
109 struct task_struct * p = (struct task_struct *) __data;
110
111 wake_up_process(p);
112}
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130static int kjournald2(void *arg)
131{
132 journal_t *journal = arg;
133 transaction_t *transaction;
134
135
136
137
138
139 setup_timer(&journal->j_commit_timer, commit_timeout,
140 (unsigned long)current);
141
142
143 journal->j_task = current;
144 wake_up(&journal->j_wait_done_commit);
145
146
147
148
149 write_lock(&journal->j_state_lock);
150
151loop:
152 if (journal->j_flags & JBD2_UNMOUNT)
153 goto end_loop;
154
155 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
156 journal->j_commit_sequence, journal->j_commit_request);
157
158 if (journal->j_commit_sequence != journal->j_commit_request) {
159 jbd_debug(1, "OK, requests differ\n");
160 write_unlock(&journal->j_state_lock);
161 del_timer_sync(&journal->j_commit_timer);
162 jbd2_journal_commit_transaction(journal);
163 write_lock(&journal->j_state_lock);
164 goto loop;
165 }
166
167 wake_up(&journal->j_wait_done_commit);
168 if (freezing(current)) {
169
170
171
172
173
174 jbd_debug(1, "Now suspending kjournald2\n");
175 write_unlock(&journal->j_state_lock);
176 try_to_freeze();
177 write_lock(&journal->j_state_lock);
178 } else {
179
180
181
182
183 DEFINE_WAIT(wait);
184 int should_sleep = 1;
185
186 prepare_to_wait(&journal->j_wait_commit, &wait,
187 TASK_INTERRUPTIBLE);
188 if (journal->j_commit_sequence != journal->j_commit_request)
189 should_sleep = 0;
190 transaction = journal->j_running_transaction;
191 if (transaction && time_after_eq(jiffies,
192 transaction->t_expires))
193 should_sleep = 0;
194 if (journal->j_flags & JBD2_UNMOUNT)
195 should_sleep = 0;
196 if (should_sleep) {
197 write_unlock(&journal->j_state_lock);
198 schedule();
199 write_lock(&journal->j_state_lock);
200 }
201 finish_wait(&journal->j_wait_commit, &wait);
202 }
203
204 jbd_debug(1, "kjournald2 wakes\n");
205
206
207
208
209 transaction = journal->j_running_transaction;
210 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
211 journal->j_commit_request = transaction->t_tid;
212 jbd_debug(1, "woke because of timeout\n");
213 }
214 goto loop;
215
216end_loop:
217 write_unlock(&journal->j_state_lock);
218 del_timer_sync(&journal->j_commit_timer);
219 journal->j_task = NULL;
220 wake_up(&journal->j_wait_done_commit);
221 jbd_debug(1, "Journal thread exiting.\n");
222 return 0;
223}
224
225static int jbd2_journal_start_thread(journal_t *journal)
226{
227 struct task_struct *t;
228
229 t = kthread_run(kjournald2, journal, "jbd2/%s",
230 journal->j_devname);
231 if (IS_ERR(t))
232 return PTR_ERR(t);
233
234 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
235 return 0;
236}
237
238static void journal_kill_thread(journal_t *journal)
239{
240 write_lock(&journal->j_state_lock);
241 journal->j_flags |= JBD2_UNMOUNT;
242
243 while (journal->j_task) {
244 wake_up(&journal->j_wait_commit);
245 write_unlock(&journal->j_state_lock);
246 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
247 write_lock(&journal->j_state_lock);
248 }
249 write_unlock(&journal->j_state_lock);
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
279
280
281
282
283
284
285
286
287
288
289int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
290 struct journal_head *jh_in,
291 struct journal_head **jh_out,
292 unsigned long long blocknr)
293{
294 int need_copy_out = 0;
295 int done_copy_out = 0;
296 int do_escape = 0;
297 char *mapped_data;
298 struct buffer_head *new_bh;
299 struct journal_head *new_jh;
300 struct page *new_page;
301 unsigned int new_offset;
302 struct buffer_head *bh_in = jh2bh(jh_in);
303 journal_t *journal = transaction->t_journal;
304
305
306
307
308
309
310
311
312
313
314 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
315
316retry_alloc:
317 new_bh = alloc_buffer_head(GFP_NOFS);
318 if (!new_bh) {
319
320
321
322
323 congestion_wait(BLK_RW_ASYNC, HZ/50);
324 goto retry_alloc;
325 }
326
327
328 new_bh->b_state = 0;
329 init_buffer(new_bh, NULL, NULL);
330 atomic_set(&new_bh->b_count, 1);
331 new_jh = jbd2_journal_add_journal_head(new_bh);
332
333
334
335
336
337 jbd_lock_bh_state(bh_in);
338repeat:
339 if (jh_in->b_frozen_data) {
340 done_copy_out = 1;
341 new_page = virt_to_page(jh_in->b_frozen_data);
342 new_offset = offset_in_page(jh_in->b_frozen_data);
343 } else {
344 new_page = jh2bh(jh_in)->b_page;
345 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
346 }
347
348 mapped_data = kmap_atomic(new_page, KM_USER0);
349
350
351
352
353
354
355 if (!done_copy_out)
356 jbd2_buffer_frozen_trigger(jh_in, mapped_data + new_offset,
357 jh_in->b_triggers);
358
359
360
361
362 if (*((__be32 *)(mapped_data + new_offset)) ==
363 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
364 need_copy_out = 1;
365 do_escape = 1;
366 }
367 kunmap_atomic(mapped_data, KM_USER0);
368
369
370
371
372 if (need_copy_out && !done_copy_out) {
373 char *tmp;
374
375 jbd_unlock_bh_state(bh_in);
376 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
377 if (!tmp) {
378 jbd2_journal_put_journal_head(new_jh);
379 return -ENOMEM;
380 }
381 jbd_lock_bh_state(bh_in);
382 if (jh_in->b_frozen_data) {
383 jbd2_free(tmp, bh_in->b_size);
384 goto repeat;
385 }
386
387 jh_in->b_frozen_data = tmp;
388 mapped_data = kmap_atomic(new_page, KM_USER0);
389 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
390 kunmap_atomic(mapped_data, KM_USER0);
391
392 new_page = virt_to_page(tmp);
393 new_offset = offset_in_page(tmp);
394 done_copy_out = 1;
395
396
397
398
399
400
401 jh_in->b_frozen_triggers = jh_in->b_triggers;
402 }
403
404
405
406
407
408 if (do_escape) {
409 mapped_data = kmap_atomic(new_page, KM_USER0);
410 *((unsigned int *)(mapped_data + new_offset)) = 0;
411 kunmap_atomic(mapped_data, KM_USER0);
412 }
413
414 set_bh_page(new_bh, new_page, new_offset);
415 new_jh->b_transaction = NULL;
416 new_bh->b_size = jh2bh(jh_in)->b_size;
417 new_bh->b_bdev = transaction->t_journal->j_dev;
418 new_bh->b_blocknr = blocknr;
419 set_buffer_mapped(new_bh);
420 set_buffer_dirty(new_bh);
421
422 *jh_out = new_jh;
423
424
425
426
427
428
429 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
430 spin_lock(&journal->j_list_lock);
431 __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
432 spin_unlock(&journal->j_list_lock);
433 jbd_unlock_bh_state(bh_in);
434
435 JBUFFER_TRACE(new_jh, "file as BJ_IO");
436 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
437
438 return do_escape | (done_copy_out << 1);
439}
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454int __jbd2_log_space_left(journal_t *journal)
455{
456 int left = journal->j_free;
457
458
459
460
461
462
463
464
465#define MIN_LOG_RESERVED_BLOCKS 32
466
467 left -= MIN_LOG_RESERVED_BLOCKS;
468
469 if (left <= 0)
470 return 0;
471 left -= (left >> 3);
472 return left;
473}
474
475
476
477
478
479int __jbd2_log_start_commit(journal_t *journal, tid_t target)
480{
481
482
483
484
485
486 if (journal->j_running_transaction &&
487 journal->j_running_transaction->t_tid == target) {
488
489
490
491
492
493 journal->j_commit_request = target;
494 jbd_debug(1, "JBD2: requesting commit %d/%d\n",
495 journal->j_commit_request,
496 journal->j_commit_sequence);
497 wake_up(&journal->j_wait_commit);
498 return 1;
499 } else if (!tid_geq(journal->j_commit_request, target))
500
501
502
503 WARN_ONCE(1, "JBD2: bad log_start_commit: %u %u %u %u\n",
504 journal->j_commit_request,
505 journal->j_commit_sequence,
506 target, journal->j_running_transaction ?
507 journal->j_running_transaction->t_tid : 0);
508 return 0;
509}
510
511int jbd2_log_start_commit(journal_t *journal, tid_t tid)
512{
513 int ret;
514
515 write_lock(&journal->j_state_lock);
516 ret = __jbd2_log_start_commit(journal, tid);
517 write_unlock(&journal->j_state_lock);
518 return ret;
519}
520
521
522
523
524
525
526
527
528
529
530
531int jbd2_journal_force_commit_nested(journal_t *journal)
532{
533 transaction_t *transaction = NULL;
534 tid_t tid;
535 int need_to_start = 0;
536
537 read_lock(&journal->j_state_lock);
538 if (journal->j_running_transaction && !current->journal_info) {
539 transaction = journal->j_running_transaction;
540 if (!tid_geq(journal->j_commit_request, transaction->t_tid))
541 need_to_start = 1;
542 } else if (journal->j_committing_transaction)
543 transaction = journal->j_committing_transaction;
544
545 if (!transaction) {
546 read_unlock(&journal->j_state_lock);
547 return 0;
548 }
549
550 tid = transaction->t_tid;
551 read_unlock(&journal->j_state_lock);
552 if (need_to_start)
553 jbd2_log_start_commit(journal, tid);
554 jbd2_log_wait_commit(journal, tid);
555 return 1;
556}
557
558
559
560
561
562
563int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
564{
565 int ret = 0;
566
567 write_lock(&journal->j_state_lock);
568 if (journal->j_running_transaction) {
569 tid_t tid = journal->j_running_transaction->t_tid;
570
571 __jbd2_log_start_commit(journal, tid);
572
573
574 if (ptid)
575 *ptid = tid;
576 ret = 1;
577 } else if (journal->j_committing_transaction) {
578
579
580
581
582 if (ptid)
583 *ptid = journal->j_committing_transaction->t_tid;
584 ret = 1;
585 }
586 write_unlock(&journal->j_state_lock);
587 return ret;
588}
589
590
591
592
593
594
595
596int jbd2_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
597{
598 int ret = 0;
599 transaction_t *commit_trans;
600
601 if (!(journal->j_flags & JBD2_BARRIER))
602 return 0;
603 read_lock(&journal->j_state_lock);
604
605 if (tid_geq(journal->j_commit_sequence, tid))
606 goto out;
607 commit_trans = journal->j_committing_transaction;
608 if (!commit_trans || commit_trans->t_tid != tid) {
609 ret = 1;
610 goto out;
611 }
612
613
614
615
616 if (journal->j_fs_dev != journal->j_dev) {
617 if (!commit_trans->t_need_data_flush ||
618 commit_trans->t_state >= T_COMMIT_DFLUSH)
619 goto out;
620 } else {
621 if (commit_trans->t_state >= T_COMMIT_JFLUSH)
622 goto out;
623 }
624 ret = 1;
625out:
626 read_unlock(&journal->j_state_lock);
627 return ret;
628}
629EXPORT_SYMBOL(jbd2_trans_will_send_data_barrier);
630
631
632
633
634
635int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
636{
637 int err = 0;
638
639 read_lock(&journal->j_state_lock);
640#ifdef CONFIG_JBD2_DEBUG
641 if (!tid_geq(journal->j_commit_request, tid)) {
642 printk(KERN_EMERG
643 "%s: error: j_commit_request=%d, tid=%d\n",
644 __func__, journal->j_commit_request, tid);
645 }
646#endif
647 while (tid_gt(tid, journal->j_commit_sequence)) {
648 jbd_debug(1, "JBD2: want %d, j_commit_sequence=%d\n",
649 tid, journal->j_commit_sequence);
650 wake_up(&journal->j_wait_commit);
651 read_unlock(&journal->j_state_lock);
652 wait_event(journal->j_wait_done_commit,
653 !tid_gt(tid, journal->j_commit_sequence));
654 read_lock(&journal->j_state_lock);
655 }
656 read_unlock(&journal->j_state_lock);
657
658 if (unlikely(is_journal_aborted(journal))) {
659 printk(KERN_EMERG "journal commit I/O error\n");
660 err = -EIO;
661 }
662 return err;
663}
664
665
666
667
668
669int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
670{
671 unsigned long blocknr;
672
673 write_lock(&journal->j_state_lock);
674 J_ASSERT(journal->j_free > 1);
675
676 blocknr = journal->j_head;
677 journal->j_head++;
678 journal->j_free--;
679 if (journal->j_head == journal->j_last)
680 journal->j_head = journal->j_first;
681 write_unlock(&journal->j_state_lock);
682 return jbd2_journal_bmap(journal, blocknr, retp);
683}
684
685
686
687
688
689
690
691
692int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
693 unsigned long long *retp)
694{
695 int err = 0;
696 unsigned long long ret;
697
698 if (journal->j_inode) {
699 ret = bmap(journal->j_inode, blocknr);
700 if (ret)
701 *retp = ret;
702 else {
703 printk(KERN_ALERT "%s: journal block not found "
704 "at offset %lu on %s\n",
705 __func__, blocknr, journal->j_devname);
706 err = -EIO;
707 __journal_abort_soft(journal, err);
708 }
709 } else {
710 *retp = blocknr;
711 }
712 return err;
713}
714
715
716
717
718
719
720
721
722
723
724
725struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
726{
727 struct buffer_head *bh;
728 unsigned long long blocknr;
729 int err;
730
731 err = jbd2_journal_next_log_block(journal, &blocknr);
732
733 if (err)
734 return NULL;
735
736 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
737 if (!bh)
738 return NULL;
739 lock_buffer(bh);
740 memset(bh->b_data, 0, journal->j_blocksize);
741 set_buffer_uptodate(bh);
742 unlock_buffer(bh);
743 BUFFER_TRACE(bh, "return this buffer");
744 return jbd2_journal_add_journal_head(bh);
745}
746
747struct jbd2_stats_proc_session {
748 journal_t *journal;
749 struct transaction_stats_s *stats;
750 int start;
751 int max;
752};
753
754static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
755{
756 return *pos ? NULL : SEQ_START_TOKEN;
757}
758
759static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
760{
761 return NULL;
762}
763
764static int jbd2_seq_info_show(struct seq_file *seq, void *v)
765{
766 struct jbd2_stats_proc_session *s = seq->private;
767
768 if (v != SEQ_START_TOKEN)
769 return 0;
770 seq_printf(seq, "%lu transaction, each up to %u blocks\n",
771 s->stats->ts_tid,
772 s->journal->j_max_transaction_buffers);
773 if (s->stats->ts_tid == 0)
774 return 0;
775 seq_printf(seq, "average: \n %ums waiting for transaction\n",
776 jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid));
777 seq_printf(seq, " %ums running transaction\n",
778 jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid));
779 seq_printf(seq, " %ums transaction was being locked\n",
780 jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid));
781 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
782 jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid));
783 seq_printf(seq, " %ums logging transaction\n",
784 jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid));
785 seq_printf(seq, " %lluus average transaction commit time\n",
786 div_u64(s->journal->j_average_commit_time, 1000));
787 seq_printf(seq, " %lu handles per transaction\n",
788 s->stats->run.rs_handle_count / s->stats->ts_tid);
789 seq_printf(seq, " %lu blocks per transaction\n",
790 s->stats->run.rs_blocks / s->stats->ts_tid);
791 seq_printf(seq, " %lu logged blocks per transaction\n",
792 s->stats->run.rs_blocks_logged / s->stats->ts_tid);
793 return 0;
794}
795
796static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
797{
798}
799
800static const struct seq_operations jbd2_seq_info_ops = {
801 .start = jbd2_seq_info_start,
802 .next = jbd2_seq_info_next,
803 .stop = jbd2_seq_info_stop,
804 .show = jbd2_seq_info_show,
805};
806
807static int jbd2_seq_info_open(struct inode *inode, struct file *file)
808{
809 journal_t *journal = PDE(inode)->data;
810 struct jbd2_stats_proc_session *s;
811 int rc, size;
812
813 s = kmalloc(sizeof(*s), GFP_KERNEL);
814 if (s == NULL)
815 return -ENOMEM;
816 size = sizeof(struct transaction_stats_s);
817 s->stats = kmalloc(size, GFP_KERNEL);
818 if (s->stats == NULL) {
819 kfree(s);
820 return -ENOMEM;
821 }
822 spin_lock(&journal->j_history_lock);
823 memcpy(s->stats, &journal->j_stats, size);
824 s->journal = journal;
825 spin_unlock(&journal->j_history_lock);
826
827 rc = seq_open(file, &jbd2_seq_info_ops);
828 if (rc == 0) {
829 struct seq_file *m = file->private_data;
830 m->private = s;
831 } else {
832 kfree(s->stats);
833 kfree(s);
834 }
835 return rc;
836
837}
838
839static int jbd2_seq_info_release(struct inode *inode, struct file *file)
840{
841 struct seq_file *seq = file->private_data;
842 struct jbd2_stats_proc_session *s = seq->private;
843 kfree(s->stats);
844 kfree(s);
845 return seq_release(inode, file);
846}
847
848static const struct file_operations jbd2_seq_info_fops = {
849 .owner = THIS_MODULE,
850 .open = jbd2_seq_info_open,
851 .read = seq_read,
852 .llseek = seq_lseek,
853 .release = jbd2_seq_info_release,
854};
855
856static struct proc_dir_entry *proc_jbd2_stats;
857
858static void jbd2_stats_proc_init(journal_t *journal)
859{
860 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
861 if (journal->j_proc_entry) {
862 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
863 &jbd2_seq_info_fops, journal);
864 }
865}
866
867static void jbd2_stats_proc_exit(journal_t *journal)
868{
869 remove_proc_entry("info", journal->j_proc_entry);
870 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
871}
872
873
874
875
876
877
878
879
880
881
882static journal_t * journal_init_common (void)
883{
884 journal_t *journal;
885 int err;
886
887 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
888 if (!journal)
889 return NULL;
890
891 init_waitqueue_head(&journal->j_wait_transaction_locked);
892 init_waitqueue_head(&journal->j_wait_logspace);
893 init_waitqueue_head(&journal->j_wait_done_commit);
894 init_waitqueue_head(&journal->j_wait_checkpoint);
895 init_waitqueue_head(&journal->j_wait_commit);
896 init_waitqueue_head(&journal->j_wait_updates);
897 mutex_init(&journal->j_barrier);
898 mutex_init(&journal->j_checkpoint_mutex);
899 spin_lock_init(&journal->j_revoke_lock);
900 spin_lock_init(&journal->j_list_lock);
901 rwlock_init(&journal->j_state_lock);
902
903 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
904 journal->j_min_batch_time = 0;
905 journal->j_max_batch_time = 15000;
906
907
908 journal->j_flags = JBD2_ABORT;
909
910
911 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
912 if (err) {
913 kfree(journal);
914 return NULL;
915 }
916
917 spin_lock_init(&journal->j_history_lock);
918
919 return journal;
920}
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945journal_t * jbd2_journal_init_dev(struct block_device *bdev,
946 struct block_device *fs_dev,
947 unsigned long long start, int len, int blocksize)
948{
949 journal_t *journal = journal_init_common();
950 struct buffer_head *bh;
951 char *p;
952 int n;
953
954 if (!journal)
955 return NULL;
956
957
958 journal->j_blocksize = blocksize;
959 journal->j_dev = bdev;
960 journal->j_fs_dev = fs_dev;
961 journal->j_blk_offset = start;
962 journal->j_maxlen = len;
963 bdevname(journal->j_dev, journal->j_devname);
964 p = journal->j_devname;
965 while ((p = strchr(p, '/')))
966 *p = '!';
967 jbd2_stats_proc_init(journal);
968 n = journal->j_blocksize / sizeof(journal_block_tag_t);
969 journal->j_wbufsize = n;
970 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
971 if (!journal->j_wbuf) {
972 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
973 __func__);
974 goto out_err;
975 }
976
977 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
978 if (!bh) {
979 printk(KERN_ERR
980 "%s: Cannot get buffer for journal superblock\n",
981 __func__);
982 goto out_err;
983 }
984 journal->j_sb_buffer = bh;
985 journal->j_superblock = (journal_superblock_t *)bh->b_data;
986
987 return journal;
988out_err:
989 kfree(journal->j_wbuf);
990 jbd2_stats_proc_exit(journal);
991 kfree(journal);
992 return NULL;
993}
994
995
996
997
998
999
1000
1001
1002
1003journal_t * jbd2_journal_init_inode (struct inode *inode)
1004{
1005 struct buffer_head *bh;
1006 journal_t *journal = journal_init_common();
1007 char *p;
1008 int err;
1009 int n;
1010 unsigned long long blocknr;
1011
1012 if (!journal)
1013 return NULL;
1014
1015 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1016 journal->j_inode = inode;
1017 bdevname(journal->j_dev, journal->j_devname);
1018 p = journal->j_devname;
1019 while ((p = strchr(p, '/')))
1020 *p = '!';
1021 p = journal->j_devname + strlen(journal->j_devname);
1022 sprintf(p, "-%lu", journal->j_inode->i_ino);
1023 jbd_debug(1,
1024 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1025 journal, inode->i_sb->s_id, inode->i_ino,
1026 (long long) inode->i_size,
1027 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1028
1029 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1030 journal->j_blocksize = inode->i_sb->s_blocksize;
1031 jbd2_stats_proc_init(journal);
1032
1033
1034 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1035 journal->j_wbufsize = n;
1036 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1037 if (!journal->j_wbuf) {
1038 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
1039 __func__);
1040 goto out_err;
1041 }
1042
1043 err = jbd2_journal_bmap(journal, 0, &blocknr);
1044
1045 if (err) {
1046 printk(KERN_ERR "%s: Cannot locate journal superblock\n",
1047 __func__);
1048 goto out_err;
1049 }
1050
1051 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
1052 if (!bh) {
1053 printk(KERN_ERR
1054 "%s: Cannot get buffer for journal superblock\n",
1055 __func__);
1056 goto out_err;
1057 }
1058 journal->j_sb_buffer = bh;
1059 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1060
1061 return journal;
1062out_err:
1063 kfree(journal->j_wbuf);
1064 jbd2_stats_proc_exit(journal);
1065 kfree(journal);
1066 return NULL;
1067}
1068
1069
1070
1071
1072
1073
1074static void journal_fail_superblock (journal_t *journal)
1075{
1076 struct buffer_head *bh = journal->j_sb_buffer;
1077 brelse(bh);
1078 journal->j_sb_buffer = NULL;
1079}
1080
1081
1082
1083
1084
1085
1086
1087
1088static int journal_reset(journal_t *journal)
1089{
1090 journal_superblock_t *sb = journal->j_superblock;
1091 unsigned long long first, last;
1092
1093 first = be32_to_cpu(sb->s_first);
1094 last = be32_to_cpu(sb->s_maxlen);
1095 if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
1096 printk(KERN_ERR "JBD2: Journal too short (blocks %llu-%llu).\n",
1097 first, last);
1098 journal_fail_superblock(journal);
1099 return -EINVAL;
1100 }
1101
1102 journal->j_first = first;
1103 journal->j_last = last;
1104
1105 journal->j_head = first;
1106 journal->j_tail = first;
1107 journal->j_free = last - first;
1108
1109 journal->j_tail_sequence = journal->j_transaction_sequence;
1110 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1111 journal->j_commit_request = journal->j_commit_sequence;
1112
1113 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1114
1115
1116 jbd2_journal_update_superblock(journal, 1);
1117 return jbd2_journal_start_thread(journal);
1118}
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128void jbd2_journal_update_superblock(journal_t *journal, int wait)
1129{
1130 journal_superblock_t *sb = journal->j_superblock;
1131 struct buffer_head *bh = journal->j_sb_buffer;
1132
1133
1134
1135
1136
1137
1138
1139
1140 if (sb->s_start == 0 && journal->j_tail_sequence ==
1141 journal->j_transaction_sequence) {
1142 jbd_debug(1, "JBD2: Skipping superblock update on recovered sb "
1143 "(start %ld, seq %d, errno %d)\n",
1144 journal->j_tail, journal->j_tail_sequence,
1145 journal->j_errno);
1146 goto out;
1147 }
1148
1149 if (buffer_write_io_error(bh)) {
1150
1151
1152
1153
1154
1155
1156
1157
1158 printk(KERN_ERR "JBD2: previous I/O error detected "
1159 "for journal superblock update for %s.\n",
1160 journal->j_devname);
1161 clear_buffer_write_io_error(bh);
1162 set_buffer_uptodate(bh);
1163 }
1164
1165 read_lock(&journal->j_state_lock);
1166 jbd_debug(1, "JBD2: updating superblock (start %ld, seq %d, errno %d)\n",
1167 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1168
1169 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1170 sb->s_start = cpu_to_be32(journal->j_tail);
1171 sb->s_errno = cpu_to_be32(journal->j_errno);
1172 read_unlock(&journal->j_state_lock);
1173
1174 BUFFER_TRACE(bh, "marking dirty");
1175 mark_buffer_dirty(bh);
1176 if (wait) {
1177 sync_dirty_buffer(bh);
1178 if (buffer_write_io_error(bh)) {
1179 printk(KERN_ERR "JBD2: I/O error detected "
1180 "when updating journal superblock for %s.\n",
1181 journal->j_devname);
1182 clear_buffer_write_io_error(bh);
1183 set_buffer_uptodate(bh);
1184 }
1185 } else
1186 write_dirty_buffer(bh, WRITE);
1187
1188out:
1189
1190
1191
1192
1193 write_lock(&journal->j_state_lock);
1194 if (sb->s_start)
1195 journal->j_flags &= ~JBD2_FLUSHED;
1196 else
1197 journal->j_flags |= JBD2_FLUSHED;
1198 write_unlock(&journal->j_state_lock);
1199}
1200
1201
1202
1203
1204
1205
1206static int journal_get_superblock(journal_t *journal)
1207{
1208 struct buffer_head *bh;
1209 journal_superblock_t *sb;
1210 int err = -EIO;
1211
1212 bh = journal->j_sb_buffer;
1213
1214 J_ASSERT(bh != NULL);
1215 if (!buffer_uptodate(bh)) {
1216 ll_rw_block(READ, 1, &bh);
1217 wait_on_buffer(bh);
1218 if (!buffer_uptodate(bh)) {
1219 printk(KERN_ERR
1220 "JBD2: IO error reading journal superblock\n");
1221 goto out;
1222 }
1223 }
1224
1225 sb = journal->j_superblock;
1226
1227 err = -EINVAL;
1228
1229 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
1230 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1231 printk(KERN_WARNING "JBD2: no valid journal superblock found\n");
1232 goto out;
1233 }
1234
1235 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1236 case JBD2_SUPERBLOCK_V1:
1237 journal->j_format_version = 1;
1238 break;
1239 case JBD2_SUPERBLOCK_V2:
1240 journal->j_format_version = 2;
1241 break;
1242 default:
1243 printk(KERN_WARNING "JBD2: unrecognised superblock format ID\n");
1244 goto out;
1245 }
1246
1247 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1248 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1249 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1250 printk(KERN_WARNING "JBD2: journal file too short\n");
1251 goto out;
1252 }
1253
1254 if (be32_to_cpu(sb->s_first) == 0 ||
1255 be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
1256 printk(KERN_WARNING
1257 "JBD2: Invalid start block of journal: %u\n",
1258 be32_to_cpu(sb->s_first));
1259 goto out;
1260 }
1261
1262 return 0;
1263
1264out:
1265 journal_fail_superblock(journal);
1266 return err;
1267}
1268
1269
1270
1271
1272
1273
1274static int load_superblock(journal_t *journal)
1275{
1276 int err;
1277 journal_superblock_t *sb;
1278
1279 err = journal_get_superblock(journal);
1280 if (err)
1281 return err;
1282
1283 sb = journal->j_superblock;
1284
1285 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1286 journal->j_tail = be32_to_cpu(sb->s_start);
1287 journal->j_first = be32_to_cpu(sb->s_first);
1288 journal->j_last = be32_to_cpu(sb->s_maxlen);
1289 journal->j_errno = be32_to_cpu(sb->s_errno);
1290
1291 return 0;
1292}
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303int jbd2_journal_load(journal_t *journal)
1304{
1305 int err;
1306 journal_superblock_t *sb;
1307
1308 err = load_superblock(journal);
1309 if (err)
1310 return err;
1311
1312 sb = journal->j_superblock;
1313
1314
1315
1316 if (journal->j_format_version >= 2) {
1317 if ((sb->s_feature_ro_compat &
1318 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
1319 (sb->s_feature_incompat &
1320 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
1321 printk(KERN_WARNING
1322 "JBD2: Unrecognised features on journal\n");
1323 return -EINVAL;
1324 }
1325 }
1326
1327
1328
1329
1330 err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize));
1331 if (err)
1332 return err;
1333
1334
1335
1336 if (jbd2_journal_recover(journal))
1337 goto recovery_error;
1338
1339 if (journal->j_failed_commit) {
1340 printk(KERN_ERR "JBD2: journal transaction %u on %s "
1341 "is corrupt.\n", journal->j_failed_commit,
1342 journal->j_devname);
1343 return -EIO;
1344 }
1345
1346
1347
1348
1349 if (journal_reset(journal))
1350 goto recovery_error;
1351
1352 journal->j_flags &= ~JBD2_ABORT;
1353 journal->j_flags |= JBD2_LOADED;
1354 return 0;
1355
1356recovery_error:
1357 printk(KERN_WARNING "JBD2: recovery failed\n");
1358 return -EIO;
1359}
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369int jbd2_journal_destroy(journal_t *journal)
1370{
1371 int err = 0;
1372
1373
1374 journal_kill_thread(journal);
1375
1376
1377 if (journal->j_running_transaction)
1378 jbd2_journal_commit_transaction(journal);
1379
1380
1381
1382
1383 spin_lock(&journal->j_list_lock);
1384 while (journal->j_checkpoint_transactions != NULL) {
1385 spin_unlock(&journal->j_list_lock);
1386 mutex_lock(&journal->j_checkpoint_mutex);
1387 jbd2_log_do_checkpoint(journal);
1388 mutex_unlock(&journal->j_checkpoint_mutex);
1389 spin_lock(&journal->j_list_lock);
1390 }
1391
1392 J_ASSERT(journal->j_running_transaction == NULL);
1393 J_ASSERT(journal->j_committing_transaction == NULL);
1394 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1395 spin_unlock(&journal->j_list_lock);
1396
1397 if (journal->j_sb_buffer) {
1398 if (!is_journal_aborted(journal)) {
1399
1400 journal->j_tail = 0;
1401 journal->j_tail_sequence =
1402 ++journal->j_transaction_sequence;
1403 jbd2_journal_update_superblock(journal, 1);
1404 } else {
1405 err = -EIO;
1406 }
1407 brelse(journal->j_sb_buffer);
1408 }
1409
1410 if (journal->j_proc_entry)
1411 jbd2_stats_proc_exit(journal);
1412 if (journal->j_inode)
1413 iput(journal->j_inode);
1414 if (journal->j_revoke)
1415 jbd2_journal_destroy_revoke(journal);
1416 kfree(journal->j_wbuf);
1417 kfree(journal);
1418
1419 return err;
1420}
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
1435 unsigned long ro, unsigned long incompat)
1436{
1437 journal_superblock_t *sb;
1438
1439 if (!compat && !ro && !incompat)
1440 return 1;
1441
1442 if (journal->j_format_version == 0 &&
1443 journal_get_superblock(journal) != 0)
1444 return 0;
1445 if (journal->j_format_version == 1)
1446 return 0;
1447
1448 sb = journal->j_superblock;
1449
1450 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1451 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1452 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1453 return 1;
1454
1455 return 0;
1456}
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
1470 unsigned long ro, unsigned long incompat)
1471{
1472 if (!compat && !ro && !incompat)
1473 return 1;
1474
1475
1476
1477
1478
1479 if (journal->j_format_version != 2)
1480 return 0;
1481
1482 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1483 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1484 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
1485 return 1;
1486
1487 return 0;
1488}
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
1503 unsigned long ro, unsigned long incompat)
1504{
1505 journal_superblock_t *sb;
1506
1507 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
1508 return 1;
1509
1510 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
1511 return 0;
1512
1513 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1514 compat, ro, incompat);
1515
1516 sb = journal->j_superblock;
1517
1518 sb->s_feature_compat |= cpu_to_be32(compat);
1519 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1520 sb->s_feature_incompat |= cpu_to_be32(incompat);
1521
1522 return 1;
1523}
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1537 unsigned long ro, unsigned long incompat)
1538{
1539 journal_superblock_t *sb;
1540
1541 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1542 compat, ro, incompat);
1543
1544 sb = journal->j_superblock;
1545
1546 sb->s_feature_compat &= ~cpu_to_be32(compat);
1547 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1548 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1549}
1550EXPORT_SYMBOL(jbd2_journal_clear_features);
1551
1552
1553
1554
1555
1556
1557
1558
1559int jbd2_journal_update_format (journal_t *journal)
1560{
1561 journal_superblock_t *sb;
1562 int err;
1563
1564 err = journal_get_superblock(journal);
1565 if (err)
1566 return err;
1567
1568 sb = journal->j_superblock;
1569
1570 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
1571 case JBD2_SUPERBLOCK_V2:
1572 return 0;
1573 case JBD2_SUPERBLOCK_V1:
1574 return journal_convert_superblock_v1(journal, sb);
1575 default:
1576 break;
1577 }
1578 return -EINVAL;
1579}
1580
1581static int journal_convert_superblock_v1(journal_t *journal,
1582 journal_superblock_t *sb)
1583{
1584 int offset, blocksize;
1585 struct buffer_head *bh;
1586
1587 printk(KERN_WARNING
1588 "JBD2: Converting superblock from version 1 to 2.\n");
1589
1590
1591 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1592 blocksize = be32_to_cpu(sb->s_blocksize);
1593 memset(&sb->s_feature_compat, 0, blocksize-offset);
1594
1595 sb->s_nr_users = cpu_to_be32(1);
1596 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
1597 journal->j_format_version = 2;
1598
1599 bh = journal->j_sb_buffer;
1600 BUFFER_TRACE(bh, "marking dirty");
1601 mark_buffer_dirty(bh);
1602 sync_dirty_buffer(bh);
1603 return 0;
1604}
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616int jbd2_journal_flush(journal_t *journal)
1617{
1618 int err = 0;
1619 transaction_t *transaction = NULL;
1620 unsigned long old_tail;
1621
1622 write_lock(&journal->j_state_lock);
1623
1624
1625 if (journal->j_running_transaction) {
1626 transaction = journal->j_running_transaction;
1627 __jbd2_log_start_commit(journal, transaction->t_tid);
1628 } else if (journal->j_committing_transaction)
1629 transaction = journal->j_committing_transaction;
1630
1631
1632 if (transaction) {
1633 tid_t tid = transaction->t_tid;
1634
1635 write_unlock(&journal->j_state_lock);
1636 jbd2_log_wait_commit(journal, tid);
1637 } else {
1638 write_unlock(&journal->j_state_lock);
1639 }
1640
1641
1642 spin_lock(&journal->j_list_lock);
1643 while (!err && journal->j_checkpoint_transactions != NULL) {
1644 spin_unlock(&journal->j_list_lock);
1645 mutex_lock(&journal->j_checkpoint_mutex);
1646 err = jbd2_log_do_checkpoint(journal);
1647 mutex_unlock(&journal->j_checkpoint_mutex);
1648 spin_lock(&journal->j_list_lock);
1649 }
1650 spin_unlock(&journal->j_list_lock);
1651
1652 if (is_journal_aborted(journal))
1653 return -EIO;
1654
1655 jbd2_cleanup_journal_tail(journal);
1656
1657
1658
1659
1660
1661
1662 write_lock(&journal->j_state_lock);
1663 old_tail = journal->j_tail;
1664 journal->j_tail = 0;
1665 write_unlock(&journal->j_state_lock);
1666 jbd2_journal_update_superblock(journal, 1);
1667 write_lock(&journal->j_state_lock);
1668 journal->j_tail = old_tail;
1669
1670 J_ASSERT(!journal->j_running_transaction);
1671 J_ASSERT(!journal->j_committing_transaction);
1672 J_ASSERT(!journal->j_checkpoint_transactions);
1673 J_ASSERT(journal->j_head == journal->j_tail);
1674 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1675 write_unlock(&journal->j_state_lock);
1676 return 0;
1677}
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692int jbd2_journal_wipe(journal_t *journal, int write)
1693{
1694 int err = 0;
1695
1696 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
1697
1698 err = load_superblock(journal);
1699 if (err)
1700 return err;
1701
1702 if (!journal->j_tail)
1703 goto no_recovery;
1704
1705 printk(KERN_WARNING "JBD2: %s recovery information on journal\n",
1706 write ? "Clearing" : "Ignoring");
1707
1708 err = jbd2_journal_skip_recovery(journal);
1709 if (write)
1710 jbd2_journal_update_superblock(journal, 1);
1711
1712 no_recovery:
1713 return err;
1714}
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729void __jbd2_journal_abort_hard(journal_t *journal)
1730{
1731 transaction_t *transaction;
1732
1733 if (journal->j_flags & JBD2_ABORT)
1734 return;
1735
1736 printk(KERN_ERR "Aborting journal on device %s.\n",
1737 journal->j_devname);
1738
1739 write_lock(&journal->j_state_lock);
1740 journal->j_flags |= JBD2_ABORT;
1741 transaction = journal->j_running_transaction;
1742 if (transaction)
1743 __jbd2_log_start_commit(journal, transaction->t_tid);
1744 write_unlock(&journal->j_state_lock);
1745}
1746
1747
1748
1749static void __journal_abort_soft (journal_t *journal, int errno)
1750{
1751 if (journal->j_flags & JBD2_ABORT)
1752 return;
1753
1754 if (!journal->j_errno)
1755 journal->j_errno = errno;
1756
1757 __jbd2_journal_abort_hard(journal);
1758
1759 if (errno)
1760 jbd2_journal_update_superblock(journal, 1);
1761}
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
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
1809void jbd2_journal_abort(journal_t *journal, int errno)
1810{
1811 __journal_abort_soft(journal, errno);
1812}
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825int jbd2_journal_errno(journal_t *journal)
1826{
1827 int err;
1828
1829 read_lock(&journal->j_state_lock);
1830 if (journal->j_flags & JBD2_ABORT)
1831 err = -EROFS;
1832 else
1833 err = journal->j_errno;
1834 read_unlock(&journal->j_state_lock);
1835 return err;
1836}
1837
1838
1839
1840
1841
1842
1843
1844
1845int jbd2_journal_clear_err(journal_t *journal)
1846{
1847 int err = 0;
1848
1849 write_lock(&journal->j_state_lock);
1850 if (journal->j_flags & JBD2_ABORT)
1851 err = -EROFS;
1852 else
1853 journal->j_errno = 0;
1854 write_unlock(&journal->j_state_lock);
1855 return err;
1856}
1857
1858
1859
1860
1861
1862
1863
1864
1865void jbd2_journal_ack_err(journal_t *journal)
1866{
1867 write_lock(&journal->j_state_lock);
1868 if (journal->j_errno)
1869 journal->j_flags |= JBD2_ACK_ERR;
1870 write_unlock(&journal->j_state_lock);
1871}
1872
1873int jbd2_journal_blocks_per_page(struct inode *inode)
1874{
1875 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1876}
1877
1878
1879
1880
1881size_t journal_tag_bytes(journal_t *journal)
1882{
1883 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
1884 return JBD2_TAG_SIZE64;
1885 else
1886 return JBD2_TAG_SIZE32;
1887}
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904#define JBD2_MAX_SLABS 8
1905static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS];
1906
1907static const char *jbd2_slab_names[JBD2_MAX_SLABS] = {
1908 "jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k",
1909 "jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k"
1910};
1911
1912
1913static void jbd2_journal_destroy_slabs(void)
1914{
1915 int i;
1916
1917 for (i = 0; i < JBD2_MAX_SLABS; i++) {
1918 if (jbd2_slab[i])
1919 kmem_cache_destroy(jbd2_slab[i]);
1920 jbd2_slab[i] = NULL;
1921 }
1922}
1923
1924static int jbd2_journal_create_slab(size_t size)
1925{
1926 static DEFINE_MUTEX(jbd2_slab_create_mutex);
1927 int i = order_base_2(size) - 10;
1928 size_t slab_size;
1929
1930 if (size == PAGE_SIZE)
1931 return 0;
1932
1933 if (i >= JBD2_MAX_SLABS)
1934 return -EINVAL;
1935
1936 if (unlikely(i < 0))
1937 i = 0;
1938 mutex_lock(&jbd2_slab_create_mutex);
1939 if (jbd2_slab[i]) {
1940 mutex_unlock(&jbd2_slab_create_mutex);
1941 return 0;
1942 }
1943
1944 slab_size = 1 << (i+10);
1945 jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size,
1946 slab_size, 0, NULL);
1947 mutex_unlock(&jbd2_slab_create_mutex);
1948 if (!jbd2_slab[i]) {
1949 printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n");
1950 return -ENOMEM;
1951 }
1952 return 0;
1953}
1954
1955static struct kmem_cache *get_slab(size_t size)
1956{
1957 int i = order_base_2(size) - 10;
1958
1959 BUG_ON(i >= JBD2_MAX_SLABS);
1960 if (unlikely(i < 0))
1961 i = 0;
1962 BUG_ON(jbd2_slab[i] == NULL);
1963 return jbd2_slab[i];
1964}
1965
1966void *jbd2_alloc(size_t size, gfp_t flags)
1967{
1968 void *ptr;
1969
1970 BUG_ON(size & (size-1));
1971
1972 flags |= __GFP_REPEAT;
1973 if (size == PAGE_SIZE)
1974 ptr = (void *)__get_free_pages(flags, 0);
1975 else if (size > PAGE_SIZE) {
1976 int order = get_order(size);
1977
1978 if (order < 3)
1979 ptr = (void *)__get_free_pages(flags, order);
1980 else
1981 ptr = vmalloc(size);
1982 } else
1983 ptr = kmem_cache_alloc(get_slab(size), flags);
1984
1985
1986
1987 BUG_ON(((unsigned long) ptr) & (size-1));
1988
1989 return ptr;
1990}
1991
1992void jbd2_free(void *ptr, size_t size)
1993{
1994 if (size == PAGE_SIZE) {
1995 free_pages((unsigned long)ptr, 0);
1996 return;
1997 }
1998 if (size > PAGE_SIZE) {
1999 int order = get_order(size);
2000
2001 if (order < 3)
2002 free_pages((unsigned long)ptr, order);
2003 else
2004 vfree(ptr);
2005 return;
2006 }
2007 kmem_cache_free(get_slab(size), ptr);
2008};
2009
2010
2011
2012
2013static struct kmem_cache *jbd2_journal_head_cache;
2014#ifdef CONFIG_JBD2_DEBUG
2015static atomic_t nr_journal_heads = ATOMIC_INIT(0);
2016#endif
2017
2018static int journal_init_jbd2_journal_head_cache(void)
2019{
2020 int retval;
2021
2022 J_ASSERT(jbd2_journal_head_cache == NULL);
2023 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
2024 sizeof(struct journal_head),
2025 0,
2026 SLAB_TEMPORARY,
2027 NULL);
2028 retval = 0;
2029 if (!jbd2_journal_head_cache) {
2030 retval = -ENOMEM;
2031 printk(KERN_EMERG "JBD2: no memory for journal_head cache\n");
2032 }
2033 return retval;
2034}
2035
2036static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
2037{
2038 if (jbd2_journal_head_cache) {
2039 kmem_cache_destroy(jbd2_journal_head_cache);
2040 jbd2_journal_head_cache = NULL;
2041 }
2042}
2043
2044
2045
2046
2047static struct journal_head *journal_alloc_journal_head(void)
2048{
2049 struct journal_head *ret;
2050
2051#ifdef CONFIG_JBD2_DEBUG
2052 atomic_inc(&nr_journal_heads);
2053#endif
2054 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
2055 if (!ret) {
2056 jbd_debug(1, "out of memory for journal_head\n");
2057 pr_notice_ratelimited("ENOMEM in %s, retrying.\n", __func__);
2058 while (!ret) {
2059 yield();
2060 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
2061 }
2062 }
2063 return ret;
2064}
2065
2066static void journal_free_journal_head(struct journal_head *jh)
2067{
2068#ifdef CONFIG_JBD2_DEBUG
2069 atomic_dec(&nr_journal_heads);
2070 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
2071#endif
2072 kmem_cache_free(jbd2_journal_head_cache, jh);
2073}
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
2117{
2118 struct journal_head *jh;
2119 struct journal_head *new_jh = NULL;
2120
2121repeat:
2122 if (!buffer_jbd(bh)) {
2123 new_jh = journal_alloc_journal_head();
2124 memset(new_jh, 0, sizeof(*new_jh));
2125 }
2126
2127 jbd_lock_bh_journal_head(bh);
2128 if (buffer_jbd(bh)) {
2129 jh = bh2jh(bh);
2130 } else {
2131 J_ASSERT_BH(bh,
2132 (atomic_read(&bh->b_count) > 0) ||
2133 (bh->b_page && bh->b_page->mapping));
2134
2135 if (!new_jh) {
2136 jbd_unlock_bh_journal_head(bh);
2137 goto repeat;
2138 }
2139
2140 jh = new_jh;
2141 new_jh = NULL;
2142 set_buffer_jbd(bh);
2143 bh->b_private = jh;
2144 jh->b_bh = bh;
2145 get_bh(bh);
2146 BUFFER_TRACE(bh, "added journal_head");
2147 }
2148 jh->b_jcount++;
2149 jbd_unlock_bh_journal_head(bh);
2150 if (new_jh)
2151 journal_free_journal_head(new_jh);
2152 return bh->b_private;
2153}
2154
2155
2156
2157
2158
2159struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
2160{
2161 struct journal_head *jh = NULL;
2162
2163 jbd_lock_bh_journal_head(bh);
2164 if (buffer_jbd(bh)) {
2165 jh = bh2jh(bh);
2166 jh->b_jcount++;
2167 }
2168 jbd_unlock_bh_journal_head(bh);
2169 return jh;
2170}
2171
2172static void __journal_remove_journal_head(struct buffer_head *bh)
2173{
2174 struct journal_head *jh = bh2jh(bh);
2175
2176 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2177 J_ASSERT_JH(jh, jh->b_transaction == NULL);
2178 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
2179 J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
2180 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2181 J_ASSERT_BH(bh, buffer_jbd(bh));
2182 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2183 BUFFER_TRACE(bh, "remove journal_head");
2184 if (jh->b_frozen_data) {
2185 printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
2186 jbd2_free(jh->b_frozen_data, bh->b_size);
2187 }
2188 if (jh->b_committed_data) {
2189 printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
2190 jbd2_free(jh->b_committed_data, bh->b_size);
2191 }
2192 bh->b_private = NULL;
2193 jh->b_bh = NULL;
2194 clear_buffer_jbd(bh);
2195 journal_free_journal_head(jh);
2196}
2197
2198
2199
2200
2201
2202void jbd2_journal_put_journal_head(struct journal_head *jh)
2203{
2204 struct buffer_head *bh = jh2bh(jh);
2205
2206 jbd_lock_bh_journal_head(bh);
2207 J_ASSERT_JH(jh, jh->b_jcount > 0);
2208 --jh->b_jcount;
2209 if (!jh->b_jcount) {
2210 __journal_remove_journal_head(bh);
2211 jbd_unlock_bh_journal_head(bh);
2212 __brelse(bh);
2213 } else
2214 jbd_unlock_bh_journal_head(bh);
2215}
2216
2217
2218
2219
2220void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2221{
2222 jinode->i_transaction = NULL;
2223 jinode->i_next_transaction = NULL;
2224 jinode->i_vfs_inode = inode;
2225 jinode->i_flags = 0;
2226 INIT_LIST_HEAD(&jinode->i_list);
2227}
2228
2229
2230
2231
2232
2233
2234void jbd2_journal_release_jbd_inode(journal_t *journal,
2235 struct jbd2_inode *jinode)
2236{
2237 if (!journal)
2238 return;
2239restart:
2240 spin_lock(&journal->j_list_lock);
2241
2242 if (test_bit(__JI_COMMIT_RUNNING, &jinode->i_flags)) {
2243 wait_queue_head_t *wq;
2244 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2245 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2246 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2247 spin_unlock(&journal->j_list_lock);
2248 schedule();
2249 finish_wait(wq, &wait.wait);
2250 goto restart;
2251 }
2252
2253 if (jinode->i_transaction) {
2254 list_del(&jinode->i_list);
2255 jinode->i_transaction = NULL;
2256 }
2257 spin_unlock(&journal->j_list_lock);
2258}
2259
2260
2261
2262
2263#ifdef CONFIG_JBD2_DEBUG
2264u8 jbd2_journal_enable_debug __read_mostly;
2265EXPORT_SYMBOL(jbd2_journal_enable_debug);
2266
2267#define JBD2_DEBUG_NAME "jbd2-debug"
2268
2269static struct dentry *jbd2_debugfs_dir;
2270static struct dentry *jbd2_debug;
2271
2272static void __init jbd2_create_debugfs_entry(void)
2273{
2274 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2275 if (jbd2_debugfs_dir)
2276 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME,
2277 S_IRUGO | S_IWUSR,
2278 jbd2_debugfs_dir,
2279 &jbd2_journal_enable_debug);
2280}
2281
2282static void __exit jbd2_remove_debugfs_entry(void)
2283{
2284 debugfs_remove(jbd2_debug);
2285 debugfs_remove(jbd2_debugfs_dir);
2286}
2287
2288#else
2289
2290static void __init jbd2_create_debugfs_entry(void)
2291{
2292}
2293
2294static void __exit jbd2_remove_debugfs_entry(void)
2295{
2296}
2297
2298#endif
2299
2300#ifdef CONFIG_PROC_FS
2301
2302#define JBD2_STATS_PROC_NAME "fs/jbd2"
2303
2304static void __init jbd2_create_jbd_stats_proc_entry(void)
2305{
2306 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2307}
2308
2309static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2310{
2311 if (proc_jbd2_stats)
2312 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2313}
2314
2315#else
2316
2317#define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2318#define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2319
2320#endif
2321
2322struct kmem_cache *jbd2_handle_cache, *jbd2_inode_cache;
2323
2324static int __init journal_init_handle_cache(void)
2325{
2326 jbd2_handle_cache = KMEM_CACHE(jbd2_journal_handle, SLAB_TEMPORARY);
2327 if (jbd2_handle_cache == NULL) {
2328 printk(KERN_EMERG "JBD2: failed to create handle cache\n");
2329 return -ENOMEM;
2330 }
2331 jbd2_inode_cache = KMEM_CACHE(jbd2_inode, 0);
2332 if (jbd2_inode_cache == NULL) {
2333 printk(KERN_EMERG "JBD2: failed to create inode cache\n");
2334 kmem_cache_destroy(jbd2_handle_cache);
2335 return -ENOMEM;
2336 }
2337 return 0;
2338}
2339
2340static void jbd2_journal_destroy_handle_cache(void)
2341{
2342 if (jbd2_handle_cache)
2343 kmem_cache_destroy(jbd2_handle_cache);
2344 if (jbd2_inode_cache)
2345 kmem_cache_destroy(jbd2_inode_cache);
2346
2347}
2348
2349
2350
2351
2352
2353static int __init journal_init_caches(void)
2354{
2355 int ret;
2356
2357 ret = jbd2_journal_init_revoke_caches();
2358 if (ret == 0)
2359 ret = journal_init_jbd2_journal_head_cache();
2360 if (ret == 0)
2361 ret = journal_init_handle_cache();
2362 return ret;
2363}
2364
2365static void jbd2_journal_destroy_caches(void)
2366{
2367 jbd2_journal_destroy_revoke_caches();
2368 jbd2_journal_destroy_jbd2_journal_head_cache();
2369 jbd2_journal_destroy_handle_cache();
2370 jbd2_journal_destroy_slabs();
2371}
2372
2373static int __init journal_init(void)
2374{
2375 int ret;
2376
2377 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2378
2379 ret = journal_init_caches();
2380 if (ret == 0) {
2381 jbd2_create_debugfs_entry();
2382 jbd2_create_jbd_stats_proc_entry();
2383 } else {
2384 jbd2_journal_destroy_caches();
2385 }
2386 return ret;
2387}
2388
2389static void __exit journal_exit(void)
2390{
2391#ifdef CONFIG_JBD2_DEBUG
2392 int n = atomic_read(&nr_journal_heads);
2393 if (n)
2394 printk(KERN_EMERG "JBD2: leaked %d journal_heads!\n", n);
2395#endif
2396 jbd2_remove_debugfs_entry();
2397 jbd2_remove_jbd_stats_proc_entry();
2398 jbd2_journal_destroy_caches();
2399}
2400
2401MODULE_LICENSE("GPL");
2402module_init(journal_init);
2403module_exit(journal_exit);
2404
2405