]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/jbd/journal.c
Merge tag 'for-v3.10' of git://git.infradead.org/users/cbou/linux-pstore
[karo-tx-linux.git] / fs / jbd / journal.c
1 /*
2  * linux/fs/jbd/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/jbd.h>
29 #include <linux/errno.h>
30 #include <linux/slab.h>
31 #include <linux/init.h>
32 #include <linux/mm.h>
33 #include <linux/freezer.h>
34 #include <linux/pagemap.h>
35 #include <linux/kthread.h>
36 #include <linux/poison.h>
37 #include <linux/proc_fs.h>
38 #include <linux/debugfs.h>
39 #include <linux/ratelimit.h>
40
41 #define CREATE_TRACE_POINTS
42 #include <trace/events/jbd.h>
43
44 #include <asm/uaccess.h>
45 #include <asm/page.h>
46
47 EXPORT_SYMBOL(journal_start);
48 EXPORT_SYMBOL(journal_restart);
49 EXPORT_SYMBOL(journal_extend);
50 EXPORT_SYMBOL(journal_stop);
51 EXPORT_SYMBOL(journal_lock_updates);
52 EXPORT_SYMBOL(journal_unlock_updates);
53 EXPORT_SYMBOL(journal_get_write_access);
54 EXPORT_SYMBOL(journal_get_create_access);
55 EXPORT_SYMBOL(journal_get_undo_access);
56 EXPORT_SYMBOL(journal_dirty_data);
57 EXPORT_SYMBOL(journal_dirty_metadata);
58 EXPORT_SYMBOL(journal_release_buffer);
59 EXPORT_SYMBOL(journal_forget);
60 #if 0
61 EXPORT_SYMBOL(journal_sync_buffer);
62 #endif
63 EXPORT_SYMBOL(journal_flush);
64 EXPORT_SYMBOL(journal_revoke);
65
66 EXPORT_SYMBOL(journal_init_dev);
67 EXPORT_SYMBOL(journal_init_inode);
68 EXPORT_SYMBOL(journal_update_format);
69 EXPORT_SYMBOL(journal_check_used_features);
70 EXPORT_SYMBOL(journal_check_available_features);
71 EXPORT_SYMBOL(journal_set_features);
72 EXPORT_SYMBOL(journal_create);
73 EXPORT_SYMBOL(journal_load);
74 EXPORT_SYMBOL(journal_destroy);
75 EXPORT_SYMBOL(journal_abort);
76 EXPORT_SYMBOL(journal_errno);
77 EXPORT_SYMBOL(journal_ack_err);
78 EXPORT_SYMBOL(journal_clear_err);
79 EXPORT_SYMBOL(log_wait_commit);
80 EXPORT_SYMBOL(log_start_commit);
81 EXPORT_SYMBOL(journal_start_commit);
82 EXPORT_SYMBOL(journal_force_commit_nested);
83 EXPORT_SYMBOL(journal_wipe);
84 EXPORT_SYMBOL(journal_blocks_per_page);
85 EXPORT_SYMBOL(journal_invalidatepage);
86 EXPORT_SYMBOL(journal_try_to_free_buffers);
87 EXPORT_SYMBOL(journal_force_commit);
88
89 static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
90 static void __journal_abort_soft (journal_t *journal, int errno);
91 static const char *journal_dev_name(journal_t *journal, char *buffer);
92
93 /*
94  * Helper function used to manage commit timeouts
95  */
96
97 static void commit_timeout(unsigned long __data)
98 {
99         struct task_struct * p = (struct task_struct *) __data;
100
101         wake_up_process(p);
102 }
103
104 /*
105  * kjournald: The main thread function used to manage a logging device
106  * journal.
107  *
108  * This kernel thread is responsible for two things:
109  *
110  * 1) COMMIT:  Every so often we need to commit the current state of the
111  *    filesystem to disk.  The journal thread is responsible for writing
112  *    all of the metadata buffers to disk.
113  *
114  * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
115  *    of the data in that part of the log has been rewritten elsewhere on
116  *    the disk.  Flushing these old buffers to reclaim space in the log is
117  *    known as checkpointing, and this thread is responsible for that job.
118  */
119
120 static int kjournald(void *arg)
121 {
122         journal_t *journal = arg;
123         transaction_t *transaction;
124
125         /*
126          * Set up an interval timer which can be used to trigger a commit wakeup
127          * after the commit interval expires
128          */
129         setup_timer(&journal->j_commit_timer, commit_timeout,
130                         (unsigned long)current);
131
132         set_freezable();
133
134         /* Record that the journal thread is running */
135         journal->j_task = current;
136         wake_up(&journal->j_wait_done_commit);
137
138         printk(KERN_INFO "kjournald starting.  Commit interval %ld seconds\n",
139                         journal->j_commit_interval / HZ);
140
141         /*
142          * And now, wait forever for commit wakeup events.
143          */
144         spin_lock(&journal->j_state_lock);
145
146 loop:
147         if (journal->j_flags & JFS_UNMOUNT)
148                 goto end_loop;
149
150         jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
151                 journal->j_commit_sequence, journal->j_commit_request);
152
153         if (journal->j_commit_sequence != journal->j_commit_request) {
154                 jbd_debug(1, "OK, requests differ\n");
155                 spin_unlock(&journal->j_state_lock);
156                 del_timer_sync(&journal->j_commit_timer);
157                 journal_commit_transaction(journal);
158                 spin_lock(&journal->j_state_lock);
159                 goto loop;
160         }
161
162         wake_up(&journal->j_wait_done_commit);
163         if (freezing(current)) {
164                 /*
165                  * The simpler the better. Flushing journal isn't a
166                  * good idea, because that depends on threads that may
167                  * be already stopped.
168                  */
169                 jbd_debug(1, "Now suspending kjournald\n");
170                 spin_unlock(&journal->j_state_lock);
171                 try_to_freeze();
172                 spin_lock(&journal->j_state_lock);
173         } else {
174                 /*
175                  * We assume on resume that commits are already there,
176                  * so we don't sleep
177                  */
178                 DEFINE_WAIT(wait);
179                 int should_sleep = 1;
180
181                 prepare_to_wait(&journal->j_wait_commit, &wait,
182                                 TASK_INTERRUPTIBLE);
183                 if (journal->j_commit_sequence != journal->j_commit_request)
184                         should_sleep = 0;
185                 transaction = journal->j_running_transaction;
186                 if (transaction && time_after_eq(jiffies,
187                                                 transaction->t_expires))
188                         should_sleep = 0;
189                 if (journal->j_flags & JFS_UNMOUNT)
190                         should_sleep = 0;
191                 if (should_sleep) {
192                         spin_unlock(&journal->j_state_lock);
193                         schedule();
194                         spin_lock(&journal->j_state_lock);
195                 }
196                 finish_wait(&journal->j_wait_commit, &wait);
197         }
198
199         jbd_debug(1, "kjournald wakes\n");
200
201         /*
202          * Were we woken up by a commit wakeup event?
203          */
204         transaction = journal->j_running_transaction;
205         if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
206                 journal->j_commit_request = transaction->t_tid;
207                 jbd_debug(1, "woke because of timeout\n");
208         }
209         goto loop;
210
211 end_loop:
212         spin_unlock(&journal->j_state_lock);
213         del_timer_sync(&journal->j_commit_timer);
214         journal->j_task = NULL;
215         wake_up(&journal->j_wait_done_commit);
216         jbd_debug(1, "Journal thread exiting.\n");
217         return 0;
218 }
219
220 static int journal_start_thread(journal_t *journal)
221 {
222         struct task_struct *t;
223
224         t = kthread_run(kjournald, journal, "kjournald");
225         if (IS_ERR(t))
226                 return PTR_ERR(t);
227
228         wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
229         return 0;
230 }
231
232 static void journal_kill_thread(journal_t *journal)
233 {
234         spin_lock(&journal->j_state_lock);
235         journal->j_flags |= JFS_UNMOUNT;
236
237         while (journal->j_task) {
238                 wake_up(&journal->j_wait_commit);
239                 spin_unlock(&journal->j_state_lock);
240                 wait_event(journal->j_wait_done_commit,
241                                 journal->j_task == NULL);
242                 spin_lock(&journal->j_state_lock);
243         }
244         spin_unlock(&journal->j_state_lock);
245 }
246
247 /*
248  * journal_write_metadata_buffer: write a metadata buffer to the journal.
249  *
250  * Writes a metadata buffer to a given disk block.  The actual IO is not
251  * performed but a new buffer_head is constructed which labels the data
252  * to be written with the correct destination disk block.
253  *
254  * Any magic-number escaping which needs to be done will cause a
255  * copy-out here.  If the buffer happens to start with the
256  * JFS_MAGIC_NUMBER, then we can't write it to the log directly: the
257  * magic number is only written to the log for descripter blocks.  In
258  * this case, we copy the data and replace the first word with 0, and we
259  * return a result code which indicates that this buffer needs to be
260  * marked as an escaped buffer in the corresponding log descriptor
261  * block.  The missing word can then be restored when the block is read
262  * during recovery.
263  *
264  * If the source buffer has already been modified by a new transaction
265  * since we took the last commit snapshot, we use the frozen copy of
266  * that data for IO.  If we end up using the existing buffer_head's data
267  * for the write, then we *have* to lock the buffer to prevent anyone
268  * else from using and possibly modifying it while the IO is in
269  * progress.
270  *
271  * The function returns a pointer to the buffer_heads to be used for IO.
272  *
273  * We assume that the journal has already been locked in this function.
274  *
275  * Return value:
276  *  <0: Error
277  * >=0: Finished OK
278  *
279  * On success:
280  * Bit 0 set == escape performed on the data
281  * Bit 1 set == buffer copy-out performed (kfree the data after IO)
282  */
283
284 int journal_write_metadata_buffer(transaction_t *transaction,
285                                   struct journal_head  *jh_in,
286                                   struct journal_head **jh_out,
287                                   unsigned int blocknr)
288 {
289         int need_copy_out = 0;
290         int done_copy_out = 0;
291         int do_escape = 0;
292         char *mapped_data;
293         struct buffer_head *new_bh;
294         struct journal_head *new_jh;
295         struct page *new_page;
296         unsigned int new_offset;
297         struct buffer_head *bh_in = jh2bh(jh_in);
298         journal_t *journal = transaction->t_journal;
299
300         /*
301          * The buffer really shouldn't be locked: only the current committing
302          * transaction is allowed to write it, so nobody else is allowed
303          * to do any IO.
304          *
305          * akpm: except if we're journalling data, and write() output is
306          * also part of a shared mapping, and another thread has
307          * decided to launch a writepage() against this buffer.
308          */
309         J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
310
311         new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
312         /* keep subsequent assertions sane */
313         atomic_set(&new_bh->b_count, 1);
314         new_jh = journal_add_journal_head(new_bh);      /* This sleeps */
315
316         /*
317          * If a new transaction has already done a buffer copy-out, then
318          * we use that version of the data for the commit.
319          */
320         jbd_lock_bh_state(bh_in);
321 repeat:
322         if (jh_in->b_frozen_data) {
323                 done_copy_out = 1;
324                 new_page = virt_to_page(jh_in->b_frozen_data);
325                 new_offset = offset_in_page(jh_in->b_frozen_data);
326         } else {
327                 new_page = jh2bh(jh_in)->b_page;
328                 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
329         }
330
331         mapped_data = kmap_atomic(new_page);
332         /*
333          * Check for escaping
334          */
335         if (*((__be32 *)(mapped_data + new_offset)) ==
336                                 cpu_to_be32(JFS_MAGIC_NUMBER)) {
337                 need_copy_out = 1;
338                 do_escape = 1;
339         }
340         kunmap_atomic(mapped_data);
341
342         /*
343          * Do we need to do a data copy?
344          */
345         if (need_copy_out && !done_copy_out) {
346                 char *tmp;
347
348                 jbd_unlock_bh_state(bh_in);
349                 tmp = jbd_alloc(bh_in->b_size, GFP_NOFS);
350                 jbd_lock_bh_state(bh_in);
351                 if (jh_in->b_frozen_data) {
352                         jbd_free(tmp, bh_in->b_size);
353                         goto repeat;
354                 }
355
356                 jh_in->b_frozen_data = tmp;
357                 mapped_data = kmap_atomic(new_page);
358                 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
359                 kunmap_atomic(mapped_data);
360
361                 new_page = virt_to_page(tmp);
362                 new_offset = offset_in_page(tmp);
363                 done_copy_out = 1;
364         }
365
366         /*
367          * Did we need to do an escaping?  Now we've done all the
368          * copying, we can finally do so.
369          */
370         if (do_escape) {
371                 mapped_data = kmap_atomic(new_page);
372                 *((unsigned int *)(mapped_data + new_offset)) = 0;
373                 kunmap_atomic(mapped_data);
374         }
375
376         set_bh_page(new_bh, new_page, new_offset);
377         new_jh->b_transaction = NULL;
378         new_bh->b_size = jh2bh(jh_in)->b_size;
379         new_bh->b_bdev = transaction->t_journal->j_dev;
380         new_bh->b_blocknr = blocknr;
381         set_buffer_mapped(new_bh);
382         set_buffer_dirty(new_bh);
383
384         *jh_out = new_jh;
385
386         /*
387          * The to-be-written buffer needs to get moved to the io queue,
388          * and the original buffer whose contents we are shadowing or
389          * copying is moved to the transaction's shadow queue.
390          */
391         JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
392         spin_lock(&journal->j_list_lock);
393         __journal_file_buffer(jh_in, transaction, BJ_Shadow);
394         spin_unlock(&journal->j_list_lock);
395         jbd_unlock_bh_state(bh_in);
396
397         JBUFFER_TRACE(new_jh, "file as BJ_IO");
398         journal_file_buffer(new_jh, transaction, BJ_IO);
399
400         return do_escape | (done_copy_out << 1);
401 }
402
403 /*
404  * Allocation code for the journal file.  Manage the space left in the
405  * journal, so that we can begin checkpointing when appropriate.
406  */
407
408 /*
409  * __log_space_left: Return the number of free blocks left in the journal.
410  *
411  * Called with the journal already locked.
412  *
413  * Called under j_state_lock
414  */
415
416 int __log_space_left(journal_t *journal)
417 {
418         int left = journal->j_free;
419
420         assert_spin_locked(&journal->j_state_lock);
421
422         /*
423          * Be pessimistic here about the number of those free blocks which
424          * might be required for log descriptor control blocks.
425          */
426
427 #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
428
429         left -= MIN_LOG_RESERVED_BLOCKS;
430
431         if (left <= 0)
432                 return 0;
433         left -= (left >> 3);
434         return left;
435 }
436
437 /*
438  * Called under j_state_lock.  Returns true if a transaction commit was started.
439  */
440 int __log_start_commit(journal_t *journal, tid_t target)
441 {
442         /*
443          * The only transaction we can possibly wait upon is the
444          * currently running transaction (if it exists).  Otherwise,
445          * the target tid must be an old one.
446          */
447         if (journal->j_commit_request != target &&
448             journal->j_running_transaction &&
449             journal->j_running_transaction->t_tid == target) {
450                 /*
451                  * We want a new commit: OK, mark the request and wakeup the
452                  * commit thread.  We do _not_ do the commit ourselves.
453                  */
454
455                 journal->j_commit_request = target;
456                 jbd_debug(1, "JBD: requesting commit %d/%d\n",
457                           journal->j_commit_request,
458                           journal->j_commit_sequence);
459                 wake_up(&journal->j_wait_commit);
460                 return 1;
461         } else if (!tid_geq(journal->j_commit_request, target))
462                 /* This should never happen, but if it does, preserve
463                    the evidence before kjournald goes into a loop and
464                    increments j_commit_sequence beyond all recognition. */
465                 WARN_ONCE(1, "jbd: bad log_start_commit: %u %u %u %u\n",
466                     journal->j_commit_request, journal->j_commit_sequence,
467                     target, journal->j_running_transaction ?
468                     journal->j_running_transaction->t_tid : 0);
469         return 0;
470 }
471
472 int log_start_commit(journal_t *journal, tid_t tid)
473 {
474         int ret;
475
476         spin_lock(&journal->j_state_lock);
477         ret = __log_start_commit(journal, tid);
478         spin_unlock(&journal->j_state_lock);
479         return ret;
480 }
481
482 /*
483  * Force and wait upon a commit if the calling process is not within
484  * transaction.  This is used for forcing out undo-protected data which contains
485  * bitmaps, when the fs is running out of space.
486  *
487  * We can only force the running transaction if we don't have an active handle;
488  * otherwise, we will deadlock.
489  *
490  * Returns true if a transaction was started.
491  */
492 int journal_force_commit_nested(journal_t *journal)
493 {
494         transaction_t *transaction = NULL;
495         tid_t tid;
496
497         spin_lock(&journal->j_state_lock);
498         if (journal->j_running_transaction && !current->journal_info) {
499                 transaction = journal->j_running_transaction;
500                 __log_start_commit(journal, transaction->t_tid);
501         } else if (journal->j_committing_transaction)
502                 transaction = journal->j_committing_transaction;
503
504         if (!transaction) {
505                 spin_unlock(&journal->j_state_lock);
506                 return 0;       /* Nothing to retry */
507         }
508
509         tid = transaction->t_tid;
510         spin_unlock(&journal->j_state_lock);
511         log_wait_commit(journal, tid);
512         return 1;
513 }
514
515 /*
516  * Start a commit of the current running transaction (if any).  Returns true
517  * if a transaction is going to be committed (or is currently already
518  * committing), and fills its tid in at *ptid
519  */
520 int journal_start_commit(journal_t *journal, tid_t *ptid)
521 {
522         int ret = 0;
523
524         spin_lock(&journal->j_state_lock);
525         if (journal->j_running_transaction) {
526                 tid_t tid = journal->j_running_transaction->t_tid;
527
528                 __log_start_commit(journal, tid);
529                 /* There's a running transaction and we've just made sure
530                  * it's commit has been scheduled. */
531                 if (ptid)
532                         *ptid = tid;
533                 ret = 1;
534         } else if (journal->j_committing_transaction) {
535                 /*
536                  * If commit has been started, then we have to wait for
537                  * completion of that transaction.
538                  */
539                 if (ptid)
540                         *ptid = journal->j_committing_transaction->t_tid;
541                 ret = 1;
542         }
543         spin_unlock(&journal->j_state_lock);
544         return ret;
545 }
546
547 /*
548  * Wait for a specified commit to complete.
549  * The caller may not hold the journal lock.
550  */
551 int log_wait_commit(journal_t *journal, tid_t tid)
552 {
553         int err = 0;
554
555 #ifdef CONFIG_JBD_DEBUG
556         spin_lock(&journal->j_state_lock);
557         if (!tid_geq(journal->j_commit_request, tid)) {
558                 printk(KERN_EMERG
559                        "%s: error: j_commit_request=%d, tid=%d\n",
560                        __func__, journal->j_commit_request, tid);
561         }
562         spin_unlock(&journal->j_state_lock);
563 #endif
564         spin_lock(&journal->j_state_lock);
565         if (!tid_geq(journal->j_commit_waited, tid))
566                 journal->j_commit_waited = tid;
567         while (tid_gt(tid, journal->j_commit_sequence)) {
568                 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
569                                   tid, journal->j_commit_sequence);
570                 wake_up(&journal->j_wait_commit);
571                 spin_unlock(&journal->j_state_lock);
572                 wait_event(journal->j_wait_done_commit,
573                                 !tid_gt(tid, journal->j_commit_sequence));
574                 spin_lock(&journal->j_state_lock);
575         }
576         spin_unlock(&journal->j_state_lock);
577
578         if (unlikely(is_journal_aborted(journal))) {
579                 printk(KERN_EMERG "journal commit I/O error\n");
580                 err = -EIO;
581         }
582         return err;
583 }
584
585 /*
586  * Return 1 if a given transaction has not yet sent barrier request
587  * connected with a transaction commit. If 0 is returned, transaction
588  * may or may not have sent the barrier. Used to avoid sending barrier
589  * twice in common cases.
590  */
591 int journal_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
592 {
593         int ret = 0;
594         transaction_t *commit_trans;
595
596         if (!(journal->j_flags & JFS_BARRIER))
597                 return 0;
598         spin_lock(&journal->j_state_lock);
599         /* Transaction already committed? */
600         if (tid_geq(journal->j_commit_sequence, tid))
601                 goto out;
602         /*
603          * Transaction is being committed and we already proceeded to
604          * writing commit record?
605          */
606         commit_trans = journal->j_committing_transaction;
607         if (commit_trans && commit_trans->t_tid == tid &&
608             commit_trans->t_state >= T_COMMIT_RECORD)
609                 goto out;
610         ret = 1;
611 out:
612         spin_unlock(&journal->j_state_lock);
613         return ret;
614 }
615 EXPORT_SYMBOL(journal_trans_will_send_data_barrier);
616
617 /*
618  * Log buffer allocation routines:
619  */
620
621 int journal_next_log_block(journal_t *journal, unsigned int *retp)
622 {
623         unsigned int blocknr;
624
625         spin_lock(&journal->j_state_lock);
626         J_ASSERT(journal->j_free > 1);
627
628         blocknr = journal->j_head;
629         journal->j_head++;
630         journal->j_free--;
631         if (journal->j_head == journal->j_last)
632                 journal->j_head = journal->j_first;
633         spin_unlock(&journal->j_state_lock);
634         return journal_bmap(journal, blocknr, retp);
635 }
636
637 /*
638  * Conversion of logical to physical block numbers for the journal
639  *
640  * On external journals the journal blocks are identity-mapped, so
641  * this is a no-op.  If needed, we can use j_blk_offset - everything is
642  * ready.
643  */
644 int journal_bmap(journal_t *journal, unsigned int blocknr,
645                  unsigned int *retp)
646 {
647         int err = 0;
648         unsigned int ret;
649
650         if (journal->j_inode) {
651                 ret = bmap(journal->j_inode, blocknr);
652                 if (ret)
653                         *retp = ret;
654                 else {
655                         char b[BDEVNAME_SIZE];
656
657                         printk(KERN_ALERT "%s: journal block not found "
658                                         "at offset %u on %s\n",
659                                 __func__,
660                                 blocknr,
661                                 bdevname(journal->j_dev, b));
662                         err = -EIO;
663                         __journal_abort_soft(journal, err);
664                 }
665         } else {
666                 *retp = blocknr; /* +journal->j_blk_offset */
667         }
668         return err;
669 }
670
671 /*
672  * We play buffer_head aliasing tricks to write data/metadata blocks to
673  * the journal without copying their contents, but for journal
674  * descriptor blocks we do need to generate bona fide buffers.
675  *
676  * After the caller of journal_get_descriptor_buffer() has finished modifying
677  * the buffer's contents they really should run flush_dcache_page(bh->b_page).
678  * But we don't bother doing that, so there will be coherency problems with
679  * mmaps of blockdevs which hold live JBD-controlled filesystems.
680  */
681 struct journal_head *journal_get_descriptor_buffer(journal_t *journal)
682 {
683         struct buffer_head *bh;
684         unsigned int blocknr;
685         int err;
686
687         err = journal_next_log_block(journal, &blocknr);
688
689         if (err)
690                 return NULL;
691
692         bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
693         if (!bh)
694                 return NULL;
695         lock_buffer(bh);
696         memset(bh->b_data, 0, journal->j_blocksize);
697         set_buffer_uptodate(bh);
698         unlock_buffer(bh);
699         BUFFER_TRACE(bh, "return this buffer");
700         return journal_add_journal_head(bh);
701 }
702
703 /*
704  * Management for journal control blocks: functions to create and
705  * destroy journal_t structures, and to initialise and read existing
706  * journal blocks from disk.  */
707
708 /* First: create and setup a journal_t object in memory.  We initialise
709  * very few fields yet: that has to wait until we have created the
710  * journal structures from from scratch, or loaded them from disk. */
711
712 static journal_t * journal_init_common (void)
713 {
714         journal_t *journal;
715         int err;
716
717         journal = kzalloc(sizeof(*journal), GFP_KERNEL);
718         if (!journal)
719                 goto fail;
720
721         init_waitqueue_head(&journal->j_wait_transaction_locked);
722         init_waitqueue_head(&journal->j_wait_logspace);
723         init_waitqueue_head(&journal->j_wait_done_commit);
724         init_waitqueue_head(&journal->j_wait_checkpoint);
725         init_waitqueue_head(&journal->j_wait_commit);
726         init_waitqueue_head(&journal->j_wait_updates);
727         mutex_init(&journal->j_checkpoint_mutex);
728         spin_lock_init(&journal->j_revoke_lock);
729         spin_lock_init(&journal->j_list_lock);
730         spin_lock_init(&journal->j_state_lock);
731
732         journal->j_commit_interval = (HZ * JBD_DEFAULT_MAX_COMMIT_AGE);
733
734         /* The journal is marked for error until we succeed with recovery! */
735         journal->j_flags = JFS_ABORT;
736
737         /* Set up a default-sized revoke table for the new mount. */
738         err = journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
739         if (err) {
740                 kfree(journal);
741                 goto fail;
742         }
743         return journal;
744 fail:
745         return NULL;
746 }
747
748 /* journal_init_dev and journal_init_inode:
749  *
750  * Create a journal structure assigned some fixed set of disk blocks to
751  * the journal.  We don't actually touch those disk blocks yet, but we
752  * need to set up all of the mapping information to tell the journaling
753  * system where the journal blocks are.
754  *
755  */
756
757 /**
758  *  journal_t * journal_init_dev() - creates and initialises a journal structure
759  *  @bdev: Block device on which to create the journal
760  *  @fs_dev: Device which hold journalled filesystem for this journal.
761  *  @start: Block nr Start of journal.
762  *  @len:  Length of the journal in blocks.
763  *  @blocksize: blocksize of journalling device
764  *
765  *  Returns: a newly created journal_t *
766  *
767  *  journal_init_dev creates a journal which maps a fixed contiguous
768  *  range of blocks on an arbitrary block device.
769  *
770  */
771 journal_t * journal_init_dev(struct block_device *bdev,
772                         struct block_device *fs_dev,
773                         int start, int len, int blocksize)
774 {
775         journal_t *journal = journal_init_common();
776         struct buffer_head *bh;
777         int n;
778
779         if (!journal)
780                 return NULL;
781
782         /* journal descriptor can store up to n blocks -bzzz */
783         journal->j_blocksize = blocksize;
784         n = journal->j_blocksize / sizeof(journal_block_tag_t);
785         journal->j_wbufsize = n;
786         journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
787         if (!journal->j_wbuf) {
788                 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
789                         __func__);
790                 goto out_err;
791         }
792         journal->j_dev = bdev;
793         journal->j_fs_dev = fs_dev;
794         journal->j_blk_offset = start;
795         journal->j_maxlen = len;
796
797         bh = __getblk(journal->j_dev, start, journal->j_blocksize);
798         if (!bh) {
799                 printk(KERN_ERR
800                        "%s: Cannot get buffer for journal superblock\n",
801                        __func__);
802                 goto out_err;
803         }
804         journal->j_sb_buffer = bh;
805         journal->j_superblock = (journal_superblock_t *)bh->b_data;
806
807         return journal;
808 out_err:
809         kfree(journal->j_wbuf);
810         kfree(journal);
811         return NULL;
812 }
813
814 /**
815  *  journal_t * journal_init_inode () - creates a journal which maps to a inode.
816  *  @inode: An inode to create the journal in
817  *
818  * journal_init_inode creates a journal which maps an on-disk inode as
819  * the journal.  The inode must exist already, must support bmap() and
820  * must have all data blocks preallocated.
821  */
822 journal_t * journal_init_inode (struct inode *inode)
823 {
824         struct buffer_head *bh;
825         journal_t *journal = journal_init_common();
826         int err;
827         int n;
828         unsigned int blocknr;
829
830         if (!journal)
831                 return NULL;
832
833         journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
834         journal->j_inode = inode;
835         jbd_debug(1,
836                   "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
837                   journal, inode->i_sb->s_id, inode->i_ino,
838                   (long long) inode->i_size,
839                   inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
840
841         journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
842         journal->j_blocksize = inode->i_sb->s_blocksize;
843
844         /* journal descriptor can store up to n blocks -bzzz */
845         n = journal->j_blocksize / sizeof(journal_block_tag_t);
846         journal->j_wbufsize = n;
847         journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
848         if (!journal->j_wbuf) {
849                 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
850                         __func__);
851                 goto out_err;
852         }
853
854         err = journal_bmap(journal, 0, &blocknr);
855         /* If that failed, give up */
856         if (err) {
857                 printk(KERN_ERR "%s: Cannot locate journal superblock\n",
858                        __func__);
859                 goto out_err;
860         }
861
862         bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
863         if (!bh) {
864                 printk(KERN_ERR
865                        "%s: Cannot get buffer for journal superblock\n",
866                        __func__);
867                 goto out_err;
868         }
869         journal->j_sb_buffer = bh;
870         journal->j_superblock = (journal_superblock_t *)bh->b_data;
871
872         return journal;
873 out_err:
874         kfree(journal->j_wbuf);
875         kfree(journal);
876         return NULL;
877 }
878
879 /*
880  * If the journal init or create aborts, we need to mark the journal
881  * superblock as being NULL to prevent the journal destroy from writing
882  * back a bogus superblock.
883  */
884 static void journal_fail_superblock (journal_t *journal)
885 {
886         struct buffer_head *bh = journal->j_sb_buffer;
887         brelse(bh);
888         journal->j_sb_buffer = NULL;
889 }
890
891 /*
892  * Given a journal_t structure, initialise the various fields for
893  * startup of a new journaling session.  We use this both when creating
894  * a journal, and after recovering an old journal to reset it for
895  * subsequent use.
896  */
897
898 static int journal_reset(journal_t *journal)
899 {
900         journal_superblock_t *sb = journal->j_superblock;
901         unsigned int first, last;
902
903         first = be32_to_cpu(sb->s_first);
904         last = be32_to_cpu(sb->s_maxlen);
905         if (first + JFS_MIN_JOURNAL_BLOCKS > last + 1) {
906                 printk(KERN_ERR "JBD: Journal too short (blocks %u-%u).\n",
907                        first, last);
908                 journal_fail_superblock(journal);
909                 return -EINVAL;
910         }
911
912         journal->j_first = first;
913         journal->j_last = last;
914
915         journal->j_head = first;
916         journal->j_tail = first;
917         journal->j_free = last - first;
918
919         journal->j_tail_sequence = journal->j_transaction_sequence;
920         journal->j_commit_sequence = journal->j_transaction_sequence - 1;
921         journal->j_commit_request = journal->j_commit_sequence;
922
923         journal->j_max_transaction_buffers = journal->j_maxlen / 4;
924
925         /*
926          * As a special case, if the on-disk copy is already marked as needing
927          * no recovery (s_start == 0), then we can safely defer the superblock
928          * update until the next commit by setting JFS_FLUSHED.  This avoids
929          * attempting a write to a potential-readonly device.
930          */
931         if (sb->s_start == 0) {
932                 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
933                         "(start %u, seq %d, errno %d)\n",
934                         journal->j_tail, journal->j_tail_sequence,
935                         journal->j_errno);
936                 journal->j_flags |= JFS_FLUSHED;
937         } else {
938                 /* Lock here to make assertions happy... */
939                 mutex_lock(&journal->j_checkpoint_mutex);
940                 /*
941                  * Update log tail information. We use WRITE_FUA since new
942                  * transaction will start reusing journal space and so we
943                  * must make sure information about current log tail is on
944                  * disk before that.
945                  */
946                 journal_update_sb_log_tail(journal,
947                                            journal->j_tail_sequence,
948                                            journal->j_tail,
949                                            WRITE_FUA);
950                 mutex_unlock(&journal->j_checkpoint_mutex);
951         }
952         return journal_start_thread(journal);
953 }
954
955 /**
956  * int journal_create() - Initialise the new journal file
957  * @journal: Journal to create. This structure must have been initialised
958  *
959  * Given a journal_t structure which tells us which disk blocks we can
960  * use, create a new journal superblock and initialise all of the
961  * journal fields from scratch.
962  **/
963 int journal_create(journal_t *journal)
964 {
965         unsigned int blocknr;
966         struct buffer_head *bh;
967         journal_superblock_t *sb;
968         int i, err;
969
970         if (journal->j_maxlen < JFS_MIN_JOURNAL_BLOCKS) {
971                 printk (KERN_ERR "Journal length (%d blocks) too short.\n",
972                         journal->j_maxlen);
973                 journal_fail_superblock(journal);
974                 return -EINVAL;
975         }
976
977         if (journal->j_inode == NULL) {
978                 /*
979                  * We don't know what block to start at!
980                  */
981                 printk(KERN_EMERG
982                        "%s: creation of journal on external device!\n",
983                        __func__);
984                 BUG();
985         }
986
987         /* Zero out the entire journal on disk.  We cannot afford to
988            have any blocks on disk beginning with JFS_MAGIC_NUMBER. */
989         jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
990         for (i = 0; i < journal->j_maxlen; i++) {
991                 err = journal_bmap(journal, i, &blocknr);
992                 if (err)
993                         return err;
994                 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
995                 if (unlikely(!bh))
996                         return -ENOMEM;
997                 lock_buffer(bh);
998                 memset (bh->b_data, 0, journal->j_blocksize);
999                 BUFFER_TRACE(bh, "marking dirty");
1000                 mark_buffer_dirty(bh);
1001                 BUFFER_TRACE(bh, "marking uptodate");
1002                 set_buffer_uptodate(bh);
1003                 unlock_buffer(bh);
1004                 __brelse(bh);
1005         }
1006
1007         sync_blockdev(journal->j_dev);
1008         jbd_debug(1, "JBD: journal cleared.\n");
1009
1010         /* OK, fill in the initial static fields in the new superblock */
1011         sb = journal->j_superblock;
1012
1013         sb->s_header.h_magic     = cpu_to_be32(JFS_MAGIC_NUMBER);
1014         sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
1015
1016         sb->s_blocksize = cpu_to_be32(journal->j_blocksize);
1017         sb->s_maxlen    = cpu_to_be32(journal->j_maxlen);
1018         sb->s_first     = cpu_to_be32(1);
1019
1020         journal->j_transaction_sequence = 1;
1021
1022         journal->j_flags &= ~JFS_ABORT;
1023         journal->j_format_version = 2;
1024
1025         return journal_reset(journal);
1026 }
1027
1028 static void journal_write_superblock(journal_t *journal, int write_op)
1029 {
1030         struct buffer_head *bh = journal->j_sb_buffer;
1031         int ret;
1032
1033         trace_journal_write_superblock(journal, write_op);
1034         if (!(journal->j_flags & JFS_BARRIER))
1035                 write_op &= ~(REQ_FUA | REQ_FLUSH);
1036         lock_buffer(bh);
1037         if (buffer_write_io_error(bh)) {
1038                 char b[BDEVNAME_SIZE];
1039                 /*
1040                  * Oh, dear.  A previous attempt to write the journal
1041                  * superblock failed.  This could happen because the
1042                  * USB device was yanked out.  Or it could happen to
1043                  * be a transient write error and maybe the block will
1044                  * be remapped.  Nothing we can do but to retry the
1045                  * write and hope for the best.
1046                  */
1047                 printk(KERN_ERR "JBD: previous I/O error detected "
1048                        "for journal superblock update for %s.\n",
1049                        journal_dev_name(journal, b));
1050                 clear_buffer_write_io_error(bh);
1051                 set_buffer_uptodate(bh);
1052         }
1053
1054         get_bh(bh);
1055         bh->b_end_io = end_buffer_write_sync;
1056         ret = submit_bh(write_op, bh);
1057         wait_on_buffer(bh);
1058         if (buffer_write_io_error(bh)) {
1059                 clear_buffer_write_io_error(bh);
1060                 set_buffer_uptodate(bh);
1061                 ret = -EIO;
1062         }
1063         if (ret) {
1064                 char b[BDEVNAME_SIZE];
1065                 printk(KERN_ERR "JBD: Error %d detected "
1066                        "when updating journal superblock for %s.\n",
1067                        ret, journal_dev_name(journal, b));
1068         }
1069 }
1070
1071 /**
1072  * journal_update_sb_log_tail() - Update log tail in journal sb on disk.
1073  * @journal: The journal to update.
1074  * @tail_tid: TID of the new transaction at the tail of the log
1075  * @tail_block: The first block of the transaction at the tail of the log
1076  * @write_op: With which operation should we write the journal sb
1077  *
1078  * Update a journal's superblock information about log tail and write it to
1079  * disk, waiting for the IO to complete.
1080  */
1081 void journal_update_sb_log_tail(journal_t *journal, tid_t tail_tid,
1082                                 unsigned int tail_block, int write_op)
1083 {
1084         journal_superblock_t *sb = journal->j_superblock;
1085
1086         BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
1087         jbd_debug(1,"JBD: updating superblock (start %u, seq %u)\n",
1088                   tail_block, tail_tid);
1089
1090         sb->s_sequence = cpu_to_be32(tail_tid);
1091         sb->s_start    = cpu_to_be32(tail_block);
1092
1093         journal_write_superblock(journal, write_op);
1094
1095         /* Log is no longer empty */
1096         spin_lock(&journal->j_state_lock);
1097         WARN_ON(!sb->s_sequence);
1098         journal->j_flags &= ~JFS_FLUSHED;
1099         spin_unlock(&journal->j_state_lock);
1100 }
1101
1102 /**
1103  * mark_journal_empty() - Mark on disk journal as empty.
1104  * @journal: The journal to update.
1105  *
1106  * Update a journal's dynamic superblock fields to show that journal is empty.
1107  * Write updated superblock to disk waiting for IO to complete.
1108  */
1109 static void mark_journal_empty(journal_t *journal)
1110 {
1111         journal_superblock_t *sb = journal->j_superblock;
1112
1113         BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
1114         spin_lock(&journal->j_state_lock);
1115         /* Is it already empty? */
1116         if (sb->s_start == 0) {
1117                 spin_unlock(&journal->j_state_lock);
1118                 return;
1119         }
1120         jbd_debug(1, "JBD: Marking journal as empty (seq %d)\n",
1121                   journal->j_tail_sequence);
1122
1123         sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1124         sb->s_start    = cpu_to_be32(0);
1125         spin_unlock(&journal->j_state_lock);
1126
1127         journal_write_superblock(journal, WRITE_FUA);
1128
1129         spin_lock(&journal->j_state_lock);
1130         /* Log is empty */
1131         journal->j_flags |= JFS_FLUSHED;
1132         spin_unlock(&journal->j_state_lock);
1133 }
1134
1135 /**
1136  * journal_update_sb_errno() - Update error in the journal.
1137  * @journal: The journal to update.
1138  *
1139  * Update a journal's errno.  Write updated superblock to disk waiting for IO
1140  * to complete.
1141  */
1142 static void journal_update_sb_errno(journal_t *journal)
1143 {
1144         journal_superblock_t *sb = journal->j_superblock;
1145
1146         spin_lock(&journal->j_state_lock);
1147         jbd_debug(1, "JBD: updating superblock error (errno %d)\n",
1148                   journal->j_errno);
1149         sb->s_errno = cpu_to_be32(journal->j_errno);
1150         spin_unlock(&journal->j_state_lock);
1151
1152         journal_write_superblock(journal, WRITE_SYNC);
1153 }
1154
1155 /*
1156  * Read the superblock for a given journal, performing initial
1157  * validation of the format.
1158  */
1159
1160 static int journal_get_superblock(journal_t *journal)
1161 {
1162         struct buffer_head *bh;
1163         journal_superblock_t *sb;
1164         int err = -EIO;
1165
1166         bh = journal->j_sb_buffer;
1167
1168         J_ASSERT(bh != NULL);
1169         if (!buffer_uptodate(bh)) {
1170                 ll_rw_block(READ, 1, &bh);
1171                 wait_on_buffer(bh);
1172                 if (!buffer_uptodate(bh)) {
1173                         printk (KERN_ERR
1174                                 "JBD: IO error reading journal superblock\n");
1175                         goto out;
1176                 }
1177         }
1178
1179         sb = journal->j_superblock;
1180
1181         err = -EINVAL;
1182
1183         if (sb->s_header.h_magic != cpu_to_be32(JFS_MAGIC_NUMBER) ||
1184             sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1185                 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1186                 goto out;
1187         }
1188
1189         switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1190         case JFS_SUPERBLOCK_V1:
1191                 journal->j_format_version = 1;
1192                 break;
1193         case JFS_SUPERBLOCK_V2:
1194                 journal->j_format_version = 2;
1195                 break;
1196         default:
1197                 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1198                 goto out;
1199         }
1200
1201         if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1202                 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1203         else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1204                 printk (KERN_WARNING "JBD: journal file too short\n");
1205                 goto out;
1206         }
1207
1208         if (be32_to_cpu(sb->s_first) == 0 ||
1209             be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
1210                 printk(KERN_WARNING
1211                         "JBD: Invalid start block of journal: %u\n",
1212                         be32_to_cpu(sb->s_first));
1213                 goto out;
1214         }
1215
1216         return 0;
1217
1218 out:
1219         journal_fail_superblock(journal);
1220         return err;
1221 }
1222
1223 /*
1224  * Load the on-disk journal superblock and read the key fields into the
1225  * journal_t.
1226  */
1227
1228 static int load_superblock(journal_t *journal)
1229 {
1230         int err;
1231         journal_superblock_t *sb;
1232
1233         err = journal_get_superblock(journal);
1234         if (err)
1235                 return err;
1236
1237         sb = journal->j_superblock;
1238
1239         journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1240         journal->j_tail = be32_to_cpu(sb->s_start);
1241         journal->j_first = be32_to_cpu(sb->s_first);
1242         journal->j_last = be32_to_cpu(sb->s_maxlen);
1243         journal->j_errno = be32_to_cpu(sb->s_errno);
1244
1245         return 0;
1246 }
1247
1248
1249 /**
1250  * int journal_load() - Read journal from disk.
1251  * @journal: Journal to act on.
1252  *
1253  * Given a journal_t structure which tells us which disk blocks contain
1254  * a journal, read the journal from disk to initialise the in-memory
1255  * structures.
1256  */
1257 int journal_load(journal_t *journal)
1258 {
1259         int err;
1260         journal_superblock_t *sb;
1261
1262         err = load_superblock(journal);
1263         if (err)
1264                 return err;
1265
1266         sb = journal->j_superblock;
1267         /* If this is a V2 superblock, then we have to check the
1268          * features flags on it. */
1269
1270         if (journal->j_format_version >= 2) {
1271                 if ((sb->s_feature_ro_compat &
1272                      ~cpu_to_be32(JFS_KNOWN_ROCOMPAT_FEATURES)) ||
1273                     (sb->s_feature_incompat &
1274                      ~cpu_to_be32(JFS_KNOWN_INCOMPAT_FEATURES))) {
1275                         printk (KERN_WARNING
1276                                 "JBD: Unrecognised features on journal\n");
1277                         return -EINVAL;
1278                 }
1279         }
1280
1281         /* Let the recovery code check whether it needs to recover any
1282          * data from the journal. */
1283         if (journal_recover(journal))
1284                 goto recovery_error;
1285
1286         /* OK, we've finished with the dynamic journal bits:
1287          * reinitialise the dynamic contents of the superblock in memory
1288          * and reset them on disk. */
1289         if (journal_reset(journal))
1290                 goto recovery_error;
1291
1292         journal->j_flags &= ~JFS_ABORT;
1293         journal->j_flags |= JFS_LOADED;
1294         return 0;
1295
1296 recovery_error:
1297         printk (KERN_WARNING "JBD: recovery failed\n");
1298         return -EIO;
1299 }
1300
1301 /**
1302  * void journal_destroy() - Release a journal_t structure.
1303  * @journal: Journal to act on.
1304  *
1305  * Release a journal_t structure once it is no longer in use by the
1306  * journaled object.
1307  * Return <0 if we couldn't clean up the journal.
1308  */
1309 int journal_destroy(journal_t *journal)
1310 {
1311         int err = 0;
1312
1313         
1314         /* Wait for the commit thread to wake up and die. */
1315         journal_kill_thread(journal);
1316
1317         /* Force a final log commit */
1318         if (journal->j_running_transaction)
1319                 journal_commit_transaction(journal);
1320
1321         /* Force any old transactions to disk */
1322
1323         /* We cannot race with anybody but must keep assertions happy */
1324         mutex_lock(&journal->j_checkpoint_mutex);
1325         /* Totally anal locking here... */
1326         spin_lock(&journal->j_list_lock);
1327         while (journal->j_checkpoint_transactions != NULL) {
1328                 spin_unlock(&journal->j_list_lock);
1329                 log_do_checkpoint(journal);
1330                 spin_lock(&journal->j_list_lock);
1331         }
1332
1333         J_ASSERT(journal->j_running_transaction == NULL);
1334         J_ASSERT(journal->j_committing_transaction == NULL);
1335         J_ASSERT(journal->j_checkpoint_transactions == NULL);
1336         spin_unlock(&journal->j_list_lock);
1337
1338         if (journal->j_sb_buffer) {
1339                 if (!is_journal_aborted(journal)) {
1340                         journal->j_tail_sequence =
1341                                 ++journal->j_transaction_sequence;
1342                         mark_journal_empty(journal);
1343                 } else
1344                         err = -EIO;
1345                 brelse(journal->j_sb_buffer);
1346         }
1347         mutex_unlock(&journal->j_checkpoint_mutex);
1348
1349         if (journal->j_inode)
1350                 iput(journal->j_inode);
1351         if (journal->j_revoke)
1352                 journal_destroy_revoke(journal);
1353         kfree(journal->j_wbuf);
1354         kfree(journal);
1355
1356         return err;
1357 }
1358
1359
1360 /**
1361  *int journal_check_used_features () - Check if features specified are used.
1362  * @journal: Journal to check.
1363  * @compat: bitmask of compatible features
1364  * @ro: bitmask of features that force read-only mount
1365  * @incompat: bitmask of incompatible features
1366  *
1367  * Check whether the journal uses all of a given set of
1368  * features.  Return true (non-zero) if it does.
1369  **/
1370
1371 int journal_check_used_features (journal_t *journal, unsigned long compat,
1372                                  unsigned long ro, unsigned long incompat)
1373 {
1374         journal_superblock_t *sb;
1375
1376         if (!compat && !ro && !incompat)
1377                 return 1;
1378         if (journal->j_format_version == 1)
1379                 return 0;
1380
1381         sb = journal->j_superblock;
1382
1383         if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1384             ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1385             ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1386                 return 1;
1387
1388         return 0;
1389 }
1390
1391 /**
1392  * int journal_check_available_features() - Check feature set in journalling layer
1393  * @journal: Journal to check.
1394  * @compat: bitmask of compatible features
1395  * @ro: bitmask of features that force read-only mount
1396  * @incompat: bitmask of incompatible features
1397  *
1398  * Check whether the journaling code supports the use of
1399  * all of a given set of features on this journal.  Return true
1400  * (non-zero) if it can. */
1401
1402 int journal_check_available_features (journal_t *journal, unsigned long compat,
1403                                       unsigned long ro, unsigned long incompat)
1404 {
1405         if (!compat && !ro && !incompat)
1406                 return 1;
1407
1408         /* We can support any known requested features iff the
1409          * superblock is in version 2.  Otherwise we fail to support any
1410          * extended sb features. */
1411
1412         if (journal->j_format_version != 2)
1413                 return 0;
1414
1415         if ((compat   & JFS_KNOWN_COMPAT_FEATURES) == compat &&
1416             (ro       & JFS_KNOWN_ROCOMPAT_FEATURES) == ro &&
1417             (incompat & JFS_KNOWN_INCOMPAT_FEATURES) == incompat)
1418                 return 1;
1419
1420         return 0;
1421 }
1422
1423 /**
1424  * int journal_set_features () - Mark a given journal feature in the superblock
1425  * @journal: Journal to act on.
1426  * @compat: bitmask of compatible features
1427  * @ro: bitmask of features that force read-only mount
1428  * @incompat: bitmask of incompatible features
1429  *
1430  * Mark a given journal feature as present on the
1431  * superblock.  Returns true if the requested features could be set.
1432  *
1433  */
1434
1435 int journal_set_features (journal_t *journal, unsigned long compat,
1436                           unsigned long ro, unsigned long incompat)
1437 {
1438         journal_superblock_t *sb;
1439
1440         if (journal_check_used_features(journal, compat, ro, incompat))
1441                 return 1;
1442
1443         if (!journal_check_available_features(journal, compat, ro, incompat))
1444                 return 0;
1445
1446         jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1447                   compat, ro, incompat);
1448
1449         sb = journal->j_superblock;
1450
1451         sb->s_feature_compat    |= cpu_to_be32(compat);
1452         sb->s_feature_ro_compat |= cpu_to_be32(ro);
1453         sb->s_feature_incompat  |= cpu_to_be32(incompat);
1454
1455         return 1;
1456 }
1457
1458
1459 /**
1460  * int journal_update_format () - Update on-disk journal structure.
1461  * @journal: Journal to act on.
1462  *
1463  * Given an initialised but unloaded journal struct, poke about in the
1464  * on-disk structure to update it to the most recent supported version.
1465  */
1466 int journal_update_format (journal_t *journal)
1467 {
1468         journal_superblock_t *sb;
1469         int err;
1470
1471         err = journal_get_superblock(journal);
1472         if (err)
1473                 return err;
1474
1475         sb = journal->j_superblock;
1476
1477         switch (be32_to_cpu(sb->s_header.h_blocktype)) {
1478         case JFS_SUPERBLOCK_V2:
1479                 return 0;
1480         case JFS_SUPERBLOCK_V1:
1481                 return journal_convert_superblock_v1(journal, sb);
1482         default:
1483                 break;
1484         }
1485         return -EINVAL;
1486 }
1487
1488 static int journal_convert_superblock_v1(journal_t *journal,
1489                                          journal_superblock_t *sb)
1490 {
1491         int offset, blocksize;
1492         struct buffer_head *bh;
1493
1494         printk(KERN_WARNING
1495                 "JBD: Converting superblock from version 1 to 2.\n");
1496
1497         /* Pre-initialise new fields to zero */
1498         offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1499         blocksize = be32_to_cpu(sb->s_blocksize);
1500         memset(&sb->s_feature_compat, 0, blocksize-offset);
1501
1502         sb->s_nr_users = cpu_to_be32(1);
1503         sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
1504         journal->j_format_version = 2;
1505
1506         bh = journal->j_sb_buffer;
1507         BUFFER_TRACE(bh, "marking dirty");
1508         mark_buffer_dirty(bh);
1509         sync_dirty_buffer(bh);
1510         return 0;
1511 }
1512
1513
1514 /**
1515  * int journal_flush () - Flush journal
1516  * @journal: Journal to act on.
1517  *
1518  * Flush all data for a given journal to disk and empty the journal.
1519  * Filesystems can use this when remounting readonly to ensure that
1520  * recovery does not need to happen on remount.
1521  */
1522
1523 int journal_flush(journal_t *journal)
1524 {
1525         int err = 0;
1526         transaction_t *transaction = NULL;
1527
1528         spin_lock(&journal->j_state_lock);
1529
1530         /* Force everything buffered to the log... */
1531         if (journal->j_running_transaction) {
1532                 transaction = journal->j_running_transaction;
1533                 __log_start_commit(journal, transaction->t_tid);
1534         } else if (journal->j_committing_transaction)
1535                 transaction = journal->j_committing_transaction;
1536
1537         /* Wait for the log commit to complete... */
1538         if (transaction) {
1539                 tid_t tid = transaction->t_tid;
1540
1541                 spin_unlock(&journal->j_state_lock);
1542                 log_wait_commit(journal, tid);
1543         } else {
1544                 spin_unlock(&journal->j_state_lock);
1545         }
1546
1547         /* ...and flush everything in the log out to disk. */
1548         spin_lock(&journal->j_list_lock);
1549         while (!err && journal->j_checkpoint_transactions != NULL) {
1550                 spin_unlock(&journal->j_list_lock);
1551                 mutex_lock(&journal->j_checkpoint_mutex);
1552                 err = log_do_checkpoint(journal);
1553                 mutex_unlock(&journal->j_checkpoint_mutex);
1554                 spin_lock(&journal->j_list_lock);
1555         }
1556         spin_unlock(&journal->j_list_lock);
1557
1558         if (is_journal_aborted(journal))
1559                 return -EIO;
1560
1561         mutex_lock(&journal->j_checkpoint_mutex);
1562         cleanup_journal_tail(journal);
1563
1564         /* Finally, mark the journal as really needing no recovery.
1565          * This sets s_start==0 in the underlying superblock, which is
1566          * the magic code for a fully-recovered superblock.  Any future
1567          * commits of data to the journal will restore the current
1568          * s_start value. */
1569         mark_journal_empty(journal);
1570         mutex_unlock(&journal->j_checkpoint_mutex);
1571         spin_lock(&journal->j_state_lock);
1572         J_ASSERT(!journal->j_running_transaction);
1573         J_ASSERT(!journal->j_committing_transaction);
1574         J_ASSERT(!journal->j_checkpoint_transactions);
1575         J_ASSERT(journal->j_head == journal->j_tail);
1576         J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1577         spin_unlock(&journal->j_state_lock);
1578         return 0;
1579 }
1580
1581 /**
1582  * int journal_wipe() - Wipe journal contents
1583  * @journal: Journal to act on.
1584  * @write: flag (see below)
1585  *
1586  * Wipe out all of the contents of a journal, safely.  This will produce
1587  * a warning if the journal contains any valid recovery information.
1588  * Must be called between journal_init_*() and journal_load().
1589  *
1590  * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1591  * we merely suppress recovery.
1592  */
1593
1594 int journal_wipe(journal_t *journal, int write)
1595 {
1596         int err = 0;
1597
1598         J_ASSERT (!(journal->j_flags & JFS_LOADED));
1599
1600         err = load_superblock(journal);
1601         if (err)
1602                 return err;
1603
1604         if (!journal->j_tail)
1605                 goto no_recovery;
1606
1607         printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1608                 write ? "Clearing" : "Ignoring");
1609
1610         err = journal_skip_recovery(journal);
1611         if (write) {
1612                 /* Lock to make assertions happy... */
1613                 mutex_lock(&journal->j_checkpoint_mutex);
1614                 mark_journal_empty(journal);
1615                 mutex_unlock(&journal->j_checkpoint_mutex);
1616         }
1617
1618  no_recovery:
1619         return err;
1620 }
1621
1622 /*
1623  * journal_dev_name: format a character string to describe on what
1624  * device this journal is present.
1625  */
1626
1627 static const char *journal_dev_name(journal_t *journal, char *buffer)
1628 {
1629         struct block_device *bdev;
1630
1631         if (journal->j_inode)
1632                 bdev = journal->j_inode->i_sb->s_bdev;
1633         else
1634                 bdev = journal->j_dev;
1635
1636         return bdevname(bdev, buffer);
1637 }
1638
1639 /*
1640  * Journal abort has very specific semantics, which we describe
1641  * for journal abort.
1642  *
1643  * Two internal function, which provide abort to te jbd layer
1644  * itself are here.
1645  */
1646
1647 /*
1648  * Quick version for internal journal use (doesn't lock the journal).
1649  * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1650  * and don't attempt to make any other journal updates.
1651  */
1652 static void __journal_abort_hard(journal_t *journal)
1653 {
1654         transaction_t *transaction;
1655         char b[BDEVNAME_SIZE];
1656
1657         if (journal->j_flags & JFS_ABORT)
1658                 return;
1659
1660         printk(KERN_ERR "Aborting journal on device %s.\n",
1661                 journal_dev_name(journal, b));
1662
1663         spin_lock(&journal->j_state_lock);
1664         journal->j_flags |= JFS_ABORT;
1665         transaction = journal->j_running_transaction;
1666         if (transaction)
1667                 __log_start_commit(journal, transaction->t_tid);
1668         spin_unlock(&journal->j_state_lock);
1669 }
1670
1671 /* Soft abort: record the abort error status in the journal superblock,
1672  * but don't do any other IO. */
1673 static void __journal_abort_soft (journal_t *journal, int errno)
1674 {
1675         if (journal->j_flags & JFS_ABORT)
1676                 return;
1677
1678         if (!journal->j_errno)
1679                 journal->j_errno = errno;
1680
1681         __journal_abort_hard(journal);
1682
1683         if (errno)
1684                 journal_update_sb_errno(journal);
1685 }
1686
1687 /**
1688  * void journal_abort () - Shutdown the journal immediately.
1689  * @journal: the journal to shutdown.
1690  * @errno:   an error number to record in the journal indicating
1691  *           the reason for the shutdown.
1692  *
1693  * Perform a complete, immediate shutdown of the ENTIRE
1694  * journal (not of a single transaction).  This operation cannot be
1695  * undone without closing and reopening the journal.
1696  *
1697  * The journal_abort function is intended to support higher level error
1698  * recovery mechanisms such as the ext2/ext3 remount-readonly error
1699  * mode.
1700  *
1701  * Journal abort has very specific semantics.  Any existing dirty,
1702  * unjournaled buffers in the main filesystem will still be written to
1703  * disk by bdflush, but the journaling mechanism will be suspended
1704  * immediately and no further transaction commits will be honoured.
1705  *
1706  * Any dirty, journaled buffers will be written back to disk without
1707  * hitting the journal.  Atomicity cannot be guaranteed on an aborted
1708  * filesystem, but we _do_ attempt to leave as much data as possible
1709  * behind for fsck to use for cleanup.
1710  *
1711  * Any attempt to get a new transaction handle on a journal which is in
1712  * ABORT state will just result in an -EROFS error return.  A
1713  * journal_stop on an existing handle will return -EIO if we have
1714  * entered abort state during the update.
1715  *
1716  * Recursive transactions are not disturbed by journal abort until the
1717  * final journal_stop, which will receive the -EIO error.
1718  *
1719  * Finally, the journal_abort call allows the caller to supply an errno
1720  * which will be recorded (if possible) in the journal superblock.  This
1721  * allows a client to record failure conditions in the middle of a
1722  * transaction without having to complete the transaction to record the
1723  * failure to disk.  ext3_error, for example, now uses this
1724  * functionality.
1725  *
1726  * Errors which originate from within the journaling layer will NOT
1727  * supply an errno; a null errno implies that absolutely no further
1728  * writes are done to the journal (unless there are any already in
1729  * progress).
1730  *
1731  */
1732
1733 void journal_abort(journal_t *journal, int errno)
1734 {
1735         __journal_abort_soft(journal, errno);
1736 }
1737
1738 /**
1739  * int journal_errno () - returns the journal's error state.
1740  * @journal: journal to examine.
1741  *
1742  * This is the errno numbet set with journal_abort(), the last
1743  * time the journal was mounted - if the journal was stopped
1744  * without calling abort this will be 0.
1745  *
1746  * If the journal has been aborted on this mount time -EROFS will
1747  * be returned.
1748  */
1749 int journal_errno(journal_t *journal)
1750 {
1751         int err;
1752
1753         spin_lock(&journal->j_state_lock);
1754         if (journal->j_flags & JFS_ABORT)
1755                 err = -EROFS;
1756         else
1757                 err = journal->j_errno;
1758         spin_unlock(&journal->j_state_lock);
1759         return err;
1760 }
1761
1762 /**
1763  * int journal_clear_err () - clears the journal's error state
1764  * @journal: journal to act on.
1765  *
1766  * An error must be cleared or Acked to take a FS out of readonly
1767  * mode.
1768  */
1769 int journal_clear_err(journal_t *journal)
1770 {
1771         int err = 0;
1772
1773         spin_lock(&journal->j_state_lock);
1774         if (journal->j_flags & JFS_ABORT)
1775                 err = -EROFS;
1776         else
1777                 journal->j_errno = 0;
1778         spin_unlock(&journal->j_state_lock);
1779         return err;
1780 }
1781
1782 /**
1783  * void journal_ack_err() - Ack journal err.
1784  * @journal: journal to act on.
1785  *
1786  * An error must be cleared or Acked to take a FS out of readonly
1787  * mode.
1788  */
1789 void journal_ack_err(journal_t *journal)
1790 {
1791         spin_lock(&journal->j_state_lock);
1792         if (journal->j_errno)
1793                 journal->j_flags |= JFS_ACK_ERR;
1794         spin_unlock(&journal->j_state_lock);
1795 }
1796
1797 int journal_blocks_per_page(struct inode *inode)
1798 {
1799         return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1800 }
1801
1802 /*
1803  * Journal_head storage management
1804  */
1805 static struct kmem_cache *journal_head_cache;
1806 #ifdef CONFIG_JBD_DEBUG
1807 static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1808 #endif
1809
1810 static int journal_init_journal_head_cache(void)
1811 {
1812         int retval;
1813
1814         J_ASSERT(journal_head_cache == NULL);
1815         journal_head_cache = kmem_cache_create("journal_head",
1816                                 sizeof(struct journal_head),
1817                                 0,              /* offset */
1818                                 SLAB_TEMPORARY, /* flags */
1819                                 NULL);          /* ctor */
1820         retval = 0;
1821         if (!journal_head_cache) {
1822                 retval = -ENOMEM;
1823                 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1824         }
1825         return retval;
1826 }
1827
1828 static void journal_destroy_journal_head_cache(void)
1829 {
1830         if (journal_head_cache) {
1831                 kmem_cache_destroy(journal_head_cache);
1832                 journal_head_cache = NULL;
1833         }
1834 }
1835
1836 /*
1837  * journal_head splicing and dicing
1838  */
1839 static struct journal_head *journal_alloc_journal_head(void)
1840 {
1841         struct journal_head *ret;
1842
1843 #ifdef CONFIG_JBD_DEBUG
1844         atomic_inc(&nr_journal_heads);
1845 #endif
1846         ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
1847         if (ret == NULL) {
1848                 jbd_debug(1, "out of memory for journal_head\n");
1849                 printk_ratelimited(KERN_NOTICE "ENOMEM in %s, retrying.\n",
1850                                    __func__);
1851
1852                 while (ret == NULL) {
1853                         yield();
1854                         ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
1855                 }
1856         }
1857         return ret;
1858 }
1859
1860 static void journal_free_journal_head(struct journal_head *jh)
1861 {
1862 #ifdef CONFIG_JBD_DEBUG
1863         atomic_dec(&nr_journal_heads);
1864         memset(jh, JBD_POISON_FREE, sizeof(*jh));
1865 #endif
1866         kmem_cache_free(journal_head_cache, jh);
1867 }
1868
1869 /*
1870  * A journal_head is attached to a buffer_head whenever JBD has an
1871  * interest in the buffer.
1872  *
1873  * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
1874  * is set.  This bit is tested in core kernel code where we need to take
1875  * JBD-specific actions.  Testing the zeroness of ->b_private is not reliable
1876  * there.
1877  *
1878  * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
1879  *
1880  * When a buffer has its BH_JBD bit set it is immune from being released by
1881  * core kernel code, mainly via ->b_count.
1882  *
1883  * A journal_head is detached from its buffer_head when the journal_head's
1884  * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint
1885  * transaction (b_cp_transaction) hold their references to b_jcount.
1886  *
1887  * Various places in the kernel want to attach a journal_head to a buffer_head
1888  * _before_ attaching the journal_head to a transaction.  To protect the
1889  * journal_head in this situation, journal_add_journal_head elevates the
1890  * journal_head's b_jcount refcount by one.  The caller must call
1891  * journal_put_journal_head() to undo this.
1892  *
1893  * So the typical usage would be:
1894  *
1895  *      (Attach a journal_head if needed.  Increments b_jcount)
1896  *      struct journal_head *jh = journal_add_journal_head(bh);
1897  *      ...
1898  *      (Get another reference for transaction)
1899  *      journal_grab_journal_head(bh);
1900  *      jh->b_transaction = xxx;
1901  *      (Put original reference)
1902  *      journal_put_journal_head(jh);
1903  */
1904
1905 /*
1906  * Give a buffer_head a journal_head.
1907  *
1908  * May sleep.
1909  */
1910 struct journal_head *journal_add_journal_head(struct buffer_head *bh)
1911 {
1912         struct journal_head *jh;
1913         struct journal_head *new_jh = NULL;
1914
1915 repeat:
1916         if (!buffer_jbd(bh)) {
1917                 new_jh = journal_alloc_journal_head();
1918                 memset(new_jh, 0, sizeof(*new_jh));
1919         }
1920
1921         jbd_lock_bh_journal_head(bh);
1922         if (buffer_jbd(bh)) {
1923                 jh = bh2jh(bh);
1924         } else {
1925                 J_ASSERT_BH(bh,
1926                         (atomic_read(&bh->b_count) > 0) ||
1927                         (bh->b_page && bh->b_page->mapping));
1928
1929                 if (!new_jh) {
1930                         jbd_unlock_bh_journal_head(bh);
1931                         goto repeat;
1932                 }
1933
1934                 jh = new_jh;
1935                 new_jh = NULL;          /* We consumed it */
1936                 set_buffer_jbd(bh);
1937                 bh->b_private = jh;
1938                 jh->b_bh = bh;
1939                 get_bh(bh);
1940                 BUFFER_TRACE(bh, "added journal_head");
1941         }
1942         jh->b_jcount++;
1943         jbd_unlock_bh_journal_head(bh);
1944         if (new_jh)
1945                 journal_free_journal_head(new_jh);
1946         return bh->b_private;
1947 }
1948
1949 /*
1950  * Grab a ref against this buffer_head's journal_head.  If it ended up not
1951  * having a journal_head, return NULL
1952  */
1953 struct journal_head *journal_grab_journal_head(struct buffer_head *bh)
1954 {
1955         struct journal_head *jh = NULL;
1956
1957         jbd_lock_bh_journal_head(bh);
1958         if (buffer_jbd(bh)) {
1959                 jh = bh2jh(bh);
1960                 jh->b_jcount++;
1961         }
1962         jbd_unlock_bh_journal_head(bh);
1963         return jh;
1964 }
1965
1966 static void __journal_remove_journal_head(struct buffer_head *bh)
1967 {
1968         struct journal_head *jh = bh2jh(bh);
1969
1970         J_ASSERT_JH(jh, jh->b_jcount >= 0);
1971         J_ASSERT_JH(jh, jh->b_transaction == NULL);
1972         J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
1973         J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
1974         J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
1975         J_ASSERT_BH(bh, buffer_jbd(bh));
1976         J_ASSERT_BH(bh, jh2bh(jh) == bh);
1977         BUFFER_TRACE(bh, "remove journal_head");
1978         if (jh->b_frozen_data) {
1979                 printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
1980                 jbd_free(jh->b_frozen_data, bh->b_size);
1981         }
1982         if (jh->b_committed_data) {
1983                 printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
1984                 jbd_free(jh->b_committed_data, bh->b_size);
1985         }
1986         bh->b_private = NULL;
1987         jh->b_bh = NULL;        /* debug, really */
1988         clear_buffer_jbd(bh);
1989         journal_free_journal_head(jh);
1990 }
1991
1992 /*
1993  * Drop a reference on the passed journal_head.  If it fell to zero then
1994  * release the journal_head from the buffer_head.
1995  */
1996 void journal_put_journal_head(struct journal_head *jh)
1997 {
1998         struct buffer_head *bh = jh2bh(jh);
1999
2000         jbd_lock_bh_journal_head(bh);
2001         J_ASSERT_JH(jh, jh->b_jcount > 0);
2002         --jh->b_jcount;
2003         if (!jh->b_jcount) {
2004                 __journal_remove_journal_head(bh);
2005                 jbd_unlock_bh_journal_head(bh);
2006                 __brelse(bh);
2007         } else
2008                 jbd_unlock_bh_journal_head(bh);
2009 }
2010
2011 /*
2012  * debugfs tunables
2013  */
2014 #ifdef CONFIG_JBD_DEBUG
2015
2016 u8 journal_enable_debug __read_mostly;
2017 EXPORT_SYMBOL(journal_enable_debug);
2018
2019 static struct dentry *jbd_debugfs_dir;
2020 static struct dentry *jbd_debug;
2021
2022 static void __init jbd_create_debugfs_entry(void)
2023 {
2024         jbd_debugfs_dir = debugfs_create_dir("jbd", NULL);
2025         if (jbd_debugfs_dir)
2026                 jbd_debug = debugfs_create_u8("jbd-debug", S_IRUGO | S_IWUSR,
2027                                                jbd_debugfs_dir,
2028                                                &journal_enable_debug);
2029 }
2030
2031 static void __exit jbd_remove_debugfs_entry(void)
2032 {
2033         debugfs_remove(jbd_debug);
2034         debugfs_remove(jbd_debugfs_dir);
2035 }
2036
2037 #else
2038
2039 static inline void jbd_create_debugfs_entry(void)
2040 {
2041 }
2042
2043 static inline void jbd_remove_debugfs_entry(void)
2044 {
2045 }
2046
2047 #endif
2048
2049 struct kmem_cache *jbd_handle_cache;
2050
2051 static int __init journal_init_handle_cache(void)
2052 {
2053         jbd_handle_cache = kmem_cache_create("journal_handle",
2054                                 sizeof(handle_t),
2055                                 0,              /* offset */
2056                                 SLAB_TEMPORARY, /* flags */
2057                                 NULL);          /* ctor */
2058         if (jbd_handle_cache == NULL) {
2059                 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2060                 return -ENOMEM;
2061         }
2062         return 0;
2063 }
2064
2065 static void journal_destroy_handle_cache(void)
2066 {
2067         if (jbd_handle_cache)
2068                 kmem_cache_destroy(jbd_handle_cache);
2069 }
2070
2071 /*
2072  * Module startup and shutdown
2073  */
2074
2075 static int __init journal_init_caches(void)
2076 {
2077         int ret;
2078
2079         ret = journal_init_revoke_caches();
2080         if (ret == 0)
2081                 ret = journal_init_journal_head_cache();
2082         if (ret == 0)
2083                 ret = journal_init_handle_cache();
2084         return ret;
2085 }
2086
2087 static void journal_destroy_caches(void)
2088 {
2089         journal_destroy_revoke_caches();
2090         journal_destroy_journal_head_cache();
2091         journal_destroy_handle_cache();
2092 }
2093
2094 static int __init journal_init(void)
2095 {
2096         int ret;
2097
2098         BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2099
2100         ret = journal_init_caches();
2101         if (ret != 0)
2102                 journal_destroy_caches();
2103         jbd_create_debugfs_entry();
2104         return ret;
2105 }
2106
2107 static void __exit journal_exit(void)
2108 {
2109 #ifdef CONFIG_JBD_DEBUG
2110         int n = atomic_read(&nr_journal_heads);
2111         if (n)
2112                 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2113 #endif
2114         jbd_remove_debugfs_entry();
2115         journal_destroy_caches();
2116 }
2117
2118 MODULE_LICENSE("GPL");
2119 module_init(journal_init);
2120 module_exit(journal_exit);
2121