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