]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/jbd2/journal.c
jbd2: cleanup needed free block estimates when starting a transaction
[karo-tx-linux.git] / fs / jbd2 / journal.c
1 /*
2  * linux/fs/jbd2/journal.c
3  *
4  * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
5  *
6  * Copyright 1998 Red Hat corp --- All Rights Reserved
7  *
8  * This file is part of the Linux kernel and is made available under
9  * the terms of the GNU General Public License, version 2, or at your
10  * option, any later version, incorporated herein by reference.
11  *
12  * Generic filesystem journal-writing code; part of the ext2fs
13  * journaling system.
14  *
15  * This file manages journals: areas of disk reserved for logging
16  * transactional updates.  This includes the kernel journaling thread
17  * which is responsible for scheduling updates to the log.
18  *
19  * We do not actually manage the physical storage of the journal in this
20  * file: that is left to a per-journal policy function, which allows us
21  * to store the journal within a filesystem-specified area for ext2
22  * journaling (ext2 can use a reserved inode for storing the log).
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/seq_file.h>
39 #include <linux/math64.h>
40 #include <linux/hash.h>
41 #include <linux/log2.h>
42 #include <linux/vmalloc.h>
43 #include <linux/backing-dev.h>
44 #include <linux/bitops.h>
45 #include <linux/ratelimit.h>
46
47 #define CREATE_TRACE_POINTS
48 #include <trace/events/jbd2.h>
49
50 #include <asm/uaccess.h>
51 #include <asm/page.h>
52
53 #ifdef CONFIG_JBD2_DEBUG
54 ushort jbd2_journal_enable_debug __read_mostly;
55 EXPORT_SYMBOL(jbd2_journal_enable_debug);
56
57 module_param_named(jbd2_debug, jbd2_journal_enable_debug, ushort, 0644);
58 MODULE_PARM_DESC(jbd2_debug, "Debugging level for jbd2");
59 #endif
60
61 EXPORT_SYMBOL(jbd2_journal_extend);
62 EXPORT_SYMBOL(jbd2_journal_stop);
63 EXPORT_SYMBOL(jbd2_journal_lock_updates);
64 EXPORT_SYMBOL(jbd2_journal_unlock_updates);
65 EXPORT_SYMBOL(jbd2_journal_get_write_access);
66 EXPORT_SYMBOL(jbd2_journal_get_create_access);
67 EXPORT_SYMBOL(jbd2_journal_get_undo_access);
68 EXPORT_SYMBOL(jbd2_journal_set_triggers);
69 EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
70 EXPORT_SYMBOL(jbd2_journal_forget);
71 #if 0
72 EXPORT_SYMBOL(journal_sync_buffer);
73 #endif
74 EXPORT_SYMBOL(jbd2_journal_flush);
75 EXPORT_SYMBOL(jbd2_journal_revoke);
76
77 EXPORT_SYMBOL(jbd2_journal_init_dev);
78 EXPORT_SYMBOL(jbd2_journal_init_inode);
79 EXPORT_SYMBOL(jbd2_journal_check_used_features);
80 EXPORT_SYMBOL(jbd2_journal_check_available_features);
81 EXPORT_SYMBOL(jbd2_journal_set_features);
82 EXPORT_SYMBOL(jbd2_journal_load);
83 EXPORT_SYMBOL(jbd2_journal_destroy);
84 EXPORT_SYMBOL(jbd2_journal_abort);
85 EXPORT_SYMBOL(jbd2_journal_errno);
86 EXPORT_SYMBOL(jbd2_journal_ack_err);
87 EXPORT_SYMBOL(jbd2_journal_clear_err);
88 EXPORT_SYMBOL(jbd2_log_wait_commit);
89 EXPORT_SYMBOL(jbd2_log_start_commit);
90 EXPORT_SYMBOL(jbd2_journal_start_commit);
91 EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
92 EXPORT_SYMBOL(jbd2_journal_wipe);
93 EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
94 EXPORT_SYMBOL(jbd2_journal_invalidatepage);
95 EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
96 EXPORT_SYMBOL(jbd2_journal_force_commit);
97 EXPORT_SYMBOL(jbd2_journal_file_inode);
98 EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
99 EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
100 EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
101 EXPORT_SYMBOL(jbd2_inode_cache);
102
103 static void __journal_abort_soft (journal_t *journal, int errno);
104 static int jbd2_journal_create_slab(size_t slab_size);
105
106 /* Checksumming functions */
107 int jbd2_verify_csum_type(journal_t *j, journal_superblock_t *sb)
108 {
109         if (!JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_CSUM_V2))
110                 return 1;
111
112         return sb->s_checksum_type == JBD2_CRC32C_CHKSUM;
113 }
114
115 static __u32 jbd2_superblock_csum(journal_t *j, journal_superblock_t *sb)
116 {
117         __u32 csum, old_csum;
118
119         old_csum = sb->s_checksum;
120         sb->s_checksum = 0;
121         csum = jbd2_chksum(j, ~0, (char *)sb, sizeof(journal_superblock_t));
122         sb->s_checksum = old_csum;
123
124         return cpu_to_be32(csum);
125 }
126
127 int jbd2_superblock_csum_verify(journal_t *j, journal_superblock_t *sb)
128 {
129         if (!JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_CSUM_V2))
130                 return 1;
131
132         return sb->s_checksum == jbd2_superblock_csum(j, sb);
133 }
134
135 void jbd2_superblock_csum_set(journal_t *j, journal_superblock_t *sb)
136 {
137         if (!JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_CSUM_V2))
138                 return;
139
140         sb->s_checksum = jbd2_superblock_csum(j, sb);
141 }
142
143 /*
144  * Helper function used to manage commit timeouts
145  */
146
147 static void commit_timeout(unsigned long __data)
148 {
149         struct task_struct * p = (struct task_struct *) __data;
150
151         wake_up_process(p);
152 }
153
154 /*
155  * kjournald2: The main thread function used to manage a logging device
156  * journal.
157  *
158  * This kernel thread is responsible for two things:
159  *
160  * 1) COMMIT:  Every so often we need to commit the current state of the
161  *    filesystem to disk.  The journal thread is responsible for writing
162  *    all of the metadata buffers to disk.
163  *
164  * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
165  *    of the data in that part of the log has been rewritten elsewhere on
166  *    the disk.  Flushing these old buffers to reclaim space in the log is
167  *    known as checkpointing, and this thread is responsible for that job.
168  */
169
170 static int kjournald2(void *arg)
171 {
172         journal_t *journal = arg;
173         transaction_t *transaction;
174
175         /*
176          * Set up an interval timer which can be used to trigger a commit wakeup
177          * after the commit interval expires
178          */
179         setup_timer(&journal->j_commit_timer, commit_timeout,
180                         (unsigned long)current);
181
182         set_freezable();
183
184         /* Record that the journal thread is running */
185         journal->j_task = current;
186         wake_up(&journal->j_wait_done_commit);
187
188         /*
189          * And now, wait forever for commit wakeup events.
190          */
191         write_lock(&journal->j_state_lock);
192
193 loop:
194         if (journal->j_flags & JBD2_UNMOUNT)
195                 goto end_loop;
196
197         jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
198                 journal->j_commit_sequence, journal->j_commit_request);
199
200         if (journal->j_commit_sequence != journal->j_commit_request) {
201                 jbd_debug(1, "OK, requests differ\n");
202                 write_unlock(&journal->j_state_lock);
203                 del_timer_sync(&journal->j_commit_timer);
204                 jbd2_journal_commit_transaction(journal);
205                 write_lock(&journal->j_state_lock);
206                 goto loop;
207         }
208
209         wake_up(&journal->j_wait_done_commit);
210         if (freezing(current)) {
211                 /*
212                  * The simpler the better. Flushing journal isn't a
213                  * good idea, because that depends on threads that may
214                  * be already stopped.
215                  */
216                 jbd_debug(1, "Now suspending kjournald2\n");
217                 write_unlock(&journal->j_state_lock);
218                 try_to_freeze();
219                 write_lock(&journal->j_state_lock);
220         } else {
221                 /*
222                  * We assume on resume that commits are already there,
223                  * so we don't sleep
224                  */
225                 DEFINE_WAIT(wait);
226                 int should_sleep = 1;
227
228                 prepare_to_wait(&journal->j_wait_commit, &wait,
229                                 TASK_INTERRUPTIBLE);
230                 if (journal->j_commit_sequence != journal->j_commit_request)
231                         should_sleep = 0;
232                 transaction = journal->j_running_transaction;
233                 if (transaction && time_after_eq(jiffies,
234                                                 transaction->t_expires))
235                         should_sleep = 0;
236                 if (journal->j_flags & JBD2_UNMOUNT)
237                         should_sleep = 0;
238                 if (should_sleep) {
239                         write_unlock(&journal->j_state_lock);
240                         schedule();
241                         write_lock(&journal->j_state_lock);
242                 }
243                 finish_wait(&journal->j_wait_commit, &wait);
244         }
245
246         jbd_debug(1, "kjournald2 wakes\n");
247
248         /*
249          * Were we woken up by a commit wakeup event?
250          */
251         transaction = journal->j_running_transaction;
252         if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
253                 journal->j_commit_request = transaction->t_tid;
254                 jbd_debug(1, "woke because of timeout\n");
255         }
256         goto loop;
257
258 end_loop:
259         write_unlock(&journal->j_state_lock);
260         del_timer_sync(&journal->j_commit_timer);
261         journal->j_task = NULL;
262         wake_up(&journal->j_wait_done_commit);
263         jbd_debug(1, "Journal thread exiting.\n");
264         return 0;
265 }
266
267 static int jbd2_journal_start_thread(journal_t *journal)
268 {
269         struct task_struct *t;
270
271         t = kthread_run(kjournald2, journal, "jbd2/%s",
272                         journal->j_devname);
273         if (IS_ERR(t))
274                 return PTR_ERR(t);
275
276         wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
277         return 0;
278 }
279
280 static void journal_kill_thread(journal_t *journal)
281 {
282         write_lock(&journal->j_state_lock);
283         journal->j_flags |= JBD2_UNMOUNT;
284
285         while (journal->j_task) {
286                 wake_up(&journal->j_wait_commit);
287                 write_unlock(&journal->j_state_lock);
288                 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
289                 write_lock(&journal->j_state_lock);
290         }
291         write_unlock(&journal->j_state_lock);
292 }
293
294 /*
295  * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
296  *
297  * Writes a metadata buffer to a given disk block.  The actual IO is not
298  * performed but a new buffer_head is constructed which labels the data
299  * to be written with the correct destination disk block.
300  *
301  * Any magic-number escaping which needs to be done will cause a
302  * copy-out here.  If the buffer happens to start with the
303  * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
304  * magic number is only written to the log for descripter blocks.  In
305  * this case, we copy the data and replace the first word with 0, and we
306  * return a result code which indicates that this buffer needs to be
307  * marked as an escaped buffer in the corresponding log descriptor
308  * block.  The missing word can then be restored when the block is read
309  * during recovery.
310  *
311  * If the source buffer has already been modified by a new transaction
312  * since we took the last commit snapshot, we use the frozen copy of
313  * that data for IO. If we end up using the existing buffer_head's data
314  * for the write, then we have to make sure nobody modifies it while the
315  * IO is in progress. do_get_write_access() handles this.
316  *
317  * The function returns a pointer to the buffer_head to be used for IO.
318  * 
319  *
320  * Return value:
321  *  <0: Error
322  * >=0: Finished OK
323  *
324  * On success:
325  * Bit 0 set == escape performed on the data
326  * Bit 1 set == buffer copy-out performed (kfree the data after IO)
327  */
328
329 int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
330                                   struct journal_head  *jh_in,
331                                   struct buffer_head **bh_out,
332                                   sector_t blocknr)
333 {
334         int need_copy_out = 0;
335         int done_copy_out = 0;
336         int do_escape = 0;
337         char *mapped_data;
338         struct buffer_head *new_bh;
339         struct page *new_page;
340         unsigned int new_offset;
341         struct buffer_head *bh_in = jh2bh(jh_in);
342         journal_t *journal = transaction->t_journal;
343
344         /*
345          * The buffer really shouldn't be locked: only the current committing
346          * transaction is allowed to write it, so nobody else is allowed
347          * to do any IO.
348          *
349          * akpm: except if we're journalling data, and write() output is
350          * also part of a shared mapping, and another thread has
351          * decided to launch a writepage() against this buffer.
352          */
353         J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
354
355 retry_alloc:
356         new_bh = alloc_buffer_head(GFP_NOFS);
357         if (!new_bh) {
358                 /*
359                  * Failure is not an option, but __GFP_NOFAIL is going
360                  * away; so we retry ourselves here.
361                  */
362                 congestion_wait(BLK_RW_ASYNC, HZ/50);
363                 goto retry_alloc;
364         }
365
366         /* keep subsequent assertions sane */
367         atomic_set(&new_bh->b_count, 1);
368
369         jbd_lock_bh_state(bh_in);
370 repeat:
371         /*
372          * If a new transaction has already done a buffer copy-out, then
373          * we use that version of the data for the commit.
374          */
375         if (jh_in->b_frozen_data) {
376                 done_copy_out = 1;
377                 new_page = virt_to_page(jh_in->b_frozen_data);
378                 new_offset = offset_in_page(jh_in->b_frozen_data);
379         } else {
380                 new_page = jh2bh(jh_in)->b_page;
381                 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
382         }
383
384         mapped_data = kmap_atomic(new_page);
385         /*
386          * Fire data frozen trigger if data already wasn't frozen.  Do this
387          * before checking for escaping, as the trigger may modify the magic
388          * offset.  If a copy-out happens afterwards, it will have the correct
389          * data in the buffer.
390          */
391         if (!done_copy_out)
392                 jbd2_buffer_frozen_trigger(jh_in, mapped_data + new_offset,
393                                            jh_in->b_triggers);
394
395         /*
396          * Check for escaping
397          */
398         if (*((__be32 *)(mapped_data + new_offset)) ==
399                                 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
400                 need_copy_out = 1;
401                 do_escape = 1;
402         }
403         kunmap_atomic(mapped_data);
404
405         /*
406          * Do we need to do a data copy?
407          */
408         if (need_copy_out && !done_copy_out) {
409                 char *tmp;
410
411                 jbd_unlock_bh_state(bh_in);
412                 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
413                 if (!tmp) {
414                         brelse(new_bh);
415                         return -ENOMEM;
416                 }
417                 jbd_lock_bh_state(bh_in);
418                 if (jh_in->b_frozen_data) {
419                         jbd2_free(tmp, bh_in->b_size);
420                         goto repeat;
421                 }
422
423                 jh_in->b_frozen_data = tmp;
424                 mapped_data = kmap_atomic(new_page);
425                 memcpy(tmp, mapped_data + new_offset, bh_in->b_size);
426                 kunmap_atomic(mapped_data);
427
428                 new_page = virt_to_page(tmp);
429                 new_offset = offset_in_page(tmp);
430                 done_copy_out = 1;
431
432                 /*
433                  * This isn't strictly necessary, as we're using frozen
434                  * data for the escaping, but it keeps consistency with
435                  * b_frozen_data usage.
436                  */
437                 jh_in->b_frozen_triggers = jh_in->b_triggers;
438         }
439
440         /*
441          * Did we need to do an escaping?  Now we've done all the
442          * copying, we can finally do so.
443          */
444         if (do_escape) {
445                 mapped_data = kmap_atomic(new_page);
446                 *((unsigned int *)(mapped_data + new_offset)) = 0;
447                 kunmap_atomic(mapped_data);
448         }
449
450         set_bh_page(new_bh, new_page, new_offset);
451         new_bh->b_size = bh_in->b_size;
452         new_bh->b_bdev = journal->j_dev;
453         new_bh->b_blocknr = blocknr;
454         new_bh->b_private = bh_in;
455         set_buffer_mapped(new_bh);
456         set_buffer_dirty(new_bh);
457
458         *bh_out = new_bh;
459
460         /*
461          * The to-be-written buffer needs to get moved to the io queue,
462          * and the original buffer whose contents we are shadowing or
463          * copying is moved to the transaction's shadow queue.
464          */
465         JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
466         spin_lock(&journal->j_list_lock);
467         __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
468         spin_unlock(&journal->j_list_lock);
469         set_buffer_shadow(bh_in);
470         jbd_unlock_bh_state(bh_in);
471
472         return do_escape | (done_copy_out << 1);
473 }
474
475 /*
476  * Allocation code for the journal file.  Manage the space left in the
477  * journal, so that we can begin checkpointing when appropriate.
478  */
479
480 /*
481  * Called with j_state_lock locked for writing.
482  * Returns true if a transaction commit was started.
483  */
484 int __jbd2_log_start_commit(journal_t *journal, tid_t target)
485 {
486         /* Return if the txn has already requested to be committed */
487         if (journal->j_commit_request == target)
488                 return 0;
489
490         /*
491          * The only transaction we can possibly wait upon is the
492          * currently running transaction (if it exists).  Otherwise,
493          * the target tid must be an old one.
494          */
495         if (journal->j_running_transaction &&
496             journal->j_running_transaction->t_tid == target) {
497                 /*
498                  * We want a new commit: OK, mark the request and wakeup the
499                  * commit thread.  We do _not_ do the commit ourselves.
500                  */
501
502                 journal->j_commit_request = target;
503                 jbd_debug(1, "JBD2: requesting commit %d/%d\n",
504                           journal->j_commit_request,
505                           journal->j_commit_sequence);
506                 journal->j_running_transaction->t_requested = jiffies;
507                 wake_up(&journal->j_wait_commit);
508                 return 1;
509         } else if (!tid_geq(journal->j_commit_request, target))
510                 /* This should never happen, but if it does, preserve
511                    the evidence before kjournald goes into a loop and
512                    increments j_commit_sequence beyond all recognition. */
513                 WARN_ONCE(1, "JBD2: bad log_start_commit: %u %u %u %u\n",
514                           journal->j_commit_request,
515                           journal->j_commit_sequence,
516                           target, journal->j_running_transaction ? 
517                           journal->j_running_transaction->t_tid : 0);
518         return 0;
519 }
520
521 int jbd2_log_start_commit(journal_t *journal, tid_t tid)
522 {
523         int ret;
524
525         write_lock(&journal->j_state_lock);
526         ret = __jbd2_log_start_commit(journal, tid);
527         write_unlock(&journal->j_state_lock);
528         return ret;
529 }
530
531 /*
532  * Force and wait upon a commit if the calling process is not within
533  * transaction.  This is used for forcing out undo-protected data which contains
534  * bitmaps, when the fs is running out of space.
535  *
536  * We can only force the running transaction if we don't have an active handle;
537  * otherwise, we will deadlock.
538  *
539  * Returns true if a transaction was started.
540  */
541 int jbd2_journal_force_commit_nested(journal_t *journal)
542 {
543         transaction_t *transaction = NULL;
544         tid_t tid;
545         int need_to_start = 0;
546
547         read_lock(&journal->j_state_lock);
548         if (journal->j_running_transaction && !current->journal_info) {
549                 transaction = journal->j_running_transaction;
550                 if (!tid_geq(journal->j_commit_request, transaction->t_tid))
551                         need_to_start = 1;
552         } else if (journal->j_committing_transaction)
553                 transaction = journal->j_committing_transaction;
554
555         if (!transaction) {
556                 read_unlock(&journal->j_state_lock);
557                 return 0;       /* Nothing to retry */
558         }
559
560         tid = transaction->t_tid;
561         read_unlock(&journal->j_state_lock);
562         if (need_to_start)
563                 jbd2_log_start_commit(journal, tid);
564         jbd2_log_wait_commit(journal, tid);
565         return 1;
566 }
567
568 /*
569  * Start a commit of the current running transaction (if any).  Returns true
570  * if a transaction is going to be committed (or is currently already
571  * committing), and fills its tid in at *ptid
572  */
573 int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
574 {
575         int ret = 0;
576
577         write_lock(&journal->j_state_lock);
578         if (journal->j_running_transaction) {
579                 tid_t tid = journal->j_running_transaction->t_tid;
580
581                 __jbd2_log_start_commit(journal, tid);
582                 /* There's a running transaction and we've just made sure
583                  * it's commit has been scheduled. */
584                 if (ptid)
585                         *ptid = tid;
586                 ret = 1;
587         } else if (journal->j_committing_transaction) {
588                 /*
589                  * If commit has been started, then we have to wait for
590                  * completion of that transaction.
591                  */
592                 if (ptid)
593                         *ptid = journal->j_committing_transaction->t_tid;
594                 ret = 1;
595         }
596         write_unlock(&journal->j_state_lock);
597         return ret;
598 }
599
600 /*
601  * Return 1 if a given transaction has not yet sent barrier request
602  * connected with a transaction commit. If 0 is returned, transaction
603  * may or may not have sent the barrier. Used to avoid sending barrier
604  * twice in common cases.
605  */
606 int jbd2_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
607 {
608         int ret = 0;
609         transaction_t *commit_trans;
610
611         if (!(journal->j_flags & JBD2_BARRIER))
612                 return 0;
613         read_lock(&journal->j_state_lock);
614         /* Transaction already committed? */
615         if (tid_geq(journal->j_commit_sequence, tid))
616                 goto out;
617         commit_trans = journal->j_committing_transaction;
618         if (!commit_trans || commit_trans->t_tid != tid) {
619                 ret = 1;
620                 goto out;
621         }
622         /*
623          * Transaction is being committed and we already proceeded to
624          * submitting a flush to fs partition?
625          */
626         if (journal->j_fs_dev != journal->j_dev) {
627                 if (!commit_trans->t_need_data_flush ||
628                     commit_trans->t_state >= T_COMMIT_DFLUSH)
629                         goto out;
630         } else {
631                 if (commit_trans->t_state >= T_COMMIT_JFLUSH)
632                         goto out;
633         }
634         ret = 1;
635 out:
636         read_unlock(&journal->j_state_lock);
637         return ret;
638 }
639 EXPORT_SYMBOL(jbd2_trans_will_send_data_barrier);
640
641 /*
642  * Wait for a specified commit to complete.
643  * The caller may not hold the journal lock.
644  */
645 int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
646 {
647         int err = 0;
648
649         read_lock(&journal->j_state_lock);
650 #ifdef CONFIG_JBD2_DEBUG
651         if (!tid_geq(journal->j_commit_request, tid)) {
652                 printk(KERN_EMERG
653                        "%s: error: j_commit_request=%d, tid=%d\n",
654                        __func__, journal->j_commit_request, tid);
655         }
656 #endif
657         while (tid_gt(tid, journal->j_commit_sequence)) {
658                 jbd_debug(1, "JBD2: want %d, j_commit_sequence=%d\n",
659                                   tid, journal->j_commit_sequence);
660                 wake_up(&journal->j_wait_commit);
661                 read_unlock(&journal->j_state_lock);
662                 wait_event(journal->j_wait_done_commit,
663                                 !tid_gt(tid, journal->j_commit_sequence));
664                 read_lock(&journal->j_state_lock);
665         }
666         read_unlock(&journal->j_state_lock);
667
668         if (unlikely(is_journal_aborted(journal))) {
669                 printk(KERN_EMERG "journal commit I/O error\n");
670                 err = -EIO;
671         }
672         return err;
673 }
674
675 /*
676  * When this function returns the transaction corresponding to tid
677  * will be completed.  If the transaction has currently running, start
678  * committing that transaction before waiting for it to complete.  If
679  * the transaction id is stale, it is by definition already completed,
680  * so just return SUCCESS.
681  */
682 int jbd2_complete_transaction(journal_t *journal, tid_t tid)
683 {
684         int     need_to_wait = 1;
685
686         read_lock(&journal->j_state_lock);
687         if (journal->j_running_transaction &&
688             journal->j_running_transaction->t_tid == tid) {
689                 if (journal->j_commit_request != tid) {
690                         /* transaction not yet started, so request it */
691                         read_unlock(&journal->j_state_lock);
692                         jbd2_log_start_commit(journal, tid);
693                         goto wait_commit;
694                 }
695         } else if (!(journal->j_committing_transaction &&
696                      journal->j_committing_transaction->t_tid == tid))
697                 need_to_wait = 0;
698         read_unlock(&journal->j_state_lock);
699         if (!need_to_wait)
700                 return 0;
701 wait_commit:
702         return jbd2_log_wait_commit(journal, tid);
703 }
704 EXPORT_SYMBOL(jbd2_complete_transaction);
705
706 /*
707  * Log buffer allocation routines:
708  */
709
710 int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
711 {
712         unsigned long blocknr;
713
714         write_lock(&journal->j_state_lock);
715         J_ASSERT(journal->j_free > 1);
716
717         blocknr = journal->j_head;
718         journal->j_head++;
719         journal->j_free--;
720         if (journal->j_head == journal->j_last)
721                 journal->j_head = journal->j_first;
722         write_unlock(&journal->j_state_lock);
723         return jbd2_journal_bmap(journal, blocknr, retp);
724 }
725
726 /*
727  * Conversion of logical to physical block numbers for the journal
728  *
729  * On external journals the journal blocks are identity-mapped, so
730  * this is a no-op.  If needed, we can use j_blk_offset - everything is
731  * ready.
732  */
733 int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
734                  unsigned long long *retp)
735 {
736         int err = 0;
737         unsigned long long ret;
738
739         if (journal->j_inode) {
740                 ret = bmap(journal->j_inode, blocknr);
741                 if (ret)
742                         *retp = ret;
743                 else {
744                         printk(KERN_ALERT "%s: journal block not found "
745                                         "at offset %lu on %s\n",
746                                __func__, blocknr, journal->j_devname);
747                         err = -EIO;
748                         __journal_abort_soft(journal, err);
749                 }
750         } else {
751                 *retp = blocknr; /* +journal->j_blk_offset */
752         }
753         return err;
754 }
755
756 /*
757  * We play buffer_head aliasing tricks to write data/metadata blocks to
758  * the journal without copying their contents, but for journal
759  * descriptor blocks we do need to generate bona fide buffers.
760  *
761  * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
762  * the buffer's contents they really should run flush_dcache_page(bh->b_page).
763  * But we don't bother doing that, so there will be coherency problems with
764  * mmaps of blockdevs which hold live JBD-controlled filesystems.
765  */
766 struct buffer_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
767 {
768         struct buffer_head *bh;
769         unsigned long long blocknr;
770         int err;
771
772         err = jbd2_journal_next_log_block(journal, &blocknr);
773
774         if (err)
775                 return NULL;
776
777         bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
778         if (!bh)
779                 return NULL;
780         lock_buffer(bh);
781         memset(bh->b_data, 0, journal->j_blocksize);
782         set_buffer_uptodate(bh);
783         unlock_buffer(bh);
784         BUFFER_TRACE(bh, "return this buffer");
785         return bh;
786 }
787
788 /*
789  * Return tid of the oldest transaction in the journal and block in the journal
790  * where the transaction starts.
791  *
792  * If the journal is now empty, return which will be the next transaction ID
793  * we will write and where will that transaction start.
794  *
795  * The return value is 0 if journal tail cannot be pushed any further, 1 if
796  * it can.
797  */
798 int jbd2_journal_get_log_tail(journal_t *journal, tid_t *tid,
799                               unsigned long *block)
800 {
801         transaction_t *transaction;
802         int ret;
803
804         read_lock(&journal->j_state_lock);
805         spin_lock(&journal->j_list_lock);
806         transaction = journal->j_checkpoint_transactions;
807         if (transaction) {
808                 *tid = transaction->t_tid;
809                 *block = transaction->t_log_start;
810         } else if ((transaction = journal->j_committing_transaction) != NULL) {
811                 *tid = transaction->t_tid;
812                 *block = transaction->t_log_start;
813         } else if ((transaction = journal->j_running_transaction) != NULL) {
814                 *tid = transaction->t_tid;
815                 *block = journal->j_head;
816         } else {
817                 *tid = journal->j_transaction_sequence;
818                 *block = journal->j_head;
819         }
820         ret = tid_gt(*tid, journal->j_tail_sequence);
821         spin_unlock(&journal->j_list_lock);
822         read_unlock(&journal->j_state_lock);
823
824         return ret;
825 }
826
827 /*
828  * Update information in journal structure and in on disk journal superblock
829  * about log tail. This function does not check whether information passed in
830  * really pushes log tail further. It's responsibility of the caller to make
831  * sure provided log tail information is valid (e.g. by holding
832  * j_checkpoint_mutex all the time between computing log tail and calling this
833  * function as is the case with jbd2_cleanup_journal_tail()).
834  *
835  * Requires j_checkpoint_mutex
836  */
837 void __jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block)
838 {
839         unsigned long freed;
840
841         BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
842
843         /*
844          * We cannot afford for write to remain in drive's caches since as
845          * soon as we update j_tail, next transaction can start reusing journal
846          * space and if we lose sb update during power failure we'd replay
847          * old transaction with possibly newly overwritten data.
848          */
849         jbd2_journal_update_sb_log_tail(journal, tid, block, WRITE_FUA);
850         write_lock(&journal->j_state_lock);
851         freed = block - journal->j_tail;
852         if (block < journal->j_tail)
853                 freed += journal->j_last - journal->j_first;
854
855         trace_jbd2_update_log_tail(journal, tid, block, freed);
856         jbd_debug(1,
857                   "Cleaning journal tail from %d to %d (offset %lu), "
858                   "freeing %lu\n",
859                   journal->j_tail_sequence, tid, block, freed);
860
861         journal->j_free += freed;
862         journal->j_tail_sequence = tid;
863         journal->j_tail = block;
864         write_unlock(&journal->j_state_lock);
865 }
866
867 /*
868  * This is a variaon of __jbd2_update_log_tail which checks for validity of
869  * provided log tail and locks j_checkpoint_mutex. So it is safe against races
870  * with other threads updating log tail.
871  */
872 void jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block)
873 {
874         mutex_lock(&journal->j_checkpoint_mutex);
875         if (tid_gt(tid, journal->j_tail_sequence))
876                 __jbd2_update_log_tail(journal, tid, block);
877         mutex_unlock(&journal->j_checkpoint_mutex);
878 }
879
880 struct jbd2_stats_proc_session {
881         journal_t *journal;
882         struct transaction_stats_s *stats;
883         int start;
884         int max;
885 };
886
887 static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
888 {
889         return *pos ? NULL : SEQ_START_TOKEN;
890 }
891
892 static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
893 {
894         return NULL;
895 }
896
897 static int jbd2_seq_info_show(struct seq_file *seq, void *v)
898 {
899         struct jbd2_stats_proc_session *s = seq->private;
900
901         if (v != SEQ_START_TOKEN)
902                 return 0;
903         seq_printf(seq, "%lu transactions (%lu requested), "
904                    "each up to %u blocks\n",
905                    s->stats->ts_tid, s->stats->ts_requested,
906                    s->journal->j_max_transaction_buffers);
907         if (s->stats->ts_tid == 0)
908                 return 0;
909         seq_printf(seq, "average: \n  %ums waiting for transaction\n",
910             jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid));
911         seq_printf(seq, "  %ums request delay\n",
912             (s->stats->ts_requested == 0) ? 0 :
913             jiffies_to_msecs(s->stats->run.rs_request_delay /
914                              s->stats->ts_requested));
915         seq_printf(seq, "  %ums running transaction\n",
916             jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid));
917         seq_printf(seq, "  %ums transaction was being locked\n",
918             jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid));
919         seq_printf(seq, "  %ums flushing data (in ordered mode)\n",
920             jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid));
921         seq_printf(seq, "  %ums logging transaction\n",
922             jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid));
923         seq_printf(seq, "  %lluus average transaction commit time\n",
924                    div_u64(s->journal->j_average_commit_time, 1000));
925         seq_printf(seq, "  %lu handles per transaction\n",
926             s->stats->run.rs_handle_count / s->stats->ts_tid);
927         seq_printf(seq, "  %lu blocks per transaction\n",
928             s->stats->run.rs_blocks / s->stats->ts_tid);
929         seq_printf(seq, "  %lu logged blocks per transaction\n",
930             s->stats->run.rs_blocks_logged / s->stats->ts_tid);
931         return 0;
932 }
933
934 static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
935 {
936 }
937
938 static const struct seq_operations jbd2_seq_info_ops = {
939         .start  = jbd2_seq_info_start,
940         .next   = jbd2_seq_info_next,
941         .stop   = jbd2_seq_info_stop,
942         .show   = jbd2_seq_info_show,
943 };
944
945 static int jbd2_seq_info_open(struct inode *inode, struct file *file)
946 {
947         journal_t *journal = PDE_DATA(inode);
948         struct jbd2_stats_proc_session *s;
949         int rc, size;
950
951         s = kmalloc(sizeof(*s), GFP_KERNEL);
952         if (s == NULL)
953                 return -ENOMEM;
954         size = sizeof(struct transaction_stats_s);
955         s->stats = kmalloc(size, GFP_KERNEL);
956         if (s->stats == NULL) {
957                 kfree(s);
958                 return -ENOMEM;
959         }
960         spin_lock(&journal->j_history_lock);
961         memcpy(s->stats, &journal->j_stats, size);
962         s->journal = journal;
963         spin_unlock(&journal->j_history_lock);
964
965         rc = seq_open(file, &jbd2_seq_info_ops);
966         if (rc == 0) {
967                 struct seq_file *m = file->private_data;
968                 m->private = s;
969         } else {
970                 kfree(s->stats);
971                 kfree(s);
972         }
973         return rc;
974
975 }
976
977 static int jbd2_seq_info_release(struct inode *inode, struct file *file)
978 {
979         struct seq_file *seq = file->private_data;
980         struct jbd2_stats_proc_session *s = seq->private;
981         kfree(s->stats);
982         kfree(s);
983         return seq_release(inode, file);
984 }
985
986 static const struct file_operations jbd2_seq_info_fops = {
987         .owner          = THIS_MODULE,
988         .open           = jbd2_seq_info_open,
989         .read           = seq_read,
990         .llseek         = seq_lseek,
991         .release        = jbd2_seq_info_release,
992 };
993
994 static struct proc_dir_entry *proc_jbd2_stats;
995
996 static void jbd2_stats_proc_init(journal_t *journal)
997 {
998         journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
999         if (journal->j_proc_entry) {
1000                 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
1001                                  &jbd2_seq_info_fops, journal);
1002         }
1003 }
1004
1005 static void jbd2_stats_proc_exit(journal_t *journal)
1006 {
1007         remove_proc_entry("info", journal->j_proc_entry);
1008         remove_proc_entry(journal->j_devname, proc_jbd2_stats);
1009 }
1010
1011 /*
1012  * Management for journal control blocks: functions to create and
1013  * destroy journal_t structures, and to initialise and read existing
1014  * journal blocks from disk.  */
1015
1016 /* First: create and setup a journal_t object in memory.  We initialise
1017  * very few fields yet: that has to wait until we have created the
1018  * journal structures from from scratch, or loaded them from disk. */
1019
1020 static journal_t * journal_init_common (void)
1021 {
1022         journal_t *journal;
1023         int err;
1024
1025         journal = kzalloc(sizeof(*journal), GFP_KERNEL);
1026         if (!journal)
1027                 return NULL;
1028
1029         init_waitqueue_head(&journal->j_wait_transaction_locked);
1030         init_waitqueue_head(&journal->j_wait_logspace);
1031         init_waitqueue_head(&journal->j_wait_done_commit);
1032         init_waitqueue_head(&journal->j_wait_checkpoint);
1033         init_waitqueue_head(&journal->j_wait_commit);
1034         init_waitqueue_head(&journal->j_wait_updates);
1035         mutex_init(&journal->j_barrier);
1036         mutex_init(&journal->j_checkpoint_mutex);
1037         spin_lock_init(&journal->j_revoke_lock);
1038         spin_lock_init(&journal->j_list_lock);
1039         rwlock_init(&journal->j_state_lock);
1040
1041         journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
1042         journal->j_min_batch_time = 0;
1043         journal->j_max_batch_time = 15000; /* 15ms */
1044
1045         /* The journal is marked for error until we succeed with recovery! */
1046         journal->j_flags = JBD2_ABORT;
1047
1048         /* Set up a default-sized revoke table for the new mount. */
1049         err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
1050         if (err) {
1051                 kfree(journal);
1052                 return NULL;
1053         }
1054
1055         spin_lock_init(&journal->j_history_lock);
1056
1057         return journal;
1058 }
1059
1060 /* jbd2_journal_init_dev and jbd2_journal_init_inode:
1061  *
1062  * Create a journal structure assigned some fixed set of disk blocks to
1063  * the journal.  We don't actually touch those disk blocks yet, but we
1064  * need to set up all of the mapping information to tell the journaling
1065  * system where the journal blocks are.
1066  *
1067  */
1068
1069 /**
1070  *  journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
1071  *  @bdev: Block device on which to create the journal
1072  *  @fs_dev: Device which hold journalled filesystem for this journal.
1073  *  @start: Block nr Start of journal.
1074  *  @len:  Length of the journal in blocks.
1075  *  @blocksize: blocksize of journalling device
1076  *
1077  *  Returns: a newly created journal_t *
1078  *
1079  *  jbd2_journal_init_dev creates a journal which maps a fixed contiguous
1080  *  range of blocks on an arbitrary block device.
1081  *
1082  */
1083 journal_t * jbd2_journal_init_dev(struct block_device *bdev,
1084                         struct block_device *fs_dev,
1085                         unsigned long long start, int len, int blocksize)
1086 {
1087         journal_t *journal = journal_init_common();
1088         struct buffer_head *bh;
1089         char *p;
1090         int n;
1091
1092         if (!journal)
1093                 return NULL;
1094
1095         /* journal descriptor can store up to n blocks -bzzz */
1096         journal->j_blocksize = blocksize;
1097         journal->j_dev = bdev;
1098         journal->j_fs_dev = fs_dev;
1099         journal->j_blk_offset = start;
1100         journal->j_maxlen = len;
1101         bdevname(journal->j_dev, journal->j_devname);
1102         p = journal->j_devname;
1103         while ((p = strchr(p, '/')))
1104                 *p = '!';
1105         jbd2_stats_proc_init(journal);
1106         n = journal->j_blocksize / sizeof(journal_block_tag_t);
1107         journal->j_wbufsize = n;
1108         journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1109         if (!journal->j_wbuf) {
1110                 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
1111                         __func__);
1112                 goto out_err;
1113         }
1114
1115         bh = __getblk(journal->j_dev, start, journal->j_blocksize);
1116         if (!bh) {
1117                 printk(KERN_ERR
1118                        "%s: Cannot get buffer for journal superblock\n",
1119                        __func__);
1120                 goto out_err;
1121         }
1122         journal->j_sb_buffer = bh;
1123         journal->j_superblock = (journal_superblock_t *)bh->b_data;
1124
1125         return journal;
1126 out_err:
1127         kfree(journal->j_wbuf);
1128         jbd2_stats_proc_exit(journal);
1129         kfree(journal);
1130         return NULL;
1131 }
1132
1133 /**
1134  *  journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
1135  *  @inode: An inode to create the journal in
1136  *
1137  * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
1138  * the journal.  The inode must exist already, must support bmap() and
1139  * must have all data blocks preallocated.
1140  */
1141 journal_t * jbd2_journal_init_inode (struct inode *inode)
1142 {
1143         struct buffer_head *bh;
1144         journal_t *journal = journal_init_common();
1145         char *p;
1146         int err;
1147         int n;
1148         unsigned long long blocknr;
1149
1150         if (!journal)
1151                 return NULL;
1152
1153         journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1154         journal->j_inode = inode;
1155         bdevname(journal->j_dev, journal->j_devname);
1156         p = journal->j_devname;
1157         while ((p = strchr(p, '/')))
1158                 *p = '!';
1159         p = journal->j_devname + strlen(journal->j_devname);
1160         sprintf(p, "-%lu", journal->j_inode->i_ino);
1161         jbd_debug(1,
1162                   "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1163                   journal, inode->i_sb->s_id, inode->i_ino,
1164                   (long long) inode->i_size,
1165                   inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1166
1167         journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1168         journal->j_blocksize = inode->i_sb->s_blocksize;
1169         jbd2_stats_proc_init(journal);
1170
1171         /* journal descriptor can store up to n blocks -bzzz */
1172         n = journal->j_blocksize / sizeof(journal_block_tag_t);
1173         journal->j_wbufsize = n;
1174         journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1175         if (!journal->j_wbuf) {
1176                 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
1177                         __func__);
1178                 goto out_err;
1179         }
1180
1181         err = jbd2_journal_bmap(journal, 0, &blocknr);
1182         /* If that failed, give up */
1183         if (err) {
1184                 printk(KERN_ERR "%s: Cannot locate journal superblock\n",
1185                        __func__);
1186                 goto out_err;
1187         }
1188
1189         bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
1190         if (!bh) {
1191                 printk(KERN_ERR
1192                        "%s: Cannot get buffer for journal superblock\n",
1193                        __func__);
1194                 goto out_err;
1195         }
1196         journal->j_sb_buffer = bh;
1197         journal->j_superblock = (journal_superblock_t *)bh->b_data;
1198
1199         return journal;
1200 out_err:
1201         kfree(journal->j_wbuf);
1202         jbd2_stats_proc_exit(journal);
1203         kfree(journal);
1204         return NULL;
1205 }
1206
1207 /*
1208  * If the journal init or create aborts, we need to mark the journal
1209  * superblock as being NULL to prevent the journal destroy from writing
1210  * back a bogus superblock.
1211  */
1212 static void journal_fail_superblock (journal_t *journal)
1213 {
1214         struct buffer_head *bh = journal->j_sb_buffer;
1215         brelse(bh);
1216         journal->j_sb_buffer = NULL;
1217 }
1218
1219 /*
1220  * Given a journal_t structure, initialise the various fields for
1221  * startup of a new journaling session.  We use this both when creating
1222  * a journal, and after recovering an old journal to reset it for
1223  * subsequent use.
1224  */
1225
1226 static int journal_reset(journal_t *journal)
1227 {
1228         journal_superblock_t *sb = journal->j_superblock;
1229         unsigned long long first, last;
1230
1231         first = be32_to_cpu(sb->s_first);
1232         last = be32_to_cpu(sb->s_maxlen);
1233         if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
1234                 printk(KERN_ERR "JBD2: Journal too short (blocks %llu-%llu).\n",
1235                        first, last);
1236                 journal_fail_superblock(journal);
1237                 return -EINVAL;
1238         }
1239
1240         journal->j_first = first;
1241         journal->j_last = last;
1242
1243         journal->j_head = first;
1244         journal->j_tail = first;
1245         journal->j_free = last - first;
1246
1247         journal->j_tail_sequence = journal->j_transaction_sequence;
1248         journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1249         journal->j_commit_request = journal->j_commit_sequence;
1250
1251         journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1252
1253         /*
1254          * As a special case, if the on-disk copy is already marked as needing
1255          * no recovery (s_start == 0), then we can safely defer the superblock
1256          * update until the next commit by setting JBD2_FLUSHED.  This avoids
1257          * attempting a write to a potential-readonly device.
1258          */
1259         if (sb->s_start == 0) {
1260                 jbd_debug(1, "JBD2: Skipping superblock update on recovered sb "
1261                         "(start %ld, seq %d, errno %d)\n",
1262                         journal->j_tail, journal->j_tail_sequence,
1263                         journal->j_errno);
1264                 journal->j_flags |= JBD2_FLUSHED;
1265         } else {
1266                 /* Lock here to make assertions happy... */
1267                 mutex_lock(&journal->j_checkpoint_mutex);
1268                 /*
1269                  * Update log tail information. We use WRITE_FUA since new
1270                  * transaction will start reusing journal space and so we
1271                  * must make sure information about current log tail is on
1272                  * disk before that.
1273                  */
1274                 jbd2_journal_update_sb_log_tail(journal,
1275                                                 journal->j_tail_sequence,
1276                                                 journal->j_tail,
1277                                                 WRITE_FUA);
1278                 mutex_unlock(&journal->j_checkpoint_mutex);
1279         }
1280         return jbd2_journal_start_thread(journal);
1281 }
1282
1283 static void jbd2_write_superblock(journal_t *journal, int write_op)
1284 {
1285         struct buffer_head *bh = journal->j_sb_buffer;
1286         int ret;
1287
1288         trace_jbd2_write_superblock(journal, write_op);
1289         if (!(journal->j_flags & JBD2_BARRIER))
1290                 write_op &= ~(REQ_FUA | REQ_FLUSH);
1291         lock_buffer(bh);
1292         if (buffer_write_io_error(bh)) {
1293                 /*
1294                  * Oh, dear.  A previous attempt to write the journal
1295                  * superblock failed.  This could happen because the
1296                  * USB device was yanked out.  Or it could happen to
1297                  * be a transient write error and maybe the block will
1298                  * be remapped.  Nothing we can do but to retry the
1299                  * write and hope for the best.
1300                  */
1301                 printk(KERN_ERR "JBD2: previous I/O error detected "
1302                        "for journal superblock update for %s.\n",
1303                        journal->j_devname);
1304                 clear_buffer_write_io_error(bh);
1305                 set_buffer_uptodate(bh);
1306         }
1307         get_bh(bh);
1308         bh->b_end_io = end_buffer_write_sync;
1309         ret = submit_bh(write_op, bh);
1310         wait_on_buffer(bh);
1311         if (buffer_write_io_error(bh)) {
1312                 clear_buffer_write_io_error(bh);
1313                 set_buffer_uptodate(bh);
1314                 ret = -EIO;
1315         }
1316         if (ret) {
1317                 printk(KERN_ERR "JBD2: Error %d detected when updating "
1318                        "journal superblock for %s.\n", ret,
1319                        journal->j_devname);
1320         }
1321 }
1322
1323 /**
1324  * jbd2_journal_update_sb_log_tail() - Update log tail in journal sb on disk.
1325  * @journal: The journal to update.
1326  * @tail_tid: TID of the new transaction at the tail of the log
1327  * @tail_block: The first block of the transaction at the tail of the log
1328  * @write_op: With which operation should we write the journal sb
1329  *
1330  * Update a journal's superblock information about log tail and write it to
1331  * disk, waiting for the IO to complete.
1332  */
1333 void jbd2_journal_update_sb_log_tail(journal_t *journal, tid_t tail_tid,
1334                                      unsigned long tail_block, int write_op)
1335 {
1336         journal_superblock_t *sb = journal->j_superblock;
1337
1338         BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
1339         jbd_debug(1, "JBD2: updating superblock (start %lu, seq %u)\n",
1340                   tail_block, tail_tid);
1341
1342         sb->s_sequence = cpu_to_be32(tail_tid);
1343         sb->s_start    = cpu_to_be32(tail_block);
1344
1345         jbd2_write_superblock(journal, write_op);
1346
1347         /* Log is no longer empty */
1348         write_lock(&journal->j_state_lock);
1349         WARN_ON(!sb->s_sequence);
1350         journal->j_flags &= ~JBD2_FLUSHED;
1351         write_unlock(&journal->j_state_lock);
1352 }
1353
1354 /**
1355  * jbd2_mark_journal_empty() - Mark on disk journal as empty.
1356  * @journal: The journal to update.
1357  *
1358  * Update a journal's dynamic superblock fields to show that journal is empty.
1359  * Write updated superblock to disk waiting for IO to complete.
1360  */
1361 static void jbd2_mark_journal_empty(journal_t *journal)
1362 {
1363         journal_superblock_t *sb = journal->j_superblock;
1364
1365         BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
1366         read_lock(&journal->j_state_lock);
1367         /* Is it already empty? */
1368         if (sb->s_start == 0) {
1369                 read_unlock(&journal->j_state_lock);
1370                 return;
1371         }
1372         jbd_debug(1, "JBD2: Marking journal as empty (seq %d)\n",
1373                   journal->j_tail_sequence);
1374
1375         sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1376         sb->s_start    = cpu_to_be32(0);
1377         read_unlock(&journal->j_state_lock);
1378
1379         jbd2_write_superblock(journal, WRITE_FUA);
1380
1381         /* Log is no longer empty */
1382         write_lock(&journal->j_state_lock);
1383         journal->j_flags |= JBD2_FLUSHED;
1384         write_unlock(&journal->j_state_lock);
1385 }
1386
1387
1388 /**
1389  * jbd2_journal_update_sb_errno() - Update error in the journal.
1390  * @journal: The journal to update.
1391  *
1392  * Update a journal's errno.  Write updated superblock to disk waiting for IO
1393  * to complete.
1394  */
1395 void jbd2_journal_update_sb_errno(journal_t *journal)
1396 {
1397         journal_superblock_t *sb = journal->j_superblock;
1398
1399         read_lock(&journal->j_state_lock);
1400         jbd_debug(1, "JBD2: updating superblock error (errno %d)\n",
1401                   journal->j_errno);
1402         sb->s_errno    = cpu_to_be32(journal->j_errno);
1403         jbd2_superblock_csum_set(journal, sb);
1404         read_unlock(&journal->j_state_lock);
1405
1406         jbd2_write_superblock(journal, WRITE_SYNC);
1407 }
1408 EXPORT_SYMBOL(jbd2_journal_update_sb_errno);
1409
1410 /*
1411  * Read the superblock for a given journal, performing initial
1412  * validation of the format.
1413  */
1414 static int journal_get_superblock(journal_t *journal)
1415 {
1416         struct buffer_head *bh;
1417         journal_superblock_t *sb;
1418         int err = -EIO;
1419
1420         bh = journal->j_sb_buffer;
1421
1422         J_ASSERT(bh != NULL);
1423         if (!buffer_uptodate(bh)) {
1424                 ll_rw_block(READ, 1, &bh);
1425                 wait_on_buffer(bh);
1426                 if (!buffer_uptodate(bh)) {
1427                         printk(KERN_ERR
1428                                 "JBD2: IO error reading journal superblock\n");
1429                         goto out;
1430                 }
1431         }
1432
1433         if (buffer_verified(bh))
1434                 return 0;
1435
1436         sb = journal->j_superblock;
1437
1438         err = -EINVAL;
1439
1440         if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
1441             sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1442                 printk(KERN_WARNING "JBD2: no valid journal superblock found\n");
1443                 goto out;
1444         }
1445
1446         switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1447         case JBD2_SUPERBLOCK_V1:
1448                 journal->j_format_version = 1;
1449                 break;
1450         case JBD2_SUPERBLOCK_V2:
1451                 journal->j_format_version = 2;
1452                 break;
1453         default:
1454                 printk(KERN_WARNING "JBD2: unrecognised superblock format ID\n");
1455                 goto out;
1456         }
1457
1458         if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1459                 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1460         else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1461                 printk(KERN_WARNING "JBD2: journal file too short\n");
1462                 goto out;
1463         }
1464
1465         if (be32_to_cpu(sb->s_first) == 0 ||
1466             be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
1467                 printk(KERN_WARNING
1468                         "JBD2: Invalid start block of journal: %u\n",
1469                         be32_to_cpu(sb->s_first));
1470                 goto out;
1471         }
1472
1473         if (JBD2_HAS_COMPAT_FEATURE(journal, JBD2_FEATURE_COMPAT_CHECKSUM) &&
1474             JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2)) {
1475                 /* Can't have checksum v1 and v2 on at the same time! */
1476                 printk(KERN_ERR "JBD: Can't enable checksumming v1 and v2 "
1477                        "at the same time!\n");
1478                 goto out;
1479         }
1480
1481         if (!jbd2_verify_csum_type(journal, sb)) {
1482                 printk(KERN_ERR "JBD: Unknown checksum type\n");
1483                 goto out;
1484         }
1485
1486         /* Load the checksum driver */
1487         if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2)) {
1488                 journal->j_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
1489                 if (IS_ERR(journal->j_chksum_driver)) {
1490                         printk(KERN_ERR "JBD: Cannot load crc32c driver.\n");
1491                         err = PTR_ERR(journal->j_chksum_driver);
1492                         journal->j_chksum_driver = NULL;
1493                         goto out;
1494                 }
1495         }
1496
1497         /* Check superblock checksum */
1498         if (!jbd2_superblock_csum_verify(journal, sb)) {
1499                 printk(KERN_ERR "JBD: journal checksum error\n");
1500                 goto out;
1501         }
1502
1503         /* Precompute checksum seed for all metadata */
1504         if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2))
1505                 journal->j_csum_seed = jbd2_chksum(journal, ~0, sb->s_uuid,
1506                                                    sizeof(sb->s_uuid));
1507
1508         set_buffer_verified(bh);
1509
1510         return 0;
1511
1512 out:
1513         journal_fail_superblock(journal);
1514         return err;
1515 }
1516
1517 /*
1518  * Load the on-disk journal superblock and read the key fields into the
1519  * journal_t.
1520  */
1521
1522 static int load_superblock(journal_t *journal)
1523 {
1524         int err;
1525         journal_superblock_t *sb;
1526
1527         err = journal_get_superblock(journal);
1528         if (err)
1529                 return err;
1530
1531         sb = journal->j_superblock;
1532
1533         journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1534         journal->j_tail = be32_to_cpu(sb->s_start);
1535         journal->j_first = be32_to_cpu(sb->s_first);
1536         journal->j_last = be32_to_cpu(sb->s_maxlen);
1537         journal->j_errno = be32_to_cpu(sb->s_errno);
1538
1539         return 0;
1540 }
1541
1542
1543 /**
1544  * int jbd2_journal_load() - Read journal from disk.
1545  * @journal: Journal to act on.
1546  *
1547  * Given a journal_t structure which tells us which disk blocks contain
1548  * a journal, read the journal from disk to initialise the in-memory
1549  * structures.
1550  */
1551 int jbd2_journal_load(journal_t *journal)
1552 {
1553         int err;
1554         journal_superblock_t *sb;
1555
1556         err = load_superblock(journal);
1557         if (err)
1558                 return err;
1559
1560         sb = journal->j_superblock;
1561         /* If this is a V2 superblock, then we have to check the
1562          * features flags on it. */
1563
1564         if (journal->j_format_version >= 2) {
1565                 if ((sb->s_feature_ro_compat &
1566                      ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
1567                     (sb->s_feature_incompat &
1568                      ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
1569                         printk(KERN_WARNING
1570                                 "JBD2: Unrecognised features on journal\n");
1571                         return -EINVAL;
1572                 }
1573         }
1574
1575         /*
1576          * Create a slab for this blocksize
1577          */
1578         err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize));
1579         if (err)
1580                 return err;
1581
1582         /* Let the recovery code check whether it needs to recover any
1583          * data from the journal. */
1584         if (jbd2_journal_recover(journal))
1585                 goto recovery_error;
1586
1587         if (journal->j_failed_commit) {
1588                 printk(KERN_ERR "JBD2: journal transaction %u on %s "
1589                        "is corrupt.\n", journal->j_failed_commit,
1590                        journal->j_devname);
1591                 return -EIO;
1592         }
1593
1594         /* OK, we've finished with the dynamic journal bits:
1595          * reinitialise the dynamic contents of the superblock in memory
1596          * and reset them on disk. */
1597         if (journal_reset(journal))
1598                 goto recovery_error;
1599
1600         journal->j_flags &= ~JBD2_ABORT;
1601         journal->j_flags |= JBD2_LOADED;
1602         return 0;
1603
1604 recovery_error:
1605         printk(KERN_WARNING "JBD2: recovery failed\n");
1606         return -EIO;
1607 }
1608
1609 /**
1610  * void jbd2_journal_destroy() - Release a journal_t structure.
1611  * @journal: Journal to act on.
1612  *
1613  * Release a journal_t structure once it is no longer in use by the
1614  * journaled object.
1615  * Return <0 if we couldn't clean up the journal.
1616  */
1617 int jbd2_journal_destroy(journal_t *journal)
1618 {
1619         int err = 0;
1620
1621         /* Wait for the commit thread to wake up and die. */
1622         journal_kill_thread(journal);
1623
1624         /* Force a final log commit */
1625         if (journal->j_running_transaction)
1626                 jbd2_journal_commit_transaction(journal);
1627
1628         /* Force any old transactions to disk */
1629
1630         /* Totally anal locking here... */
1631         spin_lock(&journal->j_list_lock);
1632         while (journal->j_checkpoint_transactions != NULL) {
1633                 spin_unlock(&journal->j_list_lock);
1634                 mutex_lock(&journal->j_checkpoint_mutex);
1635                 jbd2_log_do_checkpoint(journal);
1636                 mutex_unlock(&journal->j_checkpoint_mutex);
1637                 spin_lock(&journal->j_list_lock);
1638         }
1639
1640         J_ASSERT(journal->j_running_transaction == NULL);
1641         J_ASSERT(journal->j_committing_transaction == NULL);
1642         J_ASSERT(journal->j_checkpoint_transactions == NULL);
1643         spin_unlock(&journal->j_list_lock);
1644
1645         if (journal->j_sb_buffer) {
1646                 if (!is_journal_aborted(journal)) {
1647                         mutex_lock(&journal->j_checkpoint_mutex);
1648                         jbd2_mark_journal_empty(journal);
1649                         mutex_unlock(&journal->j_checkpoint_mutex);
1650                 } else
1651                         err = -EIO;
1652                 brelse(journal->j_sb_buffer);
1653         }
1654
1655         if (journal->j_proc_entry)
1656                 jbd2_stats_proc_exit(journal);
1657         if (journal->j_inode)
1658                 iput(journal->j_inode);
1659         if (journal->j_revoke)
1660                 jbd2_journal_destroy_revoke(journal);
1661         if (journal->j_chksum_driver)
1662                 crypto_free_shash(journal->j_chksum_driver);
1663         kfree(journal->j_wbuf);
1664         kfree(journal);
1665
1666         return err;
1667 }
1668
1669
1670 /**
1671  *int jbd2_journal_check_used_features () - Check if features specified are used.
1672  * @journal: Journal to check.
1673  * @compat: bitmask of compatible features
1674  * @ro: bitmask of features that force read-only mount
1675  * @incompat: bitmask of incompatible features
1676  *
1677  * Check whether the journal uses all of a given set of
1678  * features.  Return true (non-zero) if it does.
1679  **/
1680
1681 int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
1682                                  unsigned long ro, unsigned long incompat)
1683 {
1684         journal_superblock_t *sb;
1685
1686         if (!compat && !ro && !incompat)
1687                 return 1;
1688         /* Load journal superblock if it is not loaded yet. */
1689         if (journal->j_format_version == 0 &&
1690             journal_get_superblock(journal) != 0)
1691                 return 0;
1692         if (journal->j_format_version == 1)
1693                 return 0;
1694
1695         sb = journal->j_superblock;
1696
1697         if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1698             ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1699             ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1700                 return 1;
1701
1702         return 0;
1703 }
1704
1705 /**
1706  * int jbd2_journal_check_available_features() - Check feature set in journalling layer
1707  * @journal: Journal to check.
1708  * @compat: bitmask of compatible features
1709  * @ro: bitmask of features that force read-only mount
1710  * @incompat: bitmask of incompatible features
1711  *
1712  * Check whether the journaling code supports the use of
1713  * all of a given set of features on this journal.  Return true
1714  * (non-zero) if it can. */
1715
1716 int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
1717                                       unsigned long ro, unsigned long incompat)
1718 {
1719         if (!compat && !ro && !incompat)
1720                 return 1;
1721
1722         /* We can support any known requested features iff the
1723          * superblock is in version 2.  Otherwise we fail to support any
1724          * extended sb features. */
1725
1726         if (journal->j_format_version != 2)
1727                 return 0;
1728
1729         if ((compat   & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1730             (ro       & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1731             (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
1732                 return 1;
1733
1734         return 0;
1735 }
1736
1737 /**
1738  * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
1739  * @journal: Journal to act on.
1740  * @compat: bitmask of compatible features
1741  * @ro: bitmask of features that force read-only mount
1742  * @incompat: bitmask of incompatible features
1743  *
1744  * Mark a given journal feature as present on the
1745  * superblock.  Returns true if the requested features could be set.
1746  *
1747  */
1748
1749 int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
1750                           unsigned long ro, unsigned long incompat)
1751 {
1752 #define INCOMPAT_FEATURE_ON(f) \
1753                 ((incompat & (f)) && !(sb->s_feature_incompat & cpu_to_be32(f)))
1754 #define COMPAT_FEATURE_ON(f) \
1755                 ((compat & (f)) && !(sb->s_feature_compat & cpu_to_be32(f)))
1756         journal_superblock_t *sb;
1757
1758         if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
1759                 return 1;
1760
1761         if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
1762                 return 0;
1763
1764         /* Asking for checksumming v2 and v1?  Only give them v2. */
1765         if (incompat & JBD2_FEATURE_INCOMPAT_CSUM_V2 &&
1766             compat & JBD2_FEATURE_COMPAT_CHECKSUM)
1767                 compat &= ~JBD2_FEATURE_COMPAT_CHECKSUM;
1768
1769         jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1770                   compat, ro, incompat);
1771
1772         sb = journal->j_superblock;
1773
1774         /* If enabling v2 checksums, update superblock */
1775         if (INCOMPAT_FEATURE_ON(JBD2_FEATURE_INCOMPAT_CSUM_V2)) {
1776                 sb->s_checksum_type = JBD2_CRC32C_CHKSUM;
1777                 sb->s_feature_compat &=
1778                         ~cpu_to_be32(JBD2_FEATURE_COMPAT_CHECKSUM);
1779
1780                 /* Load the checksum driver */
1781                 if (journal->j_chksum_driver == NULL) {
1782                         journal->j_chksum_driver = crypto_alloc_shash("crc32c",
1783                                                                       0, 0);
1784                         if (IS_ERR(journal->j_chksum_driver)) {
1785                                 printk(KERN_ERR "JBD: Cannot load crc32c "
1786                                        "driver.\n");
1787                                 journal->j_chksum_driver = NULL;
1788                                 return 0;
1789                         }
1790                 }
1791
1792                 /* Precompute checksum seed for all metadata */
1793                 if (JBD2_HAS_INCOMPAT_FEATURE(journal,
1794                                               JBD2_FEATURE_INCOMPAT_CSUM_V2))
1795                         journal->j_csum_seed = jbd2_chksum(journal, ~0,
1796                                                            sb->s_uuid,
1797                                                            sizeof(sb->s_uuid));
1798         }
1799
1800         /* If enabling v1 checksums, downgrade superblock */
1801         if (COMPAT_FEATURE_ON(JBD2_FEATURE_COMPAT_CHECKSUM))
1802                 sb->s_feature_incompat &=
1803                         ~cpu_to_be32(JBD2_FEATURE_INCOMPAT_CSUM_V2);
1804
1805         sb->s_feature_compat    |= cpu_to_be32(compat);
1806         sb->s_feature_ro_compat |= cpu_to_be32(ro);
1807         sb->s_feature_incompat  |= cpu_to_be32(incompat);
1808
1809         return 1;
1810 #undef COMPAT_FEATURE_ON
1811 #undef INCOMPAT_FEATURE_ON
1812 }
1813
1814 /*
1815  * jbd2_journal_clear_features () - Clear a given journal feature in the
1816  *                                  superblock
1817  * @journal: Journal to act on.
1818  * @compat: bitmask of compatible features
1819  * @ro: bitmask of features that force read-only mount
1820  * @incompat: bitmask of incompatible features
1821  *
1822  * Clear a given journal feature as present on the
1823  * superblock.
1824  */
1825 void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1826                                 unsigned long ro, unsigned long incompat)
1827 {
1828         journal_superblock_t *sb;
1829
1830         jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1831                   compat, ro, incompat);
1832
1833         sb = journal->j_superblock;
1834
1835         sb->s_feature_compat    &= ~cpu_to_be32(compat);
1836         sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1837         sb->s_feature_incompat  &= ~cpu_to_be32(incompat);
1838 }
1839 EXPORT_SYMBOL(jbd2_journal_clear_features);
1840
1841 /**
1842  * int jbd2_journal_flush () - Flush journal
1843  * @journal: Journal to act on.
1844  *
1845  * Flush all data for a given journal to disk and empty the journal.
1846  * Filesystems can use this when remounting readonly to ensure that
1847  * recovery does not need to happen on remount.
1848  */
1849
1850 int jbd2_journal_flush(journal_t *journal)
1851 {
1852         int err = 0;
1853         transaction_t *transaction = NULL;
1854
1855         write_lock(&journal->j_state_lock);
1856
1857         /* Force everything buffered to the log... */
1858         if (journal->j_running_transaction) {
1859                 transaction = journal->j_running_transaction;
1860                 __jbd2_log_start_commit(journal, transaction->t_tid);
1861         } else if (journal->j_committing_transaction)
1862                 transaction = journal->j_committing_transaction;
1863
1864         /* Wait for the log commit to complete... */
1865         if (transaction) {
1866                 tid_t tid = transaction->t_tid;
1867
1868                 write_unlock(&journal->j_state_lock);
1869                 jbd2_log_wait_commit(journal, tid);
1870         } else {
1871                 write_unlock(&journal->j_state_lock);
1872         }
1873
1874         /* ...and flush everything in the log out to disk. */
1875         spin_lock(&journal->j_list_lock);
1876         while (!err && journal->j_checkpoint_transactions != NULL) {
1877                 spin_unlock(&journal->j_list_lock);
1878                 mutex_lock(&journal->j_checkpoint_mutex);
1879                 err = jbd2_log_do_checkpoint(journal);
1880                 mutex_unlock(&journal->j_checkpoint_mutex);
1881                 spin_lock(&journal->j_list_lock);
1882         }
1883         spin_unlock(&journal->j_list_lock);
1884
1885         if (is_journal_aborted(journal))
1886                 return -EIO;
1887
1888         mutex_lock(&journal->j_checkpoint_mutex);
1889         jbd2_cleanup_journal_tail(journal);
1890
1891         /* Finally, mark the journal as really needing no recovery.
1892          * This sets s_start==0 in the underlying superblock, which is
1893          * the magic code for a fully-recovered superblock.  Any future
1894          * commits of data to the journal will restore the current
1895          * s_start value. */
1896         jbd2_mark_journal_empty(journal);
1897         mutex_unlock(&journal->j_checkpoint_mutex);
1898         write_lock(&journal->j_state_lock);
1899         J_ASSERT(!journal->j_running_transaction);
1900         J_ASSERT(!journal->j_committing_transaction);
1901         J_ASSERT(!journal->j_checkpoint_transactions);
1902         J_ASSERT(journal->j_head == journal->j_tail);
1903         J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1904         write_unlock(&journal->j_state_lock);
1905         return 0;
1906 }
1907
1908 /**
1909  * int jbd2_journal_wipe() - Wipe journal contents
1910  * @journal: Journal to act on.
1911  * @write: flag (see below)
1912  *
1913  * Wipe out all of the contents of a journal, safely.  This will produce
1914  * a warning if the journal contains any valid recovery information.
1915  * Must be called between journal_init_*() and jbd2_journal_load().
1916  *
1917  * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1918  * we merely suppress recovery.
1919  */
1920
1921 int jbd2_journal_wipe(journal_t *journal, int write)
1922 {
1923         int err = 0;
1924
1925         J_ASSERT (!(journal->j_flags & JBD2_LOADED));
1926
1927         err = load_superblock(journal);
1928         if (err)
1929                 return err;
1930
1931         if (!journal->j_tail)
1932                 goto no_recovery;
1933
1934         printk(KERN_WARNING "JBD2: %s recovery information on journal\n",
1935                 write ? "Clearing" : "Ignoring");
1936
1937         err = jbd2_journal_skip_recovery(journal);
1938         if (write) {
1939                 /* Lock to make assertions happy... */
1940                 mutex_lock(&journal->j_checkpoint_mutex);
1941                 jbd2_mark_journal_empty(journal);
1942                 mutex_unlock(&journal->j_checkpoint_mutex);
1943         }
1944
1945  no_recovery:
1946         return err;
1947 }
1948
1949 /*
1950  * Journal abort has very specific semantics, which we describe
1951  * for journal abort.
1952  *
1953  * Two internal functions, which provide abort to the jbd layer
1954  * itself are here.
1955  */
1956
1957 /*
1958  * Quick version for internal journal use (doesn't lock the journal).
1959  * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1960  * and don't attempt to make any other journal updates.
1961  */
1962 void __jbd2_journal_abort_hard(journal_t *journal)
1963 {
1964         transaction_t *transaction;
1965
1966         if (journal->j_flags & JBD2_ABORT)
1967                 return;
1968
1969         printk(KERN_ERR "Aborting journal on device %s.\n",
1970                journal->j_devname);
1971
1972         write_lock(&journal->j_state_lock);
1973         journal->j_flags |= JBD2_ABORT;
1974         transaction = journal->j_running_transaction;
1975         if (transaction)
1976                 __jbd2_log_start_commit(journal, transaction->t_tid);
1977         write_unlock(&journal->j_state_lock);
1978 }
1979
1980 /* Soft abort: record the abort error status in the journal superblock,
1981  * but don't do any other IO. */
1982 static void __journal_abort_soft (journal_t *journal, int errno)
1983 {
1984         if (journal->j_flags & JBD2_ABORT)
1985                 return;
1986
1987         if (!journal->j_errno)
1988                 journal->j_errno = errno;
1989
1990         __jbd2_journal_abort_hard(journal);
1991
1992         if (errno)
1993                 jbd2_journal_update_sb_errno(journal);
1994 }
1995
1996 /**
1997  * void jbd2_journal_abort () - Shutdown the journal immediately.
1998  * @journal: the journal to shutdown.
1999  * @errno:   an error number to record in the journal indicating
2000  *           the reason for the shutdown.
2001  *
2002  * Perform a complete, immediate shutdown of the ENTIRE
2003  * journal (not of a single transaction).  This operation cannot be
2004  * undone without closing and reopening the journal.
2005  *
2006  * The jbd2_journal_abort function is intended to support higher level error
2007  * recovery mechanisms such as the ext2/ext3 remount-readonly error
2008  * mode.
2009  *
2010  * Journal abort has very specific semantics.  Any existing dirty,
2011  * unjournaled buffers in the main filesystem will still be written to
2012  * disk by bdflush, but the journaling mechanism will be suspended
2013  * immediately and no further transaction commits will be honoured.
2014  *
2015  * Any dirty, journaled buffers will be written back to disk without
2016  * hitting the journal.  Atomicity cannot be guaranteed on an aborted
2017  * filesystem, but we _do_ attempt to leave as much data as possible
2018  * behind for fsck to use for cleanup.
2019  *
2020  * Any attempt to get a new transaction handle on a journal which is in
2021  * ABORT state will just result in an -EROFS error return.  A
2022  * jbd2_journal_stop on an existing handle will return -EIO if we have
2023  * entered abort state during the update.
2024  *
2025  * Recursive transactions are not disturbed by journal abort until the
2026  * final jbd2_journal_stop, which will receive the -EIO error.
2027  *
2028  * Finally, the jbd2_journal_abort call allows the caller to supply an errno
2029  * which will be recorded (if possible) in the journal superblock.  This
2030  * allows a client to record failure conditions in the middle of a
2031  * transaction without having to complete the transaction to record the
2032  * failure to disk.  ext3_error, for example, now uses this
2033  * functionality.
2034  *
2035  * Errors which originate from within the journaling layer will NOT
2036  * supply an errno; a null errno implies that absolutely no further
2037  * writes are done to the journal (unless there are any already in
2038  * progress).
2039  *
2040  */
2041
2042 void jbd2_journal_abort(journal_t *journal, int errno)
2043 {
2044         __journal_abort_soft(journal, errno);
2045 }
2046
2047 /**
2048  * int jbd2_journal_errno () - returns the journal's error state.
2049  * @journal: journal to examine.
2050  *
2051  * This is the errno number set with jbd2_journal_abort(), the last
2052  * time the journal was mounted - if the journal was stopped
2053  * without calling abort this will be 0.
2054  *
2055  * If the journal has been aborted on this mount time -EROFS will
2056  * be returned.
2057  */
2058 int jbd2_journal_errno(journal_t *journal)
2059 {
2060         int err;
2061
2062         read_lock(&journal->j_state_lock);
2063         if (journal->j_flags & JBD2_ABORT)
2064                 err = -EROFS;
2065         else
2066                 err = journal->j_errno;
2067         read_unlock(&journal->j_state_lock);
2068         return err;
2069 }
2070
2071 /**
2072  * int jbd2_journal_clear_err () - clears the journal's error state
2073  * @journal: journal to act on.
2074  *
2075  * An error must be cleared or acked to take a FS out of readonly
2076  * mode.
2077  */
2078 int jbd2_journal_clear_err(journal_t *journal)
2079 {
2080         int err = 0;
2081
2082         write_lock(&journal->j_state_lock);
2083         if (journal->j_flags & JBD2_ABORT)
2084                 err = -EROFS;
2085         else
2086                 journal->j_errno = 0;
2087         write_unlock(&journal->j_state_lock);
2088         return err;
2089 }
2090
2091 /**
2092  * void jbd2_journal_ack_err() - Ack journal err.
2093  * @journal: journal to act on.
2094  *
2095  * An error must be cleared or acked to take a FS out of readonly
2096  * mode.
2097  */
2098 void jbd2_journal_ack_err(journal_t *journal)
2099 {
2100         write_lock(&journal->j_state_lock);
2101         if (journal->j_errno)
2102                 journal->j_flags |= JBD2_ACK_ERR;
2103         write_unlock(&journal->j_state_lock);
2104 }
2105
2106 int jbd2_journal_blocks_per_page(struct inode *inode)
2107 {
2108         return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
2109 }
2110
2111 /*
2112  * helper functions to deal with 32 or 64bit block numbers.
2113  */
2114 size_t journal_tag_bytes(journal_t *journal)
2115 {
2116         journal_block_tag_t tag;
2117         size_t x = 0;
2118
2119         if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2))
2120                 x += sizeof(tag.t_checksum);
2121
2122         if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
2123                 return x + JBD2_TAG_SIZE64;
2124         else
2125                 return x + JBD2_TAG_SIZE32;
2126 }
2127
2128 /*
2129  * JBD memory management
2130  *
2131  * These functions are used to allocate block-sized chunks of memory
2132  * used for making copies of buffer_head data.  Very often it will be
2133  * page-sized chunks of data, but sometimes it will be in
2134  * sub-page-size chunks.  (For example, 16k pages on Power systems
2135  * with a 4k block file system.)  For blocks smaller than a page, we
2136  * use a SLAB allocator.  There are slab caches for each block size,
2137  * which are allocated at mount time, if necessary, and we only free
2138  * (all of) the slab caches when/if the jbd2 module is unloaded.  For
2139  * this reason we don't need to a mutex to protect access to
2140  * jbd2_slab[] allocating or releasing memory; only in
2141  * jbd2_journal_create_slab().
2142  */
2143 #define JBD2_MAX_SLABS 8
2144 static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS];
2145
2146 static const char *jbd2_slab_names[JBD2_MAX_SLABS] = {
2147         "jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k",
2148         "jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k"
2149 };
2150
2151
2152 static void jbd2_journal_destroy_slabs(void)
2153 {
2154         int i;
2155
2156         for (i = 0; i < JBD2_MAX_SLABS; i++) {
2157                 if (jbd2_slab[i])
2158                         kmem_cache_destroy(jbd2_slab[i]);
2159                 jbd2_slab[i] = NULL;
2160         }
2161 }
2162
2163 static int jbd2_journal_create_slab(size_t size)
2164 {
2165         static DEFINE_MUTEX(jbd2_slab_create_mutex);
2166         int i = order_base_2(size) - 10;
2167         size_t slab_size;
2168
2169         if (size == PAGE_SIZE)
2170                 return 0;
2171
2172         if (i >= JBD2_MAX_SLABS)
2173                 return -EINVAL;
2174
2175         if (unlikely(i < 0))
2176                 i = 0;
2177         mutex_lock(&jbd2_slab_create_mutex);
2178         if (jbd2_slab[i]) {
2179                 mutex_unlock(&jbd2_slab_create_mutex);
2180                 return 0;       /* Already created */
2181         }
2182
2183         slab_size = 1 << (i+10);
2184         jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size,
2185                                          slab_size, 0, NULL);
2186         mutex_unlock(&jbd2_slab_create_mutex);
2187         if (!jbd2_slab[i]) {
2188                 printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n");
2189                 return -ENOMEM;
2190         }
2191         return 0;
2192 }
2193
2194 static struct kmem_cache *get_slab(size_t size)
2195 {
2196         int i = order_base_2(size) - 10;
2197
2198         BUG_ON(i >= JBD2_MAX_SLABS);
2199         if (unlikely(i < 0))
2200                 i = 0;
2201         BUG_ON(jbd2_slab[i] == NULL);
2202         return jbd2_slab[i];
2203 }
2204
2205 void *jbd2_alloc(size_t size, gfp_t flags)
2206 {
2207         void *ptr;
2208
2209         BUG_ON(size & (size-1)); /* Must be a power of 2 */
2210
2211         flags |= __GFP_REPEAT;
2212         if (size == PAGE_SIZE)
2213                 ptr = (void *)__get_free_pages(flags, 0);
2214         else if (size > PAGE_SIZE) {
2215                 int order = get_order(size);
2216
2217                 if (order < 3)
2218                         ptr = (void *)__get_free_pages(flags, order);
2219                 else
2220                         ptr = vmalloc(size);
2221         } else
2222                 ptr = kmem_cache_alloc(get_slab(size), flags);
2223
2224         /* Check alignment; SLUB has gotten this wrong in the past,
2225          * and this can lead to user data corruption! */
2226         BUG_ON(((unsigned long) ptr) & (size-1));
2227
2228         return ptr;
2229 }
2230
2231 void jbd2_free(void *ptr, size_t size)
2232 {
2233         if (size == PAGE_SIZE) {
2234                 free_pages((unsigned long)ptr, 0);
2235                 return;
2236         }
2237         if (size > PAGE_SIZE) {
2238                 int order = get_order(size);
2239
2240                 if (order < 3)
2241                         free_pages((unsigned long)ptr, order);
2242                 else
2243                         vfree(ptr);
2244                 return;
2245         }
2246         kmem_cache_free(get_slab(size), ptr);
2247 };
2248
2249 /*
2250  * Journal_head storage management
2251  */
2252 static struct kmem_cache *jbd2_journal_head_cache;
2253 #ifdef CONFIG_JBD2_DEBUG
2254 static atomic_t nr_journal_heads = ATOMIC_INIT(0);
2255 #endif
2256
2257 static int jbd2_journal_init_journal_head_cache(void)
2258 {
2259         int retval;
2260
2261         J_ASSERT(jbd2_journal_head_cache == NULL);
2262         jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
2263                                 sizeof(struct journal_head),
2264                                 0,              /* offset */
2265                                 SLAB_TEMPORARY, /* flags */
2266                                 NULL);          /* ctor */
2267         retval = 0;
2268         if (!jbd2_journal_head_cache) {
2269                 retval = -ENOMEM;
2270                 printk(KERN_EMERG "JBD2: no memory for journal_head cache\n");
2271         }
2272         return retval;
2273 }
2274
2275 static void jbd2_journal_destroy_journal_head_cache(void)
2276 {
2277         if (jbd2_journal_head_cache) {
2278                 kmem_cache_destroy(jbd2_journal_head_cache);
2279                 jbd2_journal_head_cache = NULL;
2280         }
2281 }
2282
2283 /*
2284  * journal_head splicing and dicing
2285  */
2286 static struct journal_head *journal_alloc_journal_head(void)
2287 {
2288         struct journal_head *ret;
2289
2290 #ifdef CONFIG_JBD2_DEBUG
2291         atomic_inc(&nr_journal_heads);
2292 #endif
2293         ret = kmem_cache_zalloc(jbd2_journal_head_cache, GFP_NOFS);
2294         if (!ret) {
2295                 jbd_debug(1, "out of memory for journal_head\n");
2296                 pr_notice_ratelimited("ENOMEM in %s, retrying.\n", __func__);
2297                 while (!ret) {
2298                         yield();
2299                         ret = kmem_cache_zalloc(jbd2_journal_head_cache, GFP_NOFS);
2300                 }
2301         }
2302         return ret;
2303 }
2304
2305 static void journal_free_journal_head(struct journal_head *jh)
2306 {
2307 #ifdef CONFIG_JBD2_DEBUG
2308         atomic_dec(&nr_journal_heads);
2309         memset(jh, JBD2_POISON_FREE, sizeof(*jh));
2310 #endif
2311         kmem_cache_free(jbd2_journal_head_cache, jh);
2312 }
2313
2314 /*
2315  * A journal_head is attached to a buffer_head whenever JBD has an
2316  * interest in the buffer.
2317  *
2318  * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2319  * is set.  This bit is tested in core kernel code where we need to take
2320  * JBD-specific actions.  Testing the zeroness of ->b_private is not reliable
2321  * there.
2322  *
2323  * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2324  *
2325  * When a buffer has its BH_JBD bit set it is immune from being released by
2326  * core kernel code, mainly via ->b_count.
2327  *
2328  * A journal_head is detached from its buffer_head when the journal_head's
2329  * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint
2330  * transaction (b_cp_transaction) hold their references to b_jcount.
2331  *
2332  * Various places in the kernel want to attach a journal_head to a buffer_head
2333  * _before_ attaching the journal_head to a transaction.  To protect the
2334  * journal_head in this situation, jbd2_journal_add_journal_head elevates the
2335  * journal_head's b_jcount refcount by one.  The caller must call
2336  * jbd2_journal_put_journal_head() to undo this.
2337  *
2338  * So the typical usage would be:
2339  *
2340  *      (Attach a journal_head if needed.  Increments b_jcount)
2341  *      struct journal_head *jh = jbd2_journal_add_journal_head(bh);
2342  *      ...
2343  *      (Get another reference for transaction)
2344  *      jbd2_journal_grab_journal_head(bh);
2345  *      jh->b_transaction = xxx;
2346  *      (Put original reference)
2347  *      jbd2_journal_put_journal_head(jh);
2348  */
2349
2350 /*
2351  * Give a buffer_head a journal_head.
2352  *
2353  * May sleep.
2354  */
2355 struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
2356 {
2357         struct journal_head *jh;
2358         struct journal_head *new_jh = NULL;
2359
2360 repeat:
2361         if (!buffer_jbd(bh))
2362                 new_jh = journal_alloc_journal_head();
2363
2364         jbd_lock_bh_journal_head(bh);
2365         if (buffer_jbd(bh)) {
2366                 jh = bh2jh(bh);
2367         } else {
2368                 J_ASSERT_BH(bh,
2369                         (atomic_read(&bh->b_count) > 0) ||
2370                         (bh->b_page && bh->b_page->mapping));
2371
2372                 if (!new_jh) {
2373                         jbd_unlock_bh_journal_head(bh);
2374                         goto repeat;
2375                 }
2376
2377                 jh = new_jh;
2378                 new_jh = NULL;          /* We consumed it */
2379                 set_buffer_jbd(bh);
2380                 bh->b_private = jh;
2381                 jh->b_bh = bh;
2382                 get_bh(bh);
2383                 BUFFER_TRACE(bh, "added journal_head");
2384         }
2385         jh->b_jcount++;
2386         jbd_unlock_bh_journal_head(bh);
2387         if (new_jh)
2388                 journal_free_journal_head(new_jh);
2389         return bh->b_private;
2390 }
2391
2392 /*
2393  * Grab a ref against this buffer_head's journal_head.  If it ended up not
2394  * having a journal_head, return NULL
2395  */
2396 struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
2397 {
2398         struct journal_head *jh = NULL;
2399
2400         jbd_lock_bh_journal_head(bh);
2401         if (buffer_jbd(bh)) {
2402                 jh = bh2jh(bh);
2403                 jh->b_jcount++;
2404         }
2405         jbd_unlock_bh_journal_head(bh);
2406         return jh;
2407 }
2408
2409 static void __journal_remove_journal_head(struct buffer_head *bh)
2410 {
2411         struct journal_head *jh = bh2jh(bh);
2412
2413         J_ASSERT_JH(jh, jh->b_jcount >= 0);
2414         J_ASSERT_JH(jh, jh->b_transaction == NULL);
2415         J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
2416         J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
2417         J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2418         J_ASSERT_BH(bh, buffer_jbd(bh));
2419         J_ASSERT_BH(bh, jh2bh(jh) == bh);
2420         BUFFER_TRACE(bh, "remove journal_head");
2421         if (jh->b_frozen_data) {
2422                 printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
2423                 jbd2_free(jh->b_frozen_data, bh->b_size);
2424         }
2425         if (jh->b_committed_data) {
2426                 printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
2427                 jbd2_free(jh->b_committed_data, bh->b_size);
2428         }
2429         bh->b_private = NULL;
2430         jh->b_bh = NULL;        /* debug, really */
2431         clear_buffer_jbd(bh);
2432         journal_free_journal_head(jh);
2433 }
2434
2435 /*
2436  * Drop a reference on the passed journal_head.  If it fell to zero then
2437  * release the journal_head from the buffer_head.
2438  */
2439 void jbd2_journal_put_journal_head(struct journal_head *jh)
2440 {
2441         struct buffer_head *bh = jh2bh(jh);
2442
2443         jbd_lock_bh_journal_head(bh);
2444         J_ASSERT_JH(jh, jh->b_jcount > 0);
2445         --jh->b_jcount;
2446         if (!jh->b_jcount) {
2447                 __journal_remove_journal_head(bh);
2448                 jbd_unlock_bh_journal_head(bh);
2449                 __brelse(bh);
2450         } else
2451                 jbd_unlock_bh_journal_head(bh);
2452 }
2453
2454 /*
2455  * Initialize jbd inode head
2456  */
2457 void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2458 {
2459         jinode->i_transaction = NULL;
2460         jinode->i_next_transaction = NULL;
2461         jinode->i_vfs_inode = inode;
2462         jinode->i_flags = 0;
2463         INIT_LIST_HEAD(&jinode->i_list);
2464 }
2465
2466 /*
2467  * Function to be called before we start removing inode from memory (i.e.,
2468  * clear_inode() is a fine place to be called from). It removes inode from
2469  * transaction's lists.
2470  */
2471 void jbd2_journal_release_jbd_inode(journal_t *journal,
2472                                     struct jbd2_inode *jinode)
2473 {
2474         if (!journal)
2475                 return;
2476 restart:
2477         spin_lock(&journal->j_list_lock);
2478         /* Is commit writing out inode - we have to wait */
2479         if (test_bit(__JI_COMMIT_RUNNING, &jinode->i_flags)) {
2480                 wait_queue_head_t *wq;
2481                 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2482                 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2483                 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2484                 spin_unlock(&journal->j_list_lock);
2485                 schedule();
2486                 finish_wait(wq, &wait.wait);
2487                 goto restart;
2488         }
2489
2490         if (jinode->i_transaction) {
2491                 list_del(&jinode->i_list);
2492                 jinode->i_transaction = NULL;
2493         }
2494         spin_unlock(&journal->j_list_lock);
2495 }
2496
2497
2498 #ifdef CONFIG_PROC_FS
2499
2500 #define JBD2_STATS_PROC_NAME "fs/jbd2"
2501
2502 static void __init jbd2_create_jbd_stats_proc_entry(void)
2503 {
2504         proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2505 }
2506
2507 static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2508 {
2509         if (proc_jbd2_stats)
2510                 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2511 }
2512
2513 #else
2514
2515 #define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2516 #define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2517
2518 #endif
2519
2520 struct kmem_cache *jbd2_handle_cache, *jbd2_inode_cache;
2521
2522 static int __init jbd2_journal_init_handle_cache(void)
2523 {
2524         jbd2_handle_cache = KMEM_CACHE(jbd2_journal_handle, SLAB_TEMPORARY);
2525         if (jbd2_handle_cache == NULL) {
2526                 printk(KERN_EMERG "JBD2: failed to create handle cache\n");
2527                 return -ENOMEM;
2528         }
2529         jbd2_inode_cache = KMEM_CACHE(jbd2_inode, 0);
2530         if (jbd2_inode_cache == NULL) {
2531                 printk(KERN_EMERG "JBD2: failed to create inode cache\n");
2532                 kmem_cache_destroy(jbd2_handle_cache);
2533                 return -ENOMEM;
2534         }
2535         return 0;
2536 }
2537
2538 static void jbd2_journal_destroy_handle_cache(void)
2539 {
2540         if (jbd2_handle_cache)
2541                 kmem_cache_destroy(jbd2_handle_cache);
2542         if (jbd2_inode_cache)
2543                 kmem_cache_destroy(jbd2_inode_cache);
2544
2545 }
2546
2547 /*
2548  * Module startup and shutdown
2549  */
2550
2551 static int __init journal_init_caches(void)
2552 {
2553         int ret;
2554
2555         ret = jbd2_journal_init_revoke_caches();
2556         if (ret == 0)
2557                 ret = jbd2_journal_init_journal_head_cache();
2558         if (ret == 0)
2559                 ret = jbd2_journal_init_handle_cache();
2560         if (ret == 0)
2561                 ret = jbd2_journal_init_transaction_cache();
2562         return ret;
2563 }
2564
2565 static void jbd2_journal_destroy_caches(void)
2566 {
2567         jbd2_journal_destroy_revoke_caches();
2568         jbd2_journal_destroy_journal_head_cache();
2569         jbd2_journal_destroy_handle_cache();
2570         jbd2_journal_destroy_transaction_cache();
2571         jbd2_journal_destroy_slabs();
2572 }
2573
2574 static int __init journal_init(void)
2575 {
2576         int ret;
2577
2578         BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2579
2580         ret = journal_init_caches();
2581         if (ret == 0) {
2582                 jbd2_create_jbd_stats_proc_entry();
2583         } else {
2584                 jbd2_journal_destroy_caches();
2585         }
2586         return ret;
2587 }
2588
2589 static void __exit journal_exit(void)
2590 {
2591 #ifdef CONFIG_JBD2_DEBUG
2592         int n = atomic_read(&nr_journal_heads);
2593         if (n)
2594                 printk(KERN_EMERG "JBD2: leaked %d journal_heads!\n", n);
2595 #endif
2596         jbd2_remove_jbd_stats_proc_entry();
2597         jbd2_journal_destroy_caches();
2598 }
2599
2600 MODULE_LICENSE("GPL");
2601 module_init(journal_init);
2602 module_exit(journal_exit);
2603