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