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