ARM: 7409/1: Do not call flush_cache_user_range with mmap_sem held
[linux/fpc-iii.git] / fs / jbd / journal.c
blob9f36384e2e8779739451d97ed1d8182ad358bc69
1 /*
2 * linux/fs/jbd/journal.c
4 * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
6 * Copyright 1998 Red Hat corp --- All Rights Reserved
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.
12 * Generic filesystem journal-writing code; part of the ext2fs
13 * journaling system.
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.
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).
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>
41 #include <asm/uaccess.h>
42 #include <asm/page.h>
44 EXPORT_SYMBOL(journal_start);
45 EXPORT_SYMBOL(journal_restart);
46 EXPORT_SYMBOL(journal_extend);
47 EXPORT_SYMBOL(journal_stop);
48 EXPORT_SYMBOL(journal_lock_updates);
49 EXPORT_SYMBOL(journal_unlock_updates);
50 EXPORT_SYMBOL(journal_get_write_access);
51 EXPORT_SYMBOL(journal_get_create_access);
52 EXPORT_SYMBOL(journal_get_undo_access);
53 EXPORT_SYMBOL(journal_dirty_data);
54 EXPORT_SYMBOL(journal_dirty_metadata);
55 EXPORT_SYMBOL(journal_release_buffer);
56 EXPORT_SYMBOL(journal_forget);
57 #if 0
58 EXPORT_SYMBOL(journal_sync_buffer);
59 #endif
60 EXPORT_SYMBOL(journal_flush);
61 EXPORT_SYMBOL(journal_revoke);
63 EXPORT_SYMBOL(journal_init_dev);
64 EXPORT_SYMBOL(journal_init_inode);
65 EXPORT_SYMBOL(journal_update_format);
66 EXPORT_SYMBOL(journal_check_used_features);
67 EXPORT_SYMBOL(journal_check_available_features);
68 EXPORT_SYMBOL(journal_set_features);
69 EXPORT_SYMBOL(journal_create);
70 EXPORT_SYMBOL(journal_load);
71 EXPORT_SYMBOL(journal_destroy);
72 EXPORT_SYMBOL(journal_abort);
73 EXPORT_SYMBOL(journal_errno);
74 EXPORT_SYMBOL(journal_ack_err);
75 EXPORT_SYMBOL(journal_clear_err);
76 EXPORT_SYMBOL(log_wait_commit);
77 EXPORT_SYMBOL(log_start_commit);
78 EXPORT_SYMBOL(journal_start_commit);
79 EXPORT_SYMBOL(journal_force_commit_nested);
80 EXPORT_SYMBOL(journal_wipe);
81 EXPORT_SYMBOL(journal_blocks_per_page);
82 EXPORT_SYMBOL(journal_invalidatepage);
83 EXPORT_SYMBOL(journal_try_to_free_buffers);
84 EXPORT_SYMBOL(journal_force_commit);
86 static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
87 static void __journal_abort_soft (journal_t *journal, int errno);
88 static const char *journal_dev_name(journal_t *journal, char *buffer);
91 * Helper function used to manage commit timeouts
94 static void commit_timeout(unsigned long __data)
96 struct task_struct * p = (struct task_struct *) __data;
98 wake_up_process(p);
102 * kjournald: The main thread function used to manage a logging device
103 * journal.
105 * This kernel thread is responsible for two things:
107 * 1) COMMIT: Every so often we need to commit the current state of the
108 * filesystem to disk. The journal thread is responsible for writing
109 * all of the metadata buffers to disk.
111 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
112 * of the data in that part of the log has been rewritten elsewhere on
113 * the disk. Flushing these old buffers to reclaim space in the log is
114 * known as checkpointing, and this thread is responsible for that job.
117 static int kjournald(void *arg)
119 journal_t *journal = arg;
120 transaction_t *transaction;
123 * Set up an interval timer which can be used to trigger a commit wakeup
124 * after the commit interval expires
126 setup_timer(&journal->j_commit_timer, commit_timeout,
127 (unsigned long)current);
129 /* Record that the journal thread is running */
130 journal->j_task = current;
131 wake_up(&journal->j_wait_done_commit);
133 printk(KERN_INFO "kjournald starting. Commit interval %ld seconds\n",
134 journal->j_commit_interval / HZ);
137 * And now, wait forever for commit wakeup events.
139 spin_lock(&journal->j_state_lock);
141 loop:
142 if (journal->j_flags & JFS_UNMOUNT)
143 goto end_loop;
145 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
146 journal->j_commit_sequence, journal->j_commit_request);
148 if (journal->j_commit_sequence != journal->j_commit_request) {
149 jbd_debug(1, "OK, requests differ\n");
150 spin_unlock(&journal->j_state_lock);
151 del_timer_sync(&journal->j_commit_timer);
152 journal_commit_transaction(journal);
153 spin_lock(&journal->j_state_lock);
154 goto loop;
157 wake_up(&journal->j_wait_done_commit);
158 if (freezing(current)) {
160 * The simpler the better. Flushing journal isn't a
161 * good idea, because that depends on threads that may
162 * be already stopped.
164 jbd_debug(1, "Now suspending kjournald\n");
165 spin_unlock(&journal->j_state_lock);
166 refrigerator();
167 spin_lock(&journal->j_state_lock);
168 } else {
170 * We assume on resume that commits are already there,
171 * so we don't sleep
173 DEFINE_WAIT(wait);
174 int should_sleep = 1;
176 prepare_to_wait(&journal->j_wait_commit, &wait,
177 TASK_INTERRUPTIBLE);
178 if (journal->j_commit_sequence != journal->j_commit_request)
179 should_sleep = 0;
180 transaction = journal->j_running_transaction;
181 if (transaction && time_after_eq(jiffies,
182 transaction->t_expires))
183 should_sleep = 0;
184 if (journal->j_flags & JFS_UNMOUNT)
185 should_sleep = 0;
186 if (should_sleep) {
187 spin_unlock(&journal->j_state_lock);
188 schedule();
189 spin_lock(&journal->j_state_lock);
191 finish_wait(&journal->j_wait_commit, &wait);
194 jbd_debug(1, "kjournald wakes\n");
197 * Were we woken up by a commit wakeup event?
199 transaction = journal->j_running_transaction;
200 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
201 journal->j_commit_request = transaction->t_tid;
202 jbd_debug(1, "woke because of timeout\n");
204 goto loop;
206 end_loop:
207 spin_unlock(&journal->j_state_lock);
208 del_timer_sync(&journal->j_commit_timer);
209 journal->j_task = NULL;
210 wake_up(&journal->j_wait_done_commit);
211 jbd_debug(1, "Journal thread exiting.\n");
212 return 0;
215 static int journal_start_thread(journal_t *journal)
217 struct task_struct *t;
219 t = kthread_run(kjournald, journal, "kjournald");
220 if (IS_ERR(t))
221 return PTR_ERR(t);
223 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
224 return 0;
227 static void journal_kill_thread(journal_t *journal)
229 spin_lock(&journal->j_state_lock);
230 journal->j_flags |= JFS_UNMOUNT;
232 while (journal->j_task) {
233 wake_up(&journal->j_wait_commit);
234 spin_unlock(&journal->j_state_lock);
235 wait_event(journal->j_wait_done_commit,
236 journal->j_task == NULL);
237 spin_lock(&journal->j_state_lock);
239 spin_unlock(&journal->j_state_lock);
243 * journal_write_metadata_buffer: write a metadata buffer to the journal.
245 * Writes a metadata buffer to a given disk block. The actual IO is not
246 * performed but a new buffer_head is constructed which labels the data
247 * to be written with the correct destination disk block.
249 * Any magic-number escaping which needs to be done will cause a
250 * copy-out here. If the buffer happens to start with the
251 * JFS_MAGIC_NUMBER, then we can't write it to the log directly: the
252 * magic number is only written to the log for descripter blocks. In
253 * this case, we copy the data and replace the first word with 0, and we
254 * return a result code which indicates that this buffer needs to be
255 * marked as an escaped buffer in the corresponding log descriptor
256 * block. The missing word can then be restored when the block is read
257 * during recovery.
259 * If the source buffer has already been modified by a new transaction
260 * since we took the last commit snapshot, we use the frozen copy of
261 * that data for IO. If we end up using the existing buffer_head's data
262 * for the write, then we *have* to lock the buffer to prevent anyone
263 * else from using and possibly modifying it while the IO is in
264 * progress.
266 * The function returns a pointer to the buffer_heads to be used for IO.
268 * We assume that the journal has already been locked in this function.
270 * Return value:
271 * <0: Error
272 * >=0: Finished OK
274 * On success:
275 * Bit 0 set == escape performed on the data
276 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
279 int journal_write_metadata_buffer(transaction_t *transaction,
280 struct journal_head *jh_in,
281 struct journal_head **jh_out,
282 unsigned int blocknr)
284 int need_copy_out = 0;
285 int done_copy_out = 0;
286 int do_escape = 0;
287 char *mapped_data;
288 struct buffer_head *new_bh;
289 struct journal_head *new_jh;
290 struct page *new_page;
291 unsigned int new_offset;
292 struct buffer_head *bh_in = jh2bh(jh_in);
293 journal_t *journal = transaction->t_journal;
296 * The buffer really shouldn't be locked: only the current committing
297 * transaction is allowed to write it, so nobody else is allowed
298 * to do any IO.
300 * akpm: except if we're journalling data, and write() output is
301 * also part of a shared mapping, and another thread has
302 * decided to launch a writepage() against this buffer.
304 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
306 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
307 /* keep subsequent assertions sane */
308 new_bh->b_state = 0;
309 init_buffer(new_bh, NULL, NULL);
310 atomic_set(&new_bh->b_count, 1);
311 new_jh = journal_add_journal_head(new_bh); /* This sleeps */
314 * If a new transaction has already done a buffer copy-out, then
315 * we use that version of the data for the commit.
317 jbd_lock_bh_state(bh_in);
318 repeat:
319 if (jh_in->b_frozen_data) {
320 done_copy_out = 1;
321 new_page = virt_to_page(jh_in->b_frozen_data);
322 new_offset = offset_in_page(jh_in->b_frozen_data);
323 } else {
324 new_page = jh2bh(jh_in)->b_page;
325 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
328 mapped_data = kmap_atomic(new_page, KM_USER0);
330 * Check for escaping
332 if (*((__be32 *)(mapped_data + new_offset)) ==
333 cpu_to_be32(JFS_MAGIC_NUMBER)) {
334 need_copy_out = 1;
335 do_escape = 1;
337 kunmap_atomic(mapped_data, KM_USER0);
340 * Do we need to do a data copy?
342 if (need_copy_out && !done_copy_out) {
343 char *tmp;
345 jbd_unlock_bh_state(bh_in);
346 tmp = jbd_alloc(bh_in->b_size, GFP_NOFS);
347 jbd_lock_bh_state(bh_in);
348 if (jh_in->b_frozen_data) {
349 jbd_free(tmp, bh_in->b_size);
350 goto repeat;
353 jh_in->b_frozen_data = tmp;
354 mapped_data = kmap_atomic(new_page, KM_USER0);
355 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
356 kunmap_atomic(mapped_data, KM_USER0);
358 new_page = virt_to_page(tmp);
359 new_offset = offset_in_page(tmp);
360 done_copy_out = 1;
364 * Did we need to do an escaping? Now we've done all the
365 * copying, we can finally do so.
367 if (do_escape) {
368 mapped_data = kmap_atomic(new_page, KM_USER0);
369 *((unsigned int *)(mapped_data + new_offset)) = 0;
370 kunmap_atomic(mapped_data, KM_USER0);
373 set_bh_page(new_bh, new_page, new_offset);
374 new_jh->b_transaction = NULL;
375 new_bh->b_size = jh2bh(jh_in)->b_size;
376 new_bh->b_bdev = transaction->t_journal->j_dev;
377 new_bh->b_blocknr = blocknr;
378 set_buffer_mapped(new_bh);
379 set_buffer_dirty(new_bh);
381 *jh_out = new_jh;
384 * The to-be-written buffer needs to get moved to the io queue,
385 * and the original buffer whose contents we are shadowing or
386 * copying is moved to the transaction's shadow queue.
388 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
389 spin_lock(&journal->j_list_lock);
390 __journal_file_buffer(jh_in, transaction, BJ_Shadow);
391 spin_unlock(&journal->j_list_lock);
392 jbd_unlock_bh_state(bh_in);
394 JBUFFER_TRACE(new_jh, "file as BJ_IO");
395 journal_file_buffer(new_jh, transaction, BJ_IO);
397 return do_escape | (done_copy_out << 1);
401 * Allocation code for the journal file. Manage the space left in the
402 * journal, so that we can begin checkpointing when appropriate.
406 * __log_space_left: Return the number of free blocks left in the journal.
408 * Called with the journal already locked.
410 * Called under j_state_lock
413 int __log_space_left(journal_t *journal)
415 int left = journal->j_free;
417 assert_spin_locked(&journal->j_state_lock);
420 * Be pessimistic here about the number of those free blocks which
421 * might be required for log descriptor control blocks.
424 #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
426 left -= MIN_LOG_RESERVED_BLOCKS;
428 if (left <= 0)
429 return 0;
430 left -= (left >> 3);
431 return left;
435 * Called under j_state_lock. Returns true if a transaction commit was started.
437 int __log_start_commit(journal_t *journal, tid_t target)
440 * The only transaction we can possibly wait upon is the
441 * currently running transaction (if it exists). Otherwise,
442 * the target tid must be an old one.
444 if (journal->j_running_transaction &&
445 journal->j_running_transaction->t_tid == target) {
447 * We want a new commit: OK, mark the request and wakeup the
448 * commit thread. We do _not_ do the commit ourselves.
451 journal->j_commit_request = target;
452 jbd_debug(1, "JBD: requesting commit %d/%d\n",
453 journal->j_commit_request,
454 journal->j_commit_sequence);
455 wake_up(&journal->j_wait_commit);
456 return 1;
457 } else if (!tid_geq(journal->j_commit_request, target))
458 /* This should never happen, but if it does, preserve
459 the evidence before kjournald goes into a loop and
460 increments j_commit_sequence beyond all recognition. */
461 WARN_ONCE(1, "jbd: bad log_start_commit: %u %u %u %u\n",
462 journal->j_commit_request, journal->j_commit_sequence,
463 target, journal->j_running_transaction ?
464 journal->j_running_transaction->t_tid : 0);
465 return 0;
468 int log_start_commit(journal_t *journal, tid_t tid)
470 int ret;
472 spin_lock(&journal->j_state_lock);
473 ret = __log_start_commit(journal, tid);
474 spin_unlock(&journal->j_state_lock);
475 return ret;
479 * Force and wait upon a commit if the calling process is not within
480 * transaction. This is used for forcing out undo-protected data which contains
481 * bitmaps, when the fs is running out of space.
483 * We can only force the running transaction if we don't have an active handle;
484 * otherwise, we will deadlock.
486 * Returns true if a transaction was started.
488 int journal_force_commit_nested(journal_t *journal)
490 transaction_t *transaction = NULL;
491 tid_t tid;
493 spin_lock(&journal->j_state_lock);
494 if (journal->j_running_transaction && !current->journal_info) {
495 transaction = journal->j_running_transaction;
496 __log_start_commit(journal, transaction->t_tid);
497 } else if (journal->j_committing_transaction)
498 transaction = journal->j_committing_transaction;
500 if (!transaction) {
501 spin_unlock(&journal->j_state_lock);
502 return 0; /* Nothing to retry */
505 tid = transaction->t_tid;
506 spin_unlock(&journal->j_state_lock);
507 log_wait_commit(journal, tid);
508 return 1;
512 * Start a commit of the current running transaction (if any). Returns true
513 * if a transaction is going to be committed (or is currently already
514 * committing), and fills its tid in at *ptid
516 int journal_start_commit(journal_t *journal, tid_t *ptid)
518 int ret = 0;
520 spin_lock(&journal->j_state_lock);
521 if (journal->j_running_transaction) {
522 tid_t tid = journal->j_running_transaction->t_tid;
524 __log_start_commit(journal, tid);
525 /* There's a running transaction and we've just made sure
526 * it's commit has been scheduled. */
527 if (ptid)
528 *ptid = tid;
529 ret = 1;
530 } else if (journal->j_committing_transaction) {
532 * If ext3_write_super() recently started a commit, then we
533 * have to wait for completion of that transaction
535 if (ptid)
536 *ptid = journal->j_committing_transaction->t_tid;
537 ret = 1;
539 spin_unlock(&journal->j_state_lock);
540 return ret;
544 * Wait for a specified commit to complete.
545 * The caller may not hold the journal lock.
547 int log_wait_commit(journal_t *journal, tid_t tid)
549 int err = 0;
551 #ifdef CONFIG_JBD_DEBUG
552 spin_lock(&journal->j_state_lock);
553 if (!tid_geq(journal->j_commit_request, tid)) {
554 printk(KERN_EMERG
555 "%s: error: j_commit_request=%d, tid=%d\n",
556 __func__, journal->j_commit_request, tid);
558 spin_unlock(&journal->j_state_lock);
559 #endif
560 spin_lock(&journal->j_state_lock);
561 while (tid_gt(tid, journal->j_commit_sequence)) {
562 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
563 tid, journal->j_commit_sequence);
564 wake_up(&journal->j_wait_commit);
565 spin_unlock(&journal->j_state_lock);
566 wait_event(journal->j_wait_done_commit,
567 !tid_gt(tid, journal->j_commit_sequence));
568 spin_lock(&journal->j_state_lock);
570 spin_unlock(&journal->j_state_lock);
572 if (unlikely(is_journal_aborted(journal))) {
573 printk(KERN_EMERG "journal commit I/O error\n");
574 err = -EIO;
576 return err;
580 * Return 1 if a given transaction has not yet sent barrier request
581 * connected with a transaction commit. If 0 is returned, transaction
582 * may or may not have sent the barrier. Used to avoid sending barrier
583 * twice in common cases.
585 int journal_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
587 int ret = 0;
588 transaction_t *commit_trans;
590 if (!(journal->j_flags & JFS_BARRIER))
591 return 0;
592 spin_lock(&journal->j_state_lock);
593 /* Transaction already committed? */
594 if (tid_geq(journal->j_commit_sequence, tid))
595 goto out;
597 * Transaction is being committed and we already proceeded to
598 * writing commit record?
600 commit_trans = journal->j_committing_transaction;
601 if (commit_trans && commit_trans->t_tid == tid &&
602 commit_trans->t_state >= T_COMMIT_RECORD)
603 goto out;
604 ret = 1;
605 out:
606 spin_unlock(&journal->j_state_lock);
607 return ret;
609 EXPORT_SYMBOL(journal_trans_will_send_data_barrier);
612 * Log buffer allocation routines:
615 int journal_next_log_block(journal_t *journal, unsigned int *retp)
617 unsigned int blocknr;
619 spin_lock(&journal->j_state_lock);
620 J_ASSERT(journal->j_free > 1);
622 blocknr = journal->j_head;
623 journal->j_head++;
624 journal->j_free--;
625 if (journal->j_head == journal->j_last)
626 journal->j_head = journal->j_first;
627 spin_unlock(&journal->j_state_lock);
628 return journal_bmap(journal, blocknr, retp);
632 * Conversion of logical to physical block numbers for the journal
634 * On external journals the journal blocks are identity-mapped, so
635 * this is a no-op. If needed, we can use j_blk_offset - everything is
636 * ready.
638 int journal_bmap(journal_t *journal, unsigned int blocknr,
639 unsigned int *retp)
641 int err = 0;
642 unsigned int ret;
644 if (journal->j_inode) {
645 ret = bmap(journal->j_inode, blocknr);
646 if (ret)
647 *retp = ret;
648 else {
649 char b[BDEVNAME_SIZE];
651 printk(KERN_ALERT "%s: journal block not found "
652 "at offset %u on %s\n",
653 __func__,
654 blocknr,
655 bdevname(journal->j_dev, b));
656 err = -EIO;
657 __journal_abort_soft(journal, err);
659 } else {
660 *retp = blocknr; /* +journal->j_blk_offset */
662 return err;
666 * We play buffer_head aliasing tricks to write data/metadata blocks to
667 * the journal without copying their contents, but for journal
668 * descriptor blocks we do need to generate bona fide buffers.
670 * After the caller of journal_get_descriptor_buffer() has finished modifying
671 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
672 * But we don't bother doing that, so there will be coherency problems with
673 * mmaps of blockdevs which hold live JBD-controlled filesystems.
675 struct journal_head *journal_get_descriptor_buffer(journal_t *journal)
677 struct buffer_head *bh;
678 unsigned int blocknr;
679 int err;
681 err = journal_next_log_block(journal, &blocknr);
683 if (err)
684 return NULL;
686 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
687 if (!bh)
688 return NULL;
689 lock_buffer(bh);
690 memset(bh->b_data, 0, journal->j_blocksize);
691 set_buffer_uptodate(bh);
692 unlock_buffer(bh);
693 BUFFER_TRACE(bh, "return this buffer");
694 return journal_add_journal_head(bh);
698 * Management for journal control blocks: functions to create and
699 * destroy journal_t structures, and to initialise and read existing
700 * journal blocks from disk. */
702 /* First: create and setup a journal_t object in memory. We initialise
703 * very few fields yet: that has to wait until we have created the
704 * journal structures from from scratch, or loaded them from disk. */
706 static journal_t * journal_init_common (void)
708 journal_t *journal;
709 int err;
711 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
712 if (!journal)
713 goto fail;
715 init_waitqueue_head(&journal->j_wait_transaction_locked);
716 init_waitqueue_head(&journal->j_wait_logspace);
717 init_waitqueue_head(&journal->j_wait_done_commit);
718 init_waitqueue_head(&journal->j_wait_checkpoint);
719 init_waitqueue_head(&journal->j_wait_commit);
720 init_waitqueue_head(&journal->j_wait_updates);
721 mutex_init(&journal->j_barrier);
722 mutex_init(&journal->j_checkpoint_mutex);
723 spin_lock_init(&journal->j_revoke_lock);
724 spin_lock_init(&journal->j_list_lock);
725 spin_lock_init(&journal->j_state_lock);
727 journal->j_commit_interval = (HZ * JBD_DEFAULT_MAX_COMMIT_AGE);
729 /* The journal is marked for error until we succeed with recovery! */
730 journal->j_flags = JFS_ABORT;
732 /* Set up a default-sized revoke table for the new mount. */
733 err = journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
734 if (err) {
735 kfree(journal);
736 goto fail;
738 return journal;
739 fail:
740 return NULL;
743 /* journal_init_dev and journal_init_inode:
745 * Create a journal structure assigned some fixed set of disk blocks to
746 * the journal. We don't actually touch those disk blocks yet, but we
747 * need to set up all of the mapping information to tell the journaling
748 * system where the journal blocks are.
753 * journal_t * journal_init_dev() - creates and initialises a journal structure
754 * @bdev: Block device on which to create the journal
755 * @fs_dev: Device which hold journalled filesystem for this journal.
756 * @start: Block nr Start of journal.
757 * @len: Length of the journal in blocks.
758 * @blocksize: blocksize of journalling device
760 * Returns: a newly created journal_t *
762 * journal_init_dev creates a journal which maps a fixed contiguous
763 * range of blocks on an arbitrary block device.
766 journal_t * journal_init_dev(struct block_device *bdev,
767 struct block_device *fs_dev,
768 int start, int len, int blocksize)
770 journal_t *journal = journal_init_common();
771 struct buffer_head *bh;
772 int n;
774 if (!journal)
775 return NULL;
777 /* journal descriptor can store up to n blocks -bzzz */
778 journal->j_blocksize = blocksize;
779 n = journal->j_blocksize / sizeof(journal_block_tag_t);
780 journal->j_wbufsize = n;
781 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
782 if (!journal->j_wbuf) {
783 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
784 __func__);
785 goto out_err;
787 journal->j_dev = bdev;
788 journal->j_fs_dev = fs_dev;
789 journal->j_blk_offset = start;
790 journal->j_maxlen = len;
792 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
793 if (!bh) {
794 printk(KERN_ERR
795 "%s: Cannot get buffer for journal superblock\n",
796 __func__);
797 goto out_err;
799 journal->j_sb_buffer = bh;
800 journal->j_superblock = (journal_superblock_t *)bh->b_data;
802 return journal;
803 out_err:
804 kfree(journal->j_wbuf);
805 kfree(journal);
806 return NULL;
810 * journal_t * journal_init_inode () - creates a journal which maps to a inode.
811 * @inode: An inode to create the journal in
813 * journal_init_inode creates a journal which maps an on-disk inode as
814 * the journal. The inode must exist already, must support bmap() and
815 * must have all data blocks preallocated.
817 journal_t * journal_init_inode (struct inode *inode)
819 struct buffer_head *bh;
820 journal_t *journal = journal_init_common();
821 int err;
822 int n;
823 unsigned int blocknr;
825 if (!journal)
826 return NULL;
828 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
829 journal->j_inode = inode;
830 jbd_debug(1,
831 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
832 journal, inode->i_sb->s_id, inode->i_ino,
833 (long long) inode->i_size,
834 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
836 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
837 journal->j_blocksize = inode->i_sb->s_blocksize;
839 /* journal descriptor can store up to n blocks -bzzz */
840 n = journal->j_blocksize / sizeof(journal_block_tag_t);
841 journal->j_wbufsize = n;
842 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
843 if (!journal->j_wbuf) {
844 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
845 __func__);
846 goto out_err;
849 err = journal_bmap(journal, 0, &blocknr);
850 /* If that failed, give up */
851 if (err) {
852 printk(KERN_ERR "%s: Cannot locate journal superblock\n",
853 __func__);
854 goto out_err;
857 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
858 if (!bh) {
859 printk(KERN_ERR
860 "%s: Cannot get buffer for journal superblock\n",
861 __func__);
862 goto out_err;
864 journal->j_sb_buffer = bh;
865 journal->j_superblock = (journal_superblock_t *)bh->b_data;
867 return journal;
868 out_err:
869 kfree(journal->j_wbuf);
870 kfree(journal);
871 return NULL;
875 * If the journal init or create aborts, we need to mark the journal
876 * superblock as being NULL to prevent the journal destroy from writing
877 * back a bogus superblock.
879 static void journal_fail_superblock (journal_t *journal)
881 struct buffer_head *bh = journal->j_sb_buffer;
882 brelse(bh);
883 journal->j_sb_buffer = NULL;
887 * Given a journal_t structure, initialise the various fields for
888 * startup of a new journaling session. We use this both when creating
889 * a journal, and after recovering an old journal to reset it for
890 * subsequent use.
893 static int journal_reset(journal_t *journal)
895 journal_superblock_t *sb = journal->j_superblock;
896 unsigned int first, last;
898 first = be32_to_cpu(sb->s_first);
899 last = be32_to_cpu(sb->s_maxlen);
900 if (first + JFS_MIN_JOURNAL_BLOCKS > last + 1) {
901 printk(KERN_ERR "JBD: Journal too short (blocks %u-%u).\n",
902 first, last);
903 journal_fail_superblock(journal);
904 return -EINVAL;
907 journal->j_first = first;
908 journal->j_last = last;
910 journal->j_head = first;
911 journal->j_tail = first;
912 journal->j_free = last - first;
914 journal->j_tail_sequence = journal->j_transaction_sequence;
915 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
916 journal->j_commit_request = journal->j_commit_sequence;
918 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
920 /* Add the dynamic fields and write it to disk. */
921 journal_update_superblock(journal, 1);
922 return journal_start_thread(journal);
926 * int journal_create() - Initialise the new journal file
927 * @journal: Journal to create. This structure must have been initialised
929 * Given a journal_t structure which tells us which disk blocks we can
930 * use, create a new journal superblock and initialise all of the
931 * journal fields from scratch.
933 int journal_create(journal_t *journal)
935 unsigned int blocknr;
936 struct buffer_head *bh;
937 journal_superblock_t *sb;
938 int i, err;
940 if (journal->j_maxlen < JFS_MIN_JOURNAL_BLOCKS) {
941 printk (KERN_ERR "Journal length (%d blocks) too short.\n",
942 journal->j_maxlen);
943 journal_fail_superblock(journal);
944 return -EINVAL;
947 if (journal->j_inode == NULL) {
949 * We don't know what block to start at!
951 printk(KERN_EMERG
952 "%s: creation of journal on external device!\n",
953 __func__);
954 BUG();
957 /* Zero out the entire journal on disk. We cannot afford to
958 have any blocks on disk beginning with JFS_MAGIC_NUMBER. */
959 jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
960 for (i = 0; i < journal->j_maxlen; i++) {
961 err = journal_bmap(journal, i, &blocknr);
962 if (err)
963 return err;
964 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
965 if (unlikely(!bh))
966 return -ENOMEM;
967 lock_buffer(bh);
968 memset (bh->b_data, 0, journal->j_blocksize);
969 BUFFER_TRACE(bh, "marking dirty");
970 mark_buffer_dirty(bh);
971 BUFFER_TRACE(bh, "marking uptodate");
972 set_buffer_uptodate(bh);
973 unlock_buffer(bh);
974 __brelse(bh);
977 sync_blockdev(journal->j_dev);
978 jbd_debug(1, "JBD: journal cleared.\n");
980 /* OK, fill in the initial static fields in the new superblock */
981 sb = journal->j_superblock;
983 sb->s_header.h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
984 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
986 sb->s_blocksize = cpu_to_be32(journal->j_blocksize);
987 sb->s_maxlen = cpu_to_be32(journal->j_maxlen);
988 sb->s_first = cpu_to_be32(1);
990 journal->j_transaction_sequence = 1;
992 journal->j_flags &= ~JFS_ABORT;
993 journal->j_format_version = 2;
995 return journal_reset(journal);
999 * void journal_update_superblock() - Update journal sb on disk.
1000 * @journal: The journal to update.
1001 * @wait: Set to '0' if you don't want to wait for IO completion.
1003 * Update a journal's dynamic superblock fields and write it to disk,
1004 * optionally waiting for the IO to complete.
1006 void journal_update_superblock(journal_t *journal, int wait)
1008 journal_superblock_t *sb = journal->j_superblock;
1009 struct buffer_head *bh = journal->j_sb_buffer;
1012 * As a special case, if the on-disk copy is already marked as needing
1013 * no recovery (s_start == 0) and there are no outstanding transactions
1014 * in the filesystem, then we can safely defer the superblock update
1015 * until the next commit by setting JFS_FLUSHED. This avoids
1016 * attempting a write to a potential-readonly device.
1018 if (sb->s_start == 0 && journal->j_tail_sequence ==
1019 journal->j_transaction_sequence) {
1020 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
1021 "(start %u, seq %d, errno %d)\n",
1022 journal->j_tail, journal->j_tail_sequence,
1023 journal->j_errno);
1024 goto out;
1027 if (buffer_write_io_error(bh)) {
1028 char b[BDEVNAME_SIZE];
1030 * Oh, dear. A previous attempt to write the journal
1031 * superblock failed. This could happen because the
1032 * USB device was yanked out. Or it could happen to
1033 * be a transient write error and maybe the block will
1034 * be remapped. Nothing we can do but to retry the
1035 * write and hope for the best.
1037 printk(KERN_ERR "JBD: previous I/O error detected "
1038 "for journal superblock update for %s.\n",
1039 journal_dev_name(journal, b));
1040 clear_buffer_write_io_error(bh);
1041 set_buffer_uptodate(bh);
1044 spin_lock(&journal->j_state_lock);
1045 jbd_debug(1,"JBD: updating superblock (start %u, seq %d, errno %d)\n",
1046 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1048 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1049 sb->s_start = cpu_to_be32(journal->j_tail);
1050 sb->s_errno = cpu_to_be32(journal->j_errno);
1051 spin_unlock(&journal->j_state_lock);
1053 BUFFER_TRACE(bh, "marking dirty");
1054 mark_buffer_dirty(bh);
1055 if (wait) {
1056 sync_dirty_buffer(bh);
1057 if (buffer_write_io_error(bh)) {
1058 char b[BDEVNAME_SIZE];
1059 printk(KERN_ERR "JBD: I/O error detected "
1060 "when updating journal superblock for %s.\n",
1061 journal_dev_name(journal, b));
1062 clear_buffer_write_io_error(bh);
1063 set_buffer_uptodate(bh);
1065 } else
1066 write_dirty_buffer(bh, WRITE);
1068 out:
1069 /* If we have just flushed the log (by marking s_start==0), then
1070 * any future commit will have to be careful to update the
1071 * superblock again to re-record the true start of the log. */
1073 spin_lock(&journal->j_state_lock);
1074 if (sb->s_start)
1075 journal->j_flags &= ~JFS_FLUSHED;
1076 else
1077 journal->j_flags |= JFS_FLUSHED;
1078 spin_unlock(&journal->j_state_lock);
1082 * Read the superblock for a given journal, performing initial
1083 * validation of the format.
1086 static int journal_get_superblock(journal_t *journal)
1088 struct buffer_head *bh;
1089 journal_superblock_t *sb;
1090 int err = -EIO;
1092 bh = journal->j_sb_buffer;
1094 J_ASSERT(bh != NULL);
1095 if (!buffer_uptodate(bh)) {
1096 ll_rw_block(READ, 1, &bh);
1097 wait_on_buffer(bh);
1098 if (!buffer_uptodate(bh)) {
1099 printk (KERN_ERR
1100 "JBD: IO error reading journal superblock\n");
1101 goto out;
1105 sb = journal->j_superblock;
1107 err = -EINVAL;
1109 if (sb->s_header.h_magic != cpu_to_be32(JFS_MAGIC_NUMBER) ||
1110 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1111 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1112 goto out;
1115 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1116 case JFS_SUPERBLOCK_V1:
1117 journal->j_format_version = 1;
1118 break;
1119 case JFS_SUPERBLOCK_V2:
1120 journal->j_format_version = 2;
1121 break;
1122 default:
1123 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1124 goto out;
1127 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1128 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1129 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1130 printk (KERN_WARNING "JBD: journal file too short\n");
1131 goto out;
1134 if (be32_to_cpu(sb->s_first) == 0 ||
1135 be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
1136 printk(KERN_WARNING
1137 "JBD: Invalid start block of journal: %u\n",
1138 be32_to_cpu(sb->s_first));
1139 goto out;
1142 return 0;
1144 out:
1145 journal_fail_superblock(journal);
1146 return err;
1150 * Load the on-disk journal superblock and read the key fields into the
1151 * journal_t.
1154 static int load_superblock(journal_t *journal)
1156 int err;
1157 journal_superblock_t *sb;
1159 err = journal_get_superblock(journal);
1160 if (err)
1161 return err;
1163 sb = journal->j_superblock;
1165 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1166 journal->j_tail = be32_to_cpu(sb->s_start);
1167 journal->j_first = be32_to_cpu(sb->s_first);
1168 journal->j_last = be32_to_cpu(sb->s_maxlen);
1169 journal->j_errno = be32_to_cpu(sb->s_errno);
1171 return 0;
1176 * int journal_load() - Read journal from disk.
1177 * @journal: Journal to act on.
1179 * Given a journal_t structure which tells us which disk blocks contain
1180 * a journal, read the journal from disk to initialise the in-memory
1181 * structures.
1183 int journal_load(journal_t *journal)
1185 int err;
1186 journal_superblock_t *sb;
1188 err = load_superblock(journal);
1189 if (err)
1190 return err;
1192 sb = journal->j_superblock;
1193 /* If this is a V2 superblock, then we have to check the
1194 * features flags on it. */
1196 if (journal->j_format_version >= 2) {
1197 if ((sb->s_feature_ro_compat &
1198 ~cpu_to_be32(JFS_KNOWN_ROCOMPAT_FEATURES)) ||
1199 (sb->s_feature_incompat &
1200 ~cpu_to_be32(JFS_KNOWN_INCOMPAT_FEATURES))) {
1201 printk (KERN_WARNING
1202 "JBD: Unrecognised features on journal\n");
1203 return -EINVAL;
1207 /* Let the recovery code check whether it needs to recover any
1208 * data from the journal. */
1209 if (journal_recover(journal))
1210 goto recovery_error;
1212 /* OK, we've finished with the dynamic journal bits:
1213 * reinitialise the dynamic contents of the superblock in memory
1214 * and reset them on disk. */
1215 if (journal_reset(journal))
1216 goto recovery_error;
1218 journal->j_flags &= ~JFS_ABORT;
1219 journal->j_flags |= JFS_LOADED;
1220 return 0;
1222 recovery_error:
1223 printk (KERN_WARNING "JBD: recovery failed\n");
1224 return -EIO;
1228 * void journal_destroy() - Release a journal_t structure.
1229 * @journal: Journal to act on.
1231 * Release a journal_t structure once it is no longer in use by the
1232 * journaled object.
1233 * Return <0 if we couldn't clean up the journal.
1235 int journal_destroy(journal_t *journal)
1237 int err = 0;
1240 /* Wait for the commit thread to wake up and die. */
1241 journal_kill_thread(journal);
1243 /* Force a final log commit */
1244 if (journal->j_running_transaction)
1245 journal_commit_transaction(journal);
1247 /* Force any old transactions to disk */
1249 /* Totally anal locking here... */
1250 spin_lock(&journal->j_list_lock);
1251 while (journal->j_checkpoint_transactions != NULL) {
1252 spin_unlock(&journal->j_list_lock);
1253 log_do_checkpoint(journal);
1254 spin_lock(&journal->j_list_lock);
1257 J_ASSERT(journal->j_running_transaction == NULL);
1258 J_ASSERT(journal->j_committing_transaction == NULL);
1259 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1260 spin_unlock(&journal->j_list_lock);
1262 if (journal->j_sb_buffer) {
1263 if (!is_journal_aborted(journal)) {
1264 /* We can now mark the journal as empty. */
1265 journal->j_tail = 0;
1266 journal->j_tail_sequence =
1267 ++journal->j_transaction_sequence;
1268 journal_update_superblock(journal, 1);
1269 } else {
1270 err = -EIO;
1272 brelse(journal->j_sb_buffer);
1275 if (journal->j_inode)
1276 iput(journal->j_inode);
1277 if (journal->j_revoke)
1278 journal_destroy_revoke(journal);
1279 kfree(journal->j_wbuf);
1280 kfree(journal);
1282 return err;
1287 *int journal_check_used_features () - Check if features specified are used.
1288 * @journal: Journal to check.
1289 * @compat: bitmask of compatible features
1290 * @ro: bitmask of features that force read-only mount
1291 * @incompat: bitmask of incompatible features
1293 * Check whether the journal uses all of a given set of
1294 * features. Return true (non-zero) if it does.
1297 int journal_check_used_features (journal_t *journal, unsigned long compat,
1298 unsigned long ro, unsigned long incompat)
1300 journal_superblock_t *sb;
1302 if (!compat && !ro && !incompat)
1303 return 1;
1304 if (journal->j_format_version == 1)
1305 return 0;
1307 sb = journal->j_superblock;
1309 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1310 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1311 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1312 return 1;
1314 return 0;
1318 * int journal_check_available_features() - Check feature set in journalling layer
1319 * @journal: Journal to check.
1320 * @compat: bitmask of compatible features
1321 * @ro: bitmask of features that force read-only mount
1322 * @incompat: bitmask of incompatible features
1324 * Check whether the journaling code supports the use of
1325 * all of a given set of features on this journal. Return true
1326 * (non-zero) if it can. */
1328 int journal_check_available_features (journal_t *journal, unsigned long compat,
1329 unsigned long ro, unsigned long incompat)
1331 if (!compat && !ro && !incompat)
1332 return 1;
1334 /* We can support any known requested features iff the
1335 * superblock is in version 2. Otherwise we fail to support any
1336 * extended sb features. */
1338 if (journal->j_format_version != 2)
1339 return 0;
1341 if ((compat & JFS_KNOWN_COMPAT_FEATURES) == compat &&
1342 (ro & JFS_KNOWN_ROCOMPAT_FEATURES) == ro &&
1343 (incompat & JFS_KNOWN_INCOMPAT_FEATURES) == incompat)
1344 return 1;
1346 return 0;
1350 * int journal_set_features () - Mark a given journal feature in the superblock
1351 * @journal: Journal to act on.
1352 * @compat: bitmask of compatible features
1353 * @ro: bitmask of features that force read-only mount
1354 * @incompat: bitmask of incompatible features
1356 * Mark a given journal feature as present on the
1357 * superblock. Returns true if the requested features could be set.
1361 int journal_set_features (journal_t *journal, unsigned long compat,
1362 unsigned long ro, unsigned long incompat)
1364 journal_superblock_t *sb;
1366 if (journal_check_used_features(journal, compat, ro, incompat))
1367 return 1;
1369 if (!journal_check_available_features(journal, compat, ro, incompat))
1370 return 0;
1372 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1373 compat, ro, incompat);
1375 sb = journal->j_superblock;
1377 sb->s_feature_compat |= cpu_to_be32(compat);
1378 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1379 sb->s_feature_incompat |= cpu_to_be32(incompat);
1381 return 1;
1386 * int journal_update_format () - Update on-disk journal structure.
1387 * @journal: Journal to act on.
1389 * Given an initialised but unloaded journal struct, poke about in the
1390 * on-disk structure to update it to the most recent supported version.
1392 int journal_update_format (journal_t *journal)
1394 journal_superblock_t *sb;
1395 int err;
1397 err = journal_get_superblock(journal);
1398 if (err)
1399 return err;
1401 sb = journal->j_superblock;
1403 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
1404 case JFS_SUPERBLOCK_V2:
1405 return 0;
1406 case JFS_SUPERBLOCK_V1:
1407 return journal_convert_superblock_v1(journal, sb);
1408 default:
1409 break;
1411 return -EINVAL;
1414 static int journal_convert_superblock_v1(journal_t *journal,
1415 journal_superblock_t *sb)
1417 int offset, blocksize;
1418 struct buffer_head *bh;
1420 printk(KERN_WARNING
1421 "JBD: Converting superblock from version 1 to 2.\n");
1423 /* Pre-initialise new fields to zero */
1424 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1425 blocksize = be32_to_cpu(sb->s_blocksize);
1426 memset(&sb->s_feature_compat, 0, blocksize-offset);
1428 sb->s_nr_users = cpu_to_be32(1);
1429 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
1430 journal->j_format_version = 2;
1432 bh = journal->j_sb_buffer;
1433 BUFFER_TRACE(bh, "marking dirty");
1434 mark_buffer_dirty(bh);
1435 sync_dirty_buffer(bh);
1436 return 0;
1441 * int journal_flush () - Flush journal
1442 * @journal: Journal to act on.
1444 * Flush all data for a given journal to disk and empty the journal.
1445 * Filesystems can use this when remounting readonly to ensure that
1446 * recovery does not need to happen on remount.
1449 int journal_flush(journal_t *journal)
1451 int err = 0;
1452 transaction_t *transaction = NULL;
1453 unsigned int old_tail;
1455 spin_lock(&journal->j_state_lock);
1457 /* Force everything buffered to the log... */
1458 if (journal->j_running_transaction) {
1459 transaction = journal->j_running_transaction;
1460 __log_start_commit(journal, transaction->t_tid);
1461 } else if (journal->j_committing_transaction)
1462 transaction = journal->j_committing_transaction;
1464 /* Wait for the log commit to complete... */
1465 if (transaction) {
1466 tid_t tid = transaction->t_tid;
1468 spin_unlock(&journal->j_state_lock);
1469 log_wait_commit(journal, tid);
1470 } else {
1471 spin_unlock(&journal->j_state_lock);
1474 /* ...and flush everything in the log out to disk. */
1475 spin_lock(&journal->j_list_lock);
1476 while (!err && journal->j_checkpoint_transactions != NULL) {
1477 spin_unlock(&journal->j_list_lock);
1478 mutex_lock(&journal->j_checkpoint_mutex);
1479 err = log_do_checkpoint(journal);
1480 mutex_unlock(&journal->j_checkpoint_mutex);
1481 spin_lock(&journal->j_list_lock);
1483 spin_unlock(&journal->j_list_lock);
1485 if (is_journal_aborted(journal))
1486 return -EIO;
1488 cleanup_journal_tail(journal);
1490 /* Finally, mark the journal as really needing no recovery.
1491 * This sets s_start==0 in the underlying superblock, which is
1492 * the magic code for a fully-recovered superblock. Any future
1493 * commits of data to the journal will restore the current
1494 * s_start value. */
1495 spin_lock(&journal->j_state_lock);
1496 old_tail = journal->j_tail;
1497 journal->j_tail = 0;
1498 spin_unlock(&journal->j_state_lock);
1499 journal_update_superblock(journal, 1);
1500 spin_lock(&journal->j_state_lock);
1501 journal->j_tail = old_tail;
1503 J_ASSERT(!journal->j_running_transaction);
1504 J_ASSERT(!journal->j_committing_transaction);
1505 J_ASSERT(!journal->j_checkpoint_transactions);
1506 J_ASSERT(journal->j_head == journal->j_tail);
1507 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1508 spin_unlock(&journal->j_state_lock);
1509 return 0;
1513 * int journal_wipe() - Wipe journal contents
1514 * @journal: Journal to act on.
1515 * @write: flag (see below)
1517 * Wipe out all of the contents of a journal, safely. This will produce
1518 * a warning if the journal contains any valid recovery information.
1519 * Must be called between journal_init_*() and journal_load().
1521 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1522 * we merely suppress recovery.
1525 int journal_wipe(journal_t *journal, int write)
1527 int err = 0;
1529 J_ASSERT (!(journal->j_flags & JFS_LOADED));
1531 err = load_superblock(journal);
1532 if (err)
1533 return err;
1535 if (!journal->j_tail)
1536 goto no_recovery;
1538 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1539 write ? "Clearing" : "Ignoring");
1541 err = journal_skip_recovery(journal);
1542 if (write)
1543 journal_update_superblock(journal, 1);
1545 no_recovery:
1546 return err;
1550 * journal_dev_name: format a character string to describe on what
1551 * device this journal is present.
1554 static const char *journal_dev_name(journal_t *journal, char *buffer)
1556 struct block_device *bdev;
1558 if (journal->j_inode)
1559 bdev = journal->j_inode->i_sb->s_bdev;
1560 else
1561 bdev = journal->j_dev;
1563 return bdevname(bdev, buffer);
1567 * Journal abort has very specific semantics, which we describe
1568 * for journal abort.
1570 * Two internal function, which provide abort to te jbd layer
1571 * itself are here.
1575 * Quick version for internal journal use (doesn't lock the journal).
1576 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1577 * and don't attempt to make any other journal updates.
1579 static void __journal_abort_hard(journal_t *journal)
1581 transaction_t *transaction;
1582 char b[BDEVNAME_SIZE];
1584 if (journal->j_flags & JFS_ABORT)
1585 return;
1587 printk(KERN_ERR "Aborting journal on device %s.\n",
1588 journal_dev_name(journal, b));
1590 spin_lock(&journal->j_state_lock);
1591 journal->j_flags |= JFS_ABORT;
1592 transaction = journal->j_running_transaction;
1593 if (transaction)
1594 __log_start_commit(journal, transaction->t_tid);
1595 spin_unlock(&journal->j_state_lock);
1598 /* Soft abort: record the abort error status in the journal superblock,
1599 * but don't do any other IO. */
1600 static void __journal_abort_soft (journal_t *journal, int errno)
1602 if (journal->j_flags & JFS_ABORT)
1603 return;
1605 if (!journal->j_errno)
1606 journal->j_errno = errno;
1608 __journal_abort_hard(journal);
1610 if (errno)
1611 journal_update_superblock(journal, 1);
1615 * void journal_abort () - Shutdown the journal immediately.
1616 * @journal: the journal to shutdown.
1617 * @errno: an error number to record in the journal indicating
1618 * the reason for the shutdown.
1620 * Perform a complete, immediate shutdown of the ENTIRE
1621 * journal (not of a single transaction). This operation cannot be
1622 * undone without closing and reopening the journal.
1624 * The journal_abort function is intended to support higher level error
1625 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1626 * mode.
1628 * Journal abort has very specific semantics. Any existing dirty,
1629 * unjournaled buffers in the main filesystem will still be written to
1630 * disk by bdflush, but the journaling mechanism will be suspended
1631 * immediately and no further transaction commits will be honoured.
1633 * Any dirty, journaled buffers will be written back to disk without
1634 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1635 * filesystem, but we _do_ attempt to leave as much data as possible
1636 * behind for fsck to use for cleanup.
1638 * Any attempt to get a new transaction handle on a journal which is in
1639 * ABORT state will just result in an -EROFS error return. A
1640 * journal_stop on an existing handle will return -EIO if we have
1641 * entered abort state during the update.
1643 * Recursive transactions are not disturbed by journal abort until the
1644 * final journal_stop, which will receive the -EIO error.
1646 * Finally, the journal_abort call allows the caller to supply an errno
1647 * which will be recorded (if possible) in the journal superblock. This
1648 * allows a client to record failure conditions in the middle of a
1649 * transaction without having to complete the transaction to record the
1650 * failure to disk. ext3_error, for example, now uses this
1651 * functionality.
1653 * Errors which originate from within the journaling layer will NOT
1654 * supply an errno; a null errno implies that absolutely no further
1655 * writes are done to the journal (unless there are any already in
1656 * progress).
1660 void journal_abort(journal_t *journal, int errno)
1662 __journal_abort_soft(journal, errno);
1666 * int journal_errno () - returns the journal's error state.
1667 * @journal: journal to examine.
1669 * This is the errno numbet set with journal_abort(), the last
1670 * time the journal was mounted - if the journal was stopped
1671 * without calling abort this will be 0.
1673 * If the journal has been aborted on this mount time -EROFS will
1674 * be returned.
1676 int journal_errno(journal_t *journal)
1678 int err;
1680 spin_lock(&journal->j_state_lock);
1681 if (journal->j_flags & JFS_ABORT)
1682 err = -EROFS;
1683 else
1684 err = journal->j_errno;
1685 spin_unlock(&journal->j_state_lock);
1686 return err;
1690 * int journal_clear_err () - clears the journal's error state
1691 * @journal: journal to act on.
1693 * An error must be cleared or Acked to take a FS out of readonly
1694 * mode.
1696 int journal_clear_err(journal_t *journal)
1698 int err = 0;
1700 spin_lock(&journal->j_state_lock);
1701 if (journal->j_flags & JFS_ABORT)
1702 err = -EROFS;
1703 else
1704 journal->j_errno = 0;
1705 spin_unlock(&journal->j_state_lock);
1706 return err;
1710 * void journal_ack_err() - Ack journal err.
1711 * @journal: journal to act on.
1713 * An error must be cleared or Acked to take a FS out of readonly
1714 * mode.
1716 void journal_ack_err(journal_t *journal)
1718 spin_lock(&journal->j_state_lock);
1719 if (journal->j_errno)
1720 journal->j_flags |= JFS_ACK_ERR;
1721 spin_unlock(&journal->j_state_lock);
1724 int journal_blocks_per_page(struct inode *inode)
1726 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1730 * Journal_head storage management
1732 static struct kmem_cache *journal_head_cache;
1733 #ifdef CONFIG_JBD_DEBUG
1734 static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1735 #endif
1737 static int journal_init_journal_head_cache(void)
1739 int retval;
1741 J_ASSERT(journal_head_cache == NULL);
1742 journal_head_cache = kmem_cache_create("journal_head",
1743 sizeof(struct journal_head),
1744 0, /* offset */
1745 SLAB_TEMPORARY, /* flags */
1746 NULL); /* ctor */
1747 retval = 0;
1748 if (!journal_head_cache) {
1749 retval = -ENOMEM;
1750 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1752 return retval;
1755 static void journal_destroy_journal_head_cache(void)
1757 if (journal_head_cache) {
1758 kmem_cache_destroy(journal_head_cache);
1759 journal_head_cache = NULL;
1764 * journal_head splicing and dicing
1766 static struct journal_head *journal_alloc_journal_head(void)
1768 struct journal_head *ret;
1770 #ifdef CONFIG_JBD_DEBUG
1771 atomic_inc(&nr_journal_heads);
1772 #endif
1773 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
1774 if (ret == NULL) {
1775 jbd_debug(1, "out of memory for journal_head\n");
1776 printk_ratelimited(KERN_NOTICE "ENOMEM in %s, retrying.\n",
1777 __func__);
1779 while (ret == NULL) {
1780 yield();
1781 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
1784 return ret;
1787 static void journal_free_journal_head(struct journal_head *jh)
1789 #ifdef CONFIG_JBD_DEBUG
1790 atomic_dec(&nr_journal_heads);
1791 memset(jh, JBD_POISON_FREE, sizeof(*jh));
1792 #endif
1793 kmem_cache_free(journal_head_cache, jh);
1797 * A journal_head is attached to a buffer_head whenever JBD has an
1798 * interest in the buffer.
1800 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
1801 * is set. This bit is tested in core kernel code where we need to take
1802 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
1803 * there.
1805 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
1807 * When a buffer has its BH_JBD bit set it is immune from being released by
1808 * core kernel code, mainly via ->b_count.
1810 * A journal_head may be detached from its buffer_head when the journal_head's
1811 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
1812 * Various places in JBD call journal_remove_journal_head() to indicate that the
1813 * journal_head can be dropped if needed.
1815 * Various places in the kernel want to attach a journal_head to a buffer_head
1816 * _before_ attaching the journal_head to a transaction. To protect the
1817 * journal_head in this situation, journal_add_journal_head elevates the
1818 * journal_head's b_jcount refcount by one. The caller must call
1819 * journal_put_journal_head() to undo this.
1821 * So the typical usage would be:
1823 * (Attach a journal_head if needed. Increments b_jcount)
1824 * struct journal_head *jh = journal_add_journal_head(bh);
1825 * ...
1826 * jh->b_transaction = xxx;
1827 * journal_put_journal_head(jh);
1829 * Now, the journal_head's b_jcount is zero, but it is safe from being released
1830 * because it has a non-zero b_transaction.
1834 * Give a buffer_head a journal_head.
1836 * Doesn't need the journal lock.
1837 * May sleep.
1839 struct journal_head *journal_add_journal_head(struct buffer_head *bh)
1841 struct journal_head *jh;
1842 struct journal_head *new_jh = NULL;
1844 repeat:
1845 if (!buffer_jbd(bh)) {
1846 new_jh = journal_alloc_journal_head();
1847 memset(new_jh, 0, sizeof(*new_jh));
1850 jbd_lock_bh_journal_head(bh);
1851 if (buffer_jbd(bh)) {
1852 jh = bh2jh(bh);
1853 } else {
1854 J_ASSERT_BH(bh,
1855 (atomic_read(&bh->b_count) > 0) ||
1856 (bh->b_page && bh->b_page->mapping));
1858 if (!new_jh) {
1859 jbd_unlock_bh_journal_head(bh);
1860 goto repeat;
1863 jh = new_jh;
1864 new_jh = NULL; /* We consumed it */
1865 set_buffer_jbd(bh);
1866 bh->b_private = jh;
1867 jh->b_bh = bh;
1868 get_bh(bh);
1869 BUFFER_TRACE(bh, "added journal_head");
1871 jh->b_jcount++;
1872 jbd_unlock_bh_journal_head(bh);
1873 if (new_jh)
1874 journal_free_journal_head(new_jh);
1875 return bh->b_private;
1879 * Grab a ref against this buffer_head's journal_head. If it ended up not
1880 * having a journal_head, return NULL
1882 struct journal_head *journal_grab_journal_head(struct buffer_head *bh)
1884 struct journal_head *jh = NULL;
1886 jbd_lock_bh_journal_head(bh);
1887 if (buffer_jbd(bh)) {
1888 jh = bh2jh(bh);
1889 jh->b_jcount++;
1891 jbd_unlock_bh_journal_head(bh);
1892 return jh;
1895 static void __journal_remove_journal_head(struct buffer_head *bh)
1897 struct journal_head *jh = bh2jh(bh);
1899 J_ASSERT_JH(jh, jh->b_jcount >= 0);
1901 get_bh(bh);
1902 if (jh->b_jcount == 0) {
1903 if (jh->b_transaction == NULL &&
1904 jh->b_next_transaction == NULL &&
1905 jh->b_cp_transaction == NULL) {
1906 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
1907 J_ASSERT_BH(bh, buffer_jbd(bh));
1908 J_ASSERT_BH(bh, jh2bh(jh) == bh);
1909 BUFFER_TRACE(bh, "remove journal_head");
1910 if (jh->b_frozen_data) {
1911 printk(KERN_WARNING "%s: freeing "
1912 "b_frozen_data\n",
1913 __func__);
1914 jbd_free(jh->b_frozen_data, bh->b_size);
1916 if (jh->b_committed_data) {
1917 printk(KERN_WARNING "%s: freeing "
1918 "b_committed_data\n",
1919 __func__);
1920 jbd_free(jh->b_committed_data, bh->b_size);
1922 bh->b_private = NULL;
1923 jh->b_bh = NULL; /* debug, really */
1924 clear_buffer_jbd(bh);
1925 __brelse(bh);
1926 journal_free_journal_head(jh);
1927 } else {
1928 BUFFER_TRACE(bh, "journal_head was locked");
1934 * journal_remove_journal_head(): if the buffer isn't attached to a transaction
1935 * and has a zero b_jcount then remove and release its journal_head. If we did
1936 * see that the buffer is not used by any transaction we also "logically"
1937 * decrement ->b_count.
1939 * We in fact take an additional increment on ->b_count as a convenience,
1940 * because the caller usually wants to do additional things with the bh
1941 * after calling here.
1942 * The caller of journal_remove_journal_head() *must* run __brelse(bh) at some
1943 * time. Once the caller has run __brelse(), the buffer is eligible for
1944 * reaping by try_to_free_buffers().
1946 void journal_remove_journal_head(struct buffer_head *bh)
1948 jbd_lock_bh_journal_head(bh);
1949 __journal_remove_journal_head(bh);
1950 jbd_unlock_bh_journal_head(bh);
1954 * Drop a reference on the passed journal_head. If it fell to zero then try to
1955 * release the journal_head from the buffer_head.
1957 void journal_put_journal_head(struct journal_head *jh)
1959 struct buffer_head *bh = jh2bh(jh);
1961 jbd_lock_bh_journal_head(bh);
1962 J_ASSERT_JH(jh, jh->b_jcount > 0);
1963 --jh->b_jcount;
1964 if (!jh->b_jcount && !jh->b_transaction) {
1965 __journal_remove_journal_head(bh);
1966 __brelse(bh);
1968 jbd_unlock_bh_journal_head(bh);
1972 * debugfs tunables
1974 #ifdef CONFIG_JBD_DEBUG
1976 u8 journal_enable_debug __read_mostly;
1977 EXPORT_SYMBOL(journal_enable_debug);
1979 static struct dentry *jbd_debugfs_dir;
1980 static struct dentry *jbd_debug;
1982 static void __init jbd_create_debugfs_entry(void)
1984 jbd_debugfs_dir = debugfs_create_dir("jbd", NULL);
1985 if (jbd_debugfs_dir)
1986 jbd_debug = debugfs_create_u8("jbd-debug", S_IRUGO | S_IWUSR,
1987 jbd_debugfs_dir,
1988 &journal_enable_debug);
1991 static void __exit jbd_remove_debugfs_entry(void)
1993 debugfs_remove(jbd_debug);
1994 debugfs_remove(jbd_debugfs_dir);
1997 #else
1999 static inline void jbd_create_debugfs_entry(void)
2003 static inline void jbd_remove_debugfs_entry(void)
2007 #endif
2009 struct kmem_cache *jbd_handle_cache;
2011 static int __init journal_init_handle_cache(void)
2013 jbd_handle_cache = kmem_cache_create("journal_handle",
2014 sizeof(handle_t),
2015 0, /* offset */
2016 SLAB_TEMPORARY, /* flags */
2017 NULL); /* ctor */
2018 if (jbd_handle_cache == NULL) {
2019 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2020 return -ENOMEM;
2022 return 0;
2025 static void journal_destroy_handle_cache(void)
2027 if (jbd_handle_cache)
2028 kmem_cache_destroy(jbd_handle_cache);
2032 * Module startup and shutdown
2035 static int __init journal_init_caches(void)
2037 int ret;
2039 ret = journal_init_revoke_caches();
2040 if (ret == 0)
2041 ret = journal_init_journal_head_cache();
2042 if (ret == 0)
2043 ret = journal_init_handle_cache();
2044 return ret;
2047 static void journal_destroy_caches(void)
2049 journal_destroy_revoke_caches();
2050 journal_destroy_journal_head_cache();
2051 journal_destroy_handle_cache();
2054 static int __init journal_init(void)
2056 int ret;
2058 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2060 ret = journal_init_caches();
2061 if (ret != 0)
2062 journal_destroy_caches();
2063 jbd_create_debugfs_entry();
2064 return ret;
2067 static void __exit journal_exit(void)
2069 #ifdef CONFIG_JBD_DEBUG
2070 int n = atomic_read(&nr_journal_heads);
2071 if (n)
2072 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2073 #endif
2074 jbd_remove_debugfs_entry();
2075 journal_destroy_caches();
2078 MODULE_LICENSE("GPL");
2079 module_init(journal_init);
2080 module_exit(journal_exit);