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