jbd: Use printk_ratelimited() in journal_alloc_journal_head()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / jbd / journal.c
blobd7a86935553a919ccb6ce2d6be6b1418565972e4
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);
90 * Helper function used to manage commit timeouts
93 static void commit_timeout(unsigned long __data)
95 struct task_struct * p = (struct task_struct *) __data;
97 wake_up_process(p);
101 * kjournald: The main thread function used to manage a logging device
102 * journal.
104 * This kernel thread is responsible for two things:
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.
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.
116 static int kjournald(void *arg)
118 journal_t *journal = arg;
119 transaction_t *transaction;
122 * Set up an interval timer which can be used to trigger a commit wakeup
123 * after the commit interval expires
125 setup_timer(&journal->j_commit_timer, commit_timeout,
126 (unsigned long)current);
128 /* Record that the journal thread is running */
129 journal->j_task = current;
130 wake_up(&journal->j_wait_done_commit);
132 printk(KERN_INFO "kjournald starting. Commit interval %ld seconds\n",
133 journal->j_commit_interval / HZ);
136 * And now, wait forever for commit wakeup events.
138 spin_lock(&journal->j_state_lock);
140 loop:
141 if (journal->j_flags & JFS_UNMOUNT)
142 goto end_loop;
144 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
145 journal->j_commit_sequence, journal->j_commit_request);
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);
150 del_timer_sync(&journal->j_commit_timer);
151 journal_commit_transaction(journal);
152 spin_lock(&journal->j_state_lock);
153 goto loop;
156 wake_up(&journal->j_wait_done_commit);
157 if (freezing(current)) {
159 * The simpler the better. Flushing journal isn't a
160 * good idea, because that depends on threads that may
161 * be already stopped.
163 jbd_debug(1, "Now suspending kjournald\n");
164 spin_unlock(&journal->j_state_lock);
165 refrigerator();
166 spin_lock(&journal->j_state_lock);
167 } else {
169 * We assume on resume that commits are already there,
170 * so we don't sleep
172 DEFINE_WAIT(wait);
173 int should_sleep = 1;
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;
183 if (journal->j_flags & JFS_UNMOUNT)
184 should_sleep = 0;
185 if (should_sleep) {
186 spin_unlock(&journal->j_state_lock);
187 schedule();
188 spin_lock(&journal->j_state_lock);
190 finish_wait(&journal->j_wait_commit, &wait);
193 jbd_debug(1, "kjournald wakes\n");
196 * Were we woken up by a commit wakeup event?
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");
203 goto loop;
205 end_loop:
206 spin_unlock(&journal->j_state_lock);
207 del_timer_sync(&journal->j_commit_timer);
208 journal->j_task = NULL;
209 wake_up(&journal->j_wait_done_commit);
210 jbd_debug(1, "Journal thread exiting.\n");
211 return 0;
214 static int journal_start_thread(journal_t *journal)
216 struct task_struct *t;
218 t = kthread_run(kjournald, journal, "kjournald");
219 if (IS_ERR(t))
220 return PTR_ERR(t);
222 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
223 return 0;
226 static void journal_kill_thread(journal_t *journal)
228 spin_lock(&journal->j_state_lock);
229 journal->j_flags |= JFS_UNMOUNT;
231 while (journal->j_task) {
232 wake_up(&journal->j_wait_commit);
233 spin_unlock(&journal->j_state_lock);
234 wait_event(journal->j_wait_done_commit,
235 journal->j_task == NULL);
236 spin_lock(&journal->j_state_lock);
238 spin_unlock(&journal->j_state_lock);
242 * journal_write_metadata_buffer: write a metadata buffer to the journal.
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.
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.
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.
265 * The function returns a pointer to the buffer_heads to be used for IO.
267 * We assume that the journal has already been locked in this function.
269 * Return value:
270 * <0: Error
271 * >=0: Finished OK
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)
278 int journal_write_metadata_buffer(transaction_t *transaction,
279 struct journal_head *jh_in,
280 struct journal_head **jh_out,
281 unsigned int blocknr)
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);
292 journal_t *journal = transaction->t_journal;
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.
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.
303 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
305 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
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 */
313 * If a new transaction has already done a buffer copy-out, then
314 * we use that version of the data for the commit.
316 jbd_lock_bh_state(bh_in);
317 repeat:
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);
327 mapped_data = kmap_atomic(new_page, KM_USER0);
329 * Check for escaping
331 if (*((__be32 *)(mapped_data + new_offset)) ==
332 cpu_to_be32(JFS_MAGIC_NUMBER)) {
333 need_copy_out = 1;
334 do_escape = 1;
336 kunmap_atomic(mapped_data, KM_USER0);
339 * Do we need to do a data copy?
341 if (need_copy_out && !done_copy_out) {
342 char *tmp;
344 jbd_unlock_bh_state(bh_in);
345 tmp = jbd_alloc(bh_in->b_size, GFP_NOFS);
346 jbd_lock_bh_state(bh_in);
347 if (jh_in->b_frozen_data) {
348 jbd_free(tmp, bh_in->b_size);
349 goto repeat;
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);
357 new_page = virt_to_page(tmp);
358 new_offset = offset_in_page(tmp);
359 done_copy_out = 1;
363 * Did we need to do an escaping? Now we've done all the
364 * copying, we can finally do so.
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);
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);
380 *jh_out = new_jh;
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.
387 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
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);
393 JBUFFER_TRACE(new_jh, "file as BJ_IO");
394 journal_file_buffer(new_jh, transaction, BJ_IO);
396 return do_escape | (done_copy_out << 1);
400 * Allocation code for the journal file. Manage the space left in the
401 * journal, so that we can begin checkpointing when appropriate.
405 * __log_space_left: Return the number of free blocks left in the journal.
407 * Called with the journal already locked.
409 * Called under j_state_lock
412 int __log_space_left(journal_t *journal)
414 int left = journal->j_free;
416 assert_spin_locked(&journal->j_state_lock);
419 * Be pessimistic here about the number of those free blocks which
420 * might be required for log descriptor control blocks.
423 #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
425 left -= MIN_LOG_RESERVED_BLOCKS;
427 if (left <= 0)
428 return 0;
429 left -= (left >> 3);
430 return left;
434 * Called under j_state_lock. Returns true if a transaction commit was started.
436 int __log_start_commit(journal_t *journal, tid_t target)
439 * Are we already doing a recent enough commit?
441 if (!tid_geq(journal->j_commit_request, target)) {
443 * We want a new commit: OK, mark the request and wakup the
444 * commit thread. We do _not_ do the commit ourselves.
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;
454 return 0;
457 int log_start_commit(journal_t *journal, tid_t tid)
459 int ret;
461 spin_lock(&journal->j_state_lock);
462 ret = __log_start_commit(journal, tid);
463 spin_unlock(&journal->j_state_lock);
464 return ret;
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.
472 * We can only force the running transaction if we don't have an active handle;
473 * otherwise, we will deadlock.
475 * Returns true if a transaction was started.
477 int journal_force_commit_nested(journal_t *journal)
479 transaction_t *transaction = NULL;
480 tid_t tid;
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;
489 if (!transaction) {
490 spin_unlock(&journal->j_state_lock);
491 return 0; /* Nothing to retry */
494 tid = transaction->t_tid;
495 spin_unlock(&journal->j_state_lock);
496 log_wait_commit(journal, tid);
497 return 1;
501 * Start a commit of the current running transaction (if any). Returns true
502 * if a transaction is going to be committed (or is currently already
503 * committing), and fills its tid in at *ptid
505 int journal_start_commit(journal_t *journal, tid_t *ptid)
507 int ret = 0;
509 spin_lock(&journal->j_state_lock);
510 if (journal->j_running_transaction) {
511 tid_t tid = journal->j_running_transaction->t_tid;
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)
517 *ptid = tid;
518 ret = 1;
519 } else if (journal->j_committing_transaction) {
521 * If ext3_write_super() recently started a commit, then we
522 * have to wait for completion of that transaction
524 if (ptid)
525 *ptid = journal->j_committing_transaction->t_tid;
526 ret = 1;
528 spin_unlock(&journal->j_state_lock);
529 return ret;
533 * Wait for a specified commit to complete.
534 * The caller may not hold the journal lock.
536 int log_wait_commit(journal_t *journal, tid_t tid)
538 int err = 0;
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",
545 __func__, journal->j_commit_request, tid);
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);
559 spin_unlock(&journal->j_state_lock);
561 if (unlikely(is_journal_aborted(journal))) {
562 printk(KERN_EMERG "journal commit I/O error\n");
563 err = -EIO;
565 return err;
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.
574 int journal_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
576 int ret = 0;
577 transaction_t *commit_trans;
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;
586 * Transaction is being committed and we already proceeded to
587 * writing commit record?
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;
594 out:
595 spin_unlock(&journal->j_state_lock);
596 return ret;
598 EXPORT_SYMBOL(journal_trans_will_send_data_barrier);
601 * Log buffer allocation routines:
604 int journal_next_log_block(journal_t *journal, unsigned int *retp)
606 unsigned int blocknr;
608 spin_lock(&journal->j_state_lock);
609 J_ASSERT(journal->j_free > 1);
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);
621 * Conversion of logical to physical block numbers for the journal
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.
627 int journal_bmap(journal_t *journal, unsigned int blocknr,
628 unsigned int *retp)
630 int err = 0;
631 unsigned int ret;
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];
640 printk(KERN_ALERT "%s: journal block not found "
641 "at offset %u on %s\n",
642 __func__,
643 blocknr,
644 bdevname(journal->j_dev, b));
645 err = -EIO;
646 __journal_abort_soft(journal, err);
648 } else {
649 *retp = blocknr; /* +journal->j_blk_offset */
651 return err;
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.
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.
664 struct journal_head *journal_get_descriptor_buffer(journal_t *journal)
666 struct buffer_head *bh;
667 unsigned int blocknr;
668 int err;
670 err = journal_next_log_block(journal, &blocknr);
672 if (err)
673 return NULL;
675 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
676 if (!bh)
677 return NULL;
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);
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. */
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. */
695 static journal_t * journal_init_common (void)
697 journal_t *journal;
698 int err;
700 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
701 if (!journal)
702 goto fail;
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);
710 mutex_init(&journal->j_barrier);
711 mutex_init(&journal->j_checkpoint_mutex);
712 spin_lock_init(&journal->j_revoke_lock);
713 spin_lock_init(&journal->j_list_lock);
714 spin_lock_init(&journal->j_state_lock);
716 journal->j_commit_interval = (HZ * JBD_DEFAULT_MAX_COMMIT_AGE);
718 /* The journal is marked for error until we succeed with recovery! */
719 journal->j_flags = JFS_ABORT;
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;
727 return journal;
728 fail:
729 return NULL;
732 /* journal_init_dev and journal_init_inode:
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.
742 * journal_t * journal_init_dev() - creates and initialises a journal structure
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.
746 * @len: Length of the journal in blocks.
747 * @blocksize: blocksize of journalling device
749 * Returns: a newly created journal_t *
751 * journal_init_dev creates a journal which maps a fixed contiguous
752 * range of blocks on an arbitrary block device.
755 journal_t * journal_init_dev(struct block_device *bdev,
756 struct block_device *fs_dev,
757 int start, int len, int blocksize)
759 journal_t *journal = journal_init_common();
760 struct buffer_head *bh;
761 int n;
763 if (!journal)
764 return NULL;
766 /* journal descriptor can store up to n blocks -bzzz */
767 journal->j_blocksize = blocksize;
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",
773 __func__);
774 goto out_err;
776 journal->j_dev = bdev;
777 journal->j_fs_dev = fs_dev;
778 journal->j_blk_offset = start;
779 journal->j_maxlen = len;
781 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
782 if (!bh) {
783 printk(KERN_ERR
784 "%s: Cannot get buffer for journal superblock\n",
785 __func__);
786 goto out_err;
788 journal->j_sb_buffer = bh;
789 journal->j_superblock = (journal_superblock_t *)bh->b_data;
791 return journal;
792 out_err:
793 kfree(journal->j_wbuf);
794 kfree(journal);
795 return NULL;
799 * journal_t * journal_init_inode () - creates a journal which maps to a inode.
800 * @inode: An inode to create the journal in
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.
806 journal_t * journal_init_inode (struct inode *inode)
808 struct buffer_head *bh;
809 journal_t *journal = journal_init_common();
810 int err;
811 int n;
812 unsigned int blocknr;
814 if (!journal)
815 return NULL;
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",
821 journal, inode->i_sb->s_id, inode->i_ino,
822 (long long) inode->i_size,
823 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
825 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
826 journal->j_blocksize = inode->i_sb->s_blocksize;
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",
834 __func__);
835 goto out_err;
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",
842 __func__);
843 goto out_err;
846 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
847 if (!bh) {
848 printk(KERN_ERR
849 "%s: Cannot get buffer for journal superblock\n",
850 __func__);
851 goto out_err;
853 journal->j_sb_buffer = bh;
854 journal->j_superblock = (journal_superblock_t *)bh->b_data;
856 return journal;
857 out_err:
858 kfree(journal->j_wbuf);
859 kfree(journal);
860 return NULL;
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
866 * back a bogus superblock.
868 static void journal_fail_superblock (journal_t *journal)
870 struct buffer_head *bh = journal->j_sb_buffer;
871 brelse(bh);
872 journal->j_sb_buffer = NULL;
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.
882 static int journal_reset(journal_t *journal)
884 journal_superblock_t *sb = journal->j_superblock;
885 unsigned int first, last;
887 first = be32_to_cpu(sb->s_first);
888 last = be32_to_cpu(sb->s_maxlen);
889 if (first + JFS_MIN_JOURNAL_BLOCKS > last + 1) {
890 printk(KERN_ERR "JBD: Journal too short (blocks %u-%u).\n",
891 first, last);
892 journal_fail_superblock(journal);
893 return -EINVAL;
896 journal->j_first = first;
897 journal->j_last = last;
899 journal->j_head = first;
900 journal->j_tail = first;
901 journal->j_free = last - first;
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;
907 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
909 /* Add the dynamic fields and write it to disk. */
910 journal_update_superblock(journal, 1);
911 return journal_start_thread(journal);
915 * int journal_create() - Initialise the new journal file
916 * @journal: Journal to create. This structure must have been initialised
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
920 * journal fields from scratch.
922 int journal_create(journal_t *journal)
924 unsigned int blocknr;
925 struct buffer_head *bh;
926 journal_superblock_t *sb;
927 int i, err;
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;
936 if (journal->j_inode == NULL) {
938 * We don't know what block to start at!
940 printk(KERN_EMERG
941 "%s: creation of journal on external device!\n",
942 __func__);
943 BUG();
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);
964 sync_blockdev(journal->j_dev);
965 jbd_debug(1, "JBD: journal cleared.\n");
967 /* OK, fill in the initial static fields in the new superblock */
968 sb = journal->j_superblock;
970 sb->s_header.h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
971 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
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);
977 journal->j_transaction_sequence = 1;
979 journal->j_flags &= ~JFS_ABORT;
980 journal->j_format_version = 2;
982 return journal_reset(journal);
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.
990 * Update a journal's dynamic superblock fields and write it to disk,
991 * optionally waiting for the IO to complete.
993 void journal_update_superblock(journal_t *journal, int wait)
995 journal_superblock_t *sb = journal->j_superblock;
996 struct buffer_head *bh = journal->j_sb_buffer;
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.
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 "
1008 "(start %u, seq %d, errno %d)\n",
1009 journal->j_tail, journal->j_tail_sequence,
1010 journal->j_errno);
1011 goto out;
1014 spin_lock(&journal->j_state_lock);
1015 jbd_debug(1,"JBD: updating superblock (start %u, seq %d, errno %d)\n",
1016 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
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);
1023 BUFFER_TRACE(bh, "marking dirty");
1024 mark_buffer_dirty(bh);
1025 if (wait)
1026 sync_dirty_buffer(bh);
1027 else
1028 write_dirty_buffer(bh, WRITE);
1030 out:
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. */
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);
1044 * Read the superblock for a given journal, performing initial
1045 * validation of the format.
1048 static int journal_get_superblock(journal_t *journal)
1050 struct buffer_head *bh;
1051 journal_superblock_t *sb;
1052 int err = -EIO;
1054 bh = journal->j_sb_buffer;
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;
1067 sb = journal->j_superblock;
1069 err = -EINVAL;
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;
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;
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;
1096 return 0;
1098 out:
1099 journal_fail_superblock(journal);
1100 return err;
1104 * Load the on-disk journal superblock and read the key fields into the
1105 * journal_t.
1108 static int load_superblock(journal_t *journal)
1110 int err;
1111 journal_superblock_t *sb;
1113 err = journal_get_superblock(journal);
1114 if (err)
1115 return err;
1117 sb = journal->j_superblock;
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);
1125 return 0;
1130 * int journal_load() - Read journal from disk.
1131 * @journal: Journal to act on.
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.
1137 int journal_load(journal_t *journal)
1139 int err;
1140 journal_superblock_t *sb;
1142 err = load_superblock(journal);
1143 if (err)
1144 return err;
1146 sb = journal->j_superblock;
1147 /* If this is a V2 superblock, then we have to check the
1148 * features flags on it. */
1150 if (journal->j_format_version >= 2) {
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;
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;
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;
1172 journal->j_flags &= ~JFS_ABORT;
1173 journal->j_flags |= JFS_LOADED;
1174 return 0;
1176 recovery_error:
1177 printk (KERN_WARNING "JBD: recovery failed\n");
1178 return -EIO;
1182 * void journal_destroy() - Release a journal_t structure.
1183 * @journal: Journal to act on.
1185 * Release a journal_t structure once it is no longer in use by the
1186 * journaled object.
1187 * Return <0 if we couldn't clean up the journal.
1189 int journal_destroy(journal_t *journal)
1191 int err = 0;
1194 /* Wait for the commit thread to wake up and die. */
1195 journal_kill_thread(journal);
1197 /* Force a final log commit */
1198 if (journal->j_running_transaction)
1199 journal_commit_transaction(journal);
1201 /* Force any old transactions to disk */
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);
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);
1216 if (journal->j_sb_buffer) {
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;
1226 brelse(journal->j_sb_buffer);
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);
1236 return err;
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
1247 * Check whether the journal uses all of a given set of
1248 * features. Return true (non-zero) if it does.
1251 int journal_check_used_features (journal_t *journal, unsigned long compat,
1252 unsigned long ro, unsigned long incompat)
1254 journal_superblock_t *sb;
1256 if (!compat && !ro && !incompat)
1257 return 1;
1258 if (journal->j_format_version == 1)
1259 return 0;
1261 sb = journal->j_superblock;
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;
1268 return 0;
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
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. */
1282 int journal_check_available_features (journal_t *journal, unsigned long compat,
1283 unsigned long ro, unsigned long incompat)
1285 if (!compat && !ro && !incompat)
1286 return 1;
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. */
1292 if (journal->j_format_version != 2)
1293 return 0;
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;
1300 return 0;
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
1310 * Mark a given journal feature as present on the
1311 * superblock. Returns true if the requested features could be set.
1315 int journal_set_features (journal_t *journal, unsigned long compat,
1316 unsigned long ro, unsigned long incompat)
1318 journal_superblock_t *sb;
1320 if (journal_check_used_features(journal, compat, ro, incompat))
1321 return 1;
1323 if (!journal_check_available_features(journal, compat, ro, incompat))
1324 return 0;
1326 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1327 compat, ro, incompat);
1329 sb = journal->j_superblock;
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);
1335 return 1;
1340 * int journal_update_format () - Update on-disk journal structure.
1341 * @journal: Journal to act on.
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.
1346 int journal_update_format (journal_t *journal)
1348 journal_superblock_t *sb;
1349 int err;
1351 err = journal_get_superblock(journal);
1352 if (err)
1353 return err;
1355 sb = journal->j_superblock;
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;
1365 return -EINVAL;
1368 static int journal_convert_superblock_v1(journal_t *journal,
1369 journal_superblock_t *sb)
1371 int offset, blocksize;
1372 struct buffer_head *bh;
1374 printk(KERN_WARNING
1375 "JBD: Converting superblock from version 1 to 2.\n");
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);
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;
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;
1395 * int journal_flush () - Flush journal
1396 * @journal: Journal to act on.
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.
1403 int journal_flush(journal_t *journal)
1405 int err = 0;
1406 transaction_t *transaction = NULL;
1407 unsigned int old_tail;
1409 spin_lock(&journal->j_state_lock);
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;
1418 /* Wait for the log commit to complete... */
1419 if (transaction) {
1420 tid_t tid = transaction->t_tid;
1422 spin_unlock(&journal->j_state_lock);
1423 log_wait_commit(journal, tid);
1424 } else {
1425 spin_unlock(&journal->j_state_lock);
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);
1432 mutex_lock(&journal->j_checkpoint_mutex);
1433 err = log_do_checkpoint(journal);
1434 mutex_unlock(&journal->j_checkpoint_mutex);
1435 spin_lock(&journal->j_list_lock);
1437 spin_unlock(&journal->j_list_lock);
1439 if (is_journal_aborted(journal))
1440 return -EIO;
1442 cleanup_journal_tail(journal);
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;
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);
1463 return 0;
1467 * int journal_wipe() - Wipe journal contents
1468 * @journal: Journal to act on.
1469 * @write: flag (see below)
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().
1475 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1476 * we merely suppress recovery.
1479 int journal_wipe(journal_t *journal, int write)
1481 int err = 0;
1483 J_ASSERT (!(journal->j_flags & JFS_LOADED));
1485 err = load_superblock(journal);
1486 if (err)
1487 return err;
1489 if (!journal->j_tail)
1490 goto no_recovery;
1492 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1493 write ? "Clearing" : "Ignoring");
1495 err = journal_skip_recovery(journal);
1496 if (write)
1497 journal_update_superblock(journal, 1);
1499 no_recovery:
1500 return err;
1504 * journal_dev_name: format a character string to describe on what
1505 * device this journal is present.
1508 static const char *journal_dev_name(journal_t *journal, char *buffer)
1510 struct block_device *bdev;
1512 if (journal->j_inode)
1513 bdev = journal->j_inode->i_sb->s_bdev;
1514 else
1515 bdev = journal->j_dev;
1517 return bdevname(bdev, buffer);
1521 * Journal abort has very specific semantics, which we describe
1522 * for journal abort.
1524 * Two internal function, which provide abort to te jbd layer
1525 * itself are here.
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.
1533 static void __journal_abort_hard(journal_t *journal)
1535 transaction_t *transaction;
1536 char b[BDEVNAME_SIZE];
1538 if (journal->j_flags & JFS_ABORT)
1539 return;
1541 printk(KERN_ERR "Aborting journal on device %s.\n",
1542 journal_dev_name(journal, b));
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);
1552 /* Soft abort: record the abort error status in the journal superblock,
1553 * but don't do any other IO. */
1554 static void __journal_abort_soft (journal_t *journal, int errno)
1556 if (journal->j_flags & JFS_ABORT)
1557 return;
1559 if (!journal->j_errno)
1560 journal->j_errno = errno;
1562 __journal_abort_hard(journal);
1564 if (errno)
1565 journal_update_superblock(journal, 1);
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.
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.
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.
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.
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.
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.
1597 * Recursive transactions are not disturbed by journal abort until the
1598 * final journal_stop, which will receive the -EIO error.
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.
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).
1614 void journal_abort(journal_t *journal, int errno)
1616 __journal_abort_soft(journal, errno);
1620 * int journal_errno () - returns the journal's error state.
1621 * @journal: journal to examine.
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.
1627 * If the journal has been aborted on this mount time -EROFS will
1628 * be returned.
1630 int journal_errno(journal_t *journal)
1632 int err;
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;
1644 * int journal_clear_err () - clears the journal's error state
1645 * @journal: journal to act on.
1647 * An error must be cleared or Acked to take a FS out of readonly
1648 * mode.
1650 int journal_clear_err(journal_t *journal)
1652 int err = 0;
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;
1664 * void journal_ack_err() - Ack journal err.
1665 * @journal: journal to act on.
1667 * An error must be cleared or Acked to take a FS out of readonly
1668 * mode.
1670 void journal_ack_err(journal_t *journal)
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);
1678 int journal_blocks_per_page(struct inode *inode)
1680 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1684 * Journal_head storage management
1686 static struct kmem_cache *journal_head_cache;
1687 #ifdef CONFIG_JBD_DEBUG
1688 static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1689 #endif
1691 static int journal_init_journal_head_cache(void)
1693 int retval;
1695 J_ASSERT(journal_head_cache == NULL);
1696 journal_head_cache = kmem_cache_create("journal_head",
1697 sizeof(struct journal_head),
1698 0, /* offset */
1699 SLAB_TEMPORARY, /* flags */
1700 NULL); /* ctor */
1701 retval = 0;
1702 if (!journal_head_cache) {
1703 retval = -ENOMEM;
1704 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1706 return retval;
1709 static void journal_destroy_journal_head_cache(void)
1711 if (journal_head_cache) {
1712 kmem_cache_destroy(journal_head_cache);
1713 journal_head_cache = NULL;
1718 * journal_head splicing and dicing
1720 static struct journal_head *journal_alloc_journal_head(void)
1722 struct journal_head *ret;
1724 #ifdef CONFIG_JBD_DEBUG
1725 atomic_inc(&nr_journal_heads);
1726 #endif
1727 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
1728 if (ret == NULL) {
1729 jbd_debug(1, "out of memory for journal_head\n");
1730 printk_ratelimited(KERN_NOTICE "ENOMEM in %s, retrying.\n",
1731 __func__);
1733 while (ret == NULL) {
1734 yield();
1735 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
1738 return ret;
1741 static void journal_free_journal_head(struct journal_head *jh)
1743 #ifdef CONFIG_JBD_DEBUG
1744 atomic_dec(&nr_journal_heads);
1745 memset(jh, JBD_POISON_FREE, sizeof(*jh));
1746 #endif
1747 kmem_cache_free(journal_head_cache, jh);
1751 * A journal_head is attached to a buffer_head whenever JBD has an
1752 * interest in the buffer.
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.
1759 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
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.
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.
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.
1775 * So the typical usage would be:
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);
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.
1788 * Give a buffer_head a journal_head.
1790 * Doesn't need the journal lock.
1791 * May sleep.
1793 struct journal_head *journal_add_journal_head(struct buffer_head *bh)
1795 struct journal_head *jh;
1796 struct journal_head *new_jh = NULL;
1798 repeat:
1799 if (!buffer_jbd(bh)) {
1800 new_jh = journal_alloc_journal_head();
1801 memset(new_jh, 0, sizeof(*new_jh));
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));
1812 if (!new_jh) {
1813 jbd_unlock_bh_journal_head(bh);
1814 goto repeat;
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");
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;
1833 * Grab a ref against this buffer_head's journal_head. If it ended up not
1834 * having a journal_head, return NULL
1836 struct journal_head *journal_grab_journal_head(struct buffer_head *bh)
1838 struct journal_head *jh = NULL;
1840 jbd_lock_bh_journal_head(bh);
1841 if (buffer_jbd(bh)) {
1842 jh = bh2jh(bh);
1843 jh->b_jcount++;
1845 jbd_unlock_bh_journal_head(bh);
1846 return jh;
1849 static void __journal_remove_journal_head(struct buffer_head *bh)
1851 struct journal_head *jh = bh2jh(bh);
1853 J_ASSERT_JH(jh, jh->b_jcount >= 0);
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",
1867 __func__);
1868 jbd_free(jh->b_frozen_data, bh->b_size);
1870 if (jh->b_committed_data) {
1871 printk(KERN_WARNING "%s: freeing "
1872 "b_committed_data\n",
1873 __func__);
1874 jbd_free(jh->b_committed_data, bh->b_size);
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");
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.
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().
1900 void journal_remove_journal_head(struct buffer_head *bh)
1902 jbd_lock_bh_journal_head(bh);
1903 __journal_remove_journal_head(bh);
1904 jbd_unlock_bh_journal_head(bh);
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.
1911 void journal_put_journal_head(struct journal_head *jh)
1913 struct buffer_head *bh = jh2bh(jh);
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);
1922 jbd_unlock_bh_journal_head(bh);
1926 * debugfs tunables
1928 #ifdef CONFIG_JBD_DEBUG
1930 u8 journal_enable_debug __read_mostly;
1931 EXPORT_SYMBOL(journal_enable_debug);
1933 static struct dentry *jbd_debugfs_dir;
1934 static struct dentry *jbd_debug;
1936 static void __init jbd_create_debugfs_entry(void)
1938 jbd_debugfs_dir = debugfs_create_dir("jbd", NULL);
1939 if (jbd_debugfs_dir)
1940 jbd_debug = debugfs_create_u8("jbd-debug", S_IRUGO | S_IWUSR,
1941 jbd_debugfs_dir,
1942 &journal_enable_debug);
1945 static void __exit jbd_remove_debugfs_entry(void)
1947 debugfs_remove(jbd_debug);
1948 debugfs_remove(jbd_debugfs_dir);
1951 #else
1953 static inline void jbd_create_debugfs_entry(void)
1957 static inline void jbd_remove_debugfs_entry(void)
1961 #endif
1963 struct kmem_cache *jbd_handle_cache;
1965 static int __init journal_init_handle_cache(void)
1967 jbd_handle_cache = kmem_cache_create("journal_handle",
1968 sizeof(handle_t),
1969 0, /* offset */
1970 SLAB_TEMPORARY, /* flags */
1971 NULL); /* ctor */
1972 if (jbd_handle_cache == NULL) {
1973 printk(KERN_EMERG "JBD: failed to create handle cache\n");
1974 return -ENOMEM;
1976 return 0;
1979 static void journal_destroy_handle_cache(void)
1981 if (jbd_handle_cache)
1982 kmem_cache_destroy(jbd_handle_cache);
1986 * Module startup and shutdown
1989 static int __init journal_init_caches(void)
1991 int ret;
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;
2001 static void journal_destroy_caches(void)
2003 journal_destroy_revoke_caches();
2004 journal_destroy_journal_head_cache();
2005 journal_destroy_handle_cache();
2008 static int __init journal_init(void)
2010 int ret;
2012 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2014 ret = journal_init_caches();
2015 if (ret != 0)
2016 journal_destroy_caches();
2017 jbd_create_debugfs_entry();
2018 return ret;
2021 static void __exit journal_exit(void)
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
2028 jbd_remove_debugfs_entry();
2029 journal_destroy_caches();
2032 MODULE_LICENSE("GPL");
2033 module_init(journal_init);
2034 module_exit(journal_exit);